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