Daily bump.
[official-gcc.git] / gcc / ada / exp_util.adb
blobeec7149ebb285112be1784784cd998a262ab1333
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-2015, 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 Ghost; use Ghost;
38 with Inline; use Inline;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Res; use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sem_Util; use Sem_Util;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Stringt; use Stringt;
56 with Targparm; use Targparm;
57 with Tbuild; use Tbuild;
58 with Ttypes; use Ttypes;
59 with Urealp; use Urealp;
60 with Validsw; use Validsw;
62 package body Exp_Util is
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 function Build_Task_Array_Image
69 (Loc : Source_Ptr;
70 Id_Ref : Node_Id;
71 A_Type : Entity_Id;
72 Dyn : Boolean := False) return Node_Id;
73 -- Build function to generate the image string for a task that is an array
74 -- component, concatenating the images of each index. To avoid storage
75 -- leaks, the string is built with successive slice assignments. The flag
76 -- Dyn indicates whether this is called for the initialization procedure of
77 -- an array of tasks, or for the name of a dynamically created task that is
78 -- assigned to an indexed component.
80 function Build_Task_Image_Function
81 (Loc : Source_Ptr;
82 Decls : List_Id;
83 Stats : List_Id;
84 Res : Entity_Id) return Node_Id;
85 -- Common processing for Task_Array_Image and Task_Record_Image. Build
86 -- function body that computes image.
88 procedure Build_Task_Image_Prefix
89 (Loc : Source_Ptr;
90 Len : out Entity_Id;
91 Res : out Entity_Id;
92 Pos : out Entity_Id;
93 Prefix : Entity_Id;
94 Sum : Node_Id;
95 Decls : List_Id;
96 Stats : List_Id);
97 -- Common processing for Task_Array_Image and Task_Record_Image. Create
98 -- local variables and assign prefix of name to result string.
100 function Build_Task_Record_Image
101 (Loc : Source_Ptr;
102 Id_Ref : Node_Id;
103 Dyn : Boolean := False) return Node_Id;
104 -- Build function to generate the image string for a task that is a record
105 -- component. Concatenate name of variable with that of selector. The flag
106 -- Dyn indicates whether this is called for the initialization procedure of
107 -- record with task components, or for a dynamically created task that is
108 -- assigned to a selected component.
110 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
111 -- Force evaluation of bounds of a slice, which may be given by a range
112 -- or by a subtype indication with or without a constraint.
114 function Make_CW_Equivalent_Type
115 (T : Entity_Id;
116 E : Node_Id) return Entity_Id;
117 -- T is a class-wide type entity, E is the initial expression node that
118 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
119 -- returns the entity of the Equivalent type and inserts on the fly the
120 -- necessary declaration such as:
122 -- type anon is record
123 -- _parent : Root_Type (T); constrained with E discriminants (if any)
124 -- Extension : String (1 .. expr to match size of E);
125 -- end record;
127 -- This record is compatible with any object of the class of T thanks to
128 -- the first field and has the same size as E thanks to the second.
130 function Make_Literal_Range
131 (Loc : Source_Ptr;
132 Literal_Typ : Entity_Id) return Node_Id;
133 -- Produce a Range node whose bounds are:
134 -- Low_Bound (Literal_Type) ..
135 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
136 -- this is used for expanding declarations like X : String := "sdfgdfg";
138 -- If the index type of the target array is not integer, we generate:
139 -- Low_Bound (Literal_Type) ..
140 -- Literal_Type'Val
141 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
142 -- + (Length (Literal_Typ) -1))
144 function Make_Non_Empty_Check
145 (Loc : Source_Ptr;
146 N : Node_Id) return Node_Id;
147 -- Produce a boolean expression checking that the unidimensional array
148 -- node N is not empty.
150 function New_Class_Wide_Subtype
151 (CW_Typ : Entity_Id;
152 N : Node_Id) return Entity_Id;
153 -- Create an implicit subtype of CW_Typ attached to node N
155 function Requires_Cleanup_Actions
156 (L : List_Id;
157 Lib_Level : Boolean;
158 Nested_Constructs : Boolean) return Boolean;
159 -- Given a list L, determine whether it contains one of the following:
161 -- 1) controlled objects
162 -- 2) library-level tagged types
164 -- Lib_Level is True when the list comes from a construct at the library
165 -- level, and False otherwise. Nested_Constructs is True when any nested
166 -- packages declared in L must be processed, and False otherwise.
168 -------------------------------------
169 -- Activate_Atomic_Synchronization --
170 -------------------------------------
172 procedure Activate_Atomic_Synchronization (N : Node_Id) is
173 Msg_Node : Node_Id;
175 begin
176 case Nkind (Parent (N)) is
178 -- Check for cases of appearing in the prefix of a construct where
179 -- we don't need atomic synchronization for this kind of usage.
181 when
182 -- Nothing to do if we are the prefix of an attribute, since we
183 -- do not want an atomic sync operation for things like 'Size.
185 N_Attribute_Reference |
187 -- The N_Reference node is like an attribute
189 N_Reference |
191 -- Nothing to do for a reference to a component (or components)
192 -- of a composite object. Only reads and updates of the object
193 -- as a whole require atomic synchronization (RM C.6 (15)).
195 N_Indexed_Component |
196 N_Selected_Component |
197 N_Slice =>
199 -- For all the above cases, nothing to do if we are the prefix
201 if Prefix (Parent (N)) = N then
202 return;
203 end if;
205 when others => null;
206 end case;
208 -- Nothing to do for the identifier in an object renaming declaration,
209 -- the renaming itself does not need atomic syncrhonization.
211 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
212 return;
213 end if;
215 -- Go ahead and set the flag
217 Set_Atomic_Sync_Required (N);
219 -- Generate info message if requested
221 if Warn_On_Atomic_Synchronization then
222 case Nkind (N) is
223 when N_Identifier =>
224 Msg_Node := N;
226 when N_Selected_Component | N_Expanded_Name =>
227 Msg_Node := Selector_Name (N);
229 when N_Explicit_Dereference | N_Indexed_Component =>
230 Msg_Node := Empty;
232 when others =>
233 pragma Assert (False);
234 return;
235 end case;
237 if Present (Msg_Node) then
238 Error_Msg_N
239 ("info: atomic synchronization set for &?N?", Msg_Node);
240 else
241 Error_Msg_N
242 ("info: atomic synchronization set?N?", N);
243 end if;
244 end if;
245 end Activate_Atomic_Synchronization;
247 ----------------------
248 -- Adjust_Condition --
249 ----------------------
251 procedure Adjust_Condition (N : Node_Id) is
252 begin
253 if No (N) then
254 return;
255 end if;
257 declare
258 Loc : constant Source_Ptr := Sloc (N);
259 T : constant Entity_Id := Etype (N);
260 Ti : Entity_Id;
262 begin
263 -- Defend against a call where the argument has no type, or has a
264 -- type that is not Boolean. This can occur because of prior errors.
266 if No (T) or else not Is_Boolean_Type (T) then
267 return;
268 end if;
270 -- Apply validity checking if needed
272 if Validity_Checks_On and Validity_Check_Tests then
273 Ensure_Valid (N);
274 end if;
276 -- Immediate return if standard boolean, the most common case,
277 -- where nothing needs to be done.
279 if Base_Type (T) = Standard_Boolean then
280 return;
281 end if;
283 -- Case of zero/non-zero semantics or non-standard enumeration
284 -- representation. In each case, we rewrite the node as:
286 -- ityp!(N) /= False'Enum_Rep
288 -- where ityp is an integer type with large enough size to hold any
289 -- value of type T.
291 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
292 if Esize (T) <= Esize (Standard_Integer) then
293 Ti := Standard_Integer;
294 else
295 Ti := Standard_Long_Long_Integer;
296 end if;
298 Rewrite (N,
299 Make_Op_Ne (Loc,
300 Left_Opnd => Unchecked_Convert_To (Ti, N),
301 Right_Opnd =>
302 Make_Attribute_Reference (Loc,
303 Attribute_Name => Name_Enum_Rep,
304 Prefix =>
305 New_Occurrence_Of (First_Literal (T), Loc))));
306 Analyze_And_Resolve (N, Standard_Boolean);
308 else
309 Rewrite (N, Convert_To (Standard_Boolean, N));
310 Analyze_And_Resolve (N, Standard_Boolean);
311 end if;
312 end;
313 end Adjust_Condition;
315 ------------------------
316 -- Adjust_Result_Type --
317 ------------------------
319 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
320 begin
321 -- Ignore call if current type is not Standard.Boolean
323 if Etype (N) /= Standard_Boolean then
324 return;
325 end if;
327 -- If result is already of correct type, nothing to do. Note that
328 -- this will get the most common case where everything has a type
329 -- of Standard.Boolean.
331 if Base_Type (T) = Standard_Boolean then
332 return;
334 else
335 declare
336 KP : constant Node_Kind := Nkind (Parent (N));
338 begin
339 -- If result is to be used as a Condition in the syntax, no need
340 -- to convert it back, since if it was changed to Standard.Boolean
341 -- using Adjust_Condition, that is just fine for this usage.
343 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
344 return;
346 -- If result is an operand of another logical operation, no need
347 -- to reset its type, since Standard.Boolean is just fine, and
348 -- such operations always do Adjust_Condition on their operands.
350 elsif KP in N_Op_Boolean
351 or else KP in N_Short_Circuit
352 or else KP = N_Op_Not
353 then
354 return;
356 -- Otherwise we perform a conversion from the current type, which
357 -- must be Standard.Boolean, to the desired type.
359 else
360 Set_Analyzed (N);
361 Rewrite (N, Convert_To (T, N));
362 Analyze_And_Resolve (N, T);
363 end if;
364 end;
365 end if;
366 end Adjust_Result_Type;
368 --------------------------
369 -- Append_Freeze_Action --
370 --------------------------
372 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
373 Fnode : Node_Id;
375 begin
376 Ensure_Freeze_Node (T);
377 Fnode := Freeze_Node (T);
379 if No (Actions (Fnode)) then
380 Set_Actions (Fnode, New_List (N));
381 else
382 Append (N, Actions (Fnode));
383 end if;
385 end Append_Freeze_Action;
387 ---------------------------
388 -- Append_Freeze_Actions --
389 ---------------------------
391 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
392 Fnode : Node_Id;
394 begin
395 if No (L) then
396 return;
397 end if;
399 Ensure_Freeze_Node (T);
400 Fnode := Freeze_Node (T);
402 if No (Actions (Fnode)) then
403 Set_Actions (Fnode, L);
404 else
405 Append_List (L, Actions (Fnode));
406 end if;
407 end Append_Freeze_Actions;
409 ------------------------------------
410 -- Build_Allocate_Deallocate_Proc --
411 ------------------------------------
413 procedure Build_Allocate_Deallocate_Proc
414 (N : Node_Id;
415 Is_Allocate : Boolean)
417 Desig_Typ : Entity_Id;
418 Expr : Node_Id;
419 Pool_Id : Entity_Id;
420 Proc_To_Call : Node_Id := Empty;
421 Ptr_Typ : Entity_Id;
423 function Find_Object (E : Node_Id) return Node_Id;
424 -- Given an arbitrary expression of an allocator, try to find an object
425 -- reference in it, otherwise return the original expression.
427 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
428 -- Determine whether subprogram Subp denotes a custom allocate or
429 -- deallocate.
431 -----------------
432 -- Find_Object --
433 -----------------
435 function Find_Object (E : Node_Id) return Node_Id is
436 Expr : Node_Id;
438 begin
439 pragma Assert (Is_Allocate);
441 Expr := E;
442 loop
443 if Nkind (Expr) = N_Explicit_Dereference then
444 Expr := Prefix (Expr);
446 elsif Nkind (Expr) = N_Qualified_Expression then
447 Expr := Expression (Expr);
449 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
451 -- When interface class-wide types are involved in allocation,
452 -- the expander introduces several levels of address arithmetic
453 -- to perform dispatch table displacement. In this scenario the
454 -- object appears as:
456 -- Tag_Ptr (Base_Address (<object>'Address))
458 -- Detect this case and utilize the whole expression as the
459 -- "object" since it now points to the proper dispatch table.
461 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
462 exit;
464 -- Continue to strip the object
466 else
467 Expr := Expression (Expr);
468 end if;
470 else
471 exit;
472 end if;
473 end loop;
475 return Expr;
476 end Find_Object;
478 ---------------------------------
479 -- Is_Allocate_Deallocate_Proc --
480 ---------------------------------
482 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
483 begin
484 -- Look for a subprogram body with only one statement which is a
485 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
487 if Ekind (Subp) = E_Procedure
488 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
489 then
490 declare
491 HSS : constant Node_Id :=
492 Handled_Statement_Sequence (Parent (Parent (Subp)));
493 Proc : Entity_Id;
495 begin
496 if Present (Statements (HSS))
497 and then Nkind (First (Statements (HSS))) =
498 N_Procedure_Call_Statement
499 then
500 Proc := Entity (Name (First (Statements (HSS))));
502 return
503 Is_RTE (Proc, RE_Allocate_Any_Controlled)
504 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
505 end if;
506 end;
507 end if;
509 return False;
510 end Is_Allocate_Deallocate_Proc;
512 -- Start of processing for Build_Allocate_Deallocate_Proc
514 begin
515 -- Obtain the attributes of the allocation / deallocation
517 if Nkind (N) = N_Free_Statement then
518 Expr := Expression (N);
519 Ptr_Typ := Base_Type (Etype (Expr));
520 Proc_To_Call := Procedure_To_Call (N);
522 else
523 if Nkind (N) = N_Object_Declaration then
524 Expr := Expression (N);
525 else
526 Expr := N;
527 end if;
529 -- In certain cases an allocator with a qualified expression may
530 -- be relocated and used as the initialization expression of a
531 -- temporary:
533 -- before:
534 -- Obj : Ptr_Typ := new Desig_Typ'(...);
536 -- after:
537 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
538 -- Obj : Ptr_Typ := Tmp;
540 -- Since the allocator is always marked as analyzed to avoid infinite
541 -- expansion, it will never be processed by this routine given that
542 -- the designated type needs finalization actions. Detect this case
543 -- and complete the expansion of the allocator.
545 if Nkind (Expr) = N_Identifier
546 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
547 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
548 then
549 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
550 return;
551 end if;
553 -- The allocator may have been rewritten into something else in which
554 -- case the expansion performed by this routine does not apply.
556 if Nkind (Expr) /= N_Allocator then
557 return;
558 end if;
560 Ptr_Typ := Base_Type (Etype (Expr));
561 Proc_To_Call := Procedure_To_Call (Expr);
562 end if;
564 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
565 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
567 -- Handle concurrent types
569 if Is_Concurrent_Type (Desig_Typ)
570 and then Present (Corresponding_Record_Type (Desig_Typ))
571 then
572 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
573 end if;
575 -- Do not process allocations / deallocations without a pool
577 if No (Pool_Id) then
578 return;
580 -- Do not process allocations on / deallocations from the secondary
581 -- stack.
583 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
584 return;
586 -- Do not replicate the machinery if the allocator / free has already
587 -- been expanded and has a custom Allocate / Deallocate.
589 elsif Present (Proc_To_Call)
590 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
591 then
592 return;
593 end if;
595 if Needs_Finalization (Desig_Typ) then
597 -- Certain run-time configurations and targets do not provide support
598 -- for controlled types.
600 if Restriction_Active (No_Finalization) then
601 return;
603 -- Do nothing if the access type may never allocate / deallocate
604 -- objects.
606 elsif No_Pool_Assigned (Ptr_Typ) then
607 return;
609 -- Access-to-controlled types are not supported on .NET/JVM since
610 -- these targets cannot support pools and address arithmetic.
612 elsif VM_Target /= No_VM then
613 return;
614 end if;
616 -- The allocation / deallocation of a controlled object must be
617 -- chained on / detached from a finalization master.
619 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
621 -- The only other kind of allocation / deallocation supported by this
622 -- routine is on / from a subpool.
624 elsif Nkind (Expr) = N_Allocator
625 and then No (Subpool_Handle_Name (Expr))
626 then
627 return;
628 end if;
630 declare
631 Loc : constant Source_Ptr := Sloc (N);
632 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
633 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
634 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
635 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
637 Actuals : List_Id;
638 Fin_Addr_Id : Entity_Id;
639 Fin_Mas_Act : Node_Id;
640 Fin_Mas_Id : Entity_Id;
641 Proc_To_Call : Entity_Id;
642 Subpool : Node_Id := Empty;
644 begin
645 -- Step 1: Construct all the actuals for the call to library routine
646 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
648 -- a) Storage pool
650 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
652 if Is_Allocate then
654 -- b) Subpool
656 if Nkind (Expr) = N_Allocator then
657 Subpool := Subpool_Handle_Name (Expr);
658 end if;
660 -- If a subpool is present it can be an arbitrary name, so make
661 -- the actual by copying the tree.
663 if Present (Subpool) then
664 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
665 else
666 Append_To (Actuals, Make_Null (Loc));
667 end if;
669 -- c) Finalization master
671 if Needs_Finalization (Desig_Typ) then
672 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
673 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
675 -- Handle the case where the master is actually a pointer to a
676 -- master. This case arises in build-in-place functions.
678 if Is_Access_Type (Etype (Fin_Mas_Id)) then
679 Append_To (Actuals, Fin_Mas_Act);
680 else
681 Append_To (Actuals,
682 Make_Attribute_Reference (Loc,
683 Prefix => Fin_Mas_Act,
684 Attribute_Name => Name_Unrestricted_Access));
685 end if;
686 else
687 Append_To (Actuals, Make_Null (Loc));
688 end if;
690 -- d) Finalize_Address
692 -- Primitive Finalize_Address is never generated in CodePeer mode
693 -- since it contains an Unchecked_Conversion.
695 if Needs_Finalization (Desig_Typ) and then not CodePeer_Mode then
696 Fin_Addr_Id := Finalize_Address (Desig_Typ);
697 pragma Assert (Present (Fin_Addr_Id));
699 Append_To (Actuals,
700 Make_Attribute_Reference (Loc,
701 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
702 Attribute_Name => Name_Unrestricted_Access));
703 else
704 Append_To (Actuals, Make_Null (Loc));
705 end if;
706 end if;
708 -- e) Address
709 -- f) Storage_Size
710 -- g) Alignment
712 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
713 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
715 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
716 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
718 -- For deallocation of class-wide types we obtain the value of
719 -- alignment from the Type Specific Record of the deallocated object.
720 -- This is needed because the frontend expansion of class-wide types
721 -- into equivalent types confuses the backend.
723 else
724 -- Generate:
725 -- Obj.all'Alignment
727 -- ... because 'Alignment applied to class-wide types is expanded
728 -- into the code that reads the value of alignment from the TSD
729 -- (see Expand_N_Attribute_Reference)
731 Append_To (Actuals,
732 Unchecked_Convert_To (RTE (RE_Storage_Offset),
733 Make_Attribute_Reference (Loc,
734 Prefix =>
735 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
736 Attribute_Name => Name_Alignment)));
737 end if;
739 -- h) Is_Controlled
741 if Needs_Finalization (Desig_Typ) then
742 declare
743 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
744 Flag_Expr : Node_Id;
745 Param : Node_Id;
746 Temp : Node_Id;
748 begin
749 if Is_Allocate then
750 Temp := Find_Object (Expression (Expr));
751 else
752 Temp := Expr;
753 end if;
755 -- Processing for allocations where the expression is a subtype
756 -- indication.
758 if Is_Allocate
759 and then Is_Entity_Name (Temp)
760 and then Is_Type (Entity (Temp))
761 then
762 Flag_Expr :=
763 New_Occurrence_Of
764 (Boolean_Literals
765 (Needs_Finalization (Entity (Temp))), Loc);
767 -- The allocation / deallocation of a class-wide object relies
768 -- on a runtime check to determine whether the object is truly
769 -- controlled or not. Depending on this check, the finalization
770 -- machinery will request or reclaim extra storage reserved for
771 -- a list header.
773 elsif Is_Class_Wide_Type (Desig_Typ) then
775 -- Detect a special case where interface class-wide types
776 -- are involved as the object appears as:
778 -- Tag_Ptr (Base_Address (<object>'Address))
780 -- The expression already yields the proper tag, generate:
782 -- Temp.all
784 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
785 Param :=
786 Make_Explicit_Dereference (Loc,
787 Prefix => Relocate_Node (Temp));
789 -- In the default case, obtain the tag of the object about
790 -- to be allocated / deallocated. Generate:
792 -- Temp'Tag
794 else
795 Param :=
796 Make_Attribute_Reference (Loc,
797 Prefix => Relocate_Node (Temp),
798 Attribute_Name => Name_Tag);
799 end if;
801 -- Generate:
802 -- Needs_Finalization (<Param>)
804 Flag_Expr :=
805 Make_Function_Call (Loc,
806 Name =>
807 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
808 Parameter_Associations => New_List (Param));
810 -- Processing for generic actuals
812 elsif Is_Generic_Actual_Type (Desig_Typ) then
813 Flag_Expr :=
814 New_Occurrence_Of (Boolean_Literals
815 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
817 -- The object does not require any specialized checks, it is
818 -- known to be controlled.
820 else
821 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
822 end if;
824 -- Create the temporary which represents the finalization state
825 -- of the expression. Generate:
827 -- F : constant Boolean := <Flag_Expr>;
829 Insert_Action (N,
830 Make_Object_Declaration (Loc,
831 Defining_Identifier => Flag_Id,
832 Constant_Present => True,
833 Object_Definition =>
834 New_Occurrence_Of (Standard_Boolean, Loc),
835 Expression => Flag_Expr));
837 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
838 end;
840 -- The object is not controlled
842 else
843 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
844 end if;
846 -- i) On_Subpool
848 if Is_Allocate then
849 Append_To (Actuals,
850 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
851 end if;
853 -- Step 2: Build a wrapper Allocate / Deallocate which internally
854 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
856 -- Select the proper routine to call
858 if Is_Allocate then
859 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
860 else
861 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
862 end if;
864 -- Create a custom Allocate / Deallocate routine which has identical
865 -- profile to that of System.Storage_Pools.
867 Insert_Action (N,
868 Make_Subprogram_Body (Loc,
869 Specification =>
871 -- procedure Pnn
873 Make_Procedure_Specification (Loc,
874 Defining_Unit_Name => Proc_Id,
875 Parameter_Specifications => New_List (
877 -- P : Root_Storage_Pool
879 Make_Parameter_Specification (Loc,
880 Defining_Identifier => Make_Temporary (Loc, 'P'),
881 Parameter_Type =>
882 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
884 -- A : [out] Address
886 Make_Parameter_Specification (Loc,
887 Defining_Identifier => Addr_Id,
888 Out_Present => Is_Allocate,
889 Parameter_Type =>
890 New_Occurrence_Of (RTE (RE_Address), Loc)),
892 -- S : Storage_Count
894 Make_Parameter_Specification (Loc,
895 Defining_Identifier => Size_Id,
896 Parameter_Type =>
897 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
899 -- L : Storage_Count
901 Make_Parameter_Specification (Loc,
902 Defining_Identifier => Alig_Id,
903 Parameter_Type =>
904 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
906 Declarations => No_List,
908 Handled_Statement_Sequence =>
909 Make_Handled_Sequence_Of_Statements (Loc,
910 Statements => New_List (
911 Make_Procedure_Call_Statement (Loc,
912 Name => New_Occurrence_Of (Proc_To_Call, Loc),
913 Parameter_Associations => Actuals)))));
915 -- The newly generated Allocate / Deallocate becomes the default
916 -- procedure to call when the back end processes the allocation /
917 -- deallocation.
919 if Is_Allocate then
920 Set_Procedure_To_Call (Expr, Proc_Id);
921 else
922 Set_Procedure_To_Call (N, Proc_Id);
923 end if;
924 end;
925 end Build_Allocate_Deallocate_Proc;
927 ------------------------
928 -- Build_Runtime_Call --
929 ------------------------
931 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
932 begin
933 -- If entity is not available, we can skip making the call (this avoids
934 -- junk duplicated error messages in a number of cases).
936 if not RTE_Available (RE) then
937 return Make_Null_Statement (Loc);
938 else
939 return
940 Make_Procedure_Call_Statement (Loc,
941 Name => New_Occurrence_Of (RTE (RE), Loc));
942 end if;
943 end Build_Runtime_Call;
945 ------------------------
946 -- Build_SS_Mark_Call --
947 ------------------------
949 function Build_SS_Mark_Call
950 (Loc : Source_Ptr;
951 Mark : Entity_Id) return Node_Id
953 begin
954 -- Generate:
955 -- Mark : constant Mark_Id := SS_Mark;
957 return
958 Make_Object_Declaration (Loc,
959 Defining_Identifier => Mark,
960 Constant_Present => True,
961 Object_Definition =>
962 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
963 Expression =>
964 Make_Function_Call (Loc,
965 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
966 end Build_SS_Mark_Call;
968 ---------------------------
969 -- Build_SS_Release_Call --
970 ---------------------------
972 function Build_SS_Release_Call
973 (Loc : Source_Ptr;
974 Mark : Entity_Id) return Node_Id
976 begin
977 -- Generate:
978 -- SS_Release (Mark);
980 return
981 Make_Procedure_Call_Statement (Loc,
982 Name =>
983 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
984 Parameter_Associations => New_List (
985 New_Occurrence_Of (Mark, Loc)));
986 end Build_SS_Release_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 -- Indicate that the result is an internal temporary, so it does not
1403 -- receive a bogus initialization when declaration is expanded. This
1404 -- is both efficient, and prevents anomalies in the handling of
1405 -- dynamic objects on the secondary stack.
1407 Set_Is_Internal (Res);
1408 Pos := Make_Temporary (Loc, 'P');
1410 Append_To (Decls,
1411 Make_Object_Declaration (Loc,
1412 Defining_Identifier => Pos,
1413 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
1415 -- Pos := Prefix'Length;
1417 Append_To (Stats,
1418 Make_Assignment_Statement (Loc,
1419 Name => New_Occurrence_Of (Pos, Loc),
1420 Expression =>
1421 Make_Attribute_Reference (Loc,
1422 Attribute_Name => Name_Length,
1423 Prefix => New_Occurrence_Of (Prefix, Loc),
1424 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
1426 -- Res (1 .. Pos) := Prefix;
1428 Append_To (Stats,
1429 Make_Assignment_Statement (Loc,
1430 Name =>
1431 Make_Slice (Loc,
1432 Prefix => New_Occurrence_Of (Res, Loc),
1433 Discrete_Range =>
1434 Make_Range (Loc,
1435 Low_Bound => Make_Integer_Literal (Loc, 1),
1436 High_Bound => New_Occurrence_Of (Pos, Loc))),
1438 Expression => New_Occurrence_Of (Prefix, Loc)));
1440 Append_To (Stats,
1441 Make_Assignment_Statement (Loc,
1442 Name => New_Occurrence_Of (Pos, Loc),
1443 Expression =>
1444 Make_Op_Add (Loc,
1445 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1446 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1447 end Build_Task_Image_Prefix;
1449 -----------------------------
1450 -- Build_Task_Record_Image --
1451 -----------------------------
1453 function Build_Task_Record_Image
1454 (Loc : Source_Ptr;
1455 Id_Ref : Node_Id;
1456 Dyn : Boolean := False) return Node_Id
1458 Len : Entity_Id;
1459 -- Total length of generated name
1461 Pos : Entity_Id;
1462 -- Index into result
1464 Res : Entity_Id;
1465 -- String to hold result
1467 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1468 -- Name of enclosing variable, prefix of resulting name
1470 Sum : Node_Id;
1471 -- Expression to compute total size of string
1473 Sel : Entity_Id;
1474 -- Entity for selector name
1476 Decls : constant List_Id := New_List;
1477 Stats : constant List_Id := New_List;
1479 begin
1480 -- For a dynamic task, the name comes from the target variable. For a
1481 -- static one it is a formal of the enclosing init proc.
1483 if Dyn then
1484 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1485 Append_To (Decls,
1486 Make_Object_Declaration (Loc,
1487 Defining_Identifier => Pref,
1488 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1489 Expression =>
1490 Make_String_Literal (Loc,
1491 Strval => String_From_Name_Buffer)));
1493 else
1494 Append_To (Decls,
1495 Make_Object_Renaming_Declaration (Loc,
1496 Defining_Identifier => Pref,
1497 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1498 Name => Make_Identifier (Loc, Name_uTask_Name)));
1499 end if;
1501 Sel := Make_Temporary (Loc, 'S');
1503 Get_Name_String (Chars (Selector_Name (Id_Ref)));
1505 Append_To (Decls,
1506 Make_Object_Declaration (Loc,
1507 Defining_Identifier => Sel,
1508 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1509 Expression =>
1510 Make_String_Literal (Loc,
1511 Strval => String_From_Name_Buffer)));
1513 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
1515 Sum :=
1516 Make_Op_Add (Loc,
1517 Left_Opnd => Sum,
1518 Right_Opnd =>
1519 Make_Attribute_Reference (Loc,
1520 Attribute_Name => Name_Length,
1521 Prefix =>
1522 New_Occurrence_Of (Pref, Loc),
1523 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1525 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1527 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
1529 -- Res (Pos) := '.';
1531 Append_To (Stats,
1532 Make_Assignment_Statement (Loc,
1533 Name => Make_Indexed_Component (Loc,
1534 Prefix => New_Occurrence_Of (Res, Loc),
1535 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1536 Expression =>
1537 Make_Character_Literal (Loc,
1538 Chars => Name_Find,
1539 Char_Literal_Value =>
1540 UI_From_Int (Character'Pos ('.')))));
1542 Append_To (Stats,
1543 Make_Assignment_Statement (Loc,
1544 Name => New_Occurrence_Of (Pos, Loc),
1545 Expression =>
1546 Make_Op_Add (Loc,
1547 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1548 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1550 -- Res (Pos .. Len) := Selector;
1552 Append_To (Stats,
1553 Make_Assignment_Statement (Loc,
1554 Name => Make_Slice (Loc,
1555 Prefix => New_Occurrence_Of (Res, Loc),
1556 Discrete_Range =>
1557 Make_Range (Loc,
1558 Low_Bound => New_Occurrence_Of (Pos, Loc),
1559 High_Bound => New_Occurrence_Of (Len, Loc))),
1560 Expression => New_Occurrence_Of (Sel, Loc)));
1562 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1563 end Build_Task_Record_Image;
1565 -----------------------------
1566 -- Check_Float_Op_Overflow --
1567 -----------------------------
1569 procedure Check_Float_Op_Overflow (N : Node_Id) is
1570 begin
1571 -- Return if no check needed
1573 if not Is_Floating_Point_Type (Etype (N))
1574 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
1576 -- In CodePeer_Mode, rely on the overflow check flag being set instead
1577 -- and do not expand the code for float overflow checking.
1579 or else CodePeer_Mode
1580 then
1581 return;
1582 end if;
1584 -- Otherwise we replace the expression by
1586 -- do Tnn : constant ftype := expression;
1587 -- constraint_error when not Tnn'Valid;
1588 -- in Tnn;
1590 declare
1591 Loc : constant Source_Ptr := Sloc (N);
1592 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
1593 Typ : constant Entity_Id := Etype (N);
1595 begin
1596 -- Turn off the Do_Overflow_Check flag, since we are doing that work
1597 -- right here. We also set the node as analyzed to prevent infinite
1598 -- recursion from repeating the operation in the expansion.
1600 Set_Do_Overflow_Check (N, False);
1601 Set_Analyzed (N, True);
1603 -- Do the rewrite to include the check
1605 Rewrite (N,
1606 Make_Expression_With_Actions (Loc,
1607 Actions => New_List (
1608 Make_Object_Declaration (Loc,
1609 Defining_Identifier => Tnn,
1610 Object_Definition => New_Occurrence_Of (Typ, Loc),
1611 Constant_Present => True,
1612 Expression => Relocate_Node (N)),
1613 Make_Raise_Constraint_Error (Loc,
1614 Condition =>
1615 Make_Op_Not (Loc,
1616 Right_Opnd =>
1617 Make_Attribute_Reference (Loc,
1618 Prefix => New_Occurrence_Of (Tnn, Loc),
1619 Attribute_Name => Name_Valid)),
1620 Reason => CE_Overflow_Check_Failed)),
1621 Expression => New_Occurrence_Of (Tnn, Loc)));
1623 Analyze_And_Resolve (N, Typ);
1624 end;
1625 end Check_Float_Op_Overflow;
1627 ----------------------------------
1628 -- Component_May_Be_Bit_Aligned --
1629 ----------------------------------
1631 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
1632 UT : Entity_Id;
1634 begin
1635 -- If no component clause, then everything is fine, since the back end
1636 -- never bit-misaligns by default, even if there is a pragma Packed for
1637 -- the record.
1639 if No (Comp) or else No (Component_Clause (Comp)) then
1640 return False;
1641 end if;
1643 UT := Underlying_Type (Etype (Comp));
1645 -- It is only array and record types that cause trouble
1647 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
1648 return False;
1650 -- If we know that we have a small (64 bits or less) record or small
1651 -- bit-packed array, then everything is fine, since the back end can
1652 -- handle these cases correctly.
1654 elsif Esize (Comp) <= 64
1655 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
1656 then
1657 return False;
1659 -- Otherwise if the component is not byte aligned, we know we have the
1660 -- nasty unaligned case.
1662 elsif Normalized_First_Bit (Comp) /= Uint_0
1663 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
1664 then
1665 return True;
1667 -- If we are large and byte aligned, then OK at this level
1669 else
1670 return False;
1671 end if;
1672 end Component_May_Be_Bit_Aligned;
1674 ----------------------------------------
1675 -- Containing_Package_With_Ext_Axioms --
1676 ----------------------------------------
1678 function Containing_Package_With_Ext_Axioms
1679 (E : Entity_Id) return Entity_Id
1681 Decl : Node_Id;
1683 begin
1684 if Ekind (E) = E_Package then
1685 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
1686 Decl := Parent (Parent (E));
1687 else
1688 Decl := Parent (E);
1689 end if;
1690 end if;
1692 -- E is the package or generic package which is externally axiomatized
1694 if Ekind_In (E, E_Package, E_Generic_Package)
1695 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
1696 then
1697 return E;
1698 end if;
1700 -- If E's scope is axiomatized, E is axiomatized.
1702 declare
1703 First_Ax_Parent_Scope : Entity_Id := Empty;
1705 begin
1706 if Present (Scope (E)) then
1707 First_Ax_Parent_Scope :=
1708 Containing_Package_With_Ext_Axioms (Scope (E));
1709 end if;
1711 if Present (First_Ax_Parent_Scope) then
1712 return First_Ax_Parent_Scope;
1713 end if;
1715 -- otherwise, if E is a package instance, it is axiomatized if the
1716 -- corresponding generic package is axiomatized.
1718 if Ekind (E) = E_Package
1719 and then Present (Generic_Parent (Decl))
1720 then
1721 return
1722 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
1723 else
1724 return Empty;
1725 end if;
1726 end;
1727 end Containing_Package_With_Ext_Axioms;
1729 -------------------------------
1730 -- Convert_To_Actual_Subtype --
1731 -------------------------------
1733 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1734 Act_ST : Entity_Id;
1736 begin
1737 Act_ST := Get_Actual_Subtype (Exp);
1739 if Act_ST = Etype (Exp) then
1740 return;
1741 else
1742 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
1743 Analyze_And_Resolve (Exp, Act_ST);
1744 end if;
1745 end Convert_To_Actual_Subtype;
1747 -----------------------------------
1748 -- Corresponding_Runtime_Package --
1749 -----------------------------------
1751 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1752 Pkg_Id : RTU_Id := RTU_Null;
1754 begin
1755 pragma Assert (Is_Concurrent_Type (Typ));
1757 if Ekind (Typ) in Protected_Kind then
1758 if Has_Entries (Typ)
1760 -- A protected type without entries that covers an interface and
1761 -- overrides the abstract routines with protected procedures is
1762 -- considered equivalent to a protected type with entries in the
1763 -- context of dispatching select statements. It is sufficient to
1764 -- check for the presence of an interface list in the declaration
1765 -- node to recognize this case.
1767 or else Present (Interface_List (Parent (Typ)))
1769 -- Protected types with interrupt handlers (when not using a
1770 -- restricted profile) are also considered equivalent to
1771 -- protected types with entries. The types which are used
1772 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
1773 -- are derived from Protection_Entries.
1775 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
1776 or else Has_Interrupt_Handler (Typ)
1777 then
1778 if Abort_Allowed
1779 or else Restriction_Active (No_Entry_Queue) = False
1780 or else Restriction_Active (No_Select_Statements) = False
1781 or else Number_Entries (Typ) > 1
1782 or else (Has_Attach_Handler (Typ)
1783 and then not Restricted_Profile)
1784 then
1785 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1786 else
1787 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1788 end if;
1790 else
1791 Pkg_Id := System_Tasking_Protected_Objects;
1792 end if;
1793 end if;
1795 return Pkg_Id;
1796 end Corresponding_Runtime_Package;
1798 -----------------------------------
1799 -- Current_Sem_Unit_Declarations --
1800 -----------------------------------
1802 function Current_Sem_Unit_Declarations return List_Id is
1803 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1804 Decls : List_Id;
1806 begin
1807 -- If the current unit is a package body, locate the visible
1808 -- declarations of the package spec.
1810 if Nkind (U) = N_Package_Body then
1811 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1812 end if;
1814 if Nkind (U) = N_Package_Declaration then
1815 U := Specification (U);
1816 Decls := Visible_Declarations (U);
1818 if No (Decls) then
1819 Decls := New_List;
1820 Set_Visible_Declarations (U, Decls);
1821 end if;
1823 else
1824 Decls := Declarations (U);
1826 if No (Decls) then
1827 Decls := New_List;
1828 Set_Declarations (U, Decls);
1829 end if;
1830 end if;
1832 return Decls;
1833 end Current_Sem_Unit_Declarations;
1835 -----------------------
1836 -- Duplicate_Subexpr --
1837 -----------------------
1839 function Duplicate_Subexpr
1840 (Exp : Node_Id;
1841 Name_Req : Boolean := False;
1842 Renaming_Req : Boolean := False) return Node_Id
1844 begin
1845 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1846 return New_Copy_Tree (Exp);
1847 end Duplicate_Subexpr;
1849 ---------------------------------
1850 -- Duplicate_Subexpr_No_Checks --
1851 ---------------------------------
1853 function Duplicate_Subexpr_No_Checks
1854 (Exp : Node_Id;
1855 Name_Req : Boolean := False;
1856 Renaming_Req : Boolean := False;
1857 Related_Id : Entity_Id := Empty;
1858 Is_Low_Bound : Boolean := False;
1859 Is_High_Bound : Boolean := False) return Node_Id
1861 New_Exp : Node_Id;
1863 begin
1864 Remove_Side_Effects
1865 (Exp => Exp,
1866 Name_Req => Name_Req,
1867 Renaming_Req => Renaming_Req,
1868 Related_Id => Related_Id,
1869 Is_Low_Bound => Is_Low_Bound,
1870 Is_High_Bound => Is_High_Bound);
1872 New_Exp := New_Copy_Tree (Exp);
1873 Remove_Checks (New_Exp);
1874 return New_Exp;
1875 end Duplicate_Subexpr_No_Checks;
1877 -----------------------------------
1878 -- Duplicate_Subexpr_Move_Checks --
1879 -----------------------------------
1881 function Duplicate_Subexpr_Move_Checks
1882 (Exp : Node_Id;
1883 Name_Req : Boolean := False;
1884 Renaming_Req : Boolean := False) return Node_Id
1886 New_Exp : Node_Id;
1888 begin
1889 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1890 New_Exp := New_Copy_Tree (Exp);
1891 Remove_Checks (Exp);
1892 return New_Exp;
1893 end Duplicate_Subexpr_Move_Checks;
1895 --------------------
1896 -- Ensure_Defined --
1897 --------------------
1899 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1900 IR : Node_Id;
1902 begin
1903 -- An itype reference must only be created if this is a local itype, so
1904 -- that gigi can elaborate it on the proper objstack.
1906 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
1907 IR := Make_Itype_Reference (Sloc (N));
1908 Set_Itype (IR, Typ);
1909 Insert_Action (N, IR);
1910 end if;
1911 end Ensure_Defined;
1913 --------------------
1914 -- Entry_Names_OK --
1915 --------------------
1917 function Entry_Names_OK return Boolean is
1918 begin
1919 return
1920 not Restricted_Profile
1921 and then not Global_Discard_Names
1922 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1923 and then not Restriction_Active (No_Local_Allocators);
1924 end Entry_Names_OK;
1926 -------------------
1927 -- Evaluate_Name --
1928 -------------------
1930 procedure Evaluate_Name (Nam : Node_Id) is
1931 K : constant Node_Kind := Nkind (Nam);
1933 begin
1934 -- For an explicit dereference, we simply force the evaluation of the
1935 -- name expression. The dereference provides a value that is the address
1936 -- for the renamed object, and it is precisely this value that we want
1937 -- to preserve.
1939 if K = N_Explicit_Dereference then
1940 Force_Evaluation (Prefix (Nam));
1942 -- For a selected component, we simply evaluate the prefix
1944 elsif K = N_Selected_Component then
1945 Evaluate_Name (Prefix (Nam));
1947 -- For an indexed component, or an attribute reference, we evaluate the
1948 -- prefix, which is itself a name, recursively, and then force the
1949 -- evaluation of all the subscripts (or attribute expressions).
1951 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
1952 Evaluate_Name (Prefix (Nam));
1954 declare
1955 E : Node_Id;
1957 begin
1958 E := First (Expressions (Nam));
1959 while Present (E) loop
1960 Force_Evaluation (E);
1962 if Original_Node (E) /= E then
1963 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
1964 end if;
1966 Next (E);
1967 end loop;
1968 end;
1970 -- For a slice, we evaluate the prefix, as for the indexed component
1971 -- case and then, if there is a range present, either directly or as the
1972 -- constraint of a discrete subtype indication, we evaluate the two
1973 -- bounds of this range.
1975 elsif K = N_Slice then
1976 Evaluate_Name (Prefix (Nam));
1977 Evaluate_Slice_Bounds (Nam);
1979 -- For a type conversion, the expression of the conversion must be the
1980 -- name of an object, and we simply need to evaluate this name.
1982 elsif K = N_Type_Conversion then
1983 Evaluate_Name (Expression (Nam));
1985 -- For a function call, we evaluate the call
1987 elsif K = N_Function_Call then
1988 Force_Evaluation (Nam);
1990 -- The remaining cases are direct name, operator symbol and character
1991 -- literal. In all these cases, we do nothing, since we want to
1992 -- reevaluate each time the renamed object is used.
1994 else
1995 return;
1996 end if;
1997 end Evaluate_Name;
1999 ---------------------------
2000 -- Evaluate_Slice_Bounds --
2001 ---------------------------
2003 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
2004 DR : constant Node_Id := Discrete_Range (Slice);
2005 Constr : Node_Id;
2006 Rexpr : Node_Id;
2008 begin
2009 if Nkind (DR) = N_Range then
2010 Force_Evaluation (Low_Bound (DR));
2011 Force_Evaluation (High_Bound (DR));
2013 elsif Nkind (DR) = N_Subtype_Indication then
2014 Constr := Constraint (DR);
2016 if Nkind (Constr) = N_Range_Constraint then
2017 Rexpr := Range_Expression (Constr);
2019 Force_Evaluation (Low_Bound (Rexpr));
2020 Force_Evaluation (High_Bound (Rexpr));
2021 end if;
2022 end if;
2023 end Evaluate_Slice_Bounds;
2025 ---------------------
2026 -- Evolve_And_Then --
2027 ---------------------
2029 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
2030 begin
2031 if No (Cond) then
2032 Cond := Cond1;
2033 else
2034 Cond :=
2035 Make_And_Then (Sloc (Cond1),
2036 Left_Opnd => Cond,
2037 Right_Opnd => Cond1);
2038 end if;
2039 end Evolve_And_Then;
2041 --------------------
2042 -- Evolve_Or_Else --
2043 --------------------
2045 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
2046 begin
2047 if No (Cond) then
2048 Cond := Cond1;
2049 else
2050 Cond :=
2051 Make_Or_Else (Sloc (Cond1),
2052 Left_Opnd => Cond,
2053 Right_Opnd => Cond1);
2054 end if;
2055 end Evolve_Or_Else;
2057 -----------------------------------------
2058 -- Expand_Static_Predicates_In_Choices --
2059 -----------------------------------------
2061 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
2062 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
2064 Choices : constant List_Id := Discrete_Choices (N);
2066 Choice : Node_Id;
2067 Next_C : Node_Id;
2068 P : Node_Id;
2069 C : Node_Id;
2071 begin
2072 Choice := First (Choices);
2073 while Present (Choice) loop
2074 Next_C := Next (Choice);
2076 -- Check for name of subtype with static predicate
2078 if Is_Entity_Name (Choice)
2079 and then Is_Type (Entity (Choice))
2080 and then Has_Predicates (Entity (Choice))
2081 then
2082 -- Loop through entries in predicate list, converting to choices
2083 -- and inserting in the list before the current choice. Note that
2084 -- if the list is empty, corresponding to a False predicate, then
2085 -- no choices are inserted.
2087 P := First (Static_Discrete_Predicate (Entity (Choice)));
2088 while Present (P) loop
2090 -- If low bound and high bounds are equal, copy simple choice
2092 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
2093 C := New_Copy (Low_Bound (P));
2095 -- Otherwise copy a range
2097 else
2098 C := New_Copy (P);
2099 end if;
2101 -- Change Sloc to referencing choice (rather than the Sloc of
2102 -- the predicate declaration element itself).
2104 Set_Sloc (C, Sloc (Choice));
2105 Insert_Before (Choice, C);
2106 Next (P);
2107 end loop;
2109 -- Delete the predicated entry
2111 Remove (Choice);
2112 end if;
2114 -- Move to next choice to check
2116 Choice := Next_C;
2117 end loop;
2118 end Expand_Static_Predicates_In_Choices;
2120 ------------------------------
2121 -- Expand_Subtype_From_Expr --
2122 ------------------------------
2124 -- This function is applicable for both static and dynamic allocation of
2125 -- objects which are constrained by an initial expression. Basically it
2126 -- transforms an unconstrained subtype indication into a constrained one.
2128 -- The expression may also be transformed in certain cases in order to
2129 -- avoid multiple evaluation. In the static allocation case, the general
2130 -- scheme is:
2132 -- Val : T := Expr;
2134 -- is transformed into
2136 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
2138 -- Here are the main cases :
2140 -- <if Expr is a Slice>
2141 -- Val : T ([Index_Subtype (Expr)]) := Expr;
2143 -- <elsif Expr is a String Literal>
2144 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
2146 -- <elsif Expr is Constrained>
2147 -- subtype T is Type_Of_Expr
2148 -- Val : T := Expr;
2150 -- <elsif Expr is an entity_name>
2151 -- Val : T (constraints taken from Expr) := Expr;
2153 -- <else>
2154 -- type Axxx is access all T;
2155 -- Rval : Axxx := Expr'ref;
2156 -- Val : T (constraints taken from Rval) := Rval.all;
2158 -- ??? note: when the Expression is allocated in the secondary stack
2159 -- we could use it directly instead of copying it by declaring
2160 -- Val : T (...) renames Rval.all
2162 procedure Expand_Subtype_From_Expr
2163 (N : Node_Id;
2164 Unc_Type : Entity_Id;
2165 Subtype_Indic : Node_Id;
2166 Exp : Node_Id)
2168 Loc : constant Source_Ptr := Sloc (N);
2169 Exp_Typ : constant Entity_Id := Etype (Exp);
2170 T : Entity_Id;
2172 begin
2173 -- In general we cannot build the subtype if expansion is disabled,
2174 -- because internal entities may not have been defined. However, to
2175 -- avoid some cascaded errors, we try to continue when the expression is
2176 -- an array (or string), because it is safe to compute the bounds. It is
2177 -- in fact required to do so even in a generic context, because there
2178 -- may be constants that depend on the bounds of a string literal, both
2179 -- standard string types and more generally arrays of characters.
2181 -- In GNATprove mode, these extra subtypes are not needed
2183 if GNATprove_Mode then
2184 return;
2185 end if;
2187 if not Expander_Active
2188 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
2189 then
2190 return;
2191 end if;
2193 if Nkind (Exp) = N_Slice then
2194 declare
2195 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
2197 begin
2198 Rewrite (Subtype_Indic,
2199 Make_Subtype_Indication (Loc,
2200 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
2201 Constraint =>
2202 Make_Index_Or_Discriminant_Constraint (Loc,
2203 Constraints => New_List
2204 (New_Occurrence_Of (Slice_Type, Loc)))));
2206 -- This subtype indication may be used later for constraint checks
2207 -- we better make sure that if a variable was used as a bound of
2208 -- of the original slice, its value is frozen.
2210 Evaluate_Slice_Bounds (Exp);
2211 end;
2213 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2214 Rewrite (Subtype_Indic,
2215 Make_Subtype_Indication (Loc,
2216 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
2217 Constraint =>
2218 Make_Index_Or_Discriminant_Constraint (Loc,
2219 Constraints => New_List (
2220 Make_Literal_Range (Loc,
2221 Literal_Typ => Exp_Typ)))));
2223 -- If the type of the expression is an internally generated type it
2224 -- may not be necessary to create a new subtype. However there are two
2225 -- exceptions: references to the current instances, and aliased array
2226 -- object declarations for which the backend needs to create a template.
2228 elsif Is_Constrained (Exp_Typ)
2229 and then not Is_Class_Wide_Type (Unc_Type)
2230 and then
2231 (Nkind (N) /= N_Object_Declaration
2232 or else not Is_Entity_Name (Expression (N))
2233 or else not Comes_From_Source (Entity (Expression (N)))
2234 or else not Is_Array_Type (Exp_Typ)
2235 or else not Aliased_Present (N))
2236 then
2237 if Is_Itype (Exp_Typ) then
2239 -- Within an initialization procedure, a selected component
2240 -- denotes a component of the enclosing record, and it appears as
2241 -- an actual in a call to its own initialization procedure. If
2242 -- this component depends on the outer discriminant, we must
2243 -- generate the proper actual subtype for it.
2245 if Nkind (Exp) = N_Selected_Component
2246 and then Within_Init_Proc
2247 then
2248 declare
2249 Decl : constant Node_Id :=
2250 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2251 begin
2252 if Present (Decl) then
2253 Insert_Action (N, Decl);
2254 T := Defining_Identifier (Decl);
2255 else
2256 T := Exp_Typ;
2257 end if;
2258 end;
2260 -- No need to generate a new subtype
2262 else
2263 T := Exp_Typ;
2264 end if;
2266 else
2267 T := Make_Temporary (Loc, 'T');
2269 Insert_Action (N,
2270 Make_Subtype_Declaration (Loc,
2271 Defining_Identifier => T,
2272 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
2274 -- This type is marked as an itype even though it has an explicit
2275 -- declaration since otherwise Is_Generic_Actual_Type can get
2276 -- set, resulting in the generation of spurious errors. (See
2277 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
2279 Set_Is_Itype (T);
2280 Set_Associated_Node_For_Itype (T, Exp);
2281 end if;
2283 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
2285 -- Nothing needs to be done for private types with unknown discriminants
2286 -- if the underlying type is not an unconstrained composite type or it
2287 -- is an unchecked union.
2289 elsif Is_Private_Type (Unc_Type)
2290 and then Has_Unknown_Discriminants (Unc_Type)
2291 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
2292 or else Is_Constrained (Underlying_Type (Unc_Type))
2293 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
2294 then
2295 null;
2297 -- Case of derived type with unknown discriminants where the parent type
2298 -- also has unknown discriminants.
2300 elsif Is_Record_Type (Unc_Type)
2301 and then not Is_Class_Wide_Type (Unc_Type)
2302 and then Has_Unknown_Discriminants (Unc_Type)
2303 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2304 then
2305 -- Nothing to be done if no underlying record view available
2307 if No (Underlying_Record_View (Unc_Type)) then
2308 null;
2310 -- Otherwise use the Underlying_Record_View to create the proper
2311 -- constrained subtype for an object of a derived type with unknown
2312 -- discriminants.
2314 else
2315 Remove_Side_Effects (Exp);
2316 Rewrite (Subtype_Indic,
2317 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2318 end if;
2320 -- Renamings of class-wide interface types require no equivalent
2321 -- constrained type declarations because we only need to reference
2322 -- the tag component associated with the interface. The same is
2323 -- presumably true for class-wide types in general, so this test
2324 -- is broadened to include all class-wide renamings, which also
2325 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2326 -- (Is this really correct, or are there some cases of class-wide
2327 -- renamings that require action in this procedure???)
2329 elsif Present (N)
2330 and then Nkind (N) = N_Object_Renaming_Declaration
2331 and then Is_Class_Wide_Type (Unc_Type)
2332 then
2333 null;
2335 -- In Ada 95 nothing to be done if the type of the expression is limited
2336 -- because in this case the expression cannot be copied, and its use can
2337 -- only be by reference.
2339 -- In Ada 2005 the context can be an object declaration whose expression
2340 -- is a function that returns in place. If the nominal subtype has
2341 -- unknown discriminants, the call still provides constraints on the
2342 -- object, and we have to create an actual subtype from it.
2344 -- If the type is class-wide, the expression is dynamically tagged and
2345 -- we do not create an actual subtype either. Ditto for an interface.
2346 -- For now this applies only if the type is immutably limited, and the
2347 -- function being called is build-in-place. This will have to be revised
2348 -- when build-in-place functions are generalized to other types.
2350 elsif Is_Limited_View (Exp_Typ)
2351 and then
2352 (Is_Class_Wide_Type (Exp_Typ)
2353 or else Is_Interface (Exp_Typ)
2354 or else not Has_Unknown_Discriminants (Exp_Typ)
2355 or else not Is_Composite_Type (Unc_Type))
2356 then
2357 null;
2359 -- For limited objects initialized with build in place function calls,
2360 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2361 -- node in the expression initializing the object, which breaks the
2362 -- circuitry that detects and adds the additional arguments to the
2363 -- called function.
2365 elsif Is_Build_In_Place_Function_Call (Exp) then
2366 null;
2368 else
2369 Remove_Side_Effects (Exp);
2370 Rewrite (Subtype_Indic,
2371 Make_Subtype_From_Expr (Exp, Unc_Type));
2372 end if;
2373 end Expand_Subtype_From_Expr;
2375 ----------------------
2376 -- Finalize_Address --
2377 ----------------------
2379 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
2380 Utyp : Entity_Id := Typ;
2382 begin
2383 -- Handle protected class-wide or task class-wide types
2385 if Is_Class_Wide_Type (Utyp) then
2386 if Is_Concurrent_Type (Root_Type (Utyp)) then
2387 Utyp := Root_Type (Utyp);
2389 elsif Is_Private_Type (Root_Type (Utyp))
2390 and then Present (Full_View (Root_Type (Utyp)))
2391 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
2392 then
2393 Utyp := Full_View (Root_Type (Utyp));
2394 end if;
2395 end if;
2397 -- Handle private types
2399 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
2400 Utyp := Full_View (Utyp);
2401 end if;
2403 -- Handle protected and task types
2405 if Is_Concurrent_Type (Utyp)
2406 and then Present (Corresponding_Record_Type (Utyp))
2407 then
2408 Utyp := Corresponding_Record_Type (Utyp);
2409 end if;
2411 Utyp := Underlying_Type (Base_Type (Utyp));
2413 -- Deal with untagged derivation of private views. If the parent is
2414 -- now known to be protected, the finalization routine is the one
2415 -- defined on the corresponding record of the ancestor (corresponding
2416 -- records do not automatically inherit operations, but maybe they
2417 -- should???)
2419 if Is_Untagged_Derivation (Typ) then
2420 if Is_Protected_Type (Typ) then
2421 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
2423 else
2424 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
2426 if Is_Protected_Type (Utyp) then
2427 Utyp := Corresponding_Record_Type (Utyp);
2428 end if;
2429 end if;
2430 end if;
2432 -- If the underlying_type is a subtype, we are dealing with the
2433 -- completion of a private type. We need to access the base type and
2434 -- generate a conversion to it.
2436 if Utyp /= Base_Type (Utyp) then
2437 pragma Assert (Is_Private_Type (Typ));
2439 Utyp := Base_Type (Utyp);
2440 end if;
2442 -- When dealing with an internally built full view for a type with
2443 -- unknown discriminants, use the original record type.
2445 if Is_Underlying_Record_View (Utyp) then
2446 Utyp := Etype (Utyp);
2447 end if;
2449 return TSS (Utyp, TSS_Finalize_Address);
2450 end Finalize_Address;
2452 ------------------------
2453 -- Find_Interface_ADT --
2454 ------------------------
2456 function Find_Interface_ADT
2457 (T : Entity_Id;
2458 Iface : Entity_Id) return Elmt_Id
2460 ADT : Elmt_Id;
2461 Typ : Entity_Id := T;
2463 begin
2464 pragma Assert (Is_Interface (Iface));
2466 -- Handle private types
2468 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2469 Typ := Full_View (Typ);
2470 end if;
2472 -- Handle access types
2474 if Is_Access_Type (Typ) then
2475 Typ := Designated_Type (Typ);
2476 end if;
2478 -- Handle task and protected types implementing interfaces
2480 if Is_Concurrent_Type (Typ) then
2481 Typ := Corresponding_Record_Type (Typ);
2482 end if;
2484 pragma Assert
2485 (not Is_Class_Wide_Type (Typ)
2486 and then Ekind (Typ) /= E_Incomplete_Type);
2488 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2489 return First_Elmt (Access_Disp_Table (Typ));
2491 else
2492 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
2493 while Present (ADT)
2494 and then Present (Related_Type (Node (ADT)))
2495 and then Related_Type (Node (ADT)) /= Iface
2496 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2497 Use_Full_View => True)
2498 loop
2499 Next_Elmt (ADT);
2500 end loop;
2502 pragma Assert (Present (Related_Type (Node (ADT))));
2503 return ADT;
2504 end if;
2505 end Find_Interface_ADT;
2507 ------------------------
2508 -- Find_Interface_Tag --
2509 ------------------------
2511 function Find_Interface_Tag
2512 (T : Entity_Id;
2513 Iface : Entity_Id) return Entity_Id
2515 AI_Tag : Entity_Id;
2516 Found : Boolean := False;
2517 Typ : Entity_Id := T;
2519 procedure Find_Tag (Typ : Entity_Id);
2520 -- Internal subprogram used to recursively climb to the ancestors
2522 --------------
2523 -- Find_Tag --
2524 --------------
2526 procedure Find_Tag (Typ : Entity_Id) is
2527 AI_Elmt : Elmt_Id;
2528 AI : Node_Id;
2530 begin
2531 -- This routine does not handle the case in which the interface is an
2532 -- ancestor of Typ. That case is handled by the enclosing subprogram.
2534 pragma Assert (Typ /= Iface);
2536 -- Climb to the root type handling private types
2538 if Present (Full_View (Etype (Typ))) then
2539 if Full_View (Etype (Typ)) /= Typ then
2540 Find_Tag (Full_View (Etype (Typ)));
2541 end if;
2543 elsif Etype (Typ) /= Typ then
2544 Find_Tag (Etype (Typ));
2545 end if;
2547 -- Traverse the list of interfaces implemented by the type
2549 if not Found
2550 and then Present (Interfaces (Typ))
2551 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
2552 then
2553 -- Skip the tag associated with the primary table
2555 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2556 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2557 pragma Assert (Present (AI_Tag));
2559 AI_Elmt := First_Elmt (Interfaces (Typ));
2560 while Present (AI_Elmt) loop
2561 AI := Node (AI_Elmt);
2563 if AI = Iface
2564 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2565 then
2566 Found := True;
2567 return;
2568 end if;
2570 AI_Tag := Next_Tag_Component (AI_Tag);
2571 Next_Elmt (AI_Elmt);
2572 end loop;
2573 end if;
2574 end Find_Tag;
2576 -- Start of processing for Find_Interface_Tag
2578 begin
2579 pragma Assert (Is_Interface (Iface));
2581 -- Handle access types
2583 if Is_Access_Type (Typ) then
2584 Typ := Designated_Type (Typ);
2585 end if;
2587 -- Handle class-wide types
2589 if Is_Class_Wide_Type (Typ) then
2590 Typ := Root_Type (Typ);
2591 end if;
2593 -- Handle private types
2595 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2596 Typ := Full_View (Typ);
2597 end if;
2599 -- Handle entities from the limited view
2601 if Ekind (Typ) = E_Incomplete_Type then
2602 pragma Assert (Present (Non_Limited_View (Typ)));
2603 Typ := Non_Limited_View (Typ);
2604 end if;
2606 -- Handle task and protected types implementing interfaces
2608 if Is_Concurrent_Type (Typ) then
2609 Typ := Corresponding_Record_Type (Typ);
2610 end if;
2612 -- If the interface is an ancestor of the type, then it shared the
2613 -- primary dispatch table.
2615 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2616 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2617 return First_Tag_Component (Typ);
2619 -- Otherwise we need to search for its associated tag component
2621 else
2622 Find_Tag (Typ);
2623 pragma Assert (Found);
2624 return AI_Tag;
2625 end if;
2626 end Find_Interface_Tag;
2628 ---------------------------
2629 -- Find_Optional_Prim_Op --
2630 ---------------------------
2632 function Find_Optional_Prim_Op
2633 (T : Entity_Id; Name : Name_Id) return Entity_Id
2635 Prim : Elmt_Id;
2636 Typ : Entity_Id := T;
2637 Op : Entity_Id;
2639 begin
2640 if Is_Class_Wide_Type (Typ) then
2641 Typ := Root_Type (Typ);
2642 end if;
2644 Typ := Underlying_Type (Typ);
2646 -- Loop through primitive operations
2648 Prim := First_Elmt (Primitive_Operations (Typ));
2649 while Present (Prim) loop
2650 Op := Node (Prim);
2652 -- We can retrieve primitive operations by name if it is an internal
2653 -- name. For equality we must check that both of its operands have
2654 -- the same type, to avoid confusion with user-defined equalities
2655 -- than may have a non-symmetric signature.
2657 exit when Chars (Op) = Name
2658 and then
2659 (Name /= Name_Op_Eq
2660 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
2662 Next_Elmt (Prim);
2663 end loop;
2665 return Node (Prim); -- Empty if not found
2666 end Find_Optional_Prim_Op;
2668 ---------------------------
2669 -- Find_Optional_Prim_Op --
2670 ---------------------------
2672 function Find_Optional_Prim_Op
2673 (T : Entity_Id;
2674 Name : TSS_Name_Type) return Entity_Id
2676 Inher_Op : Entity_Id := Empty;
2677 Own_Op : Entity_Id := Empty;
2678 Prim_Elmt : Elmt_Id;
2679 Prim_Id : Entity_Id;
2680 Typ : Entity_Id := T;
2682 begin
2683 if Is_Class_Wide_Type (Typ) then
2684 Typ := Root_Type (Typ);
2685 end if;
2687 Typ := Underlying_Type (Typ);
2689 -- This search is based on the assertion that the dispatching version
2690 -- of the TSS routine always precedes the real primitive.
2692 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2693 while Present (Prim_Elmt) loop
2694 Prim_Id := Node (Prim_Elmt);
2696 if Is_TSS (Prim_Id, Name) then
2697 if Present (Alias (Prim_Id)) then
2698 Inher_Op := Prim_Id;
2699 else
2700 Own_Op := Prim_Id;
2701 end if;
2702 end if;
2704 Next_Elmt (Prim_Elmt);
2705 end loop;
2707 if Present (Own_Op) then
2708 return Own_Op;
2709 elsif Present (Inher_Op) then
2710 return Inher_Op;
2711 else
2712 return Empty;
2713 end if;
2714 end Find_Optional_Prim_Op;
2716 ------------------
2717 -- Find_Prim_Op --
2718 ------------------
2720 function Find_Prim_Op
2721 (T : Entity_Id; Name : Name_Id) return Entity_Id
2723 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
2724 begin
2725 if No (Result) then
2726 raise Program_Error;
2727 end if;
2729 return Result;
2730 end Find_Prim_Op;
2732 ------------------
2733 -- Find_Prim_Op --
2734 ------------------
2736 function Find_Prim_Op
2737 (T : Entity_Id;
2738 Name : TSS_Name_Type) return Entity_Id
2740 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
2741 begin
2742 if No (Result) then
2743 raise Program_Error;
2744 end if;
2746 return Result;
2747 end Find_Prim_Op;
2749 ----------------------------
2750 -- Find_Protection_Object --
2751 ----------------------------
2753 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2754 S : Entity_Id;
2756 begin
2757 S := Scop;
2758 while Present (S) loop
2759 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
2760 and then Present (Protection_Object (S))
2761 then
2762 return Protection_Object (S);
2763 end if;
2765 S := Scope (S);
2766 end loop;
2768 -- If we do not find a Protection object in the scope chain, then
2769 -- something has gone wrong, most likely the object was never created.
2771 raise Program_Error;
2772 end Find_Protection_Object;
2774 --------------------------
2775 -- Find_Protection_Type --
2776 --------------------------
2778 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2779 Comp : Entity_Id;
2780 Typ : Entity_Id := Conc_Typ;
2782 begin
2783 if Is_Concurrent_Type (Typ) then
2784 Typ := Corresponding_Record_Type (Typ);
2785 end if;
2787 -- Since restriction violations are not considered serious errors, the
2788 -- expander remains active, but may leave the corresponding record type
2789 -- malformed. In such cases, component _object is not available so do
2790 -- not look for it.
2792 if not Analyzed (Typ) then
2793 return Empty;
2794 end if;
2796 Comp := First_Component (Typ);
2797 while Present (Comp) loop
2798 if Chars (Comp) = Name_uObject then
2799 return Base_Type (Etype (Comp));
2800 end if;
2802 Next_Component (Comp);
2803 end loop;
2805 -- The corresponding record of a protected type should always have an
2806 -- _object field.
2808 raise Program_Error;
2809 end Find_Protection_Type;
2811 -----------------------
2812 -- Find_Hook_Context --
2813 -----------------------
2815 function Find_Hook_Context (N : Node_Id) return Node_Id is
2816 Par : Node_Id;
2817 Top : Node_Id;
2819 Wrapped_Node : Node_Id;
2820 -- Note: if we are in a transient scope, we want to reuse it as
2821 -- the context for actions insertion, if possible. But if N is itself
2822 -- part of the stored actions for the current transient scope,
2823 -- then we need to insert at the appropriate (inner) location in
2824 -- the not as an action on Node_To_Be_Wrapped.
2826 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
2828 begin
2829 -- When the node is inside a case/if expression, the lifetime of any
2830 -- temporary controlled object is extended. Find a suitable insertion
2831 -- node by locating the topmost case or if expressions.
2833 if In_Cond_Expr then
2834 Par := N;
2835 Top := N;
2836 while Present (Par) loop
2837 if Nkind_In (Original_Node (Par), N_Case_Expression,
2838 N_If_Expression)
2839 then
2840 Top := Par;
2842 -- Prevent the search from going too far
2844 elsif Is_Body_Or_Package_Declaration (Par) then
2845 exit;
2846 end if;
2848 Par := Parent (Par);
2849 end loop;
2851 -- The topmost case or if expression is now recovered, but it may
2852 -- still not be the correct place to add generated code. Climb to
2853 -- find a parent that is part of a declarative or statement list,
2854 -- and is not a list of actuals in a call.
2856 Par := Top;
2857 while Present (Par) loop
2858 if Is_List_Member (Par)
2859 and then not Nkind_In (Par, N_Component_Association,
2860 N_Discriminant_Association,
2861 N_Parameter_Association,
2862 N_Pragma_Argument_Association)
2863 and then not Nkind_In
2864 (Parent (Par), N_Function_Call,
2865 N_Procedure_Call_Statement,
2866 N_Entry_Call_Statement)
2868 then
2869 return Par;
2871 -- Prevent the search from going too far
2873 elsif Is_Body_Or_Package_Declaration (Par) then
2874 exit;
2875 end if;
2877 Par := Parent (Par);
2878 end loop;
2880 return Par;
2882 else
2883 Par := N;
2884 while Present (Par) loop
2886 -- Keep climbing past various operators
2888 if Nkind (Parent (Par)) in N_Op
2889 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
2890 then
2891 Par := Parent (Par);
2892 else
2893 exit;
2894 end if;
2895 end loop;
2897 Top := Par;
2899 -- The node may be located in a pragma in which case return the
2900 -- pragma itself:
2902 -- pragma Precondition (... and then Ctrl_Func_Call ...);
2904 -- Similar case occurs when the node is related to an object
2905 -- declaration or assignment:
2907 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
2909 -- Another case to consider is when the node is part of a return
2910 -- statement:
2912 -- return ... and then Ctrl_Func_Call ...;
2914 -- Another case is when the node acts as a formal in a procedure
2915 -- call statement:
2917 -- Proc (... and then Ctrl_Func_Call ...);
2919 if Scope_Is_Transient then
2920 Wrapped_Node := Node_To_Be_Wrapped;
2921 else
2922 Wrapped_Node := Empty;
2923 end if;
2925 while Present (Par) loop
2926 if Par = Wrapped_Node
2927 or else Nkind_In (Par, N_Assignment_Statement,
2928 N_Object_Declaration,
2929 N_Pragma,
2930 N_Procedure_Call_Statement,
2931 N_Simple_Return_Statement)
2932 then
2933 return Par;
2935 -- Prevent the search from going too far
2937 elsif Is_Body_Or_Package_Declaration (Par) then
2938 exit;
2939 end if;
2941 Par := Parent (Par);
2942 end loop;
2944 -- Return the topmost short circuit operator
2946 return Top;
2947 end if;
2948 end Find_Hook_Context;
2950 ------------------------------
2951 -- Following_Address_Clause --
2952 ------------------------------
2954 function Following_Address_Clause (D : Node_Id) return Node_Id is
2955 Id : constant Entity_Id := Defining_Identifier (D);
2956 Result : Node_Id;
2957 Par : Node_Id;
2959 function Check_Decls (D : Node_Id) return Node_Id;
2960 -- This internal function differs from the main function in that it
2961 -- gets called to deal with a following package private part, and
2962 -- it checks declarations starting with D (the main function checks
2963 -- declarations following D). If D is Empty, then Empty is returned.
2965 -----------------
2966 -- Check_Decls --
2967 -----------------
2969 function Check_Decls (D : Node_Id) return Node_Id is
2970 Decl : Node_Id;
2972 begin
2973 Decl := D;
2974 while Present (Decl) loop
2975 if Nkind (Decl) = N_At_Clause
2976 and then Chars (Identifier (Decl)) = Chars (Id)
2977 then
2978 return Decl;
2980 elsif Nkind (Decl) = N_Attribute_Definition_Clause
2981 and then Chars (Decl) = Name_Address
2982 and then Chars (Name (Decl)) = Chars (Id)
2983 then
2984 return Decl;
2985 end if;
2987 Next (Decl);
2988 end loop;
2990 -- Otherwise not found, return Empty
2992 return Empty;
2993 end Check_Decls;
2995 -- Start of processing for Following_Address_Clause
2997 begin
2998 -- If parser detected no address clause for the identifier in question,
2999 -- then the answer is a quick NO, without the need for a search.
3001 if not Get_Name_Table_Boolean1 (Chars (Id)) then
3002 return Empty;
3003 end if;
3005 -- Otherwise search current declarative unit
3007 Result := Check_Decls (Next (D));
3009 if Present (Result) then
3010 return Result;
3011 end if;
3013 -- Check for possible package private part following
3015 Par := Parent (D);
3017 if Nkind (Par) = N_Package_Specification
3018 and then Visible_Declarations (Par) = List_Containing (D)
3019 and then Present (Private_Declarations (Par))
3020 then
3021 -- Private part present, check declarations there
3023 return Check_Decls (First (Private_Declarations (Par)));
3025 else
3026 -- No private part, clause not found, return Empty
3028 return Empty;
3029 end if;
3030 end Following_Address_Clause;
3032 ----------------------
3033 -- Force_Evaluation --
3034 ----------------------
3036 procedure Force_Evaluation
3037 (Exp : Node_Id;
3038 Name_Req : Boolean := False;
3039 Related_Id : Entity_Id := Empty;
3040 Is_Low_Bound : Boolean := False;
3041 Is_High_Bound : Boolean := False)
3043 begin
3044 Remove_Side_Effects
3045 (Exp => Exp,
3046 Name_Req => Name_Req,
3047 Variable_Ref => True,
3048 Renaming_Req => False,
3049 Related_Id => Related_Id,
3050 Is_Low_Bound => Is_Low_Bound,
3051 Is_High_Bound => Is_High_Bound);
3052 end Force_Evaluation;
3054 ---------------------------------
3055 -- Fully_Qualified_Name_String --
3056 ---------------------------------
3058 function Fully_Qualified_Name_String
3059 (E : Entity_Id;
3060 Append_NUL : Boolean := True) return String_Id
3062 procedure Internal_Full_Qualified_Name (E : Entity_Id);
3063 -- Compute recursively the qualified name without NUL at the end, adding
3064 -- it to the currently started string being generated
3066 ----------------------------------
3067 -- Internal_Full_Qualified_Name --
3068 ----------------------------------
3070 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
3071 Ent : Entity_Id;
3073 begin
3074 -- Deal properly with child units
3076 if Nkind (E) = N_Defining_Program_Unit_Name then
3077 Ent := Defining_Identifier (E);
3078 else
3079 Ent := E;
3080 end if;
3082 -- Compute qualification recursively (only "Standard" has no scope)
3084 if Present (Scope (Scope (Ent))) then
3085 Internal_Full_Qualified_Name (Scope (Ent));
3086 Store_String_Char (Get_Char_Code ('.'));
3087 end if;
3089 -- Every entity should have a name except some expanded blocks
3090 -- don't bother about those.
3092 if Chars (Ent) = No_Name then
3093 return;
3094 end if;
3096 -- Generates the entity name in upper case
3098 Get_Decoded_Name_String (Chars (Ent));
3099 Set_All_Upper_Case;
3100 Store_String_Chars (Name_Buffer (1 .. Name_Len));
3101 return;
3102 end Internal_Full_Qualified_Name;
3104 -- Start of processing for Full_Qualified_Name
3106 begin
3107 Start_String;
3108 Internal_Full_Qualified_Name (E);
3110 if Append_NUL then
3111 Store_String_Char (Get_Char_Code (ASCII.NUL));
3112 end if;
3114 return End_String;
3115 end Fully_Qualified_Name_String;
3117 ------------------------
3118 -- Generate_Poll_Call --
3119 ------------------------
3121 procedure Generate_Poll_Call (N : Node_Id) is
3122 begin
3123 -- No poll call if polling not active
3125 if not Polling_Required then
3126 return;
3128 -- Otherwise generate require poll call
3130 else
3131 Insert_Before_And_Analyze (N,
3132 Make_Procedure_Call_Statement (Sloc (N),
3133 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
3134 end if;
3135 end Generate_Poll_Call;
3137 ---------------------------------
3138 -- Get_Current_Value_Condition --
3139 ---------------------------------
3141 -- Note: the implementation of this procedure is very closely tied to the
3142 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
3143 -- interpret Current_Value fields set by the Set procedure, so the two
3144 -- procedures need to be closely coordinated.
3146 procedure Get_Current_Value_Condition
3147 (Var : Node_Id;
3148 Op : out Node_Kind;
3149 Val : out Node_Id)
3151 Loc : constant Source_Ptr := Sloc (Var);
3152 Ent : constant Entity_Id := Entity (Var);
3154 procedure Process_Current_Value_Condition
3155 (N : Node_Id;
3156 S : Boolean);
3157 -- N is an expression which holds either True (S = True) or False (S =
3158 -- False) in the condition. This procedure digs out the expression and
3159 -- if it refers to Ent, sets Op and Val appropriately.
3161 -------------------------------------
3162 -- Process_Current_Value_Condition --
3163 -------------------------------------
3165 procedure Process_Current_Value_Condition
3166 (N : Node_Id;
3167 S : Boolean)
3169 Cond : Node_Id;
3170 Prev_Cond : Node_Id;
3171 Sens : Boolean;
3173 begin
3174 Cond := N;
3175 Sens := S;
3177 loop
3178 Prev_Cond := Cond;
3180 -- Deal with NOT operators, inverting sense
3182 while Nkind (Cond) = N_Op_Not loop
3183 Cond := Right_Opnd (Cond);
3184 Sens := not Sens;
3185 end loop;
3187 -- Deal with conversions, qualifications, and expressions with
3188 -- actions.
3190 while Nkind_In (Cond,
3191 N_Type_Conversion,
3192 N_Qualified_Expression,
3193 N_Expression_With_Actions)
3194 loop
3195 Cond := Expression (Cond);
3196 end loop;
3198 exit when Cond = Prev_Cond;
3199 end loop;
3201 -- Deal with AND THEN and AND cases
3203 if Nkind_In (Cond, N_And_Then, N_Op_And) then
3205 -- Don't ever try to invert a condition that is of the form of an
3206 -- AND or AND THEN (since we are not doing sufficiently general
3207 -- processing to allow this).
3209 if Sens = False then
3210 Op := N_Empty;
3211 Val := Empty;
3212 return;
3213 end if;
3215 -- Recursively process AND and AND THEN branches
3217 Process_Current_Value_Condition (Left_Opnd (Cond), True);
3219 if Op /= N_Empty then
3220 return;
3221 end if;
3223 Process_Current_Value_Condition (Right_Opnd (Cond), True);
3224 return;
3226 -- Case of relational operator
3228 elsif Nkind (Cond) in N_Op_Compare then
3229 Op := Nkind (Cond);
3231 -- Invert sense of test if inverted test
3233 if Sens = False then
3234 case Op is
3235 when N_Op_Eq => Op := N_Op_Ne;
3236 when N_Op_Ne => Op := N_Op_Eq;
3237 when N_Op_Lt => Op := N_Op_Ge;
3238 when N_Op_Gt => Op := N_Op_Le;
3239 when N_Op_Le => Op := N_Op_Gt;
3240 when N_Op_Ge => Op := N_Op_Lt;
3241 when others => raise Program_Error;
3242 end case;
3243 end if;
3245 -- Case of entity op value
3247 if Is_Entity_Name (Left_Opnd (Cond))
3248 and then Ent = Entity (Left_Opnd (Cond))
3249 and then Compile_Time_Known_Value (Right_Opnd (Cond))
3250 then
3251 Val := Right_Opnd (Cond);
3253 -- Case of value op entity
3255 elsif Is_Entity_Name (Right_Opnd (Cond))
3256 and then Ent = Entity (Right_Opnd (Cond))
3257 and then Compile_Time_Known_Value (Left_Opnd (Cond))
3258 then
3259 Val := Left_Opnd (Cond);
3261 -- We are effectively swapping operands
3263 case Op is
3264 when N_Op_Eq => null;
3265 when N_Op_Ne => null;
3266 when N_Op_Lt => Op := N_Op_Gt;
3267 when N_Op_Gt => Op := N_Op_Lt;
3268 when N_Op_Le => Op := N_Op_Ge;
3269 when N_Op_Ge => Op := N_Op_Le;
3270 when others => raise Program_Error;
3271 end case;
3273 else
3274 Op := N_Empty;
3275 end if;
3277 return;
3279 elsif Nkind_In (Cond,
3280 N_Type_Conversion,
3281 N_Qualified_Expression,
3282 N_Expression_With_Actions)
3283 then
3284 Cond := Expression (Cond);
3286 -- Case of Boolean variable reference, return as though the
3287 -- reference had said var = True.
3289 else
3290 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
3291 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
3293 if Sens = False then
3294 Op := N_Op_Ne;
3295 else
3296 Op := N_Op_Eq;
3297 end if;
3298 end if;
3299 end if;
3300 end Process_Current_Value_Condition;
3302 -- Start of processing for Get_Current_Value_Condition
3304 begin
3305 Op := N_Empty;
3306 Val := Empty;
3308 -- Immediate return, nothing doing, if this is not an object
3310 if Ekind (Ent) not in Object_Kind then
3311 return;
3312 end if;
3314 -- Otherwise examine current value
3316 declare
3317 CV : constant Node_Id := Current_Value (Ent);
3318 Sens : Boolean;
3319 Stm : Node_Id;
3321 begin
3322 -- If statement. Condition is known true in THEN section, known False
3323 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
3325 if Nkind (CV) = N_If_Statement then
3327 -- Before start of IF statement
3329 if Loc < Sloc (CV) then
3330 return;
3332 -- After end of IF statement
3334 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
3335 return;
3336 end if;
3338 -- At this stage we know that we are within the IF statement, but
3339 -- unfortunately, the tree does not record the SLOC of the ELSE so
3340 -- we cannot use a simple SLOC comparison to distinguish between
3341 -- the then/else statements, so we have to climb the tree.
3343 declare
3344 N : Node_Id;
3346 begin
3347 N := Parent (Var);
3348 while Parent (N) /= CV loop
3349 N := Parent (N);
3351 -- If we fall off the top of the tree, then that's odd, but
3352 -- perhaps it could occur in some error situation, and the
3353 -- safest response is simply to assume that the outcome of
3354 -- the condition is unknown. No point in bombing during an
3355 -- attempt to optimize things.
3357 if No (N) then
3358 return;
3359 end if;
3360 end loop;
3362 -- Now we have N pointing to a node whose parent is the IF
3363 -- statement in question, so now we can tell if we are within
3364 -- the THEN statements.
3366 if Is_List_Member (N)
3367 and then List_Containing (N) = Then_Statements (CV)
3368 then
3369 Sens := True;
3371 -- If the variable reference does not come from source, we
3372 -- cannot reliably tell whether it appears in the else part.
3373 -- In particular, if it appears in generated code for a node
3374 -- that requires finalization, it may be attached to a list
3375 -- that has not been yet inserted into the code. For now,
3376 -- treat it as unknown.
3378 elsif not Comes_From_Source (N) then
3379 return;
3381 -- Otherwise we must be in ELSIF or ELSE part
3383 else
3384 Sens := False;
3385 end if;
3386 end;
3388 -- ELSIF part. Condition is known true within the referenced
3389 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
3390 -- and unknown before the ELSE part or after the IF statement.
3392 elsif Nkind (CV) = N_Elsif_Part then
3394 -- if the Elsif_Part had condition_actions, the elsif has been
3395 -- rewritten as a nested if, and the original elsif_part is
3396 -- detached from the tree, so there is no way to obtain useful
3397 -- information on the current value of the variable.
3398 -- Can this be improved ???
3400 if No (Parent (CV)) then
3401 return;
3402 end if;
3404 Stm := Parent (CV);
3406 -- If the tree has been otherwise rewritten there is nothing
3407 -- else to be done either.
3409 if Nkind (Stm) /= N_If_Statement then
3410 return;
3411 end if;
3413 -- Before start of ELSIF part
3415 if Loc < Sloc (CV) then
3416 return;
3418 -- After end of IF statement
3420 elsif Loc >= Sloc (Stm) +
3421 Text_Ptr (UI_To_Int (End_Span (Stm)))
3422 then
3423 return;
3424 end if;
3426 -- Again we lack the SLOC of the ELSE, so we need to climb the
3427 -- tree to see if we are within the ELSIF part in question.
3429 declare
3430 N : Node_Id;
3432 begin
3433 N := Parent (Var);
3434 while Parent (N) /= Stm loop
3435 N := Parent (N);
3437 -- If we fall off the top of the tree, then that's odd, but
3438 -- perhaps it could occur in some error situation, and the
3439 -- safest response is simply to assume that the outcome of
3440 -- the condition is unknown. No point in bombing during an
3441 -- attempt to optimize things.
3443 if No (N) then
3444 return;
3445 end if;
3446 end loop;
3448 -- Now we have N pointing to a node whose parent is the IF
3449 -- statement in question, so see if is the ELSIF part we want.
3450 -- the THEN statements.
3452 if N = CV then
3453 Sens := True;
3455 -- Otherwise we must be in subsequent ELSIF or ELSE part
3457 else
3458 Sens := False;
3459 end if;
3460 end;
3462 -- Iteration scheme of while loop. The condition is known to be
3463 -- true within the body of the loop.
3465 elsif Nkind (CV) = N_Iteration_Scheme then
3466 declare
3467 Loop_Stmt : constant Node_Id := Parent (CV);
3469 begin
3470 -- Before start of body of loop
3472 if Loc < Sloc (Loop_Stmt) then
3473 return;
3475 -- After end of LOOP statement
3477 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
3478 return;
3480 -- We are within the body of the loop
3482 else
3483 Sens := True;
3484 end if;
3485 end;
3487 -- All other cases of Current_Value settings
3489 else
3490 return;
3491 end if;
3493 -- If we fall through here, then we have a reportable condition, Sens
3494 -- is True if the condition is true and False if it needs inverting.
3496 Process_Current_Value_Condition (Condition (CV), Sens);
3497 end;
3498 end Get_Current_Value_Condition;
3500 ---------------------
3501 -- Get_Stream_Size --
3502 ---------------------
3504 function Get_Stream_Size (E : Entity_Id) return Uint is
3505 begin
3506 -- If we have a Stream_Size clause for this type use it
3508 if Has_Stream_Size_Clause (E) then
3509 return Static_Integer (Expression (Stream_Size_Clause (E)));
3511 -- Otherwise the Stream_Size if the size of the type
3513 else
3514 return Esize (E);
3515 end if;
3516 end Get_Stream_Size;
3518 ---------------------------
3519 -- Has_Access_Constraint --
3520 ---------------------------
3522 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3523 Disc : Entity_Id;
3524 T : constant Entity_Id := Etype (E);
3526 begin
3527 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
3528 Disc := First_Discriminant (T);
3529 while Present (Disc) loop
3530 if Is_Access_Type (Etype (Disc)) then
3531 return True;
3532 end if;
3534 Next_Discriminant (Disc);
3535 end loop;
3537 return False;
3538 else
3539 return False;
3540 end if;
3541 end Has_Access_Constraint;
3543 -----------------------------------------------------
3544 -- Has_Annotate_Pragma_For_External_Axiomatization --
3545 -----------------------------------------------------
3547 function Has_Annotate_Pragma_For_External_Axiomatization
3548 (E : Entity_Id) return Boolean
3550 function Is_Annotate_Pragma_For_External_Axiomatization
3551 (N : Node_Id) return Boolean;
3552 -- Returns whether N is
3553 -- pragma Annotate (GNATprove, External_Axiomatization);
3555 ----------------------------------------------------
3556 -- Is_Annotate_Pragma_For_External_Axiomatization --
3557 ----------------------------------------------------
3559 -- The general form of pragma Annotate is
3561 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
3562 -- ARG ::= NAME | EXPRESSION
3564 -- The first two arguments are by convention intended to refer to an
3565 -- external tool and a tool-specific function. These arguments are
3566 -- not analyzed.
3568 -- The following is used to annotate a package specification which
3569 -- GNATprove should treat specially, because the axiomatization of
3570 -- this unit is given by the user instead of being automatically
3571 -- generated.
3573 -- pragma Annotate (GNATprove, External_Axiomatization);
3575 function Is_Annotate_Pragma_For_External_Axiomatization
3576 (N : Node_Id) return Boolean
3578 Name_GNATprove : constant String :=
3579 "gnatprove";
3580 Name_External_Axiomatization : constant String :=
3581 "external_axiomatization";
3582 -- Special names
3584 begin
3585 if Nkind (N) = N_Pragma
3586 and then Get_Pragma_Id (Pragma_Name (N)) = Pragma_Annotate
3587 and then List_Length (Pragma_Argument_Associations (N)) = 2
3588 then
3589 declare
3590 Arg1 : constant Node_Id :=
3591 First (Pragma_Argument_Associations (N));
3592 Arg2 : constant Node_Id := Next (Arg1);
3593 Nam1 : Name_Id;
3594 Nam2 : Name_Id;
3596 begin
3597 -- Fill in Name_Buffer with Name_GNATprove first, and then with
3598 -- Name_External_Axiomatization so that Name_Find returns the
3599 -- corresponding name. This takes care of all possible casings.
3601 Name_Len := 0;
3602 Add_Str_To_Name_Buffer (Name_GNATprove);
3603 Nam1 := Name_Find;
3605 Name_Len := 0;
3606 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
3607 Nam2 := Name_Find;
3609 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
3610 and then
3611 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
3612 end;
3614 else
3615 return False;
3616 end if;
3617 end Is_Annotate_Pragma_For_External_Axiomatization;
3619 -- Local variables
3621 Decl : Node_Id;
3622 Vis_Decls : List_Id;
3623 N : Node_Id;
3625 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
3627 begin
3628 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
3629 Decl := Parent (Parent (E));
3630 else
3631 Decl := Parent (E);
3632 end if;
3634 Vis_Decls := Visible_Declarations (Decl);
3636 N := First (Vis_Decls);
3637 while Present (N) loop
3639 -- Skip declarations generated by the frontend. Skip all pragmas
3640 -- that are not the desired Annotate pragma. Stop the search on
3641 -- the first non-pragma source declaration.
3643 if Comes_From_Source (N) then
3644 if Nkind (N) = N_Pragma then
3645 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
3646 return True;
3647 end if;
3648 else
3649 return False;
3650 end if;
3651 end if;
3653 Next (N);
3654 end loop;
3656 return False;
3657 end Has_Annotate_Pragma_For_External_Axiomatization;
3659 --------------------
3660 -- Homonym_Number --
3661 --------------------
3663 function Homonym_Number (Subp : Entity_Id) return Nat is
3664 Count : Nat;
3665 Hom : Entity_Id;
3667 begin
3668 Count := 1;
3669 Hom := Homonym (Subp);
3670 while Present (Hom) loop
3671 if Scope (Hom) = Scope (Subp) then
3672 Count := Count + 1;
3673 end if;
3675 Hom := Homonym (Hom);
3676 end loop;
3678 return Count;
3679 end Homonym_Number;
3681 -----------------------------------
3682 -- In_Library_Level_Package_Body --
3683 -----------------------------------
3685 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3686 begin
3687 -- First determine whether the entity appears at the library level, then
3688 -- look at the containing unit.
3690 if Is_Library_Level_Entity (Id) then
3691 declare
3692 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3694 begin
3695 return Nkind (Unit (Container)) = N_Package_Body;
3696 end;
3697 end if;
3699 return False;
3700 end In_Library_Level_Package_Body;
3702 ------------------------------
3703 -- In_Unconditional_Context --
3704 ------------------------------
3706 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3707 P : Node_Id;
3709 begin
3710 P := Node;
3711 while Present (P) loop
3712 case Nkind (P) is
3713 when N_Subprogram_Body =>
3714 return True;
3716 when N_If_Statement =>
3717 return False;
3719 when N_Loop_Statement =>
3720 return False;
3722 when N_Case_Statement =>
3723 return False;
3725 when others =>
3726 P := Parent (P);
3727 end case;
3728 end loop;
3730 return False;
3731 end In_Unconditional_Context;
3733 -------------------
3734 -- Insert_Action --
3735 -------------------
3737 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3738 begin
3739 if Present (Ins_Action) then
3740 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3741 end if;
3742 end Insert_Action;
3744 -- Version with check(s) suppressed
3746 procedure Insert_Action
3747 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3749 begin
3750 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3751 end Insert_Action;
3753 -------------------------
3754 -- Insert_Action_After --
3755 -------------------------
3757 procedure Insert_Action_After
3758 (Assoc_Node : Node_Id;
3759 Ins_Action : Node_Id)
3761 begin
3762 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3763 end Insert_Action_After;
3765 --------------------
3766 -- Insert_Actions --
3767 --------------------
3769 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3770 N : Node_Id;
3771 P : Node_Id;
3773 Wrapped_Node : Node_Id := Empty;
3775 begin
3776 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3777 return;
3778 end if;
3780 -- Ignore insert of actions from inside default expression (or other
3781 -- similar "spec expression") in the special spec-expression analyze
3782 -- mode. Any insertions at this point have no relevance, since we are
3783 -- only doing the analyze to freeze the types of any static expressions.
3784 -- See section "Handling of Default Expressions" in the spec of package
3785 -- Sem for further details.
3787 if In_Spec_Expression then
3788 return;
3789 end if;
3791 -- If the action derives from stuff inside a record, then the actions
3792 -- are attached to the current scope, to be inserted and analyzed on
3793 -- exit from the scope. The reason for this is that we may also be
3794 -- generating freeze actions at the same time, and they must eventually
3795 -- be elaborated in the correct order.
3797 if Is_Record_Type (Current_Scope)
3798 and then not Is_Frozen (Current_Scope)
3799 then
3800 if No (Scope_Stack.Table
3801 (Scope_Stack.Last).Pending_Freeze_Actions)
3802 then
3803 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3804 Ins_Actions;
3805 else
3806 Append_List
3807 (Ins_Actions,
3808 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3809 end if;
3811 return;
3812 end if;
3814 -- We now intend to climb up the tree to find the right point to
3815 -- insert the actions. We start at Assoc_Node, unless this node is a
3816 -- subexpression in which case we start with its parent. We do this for
3817 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3818 -- itself one of the special nodes like N_And_Then, then we assume that
3819 -- an initial request to insert actions for such a node does not expect
3820 -- the actions to get deposited in the node for later handling when the
3821 -- node is expanded, since clearly the node is being dealt with by the
3822 -- caller. Note that in the subexpression case, N is always the child we
3823 -- came from.
3825 -- N_Raise_xxx_Error is an annoying special case, it is a statement if
3826 -- it has type Standard_Void_Type, and a subexpression otherwise.
3827 -- otherwise. Procedure calls, and similarly procedure attribute
3828 -- references, are also statements.
3830 if Nkind (Assoc_Node) in N_Subexpr
3831 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
3832 or else Etype (Assoc_Node) /= Standard_Void_Type)
3833 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
3834 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
3835 or else not Is_Procedure_Attribute_Name
3836 (Attribute_Name (Assoc_Node)))
3837 then
3838 N := Assoc_Node;
3839 P := Parent (Assoc_Node);
3841 -- Non-subexpression case. Note that N is initially Empty in this case
3842 -- (N is only guaranteed Non-Empty in the subexpr case).
3844 else
3845 N := Empty;
3846 P := Assoc_Node;
3847 end if;
3849 -- Capture root of the transient scope
3851 if Scope_Is_Transient then
3852 Wrapped_Node := Node_To_Be_Wrapped;
3853 end if;
3855 loop
3856 pragma Assert (Present (P));
3858 -- Make sure that inserted actions stay in the transient scope
3860 if Present (Wrapped_Node) and then N = Wrapped_Node then
3861 Store_Before_Actions_In_Scope (Ins_Actions);
3862 return;
3863 end if;
3865 case Nkind (P) is
3867 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3868 -- in the Actions field of the right operand. They will be moved
3869 -- out further when the AND THEN or OR ELSE operator is expanded.
3870 -- Nothing special needs to be done for the left operand since
3871 -- in that case the actions are executed unconditionally.
3873 when N_Short_Circuit =>
3874 if N = Right_Opnd (P) then
3876 -- We are now going to either append the actions to the
3877 -- actions field of the short-circuit operation. We will
3878 -- also analyze the actions now.
3880 -- This analysis is really too early, the proper thing would
3881 -- be to just park them there now, and only analyze them if
3882 -- we find we really need them, and to it at the proper
3883 -- final insertion point. However attempting to this proved
3884 -- tricky, so for now we just kill current values before and
3885 -- after the analyze call to make sure we avoid peculiar
3886 -- optimizations from this out of order insertion.
3888 Kill_Current_Values;
3890 -- If P has already been expanded, we can't park new actions
3891 -- on it, so we need to expand them immediately, introducing
3892 -- an Expression_With_Actions. N can't be an expression
3893 -- with actions, or else then the actions would have been
3894 -- inserted at an inner level.
3896 if Analyzed (P) then
3897 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
3898 Rewrite (N,
3899 Make_Expression_With_Actions (Sloc (N),
3900 Actions => Ins_Actions,
3901 Expression => Relocate_Node (N)));
3902 Analyze_And_Resolve (N);
3904 elsif Present (Actions (P)) then
3905 Insert_List_After_And_Analyze
3906 (Last (Actions (P)), Ins_Actions);
3907 else
3908 Set_Actions (P, Ins_Actions);
3909 Analyze_List (Actions (P));
3910 end if;
3912 Kill_Current_Values;
3914 return;
3915 end if;
3917 -- Then or Else dependent expression of an if expression. Add
3918 -- actions to Then_Actions or Else_Actions field as appropriate.
3919 -- The actions will be moved further out when the if is expanded.
3921 when N_If_Expression =>
3922 declare
3923 ThenX : constant Node_Id := Next (First (Expressions (P)));
3924 ElseX : constant Node_Id := Next (ThenX);
3926 begin
3927 -- If the enclosing expression is already analyzed, as
3928 -- is the case for nested elaboration checks, insert the
3929 -- conditional further out.
3931 if Analyzed (P) then
3932 null;
3934 -- Actions belong to the then expression, temporarily place
3935 -- them as Then_Actions of the if expression. They will be
3936 -- moved to the proper place later when the if expression
3937 -- is expanded.
3939 elsif N = ThenX then
3940 if Present (Then_Actions (P)) then
3941 Insert_List_After_And_Analyze
3942 (Last (Then_Actions (P)), Ins_Actions);
3943 else
3944 Set_Then_Actions (P, Ins_Actions);
3945 Analyze_List (Then_Actions (P));
3946 end if;
3948 return;
3950 -- Actions belong to the else expression, temporarily place
3951 -- them as Else_Actions of the if expression. They will be
3952 -- moved to the proper place later when the if expression
3953 -- is expanded.
3955 elsif N = ElseX then
3956 if Present (Else_Actions (P)) then
3957 Insert_List_After_And_Analyze
3958 (Last (Else_Actions (P)), Ins_Actions);
3959 else
3960 Set_Else_Actions (P, Ins_Actions);
3961 Analyze_List (Else_Actions (P));
3962 end if;
3964 return;
3966 -- Actions belong to the condition. In this case they are
3967 -- unconditionally executed, and so we can continue the
3968 -- search for the proper insert point.
3970 else
3971 null;
3972 end if;
3973 end;
3975 -- Alternative of case expression, we place the action in the
3976 -- Actions field of the case expression alternative, this will
3977 -- be handled when the case expression is expanded.
3979 when N_Case_Expression_Alternative =>
3980 if Present (Actions (P)) then
3981 Insert_List_After_And_Analyze
3982 (Last (Actions (P)), Ins_Actions);
3983 else
3984 Set_Actions (P, Ins_Actions);
3985 Analyze_List (Actions (P));
3986 end if;
3988 return;
3990 -- Case of appearing within an Expressions_With_Actions node. When
3991 -- the new actions come from the expression of the expression with
3992 -- actions, they must be added to the existing actions. The other
3993 -- alternative is when the new actions are related to one of the
3994 -- existing actions of the expression with actions, and should
3995 -- never reach here: if actions are inserted on a statement
3996 -- within the Actions of an expression with actions, or on some
3997 -- sub-expression of such a statement, then the outermost proper
3998 -- insertion point is right before the statement, and we should
3999 -- never climb up as far as the N_Expression_With_Actions itself.
4001 when N_Expression_With_Actions =>
4002 if N = Expression (P) then
4003 if Is_Empty_List (Actions (P)) then
4004 Append_List_To (Actions (P), Ins_Actions);
4005 Analyze_List (Actions (P));
4006 else
4007 Insert_List_After_And_Analyze
4008 (Last (Actions (P)), Ins_Actions);
4009 end if;
4011 return;
4013 else
4014 raise Program_Error;
4015 end if;
4017 -- Case of appearing in the condition of a while expression or
4018 -- elsif. We insert the actions into the Condition_Actions field.
4019 -- They will be moved further out when the while loop or elsif
4020 -- is analyzed.
4022 when N_Iteration_Scheme |
4023 N_Elsif_Part
4025 if N = Condition (P) then
4026 if Present (Condition_Actions (P)) then
4027 Insert_List_After_And_Analyze
4028 (Last (Condition_Actions (P)), Ins_Actions);
4029 else
4030 Set_Condition_Actions (P, Ins_Actions);
4032 -- Set the parent of the insert actions explicitly. This
4033 -- is not a syntactic field, but we need the parent field
4034 -- set, in particular so that freeze can understand that
4035 -- it is dealing with condition actions, and properly
4036 -- insert the freezing actions.
4038 Set_Parent (Ins_Actions, P);
4039 Analyze_List (Condition_Actions (P));
4040 end if;
4042 return;
4043 end if;
4045 -- Statements, declarations, pragmas, representation clauses
4047 when
4048 -- Statements
4050 N_Procedure_Call_Statement |
4051 N_Statement_Other_Than_Procedure_Call |
4053 -- Pragmas
4055 N_Pragma |
4057 -- Representation_Clause
4059 N_At_Clause |
4060 N_Attribute_Definition_Clause |
4061 N_Enumeration_Representation_Clause |
4062 N_Record_Representation_Clause |
4064 -- Declarations
4066 N_Abstract_Subprogram_Declaration |
4067 N_Entry_Body |
4068 N_Exception_Declaration |
4069 N_Exception_Renaming_Declaration |
4070 N_Expression_Function |
4071 N_Formal_Abstract_Subprogram_Declaration |
4072 N_Formal_Concrete_Subprogram_Declaration |
4073 N_Formal_Object_Declaration |
4074 N_Formal_Type_Declaration |
4075 N_Full_Type_Declaration |
4076 N_Function_Instantiation |
4077 N_Generic_Function_Renaming_Declaration |
4078 N_Generic_Package_Declaration |
4079 N_Generic_Package_Renaming_Declaration |
4080 N_Generic_Procedure_Renaming_Declaration |
4081 N_Generic_Subprogram_Declaration |
4082 N_Implicit_Label_Declaration |
4083 N_Incomplete_Type_Declaration |
4084 N_Number_Declaration |
4085 N_Object_Declaration |
4086 N_Object_Renaming_Declaration |
4087 N_Package_Body |
4088 N_Package_Body_Stub |
4089 N_Package_Declaration |
4090 N_Package_Instantiation |
4091 N_Package_Renaming_Declaration |
4092 N_Private_Extension_Declaration |
4093 N_Private_Type_Declaration |
4094 N_Procedure_Instantiation |
4095 N_Protected_Body |
4096 N_Protected_Body_Stub |
4097 N_Protected_Type_Declaration |
4098 N_Single_Task_Declaration |
4099 N_Subprogram_Body |
4100 N_Subprogram_Body_Stub |
4101 N_Subprogram_Declaration |
4102 N_Subprogram_Renaming_Declaration |
4103 N_Subtype_Declaration |
4104 N_Task_Body |
4105 N_Task_Body_Stub |
4106 N_Task_Type_Declaration |
4108 -- Use clauses can appear in lists of declarations
4110 N_Use_Package_Clause |
4111 N_Use_Type_Clause |
4113 -- Freeze entity behaves like a declaration or statement
4115 N_Freeze_Entity |
4116 N_Freeze_Generic_Entity
4118 -- Do not insert here if the item is not a list member (this
4119 -- happens for example with a triggering statement, and the
4120 -- proper approach is to insert before the entire select).
4122 if not Is_List_Member (P) then
4123 null;
4125 -- Do not insert if parent of P is an N_Component_Association
4126 -- node (i.e. we are in the context of an N_Aggregate or
4127 -- N_Extension_Aggregate node. In this case we want to insert
4128 -- before the entire aggregate.
4130 elsif Nkind (Parent (P)) = N_Component_Association then
4131 null;
4133 -- Do not insert if the parent of P is either an N_Variant node
4134 -- or an N_Record_Definition node, meaning in either case that
4135 -- P is a member of a component list, and that therefore the
4136 -- actions should be inserted outside the complete record
4137 -- declaration.
4139 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
4140 null;
4142 -- Do not insert freeze nodes within the loop generated for
4143 -- an aggregate, because they may be elaborated too late for
4144 -- subsequent use in the back end: within a package spec the
4145 -- loop is part of the elaboration procedure and is only
4146 -- elaborated during the second pass.
4148 -- If the loop comes from source, or the entity is local to the
4149 -- loop itself it must remain within.
4151 elsif Nkind (Parent (P)) = N_Loop_Statement
4152 and then not Comes_From_Source (Parent (P))
4153 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
4154 and then
4155 Scope (Entity (First (Ins_Actions))) /= Current_Scope
4156 then
4157 null;
4159 -- Otherwise we can go ahead and do the insertion
4161 elsif P = Wrapped_Node then
4162 Store_Before_Actions_In_Scope (Ins_Actions);
4163 return;
4165 else
4166 Insert_List_Before_And_Analyze (P, Ins_Actions);
4167 return;
4168 end if;
4170 -- A special case, N_Raise_xxx_Error can act either as a statement
4171 -- or a subexpression. We tell the difference by looking at the
4172 -- Etype. It is set to Standard_Void_Type in the statement case.
4174 when
4175 N_Raise_xxx_Error =>
4176 if Etype (P) = Standard_Void_Type then
4177 if P = Wrapped_Node then
4178 Store_Before_Actions_In_Scope (Ins_Actions);
4179 else
4180 Insert_List_Before_And_Analyze (P, Ins_Actions);
4181 end if;
4183 return;
4185 -- In the subexpression case, keep climbing
4187 else
4188 null;
4189 end if;
4191 -- If a component association appears within a loop created for
4192 -- an array aggregate, attach the actions to the association so
4193 -- they can be subsequently inserted within the loop. For other
4194 -- component associations insert outside of the aggregate. For
4195 -- an association that will generate a loop, its Loop_Actions
4196 -- attribute is already initialized (see exp_aggr.adb).
4198 -- The list of loop_actions can in turn generate additional ones,
4199 -- that are inserted before the associated node. If the associated
4200 -- node is outside the aggregate, the new actions are collected
4201 -- at the end of the loop actions, to respect the order in which
4202 -- they are to be elaborated.
4204 when
4205 N_Component_Association =>
4206 if Nkind (Parent (P)) = N_Aggregate
4207 and then Present (Loop_Actions (P))
4208 then
4209 if Is_Empty_List (Loop_Actions (P)) then
4210 Set_Loop_Actions (P, Ins_Actions);
4211 Analyze_List (Ins_Actions);
4213 else
4214 declare
4215 Decl : Node_Id;
4217 begin
4218 -- Check whether these actions were generated by a
4219 -- declaration that is part of the loop_ actions
4220 -- for the component_association.
4222 Decl := Assoc_Node;
4223 while Present (Decl) loop
4224 exit when Parent (Decl) = P
4225 and then Is_List_Member (Decl)
4226 and then
4227 List_Containing (Decl) = Loop_Actions (P);
4228 Decl := Parent (Decl);
4229 end loop;
4231 if Present (Decl) then
4232 Insert_List_Before_And_Analyze
4233 (Decl, Ins_Actions);
4234 else
4235 Insert_List_After_And_Analyze
4236 (Last (Loop_Actions (P)), Ins_Actions);
4237 end if;
4238 end;
4239 end if;
4241 return;
4243 else
4244 null;
4245 end if;
4247 -- Another special case, an attribute denoting a procedure call
4249 when
4250 N_Attribute_Reference =>
4251 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
4252 if P = Wrapped_Node then
4253 Store_Before_Actions_In_Scope (Ins_Actions);
4254 else
4255 Insert_List_Before_And_Analyze (P, Ins_Actions);
4256 end if;
4258 return;
4260 -- In the subexpression case, keep climbing
4262 else
4263 null;
4264 end if;
4266 -- A contract node should not belong to the tree
4268 when N_Contract =>
4269 raise Program_Error;
4271 -- For all other node types, keep climbing tree
4273 when
4274 N_Abortable_Part |
4275 N_Accept_Alternative |
4276 N_Access_Definition |
4277 N_Access_Function_Definition |
4278 N_Access_Procedure_Definition |
4279 N_Access_To_Object_Definition |
4280 N_Aggregate |
4281 N_Allocator |
4282 N_Aspect_Specification |
4283 N_Case_Expression |
4284 N_Case_Statement_Alternative |
4285 N_Character_Literal |
4286 N_Compilation_Unit |
4287 N_Compilation_Unit_Aux |
4288 N_Component_Clause |
4289 N_Component_Declaration |
4290 N_Component_Definition |
4291 N_Component_List |
4292 N_Constrained_Array_Definition |
4293 N_Decimal_Fixed_Point_Definition |
4294 N_Defining_Character_Literal |
4295 N_Defining_Identifier |
4296 N_Defining_Operator_Symbol |
4297 N_Defining_Program_Unit_Name |
4298 N_Delay_Alternative |
4299 N_Delta_Constraint |
4300 N_Derived_Type_Definition |
4301 N_Designator |
4302 N_Digits_Constraint |
4303 N_Discriminant_Association |
4304 N_Discriminant_Specification |
4305 N_Empty |
4306 N_Entry_Body_Formal_Part |
4307 N_Entry_Call_Alternative |
4308 N_Entry_Declaration |
4309 N_Entry_Index_Specification |
4310 N_Enumeration_Type_Definition |
4311 N_Error |
4312 N_Exception_Handler |
4313 N_Expanded_Name |
4314 N_Explicit_Dereference |
4315 N_Extension_Aggregate |
4316 N_Floating_Point_Definition |
4317 N_Formal_Decimal_Fixed_Point_Definition |
4318 N_Formal_Derived_Type_Definition |
4319 N_Formal_Discrete_Type_Definition |
4320 N_Formal_Floating_Point_Definition |
4321 N_Formal_Modular_Type_Definition |
4322 N_Formal_Ordinary_Fixed_Point_Definition |
4323 N_Formal_Package_Declaration |
4324 N_Formal_Private_Type_Definition |
4325 N_Formal_Incomplete_Type_Definition |
4326 N_Formal_Signed_Integer_Type_Definition |
4327 N_Function_Call |
4328 N_Function_Specification |
4329 N_Generic_Association |
4330 N_Handled_Sequence_Of_Statements |
4331 N_Identifier |
4332 N_In |
4333 N_Index_Or_Discriminant_Constraint |
4334 N_Indexed_Component |
4335 N_Integer_Literal |
4336 N_Iterator_Specification |
4337 N_Itype_Reference |
4338 N_Label |
4339 N_Loop_Parameter_Specification |
4340 N_Mod_Clause |
4341 N_Modular_Type_Definition |
4342 N_Not_In |
4343 N_Null |
4344 N_Op_Abs |
4345 N_Op_Add |
4346 N_Op_And |
4347 N_Op_Concat |
4348 N_Op_Divide |
4349 N_Op_Eq |
4350 N_Op_Expon |
4351 N_Op_Ge |
4352 N_Op_Gt |
4353 N_Op_Le |
4354 N_Op_Lt |
4355 N_Op_Minus |
4356 N_Op_Mod |
4357 N_Op_Multiply |
4358 N_Op_Ne |
4359 N_Op_Not |
4360 N_Op_Or |
4361 N_Op_Plus |
4362 N_Op_Rem |
4363 N_Op_Rotate_Left |
4364 N_Op_Rotate_Right |
4365 N_Op_Shift_Left |
4366 N_Op_Shift_Right |
4367 N_Op_Shift_Right_Arithmetic |
4368 N_Op_Subtract |
4369 N_Op_Xor |
4370 N_Operator_Symbol |
4371 N_Ordinary_Fixed_Point_Definition |
4372 N_Others_Choice |
4373 N_Package_Specification |
4374 N_Parameter_Association |
4375 N_Parameter_Specification |
4376 N_Pop_Constraint_Error_Label |
4377 N_Pop_Program_Error_Label |
4378 N_Pop_Storage_Error_Label |
4379 N_Pragma_Argument_Association |
4380 N_Procedure_Specification |
4381 N_Protected_Definition |
4382 N_Push_Constraint_Error_Label |
4383 N_Push_Program_Error_Label |
4384 N_Push_Storage_Error_Label |
4385 N_Qualified_Expression |
4386 N_Quantified_Expression |
4387 N_Raise_Expression |
4388 N_Range |
4389 N_Range_Constraint |
4390 N_Real_Literal |
4391 N_Real_Range_Specification |
4392 N_Record_Definition |
4393 N_Reference |
4394 N_SCIL_Dispatch_Table_Tag_Init |
4395 N_SCIL_Dispatching_Call |
4396 N_SCIL_Membership_Test |
4397 N_Selected_Component |
4398 N_Signed_Integer_Type_Definition |
4399 N_Single_Protected_Declaration |
4400 N_Slice |
4401 N_String_Literal |
4402 N_Subtype_Indication |
4403 N_Subunit |
4404 N_Task_Definition |
4405 N_Terminate_Alternative |
4406 N_Triggering_Alternative |
4407 N_Type_Conversion |
4408 N_Unchecked_Expression |
4409 N_Unchecked_Type_Conversion |
4410 N_Unconstrained_Array_Definition |
4411 N_Unused_At_End |
4412 N_Unused_At_Start |
4413 N_Variant |
4414 N_Variant_Part |
4415 N_Validate_Unchecked_Conversion |
4416 N_With_Clause
4418 null;
4420 end case;
4422 -- If we fall through above tests, keep climbing tree
4424 N := P;
4426 if Nkind (Parent (N)) = N_Subunit then
4428 -- This is the proper body corresponding to a stub. Insertion must
4429 -- be done at the point of the stub, which is in the declarative
4430 -- part of the parent unit.
4432 P := Corresponding_Stub (Parent (N));
4434 else
4435 P := Parent (N);
4436 end if;
4437 end loop;
4438 end Insert_Actions;
4440 -- Version with check(s) suppressed
4442 procedure Insert_Actions
4443 (Assoc_Node : Node_Id;
4444 Ins_Actions : List_Id;
4445 Suppress : Check_Id)
4447 begin
4448 if Suppress = All_Checks then
4449 declare
4450 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
4451 begin
4452 Scope_Suppress.Suppress := (others => True);
4453 Insert_Actions (Assoc_Node, Ins_Actions);
4454 Scope_Suppress.Suppress := Sva;
4455 end;
4457 else
4458 declare
4459 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
4460 begin
4461 Scope_Suppress.Suppress (Suppress) := True;
4462 Insert_Actions (Assoc_Node, Ins_Actions);
4463 Scope_Suppress.Suppress (Suppress) := Svg;
4464 end;
4465 end if;
4466 end Insert_Actions;
4468 --------------------------
4469 -- Insert_Actions_After --
4470 --------------------------
4472 procedure Insert_Actions_After
4473 (Assoc_Node : Node_Id;
4474 Ins_Actions : List_Id)
4476 begin
4477 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
4478 Store_After_Actions_In_Scope (Ins_Actions);
4479 else
4480 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
4481 end if;
4482 end Insert_Actions_After;
4484 ------------------------
4485 -- Insert_Declaration --
4486 ------------------------
4488 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
4489 P : Node_Id;
4491 begin
4492 pragma Assert (Nkind (N) in N_Subexpr);
4494 -- Climb until we find a procedure or a package
4496 P := N;
4497 loop
4498 pragma Assert (Present (Parent (P)));
4499 P := Parent (P);
4501 if Is_List_Member (P) then
4502 exit when Nkind_In (Parent (P), N_Package_Specification,
4503 N_Subprogram_Body);
4505 -- Special handling for handled sequence of statements, we must
4506 -- insert in the statements not the exception handlers!
4508 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
4509 P := First (Statements (Parent (P)));
4510 exit;
4511 end if;
4512 end if;
4513 end loop;
4515 -- Now do the insertion
4517 Insert_Before (P, Decl);
4518 Analyze (Decl);
4519 end Insert_Declaration;
4521 ---------------------------------
4522 -- Insert_Library_Level_Action --
4523 ---------------------------------
4525 procedure Insert_Library_Level_Action (N : Node_Id) is
4526 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4528 begin
4529 Push_Scope (Cunit_Entity (Main_Unit));
4530 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
4532 if No (Actions (Aux)) then
4533 Set_Actions (Aux, New_List (N));
4534 else
4535 Append (N, Actions (Aux));
4536 end if;
4538 Analyze (N);
4539 Pop_Scope;
4540 end Insert_Library_Level_Action;
4542 ----------------------------------
4543 -- Insert_Library_Level_Actions --
4544 ----------------------------------
4546 procedure Insert_Library_Level_Actions (L : List_Id) is
4547 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4549 begin
4550 if Is_Non_Empty_List (L) then
4551 Push_Scope (Cunit_Entity (Main_Unit));
4552 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
4554 if No (Actions (Aux)) then
4555 Set_Actions (Aux, L);
4556 Analyze_List (L);
4557 else
4558 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
4559 end if;
4561 Pop_Scope;
4562 end if;
4563 end Insert_Library_Level_Actions;
4565 ----------------------
4566 -- Inside_Init_Proc --
4567 ----------------------
4569 function Inside_Init_Proc return Boolean is
4570 S : Entity_Id;
4572 begin
4573 S := Current_Scope;
4574 while Present (S) and then S /= Standard_Standard loop
4575 if Is_Init_Proc (S) then
4576 return True;
4577 else
4578 S := Scope (S);
4579 end if;
4580 end loop;
4582 return False;
4583 end Inside_Init_Proc;
4585 ----------------------------
4586 -- Is_All_Null_Statements --
4587 ----------------------------
4589 function Is_All_Null_Statements (L : List_Id) return Boolean is
4590 Stm : Node_Id;
4592 begin
4593 Stm := First (L);
4594 while Present (Stm) loop
4595 if Nkind (Stm) /= N_Null_Statement then
4596 return False;
4597 end if;
4599 Next (Stm);
4600 end loop;
4602 return True;
4603 end Is_All_Null_Statements;
4605 --------------------------------------------------
4606 -- Is_Displacement_Of_Object_Or_Function_Result --
4607 --------------------------------------------------
4609 function Is_Displacement_Of_Object_Or_Function_Result
4610 (Obj_Id : Entity_Id) return Boolean
4612 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
4613 -- Determine if particular node denotes a controlled function call. The
4614 -- call may have been heavily expanded.
4616 function Is_Displace_Call (N : Node_Id) return Boolean;
4617 -- Determine whether a particular node is a call to Ada.Tags.Displace.
4618 -- The call might be nested within other actions such as conversions.
4620 function Is_Source_Object (N : Node_Id) return Boolean;
4621 -- Determine whether a particular node denotes a source object
4623 ---------------------------------
4624 -- Is_Controlled_Function_Call --
4625 ---------------------------------
4627 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
4628 Expr : Node_Id := Original_Node (N);
4630 begin
4631 if Nkind (Expr) = N_Function_Call then
4632 Expr := Name (Expr);
4634 -- When a function call appears in Object.Operation format, the
4635 -- original representation has two possible forms depending on the
4636 -- availability of actual parameters:
4638 -- Obj.Func_Call N_Selected_Component
4639 -- Obj.Func_Call (Param) N_Indexed_Component
4641 else
4642 if Nkind (Expr) = N_Indexed_Component then
4643 Expr := Prefix (Expr);
4644 end if;
4646 if Nkind (Expr) = N_Selected_Component then
4647 Expr := Selector_Name (Expr);
4648 end if;
4649 end if;
4651 return
4652 Nkind_In (Expr, N_Expanded_Name, N_Identifier)
4653 and then Ekind (Entity (Expr)) = E_Function
4654 and then Needs_Finalization (Etype (Entity (Expr)));
4655 end Is_Controlled_Function_Call;
4657 ----------------------
4658 -- Is_Displace_Call --
4659 ----------------------
4661 function Is_Displace_Call (N : Node_Id) return Boolean is
4662 Call : Node_Id := N;
4664 begin
4665 -- Strip various actions which may precede a call to Displace
4667 loop
4668 if Nkind (Call) = N_Explicit_Dereference then
4669 Call := Prefix (Call);
4671 elsif Nkind_In (Call, N_Type_Conversion,
4672 N_Unchecked_Type_Conversion)
4673 then
4674 Call := Expression (Call);
4676 else
4677 exit;
4678 end if;
4679 end loop;
4681 return
4682 Present (Call)
4683 and then Nkind (Call) = N_Function_Call
4684 and then Is_RTE (Entity (Name (Call)), RE_Displace);
4685 end Is_Displace_Call;
4687 ----------------------
4688 -- Is_Source_Object --
4689 ----------------------
4691 function Is_Source_Object (N : Node_Id) return Boolean is
4692 begin
4693 return
4694 Present (N)
4695 and then Nkind (N) in N_Has_Entity
4696 and then Is_Object (Entity (N))
4697 and then Comes_From_Source (N);
4698 end Is_Source_Object;
4700 -- Local variables
4702 Decl : constant Node_Id := Parent (Obj_Id);
4703 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4704 Orig_Decl : constant Node_Id := Original_Node (Decl);
4706 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
4708 begin
4709 -- Case 1:
4711 -- Obj : CW_Type := Function_Call (...);
4713 -- rewritten into:
4715 -- Tmp : ... := Function_Call (...)'reference;
4716 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
4718 -- where the return type of the function and the class-wide type require
4719 -- dispatch table pointer displacement.
4721 -- Case 2:
4723 -- Obj : CW_Type := Src_Obj;
4725 -- rewritten into:
4727 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
4729 -- where the type of the source object and the class-wide type require
4730 -- dispatch table pointer displacement.
4732 return
4733 Nkind (Decl) = N_Object_Renaming_Declaration
4734 and then Nkind (Orig_Decl) = N_Object_Declaration
4735 and then Comes_From_Source (Orig_Decl)
4736 and then Is_Class_Wide_Type (Obj_Typ)
4737 and then Is_Displace_Call (Renamed_Object (Obj_Id))
4738 and then
4739 (Is_Controlled_Function_Call (Expression (Orig_Decl))
4740 or else Is_Source_Object (Expression (Orig_Decl)));
4741 end Is_Displacement_Of_Object_Or_Function_Result;
4743 ------------------------------
4744 -- Is_Finalizable_Transient --
4745 ------------------------------
4747 function Is_Finalizable_Transient
4748 (Decl : Node_Id;
4749 Rel_Node : Node_Id) return Boolean
4751 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4752 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4754 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
4755 -- Determine whether transient object Trans_Id is initialized either
4756 -- by a function call which returns an access type or simply renames
4757 -- another pointer.
4759 function Initialized_By_Aliased_BIP_Func_Call
4760 (Trans_Id : Entity_Id) return Boolean;
4761 -- Determine whether transient object Trans_Id is initialized by a
4762 -- build-in-place function call where the BIPalloc parameter is of
4763 -- value 1 and BIPaccess is not null. This case creates an aliasing
4764 -- between the returned value and the value denoted by BIPaccess.
4766 function Is_Aliased
4767 (Trans_Id : Entity_Id;
4768 First_Stmt : Node_Id) return Boolean;
4769 -- Determine whether transient object Trans_Id has been renamed or
4770 -- aliased through 'reference in the statement list starting from
4771 -- First_Stmt.
4773 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4774 -- Determine whether transient object Trans_Id is allocated on the heap
4776 function Is_Iterated_Container
4777 (Trans_Id : Entity_Id;
4778 First_Stmt : Node_Id) return Boolean;
4779 -- Determine whether transient object Trans_Id denotes a container which
4780 -- is in the process of being iterated in the statement list starting
4781 -- from First_Stmt.
4783 ---------------------------
4784 -- Initialized_By_Access --
4785 ---------------------------
4787 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4788 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4790 begin
4791 return
4792 Present (Expr)
4793 and then Nkind (Expr) /= N_Reference
4794 and then Is_Access_Type (Etype (Expr));
4795 end Initialized_By_Access;
4797 ------------------------------------------
4798 -- Initialized_By_Aliased_BIP_Func_Call --
4799 ------------------------------------------
4801 function Initialized_By_Aliased_BIP_Func_Call
4802 (Trans_Id : Entity_Id) return Boolean
4804 Call : Node_Id := Expression (Parent (Trans_Id));
4806 begin
4807 -- Build-in-place calls usually appear in 'reference format
4809 if Nkind (Call) = N_Reference then
4810 Call := Prefix (Call);
4811 end if;
4813 if Is_Build_In_Place_Function_Call (Call) then
4814 declare
4815 Access_Nam : Name_Id := No_Name;
4816 Access_OK : Boolean := False;
4817 Actual : Node_Id;
4818 Alloc_Nam : Name_Id := No_Name;
4819 Alloc_OK : Boolean := False;
4820 Formal : Node_Id;
4821 Func_Id : Entity_Id;
4822 Param : Node_Id;
4824 begin
4825 -- Examine all parameter associations of the function call
4827 Param := First (Parameter_Associations (Call));
4828 while Present (Param) loop
4829 if Nkind (Param) = N_Parameter_Association
4830 and then Nkind (Selector_Name (Param)) = N_Identifier
4831 then
4832 Actual := Explicit_Actual_Parameter (Param);
4833 Formal := Selector_Name (Param);
4835 -- Construct the names of formals BIPaccess and BIPalloc
4836 -- using the function name retrieved from an arbitrary
4837 -- formal.
4839 if Access_Nam = No_Name
4840 and then Alloc_Nam = No_Name
4841 and then Present (Entity (Formal))
4842 then
4843 Func_Id := Scope (Entity (Formal));
4845 Access_Nam :=
4846 New_External_Name (Chars (Func_Id),
4847 BIP_Formal_Suffix (BIP_Object_Access));
4849 Alloc_Nam :=
4850 New_External_Name (Chars (Func_Id),
4851 BIP_Formal_Suffix (BIP_Alloc_Form));
4852 end if;
4854 -- A match for BIPaccess => Temp has been found
4856 if Chars (Formal) = Access_Nam
4857 and then Nkind (Actual) /= N_Null
4858 then
4859 Access_OK := True;
4860 end if;
4862 -- A match for BIPalloc => 1 has been found
4864 if Chars (Formal) = Alloc_Nam
4865 and then Nkind (Actual) = N_Integer_Literal
4866 and then Intval (Actual) = Uint_1
4867 then
4868 Alloc_OK := True;
4869 end if;
4870 end if;
4872 Next (Param);
4873 end loop;
4875 return Access_OK and Alloc_OK;
4876 end;
4877 end if;
4879 return False;
4880 end Initialized_By_Aliased_BIP_Func_Call;
4882 ----------------
4883 -- Is_Aliased --
4884 ----------------
4886 function Is_Aliased
4887 (Trans_Id : Entity_Id;
4888 First_Stmt : Node_Id) return Boolean
4890 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
4891 -- Given an object renaming declaration, retrieve the entity of the
4892 -- renamed name. Return Empty if the renamed name is anything other
4893 -- than a variable or a constant.
4895 -------------------------
4896 -- Find_Renamed_Object --
4897 -------------------------
4899 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
4900 Ren_Obj : Node_Id := Empty;
4902 function Find_Object (N : Node_Id) return Traverse_Result;
4903 -- Try to detect an object which is either a constant or a
4904 -- variable.
4906 -----------------
4907 -- Find_Object --
4908 -----------------
4910 function Find_Object (N : Node_Id) return Traverse_Result is
4911 begin
4912 -- Stop the search once a constant or a variable has been
4913 -- detected.
4915 if Nkind (N) = N_Identifier
4916 and then Present (Entity (N))
4917 and then Ekind_In (Entity (N), E_Constant, E_Variable)
4918 then
4919 Ren_Obj := Entity (N);
4920 return Abandon;
4921 end if;
4923 return OK;
4924 end Find_Object;
4926 procedure Search is new Traverse_Proc (Find_Object);
4928 -- Local variables
4930 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
4932 -- Start of processing for Find_Renamed_Object
4934 begin
4935 -- Actions related to dispatching calls may appear as renamings of
4936 -- tags. Do not process this type of renaming because it does not
4937 -- use the actual value of the object.
4939 if not Is_RTE (Typ, RE_Tag_Ptr) then
4940 Search (Name (Ren_Decl));
4941 end if;
4943 return Ren_Obj;
4944 end Find_Renamed_Object;
4946 -- Local variables
4948 Expr : Node_Id;
4949 Ren_Obj : Entity_Id;
4950 Stmt : Node_Id;
4952 -- Start of processing for Is_Aliased
4954 begin
4955 -- A controlled transient object is not considered aliased when it
4956 -- appears inside an expression_with_actions node even when there are
4957 -- explicit aliases of it:
4959 -- do
4960 -- Trans_Id : Ctrl_Typ ...; -- controlled transient object
4961 -- Alias : ... := Trans_Id; -- object is aliased
4962 -- Val : constant Boolean :=
4963 -- ... Alias ...; -- aliasing ends
4964 -- <finalize Trans_Id> -- object safe to finalize
4965 -- in Val end;
4967 -- Expansion ensures that all aliases are encapsulated in the actions
4968 -- list and do not leak to the expression by forcing the evaluation
4969 -- of the expression.
4971 if Nkind (Rel_Node) = N_Expression_With_Actions then
4972 return False;
4974 -- Otherwise examine the statements after the controlled transient
4975 -- object and look for various forms of aliasing.
4977 else
4978 Stmt := First_Stmt;
4979 while Present (Stmt) loop
4980 if Nkind (Stmt) = N_Object_Declaration then
4981 Expr := Expression (Stmt);
4983 -- Aliasing of the form:
4984 -- Obj : ... := Trans_Id'reference;
4986 if Present (Expr)
4987 and then Nkind (Expr) = N_Reference
4988 and then Nkind (Prefix (Expr)) = N_Identifier
4989 and then Entity (Prefix (Expr)) = Trans_Id
4990 then
4991 return True;
4992 end if;
4994 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
4995 Ren_Obj := Find_Renamed_Object (Stmt);
4997 -- Aliasing of the form:
4998 -- Obj : ... renames ... Trans_Id ...;
5000 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
5001 return True;
5002 end if;
5003 end if;
5005 Next (Stmt);
5006 end loop;
5008 return False;
5009 end if;
5010 end Is_Aliased;
5012 ------------------
5013 -- Is_Allocated --
5014 ------------------
5016 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
5017 Expr : constant Node_Id := Expression (Parent (Trans_Id));
5018 begin
5019 return
5020 Is_Access_Type (Etype (Trans_Id))
5021 and then Present (Expr)
5022 and then Nkind (Expr) = N_Allocator;
5023 end Is_Allocated;
5025 ---------------------------
5026 -- Is_Iterated_Container --
5027 ---------------------------
5029 function Is_Iterated_Container
5030 (Trans_Id : Entity_Id;
5031 First_Stmt : Node_Id) return Boolean
5033 Aspect : Node_Id;
5034 Call : Node_Id;
5035 Iter : Entity_Id;
5036 Param : Node_Id;
5037 Stmt : Node_Id;
5038 Typ : Entity_Id;
5040 begin
5041 -- It is not possible to iterate over containers in non-Ada 2012 code
5043 if Ada_Version < Ada_2012 then
5044 return False;
5045 end if;
5047 Typ := Etype (Trans_Id);
5049 -- Handle access type created for secondary stack use
5051 if Is_Access_Type (Typ) then
5052 Typ := Designated_Type (Typ);
5053 end if;
5055 -- Look for aspect Default_Iterator. It may be part of a type
5056 -- declaration for a container, or inherited from a base type
5057 -- or parent type.
5059 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
5061 if Present (Aspect) then
5062 Iter := Entity (Aspect);
5064 -- Examine the statements following the container object and
5065 -- look for a call to the default iterate routine where the
5066 -- first parameter is the transient. Such a call appears as:
5068 -- It : Access_To_CW_Iterator :=
5069 -- Iterate (Tran_Id.all, ...)'reference;
5071 Stmt := First_Stmt;
5072 while Present (Stmt) loop
5074 -- Detect an object declaration which is initialized by a
5075 -- secondary stack function call.
5077 if Nkind (Stmt) = N_Object_Declaration
5078 and then Present (Expression (Stmt))
5079 and then Nkind (Expression (Stmt)) = N_Reference
5080 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
5081 then
5082 Call := Prefix (Expression (Stmt));
5084 -- The call must invoke the default iterate routine of
5085 -- the container and the transient object must appear as
5086 -- the first actual parameter. Skip any calls whose names
5087 -- are not entities.
5089 if Is_Entity_Name (Name (Call))
5090 and then Entity (Name (Call)) = Iter
5091 and then Present (Parameter_Associations (Call))
5092 then
5093 Param := First (Parameter_Associations (Call));
5095 if Nkind (Param) = N_Explicit_Dereference
5096 and then Entity (Prefix (Param)) = Trans_Id
5097 then
5098 return True;
5099 end if;
5100 end if;
5101 end if;
5103 Next (Stmt);
5104 end loop;
5105 end if;
5107 return False;
5108 end Is_Iterated_Container;
5110 -- Local variables
5112 Desig : Entity_Id := Obj_Typ;
5114 -- Start of processing for Is_Finalizable_Transient
5116 begin
5117 -- Handle access types
5119 if Is_Access_Type (Desig) then
5120 Desig := Available_View (Designated_Type (Desig));
5121 end if;
5123 return
5124 Ekind_In (Obj_Id, E_Constant, E_Variable)
5125 and then Needs_Finalization (Desig)
5126 and then Requires_Transient_Scope (Desig)
5127 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
5129 -- Do not consider renamed or 'reference-d transient objects because
5130 -- the act of renaming extends the object's lifetime.
5132 and then not Is_Aliased (Obj_Id, Decl)
5134 -- Do not consider transient objects allocated on the heap since
5135 -- they are attached to a finalization master.
5137 and then not Is_Allocated (Obj_Id)
5139 -- If the transient object is a pointer, check that it is not
5140 -- initialized by a function that returns a pointer or acts as a
5141 -- renaming of another pointer.
5143 and then
5144 (not Is_Access_Type (Obj_Typ)
5145 or else not Initialized_By_Access (Obj_Id))
5147 -- Do not consider transient objects which act as indirect aliases
5148 -- of build-in-place function results.
5150 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
5152 -- Do not consider conversions of tags to class-wide types
5154 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
5156 -- Do not consider iterators because those are treated as normal
5157 -- controlled objects and are processed by the usual finalization
5158 -- machinery. This avoids the double finalization of an iterator.
5160 and then not Is_Iterator (Desig)
5162 -- Do not consider containers in the context of iterator loops. Such
5163 -- transient objects must exist for as long as the loop is around,
5164 -- otherwise any operation carried out by the iterator will fail.
5166 and then not Is_Iterated_Container (Obj_Id, Decl);
5167 end Is_Finalizable_Transient;
5169 ---------------------------------
5170 -- Is_Fully_Repped_Tagged_Type --
5171 ---------------------------------
5173 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
5174 U : constant Entity_Id := Underlying_Type (T);
5175 Comp : Entity_Id;
5177 begin
5178 if No (U) or else not Is_Tagged_Type (U) then
5179 return False;
5180 elsif Has_Discriminants (U) then
5181 return False;
5182 elsif not Has_Specified_Layout (U) then
5183 return False;
5184 end if;
5186 -- Here we have a tagged type, see if it has any unlayed out fields
5187 -- other than a possible tag and parent fields. If so, we return False.
5189 Comp := First_Component (U);
5190 while Present (Comp) loop
5191 if not Is_Tag (Comp)
5192 and then Chars (Comp) /= Name_uParent
5193 and then No (Component_Clause (Comp))
5194 then
5195 return False;
5196 else
5197 Next_Component (Comp);
5198 end if;
5199 end loop;
5201 -- All components are layed out
5203 return True;
5204 end Is_Fully_Repped_Tagged_Type;
5206 ----------------------------------
5207 -- Is_Library_Level_Tagged_Type --
5208 ----------------------------------
5210 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
5211 begin
5212 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
5213 end Is_Library_Level_Tagged_Type;
5215 --------------------------
5216 -- Is_Non_BIP_Func_Call --
5217 --------------------------
5219 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
5220 begin
5221 -- The expected call is of the format
5223 -- Func_Call'reference
5225 return
5226 Nkind (Expr) = N_Reference
5227 and then Nkind (Prefix (Expr)) = N_Function_Call
5228 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
5229 end Is_Non_BIP_Func_Call;
5231 ------------------------------------
5232 -- Is_Object_Access_BIP_Func_Call --
5233 ------------------------------------
5235 function Is_Object_Access_BIP_Func_Call
5236 (Expr : Node_Id;
5237 Obj_Id : Entity_Id) return Boolean
5239 Access_Nam : Name_Id := No_Name;
5240 Actual : Node_Id;
5241 Call : Node_Id;
5242 Formal : Node_Id;
5243 Param : Node_Id;
5245 begin
5246 -- Build-in-place calls usually appear in 'reference format. Note that
5247 -- the accessibility check machinery may add an extra 'reference due to
5248 -- side effect removal.
5250 Call := Expr;
5251 while Nkind (Call) = N_Reference loop
5252 Call := Prefix (Call);
5253 end loop;
5255 if Nkind_In (Call, N_Qualified_Expression,
5256 N_Unchecked_Type_Conversion)
5257 then
5258 Call := Expression (Call);
5259 end if;
5261 if Is_Build_In_Place_Function_Call (Call) then
5263 -- Examine all parameter associations of the function call
5265 Param := First (Parameter_Associations (Call));
5266 while Present (Param) loop
5267 if Nkind (Param) = N_Parameter_Association
5268 and then Nkind (Selector_Name (Param)) = N_Identifier
5269 then
5270 Formal := Selector_Name (Param);
5271 Actual := Explicit_Actual_Parameter (Param);
5273 -- Construct the name of formal BIPaccess. It is much easier to
5274 -- extract the name of the function using an arbitrary formal's
5275 -- scope rather than the Name field of Call.
5277 if Access_Nam = No_Name and then Present (Entity (Formal)) then
5278 Access_Nam :=
5279 New_External_Name
5280 (Chars (Scope (Entity (Formal))),
5281 BIP_Formal_Suffix (BIP_Object_Access));
5282 end if;
5284 -- A match for BIPaccess => Obj_Id'Unrestricted_Access has been
5285 -- found.
5287 if Chars (Formal) = Access_Nam
5288 and then Nkind (Actual) = N_Attribute_Reference
5289 and then Attribute_Name (Actual) = Name_Unrestricted_Access
5290 and then Nkind (Prefix (Actual)) = N_Identifier
5291 and then Entity (Prefix (Actual)) = Obj_Id
5292 then
5293 return True;
5294 end if;
5295 end if;
5297 Next (Param);
5298 end loop;
5299 end if;
5301 return False;
5302 end Is_Object_Access_BIP_Func_Call;
5304 ----------------------------------
5305 -- Is_Possibly_Unaligned_Object --
5306 ----------------------------------
5308 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
5309 T : constant Entity_Id := Etype (N);
5311 begin
5312 -- Objects are never unaligned on VMs
5314 if VM_Target /= No_VM then
5315 return False;
5316 end if;
5318 -- If renamed object, apply test to underlying object
5320 if Is_Entity_Name (N)
5321 and then Is_Object (Entity (N))
5322 and then Present (Renamed_Object (Entity (N)))
5323 then
5324 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
5325 end if;
5327 -- Tagged and controlled types and aliased types are always aligned, as
5328 -- are concurrent types.
5330 if Is_Aliased (T)
5331 or else Has_Controlled_Component (T)
5332 or else Is_Concurrent_Type (T)
5333 or else Is_Tagged_Type (T)
5334 or else Is_Controlled (T)
5335 then
5336 return False;
5337 end if;
5339 -- If this is an element of a packed array, may be unaligned
5341 if Is_Ref_To_Bit_Packed_Array (N) then
5342 return True;
5343 end if;
5345 -- Case of indexed component reference: test whether prefix is unaligned
5347 if Nkind (N) = N_Indexed_Component then
5348 return Is_Possibly_Unaligned_Object (Prefix (N));
5350 -- Case of selected component reference
5352 elsif Nkind (N) = N_Selected_Component then
5353 declare
5354 P : constant Node_Id := Prefix (N);
5355 C : constant Entity_Id := Entity (Selector_Name (N));
5356 M : Nat;
5357 S : Nat;
5359 begin
5360 -- If component reference is for an array with non-static bounds,
5361 -- then it is always aligned: we can only process unaligned arrays
5362 -- with static bounds (more precisely compile time known bounds).
5364 if Is_Array_Type (T)
5365 and then not Compile_Time_Known_Bounds (T)
5366 then
5367 return False;
5368 end if;
5370 -- If component is aliased, it is definitely properly aligned
5372 if Is_Aliased (C) then
5373 return False;
5374 end if;
5376 -- If component is for a type implemented as a scalar, and the
5377 -- record is packed, and the component is other than the first
5378 -- component of the record, then the component may be unaligned.
5380 if Is_Packed (Etype (P))
5381 and then Represented_As_Scalar (Etype (C))
5382 and then First_Entity (Scope (C)) /= C
5383 then
5384 return True;
5385 end if;
5387 -- Compute maximum possible alignment for T
5389 -- If alignment is known, then that settles things
5391 if Known_Alignment (T) then
5392 M := UI_To_Int (Alignment (T));
5394 -- If alignment is not known, tentatively set max alignment
5396 else
5397 M := Ttypes.Maximum_Alignment;
5399 -- We can reduce this if the Esize is known since the default
5400 -- alignment will never be more than the smallest power of 2
5401 -- that does not exceed this Esize value.
5403 if Known_Esize (T) then
5404 S := UI_To_Int (Esize (T));
5406 while (M / 2) >= S loop
5407 M := M / 2;
5408 end loop;
5409 end if;
5410 end if;
5412 -- The following code is historical, it used to be present but it
5413 -- is too cautious, because the front-end does not know the proper
5414 -- default alignments for the target. Also, if the alignment is
5415 -- not known, the front end can't know in any case. If a copy is
5416 -- needed, the back-end will take care of it. This whole section
5417 -- including this comment can be removed later ???
5419 -- If the component reference is for a record that has a specified
5420 -- alignment, and we either know it is too small, or cannot tell,
5421 -- then the component may be unaligned.
5423 -- What is the following commented out code ???
5425 -- if Known_Alignment (Etype (P))
5426 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
5427 -- and then M > Alignment (Etype (P))
5428 -- then
5429 -- return True;
5430 -- end if;
5432 -- Case of component clause present which may specify an
5433 -- unaligned position.
5435 if Present (Component_Clause (C)) then
5437 -- Otherwise we can do a test to make sure that the actual
5438 -- start position in the record, and the length, are both
5439 -- consistent with the required alignment. If not, we know
5440 -- that we are unaligned.
5442 declare
5443 Align_In_Bits : constant Nat := M * System_Storage_Unit;
5444 begin
5445 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
5446 or else Esize (C) mod Align_In_Bits /= 0
5447 then
5448 return True;
5449 end if;
5450 end;
5451 end if;
5453 -- Otherwise, for a component reference, test prefix
5455 return Is_Possibly_Unaligned_Object (P);
5456 end;
5458 -- If not a component reference, must be aligned
5460 else
5461 return False;
5462 end if;
5463 end Is_Possibly_Unaligned_Object;
5465 ---------------------------------
5466 -- Is_Possibly_Unaligned_Slice --
5467 ---------------------------------
5469 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
5470 begin
5471 -- Go to renamed object
5473 if Is_Entity_Name (N)
5474 and then Is_Object (Entity (N))
5475 and then Present (Renamed_Object (Entity (N)))
5476 then
5477 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
5478 end if;
5480 -- The reference must be a slice
5482 if Nkind (N) /= N_Slice then
5483 return False;
5484 end if;
5486 -- We only need to worry if the target has strict alignment
5488 if not Target_Strict_Alignment then
5489 return False;
5490 end if;
5492 -- If it is a slice, then look at the array type being sliced
5494 declare
5495 Sarr : constant Node_Id := Prefix (N);
5496 -- Prefix of the slice, i.e. the array being sliced
5498 Styp : constant Entity_Id := Etype (Prefix (N));
5499 -- Type of the array being sliced
5501 Pref : Node_Id;
5502 Ptyp : Entity_Id;
5504 begin
5505 -- The problems arise if the array object that is being sliced
5506 -- is a component of a record or array, and we cannot guarantee
5507 -- the alignment of the array within its containing object.
5509 -- To investigate this, we look at successive prefixes to see
5510 -- if we have a worrisome indexed or selected component.
5512 Pref := Sarr;
5513 loop
5514 -- Case of array is part of an indexed component reference
5516 if Nkind (Pref) = N_Indexed_Component then
5517 Ptyp := Etype (Prefix (Pref));
5519 -- The only problematic case is when the array is packed, in
5520 -- which case we really know nothing about the alignment of
5521 -- individual components.
5523 if Is_Bit_Packed_Array (Ptyp) then
5524 return True;
5525 end if;
5527 -- Case of array is part of a selected component reference
5529 elsif Nkind (Pref) = N_Selected_Component then
5530 Ptyp := Etype (Prefix (Pref));
5532 -- We are definitely in trouble if the record in question
5533 -- has an alignment, and either we know this alignment is
5534 -- inconsistent with the alignment of the slice, or we don't
5535 -- know what the alignment of the slice should be.
5537 if Known_Alignment (Ptyp)
5538 and then (Unknown_Alignment (Styp)
5539 or else Alignment (Styp) > Alignment (Ptyp))
5540 then
5541 return True;
5542 end if;
5544 -- We are in potential trouble if the record type is packed.
5545 -- We could special case when we know that the array is the
5546 -- first component, but that's not such a simple case ???
5548 if Is_Packed (Ptyp) then
5549 return True;
5550 end if;
5552 -- We are in trouble if there is a component clause, and
5553 -- either we do not know the alignment of the slice, or
5554 -- the alignment of the slice is inconsistent with the
5555 -- bit position specified by the component clause.
5557 declare
5558 Field : constant Entity_Id := Entity (Selector_Name (Pref));
5559 begin
5560 if Present (Component_Clause (Field))
5561 and then
5562 (Unknown_Alignment (Styp)
5563 or else
5564 (Component_Bit_Offset (Field) mod
5565 (System_Storage_Unit * Alignment (Styp))) /= 0)
5566 then
5567 return True;
5568 end if;
5569 end;
5571 -- For cases other than selected or indexed components we know we
5572 -- are OK, since no issues arise over alignment.
5574 else
5575 return False;
5576 end if;
5578 -- We processed an indexed component or selected component
5579 -- reference that looked safe, so keep checking prefixes.
5581 Pref := Prefix (Pref);
5582 end loop;
5583 end;
5584 end Is_Possibly_Unaligned_Slice;
5586 -------------------------------
5587 -- Is_Related_To_Func_Return --
5588 -------------------------------
5590 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
5591 Expr : constant Node_Id := Related_Expression (Id);
5592 begin
5593 return
5594 Present (Expr)
5595 and then Nkind (Expr) = N_Explicit_Dereference
5596 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
5597 end Is_Related_To_Func_Return;
5599 --------------------------------
5600 -- Is_Ref_To_Bit_Packed_Array --
5601 --------------------------------
5603 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
5604 Result : Boolean;
5605 Expr : Node_Id;
5607 begin
5608 if Is_Entity_Name (N)
5609 and then Is_Object (Entity (N))
5610 and then Present (Renamed_Object (Entity (N)))
5611 then
5612 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
5613 end if;
5615 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5616 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
5617 Result := True;
5618 else
5619 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
5620 end if;
5622 if Result and then Nkind (N) = N_Indexed_Component then
5623 Expr := First (Expressions (N));
5624 while Present (Expr) loop
5625 Force_Evaluation (Expr);
5626 Next (Expr);
5627 end loop;
5628 end if;
5630 return Result;
5632 else
5633 return False;
5634 end if;
5635 end Is_Ref_To_Bit_Packed_Array;
5637 --------------------------------
5638 -- Is_Ref_To_Bit_Packed_Slice --
5639 --------------------------------
5641 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
5642 begin
5643 if Nkind (N) = N_Type_Conversion then
5644 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
5646 elsif Is_Entity_Name (N)
5647 and then Is_Object (Entity (N))
5648 and then Present (Renamed_Object (Entity (N)))
5649 then
5650 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
5652 elsif Nkind (N) = N_Slice
5653 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
5654 then
5655 return True;
5657 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5658 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
5660 else
5661 return False;
5662 end if;
5663 end Is_Ref_To_Bit_Packed_Slice;
5665 -----------------------
5666 -- Is_Renamed_Object --
5667 -----------------------
5669 function Is_Renamed_Object (N : Node_Id) return Boolean is
5670 Pnod : constant Node_Id := Parent (N);
5671 Kind : constant Node_Kind := Nkind (Pnod);
5672 begin
5673 if Kind = N_Object_Renaming_Declaration then
5674 return True;
5675 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
5676 return Is_Renamed_Object (Pnod);
5677 else
5678 return False;
5679 end if;
5680 end Is_Renamed_Object;
5682 --------------------------------------
5683 -- Is_Secondary_Stack_BIP_Func_Call --
5684 --------------------------------------
5686 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
5687 Alloc_Nam : Name_Id := No_Name;
5688 Actual : Node_Id;
5689 Call : Node_Id := Expr;
5690 Formal : Node_Id;
5691 Param : Node_Id;
5693 begin
5694 -- Build-in-place calls usually appear in 'reference format. Note that
5695 -- the accessibility check machinery may add an extra 'reference due to
5696 -- side effect removal.
5698 while Nkind (Call) = N_Reference loop
5699 Call := Prefix (Call);
5700 end loop;
5702 if Nkind_In (Call, N_Qualified_Expression,
5703 N_Unchecked_Type_Conversion)
5704 then
5705 Call := Expression (Call);
5706 end if;
5708 if Is_Build_In_Place_Function_Call (Call) then
5710 -- Examine all parameter associations of the function call
5712 Param := First (Parameter_Associations (Call));
5713 while Present (Param) loop
5714 if Nkind (Param) = N_Parameter_Association
5715 and then Nkind (Selector_Name (Param)) = N_Identifier
5716 then
5717 Formal := Selector_Name (Param);
5718 Actual := Explicit_Actual_Parameter (Param);
5720 -- Construct the name of formal BIPalloc. It is much easier to
5721 -- extract the name of the function using an arbitrary formal's
5722 -- scope rather than the Name field of Call.
5724 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
5725 Alloc_Nam :=
5726 New_External_Name
5727 (Chars (Scope (Entity (Formal))),
5728 BIP_Formal_Suffix (BIP_Alloc_Form));
5729 end if;
5731 -- A match for BIPalloc => 2 has been found
5733 if Chars (Formal) = Alloc_Nam
5734 and then Nkind (Actual) = N_Integer_Literal
5735 and then Intval (Actual) = Uint_2
5736 then
5737 return True;
5738 end if;
5739 end if;
5741 Next (Param);
5742 end loop;
5743 end if;
5745 return False;
5746 end Is_Secondary_Stack_BIP_Func_Call;
5748 -------------------------------------
5749 -- Is_Tag_To_Class_Wide_Conversion --
5750 -------------------------------------
5752 function Is_Tag_To_Class_Wide_Conversion
5753 (Obj_Id : Entity_Id) return Boolean
5755 Expr : constant Node_Id := Expression (Parent (Obj_Id));
5757 begin
5758 return
5759 Is_Class_Wide_Type (Etype (Obj_Id))
5760 and then Present (Expr)
5761 and then Nkind (Expr) = N_Unchecked_Type_Conversion
5762 and then Etype (Expression (Expr)) = RTE (RE_Tag);
5763 end Is_Tag_To_Class_Wide_Conversion;
5765 ----------------------------
5766 -- Is_Untagged_Derivation --
5767 ----------------------------
5769 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
5770 begin
5771 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
5772 or else
5773 (Is_Private_Type (T) and then Present (Full_View (T))
5774 and then not Is_Tagged_Type (Full_View (T))
5775 and then Is_Derived_Type (Full_View (T))
5776 and then Etype (Full_View (T)) /= T);
5777 end Is_Untagged_Derivation;
5779 ---------------------------
5780 -- Is_Volatile_Reference --
5781 ---------------------------
5783 function Is_Volatile_Reference (N : Node_Id) return Boolean is
5784 begin
5785 -- Only source references are to be treated as volatile, internally
5786 -- generated stuff cannot have volatile external effects.
5788 if not Comes_From_Source (N) then
5789 return False;
5791 -- Never true for reference to a type
5793 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
5794 return False;
5796 -- Never true for a compile time known constant
5798 elsif Compile_Time_Known_Value (N) then
5799 return False;
5801 -- True if object reference with volatile type
5803 elsif Is_Volatile_Object (N) then
5804 return True;
5806 -- True if reference to volatile entity
5808 elsif Is_Entity_Name (N) then
5809 return Treat_As_Volatile (Entity (N));
5811 -- True for slice of volatile array
5813 elsif Nkind (N) = N_Slice then
5814 return Is_Volatile_Reference (Prefix (N));
5816 -- True if volatile component
5818 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5819 if (Is_Entity_Name (Prefix (N))
5820 and then Has_Volatile_Components (Entity (Prefix (N))))
5821 or else (Present (Etype (Prefix (N)))
5822 and then Has_Volatile_Components (Etype (Prefix (N))))
5823 then
5824 return True;
5825 else
5826 return Is_Volatile_Reference (Prefix (N));
5827 end if;
5829 -- Otherwise false
5831 else
5832 return False;
5833 end if;
5834 end Is_Volatile_Reference;
5836 --------------------------
5837 -- Is_VM_By_Copy_Actual --
5838 --------------------------
5840 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
5841 begin
5842 return VM_Target /= No_VM
5843 and then (Nkind (N) = N_Slice
5844 or else
5845 (Nkind (N) = N_Identifier
5846 and then Present (Renamed_Object (Entity (N)))
5847 and then Nkind (Renamed_Object (Entity (N))) =
5848 N_Slice));
5849 end Is_VM_By_Copy_Actual;
5851 --------------------
5852 -- Kill_Dead_Code --
5853 --------------------
5855 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
5856 W : Boolean := Warn;
5857 -- Set False if warnings suppressed
5859 begin
5860 if Present (N) then
5861 Remove_Warning_Messages (N);
5863 -- Generate warning if appropriate
5865 if W then
5867 -- We suppress the warning if this code is under control of an
5868 -- if statement, whose condition is a simple identifier, and
5869 -- either we are in an instance, or warnings off is set for this
5870 -- identifier. The reason for killing it in the instance case is
5871 -- that it is common and reasonable for code to be deleted in
5872 -- instances for various reasons.
5874 -- Could we use Is_Statically_Unevaluated here???
5876 if Nkind (Parent (N)) = N_If_Statement then
5877 declare
5878 C : constant Node_Id := Condition (Parent (N));
5879 begin
5880 if Nkind (C) = N_Identifier
5881 and then
5882 (In_Instance
5883 or else (Present (Entity (C))
5884 and then Has_Warnings_Off (Entity (C))))
5885 then
5886 W := False;
5887 end if;
5888 end;
5889 end if;
5891 -- Generate warning if not suppressed
5893 if W then
5894 Error_Msg_F
5895 ("?t?this code can never be executed and has been deleted!",
5897 end if;
5898 end if;
5900 -- Recurse into block statements and bodies to process declarations
5901 -- and statements.
5903 if Nkind (N) = N_Block_Statement
5904 or else Nkind (N) = N_Subprogram_Body
5905 or else Nkind (N) = N_Package_Body
5906 then
5907 Kill_Dead_Code (Declarations (N), False);
5908 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
5910 if Nkind (N) = N_Subprogram_Body then
5911 Set_Is_Eliminated (Defining_Entity (N));
5912 end if;
5914 elsif Nkind (N) = N_Package_Declaration then
5915 Kill_Dead_Code (Visible_Declarations (Specification (N)));
5916 Kill_Dead_Code (Private_Declarations (Specification (N)));
5918 -- ??? After this point, Delete_Tree has been called on all
5919 -- declarations in Specification (N), so references to entities
5920 -- therein look suspicious.
5922 declare
5923 E : Entity_Id := First_Entity (Defining_Entity (N));
5925 begin
5926 while Present (E) loop
5927 if Ekind (E) = E_Operator then
5928 Set_Is_Eliminated (E);
5929 end if;
5931 Next_Entity (E);
5932 end loop;
5933 end;
5935 -- Recurse into composite statement to kill individual statements in
5936 -- particular instantiations.
5938 elsif Nkind (N) = N_If_Statement then
5939 Kill_Dead_Code (Then_Statements (N));
5940 Kill_Dead_Code (Elsif_Parts (N));
5941 Kill_Dead_Code (Else_Statements (N));
5943 elsif Nkind (N) = N_Loop_Statement then
5944 Kill_Dead_Code (Statements (N));
5946 elsif Nkind (N) = N_Case_Statement then
5947 declare
5948 Alt : Node_Id;
5949 begin
5950 Alt := First (Alternatives (N));
5951 while Present (Alt) loop
5952 Kill_Dead_Code (Statements (Alt));
5953 Next (Alt);
5954 end loop;
5955 end;
5957 elsif Nkind (N) = N_Case_Statement_Alternative then
5958 Kill_Dead_Code (Statements (N));
5960 -- Deal with dead instances caused by deleting instantiations
5962 elsif Nkind (N) in N_Generic_Instantiation then
5963 Remove_Dead_Instance (N);
5964 end if;
5965 end if;
5966 end Kill_Dead_Code;
5968 -- Case where argument is a list of nodes to be killed
5970 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
5971 N : Node_Id;
5972 W : Boolean;
5974 begin
5975 W := Warn;
5977 if Is_Non_Empty_List (L) then
5978 N := First (L);
5979 while Present (N) loop
5980 Kill_Dead_Code (N, W);
5981 W := False;
5982 Next (N);
5983 end loop;
5984 end if;
5985 end Kill_Dead_Code;
5987 ------------------------
5988 -- Known_Non_Negative --
5989 ------------------------
5991 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
5992 begin
5993 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
5994 return True;
5996 else
5997 declare
5998 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
5999 begin
6000 return
6001 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
6002 end;
6003 end if;
6004 end Known_Non_Negative;
6006 --------------------
6007 -- Known_Non_Null --
6008 --------------------
6010 function Known_Non_Null (N : Node_Id) return Boolean is
6011 begin
6012 -- Checks for case where N is an entity reference
6014 if Is_Entity_Name (N) and then Present (Entity (N)) then
6015 declare
6016 E : constant Entity_Id := Entity (N);
6017 Op : Node_Kind;
6018 Val : Node_Id;
6020 begin
6021 -- First check if we are in decisive conditional
6023 Get_Current_Value_Condition (N, Op, Val);
6025 if Known_Null (Val) then
6026 if Op = N_Op_Eq then
6027 return False;
6028 elsif Op = N_Op_Ne then
6029 return True;
6030 end if;
6031 end if;
6033 -- If OK to do replacement, test Is_Known_Non_Null flag
6035 if OK_To_Do_Constant_Replacement (E) then
6036 return Is_Known_Non_Null (E);
6038 -- Otherwise if not safe to do replacement, then say so
6040 else
6041 return False;
6042 end if;
6043 end;
6045 -- True if access attribute
6047 elsif Nkind (N) = N_Attribute_Reference
6048 and then Nam_In (Attribute_Name (N), Name_Access,
6049 Name_Unchecked_Access,
6050 Name_Unrestricted_Access)
6051 then
6052 return True;
6054 -- True if allocator
6056 elsif Nkind (N) = N_Allocator then
6057 return True;
6059 -- For a conversion, true if expression is known non-null
6061 elsif Nkind (N) = N_Type_Conversion then
6062 return Known_Non_Null (Expression (N));
6064 -- Above are all cases where the value could be determined to be
6065 -- non-null. In all other cases, we don't know, so return False.
6067 else
6068 return False;
6069 end if;
6070 end Known_Non_Null;
6072 ----------------
6073 -- Known_Null --
6074 ----------------
6076 function Known_Null (N : Node_Id) return Boolean is
6077 begin
6078 -- Checks for case where N is an entity reference
6080 if Is_Entity_Name (N) and then Present (Entity (N)) then
6081 declare
6082 E : constant Entity_Id := Entity (N);
6083 Op : Node_Kind;
6084 Val : Node_Id;
6086 begin
6087 -- Constant null value is for sure null
6089 if Ekind (E) = E_Constant
6090 and then Known_Null (Constant_Value (E))
6091 then
6092 return True;
6093 end if;
6095 -- First check if we are in decisive conditional
6097 Get_Current_Value_Condition (N, Op, Val);
6099 if Known_Null (Val) then
6100 if Op = N_Op_Eq then
6101 return True;
6102 elsif Op = N_Op_Ne then
6103 return False;
6104 end if;
6105 end if;
6107 -- If OK to do replacement, test Is_Known_Null flag
6109 if OK_To_Do_Constant_Replacement (E) then
6110 return Is_Known_Null (E);
6112 -- Otherwise if not safe to do replacement, then say so
6114 else
6115 return False;
6116 end if;
6117 end;
6119 -- True if explicit reference to null
6121 elsif Nkind (N) = N_Null then
6122 return True;
6124 -- For a conversion, true if expression is known null
6126 elsif Nkind (N) = N_Type_Conversion then
6127 return Known_Null (Expression (N));
6129 -- Above are all cases where the value could be determined to be null.
6130 -- In all other cases, we don't know, so return False.
6132 else
6133 return False;
6134 end if;
6135 end Known_Null;
6137 -----------------------------
6138 -- Make_CW_Equivalent_Type --
6139 -----------------------------
6141 -- Create a record type used as an equivalent of any member of the class
6142 -- which takes its size from exp.
6144 -- Generate the following code:
6146 -- type Equiv_T is record
6147 -- _parent : T (List of discriminant constraints taken from Exp);
6148 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
6149 -- end Equiv_T;
6151 -- ??? Note that this type does not guarantee same alignment as all
6152 -- derived types
6154 function Make_CW_Equivalent_Type
6155 (T : Entity_Id;
6156 E : Node_Id) return Entity_Id
6158 Loc : constant Source_Ptr := Sloc (E);
6159 Root_Typ : constant Entity_Id := Root_Type (T);
6160 List_Def : constant List_Id := Empty_List;
6161 Comp_List : constant List_Id := New_List;
6162 Equiv_Type : Entity_Id;
6163 Range_Type : Entity_Id;
6164 Str_Type : Entity_Id;
6165 Constr_Root : Entity_Id;
6166 Sizexpr : Node_Id;
6168 begin
6169 -- If the root type is already constrained, there are no discriminants
6170 -- in the expression.
6172 if not Has_Discriminants (Root_Typ)
6173 or else Is_Constrained (Root_Typ)
6174 then
6175 Constr_Root := Root_Typ;
6177 -- At this point in the expansion, non-limited view of the type
6178 -- must be available, otherwise the error will be reported later.
6180 if From_Limited_With (Constr_Root)
6181 and then Present (Non_Limited_View (Constr_Root))
6182 then
6183 Constr_Root := Non_Limited_View (Constr_Root);
6184 end if;
6186 else
6187 Constr_Root := Make_Temporary (Loc, 'R');
6189 -- subtype cstr__n is T (List of discr constraints taken from Exp)
6191 Append_To (List_Def,
6192 Make_Subtype_Declaration (Loc,
6193 Defining_Identifier => Constr_Root,
6194 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
6195 end if;
6197 -- Generate the range subtype declaration
6199 Range_Type := Make_Temporary (Loc, 'G');
6201 if not Is_Interface (Root_Typ) then
6203 -- subtype rg__xx is
6204 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
6206 Sizexpr :=
6207 Make_Op_Subtract (Loc,
6208 Left_Opnd =>
6209 Make_Attribute_Reference (Loc,
6210 Prefix =>
6211 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6212 Attribute_Name => Name_Size),
6213 Right_Opnd =>
6214 Make_Attribute_Reference (Loc,
6215 Prefix => New_Occurrence_Of (Constr_Root, Loc),
6216 Attribute_Name => Name_Object_Size));
6217 else
6218 -- subtype rg__xx is
6219 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
6221 Sizexpr :=
6222 Make_Attribute_Reference (Loc,
6223 Prefix =>
6224 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6225 Attribute_Name => Name_Size);
6226 end if;
6228 Set_Paren_Count (Sizexpr, 1);
6230 Append_To (List_Def,
6231 Make_Subtype_Declaration (Loc,
6232 Defining_Identifier => Range_Type,
6233 Subtype_Indication =>
6234 Make_Subtype_Indication (Loc,
6235 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
6236 Constraint => Make_Range_Constraint (Loc,
6237 Range_Expression =>
6238 Make_Range (Loc,
6239 Low_Bound => Make_Integer_Literal (Loc, 1),
6240 High_Bound =>
6241 Make_Op_Divide (Loc,
6242 Left_Opnd => Sizexpr,
6243 Right_Opnd => Make_Integer_Literal (Loc,
6244 Intval => System_Storage_Unit)))))));
6246 -- subtype str__nn is Storage_Array (rg__x);
6248 Str_Type := Make_Temporary (Loc, 'S');
6249 Append_To (List_Def,
6250 Make_Subtype_Declaration (Loc,
6251 Defining_Identifier => Str_Type,
6252 Subtype_Indication =>
6253 Make_Subtype_Indication (Loc,
6254 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
6255 Constraint =>
6256 Make_Index_Or_Discriminant_Constraint (Loc,
6257 Constraints =>
6258 New_List (New_Occurrence_Of (Range_Type, Loc))))));
6260 -- type Equiv_T is record
6261 -- [ _parent : Tnn; ]
6262 -- E : Str_Type;
6263 -- end Equiv_T;
6265 Equiv_Type := Make_Temporary (Loc, 'T');
6266 Set_Ekind (Equiv_Type, E_Record_Type);
6267 Set_Parent_Subtype (Equiv_Type, Constr_Root);
6269 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
6270 -- treatment for this type. In particular, even though _parent's type
6271 -- is a controlled type or contains controlled components, we do not
6272 -- want to set Has_Controlled_Component on it to avoid making it gain
6273 -- an unwanted _controller component.
6275 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
6277 -- A class-wide equivalent type does not require initialization
6279 Set_Suppress_Initialization (Equiv_Type);
6281 if not Is_Interface (Root_Typ) then
6282 Append_To (Comp_List,
6283 Make_Component_Declaration (Loc,
6284 Defining_Identifier =>
6285 Make_Defining_Identifier (Loc, Name_uParent),
6286 Component_Definition =>
6287 Make_Component_Definition (Loc,
6288 Aliased_Present => False,
6289 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
6290 end if;
6292 Append_To (Comp_List,
6293 Make_Component_Declaration (Loc,
6294 Defining_Identifier => Make_Temporary (Loc, 'C'),
6295 Component_Definition =>
6296 Make_Component_Definition (Loc,
6297 Aliased_Present => False,
6298 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
6300 Append_To (List_Def,
6301 Make_Full_Type_Declaration (Loc,
6302 Defining_Identifier => Equiv_Type,
6303 Type_Definition =>
6304 Make_Record_Definition (Loc,
6305 Component_List =>
6306 Make_Component_List (Loc,
6307 Component_Items => Comp_List,
6308 Variant_Part => Empty))));
6310 -- Suppress all checks during the analysis of the expanded code to avoid
6311 -- the generation of spurious warnings under ZFP run-time.
6313 Insert_Actions (E, List_Def, Suppress => All_Checks);
6314 return Equiv_Type;
6315 end Make_CW_Equivalent_Type;
6317 -------------------------
6318 -- Make_Invariant_Call --
6319 -------------------------
6321 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
6322 Loc : constant Source_Ptr := Sloc (Expr);
6323 Typ : Entity_Id;
6325 begin
6326 Typ := Etype (Expr);
6328 -- Subtypes may be subject to invariants coming from their respective
6329 -- base types. The subtype may be fully or partially private.
6331 if Ekind_In (Typ, E_Array_Subtype,
6332 E_Private_Subtype,
6333 E_Record_Subtype,
6334 E_Record_Subtype_With_Private)
6335 then
6336 Typ := Base_Type (Typ);
6337 end if;
6339 pragma Assert
6340 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
6342 return
6343 Make_Procedure_Call_Statement (Loc,
6344 Name =>
6345 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
6346 Parameter_Associations => New_List (Relocate_Node (Expr)));
6347 end Make_Invariant_Call;
6349 ------------------------
6350 -- Make_Literal_Range --
6351 ------------------------
6353 function Make_Literal_Range
6354 (Loc : Source_Ptr;
6355 Literal_Typ : Entity_Id) return Node_Id
6357 Lo : constant Node_Id :=
6358 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
6359 Index : constant Entity_Id := Etype (Lo);
6361 Hi : Node_Id;
6362 Length_Expr : constant Node_Id :=
6363 Make_Op_Subtract (Loc,
6364 Left_Opnd =>
6365 Make_Integer_Literal (Loc,
6366 Intval => String_Literal_Length (Literal_Typ)),
6367 Right_Opnd =>
6368 Make_Integer_Literal (Loc, 1));
6370 begin
6371 Set_Analyzed (Lo, False);
6373 if Is_Integer_Type (Index) then
6374 Hi :=
6375 Make_Op_Add (Loc,
6376 Left_Opnd => New_Copy_Tree (Lo),
6377 Right_Opnd => Length_Expr);
6378 else
6379 Hi :=
6380 Make_Attribute_Reference (Loc,
6381 Attribute_Name => Name_Val,
6382 Prefix => New_Occurrence_Of (Index, Loc),
6383 Expressions => New_List (
6384 Make_Op_Add (Loc,
6385 Left_Opnd =>
6386 Make_Attribute_Reference (Loc,
6387 Attribute_Name => Name_Pos,
6388 Prefix => New_Occurrence_Of (Index, Loc),
6389 Expressions => New_List (New_Copy_Tree (Lo))),
6390 Right_Opnd => Length_Expr)));
6391 end if;
6393 return
6394 Make_Range (Loc,
6395 Low_Bound => Lo,
6396 High_Bound => Hi);
6397 end Make_Literal_Range;
6399 --------------------------
6400 -- Make_Non_Empty_Check --
6401 --------------------------
6403 function Make_Non_Empty_Check
6404 (Loc : Source_Ptr;
6405 N : Node_Id) return Node_Id
6407 begin
6408 return
6409 Make_Op_Ne (Loc,
6410 Left_Opnd =>
6411 Make_Attribute_Reference (Loc,
6412 Attribute_Name => Name_Length,
6413 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
6414 Right_Opnd =>
6415 Make_Integer_Literal (Loc, 0));
6416 end Make_Non_Empty_Check;
6418 -------------------------
6419 -- Make_Predicate_Call --
6420 -------------------------
6422 function Make_Predicate_Call
6423 (Typ : Entity_Id;
6424 Expr : Node_Id;
6425 Mem : Boolean := False) return Node_Id
6427 GM : constant Ghost_Mode_Type := Ghost_Mode;
6429 procedure Restore_Globals;
6430 -- Restore the values of all saved global variables
6432 ---------------------
6433 -- Restore_Globals --
6434 ---------------------
6436 procedure Restore_Globals is
6437 begin
6438 Ghost_Mode := GM;
6439 end Restore_Globals;
6441 -- Local variables
6443 Loc : constant Source_Ptr := Sloc (Expr);
6444 Call : Node_Id;
6445 PFM : Entity_Id;
6447 -- Start of processing for Make_Predicate_Call
6449 begin
6450 pragma Assert (Present (Predicate_Function (Typ)));
6452 -- The related type may be subject to pragma Ghost with policy Ignore.
6453 -- Set the mode now to ensure that the call is properly flagged as
6454 -- ignored Ghost.
6456 Set_Ghost_Mode_From_Entity (Typ);
6458 -- Call special membership version if requested and available
6460 if Mem then
6461 PFM := Predicate_Function_M (Typ);
6463 if Present (PFM) then
6464 Call :=
6465 Make_Function_Call (Loc,
6466 Name => New_Occurrence_Of (PFM, Loc),
6467 Parameter_Associations => New_List (Relocate_Node (Expr)));
6469 Restore_Globals;
6470 return Call;
6471 end if;
6472 end if;
6474 -- Case of calling normal predicate function
6476 Call :=
6477 Make_Function_Call (Loc,
6478 Name =>
6479 New_Occurrence_Of (Predicate_Function (Typ), Loc),
6480 Parameter_Associations => New_List (Relocate_Node (Expr)));
6482 Restore_Globals;
6483 return Call;
6484 end Make_Predicate_Call;
6486 --------------------------
6487 -- Make_Predicate_Check --
6488 --------------------------
6490 function Make_Predicate_Check
6491 (Typ : Entity_Id;
6492 Expr : Node_Id) return Node_Id
6494 Loc : constant Source_Ptr := Sloc (Expr);
6495 Nam : Name_Id;
6497 begin
6498 -- If predicate checks are suppressed, then return a null statement.
6499 -- For this call, we check only the scope setting. If the caller wants
6500 -- to check a specific entity's setting, they must do it manually.
6502 if Predicate_Checks_Suppressed (Empty) then
6503 return Make_Null_Statement (Loc);
6504 end if;
6506 -- Do not generate a check within an internal subprogram (stream
6507 -- functions and the like, including including predicate functions).
6509 if Within_Internal_Subprogram then
6510 return Make_Null_Statement (Loc);
6511 end if;
6513 -- Compute proper name to use, we need to get this right so that the
6514 -- right set of check policies apply to the Check pragma we are making.
6516 if Has_Dynamic_Predicate_Aspect (Typ) then
6517 Nam := Name_Dynamic_Predicate;
6518 elsif Has_Static_Predicate_Aspect (Typ) then
6519 Nam := Name_Static_Predicate;
6520 else
6521 Nam := Name_Predicate;
6522 end if;
6524 return
6525 Make_Pragma (Loc,
6526 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
6527 Pragma_Argument_Associations => New_List (
6528 Make_Pragma_Argument_Association (Loc,
6529 Expression => Make_Identifier (Loc, Nam)),
6530 Make_Pragma_Argument_Association (Loc,
6531 Expression => Make_Predicate_Call (Typ, Expr))));
6532 end Make_Predicate_Check;
6534 ----------------------------
6535 -- Make_Subtype_From_Expr --
6536 ----------------------------
6538 -- 1. If Expr is an unconstrained array expression, creates
6539 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
6541 -- 2. If Expr is a unconstrained discriminated type expression, creates
6542 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
6544 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
6546 function Make_Subtype_From_Expr
6547 (E : Node_Id;
6548 Unc_Typ : Entity_Id) return Node_Id
6550 List_Constr : constant List_Id := New_List;
6551 Loc : constant Source_Ptr := Sloc (E);
6552 D : Entity_Id;
6553 Full_Exp : Node_Id;
6554 Full_Subtyp : Entity_Id;
6555 High_Bound : Entity_Id;
6556 Index_Typ : Entity_Id;
6557 Low_Bound : Entity_Id;
6558 Priv_Subtyp : Entity_Id;
6559 Utyp : Entity_Id;
6561 begin
6562 if Is_Private_Type (Unc_Typ)
6563 and then Has_Unknown_Discriminants (Unc_Typ)
6564 then
6565 -- Prepare the subtype completion. Use the base type to find the
6566 -- underlying type because the type may be a generic actual or an
6567 -- explicit subtype.
6569 Utyp := Underlying_Type (Base_Type (Unc_Typ));
6570 Full_Subtyp := Make_Temporary (Loc, 'C');
6571 Full_Exp :=
6572 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
6573 Set_Parent (Full_Exp, Parent (E));
6575 Priv_Subtyp := Make_Temporary (Loc, 'P');
6577 Insert_Action (E,
6578 Make_Subtype_Declaration (Loc,
6579 Defining_Identifier => Full_Subtyp,
6580 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
6582 -- Define the dummy private subtype
6584 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
6585 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
6586 Set_Scope (Priv_Subtyp, Full_Subtyp);
6587 Set_Is_Constrained (Priv_Subtyp);
6588 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
6589 Set_Is_Itype (Priv_Subtyp);
6590 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
6592 if Is_Tagged_Type (Priv_Subtyp) then
6593 Set_Class_Wide_Type
6594 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
6595 Set_Direct_Primitive_Operations (Priv_Subtyp,
6596 Direct_Primitive_Operations (Unc_Typ));
6597 end if;
6599 Set_Full_View (Priv_Subtyp, Full_Subtyp);
6601 return New_Occurrence_Of (Priv_Subtyp, Loc);
6603 elsif Is_Array_Type (Unc_Typ) then
6604 Index_Typ := First_Index (Unc_Typ);
6605 for J in 1 .. Number_Dimensions (Unc_Typ) loop
6607 -- Capture the bounds of each index constraint in case the context
6608 -- is an object declaration of an unconstrained type initialized
6609 -- by a function call:
6611 -- Obj : Unconstr_Typ := Func_Call;
6613 -- This scenario requires secondary scope management and the index
6614 -- constraint cannot depend on the temporary used to capture the
6615 -- result of the function call.
6617 -- SS_Mark;
6618 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
6619 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
6620 -- Obj : S := Temp.all;
6621 -- SS_Release; -- Temp is gone at this point, bounds of S are
6622 -- -- non existent.
6624 -- Generate:
6625 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
6627 Low_Bound := Make_Temporary (Loc, 'B');
6628 Insert_Action (E,
6629 Make_Object_Declaration (Loc,
6630 Defining_Identifier => Low_Bound,
6631 Object_Definition =>
6632 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
6633 Constant_Present => True,
6634 Expression =>
6635 Make_Attribute_Reference (Loc,
6636 Prefix => Duplicate_Subexpr_No_Checks (E),
6637 Attribute_Name => Name_First,
6638 Expressions => New_List (
6639 Make_Integer_Literal (Loc, J)))));
6641 -- Generate:
6642 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
6644 High_Bound := Make_Temporary (Loc, 'B');
6645 Insert_Action (E,
6646 Make_Object_Declaration (Loc,
6647 Defining_Identifier => High_Bound,
6648 Object_Definition =>
6649 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
6650 Constant_Present => True,
6651 Expression =>
6652 Make_Attribute_Reference (Loc,
6653 Prefix => Duplicate_Subexpr_No_Checks (E),
6654 Attribute_Name => Name_Last,
6655 Expressions => New_List (
6656 Make_Integer_Literal (Loc, J)))));
6658 Append_To (List_Constr,
6659 Make_Range (Loc,
6660 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
6661 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
6663 Index_Typ := Next_Index (Index_Typ);
6664 end loop;
6666 elsif Is_Class_Wide_Type (Unc_Typ) then
6667 declare
6668 CW_Subtype : Entity_Id;
6669 EQ_Typ : Entity_Id := Empty;
6671 begin
6672 -- A class-wide equivalent type is not needed when VM_Target
6673 -- because the VM back-ends handle the class-wide object
6674 -- initialization itself (and doesn't need or want the
6675 -- additional intermediate type to handle the assignment).
6677 if Expander_Active and then Tagged_Type_Expansion then
6679 -- If this is the class-wide type of a completion that is a
6680 -- record subtype, set the type of the class-wide type to be
6681 -- the full base type, for use in the expanded code for the
6682 -- equivalent type. Should this be done earlier when the
6683 -- completion is analyzed ???
6685 if Is_Private_Type (Etype (Unc_Typ))
6686 and then
6687 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
6688 then
6689 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
6690 end if;
6692 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
6693 end if;
6695 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
6696 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
6697 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
6699 return New_Occurrence_Of (CW_Subtype, Loc);
6700 end;
6702 -- Indefinite record type with discriminants
6704 else
6705 D := First_Discriminant (Unc_Typ);
6706 while Present (D) loop
6707 Append_To (List_Constr,
6708 Make_Selected_Component (Loc,
6709 Prefix => Duplicate_Subexpr_No_Checks (E),
6710 Selector_Name => New_Occurrence_Of (D, Loc)));
6712 Next_Discriminant (D);
6713 end loop;
6714 end if;
6716 return
6717 Make_Subtype_Indication (Loc,
6718 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
6719 Constraint =>
6720 Make_Index_Or_Discriminant_Constraint (Loc,
6721 Constraints => List_Constr));
6722 end Make_Subtype_From_Expr;
6724 ----------------------------
6725 -- Matching_Standard_Type --
6726 ----------------------------
6728 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
6729 pragma Assert (Is_Scalar_Type (Typ));
6730 Siz : constant Uint := Esize (Typ);
6732 begin
6733 -- Floating-point cases
6735 if Is_Floating_Point_Type (Typ) then
6736 if Siz <= Esize (Standard_Short_Float) then
6737 return Standard_Short_Float;
6738 elsif Siz <= Esize (Standard_Float) then
6739 return Standard_Float;
6740 elsif Siz <= Esize (Standard_Long_Float) then
6741 return Standard_Long_Float;
6742 elsif Siz <= Esize (Standard_Long_Long_Float) then
6743 return Standard_Long_Long_Float;
6744 else
6745 raise Program_Error;
6746 end if;
6748 -- Integer cases (includes fixed-point types)
6750 -- Unsigned integer cases (includes normal enumeration types)
6752 elsif Is_Unsigned_Type (Typ) then
6753 if Siz <= Esize (Standard_Short_Short_Unsigned) then
6754 return Standard_Short_Short_Unsigned;
6755 elsif Siz <= Esize (Standard_Short_Unsigned) then
6756 return Standard_Short_Unsigned;
6757 elsif Siz <= Esize (Standard_Unsigned) then
6758 return Standard_Unsigned;
6759 elsif Siz <= Esize (Standard_Long_Unsigned) then
6760 return Standard_Long_Unsigned;
6761 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
6762 return Standard_Long_Long_Unsigned;
6763 else
6764 raise Program_Error;
6765 end if;
6767 -- Signed integer cases
6769 else
6770 if Siz <= Esize (Standard_Short_Short_Integer) then
6771 return Standard_Short_Short_Integer;
6772 elsif Siz <= Esize (Standard_Short_Integer) then
6773 return Standard_Short_Integer;
6774 elsif Siz <= Esize (Standard_Integer) then
6775 return Standard_Integer;
6776 elsif Siz <= Esize (Standard_Long_Integer) then
6777 return Standard_Long_Integer;
6778 elsif Siz <= Esize (Standard_Long_Long_Integer) then
6779 return Standard_Long_Long_Integer;
6780 else
6781 raise Program_Error;
6782 end if;
6783 end if;
6784 end Matching_Standard_Type;
6786 -----------------------------
6787 -- May_Generate_Large_Temp --
6788 -----------------------------
6790 -- At the current time, the only types that we return False for (i.e. where
6791 -- we decide we know they cannot generate large temps) are ones where we
6792 -- know the size is 256 bits or less at compile time, and we are still not
6793 -- doing a thorough job on arrays and records ???
6795 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
6796 begin
6797 if not Size_Known_At_Compile_Time (Typ) then
6798 return False;
6800 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
6801 return False;
6803 elsif Is_Array_Type (Typ)
6804 and then Present (Packed_Array_Impl_Type (Typ))
6805 then
6806 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
6808 -- We could do more here to find other small types ???
6810 else
6811 return True;
6812 end if;
6813 end May_Generate_Large_Temp;
6815 ------------------------
6816 -- Needs_Finalization --
6817 ------------------------
6819 function Needs_Finalization (T : Entity_Id) return Boolean is
6820 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
6821 -- If type is not frozen yet, check explicitly among its components,
6822 -- because the Has_Controlled_Component flag is not necessarily set.
6824 -----------------------------------
6825 -- Has_Some_Controlled_Component --
6826 -----------------------------------
6828 function Has_Some_Controlled_Component
6829 (Rec : Entity_Id) return Boolean
6831 Comp : Entity_Id;
6833 begin
6834 if Has_Controlled_Component (Rec) then
6835 return True;
6837 elsif not Is_Frozen (Rec) then
6838 if Is_Record_Type (Rec) then
6839 Comp := First_Entity (Rec);
6841 while Present (Comp) loop
6842 if not Is_Type (Comp)
6843 and then Needs_Finalization (Etype (Comp))
6844 then
6845 return True;
6846 end if;
6848 Next_Entity (Comp);
6849 end loop;
6851 return False;
6853 elsif Is_Array_Type (Rec) then
6854 return Needs_Finalization (Component_Type (Rec));
6856 else
6857 return Has_Controlled_Component (Rec);
6858 end if;
6859 else
6860 return False;
6861 end if;
6862 end Has_Some_Controlled_Component;
6864 -- Start of processing for Needs_Finalization
6866 begin
6867 -- Certain run-time configurations and targets do not provide support
6868 -- for controlled types.
6870 if Restriction_Active (No_Finalization) then
6871 return False;
6873 -- C++, CIL and Java types are not considered controlled. It is assumed
6874 -- that the non-Ada side will handle their clean up.
6876 elsif Convention (T) = Convention_CIL
6877 or else Convention (T) = Convention_CPP
6878 or else Convention (T) = Convention_Java
6879 then
6880 return False;
6882 -- Never needs finalization if Disable_Controlled set
6884 elsif Disable_Controlled (T) then
6885 return False;
6887 else
6888 -- Class-wide types are treated as controlled because derivations
6889 -- from the root type can introduce controlled components.
6891 return Is_Class_Wide_Type (T)
6892 or else Is_Controlled (T)
6893 or else Has_Controlled_Component (T)
6894 or else Has_Some_Controlled_Component (T)
6895 or else
6896 (Is_Concurrent_Type (T)
6897 and then Present (Corresponding_Record_Type (T))
6898 and then Needs_Finalization (Corresponding_Record_Type (T)));
6899 end if;
6900 end Needs_Finalization;
6902 ----------------------------
6903 -- Needs_Constant_Address --
6904 ----------------------------
6906 function Needs_Constant_Address
6907 (Decl : Node_Id;
6908 Typ : Entity_Id) return Boolean
6910 begin
6912 -- If we have no initialization of any kind, then we don't need to place
6913 -- any restrictions on the address clause, because the object will be
6914 -- elaborated after the address clause is evaluated. This happens if the
6915 -- declaration has no initial expression, or the type has no implicit
6916 -- initialization, or the object is imported.
6918 -- The same holds for all initialized scalar types and all access types.
6919 -- Packed bit arrays of size up to 64 are represented using a modular
6920 -- type with an initialization (to zero) and can be processed like other
6921 -- initialized scalar types.
6923 -- If the type is controlled, code to attach the object to a
6924 -- finalization chain is generated at the point of declaration, and
6925 -- therefore the elaboration of the object cannot be delayed: the
6926 -- address expression must be a constant.
6928 if No (Expression (Decl))
6929 and then not Needs_Finalization (Typ)
6930 and then
6931 (not Has_Non_Null_Base_Init_Proc (Typ)
6932 or else Is_Imported (Defining_Identifier (Decl)))
6933 then
6934 return False;
6936 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
6937 or else Is_Access_Type (Typ)
6938 or else
6939 (Is_Bit_Packed_Array (Typ)
6940 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
6941 then
6942 return False;
6944 else
6946 -- Otherwise, we require the address clause to be constant because
6947 -- the call to the initialization procedure (or the attach code) has
6948 -- to happen at the point of the declaration.
6950 -- Actually the IP call has been moved to the freeze actions anyway,
6951 -- so maybe we can relax this restriction???
6953 return True;
6954 end if;
6955 end Needs_Constant_Address;
6957 ----------------------------
6958 -- New_Class_Wide_Subtype --
6959 ----------------------------
6961 function New_Class_Wide_Subtype
6962 (CW_Typ : Entity_Id;
6963 N : Node_Id) return Entity_Id
6965 Res : constant Entity_Id := Create_Itype (E_Void, N);
6966 Res_Name : constant Name_Id := Chars (Res);
6967 Res_Scope : constant Entity_Id := Scope (Res);
6969 begin
6970 Copy_Node (CW_Typ, Res);
6971 Set_Comes_From_Source (Res, False);
6972 Set_Sloc (Res, Sloc (N));
6973 Set_Is_Itype (Res);
6974 Set_Associated_Node_For_Itype (Res, N);
6975 Set_Is_Public (Res, False); -- By default, may be changed below.
6976 Set_Public_Status (Res);
6977 Set_Chars (Res, Res_Name);
6978 Set_Scope (Res, Res_Scope);
6979 Set_Ekind (Res, E_Class_Wide_Subtype);
6980 Set_Next_Entity (Res, Empty);
6981 Set_Etype (Res, Base_Type (CW_Typ));
6982 Set_Is_Frozen (Res, False);
6983 Set_Freeze_Node (Res, Empty);
6984 return (Res);
6985 end New_Class_Wide_Subtype;
6987 --------------------------------
6988 -- Non_Limited_Designated_Type --
6989 ---------------------------------
6991 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
6992 Desig : constant Entity_Id := Designated_Type (T);
6993 begin
6994 if Has_Non_Limited_View (Desig) then
6995 return Non_Limited_View (Desig);
6996 else
6997 return Desig;
6998 end if;
6999 end Non_Limited_Designated_Type;
7001 -----------------------------------
7002 -- OK_To_Do_Constant_Replacement --
7003 -----------------------------------
7005 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
7006 ES : constant Entity_Id := Scope (E);
7007 CS : Entity_Id;
7009 begin
7010 -- Do not replace statically allocated objects, because they may be
7011 -- modified outside the current scope.
7013 if Is_Statically_Allocated (E) then
7014 return False;
7016 -- Do not replace aliased or volatile objects, since we don't know what
7017 -- else might change the value.
7019 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
7020 return False;
7022 -- Debug flag -gnatdM disconnects this optimization
7024 elsif Debug_Flag_MM then
7025 return False;
7027 -- Otherwise check scopes
7029 else
7030 CS := Current_Scope;
7032 loop
7033 -- If we are in right scope, replacement is safe
7035 if CS = ES then
7036 return True;
7038 -- Packages do not affect the determination of safety
7040 elsif Ekind (CS) = E_Package then
7041 exit when CS = Standard_Standard;
7042 CS := Scope (CS);
7044 -- Blocks do not affect the determination of safety
7046 elsif Ekind (CS) = E_Block then
7047 CS := Scope (CS);
7049 -- Loops do not affect the determination of safety. Note that we
7050 -- kill all current values on entry to a loop, so we are just
7051 -- talking about processing within a loop here.
7053 elsif Ekind (CS) = E_Loop then
7054 CS := Scope (CS);
7056 -- Otherwise, the reference is dubious, and we cannot be sure that
7057 -- it is safe to do the replacement.
7059 else
7060 exit;
7061 end if;
7062 end loop;
7064 return False;
7065 end if;
7066 end OK_To_Do_Constant_Replacement;
7068 ------------------------------------
7069 -- Possible_Bit_Aligned_Component --
7070 ------------------------------------
7072 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
7073 begin
7074 -- Do not process an unanalyzed node because it is not yet decorated and
7075 -- most checks performed below will fail.
7077 if not Analyzed (N) then
7078 return False;
7079 end if;
7081 case Nkind (N) is
7083 -- Case of indexed component
7085 when N_Indexed_Component =>
7086 declare
7087 P : constant Node_Id := Prefix (N);
7088 Ptyp : constant Entity_Id := Etype (P);
7090 begin
7091 -- If we know the component size and it is less than 64, then
7092 -- we are definitely OK. The back end always does assignment of
7093 -- misaligned small objects correctly.
7095 if Known_Static_Component_Size (Ptyp)
7096 and then Component_Size (Ptyp) <= 64
7097 then
7098 return False;
7100 -- Otherwise, we need to test the prefix, to see if we are
7101 -- indexing from a possibly unaligned component.
7103 else
7104 return Possible_Bit_Aligned_Component (P);
7105 end if;
7106 end;
7108 -- Case of selected component
7110 when N_Selected_Component =>
7111 declare
7112 P : constant Node_Id := Prefix (N);
7113 Comp : constant Entity_Id := Entity (Selector_Name (N));
7115 begin
7116 -- If there is no component clause, then we are in the clear
7117 -- since the back end will never misalign a large component
7118 -- unless it is forced to do so. In the clear means we need
7119 -- only the recursive test on the prefix.
7121 if Component_May_Be_Bit_Aligned (Comp) then
7122 return True;
7123 else
7124 return Possible_Bit_Aligned_Component (P);
7125 end if;
7126 end;
7128 -- For a slice, test the prefix, if that is possibly misaligned,
7129 -- then for sure the slice is.
7131 when N_Slice =>
7132 return Possible_Bit_Aligned_Component (Prefix (N));
7134 -- For an unchecked conversion, check whether the expression may
7135 -- be bit-aligned.
7137 when N_Unchecked_Type_Conversion =>
7138 return Possible_Bit_Aligned_Component (Expression (N));
7140 -- If we have none of the above, it means that we have fallen off the
7141 -- top testing prefixes recursively, and we now have a stand alone
7142 -- object, where we don't have a problem, unless this is a renaming,
7143 -- in which case we need to look into the renamed object.
7145 when others =>
7146 if Is_Entity_Name (N)
7147 and then Present (Renamed_Object (Entity (N)))
7148 then
7149 return
7150 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
7151 else
7152 return False;
7153 end if;
7155 end case;
7156 end Possible_Bit_Aligned_Component;
7158 -----------------------------------------------
7159 -- Process_Statements_For_Controlled_Objects --
7160 -----------------------------------------------
7162 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
7163 Loc : constant Source_Ptr := Sloc (N);
7165 function Are_Wrapped (L : List_Id) return Boolean;
7166 -- Determine whether list L contains only one statement which is a block
7168 function Wrap_Statements_In_Block
7169 (L : List_Id;
7170 Scop : Entity_Id := Current_Scope) return Node_Id;
7171 -- Given a list of statements L, wrap it in a block statement and return
7172 -- the generated node. Scop is either the current scope or the scope of
7173 -- the context (if applicable).
7175 -----------------
7176 -- Are_Wrapped --
7177 -----------------
7179 function Are_Wrapped (L : List_Id) return Boolean is
7180 Stmt : constant Node_Id := First (L);
7181 begin
7182 return
7183 Present (Stmt)
7184 and then No (Next (Stmt))
7185 and then Nkind (Stmt) = N_Block_Statement;
7186 end Are_Wrapped;
7188 ------------------------------
7189 -- Wrap_Statements_In_Block --
7190 ------------------------------
7192 function Wrap_Statements_In_Block
7193 (L : List_Id;
7194 Scop : Entity_Id := Current_Scope) return Node_Id
7196 Block_Id : Entity_Id;
7197 Block_Nod : Node_Id;
7198 Iter_Loop : Entity_Id;
7200 begin
7201 Block_Nod :=
7202 Make_Block_Statement (Loc,
7203 Declarations => No_List,
7204 Handled_Statement_Sequence =>
7205 Make_Handled_Sequence_Of_Statements (Loc,
7206 Statements => L));
7208 -- Create a label for the block in case the block needs to manage the
7209 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
7211 Add_Block_Identifier (Block_Nod, Block_Id);
7213 -- When wrapping the statements of an iterator loop, check whether
7214 -- the loop requires secondary stack management and if so, propagate
7215 -- the appropriate flags to the block. This ensures that the cursor
7216 -- is properly cleaned up at each iteration of the loop.
7218 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
7220 if Present (Iter_Loop) then
7221 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
7223 -- Secondary stack reclamation is suppressed when the associated
7224 -- iterator loop contains a return statement which uses the stack.
7226 Set_Sec_Stack_Needed_For_Return
7227 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
7228 end if;
7230 return Block_Nod;
7231 end Wrap_Statements_In_Block;
7233 -- Local variables
7235 Block : Node_Id;
7237 -- Start of processing for Process_Statements_For_Controlled_Objects
7239 begin
7240 -- Whenever a non-handled statement list is wrapped in a block, the
7241 -- block must be explicitly analyzed to redecorate all entities in the
7242 -- list and ensure that a finalizer is properly built.
7244 case Nkind (N) is
7245 when N_Elsif_Part |
7246 N_If_Statement |
7247 N_Conditional_Entry_Call |
7248 N_Selective_Accept =>
7250 -- Check the "then statements" for elsif parts and if statements
7252 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
7253 and then not Is_Empty_List (Then_Statements (N))
7254 and then not Are_Wrapped (Then_Statements (N))
7255 and then Requires_Cleanup_Actions
7256 (Then_Statements (N), False, False)
7257 then
7258 Block := Wrap_Statements_In_Block (Then_Statements (N));
7259 Set_Then_Statements (N, New_List (Block));
7261 Analyze (Block);
7262 end if;
7264 -- Check the "else statements" for conditional entry calls, if
7265 -- statements and selective accepts.
7267 if Nkind_In (N, N_Conditional_Entry_Call,
7268 N_If_Statement,
7269 N_Selective_Accept)
7270 and then not Is_Empty_List (Else_Statements (N))
7271 and then not Are_Wrapped (Else_Statements (N))
7272 and then Requires_Cleanup_Actions
7273 (Else_Statements (N), False, False)
7274 then
7275 Block := Wrap_Statements_In_Block (Else_Statements (N));
7276 Set_Else_Statements (N, New_List (Block));
7278 Analyze (Block);
7279 end if;
7281 when N_Abortable_Part |
7282 N_Accept_Alternative |
7283 N_Case_Statement_Alternative |
7284 N_Delay_Alternative |
7285 N_Entry_Call_Alternative |
7286 N_Exception_Handler |
7287 N_Loop_Statement |
7288 N_Triggering_Alternative =>
7290 if not Is_Empty_List (Statements (N))
7291 and then not Are_Wrapped (Statements (N))
7292 and then Requires_Cleanup_Actions (Statements (N), False, False)
7293 then
7294 if Nkind (N) = N_Loop_Statement
7295 and then Present (Identifier (N))
7296 then
7297 Block :=
7298 Wrap_Statements_In_Block
7299 (L => Statements (N),
7300 Scop => Entity (Identifier (N)));
7301 else
7302 Block := Wrap_Statements_In_Block (Statements (N));
7303 end if;
7305 Set_Statements (N, New_List (Block));
7306 Analyze (Block);
7307 end if;
7309 when others =>
7310 null;
7311 end case;
7312 end Process_Statements_For_Controlled_Objects;
7314 ------------------
7315 -- Power_Of_Two --
7316 ------------------
7318 function Power_Of_Two (N : Node_Id) return Nat is
7319 Typ : constant Entity_Id := Etype (N);
7320 pragma Assert (Is_Integer_Type (Typ));
7322 Siz : constant Nat := UI_To_Int (Esize (Typ));
7323 Val : Uint;
7325 begin
7326 if not Compile_Time_Known_Value (N) then
7327 return 0;
7329 else
7330 Val := Expr_Value (N);
7331 for J in 1 .. Siz - 1 loop
7332 if Val = Uint_2 ** J then
7333 return J;
7334 end if;
7335 end loop;
7337 return 0;
7338 end if;
7339 end Power_Of_Two;
7341 ----------------------
7342 -- Remove_Init_Call --
7343 ----------------------
7345 function Remove_Init_Call
7346 (Var : Entity_Id;
7347 Rep_Clause : Node_Id) return Node_Id
7349 Par : constant Node_Id := Parent (Var);
7350 Typ : constant Entity_Id := Etype (Var);
7352 Init_Proc : Entity_Id;
7353 -- Initialization procedure for Typ
7355 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
7356 -- Look for init call for Var starting at From and scanning the
7357 -- enclosing list until Rep_Clause or the end of the list is reached.
7359 ----------------------------
7360 -- Find_Init_Call_In_List --
7361 ----------------------------
7363 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
7364 Init_Call : Node_Id;
7366 begin
7367 Init_Call := From;
7368 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
7369 if Nkind (Init_Call) = N_Procedure_Call_Statement
7370 and then Is_Entity_Name (Name (Init_Call))
7371 and then Entity (Name (Init_Call)) = Init_Proc
7372 then
7373 return Init_Call;
7374 end if;
7376 Next (Init_Call);
7377 end loop;
7379 return Empty;
7380 end Find_Init_Call_In_List;
7382 Init_Call : Node_Id;
7384 -- Start of processing for Find_Init_Call
7386 begin
7387 if Present (Initialization_Statements (Var)) then
7388 Init_Call := Initialization_Statements (Var);
7389 Set_Initialization_Statements (Var, Empty);
7391 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
7393 -- No init proc for the type, so obviously no call to be found
7395 return Empty;
7397 else
7398 -- We might be able to handle other cases below by just properly
7399 -- setting Initialization_Statements at the point where the init proc
7400 -- call is generated???
7402 Init_Proc := Base_Init_Proc (Typ);
7404 -- First scan the list containing the declaration of Var
7406 Init_Call := Find_Init_Call_In_List (From => Next (Par));
7408 -- If not found, also look on Var's freeze actions list, if any,
7409 -- since the init call may have been moved there (case of an address
7410 -- clause applying to Var).
7412 if No (Init_Call) and then Present (Freeze_Node (Var)) then
7413 Init_Call :=
7414 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
7415 end if;
7417 -- If the initialization call has actuals that use the secondary
7418 -- stack, the call may have been wrapped into a temporary block, in
7419 -- which case the block itself has to be removed.
7421 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
7422 declare
7423 Blk : constant Node_Id := Next (Par);
7424 begin
7425 if Present
7426 (Find_Init_Call_In_List
7427 (First (Statements (Handled_Statement_Sequence (Blk)))))
7428 then
7429 Init_Call := Blk;
7430 end if;
7431 end;
7432 end if;
7433 end if;
7435 if Present (Init_Call) then
7436 Remove (Init_Call);
7437 end if;
7438 return Init_Call;
7439 end Remove_Init_Call;
7441 -------------------------
7442 -- Remove_Side_Effects --
7443 -------------------------
7445 procedure Remove_Side_Effects
7446 (Exp : Node_Id;
7447 Name_Req : Boolean := False;
7448 Renaming_Req : Boolean := False;
7449 Variable_Ref : Boolean := False;
7450 Related_Id : Entity_Id := Empty;
7451 Is_Low_Bound : Boolean := False;
7452 Is_High_Bound : Boolean := False)
7454 function Build_Temporary
7455 (Loc : Source_Ptr;
7456 Id : Character;
7457 Related_Nod : Node_Id := Empty) return Entity_Id;
7458 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
7459 -- is present (xxx is taken from the Chars field of Related_Nod),
7460 -- otherwise it generates an internal temporary.
7462 function Is_Name_Reference (N : Node_Id) return Boolean;
7463 -- Determine if the tree referenced by N represents a name. This is
7464 -- similar to Is_Object_Reference but returns true only if N can be
7465 -- renamed without the need for a temporary, the typical example of
7466 -- an object not in this category being a function call.
7468 ---------------------
7469 -- Build_Temporary --
7470 ---------------------
7472 function Build_Temporary
7473 (Loc : Source_Ptr;
7474 Id : Character;
7475 Related_Nod : Node_Id := Empty) return Entity_Id
7477 Temp_Nam : Name_Id;
7479 begin
7480 -- The context requires an external symbol
7482 if Present (Related_Id) then
7483 if Is_Low_Bound then
7484 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
7485 else pragma Assert (Is_High_Bound);
7486 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
7487 end if;
7489 return Make_Defining_Identifier (Loc, Temp_Nam);
7491 -- Otherwise generate an internal temporary
7493 else
7494 return Make_Temporary (Loc, Id, Related_Nod);
7495 end if;
7496 end Build_Temporary;
7498 -----------------------
7499 -- Is_Name_Reference --
7500 -----------------------
7502 function Is_Name_Reference (N : Node_Id) return Boolean is
7503 begin
7504 if Is_Entity_Name (N) then
7505 return Present (Entity (N)) and then Is_Object (Entity (N));
7506 end if;
7508 case Nkind (N) is
7509 when N_Indexed_Component | N_Slice =>
7510 return
7511 Is_Name_Reference (Prefix (N))
7512 or else Is_Access_Type (Etype (Prefix (N)));
7514 -- Attributes 'Input, 'Old and 'Result produce objects
7516 when N_Attribute_Reference =>
7517 return
7518 Nam_In
7519 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
7521 when N_Selected_Component =>
7522 return
7523 Is_Name_Reference (Selector_Name (N))
7524 and then
7525 (Is_Name_Reference (Prefix (N))
7526 or else Is_Access_Type (Etype (Prefix (N))));
7528 when N_Explicit_Dereference =>
7529 return True;
7531 -- A view conversion of a tagged name is a name reference
7533 when N_Type_Conversion =>
7534 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
7535 and then Is_Tagged_Type (Etype (Expression (N)))
7536 and then Is_Name_Reference (Expression (N));
7538 -- An unchecked type conversion is considered to be a name if
7539 -- the operand is a name (this construction arises only as a
7540 -- result of expansion activities).
7542 when N_Unchecked_Type_Conversion =>
7543 return Is_Name_Reference (Expression (N));
7545 when others =>
7546 return False;
7547 end case;
7548 end Is_Name_Reference;
7550 -- Local variables
7552 Loc : constant Source_Ptr := Sloc (Exp);
7553 Exp_Type : constant Entity_Id := Etype (Exp);
7554 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
7555 Def_Id : Entity_Id;
7556 E : Node_Id;
7557 New_Exp : Node_Id;
7558 Ptr_Typ_Decl : Node_Id;
7559 Ref_Type : Entity_Id;
7560 Res : Node_Id;
7562 -- Start of processing for Remove_Side_Effects
7564 begin
7565 -- Handle cases in which there is nothing to do. In GNATprove mode,
7566 -- removal of side effects is useful for the light expansion of
7567 -- renamings. This removal should only occur when not inside a
7568 -- generic and not doing a pre-analysis.
7570 if not Expander_Active
7571 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
7572 then
7573 return;
7574 end if;
7576 -- Cannot generate temporaries if the invocation to remove side effects
7577 -- was issued too early and the type of the expression is not resolved
7578 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
7579 -- Remove_Side_Effects).
7581 if No (Exp_Type) or else Ekind (Exp_Type) = E_Access_Attribute_Type then
7582 return;
7584 -- No action needed for side-effect free expressions
7586 elsif Side_Effect_Free (Exp, Name_Req, Variable_Ref) then
7587 return;
7588 end if;
7590 -- The remaining processing is done with all checks suppressed
7592 -- Note: from now on, don't use return statements, instead do a goto
7593 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
7595 Scope_Suppress.Suppress := (others => True);
7597 -- If it is an elementary type and we need to capture the value, just
7598 -- make a constant. Likewise if this is not a name reference, except
7599 -- for a type conversion because we would enter an infinite recursion
7600 -- with Checks.Apply_Predicate_Check if the target type has predicates.
7601 -- And type conversions need a specific treatment anyway, see below.
7602 -- Also do it if we have a volatile reference and Name_Req is not set
7603 -- (see comments for Side_Effect_Free).
7605 if Is_Elementary_Type (Exp_Type)
7606 and then (Variable_Ref
7607 or else (not Is_Name_Reference (Exp)
7608 and then Nkind (Exp) /= N_Type_Conversion)
7609 or else (not Name_Req
7610 and then Is_Volatile_Reference (Exp)))
7611 then
7612 Def_Id := Build_Temporary (Loc, 'R', Exp);
7613 Set_Etype (Def_Id, Exp_Type);
7614 Res := New_Occurrence_Of (Def_Id, Loc);
7616 -- If the expression is a packed reference, it must be reanalyzed and
7617 -- expanded, depending on context. This is the case for actuals where
7618 -- a constraint check may capture the actual before expansion of the
7619 -- call is complete.
7621 if Nkind (Exp) = N_Indexed_Component
7622 and then Is_Packed (Etype (Prefix (Exp)))
7623 then
7624 Set_Analyzed (Exp, False);
7625 Set_Analyzed (Prefix (Exp), False);
7626 end if;
7628 -- Generate:
7629 -- Rnn : Exp_Type renames Expr;
7631 if Renaming_Req then
7632 E :=
7633 Make_Object_Renaming_Declaration (Loc,
7634 Defining_Identifier => Def_Id,
7635 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7636 Name => Relocate_Node (Exp));
7638 -- Generate:
7639 -- Rnn : constant Exp_Type := Expr;
7641 else
7642 E :=
7643 Make_Object_Declaration (Loc,
7644 Defining_Identifier => Def_Id,
7645 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7646 Constant_Present => True,
7647 Expression => Relocate_Node (Exp));
7649 Set_Assignment_OK (E);
7650 end if;
7652 Insert_Action (Exp, E);
7654 -- If the expression has the form v.all then we can just capture the
7655 -- pointer, and then do an explicit dereference on the result, but
7656 -- this is not right if this is a volatile reference.
7658 elsif Nkind (Exp) = N_Explicit_Dereference
7659 and then not Is_Volatile_Reference (Exp)
7660 then
7661 Def_Id := Build_Temporary (Loc, 'R', Exp);
7662 Res :=
7663 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
7665 Insert_Action (Exp,
7666 Make_Object_Declaration (Loc,
7667 Defining_Identifier => Def_Id,
7668 Object_Definition =>
7669 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
7670 Constant_Present => True,
7671 Expression => Relocate_Node (Prefix (Exp))));
7673 -- Similar processing for an unchecked conversion of an expression of
7674 -- the form v.all, where we want the same kind of treatment.
7676 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7677 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
7678 then
7679 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
7680 goto Leave;
7682 -- If this is a type conversion, leave the type conversion and remove
7683 -- the side effects in the expression. This is important in several
7684 -- circumstances: for change of representations, and also when this is a
7685 -- view conversion to a smaller object, where gigi can end up creating
7686 -- its own temporary of the wrong size.
7688 elsif Nkind (Exp) = N_Type_Conversion then
7689 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
7690 goto Leave;
7692 -- If this is an unchecked conversion that Gigi can't handle, make
7693 -- a copy or a use a renaming to capture the value.
7695 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7696 and then not Safe_Unchecked_Type_Conversion (Exp)
7697 then
7698 if CW_Or_Has_Controlled_Part (Exp_Type) then
7700 -- Use a renaming to capture the expression, rather than create
7701 -- a controlled temporary.
7703 Def_Id := Build_Temporary (Loc, 'R', Exp);
7704 Res := New_Occurrence_Of (Def_Id, Loc);
7706 Insert_Action (Exp,
7707 Make_Object_Renaming_Declaration (Loc,
7708 Defining_Identifier => Def_Id,
7709 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7710 Name => Relocate_Node (Exp)));
7712 else
7713 Def_Id := Build_Temporary (Loc, 'R', Exp);
7714 Set_Etype (Def_Id, Exp_Type);
7715 Res := New_Occurrence_Of (Def_Id, Loc);
7717 E :=
7718 Make_Object_Declaration (Loc,
7719 Defining_Identifier => Def_Id,
7720 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7721 Constant_Present => not Is_Variable (Exp),
7722 Expression => Relocate_Node (Exp));
7724 Set_Assignment_OK (E);
7725 Insert_Action (Exp, E);
7726 end if;
7728 -- For expressions that denote names, we can use a renaming scheme.
7729 -- This is needed for correctness in the case of a volatile object of
7730 -- a non-volatile type because the Make_Reference call of the "default"
7731 -- approach would generate an illegal access value (an access value
7732 -- cannot designate such an object - see Analyze_Reference).
7734 elsif Is_Name_Reference (Exp)
7736 -- We skip using this scheme if we have an object of a volatile
7737 -- type and we do not have Name_Req set true (see comments for
7738 -- Side_Effect_Free).
7740 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
7741 then
7742 Def_Id := Build_Temporary (Loc, 'R', Exp);
7743 Res := New_Occurrence_Of (Def_Id, Loc);
7745 Insert_Action (Exp,
7746 Make_Object_Renaming_Declaration (Loc,
7747 Defining_Identifier => Def_Id,
7748 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7749 Name => Relocate_Node (Exp)));
7751 -- If this is a packed reference, or a selected component with
7752 -- a non-standard representation, a reference to the temporary
7753 -- will be replaced by a copy of the original expression (see
7754 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
7755 -- elaborated by gigi, and is of course not to be replaced in-line
7756 -- by the expression it renames, which would defeat the purpose of
7757 -- removing the side-effect.
7759 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
7760 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
7761 then
7762 null;
7763 else
7764 Set_Is_Renaming_Of_Object (Def_Id, False);
7765 end if;
7767 -- Avoid generating a variable-sized temporary, by generating the
7768 -- reference just for the function call. The transformation could be
7769 -- refined to apply only when the array component is constrained by a
7770 -- discriminant???
7772 elsif Nkind (Exp) = N_Selected_Component
7773 and then Nkind (Prefix (Exp)) = N_Function_Call
7774 and then Is_Array_Type (Exp_Type)
7775 then
7776 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
7777 goto Leave;
7779 -- Otherwise we generate a reference to the expression
7781 else
7782 -- An expression which is in SPARK mode is considered side effect
7783 -- free if the resulting value is captured by a variable or a
7784 -- constant.
7786 if GNATprove_Mode
7787 and then Nkind (Parent (Exp)) = N_Object_Declaration
7788 then
7789 goto Leave;
7790 end if;
7792 -- Special processing for function calls that return a limited type.
7793 -- We need to build a declaration that will enable build-in-place
7794 -- expansion of the call. This is not done if the context is already
7795 -- an object declaration, to prevent infinite recursion.
7797 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
7798 -- to accommodate functions returning limited objects by reference.
7800 if Ada_Version >= Ada_2005
7801 and then Nkind (Exp) = N_Function_Call
7802 and then Is_Limited_View (Etype (Exp))
7803 and then Nkind (Parent (Exp)) /= N_Object_Declaration
7804 then
7805 declare
7806 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
7807 Decl : Node_Id;
7809 begin
7810 Decl :=
7811 Make_Object_Declaration (Loc,
7812 Defining_Identifier => Obj,
7813 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7814 Expression => Relocate_Node (Exp));
7816 Insert_Action (Exp, Decl);
7817 Set_Etype (Obj, Exp_Type);
7818 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
7819 goto Leave;
7820 end;
7821 end if;
7823 Def_Id := Build_Temporary (Loc, 'R', Exp);
7825 -- The regular expansion of functions with side effects involves the
7826 -- generation of an access type to capture the return value found on
7827 -- the secondary stack. Since SPARK (and why) cannot process access
7828 -- types, use a different approach which ignores the secondary stack
7829 -- and "copies" the returned object.
7831 if GNATprove_Mode then
7832 Res := New_Occurrence_Of (Def_Id, Loc);
7833 Ref_Type := Exp_Type;
7835 -- Regular expansion utilizing an access type and 'reference
7837 else
7838 Res :=
7839 Make_Explicit_Dereference (Loc,
7840 Prefix => New_Occurrence_Of (Def_Id, Loc));
7842 -- Generate:
7843 -- type Ann is access all <Exp_Type>;
7845 Ref_Type := Make_Temporary (Loc, 'A');
7847 Ptr_Typ_Decl :=
7848 Make_Full_Type_Declaration (Loc,
7849 Defining_Identifier => Ref_Type,
7850 Type_Definition =>
7851 Make_Access_To_Object_Definition (Loc,
7852 All_Present => True,
7853 Subtype_Indication =>
7854 New_Occurrence_Of (Exp_Type, Loc)));
7856 Insert_Action (Exp, Ptr_Typ_Decl);
7857 end if;
7859 E := Exp;
7860 if Nkind (E) = N_Explicit_Dereference then
7861 New_Exp := Relocate_Node (Prefix (E));
7863 else
7864 E := Relocate_Node (E);
7866 -- Do not generate a 'reference in SPARK mode since the access
7867 -- type is not created in the first place.
7869 if GNATprove_Mode then
7870 New_Exp := E;
7872 -- Otherwise generate reference, marking the value as non-null
7873 -- since we know it cannot be null and we don't want a check.
7875 else
7876 New_Exp := Make_Reference (Loc, E);
7877 Set_Is_Known_Non_Null (Def_Id);
7878 end if;
7879 end if;
7881 if Is_Delayed_Aggregate (E) then
7883 -- The expansion of nested aggregates is delayed until the
7884 -- enclosing aggregate is expanded. As aggregates are often
7885 -- qualified, the predicate applies to qualified expressions as
7886 -- well, indicating that the enclosing aggregate has not been
7887 -- expanded yet. At this point the aggregate is part of a
7888 -- stand-alone declaration, and must be fully expanded.
7890 if Nkind (E) = N_Qualified_Expression then
7891 Set_Expansion_Delayed (Expression (E), False);
7892 Set_Analyzed (Expression (E), False);
7893 else
7894 Set_Expansion_Delayed (E, False);
7895 end if;
7897 Set_Analyzed (E, False);
7898 end if;
7900 Insert_Action (Exp,
7901 Make_Object_Declaration (Loc,
7902 Defining_Identifier => Def_Id,
7903 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
7904 Constant_Present => True,
7905 Expression => New_Exp));
7906 end if;
7908 -- Preserve the Assignment_OK flag in all copies, since at least one
7909 -- copy may be used in a context where this flag must be set (otherwise
7910 -- why would the flag be set in the first place).
7912 Set_Assignment_OK (Res, Assignment_OK (Exp));
7914 -- Finally rewrite the original expression and we are done
7916 Rewrite (Exp, Res);
7917 Analyze_And_Resolve (Exp, Exp_Type);
7919 <<Leave>>
7920 Scope_Suppress := Svg_Suppress;
7921 end Remove_Side_Effects;
7923 ---------------------------
7924 -- Represented_As_Scalar --
7925 ---------------------------
7927 function Represented_As_Scalar (T : Entity_Id) return Boolean is
7928 UT : constant Entity_Id := Underlying_Type (T);
7929 begin
7930 return Is_Scalar_Type (UT)
7931 or else (Is_Bit_Packed_Array (UT)
7932 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
7933 end Represented_As_Scalar;
7935 ------------------------------
7936 -- Requires_Cleanup_Actions --
7937 ------------------------------
7939 function Requires_Cleanup_Actions
7940 (N : Node_Id;
7941 Lib_Level : Boolean) return Boolean
7943 At_Lib_Level : constant Boolean :=
7944 Lib_Level
7945 and then Nkind_In (N, N_Package_Body,
7946 N_Package_Specification);
7947 -- N is at the library level if the top-most context is a package and
7948 -- the path taken to reach N does not inlcude non-package constructs.
7950 begin
7951 case Nkind (N) is
7952 when N_Accept_Statement |
7953 N_Block_Statement |
7954 N_Entry_Body |
7955 N_Package_Body |
7956 N_Protected_Body |
7957 N_Subprogram_Body |
7958 N_Task_Body =>
7959 return
7960 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
7961 or else
7962 (Present (Handled_Statement_Sequence (N))
7963 and then
7964 Requires_Cleanup_Actions
7965 (Statements (Handled_Statement_Sequence (N)),
7966 At_Lib_Level, True));
7968 when N_Package_Specification =>
7969 return
7970 Requires_Cleanup_Actions
7971 (Visible_Declarations (N), At_Lib_Level, True)
7972 or else
7973 Requires_Cleanup_Actions
7974 (Private_Declarations (N), At_Lib_Level, True);
7976 when others =>
7977 return False;
7978 end case;
7979 end Requires_Cleanup_Actions;
7981 ------------------------------
7982 -- Requires_Cleanup_Actions --
7983 ------------------------------
7985 function Requires_Cleanup_Actions
7986 (L : List_Id;
7987 Lib_Level : Boolean;
7988 Nested_Constructs : Boolean) return Boolean
7990 Decl : Node_Id;
7991 Expr : Node_Id;
7992 Obj_Id : Entity_Id;
7993 Obj_Typ : Entity_Id;
7994 Pack_Id : Entity_Id;
7995 Typ : Entity_Id;
7997 begin
7998 if No (L)
7999 or else Is_Empty_List (L)
8000 then
8001 return False;
8002 end if;
8004 Decl := First (L);
8005 while Present (Decl) loop
8007 -- Library-level tagged types
8009 if Nkind (Decl) = N_Full_Type_Declaration then
8010 Typ := Defining_Identifier (Decl);
8012 -- Ignored Ghost types do not need any cleanup actions because
8013 -- they will not appear in the final tree.
8015 if Is_Ignored_Ghost_Entity (Typ) then
8016 null;
8018 elsif Is_Tagged_Type (Typ)
8019 and then Is_Library_Level_Entity (Typ)
8020 and then Convention (Typ) = Convention_Ada
8021 and then Present (Access_Disp_Table (Typ))
8022 and then RTE_Available (RE_Unregister_Tag)
8023 and then not Is_Abstract_Type (Typ)
8024 and then not No_Run_Time_Mode
8025 then
8026 return True;
8027 end if;
8029 -- Regular object declarations
8031 elsif Nkind (Decl) = N_Object_Declaration then
8032 Obj_Id := Defining_Identifier (Decl);
8033 Obj_Typ := Base_Type (Etype (Obj_Id));
8034 Expr := Expression (Decl);
8036 -- Bypass any form of processing for objects which have their
8037 -- finalization disabled. This applies only to objects at the
8038 -- library level.
8040 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
8041 null;
8043 -- Transient variables are treated separately in order to minimize
8044 -- the size of the generated code. See Exp_Ch7.Process_Transient_
8045 -- Objects.
8047 elsif Is_Processed_Transient (Obj_Id) then
8048 null;
8050 -- Ignored Ghost objects do not need any cleanup actions because
8051 -- they will not appear in the final tree.
8053 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
8054 null;
8056 -- The object is of the form:
8057 -- Obj : Typ [:= Expr];
8059 -- Do not process the incomplete view of a deferred constant. Do
8060 -- not consider tag-to-class-wide conversions.
8062 elsif not Is_Imported (Obj_Id)
8063 and then Needs_Finalization (Obj_Typ)
8064 and then not (Ekind (Obj_Id) = E_Constant
8065 and then not Has_Completion (Obj_Id))
8066 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8067 then
8068 return True;
8070 -- The object is of the form:
8071 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
8073 -- Obj : Access_Typ :=
8074 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
8076 elsif Is_Access_Type (Obj_Typ)
8077 and then Needs_Finalization
8078 (Available_View (Designated_Type (Obj_Typ)))
8079 and then Present (Expr)
8080 and then
8081 (Is_Secondary_Stack_BIP_Func_Call (Expr)
8082 or else
8083 (Is_Non_BIP_Func_Call (Expr)
8084 and then not Is_Related_To_Func_Return (Obj_Id)))
8085 then
8086 return True;
8088 -- Processing for "hook" objects generated for controlled
8089 -- transients declared inside an Expression_With_Actions.
8091 elsif Is_Access_Type (Obj_Typ)
8092 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
8093 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
8094 N_Object_Declaration
8095 then
8096 return True;
8098 -- Processing for intermediate results of if expressions where
8099 -- one of the alternatives uses a controlled function call.
8101 elsif Is_Access_Type (Obj_Typ)
8102 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
8103 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
8104 N_Defining_Identifier
8105 and then Present (Expr)
8106 and then Nkind (Expr) = N_Null
8107 then
8108 return True;
8110 -- Simple protected objects which use type System.Tasking.
8111 -- Protected_Objects.Protection to manage their locks should be
8112 -- treated as controlled since they require manual cleanup.
8114 elsif Ekind (Obj_Id) = E_Variable
8115 and then (Is_Simple_Protected_Type (Obj_Typ)
8116 or else Has_Simple_Protected_Object (Obj_Typ))
8117 then
8118 return True;
8119 end if;
8121 -- Specific cases of object renamings
8123 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
8124 Obj_Id := Defining_Identifier (Decl);
8125 Obj_Typ := Base_Type (Etype (Obj_Id));
8127 -- Bypass any form of processing for objects which have their
8128 -- finalization disabled. This applies only to objects at the
8129 -- library level.
8131 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
8132 null;
8134 -- Ignored Ghost object renamings do not need any cleanup actions
8135 -- because they will not appear in the final tree.
8137 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
8138 null;
8140 -- Return object of a build-in-place function. This case is
8141 -- recognized and marked by the expansion of an extended return
8142 -- statement (see Expand_N_Extended_Return_Statement).
8144 elsif Needs_Finalization (Obj_Typ)
8145 and then Is_Return_Object (Obj_Id)
8146 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
8147 then
8148 return True;
8150 -- Detect a case where a source object has been initialized by
8151 -- a controlled function call or another object which was later
8152 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
8154 -- Obj1 : CW_Type := Src_Obj;
8155 -- Obj2 : CW_Type := Function_Call (...);
8157 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
8158 -- Tmp : ... := Function_Call (...)'reference;
8159 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
8161 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
8162 return True;
8163 end if;
8165 -- Inspect the freeze node of an access-to-controlled type and look
8166 -- for a delayed finalization master. This case arises when the
8167 -- freeze actions are inserted at a later time than the expansion of
8168 -- the context. Since Build_Finalizer is never called on a single
8169 -- construct twice, the master will be ultimately left out and never
8170 -- finalized. This is also needed for freeze actions of designated
8171 -- types themselves, since in some cases the finalization master is
8172 -- associated with a designated type's freeze node rather than that
8173 -- of the access type (see handling for freeze actions in
8174 -- Build_Finalization_Master).
8176 elsif Nkind (Decl) = N_Freeze_Entity
8177 and then Present (Actions (Decl))
8178 then
8179 Typ := Entity (Decl);
8181 -- Freeze nodes for ignored Ghost types do not need cleanup
8182 -- actions because they will never appear in the final tree.
8184 if Is_Ignored_Ghost_Entity (Typ) then
8185 null;
8187 elsif ((Is_Access_Type (Typ)
8188 and then not Is_Access_Subprogram_Type (Typ)
8189 and then Needs_Finalization
8190 (Available_View (Designated_Type (Typ))))
8191 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
8192 and then Requires_Cleanup_Actions
8193 (Actions (Decl), Lib_Level, Nested_Constructs)
8194 then
8195 return True;
8196 end if;
8198 -- Nested package declarations
8200 elsif Nested_Constructs
8201 and then Nkind (Decl) = N_Package_Declaration
8202 then
8203 Pack_Id := Defining_Entity (Decl);
8205 -- Do not inspect an ignored Ghost package because all code found
8206 -- within will not appear in the final tree.
8208 if Is_Ignored_Ghost_Entity (Pack_Id) then
8209 null;
8211 elsif Ekind (Pack_Id) /= E_Generic_Package
8212 and then Requires_Cleanup_Actions
8213 (Specification (Decl), Lib_Level)
8214 then
8215 return True;
8216 end if;
8218 -- Nested package bodies
8220 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
8222 -- Do not inspect an ignored Ghost package body because all code
8223 -- found within will not appear in the final tree.
8225 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
8226 null;
8228 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
8229 and then Requires_Cleanup_Actions (Decl, Lib_Level)
8230 then
8231 return True;
8232 end if;
8234 elsif Nkind (Decl) = N_Block_Statement
8235 and then
8237 -- Handle a rare case caused by a controlled transient variable
8238 -- created as part of a record init proc. The variable is wrapped
8239 -- in a block, but the block is not associated with a transient
8240 -- scope.
8242 (Inside_Init_Proc
8244 -- Handle the case where the original context has been wrapped in
8245 -- a block to avoid interference between exception handlers and
8246 -- At_End handlers. Treat the block as transparent and process its
8247 -- contents.
8249 or else Is_Finalization_Wrapper (Decl))
8250 then
8251 if Requires_Cleanup_Actions (Decl, Lib_Level) then
8252 return True;
8253 end if;
8254 end if;
8256 Next (Decl);
8257 end loop;
8259 return False;
8260 end Requires_Cleanup_Actions;
8262 ------------------------------------
8263 -- Safe_Unchecked_Type_Conversion --
8264 ------------------------------------
8266 -- Note: this function knows quite a bit about the exact requirements of
8267 -- Gigi with respect to unchecked type conversions, and its code must be
8268 -- coordinated with any changes in Gigi in this area.
8270 -- The above requirements should be documented in Sinfo ???
8272 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
8273 Otyp : Entity_Id;
8274 Ityp : Entity_Id;
8275 Oalign : Uint;
8276 Ialign : Uint;
8277 Pexp : constant Node_Id := Parent (Exp);
8279 begin
8280 -- If the expression is the RHS of an assignment or object declaration
8281 -- we are always OK because there will always be a target.
8283 -- Object renaming declarations, (generated for view conversions of
8284 -- actuals in inlined calls), like object declarations, provide an
8285 -- explicit type, and are safe as well.
8287 if (Nkind (Pexp) = N_Assignment_Statement
8288 and then Expression (Pexp) = Exp)
8289 or else Nkind_In (Pexp, N_Object_Declaration,
8290 N_Object_Renaming_Declaration)
8291 then
8292 return True;
8294 -- If the expression is the prefix of an N_Selected_Component we should
8295 -- also be OK because GCC knows to look inside the conversion except if
8296 -- the type is discriminated. We assume that we are OK anyway if the
8297 -- type is not set yet or if it is controlled since we can't afford to
8298 -- introduce a temporary in this case.
8300 elsif Nkind (Pexp) = N_Selected_Component
8301 and then Prefix (Pexp) = Exp
8302 then
8303 if No (Etype (Pexp)) then
8304 return True;
8305 else
8306 return
8307 not Has_Discriminants (Etype (Pexp))
8308 or else Is_Constrained (Etype (Pexp));
8309 end if;
8310 end if;
8312 -- Set the output type, this comes from Etype if it is set, otherwise we
8313 -- take it from the subtype mark, which we assume was already fully
8314 -- analyzed.
8316 if Present (Etype (Exp)) then
8317 Otyp := Etype (Exp);
8318 else
8319 Otyp := Entity (Subtype_Mark (Exp));
8320 end if;
8322 -- The input type always comes from the expression, and we assume this
8323 -- is indeed always analyzed, so we can simply get the Etype.
8325 Ityp := Etype (Expression (Exp));
8327 -- Initialize alignments to unknown so far
8329 Oalign := No_Uint;
8330 Ialign := No_Uint;
8332 -- Replace a concurrent type by its corresponding record type and each
8333 -- type by its underlying type and do the tests on those. The original
8334 -- type may be a private type whose completion is a concurrent type, so
8335 -- find the underlying type first.
8337 if Present (Underlying_Type (Otyp)) then
8338 Otyp := Underlying_Type (Otyp);
8339 end if;
8341 if Present (Underlying_Type (Ityp)) then
8342 Ityp := Underlying_Type (Ityp);
8343 end if;
8345 if Is_Concurrent_Type (Otyp) then
8346 Otyp := Corresponding_Record_Type (Otyp);
8347 end if;
8349 if Is_Concurrent_Type (Ityp) then
8350 Ityp := Corresponding_Record_Type (Ityp);
8351 end if;
8353 -- If the base types are the same, we know there is no problem since
8354 -- this conversion will be a noop.
8356 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
8357 return True;
8359 -- Same if this is an upwards conversion of an untagged type, and there
8360 -- are no constraints involved (could be more general???)
8362 elsif Etype (Ityp) = Otyp
8363 and then not Is_Tagged_Type (Ityp)
8364 and then not Has_Discriminants (Ityp)
8365 and then No (First_Rep_Item (Base_Type (Ityp)))
8366 then
8367 return True;
8369 -- If the expression has an access type (object or subprogram) we assume
8370 -- that the conversion is safe, because the size of the target is safe,
8371 -- even if it is a record (which might be treated as having unknown size
8372 -- at this point).
8374 elsif Is_Access_Type (Ityp) then
8375 return True;
8377 -- If the size of output type is known at compile time, there is never
8378 -- a problem. Note that unconstrained records are considered to be of
8379 -- known size, but we can't consider them that way here, because we are
8380 -- talking about the actual size of the object.
8382 -- We also make sure that in addition to the size being known, we do not
8383 -- have a case which might generate an embarrassingly large temp in
8384 -- stack checking mode.
8386 elsif Size_Known_At_Compile_Time (Otyp)
8387 and then
8388 (not Stack_Checking_Enabled
8389 or else not May_Generate_Large_Temp (Otyp))
8390 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
8391 then
8392 return True;
8394 -- If either type is tagged, then we know the alignment is OK so Gigi
8395 -- will be able to use pointer punning.
8397 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
8398 return True;
8400 -- If either type is a limited record type, we cannot do a copy, so say
8401 -- safe since there's nothing else we can do.
8403 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
8404 return True;
8406 -- Conversions to and from packed array types are always ignored and
8407 -- hence are safe.
8409 elsif Is_Packed_Array_Impl_Type (Otyp)
8410 or else Is_Packed_Array_Impl_Type (Ityp)
8411 then
8412 return True;
8413 end if;
8415 -- The only other cases known to be safe is if the input type's
8416 -- alignment is known to be at least the maximum alignment for the
8417 -- target or if both alignments are known and the output type's
8418 -- alignment is no stricter than the input's. We can use the component
8419 -- type alignement for an array if a type is an unpacked array type.
8421 if Present (Alignment_Clause (Otyp)) then
8422 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
8424 elsif Is_Array_Type (Otyp)
8425 and then Present (Alignment_Clause (Component_Type (Otyp)))
8426 then
8427 Oalign := Expr_Value (Expression (Alignment_Clause
8428 (Component_Type (Otyp))));
8429 end if;
8431 if Present (Alignment_Clause (Ityp)) then
8432 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
8434 elsif Is_Array_Type (Ityp)
8435 and then Present (Alignment_Clause (Component_Type (Ityp)))
8436 then
8437 Ialign := Expr_Value (Expression (Alignment_Clause
8438 (Component_Type (Ityp))));
8439 end if;
8441 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
8442 return True;
8444 elsif Ialign /= No_Uint
8445 and then Oalign /= No_Uint
8446 and then Ialign <= Oalign
8447 then
8448 return True;
8450 -- Otherwise, Gigi cannot handle this and we must make a temporary
8452 else
8453 return False;
8454 end if;
8455 end Safe_Unchecked_Type_Conversion;
8457 ---------------------------------
8458 -- Set_Current_Value_Condition --
8459 ---------------------------------
8461 -- Note: the implementation of this procedure is very closely tied to the
8462 -- implementation of Get_Current_Value_Condition. Here we set required
8463 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
8464 -- them, so they must have a consistent view.
8466 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
8468 procedure Set_Entity_Current_Value (N : Node_Id);
8469 -- If N is an entity reference, where the entity is of an appropriate
8470 -- kind, then set the current value of this entity to Cnode, unless
8471 -- there is already a definite value set there.
8473 procedure Set_Expression_Current_Value (N : Node_Id);
8474 -- If N is of an appropriate form, sets an appropriate entry in current
8475 -- value fields of relevant entities. Multiple entities can be affected
8476 -- in the case of an AND or AND THEN.
8478 ------------------------------
8479 -- Set_Entity_Current_Value --
8480 ------------------------------
8482 procedure Set_Entity_Current_Value (N : Node_Id) is
8483 begin
8484 if Is_Entity_Name (N) then
8485 declare
8486 Ent : constant Entity_Id := Entity (N);
8488 begin
8489 -- Don't capture if not safe to do so
8491 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
8492 return;
8493 end if;
8495 -- Here we have a case where the Current_Value field may need
8496 -- to be set. We set it if it is not already set to a compile
8497 -- time expression value.
8499 -- Note that this represents a decision that one condition
8500 -- blots out another previous one. That's certainly right if
8501 -- they occur at the same level. If the second one is nested,
8502 -- then the decision is neither right nor wrong (it would be
8503 -- equally OK to leave the outer one in place, or take the new
8504 -- inner one. Really we should record both, but our data
8505 -- structures are not that elaborate.
8507 if Nkind (Current_Value (Ent)) not in N_Subexpr then
8508 Set_Current_Value (Ent, Cnode);
8509 end if;
8510 end;
8511 end if;
8512 end Set_Entity_Current_Value;
8514 ----------------------------------
8515 -- Set_Expression_Current_Value --
8516 ----------------------------------
8518 procedure Set_Expression_Current_Value (N : Node_Id) is
8519 Cond : Node_Id;
8521 begin
8522 Cond := N;
8524 -- Loop to deal with (ignore for now) any NOT operators present. The
8525 -- presence of NOT operators will be handled properly when we call
8526 -- Get_Current_Value_Condition.
8528 while Nkind (Cond) = N_Op_Not loop
8529 Cond := Right_Opnd (Cond);
8530 end loop;
8532 -- For an AND or AND THEN, recursively process operands
8534 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
8535 Set_Expression_Current_Value (Left_Opnd (Cond));
8536 Set_Expression_Current_Value (Right_Opnd (Cond));
8537 return;
8538 end if;
8540 -- Check possible relational operator
8542 if Nkind (Cond) in N_Op_Compare then
8543 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
8544 Set_Entity_Current_Value (Left_Opnd (Cond));
8545 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
8546 Set_Entity_Current_Value (Right_Opnd (Cond));
8547 end if;
8549 elsif Nkind_In (Cond,
8550 N_Type_Conversion,
8551 N_Qualified_Expression,
8552 N_Expression_With_Actions)
8553 then
8554 Set_Expression_Current_Value (Expression (Cond));
8556 -- Check possible boolean variable reference
8558 else
8559 Set_Entity_Current_Value (Cond);
8560 end if;
8561 end Set_Expression_Current_Value;
8563 -- Start of processing for Set_Current_Value_Condition
8565 begin
8566 Set_Expression_Current_Value (Condition (Cnode));
8567 end Set_Current_Value_Condition;
8569 --------------------------
8570 -- Set_Elaboration_Flag --
8571 --------------------------
8573 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
8574 Loc : constant Source_Ptr := Sloc (N);
8575 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
8576 Asn : Node_Id;
8578 begin
8579 if Present (Ent) then
8581 -- Nothing to do if at the compilation unit level, because in this
8582 -- case the flag is set by the binder generated elaboration routine.
8584 if Nkind (Parent (N)) = N_Compilation_Unit then
8585 null;
8587 -- Here we do need to generate an assignment statement
8589 else
8590 Check_Restriction (No_Elaboration_Code, N);
8591 Asn :=
8592 Make_Assignment_Statement (Loc,
8593 Name => New_Occurrence_Of (Ent, Loc),
8594 Expression => Make_Integer_Literal (Loc, Uint_1));
8596 if Nkind (Parent (N)) = N_Subunit then
8597 Insert_After (Corresponding_Stub (Parent (N)), Asn);
8598 else
8599 Insert_After (N, Asn);
8600 end if;
8602 Analyze (Asn);
8604 -- Kill current value indication. This is necessary because the
8605 -- tests of this flag are inserted out of sequence and must not
8606 -- pick up bogus indications of the wrong constant value.
8608 Set_Current_Value (Ent, Empty);
8610 -- If the subprogram is in the current declarative part and
8611 -- 'access has been applied to it, generate an elaboration
8612 -- check at the beginning of the declarations of the body.
8614 if Nkind (N) = N_Subprogram_Body
8615 and then Address_Taken (Spec_Id)
8616 and then
8617 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
8618 then
8619 declare
8620 Loc : constant Source_Ptr := Sloc (N);
8621 Decls : constant List_Id := Declarations (N);
8622 Chk : Node_Id;
8624 begin
8625 -- No need to generate this check if first entry in the
8626 -- declaration list is a raise of Program_Error now.
8628 if Present (Decls)
8629 and then Nkind (First (Decls)) = N_Raise_Program_Error
8630 then
8631 return;
8632 end if;
8634 -- Otherwise generate the check
8636 Chk :=
8637 Make_Raise_Program_Error (Loc,
8638 Condition =>
8639 Make_Op_Eq (Loc,
8640 Left_Opnd => New_Occurrence_Of (Ent, Loc),
8641 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
8642 Reason => PE_Access_Before_Elaboration);
8644 if No (Decls) then
8645 Set_Declarations (N, New_List (Chk));
8646 else
8647 Prepend (Chk, Decls);
8648 end if;
8650 Analyze (Chk);
8651 end;
8652 end if;
8653 end if;
8654 end if;
8655 end Set_Elaboration_Flag;
8657 ----------------------------
8658 -- Set_Renamed_Subprogram --
8659 ----------------------------
8661 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
8662 begin
8663 -- If input node is an identifier, we can just reset it
8665 if Nkind (N) = N_Identifier then
8666 Set_Chars (N, Chars (E));
8667 Set_Entity (N, E);
8669 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
8671 else
8672 declare
8673 CS : constant Boolean := Comes_From_Source (N);
8674 begin
8675 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
8676 Set_Entity (N, E);
8677 Set_Comes_From_Source (N, CS);
8678 Set_Analyzed (N, True);
8679 end;
8680 end if;
8681 end Set_Renamed_Subprogram;
8683 ----------------------
8684 -- Side_Effect_Free --
8685 ----------------------
8687 function Side_Effect_Free
8688 (N : Node_Id;
8689 Name_Req : Boolean := False;
8690 Variable_Ref : Boolean := False) return Boolean
8692 Typ : constant Entity_Id := Etype (N);
8693 -- Result type of the expression
8695 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
8696 -- The argument N is a construct where the Prefix is dereferenced if it
8697 -- is an access type and the result is a variable. The call returns True
8698 -- if the construct is side effect free (not considering side effects in
8699 -- other than the prefix which are to be tested by the caller).
8701 function Within_In_Parameter (N : Node_Id) return Boolean;
8702 -- Determines if N is a subcomponent of a composite in-parameter. If so,
8703 -- N is not side-effect free when the actual is global and modifiable
8704 -- indirectly from within a subprogram, because it may be passed by
8705 -- reference. The front-end must be conservative here and assume that
8706 -- this may happen with any array or record type. On the other hand, we
8707 -- cannot create temporaries for all expressions for which this
8708 -- condition is true, for various reasons that might require clearing up
8709 -- ??? For example, discriminant references that appear out of place, or
8710 -- spurious type errors with class-wide expressions. As a result, we
8711 -- limit the transformation to loop bounds, which is so far the only
8712 -- case that requires it.
8714 -----------------------------
8715 -- Safe_Prefixed_Reference --
8716 -----------------------------
8718 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
8719 begin
8720 -- If prefix is not side effect free, definitely not safe
8722 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
8723 return False;
8725 -- If the prefix is of an access type that is not access-to-constant,
8726 -- then this construct is a variable reference, which means it is to
8727 -- be considered to have side effects if Variable_Ref is set True.
8729 elsif Is_Access_Type (Etype (Prefix (N)))
8730 and then not Is_Access_Constant (Etype (Prefix (N)))
8731 and then Variable_Ref
8732 then
8733 -- Exception is a prefix that is the result of a previous removal
8734 -- of side-effects.
8736 return Is_Entity_Name (Prefix (N))
8737 and then not Comes_From_Source (Prefix (N))
8738 and then Ekind (Entity (Prefix (N))) = E_Constant
8739 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
8741 -- If the prefix is an explicit dereference then this construct is a
8742 -- variable reference, which means it is to be considered to have
8743 -- side effects if Variable_Ref is True.
8745 -- We do NOT exclude dereferences of access-to-constant types because
8746 -- we handle them as constant view of variables.
8748 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
8749 and then Variable_Ref
8750 then
8751 return False;
8753 -- Note: The following test is the simplest way of solving a complex
8754 -- problem uncovered by the following test (Side effect on loop bound
8755 -- that is a subcomponent of a global variable:
8757 -- with Text_Io; use Text_Io;
8758 -- procedure Tloop is
8759 -- type X is
8760 -- record
8761 -- V : Natural := 4;
8762 -- S : String (1..5) := (others => 'a');
8763 -- end record;
8764 -- X1 : X;
8766 -- procedure Modi;
8768 -- generic
8769 -- with procedure Action;
8770 -- procedure Loop_G (Arg : X; Msg : String)
8772 -- procedure Loop_G (Arg : X; Msg : String) is
8773 -- begin
8774 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
8775 -- & Natural'Image (Arg.V));
8776 -- for Index in 1 .. Arg.V loop
8777 -- Text_Io.Put_Line
8778 -- (Natural'Image (Index) & " " & Arg.S (Index));
8779 -- if Index > 2 then
8780 -- Modi;
8781 -- end if;
8782 -- end loop;
8783 -- Put_Line ("end loop_g " & Msg);
8784 -- end;
8786 -- procedure Loop1 is new Loop_G (Modi);
8787 -- procedure Modi is
8788 -- begin
8789 -- X1.V := 1;
8790 -- Loop1 (X1, "from modi");
8791 -- end;
8793 -- begin
8794 -- Loop1 (X1, "initial");
8795 -- end;
8797 -- The output of the above program should be:
8799 -- begin loop_g initial will loop till: 4
8800 -- 1 a
8801 -- 2 a
8802 -- 3 a
8803 -- begin loop_g from modi will loop till: 1
8804 -- 1 a
8805 -- end loop_g from modi
8806 -- 4 a
8807 -- begin loop_g from modi will loop till: 1
8808 -- 1 a
8809 -- end loop_g from modi
8810 -- end loop_g initial
8812 -- If a loop bound is a subcomponent of a global variable, a
8813 -- modification of that variable within the loop may incorrectly
8814 -- affect the execution of the loop.
8816 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
8817 and then Within_In_Parameter (Prefix (N))
8818 and then Variable_Ref
8819 then
8820 return False;
8822 -- All other cases are side effect free
8824 else
8825 return True;
8826 end if;
8827 end Safe_Prefixed_Reference;
8829 -------------------------
8830 -- Within_In_Parameter --
8831 -------------------------
8833 function Within_In_Parameter (N : Node_Id) return Boolean is
8834 begin
8835 if not Comes_From_Source (N) then
8836 return False;
8838 elsif Is_Entity_Name (N) then
8839 return Ekind (Entity (N)) = E_In_Parameter;
8841 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8842 return Within_In_Parameter (Prefix (N));
8844 else
8845 return False;
8846 end if;
8847 end Within_In_Parameter;
8849 -- Start of processing for Side_Effect_Free
8851 begin
8852 -- If volatile reference, always consider it to have side effects
8854 if Is_Volatile_Reference (N) then
8855 return False;
8856 end if;
8858 -- Note on checks that could raise Constraint_Error. Strictly, if we
8859 -- take advantage of 11.6, these checks do not count as side effects.
8860 -- However, we would prefer to consider that they are side effects,
8861 -- since the backend CSE does not work very well on expressions which
8862 -- can raise Constraint_Error. On the other hand if we don't consider
8863 -- them to be side effect free, then we get some awkward expansions
8864 -- in -gnato mode, resulting in code insertions at a point where we
8865 -- do not have a clear model for performing the insertions.
8867 -- Special handling for entity names
8869 if Is_Entity_Name (N) then
8871 -- A type reference is always side effect free
8873 if Is_Type (Entity (N)) then
8874 return True;
8876 -- Variables are considered to be a side effect if Variable_Ref
8877 -- is set or if we have a volatile reference and Name_Req is off.
8878 -- If Name_Req is True then we can't help returning a name which
8879 -- effectively allows multiple references in any case.
8881 elsif Is_Variable (N, Use_Original_Node => False) then
8882 return not Variable_Ref
8883 and then (not Is_Volatile_Reference (N) or else Name_Req);
8885 -- Any other entity (e.g. a subtype name) is definitely side
8886 -- effect free.
8888 else
8889 return True;
8890 end if;
8892 -- A value known at compile time is always side effect free
8894 elsif Compile_Time_Known_Value (N) then
8895 return True;
8897 -- A variable renaming is not side-effect free, because the renaming
8898 -- will function like a macro in the front-end in some cases, and an
8899 -- assignment can modify the component designated by N, so we need to
8900 -- create a temporary for it.
8902 -- The guard testing for Entity being present is needed at least in
8903 -- the case of rewritten predicate expressions, and may well also be
8904 -- appropriate elsewhere. Obviously we can't go testing the entity
8905 -- field if it does not exist, so it's reasonable to say that this is
8906 -- not the renaming case if it does not exist.
8908 elsif Is_Entity_Name (Original_Node (N))
8909 and then Present (Entity (Original_Node (N)))
8910 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
8911 and then Ekind (Entity (Original_Node (N))) /= E_Constant
8912 then
8913 declare
8914 RO : constant Node_Id :=
8915 Renamed_Object (Entity (Original_Node (N)));
8917 begin
8918 -- If the renamed object is an indexed component, or an
8919 -- explicit dereference, then the designated object could
8920 -- be modified by an assignment.
8922 if Nkind_In (RO, N_Indexed_Component,
8923 N_Explicit_Dereference)
8924 then
8925 return False;
8927 -- A selected component must have a safe prefix
8929 elsif Nkind (RO) = N_Selected_Component then
8930 return Safe_Prefixed_Reference (RO);
8932 -- In all other cases, designated object cannot be changed so
8933 -- we are side effect free.
8935 else
8936 return True;
8937 end if;
8938 end;
8940 -- Remove_Side_Effects generates an object renaming declaration to
8941 -- capture the expression of a class-wide expression. In VM targets
8942 -- the frontend performs no expansion for dispatching calls to
8943 -- class- wide types since they are handled by the VM. Hence, we must
8944 -- locate here if this node corresponds to a previous invocation of
8945 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
8947 elsif VM_Target /= No_VM
8948 and then not Comes_From_Source (N)
8949 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
8950 and then Is_Class_Wide_Type (Typ)
8951 then
8952 return True;
8953 end if;
8955 -- For other than entity names and compile time known values,
8956 -- check the node kind for special processing.
8958 case Nkind (N) is
8960 -- An attribute reference is side effect free if its expressions
8961 -- are side effect free and its prefix is side effect free or
8962 -- is an entity reference.
8964 -- Is this right? what about x'first where x is a variable???
8966 when N_Attribute_Reference =>
8967 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
8968 and then Attribute_Name (N) /= Name_Input
8969 and then (Is_Entity_Name (Prefix (N))
8970 or else Side_Effect_Free
8971 (Prefix (N), Name_Req, Variable_Ref));
8973 -- A binary operator is side effect free if and both operands are
8974 -- side effect free. For this purpose binary operators include
8975 -- membership tests and short circuit forms.
8977 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
8978 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
8979 and then
8980 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
8982 -- An explicit dereference is side effect free only if it is
8983 -- a side effect free prefixed reference.
8985 when N_Explicit_Dereference =>
8986 return Safe_Prefixed_Reference (N);
8988 -- An expression with action is side effect free if its expression
8989 -- is side effect free and it has no actions.
8991 when N_Expression_With_Actions =>
8992 return Is_Empty_List (Actions (N))
8993 and then
8994 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8996 -- A call to _rep_to_pos is side effect free, since we generate
8997 -- this pure function call ourselves. Moreover it is critically
8998 -- important to make this exception, since otherwise we can have
8999 -- discriminants in array components which don't look side effect
9000 -- free in the case of an array whose index type is an enumeration
9001 -- type with an enumeration rep clause.
9003 -- All other function calls are not side effect free
9005 when N_Function_Call =>
9006 return Nkind (Name (N)) = N_Identifier
9007 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
9008 and then
9009 Side_Effect_Free
9010 (First (Parameter_Associations (N)), Name_Req, Variable_Ref);
9012 -- An IF expression is side effect free if it's of a scalar type, and
9013 -- all its components are all side effect free (conditions and then
9014 -- actions and else actions). We restrict to scalar types, since it
9015 -- is annoying to deal with things like (if A then B else C)'First
9016 -- where the type involved is a string type.
9018 when N_If_Expression =>
9019 return Is_Scalar_Type (Typ)
9020 and then
9021 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref);
9023 -- An indexed component is side effect free if it is a side
9024 -- effect free prefixed reference and all the indexing
9025 -- expressions are side effect free.
9027 when N_Indexed_Component =>
9028 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
9029 and then Safe_Prefixed_Reference (N);
9031 -- A type qualification is side effect free if the expression
9032 -- is side effect free.
9034 when N_Qualified_Expression =>
9035 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9037 -- A selected component is side effect free only if it is a side
9038 -- effect free prefixed reference.
9040 when N_Selected_Component =>
9041 return Safe_Prefixed_Reference (N);
9043 -- A range is side effect free if the bounds are side effect free
9045 when N_Range =>
9046 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
9047 and then
9048 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
9050 -- A slice is side effect free if it is a side effect free
9051 -- prefixed reference and the bounds are side effect free.
9053 when N_Slice =>
9054 return Side_Effect_Free
9055 (Discrete_Range (N), Name_Req, Variable_Ref)
9056 and then Safe_Prefixed_Reference (N);
9058 -- A type conversion is side effect free if the expression to be
9059 -- converted is side effect free.
9061 when N_Type_Conversion =>
9062 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9064 -- A unary operator is side effect free if the operand
9065 -- is side effect free.
9067 when N_Unary_Op =>
9068 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
9070 -- An unchecked type conversion is side effect free only if it
9071 -- is safe and its argument is side effect free.
9073 when N_Unchecked_Type_Conversion =>
9074 return Safe_Unchecked_Type_Conversion (N)
9075 and then
9076 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9078 -- An unchecked expression is side effect free if its expression
9079 -- is side effect free.
9081 when N_Unchecked_Expression =>
9082 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
9084 -- A literal is side effect free
9086 when N_Character_Literal |
9087 N_Integer_Literal |
9088 N_Real_Literal |
9089 N_String_Literal =>
9090 return True;
9092 -- We consider that anything else has side effects. This is a bit
9093 -- crude, but we are pretty close for most common cases, and we
9094 -- are certainly correct (i.e. we never return True when the
9095 -- answer should be False).
9097 when others =>
9098 return False;
9099 end case;
9100 end Side_Effect_Free;
9102 -- A list is side effect free if all elements of the list are side
9103 -- effect free.
9105 function Side_Effect_Free
9106 (L : List_Id;
9107 Name_Req : Boolean := False;
9108 Variable_Ref : Boolean := False) return Boolean
9110 N : Node_Id;
9112 begin
9113 if L = No_List or else L = Error_List then
9114 return True;
9116 else
9117 N := First (L);
9118 while Present (N) loop
9119 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
9120 return False;
9121 else
9122 Next (N);
9123 end if;
9124 end loop;
9126 return True;
9127 end if;
9128 end Side_Effect_Free;
9130 ----------------------------------
9131 -- Silly_Boolean_Array_Not_Test --
9132 ----------------------------------
9134 -- This procedure implements an odd and silly test. We explicitly check
9135 -- for the case where the 'First of the component type is equal to the
9136 -- 'Last of this component type, and if this is the case, we make sure
9137 -- that constraint error is raised. The reason is that the NOT is bound
9138 -- to cause CE in this case, and we will not otherwise catch it.
9140 -- No such check is required for AND and OR, since for both these cases
9141 -- False op False = False, and True op True = True. For the XOR case,
9142 -- see Silly_Boolean_Array_Xor_Test.
9144 -- Believe it or not, this was reported as a bug. Note that nearly always,
9145 -- the test will evaluate statically to False, so the code will be
9146 -- statically removed, and no extra overhead caused.
9148 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
9149 Loc : constant Source_Ptr := Sloc (N);
9150 CT : constant Entity_Id := Component_Type (T);
9152 begin
9153 -- The check we install is
9155 -- constraint_error when
9156 -- component_type'first = component_type'last
9157 -- and then array_type'Length /= 0)
9159 -- We need the last guard because we don't want to raise CE for empty
9160 -- arrays since no out of range values result. (Empty arrays with a
9161 -- component type of True .. True -- very useful -- even the ACATS
9162 -- does not test that marginal case).
9164 Insert_Action (N,
9165 Make_Raise_Constraint_Error (Loc,
9166 Condition =>
9167 Make_And_Then (Loc,
9168 Left_Opnd =>
9169 Make_Op_Eq (Loc,
9170 Left_Opnd =>
9171 Make_Attribute_Reference (Loc,
9172 Prefix => New_Occurrence_Of (CT, Loc),
9173 Attribute_Name => Name_First),
9175 Right_Opnd =>
9176 Make_Attribute_Reference (Loc,
9177 Prefix => New_Occurrence_Of (CT, Loc),
9178 Attribute_Name => Name_Last)),
9180 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
9181 Reason => CE_Range_Check_Failed));
9182 end Silly_Boolean_Array_Not_Test;
9184 ----------------------------------
9185 -- Silly_Boolean_Array_Xor_Test --
9186 ----------------------------------
9188 -- This procedure implements an odd and silly test. We explicitly check
9189 -- for the XOR case where the component type is True .. True, since this
9190 -- will raise constraint error. A special check is required since CE
9191 -- will not be generated otherwise (cf Expand_Packed_Not).
9193 -- No such check is required for AND and OR, since for both these cases
9194 -- False op False = False, and True op True = True, and no check is
9195 -- required for the case of False .. False, since False xor False = False.
9196 -- See also Silly_Boolean_Array_Not_Test
9198 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
9199 Loc : constant Source_Ptr := Sloc (N);
9200 CT : constant Entity_Id := Component_Type (T);
9202 begin
9203 -- The check we install is
9205 -- constraint_error when
9206 -- Boolean (component_type'First)
9207 -- and then Boolean (component_type'Last)
9208 -- and then array_type'Length /= 0)
9210 -- We need the last guard because we don't want to raise CE for empty
9211 -- arrays since no out of range values result (Empty arrays with a
9212 -- component type of True .. True -- very useful -- even the ACATS
9213 -- does not test that marginal case).
9215 Insert_Action (N,
9216 Make_Raise_Constraint_Error (Loc,
9217 Condition =>
9218 Make_And_Then (Loc,
9219 Left_Opnd =>
9220 Make_And_Then (Loc,
9221 Left_Opnd =>
9222 Convert_To (Standard_Boolean,
9223 Make_Attribute_Reference (Loc,
9224 Prefix => New_Occurrence_Of (CT, Loc),
9225 Attribute_Name => Name_First)),
9227 Right_Opnd =>
9228 Convert_To (Standard_Boolean,
9229 Make_Attribute_Reference (Loc,
9230 Prefix => New_Occurrence_Of (CT, Loc),
9231 Attribute_Name => Name_Last))),
9233 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
9234 Reason => CE_Range_Check_Failed));
9235 end Silly_Boolean_Array_Xor_Test;
9237 --------------------------
9238 -- Target_Has_Fixed_Ops --
9239 --------------------------
9241 Integer_Sized_Small : Ureal;
9242 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
9243 -- called (we don't want to compute it more than once).
9245 Long_Integer_Sized_Small : Ureal;
9246 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
9247 -- is called (we don't want to compute it more than once)
9249 First_Time_For_THFO : Boolean := True;
9250 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
9252 function Target_Has_Fixed_Ops
9253 (Left_Typ : Entity_Id;
9254 Right_Typ : Entity_Id;
9255 Result_Typ : Entity_Id) return Boolean
9257 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
9258 -- Return True if the given type is a fixed-point type with a small
9259 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
9260 -- an absolute value less than 1.0. This is currently limited to
9261 -- fixed-point types that map to Integer or Long_Integer.
9263 ------------------------
9264 -- Is_Fractional_Type --
9265 ------------------------
9267 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
9268 begin
9269 if Esize (Typ) = Standard_Integer_Size then
9270 return Small_Value (Typ) = Integer_Sized_Small;
9272 elsif Esize (Typ) = Standard_Long_Integer_Size then
9273 return Small_Value (Typ) = Long_Integer_Sized_Small;
9275 else
9276 return False;
9277 end if;
9278 end Is_Fractional_Type;
9280 -- Start of processing for Target_Has_Fixed_Ops
9282 begin
9283 -- Return False if Fractional_Fixed_Ops_On_Target is false
9285 if not Fractional_Fixed_Ops_On_Target then
9286 return False;
9287 end if;
9289 -- Here the target has Fractional_Fixed_Ops, if first time, compute
9290 -- standard constants used by Is_Fractional_Type.
9292 if First_Time_For_THFO then
9293 First_Time_For_THFO := False;
9295 Integer_Sized_Small :=
9296 UR_From_Components
9297 (Num => Uint_1,
9298 Den => UI_From_Int (Standard_Integer_Size - 1),
9299 Rbase => 2);
9301 Long_Integer_Sized_Small :=
9302 UR_From_Components
9303 (Num => Uint_1,
9304 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
9305 Rbase => 2);
9306 end if;
9308 -- Return True if target supports fixed-by-fixed multiply/divide for
9309 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
9310 -- and result types are equivalent fractional types.
9312 return Is_Fractional_Type (Base_Type (Left_Typ))
9313 and then Is_Fractional_Type (Base_Type (Right_Typ))
9314 and then Is_Fractional_Type (Base_Type (Result_Typ))
9315 and then Esize (Left_Typ) = Esize (Right_Typ)
9316 and then Esize (Left_Typ) = Esize (Result_Typ);
9317 end Target_Has_Fixed_Ops;
9319 ------------------------------------------
9320 -- Type_May_Have_Bit_Aligned_Components --
9321 ------------------------------------------
9323 function Type_May_Have_Bit_Aligned_Components
9324 (Typ : Entity_Id) return Boolean
9326 begin
9327 -- Array type, check component type
9329 if Is_Array_Type (Typ) then
9330 return
9331 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
9333 -- Record type, check components
9335 elsif Is_Record_Type (Typ) then
9336 declare
9337 E : Entity_Id;
9339 begin
9340 E := First_Component_Or_Discriminant (Typ);
9341 while Present (E) loop
9342 if Component_May_Be_Bit_Aligned (E)
9343 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
9344 then
9345 return True;
9346 end if;
9348 Next_Component_Or_Discriminant (E);
9349 end loop;
9351 return False;
9352 end;
9354 -- Type other than array or record is always OK
9356 else
9357 return False;
9358 end if;
9359 end Type_May_Have_Bit_Aligned_Components;
9361 ----------------------------------
9362 -- Within_Case_Or_If_Expression --
9363 ----------------------------------
9365 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
9366 Par : Node_Id;
9368 begin
9369 -- Locate an enclosing case or if expression. Note that these constructs
9370 -- can be expanded into Expression_With_Actions, hence the test of the
9371 -- original node.
9373 Par := Parent (N);
9374 while Present (Par) loop
9375 if Nkind_In (Original_Node (Par), N_Case_Expression,
9376 N_If_Expression)
9377 then
9378 return True;
9380 -- Prevent the search from going too far
9382 elsif Is_Body_Or_Package_Declaration (Par) then
9383 return False;
9384 end if;
9386 Par := Parent (Par);
9387 end loop;
9389 return False;
9390 end Within_Case_Or_If_Expression;
9392 --------------------------------
9393 -- Within_Internal_Subprogram --
9394 --------------------------------
9396 function Within_Internal_Subprogram return Boolean is
9397 S : Entity_Id;
9399 begin
9400 S := Current_Scope;
9401 while Present (S) and then not Is_Subprogram (S) loop
9402 S := Scope (S);
9403 end loop;
9405 return Present (S)
9406 and then Get_TSS_Name (S) /= TSS_Null
9407 and then not Is_Predicate_Function (S);
9408 end Within_Internal_Subprogram;
9410 ----------------------------
9411 -- Wrap_Cleanup_Procedure --
9412 ----------------------------
9414 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
9415 Loc : constant Source_Ptr := Sloc (N);
9416 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
9417 Stmts : constant List_Id := Statements (Stseq);
9418 begin
9419 if Abort_Allowed then
9420 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
9421 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
9422 end if;
9423 end Wrap_Cleanup_Procedure;
9425 end Exp_Util;