]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/exp_ch4.adb
[multiple changes]
[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-2011, 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_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Fixd; use Exp_Fixd;
40 with Exp_Intr; use Exp_Intr;
41 with Exp_Pakd; use Exp_Pakd;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Exp_VFpt; use Exp_VFpt;
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_Ch13; use Sem_Ch13;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Sinfo; use Sinfo;
67 with Snames; use Snames;
68 with Stand; use Stand;
69 with SCIL_LL; use SCIL_LL;
70 with Targparm; use Targparm;
71 with Tbuild; use Tbuild;
72 with Ttypes; use Ttypes;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
75 with Validsw; use Validsw;
76
77 package body Exp_Ch4 is
78
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
82
83 procedure Binary_Op_Validity_Checks (N : Node_Id);
84 pragma Inline (Binary_Op_Validity_Checks);
85 -- Performs validity checks for a binary operator
86
87 procedure Build_Boolean_Array_Proc_Call
88 (N : Node_Id;
89 Op1 : Node_Id;
90 Op2 : Node_Id);
91 -- If a boolean array assignment can be done in place, build call to
92 -- corresponding library procedure.
93
94 procedure Complete_Controlled_Allocation (Temp_Decl : Node_Id);
95 -- Subsidiary to Expand_N_Allocator and Expand_Allocator_Expression. Formal
96 -- Temp_Decl is the declaration of a temporary which hold the value of the
97 -- original allocator. Create a custom Allocate routine for the expression
98 -- of Temp_Decl. The routine does special processing for anonymous access
99 -- types.
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 function Expand_Composite_Equality
143 (Nod : Node_Id;
144 Typ : Entity_Id;
145 Lhs : Node_Id;
146 Rhs : Node_Id;
147 Bodies : List_Id) return Node_Id;
148 -- Local recursive function used to expand equality for nested composite
149 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
150 -- to attach bodies of local functions that are created in the process.
151 -- This is the responsibility of the caller to insert those bodies at the
152 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
153 -- are the left and right sides for the comparison, and Typ is the type of
154 -- the arrays to compare.
155
156 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
157 -- Routine to expand concatenation of a sequence of two or more operands
158 -- (in the list Operands) and replace node Cnode with the result of the
159 -- concatenation. The operands can be of any appropriate type, and can
160 -- include both arrays and singleton elements.
161
162 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
163 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
164 -- fixed. We do not have such a type at runtime, so the purpose of this
165 -- routine is to find the real type by looking up the tree. We also
166 -- determine if the operation must be rounded.
167
168 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
169 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
170 -- discriminants if it has a constrained nominal type, unless the object
171 -- is a component of an enclosing Unchecked_Union object that is subject
172 -- to a per-object constraint and the enclosing object lacks inferable
173 -- discriminants.
174 --
175 -- An expression of an Unchecked_Union type has inferable discriminants
176 -- if it is either a name of an object with inferable discriminants or a
177 -- qualified expression whose subtype mark denotes a constrained subtype.
178
179 procedure Insert_Dereference_Action (N : Node_Id);
180 -- N is an expression whose type is an access. When the type of the
181 -- associated storage pool is derived from Checked_Pool, generate a
182 -- call to the 'Dereference' primitive operation.
183
184 function Make_Array_Comparison_Op
185 (Typ : Entity_Id;
186 Nod : Node_Id) return Node_Id;
187 -- Comparisons between arrays are expanded in line. This function produces
188 -- the body of the implementation of (a > b), where a and b are one-
189 -- dimensional arrays of some discrete type. The original node is then
190 -- expanded into the appropriate call to this function. Nod provides the
191 -- Sloc value for the generated code.
192
193 function Make_Boolean_Array_Op
194 (Typ : Entity_Id;
195 N : Node_Id) return Node_Id;
196 -- Boolean operations on boolean arrays are expanded in line. This function
197 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
198 -- b). It is used only the normal case and not the packed case. The type
199 -- involved, Typ, is the Boolean array type, and the logical operations in
200 -- the body are simple boolean operations. Note that Typ is always a
201 -- constrained type (the caller has ensured this by using
202 -- Convert_To_Actual_Subtype if necessary).
203
204 procedure Optimize_Length_Comparison (N : Node_Id);
205 -- Given an expression, if it is of the form X'Length op N (or the other
206 -- way round), where N is known at compile time to be 0 or 1, and X is a
207 -- simple entity, and op is a comparison operator, optimizes it into a
208 -- comparison of First and Last.
209
210 procedure Rewrite_Comparison (N : Node_Id);
211 -- If N is the node for a comparison whose outcome can be determined at
212 -- compile time, then the node N can be rewritten with True or False. If
213 -- the outcome cannot be determined at compile time, the call has no
214 -- effect. If N is a type conversion, then this processing is applied to
215 -- its expression. If N is neither comparison nor a type conversion, the
216 -- call has no effect.
217
218 procedure Tagged_Membership
219 (N : Node_Id;
220 SCIL_Node : out Node_Id;
221 Result : out Node_Id);
222 -- Construct the expression corresponding to the tagged membership test.
223 -- Deals with a second operand being (or not) a class-wide type.
224
225 function Safe_In_Place_Array_Op
226 (Lhs : Node_Id;
227 Op1 : Node_Id;
228 Op2 : Node_Id) return Boolean;
229 -- In the context of an assignment, where the right-hand side is a boolean
230 -- operation on arrays, check whether operation can be performed in place.
231
232 procedure Unary_Op_Validity_Checks (N : Node_Id);
233 pragma Inline (Unary_Op_Validity_Checks);
234 -- Performs validity checks for a unary operator
235
236 -------------------------------
237 -- Binary_Op_Validity_Checks --
238 -------------------------------
239
240 procedure Binary_Op_Validity_Checks (N : Node_Id) is
241 begin
242 if Validity_Checks_On and Validity_Check_Operands then
243 Ensure_Valid (Left_Opnd (N));
244 Ensure_Valid (Right_Opnd (N));
245 end if;
246 end Binary_Op_Validity_Checks;
247
248 ------------------------------------
249 -- Build_Boolean_Array_Proc_Call --
250 ------------------------------------
251
252 procedure Build_Boolean_Array_Proc_Call
253 (N : Node_Id;
254 Op1 : Node_Id;
255 Op2 : Node_Id)
256 is
257 Loc : constant Source_Ptr := Sloc (N);
258 Kind : constant Node_Kind := Nkind (Expression (N));
259 Target : constant Node_Id :=
260 Make_Attribute_Reference (Loc,
261 Prefix => Name (N),
262 Attribute_Name => Name_Address);
263
264 Arg1 : Node_Id := Op1;
265 Arg2 : Node_Id := Op2;
266 Call_Node : Node_Id;
267 Proc_Name : Entity_Id;
268
269 begin
270 if Kind = N_Op_Not then
271 if Nkind (Op1) in N_Binary_Op then
272
273 -- Use negated version of the binary operators
274
275 if Nkind (Op1) = N_Op_And then
276 Proc_Name := RTE (RE_Vector_Nand);
277
278 elsif Nkind (Op1) = N_Op_Or then
279 Proc_Name := RTE (RE_Vector_Nor);
280
281 else pragma Assert (Nkind (Op1) = N_Op_Xor);
282 Proc_Name := RTE (RE_Vector_Xor);
283 end if;
284
285 Call_Node :=
286 Make_Procedure_Call_Statement (Loc,
287 Name => New_Occurrence_Of (Proc_Name, Loc),
288
289 Parameter_Associations => New_List (
290 Target,
291 Make_Attribute_Reference (Loc,
292 Prefix => Left_Opnd (Op1),
293 Attribute_Name => Name_Address),
294
295 Make_Attribute_Reference (Loc,
296 Prefix => Right_Opnd (Op1),
297 Attribute_Name => Name_Address),
298
299 Make_Attribute_Reference (Loc,
300 Prefix => Left_Opnd (Op1),
301 Attribute_Name => Name_Length)));
302
303 else
304 Proc_Name := RTE (RE_Vector_Not);
305
306 Call_Node :=
307 Make_Procedure_Call_Statement (Loc,
308 Name => New_Occurrence_Of (Proc_Name, Loc),
309 Parameter_Associations => New_List (
310 Target,
311
312 Make_Attribute_Reference (Loc,
313 Prefix => Op1,
314 Attribute_Name => Name_Address),
315
316 Make_Attribute_Reference (Loc,
317 Prefix => Op1,
318 Attribute_Name => Name_Length)));
319 end if;
320
321 else
322 -- We use the following equivalences:
323
324 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
325 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
326 -- (not X) xor (not Y) = X xor Y
327 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
328
329 if Nkind (Op1) = N_Op_Not then
330 Arg1 := Right_Opnd (Op1);
331 Arg2 := Right_Opnd (Op2);
332 if Kind = N_Op_And then
333 Proc_Name := RTE (RE_Vector_Nor);
334 elsif Kind = N_Op_Or then
335 Proc_Name := RTE (RE_Vector_Nand);
336 else
337 Proc_Name := RTE (RE_Vector_Xor);
338 end if;
339
340 else
341 if Kind = N_Op_And then
342 Proc_Name := RTE (RE_Vector_And);
343 elsif Kind = N_Op_Or then
344 Proc_Name := RTE (RE_Vector_Or);
345 elsif Nkind (Op2) = N_Op_Not then
346 Proc_Name := RTE (RE_Vector_Nxor);
347 Arg2 := Right_Opnd (Op2);
348 else
349 Proc_Name := RTE (RE_Vector_Xor);
350 end if;
351 end if;
352
353 Call_Node :=
354 Make_Procedure_Call_Statement (Loc,
355 Name => New_Occurrence_Of (Proc_Name, Loc),
356 Parameter_Associations => New_List (
357 Target,
358 Make_Attribute_Reference (Loc,
359 Prefix => Arg1,
360 Attribute_Name => Name_Address),
361 Make_Attribute_Reference (Loc,
362 Prefix => Arg2,
363 Attribute_Name => Name_Address),
364 Make_Attribute_Reference (Loc,
365 Prefix => Arg1,
366 Attribute_Name => Name_Length)));
367 end if;
368
369 Rewrite (N, Call_Node);
370 Analyze (N);
371
372 exception
373 when RE_Not_Available =>
374 return;
375 end Build_Boolean_Array_Proc_Call;
376
377 ------------------------------------
378 -- Complete_Controlled_Allocation --
379 ------------------------------------
380
381 procedure Complete_Controlled_Allocation (Temp_Decl : Node_Id) is
382 pragma Assert (Nkind (Temp_Decl) = N_Object_Declaration);
383
384 Ptr_Typ : constant Entity_Id := Etype (Defining_Identifier (Temp_Decl));
385
386 function First_Declaration_Of_Current_Unit return Node_Id;
387 -- Return the current unit's first declaration. If the declaration list
388 -- is empty, the routine generates a null statement and returns it.
389
390 ---------------------------------------
391 -- First_Declaration_Of_Current_Unit --
392 ---------------------------------------
393
394 function First_Declaration_Of_Current_Unit return Node_Id is
395 Sem_U : Node_Id := Unit (Cunit (Current_Sem_Unit));
396 Decl : Node_Id;
397 Decls : List_Id;
398
399 begin
400 if Nkind (Sem_U) = N_Package_Declaration then
401 Sem_U := Specification (Sem_U);
402 Decls := Visible_Declarations (Sem_U);
403
404 if No (Decls) then
405 Decl := Make_Null_Statement (Sloc (Sem_U));
406 Decls := New_List (Decl);
407 Set_Visible_Declarations (Sem_U, Decls);
408 else
409 Decl := First (Decls);
410 end if;
411
412 else
413 Decls := Declarations (Sem_U);
414
415 if No (Decls) then
416 Decl := Make_Null_Statement (Sloc (Sem_U));
417 Decls := New_List (Decl);
418 Set_Declarations (Sem_U, Decls);
419 else
420 Decl := First (Decls);
421 end if;
422 end if;
423
424 return Decl;
425 end First_Declaration_Of_Current_Unit;
426
427 -- Start of processing for Complete_Controlled_Allocation
428
429 begin
430 -- Certain run-time configurations and targets do not provide support
431 -- for controlled types.
432
433 if Restriction_Active (No_Finalization) then
434 return;
435
436 -- Do nothing if the access type may never allocate an object
437
438 elsif No_Pool_Assigned (Ptr_Typ) then
439 return;
440
441 -- Access-to-controlled types are not supported on .NET/JVM
442
443 elsif VM_Target /= No_VM then
444 return;
445 end if;
446
447 -- Processing for anonymous access-to-controlled types. These access
448 -- types receive a special collection which appears on the declarations
449 -- of the enclosing semantic unit.
450
451 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
452 and then No (Associated_Collection (Ptr_Typ))
453 and then
454 (not Restriction_Active (No_Nested_Finalization)
455 or else Is_Library_Level_Entity (Ptr_Typ))
456 then
457 declare
458 Pool_Id : constant Entity_Id := RTE (RE_Global_Pool_Object);
459 Scop : Node_Id := Cunit_Entity (Current_Sem_Unit);
460
461 begin
462 -- Use the scope of the current semantic unit when analyzing
463
464 if Ekind (Scop) = E_Subprogram_Body then
465 Scop := Corresponding_Spec (Parent (Parent (Parent (Scop))));
466 end if;
467
468 Build_Finalization_Collection
469 (Typ => Ptr_Typ,
470 Ins_Node => First_Declaration_Of_Current_Unit,
471 Encl_Scope => Scop);
472
473 -- Decorate the anonymous access type and the allocator node
474
475 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
476 Set_Storage_Pool (Expression (Temp_Decl), Pool_Id);
477 end;
478 end if;
479
480 -- Since the temporary object reuses the original allocator, generate a
481 -- custom Allocate routine for the temporary.
482
483 if Present (Associated_Collection (Ptr_Typ)) then
484 Build_Allocate_Deallocate_Proc
485 (N => Temp_Decl,
486 Is_Allocate => True);
487 end if;
488 end Complete_Controlled_Allocation;
489
490 --------------------------------
491 -- Displace_Allocator_Pointer --
492 --------------------------------
493
494 procedure Displace_Allocator_Pointer (N : Node_Id) is
495 Loc : constant Source_Ptr := Sloc (N);
496 Orig_Node : constant Node_Id := Original_Node (N);
497 Dtyp : Entity_Id;
498 Etyp : Entity_Id;
499 PtrT : Entity_Id;
500
501 begin
502 -- Do nothing in case of VM targets: the virtual machine will handle
503 -- interfaces directly.
504
505 if not Tagged_Type_Expansion then
506 return;
507 end if;
508
509 pragma Assert (Nkind (N) = N_Identifier
510 and then Nkind (Orig_Node) = N_Allocator);
511
512 PtrT := Etype (Orig_Node);
513 Dtyp := Available_View (Designated_Type (PtrT));
514 Etyp := Etype (Expression (Orig_Node));
515
516 if Is_Class_Wide_Type (Dtyp)
517 and then Is_Interface (Dtyp)
518 then
519 -- If the type of the allocator expression is not an interface type
520 -- we can generate code to reference the record component containing
521 -- the pointer to the secondary dispatch table.
522
523 if not Is_Interface (Etyp) then
524 declare
525 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
526
527 begin
528 -- 1) Get access to the allocated object
529
530 Rewrite (N,
531 Make_Explicit_Dereference (Loc,
532 Relocate_Node (N)));
533 Set_Etype (N, Etyp);
534 Set_Analyzed (N);
535
536 -- 2) Add the conversion to displace the pointer to reference
537 -- the secondary dispatch table.
538
539 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
540 Analyze_And_Resolve (N, Dtyp);
541
542 -- 3) The 'access to the secondary dispatch table will be used
543 -- as the value returned by the allocator.
544
545 Rewrite (N,
546 Make_Attribute_Reference (Loc,
547 Prefix => Relocate_Node (N),
548 Attribute_Name => Name_Access));
549 Set_Etype (N, Saved_Typ);
550 Set_Analyzed (N);
551 end;
552
553 -- If the type of the allocator expression is an interface type we
554 -- generate a run-time call to displace "this" to reference the
555 -- component containing the pointer to the secondary dispatch table
556 -- or else raise Constraint_Error if the actual object does not
557 -- implement the target interface. This case corresponds with the
558 -- following example:
559
560 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
561 -- begin
562 -- return new Iface_2'Class'(Obj);
563 -- end Op;
564
565 else
566 Rewrite (N,
567 Unchecked_Convert_To (PtrT,
568 Make_Function_Call (Loc,
569 Name => New_Reference_To (RTE (RE_Displace), Loc),
570 Parameter_Associations => New_List (
571 Unchecked_Convert_To (RTE (RE_Address),
572 Relocate_Node (N)),
573
574 New_Occurrence_Of
575 (Elists.Node
576 (First_Elmt
577 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
578 Loc)))));
579 Analyze_And_Resolve (N, PtrT);
580 end if;
581 end if;
582 end Displace_Allocator_Pointer;
583
584 ---------------------------------
585 -- Expand_Allocator_Expression --
586 ---------------------------------
587
588 procedure Expand_Allocator_Expression (N : Node_Id) is
589 Loc : constant Source_Ptr := Sloc (N);
590 Exp : constant Node_Id := Expression (Expression (N));
591 PtrT : constant Entity_Id := Etype (N);
592 DesigT : constant Entity_Id := Designated_Type (PtrT);
593
594 procedure Apply_Accessibility_Check
595 (Ref : Node_Id;
596 Built_In_Place : Boolean := False);
597 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
598 -- type, generate an accessibility check to verify that the level of the
599 -- type of the created object is not deeper than the level of the access
600 -- type. If the type of the qualified expression is class- wide, then
601 -- always generate the check (except in the case where it is known to be
602 -- unnecessary, see comment below). Otherwise, only generate the check
603 -- if the level of the qualified expression type is statically deeper
604 -- than the access type.
605 --
606 -- Although the static accessibility will generally have been performed
607 -- as a legality check, it won't have been done in cases where the
608 -- allocator appears in generic body, so a run-time check is needed in
609 -- general. One special case is when the access type is declared in the
610 -- same scope as the class-wide allocator, in which case the check can
611 -- never fail, so it need not be generated.
612 --
613 -- As an open issue, there seem to be cases where the static level
614 -- associated with the class-wide object's underlying type is not
615 -- sufficient to perform the proper accessibility check, such as for
616 -- allocators in nested subprograms or accept statements initialized by
617 -- class-wide formals when the actual originates outside at a deeper
618 -- static level. The nested subprogram case might require passing
619 -- accessibility levels along with class-wide parameters, and the task
620 -- case seems to be an actual gap in the language rules that needs to
621 -- be fixed by the ARG. ???
622
623 -------------------------------
624 -- Apply_Accessibility_Check --
625 -------------------------------
626
627 procedure Apply_Accessibility_Check
628 (Ref : Node_Id;
629 Built_In_Place : Boolean := False)
630 is
631 Ref_Node : Node_Id;
632
633 begin
634 -- Note: we skip the accessibility check for the VM case, since
635 -- there does not seem to be any practical way of implementing it.
636
637 if Ada_Version >= Ada_2005
638 and then Tagged_Type_Expansion
639 and then Is_Class_Wide_Type (DesigT)
640 and then not Scope_Suppress (Accessibility_Check)
641 and then
642 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
643 or else
644 (Is_Class_Wide_Type (Etype (Exp))
645 and then Scope (PtrT) /= Current_Scope))
646 then
647 -- If the allocator was built in place Ref is already a reference
648 -- to the access object initialized to the result of the allocator
649 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
650 -- it is the entity associated with the object containing the
651 -- address of the allocated object.
652
653 if Built_In_Place then
654 Ref_Node := New_Copy (Ref);
655 else
656 Ref_Node := New_Reference_To (Ref, Loc);
657 end if;
658
659 Insert_Action (N,
660 Make_Raise_Program_Error (Loc,
661 Condition =>
662 Make_Op_Gt (Loc,
663 Left_Opnd =>
664 Build_Get_Access_Level (Loc,
665 Make_Attribute_Reference (Loc,
666 Prefix => Ref_Node,
667 Attribute_Name => Name_Tag)),
668 Right_Opnd =>
669 Make_Integer_Literal (Loc, Type_Access_Level (PtrT))),
670 Reason => PE_Accessibility_Check_Failed));
671 end if;
672 end Apply_Accessibility_Check;
673
674 -- Local variables
675
676 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
677 Indic : constant Node_Id := Subtype_Mark (Expression (N));
678 T : constant Entity_Id := Entity (Indic);
679 Node : Node_Id;
680 Tag_Assign : Node_Id;
681 Temp : Entity_Id;
682 Temp_Decl : Node_Id;
683
684 TagT : Entity_Id := Empty;
685 -- Type used as source for tag assignment
686
687 TagR : Node_Id := Empty;
688 -- Target reference for tag assignment
689
690 -- Start of processing for Expand_Allocator_Expression
691
692 begin
693 if Is_Tagged_Type (T)
694 or else Needs_Finalization (T)
695 then
696 if Is_CPP_Constructor_Call (Exp) then
697
698 -- Generate:
699 -- Pnnn : constant ptr_T := new (T);
700 -- Init (Pnnn.all,...);
701
702 -- Allocate the object without an expression
703
704 Node := Relocate_Node (N);
705 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
706
707 -- Avoid its expansion to avoid generating a call to the default
708 -- C++ constructor.
709
710 Set_Analyzed (Node);
711
712 Temp := Make_Temporary (Loc, 'P', N);
713
714 Temp_Decl :=
715 Make_Object_Declaration (Loc,
716 Defining_Identifier => Temp,
717 Constant_Present => True,
718 Object_Definition => New_Reference_To (PtrT, Loc),
719 Expression => Node);
720 Insert_Action (N, Temp_Decl);
721
722 Apply_Accessibility_Check (Temp);
723
724 -- Locate the enclosing list and insert the C++ constructor call
725
726 declare
727 P : Node_Id;
728
729 begin
730 P := Parent (Node);
731 while not Is_List_Member (P) loop
732 P := Parent (P);
733 end loop;
734
735 Insert_List_After_And_Analyze (P,
736 Build_Initialization_Call (Loc,
737 Id_Ref =>
738 Make_Explicit_Dereference (Loc,
739 Prefix => New_Reference_To (Temp, Loc)),
740 Typ => Etype (Exp),
741 Constructor_Ref => Exp));
742 end;
743
744 Rewrite (N, New_Reference_To (Temp, Loc));
745 Analyze_And_Resolve (N, PtrT);
746 return;
747 end if;
748
749 -- Ada 2005 (AI-318-02): If the initialization expression is a call
750 -- to a build-in-place function, then access to the allocated object
751 -- must be passed to the function. Currently we limit such functions
752 -- to those with constrained limited result subtypes, but eventually
753 -- we plan to expand the allowed forms of functions that are treated
754 -- as build-in-place.
755
756 if Ada_Version >= Ada_2005
757 and then Is_Build_In_Place_Function_Call (Exp)
758 then
759 Make_Build_In_Place_Call_In_Allocator (N, Exp);
760 Apply_Accessibility_Check (N, Built_In_Place => True);
761 return;
762 end if;
763
764 -- Actions inserted before:
765 -- Temp : constant ptr_T := new T'(Expression);
766 -- <no CW> Temp._tag := T'tag;
767 -- <CTRL> Adjust (Finalizable (Temp.all));
768 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
769
770 -- We analyze by hand the new internal allocator to avoid
771 -- any recursion and inappropriate call to Initialize
772
773 -- We don't want to remove side effects when the expression must be
774 -- built in place. In the case of a build-in-place function call,
775 -- that could lead to a duplication of the call, which was already
776 -- substituted for the allocator.
777
778 if not Aggr_In_Place then
779 Remove_Side_Effects (Exp);
780 end if;
781
782 Temp := Make_Temporary (Loc, 'P', N);
783
784 -- For a class wide allocation generate the following code:
785
786 -- type Equiv_Record is record ... end record;
787 -- implicit subtype CW is <Class_Wide_Subytpe>;
788 -- temp : PtrT := new CW'(CW!(expr));
789
790 if Is_Class_Wide_Type (T) then
791 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
792
793 -- Ada 2005 (AI-251): If the expression is a class-wide interface
794 -- object we generate code to move up "this" to reference the
795 -- base of the object before allocating the new object.
796
797 -- Note that Exp'Address is recursively expanded into a call
798 -- to Base_Address (Exp.Tag)
799
800 if Is_Class_Wide_Type (Etype (Exp))
801 and then Is_Interface (Etype (Exp))
802 and then Tagged_Type_Expansion
803 then
804 Set_Expression
805 (Expression (N),
806 Unchecked_Convert_To (Entity (Indic),
807 Make_Explicit_Dereference (Loc,
808 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
809 Make_Attribute_Reference (Loc,
810 Prefix => Exp,
811 Attribute_Name => Name_Address)))));
812 else
813 Set_Expression
814 (Expression (N),
815 Unchecked_Convert_To (Entity (Indic), Exp));
816 end if;
817
818 Analyze_And_Resolve (Expression (N), Entity (Indic));
819 end if;
820
821 -- Processing for allocators returning non-interface types
822
823 if not Is_Interface (Directly_Designated_Type (PtrT)) then
824 if Aggr_In_Place then
825 Temp_Decl :=
826 Make_Object_Declaration (Loc,
827 Defining_Identifier => Temp,
828 Object_Definition => New_Reference_To (PtrT, Loc),
829 Expression =>
830 Make_Allocator (Loc,
831 Expression =>
832 New_Reference_To (Etype (Exp), Loc)));
833
834 -- Copy the Comes_From_Source flag for the allocator we just
835 -- built, since logically this allocator is a replacement of
836 -- the original allocator node. This is for proper handling of
837 -- restriction No_Implicit_Heap_Allocations.
838
839 Set_Comes_From_Source
840 (Expression (Temp_Decl), Comes_From_Source (N));
841
842 Set_No_Initialization (Expression (Temp_Decl));
843 Insert_Action (N, Temp_Decl);
844
845 Complete_Controlled_Allocation (Temp_Decl);
846 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
847
848 -- Attach the object to the associated finalization collection.
849 -- This is done manually on .NET/JVM since those compilers do
850 -- no support pools and can't benefit from internally generated
851 -- Allocate / Deallocate procedures.
852
853 if VM_Target /= No_VM
854 and then Is_Controlled (DesigT)
855 and then Present (Associated_Collection (PtrT))
856 then
857 Insert_Action (N,
858 Make_Attach_Call (
859 Obj_Ref =>
860 New_Reference_To (Temp, Loc),
861 Ptr_Typ => PtrT));
862 end if;
863
864 else
865 Node := Relocate_Node (N);
866 Set_Analyzed (Node);
867
868 Temp_Decl :=
869 Make_Object_Declaration (Loc,
870 Defining_Identifier => Temp,
871 Constant_Present => True,
872 Object_Definition => New_Reference_To (PtrT, Loc),
873 Expression => Node);
874
875 Insert_Action (N, Temp_Decl);
876 Complete_Controlled_Allocation (Temp_Decl);
877
878 -- Attach the object to the associated finalization collection.
879 -- This is done manually on .NET/JVM since those compilers do
880 -- no support pools and can't benefit from internally generated
881 -- Allocate / Deallocate procedures.
882
883 if VM_Target /= No_VM
884 and then Is_Controlled (DesigT)
885 and then Present (Associated_Collection (PtrT))
886 then
887 Insert_Action (N,
888 Make_Attach_Call (
889 Obj_Ref =>
890 New_Reference_To (Temp, Loc),
891 Ptr_Typ => PtrT));
892 end if;
893 end if;
894
895 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
896 -- interface type. In this case we use the type of the qualified
897 -- expression to allocate the object.
898
899 else
900 declare
901 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
902 New_Decl : Node_Id;
903
904 begin
905 New_Decl :=
906 Make_Full_Type_Declaration (Loc,
907 Defining_Identifier => Def_Id,
908 Type_Definition =>
909 Make_Access_To_Object_Definition (Loc,
910 All_Present => True,
911 Null_Exclusion_Present => False,
912 Constant_Present => False,
913 Subtype_Indication =>
914 New_Reference_To (Etype (Exp), Loc)));
915
916 Insert_Action (N, New_Decl);
917
918 -- Inherit the allocation-related attributes from the original
919 -- access type.
920
921 Set_Associated_Collection (Def_Id,
922 Associated_Collection (PtrT));
923
924 Set_Associated_Storage_Pool (Def_Id,
925 Associated_Storage_Pool (PtrT));
926
927 -- Declare the object using the previous type declaration
928
929 if Aggr_In_Place then
930 Temp_Decl :=
931 Make_Object_Declaration (Loc,
932 Defining_Identifier => Temp,
933 Object_Definition => New_Reference_To (Def_Id, Loc),
934 Expression =>
935 Make_Allocator (Loc,
936 New_Reference_To (Etype (Exp), Loc)));
937
938 -- Copy the Comes_From_Source flag for the allocator we just
939 -- built, since logically this allocator is a replacement of
940 -- the original allocator node. This is for proper handling
941 -- of restriction No_Implicit_Heap_Allocations.
942
943 Set_Comes_From_Source
944 (Expression (Temp_Decl), Comes_From_Source (N));
945
946 Set_No_Initialization (Expression (Temp_Decl));
947 Insert_Action (N, Temp_Decl);
948
949 Complete_Controlled_Allocation (Temp_Decl);
950 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
951
952 else
953 Node := Relocate_Node (N);
954 Set_Analyzed (Node);
955
956 Temp_Decl :=
957 Make_Object_Declaration (Loc,
958 Defining_Identifier => Temp,
959 Constant_Present => True,
960 Object_Definition => New_Reference_To (Def_Id, Loc),
961 Expression => Node);
962
963 Insert_Action (N, Temp_Decl);
964 Complete_Controlled_Allocation (Temp_Decl);
965 end if;
966
967 -- Generate an additional object containing the address of the
968 -- returned object. The type of this second object declaration
969 -- is the correct type required for the common processing that
970 -- is still performed by this subprogram. The displacement of
971 -- this pointer to reference the component associated with the
972 -- interface type will be done at the end of common processing.
973
974 New_Decl :=
975 Make_Object_Declaration (Loc,
976 Defining_Identifier => Make_Temporary (Loc, 'P'),
977 Object_Definition => New_Reference_To (PtrT, Loc),
978 Expression =>
979 Unchecked_Convert_To (PtrT,
980 New_Reference_To (Temp, Loc)));
981
982 Insert_Action (N, New_Decl);
983
984 Temp_Decl := New_Decl;
985 Temp := Defining_Identifier (New_Decl);
986 end;
987 end if;
988
989 Apply_Accessibility_Check (Temp);
990
991 -- Generate the tag assignment
992
993 -- Suppress the tag assignment when VM_Target because VM tags are
994 -- represented implicitly in objects.
995
996 if not Tagged_Type_Expansion then
997 null;
998
999 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1000 -- interface objects because in this case the tag does not change.
1001
1002 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1003 pragma Assert (Is_Class_Wide_Type
1004 (Directly_Designated_Type (Etype (N))));
1005 null;
1006
1007 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1008 TagT := T;
1009 TagR := New_Reference_To (Temp, Loc);
1010
1011 elsif Is_Private_Type (T)
1012 and then Is_Tagged_Type (Underlying_Type (T))
1013 then
1014 TagT := Underlying_Type (T);
1015 TagR :=
1016 Unchecked_Convert_To (Underlying_Type (T),
1017 Make_Explicit_Dereference (Loc,
1018 Prefix => New_Reference_To (Temp, Loc)));
1019 end if;
1020
1021 if Present (TagT) then
1022 declare
1023 Full_T : constant Entity_Id := Underlying_Type (TagT);
1024 begin
1025 Tag_Assign :=
1026 Make_Assignment_Statement (Loc,
1027 Name =>
1028 Make_Selected_Component (Loc,
1029 Prefix => TagR,
1030 Selector_Name =>
1031 New_Reference_To (First_Tag_Component (Full_T), Loc)),
1032 Expression =>
1033 Unchecked_Convert_To (RTE (RE_Tag),
1034 New_Reference_To
1035 (Elists.Node
1036 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1037 end;
1038
1039 -- The previous assignment has to be done in any case
1040
1041 Set_Assignment_OK (Name (Tag_Assign));
1042 Insert_Action (N, Tag_Assign);
1043 end if;
1044
1045 if Needs_Finalization (DesigT)
1046 and then Needs_Finalization (T)
1047 then
1048 -- Generate an Adjust call if the object will be moved. In Ada
1049 -- 2005, the object may be inherently limited, in which case
1050 -- there is no Adjust procedure, and the object is built in
1051 -- place. In Ada 95, the object can be limited but not
1052 -- inherently limited if this allocator came from a return
1053 -- statement (we're allocating the result on the secondary
1054 -- stack). In that case, the object will be moved, so we _do_
1055 -- want to Adjust.
1056
1057 if not Aggr_In_Place
1058 and then not Is_Immutably_Limited_Type (T)
1059 then
1060 Insert_Action (N,
1061 Make_Adjust_Call (
1062 Obj_Ref =>
1063
1064 -- An unchecked conversion is needed in the classwide
1065 -- case because the designated type can be an ancestor
1066 -- of the subtype mark of the allocator.
1067
1068 Unchecked_Convert_To (T,
1069 Make_Explicit_Dereference (Loc,
1070 Prefix => New_Reference_To (Temp, Loc))),
1071 Typ => T));
1072 end if;
1073
1074 -- Generate:
1075 -- Set_Finalize_Address_Ptr
1076 -- (Collection, <Finalize_Address>'Unrestricted_Access)
1077
1078 -- Since .NET/JVM compilers do not support address arithmetic,
1079 -- this call is skipped. The same is done for CodePeer because
1080 -- Finalize_Address is never generated.
1081
1082 if VM_Target = No_VM
1083 and then not CodePeer_Mode
1084 and then Present (Associated_Collection (PtrT))
1085 then
1086 Insert_Action (N,
1087 Make_Set_Finalize_Address_Ptr_Call
1088 (Loc => Loc,
1089 Typ => T,
1090 Ptr_Typ => PtrT));
1091 end if;
1092 end if;
1093
1094 Rewrite (N, New_Reference_To (Temp, Loc));
1095 Analyze_And_Resolve (N, PtrT);
1096
1097 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1098 -- component containing the secondary dispatch table of the interface
1099 -- type.
1100
1101 if Is_Interface (Directly_Designated_Type (PtrT)) then
1102 Displace_Allocator_Pointer (N);
1103 end if;
1104
1105 elsif Aggr_In_Place then
1106 Temp := Make_Temporary (Loc, 'P', N);
1107 Temp_Decl :=
1108 Make_Object_Declaration (Loc,
1109 Defining_Identifier => Temp,
1110 Object_Definition => New_Reference_To (PtrT, Loc),
1111 Expression =>
1112 Make_Allocator (Loc,
1113 Expression => New_Reference_To (Etype (Exp), Loc)));
1114
1115 -- Copy the Comes_From_Source flag for the allocator we just built,
1116 -- since logically this allocator is a replacement of the original
1117 -- allocator node. This is for proper handling of restriction
1118 -- No_Implicit_Heap_Allocations.
1119
1120 Set_Comes_From_Source
1121 (Expression (Temp_Decl), Comes_From_Source (N));
1122
1123 Set_No_Initialization (Expression (Temp_Decl));
1124 Insert_Action (N, Temp_Decl);
1125
1126 Complete_Controlled_Allocation (Temp_Decl);
1127 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1128
1129 -- Attach the object to the associated finalization collection. This
1130 -- is done manually on .NET/JVM since those compilers do no support
1131 -- pools and cannot benefit from internally generated Allocate and
1132 -- Deallocate procedures.
1133
1134 if VM_Target /= No_VM
1135 and then Is_Controlled (DesigT)
1136 and then Present (Associated_Collection (PtrT))
1137 then
1138 Insert_Action (N,
1139 Make_Attach_Call
1140 (Obj_Ref => New_Reference_To (Temp, Loc),
1141 Ptr_Typ => PtrT));
1142 end if;
1143
1144 Rewrite (N, New_Reference_To (Temp, Loc));
1145 Analyze_And_Resolve (N, PtrT);
1146
1147 elsif Is_Access_Type (T)
1148 and then Can_Never_Be_Null (T)
1149 then
1150 Install_Null_Excluding_Check (Exp);
1151
1152 elsif Is_Access_Type (DesigT)
1153 and then Nkind (Exp) = N_Allocator
1154 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1155 then
1156 -- Apply constraint to designated subtype indication
1157
1158 Apply_Constraint_Check (Expression (Exp),
1159 Designated_Type (DesigT),
1160 No_Sliding => True);
1161
1162 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1163
1164 -- Propagate constraint_error to enclosing allocator
1165
1166 Rewrite (Exp, New_Copy (Expression (Exp)));
1167 end if;
1168 else
1169 -- If we have:
1170 -- type A is access T1;
1171 -- X : A := new T2'(...);
1172 -- T1 and T2 can be different subtypes, and we might need to check
1173 -- both constraints. First check against the type of the qualified
1174 -- expression.
1175
1176 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1177
1178 if Do_Range_Check (Exp) then
1179 Set_Do_Range_Check (Exp, False);
1180 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1181 end if;
1182
1183 -- A check is also needed in cases where the designated subtype is
1184 -- constrained and differs from the subtype given in the qualified
1185 -- expression. Note that the check on the qualified expression does
1186 -- not allow sliding, but this check does (a relaxation from Ada 83).
1187
1188 if Is_Constrained (DesigT)
1189 and then not Subtypes_Statically_Match (T, DesigT)
1190 then
1191 Apply_Constraint_Check
1192 (Exp, DesigT, No_Sliding => False);
1193
1194 if Do_Range_Check (Exp) then
1195 Set_Do_Range_Check (Exp, False);
1196 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1197 end if;
1198 end if;
1199
1200 -- For an access to unconstrained packed array, GIGI needs to see an
1201 -- expression with a constrained subtype in order to compute the
1202 -- proper size for the allocator.
1203
1204 if Is_Array_Type (T)
1205 and then not Is_Constrained (T)
1206 and then Is_Packed (T)
1207 then
1208 declare
1209 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1210 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1211 begin
1212 Insert_Action (Exp,
1213 Make_Subtype_Declaration (Loc,
1214 Defining_Identifier => ConstrT,
1215 Subtype_Indication => Make_Subtype_From_Expr (Exp, T)));
1216 Freeze_Itype (ConstrT, Exp);
1217 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1218 end;
1219 end if;
1220
1221 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1222 -- to a build-in-place function, then access to the allocated object
1223 -- must be passed to the function. Currently we limit such functions
1224 -- to those with constrained limited result subtypes, but eventually
1225 -- we plan to expand the allowed forms of functions that are treated
1226 -- as build-in-place.
1227
1228 if Ada_Version >= Ada_2005
1229 and then Is_Build_In_Place_Function_Call (Exp)
1230 then
1231 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1232 end if;
1233 end if;
1234
1235 exception
1236 when RE_Not_Available =>
1237 return;
1238 end Expand_Allocator_Expression;
1239
1240 -----------------------------
1241 -- Expand_Array_Comparison --
1242 -----------------------------
1243
1244 -- Expansion is only required in the case of array types. For the unpacked
1245 -- case, an appropriate runtime routine is called. For packed cases, and
1246 -- also in some other cases where a runtime routine cannot be called, the
1247 -- form of the expansion is:
1248
1249 -- [body for greater_nn; boolean_expression]
1250
1251 -- The body is built by Make_Array_Comparison_Op, and the form of the
1252 -- Boolean expression depends on the operator involved.
1253
1254 procedure Expand_Array_Comparison (N : Node_Id) is
1255 Loc : constant Source_Ptr := Sloc (N);
1256 Op1 : Node_Id := Left_Opnd (N);
1257 Op2 : Node_Id := Right_Opnd (N);
1258 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1259 Ctyp : constant Entity_Id := Component_Type (Typ1);
1260
1261 Expr : Node_Id;
1262 Func_Body : Node_Id;
1263 Func_Name : Entity_Id;
1264
1265 Comp : RE_Id;
1266
1267 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1268 -- True for byte addressable target
1269
1270 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1271 -- Returns True if the length of the given operand is known to be less
1272 -- than 4. Returns False if this length is known to be four or greater
1273 -- or is not known at compile time.
1274
1275 ------------------------
1276 -- Length_Less_Than_4 --
1277 ------------------------
1278
1279 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1280 Otyp : constant Entity_Id := Etype (Opnd);
1281
1282 begin
1283 if Ekind (Otyp) = E_String_Literal_Subtype then
1284 return String_Literal_Length (Otyp) < 4;
1285
1286 else
1287 declare
1288 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1289 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1290 Hi : constant Node_Id := Type_High_Bound (Ityp);
1291 Lov : Uint;
1292 Hiv : Uint;
1293
1294 begin
1295 if Compile_Time_Known_Value (Lo) then
1296 Lov := Expr_Value (Lo);
1297 else
1298 return False;
1299 end if;
1300
1301 if Compile_Time_Known_Value (Hi) then
1302 Hiv := Expr_Value (Hi);
1303 else
1304 return False;
1305 end if;
1306
1307 return Hiv < Lov + 3;
1308 end;
1309 end if;
1310 end Length_Less_Than_4;
1311
1312 -- Start of processing for Expand_Array_Comparison
1313
1314 begin
1315 -- Deal first with unpacked case, where we can call a runtime routine
1316 -- except that we avoid this for targets for which are not addressable
1317 -- by bytes, and for the JVM/CIL, since they do not support direct
1318 -- addressing of array components.
1319
1320 if not Is_Bit_Packed_Array (Typ1)
1321 and then Byte_Addressable
1322 and then VM_Target = No_VM
1323 then
1324 -- The call we generate is:
1325
1326 -- Compare_Array_xn[_Unaligned]
1327 -- (left'address, right'address, left'length, right'length) <op> 0
1328
1329 -- x = U for unsigned, S for signed
1330 -- n = 8,16,32,64 for component size
1331 -- Add _Unaligned if length < 4 and component size is 8.
1332 -- <op> is the standard comparison operator
1333
1334 if Component_Size (Typ1) = 8 then
1335 if Length_Less_Than_4 (Op1)
1336 or else
1337 Length_Less_Than_4 (Op2)
1338 then
1339 if Is_Unsigned_Type (Ctyp) then
1340 Comp := RE_Compare_Array_U8_Unaligned;
1341 else
1342 Comp := RE_Compare_Array_S8_Unaligned;
1343 end if;
1344
1345 else
1346 if Is_Unsigned_Type (Ctyp) then
1347 Comp := RE_Compare_Array_U8;
1348 else
1349 Comp := RE_Compare_Array_S8;
1350 end if;
1351 end if;
1352
1353 elsif Component_Size (Typ1) = 16 then
1354 if Is_Unsigned_Type (Ctyp) then
1355 Comp := RE_Compare_Array_U16;
1356 else
1357 Comp := RE_Compare_Array_S16;
1358 end if;
1359
1360 elsif Component_Size (Typ1) = 32 then
1361 if Is_Unsigned_Type (Ctyp) then
1362 Comp := RE_Compare_Array_U32;
1363 else
1364 Comp := RE_Compare_Array_S32;
1365 end if;
1366
1367 else pragma Assert (Component_Size (Typ1) = 64);
1368 if Is_Unsigned_Type (Ctyp) then
1369 Comp := RE_Compare_Array_U64;
1370 else
1371 Comp := RE_Compare_Array_S64;
1372 end if;
1373 end if;
1374
1375 Remove_Side_Effects (Op1, Name_Req => True);
1376 Remove_Side_Effects (Op2, Name_Req => True);
1377
1378 Rewrite (Op1,
1379 Make_Function_Call (Sloc (Op1),
1380 Name => New_Occurrence_Of (RTE (Comp), Loc),
1381
1382 Parameter_Associations => New_List (
1383 Make_Attribute_Reference (Loc,
1384 Prefix => Relocate_Node (Op1),
1385 Attribute_Name => Name_Address),
1386
1387 Make_Attribute_Reference (Loc,
1388 Prefix => Relocate_Node (Op2),
1389 Attribute_Name => Name_Address),
1390
1391 Make_Attribute_Reference (Loc,
1392 Prefix => Relocate_Node (Op1),
1393 Attribute_Name => Name_Length),
1394
1395 Make_Attribute_Reference (Loc,
1396 Prefix => Relocate_Node (Op2),
1397 Attribute_Name => Name_Length))));
1398
1399 Rewrite (Op2,
1400 Make_Integer_Literal (Sloc (Op2),
1401 Intval => Uint_0));
1402
1403 Analyze_And_Resolve (Op1, Standard_Integer);
1404 Analyze_And_Resolve (Op2, Standard_Integer);
1405 return;
1406 end if;
1407
1408 -- Cases where we cannot make runtime call
1409
1410 -- For (a <= b) we convert to not (a > b)
1411
1412 if Chars (N) = Name_Op_Le then
1413 Rewrite (N,
1414 Make_Op_Not (Loc,
1415 Right_Opnd =>
1416 Make_Op_Gt (Loc,
1417 Left_Opnd => Op1,
1418 Right_Opnd => Op2)));
1419 Analyze_And_Resolve (N, Standard_Boolean);
1420 return;
1421
1422 -- For < the Boolean expression is
1423 -- greater__nn (op2, op1)
1424
1425 elsif Chars (N) = Name_Op_Lt then
1426 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1427
1428 -- Switch operands
1429
1430 Op1 := Right_Opnd (N);
1431 Op2 := Left_Opnd (N);
1432
1433 -- For (a >= b) we convert to not (a < b)
1434
1435 elsif Chars (N) = Name_Op_Ge then
1436 Rewrite (N,
1437 Make_Op_Not (Loc,
1438 Right_Opnd =>
1439 Make_Op_Lt (Loc,
1440 Left_Opnd => Op1,
1441 Right_Opnd => Op2)));
1442 Analyze_And_Resolve (N, Standard_Boolean);
1443 return;
1444
1445 -- For > the Boolean expression is
1446 -- greater__nn (op1, op2)
1447
1448 else
1449 pragma Assert (Chars (N) = Name_Op_Gt);
1450 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1451 end if;
1452
1453 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1454 Expr :=
1455 Make_Function_Call (Loc,
1456 Name => New_Reference_To (Func_Name, Loc),
1457 Parameter_Associations => New_List (Op1, Op2));
1458
1459 Insert_Action (N, Func_Body);
1460 Rewrite (N, Expr);
1461 Analyze_And_Resolve (N, Standard_Boolean);
1462
1463 exception
1464 when RE_Not_Available =>
1465 return;
1466 end Expand_Array_Comparison;
1467
1468 ---------------------------
1469 -- Expand_Array_Equality --
1470 ---------------------------
1471
1472 -- Expand an equality function for multi-dimensional arrays. Here is an
1473 -- example of such a function for Nb_Dimension = 2
1474
1475 -- function Enn (A : atyp; B : btyp) return boolean is
1476 -- begin
1477 -- if (A'length (1) = 0 or else A'length (2) = 0)
1478 -- and then
1479 -- (B'length (1) = 0 or else B'length (2) = 0)
1480 -- then
1481 -- return True; -- RM 4.5.2(22)
1482 -- end if;
1483
1484 -- if A'length (1) /= B'length (1)
1485 -- or else
1486 -- A'length (2) /= B'length (2)
1487 -- then
1488 -- return False; -- RM 4.5.2(23)
1489 -- end if;
1490
1491 -- declare
1492 -- A1 : Index_T1 := A'first (1);
1493 -- B1 : Index_T1 := B'first (1);
1494 -- begin
1495 -- loop
1496 -- declare
1497 -- A2 : Index_T2 := A'first (2);
1498 -- B2 : Index_T2 := B'first (2);
1499 -- begin
1500 -- loop
1501 -- if A (A1, A2) /= B (B1, B2) then
1502 -- return False;
1503 -- end if;
1504
1505 -- exit when A2 = A'last (2);
1506 -- A2 := Index_T2'succ (A2);
1507 -- B2 := Index_T2'succ (B2);
1508 -- end loop;
1509 -- end;
1510
1511 -- exit when A1 = A'last (1);
1512 -- A1 := Index_T1'succ (A1);
1513 -- B1 := Index_T1'succ (B1);
1514 -- end loop;
1515 -- end;
1516
1517 -- return true;
1518 -- end Enn;
1519
1520 -- Note on the formal types used (atyp and btyp). If either of the arrays
1521 -- is of a private type, we use the underlying type, and do an unchecked
1522 -- conversion of the actual. If either of the arrays has a bound depending
1523 -- on a discriminant, then we use the base type since otherwise we have an
1524 -- escaped discriminant in the function.
1525
1526 -- If both arrays are constrained and have the same bounds, we can generate
1527 -- a loop with an explicit iteration scheme using a 'Range attribute over
1528 -- the first array.
1529
1530 function Expand_Array_Equality
1531 (Nod : Node_Id;
1532 Lhs : Node_Id;
1533 Rhs : Node_Id;
1534 Bodies : List_Id;
1535 Typ : Entity_Id) return Node_Id
1536 is
1537 Loc : constant Source_Ptr := Sloc (Nod);
1538 Decls : constant List_Id := New_List;
1539 Index_List1 : constant List_Id := New_List;
1540 Index_List2 : constant List_Id := New_List;
1541
1542 Actuals : List_Id;
1543 Formals : List_Id;
1544 Func_Name : Entity_Id;
1545 Func_Body : Node_Id;
1546
1547 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1548 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1549
1550 Ltyp : Entity_Id;
1551 Rtyp : Entity_Id;
1552 -- The parameter types to be used for the formals
1553
1554 function Arr_Attr
1555 (Arr : Entity_Id;
1556 Nam : Name_Id;
1557 Num : Int) return Node_Id;
1558 -- This builds the attribute reference Arr'Nam (Expr)
1559
1560 function Component_Equality (Typ : Entity_Id) return Node_Id;
1561 -- Create one statement to compare corresponding components, designated
1562 -- by a full set of indexes.
1563
1564 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1565 -- Given one of the arguments, computes the appropriate type to be used
1566 -- for that argument in the corresponding function formal
1567
1568 function Handle_One_Dimension
1569 (N : Int;
1570 Index : Node_Id) return Node_Id;
1571 -- This procedure returns the following code
1572 --
1573 -- declare
1574 -- Bn : Index_T := B'First (N);
1575 -- begin
1576 -- loop
1577 -- xxx
1578 -- exit when An = A'Last (N);
1579 -- An := Index_T'Succ (An)
1580 -- Bn := Index_T'Succ (Bn)
1581 -- end loop;
1582 -- end;
1583 --
1584 -- If both indexes are constrained and identical, the procedure
1585 -- returns a simpler loop:
1586 --
1587 -- for An in A'Range (N) loop
1588 -- xxx
1589 -- end loop
1590 --
1591 -- N is the dimension for which we are generating a loop. Index is the
1592 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1593 -- xxx statement is either the loop or declare for the next dimension
1594 -- or if this is the last dimension the comparison of corresponding
1595 -- components of the arrays.
1596 --
1597 -- The actual way the code works is to return the comparison of
1598 -- corresponding components for the N+1 call. That's neater!
1599
1600 function Test_Empty_Arrays return Node_Id;
1601 -- This function constructs the test for both arrays being empty
1602 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1603 -- and then
1604 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1605
1606 function Test_Lengths_Correspond return Node_Id;
1607 -- This function constructs the test for arrays having different lengths
1608 -- in at least one index position, in which case the resulting code is:
1609
1610 -- A'length (1) /= B'length (1)
1611 -- or else
1612 -- A'length (2) /= B'length (2)
1613 -- or else
1614 -- ...
1615
1616 --------------
1617 -- Arr_Attr --
1618 --------------
1619
1620 function Arr_Attr
1621 (Arr : Entity_Id;
1622 Nam : Name_Id;
1623 Num : Int) return Node_Id
1624 is
1625 begin
1626 return
1627 Make_Attribute_Reference (Loc,
1628 Attribute_Name => Nam,
1629 Prefix => New_Reference_To (Arr, Loc),
1630 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1631 end Arr_Attr;
1632
1633 ------------------------
1634 -- Component_Equality --
1635 ------------------------
1636
1637 function Component_Equality (Typ : Entity_Id) return Node_Id is
1638 Test : Node_Id;
1639 L, R : Node_Id;
1640
1641 begin
1642 -- if a(i1...) /= b(j1...) then return false; end if;
1643
1644 L :=
1645 Make_Indexed_Component (Loc,
1646 Prefix => Make_Identifier (Loc, Chars (A)),
1647 Expressions => Index_List1);
1648
1649 R :=
1650 Make_Indexed_Component (Loc,
1651 Prefix => Make_Identifier (Loc, Chars (B)),
1652 Expressions => Index_List2);
1653
1654 Test := Expand_Composite_Equality
1655 (Nod, Component_Type (Typ), L, R, Decls);
1656
1657 -- If some (sub)component is an unchecked_union, the whole operation
1658 -- will raise program error.
1659
1660 if Nkind (Test) = N_Raise_Program_Error then
1661
1662 -- This node is going to be inserted at a location where a
1663 -- statement is expected: clear its Etype so analysis will set
1664 -- it to the expected Standard_Void_Type.
1665
1666 Set_Etype (Test, Empty);
1667 return Test;
1668
1669 else
1670 return
1671 Make_Implicit_If_Statement (Nod,
1672 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1673 Then_Statements => New_List (
1674 Make_Simple_Return_Statement (Loc,
1675 Expression => New_Occurrence_Of (Standard_False, Loc))));
1676 end if;
1677 end Component_Equality;
1678
1679 ------------------
1680 -- Get_Arg_Type --
1681 ------------------
1682
1683 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1684 T : Entity_Id;
1685 X : Node_Id;
1686
1687 begin
1688 T := Etype (N);
1689
1690 if No (T) then
1691 return Typ;
1692
1693 else
1694 T := Underlying_Type (T);
1695
1696 X := First_Index (T);
1697 while Present (X) loop
1698 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1699 or else
1700 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1701 then
1702 T := Base_Type (T);
1703 exit;
1704 end if;
1705
1706 Next_Index (X);
1707 end loop;
1708
1709 return T;
1710 end if;
1711 end Get_Arg_Type;
1712
1713 --------------------------
1714 -- Handle_One_Dimension --
1715 ---------------------------
1716
1717 function Handle_One_Dimension
1718 (N : Int;
1719 Index : Node_Id) return Node_Id
1720 is
1721 Need_Separate_Indexes : constant Boolean :=
1722 Ltyp /= Rtyp
1723 or else not Is_Constrained (Ltyp);
1724 -- If the index types are identical, and we are working with
1725 -- constrained types, then we can use the same index for both
1726 -- of the arrays.
1727
1728 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1729
1730 Bn : Entity_Id;
1731 Index_T : Entity_Id;
1732 Stm_List : List_Id;
1733 Loop_Stm : Node_Id;
1734
1735 begin
1736 if N > Number_Dimensions (Ltyp) then
1737 return Component_Equality (Ltyp);
1738 end if;
1739
1740 -- Case where we generate a loop
1741
1742 Index_T := Base_Type (Etype (Index));
1743
1744 if Need_Separate_Indexes then
1745 Bn := Make_Temporary (Loc, 'B');
1746 else
1747 Bn := An;
1748 end if;
1749
1750 Append (New_Reference_To (An, Loc), Index_List1);
1751 Append (New_Reference_To (Bn, Loc), Index_List2);
1752
1753 Stm_List := New_List (
1754 Handle_One_Dimension (N + 1, Next_Index (Index)));
1755
1756 if Need_Separate_Indexes then
1757
1758 -- Generate guard for loop, followed by increments of indexes
1759
1760 Append_To (Stm_List,
1761 Make_Exit_Statement (Loc,
1762 Condition =>
1763 Make_Op_Eq (Loc,
1764 Left_Opnd => New_Reference_To (An, Loc),
1765 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1766
1767 Append_To (Stm_List,
1768 Make_Assignment_Statement (Loc,
1769 Name => New_Reference_To (An, Loc),
1770 Expression =>
1771 Make_Attribute_Reference (Loc,
1772 Prefix => New_Reference_To (Index_T, Loc),
1773 Attribute_Name => Name_Succ,
1774 Expressions => New_List (New_Reference_To (An, Loc)))));
1775
1776 Append_To (Stm_List,
1777 Make_Assignment_Statement (Loc,
1778 Name => New_Reference_To (Bn, Loc),
1779 Expression =>
1780 Make_Attribute_Reference (Loc,
1781 Prefix => New_Reference_To (Index_T, Loc),
1782 Attribute_Name => Name_Succ,
1783 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1784 end if;
1785
1786 -- If separate indexes, we need a declare block for An and Bn, and a
1787 -- loop without an iteration scheme.
1788
1789 if Need_Separate_Indexes then
1790 Loop_Stm :=
1791 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1792
1793 return
1794 Make_Block_Statement (Loc,
1795 Declarations => New_List (
1796 Make_Object_Declaration (Loc,
1797 Defining_Identifier => An,
1798 Object_Definition => New_Reference_To (Index_T, Loc),
1799 Expression => Arr_Attr (A, Name_First, N)),
1800
1801 Make_Object_Declaration (Loc,
1802 Defining_Identifier => Bn,
1803 Object_Definition => New_Reference_To (Index_T, Loc),
1804 Expression => Arr_Attr (B, Name_First, N))),
1805
1806 Handled_Statement_Sequence =>
1807 Make_Handled_Sequence_Of_Statements (Loc,
1808 Statements => New_List (Loop_Stm)));
1809
1810 -- If no separate indexes, return loop statement with explicit
1811 -- iteration scheme on its own
1812
1813 else
1814 Loop_Stm :=
1815 Make_Implicit_Loop_Statement (Nod,
1816 Statements => Stm_List,
1817 Iteration_Scheme =>
1818 Make_Iteration_Scheme (Loc,
1819 Loop_Parameter_Specification =>
1820 Make_Loop_Parameter_Specification (Loc,
1821 Defining_Identifier => An,
1822 Discrete_Subtype_Definition =>
1823 Arr_Attr (A, Name_Range, N))));
1824 return Loop_Stm;
1825 end if;
1826 end Handle_One_Dimension;
1827
1828 -----------------------
1829 -- Test_Empty_Arrays --
1830 -----------------------
1831
1832 function Test_Empty_Arrays return Node_Id is
1833 Alist : Node_Id;
1834 Blist : Node_Id;
1835
1836 Atest : Node_Id;
1837 Btest : Node_Id;
1838
1839 begin
1840 Alist := Empty;
1841 Blist := Empty;
1842 for J in 1 .. Number_Dimensions (Ltyp) loop
1843 Atest :=
1844 Make_Op_Eq (Loc,
1845 Left_Opnd => Arr_Attr (A, Name_Length, J),
1846 Right_Opnd => Make_Integer_Literal (Loc, 0));
1847
1848 Btest :=
1849 Make_Op_Eq (Loc,
1850 Left_Opnd => Arr_Attr (B, Name_Length, J),
1851 Right_Opnd => Make_Integer_Literal (Loc, 0));
1852
1853 if No (Alist) then
1854 Alist := Atest;
1855 Blist := Btest;
1856
1857 else
1858 Alist :=
1859 Make_Or_Else (Loc,
1860 Left_Opnd => Relocate_Node (Alist),
1861 Right_Opnd => Atest);
1862
1863 Blist :=
1864 Make_Or_Else (Loc,
1865 Left_Opnd => Relocate_Node (Blist),
1866 Right_Opnd => Btest);
1867 end if;
1868 end loop;
1869
1870 return
1871 Make_And_Then (Loc,
1872 Left_Opnd => Alist,
1873 Right_Opnd => Blist);
1874 end Test_Empty_Arrays;
1875
1876 -----------------------------
1877 -- Test_Lengths_Correspond --
1878 -----------------------------
1879
1880 function Test_Lengths_Correspond return Node_Id is
1881 Result : Node_Id;
1882 Rtest : Node_Id;
1883
1884 begin
1885 Result := Empty;
1886 for J in 1 .. Number_Dimensions (Ltyp) loop
1887 Rtest :=
1888 Make_Op_Ne (Loc,
1889 Left_Opnd => Arr_Attr (A, Name_Length, J),
1890 Right_Opnd => Arr_Attr (B, Name_Length, J));
1891
1892 if No (Result) then
1893 Result := Rtest;
1894 else
1895 Result :=
1896 Make_Or_Else (Loc,
1897 Left_Opnd => Relocate_Node (Result),
1898 Right_Opnd => Rtest);
1899 end if;
1900 end loop;
1901
1902 return Result;
1903 end Test_Lengths_Correspond;
1904
1905 -- Start of processing for Expand_Array_Equality
1906
1907 begin
1908 Ltyp := Get_Arg_Type (Lhs);
1909 Rtyp := Get_Arg_Type (Rhs);
1910
1911 -- For now, if the argument types are not the same, go to the base type,
1912 -- since the code assumes that the formals have the same type. This is
1913 -- fixable in future ???
1914
1915 if Ltyp /= Rtyp then
1916 Ltyp := Base_Type (Ltyp);
1917 Rtyp := Base_Type (Rtyp);
1918 pragma Assert (Ltyp = Rtyp);
1919 end if;
1920
1921 -- Build list of formals for function
1922
1923 Formals := New_List (
1924 Make_Parameter_Specification (Loc,
1925 Defining_Identifier => A,
1926 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1927
1928 Make_Parameter_Specification (Loc,
1929 Defining_Identifier => B,
1930 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1931
1932 Func_Name := Make_Temporary (Loc, 'E');
1933
1934 -- Build statement sequence for function
1935
1936 Func_Body :=
1937 Make_Subprogram_Body (Loc,
1938 Specification =>
1939 Make_Function_Specification (Loc,
1940 Defining_Unit_Name => Func_Name,
1941 Parameter_Specifications => Formals,
1942 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1943
1944 Declarations => Decls,
1945
1946 Handled_Statement_Sequence =>
1947 Make_Handled_Sequence_Of_Statements (Loc,
1948 Statements => New_List (
1949
1950 Make_Implicit_If_Statement (Nod,
1951 Condition => Test_Empty_Arrays,
1952 Then_Statements => New_List (
1953 Make_Simple_Return_Statement (Loc,
1954 Expression =>
1955 New_Occurrence_Of (Standard_True, Loc)))),
1956
1957 Make_Implicit_If_Statement (Nod,
1958 Condition => Test_Lengths_Correspond,
1959 Then_Statements => New_List (
1960 Make_Simple_Return_Statement (Loc,
1961 Expression =>
1962 New_Occurrence_Of (Standard_False, Loc)))),
1963
1964 Handle_One_Dimension (1, First_Index (Ltyp)),
1965
1966 Make_Simple_Return_Statement (Loc,
1967 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1968
1969 Set_Has_Completion (Func_Name, True);
1970 Set_Is_Inlined (Func_Name);
1971
1972 -- If the array type is distinct from the type of the arguments, it
1973 -- is the full view of a private type. Apply an unchecked conversion
1974 -- to insure that analysis of the call succeeds.
1975
1976 declare
1977 L, R : Node_Id;
1978
1979 begin
1980 L := Lhs;
1981 R := Rhs;
1982
1983 if No (Etype (Lhs))
1984 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1985 then
1986 L := OK_Convert_To (Ltyp, Lhs);
1987 end if;
1988
1989 if No (Etype (Rhs))
1990 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1991 then
1992 R := OK_Convert_To (Rtyp, Rhs);
1993 end if;
1994
1995 Actuals := New_List (L, R);
1996 end;
1997
1998 Append_To (Bodies, Func_Body);
1999
2000 return
2001 Make_Function_Call (Loc,
2002 Name => New_Reference_To (Func_Name, Loc),
2003 Parameter_Associations => Actuals);
2004 end Expand_Array_Equality;
2005
2006 -----------------------------
2007 -- Expand_Boolean_Operator --
2008 -----------------------------
2009
2010 -- Note that we first get the actual subtypes of the operands, since we
2011 -- always want to deal with types that have bounds.
2012
2013 procedure Expand_Boolean_Operator (N : Node_Id) is
2014 Typ : constant Entity_Id := Etype (N);
2015
2016 begin
2017 -- Special case of bit packed array where both operands are known to be
2018 -- properly aligned. In this case we use an efficient run time routine
2019 -- to carry out the operation (see System.Bit_Ops).
2020
2021 if Is_Bit_Packed_Array (Typ)
2022 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2023 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2024 then
2025 Expand_Packed_Boolean_Operator (N);
2026 return;
2027 end if;
2028
2029 -- For the normal non-packed case, the general expansion is to build
2030 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2031 -- and then inserting it into the tree. The original operator node is
2032 -- then rewritten as a call to this function. We also use this in the
2033 -- packed case if either operand is a possibly unaligned object.
2034
2035 declare
2036 Loc : constant Source_Ptr := Sloc (N);
2037 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2038 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2039 Func_Body : Node_Id;
2040 Func_Name : Entity_Id;
2041
2042 begin
2043 Convert_To_Actual_Subtype (L);
2044 Convert_To_Actual_Subtype (R);
2045 Ensure_Defined (Etype (L), N);
2046 Ensure_Defined (Etype (R), N);
2047 Apply_Length_Check (R, Etype (L));
2048
2049 if Nkind (N) = N_Op_Xor then
2050 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2051 end if;
2052
2053 if Nkind (Parent (N)) = N_Assignment_Statement
2054 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2055 then
2056 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2057
2058 elsif Nkind (Parent (N)) = N_Op_Not
2059 and then Nkind (N) = N_Op_And
2060 and then
2061 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2062 then
2063 return;
2064 else
2065
2066 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2067 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2068 Insert_Action (N, Func_Body);
2069
2070 -- Now rewrite the expression with a call
2071
2072 Rewrite (N,
2073 Make_Function_Call (Loc,
2074 Name => New_Reference_To (Func_Name, Loc),
2075 Parameter_Associations =>
2076 New_List (
2077 L,
2078 Make_Type_Conversion
2079 (Loc, New_Reference_To (Etype (L), Loc), R))));
2080
2081 Analyze_And_Resolve (N, Typ);
2082 end if;
2083 end;
2084 end Expand_Boolean_Operator;
2085
2086 -------------------------------
2087 -- Expand_Composite_Equality --
2088 -------------------------------
2089
2090 -- This function is only called for comparing internal fields of composite
2091 -- types when these fields are themselves composites. This is a special
2092 -- case because it is not possible to respect normal Ada visibility rules.
2093
2094 function Expand_Composite_Equality
2095 (Nod : Node_Id;
2096 Typ : Entity_Id;
2097 Lhs : Node_Id;
2098 Rhs : Node_Id;
2099 Bodies : List_Id) return Node_Id
2100 is
2101 Loc : constant Source_Ptr := Sloc (Nod);
2102 Full_Type : Entity_Id;
2103 Prim : Elmt_Id;
2104 Eq_Op : Entity_Id;
2105
2106 function Find_Primitive_Eq return Node_Id;
2107 -- AI05-0123: Locate primitive equality for type if it exists, and
2108 -- build the corresponding call. If operation is abstract, replace
2109 -- call with an explicit raise. Return Empty if there is no primitive.
2110
2111 -----------------------
2112 -- Find_Primitive_Eq --
2113 -----------------------
2114
2115 function Find_Primitive_Eq return Node_Id is
2116 Prim_E : Elmt_Id;
2117 Prim : Node_Id;
2118
2119 begin
2120 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2121 while Present (Prim_E) loop
2122 Prim := Node (Prim_E);
2123
2124 -- Locate primitive equality with the right signature
2125
2126 if Chars (Prim) = Name_Op_Eq
2127 and then Etype (First_Formal (Prim)) =
2128 Etype (Next_Formal (First_Formal (Prim)))
2129 and then Etype (Prim) = Standard_Boolean
2130 then
2131 if Is_Abstract_Subprogram (Prim) then
2132 return
2133 Make_Raise_Program_Error (Loc,
2134 Reason => PE_Explicit_Raise);
2135
2136 else
2137 return
2138 Make_Function_Call (Loc,
2139 Name => New_Reference_To (Prim, Loc),
2140 Parameter_Associations => New_List (Lhs, Rhs));
2141 end if;
2142 end if;
2143
2144 Next_Elmt (Prim_E);
2145 end loop;
2146
2147 -- If not found, predefined operation will be used
2148
2149 return Empty;
2150 end Find_Primitive_Eq;
2151
2152 -- Start of processing for Expand_Composite_Equality
2153
2154 begin
2155 if Is_Private_Type (Typ) then
2156 Full_Type := Underlying_Type (Typ);
2157 else
2158 Full_Type := Typ;
2159 end if;
2160
2161 -- Defense against malformed private types with no completion the error
2162 -- will be diagnosed later by check_completion
2163
2164 if No (Full_Type) then
2165 return New_Reference_To (Standard_False, Loc);
2166 end if;
2167
2168 Full_Type := Base_Type (Full_Type);
2169
2170 if Is_Array_Type (Full_Type) then
2171
2172 -- If the operand is an elementary type other than a floating-point
2173 -- type, then we can simply use the built-in block bitwise equality,
2174 -- since the predefined equality operators always apply and bitwise
2175 -- equality is fine for all these cases.
2176
2177 if Is_Elementary_Type (Component_Type (Full_Type))
2178 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2179 then
2180 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2181
2182 -- For composite component types, and floating-point types, use the
2183 -- expansion. This deals with tagged component types (where we use
2184 -- the applicable equality routine) and floating-point, (where we
2185 -- need to worry about negative zeroes), and also the case of any
2186 -- composite type recursively containing such fields.
2187
2188 else
2189 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2190 end if;
2191
2192 elsif Is_Tagged_Type (Full_Type) then
2193
2194 -- Call the primitive operation "=" of this type
2195
2196 if Is_Class_Wide_Type (Full_Type) then
2197 Full_Type := Root_Type (Full_Type);
2198 end if;
2199
2200 -- If this is derived from an untagged private type completed with a
2201 -- tagged type, it does not have a full view, so we use the primitive
2202 -- operations of the private type. This check should no longer be
2203 -- necessary when these types receive their full views ???
2204
2205 if Is_Private_Type (Typ)
2206 and then not Is_Tagged_Type (Typ)
2207 and then not Is_Controlled (Typ)
2208 and then Is_Derived_Type (Typ)
2209 and then No (Full_View (Typ))
2210 then
2211 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2212 else
2213 Prim := First_Elmt (Primitive_Operations (Full_Type));
2214 end if;
2215
2216 loop
2217 Eq_Op := Node (Prim);
2218 exit when Chars (Eq_Op) = Name_Op_Eq
2219 and then Etype (First_Formal (Eq_Op)) =
2220 Etype (Next_Formal (First_Formal (Eq_Op)))
2221 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2222 Next_Elmt (Prim);
2223 pragma Assert (Present (Prim));
2224 end loop;
2225
2226 Eq_Op := Node (Prim);
2227
2228 return
2229 Make_Function_Call (Loc,
2230 Name => New_Reference_To (Eq_Op, Loc),
2231 Parameter_Associations =>
2232 New_List
2233 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2234 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2235
2236 elsif Is_Record_Type (Full_Type) then
2237 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2238
2239 if Present (Eq_Op) then
2240 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2241
2242 -- Inherited equality from parent type. Convert the actuals to
2243 -- match signature of operation.
2244
2245 declare
2246 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2247
2248 begin
2249 return
2250 Make_Function_Call (Loc,
2251 Name => New_Reference_To (Eq_Op, Loc),
2252 Parameter_Associations => New_List (
2253 OK_Convert_To (T, Lhs),
2254 OK_Convert_To (T, Rhs)));
2255 end;
2256
2257 else
2258 -- Comparison between Unchecked_Union components
2259
2260 if Is_Unchecked_Union (Full_Type) then
2261 declare
2262 Lhs_Type : Node_Id := Full_Type;
2263 Rhs_Type : Node_Id := Full_Type;
2264 Lhs_Discr_Val : Node_Id;
2265 Rhs_Discr_Val : Node_Id;
2266
2267 begin
2268 -- Lhs subtype
2269
2270 if Nkind (Lhs) = N_Selected_Component then
2271 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2272 end if;
2273
2274 -- Rhs subtype
2275
2276 if Nkind (Rhs) = N_Selected_Component then
2277 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2278 end if;
2279
2280 -- Lhs of the composite equality
2281
2282 if Is_Constrained (Lhs_Type) then
2283
2284 -- Since the enclosing record type can never be an
2285 -- Unchecked_Union (this code is executed for records
2286 -- that do not have variants), we may reference its
2287 -- discriminant(s).
2288
2289 if Nkind (Lhs) = N_Selected_Component
2290 and then Has_Per_Object_Constraint (
2291 Entity (Selector_Name (Lhs)))
2292 then
2293 Lhs_Discr_Val :=
2294 Make_Selected_Component (Loc,
2295 Prefix => Prefix (Lhs),
2296 Selector_Name =>
2297 New_Copy
2298 (Get_Discriminant_Value
2299 (First_Discriminant (Lhs_Type),
2300 Lhs_Type,
2301 Stored_Constraint (Lhs_Type))));
2302
2303 else
2304 Lhs_Discr_Val :=
2305 New_Copy
2306 (Get_Discriminant_Value
2307 (First_Discriminant (Lhs_Type),
2308 Lhs_Type,
2309 Stored_Constraint (Lhs_Type)));
2310
2311 end if;
2312 else
2313 -- It is not possible to infer the discriminant since
2314 -- the subtype is not constrained.
2315
2316 return
2317 Make_Raise_Program_Error (Loc,
2318 Reason => PE_Unchecked_Union_Restriction);
2319 end if;
2320
2321 -- Rhs of the composite equality
2322
2323 if Is_Constrained (Rhs_Type) then
2324 if Nkind (Rhs) = N_Selected_Component
2325 and then Has_Per_Object_Constraint
2326 (Entity (Selector_Name (Rhs)))
2327 then
2328 Rhs_Discr_Val :=
2329 Make_Selected_Component (Loc,
2330 Prefix => Prefix (Rhs),
2331 Selector_Name =>
2332 New_Copy
2333 (Get_Discriminant_Value
2334 (First_Discriminant (Rhs_Type),
2335 Rhs_Type,
2336 Stored_Constraint (Rhs_Type))));
2337
2338 else
2339 Rhs_Discr_Val :=
2340 New_Copy
2341 (Get_Discriminant_Value
2342 (First_Discriminant (Rhs_Type),
2343 Rhs_Type,
2344 Stored_Constraint (Rhs_Type)));
2345
2346 end if;
2347 else
2348 return
2349 Make_Raise_Program_Error (Loc,
2350 Reason => PE_Unchecked_Union_Restriction);
2351 end if;
2352
2353 -- Call the TSS equality function with the inferred
2354 -- discriminant values.
2355
2356 return
2357 Make_Function_Call (Loc,
2358 Name => New_Reference_To (Eq_Op, Loc),
2359 Parameter_Associations => New_List (
2360 Lhs,
2361 Rhs,
2362 Lhs_Discr_Val,
2363 Rhs_Discr_Val));
2364 end;
2365
2366 else
2367 return
2368 Make_Function_Call (Loc,
2369 Name => New_Reference_To (Eq_Op, Loc),
2370 Parameter_Associations => New_List (Lhs, Rhs));
2371 end if;
2372 end if;
2373
2374 elsif Ada_Version >= Ada_2012 then
2375
2376 -- if no TSS has been created for the type, check whether there is
2377 -- a primitive equality declared for it.
2378
2379 declare
2380 Ada_2012_Op : constant Node_Id := Find_Primitive_Eq;
2381
2382 begin
2383 if Present (Ada_2012_Op) then
2384 return Ada_2012_Op;
2385 else
2386
2387 -- Use predefined equality if no user-defined primitive exists
2388
2389 return Make_Op_Eq (Loc, Lhs, Rhs);
2390 end if;
2391 end;
2392
2393 else
2394 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2395 end if;
2396
2397 else
2398 -- If not array or record type, it is predefined equality.
2399
2400 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2401 end if;
2402 end Expand_Composite_Equality;
2403
2404 ------------------------
2405 -- Expand_Concatenate --
2406 ------------------------
2407
2408 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2409 Loc : constant Source_Ptr := Sloc (Cnode);
2410
2411 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2412 -- Result type of concatenation
2413
2414 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2415 -- Component type. Elements of this component type can appear as one
2416 -- of the operands of concatenation as well as arrays.
2417
2418 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2419 -- Index subtype
2420
2421 Ityp : constant Entity_Id := Base_Type (Istyp);
2422 -- Index type. This is the base type of the index subtype, and is used
2423 -- for all computed bounds (which may be out of range of Istyp in the
2424 -- case of null ranges).
2425
2426 Artyp : Entity_Id;
2427 -- This is the type we use to do arithmetic to compute the bounds and
2428 -- lengths of operands. The choice of this type is a little subtle and
2429 -- is discussed in a separate section at the start of the body code.
2430
2431 Concatenation_Error : exception;
2432 -- Raised if concatenation is sure to raise a CE
2433
2434 Result_May_Be_Null : Boolean := True;
2435 -- Reset to False if at least one operand is encountered which is known
2436 -- at compile time to be non-null. Used for handling the special case
2437 -- of setting the high bound to the last operand high bound for a null
2438 -- result, thus ensuring a proper high bound in the super-flat case.
2439
2440 N : constant Nat := List_Length (Opnds);
2441 -- Number of concatenation operands including possibly null operands
2442
2443 NN : Nat := 0;
2444 -- Number of operands excluding any known to be null, except that the
2445 -- last operand is always retained, in case it provides the bounds for
2446 -- a null result.
2447
2448 Opnd : Node_Id;
2449 -- Current operand being processed in the loop through operands. After
2450 -- this loop is complete, always contains the last operand (which is not
2451 -- the same as Operands (NN), since null operands are skipped).
2452
2453 -- Arrays describing the operands, only the first NN entries of each
2454 -- array are set (NN < N when we exclude known null operands).
2455
2456 Is_Fixed_Length : array (1 .. N) of Boolean;
2457 -- True if length of corresponding operand known at compile time
2458
2459 Operands : array (1 .. N) of Node_Id;
2460 -- Set to the corresponding entry in the Opnds list (but note that null
2461 -- operands are excluded, so not all entries in the list are stored).
2462
2463 Fixed_Length : array (1 .. N) of Uint;
2464 -- Set to length of operand. Entries in this array are set only if the
2465 -- corresponding entry in Is_Fixed_Length is True.
2466
2467 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2468 -- Set to lower bound of operand. Either an integer literal in the case
2469 -- where the bound is known at compile time, else actual lower bound.
2470 -- The operand low bound is of type Ityp.
2471
2472 Var_Length : array (1 .. N) of Entity_Id;
2473 -- Set to an entity of type Natural that contains the length of an
2474 -- operand whose length is not known at compile time. Entries in this
2475 -- array are set only if the corresponding entry in Is_Fixed_Length
2476 -- is False. The entity is of type Artyp.
2477
2478 Aggr_Length : array (0 .. N) of Node_Id;
2479 -- The J'th entry in an expression node that represents the total length
2480 -- of operands 1 through J. It is either an integer literal node, or a
2481 -- reference to a constant entity with the right value, so it is fine
2482 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2483 -- entry always is set to zero. The length is of type Artyp.
2484
2485 Low_Bound : Node_Id;
2486 -- A tree node representing the low bound of the result (of type Ityp).
2487 -- This is either an integer literal node, or an identifier reference to
2488 -- a constant entity initialized to the appropriate value.
2489
2490 Last_Opnd_High_Bound : Node_Id;
2491 -- A tree node representing the high bound of the last operand. This
2492 -- need only be set if the result could be null. It is used for the
2493 -- special case of setting the right high bound for a null result.
2494 -- This is of type Ityp.
2495
2496 High_Bound : Node_Id;
2497 -- A tree node representing the high bound of the result (of type Ityp)
2498
2499 Result : Node_Id;
2500 -- Result of the concatenation (of type Ityp)
2501
2502 Actions : constant List_Id := New_List;
2503 -- Collect actions to be inserted if Save_Space is False
2504
2505 Save_Space : Boolean;
2506 pragma Warnings (Off, Save_Space);
2507 -- Set to True if we are saving generated code space by calling routines
2508 -- in packages System.Concat_n.
2509
2510 Known_Non_Null_Operand_Seen : Boolean;
2511 -- Set True during generation of the assignments of operands into
2512 -- result once an operand known to be non-null has been seen.
2513
2514 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2515 -- This function makes an N_Integer_Literal node that is returned in
2516 -- analyzed form with the type set to Artyp. Importantly this literal
2517 -- is not flagged as static, so that if we do computations with it that
2518 -- result in statically detected out of range conditions, we will not
2519 -- generate error messages but instead warning messages.
2520
2521 function To_Artyp (X : Node_Id) return Node_Id;
2522 -- Given a node of type Ityp, returns the corresponding value of type
2523 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2524 -- For enum types, the Pos of the value is returned.
2525
2526 function To_Ityp (X : Node_Id) return Node_Id;
2527 -- The inverse function (uses Val in the case of enumeration types)
2528
2529 ------------------------
2530 -- Make_Artyp_Literal --
2531 ------------------------
2532
2533 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2534 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2535 begin
2536 Set_Etype (Result, Artyp);
2537 Set_Analyzed (Result, True);
2538 Set_Is_Static_Expression (Result, False);
2539 return Result;
2540 end Make_Artyp_Literal;
2541
2542 --------------
2543 -- To_Artyp --
2544 --------------
2545
2546 function To_Artyp (X : Node_Id) return Node_Id is
2547 begin
2548 if Ityp = Base_Type (Artyp) then
2549 return X;
2550
2551 elsif Is_Enumeration_Type (Ityp) then
2552 return
2553 Make_Attribute_Reference (Loc,
2554 Prefix => New_Occurrence_Of (Ityp, Loc),
2555 Attribute_Name => Name_Pos,
2556 Expressions => New_List (X));
2557
2558 else
2559 return Convert_To (Artyp, X);
2560 end if;
2561 end To_Artyp;
2562
2563 -------------
2564 -- To_Ityp --
2565 -------------
2566
2567 function To_Ityp (X : Node_Id) return Node_Id is
2568 begin
2569 if Is_Enumeration_Type (Ityp) then
2570 return
2571 Make_Attribute_Reference (Loc,
2572 Prefix => New_Occurrence_Of (Ityp, Loc),
2573 Attribute_Name => Name_Val,
2574 Expressions => New_List (X));
2575
2576 -- Case where we will do a type conversion
2577
2578 else
2579 if Ityp = Base_Type (Artyp) then
2580 return X;
2581 else
2582 return Convert_To (Ityp, X);
2583 end if;
2584 end if;
2585 end To_Ityp;
2586
2587 -- Local Declarations
2588
2589 Opnd_Typ : Entity_Id;
2590 Ent : Entity_Id;
2591 Len : Uint;
2592 J : Nat;
2593 Clen : Node_Id;
2594 Set : Boolean;
2595
2596 begin
2597 -- Choose an appropriate computational type
2598
2599 -- We will be doing calculations of lengths and bounds in this routine
2600 -- and computing one from the other in some cases, e.g. getting the high
2601 -- bound by adding the length-1 to the low bound.
2602
2603 -- We can't just use the index type, or even its base type for this
2604 -- purpose for two reasons. First it might be an enumeration type which
2605 -- is not suitable for computations of any kind, and second it may
2606 -- simply not have enough range. For example if the index type is
2607 -- -128..+127 then lengths can be up to 256, which is out of range of
2608 -- the type.
2609
2610 -- For enumeration types, we can simply use Standard_Integer, this is
2611 -- sufficient since the actual number of enumeration literals cannot
2612 -- possibly exceed the range of integer (remember we will be doing the
2613 -- arithmetic with POS values, not representation values).
2614
2615 if Is_Enumeration_Type (Ityp) then
2616 Artyp := Standard_Integer;
2617
2618 -- If index type is Positive, we use the standard unsigned type, to give
2619 -- more room on the top of the range, obviating the need for an overflow
2620 -- check when creating the upper bound. This is needed to avoid junk
2621 -- overflow checks in the common case of String types.
2622
2623 -- ??? Disabled for now
2624
2625 -- elsif Istyp = Standard_Positive then
2626 -- Artyp := Standard_Unsigned;
2627
2628 -- For modular types, we use a 32-bit modular type for types whose size
2629 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2630 -- identity type, and for larger unsigned types we use 64-bits.
2631
2632 elsif Is_Modular_Integer_Type (Ityp) then
2633 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2634 Artyp := Standard_Unsigned;
2635 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2636 Artyp := Ityp;
2637 else
2638 Artyp := RTE (RE_Long_Long_Unsigned);
2639 end if;
2640
2641 -- Similar treatment for signed types
2642
2643 else
2644 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2645 Artyp := Standard_Integer;
2646 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2647 Artyp := Ityp;
2648 else
2649 Artyp := Standard_Long_Long_Integer;
2650 end if;
2651 end if;
2652
2653 -- Supply dummy entry at start of length array
2654
2655 Aggr_Length (0) := Make_Artyp_Literal (0);
2656
2657 -- Go through operands setting up the above arrays
2658
2659 J := 1;
2660 while J <= N loop
2661 Opnd := Remove_Head (Opnds);
2662 Opnd_Typ := Etype (Opnd);
2663
2664 -- The parent got messed up when we put the operands in a list,
2665 -- so now put back the proper parent for the saved operand, that
2666 -- is to say the concatenation node, to make sure that each operand
2667 -- is seen as a subexpression, e.g. if actions must be inserted.
2668
2669 Set_Parent (Opnd, Cnode);
2670
2671 -- Set will be True when we have setup one entry in the array
2672
2673 Set := False;
2674
2675 -- Singleton element (or character literal) case
2676
2677 if Base_Type (Opnd_Typ) = Ctyp then
2678 NN := NN + 1;
2679 Operands (NN) := Opnd;
2680 Is_Fixed_Length (NN) := True;
2681 Fixed_Length (NN) := Uint_1;
2682 Result_May_Be_Null := False;
2683
2684 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2685 -- since we know that the result cannot be null).
2686
2687 Opnd_Low_Bound (NN) :=
2688 Make_Attribute_Reference (Loc,
2689 Prefix => New_Reference_To (Istyp, Loc),
2690 Attribute_Name => Name_First);
2691
2692 Set := True;
2693
2694 -- String literal case (can only occur for strings of course)
2695
2696 elsif Nkind (Opnd) = N_String_Literal then
2697 Len := String_Literal_Length (Opnd_Typ);
2698
2699 if Len /= 0 then
2700 Result_May_Be_Null := False;
2701 end if;
2702
2703 -- Capture last operand high bound if result could be null
2704
2705 if J = N and then Result_May_Be_Null then
2706 Last_Opnd_High_Bound :=
2707 Make_Op_Add (Loc,
2708 Left_Opnd =>
2709 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2710 Right_Opnd => Make_Integer_Literal (Loc, 1));
2711 end if;
2712
2713 -- Skip null string literal
2714
2715 if J < N and then Len = 0 then
2716 goto Continue;
2717 end if;
2718
2719 NN := NN + 1;
2720 Operands (NN) := Opnd;
2721 Is_Fixed_Length (NN) := True;
2722
2723 -- Set length and bounds
2724
2725 Fixed_Length (NN) := Len;
2726
2727 Opnd_Low_Bound (NN) :=
2728 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2729
2730 Set := True;
2731
2732 -- All other cases
2733
2734 else
2735 -- Check constrained case with known bounds
2736
2737 if Is_Constrained (Opnd_Typ) then
2738 declare
2739 Index : constant Node_Id := First_Index (Opnd_Typ);
2740 Indx_Typ : constant Entity_Id := Etype (Index);
2741 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2742 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2743
2744 begin
2745 -- Fixed length constrained array type with known at compile
2746 -- time bounds is last case of fixed length operand.
2747
2748 if Compile_Time_Known_Value (Lo)
2749 and then
2750 Compile_Time_Known_Value (Hi)
2751 then
2752 declare
2753 Loval : constant Uint := Expr_Value (Lo);
2754 Hival : constant Uint := Expr_Value (Hi);
2755 Len : constant Uint :=
2756 UI_Max (Hival - Loval + 1, Uint_0);
2757
2758 begin
2759 if Len > 0 then
2760 Result_May_Be_Null := False;
2761 end if;
2762
2763 -- Capture last operand bound if result could be null
2764
2765 if J = N and then Result_May_Be_Null then
2766 Last_Opnd_High_Bound :=
2767 Convert_To (Ityp,
2768 Make_Integer_Literal (Loc, Expr_Value (Hi)));
2769 end if;
2770
2771 -- Exclude null length case unless last operand
2772
2773 if J < N and then Len = 0 then
2774 goto Continue;
2775 end if;
2776
2777 NN := NN + 1;
2778 Operands (NN) := Opnd;
2779 Is_Fixed_Length (NN) := True;
2780 Fixed_Length (NN) := Len;
2781
2782 Opnd_Low_Bound (NN) :=
2783 To_Ityp
2784 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
2785 Set := True;
2786 end;
2787 end if;
2788 end;
2789 end if;
2790
2791 -- All cases where the length is not known at compile time, or the
2792 -- special case of an operand which is known to be null but has a
2793 -- lower bound other than 1 or is other than a string type.
2794
2795 if not Set then
2796 NN := NN + 1;
2797
2798 -- Capture operand bounds
2799
2800 Opnd_Low_Bound (NN) :=
2801 Make_Attribute_Reference (Loc,
2802 Prefix =>
2803 Duplicate_Subexpr (Opnd, Name_Req => True),
2804 Attribute_Name => Name_First);
2805
2806 if J = N and Result_May_Be_Null then
2807 Last_Opnd_High_Bound :=
2808 Convert_To (Ityp,
2809 Make_Attribute_Reference (Loc,
2810 Prefix =>
2811 Duplicate_Subexpr (Opnd, Name_Req => True),
2812 Attribute_Name => Name_Last));
2813 end if;
2814
2815 -- Capture length of operand in entity
2816
2817 Operands (NN) := Opnd;
2818 Is_Fixed_Length (NN) := False;
2819
2820 Var_Length (NN) := Make_Temporary (Loc, 'L');
2821
2822 Append_To (Actions,
2823 Make_Object_Declaration (Loc,
2824 Defining_Identifier => Var_Length (NN),
2825 Constant_Present => True,
2826 Object_Definition => New_Occurrence_Of (Artyp, Loc),
2827 Expression =>
2828 Make_Attribute_Reference (Loc,
2829 Prefix =>
2830 Duplicate_Subexpr (Opnd, Name_Req => True),
2831 Attribute_Name => Name_Length)));
2832 end if;
2833 end if;
2834
2835 -- Set next entry in aggregate length array
2836
2837 -- For first entry, make either integer literal for fixed length
2838 -- or a reference to the saved length for variable length.
2839
2840 if NN = 1 then
2841 if Is_Fixed_Length (1) then
2842 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
2843 else
2844 Aggr_Length (1) := New_Reference_To (Var_Length (1), Loc);
2845 end if;
2846
2847 -- If entry is fixed length and only fixed lengths so far, make
2848 -- appropriate new integer literal adding new length.
2849
2850 elsif Is_Fixed_Length (NN)
2851 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2852 then
2853 Aggr_Length (NN) :=
2854 Make_Integer_Literal (Loc,
2855 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2856
2857 -- All other cases, construct an addition node for the length and
2858 -- create an entity initialized to this length.
2859
2860 else
2861 Ent := Make_Temporary (Loc, 'L');
2862
2863 if Is_Fixed_Length (NN) then
2864 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2865 else
2866 Clen := New_Reference_To (Var_Length (NN), Loc);
2867 end if;
2868
2869 Append_To (Actions,
2870 Make_Object_Declaration (Loc,
2871 Defining_Identifier => Ent,
2872 Constant_Present => True,
2873 Object_Definition => New_Occurrence_Of (Artyp, Loc),
2874 Expression =>
2875 Make_Op_Add (Loc,
2876 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2877 Right_Opnd => Clen)));
2878
2879 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2880 end if;
2881
2882 <<Continue>>
2883 J := J + 1;
2884 end loop;
2885
2886 -- If we have only skipped null operands, return the last operand
2887
2888 if NN = 0 then
2889 Result := Opnd;
2890 goto Done;
2891 end if;
2892
2893 -- If we have only one non-null operand, return it and we are done.
2894 -- There is one case in which this cannot be done, and that is when
2895 -- the sole operand is of the element type, in which case it must be
2896 -- converted to an array, and the easiest way of doing that is to go
2897 -- through the normal general circuit.
2898
2899 if NN = 1
2900 and then Base_Type (Etype (Operands (1))) /= Ctyp
2901 then
2902 Result := Operands (1);
2903 goto Done;
2904 end if;
2905
2906 -- Cases where we have a real concatenation
2907
2908 -- Next step is to find the low bound for the result array that we
2909 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2910
2911 -- If the ultimate ancestor of the index subtype is a constrained array
2912 -- definition, then the lower bound is that of the index subtype as
2913 -- specified by (RM 4.5.3(6)).
2914
2915 -- The right test here is to go to the root type, and then the ultimate
2916 -- ancestor is the first subtype of this root type.
2917
2918 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2919 Low_Bound :=
2920 Make_Attribute_Reference (Loc,
2921 Prefix =>
2922 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2923 Attribute_Name => Name_First);
2924
2925 -- If the first operand in the list has known length we know that
2926 -- the lower bound of the result is the lower bound of this operand.
2927
2928 elsif Is_Fixed_Length (1) then
2929 Low_Bound := Opnd_Low_Bound (1);
2930
2931 -- OK, we don't know the lower bound, we have to build a horrible
2932 -- expression actions node of the form
2933
2934 -- if Cond1'Length /= 0 then
2935 -- Opnd1 low bound
2936 -- else
2937 -- if Opnd2'Length /= 0 then
2938 -- Opnd2 low bound
2939 -- else
2940 -- ...
2941
2942 -- The nesting ends either when we hit an operand whose length is known
2943 -- at compile time, or on reaching the last operand, whose low bound we
2944 -- take unconditionally whether or not it is null. It's easiest to do
2945 -- this with a recursive procedure:
2946
2947 else
2948 declare
2949 function Get_Known_Bound (J : Nat) return Node_Id;
2950 -- Returns the lower bound determined by operands J .. NN
2951
2952 ---------------------
2953 -- Get_Known_Bound --
2954 ---------------------
2955
2956 function Get_Known_Bound (J : Nat) return Node_Id is
2957 begin
2958 if Is_Fixed_Length (J) or else J = NN then
2959 return New_Copy (Opnd_Low_Bound (J));
2960
2961 else
2962 return
2963 Make_Conditional_Expression (Loc,
2964 Expressions => New_List (
2965
2966 Make_Op_Ne (Loc,
2967 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2968 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2969
2970 New_Copy (Opnd_Low_Bound (J)),
2971 Get_Known_Bound (J + 1)));
2972 end if;
2973 end Get_Known_Bound;
2974
2975 begin
2976 Ent := Make_Temporary (Loc, 'L');
2977
2978 Append_To (Actions,
2979 Make_Object_Declaration (Loc,
2980 Defining_Identifier => Ent,
2981 Constant_Present => True,
2982 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2983 Expression => Get_Known_Bound (1)));
2984
2985 Low_Bound := New_Reference_To (Ent, Loc);
2986 end;
2987 end if;
2988
2989 -- Now we can safely compute the upper bound, normally
2990 -- Low_Bound + Length - 1.
2991
2992 High_Bound :=
2993 To_Ityp (
2994 Make_Op_Add (Loc,
2995 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2996 Right_Opnd =>
2997 Make_Op_Subtract (Loc,
2998 Left_Opnd => New_Copy (Aggr_Length (NN)),
2999 Right_Opnd => Make_Artyp_Literal (1))));
3000
3001 -- Note that calculation of the high bound may cause overflow in some
3002 -- very weird cases, so in the general case we need an overflow check on
3003 -- the high bound. We can avoid this for the common case of string types
3004 -- and other types whose index is Positive, since we chose a wider range
3005 -- for the arithmetic type.
3006
3007 if Istyp /= Standard_Positive then
3008 Activate_Overflow_Check (High_Bound);
3009 end if;
3010
3011 -- Handle the exceptional case where the result is null, in which case
3012 -- case the bounds come from the last operand (so that we get the proper
3013 -- bounds if the last operand is super-flat).
3014
3015 if Result_May_Be_Null then
3016 High_Bound :=
3017 Make_Conditional_Expression (Loc,
3018 Expressions => New_List (
3019 Make_Op_Eq (Loc,
3020 Left_Opnd => New_Copy (Aggr_Length (NN)),
3021 Right_Opnd => Make_Artyp_Literal (0)),
3022 Last_Opnd_High_Bound,
3023 High_Bound));
3024 end if;
3025
3026 -- Here is where we insert the saved up actions
3027
3028 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3029
3030 -- Now we construct an array object with appropriate bounds. We mark
3031 -- the target as internal to prevent useless initialization when
3032 -- Initialize_Scalars is enabled. Also since this is the actual result
3033 -- entity, we make sure we have debug information for the result.
3034
3035 Ent := Make_Temporary (Loc, 'S');
3036 Set_Is_Internal (Ent);
3037 Set_Needs_Debug_Info (Ent);
3038
3039 -- If the bound is statically known to be out of range, we do not want
3040 -- to abort, we want a warning and a runtime constraint error. Note that
3041 -- we have arranged that the result will not be treated as a static
3042 -- constant, so we won't get an illegality during this insertion.
3043
3044 Insert_Action (Cnode,
3045 Make_Object_Declaration (Loc,
3046 Defining_Identifier => Ent,
3047 Object_Definition =>
3048 Make_Subtype_Indication (Loc,
3049 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3050 Constraint =>
3051 Make_Index_Or_Discriminant_Constraint (Loc,
3052 Constraints => New_List (
3053 Make_Range (Loc,
3054 Low_Bound => Low_Bound,
3055 High_Bound => High_Bound))))),
3056 Suppress => All_Checks);
3057
3058 -- If the result of the concatenation appears as the initializing
3059 -- expression of an object declaration, we can just rename the
3060 -- result, rather than copying it.
3061
3062 Set_OK_To_Rename (Ent);
3063
3064 -- Catch the static out of range case now
3065
3066 if Raises_Constraint_Error (High_Bound) then
3067 raise Concatenation_Error;
3068 end if;
3069
3070 -- Now we will generate the assignments to do the actual concatenation
3071
3072 -- There is one case in which we will not do this, namely when all the
3073 -- following conditions are met:
3074
3075 -- The result type is Standard.String
3076
3077 -- There are nine or fewer retained (non-null) operands
3078
3079 -- The optimization level is -O0
3080
3081 -- The corresponding System.Concat_n.Str_Concat_n routine is
3082 -- available in the run time.
3083
3084 -- The debug flag gnatd.c is not set
3085
3086 -- If all these conditions are met then we generate a call to the
3087 -- relevant concatenation routine. The purpose of this is to avoid
3088 -- undesirable code bloat at -O0.
3089
3090 if Atyp = Standard_String
3091 and then NN in 2 .. 9
3092 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3093 and then not Debug_Flag_Dot_C
3094 then
3095 declare
3096 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3097 (RE_Str_Concat_2,
3098 RE_Str_Concat_3,
3099 RE_Str_Concat_4,
3100 RE_Str_Concat_5,
3101 RE_Str_Concat_6,
3102 RE_Str_Concat_7,
3103 RE_Str_Concat_8,
3104 RE_Str_Concat_9);
3105
3106 begin
3107 if RTE_Available (RR (NN)) then
3108 declare
3109 Opnds : constant List_Id :=
3110 New_List (New_Occurrence_Of (Ent, Loc));
3111
3112 begin
3113 for J in 1 .. NN loop
3114 if Is_List_Member (Operands (J)) then
3115 Remove (Operands (J));
3116 end if;
3117
3118 if Base_Type (Etype (Operands (J))) = Ctyp then
3119 Append_To (Opnds,
3120 Make_Aggregate (Loc,
3121 Component_Associations => New_List (
3122 Make_Component_Association (Loc,
3123 Choices => New_List (
3124 Make_Integer_Literal (Loc, 1)),
3125 Expression => Operands (J)))));
3126
3127 else
3128 Append_To (Opnds, Operands (J));
3129 end if;
3130 end loop;
3131
3132 Insert_Action (Cnode,
3133 Make_Procedure_Call_Statement (Loc,
3134 Name => New_Reference_To (RTE (RR (NN)), Loc),
3135 Parameter_Associations => Opnds));
3136
3137 Result := New_Reference_To (Ent, Loc);
3138 goto Done;
3139 end;
3140 end if;
3141 end;
3142 end if;
3143
3144 -- Not special case so generate the assignments
3145
3146 Known_Non_Null_Operand_Seen := False;
3147
3148 for J in 1 .. NN loop
3149 declare
3150 Lo : constant Node_Id :=
3151 Make_Op_Add (Loc,
3152 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3153 Right_Opnd => Aggr_Length (J - 1));
3154
3155 Hi : constant Node_Id :=
3156 Make_Op_Add (Loc,
3157 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3158 Right_Opnd =>
3159 Make_Op_Subtract (Loc,
3160 Left_Opnd => Aggr_Length (J),
3161 Right_Opnd => Make_Artyp_Literal (1)));
3162
3163 begin
3164 -- Singleton case, simple assignment
3165
3166 if Base_Type (Etype (Operands (J))) = Ctyp then
3167 Known_Non_Null_Operand_Seen := True;
3168 Insert_Action (Cnode,
3169 Make_Assignment_Statement (Loc,
3170 Name =>
3171 Make_Indexed_Component (Loc,
3172 Prefix => New_Occurrence_Of (Ent, Loc),
3173 Expressions => New_List (To_Ityp (Lo))),
3174 Expression => Operands (J)),
3175 Suppress => All_Checks);
3176
3177 -- Array case, slice assignment, skipped when argument is fixed
3178 -- length and known to be null.
3179
3180 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3181 declare
3182 Assign : Node_Id :=
3183 Make_Assignment_Statement (Loc,
3184 Name =>
3185 Make_Slice (Loc,
3186 Prefix =>
3187 New_Occurrence_Of (Ent, Loc),
3188 Discrete_Range =>
3189 Make_Range (Loc,
3190 Low_Bound => To_Ityp (Lo),
3191 High_Bound => To_Ityp (Hi))),
3192 Expression => Operands (J));
3193 begin
3194 if Is_Fixed_Length (J) then
3195 Known_Non_Null_Operand_Seen := True;
3196
3197 elsif not Known_Non_Null_Operand_Seen then
3198
3199 -- Here if operand length is not statically known and no
3200 -- operand known to be non-null has been processed yet.
3201 -- If operand length is 0, we do not need to perform the
3202 -- assignment, and we must avoid the evaluation of the
3203 -- high bound of the slice, since it may underflow if the
3204 -- low bound is Ityp'First.
3205
3206 Assign :=
3207 Make_Implicit_If_Statement (Cnode,
3208 Condition =>
3209 Make_Op_Ne (Loc,
3210 Left_Opnd =>
3211 New_Occurrence_Of (Var_Length (J), Loc),
3212 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3213 Then_Statements => New_List (Assign));
3214 end if;
3215
3216 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3217 end;
3218 end if;
3219 end;
3220 end loop;
3221
3222 -- Finally we build the result, which is a reference to the array object
3223
3224 Result := New_Reference_To (Ent, Loc);
3225
3226 <<Done>>
3227 Rewrite (Cnode, Result);
3228 Analyze_And_Resolve (Cnode, Atyp);
3229
3230 exception
3231 when Concatenation_Error =>
3232
3233 -- Kill warning generated for the declaration of the static out of
3234 -- range high bound, and instead generate a Constraint_Error with
3235 -- an appropriate specific message.
3236
3237 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3238 Apply_Compile_Time_Constraint_Error
3239 (N => Cnode,
3240 Msg => "concatenation result upper bound out of range?",
3241 Reason => CE_Range_Check_Failed);
3242 -- Set_Etype (Cnode, Atyp);
3243 end Expand_Concatenate;
3244
3245 ------------------------
3246 -- Expand_N_Allocator --
3247 ------------------------
3248
3249 procedure Expand_N_Allocator (N : Node_Id) is
3250 PtrT : constant Entity_Id := Etype (N);
3251 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3252 Etyp : constant Entity_Id := Etype (Expression (N));
3253 Loc : constant Source_Ptr := Sloc (N);
3254 Desig : Entity_Id;
3255 Temp : Entity_Id;
3256 Nod : Node_Id;
3257
3258 procedure Rewrite_Coextension (N : Node_Id);
3259 -- Static coextensions have the same lifetime as the entity they
3260 -- constrain. Such occurrences can be rewritten as aliased objects
3261 -- and their unrestricted access used instead of the coextension.
3262
3263 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3264 -- Given a constrained array type E, returns a node representing the
3265 -- code to compute the size in storage elements for the given type.
3266 -- This is done without using the attribute (which malfunctions for
3267 -- large sizes ???)
3268
3269 -------------------------
3270 -- Rewrite_Coextension --
3271 -------------------------
3272
3273 procedure Rewrite_Coextension (N : Node_Id) is
3274 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
3275 Temp_Decl : Node_Id;
3276 Insert_Nod : Node_Id;
3277
3278 begin
3279 -- Generate:
3280 -- Cnn : aliased Etyp;
3281
3282 Temp_Decl :=
3283 Make_Object_Declaration (Loc,
3284 Defining_Identifier => Temp_Id,
3285 Aliased_Present => True,
3286 Object_Definition => New_Occurrence_Of (Etyp, Loc));
3287
3288 if Nkind (Expression (N)) = N_Qualified_Expression then
3289 Set_Expression (Temp_Decl, Expression (Expression (N)));
3290 end if;
3291
3292 -- Find the proper insertion node for the declaration
3293
3294 Insert_Nod := Parent (N);
3295 while Present (Insert_Nod) loop
3296 exit when
3297 Nkind (Insert_Nod) in N_Statement_Other_Than_Procedure_Call
3298 or else Nkind (Insert_Nod) = N_Procedure_Call_Statement
3299 or else Nkind (Insert_Nod) in N_Declaration;
3300
3301 Insert_Nod := Parent (Insert_Nod);
3302 end loop;
3303
3304 Insert_Before (Insert_Nod, Temp_Decl);
3305 Analyze (Temp_Decl);
3306
3307 Rewrite (N,
3308 Make_Attribute_Reference (Loc,
3309 Prefix => New_Occurrence_Of (Temp_Id, Loc),
3310 Attribute_Name => Name_Unrestricted_Access));
3311
3312 Analyze_And_Resolve (N, PtrT);
3313 end Rewrite_Coextension;
3314
3315 ------------------------------
3316 -- Size_In_Storage_Elements --
3317 ------------------------------
3318
3319 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3320 begin
3321 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3322 -- However, the reason for the existence of this function is
3323 -- to construct a test for sizes too large, which means near the
3324 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3325 -- is that we get overflows when sizes are greater than 2**31.
3326
3327 -- So what we end up doing for array types is to use the expression:
3328
3329 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3330
3331 -- which avoids this problem. All this is a bit bogus, but it does
3332 -- mean we catch common cases of trying to allocate arrays that
3333 -- are too large, and which in the absence of a check results in
3334 -- undetected chaos ???
3335
3336 declare
3337 Len : Node_Id;
3338 Res : Node_Id;
3339
3340 begin
3341 for J in 1 .. Number_Dimensions (E) loop
3342 Len :=
3343 Make_Attribute_Reference (Loc,
3344 Prefix => New_Occurrence_Of (E, Loc),
3345 Attribute_Name => Name_Length,
3346 Expressions => New_List (Make_Integer_Literal (Loc, J)));
3347
3348 if J = 1 then
3349 Res := Len;
3350
3351 else
3352 Res :=
3353 Make_Op_Multiply (Loc,
3354 Left_Opnd => Res,
3355 Right_Opnd => Len);
3356 end if;
3357 end loop;
3358
3359 return
3360 Make_Op_Multiply (Loc,
3361 Left_Opnd => Len,
3362 Right_Opnd =>
3363 Make_Attribute_Reference (Loc,
3364 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3365 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3366 end;
3367 end Size_In_Storage_Elements;
3368
3369 -- Start of processing for Expand_N_Allocator
3370
3371 begin
3372 -- RM E.2.3(22). We enforce that the expected type of an allocator
3373 -- shall not be a remote access-to-class-wide-limited-private type
3374
3375 -- Why is this being done at expansion time, seems clearly wrong ???
3376
3377 Validate_Remote_Access_To_Class_Wide_Type (N);
3378
3379 -- Set the Storage Pool
3380
3381 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3382
3383 if Present (Storage_Pool (N)) then
3384 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3385 if VM_Target = No_VM then
3386 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3387 end if;
3388
3389 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3390 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3391
3392 else
3393 Set_Procedure_To_Call (N,
3394 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3395 end if;
3396 end if;
3397
3398 -- Under certain circumstances we can replace an allocator by an access
3399 -- to statically allocated storage. The conditions, as noted in AARM
3400 -- 3.10 (10c) are as follows:
3401
3402 -- Size and initial value is known at compile time
3403 -- Access type is access-to-constant
3404
3405 -- The allocator is not part of a constraint on a record component,
3406 -- because in that case the inserted actions are delayed until the
3407 -- record declaration is fully analyzed, which is too late for the
3408 -- analysis of the rewritten allocator.
3409
3410 if Is_Access_Constant (PtrT)
3411 and then Nkind (Expression (N)) = N_Qualified_Expression
3412 and then Compile_Time_Known_Value (Expression (Expression (N)))
3413 and then Size_Known_At_Compile_Time
3414 (Etype (Expression (Expression (N))))
3415 and then not Is_Record_Type (Current_Scope)
3416 then
3417 -- Here we can do the optimization. For the allocator
3418
3419 -- new x'(y)
3420
3421 -- We insert an object declaration
3422
3423 -- Tnn : aliased x := y;
3424
3425 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3426 -- marked as requiring static allocation.
3427
3428 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
3429 Desig := Subtype_Mark (Expression (N));
3430
3431 -- If context is constrained, use constrained subtype directly,
3432 -- so that the constant is not labelled as having a nominally
3433 -- unconstrained subtype.
3434
3435 if Entity (Desig) = Base_Type (Dtyp) then
3436 Desig := New_Occurrence_Of (Dtyp, Loc);
3437 end if;
3438
3439 Insert_Action (N,
3440 Make_Object_Declaration (Loc,
3441 Defining_Identifier => Temp,
3442 Aliased_Present => True,
3443 Constant_Present => Is_Access_Constant (PtrT),
3444 Object_Definition => Desig,
3445 Expression => Expression (Expression (N))));
3446
3447 Rewrite (N,
3448 Make_Attribute_Reference (Loc,
3449 Prefix => New_Occurrence_Of (Temp, Loc),
3450 Attribute_Name => Name_Unrestricted_Access));
3451
3452 Analyze_And_Resolve (N, PtrT);
3453
3454 -- We set the variable as statically allocated, since we don't want
3455 -- it going on the stack of the current procedure!
3456
3457 Set_Is_Statically_Allocated (Temp);
3458 return;
3459 end if;
3460
3461 -- Same if the allocator is an access discriminant for a local object:
3462 -- instead of an allocator we create a local value and constrain the
3463 -- enclosing object with the corresponding access attribute.
3464
3465 if Is_Static_Coextension (N) then
3466 Rewrite_Coextension (N);
3467 return;
3468 end if;
3469
3470 -- Check for size too large, we do this because the back end misses
3471 -- proper checks here and can generate rubbish allocation calls when
3472 -- we are near the limit. We only do this for the 32-bit address case
3473 -- since that is from a practical point of view where we see a problem.
3474
3475 if System_Address_Size = 32
3476 and then not Storage_Checks_Suppressed (PtrT)
3477 and then not Storage_Checks_Suppressed (Dtyp)
3478 and then not Storage_Checks_Suppressed (Etyp)
3479 then
3480 -- The check we want to generate should look like
3481
3482 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3483 -- raise Storage_Error;
3484 -- end if;
3485
3486 -- where 3.5 gigabytes is a constant large enough to accommodate any
3487 -- reasonable request for. But we can't do it this way because at
3488 -- least at the moment we don't compute this attribute right, and
3489 -- can silently give wrong results when the result gets large. Since
3490 -- this is all about large results, that's bad, so instead we only
3491 -- apply the check for constrained arrays, and manually compute the
3492 -- value of the attribute ???
3493
3494 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3495 Insert_Action (N,
3496 Make_Raise_Storage_Error (Loc,
3497 Condition =>
3498 Make_Op_Gt (Loc,
3499 Left_Opnd => Size_In_Storage_Elements (Etyp),
3500 Right_Opnd =>
3501 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
3502 Reason => SE_Object_Too_Large));
3503 end if;
3504 end if;
3505
3506 -- Handle case of qualified expression (other than optimization above)
3507 -- First apply constraint checks, because the bounds or discriminants
3508 -- in the aggregate might not match the subtype mark in the allocator.
3509
3510 if Nkind (Expression (N)) = N_Qualified_Expression then
3511 Apply_Constraint_Check
3512 (Expression (Expression (N)), Etype (Expression (N)));
3513
3514 Expand_Allocator_Expression (N);
3515 return;
3516 end if;
3517
3518 -- If the allocator is for a type which requires initialization, and
3519 -- there is no initial value (i.e. operand is a subtype indication
3520 -- rather than a qualified expression), then we must generate a call to
3521 -- the initialization routine using an expressions action node:
3522
3523 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3524
3525 -- Here ptr_T is the pointer type for the allocator, and T is the
3526 -- subtype of the allocator. A special case arises if the designated
3527 -- type of the access type is a task or contains tasks. In this case
3528 -- the call to Init (Temp.all ...) is replaced by code that ensures
3529 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3530 -- for details). In addition, if the type T is a task T, then the
3531 -- first argument to Init must be converted to the task record type.
3532
3533 declare
3534 T : constant Entity_Id := Entity (Expression (N));
3535 Args : List_Id;
3536 Decls : List_Id;
3537 Decl : Node_Id;
3538 Discr : Elmt_Id;
3539 Init : Entity_Id;
3540 Init_Arg1 : Node_Id;
3541 Temp_Decl : Node_Id;
3542 Temp_Type : Entity_Id;
3543
3544 begin
3545 if No_Initialization (N) then
3546
3547 -- Even though this might be a simple allocation, create a custom
3548 -- Allocate if the context requires it. Since .NET/JVM compilers
3549 -- do not support pools, this step is skipped.
3550
3551 if VM_Target = No_VM
3552 and then Present (Associated_Collection (PtrT))
3553 then
3554 Build_Allocate_Deallocate_Proc
3555 (N => Parent (N),
3556 Is_Allocate => True);
3557 end if;
3558
3559 -- Case of no initialization procedure present
3560
3561 elsif not Has_Non_Null_Base_Init_Proc (T) then
3562
3563 -- Case of simple initialization required
3564
3565 if Needs_Simple_Initialization (T) then
3566 Check_Restriction (No_Default_Initialization, N);
3567 Rewrite (Expression (N),
3568 Make_Qualified_Expression (Loc,
3569 Subtype_Mark => New_Occurrence_Of (T, Loc),
3570 Expression => Get_Simple_Init_Val (T, N)));
3571
3572 Analyze_And_Resolve (Expression (Expression (N)), T);
3573 Analyze_And_Resolve (Expression (N), T);
3574 Set_Paren_Count (Expression (Expression (N)), 1);
3575 Expand_N_Allocator (N);
3576
3577 -- No initialization required
3578
3579 else
3580 null;
3581 end if;
3582
3583 -- Case of initialization procedure present, must be called
3584
3585 else
3586 Check_Restriction (No_Default_Initialization, N);
3587
3588 if not Restriction_Active (No_Default_Initialization) then
3589 Init := Base_Init_Proc (T);
3590 Nod := N;
3591 Temp := Make_Temporary (Loc, 'P');
3592
3593 -- Construct argument list for the initialization routine call
3594
3595 Init_Arg1 :=
3596 Make_Explicit_Dereference (Loc,
3597 Prefix =>
3598 New_Reference_To (Temp, Loc));
3599
3600 Set_Assignment_OK (Init_Arg1);
3601 Temp_Type := PtrT;
3602
3603 -- The initialization procedure expects a specific type. if the
3604 -- context is access to class wide, indicate that the object
3605 -- being allocated has the right specific type.
3606
3607 if Is_Class_Wide_Type (Dtyp) then
3608 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
3609 end if;
3610
3611 -- If designated type is a concurrent type or if it is private
3612 -- type whose definition is a concurrent type, the first
3613 -- argument in the Init routine has to be unchecked conversion
3614 -- to the corresponding record type. If the designated type is
3615 -- a derived type, also convert the argument to its root type.
3616
3617 if Is_Concurrent_Type (T) then
3618 Init_Arg1 :=
3619 Unchecked_Convert_To (
3620 Corresponding_Record_Type (T), Init_Arg1);
3621
3622 elsif Is_Private_Type (T)
3623 and then Present (Full_View (T))
3624 and then Is_Concurrent_Type (Full_View (T))
3625 then
3626 Init_Arg1 :=
3627 Unchecked_Convert_To
3628 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
3629
3630 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3631 declare
3632 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3633
3634 begin
3635 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
3636 Set_Etype (Init_Arg1, Ftyp);
3637 end;
3638 end if;
3639
3640 Args := New_List (Init_Arg1);
3641
3642 -- For the task case, pass the Master_Id of the access type as
3643 -- the value of the _Master parameter, and _Chain as the value
3644 -- of the _Chain parameter (_Chain will be defined as part of
3645 -- the generated code for the allocator).
3646
3647 -- In Ada 2005, the context may be a function that returns an
3648 -- anonymous access type. In that case the Master_Id has been
3649 -- created when expanding the function declaration.
3650
3651 if Has_Task (T) then
3652 if No (Master_Id (Base_Type (PtrT))) then
3653
3654 -- The designated type was an incomplete type, and the
3655 -- access type did not get expanded. Salvage it now.
3656
3657 if not Restriction_Active (No_Task_Hierarchy) then
3658 pragma Assert (Present (Parent (Base_Type (PtrT))));
3659 Expand_N_Full_Type_Declaration
3660 (Parent (Base_Type (PtrT)));
3661 end if;
3662 end if;
3663
3664 -- If the context of the allocator is a declaration or an
3665 -- assignment, we can generate a meaningful image for it,
3666 -- even though subsequent assignments might remove the
3667 -- connection between task and entity. We build this image
3668 -- when the left-hand side is a simple variable, a simple
3669 -- indexed assignment or a simple selected component.
3670
3671 if Nkind (Parent (N)) = N_Assignment_Statement then
3672 declare
3673 Nam : constant Node_Id := Name (Parent (N));
3674
3675 begin
3676 if Is_Entity_Name (Nam) then
3677 Decls :=
3678 Build_Task_Image_Decls
3679 (Loc,
3680 New_Occurrence_Of
3681 (Entity (Nam), Sloc (Nam)), T);
3682
3683 elsif Nkind_In (Nam, N_Indexed_Component,
3684 N_Selected_Component)
3685 and then Is_Entity_Name (Prefix (Nam))
3686 then
3687 Decls :=
3688 Build_Task_Image_Decls
3689 (Loc, Nam, Etype (Prefix (Nam)));
3690 else
3691 Decls := Build_Task_Image_Decls (Loc, T, T);
3692 end if;
3693 end;
3694
3695 elsif Nkind (Parent (N)) = N_Object_Declaration then
3696 Decls :=
3697 Build_Task_Image_Decls
3698 (Loc, Defining_Identifier (Parent (N)), T);
3699
3700 else
3701 Decls := Build_Task_Image_Decls (Loc, T, T);
3702 end if;
3703
3704 if Restriction_Active (No_Task_Hierarchy) then
3705 Append_To (Args,
3706 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
3707 else
3708 Append_To (Args,
3709 New_Reference_To
3710 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3711 end if;
3712
3713 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3714
3715 Decl := Last (Decls);
3716 Append_To (Args,
3717 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3718
3719 -- Has_Task is false, Decls not used
3720
3721 else
3722 Decls := No_List;
3723 end if;
3724
3725 -- Add discriminants if discriminated type
3726
3727 declare
3728 Dis : Boolean := False;
3729 Typ : Entity_Id;
3730
3731 begin
3732 if Has_Discriminants (T) then
3733 Dis := True;
3734 Typ := T;
3735
3736 elsif Is_Private_Type (T)
3737 and then Present (Full_View (T))
3738 and then Has_Discriminants (Full_View (T))
3739 then
3740 Dis := True;
3741 Typ := Full_View (T);
3742 end if;
3743
3744 if Dis then
3745
3746 -- If the allocated object will be constrained by the
3747 -- default values for discriminants, then build a subtype
3748 -- with those defaults, and change the allocated subtype
3749 -- to that. Note that this happens in fewer cases in Ada
3750 -- 2005 (AI-363).
3751
3752 if not Is_Constrained (Typ)
3753 and then Present (Discriminant_Default_Value
3754 (First_Discriminant (Typ)))
3755 and then (Ada_Version < Ada_2005
3756 or else
3757 not Has_Constrained_Partial_View (Typ))
3758 then
3759 Typ := Build_Default_Subtype (Typ, N);
3760 Set_Expression (N, New_Reference_To (Typ, Loc));
3761 end if;
3762
3763 Discr := First_Elmt (Discriminant_Constraint (Typ));
3764 while Present (Discr) loop
3765 Nod := Node (Discr);
3766 Append (New_Copy_Tree (Node (Discr)), Args);
3767
3768 -- AI-416: when the discriminant constraint is an
3769 -- anonymous access type make sure an accessibility
3770 -- check is inserted if necessary (3.10.2(22.q/2))
3771
3772 if Ada_Version >= Ada_2005
3773 and then
3774 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3775 then
3776 Apply_Accessibility_Check
3777 (Nod, Typ, Insert_Node => Nod);
3778 end if;
3779
3780 Next_Elmt (Discr);
3781 end loop;
3782 end if;
3783 end;
3784
3785 -- We set the allocator as analyzed so that when we analyze the
3786 -- expression actions node, we do not get an unwanted recursive
3787 -- expansion of the allocator expression.
3788
3789 Set_Analyzed (N, True);
3790 Nod := Relocate_Node (N);
3791
3792 -- Here is the transformation:
3793 -- input: new T
3794 -- output: Temp : constant ptr_T := new T;
3795 -- Init (Temp.all, ...);
3796 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3797 -- <CTRL> Initialize (Finalizable (Temp.all));
3798
3799 -- Here ptr_T is the pointer type for the allocator, and is the
3800 -- subtype of the allocator.
3801
3802 Temp_Decl :=
3803 Make_Object_Declaration (Loc,
3804 Defining_Identifier => Temp,
3805 Constant_Present => True,
3806 Object_Definition => New_Reference_To (Temp_Type, Loc),
3807 Expression => Nod);
3808
3809 Set_Assignment_OK (Temp_Decl);
3810 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3811
3812 Complete_Controlled_Allocation (Temp_Decl);
3813
3814 -- If the designated type is a task type or contains tasks,
3815 -- create block to activate created tasks, and insert
3816 -- declaration for Task_Image variable ahead of call.
3817
3818 if Has_Task (T) then
3819 declare
3820 L : constant List_Id := New_List;
3821 Blk : Node_Id;
3822 begin
3823 Build_Task_Allocate_Block (L, Nod, Args);
3824 Blk := Last (L);
3825 Insert_List_Before (First (Declarations (Blk)), Decls);
3826 Insert_Actions (N, L);
3827 end;
3828
3829 else
3830 Insert_Action (N,
3831 Make_Procedure_Call_Statement (Loc,
3832 Name => New_Reference_To (Init, Loc),
3833 Parameter_Associations => Args));
3834 end if;
3835
3836 if Needs_Finalization (T) then
3837
3838 -- Generate:
3839 -- [Deep_]Initialize (Init_Arg1);
3840
3841 Insert_Action (N,
3842 Make_Init_Call
3843 (Obj_Ref => New_Copy_Tree (Init_Arg1),
3844 Typ => T));
3845
3846 if Present (Associated_Collection (PtrT)) then
3847
3848 -- Special processing for .NET/JVM, the allocated object
3849 -- is attached to the finalization collection. Generate:
3850
3851 -- Attach (<PtrT>FC, Root_Controlled_Ptr (Init_Arg1));
3852
3853 -- Types derived from [Limited_]Controlled are the only
3854 -- ones considered since they have fields Prev and Next.
3855
3856 if VM_Target /= No_VM then
3857 if Is_Controlled (T) then
3858 Insert_Action (N,
3859 Make_Attach_Call
3860 (Obj_Ref => New_Copy_Tree (Init_Arg1),
3861 Ptr_Typ => PtrT));
3862 end if;
3863
3864 -- Default case, generate:
3865
3866 -- Set_Finalize_Address_Ptr
3867 -- (Pool, <Finalize_Address>'Unrestricted_Access)
3868
3869 -- Do not generate the above for CodePeer compilations
3870 -- because Finalize_Address is never built.
3871
3872 elsif not CodePeer_Mode then
3873 Insert_Action (N,
3874 Make_Set_Finalize_Address_Ptr_Call
3875 (Loc => Loc,
3876 Typ => T,
3877 Ptr_Typ => PtrT));
3878 end if;
3879 end if;
3880 end if;
3881
3882 Rewrite (N, New_Reference_To (Temp, Loc));
3883 Analyze_And_Resolve (N, PtrT);
3884 end if;
3885 end if;
3886 end;
3887
3888 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3889 -- object that has been rewritten as a reference, we displace "this"
3890 -- to reference properly its secondary dispatch table.
3891
3892 if Nkind (N) = N_Identifier
3893 and then Is_Interface (Dtyp)
3894 then
3895 Displace_Allocator_Pointer (N);
3896 end if;
3897
3898 exception
3899 when RE_Not_Available =>
3900 return;
3901 end Expand_N_Allocator;
3902
3903 -----------------------
3904 -- Expand_N_And_Then --
3905 -----------------------
3906
3907 procedure Expand_N_And_Then (N : Node_Id)
3908 renames Expand_Short_Circuit_Operator;
3909
3910 ------------------------------
3911 -- Expand_N_Case_Expression --
3912 ------------------------------
3913
3914 procedure Expand_N_Case_Expression (N : Node_Id) is
3915 Loc : constant Source_Ptr := Sloc (N);
3916 Typ : constant Entity_Id := Etype (N);
3917 Cstmt : Node_Id;
3918 Tnn : Entity_Id;
3919 Pnn : Entity_Id;
3920 Actions : List_Id;
3921 Ttyp : Entity_Id;
3922 Alt : Node_Id;
3923 Fexp : Node_Id;
3924
3925 begin
3926 -- We expand
3927
3928 -- case X is when A => AX, when B => BX ...
3929
3930 -- to
3931
3932 -- do
3933 -- Tnn : typ;
3934 -- case X is
3935 -- when A =>
3936 -- Tnn := AX;
3937 -- when B =>
3938 -- Tnn := BX;
3939 -- ...
3940 -- end case;
3941 -- in Tnn end;
3942
3943 -- However, this expansion is wrong for limited types, and also
3944 -- wrong for unconstrained types (since the bounds may not be the
3945 -- same in all branches). Furthermore it involves an extra copy
3946 -- for large objects. So we take care of this by using the following
3947 -- modified expansion for non-scalar types:
3948
3949 -- do
3950 -- type Pnn is access all typ;
3951 -- Tnn : Pnn;
3952 -- case X is
3953 -- when A =>
3954 -- T := AX'Unrestricted_Access;
3955 -- when B =>
3956 -- T := BX'Unrestricted_Access;
3957 -- ...
3958 -- end case;
3959 -- in Tnn.all end;
3960
3961 Cstmt :=
3962 Make_Case_Statement (Loc,
3963 Expression => Expression (N),
3964 Alternatives => New_List);
3965
3966 Actions := New_List;
3967
3968 -- Scalar case
3969
3970 if Is_Scalar_Type (Typ) then
3971 Ttyp := Typ;
3972
3973 else
3974 Pnn := Make_Temporary (Loc, 'P');
3975 Append_To (Actions,
3976 Make_Full_Type_Declaration (Loc,
3977 Defining_Identifier => Pnn,
3978 Type_Definition =>
3979 Make_Access_To_Object_Definition (Loc,
3980 All_Present => True,
3981 Subtype_Indication =>
3982 New_Reference_To (Typ, Loc))));
3983 Ttyp := Pnn;
3984 end if;
3985
3986 Tnn := Make_Temporary (Loc, 'T');
3987 Append_To (Actions,
3988 Make_Object_Declaration (Loc,
3989 Defining_Identifier => Tnn,
3990 Object_Definition => New_Occurrence_Of (Ttyp, Loc)));
3991
3992 -- Now process the alternatives
3993
3994 Alt := First (Alternatives (N));
3995 while Present (Alt) loop
3996 declare
3997 Aexp : Node_Id := Expression (Alt);
3998 Aloc : constant Source_Ptr := Sloc (Aexp);
3999
4000 begin
4001 -- Propagate declarations inserted in the node by Insert_Actions
4002 -- (for example, temporaries generated to remove side effects).
4003
4004 Append_List_To (Actions, Sinfo.Actions (Alt));
4005
4006 if not Is_Scalar_Type (Typ) then
4007 Aexp :=
4008 Make_Attribute_Reference (Aloc,
4009 Prefix => Relocate_Node (Aexp),
4010 Attribute_Name => Name_Unrestricted_Access);
4011 end if;
4012
4013 Append_To
4014 (Alternatives (Cstmt),
4015 Make_Case_Statement_Alternative (Sloc (Alt),
4016 Discrete_Choices => Discrete_Choices (Alt),
4017 Statements => New_List (
4018 Make_Assignment_Statement (Aloc,
4019 Name => New_Occurrence_Of (Tnn, Loc),
4020 Expression => Aexp))));
4021 end;
4022
4023 Next (Alt);
4024 end loop;
4025
4026 Append_To (Actions, Cstmt);
4027
4028 -- Construct and return final expression with actions
4029
4030 if Is_Scalar_Type (Typ) then
4031 Fexp := New_Occurrence_Of (Tnn, Loc);
4032 else
4033 Fexp :=
4034 Make_Explicit_Dereference (Loc,
4035 Prefix => New_Occurrence_Of (Tnn, Loc));
4036 end if;
4037
4038 Rewrite (N,
4039 Make_Expression_With_Actions (Loc,
4040 Expression => Fexp,
4041 Actions => Actions));
4042
4043 Analyze_And_Resolve (N, Typ);
4044 end Expand_N_Case_Expression;
4045
4046 -------------------------------------
4047 -- Expand_N_Conditional_Expression --
4048 -------------------------------------
4049
4050 -- Deal with limited types and expression actions
4051
4052 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4053 Loc : constant Source_Ptr := Sloc (N);
4054 Cond : constant Node_Id := First (Expressions (N));
4055 Thenx : constant Node_Id := Next (Cond);
4056 Elsex : constant Node_Id := Next (Thenx);
4057 Typ : constant Entity_Id := Etype (N);
4058
4059 Cnn : Entity_Id;
4060 Decl : Node_Id;
4061 New_If : Node_Id;
4062 New_N : Node_Id;
4063 P_Decl : Node_Id;
4064 Expr : Node_Id;
4065 Actions : List_Id;
4066
4067 begin
4068 -- Fold at compile time if condition known. We have already folded
4069 -- static conditional expressions, but it is possible to fold any
4070 -- case in which the condition is known at compile time, even though
4071 -- the result is non-static.
4072
4073 -- Note that we don't do the fold of such cases in Sem_Elab because
4074 -- it can cause infinite loops with the expander adding a conditional
4075 -- expression, and Sem_Elab circuitry removing it repeatedly.
4076
4077 if Compile_Time_Known_Value (Cond) then
4078 if Is_True (Expr_Value (Cond)) then
4079 Expr := Thenx;
4080 Actions := Then_Actions (N);
4081 else
4082 Expr := Elsex;
4083 Actions := Else_Actions (N);
4084 end if;
4085
4086 Remove (Expr);
4087
4088 if Present (Actions) then
4089
4090 -- If we are not allowed to use Expression_With_Actions, just
4091 -- skip the optimization, it is not critical for correctness.
4092
4093 if not Use_Expression_With_Actions then
4094 goto Skip_Optimization;
4095 end if;
4096
4097 Rewrite (N,
4098 Make_Expression_With_Actions (Loc,
4099 Expression => Relocate_Node (Expr),
4100 Actions => Actions));
4101 Analyze_And_Resolve (N, Typ);
4102
4103 else
4104 Rewrite (N, Relocate_Node (Expr));
4105 end if;
4106
4107 -- Note that the result is never static (legitimate cases of static
4108 -- conditional expressions were folded in Sem_Eval).
4109
4110 Set_Is_Static_Expression (N, False);
4111 return;
4112 end if;
4113
4114 <<Skip_Optimization>>
4115
4116 -- If the type is limited or unconstrained, we expand as follows to
4117 -- avoid any possibility of improper copies.
4118
4119 -- Note: it may be possible to avoid this special processing if the
4120 -- back end uses its own mechanisms for handling by-reference types ???
4121
4122 -- type Ptr is access all Typ;
4123 -- Cnn : Ptr;
4124 -- if cond then
4125 -- <<then actions>>
4126 -- Cnn := then-expr'Unrestricted_Access;
4127 -- else
4128 -- <<else actions>>
4129 -- Cnn := else-expr'Unrestricted_Access;
4130 -- end if;
4131
4132 -- and replace the conditional expression by a reference to Cnn.all.
4133
4134 -- This special case can be skipped if the back end handles limited
4135 -- types properly and ensures that no incorrect copies are made.
4136
4137 if Is_By_Reference_Type (Typ)
4138 and then not Back_End_Handles_Limited_Types
4139 then
4140 Cnn := Make_Temporary (Loc, 'C', N);
4141
4142 P_Decl :=
4143 Make_Full_Type_Declaration (Loc,
4144 Defining_Identifier =>
4145 Make_Temporary (Loc, 'A'),
4146 Type_Definition =>
4147 Make_Access_To_Object_Definition (Loc,
4148 All_Present => True,
4149 Subtype_Indication => New_Reference_To (Typ, Loc)));
4150
4151 Insert_Action (N, P_Decl);
4152
4153 Decl :=
4154 Make_Object_Declaration (Loc,
4155 Defining_Identifier => Cnn,
4156 Object_Definition =>
4157 New_Occurrence_Of (Defining_Identifier (P_Decl), Loc));
4158
4159 New_If :=
4160 Make_Implicit_If_Statement (N,
4161 Condition => Relocate_Node (Cond),
4162
4163 Then_Statements => New_List (
4164 Make_Assignment_Statement (Sloc (Thenx),
4165 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4166 Expression =>
4167 Make_Attribute_Reference (Loc,
4168 Attribute_Name => Name_Unrestricted_Access,
4169 Prefix => Relocate_Node (Thenx)))),
4170
4171 Else_Statements => New_List (
4172 Make_Assignment_Statement (Sloc (Elsex),
4173 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4174 Expression =>
4175 Make_Attribute_Reference (Loc,
4176 Attribute_Name => Name_Unrestricted_Access,
4177 Prefix => Relocate_Node (Elsex)))));
4178
4179 New_N :=
4180 Make_Explicit_Dereference (Loc,
4181 Prefix => New_Occurrence_Of (Cnn, Loc));
4182
4183 -- For other types, we only need to expand if there are other actions
4184 -- associated with either branch.
4185
4186 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
4187
4188 -- We have two approaches to handling this. If we are allowed to use
4189 -- N_Expression_With_Actions, then we can just wrap the actions into
4190 -- the appropriate expression.
4191
4192 if Use_Expression_With_Actions then
4193 if Present (Then_Actions (N)) then
4194 Rewrite (Thenx,
4195 Make_Expression_With_Actions (Sloc (Thenx),
4196 Actions => Then_Actions (N),
4197 Expression => Relocate_Node (Thenx)));
4198 Set_Then_Actions (N, No_List);
4199 Analyze_And_Resolve (Thenx, Typ);
4200 end if;
4201
4202 if Present (Else_Actions (N)) then
4203 Rewrite (Elsex,
4204 Make_Expression_With_Actions (Sloc (Elsex),
4205 Actions => Else_Actions (N),
4206 Expression => Relocate_Node (Elsex)));
4207 Set_Else_Actions (N, No_List);
4208 Analyze_And_Resolve (Elsex, Typ);
4209 end if;
4210
4211 return;
4212
4213 -- if we can't use N_Expression_With_Actions nodes, then we insert
4214 -- the following sequence of actions (using Insert_Actions):
4215
4216 -- Cnn : typ;
4217 -- if cond then
4218 -- <<then actions>>
4219 -- Cnn := then-expr;
4220 -- else
4221 -- <<else actions>>
4222 -- Cnn := else-expr
4223 -- end if;
4224
4225 -- and replace the conditional expression by a reference to Cnn
4226
4227 else
4228 Cnn := Make_Temporary (Loc, 'C', N);
4229
4230 Decl :=
4231 Make_Object_Declaration (Loc,
4232 Defining_Identifier => Cnn,
4233 Object_Definition => New_Occurrence_Of (Typ, Loc));
4234
4235 New_If :=
4236 Make_Implicit_If_Statement (N,
4237 Condition => Relocate_Node (Cond),
4238
4239 Then_Statements => New_List (
4240 Make_Assignment_Statement (Sloc (Thenx),
4241 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4242 Expression => Relocate_Node (Thenx))),
4243
4244 Else_Statements => New_List (
4245 Make_Assignment_Statement (Sloc (Elsex),
4246 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4247 Expression => Relocate_Node (Elsex))));
4248
4249 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
4250 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4251
4252 New_N := New_Occurrence_Of (Cnn, Loc);
4253 end if;
4254
4255 -- If no actions then no expansion needed, gigi will handle it using
4256 -- the same approach as a C conditional expression.
4257
4258 else
4259 return;
4260 end if;
4261
4262 -- Fall through here for either the limited expansion, or the case of
4263 -- inserting actions for non-limited types. In both these cases, we must
4264 -- move the SLOC of the parent If statement to the newly created one and
4265 -- change it to the SLOC of the expression which, after expansion, will
4266 -- correspond to what is being evaluated.
4267
4268 if Present (Parent (N))
4269 and then Nkind (Parent (N)) = N_If_Statement
4270 then
4271 Set_Sloc (New_If, Sloc (Parent (N)));
4272 Set_Sloc (Parent (N), Loc);
4273 end if;
4274
4275 -- Make sure Then_Actions and Else_Actions are appropriately moved
4276 -- to the new if statement.
4277
4278 if Present (Then_Actions (N)) then
4279 Insert_List_Before
4280 (First (Then_Statements (New_If)), Then_Actions (N));
4281 end if;
4282
4283 if Present (Else_Actions (N)) then
4284 Insert_List_Before
4285 (First (Else_Statements (New_If)), Else_Actions (N));
4286 end if;
4287
4288 Insert_Action (N, Decl);
4289 Insert_Action (N, New_If);
4290 Rewrite (N, New_N);
4291 Analyze_And_Resolve (N, Typ);
4292 end Expand_N_Conditional_Expression;
4293
4294 -----------------------------------
4295 -- Expand_N_Explicit_Dereference --
4296 -----------------------------------
4297
4298 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4299 begin
4300 -- Insert explicit dereference call for the checked storage pool case
4301
4302 Insert_Dereference_Action (Prefix (N));
4303 end Expand_N_Explicit_Dereference;
4304
4305 --------------------------------------
4306 -- Expand_N_Expression_With_Actions --
4307 --------------------------------------
4308
4309 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
4310
4311 procedure Process_Transient_Object (Decl : Node_Id);
4312 -- Given the declaration of a controlled transient declared inside the
4313 -- Actions list of an Expression_With_Actions, generate all necessary
4314 -- types and hooks in order to properly finalize the transient. This
4315 -- mechanism works in conjunction with Build_Finalizer.
4316
4317 ------------------------------
4318 -- Process_Transient_Object --
4319 ------------------------------
4320
4321 procedure Process_Transient_Object (Decl : Node_Id) is
4322 Ins_Nod : constant Node_Id := Parent (N);
4323 -- To avoid the insertion of generated code in the list of Actions,
4324 -- Insert_Action must look at the parent field of the EWA.
4325
4326 Loc : constant Source_Ptr := Sloc (Decl);
4327 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4328 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4329 Desig_Typ : Entity_Id;
4330 Expr : Node_Id;
4331 Ptr_Decl : Node_Id;
4332 Ptr_Id : Entity_Id;
4333 Temp_Decl : Node_Id;
4334 Temp_Id : Node_Id;
4335
4336 begin
4337 -- Step 1: Create the access type which provides a reference to
4338 -- the transient object.
4339
4340 if Is_Access_Type (Obj_Typ) then
4341 Desig_Typ := Directly_Designated_Type (Obj_Typ);
4342 else
4343 Desig_Typ := Obj_Typ;
4344 end if;
4345
4346 -- Generate:
4347 -- Ann : access [all] <Desig_Typ>;
4348
4349 Ptr_Id := Make_Temporary (Loc, 'A');
4350
4351 Ptr_Decl :=
4352 Make_Full_Type_Declaration (Loc,
4353 Defining_Identifier => Ptr_Id,
4354 Type_Definition =>
4355 Make_Access_To_Object_Definition (Loc,
4356 All_Present =>
4357 Ekind (Obj_Typ) = E_General_Access_Type,
4358 Subtype_Indication =>
4359 New_Reference_To (Desig_Typ, Loc)));
4360
4361 Insert_Action (Ins_Nod, Ptr_Decl);
4362 Analyze (Ptr_Decl);
4363
4364 -- Step 2: Create a temporary which acts as a hook to the transient
4365 -- object. Generate:
4366
4367 -- Temp : Ptr_Id := null;
4368
4369 Temp_Id := Make_Temporary (Loc, 'T');
4370
4371 Temp_Decl :=
4372 Make_Object_Declaration (Loc,
4373 Defining_Identifier => Temp_Id,
4374 Object_Definition => New_Reference_To (Ptr_Id, Loc));
4375
4376 Insert_Action (Ins_Nod, Temp_Decl);
4377 Analyze (Temp_Decl);
4378
4379 -- Mark this temporary as created for the purposes of "exporting" the
4380 -- transient declaration out of the Actions list. This signals the
4381 -- machinery in Build_Finalizer to recognize this special case.
4382
4383 Set_Return_Flag_Or_Transient_Decl (Temp_Id, Decl);
4384
4385 -- Step 3: "Hook" the transient object to the temporary
4386
4387 if Is_Access_Type (Obj_Typ) then
4388 Expr := Convert_To (Ptr_Id, New_Reference_To (Obj_Id, Loc));
4389 else
4390 Expr :=
4391 Make_Attribute_Reference (Loc,
4392 Prefix =>
4393 New_Reference_To (Obj_Id, Loc),
4394 Attribute_Name => Name_Unrestricted_Access);
4395 end if;
4396
4397 -- Generate:
4398 -- Temp := Ptr_Id (Obj_Id);
4399 -- <or>
4400 -- Temp := Obj_Id'Unrestricted_Access;
4401
4402 Insert_After_And_Analyze (Decl,
4403 Make_Assignment_Statement (Loc,
4404 Name => New_Reference_To (Temp_Id, Loc),
4405 Expression => Expr));
4406 end Process_Transient_Object;
4407
4408 Decl : Node_Id;
4409
4410 -- Start of processing for Expand_N_Expression_With_Actions
4411
4412 begin
4413 Decl := First (Actions (N));
4414 while Present (Decl) loop
4415 if Nkind (Decl) = N_Object_Declaration
4416 and then Is_Finalizable_Transient (Decl, N)
4417 then
4418 Process_Transient_Object (Decl);
4419 end if;
4420
4421 Next (Decl);
4422 end loop;
4423 end Expand_N_Expression_With_Actions;
4424
4425 -----------------
4426 -- Expand_N_In --
4427 -----------------
4428
4429 procedure Expand_N_In (N : Node_Id) is
4430 Loc : constant Source_Ptr := Sloc (N);
4431 Restyp : constant Entity_Id := Etype (N);
4432 Lop : constant Node_Id := Left_Opnd (N);
4433 Rop : constant Node_Id := Right_Opnd (N);
4434 Static : constant Boolean := Is_OK_Static_Expression (N);
4435
4436 Ltyp : Entity_Id;
4437 Rtyp : Entity_Id;
4438
4439 procedure Expand_Set_Membership;
4440 -- For each choice we create a simple equality or membership test.
4441 -- The whole membership is rewritten connecting these with OR ELSE.
4442
4443 ---------------------------
4444 -- Expand_Set_Membership --
4445 ---------------------------
4446
4447 procedure Expand_Set_Membership is
4448 Alt : Node_Id;
4449 Res : Node_Id;
4450
4451 function Make_Cond (Alt : Node_Id) return Node_Id;
4452 -- If the alternative is a subtype mark, create a simple membership
4453 -- test. Otherwise create an equality test for it.
4454
4455 ---------------
4456 -- Make_Cond --
4457 ---------------
4458
4459 function Make_Cond (Alt : Node_Id) return Node_Id is
4460 Cond : Node_Id;
4461 L : constant Node_Id := New_Copy (Lop);
4462 R : constant Node_Id := Relocate_Node (Alt);
4463
4464 begin
4465 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
4466 or else Nkind (Alt) = N_Range
4467 then
4468 Cond :=
4469 Make_In (Sloc (Alt),
4470 Left_Opnd => L,
4471 Right_Opnd => R);
4472 else
4473 Cond :=
4474 Make_Op_Eq (Sloc (Alt),
4475 Left_Opnd => L,
4476 Right_Opnd => R);
4477 end if;
4478
4479 return Cond;
4480 end Make_Cond;
4481
4482 -- Start of processing for Expand_Set_Membership
4483
4484 begin
4485 Alt := Last (Alternatives (N));
4486 Res := Make_Cond (Alt);
4487
4488 Prev (Alt);
4489 while Present (Alt) loop
4490 Res :=
4491 Make_Or_Else (Sloc (Alt),
4492 Left_Opnd => Make_Cond (Alt),
4493 Right_Opnd => Res);
4494 Prev (Alt);
4495 end loop;
4496
4497 Rewrite (N, Res);
4498 Analyze_And_Resolve (N, Standard_Boolean);
4499 end Expand_Set_Membership;
4500
4501 procedure Substitute_Valid_Check;
4502 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4503 -- test for the left operand being in range of its subtype.
4504
4505 ----------------------------
4506 -- Substitute_Valid_Check --
4507 ----------------------------
4508
4509 procedure Substitute_Valid_Check is
4510 begin
4511 Rewrite (N,
4512 Make_Attribute_Reference (Loc,
4513 Prefix => Relocate_Node (Lop),
4514 Attribute_Name => Name_Valid));
4515
4516 Analyze_And_Resolve (N, Restyp);
4517
4518 Error_Msg_N ("?explicit membership test may be optimized away", N);
4519 Error_Msg_N -- CODEFIX
4520 ("\?use ''Valid attribute instead", N);
4521 return;
4522 end Substitute_Valid_Check;
4523
4524 -- Start of processing for Expand_N_In
4525
4526 begin
4527 -- If set membership case, expand with separate procedure
4528
4529 if Present (Alternatives (N)) then
4530 Remove_Side_Effects (Lop);
4531 Expand_Set_Membership;
4532 return;
4533 end if;
4534
4535 -- Not set membership, proceed with expansion
4536
4537 Ltyp := Etype (Left_Opnd (N));
4538 Rtyp := Etype (Right_Opnd (N));
4539
4540 -- Check case of explicit test for an expression in range of its
4541 -- subtype. This is suspicious usage and we replace it with a 'Valid
4542 -- test and give a warning. For floating point types however, this is a
4543 -- standard way to check for finite numbers, and using 'Valid would
4544 -- typically be a pessimization. Also skip this test for predicated
4545 -- types, since it is perfectly reasonable to check if a value meets
4546 -- its predicate.
4547
4548 if Is_Scalar_Type (Ltyp)
4549 and then not Is_Floating_Point_Type (Ltyp)
4550 and then Nkind (Rop) in N_Has_Entity
4551 and then Ltyp = Entity (Rop)
4552 and then Comes_From_Source (N)
4553 and then VM_Target = No_VM
4554 and then not (Is_Discrete_Type (Ltyp)
4555 and then Present (Predicate_Function (Ltyp)))
4556 then
4557 Substitute_Valid_Check;
4558 return;
4559 end if;
4560
4561 -- Do validity check on operands
4562
4563 if Validity_Checks_On and Validity_Check_Operands then
4564 Ensure_Valid (Left_Opnd (N));
4565 Validity_Check_Range (Right_Opnd (N));
4566 end if;
4567
4568 -- Case of explicit range
4569
4570 if Nkind (Rop) = N_Range then
4571 declare
4572 Lo : constant Node_Id := Low_Bound (Rop);
4573 Hi : constant Node_Id := High_Bound (Rop);
4574
4575 Lo_Orig : constant Node_Id := Original_Node (Lo);
4576 Hi_Orig : constant Node_Id := Original_Node (Hi);
4577
4578 Lcheck : Compare_Result;
4579 Ucheck : Compare_Result;
4580
4581 Warn1 : constant Boolean :=
4582 Constant_Condition_Warnings
4583 and then Comes_From_Source (N)
4584 and then not In_Instance;
4585 -- This must be true for any of the optimization warnings, we
4586 -- clearly want to give them only for source with the flag on. We
4587 -- also skip these warnings in an instance since it may be the
4588 -- case that different instantiations have different ranges.
4589
4590 Warn2 : constant Boolean :=
4591 Warn1
4592 and then Nkind (Original_Node (Rop)) = N_Range
4593 and then Is_Integer_Type (Etype (Lo));
4594 -- For the case where only one bound warning is elided, we also
4595 -- insist on an explicit range and an integer type. The reason is
4596 -- that the use of enumeration ranges including an end point is
4597 -- common, as is the use of a subtype name, one of whose bounds is
4598 -- the same as the type of the expression.
4599
4600 begin
4601 -- If test is explicit x'First .. x'Last, replace by valid check
4602
4603 -- Could use some individual comments for this complex test ???
4604
4605 if Is_Scalar_Type (Ltyp)
4606 and then Nkind (Lo_Orig) = N_Attribute_Reference
4607 and then Attribute_Name (Lo_Orig) = Name_First
4608 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4609 and then Entity (Prefix (Lo_Orig)) = Ltyp
4610 and then Nkind (Hi_Orig) = N_Attribute_Reference
4611 and then Attribute_Name (Hi_Orig) = Name_Last
4612 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4613 and then Entity (Prefix (Hi_Orig)) = Ltyp
4614 and then Comes_From_Source (N)
4615 and then VM_Target = No_VM
4616 then
4617 Substitute_Valid_Check;
4618 goto Leave;
4619 end if;
4620
4621 -- If bounds of type are known at compile time, and the end points
4622 -- are known at compile time and identical, this is another case
4623 -- for substituting a valid test. We only do this for discrete
4624 -- types, since it won't arise in practice for float types.
4625
4626 if Comes_From_Source (N)
4627 and then Is_Discrete_Type (Ltyp)
4628 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4629 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4630 and then Compile_Time_Known_Value (Lo)
4631 and then Compile_Time_Known_Value (Hi)
4632 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4633 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4634
4635 -- Kill warnings in instances, since they may be cases where we
4636 -- have a test in the generic that makes sense with some types
4637 -- and not with other types.
4638
4639 and then not In_Instance
4640 then
4641 Substitute_Valid_Check;
4642 goto Leave;
4643 end if;
4644
4645 -- If we have an explicit range, do a bit of optimization based on
4646 -- range analysis (we may be able to kill one or both checks).
4647
4648 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4649 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4650
4651 -- If either check is known to fail, replace result by False since
4652 -- the other check does not matter. Preserve the static flag for
4653 -- legality checks, because we are constant-folding beyond RM 4.9.
4654
4655 if Lcheck = LT or else Ucheck = GT then
4656 if Warn1 then
4657 Error_Msg_N ("?range test optimized away", N);
4658 Error_Msg_N ("\?value is known to be out of range", N);
4659 end if;
4660
4661 Rewrite (N, New_Reference_To (Standard_False, Loc));
4662 Analyze_And_Resolve (N, Restyp);
4663 Set_Is_Static_Expression (N, Static);
4664 goto Leave;
4665
4666 -- If both checks are known to succeed, replace result by True,
4667 -- since we know we are in range.
4668
4669 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4670 if Warn1 then
4671 Error_Msg_N ("?range test optimized away", N);
4672 Error_Msg_N ("\?value is known to be in range", N);
4673 end if;
4674
4675 Rewrite (N, New_Reference_To (Standard_True, Loc));
4676 Analyze_And_Resolve (N, Restyp);
4677 Set_Is_Static_Expression (N, Static);
4678 goto Leave;
4679
4680 -- If lower bound check succeeds and upper bound check is not
4681 -- known to succeed or fail, then replace the range check with
4682 -- a comparison against the upper bound.
4683
4684 elsif Lcheck in Compare_GE then
4685 if Warn2 and then not In_Instance then
4686 Error_Msg_N ("?lower bound test optimized away", Lo);
4687 Error_Msg_N ("\?value is known to be in range", Lo);
4688 end if;
4689
4690 Rewrite (N,
4691 Make_Op_Le (Loc,
4692 Left_Opnd => Lop,
4693 Right_Opnd => High_Bound (Rop)));
4694 Analyze_And_Resolve (N, Restyp);
4695 goto Leave;
4696
4697 -- If upper bound check succeeds and lower bound check is not
4698 -- known to succeed or fail, then replace the range check with
4699 -- a comparison against the lower bound.
4700
4701 elsif Ucheck in Compare_LE then
4702 if Warn2 and then not In_Instance then
4703 Error_Msg_N ("?upper bound test optimized away", Hi);
4704 Error_Msg_N ("\?value is known to be in range", Hi);
4705 end if;
4706
4707 Rewrite (N,
4708 Make_Op_Ge (Loc,
4709 Left_Opnd => Lop,
4710 Right_Opnd => Low_Bound (Rop)));
4711 Analyze_And_Resolve (N, Restyp);
4712 goto Leave;
4713 end if;
4714
4715 -- We couldn't optimize away the range check, but there is one
4716 -- more issue. If we are checking constant conditionals, then we
4717 -- see if we can determine the outcome assuming everything is
4718 -- valid, and if so give an appropriate warning.
4719
4720 if Warn1 and then not Assume_No_Invalid_Values then
4721 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4722 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4723
4724 -- Result is out of range for valid value
4725
4726 if Lcheck = LT or else Ucheck = GT then
4727 Error_Msg_N
4728 ("?value can only be in range if it is invalid", N);
4729
4730 -- Result is in range for valid value
4731
4732 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4733 Error_Msg_N
4734 ("?value can only be out of range if it is invalid", N);
4735
4736 -- Lower bound check succeeds if value is valid
4737
4738 elsif Warn2 and then Lcheck in Compare_GE then
4739 Error_Msg_N
4740 ("?lower bound check only fails if it is invalid", Lo);
4741
4742 -- Upper bound check succeeds if value is valid
4743
4744 elsif Warn2 and then Ucheck in Compare_LE then
4745 Error_Msg_N
4746 ("?upper bound check only fails for invalid values", Hi);
4747 end if;
4748 end if;
4749 end;
4750
4751 -- For all other cases of an explicit range, nothing to be done
4752
4753 goto Leave;
4754
4755 -- Here right operand is a subtype mark
4756
4757 else
4758 declare
4759 Typ : Entity_Id := Etype (Rop);
4760 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4761 Cond : Node_Id := Empty;
4762 New_N : Node_Id;
4763 Obj : Node_Id := Lop;
4764 SCIL_Node : Node_Id;
4765
4766 begin
4767 Remove_Side_Effects (Obj);
4768
4769 -- For tagged type, do tagged membership operation
4770
4771 if Is_Tagged_Type (Typ) then
4772
4773 -- No expansion will be performed when VM_Target, as the VM
4774 -- back-ends will handle the membership tests directly (tags
4775 -- are not explicitly represented in Java objects, so the
4776 -- normal tagged membership expansion is not what we want).
4777
4778 if Tagged_Type_Expansion then
4779 Tagged_Membership (N, SCIL_Node, New_N);
4780 Rewrite (N, New_N);
4781 Analyze_And_Resolve (N, Restyp);
4782
4783 -- Update decoration of relocated node referenced by the
4784 -- SCIL node.
4785
4786 if Generate_SCIL and then Present (SCIL_Node) then
4787 Set_SCIL_Node (N, SCIL_Node);
4788 end if;
4789 end if;
4790
4791 goto Leave;
4792
4793 -- If type is scalar type, rewrite as x in t'First .. t'Last.
4794 -- This reason we do this is that the bounds may have the wrong
4795 -- type if they come from the original type definition. Also this
4796 -- way we get all the processing above for an explicit range.
4797
4798 -- Don't do this for predicated types, since in this case we
4799 -- want to check the predicate!
4800
4801 elsif Is_Scalar_Type (Typ) then
4802 if No (Predicate_Function (Typ)) then
4803 Rewrite (Rop,
4804 Make_Range (Loc,
4805 Low_Bound =>
4806 Make_Attribute_Reference (Loc,
4807 Attribute_Name => Name_First,
4808 Prefix => New_Reference_To (Typ, Loc)),
4809
4810 High_Bound =>
4811 Make_Attribute_Reference (Loc,
4812 Attribute_Name => Name_Last,
4813 Prefix => New_Reference_To (Typ, Loc))));
4814 Analyze_And_Resolve (N, Restyp);
4815 end if;
4816
4817 goto Leave;
4818
4819 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4820 -- a membership test if the subtype mark denotes a constrained
4821 -- Unchecked_Union subtype and the expression lacks inferable
4822 -- discriminants.
4823
4824 elsif Is_Unchecked_Union (Base_Type (Typ))
4825 and then Is_Constrained (Typ)
4826 and then not Has_Inferable_Discriminants (Lop)
4827 then
4828 Insert_Action (N,
4829 Make_Raise_Program_Error (Loc,
4830 Reason => PE_Unchecked_Union_Restriction));
4831
4832 -- Prevent Gigi from generating incorrect code by rewriting the
4833 -- test as False.
4834
4835 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4836 goto Leave;
4837 end if;
4838
4839 -- Here we have a non-scalar type
4840
4841 if Is_Acc then
4842 Typ := Designated_Type (Typ);
4843 end if;
4844
4845 if not Is_Constrained (Typ) then
4846 Rewrite (N, New_Reference_To (Standard_True, Loc));
4847 Analyze_And_Resolve (N, Restyp);
4848
4849 -- For the constrained array case, we have to check the subscripts
4850 -- for an exact match if the lengths are non-zero (the lengths
4851 -- must match in any case).
4852
4853 elsif Is_Array_Type (Typ) then
4854 Check_Subscripts : declare
4855 function Build_Attribute_Reference
4856 (E : Node_Id;
4857 Nam : Name_Id;
4858 Dim : Nat) return Node_Id;
4859 -- Build attribute reference E'Nam (Dim)
4860
4861 -------------------------------
4862 -- Build_Attribute_Reference --
4863 -------------------------------
4864
4865 function Build_Attribute_Reference
4866 (E : Node_Id;
4867 Nam : Name_Id;
4868 Dim : Nat) return Node_Id
4869 is
4870 begin
4871 return
4872 Make_Attribute_Reference (Loc,
4873 Prefix => E,
4874 Attribute_Name => Nam,
4875 Expressions => New_List (
4876 Make_Integer_Literal (Loc, Dim)));
4877 end Build_Attribute_Reference;
4878
4879 -- Start of processing for Check_Subscripts
4880
4881 begin
4882 for J in 1 .. Number_Dimensions (Typ) loop
4883 Evolve_And_Then (Cond,
4884 Make_Op_Eq (Loc,
4885 Left_Opnd =>
4886 Build_Attribute_Reference
4887 (Duplicate_Subexpr_No_Checks (Obj),
4888 Name_First, J),
4889 Right_Opnd =>
4890 Build_Attribute_Reference
4891 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4892
4893 Evolve_And_Then (Cond,
4894 Make_Op_Eq (Loc,
4895 Left_Opnd =>
4896 Build_Attribute_Reference
4897 (Duplicate_Subexpr_No_Checks (Obj),
4898 Name_Last, J),
4899 Right_Opnd =>
4900 Build_Attribute_Reference
4901 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4902 end loop;
4903
4904 if Is_Acc then
4905 Cond :=
4906 Make_Or_Else (Loc,
4907 Left_Opnd =>
4908 Make_Op_Eq (Loc,
4909 Left_Opnd => Obj,
4910 Right_Opnd => Make_Null (Loc)),
4911 Right_Opnd => Cond);
4912 end if;
4913
4914 Rewrite (N, Cond);
4915 Analyze_And_Resolve (N, Restyp);
4916 end Check_Subscripts;
4917
4918 -- These are the cases where constraint checks may be required,
4919 -- e.g. records with possible discriminants
4920
4921 else
4922 -- Expand the test into a series of discriminant comparisons.
4923 -- The expression that is built is the negation of the one that
4924 -- is used for checking discriminant constraints.
4925
4926 Obj := Relocate_Node (Left_Opnd (N));
4927
4928 if Has_Discriminants (Typ) then
4929 Cond := Make_Op_Not (Loc,
4930 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4931
4932 if Is_Acc then
4933 Cond := Make_Or_Else (Loc,
4934 Left_Opnd =>
4935 Make_Op_Eq (Loc,
4936 Left_Opnd => Obj,
4937 Right_Opnd => Make_Null (Loc)),
4938 Right_Opnd => Cond);
4939 end if;
4940
4941 else
4942 Cond := New_Occurrence_Of (Standard_True, Loc);
4943 end if;
4944
4945 Rewrite (N, Cond);
4946 Analyze_And_Resolve (N, Restyp);
4947 end if;
4948 end;
4949 end if;
4950
4951 -- At this point, we have done the processing required for the basic
4952 -- membership test, but not yet dealt with the predicate.
4953
4954 <<Leave>>
4955
4956 -- If a predicate is present, then we do the predicate test, but we
4957 -- most certainly want to omit this if we are within the predicate
4958 -- function itself, since otherwise we have an infinite recursion!
4959
4960 declare
4961 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
4962
4963 begin
4964 if Present (PFunc)
4965 and then Current_Scope /= PFunc
4966 then
4967 Rewrite (N,
4968 Make_And_Then (Loc,
4969 Left_Opnd => Relocate_Node (N),
4970 Right_Opnd => Make_Predicate_Call (Rtyp, Lop)));
4971
4972 -- Analyze new expression, mark left operand as analyzed to
4973 -- avoid infinite recursion adding predicate calls.
4974
4975 Set_Analyzed (Left_Opnd (N));
4976 Analyze_And_Resolve (N, Standard_Boolean);
4977
4978 -- All done, skip attempt at compile time determination of result
4979
4980 return;
4981 end if;
4982 end;
4983 end Expand_N_In;
4984
4985 --------------------------------
4986 -- Expand_N_Indexed_Component --
4987 --------------------------------
4988
4989 procedure Expand_N_Indexed_Component (N : Node_Id) is
4990 Loc : constant Source_Ptr := Sloc (N);
4991 Typ : constant Entity_Id := Etype (N);
4992 P : constant Node_Id := Prefix (N);
4993 T : constant Entity_Id := Etype (P);
4994
4995 begin
4996 -- A special optimization, if we have an indexed component that is
4997 -- selecting from a slice, then we can eliminate the slice, since, for
4998 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4999 -- the range check required by the slice. The range check for the slice
5000 -- itself has already been generated. The range check for the
5001 -- subscripting operation is ensured by converting the subject to
5002 -- the subtype of the slice.
5003
5004 -- This optimization not only generates better code, avoiding slice
5005 -- messing especially in the packed case, but more importantly bypasses
5006 -- some problems in handling this peculiar case, for example, the issue
5007 -- of dealing specially with object renamings.
5008
5009 if Nkind (P) = N_Slice then
5010 Rewrite (N,
5011 Make_Indexed_Component (Loc,
5012 Prefix => Prefix (P),
5013 Expressions => New_List (
5014 Convert_To
5015 (Etype (First_Index (Etype (P))),
5016 First (Expressions (N))))));
5017 Analyze_And_Resolve (N, Typ);
5018 return;
5019 end if;
5020
5021 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
5022 -- function, then additional actuals must be passed.
5023
5024 if Ada_Version >= Ada_2005
5025 and then Is_Build_In_Place_Function_Call (P)
5026 then
5027 Make_Build_In_Place_Call_In_Anonymous_Context (P);
5028 end if;
5029
5030 -- If the prefix is an access type, then we unconditionally rewrite if
5031 -- as an explicit dereference. This simplifies processing for several
5032 -- cases, including packed array cases and certain cases in which checks
5033 -- must be generated. We used to try to do this only when it was
5034 -- necessary, but it cleans up the code to do it all the time.
5035
5036 if Is_Access_Type (T) then
5037 Insert_Explicit_Dereference (P);
5038 Analyze_And_Resolve (P, Designated_Type (T));
5039 end if;
5040
5041 -- Generate index and validity checks
5042
5043 Generate_Index_Checks (N);
5044
5045 if Validity_Checks_On and then Validity_Check_Subscripts then
5046 Apply_Subscript_Validity_Checks (N);
5047 end if;
5048
5049 -- All done for the non-packed case
5050
5051 if not Is_Packed (Etype (Prefix (N))) then
5052 return;
5053 end if;
5054
5055 -- For packed arrays that are not bit-packed (i.e. the case of an array
5056 -- with one or more index types with a non-contiguous enumeration type),
5057 -- we can always use the normal packed element get circuit.
5058
5059 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
5060 Expand_Packed_Element_Reference (N);
5061 return;
5062 end if;
5063
5064 -- For a reference to a component of a bit packed array, we have to
5065 -- convert it to a reference to the corresponding Packed_Array_Type.
5066 -- We only want to do this for simple references, and not for:
5067
5068 -- Left side of assignment, or prefix of left side of assignment, or
5069 -- prefix of the prefix, to handle packed arrays of packed arrays,
5070 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
5071
5072 -- Renaming objects in renaming associations
5073 -- This case is handled when a use of the renamed variable occurs
5074
5075 -- Actual parameters for a procedure call
5076 -- This case is handled in Exp_Ch6.Expand_Actuals
5077
5078 -- The second expression in a 'Read attribute reference
5079
5080 -- The prefix of an address or bit or size attribute reference
5081
5082 -- The following circuit detects these exceptions
5083
5084 declare
5085 Child : Node_Id := N;
5086 Parnt : Node_Id := Parent (N);
5087
5088 begin
5089 loop
5090 if Nkind (Parnt) = N_Unchecked_Expression then
5091 null;
5092
5093 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
5094 N_Procedure_Call_Statement)
5095 or else (Nkind (Parnt) = N_Parameter_Association
5096 and then
5097 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
5098 then
5099 return;
5100
5101 elsif Nkind (Parnt) = N_Attribute_Reference
5102 and then (Attribute_Name (Parnt) = Name_Address
5103 or else
5104 Attribute_Name (Parnt) = Name_Bit
5105 or else
5106 Attribute_Name (Parnt) = Name_Size)
5107 and then Prefix (Parnt) = Child
5108 then
5109 return;
5110
5111 elsif Nkind (Parnt) = N_Assignment_Statement
5112 and then Name (Parnt) = Child
5113 then
5114 return;
5115
5116 -- If the expression is an index of an indexed component, it must
5117 -- be expanded regardless of context.
5118
5119 elsif Nkind (Parnt) = N_Indexed_Component
5120 and then Child /= Prefix (Parnt)
5121 then
5122 Expand_Packed_Element_Reference (N);
5123 return;
5124
5125 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
5126 and then Name (Parent (Parnt)) = Parnt
5127 then
5128 return;
5129
5130 elsif Nkind (Parnt) = N_Attribute_Reference
5131 and then Attribute_Name (Parnt) = Name_Read
5132 and then Next (First (Expressions (Parnt))) = Child
5133 then
5134 return;
5135
5136 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
5137 and then Prefix (Parnt) = Child
5138 then
5139 null;
5140
5141 else
5142 Expand_Packed_Element_Reference (N);
5143 return;
5144 end if;
5145
5146 -- Keep looking up tree for unchecked expression, or if we are the
5147 -- prefix of a possible assignment left side.
5148
5149 Child := Parnt;
5150 Parnt := Parent (Child);
5151 end loop;
5152 end;
5153 end Expand_N_Indexed_Component;
5154
5155 ---------------------
5156 -- Expand_N_Not_In --
5157 ---------------------
5158
5159 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
5160 -- can be done. This avoids needing to duplicate this expansion code.
5161
5162 procedure Expand_N_Not_In (N : Node_Id) is
5163 Loc : constant Source_Ptr := Sloc (N);
5164 Typ : constant Entity_Id := Etype (N);
5165 Cfs : constant Boolean := Comes_From_Source (N);
5166
5167 begin
5168 Rewrite (N,
5169 Make_Op_Not (Loc,
5170 Right_Opnd =>
5171 Make_In (Loc,
5172 Left_Opnd => Left_Opnd (N),
5173 Right_Opnd => Right_Opnd (N))));
5174
5175 -- If this is a set membership, preserve list of alternatives
5176
5177 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
5178
5179 -- We want this to appear as coming from source if original does (see
5180 -- transformations in Expand_N_In).
5181
5182 Set_Comes_From_Source (N, Cfs);
5183 Set_Comes_From_Source (Right_Opnd (N), Cfs);
5184
5185 -- Now analyze transformed node
5186
5187 Analyze_And_Resolve (N, Typ);
5188 end Expand_N_Not_In;
5189
5190 -------------------
5191 -- Expand_N_Null --
5192 -------------------
5193
5194 -- The only replacement required is for the case of a null of a type that
5195 -- is an access to protected subprogram, or a subtype thereof. We represent
5196 -- such access values as a record, and so we must replace the occurrence of
5197 -- null by the equivalent record (with a null address and a null pointer in
5198 -- it), so that the backend creates the proper value.
5199
5200 procedure Expand_N_Null (N : Node_Id) is
5201 Loc : constant Source_Ptr := Sloc (N);
5202 Typ : constant Entity_Id := Base_Type (Etype (N));
5203 Agg : Node_Id;
5204
5205 begin
5206 if Is_Access_Protected_Subprogram_Type (Typ) then
5207 Agg :=
5208 Make_Aggregate (Loc,
5209 Expressions => New_List (
5210 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
5211 Make_Null (Loc)));
5212
5213 Rewrite (N, Agg);
5214 Analyze_And_Resolve (N, Equivalent_Type (Typ));
5215
5216 -- For subsequent semantic analysis, the node must retain its type.
5217 -- Gigi in any case replaces this type by the corresponding record
5218 -- type before processing the node.
5219
5220 Set_Etype (N, Typ);
5221 end if;
5222
5223 exception
5224 when RE_Not_Available =>
5225 return;
5226 end Expand_N_Null;
5227
5228 ---------------------
5229 -- Expand_N_Op_Abs --
5230 ---------------------
5231
5232 procedure Expand_N_Op_Abs (N : Node_Id) is
5233 Loc : constant Source_Ptr := Sloc (N);
5234 Expr : constant Node_Id := Right_Opnd (N);
5235
5236 begin
5237 Unary_Op_Validity_Checks (N);
5238
5239 -- Deal with software overflow checking
5240
5241 if not Backend_Overflow_Checks_On_Target
5242 and then Is_Signed_Integer_Type (Etype (N))
5243 and then Do_Overflow_Check (N)
5244 then
5245 -- The only case to worry about is when the argument is equal to the
5246 -- largest negative number, so what we do is to insert the check:
5247
5248 -- [constraint_error when Expr = typ'Base'First]
5249
5250 -- with the usual Duplicate_Subexpr use coding for expr
5251
5252 Insert_Action (N,
5253 Make_Raise_Constraint_Error (Loc,
5254 Condition =>
5255 Make_Op_Eq (Loc,
5256 Left_Opnd => Duplicate_Subexpr (Expr),
5257 Right_Opnd =>
5258 Make_Attribute_Reference (Loc,
5259 Prefix =>
5260 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
5261 Attribute_Name => Name_First)),
5262 Reason => CE_Overflow_Check_Failed));
5263 end if;
5264
5265 -- Vax floating-point types case
5266
5267 if Vax_Float (Etype (N)) then
5268 Expand_Vax_Arith (N);
5269 end if;
5270 end Expand_N_Op_Abs;
5271
5272 ---------------------
5273 -- Expand_N_Op_Add --
5274 ---------------------
5275
5276 procedure Expand_N_Op_Add (N : Node_Id) is
5277 Typ : constant Entity_Id := Etype (N);
5278
5279 begin
5280 Binary_Op_Validity_Checks (N);
5281
5282 -- N + 0 = 0 + N = N for integer types
5283
5284 if Is_Integer_Type (Typ) then
5285 if Compile_Time_Known_Value (Right_Opnd (N))
5286 and then Expr_Value (Right_Opnd (N)) = Uint_0
5287 then
5288 Rewrite (N, Left_Opnd (N));
5289 return;
5290
5291 elsif Compile_Time_Known_Value (Left_Opnd (N))
5292 and then Expr_Value (Left_Opnd (N)) = Uint_0
5293 then
5294 Rewrite (N, Right_Opnd (N));
5295 return;
5296 end if;
5297 end if;
5298
5299 -- Arithmetic overflow checks for signed integer/fixed point types
5300
5301 if Is_Signed_Integer_Type (Typ)
5302 or else Is_Fixed_Point_Type (Typ)
5303 then
5304 Apply_Arithmetic_Overflow_Check (N);
5305 return;
5306
5307 -- Vax floating-point types case
5308
5309 elsif Vax_Float (Typ) then
5310 Expand_Vax_Arith (N);
5311 end if;
5312 end Expand_N_Op_Add;
5313
5314 ---------------------
5315 -- Expand_N_Op_And --
5316 ---------------------
5317
5318 procedure Expand_N_Op_And (N : Node_Id) is
5319 Typ : constant Entity_Id := Etype (N);
5320
5321 begin
5322 Binary_Op_Validity_Checks (N);
5323
5324 if Is_Array_Type (Etype (N)) then
5325 Expand_Boolean_Operator (N);
5326
5327 elsif Is_Boolean_Type (Etype (N)) then
5328
5329 -- Replace AND by AND THEN if Short_Circuit_And_Or active and the
5330 -- type is standard Boolean (do not mess with AND that uses a non-
5331 -- standard Boolean type, because something strange is going on).
5332
5333 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
5334 Rewrite (N,
5335 Make_And_Then (Sloc (N),
5336 Left_Opnd => Relocate_Node (Left_Opnd (N)),
5337 Right_Opnd => Relocate_Node (Right_Opnd (N))));
5338 Analyze_And_Resolve (N, Typ);
5339
5340 -- Otherwise, adjust conditions
5341
5342 else
5343 Adjust_Condition (Left_Opnd (N));
5344 Adjust_Condition (Right_Opnd (N));
5345 Set_Etype (N, Standard_Boolean);
5346 Adjust_Result_Type (N, Typ);
5347 end if;
5348
5349 elsif Is_Intrinsic_Subprogram (Entity (N)) then
5350 Expand_Intrinsic_Call (N, Entity (N));
5351
5352 end if;
5353 end Expand_N_Op_And;
5354
5355 ------------------------
5356 -- Expand_N_Op_Concat --
5357 ------------------------
5358
5359 procedure Expand_N_Op_Concat (N : Node_Id) is
5360 Opnds : List_Id;
5361 -- List of operands to be concatenated
5362
5363 Cnode : Node_Id;
5364 -- Node which is to be replaced by the result of concatenating the nodes
5365 -- in the list Opnds.
5366
5367 begin
5368 -- Ensure validity of both operands
5369
5370 Binary_Op_Validity_Checks (N);
5371
5372 -- If we are the left operand of a concatenation higher up the tree,
5373 -- then do nothing for now, since we want to deal with a series of
5374 -- concatenations as a unit.
5375
5376 if Nkind (Parent (N)) = N_Op_Concat
5377 and then N = Left_Opnd (Parent (N))
5378 then
5379 return;
5380 end if;
5381
5382 -- We get here with a concatenation whose left operand may be a
5383 -- concatenation itself with a consistent type. We need to process
5384 -- these concatenation operands from left to right, which means
5385 -- from the deepest node in the tree to the highest node.
5386
5387 Cnode := N;
5388 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
5389 Cnode := Left_Opnd (Cnode);
5390 end loop;
5391
5392 -- Now Cnode is the deepest concatenation, and its parents are the
5393 -- concatenation nodes above, so now we process bottom up, doing the
5394 -- operations. We gather a string that is as long as possible up to five
5395 -- operands.
5396
5397 -- The outer loop runs more than once if more than one concatenation
5398 -- type is involved.
5399
5400 Outer : loop
5401 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
5402 Set_Parent (Opnds, N);
5403
5404 -- The inner loop gathers concatenation operands
5405
5406 Inner : while Cnode /= N
5407 and then Base_Type (Etype (Cnode)) =
5408 Base_Type (Etype (Parent (Cnode)))
5409 loop
5410 Cnode := Parent (Cnode);
5411 Append (Right_Opnd (Cnode), Opnds);
5412 end loop Inner;
5413
5414 Expand_Concatenate (Cnode, Opnds);
5415
5416 exit Outer when Cnode = N;
5417 Cnode := Parent (Cnode);
5418 end loop Outer;
5419 end Expand_N_Op_Concat;
5420
5421 ------------------------
5422 -- Expand_N_Op_Divide --
5423 ------------------------
5424
5425 procedure Expand_N_Op_Divide (N : Node_Id) is
5426 Loc : constant Source_Ptr := Sloc (N);
5427 Lopnd : constant Node_Id := Left_Opnd (N);
5428 Ropnd : constant Node_Id := Right_Opnd (N);
5429 Ltyp : constant Entity_Id := Etype (Lopnd);
5430 Rtyp : constant Entity_Id := Etype (Ropnd);
5431 Typ : Entity_Id := Etype (N);
5432 Rknow : constant Boolean := Is_Integer_Type (Typ)
5433 and then
5434 Compile_Time_Known_Value (Ropnd);
5435 Rval : Uint;
5436
5437 begin
5438 Binary_Op_Validity_Checks (N);
5439
5440 if Rknow then
5441 Rval := Expr_Value (Ropnd);
5442 end if;
5443
5444 -- N / 1 = N for integer types
5445
5446 if Rknow and then Rval = Uint_1 then
5447 Rewrite (N, Lopnd);
5448 return;
5449 end if;
5450
5451 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
5452 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5453 -- operand is an unsigned integer, as required for this to work.
5454
5455 if Nkind (Ropnd) = N_Op_Expon
5456 and then Is_Power_Of_2_For_Shift (Ropnd)
5457
5458 -- We cannot do this transformation in configurable run time mode if we
5459 -- have 64-bit integers and long shifts are not available.
5460
5461 and then
5462 (Esize (Ltyp) <= 32
5463 or else Support_Long_Shifts_On_Target)
5464 then
5465 Rewrite (N,
5466 Make_Op_Shift_Right (Loc,
5467 Left_Opnd => Lopnd,
5468 Right_Opnd =>
5469 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
5470 Analyze_And_Resolve (N, Typ);
5471 return;
5472 end if;
5473
5474 -- Do required fixup of universal fixed operation
5475
5476 if Typ = Universal_Fixed then
5477 Fixup_Universal_Fixed_Operation (N);
5478 Typ := Etype (N);
5479 end if;
5480
5481 -- Divisions with fixed-point results
5482
5483 if Is_Fixed_Point_Type (Typ) then
5484
5485 -- No special processing if Treat_Fixed_As_Integer is set, since
5486 -- from a semantic point of view such operations are simply integer
5487 -- operations and will be treated that way.
5488
5489 if not Treat_Fixed_As_Integer (N) then
5490 if Is_Integer_Type (Rtyp) then
5491 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
5492 else
5493 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
5494 end if;
5495 end if;
5496
5497 -- Other cases of division of fixed-point operands. Again we exclude the
5498 -- case where Treat_Fixed_As_Integer is set.
5499
5500 elsif (Is_Fixed_Point_Type (Ltyp) or else
5501 Is_Fixed_Point_Type (Rtyp))
5502 and then not Treat_Fixed_As_Integer (N)
5503 then
5504 if Is_Integer_Type (Typ) then
5505 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
5506 else
5507 pragma Assert (Is_Floating_Point_Type (Typ));
5508 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
5509 end if;
5510
5511 -- Mixed-mode operations can appear in a non-static universal context,
5512 -- in which case the integer argument must be converted explicitly.
5513
5514 elsif Typ = Universal_Real
5515 and then Is_Integer_Type (Rtyp)
5516 then
5517 Rewrite (Ropnd,
5518 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
5519
5520 Analyze_And_Resolve (Ropnd, Universal_Real);
5521
5522 elsif Typ = Universal_Real
5523 and then Is_Integer_Type (Ltyp)
5524 then
5525 Rewrite (Lopnd,
5526 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
5527
5528 Analyze_And_Resolve (Lopnd, Universal_Real);
5529
5530 -- Non-fixed point cases, do integer zero divide and overflow checks
5531
5532 elsif Is_Integer_Type (Typ) then
5533 Apply_Divide_Check (N);
5534
5535 -- Check for 64-bit division available, or long shifts if the divisor
5536 -- is a small power of 2 (since such divides will be converted into
5537 -- long shifts).
5538
5539 if Esize (Ltyp) > 32
5540 and then not Support_64_Bit_Divides_On_Target
5541 and then
5542 (not Rknow
5543 or else not Support_Long_Shifts_On_Target
5544 or else (Rval /= Uint_2 and then
5545 Rval /= Uint_4 and then
5546 Rval /= Uint_8 and then
5547 Rval /= Uint_16 and then
5548 Rval /= Uint_32 and then
5549 Rval /= Uint_64))
5550 then
5551 Error_Msg_CRT ("64-bit division", N);
5552 end if;
5553
5554 -- Deal with Vax_Float
5555
5556 elsif Vax_Float (Typ) then
5557 Expand_Vax_Arith (N);
5558 return;
5559 end if;
5560 end Expand_N_Op_Divide;
5561
5562 --------------------
5563 -- Expand_N_Op_Eq --
5564 --------------------
5565
5566 procedure Expand_N_Op_Eq (N : Node_Id) is
5567 Loc : constant Source_Ptr := Sloc (N);
5568 Typ : constant Entity_Id := Etype (N);
5569 Lhs : constant Node_Id := Left_Opnd (N);
5570 Rhs : constant Node_Id := Right_Opnd (N);
5571 Bodies : constant List_Id := New_List;
5572 A_Typ : constant Entity_Id := Etype (Lhs);
5573
5574 Typl : Entity_Id := A_Typ;
5575 Op_Name : Entity_Id;
5576 Prim : Elmt_Id;
5577
5578 procedure Build_Equality_Call (Eq : Entity_Id);
5579 -- If a constructed equality exists for the type or for its parent,
5580 -- build and analyze call, adding conversions if the operation is
5581 -- inherited.
5582
5583 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5584 -- Determines whether a type has a subcomponent of an unconstrained
5585 -- Unchecked_Union subtype. Typ is a record type.
5586
5587 -------------------------
5588 -- Build_Equality_Call --
5589 -------------------------
5590
5591 procedure Build_Equality_Call (Eq : Entity_Id) is
5592 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5593 L_Exp : Node_Id := Relocate_Node (Lhs);
5594 R_Exp : Node_Id := Relocate_Node (Rhs);
5595
5596 begin
5597 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5598 and then not Is_Class_Wide_Type (A_Typ)
5599 then
5600 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5601 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5602 end if;
5603
5604 -- If we have an Unchecked_Union, we need to add the inferred
5605 -- discriminant values as actuals in the function call. At this
5606 -- point, the expansion has determined that both operands have
5607 -- inferable discriminants.
5608
5609 if Is_Unchecked_Union (Op_Type) then
5610 declare
5611 Lhs_Type : constant Node_Id := Etype (L_Exp);
5612 Rhs_Type : constant Node_Id := Etype (R_Exp);
5613 Lhs_Discr_Val : Node_Id;
5614 Rhs_Discr_Val : Node_Id;
5615
5616 begin
5617 -- Per-object constrained selected components require special
5618 -- attention. If the enclosing scope of the component is an
5619 -- Unchecked_Union, we cannot reference its discriminants
5620 -- directly. This is why we use the two extra parameters of
5621 -- the equality function of the enclosing Unchecked_Union.
5622
5623 -- type UU_Type (Discr : Integer := 0) is
5624 -- . . .
5625 -- end record;
5626 -- pragma Unchecked_Union (UU_Type);
5627
5628 -- 1. Unchecked_Union enclosing record:
5629
5630 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5631 -- . . .
5632 -- Comp : UU_Type (Discr);
5633 -- . . .
5634 -- end Enclosing_UU_Type;
5635 -- pragma Unchecked_Union (Enclosing_UU_Type);
5636
5637 -- Obj1 : Enclosing_UU_Type;
5638 -- Obj2 : Enclosing_UU_Type (1);
5639
5640 -- [. . .] Obj1 = Obj2 [. . .]
5641
5642 -- Generated code:
5643
5644 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5645
5646 -- A and B are the formal parameters of the equality function
5647 -- of Enclosing_UU_Type. The function always has two extra
5648 -- formals to capture the inferred discriminant values.
5649
5650 -- 2. Non-Unchecked_Union enclosing record:
5651
5652 -- type
5653 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5654 -- is record
5655 -- . . .
5656 -- Comp : UU_Type (Discr);
5657 -- . . .
5658 -- end Enclosing_Non_UU_Type;
5659
5660 -- Obj1 : Enclosing_Non_UU_Type;
5661 -- Obj2 : Enclosing_Non_UU_Type (1);
5662
5663 -- ... Obj1 = Obj2 ...
5664
5665 -- Generated code:
5666
5667 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5668 -- obj1.discr, obj2.discr)) then
5669
5670 -- In this case we can directly reference the discriminants of
5671 -- the enclosing record.
5672
5673 -- Lhs of equality
5674
5675 if Nkind (Lhs) = N_Selected_Component
5676 and then Has_Per_Object_Constraint
5677 (Entity (Selector_Name (Lhs)))
5678 then
5679 -- Enclosing record is an Unchecked_Union, use formal A
5680
5681 if Is_Unchecked_Union
5682 (Scope (Entity (Selector_Name (Lhs))))
5683 then
5684 Lhs_Discr_Val := Make_Identifier (Loc, Name_A);
5685
5686 -- Enclosing record is of a non-Unchecked_Union type, it is
5687 -- possible to reference the discriminant.
5688
5689 else
5690 Lhs_Discr_Val :=
5691 Make_Selected_Component (Loc,
5692 Prefix => Prefix (Lhs),
5693 Selector_Name =>
5694 New_Copy
5695 (Get_Discriminant_Value
5696 (First_Discriminant (Lhs_Type),
5697 Lhs_Type,
5698 Stored_Constraint (Lhs_Type))));
5699 end if;
5700
5701 -- Comment needed here ???
5702
5703 else
5704 -- Infer the discriminant value
5705
5706 Lhs_Discr_Val :=
5707 New_Copy
5708 (Get_Discriminant_Value
5709 (First_Discriminant (Lhs_Type),
5710 Lhs_Type,
5711 Stored_Constraint (Lhs_Type)));
5712 end if;
5713
5714 -- Rhs of equality
5715
5716 if Nkind (Rhs) = N_Selected_Component
5717 and then Has_Per_Object_Constraint
5718 (Entity (Selector_Name (Rhs)))
5719 then
5720 if Is_Unchecked_Union
5721 (Scope (Entity (Selector_Name (Rhs))))
5722 then
5723 Rhs_Discr_Val := Make_Identifier (Loc, Name_B);
5724
5725 else
5726 Rhs_Discr_Val :=
5727 Make_Selected_Component (Loc,
5728 Prefix => Prefix (Rhs),
5729 Selector_Name =>
5730 New_Copy (Get_Discriminant_Value (
5731 First_Discriminant (Rhs_Type),
5732 Rhs_Type,
5733 Stored_Constraint (Rhs_Type))));
5734
5735 end if;
5736 else
5737 Rhs_Discr_Val :=
5738 New_Copy (Get_Discriminant_Value (
5739 First_Discriminant (Rhs_Type),
5740 Rhs_Type,
5741 Stored_Constraint (Rhs_Type)));
5742
5743 end if;
5744
5745 Rewrite (N,
5746 Make_Function_Call (Loc,
5747 Name => New_Reference_To (Eq, Loc),
5748 Parameter_Associations => New_List (
5749 L_Exp,
5750 R_Exp,
5751 Lhs_Discr_Val,
5752 Rhs_Discr_Val)));
5753 end;
5754
5755 -- Normal case, not an unchecked union
5756
5757 else
5758 Rewrite (N,
5759 Make_Function_Call (Loc,
5760 Name => New_Reference_To (Eq, Loc),
5761 Parameter_Associations => New_List (L_Exp, R_Exp)));
5762 end if;
5763
5764 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5765 end Build_Equality_Call;
5766
5767 ------------------------------------
5768 -- Has_Unconstrained_UU_Component --
5769 ------------------------------------
5770
5771 function Has_Unconstrained_UU_Component
5772 (Typ : Node_Id) return Boolean
5773 is
5774 Tdef : constant Node_Id :=
5775 Type_Definition (Declaration_Node (Base_Type (Typ)));
5776 Clist : Node_Id;
5777 Vpart : Node_Id;
5778
5779 function Component_Is_Unconstrained_UU
5780 (Comp : Node_Id) return Boolean;
5781 -- Determines whether the subtype of the component is an
5782 -- unconstrained Unchecked_Union.
5783
5784 function Variant_Is_Unconstrained_UU
5785 (Variant : Node_Id) return Boolean;
5786 -- Determines whether a component of the variant has an unconstrained
5787 -- Unchecked_Union subtype.
5788
5789 -----------------------------------
5790 -- Component_Is_Unconstrained_UU --
5791 -----------------------------------
5792
5793 function Component_Is_Unconstrained_UU
5794 (Comp : Node_Id) return Boolean
5795 is
5796 begin
5797 if Nkind (Comp) /= N_Component_Declaration then
5798 return False;
5799 end if;
5800
5801 declare
5802 Sindic : constant Node_Id :=
5803 Subtype_Indication (Component_Definition (Comp));
5804
5805 begin
5806 -- Unconstrained nominal type. In the case of a constraint
5807 -- present, the node kind would have been N_Subtype_Indication.
5808
5809 if Nkind (Sindic) = N_Identifier then
5810 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5811 end if;
5812
5813 return False;
5814 end;
5815 end Component_Is_Unconstrained_UU;
5816
5817 ---------------------------------
5818 -- Variant_Is_Unconstrained_UU --
5819 ---------------------------------
5820
5821 function Variant_Is_Unconstrained_UU
5822 (Variant : Node_Id) return Boolean
5823 is
5824 Clist : constant Node_Id := Component_List (Variant);
5825
5826 begin
5827 if Is_Empty_List (Component_Items (Clist)) then
5828 return False;
5829 end if;
5830
5831 -- We only need to test one component
5832
5833 declare
5834 Comp : Node_Id := First (Component_Items (Clist));
5835
5836 begin
5837 while Present (Comp) loop
5838 if Component_Is_Unconstrained_UU (Comp) then
5839 return True;
5840 end if;
5841
5842 Next (Comp);
5843 end loop;
5844 end;
5845
5846 -- None of the components withing the variant were of
5847 -- unconstrained Unchecked_Union type.
5848
5849 return False;
5850 end Variant_Is_Unconstrained_UU;
5851
5852 -- Start of processing for Has_Unconstrained_UU_Component
5853
5854 begin
5855 if Null_Present (Tdef) then
5856 return False;
5857 end if;
5858
5859 Clist := Component_List (Tdef);
5860 Vpart := Variant_Part (Clist);
5861
5862 -- Inspect available components
5863
5864 if Present (Component_Items (Clist)) then
5865 declare
5866 Comp : Node_Id := First (Component_Items (Clist));
5867
5868 begin
5869 while Present (Comp) loop
5870
5871 -- One component is sufficient
5872
5873 if Component_Is_Unconstrained_UU (Comp) then
5874 return True;
5875 end if;
5876
5877 Next (Comp);
5878 end loop;
5879 end;
5880 end if;
5881
5882 -- Inspect available components withing variants
5883
5884 if Present (Vpart) then
5885 declare
5886 Variant : Node_Id := First (Variants (Vpart));
5887
5888 begin
5889 while Present (Variant) loop
5890
5891 -- One component within a variant is sufficient
5892
5893 if Variant_Is_Unconstrained_UU (Variant) then
5894 return True;
5895 end if;
5896
5897 Next (Variant);
5898 end loop;
5899 end;
5900 end if;
5901
5902 -- Neither the available components, nor the components inside the
5903 -- variant parts were of an unconstrained Unchecked_Union subtype.
5904
5905 return False;
5906 end Has_Unconstrained_UU_Component;
5907
5908 -- Start of processing for Expand_N_Op_Eq
5909
5910 begin
5911 Binary_Op_Validity_Checks (N);
5912
5913 if Ekind (Typl) = E_Private_Type then
5914 Typl := Underlying_Type (Typl);
5915 elsif Ekind (Typl) = E_Private_Subtype then
5916 Typl := Underlying_Type (Base_Type (Typl));
5917 else
5918 null;
5919 end if;
5920
5921 -- It may happen in error situations that the underlying type is not
5922 -- set. The error will be detected later, here we just defend the
5923 -- expander code.
5924
5925 if No (Typl) then
5926 return;
5927 end if;
5928
5929 Typl := Base_Type (Typl);
5930
5931 -- Boolean types (requiring handling of non-standard case)
5932
5933 if Is_Boolean_Type (Typl) then
5934 Adjust_Condition (Left_Opnd (N));
5935 Adjust_Condition (Right_Opnd (N));
5936 Set_Etype (N, Standard_Boolean);
5937 Adjust_Result_Type (N, Typ);
5938
5939 -- Array types
5940
5941 elsif Is_Array_Type (Typl) then
5942
5943 -- If we are doing full validity checking, and it is possible for the
5944 -- array elements to be invalid then expand out array comparisons to
5945 -- make sure that we check the array elements.
5946
5947 if Validity_Check_Operands
5948 and then not Is_Known_Valid (Component_Type (Typl))
5949 then
5950 declare
5951 Save_Force_Validity_Checks : constant Boolean :=
5952 Force_Validity_Checks;
5953 begin
5954 Force_Validity_Checks := True;
5955 Rewrite (N,
5956 Expand_Array_Equality
5957 (N,
5958 Relocate_Node (Lhs),
5959 Relocate_Node (Rhs),
5960 Bodies,
5961 Typl));
5962 Insert_Actions (N, Bodies);
5963 Analyze_And_Resolve (N, Standard_Boolean);
5964 Force_Validity_Checks := Save_Force_Validity_Checks;
5965 end;
5966
5967 -- Packed case where both operands are known aligned
5968
5969 elsif Is_Bit_Packed_Array (Typl)
5970 and then not Is_Possibly_Unaligned_Object (Lhs)
5971 and then not Is_Possibly_Unaligned_Object (Rhs)
5972 then
5973 Expand_Packed_Eq (N);
5974
5975 -- Where the component type is elementary we can use a block bit
5976 -- comparison (if supported on the target) exception in the case
5977 -- of floating-point (negative zero issues require element by
5978 -- element comparison), and atomic types (where we must be sure
5979 -- to load elements independently) and possibly unaligned arrays.
5980
5981 elsif Is_Elementary_Type (Component_Type (Typl))
5982 and then not Is_Floating_Point_Type (Component_Type (Typl))
5983 and then not Is_Atomic (Component_Type (Typl))
5984 and then not Is_Possibly_Unaligned_Object (Lhs)
5985 and then not Is_Possibly_Unaligned_Object (Rhs)
5986 and then Support_Composite_Compare_On_Target
5987 then
5988 null;
5989
5990 -- For composite and floating-point cases, expand equality loop to
5991 -- make sure of using proper comparisons for tagged types, and
5992 -- correctly handling the floating-point case.
5993
5994 else
5995 Rewrite (N,
5996 Expand_Array_Equality
5997 (N,
5998 Relocate_Node (Lhs),
5999 Relocate_Node (Rhs),
6000 Bodies,
6001 Typl));
6002 Insert_Actions (N, Bodies, Suppress => All_Checks);
6003 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6004 end if;
6005
6006 -- Record Types
6007
6008 elsif Is_Record_Type (Typl) then
6009
6010 -- For tagged types, use the primitive "="
6011
6012 if Is_Tagged_Type (Typl) then
6013
6014 -- No need to do anything else compiling under restriction
6015 -- No_Dispatching_Calls. During the semantic analysis we
6016 -- already notified such violation.
6017
6018 if Restriction_Active (No_Dispatching_Calls) then
6019 return;
6020 end if;
6021
6022 -- If this is derived from an untagged private type completed with
6023 -- a tagged type, it does not have a full view, so we use the
6024 -- primitive operations of the private type. This check should no
6025 -- longer be necessary when these types get their full views???
6026
6027 if Is_Private_Type (A_Typ)
6028 and then not Is_Tagged_Type (A_Typ)
6029 and then Is_Derived_Type (A_Typ)
6030 and then No (Full_View (A_Typ))
6031 then
6032 -- Search for equality operation, checking that the operands
6033 -- have the same type. Note that we must find a matching entry,
6034 -- or something is very wrong!
6035
6036 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
6037
6038 while Present (Prim) loop
6039 exit when Chars (Node (Prim)) = Name_Op_Eq
6040 and then Etype (First_Formal (Node (Prim))) =
6041 Etype (Next_Formal (First_Formal (Node (Prim))))
6042 and then
6043 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
6044
6045 Next_Elmt (Prim);
6046 end loop;
6047
6048 pragma Assert (Present (Prim));
6049 Op_Name := Node (Prim);
6050
6051 -- Find the type's predefined equality or an overriding
6052 -- user- defined equality. The reason for not simply calling
6053 -- Find_Prim_Op here is that there may be a user-defined
6054 -- overloaded equality op that precedes the equality that we want,
6055 -- so we have to explicitly search (e.g., there could be an
6056 -- equality with two different parameter types).
6057
6058 else
6059 if Is_Class_Wide_Type (Typl) then
6060 Typl := Root_Type (Typl);
6061 end if;
6062
6063 Prim := First_Elmt (Primitive_Operations (Typl));
6064 while Present (Prim) loop
6065 exit when Chars (Node (Prim)) = Name_Op_Eq
6066 and then Etype (First_Formal (Node (Prim))) =
6067 Etype (Next_Formal (First_Formal (Node (Prim))))
6068 and then
6069 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
6070
6071 Next_Elmt (Prim);
6072 end loop;
6073
6074 pragma Assert (Present (Prim));
6075 Op_Name := Node (Prim);
6076 end if;
6077
6078 Build_Equality_Call (Op_Name);
6079
6080 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
6081 -- predefined equality operator for a type which has a subcomponent
6082 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
6083
6084 elsif Has_Unconstrained_UU_Component (Typl) then
6085 Insert_Action (N,
6086 Make_Raise_Program_Error (Loc,
6087 Reason => PE_Unchecked_Union_Restriction));
6088
6089 -- Prevent Gigi from generating incorrect code by rewriting the
6090 -- equality as a standard False.
6091
6092 Rewrite (N,
6093 New_Occurrence_Of (Standard_False, Loc));
6094
6095 elsif Is_Unchecked_Union (Typl) then
6096
6097 -- If we can infer the discriminants of the operands, we make a
6098 -- call to the TSS equality function.
6099
6100 if Has_Inferable_Discriminants (Lhs)
6101 and then
6102 Has_Inferable_Discriminants (Rhs)
6103 then
6104 Build_Equality_Call
6105 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6106
6107 else
6108 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6109 -- the predefined equality operator for an Unchecked_Union type
6110 -- if either of the operands lack inferable discriminants.
6111
6112 Insert_Action (N,
6113 Make_Raise_Program_Error (Loc,
6114 Reason => PE_Unchecked_Union_Restriction));
6115
6116 -- Prevent Gigi from generating incorrect code by rewriting
6117 -- the equality as a standard False.
6118
6119 Rewrite (N,
6120 New_Occurrence_Of (Standard_False, Loc));
6121
6122 end if;
6123
6124 -- If a type support function is present (for complex cases), use it
6125
6126 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
6127 Build_Equality_Call
6128 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6129
6130 -- Otherwise expand the component by component equality. Note that
6131 -- we never use block-bit comparisons for records, because of the
6132 -- problems with gaps. The backend will often be able to recombine
6133 -- the separate comparisons that we generate here.
6134
6135 else
6136 Remove_Side_Effects (Lhs);
6137 Remove_Side_Effects (Rhs);
6138 Rewrite (N,
6139 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
6140
6141 Insert_Actions (N, Bodies, Suppress => All_Checks);
6142 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6143 end if;
6144 end if;
6145
6146 -- Test if result is known at compile time
6147
6148 Rewrite_Comparison (N);
6149
6150 -- If we still have comparison for Vax_Float, process it
6151
6152 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
6153 Expand_Vax_Comparison (N);
6154 return;
6155 end if;
6156
6157 Optimize_Length_Comparison (N);
6158 end Expand_N_Op_Eq;
6159
6160 -----------------------
6161 -- Expand_N_Op_Expon --
6162 -----------------------
6163
6164 procedure Expand_N_Op_Expon (N : Node_Id) is
6165 Loc : constant Source_Ptr := Sloc (N);
6166 Typ : constant Entity_Id := Etype (N);
6167 Rtyp : constant Entity_Id := Root_Type (Typ);
6168 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
6169 Bastyp : constant Node_Id := Etype (Base);
6170 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
6171 Exptyp : constant Entity_Id := Etype (Exp);
6172 Ovflo : constant Boolean := Do_Overflow_Check (N);
6173 Expv : Uint;
6174 Xnode : Node_Id;
6175 Temp : Node_Id;
6176 Rent : RE_Id;
6177 Ent : Entity_Id;
6178 Etyp : Entity_Id;
6179
6180 begin
6181 Binary_Op_Validity_Checks (N);
6182
6183 -- If either operand is of a private type, then we have the use of an
6184 -- intrinsic operator, and we get rid of the privateness, by using root
6185 -- types of underlying types for the actual operation. Otherwise the
6186 -- private types will cause trouble if we expand multiplications or
6187 -- shifts etc. We also do this transformation if the result type is
6188 -- different from the base type.
6189
6190 if Is_Private_Type (Etype (Base))
6191 or else
6192 Is_Private_Type (Typ)
6193 or else
6194 Is_Private_Type (Exptyp)
6195 or else
6196 Rtyp /= Root_Type (Bastyp)
6197 then
6198 declare
6199 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
6200 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
6201
6202 begin
6203 Rewrite (N,
6204 Unchecked_Convert_To (Typ,
6205 Make_Op_Expon (Loc,
6206 Left_Opnd => Unchecked_Convert_To (Bt, Base),
6207 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
6208 Analyze_And_Resolve (N, Typ);
6209 return;
6210 end;
6211 end if;
6212
6213 -- Test for case of known right argument
6214
6215 if Compile_Time_Known_Value (Exp) then
6216 Expv := Expr_Value (Exp);
6217
6218 -- We only fold small non-negative exponents. You might think we
6219 -- could fold small negative exponents for the real case, but we
6220 -- can't because we are required to raise Constraint_Error for
6221 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
6222 -- See ACVC test C4A012B.
6223
6224 if Expv >= 0 and then Expv <= 4 then
6225
6226 -- X ** 0 = 1 (or 1.0)
6227
6228 if Expv = 0 then
6229
6230 -- Call Remove_Side_Effects to ensure that any side effects
6231 -- in the ignored left operand (in particular function calls
6232 -- to user defined functions) are properly executed.
6233
6234 Remove_Side_Effects (Base);
6235
6236 if Ekind (Typ) in Integer_Kind then
6237 Xnode := Make_Integer_Literal (Loc, Intval => 1);
6238 else
6239 Xnode := Make_Real_Literal (Loc, Ureal_1);
6240 end if;
6241
6242 -- X ** 1 = X
6243
6244 elsif Expv = 1 then
6245 Xnode := Base;
6246
6247 -- X ** 2 = X * X
6248
6249 elsif Expv = 2 then
6250 Xnode :=
6251 Make_Op_Multiply (Loc,
6252 Left_Opnd => Duplicate_Subexpr (Base),
6253 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6254
6255 -- X ** 3 = X * X * X
6256
6257 elsif Expv = 3 then
6258 Xnode :=
6259 Make_Op_Multiply (Loc,
6260 Left_Opnd =>
6261 Make_Op_Multiply (Loc,
6262 Left_Opnd => Duplicate_Subexpr (Base),
6263 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
6264 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6265
6266 -- X ** 4 ->
6267 -- En : constant base'type := base * base;
6268 -- ...
6269 -- En * En
6270
6271 else -- Expv = 4
6272 Temp := Make_Temporary (Loc, 'E', Base);
6273
6274 Insert_Actions (N, New_List (
6275 Make_Object_Declaration (Loc,
6276 Defining_Identifier => Temp,
6277 Constant_Present => True,
6278 Object_Definition => New_Reference_To (Typ, Loc),
6279 Expression =>
6280 Make_Op_Multiply (Loc,
6281 Left_Opnd => Duplicate_Subexpr (Base),
6282 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
6283
6284 Xnode :=
6285 Make_Op_Multiply (Loc,
6286 Left_Opnd => New_Reference_To (Temp, Loc),
6287 Right_Opnd => New_Reference_To (Temp, Loc));
6288 end if;
6289
6290 Rewrite (N, Xnode);
6291 Analyze_And_Resolve (N, Typ);
6292 return;
6293 end if;
6294 end if;
6295
6296 -- Case of (2 ** expression) appearing as an argument of an integer
6297 -- multiplication, or as the right argument of a division of a non-
6298 -- negative integer. In such cases we leave the node untouched, setting
6299 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
6300 -- of the higher level node converts it into a shift.
6301
6302 -- Another case is 2 ** N in any other context. We simply convert
6303 -- this to 1 * 2 ** N, and then the above transformation applies.
6304
6305 -- Note: this transformation is not applicable for a modular type with
6306 -- a non-binary modulus in the multiplication case, since we get a wrong
6307 -- result if the shift causes an overflow before the modular reduction.
6308
6309 if Nkind (Base) = N_Integer_Literal
6310 and then Intval (Base) = 2
6311 and then Is_Integer_Type (Root_Type (Exptyp))
6312 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
6313 and then Is_Unsigned_Type (Exptyp)
6314 and then not Ovflo
6315 then
6316 -- First the multiply and divide cases
6317
6318 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
6319 declare
6320 P : constant Node_Id := Parent (N);
6321 L : constant Node_Id := Left_Opnd (P);
6322 R : constant Node_Id := Right_Opnd (P);
6323
6324 begin
6325 if (Nkind (P) = N_Op_Multiply
6326 and then not Non_Binary_Modulus (Typ)
6327 and then
6328 ((Is_Integer_Type (Etype (L)) and then R = N)
6329 or else
6330 (Is_Integer_Type (Etype (R)) and then L = N))
6331 and then not Do_Overflow_Check (P))
6332 or else
6333 (Nkind (P) = N_Op_Divide
6334 and then Is_Integer_Type (Etype (L))
6335 and then Is_Unsigned_Type (Etype (L))
6336 and then R = N
6337 and then not Do_Overflow_Check (P))
6338 then
6339 Set_Is_Power_Of_2_For_Shift (N);
6340 return;
6341 end if;
6342 end;
6343
6344 -- Now the other cases
6345
6346 elsif not Non_Binary_Modulus (Typ) then
6347 Rewrite (N,
6348 Make_Op_Multiply (Loc,
6349 Left_Opnd => Make_Integer_Literal (Loc, 1),
6350 Right_Opnd => Relocate_Node (N)));
6351 Analyze_And_Resolve (N, Typ);
6352 return;
6353 end if;
6354 end if;
6355
6356 -- Fall through if exponentiation must be done using a runtime routine
6357
6358 -- First deal with modular case
6359
6360 if Is_Modular_Integer_Type (Rtyp) then
6361
6362 -- Non-binary case, we call the special exponentiation routine for
6363 -- the non-binary case, converting the argument to Long_Long_Integer
6364 -- and passing the modulus value. Then the result is converted back
6365 -- to the base type.
6366
6367 if Non_Binary_Modulus (Rtyp) then
6368 Rewrite (N,
6369 Convert_To (Typ,
6370 Make_Function_Call (Loc,
6371 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
6372 Parameter_Associations => New_List (
6373 Convert_To (Standard_Integer, Base),
6374 Make_Integer_Literal (Loc, Modulus (Rtyp)),
6375 Exp))));
6376
6377 -- Binary case, in this case, we call one of two routines, either the
6378 -- unsigned integer case, or the unsigned long long integer case,
6379 -- with a final "and" operation to do the required mod.
6380
6381 else
6382 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
6383 Ent := RTE (RE_Exp_Unsigned);
6384 else
6385 Ent := RTE (RE_Exp_Long_Long_Unsigned);
6386 end if;
6387
6388 Rewrite (N,
6389 Convert_To (Typ,
6390 Make_Op_And (Loc,
6391 Left_Opnd =>
6392 Make_Function_Call (Loc,
6393 Name => New_Reference_To (Ent, Loc),
6394 Parameter_Associations => New_List (
6395 Convert_To (Etype (First_Formal (Ent)), Base),
6396 Exp)),
6397 Right_Opnd =>
6398 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
6399
6400 end if;
6401
6402 -- Common exit point for modular type case
6403
6404 Analyze_And_Resolve (N, Typ);
6405 return;
6406
6407 -- Signed integer cases, done using either Integer or Long_Long_Integer.
6408 -- It is not worth having routines for Short_[Short_]Integer, since for
6409 -- most machines it would not help, and it would generate more code that
6410 -- might need certification when a certified run time is required.
6411
6412 -- In the integer cases, we have two routines, one for when overflow
6413 -- checks are required, and one when they are not required, since there
6414 -- is a real gain in omitting checks on many machines.
6415
6416 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
6417 or else (Rtyp = Base_Type (Standard_Long_Integer)
6418 and then
6419 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
6420 or else (Rtyp = Universal_Integer)
6421 then
6422 Etyp := Standard_Long_Long_Integer;
6423
6424 if Ovflo then
6425 Rent := RE_Exp_Long_Long_Integer;
6426 else
6427 Rent := RE_Exn_Long_Long_Integer;
6428 end if;
6429
6430 elsif Is_Signed_Integer_Type (Rtyp) then
6431 Etyp := Standard_Integer;
6432
6433 if Ovflo then
6434 Rent := RE_Exp_Integer;
6435 else
6436 Rent := RE_Exn_Integer;
6437 end if;
6438
6439 -- Floating-point cases, always done using Long_Long_Float. We do not
6440 -- need separate routines for the overflow case here, since in the case
6441 -- of floating-point, we generate infinities anyway as a rule (either
6442 -- that or we automatically trap overflow), and if there is an infinity
6443 -- generated and a range check is required, the check will fail anyway.
6444
6445 else
6446 pragma Assert (Is_Floating_Point_Type (Rtyp));
6447 Etyp := Standard_Long_Long_Float;
6448 Rent := RE_Exn_Long_Long_Float;
6449 end if;
6450
6451 -- Common processing for integer cases and floating-point cases.
6452 -- If we are in the right type, we can call runtime routine directly
6453
6454 if Typ = Etyp
6455 and then Rtyp /= Universal_Integer
6456 and then Rtyp /= Universal_Real
6457 then
6458 Rewrite (N,
6459 Make_Function_Call (Loc,
6460 Name => New_Reference_To (RTE (Rent), Loc),
6461 Parameter_Associations => New_List (Base, Exp)));
6462
6463 -- Otherwise we have to introduce conversions (conversions are also
6464 -- required in the universal cases, since the runtime routine is
6465 -- typed using one of the standard types).
6466
6467 else
6468 Rewrite (N,
6469 Convert_To (Typ,
6470 Make_Function_Call (Loc,
6471 Name => New_Reference_To (RTE (Rent), Loc),
6472 Parameter_Associations => New_List (
6473 Convert_To (Etyp, Base),
6474 Exp))));
6475 end if;
6476
6477 Analyze_And_Resolve (N, Typ);
6478 return;
6479
6480 exception
6481 when RE_Not_Available =>
6482 return;
6483 end Expand_N_Op_Expon;
6484
6485 --------------------
6486 -- Expand_N_Op_Ge --
6487 --------------------
6488
6489 procedure Expand_N_Op_Ge (N : Node_Id) is
6490 Typ : constant Entity_Id := Etype (N);
6491 Op1 : constant Node_Id := Left_Opnd (N);
6492 Op2 : constant Node_Id := Right_Opnd (N);
6493 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6494
6495 begin
6496 Binary_Op_Validity_Checks (N);
6497
6498 if Is_Array_Type (Typ1) then
6499 Expand_Array_Comparison (N);
6500 return;
6501 end if;
6502
6503 if Is_Boolean_Type (Typ1) then
6504 Adjust_Condition (Op1);
6505 Adjust_Condition (Op2);
6506 Set_Etype (N, Standard_Boolean);
6507 Adjust_Result_Type (N, Typ);
6508 end if;
6509
6510 Rewrite_Comparison (N);
6511
6512 -- If we still have comparison, and Vax_Float type, process it
6513
6514 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6515 Expand_Vax_Comparison (N);
6516 return;
6517 end if;
6518
6519 Optimize_Length_Comparison (N);
6520 end Expand_N_Op_Ge;
6521
6522 --------------------
6523 -- Expand_N_Op_Gt --
6524 --------------------
6525
6526 procedure Expand_N_Op_Gt (N : Node_Id) is
6527 Typ : constant Entity_Id := Etype (N);
6528 Op1 : constant Node_Id := Left_Opnd (N);
6529 Op2 : constant Node_Id := Right_Opnd (N);
6530 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6531
6532 begin
6533 Binary_Op_Validity_Checks (N);
6534
6535 if Is_Array_Type (Typ1) then
6536 Expand_Array_Comparison (N);
6537 return;
6538 end if;
6539
6540 if Is_Boolean_Type (Typ1) then
6541 Adjust_Condition (Op1);
6542 Adjust_Condition (Op2);
6543 Set_Etype (N, Standard_Boolean);
6544 Adjust_Result_Type (N, Typ);
6545 end if;
6546
6547 Rewrite_Comparison (N);
6548
6549 -- If we still have comparison, and Vax_Float type, process it
6550
6551 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6552 Expand_Vax_Comparison (N);
6553 return;
6554 end if;
6555
6556 Optimize_Length_Comparison (N);
6557 end Expand_N_Op_Gt;
6558
6559 --------------------
6560 -- Expand_N_Op_Le --
6561 --------------------
6562
6563 procedure Expand_N_Op_Le (N : Node_Id) is
6564 Typ : constant Entity_Id := Etype (N);
6565 Op1 : constant Node_Id := Left_Opnd (N);
6566 Op2 : constant Node_Id := Right_Opnd (N);
6567 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6568
6569 begin
6570 Binary_Op_Validity_Checks (N);
6571
6572 if Is_Array_Type (Typ1) then
6573 Expand_Array_Comparison (N);
6574 return;
6575 end if;
6576
6577 if Is_Boolean_Type (Typ1) then
6578 Adjust_Condition (Op1);
6579 Adjust_Condition (Op2);
6580 Set_Etype (N, Standard_Boolean);
6581 Adjust_Result_Type (N, Typ);
6582 end if;
6583
6584 Rewrite_Comparison (N);
6585
6586 -- If we still have comparison, and Vax_Float type, process it
6587
6588 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6589 Expand_Vax_Comparison (N);
6590 return;
6591 end if;
6592
6593 Optimize_Length_Comparison (N);
6594 end Expand_N_Op_Le;
6595
6596 --------------------
6597 -- Expand_N_Op_Lt --
6598 --------------------
6599
6600 procedure Expand_N_Op_Lt (N : Node_Id) is
6601 Typ : constant Entity_Id := Etype (N);
6602 Op1 : constant Node_Id := Left_Opnd (N);
6603 Op2 : constant Node_Id := Right_Opnd (N);
6604 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6605
6606 begin
6607 Binary_Op_Validity_Checks (N);
6608
6609 if Is_Array_Type (Typ1) then
6610 Expand_Array_Comparison (N);
6611 return;
6612 end if;
6613
6614 if Is_Boolean_Type (Typ1) then
6615 Adjust_Condition (Op1);
6616 Adjust_Condition (Op2);
6617 Set_Etype (N, Standard_Boolean);
6618 Adjust_Result_Type (N, Typ);
6619 end if;
6620
6621 Rewrite_Comparison (N);
6622
6623 -- If we still have comparison, and Vax_Float type, process it
6624
6625 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6626 Expand_Vax_Comparison (N);
6627 return;
6628 end if;
6629
6630 Optimize_Length_Comparison (N);
6631 end Expand_N_Op_Lt;
6632
6633 -----------------------
6634 -- Expand_N_Op_Minus --
6635 -----------------------
6636
6637 procedure Expand_N_Op_Minus (N : Node_Id) is
6638 Loc : constant Source_Ptr := Sloc (N);
6639 Typ : constant Entity_Id := Etype (N);
6640
6641 begin
6642 Unary_Op_Validity_Checks (N);
6643
6644 if not Backend_Overflow_Checks_On_Target
6645 and then Is_Signed_Integer_Type (Etype (N))
6646 and then Do_Overflow_Check (N)
6647 then
6648 -- Software overflow checking expands -expr into (0 - expr)
6649
6650 Rewrite (N,
6651 Make_Op_Subtract (Loc,
6652 Left_Opnd => Make_Integer_Literal (Loc, 0),
6653 Right_Opnd => Right_Opnd (N)));
6654
6655 Analyze_And_Resolve (N, Typ);
6656
6657 -- Vax floating-point types case
6658
6659 elsif Vax_Float (Etype (N)) then
6660 Expand_Vax_Arith (N);
6661 end if;
6662 end Expand_N_Op_Minus;
6663
6664 ---------------------
6665 -- Expand_N_Op_Mod --
6666 ---------------------
6667
6668 procedure Expand_N_Op_Mod (N : Node_Id) is
6669 Loc : constant Source_Ptr := Sloc (N);
6670 Typ : constant Entity_Id := Etype (N);
6671 Left : constant Node_Id := Left_Opnd (N);
6672 Right : constant Node_Id := Right_Opnd (N);
6673 DOC : constant Boolean := Do_Overflow_Check (N);
6674 DDC : constant Boolean := Do_Division_Check (N);
6675
6676 LLB : Uint;
6677 Llo : Uint;
6678 Lhi : Uint;
6679 LOK : Boolean;
6680 Rlo : Uint;
6681 Rhi : Uint;
6682 ROK : Boolean;
6683
6684 pragma Warnings (Off, Lhi);
6685
6686 begin
6687 Binary_Op_Validity_Checks (N);
6688
6689 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
6690 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
6691
6692 -- Convert mod to rem if operands are known non-negative. We do this
6693 -- since it is quite likely that this will improve the quality of code,
6694 -- (the operation now corresponds to the hardware remainder), and it
6695 -- does not seem likely that it could be harmful.
6696
6697 if LOK and then Llo >= 0
6698 and then
6699 ROK and then Rlo >= 0
6700 then
6701 Rewrite (N,
6702 Make_Op_Rem (Sloc (N),
6703 Left_Opnd => Left_Opnd (N),
6704 Right_Opnd => Right_Opnd (N)));
6705
6706 -- Instead of reanalyzing the node we do the analysis manually. This
6707 -- avoids anomalies when the replacement is done in an instance and
6708 -- is epsilon more efficient.
6709
6710 Set_Entity (N, Standard_Entity (S_Op_Rem));
6711 Set_Etype (N, Typ);
6712 Set_Do_Overflow_Check (N, DOC);
6713 Set_Do_Division_Check (N, DDC);
6714 Expand_N_Op_Rem (N);
6715 Set_Analyzed (N);
6716
6717 -- Otherwise, normal mod processing
6718
6719 else
6720 if Is_Integer_Type (Etype (N)) then
6721 Apply_Divide_Check (N);
6722 end if;
6723
6724 -- Apply optimization x mod 1 = 0. We don't really need that with
6725 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6726 -- certainly harmless.
6727
6728 if Is_Integer_Type (Etype (N))
6729 and then Compile_Time_Known_Value (Right)
6730 and then Expr_Value (Right) = Uint_1
6731 then
6732 -- Call Remove_Side_Effects to ensure that any side effects in
6733 -- the ignored left operand (in particular function calls to
6734 -- user defined functions) are properly executed.
6735
6736 Remove_Side_Effects (Left);
6737
6738 Rewrite (N, Make_Integer_Literal (Loc, 0));
6739 Analyze_And_Resolve (N, Typ);
6740 return;
6741 end if;
6742
6743 -- Deal with annoying case of largest negative number remainder
6744 -- minus one. Gigi does not handle this case correctly, because
6745 -- it generates a divide instruction which may trap in this case.
6746
6747 -- In fact the check is quite easy, if the right operand is -1, then
6748 -- the mod value is always 0, and we can just ignore the left operand
6749 -- completely in this case.
6750
6751 -- The operand type may be private (e.g. in the expansion of an
6752 -- intrinsic operation) so we must use the underlying type to get the
6753 -- bounds, and convert the literals explicitly.
6754
6755 LLB :=
6756 Expr_Value
6757 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6758
6759 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6760 and then
6761 ((not LOK) or else (Llo = LLB))
6762 then
6763 Rewrite (N,
6764 Make_Conditional_Expression (Loc,
6765 Expressions => New_List (
6766 Make_Op_Eq (Loc,
6767 Left_Opnd => Duplicate_Subexpr (Right),
6768 Right_Opnd =>
6769 Unchecked_Convert_To (Typ,
6770 Make_Integer_Literal (Loc, -1))),
6771 Unchecked_Convert_To (Typ,
6772 Make_Integer_Literal (Loc, Uint_0)),
6773 Relocate_Node (N))));
6774
6775 Set_Analyzed (Next (Next (First (Expressions (N)))));
6776 Analyze_And_Resolve (N, Typ);
6777 end if;
6778 end if;
6779 end Expand_N_Op_Mod;
6780
6781 --------------------------
6782 -- Expand_N_Op_Multiply --
6783 --------------------------
6784
6785 procedure Expand_N_Op_Multiply (N : Node_Id) is
6786 Loc : constant Source_Ptr := Sloc (N);
6787 Lop : constant Node_Id := Left_Opnd (N);
6788 Rop : constant Node_Id := Right_Opnd (N);
6789
6790 Lp2 : constant Boolean :=
6791 Nkind (Lop) = N_Op_Expon
6792 and then Is_Power_Of_2_For_Shift (Lop);
6793
6794 Rp2 : constant Boolean :=
6795 Nkind (Rop) = N_Op_Expon
6796 and then Is_Power_Of_2_For_Shift (Rop);
6797
6798 Ltyp : constant Entity_Id := Etype (Lop);
6799 Rtyp : constant Entity_Id := Etype (Rop);
6800 Typ : Entity_Id := Etype (N);
6801
6802 begin
6803 Binary_Op_Validity_Checks (N);
6804
6805 -- Special optimizations for integer types
6806
6807 if Is_Integer_Type (Typ) then
6808
6809 -- N * 0 = 0 for integer types
6810
6811 if Compile_Time_Known_Value (Rop)
6812 and then Expr_Value (Rop) = Uint_0
6813 then
6814 -- Call Remove_Side_Effects to ensure that any side effects in
6815 -- the ignored left operand (in particular function calls to
6816 -- user defined functions) are properly executed.
6817
6818 Remove_Side_Effects (Lop);
6819
6820 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6821 Analyze_And_Resolve (N, Typ);
6822 return;
6823 end if;
6824
6825 -- Similar handling for 0 * N = 0
6826
6827 if Compile_Time_Known_Value (Lop)
6828 and then Expr_Value (Lop) = Uint_0
6829 then
6830 Remove_Side_Effects (Rop);
6831 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6832 Analyze_And_Resolve (N, Typ);
6833 return;
6834 end if;
6835
6836 -- N * 1 = 1 * N = N for integer types
6837
6838 -- This optimisation is not done if we are going to
6839 -- rewrite the product 1 * 2 ** N to a shift.
6840
6841 if Compile_Time_Known_Value (Rop)
6842 and then Expr_Value (Rop) = Uint_1
6843 and then not Lp2
6844 then
6845 Rewrite (N, Lop);
6846 return;
6847
6848 elsif Compile_Time_Known_Value (Lop)
6849 and then Expr_Value (Lop) = Uint_1
6850 and then not Rp2
6851 then
6852 Rewrite (N, Rop);
6853 return;
6854 end if;
6855 end if;
6856
6857 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6858 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6859 -- operand is an integer, as required for this to work.
6860
6861 if Rp2 then
6862 if Lp2 then
6863
6864 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6865
6866 Rewrite (N,
6867 Make_Op_Expon (Loc,
6868 Left_Opnd => Make_Integer_Literal (Loc, 2),
6869 Right_Opnd =>
6870 Make_Op_Add (Loc,
6871 Left_Opnd => Right_Opnd (Lop),
6872 Right_Opnd => Right_Opnd (Rop))));
6873 Analyze_And_Resolve (N, Typ);
6874 return;
6875
6876 else
6877 Rewrite (N,
6878 Make_Op_Shift_Left (Loc,
6879 Left_Opnd => Lop,
6880 Right_Opnd =>
6881 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6882 Analyze_And_Resolve (N, Typ);
6883 return;
6884 end if;
6885
6886 -- Same processing for the operands the other way round
6887
6888 elsif Lp2 then
6889 Rewrite (N,
6890 Make_Op_Shift_Left (Loc,
6891 Left_Opnd => Rop,
6892 Right_Opnd =>
6893 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6894 Analyze_And_Resolve (N, Typ);
6895 return;
6896 end if;
6897
6898 -- Do required fixup of universal fixed operation
6899
6900 if Typ = Universal_Fixed then
6901 Fixup_Universal_Fixed_Operation (N);
6902 Typ := Etype (N);
6903 end if;
6904
6905 -- Multiplications with fixed-point results
6906
6907 if Is_Fixed_Point_Type (Typ) then
6908
6909 -- No special processing if Treat_Fixed_As_Integer is set, since from
6910 -- a semantic point of view such operations are simply integer
6911 -- operations and will be treated that way.
6912
6913 if not Treat_Fixed_As_Integer (N) then
6914
6915 -- Case of fixed * integer => fixed
6916
6917 if Is_Integer_Type (Rtyp) then
6918 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6919
6920 -- Case of integer * fixed => fixed
6921
6922 elsif Is_Integer_Type (Ltyp) then
6923 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6924
6925 -- Case of fixed * fixed => fixed
6926
6927 else
6928 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6929 end if;
6930 end if;
6931
6932 -- Other cases of multiplication of fixed-point operands. Again we
6933 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6934
6935 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6936 and then not Treat_Fixed_As_Integer (N)
6937 then
6938 if Is_Integer_Type (Typ) then
6939 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6940 else
6941 pragma Assert (Is_Floating_Point_Type (Typ));
6942 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6943 end if;
6944
6945 -- Mixed-mode operations can appear in a non-static universal context,
6946 -- in which case the integer argument must be converted explicitly.
6947
6948 elsif Typ = Universal_Real
6949 and then Is_Integer_Type (Rtyp)
6950 then
6951 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6952
6953 Analyze_And_Resolve (Rop, Universal_Real);
6954
6955 elsif Typ = Universal_Real
6956 and then Is_Integer_Type (Ltyp)
6957 then
6958 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6959
6960 Analyze_And_Resolve (Lop, Universal_Real);
6961
6962 -- Non-fixed point cases, check software overflow checking required
6963
6964 elsif Is_Signed_Integer_Type (Etype (N)) then
6965 Apply_Arithmetic_Overflow_Check (N);
6966
6967 -- Deal with VAX float case
6968
6969 elsif Vax_Float (Typ) then
6970 Expand_Vax_Arith (N);
6971 return;
6972 end if;
6973 end Expand_N_Op_Multiply;
6974
6975 --------------------
6976 -- Expand_N_Op_Ne --
6977 --------------------
6978
6979 procedure Expand_N_Op_Ne (N : Node_Id) is
6980 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6981
6982 begin
6983 -- Case of elementary type with standard operator
6984
6985 if Is_Elementary_Type (Typ)
6986 and then Sloc (Entity (N)) = Standard_Location
6987 then
6988 Binary_Op_Validity_Checks (N);
6989
6990 -- Boolean types (requiring handling of non-standard case)
6991
6992 if Is_Boolean_Type (Typ) then
6993 Adjust_Condition (Left_Opnd (N));
6994 Adjust_Condition (Right_Opnd (N));
6995 Set_Etype (N, Standard_Boolean);
6996 Adjust_Result_Type (N, Typ);
6997 end if;
6998
6999 Rewrite_Comparison (N);
7000
7001 -- If we still have comparison for Vax_Float, process it
7002
7003 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
7004 Expand_Vax_Comparison (N);
7005 return;
7006 end if;
7007
7008 -- For all cases other than elementary types, we rewrite node as the
7009 -- negation of an equality operation, and reanalyze. The equality to be
7010 -- used is defined in the same scope and has the same signature. This
7011 -- signature must be set explicitly since in an instance it may not have
7012 -- the same visibility as in the generic unit. This avoids duplicating
7013 -- or factoring the complex code for record/array equality tests etc.
7014
7015 else
7016 declare
7017 Loc : constant Source_Ptr := Sloc (N);
7018 Neg : Node_Id;
7019 Ne : constant Entity_Id := Entity (N);
7020
7021 begin
7022 Binary_Op_Validity_Checks (N);
7023
7024 Neg :=
7025 Make_Op_Not (Loc,
7026 Right_Opnd =>
7027 Make_Op_Eq (Loc,
7028 Left_Opnd => Left_Opnd (N),
7029 Right_Opnd => Right_Opnd (N)));
7030 Set_Paren_Count (Right_Opnd (Neg), 1);
7031
7032 if Scope (Ne) /= Standard_Standard then
7033 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
7034 end if;
7035
7036 -- For navigation purposes, we want to treat the inequality as an
7037 -- implicit reference to the corresponding equality. Preserve the
7038 -- Comes_From_ source flag to generate proper Xref entries.
7039
7040 Preserve_Comes_From_Source (Neg, N);
7041 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
7042 Rewrite (N, Neg);
7043 Analyze_And_Resolve (N, Standard_Boolean);
7044 end;
7045 end if;
7046
7047 Optimize_Length_Comparison (N);
7048 end Expand_N_Op_Ne;
7049
7050 ---------------------
7051 -- Expand_N_Op_Not --
7052 ---------------------
7053
7054 -- If the argument is other than a Boolean array type, there is no special
7055 -- expansion required, except for VMS operations on signed integers.
7056
7057 -- For the packed case, we call the special routine in Exp_Pakd, except
7058 -- that if the component size is greater than one, we use the standard
7059 -- routine generating a gruesome loop (it is so peculiar to have packed
7060 -- arrays with non-standard Boolean representations anyway, so it does not
7061 -- matter that we do not handle this case efficiently).
7062
7063 -- For the unpacked case (and for the special packed case where we have non
7064 -- standard Booleans, as discussed above), we generate and insert into the
7065 -- tree the following function definition:
7066
7067 -- function Nnnn (A : arr) is
7068 -- B : arr;
7069 -- begin
7070 -- for J in a'range loop
7071 -- B (J) := not A (J);
7072 -- end loop;
7073 -- return B;
7074 -- end Nnnn;
7075
7076 -- Here arr is the actual subtype of the parameter (and hence always
7077 -- constrained). Then we replace the not with a call to this function.
7078
7079 procedure Expand_N_Op_Not (N : Node_Id) is
7080 Loc : constant Source_Ptr := Sloc (N);
7081 Typ : constant Entity_Id := Etype (N);
7082 Opnd : Node_Id;
7083 Arr : Entity_Id;
7084 A : Entity_Id;
7085 B : Entity_Id;
7086 J : Entity_Id;
7087 A_J : Node_Id;
7088 B_J : Node_Id;
7089
7090 Func_Name : Entity_Id;
7091 Loop_Statement : Node_Id;
7092
7093 begin
7094 Unary_Op_Validity_Checks (N);
7095
7096 -- For boolean operand, deal with non-standard booleans
7097
7098 if Is_Boolean_Type (Typ) then
7099 Adjust_Condition (Right_Opnd (N));
7100 Set_Etype (N, Standard_Boolean);
7101 Adjust_Result_Type (N, Typ);
7102 return;
7103 end if;
7104
7105 -- For the VMS "not" on signed integer types, use conversion to and from
7106 -- a predefined modular type.
7107
7108 if Is_VMS_Operator (Entity (N)) then
7109 declare
7110 Rtyp : Entity_Id;
7111 Utyp : Entity_Id;
7112
7113 begin
7114 -- If this is a derived type, retrieve original VMS type so that
7115 -- the proper sized type is used for intermediate values.
7116
7117 if Is_Derived_Type (Typ) then
7118 Rtyp := First_Subtype (Etype (Typ));
7119 else
7120 Rtyp := Typ;
7121 end if;
7122
7123 -- The proper unsigned type must have a size compatible with the
7124 -- operand, to prevent misalignment.
7125
7126 if RM_Size (Rtyp) <= 8 then
7127 Utyp := RTE (RE_Unsigned_8);
7128
7129 elsif RM_Size (Rtyp) <= 16 then
7130 Utyp := RTE (RE_Unsigned_16);
7131
7132 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
7133 Utyp := RTE (RE_Unsigned_32);
7134
7135 else
7136 Utyp := RTE (RE_Long_Long_Unsigned);
7137 end if;
7138
7139 Rewrite (N,
7140 Unchecked_Convert_To (Typ,
7141 Make_Op_Not (Loc,
7142 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
7143 Analyze_And_Resolve (N, Typ);
7144 return;
7145 end;
7146 end if;
7147
7148 -- Only array types need any other processing
7149
7150 if not Is_Array_Type (Typ) then
7151 return;
7152 end if;
7153
7154 -- Case of array operand. If bit packed with a component size of 1,
7155 -- handle it in Exp_Pakd if the operand is known to be aligned.
7156
7157 if Is_Bit_Packed_Array (Typ)
7158 and then Component_Size (Typ) = 1
7159 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
7160 then
7161 Expand_Packed_Not (N);
7162 return;
7163 end if;
7164
7165 -- Case of array operand which is not bit-packed. If the context is
7166 -- a safe assignment, call in-place operation, If context is a larger
7167 -- boolean expression in the context of a safe assignment, expansion is
7168 -- done by enclosing operation.
7169
7170 Opnd := Relocate_Node (Right_Opnd (N));
7171 Convert_To_Actual_Subtype (Opnd);
7172 Arr := Etype (Opnd);
7173 Ensure_Defined (Arr, N);
7174 Silly_Boolean_Array_Not_Test (N, Arr);
7175
7176 if Nkind (Parent (N)) = N_Assignment_Statement then
7177 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
7178 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7179 return;
7180
7181 -- Special case the negation of a binary operation
7182
7183 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
7184 and then Safe_In_Place_Array_Op
7185 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
7186 then
7187 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7188 return;
7189 end if;
7190
7191 elsif Nkind (Parent (N)) in N_Binary_Op
7192 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
7193 then
7194 declare
7195 Op1 : constant Node_Id := Left_Opnd (Parent (N));
7196 Op2 : constant Node_Id := Right_Opnd (Parent (N));
7197 Lhs : constant Node_Id := Name (Parent (Parent (N)));
7198
7199 begin
7200 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
7201
7202 -- (not A) op (not B) can be reduced to a single call
7203
7204 if N = Op1 and then Nkind (Op2) = N_Op_Not then
7205 return;
7206
7207 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
7208 return;
7209
7210 -- A xor (not B) can also be special-cased
7211
7212 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
7213 return;
7214 end if;
7215 end if;
7216 end;
7217 end if;
7218
7219 A := Make_Defining_Identifier (Loc, Name_uA);
7220 B := Make_Defining_Identifier (Loc, Name_uB);
7221 J := Make_Defining_Identifier (Loc, Name_uJ);
7222
7223 A_J :=
7224 Make_Indexed_Component (Loc,
7225 Prefix => New_Reference_To (A, Loc),
7226 Expressions => New_List (New_Reference_To (J, Loc)));
7227
7228 B_J :=
7229 Make_Indexed_Component (Loc,
7230 Prefix => New_Reference_To (B, Loc),
7231 Expressions => New_List (New_Reference_To (J, Loc)));
7232
7233 Loop_Statement :=
7234 Make_Implicit_Loop_Statement (N,
7235 Identifier => Empty,
7236
7237 Iteration_Scheme =>
7238 Make_Iteration_Scheme (Loc,
7239 Loop_Parameter_Specification =>
7240 Make_Loop_Parameter_Specification (Loc,
7241 Defining_Identifier => J,
7242 Discrete_Subtype_Definition =>
7243 Make_Attribute_Reference (Loc,
7244 Prefix => Make_Identifier (Loc, Chars (A)),
7245 Attribute_Name => Name_Range))),
7246
7247 Statements => New_List (
7248 Make_Assignment_Statement (Loc,
7249 Name => B_J,
7250 Expression => Make_Op_Not (Loc, A_J))));
7251
7252 Func_Name := Make_Temporary (Loc, 'N');
7253 Set_Is_Inlined (Func_Name);
7254
7255 Insert_Action (N,
7256 Make_Subprogram_Body (Loc,
7257 Specification =>
7258 Make_Function_Specification (Loc,
7259 Defining_Unit_Name => Func_Name,
7260 Parameter_Specifications => New_List (
7261 Make_Parameter_Specification (Loc,
7262 Defining_Identifier => A,
7263 Parameter_Type => New_Reference_To (Typ, Loc))),
7264 Result_Definition => New_Reference_To (Typ, Loc)),
7265
7266 Declarations => New_List (
7267 Make_Object_Declaration (Loc,
7268 Defining_Identifier => B,
7269 Object_Definition => New_Reference_To (Arr, Loc))),
7270
7271 Handled_Statement_Sequence =>
7272 Make_Handled_Sequence_Of_Statements (Loc,
7273 Statements => New_List (
7274 Loop_Statement,
7275 Make_Simple_Return_Statement (Loc,
7276 Expression => Make_Identifier (Loc, Chars (B)))))));
7277
7278 Rewrite (N,
7279 Make_Function_Call (Loc,
7280 Name => New_Reference_To (Func_Name, Loc),
7281 Parameter_Associations => New_List (Opnd)));
7282
7283 Analyze_And_Resolve (N, Typ);
7284 end Expand_N_Op_Not;
7285
7286 --------------------
7287 -- Expand_N_Op_Or --
7288 --------------------
7289
7290 procedure Expand_N_Op_Or (N : Node_Id) is
7291 Typ : constant Entity_Id := Etype (N);
7292
7293 begin
7294 Binary_Op_Validity_Checks (N);
7295
7296 if Is_Array_Type (Etype (N)) then
7297 Expand_Boolean_Operator (N);
7298
7299 elsif Is_Boolean_Type (Etype (N)) then
7300
7301 -- Replace OR by OR ELSE if Short_Circuit_And_Or active and the type
7302 -- is standard Boolean (do not mess with AND that uses a non-standard
7303 -- Boolean type, because something strange is going on).
7304
7305 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
7306 Rewrite (N,
7307 Make_Or_Else (Sloc (N),
7308 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7309 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7310 Analyze_And_Resolve (N, Typ);
7311
7312 -- Otherwise, adjust conditions
7313
7314 else
7315 Adjust_Condition (Left_Opnd (N));
7316 Adjust_Condition (Right_Opnd (N));
7317 Set_Etype (N, Standard_Boolean);
7318 Adjust_Result_Type (N, Typ);
7319 end if;
7320
7321 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7322 Expand_Intrinsic_Call (N, Entity (N));
7323
7324 end if;
7325 end Expand_N_Op_Or;
7326
7327 ----------------------
7328 -- Expand_N_Op_Plus --
7329 ----------------------
7330
7331 procedure Expand_N_Op_Plus (N : Node_Id) is
7332 begin
7333 Unary_Op_Validity_Checks (N);
7334 end Expand_N_Op_Plus;
7335
7336 ---------------------
7337 -- Expand_N_Op_Rem --
7338 ---------------------
7339
7340 procedure Expand_N_Op_Rem (N : Node_Id) is
7341 Loc : constant Source_Ptr := Sloc (N);
7342 Typ : constant Entity_Id := Etype (N);
7343
7344 Left : constant Node_Id := Left_Opnd (N);
7345 Right : constant Node_Id := Right_Opnd (N);
7346
7347 Lo : Uint;
7348 Hi : Uint;
7349 OK : Boolean;
7350
7351 Lneg : Boolean;
7352 Rneg : Boolean;
7353 -- Set if corresponding operand can be negative
7354
7355 pragma Unreferenced (Hi);
7356
7357 begin
7358 Binary_Op_Validity_Checks (N);
7359
7360 if Is_Integer_Type (Etype (N)) then
7361 Apply_Divide_Check (N);
7362 end if;
7363
7364 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
7365 -- but it is useful with other back ends (e.g. AAMP), and is certainly
7366 -- harmless.
7367
7368 if Is_Integer_Type (Etype (N))
7369 and then Compile_Time_Known_Value (Right)
7370 and then Expr_Value (Right) = Uint_1
7371 then
7372 -- Call Remove_Side_Effects to ensure that any side effects in the
7373 -- ignored left operand (in particular function calls to user defined
7374 -- functions) are properly executed.
7375
7376 Remove_Side_Effects (Left);
7377
7378 Rewrite (N, Make_Integer_Literal (Loc, 0));
7379 Analyze_And_Resolve (N, Typ);
7380 return;
7381 end if;
7382
7383 -- Deal with annoying case of largest negative number remainder minus
7384 -- one. Gigi does not handle this case correctly, because it generates
7385 -- a divide instruction which may trap in this case.
7386
7387 -- In fact the check is quite easy, if the right operand is -1, then
7388 -- the remainder is always 0, and we can just ignore the left operand
7389 -- completely in this case.
7390
7391 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
7392 Lneg := (not OK) or else Lo < 0;
7393
7394 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
7395 Rneg := (not OK) or else Lo < 0;
7396
7397 -- We won't mess with trying to find out if the left operand can really
7398 -- be the largest negative number (that's a pain in the case of private
7399 -- types and this is really marginal). We will just assume that we need
7400 -- the test if the left operand can be negative at all.
7401
7402 if Lneg and Rneg then
7403 Rewrite (N,
7404 Make_Conditional_Expression (Loc,
7405 Expressions => New_List (
7406 Make_Op_Eq (Loc,
7407 Left_Opnd => Duplicate_Subexpr (Right),
7408 Right_Opnd =>
7409 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
7410
7411 Unchecked_Convert_To (Typ,
7412 Make_Integer_Literal (Loc, Uint_0)),
7413
7414 Relocate_Node (N))));
7415
7416 Set_Analyzed (Next (Next (First (Expressions (N)))));
7417 Analyze_And_Resolve (N, Typ);
7418 end if;
7419 end Expand_N_Op_Rem;
7420
7421 -----------------------------
7422 -- Expand_N_Op_Rotate_Left --
7423 -----------------------------
7424
7425 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
7426 begin
7427 Binary_Op_Validity_Checks (N);
7428 end Expand_N_Op_Rotate_Left;
7429
7430 ------------------------------
7431 -- Expand_N_Op_Rotate_Right --
7432 ------------------------------
7433
7434 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
7435 begin
7436 Binary_Op_Validity_Checks (N);
7437 end Expand_N_Op_Rotate_Right;
7438
7439 ----------------------------
7440 -- Expand_N_Op_Shift_Left --
7441 ----------------------------
7442
7443 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
7444 begin
7445 Binary_Op_Validity_Checks (N);
7446 end Expand_N_Op_Shift_Left;
7447
7448 -----------------------------
7449 -- Expand_N_Op_Shift_Right --
7450 -----------------------------
7451
7452 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
7453 begin
7454 Binary_Op_Validity_Checks (N);
7455 end Expand_N_Op_Shift_Right;
7456
7457 ----------------------------------------
7458 -- Expand_N_Op_Shift_Right_Arithmetic --
7459 ----------------------------------------
7460
7461 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
7462 begin
7463 Binary_Op_Validity_Checks (N);
7464 end Expand_N_Op_Shift_Right_Arithmetic;
7465
7466 --------------------------
7467 -- Expand_N_Op_Subtract --
7468 --------------------------
7469
7470 procedure Expand_N_Op_Subtract (N : Node_Id) is
7471 Typ : constant Entity_Id := Etype (N);
7472
7473 begin
7474 Binary_Op_Validity_Checks (N);
7475
7476 -- N - 0 = N for integer types
7477
7478 if Is_Integer_Type (Typ)
7479 and then Compile_Time_Known_Value (Right_Opnd (N))
7480 and then Expr_Value (Right_Opnd (N)) = 0
7481 then
7482 Rewrite (N, Left_Opnd (N));
7483 return;
7484 end if;
7485
7486 -- Arithmetic overflow checks for signed integer/fixed point types
7487
7488 if Is_Signed_Integer_Type (Typ)
7489 or else
7490 Is_Fixed_Point_Type (Typ)
7491 then
7492 Apply_Arithmetic_Overflow_Check (N);
7493
7494 -- VAX floating-point types case
7495
7496 elsif Vax_Float (Typ) then
7497 Expand_Vax_Arith (N);
7498 end if;
7499 end Expand_N_Op_Subtract;
7500
7501 ---------------------
7502 -- Expand_N_Op_Xor --
7503 ---------------------
7504
7505 procedure Expand_N_Op_Xor (N : Node_Id) is
7506 Typ : constant Entity_Id := Etype (N);
7507
7508 begin
7509 Binary_Op_Validity_Checks (N);
7510
7511 if Is_Array_Type (Etype (N)) then
7512 Expand_Boolean_Operator (N);
7513
7514 elsif Is_Boolean_Type (Etype (N)) then
7515 Adjust_Condition (Left_Opnd (N));
7516 Adjust_Condition (Right_Opnd (N));
7517 Set_Etype (N, Standard_Boolean);
7518 Adjust_Result_Type (N, Typ);
7519
7520 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7521 Expand_Intrinsic_Call (N, Entity (N));
7522
7523 end if;
7524 end Expand_N_Op_Xor;
7525
7526 ----------------------
7527 -- Expand_N_Or_Else --
7528 ----------------------
7529
7530 procedure Expand_N_Or_Else (N : Node_Id)
7531 renames Expand_Short_Circuit_Operator;
7532
7533 -----------------------------------
7534 -- Expand_N_Qualified_Expression --
7535 -----------------------------------
7536
7537 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7538 Operand : constant Node_Id := Expression (N);
7539 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7540
7541 begin
7542 -- Do validity check if validity checking operands
7543
7544 if Validity_Checks_On
7545 and then Validity_Check_Operands
7546 then
7547 Ensure_Valid (Operand);
7548 end if;
7549
7550 -- Apply possible constraint check
7551
7552 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7553
7554 if Do_Range_Check (Operand) then
7555 Set_Do_Range_Check (Operand, False);
7556 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
7557 end if;
7558 end Expand_N_Qualified_Expression;
7559
7560 ------------------------------------
7561 -- Expand_N_Quantified_Expression --
7562 ------------------------------------
7563
7564 -- We expand:
7565
7566 -- for all X in range => Cond
7567
7568 -- into:
7569
7570 -- T := True;
7571 -- for X in range loop
7572 -- if not Cond then
7573 -- T := False;
7574 -- exit;
7575 -- end if;
7576 -- end loop;
7577
7578 -- Conversely, an existentially quantified expression:
7579
7580 -- for some X in range => Cond
7581
7582 -- becomes:
7583
7584 -- T := False;
7585 -- for X in range loop
7586 -- if Cond then
7587 -- T := True;
7588 -- exit;
7589 -- end if;
7590 -- end loop;
7591
7592 -- In both cases, the iteration may be over a container in which case it is
7593 -- given by an iterator specification, not a loop parameter specification.
7594
7595 procedure Expand_N_Quantified_Expression (N : Node_Id) is
7596 Loc : constant Source_Ptr := Sloc (N);
7597 Is_Universal : constant Boolean := All_Present (N);
7598 Actions : constant List_Id := New_List;
7599 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7600 Cond : Node_Id;
7601 Decl : Node_Id;
7602 I_Scheme : Node_Id;
7603 Test : Node_Id;
7604
7605 begin
7606 Decl :=
7607 Make_Object_Declaration (Loc,
7608 Defining_Identifier => Tnn,
7609 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7610 Expression =>
7611 New_Occurrence_Of (Boolean_Literals (Is_Universal), Loc));
7612 Append_To (Actions, Decl);
7613
7614 Cond := Relocate_Node (Condition (N));
7615
7616 -- Reset flag analyzed in the condition to force its analysis. Required
7617 -- since the previous analysis was done with expansion disabled (see
7618 -- Resolve_Quantified_Expression) and hence checks were not inserted
7619 -- and record comparisons have not been expanded.
7620
7621 Reset_Analyzed_Flags (Cond);
7622
7623 if Is_Universal then
7624 Cond := Make_Op_Not (Loc, Cond);
7625 end if;
7626
7627 Test :=
7628 Make_Implicit_If_Statement (N,
7629 Condition => Cond,
7630 Then_Statements => New_List (
7631 Make_Assignment_Statement (Loc,
7632 Name => New_Occurrence_Of (Tnn, Loc),
7633 Expression =>
7634 New_Occurrence_Of (Boolean_Literals (not Is_Universal), Loc)),
7635 Make_Exit_Statement (Loc)));
7636
7637 if Present (Loop_Parameter_Specification (N)) then
7638 I_Scheme :=
7639 Make_Iteration_Scheme (Loc,
7640 Loop_Parameter_Specification =>
7641 Loop_Parameter_Specification (N));
7642 else
7643 I_Scheme :=
7644 Make_Iteration_Scheme (Loc,
7645 Iterator_Specification => Iterator_Specification (N));
7646 end if;
7647
7648 Append_To (Actions,
7649 Make_Loop_Statement (Loc,
7650 Iteration_Scheme => I_Scheme,
7651 Statements => New_List (Test),
7652 End_Label => Empty));
7653
7654 -- The components of the scheme have already been analyzed, and the loop
7655 -- parameter declaration has been processed.
7656
7657 Set_Analyzed (Iteration_Scheme (Last (Actions)));
7658
7659 Rewrite (N,
7660 Make_Expression_With_Actions (Loc,
7661 Expression => New_Occurrence_Of (Tnn, Loc),
7662 Actions => Actions));
7663
7664 Analyze_And_Resolve (N, Standard_Boolean);
7665 end Expand_N_Quantified_Expression;
7666
7667 ---------------------------------
7668 -- Expand_N_Selected_Component --
7669 ---------------------------------
7670
7671 -- If the selector is a discriminant of a concurrent object, rewrite the
7672 -- prefix to denote the corresponding record type.
7673
7674 procedure Expand_N_Selected_Component (N : Node_Id) is
7675 Loc : constant Source_Ptr := Sloc (N);
7676 Par : constant Node_Id := Parent (N);
7677 P : constant Node_Id := Prefix (N);
7678 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7679 Disc : Entity_Id;
7680 New_N : Node_Id;
7681 Dcon : Elmt_Id;
7682 Dval : Node_Id;
7683
7684 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7685 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7686 -- unless the context of an assignment can provide size information.
7687 -- Don't we have a general routine that does this???
7688
7689 function Is_Subtype_Declaration return Boolean;
7690 -- The replacement of a discriminant reference by its value is required
7691 -- if this is part of the initialization of an temporary generated by a
7692 -- change of representation. This shows up as the construction of a
7693 -- discriminant constraint for a subtype declared at the same point as
7694 -- the entity in the prefix of the selected component. We recognize this
7695 -- case when the context of the reference is:
7696 -- subtype ST is T(Obj.D);
7697 -- where the entity for Obj comes from source, and ST has the same sloc.
7698
7699 -----------------------
7700 -- In_Left_Hand_Side --
7701 -----------------------
7702
7703 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7704 begin
7705 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7706 and then Comp = Name (Parent (Comp)))
7707 or else (Present (Parent (Comp))
7708 and then Nkind (Parent (Comp)) in N_Subexpr
7709 and then In_Left_Hand_Side (Parent (Comp)));
7710 end In_Left_Hand_Side;
7711
7712 -----------------------------
7713 -- Is_Subtype_Declaration --
7714 -----------------------------
7715
7716 function Is_Subtype_Declaration return Boolean is
7717 Par : constant Node_Id := Parent (N);
7718 begin
7719 return
7720 Nkind (Par) = N_Index_Or_Discriminant_Constraint
7721 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
7722 and then Comes_From_Source (Entity (Prefix (N)))
7723 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
7724 end Is_Subtype_Declaration;
7725
7726 -- Start of processing for Expand_N_Selected_Component
7727
7728 begin
7729 -- Insert explicit dereference if required
7730
7731 if Is_Access_Type (Ptyp) then
7732 Insert_Explicit_Dereference (P);
7733 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7734
7735 if Ekind (Etype (P)) = E_Private_Subtype
7736 and then Is_For_Access_Subtype (Etype (P))
7737 then
7738 Set_Etype (P, Base_Type (Etype (P)));
7739 end if;
7740
7741 Ptyp := Etype (P);
7742 end if;
7743
7744 -- Deal with discriminant check required
7745
7746 if Do_Discriminant_Check (N) then
7747
7748 -- Present the discriminant checking function to the backend, so that
7749 -- it can inline the call to the function.
7750
7751 Add_Inlined_Body
7752 (Discriminant_Checking_Func
7753 (Original_Record_Component (Entity (Selector_Name (N)))));
7754
7755 -- Now reset the flag and generate the call
7756
7757 Set_Do_Discriminant_Check (N, False);
7758 Generate_Discriminant_Check (N);
7759 end if;
7760
7761 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7762 -- function, then additional actuals must be passed.
7763
7764 if Ada_Version >= Ada_2005
7765 and then Is_Build_In_Place_Function_Call (P)
7766 then
7767 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7768 end if;
7769
7770 -- Gigi cannot handle unchecked conversions that are the prefix of a
7771 -- selected component with discriminants. This must be checked during
7772 -- expansion, because during analysis the type of the selector is not
7773 -- known at the point the prefix is analyzed. If the conversion is the
7774 -- target of an assignment, then we cannot force the evaluation.
7775
7776 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7777 and then Has_Discriminants (Etype (N))
7778 and then not In_Left_Hand_Side (N)
7779 then
7780 Force_Evaluation (Prefix (N));
7781 end if;
7782
7783 -- Remaining processing applies only if selector is a discriminant
7784
7785 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7786
7787 -- If the selector is a discriminant of a constrained record type,
7788 -- we may be able to rewrite the expression with the actual value
7789 -- of the discriminant, a useful optimization in some cases.
7790
7791 if Is_Record_Type (Ptyp)
7792 and then Has_Discriminants (Ptyp)
7793 and then Is_Constrained (Ptyp)
7794 then
7795 -- Do this optimization for discrete types only, and not for
7796 -- access types (access discriminants get us into trouble!)
7797
7798 if not Is_Discrete_Type (Etype (N)) then
7799 null;
7800
7801 -- Don't do this on the left hand of an assignment statement.
7802 -- Normally one would think that references like this would not
7803 -- occur, but they do in generated code, and mean that we really
7804 -- do want to assign the discriminant!
7805
7806 elsif Nkind (Par) = N_Assignment_Statement
7807 and then Name (Par) = N
7808 then
7809 null;
7810
7811 -- Don't do this optimization for the prefix of an attribute or
7812 -- the name of an object renaming declaration since these are
7813 -- contexts where we do not want the value anyway.
7814
7815 elsif (Nkind (Par) = N_Attribute_Reference
7816 and then Prefix (Par) = N)
7817 or else Is_Renamed_Object (N)
7818 then
7819 null;
7820
7821 -- Don't do this optimization if we are within the code for a
7822 -- discriminant check, since the whole point of such a check may
7823 -- be to verify the condition on which the code below depends!
7824
7825 elsif Is_In_Discriminant_Check (N) then
7826 null;
7827
7828 -- Green light to see if we can do the optimization. There is
7829 -- still one condition that inhibits the optimization below but
7830 -- now is the time to check the particular discriminant.
7831
7832 else
7833 -- Loop through discriminants to find the matching discriminant
7834 -- constraint to see if we can copy it.
7835
7836 Disc := First_Discriminant (Ptyp);
7837 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7838 Discr_Loop : while Present (Dcon) loop
7839 Dval := Node (Dcon);
7840
7841 -- Check if this is the matching discriminant and if the
7842 -- discriminant value is simple enough to make sense to
7843 -- copy. We don't want to copy complex expressions, and
7844 -- indeed to do so can cause trouble (before we put in
7845 -- this guard, a discriminant expression containing an
7846 -- AND THEN was copied, causing problems for coverage
7847 -- analysis tools).
7848
7849 -- However, if the reference is part of the initialization
7850 -- code generated for an object declaration, we must use
7851 -- the discriminant value from the subtype constraint,
7852 -- because the selected component may be a reference to the
7853 -- object being initialized, whose discriminant is not yet
7854 -- set. This only happens in complex cases involving changes
7855 -- or representation.
7856
7857 if Disc = Entity (Selector_Name (N))
7858 and then (Is_Entity_Name (Dval)
7859 or else Compile_Time_Known_Value (Dval)
7860 or else Is_Subtype_Declaration)
7861 then
7862 -- Here we have the matching discriminant. Check for
7863 -- the case of a discriminant of a component that is
7864 -- constrained by an outer discriminant, which cannot
7865 -- be optimized away.
7866
7867 if Denotes_Discriminant
7868 (Dval, Check_Concurrent => True)
7869 then
7870 exit Discr_Loop;
7871
7872 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
7873 and then
7874 Denotes_Discriminant
7875 (Selector_Name (Original_Node (Dval)), True)
7876 then
7877 exit Discr_Loop;
7878
7879 -- Do not retrieve value if constraint is not static. It
7880 -- is generally not useful, and the constraint may be a
7881 -- rewritten outer discriminant in which case it is in
7882 -- fact incorrect.
7883
7884 elsif Is_Entity_Name (Dval)
7885 and then Nkind (Parent (Entity (Dval))) =
7886 N_Object_Declaration
7887 and then Present (Expression (Parent (Entity (Dval))))
7888 and then
7889 not Is_Static_Expression
7890 (Expression (Parent (Entity (Dval))))
7891 then
7892 exit Discr_Loop;
7893
7894 -- In the context of a case statement, the expression may
7895 -- have the base type of the discriminant, and we need to
7896 -- preserve the constraint to avoid spurious errors on
7897 -- missing cases.
7898
7899 elsif Nkind (Parent (N)) = N_Case_Statement
7900 and then Etype (Dval) /= Etype (Disc)
7901 then
7902 Rewrite (N,
7903 Make_Qualified_Expression (Loc,
7904 Subtype_Mark =>
7905 New_Occurrence_Of (Etype (Disc), Loc),
7906 Expression =>
7907 New_Copy_Tree (Dval)));
7908 Analyze_And_Resolve (N, Etype (Disc));
7909
7910 -- In case that comes out as a static expression,
7911 -- reset it (a selected component is never static).
7912
7913 Set_Is_Static_Expression (N, False);
7914 return;
7915
7916 -- Otherwise we can just copy the constraint, but the
7917 -- result is certainly not static! In some cases the
7918 -- discriminant constraint has been analyzed in the
7919 -- context of the original subtype indication, but for
7920 -- itypes the constraint might not have been analyzed
7921 -- yet, and this must be done now.
7922
7923 else
7924 Rewrite (N, New_Copy_Tree (Dval));
7925 Analyze_And_Resolve (N);
7926 Set_Is_Static_Expression (N, False);
7927 return;
7928 end if;
7929 end if;
7930
7931 Next_Elmt (Dcon);
7932 Next_Discriminant (Disc);
7933 end loop Discr_Loop;
7934
7935 -- Note: the above loop should always find a matching
7936 -- discriminant, but if it does not, we just missed an
7937 -- optimization due to some glitch (perhaps a previous
7938 -- error), so ignore.
7939
7940 end if;
7941 end if;
7942
7943 -- The only remaining processing is in the case of a discriminant of
7944 -- a concurrent object, where we rewrite the prefix to denote the
7945 -- corresponding record type. If the type is derived and has renamed
7946 -- discriminants, use corresponding discriminant, which is the one
7947 -- that appears in the corresponding record.
7948
7949 if not Is_Concurrent_Type (Ptyp) then
7950 return;
7951 end if;
7952
7953 Disc := Entity (Selector_Name (N));
7954
7955 if Is_Derived_Type (Ptyp)
7956 and then Present (Corresponding_Discriminant (Disc))
7957 then
7958 Disc := Corresponding_Discriminant (Disc);
7959 end if;
7960
7961 New_N :=
7962 Make_Selected_Component (Loc,
7963 Prefix =>
7964 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7965 New_Copy_Tree (P)),
7966 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7967
7968 Rewrite (N, New_N);
7969 Analyze (N);
7970 end if;
7971 end Expand_N_Selected_Component;
7972
7973 --------------------
7974 -- Expand_N_Slice --
7975 --------------------
7976
7977 procedure Expand_N_Slice (N : Node_Id) is
7978 Loc : constant Source_Ptr := Sloc (N);
7979 Typ : constant Entity_Id := Etype (N);
7980 Pfx : constant Node_Id := Prefix (N);
7981 Ptp : Entity_Id := Etype (Pfx);
7982
7983 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7984 -- Check whether the argument is an actual for a procedure call, in
7985 -- which case the expansion of a bit-packed slice is deferred until the
7986 -- call itself is expanded. The reason this is required is that we might
7987 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7988 -- that copy out would be missed if we created a temporary here in
7989 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7990 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7991 -- is harmless to defer expansion in the IN case, since the call
7992 -- processing will still generate the appropriate copy in operation,
7993 -- which will take care of the slice.
7994
7995 procedure Make_Temporary_For_Slice;
7996 -- Create a named variable for the value of the slice, in cases where
7997 -- the back-end cannot handle it properly, e.g. when packed types or
7998 -- unaligned slices are involved.
7999
8000 -------------------------
8001 -- Is_Procedure_Actual --
8002 -------------------------
8003
8004 function Is_Procedure_Actual (N : Node_Id) return Boolean is
8005 Par : Node_Id := Parent (N);
8006
8007 begin
8008 loop
8009 -- If our parent is a procedure call we can return
8010
8011 if Nkind (Par) = N_Procedure_Call_Statement then
8012 return True;
8013
8014 -- If our parent is a type conversion, keep climbing the tree,
8015 -- since a type conversion can be a procedure actual. Also keep
8016 -- climbing if parameter association or a qualified expression,
8017 -- since these are additional cases that do can appear on
8018 -- procedure actuals.
8019
8020 elsif Nkind_In (Par, N_Type_Conversion,
8021 N_Parameter_Association,
8022 N_Qualified_Expression)
8023 then
8024 Par := Parent (Par);
8025
8026 -- Any other case is not what we are looking for
8027
8028 else
8029 return False;
8030 end if;
8031 end loop;
8032 end Is_Procedure_Actual;
8033
8034 ------------------------------
8035 -- Make_Temporary_For_Slice --
8036 ------------------------------
8037
8038 procedure Make_Temporary_For_Slice is
8039 Decl : Node_Id;
8040 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
8041
8042 begin
8043 Decl :=
8044 Make_Object_Declaration (Loc,
8045 Defining_Identifier => Ent,
8046 Object_Definition => New_Occurrence_Of (Typ, Loc));
8047
8048 Set_No_Initialization (Decl);
8049
8050 Insert_Actions (N, New_List (
8051 Decl,
8052 Make_Assignment_Statement (Loc,
8053 Name => New_Occurrence_Of (Ent, Loc),
8054 Expression => Relocate_Node (N))));
8055
8056 Rewrite (N, New_Occurrence_Of (Ent, Loc));
8057 Analyze_And_Resolve (N, Typ);
8058 end Make_Temporary_For_Slice;
8059
8060 -- Start of processing for Expand_N_Slice
8061
8062 begin
8063 -- Special handling for access types
8064
8065 if Is_Access_Type (Ptp) then
8066
8067 Ptp := Designated_Type (Ptp);
8068
8069 Rewrite (Pfx,
8070 Make_Explicit_Dereference (Sloc (N),
8071 Prefix => Relocate_Node (Pfx)));
8072
8073 Analyze_And_Resolve (Pfx, Ptp);
8074 end if;
8075
8076 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
8077 -- function, then additional actuals must be passed.
8078
8079 if Ada_Version >= Ada_2005
8080 and then Is_Build_In_Place_Function_Call (Pfx)
8081 then
8082 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
8083 end if;
8084
8085 -- The remaining case to be handled is packed slices. We can leave
8086 -- packed slices as they are in the following situations:
8087
8088 -- 1. Right or left side of an assignment (we can handle this
8089 -- situation correctly in the assignment statement expansion).
8090
8091 -- 2. Prefix of indexed component (the slide is optimized away in this
8092 -- case, see the start of Expand_N_Slice.)
8093
8094 -- 3. Object renaming declaration, since we want the name of the
8095 -- slice, not the value.
8096
8097 -- 4. Argument to procedure call, since copy-in/copy-out handling may
8098 -- be required, and this is handled in the expansion of call
8099 -- itself.
8100
8101 -- 5. Prefix of an address attribute (this is an error which is caught
8102 -- elsewhere, and the expansion would interfere with generating the
8103 -- error message).
8104
8105 if not Is_Packed (Typ) then
8106
8107 -- Apply transformation for actuals of a function call, where
8108 -- Expand_Actuals is not used.
8109
8110 if Nkind (Parent (N)) = N_Function_Call
8111 and then Is_Possibly_Unaligned_Slice (N)
8112 then
8113 Make_Temporary_For_Slice;
8114 end if;
8115
8116 elsif Nkind (Parent (N)) = N_Assignment_Statement
8117 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
8118 and then Parent (N) = Name (Parent (Parent (N))))
8119 then
8120 return;
8121
8122 elsif Nkind (Parent (N)) = N_Indexed_Component
8123 or else Is_Renamed_Object (N)
8124 or else Is_Procedure_Actual (N)
8125 then
8126 return;
8127
8128 elsif Nkind (Parent (N)) = N_Attribute_Reference
8129 and then Attribute_Name (Parent (N)) = Name_Address
8130 then
8131 return;
8132
8133 else
8134 Make_Temporary_For_Slice;
8135 end if;
8136 end Expand_N_Slice;
8137
8138 ------------------------------
8139 -- Expand_N_Type_Conversion --
8140 ------------------------------
8141
8142 procedure Expand_N_Type_Conversion (N : Node_Id) is
8143 Loc : constant Source_Ptr := Sloc (N);
8144 Operand : constant Node_Id := Expression (N);
8145 Target_Type : constant Entity_Id := Etype (N);
8146 Operand_Type : Entity_Id := Etype (Operand);
8147
8148 procedure Handle_Changed_Representation;
8149 -- This is called in the case of record and array type conversions to
8150 -- see if there is a change of representation to be handled. Change of
8151 -- representation is actually handled at the assignment statement level,
8152 -- and what this procedure does is rewrite node N conversion as an
8153 -- assignment to temporary. If there is no change of representation,
8154 -- then the conversion node is unchanged.
8155
8156 procedure Raise_Accessibility_Error;
8157 -- Called when we know that an accessibility check will fail. Rewrites
8158 -- node N to an appropriate raise statement and outputs warning msgs.
8159 -- The Etype of the raise node is set to Target_Type.
8160
8161 procedure Real_Range_Check;
8162 -- Handles generation of range check for real target value
8163
8164 -----------------------------------
8165 -- Handle_Changed_Representation --
8166 -----------------------------------
8167
8168 procedure Handle_Changed_Representation is
8169 Temp : Entity_Id;
8170 Decl : Node_Id;
8171 Odef : Node_Id;
8172 Disc : Node_Id;
8173 N_Ix : Node_Id;
8174 Cons : List_Id;
8175
8176 begin
8177 -- Nothing else to do if no change of representation
8178
8179 if Same_Representation (Operand_Type, Target_Type) then
8180 return;
8181
8182 -- The real change of representation work is done by the assignment
8183 -- statement processing. So if this type conversion is appearing as
8184 -- the expression of an assignment statement, nothing needs to be
8185 -- done to the conversion.
8186
8187 elsif Nkind (Parent (N)) = N_Assignment_Statement then
8188 return;
8189
8190 -- Otherwise we need to generate a temporary variable, and do the
8191 -- change of representation assignment into that temporary variable.
8192 -- The conversion is then replaced by a reference to this variable.
8193
8194 else
8195 Cons := No_List;
8196
8197 -- If type is unconstrained we have to add a constraint, copied
8198 -- from the actual value of the left hand side.
8199
8200 if not Is_Constrained (Target_Type) then
8201 if Has_Discriminants (Operand_Type) then
8202 Disc := First_Discriminant (Operand_Type);
8203
8204 if Disc /= First_Stored_Discriminant (Operand_Type) then
8205 Disc := First_Stored_Discriminant (Operand_Type);
8206 end if;
8207
8208 Cons := New_List;
8209 while Present (Disc) loop
8210 Append_To (Cons,
8211 Make_Selected_Component (Loc,
8212 Prefix =>
8213 Duplicate_Subexpr_Move_Checks (Operand),
8214 Selector_Name =>
8215 Make_Identifier (Loc, Chars (Disc))));
8216 Next_Discriminant (Disc);
8217 end loop;
8218
8219 elsif Is_Array_Type (Operand_Type) then
8220 N_Ix := First_Index (Target_Type);
8221 Cons := New_List;
8222
8223 for J in 1 .. Number_Dimensions (Operand_Type) loop
8224
8225 -- We convert the bounds explicitly. We use an unchecked
8226 -- conversion because bounds checks are done elsewhere.
8227
8228 Append_To (Cons,
8229 Make_Range (Loc,
8230 Low_Bound =>
8231 Unchecked_Convert_To (Etype (N_Ix),
8232 Make_Attribute_Reference (Loc,
8233 Prefix =>
8234 Duplicate_Subexpr_No_Checks
8235 (Operand, Name_Req => True),
8236 Attribute_Name => Name_First,
8237 Expressions => New_List (
8238 Make_Integer_Literal (Loc, J)))),
8239
8240 High_Bound =>
8241 Unchecked_Convert_To (Etype (N_Ix),
8242 Make_Attribute_Reference (Loc,
8243 Prefix =>
8244 Duplicate_Subexpr_No_Checks
8245 (Operand, Name_Req => True),
8246 Attribute_Name => Name_Last,
8247 Expressions => New_List (
8248 Make_Integer_Literal (Loc, J))))));
8249
8250 Next_Index (N_Ix);
8251 end loop;
8252 end if;
8253 end if;
8254
8255 Odef := New_Occurrence_Of (Target_Type, Loc);
8256
8257 if Present (Cons) then
8258 Odef :=
8259 Make_Subtype_Indication (Loc,
8260 Subtype_Mark => Odef,
8261 Constraint =>
8262 Make_Index_Or_Discriminant_Constraint (Loc,
8263 Constraints => Cons));
8264 end if;
8265
8266 Temp := Make_Temporary (Loc, 'C');
8267 Decl :=
8268 Make_Object_Declaration (Loc,
8269 Defining_Identifier => Temp,
8270 Object_Definition => Odef);
8271
8272 Set_No_Initialization (Decl, True);
8273
8274 -- Insert required actions. It is essential to suppress checks
8275 -- since we have suppressed default initialization, which means
8276 -- that the variable we create may have no discriminants.
8277
8278 Insert_Actions (N,
8279 New_List (
8280 Decl,
8281 Make_Assignment_Statement (Loc,
8282 Name => New_Occurrence_Of (Temp, Loc),
8283 Expression => Relocate_Node (N))),
8284 Suppress => All_Checks);
8285
8286 Rewrite (N, New_Occurrence_Of (Temp, Loc));
8287 return;
8288 end if;
8289 end Handle_Changed_Representation;
8290
8291 -------------------------------
8292 -- Raise_Accessibility_Error --
8293 -------------------------------
8294
8295 procedure Raise_Accessibility_Error is
8296 begin
8297 Rewrite (N,
8298 Make_Raise_Program_Error (Sloc (N),
8299 Reason => PE_Accessibility_Check_Failed));
8300 Set_Etype (N, Target_Type);
8301
8302 Error_Msg_N ("?accessibility check failure", N);
8303 Error_Msg_NE
8304 ("\?& will be raised at run time", N, Standard_Program_Error);
8305 end Raise_Accessibility_Error;
8306
8307 ----------------------
8308 -- Real_Range_Check --
8309 ----------------------
8310
8311 -- Case of conversions to floating-point or fixed-point. If range checks
8312 -- are enabled and the target type has a range constraint, we convert:
8313
8314 -- typ (x)
8315
8316 -- to
8317
8318 -- Tnn : typ'Base := typ'Base (x);
8319 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
8320 -- Tnn
8321
8322 -- This is necessary when there is a conversion of integer to float or
8323 -- to fixed-point to ensure that the correct checks are made. It is not
8324 -- necessary for float to float where it is enough to simply set the
8325 -- Do_Range_Check flag.
8326
8327 procedure Real_Range_Check is
8328 Btyp : constant Entity_Id := Base_Type (Target_Type);
8329 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
8330 Hi : constant Node_Id := Type_High_Bound (Target_Type);
8331 Xtyp : constant Entity_Id := Etype (Operand);
8332 Conv : Node_Id;
8333 Tnn : Entity_Id;
8334
8335 begin
8336 -- Nothing to do if conversion was rewritten
8337
8338 if Nkind (N) /= N_Type_Conversion then
8339 return;
8340 end if;
8341
8342 -- Nothing to do if range checks suppressed, or target has the same
8343 -- range as the base type (or is the base type).
8344
8345 if Range_Checks_Suppressed (Target_Type)
8346 or else (Lo = Type_Low_Bound (Btyp)
8347 and then
8348 Hi = Type_High_Bound (Btyp))
8349 then
8350 return;
8351 end if;
8352
8353 -- Nothing to do if expression is an entity on which checks have been
8354 -- suppressed.
8355
8356 if Is_Entity_Name (Operand)
8357 and then Range_Checks_Suppressed (Entity (Operand))
8358 then
8359 return;
8360 end if;
8361
8362 -- Nothing to do if bounds are all static and we can tell that the
8363 -- expression is within the bounds of the target. Note that if the
8364 -- operand is of an unconstrained floating-point type, then we do
8365 -- not trust it to be in range (might be infinite)
8366
8367 declare
8368 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
8369 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
8370
8371 begin
8372 if (not Is_Floating_Point_Type (Xtyp)
8373 or else Is_Constrained (Xtyp))
8374 and then Compile_Time_Known_Value (S_Lo)
8375 and then Compile_Time_Known_Value (S_Hi)
8376 and then Compile_Time_Known_Value (Hi)
8377 and then Compile_Time_Known_Value (Lo)
8378 then
8379 declare
8380 D_Lov : constant Ureal := Expr_Value_R (Lo);
8381 D_Hiv : constant Ureal := Expr_Value_R (Hi);
8382 S_Lov : Ureal;
8383 S_Hiv : Ureal;
8384
8385 begin
8386 if Is_Real_Type (Xtyp) then
8387 S_Lov := Expr_Value_R (S_Lo);
8388 S_Hiv := Expr_Value_R (S_Hi);
8389 else
8390 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
8391 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
8392 end if;
8393
8394 if D_Hiv > D_Lov
8395 and then S_Lov >= D_Lov
8396 and then S_Hiv <= D_Hiv
8397 then
8398 Set_Do_Range_Check (Operand, False);
8399 return;
8400 end if;
8401 end;
8402 end if;
8403 end;
8404
8405 -- For float to float conversions, we are done
8406
8407 if Is_Floating_Point_Type (Xtyp)
8408 and then
8409 Is_Floating_Point_Type (Btyp)
8410 then
8411 return;
8412 end if;
8413
8414 -- Otherwise rewrite the conversion as described above
8415
8416 Conv := Relocate_Node (N);
8417 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
8418 Set_Etype (Conv, Btyp);
8419
8420 -- Enable overflow except for case of integer to float conversions,
8421 -- where it is never required, since we can never have overflow in
8422 -- this case.
8423
8424 if not Is_Integer_Type (Etype (Operand)) then
8425 Enable_Overflow_Check (Conv);
8426 end if;
8427
8428 Tnn := Make_Temporary (Loc, 'T', Conv);
8429
8430 Insert_Actions (N, New_List (
8431 Make_Object_Declaration (Loc,
8432 Defining_Identifier => Tnn,
8433 Object_Definition => New_Occurrence_Of (Btyp, Loc),
8434 Constant_Present => True,
8435 Expression => Conv),
8436
8437 Make_Raise_Constraint_Error (Loc,
8438 Condition =>
8439 Make_Or_Else (Loc,
8440 Left_Opnd =>
8441 Make_Op_Lt (Loc,
8442 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8443 Right_Opnd =>
8444 Make_Attribute_Reference (Loc,
8445 Attribute_Name => Name_First,
8446 Prefix =>
8447 New_Occurrence_Of (Target_Type, Loc))),
8448
8449 Right_Opnd =>
8450 Make_Op_Gt (Loc,
8451 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8452 Right_Opnd =>
8453 Make_Attribute_Reference (Loc,
8454 Attribute_Name => Name_Last,
8455 Prefix =>
8456 New_Occurrence_Of (Target_Type, Loc)))),
8457 Reason => CE_Range_Check_Failed)));
8458
8459 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
8460 Analyze_And_Resolve (N, Btyp);
8461 end Real_Range_Check;
8462
8463 -- Start of processing for Expand_N_Type_Conversion
8464
8465 begin
8466 -- Nothing at all to do if conversion is to the identical type so remove
8467 -- the conversion completely, it is useless, except that it may carry
8468 -- an Assignment_OK attribute, which must be propagated to the operand.
8469
8470 if Operand_Type = Target_Type then
8471 if Assignment_OK (N) then
8472 Set_Assignment_OK (Operand);
8473 end if;
8474
8475 Rewrite (N, Relocate_Node (Operand));
8476 goto Done;
8477 end if;
8478
8479 -- Nothing to do if this is the second argument of read. This is a
8480 -- "backwards" conversion that will be handled by the specialized code
8481 -- in attribute processing.
8482
8483 if Nkind (Parent (N)) = N_Attribute_Reference
8484 and then Attribute_Name (Parent (N)) = Name_Read
8485 and then Next (First (Expressions (Parent (N)))) = N
8486 then
8487 goto Done;
8488 end if;
8489
8490 -- Check for case of converting to a type that has an invariant
8491 -- associated with it. This required an invariant check. We convert
8492
8493 -- typ (expr)
8494
8495 -- into
8496
8497 -- do invariant_check (typ (expr)) in typ (expr);
8498
8499 -- using Duplicate_Subexpr to avoid multiple side effects
8500
8501 -- Note: the Comes_From_Source check, and then the resetting of this
8502 -- flag prevents what would otherwise be an infinite recursion.
8503
8504 if Has_Invariants (Target_Type)
8505 and then Present (Invariant_Procedure (Target_Type))
8506 and then Comes_From_Source (N)
8507 then
8508 Set_Comes_From_Source (N, False);
8509 Rewrite (N,
8510 Make_Expression_With_Actions (Loc,
8511 Actions => New_List (
8512 Make_Invariant_Call (Duplicate_Subexpr (N))),
8513 Expression => Duplicate_Subexpr_No_Checks (N)));
8514 Analyze_And_Resolve (N, Target_Type);
8515 goto Done;
8516 end if;
8517
8518 -- Here if we may need to expand conversion
8519
8520 -- If the operand of the type conversion is an arithmetic operation on
8521 -- signed integers, and the based type of the signed integer type in
8522 -- question is smaller than Standard.Integer, we promote both of the
8523 -- operands to type Integer.
8524
8525 -- For example, if we have
8526
8527 -- target-type (opnd1 + opnd2)
8528
8529 -- and opnd1 and opnd2 are of type short integer, then we rewrite
8530 -- this as:
8531
8532 -- target-type (integer(opnd1) + integer(opnd2))
8533
8534 -- We do this because we are always allowed to compute in a larger type
8535 -- if we do the right thing with the result, and in this case we are
8536 -- going to do a conversion which will do an appropriate check to make
8537 -- sure that things are in range of the target type in any case. This
8538 -- avoids some unnecessary intermediate overflows.
8539
8540 -- We might consider a similar transformation in the case where the
8541 -- target is a real type or a 64-bit integer type, and the operand
8542 -- is an arithmetic operation using a 32-bit integer type. However,
8543 -- we do not bother with this case, because it could cause significant
8544 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
8545 -- much cheaper, but we don't want different behavior on 32-bit and
8546 -- 64-bit machines. Note that the exclusion of the 64-bit case also
8547 -- handles the configurable run-time cases where 64-bit arithmetic
8548 -- may simply be unavailable.
8549
8550 -- Note: this circuit is partially redundant with respect to the circuit
8551 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
8552 -- the processing here. Also we still need the Checks circuit, since we
8553 -- have to be sure not to generate junk overflow checks in the first
8554 -- place, since it would be trick to remove them here!
8555
8556 if Integer_Promotion_Possible (N) then
8557
8558 -- All conditions met, go ahead with transformation
8559
8560 declare
8561 Opnd : Node_Id;
8562 L, R : Node_Id;
8563
8564 begin
8565 R :=
8566 Make_Type_Conversion (Loc,
8567 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8568 Expression => Relocate_Node (Right_Opnd (Operand)));
8569
8570 Opnd := New_Op_Node (Nkind (Operand), Loc);
8571 Set_Right_Opnd (Opnd, R);
8572
8573 if Nkind (Operand) in N_Binary_Op then
8574 L :=
8575 Make_Type_Conversion (Loc,
8576 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8577 Expression => Relocate_Node (Left_Opnd (Operand)));
8578
8579 Set_Left_Opnd (Opnd, L);
8580 end if;
8581
8582 Rewrite (N,
8583 Make_Type_Conversion (Loc,
8584 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
8585 Expression => Opnd));
8586
8587 Analyze_And_Resolve (N, Target_Type);
8588 goto Done;
8589 end;
8590 end if;
8591
8592 -- Do validity check if validity checking operands
8593
8594 if Validity_Checks_On
8595 and then Validity_Check_Operands
8596 then
8597 Ensure_Valid (Operand);
8598 end if;
8599
8600 -- Special case of converting from non-standard boolean type
8601
8602 if Is_Boolean_Type (Operand_Type)
8603 and then (Nonzero_Is_True (Operand_Type))
8604 then
8605 Adjust_Condition (Operand);
8606 Set_Etype (Operand, Standard_Boolean);
8607 Operand_Type := Standard_Boolean;
8608 end if;
8609
8610 -- Case of converting to an access type
8611
8612 if Is_Access_Type (Target_Type) then
8613
8614 -- Apply an accessibility check when the conversion operand is an
8615 -- access parameter (or a renaming thereof), unless conversion was
8616 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
8617 -- Note that other checks may still need to be applied below (such
8618 -- as tagged type checks).
8619
8620 if Is_Entity_Name (Operand)
8621 and then
8622 (Is_Formal (Entity (Operand))
8623 or else
8624 (Present (Renamed_Object (Entity (Operand)))
8625 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
8626 and then Is_Formal
8627 (Entity (Renamed_Object (Entity (Operand))))))
8628 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
8629 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
8630 or else Attribute_Name (Original_Node (N)) = Name_Access)
8631 then
8632 Apply_Accessibility_Check
8633 (Operand, Target_Type, Insert_Node => Operand);
8634
8635 -- If the level of the operand type is statically deeper than the
8636 -- level of the target type, then force Program_Error. Note that this
8637 -- can only occur for cases where the attribute is within the body of
8638 -- an instantiation (otherwise the conversion will already have been
8639 -- rejected as illegal). Note: warnings are issued by the analyzer
8640 -- for the instance cases.
8641
8642 elsif In_Instance_Body
8643 and then Type_Access_Level (Operand_Type) >
8644 Type_Access_Level (Target_Type)
8645 then
8646 Raise_Accessibility_Error;
8647
8648 -- When the operand is a selected access discriminant the check needs
8649 -- to be made against the level of the object denoted by the prefix
8650 -- of the selected name. Force Program_Error for this case as well
8651 -- (this accessibility violation can only happen if within the body
8652 -- of an instantiation).
8653
8654 elsif In_Instance_Body
8655 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
8656 and then Nkind (Operand) = N_Selected_Component
8657 and then Object_Access_Level (Operand) >
8658 Type_Access_Level (Target_Type)
8659 then
8660 Raise_Accessibility_Error;
8661 goto Done;
8662 end if;
8663 end if;
8664
8665 -- Case of conversions of tagged types and access to tagged types
8666
8667 -- When needed, that is to say when the expression is class-wide, Add
8668 -- runtime a tag check for (strict) downward conversion by using the
8669 -- membership test, generating:
8670
8671 -- [constraint_error when Operand not in Target_Type'Class]
8672
8673 -- or in the access type case
8674
8675 -- [constraint_error
8676 -- when Operand /= null
8677 -- and then Operand.all not in
8678 -- Designated_Type (Target_Type)'Class]
8679
8680 if (Is_Access_Type (Target_Type)
8681 and then Is_Tagged_Type (Designated_Type (Target_Type)))
8682 or else Is_Tagged_Type (Target_Type)
8683 then
8684 -- Do not do any expansion in the access type case if the parent is a
8685 -- renaming, since this is an error situation which will be caught by
8686 -- Sem_Ch8, and the expansion can interfere with this error check.
8687
8688 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
8689 goto Done;
8690 end if;
8691
8692 -- Otherwise, proceed with processing tagged conversion
8693
8694 Tagged_Conversion : declare
8695 Actual_Op_Typ : Entity_Id;
8696 Actual_Targ_Typ : Entity_Id;
8697 Make_Conversion : Boolean := False;
8698 Root_Op_Typ : Entity_Id;
8699
8700 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
8701 -- Create a membership check to test whether Operand is a member
8702 -- of Targ_Typ. If the original Target_Type is an access, include
8703 -- a test for null value. The check is inserted at N.
8704
8705 --------------------
8706 -- Make_Tag_Check --
8707 --------------------
8708
8709 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
8710 Cond : Node_Id;
8711
8712 begin
8713 -- Generate:
8714 -- [Constraint_Error
8715 -- when Operand /= null
8716 -- and then Operand.all not in Targ_Typ]
8717
8718 if Is_Access_Type (Target_Type) then
8719 Cond :=
8720 Make_And_Then (Loc,
8721 Left_Opnd =>
8722 Make_Op_Ne (Loc,
8723 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8724 Right_Opnd => Make_Null (Loc)),
8725
8726 Right_Opnd =>
8727 Make_Not_In (Loc,
8728 Left_Opnd =>
8729 Make_Explicit_Dereference (Loc,
8730 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
8731 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
8732
8733 -- Generate:
8734 -- [Constraint_Error when Operand not in Targ_Typ]
8735
8736 else
8737 Cond :=
8738 Make_Not_In (Loc,
8739 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8740 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
8741 end if;
8742
8743 Insert_Action (N,
8744 Make_Raise_Constraint_Error (Loc,
8745 Condition => Cond,
8746 Reason => CE_Tag_Check_Failed));
8747 end Make_Tag_Check;
8748
8749 -- Start of processing for Tagged_Conversion
8750
8751 begin
8752 -- Handle entities from the limited view
8753
8754 if Is_Access_Type (Operand_Type) then
8755 Actual_Op_Typ :=
8756 Available_View (Designated_Type (Operand_Type));
8757 else
8758 Actual_Op_Typ := Operand_Type;
8759 end if;
8760
8761 if Is_Access_Type (Target_Type) then
8762 Actual_Targ_Typ :=
8763 Available_View (Designated_Type (Target_Type));
8764 else
8765 Actual_Targ_Typ := Target_Type;
8766 end if;
8767
8768 Root_Op_Typ := Root_Type (Actual_Op_Typ);
8769
8770 -- Ada 2005 (AI-251): Handle interface type conversion
8771
8772 if Is_Interface (Actual_Op_Typ) then
8773 Expand_Interface_Conversion (N, Is_Static => False);
8774 goto Done;
8775 end if;
8776
8777 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
8778
8779 -- Create a runtime tag check for a downward class-wide type
8780 -- conversion.
8781
8782 if Is_Class_Wide_Type (Actual_Op_Typ)
8783 and then Actual_Op_Typ /= Actual_Targ_Typ
8784 and then Root_Op_Typ /= Actual_Targ_Typ
8785 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
8786 Use_Full_View => True)
8787 then
8788 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
8789 Make_Conversion := True;
8790 end if;
8791
8792 -- AI05-0073: If the result subtype of the function is defined
8793 -- by an access_definition designating a specific tagged type
8794 -- T, a check is made that the result value is null or the tag
8795 -- of the object designated by the result value identifies T.
8796 -- Constraint_Error is raised if this check fails.
8797
8798 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
8799 declare
8800 Func : Entity_Id;
8801 Func_Typ : Entity_Id;
8802
8803 begin
8804 -- Climb scope stack looking for the enclosing function
8805
8806 Func := Current_Scope;
8807 while Present (Func)
8808 and then Ekind (Func) /= E_Function
8809 loop
8810 Func := Scope (Func);
8811 end loop;
8812
8813 -- The function's return subtype must be defined using
8814 -- an access definition.
8815
8816 if Nkind (Result_Definition (Parent (Func))) =
8817 N_Access_Definition
8818 then
8819 Func_Typ := Directly_Designated_Type (Etype (Func));
8820
8821 -- The return subtype denotes a specific tagged type,
8822 -- in other words, a non class-wide type.
8823
8824 if Is_Tagged_Type (Func_Typ)
8825 and then not Is_Class_Wide_Type (Func_Typ)
8826 then
8827 Make_Tag_Check (Actual_Targ_Typ);
8828 Make_Conversion := True;
8829 end if;
8830 end if;
8831 end;
8832 end if;
8833
8834 -- We have generated a tag check for either a class-wide type
8835 -- conversion or for AI05-0073.
8836
8837 if Make_Conversion then
8838 declare
8839 Conv : Node_Id;
8840 begin
8841 Conv :=
8842 Make_Unchecked_Type_Conversion (Loc,
8843 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8844 Expression => Relocate_Node (Expression (N)));
8845 Rewrite (N, Conv);
8846 Analyze_And_Resolve (N, Target_Type);
8847 end;
8848 end if;
8849 end if;
8850 end Tagged_Conversion;
8851
8852 -- Case of other access type conversions
8853
8854 elsif Is_Access_Type (Target_Type) then
8855 Apply_Constraint_Check (Operand, Target_Type);
8856
8857 -- Case of conversions from a fixed-point type
8858
8859 -- These conversions require special expansion and processing, found in
8860 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8861 -- since from a semantic point of view, these are simple integer
8862 -- conversions, which do not need further processing.
8863
8864 elsif Is_Fixed_Point_Type (Operand_Type)
8865 and then not Conversion_OK (N)
8866 then
8867 -- We should never see universal fixed at this case, since the
8868 -- expansion of the constituent divide or multiply should have
8869 -- eliminated the explicit mention of universal fixed.
8870
8871 pragma Assert (Operand_Type /= Universal_Fixed);
8872
8873 -- Check for special case of the conversion to universal real that
8874 -- occurs as a result of the use of a round attribute. In this case,
8875 -- the real type for the conversion is taken from the target type of
8876 -- the Round attribute and the result must be marked as rounded.
8877
8878 if Target_Type = Universal_Real
8879 and then Nkind (Parent (N)) = N_Attribute_Reference
8880 and then Attribute_Name (Parent (N)) = Name_Round
8881 then
8882 Set_Rounded_Result (N);
8883 Set_Etype (N, Etype (Parent (N)));
8884 end if;
8885
8886 -- Otherwise do correct fixed-conversion, but skip these if the
8887 -- Conversion_OK flag is set, because from a semantic point of view
8888 -- these are simple integer conversions needing no further processing
8889 -- (the backend will simply treat them as integers).
8890
8891 if not Conversion_OK (N) then
8892 if Is_Fixed_Point_Type (Etype (N)) then
8893 Expand_Convert_Fixed_To_Fixed (N);
8894 Real_Range_Check;
8895
8896 elsif Is_Integer_Type (Etype (N)) then
8897 Expand_Convert_Fixed_To_Integer (N);
8898
8899 else
8900 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8901 Expand_Convert_Fixed_To_Float (N);
8902 Real_Range_Check;
8903 end if;
8904 end if;
8905
8906 -- Case of conversions to a fixed-point type
8907
8908 -- These conversions require special expansion and processing, found in
8909 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8910 -- since from a semantic point of view, these are simple integer
8911 -- conversions, which do not need further processing.
8912
8913 elsif Is_Fixed_Point_Type (Target_Type)
8914 and then not Conversion_OK (N)
8915 then
8916 if Is_Integer_Type (Operand_Type) then
8917 Expand_Convert_Integer_To_Fixed (N);
8918 Real_Range_Check;
8919 else
8920 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8921 Expand_Convert_Float_To_Fixed (N);
8922 Real_Range_Check;
8923 end if;
8924
8925 -- Case of float-to-integer conversions
8926
8927 -- We also handle float-to-fixed conversions with Conversion_OK set
8928 -- since semantically the fixed-point target is treated as though it
8929 -- were an integer in such cases.
8930
8931 elsif Is_Floating_Point_Type (Operand_Type)
8932 and then
8933 (Is_Integer_Type (Target_Type)
8934 or else
8935 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8936 then
8937 -- One more check here, gcc is still not able to do conversions of
8938 -- this type with proper overflow checking, and so gigi is doing an
8939 -- approximation of what is required by doing floating-point compares
8940 -- with the end-point. But that can lose precision in some cases, and
8941 -- give a wrong result. Converting the operand to Universal_Real is
8942 -- helpful, but still does not catch all cases with 64-bit integers
8943 -- on targets with only 64-bit floats.
8944
8945 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8946 -- Can this code be removed ???
8947
8948 if Do_Range_Check (Operand) then
8949 Rewrite (Operand,
8950 Make_Type_Conversion (Loc,
8951 Subtype_Mark =>
8952 New_Occurrence_Of (Universal_Real, Loc),
8953 Expression =>
8954 Relocate_Node (Operand)));
8955
8956 Set_Etype (Operand, Universal_Real);
8957 Enable_Range_Check (Operand);
8958 Set_Do_Range_Check (Expression (Operand), False);
8959 end if;
8960
8961 -- Case of array conversions
8962
8963 -- Expansion of array conversions, add required length/range checks but
8964 -- only do this if there is no change of representation. For handling of
8965 -- this case, see Handle_Changed_Representation.
8966
8967 elsif Is_Array_Type (Target_Type) then
8968 if Is_Constrained (Target_Type) then
8969 Apply_Length_Check (Operand, Target_Type);
8970 else
8971 Apply_Range_Check (Operand, Target_Type);
8972 end if;
8973
8974 Handle_Changed_Representation;
8975
8976 -- Case of conversions of discriminated types
8977
8978 -- Add required discriminant checks if target is constrained. Again this
8979 -- change is skipped if we have a change of representation.
8980
8981 elsif Has_Discriminants (Target_Type)
8982 and then Is_Constrained (Target_Type)
8983 then
8984 Apply_Discriminant_Check (Operand, Target_Type);
8985 Handle_Changed_Representation;
8986
8987 -- Case of all other record conversions. The only processing required
8988 -- is to check for a change of representation requiring the special
8989 -- assignment processing.
8990
8991 elsif Is_Record_Type (Target_Type) then
8992
8993 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8994 -- a derived Unchecked_Union type to an unconstrained type that is
8995 -- not Unchecked_Union if the operand lacks inferable discriminants.
8996
8997 if Is_Derived_Type (Operand_Type)
8998 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8999 and then not Is_Constrained (Target_Type)
9000 and then not Is_Unchecked_Union (Base_Type (Target_Type))
9001 and then not Has_Inferable_Discriminants (Operand)
9002 then
9003 -- To prevent Gigi from generating illegal code, we generate a
9004 -- Program_Error node, but we give it the target type of the
9005 -- conversion.
9006
9007 declare
9008 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
9009 Reason => PE_Unchecked_Union_Restriction);
9010
9011 begin
9012 Set_Etype (PE, Target_Type);
9013 Rewrite (N, PE);
9014
9015 end;
9016 else
9017 Handle_Changed_Representation;
9018 end if;
9019
9020 -- Case of conversions of enumeration types
9021
9022 elsif Is_Enumeration_Type (Target_Type) then
9023
9024 -- Special processing is required if there is a change of
9025 -- representation (from enumeration representation clauses).
9026
9027 if not Same_Representation (Target_Type, Operand_Type) then
9028
9029 -- Convert: x(y) to x'val (ytyp'val (y))
9030
9031 Rewrite (N,
9032 Make_Attribute_Reference (Loc,
9033 Prefix => New_Occurrence_Of (Target_Type, Loc),
9034 Attribute_Name => Name_Val,
9035 Expressions => New_List (
9036 Make_Attribute_Reference (Loc,
9037 Prefix => New_Occurrence_Of (Operand_Type, Loc),
9038 Attribute_Name => Name_Pos,
9039 Expressions => New_List (Operand)))));
9040
9041 Analyze_And_Resolve (N, Target_Type);
9042 end if;
9043
9044 -- Case of conversions to floating-point
9045
9046 elsif Is_Floating_Point_Type (Target_Type) then
9047 Real_Range_Check;
9048 end if;
9049
9050 -- At this stage, either the conversion node has been transformed into
9051 -- some other equivalent expression, or left as a conversion that can be
9052 -- handled by Gigi, in the following cases:
9053
9054 -- Conversions with no change of representation or type
9055
9056 -- Numeric conversions involving integer, floating- and fixed-point
9057 -- values. Fixed-point values are allowed only if Conversion_OK is
9058 -- set, i.e. if the fixed-point values are to be treated as integers.
9059
9060 -- No other conversions should be passed to Gigi
9061
9062 -- Check: are these rules stated in sinfo??? if so, why restate here???
9063
9064 -- The only remaining step is to generate a range check if we still have
9065 -- a type conversion at this stage and Do_Range_Check is set. For now we
9066 -- do this only for conversions of discrete types.
9067
9068 if Nkind (N) = N_Type_Conversion
9069 and then Is_Discrete_Type (Etype (N))
9070 then
9071 declare
9072 Expr : constant Node_Id := Expression (N);
9073 Ftyp : Entity_Id;
9074 Ityp : Entity_Id;
9075
9076 begin
9077 if Do_Range_Check (Expr)
9078 and then Is_Discrete_Type (Etype (Expr))
9079 then
9080 Set_Do_Range_Check (Expr, False);
9081
9082 -- Before we do a range check, we have to deal with treating a
9083 -- fixed-point operand as an integer. The way we do this is
9084 -- simply to do an unchecked conversion to an appropriate
9085 -- integer type large enough to hold the result.
9086
9087 -- This code is not active yet, because we are only dealing
9088 -- with discrete types so far ???
9089
9090 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
9091 and then Treat_Fixed_As_Integer (Expr)
9092 then
9093 Ftyp := Base_Type (Etype (Expr));
9094
9095 if Esize (Ftyp) >= Esize (Standard_Integer) then
9096 Ityp := Standard_Long_Long_Integer;
9097 else
9098 Ityp := Standard_Integer;
9099 end if;
9100
9101 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
9102 end if;
9103
9104 -- Reset overflow flag, since the range check will include
9105 -- dealing with possible overflow, and generate the check. If
9106 -- Address is either a source type or target type, suppress
9107 -- range check to avoid typing anomalies when it is a visible
9108 -- integer type.
9109
9110 Set_Do_Overflow_Check (N, False);
9111 if not Is_Descendent_Of_Address (Etype (Expr))
9112 and then not Is_Descendent_Of_Address (Target_Type)
9113 then
9114 Generate_Range_Check
9115 (Expr, Target_Type, CE_Range_Check_Failed);
9116 end if;
9117 end if;
9118 end;
9119 end if;
9120
9121 -- Final step, if the result is a type conversion involving Vax_Float
9122 -- types, then it is subject for further special processing.
9123
9124 if Nkind (N) = N_Type_Conversion
9125 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
9126 then
9127 Expand_Vax_Conversion (N);
9128 goto Done;
9129 end if;
9130
9131 -- Here at end of processing
9132
9133 <<Done>>
9134 -- Apply predicate check if required. Note that we can't just call
9135 -- Apply_Predicate_Check here, because the type looks right after
9136 -- the conversion and it would omit the check. The Comes_From_Source
9137 -- guard is necessary to prevent infinite recursions when we generate
9138 -- internal conversions for the purpose of checking predicates.
9139
9140 if Present (Predicate_Function (Target_Type))
9141 and then Target_Type /= Operand_Type
9142 and then Comes_From_Source (N)
9143 then
9144 Insert_Action (N,
9145 Make_Predicate_Check (Target_Type, Duplicate_Subexpr (N)));
9146 end if;
9147 end Expand_N_Type_Conversion;
9148
9149 -----------------------------------
9150 -- Expand_N_Unchecked_Expression --
9151 -----------------------------------
9152
9153 -- Remove the unchecked expression node from the tree. Its job was simply
9154 -- to make sure that its constituent expression was handled with checks
9155 -- off, and now that that is done, we can remove it from the tree, and
9156 -- indeed must, since Gigi does not expect to see these nodes.
9157
9158 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
9159 Exp : constant Node_Id := Expression (N);
9160 begin
9161 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
9162 Rewrite (N, Exp);
9163 end Expand_N_Unchecked_Expression;
9164
9165 ----------------------------------------
9166 -- Expand_N_Unchecked_Type_Conversion --
9167 ----------------------------------------
9168
9169 -- If this cannot be handled by Gigi and we haven't already made a
9170 -- temporary for it, do it now.
9171
9172 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
9173 Target_Type : constant Entity_Id := Etype (N);
9174 Operand : constant Node_Id := Expression (N);
9175 Operand_Type : constant Entity_Id := Etype (Operand);
9176
9177 begin
9178 -- Nothing at all to do if conversion is to the identical type so remove
9179 -- the conversion completely, it is useless, except that it may carry
9180 -- an Assignment_OK indication which must be propagated to the operand.
9181
9182 if Operand_Type = Target_Type then
9183
9184 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
9185
9186 if Assignment_OK (N) then
9187 Set_Assignment_OK (Operand);
9188 end if;
9189
9190 Rewrite (N, Relocate_Node (Operand));
9191 return;
9192 end if;
9193
9194 -- If we have a conversion of a compile time known value to a target
9195 -- type and the value is in range of the target type, then we can simply
9196 -- replace the construct by an integer literal of the correct type. We
9197 -- only apply this to integer types being converted. Possibly it may
9198 -- apply in other cases, but it is too much trouble to worry about.
9199
9200 -- Note that we do not do this transformation if the Kill_Range_Check
9201 -- flag is set, since then the value may be outside the expected range.
9202 -- This happens in the Normalize_Scalars case.
9203
9204 -- We also skip this if either the target or operand type is biased
9205 -- because in this case, the unchecked conversion is supposed to
9206 -- preserve the bit pattern, not the integer value.
9207
9208 if Is_Integer_Type (Target_Type)
9209 and then not Has_Biased_Representation (Target_Type)
9210 and then Is_Integer_Type (Operand_Type)
9211 and then not Has_Biased_Representation (Operand_Type)
9212 and then Compile_Time_Known_Value (Operand)
9213 and then not Kill_Range_Check (N)
9214 then
9215 declare
9216 Val : constant Uint := Expr_Value (Operand);
9217
9218 begin
9219 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
9220 and then
9221 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
9222 and then
9223 Val >= Expr_Value (Type_Low_Bound (Target_Type))
9224 and then
9225 Val <= Expr_Value (Type_High_Bound (Target_Type))
9226 then
9227 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
9228
9229 -- If Address is the target type, just set the type to avoid a
9230 -- spurious type error on the literal when Address is a visible
9231 -- integer type.
9232
9233 if Is_Descendent_Of_Address (Target_Type) then
9234 Set_Etype (N, Target_Type);
9235 else
9236 Analyze_And_Resolve (N, Target_Type);
9237 end if;
9238
9239 return;
9240 end if;
9241 end;
9242 end if;
9243
9244 -- Nothing to do if conversion is safe
9245
9246 if Safe_Unchecked_Type_Conversion (N) then
9247 return;
9248 end if;
9249
9250 -- Otherwise force evaluation unless Assignment_OK flag is set (this
9251 -- flag indicates ??? -- more comments needed here)
9252
9253 if Assignment_OK (N) then
9254 null;
9255 else
9256 Force_Evaluation (N);
9257 end if;
9258 end Expand_N_Unchecked_Type_Conversion;
9259
9260 ----------------------------
9261 -- Expand_Record_Equality --
9262 ----------------------------
9263
9264 -- For non-variant records, Equality is expanded when needed into:
9265
9266 -- and then Lhs.Discr1 = Rhs.Discr1
9267 -- and then ...
9268 -- and then Lhs.Discrn = Rhs.Discrn
9269 -- and then Lhs.Cmp1 = Rhs.Cmp1
9270 -- and then ...
9271 -- and then Lhs.Cmpn = Rhs.Cmpn
9272
9273 -- The expression is folded by the back-end for adjacent fields. This
9274 -- function is called for tagged record in only one occasion: for imple-
9275 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
9276 -- otherwise the primitive "=" is used directly.
9277
9278 function Expand_Record_Equality
9279 (Nod : Node_Id;
9280 Typ : Entity_Id;
9281 Lhs : Node_Id;
9282 Rhs : Node_Id;
9283 Bodies : List_Id) return Node_Id
9284 is
9285 Loc : constant Source_Ptr := Sloc (Nod);
9286
9287 Result : Node_Id;
9288 C : Entity_Id;
9289
9290 First_Time : Boolean := True;
9291
9292 function Suitable_Element (C : Entity_Id) return Entity_Id;
9293 -- Return the first field to compare beginning with C, skipping the
9294 -- inherited components.
9295
9296 ----------------------
9297 -- Suitable_Element --
9298 ----------------------
9299
9300 function Suitable_Element (C : Entity_Id) return Entity_Id is
9301 begin
9302 if No (C) then
9303 return Empty;
9304
9305 elsif Ekind (C) /= E_Discriminant
9306 and then Ekind (C) /= E_Component
9307 then
9308 return Suitable_Element (Next_Entity (C));
9309
9310 elsif Is_Tagged_Type (Typ)
9311 and then C /= Original_Record_Component (C)
9312 then
9313 return Suitable_Element (Next_Entity (C));
9314
9315 elsif Chars (C) = Name_uTag then
9316 return Suitable_Element (Next_Entity (C));
9317
9318 elsif Is_Interface (Etype (C)) then
9319 return Suitable_Element (Next_Entity (C));
9320
9321 else
9322 return C;
9323 end if;
9324 end Suitable_Element;
9325
9326 -- Start of processing for Expand_Record_Equality
9327
9328 begin
9329 -- Generates the following code: (assuming that Typ has one Discr and
9330 -- component C2 is also a record)
9331
9332 -- True
9333 -- and then Lhs.Discr1 = Rhs.Discr1
9334 -- and then Lhs.C1 = Rhs.C1
9335 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
9336 -- and then ...
9337 -- and then Lhs.Cmpn = Rhs.Cmpn
9338
9339 Result := New_Reference_To (Standard_True, Loc);
9340 C := Suitable_Element (First_Entity (Typ));
9341 while Present (C) loop
9342 declare
9343 New_Lhs : Node_Id;
9344 New_Rhs : Node_Id;
9345 Check : Node_Id;
9346
9347 begin
9348 if First_Time then
9349 First_Time := False;
9350 New_Lhs := Lhs;
9351 New_Rhs := Rhs;
9352 else
9353 New_Lhs := New_Copy_Tree (Lhs);
9354 New_Rhs := New_Copy_Tree (Rhs);
9355 end if;
9356
9357 Check :=
9358 Expand_Composite_Equality (Nod, Etype (C),
9359 Lhs =>
9360 Make_Selected_Component (Loc,
9361 Prefix => New_Lhs,
9362 Selector_Name => New_Reference_To (C, Loc)),
9363 Rhs =>
9364 Make_Selected_Component (Loc,
9365 Prefix => New_Rhs,
9366 Selector_Name => New_Reference_To (C, Loc)),
9367 Bodies => Bodies);
9368
9369 -- If some (sub)component is an unchecked_union, the whole
9370 -- operation will raise program error.
9371
9372 if Nkind (Check) = N_Raise_Program_Error then
9373 Result := Check;
9374 Set_Etype (Result, Standard_Boolean);
9375 exit;
9376 else
9377 Result :=
9378 Make_And_Then (Loc,
9379 Left_Opnd => Result,
9380 Right_Opnd => Check);
9381 end if;
9382 end;
9383
9384 C := Suitable_Element (Next_Entity (C));
9385 end loop;
9386
9387 return Result;
9388 end Expand_Record_Equality;
9389
9390 -----------------------------------
9391 -- Expand_Short_Circuit_Operator --
9392 -----------------------------------
9393
9394 -- Deal with special expansion if actions are present for the right operand
9395 -- and deal with optimizing case of arguments being True or False. We also
9396 -- deal with the special case of non-standard boolean values.
9397
9398 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
9399 Loc : constant Source_Ptr := Sloc (N);
9400 Typ : constant Entity_Id := Etype (N);
9401 Left : constant Node_Id := Left_Opnd (N);
9402 Right : constant Node_Id := Right_Opnd (N);
9403 LocR : constant Source_Ptr := Sloc (Right);
9404 Actlist : List_Id;
9405
9406 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
9407 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
9408 -- If Left = Shortcut_Value then Right need not be evaluated
9409
9410 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
9411 -- For Opnd a boolean expression, return a Boolean expression equivalent
9412 -- to Opnd /= Shortcut_Value.
9413
9414 --------------------
9415 -- Make_Test_Expr --
9416 --------------------
9417
9418 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
9419 begin
9420 if Shortcut_Value then
9421 return Make_Op_Not (Sloc (Opnd), Opnd);
9422 else
9423 return Opnd;
9424 end if;
9425 end Make_Test_Expr;
9426
9427 Op_Var : Entity_Id;
9428 -- Entity for a temporary variable holding the value of the operator,
9429 -- used for expansion in the case where actions are present.
9430
9431 -- Start of processing for Expand_Short_Circuit_Operator
9432
9433 begin
9434 -- Deal with non-standard booleans
9435
9436 if Is_Boolean_Type (Typ) then
9437 Adjust_Condition (Left);
9438 Adjust_Condition (Right);
9439 Set_Etype (N, Standard_Boolean);
9440 end if;
9441
9442 -- Check for cases where left argument is known to be True or False
9443
9444 if Compile_Time_Known_Value (Left) then
9445
9446 -- Mark SCO for left condition as compile time known
9447
9448 if Generate_SCO and then Comes_From_Source (Left) then
9449 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
9450 end if;
9451
9452 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
9453 -- Any actions associated with Right will be executed unconditionally
9454 -- and can thus be inserted into the tree unconditionally.
9455
9456 if Expr_Value_E (Left) /= Shortcut_Ent then
9457 if Present (Actions (N)) then
9458 Insert_Actions (N, Actions (N));
9459 end if;
9460
9461 Rewrite (N, Right);
9462
9463 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
9464 -- In this case we can forget the actions associated with Right,
9465 -- since they will never be executed.
9466
9467 else
9468 Kill_Dead_Code (Right);
9469 Kill_Dead_Code (Actions (N));
9470 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9471 end if;
9472
9473 Adjust_Result_Type (N, Typ);
9474 return;
9475 end if;
9476
9477 -- If Actions are present for the right operand, we have to do some
9478 -- special processing. We can't just let these actions filter back into
9479 -- code preceding the short circuit (which is what would have happened
9480 -- if we had not trapped them in the short-circuit form), since they
9481 -- must only be executed if the right operand of the short circuit is
9482 -- executed and not otherwise.
9483
9484 -- the temporary variable C.
9485
9486 if Present (Actions (N)) then
9487 Actlist := Actions (N);
9488
9489 -- The old approach is to expand:
9490
9491 -- left AND THEN right
9492
9493 -- into
9494
9495 -- C : Boolean := False;
9496 -- IF left THEN
9497 -- Actions;
9498 -- IF right THEN
9499 -- C := True;
9500 -- END IF;
9501 -- END IF;
9502
9503 -- and finally rewrite the operator into a reference to C. Similarly
9504 -- for left OR ELSE right, with negated values. Note that this
9505 -- rewrite causes some difficulties for coverage analysis because
9506 -- of the introduction of the new variable C, which obscures the
9507 -- structure of the test.
9508
9509 -- We use this "old approach" if use of N_Expression_With_Actions
9510 -- is False (see description in Opt of when this is or is not set).
9511
9512 if not Use_Expression_With_Actions then
9513 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
9514
9515 Insert_Action (N,
9516 Make_Object_Declaration (Loc,
9517 Defining_Identifier =>
9518 Op_Var,
9519 Object_Definition =>
9520 New_Occurrence_Of (Standard_Boolean, Loc),
9521 Expression =>
9522 New_Occurrence_Of (Shortcut_Ent, Loc)));
9523
9524 Append_To (Actlist,
9525 Make_Implicit_If_Statement (Right,
9526 Condition => Make_Test_Expr (Right),
9527 Then_Statements => New_List (
9528 Make_Assignment_Statement (LocR,
9529 Name => New_Occurrence_Of (Op_Var, LocR),
9530 Expression =>
9531 New_Occurrence_Of
9532 (Boolean_Literals (not Shortcut_Value), LocR)))));
9533
9534 Insert_Action (N,
9535 Make_Implicit_If_Statement (Left,
9536 Condition => Make_Test_Expr (Left),
9537 Then_Statements => Actlist));
9538
9539 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
9540 Analyze_And_Resolve (N, Standard_Boolean);
9541
9542 -- The new approach, activated for now by the use of debug flag
9543 -- -gnatd.X is to use the new Expression_With_Actions node for the
9544 -- right operand of the short-circuit form. This should solve the
9545 -- traceability problems for coverage analysis.
9546
9547 else
9548 Rewrite (Right,
9549 Make_Expression_With_Actions (LocR,
9550 Expression => Relocate_Node (Right),
9551 Actions => Actlist));
9552 Set_Actions (N, No_List);
9553 Analyze_And_Resolve (Right, Standard_Boolean);
9554 end if;
9555
9556 Adjust_Result_Type (N, Typ);
9557 return;
9558 end if;
9559
9560 -- No actions present, check for cases of right argument True/False
9561
9562 if Compile_Time_Known_Value (Right) then
9563
9564 -- Mark SCO for left condition as compile time known
9565
9566 if Generate_SCO and then Comes_From_Source (Right) then
9567 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
9568 end if;
9569
9570 -- Change (Left and then True), (Left or else False) to Left.
9571 -- Note that we know there are no actions associated with the right
9572 -- operand, since we just checked for this case above.
9573
9574 if Expr_Value_E (Right) /= Shortcut_Ent then
9575 Rewrite (N, Left);
9576
9577 -- Change (Left and then False), (Left or else True) to Right,
9578 -- making sure to preserve any side effects associated with the Left
9579 -- operand.
9580
9581 else
9582 Remove_Side_Effects (Left);
9583 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9584 end if;
9585 end if;
9586
9587 Adjust_Result_Type (N, Typ);
9588 end Expand_Short_Circuit_Operator;
9589
9590 -------------------------------------
9591 -- Fixup_Universal_Fixed_Operation --
9592 -------------------------------------
9593
9594 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
9595 Conv : constant Node_Id := Parent (N);
9596
9597 begin
9598 -- We must have a type conversion immediately above us
9599
9600 pragma Assert (Nkind (Conv) = N_Type_Conversion);
9601
9602 -- Normally the type conversion gives our target type. The exception
9603 -- occurs in the case of the Round attribute, where the conversion
9604 -- will be to universal real, and our real type comes from the Round
9605 -- attribute (as well as an indication that we must round the result)
9606
9607 if Nkind (Parent (Conv)) = N_Attribute_Reference
9608 and then Attribute_Name (Parent (Conv)) = Name_Round
9609 then
9610 Set_Etype (N, Etype (Parent (Conv)));
9611 Set_Rounded_Result (N);
9612
9613 -- Normal case where type comes from conversion above us
9614
9615 else
9616 Set_Etype (N, Etype (Conv));
9617 end if;
9618 end Fixup_Universal_Fixed_Operation;
9619
9620 ---------------------------------
9621 -- Has_Inferable_Discriminants --
9622 ---------------------------------
9623
9624 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
9625
9626 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
9627 -- Determines whether the left-most prefix of a selected component is a
9628 -- formal parameter in a subprogram. Assumes N is a selected component.
9629
9630 --------------------------------
9631 -- Prefix_Is_Formal_Parameter --
9632 --------------------------------
9633
9634 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
9635 Sel_Comp : Node_Id := N;
9636
9637 begin
9638 -- Move to the left-most prefix by climbing up the tree
9639
9640 while Present (Parent (Sel_Comp))
9641 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
9642 loop
9643 Sel_Comp := Parent (Sel_Comp);
9644 end loop;
9645
9646 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
9647 end Prefix_Is_Formal_Parameter;
9648
9649 -- Start of processing for Has_Inferable_Discriminants
9650
9651 begin
9652 -- For identifiers and indexed components, it is sufficient to have a
9653 -- constrained Unchecked_Union nominal subtype.
9654
9655 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
9656 return Is_Unchecked_Union (Base_Type (Etype (N)))
9657 and then
9658 Is_Constrained (Etype (N));
9659
9660 -- For selected components, the subtype of the selector must be a
9661 -- constrained Unchecked_Union. If the component is subject to a
9662 -- per-object constraint, then the enclosing object must have inferable
9663 -- discriminants.
9664
9665 elsif Nkind (N) = N_Selected_Component then
9666 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
9667
9668 -- A small hack. If we have a per-object constrained selected
9669 -- component of a formal parameter, return True since we do not
9670 -- know the actual parameter association yet.
9671
9672 if Prefix_Is_Formal_Parameter (N) then
9673 return True;
9674 end if;
9675
9676 -- Otherwise, check the enclosing object and the selector
9677
9678 return Has_Inferable_Discriminants (Prefix (N))
9679 and then
9680 Has_Inferable_Discriminants (Selector_Name (N));
9681 end if;
9682
9683 -- The call to Has_Inferable_Discriminants will determine whether
9684 -- the selector has a constrained Unchecked_Union nominal type.
9685
9686 return Has_Inferable_Discriminants (Selector_Name (N));
9687
9688 -- A qualified expression has inferable discriminants if its subtype
9689 -- mark is a constrained Unchecked_Union subtype.
9690
9691 elsif Nkind (N) = N_Qualified_Expression then
9692 return Is_Unchecked_Union (Subtype_Mark (N))
9693 and then
9694 Is_Constrained (Subtype_Mark (N));
9695
9696 end if;
9697
9698 return False;
9699 end Has_Inferable_Discriminants;
9700
9701 -------------------------------
9702 -- Insert_Dereference_Action --
9703 -------------------------------
9704
9705 procedure Insert_Dereference_Action (N : Node_Id) is
9706 Loc : constant Source_Ptr := Sloc (N);
9707 Typ : constant Entity_Id := Etype (N);
9708 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
9709 Pnod : constant Node_Id := Parent (N);
9710
9711 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
9712 -- Return true if type of P is derived from Checked_Pool;
9713
9714 -----------------------------
9715 -- Is_Checked_Storage_Pool --
9716 -----------------------------
9717
9718 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
9719 T : Entity_Id;
9720
9721 begin
9722 if No (P) then
9723 return False;
9724 end if;
9725
9726 T := Etype (P);
9727 while T /= Etype (T) loop
9728 if Is_RTE (T, RE_Checked_Pool) then
9729 return True;
9730 else
9731 T := Etype (T);
9732 end if;
9733 end loop;
9734
9735 return False;
9736 end Is_Checked_Storage_Pool;
9737
9738 -- Start of processing for Insert_Dereference_Action
9739
9740 begin
9741 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
9742
9743 if not (Is_Checked_Storage_Pool (Pool)
9744 and then Comes_From_Source (Original_Node (Pnod)))
9745 then
9746 return;
9747 end if;
9748
9749 Insert_Action (N,
9750 Make_Procedure_Call_Statement (Loc,
9751 Name => New_Reference_To (
9752 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
9753
9754 Parameter_Associations => New_List (
9755
9756 -- Pool
9757
9758 New_Reference_To (Pool, Loc),
9759
9760 -- Storage_Address. We use the attribute Pool_Address, which uses
9761 -- the pointer itself to find the address of the object, and which
9762 -- handles unconstrained arrays properly by computing the address
9763 -- of the template. i.e. the correct address of the corresponding
9764 -- allocation.
9765
9766 Make_Attribute_Reference (Loc,
9767 Prefix => Duplicate_Subexpr_Move_Checks (N),
9768 Attribute_Name => Name_Pool_Address),
9769
9770 -- Size_In_Storage_Elements
9771
9772 Make_Op_Divide (Loc,
9773 Left_Opnd =>
9774 Make_Attribute_Reference (Loc,
9775 Prefix =>
9776 Make_Explicit_Dereference (Loc,
9777 Duplicate_Subexpr_Move_Checks (N)),
9778 Attribute_Name => Name_Size),
9779 Right_Opnd =>
9780 Make_Integer_Literal (Loc, System_Storage_Unit)),
9781
9782 -- Alignment
9783
9784 Make_Attribute_Reference (Loc,
9785 Prefix =>
9786 Make_Explicit_Dereference (Loc,
9787 Duplicate_Subexpr_Move_Checks (N)),
9788 Attribute_Name => Name_Alignment))));
9789
9790 exception
9791 when RE_Not_Available =>
9792 return;
9793 end Insert_Dereference_Action;
9794
9795 --------------------------------
9796 -- Integer_Promotion_Possible --
9797 --------------------------------
9798
9799 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
9800 Operand : constant Node_Id := Expression (N);
9801 Operand_Type : constant Entity_Id := Etype (Operand);
9802 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
9803
9804 begin
9805 pragma Assert (Nkind (N) = N_Type_Conversion);
9806
9807 return
9808
9809 -- We only do the transformation for source constructs. We assume
9810 -- that the expander knows what it is doing when it generates code.
9811
9812 Comes_From_Source (N)
9813
9814 -- If the operand type is Short_Integer or Short_Short_Integer,
9815 -- then we will promote to Integer, which is available on all
9816 -- targets, and is sufficient to ensure no intermediate overflow.
9817 -- Furthermore it is likely to be as efficient or more efficient
9818 -- than using the smaller type for the computation so we do this
9819 -- unconditionally.
9820
9821 and then
9822 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
9823 or else
9824 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
9825
9826 -- Test for interesting operation, which includes addition,
9827 -- division, exponentiation, multiplication, subtraction, absolute
9828 -- value and unary negation. Unary "+" is omitted since it is a
9829 -- no-op and thus can't overflow.
9830
9831 and then Nkind_In (Operand, N_Op_Abs,
9832 N_Op_Add,
9833 N_Op_Divide,
9834 N_Op_Expon,
9835 N_Op_Minus,
9836 N_Op_Multiply,
9837 N_Op_Subtract);
9838 end Integer_Promotion_Possible;
9839
9840 ------------------------------
9841 -- Make_Array_Comparison_Op --
9842 ------------------------------
9843
9844 -- This is a hand-coded expansion of the following generic function:
9845
9846 -- generic
9847 -- type elem is (<>);
9848 -- type index is (<>);
9849 -- type a is array (index range <>) of elem;
9850
9851 -- function Gnnn (X : a; Y: a) return boolean is
9852 -- J : index := Y'first;
9853
9854 -- begin
9855 -- if X'length = 0 then
9856 -- return false;
9857
9858 -- elsif Y'length = 0 then
9859 -- return true;
9860
9861 -- else
9862 -- for I in X'range loop
9863 -- if X (I) = Y (J) then
9864 -- if J = Y'last then
9865 -- exit;
9866 -- else
9867 -- J := index'succ (J);
9868 -- end if;
9869
9870 -- else
9871 -- return X (I) > Y (J);
9872 -- end if;
9873 -- end loop;
9874
9875 -- return X'length > Y'length;
9876 -- end if;
9877 -- end Gnnn;
9878
9879 -- Note that since we are essentially doing this expansion by hand, we
9880 -- do not need to generate an actual or formal generic part, just the
9881 -- instantiated function itself.
9882
9883 function Make_Array_Comparison_Op
9884 (Typ : Entity_Id;
9885 Nod : Node_Id) return Node_Id
9886 is
9887 Loc : constant Source_Ptr := Sloc (Nod);
9888
9889 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
9890 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
9891 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
9892 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9893
9894 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
9895
9896 Loop_Statement : Node_Id;
9897 Loop_Body : Node_Id;
9898 If_Stat : Node_Id;
9899 Inner_If : Node_Id;
9900 Final_Expr : Node_Id;
9901 Func_Body : Node_Id;
9902 Func_Name : Entity_Id;
9903 Formals : List_Id;
9904 Length1 : Node_Id;
9905 Length2 : Node_Id;
9906
9907 begin
9908 -- if J = Y'last then
9909 -- exit;
9910 -- else
9911 -- J := index'succ (J);
9912 -- end if;
9913
9914 Inner_If :=
9915 Make_Implicit_If_Statement (Nod,
9916 Condition =>
9917 Make_Op_Eq (Loc,
9918 Left_Opnd => New_Reference_To (J, Loc),
9919 Right_Opnd =>
9920 Make_Attribute_Reference (Loc,
9921 Prefix => New_Reference_To (Y, Loc),
9922 Attribute_Name => Name_Last)),
9923
9924 Then_Statements => New_List (
9925 Make_Exit_Statement (Loc)),
9926
9927 Else_Statements =>
9928 New_List (
9929 Make_Assignment_Statement (Loc,
9930 Name => New_Reference_To (J, Loc),
9931 Expression =>
9932 Make_Attribute_Reference (Loc,
9933 Prefix => New_Reference_To (Index, Loc),
9934 Attribute_Name => Name_Succ,
9935 Expressions => New_List (New_Reference_To (J, Loc))))));
9936
9937 -- if X (I) = Y (J) then
9938 -- if ... end if;
9939 -- else
9940 -- return X (I) > Y (J);
9941 -- end if;
9942
9943 Loop_Body :=
9944 Make_Implicit_If_Statement (Nod,
9945 Condition =>
9946 Make_Op_Eq (Loc,
9947 Left_Opnd =>
9948 Make_Indexed_Component (Loc,
9949 Prefix => New_Reference_To (X, Loc),
9950 Expressions => New_List (New_Reference_To (I, Loc))),
9951
9952 Right_Opnd =>
9953 Make_Indexed_Component (Loc,
9954 Prefix => New_Reference_To (Y, Loc),
9955 Expressions => New_List (New_Reference_To (J, Loc)))),
9956
9957 Then_Statements => New_List (Inner_If),
9958
9959 Else_Statements => New_List (
9960 Make_Simple_Return_Statement (Loc,
9961 Expression =>
9962 Make_Op_Gt (Loc,
9963 Left_Opnd =>
9964 Make_Indexed_Component (Loc,
9965 Prefix => New_Reference_To (X, Loc),
9966 Expressions => New_List (New_Reference_To (I, Loc))),
9967
9968 Right_Opnd =>
9969 Make_Indexed_Component (Loc,
9970 Prefix => New_Reference_To (Y, Loc),
9971 Expressions => New_List (
9972 New_Reference_To (J, Loc)))))));
9973
9974 -- for I in X'range loop
9975 -- if ... end if;
9976 -- end loop;
9977
9978 Loop_Statement :=
9979 Make_Implicit_Loop_Statement (Nod,
9980 Identifier => Empty,
9981
9982 Iteration_Scheme =>
9983 Make_Iteration_Scheme (Loc,
9984 Loop_Parameter_Specification =>
9985 Make_Loop_Parameter_Specification (Loc,
9986 Defining_Identifier => I,
9987 Discrete_Subtype_Definition =>
9988 Make_Attribute_Reference (Loc,
9989 Prefix => New_Reference_To (X, Loc),
9990 Attribute_Name => Name_Range))),
9991
9992 Statements => New_List (Loop_Body));
9993
9994 -- if X'length = 0 then
9995 -- return false;
9996 -- elsif Y'length = 0 then
9997 -- return true;
9998 -- else
9999 -- for ... loop ... end loop;
10000 -- return X'length > Y'length;
10001 -- end if;
10002
10003 Length1 :=
10004 Make_Attribute_Reference (Loc,
10005 Prefix => New_Reference_To (X, Loc),
10006 Attribute_Name => Name_Length);
10007
10008 Length2 :=
10009 Make_Attribute_Reference (Loc,
10010 Prefix => New_Reference_To (Y, Loc),
10011 Attribute_Name => Name_Length);
10012
10013 Final_Expr :=
10014 Make_Op_Gt (Loc,
10015 Left_Opnd => Length1,
10016 Right_Opnd => Length2);
10017
10018 If_Stat :=
10019 Make_Implicit_If_Statement (Nod,
10020 Condition =>
10021 Make_Op_Eq (Loc,
10022 Left_Opnd =>
10023 Make_Attribute_Reference (Loc,
10024 Prefix => New_Reference_To (X, Loc),
10025 Attribute_Name => Name_Length),
10026 Right_Opnd =>
10027 Make_Integer_Literal (Loc, 0)),
10028
10029 Then_Statements =>
10030 New_List (
10031 Make_Simple_Return_Statement (Loc,
10032 Expression => New_Reference_To (Standard_False, Loc))),
10033
10034 Elsif_Parts => New_List (
10035 Make_Elsif_Part (Loc,
10036 Condition =>
10037 Make_Op_Eq (Loc,
10038 Left_Opnd =>
10039 Make_Attribute_Reference (Loc,
10040 Prefix => New_Reference_To (Y, Loc),
10041 Attribute_Name => Name_Length),
10042 Right_Opnd =>
10043 Make_Integer_Literal (Loc, 0)),
10044
10045 Then_Statements =>
10046 New_List (
10047 Make_Simple_Return_Statement (Loc,
10048 Expression => New_Reference_To (Standard_True, Loc))))),
10049
10050 Else_Statements => New_List (
10051 Loop_Statement,
10052 Make_Simple_Return_Statement (Loc,
10053 Expression => Final_Expr)));
10054
10055 -- (X : a; Y: a)
10056
10057 Formals := New_List (
10058 Make_Parameter_Specification (Loc,
10059 Defining_Identifier => X,
10060 Parameter_Type => New_Reference_To (Typ, Loc)),
10061
10062 Make_Parameter_Specification (Loc,
10063 Defining_Identifier => Y,
10064 Parameter_Type => New_Reference_To (Typ, Loc)));
10065
10066 -- function Gnnn (...) return boolean is
10067 -- J : index := Y'first;
10068 -- begin
10069 -- if ... end if;
10070 -- end Gnnn;
10071
10072 Func_Name := Make_Temporary (Loc, 'G');
10073
10074 Func_Body :=
10075 Make_Subprogram_Body (Loc,
10076 Specification =>
10077 Make_Function_Specification (Loc,
10078 Defining_Unit_Name => Func_Name,
10079 Parameter_Specifications => Formals,
10080 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
10081
10082 Declarations => New_List (
10083 Make_Object_Declaration (Loc,
10084 Defining_Identifier => J,
10085 Object_Definition => New_Reference_To (Index, Loc),
10086 Expression =>
10087 Make_Attribute_Reference (Loc,
10088 Prefix => New_Reference_To (Y, Loc),
10089 Attribute_Name => Name_First))),
10090
10091 Handled_Statement_Sequence =>
10092 Make_Handled_Sequence_Of_Statements (Loc,
10093 Statements => New_List (If_Stat)));
10094
10095 return Func_Body;
10096 end Make_Array_Comparison_Op;
10097
10098 ---------------------------
10099 -- Make_Boolean_Array_Op --
10100 ---------------------------
10101
10102 -- For logical operations on boolean arrays, expand in line the following,
10103 -- replacing 'and' with 'or' or 'xor' where needed:
10104
10105 -- function Annn (A : typ; B: typ) return typ is
10106 -- C : typ;
10107 -- begin
10108 -- for J in A'range loop
10109 -- C (J) := A (J) op B (J);
10110 -- end loop;
10111 -- return C;
10112 -- end Annn;
10113
10114 -- Here typ is the boolean array type
10115
10116 function Make_Boolean_Array_Op
10117 (Typ : Entity_Id;
10118 N : Node_Id) return Node_Id
10119 is
10120 Loc : constant Source_Ptr := Sloc (N);
10121
10122 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
10123 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
10124 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
10125 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
10126
10127 A_J : Node_Id;
10128 B_J : Node_Id;
10129 C_J : Node_Id;
10130 Op : Node_Id;
10131
10132 Formals : List_Id;
10133 Func_Name : Entity_Id;
10134 Func_Body : Node_Id;
10135 Loop_Statement : Node_Id;
10136
10137 begin
10138 A_J :=
10139 Make_Indexed_Component (Loc,
10140 Prefix => New_Reference_To (A, Loc),
10141 Expressions => New_List (New_Reference_To (J, Loc)));
10142
10143 B_J :=
10144 Make_Indexed_Component (Loc,
10145 Prefix => New_Reference_To (B, Loc),
10146 Expressions => New_List (New_Reference_To (J, Loc)));
10147
10148 C_J :=
10149 Make_Indexed_Component (Loc,
10150 Prefix => New_Reference_To (C, Loc),
10151 Expressions => New_List (New_Reference_To (J, Loc)));
10152
10153 if Nkind (N) = N_Op_And then
10154 Op :=
10155 Make_Op_And (Loc,
10156 Left_Opnd => A_J,
10157 Right_Opnd => B_J);
10158
10159 elsif Nkind (N) = N_Op_Or then
10160 Op :=
10161 Make_Op_Or (Loc,
10162 Left_Opnd => A_J,
10163 Right_Opnd => B_J);
10164
10165 else
10166 Op :=
10167 Make_Op_Xor (Loc,
10168 Left_Opnd => A_J,
10169 Right_Opnd => B_J);
10170 end if;
10171
10172 Loop_Statement :=
10173 Make_Implicit_Loop_Statement (N,
10174 Identifier => Empty,
10175
10176 Iteration_Scheme =>
10177 Make_Iteration_Scheme (Loc,
10178 Loop_Parameter_Specification =>
10179 Make_Loop_Parameter_Specification (Loc,
10180 Defining_Identifier => J,
10181 Discrete_Subtype_Definition =>
10182 Make_Attribute_Reference (Loc,
10183 Prefix => New_Reference_To (A, Loc),
10184 Attribute_Name => Name_Range))),
10185
10186 Statements => New_List (
10187 Make_Assignment_Statement (Loc,
10188 Name => C_J,
10189 Expression => Op)));
10190
10191 Formals := New_List (
10192 Make_Parameter_Specification (Loc,
10193 Defining_Identifier => A,
10194 Parameter_Type => New_Reference_To (Typ, Loc)),
10195
10196 Make_Parameter_Specification (Loc,
10197 Defining_Identifier => B,
10198 Parameter_Type => New_Reference_To (Typ, Loc)));
10199
10200 Func_Name := Make_Temporary (Loc, 'A');
10201 Set_Is_Inlined (Func_Name);
10202
10203 Func_Body :=
10204 Make_Subprogram_Body (Loc,
10205 Specification =>
10206 Make_Function_Specification (Loc,
10207 Defining_Unit_Name => Func_Name,
10208 Parameter_Specifications => Formals,
10209 Result_Definition => New_Reference_To (Typ, Loc)),
10210
10211 Declarations => New_List (
10212 Make_Object_Declaration (Loc,
10213 Defining_Identifier => C,
10214 Object_Definition => New_Reference_To (Typ, Loc))),
10215
10216 Handled_Statement_Sequence =>
10217 Make_Handled_Sequence_Of_Statements (Loc,
10218 Statements => New_List (
10219 Loop_Statement,
10220 Make_Simple_Return_Statement (Loc,
10221 Expression => New_Reference_To (C, Loc)))));
10222
10223 return Func_Body;
10224 end Make_Boolean_Array_Op;
10225
10226 --------------------------------
10227 -- Optimize_Length_Comparison --
10228 --------------------------------
10229
10230 procedure Optimize_Length_Comparison (N : Node_Id) is
10231 Loc : constant Source_Ptr := Sloc (N);
10232 Typ : constant Entity_Id := Etype (N);
10233 Result : Node_Id;
10234
10235 Left : Node_Id;
10236 Right : Node_Id;
10237 -- First and Last attribute reference nodes, which end up as left and
10238 -- right operands of the optimized result.
10239
10240 Is_Zero : Boolean;
10241 -- True for comparison operand of zero
10242
10243 Comp : Node_Id;
10244 -- Comparison operand, set only if Is_Zero is false
10245
10246 Ent : Entity_Id;
10247 -- Entity whose length is being compared
10248
10249 Index : Node_Id;
10250 -- Integer_Literal node for length attribute expression, or Empty
10251 -- if there is no such expression present.
10252
10253 Ityp : Entity_Id;
10254 -- Type of array index to which 'Length is applied
10255
10256 Op : Node_Kind := Nkind (N);
10257 -- Kind of comparison operator, gets flipped if operands backwards
10258
10259 function Is_Optimizable (N : Node_Id) return Boolean;
10260 -- Tests N to see if it is an optimizable comparison value (defined as
10261 -- constant zero or one, or something else where the value is known to
10262 -- be positive and in the range of 32-bits, and where the corresponding
10263 -- Length value is also known to be 32-bits. If result is true, sets
10264 -- Is_Zero, Ityp, and Comp accordingly.
10265
10266 function Is_Entity_Length (N : Node_Id) return Boolean;
10267 -- Tests if N is a length attribute applied to a simple entity. If so,
10268 -- returns True, and sets Ent to the entity, and Index to the integer
10269 -- literal provided as an attribute expression, or to Empty if none.
10270 -- Also returns True if the expression is a generated type conversion
10271 -- whose expression is of the desired form. This latter case arises
10272 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
10273 -- to check for being in range, which is not needed in this context.
10274 -- Returns False if neither condition holds.
10275
10276 function Prepare_64 (N : Node_Id) return Node_Id;
10277 -- Given a discrete expression, returns a Long_Long_Integer typed
10278 -- expression representing the underlying value of the expression.
10279 -- This is done with an unchecked conversion to the result type. We
10280 -- use unchecked conversion to handle the enumeration type case.
10281
10282 ----------------------
10283 -- Is_Entity_Length --
10284 ----------------------
10285
10286 function Is_Entity_Length (N : Node_Id) return Boolean is
10287 begin
10288 if Nkind (N) = N_Attribute_Reference
10289 and then Attribute_Name (N) = Name_Length
10290 and then Is_Entity_Name (Prefix (N))
10291 then
10292 Ent := Entity (Prefix (N));
10293
10294 if Present (Expressions (N)) then
10295 Index := First (Expressions (N));
10296 else
10297 Index := Empty;
10298 end if;
10299
10300 return True;
10301
10302 elsif Nkind (N) = N_Type_Conversion
10303 and then not Comes_From_Source (N)
10304 then
10305 return Is_Entity_Length (Expression (N));
10306
10307 else
10308 return False;
10309 end if;
10310 end Is_Entity_Length;
10311
10312 --------------------
10313 -- Is_Optimizable --
10314 --------------------
10315
10316 function Is_Optimizable (N : Node_Id) return Boolean is
10317 Val : Uint;
10318 OK : Boolean;
10319 Lo : Uint;
10320 Hi : Uint;
10321 Indx : Node_Id;
10322
10323 begin
10324 if Compile_Time_Known_Value (N) then
10325 Val := Expr_Value (N);
10326
10327 if Val = Uint_0 then
10328 Is_Zero := True;
10329 Comp := Empty;
10330 return True;
10331
10332 elsif Val = Uint_1 then
10333 Is_Zero := False;
10334 Comp := Empty;
10335 return True;
10336 end if;
10337 end if;
10338
10339 -- Here we have to make sure of being within 32-bits
10340
10341 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
10342
10343 if not OK
10344 or else Lo < Uint_1
10345 or else Hi > UI_From_Int (Int'Last)
10346 then
10347 return False;
10348 end if;
10349
10350 -- Comparison value was within range, so now we must check the index
10351 -- value to make sure it is also within 32-bits.
10352
10353 Indx := First_Index (Etype (Ent));
10354
10355 if Present (Index) then
10356 for J in 2 .. UI_To_Int (Intval (Index)) loop
10357 Next_Index (Indx);
10358 end loop;
10359 end if;
10360
10361 Ityp := Etype (Indx);
10362
10363 if Esize (Ityp) > 32 then
10364 return False;
10365 end if;
10366
10367 Is_Zero := False;
10368 Comp := N;
10369 return True;
10370 end Is_Optimizable;
10371
10372 ----------------
10373 -- Prepare_64 --
10374 ----------------
10375
10376 function Prepare_64 (N : Node_Id) return Node_Id is
10377 begin
10378 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
10379 end Prepare_64;
10380
10381 -- Start of processing for Optimize_Length_Comparison
10382
10383 begin
10384 -- Nothing to do if not a comparison
10385
10386 if Op not in N_Op_Compare then
10387 return;
10388 end if;
10389
10390 -- Nothing to do if special -gnatd.P debug flag set
10391
10392 if Debug_Flag_Dot_PP then
10393 return;
10394 end if;
10395
10396 -- Ent'Length op 0/1
10397
10398 if Is_Entity_Length (Left_Opnd (N))
10399 and then Is_Optimizable (Right_Opnd (N))
10400 then
10401 null;
10402
10403 -- 0/1 op Ent'Length
10404
10405 elsif Is_Entity_Length (Right_Opnd (N))
10406 and then Is_Optimizable (Left_Opnd (N))
10407 then
10408 -- Flip comparison to opposite sense
10409
10410 case Op is
10411 when N_Op_Lt => Op := N_Op_Gt;
10412 when N_Op_Le => Op := N_Op_Ge;
10413 when N_Op_Gt => Op := N_Op_Lt;
10414 when N_Op_Ge => Op := N_Op_Le;
10415 when others => null;
10416 end case;
10417
10418 -- Else optimization not possible
10419
10420 else
10421 return;
10422 end if;
10423
10424 -- Fall through if we will do the optimization
10425
10426 -- Cases to handle:
10427
10428 -- X'Length = 0 => X'First > X'Last
10429 -- X'Length = 1 => X'First = X'Last
10430 -- X'Length = n => X'First + (n - 1) = X'Last
10431
10432 -- X'Length /= 0 => X'First <= X'Last
10433 -- X'Length /= 1 => X'First /= X'Last
10434 -- X'Length /= n => X'First + (n - 1) /= X'Last
10435
10436 -- X'Length >= 0 => always true, warn
10437 -- X'Length >= 1 => X'First <= X'Last
10438 -- X'Length >= n => X'First + (n - 1) <= X'Last
10439
10440 -- X'Length > 0 => X'First <= X'Last
10441 -- X'Length > 1 => X'First < X'Last
10442 -- X'Length > n => X'First + (n - 1) < X'Last
10443
10444 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
10445 -- X'Length <= 1 => X'First >= X'Last
10446 -- X'Length <= n => X'First + (n - 1) >= X'Last
10447
10448 -- X'Length < 0 => always false (warn)
10449 -- X'Length < 1 => X'First > X'Last
10450 -- X'Length < n => X'First + (n - 1) > X'Last
10451
10452 -- Note: for the cases of n (not constant 0,1), we require that the
10453 -- corresponding index type be integer or shorter (i.e. not 64-bit),
10454 -- and the same for the comparison value. Then we do the comparison
10455 -- using 64-bit arithmetic (actually long long integer), so that we
10456 -- cannot have overflow intefering with the result.
10457
10458 -- First deal with warning cases
10459
10460 if Is_Zero then
10461 case Op is
10462
10463 -- X'Length >= 0
10464
10465 when N_Op_Ge =>
10466 Rewrite (N,
10467 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
10468 Analyze_And_Resolve (N, Typ);
10469 Warn_On_Known_Condition (N);
10470 return;
10471
10472 -- X'Length < 0
10473
10474 when N_Op_Lt =>
10475 Rewrite (N,
10476 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
10477 Analyze_And_Resolve (N, Typ);
10478 Warn_On_Known_Condition (N);
10479 return;
10480
10481 when N_Op_Le =>
10482 if Constant_Condition_Warnings
10483 and then Comes_From_Source (Original_Node (N))
10484 then
10485 Error_Msg_N ("could replace by ""'=""?", N);
10486 end if;
10487
10488 Op := N_Op_Eq;
10489
10490 when others =>
10491 null;
10492 end case;
10493 end if;
10494
10495 -- Build the First reference we will use
10496
10497 Left :=
10498 Make_Attribute_Reference (Loc,
10499 Prefix => New_Occurrence_Of (Ent, Loc),
10500 Attribute_Name => Name_First);
10501
10502 if Present (Index) then
10503 Set_Expressions (Left, New_List (New_Copy (Index)));
10504 end if;
10505
10506 -- If general value case, then do the addition of (n - 1), and
10507 -- also add the needed conversions to type Long_Long_Integer.
10508
10509 if Present (Comp) then
10510 Left :=
10511 Make_Op_Add (Loc,
10512 Left_Opnd => Prepare_64 (Left),
10513 Right_Opnd =>
10514 Make_Op_Subtract (Loc,
10515 Left_Opnd => Prepare_64 (Comp),
10516 Right_Opnd => Make_Integer_Literal (Loc, 1)));
10517 end if;
10518
10519 -- Build the Last reference we will use
10520
10521 Right :=
10522 Make_Attribute_Reference (Loc,
10523 Prefix => New_Occurrence_Of (Ent, Loc),
10524 Attribute_Name => Name_Last);
10525
10526 if Present (Index) then
10527 Set_Expressions (Right, New_List (New_Copy (Index)));
10528 end if;
10529
10530 -- If general operand, convert Last reference to Long_Long_Integer
10531
10532 if Present (Comp) then
10533 Right := Prepare_64 (Right);
10534 end if;
10535
10536 -- Check for cases to optimize
10537
10538 -- X'Length = 0 => X'First > X'Last
10539 -- X'Length < 1 => X'First > X'Last
10540 -- X'Length < n => X'First + (n - 1) > X'Last
10541
10542 if (Is_Zero and then Op = N_Op_Eq)
10543 or else (not Is_Zero and then Op = N_Op_Lt)
10544 then
10545 Result :=
10546 Make_Op_Gt (Loc,
10547 Left_Opnd => Left,
10548 Right_Opnd => Right);
10549
10550 -- X'Length = 1 => X'First = X'Last
10551 -- X'Length = n => X'First + (n - 1) = X'Last
10552
10553 elsif not Is_Zero and then Op = N_Op_Eq then
10554 Result :=
10555 Make_Op_Eq (Loc,
10556 Left_Opnd => Left,
10557 Right_Opnd => Right);
10558
10559 -- X'Length /= 0 => X'First <= X'Last
10560 -- X'Length > 0 => X'First <= X'Last
10561
10562 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
10563 Result :=
10564 Make_Op_Le (Loc,
10565 Left_Opnd => Left,
10566 Right_Opnd => Right);
10567
10568 -- X'Length /= 1 => X'First /= X'Last
10569 -- X'Length /= n => X'First + (n - 1) /= X'Last
10570
10571 elsif not Is_Zero and then Op = N_Op_Ne then
10572 Result :=
10573 Make_Op_Ne (Loc,
10574 Left_Opnd => Left,
10575 Right_Opnd => Right);
10576
10577 -- X'Length >= 1 => X'First <= X'Last
10578 -- X'Length >= n => X'First + (n - 1) <= X'Last
10579
10580 elsif not Is_Zero and then Op = N_Op_Ge then
10581 Result :=
10582 Make_Op_Le (Loc,
10583 Left_Opnd => Left,
10584 Right_Opnd => Right);
10585
10586 -- X'Length > 1 => X'First < X'Last
10587 -- X'Length > n => X'First + (n = 1) < X'Last
10588
10589 elsif not Is_Zero and then Op = N_Op_Gt then
10590 Result :=
10591 Make_Op_Lt (Loc,
10592 Left_Opnd => Left,
10593 Right_Opnd => Right);
10594
10595 -- X'Length <= 1 => X'First >= X'Last
10596 -- X'Length <= n => X'First + (n - 1) >= X'Last
10597
10598 elsif not Is_Zero and then Op = N_Op_Le then
10599 Result :=
10600 Make_Op_Ge (Loc,
10601 Left_Opnd => Left,
10602 Right_Opnd => Right);
10603
10604 -- Should not happen at this stage
10605
10606 else
10607 raise Program_Error;
10608 end if;
10609
10610 -- Rewrite and finish up
10611
10612 Rewrite (N, Result);
10613 Analyze_And_Resolve (N, Typ);
10614 return;
10615 end Optimize_Length_Comparison;
10616
10617 ------------------------
10618 -- Rewrite_Comparison --
10619 ------------------------
10620
10621 procedure Rewrite_Comparison (N : Node_Id) is
10622 Warning_Generated : Boolean := False;
10623 -- Set to True if first pass with Assume_Valid generates a warning in
10624 -- which case we skip the second pass to avoid warning overloaded.
10625
10626 Result : Node_Id;
10627 -- Set to Standard_True or Standard_False
10628
10629 begin
10630 if Nkind (N) = N_Type_Conversion then
10631 Rewrite_Comparison (Expression (N));
10632 return;
10633
10634 elsif Nkind (N) not in N_Op_Compare then
10635 return;
10636 end if;
10637
10638 -- Now start looking at the comparison in detail. We potentially go
10639 -- through this loop twice. The first time, Assume_Valid is set False
10640 -- in the call to Compile_Time_Compare. If this call results in a
10641 -- clear result of always True or Always False, that's decisive and
10642 -- we are done. Otherwise we repeat the processing with Assume_Valid
10643 -- set to True to generate additional warnings. We can skip that step
10644 -- if Constant_Condition_Warnings is False.
10645
10646 for AV in False .. True loop
10647 declare
10648 Typ : constant Entity_Id := Etype (N);
10649 Op1 : constant Node_Id := Left_Opnd (N);
10650 Op2 : constant Node_Id := Right_Opnd (N);
10651
10652 Res : constant Compare_Result :=
10653 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
10654 -- Res indicates if compare outcome can be compile time determined
10655
10656 True_Result : Boolean;
10657 False_Result : Boolean;
10658
10659 begin
10660 case N_Op_Compare (Nkind (N)) is
10661 when N_Op_Eq =>
10662 True_Result := Res = EQ;
10663 False_Result := Res = LT or else Res = GT or else Res = NE;
10664
10665 when N_Op_Ge =>
10666 True_Result := Res in Compare_GE;
10667 False_Result := Res = LT;
10668
10669 if Res = LE
10670 and then Constant_Condition_Warnings
10671 and then Comes_From_Source (Original_Node (N))
10672 and then Nkind (Original_Node (N)) = N_Op_Ge
10673 and then not In_Instance
10674 and then Is_Integer_Type (Etype (Left_Opnd (N)))
10675 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
10676 then
10677 Error_Msg_N
10678 ("can never be greater than, could replace by ""'=""?", N);
10679 Warning_Generated := True;
10680 end if;
10681
10682 when N_Op_Gt =>
10683 True_Result := Res = GT;
10684 False_Result := Res in Compare_LE;
10685
10686 when N_Op_Lt =>
10687 True_Result := Res = LT;
10688 False_Result := Res in Compare_GE;
10689
10690 when N_Op_Le =>
10691 True_Result := Res in Compare_LE;
10692 False_Result := Res = GT;
10693
10694 if Res = GE
10695 and then Constant_Condition_Warnings
10696 and then Comes_From_Source (Original_Node (N))
10697 and then Nkind (Original_Node (N)) = N_Op_Le
10698 and then not In_Instance
10699 and then Is_Integer_Type (Etype (Left_Opnd (N)))
10700 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
10701 then
10702 Error_Msg_N
10703 ("can never be less than, could replace by ""'=""?", N);
10704 Warning_Generated := True;
10705 end if;
10706
10707 when N_Op_Ne =>
10708 True_Result := Res = NE or else Res = GT or else Res = LT;
10709 False_Result := Res = EQ;
10710 end case;
10711
10712 -- If this is the first iteration, then we actually convert the
10713 -- comparison into True or False, if the result is certain.
10714
10715 if AV = False then
10716 if True_Result or False_Result then
10717 if True_Result then
10718 Result := Standard_True;
10719 else
10720 Result := Standard_False;
10721 end if;
10722
10723 Rewrite (N,
10724 Convert_To (Typ,
10725 New_Occurrence_Of (Result, Sloc (N))));
10726 Analyze_And_Resolve (N, Typ);
10727 Warn_On_Known_Condition (N);
10728 return;
10729 end if;
10730
10731 -- If this is the second iteration (AV = True), and the original
10732 -- node comes from source and we are not in an instance, then give
10733 -- a warning if we know result would be True or False. Note: we
10734 -- know Constant_Condition_Warnings is set if we get here.
10735
10736 elsif Comes_From_Source (Original_Node (N))
10737 and then not In_Instance
10738 then
10739 if True_Result then
10740 Error_Msg_N
10741 ("condition can only be False if invalid values present?",
10742 N);
10743 elsif False_Result then
10744 Error_Msg_N
10745 ("condition can only be True if invalid values present?",
10746 N);
10747 end if;
10748 end if;
10749 end;
10750
10751 -- Skip second iteration if not warning on constant conditions or
10752 -- if the first iteration already generated a warning of some kind or
10753 -- if we are in any case assuming all values are valid (so that the
10754 -- first iteration took care of the valid case).
10755
10756 exit when not Constant_Condition_Warnings;
10757 exit when Warning_Generated;
10758 exit when Assume_No_Invalid_Values;
10759 end loop;
10760 end Rewrite_Comparison;
10761
10762 ----------------------------
10763 -- Safe_In_Place_Array_Op --
10764 ----------------------------
10765
10766 function Safe_In_Place_Array_Op
10767 (Lhs : Node_Id;
10768 Op1 : Node_Id;
10769 Op2 : Node_Id) return Boolean
10770 is
10771 Target : Entity_Id;
10772
10773 function Is_Safe_Operand (Op : Node_Id) return Boolean;
10774 -- Operand is safe if it cannot overlap part of the target of the
10775 -- operation. If the operand and the target are identical, the operand
10776 -- is safe. The operand can be empty in the case of negation.
10777
10778 function Is_Unaliased (N : Node_Id) return Boolean;
10779 -- Check that N is a stand-alone entity
10780
10781 ------------------
10782 -- Is_Unaliased --
10783 ------------------
10784
10785 function Is_Unaliased (N : Node_Id) return Boolean is
10786 begin
10787 return
10788 Is_Entity_Name (N)
10789 and then No (Address_Clause (Entity (N)))
10790 and then No (Renamed_Object (Entity (N)));
10791 end Is_Unaliased;
10792
10793 ---------------------
10794 -- Is_Safe_Operand --
10795 ---------------------
10796
10797 function Is_Safe_Operand (Op : Node_Id) return Boolean is
10798 begin
10799 if No (Op) then
10800 return True;
10801
10802 elsif Is_Entity_Name (Op) then
10803 return Is_Unaliased (Op);
10804
10805 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
10806 return Is_Unaliased (Prefix (Op));
10807
10808 elsif Nkind (Op) = N_Slice then
10809 return
10810 Is_Unaliased (Prefix (Op))
10811 and then Entity (Prefix (Op)) /= Target;
10812
10813 elsif Nkind (Op) = N_Op_Not then
10814 return Is_Safe_Operand (Right_Opnd (Op));
10815
10816 else
10817 return False;
10818 end if;
10819 end Is_Safe_Operand;
10820
10821 -- Start of processing for Is_Safe_In_Place_Array_Op
10822
10823 begin
10824 -- Skip this processing if the component size is different from system
10825 -- storage unit (since at least for NOT this would cause problems).
10826
10827 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
10828 return False;
10829
10830 -- Cannot do in place stuff on VM_Target since cannot pass addresses
10831
10832 elsif VM_Target /= No_VM then
10833 return False;
10834
10835 -- Cannot do in place stuff if non-standard Boolean representation
10836
10837 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
10838 return False;
10839
10840 elsif not Is_Unaliased (Lhs) then
10841 return False;
10842
10843 else
10844 Target := Entity (Lhs);
10845 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
10846 end if;
10847 end Safe_In_Place_Array_Op;
10848
10849 -----------------------
10850 -- Tagged_Membership --
10851 -----------------------
10852
10853 -- There are two different cases to consider depending on whether the right
10854 -- operand is a class-wide type or not. If not we just compare the actual
10855 -- tag of the left expr to the target type tag:
10856 --
10857 -- Left_Expr.Tag = Right_Type'Tag;
10858 --
10859 -- If it is a class-wide type we use the RT function CW_Membership which is
10860 -- usually implemented by looking in the ancestor tables contained in the
10861 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
10862
10863 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
10864 -- function IW_Membership which is usually implemented by looking in the
10865 -- table of abstract interface types plus the ancestor table contained in
10866 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
10867
10868 procedure Tagged_Membership
10869 (N : Node_Id;
10870 SCIL_Node : out Node_Id;
10871 Result : out Node_Id)
10872 is
10873 Left : constant Node_Id := Left_Opnd (N);
10874 Right : constant Node_Id := Right_Opnd (N);
10875 Loc : constant Source_Ptr := Sloc (N);
10876
10877 Full_R_Typ : Entity_Id;
10878 Left_Type : Entity_Id;
10879 New_Node : Node_Id;
10880 Right_Type : Entity_Id;
10881 Obj_Tag : Node_Id;
10882
10883 begin
10884 SCIL_Node := Empty;
10885
10886 -- Handle entities from the limited view
10887
10888 Left_Type := Available_View (Etype (Left));
10889 Right_Type := Available_View (Etype (Right));
10890
10891 if Is_Class_Wide_Type (Left_Type) then
10892 Left_Type := Root_Type (Left_Type);
10893 end if;
10894
10895 if Is_Class_Wide_Type (Right_Type) then
10896 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
10897 else
10898 Full_R_Typ := Underlying_Type (Right_Type);
10899 end if;
10900
10901 Obj_Tag :=
10902 Make_Selected_Component (Loc,
10903 Prefix => Relocate_Node (Left),
10904 Selector_Name =>
10905 New_Reference_To (First_Tag_Component (Left_Type), Loc));
10906
10907 if Is_Class_Wide_Type (Right_Type) then
10908
10909 -- No need to issue a run-time check if we statically know that the
10910 -- result of this membership test is always true. For example,
10911 -- considering the following declarations:
10912
10913 -- type Iface is interface;
10914 -- type T is tagged null record;
10915 -- type DT is new T and Iface with null record;
10916
10917 -- Obj1 : T;
10918 -- Obj2 : DT;
10919
10920 -- These membership tests are always true:
10921
10922 -- Obj1 in T'Class
10923 -- Obj2 in T'Class;
10924 -- Obj2 in Iface'Class;
10925
10926 -- We do not need to handle cases where the membership is illegal.
10927 -- For example:
10928
10929 -- Obj1 in DT'Class; -- Compile time error
10930 -- Obj1 in Iface'Class; -- Compile time error
10931
10932 if not Is_Class_Wide_Type (Left_Type)
10933 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
10934 Use_Full_View => True)
10935 or else (Is_Interface (Etype (Right_Type))
10936 and then Interface_Present_In_Ancestor
10937 (Typ => Left_Type,
10938 Iface => Etype (Right_Type))))
10939 then
10940 Result := New_Reference_To (Standard_True, Loc);
10941 return;
10942 end if;
10943
10944 -- Ada 2005 (AI-251): Class-wide applied to interfaces
10945
10946 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
10947
10948 -- Support to: "Iface_CW_Typ in Typ'Class"
10949
10950 or else Is_Interface (Left_Type)
10951 then
10952 -- Issue error if IW_Membership operation not available in a
10953 -- configurable run time setting.
10954
10955 if not RTE_Available (RE_IW_Membership) then
10956 Error_Msg_CRT
10957 ("dynamic membership test on interface types", N);
10958 Result := Empty;
10959 return;
10960 end if;
10961
10962 Result :=
10963 Make_Function_Call (Loc,
10964 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
10965 Parameter_Associations => New_List (
10966 Make_Attribute_Reference (Loc,
10967 Prefix => Obj_Tag,
10968 Attribute_Name => Name_Address),
10969 New_Reference_To (
10970 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
10971 Loc)));
10972
10973 -- Ada 95: Normal case
10974
10975 else
10976 Build_CW_Membership (Loc,
10977 Obj_Tag_Node => Obj_Tag,
10978 Typ_Tag_Node =>
10979 New_Reference_To (
10980 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
10981 Related_Nod => N,
10982 New_Node => New_Node);
10983
10984 -- Generate the SCIL node for this class-wide membership test.
10985 -- Done here because the previous call to Build_CW_Membership
10986 -- relocates Obj_Tag.
10987
10988 if Generate_SCIL then
10989 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
10990 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
10991 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
10992 end if;
10993
10994 Result := New_Node;
10995 end if;
10996
10997 -- Right_Type is not a class-wide type
10998
10999 else
11000 -- No need to check the tag of the object if Right_Typ is abstract
11001
11002 if Is_Abstract_Type (Right_Type) then
11003 Result := New_Reference_To (Standard_False, Loc);
11004
11005 else
11006 Result :=
11007 Make_Op_Eq (Loc,
11008 Left_Opnd => Obj_Tag,
11009 Right_Opnd =>
11010 New_Reference_To
11011 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
11012 end if;
11013 end if;
11014 end Tagged_Membership;
11015
11016 ------------------------------
11017 -- Unary_Op_Validity_Checks --
11018 ------------------------------
11019
11020 procedure Unary_Op_Validity_Checks (N : Node_Id) is
11021 begin
11022 if Validity_Checks_On and Validity_Check_Operands then
11023 Ensure_Valid (Right_Opnd (N));
11024 end if;
11025 end Unary_Op_Validity_Checks;
11026
11027 end Exp_Ch4;
This page took 0.563222 seconds and 5 git commands to generate.