Reverting merge from trunk
[official-gcc.git] / gcc / ada / exp_util.adb
bloba14b1bc1e19b456b6977dfe0ebbc34fb545a5210
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-2013, 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 ------------------------------------------------------------------------------
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_Res; use Sem_Res;
50 with Sem_Type; use Sem_Type;
51 with Sem_Util; use Sem_Util;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Stringt; use Stringt;
55 with Targparm; use Targparm;
56 with Tbuild; use Tbuild;
57 with Ttypes; use Ttypes;
58 with Urealp; use Urealp;
59 with Validsw; use Validsw;
61 package body Exp_Util is
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Build_Task_Array_Image
68 (Loc : Source_Ptr;
69 Id_Ref : Node_Id;
70 A_Type : Entity_Id;
71 Dyn : Boolean := False) return Node_Id;
72 -- Build function to generate the image string for a task that is an array
73 -- component, concatenating the images of each index. To avoid storage
74 -- leaks, the string is built with successive slice assignments. The flag
75 -- Dyn indicates whether this is called for the initialization procedure of
76 -- an array of tasks, or for the name of a dynamically created task that is
77 -- assigned to an indexed component.
79 function Build_Task_Image_Function
80 (Loc : Source_Ptr;
81 Decls : List_Id;
82 Stats : List_Id;
83 Res : Entity_Id) return Node_Id;
84 -- Common processing for Task_Array_Image and Task_Record_Image. Build
85 -- function body that computes image.
87 procedure Build_Task_Image_Prefix
88 (Loc : Source_Ptr;
89 Len : out Entity_Id;
90 Res : out Entity_Id;
91 Pos : out Entity_Id;
92 Prefix : Entity_Id;
93 Sum : Node_Id;
94 Decls : List_Id;
95 Stats : List_Id);
96 -- Common processing for Task_Array_Image and Task_Record_Image. Create
97 -- local variables and assign prefix of name to result string.
99 function Build_Task_Record_Image
100 (Loc : Source_Ptr;
101 Id_Ref : Node_Id;
102 Dyn : Boolean := False) return Node_Id;
103 -- Build function to generate the image string for a task that is a record
104 -- component. Concatenate name of variable with that of selector. The flag
105 -- Dyn indicates whether this is called for the initialization procedure of
106 -- record with task components, or for a dynamically created task that is
107 -- assigned to a selected component.
109 function Make_CW_Equivalent_Type
110 (T : Entity_Id;
111 E : Node_Id) return Entity_Id;
112 -- T is a class-wide type entity, E is the initial expression node that
113 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
114 -- returns the entity of the Equivalent type and inserts on the fly the
115 -- necessary declaration such as:
117 -- type anon is record
118 -- _parent : Root_Type (T); constrained with E discriminants (if any)
119 -- Extension : String (1 .. expr to match size of E);
120 -- end record;
122 -- This record is compatible with any object of the class of T thanks to
123 -- the first field and has the same size as E thanks to the second.
125 function Make_Literal_Range
126 (Loc : Source_Ptr;
127 Literal_Typ : Entity_Id) return Node_Id;
128 -- Produce a Range node whose bounds are:
129 -- Low_Bound (Literal_Type) ..
130 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
131 -- this is used for expanding declarations like X : String := "sdfgdfg";
133 -- If the index type of the target array is not integer, we generate:
134 -- Low_Bound (Literal_Type) ..
135 -- Literal_Type'Val
136 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
137 -- + (Length (Literal_Typ) -1))
139 function Make_Non_Empty_Check
140 (Loc : Source_Ptr;
141 N : Node_Id) return Node_Id;
142 -- Produce a boolean expression checking that the unidimensional array
143 -- node N is not empty.
145 function New_Class_Wide_Subtype
146 (CW_Typ : Entity_Id;
147 N : Node_Id) return Entity_Id;
148 -- Create an implicit subtype of CW_Typ attached to node N
150 function Requires_Cleanup_Actions
151 (L : List_Id;
152 Lib_Level : Boolean;
153 Nested_Constructs : Boolean) return Boolean;
154 -- Given a list L, determine whether it contains one of the following:
156 -- 1) controlled objects
157 -- 2) library-level tagged types
159 -- Lib_Level is True when the list comes from a construct at the library
160 -- level, and False otherwise. Nested_Constructs is True when any nested
161 -- packages declared in L must be processed, and False otherwise.
163 -------------------------------------
164 -- Activate_Atomic_Synchronization --
165 -------------------------------------
167 procedure Activate_Atomic_Synchronization (N : Node_Id) is
168 Msg_Node : Node_Id;
170 begin
171 case Nkind (Parent (N)) is
173 -- Check for cases of appearing in the prefix of a construct where
174 -- we don't need atomic synchronization for this kind of usage.
176 when
177 -- Nothing to do if we are the prefix of an attribute, since we
178 -- do not want an atomic sync operation for things like 'Size.
180 N_Attribute_Reference |
182 -- The N_Reference node is like an attribute
184 N_Reference |
186 -- Nothing to do for a reference to a component (or components)
187 -- of a composite object. Only reads and updates of the object
188 -- as a whole require atomic synchronization (RM C.6 (15)).
190 N_Indexed_Component |
191 N_Selected_Component |
192 N_Slice =>
194 -- For all the above cases, nothing to do if we are the prefix
196 if Prefix (Parent (N)) = N then
197 return;
198 end if;
200 when others => null;
201 end case;
203 -- Go ahead and set the flag
205 Set_Atomic_Sync_Required (N);
207 -- Generate info message if requested
209 if Warn_On_Atomic_Synchronization then
210 case Nkind (N) is
211 when N_Identifier =>
212 Msg_Node := N;
214 when N_Selected_Component | N_Expanded_Name =>
215 Msg_Node := Selector_Name (N);
217 when N_Explicit_Dereference | N_Indexed_Component =>
218 Msg_Node := Empty;
220 when others =>
221 pragma Assert (False);
222 return;
223 end case;
225 if Present (Msg_Node) then
226 Error_Msg_N
227 ("?N?info: atomic synchronization set for &", Msg_Node);
228 else
229 Error_Msg_N
230 ("?N?info: atomic synchronization set", N);
231 end if;
232 end if;
233 end Activate_Atomic_Synchronization;
235 ----------------------
236 -- Adjust_Condition --
237 ----------------------
239 procedure Adjust_Condition (N : Node_Id) is
240 begin
241 if No (N) then
242 return;
243 end if;
245 declare
246 Loc : constant Source_Ptr := Sloc (N);
247 T : constant Entity_Id := Etype (N);
248 Ti : Entity_Id;
250 begin
251 -- Defend against a call where the argument has no type, or has a
252 -- type that is not Boolean. This can occur because of prior errors.
254 if No (T) or else not Is_Boolean_Type (T) then
255 return;
256 end if;
258 -- Apply validity checking if needed
260 if Validity_Checks_On and Validity_Check_Tests then
261 Ensure_Valid (N);
262 end if;
264 -- Immediate return if standard boolean, the most common case,
265 -- where nothing needs to be done.
267 if Base_Type (T) = Standard_Boolean then
268 return;
269 end if;
271 -- Case of zero/non-zero semantics or non-standard enumeration
272 -- representation. In each case, we rewrite the node as:
274 -- ityp!(N) /= False'Enum_Rep
276 -- where ityp is an integer type with large enough size to hold any
277 -- value of type T.
279 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
280 if Esize (T) <= Esize (Standard_Integer) then
281 Ti := Standard_Integer;
282 else
283 Ti := Standard_Long_Long_Integer;
284 end if;
286 Rewrite (N,
287 Make_Op_Ne (Loc,
288 Left_Opnd => Unchecked_Convert_To (Ti, N),
289 Right_Opnd =>
290 Make_Attribute_Reference (Loc,
291 Attribute_Name => Name_Enum_Rep,
292 Prefix =>
293 New_Occurrence_Of (First_Literal (T), Loc))));
294 Analyze_And_Resolve (N, Standard_Boolean);
296 else
297 Rewrite (N, Convert_To (Standard_Boolean, N));
298 Analyze_And_Resolve (N, Standard_Boolean);
299 end if;
300 end;
301 end Adjust_Condition;
303 ------------------------
304 -- Adjust_Result_Type --
305 ------------------------
307 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
308 begin
309 -- Ignore call if current type is not Standard.Boolean
311 if Etype (N) /= Standard_Boolean then
312 return;
313 end if;
315 -- If result is already of correct type, nothing to do. Note that
316 -- this will get the most common case where everything has a type
317 -- of Standard.Boolean.
319 if Base_Type (T) = Standard_Boolean then
320 return;
322 else
323 declare
324 KP : constant Node_Kind := Nkind (Parent (N));
326 begin
327 -- If result is to be used as a Condition in the syntax, no need
328 -- to convert it back, since if it was changed to Standard.Boolean
329 -- using Adjust_Condition, that is just fine for this usage.
331 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
332 return;
334 -- If result is an operand of another logical operation, no need
335 -- to reset its type, since Standard.Boolean is just fine, and
336 -- such operations always do Adjust_Condition on their operands.
338 elsif KP in N_Op_Boolean
339 or else KP in N_Short_Circuit
340 or else KP = N_Op_Not
341 then
342 return;
344 -- Otherwise we perform a conversion from the current type, which
345 -- must be Standard.Boolean, to the desired type.
347 else
348 Set_Analyzed (N);
349 Rewrite (N, Convert_To (T, N));
350 Analyze_And_Resolve (N, T);
351 end if;
352 end;
353 end if;
354 end Adjust_Result_Type;
356 --------------------------
357 -- Append_Freeze_Action --
358 --------------------------
360 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
361 Fnode : Node_Id;
363 begin
364 Ensure_Freeze_Node (T);
365 Fnode := Freeze_Node (T);
367 if No (Actions (Fnode)) then
368 Set_Actions (Fnode, New_List (N));
369 else
370 Append (N, Actions (Fnode));
371 end if;
373 end Append_Freeze_Action;
375 ---------------------------
376 -- Append_Freeze_Actions --
377 ---------------------------
379 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
380 Fnode : Node_Id;
382 begin
383 if No (L) then
384 return;
385 end if;
387 Ensure_Freeze_Node (T);
388 Fnode := Freeze_Node (T);
390 if No (Actions (Fnode)) then
391 Set_Actions (Fnode, L);
392 else
393 Append_List (L, Actions (Fnode));
394 end if;
395 end Append_Freeze_Actions;
397 ------------------------------------
398 -- Build_Allocate_Deallocate_Proc --
399 ------------------------------------
401 procedure Build_Allocate_Deallocate_Proc
402 (N : Node_Id;
403 Is_Allocate : Boolean)
405 Desig_Typ : Entity_Id;
406 Expr : Node_Id;
407 Pool_Id : Entity_Id;
408 Proc_To_Call : Node_Id := Empty;
409 Ptr_Typ : Entity_Id;
411 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id;
412 -- Locate TSS primitive Finalize_Address in type Typ
414 function Find_Object (E : Node_Id) return Node_Id;
415 -- Given an arbitrary expression of an allocator, try to find an object
416 -- reference in it, otherwise return the original expression.
418 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
419 -- Determine whether subprogram Subp denotes a custom allocate or
420 -- deallocate.
422 ---------------------------
423 -- Find_Finalize_Address --
424 ---------------------------
426 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id is
427 Utyp : Entity_Id := Typ;
429 begin
430 -- Handle protected class-wide or task class-wide types
432 if Is_Class_Wide_Type (Utyp) then
433 if Is_Concurrent_Type (Root_Type (Utyp)) then
434 Utyp := Root_Type (Utyp);
436 elsif Is_Private_Type (Root_Type (Utyp))
437 and then Present (Full_View (Root_Type (Utyp)))
438 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
439 then
440 Utyp := Full_View (Root_Type (Utyp));
441 end if;
442 end if;
444 -- Handle private types
446 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
447 Utyp := Full_View (Utyp);
448 end if;
450 -- Handle protected and task types
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;
458 Utyp := Underlying_Type (Base_Type (Utyp));
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???)
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)));
472 if Is_Protected_Type (Utyp) then
473 Utyp := Corresponding_Record_Type (Utyp);
474 end if;
475 end if;
476 end if;
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.
482 if Utyp /= Base_Type (Utyp) then
483 pragma Assert (Is_Private_Type (Typ));
485 Utyp := Base_Type (Utyp);
486 end if;
488 -- When dealing with an internally built full view for a type with
489 -- unknown discriminants, use the original record type.
491 if Is_Underlying_Record_View (Utyp) then
492 Utyp := Etype (Utyp);
493 end if;
495 return TSS (Utyp, TSS_Finalize_Address);
496 end Find_Finalize_Address;
498 -----------------
499 -- Find_Object --
500 -----------------
502 function Find_Object (E : Node_Id) return Node_Id is
503 Expr : Node_Id;
505 begin
506 pragma Assert (Is_Allocate);
508 Expr := E;
509 loop
510 if Nkind_In (Expr, N_Qualified_Expression,
511 N_Unchecked_Type_Conversion)
512 then
513 Expr := Expression (Expr);
515 elsif Nkind (Expr) = N_Explicit_Dereference then
516 Expr := Prefix (Expr);
518 else
519 exit;
520 end if;
521 end loop;
523 return Expr;
524 end Find_Object;
526 ---------------------------------
527 -- Is_Allocate_Deallocate_Proc --
528 ---------------------------------
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.
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;
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))));
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;
557 return False;
558 end Is_Allocate_Deallocate_Proc;
560 -- Start of processing for Build_Allocate_Deallocate_Proc
562 begin
563 -- Obtain the attributes of the allocation / deallocation
565 if Nkind (N) = N_Free_Statement then
566 Expr := Expression (N);
567 Ptr_Typ := Base_Type (Etype (Expr));
568 Proc_To_Call := Procedure_To_Call (N);
570 else
571 if Nkind (N) = N_Object_Declaration then
572 Expr := Expression (N);
573 else
574 Expr := N;
575 end if;
577 -- In certain cases an allocator with a qualified expression may
578 -- be relocated and used as the initialization expression of a
579 -- temporary:
581 -- before:
582 -- Obj : Ptr_Typ := new Desig_Typ'(...);
584 -- after:
585 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
586 -- Obj : Ptr_Typ := Tmp;
588 -- Since the allocator is always marked as analyzed to avoid infinite
589 -- expansion, it will never be processed by this routine given that
590 -- the designated type needs finalization actions. Detect this case
591 -- and complete the expansion of the allocator.
593 if Nkind (Expr) = N_Identifier
594 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
595 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
596 then
597 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
598 return;
599 end if;
601 -- The allocator may have been rewritten into something else in which
602 -- case the expansion performed by this routine does not apply.
604 if Nkind (Expr) /= N_Allocator then
605 return;
606 end if;
608 Ptr_Typ := Base_Type (Etype (Expr));
609 Proc_To_Call := Procedure_To_Call (Expr);
610 end if;
612 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
613 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
615 -- Handle concurrent types
617 if Is_Concurrent_Type (Desig_Typ)
618 and then Present (Corresponding_Record_Type (Desig_Typ))
619 then
620 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
621 end if;
623 -- Do not process allocations / deallocations without a pool
625 if No (Pool_Id) then
626 return;
628 -- Do not process allocations on / deallocations from the secondary
629 -- stack.
631 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
632 return;
634 -- Do not replicate the machinery if the allocator / free has already
635 -- been expanded and has a custom Allocate / Deallocate.
637 elsif Present (Proc_To_Call)
638 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
639 then
640 return;
641 end if;
643 if Needs_Finalization (Desig_Typ) then
645 -- Certain run-time configurations and targets do not provide support
646 -- for controlled types.
648 if Restriction_Active (No_Finalization) then
649 return;
651 -- Do nothing if the access type may never allocate / deallocate
652 -- objects.
654 elsif No_Pool_Assigned (Ptr_Typ) then
655 return;
657 -- Access-to-controlled types are not supported on .NET/JVM since
658 -- these targets cannot support pools and address arithmetic.
660 elsif VM_Target /= No_VM then
661 return;
662 end if;
664 -- The allocation / deallocation of a controlled object must be
665 -- chained on / detached from a finalization master.
667 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
669 -- The only other kind of allocation / deallocation supported by this
670 -- routine is on / from a subpool.
672 elsif Nkind (Expr) = N_Allocator
673 and then No (Subpool_Handle_Name (Expr))
674 then
675 return;
676 end if;
678 declare
679 Loc : constant Source_Ptr := Sloc (N);
680 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
681 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
682 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
683 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
685 Actuals : List_Id;
686 Fin_Addr_Id : Entity_Id;
687 Fin_Mas_Act : Node_Id;
688 Fin_Mas_Id : Entity_Id;
689 Proc_To_Call : Entity_Id;
690 Subpool : Node_Id := Empty;
692 begin
693 -- Step 1: Construct all the actuals for the call to library routine
694 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
696 -- a) Storage pool
698 Actuals := New_List (New_Reference_To (Pool_Id, Loc));
700 if Is_Allocate then
702 -- b) Subpool
704 if Nkind (Expr) = N_Allocator then
705 Subpool := Subpool_Handle_Name (Expr);
706 end if;
708 -- If a subpool is present it can be an arbitrary name, so make
709 -- the actual by copying the tree.
711 if Present (Subpool) then
712 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
713 else
714 Append_To (Actuals, Make_Null (Loc));
715 end if;
717 -- c) Finalization master
719 if Needs_Finalization (Desig_Typ) then
720 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
721 Fin_Mas_Act := New_Reference_To (Fin_Mas_Id, Loc);
723 -- Handle the case where the master is actually a pointer to a
724 -- master. This case arises in build-in-place functions.
726 if Is_Access_Type (Etype (Fin_Mas_Id)) then
727 Append_To (Actuals, Fin_Mas_Act);
728 else
729 Append_To (Actuals,
730 Make_Attribute_Reference (Loc,
731 Prefix => Fin_Mas_Act,
732 Attribute_Name => Name_Unrestricted_Access));
733 end if;
734 else
735 Append_To (Actuals, Make_Null (Loc));
736 end if;
738 -- d) Finalize_Address
740 -- Primitive Finalize_Address is never generated in CodePeer mode
741 -- since it contains an Unchecked_Conversion.
743 if Needs_Finalization (Desig_Typ) and then not CodePeer_Mode then
744 Fin_Addr_Id := Find_Finalize_Address (Desig_Typ);
745 pragma Assert (Present (Fin_Addr_Id));
747 Append_To (Actuals,
748 Make_Attribute_Reference (Loc,
749 Prefix => New_Reference_To (Fin_Addr_Id, Loc),
750 Attribute_Name => Name_Unrestricted_Access));
751 else
752 Append_To (Actuals, Make_Null (Loc));
753 end if;
754 end if;
756 -- e) Address
757 -- f) Storage_Size
758 -- g) Alignment
760 Append_To (Actuals, New_Reference_To (Addr_Id, Loc));
761 Append_To (Actuals, New_Reference_To (Size_Id, Loc));
763 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
764 Append_To (Actuals, New_Reference_To (Alig_Id, Loc));
766 -- For deallocation of class wide types we obtain the value of
767 -- alignment from the Type Specific Record of the deallocated object.
768 -- This is needed because the frontend expansion of class-wide types
769 -- into equivalent types confuses the backend.
771 else
772 -- Generate:
773 -- Obj.all'Alignment
775 -- ... because 'Alignment applied to class-wide types is expanded
776 -- into the code that reads the value of alignment from the TSD
777 -- (see Expand_N_Attribute_Reference)
779 Append_To (Actuals,
780 Unchecked_Convert_To (RTE (RE_Storage_Offset),
781 Make_Attribute_Reference (Loc,
782 Prefix =>
783 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
784 Attribute_Name => Name_Alignment)));
785 end if;
787 -- h) Is_Controlled
789 -- Generate a run-time check to determine whether a class-wide object
790 -- is truly controlled.
792 if Needs_Finalization (Desig_Typ) then
793 if Is_Class_Wide_Type (Desig_Typ)
794 or else Is_Generic_Actual_Type (Desig_Typ)
795 then
796 declare
797 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
798 Flag_Expr : Node_Id;
799 Param : Node_Id;
800 Temp : Node_Id;
802 begin
803 if Is_Allocate then
804 Temp := Find_Object (Expression (Expr));
805 else
806 Temp := Expr;
807 end if;
809 -- Processing for generic actuals
811 if Is_Generic_Actual_Type (Desig_Typ) then
812 Flag_Expr :=
813 New_Reference_To (Boolean_Literals
814 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
816 -- Processing for subtype indications
818 elsif Nkind (Temp) in N_Has_Entity
819 and then Is_Type (Entity (Temp))
820 then
821 Flag_Expr :=
822 New_Reference_To (Boolean_Literals
823 (Needs_Finalization (Entity (Temp))), Loc);
825 -- Generate a runtime check to test the controlled state of
826 -- an object for the purposes of allocation / deallocation.
828 else
829 -- The following case arises when allocating through an
830 -- interface class-wide type, generate:
832 -- Temp.all
834 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
835 Param :=
836 Make_Explicit_Dereference (Loc,
837 Prefix =>
838 Relocate_Node (Temp));
840 -- Generate:
841 -- Temp'Tag
843 else
844 Param :=
845 Make_Attribute_Reference (Loc,
846 Prefix =>
847 Relocate_Node (Temp),
848 Attribute_Name => Name_Tag);
849 end if;
851 -- Generate:
852 -- Needs_Finalization (<Param>)
854 Flag_Expr :=
855 Make_Function_Call (Loc,
856 Name =>
857 New_Reference_To (RTE (RE_Needs_Finalization), Loc),
858 Parameter_Associations => New_List (Param));
859 end if;
861 -- Create the temporary which represents the finalization
862 -- state of the expression. Generate:
864 -- F : constant Boolean := <Flag_Expr>;
866 Insert_Action (N,
867 Make_Object_Declaration (Loc,
868 Defining_Identifier => Flag_Id,
869 Constant_Present => True,
870 Object_Definition =>
871 New_Reference_To (Standard_Boolean, Loc),
872 Expression => Flag_Expr));
874 -- The flag acts as the last actual
876 Append_To (Actuals, New_Reference_To (Flag_Id, Loc));
877 end;
879 -- The object is statically known to be controlled
881 else
882 Append_To (Actuals, New_Reference_To (Standard_True, Loc));
883 end if;
885 else
886 Append_To (Actuals, New_Reference_To (Standard_False, Loc));
887 end if;
889 -- i) On_Subpool
891 if Is_Allocate then
892 Append_To (Actuals,
893 New_Reference_To (Boolean_Literals (Present (Subpool)), Loc));
894 end if;
896 -- Step 2: Build a wrapper Allocate / Deallocate which internally
897 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
899 -- Select the proper routine to call
901 if Is_Allocate then
902 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
903 else
904 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
905 end if;
907 -- Create a custom Allocate / Deallocate routine which has identical
908 -- profile to that of System.Storage_Pools.
910 Insert_Action (N,
911 Make_Subprogram_Body (Loc,
912 Specification =>
914 -- procedure Pnn
916 Make_Procedure_Specification (Loc,
917 Defining_Unit_Name => Proc_Id,
918 Parameter_Specifications => New_List (
920 -- P : Root_Storage_Pool
922 Make_Parameter_Specification (Loc,
923 Defining_Identifier => Make_Temporary (Loc, 'P'),
924 Parameter_Type =>
925 New_Reference_To (RTE (RE_Root_Storage_Pool), Loc)),
927 -- A : [out] Address
929 Make_Parameter_Specification (Loc,
930 Defining_Identifier => Addr_Id,
931 Out_Present => Is_Allocate,
932 Parameter_Type =>
933 New_Reference_To (RTE (RE_Address), Loc)),
935 -- S : Storage_Count
937 Make_Parameter_Specification (Loc,
938 Defining_Identifier => Size_Id,
939 Parameter_Type =>
940 New_Reference_To (RTE (RE_Storage_Count), Loc)),
942 -- L : Storage_Count
944 Make_Parameter_Specification (Loc,
945 Defining_Identifier => Alig_Id,
946 Parameter_Type =>
947 New_Reference_To (RTE (RE_Storage_Count), Loc)))),
949 Declarations => No_List,
951 Handled_Statement_Sequence =>
952 Make_Handled_Sequence_Of_Statements (Loc,
953 Statements => New_List (
954 Make_Procedure_Call_Statement (Loc,
955 Name => New_Reference_To (Proc_To_Call, Loc),
956 Parameter_Associations => Actuals)))));
958 -- The newly generated Allocate / Deallocate becomes the default
959 -- procedure to call when the back end processes the allocation /
960 -- deallocation.
962 if Is_Allocate then
963 Set_Procedure_To_Call (Expr, Proc_Id);
964 else
965 Set_Procedure_To_Call (N, Proc_Id);
966 end if;
967 end;
968 end Build_Allocate_Deallocate_Proc;
970 ------------------------
971 -- Build_Runtime_Call --
972 ------------------------
974 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
975 begin
976 -- If entity is not available, we can skip making the call (this avoids
977 -- junk duplicated error messages in a number of cases).
979 if not RTE_Available (RE) then
980 return Make_Null_Statement (Loc);
981 else
982 return
983 Make_Procedure_Call_Statement (Loc,
984 Name => New_Reference_To (RTE (RE), Loc));
985 end if;
986 end Build_Runtime_Call;
988 ----------------------------
989 -- Build_Task_Array_Image --
990 ----------------------------
992 -- This function generates the body for a function that constructs the
993 -- image string for a task that is an array component. The function is
994 -- local to the init proc for the array type, and is called for each one
995 -- of the components. The constructed image has the form of an indexed
996 -- component, whose prefix is the outer variable of the array type.
997 -- The n-dimensional array type has known indexes Index, Index2...
999 -- Id_Ref is an indexed component form created by the enclosing init proc.
1000 -- Its successive indexes are Val1, Val2, ... which are the loop variables
1001 -- in the loops that call the individual task init proc on each component.
1003 -- The generated function has the following structure:
1005 -- function F return String is
1006 -- Pref : string renames Task_Name;
1007 -- T1 : String := Index1'Image (Val1);
1008 -- ...
1009 -- Tn : String := indexn'image (Valn);
1010 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
1011 -- -- Len includes commas and the end parentheses.
1012 -- Res : String (1..Len);
1013 -- Pos : Integer := Pref'Length;
1015 -- begin
1016 -- Res (1 .. Pos) := Pref;
1017 -- Pos := Pos + 1;
1018 -- Res (Pos) := '(';
1019 -- Pos := Pos + 1;
1020 -- Res (Pos .. Pos + T1'Length - 1) := T1;
1021 -- Pos := Pos + T1'Length;
1022 -- Res (Pos) := '.';
1023 -- Pos := Pos + 1;
1024 -- ...
1025 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
1026 -- Res (Len) := ')';
1028 -- return Res;
1029 -- end F;
1031 -- Needless to say, multidimensional arrays of tasks are rare enough that
1032 -- the bulkiness of this code is not really a concern.
1034 function Build_Task_Array_Image
1035 (Loc : Source_Ptr;
1036 Id_Ref : Node_Id;
1037 A_Type : Entity_Id;
1038 Dyn : Boolean := False) return Node_Id
1040 Dims : constant Nat := Number_Dimensions (A_Type);
1041 -- Number of dimensions for array of tasks
1043 Temps : array (1 .. Dims) of Entity_Id;
1044 -- Array of temporaries to hold string for each index
1046 Indx : Node_Id;
1047 -- Index expression
1049 Len : Entity_Id;
1050 -- Total length of generated name
1052 Pos : Entity_Id;
1053 -- Running index for substring assignments
1055 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1056 -- Name of enclosing variable, prefix of resulting name
1058 Res : Entity_Id;
1059 -- String to hold result
1061 Val : Node_Id;
1062 -- Value of successive indexes
1064 Sum : Node_Id;
1065 -- Expression to compute total size of string
1067 T : Entity_Id;
1068 -- Entity for name at one index position
1070 Decls : constant List_Id := New_List;
1071 Stats : constant List_Id := New_List;
1073 begin
1074 -- For a dynamic task, the name comes from the target variable. For a
1075 -- static one it is a formal of the enclosing init proc.
1077 if Dyn then
1078 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1079 Append_To (Decls,
1080 Make_Object_Declaration (Loc,
1081 Defining_Identifier => Pref,
1082 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1083 Expression =>
1084 Make_String_Literal (Loc,
1085 Strval => String_From_Name_Buffer)));
1087 else
1088 Append_To (Decls,
1089 Make_Object_Renaming_Declaration (Loc,
1090 Defining_Identifier => Pref,
1091 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1092 Name => Make_Identifier (Loc, Name_uTask_Name)));
1093 end if;
1095 Indx := First_Index (A_Type);
1096 Val := First (Expressions (Id_Ref));
1098 for J in 1 .. Dims loop
1099 T := Make_Temporary (Loc, 'T');
1100 Temps (J) := T;
1102 Append_To (Decls,
1103 Make_Object_Declaration (Loc,
1104 Defining_Identifier => T,
1105 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1106 Expression =>
1107 Make_Attribute_Reference (Loc,
1108 Attribute_Name => Name_Image,
1109 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
1110 Expressions => New_List (New_Copy_Tree (Val)))));
1112 Next_Index (Indx);
1113 Next (Val);
1114 end loop;
1116 Sum := Make_Integer_Literal (Loc, Dims + 1);
1118 Sum :=
1119 Make_Op_Add (Loc,
1120 Left_Opnd => Sum,
1121 Right_Opnd =>
1122 Make_Attribute_Reference (Loc,
1123 Attribute_Name => Name_Length,
1124 Prefix => New_Occurrence_Of (Pref, Loc),
1125 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1127 for J in 1 .. Dims loop
1128 Sum :=
1129 Make_Op_Add (Loc,
1130 Left_Opnd => Sum,
1131 Right_Opnd =>
1132 Make_Attribute_Reference (Loc,
1133 Attribute_Name => Name_Length,
1134 Prefix =>
1135 New_Occurrence_Of (Temps (J), Loc),
1136 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1137 end loop;
1139 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1141 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
1143 Append_To (Stats,
1144 Make_Assignment_Statement (Loc,
1145 Name =>
1146 Make_Indexed_Component (Loc,
1147 Prefix => New_Occurrence_Of (Res, Loc),
1148 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1149 Expression =>
1150 Make_Character_Literal (Loc,
1151 Chars => Name_Find,
1152 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
1154 Append_To (Stats,
1155 Make_Assignment_Statement (Loc,
1156 Name => New_Occurrence_Of (Pos, Loc),
1157 Expression =>
1158 Make_Op_Add (Loc,
1159 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1160 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1162 for J in 1 .. Dims loop
1164 Append_To (Stats,
1165 Make_Assignment_Statement (Loc,
1166 Name =>
1167 Make_Slice (Loc,
1168 Prefix => New_Occurrence_Of (Res, Loc),
1169 Discrete_Range =>
1170 Make_Range (Loc,
1171 Low_Bound => New_Occurrence_Of (Pos, Loc),
1172 High_Bound =>
1173 Make_Op_Subtract (Loc,
1174 Left_Opnd =>
1175 Make_Op_Add (Loc,
1176 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1177 Right_Opnd =>
1178 Make_Attribute_Reference (Loc,
1179 Attribute_Name => Name_Length,
1180 Prefix =>
1181 New_Occurrence_Of (Temps (J), Loc),
1182 Expressions =>
1183 New_List (Make_Integer_Literal (Loc, 1)))),
1184 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
1186 Expression => New_Occurrence_Of (Temps (J), Loc)));
1188 if J < Dims then
1189 Append_To (Stats,
1190 Make_Assignment_Statement (Loc,
1191 Name => New_Occurrence_Of (Pos, Loc),
1192 Expression =>
1193 Make_Op_Add (Loc,
1194 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1195 Right_Opnd =>
1196 Make_Attribute_Reference (Loc,
1197 Attribute_Name => Name_Length,
1198 Prefix => New_Occurrence_Of (Temps (J), Loc),
1199 Expressions =>
1200 New_List (Make_Integer_Literal (Loc, 1))))));
1202 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
1204 Append_To (Stats,
1205 Make_Assignment_Statement (Loc,
1206 Name => Make_Indexed_Component (Loc,
1207 Prefix => New_Occurrence_Of (Res, Loc),
1208 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1209 Expression =>
1210 Make_Character_Literal (Loc,
1211 Chars => Name_Find,
1212 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
1214 Append_To (Stats,
1215 Make_Assignment_Statement (Loc,
1216 Name => New_Occurrence_Of (Pos, Loc),
1217 Expression =>
1218 Make_Op_Add (Loc,
1219 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1220 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1221 end if;
1222 end loop;
1224 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
1226 Append_To (Stats,
1227 Make_Assignment_Statement (Loc,
1228 Name =>
1229 Make_Indexed_Component (Loc,
1230 Prefix => New_Occurrence_Of (Res, Loc),
1231 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
1232 Expression =>
1233 Make_Character_Literal (Loc,
1234 Chars => Name_Find,
1235 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
1236 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1237 end Build_Task_Array_Image;
1239 ----------------------------
1240 -- Build_Task_Image_Decls --
1241 ----------------------------
1243 function Build_Task_Image_Decls
1244 (Loc : Source_Ptr;
1245 Id_Ref : Node_Id;
1246 A_Type : Entity_Id;
1247 In_Init_Proc : Boolean := False) return List_Id
1249 Decls : constant List_Id := New_List;
1250 T_Id : Entity_Id := Empty;
1251 Decl : Node_Id;
1252 Expr : Node_Id := Empty;
1253 Fun : Node_Id := Empty;
1254 Is_Dyn : constant Boolean :=
1255 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
1256 and then
1257 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
1259 begin
1260 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
1261 -- generate a dummy declaration only.
1263 if Restriction_Active (No_Implicit_Heap_Allocations)
1264 or else Global_Discard_Names
1265 then
1266 T_Id := Make_Temporary (Loc, 'J');
1267 Name_Len := 0;
1269 return
1270 New_List (
1271 Make_Object_Declaration (Loc,
1272 Defining_Identifier => T_Id,
1273 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1274 Expression =>
1275 Make_String_Literal (Loc,
1276 Strval => String_From_Name_Buffer)));
1278 else
1279 if Nkind (Id_Ref) = N_Identifier
1280 or else Nkind (Id_Ref) = N_Defining_Identifier
1281 then
1282 -- For a simple variable, the image of the task is built from
1283 -- the name of the variable. To avoid possible conflict with the
1284 -- anonymous type created for a single protected object, add a
1285 -- numeric suffix.
1287 T_Id :=
1288 Make_Defining_Identifier (Loc,
1289 New_External_Name (Chars (Id_Ref), 'T', 1));
1291 Get_Name_String (Chars (Id_Ref));
1293 Expr :=
1294 Make_String_Literal (Loc,
1295 Strval => String_From_Name_Buffer);
1297 elsif Nkind (Id_Ref) = N_Selected_Component then
1298 T_Id :=
1299 Make_Defining_Identifier (Loc,
1300 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
1301 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
1303 elsif Nkind (Id_Ref) = N_Indexed_Component then
1304 T_Id :=
1305 Make_Defining_Identifier (Loc,
1306 New_External_Name (Chars (A_Type), 'N'));
1308 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
1309 end if;
1310 end if;
1312 if Present (Fun) then
1313 Append (Fun, Decls);
1314 Expr := Make_Function_Call (Loc,
1315 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
1317 if not In_Init_Proc and then VM_Target = No_VM then
1318 Set_Uses_Sec_Stack (Defining_Entity (Fun));
1319 end if;
1320 end if;
1322 Decl := Make_Object_Declaration (Loc,
1323 Defining_Identifier => T_Id,
1324 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1325 Constant_Present => True,
1326 Expression => Expr);
1328 Append (Decl, Decls);
1329 return Decls;
1330 end Build_Task_Image_Decls;
1332 -------------------------------
1333 -- Build_Task_Image_Function --
1334 -------------------------------
1336 function Build_Task_Image_Function
1337 (Loc : Source_Ptr;
1338 Decls : List_Id;
1339 Stats : List_Id;
1340 Res : Entity_Id) return Node_Id
1342 Spec : Node_Id;
1344 begin
1345 Append_To (Stats,
1346 Make_Simple_Return_Statement (Loc,
1347 Expression => New_Occurrence_Of (Res, Loc)));
1349 Spec := Make_Function_Specification (Loc,
1350 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
1351 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
1353 -- Calls to 'Image use the secondary stack, which must be cleaned up
1354 -- after the task name is built.
1356 return Make_Subprogram_Body (Loc,
1357 Specification => Spec,
1358 Declarations => Decls,
1359 Handled_Statement_Sequence =>
1360 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
1361 end Build_Task_Image_Function;
1363 -----------------------------
1364 -- Build_Task_Image_Prefix --
1365 -----------------------------
1367 procedure Build_Task_Image_Prefix
1368 (Loc : Source_Ptr;
1369 Len : out Entity_Id;
1370 Res : out Entity_Id;
1371 Pos : out Entity_Id;
1372 Prefix : Entity_Id;
1373 Sum : Node_Id;
1374 Decls : List_Id;
1375 Stats : List_Id)
1377 begin
1378 Len := Make_Temporary (Loc, 'L', Sum);
1380 Append_To (Decls,
1381 Make_Object_Declaration (Loc,
1382 Defining_Identifier => Len,
1383 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
1384 Expression => Sum));
1386 Res := Make_Temporary (Loc, 'R');
1388 Append_To (Decls,
1389 Make_Object_Declaration (Loc,
1390 Defining_Identifier => Res,
1391 Object_Definition =>
1392 Make_Subtype_Indication (Loc,
1393 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1394 Constraint =>
1395 Make_Index_Or_Discriminant_Constraint (Loc,
1396 Constraints =>
1397 New_List (
1398 Make_Range (Loc,
1399 Low_Bound => Make_Integer_Literal (Loc, 1),
1400 High_Bound => New_Occurrence_Of (Len, Loc)))))));
1402 Pos := Make_Temporary (Loc, 'P');
1404 Append_To (Decls,
1405 Make_Object_Declaration (Loc,
1406 Defining_Identifier => Pos,
1407 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
1409 -- Pos := Prefix'Length;
1411 Append_To (Stats,
1412 Make_Assignment_Statement (Loc,
1413 Name => New_Occurrence_Of (Pos, Loc),
1414 Expression =>
1415 Make_Attribute_Reference (Loc,
1416 Attribute_Name => Name_Length,
1417 Prefix => New_Occurrence_Of (Prefix, Loc),
1418 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
1420 -- Res (1 .. Pos) := Prefix;
1422 Append_To (Stats,
1423 Make_Assignment_Statement (Loc,
1424 Name =>
1425 Make_Slice (Loc,
1426 Prefix => New_Occurrence_Of (Res, Loc),
1427 Discrete_Range =>
1428 Make_Range (Loc,
1429 Low_Bound => Make_Integer_Literal (Loc, 1),
1430 High_Bound => New_Occurrence_Of (Pos, Loc))),
1432 Expression => New_Occurrence_Of (Prefix, Loc)));
1434 Append_To (Stats,
1435 Make_Assignment_Statement (Loc,
1436 Name => New_Occurrence_Of (Pos, Loc),
1437 Expression =>
1438 Make_Op_Add (Loc,
1439 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1440 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1441 end Build_Task_Image_Prefix;
1443 -----------------------------
1444 -- Build_Task_Record_Image --
1445 -----------------------------
1447 function Build_Task_Record_Image
1448 (Loc : Source_Ptr;
1449 Id_Ref : Node_Id;
1450 Dyn : Boolean := False) return Node_Id
1452 Len : Entity_Id;
1453 -- Total length of generated name
1455 Pos : Entity_Id;
1456 -- Index into result
1458 Res : Entity_Id;
1459 -- String to hold result
1461 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1462 -- Name of enclosing variable, prefix of resulting name
1464 Sum : Node_Id;
1465 -- Expression to compute total size of string
1467 Sel : Entity_Id;
1468 -- Entity for selector name
1470 Decls : constant List_Id := New_List;
1471 Stats : constant List_Id := New_List;
1473 begin
1474 -- For a dynamic task, the name comes from the target variable. For a
1475 -- static one it is a formal of the enclosing init proc.
1477 if Dyn then
1478 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1479 Append_To (Decls,
1480 Make_Object_Declaration (Loc,
1481 Defining_Identifier => Pref,
1482 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1483 Expression =>
1484 Make_String_Literal (Loc,
1485 Strval => String_From_Name_Buffer)));
1487 else
1488 Append_To (Decls,
1489 Make_Object_Renaming_Declaration (Loc,
1490 Defining_Identifier => Pref,
1491 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1492 Name => Make_Identifier (Loc, Name_uTask_Name)));
1493 end if;
1495 Sel := Make_Temporary (Loc, 'S');
1497 Get_Name_String (Chars (Selector_Name (Id_Ref)));
1499 Append_To (Decls,
1500 Make_Object_Declaration (Loc,
1501 Defining_Identifier => Sel,
1502 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1503 Expression =>
1504 Make_String_Literal (Loc,
1505 Strval => String_From_Name_Buffer)));
1507 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
1509 Sum :=
1510 Make_Op_Add (Loc,
1511 Left_Opnd => Sum,
1512 Right_Opnd =>
1513 Make_Attribute_Reference (Loc,
1514 Attribute_Name => Name_Length,
1515 Prefix =>
1516 New_Occurrence_Of (Pref, Loc),
1517 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1519 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1521 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
1523 -- Res (Pos) := '.';
1525 Append_To (Stats,
1526 Make_Assignment_Statement (Loc,
1527 Name => Make_Indexed_Component (Loc,
1528 Prefix => New_Occurrence_Of (Res, Loc),
1529 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1530 Expression =>
1531 Make_Character_Literal (Loc,
1532 Chars => Name_Find,
1533 Char_Literal_Value =>
1534 UI_From_Int (Character'Pos ('.')))));
1536 Append_To (Stats,
1537 Make_Assignment_Statement (Loc,
1538 Name => New_Occurrence_Of (Pos, Loc),
1539 Expression =>
1540 Make_Op_Add (Loc,
1541 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1542 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1544 -- Res (Pos .. Len) := Selector;
1546 Append_To (Stats,
1547 Make_Assignment_Statement (Loc,
1548 Name => Make_Slice (Loc,
1549 Prefix => New_Occurrence_Of (Res, Loc),
1550 Discrete_Range =>
1551 Make_Range (Loc,
1552 Low_Bound => New_Occurrence_Of (Pos, Loc),
1553 High_Bound => New_Occurrence_Of (Len, Loc))),
1554 Expression => New_Occurrence_Of (Sel, Loc)));
1556 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1557 end Build_Task_Record_Image;
1559 ----------------------------------
1560 -- Component_May_Be_Bit_Aligned --
1561 ----------------------------------
1563 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
1564 UT : Entity_Id;
1566 begin
1567 -- If no component clause, then everything is fine, since the back end
1568 -- never bit-misaligns by default, even if there is a pragma Packed for
1569 -- the record.
1571 if No (Comp) or else No (Component_Clause (Comp)) then
1572 return False;
1573 end if;
1575 UT := Underlying_Type (Etype (Comp));
1577 -- It is only array and record types that cause trouble
1579 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
1580 return False;
1582 -- If we know that we have a small (64 bits or less) record or small
1583 -- bit-packed array, then everything is fine, since the back end can
1584 -- handle these cases correctly.
1586 elsif Esize (Comp) <= 64
1587 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
1588 then
1589 return False;
1591 -- Otherwise if the component is not byte aligned, we know we have the
1592 -- nasty unaligned case.
1594 elsif Normalized_First_Bit (Comp) /= Uint_0
1595 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
1596 then
1597 return True;
1599 -- If we are large and byte aligned, then OK at this level
1601 else
1602 return False;
1603 end if;
1604 end Component_May_Be_Bit_Aligned;
1606 -----------------------------------
1607 -- Corresponding_Runtime_Package --
1608 -----------------------------------
1610 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1611 Pkg_Id : RTU_Id := RTU_Null;
1613 begin
1614 pragma Assert (Is_Concurrent_Type (Typ));
1616 if Ekind (Typ) in Protected_Kind then
1617 if Has_Entries (Typ)
1619 -- A protected type without entries that covers an interface and
1620 -- overrides the abstract routines with protected procedures is
1621 -- considered equivalent to a protected type with entries in the
1622 -- context of dispatching select statements. It is sufficient to
1623 -- check for the presence of an interface list in the declaration
1624 -- node to recognize this case.
1626 or else Present (Interface_List (Parent (Typ)))
1628 -- Protected types with interrupt handlers (when not using a
1629 -- restricted profile) are also considered equivalent to
1630 -- protected types with entries. The types which are used
1631 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
1632 -- are derived from Protection_Entries.
1634 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
1635 or else Has_Interrupt_Handler (Typ)
1636 then
1637 if Abort_Allowed
1638 or else Restriction_Active (No_Entry_Queue) = False
1639 or else Number_Entries (Typ) > 1
1640 or else (Has_Attach_Handler (Typ)
1641 and then not Restricted_Profile)
1642 then
1643 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1644 else
1645 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1646 end if;
1648 else
1649 Pkg_Id := System_Tasking_Protected_Objects;
1650 end if;
1651 end if;
1653 return Pkg_Id;
1654 end Corresponding_Runtime_Package;
1656 -------------------------------
1657 -- Convert_To_Actual_Subtype --
1658 -------------------------------
1660 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1661 Act_ST : Entity_Id;
1663 begin
1664 Act_ST := Get_Actual_Subtype (Exp);
1666 if Act_ST = Etype (Exp) then
1667 return;
1668 else
1669 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
1670 Analyze_And_Resolve (Exp, Act_ST);
1671 end if;
1672 end Convert_To_Actual_Subtype;
1674 -----------------------------------
1675 -- Current_Sem_Unit_Declarations --
1676 -----------------------------------
1678 function Current_Sem_Unit_Declarations return List_Id is
1679 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1680 Decls : List_Id;
1682 begin
1683 -- If the current unit is a package body, locate the visible
1684 -- declarations of the package spec.
1686 if Nkind (U) = N_Package_Body then
1687 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1688 end if;
1690 if Nkind (U) = N_Package_Declaration then
1691 U := Specification (U);
1692 Decls := Visible_Declarations (U);
1694 if No (Decls) then
1695 Decls := New_List;
1696 Set_Visible_Declarations (U, Decls);
1697 end if;
1699 else
1700 Decls := Declarations (U);
1702 if No (Decls) then
1703 Decls := New_List;
1704 Set_Declarations (U, Decls);
1705 end if;
1706 end if;
1708 return Decls;
1709 end Current_Sem_Unit_Declarations;
1711 -----------------------
1712 -- Duplicate_Subexpr --
1713 -----------------------
1715 function Duplicate_Subexpr
1716 (Exp : Node_Id;
1717 Name_Req : Boolean := False) return Node_Id
1719 begin
1720 Remove_Side_Effects (Exp, Name_Req);
1721 return New_Copy_Tree (Exp);
1722 end Duplicate_Subexpr;
1724 ---------------------------------
1725 -- Duplicate_Subexpr_No_Checks --
1726 ---------------------------------
1728 function Duplicate_Subexpr_No_Checks
1729 (Exp : Node_Id;
1730 Name_Req : Boolean := False) return Node_Id
1732 New_Exp : Node_Id;
1733 begin
1734 Remove_Side_Effects (Exp, Name_Req);
1735 New_Exp := New_Copy_Tree (Exp);
1736 Remove_Checks (New_Exp);
1737 return New_Exp;
1738 end Duplicate_Subexpr_No_Checks;
1740 -----------------------------------
1741 -- Duplicate_Subexpr_Move_Checks --
1742 -----------------------------------
1744 function Duplicate_Subexpr_Move_Checks
1745 (Exp : Node_Id;
1746 Name_Req : Boolean := False) return Node_Id
1748 New_Exp : Node_Id;
1749 begin
1750 Remove_Side_Effects (Exp, Name_Req);
1751 New_Exp := New_Copy_Tree (Exp);
1752 Remove_Checks (Exp);
1753 return New_Exp;
1754 end Duplicate_Subexpr_Move_Checks;
1756 --------------------
1757 -- Ensure_Defined --
1758 --------------------
1760 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1761 IR : Node_Id;
1763 begin
1764 -- An itype reference must only be created if this is a local itype, so
1765 -- that gigi can elaborate it on the proper objstack.
1767 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
1768 IR := Make_Itype_Reference (Sloc (N));
1769 Set_Itype (IR, Typ);
1770 Insert_Action (N, IR);
1771 end if;
1772 end Ensure_Defined;
1774 --------------------
1775 -- Entry_Names_OK --
1776 --------------------
1778 function Entry_Names_OK return Boolean is
1779 begin
1780 return
1781 not Restricted_Profile
1782 and then not Global_Discard_Names
1783 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1784 and then not Restriction_Active (No_Local_Allocators);
1785 end Entry_Names_OK;
1787 -------------------
1788 -- Evaluate_Name --
1789 -------------------
1791 procedure Evaluate_Name (Nam : Node_Id) is
1792 K : constant Node_Kind := Nkind (Nam);
1794 begin
1795 -- For an explicit dereference, we simply force the evaluation of the
1796 -- name expression. The dereference provides a value that is the address
1797 -- for the renamed object, and it is precisely this value that we want
1798 -- to preserve.
1800 if K = N_Explicit_Dereference then
1801 Force_Evaluation (Prefix (Nam));
1803 -- For a selected component, we simply evaluate the prefix
1805 elsif K = N_Selected_Component then
1806 Evaluate_Name (Prefix (Nam));
1808 -- For an indexed component, or an attribute reference, we evaluate the
1809 -- prefix, which is itself a name, recursively, and then force the
1810 -- evaluation of all the subscripts (or attribute expressions).
1812 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
1813 Evaluate_Name (Prefix (Nam));
1815 declare
1816 E : Node_Id;
1818 begin
1819 E := First (Expressions (Nam));
1820 while Present (E) loop
1821 Force_Evaluation (E);
1823 if Original_Node (E) /= E then
1824 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
1825 end if;
1827 Next (E);
1828 end loop;
1829 end;
1831 -- For a slice, we evaluate the prefix, as for the indexed component
1832 -- case and then, if there is a range present, either directly or as the
1833 -- constraint of a discrete subtype indication, we evaluate the two
1834 -- bounds of this range.
1836 elsif K = N_Slice then
1837 Evaluate_Name (Prefix (Nam));
1839 declare
1840 DR : constant Node_Id := Discrete_Range (Nam);
1841 Constr : Node_Id;
1842 Rexpr : Node_Id;
1844 begin
1845 if Nkind (DR) = N_Range then
1846 Force_Evaluation (Low_Bound (DR));
1847 Force_Evaluation (High_Bound (DR));
1849 elsif Nkind (DR) = N_Subtype_Indication then
1850 Constr := Constraint (DR);
1852 if Nkind (Constr) = N_Range_Constraint then
1853 Rexpr := Range_Expression (Constr);
1855 Force_Evaluation (Low_Bound (Rexpr));
1856 Force_Evaluation (High_Bound (Rexpr));
1857 end if;
1858 end if;
1859 end;
1861 -- For a type conversion, the expression of the conversion must be the
1862 -- name of an object, and we simply need to evaluate this name.
1864 elsif K = N_Type_Conversion then
1865 Evaluate_Name (Expression (Nam));
1867 -- For a function call, we evaluate the call
1869 elsif K = N_Function_Call then
1870 Force_Evaluation (Nam);
1872 -- The remaining cases are direct name, operator symbol and character
1873 -- literal. In all these cases, we do nothing, since we want to
1874 -- reevaluate each time the renamed object is used.
1876 else
1877 return;
1878 end if;
1879 end Evaluate_Name;
1881 ---------------------
1882 -- Evolve_And_Then --
1883 ---------------------
1885 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1886 begin
1887 if No (Cond) then
1888 Cond := Cond1;
1889 else
1890 Cond :=
1891 Make_And_Then (Sloc (Cond1),
1892 Left_Opnd => Cond,
1893 Right_Opnd => Cond1);
1894 end if;
1895 end Evolve_And_Then;
1897 --------------------
1898 -- Evolve_Or_Else --
1899 --------------------
1901 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1902 begin
1903 if No (Cond) then
1904 Cond := Cond1;
1905 else
1906 Cond :=
1907 Make_Or_Else (Sloc (Cond1),
1908 Left_Opnd => Cond,
1909 Right_Opnd => Cond1);
1910 end if;
1911 end Evolve_Or_Else;
1913 -----------------------------------------
1914 -- Expand_Static_Predicates_In_Choices --
1915 -----------------------------------------
1917 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
1918 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
1920 Choices : constant List_Id := Discrete_Choices (N);
1922 Choice : Node_Id;
1923 Next_C : Node_Id;
1924 P : Node_Id;
1925 C : Node_Id;
1927 begin
1928 Choice := First (Choices);
1929 while Present (Choice) loop
1930 Next_C := Next (Choice);
1932 -- Check for name of subtype with static predicate
1934 if Is_Entity_Name (Choice)
1935 and then Is_Type (Entity (Choice))
1936 and then Has_Predicates (Entity (Choice))
1937 then
1938 -- Loop through entries in predicate list, converting to choices
1939 -- and inserting in the list before the current choice. Note that
1940 -- if the list is empty, corresponding to a False predicate, then
1941 -- no choices are inserted.
1943 P := First (Static_Predicate (Entity (Choice)));
1944 while Present (P) loop
1946 -- If low bound and high bounds are equal, copy simple choice
1948 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
1949 C := New_Copy (Low_Bound (P));
1951 -- Otherwise copy a range
1953 else
1954 C := New_Copy (P);
1955 end if;
1957 -- Change Sloc to referencing choice (rather than the Sloc of
1958 -- the predicate declaration element itself).
1960 Set_Sloc (C, Sloc (Choice));
1961 Insert_Before (Choice, C);
1962 Next (P);
1963 end loop;
1965 -- Delete the predicated entry
1967 Remove (Choice);
1968 end if;
1970 -- Move to next choice to check
1972 Choice := Next_C;
1973 end loop;
1974 end Expand_Static_Predicates_In_Choices;
1976 ------------------------------
1977 -- Expand_Subtype_From_Expr --
1978 ------------------------------
1980 -- This function is applicable for both static and dynamic allocation of
1981 -- objects which are constrained by an initial expression. Basically it
1982 -- transforms an unconstrained subtype indication into a constrained one.
1984 -- The expression may also be transformed in certain cases in order to
1985 -- avoid multiple evaluation. In the static allocation case, the general
1986 -- scheme is:
1988 -- Val : T := Expr;
1990 -- is transformed into
1992 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1994 -- Here are the main cases :
1996 -- <if Expr is a Slice>
1997 -- Val : T ([Index_Subtype (Expr)]) := Expr;
1999 -- <elsif Expr is a String Literal>
2000 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
2002 -- <elsif Expr is Constrained>
2003 -- subtype T is Type_Of_Expr
2004 -- Val : T := Expr;
2006 -- <elsif Expr is an entity_name>
2007 -- Val : T (constraints taken from Expr) := Expr;
2009 -- <else>
2010 -- type Axxx is access all T;
2011 -- Rval : Axxx := Expr'ref;
2012 -- Val : T (constraints taken from Rval) := Rval.all;
2014 -- ??? note: when the Expression is allocated in the secondary stack
2015 -- we could use it directly instead of copying it by declaring
2016 -- Val : T (...) renames Rval.all
2018 procedure Expand_Subtype_From_Expr
2019 (N : Node_Id;
2020 Unc_Type : Entity_Id;
2021 Subtype_Indic : Node_Id;
2022 Exp : Node_Id)
2024 Loc : constant Source_Ptr := Sloc (N);
2025 Exp_Typ : constant Entity_Id := Etype (Exp);
2026 T : Entity_Id;
2028 begin
2029 -- In general we cannot build the subtype if expansion is disabled,
2030 -- because internal entities may not have been defined. However, to
2031 -- avoid some cascaded errors, we try to continue when the expression is
2032 -- an array (or string), because it is safe to compute the bounds. It is
2033 -- in fact required to do so even in a generic context, because there
2034 -- may be constants that depend on the bounds of a string literal, both
2035 -- standard string types and more generally arrays of characters.
2037 if not Expander_Active
2038 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
2039 then
2040 return;
2041 end if;
2043 if Nkind (Exp) = N_Slice then
2044 declare
2045 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
2047 begin
2048 Rewrite (Subtype_Indic,
2049 Make_Subtype_Indication (Loc,
2050 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
2051 Constraint =>
2052 Make_Index_Or_Discriminant_Constraint (Loc,
2053 Constraints => New_List
2054 (New_Reference_To (Slice_Type, Loc)))));
2056 -- This subtype indication may be used later for constraint checks
2057 -- we better make sure that if a variable was used as a bound of
2058 -- of the original slice, its value is frozen.
2060 Force_Evaluation (Low_Bound (Scalar_Range (Slice_Type)));
2061 Force_Evaluation (High_Bound (Scalar_Range (Slice_Type)));
2062 end;
2064 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2065 Rewrite (Subtype_Indic,
2066 Make_Subtype_Indication (Loc,
2067 Subtype_Mark => New_Reference_To (Unc_Type, Loc),
2068 Constraint =>
2069 Make_Index_Or_Discriminant_Constraint (Loc,
2070 Constraints => New_List (
2071 Make_Literal_Range (Loc,
2072 Literal_Typ => Exp_Typ)))));
2074 -- If the type of the expression is an internally generated type it
2075 -- may not be necessary to create a new subtype. However there are two
2076 -- exceptions: references to the current instances, and aliased array
2077 -- object declarations for which the backend needs to create a template.
2079 elsif Is_Constrained (Exp_Typ)
2080 and then not Is_Class_Wide_Type (Unc_Type)
2081 and then
2082 (Nkind (N) /= N_Object_Declaration
2083 or else not Is_Entity_Name (Expression (N))
2084 or else not Comes_From_Source (Entity (Expression (N)))
2085 or else not Is_Array_Type (Exp_Typ)
2086 or else not Aliased_Present (N))
2087 then
2088 if Is_Itype (Exp_Typ) then
2090 -- Within an initialization procedure, a selected component
2091 -- denotes a component of the enclosing record, and it appears as
2092 -- an actual in a call to its own initialization procedure. If
2093 -- this component depends on the outer discriminant, we must
2094 -- generate the proper actual subtype for it.
2096 if Nkind (Exp) = N_Selected_Component
2097 and then Within_Init_Proc
2098 then
2099 declare
2100 Decl : constant Node_Id :=
2101 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2102 begin
2103 if Present (Decl) then
2104 Insert_Action (N, Decl);
2105 T := Defining_Identifier (Decl);
2106 else
2107 T := Exp_Typ;
2108 end if;
2109 end;
2111 -- No need to generate a new subtype
2113 else
2114 T := Exp_Typ;
2115 end if;
2117 else
2118 T := Make_Temporary (Loc, 'T');
2120 Insert_Action (N,
2121 Make_Subtype_Declaration (Loc,
2122 Defining_Identifier => T,
2123 Subtype_Indication => New_Reference_To (Exp_Typ, Loc)));
2125 -- This type is marked as an itype even though it has an explicit
2126 -- declaration since otherwise Is_Generic_Actual_Type can get
2127 -- set, resulting in the generation of spurious errors. (See
2128 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
2130 Set_Is_Itype (T);
2131 Set_Associated_Node_For_Itype (T, Exp);
2132 end if;
2134 Rewrite (Subtype_Indic, New_Reference_To (T, Loc));
2136 -- Nothing needs to be done for private types with unknown discriminants
2137 -- if the underlying type is not an unconstrained composite type or it
2138 -- is an unchecked union.
2140 elsif Is_Private_Type (Unc_Type)
2141 and then Has_Unknown_Discriminants (Unc_Type)
2142 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
2143 or else Is_Constrained (Underlying_Type (Unc_Type))
2144 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
2145 then
2146 null;
2148 -- Case of derived type with unknown discriminants where the parent type
2149 -- also has unknown discriminants.
2151 elsif Is_Record_Type (Unc_Type)
2152 and then not Is_Class_Wide_Type (Unc_Type)
2153 and then Has_Unknown_Discriminants (Unc_Type)
2154 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2155 then
2156 -- Nothing to be done if no underlying record view available
2158 if No (Underlying_Record_View (Unc_Type)) then
2159 null;
2161 -- Otherwise use the Underlying_Record_View to create the proper
2162 -- constrained subtype for an object of a derived type with unknown
2163 -- discriminants.
2165 else
2166 Remove_Side_Effects (Exp);
2167 Rewrite (Subtype_Indic,
2168 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2169 end if;
2171 -- Renamings of class-wide interface types require no equivalent
2172 -- constrained type declarations because we only need to reference
2173 -- the tag component associated with the interface. The same is
2174 -- presumably true for class-wide types in general, so this test
2175 -- is broadened to include all class-wide renamings, which also
2176 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2177 -- (Is this really correct, or are there some cases of class-wide
2178 -- renamings that require action in this procedure???)
2180 elsif Present (N)
2181 and then Nkind (N) = N_Object_Renaming_Declaration
2182 and then Is_Class_Wide_Type (Unc_Type)
2183 then
2184 null;
2186 -- In Ada 95 nothing to be done if the type of the expression is limited
2187 -- because in this case the expression cannot be copied, and its use can
2188 -- only be by reference.
2190 -- In Ada 2005 the context can be an object declaration whose expression
2191 -- is a function that returns in place. If the nominal subtype has
2192 -- unknown discriminants, the call still provides constraints on the
2193 -- object, and we have to create an actual subtype from it.
2195 -- If the type is class-wide, the expression is dynamically tagged and
2196 -- we do not create an actual subtype either. Ditto for an interface.
2197 -- For now this applies only if the type is immutably limited, and the
2198 -- function being called is build-in-place. This will have to be revised
2199 -- when build-in-place functions are generalized to other types.
2201 elsif Is_Limited_View (Exp_Typ)
2202 and then
2203 (Is_Class_Wide_Type (Exp_Typ)
2204 or else Is_Interface (Exp_Typ)
2205 or else not Has_Unknown_Discriminants (Exp_Typ)
2206 or else not Is_Composite_Type (Unc_Type))
2207 then
2208 null;
2210 -- For limited objects initialized with build in place function calls,
2211 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2212 -- node in the expression initializing the object, which breaks the
2213 -- circuitry that detects and adds the additional arguments to the
2214 -- called function.
2216 elsif Is_Build_In_Place_Function_Call (Exp) then
2217 null;
2219 else
2220 Remove_Side_Effects (Exp);
2221 Rewrite (Subtype_Indic,
2222 Make_Subtype_From_Expr (Exp, Unc_Type));
2223 end if;
2224 end Expand_Subtype_From_Expr;
2226 ------------------------
2227 -- Find_Interface_ADT --
2228 ------------------------
2230 function Find_Interface_ADT
2231 (T : Entity_Id;
2232 Iface : Entity_Id) return Elmt_Id
2234 ADT : Elmt_Id;
2235 Typ : Entity_Id := T;
2237 begin
2238 pragma Assert (Is_Interface (Iface));
2240 -- Handle private types
2242 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2243 Typ := Full_View (Typ);
2244 end if;
2246 -- Handle access types
2248 if Is_Access_Type (Typ) then
2249 Typ := Designated_Type (Typ);
2250 end if;
2252 -- Handle task and protected types implementing interfaces
2254 if Is_Concurrent_Type (Typ) then
2255 Typ := Corresponding_Record_Type (Typ);
2256 end if;
2258 pragma Assert
2259 (not Is_Class_Wide_Type (Typ)
2260 and then Ekind (Typ) /= E_Incomplete_Type);
2262 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2263 return First_Elmt (Access_Disp_Table (Typ));
2265 else
2266 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
2267 while Present (ADT)
2268 and then Present (Related_Type (Node (ADT)))
2269 and then Related_Type (Node (ADT)) /= Iface
2270 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2271 Use_Full_View => True)
2272 loop
2273 Next_Elmt (ADT);
2274 end loop;
2276 pragma Assert (Present (Related_Type (Node (ADT))));
2277 return ADT;
2278 end if;
2279 end Find_Interface_ADT;
2281 ------------------------
2282 -- Find_Interface_Tag --
2283 ------------------------
2285 function Find_Interface_Tag
2286 (T : Entity_Id;
2287 Iface : Entity_Id) return Entity_Id
2289 AI_Tag : Entity_Id;
2290 Found : Boolean := False;
2291 Typ : Entity_Id := T;
2293 procedure Find_Tag (Typ : Entity_Id);
2294 -- Internal subprogram used to recursively climb to the ancestors
2296 --------------
2297 -- Find_Tag --
2298 --------------
2300 procedure Find_Tag (Typ : Entity_Id) is
2301 AI_Elmt : Elmt_Id;
2302 AI : Node_Id;
2304 begin
2305 -- This routine does not handle the case in which the interface is an
2306 -- ancestor of Typ. That case is handled by the enclosing subprogram.
2308 pragma Assert (Typ /= Iface);
2310 -- Climb to the root type handling private types
2312 if Present (Full_View (Etype (Typ))) then
2313 if Full_View (Etype (Typ)) /= Typ then
2314 Find_Tag (Full_View (Etype (Typ)));
2315 end if;
2317 elsif Etype (Typ) /= Typ then
2318 Find_Tag (Etype (Typ));
2319 end if;
2321 -- Traverse the list of interfaces implemented by the type
2323 if not Found
2324 and then Present (Interfaces (Typ))
2325 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
2326 then
2327 -- Skip the tag associated with the primary table
2329 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2330 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2331 pragma Assert (Present (AI_Tag));
2333 AI_Elmt := First_Elmt (Interfaces (Typ));
2334 while Present (AI_Elmt) loop
2335 AI := Node (AI_Elmt);
2337 if AI = Iface
2338 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2339 then
2340 Found := True;
2341 return;
2342 end if;
2344 AI_Tag := Next_Tag_Component (AI_Tag);
2345 Next_Elmt (AI_Elmt);
2346 end loop;
2347 end if;
2348 end Find_Tag;
2350 -- Start of processing for Find_Interface_Tag
2352 begin
2353 pragma Assert (Is_Interface (Iface));
2355 -- Handle access types
2357 if Is_Access_Type (Typ) then
2358 Typ := Designated_Type (Typ);
2359 end if;
2361 -- Handle class-wide types
2363 if Is_Class_Wide_Type (Typ) then
2364 Typ := Root_Type (Typ);
2365 end if;
2367 -- Handle private types
2369 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2370 Typ := Full_View (Typ);
2371 end if;
2373 -- Handle entities from the limited view
2375 if Ekind (Typ) = E_Incomplete_Type then
2376 pragma Assert (Present (Non_Limited_View (Typ)));
2377 Typ := Non_Limited_View (Typ);
2378 end if;
2380 -- Handle task and protected types implementing interfaces
2382 if Is_Concurrent_Type (Typ) then
2383 Typ := Corresponding_Record_Type (Typ);
2384 end if;
2386 -- If the interface is an ancestor of the type, then it shared the
2387 -- primary dispatch table.
2389 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2390 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2391 return First_Tag_Component (Typ);
2393 -- Otherwise we need to search for its associated tag component
2395 else
2396 Find_Tag (Typ);
2397 pragma Assert (Found);
2398 return AI_Tag;
2399 end if;
2400 end Find_Interface_Tag;
2402 ------------------
2403 -- Find_Prim_Op --
2404 ------------------
2406 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
2407 Prim : Elmt_Id;
2408 Typ : Entity_Id := T;
2409 Op : Entity_Id;
2411 begin
2412 if Is_Class_Wide_Type (Typ) then
2413 Typ := Root_Type (Typ);
2414 end if;
2416 Typ := Underlying_Type (Typ);
2418 -- Loop through primitive operations
2420 Prim := First_Elmt (Primitive_Operations (Typ));
2421 while Present (Prim) loop
2422 Op := Node (Prim);
2424 -- We can retrieve primitive operations by name if it is an internal
2425 -- name. For equality we must check that both of its operands have
2426 -- the same type, to avoid confusion with user-defined equalities
2427 -- than may have a non-symmetric signature.
2429 exit when Chars (Op) = Name
2430 and then
2431 (Name /= Name_Op_Eq
2432 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
2434 Next_Elmt (Prim);
2436 -- Raise Program_Error if no primitive found
2438 if No (Prim) then
2439 raise Program_Error;
2440 end if;
2441 end loop;
2443 return Node (Prim);
2444 end Find_Prim_Op;
2446 ------------------
2447 -- Find_Prim_Op --
2448 ------------------
2450 function Find_Prim_Op
2451 (T : Entity_Id;
2452 Name : TSS_Name_Type) return Entity_Id
2454 Inher_Op : Entity_Id := Empty;
2455 Own_Op : Entity_Id := Empty;
2456 Prim_Elmt : Elmt_Id;
2457 Prim_Id : Entity_Id;
2458 Typ : Entity_Id := T;
2460 begin
2461 if Is_Class_Wide_Type (Typ) then
2462 Typ := Root_Type (Typ);
2463 end if;
2465 Typ := Underlying_Type (Typ);
2467 -- This search is based on the assertion that the dispatching version
2468 -- of the TSS routine always precedes the real primitive.
2470 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2471 while Present (Prim_Elmt) loop
2472 Prim_Id := Node (Prim_Elmt);
2474 if Is_TSS (Prim_Id, Name) then
2475 if Present (Alias (Prim_Id)) then
2476 Inher_Op := Prim_Id;
2477 else
2478 Own_Op := Prim_Id;
2479 end if;
2480 end if;
2482 Next_Elmt (Prim_Elmt);
2483 end loop;
2485 if Present (Own_Op) then
2486 return Own_Op;
2487 elsif Present (Inher_Op) then
2488 return Inher_Op;
2489 else
2490 raise Program_Error;
2491 end if;
2492 end Find_Prim_Op;
2494 ----------------------------
2495 -- Find_Protection_Object --
2496 ----------------------------
2498 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2499 S : Entity_Id;
2501 begin
2502 S := Scop;
2503 while Present (S) loop
2504 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
2505 and then Present (Protection_Object (S))
2506 then
2507 return Protection_Object (S);
2508 end if;
2510 S := Scope (S);
2511 end loop;
2513 -- If we do not find a Protection object in the scope chain, then
2514 -- something has gone wrong, most likely the object was never created.
2516 raise Program_Error;
2517 end Find_Protection_Object;
2519 --------------------------
2520 -- Find_Protection_Type --
2521 --------------------------
2523 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2524 Comp : Entity_Id;
2525 Typ : Entity_Id := Conc_Typ;
2527 begin
2528 if Is_Concurrent_Type (Typ) then
2529 Typ := Corresponding_Record_Type (Typ);
2530 end if;
2532 -- Since restriction violations are not considered serious errors, the
2533 -- expander remains active, but may leave the corresponding record type
2534 -- malformed. In such cases, component _object is not available so do
2535 -- not look for it.
2537 if not Analyzed (Typ) then
2538 return Empty;
2539 end if;
2541 Comp := First_Component (Typ);
2542 while Present (Comp) loop
2543 if Chars (Comp) = Name_uObject then
2544 return Base_Type (Etype (Comp));
2545 end if;
2547 Next_Component (Comp);
2548 end loop;
2550 -- The corresponding record of a protected type should always have an
2551 -- _object field.
2553 raise Program_Error;
2554 end Find_Protection_Type;
2556 ----------------------
2557 -- Force_Evaluation --
2558 ----------------------
2560 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
2561 begin
2562 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
2563 end Force_Evaluation;
2565 ---------------------------------
2566 -- Fully_Qualified_Name_String --
2567 ---------------------------------
2569 function Fully_Qualified_Name_String
2570 (E : Entity_Id;
2571 Append_NUL : Boolean := True) return String_Id
2573 procedure Internal_Full_Qualified_Name (E : Entity_Id);
2574 -- Compute recursively the qualified name without NUL at the end, adding
2575 -- it to the currently started string being generated
2577 ----------------------------------
2578 -- Internal_Full_Qualified_Name --
2579 ----------------------------------
2581 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
2582 Ent : Entity_Id;
2584 begin
2585 -- Deal properly with child units
2587 if Nkind (E) = N_Defining_Program_Unit_Name then
2588 Ent := Defining_Identifier (E);
2589 else
2590 Ent := E;
2591 end if;
2593 -- Compute qualification recursively (only "Standard" has no scope)
2595 if Present (Scope (Scope (Ent))) then
2596 Internal_Full_Qualified_Name (Scope (Ent));
2597 Store_String_Char (Get_Char_Code ('.'));
2598 end if;
2600 -- Every entity should have a name except some expanded blocks
2601 -- don't bother about those.
2603 if Chars (Ent) = No_Name then
2604 return;
2605 end if;
2607 -- Generates the entity name in upper case
2609 Get_Decoded_Name_String (Chars (Ent));
2610 Set_All_Upper_Case;
2611 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2612 return;
2613 end Internal_Full_Qualified_Name;
2615 -- Start of processing for Full_Qualified_Name
2617 begin
2618 Start_String;
2619 Internal_Full_Qualified_Name (E);
2621 if Append_NUL then
2622 Store_String_Char (Get_Char_Code (ASCII.NUL));
2623 end if;
2625 return End_String;
2626 end Fully_Qualified_Name_String;
2628 ------------------------
2629 -- Generate_Poll_Call --
2630 ------------------------
2632 procedure Generate_Poll_Call (N : Node_Id) is
2633 begin
2634 -- No poll call if polling not active
2636 if not Polling_Required then
2637 return;
2639 -- Otherwise generate require poll call
2641 else
2642 Insert_Before_And_Analyze (N,
2643 Make_Procedure_Call_Statement (Sloc (N),
2644 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
2645 end if;
2646 end Generate_Poll_Call;
2648 ---------------------------------
2649 -- Get_Current_Value_Condition --
2650 ---------------------------------
2652 -- Note: the implementation of this procedure is very closely tied to the
2653 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
2654 -- interpret Current_Value fields set by the Set procedure, so the two
2655 -- procedures need to be closely coordinated.
2657 procedure Get_Current_Value_Condition
2658 (Var : Node_Id;
2659 Op : out Node_Kind;
2660 Val : out Node_Id)
2662 Loc : constant Source_Ptr := Sloc (Var);
2663 Ent : constant Entity_Id := Entity (Var);
2665 procedure Process_Current_Value_Condition
2666 (N : Node_Id;
2667 S : Boolean);
2668 -- N is an expression which holds either True (S = True) or False (S =
2669 -- False) in the condition. This procedure digs out the expression and
2670 -- if it refers to Ent, sets Op and Val appropriately.
2672 -------------------------------------
2673 -- Process_Current_Value_Condition --
2674 -------------------------------------
2676 procedure Process_Current_Value_Condition
2677 (N : Node_Id;
2678 S : Boolean)
2680 Cond : Node_Id;
2681 Prev_Cond : Node_Id;
2682 Sens : Boolean;
2684 begin
2685 Cond := N;
2686 Sens := S;
2688 loop
2689 Prev_Cond := Cond;
2691 -- Deal with NOT operators, inverting sense
2693 while Nkind (Cond) = N_Op_Not loop
2694 Cond := Right_Opnd (Cond);
2695 Sens := not Sens;
2696 end loop;
2698 -- Deal with conversions, qualifications, and expressions with
2699 -- actions.
2701 while Nkind_In (Cond,
2702 N_Type_Conversion,
2703 N_Qualified_Expression,
2704 N_Expression_With_Actions)
2705 loop
2706 Cond := Expression (Cond);
2707 end loop;
2709 exit when Cond = Prev_Cond;
2710 end loop;
2712 -- Deal with AND THEN and AND cases
2714 if Nkind_In (Cond, N_And_Then, N_Op_And) then
2716 -- Don't ever try to invert a condition that is of the form of an
2717 -- AND or AND THEN (since we are not doing sufficiently general
2718 -- processing to allow this).
2720 if Sens = False then
2721 Op := N_Empty;
2722 Val := Empty;
2723 return;
2724 end if;
2726 -- Recursively process AND and AND THEN branches
2728 Process_Current_Value_Condition (Left_Opnd (Cond), True);
2730 if Op /= N_Empty then
2731 return;
2732 end if;
2734 Process_Current_Value_Condition (Right_Opnd (Cond), True);
2735 return;
2737 -- Case of relational operator
2739 elsif Nkind (Cond) in N_Op_Compare then
2740 Op := Nkind (Cond);
2742 -- Invert sense of test if inverted test
2744 if Sens = False then
2745 case Op is
2746 when N_Op_Eq => Op := N_Op_Ne;
2747 when N_Op_Ne => Op := N_Op_Eq;
2748 when N_Op_Lt => Op := N_Op_Ge;
2749 when N_Op_Gt => Op := N_Op_Le;
2750 when N_Op_Le => Op := N_Op_Gt;
2751 when N_Op_Ge => Op := N_Op_Lt;
2752 when others => raise Program_Error;
2753 end case;
2754 end if;
2756 -- Case of entity op value
2758 if Is_Entity_Name (Left_Opnd (Cond))
2759 and then Ent = Entity (Left_Opnd (Cond))
2760 and then Compile_Time_Known_Value (Right_Opnd (Cond))
2761 then
2762 Val := Right_Opnd (Cond);
2764 -- Case of value op entity
2766 elsif Is_Entity_Name (Right_Opnd (Cond))
2767 and then Ent = Entity (Right_Opnd (Cond))
2768 and then Compile_Time_Known_Value (Left_Opnd (Cond))
2769 then
2770 Val := Left_Opnd (Cond);
2772 -- We are effectively swapping operands
2774 case Op is
2775 when N_Op_Eq => null;
2776 when N_Op_Ne => null;
2777 when N_Op_Lt => Op := N_Op_Gt;
2778 when N_Op_Gt => Op := N_Op_Lt;
2779 when N_Op_Le => Op := N_Op_Ge;
2780 when N_Op_Ge => Op := N_Op_Le;
2781 when others => raise Program_Error;
2782 end case;
2784 else
2785 Op := N_Empty;
2786 end if;
2788 return;
2790 elsif Nkind_In (Cond,
2791 N_Type_Conversion,
2792 N_Qualified_Expression,
2793 N_Expression_With_Actions)
2794 then
2795 Cond := Expression (Cond);
2797 -- Case of Boolean variable reference, return as though the
2798 -- reference had said var = True.
2800 else
2801 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
2802 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
2804 if Sens = False then
2805 Op := N_Op_Ne;
2806 else
2807 Op := N_Op_Eq;
2808 end if;
2809 end if;
2810 end if;
2811 end Process_Current_Value_Condition;
2813 -- Start of processing for Get_Current_Value_Condition
2815 begin
2816 Op := N_Empty;
2817 Val := Empty;
2819 -- Immediate return, nothing doing, if this is not an object
2821 if Ekind (Ent) not in Object_Kind then
2822 return;
2823 end if;
2825 -- Otherwise examine current value
2827 declare
2828 CV : constant Node_Id := Current_Value (Ent);
2829 Sens : Boolean;
2830 Stm : Node_Id;
2832 begin
2833 -- If statement. Condition is known true in THEN section, known False
2834 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
2836 if Nkind (CV) = N_If_Statement then
2838 -- Before start of IF statement
2840 if Loc < Sloc (CV) then
2841 return;
2843 -- After end of IF statement
2845 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
2846 return;
2847 end if;
2849 -- At this stage we know that we are within the IF statement, but
2850 -- unfortunately, the tree does not record the SLOC of the ELSE so
2851 -- we cannot use a simple SLOC comparison to distinguish between
2852 -- the then/else statements, so we have to climb the tree.
2854 declare
2855 N : Node_Id;
2857 begin
2858 N := Parent (Var);
2859 while Parent (N) /= CV loop
2860 N := Parent (N);
2862 -- If we fall off the top of the tree, then that's odd, but
2863 -- perhaps it could occur in some error situation, and the
2864 -- safest response is simply to assume that the outcome of
2865 -- the condition is unknown. No point in bombing during an
2866 -- attempt to optimize things.
2868 if No (N) then
2869 return;
2870 end if;
2871 end loop;
2873 -- Now we have N pointing to a node whose parent is the IF
2874 -- statement in question, so now we can tell if we are within
2875 -- the THEN statements.
2877 if Is_List_Member (N)
2878 and then List_Containing (N) = Then_Statements (CV)
2879 then
2880 Sens := True;
2882 -- If the variable reference does not come from source, we
2883 -- cannot reliably tell whether it appears in the else part.
2884 -- In particular, if it appears in generated code for a node
2885 -- that requires finalization, it may be attached to a list
2886 -- that has not been yet inserted into the code. For now,
2887 -- treat it as unknown.
2889 elsif not Comes_From_Source (N) then
2890 return;
2892 -- Otherwise we must be in ELSIF or ELSE part
2894 else
2895 Sens := False;
2896 end if;
2897 end;
2899 -- ELSIF part. Condition is known true within the referenced
2900 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
2901 -- and unknown before the ELSE part or after the IF statement.
2903 elsif Nkind (CV) = N_Elsif_Part then
2905 -- if the Elsif_Part had condition_actions, the elsif has been
2906 -- rewritten as a nested if, and the original elsif_part is
2907 -- detached from the tree, so there is no way to obtain useful
2908 -- information on the current value of the variable.
2909 -- Can this be improved ???
2911 if No (Parent (CV)) then
2912 return;
2913 end if;
2915 Stm := Parent (CV);
2917 -- Before start of ELSIF part
2919 if Loc < Sloc (CV) then
2920 return;
2922 -- After end of IF statement
2924 elsif Loc >= Sloc (Stm) +
2925 Text_Ptr (UI_To_Int (End_Span (Stm)))
2926 then
2927 return;
2928 end if;
2930 -- Again we lack the SLOC of the ELSE, so we need to climb the
2931 -- tree to see if we are within the ELSIF part in question.
2933 declare
2934 N : Node_Id;
2936 begin
2937 N := Parent (Var);
2938 while Parent (N) /= Stm loop
2939 N := Parent (N);
2941 -- If we fall off the top of the tree, then that's odd, but
2942 -- perhaps it could occur in some error situation, and the
2943 -- safest response is simply to assume that the outcome of
2944 -- the condition is unknown. No point in bombing during an
2945 -- attempt to optimize things.
2947 if No (N) then
2948 return;
2949 end if;
2950 end loop;
2952 -- Now we have N pointing to a node whose parent is the IF
2953 -- statement in question, so see if is the ELSIF part we want.
2954 -- the THEN statements.
2956 if N = CV then
2957 Sens := True;
2959 -- Otherwise we must be in subsequent ELSIF or ELSE part
2961 else
2962 Sens := False;
2963 end if;
2964 end;
2966 -- Iteration scheme of while loop. The condition is known to be
2967 -- true within the body of the loop.
2969 elsif Nkind (CV) = N_Iteration_Scheme then
2970 declare
2971 Loop_Stmt : constant Node_Id := Parent (CV);
2973 begin
2974 -- Before start of body of loop
2976 if Loc < Sloc (Loop_Stmt) then
2977 return;
2979 -- After end of LOOP statement
2981 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
2982 return;
2984 -- We are within the body of the loop
2986 else
2987 Sens := True;
2988 end if;
2989 end;
2991 -- All other cases of Current_Value settings
2993 else
2994 return;
2995 end if;
2997 -- If we fall through here, then we have a reportable condition, Sens
2998 -- is True if the condition is true and False if it needs inverting.
3000 Process_Current_Value_Condition (Condition (CV), Sens);
3001 end;
3002 end Get_Current_Value_Condition;
3004 ---------------------
3005 -- Get_Stream_Size --
3006 ---------------------
3008 function Get_Stream_Size (E : Entity_Id) return Uint is
3009 begin
3010 -- If we have a Stream_Size clause for this type use it
3012 if Has_Stream_Size_Clause (E) then
3013 return Static_Integer (Expression (Stream_Size_Clause (E)));
3015 -- Otherwise the Stream_Size if the size of the type
3017 else
3018 return Esize (E);
3019 end if;
3020 end Get_Stream_Size;
3022 ---------------------------
3023 -- Has_Access_Constraint --
3024 ---------------------------
3026 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3027 Disc : Entity_Id;
3028 T : constant Entity_Id := Etype (E);
3030 begin
3031 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
3032 Disc := First_Discriminant (T);
3033 while Present (Disc) loop
3034 if Is_Access_Type (Etype (Disc)) then
3035 return True;
3036 end if;
3038 Next_Discriminant (Disc);
3039 end loop;
3041 return False;
3042 else
3043 return False;
3044 end if;
3045 end Has_Access_Constraint;
3047 ----------------------------------
3048 -- Has_Following_Address_Clause --
3049 ----------------------------------
3051 -- Should this function check the private part in a package ???
3053 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
3054 Id : constant Entity_Id := Defining_Identifier (D);
3055 Decl : Node_Id;
3057 begin
3058 Decl := Next (D);
3059 while Present (Decl) loop
3060 if Nkind (Decl) = N_At_Clause
3061 and then Chars (Identifier (Decl)) = Chars (Id)
3062 then
3063 return True;
3065 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3066 and then Chars (Decl) = Name_Address
3067 and then Chars (Name (Decl)) = Chars (Id)
3068 then
3069 return True;
3070 end if;
3072 Next (Decl);
3073 end loop;
3075 return False;
3076 end Has_Following_Address_Clause;
3078 --------------------
3079 -- Homonym_Number --
3080 --------------------
3082 function Homonym_Number (Subp : Entity_Id) return Nat is
3083 Count : Nat;
3084 Hom : Entity_Id;
3086 begin
3087 Count := 1;
3088 Hom := Homonym (Subp);
3089 while Present (Hom) loop
3090 if Scope (Hom) = Scope (Subp) then
3091 Count := Count + 1;
3092 end if;
3094 Hom := Homonym (Hom);
3095 end loop;
3097 return Count;
3098 end Homonym_Number;
3100 -----------------------------------
3101 -- In_Library_Level_Package_Body --
3102 -----------------------------------
3104 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3105 begin
3106 -- First determine whether the entity appears at the library level, then
3107 -- look at the containing unit.
3109 if Is_Library_Level_Entity (Id) then
3110 declare
3111 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3113 begin
3114 return Nkind (Unit (Container)) = N_Package_Body;
3115 end;
3116 end if;
3118 return False;
3119 end In_Library_Level_Package_Body;
3121 ------------------------------
3122 -- In_Unconditional_Context --
3123 ------------------------------
3125 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3126 P : Node_Id;
3128 begin
3129 P := Node;
3130 while Present (P) loop
3131 case Nkind (P) is
3132 when N_Subprogram_Body =>
3133 return True;
3135 when N_If_Statement =>
3136 return False;
3138 when N_Loop_Statement =>
3139 return False;
3141 when N_Case_Statement =>
3142 return False;
3144 when others =>
3145 P := Parent (P);
3146 end case;
3147 end loop;
3149 return False;
3150 end In_Unconditional_Context;
3152 -------------------
3153 -- Insert_Action --
3154 -------------------
3156 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3157 begin
3158 if Present (Ins_Action) then
3159 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3160 end if;
3161 end Insert_Action;
3163 -- Version with check(s) suppressed
3165 procedure Insert_Action
3166 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3168 begin
3169 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3170 end Insert_Action;
3172 -------------------------
3173 -- Insert_Action_After --
3174 -------------------------
3176 procedure Insert_Action_After
3177 (Assoc_Node : Node_Id;
3178 Ins_Action : Node_Id)
3180 begin
3181 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3182 end Insert_Action_After;
3184 --------------------
3185 -- Insert_Actions --
3186 --------------------
3188 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3189 N : Node_Id;
3190 P : Node_Id;
3192 Wrapped_Node : Node_Id := Empty;
3194 begin
3195 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3196 return;
3197 end if;
3199 -- Ignore insert of actions from inside default expression (or other
3200 -- similar "spec expression") in the special spec-expression analyze
3201 -- mode. Any insertions at this point have no relevance, since we are
3202 -- only doing the analyze to freeze the types of any static expressions.
3203 -- See section "Handling of Default Expressions" in the spec of package
3204 -- Sem for further details.
3206 if In_Spec_Expression then
3207 return;
3208 end if;
3210 -- If the action derives from stuff inside a record, then the actions
3211 -- are attached to the current scope, to be inserted and analyzed on
3212 -- exit from the scope. The reason for this is that we may also be
3213 -- generating freeze actions at the same time, and they must eventually
3214 -- be elaborated in the correct order.
3216 if Is_Record_Type (Current_Scope)
3217 and then not Is_Frozen (Current_Scope)
3218 then
3219 if No (Scope_Stack.Table
3220 (Scope_Stack.Last).Pending_Freeze_Actions)
3221 then
3222 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3223 Ins_Actions;
3224 else
3225 Append_List
3226 (Ins_Actions,
3227 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3228 end if;
3230 return;
3231 end if;
3233 -- We now intend to climb up the tree to find the right point to
3234 -- insert the actions. We start at Assoc_Node, unless this node is a
3235 -- subexpression in which case we start with its parent. We do this for
3236 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3237 -- itself one of the special nodes like N_And_Then, then we assume that
3238 -- an initial request to insert actions for such a node does not expect
3239 -- the actions to get deposited in the node for later handling when the
3240 -- node is expanded, since clearly the node is being dealt with by the
3241 -- caller. Note that in the subexpression case, N is always the child we
3242 -- came from.
3244 -- N_Raise_xxx_Error is an annoying special case, it is a statement if
3245 -- it has type Standard_Void_Type, and a subexpression otherwise.
3246 -- otherwise. Procedure calls, and similarly procedure attribute
3247 -- references, are also statements.
3249 if Nkind (Assoc_Node) in N_Subexpr
3250 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
3251 or else Etype (Assoc_Node) /= Standard_Void_Type)
3252 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
3253 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
3254 or else
3255 not Is_Procedure_Attribute_Name
3256 (Attribute_Name (Assoc_Node)))
3257 then
3258 N := Assoc_Node;
3259 P := Parent (Assoc_Node);
3261 -- Non-subexpression case. Note that N is initially Empty in this case
3262 -- (N is only guaranteed Non-Empty in the subexpr case).
3264 else
3265 N := Empty;
3266 P := Assoc_Node;
3267 end if;
3269 -- Capture root of the transient scope
3271 if Scope_Is_Transient then
3272 Wrapped_Node := Node_To_Be_Wrapped;
3273 end if;
3275 loop
3276 pragma Assert (Present (P));
3278 -- Make sure that inserted actions stay in the transient scope
3280 if Present (Wrapped_Node) and then N = Wrapped_Node then
3281 Store_Before_Actions_In_Scope (Ins_Actions);
3282 return;
3283 end if;
3285 case Nkind (P) is
3287 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3288 -- in the Actions field of the right operand. They will be moved
3289 -- out further when the AND THEN or OR ELSE operator is expanded.
3290 -- Nothing special needs to be done for the left operand since
3291 -- in that case the actions are executed unconditionally.
3293 when N_Short_Circuit =>
3294 if N = Right_Opnd (P) then
3296 -- We are now going to either append the actions to the
3297 -- actions field of the short-circuit operation. We will
3298 -- also analyze the actions now.
3300 -- This analysis is really too early, the proper thing would
3301 -- be to just park them there now, and only analyze them if
3302 -- we find we really need them, and to it at the proper
3303 -- final insertion point. However attempting to this proved
3304 -- tricky, so for now we just kill current values before and
3305 -- after the analyze call to make sure we avoid peculiar
3306 -- optimizations from this out of order insertion.
3308 Kill_Current_Values;
3310 if Present (Actions (P)) then
3311 Insert_List_After_And_Analyze
3312 (Last (Actions (P)), Ins_Actions);
3313 else
3314 Set_Actions (P, Ins_Actions);
3315 Analyze_List (Actions (P));
3316 end if;
3318 Kill_Current_Values;
3320 return;
3321 end if;
3323 -- Then or Else dependent expression of an if expression. Add
3324 -- actions to Then_Actions or Else_Actions field as appropriate.
3325 -- The actions will be moved further out when the if is expanded.
3327 when N_If_Expression =>
3328 declare
3329 ThenX : constant Node_Id := Next (First (Expressions (P)));
3330 ElseX : constant Node_Id := Next (ThenX);
3332 begin
3333 -- If the enclosing expression is already analyzed, as
3334 -- is the case for nested elaboration checks, insert the
3335 -- conditional further out.
3337 if Analyzed (P) then
3338 null;
3340 -- Actions belong to the then expression, temporarily place
3341 -- them as Then_Actions of the if expression. They will be
3342 -- moved to the proper place later when the if expression
3343 -- is expanded.
3345 elsif N = ThenX then
3346 if Present (Then_Actions (P)) then
3347 Insert_List_After_And_Analyze
3348 (Last (Then_Actions (P)), Ins_Actions);
3349 else
3350 Set_Then_Actions (P, Ins_Actions);
3351 Analyze_List (Then_Actions (P));
3352 end if;
3354 return;
3356 -- Actions belong to the else expression, temporarily place
3357 -- them as Else_Actions of the if expression. They will be
3358 -- moved to the proper place later when the if expression
3359 -- is expanded.
3361 elsif N = ElseX then
3362 if Present (Else_Actions (P)) then
3363 Insert_List_After_And_Analyze
3364 (Last (Else_Actions (P)), Ins_Actions);
3365 else
3366 Set_Else_Actions (P, Ins_Actions);
3367 Analyze_List (Else_Actions (P));
3368 end if;
3370 return;
3372 -- Actions belong to the condition. In this case they are
3373 -- unconditionally executed, and so we can continue the
3374 -- search for the proper insert point.
3376 else
3377 null;
3378 end if;
3379 end;
3381 -- Alternative of case expression, we place the action in the
3382 -- Actions field of the case expression alternative, this will
3383 -- be handled when the case expression is expanded.
3385 when N_Case_Expression_Alternative =>
3386 if Present (Actions (P)) then
3387 Insert_List_After_And_Analyze
3388 (Last (Actions (P)), Ins_Actions);
3389 else
3390 Set_Actions (P, Ins_Actions);
3391 Analyze_List (Actions (P));
3392 end if;
3394 return;
3396 -- Case of appearing within an Expressions_With_Actions node. When
3397 -- the new actions come from the expression of the expression with
3398 -- actions, they must be added to the existing actions. The other
3399 -- alternative is when the new actions are related to one of the
3400 -- existing actions of the expression with actions. In that case
3401 -- they must be inserted further up the tree.
3403 when N_Expression_With_Actions =>
3404 if N = Expression (P) then
3405 if Is_Empty_List (Actions (P)) then
3406 Append_List_To (Actions (P), Ins_Actions);
3407 Analyze_List (Actions (P));
3408 else
3409 Insert_List_After_And_Analyze
3410 (Last (Actions (P)), Ins_Actions);
3411 end if;
3412 return;
3413 end if;
3415 -- Case of appearing in the condition of a while expression or
3416 -- elsif. We insert the actions into the Condition_Actions field.
3417 -- They will be moved further out when the while loop or elsif
3418 -- is analyzed.
3420 when N_Iteration_Scheme |
3421 N_Elsif_Part
3423 if N = Condition (P) then
3424 if Present (Condition_Actions (P)) then
3425 Insert_List_After_And_Analyze
3426 (Last (Condition_Actions (P)), Ins_Actions);
3427 else
3428 Set_Condition_Actions (P, Ins_Actions);
3430 -- Set the parent of the insert actions explicitly. This
3431 -- is not a syntactic field, but we need the parent field
3432 -- set, in particular so that freeze can understand that
3433 -- it is dealing with condition actions, and properly
3434 -- insert the freezing actions.
3436 Set_Parent (Ins_Actions, P);
3437 Analyze_List (Condition_Actions (P));
3438 end if;
3440 return;
3441 end if;
3443 -- Statements, declarations, pragmas, representation clauses
3445 when
3446 -- Statements
3448 N_Procedure_Call_Statement |
3449 N_Statement_Other_Than_Procedure_Call |
3451 -- Pragmas
3453 N_Pragma |
3455 -- Representation_Clause
3457 N_At_Clause |
3458 N_Attribute_Definition_Clause |
3459 N_Enumeration_Representation_Clause |
3460 N_Record_Representation_Clause |
3462 -- Declarations
3464 N_Abstract_Subprogram_Declaration |
3465 N_Entry_Body |
3466 N_Exception_Declaration |
3467 N_Exception_Renaming_Declaration |
3468 N_Expression_Function |
3469 N_Formal_Abstract_Subprogram_Declaration |
3470 N_Formal_Concrete_Subprogram_Declaration |
3471 N_Formal_Object_Declaration |
3472 N_Formal_Type_Declaration |
3473 N_Full_Type_Declaration |
3474 N_Function_Instantiation |
3475 N_Generic_Function_Renaming_Declaration |
3476 N_Generic_Package_Declaration |
3477 N_Generic_Package_Renaming_Declaration |
3478 N_Generic_Procedure_Renaming_Declaration |
3479 N_Generic_Subprogram_Declaration |
3480 N_Implicit_Label_Declaration |
3481 N_Incomplete_Type_Declaration |
3482 N_Number_Declaration |
3483 N_Object_Declaration |
3484 N_Object_Renaming_Declaration |
3485 N_Package_Body |
3486 N_Package_Body_Stub |
3487 N_Package_Declaration |
3488 N_Package_Instantiation |
3489 N_Package_Renaming_Declaration |
3490 N_Private_Extension_Declaration |
3491 N_Private_Type_Declaration |
3492 N_Procedure_Instantiation |
3493 N_Protected_Body |
3494 N_Protected_Body_Stub |
3495 N_Protected_Type_Declaration |
3496 N_Single_Task_Declaration |
3497 N_Subprogram_Body |
3498 N_Subprogram_Body_Stub |
3499 N_Subprogram_Declaration |
3500 N_Subprogram_Renaming_Declaration |
3501 N_Subtype_Declaration |
3502 N_Task_Body |
3503 N_Task_Body_Stub |
3504 N_Task_Type_Declaration |
3506 -- Use clauses can appear in lists of declarations
3508 N_Use_Package_Clause |
3509 N_Use_Type_Clause |
3511 -- Freeze entity behaves like a declaration or statement
3513 N_Freeze_Entity |
3514 N_Freeze_Generic_Entity
3516 -- Do not insert here if the item is not a list member (this
3517 -- happens for example with a triggering statement, and the
3518 -- proper approach is to insert before the entire select).
3520 if not Is_List_Member (P) then
3521 null;
3523 -- Do not insert if parent of P is an N_Component_Association
3524 -- node (i.e. we are in the context of an N_Aggregate or
3525 -- N_Extension_Aggregate node. In this case we want to insert
3526 -- before the entire aggregate.
3528 elsif Nkind (Parent (P)) = N_Component_Association then
3529 null;
3531 -- Do not insert if the parent of P is either an N_Variant node
3532 -- or an N_Record_Definition node, meaning in either case that
3533 -- P is a member of a component list, and that therefore the
3534 -- actions should be inserted outside the complete record
3535 -- declaration.
3537 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
3538 null;
3540 -- Do not insert freeze nodes within the loop generated for
3541 -- an aggregate, because they may be elaborated too late for
3542 -- subsequent use in the back end: within a package spec the
3543 -- loop is part of the elaboration procedure and is only
3544 -- elaborated during the second pass.
3546 -- If the loop comes from source, or the entity is local to the
3547 -- loop itself it must remain within.
3549 elsif Nkind (Parent (P)) = N_Loop_Statement
3550 and then not Comes_From_Source (Parent (P))
3551 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
3552 and then
3553 Scope (Entity (First (Ins_Actions))) /= Current_Scope
3554 then
3555 null;
3557 -- Otherwise we can go ahead and do the insertion
3559 elsif P = Wrapped_Node then
3560 Store_Before_Actions_In_Scope (Ins_Actions);
3561 return;
3563 else
3564 Insert_List_Before_And_Analyze (P, Ins_Actions);
3565 return;
3566 end if;
3568 -- A special case, N_Raise_xxx_Error can act either as a statement
3569 -- or a subexpression. We tell the difference by looking at the
3570 -- Etype. It is set to Standard_Void_Type in the statement case.
3572 when
3573 N_Raise_xxx_Error =>
3574 if Etype (P) = Standard_Void_Type then
3575 if P = Wrapped_Node then
3576 Store_Before_Actions_In_Scope (Ins_Actions);
3577 else
3578 Insert_List_Before_And_Analyze (P, Ins_Actions);
3579 end if;
3581 return;
3583 -- In the subexpression case, keep climbing
3585 else
3586 null;
3587 end if;
3589 -- If a component association appears within a loop created for
3590 -- an array aggregate, attach the actions to the association so
3591 -- they can be subsequently inserted within the loop. For other
3592 -- component associations insert outside of the aggregate. For
3593 -- an association that will generate a loop, its Loop_Actions
3594 -- attribute is already initialized (see exp_aggr.adb).
3596 -- The list of loop_actions can in turn generate additional ones,
3597 -- that are inserted before the associated node. If the associated
3598 -- node is outside the aggregate, the new actions are collected
3599 -- at the end of the loop actions, to respect the order in which
3600 -- they are to be elaborated.
3602 when
3603 N_Component_Association =>
3604 if Nkind (Parent (P)) = N_Aggregate
3605 and then Present (Loop_Actions (P))
3606 then
3607 if Is_Empty_List (Loop_Actions (P)) then
3608 Set_Loop_Actions (P, Ins_Actions);
3609 Analyze_List (Ins_Actions);
3611 else
3612 declare
3613 Decl : Node_Id;
3615 begin
3616 -- Check whether these actions were generated by a
3617 -- declaration that is part of the loop_ actions
3618 -- for the component_association.
3620 Decl := Assoc_Node;
3621 while Present (Decl) loop
3622 exit when Parent (Decl) = P
3623 and then Is_List_Member (Decl)
3624 and then
3625 List_Containing (Decl) = Loop_Actions (P);
3626 Decl := Parent (Decl);
3627 end loop;
3629 if Present (Decl) then
3630 Insert_List_Before_And_Analyze
3631 (Decl, Ins_Actions);
3632 else
3633 Insert_List_After_And_Analyze
3634 (Last (Loop_Actions (P)), Ins_Actions);
3635 end if;
3636 end;
3637 end if;
3639 return;
3641 else
3642 null;
3643 end if;
3645 -- Another special case, an attribute denoting a procedure call
3647 when
3648 N_Attribute_Reference =>
3649 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
3650 if P = Wrapped_Node then
3651 Store_Before_Actions_In_Scope (Ins_Actions);
3652 else
3653 Insert_List_Before_And_Analyze (P, Ins_Actions);
3654 end if;
3656 return;
3658 -- In the subexpression case, keep climbing
3660 else
3661 null;
3662 end if;
3664 -- A contract node should not belong to the tree
3666 when N_Contract =>
3667 raise Program_Error;
3669 -- For all other node types, keep climbing tree
3671 when
3672 N_Abortable_Part |
3673 N_Accept_Alternative |
3674 N_Access_Definition |
3675 N_Access_Function_Definition |
3676 N_Access_Procedure_Definition |
3677 N_Access_To_Object_Definition |
3678 N_Aggregate |
3679 N_Allocator |
3680 N_Aspect_Specification |
3681 N_Case_Expression |
3682 N_Case_Statement_Alternative |
3683 N_Character_Literal |
3684 N_Compilation_Unit |
3685 N_Compilation_Unit_Aux |
3686 N_Component_Clause |
3687 N_Component_Declaration |
3688 N_Component_Definition |
3689 N_Component_List |
3690 N_Constrained_Array_Definition |
3691 N_Decimal_Fixed_Point_Definition |
3692 N_Defining_Character_Literal |
3693 N_Defining_Identifier |
3694 N_Defining_Operator_Symbol |
3695 N_Defining_Program_Unit_Name |
3696 N_Delay_Alternative |
3697 N_Delta_Constraint |
3698 N_Derived_Type_Definition |
3699 N_Designator |
3700 N_Digits_Constraint |
3701 N_Discriminant_Association |
3702 N_Discriminant_Specification |
3703 N_Empty |
3704 N_Entry_Body_Formal_Part |
3705 N_Entry_Call_Alternative |
3706 N_Entry_Declaration |
3707 N_Entry_Index_Specification |
3708 N_Enumeration_Type_Definition |
3709 N_Error |
3710 N_Exception_Handler |
3711 N_Expanded_Name |
3712 N_Explicit_Dereference |
3713 N_Extension_Aggregate |
3714 N_Floating_Point_Definition |
3715 N_Formal_Decimal_Fixed_Point_Definition |
3716 N_Formal_Derived_Type_Definition |
3717 N_Formal_Discrete_Type_Definition |
3718 N_Formal_Floating_Point_Definition |
3719 N_Formal_Modular_Type_Definition |
3720 N_Formal_Ordinary_Fixed_Point_Definition |
3721 N_Formal_Package_Declaration |
3722 N_Formal_Private_Type_Definition |
3723 N_Formal_Incomplete_Type_Definition |
3724 N_Formal_Signed_Integer_Type_Definition |
3725 N_Function_Call |
3726 N_Function_Specification |
3727 N_Generic_Association |
3728 N_Handled_Sequence_Of_Statements |
3729 N_Identifier |
3730 N_In |
3731 N_Index_Or_Discriminant_Constraint |
3732 N_Indexed_Component |
3733 N_Integer_Literal |
3734 N_Iterator_Specification |
3735 N_Itype_Reference |
3736 N_Label |
3737 N_Loop_Parameter_Specification |
3738 N_Mod_Clause |
3739 N_Modular_Type_Definition |
3740 N_Not_In |
3741 N_Null |
3742 N_Op_Abs |
3743 N_Op_Add |
3744 N_Op_And |
3745 N_Op_Concat |
3746 N_Op_Divide |
3747 N_Op_Eq |
3748 N_Op_Expon |
3749 N_Op_Ge |
3750 N_Op_Gt |
3751 N_Op_Le |
3752 N_Op_Lt |
3753 N_Op_Minus |
3754 N_Op_Mod |
3755 N_Op_Multiply |
3756 N_Op_Ne |
3757 N_Op_Not |
3758 N_Op_Or |
3759 N_Op_Plus |
3760 N_Op_Rem |
3761 N_Op_Rotate_Left |
3762 N_Op_Rotate_Right |
3763 N_Op_Shift_Left |
3764 N_Op_Shift_Right |
3765 N_Op_Shift_Right_Arithmetic |
3766 N_Op_Subtract |
3767 N_Op_Xor |
3768 N_Operator_Symbol |
3769 N_Ordinary_Fixed_Point_Definition |
3770 N_Others_Choice |
3771 N_Package_Specification |
3772 N_Parameter_Association |
3773 N_Parameter_Specification |
3774 N_Pop_Constraint_Error_Label |
3775 N_Pop_Program_Error_Label |
3776 N_Pop_Storage_Error_Label |
3777 N_Pragma_Argument_Association |
3778 N_Procedure_Specification |
3779 N_Protected_Definition |
3780 N_Push_Constraint_Error_Label |
3781 N_Push_Program_Error_Label |
3782 N_Push_Storage_Error_Label |
3783 N_Qualified_Expression |
3784 N_Quantified_Expression |
3785 N_Raise_Expression |
3786 N_Range |
3787 N_Range_Constraint |
3788 N_Real_Literal |
3789 N_Real_Range_Specification |
3790 N_Record_Definition |
3791 N_Reference |
3792 N_SCIL_Dispatch_Table_Tag_Init |
3793 N_SCIL_Dispatching_Call |
3794 N_SCIL_Membership_Test |
3795 N_Selected_Component |
3796 N_Signed_Integer_Type_Definition |
3797 N_Single_Protected_Declaration |
3798 N_Slice |
3799 N_String_Literal |
3800 N_Subprogram_Info |
3801 N_Subtype_Indication |
3802 N_Subunit |
3803 N_Task_Definition |
3804 N_Terminate_Alternative |
3805 N_Triggering_Alternative |
3806 N_Type_Conversion |
3807 N_Unchecked_Expression |
3808 N_Unchecked_Type_Conversion |
3809 N_Unconstrained_Array_Definition |
3810 N_Unused_At_End |
3811 N_Unused_At_Start |
3812 N_Variant |
3813 N_Variant_Part |
3814 N_Validate_Unchecked_Conversion |
3815 N_With_Clause
3817 null;
3819 end case;
3821 -- If we fall through above tests, keep climbing tree
3823 N := P;
3825 if Nkind (Parent (N)) = N_Subunit then
3827 -- This is the proper body corresponding to a stub. Insertion must
3828 -- be done at the point of the stub, which is in the declarative
3829 -- part of the parent unit.
3831 P := Corresponding_Stub (Parent (N));
3833 else
3834 P := Parent (N);
3835 end if;
3836 end loop;
3837 end Insert_Actions;
3839 -- Version with check(s) suppressed
3841 procedure Insert_Actions
3842 (Assoc_Node : Node_Id;
3843 Ins_Actions : List_Id;
3844 Suppress : Check_Id)
3846 begin
3847 if Suppress = All_Checks then
3848 declare
3849 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3850 begin
3851 Scope_Suppress.Suppress := (others => True);
3852 Insert_Actions (Assoc_Node, Ins_Actions);
3853 Scope_Suppress.Suppress := Sva;
3854 end;
3856 else
3857 declare
3858 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3859 begin
3860 Scope_Suppress.Suppress (Suppress) := True;
3861 Insert_Actions (Assoc_Node, Ins_Actions);
3862 Scope_Suppress.Suppress (Suppress) := Svg;
3863 end;
3864 end if;
3865 end Insert_Actions;
3867 --------------------------
3868 -- Insert_Actions_After --
3869 --------------------------
3871 procedure Insert_Actions_After
3872 (Assoc_Node : Node_Id;
3873 Ins_Actions : List_Id)
3875 begin
3876 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
3877 Store_After_Actions_In_Scope (Ins_Actions);
3878 else
3879 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
3880 end if;
3881 end Insert_Actions_After;
3883 ---------------------------------
3884 -- Insert_Library_Level_Action --
3885 ---------------------------------
3887 procedure Insert_Library_Level_Action (N : Node_Id) is
3888 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3890 begin
3891 Push_Scope (Cunit_Entity (Main_Unit));
3892 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
3894 if No (Actions (Aux)) then
3895 Set_Actions (Aux, New_List (N));
3896 else
3897 Append (N, Actions (Aux));
3898 end if;
3900 Analyze (N);
3901 Pop_Scope;
3902 end Insert_Library_Level_Action;
3904 ----------------------------------
3905 -- Insert_Library_Level_Actions --
3906 ----------------------------------
3908 procedure Insert_Library_Level_Actions (L : List_Id) is
3909 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
3911 begin
3912 if Is_Non_Empty_List (L) then
3913 Push_Scope (Cunit_Entity (Main_Unit));
3914 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
3916 if No (Actions (Aux)) then
3917 Set_Actions (Aux, L);
3918 Analyze_List (L);
3919 else
3920 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
3921 end if;
3923 Pop_Scope;
3924 end if;
3925 end Insert_Library_Level_Actions;
3927 ----------------------
3928 -- Inside_Init_Proc --
3929 ----------------------
3931 function Inside_Init_Proc return Boolean is
3932 S : Entity_Id;
3934 begin
3935 S := Current_Scope;
3936 while Present (S) and then S /= Standard_Standard loop
3937 if Is_Init_Proc (S) then
3938 return True;
3939 else
3940 S := Scope (S);
3941 end if;
3942 end loop;
3944 return False;
3945 end Inside_Init_Proc;
3947 ----------------------------
3948 -- Is_All_Null_Statements --
3949 ----------------------------
3951 function Is_All_Null_Statements (L : List_Id) return Boolean is
3952 Stm : Node_Id;
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;
3961 Next (Stm);
3962 end loop;
3964 return True;
3965 end Is_All_Null_Statements;
3967 --------------------------------------------------
3968 -- Is_Displacement_Of_Object_Or_Function_Result --
3969 --------------------------------------------------
3971 function Is_Displacement_Of_Object_Or_Function_Result
3972 (Obj_Id : Entity_Id) return Boolean
3974 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
3975 -- Determine if particular node denotes a controlled function call
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.
3981 function Is_Source_Object (N : Node_Id) return Boolean;
3982 -- Determine whether a particular node denotes a source object
3984 ---------------------------------
3985 -- Is_Controlled_Function_Call --
3986 ---------------------------------
3988 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
3989 Expr : Node_Id := Original_Node (N);
3991 begin
3992 if Nkind (Expr) = N_Function_Call then
3993 Expr := Name (Expr);
3994 end if;
3996 -- The function call may appear in object.operation format
3998 if Nkind (Expr) = N_Selected_Component then
3999 Expr := Selector_Name (Expr);
4000 end if;
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;
4008 ----------------------
4009 -- Is_Displace_Call --
4010 ----------------------
4012 function Is_Displace_Call (N : Node_Id) return Boolean is
4013 Call : Node_Id := N;
4015 begin
4016 -- Strip various actions which may precede a call to Displace
4018 loop
4019 if Nkind (Call) = N_Explicit_Dereference then
4020 Call := Prefix (Call);
4022 elsif Nkind_In (Call, N_Type_Conversion,
4023 N_Unchecked_Type_Conversion)
4024 then
4025 Call := Expression (Call);
4027 else
4028 exit;
4029 end if;
4030 end loop;
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;
4038 ----------------------
4039 -- Is_Source_Object --
4040 ----------------------
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;
4051 -- Local variables
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);
4057 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
4059 begin
4060 -- Case 1:
4062 -- Obj : CW_Type := Function_Call (...);
4064 -- rewritten into:
4066 -- Tmp : ... := Function_Call (...)'reference;
4067 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
4069 -- where the return type of the function and the class-wide type require
4070 -- dispatch table pointer displacement.
4072 -- Case 2:
4074 -- Obj : CW_Type := Src_Obj;
4076 -- rewritten into:
4078 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
4080 -- where the type of the source object and the class-wide type require
4081 -- dispatch table pointer displacement.
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;
4094 ------------------------------
4095 -- Is_Finalizable_Transient --
4096 ------------------------------
4098 function Is_Finalizable_Transient
4099 (Decl : Node_Id;
4100 Rel_Node : Node_Id) return Boolean
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;
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.
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.
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.
4125 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4126 -- Determine whether transient object Trans_Id is allocated on the heap
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.
4135 ---------------------------
4136 -- Initialized_By_Access --
4137 ---------------------------
4139 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4140 Expr : constant Node_Id := Expression (Parent (Trans_Id));
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;
4149 ------------------------------------------
4150 -- Initialized_By_Aliased_BIP_Func_Call --
4151 ------------------------------------------
4153 function Initialized_By_Aliased_BIP_Func_Call
4154 (Trans_Id : Entity_Id) return Boolean
4156 Call : Node_Id := Expression (Parent (Trans_Id));
4158 begin
4159 -- Build-in-place calls usually appear in 'reference format
4161 if Nkind (Call) = N_Reference then
4162 Call := Prefix (Call);
4163 end if;
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;
4176 begin
4177 -- Examine all parameter associations of the function call
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);
4187 -- Construct the names of formals BIPaccess and BIPalloc
4188 -- using the function name retrieved from an arbitrary
4189 -- formal.
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));
4197 Access_Nam :=
4198 New_External_Name (Chars (Func_Id),
4199 BIP_Formal_Suffix (BIP_Object_Access));
4201 Alloc_Nam :=
4202 New_External_Name (Chars (Func_Id),
4203 BIP_Formal_Suffix (BIP_Alloc_Form));
4204 end if;
4206 -- A match for BIPaccess => Temp has been found
4208 if Chars (Formal) = Access_Nam
4209 and then Nkind (Actual) /= N_Null
4210 then
4211 Access_OK := True;
4212 end if;
4214 -- A match for BIPalloc => 1 has been found
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;
4224 Next (Param);
4225 end loop;
4227 return Access_OK and Alloc_OK;
4228 end;
4229 end if;
4231 return False;
4232 end Initialized_By_Aliased_BIP_Func_Call;
4234 ----------------
4235 -- Is_Aliased --
4236 ----------------
4238 function Is_Aliased
4239 (Trans_Id : Entity_Id;
4240 First_Stmt : Node_Id) return Boolean
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.
4247 -------------------------
4248 -- Find_Renamed_Object --
4249 -------------------------
4251 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
4252 Ren_Obj : Node_Id := Empty;
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.
4258 -----------------
4259 -- Find_Object --
4260 -----------------
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.
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;
4275 return OK;
4276 end Find_Object;
4278 procedure Search is new Traverse_Proc (Find_Object);
4280 -- Local variables
4282 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
4284 -- Start of processing for Find_Renamed_Object
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.
4291 if not Is_RTE (Typ, RE_Tag_Ptr) then
4292 Search (Name (Ren_Decl));
4293 end if;
4295 return Ren_Obj;
4296 end Find_Renamed_Object;
4298 -- Local variables
4300 Expr : Node_Id;
4301 Ren_Obj : Entity_Id;
4302 Stmt : Node_Id;
4304 -- Start of processing for Is_Aliased
4306 begin
4307 Stmt := First_Stmt;
4308 while Present (Stmt) loop
4309 if Nkind (Stmt) = N_Object_Declaration then
4310 Expr := Expression (Stmt);
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;
4320 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
4321 Ren_Obj := Find_Renamed_Object (Stmt);
4323 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
4324 return True;
4325 end if;
4326 end if;
4328 Next (Stmt);
4329 end loop;
4331 return False;
4332 end Is_Aliased;
4334 ------------------
4335 -- Is_Allocated --
4336 ------------------
4338 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
4339 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4340 begin
4341 return
4342 Is_Access_Type (Etype (Trans_Id))
4343 and then Present (Expr)
4344 and then Nkind (Expr) = N_Allocator;
4345 end Is_Allocated;
4347 ---------------------------
4348 -- Is_Iterated_Container --
4349 ---------------------------
4351 function Is_Iterated_Container
4352 (Trans_Id : Entity_Id;
4353 First_Stmt : Node_Id) return Boolean
4355 Aspect : Node_Id;
4356 Call : Node_Id;
4357 Iter : Entity_Id;
4358 Param : Node_Id;
4359 Stmt : Node_Id;
4360 Typ : Entity_Id;
4362 begin
4363 -- It is not possible to iterate over containers in non-Ada 2012 code
4365 if Ada_Version < Ada_2012 then
4366 return False;
4367 end if;
4369 Typ := Etype (Trans_Id);
4371 -- Handle access type created for secondary stack use
4373 if Is_Access_Type (Typ) then
4374 Typ := Designated_Type (Typ);
4375 end if;
4377 -- Look for aspect Default_Iterator
4379 if Has_Aspects (Parent (Typ)) then
4380 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
4382 if Present (Aspect) then
4383 Iter := Entity (Aspect);
4385 -- Examine the statements following the container object and
4386 -- look for a call to the default iterate routine where the
4387 -- first parameter is the transient. Such a call appears as:
4389 -- It : Access_To_CW_Iterator :=
4390 -- Iterate (Tran_Id.all, ...)'reference;
4392 Stmt := First_Stmt;
4393 while Present (Stmt) loop
4395 -- Detect an object declaration which is initialized by a
4396 -- secondary stack function call.
4398 if Nkind (Stmt) = N_Object_Declaration
4399 and then Present (Expression (Stmt))
4400 and then Nkind (Expression (Stmt)) = N_Reference
4401 and then Nkind (Prefix (Expression (Stmt))) =
4402 N_Function_Call
4403 then
4404 Call := Prefix (Expression (Stmt));
4406 -- The call must invoke the default iterate routine of
4407 -- the container and the transient object must appear as
4408 -- the first actual parameter. Skip any calls whose names
4409 -- are not entities.
4411 if Is_Entity_Name (Name (Call))
4412 and then Entity (Name (Call)) = Iter
4413 and then Present (Parameter_Associations (Call))
4414 then
4415 Param := First (Parameter_Associations (Call));
4417 if Nkind (Param) = N_Explicit_Dereference
4418 and then Entity (Prefix (Param)) = Trans_Id
4419 then
4420 return True;
4421 end if;
4422 end if;
4423 end if;
4425 Next (Stmt);
4426 end loop;
4427 end if;
4428 end if;
4430 return False;
4431 end Is_Iterated_Container;
4433 -- Start of processing for Is_Finalizable_Transient
4435 begin
4436 -- Handle access types
4438 if Is_Access_Type (Desig) then
4439 Desig := Available_View (Designated_Type (Desig));
4440 end if;
4442 return
4443 Ekind_In (Obj_Id, E_Constant, E_Variable)
4444 and then Needs_Finalization (Desig)
4445 and then Requires_Transient_Scope (Desig)
4446 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
4448 -- Do not consider renamed or 'reference-d transient objects because
4449 -- the act of renaming extends the object's lifetime.
4451 and then not Is_Aliased (Obj_Id, Decl)
4453 -- Do not consider transient objects allocated on the heap since
4454 -- they are attached to a finalization master.
4456 and then not Is_Allocated (Obj_Id)
4458 -- If the transient object is a pointer, check that it is not
4459 -- initialized by a function which returns a pointer or acts as a
4460 -- renaming of another pointer.
4462 and then
4463 (not Is_Access_Type (Obj_Typ)
4464 or else not Initialized_By_Access (Obj_Id))
4466 -- Do not consider transient objects which act as indirect aliases
4467 -- of build-in-place function results.
4469 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
4471 -- Do not consider conversions of tags to class-wide types
4473 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
4475 -- Do not consider containers in the context of iterator loops. Such
4476 -- transient objects must exist for as long as the loop is around,
4477 -- otherwise any operation carried out by the iterator will fail.
4479 and then not Is_Iterated_Container (Obj_Id, Decl);
4480 end Is_Finalizable_Transient;
4482 ---------------------------------
4483 -- Is_Fully_Repped_Tagged_Type --
4484 ---------------------------------
4486 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
4487 U : constant Entity_Id := Underlying_Type (T);
4488 Comp : Entity_Id;
4490 begin
4491 if No (U) or else not Is_Tagged_Type (U) then
4492 return False;
4493 elsif Has_Discriminants (U) then
4494 return False;
4495 elsif not Has_Specified_Layout (U) then
4496 return False;
4497 end if;
4499 -- Here we have a tagged type, see if it has any unlayed out fields
4500 -- other than a possible tag and parent fields. If so, we return False.
4502 Comp := First_Component (U);
4503 while Present (Comp) loop
4504 if not Is_Tag (Comp)
4505 and then Chars (Comp) /= Name_uParent
4506 and then No (Component_Clause (Comp))
4507 then
4508 return False;
4509 else
4510 Next_Component (Comp);
4511 end if;
4512 end loop;
4514 -- All components are layed out
4516 return True;
4517 end Is_Fully_Repped_Tagged_Type;
4519 ----------------------------------
4520 -- Is_Library_Level_Tagged_Type --
4521 ----------------------------------
4523 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
4524 begin
4525 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
4526 end Is_Library_Level_Tagged_Type;
4528 --------------------------
4529 -- Is_Non_BIP_Func_Call --
4530 --------------------------
4532 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
4533 begin
4534 -- The expected call is of the format
4536 -- Func_Call'reference
4538 return
4539 Nkind (Expr) = N_Reference
4540 and then Nkind (Prefix (Expr)) = N_Function_Call
4541 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
4542 end Is_Non_BIP_Func_Call;
4544 ----------------------------------
4545 -- Is_Possibly_Unaligned_Object --
4546 ----------------------------------
4548 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
4549 T : constant Entity_Id := Etype (N);
4551 begin
4552 -- If renamed object, apply test to underlying object
4554 if Is_Entity_Name (N)
4555 and then Is_Object (Entity (N))
4556 and then Present (Renamed_Object (Entity (N)))
4557 then
4558 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
4559 end if;
4561 -- Tagged and controlled types and aliased types are always aligned, as
4562 -- are concurrent types.
4564 if Is_Aliased (T)
4565 or else Has_Controlled_Component (T)
4566 or else Is_Concurrent_Type (T)
4567 or else Is_Tagged_Type (T)
4568 or else Is_Controlled (T)
4569 then
4570 return False;
4571 end if;
4573 -- If this is an element of a packed array, may be unaligned
4575 if Is_Ref_To_Bit_Packed_Array (N) then
4576 return True;
4577 end if;
4579 -- Case of indexed component reference: test whether prefix is unaligned
4581 if Nkind (N) = N_Indexed_Component then
4582 return Is_Possibly_Unaligned_Object (Prefix (N));
4584 -- Case of selected component reference
4586 elsif Nkind (N) = N_Selected_Component then
4587 declare
4588 P : constant Node_Id := Prefix (N);
4589 C : constant Entity_Id := Entity (Selector_Name (N));
4590 M : Nat;
4591 S : Nat;
4593 begin
4594 -- If component reference is for an array with non-static bounds,
4595 -- then it is always aligned: we can only process unaligned arrays
4596 -- with static bounds (more precisely compile time known bounds).
4598 if Is_Array_Type (T)
4599 and then not Compile_Time_Known_Bounds (T)
4600 then
4601 return False;
4602 end if;
4604 -- If component is aliased, it is definitely properly aligned
4606 if Is_Aliased (C) then
4607 return False;
4608 end if;
4610 -- If component is for a type implemented as a scalar, and the
4611 -- record is packed, and the component is other than the first
4612 -- component of the record, then the component may be unaligned.
4614 if Is_Packed (Etype (P))
4615 and then Represented_As_Scalar (Etype (C))
4616 and then First_Entity (Scope (C)) /= C
4617 then
4618 return True;
4619 end if;
4621 -- Compute maximum possible alignment for T
4623 -- If alignment is known, then that settles things
4625 if Known_Alignment (T) then
4626 M := UI_To_Int (Alignment (T));
4628 -- If alignment is not known, tentatively set max alignment
4630 else
4631 M := Ttypes.Maximum_Alignment;
4633 -- We can reduce this if the Esize is known since the default
4634 -- alignment will never be more than the smallest power of 2
4635 -- that does not exceed this Esize value.
4637 if Known_Esize (T) then
4638 S := UI_To_Int (Esize (T));
4640 while (M / 2) >= S loop
4641 M := M / 2;
4642 end loop;
4643 end if;
4644 end if;
4646 -- The following code is historical, it used to be present but it
4647 -- is too cautious, because the front-end does not know the proper
4648 -- default alignments for the target. Also, if the alignment is
4649 -- not known, the front end can't know in any case! If a copy is
4650 -- needed, the back-end will take care of it. This whole section
4651 -- including this comment can be removed later ???
4653 -- If the component reference is for a record that has a specified
4654 -- alignment, and we either know it is too small, or cannot tell,
4655 -- then the component may be unaligned.
4657 -- What is the following commented out code ???
4659 -- if Known_Alignment (Etype (P))
4660 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
4661 -- and then M > Alignment (Etype (P))
4662 -- then
4663 -- return True;
4664 -- end if;
4666 -- Case of component clause present which may specify an
4667 -- unaligned position.
4669 if Present (Component_Clause (C)) then
4671 -- Otherwise we can do a test to make sure that the actual
4672 -- start position in the record, and the length, are both
4673 -- consistent with the required alignment. If not, we know
4674 -- that we are unaligned.
4676 declare
4677 Align_In_Bits : constant Nat := M * System_Storage_Unit;
4678 begin
4679 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
4680 or else Esize (C) mod Align_In_Bits /= 0
4681 then
4682 return True;
4683 end if;
4684 end;
4685 end if;
4687 -- Otherwise, for a component reference, test prefix
4689 return Is_Possibly_Unaligned_Object (P);
4690 end;
4692 -- If not a component reference, must be aligned
4694 else
4695 return False;
4696 end if;
4697 end Is_Possibly_Unaligned_Object;
4699 ---------------------------------
4700 -- Is_Possibly_Unaligned_Slice --
4701 ---------------------------------
4703 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
4704 begin
4705 -- Go to renamed object
4707 if Is_Entity_Name (N)
4708 and then Is_Object (Entity (N))
4709 and then Present (Renamed_Object (Entity (N)))
4710 then
4711 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
4712 end if;
4714 -- The reference must be a slice
4716 if Nkind (N) /= N_Slice then
4717 return False;
4718 end if;
4720 -- Always assume the worst for a nested record component with a
4721 -- component clause, which gigi/gcc does not appear to handle well.
4722 -- It is not clear why this special test is needed at all ???
4724 if Nkind (Prefix (N)) = N_Selected_Component
4725 and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
4726 and then
4727 Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
4728 then
4729 return True;
4730 end if;
4732 -- We only need to worry if the target has strict alignment
4734 if not Target_Strict_Alignment then
4735 return False;
4736 end if;
4738 -- If it is a slice, then look at the array type being sliced
4740 declare
4741 Sarr : constant Node_Id := Prefix (N);
4742 -- Prefix of the slice, i.e. the array being sliced
4744 Styp : constant Entity_Id := Etype (Prefix (N));
4745 -- Type of the array being sliced
4747 Pref : Node_Id;
4748 Ptyp : Entity_Id;
4750 begin
4751 -- The problems arise if the array object that is being sliced
4752 -- is a component of a record or array, and we cannot guarantee
4753 -- the alignment of the array within its containing object.
4755 -- To investigate this, we look at successive prefixes to see
4756 -- if we have a worrisome indexed or selected component.
4758 Pref := Sarr;
4759 loop
4760 -- Case of array is part of an indexed component reference
4762 if Nkind (Pref) = N_Indexed_Component then
4763 Ptyp := Etype (Prefix (Pref));
4765 -- The only problematic case is when the array is packed, in
4766 -- which case we really know nothing about the alignment of
4767 -- individual components.
4769 if Is_Bit_Packed_Array (Ptyp) then
4770 return True;
4771 end if;
4773 -- Case of array is part of a selected component reference
4775 elsif Nkind (Pref) = N_Selected_Component then
4776 Ptyp := Etype (Prefix (Pref));
4778 -- We are definitely in trouble if the record in question
4779 -- has an alignment, and either we know this alignment is
4780 -- inconsistent with the alignment of the slice, or we don't
4781 -- know what the alignment of the slice should be.
4783 if Known_Alignment (Ptyp)
4784 and then (Unknown_Alignment (Styp)
4785 or else Alignment (Styp) > Alignment (Ptyp))
4786 then
4787 return True;
4788 end if;
4790 -- We are in potential trouble if the record type is packed.
4791 -- We could special case when we know that the array is the
4792 -- first component, but that's not such a simple case ???
4794 if Is_Packed (Ptyp) then
4795 return True;
4796 end if;
4798 -- We are in trouble if there is a component clause, and
4799 -- either we do not know the alignment of the slice, or
4800 -- the alignment of the slice is inconsistent with the
4801 -- bit position specified by the component clause.
4803 declare
4804 Field : constant Entity_Id := Entity (Selector_Name (Pref));
4805 begin
4806 if Present (Component_Clause (Field))
4807 and then
4808 (Unknown_Alignment (Styp)
4809 or else
4810 (Component_Bit_Offset (Field) mod
4811 (System_Storage_Unit * Alignment (Styp))) /= 0)
4812 then
4813 return True;
4814 end if;
4815 end;
4817 -- For cases other than selected or indexed components we know we
4818 -- are OK, since no issues arise over alignment.
4820 else
4821 return False;
4822 end if;
4824 -- We processed an indexed component or selected component
4825 -- reference that looked safe, so keep checking prefixes.
4827 Pref := Prefix (Pref);
4828 end loop;
4829 end;
4830 end Is_Possibly_Unaligned_Slice;
4832 -------------------------------
4833 -- Is_Related_To_Func_Return --
4834 -------------------------------
4836 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
4837 Expr : constant Node_Id := Related_Expression (Id);
4838 begin
4839 return
4840 Present (Expr)
4841 and then Nkind (Expr) = N_Explicit_Dereference
4842 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
4843 end Is_Related_To_Func_Return;
4845 --------------------------------
4846 -- Is_Ref_To_Bit_Packed_Array --
4847 --------------------------------
4849 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
4850 Result : Boolean;
4851 Expr : Node_Id;
4853 begin
4854 if Is_Entity_Name (N)
4855 and then Is_Object (Entity (N))
4856 and then Present (Renamed_Object (Entity (N)))
4857 then
4858 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
4859 end if;
4861 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
4862 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
4863 Result := True;
4864 else
4865 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
4866 end if;
4868 if Result and then Nkind (N) = N_Indexed_Component then
4869 Expr := First (Expressions (N));
4870 while Present (Expr) loop
4871 Force_Evaluation (Expr);
4872 Next (Expr);
4873 end loop;
4874 end if;
4876 return Result;
4878 else
4879 return False;
4880 end if;
4881 end Is_Ref_To_Bit_Packed_Array;
4883 --------------------------------
4884 -- Is_Ref_To_Bit_Packed_Slice --
4885 --------------------------------
4887 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
4888 begin
4889 if Nkind (N) = N_Type_Conversion then
4890 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
4892 elsif Is_Entity_Name (N)
4893 and then Is_Object (Entity (N))
4894 and then Present (Renamed_Object (Entity (N)))
4895 then
4896 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
4898 elsif Nkind (N) = N_Slice
4899 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
4900 then
4901 return True;
4903 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
4904 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
4906 else
4907 return False;
4908 end if;
4909 end Is_Ref_To_Bit_Packed_Slice;
4911 -----------------------
4912 -- Is_Renamed_Object --
4913 -----------------------
4915 function Is_Renamed_Object (N : Node_Id) return Boolean is
4916 Pnod : constant Node_Id := Parent (N);
4917 Kind : constant Node_Kind := Nkind (Pnod);
4918 begin
4919 if Kind = N_Object_Renaming_Declaration then
4920 return True;
4921 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
4922 return Is_Renamed_Object (Pnod);
4923 else
4924 return False;
4925 end if;
4926 end Is_Renamed_Object;
4928 --------------------------------------
4929 -- Is_Secondary_Stack_BIP_Func_Call --
4930 --------------------------------------
4932 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
4933 Call : Node_Id := Expr;
4935 begin
4936 -- Build-in-place calls usually appear in 'reference format. Note that
4937 -- the accessibility check machinery may add an extra 'reference due to
4938 -- side effect removal.
4940 while Nkind (Call) = N_Reference loop
4941 Call := Prefix (Call);
4942 end loop;
4944 if Nkind_In (Call, N_Qualified_Expression,
4945 N_Unchecked_Type_Conversion)
4946 then
4947 Call := Expression (Call);
4948 end if;
4950 if Is_Build_In_Place_Function_Call (Call) then
4951 declare
4952 Access_Nam : Name_Id := No_Name;
4953 Actual : Node_Id;
4954 Param : Node_Id;
4955 Formal : Node_Id;
4957 begin
4958 -- Examine all parameter associations of the function call
4960 Param := First (Parameter_Associations (Call));
4961 while Present (Param) loop
4962 if Nkind (Param) = N_Parameter_Association
4963 and then Nkind (Selector_Name (Param)) = N_Identifier
4964 then
4965 Formal := Selector_Name (Param);
4966 Actual := Explicit_Actual_Parameter (Param);
4968 -- Construct the name of formal BIPalloc. It is much easier
4969 -- to extract the name of the function using an arbitrary
4970 -- formal's scope rather than the Name field of Call.
4972 if Access_Nam = No_Name
4973 and then Present (Entity (Formal))
4974 then
4975 Access_Nam :=
4976 New_External_Name
4977 (Chars (Scope (Entity (Formal))),
4978 BIP_Formal_Suffix (BIP_Alloc_Form));
4979 end if;
4981 -- A match for BIPalloc => 2 has been found
4983 if Chars (Formal) = Access_Nam
4984 and then Nkind (Actual) = N_Integer_Literal
4985 and then Intval (Actual) = Uint_2
4986 then
4987 return True;
4988 end if;
4989 end if;
4991 Next (Param);
4992 end loop;
4993 end;
4994 end if;
4996 return False;
4997 end Is_Secondary_Stack_BIP_Func_Call;
4999 -------------------------------------
5000 -- Is_Tag_To_Class_Wide_Conversion --
5001 -------------------------------------
5003 function Is_Tag_To_Class_Wide_Conversion
5004 (Obj_Id : Entity_Id) return Boolean
5006 Expr : constant Node_Id := Expression (Parent (Obj_Id));
5008 begin
5009 return
5010 Is_Class_Wide_Type (Etype (Obj_Id))
5011 and then Present (Expr)
5012 and then Nkind (Expr) = N_Unchecked_Type_Conversion
5013 and then Etype (Expression (Expr)) = RTE (RE_Tag);
5014 end Is_Tag_To_Class_Wide_Conversion;
5016 ----------------------------
5017 -- Is_Untagged_Derivation --
5018 ----------------------------
5020 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
5021 begin
5022 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
5023 or else
5024 (Is_Private_Type (T) and then Present (Full_View (T))
5025 and then not Is_Tagged_Type (Full_View (T))
5026 and then Is_Derived_Type (Full_View (T))
5027 and then Etype (Full_View (T)) /= T);
5028 end Is_Untagged_Derivation;
5030 ---------------------------
5031 -- Is_Volatile_Reference --
5032 ---------------------------
5034 function Is_Volatile_Reference (N : Node_Id) return Boolean is
5035 begin
5036 if Nkind (N) in N_Has_Etype
5037 and then Present (Etype (N))
5038 and then Treat_As_Volatile (Etype (N))
5039 then
5040 return True;
5042 elsif Is_Entity_Name (N) then
5043 return Treat_As_Volatile (Entity (N));
5045 elsif Nkind (N) = N_Slice then
5046 return Is_Volatile_Reference (Prefix (N));
5048 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5049 if (Is_Entity_Name (Prefix (N))
5050 and then Has_Volatile_Components (Entity (Prefix (N))))
5051 or else (Present (Etype (Prefix (N)))
5052 and then Has_Volatile_Components (Etype (Prefix (N))))
5053 then
5054 return True;
5055 else
5056 return Is_Volatile_Reference (Prefix (N));
5057 end if;
5059 else
5060 return False;
5061 end if;
5062 end Is_Volatile_Reference;
5064 --------------------------
5065 -- Is_VM_By_Copy_Actual --
5066 --------------------------
5068 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
5069 begin
5070 return VM_Target /= No_VM
5071 and then (Nkind (N) = N_Slice
5072 or else
5073 (Nkind (N) = N_Identifier
5074 and then Present (Renamed_Object (Entity (N)))
5075 and then Nkind (Renamed_Object (Entity (N))) =
5076 N_Slice));
5077 end Is_VM_By_Copy_Actual;
5079 --------------------
5080 -- Kill_Dead_Code --
5081 --------------------
5083 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
5084 W : Boolean := Warn;
5085 -- Set False if warnings suppressed
5087 begin
5088 if Present (N) then
5089 Remove_Warning_Messages (N);
5091 -- Generate warning if appropriate
5093 if W then
5095 -- We suppress the warning if this code is under control of an
5096 -- if statement, whose condition is a simple identifier, and
5097 -- either we are in an instance, or warnings off is set for this
5098 -- identifier. The reason for killing it in the instance case is
5099 -- that it is common and reasonable for code to be deleted in
5100 -- instances for various reasons.
5102 if Nkind (Parent (N)) = N_If_Statement then
5103 declare
5104 C : constant Node_Id := Condition (Parent (N));
5105 begin
5106 if Nkind (C) = N_Identifier
5107 and then
5108 (In_Instance
5109 or else (Present (Entity (C))
5110 and then Has_Warnings_Off (Entity (C))))
5111 then
5112 W := False;
5113 end if;
5114 end;
5115 end if;
5117 -- Generate warning if not suppressed
5119 if W then
5120 Error_Msg_F
5121 ("?t?this code can never be executed and has been deleted!",
5123 end if;
5124 end if;
5126 -- Recurse into block statements and bodies to process declarations
5127 -- and statements.
5129 if Nkind (N) = N_Block_Statement
5130 or else Nkind (N) = N_Subprogram_Body
5131 or else Nkind (N) = N_Package_Body
5132 then
5133 Kill_Dead_Code (Declarations (N), False);
5134 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
5136 if Nkind (N) = N_Subprogram_Body then
5137 Set_Is_Eliminated (Defining_Entity (N));
5138 end if;
5140 elsif Nkind (N) = N_Package_Declaration then
5141 Kill_Dead_Code (Visible_Declarations (Specification (N)));
5142 Kill_Dead_Code (Private_Declarations (Specification (N)));
5144 -- ??? After this point, Delete_Tree has been called on all
5145 -- declarations in Specification (N), so references to entities
5146 -- therein look suspicious.
5148 declare
5149 E : Entity_Id := First_Entity (Defining_Entity (N));
5150 begin
5151 while Present (E) loop
5152 if Ekind (E) = E_Operator then
5153 Set_Is_Eliminated (E);
5154 end if;
5156 Next_Entity (E);
5157 end loop;
5158 end;
5160 -- Recurse into composite statement to kill individual statements in
5161 -- particular instantiations.
5163 elsif Nkind (N) = N_If_Statement then
5164 Kill_Dead_Code (Then_Statements (N));
5165 Kill_Dead_Code (Elsif_Parts (N));
5166 Kill_Dead_Code (Else_Statements (N));
5168 elsif Nkind (N) = N_Loop_Statement then
5169 Kill_Dead_Code (Statements (N));
5171 elsif Nkind (N) = N_Case_Statement then
5172 declare
5173 Alt : Node_Id;
5174 begin
5175 Alt := First (Alternatives (N));
5176 while Present (Alt) loop
5177 Kill_Dead_Code (Statements (Alt));
5178 Next (Alt);
5179 end loop;
5180 end;
5182 elsif Nkind (N) = N_Case_Statement_Alternative then
5183 Kill_Dead_Code (Statements (N));
5185 -- Deal with dead instances caused by deleting instantiations
5187 elsif Nkind (N) in N_Generic_Instantiation then
5188 Remove_Dead_Instance (N);
5189 end if;
5190 end if;
5191 end Kill_Dead_Code;
5193 -- Case where argument is a list of nodes to be killed
5195 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
5196 N : Node_Id;
5197 W : Boolean;
5198 begin
5199 W := Warn;
5200 if Is_Non_Empty_List (L) then
5201 N := First (L);
5202 while Present (N) loop
5203 Kill_Dead_Code (N, W);
5204 W := False;
5205 Next (N);
5206 end loop;
5207 end if;
5208 end Kill_Dead_Code;
5210 ------------------------
5211 -- Known_Non_Negative --
5212 ------------------------
5214 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
5215 begin
5216 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
5217 return True;
5219 else
5220 declare
5221 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
5222 begin
5223 return
5224 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
5225 end;
5226 end if;
5227 end Known_Non_Negative;
5229 --------------------
5230 -- Known_Non_Null --
5231 --------------------
5233 function Known_Non_Null (N : Node_Id) return Boolean is
5234 begin
5235 -- Checks for case where N is an entity reference
5237 if Is_Entity_Name (N) and then Present (Entity (N)) then
5238 declare
5239 E : constant Entity_Id := Entity (N);
5240 Op : Node_Kind;
5241 Val : Node_Id;
5243 begin
5244 -- First check if we are in decisive conditional
5246 Get_Current_Value_Condition (N, Op, Val);
5248 if Known_Null (Val) then
5249 if Op = N_Op_Eq then
5250 return False;
5251 elsif Op = N_Op_Ne then
5252 return True;
5253 end if;
5254 end if;
5256 -- If OK to do replacement, test Is_Known_Non_Null flag
5258 if OK_To_Do_Constant_Replacement (E) then
5259 return Is_Known_Non_Null (E);
5261 -- Otherwise if not safe to do replacement, then say so
5263 else
5264 return False;
5265 end if;
5266 end;
5268 -- True if access attribute
5270 elsif Nkind (N) = N_Attribute_Reference
5271 and then Nam_In (Attribute_Name (N), Name_Access,
5272 Name_Unchecked_Access,
5273 Name_Unrestricted_Access)
5274 then
5275 return True;
5277 -- True if allocator
5279 elsif Nkind (N) = N_Allocator then
5280 return True;
5282 -- For a conversion, true if expression is known non-null
5284 elsif Nkind (N) = N_Type_Conversion then
5285 return Known_Non_Null (Expression (N));
5287 -- Above are all cases where the value could be determined to be
5288 -- non-null. In all other cases, we don't know, so return False.
5290 else
5291 return False;
5292 end if;
5293 end Known_Non_Null;
5295 ----------------
5296 -- Known_Null --
5297 ----------------
5299 function Known_Null (N : Node_Id) return Boolean is
5300 begin
5301 -- Checks for case where N is an entity reference
5303 if Is_Entity_Name (N) and then Present (Entity (N)) then
5304 declare
5305 E : constant Entity_Id := Entity (N);
5306 Op : Node_Kind;
5307 Val : Node_Id;
5309 begin
5310 -- Constant null value is for sure null
5312 if Ekind (E) = E_Constant
5313 and then Known_Null (Constant_Value (E))
5314 then
5315 return True;
5316 end if;
5318 -- First check if we are in decisive conditional
5320 Get_Current_Value_Condition (N, Op, Val);
5322 if Known_Null (Val) then
5323 if Op = N_Op_Eq then
5324 return True;
5325 elsif Op = N_Op_Ne then
5326 return False;
5327 end if;
5328 end if;
5330 -- If OK to do replacement, test Is_Known_Null flag
5332 if OK_To_Do_Constant_Replacement (E) then
5333 return Is_Known_Null (E);
5335 -- Otherwise if not safe to do replacement, then say so
5337 else
5338 return False;
5339 end if;
5340 end;
5342 -- True if explicit reference to null
5344 elsif Nkind (N) = N_Null then
5345 return True;
5347 -- For a conversion, true if expression is known null
5349 elsif Nkind (N) = N_Type_Conversion then
5350 return Known_Null (Expression (N));
5352 -- Above are all cases where the value could be determined to be null.
5353 -- In all other cases, we don't know, so return False.
5355 else
5356 return False;
5357 end if;
5358 end Known_Null;
5360 -----------------------------
5361 -- Make_CW_Equivalent_Type --
5362 -----------------------------
5364 -- Create a record type used as an equivalent of any member of the class
5365 -- which takes its size from exp.
5367 -- Generate the following code:
5369 -- type Equiv_T is record
5370 -- _parent : T (List of discriminant constraints taken from Exp);
5371 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
5372 -- end Equiv_T;
5374 -- ??? Note that this type does not guarantee same alignment as all
5375 -- derived types
5377 function Make_CW_Equivalent_Type
5378 (T : Entity_Id;
5379 E : Node_Id) return Entity_Id
5381 Loc : constant Source_Ptr := Sloc (E);
5382 Root_Typ : constant Entity_Id := Root_Type (T);
5383 List_Def : constant List_Id := Empty_List;
5384 Comp_List : constant List_Id := New_List;
5385 Equiv_Type : Entity_Id;
5386 Range_Type : Entity_Id;
5387 Str_Type : Entity_Id;
5388 Constr_Root : Entity_Id;
5389 Sizexpr : Node_Id;
5391 begin
5392 -- If the root type is already constrained, there are no discriminants
5393 -- in the expression.
5395 if not Has_Discriminants (Root_Typ)
5396 or else Is_Constrained (Root_Typ)
5397 then
5398 Constr_Root := Root_Typ;
5399 else
5400 Constr_Root := Make_Temporary (Loc, 'R');
5402 -- subtype cstr__n is T (List of discr constraints taken from Exp)
5404 Append_To (List_Def,
5405 Make_Subtype_Declaration (Loc,
5406 Defining_Identifier => Constr_Root,
5407 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
5408 end if;
5410 -- Generate the range subtype declaration
5412 Range_Type := Make_Temporary (Loc, 'G');
5414 if not Is_Interface (Root_Typ) then
5416 -- subtype rg__xx is
5417 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
5419 Sizexpr :=
5420 Make_Op_Subtract (Loc,
5421 Left_Opnd =>
5422 Make_Attribute_Reference (Loc,
5423 Prefix =>
5424 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
5425 Attribute_Name => Name_Size),
5426 Right_Opnd =>
5427 Make_Attribute_Reference (Loc,
5428 Prefix => New_Reference_To (Constr_Root, Loc),
5429 Attribute_Name => Name_Object_Size));
5430 else
5431 -- subtype rg__xx is
5432 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
5434 Sizexpr :=
5435 Make_Attribute_Reference (Loc,
5436 Prefix =>
5437 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
5438 Attribute_Name => Name_Size);
5439 end if;
5441 Set_Paren_Count (Sizexpr, 1);
5443 Append_To (List_Def,
5444 Make_Subtype_Declaration (Loc,
5445 Defining_Identifier => Range_Type,
5446 Subtype_Indication =>
5447 Make_Subtype_Indication (Loc,
5448 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
5449 Constraint => Make_Range_Constraint (Loc,
5450 Range_Expression =>
5451 Make_Range (Loc,
5452 Low_Bound => Make_Integer_Literal (Loc, 1),
5453 High_Bound =>
5454 Make_Op_Divide (Loc,
5455 Left_Opnd => Sizexpr,
5456 Right_Opnd => Make_Integer_Literal (Loc,
5457 Intval => System_Storage_Unit)))))));
5459 -- subtype str__nn is Storage_Array (rg__x);
5461 Str_Type := Make_Temporary (Loc, 'S');
5462 Append_To (List_Def,
5463 Make_Subtype_Declaration (Loc,
5464 Defining_Identifier => Str_Type,
5465 Subtype_Indication =>
5466 Make_Subtype_Indication (Loc,
5467 Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
5468 Constraint =>
5469 Make_Index_Or_Discriminant_Constraint (Loc,
5470 Constraints =>
5471 New_List (New_Reference_To (Range_Type, Loc))))));
5473 -- type Equiv_T is record
5474 -- [ _parent : Tnn; ]
5475 -- E : Str_Type;
5476 -- end Equiv_T;
5478 Equiv_Type := Make_Temporary (Loc, 'T');
5479 Set_Ekind (Equiv_Type, E_Record_Type);
5480 Set_Parent_Subtype (Equiv_Type, Constr_Root);
5482 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
5483 -- treatment for this type. In particular, even though _parent's type
5484 -- is a controlled type or contains controlled components, we do not
5485 -- want to set Has_Controlled_Component on it to avoid making it gain
5486 -- an unwanted _controller component.
5488 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
5490 if not Is_Interface (Root_Typ) then
5491 Append_To (Comp_List,
5492 Make_Component_Declaration (Loc,
5493 Defining_Identifier =>
5494 Make_Defining_Identifier (Loc, Name_uParent),
5495 Component_Definition =>
5496 Make_Component_Definition (Loc,
5497 Aliased_Present => False,
5498 Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
5499 end if;
5501 Append_To (Comp_List,
5502 Make_Component_Declaration (Loc,
5503 Defining_Identifier => Make_Temporary (Loc, 'C'),
5504 Component_Definition =>
5505 Make_Component_Definition (Loc,
5506 Aliased_Present => False,
5507 Subtype_Indication => New_Reference_To (Str_Type, Loc))));
5509 Append_To (List_Def,
5510 Make_Full_Type_Declaration (Loc,
5511 Defining_Identifier => Equiv_Type,
5512 Type_Definition =>
5513 Make_Record_Definition (Loc,
5514 Component_List =>
5515 Make_Component_List (Loc,
5516 Component_Items => Comp_List,
5517 Variant_Part => Empty))));
5519 -- Suppress all checks during the analysis of the expanded code to avoid
5520 -- the generation of spurious warnings under ZFP run-time.
5522 Insert_Actions (E, List_Def, Suppress => All_Checks);
5523 return Equiv_Type;
5524 end Make_CW_Equivalent_Type;
5526 -------------------------
5527 -- Make_Invariant_Call --
5528 -------------------------
5530 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
5531 Loc : constant Source_Ptr := Sloc (Expr);
5532 Typ : Entity_Id;
5534 begin
5535 Typ := Etype (Expr);
5537 -- Subtypes may be subject to invariants coming from their respective
5538 -- base types.
5540 if Ekind_In (Typ, E_Array_Subtype,
5541 E_Private_Subtype,
5542 E_Record_Subtype)
5543 then
5544 Typ := Base_Type (Typ);
5545 end if;
5547 pragma Assert
5548 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
5550 return
5551 Make_Procedure_Call_Statement (Loc,
5552 Name =>
5553 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
5554 Parameter_Associations => New_List (Relocate_Node (Expr)));
5555 end Make_Invariant_Call;
5557 ------------------------
5558 -- Make_Literal_Range --
5559 ------------------------
5561 function Make_Literal_Range
5562 (Loc : Source_Ptr;
5563 Literal_Typ : Entity_Id) return Node_Id
5565 Lo : constant Node_Id :=
5566 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
5567 Index : constant Entity_Id := Etype (Lo);
5569 Hi : Node_Id;
5570 Length_Expr : constant Node_Id :=
5571 Make_Op_Subtract (Loc,
5572 Left_Opnd =>
5573 Make_Integer_Literal (Loc,
5574 Intval => String_Literal_Length (Literal_Typ)),
5575 Right_Opnd =>
5576 Make_Integer_Literal (Loc, 1));
5578 begin
5579 Set_Analyzed (Lo, False);
5581 if Is_Integer_Type (Index) then
5582 Hi :=
5583 Make_Op_Add (Loc,
5584 Left_Opnd => New_Copy_Tree (Lo),
5585 Right_Opnd => Length_Expr);
5586 else
5587 Hi :=
5588 Make_Attribute_Reference (Loc,
5589 Attribute_Name => Name_Val,
5590 Prefix => New_Occurrence_Of (Index, Loc),
5591 Expressions => New_List (
5592 Make_Op_Add (Loc,
5593 Left_Opnd =>
5594 Make_Attribute_Reference (Loc,
5595 Attribute_Name => Name_Pos,
5596 Prefix => New_Occurrence_Of (Index, Loc),
5597 Expressions => New_List (New_Copy_Tree (Lo))),
5598 Right_Opnd => Length_Expr)));
5599 end if;
5601 return
5602 Make_Range (Loc,
5603 Low_Bound => Lo,
5604 High_Bound => Hi);
5605 end Make_Literal_Range;
5607 --------------------------
5608 -- Make_Non_Empty_Check --
5609 --------------------------
5611 function Make_Non_Empty_Check
5612 (Loc : Source_Ptr;
5613 N : Node_Id) return Node_Id
5615 begin
5616 return
5617 Make_Op_Ne (Loc,
5618 Left_Opnd =>
5619 Make_Attribute_Reference (Loc,
5620 Attribute_Name => Name_Length,
5621 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
5622 Right_Opnd =>
5623 Make_Integer_Literal (Loc, 0));
5624 end Make_Non_Empty_Check;
5626 -------------------------
5627 -- Make_Predicate_Call --
5628 -------------------------
5630 function Make_Predicate_Call
5631 (Typ : Entity_Id;
5632 Expr : Node_Id;
5633 Mem : Boolean := False) return Node_Id
5635 Loc : constant Source_Ptr := Sloc (Expr);
5637 begin
5638 pragma Assert (Present (Predicate_Function (Typ)));
5640 -- Call special membership version if requested and available
5642 if Mem then
5643 declare
5644 PFM : constant Entity_Id := Predicate_Function_M (Typ);
5645 begin
5646 if Present (PFM) then
5647 return
5648 Make_Function_Call (Loc,
5649 Name => New_Occurrence_Of (PFM, Loc),
5650 Parameter_Associations => New_List (Relocate_Node (Expr)));
5651 end if;
5652 end;
5653 end if;
5655 -- Case of calling normal predicate function
5657 return
5658 Make_Function_Call (Loc,
5659 Name =>
5660 New_Occurrence_Of (Predicate_Function (Typ), Loc),
5661 Parameter_Associations => New_List (Relocate_Node (Expr)));
5662 end Make_Predicate_Call;
5664 --------------------------
5665 -- Make_Predicate_Check --
5666 --------------------------
5668 function Make_Predicate_Check
5669 (Typ : Entity_Id;
5670 Expr : Node_Id) return Node_Id
5672 Loc : constant Source_Ptr := Sloc (Expr);
5673 Nam : Name_Id;
5675 begin
5676 -- If predicate checks are suppressed, then return a null statement.
5677 -- For this call, we check only the scope setting. If the caller wants
5678 -- to check a specific entity's setting, they must do it manually.
5680 if Predicate_Checks_Suppressed (Empty) then
5681 return Make_Null_Statement (Loc);
5682 end if;
5684 -- Compute proper name to use, we need to get this right so that the
5685 -- right set of check policies apply to the Check pragma we are making.
5687 if Has_Dynamic_Predicate_Aspect (Typ) then
5688 Nam := Name_Dynamic_Predicate;
5689 elsif Has_Static_Predicate_Aspect (Typ) then
5690 Nam := Name_Static_Predicate;
5691 else
5692 Nam := Name_Predicate;
5693 end if;
5695 return
5696 Make_Pragma (Loc,
5697 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
5698 Pragma_Argument_Associations => New_List (
5699 Make_Pragma_Argument_Association (Loc,
5700 Expression => Make_Identifier (Loc, Nam)),
5701 Make_Pragma_Argument_Association (Loc,
5702 Expression => Make_Predicate_Call (Typ, Expr))));
5703 end Make_Predicate_Check;
5705 ----------------------------
5706 -- Make_Subtype_From_Expr --
5707 ----------------------------
5709 -- 1. If Expr is an unconstrained array expression, creates
5710 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
5712 -- 2. If Expr is a unconstrained discriminated type expression, creates
5713 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
5715 -- 3. If Expr is class-wide, creates an implicit class wide subtype
5717 function Make_Subtype_From_Expr
5718 (E : Node_Id;
5719 Unc_Typ : Entity_Id) return Node_Id
5721 Loc : constant Source_Ptr := Sloc (E);
5722 List_Constr : constant List_Id := New_List;
5723 D : Entity_Id;
5725 Full_Subtyp : Entity_Id;
5726 Priv_Subtyp : Entity_Id;
5727 Utyp : Entity_Id;
5728 Full_Exp : Node_Id;
5730 begin
5731 if Is_Private_Type (Unc_Typ)
5732 and then Has_Unknown_Discriminants (Unc_Typ)
5733 then
5734 -- Prepare the subtype completion, Go to base type to
5735 -- find underlying type, because the type may be a generic
5736 -- actual or an explicit subtype.
5738 Utyp := Underlying_Type (Base_Type (Unc_Typ));
5739 Full_Subtyp := Make_Temporary (Loc, 'C');
5740 Full_Exp :=
5741 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
5742 Set_Parent (Full_Exp, Parent (E));
5744 Priv_Subtyp := Make_Temporary (Loc, 'P');
5746 Insert_Action (E,
5747 Make_Subtype_Declaration (Loc,
5748 Defining_Identifier => Full_Subtyp,
5749 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
5751 -- Define the dummy private subtype
5753 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
5754 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
5755 Set_Scope (Priv_Subtyp, Full_Subtyp);
5756 Set_Is_Constrained (Priv_Subtyp);
5757 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
5758 Set_Is_Itype (Priv_Subtyp);
5759 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
5761 if Is_Tagged_Type (Priv_Subtyp) then
5762 Set_Class_Wide_Type
5763 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
5764 Set_Direct_Primitive_Operations (Priv_Subtyp,
5765 Direct_Primitive_Operations (Unc_Typ));
5766 end if;
5768 Set_Full_View (Priv_Subtyp, Full_Subtyp);
5770 return New_Reference_To (Priv_Subtyp, Loc);
5772 elsif Is_Array_Type (Unc_Typ) then
5773 for J in 1 .. Number_Dimensions (Unc_Typ) loop
5774 Append_To (List_Constr,
5775 Make_Range (Loc,
5776 Low_Bound =>
5777 Make_Attribute_Reference (Loc,
5778 Prefix => Duplicate_Subexpr_No_Checks (E),
5779 Attribute_Name => Name_First,
5780 Expressions => New_List (
5781 Make_Integer_Literal (Loc, J))),
5783 High_Bound =>
5784 Make_Attribute_Reference (Loc,
5785 Prefix => Duplicate_Subexpr_No_Checks (E),
5786 Attribute_Name => Name_Last,
5787 Expressions => New_List (
5788 Make_Integer_Literal (Loc, J)))));
5789 end loop;
5791 elsif Is_Class_Wide_Type (Unc_Typ) then
5792 declare
5793 CW_Subtype : Entity_Id;
5794 EQ_Typ : Entity_Id := Empty;
5796 begin
5797 -- A class-wide equivalent type is not needed when VM_Target
5798 -- because the VM back-ends handle the class-wide object
5799 -- initialization itself (and doesn't need or want the
5800 -- additional intermediate type to handle the assignment).
5802 if Expander_Active and then Tagged_Type_Expansion then
5804 -- If this is the class_wide type of a completion that is a
5805 -- record subtype, set the type of the class_wide type to be
5806 -- the full base type, for use in the expanded code for the
5807 -- equivalent type. Should this be done earlier when the
5808 -- completion is analyzed ???
5810 if Is_Private_Type (Etype (Unc_Typ))
5811 and then
5812 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
5813 then
5814 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
5815 end if;
5817 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
5818 end if;
5820 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
5821 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
5822 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
5824 return New_Occurrence_Of (CW_Subtype, Loc);
5825 end;
5827 -- Indefinite record type with discriminants
5829 else
5830 D := First_Discriminant (Unc_Typ);
5831 while Present (D) loop
5832 Append_To (List_Constr,
5833 Make_Selected_Component (Loc,
5834 Prefix => Duplicate_Subexpr_No_Checks (E),
5835 Selector_Name => New_Reference_To (D, Loc)));
5837 Next_Discriminant (D);
5838 end loop;
5839 end if;
5841 return
5842 Make_Subtype_Indication (Loc,
5843 Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
5844 Constraint =>
5845 Make_Index_Or_Discriminant_Constraint (Loc,
5846 Constraints => List_Constr));
5847 end Make_Subtype_From_Expr;
5849 -----------------------------
5850 -- May_Generate_Large_Temp --
5851 -----------------------------
5853 -- At the current time, the only types that we return False for (i.e. where
5854 -- we decide we know they cannot generate large temps) are ones where we
5855 -- know the size is 256 bits or less at compile time, and we are still not
5856 -- doing a thorough job on arrays and records ???
5858 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
5859 begin
5860 if not Size_Known_At_Compile_Time (Typ) then
5861 return False;
5863 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
5864 return False;
5866 elsif Is_Array_Type (Typ) and then Present (Packed_Array_Type (Typ)) then
5867 return May_Generate_Large_Temp (Packed_Array_Type (Typ));
5869 -- We could do more here to find other small types ???
5871 else
5872 return True;
5873 end if;
5874 end May_Generate_Large_Temp;
5876 ------------------------
5877 -- Needs_Finalization --
5878 ------------------------
5880 function Needs_Finalization (T : Entity_Id) return Boolean is
5881 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
5882 -- If type is not frozen yet, check explicitly among its components,
5883 -- because the Has_Controlled_Component flag is not necessarily set.
5885 -----------------------------------
5886 -- Has_Some_Controlled_Component --
5887 -----------------------------------
5889 function Has_Some_Controlled_Component
5890 (Rec : Entity_Id) return Boolean
5892 Comp : Entity_Id;
5894 begin
5895 if Has_Controlled_Component (Rec) then
5896 return True;
5898 elsif not Is_Frozen (Rec) then
5899 if Is_Record_Type (Rec) then
5900 Comp := First_Entity (Rec);
5902 while Present (Comp) loop
5903 if not Is_Type (Comp)
5904 and then Needs_Finalization (Etype (Comp))
5905 then
5906 return True;
5907 end if;
5909 Next_Entity (Comp);
5910 end loop;
5912 return False;
5914 elsif Is_Array_Type (Rec) then
5915 return Needs_Finalization (Component_Type (Rec));
5917 else
5918 return Has_Controlled_Component (Rec);
5919 end if;
5920 else
5921 return False;
5922 end if;
5923 end Has_Some_Controlled_Component;
5925 -- Start of processing for Needs_Finalization
5927 begin
5928 -- Certain run-time configurations and targets do not provide support
5929 -- for controlled types.
5931 if Restriction_Active (No_Finalization) then
5932 return False;
5934 -- C, C++, CIL and Java types are not considered controlled. It is
5935 -- assumed that the non-Ada side will handle their clean up.
5937 elsif Convention (T) = Convention_C
5938 or else Convention (T) = Convention_CIL
5939 or else Convention (T) = Convention_CPP
5940 or else Convention (T) = Convention_Java
5941 then
5942 return False;
5944 else
5945 -- Class-wide types are treated as controlled because derivations
5946 -- from the root type can introduce controlled components.
5948 return
5949 Is_Class_Wide_Type (T)
5950 or else Is_Controlled (T)
5951 or else Has_Controlled_Component (T)
5952 or else Has_Some_Controlled_Component (T)
5953 or else
5954 (Is_Concurrent_Type (T)
5955 and then Present (Corresponding_Record_Type (T))
5956 and then Needs_Finalization (Corresponding_Record_Type (T)));
5957 end if;
5958 end Needs_Finalization;
5960 ----------------------------
5961 -- Needs_Constant_Address --
5962 ----------------------------
5964 function Needs_Constant_Address
5965 (Decl : Node_Id;
5966 Typ : Entity_Id) return Boolean
5968 begin
5970 -- If we have no initialization of any kind, then we don't need to place
5971 -- any restrictions on the address clause, because the object will be
5972 -- elaborated after the address clause is evaluated. This happens if the
5973 -- declaration has no initial expression, or the type has no implicit
5974 -- initialization, or the object is imported.
5976 -- The same holds for all initialized scalar types and all access types.
5977 -- Packed bit arrays of size up to 64 are represented using a modular
5978 -- type with an initialization (to zero) and can be processed like other
5979 -- initialized scalar types.
5981 -- If the type is controlled, code to attach the object to a
5982 -- finalization chain is generated at the point of declaration, and
5983 -- therefore the elaboration of the object cannot be delayed: the
5984 -- address expression must be a constant.
5986 if No (Expression (Decl))
5987 and then not Needs_Finalization (Typ)
5988 and then
5989 (not Has_Non_Null_Base_Init_Proc (Typ)
5990 or else Is_Imported (Defining_Identifier (Decl)))
5991 then
5992 return False;
5994 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
5995 or else Is_Access_Type (Typ)
5996 or else
5997 (Is_Bit_Packed_Array (Typ)
5998 and then Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
5999 then
6000 return False;
6002 else
6004 -- Otherwise, we require the address clause to be constant because
6005 -- the call to the initialization procedure (or the attach code) has
6006 -- to happen at the point of the declaration.
6008 -- Actually the IP call has been moved to the freeze actions anyway,
6009 -- so maybe we can relax this restriction???
6011 return True;
6012 end if;
6013 end Needs_Constant_Address;
6015 ----------------------------
6016 -- New_Class_Wide_Subtype --
6017 ----------------------------
6019 function New_Class_Wide_Subtype
6020 (CW_Typ : Entity_Id;
6021 N : Node_Id) return Entity_Id
6023 Res : constant Entity_Id := Create_Itype (E_Void, N);
6024 Res_Name : constant Name_Id := Chars (Res);
6025 Res_Scope : constant Entity_Id := Scope (Res);
6027 begin
6028 Copy_Node (CW_Typ, Res);
6029 Set_Comes_From_Source (Res, False);
6030 Set_Sloc (Res, Sloc (N));
6031 Set_Is_Itype (Res);
6032 Set_Associated_Node_For_Itype (Res, N);
6033 Set_Is_Public (Res, False); -- By default, may be changed below.
6034 Set_Public_Status (Res);
6035 Set_Chars (Res, Res_Name);
6036 Set_Scope (Res, Res_Scope);
6037 Set_Ekind (Res, E_Class_Wide_Subtype);
6038 Set_Next_Entity (Res, Empty);
6039 Set_Etype (Res, Base_Type (CW_Typ));
6040 Set_Is_Frozen (Res, False);
6041 Set_Freeze_Node (Res, Empty);
6042 return (Res);
6043 end New_Class_Wide_Subtype;
6045 --------------------------------
6046 -- Non_Limited_Designated_Type --
6047 ---------------------------------
6049 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
6050 Desig : constant Entity_Id := Designated_Type (T);
6051 begin
6052 if Ekind (Desig) = E_Incomplete_Type
6053 and then Present (Non_Limited_View (Desig))
6054 then
6055 return Non_Limited_View (Desig);
6056 else
6057 return Desig;
6058 end if;
6059 end Non_Limited_Designated_Type;
6061 -----------------------------------
6062 -- OK_To_Do_Constant_Replacement --
6063 -----------------------------------
6065 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
6066 ES : constant Entity_Id := Scope (E);
6067 CS : Entity_Id;
6069 begin
6070 -- Do not replace statically allocated objects, because they may be
6071 -- modified outside the current scope.
6073 if Is_Statically_Allocated (E) then
6074 return False;
6076 -- Do not replace aliased or volatile objects, since we don't know what
6077 -- else might change the value.
6079 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
6080 return False;
6082 -- Debug flag -gnatdM disconnects this optimization
6084 elsif Debug_Flag_MM then
6085 return False;
6087 -- Otherwise check scopes
6089 else
6090 CS := Current_Scope;
6092 loop
6093 -- If we are in right scope, replacement is safe
6095 if CS = ES then
6096 return True;
6098 -- Packages do not affect the determination of safety
6100 elsif Ekind (CS) = E_Package then
6101 exit when CS = Standard_Standard;
6102 CS := Scope (CS);
6104 -- Blocks do not affect the determination of safety
6106 elsif Ekind (CS) = E_Block then
6107 CS := Scope (CS);
6109 -- Loops do not affect the determination of safety. Note that we
6110 -- kill all current values on entry to a loop, so we are just
6111 -- talking about processing within a loop here.
6113 elsif Ekind (CS) = E_Loop then
6114 CS := Scope (CS);
6116 -- Otherwise, the reference is dubious, and we cannot be sure that
6117 -- it is safe to do the replacement.
6119 else
6120 exit;
6121 end if;
6122 end loop;
6124 return False;
6125 end if;
6126 end OK_To_Do_Constant_Replacement;
6128 ------------------------------------
6129 -- Possible_Bit_Aligned_Component --
6130 ------------------------------------
6132 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
6133 begin
6134 case Nkind (N) is
6136 -- Case of indexed component
6138 when N_Indexed_Component =>
6139 declare
6140 P : constant Node_Id := Prefix (N);
6141 Ptyp : constant Entity_Id := Etype (P);
6143 begin
6144 -- If we know the component size and it is less than 64, then
6145 -- we are definitely OK. The back end always does assignment of
6146 -- misaligned small objects correctly.
6148 if Known_Static_Component_Size (Ptyp)
6149 and then Component_Size (Ptyp) <= 64
6150 then
6151 return False;
6153 -- Otherwise, we need to test the prefix, to see if we are
6154 -- indexing from a possibly unaligned component.
6156 else
6157 return Possible_Bit_Aligned_Component (P);
6158 end if;
6159 end;
6161 -- Case of selected component
6163 when N_Selected_Component =>
6164 declare
6165 P : constant Node_Id := Prefix (N);
6166 Comp : constant Entity_Id := Entity (Selector_Name (N));
6168 begin
6169 -- If there is no component clause, then we are in the clear
6170 -- since the back end will never misalign a large component
6171 -- unless it is forced to do so. In the clear means we need
6172 -- only the recursive test on the prefix.
6174 if Component_May_Be_Bit_Aligned (Comp) then
6175 return True;
6176 else
6177 return Possible_Bit_Aligned_Component (P);
6178 end if;
6179 end;
6181 -- For a slice, test the prefix, if that is possibly misaligned,
6182 -- then for sure the slice is!
6184 when N_Slice =>
6185 return Possible_Bit_Aligned_Component (Prefix (N));
6187 -- For an unchecked conversion, check whether the expression may
6188 -- be bit-aligned.
6190 when N_Unchecked_Type_Conversion =>
6191 return Possible_Bit_Aligned_Component (Expression (N));
6193 -- If we have none of the above, it means that we have fallen off the
6194 -- top testing prefixes recursively, and we now have a stand alone
6195 -- object, where we don't have a problem.
6197 when others =>
6198 return False;
6200 end case;
6201 end Possible_Bit_Aligned_Component;
6203 -----------------------------------------------
6204 -- Process_Statements_For_Controlled_Objects --
6205 -----------------------------------------------
6207 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
6208 Loc : constant Source_Ptr := Sloc (N);
6210 function Are_Wrapped (L : List_Id) return Boolean;
6211 -- Determine whether list L contains only one statement which is a block
6213 function Wrap_Statements_In_Block (L : List_Id) return Node_Id;
6214 -- Given a list of statements L, wrap it in a block statement and return
6215 -- the generated node.
6217 -----------------
6218 -- Are_Wrapped --
6219 -----------------
6221 function Are_Wrapped (L : List_Id) return Boolean is
6222 Stmt : constant Node_Id := First (L);
6223 begin
6224 return
6225 Present (Stmt)
6226 and then No (Next (Stmt))
6227 and then Nkind (Stmt) = N_Block_Statement;
6228 end Are_Wrapped;
6230 ------------------------------
6231 -- Wrap_Statements_In_Block --
6232 ------------------------------
6234 function Wrap_Statements_In_Block (L : List_Id) return Node_Id is
6235 begin
6236 return
6237 Make_Block_Statement (Loc,
6238 Declarations => No_List,
6239 Handled_Statement_Sequence =>
6240 Make_Handled_Sequence_Of_Statements (Loc,
6241 Statements => L));
6242 end Wrap_Statements_In_Block;
6244 -- Local variables
6246 Block : Node_Id;
6248 -- Start of processing for Process_Statements_For_Controlled_Objects
6250 begin
6251 -- Whenever a non-handled statement list is wrapped in a block, the
6252 -- block must be explicitly analyzed to redecorate all entities in the
6253 -- list and ensure that a finalizer is properly built.
6255 case Nkind (N) is
6256 when N_Elsif_Part |
6257 N_If_Statement |
6258 N_Conditional_Entry_Call |
6259 N_Selective_Accept =>
6261 -- Check the "then statements" for elsif parts and if statements
6263 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
6264 and then not Is_Empty_List (Then_Statements (N))
6265 and then not Are_Wrapped (Then_Statements (N))
6266 and then Requires_Cleanup_Actions
6267 (Then_Statements (N), False, False)
6268 then
6269 Block := Wrap_Statements_In_Block (Then_Statements (N));
6270 Set_Then_Statements (N, New_List (Block));
6272 Analyze (Block);
6273 end if;
6275 -- Check the "else statements" for conditional entry calls, if
6276 -- statements and selective accepts.
6278 if Nkind_In (N, N_Conditional_Entry_Call,
6279 N_If_Statement,
6280 N_Selective_Accept)
6281 and then not Is_Empty_List (Else_Statements (N))
6282 and then not Are_Wrapped (Else_Statements (N))
6283 and then Requires_Cleanup_Actions
6284 (Else_Statements (N), False, False)
6285 then
6286 Block := Wrap_Statements_In_Block (Else_Statements (N));
6287 Set_Else_Statements (N, New_List (Block));
6289 Analyze (Block);
6290 end if;
6292 when N_Abortable_Part |
6293 N_Accept_Alternative |
6294 N_Case_Statement_Alternative |
6295 N_Delay_Alternative |
6296 N_Entry_Call_Alternative |
6297 N_Exception_Handler |
6298 N_Loop_Statement |
6299 N_Triggering_Alternative =>
6301 if not Is_Empty_List (Statements (N))
6302 and then not Are_Wrapped (Statements (N))
6303 and then Requires_Cleanup_Actions (Statements (N), False, False)
6304 then
6305 Block := Wrap_Statements_In_Block (Statements (N));
6306 Set_Statements (N, New_List (Block));
6308 Analyze (Block);
6309 end if;
6311 when others =>
6312 null;
6313 end case;
6314 end Process_Statements_For_Controlled_Objects;
6316 ----------------------
6317 -- Remove_Init_Call --
6318 ----------------------
6320 function Remove_Init_Call
6321 (Var : Entity_Id;
6322 Rep_Clause : Node_Id) return Node_Id
6324 Par : constant Node_Id := Parent (Var);
6325 Typ : constant Entity_Id := Etype (Var);
6327 Init_Proc : Entity_Id;
6328 -- Initialization procedure for Typ
6330 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
6331 -- Look for init call for Var starting at From and scanning the
6332 -- enclosing list until Rep_Clause or the end of the list is reached.
6334 ----------------------------
6335 -- Find_Init_Call_In_List --
6336 ----------------------------
6338 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
6339 Init_Call : Node_Id;
6341 begin
6342 Init_Call := From;
6343 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
6344 if Nkind (Init_Call) = N_Procedure_Call_Statement
6345 and then Is_Entity_Name (Name (Init_Call))
6346 and then Entity (Name (Init_Call)) = Init_Proc
6347 then
6348 return Init_Call;
6349 end if;
6351 Next (Init_Call);
6352 end loop;
6354 return Empty;
6355 end Find_Init_Call_In_List;
6357 Init_Call : Node_Id;
6359 -- Start of processing for Find_Init_Call
6361 begin
6362 if Present (Initialization_Statements (Var)) then
6363 Init_Call := Initialization_Statements (Var);
6364 Set_Initialization_Statements (Var, Empty);
6366 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
6368 -- No init proc for the type, so obviously no call to be found
6370 return Empty;
6372 else
6373 -- We might be able to handle other cases below by just properly
6374 -- setting Initialization_Statements at the point where the init proc
6375 -- call is generated???
6377 Init_Proc := Base_Init_Proc (Typ);
6379 -- First scan the list containing the declaration of Var
6381 Init_Call := Find_Init_Call_In_List (From => Next (Par));
6383 -- If not found, also look on Var's freeze actions list, if any,
6384 -- since the init call may have been moved there (case of an address
6385 -- clause applying to Var).
6387 if No (Init_Call) and then Present (Freeze_Node (Var)) then
6388 Init_Call :=
6389 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
6390 end if;
6392 -- If the initialization call has actuals that use the secondary
6393 -- stack, the call may have been wrapped into a temporary block, in
6394 -- which case the block itself has to be removed.
6396 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
6397 declare
6398 Blk : constant Node_Id := Next (Par);
6399 begin
6400 if Present
6401 (Find_Init_Call_In_List
6402 (First (Statements (Handled_Statement_Sequence (Blk)))))
6403 then
6404 Init_Call := Blk;
6405 end if;
6406 end;
6407 end if;
6408 end if;
6410 if Present (Init_Call) then
6411 Remove (Init_Call);
6412 end if;
6413 return Init_Call;
6414 end Remove_Init_Call;
6416 -------------------------
6417 -- Remove_Side_Effects --
6418 -------------------------
6420 procedure Remove_Side_Effects
6421 (Exp : Node_Id;
6422 Name_Req : Boolean := False;
6423 Variable_Ref : Boolean := False)
6425 Loc : constant Source_Ptr := Sloc (Exp);
6426 Exp_Type : constant Entity_Id := Etype (Exp);
6427 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
6428 Def_Id : Entity_Id;
6429 E : Node_Id;
6430 New_Exp : Node_Id;
6431 Ptr_Typ_Decl : Node_Id;
6432 Ref_Type : Entity_Id;
6433 Res : Node_Id;
6435 function Side_Effect_Free (N : Node_Id) return Boolean;
6436 -- Determines if the tree N represents an expression that is known not
6437 -- to have side effects, and for which no processing is required.
6439 function Side_Effect_Free (L : List_Id) return Boolean;
6440 -- Determines if all elements of the list L are side effect free
6442 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
6443 -- The argument N is a construct where the Prefix is dereferenced if it
6444 -- is an access type and the result is a variable. The call returns True
6445 -- if the construct is side effect free (not considering side effects in
6446 -- other than the prefix which are to be tested by the caller).
6448 function Within_In_Parameter (N : Node_Id) return Boolean;
6449 -- Determines if N is a subcomponent of a composite in-parameter. If so,
6450 -- N is not side-effect free when the actual is global and modifiable
6451 -- indirectly from within a subprogram, because it may be passed by
6452 -- reference. The front-end must be conservative here and assume that
6453 -- this may happen with any array or record type. On the other hand, we
6454 -- cannot create temporaries for all expressions for which this
6455 -- condition is true, for various reasons that might require clearing up
6456 -- ??? For example, discriminant references that appear out of place, or
6457 -- spurious type errors with class-wide expressions. As a result, we
6458 -- limit the transformation to loop bounds, which is so far the only
6459 -- case that requires it.
6461 -----------------------------
6462 -- Safe_Prefixed_Reference --
6463 -----------------------------
6465 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
6466 begin
6467 -- If prefix is not side effect free, definitely not safe
6469 if not Side_Effect_Free (Prefix (N)) then
6470 return False;
6472 -- If the prefix is of an access type that is not access-to-constant,
6473 -- then this construct is a variable reference, which means it is to
6474 -- be considered to have side effects if Variable_Ref is set True.
6476 elsif Is_Access_Type (Etype (Prefix (N)))
6477 and then not Is_Access_Constant (Etype (Prefix (N)))
6478 and then Variable_Ref
6479 then
6480 -- Exception is a prefix that is the result of a previous removal
6481 -- of side-effects.
6483 return Is_Entity_Name (Prefix (N))
6484 and then not Comes_From_Source (Prefix (N))
6485 and then Ekind (Entity (Prefix (N))) = E_Constant
6486 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
6488 -- If the prefix is an explicit dereference then this construct is a
6489 -- variable reference, which means it is to be considered to have
6490 -- side effects if Variable_Ref is True.
6492 -- We do NOT exclude dereferences of access-to-constant types because
6493 -- we handle them as constant view of variables.
6495 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
6496 and then Variable_Ref
6497 then
6498 return False;
6500 -- Note: The following test is the simplest way of solving a complex
6501 -- problem uncovered by the following test (Side effect on loop bound
6502 -- that is a subcomponent of a global variable:
6504 -- with Text_Io; use Text_Io;
6505 -- procedure Tloop is
6506 -- type X is
6507 -- record
6508 -- V : Natural := 4;
6509 -- S : String (1..5) := (others => 'a');
6510 -- end record;
6511 -- X1 : X;
6513 -- procedure Modi;
6515 -- generic
6516 -- with procedure Action;
6517 -- procedure Loop_G (Arg : X; Msg : String)
6519 -- procedure Loop_G (Arg : X; Msg : String) is
6520 -- begin
6521 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
6522 -- & Natural'Image (Arg.V));
6523 -- for Index in 1 .. Arg.V loop
6524 -- Text_Io.Put_Line
6525 -- (Natural'Image (Index) & " " & Arg.S (Index));
6526 -- if Index > 2 then
6527 -- Modi;
6528 -- end if;
6529 -- end loop;
6530 -- Put_Line ("end loop_g " & Msg);
6531 -- end;
6533 -- procedure Loop1 is new Loop_G (Modi);
6534 -- procedure Modi is
6535 -- begin
6536 -- X1.V := 1;
6537 -- Loop1 (X1, "from modi");
6538 -- end;
6540 -- begin
6541 -- Loop1 (X1, "initial");
6542 -- end;
6544 -- The output of the above program should be:
6546 -- begin loop_g initial will loop till: 4
6547 -- 1 a
6548 -- 2 a
6549 -- 3 a
6550 -- begin loop_g from modi will loop till: 1
6551 -- 1 a
6552 -- end loop_g from modi
6553 -- 4 a
6554 -- begin loop_g from modi will loop till: 1
6555 -- 1 a
6556 -- end loop_g from modi
6557 -- end loop_g initial
6559 -- If a loop bound is a subcomponent of a global variable, a
6560 -- modification of that variable within the loop may incorrectly
6561 -- affect the execution of the loop.
6563 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
6564 and then Within_In_Parameter (Prefix (N))
6565 and then Variable_Ref
6566 then
6567 return False;
6569 -- All other cases are side effect free
6571 else
6572 return True;
6573 end if;
6574 end Safe_Prefixed_Reference;
6576 ----------------------
6577 -- Side_Effect_Free --
6578 ----------------------
6580 function Side_Effect_Free (N : Node_Id) return Boolean is
6581 begin
6582 -- Note on checks that could raise Constraint_Error. Strictly, if we
6583 -- take advantage of 11.6, these checks do not count as side effects.
6584 -- However, we would prefer to consider that they are side effects,
6585 -- since the backend CSE does not work very well on expressions which
6586 -- can raise Constraint_Error. On the other hand if we don't consider
6587 -- them to be side effect free, then we get some awkward expansions
6588 -- in -gnato mode, resulting in code insertions at a point where we
6589 -- do not have a clear model for performing the insertions.
6591 -- Special handling for entity names
6593 if Is_Entity_Name (N) then
6595 -- Variables are considered to be a side effect if Variable_Ref
6596 -- is set or if we have a volatile reference and Name_Req is off.
6597 -- If Name_Req is True then we can't help returning a name which
6598 -- effectively allows multiple references in any case.
6600 if Is_Variable (N, Use_Original_Node => False) then
6601 return not Variable_Ref
6602 and then (not Is_Volatile_Reference (N) or else Name_Req);
6604 -- Any other entity (e.g. a subtype name) is definitely side
6605 -- effect free.
6607 else
6608 return True;
6609 end if;
6611 -- A value known at compile time is always side effect free
6613 elsif Compile_Time_Known_Value (N) then
6614 return True;
6616 -- A variable renaming is not side-effect free, because the renaming
6617 -- will function like a macro in the front-end in some cases, and an
6618 -- assignment can modify the component designated by N, so we need to
6619 -- create a temporary for it.
6621 -- The guard testing for Entity being present is needed at least in
6622 -- the case of rewritten predicate expressions, and may well also be
6623 -- appropriate elsewhere. Obviously we can't go testing the entity
6624 -- field if it does not exist, so it's reasonable to say that this is
6625 -- not the renaming case if it does not exist.
6627 elsif Is_Entity_Name (Original_Node (N))
6628 and then Present (Entity (Original_Node (N)))
6629 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
6630 and then Ekind (Entity (Original_Node (N))) /= E_Constant
6631 then
6632 declare
6633 RO : constant Node_Id :=
6634 Renamed_Object (Entity (Original_Node (N)));
6636 begin
6637 -- If the renamed object is an indexed component, or an
6638 -- explicit dereference, then the designated object could
6639 -- be modified by an assignment.
6641 if Nkind_In (RO, N_Indexed_Component,
6642 N_Explicit_Dereference)
6643 then
6644 return False;
6646 -- A selected component must have a safe prefix
6648 elsif Nkind (RO) = N_Selected_Component then
6649 return Safe_Prefixed_Reference (RO);
6651 -- In all other cases, designated object cannot be changed so
6652 -- we are side effect free.
6654 else
6655 return True;
6656 end if;
6657 end;
6659 -- Remove_Side_Effects generates an object renaming declaration to
6660 -- capture the expression of a class-wide expression. In VM targets
6661 -- the frontend performs no expansion for dispatching calls to
6662 -- class- wide types since they are handled by the VM. Hence, we must
6663 -- locate here if this node corresponds to a previous invocation of
6664 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
6666 elsif VM_Target /= No_VM
6667 and then not Comes_From_Source (N)
6668 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
6669 and then Is_Class_Wide_Type (Etype (N))
6670 then
6671 return True;
6672 end if;
6674 -- For other than entity names and compile time known values,
6675 -- check the node kind for special processing.
6677 case Nkind (N) is
6679 -- An attribute reference is side effect free if its expressions
6680 -- are side effect free and its prefix is side effect free or
6681 -- is an entity reference.
6683 -- Is this right? what about x'first where x is a variable???
6685 when N_Attribute_Reference =>
6686 return Side_Effect_Free (Expressions (N))
6687 and then Attribute_Name (N) /= Name_Input
6688 and then (Is_Entity_Name (Prefix (N))
6689 or else Side_Effect_Free (Prefix (N)));
6691 -- A binary operator is side effect free if and both operands are
6692 -- side effect free. For this purpose binary operators include
6693 -- membership tests and short circuit forms.
6695 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
6696 return Side_Effect_Free (Left_Opnd (N))
6697 and then
6698 Side_Effect_Free (Right_Opnd (N));
6700 -- An explicit dereference is side effect free only if it is
6701 -- a side effect free prefixed reference.
6703 when N_Explicit_Dereference =>
6704 return Safe_Prefixed_Reference (N);
6706 -- An expression with action is side effect free if its expression
6707 -- is side effect free and it has no actions.
6709 when N_Expression_With_Actions =>
6710 return Is_Empty_List (Actions (N))
6711 and then
6712 Side_Effect_Free (Expression (N));
6714 -- A call to _rep_to_pos is side effect free, since we generate
6715 -- this pure function call ourselves. Moreover it is critically
6716 -- important to make this exception, since otherwise we can have
6717 -- discriminants in array components which don't look side effect
6718 -- free in the case of an array whose index type is an enumeration
6719 -- type with an enumeration rep clause.
6721 -- All other function calls are not side effect free
6723 when N_Function_Call =>
6724 return Nkind (Name (N)) = N_Identifier
6725 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
6726 and then
6727 Side_Effect_Free (First (Parameter_Associations (N)));
6729 -- An indexed component is side effect free if it is a side
6730 -- effect free prefixed reference and all the indexing
6731 -- expressions are side effect free.
6733 when N_Indexed_Component =>
6734 return Side_Effect_Free (Expressions (N))
6735 and then Safe_Prefixed_Reference (N);
6737 -- A type qualification is side effect free if the expression
6738 -- is side effect free.
6740 when N_Qualified_Expression =>
6741 return Side_Effect_Free (Expression (N));
6743 -- A selected component is side effect free only if it is a side
6744 -- effect free prefixed reference. If it designates a component
6745 -- with a rep. clause it must be treated has having a potential
6746 -- side effect, because it may be modified through a renaming, and
6747 -- a subsequent use of the renaming as a macro will yield the
6748 -- wrong value. This complex interaction between renaming and
6749 -- removing side effects is a reminder that the latter has become
6750 -- a headache to maintain, and that it should be removed in favor
6751 -- of the gcc mechanism to capture values ???
6753 when N_Selected_Component =>
6754 if Nkind (Parent (N)) = N_Explicit_Dereference
6755 and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
6756 then
6757 return False;
6758 else
6759 return Safe_Prefixed_Reference (N);
6760 end if;
6762 -- A range is side effect free if the bounds are side effect free
6764 when N_Range =>
6765 return Side_Effect_Free (Low_Bound (N))
6766 and then Side_Effect_Free (High_Bound (N));
6768 -- A slice is side effect free if it is a side effect free
6769 -- prefixed reference and the bounds are side effect free.
6771 when N_Slice =>
6772 return Side_Effect_Free (Discrete_Range (N))
6773 and then Safe_Prefixed_Reference (N);
6775 -- A type conversion is side effect free if the expression to be
6776 -- converted is side effect free.
6778 when N_Type_Conversion =>
6779 return Side_Effect_Free (Expression (N));
6781 -- A unary operator is side effect free if the operand
6782 -- is side effect free.
6784 when N_Unary_Op =>
6785 return Side_Effect_Free (Right_Opnd (N));
6787 -- An unchecked type conversion is side effect free only if it
6788 -- is safe and its argument is side effect free.
6790 when N_Unchecked_Type_Conversion =>
6791 return Safe_Unchecked_Type_Conversion (N)
6792 and then Side_Effect_Free (Expression (N));
6794 -- An unchecked expression is side effect free if its expression
6795 -- is side effect free.
6797 when N_Unchecked_Expression =>
6798 return Side_Effect_Free (Expression (N));
6800 -- A literal is side effect free
6802 when N_Character_Literal |
6803 N_Integer_Literal |
6804 N_Real_Literal |
6805 N_String_Literal =>
6806 return True;
6808 -- We consider that anything else has side effects. This is a bit
6809 -- crude, but we are pretty close for most common cases, and we
6810 -- are certainly correct (i.e. we never return True when the
6811 -- answer should be False).
6813 when others =>
6814 return False;
6815 end case;
6816 end Side_Effect_Free;
6818 -- A list is side effect free if all elements of the list are side
6819 -- effect free.
6821 function Side_Effect_Free (L : List_Id) return Boolean is
6822 N : Node_Id;
6824 begin
6825 if L = No_List or else L = Error_List then
6826 return True;
6828 else
6829 N := First (L);
6830 while Present (N) loop
6831 if not Side_Effect_Free (N) then
6832 return False;
6833 else
6834 Next (N);
6835 end if;
6836 end loop;
6838 return True;
6839 end if;
6840 end Side_Effect_Free;
6842 -------------------------
6843 -- Within_In_Parameter --
6844 -------------------------
6846 function Within_In_Parameter (N : Node_Id) return Boolean is
6847 begin
6848 if not Comes_From_Source (N) then
6849 return False;
6851 elsif Is_Entity_Name (N) then
6852 return Ekind (Entity (N)) = E_In_Parameter;
6854 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
6855 return Within_In_Parameter (Prefix (N));
6857 else
6858 return False;
6859 end if;
6860 end Within_In_Parameter;
6862 -- Start of processing for Remove_Side_Effects
6864 begin
6865 -- Handle cases in which there is nothing to do
6867 if not Expander_Active then
6868 return;
6869 end if;
6871 -- Cannot generate temporaries if the invocation to remove side effects
6872 -- was issued too early and the type of the expression is not resolved
6873 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
6874 -- Remove_Side_Effects).
6876 if No (Exp_Type)
6877 or else Ekind (Exp_Type) = E_Access_Attribute_Type
6878 then
6879 return;
6881 -- No action needed for side-effect free expressions
6883 elsif Side_Effect_Free (Exp) then
6884 return;
6885 end if;
6887 -- The remaining procesaing is done with all checks suppressed
6889 -- Note: from now on, don't use return statements, instead do a goto
6890 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
6892 Scope_Suppress.Suppress := (others => True);
6894 -- If it is a scalar type and we need to capture the value, just make
6895 -- a copy. Likewise for a function call, an attribute reference, an
6896 -- allocator, or an operator. And if we have a volatile reference and
6897 -- Name_Req is not set (see comments above for Side_Effect_Free).
6899 if Is_Elementary_Type (Exp_Type)
6900 and then (Variable_Ref
6901 or else Nkind_In (Exp, N_Function_Call,
6902 N_Attribute_Reference,
6903 N_Allocator)
6904 or else Nkind (Exp) in N_Op
6905 or else (not Name_Req and then Is_Volatile_Reference (Exp)))
6906 then
6907 Def_Id := Make_Temporary (Loc, 'R', Exp);
6908 Set_Etype (Def_Id, Exp_Type);
6909 Res := New_Reference_To (Def_Id, Loc);
6911 -- If the expression is a packed reference, it must be reanalyzed and
6912 -- expanded, depending on context. This is the case for actuals where
6913 -- a constraint check may capture the actual before expansion of the
6914 -- call is complete.
6916 if Nkind (Exp) = N_Indexed_Component
6917 and then Is_Packed (Etype (Prefix (Exp)))
6918 then
6919 Set_Analyzed (Exp, False);
6920 Set_Analyzed (Prefix (Exp), False);
6921 end if;
6923 E :=
6924 Make_Object_Declaration (Loc,
6925 Defining_Identifier => Def_Id,
6926 Object_Definition => New_Reference_To (Exp_Type, Loc),
6927 Constant_Present => True,
6928 Expression => Relocate_Node (Exp));
6930 Set_Assignment_OK (E);
6931 Insert_Action (Exp, E);
6933 -- If the expression has the form v.all then we can just capture the
6934 -- pointer, and then do an explicit dereference on the result.
6936 elsif Nkind (Exp) = N_Explicit_Dereference then
6937 Def_Id := Make_Temporary (Loc, 'R', Exp);
6938 Res :=
6939 Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
6941 Insert_Action (Exp,
6942 Make_Object_Declaration (Loc,
6943 Defining_Identifier => Def_Id,
6944 Object_Definition =>
6945 New_Reference_To (Etype (Prefix (Exp)), Loc),
6946 Constant_Present => True,
6947 Expression => Relocate_Node (Prefix (Exp))));
6949 -- Similar processing for an unchecked conversion of an expression of
6950 -- the form v.all, where we want the same kind of treatment.
6952 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
6953 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
6954 then
6955 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
6956 goto Leave;
6958 -- If this is a type conversion, leave the type conversion and remove
6959 -- the side effects in the expression. This is important in several
6960 -- circumstances: for change of representations, and also when this is a
6961 -- view conversion to a smaller object, where gigi can end up creating
6962 -- its own temporary of the wrong size.
6964 elsif Nkind (Exp) = N_Type_Conversion then
6965 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
6966 goto Leave;
6968 -- If this is an unchecked conversion that Gigi can't handle, make
6969 -- a copy or a use a renaming to capture the value.
6971 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
6972 and then not Safe_Unchecked_Type_Conversion (Exp)
6973 then
6974 if CW_Or_Has_Controlled_Part (Exp_Type) then
6976 -- Use a renaming to capture the expression, rather than create
6977 -- a controlled temporary.
6979 Def_Id := Make_Temporary (Loc, 'R', Exp);
6980 Res := New_Reference_To (Def_Id, Loc);
6982 Insert_Action (Exp,
6983 Make_Object_Renaming_Declaration (Loc,
6984 Defining_Identifier => Def_Id,
6985 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
6986 Name => Relocate_Node (Exp)));
6988 else
6989 Def_Id := Make_Temporary (Loc, 'R', Exp);
6990 Set_Etype (Def_Id, Exp_Type);
6991 Res := New_Reference_To (Def_Id, Loc);
6993 E :=
6994 Make_Object_Declaration (Loc,
6995 Defining_Identifier => Def_Id,
6996 Object_Definition => New_Reference_To (Exp_Type, Loc),
6997 Constant_Present => not Is_Variable (Exp),
6998 Expression => Relocate_Node (Exp));
7000 Set_Assignment_OK (E);
7001 Insert_Action (Exp, E);
7002 end if;
7004 -- For expressions that denote objects, we can use a renaming scheme.
7005 -- This is needed for correctness in the case of a volatile object of
7006 -- a non-volatile type because the Make_Reference call of the "default"
7007 -- approach would generate an illegal access value (an access value
7008 -- cannot designate such an object - see Analyze_Reference). We skip
7009 -- using this scheme if we have an object of a volatile type and we do
7010 -- not have Name_Req set true (see comments above for Side_Effect_Free).
7012 -- In Ada 2012 a qualified expression is an object, but for purposes of
7013 -- removing side effects it still need to be transformed into a separate
7014 -- declaration, particularly if the expression is an aggregate.
7016 elsif Is_Object_Reference (Exp)
7017 and then Nkind (Exp) /= N_Function_Call
7018 and then Nkind (Exp) /= N_Qualified_Expression
7019 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
7020 then
7021 Def_Id := Make_Temporary (Loc, 'R', Exp);
7023 if Nkind (Exp) = N_Selected_Component
7024 and then Nkind (Prefix (Exp)) = N_Function_Call
7025 and then Is_Array_Type (Exp_Type)
7026 then
7027 -- Avoid generating a variable-sized temporary, by generating
7028 -- the renaming declaration just for the function call. The
7029 -- transformation could be refined to apply only when the array
7030 -- component is constrained by a discriminant???
7032 Res :=
7033 Make_Selected_Component (Loc,
7034 Prefix => New_Occurrence_Of (Def_Id, Loc),
7035 Selector_Name => Selector_Name (Exp));
7037 Insert_Action (Exp,
7038 Make_Object_Renaming_Declaration (Loc,
7039 Defining_Identifier => Def_Id,
7040 Subtype_Mark =>
7041 New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
7042 Name => Relocate_Node (Prefix (Exp))));
7044 else
7045 Res := New_Reference_To (Def_Id, Loc);
7047 Insert_Action (Exp,
7048 Make_Object_Renaming_Declaration (Loc,
7049 Defining_Identifier => Def_Id,
7050 Subtype_Mark => New_Reference_To (Exp_Type, Loc),
7051 Name => Relocate_Node (Exp)));
7052 end if;
7054 -- If this is a packed reference, or a selected component with
7055 -- a non-standard representation, a reference to the temporary
7056 -- will be replaced by a copy of the original expression (see
7057 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
7058 -- elaborated by gigi, and is of course not to be replaced in-line
7059 -- by the expression it renames, which would defeat the purpose of
7060 -- removing the side-effect.
7062 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
7063 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
7064 then
7065 null;
7066 else
7067 Set_Is_Renaming_Of_Object (Def_Id, False);
7068 end if;
7070 -- Otherwise we generate a reference to the value
7072 else
7073 -- An expression which is in SPARK mode is considered side effect
7074 -- free if the resulting value is captured by a variable or a
7075 -- constant.
7077 if SPARK_Mode
7078 and then Nkind (Parent (Exp)) = N_Object_Declaration
7079 then
7080 goto Leave;
7081 end if;
7083 -- Special processing for function calls that return a limited type.
7084 -- We need to build a declaration that will enable build-in-place
7085 -- expansion of the call. This is not done if the context is already
7086 -- an object declaration, to prevent infinite recursion.
7088 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
7089 -- to accommodate functions returning limited objects by reference.
7091 if Ada_Version >= Ada_2005
7092 and then Nkind (Exp) = N_Function_Call
7093 and then Is_Limited_View (Etype (Exp))
7094 and then Nkind (Parent (Exp)) /= N_Object_Declaration
7095 then
7096 declare
7097 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
7098 Decl : Node_Id;
7100 begin
7101 Decl :=
7102 Make_Object_Declaration (Loc,
7103 Defining_Identifier => Obj,
7104 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7105 Expression => Relocate_Node (Exp));
7107 Insert_Action (Exp, Decl);
7108 Set_Etype (Obj, Exp_Type);
7109 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
7110 goto Leave;
7111 end;
7112 end if;
7114 Def_Id := Make_Temporary (Loc, 'R', Exp);
7116 -- The regular expansion of functions with side effects involves the
7117 -- generation of an access type to capture the return value found on
7118 -- the secondary stack. Since SPARK (and why) cannot process access
7119 -- types, use a different approach which ignores the secondary stack
7120 -- and "copies" the returned object.
7122 if SPARK_Mode then
7123 Res := New_Reference_To (Def_Id, Loc);
7124 Ref_Type := Exp_Type;
7126 -- Regular expansion utilizing an access type and 'reference
7128 else
7129 Res :=
7130 Make_Explicit_Dereference (Loc,
7131 Prefix => New_Reference_To (Def_Id, Loc));
7133 -- Generate:
7134 -- type Ann is access all <Exp_Type>;
7136 Ref_Type := Make_Temporary (Loc, 'A');
7138 Ptr_Typ_Decl :=
7139 Make_Full_Type_Declaration (Loc,
7140 Defining_Identifier => Ref_Type,
7141 Type_Definition =>
7142 Make_Access_To_Object_Definition (Loc,
7143 All_Present => True,
7144 Subtype_Indication =>
7145 New_Reference_To (Exp_Type, Loc)));
7147 Insert_Action (Exp, Ptr_Typ_Decl);
7148 end if;
7150 E := Exp;
7151 if Nkind (E) = N_Explicit_Dereference then
7152 New_Exp := Relocate_Node (Prefix (E));
7153 else
7154 E := Relocate_Node (E);
7156 -- Do not generate a 'reference in SPARK mode since the access
7157 -- type is not created in the first place.
7159 if SPARK_Mode then
7160 New_Exp := E;
7162 -- Otherwise generate reference, marking the value as non-null
7163 -- since we know it cannot be null and we don't want a check.
7165 else
7166 New_Exp := Make_Reference (Loc, E);
7167 Set_Is_Known_Non_Null (Def_Id);
7168 end if;
7169 end if;
7171 if Is_Delayed_Aggregate (E) then
7173 -- The expansion of nested aggregates is delayed until the
7174 -- enclosing aggregate is expanded. As aggregates are often
7175 -- qualified, the predicate applies to qualified expressions as
7176 -- well, indicating that the enclosing aggregate has not been
7177 -- expanded yet. At this point the aggregate is part of a
7178 -- stand-alone declaration, and must be fully expanded.
7180 if Nkind (E) = N_Qualified_Expression then
7181 Set_Expansion_Delayed (Expression (E), False);
7182 Set_Analyzed (Expression (E), False);
7183 else
7184 Set_Expansion_Delayed (E, False);
7185 end if;
7187 Set_Analyzed (E, False);
7188 end if;
7190 Insert_Action (Exp,
7191 Make_Object_Declaration (Loc,
7192 Defining_Identifier => Def_Id,
7193 Object_Definition => New_Reference_To (Ref_Type, Loc),
7194 Constant_Present => True,
7195 Expression => New_Exp));
7196 end if;
7198 -- Preserve the Assignment_OK flag in all copies, since at least one
7199 -- copy may be used in a context where this flag must be set (otherwise
7200 -- why would the flag be set in the first place).
7202 Set_Assignment_OK (Res, Assignment_OK (Exp));
7204 -- Finally rewrite the original expression and we are done
7206 Rewrite (Exp, Res);
7207 Analyze_And_Resolve (Exp, Exp_Type);
7209 <<Leave>>
7210 Scope_Suppress := Svg_Suppress;
7211 end Remove_Side_Effects;
7213 ---------------------------
7214 -- Represented_As_Scalar --
7215 ---------------------------
7217 function Represented_As_Scalar (T : Entity_Id) return Boolean is
7218 UT : constant Entity_Id := Underlying_Type (T);
7219 begin
7220 return Is_Scalar_Type (UT)
7221 or else (Is_Bit_Packed_Array (UT)
7222 and then Is_Scalar_Type (Packed_Array_Type (UT)));
7223 end Represented_As_Scalar;
7225 ------------------------------
7226 -- Requires_Cleanup_Actions --
7227 ------------------------------
7229 function Requires_Cleanup_Actions
7230 (N : Node_Id;
7231 Lib_Level : Boolean) return Boolean
7233 At_Lib_Level : constant Boolean :=
7234 Lib_Level
7235 and then Nkind_In (N, N_Package_Body,
7236 N_Package_Specification);
7237 -- N is at the library level if the top-most context is a package and
7238 -- the path taken to reach N does not inlcude non-package constructs.
7240 begin
7241 case Nkind (N) is
7242 when N_Accept_Statement |
7243 N_Block_Statement |
7244 N_Entry_Body |
7245 N_Package_Body |
7246 N_Protected_Body |
7247 N_Subprogram_Body |
7248 N_Task_Body =>
7249 return
7250 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
7251 or else
7252 (Present (Handled_Statement_Sequence (N))
7253 and then
7254 Requires_Cleanup_Actions
7255 (Statements (Handled_Statement_Sequence (N)),
7256 At_Lib_Level, True));
7258 when N_Package_Specification =>
7259 return
7260 Requires_Cleanup_Actions
7261 (Visible_Declarations (N), At_Lib_Level, True)
7262 or else
7263 Requires_Cleanup_Actions
7264 (Private_Declarations (N), At_Lib_Level, True);
7266 when others =>
7267 return False;
7268 end case;
7269 end Requires_Cleanup_Actions;
7271 ------------------------------
7272 -- Requires_Cleanup_Actions --
7273 ------------------------------
7275 function Requires_Cleanup_Actions
7276 (L : List_Id;
7277 Lib_Level : Boolean;
7278 Nested_Constructs : Boolean) return Boolean
7280 Decl : Node_Id;
7281 Expr : Node_Id;
7282 Obj_Id : Entity_Id;
7283 Obj_Typ : Entity_Id;
7284 Pack_Id : Entity_Id;
7285 Typ : Entity_Id;
7287 begin
7288 if No (L)
7289 or else Is_Empty_List (L)
7290 then
7291 return False;
7292 end if;
7294 Decl := First (L);
7295 while Present (Decl) loop
7297 -- Library-level tagged types
7299 if Nkind (Decl) = N_Full_Type_Declaration then
7300 Typ := Defining_Identifier (Decl);
7302 if Is_Tagged_Type (Typ)
7303 and then Is_Library_Level_Entity (Typ)
7304 and then Convention (Typ) = Convention_Ada
7305 and then Present (Access_Disp_Table (Typ))
7306 and then RTE_Available (RE_Unregister_Tag)
7307 and then not No_Run_Time_Mode
7308 and then not Is_Abstract_Type (Typ)
7309 then
7310 return True;
7311 end if;
7313 -- Regular object declarations
7315 elsif Nkind (Decl) = N_Object_Declaration then
7316 Obj_Id := Defining_Identifier (Decl);
7317 Obj_Typ := Base_Type (Etype (Obj_Id));
7318 Expr := Expression (Decl);
7320 -- Bypass any form of processing for objects which have their
7321 -- finalization disabled. This applies only to objects at the
7322 -- library level.
7324 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7325 null;
7327 -- Transient variables are treated separately in order to minimize
7328 -- the size of the generated code. See Exp_Ch7.Process_Transient_
7329 -- Objects.
7331 elsif Is_Processed_Transient (Obj_Id) then
7332 null;
7334 -- The object is of the form:
7335 -- Obj : Typ [:= Expr];
7337 -- Do not process the incomplete view of a deferred constant. Do
7338 -- not consider tag-to-class-wide conversions.
7340 elsif not Is_Imported (Obj_Id)
7341 and then Needs_Finalization (Obj_Typ)
7342 and then not (Ekind (Obj_Id) = E_Constant
7343 and then not Has_Completion (Obj_Id))
7344 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
7345 then
7346 return True;
7348 -- The object is of the form:
7349 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
7351 -- Obj : Access_Typ :=
7352 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
7354 elsif Is_Access_Type (Obj_Typ)
7355 and then Needs_Finalization
7356 (Available_View (Designated_Type (Obj_Typ)))
7357 and then Present (Expr)
7358 and then
7359 (Is_Secondary_Stack_BIP_Func_Call (Expr)
7360 or else
7361 (Is_Non_BIP_Func_Call (Expr)
7362 and then not Is_Related_To_Func_Return (Obj_Id)))
7363 then
7364 return True;
7366 -- Processing for "hook" objects generated for controlled
7367 -- transients declared inside an Expression_With_Actions.
7369 elsif Is_Access_Type (Obj_Typ)
7370 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7371 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7372 N_Object_Declaration
7373 and then Is_Finalizable_Transient
7374 (Status_Flag_Or_Transient_Decl (Obj_Id), Decl)
7375 then
7376 return True;
7378 -- Processing for intermediate results of if expressions where
7379 -- one of the alternatives uses a controlled function call.
7381 elsif Is_Access_Type (Obj_Typ)
7382 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7383 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7384 N_Defining_Identifier
7385 and then Present (Expr)
7386 and then Nkind (Expr) = N_Null
7387 then
7388 return True;
7390 -- Simple protected objects which use type System.Tasking.
7391 -- Protected_Objects.Protection to manage their locks should be
7392 -- treated as controlled since they require manual cleanup.
7394 elsif Ekind (Obj_Id) = E_Variable
7395 and then
7396 (Is_Simple_Protected_Type (Obj_Typ)
7397 or else Has_Simple_Protected_Object (Obj_Typ))
7398 then
7399 return True;
7400 end if;
7402 -- Specific cases of object renamings
7404 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
7405 Obj_Id := Defining_Identifier (Decl);
7406 Obj_Typ := Base_Type (Etype (Obj_Id));
7408 -- Bypass any form of processing for objects which have their
7409 -- finalization disabled. This applies only to objects at the
7410 -- library level.
7412 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7413 null;
7415 -- Return object of a build-in-place function. This case is
7416 -- recognized and marked by the expansion of an extended return
7417 -- statement (see Expand_N_Extended_Return_Statement).
7419 elsif Needs_Finalization (Obj_Typ)
7420 and then Is_Return_Object (Obj_Id)
7421 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7422 then
7423 return True;
7425 -- Detect a case where a source object has been initialized by
7426 -- a controlled function call or another object which was later
7427 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
7429 -- Obj1 : CW_Type := Src_Obj;
7430 -- Obj2 : CW_Type := Function_Call (...);
7432 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7433 -- Tmp : ... := Function_Call (...)'reference;
7434 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
7436 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
7437 return True;
7438 end if;
7440 -- Inspect the freeze node of an access-to-controlled type and look
7441 -- for a delayed finalization master. This case arises when the
7442 -- freeze actions are inserted at a later time than the expansion of
7443 -- the context. Since Build_Finalizer is never called on a single
7444 -- construct twice, the master will be ultimately left out and never
7445 -- finalized. This is also needed for freeze actions of designated
7446 -- types themselves, since in some cases the finalization master is
7447 -- associated with a designated type's freeze node rather than that
7448 -- of the access type (see handling for freeze actions in
7449 -- Build_Finalization_Master).
7451 elsif Nkind (Decl) = N_Freeze_Entity
7452 and then Present (Actions (Decl))
7453 then
7454 Typ := Entity (Decl);
7456 if ((Is_Access_Type (Typ)
7457 and then not Is_Access_Subprogram_Type (Typ)
7458 and then Needs_Finalization
7459 (Available_View (Designated_Type (Typ))))
7460 or else
7461 (Is_Type (Typ)
7462 and then Needs_Finalization (Typ)))
7463 and then Requires_Cleanup_Actions
7464 (Actions (Decl), Lib_Level, Nested_Constructs)
7465 then
7466 return True;
7467 end if;
7469 -- Nested package declarations
7471 elsif Nested_Constructs
7472 and then Nkind (Decl) = N_Package_Declaration
7473 then
7474 Pack_Id := Defining_Unit_Name (Specification (Decl));
7476 if Nkind (Pack_Id) = N_Defining_Program_Unit_Name then
7477 Pack_Id := Defining_Identifier (Pack_Id);
7478 end if;
7480 if Ekind (Pack_Id) /= E_Generic_Package
7481 and then
7482 Requires_Cleanup_Actions (Specification (Decl), Lib_Level)
7483 then
7484 return True;
7485 end if;
7487 -- Nested package bodies
7489 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
7490 Pack_Id := Corresponding_Spec (Decl);
7492 if Ekind (Pack_Id) /= E_Generic_Package
7493 and then Requires_Cleanup_Actions (Decl, Lib_Level)
7494 then
7495 return True;
7496 end if;
7497 end if;
7499 Next (Decl);
7500 end loop;
7502 return False;
7503 end Requires_Cleanup_Actions;
7505 ------------------------------------
7506 -- Safe_Unchecked_Type_Conversion --
7507 ------------------------------------
7509 -- Note: this function knows quite a bit about the exact requirements of
7510 -- Gigi with respect to unchecked type conversions, and its code must be
7511 -- coordinated with any changes in Gigi in this area.
7513 -- The above requirements should be documented in Sinfo ???
7515 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
7516 Otyp : Entity_Id;
7517 Ityp : Entity_Id;
7518 Oalign : Uint;
7519 Ialign : Uint;
7520 Pexp : constant Node_Id := Parent (Exp);
7522 begin
7523 -- If the expression is the RHS of an assignment or object declaration
7524 -- we are always OK because there will always be a target.
7526 -- Object renaming declarations, (generated for view conversions of
7527 -- actuals in inlined calls), like object declarations, provide an
7528 -- explicit type, and are safe as well.
7530 if (Nkind (Pexp) = N_Assignment_Statement
7531 and then Expression (Pexp) = Exp)
7532 or else Nkind_In (Pexp, N_Object_Declaration,
7533 N_Object_Renaming_Declaration)
7534 then
7535 return True;
7537 -- If the expression is the prefix of an N_Selected_Component we should
7538 -- also be OK because GCC knows to look inside the conversion except if
7539 -- the type is discriminated. We assume that we are OK anyway if the
7540 -- type is not set yet or if it is controlled since we can't afford to
7541 -- introduce a temporary in this case.
7543 elsif Nkind (Pexp) = N_Selected_Component
7544 and then Prefix (Pexp) = Exp
7545 then
7546 if No (Etype (Pexp)) then
7547 return True;
7548 else
7549 return
7550 not Has_Discriminants (Etype (Pexp))
7551 or else Is_Constrained (Etype (Pexp));
7552 end if;
7553 end if;
7555 -- Set the output type, this comes from Etype if it is set, otherwise we
7556 -- take it from the subtype mark, which we assume was already fully
7557 -- analyzed.
7559 if Present (Etype (Exp)) then
7560 Otyp := Etype (Exp);
7561 else
7562 Otyp := Entity (Subtype_Mark (Exp));
7563 end if;
7565 -- The input type always comes from the expression, and we assume
7566 -- this is indeed always analyzed, so we can simply get the Etype.
7568 Ityp := Etype (Expression (Exp));
7570 -- Initialize alignments to unknown so far
7572 Oalign := No_Uint;
7573 Ialign := No_Uint;
7575 -- Replace a concurrent type by its corresponding record type and each
7576 -- type by its underlying type and do the tests on those. The original
7577 -- type may be a private type whose completion is a concurrent type, so
7578 -- find the underlying type first.
7580 if Present (Underlying_Type (Otyp)) then
7581 Otyp := Underlying_Type (Otyp);
7582 end if;
7584 if Present (Underlying_Type (Ityp)) then
7585 Ityp := Underlying_Type (Ityp);
7586 end if;
7588 if Is_Concurrent_Type (Otyp) then
7589 Otyp := Corresponding_Record_Type (Otyp);
7590 end if;
7592 if Is_Concurrent_Type (Ityp) then
7593 Ityp := Corresponding_Record_Type (Ityp);
7594 end if;
7596 -- If the base types are the same, we know there is no problem since
7597 -- this conversion will be a noop.
7599 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
7600 return True;
7602 -- Same if this is an upwards conversion of an untagged type, and there
7603 -- are no constraints involved (could be more general???)
7605 elsif Etype (Ityp) = Otyp
7606 and then not Is_Tagged_Type (Ityp)
7607 and then not Has_Discriminants (Ityp)
7608 and then No (First_Rep_Item (Base_Type (Ityp)))
7609 then
7610 return True;
7612 -- If the expression has an access type (object or subprogram) we assume
7613 -- that the conversion is safe, because the size of the target is safe,
7614 -- even if it is a record (which might be treated as having unknown size
7615 -- at this point).
7617 elsif Is_Access_Type (Ityp) then
7618 return True;
7620 -- If the size of output type is known at compile time, there is never
7621 -- a problem. Note that unconstrained records are considered to be of
7622 -- known size, but we can't consider them that way here, because we are
7623 -- talking about the actual size of the object.
7625 -- We also make sure that in addition to the size being known, we do not
7626 -- have a case which might generate an embarrassingly large temp in
7627 -- stack checking mode.
7629 elsif Size_Known_At_Compile_Time (Otyp)
7630 and then
7631 (not Stack_Checking_Enabled
7632 or else not May_Generate_Large_Temp (Otyp))
7633 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
7634 then
7635 return True;
7637 -- If either type is tagged, then we know the alignment is OK so
7638 -- Gigi will be able to use pointer punning.
7640 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
7641 return True;
7643 -- If either type is a limited record type, we cannot do a copy, so say
7644 -- safe since there's nothing else we can do.
7646 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
7647 return True;
7649 -- Conversions to and from packed array types are always ignored and
7650 -- hence are safe.
7652 elsif Is_Packed_Array_Type (Otyp)
7653 or else Is_Packed_Array_Type (Ityp)
7654 then
7655 return True;
7656 end if;
7658 -- The only other cases known to be safe is if the input type's
7659 -- alignment is known to be at least the maximum alignment for the
7660 -- target or if both alignments are known and the output type's
7661 -- alignment is no stricter than the input's. We can use the component
7662 -- type alignement for an array if a type is an unpacked array type.
7664 if Present (Alignment_Clause (Otyp)) then
7665 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
7667 elsif Is_Array_Type (Otyp)
7668 and then Present (Alignment_Clause (Component_Type (Otyp)))
7669 then
7670 Oalign := Expr_Value (Expression (Alignment_Clause
7671 (Component_Type (Otyp))));
7672 end if;
7674 if Present (Alignment_Clause (Ityp)) then
7675 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
7677 elsif Is_Array_Type (Ityp)
7678 and then Present (Alignment_Clause (Component_Type (Ityp)))
7679 then
7680 Ialign := Expr_Value (Expression (Alignment_Clause
7681 (Component_Type (Ityp))));
7682 end if;
7684 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
7685 return True;
7687 elsif Ialign /= No_Uint and then Oalign /= No_Uint
7688 and then Ialign <= Oalign
7689 then
7690 return True;
7692 -- Otherwise, Gigi cannot handle this and we must make a temporary
7694 else
7695 return False;
7696 end if;
7697 end Safe_Unchecked_Type_Conversion;
7699 ---------------------------------
7700 -- Set_Current_Value_Condition --
7701 ---------------------------------
7703 -- Note: the implementation of this procedure is very closely tied to the
7704 -- implementation of Get_Current_Value_Condition. Here we set required
7705 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
7706 -- them, so they must have a consistent view.
7708 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
7710 procedure Set_Entity_Current_Value (N : Node_Id);
7711 -- If N is an entity reference, where the entity is of an appropriate
7712 -- kind, then set the current value of this entity to Cnode, unless
7713 -- there is already a definite value set there.
7715 procedure Set_Expression_Current_Value (N : Node_Id);
7716 -- If N is of an appropriate form, sets an appropriate entry in current
7717 -- value fields of relevant entities. Multiple entities can be affected
7718 -- in the case of an AND or AND THEN.
7720 ------------------------------
7721 -- Set_Entity_Current_Value --
7722 ------------------------------
7724 procedure Set_Entity_Current_Value (N : Node_Id) is
7725 begin
7726 if Is_Entity_Name (N) then
7727 declare
7728 Ent : constant Entity_Id := Entity (N);
7730 begin
7731 -- Don't capture if not safe to do so
7733 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
7734 return;
7735 end if;
7737 -- Here we have a case where the Current_Value field may need
7738 -- to be set. We set it if it is not already set to a compile
7739 -- time expression value.
7741 -- Note that this represents a decision that one condition
7742 -- blots out another previous one. That's certainly right if
7743 -- they occur at the same level. If the second one is nested,
7744 -- then the decision is neither right nor wrong (it would be
7745 -- equally OK to leave the outer one in place, or take the new
7746 -- inner one. Really we should record both, but our data
7747 -- structures are not that elaborate.
7749 if Nkind (Current_Value (Ent)) not in N_Subexpr then
7750 Set_Current_Value (Ent, Cnode);
7751 end if;
7752 end;
7753 end if;
7754 end Set_Entity_Current_Value;
7756 ----------------------------------
7757 -- Set_Expression_Current_Value --
7758 ----------------------------------
7760 procedure Set_Expression_Current_Value (N : Node_Id) is
7761 Cond : Node_Id;
7763 begin
7764 Cond := N;
7766 -- Loop to deal with (ignore for now) any NOT operators present. The
7767 -- presence of NOT operators will be handled properly when we call
7768 -- Get_Current_Value_Condition.
7770 while Nkind (Cond) = N_Op_Not loop
7771 Cond := Right_Opnd (Cond);
7772 end loop;
7774 -- For an AND or AND THEN, recursively process operands
7776 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
7777 Set_Expression_Current_Value (Left_Opnd (Cond));
7778 Set_Expression_Current_Value (Right_Opnd (Cond));
7779 return;
7780 end if;
7782 -- Check possible relational operator
7784 if Nkind (Cond) in N_Op_Compare then
7785 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
7786 Set_Entity_Current_Value (Left_Opnd (Cond));
7787 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
7788 Set_Entity_Current_Value (Right_Opnd (Cond));
7789 end if;
7791 elsif Nkind_In (Cond,
7792 N_Type_Conversion,
7793 N_Qualified_Expression,
7794 N_Expression_With_Actions)
7795 then
7796 Set_Expression_Current_Value (Expression (Cond));
7798 -- Check possible boolean variable reference
7800 else
7801 Set_Entity_Current_Value (Cond);
7802 end if;
7803 end Set_Expression_Current_Value;
7805 -- Start of processing for Set_Current_Value_Condition
7807 begin
7808 Set_Expression_Current_Value (Condition (Cnode));
7809 end Set_Current_Value_Condition;
7811 --------------------------
7812 -- Set_Elaboration_Flag --
7813 --------------------------
7815 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
7816 Loc : constant Source_Ptr := Sloc (N);
7817 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
7818 Asn : Node_Id;
7820 begin
7821 if Present (Ent) then
7823 -- Nothing to do if at the compilation unit level, because in this
7824 -- case the flag is set by the binder generated elaboration routine.
7826 if Nkind (Parent (N)) = N_Compilation_Unit then
7827 null;
7829 -- Here we do need to generate an assignment statement
7831 else
7832 Check_Restriction (No_Elaboration_Code, N);
7833 Asn :=
7834 Make_Assignment_Statement (Loc,
7835 Name => New_Occurrence_Of (Ent, Loc),
7836 Expression => Make_Integer_Literal (Loc, Uint_1));
7838 if Nkind (Parent (N)) = N_Subunit then
7839 Insert_After (Corresponding_Stub (Parent (N)), Asn);
7840 else
7841 Insert_After (N, Asn);
7842 end if;
7844 Analyze (Asn);
7846 -- Kill current value indication. This is necessary because the
7847 -- tests of this flag are inserted out of sequence and must not
7848 -- pick up bogus indications of the wrong constant value.
7850 Set_Current_Value (Ent, Empty);
7851 end if;
7852 end if;
7853 end Set_Elaboration_Flag;
7855 ----------------------------
7856 -- Set_Renamed_Subprogram --
7857 ----------------------------
7859 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
7860 begin
7861 -- If input node is an identifier, we can just reset it
7863 if Nkind (N) = N_Identifier then
7864 Set_Chars (N, Chars (E));
7865 Set_Entity (N, E);
7867 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
7869 else
7870 declare
7871 CS : constant Boolean := Comes_From_Source (N);
7872 begin
7873 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
7874 Set_Entity (N, E);
7875 Set_Comes_From_Source (N, CS);
7876 Set_Analyzed (N, True);
7877 end;
7878 end if;
7879 end Set_Renamed_Subprogram;
7881 ----------------------------------
7882 -- Silly_Boolean_Array_Not_Test --
7883 ----------------------------------
7885 -- This procedure implements an odd and silly test. We explicitly check
7886 -- for the case where the 'First of the component type is equal to the
7887 -- 'Last of this component type, and if this is the case, we make sure
7888 -- that constraint error is raised. The reason is that the NOT is bound
7889 -- to cause CE in this case, and we will not otherwise catch it.
7891 -- No such check is required for AND and OR, since for both these cases
7892 -- False op False = False, and True op True = True. For the XOR case,
7893 -- see Silly_Boolean_Array_Xor_Test.
7895 -- Believe it or not, this was reported as a bug. Note that nearly always,
7896 -- the test will evaluate statically to False, so the code will be
7897 -- statically removed, and no extra overhead caused.
7899 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
7900 Loc : constant Source_Ptr := Sloc (N);
7901 CT : constant Entity_Id := Component_Type (T);
7903 begin
7904 -- The check we install is
7906 -- constraint_error when
7907 -- component_type'first = component_type'last
7908 -- and then array_type'Length /= 0)
7910 -- We need the last guard because we don't want to raise CE for empty
7911 -- arrays since no out of range values result. (Empty arrays with a
7912 -- component type of True .. True -- very useful -- even the ACATS
7913 -- does not test that marginal case!)
7915 Insert_Action (N,
7916 Make_Raise_Constraint_Error (Loc,
7917 Condition =>
7918 Make_And_Then (Loc,
7919 Left_Opnd =>
7920 Make_Op_Eq (Loc,
7921 Left_Opnd =>
7922 Make_Attribute_Reference (Loc,
7923 Prefix => New_Occurrence_Of (CT, Loc),
7924 Attribute_Name => Name_First),
7926 Right_Opnd =>
7927 Make_Attribute_Reference (Loc,
7928 Prefix => New_Occurrence_Of (CT, Loc),
7929 Attribute_Name => Name_Last)),
7931 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
7932 Reason => CE_Range_Check_Failed));
7933 end Silly_Boolean_Array_Not_Test;
7935 ----------------------------------
7936 -- Silly_Boolean_Array_Xor_Test --
7937 ----------------------------------
7939 -- This procedure implements an odd and silly test. We explicitly check
7940 -- for the XOR case where the component type is True .. True, since this
7941 -- will raise constraint error. A special check is required since CE
7942 -- will not be generated otherwise (cf Expand_Packed_Not).
7944 -- No such check is required for AND and OR, since for both these cases
7945 -- False op False = False, and True op True = True, and no check is
7946 -- required for the case of False .. False, since False xor False = False.
7947 -- See also Silly_Boolean_Array_Not_Test
7949 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
7950 Loc : constant Source_Ptr := Sloc (N);
7951 CT : constant Entity_Id := Component_Type (T);
7953 begin
7954 -- The check we install is
7956 -- constraint_error when
7957 -- Boolean (component_type'First)
7958 -- and then Boolean (component_type'Last)
7959 -- and then array_type'Length /= 0)
7961 -- We need the last guard because we don't want to raise CE for empty
7962 -- arrays since no out of range values result (Empty arrays with a
7963 -- component type of True .. True -- very useful -- even the ACATS
7964 -- does not test that marginal case!).
7966 Insert_Action (N,
7967 Make_Raise_Constraint_Error (Loc,
7968 Condition =>
7969 Make_And_Then (Loc,
7970 Left_Opnd =>
7971 Make_And_Then (Loc,
7972 Left_Opnd =>
7973 Convert_To (Standard_Boolean,
7974 Make_Attribute_Reference (Loc,
7975 Prefix => New_Occurrence_Of (CT, Loc),
7976 Attribute_Name => Name_First)),
7978 Right_Opnd =>
7979 Convert_To (Standard_Boolean,
7980 Make_Attribute_Reference (Loc,
7981 Prefix => New_Occurrence_Of (CT, Loc),
7982 Attribute_Name => Name_Last))),
7984 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
7985 Reason => CE_Range_Check_Failed));
7986 end Silly_Boolean_Array_Xor_Test;
7988 --------------------------
7989 -- Target_Has_Fixed_Ops --
7990 --------------------------
7992 Integer_Sized_Small : Ureal;
7993 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
7994 -- called (we don't want to compute it more than once!)
7996 Long_Integer_Sized_Small : Ureal;
7997 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
7998 -- is called (we don't want to compute it more than once)
8000 First_Time_For_THFO : Boolean := True;
8001 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
8003 function Target_Has_Fixed_Ops
8004 (Left_Typ : Entity_Id;
8005 Right_Typ : Entity_Id;
8006 Result_Typ : Entity_Id) return Boolean
8008 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
8009 -- Return True if the given type is a fixed-point type with a small
8010 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
8011 -- an absolute value less than 1.0. This is currently limited to
8012 -- fixed-point types that map to Integer or Long_Integer.
8014 ------------------------
8015 -- Is_Fractional_Type --
8016 ------------------------
8018 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
8019 begin
8020 if Esize (Typ) = Standard_Integer_Size then
8021 return Small_Value (Typ) = Integer_Sized_Small;
8023 elsif Esize (Typ) = Standard_Long_Integer_Size then
8024 return Small_Value (Typ) = Long_Integer_Sized_Small;
8026 else
8027 return False;
8028 end if;
8029 end Is_Fractional_Type;
8031 -- Start of processing for Target_Has_Fixed_Ops
8033 begin
8034 -- Return False if Fractional_Fixed_Ops_On_Target is false
8036 if not Fractional_Fixed_Ops_On_Target then
8037 return False;
8038 end if;
8040 -- Here the target has Fractional_Fixed_Ops, if first time, compute
8041 -- standard constants used by Is_Fractional_Type.
8043 if First_Time_For_THFO then
8044 First_Time_For_THFO := False;
8046 Integer_Sized_Small :=
8047 UR_From_Components
8048 (Num => Uint_1,
8049 Den => UI_From_Int (Standard_Integer_Size - 1),
8050 Rbase => 2);
8052 Long_Integer_Sized_Small :=
8053 UR_From_Components
8054 (Num => Uint_1,
8055 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
8056 Rbase => 2);
8057 end if;
8059 -- Return True if target supports fixed-by-fixed multiply/divide for
8060 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
8061 -- and result types are equivalent fractional types.
8063 return Is_Fractional_Type (Base_Type (Left_Typ))
8064 and then Is_Fractional_Type (Base_Type (Right_Typ))
8065 and then Is_Fractional_Type (Base_Type (Result_Typ))
8066 and then Esize (Left_Typ) = Esize (Right_Typ)
8067 and then Esize (Left_Typ) = Esize (Result_Typ);
8068 end Target_Has_Fixed_Ops;
8070 ------------------------------------------
8071 -- Type_May_Have_Bit_Aligned_Components --
8072 ------------------------------------------
8074 function Type_May_Have_Bit_Aligned_Components
8075 (Typ : Entity_Id) return Boolean
8077 begin
8078 -- Array type, check component type
8080 if Is_Array_Type (Typ) then
8081 return
8082 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
8084 -- Record type, check components
8086 elsif Is_Record_Type (Typ) then
8087 declare
8088 E : Entity_Id;
8090 begin
8091 E := First_Component_Or_Discriminant (Typ);
8092 while Present (E) loop
8093 if Component_May_Be_Bit_Aligned (E)
8094 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
8095 then
8096 return True;
8097 end if;
8099 Next_Component_Or_Discriminant (E);
8100 end loop;
8102 return False;
8103 end;
8105 -- Type other than array or record is always OK
8107 else
8108 return False;
8109 end if;
8110 end Type_May_Have_Bit_Aligned_Components;
8112 ----------------------------------
8113 -- Within_Case_Or_If_Expression --
8114 ----------------------------------
8116 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
8117 Par : Node_Id;
8119 begin
8120 -- Locate an enclosing case or if expression. Note that these constructs
8121 -- can be expanded into Expression_With_Actions, hence the test of the
8122 -- original node.
8124 Par := Parent (N);
8125 while Present (Par) loop
8126 if Nkind_In (Original_Node (Par), N_Case_Expression,
8127 N_If_Expression)
8128 then
8129 return True;
8131 -- Prevent the search from going too far
8133 elsif Is_Body_Or_Package_Declaration (Par) then
8134 return False;
8135 end if;
8137 Par := Parent (Par);
8138 end loop;
8140 return False;
8141 end Within_Case_Or_If_Expression;
8143 ----------------------------
8144 -- Wrap_Cleanup_Procedure --
8145 ----------------------------
8147 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
8148 Loc : constant Source_Ptr := Sloc (N);
8149 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
8150 Stmts : constant List_Id := Statements (Stseq);
8152 begin
8153 if Abort_Allowed then
8154 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
8155 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
8156 end if;
8157 end Wrap_Cleanup_Procedure;
8159 end Exp_Util;