[multiple changes]
[gcc.git] / gcc / ada / exp_util.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2012, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Exp_Aggr; use Exp_Aggr;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Inline; use Inline;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Prag; use Sem_Prag;
50 with Sem_Res; use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sem_Util; use Sem_Util;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Stringt; use Stringt;
56 with Targparm; use Targparm;
57 with Tbuild; use Tbuild;
58 with Ttypes; use Ttypes;
59 with Urealp; use Urealp;
60 with Validsw; use Validsw;
61
62 package body Exp_Util is
63
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
67
68 function Build_Task_Array_Image
69 (Loc : Source_Ptr;
70 Id_Ref : Node_Id;
71 A_Type : Entity_Id;
72 Dyn : Boolean := False) return Node_Id;
73 -- Build function to generate the image string for a task that is an array
74 -- component, concatenating the images of each index. To avoid storage
75 -- leaks, the string is built with successive slice assignments. The flag
76 -- Dyn indicates whether this is called for the initialization procedure of
77 -- an array of tasks, or for the name of a dynamically created task that is
78 -- assigned to an indexed component.
79
80 function Build_Task_Image_Function
81 (Loc : Source_Ptr;
82 Decls : List_Id;
83 Stats : List_Id;
84 Res : Entity_Id) return Node_Id;
85 -- Common processing for Task_Array_Image and Task_Record_Image. Build
86 -- function body that computes image.
87
88 procedure Build_Task_Image_Prefix
89 (Loc : Source_Ptr;
90 Len : out Entity_Id;
91 Res : out Entity_Id;
92 Pos : out Entity_Id;
93 Prefix : Entity_Id;
94 Sum : Node_Id;
95 Decls : List_Id;
96 Stats : List_Id);
97 -- Common processing for Task_Array_Image and Task_Record_Image. Create
98 -- local variables and assign prefix of name to result string.
99
100 function Build_Task_Record_Image
101 (Loc : Source_Ptr;
102 Id_Ref : Node_Id;
103 Dyn : Boolean := False) return Node_Id;
104 -- Build function to generate the image string for a task that is a record
105 -- component. Concatenate name of variable with that of selector. The flag
106 -- Dyn indicates whether this is called for the initialization procedure of
107 -- record with task components, or for a dynamically created task that is
108 -- assigned to a selected component.
109
110 function Make_CW_Equivalent_Type
111 (T : Entity_Id;
112 E : Node_Id) return Entity_Id;
113 -- T is a class-wide type entity, E is the initial expression node that
114 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
115 -- returns the entity of the Equivalent type and inserts on the fly the
116 -- necessary declaration such as:
117 --
118 -- type anon is record
119 -- _parent : Root_Type (T); constrained with E discriminants (if any)
120 -- Extension : String (1 .. expr to match size of E);
121 -- end record;
122 --
123 -- This record is compatible with any object of the class of T thanks to
124 -- the first field and has the same size as E thanks to the second.
125
126 function Make_Literal_Range
127 (Loc : Source_Ptr;
128 Literal_Typ : Entity_Id) return Node_Id;
129 -- Produce a Range node whose bounds are:
130 -- Low_Bound (Literal_Type) ..
131 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
132 -- this is used for expanding declarations like X : String := "sdfgdfg";
133 --
134 -- If the index type of the target array is not integer, we generate:
135 -- Low_Bound (Literal_Type) ..
136 -- Literal_Type'Val
137 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
138 -- + (Length (Literal_Typ) -1))
139
140 function Make_Non_Empty_Check
141 (Loc : Source_Ptr;
142 N : Node_Id) return Node_Id;
143 -- Produce a boolean expression checking that the unidimensional array
144 -- node N is not empty.
145
146 function New_Class_Wide_Subtype
147 (CW_Typ : Entity_Id;
148 N : Node_Id) return Entity_Id;
149 -- Create an implicit subtype of CW_Typ attached to node N
150
151 function Requires_Cleanup_Actions
152 (L : List_Id;
153 Lib_Level : Boolean;
154 Nested_Constructs : Boolean) return Boolean;
155 -- Given a list L, determine whether it contains one of the following:
156 --
157 -- 1) controlled objects
158 -- 2) library-level tagged types
159 --
160 -- Lib_Level is True when the list comes from a construct at the library
161 -- level, and False otherwise. Nested_Constructs is True when any nested
162 -- packages declared in L must be processed, and False otherwise.
163
164 -------------------------------------
165 -- Activate_Atomic_Synchronization --
166 -------------------------------------
167
168 procedure Activate_Atomic_Synchronization (N : Node_Id) is
169 Msg_Node : Node_Id;
170
171 begin
172 case Nkind (Parent (N)) is
173
174 -- Check for cases of appearing in the prefix of a construct where
175 -- we don't need atomic synchronization for this kind of usage.
176
177 when
178 -- Nothing to do if we are the prefix of an attribute, since we
179 -- do not want an atomic sync operation for things like 'Size.
180
181 N_Attribute_Reference |
182
183 -- The N_Reference node is like an attribute
184
185 N_Reference |
186
187 -- Nothing to do for a reference to a component (or components)
188 -- of a composite object. Only reads and updates of the object
189 -- as a whole require atomic synchronization (RM C.6 (15)).
190
191 N_Indexed_Component |
192 N_Selected_Component |
193 N_Slice =>
194
195 -- For all the above cases, nothing to do if we are the prefix
196
197 if Prefix (Parent (N)) = N then
198 return;
199 end if;
200
201 when others => null;
202 end case;
203
204 -- Go ahead and set the flag
205
206 Set_Atomic_Sync_Required (N);
207
208 -- Generate info message if requested
209
210 if Warn_On_Atomic_Synchronization then
211 case Nkind (N) is
212 when N_Identifier =>
213 Msg_Node := N;
214
215 when N_Selected_Component | N_Expanded_Name =>
216 Msg_Node := Selector_Name (N);
217
218 when N_Explicit_Dereference | N_Indexed_Component =>
219 Msg_Node := Empty;
220
221 when others =>
222 pragma Assert (False);
223 return;
224 end case;
225
226 if Present (Msg_Node) then
227 Error_Msg_N
228 ("?n?info: atomic synchronization set for &", Msg_Node);
229 else
230 Error_Msg_N
231 ("?n?info: atomic synchronization set", N);
232 end if;
233 end if;
234 end Activate_Atomic_Synchronization;
235
236 ----------------------
237 -- Adjust_Condition --
238 ----------------------
239
240 procedure Adjust_Condition (N : Node_Id) is
241 begin
242 if No (N) then
243 return;
244 end if;
245
246 declare
247 Loc : constant Source_Ptr := Sloc (N);
248 T : constant Entity_Id := Etype (N);
249 Ti : Entity_Id;
250
251 begin
252 -- Defend against a call where the argument has no type, or has a
253 -- type that is not Boolean. This can occur because of prior errors.
254
255 if No (T) or else not Is_Boolean_Type (T) then
256 return;
257 end if;
258
259 -- Apply validity checking if needed
260
261 if Validity_Checks_On and Validity_Check_Tests then
262 Ensure_Valid (N);
263 end if;
264
265 -- Immediate return if standard boolean, the most common case,
266 -- where nothing needs to be done.
267
268 if Base_Type (T) = Standard_Boolean then
269 return;
270 end if;
271
272 -- Case of zero/non-zero semantics or non-standard enumeration
273 -- representation. In each case, we rewrite the node as:
274
275 -- ityp!(N) /= False'Enum_Rep
276
277 -- where ityp is an integer type with large enough size to hold any
278 -- value of type T.
279
280 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
281 if Esize (T) <= Esize (Standard_Integer) then
282 Ti := Standard_Integer;
283 else
284 Ti := Standard_Long_Long_Integer;
285 end if;
286
287 Rewrite (N,
288 Make_Op_Ne (Loc,
289 Left_Opnd => Unchecked_Convert_To (Ti, N),
290 Right_Opnd =>
291 Make_Attribute_Reference (Loc,
292 Attribute_Name => Name_Enum_Rep,
293 Prefix =>
294 New_Occurrence_Of (First_Literal (T), Loc))));
295 Analyze_And_Resolve (N, Standard_Boolean);
296
297 else
298 Rewrite (N, Convert_To (Standard_Boolean, N));
299 Analyze_And_Resolve (N, Standard_Boolean);
300 end if;
301 end;
302 end Adjust_Condition;
303
304 ------------------------
305 -- Adjust_Result_Type --
306 ------------------------
307
308 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
309 begin
310 -- Ignore call if current type is not Standard.Boolean
311
312 if Etype (N) /= Standard_Boolean then
313 return;
314 end if;
315
316 -- If result is already of correct type, nothing to do. Note that
317 -- this will get the most common case where everything has a type
318 -- of Standard.Boolean.
319
320 if Base_Type (T) = Standard_Boolean then
321 return;
322
323 else
324 declare
325 KP : constant Node_Kind := Nkind (Parent (N));
326
327 begin
328 -- If result is to be used as a Condition in the syntax, no need
329 -- to convert it back, since if it was changed to Standard.Boolean
330 -- using Adjust_Condition, that is just fine for this usage.
331
332 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
333 return;
334
335 -- If result is an operand of another logical operation, no need
336 -- to reset its type, since Standard.Boolean is just fine, and
337 -- such operations always do Adjust_Condition on their operands.
338
339 elsif KP in N_Op_Boolean
340 or else KP in N_Short_Circuit
341 or else KP = N_Op_Not
342 then
343 return;
344
345 -- Otherwise we perform a conversion from the current type, which
346 -- must be Standard.Boolean, to the desired type.
347
348 else
349 Set_Analyzed (N);
350 Rewrite (N, Convert_To (T, N));
351 Analyze_And_Resolve (N, T);
352 end if;
353 end;
354 end if;
355 end Adjust_Result_Type;
356
357 --------------------------
358 -- Append_Freeze_Action --
359 --------------------------
360
361 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
362 Fnode : Node_Id;
363
364 begin
365 Ensure_Freeze_Node (T);
366 Fnode := Freeze_Node (T);
367
368 if No (Actions (Fnode)) then
369 Set_Actions (Fnode, New_List);
370 end if;
371
372 Append (N, Actions (Fnode));
373 end Append_Freeze_Action;
374
375 ---------------------------
376 -- Append_Freeze_Actions --
377 ---------------------------
378
379 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
380 Fnode : constant Node_Id := Freeze_Node (T);
381
382 begin
383 if No (L) then
384 return;
385
386 else
387 if No (Actions (Fnode)) then
388 Set_Actions (Fnode, L);
389 else
390 Append_List (L, Actions (Fnode));
391 end if;
392 end if;
393 end Append_Freeze_Actions;
394
395 ------------------------------------
396 -- Build_Allocate_Deallocate_Proc --
397 ------------------------------------
398
399 procedure Build_Allocate_Deallocate_Proc
400 (N : Node_Id;
401 Is_Allocate : Boolean)
402 is
403 Desig_Typ : Entity_Id;
404 Expr : Node_Id;
405 Pool_Id : Entity_Id;
406 Proc_To_Call : Node_Id := Empty;
407 Ptr_Typ : Entity_Id;
408
409 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id;
410 -- Locate TSS primitive Finalize_Address in type Typ
411
412 function Find_Object (E : Node_Id) return Node_Id;
413 -- Given an arbitrary expression of an allocator, try to find an object
414 -- reference in it, otherwise return the original expression.
415
416 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
417 -- Determine whether subprogram Subp denotes a custom allocate or
418 -- deallocate.
419
420 ---------------------------
421 -- Find_Finalize_Address --
422 ---------------------------
423
424 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id is
425 Utyp : Entity_Id := Typ;
426
427 begin
428 -- Handle protected class-wide or task class-wide types
429
430 if Is_Class_Wide_Type (Utyp) then
431 if Is_Concurrent_Type (Root_Type (Utyp)) then
432 Utyp := Root_Type (Utyp);
433
434 elsif Is_Private_Type (Root_Type (Utyp))
435 and then Present (Full_View (Root_Type (Utyp)))
436 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
437 then
438 Utyp := Full_View (Root_Type (Utyp));
439 end if;
440 end if;
441
442 -- Handle private types
443
444 if Is_Private_Type (Utyp)
445 and then Present (Full_View (Utyp))
446 then
447 Utyp := Full_View (Utyp);
448 end if;
449
450 -- Handle protected and task types
451
452 if Is_Concurrent_Type (Utyp)
453 and then Present (Corresponding_Record_Type (Utyp))
454 then
455 Utyp := Corresponding_Record_Type (Utyp);
456 end if;
457
458 Utyp := Underlying_Type (Base_Type (Utyp));
459
460 -- Deal with non-tagged derivation of private views. If the parent is
461 -- now known to be protected, the finalization routine is the one
462 -- defined on the corresponding record of the ancestor (corresponding
463 -- records do not automatically inherit operations, but maybe they
464 -- should???)
465
466 if Is_Untagged_Derivation (Typ) then
467 if Is_Protected_Type (Typ) then
468 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
469 else
470 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
471
472 if Is_Protected_Type (Utyp) then
473 Utyp := Corresponding_Record_Type (Utyp);
474 end if;
475 end if;
476 end if;
477
478 -- If the underlying_type is a subtype, we are dealing with the
479 -- completion of a private type. We need to access the base type and
480 -- generate a conversion to it.
481
482 if Utyp /= Base_Type (Utyp) then
483 pragma Assert (Is_Private_Type (Typ));
484
485 Utyp := Base_Type (Utyp);
486 end if;
487
488 -- When dealing with an internally built full view for a type with
489 -- unknown discriminants, use the original record type.
490
491 if Is_Underlying_Record_View (Utyp) then
492 Utyp := Etype (Utyp);
493 end if;
494
495 return TSS (Utyp, TSS_Finalize_Address);
496 end Find_Finalize_Address;
497
498 -----------------
499 -- Find_Object --
500 -----------------
501
502 function Find_Object (E : Node_Id) return Node_Id is
503 Expr : Node_Id;
504
505 begin
506 pragma Assert (Is_Allocate);
507
508 Expr := E;
509 loop
510 if Nkind_In (Expr, N_Qualified_Expression,
511 N_Unchecked_Type_Conversion)
512 then
513 Expr := Expression (Expr);
514
515 elsif Nkind (Expr) = N_Explicit_Dereference then
516 Expr := Prefix (Expr);
517
518 else
519 exit;
520 end if;
521 end loop;
522
523 return Expr;
524 end Find_Object;
525
526 ---------------------------------
527 -- Is_Allocate_Deallocate_Proc --
528 ---------------------------------
529
530 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
531 begin
532 -- Look for a subprogram body with only one statement which is a
533 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
534
535 if Ekind (Subp) = E_Procedure
536 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
537 then
538 declare
539 HSS : constant Node_Id :=
540 Handled_Statement_Sequence (Parent (Parent (Subp)));
541 Proc : Entity_Id;
542
543 begin
544 if Present (Statements (HSS))
545 and then Nkind (First (Statements (HSS))) =
546 N_Procedure_Call_Statement
547 then
548 Proc := Entity (Name (First (Statements (HSS))));
549
550 return
551 Is_RTE (Proc, RE_Allocate_Any_Controlled)
552 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
553 end if;
554 end;
555 end if;
556
557 return False;
558 end Is_Allocate_Deallocate_Proc;
559
560 -- Start of processing for Build_Allocate_Deallocate_Proc
561
562 begin
563 -- Do not perform this expansion in Alfa mode because it is not
564 -- necessary.
565
566 if Alfa_Mode then
567 return;
568 end if;
569
570 -- Obtain the attributes of the allocation / deallocation
571
572 if Nkind (N) = N_Free_Statement then
573 Expr := Expression (N);
574 Ptr_Typ := Base_Type (Etype (Expr));
575 Proc_To_Call := Procedure_To_Call (N);
576
577 else
578 if Nkind (N) = N_Object_Declaration then
579 Expr := Expression (N);
580 else
581 Expr := N;
582 end if;
583
584 -- In certain cases an allocator with a qualified expression may
585 -- be relocated and used as the initialization expression of a
586 -- temporary:
587
588 -- before:
589 -- Obj : Ptr_Typ := new Desig_Typ'(...);
590
591 -- after:
592 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
593 -- Obj : Ptr_Typ := Tmp;
594
595 -- Since the allocator is always marked as analyzed to avoid infinite
596 -- expansion, it will never be processed by this routine given that
597 -- the designated type needs finalization actions. Detect this case
598 -- and complete the expansion of the allocator.
599
600 if Nkind (Expr) = N_Identifier
601 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
602 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
603 then
604 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
605 return;
606 end if;
607
608 -- The allocator may have been rewritten into something else in which
609 -- case the expansion performed by this routine does not apply.
610
611 if Nkind (Expr) /= N_Allocator then
612 return;
613 end if;
614
615 Ptr_Typ := Base_Type (Etype (Expr));
616 Proc_To_Call := Procedure_To_Call (Expr);
617 end if;
618
619 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
620 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
621
622 -- Handle concurrent types
623
624 if Is_Concurrent_Type (Desig_Typ)
625 and then Present (Corresponding_Record_Type (Desig_Typ))
626 then
627 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
628 end if;
629
630 -- Do not process allocations / deallocations without a pool
631
632 if No (Pool_Id) then
633 return;
634
635 -- Do not process allocations on / deallocations from the secondary
636 -- stack.
637
638 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
639 return;
640
641 -- Do not replicate the machinery if the allocator / free has already
642 -- been expanded and has a custom Allocate / Deallocate.
643
644 elsif Present (Proc_To_Call)
645 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
646 then
647 return;
648 end if;
649
650 if Needs_Finalization (Desig_Typ) then
651
652 -- Certain run-time configurations and targets do not provide support
653 -- for controlled types.
654
655 if Restriction_Active (No_Finalization) then
656 return;
657
658 -- Do nothing if the access type may never allocate / deallocate
659 -- objects.
660
661 elsif No_Pool_Assigned (Ptr_Typ) then
662 return;
663
664 -- Access-to-controlled types are not supported on .NET/JVM since
665 -- these targets cannot support pools and address arithmetic.
666
667 elsif VM_Target /= No_VM then
668 return;
669 end if;
670
671 -- The allocation / deallocation of a controlled object must be
672 -- chained on / detached from a finalization master.
673
674 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
675
676 -- The only other kind of allocation / deallocation supported by this
677 -- routine is on / from a subpool.
678
679 elsif Nkind (Expr) = N_Allocator
680 and then No (Subpool_Handle_Name (Expr))
681 then
682 return;
683 end if;
684
685 declare
686 Loc : constant Source_Ptr := Sloc (N);
687 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
688 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
689 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
690 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
691
692 Actuals : List_Id;
693 Fin_Addr_Id : Entity_Id;
694 Fin_Mas_Act : Node_Id;
695 Fin_Mas_Id : Entity_Id;
696 Proc_To_Call : Entity_Id;
697 Subpool : Node_Id := Empty;
698
699 begin
700 -- Step 1: Construct all the actuals for the call to library routine
701 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
702
703 -- a) Storage pool
704
705 Actuals := New_List (New_Reference_To (Pool_Id, Loc));
706
707 if Is_Allocate then
708
709 -- b) Subpool
710
711 if Nkind (Expr) = N_Allocator then
712 Subpool := Subpool_Handle_Name (Expr);
713 end if;
714
715 -- If a subpool is present it can be an arbitrary name, so make
716 -- the actual by copying the tree.
717
718 if Present (Subpool) then
719 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
720 else
721 Append_To (Actuals, Make_Null (Loc));
722 end if;
723
724 -- c) Finalization master
725
726 if Needs_Finalization (Desig_Typ) then
727 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
728 Fin_Mas_Act := New_Reference_To (Fin_Mas_Id, Loc);
729
730 -- Handle the case where the master is actually a pointer to a
731 -- master. This case arises in build-in-place functions.
732
733 if Is_Access_Type (Etype (Fin_Mas_Id)) then
734 Append_To (Actuals, Fin_Mas_Act);
735 else
736 Append_To (Actuals,
737 Make_Attribute_Reference (Loc,
738 Prefix => Fin_Mas_Act,
739 Attribute_Name => Name_Unrestricted_Access));
740 end if;
741 else
742 Append_To (Actuals, Make_Null (Loc));
743 end if;
744
745 -- d) Finalize_Address
746
747 -- Primitive Finalize_Address is never generated in CodePeer mode
748 -- since it contains an Unchecked_Conversion.
749
750 if Needs_Finalization (Desig_Typ)
751 and then not CodePeer_Mode
752 then
753 Fin_Addr_Id := Find_Finalize_Address (Desig_Typ);
754 pragma Assert (Present (Fin_Addr_Id));
755
756 Append_To (Actuals,
757 Make_Attribute_Reference (Loc,
758 Prefix => New_Reference_To (Fin_Addr_Id, Loc),
759 Attribute_Name => Name_Unrestricted_Access));
760 else
761 Append_To (Actuals, Make_Null (Loc));
762 end if;
763 end if;
764
765 -- e) Address
766 -- f) Storage_Size
767 -- g) Alignment
768
769 Append_To (Actuals, New_Reference_To (Addr_Id, Loc));
770 Append_To (Actuals, New_Reference_To (Size_Id, Loc));
771
772 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
773 Append_To (Actuals, New_Reference_To (Alig_Id, Loc));
774
775 -- For deallocation of class wide types we obtain the value of
776 -- alignment from the Type Specific Record of the deallocated object.
777 -- This is needed because the frontend expansion of class-wide types
778 -- into equivalent types confuses the backend.
779
780 else
781 -- Generate:
782 -- Obj.all'Alignment
783
784 -- ... because 'Alignment applied to class-wide types is expanded
785 -- into the code that reads the value of alignment from the TSD
786 -- (see Expand_N_Attribute_Reference)
787
788 Append_To (Actuals,
789 Unchecked_Convert_To (RTE (RE_Storage_Offset),
790 Make_Attribute_Reference (Loc,
791 Prefix =>
792 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
793 Attribute_Name => Name_Alignment)));
794 end if;
795
796 -- h) Is_Controlled
797
798 -- Generate a run-time check to determine whether a class-wide object
799 -- is truly controlled.
800
801 if Needs_Finalization (Desig_Typ) then
802 if Is_Class_Wide_Type (Desig_Typ)
803 or else Is_Generic_Actual_Type (Desig_Typ)
804 then
805 declare
806 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
807 Flag_Expr : Node_Id;
808 Param : Node_Id;
809 Temp : Node_Id;
810
811 begin
812 if Is_Allocate then
813 Temp := Find_Object (Expression (Expr));
814 else
815 Temp := Expr;
816 end if;
817
818 -- Processing for generic actuals
819
820 if Is_Generic_Actual_Type (Desig_Typ) then
821 Flag_Expr :=
822 New_Reference_To (Boolean_Literals
823 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
824
825 -- Processing for subtype indications
826
827 elsif Nkind (Temp) in N_Has_Entity
828 and then Is_Type (Entity (Temp))
829 then
830 Flag_Expr :=
831 New_Reference_To (Boolean_Literals
832 (Needs_Finalization (Entity (Temp))), Loc);
833
834 -- Generate a runtime check to test the controlled state of
835 -- an object for the purposes of allocation / deallocation.
836
837 else
838 -- The following case arises when allocating through an
839 -- interface class-wide type, generate:
840 --
841 -- Temp.all
842
843 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
844 Param :=
845 Make_Explicit_Dereference (Loc,
846 Prefix =>
847 Relocate_Node (Temp));
848
849 -- Generate:
850 -- Temp'Tag
851
852 else
853 Param :=
854 Make_Attribute_Reference (Loc,
855 Prefix =>
856 Relocate_Node (Temp),
857 Attribute_Name => Name_Tag);
858 end if;
859
860 -- Generate:
861 -- Needs_Finalization (<Param>)
862
863 Flag_Expr :=
864 Make_Function_Call (Loc,
865 Name =>
866 New_Reference_To (RTE (RE_Needs_Finalization), Loc),
867 Parameter_Associations => New_List (Param));
868 end if;
869
870 -- Create the temporary which represents the finalization
871 -- state of the expression. Generate:
872 --
873 -- F : constant Boolean := <Flag_Expr>;
874
875 Insert_Action (N,
876 Make_Object_Declaration (Loc,
877 Defining_Identifier => Flag_Id,
878 Constant_Present => True,
879 Object_Definition =>
880 New_Reference_To (Standard_Boolean, Loc),
881 Expression => Flag_Expr));
882
883 -- The flag acts as the last actual
884
885 Append_To (Actuals, New_Reference_To (Flag_Id, Loc));
886 end;
887
888 -- The object is statically known to be controlled
889
890 else
891 Append_To (Actuals, New_Reference_To (Standard_True, Loc));
892 end if;
893
894 else
895 Append_To (Actuals, New_Reference_To (Standard_False, Loc));
896 end if;
897
898 -- i) On_Subpool
899
900 if Is_Allocate then
901 Append_To (Actuals,
902 New_Reference_To (Boolean_Literals (Present (Subpool)), Loc));
903 end if;
904
905 -- Step 2: Build a wrapper Allocate / Deallocate which internally
906 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
907
908 -- Select the proper routine to call
909
910 if Is_Allocate then
911 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
912 else
913 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
914 end if;
915
916 -- Create a custom Allocate / Deallocate routine which has identical
917 -- profile to that of System.Storage_Pools.
918
919 Insert_Action (N,
920 Make_Subprogram_Body (Loc,
921 Specification =>
922
923 -- procedure Pnn
924
925 Make_Procedure_Specification (Loc,
926 Defining_Unit_Name => Proc_Id,
927 Parameter_Specifications => New_List (
928
929 -- P : Root_Storage_Pool
930
931 Make_Parameter_Specification (Loc,
932 Defining_Identifier => Make_Temporary (Loc, 'P'),
933 Parameter_Type =>
934 New_Reference_To (RTE (RE_Root_Storage_Pool), Loc)),
935
936 -- A : [out] Address
937
938 Make_Parameter_Specification (Loc,
939 Defining_Identifier => Addr_Id,
940 Out_Present => Is_Allocate,
941 Parameter_Type =>
942 New_Reference_To (RTE (RE_Address), Loc)),
943
944 -- S : Storage_Count
945
946 Make_Parameter_Specification (Loc,
947 Defining_Identifier => Size_Id,
948 Parameter_Type =>
949 New_Reference_To (RTE (RE_Storage_Count), Loc)),
950
951 -- L : Storage_Count
952
953 Make_Parameter_Specification (Loc,
954 Defining_Identifier => Alig_Id,
955 Parameter_Type =>
956 New_Reference_To (RTE (RE_Storage_Count), Loc)))),
957
958 Declarations => No_List,
959
960 Handled_Statement_Sequence =>
961 Make_Handled_Sequence_Of_Statements (Loc,
962 Statements => New_List (
963 Make_Procedure_Call_Statement (Loc,
964 Name => New_Reference_To (Proc_To_Call, Loc),
965 Parameter_Associations => Actuals)))));
966
967 -- The newly generated Allocate / Deallocate becomes the default
968 -- procedure to call when the back end processes the allocation /
969 -- deallocation.
970
971 if Is_Allocate then
972 Set_Procedure_To_Call (Expr, Proc_Id);
973 else
974 Set_Procedure_To_Call (N, Proc_Id);
975 end if;
976 end;
977 end Build_Allocate_Deallocate_Proc;
978
979 ------------------------
980 -- Build_Runtime_Call --
981 ------------------------
982
983 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
984 begin
985 -- If entity is not available, we can skip making the call (this avoids
986 -- junk duplicated error messages in a number of cases).
987
988 if not RTE_Available (RE) then
989 return Make_Null_Statement (Loc);
990 else
991 return
992 Make_Procedure_Call_Statement (Loc,
993 Name => New_Reference_To (RTE (RE), Loc));
994 end if;
995 end Build_Runtime_Call;
996
997 ----------------------------
998 -- Build_Task_Array_Image --
999 ----------------------------
1000
1001 -- This function generates the body for a function that constructs the
1002 -- image string for a task that is an array component. The function is
1003 -- local to the init proc for the array type, and is called for each one
1004 -- of the components. The constructed image has the form of an indexed
1005 -- component, whose prefix is the outer variable of the array type.
1006 -- The n-dimensional array type has known indexes Index, Index2...
1007
1008 -- Id_Ref is an indexed component form created by the enclosing init proc.
1009 -- Its successive indexes are Val1, Val2, ... which are the loop variables
1010 -- in the loops that call the individual task init proc on each component.
1011
1012 -- The generated function has the following structure:
1013
1014 -- function F return String is
1015 -- Pref : string renames Task_Name;
1016 -- T1 : String := Index1'Image (Val1);
1017 -- ...
1018 -- Tn : String := indexn'image (Valn);
1019 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
1020 -- -- Len includes commas and the end parentheses.
1021 -- Res : String (1..Len);
1022 -- Pos : Integer := Pref'Length;
1023 --
1024 -- begin
1025 -- Res (1 .. Pos) := Pref;
1026 -- Pos := Pos + 1;
1027 -- Res (Pos) := '(';
1028 -- Pos := Pos + 1;
1029 -- Res (Pos .. Pos + T1'Length - 1) := T1;
1030 -- Pos := Pos + T1'Length;
1031 -- Res (Pos) := '.';
1032 -- Pos := Pos + 1;
1033 -- ...
1034 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
1035 -- Res (Len) := ')';
1036 --
1037 -- return Res;
1038 -- end F;
1039 --
1040 -- Needless to say, multidimensional arrays of tasks are rare enough that
1041 -- the bulkiness of this code is not really a concern.
1042
1043 function Build_Task_Array_Image
1044 (Loc : Source_Ptr;
1045 Id_Ref : Node_Id;
1046 A_Type : Entity_Id;
1047 Dyn : Boolean := False) return Node_Id
1048 is
1049 Dims : constant Nat := Number_Dimensions (A_Type);
1050 -- Number of dimensions for array of tasks
1051
1052 Temps : array (1 .. Dims) of Entity_Id;
1053 -- Array of temporaries to hold string for each index
1054
1055 Indx : Node_Id;
1056 -- Index expression
1057
1058 Len : Entity_Id;
1059 -- Total length of generated name
1060
1061 Pos : Entity_Id;
1062 -- Running index for substring assignments
1063
1064 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1065 -- Name of enclosing variable, prefix of resulting name
1066
1067 Res : Entity_Id;
1068 -- String to hold result
1069
1070 Val : Node_Id;
1071 -- Value of successive indexes
1072
1073 Sum : Node_Id;
1074 -- Expression to compute total size of string
1075
1076 T : Entity_Id;
1077 -- Entity for name at one index position
1078
1079 Decls : constant List_Id := New_List;
1080 Stats : constant List_Id := New_List;
1081
1082 begin
1083 -- For a dynamic task, the name comes from the target variable. For a
1084 -- static one it is a formal of the enclosing init proc.
1085
1086 if Dyn then
1087 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1088 Append_To (Decls,
1089 Make_Object_Declaration (Loc,
1090 Defining_Identifier => Pref,
1091 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1092 Expression =>
1093 Make_String_Literal (Loc,
1094 Strval => String_From_Name_Buffer)));
1095
1096 else
1097 Append_To (Decls,
1098 Make_Object_Renaming_Declaration (Loc,
1099 Defining_Identifier => Pref,
1100 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1101 Name => Make_Identifier (Loc, Name_uTask_Name)));
1102 end if;
1103
1104 Indx := First_Index (A_Type);
1105 Val := First (Expressions (Id_Ref));
1106
1107 for J in 1 .. Dims loop
1108 T := Make_Temporary (Loc, 'T');
1109 Temps (J) := T;
1110
1111 Append_To (Decls,
1112 Make_Object_Declaration (Loc,
1113 Defining_Identifier => T,
1114 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1115 Expression =>
1116 Make_Attribute_Reference (Loc,
1117 Attribute_Name => Name_Image,
1118 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
1119 Expressions => New_List (New_Copy_Tree (Val)))));
1120
1121 Next_Index (Indx);
1122 Next (Val);
1123 end loop;
1124
1125 Sum := Make_Integer_Literal (Loc, Dims + 1);
1126
1127 Sum :=
1128 Make_Op_Add (Loc,
1129 Left_Opnd => Sum,
1130 Right_Opnd =>
1131 Make_Attribute_Reference (Loc,
1132 Attribute_Name => Name_Length,
1133 Prefix => New_Occurrence_Of (Pref, Loc),
1134 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1135
1136 for J in 1 .. Dims loop
1137 Sum :=
1138 Make_Op_Add (Loc,
1139 Left_Opnd => Sum,
1140 Right_Opnd =>
1141 Make_Attribute_Reference (Loc,
1142 Attribute_Name => Name_Length,
1143 Prefix =>
1144 New_Occurrence_Of (Temps (J), Loc),
1145 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1146 end loop;
1147
1148 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1149
1150 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
1151
1152 Append_To (Stats,
1153 Make_Assignment_Statement (Loc,
1154 Name =>
1155 Make_Indexed_Component (Loc,
1156 Prefix => New_Occurrence_Of (Res, Loc),
1157 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1158 Expression =>
1159 Make_Character_Literal (Loc,
1160 Chars => Name_Find,
1161 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
1162
1163 Append_To (Stats,
1164 Make_Assignment_Statement (Loc,
1165 Name => New_Occurrence_Of (Pos, Loc),
1166 Expression =>
1167 Make_Op_Add (Loc,
1168 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1169 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1170
1171 for J in 1 .. Dims loop
1172
1173 Append_To (Stats,
1174 Make_Assignment_Statement (Loc,
1175 Name =>
1176 Make_Slice (Loc,
1177 Prefix => New_Occurrence_Of (Res, Loc),
1178 Discrete_Range =>
1179 Make_Range (Loc,
1180 Low_Bound => New_Occurrence_Of (Pos, Loc),
1181 High_Bound =>
1182 Make_Op_Subtract (Loc,
1183 Left_Opnd =>
1184 Make_Op_Add (Loc,
1185 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1186 Right_Opnd =>
1187 Make_Attribute_Reference (Loc,
1188 Attribute_Name => Name_Length,
1189 Prefix =>
1190 New_Occurrence_Of (Temps (J), Loc),
1191 Expressions =>
1192 New_List (Make_Integer_Literal (Loc, 1)))),
1193 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
1194
1195 Expression => New_Occurrence_Of (Temps (J), Loc)));
1196
1197 if J < Dims then
1198 Append_To (Stats,
1199 Make_Assignment_Statement (Loc,
1200 Name => New_Occurrence_Of (Pos, Loc),
1201 Expression =>
1202 Make_Op_Add (Loc,
1203 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1204 Right_Opnd =>
1205 Make_Attribute_Reference (Loc,
1206 Attribute_Name => Name_Length,
1207 Prefix => New_Occurrence_Of (Temps (J), Loc),
1208 Expressions =>
1209 New_List (Make_Integer_Literal (Loc, 1))))));
1210
1211 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
1212
1213 Append_To (Stats,
1214 Make_Assignment_Statement (Loc,
1215 Name => Make_Indexed_Component (Loc,
1216 Prefix => New_Occurrence_Of (Res, Loc),
1217 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1218 Expression =>
1219 Make_Character_Literal (Loc,
1220 Chars => Name_Find,
1221 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
1222
1223 Append_To (Stats,
1224 Make_Assignment_Statement (Loc,
1225 Name => New_Occurrence_Of (Pos, Loc),
1226 Expression =>
1227 Make_Op_Add (Loc,
1228 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1229 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1230 end if;
1231 end loop;
1232
1233 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
1234
1235 Append_To (Stats,
1236 Make_Assignment_Statement (Loc,
1237 Name =>
1238 Make_Indexed_Component (Loc,
1239 Prefix => New_Occurrence_Of (Res, Loc),
1240 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
1241 Expression =>
1242 Make_Character_Literal (Loc,
1243 Chars => Name_Find,
1244 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
1245 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1246 end Build_Task_Array_Image;
1247
1248 ----------------------------
1249 -- Build_Task_Image_Decls --
1250 ----------------------------
1251
1252 function Build_Task_Image_Decls
1253 (Loc : Source_Ptr;
1254 Id_Ref : Node_Id;
1255 A_Type : Entity_Id;
1256 In_Init_Proc : Boolean := False) return List_Id
1257 is
1258 Decls : constant List_Id := New_List;
1259 T_Id : Entity_Id := Empty;
1260 Decl : Node_Id;
1261 Expr : Node_Id := Empty;
1262 Fun : Node_Id := Empty;
1263 Is_Dyn : constant Boolean :=
1264 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
1265 and then
1266 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
1267
1268 begin
1269 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
1270 -- generate a dummy declaration only.
1271
1272 if Restriction_Active (No_Implicit_Heap_Allocations)
1273 or else Global_Discard_Names
1274 then
1275 T_Id := Make_Temporary (Loc, 'J');
1276 Name_Len := 0;
1277
1278 return
1279 New_List (
1280 Make_Object_Declaration (Loc,
1281 Defining_Identifier => T_Id,
1282 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1283 Expression =>
1284 Make_String_Literal (Loc,
1285 Strval => String_From_Name_Buffer)));
1286
1287 else
1288 if Nkind (Id_Ref) = N_Identifier
1289 or else Nkind (Id_Ref) = N_Defining_Identifier
1290 then
1291 -- For a simple variable, the image of the task is built from
1292 -- the name of the variable. To avoid possible conflict with the
1293 -- anonymous type created for a single protected object, add a
1294 -- numeric suffix.
1295
1296 T_Id :=
1297 Make_Defining_Identifier (Loc,
1298 New_External_Name (Chars (Id_Ref), 'T', 1));
1299
1300 Get_Name_String (Chars (Id_Ref));
1301
1302 Expr :=
1303 Make_String_Literal (Loc,
1304 Strval => String_From_Name_Buffer);
1305
1306 elsif Nkind (Id_Ref) = N_Selected_Component then
1307 T_Id :=
1308 Make_Defining_Identifier (Loc,
1309 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
1310 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
1311
1312 elsif Nkind (Id_Ref) = N_Indexed_Component then
1313 T_Id :=
1314 Make_Defining_Identifier (Loc,
1315 New_External_Name (Chars (A_Type), 'N'));
1316
1317 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
1318 end if;
1319 end if;
1320
1321 if Present (Fun) then
1322 Append (Fun, Decls);
1323 Expr := Make_Function_Call (Loc,
1324 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
1325
1326 if not In_Init_Proc and then VM_Target = No_VM then
1327 Set_Uses_Sec_Stack (Defining_Entity (Fun));
1328 end if;
1329 end if;
1330
1331 Decl := Make_Object_Declaration (Loc,
1332 Defining_Identifier => T_Id,
1333 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1334 Constant_Present => True,
1335 Expression => Expr);
1336
1337 Append (Decl, Decls);
1338 return Decls;
1339 end Build_Task_Image_Decls;
1340
1341 -------------------------------
1342 -- Build_Task_Image_Function --
1343 -------------------------------
1344
1345 function Build_Task_Image_Function
1346 (Loc : Source_Ptr;
1347 Decls : List_Id;
1348 Stats : List_Id;
1349 Res : Entity_Id) return Node_Id
1350 is
1351 Spec : Node_Id;
1352
1353 begin
1354 Append_To (Stats,
1355 Make_Simple_Return_Statement (Loc,
1356 Expression => New_Occurrence_Of (Res, Loc)));
1357
1358 Spec := Make_Function_Specification (Loc,
1359 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
1360 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
1361
1362 -- Calls to 'Image use the secondary stack, which must be cleaned up
1363 -- after the task name is built.
1364
1365 return Make_Subprogram_Body (Loc,
1366 Specification => Spec,
1367 Declarations => Decls,
1368 Handled_Statement_Sequence =>
1369 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
1370 end Build_Task_Image_Function;
1371
1372 -----------------------------
1373 -- Build_Task_Image_Prefix --
1374 -----------------------------
1375
1376 procedure Build_Task_Image_Prefix
1377 (Loc : Source_Ptr;
1378 Len : out Entity_Id;
1379 Res : out Entity_Id;
1380 Pos : out Entity_Id;
1381 Prefix : Entity_Id;
1382 Sum : Node_Id;
1383 Decls : List_Id;
1384 Stats : List_Id)
1385 is
1386 begin
1387 Len := Make_Temporary (Loc, 'L', Sum);
1388
1389 Append_To (Decls,
1390 Make_Object_Declaration (Loc,
1391 Defining_Identifier => Len,
1392 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
1393 Expression => Sum));
1394
1395 Res := Make_Temporary (Loc, 'R');
1396
1397 Append_To (Decls,
1398 Make_Object_Declaration (Loc,
1399 Defining_Identifier => Res,
1400 Object_Definition =>
1401 Make_Subtype_Indication (Loc,
1402 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1403 Constraint =>
1404 Make_Index_Or_Discriminant_Constraint (Loc,
1405 Constraints =>
1406 New_List (
1407 Make_Range (Loc,
1408 Low_Bound => Make_Integer_Literal (Loc, 1),
1409 High_Bound => New_Occurrence_Of (Len, Loc)))))));
1410
1411 Pos := Make_Temporary (Loc, 'P');
1412
1413 Append_To (Decls,
1414 Make_Object_Declaration (Loc,
1415 Defining_Identifier => Pos,
1416 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
1417
1418 -- Pos := Prefix'Length;
1419
1420 Append_To (Stats,
1421 Make_Assignment_Statement (Loc,
1422 Name => New_Occurrence_Of (Pos, Loc),
1423 Expression =>
1424 Make_Attribute_Reference (Loc,
1425 Attribute_Name => Name_Length,
1426 Prefix => New_Occurrence_Of (Prefix, Loc),
1427 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
1428
1429 -- Res (1 .. Pos) := Prefix;
1430
1431 Append_To (Stats,
1432 Make_Assignment_Statement (Loc,
1433 Name =>
1434 Make_Slice (Loc,
1435 Prefix => New_Occurrence_Of (Res, Loc),
1436 Discrete_Range =>
1437 Make_Range (Loc,
1438 Low_Bound => Make_Integer_Literal (Loc, 1),
1439 High_Bound => New_Occurrence_Of (Pos, Loc))),
1440
1441 Expression => New_Occurrence_Of (Prefix, Loc)));
1442
1443 Append_To (Stats,
1444 Make_Assignment_Statement (Loc,
1445 Name => New_Occurrence_Of (Pos, Loc),
1446 Expression =>
1447 Make_Op_Add (Loc,
1448 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1449 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1450 end Build_Task_Image_Prefix;
1451
1452 -----------------------------
1453 -- Build_Task_Record_Image --
1454 -----------------------------
1455
1456 function Build_Task_Record_Image
1457 (Loc : Source_Ptr;
1458 Id_Ref : Node_Id;
1459 Dyn : Boolean := False) return Node_Id
1460 is
1461 Len : Entity_Id;
1462 -- Total length of generated name
1463
1464 Pos : Entity_Id;
1465 -- Index into result
1466
1467 Res : Entity_Id;
1468 -- String to hold result
1469
1470 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1471 -- Name of enclosing variable, prefix of resulting name
1472
1473 Sum : Node_Id;
1474 -- Expression to compute total size of string
1475
1476 Sel : Entity_Id;
1477 -- Entity for selector name
1478
1479 Decls : constant List_Id := New_List;
1480 Stats : constant List_Id := New_List;
1481
1482 begin
1483 -- For a dynamic task, the name comes from the target variable. For a
1484 -- static one it is a formal of the enclosing init proc.
1485
1486 if Dyn then
1487 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1488 Append_To (Decls,
1489 Make_Object_Declaration (Loc,
1490 Defining_Identifier => Pref,
1491 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1492 Expression =>
1493 Make_String_Literal (Loc,
1494 Strval => String_From_Name_Buffer)));
1495
1496 else
1497 Append_To (Decls,
1498 Make_Object_Renaming_Declaration (Loc,
1499 Defining_Identifier => Pref,
1500 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1501 Name => Make_Identifier (Loc, Name_uTask_Name)));
1502 end if;
1503
1504 Sel := Make_Temporary (Loc, 'S');
1505
1506 Get_Name_String (Chars (Selector_Name (Id_Ref)));
1507
1508 Append_To (Decls,
1509 Make_Object_Declaration (Loc,
1510 Defining_Identifier => Sel,
1511 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1512 Expression =>
1513 Make_String_Literal (Loc,
1514 Strval => String_From_Name_Buffer)));
1515
1516 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
1517
1518 Sum :=
1519 Make_Op_Add (Loc,
1520 Left_Opnd => Sum,
1521 Right_Opnd =>
1522 Make_Attribute_Reference (Loc,
1523 Attribute_Name => Name_Length,
1524 Prefix =>
1525 New_Occurrence_Of (Pref, Loc),
1526 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1527
1528 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1529
1530 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
1531
1532 -- Res (Pos) := '.';
1533
1534 Append_To (Stats,
1535 Make_Assignment_Statement (Loc,
1536 Name => Make_Indexed_Component (Loc,
1537 Prefix => New_Occurrence_Of (Res, Loc),
1538 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1539 Expression =>
1540 Make_Character_Literal (Loc,
1541 Chars => Name_Find,
1542 Char_Literal_Value =>
1543 UI_From_Int (Character'Pos ('.')))));
1544
1545 Append_To (Stats,
1546 Make_Assignment_Statement (Loc,
1547 Name => New_Occurrence_Of (Pos, Loc),
1548 Expression =>
1549 Make_Op_Add (Loc,
1550 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1551 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1552
1553 -- Res (Pos .. Len) := Selector;
1554
1555 Append_To (Stats,
1556 Make_Assignment_Statement (Loc,
1557 Name => Make_Slice (Loc,
1558 Prefix => New_Occurrence_Of (Res, Loc),
1559 Discrete_Range =>
1560 Make_Range (Loc,
1561 Low_Bound => New_Occurrence_Of (Pos, Loc),
1562 High_Bound => New_Occurrence_Of (Len, Loc))),
1563 Expression => New_Occurrence_Of (Sel, Loc)));
1564
1565 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1566 end Build_Task_Record_Image;
1567
1568 ----------------------------------
1569 -- Component_May_Be_Bit_Aligned --
1570 ----------------------------------
1571
1572 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
1573 UT : Entity_Id;
1574
1575 begin
1576 -- If no component clause, then everything is fine, since the back end
1577 -- never bit-misaligns by default, even if there is a pragma Packed for
1578 -- the record.
1579
1580 if No (Comp) or else No (Component_Clause (Comp)) then
1581 return False;
1582 end if;
1583
1584 UT := Underlying_Type (Etype (Comp));
1585
1586 -- It is only array and record types that cause trouble
1587
1588 if not Is_Record_Type (UT)
1589 and then not Is_Array_Type (UT)
1590 then
1591 return False;
1592
1593 -- If we know that we have a small (64 bits or less) record or small
1594 -- bit-packed array, then everything is fine, since the back end can
1595 -- handle these cases correctly.
1596
1597 elsif Esize (Comp) <= 64
1598 and then (Is_Record_Type (UT)
1599 or else Is_Bit_Packed_Array (UT))
1600 then
1601 return False;
1602
1603 -- Otherwise if the component is not byte aligned, we know we have the
1604 -- nasty unaligned case.
1605
1606 elsif Normalized_First_Bit (Comp) /= Uint_0
1607 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
1608 then
1609 return True;
1610
1611 -- If we are large and byte aligned, then OK at this level
1612
1613 else
1614 return False;
1615 end if;
1616 end Component_May_Be_Bit_Aligned;
1617
1618 -----------------------------------
1619 -- Corresponding_Runtime_Package --
1620 -----------------------------------
1621
1622 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1623 Pkg_Id : RTU_Id := RTU_Null;
1624
1625 begin
1626 pragma Assert (Is_Concurrent_Type (Typ));
1627
1628 if Ekind (Typ) in Protected_Kind then
1629 if Has_Entries (Typ)
1630
1631 -- A protected type without entries that covers an interface and
1632 -- overrides the abstract routines with protected procedures is
1633 -- considered equivalent to a protected type with entries in the
1634 -- context of dispatching select statements. It is sufficient to
1635 -- check for the presence of an interface list in the declaration
1636 -- node to recognize this case.
1637
1638 or else Present (Interface_List (Parent (Typ)))
1639 or else
1640 (((Has_Attach_Handler (Typ) and then not Restricted_Profile)
1641 or else Has_Interrupt_Handler (Typ))
1642 and then not Restriction_Active (No_Dynamic_Attachment))
1643 then
1644 if Abort_Allowed
1645 or else Restriction_Active (No_Entry_Queue) = False
1646 or else Number_Entries (Typ) > 1
1647 or else (Has_Attach_Handler (Typ)
1648 and then not Restricted_Profile)
1649 then
1650 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1651 else
1652 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1653 end if;
1654
1655 else
1656 Pkg_Id := System_Tasking_Protected_Objects;
1657 end if;
1658 end if;
1659
1660 return Pkg_Id;
1661 end Corresponding_Runtime_Package;
1662
1663 -------------------------------
1664 -- Convert_To_Actual_Subtype --
1665 -------------------------------
1666
1667 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1668 Act_ST : Entity_Id;
1669
1670 begin
1671 Act_ST := Get_Actual_Subtype (Exp);
1672
1673 if Act_ST = Etype (Exp) then
1674 return;
1675 else
1676 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
1677 Analyze_And_Resolve (Exp, Act_ST);
1678 end if;
1679 end Convert_To_Actual_Subtype;
1680
1681 -----------------------------------
1682 -- Current_Sem_Unit_Declarations --
1683 -----------------------------------
1684
1685 function Current_Sem_Unit_Declarations return List_Id is
1686 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1687 Decls : List_Id;
1688
1689 begin
1690 -- If the current unit is a package body, locate the visible
1691 -- declarations of the package spec.
1692
1693 if Nkind (U) = N_Package_Body then
1694 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1695 end if;
1696
1697 if Nkind (U) = N_Package_Declaration then
1698 U := Specification (U);
1699 Decls := Visible_Declarations (U);
1700
1701 if No (Decls) then
1702 Decls := New_List;
1703 Set_Visible_Declarations (U, Decls);
1704 end if;
1705
1706 else
1707 Decls := Declarations (U);
1708
1709 if No (Decls) then
1710 Decls := New_List;
1711 Set_Declarations (U, Decls);
1712 end if;
1713 end if;
1714
1715 return Decls;
1716 end Current_Sem_Unit_Declarations;
1717
1718 -----------------------
1719 -- Duplicate_Subexpr --
1720 -----------------------
1721
1722 function Duplicate_Subexpr
1723 (Exp : Node_Id;
1724 Name_Req : Boolean := False) return Node_Id
1725 is
1726 begin
1727 Remove_Side_Effects (Exp, Name_Req);
1728 return New_Copy_Tree (Exp);
1729 end Duplicate_Subexpr;
1730
1731 ---------------------------------
1732 -- Duplicate_Subexpr_No_Checks --
1733 ---------------------------------
1734
1735 function Duplicate_Subexpr_No_Checks
1736 (Exp : Node_Id;
1737 Name_Req : Boolean := False) return Node_Id
1738 is
1739 New_Exp : Node_Id;
1740
1741 begin
1742 Remove_Side_Effects (Exp, Name_Req);
1743 New_Exp := New_Copy_Tree (Exp);
1744 Remove_Checks (New_Exp);
1745 return New_Exp;
1746 end Duplicate_Subexpr_No_Checks;
1747
1748 -----------------------------------
1749 -- Duplicate_Subexpr_Move_Checks --
1750 -----------------------------------
1751
1752 function Duplicate_Subexpr_Move_Checks
1753 (Exp : Node_Id;
1754 Name_Req : Boolean := False) return Node_Id
1755 is
1756 New_Exp : Node_Id;
1757 begin
1758 Remove_Side_Effects (Exp, Name_Req);
1759 New_Exp := New_Copy_Tree (Exp);
1760 Remove_Checks (Exp);
1761 return New_Exp;
1762 end Duplicate_Subexpr_Move_Checks;
1763
1764 --------------------
1765 -- Ensure_Defined --
1766 --------------------
1767
1768 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1769 IR : Node_Id;
1770
1771 begin
1772 -- An itype reference must only be created if this is a local itype, so
1773 -- that gigi can elaborate it on the proper objstack.
1774
1775 if Is_Itype (Typ)
1776 and then Scope (Typ) = Current_Scope
1777 then
1778 IR := Make_Itype_Reference (Sloc (N));
1779 Set_Itype (IR, Typ);
1780 Insert_Action (N, IR);
1781 end if;
1782 end Ensure_Defined;
1783
1784 --------------------
1785 -- Entry_Names_OK --
1786 --------------------
1787
1788 function Entry_Names_OK return Boolean is
1789 begin
1790 return
1791 not Restricted_Profile
1792 and then not Global_Discard_Names
1793 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1794 and then not Restriction_Active (No_Local_Allocators);
1795 end Entry_Names_OK;
1796
1797 -------------------
1798 -- Evaluate_Name --
1799 -------------------
1800
1801 procedure Evaluate_Name (Nam : Node_Id) is
1802 K : constant Node_Kind := Nkind (Nam);
1803
1804 begin
1805 -- For an explicit dereference, we simply force the evaluation of the
1806 -- name expression. The dereference provides a value that is the address
1807 -- for the renamed object, and it is precisely this value that we want
1808 -- to preserve.
1809
1810 if K = N_Explicit_Dereference then
1811 Force_Evaluation (Prefix (Nam));
1812
1813 -- For a selected component, we simply evaluate the prefix
1814
1815 elsif K = N_Selected_Component then
1816 Evaluate_Name (Prefix (Nam));
1817
1818 -- For an indexed component, or an attribute reference, we evaluate the
1819 -- prefix, which is itself a name, recursively, and then force the
1820 -- evaluation of all the subscripts (or attribute expressions).
1821
1822 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
1823 Evaluate_Name (Prefix (Nam));
1824
1825 declare
1826 E : Node_Id;
1827
1828 begin
1829 E := First (Expressions (Nam));
1830 while Present (E) loop
1831 Force_Evaluation (E);
1832
1833 if Original_Node (E) /= E then
1834 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
1835 end if;
1836
1837 Next (E);
1838 end loop;
1839 end;
1840
1841 -- For a slice, we evaluate the prefix, as for the indexed component
1842 -- case and then, if there is a range present, either directly or as the
1843 -- constraint of a discrete subtype indication, we evaluate the two
1844 -- bounds of this range.
1845
1846 elsif K = N_Slice then
1847 Evaluate_Name (Prefix (Nam));
1848
1849 declare
1850 DR : constant Node_Id := Discrete_Range (Nam);
1851 Constr : Node_Id;
1852 Rexpr : Node_Id;
1853
1854 begin
1855 if Nkind (DR) = N_Range then
1856 Force_Evaluation (Low_Bound (DR));
1857 Force_Evaluation (High_Bound (DR));
1858
1859 elsif Nkind (DR) = N_Subtype_Indication then
1860 Constr := Constraint (DR);
1861
1862 if Nkind (Constr) = N_Range_Constraint then
1863 Rexpr := Range_Expression (Constr);
1864
1865 Force_Evaluation (Low_Bound (Rexpr));
1866 Force_Evaluation (High_Bound (Rexpr));
1867 end if;
1868 end if;
1869 end;
1870
1871 -- For a type conversion, the expression of the conversion must be the
1872 -- name of an object, and we simply need to evaluate this name.
1873
1874 elsif K = N_Type_Conversion then
1875 Evaluate_Name (Expression (Nam));
1876
1877 -- For a function call, we evaluate the call
1878
1879 elsif K = N_Function_Call then
1880 Force_Evaluation (Nam);
1881
1882 -- The remaining cases are direct name, operator symbol and character
1883 -- literal. In all these cases, we do nothing, since we want to
1884 -- reevaluate each time the renamed object is used.
1885
1886 else
1887 return;
1888 end if;
1889 end Evaluate_Name;
1890
1891 ---------------------
1892 -- Evolve_And_Then --
1893 ---------------------
1894
1895 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1896 begin
1897 if No (Cond) then
1898 Cond := Cond1;
1899 else
1900 Cond :=
1901 Make_And_Then (Sloc (Cond1),
1902 Left_Opnd => Cond,
1903 Right_Opnd => Cond1);
1904 end if;
1905 end Evolve_And_Then;
1906
1907 --------------------
1908 -- Evolve_Or_Else --
1909 --------------------
1910
1911 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1912 begin
1913 if No (Cond) then
1914 Cond := Cond1;
1915 else
1916 Cond :=
1917 Make_Or_Else (Sloc (Cond1),
1918 Left_Opnd => Cond,
1919 Right_Opnd => Cond1);
1920 end if;
1921 end Evolve_Or_Else;
1922
1923 ------------------------------
1924 -- Expand_Subtype_From_Expr --
1925 ------------------------------
1926
1927 -- This function is applicable for both static and dynamic allocation of
1928 -- objects which are constrained by an initial expression. Basically it
1929 -- transforms an unconstrained subtype indication into a constrained one.
1930
1931 -- The expression may also be transformed in certain cases in order to
1932 -- avoid multiple evaluation. In the static allocation case, the general
1933 -- scheme is:
1934
1935 -- Val : T := Expr;
1936
1937 -- is transformed into
1938
1939 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1940 --
1941 -- Here are the main cases :
1942 --
1943 -- <if Expr is a Slice>
1944 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1945 --
1946 -- <elsif Expr is a String Literal>
1947 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1948 --
1949 -- <elsif Expr is Constrained>
1950 -- subtype T is Type_Of_Expr
1951 -- Val : T := Expr;
1952 --
1953 -- <elsif Expr is an entity_name>
1954 -- Val : T (constraints taken from Expr) := Expr;
1955 --
1956 -- <else>
1957 -- type Axxx is access all T;
1958 -- Rval : Axxx := Expr'ref;
1959 -- Val : T (constraints taken from Rval) := Rval.all;
1960
1961 -- ??? note: when the Expression is allocated in the secondary stack
1962 -- we could use it directly instead of copying it by declaring
1963 -- Val : T (...) renames Rval.all
1964
1965 procedure Expand_Subtype_From_Expr
1966 (N : Node_Id;
1967 Unc_Type : Entity_Id;
1968 Subtype_Indic : Node_Id;
1969 Exp : Node_Id)
1970 is
1971 Loc : constant Source_Ptr := Sloc (N);
1972 Exp_Typ : constant Entity_Id := Etype (Exp);
1973 T : Entity_Id;
1974
1975 begin
1976 -- In general we cannot build the subtype if expansion is disabled,
1977 -- because internal entities may not have been defined. However, to
1978 -- avoid some cascaded errors, we try to continue when the expression is
1979 -- an array (or string), because it is safe to compute the bounds. It is
1980 -- in fact required to do so even in a generic context, because there
1981 -- may be constants that depend on the bounds of a string literal, both
1982 -- standard string types and more generally arrays of characters.
1983
1984 if not Expander_Active
1985 and then (No (Etype (Exp))
1986 or else not Is_String_Type (Etype (Exp)))
1987 then
1988 return;
1989 end if;
1990
1991 if Nkind (Exp) = N_Slice then
1992 declare
1993 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
1994
1995 begin
1996 Rewrite (Subtype_Indic,
1997 Make_Subtype_Indication (Loc,
1998 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
1999 Constraint =>
2000 Make_Index_Or_Discriminant_Constraint (Loc,
2001 Constraints => New_List
2002 (New_Reference_To (Slice_Type, Loc)))));
2003
2004 -- This subtype indication may be used later for constraint checks
2005 -- we better make sure that if a variable was used as a bound of
2006 -- of the original slice, its value is frozen.
2007
2008 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
2009 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
2010 end;
2011
2012 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2013 Rewrite (Subtype_Indic,
2014 Make_Subtype_Indication (Loc,
2015 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
2016 Constraint =>
2017 Make_Index_Or_Discriminant_Constraint (Loc,
2018 Constraints => New_List (
2019 Make_Literal_Range (Loc,
2020 Literal_Typ => Exp_Typ)))));
2021
2022 elsif Is_Constrained (Exp_Typ)
2023 and then not Is_Class_Wide_Type (Unc_Type)
2024 then
2025 if Is_Itype (Exp_Typ) then
2026
2027 -- Within an initialization procedure, a selected component
2028 -- denotes a component of the enclosing record, and it appears as
2029 -- an actual in a call to its own initialization procedure. If
2030 -- this component depends on the outer discriminant, we must
2031 -- generate the proper actual subtype for it.
2032
2033 if Nkind (Exp) = N_Selected_Component
2034 and then Within_Init_Proc
2035 then
2036 declare
2037 Decl : constant Node_Id :=
2038 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2039 begin
2040 if Present (Decl) then
2041 Insert_Action (N, Decl);
2042 T := Defining_Identifier (Decl);
2043 else
2044 T := Exp_Typ;
2045 end if;
2046 end;
2047
2048 -- No need to generate a new one (new what???)
2049
2050 else
2051 T := Exp_Typ;
2052 end if;
2053
2054 else
2055 T := Make_Temporary (Loc, 'T');
2056
2057 Insert_Action (N,
2058 Make_Subtype_Declaration (Loc,
2059 Defining_Identifier => T,
2060 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
2061
2062 -- This type is marked as an itype even though it has an explicit
2063 -- declaration since otherwise Is_Generic_Actual_Type can get
2064 -- set, resulting in the generation of spurious errors. (See
2065 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
2066
2067 Set_Is_Itype (T);
2068 Set_Associated_Node_For_Itype (T, Exp);
2069 end if;
2070
2071 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
2072
2073 -- Nothing needs to be done for private types with unknown discriminants
2074 -- if the underlying type is not an unconstrained composite type or it
2075 -- is an unchecked union.
2076
2077 elsif Is_Private_Type (Unc_Type)
2078 and then Has_Unknown_Discriminants (Unc_Type)
2079 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
2080 or else Is_Constrained (Underlying_Type (Unc_Type))
2081 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
2082 then
2083 null;
2084
2085 -- Case of derived type with unknown discriminants where the parent type
2086 -- also has unknown discriminants.
2087
2088 elsif Is_Record_Type (Unc_Type)
2089 and then not Is_Class_Wide_Type (Unc_Type)
2090 and then Has_Unknown_Discriminants (Unc_Type)
2091 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2092 then
2093 -- Nothing to be done if no underlying record view available
2094
2095 if No (Underlying_Record_View (Unc_Type)) then
2096 null;
2097
2098 -- Otherwise use the Underlying_Record_View to create the proper
2099 -- constrained subtype for an object of a derived type with unknown
2100 -- discriminants.
2101
2102 else
2103 Remove_Side_Effects (Exp);
2104 Rewrite (Subtype_Indic,
2105 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2106 end if;
2107
2108 -- Renamings of class-wide interface types require no equivalent
2109 -- constrained type declarations because we only need to reference
2110 -- the tag component associated with the interface. The same is
2111 -- presumably true for class-wide types in general, so this test
2112 -- is broadened to include all class-wide renamings, which also
2113 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2114 -- (Is this really correct, or are there some cases of class-wide
2115 -- renamings that require action in this procedure???)
2116
2117 elsif Present (N)
2118 and then Nkind (N) = N_Object_Renaming_Declaration
2119 and then Is_Class_Wide_Type (Unc_Type)
2120 then
2121 null;
2122
2123 -- In Ada 95 nothing to be done if the type of the expression is limited
2124 -- because in this case the expression cannot be copied, and its use can
2125 -- only be by reference.
2126
2127 -- In Ada 2005 the context can be an object declaration whose expression
2128 -- is a function that returns in place. If the nominal subtype has
2129 -- unknown discriminants, the call still provides constraints on the
2130 -- object, and we have to create an actual subtype from it.
2131
2132 -- If the type is class-wide, the expression is dynamically tagged and
2133 -- we do not create an actual subtype either. Ditto for an interface.
2134 -- For now this applies only if the type is immutably limited, and the
2135 -- function being called is build-in-place. This will have to be revised
2136 -- when build-in-place functions are generalized to other types.
2137
2138 elsif Is_Immutably_Limited_Type (Exp_Typ)
2139 and then
2140 (Is_Class_Wide_Type (Exp_Typ)
2141 or else Is_Interface (Exp_Typ)
2142 or else not Has_Unknown_Discriminants (Exp_Typ)
2143 or else not Is_Composite_Type (Unc_Type))
2144 then
2145 null;
2146
2147 -- For limited objects initialized with build in place function calls,
2148 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2149 -- node in the expression initializing the object, which breaks the
2150 -- circuitry that detects and adds the additional arguments to the
2151 -- called function.
2152
2153 elsif Is_Build_In_Place_Function_Call (Exp) then
2154 null;
2155
2156 else
2157 Remove_Side_Effects (Exp);
2158 Rewrite (Subtype_Indic,
2159 Make_Subtype_From_Expr (Exp, Unc_Type));
2160 end if;
2161 end Expand_Subtype_From_Expr;
2162
2163 --------------------
2164 -- Find_Init_Call --
2165 --------------------
2166
2167 function Find_Init_Call
2168 (Var : Entity_Id;
2169 Rep_Clause : Node_Id) return Node_Id
2170 is
2171 Par : constant Node_Id := Parent (Var);
2172 Typ : constant Entity_Id := Etype (Var);
2173
2174 Init_Proc : Entity_Id;
2175 -- Initialization procedure for Typ
2176
2177 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
2178 -- Look for init call for Var starting at From and scanning the
2179 -- enclosing list until Rep_Clause or the end of the list is reached.
2180
2181 ----------------------------
2182 -- Find_Init_Call_In_List --
2183 ----------------------------
2184
2185 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
2186 Init_Call : Node_Id;
2187 begin
2188 Init_Call := From;
2189
2190 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
2191 if Nkind (Init_Call) = N_Procedure_Call_Statement
2192 and then Is_Entity_Name (Name (Init_Call))
2193 and then Entity (Name (Init_Call)) = Init_Proc
2194 then
2195 return Init_Call;
2196 end if;
2197
2198 Next (Init_Call);
2199 end loop;
2200
2201 return Empty;
2202 end Find_Init_Call_In_List;
2203
2204 Init_Call : Node_Id;
2205
2206 -- Start of processing for Find_Init_Call
2207
2208 begin
2209 if not Has_Non_Null_Base_Init_Proc (Typ) then
2210
2211 -- No init proc for the type, so obviously no call to be found
2212
2213 return Empty;
2214 end if;
2215
2216 Init_Proc := Base_Init_Proc (Typ);
2217
2218 -- First scan the list containing the declaration of Var
2219
2220 Init_Call := Find_Init_Call_In_List (From => Next (Par));
2221
2222 -- If not found, also look on Var's freeze actions list, if any, since
2223 -- the init call may have been moved there (case of an address clause
2224 -- applying to Var).
2225
2226 if No (Init_Call) and then Present (Freeze_Node (Var)) then
2227 Init_Call :=
2228 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
2229 end if;
2230
2231 -- If the initialization call has actuals that use the secondary stack,
2232 -- the call may have been wrapped into a temporary block, in which case
2233 -- the block itself has to be removed.
2234
2235 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
2236 declare
2237 Blk : constant Node_Id := Next (Par);
2238 begin
2239 if Present
2240 (Find_Init_Call_In_List
2241 (First (Statements (Handled_Statement_Sequence (Blk)))))
2242 then
2243 Init_Call := Blk;
2244 end if;
2245 end;
2246 end if;
2247
2248 return Init_Call;
2249 end Find_Init_Call;
2250
2251 ------------------------
2252 -- Find_Interface_ADT --
2253 ------------------------
2254
2255 function Find_Interface_ADT
2256 (T : Entity_Id;
2257 Iface : Entity_Id) return Elmt_Id
2258 is
2259 ADT : Elmt_Id;
2260 Typ : Entity_Id := T;
2261
2262 begin
2263 pragma Assert (Is_Interface (Iface));
2264
2265 -- Handle private types
2266
2267 if Has_Private_Declaration (Typ)
2268 and then Present (Full_View (Typ))
2269 then
2270 Typ := Full_View (Typ);
2271 end if;
2272
2273 -- Handle access types
2274
2275 if Is_Access_Type (Typ) then
2276 Typ := Designated_Type (Typ);
2277 end if;
2278
2279 -- Handle task and protected types implementing interfaces
2280
2281 if Is_Concurrent_Type (Typ) then
2282 Typ := Corresponding_Record_Type (Typ);
2283 end if;
2284
2285 pragma Assert
2286 (not Is_Class_Wide_Type (Typ)
2287 and then Ekind (Typ) /= E_Incomplete_Type);
2288
2289 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2290 return First_Elmt (Access_Disp_Table (Typ));
2291
2292 else
2293 ADT :=
2294 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
2295 while Present (ADT)
2296 and then Present (Related_Type (Node (ADT)))
2297 and then Related_Type (Node (ADT)) /= Iface
2298 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2299 Use_Full_View => True)
2300 loop
2301 Next_Elmt (ADT);
2302 end loop;
2303
2304 pragma Assert (Present (Related_Type (Node (ADT))));
2305 return ADT;
2306 end if;
2307 end Find_Interface_ADT;
2308
2309 ------------------------
2310 -- Find_Interface_Tag --
2311 ------------------------
2312
2313 function Find_Interface_Tag
2314 (T : Entity_Id;
2315 Iface : Entity_Id) return Entity_Id
2316 is
2317 AI_Tag : Entity_Id;
2318 Found : Boolean := False;
2319 Typ : Entity_Id := T;
2320
2321 procedure Find_Tag (Typ : Entity_Id);
2322 -- Internal subprogram used to recursively climb to the ancestors
2323
2324 --------------
2325 -- Find_Tag --
2326 --------------
2327
2328 procedure Find_Tag (Typ : Entity_Id) is
2329 AI_Elmt : Elmt_Id;
2330 AI : Node_Id;
2331
2332 begin
2333 -- This routine does not handle the case in which the interface is an
2334 -- ancestor of Typ. That case is handled by the enclosing subprogram.
2335
2336 pragma Assert (Typ /= Iface);
2337
2338 -- Climb to the root type handling private types
2339
2340 if Present (Full_View (Etype (Typ))) then
2341 if Full_View (Etype (Typ)) /= Typ then
2342 Find_Tag (Full_View (Etype (Typ)));
2343 end if;
2344
2345 elsif Etype (Typ) /= Typ then
2346 Find_Tag (Etype (Typ));
2347 end if;
2348
2349 -- Traverse the list of interfaces implemented by the type
2350
2351 if not Found
2352 and then Present (Interfaces (Typ))
2353 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
2354 then
2355 -- Skip the tag associated with the primary table
2356
2357 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2358 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2359 pragma Assert (Present (AI_Tag));
2360
2361 AI_Elmt := First_Elmt (Interfaces (Typ));
2362 while Present (AI_Elmt) loop
2363 AI := Node (AI_Elmt);
2364
2365 if AI = Iface
2366 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2367 then
2368 Found := True;
2369 return;
2370 end if;
2371
2372 AI_Tag := Next_Tag_Component (AI_Tag);
2373 Next_Elmt (AI_Elmt);
2374 end loop;
2375 end if;
2376 end Find_Tag;
2377
2378 -- Start of processing for Find_Interface_Tag
2379
2380 begin
2381 pragma Assert (Is_Interface (Iface));
2382
2383 -- Handle access types
2384
2385 if Is_Access_Type (Typ) then
2386 Typ := Designated_Type (Typ);
2387 end if;
2388
2389 -- Handle class-wide types
2390
2391 if Is_Class_Wide_Type (Typ) then
2392 Typ := Root_Type (Typ);
2393 end if;
2394
2395 -- Handle private types
2396
2397 if Has_Private_Declaration (Typ)
2398 and then Present (Full_View (Typ))
2399 then
2400 Typ := Full_View (Typ);
2401 end if;
2402
2403 -- Handle entities from the limited view
2404
2405 if Ekind (Typ) = E_Incomplete_Type then
2406 pragma Assert (Present (Non_Limited_View (Typ)));
2407 Typ := Non_Limited_View (Typ);
2408 end if;
2409
2410 -- Handle task and protected types implementing interfaces
2411
2412 if Is_Concurrent_Type (Typ) then
2413 Typ := Corresponding_Record_Type (Typ);
2414 end if;
2415
2416 -- If the interface is an ancestor of the type, then it shared the
2417 -- primary dispatch table.
2418
2419 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2420 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2421 return First_Tag_Component (Typ);
2422
2423 -- Otherwise we need to search for its associated tag component
2424
2425 else
2426 Find_Tag (Typ);
2427 pragma Assert (Found);
2428 return AI_Tag;
2429 end if;
2430 end Find_Interface_Tag;
2431
2432 ------------------
2433 -- Find_Prim_Op --
2434 ------------------
2435
2436 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
2437 Prim : Elmt_Id;
2438 Typ : Entity_Id := T;
2439 Op : Entity_Id;
2440
2441 begin
2442 if Is_Class_Wide_Type (Typ) then
2443 Typ := Root_Type (Typ);
2444 end if;
2445
2446 Typ := Underlying_Type (Typ);
2447
2448 -- Loop through primitive operations
2449
2450 Prim := First_Elmt (Primitive_Operations (Typ));
2451 while Present (Prim) loop
2452 Op := Node (Prim);
2453
2454 -- We can retrieve primitive operations by name if it is an internal
2455 -- name. For equality we must check that both of its operands have
2456 -- the same type, to avoid confusion with user-defined equalities
2457 -- than may have a non-symmetric signature.
2458
2459 exit when Chars (Op) = Name
2460 and then
2461 (Name /= Name_Op_Eq
2462 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
2463
2464 Next_Elmt (Prim);
2465
2466 -- Raise Program_Error if no primitive found
2467
2468 if No (Prim) then
2469 raise Program_Error;
2470 end if;
2471 end loop;
2472
2473 return Node (Prim);
2474 end Find_Prim_Op;
2475
2476 ------------------
2477 -- Find_Prim_Op --
2478 ------------------
2479
2480 function Find_Prim_Op
2481 (T : Entity_Id;
2482 Name : TSS_Name_Type) return Entity_Id
2483 is
2484 Inher_Op : Entity_Id := Empty;
2485 Own_Op : Entity_Id := Empty;
2486 Prim_Elmt : Elmt_Id;
2487 Prim_Id : Entity_Id;
2488 Typ : Entity_Id := T;
2489
2490 begin
2491 if Is_Class_Wide_Type (Typ) then
2492 Typ := Root_Type (Typ);
2493 end if;
2494
2495 Typ := Underlying_Type (Typ);
2496
2497 -- This search is based on the assertion that the dispatching version
2498 -- of the TSS routine always precedes the real primitive.
2499
2500 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2501 while Present (Prim_Elmt) loop
2502 Prim_Id := Node (Prim_Elmt);
2503
2504 if Is_TSS (Prim_Id, Name) then
2505 if Present (Alias (Prim_Id)) then
2506 Inher_Op := Prim_Id;
2507 else
2508 Own_Op := Prim_Id;
2509 end if;
2510 end if;
2511
2512 Next_Elmt (Prim_Elmt);
2513 end loop;
2514
2515 if Present (Own_Op) then
2516 return Own_Op;
2517 elsif Present (Inher_Op) then
2518 return Inher_Op;
2519 else
2520 raise Program_Error;
2521 end if;
2522 end Find_Prim_Op;
2523
2524 ----------------------------
2525 -- Find_Protection_Object --
2526 ----------------------------
2527
2528 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2529 S : Entity_Id;
2530
2531 begin
2532 S := Scop;
2533 while Present (S) loop
2534 if (Ekind (S) = E_Entry
2535 or else Ekind (S) = E_Entry_Family
2536 or else Ekind (S) = E_Function
2537 or else Ekind (S) = E_Procedure)
2538 and then Present (Protection_Object (S))
2539 then
2540 return Protection_Object (S);
2541 end if;
2542
2543 S := Scope (S);
2544 end loop;
2545
2546 -- If we do not find a Protection object in the scope chain, then
2547 -- something has gone wrong, most likely the object was never created.
2548
2549 raise Program_Error;
2550 end Find_Protection_Object;
2551
2552 --------------------------
2553 -- Find_Protection_Type --
2554 --------------------------
2555
2556 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2557 Comp : Entity_Id;
2558 Typ : Entity_Id := Conc_Typ;
2559
2560 begin
2561 if Is_Concurrent_Type (Typ) then
2562 Typ := Corresponding_Record_Type (Typ);
2563 end if;
2564
2565 -- Since restriction violations are not considered serious errors, the
2566 -- expander remains active, but may leave the corresponding record type
2567 -- malformed. In such cases, component _object is not available so do
2568 -- not look for it.
2569
2570 if not Analyzed (Typ) then
2571 return Empty;
2572 end if;
2573
2574 Comp := First_Component (Typ);
2575 while Present (Comp) loop
2576 if Chars (Comp) = Name_uObject then
2577 return Base_Type (Etype (Comp));
2578 end if;
2579
2580 Next_Component (Comp);
2581 end loop;
2582
2583 -- The corresponding record of a protected type should always have an
2584 -- _object field.
2585
2586 raise Program_Error;
2587 end Find_Protection_Type;
2588
2589 ----------------------
2590 -- Force_Evaluation --
2591 ----------------------
2592
2593 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
2594 begin
2595 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
2596 end Force_Evaluation;
2597
2598 ---------------------------------
2599 -- Fully_Qualified_Name_String --
2600 ---------------------------------
2601
2602 function Fully_Qualified_Name_String (E : Entity_Id) return String_Id is
2603 procedure Internal_Full_Qualified_Name (E : Entity_Id);
2604 -- Compute recursively the qualified name without NUL at the end, adding
2605 -- it to the currently started string being generated
2606
2607 ----------------------------------
2608 -- Internal_Full_Qualified_Name --
2609 ----------------------------------
2610
2611 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
2612 Ent : Entity_Id;
2613
2614 begin
2615 -- Deal properly with child units
2616
2617 if Nkind (E) = N_Defining_Program_Unit_Name then
2618 Ent := Defining_Identifier (E);
2619 else
2620 Ent := E;
2621 end if;
2622
2623 -- Compute qualification recursively (only "Standard" has no scope)
2624
2625 if Present (Scope (Scope (Ent))) then
2626 Internal_Full_Qualified_Name (Scope (Ent));
2627 Store_String_Char (Get_Char_Code ('.'));
2628 end if;
2629
2630 -- Every entity should have a name except some expanded blocks
2631 -- don't bother about those.
2632
2633 if Chars (Ent) = No_Name then
2634 return;
2635 end if;
2636
2637 -- Generates the entity name in upper case
2638
2639 Get_Decoded_Name_String (Chars (Ent));
2640 Set_All_Upper_Case;
2641 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2642 return;
2643 end Internal_Full_Qualified_Name;
2644
2645 -- Start of processing for Full_Qualified_Name
2646
2647 begin
2648 Start_String;
2649 Internal_Full_Qualified_Name (E);
2650 Store_String_Char (Get_Char_Code (ASCII.NUL));
2651 return End_String;
2652 end Fully_Qualified_Name_String;
2653
2654 ------------------------
2655 -- Generate_Poll_Call --
2656 ------------------------
2657
2658 procedure Generate_Poll_Call (N : Node_Id) is
2659 begin
2660 -- No poll call if polling not active
2661
2662 if not Polling_Required then
2663 return;
2664
2665 -- Otherwise generate require poll call
2666
2667 else
2668 Insert_Before_And_Analyze (N,
2669 Make_Procedure_Call_Statement (Sloc (N),
2670 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
2671 end if;
2672 end Generate_Poll_Call;
2673
2674 ---------------------------------
2675 -- Get_Current_Value_Condition --
2676 ---------------------------------
2677
2678 -- Note: the implementation of this procedure is very closely tied to the
2679 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
2680 -- interpret Current_Value fields set by the Set procedure, so the two
2681 -- procedures need to be closely coordinated.
2682
2683 procedure Get_Current_Value_Condition
2684 (Var : Node_Id;
2685 Op : out Node_Kind;
2686 Val : out Node_Id)
2687 is
2688 Loc : constant Source_Ptr := Sloc (Var);
2689 Ent : constant Entity_Id := Entity (Var);
2690
2691 procedure Process_Current_Value_Condition
2692 (N : Node_Id;
2693 S : Boolean);
2694 -- N is an expression which holds either True (S = True) or False (S =
2695 -- False) in the condition. This procedure digs out the expression and
2696 -- if it refers to Ent, sets Op and Val appropriately.
2697
2698 -------------------------------------
2699 -- Process_Current_Value_Condition --
2700 -------------------------------------
2701
2702 procedure Process_Current_Value_Condition
2703 (N : Node_Id;
2704 S : Boolean)
2705 is
2706 Cond : Node_Id;
2707 Sens : Boolean;
2708
2709 begin
2710 Cond := N;
2711 Sens := S;
2712
2713 -- Deal with NOT operators, inverting sense
2714
2715 while Nkind (Cond) = N_Op_Not loop
2716 Cond := Right_Opnd (Cond);
2717 Sens := not Sens;
2718 end loop;
2719
2720 -- Deal with AND THEN and AND cases
2721
2722 if Nkind (Cond) = N_And_Then
2723 or else Nkind (Cond) = N_Op_And
2724 then
2725 -- Don't ever try to invert a condition that is of the form of an
2726 -- AND or AND THEN (since we are not doing sufficiently general
2727 -- processing to allow this).
2728
2729 if Sens = False then
2730 Op := N_Empty;
2731 Val := Empty;
2732 return;
2733 end if;
2734
2735 -- Recursively process AND and AND THEN branches
2736
2737 Process_Current_Value_Condition (Left_Opnd (Cond), True);
2738
2739 if Op /= N_Empty then
2740 return;
2741 end if;
2742
2743 Process_Current_Value_Condition (Right_Opnd (Cond), True);
2744 return;
2745
2746 -- Case of relational operator
2747
2748 elsif Nkind (Cond) in N_Op_Compare then
2749 Op := Nkind (Cond);
2750
2751 -- Invert sense of test if inverted test
2752
2753 if Sens = False then
2754 case Op is
2755 when N_Op_Eq => Op := N_Op_Ne;
2756 when N_Op_Ne => Op := N_Op_Eq;
2757 when N_Op_Lt => Op := N_Op_Ge;
2758 when N_Op_Gt => Op := N_Op_Le;
2759 when N_Op_Le => Op := N_Op_Gt;
2760 when N_Op_Ge => Op := N_Op_Lt;
2761 when others => raise Program_Error;
2762 end case;
2763 end if;
2764
2765 -- Case of entity op value
2766
2767 if Is_Entity_Name (Left_Opnd (Cond))
2768 and then Ent = Entity (Left_Opnd (Cond))
2769 and then Compile_Time_Known_Value (Right_Opnd (Cond))
2770 then
2771 Val := Right_Opnd (Cond);
2772
2773 -- Case of value op entity
2774
2775 elsif Is_Entity_Name (Right_Opnd (Cond))
2776 and then Ent = Entity (Right_Opnd (Cond))
2777 and then Compile_Time_Known_Value (Left_Opnd (Cond))
2778 then
2779 Val := Left_Opnd (Cond);
2780
2781 -- We are effectively swapping operands
2782
2783 case Op is
2784 when N_Op_Eq => null;
2785 when N_Op_Ne => null;
2786 when N_Op_Lt => Op := N_Op_Gt;
2787 when N_Op_Gt => Op := N_Op_Lt;
2788 when N_Op_Le => Op := N_Op_Ge;
2789 when N_Op_Ge => Op := N_Op_Le;
2790 when others => raise Program_Error;
2791 end case;
2792
2793 else
2794 Op := N_Empty;
2795 end if;
2796
2797 return;
2798
2799 -- Case of Boolean variable reference, return as though the
2800 -- reference had said var = True.
2801
2802 else
2803 if Is_Entity_Name (Cond)
2804 and then Ent = Entity (Cond)
2805 then
2806 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
2807
2808 if Sens = False then
2809 Op := N_Op_Ne;
2810 else
2811 Op := N_Op_Eq;
2812 end if;
2813 end if;
2814 end if;
2815 end Process_Current_Value_Condition;
2816
2817 -- Start of processing for Get_Current_Value_Condition
2818
2819 begin
2820 Op := N_Empty;
2821 Val := Empty;
2822
2823 -- Immediate return, nothing doing, if this is not an object
2824
2825 if Ekind (Ent) not in Object_Kind then
2826 return;
2827 end if;
2828
2829 -- Otherwise examine current value
2830
2831 declare
2832 CV : constant Node_Id := Current_Value (Ent);
2833 Sens : Boolean;
2834 Stm : Node_Id;
2835
2836 begin
2837 -- If statement. Condition is known true in THEN section, known False
2838 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
2839
2840 if Nkind (CV) = N_If_Statement then
2841
2842 -- Before start of IF statement
2843
2844 if Loc < Sloc (CV) then
2845 return;
2846
2847 -- After end of IF statement
2848
2849 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
2850 return;
2851 end if;
2852
2853 -- At this stage we know that we are within the IF statement, but
2854 -- unfortunately, the tree does not record the SLOC of the ELSE so
2855 -- we cannot use a simple SLOC comparison to distinguish between
2856 -- the then/else statements, so we have to climb the tree.
2857
2858 declare
2859 N : Node_Id;
2860
2861 begin
2862 N := Parent (Var);
2863 while Parent (N) /= CV loop
2864 N := Parent (N);
2865
2866 -- If we fall off the top of the tree, then that's odd, but
2867 -- perhaps it could occur in some error situation, and the
2868 -- safest response is simply to assume that the outcome of
2869 -- the condition is unknown. No point in bombing during an
2870 -- attempt to optimize things.
2871
2872 if No (N) then
2873 return;
2874 end if;
2875 end loop;
2876
2877 -- Now we have N pointing to a node whose parent is the IF
2878 -- statement in question, so now we can tell if we are within
2879 -- the THEN statements.
2880
2881 if Is_List_Member (N)
2882 and then List_Containing (N) = Then_Statements (CV)
2883 then
2884 Sens := True;
2885
2886 -- If the variable reference does not come from source, we
2887 -- cannot reliably tell whether it appears in the else part.
2888 -- In particular, if it appears in generated code for a node
2889 -- that requires finalization, it may be attached to a list
2890 -- that has not been yet inserted into the code. For now,
2891 -- treat it as unknown.
2892
2893 elsif not Comes_From_Source (N) then
2894 return;
2895
2896 -- Otherwise we must be in ELSIF or ELSE part
2897
2898 else
2899 Sens := False;
2900 end if;
2901 end;
2902
2903 -- ELSIF part. Condition is known true within the referenced
2904 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
2905 -- and unknown before the ELSE part or after the IF statement.
2906
2907 elsif Nkind (CV) = N_Elsif_Part then
2908
2909 -- if the Elsif_Part had condition_actions, the elsif has been
2910 -- rewritten as a nested if, and the original elsif_part is
2911 -- detached from the tree, so there is no way to obtain useful
2912 -- information on the current value of the variable.
2913 -- Can this be improved ???
2914
2915 if No (Parent (CV)) then
2916 return;
2917 end if;
2918
2919 Stm := Parent (CV);
2920
2921 -- Before start of ELSIF part
2922
2923 if Loc < Sloc (CV) then
2924 return;
2925
2926 -- After end of IF statement
2927
2928 elsif Loc >= Sloc (Stm) +
2929 Text_Ptr (UI_To_Int (End_Span (Stm)))
2930 then
2931 return;
2932 end if;
2933
2934 -- Again we lack the SLOC of the ELSE, so we need to climb the
2935 -- tree to see if we are within the ELSIF part in question.
2936
2937 declare
2938 N : Node_Id;
2939
2940 begin
2941 N := Parent (Var);
2942 while Parent (N) /= Stm loop
2943 N := Parent (N);
2944
2945 -- If we fall off the top of the tree, then that's odd, but
2946 -- perhaps it could occur in some error situation, and the
2947 -- safest response is simply to assume that the outcome of
2948 -- the condition is unknown. No point in bombing during an
2949 -- attempt to optimize things.
2950
2951 if No (N) then
2952 return;
2953 end if;
2954 end loop;
2955
2956 -- Now we have N pointing to a node whose parent is the IF
2957 -- statement in question, so see if is the ELSIF part we want.
2958 -- the THEN statements.
2959
2960 if N = CV then
2961 Sens := True;
2962
2963 -- Otherwise we must be in subsequent ELSIF or ELSE part
2964
2965 else
2966 Sens := False;
2967 end if;
2968 end;
2969
2970 -- Iteration scheme of while loop. The condition is known to be
2971 -- true within the body of the loop.
2972
2973 elsif Nkind (CV) = N_Iteration_Scheme then
2974 declare
2975 Loop_Stmt : constant Node_Id := Parent (CV);
2976
2977 begin
2978 -- Before start of body of loop
2979
2980 if Loc < Sloc (Loop_Stmt) then
2981 return;
2982
2983 -- After end of LOOP statement
2984
2985 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2986 return;
2987
2988 -- We are within the body of the loop
2989
2990 else
2991 Sens := True;
2992 end if;
2993 end;
2994
2995 -- All other cases of Current_Value settings
2996
2997 else
2998 return;
2999 end if;
3000
3001 -- If we fall through here, then we have a reportable condition, Sens
3002 -- is True if the condition is true and False if it needs inverting.
3003
3004 Process_Current_Value_Condition (Condition (CV), Sens);
3005 end;
3006 end Get_Current_Value_Condition;
3007
3008 ---------------------
3009 -- Get_Stream_Size --
3010 ---------------------
3011
3012 function Get_Stream_Size (E : Entity_Id) return Uint is
3013 begin
3014 -- If we have a Stream_Size clause for this type use it
3015
3016 if Has_Stream_Size_Clause (E) then
3017 return Static_Integer (Expression (Stream_Size_Clause (E)));
3018
3019 -- Otherwise the Stream_Size if the size of the type
3020
3021 else
3022 return Esize (E);
3023 end if;
3024 end Get_Stream_Size;
3025
3026 ---------------------------
3027 -- Has_Access_Constraint --
3028 ---------------------------
3029
3030 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3031 Disc : Entity_Id;
3032 T : constant Entity_Id := Etype (E);
3033
3034 begin
3035 if Has_Per_Object_Constraint (E)
3036 and then Has_Discriminants (T)
3037 then
3038 Disc := First_Discriminant (T);
3039 while Present (Disc) loop
3040 if Is_Access_Type (Etype (Disc)) then
3041 return True;
3042 end if;
3043
3044 Next_Discriminant (Disc);
3045 end loop;
3046
3047 return False;
3048 else
3049 return False;
3050 end if;
3051 end Has_Access_Constraint;
3052
3053 ----------------------------------
3054 -- Has_Following_Address_Clause --
3055 ----------------------------------
3056
3057 -- Should this function check the private part in a package ???
3058
3059 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
3060 Id : constant Entity_Id := Defining_Identifier (D);
3061 Decl : Node_Id;
3062
3063 begin
3064 Decl := Next (D);
3065 while Present (Decl) loop
3066 if Nkind (Decl) = N_At_Clause
3067 and then Chars (Identifier (Decl)) = Chars (Id)
3068 then
3069 return True;
3070
3071 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3072 and then Chars (Decl) = Name_Address
3073 and then Chars (Name (Decl)) = Chars (Id)
3074 then
3075 return True;
3076 end if;
3077
3078 Next (Decl);
3079 end loop;
3080
3081 return False;
3082 end Has_Following_Address_Clause;
3083
3084 --------------------
3085 -- Homonym_Number --
3086 --------------------
3087
3088 function Homonym_Number (Subp : Entity_Id) return Nat is
3089 Count : Nat;
3090 Hom : Entity_Id;
3091
3092 begin
3093 Count := 1;
3094 Hom := Homonym (Subp);
3095 while Present (Hom) loop
3096 if Scope (Hom) = Scope (Subp) then
3097 Count := Count + 1;
3098 end if;
3099
3100 Hom := Homonym (Hom);
3101 end loop;
3102
3103 return Count;
3104 end Homonym_Number;
3105
3106 -----------------------------------
3107 -- In_Library_Level_Package_Body --
3108 -----------------------------------
3109
3110 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3111 begin
3112 -- First determine whether the entity appears at the library level, then
3113 -- look at the containing unit.
3114
3115 if Is_Library_Level_Entity (Id) then
3116 declare
3117 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3118
3119 begin
3120 return Nkind (Unit (Container)) = N_Package_Body;
3121 end;
3122 end if;
3123
3124 return False;
3125 end In_Library_Level_Package_Body;
3126
3127 ------------------------------
3128 -- In_Unconditional_Context --
3129 ------------------------------
3130
3131 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3132 P : Node_Id;
3133
3134 begin
3135 P := Node;
3136 while Present (P) loop
3137 case Nkind (P) is
3138 when N_Subprogram_Body =>
3139 return True;
3140
3141 when N_If_Statement =>
3142 return False;
3143
3144 when N_Loop_Statement =>
3145 return False;
3146
3147 when N_Case_Statement =>
3148 return False;
3149
3150 when others =>
3151 P := Parent (P);
3152 end case;
3153 end loop;
3154
3155 return False;
3156 end In_Unconditional_Context;
3157
3158 -------------------
3159 -- Insert_Action --
3160 -------------------
3161
3162 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3163 begin
3164 if Present (Ins_Action) then
3165 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3166 end if;
3167 end Insert_Action;
3168
3169 -- Version with check(s) suppressed
3170
3171 procedure Insert_Action
3172 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3173 is
3174 begin
3175 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3176 end Insert_Action;
3177
3178 -------------------------
3179 -- Insert_Action_After --
3180 -------------------------
3181
3182 procedure Insert_Action_After
3183 (Assoc_Node : Node_Id;
3184 Ins_Action : Node_Id)
3185 is
3186 begin
3187 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3188 end Insert_Action_After;
3189
3190 --------------------
3191 -- Insert_Actions --
3192 --------------------
3193
3194 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3195 N : Node_Id;
3196 P : Node_Id;
3197
3198 Wrapped_Node : Node_Id := Empty;
3199
3200 begin
3201 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3202 return;
3203 end if;
3204
3205 -- Ignore insert of actions from inside default expression (or other
3206 -- similar "spec expression") in the special spec-expression analyze
3207 -- mode. Any insertions at this point have no relevance, since we are
3208 -- only doing the analyze to freeze the types of any static expressions.
3209 -- See section "Handling of Default Expressions" in the spec of package
3210 -- Sem for further details.
3211
3212 if In_Spec_Expression then
3213 return;
3214 end if;
3215
3216 -- If the action derives from stuff inside a record, then the actions
3217 -- are attached to the current scope, to be inserted and analyzed on
3218 -- exit from the scope. The reason for this is that we may also be
3219 -- generating freeze actions at the same time, and they must eventually
3220 -- be elaborated in the correct order.
3221
3222 if Is_Record_Type (Current_Scope)
3223 and then not Is_Frozen (Current_Scope)
3224 then
3225 if No (Scope_Stack.Table
3226 (Scope_Stack.Last).Pending_Freeze_Actions)
3227 then
3228 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3229 Ins_Actions;
3230 else
3231 Append_List
3232 (Ins_Actions,
3233 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3234 end if;
3235
3236 return;
3237 end if;
3238
3239 -- We now intend to climb up the tree to find the right point to
3240 -- insert the actions. We start at Assoc_Node, unless this node is a
3241 -- subexpression in which case we start with its parent. We do this for
3242 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3243 -- itself one of the special nodes like N_And_Then, then we assume that
3244 -- an initial request to insert actions for such a node does not expect
3245 -- the actions to get deposited in the node for later handling when the
3246 -- node is expanded, since clearly the node is being dealt with by the
3247 -- caller. Note that in the subexpression case, N is always the child we
3248 -- came from.
3249
3250 -- N_Raise_xxx_Error is an annoying special case, it is a statement if
3251 -- it has type Standard_Void_Type, and a subexpression otherwise.
3252 -- otherwise. Procedure attribute references are also statements.
3253
3254 if Nkind (Assoc_Node) in N_Subexpr
3255 and then (Nkind (Assoc_Node) in N_Raise_xxx_Error
3256 or else Etype (Assoc_Node) /= Standard_Void_Type)
3257 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
3258 or else
3259 not Is_Procedure_Attribute_Name
3260 (Attribute_Name (Assoc_Node)))
3261 then
3262 P := Assoc_Node; -- ??? does not agree with above!
3263 N := Parent (Assoc_Node);
3264
3265 -- Non-subexpression case. Note that N is initially Empty in this case
3266 -- (N is only guaranteed Non-Empty in the subexpr case).
3267
3268 else
3269 P := Assoc_Node;
3270 N := Empty;
3271 end if;
3272
3273 -- Capture root of the transient scope
3274
3275 if Scope_Is_Transient then
3276 Wrapped_Node := Node_To_Be_Wrapped;
3277 end if;
3278
3279 loop
3280 pragma Assert (Present (P));
3281
3282 case Nkind (P) is
3283
3284 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3285 -- in the Actions field of the right operand. They will be moved
3286 -- out further when the AND THEN or OR ELSE operator is expanded.
3287 -- Nothing special needs to be done for the left operand since
3288 -- in that case the actions are executed unconditionally.
3289
3290 when N_Short_Circuit =>
3291 if N = Right_Opnd (P) then
3292
3293 -- We are now going to either append the actions to the
3294 -- actions field of the short-circuit operation. We will
3295 -- also analyze the actions now.
3296
3297 -- This analysis is really too early, the proper thing would
3298 -- be to just park them there now, and only analyze them if
3299 -- we find we really need them, and to it at the proper
3300 -- final insertion point. However attempting to this proved
3301 -- tricky, so for now we just kill current values before and
3302 -- after the analyze call to make sure we avoid peculiar
3303 -- optimizations from this out of order insertion.
3304
3305 Kill_Current_Values;
3306
3307 if Present (Actions (P)) then
3308 Insert_List_After_And_Analyze
3309 (Last (Actions (P)), Ins_Actions);
3310 else
3311 Set_Actions (P, Ins_Actions);
3312 Analyze_List (Actions (P));
3313 end if;
3314
3315 Kill_Current_Values;
3316
3317 return;
3318 end if;
3319
3320 -- Then or Else dependent expression of an if expression. Add
3321 -- actions to Then_Actions or Else_Actions field as appropriate.
3322 -- The actions will be moved further out when the if is expanded.
3323
3324 when N_If_Expression =>
3325 declare
3326 ThenX : constant Node_Id := Next (First (Expressions (P)));
3327 ElseX : constant Node_Id := Next (ThenX);
3328
3329 begin
3330 -- If the enclosing expression is already analyzed, as
3331 -- is the case for nested elaboration checks, insert the
3332 -- conditional further out.
3333
3334 if Analyzed (P) then
3335 null;
3336
3337 -- Actions belong to the then expression, temporarily place
3338 -- them as Then_Actions of the if expression. They will be
3339 -- moved to the proper place later when the if expression
3340 -- is expanded.
3341
3342 elsif N = ThenX then
3343 if Present (Then_Actions (P)) then
3344 Insert_List_After_And_Analyze
3345 (Last (Then_Actions (P)), Ins_Actions);
3346 else
3347 Set_Then_Actions (P, Ins_Actions);
3348 Analyze_List (Then_Actions (P));
3349 end if;
3350
3351 return;
3352
3353 -- Actions belong to the else expression, temporarily place
3354 -- them as Else_Actions of the if expression. They will be
3355 -- moved to the proper place later when the if expression
3356 -- is expanded.
3357
3358 elsif N = ElseX then
3359 if Present (Else_Actions (P)) then
3360 Insert_List_After_And_Analyze
3361 (Last (Else_Actions (P)), Ins_Actions);
3362 else
3363 Set_Else_Actions (P, Ins_Actions);
3364 Analyze_List (Else_Actions (P));
3365 end if;
3366
3367 return;
3368
3369 -- Actions belong to the condition. In this case they are
3370 -- unconditionally executed, and so we can continue the
3371 -- search for the proper insert point.
3372
3373 else
3374 null;
3375 end if;
3376 end;
3377
3378 -- Alternative of case expression, we place the action in the
3379 -- Actions field of the case expression alternative, this will
3380 -- be handled when the case expression is expanded.
3381
3382 when N_Case_Expression_Alternative =>
3383 if Present (Actions (P)) then
3384 Insert_List_After_And_Analyze
3385 (Last (Actions (P)), Ins_Actions);
3386 else
3387 Set_Actions (P, Ins_Actions);
3388 Analyze_List (Actions (P));
3389 end if;
3390
3391 return;
3392
3393 -- Case of appearing within an Expressions_With_Actions node. We
3394 -- prepend the actions to the list of actions already there, if
3395 -- the node has not been analyzed yet. Otherwise find insertion
3396 -- location further up the tree.
3397
3398 when N_Expression_With_Actions =>
3399 if not Analyzed (P) then
3400 Prepend_List (Ins_Actions, Actions (P));
3401 return;
3402 end if;
3403
3404 -- Case of appearing in the condition of a while expression or
3405 -- elsif. We insert the actions into the Condition_Actions field.
3406 -- They will be moved further out when the while loop or elsif
3407 -- is analyzed.
3408
3409 when N_Iteration_Scheme |
3410 N_Elsif_Part
3411 =>
3412 if N = Condition (P) then
3413 if Present (Condition_Actions (P)) then
3414 Insert_List_After_And_Analyze
3415 (Last (Condition_Actions (P)), Ins_Actions);
3416 else
3417 Set_Condition_Actions (P, Ins_Actions);
3418
3419 -- Set the parent of the insert actions explicitly. This
3420 -- is not a syntactic field, but we need the parent field
3421 -- set, in particular so that freeze can understand that
3422 -- it is dealing with condition actions, and properly
3423 -- insert the freezing actions.
3424
3425 Set_Parent (Ins_Actions, P);
3426 Analyze_List (Condition_Actions (P));
3427 end if;
3428
3429 return;
3430 end if;
3431
3432 -- Statements, declarations, pragmas, representation clauses
3433
3434 when
3435 -- Statements
3436
3437 N_Procedure_Call_Statement |
3438 N_Statement_Other_Than_Procedure_Call |
3439
3440 -- Pragmas
3441
3442 N_Pragma |
3443
3444 -- Representation_Clause
3445
3446 N_At_Clause |
3447 N_Attribute_Definition_Clause |
3448 N_Enumeration_Representation_Clause |
3449 N_Record_Representation_Clause |
3450
3451 -- Declarations
3452
3453 N_Abstract_Subprogram_Declaration |
3454 N_Entry_Body |
3455 N_Exception_Declaration |
3456 N_Exception_Renaming_Declaration |
3457 N_Expression_Function |
3458 N_Formal_Abstract_Subprogram_Declaration |
3459 N_Formal_Concrete_Subprogram_Declaration |
3460 N_Formal_Object_Declaration |
3461 N_Formal_Type_Declaration |
3462 N_Full_Type_Declaration |
3463 N_Function_Instantiation |
3464 N_Generic_Function_Renaming_Declaration |
3465 N_Generic_Package_Declaration |
3466 N_Generic_Package_Renaming_Declaration |
3467 N_Generic_Procedure_Renaming_Declaration |
3468 N_Generic_Subprogram_Declaration |
3469 N_Implicit_Label_Declaration |
3470 N_Incomplete_Type_Declaration |
3471 N_Number_Declaration |
3472 N_Object_Declaration |
3473 N_Object_Renaming_Declaration |
3474 N_Package_Body |
3475 N_Package_Body_Stub |
3476 N_Package_Declaration |
3477 N_Package_Instantiation |
3478 N_Package_Renaming_Declaration |
3479 N_Private_Extension_Declaration |
3480 N_Private_Type_Declaration |
3481 N_Procedure_Instantiation |
3482 N_Protected_Body |
3483 N_Protected_Body_Stub |
3484 N_Protected_Type_Declaration |
3485 N_Single_Task_Declaration |
3486 N_Subprogram_Body |
3487 N_Subprogram_Body_Stub |
3488 N_Subprogram_Declaration |
3489 N_Subprogram_Renaming_Declaration |
3490 N_Subtype_Declaration |
3491 N_Task_Body |
3492 N_Task_Body_Stub |
3493 N_Task_Type_Declaration |
3494
3495 -- Use clauses can appear in lists of declarations
3496
3497 N_Use_Package_Clause |
3498 N_Use_Type_Clause |
3499
3500 -- Freeze entity behaves like a declaration or statement
3501
3502 N_Freeze_Entity
3503 =>
3504 -- Do not insert here if the item is not a list member (this
3505 -- happens for example with a triggering statement, and the
3506 -- proper approach is to insert before the entire select).
3507
3508 if not Is_List_Member (P) then
3509 null;
3510
3511 -- Do not insert if parent of P is an N_Component_Association
3512 -- node (i.e. we are in the context of an N_Aggregate or
3513 -- N_Extension_Aggregate node. In this case we want to insert
3514 -- before the entire aggregate.
3515
3516 elsif Nkind (Parent (P)) = N_Component_Association then
3517 null;
3518
3519 -- Do not insert if the parent of P is either an N_Variant node
3520 -- or an N_Record_Definition node, meaning in either case that
3521 -- P is a member of a component list, and that therefore the
3522 -- actions should be inserted outside the complete record
3523 -- declaration.
3524
3525 elsif Nkind (Parent (P)) = N_Variant
3526 or else Nkind (Parent (P)) = N_Record_Definition
3527 then
3528 null;
3529
3530 -- Do not insert freeze nodes within the loop generated for
3531 -- an aggregate, because they may be elaborated too late for
3532 -- subsequent use in the back end: within a package spec the
3533 -- loop is part of the elaboration procedure and is only
3534 -- elaborated during the second pass.
3535
3536 -- If the loop comes from source, or the entity is local to the
3537 -- loop itself it must remain within.
3538
3539 elsif Nkind (Parent (P)) = N_Loop_Statement
3540 and then not Comes_From_Source (Parent (P))
3541 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
3542 and then
3543 Scope (Entity (First (Ins_Actions))) /= Current_Scope
3544 then
3545 null;
3546
3547 -- Otherwise we can go ahead and do the insertion
3548
3549 elsif P = Wrapped_Node then
3550 Store_Before_Actions_In_Scope (Ins_Actions);
3551 return;
3552
3553 else
3554 Insert_List_Before_And_Analyze (P, Ins_Actions);
3555 return;
3556 end if;
3557
3558 -- A special case, N_Raise_xxx_Error can act either as a statement
3559 -- or a subexpression. We tell the difference by looking at the
3560 -- Etype. It is set to Standard_Void_Type in the statement case.
3561
3562 when
3563 N_Raise_xxx_Error =>
3564 if Etype (P) = Standard_Void_Type then
3565 if P = Wrapped_Node then
3566 Store_Before_Actions_In_Scope (Ins_Actions);
3567 else
3568 Insert_List_Before_And_Analyze (P, Ins_Actions);
3569 end if;
3570
3571 return;
3572
3573 -- In the subexpression case, keep climbing
3574
3575 else
3576 null;
3577 end if;
3578
3579 -- If a component association appears within a loop created for
3580 -- an array aggregate, attach the actions to the association so
3581 -- they can be subsequently inserted within the loop. For other
3582 -- component associations insert outside of the aggregate. For
3583 -- an association that will generate a loop, its Loop_Actions
3584 -- attribute is already initialized (see exp_aggr.adb).
3585
3586 -- The list of loop_actions can in turn generate additional ones,
3587 -- that are inserted before the associated node. If the associated
3588 -- node is outside the aggregate, the new actions are collected
3589 -- at the end of the loop actions, to respect the order in which
3590 -- they are to be elaborated.
3591
3592 when
3593 N_Component_Association =>
3594 if Nkind (Parent (P)) = N_Aggregate
3595 and then Present (Loop_Actions (P))
3596 then
3597 if Is_Empty_List (Loop_Actions (P)) then
3598 Set_Loop_Actions (P, Ins_Actions);
3599 Analyze_List (Ins_Actions);
3600
3601 else
3602 declare
3603 Decl : Node_Id;
3604
3605 begin
3606 -- Check whether these actions were generated by a
3607 -- declaration that is part of the loop_ actions
3608 -- for the component_association.
3609
3610 Decl := Assoc_Node;
3611 while Present (Decl) loop
3612 exit when Parent (Decl) = P
3613 and then Is_List_Member (Decl)
3614 and then
3615 List_Containing (Decl) = Loop_Actions (P);
3616 Decl := Parent (Decl);
3617 end loop;
3618
3619 if Present (Decl) then
3620 Insert_List_Before_And_Analyze
3621 (Decl, Ins_Actions);
3622 else
3623 Insert_List_After_And_Analyze
3624 (Last (Loop_Actions (P)), Ins_Actions);
3625 end if;
3626 end;
3627 end if;
3628
3629 return;
3630
3631 else
3632 null;
3633 end if;
3634
3635 -- Another special case, an attribute denoting a procedure call
3636
3637 when
3638 N_Attribute_Reference =>
3639 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
3640 if P = Wrapped_Node then
3641 Store_Before_Actions_In_Scope (Ins_Actions);
3642 else
3643 Insert_List_Before_And_Analyze (P, Ins_Actions);
3644 end if;
3645
3646 return;
3647
3648 -- In the subexpression case, keep climbing
3649
3650 else
3651 null;
3652 end if;
3653
3654 -- A contract node should not belong to the tree
3655
3656 when N_Contract =>
3657 raise Program_Error;
3658
3659 -- For all other node types, keep climbing tree
3660
3661 when
3662 N_Abortable_Part |
3663 N_Accept_Alternative |
3664 N_Access_Definition |
3665 N_Access_Function_Definition |
3666 N_Access_Procedure_Definition |
3667 N_Access_To_Object_Definition |
3668 N_Aggregate |
3669 N_Allocator |
3670 N_Aspect_Specification |
3671 N_Case_Expression |
3672 N_Case_Statement_Alternative |
3673 N_Character_Literal |
3674 N_Compilation_Unit |
3675 N_Compilation_Unit_Aux |
3676 N_Component_Clause |
3677 N_Component_Declaration |
3678 N_Component_Definition |
3679 N_Component_List |
3680 N_Constrained_Array_Definition |
3681 N_Decimal_Fixed_Point_Definition |
3682 N_Defining_Character_Literal |
3683 N_Defining_Identifier |
3684 N_Defining_Operator_Symbol |
3685 N_Defining_Program_Unit_Name |
3686 N_Delay_Alternative |
3687 N_Delta_Constraint |
3688 N_Derived_Type_Definition |
3689 N_Designator |
3690 N_Digits_Constraint |
3691 N_Discriminant_Association |
3692 N_Discriminant_Specification |
3693 N_Empty |
3694 N_Entry_Body_Formal_Part |
3695 N_Entry_Call_Alternative |
3696 N_Entry_Declaration |
3697 N_Entry_Index_Specification |
3698 N_Enumeration_Type_Definition |
3699 N_Error |
3700 N_Exception_Handler |
3701 N_Expanded_Name |
3702 N_Explicit_Dereference |
3703 N_Extension_Aggregate |
3704 N_Floating_Point_Definition |
3705 N_Formal_Decimal_Fixed_Point_Definition |
3706 N_Formal_Derived_Type_Definition |
3707 N_Formal_Discrete_Type_Definition |
3708 N_Formal_Floating_Point_Definition |
3709 N_Formal_Modular_Type_Definition |
3710 N_Formal_Ordinary_Fixed_Point_Definition |
3711 N_Formal_Package_Declaration |
3712 N_Formal_Private_Type_Definition |
3713 N_Formal_Incomplete_Type_Definition |
3714 N_Formal_Signed_Integer_Type_Definition |
3715 N_Function_Call |
3716 N_Function_Specification |
3717 N_Generic_Association |
3718 N_Handled_Sequence_Of_Statements |
3719 N_Identifier |
3720 N_In |
3721 N_Index_Or_Discriminant_Constraint |
3722 N_Indexed_Component |
3723 N_Integer_Literal |
3724 N_Iterator_Specification |
3725 N_Itype_Reference |
3726 N_Label |
3727 N_Loop_Parameter_Specification |
3728 N_Mod_Clause |
3729 N_Modular_Type_Definition |
3730 N_Not_In |
3731 N_Null |
3732 N_Op_Abs |
3733 N_Op_Add |
3734 N_Op_And |
3735 N_Op_Concat |
3736 N_Op_Divide |
3737 N_Op_Eq |
3738 N_Op_Expon |
3739 N_Op_Ge |
3740 N_Op_Gt |
3741 N_Op_Le |
3742 N_Op_Lt |
3743 N_Op_Minus |
3744 N_Op_Mod |
3745 N_Op_Multiply |
3746 N_Op_Ne |
3747 N_Op_Not |
3748 N_Op_Or |
3749 N_Op_Plus |
3750 N_Op_Rem |
3751 N_Op_Rotate_Left |
3752 N_Op_Rotate_Right |
3753 N_Op_Shift_Left |
3754 N_Op_Shift_Right |
3755 N_Op_Shift_Right_Arithmetic |
3756 N_Op_Subtract |
3757 N_Op_Xor |
3758 N_Operator_Symbol |
3759 N_Ordinary_Fixed_Point_Definition |
3760 N_Others_Choice |
3761 N_Package_Specification |
3762 N_Parameter_Association |
3763 N_Parameter_Specification |
3764 N_Pop_Constraint_Error_Label |
3765 N_Pop_Program_Error_Label |
3766 N_Pop_Storage_Error_Label |
3767 N_Pragma_Argument_Association |
3768 N_Procedure_Specification |
3769 N_Protected_Definition |
3770 N_Push_Constraint_Error_Label |
3771 N_Push_Program_Error_Label |
3772 N_Push_Storage_Error_Label |
3773 N_Qualified_Expression |
3774 N_Quantified_Expression |
3775 N_Range |
3776 N_Range_Constraint |
3777 N_Real_Literal |
3778 N_Real_Range_Specification |
3779 N_Record_Definition |
3780 N_Reference |
3781 N_SCIL_Dispatch_Table_Tag_Init |
3782 N_SCIL_Dispatching_Call |
3783 N_SCIL_Membership_Test |
3784 N_Selected_Component |
3785 N_Signed_Integer_Type_Definition |
3786 N_Single_Protected_Declaration |
3787 N_Slice |
3788 N_String_Literal |
3789 N_Subprogram_Info |
3790 N_Subtype_Indication |
3791 N_Subunit |
3792 N_Task_Definition |
3793 N_Terminate_Alternative |
3794 N_Triggering_Alternative |
3795 N_Type_Conversion |
3796 N_Unchecked_Expression |
3797 N_Unchecked_Type_Conversion |
3798 N_Unconstrained_Array_Definition |
3799 N_Unused_At_End |
3800 N_Unused_At_Start |
3801 N_Variant |
3802 N_Variant_Part |
3803 N_Validate_Unchecked_Conversion |
3804 N_With_Clause
3805 =>
3806 null;
3807
3808 end case;
3809
3810 -- Make sure that inserted actions stay in the transient scope
3811
3812 if P = Wrapped_Node then
3813 Store_Before_Actions_In_Scope (Ins_Actions);
3814 return;
3815 end if;
3816
3817 -- If we fall through above tests, keep climbing tree
3818
3819 N := P;
3820
3821 if Nkind (Parent (N)) = N_Subunit then
3822
3823 -- This is the proper body corresponding to a stub. Insertion must
3824 -- be done at the point of the stub, which is in the declarative
3825 -- part of the parent unit.
3826
3827 P := Corresponding_Stub (Parent (N));
3828
3829 else
3830 P := Parent (N);
3831 end if;
3832 end loop;
3833 end Insert_Actions;
3834
3835 -- Version with check(s) suppressed
3836
3837 procedure Insert_Actions
3838 (Assoc_Node : Node_Id;
3839 Ins_Actions : List_Id;
3840 Suppress : Check_Id)
3841 is
3842 begin
3843 if Suppress = All_Checks then
3844 declare
3845 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3846 begin
3847 Scope_Suppress.Suppress := (others => True);
3848 Insert_Actions (Assoc_Node, Ins_Actions);
3849 Scope_Suppress.Suppress := Sva;
3850 end;
3851
3852 else
3853 declare
3854 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3855 begin
3856 Scope_Suppress.Suppress (Suppress) := True;
3857 Insert_Actions (Assoc_Node, Ins_Actions);
3858 Scope_Suppress.Suppress (Suppress) := Svg;
3859 end;
3860 end if;
3861 end Insert_Actions;
3862
3863 --------------------------
3864 -- Insert_Actions_After --
3865 --------------------------
3866
3867 procedure Insert_Actions_After
3868 (Assoc_Node : Node_Id;
3869 Ins_Actions : List_Id)
3870 is
3871 begin
3872 if Scope_Is_Transient
3873 and then Assoc_Node = Node_To_Be_Wrapped
3874 then
3875 Store_After_Actions_In_Scope (Ins_Actions);
3876 else
3877 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
3878 end if;
3879 end Insert_Actions_After;
3880
3881 ---------------------------------
3882 -- Insert_Library_Level_Action --
3883 ---------------------------------
3884
3885 procedure Insert_Library_Level_Action (N : Node_Id) is
3886 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3887
3888 begin
3889 Push_Scope (Cunit_Entity (Main_Unit));
3890 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
3891
3892 if No (Actions (Aux)) then
3893 Set_Actions (Aux, New_List (N));
3894 else
3895 Append (N, Actions (Aux));
3896 end if;
3897
3898 Analyze (N);
3899 Pop_Scope;
3900 end Insert_Library_Level_Action;
3901
3902 ----------------------------------
3903 -- Insert_Library_Level_Actions --
3904 ----------------------------------
3905
3906 procedure Insert_Library_Level_Actions (L : List_Id) is
3907 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3908
3909 begin
3910 if Is_Non_Empty_List (L) then
3911 Push_Scope (Cunit_Entity (Main_Unit));
3912 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
3913
3914 if No (Actions (Aux)) then
3915 Set_Actions (Aux, L);
3916 Analyze_List (L);
3917 else
3918 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
3919 end if;
3920
3921 Pop_Scope;
3922 end if;
3923 end Insert_Library_Level_Actions;
3924
3925 ----------------------
3926 -- Inside_Init_Proc --
3927 ----------------------
3928
3929 function Inside_Init_Proc return Boolean is
3930 S : Entity_Id;
3931
3932 begin
3933 S := Current_Scope;
3934 while Present (S)
3935 and then S /= Standard_Standard
3936 loop
3937 if Is_Init_Proc (S) then
3938 return True;
3939 else
3940 S := Scope (S);
3941 end if;
3942 end loop;
3943
3944 return False;
3945 end Inside_Init_Proc;
3946
3947 ----------------------------
3948 -- Is_All_Null_Statements --
3949 ----------------------------
3950
3951 function Is_All_Null_Statements (L : List_Id) return Boolean is
3952 Stm : Node_Id;
3953
3954 begin
3955 Stm := First (L);
3956 while Present (Stm) loop
3957 if Nkind (Stm) /= N_Null_Statement then
3958 return False;
3959 end if;
3960
3961 Next (Stm);
3962 end loop;
3963
3964 return True;
3965 end Is_All_Null_Statements;
3966
3967 --------------------------------------------------
3968 -- Is_Displacement_Of_Object_Or_Function_Result --
3969 --------------------------------------------------
3970
3971 function Is_Displacement_Of_Object_Or_Function_Result
3972 (Obj_Id : Entity_Id) return Boolean
3973 is
3974 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
3975 -- Determine if particular node denotes a controlled function call
3976
3977 function Is_Displace_Call (N : Node_Id) return Boolean;
3978 -- Determine whether a particular node is a call to Ada.Tags.Displace.
3979 -- The call might be nested within other actions such as conversions.
3980
3981 function Is_Source_Object (N : Node_Id) return Boolean;
3982 -- Determine whether a particular node denotes a source object
3983
3984 ---------------------------------
3985 -- Is_Controlled_Function_Call --
3986 ---------------------------------
3987
3988 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
3989 Expr : Node_Id := Original_Node (N);
3990
3991 begin
3992 if Nkind (Expr) = N_Function_Call then
3993 Expr := Name (Expr);
3994 end if;
3995
3996 -- The function call may appear in object.operation format
3997
3998 if Nkind (Expr) = N_Selected_Component then
3999 Expr := Selector_Name (Expr);
4000 end if;
4001
4002 return
4003 Nkind_In (Expr, N_Expanded_Name, N_Identifier)
4004 and then Ekind (Entity (Expr)) = E_Function
4005 and then Needs_Finalization (Etype (Entity (Expr)));
4006 end Is_Controlled_Function_Call;
4007
4008 ----------------------
4009 -- Is_Displace_Call --
4010 ----------------------
4011
4012 function Is_Displace_Call (N : Node_Id) return Boolean is
4013 Call : Node_Id := N;
4014
4015 begin
4016 -- Strip various actions which may precede a call to Displace
4017
4018 loop
4019 if Nkind (Call) = N_Explicit_Dereference then
4020 Call := Prefix (Call);
4021
4022 elsif Nkind_In (Call, N_Type_Conversion,
4023 N_Unchecked_Type_Conversion)
4024 then
4025 Call := Expression (Call);
4026
4027 else
4028 exit;
4029 end if;
4030 end loop;
4031
4032 return
4033 Present (Call)
4034 and then Nkind (Call) = N_Function_Call
4035 and then Is_RTE (Entity (Name (Call)), RE_Displace);
4036 end Is_Displace_Call;
4037
4038 ----------------------
4039 -- Is_Source_Object --
4040 ----------------------
4041
4042 function Is_Source_Object (N : Node_Id) return Boolean is
4043 begin
4044 return
4045 Present (N)
4046 and then Nkind (N) in N_Has_Entity
4047 and then Is_Object (Entity (N))
4048 and then Comes_From_Source (N);
4049 end Is_Source_Object;
4050
4051 -- Local variables
4052
4053 Decl : constant Node_Id := Parent (Obj_Id);
4054 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4055 Orig_Decl : constant Node_Id := Original_Node (Decl);
4056
4057 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
4058
4059 begin
4060 -- Case 1:
4061
4062 -- Obj : CW_Type := Function_Call (...);
4063
4064 -- rewritten into:
4065
4066 -- Tmp : ... := Function_Call (...)'reference;
4067 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
4068
4069 -- where the return type of the function and the class-wide type require
4070 -- dispatch table pointer displacement.
4071
4072 -- Case 2:
4073
4074 -- Obj : CW_Type := Src_Obj;
4075
4076 -- rewritten into:
4077
4078 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
4079
4080 -- where the type of the source object and the class-wide type require
4081 -- dispatch table pointer displacement.
4082
4083 return
4084 Nkind (Decl) = N_Object_Renaming_Declaration
4085 and then Nkind (Orig_Decl) = N_Object_Declaration
4086 and then Comes_From_Source (Orig_Decl)
4087 and then Is_Class_Wide_Type (Obj_Typ)
4088 and then Is_Displace_Call (Renamed_Object (Obj_Id))
4089 and then
4090 (Is_Controlled_Function_Call (Expression (Orig_Decl))
4091 or else Is_Source_Object (Expression (Orig_Decl)));
4092 end Is_Displacement_Of_Object_Or_Function_Result;
4093
4094 ------------------------------
4095 -- Is_Finalizable_Transient --
4096 ------------------------------
4097
4098 function Is_Finalizable_Transient
4099 (Decl : Node_Id;
4100 Rel_Node : Node_Id) return Boolean
4101 is
4102 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4103 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4104 Desig : Entity_Id := Obj_Typ;
4105
4106 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
4107 -- Determine whether transient object Trans_Id is initialized either
4108 -- by a function call which returns an access type or simply renames
4109 -- another pointer.
4110
4111 function Initialized_By_Aliased_BIP_Func_Call
4112 (Trans_Id : Entity_Id) return Boolean;
4113 -- Determine whether transient object Trans_Id is initialized by a
4114 -- build-in-place function call where the BIPalloc parameter is of
4115 -- value 1 and BIPaccess is not null. This case creates an aliasing
4116 -- between the returned value and the value denoted by BIPaccess.
4117
4118 function Is_Aliased
4119 (Trans_Id : Entity_Id;
4120 First_Stmt : Node_Id) return Boolean;
4121 -- Determine whether transient object Trans_Id has been renamed or
4122 -- aliased through 'reference in the statement list starting from
4123 -- First_Stmt.
4124
4125 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4126 -- Determine whether transient object Trans_Id is allocated on the heap
4127
4128 function Is_Iterated_Container
4129 (Trans_Id : Entity_Id;
4130 First_Stmt : Node_Id) return Boolean;
4131 -- Determine whether transient object Trans_Id denotes a container which
4132 -- is in the process of being iterated in the statement list starting
4133 -- from First_Stmt.
4134
4135 ---------------------------
4136 -- Initialized_By_Access --
4137 ---------------------------
4138
4139 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4140 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4141
4142 begin
4143 return
4144 Present (Expr)
4145 and then Nkind (Expr) /= N_Reference
4146 and then Is_Access_Type (Etype (Expr));
4147 end Initialized_By_Access;
4148
4149 ------------------------------------------
4150 -- Initialized_By_Aliased_BIP_Func_Call --
4151 ------------------------------------------
4152
4153 function Initialized_By_Aliased_BIP_Func_Call
4154 (Trans_Id : Entity_Id) return Boolean
4155 is
4156 Call : Node_Id := Expression (Parent (Trans_Id));
4157
4158 begin
4159 -- Build-in-place calls usually appear in 'reference format
4160
4161 if Nkind (Call) = N_Reference then
4162 Call := Prefix (Call);
4163 end if;
4164
4165 if Is_Build_In_Place_Function_Call (Call) then
4166 declare
4167 Access_Nam : Name_Id := No_Name;
4168 Access_OK : Boolean := False;
4169 Actual : Node_Id;
4170 Alloc_Nam : Name_Id := No_Name;
4171 Alloc_OK : Boolean := False;
4172 Formal : Node_Id;
4173 Func_Id : Entity_Id;
4174 Param : Node_Id;
4175
4176 begin
4177 -- Examine all parameter associations of the function call
4178
4179 Param := First (Parameter_Associations (Call));
4180 while Present (Param) loop
4181 if Nkind (Param) = N_Parameter_Association
4182 and then Nkind (Selector_Name (Param)) = N_Identifier
4183 then
4184 Actual := Explicit_Actual_Parameter (Param);
4185 Formal := Selector_Name (Param);
4186
4187 -- Construct the names of formals BIPaccess and BIPalloc
4188 -- using the function name retrieved from an arbitrary
4189 -- formal.
4190
4191 if Access_Nam = No_Name
4192 and then Alloc_Nam = No_Name
4193 and then Present (Entity (Formal))
4194 then
4195 Func_Id := Scope (Entity (Formal));
4196
4197 Access_Nam :=
4198 New_External_Name (Chars (Func_Id),
4199 BIP_Formal_Suffix (BIP_Object_Access));
4200
4201 Alloc_Nam :=
4202 New_External_Name (Chars (Func_Id),
4203 BIP_Formal_Suffix (BIP_Alloc_Form));
4204 end if;
4205
4206 -- A match for BIPaccess => Temp has been found
4207
4208 if Chars (Formal) = Access_Nam
4209 and then Nkind (Actual) /= N_Null
4210 then
4211 Access_OK := True;
4212 end if;
4213
4214 -- A match for BIPalloc => 1 has been found
4215
4216 if Chars (Formal) = Alloc_Nam
4217 and then Nkind (Actual) = N_Integer_Literal
4218 and then Intval (Actual) = Uint_1
4219 then
4220 Alloc_OK := True;
4221 end if;
4222 end if;
4223
4224 Next (Param);
4225 end loop;
4226
4227 return Access_OK and then Alloc_OK;
4228 end;
4229 end if;
4230
4231 return False;
4232 end Initialized_By_Aliased_BIP_Func_Call;
4233
4234 ----------------
4235 -- Is_Aliased --
4236 ----------------
4237
4238 function Is_Aliased
4239 (Trans_Id : Entity_Id;
4240 First_Stmt : Node_Id) return Boolean
4241 is
4242 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
4243 -- Given an object renaming declaration, retrieve the entity of the
4244 -- renamed name. Return Empty if the renamed name is anything other
4245 -- than a variable or a constant.
4246
4247 -------------------------
4248 -- Find_Renamed_Object --
4249 -------------------------
4250
4251 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
4252 Ren_Obj : Node_Id := Empty;
4253
4254 function Find_Object (N : Node_Id) return Traverse_Result;
4255 -- Try to detect an object which is either a constant or a
4256 -- variable.
4257
4258 -----------------
4259 -- Find_Object --
4260 -----------------
4261
4262 function Find_Object (N : Node_Id) return Traverse_Result is
4263 begin
4264 -- Stop the search once a constant or a variable has been
4265 -- detected.
4266
4267 if Nkind (N) = N_Identifier
4268 and then Present (Entity (N))
4269 and then Ekind_In (Entity (N), E_Constant, E_Variable)
4270 then
4271 Ren_Obj := Entity (N);
4272 return Abandon;
4273 end if;
4274
4275 return OK;
4276 end Find_Object;
4277
4278 procedure Search is new Traverse_Proc (Find_Object);
4279
4280 -- Local variables
4281
4282 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
4283
4284 -- Start of processing for Find_Renamed_Object
4285
4286 begin
4287 -- Actions related to dispatching calls may appear as renamings of
4288 -- tags. Do not process this type of renaming because it does not
4289 -- use the actual value of the object.
4290
4291 if not Is_RTE (Typ, RE_Tag_Ptr) then
4292 Search (Name (Ren_Decl));
4293 end if;
4294
4295 return Ren_Obj;
4296 end Find_Renamed_Object;
4297
4298 -- Local variables
4299
4300 Expr : Node_Id;
4301 Ren_Obj : Entity_Id;
4302 Stmt : Node_Id;
4303
4304 -- Start of processing for Is_Aliased
4305
4306 begin
4307 Stmt := First_Stmt;
4308 while Present (Stmt) loop
4309 if Nkind (Stmt) = N_Object_Declaration then
4310 Expr := Expression (Stmt);
4311
4312 if Present (Expr)
4313 and then Nkind (Expr) = N_Reference
4314 and then Nkind (Prefix (Expr)) = N_Identifier
4315 and then Entity (Prefix (Expr)) = Trans_Id
4316 then
4317 return True;
4318 end if;
4319
4320 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
4321 Ren_Obj := Find_Renamed_Object (Stmt);
4322
4323 if Present (Ren_Obj)
4324 and then Ren_Obj = Trans_Id
4325 then
4326 return True;
4327 end if;
4328 end if;
4329
4330 Next (Stmt);
4331 end loop;
4332
4333 return False;
4334 end Is_Aliased;
4335
4336 ------------------
4337 -- Is_Allocated --
4338 ------------------
4339
4340 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
4341 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4342 begin
4343 return
4344 Is_Access_Type (Etype (Trans_Id))
4345 and then Present (Expr)
4346 and then Nkind (Expr) = N_Allocator;
4347 end Is_Allocated;
4348
4349 ---------------------------
4350 -- Is_Iterated_Container --
4351 ---------------------------
4352
4353 function Is_Iterated_Container
4354 (Trans_Id : Entity_Id;
4355 First_Stmt : Node_Id) return Boolean
4356 is
4357 Aspect : Node_Id;
4358 Call : Node_Id;
4359 Iter : Entity_Id;
4360 Param : Node_Id;
4361 Stmt : Node_Id;
4362 Typ : Entity_Id;
4363
4364 begin
4365 -- It is not possible to iterate over containers in non-Ada 2012 code
4366
4367 if Ada_Version < Ada_2012 then
4368 return False;
4369 end if;
4370
4371 Typ := Etype (Trans_Id);
4372
4373 -- Handle access type created for secondary stack use
4374
4375 if Is_Access_Type (Typ) then
4376 Typ := Designated_Type (Typ);
4377 end if;
4378
4379 -- Look for aspect Default_Iterator
4380
4381 if Has_Aspects (Parent (Typ)) then
4382 Aspect := Find_Aspect (Typ, Aspect_Default_Iterator);
4383
4384 if Present (Aspect) then
4385 Iter := Entity (Aspect);
4386
4387 -- Examine the statements following the container object and
4388 -- look for a call to the default iterate routine where the
4389 -- first parameter is the transient. Such a call appears as:
4390
4391 -- It : Access_To_CW_Iterator :=
4392 -- Iterate (Tran_Id.all, ...)'reference;
4393
4394 Stmt := First_Stmt;
4395 while Present (Stmt) loop
4396
4397 -- Detect an object declaration which is initialized by a
4398 -- secondary stack function call.
4399
4400 if Nkind (Stmt) = N_Object_Declaration
4401 and then Present (Expression (Stmt))
4402 and then Nkind (Expression (Stmt)) = N_Reference
4403 and then Nkind (Prefix (Expression (Stmt))) =
4404 N_Function_Call
4405 then
4406 Call := Prefix (Expression (Stmt));
4407
4408 -- The call must invoke the default iterate routine of
4409 -- the container and the transient object must appear as
4410 -- the first actual parameter. Skip any calls whose names
4411 -- are not entities.
4412
4413 if Is_Entity_Name (Name (Call))
4414 and then Entity (Name (Call)) = Iter
4415 and then Present (Parameter_Associations (Call))
4416 then
4417 Param := First (Parameter_Associations (Call));
4418
4419 if Nkind (Param) = N_Explicit_Dereference
4420 and then Entity (Prefix (Param)) = Trans_Id
4421 then
4422 return True;
4423 end if;
4424 end if;
4425 end if;
4426
4427 Next (Stmt);
4428 end loop;
4429 end if;
4430 end if;
4431
4432 return False;
4433 end Is_Iterated_Container;
4434
4435 -- Start of processing for Is_Finalizable_Transient
4436
4437 begin
4438 -- Handle access types
4439
4440 if Is_Access_Type (Desig) then
4441 Desig := Available_View (Designated_Type (Desig));
4442 end if;
4443
4444 return
4445 Ekind_In (Obj_Id, E_Constant, E_Variable)
4446 and then Needs_Finalization (Desig)
4447 and then Requires_Transient_Scope (Desig)
4448 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
4449
4450 -- Do not consider renamed or 'reference-d transient objects because
4451 -- the act of renaming extends the object's lifetime.
4452
4453 and then not Is_Aliased (Obj_Id, Decl)
4454
4455 -- Do not consider transient objects allocated on the heap since
4456 -- they are attached to a finalization master.
4457
4458 and then not Is_Allocated (Obj_Id)
4459
4460 -- If the transient object is a pointer, check that it is not
4461 -- initialized by a function which returns a pointer or acts as a
4462 -- renaming of another pointer.
4463
4464 and then
4465 (not Is_Access_Type (Obj_Typ)
4466 or else not Initialized_By_Access (Obj_Id))
4467
4468 -- Do not consider transient objects which act as indirect aliases
4469 -- of build-in-place function results.
4470
4471 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
4472
4473 -- Do not consider conversions of tags to class-wide types
4474
4475 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
4476
4477 -- Do not consider containers in the context of iterator loops. Such
4478 -- transient objects must exist for as long as the loop is around,
4479 -- otherwise any operation carried out by the iterator will fail.
4480
4481 and then not Is_Iterated_Container (Obj_Id, Decl);
4482 end Is_Finalizable_Transient;
4483
4484 ---------------------------------
4485 -- Is_Fully_Repped_Tagged_Type --
4486 ---------------------------------
4487
4488 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
4489 U : constant Entity_Id := Underlying_Type (T);
4490 Comp : Entity_Id;
4491
4492 begin
4493 if No (U) or else not Is_Tagged_Type (U) then
4494 return False;
4495 elsif Has_Discriminants (U) then
4496 return False;
4497 elsif not Has_Specified_Layout (U) then
4498 return False;
4499 end if;
4500
4501 -- Here we have a tagged type, see if it has any unlayed out fields
4502 -- other than a possible tag and parent fields. If so, we return False.
4503
4504 Comp := First_Component (U);
4505 while Present (Comp) loop
4506 if not Is_Tag (Comp)
4507 and then Chars (Comp) /= Name_uParent
4508 and then No (Component_Clause (Comp))
4509 then
4510 return False;
4511 else
4512 Next_Component (Comp);
4513 end if;
4514 end loop;
4515
4516 -- All components are layed out
4517
4518 return True;
4519 end Is_Fully_Repped_Tagged_Type;
4520
4521 ----------------------------------
4522 -- Is_Library_Level_Tagged_Type --
4523 ----------------------------------
4524
4525 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
4526 begin
4527 return Is_Tagged_Type (Typ)
4528 and then Is_Library_Level_Entity (Typ);
4529 end Is_Library_Level_Tagged_Type;
4530
4531 --------------------------
4532 -- Is_Non_BIP_Func_Call --
4533 --------------------------
4534
4535 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
4536 begin
4537 -- The expected call is of the format
4538 --
4539 -- Func_Call'reference
4540
4541 return
4542 Nkind (Expr) = N_Reference
4543 and then Nkind (Prefix (Expr)) = N_Function_Call
4544 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
4545 end Is_Non_BIP_Func_Call;
4546
4547 ----------------------------------
4548 -- Is_Possibly_Unaligned_Object --
4549 ----------------------------------
4550
4551 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
4552 T : constant Entity_Id := Etype (N);
4553
4554 begin
4555 -- If renamed object, apply test to underlying object
4556
4557 if Is_Entity_Name (N)
4558 and then Is_Object (Entity (N))
4559 and then Present (Renamed_Object (Entity (N)))
4560 then
4561 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
4562 end if;
4563
4564 -- Tagged and controlled types and aliased types are always aligned, as
4565 -- are concurrent types.
4566
4567 if Is_Aliased (T)
4568 or else Has_Controlled_Component (T)
4569 or else Is_Concurrent_Type (T)
4570 or else Is_Tagged_Type (T)
4571 or else Is_Controlled (T)
4572 then
4573 return False;
4574 end if;
4575
4576 -- If this is an element of a packed array, may be unaligned
4577
4578 if Is_Ref_To_Bit_Packed_Array (N) then
4579 return True;
4580 end if;
4581
4582 -- Case of indexed component reference: test whether prefix is unaligned
4583
4584 if Nkind (N) = N_Indexed_Component then
4585 return Is_Possibly_Unaligned_Object (Prefix (N));
4586
4587 -- Case of selected component reference
4588
4589 elsif Nkind (N) = N_Selected_Component then
4590 declare
4591 P : constant Node_Id := Prefix (N);
4592 C : constant Entity_Id := Entity (Selector_Name (N));
4593 M : Nat;
4594 S : Nat;
4595
4596 begin
4597 -- If component reference is for an array with non-static bounds,
4598 -- then it is always aligned: we can only process unaligned arrays
4599 -- with static bounds (more precisely compile time known bounds).
4600
4601 if Is_Array_Type (T)
4602 and then not Compile_Time_Known_Bounds (T)
4603 then
4604 return False;
4605 end if;
4606
4607 -- If component is aliased, it is definitely properly aligned
4608
4609 if Is_Aliased (C) then
4610 return False;
4611 end if;
4612
4613 -- If component is for a type implemented as a scalar, and the
4614 -- record is packed, and the component is other than the first
4615 -- component of the record, then the component may be unaligned.
4616
4617 if Is_Packed (Etype (P))
4618 and then Represented_As_Scalar (Etype (C))
4619 and then First_Entity (Scope (C)) /= C
4620 then
4621 return True;
4622 end if;
4623
4624 -- Compute maximum possible alignment for T
4625
4626 -- If alignment is known, then that settles things
4627
4628 if Known_Alignment (T) then
4629 M := UI_To_Int (Alignment (T));
4630
4631 -- If alignment is not known, tentatively set max alignment
4632
4633 else
4634 M := Ttypes.Maximum_Alignment;
4635
4636 -- We can reduce this if the Esize is known since the default
4637 -- alignment will never be more than the smallest power of 2
4638 -- that does not exceed this Esize value.
4639
4640 if Known_Esize (T) then
4641 S := UI_To_Int (Esize (T));
4642
4643 while (M / 2) >= S loop
4644 M := M / 2;
4645 end loop;
4646 end if;
4647 end if;
4648
4649 -- The following code is historical, it used to be present but it
4650 -- is too cautious, because the front-end does not know the proper
4651 -- default alignments for the target. Also, if the alignment is
4652 -- not known, the front end can't know in any case! If a copy is
4653 -- needed, the back-end will take care of it. This whole section
4654 -- including this comment can be removed later ???
4655
4656 -- If the component reference is for a record that has a specified
4657 -- alignment, and we either know it is too small, or cannot tell,
4658 -- then the component may be unaligned.
4659
4660 -- What is the following commented out code ???
4661
4662 -- if Known_Alignment (Etype (P))
4663 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
4664 -- and then M > Alignment (Etype (P))
4665 -- then
4666 -- return True;
4667 -- end if;
4668
4669 -- Case of component clause present which may specify an
4670 -- unaligned position.
4671
4672 if Present (Component_Clause (C)) then
4673
4674 -- Otherwise we can do a test to make sure that the actual
4675 -- start position in the record, and the length, are both
4676 -- consistent with the required alignment. If not, we know
4677 -- that we are unaligned.
4678
4679 declare
4680 Align_In_Bits : constant Nat := M * System_Storage_Unit;
4681 begin
4682 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
4683 or else Esize (C) mod Align_In_Bits /= 0
4684 then
4685 return True;
4686 end if;
4687 end;
4688 end if;
4689
4690 -- Otherwise, for a component reference, test prefix
4691
4692 return Is_Possibly_Unaligned_Object (P);
4693 end;
4694
4695 -- If not a component reference, must be aligned
4696
4697 else
4698 return False;
4699 end if;
4700 end Is_Possibly_Unaligned_Object;
4701
4702 ---------------------------------
4703 -- Is_Possibly_Unaligned_Slice --
4704 ---------------------------------
4705
4706 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
4707 begin
4708 -- Go to renamed object
4709
4710 if Is_Entity_Name (N)
4711 and then Is_Object (Entity (N))
4712 and then Present (Renamed_Object (Entity (N)))
4713 then
4714 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
4715 end if;
4716
4717 -- The reference must be a slice
4718
4719 if Nkind (N) /= N_Slice then
4720 return False;
4721 end if;
4722
4723 -- Always assume the worst for a nested record component with a
4724 -- component clause, which gigi/gcc does not appear to handle well.
4725 -- It is not clear why this special test is needed at all ???
4726
4727 if Nkind (Prefix (N)) = N_Selected_Component
4728 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
4729 and then
4730 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
4731 then
4732 return True;
4733 end if;
4734
4735 -- We only need to worry if the target has strict alignment
4736
4737 if not Target_Strict_Alignment then
4738 return False;
4739 end if;
4740
4741 -- If it is a slice, then look at the array type being sliced
4742
4743 declare
4744 Sarr : constant Node_Id := Prefix (N);
4745 -- Prefix of the slice, i.e. the array being sliced
4746
4747 Styp : constant Entity_Id := Etype (Prefix (N));
4748 -- Type of the array being sliced
4749
4750 Pref : Node_Id;
4751 Ptyp : Entity_Id;
4752
4753 begin
4754 -- The problems arise if the array object that is being sliced
4755 -- is a component of a record or array, and we cannot guarantee
4756 -- the alignment of the array within its containing object.
4757
4758 -- To investigate this, we look at successive prefixes to see
4759 -- if we have a worrisome indexed or selected component.
4760
4761 Pref := Sarr;
4762 loop
4763 -- Case of array is part of an indexed component reference
4764
4765 if Nkind (Pref) = N_Indexed_Component then
4766 Ptyp := Etype (Prefix (Pref));
4767
4768 -- The only problematic case is when the array is packed, in
4769 -- which case we really know nothing about the alignment of
4770 -- individual components.
4771
4772 if Is_Bit_Packed_Array (Ptyp) then
4773 return True;
4774 end if;
4775
4776 -- Case of array is part of a selected component reference
4777
4778 elsif Nkind (Pref) = N_Selected_Component then
4779 Ptyp := Etype (Prefix (Pref));
4780
4781 -- We are definitely in trouble if the record in question
4782 -- has an alignment, and either we know this alignment is
4783 -- inconsistent with the alignment of the slice, or we don't
4784 -- know what the alignment of the slice should be.
4785
4786 if Known_Alignment (Ptyp)
4787 and then (Unknown_Alignment (Styp)
4788 or else Alignment (Styp) > Alignment (Ptyp))
4789 then
4790 return True;
4791 end if;
4792
4793 -- We are in potential trouble if the record type is packed.
4794 -- We could special case when we know that the array is the
4795 -- first component, but that's not such a simple case ???
4796
4797 if Is_Packed (Ptyp) then
4798 return True;
4799 end if;
4800
4801 -- We are in trouble if there is a component clause, and
4802 -- either we do not know the alignment of the slice, or
4803 -- the alignment of the slice is inconsistent with the
4804 -- bit position specified by the component clause.
4805
4806 declare
4807 Field : constant Entity_Id := Entity (Selector_Name (Pref));
4808 begin
4809 if Present (Component_Clause (Field))
4810 and then
4811 (Unknown_Alignment (Styp)
4812 or else
4813 (Component_Bit_Offset (Field) mod
4814 (System_Storage_Unit * Alignment (Styp))) /= 0)
4815 then
4816 return True;
4817 end if;
4818 end;
4819
4820 -- For cases other than selected or indexed components we know we
4821 -- are OK, since no issues arise over alignment.
4822
4823 else
4824 return False;
4825 end if;
4826
4827 -- We processed an indexed component or selected component
4828 -- reference that looked safe, so keep checking prefixes.
4829
4830 Pref := Prefix (Pref);
4831 end loop;
4832 end;
4833 end Is_Possibly_Unaligned_Slice;
4834
4835 -------------------------------
4836 -- Is_Related_To_Func_Return --
4837 -------------------------------
4838
4839 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
4840 Expr : constant Node_Id := Related_Expression (Id);
4841 begin
4842 return
4843 Present (Expr)
4844 and then Nkind (Expr) = N_Explicit_Dereference
4845 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
4846 end Is_Related_To_Func_Return;
4847
4848 --------------------------------
4849 -- Is_Ref_To_Bit_Packed_Array --
4850 --------------------------------
4851
4852 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
4853 Result : Boolean;
4854 Expr : Node_Id;
4855
4856 begin
4857 if Is_Entity_Name (N)
4858 and then Is_Object (Entity (N))
4859 and then Present (Renamed_Object (Entity (N)))
4860 then
4861 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
4862 end if;
4863
4864 if Nkind (N) = N_Indexed_Component
4865 or else
4866 Nkind (N) = N_Selected_Component
4867 then
4868 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
4869 Result := True;
4870 else
4871 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
4872 end if;
4873
4874 if Result and then Nkind (N) = N_Indexed_Component then
4875 Expr := First (Expressions (N));
4876 while Present (Expr) loop
4877 Force_Evaluation (Expr);
4878 Next (Expr);
4879 end loop;
4880 end if;
4881
4882 return Result;
4883
4884 else
4885 return False;
4886 end if;
4887 end Is_Ref_To_Bit_Packed_Array;
4888
4889 --------------------------------
4890 -- Is_Ref_To_Bit_Packed_Slice --
4891 --------------------------------
4892
4893 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
4894 begin
4895 if Nkind (N) = N_Type_Conversion then
4896 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
4897
4898 elsif Is_Entity_Name (N)
4899 and then Is_Object (Entity (N))
4900 and then Present (Renamed_Object (Entity (N)))
4901 then
4902 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
4903
4904 elsif Nkind (N) = N_Slice
4905 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
4906 then
4907 return True;
4908
4909 elsif Nkind (N) = N_Indexed_Component
4910 or else
4911 Nkind (N) = N_Selected_Component
4912 then
4913 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
4914
4915 else
4916 return False;
4917 end if;
4918 end Is_Ref_To_Bit_Packed_Slice;
4919
4920 -----------------------
4921 -- Is_Renamed_Object --
4922 -----------------------
4923
4924 function Is_Renamed_Object (N : Node_Id) return Boolean is
4925 Pnod : constant Node_Id := Parent (N);
4926 Kind : constant Node_Kind := Nkind (Pnod);
4927 begin
4928 if Kind = N_Object_Renaming_Declaration then
4929 return True;
4930 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
4931 return Is_Renamed_Object (Pnod);
4932 else
4933 return False;
4934 end if;
4935 end Is_Renamed_Object;
4936
4937 --------------------------------------
4938 -- Is_Secondary_Stack_BIP_Func_Call --
4939 --------------------------------------
4940
4941 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
4942 Call : Node_Id := Expr;
4943
4944 begin
4945 -- Build-in-place calls usually appear in 'reference format. Note that
4946 -- the accessibility check machinery may add an extra 'reference due to
4947 -- side effect removal.
4948
4949 while Nkind (Call) = N_Reference loop
4950 Call := Prefix (Call);
4951 end loop;
4952
4953 if Nkind_In (Call, N_Qualified_Expression,
4954 N_Unchecked_Type_Conversion)
4955 then
4956 Call := Expression (Call);
4957 end if;
4958
4959 if Is_Build_In_Place_Function_Call (Call) then
4960 declare
4961 Access_Nam : Name_Id := No_Name;
4962 Actual : Node_Id;
4963 Param : Node_Id;
4964 Formal : Node_Id;
4965
4966 begin
4967 -- Examine all parameter associations of the function call
4968
4969 Param := First (Parameter_Associations (Call));
4970 while Present (Param) loop
4971 if Nkind (Param) = N_Parameter_Association
4972 and then Nkind (Selector_Name (Param)) = N_Identifier
4973 then
4974 Formal := Selector_Name (Param);
4975 Actual := Explicit_Actual_Parameter (Param);
4976
4977 -- Construct the name of formal BIPalloc. It is much easier
4978 -- to extract the name of the function using an arbitrary
4979 -- formal's scope rather than the Name field of Call.
4980
4981 if Access_Nam = No_Name
4982 and then Present (Entity (Formal))
4983 then
4984 Access_Nam :=
4985 New_External_Name
4986 (Chars (Scope (Entity (Formal))),
4987 BIP_Formal_Suffix (BIP_Alloc_Form));
4988 end if;
4989
4990 -- A match for BIPalloc => 2 has been found
4991
4992 if Chars (Formal) = Access_Nam
4993 and then Nkind (Actual) = N_Integer_Literal
4994 and then Intval (Actual) = Uint_2
4995 then
4996 return True;
4997 end if;
4998 end if;
4999
5000 Next (Param);
5001 end loop;
5002 end;
5003 end if;
5004
5005 return False;
5006 end Is_Secondary_Stack_BIP_Func_Call;
5007
5008 -------------------------------------
5009 -- Is_Tag_To_Class_Wide_Conversion --
5010 -------------------------------------
5011
5012 function Is_Tag_To_Class_Wide_Conversion
5013 (Obj_Id : Entity_Id) return Boolean
5014 is
5015 Expr : constant Node_Id := Expression (Parent (Obj_Id));
5016
5017 begin
5018 return
5019 Is_Class_Wide_Type (Etype (Obj_Id))
5020 and then Present (Expr)
5021 and then Nkind (Expr) = N_Unchecked_Type_Conversion
5022 and then Etype (Expression (Expr)) = RTE (RE_Tag);
5023 end Is_Tag_To_Class_Wide_Conversion;
5024
5025 ----------------------------
5026 -- Is_Untagged_Derivation --
5027 ----------------------------
5028
5029 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
5030 begin
5031 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
5032 or else
5033 (Is_Private_Type (T) and then Present (Full_View (T))
5034 and then not Is_Tagged_Type (Full_View (T))
5035 and then Is_Derived_Type (Full_View (T))
5036 and then Etype (Full_View (T)) /= T);
5037 end Is_Untagged_Derivation;
5038
5039 ---------------------------
5040 -- Is_Volatile_Reference --
5041 ---------------------------
5042
5043 function Is_Volatile_Reference (N : Node_Id) return Boolean is
5044 begin
5045 if Nkind (N) in N_Has_Etype
5046 and then Present (Etype (N))
5047 and then Treat_As_Volatile (Etype (N))
5048 then
5049 return True;
5050
5051 elsif Is_Entity_Name (N) then
5052 return Treat_As_Volatile (Entity (N));
5053
5054 elsif Nkind (N) = N_Slice then
5055 return Is_Volatile_Reference (Prefix (N));
5056
5057 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5058 if (Is_Entity_Name (Prefix (N))
5059 and then Has_Volatile_Components (Entity (Prefix (N))))
5060 or else (Present (Etype (Prefix (N)))
5061 and then Has_Volatile_Components (Etype (Prefix (N))))
5062 then
5063 return True;
5064 else
5065 return Is_Volatile_Reference (Prefix (N));
5066 end if;
5067
5068 else
5069 return False;
5070 end if;
5071 end Is_Volatile_Reference;
5072
5073 --------------------------
5074 -- Is_VM_By_Copy_Actual --
5075 --------------------------
5076
5077 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
5078 begin
5079 return VM_Target /= No_VM
5080 and then (Nkind (N) = N_Slice
5081 or else
5082 (Nkind (N) = N_Identifier
5083 and then Present (Renamed_Object (Entity (N)))
5084 and then Nkind (Renamed_Object (Entity (N)))
5085 = N_Slice));
5086 end Is_VM_By_Copy_Actual;
5087
5088 --------------------
5089 -- Kill_Dead_Code --
5090 --------------------
5091
5092 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
5093 W : Boolean := Warn;
5094 -- Set False if warnings suppressed
5095
5096 begin
5097 if Present (N) then
5098 Remove_Warning_Messages (N);
5099
5100 -- Generate warning if appropriate
5101
5102 if W then
5103
5104 -- We suppress the warning if this code is under control of an
5105 -- if statement, whose condition is a simple identifier, and
5106 -- either we are in an instance, or warnings off is set for this
5107 -- identifier. The reason for killing it in the instance case is
5108 -- that it is common and reasonable for code to be deleted in
5109 -- instances for various reasons.
5110
5111 if Nkind (Parent (N)) = N_If_Statement then
5112 declare
5113 C : constant Node_Id := Condition (Parent (N));
5114 begin
5115 if Nkind (C) = N_Identifier
5116 and then
5117 (In_Instance
5118 or else (Present (Entity (C))
5119 and then Has_Warnings_Off (Entity (C))))
5120 then
5121 W := False;
5122 end if;
5123 end;
5124 end if;
5125
5126 -- Generate warning if not suppressed
5127
5128 if W then
5129 Error_Msg_F
5130 ("??this code can never be executed and has been deleted!",
5131 N);
5132 end if;
5133 end if;
5134
5135 -- Recurse into block statements and bodies to process declarations
5136 -- and statements.
5137
5138 if Nkind (N) = N_Block_Statement
5139 or else Nkind (N) = N_Subprogram_Body
5140 or else Nkind (N) = N_Package_Body
5141 then
5142 Kill_Dead_Code (Declarations (N), False);
5143 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
5144
5145 if Nkind (N) = N_Subprogram_Body then
5146 Set_Is_Eliminated (Defining_Entity (N));
5147 end if;
5148
5149 elsif Nkind (N) = N_Package_Declaration then
5150 Kill_Dead_Code (Visible_Declarations (Specification (N)));
5151 Kill_Dead_Code (Private_Declarations (Specification (N)));
5152
5153 -- ??? After this point, Delete_Tree has been called on all
5154 -- declarations in Specification (N), so references to entities
5155 -- therein look suspicious.
5156
5157 declare
5158 E : Entity_Id := First_Entity (Defining_Entity (N));
5159 begin
5160 while Present (E) loop
5161 if Ekind (E) = E_Operator then
5162 Set_Is_Eliminated (E);
5163 end if;
5164
5165 Next_Entity (E);
5166 end loop;
5167 end;
5168
5169 -- Recurse into composite statement to kill individual statements in
5170 -- particular instantiations.
5171
5172 elsif Nkind (N) = N_If_Statement then
5173 Kill_Dead_Code (Then_Statements (N));
5174 Kill_Dead_Code (Elsif_Parts (N));
5175 Kill_Dead_Code (Else_Statements (N));
5176
5177 elsif Nkind (N) = N_Loop_Statement then
5178 Kill_Dead_Code (Statements (N));
5179
5180 elsif Nkind (N) = N_Case_Statement then
5181 declare
5182 Alt : Node_Id;
5183 begin
5184 Alt := First (Alternatives (N));
5185 while Present (Alt) loop
5186 Kill_Dead_Code (Statements (Alt));
5187 Next (Alt);
5188 end loop;
5189 end;
5190
5191 elsif Nkind (N) = N_Case_Statement_Alternative then
5192 Kill_Dead_Code (Statements (N));
5193
5194 -- Deal with dead instances caused by deleting instantiations
5195
5196 elsif Nkind (N) in N_Generic_Instantiation then
5197 Remove_Dead_Instance (N);
5198 end if;
5199 end if;
5200 end Kill_Dead_Code;
5201
5202 -- Case where argument is a list of nodes to be killed
5203
5204 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
5205 N : Node_Id;
5206 W : Boolean;
5207 begin
5208 W := Warn;
5209 if Is_Non_Empty_List (L) then
5210 N := First (L);
5211 while Present (N) loop
5212 Kill_Dead_Code (N, W);
5213 W := False;
5214 Next (N);
5215 end loop;
5216 end if;
5217 end Kill_Dead_Code;
5218
5219 ------------------------
5220 -- Known_Non_Negative --
5221 ------------------------
5222
5223 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
5224 begin
5225 if Is_OK_Static_Expression (Opnd)
5226 and then Expr_Value (Opnd) >= 0
5227 then
5228 return True;
5229
5230 else
5231 declare
5232 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
5233
5234 begin
5235 return
5236 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
5237 end;
5238 end if;
5239 end Known_Non_Negative;
5240
5241 --------------------
5242 -- Known_Non_Null --
5243 --------------------
5244
5245 function Known_Non_Null (N : Node_Id) return Boolean is
5246 begin
5247 -- Checks for case where N is an entity reference
5248
5249 if Is_Entity_Name (N) and then Present (Entity (N)) then
5250 declare
5251 E : constant Entity_Id := Entity (N);
5252 Op : Node_Kind;
5253 Val : Node_Id;
5254
5255 begin
5256 -- First check if we are in decisive conditional
5257
5258 Get_Current_Value_Condition (N, Op, Val);
5259
5260 if Known_Null (Val) then
5261 if Op = N_Op_Eq then
5262 return False;
5263 elsif Op = N_Op_Ne then
5264 return True;
5265 end if;
5266 end if;
5267
5268 -- If OK to do replacement, test Is_Known_Non_Null flag
5269
5270 if OK_To_Do_Constant_Replacement (E) then
5271 return Is_Known_Non_Null (E);
5272
5273 -- Otherwise if not safe to do replacement, then say so
5274
5275 else
5276 return False;
5277 end if;
5278 end;
5279
5280 -- True if access attribute
5281
5282 elsif Nkind (N) = N_Attribute_Reference
5283 and then (Attribute_Name (N) = Name_Access
5284 or else
5285 Attribute_Name (N) = Name_Unchecked_Access
5286 or else
5287 Attribute_Name (N) = Name_Unrestricted_Access)
5288 then
5289 return True;
5290
5291 -- True if allocator
5292
5293 elsif Nkind (N) = N_Allocator then
5294 return True;
5295
5296 -- For a conversion, true if expression is known non-null
5297
5298 elsif Nkind (N) = N_Type_Conversion then
5299 return Known_Non_Null (Expression (N));
5300
5301 -- Above are all cases where the value could be determined to be
5302 -- non-null. In all other cases, we don't know, so return False.
5303
5304 else
5305 return False;
5306 end if;
5307 end Known_Non_Null;
5308
5309 ----------------
5310 -- Known_Null --
5311 ----------------
5312
5313 function Known_Null (N : Node_Id) return Boolean is
5314 begin
5315 -- Checks for case where N is an entity reference
5316
5317 if Is_Entity_Name (N) and then Present (Entity (N)) then
5318 declare
5319 E : constant Entity_Id := Entity (N);
5320 Op : Node_Kind;
5321 Val : Node_Id;
5322
5323 begin
5324 -- Constant null value is for sure null
5325
5326 if Ekind (E) = E_Constant
5327 and then Known_Null (Constant_Value (E))
5328 then
5329 return True;
5330 end if;
5331
5332 -- First check if we are in decisive conditional
5333
5334 Get_Current_Value_Condition (N, Op, Val);
5335
5336 if Known_Null (Val) then
5337 if Op = N_Op_Eq then
5338 return True;
5339 elsif Op = N_Op_Ne then
5340 return False;
5341 end if;
5342 end if;
5343
5344 -- If OK to do replacement, test Is_Known_Null flag
5345
5346 if OK_To_Do_Constant_Replacement (E) then
5347 return Is_Known_Null (E);
5348
5349 -- Otherwise if not safe to do replacement, then say so
5350
5351 else
5352 return False;
5353 end if;
5354 end;
5355
5356 -- True if explicit reference to null
5357
5358 elsif Nkind (N) = N_Null then
5359 return True;
5360
5361 -- For a conversion, true if expression is known null
5362
5363 elsif Nkind (N) = N_Type_Conversion then
5364 return Known_Null (Expression (N));
5365
5366 -- Above are all cases where the value could be determined to be null.
5367 -- In all other cases, we don't know, so return False.
5368
5369 else
5370 return False;
5371 end if;
5372 end Known_Null;
5373
5374 -----------------------------
5375 -- Make_CW_Equivalent_Type --
5376 -----------------------------
5377
5378 -- Create a record type used as an equivalent of any member of the class
5379 -- which takes its size from exp.
5380
5381 -- Generate the following code:
5382
5383 -- type Equiv_T is record
5384 -- _parent : T (List of discriminant constraints taken from Exp);
5385 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
5386 -- end Equiv_T;
5387 --
5388 -- ??? Note that this type does not guarantee same alignment as all
5389 -- derived types
5390
5391 function Make_CW_Equivalent_Type
5392 (T : Entity_Id;
5393 E : Node_Id) return Entity_Id
5394 is
5395 Loc : constant Source_Ptr := Sloc (E);
5396 Root_Typ : constant Entity_Id := Root_Type (T);
5397 List_Def : constant List_Id := Empty_List;
5398 Comp_List : constant List_Id := New_List;
5399 Equiv_Type : Entity_Id;
5400 Range_Type : Entity_Id;
5401 Str_Type : Entity_Id;
5402 Constr_Root : Entity_Id;
5403 Sizexpr : Node_Id;
5404
5405 begin
5406 -- If the root type is already constrained, there are no discriminants
5407 -- in the expression.
5408
5409 if not Has_Discriminants (Root_Typ)
5410 or else Is_Constrained (Root_Typ)
5411 then
5412 Constr_Root := Root_Typ;
5413 else
5414 Constr_Root := Make_Temporary (Loc, 'R');
5415
5416 -- subtype cstr__n is T (List of discr constraints taken from Exp)
5417
5418 Append_To (List_Def,
5419 Make_Subtype_Declaration (Loc,
5420 Defining_Identifier => Constr_Root,
5421 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
5422 end if;
5423
5424 -- Generate the range subtype declaration
5425
5426 Range_Type := Make_Temporary (Loc, 'G');
5427
5428 if not Is_Interface (Root_Typ) then
5429
5430 -- subtype rg__xx is
5431 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
5432
5433 Sizexpr :=
5434 Make_Op_Subtract (Loc,
5435 Left_Opnd =>
5436 Make_Attribute_Reference (Loc,
5437 Prefix =>
5438 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
5439 Attribute_Name => Name_Size),
5440 Right_Opnd =>
5441 Make_Attribute_Reference (Loc,
5442 Prefix => New_Reference_To (Constr_Root, Loc),
5443 Attribute_Name => Name_Object_Size));
5444 else
5445 -- subtype rg__xx is
5446 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
5447
5448 Sizexpr :=
5449 Make_Attribute_Reference (Loc,
5450 Prefix =>
5451 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
5452 Attribute_Name => Name_Size);
5453 end if;
5454
5455 Set_Paren_Count (Sizexpr, 1);
5456
5457 Append_To (List_Def,
5458 Make_Subtype_Declaration (Loc,
5459 Defining_Identifier => Range_Type,
5460 Subtype_Indication =>
5461 Make_Subtype_Indication (Loc,
5462 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
5463 Constraint => Make_Range_Constraint (Loc,
5464 Range_Expression =>
5465 Make_Range (Loc,
5466 Low_Bound => Make_Integer_Literal (Loc, 1),
5467 High_Bound =>
5468 Make_Op_Divide (Loc,
5469 Left_Opnd => Sizexpr,
5470 Right_Opnd => Make_Integer_Literal (Loc,
5471 Intval => System_Storage_Unit)))))));
5472
5473 -- subtype str__nn is Storage_Array (rg__x);
5474
5475 Str_Type := Make_Temporary (Loc, 'S');
5476 Append_To (List_Def,
5477 Make_Subtype_Declaration (Loc,
5478 Defining_Identifier => Str_Type,
5479 Subtype_Indication =>
5480 Make_Subtype_Indication (Loc,
5481 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
5482 Constraint =>
5483 Make_Index_Or_Discriminant_Constraint (Loc,
5484 Constraints =>
5485 New_List (New_Reference_To (Range_Type, Loc))))));
5486
5487 -- type Equiv_T is record
5488 -- [ _parent : Tnn; ]
5489 -- E : Str_Type;
5490 -- end Equiv_T;
5491
5492 Equiv_Type := Make_Temporary (Loc, 'T');
5493 Set_Ekind (Equiv_Type, E_Record_Type);
5494 Set_Parent_Subtype (Equiv_Type, Constr_Root);
5495
5496 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
5497 -- treatment for this type. In particular, even though _parent's type
5498 -- is a controlled type or contains controlled components, we do not
5499 -- want to set Has_Controlled_Component on it to avoid making it gain
5500 -- an unwanted _controller component.
5501
5502 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
5503
5504 if not Is_Interface (Root_Typ) then
5505 Append_To (Comp_List,
5506 Make_Component_Declaration (Loc,
5507 Defining_Identifier =>
5508 Make_Defining_Identifier (Loc, Name_uParent),
5509 Component_Definition =>
5510 Make_Component_Definition (Loc,
5511 Aliased_Present => False,
5512 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
5513 end if;
5514
5515 Append_To (Comp_List,
5516 Make_Component_Declaration (Loc,
5517 Defining_Identifier => Make_Temporary (Loc, 'C'),
5518 Component_Definition =>
5519 Make_Component_Definition (Loc,
5520 Aliased_Present => False,
5521 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
5522
5523 Append_To (List_Def,
5524 Make_Full_Type_Declaration (Loc,
5525 Defining_Identifier => Equiv_Type,
5526 Type_Definition =>
5527 Make_Record_Definition (Loc,
5528 Component_List =>
5529 Make_Component_List (Loc,
5530 Component_Items => Comp_List,
5531 Variant_Part => Empty))));
5532
5533 -- Suppress all checks during the analysis of the expanded code to avoid
5534 -- the generation of spurious warnings under ZFP run-time.
5535
5536 Insert_Actions (E, List_Def, Suppress => All_Checks);
5537 return Equiv_Type;
5538 end Make_CW_Equivalent_Type;
5539
5540 -------------------------
5541 -- Make_Invariant_Call --
5542 -------------------------
5543
5544 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
5545 Loc : constant Source_Ptr := Sloc (Expr);
5546 Typ : constant Entity_Id := Etype (Expr);
5547
5548 begin
5549 pragma Assert
5550 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
5551
5552 if Check_Enabled (Name_Invariant)
5553 or else
5554 Check_Enabled (Name_Assertion)
5555 then
5556 return
5557 Make_Procedure_Call_Statement (Loc,
5558 Name =>
5559 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
5560 Parameter_Associations => New_List (Relocate_Node (Expr)));
5561
5562 else
5563 return
5564 Make_Null_Statement (Loc);
5565 end if;
5566 end Make_Invariant_Call;
5567
5568 ------------------------
5569 -- Make_Literal_Range --
5570 ------------------------
5571
5572 function Make_Literal_Range
5573 (Loc : Source_Ptr;
5574 Literal_Typ : Entity_Id) return Node_Id
5575 is
5576 Lo : constant Node_Id :=
5577 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
5578 Index : constant Entity_Id := Etype (Lo);
5579
5580 Hi : Node_Id;
5581 Length_Expr : constant Node_Id :=
5582 Make_Op_Subtract (Loc,
5583 Left_Opnd =>
5584 Make_Integer_Literal (Loc,
5585 Intval => String_Literal_Length (Literal_Typ)),
5586 Right_Opnd =>
5587 Make_Integer_Literal (Loc, 1));
5588
5589 begin
5590 Set_Analyzed (Lo, False);
5591
5592 if Is_Integer_Type (Index) then
5593 Hi :=
5594 Make_Op_Add (Loc,
5595 Left_Opnd => New_Copy_Tree (Lo),
5596 Right_Opnd => Length_Expr);
5597 else
5598 Hi :=
5599 Make_Attribute_Reference (Loc,
5600 Attribute_Name => Name_Val,
5601 Prefix => New_Occurrence_Of (Index, Loc),
5602 Expressions => New_List (
5603 Make_Op_Add (Loc,
5604 Left_Opnd =>
5605 Make_Attribute_Reference (Loc,
5606 Attribute_Name => Name_Pos,
5607 Prefix => New_Occurrence_Of (Index, Loc),
5608 Expressions => New_List (New_Copy_Tree (Lo))),
5609 Right_Opnd => Length_Expr)));
5610 end if;
5611
5612 return
5613 Make_Range (Loc,
5614 Low_Bound => Lo,
5615 High_Bound => Hi);
5616 end Make_Literal_Range;
5617
5618 --------------------------
5619 -- Make_Non_Empty_Check --
5620 --------------------------
5621
5622 function Make_Non_Empty_Check
5623 (Loc : Source_Ptr;
5624 N : Node_Id) return Node_Id
5625 is
5626 begin
5627 return
5628 Make_Op_Ne (Loc,
5629 Left_Opnd =>
5630 Make_Attribute_Reference (Loc,
5631 Attribute_Name => Name_Length,
5632 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
5633 Right_Opnd =>
5634 Make_Integer_Literal (Loc, 0));
5635 end Make_Non_Empty_Check;
5636
5637 -------------------------
5638 -- Make_Predicate_Call --
5639 -------------------------
5640
5641 function Make_Predicate_Call
5642 (Typ : Entity_Id;
5643 Expr : Node_Id) return Node_Id
5644 is
5645 Loc : constant Source_Ptr := Sloc (Expr);
5646
5647 begin
5648 pragma Assert (Present (Predicate_Function (Typ)));
5649
5650 return
5651 Make_Function_Call (Loc,
5652 Name =>
5653 New_Occurrence_Of (Predicate_Function (Typ), Loc),
5654 Parameter_Associations => New_List (Relocate_Node (Expr)));
5655 end Make_Predicate_Call;
5656
5657 --------------------------
5658 -- Make_Predicate_Check --
5659 --------------------------
5660
5661 function Make_Predicate_Check
5662 (Typ : Entity_Id;
5663 Expr : Node_Id) return Node_Id
5664 is
5665 Loc : constant Source_Ptr := Sloc (Expr);
5666
5667 begin
5668 return
5669 Make_Pragma (Loc,
5670 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
5671 Pragma_Argument_Associations => New_List (
5672 Make_Pragma_Argument_Association (Loc,
5673 Expression => Make_Identifier (Loc, Name_Predicate)),
5674 Make_Pragma_Argument_Association (Loc,
5675 Expression => Make_Predicate_Call (Typ, Expr))));
5676 end Make_Predicate_Check;
5677
5678 ----------------------------
5679 -- Make_Subtype_From_Expr --
5680 ----------------------------
5681
5682 -- 1. If Expr is an unconstrained array expression, creates
5683 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
5684
5685 -- 2. If Expr is a unconstrained discriminated type expression, creates
5686 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
5687
5688 -- 3. If Expr is class-wide, creates an implicit class wide subtype
5689
5690 function Make_Subtype_From_Expr
5691 (E : Node_Id;
5692 Unc_Typ : Entity_Id) return Node_Id
5693 is
5694 Loc : constant Source_Ptr := Sloc (E);
5695 List_Constr : constant List_Id := New_List;
5696 D : Entity_Id;
5697
5698 Full_Subtyp : Entity_Id;
5699 Priv_Subtyp : Entity_Id;
5700 Utyp : Entity_Id;
5701 Full_Exp : Node_Id;
5702
5703 begin
5704 if Is_Private_Type (Unc_Typ)
5705 and then Has_Unknown_Discriminants (Unc_Typ)
5706 then
5707 -- Prepare the subtype completion, Go to base type to
5708 -- find underlying type, because the type may be a generic
5709 -- actual or an explicit subtype.
5710
5711 Utyp := Underlying_Type (Base_Type (Unc_Typ));
5712 Full_Subtyp := Make_Temporary (Loc, 'C');
5713 Full_Exp :=
5714 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
5715 Set_Parent (Full_Exp, Parent (E));
5716
5717 Priv_Subtyp := Make_Temporary (Loc, 'P');
5718
5719 Insert_Action (E,
5720 Make_Subtype_Declaration (Loc,
5721 Defining_Identifier => Full_Subtyp,
5722 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
5723
5724 -- Define the dummy private subtype
5725
5726 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
5727 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
5728 Set_Scope (Priv_Subtyp, Full_Subtyp);
5729 Set_Is_Constrained (Priv_Subtyp);
5730 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
5731 Set_Is_Itype (Priv_Subtyp);
5732 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
5733
5734 if Is_Tagged_Type (Priv_Subtyp) then
5735 Set_Class_Wide_Type
5736 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
5737 Set_Direct_Primitive_Operations (Priv_Subtyp,
5738 Direct_Primitive_Operations (Unc_Typ));
5739 end if;
5740
5741 Set_Full_View (Priv_Subtyp, Full_Subtyp);
5742
5743 return New_Reference_To (Priv_Subtyp, Loc);
5744
5745 elsif Is_Array_Type (Unc_Typ) then
5746 for J in 1 .. Number_Dimensions (Unc_Typ) loop
5747 Append_To (List_Constr,
5748 Make_Range (Loc,
5749 Low_Bound =>
5750 Make_Attribute_Reference (Loc,
5751 Prefix => Duplicate_Subexpr_No_Checks (E),
5752 Attribute_Name => Name_First,
5753 Expressions => New_List (
5754 Make_Integer_Literal (Loc, J))),
5755
5756 High_Bound =>
5757 Make_Attribute_Reference (Loc,
5758 Prefix => Duplicate_Subexpr_No_Checks (E),
5759 Attribute_Name => Name_Last,
5760 Expressions => New_List (
5761 Make_Integer_Literal (Loc, J)))));
5762 end loop;
5763
5764 elsif Is_Class_Wide_Type (Unc_Typ) then
5765 declare
5766 CW_Subtype : Entity_Id;
5767 EQ_Typ : Entity_Id := Empty;
5768
5769 begin
5770 -- A class-wide equivalent type is not needed when VM_Target
5771 -- because the VM back-ends handle the class-wide object
5772 -- initialization itself (and doesn't need or want the
5773 -- additional intermediate type to handle the assignment).
5774
5775 if Expander_Active and then Tagged_Type_Expansion then
5776
5777 -- If this is the class_wide type of a completion that is a
5778 -- record subtype, set the type of the class_wide type to be
5779 -- the full base type, for use in the expanded code for the
5780 -- equivalent type. Should this be done earlier when the
5781 -- completion is analyzed ???
5782
5783 if Is_Private_Type (Etype (Unc_Typ))
5784 and then
5785 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
5786 then
5787 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
5788 end if;
5789
5790 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
5791 end if;
5792
5793 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
5794 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
5795 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
5796
5797 return New_Occurrence_Of (CW_Subtype, Loc);
5798 end;
5799
5800 -- Indefinite record type with discriminants
5801
5802 else
5803 D := First_Discriminant (Unc_Typ);
5804 while Present (D) loop
5805 Append_To (List_Constr,
5806 Make_Selected_Component (Loc,
5807 Prefix => Duplicate_Subexpr_No_Checks (E),
5808 Selector_Name => New_Reference_To (D, Loc)));
5809
5810 Next_Discriminant (D);
5811 end loop;
5812 end if;
5813
5814 return
5815 Make_Subtype_Indication (Loc,
5816 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
5817 Constraint =>
5818 Make_Index_Or_Discriminant_Constraint (Loc,
5819 Constraints => List_Constr));
5820 end Make_Subtype_From_Expr;
5821
5822 -----------------------------
5823 -- May_Generate_Large_Temp --
5824 -----------------------------
5825
5826 -- At the current time, the only types that we return False for (i.e. where
5827 -- we decide we know they cannot generate large temps) are ones where we
5828 -- know the size is 256 bits or less at compile time, and we are still not
5829 -- doing a thorough job on arrays and records ???
5830
5831 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
5832 begin
5833 if not Size_Known_At_Compile_Time (Typ) then
5834 return False;
5835
5836 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
5837 return False;
5838
5839 elsif Is_Array_Type (Typ)
5840 and then Present (Packed_Array_Type (Typ))
5841 then
5842 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
5843
5844 -- We could do more here to find other small types ???
5845
5846 else
5847 return True;
5848 end if;
5849 end May_Generate_Large_Temp;
5850
5851 ------------------------
5852 -- Needs_Finalization --
5853 ------------------------
5854
5855 function Needs_Finalization (T : Entity_Id) return Boolean is
5856 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
5857 -- If type is not frozen yet, check explicitly among its components,
5858 -- because the Has_Controlled_Component flag is not necessarily set.
5859
5860 -----------------------------------
5861 -- Has_Some_Controlled_Component --
5862 -----------------------------------
5863
5864 function Has_Some_Controlled_Component
5865 (Rec : Entity_Id) return Boolean
5866 is
5867 Comp : Entity_Id;
5868
5869 begin
5870 if Has_Controlled_Component (Rec) then
5871 return True;
5872
5873 elsif not Is_Frozen (Rec) then
5874 if Is_Record_Type (Rec) then
5875 Comp := First_Entity (Rec);
5876
5877 while Present (Comp) loop
5878 if not Is_Type (Comp)
5879 and then Needs_Finalization (Etype (Comp))
5880 then
5881 return True;
5882 end if;
5883
5884 Next_Entity (Comp);
5885 end loop;
5886
5887 return False;
5888
5889 elsif Is_Array_Type (Rec) then
5890 return Needs_Finalization (Component_Type (Rec));
5891
5892 else
5893 return Has_Controlled_Component (Rec);
5894 end if;
5895 else
5896 return False;
5897 end if;
5898 end Has_Some_Controlled_Component;
5899
5900 -- Start of processing for Needs_Finalization
5901
5902 begin
5903 -- Certain run-time configurations and targets do not provide support
5904 -- for controlled types.
5905
5906 if Restriction_Active (No_Finalization) then
5907 return False;
5908
5909 -- C, C++, CIL and Java types are not considered controlled. It is
5910 -- assumed that the non-Ada side will handle their clean up.
5911
5912 elsif Convention (T) = Convention_C
5913 or else Convention (T) = Convention_CIL
5914 or else Convention (T) = Convention_CPP
5915 or else Convention (T) = Convention_Java
5916 then
5917 return False;
5918
5919 else
5920 -- Class-wide types are treated as controlled because derivations
5921 -- from the root type can introduce controlled components.
5922
5923 return
5924 Is_Class_Wide_Type (T)
5925 or else Is_Controlled (T)
5926 or else Has_Controlled_Component (T)
5927 or else Has_Some_Controlled_Component (T)
5928 or else
5929 (Is_Concurrent_Type (T)
5930 and then Present (Corresponding_Record_Type (T))
5931 and then Needs_Finalization (Corresponding_Record_Type (T)));
5932 end if;
5933 end Needs_Finalization;
5934
5935 ----------------------------
5936 -- Needs_Constant_Address --
5937 ----------------------------
5938
5939 function Needs_Constant_Address
5940 (Decl : Node_Id;
5941 Typ : Entity_Id) return Boolean
5942 is
5943 begin
5944
5945 -- If we have no initialization of any kind, then we don't need to place
5946 -- any restrictions on the address clause, because the object will be
5947 -- elaborated after the address clause is evaluated. This happens if the
5948 -- declaration has no initial expression, or the type has no implicit
5949 -- initialization, or the object is imported.
5950
5951 -- The same holds for all initialized scalar types and all access types.
5952 -- Packed bit arrays of size up to 64 are represented using a modular
5953 -- type with an initialization (to zero) and can be processed like other
5954 -- initialized scalar types.
5955
5956 -- If the type is controlled, code to attach the object to a
5957 -- finalization chain is generated at the point of declaration, and
5958 -- therefore the elaboration of the object cannot be delayed: the
5959 -- address expression must be a constant.
5960
5961 if No (Expression (Decl))
5962 and then not Needs_Finalization (Typ)
5963 and then
5964 (not Has_Non_Null_Base_Init_Proc (Typ)
5965 or else Is_Imported (Defining_Identifier (Decl)))
5966 then
5967 return False;
5968
5969 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
5970 or else Is_Access_Type (Typ)
5971 or else
5972 (Is_Bit_Packed_Array (Typ)
5973 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
5974 then
5975 return False;
5976
5977 else
5978
5979 -- Otherwise, we require the address clause to be constant because
5980 -- the call to the initialization procedure (or the attach code) has
5981 -- to happen at the point of the declaration.
5982
5983 -- Actually the IP call has been moved to the freeze actions anyway,
5984 -- so maybe we can relax this restriction???
5985
5986 return True;
5987 end if;
5988 end Needs_Constant_Address;
5989
5990 ----------------------------
5991 -- New_Class_Wide_Subtype --
5992 ----------------------------
5993
5994 function New_Class_Wide_Subtype
5995 (CW_Typ : Entity_Id;
5996 N : Node_Id) return Entity_Id
5997 is
5998 Res : constant Entity_Id := Create_Itype (E_Void, N);
5999 Res_Name : constant Name_Id := Chars (Res);
6000 Res_Scope : constant Entity_Id := Scope (Res);
6001
6002 begin
6003 Copy_Node (CW_Typ, Res);
6004 Set_Comes_From_Source (Res, False);
6005 Set_Sloc (Res, Sloc (N));
6006 Set_Is_Itype (Res);
6007 Set_Associated_Node_For_Itype (Res, N);
6008 Set_Is_Public (Res, False); -- By default, may be changed below.
6009 Set_Public_Status (Res);
6010 Set_Chars (Res, Res_Name);
6011 Set_Scope (Res, Res_Scope);
6012 Set_Ekind (Res, E_Class_Wide_Subtype);
6013 Set_Next_Entity (Res, Empty);
6014 Set_Etype (Res, Base_Type (CW_Typ));
6015 Set_Is_Frozen (Res, False);
6016 Set_Freeze_Node (Res, Empty);
6017 return (Res);
6018 end New_Class_Wide_Subtype;
6019
6020 --------------------------------
6021 -- Non_Limited_Designated_Type --
6022 ---------------------------------
6023
6024 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
6025 Desig : constant Entity_Id := Designated_Type (T);
6026 begin
6027 if Ekind (Desig) = E_Incomplete_Type
6028 and then Present (Non_Limited_View (Desig))
6029 then
6030 return Non_Limited_View (Desig);
6031 else
6032 return Desig;
6033 end if;
6034 end Non_Limited_Designated_Type;
6035
6036 -----------------------------------
6037 -- OK_To_Do_Constant_Replacement --
6038 -----------------------------------
6039
6040 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
6041 ES : constant Entity_Id := Scope (E);
6042 CS : Entity_Id;
6043
6044 begin
6045 -- Do not replace statically allocated objects, because they may be
6046 -- modified outside the current scope.
6047
6048 if Is_Statically_Allocated (E) then
6049 return False;
6050
6051 -- Do not replace aliased or volatile objects, since we don't know what
6052 -- else might change the value.
6053
6054 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
6055 return False;
6056
6057 -- Debug flag -gnatdM disconnects this optimization
6058
6059 elsif Debug_Flag_MM then
6060 return False;
6061
6062 -- Otherwise check scopes
6063
6064 else
6065 CS := Current_Scope;
6066
6067 loop
6068 -- If we are in right scope, replacement is safe
6069
6070 if CS = ES then
6071 return True;
6072
6073 -- Packages do not affect the determination of safety
6074
6075 elsif Ekind (CS) = E_Package then
6076 exit when CS = Standard_Standard;
6077 CS := Scope (CS);
6078
6079 -- Blocks do not affect the determination of safety
6080
6081 elsif Ekind (CS) = E_Block then
6082 CS := Scope (CS);
6083
6084 -- Loops do not affect the determination of safety. Note that we
6085 -- kill all current values on entry to a loop, so we are just
6086 -- talking about processing within a loop here.
6087
6088 elsif Ekind (CS) = E_Loop then
6089 CS := Scope (CS);
6090
6091 -- Otherwise, the reference is dubious, and we cannot be sure that
6092 -- it is safe to do the replacement.
6093
6094 else
6095 exit;
6096 end if;
6097 end loop;
6098
6099 return False;
6100 end if;
6101 end OK_To_Do_Constant_Replacement;
6102
6103 ------------------------------------
6104 -- Possible_Bit_Aligned_Component --
6105 ------------------------------------
6106
6107 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
6108 begin
6109 case Nkind (N) is
6110
6111 -- Case of indexed component
6112
6113 when N_Indexed_Component =>
6114 declare
6115 P : constant Node_Id := Prefix (N);
6116 Ptyp : constant Entity_Id := Etype (P);
6117
6118 begin
6119 -- If we know the component size and it is less than 64, then
6120 -- we are definitely OK. The back end always does assignment of
6121 -- misaligned small objects correctly.
6122
6123 if Known_Static_Component_Size (Ptyp)
6124 and then Component_Size (Ptyp) <= 64
6125 then
6126 return False;
6127
6128 -- Otherwise, we need to test the prefix, to see if we are
6129 -- indexing from a possibly unaligned component.
6130
6131 else
6132 return Possible_Bit_Aligned_Component (P);
6133 end if;
6134 end;
6135
6136 -- Case of selected component
6137
6138 when N_Selected_Component =>
6139 declare
6140 P : constant Node_Id := Prefix (N);
6141 Comp : constant Entity_Id := Entity (Selector_Name (N));
6142
6143 begin
6144 -- If there is no component clause, then we are in the clear
6145 -- since the back end will never misalign a large component
6146 -- unless it is forced to do so. In the clear means we need
6147 -- only the recursive test on the prefix.
6148
6149 if Component_May_Be_Bit_Aligned (Comp) then
6150 return True;
6151 else
6152 return Possible_Bit_Aligned_Component (P);
6153 end if;
6154 end;
6155
6156 -- For a slice, test the prefix, if that is possibly misaligned,
6157 -- then for sure the slice is!
6158
6159 when N_Slice =>
6160 return Possible_Bit_Aligned_Component (Prefix (N));
6161
6162 -- For an unchecked conversion, check whether the expression may
6163 -- be bit-aligned.
6164
6165 when N_Unchecked_Type_Conversion =>
6166 return Possible_Bit_Aligned_Component (Expression (N));
6167
6168 -- If we have none of the above, it means that we have fallen off the
6169 -- top testing prefixes recursively, and we now have a stand alone
6170 -- object, where we don't have a problem.
6171
6172 when others =>
6173 return False;
6174
6175 end case;
6176 end Possible_Bit_Aligned_Component;
6177
6178 -----------------------------------------------
6179 -- Process_Statements_For_Controlled_Objects --
6180 -----------------------------------------------
6181
6182 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
6183 Loc : constant Source_Ptr := Sloc (N);
6184
6185 function Are_Wrapped (L : List_Id) return Boolean;
6186 -- Determine whether list L contains only one statement which is a block
6187
6188 function Wrap_Statements_In_Block (L : List_Id) return Node_Id;
6189 -- Given a list of statements L, wrap it in a block statement and return
6190 -- the generated node.
6191
6192 -----------------
6193 -- Are_Wrapped --
6194 -----------------
6195
6196 function Are_Wrapped (L : List_Id) return Boolean is
6197 Stmt : constant Node_Id := First (L);
6198 begin
6199 return
6200 Present (Stmt)
6201 and then No (Next (Stmt))
6202 and then Nkind (Stmt) = N_Block_Statement;
6203 end Are_Wrapped;
6204
6205 ------------------------------
6206 -- Wrap_Statements_In_Block --
6207 ------------------------------
6208
6209 function Wrap_Statements_In_Block (L : List_Id) return Node_Id is
6210 begin
6211 return
6212 Make_Block_Statement (Loc,
6213 Declarations => No_List,
6214 Handled_Statement_Sequence =>
6215 Make_Handled_Sequence_Of_Statements (Loc,
6216 Statements => L));
6217 end Wrap_Statements_In_Block;
6218
6219 -- Local variables
6220
6221 Block : Node_Id;
6222
6223 -- Start of processing for Process_Statements_For_Controlled_Objects
6224
6225 begin
6226 -- Whenever a non-handled statement list is wrapped in a block, the
6227 -- block must be explicitly analyzed to redecorate all entities in the
6228 -- list and ensure that a finalizer is properly built.
6229
6230 case Nkind (N) is
6231 when N_Elsif_Part |
6232 N_If_Statement |
6233 N_Conditional_Entry_Call |
6234 N_Selective_Accept =>
6235
6236 -- Check the "then statements" for elsif parts and if statements
6237
6238 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
6239 and then not Is_Empty_List (Then_Statements (N))
6240 and then not Are_Wrapped (Then_Statements (N))
6241 and then Requires_Cleanup_Actions
6242 (Then_Statements (N), False, False)
6243 then
6244 Block := Wrap_Statements_In_Block (Then_Statements (N));
6245 Set_Then_Statements (N, New_List (Block));
6246
6247 Analyze (Block);
6248 end if;
6249
6250 -- Check the "else statements" for conditional entry calls, if
6251 -- statements and selective accepts.
6252
6253 if Nkind_In (N, N_Conditional_Entry_Call,
6254 N_If_Statement,
6255 N_Selective_Accept)
6256 and then not Is_Empty_List (Else_Statements (N))
6257 and then not Are_Wrapped (Else_Statements (N))
6258 and then Requires_Cleanup_Actions
6259 (Else_Statements (N), False, False)
6260 then
6261 Block := Wrap_Statements_In_Block (Else_Statements (N));
6262 Set_Else_Statements (N, New_List (Block));
6263
6264 Analyze (Block);
6265 end if;
6266
6267 when N_Abortable_Part |
6268 N_Accept_Alternative |
6269 N_Case_Statement_Alternative |
6270 N_Delay_Alternative |
6271 N_Entry_Call_Alternative |
6272 N_Exception_Handler |
6273 N_Loop_Statement |
6274 N_Triggering_Alternative =>
6275
6276 if not Is_Empty_List (Statements (N))
6277 and then not Are_Wrapped (Statements (N))
6278 and then Requires_Cleanup_Actions (Statements (N), False, False)
6279 then
6280 Block := Wrap_Statements_In_Block (Statements (N));
6281 Set_Statements (N, New_List (Block));
6282
6283 Analyze (Block);
6284 end if;
6285
6286 when others =>
6287 null;
6288 end case;
6289 end Process_Statements_For_Controlled_Objects;
6290
6291 -------------------------
6292 -- Remove_Side_Effects --
6293 -------------------------
6294
6295 procedure Remove_Side_Effects
6296 (Exp : Node_Id;
6297 Name_Req : Boolean := False;
6298 Variable_Ref : Boolean := False)
6299 is
6300 Loc : constant Source_Ptr := Sloc (Exp);
6301 Exp_Type : constant Entity_Id := Etype (Exp);
6302 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
6303 Def_Id : Entity_Id;
6304 E : Node_Id;
6305 New_Exp : Node_Id;
6306 Ptr_Typ_Decl : Node_Id;
6307 Ref_Type : Entity_Id;
6308 Res : Node_Id;
6309
6310 function Side_Effect_Free (N : Node_Id) return Boolean;
6311 -- Determines if the tree N represents an expression that is known not
6312 -- to have side effects, and for which no processing is required.
6313
6314 function Side_Effect_Free (L : List_Id) return Boolean;
6315 -- Determines if all elements of the list L are side effect free
6316
6317 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
6318 -- The argument N is a construct where the Prefix is dereferenced if it
6319 -- is an access type and the result is a variable. The call returns True
6320 -- if the construct is side effect free (not considering side effects in
6321 -- other than the prefix which are to be tested by the caller).
6322
6323 function Within_In_Parameter (N : Node_Id) return Boolean;
6324 -- Determines if N is a subcomponent of a composite in-parameter. If so,
6325 -- N is not side-effect free when the actual is global and modifiable
6326 -- indirectly from within a subprogram, because it may be passed by
6327 -- reference. The front-end must be conservative here and assume that
6328 -- this may happen with any array or record type. On the other hand, we
6329 -- cannot create temporaries for all expressions for which this
6330 -- condition is true, for various reasons that might require clearing up
6331 -- ??? For example, discriminant references that appear out of place, or
6332 -- spurious type errors with class-wide expressions. As a result, we
6333 -- limit the transformation to loop bounds, which is so far the only
6334 -- case that requires it.
6335
6336 -----------------------------
6337 -- Safe_Prefixed_Reference --
6338 -----------------------------
6339
6340 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
6341 begin
6342 -- If prefix is not side effect free, definitely not safe
6343
6344 if not Side_Effect_Free (Prefix (N)) then
6345 return False;
6346
6347 -- If the prefix is of an access type that is not access-to-constant,
6348 -- then this construct is a variable reference, which means it is to
6349 -- be considered to have side effects if Variable_Ref is set True.
6350
6351 elsif Is_Access_Type (Etype (Prefix (N)))
6352 and then not Is_Access_Constant (Etype (Prefix (N)))
6353 and then Variable_Ref
6354 then
6355 -- Exception is a prefix that is the result of a previous removal
6356 -- of side-effects.
6357
6358 return Is_Entity_Name (Prefix (N))
6359 and then not Comes_From_Source (Prefix (N))
6360 and then Ekind (Entity (Prefix (N))) = E_Constant
6361 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
6362
6363 -- If the prefix is an explicit dereference then this construct is a
6364 -- variable reference, which means it is to be considered to have
6365 -- side effects if Variable_Ref is True.
6366
6367 -- We do NOT exclude dereferences of access-to-constant types because
6368 -- we handle them as constant view of variables.
6369
6370 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
6371 and then Variable_Ref
6372 then
6373 return False;
6374
6375 -- Note: The following test is the simplest way of solving a complex
6376 -- problem uncovered by the following test (Side effect on loop bound
6377 -- that is a subcomponent of a global variable:
6378
6379 -- with Text_Io; use Text_Io;
6380 -- procedure Tloop is
6381 -- type X is
6382 -- record
6383 -- V : Natural := 4;
6384 -- S : String (1..5) := (others => 'a');
6385 -- end record;
6386 -- X1 : X;
6387
6388 -- procedure Modi;
6389
6390 -- generic
6391 -- with procedure Action;
6392 -- procedure Loop_G (Arg : X; Msg : String)
6393
6394 -- procedure Loop_G (Arg : X; Msg : String) is
6395 -- begin
6396 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
6397 -- & Natural'Image (Arg.V));
6398 -- for Index in 1 .. Arg.V loop
6399 -- Text_Io.Put_Line
6400 -- (Natural'Image (Index) & " " & Arg.S (Index));
6401 -- if Index > 2 then
6402 -- Modi;
6403 -- end if;
6404 -- end loop;
6405 -- Put_Line ("end loop_g " & Msg);
6406 -- end;
6407
6408 -- procedure Loop1 is new Loop_G (Modi);
6409 -- procedure Modi is
6410 -- begin
6411 -- X1.V := 1;
6412 -- Loop1 (X1, "from modi");
6413 -- end;
6414 --
6415 -- begin
6416 -- Loop1 (X1, "initial");
6417 -- end;
6418
6419 -- The output of the above program should be:
6420
6421 -- begin loop_g initial will loop till: 4
6422 -- 1 a
6423 -- 2 a
6424 -- 3 a
6425 -- begin loop_g from modi will loop till: 1
6426 -- 1 a
6427 -- end loop_g from modi
6428 -- 4 a
6429 -- begin loop_g from modi will loop till: 1
6430 -- 1 a
6431 -- end loop_g from modi
6432 -- end loop_g initial
6433
6434 -- If a loop bound is a subcomponent of a global variable, a
6435 -- modification of that variable within the loop may incorrectly
6436 -- affect the execution of the loop.
6437
6438 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
6439 and then Within_In_Parameter (Prefix (N))
6440 and then Variable_Ref
6441 then
6442 return False;
6443
6444 -- All other cases are side effect free
6445
6446 else
6447 return True;
6448 end if;
6449 end Safe_Prefixed_Reference;
6450
6451 ----------------------
6452 -- Side_Effect_Free --
6453 ----------------------
6454
6455 function Side_Effect_Free (N : Node_Id) return Boolean is
6456 begin
6457 -- Note on checks that could raise Constraint_Error. Strictly, if we
6458 -- take advantage of 11.6, these checks do not count as side effects.
6459 -- However, we would prefer to consider that they are side effects,
6460 -- since the backend CSE does not work very well on expressions which
6461 -- can raise Constraint_Error. On the other hand if we don't consider
6462 -- them to be side effect free, then we get some awkward expansions
6463 -- in -gnato mode, resulting in code insertions at a point where we
6464 -- do not have a clear model for performing the insertions.
6465
6466 -- Special handling for entity names
6467
6468 if Is_Entity_Name (N) then
6469
6470 -- Variables are considered to be a side effect if Variable_Ref
6471 -- is set or if we have a volatile reference and Name_Req is off.
6472 -- If Name_Req is True then we can't help returning a name which
6473 -- effectively allows multiple references in any case.
6474
6475 if Is_Variable (N, Use_Original_Node => False) then
6476 return not Variable_Ref
6477 and then (not Is_Volatile_Reference (N) or else Name_Req);
6478
6479 -- Any other entity (e.g. a subtype name) is definitely side
6480 -- effect free.
6481
6482 else
6483 return True;
6484 end if;
6485
6486 -- A value known at compile time is always side effect free
6487
6488 elsif Compile_Time_Known_Value (N) then
6489 return True;
6490
6491 -- A variable renaming is not side-effect free, because the renaming
6492 -- will function like a macro in the front-end in some cases, and an
6493 -- assignment can modify the component designated by N, so we need to
6494 -- create a temporary for it.
6495
6496 -- The guard testing for Entity being present is needed at least in
6497 -- the case of rewritten predicate expressions, and may well also be
6498 -- appropriate elsewhere. Obviously we can't go testing the entity
6499 -- field if it does not exist, so it's reasonable to say that this is
6500 -- not the renaming case if it does not exist.
6501
6502 elsif Is_Entity_Name (Original_Node (N))
6503 and then Present (Entity (Original_Node (N)))
6504 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
6505 and then Ekind (Entity (Original_Node (N))) /= E_Constant
6506 then
6507 return False;
6508
6509 -- Remove_Side_Effects generates an object renaming declaration to
6510 -- capture the expression of a class-wide expression. In VM targets
6511 -- the frontend performs no expansion for dispatching calls to
6512 -- class- wide types since they are handled by the VM. Hence, we must
6513 -- locate here if this node corresponds to a previous invocation of
6514 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
6515
6516 elsif VM_Target /= No_VM
6517 and then not Comes_From_Source (N)
6518 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
6519 and then Is_Class_Wide_Type (Etype (N))
6520 then
6521 return True;
6522 end if;
6523
6524 -- For other than entity names and compile time known values,
6525 -- check the node kind for special processing.
6526
6527 case Nkind (N) is
6528
6529 -- An attribute reference is side effect free if its expressions
6530 -- are side effect free and its prefix is side effect free or
6531 -- is an entity reference.
6532
6533 -- Is this right? what about x'first where x is a variable???
6534
6535 when N_Attribute_Reference =>
6536 return Side_Effect_Free (Expressions (N))
6537 and then Attribute_Name (N) /= Name_Input
6538 and then (Is_Entity_Name (Prefix (N))
6539 or else Side_Effect_Free (Prefix (N)));
6540
6541 -- A binary operator is side effect free if and both operands are
6542 -- side effect free. For this purpose binary operators include
6543 -- membership tests and short circuit forms.
6544
6545 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
6546 return Side_Effect_Free (Left_Opnd (N))
6547 and then
6548 Side_Effect_Free (Right_Opnd (N));
6549
6550 -- An explicit dereference is side effect free only if it is
6551 -- a side effect free prefixed reference.
6552
6553 when N_Explicit_Dereference =>
6554 return Safe_Prefixed_Reference (N);
6555
6556 -- A call to _rep_to_pos is side effect free, since we generate
6557 -- this pure function call ourselves. Moreover it is critically
6558 -- important to make this exception, since otherwise we can have
6559 -- discriminants in array components which don't look side effect
6560 -- free in the case of an array whose index type is an enumeration
6561 -- type with an enumeration rep clause.
6562
6563 -- All other function calls are not side effect free
6564
6565 when N_Function_Call =>
6566 return Nkind (Name (N)) = N_Identifier
6567 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
6568 and then
6569 Side_Effect_Free (First (Parameter_Associations (N)));
6570
6571 -- An indexed component is side effect free if it is a side
6572 -- effect free prefixed reference and all the indexing
6573 -- expressions are side effect free.
6574
6575 when N_Indexed_Component =>
6576 return Side_Effect_Free (Expressions (N))
6577 and then Safe_Prefixed_Reference (N);
6578
6579 -- A type qualification is side effect free if the expression
6580 -- is side effect free.
6581
6582 when N_Qualified_Expression =>
6583 return Side_Effect_Free (Expression (N));
6584
6585 -- A selected component is side effect free only if it is a side
6586 -- effect free prefixed reference. If it designates a component
6587 -- with a rep. clause it must be treated has having a potential
6588 -- side effect, because it may be modified through a renaming, and
6589 -- a subsequent use of the renaming as a macro will yield the
6590 -- wrong value. This complex interaction between renaming and
6591 -- removing side effects is a reminder that the latter has become
6592 -- a headache to maintain, and that it should be removed in favor
6593 -- of the gcc mechanism to capture values ???
6594
6595 when N_Selected_Component =>
6596 if Nkind (Parent (N)) = N_Explicit_Dereference
6597 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
6598 then
6599 return False;
6600 else
6601 return Safe_Prefixed_Reference (N);
6602 end if;
6603
6604 -- A range is side effect free if the bounds are side effect free
6605
6606 when N_Range =>
6607 return Side_Effect_Free (Low_Bound (N))
6608 and then Side_Effect_Free (High_Bound (N));
6609
6610 -- A slice is side effect free if it is a side effect free
6611 -- prefixed reference and the bounds are side effect free.
6612
6613 when N_Slice =>
6614 return Side_Effect_Free (Discrete_Range (N))
6615 and then Safe_Prefixed_Reference (N);
6616
6617 -- A type conversion is side effect free if the expression to be
6618 -- converted is side effect free.
6619
6620 when N_Type_Conversion =>
6621 return Side_Effect_Free (Expression (N));
6622
6623 -- A unary operator is side effect free if the operand
6624 -- is side effect free.
6625
6626 when N_Unary_Op =>
6627 return Side_Effect_Free (Right_Opnd (N));
6628
6629 -- An unchecked type conversion is side effect free only if it
6630 -- is safe and its argument is side effect free.
6631
6632 when N_Unchecked_Type_Conversion =>
6633 return Safe_Unchecked_Type_Conversion (N)
6634 and then Side_Effect_Free (Expression (N));
6635
6636 -- An unchecked expression is side effect free if its expression
6637 -- is side effect free.
6638
6639 when N_Unchecked_Expression =>
6640 return Side_Effect_Free (Expression (N));
6641
6642 -- A literal is side effect free
6643
6644 when N_Character_Literal |
6645 N_Integer_Literal |
6646 N_Real_Literal |
6647 N_String_Literal =>
6648 return True;
6649
6650 -- We consider that anything else has side effects. This is a bit
6651 -- crude, but we are pretty close for most common cases, and we
6652 -- are certainly correct (i.e. we never return True when the
6653 -- answer should be False).
6654
6655 when others =>
6656 return False;
6657 end case;
6658 end Side_Effect_Free;
6659
6660 -- A list is side effect free if all elements of the list are side
6661 -- effect free.
6662
6663 function Side_Effect_Free (L : List_Id) return Boolean is
6664 N : Node_Id;
6665
6666 begin
6667 if L = No_List or else L = Error_List then
6668 return True;
6669
6670 else
6671 N := First (L);
6672 while Present (N) loop
6673 if not Side_Effect_Free (N) then
6674 return False;
6675 else
6676 Next (N);
6677 end if;
6678 end loop;
6679
6680 return True;
6681 end if;
6682 end Side_Effect_Free;
6683
6684 -------------------------
6685 -- Within_In_Parameter --
6686 -------------------------
6687
6688 function Within_In_Parameter (N : Node_Id) return Boolean is
6689 begin
6690 if not Comes_From_Source (N) then
6691 return False;
6692
6693 elsif Is_Entity_Name (N) then
6694 return Ekind (Entity (N)) = E_In_Parameter;
6695
6696 elsif Nkind (N) = N_Indexed_Component
6697 or else Nkind (N) = N_Selected_Component
6698 then
6699 return Within_In_Parameter (Prefix (N));
6700 else
6701
6702 return False;
6703 end if;
6704 end Within_In_Parameter;
6705
6706 -- Start of processing for Remove_Side_Effects
6707
6708 begin
6709 -- Handle cases in which there is nothing to do
6710
6711 if not Expander_Active then
6712 return;
6713 end if;
6714
6715 -- Cannot generate temporaries if the invocation to remove side effects
6716 -- was issued too early and the type of the expression is not resolved
6717 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
6718 -- Remove_Side_Effects).
6719
6720 if No (Exp_Type)
6721 or else Ekind (Exp_Type) = E_Access_Attribute_Type
6722 then
6723 return;
6724
6725 -- No action needed for side-effect free expressions
6726
6727 elsif Side_Effect_Free (Exp) then
6728 return;
6729 end if;
6730
6731 -- All this must not have any checks
6732
6733 Scope_Suppress.Suppress := (others => True);
6734
6735 -- If it is a scalar type and we need to capture the value, just make
6736 -- a copy. Likewise for a function call, an attribute reference, an
6737 -- allocator, or an operator. And if we have a volatile reference and
6738 -- Name_Req is not set (see comments above for Side_Effect_Free).
6739
6740 if Is_Elementary_Type (Exp_Type)
6741 and then (Variable_Ref
6742 or else Nkind (Exp) = N_Function_Call
6743 or else Nkind (Exp) = N_Attribute_Reference
6744 or else Nkind (Exp) = N_Allocator
6745 or else Nkind (Exp) in N_Op
6746 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
6747 then
6748 Def_Id := Make_Temporary (Loc, 'R', Exp);
6749 Set_Etype (Def_Id, Exp_Type);
6750 Res := New_Reference_To (Def_Id, Loc);
6751
6752 -- If the expression is a packed reference, it must be reanalyzed and
6753 -- expanded, depending on context. This is the case for actuals where
6754 -- a constraint check may capture the actual before expansion of the
6755 -- call is complete.
6756
6757 if Nkind (Exp) = N_Indexed_Component
6758 and then Is_Packed (Etype (Prefix (Exp)))
6759 then
6760 Set_Analyzed (Exp, False);
6761 Set_Analyzed (Prefix (Exp), False);
6762 end if;
6763
6764 E :=
6765 Make_Object_Declaration (Loc,
6766 Defining_Identifier => Def_Id,
6767 Object_Definition => New_Reference_To (Exp_Type, Loc),
6768 Constant_Present => True,
6769 Expression => Relocate_Node (Exp));
6770
6771 Set_Assignment_OK (E);
6772 Insert_Action (Exp, E);
6773
6774 -- If the expression has the form v.all then we can just capture the
6775 -- pointer, and then do an explicit dereference on the result.
6776
6777 elsif Nkind (Exp) = N_Explicit_Dereference then
6778 Def_Id := Make_Temporary (Loc, 'R', Exp);
6779 Res :=
6780 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
6781
6782 Insert_Action (Exp,
6783 Make_Object_Declaration (Loc,
6784 Defining_Identifier => Def_Id,
6785 Object_Definition =>
6786 New_Reference_To (Etype (Prefix (Exp)), Loc),
6787 Constant_Present => True,
6788 Expression => Relocate_Node (Prefix (Exp))));
6789
6790 -- Similar processing for an unchecked conversion of an expression of
6791 -- the form v.all, where we want the same kind of treatment.
6792
6793 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
6794 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
6795 then
6796 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
6797 Scope_Suppress := Svg_Suppress;
6798 return;
6799
6800 -- If this is a type conversion, leave the type conversion and remove
6801 -- the side effects in the expression. This is important in several
6802 -- circumstances: for change of representations, and also when this is a
6803 -- view conversion to a smaller object, where gigi can end up creating
6804 -- its own temporary of the wrong size.
6805
6806 elsif Nkind (Exp) = N_Type_Conversion then
6807 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
6808 Scope_Suppress := Svg_Suppress;
6809 return;
6810
6811 -- If this is an unchecked conversion that Gigi can't handle, make
6812 -- a copy or a use a renaming to capture the value.
6813
6814 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
6815 and then not Safe_Unchecked_Type_Conversion (Exp)
6816 then
6817 if CW_Or_Has_Controlled_Part (Exp_Type) then
6818
6819 -- Use a renaming to capture the expression, rather than create
6820 -- a controlled temporary.
6821
6822 Def_Id := Make_Temporary (Loc, 'R', Exp);
6823 Res := New_Reference_To (Def_Id, Loc);
6824
6825 Insert_Action (Exp,
6826 Make_Object_Renaming_Declaration (Loc,
6827 Defining_Identifier => Def_Id,
6828 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
6829 Name => Relocate_Node (Exp)));
6830
6831 else
6832 Def_Id := Make_Temporary (Loc, 'R', Exp);
6833 Set_Etype (Def_Id, Exp_Type);
6834 Res := New_Reference_To (Def_Id, Loc);
6835
6836 E :=
6837 Make_Object_Declaration (Loc,
6838 Defining_Identifier => Def_Id,
6839 Object_Definition => New_Reference_To (Exp_Type, Loc),
6840 Constant_Present => not Is_Variable (Exp),
6841 Expression => Relocate_Node (Exp));
6842
6843 Set_Assignment_OK (E);
6844 Insert_Action (Exp, E);
6845 end if;
6846
6847 -- For expressions that denote objects, we can use a renaming scheme.
6848 -- This is needed for correctness in the case of a volatile object of
6849 -- a non-volatile type because the Make_Reference call of the "default"
6850 -- approach would generate an illegal access value (an access value
6851 -- cannot designate such an object - see Analyze_Reference). We skip
6852 -- using this scheme if we have an object of a volatile type and we do
6853 -- not have Name_Req set true (see comments above for Side_Effect_Free).
6854
6855 -- In Ada 2012 a qualified expression is an object, but for purposes of
6856 -- removing side effects it still need to be transformed into a separate
6857 -- declaration, particularly if the expression is an aggregate.
6858
6859 elsif Is_Object_Reference (Exp)
6860 and then Nkind (Exp) /= N_Function_Call
6861 and then Nkind (Exp) /= N_Qualified_Expression
6862 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
6863 then
6864 Def_Id := Make_Temporary (Loc, 'R', Exp);
6865
6866 if Nkind (Exp) = N_Selected_Component
6867 and then Nkind (Prefix (Exp)) = N_Function_Call
6868 and then Is_Array_Type (Exp_Type)
6869 then
6870 -- Avoid generating a variable-sized temporary, by generating
6871 -- the renaming declaration just for the function call. The
6872 -- transformation could be refined to apply only when the array
6873 -- component is constrained by a discriminant???
6874
6875 Res :=
6876 Make_Selected_Component (Loc,
6877 Prefix => New_Occurrence_Of (Def_Id, Loc),
6878 Selector_Name => Selector_Name (Exp));
6879
6880 Insert_Action (Exp,
6881 Make_Object_Renaming_Declaration (Loc,
6882 Defining_Identifier => Def_Id,
6883 Subtype_Mark =>
6884 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
6885 Name => Relocate_Node (Prefix (Exp))));
6886
6887 else
6888 Res := New_Reference_To (Def_Id, Loc);
6889
6890 Insert_Action (Exp,
6891 Make_Object_Renaming_Declaration (Loc,
6892 Defining_Identifier => Def_Id,
6893 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
6894 Name => Relocate_Node (Exp)));
6895 end if;
6896
6897 -- If this is a packed reference, or a selected component with
6898 -- a non-standard representation, a reference to the temporary
6899 -- will be replaced by a copy of the original expression (see
6900 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
6901 -- elaborated by gigi, and is of course not to be replaced in-line
6902 -- by the expression it renames, which would defeat the purpose of
6903 -- removing the side-effect.
6904
6905 if (Nkind (Exp) = N_Selected_Component
6906 or else Nkind (Exp) = N_Indexed_Component)
6907 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
6908 then
6909 null;
6910 else
6911 Set_Is_Renaming_Of_Object (Def_Id, False);
6912 end if;
6913
6914 -- Otherwise we generate a reference to the value
6915
6916 else
6917 -- An expression which is in Alfa mode is considered side effect free
6918 -- if the resulting value is captured by a variable or a constant.
6919
6920 if Alfa_Mode
6921 and then Nkind (Parent (Exp)) = N_Object_Declaration
6922 then
6923 return;
6924 end if;
6925
6926 -- Special processing for function calls that return a limited type.
6927 -- We need to build a declaration that will enable build-in-place
6928 -- expansion of the call. This is not done if the context is already
6929 -- an object declaration, to prevent infinite recursion.
6930
6931 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
6932 -- to accommodate functions returning limited objects by reference.
6933
6934 if Ada_Version >= Ada_2005
6935 and then Nkind (Exp) = N_Function_Call
6936 and then Is_Immutably_Limited_Type (Etype (Exp))
6937 and then Nkind (Parent (Exp)) /= N_Object_Declaration
6938 then
6939 declare
6940 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
6941 Decl : Node_Id;
6942
6943 begin
6944 Decl :=
6945 Make_Object_Declaration (Loc,
6946 Defining_Identifier => Obj,
6947 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
6948 Expression => Relocate_Node (Exp));
6949
6950 Insert_Action (Exp, Decl);
6951 Set_Etype (Obj, Exp_Type);
6952 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
6953 return;
6954 end;
6955 end if;
6956
6957 Def_Id := Make_Temporary (Loc, 'R', Exp);
6958 Set_Etype (Def_Id, Exp_Type);
6959
6960 -- The regular expansion of functions with side effects involves the
6961 -- generation of an access type to capture the return value found on
6962 -- the secondary stack. Since Alfa (and why) cannot process access
6963 -- types, use a different approach which ignores the secondary stack
6964 -- and "copies" the returned object.
6965
6966 if Alfa_Mode then
6967 Res := New_Reference_To (Def_Id, Loc);
6968 Ref_Type := Exp_Type;
6969
6970 -- Regular expansion utilizing an access type and 'reference
6971
6972 else
6973 Res :=
6974 Make_Explicit_Dereference (Loc,
6975 Prefix => New_Reference_To (Def_Id, Loc));
6976
6977 -- Generate:
6978 -- type Ann is access all <Exp_Type>;
6979
6980 Ref_Type := Make_Temporary (Loc, 'A');
6981
6982 Ptr_Typ_Decl :=
6983 Make_Full_Type_Declaration (Loc,
6984 Defining_Identifier => Ref_Type,
6985 Type_Definition =>
6986 Make_Access_To_Object_Definition (Loc,
6987 All_Present => True,
6988 Subtype_Indication =>
6989 New_Reference_To (Exp_Type, Loc)));
6990
6991 Insert_Action (Exp, Ptr_Typ_Decl);
6992 end if;
6993
6994 E := Exp;
6995 if Nkind (E) = N_Explicit_Dereference then
6996 New_Exp := Relocate_Node (Prefix (E));
6997 else
6998 E := Relocate_Node (E);
6999
7000 -- Do not generate a 'reference in Alfa mode since the access type
7001 -- is not created in the first place.
7002
7003 if Alfa_Mode then
7004 New_Exp := E;
7005
7006 -- Otherwise generate reference, marking the value as non-null
7007 -- since we know it cannot be null and we don't want a check.
7008
7009 else
7010 New_Exp := Make_Reference (Loc, E);
7011 Set_Is_Known_Non_Null (Def_Id);
7012 end if;
7013 end if;
7014
7015 if Is_Delayed_Aggregate (E) then
7016
7017 -- The expansion of nested aggregates is delayed until the
7018 -- enclosing aggregate is expanded. As aggregates are often
7019 -- qualified, the predicate applies to qualified expressions as
7020 -- well, indicating that the enclosing aggregate has not been
7021 -- expanded yet. At this point the aggregate is part of a
7022 -- stand-alone declaration, and must be fully expanded.
7023
7024 if Nkind (E) = N_Qualified_Expression then
7025 Set_Expansion_Delayed (Expression (E), False);
7026 Set_Analyzed (Expression (E), False);
7027 else
7028 Set_Expansion_Delayed (E, False);
7029 end if;
7030
7031 Set_Analyzed (E, False);
7032 end if;
7033
7034 Insert_Action (Exp,
7035 Make_Object_Declaration (Loc,
7036 Defining_Identifier => Def_Id,
7037 Object_Definition => New_Reference_To (Ref_Type, Loc),
7038 Constant_Present => True,
7039 Expression => New_Exp));
7040 end if;
7041
7042 -- Preserve the Assignment_OK flag in all copies, since at least one
7043 -- copy may be used in a context where this flag must be set (otherwise
7044 -- why would the flag be set in the first place).
7045
7046 Set_Assignment_OK (Res, Assignment_OK (Exp));
7047
7048 -- Finally rewrite the original expression and we are done
7049
7050 Rewrite (Exp, Res);
7051 Analyze_And_Resolve (Exp, Exp_Type);
7052 Scope_Suppress := Svg_Suppress;
7053 end Remove_Side_Effects;
7054
7055 ---------------------------
7056 -- Represented_As_Scalar --
7057 ---------------------------
7058
7059 function Represented_As_Scalar (T : Entity_Id) return Boolean is
7060 UT : constant Entity_Id := Underlying_Type (T);
7061 begin
7062 return Is_Scalar_Type (UT)
7063 or else (Is_Bit_Packed_Array (UT)
7064 and then Is_Scalar_Type (Packed_Array_Type (UT)));
7065 end Represented_As_Scalar;
7066
7067 ------------------------------
7068 -- Requires_Cleanup_Actions --
7069 ------------------------------
7070
7071 function Requires_Cleanup_Actions
7072 (N : Node_Id;
7073 Lib_Level : Boolean) return Boolean
7074 is
7075 At_Lib_Level : constant Boolean :=
7076 Lib_Level
7077 and then Nkind_In (N, N_Package_Body,
7078 N_Package_Specification);
7079 -- N is at the library level if the top-most context is a package and
7080 -- the path taken to reach N does not inlcude non-package constructs.
7081
7082 begin
7083 case Nkind (N) is
7084 when N_Accept_Statement |
7085 N_Block_Statement |
7086 N_Entry_Body |
7087 N_Package_Body |
7088 N_Protected_Body |
7089 N_Subprogram_Body |
7090 N_Task_Body =>
7091 return
7092 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
7093 or else
7094 (Present (Handled_Statement_Sequence (N))
7095 and then
7096 Requires_Cleanup_Actions
7097 (Statements (Handled_Statement_Sequence (N)),
7098 At_Lib_Level, True));
7099
7100 when N_Package_Specification =>
7101 return
7102 Requires_Cleanup_Actions
7103 (Visible_Declarations (N), At_Lib_Level, True)
7104 or else
7105 Requires_Cleanup_Actions
7106 (Private_Declarations (N), At_Lib_Level, True);
7107
7108 when others =>
7109 return False;
7110 end case;
7111 end Requires_Cleanup_Actions;
7112
7113 ------------------------------
7114 -- Requires_Cleanup_Actions --
7115 ------------------------------
7116
7117 function Requires_Cleanup_Actions
7118 (L : List_Id;
7119 Lib_Level : Boolean;
7120 Nested_Constructs : Boolean) return Boolean
7121 is
7122 Decl : Node_Id;
7123 Expr : Node_Id;
7124 Obj_Id : Entity_Id;
7125 Obj_Typ : Entity_Id;
7126 Pack_Id : Entity_Id;
7127 Typ : Entity_Id;
7128
7129 begin
7130 if No (L)
7131 or else Is_Empty_List (L)
7132 then
7133 return False;
7134 end if;
7135
7136 Decl := First (L);
7137 while Present (Decl) loop
7138
7139 -- Library-level tagged types
7140
7141 if Nkind (Decl) = N_Full_Type_Declaration then
7142 Typ := Defining_Identifier (Decl);
7143
7144 if Is_Tagged_Type (Typ)
7145 and then Is_Library_Level_Entity (Typ)
7146 and then Convention (Typ) = Convention_Ada
7147 and then Present (Access_Disp_Table (Typ))
7148 and then RTE_Available (RE_Unregister_Tag)
7149 and then not No_Run_Time_Mode
7150 and then not Is_Abstract_Type (Typ)
7151 then
7152 return True;
7153 end if;
7154
7155 -- Regular object declarations
7156
7157 elsif Nkind (Decl) = N_Object_Declaration then
7158 Obj_Id := Defining_Identifier (Decl);
7159 Obj_Typ := Base_Type (Etype (Obj_Id));
7160 Expr := Expression (Decl);
7161
7162 -- Bypass any form of processing for objects which have their
7163 -- finalization disabled. This applies only to objects at the
7164 -- library level.
7165
7166 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7167 null;
7168
7169 -- Transient variables are treated separately in order to minimize
7170 -- the size of the generated code. See Exp_Ch7.Process_Transient_
7171 -- Objects.
7172
7173 elsif Is_Processed_Transient (Obj_Id) then
7174 null;
7175
7176 -- The object is of the form:
7177 -- Obj : Typ [:= Expr];
7178 --
7179 -- Do not process the incomplete view of a deferred constant. Do
7180 -- not consider tag-to-class-wide conversions.
7181
7182 elsif not Is_Imported (Obj_Id)
7183 and then Needs_Finalization (Obj_Typ)
7184 and then not (Ekind (Obj_Id) = E_Constant
7185 and then not Has_Completion (Obj_Id))
7186 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
7187 then
7188 return True;
7189
7190 -- The object is of the form:
7191 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
7192 --
7193 -- Obj : Access_Typ :=
7194 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
7195
7196 elsif Is_Access_Type (Obj_Typ)
7197 and then Needs_Finalization
7198 (Available_View (Designated_Type (Obj_Typ)))
7199 and then Present (Expr)
7200 and then
7201 (Is_Secondary_Stack_BIP_Func_Call (Expr)
7202 or else
7203 (Is_Non_BIP_Func_Call (Expr)
7204 and then not Is_Related_To_Func_Return (Obj_Id)))
7205 then
7206 return True;
7207
7208 -- Processing for "hook" objects generated for controlled
7209 -- transients declared inside an Expression_With_Actions.
7210
7211 elsif Is_Access_Type (Obj_Typ)
7212 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7213 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7214 N_Object_Declaration
7215 and then Is_Finalizable_Transient
7216 (Status_Flag_Or_Transient_Decl (Obj_Id), Decl)
7217 then
7218 return True;
7219
7220 -- Processing for intermediate results of if expressions where
7221 -- one of the alternatives uses a controlled function call.
7222
7223 elsif Is_Access_Type (Obj_Typ)
7224 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7225 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7226 N_Defining_Identifier
7227 and then Present (Expr)
7228 and then Nkind (Expr) = N_Null
7229 then
7230 return True;
7231
7232 -- Simple protected objects which use type System.Tasking.
7233 -- Protected_Objects.Protection to manage their locks should be
7234 -- treated as controlled since they require manual cleanup.
7235
7236 elsif Ekind (Obj_Id) = E_Variable
7237 and then
7238 (Is_Simple_Protected_Type (Obj_Typ)
7239 or else Has_Simple_Protected_Object (Obj_Typ))
7240 then
7241 return True;
7242 end if;
7243
7244 -- Specific cases of object renamings
7245
7246 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
7247 Obj_Id := Defining_Identifier (Decl);
7248 Obj_Typ := Base_Type (Etype (Obj_Id));
7249
7250 -- Bypass any form of processing for objects which have their
7251 -- finalization disabled. This applies only to objects at the
7252 -- library level.
7253
7254 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7255 null;
7256
7257 -- Return object of a build-in-place function. This case is
7258 -- recognized and marked by the expansion of an extended return
7259 -- statement (see Expand_N_Extended_Return_Statement).
7260
7261 elsif Needs_Finalization (Obj_Typ)
7262 and then Is_Return_Object (Obj_Id)
7263 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7264 then
7265 return True;
7266
7267 -- Detect a case where a source object has been initialized by
7268 -- a controlled function call or another object which was later
7269 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
7270
7271 -- Obj1 : CW_Type := Src_Obj;
7272 -- Obj2 : CW_Type := Function_Call (...);
7273
7274 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7275 -- Tmp : ... := Function_Call (...)'reference;
7276 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
7277
7278 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
7279 return True;
7280 end if;
7281
7282 -- Inspect the freeze node of an access-to-controlled type and look
7283 -- for a delayed finalization master. This case arises when the
7284 -- freeze actions are inserted at a later time than the expansion of
7285 -- the context. Since Build_Finalizer is never called on a single
7286 -- construct twice, the master will be ultimately left out and never
7287 -- finalized. This is also needed for freeze actions of designated
7288 -- types themselves, since in some cases the finalization master is
7289 -- associated with a designated type's freeze node rather than that
7290 -- of the access type (see handling for freeze actions in
7291 -- Build_Finalization_Master).
7292
7293 elsif Nkind (Decl) = N_Freeze_Entity
7294 and then Present (Actions (Decl))
7295 then
7296 Typ := Entity (Decl);
7297
7298 if ((Is_Access_Type (Typ)
7299 and then not Is_Access_Subprogram_Type (Typ)
7300 and then Needs_Finalization
7301 (Available_View (Designated_Type (Typ))))
7302 or else
7303 (Is_Type (Typ)
7304 and then Needs_Finalization (Typ)))
7305 and then Requires_Cleanup_Actions
7306 (Actions (Decl), Lib_Level, Nested_Constructs)
7307 then
7308 return True;
7309 end if;
7310
7311 -- Nested package declarations
7312
7313 elsif Nested_Constructs
7314 and then Nkind (Decl) = N_Package_Declaration
7315 then
7316 Pack_Id := Defining_Unit_Name (Specification (Decl));
7317
7318 if Nkind (Pack_Id) = N_Defining_Program_Unit_Name then
7319 Pack_Id := Defining_Identifier (Pack_Id);
7320 end if;
7321
7322 if Ekind (Pack_Id) /= E_Generic_Package
7323 and then Requires_Cleanup_Actions
7324 (Specification (Decl), Lib_Level)
7325 then
7326 return True;
7327 end if;
7328
7329 -- Nested package bodies
7330
7331 elsif Nested_Constructs
7332 and then Nkind (Decl) = N_Package_Body
7333 then
7334 Pack_Id := Corresponding_Spec (Decl);
7335
7336 if Ekind (Pack_Id) /= E_Generic_Package
7337 and then Requires_Cleanup_Actions (Decl, Lib_Level)
7338 then
7339 return True;
7340 end if;
7341 end if;
7342
7343 Next (Decl);
7344 end loop;
7345
7346 return False;
7347 end Requires_Cleanup_Actions;
7348
7349 ------------------------------------
7350 -- Safe_Unchecked_Type_Conversion --
7351 ------------------------------------
7352
7353 -- Note: this function knows quite a bit about the exact requirements of
7354 -- Gigi with respect to unchecked type conversions, and its code must be
7355 -- coordinated with any changes in Gigi in this area.
7356
7357 -- The above requirements should be documented in Sinfo ???
7358
7359 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
7360 Otyp : Entity_Id;
7361 Ityp : Entity_Id;
7362 Oalign : Uint;
7363 Ialign : Uint;
7364 Pexp : constant Node_Id := Parent (Exp);
7365
7366 begin
7367 -- If the expression is the RHS of an assignment or object declaration
7368 -- we are always OK because there will always be a target.
7369
7370 -- Object renaming declarations, (generated for view conversions of
7371 -- actuals in inlined calls), like object declarations, provide an
7372 -- explicit type, and are safe as well.
7373
7374 if (Nkind (Pexp) = N_Assignment_Statement
7375 and then Expression (Pexp) = Exp)
7376 or else Nkind (Pexp) = N_Object_Declaration
7377 or else Nkind (Pexp) = N_Object_Renaming_Declaration
7378 then
7379 return True;
7380
7381 -- If the expression is the prefix of an N_Selected_Component we should
7382 -- also be OK because GCC knows to look inside the conversion except if
7383 -- the type is discriminated. We assume that we are OK anyway if the
7384 -- type is not set yet or if it is controlled since we can't afford to
7385 -- introduce a temporary in this case.
7386
7387 elsif Nkind (Pexp) = N_Selected_Component
7388 and then Prefix (Pexp) = Exp
7389 then
7390 if No (Etype (Pexp)) then
7391 return True;
7392 else
7393 return
7394 not Has_Discriminants (Etype (Pexp))
7395 or else Is_Constrained (Etype (Pexp));
7396 end if;
7397 end if;
7398
7399 -- Set the output type, this comes from Etype if it is set, otherwise we
7400 -- take it from the subtype mark, which we assume was already fully
7401 -- analyzed.
7402
7403 if Present (Etype (Exp)) then
7404 Otyp := Etype (Exp);
7405 else
7406 Otyp := Entity (Subtype_Mark (Exp));
7407 end if;
7408
7409 -- The input type always comes from the expression, and we assume
7410 -- this is indeed always analyzed, so we can simply get the Etype.
7411
7412 Ityp := Etype (Expression (Exp));
7413
7414 -- Initialize alignments to unknown so far
7415
7416 Oalign := No_Uint;
7417 Ialign := No_Uint;
7418
7419 -- Replace a concurrent type by its corresponding record type and each
7420 -- type by its underlying type and do the tests on those. The original
7421 -- type may be a private type whose completion is a concurrent type, so
7422 -- find the underlying type first.
7423
7424 if Present (Underlying_Type (Otyp)) then
7425 Otyp := Underlying_Type (Otyp);
7426 end if;
7427
7428 if Present (Underlying_Type (Ityp)) then
7429 Ityp := Underlying_Type (Ityp);
7430 end if;
7431
7432 if Is_Concurrent_Type (Otyp) then
7433 Otyp := Corresponding_Record_Type (Otyp);
7434 end if;
7435
7436 if Is_Concurrent_Type (Ityp) then
7437 Ityp := Corresponding_Record_Type (Ityp);
7438 end if;
7439
7440 -- If the base types are the same, we know there is no problem since
7441 -- this conversion will be a noop.
7442
7443 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
7444 return True;
7445
7446 -- Same if this is an upwards conversion of an untagged type, and there
7447 -- are no constraints involved (could be more general???)
7448
7449 elsif Etype (Ityp) = Otyp
7450 and then not Is_Tagged_Type (Ityp)
7451 and then not Has_Discriminants (Ityp)
7452 and then No (First_Rep_Item (Base_Type (Ityp)))
7453 then
7454 return True;
7455
7456 -- If the expression has an access type (object or subprogram) we assume
7457 -- that the conversion is safe, because the size of the target is safe,
7458 -- even if it is a record (which might be treated as having unknown size
7459 -- at this point).
7460
7461 elsif Is_Access_Type (Ityp) then
7462 return True;
7463
7464 -- If the size of output type is known at compile time, there is never
7465 -- a problem. Note that unconstrained records are considered to be of
7466 -- known size, but we can't consider them that way here, because we are
7467 -- talking about the actual size of the object.
7468
7469 -- We also make sure that in addition to the size being known, we do not
7470 -- have a case which might generate an embarrassingly large temp in
7471 -- stack checking mode.
7472
7473 elsif Size_Known_At_Compile_Time (Otyp)
7474 and then
7475 (not Stack_Checking_Enabled
7476 or else not May_Generate_Large_Temp (Otyp))
7477 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
7478 then
7479 return True;
7480
7481 -- If either type is tagged, then we know the alignment is OK so
7482 -- Gigi will be able to use pointer punning.
7483
7484 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
7485 return True;
7486
7487 -- If either type is a limited record type, we cannot do a copy, so say
7488 -- safe since there's nothing else we can do.
7489
7490 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
7491 return True;
7492
7493 -- Conversions to and from packed array types are always ignored and
7494 -- hence are safe.
7495
7496 elsif Is_Packed_Array_Type (Otyp)
7497 or else Is_Packed_Array_Type (Ityp)
7498 then
7499 return True;
7500 end if;
7501
7502 -- The only other cases known to be safe is if the input type's
7503 -- alignment is known to be at least the maximum alignment for the
7504 -- target or if both alignments are known and the output type's
7505 -- alignment is no stricter than the input's. We can use the component
7506 -- type alignement for an array if a type is an unpacked array type.
7507
7508 if Present (Alignment_Clause (Otyp)) then
7509 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
7510
7511 elsif Is_Array_Type (Otyp)
7512 and then Present (Alignment_Clause (Component_Type (Otyp)))
7513 then
7514 Oalign := Expr_Value (Expression (Alignment_Clause
7515 (Component_Type (Otyp))));
7516 end if;
7517
7518 if Present (Alignment_Clause (Ityp)) then
7519 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
7520
7521 elsif Is_Array_Type (Ityp)
7522 and then Present (Alignment_Clause (Component_Type (Ityp)))
7523 then
7524 Ialign := Expr_Value (Expression (Alignment_Clause
7525 (Component_Type (Ityp))));
7526 end if;
7527
7528 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
7529 return True;
7530
7531 elsif Ialign /= No_Uint and then Oalign /= No_Uint
7532 and then Ialign <= Oalign
7533 then
7534 return True;
7535
7536 -- Otherwise, Gigi cannot handle this and we must make a temporary
7537
7538 else
7539 return False;
7540 end if;
7541 end Safe_Unchecked_Type_Conversion;
7542
7543 ---------------------------------
7544 -- Set_Current_Value_Condition --
7545 ---------------------------------
7546
7547 -- Note: the implementation of this procedure is very closely tied to the
7548 -- implementation of Get_Current_Value_Condition. Here we set required
7549 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
7550 -- them, so they must have a consistent view.
7551
7552 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
7553
7554 procedure Set_Entity_Current_Value (N : Node_Id);
7555 -- If N is an entity reference, where the entity is of an appropriate
7556 -- kind, then set the current value of this entity to Cnode, unless
7557 -- there is already a definite value set there.
7558
7559 procedure Set_Expression_Current_Value (N : Node_Id);
7560 -- If N is of an appropriate form, sets an appropriate entry in current
7561 -- value fields of relevant entities. Multiple entities can be affected
7562 -- in the case of an AND or AND THEN.
7563
7564 ------------------------------
7565 -- Set_Entity_Current_Value --
7566 ------------------------------
7567
7568 procedure Set_Entity_Current_Value (N : Node_Id) is
7569 begin
7570 if Is_Entity_Name (N) then
7571 declare
7572 Ent : constant Entity_Id := Entity (N);
7573
7574 begin
7575 -- Don't capture if not safe to do so
7576
7577 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
7578 return;
7579 end if;
7580
7581 -- Here we have a case where the Current_Value field may need
7582 -- to be set. We set it if it is not already set to a compile
7583 -- time expression value.
7584
7585 -- Note that this represents a decision that one condition
7586 -- blots out another previous one. That's certainly right if
7587 -- they occur at the same level. If the second one is nested,
7588 -- then the decision is neither right nor wrong (it would be
7589 -- equally OK to leave the outer one in place, or take the new
7590 -- inner one. Really we should record both, but our data
7591 -- structures are not that elaborate.
7592
7593 if Nkind (Current_Value (Ent)) not in N_Subexpr then
7594 Set_Current_Value (Ent, Cnode);
7595 end if;
7596 end;
7597 end if;
7598 end Set_Entity_Current_Value;
7599
7600 ----------------------------------
7601 -- Set_Expression_Current_Value --
7602 ----------------------------------
7603
7604 procedure Set_Expression_Current_Value (N : Node_Id) is
7605 Cond : Node_Id;
7606
7607 begin
7608 Cond := N;
7609
7610 -- Loop to deal with (ignore for now) any NOT operators present. The
7611 -- presence of NOT operators will be handled properly when we call
7612 -- Get_Current_Value_Condition.
7613
7614 while Nkind (Cond) = N_Op_Not loop
7615 Cond := Right_Opnd (Cond);
7616 end loop;
7617
7618 -- For an AND or AND THEN, recursively process operands
7619
7620 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
7621 Set_Expression_Current_Value (Left_Opnd (Cond));
7622 Set_Expression_Current_Value (Right_Opnd (Cond));
7623 return;
7624 end if;
7625
7626 -- Check possible relational operator
7627
7628 if Nkind (Cond) in N_Op_Compare then
7629 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
7630 Set_Entity_Current_Value (Left_Opnd (Cond));
7631 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
7632 Set_Entity_Current_Value (Right_Opnd (Cond));
7633 end if;
7634
7635 -- Check possible boolean variable reference
7636
7637 else
7638 Set_Entity_Current_Value (Cond);
7639 end if;
7640 end Set_Expression_Current_Value;
7641
7642 -- Start of processing for Set_Current_Value_Condition
7643
7644 begin
7645 Set_Expression_Current_Value (Condition (Cnode));
7646 end Set_Current_Value_Condition;
7647
7648 --------------------------
7649 -- Set_Elaboration_Flag --
7650 --------------------------
7651
7652 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
7653 Loc : constant Source_Ptr := Sloc (N);
7654 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
7655 Asn : Node_Id;
7656
7657 begin
7658 if Present (Ent) then
7659
7660 -- Nothing to do if at the compilation unit level, because in this
7661 -- case the flag is set by the binder generated elaboration routine.
7662
7663 if Nkind (Parent (N)) = N_Compilation_Unit then
7664 null;
7665
7666 -- Here we do need to generate an assignment statement
7667
7668 else
7669 Check_Restriction (No_Elaboration_Code, N);
7670 Asn :=
7671 Make_Assignment_Statement (Loc,
7672 Name => New_Occurrence_Of (Ent, Loc),
7673 Expression => Make_Integer_Literal (Loc, Uint_1));
7674
7675 if Nkind (Parent (N)) = N_Subunit then
7676 Insert_After (Corresponding_Stub (Parent (N)), Asn);
7677 else
7678 Insert_After (N, Asn);
7679 end if;
7680
7681 Analyze (Asn);
7682
7683 -- Kill current value indication. This is necessary because the
7684 -- tests of this flag are inserted out of sequence and must not
7685 -- pick up bogus indications of the wrong constant value.
7686
7687 Set_Current_Value (Ent, Empty);
7688 end if;
7689 end if;
7690 end Set_Elaboration_Flag;
7691
7692 ----------------------------
7693 -- Set_Renamed_Subprogram --
7694 ----------------------------
7695
7696 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
7697 begin
7698 -- If input node is an identifier, we can just reset it
7699
7700 if Nkind (N) = N_Identifier then
7701 Set_Chars (N, Chars (E));
7702 Set_Entity (N, E);
7703
7704 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
7705
7706 else
7707 declare
7708 CS : constant Boolean := Comes_From_Source (N);
7709 begin
7710 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
7711 Set_Entity (N, E);
7712 Set_Comes_From_Source (N, CS);
7713 Set_Analyzed (N, True);
7714 end;
7715 end if;
7716 end Set_Renamed_Subprogram;
7717
7718 ----------------------------------
7719 -- Silly_Boolean_Array_Not_Test --
7720 ----------------------------------
7721
7722 -- This procedure implements an odd and silly test. We explicitly check
7723 -- for the case where the 'First of the component type is equal to the
7724 -- 'Last of this component type, and if this is the case, we make sure
7725 -- that constraint error is raised. The reason is that the NOT is bound
7726 -- to cause CE in this case, and we will not otherwise catch it.
7727
7728 -- No such check is required for AND and OR, since for both these cases
7729 -- False op False = False, and True op True = True. For the XOR case,
7730 -- see Silly_Boolean_Array_Xor_Test.
7731
7732 -- Believe it or not, this was reported as a bug. Note that nearly always,
7733 -- the test will evaluate statically to False, so the code will be
7734 -- statically removed, and no extra overhead caused.
7735
7736 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
7737 Loc : constant Source_Ptr := Sloc (N);
7738 CT : constant Entity_Id := Component_Type (T);
7739
7740 begin
7741 -- The check we install is
7742
7743 -- constraint_error when
7744 -- component_type'first = component_type'last
7745 -- and then array_type'Length /= 0)
7746
7747 -- We need the last guard because we don't want to raise CE for empty
7748 -- arrays since no out of range values result. (Empty arrays with a
7749 -- component type of True .. True -- very useful -- even the ACATS
7750 -- does not test that marginal case!)
7751
7752 Insert_Action (N,
7753 Make_Raise_Constraint_Error (Loc,
7754 Condition =>
7755 Make_And_Then (Loc,
7756 Left_Opnd =>
7757 Make_Op_Eq (Loc,
7758 Left_Opnd =>
7759 Make_Attribute_Reference (Loc,
7760 Prefix => New_Occurrence_Of (CT, Loc),
7761 Attribute_Name => Name_First),
7762
7763 Right_Opnd =>
7764 Make_Attribute_Reference (Loc,
7765 Prefix => New_Occurrence_Of (CT, Loc),
7766 Attribute_Name => Name_Last)),
7767
7768 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
7769 Reason => CE_Range_Check_Failed));
7770 end Silly_Boolean_Array_Not_Test;
7771
7772 ----------------------------------
7773 -- Silly_Boolean_Array_Xor_Test --
7774 ----------------------------------
7775
7776 -- This procedure implements an odd and silly test. We explicitly check
7777 -- for the XOR case where the component type is True .. True, since this
7778 -- will raise constraint error. A special check is required since CE
7779 -- will not be generated otherwise (cf Expand_Packed_Not).
7780
7781 -- No such check is required for AND and OR, since for both these cases
7782 -- False op False = False, and True op True = True, and no check is
7783 -- required for the case of False .. False, since False xor False = False.
7784 -- See also Silly_Boolean_Array_Not_Test
7785
7786 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
7787 Loc : constant Source_Ptr := Sloc (N);
7788 CT : constant Entity_Id := Component_Type (T);
7789
7790 begin
7791 -- The check we install is
7792
7793 -- constraint_error when
7794 -- Boolean (component_type'First)
7795 -- and then Boolean (component_type'Last)
7796 -- and then array_type'Length /= 0)
7797
7798 -- We need the last guard because we don't want to raise CE for empty
7799 -- arrays since no out of range values result (Empty arrays with a
7800 -- component type of True .. True -- very useful -- even the ACATS
7801 -- does not test that marginal case!).
7802
7803 Insert_Action (N,
7804 Make_Raise_Constraint_Error (Loc,
7805 Condition =>
7806 Make_And_Then (Loc,
7807 Left_Opnd =>
7808 Make_And_Then (Loc,
7809 Left_Opnd =>
7810 Convert_To (Standard_Boolean,
7811 Make_Attribute_Reference (Loc,
7812 Prefix => New_Occurrence_Of (CT, Loc),
7813 Attribute_Name => Name_First)),
7814
7815 Right_Opnd =>
7816 Convert_To (Standard_Boolean,
7817 Make_Attribute_Reference (Loc,
7818 Prefix => New_Occurrence_Of (CT, Loc),
7819 Attribute_Name => Name_Last))),
7820
7821 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
7822 Reason => CE_Range_Check_Failed));
7823 end Silly_Boolean_Array_Xor_Test;
7824
7825 --------------------------
7826 -- Target_Has_Fixed_Ops --
7827 --------------------------
7828
7829 Integer_Sized_Small : Ureal;
7830 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
7831 -- called (we don't want to compute it more than once!)
7832
7833 Long_Integer_Sized_Small : Ureal;
7834 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
7835 -- is called (we don't want to compute it more than once)
7836
7837 First_Time_For_THFO : Boolean := True;
7838 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
7839
7840 function Target_Has_Fixed_Ops
7841 (Left_Typ : Entity_Id;
7842 Right_Typ : Entity_Id;
7843 Result_Typ : Entity_Id) return Boolean
7844 is
7845 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
7846 -- Return True if the given type is a fixed-point type with a small
7847 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
7848 -- an absolute value less than 1.0. This is currently limited to
7849 -- fixed-point types that map to Integer or Long_Integer.
7850
7851 ------------------------
7852 -- Is_Fractional_Type --
7853 ------------------------
7854
7855 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
7856 begin
7857 if Esize (Typ) = Standard_Integer_Size then
7858 return Small_Value (Typ) = Integer_Sized_Small;
7859
7860 elsif Esize (Typ) = Standard_Long_Integer_Size then
7861 return Small_Value (Typ) = Long_Integer_Sized_Small;
7862
7863 else
7864 return False;
7865 end if;
7866 end Is_Fractional_Type;
7867
7868 -- Start of processing for Target_Has_Fixed_Ops
7869
7870 begin
7871 -- Return False if Fractional_Fixed_Ops_On_Target is false
7872
7873 if not Fractional_Fixed_Ops_On_Target then
7874 return False;
7875 end if;
7876
7877 -- Here the target has Fractional_Fixed_Ops, if first time, compute
7878 -- standard constants used by Is_Fractional_Type.
7879
7880 if First_Time_For_THFO then
7881 First_Time_For_THFO := False;
7882
7883 Integer_Sized_Small :=
7884 UR_From_Components
7885 (Num => Uint_1,
7886 Den => UI_From_Int (Standard_Integer_Size - 1),
7887 Rbase => 2);
7888
7889 Long_Integer_Sized_Small :=
7890 UR_From_Components
7891 (Num => Uint_1,
7892 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
7893 Rbase => 2);
7894 end if;
7895
7896 -- Return True if target supports fixed-by-fixed multiply/divide for
7897 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
7898 -- and result types are equivalent fractional types.
7899
7900 return Is_Fractional_Type (Base_Type (Left_Typ))
7901 and then Is_Fractional_Type (Base_Type (Right_Typ))
7902 and then Is_Fractional_Type (Base_Type (Result_Typ))
7903 and then Esize (Left_Typ) = Esize (Right_Typ)
7904 and then Esize (Left_Typ) = Esize (Result_Typ);
7905 end Target_Has_Fixed_Ops;
7906
7907 ------------------------------------------
7908 -- Type_May_Have_Bit_Aligned_Components --
7909 ------------------------------------------
7910
7911 function Type_May_Have_Bit_Aligned_Components
7912 (Typ : Entity_Id) return Boolean
7913 is
7914 begin
7915 -- Array type, check component type
7916
7917 if Is_Array_Type (Typ) then
7918 return
7919 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
7920
7921 -- Record type, check components
7922
7923 elsif Is_Record_Type (Typ) then
7924 declare
7925 E : Entity_Id;
7926
7927 begin
7928 E := First_Component_Or_Discriminant (Typ);
7929 while Present (E) loop
7930 if Component_May_Be_Bit_Aligned (E)
7931 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
7932 then
7933 return True;
7934 end if;
7935
7936 Next_Component_Or_Discriminant (E);
7937 end loop;
7938
7939 return False;
7940 end;
7941
7942 -- Type other than array or record is always OK
7943
7944 else
7945 return False;
7946 end if;
7947 end Type_May_Have_Bit_Aligned_Components;
7948
7949 ----------------------------
7950 -- Wrap_Cleanup_Procedure --
7951 ----------------------------
7952
7953 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
7954 Loc : constant Source_Ptr := Sloc (N);
7955 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
7956 Stmts : constant List_Id := Statements (Stseq);
7957
7958 begin
7959 if Abort_Allowed then
7960 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7961 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7962 end if;
7963 end Wrap_Cleanup_Procedure;
7964
7965 end Exp_Util;