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