PR ipa/65130
[official-gcc.git] / gcc / ada / exp_util.adb
bloba565e7f023be57cf867aa3d900320651b3311413
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 Inline; use Inline;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Res; use Sem_Res;
50 with Sem_Type; use Sem_Type;
51 with Sem_Util; use Sem_Util;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Stringt; use Stringt;
55 with Targparm; use Targparm;
56 with Tbuild; use Tbuild;
57 with Ttypes; use Ttypes;
58 with Urealp; use Urealp;
59 with Validsw; use Validsw;
61 package body Exp_Util is
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Build_Task_Array_Image
68 (Loc : Source_Ptr;
69 Id_Ref : Node_Id;
70 A_Type : Entity_Id;
71 Dyn : Boolean := False) return Node_Id;
72 -- Build function to generate the image string for a task that is an array
73 -- component, concatenating the images of each index. To avoid storage
74 -- leaks, the string is built with successive slice assignments. The flag
75 -- Dyn indicates whether this is called for the initialization procedure of
76 -- an array of tasks, or for the name of a dynamically created task that is
77 -- assigned to an indexed component.
79 function Build_Task_Image_Function
80 (Loc : Source_Ptr;
81 Decls : List_Id;
82 Stats : List_Id;
83 Res : Entity_Id) return Node_Id;
84 -- Common processing for Task_Array_Image and Task_Record_Image. Build
85 -- function body that computes image.
87 procedure Build_Task_Image_Prefix
88 (Loc : Source_Ptr;
89 Len : out Entity_Id;
90 Res : out Entity_Id;
91 Pos : out Entity_Id;
92 Prefix : Entity_Id;
93 Sum : Node_Id;
94 Decls : List_Id;
95 Stats : List_Id);
96 -- Common processing for Task_Array_Image and Task_Record_Image. Create
97 -- local variables and assign prefix of name to result string.
99 function Build_Task_Record_Image
100 (Loc : Source_Ptr;
101 Id_Ref : Node_Id;
102 Dyn : Boolean := False) return Node_Id;
103 -- Build function to generate the image string for a task that is a record
104 -- component. Concatenate name of variable with that of selector. The flag
105 -- Dyn indicates whether this is called for the initialization procedure of
106 -- record with task components, or for a dynamically created task that is
107 -- assigned to a selected component.
109 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
110 -- Force evaluation of bounds of a slice, which may be given by a range
111 -- or by a subtype indication with or without a constraint.
113 function Make_CW_Equivalent_Type
114 (T : Entity_Id;
115 E : Node_Id) return Entity_Id;
116 -- T is a class-wide type entity, E is the initial expression node that
117 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
118 -- returns the entity of the Equivalent type and inserts on the fly the
119 -- necessary declaration such as:
121 -- type anon is record
122 -- _parent : Root_Type (T); constrained with E discriminants (if any)
123 -- Extension : String (1 .. expr to match size of E);
124 -- end record;
126 -- This record is compatible with any object of the class of T thanks to
127 -- the first field and has the same size as E thanks to the second.
129 function Make_Literal_Range
130 (Loc : Source_Ptr;
131 Literal_Typ : Entity_Id) return Node_Id;
132 -- Produce a Range node whose bounds are:
133 -- Low_Bound (Literal_Type) ..
134 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
135 -- this is used for expanding declarations like X : String := "sdfgdfg";
137 -- If the index type of the target array is not integer, we generate:
138 -- Low_Bound (Literal_Type) ..
139 -- Literal_Type'Val
140 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
141 -- + (Length (Literal_Typ) -1))
143 function Make_Non_Empty_Check
144 (Loc : Source_Ptr;
145 N : Node_Id) return Node_Id;
146 -- Produce a boolean expression checking that the unidimensional array
147 -- node N is not empty.
149 function New_Class_Wide_Subtype
150 (CW_Typ : Entity_Id;
151 N : Node_Id) return Entity_Id;
152 -- Create an implicit subtype of CW_Typ attached to node N
154 function Requires_Cleanup_Actions
155 (L : List_Id;
156 Lib_Level : Boolean;
157 Nested_Constructs : Boolean) return Boolean;
158 -- Given a list L, determine whether it contains one of the following:
160 -- 1) controlled objects
161 -- 2) library-level tagged types
163 -- Lib_Level is True when the list comes from a construct at the library
164 -- level, and False otherwise. Nested_Constructs is True when any nested
165 -- packages declared in L must be processed, and False otherwise.
167 -------------------------------------
168 -- Activate_Atomic_Synchronization --
169 -------------------------------------
171 procedure Activate_Atomic_Synchronization (N : Node_Id) is
172 Msg_Node : Node_Id;
174 begin
175 case Nkind (Parent (N)) is
177 -- Check for cases of appearing in the prefix of a construct where
178 -- we don't need atomic synchronization for this kind of usage.
180 when
181 -- Nothing to do if we are the prefix of an attribute, since we
182 -- do not want an atomic sync operation for things like 'Size.
184 N_Attribute_Reference |
186 -- The N_Reference node is like an attribute
188 N_Reference |
190 -- Nothing to do for a reference to a component (or components)
191 -- of a composite object. Only reads and updates of the object
192 -- as a whole require atomic synchronization (RM C.6 (15)).
194 N_Indexed_Component |
195 N_Selected_Component |
196 N_Slice =>
198 -- For all the above cases, nothing to do if we are the prefix
200 if Prefix (Parent (N)) = N then
201 return;
202 end if;
204 when others => null;
205 end case;
207 -- Go ahead and set the flag
209 Set_Atomic_Sync_Required (N);
211 -- Generate info message if requested
213 if Warn_On_Atomic_Synchronization then
214 case Nkind (N) is
215 when N_Identifier =>
216 Msg_Node := N;
218 when N_Selected_Component | N_Expanded_Name =>
219 Msg_Node := Selector_Name (N);
221 when N_Explicit_Dereference | N_Indexed_Component =>
222 Msg_Node := Empty;
224 when others =>
225 pragma Assert (False);
226 return;
227 end case;
229 if Present (Msg_Node) then
230 Error_Msg_N
231 ("info: atomic synchronization set for &?N?", Msg_Node);
232 else
233 Error_Msg_N
234 ("info: atomic synchronization set?N?", N);
235 end if;
236 end if;
237 end Activate_Atomic_Synchronization;
239 ----------------------
240 -- Adjust_Condition --
241 ----------------------
243 procedure Adjust_Condition (N : Node_Id) is
244 begin
245 if No (N) then
246 return;
247 end if;
249 declare
250 Loc : constant Source_Ptr := Sloc (N);
251 T : constant Entity_Id := Etype (N);
252 Ti : Entity_Id;
254 begin
255 -- Defend against a call where the argument has no type, or has a
256 -- type that is not Boolean. This can occur because of prior errors.
258 if No (T) or else not Is_Boolean_Type (T) then
259 return;
260 end if;
262 -- Apply validity checking if needed
264 if Validity_Checks_On and Validity_Check_Tests then
265 Ensure_Valid (N);
266 end if;
268 -- Immediate return if standard boolean, the most common case,
269 -- where nothing needs to be done.
271 if Base_Type (T) = Standard_Boolean then
272 return;
273 end if;
275 -- Case of zero/non-zero semantics or non-standard enumeration
276 -- representation. In each case, we rewrite the node as:
278 -- ityp!(N) /= False'Enum_Rep
280 -- where ityp is an integer type with large enough size to hold any
281 -- value of type T.
283 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
284 if Esize (T) <= Esize (Standard_Integer) then
285 Ti := Standard_Integer;
286 else
287 Ti := Standard_Long_Long_Integer;
288 end if;
290 Rewrite (N,
291 Make_Op_Ne (Loc,
292 Left_Opnd => Unchecked_Convert_To (Ti, N),
293 Right_Opnd =>
294 Make_Attribute_Reference (Loc,
295 Attribute_Name => Name_Enum_Rep,
296 Prefix =>
297 New_Occurrence_Of (First_Literal (T), Loc))));
298 Analyze_And_Resolve (N, Standard_Boolean);
300 else
301 Rewrite (N, Convert_To (Standard_Boolean, N));
302 Analyze_And_Resolve (N, Standard_Boolean);
303 end if;
304 end;
305 end Adjust_Condition;
307 ------------------------
308 -- Adjust_Result_Type --
309 ------------------------
311 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
312 begin
313 -- Ignore call if current type is not Standard.Boolean
315 if Etype (N) /= Standard_Boolean then
316 return;
317 end if;
319 -- If result is already of correct type, nothing to do. Note that
320 -- this will get the most common case where everything has a type
321 -- of Standard.Boolean.
323 if Base_Type (T) = Standard_Boolean then
324 return;
326 else
327 declare
328 KP : constant Node_Kind := Nkind (Parent (N));
330 begin
331 -- If result is to be used as a Condition in the syntax, no need
332 -- to convert it back, since if it was changed to Standard.Boolean
333 -- using Adjust_Condition, that is just fine for this usage.
335 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
336 return;
338 -- If result is an operand of another logical operation, no need
339 -- to reset its type, since Standard.Boolean is just fine, and
340 -- such operations always do Adjust_Condition on their operands.
342 elsif KP in N_Op_Boolean
343 or else KP in N_Short_Circuit
344 or else KP = N_Op_Not
345 then
346 return;
348 -- Otherwise we perform a conversion from the current type, which
349 -- must be Standard.Boolean, to the desired type.
351 else
352 Set_Analyzed (N);
353 Rewrite (N, Convert_To (T, N));
354 Analyze_And_Resolve (N, T);
355 end if;
356 end;
357 end if;
358 end Adjust_Result_Type;
360 --------------------------
361 -- Append_Freeze_Action --
362 --------------------------
364 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
365 Fnode : Node_Id;
367 begin
368 Ensure_Freeze_Node (T);
369 Fnode := Freeze_Node (T);
371 if No (Actions (Fnode)) then
372 Set_Actions (Fnode, New_List (N));
373 else
374 Append (N, Actions (Fnode));
375 end if;
377 end Append_Freeze_Action;
379 ---------------------------
380 -- Append_Freeze_Actions --
381 ---------------------------
383 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
384 Fnode : Node_Id;
386 begin
387 if No (L) then
388 return;
389 end if;
391 Ensure_Freeze_Node (T);
392 Fnode := Freeze_Node (T);
394 if No (Actions (Fnode)) then
395 Set_Actions (Fnode, L);
396 else
397 Append_List (L, Actions (Fnode));
398 end if;
399 end Append_Freeze_Actions;
401 ------------------------------------
402 -- Build_Allocate_Deallocate_Proc --
403 ------------------------------------
405 procedure Build_Allocate_Deallocate_Proc
406 (N : Node_Id;
407 Is_Allocate : Boolean)
409 Desig_Typ : Entity_Id;
410 Expr : Node_Id;
411 Pool_Id : Entity_Id;
412 Proc_To_Call : Node_Id := Empty;
413 Ptr_Typ : Entity_Id;
415 function Find_Object (E : Node_Id) return Node_Id;
416 -- Given an arbitrary expression of an allocator, try to find an object
417 -- reference in it, otherwise return the original expression.
419 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
420 -- Determine whether subprogram Subp denotes a custom allocate or
421 -- deallocate.
423 -----------------
424 -- Find_Object --
425 -----------------
427 function Find_Object (E : Node_Id) return Node_Id is
428 Expr : Node_Id;
430 begin
431 pragma Assert (Is_Allocate);
433 Expr := E;
434 loop
435 if Nkind (Expr) = N_Explicit_Dereference then
436 Expr := Prefix (Expr);
438 elsif Nkind (Expr) = N_Qualified_Expression then
439 Expr := Expression (Expr);
441 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
443 -- When interface class-wide types are involved in allocation,
444 -- the expander introduces several levels of address arithmetic
445 -- to perform dispatch table displacement. In this scenario the
446 -- object appears as:
448 -- Tag_Ptr (Base_Address (<object>'Address))
450 -- Detect this case and utilize the whole expression as the
451 -- "object" since it now points to the proper dispatch table.
453 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
454 exit;
456 -- Continue to strip the object
458 else
459 Expr := Expression (Expr);
460 end if;
462 else
463 exit;
464 end if;
465 end loop;
467 return Expr;
468 end Find_Object;
470 ---------------------------------
471 -- Is_Allocate_Deallocate_Proc --
472 ---------------------------------
474 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
475 begin
476 -- Look for a subprogram body with only one statement which is a
477 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
479 if Ekind (Subp) = E_Procedure
480 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
481 then
482 declare
483 HSS : constant Node_Id :=
484 Handled_Statement_Sequence (Parent (Parent (Subp)));
485 Proc : Entity_Id;
487 begin
488 if Present (Statements (HSS))
489 and then Nkind (First (Statements (HSS))) =
490 N_Procedure_Call_Statement
491 then
492 Proc := Entity (Name (First (Statements (HSS))));
494 return
495 Is_RTE (Proc, RE_Allocate_Any_Controlled)
496 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
497 end if;
498 end;
499 end if;
501 return False;
502 end Is_Allocate_Deallocate_Proc;
504 -- Start of processing for Build_Allocate_Deallocate_Proc
506 begin
507 -- Obtain the attributes of the allocation / deallocation
509 if Nkind (N) = N_Free_Statement then
510 Expr := Expression (N);
511 Ptr_Typ := Base_Type (Etype (Expr));
512 Proc_To_Call := Procedure_To_Call (N);
514 else
515 if Nkind (N) = N_Object_Declaration then
516 Expr := Expression (N);
517 else
518 Expr := N;
519 end if;
521 -- In certain cases an allocator with a qualified expression may
522 -- be relocated and used as the initialization expression of a
523 -- temporary:
525 -- before:
526 -- Obj : Ptr_Typ := new Desig_Typ'(...);
528 -- after:
529 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
530 -- Obj : Ptr_Typ := Tmp;
532 -- Since the allocator is always marked as analyzed to avoid infinite
533 -- expansion, it will never be processed by this routine given that
534 -- the designated type needs finalization actions. Detect this case
535 -- and complete the expansion of the allocator.
537 if Nkind (Expr) = N_Identifier
538 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
539 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
540 then
541 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
542 return;
543 end if;
545 -- The allocator may have been rewritten into something else in which
546 -- case the expansion performed by this routine does not apply.
548 if Nkind (Expr) /= N_Allocator then
549 return;
550 end if;
552 Ptr_Typ := Base_Type (Etype (Expr));
553 Proc_To_Call := Procedure_To_Call (Expr);
554 end if;
556 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
557 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
559 -- Handle concurrent types
561 if Is_Concurrent_Type (Desig_Typ)
562 and then Present (Corresponding_Record_Type (Desig_Typ))
563 then
564 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
565 end if;
567 -- Do not process allocations / deallocations without a pool
569 if No (Pool_Id) then
570 return;
572 -- Do not process allocations on / deallocations from the secondary
573 -- stack.
575 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
576 return;
578 -- Do not replicate the machinery if the allocator / free has already
579 -- been expanded and has a custom Allocate / Deallocate.
581 elsif Present (Proc_To_Call)
582 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
583 then
584 return;
585 end if;
587 if Needs_Finalization (Desig_Typ) then
589 -- Certain run-time configurations and targets do not provide support
590 -- for controlled types.
592 if Restriction_Active (No_Finalization) then
593 return;
595 -- Do nothing if the access type may never allocate / deallocate
596 -- objects.
598 elsif No_Pool_Assigned (Ptr_Typ) then
599 return;
601 -- Access-to-controlled types are not supported on .NET/JVM since
602 -- these targets cannot support pools and address arithmetic.
604 elsif VM_Target /= No_VM then
605 return;
606 end if;
608 -- The allocation / deallocation of a controlled object must be
609 -- chained on / detached from a finalization master.
611 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
613 -- The only other kind of allocation / deallocation supported by this
614 -- routine is on / from a subpool.
616 elsif Nkind (Expr) = N_Allocator
617 and then No (Subpool_Handle_Name (Expr))
618 then
619 return;
620 end if;
622 declare
623 Loc : constant Source_Ptr := Sloc (N);
624 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
625 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
626 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
627 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
629 Actuals : List_Id;
630 Fin_Addr_Id : Entity_Id;
631 Fin_Mas_Act : Node_Id;
632 Fin_Mas_Id : Entity_Id;
633 Proc_To_Call : Entity_Id;
634 Subpool : Node_Id := Empty;
636 begin
637 -- Step 1: Construct all the actuals for the call to library routine
638 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
640 -- a) Storage pool
642 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
644 if Is_Allocate then
646 -- b) Subpool
648 if Nkind (Expr) = N_Allocator then
649 Subpool := Subpool_Handle_Name (Expr);
650 end if;
652 -- If a subpool is present it can be an arbitrary name, so make
653 -- the actual by copying the tree.
655 if Present (Subpool) then
656 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
657 else
658 Append_To (Actuals, Make_Null (Loc));
659 end if;
661 -- c) Finalization master
663 if Needs_Finalization (Desig_Typ) then
664 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
665 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
667 -- Handle the case where the master is actually a pointer to a
668 -- master. This case arises in build-in-place functions.
670 if Is_Access_Type (Etype (Fin_Mas_Id)) then
671 Append_To (Actuals, Fin_Mas_Act);
672 else
673 Append_To (Actuals,
674 Make_Attribute_Reference (Loc,
675 Prefix => Fin_Mas_Act,
676 Attribute_Name => Name_Unrestricted_Access));
677 end if;
678 else
679 Append_To (Actuals, Make_Null (Loc));
680 end if;
682 -- d) Finalize_Address
684 -- Primitive Finalize_Address is never generated in CodePeer mode
685 -- since it contains an Unchecked_Conversion.
687 if Needs_Finalization (Desig_Typ) and then not CodePeer_Mode then
688 Fin_Addr_Id := Finalize_Address (Desig_Typ);
689 pragma Assert (Present (Fin_Addr_Id));
691 Append_To (Actuals,
692 Make_Attribute_Reference (Loc,
693 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
694 Attribute_Name => Name_Unrestricted_Access));
695 else
696 Append_To (Actuals, Make_Null (Loc));
697 end if;
698 end if;
700 -- e) Address
701 -- f) Storage_Size
702 -- g) Alignment
704 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
705 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
707 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
708 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
710 -- For deallocation of class-wide types we obtain the value of
711 -- alignment from the Type Specific Record of the deallocated object.
712 -- This is needed because the frontend expansion of class-wide types
713 -- into equivalent types confuses the backend.
715 else
716 -- Generate:
717 -- Obj.all'Alignment
719 -- ... because 'Alignment applied to class-wide types is expanded
720 -- into the code that reads the value of alignment from the TSD
721 -- (see Expand_N_Attribute_Reference)
723 Append_To (Actuals,
724 Unchecked_Convert_To (RTE (RE_Storage_Offset),
725 Make_Attribute_Reference (Loc,
726 Prefix =>
727 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
728 Attribute_Name => Name_Alignment)));
729 end if;
731 -- h) Is_Controlled
733 if Needs_Finalization (Desig_Typ) then
734 declare
735 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
736 Flag_Expr : Node_Id;
737 Param : Node_Id;
738 Temp : Node_Id;
740 begin
741 if Is_Allocate then
742 Temp := Find_Object (Expression (Expr));
743 else
744 Temp := Expr;
745 end if;
747 -- Processing for allocations where the expression is a subtype
748 -- indication.
750 if Is_Allocate
751 and then Is_Entity_Name (Temp)
752 and then Is_Type (Entity (Temp))
753 then
754 Flag_Expr :=
755 New_Occurrence_Of
756 (Boolean_Literals
757 (Needs_Finalization (Entity (Temp))), Loc);
759 -- The allocation / deallocation of a class-wide object relies
760 -- on a runtime check to determine whether the object is truly
761 -- controlled or not. Depending on this check, the finalization
762 -- machinery will request or reclaim extra storage reserved for
763 -- a list header.
765 elsif Is_Class_Wide_Type (Desig_Typ) then
767 -- Detect a special case where interface class-wide types
768 -- are involved as the object appears as:
770 -- Tag_Ptr (Base_Address (<object>'Address))
772 -- The expression already yields the proper tag, generate:
774 -- Temp.all
776 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
777 Param :=
778 Make_Explicit_Dereference (Loc,
779 Prefix => Relocate_Node (Temp));
781 -- In the default case, obtain the tag of the object about
782 -- to be allocated / deallocated. Generate:
784 -- Temp'Tag
786 else
787 Param :=
788 Make_Attribute_Reference (Loc,
789 Prefix => Relocate_Node (Temp),
790 Attribute_Name => Name_Tag);
791 end if;
793 -- Generate:
794 -- Needs_Finalization (<Param>)
796 Flag_Expr :=
797 Make_Function_Call (Loc,
798 Name =>
799 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
800 Parameter_Associations => New_List (Param));
802 -- Processing for generic actuals
804 elsif Is_Generic_Actual_Type (Desig_Typ) then
805 Flag_Expr :=
806 New_Occurrence_Of (Boolean_Literals
807 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
809 -- The object does not require any specialized checks, it is
810 -- known to be controlled.
812 else
813 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
814 end if;
816 -- Create the temporary which represents the finalization state
817 -- of the expression. Generate:
819 -- F : constant Boolean := <Flag_Expr>;
821 Insert_Action (N,
822 Make_Object_Declaration (Loc,
823 Defining_Identifier => Flag_Id,
824 Constant_Present => True,
825 Object_Definition =>
826 New_Occurrence_Of (Standard_Boolean, Loc),
827 Expression => Flag_Expr));
829 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
830 end;
832 -- The object is not controlled
834 else
835 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
836 end if;
838 -- i) On_Subpool
840 if Is_Allocate then
841 Append_To (Actuals,
842 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
843 end if;
845 -- Step 2: Build a wrapper Allocate / Deallocate which internally
846 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
848 -- Select the proper routine to call
850 if Is_Allocate then
851 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
852 else
853 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
854 end if;
856 -- Create a custom Allocate / Deallocate routine which has identical
857 -- profile to that of System.Storage_Pools.
859 Insert_Action (N,
860 Make_Subprogram_Body (Loc,
861 Specification =>
863 -- procedure Pnn
865 Make_Procedure_Specification (Loc,
866 Defining_Unit_Name => Proc_Id,
867 Parameter_Specifications => New_List (
869 -- P : Root_Storage_Pool
871 Make_Parameter_Specification (Loc,
872 Defining_Identifier => Make_Temporary (Loc, 'P'),
873 Parameter_Type =>
874 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
876 -- A : [out] Address
878 Make_Parameter_Specification (Loc,
879 Defining_Identifier => Addr_Id,
880 Out_Present => Is_Allocate,
881 Parameter_Type =>
882 New_Occurrence_Of (RTE (RE_Address), Loc)),
884 -- S : Storage_Count
886 Make_Parameter_Specification (Loc,
887 Defining_Identifier => Size_Id,
888 Parameter_Type =>
889 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
891 -- L : Storage_Count
893 Make_Parameter_Specification (Loc,
894 Defining_Identifier => Alig_Id,
895 Parameter_Type =>
896 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
898 Declarations => No_List,
900 Handled_Statement_Sequence =>
901 Make_Handled_Sequence_Of_Statements (Loc,
902 Statements => New_List (
903 Make_Procedure_Call_Statement (Loc,
904 Name => New_Occurrence_Of (Proc_To_Call, Loc),
905 Parameter_Associations => Actuals)))));
907 -- The newly generated Allocate / Deallocate becomes the default
908 -- procedure to call when the back end processes the allocation /
909 -- deallocation.
911 if Is_Allocate then
912 Set_Procedure_To_Call (Expr, Proc_Id);
913 else
914 Set_Procedure_To_Call (N, Proc_Id);
915 end if;
916 end;
917 end Build_Allocate_Deallocate_Proc;
919 ------------------------
920 -- Build_Runtime_Call --
921 ------------------------
923 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
924 begin
925 -- If entity is not available, we can skip making the call (this avoids
926 -- junk duplicated error messages in a number of cases).
928 if not RTE_Available (RE) then
929 return Make_Null_Statement (Loc);
930 else
931 return
932 Make_Procedure_Call_Statement (Loc,
933 Name => New_Occurrence_Of (RTE (RE), Loc));
934 end if;
935 end Build_Runtime_Call;
937 ------------------------
938 -- Build_SS_Mark_Call --
939 ------------------------
941 function Build_SS_Mark_Call
942 (Loc : Source_Ptr;
943 Mark : Entity_Id) return Node_Id
945 begin
946 -- Generate:
947 -- Mark : constant Mark_Id := SS_Mark;
949 return
950 Make_Object_Declaration (Loc,
951 Defining_Identifier => Mark,
952 Constant_Present => True,
953 Object_Definition =>
954 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
955 Expression =>
956 Make_Function_Call (Loc,
957 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
958 end Build_SS_Mark_Call;
960 ---------------------------
961 -- Build_SS_Release_Call --
962 ---------------------------
964 function Build_SS_Release_Call
965 (Loc : Source_Ptr;
966 Mark : Entity_Id) return Node_Id
968 begin
969 -- Generate:
970 -- SS_Release (Mark);
972 return
973 Make_Procedure_Call_Statement (Loc,
974 Name =>
975 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
976 Parameter_Associations => New_List (
977 New_Occurrence_Of (Mark, Loc)));
978 end Build_SS_Release_Call;
980 ----------------------------
981 -- Build_Task_Array_Image --
982 ----------------------------
984 -- This function generates the body for a function that constructs the
985 -- image string for a task that is an array component. The function is
986 -- local to the init proc for the array type, and is called for each one
987 -- of the components. The constructed image has the form of an indexed
988 -- component, whose prefix is the outer variable of the array type.
989 -- The n-dimensional array type has known indexes Index, Index2...
991 -- Id_Ref is an indexed component form created by the enclosing init proc.
992 -- Its successive indexes are Val1, Val2, ... which are the loop variables
993 -- in the loops that call the individual task init proc on each component.
995 -- The generated function has the following structure:
997 -- function F return String is
998 -- Pref : string renames Task_Name;
999 -- T1 : String := Index1'Image (Val1);
1000 -- ...
1001 -- Tn : String := indexn'image (Valn);
1002 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
1003 -- -- Len includes commas and the end parentheses.
1004 -- Res : String (1..Len);
1005 -- Pos : Integer := Pref'Length;
1007 -- begin
1008 -- Res (1 .. Pos) := Pref;
1009 -- Pos := Pos + 1;
1010 -- Res (Pos) := '(';
1011 -- Pos := Pos + 1;
1012 -- Res (Pos .. Pos + T1'Length - 1) := T1;
1013 -- Pos := Pos + T1'Length;
1014 -- Res (Pos) := '.';
1015 -- Pos := Pos + 1;
1016 -- ...
1017 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
1018 -- Res (Len) := ')';
1020 -- return Res;
1021 -- end F;
1023 -- Needless to say, multidimensional arrays of tasks are rare enough that
1024 -- the bulkiness of this code is not really a concern.
1026 function Build_Task_Array_Image
1027 (Loc : Source_Ptr;
1028 Id_Ref : Node_Id;
1029 A_Type : Entity_Id;
1030 Dyn : Boolean := False) return Node_Id
1032 Dims : constant Nat := Number_Dimensions (A_Type);
1033 -- Number of dimensions for array of tasks
1035 Temps : array (1 .. Dims) of Entity_Id;
1036 -- Array of temporaries to hold string for each index
1038 Indx : Node_Id;
1039 -- Index expression
1041 Len : Entity_Id;
1042 -- Total length of generated name
1044 Pos : Entity_Id;
1045 -- Running index for substring assignments
1047 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1048 -- Name of enclosing variable, prefix of resulting name
1050 Res : Entity_Id;
1051 -- String to hold result
1053 Val : Node_Id;
1054 -- Value of successive indexes
1056 Sum : Node_Id;
1057 -- Expression to compute total size of string
1059 T : Entity_Id;
1060 -- Entity for name at one index position
1062 Decls : constant List_Id := New_List;
1063 Stats : constant List_Id := New_List;
1065 begin
1066 -- For a dynamic task, the name comes from the target variable. For a
1067 -- static one it is a formal of the enclosing init proc.
1069 if Dyn then
1070 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1071 Append_To (Decls,
1072 Make_Object_Declaration (Loc,
1073 Defining_Identifier => Pref,
1074 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1075 Expression =>
1076 Make_String_Literal (Loc,
1077 Strval => String_From_Name_Buffer)));
1079 else
1080 Append_To (Decls,
1081 Make_Object_Renaming_Declaration (Loc,
1082 Defining_Identifier => Pref,
1083 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1084 Name => Make_Identifier (Loc, Name_uTask_Name)));
1085 end if;
1087 Indx := First_Index (A_Type);
1088 Val := First (Expressions (Id_Ref));
1090 for J in 1 .. Dims loop
1091 T := Make_Temporary (Loc, 'T');
1092 Temps (J) := T;
1094 Append_To (Decls,
1095 Make_Object_Declaration (Loc,
1096 Defining_Identifier => T,
1097 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1098 Expression =>
1099 Make_Attribute_Reference (Loc,
1100 Attribute_Name => Name_Image,
1101 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
1102 Expressions => New_List (New_Copy_Tree (Val)))));
1104 Next_Index (Indx);
1105 Next (Val);
1106 end loop;
1108 Sum := Make_Integer_Literal (Loc, Dims + 1);
1110 Sum :=
1111 Make_Op_Add (Loc,
1112 Left_Opnd => Sum,
1113 Right_Opnd =>
1114 Make_Attribute_Reference (Loc,
1115 Attribute_Name => Name_Length,
1116 Prefix => New_Occurrence_Of (Pref, Loc),
1117 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1119 for J in 1 .. Dims loop
1120 Sum :=
1121 Make_Op_Add (Loc,
1122 Left_Opnd => Sum,
1123 Right_Opnd =>
1124 Make_Attribute_Reference (Loc,
1125 Attribute_Name => Name_Length,
1126 Prefix =>
1127 New_Occurrence_Of (Temps (J), Loc),
1128 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1129 end loop;
1131 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1133 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
1135 Append_To (Stats,
1136 Make_Assignment_Statement (Loc,
1137 Name =>
1138 Make_Indexed_Component (Loc,
1139 Prefix => New_Occurrence_Of (Res, Loc),
1140 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1141 Expression =>
1142 Make_Character_Literal (Loc,
1143 Chars => Name_Find,
1144 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
1146 Append_To (Stats,
1147 Make_Assignment_Statement (Loc,
1148 Name => New_Occurrence_Of (Pos, Loc),
1149 Expression =>
1150 Make_Op_Add (Loc,
1151 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1152 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1154 for J in 1 .. Dims loop
1156 Append_To (Stats,
1157 Make_Assignment_Statement (Loc,
1158 Name =>
1159 Make_Slice (Loc,
1160 Prefix => New_Occurrence_Of (Res, Loc),
1161 Discrete_Range =>
1162 Make_Range (Loc,
1163 Low_Bound => New_Occurrence_Of (Pos, Loc),
1164 High_Bound =>
1165 Make_Op_Subtract (Loc,
1166 Left_Opnd =>
1167 Make_Op_Add (Loc,
1168 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1169 Right_Opnd =>
1170 Make_Attribute_Reference (Loc,
1171 Attribute_Name => Name_Length,
1172 Prefix =>
1173 New_Occurrence_Of (Temps (J), Loc),
1174 Expressions =>
1175 New_List (Make_Integer_Literal (Loc, 1)))),
1176 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
1178 Expression => New_Occurrence_Of (Temps (J), Loc)));
1180 if J < Dims then
1181 Append_To (Stats,
1182 Make_Assignment_Statement (Loc,
1183 Name => New_Occurrence_Of (Pos, Loc),
1184 Expression =>
1185 Make_Op_Add (Loc,
1186 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1187 Right_Opnd =>
1188 Make_Attribute_Reference (Loc,
1189 Attribute_Name => Name_Length,
1190 Prefix => New_Occurrence_Of (Temps (J), Loc),
1191 Expressions =>
1192 New_List (Make_Integer_Literal (Loc, 1))))));
1194 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
1196 Append_To (Stats,
1197 Make_Assignment_Statement (Loc,
1198 Name => Make_Indexed_Component (Loc,
1199 Prefix => New_Occurrence_Of (Res, Loc),
1200 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1201 Expression =>
1202 Make_Character_Literal (Loc,
1203 Chars => Name_Find,
1204 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
1206 Append_To (Stats,
1207 Make_Assignment_Statement (Loc,
1208 Name => New_Occurrence_Of (Pos, Loc),
1209 Expression =>
1210 Make_Op_Add (Loc,
1211 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1212 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1213 end if;
1214 end loop;
1216 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
1218 Append_To (Stats,
1219 Make_Assignment_Statement (Loc,
1220 Name =>
1221 Make_Indexed_Component (Loc,
1222 Prefix => New_Occurrence_Of (Res, Loc),
1223 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
1224 Expression =>
1225 Make_Character_Literal (Loc,
1226 Chars => Name_Find,
1227 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
1228 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1229 end Build_Task_Array_Image;
1231 ----------------------------
1232 -- Build_Task_Image_Decls --
1233 ----------------------------
1235 function Build_Task_Image_Decls
1236 (Loc : Source_Ptr;
1237 Id_Ref : Node_Id;
1238 A_Type : Entity_Id;
1239 In_Init_Proc : Boolean := False) return List_Id
1241 Decls : constant List_Id := New_List;
1242 T_Id : Entity_Id := Empty;
1243 Decl : Node_Id;
1244 Expr : Node_Id := Empty;
1245 Fun : Node_Id := Empty;
1246 Is_Dyn : constant Boolean :=
1247 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
1248 and then
1249 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
1251 begin
1252 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
1253 -- generate a dummy declaration only.
1255 if Restriction_Active (No_Implicit_Heap_Allocations)
1256 or else Global_Discard_Names
1257 then
1258 T_Id := Make_Temporary (Loc, 'J');
1259 Name_Len := 0;
1261 return
1262 New_List (
1263 Make_Object_Declaration (Loc,
1264 Defining_Identifier => T_Id,
1265 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1266 Expression =>
1267 Make_String_Literal (Loc,
1268 Strval => String_From_Name_Buffer)));
1270 else
1271 if Nkind (Id_Ref) = N_Identifier
1272 or else Nkind (Id_Ref) = N_Defining_Identifier
1273 then
1274 -- For a simple variable, the image of the task is built from
1275 -- the name of the variable. To avoid possible conflict with the
1276 -- anonymous type created for a single protected object, add a
1277 -- numeric suffix.
1279 T_Id :=
1280 Make_Defining_Identifier (Loc,
1281 New_External_Name (Chars (Id_Ref), 'T', 1));
1283 Get_Name_String (Chars (Id_Ref));
1285 Expr :=
1286 Make_String_Literal (Loc,
1287 Strval => String_From_Name_Buffer);
1289 elsif Nkind (Id_Ref) = N_Selected_Component then
1290 T_Id :=
1291 Make_Defining_Identifier (Loc,
1292 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
1293 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
1295 elsif Nkind (Id_Ref) = N_Indexed_Component then
1296 T_Id :=
1297 Make_Defining_Identifier (Loc,
1298 New_External_Name (Chars (A_Type), 'N'));
1300 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
1301 end if;
1302 end if;
1304 if Present (Fun) then
1305 Append (Fun, Decls);
1306 Expr := Make_Function_Call (Loc,
1307 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
1309 if not In_Init_Proc and then VM_Target = No_VM then
1310 Set_Uses_Sec_Stack (Defining_Entity (Fun));
1311 end if;
1312 end if;
1314 Decl := Make_Object_Declaration (Loc,
1315 Defining_Identifier => T_Id,
1316 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1317 Constant_Present => True,
1318 Expression => Expr);
1320 Append (Decl, Decls);
1321 return Decls;
1322 end Build_Task_Image_Decls;
1324 -------------------------------
1325 -- Build_Task_Image_Function --
1326 -------------------------------
1328 function Build_Task_Image_Function
1329 (Loc : Source_Ptr;
1330 Decls : List_Id;
1331 Stats : List_Id;
1332 Res : Entity_Id) return Node_Id
1334 Spec : Node_Id;
1336 begin
1337 Append_To (Stats,
1338 Make_Simple_Return_Statement (Loc,
1339 Expression => New_Occurrence_Of (Res, Loc)));
1341 Spec := Make_Function_Specification (Loc,
1342 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
1343 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
1345 -- Calls to 'Image use the secondary stack, which must be cleaned up
1346 -- after the task name is built.
1348 return Make_Subprogram_Body (Loc,
1349 Specification => Spec,
1350 Declarations => Decls,
1351 Handled_Statement_Sequence =>
1352 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
1353 end Build_Task_Image_Function;
1355 -----------------------------
1356 -- Build_Task_Image_Prefix --
1357 -----------------------------
1359 procedure Build_Task_Image_Prefix
1360 (Loc : Source_Ptr;
1361 Len : out Entity_Id;
1362 Res : out Entity_Id;
1363 Pos : out Entity_Id;
1364 Prefix : Entity_Id;
1365 Sum : Node_Id;
1366 Decls : List_Id;
1367 Stats : List_Id)
1369 begin
1370 Len := Make_Temporary (Loc, 'L', Sum);
1372 Append_To (Decls,
1373 Make_Object_Declaration (Loc,
1374 Defining_Identifier => Len,
1375 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
1376 Expression => Sum));
1378 Res := Make_Temporary (Loc, 'R');
1380 Append_To (Decls,
1381 Make_Object_Declaration (Loc,
1382 Defining_Identifier => Res,
1383 Object_Definition =>
1384 Make_Subtype_Indication (Loc,
1385 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1386 Constraint =>
1387 Make_Index_Or_Discriminant_Constraint (Loc,
1388 Constraints =>
1389 New_List (
1390 Make_Range (Loc,
1391 Low_Bound => Make_Integer_Literal (Loc, 1),
1392 High_Bound => New_Occurrence_Of (Len, Loc)))))));
1394 -- Indicate that the result is an internal temporary, so it does not
1395 -- receive a bogus initialization when declaration is expanded. This
1396 -- is both efficient, and prevents anomalies in the handling of
1397 -- dynamic objects on the secondary stack.
1399 Set_Is_Internal (Res);
1400 Pos := Make_Temporary (Loc, 'P');
1402 Append_To (Decls,
1403 Make_Object_Declaration (Loc,
1404 Defining_Identifier => Pos,
1405 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
1407 -- Pos := Prefix'Length;
1409 Append_To (Stats,
1410 Make_Assignment_Statement (Loc,
1411 Name => New_Occurrence_Of (Pos, Loc),
1412 Expression =>
1413 Make_Attribute_Reference (Loc,
1414 Attribute_Name => Name_Length,
1415 Prefix => New_Occurrence_Of (Prefix, Loc),
1416 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
1418 -- Res (1 .. Pos) := Prefix;
1420 Append_To (Stats,
1421 Make_Assignment_Statement (Loc,
1422 Name =>
1423 Make_Slice (Loc,
1424 Prefix => New_Occurrence_Of (Res, Loc),
1425 Discrete_Range =>
1426 Make_Range (Loc,
1427 Low_Bound => Make_Integer_Literal (Loc, 1),
1428 High_Bound => New_Occurrence_Of (Pos, Loc))),
1430 Expression => New_Occurrence_Of (Prefix, Loc)));
1432 Append_To (Stats,
1433 Make_Assignment_Statement (Loc,
1434 Name => New_Occurrence_Of (Pos, Loc),
1435 Expression =>
1436 Make_Op_Add (Loc,
1437 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1438 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1439 end Build_Task_Image_Prefix;
1441 -----------------------------
1442 -- Build_Task_Record_Image --
1443 -----------------------------
1445 function Build_Task_Record_Image
1446 (Loc : Source_Ptr;
1447 Id_Ref : Node_Id;
1448 Dyn : Boolean := False) return Node_Id
1450 Len : Entity_Id;
1451 -- Total length of generated name
1453 Pos : Entity_Id;
1454 -- Index into result
1456 Res : Entity_Id;
1457 -- String to hold result
1459 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1460 -- Name of enclosing variable, prefix of resulting name
1462 Sum : Node_Id;
1463 -- Expression to compute total size of string
1465 Sel : Entity_Id;
1466 -- Entity for selector name
1468 Decls : constant List_Id := New_List;
1469 Stats : constant List_Id := New_List;
1471 begin
1472 -- For a dynamic task, the name comes from the target variable. For a
1473 -- static one it is a formal of the enclosing init proc.
1475 if Dyn then
1476 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1477 Append_To (Decls,
1478 Make_Object_Declaration (Loc,
1479 Defining_Identifier => Pref,
1480 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1481 Expression =>
1482 Make_String_Literal (Loc,
1483 Strval => String_From_Name_Buffer)));
1485 else
1486 Append_To (Decls,
1487 Make_Object_Renaming_Declaration (Loc,
1488 Defining_Identifier => Pref,
1489 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1490 Name => Make_Identifier (Loc, Name_uTask_Name)));
1491 end if;
1493 Sel := Make_Temporary (Loc, 'S');
1495 Get_Name_String (Chars (Selector_Name (Id_Ref)));
1497 Append_To (Decls,
1498 Make_Object_Declaration (Loc,
1499 Defining_Identifier => Sel,
1500 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1501 Expression =>
1502 Make_String_Literal (Loc,
1503 Strval => String_From_Name_Buffer)));
1505 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
1507 Sum :=
1508 Make_Op_Add (Loc,
1509 Left_Opnd => Sum,
1510 Right_Opnd =>
1511 Make_Attribute_Reference (Loc,
1512 Attribute_Name => Name_Length,
1513 Prefix =>
1514 New_Occurrence_Of (Pref, Loc),
1515 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1517 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1519 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
1521 -- Res (Pos) := '.';
1523 Append_To (Stats,
1524 Make_Assignment_Statement (Loc,
1525 Name => Make_Indexed_Component (Loc,
1526 Prefix => New_Occurrence_Of (Res, Loc),
1527 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1528 Expression =>
1529 Make_Character_Literal (Loc,
1530 Chars => Name_Find,
1531 Char_Literal_Value =>
1532 UI_From_Int (Character'Pos ('.')))));
1534 Append_To (Stats,
1535 Make_Assignment_Statement (Loc,
1536 Name => New_Occurrence_Of (Pos, Loc),
1537 Expression =>
1538 Make_Op_Add (Loc,
1539 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1540 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1542 -- Res (Pos .. Len) := Selector;
1544 Append_To (Stats,
1545 Make_Assignment_Statement (Loc,
1546 Name => Make_Slice (Loc,
1547 Prefix => New_Occurrence_Of (Res, Loc),
1548 Discrete_Range =>
1549 Make_Range (Loc,
1550 Low_Bound => New_Occurrence_Of (Pos, Loc),
1551 High_Bound => New_Occurrence_Of (Len, Loc))),
1552 Expression => New_Occurrence_Of (Sel, Loc)));
1554 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1555 end Build_Task_Record_Image;
1557 -----------------------------
1558 -- Check_Float_Op_Overflow --
1559 -----------------------------
1561 procedure Check_Float_Op_Overflow (N : Node_Id) is
1562 begin
1563 -- Return if no check needed
1565 if not Is_Floating_Point_Type (Etype (N))
1566 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
1568 -- In CodePeer_Mode, rely on the overflow check flag being set instead
1569 -- and do not expand the code for float overflow checking.
1571 or else CodePeer_Mode
1572 then
1573 return;
1574 end if;
1576 -- Otherwise we replace the expression by
1578 -- do Tnn : constant ftype := expression;
1579 -- constraint_error when not Tnn'Valid;
1580 -- in Tnn;
1582 declare
1583 Loc : constant Source_Ptr := Sloc (N);
1584 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
1585 Typ : constant Entity_Id := Etype (N);
1587 begin
1588 -- Turn off the Do_Overflow_Check flag, since we are doing that work
1589 -- right here. We also set the node as analyzed to prevent infinite
1590 -- recursion from repeating the operation in the expansion.
1592 Set_Do_Overflow_Check (N, False);
1593 Set_Analyzed (N, True);
1595 -- Do the rewrite to include the check
1597 Rewrite (N,
1598 Make_Expression_With_Actions (Loc,
1599 Actions => New_List (
1600 Make_Object_Declaration (Loc,
1601 Defining_Identifier => Tnn,
1602 Object_Definition => New_Occurrence_Of (Typ, Loc),
1603 Constant_Present => True,
1604 Expression => Relocate_Node (N)),
1605 Make_Raise_Constraint_Error (Loc,
1606 Condition =>
1607 Make_Op_Not (Loc,
1608 Right_Opnd =>
1609 Make_Attribute_Reference (Loc,
1610 Prefix => New_Occurrence_Of (Tnn, Loc),
1611 Attribute_Name => Name_Valid)),
1612 Reason => CE_Overflow_Check_Failed)),
1613 Expression => New_Occurrence_Of (Tnn, Loc)));
1615 Analyze_And_Resolve (N, Typ);
1616 end;
1617 end Check_Float_Op_Overflow;
1619 ----------------------------------
1620 -- Component_May_Be_Bit_Aligned --
1621 ----------------------------------
1623 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
1624 UT : Entity_Id;
1626 begin
1627 -- If no component clause, then everything is fine, since the back end
1628 -- never bit-misaligns by default, even if there is a pragma Packed for
1629 -- the record.
1631 if No (Comp) or else No (Component_Clause (Comp)) then
1632 return False;
1633 end if;
1635 UT := Underlying_Type (Etype (Comp));
1637 -- It is only array and record types that cause trouble
1639 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
1640 return False;
1642 -- If we know that we have a small (64 bits or less) record or small
1643 -- bit-packed array, then everything is fine, since the back end can
1644 -- handle these cases correctly.
1646 elsif Esize (Comp) <= 64
1647 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
1648 then
1649 return False;
1651 -- Otherwise if the component is not byte aligned, we know we have the
1652 -- nasty unaligned case.
1654 elsif Normalized_First_Bit (Comp) /= Uint_0
1655 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
1656 then
1657 return True;
1659 -- If we are large and byte aligned, then OK at this level
1661 else
1662 return False;
1663 end if;
1664 end Component_May_Be_Bit_Aligned;
1666 ----------------------------------------
1667 -- Containing_Package_With_Ext_Axioms --
1668 ----------------------------------------
1670 function Containing_Package_With_Ext_Axioms
1671 (E : Entity_Id) return Entity_Id
1673 Decl : Node_Id;
1675 begin
1676 if Ekind (E) = E_Package then
1677 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
1678 Decl := Parent (Parent (E));
1679 else
1680 Decl := Parent (E);
1681 end if;
1682 end if;
1684 -- E is the package or generic package which is externally axiomatized
1686 if Ekind_In (E, E_Package, E_Generic_Package)
1687 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
1688 then
1689 return E;
1690 end if;
1692 -- If E's scope is axiomatized, E is axiomatized.
1694 declare
1695 First_Ax_Parent_Scope : Entity_Id := Empty;
1697 begin
1698 if Present (Scope (E)) then
1699 First_Ax_Parent_Scope :=
1700 Containing_Package_With_Ext_Axioms (Scope (E));
1701 end if;
1703 if Present (First_Ax_Parent_Scope) then
1704 return First_Ax_Parent_Scope;
1705 end if;
1707 -- otherwise, if E is a package instance, it is axiomatized if the
1708 -- corresponding generic package is axiomatized.
1710 if Ekind (E) = E_Package
1711 and then Present (Generic_Parent (Decl))
1712 then
1713 return
1714 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
1715 else
1716 return Empty;
1717 end if;
1718 end;
1719 end Containing_Package_With_Ext_Axioms;
1721 -------------------------------
1722 -- Convert_To_Actual_Subtype --
1723 -------------------------------
1725 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1726 Act_ST : Entity_Id;
1728 begin
1729 Act_ST := Get_Actual_Subtype (Exp);
1731 if Act_ST = Etype (Exp) then
1732 return;
1733 else
1734 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
1735 Analyze_And_Resolve (Exp, Act_ST);
1736 end if;
1737 end Convert_To_Actual_Subtype;
1739 -----------------------------------
1740 -- Corresponding_Runtime_Package --
1741 -----------------------------------
1743 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1744 Pkg_Id : RTU_Id := RTU_Null;
1746 begin
1747 pragma Assert (Is_Concurrent_Type (Typ));
1749 if Ekind (Typ) in Protected_Kind then
1750 if Has_Entries (Typ)
1752 -- A protected type without entries that covers an interface and
1753 -- overrides the abstract routines with protected procedures is
1754 -- considered equivalent to a protected type with entries in the
1755 -- context of dispatching select statements. It is sufficient to
1756 -- check for the presence of an interface list in the declaration
1757 -- node to recognize this case.
1759 or else Present (Interface_List (Parent (Typ)))
1761 -- Protected types with interrupt handlers (when not using a
1762 -- restricted profile) are also considered equivalent to
1763 -- protected types with entries. The types which are used
1764 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
1765 -- are derived from Protection_Entries.
1767 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
1768 or else Has_Interrupt_Handler (Typ)
1769 then
1770 if Abort_Allowed
1771 or else Restriction_Active (No_Entry_Queue) = False
1772 or else Restriction_Active (No_Select_Statements) = False
1773 or else Number_Entries (Typ) > 1
1774 or else (Has_Attach_Handler (Typ)
1775 and then not Restricted_Profile)
1776 then
1777 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1778 else
1779 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1780 end if;
1782 else
1783 Pkg_Id := System_Tasking_Protected_Objects;
1784 end if;
1785 end if;
1787 return Pkg_Id;
1788 end Corresponding_Runtime_Package;
1790 -----------------------------------
1791 -- Current_Sem_Unit_Declarations --
1792 -----------------------------------
1794 function Current_Sem_Unit_Declarations return List_Id is
1795 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1796 Decls : List_Id;
1798 begin
1799 -- If the current unit is a package body, locate the visible
1800 -- declarations of the package spec.
1802 if Nkind (U) = N_Package_Body then
1803 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1804 end if;
1806 if Nkind (U) = N_Package_Declaration then
1807 U := Specification (U);
1808 Decls := Visible_Declarations (U);
1810 if No (Decls) then
1811 Decls := New_List;
1812 Set_Visible_Declarations (U, Decls);
1813 end if;
1815 else
1816 Decls := Declarations (U);
1818 if No (Decls) then
1819 Decls := New_List;
1820 Set_Declarations (U, Decls);
1821 end if;
1822 end if;
1824 return Decls;
1825 end Current_Sem_Unit_Declarations;
1827 -----------------------
1828 -- Duplicate_Subexpr --
1829 -----------------------
1831 function Duplicate_Subexpr
1832 (Exp : Node_Id;
1833 Name_Req : Boolean := False;
1834 Renaming_Req : Boolean := False) return Node_Id
1836 begin
1837 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1838 return New_Copy_Tree (Exp);
1839 end Duplicate_Subexpr;
1841 ---------------------------------
1842 -- Duplicate_Subexpr_No_Checks --
1843 ---------------------------------
1845 function Duplicate_Subexpr_No_Checks
1846 (Exp : Node_Id;
1847 Name_Req : Boolean := False;
1848 Renaming_Req : Boolean := False;
1849 Related_Id : Entity_Id := Empty;
1850 Is_Low_Bound : Boolean := False;
1851 Is_High_Bound : Boolean := False) return Node_Id
1853 New_Exp : Node_Id;
1855 begin
1856 Remove_Side_Effects
1857 (Exp => Exp,
1858 Name_Req => Name_Req,
1859 Renaming_Req => Renaming_Req,
1860 Related_Id => Related_Id,
1861 Is_Low_Bound => Is_Low_Bound,
1862 Is_High_Bound => Is_High_Bound);
1864 New_Exp := New_Copy_Tree (Exp);
1865 Remove_Checks (New_Exp);
1866 return New_Exp;
1867 end Duplicate_Subexpr_No_Checks;
1869 -----------------------------------
1870 -- Duplicate_Subexpr_Move_Checks --
1871 -----------------------------------
1873 function Duplicate_Subexpr_Move_Checks
1874 (Exp : Node_Id;
1875 Name_Req : Boolean := False;
1876 Renaming_Req : Boolean := False) return Node_Id
1878 New_Exp : Node_Id;
1880 begin
1881 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1882 New_Exp := New_Copy_Tree (Exp);
1883 Remove_Checks (Exp);
1884 return New_Exp;
1885 end Duplicate_Subexpr_Move_Checks;
1887 --------------------
1888 -- Ensure_Defined --
1889 --------------------
1891 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1892 IR : Node_Id;
1894 begin
1895 -- An itype reference must only be created if this is a local itype, so
1896 -- that gigi can elaborate it on the proper objstack.
1898 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
1899 IR := Make_Itype_Reference (Sloc (N));
1900 Set_Itype (IR, Typ);
1901 Insert_Action (N, IR);
1902 end if;
1903 end Ensure_Defined;
1905 --------------------
1906 -- Entry_Names_OK --
1907 --------------------
1909 function Entry_Names_OK return Boolean is
1910 begin
1911 return
1912 not Restricted_Profile
1913 and then not Global_Discard_Names
1914 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1915 and then not Restriction_Active (No_Local_Allocators);
1916 end Entry_Names_OK;
1918 -------------------
1919 -- Evaluate_Name --
1920 -------------------
1922 procedure Evaluate_Name (Nam : Node_Id) is
1923 K : constant Node_Kind := Nkind (Nam);
1925 begin
1926 -- For an explicit dereference, we simply force the evaluation of the
1927 -- name expression. The dereference provides a value that is the address
1928 -- for the renamed object, and it is precisely this value that we want
1929 -- to preserve.
1931 if K = N_Explicit_Dereference then
1932 Force_Evaluation (Prefix (Nam));
1934 -- For a selected component, we simply evaluate the prefix
1936 elsif K = N_Selected_Component then
1937 Evaluate_Name (Prefix (Nam));
1939 -- For an indexed component, or an attribute reference, we evaluate the
1940 -- prefix, which is itself a name, recursively, and then force the
1941 -- evaluation of all the subscripts (or attribute expressions).
1943 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
1944 Evaluate_Name (Prefix (Nam));
1946 declare
1947 E : Node_Id;
1949 begin
1950 E := First (Expressions (Nam));
1951 while Present (E) loop
1952 Force_Evaluation (E);
1954 if Original_Node (E) /= E then
1955 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
1956 end if;
1958 Next (E);
1959 end loop;
1960 end;
1962 -- For a slice, we evaluate the prefix, as for the indexed component
1963 -- case and then, if there is a range present, either directly or as the
1964 -- constraint of a discrete subtype indication, we evaluate the two
1965 -- bounds of this range.
1967 elsif K = N_Slice then
1968 Evaluate_Name (Prefix (Nam));
1969 Evaluate_Slice_Bounds (Nam);
1971 -- For a type conversion, the expression of the conversion must be the
1972 -- name of an object, and we simply need to evaluate this name.
1974 elsif K = N_Type_Conversion then
1975 Evaluate_Name (Expression (Nam));
1977 -- For a function call, we evaluate the call
1979 elsif K = N_Function_Call then
1980 Force_Evaluation (Nam);
1982 -- The remaining cases are direct name, operator symbol and character
1983 -- literal. In all these cases, we do nothing, since we want to
1984 -- reevaluate each time the renamed object is used.
1986 else
1987 return;
1988 end if;
1989 end Evaluate_Name;
1991 ---------------------------
1992 -- Evaluate_Slice_Bounds --
1993 ---------------------------
1995 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
1996 DR : constant Node_Id := Discrete_Range (Slice);
1997 Constr : Node_Id;
1998 Rexpr : Node_Id;
2000 begin
2001 if Nkind (DR) = N_Range then
2002 Force_Evaluation (Low_Bound (DR));
2003 Force_Evaluation (High_Bound (DR));
2005 elsif Nkind (DR) = N_Subtype_Indication then
2006 Constr := Constraint (DR);
2008 if Nkind (Constr) = N_Range_Constraint then
2009 Rexpr := Range_Expression (Constr);
2011 Force_Evaluation (Low_Bound (Rexpr));
2012 Force_Evaluation (High_Bound (Rexpr));
2013 end if;
2014 end if;
2015 end Evaluate_Slice_Bounds;
2017 ---------------------
2018 -- Evolve_And_Then --
2019 ---------------------
2021 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
2022 begin
2023 if No (Cond) then
2024 Cond := Cond1;
2025 else
2026 Cond :=
2027 Make_And_Then (Sloc (Cond1),
2028 Left_Opnd => Cond,
2029 Right_Opnd => Cond1);
2030 end if;
2031 end Evolve_And_Then;
2033 --------------------
2034 -- Evolve_Or_Else --
2035 --------------------
2037 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
2038 begin
2039 if No (Cond) then
2040 Cond := Cond1;
2041 else
2042 Cond :=
2043 Make_Or_Else (Sloc (Cond1),
2044 Left_Opnd => Cond,
2045 Right_Opnd => Cond1);
2046 end if;
2047 end Evolve_Or_Else;
2049 -----------------------------------------
2050 -- Expand_Static_Predicates_In_Choices --
2051 -----------------------------------------
2053 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
2054 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
2056 Choices : constant List_Id := Discrete_Choices (N);
2058 Choice : Node_Id;
2059 Next_C : Node_Id;
2060 P : Node_Id;
2061 C : Node_Id;
2063 begin
2064 Choice := First (Choices);
2065 while Present (Choice) loop
2066 Next_C := Next (Choice);
2068 -- Check for name of subtype with static predicate
2070 if Is_Entity_Name (Choice)
2071 and then Is_Type (Entity (Choice))
2072 and then Has_Predicates (Entity (Choice))
2073 then
2074 -- Loop through entries in predicate list, converting to choices
2075 -- and inserting in the list before the current choice. Note that
2076 -- if the list is empty, corresponding to a False predicate, then
2077 -- no choices are inserted.
2079 P := First (Static_Discrete_Predicate (Entity (Choice)));
2080 while Present (P) loop
2082 -- If low bound and high bounds are equal, copy simple choice
2084 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
2085 C := New_Copy (Low_Bound (P));
2087 -- Otherwise copy a range
2089 else
2090 C := New_Copy (P);
2091 end if;
2093 -- Change Sloc to referencing choice (rather than the Sloc of
2094 -- the predicate declaration element itself).
2096 Set_Sloc (C, Sloc (Choice));
2097 Insert_Before (Choice, C);
2098 Next (P);
2099 end loop;
2101 -- Delete the predicated entry
2103 Remove (Choice);
2104 end if;
2106 -- Move to next choice to check
2108 Choice := Next_C;
2109 end loop;
2110 end Expand_Static_Predicates_In_Choices;
2112 ------------------------------
2113 -- Expand_Subtype_From_Expr --
2114 ------------------------------
2116 -- This function is applicable for both static and dynamic allocation of
2117 -- objects which are constrained by an initial expression. Basically it
2118 -- transforms an unconstrained subtype indication into a constrained one.
2120 -- The expression may also be transformed in certain cases in order to
2121 -- avoid multiple evaluation. In the static allocation case, the general
2122 -- scheme is:
2124 -- Val : T := Expr;
2126 -- is transformed into
2128 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
2130 -- Here are the main cases :
2132 -- <if Expr is a Slice>
2133 -- Val : T ([Index_Subtype (Expr)]) := Expr;
2135 -- <elsif Expr is a String Literal>
2136 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
2138 -- <elsif Expr is Constrained>
2139 -- subtype T is Type_Of_Expr
2140 -- Val : T := Expr;
2142 -- <elsif Expr is an entity_name>
2143 -- Val : T (constraints taken from Expr) := Expr;
2145 -- <else>
2146 -- type Axxx is access all T;
2147 -- Rval : Axxx := Expr'ref;
2148 -- Val : T (constraints taken from Rval) := Rval.all;
2150 -- ??? note: when the Expression is allocated in the secondary stack
2151 -- we could use it directly instead of copying it by declaring
2152 -- Val : T (...) renames Rval.all
2154 procedure Expand_Subtype_From_Expr
2155 (N : Node_Id;
2156 Unc_Type : Entity_Id;
2157 Subtype_Indic : Node_Id;
2158 Exp : Node_Id)
2160 Loc : constant Source_Ptr := Sloc (N);
2161 Exp_Typ : constant Entity_Id := Etype (Exp);
2162 T : Entity_Id;
2164 begin
2165 -- In general we cannot build the subtype if expansion is disabled,
2166 -- because internal entities may not have been defined. However, to
2167 -- avoid some cascaded errors, we try to continue when the expression is
2168 -- an array (or string), because it is safe to compute the bounds. It is
2169 -- in fact required to do so even in a generic context, because there
2170 -- may be constants that depend on the bounds of a string literal, both
2171 -- standard string types and more generally arrays of characters.
2173 -- In GNATprove mode, these extra subtypes are not needed
2175 if GNATprove_Mode then
2176 return;
2177 end if;
2179 if not Expander_Active
2180 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
2181 then
2182 return;
2183 end if;
2185 if Nkind (Exp) = N_Slice then
2186 declare
2187 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
2189 begin
2190 Rewrite (Subtype_Indic,
2191 Make_Subtype_Indication (Loc,
2192 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
2193 Constraint =>
2194 Make_Index_Or_Discriminant_Constraint (Loc,
2195 Constraints => New_List
2196 (New_Occurrence_Of (Slice_Type, Loc)))));
2198 -- This subtype indication may be used later for constraint checks
2199 -- we better make sure that if a variable was used as a bound of
2200 -- of the original slice, its value is frozen.
2202 Evaluate_Slice_Bounds (Exp);
2203 end;
2205 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2206 Rewrite (Subtype_Indic,
2207 Make_Subtype_Indication (Loc,
2208 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
2209 Constraint =>
2210 Make_Index_Or_Discriminant_Constraint (Loc,
2211 Constraints => New_List (
2212 Make_Literal_Range (Loc,
2213 Literal_Typ => Exp_Typ)))));
2215 -- If the type of the expression is an internally generated type it
2216 -- may not be necessary to create a new subtype. However there are two
2217 -- exceptions: references to the current instances, and aliased array
2218 -- object declarations for which the backend needs to create a template.
2220 elsif Is_Constrained (Exp_Typ)
2221 and then not Is_Class_Wide_Type (Unc_Type)
2222 and then
2223 (Nkind (N) /= N_Object_Declaration
2224 or else not Is_Entity_Name (Expression (N))
2225 or else not Comes_From_Source (Entity (Expression (N)))
2226 or else not Is_Array_Type (Exp_Typ)
2227 or else not Aliased_Present (N))
2228 then
2229 if Is_Itype (Exp_Typ) then
2231 -- Within an initialization procedure, a selected component
2232 -- denotes a component of the enclosing record, and it appears as
2233 -- an actual in a call to its own initialization procedure. If
2234 -- this component depends on the outer discriminant, we must
2235 -- generate the proper actual subtype for it.
2237 if Nkind (Exp) = N_Selected_Component
2238 and then Within_Init_Proc
2239 then
2240 declare
2241 Decl : constant Node_Id :=
2242 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2243 begin
2244 if Present (Decl) then
2245 Insert_Action (N, Decl);
2246 T := Defining_Identifier (Decl);
2247 else
2248 T := Exp_Typ;
2249 end if;
2250 end;
2252 -- No need to generate a new subtype
2254 else
2255 T := Exp_Typ;
2256 end if;
2258 else
2259 T := Make_Temporary (Loc, 'T');
2261 Insert_Action (N,
2262 Make_Subtype_Declaration (Loc,
2263 Defining_Identifier => T,
2264 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
2266 -- This type is marked as an itype even though it has an explicit
2267 -- declaration since otherwise Is_Generic_Actual_Type can get
2268 -- set, resulting in the generation of spurious errors. (See
2269 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
2271 Set_Is_Itype (T);
2272 Set_Associated_Node_For_Itype (T, Exp);
2273 end if;
2275 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
2277 -- Nothing needs to be done for private types with unknown discriminants
2278 -- if the underlying type is not an unconstrained composite type or it
2279 -- is an unchecked union.
2281 elsif Is_Private_Type (Unc_Type)
2282 and then Has_Unknown_Discriminants (Unc_Type)
2283 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
2284 or else Is_Constrained (Underlying_Type (Unc_Type))
2285 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
2286 then
2287 null;
2289 -- Case of derived type with unknown discriminants where the parent type
2290 -- also has unknown discriminants.
2292 elsif Is_Record_Type (Unc_Type)
2293 and then not Is_Class_Wide_Type (Unc_Type)
2294 and then Has_Unknown_Discriminants (Unc_Type)
2295 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2296 then
2297 -- Nothing to be done if no underlying record view available
2299 if No (Underlying_Record_View (Unc_Type)) then
2300 null;
2302 -- Otherwise use the Underlying_Record_View to create the proper
2303 -- constrained subtype for an object of a derived type with unknown
2304 -- discriminants.
2306 else
2307 Remove_Side_Effects (Exp);
2308 Rewrite (Subtype_Indic,
2309 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2310 end if;
2312 -- Renamings of class-wide interface types require no equivalent
2313 -- constrained type declarations because we only need to reference
2314 -- the tag component associated with the interface. The same is
2315 -- presumably true for class-wide types in general, so this test
2316 -- is broadened to include all class-wide renamings, which also
2317 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2318 -- (Is this really correct, or are there some cases of class-wide
2319 -- renamings that require action in this procedure???)
2321 elsif Present (N)
2322 and then Nkind (N) = N_Object_Renaming_Declaration
2323 and then Is_Class_Wide_Type (Unc_Type)
2324 then
2325 null;
2327 -- In Ada 95 nothing to be done if the type of the expression is limited
2328 -- because in this case the expression cannot be copied, and its use can
2329 -- only be by reference.
2331 -- In Ada 2005 the context can be an object declaration whose expression
2332 -- is a function that returns in place. If the nominal subtype has
2333 -- unknown discriminants, the call still provides constraints on the
2334 -- object, and we have to create an actual subtype from it.
2336 -- If the type is class-wide, the expression is dynamically tagged and
2337 -- we do not create an actual subtype either. Ditto for an interface.
2338 -- For now this applies only if the type is immutably limited, and the
2339 -- function being called is build-in-place. This will have to be revised
2340 -- when build-in-place functions are generalized to other types.
2342 elsif Is_Limited_View (Exp_Typ)
2343 and then
2344 (Is_Class_Wide_Type (Exp_Typ)
2345 or else Is_Interface (Exp_Typ)
2346 or else not Has_Unknown_Discriminants (Exp_Typ)
2347 or else not Is_Composite_Type (Unc_Type))
2348 then
2349 null;
2351 -- For limited objects initialized with build in place function calls,
2352 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2353 -- node in the expression initializing the object, which breaks the
2354 -- circuitry that detects and adds the additional arguments to the
2355 -- called function.
2357 elsif Is_Build_In_Place_Function_Call (Exp) then
2358 null;
2360 else
2361 Remove_Side_Effects (Exp);
2362 Rewrite (Subtype_Indic,
2363 Make_Subtype_From_Expr (Exp, Unc_Type));
2364 end if;
2365 end Expand_Subtype_From_Expr;
2367 ----------------------
2368 -- Finalize_Address --
2369 ----------------------
2371 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
2372 Utyp : Entity_Id := Typ;
2374 begin
2375 -- Handle protected class-wide or task class-wide types
2377 if Is_Class_Wide_Type (Utyp) then
2378 if Is_Concurrent_Type (Root_Type (Utyp)) then
2379 Utyp := Root_Type (Utyp);
2381 elsif Is_Private_Type (Root_Type (Utyp))
2382 and then Present (Full_View (Root_Type (Utyp)))
2383 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
2384 then
2385 Utyp := Full_View (Root_Type (Utyp));
2386 end if;
2387 end if;
2389 -- Handle private types
2391 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
2392 Utyp := Full_View (Utyp);
2393 end if;
2395 -- Handle protected and task types
2397 if Is_Concurrent_Type (Utyp)
2398 and then Present (Corresponding_Record_Type (Utyp))
2399 then
2400 Utyp := Corresponding_Record_Type (Utyp);
2401 end if;
2403 Utyp := Underlying_Type (Base_Type (Utyp));
2405 -- Deal with untagged derivation of private views. If the parent is
2406 -- now known to be protected, the finalization routine is the one
2407 -- defined on the corresponding record of the ancestor (corresponding
2408 -- records do not automatically inherit operations, but maybe they
2409 -- should???)
2411 if Is_Untagged_Derivation (Typ) then
2412 if Is_Protected_Type (Typ) then
2413 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
2415 else
2416 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
2418 if Is_Protected_Type (Utyp) then
2419 Utyp := Corresponding_Record_Type (Utyp);
2420 end if;
2421 end if;
2422 end if;
2424 -- If the underlying_type is a subtype, we are dealing with the
2425 -- completion of a private type. We need to access the base type and
2426 -- generate a conversion to it.
2428 if Utyp /= Base_Type (Utyp) then
2429 pragma Assert (Is_Private_Type (Typ));
2431 Utyp := Base_Type (Utyp);
2432 end if;
2434 -- When dealing with an internally built full view for a type with
2435 -- unknown discriminants, use the original record type.
2437 if Is_Underlying_Record_View (Utyp) then
2438 Utyp := Etype (Utyp);
2439 end if;
2441 return TSS (Utyp, TSS_Finalize_Address);
2442 end Finalize_Address;
2444 ------------------------
2445 -- Find_Interface_ADT --
2446 ------------------------
2448 function Find_Interface_ADT
2449 (T : Entity_Id;
2450 Iface : Entity_Id) return Elmt_Id
2452 ADT : Elmt_Id;
2453 Typ : Entity_Id := T;
2455 begin
2456 pragma Assert (Is_Interface (Iface));
2458 -- Handle private types
2460 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2461 Typ := Full_View (Typ);
2462 end if;
2464 -- Handle access types
2466 if Is_Access_Type (Typ) then
2467 Typ := Designated_Type (Typ);
2468 end if;
2470 -- Handle task and protected types implementing interfaces
2472 if Is_Concurrent_Type (Typ) then
2473 Typ := Corresponding_Record_Type (Typ);
2474 end if;
2476 pragma Assert
2477 (not Is_Class_Wide_Type (Typ)
2478 and then Ekind (Typ) /= E_Incomplete_Type);
2480 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2481 return First_Elmt (Access_Disp_Table (Typ));
2483 else
2484 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
2485 while Present (ADT)
2486 and then Present (Related_Type (Node (ADT)))
2487 and then Related_Type (Node (ADT)) /= Iface
2488 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2489 Use_Full_View => True)
2490 loop
2491 Next_Elmt (ADT);
2492 end loop;
2494 pragma Assert (Present (Related_Type (Node (ADT))));
2495 return ADT;
2496 end if;
2497 end Find_Interface_ADT;
2499 ------------------------
2500 -- Find_Interface_Tag --
2501 ------------------------
2503 function Find_Interface_Tag
2504 (T : Entity_Id;
2505 Iface : Entity_Id) return Entity_Id
2507 AI_Tag : Entity_Id;
2508 Found : Boolean := False;
2509 Typ : Entity_Id := T;
2511 procedure Find_Tag (Typ : Entity_Id);
2512 -- Internal subprogram used to recursively climb to the ancestors
2514 --------------
2515 -- Find_Tag --
2516 --------------
2518 procedure Find_Tag (Typ : Entity_Id) is
2519 AI_Elmt : Elmt_Id;
2520 AI : Node_Id;
2522 begin
2523 -- This routine does not handle the case in which the interface is an
2524 -- ancestor of Typ. That case is handled by the enclosing subprogram.
2526 pragma Assert (Typ /= Iface);
2528 -- Climb to the root type handling private types
2530 if Present (Full_View (Etype (Typ))) then
2531 if Full_View (Etype (Typ)) /= Typ then
2532 Find_Tag (Full_View (Etype (Typ)));
2533 end if;
2535 elsif Etype (Typ) /= Typ then
2536 Find_Tag (Etype (Typ));
2537 end if;
2539 -- Traverse the list of interfaces implemented by the type
2541 if not Found
2542 and then Present (Interfaces (Typ))
2543 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
2544 then
2545 -- Skip the tag associated with the primary table
2547 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2548 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2549 pragma Assert (Present (AI_Tag));
2551 AI_Elmt := First_Elmt (Interfaces (Typ));
2552 while Present (AI_Elmt) loop
2553 AI := Node (AI_Elmt);
2555 if AI = Iface
2556 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2557 then
2558 Found := True;
2559 return;
2560 end if;
2562 AI_Tag := Next_Tag_Component (AI_Tag);
2563 Next_Elmt (AI_Elmt);
2564 end loop;
2565 end if;
2566 end Find_Tag;
2568 -- Start of processing for Find_Interface_Tag
2570 begin
2571 pragma Assert (Is_Interface (Iface));
2573 -- Handle access types
2575 if Is_Access_Type (Typ) then
2576 Typ := Designated_Type (Typ);
2577 end if;
2579 -- Handle class-wide types
2581 if Is_Class_Wide_Type (Typ) then
2582 Typ := Root_Type (Typ);
2583 end if;
2585 -- Handle private types
2587 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2588 Typ := Full_View (Typ);
2589 end if;
2591 -- Handle entities from the limited view
2593 if Ekind (Typ) = E_Incomplete_Type then
2594 pragma Assert (Present (Non_Limited_View (Typ)));
2595 Typ := Non_Limited_View (Typ);
2596 end if;
2598 -- Handle task and protected types implementing interfaces
2600 if Is_Concurrent_Type (Typ) then
2601 Typ := Corresponding_Record_Type (Typ);
2602 end if;
2604 -- If the interface is an ancestor of the type, then it shared the
2605 -- primary dispatch table.
2607 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2608 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2609 return First_Tag_Component (Typ);
2611 -- Otherwise we need to search for its associated tag component
2613 else
2614 Find_Tag (Typ);
2615 pragma Assert (Found);
2616 return AI_Tag;
2617 end if;
2618 end Find_Interface_Tag;
2620 ------------------
2621 -- Find_Prim_Op --
2622 ------------------
2624 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
2625 Prim : Elmt_Id;
2626 Typ : Entity_Id := T;
2627 Op : Entity_Id;
2629 begin
2630 if Is_Class_Wide_Type (Typ) then
2631 Typ := Root_Type (Typ);
2632 end if;
2634 Typ := Underlying_Type (Typ);
2636 -- Loop through primitive operations
2638 Prim := First_Elmt (Primitive_Operations (Typ));
2639 while Present (Prim) loop
2640 Op := Node (Prim);
2642 -- We can retrieve primitive operations by name if it is an internal
2643 -- name. For equality we must check that both of its operands have
2644 -- the same type, to avoid confusion with user-defined equalities
2645 -- than may have a non-symmetric signature.
2647 exit when Chars (Op) = Name
2648 and then
2649 (Name /= Name_Op_Eq
2650 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
2652 Next_Elmt (Prim);
2654 -- Raise Program_Error if no primitive found
2656 if No (Prim) then
2657 raise Program_Error;
2658 end if;
2659 end loop;
2661 return Node (Prim);
2662 end Find_Prim_Op;
2664 ------------------
2665 -- Find_Prim_Op --
2666 ------------------
2668 function Find_Prim_Op
2669 (T : Entity_Id;
2670 Name : TSS_Name_Type) return Entity_Id
2672 Inher_Op : Entity_Id := Empty;
2673 Own_Op : Entity_Id := Empty;
2674 Prim_Elmt : Elmt_Id;
2675 Prim_Id : Entity_Id;
2676 Typ : Entity_Id := T;
2678 begin
2679 if Is_Class_Wide_Type (Typ) then
2680 Typ := Root_Type (Typ);
2681 end if;
2683 Typ := Underlying_Type (Typ);
2685 -- This search is based on the assertion that the dispatching version
2686 -- of the TSS routine always precedes the real primitive.
2688 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2689 while Present (Prim_Elmt) loop
2690 Prim_Id := Node (Prim_Elmt);
2692 if Is_TSS (Prim_Id, Name) then
2693 if Present (Alias (Prim_Id)) then
2694 Inher_Op := Prim_Id;
2695 else
2696 Own_Op := Prim_Id;
2697 end if;
2698 end if;
2700 Next_Elmt (Prim_Elmt);
2701 end loop;
2703 if Present (Own_Op) then
2704 return Own_Op;
2705 elsif Present (Inher_Op) then
2706 return Inher_Op;
2707 else
2708 raise Program_Error;
2709 end if;
2710 end Find_Prim_Op;
2712 ----------------------------
2713 -- Find_Protection_Object --
2714 ----------------------------
2716 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2717 S : Entity_Id;
2719 begin
2720 S := Scop;
2721 while Present (S) loop
2722 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
2723 and then Present (Protection_Object (S))
2724 then
2725 return Protection_Object (S);
2726 end if;
2728 S := Scope (S);
2729 end loop;
2731 -- If we do not find a Protection object in the scope chain, then
2732 -- something has gone wrong, most likely the object was never created.
2734 raise Program_Error;
2735 end Find_Protection_Object;
2737 --------------------------
2738 -- Find_Protection_Type --
2739 --------------------------
2741 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2742 Comp : Entity_Id;
2743 Typ : Entity_Id := Conc_Typ;
2745 begin
2746 if Is_Concurrent_Type (Typ) then
2747 Typ := Corresponding_Record_Type (Typ);
2748 end if;
2750 -- Since restriction violations are not considered serious errors, the
2751 -- expander remains active, but may leave the corresponding record type
2752 -- malformed. In such cases, component _object is not available so do
2753 -- not look for it.
2755 if not Analyzed (Typ) then
2756 return Empty;
2757 end if;
2759 Comp := First_Component (Typ);
2760 while Present (Comp) loop
2761 if Chars (Comp) = Name_uObject then
2762 return Base_Type (Etype (Comp));
2763 end if;
2765 Next_Component (Comp);
2766 end loop;
2768 -- The corresponding record of a protected type should always have an
2769 -- _object field.
2771 raise Program_Error;
2772 end Find_Protection_Type;
2774 -----------------------
2775 -- Find_Hook_Context --
2776 -----------------------
2778 function Find_Hook_Context (N : Node_Id) return Node_Id is
2779 Par : Node_Id;
2780 Top : Node_Id;
2782 Wrapped_Node : Node_Id;
2783 -- Note: if we are in a transient scope, we want to reuse it as
2784 -- the context for actions insertion, if possible. But if N is itself
2785 -- part of the stored actions for the current transient scope,
2786 -- then we need to insert at the appropriate (inner) location in
2787 -- the not as an action on Node_To_Be_Wrapped.
2789 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
2791 begin
2792 -- When the node is inside a case/if expression, the lifetime of any
2793 -- temporary controlled object is extended. Find a suitable insertion
2794 -- node by locating the topmost case or if expressions.
2796 if In_Cond_Expr then
2797 Par := N;
2798 Top := N;
2799 while Present (Par) loop
2800 if Nkind_In (Original_Node (Par), N_Case_Expression,
2801 N_If_Expression)
2802 then
2803 Top := Par;
2805 -- Prevent the search from going too far
2807 elsif Is_Body_Or_Package_Declaration (Par) then
2808 exit;
2809 end if;
2811 Par := Parent (Par);
2812 end loop;
2814 -- The topmost case or if expression is now recovered, but it may
2815 -- still not be the correct place to add generated code. Climb to
2816 -- find a parent that is part of a declarative or statement list,
2817 -- and is not a list of actuals in a call.
2819 Par := Top;
2820 while Present (Par) loop
2821 if Is_List_Member (Par)
2822 and then not Nkind_In (Par, N_Component_Association,
2823 N_Discriminant_Association,
2824 N_Parameter_Association,
2825 N_Pragma_Argument_Association)
2826 and then not Nkind_In
2827 (Parent (Par), N_Function_Call,
2828 N_Procedure_Call_Statement,
2829 N_Entry_Call_Statement)
2831 then
2832 return Par;
2834 -- Prevent the search from going too far
2836 elsif Is_Body_Or_Package_Declaration (Par) then
2837 exit;
2838 end if;
2840 Par := Parent (Par);
2841 end loop;
2843 return Par;
2845 else
2846 Par := N;
2847 while Present (Par) loop
2849 -- Keep climbing past various operators
2851 if Nkind (Parent (Par)) in N_Op
2852 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
2853 then
2854 Par := Parent (Par);
2855 else
2856 exit;
2857 end if;
2858 end loop;
2860 Top := Par;
2862 -- The node may be located in a pragma in which case return the
2863 -- pragma itself:
2865 -- pragma Precondition (... and then Ctrl_Func_Call ...);
2867 -- Similar case occurs when the node is related to an object
2868 -- declaration or assignment:
2870 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
2872 -- Another case to consider is when the node is part of a return
2873 -- statement:
2875 -- return ... and then Ctrl_Func_Call ...;
2877 -- Another case is when the node acts as a formal in a procedure
2878 -- call statement:
2880 -- Proc (... and then Ctrl_Func_Call ...);
2882 if Scope_Is_Transient then
2883 Wrapped_Node := Node_To_Be_Wrapped;
2884 else
2885 Wrapped_Node := Empty;
2886 end if;
2888 while Present (Par) loop
2889 if Par = Wrapped_Node
2890 or else Nkind_In (Par, N_Assignment_Statement,
2891 N_Object_Declaration,
2892 N_Pragma,
2893 N_Procedure_Call_Statement,
2894 N_Simple_Return_Statement)
2895 then
2896 return Par;
2898 -- Prevent the search from going too far
2900 elsif Is_Body_Or_Package_Declaration (Par) then
2901 exit;
2902 end if;
2904 Par := Parent (Par);
2905 end loop;
2907 -- Return the topmost short circuit operator
2909 return Top;
2910 end if;
2911 end Find_Hook_Context;
2913 ------------------------------
2914 -- Following_Address_Clause --
2915 ------------------------------
2917 function Following_Address_Clause (D : Node_Id) return Node_Id is
2918 Id : constant Entity_Id := Defining_Identifier (D);
2919 Result : Node_Id;
2920 Par : Node_Id;
2922 function Check_Decls (D : Node_Id) return Node_Id;
2923 -- This internal function differs from the main function in that it
2924 -- gets called to deal with a following package private part, and
2925 -- it checks declarations starting with D (the main function checks
2926 -- declarations following D). If D is Empty, then Empty is returned.
2928 -----------------
2929 -- Check_Decls --
2930 -----------------
2932 function Check_Decls (D : Node_Id) return Node_Id is
2933 Decl : Node_Id;
2935 begin
2936 Decl := D;
2937 while Present (Decl) loop
2938 if Nkind (Decl) = N_At_Clause
2939 and then Chars (Identifier (Decl)) = Chars (Id)
2940 then
2941 return Decl;
2943 elsif Nkind (Decl) = N_Attribute_Definition_Clause
2944 and then Chars (Decl) = Name_Address
2945 and then Chars (Name (Decl)) = Chars (Id)
2946 then
2947 return Decl;
2948 end if;
2950 Next (Decl);
2951 end loop;
2953 -- Otherwise not found, return Empty
2955 return Empty;
2956 end Check_Decls;
2958 -- Start of processing for Following_Address_Clause
2960 begin
2961 -- If parser detected no address clause for the identifier in question,
2962 -- then the answer is a quick NO, without the need for a search.
2964 if not Get_Name_Table_Boolean1 (Chars (Id)) then
2965 return Empty;
2966 end if;
2968 -- Otherwise search current declarative unit
2970 Result := Check_Decls (Next (D));
2972 if Present (Result) then
2973 return Result;
2974 end if;
2976 -- Check for possible package private part following
2978 Par := Parent (D);
2980 if Nkind (Par) = N_Package_Specification
2981 and then Visible_Declarations (Par) = List_Containing (D)
2982 and then Present (Private_Declarations (Par))
2983 then
2984 -- Private part present, check declarations there
2986 return Check_Decls (First (Private_Declarations (Par)));
2988 else
2989 -- No private part, clause not found, return Empty
2991 return Empty;
2992 end if;
2993 end Following_Address_Clause;
2995 ----------------------
2996 -- Force_Evaluation --
2997 ----------------------
2999 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
3000 begin
3001 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
3002 end Force_Evaluation;
3004 ---------------------------------
3005 -- Fully_Qualified_Name_String --
3006 ---------------------------------
3008 function Fully_Qualified_Name_String
3009 (E : Entity_Id;
3010 Append_NUL : Boolean := True) return String_Id
3012 procedure Internal_Full_Qualified_Name (E : Entity_Id);
3013 -- Compute recursively the qualified name without NUL at the end, adding
3014 -- it to the currently started string being generated
3016 ----------------------------------
3017 -- Internal_Full_Qualified_Name --
3018 ----------------------------------
3020 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
3021 Ent : Entity_Id;
3023 begin
3024 -- Deal properly with child units
3026 if Nkind (E) = N_Defining_Program_Unit_Name then
3027 Ent := Defining_Identifier (E);
3028 else
3029 Ent := E;
3030 end if;
3032 -- Compute qualification recursively (only "Standard" has no scope)
3034 if Present (Scope (Scope (Ent))) then
3035 Internal_Full_Qualified_Name (Scope (Ent));
3036 Store_String_Char (Get_Char_Code ('.'));
3037 end if;
3039 -- Every entity should have a name except some expanded blocks
3040 -- don't bother about those.
3042 if Chars (Ent) = No_Name then
3043 return;
3044 end if;
3046 -- Generates the entity name in upper case
3048 Get_Decoded_Name_String (Chars (Ent));
3049 Set_All_Upper_Case;
3050 Store_String_Chars (Name_Buffer (1 .. Name_Len));
3051 return;
3052 end Internal_Full_Qualified_Name;
3054 -- Start of processing for Full_Qualified_Name
3056 begin
3057 Start_String;
3058 Internal_Full_Qualified_Name (E);
3060 if Append_NUL then
3061 Store_String_Char (Get_Char_Code (ASCII.NUL));
3062 end if;
3064 return End_String;
3065 end Fully_Qualified_Name_String;
3067 ------------------------
3068 -- Generate_Poll_Call --
3069 ------------------------
3071 procedure Generate_Poll_Call (N : Node_Id) is
3072 begin
3073 -- No poll call if polling not active
3075 if not Polling_Required then
3076 return;
3078 -- Otherwise generate require poll call
3080 else
3081 Insert_Before_And_Analyze (N,
3082 Make_Procedure_Call_Statement (Sloc (N),
3083 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
3084 end if;
3085 end Generate_Poll_Call;
3087 ---------------------------------
3088 -- Get_Current_Value_Condition --
3089 ---------------------------------
3091 -- Note: the implementation of this procedure is very closely tied to the
3092 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
3093 -- interpret Current_Value fields set by the Set procedure, so the two
3094 -- procedures need to be closely coordinated.
3096 procedure Get_Current_Value_Condition
3097 (Var : Node_Id;
3098 Op : out Node_Kind;
3099 Val : out Node_Id)
3101 Loc : constant Source_Ptr := Sloc (Var);
3102 Ent : constant Entity_Id := Entity (Var);
3104 procedure Process_Current_Value_Condition
3105 (N : Node_Id;
3106 S : Boolean);
3107 -- N is an expression which holds either True (S = True) or False (S =
3108 -- False) in the condition. This procedure digs out the expression and
3109 -- if it refers to Ent, sets Op and Val appropriately.
3111 -------------------------------------
3112 -- Process_Current_Value_Condition --
3113 -------------------------------------
3115 procedure Process_Current_Value_Condition
3116 (N : Node_Id;
3117 S : Boolean)
3119 Cond : Node_Id;
3120 Prev_Cond : Node_Id;
3121 Sens : Boolean;
3123 begin
3124 Cond := N;
3125 Sens := S;
3127 loop
3128 Prev_Cond := Cond;
3130 -- Deal with NOT operators, inverting sense
3132 while Nkind (Cond) = N_Op_Not loop
3133 Cond := Right_Opnd (Cond);
3134 Sens := not Sens;
3135 end loop;
3137 -- Deal with conversions, qualifications, and expressions with
3138 -- actions.
3140 while Nkind_In (Cond,
3141 N_Type_Conversion,
3142 N_Qualified_Expression,
3143 N_Expression_With_Actions)
3144 loop
3145 Cond := Expression (Cond);
3146 end loop;
3148 exit when Cond = Prev_Cond;
3149 end loop;
3151 -- Deal with AND THEN and AND cases
3153 if Nkind_In (Cond, N_And_Then, N_Op_And) then
3155 -- Don't ever try to invert a condition that is of the form of an
3156 -- AND or AND THEN (since we are not doing sufficiently general
3157 -- processing to allow this).
3159 if Sens = False then
3160 Op := N_Empty;
3161 Val := Empty;
3162 return;
3163 end if;
3165 -- Recursively process AND and AND THEN branches
3167 Process_Current_Value_Condition (Left_Opnd (Cond), True);
3169 if Op /= N_Empty then
3170 return;
3171 end if;
3173 Process_Current_Value_Condition (Right_Opnd (Cond), True);
3174 return;
3176 -- Case of relational operator
3178 elsif Nkind (Cond) in N_Op_Compare then
3179 Op := Nkind (Cond);
3181 -- Invert sense of test if inverted test
3183 if Sens = False then
3184 case Op is
3185 when N_Op_Eq => Op := N_Op_Ne;
3186 when N_Op_Ne => Op := N_Op_Eq;
3187 when N_Op_Lt => Op := N_Op_Ge;
3188 when N_Op_Gt => Op := N_Op_Le;
3189 when N_Op_Le => Op := N_Op_Gt;
3190 when N_Op_Ge => Op := N_Op_Lt;
3191 when others => raise Program_Error;
3192 end case;
3193 end if;
3195 -- Case of entity op value
3197 if Is_Entity_Name (Left_Opnd (Cond))
3198 and then Ent = Entity (Left_Opnd (Cond))
3199 and then Compile_Time_Known_Value (Right_Opnd (Cond))
3200 then
3201 Val := Right_Opnd (Cond);
3203 -- Case of value op entity
3205 elsif Is_Entity_Name (Right_Opnd (Cond))
3206 and then Ent = Entity (Right_Opnd (Cond))
3207 and then Compile_Time_Known_Value (Left_Opnd (Cond))
3208 then
3209 Val := Left_Opnd (Cond);
3211 -- We are effectively swapping operands
3213 case Op is
3214 when N_Op_Eq => null;
3215 when N_Op_Ne => null;
3216 when N_Op_Lt => Op := N_Op_Gt;
3217 when N_Op_Gt => Op := N_Op_Lt;
3218 when N_Op_Le => Op := N_Op_Ge;
3219 when N_Op_Ge => Op := N_Op_Le;
3220 when others => raise Program_Error;
3221 end case;
3223 else
3224 Op := N_Empty;
3225 end if;
3227 return;
3229 elsif Nkind_In (Cond,
3230 N_Type_Conversion,
3231 N_Qualified_Expression,
3232 N_Expression_With_Actions)
3233 then
3234 Cond := Expression (Cond);
3236 -- Case of Boolean variable reference, return as though the
3237 -- reference had said var = True.
3239 else
3240 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
3241 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
3243 if Sens = False then
3244 Op := N_Op_Ne;
3245 else
3246 Op := N_Op_Eq;
3247 end if;
3248 end if;
3249 end if;
3250 end Process_Current_Value_Condition;
3252 -- Start of processing for Get_Current_Value_Condition
3254 begin
3255 Op := N_Empty;
3256 Val := Empty;
3258 -- Immediate return, nothing doing, if this is not an object
3260 if Ekind (Ent) not in Object_Kind then
3261 return;
3262 end if;
3264 -- Otherwise examine current value
3266 declare
3267 CV : constant Node_Id := Current_Value (Ent);
3268 Sens : Boolean;
3269 Stm : Node_Id;
3271 begin
3272 -- If statement. Condition is known true in THEN section, known False
3273 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
3275 if Nkind (CV) = N_If_Statement then
3277 -- Before start of IF statement
3279 if Loc < Sloc (CV) then
3280 return;
3282 -- After end of IF statement
3284 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
3285 return;
3286 end if;
3288 -- At this stage we know that we are within the IF statement, but
3289 -- unfortunately, the tree does not record the SLOC of the ELSE so
3290 -- we cannot use a simple SLOC comparison to distinguish between
3291 -- the then/else statements, so we have to climb the tree.
3293 declare
3294 N : Node_Id;
3296 begin
3297 N := Parent (Var);
3298 while Parent (N) /= CV loop
3299 N := Parent (N);
3301 -- If we fall off the top of the tree, then that's odd, but
3302 -- perhaps it could occur in some error situation, and the
3303 -- safest response is simply to assume that the outcome of
3304 -- the condition is unknown. No point in bombing during an
3305 -- attempt to optimize things.
3307 if No (N) then
3308 return;
3309 end if;
3310 end loop;
3312 -- Now we have N pointing to a node whose parent is the IF
3313 -- statement in question, so now we can tell if we are within
3314 -- the THEN statements.
3316 if Is_List_Member (N)
3317 and then List_Containing (N) = Then_Statements (CV)
3318 then
3319 Sens := True;
3321 -- If the variable reference does not come from source, we
3322 -- cannot reliably tell whether it appears in the else part.
3323 -- In particular, if it appears in generated code for a node
3324 -- that requires finalization, it may be attached to a list
3325 -- that has not been yet inserted into the code. For now,
3326 -- treat it as unknown.
3328 elsif not Comes_From_Source (N) then
3329 return;
3331 -- Otherwise we must be in ELSIF or ELSE part
3333 else
3334 Sens := False;
3335 end if;
3336 end;
3338 -- ELSIF part. Condition is known true within the referenced
3339 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
3340 -- and unknown before the ELSE part or after the IF statement.
3342 elsif Nkind (CV) = N_Elsif_Part then
3344 -- if the Elsif_Part had condition_actions, the elsif has been
3345 -- rewritten as a nested if, and the original elsif_part is
3346 -- detached from the tree, so there is no way to obtain useful
3347 -- information on the current value of the variable.
3348 -- Can this be improved ???
3350 if No (Parent (CV)) then
3351 return;
3352 end if;
3354 Stm := Parent (CV);
3356 -- Before start of ELSIF part
3358 if Loc < Sloc (CV) then
3359 return;
3361 -- After end of IF statement
3363 elsif Loc >= Sloc (Stm) +
3364 Text_Ptr (UI_To_Int (End_Span (Stm)))
3365 then
3366 return;
3367 end if;
3369 -- Again we lack the SLOC of the ELSE, so we need to climb the
3370 -- tree to see if we are within the ELSIF part in question.
3372 declare
3373 N : Node_Id;
3375 begin
3376 N := Parent (Var);
3377 while Parent (N) /= Stm loop
3378 N := Parent (N);
3380 -- If we fall off the top of the tree, then that's odd, but
3381 -- perhaps it could occur in some error situation, and the
3382 -- safest response is simply to assume that the outcome of
3383 -- the condition is unknown. No point in bombing during an
3384 -- attempt to optimize things.
3386 if No (N) then
3387 return;
3388 end if;
3389 end loop;
3391 -- Now we have N pointing to a node whose parent is the IF
3392 -- statement in question, so see if is the ELSIF part we want.
3393 -- the THEN statements.
3395 if N = CV then
3396 Sens := True;
3398 -- Otherwise we must be in subsequent ELSIF or ELSE part
3400 else
3401 Sens := False;
3402 end if;
3403 end;
3405 -- Iteration scheme of while loop. The condition is known to be
3406 -- true within the body of the loop.
3408 elsif Nkind (CV) = N_Iteration_Scheme then
3409 declare
3410 Loop_Stmt : constant Node_Id := Parent (CV);
3412 begin
3413 -- Before start of body of loop
3415 if Loc < Sloc (Loop_Stmt) then
3416 return;
3418 -- After end of LOOP statement
3420 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
3421 return;
3423 -- We are within the body of the loop
3425 else
3426 Sens := True;
3427 end if;
3428 end;
3430 -- All other cases of Current_Value settings
3432 else
3433 return;
3434 end if;
3436 -- If we fall through here, then we have a reportable condition, Sens
3437 -- is True if the condition is true and False if it needs inverting.
3439 Process_Current_Value_Condition (Condition (CV), Sens);
3440 end;
3441 end Get_Current_Value_Condition;
3443 ---------------------
3444 -- Get_Stream_Size --
3445 ---------------------
3447 function Get_Stream_Size (E : Entity_Id) return Uint is
3448 begin
3449 -- If we have a Stream_Size clause for this type use it
3451 if Has_Stream_Size_Clause (E) then
3452 return Static_Integer (Expression (Stream_Size_Clause (E)));
3454 -- Otherwise the Stream_Size if the size of the type
3456 else
3457 return Esize (E);
3458 end if;
3459 end Get_Stream_Size;
3461 ---------------------------
3462 -- Has_Access_Constraint --
3463 ---------------------------
3465 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3466 Disc : Entity_Id;
3467 T : constant Entity_Id := Etype (E);
3469 begin
3470 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
3471 Disc := First_Discriminant (T);
3472 while Present (Disc) loop
3473 if Is_Access_Type (Etype (Disc)) then
3474 return True;
3475 end if;
3477 Next_Discriminant (Disc);
3478 end loop;
3480 return False;
3481 else
3482 return False;
3483 end if;
3484 end Has_Access_Constraint;
3486 -----------------------------------------------------
3487 -- Has_Annotate_Pragma_For_External_Axiomatization --
3488 -----------------------------------------------------
3490 function Has_Annotate_Pragma_For_External_Axiomatization
3491 (E : Entity_Id) return Boolean
3493 function Is_Annotate_Pragma_For_External_Axiomatization
3494 (N : Node_Id) return Boolean;
3495 -- Returns whether N is
3496 -- pragma Annotate (GNATprove, External_Axiomatization);
3498 ----------------------------------------------------
3499 -- Is_Annotate_Pragma_For_External_Axiomatization --
3500 ----------------------------------------------------
3502 -- The general form of pragma Annotate is
3504 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
3505 -- ARG ::= NAME | EXPRESSION
3507 -- The first two arguments are by convention intended to refer to an
3508 -- external tool and a tool-specific function. These arguments are
3509 -- not analyzed.
3511 -- The following is used to annotate a package specification which
3512 -- GNATprove should treat specially, because the axiomatization of
3513 -- this unit is given by the user instead of being automatically
3514 -- generated.
3516 -- pragma Annotate (GNATprove, External_Axiomatization);
3518 function Is_Annotate_Pragma_For_External_Axiomatization
3519 (N : Node_Id) return Boolean
3521 Name_GNATprove : constant String :=
3522 "gnatprove";
3523 Name_External_Axiomatization : constant String :=
3524 "external_axiomatization";
3525 -- Special names
3527 begin
3528 if Nkind (N) = N_Pragma
3529 and then Get_Pragma_Id (Pragma_Name (N)) = Pragma_Annotate
3530 and then List_Length (Pragma_Argument_Associations (N)) = 2
3531 then
3532 declare
3533 Arg1 : constant Node_Id :=
3534 First (Pragma_Argument_Associations (N));
3535 Arg2 : constant Node_Id := Next (Arg1);
3536 Nam1 : Name_Id;
3537 Nam2 : Name_Id;
3539 begin
3540 -- Fill in Name_Buffer with Name_GNATprove first, and then with
3541 -- Name_External_Axiomatization so that Name_Find returns the
3542 -- corresponding name. This takes care of all possible casings.
3544 Name_Len := 0;
3545 Add_Str_To_Name_Buffer (Name_GNATprove);
3546 Nam1 := Name_Find;
3548 Name_Len := 0;
3549 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
3550 Nam2 := Name_Find;
3552 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
3553 and then
3554 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
3555 end;
3557 else
3558 return False;
3559 end if;
3560 end Is_Annotate_Pragma_For_External_Axiomatization;
3562 -- Local variables
3564 Decl : Node_Id;
3565 Vis_Decls : List_Id;
3566 N : Node_Id;
3568 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
3570 begin
3571 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
3572 Decl := Parent (Parent (E));
3573 else
3574 Decl := Parent (E);
3575 end if;
3577 Vis_Decls := Visible_Declarations (Decl);
3579 N := First (Vis_Decls);
3580 while Present (N) loop
3582 -- Skip declarations generated by the frontend. Skip all pragmas
3583 -- that are not the desired Annotate pragma. Stop the search on
3584 -- the first non-pragma source declaration.
3586 if Comes_From_Source (N) then
3587 if Nkind (N) = N_Pragma then
3588 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
3589 return True;
3590 end if;
3591 else
3592 return False;
3593 end if;
3594 end if;
3596 Next (N);
3597 end loop;
3599 return False;
3600 end Has_Annotate_Pragma_For_External_Axiomatization;
3602 --------------------
3603 -- Homonym_Number --
3604 --------------------
3606 function Homonym_Number (Subp : Entity_Id) return Nat is
3607 Count : Nat;
3608 Hom : Entity_Id;
3610 begin
3611 Count := 1;
3612 Hom := Homonym (Subp);
3613 while Present (Hom) loop
3614 if Scope (Hom) = Scope (Subp) then
3615 Count := Count + 1;
3616 end if;
3618 Hom := Homonym (Hom);
3619 end loop;
3621 return Count;
3622 end Homonym_Number;
3624 -----------------------------------
3625 -- In_Library_Level_Package_Body --
3626 -----------------------------------
3628 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3629 begin
3630 -- First determine whether the entity appears at the library level, then
3631 -- look at the containing unit.
3633 if Is_Library_Level_Entity (Id) then
3634 declare
3635 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3637 begin
3638 return Nkind (Unit (Container)) = N_Package_Body;
3639 end;
3640 end if;
3642 return False;
3643 end In_Library_Level_Package_Body;
3645 ------------------------------
3646 -- In_Unconditional_Context --
3647 ------------------------------
3649 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3650 P : Node_Id;
3652 begin
3653 P := Node;
3654 while Present (P) loop
3655 case Nkind (P) is
3656 when N_Subprogram_Body =>
3657 return True;
3659 when N_If_Statement =>
3660 return False;
3662 when N_Loop_Statement =>
3663 return False;
3665 when N_Case_Statement =>
3666 return False;
3668 when others =>
3669 P := Parent (P);
3670 end case;
3671 end loop;
3673 return False;
3674 end In_Unconditional_Context;
3676 -------------------
3677 -- Insert_Action --
3678 -------------------
3680 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3681 begin
3682 if Present (Ins_Action) then
3683 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3684 end if;
3685 end Insert_Action;
3687 -- Version with check(s) suppressed
3689 procedure Insert_Action
3690 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3692 begin
3693 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3694 end Insert_Action;
3696 -------------------------
3697 -- Insert_Action_After --
3698 -------------------------
3700 procedure Insert_Action_After
3701 (Assoc_Node : Node_Id;
3702 Ins_Action : Node_Id)
3704 begin
3705 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3706 end Insert_Action_After;
3708 --------------------
3709 -- Insert_Actions --
3710 --------------------
3712 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3713 N : Node_Id;
3714 P : Node_Id;
3716 Wrapped_Node : Node_Id := Empty;
3718 begin
3719 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3720 return;
3721 end if;
3723 -- Ignore insert of actions from inside default expression (or other
3724 -- similar "spec expression") in the special spec-expression analyze
3725 -- mode. Any insertions at this point have no relevance, since we are
3726 -- only doing the analyze to freeze the types of any static expressions.
3727 -- See section "Handling of Default Expressions" in the spec of package
3728 -- Sem for further details.
3730 if In_Spec_Expression then
3731 return;
3732 end if;
3734 -- If the action derives from stuff inside a record, then the actions
3735 -- are attached to the current scope, to be inserted and analyzed on
3736 -- exit from the scope. The reason for this is that we may also be
3737 -- generating freeze actions at the same time, and they must eventually
3738 -- be elaborated in the correct order.
3740 if Is_Record_Type (Current_Scope)
3741 and then not Is_Frozen (Current_Scope)
3742 then
3743 if No (Scope_Stack.Table
3744 (Scope_Stack.Last).Pending_Freeze_Actions)
3745 then
3746 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3747 Ins_Actions;
3748 else
3749 Append_List
3750 (Ins_Actions,
3751 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3752 end if;
3754 return;
3755 end if;
3757 -- We now intend to climb up the tree to find the right point to
3758 -- insert the actions. We start at Assoc_Node, unless this node is a
3759 -- subexpression in which case we start with its parent. We do this for
3760 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3761 -- itself one of the special nodes like N_And_Then, then we assume that
3762 -- an initial request to insert actions for such a node does not expect
3763 -- the actions to get deposited in the node for later handling when the
3764 -- node is expanded, since clearly the node is being dealt with by the
3765 -- caller. Note that in the subexpression case, N is always the child we
3766 -- came from.
3768 -- N_Raise_xxx_Error is an annoying special case, it is a statement if
3769 -- it has type Standard_Void_Type, and a subexpression otherwise.
3770 -- otherwise. Procedure calls, and similarly procedure attribute
3771 -- references, are also statements.
3773 if Nkind (Assoc_Node) in N_Subexpr
3774 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
3775 or else Etype (Assoc_Node) /= Standard_Void_Type)
3776 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
3777 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
3778 or else not Is_Procedure_Attribute_Name
3779 (Attribute_Name (Assoc_Node)))
3780 then
3781 N := Assoc_Node;
3782 P := Parent (Assoc_Node);
3784 -- Non-subexpression case. Note that N is initially Empty in this case
3785 -- (N is only guaranteed Non-Empty in the subexpr case).
3787 else
3788 N := Empty;
3789 P := Assoc_Node;
3790 end if;
3792 -- Capture root of the transient scope
3794 if Scope_Is_Transient then
3795 Wrapped_Node := Node_To_Be_Wrapped;
3796 end if;
3798 loop
3799 pragma Assert (Present (P));
3801 -- Make sure that inserted actions stay in the transient scope
3803 if Present (Wrapped_Node) and then N = Wrapped_Node then
3804 Store_Before_Actions_In_Scope (Ins_Actions);
3805 return;
3806 end if;
3808 case Nkind (P) is
3810 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3811 -- in the Actions field of the right operand. They will be moved
3812 -- out further when the AND THEN or OR ELSE operator is expanded.
3813 -- Nothing special needs to be done for the left operand since
3814 -- in that case the actions are executed unconditionally.
3816 when N_Short_Circuit =>
3817 if N = Right_Opnd (P) then
3819 -- We are now going to either append the actions to the
3820 -- actions field of the short-circuit operation. We will
3821 -- also analyze the actions now.
3823 -- This analysis is really too early, the proper thing would
3824 -- be to just park them there now, and only analyze them if
3825 -- we find we really need them, and to it at the proper
3826 -- final insertion point. However attempting to this proved
3827 -- tricky, so for now we just kill current values before and
3828 -- after the analyze call to make sure we avoid peculiar
3829 -- optimizations from this out of order insertion.
3831 Kill_Current_Values;
3833 -- If P has already been expanded, we can't park new actions
3834 -- on it, so we need to expand them immediately, introducing
3835 -- an Expression_With_Actions. N can't be an expression
3836 -- with actions, or else then the actions would have been
3837 -- inserted at an inner level.
3839 if Analyzed (P) then
3840 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
3841 Rewrite (N,
3842 Make_Expression_With_Actions (Sloc (N),
3843 Actions => Ins_Actions,
3844 Expression => Relocate_Node (N)));
3845 Analyze_And_Resolve (N);
3847 elsif Present (Actions (P)) then
3848 Insert_List_After_And_Analyze
3849 (Last (Actions (P)), Ins_Actions);
3850 else
3851 Set_Actions (P, Ins_Actions);
3852 Analyze_List (Actions (P));
3853 end if;
3855 Kill_Current_Values;
3857 return;
3858 end if;
3860 -- Then or Else dependent expression of an if expression. Add
3861 -- actions to Then_Actions or Else_Actions field as appropriate.
3862 -- The actions will be moved further out when the if is expanded.
3864 when N_If_Expression =>
3865 declare
3866 ThenX : constant Node_Id := Next (First (Expressions (P)));
3867 ElseX : constant Node_Id := Next (ThenX);
3869 begin
3870 -- If the enclosing expression is already analyzed, as
3871 -- is the case for nested elaboration checks, insert the
3872 -- conditional further out.
3874 if Analyzed (P) then
3875 null;
3877 -- Actions belong to the then expression, temporarily place
3878 -- them as Then_Actions of the if expression. They will be
3879 -- moved to the proper place later when the if expression
3880 -- is expanded.
3882 elsif N = ThenX then
3883 if Present (Then_Actions (P)) then
3884 Insert_List_After_And_Analyze
3885 (Last (Then_Actions (P)), Ins_Actions);
3886 else
3887 Set_Then_Actions (P, Ins_Actions);
3888 Analyze_List (Then_Actions (P));
3889 end if;
3891 return;
3893 -- Actions belong to the else expression, temporarily place
3894 -- them as Else_Actions of the if expression. They will be
3895 -- moved to the proper place later when the if expression
3896 -- is expanded.
3898 elsif N = ElseX then
3899 if Present (Else_Actions (P)) then
3900 Insert_List_After_And_Analyze
3901 (Last (Else_Actions (P)), Ins_Actions);
3902 else
3903 Set_Else_Actions (P, Ins_Actions);
3904 Analyze_List (Else_Actions (P));
3905 end if;
3907 return;
3909 -- Actions belong to the condition. In this case they are
3910 -- unconditionally executed, and so we can continue the
3911 -- search for the proper insert point.
3913 else
3914 null;
3915 end if;
3916 end;
3918 -- Alternative of case expression, we place the action in the
3919 -- Actions field of the case expression alternative, this will
3920 -- be handled when the case expression is expanded.
3922 when N_Case_Expression_Alternative =>
3923 if Present (Actions (P)) then
3924 Insert_List_After_And_Analyze
3925 (Last (Actions (P)), Ins_Actions);
3926 else
3927 Set_Actions (P, Ins_Actions);
3928 Analyze_List (Actions (P));
3929 end if;
3931 return;
3933 -- Case of appearing within an Expressions_With_Actions node. When
3934 -- the new actions come from the expression of the expression with
3935 -- actions, they must be added to the existing actions. The other
3936 -- alternative is when the new actions are related to one of the
3937 -- existing actions of the expression with actions, and should
3938 -- never reach here: if actions are inserted on a statement
3939 -- within the Actions of an expression with actions, or on some
3940 -- sub-expression of such a statement, then the outermost proper
3941 -- insertion point is right before the statement, and we should
3942 -- never climb up as far as the N_Expression_With_Actions itself.
3944 when N_Expression_With_Actions =>
3945 if N = Expression (P) then
3946 if Is_Empty_List (Actions (P)) then
3947 Append_List_To (Actions (P), Ins_Actions);
3948 Analyze_List (Actions (P));
3949 else
3950 Insert_List_After_And_Analyze
3951 (Last (Actions (P)), Ins_Actions);
3952 end if;
3954 return;
3956 else
3957 raise Program_Error;
3958 end if;
3960 -- Case of appearing in the condition of a while expression or
3961 -- elsif. We insert the actions into the Condition_Actions field.
3962 -- They will be moved further out when the while loop or elsif
3963 -- is analyzed.
3965 when N_Iteration_Scheme |
3966 N_Elsif_Part
3968 if N = Condition (P) then
3969 if Present (Condition_Actions (P)) then
3970 Insert_List_After_And_Analyze
3971 (Last (Condition_Actions (P)), Ins_Actions);
3972 else
3973 Set_Condition_Actions (P, Ins_Actions);
3975 -- Set the parent of the insert actions explicitly. This
3976 -- is not a syntactic field, but we need the parent field
3977 -- set, in particular so that freeze can understand that
3978 -- it is dealing with condition actions, and properly
3979 -- insert the freezing actions.
3981 Set_Parent (Ins_Actions, P);
3982 Analyze_List (Condition_Actions (P));
3983 end if;
3985 return;
3986 end if;
3988 -- Statements, declarations, pragmas, representation clauses
3990 when
3991 -- Statements
3993 N_Procedure_Call_Statement |
3994 N_Statement_Other_Than_Procedure_Call |
3996 -- Pragmas
3998 N_Pragma |
4000 -- Representation_Clause
4002 N_At_Clause |
4003 N_Attribute_Definition_Clause |
4004 N_Enumeration_Representation_Clause |
4005 N_Record_Representation_Clause |
4007 -- Declarations
4009 N_Abstract_Subprogram_Declaration |
4010 N_Entry_Body |
4011 N_Exception_Declaration |
4012 N_Exception_Renaming_Declaration |
4013 N_Expression_Function |
4014 N_Formal_Abstract_Subprogram_Declaration |
4015 N_Formal_Concrete_Subprogram_Declaration |
4016 N_Formal_Object_Declaration |
4017 N_Formal_Type_Declaration |
4018 N_Full_Type_Declaration |
4019 N_Function_Instantiation |
4020 N_Generic_Function_Renaming_Declaration |
4021 N_Generic_Package_Declaration |
4022 N_Generic_Package_Renaming_Declaration |
4023 N_Generic_Procedure_Renaming_Declaration |
4024 N_Generic_Subprogram_Declaration |
4025 N_Implicit_Label_Declaration |
4026 N_Incomplete_Type_Declaration |
4027 N_Number_Declaration |
4028 N_Object_Declaration |
4029 N_Object_Renaming_Declaration |
4030 N_Package_Body |
4031 N_Package_Body_Stub |
4032 N_Package_Declaration |
4033 N_Package_Instantiation |
4034 N_Package_Renaming_Declaration |
4035 N_Private_Extension_Declaration |
4036 N_Private_Type_Declaration |
4037 N_Procedure_Instantiation |
4038 N_Protected_Body |
4039 N_Protected_Body_Stub |
4040 N_Protected_Type_Declaration |
4041 N_Single_Task_Declaration |
4042 N_Subprogram_Body |
4043 N_Subprogram_Body_Stub |
4044 N_Subprogram_Declaration |
4045 N_Subprogram_Renaming_Declaration |
4046 N_Subtype_Declaration |
4047 N_Task_Body |
4048 N_Task_Body_Stub |
4049 N_Task_Type_Declaration |
4051 -- Use clauses can appear in lists of declarations
4053 N_Use_Package_Clause |
4054 N_Use_Type_Clause |
4056 -- Freeze entity behaves like a declaration or statement
4058 N_Freeze_Entity |
4059 N_Freeze_Generic_Entity
4061 -- Do not insert here if the item is not a list member (this
4062 -- happens for example with a triggering statement, and the
4063 -- proper approach is to insert before the entire select).
4065 if not Is_List_Member (P) then
4066 null;
4068 -- Do not insert if parent of P is an N_Component_Association
4069 -- node (i.e. we are in the context of an N_Aggregate or
4070 -- N_Extension_Aggregate node. In this case we want to insert
4071 -- before the entire aggregate.
4073 elsif Nkind (Parent (P)) = N_Component_Association then
4074 null;
4076 -- Do not insert if the parent of P is either an N_Variant node
4077 -- or an N_Record_Definition node, meaning in either case that
4078 -- P is a member of a component list, and that therefore the
4079 -- actions should be inserted outside the complete record
4080 -- declaration.
4082 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
4083 null;
4085 -- Do not insert freeze nodes within the loop generated for
4086 -- an aggregate, because they may be elaborated too late for
4087 -- subsequent use in the back end: within a package spec the
4088 -- loop is part of the elaboration procedure and is only
4089 -- elaborated during the second pass.
4091 -- If the loop comes from source, or the entity is local to the
4092 -- loop itself it must remain within.
4094 elsif Nkind (Parent (P)) = N_Loop_Statement
4095 and then not Comes_From_Source (Parent (P))
4096 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
4097 and then
4098 Scope (Entity (First (Ins_Actions))) /= Current_Scope
4099 then
4100 null;
4102 -- Otherwise we can go ahead and do the insertion
4104 elsif P = Wrapped_Node then
4105 Store_Before_Actions_In_Scope (Ins_Actions);
4106 return;
4108 else
4109 Insert_List_Before_And_Analyze (P, Ins_Actions);
4110 return;
4111 end if;
4113 -- A special case, N_Raise_xxx_Error can act either as a statement
4114 -- or a subexpression. We tell the difference by looking at the
4115 -- Etype. It is set to Standard_Void_Type in the statement case.
4117 when
4118 N_Raise_xxx_Error =>
4119 if Etype (P) = Standard_Void_Type then
4120 if P = Wrapped_Node then
4121 Store_Before_Actions_In_Scope (Ins_Actions);
4122 else
4123 Insert_List_Before_And_Analyze (P, Ins_Actions);
4124 end if;
4126 return;
4128 -- In the subexpression case, keep climbing
4130 else
4131 null;
4132 end if;
4134 -- If a component association appears within a loop created for
4135 -- an array aggregate, attach the actions to the association so
4136 -- they can be subsequently inserted within the loop. For other
4137 -- component associations insert outside of the aggregate. For
4138 -- an association that will generate a loop, its Loop_Actions
4139 -- attribute is already initialized (see exp_aggr.adb).
4141 -- The list of loop_actions can in turn generate additional ones,
4142 -- that are inserted before the associated node. If the associated
4143 -- node is outside the aggregate, the new actions are collected
4144 -- at the end of the loop actions, to respect the order in which
4145 -- they are to be elaborated.
4147 when
4148 N_Component_Association =>
4149 if Nkind (Parent (P)) = N_Aggregate
4150 and then Present (Loop_Actions (P))
4151 then
4152 if Is_Empty_List (Loop_Actions (P)) then
4153 Set_Loop_Actions (P, Ins_Actions);
4154 Analyze_List (Ins_Actions);
4156 else
4157 declare
4158 Decl : Node_Id;
4160 begin
4161 -- Check whether these actions were generated by a
4162 -- declaration that is part of the loop_ actions
4163 -- for the component_association.
4165 Decl := Assoc_Node;
4166 while Present (Decl) loop
4167 exit when Parent (Decl) = P
4168 and then Is_List_Member (Decl)
4169 and then
4170 List_Containing (Decl) = Loop_Actions (P);
4171 Decl := Parent (Decl);
4172 end loop;
4174 if Present (Decl) then
4175 Insert_List_Before_And_Analyze
4176 (Decl, Ins_Actions);
4177 else
4178 Insert_List_After_And_Analyze
4179 (Last (Loop_Actions (P)), Ins_Actions);
4180 end if;
4181 end;
4182 end if;
4184 return;
4186 else
4187 null;
4188 end if;
4190 -- Another special case, an attribute denoting a procedure call
4192 when
4193 N_Attribute_Reference =>
4194 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
4195 if P = Wrapped_Node then
4196 Store_Before_Actions_In_Scope (Ins_Actions);
4197 else
4198 Insert_List_Before_And_Analyze (P, Ins_Actions);
4199 end if;
4201 return;
4203 -- In the subexpression case, keep climbing
4205 else
4206 null;
4207 end if;
4209 -- A contract node should not belong to the tree
4211 when N_Contract =>
4212 raise Program_Error;
4214 -- For all other node types, keep climbing tree
4216 when
4217 N_Abortable_Part |
4218 N_Accept_Alternative |
4219 N_Access_Definition |
4220 N_Access_Function_Definition |
4221 N_Access_Procedure_Definition |
4222 N_Access_To_Object_Definition |
4223 N_Aggregate |
4224 N_Allocator |
4225 N_Aspect_Specification |
4226 N_Case_Expression |
4227 N_Case_Statement_Alternative |
4228 N_Character_Literal |
4229 N_Compilation_Unit |
4230 N_Compilation_Unit_Aux |
4231 N_Component_Clause |
4232 N_Component_Declaration |
4233 N_Component_Definition |
4234 N_Component_List |
4235 N_Constrained_Array_Definition |
4236 N_Decimal_Fixed_Point_Definition |
4237 N_Defining_Character_Literal |
4238 N_Defining_Identifier |
4239 N_Defining_Operator_Symbol |
4240 N_Defining_Program_Unit_Name |
4241 N_Delay_Alternative |
4242 N_Delta_Constraint |
4243 N_Derived_Type_Definition |
4244 N_Designator |
4245 N_Digits_Constraint |
4246 N_Discriminant_Association |
4247 N_Discriminant_Specification |
4248 N_Empty |
4249 N_Entry_Body_Formal_Part |
4250 N_Entry_Call_Alternative |
4251 N_Entry_Declaration |
4252 N_Entry_Index_Specification |
4253 N_Enumeration_Type_Definition |
4254 N_Error |
4255 N_Exception_Handler |
4256 N_Expanded_Name |
4257 N_Explicit_Dereference |
4258 N_Extension_Aggregate |
4259 N_Floating_Point_Definition |
4260 N_Formal_Decimal_Fixed_Point_Definition |
4261 N_Formal_Derived_Type_Definition |
4262 N_Formal_Discrete_Type_Definition |
4263 N_Formal_Floating_Point_Definition |
4264 N_Formal_Modular_Type_Definition |
4265 N_Formal_Ordinary_Fixed_Point_Definition |
4266 N_Formal_Package_Declaration |
4267 N_Formal_Private_Type_Definition |
4268 N_Formal_Incomplete_Type_Definition |
4269 N_Formal_Signed_Integer_Type_Definition |
4270 N_Function_Call |
4271 N_Function_Specification |
4272 N_Generic_Association |
4273 N_Handled_Sequence_Of_Statements |
4274 N_Identifier |
4275 N_In |
4276 N_Index_Or_Discriminant_Constraint |
4277 N_Indexed_Component |
4278 N_Integer_Literal |
4279 N_Iterator_Specification |
4280 N_Itype_Reference |
4281 N_Label |
4282 N_Loop_Parameter_Specification |
4283 N_Mod_Clause |
4284 N_Modular_Type_Definition |
4285 N_Not_In |
4286 N_Null |
4287 N_Op_Abs |
4288 N_Op_Add |
4289 N_Op_And |
4290 N_Op_Concat |
4291 N_Op_Divide |
4292 N_Op_Eq |
4293 N_Op_Expon |
4294 N_Op_Ge |
4295 N_Op_Gt |
4296 N_Op_Le |
4297 N_Op_Lt |
4298 N_Op_Minus |
4299 N_Op_Mod |
4300 N_Op_Multiply |
4301 N_Op_Ne |
4302 N_Op_Not |
4303 N_Op_Or |
4304 N_Op_Plus |
4305 N_Op_Rem |
4306 N_Op_Rotate_Left |
4307 N_Op_Rotate_Right |
4308 N_Op_Shift_Left |
4309 N_Op_Shift_Right |
4310 N_Op_Shift_Right_Arithmetic |
4311 N_Op_Subtract |
4312 N_Op_Xor |
4313 N_Operator_Symbol |
4314 N_Ordinary_Fixed_Point_Definition |
4315 N_Others_Choice |
4316 N_Package_Specification |
4317 N_Parameter_Association |
4318 N_Parameter_Specification |
4319 N_Pop_Constraint_Error_Label |
4320 N_Pop_Program_Error_Label |
4321 N_Pop_Storage_Error_Label |
4322 N_Pragma_Argument_Association |
4323 N_Procedure_Specification |
4324 N_Protected_Definition |
4325 N_Push_Constraint_Error_Label |
4326 N_Push_Program_Error_Label |
4327 N_Push_Storage_Error_Label |
4328 N_Qualified_Expression |
4329 N_Quantified_Expression |
4330 N_Raise_Expression |
4331 N_Range |
4332 N_Range_Constraint |
4333 N_Real_Literal |
4334 N_Real_Range_Specification |
4335 N_Record_Definition |
4336 N_Reference |
4337 N_SCIL_Dispatch_Table_Tag_Init |
4338 N_SCIL_Dispatching_Call |
4339 N_SCIL_Membership_Test |
4340 N_Selected_Component |
4341 N_Signed_Integer_Type_Definition |
4342 N_Single_Protected_Declaration |
4343 N_Slice |
4344 N_String_Literal |
4345 N_Subtype_Indication |
4346 N_Subunit |
4347 N_Task_Definition |
4348 N_Terminate_Alternative |
4349 N_Triggering_Alternative |
4350 N_Type_Conversion |
4351 N_Unchecked_Expression |
4352 N_Unchecked_Type_Conversion |
4353 N_Unconstrained_Array_Definition |
4354 N_Unused_At_End |
4355 N_Unused_At_Start |
4356 N_Variant |
4357 N_Variant_Part |
4358 N_Validate_Unchecked_Conversion |
4359 N_With_Clause
4361 null;
4363 end case;
4365 -- If we fall through above tests, keep climbing tree
4367 N := P;
4369 if Nkind (Parent (N)) = N_Subunit then
4371 -- This is the proper body corresponding to a stub. Insertion must
4372 -- be done at the point of the stub, which is in the declarative
4373 -- part of the parent unit.
4375 P := Corresponding_Stub (Parent (N));
4377 else
4378 P := Parent (N);
4379 end if;
4380 end loop;
4381 end Insert_Actions;
4383 -- Version with check(s) suppressed
4385 procedure Insert_Actions
4386 (Assoc_Node : Node_Id;
4387 Ins_Actions : List_Id;
4388 Suppress : Check_Id)
4390 begin
4391 if Suppress = All_Checks then
4392 declare
4393 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
4394 begin
4395 Scope_Suppress.Suppress := (others => True);
4396 Insert_Actions (Assoc_Node, Ins_Actions);
4397 Scope_Suppress.Suppress := Sva;
4398 end;
4400 else
4401 declare
4402 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
4403 begin
4404 Scope_Suppress.Suppress (Suppress) := True;
4405 Insert_Actions (Assoc_Node, Ins_Actions);
4406 Scope_Suppress.Suppress (Suppress) := Svg;
4407 end;
4408 end if;
4409 end Insert_Actions;
4411 --------------------------
4412 -- Insert_Actions_After --
4413 --------------------------
4415 procedure Insert_Actions_After
4416 (Assoc_Node : Node_Id;
4417 Ins_Actions : List_Id)
4419 begin
4420 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
4421 Store_After_Actions_In_Scope (Ins_Actions);
4422 else
4423 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
4424 end if;
4425 end Insert_Actions_After;
4427 ------------------------
4428 -- Insert_Declaration --
4429 ------------------------
4431 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
4432 P : Node_Id;
4434 begin
4435 pragma Assert (Nkind (N) in N_Subexpr);
4437 -- Climb until we find a procedure or a package
4439 P := N;
4440 loop
4441 pragma Assert (Present (Parent (P)));
4442 P := Parent (P);
4444 if Is_List_Member (P) then
4445 exit when Nkind_In (Parent (P), N_Package_Specification,
4446 N_Subprogram_Body);
4448 -- Special handling for handled sequence of statements, we must
4449 -- insert in the statements not the exception handlers!
4451 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
4452 P := First (Statements (Parent (P)));
4453 exit;
4454 end if;
4455 end if;
4456 end loop;
4458 -- Now do the insertion
4460 Insert_Before (P, Decl);
4461 Analyze (Decl);
4462 end Insert_Declaration;
4464 ---------------------------------
4465 -- Insert_Library_Level_Action --
4466 ---------------------------------
4468 procedure Insert_Library_Level_Action (N : Node_Id) is
4469 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4471 begin
4472 Push_Scope (Cunit_Entity (Main_Unit));
4473 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
4475 if No (Actions (Aux)) then
4476 Set_Actions (Aux, New_List (N));
4477 else
4478 Append (N, Actions (Aux));
4479 end if;
4481 Analyze (N);
4482 Pop_Scope;
4483 end Insert_Library_Level_Action;
4485 ----------------------------------
4486 -- Insert_Library_Level_Actions --
4487 ----------------------------------
4489 procedure Insert_Library_Level_Actions (L : List_Id) is
4490 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4492 begin
4493 if Is_Non_Empty_List (L) then
4494 Push_Scope (Cunit_Entity (Main_Unit));
4495 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
4497 if No (Actions (Aux)) then
4498 Set_Actions (Aux, L);
4499 Analyze_List (L);
4500 else
4501 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
4502 end if;
4504 Pop_Scope;
4505 end if;
4506 end Insert_Library_Level_Actions;
4508 ----------------------
4509 -- Inside_Init_Proc --
4510 ----------------------
4512 function Inside_Init_Proc return Boolean is
4513 S : Entity_Id;
4515 begin
4516 S := Current_Scope;
4517 while Present (S) and then S /= Standard_Standard loop
4518 if Is_Init_Proc (S) then
4519 return True;
4520 else
4521 S := Scope (S);
4522 end if;
4523 end loop;
4525 return False;
4526 end Inside_Init_Proc;
4528 ----------------------------
4529 -- Is_All_Null_Statements --
4530 ----------------------------
4532 function Is_All_Null_Statements (L : List_Id) return Boolean is
4533 Stm : Node_Id;
4535 begin
4536 Stm := First (L);
4537 while Present (Stm) loop
4538 if Nkind (Stm) /= N_Null_Statement then
4539 return False;
4540 end if;
4542 Next (Stm);
4543 end loop;
4545 return True;
4546 end Is_All_Null_Statements;
4548 --------------------------------------------------
4549 -- Is_Displacement_Of_Object_Or_Function_Result --
4550 --------------------------------------------------
4552 function Is_Displacement_Of_Object_Or_Function_Result
4553 (Obj_Id : Entity_Id) return Boolean
4555 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
4556 -- Determine if particular node denotes a controlled function call. The
4557 -- call may have been heavily expanded.
4559 function Is_Displace_Call (N : Node_Id) return Boolean;
4560 -- Determine whether a particular node is a call to Ada.Tags.Displace.
4561 -- The call might be nested within other actions such as conversions.
4563 function Is_Source_Object (N : Node_Id) return Boolean;
4564 -- Determine whether a particular node denotes a source object
4566 ---------------------------------
4567 -- Is_Controlled_Function_Call --
4568 ---------------------------------
4570 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
4571 Expr : Node_Id := Original_Node (N);
4573 begin
4574 if Nkind (Expr) = N_Function_Call then
4575 Expr := Name (Expr);
4577 -- When a function call appears in Object.Operation format, the
4578 -- original representation has two possible forms depending on the
4579 -- availability of actual parameters:
4581 -- Obj.Func_Call N_Selected_Component
4582 -- Obj.Func_Call (Param) N_Indexed_Component
4584 else
4585 if Nkind (Expr) = N_Indexed_Component then
4586 Expr := Prefix (Expr);
4587 end if;
4589 if Nkind (Expr) = N_Selected_Component then
4590 Expr := Selector_Name (Expr);
4591 end if;
4592 end if;
4594 return
4595 Nkind_In (Expr, N_Expanded_Name, N_Identifier)
4596 and then Ekind (Entity (Expr)) = E_Function
4597 and then Needs_Finalization (Etype (Entity (Expr)));
4598 end Is_Controlled_Function_Call;
4600 ----------------------
4601 -- Is_Displace_Call --
4602 ----------------------
4604 function Is_Displace_Call (N : Node_Id) return Boolean is
4605 Call : Node_Id := N;
4607 begin
4608 -- Strip various actions which may precede a call to Displace
4610 loop
4611 if Nkind (Call) = N_Explicit_Dereference then
4612 Call := Prefix (Call);
4614 elsif Nkind_In (Call, N_Type_Conversion,
4615 N_Unchecked_Type_Conversion)
4616 then
4617 Call := Expression (Call);
4619 else
4620 exit;
4621 end if;
4622 end loop;
4624 return
4625 Present (Call)
4626 and then Nkind (Call) = N_Function_Call
4627 and then Is_RTE (Entity (Name (Call)), RE_Displace);
4628 end Is_Displace_Call;
4630 ----------------------
4631 -- Is_Source_Object --
4632 ----------------------
4634 function Is_Source_Object (N : Node_Id) return Boolean is
4635 begin
4636 return
4637 Present (N)
4638 and then Nkind (N) in N_Has_Entity
4639 and then Is_Object (Entity (N))
4640 and then Comes_From_Source (N);
4641 end Is_Source_Object;
4643 -- Local variables
4645 Decl : constant Node_Id := Parent (Obj_Id);
4646 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4647 Orig_Decl : constant Node_Id := Original_Node (Decl);
4649 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
4651 begin
4652 -- Case 1:
4654 -- Obj : CW_Type := Function_Call (...);
4656 -- rewritten into:
4658 -- Tmp : ... := Function_Call (...)'reference;
4659 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
4661 -- where the return type of the function and the class-wide type require
4662 -- dispatch table pointer displacement.
4664 -- Case 2:
4666 -- Obj : CW_Type := Src_Obj;
4668 -- rewritten into:
4670 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
4672 -- where the type of the source object and the class-wide type require
4673 -- dispatch table pointer displacement.
4675 return
4676 Nkind (Decl) = N_Object_Renaming_Declaration
4677 and then Nkind (Orig_Decl) = N_Object_Declaration
4678 and then Comes_From_Source (Orig_Decl)
4679 and then Is_Class_Wide_Type (Obj_Typ)
4680 and then Is_Displace_Call (Renamed_Object (Obj_Id))
4681 and then
4682 (Is_Controlled_Function_Call (Expression (Orig_Decl))
4683 or else Is_Source_Object (Expression (Orig_Decl)));
4684 end Is_Displacement_Of_Object_Or_Function_Result;
4686 ------------------------------
4687 -- Is_Finalizable_Transient --
4688 ------------------------------
4690 function Is_Finalizable_Transient
4691 (Decl : Node_Id;
4692 Rel_Node : Node_Id) return Boolean
4694 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4695 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4696 Desig : Entity_Id := Obj_Typ;
4698 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
4699 -- Determine whether transient object Trans_Id is initialized either
4700 -- by a function call which returns an access type or simply renames
4701 -- another pointer.
4703 function Initialized_By_Aliased_BIP_Func_Call
4704 (Trans_Id : Entity_Id) return Boolean;
4705 -- Determine whether transient object Trans_Id is initialized by a
4706 -- build-in-place function call where the BIPalloc parameter is of
4707 -- value 1 and BIPaccess is not null. This case creates an aliasing
4708 -- between the returned value and the value denoted by BIPaccess.
4710 function Is_Aliased
4711 (Trans_Id : Entity_Id;
4712 First_Stmt : Node_Id) return Boolean;
4713 -- Determine whether transient object Trans_Id has been renamed or
4714 -- aliased through 'reference in the statement list starting from
4715 -- First_Stmt.
4717 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4718 -- Determine whether transient object Trans_Id is allocated on the heap
4720 function Is_Iterated_Container
4721 (Trans_Id : Entity_Id;
4722 First_Stmt : Node_Id) return Boolean;
4723 -- Determine whether transient object Trans_Id denotes a container which
4724 -- is in the process of being iterated in the statement list starting
4725 -- from First_Stmt.
4727 ---------------------------
4728 -- Initialized_By_Access --
4729 ---------------------------
4731 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4732 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4734 begin
4735 return
4736 Present (Expr)
4737 and then Nkind (Expr) /= N_Reference
4738 and then Is_Access_Type (Etype (Expr));
4739 end Initialized_By_Access;
4741 ------------------------------------------
4742 -- Initialized_By_Aliased_BIP_Func_Call --
4743 ------------------------------------------
4745 function Initialized_By_Aliased_BIP_Func_Call
4746 (Trans_Id : Entity_Id) return Boolean
4748 Call : Node_Id := Expression (Parent (Trans_Id));
4750 begin
4751 -- Build-in-place calls usually appear in 'reference format
4753 if Nkind (Call) = N_Reference then
4754 Call := Prefix (Call);
4755 end if;
4757 if Is_Build_In_Place_Function_Call (Call) then
4758 declare
4759 Access_Nam : Name_Id := No_Name;
4760 Access_OK : Boolean := False;
4761 Actual : Node_Id;
4762 Alloc_Nam : Name_Id := No_Name;
4763 Alloc_OK : Boolean := False;
4764 Formal : Node_Id;
4765 Func_Id : Entity_Id;
4766 Param : Node_Id;
4768 begin
4769 -- Examine all parameter associations of the function call
4771 Param := First (Parameter_Associations (Call));
4772 while Present (Param) loop
4773 if Nkind (Param) = N_Parameter_Association
4774 and then Nkind (Selector_Name (Param)) = N_Identifier
4775 then
4776 Actual := Explicit_Actual_Parameter (Param);
4777 Formal := Selector_Name (Param);
4779 -- Construct the names of formals BIPaccess and BIPalloc
4780 -- using the function name retrieved from an arbitrary
4781 -- formal.
4783 if Access_Nam = No_Name
4784 and then Alloc_Nam = No_Name
4785 and then Present (Entity (Formal))
4786 then
4787 Func_Id := Scope (Entity (Formal));
4789 Access_Nam :=
4790 New_External_Name (Chars (Func_Id),
4791 BIP_Formal_Suffix (BIP_Object_Access));
4793 Alloc_Nam :=
4794 New_External_Name (Chars (Func_Id),
4795 BIP_Formal_Suffix (BIP_Alloc_Form));
4796 end if;
4798 -- A match for BIPaccess => Temp has been found
4800 if Chars (Formal) = Access_Nam
4801 and then Nkind (Actual) /= N_Null
4802 then
4803 Access_OK := True;
4804 end if;
4806 -- A match for BIPalloc => 1 has been found
4808 if Chars (Formal) = Alloc_Nam
4809 and then Nkind (Actual) = N_Integer_Literal
4810 and then Intval (Actual) = Uint_1
4811 then
4812 Alloc_OK := True;
4813 end if;
4814 end if;
4816 Next (Param);
4817 end loop;
4819 return Access_OK and Alloc_OK;
4820 end;
4821 end if;
4823 return False;
4824 end Initialized_By_Aliased_BIP_Func_Call;
4826 ----------------
4827 -- Is_Aliased --
4828 ----------------
4830 function Is_Aliased
4831 (Trans_Id : Entity_Id;
4832 First_Stmt : Node_Id) return Boolean
4834 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
4835 -- Given an object renaming declaration, retrieve the entity of the
4836 -- renamed name. Return Empty if the renamed name is anything other
4837 -- than a variable or a constant.
4839 -------------------------
4840 -- Find_Renamed_Object --
4841 -------------------------
4843 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
4844 Ren_Obj : Node_Id := Empty;
4846 function Find_Object (N : Node_Id) return Traverse_Result;
4847 -- Try to detect an object which is either a constant or a
4848 -- variable.
4850 -----------------
4851 -- Find_Object --
4852 -----------------
4854 function Find_Object (N : Node_Id) return Traverse_Result is
4855 begin
4856 -- Stop the search once a constant or a variable has been
4857 -- detected.
4859 if Nkind (N) = N_Identifier
4860 and then Present (Entity (N))
4861 and then Ekind_In (Entity (N), E_Constant, E_Variable)
4862 then
4863 Ren_Obj := Entity (N);
4864 return Abandon;
4865 end if;
4867 return OK;
4868 end Find_Object;
4870 procedure Search is new Traverse_Proc (Find_Object);
4872 -- Local variables
4874 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
4876 -- Start of processing for Find_Renamed_Object
4878 begin
4879 -- Actions related to dispatching calls may appear as renamings of
4880 -- tags. Do not process this type of renaming because it does not
4881 -- use the actual value of the object.
4883 if not Is_RTE (Typ, RE_Tag_Ptr) then
4884 Search (Name (Ren_Decl));
4885 end if;
4887 return Ren_Obj;
4888 end Find_Renamed_Object;
4890 -- Local variables
4892 Expr : Node_Id;
4893 Ren_Obj : Entity_Id;
4894 Stmt : Node_Id;
4896 -- Start of processing for Is_Aliased
4898 begin
4899 Stmt := First_Stmt;
4900 while Present (Stmt) loop
4901 if Nkind (Stmt) = N_Object_Declaration then
4902 Expr := Expression (Stmt);
4904 if Present (Expr)
4905 and then Nkind (Expr) = N_Reference
4906 and then Nkind (Prefix (Expr)) = N_Identifier
4907 and then Entity (Prefix (Expr)) = Trans_Id
4908 then
4909 return True;
4910 end if;
4912 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
4913 Ren_Obj := Find_Renamed_Object (Stmt);
4915 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
4916 return True;
4917 end if;
4918 end if;
4920 Next (Stmt);
4921 end loop;
4923 return False;
4924 end Is_Aliased;
4926 ------------------
4927 -- Is_Allocated --
4928 ------------------
4930 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
4931 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4932 begin
4933 return
4934 Is_Access_Type (Etype (Trans_Id))
4935 and then Present (Expr)
4936 and then Nkind (Expr) = N_Allocator;
4937 end Is_Allocated;
4939 ---------------------------
4940 -- Is_Iterated_Container --
4941 ---------------------------
4943 function Is_Iterated_Container
4944 (Trans_Id : Entity_Id;
4945 First_Stmt : Node_Id) return Boolean
4947 Aspect : Node_Id;
4948 Call : Node_Id;
4949 Iter : Entity_Id;
4950 Param : Node_Id;
4951 Stmt : Node_Id;
4952 Typ : Entity_Id;
4954 begin
4955 -- It is not possible to iterate over containers in non-Ada 2012 code
4957 if Ada_Version < Ada_2012 then
4958 return False;
4959 end if;
4961 Typ := Etype (Trans_Id);
4963 -- Handle access type created for secondary stack use
4965 if Is_Access_Type (Typ) then
4966 Typ := Designated_Type (Typ);
4967 end if;
4969 -- Look for aspect Default_Iterator. It may be part of a type
4970 -- declaration for a container, or inherited from a base type
4971 -- or parent type.
4973 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
4975 if Present (Aspect) then
4976 Iter := Entity (Aspect);
4978 -- Examine the statements following the container object and
4979 -- look for a call to the default iterate routine where the
4980 -- first parameter is the transient. Such a call appears as:
4982 -- It : Access_To_CW_Iterator :=
4983 -- Iterate (Tran_Id.all, ...)'reference;
4985 Stmt := First_Stmt;
4986 while Present (Stmt) loop
4988 -- Detect an object declaration which is initialized by a
4989 -- secondary stack function call.
4991 if Nkind (Stmt) = N_Object_Declaration
4992 and then Present (Expression (Stmt))
4993 and then Nkind (Expression (Stmt)) = N_Reference
4994 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
4995 then
4996 Call := Prefix (Expression (Stmt));
4998 -- The call must invoke the default iterate routine of
4999 -- the container and the transient object must appear as
5000 -- the first actual parameter. Skip any calls whose names
5001 -- are not entities.
5003 if Is_Entity_Name (Name (Call))
5004 and then Entity (Name (Call)) = Iter
5005 and then Present (Parameter_Associations (Call))
5006 then
5007 Param := First (Parameter_Associations (Call));
5009 if Nkind (Param) = N_Explicit_Dereference
5010 and then Entity (Prefix (Param)) = Trans_Id
5011 then
5012 return True;
5013 end if;
5014 end if;
5015 end if;
5017 Next (Stmt);
5018 end loop;
5019 end if;
5021 return False;
5022 end Is_Iterated_Container;
5024 -- Start of processing for Is_Finalizable_Transient
5026 begin
5027 -- Handle access types
5029 if Is_Access_Type (Desig) then
5030 Desig := Available_View (Designated_Type (Desig));
5031 end if;
5033 return
5034 Ekind_In (Obj_Id, E_Constant, E_Variable)
5035 and then Needs_Finalization (Desig)
5036 and then Requires_Transient_Scope (Desig)
5037 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
5039 -- Do not consider renamed or 'reference-d transient objects because
5040 -- the act of renaming extends the object's lifetime.
5042 and then not Is_Aliased (Obj_Id, Decl)
5044 -- Do not consider transient objects allocated on the heap since
5045 -- they are attached to a finalization master.
5047 and then not Is_Allocated (Obj_Id)
5049 -- If the transient object is a pointer, check that it is not
5050 -- initialized by a function which returns a pointer or acts as a
5051 -- renaming of another pointer.
5053 and then
5054 (not Is_Access_Type (Obj_Typ)
5055 or else not Initialized_By_Access (Obj_Id))
5057 -- Do not consider transient objects which act as indirect aliases
5058 -- of build-in-place function results.
5060 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
5062 -- Do not consider conversions of tags to class-wide types
5064 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
5066 -- Do not consider containers in the context of iterator loops. Such
5067 -- transient objects must exist for as long as the loop is around,
5068 -- otherwise any operation carried out by the iterator will fail.
5070 and then not Is_Iterated_Container (Obj_Id, Decl);
5071 end Is_Finalizable_Transient;
5073 ---------------------------------
5074 -- Is_Fully_Repped_Tagged_Type --
5075 ---------------------------------
5077 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
5078 U : constant Entity_Id := Underlying_Type (T);
5079 Comp : Entity_Id;
5081 begin
5082 if No (U) or else not Is_Tagged_Type (U) then
5083 return False;
5084 elsif Has_Discriminants (U) then
5085 return False;
5086 elsif not Has_Specified_Layout (U) then
5087 return False;
5088 end if;
5090 -- Here we have a tagged type, see if it has any unlayed out fields
5091 -- other than a possible tag and parent fields. If so, we return False.
5093 Comp := First_Component (U);
5094 while Present (Comp) loop
5095 if not Is_Tag (Comp)
5096 and then Chars (Comp) /= Name_uParent
5097 and then No (Component_Clause (Comp))
5098 then
5099 return False;
5100 else
5101 Next_Component (Comp);
5102 end if;
5103 end loop;
5105 -- All components are layed out
5107 return True;
5108 end Is_Fully_Repped_Tagged_Type;
5110 ----------------------------------
5111 -- Is_Library_Level_Tagged_Type --
5112 ----------------------------------
5114 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
5115 begin
5116 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
5117 end Is_Library_Level_Tagged_Type;
5119 --------------------------
5120 -- Is_Non_BIP_Func_Call --
5121 --------------------------
5123 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
5124 begin
5125 -- The expected call is of the format
5127 -- Func_Call'reference
5129 return
5130 Nkind (Expr) = N_Reference
5131 and then Nkind (Prefix (Expr)) = N_Function_Call
5132 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
5133 end Is_Non_BIP_Func_Call;
5135 ------------------------------------
5136 -- Is_Object_Access_BIP_Func_Call --
5137 ------------------------------------
5139 function Is_Object_Access_BIP_Func_Call
5140 (Expr : Node_Id;
5141 Obj_Id : Entity_Id) return Boolean
5143 Access_Nam : Name_Id := No_Name;
5144 Actual : Node_Id;
5145 Call : Node_Id;
5146 Formal : Node_Id;
5147 Param : Node_Id;
5149 begin
5150 -- Build-in-place calls usually appear in 'reference format. Note that
5151 -- the accessibility check machinery may add an extra 'reference due to
5152 -- side effect removal.
5154 Call := Expr;
5155 while Nkind (Call) = N_Reference loop
5156 Call := Prefix (Call);
5157 end loop;
5159 if Nkind_In (Call, N_Qualified_Expression,
5160 N_Unchecked_Type_Conversion)
5161 then
5162 Call := Expression (Call);
5163 end if;
5165 if Is_Build_In_Place_Function_Call (Call) then
5167 -- Examine all parameter associations of the function call
5169 Param := First (Parameter_Associations (Call));
5170 while Present (Param) loop
5171 if Nkind (Param) = N_Parameter_Association
5172 and then Nkind (Selector_Name (Param)) = N_Identifier
5173 then
5174 Formal := Selector_Name (Param);
5175 Actual := Explicit_Actual_Parameter (Param);
5177 -- Construct the name of formal BIPaccess. It is much easier to
5178 -- extract the name of the function using an arbitrary formal's
5179 -- scope rather than the Name field of Call.
5181 if Access_Nam = No_Name and then Present (Entity (Formal)) then
5182 Access_Nam :=
5183 New_External_Name
5184 (Chars (Scope (Entity (Formal))),
5185 BIP_Formal_Suffix (BIP_Object_Access));
5186 end if;
5188 -- A match for BIPaccess => Obj_Id'Unrestricted_Access has been
5189 -- found.
5191 if Chars (Formal) = Access_Nam
5192 and then Nkind (Actual) = N_Attribute_Reference
5193 and then Attribute_Name (Actual) = Name_Unrestricted_Access
5194 and then Nkind (Prefix (Actual)) = N_Identifier
5195 and then Entity (Prefix (Actual)) = Obj_Id
5196 then
5197 return True;
5198 end if;
5199 end if;
5201 Next (Param);
5202 end loop;
5203 end if;
5205 return False;
5206 end Is_Object_Access_BIP_Func_Call;
5208 ----------------------------------
5209 -- Is_Possibly_Unaligned_Object --
5210 ----------------------------------
5212 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
5213 T : constant Entity_Id := Etype (N);
5215 begin
5216 -- Objects are never unaligned on VMs
5218 if VM_Target /= No_VM then
5219 return False;
5220 end if;
5222 -- If renamed object, apply test to underlying object
5224 if Is_Entity_Name (N)
5225 and then Is_Object (Entity (N))
5226 and then Present (Renamed_Object (Entity (N)))
5227 then
5228 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
5229 end if;
5231 -- Tagged and controlled types and aliased types are always aligned, as
5232 -- are concurrent types.
5234 if Is_Aliased (T)
5235 or else Has_Controlled_Component (T)
5236 or else Is_Concurrent_Type (T)
5237 or else Is_Tagged_Type (T)
5238 or else Is_Controlled (T)
5239 then
5240 return False;
5241 end if;
5243 -- If this is an element of a packed array, may be unaligned
5245 if Is_Ref_To_Bit_Packed_Array (N) then
5246 return True;
5247 end if;
5249 -- Case of indexed component reference: test whether prefix is unaligned
5251 if Nkind (N) = N_Indexed_Component then
5252 return Is_Possibly_Unaligned_Object (Prefix (N));
5254 -- Case of selected component reference
5256 elsif Nkind (N) = N_Selected_Component then
5257 declare
5258 P : constant Node_Id := Prefix (N);
5259 C : constant Entity_Id := Entity (Selector_Name (N));
5260 M : Nat;
5261 S : Nat;
5263 begin
5264 -- If component reference is for an array with non-static bounds,
5265 -- then it is always aligned: we can only process unaligned arrays
5266 -- with static bounds (more precisely compile time known bounds).
5268 if Is_Array_Type (T)
5269 and then not Compile_Time_Known_Bounds (T)
5270 then
5271 return False;
5272 end if;
5274 -- If component is aliased, it is definitely properly aligned
5276 if Is_Aliased (C) then
5277 return False;
5278 end if;
5280 -- If component is for a type implemented as a scalar, and the
5281 -- record is packed, and the component is other than the first
5282 -- component of the record, then the component may be unaligned.
5284 if Is_Packed (Etype (P))
5285 and then Represented_As_Scalar (Etype (C))
5286 and then First_Entity (Scope (C)) /= C
5287 then
5288 return True;
5289 end if;
5291 -- Compute maximum possible alignment for T
5293 -- If alignment is known, then that settles things
5295 if Known_Alignment (T) then
5296 M := UI_To_Int (Alignment (T));
5298 -- If alignment is not known, tentatively set max alignment
5300 else
5301 M := Ttypes.Maximum_Alignment;
5303 -- We can reduce this if the Esize is known since the default
5304 -- alignment will never be more than the smallest power of 2
5305 -- that does not exceed this Esize value.
5307 if Known_Esize (T) then
5308 S := UI_To_Int (Esize (T));
5310 while (M / 2) >= S loop
5311 M := M / 2;
5312 end loop;
5313 end if;
5314 end if;
5316 -- The following code is historical, it used to be present but it
5317 -- is too cautious, because the front-end does not know the proper
5318 -- default alignments for the target. Also, if the alignment is
5319 -- not known, the front end can't know in any case. If a copy is
5320 -- needed, the back-end will take care of it. This whole section
5321 -- including this comment can be removed later ???
5323 -- If the component reference is for a record that has a specified
5324 -- alignment, and we either know it is too small, or cannot tell,
5325 -- then the component may be unaligned.
5327 -- What is the following commented out code ???
5329 -- if Known_Alignment (Etype (P))
5330 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
5331 -- and then M > Alignment (Etype (P))
5332 -- then
5333 -- return True;
5334 -- end if;
5336 -- Case of component clause present which may specify an
5337 -- unaligned position.
5339 if Present (Component_Clause (C)) then
5341 -- Otherwise we can do a test to make sure that the actual
5342 -- start position in the record, and the length, are both
5343 -- consistent with the required alignment. If not, we know
5344 -- that we are unaligned.
5346 declare
5347 Align_In_Bits : constant Nat := M * System_Storage_Unit;
5348 begin
5349 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
5350 or else Esize (C) mod Align_In_Bits /= 0
5351 then
5352 return True;
5353 end if;
5354 end;
5355 end if;
5357 -- Otherwise, for a component reference, test prefix
5359 return Is_Possibly_Unaligned_Object (P);
5360 end;
5362 -- If not a component reference, must be aligned
5364 else
5365 return False;
5366 end if;
5367 end Is_Possibly_Unaligned_Object;
5369 ---------------------------------
5370 -- Is_Possibly_Unaligned_Slice --
5371 ---------------------------------
5373 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
5374 begin
5375 -- Go to renamed object
5377 if Is_Entity_Name (N)
5378 and then Is_Object (Entity (N))
5379 and then Present (Renamed_Object (Entity (N)))
5380 then
5381 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
5382 end if;
5384 -- The reference must be a slice
5386 if Nkind (N) /= N_Slice then
5387 return False;
5388 end if;
5390 -- We only need to worry if the target has strict alignment
5392 if not Target_Strict_Alignment then
5393 return False;
5394 end if;
5396 -- If it is a slice, then look at the array type being sliced
5398 declare
5399 Sarr : constant Node_Id := Prefix (N);
5400 -- Prefix of the slice, i.e. the array being sliced
5402 Styp : constant Entity_Id := Etype (Prefix (N));
5403 -- Type of the array being sliced
5405 Pref : Node_Id;
5406 Ptyp : Entity_Id;
5408 begin
5409 -- The problems arise if the array object that is being sliced
5410 -- is a component of a record or array, and we cannot guarantee
5411 -- the alignment of the array within its containing object.
5413 -- To investigate this, we look at successive prefixes to see
5414 -- if we have a worrisome indexed or selected component.
5416 Pref := Sarr;
5417 loop
5418 -- Case of array is part of an indexed component reference
5420 if Nkind (Pref) = N_Indexed_Component then
5421 Ptyp := Etype (Prefix (Pref));
5423 -- The only problematic case is when the array is packed, in
5424 -- which case we really know nothing about the alignment of
5425 -- individual components.
5427 if Is_Bit_Packed_Array (Ptyp) then
5428 return True;
5429 end if;
5431 -- Case of array is part of a selected component reference
5433 elsif Nkind (Pref) = N_Selected_Component then
5434 Ptyp := Etype (Prefix (Pref));
5436 -- We are definitely in trouble if the record in question
5437 -- has an alignment, and either we know this alignment is
5438 -- inconsistent with the alignment of the slice, or we don't
5439 -- know what the alignment of the slice should be.
5441 if Known_Alignment (Ptyp)
5442 and then (Unknown_Alignment (Styp)
5443 or else Alignment (Styp) > Alignment (Ptyp))
5444 then
5445 return True;
5446 end if;
5448 -- We are in potential trouble if the record type is packed.
5449 -- We could special case when we know that the array is the
5450 -- first component, but that's not such a simple case ???
5452 if Is_Packed (Ptyp) then
5453 return True;
5454 end if;
5456 -- We are in trouble if there is a component clause, and
5457 -- either we do not know the alignment of the slice, or
5458 -- the alignment of the slice is inconsistent with the
5459 -- bit position specified by the component clause.
5461 declare
5462 Field : constant Entity_Id := Entity (Selector_Name (Pref));
5463 begin
5464 if Present (Component_Clause (Field))
5465 and then
5466 (Unknown_Alignment (Styp)
5467 or else
5468 (Component_Bit_Offset (Field) mod
5469 (System_Storage_Unit * Alignment (Styp))) /= 0)
5470 then
5471 return True;
5472 end if;
5473 end;
5475 -- For cases other than selected or indexed components we know we
5476 -- are OK, since no issues arise over alignment.
5478 else
5479 return False;
5480 end if;
5482 -- We processed an indexed component or selected component
5483 -- reference that looked safe, so keep checking prefixes.
5485 Pref := Prefix (Pref);
5486 end loop;
5487 end;
5488 end Is_Possibly_Unaligned_Slice;
5490 -------------------------------
5491 -- Is_Related_To_Func_Return --
5492 -------------------------------
5494 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
5495 Expr : constant Node_Id := Related_Expression (Id);
5496 begin
5497 return
5498 Present (Expr)
5499 and then Nkind (Expr) = N_Explicit_Dereference
5500 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
5501 end Is_Related_To_Func_Return;
5503 --------------------------------
5504 -- Is_Ref_To_Bit_Packed_Array --
5505 --------------------------------
5507 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
5508 Result : Boolean;
5509 Expr : Node_Id;
5511 begin
5512 if Is_Entity_Name (N)
5513 and then Is_Object (Entity (N))
5514 and then Present (Renamed_Object (Entity (N)))
5515 then
5516 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
5517 end if;
5519 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5520 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
5521 Result := True;
5522 else
5523 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
5524 end if;
5526 if Result and then Nkind (N) = N_Indexed_Component then
5527 Expr := First (Expressions (N));
5528 while Present (Expr) loop
5529 Force_Evaluation (Expr);
5530 Next (Expr);
5531 end loop;
5532 end if;
5534 return Result;
5536 else
5537 return False;
5538 end if;
5539 end Is_Ref_To_Bit_Packed_Array;
5541 --------------------------------
5542 -- Is_Ref_To_Bit_Packed_Slice --
5543 --------------------------------
5545 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
5546 begin
5547 if Nkind (N) = N_Type_Conversion then
5548 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
5550 elsif Is_Entity_Name (N)
5551 and then Is_Object (Entity (N))
5552 and then Present (Renamed_Object (Entity (N)))
5553 then
5554 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
5556 elsif Nkind (N) = N_Slice
5557 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
5558 then
5559 return True;
5561 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5562 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
5564 else
5565 return False;
5566 end if;
5567 end Is_Ref_To_Bit_Packed_Slice;
5569 -----------------------
5570 -- Is_Renamed_Object --
5571 -----------------------
5573 function Is_Renamed_Object (N : Node_Id) return Boolean is
5574 Pnod : constant Node_Id := Parent (N);
5575 Kind : constant Node_Kind := Nkind (Pnod);
5576 begin
5577 if Kind = N_Object_Renaming_Declaration then
5578 return True;
5579 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
5580 return Is_Renamed_Object (Pnod);
5581 else
5582 return False;
5583 end if;
5584 end Is_Renamed_Object;
5586 --------------------------------------
5587 -- Is_Secondary_Stack_BIP_Func_Call --
5588 --------------------------------------
5590 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
5591 Alloc_Nam : Name_Id := No_Name;
5592 Actual : Node_Id;
5593 Call : Node_Id := Expr;
5594 Formal : Node_Id;
5595 Param : Node_Id;
5597 begin
5598 -- Build-in-place calls usually appear in 'reference format. Note that
5599 -- the accessibility check machinery may add an extra 'reference due to
5600 -- side effect removal.
5602 while Nkind (Call) = N_Reference loop
5603 Call := Prefix (Call);
5604 end loop;
5606 if Nkind_In (Call, N_Qualified_Expression,
5607 N_Unchecked_Type_Conversion)
5608 then
5609 Call := Expression (Call);
5610 end if;
5612 if Is_Build_In_Place_Function_Call (Call) then
5614 -- Examine all parameter associations of the function call
5616 Param := First (Parameter_Associations (Call));
5617 while Present (Param) loop
5618 if Nkind (Param) = N_Parameter_Association
5619 and then Nkind (Selector_Name (Param)) = N_Identifier
5620 then
5621 Formal := Selector_Name (Param);
5622 Actual := Explicit_Actual_Parameter (Param);
5624 -- Construct the name of formal BIPalloc. It is much easier to
5625 -- extract the name of the function using an arbitrary formal's
5626 -- scope rather than the Name field of Call.
5628 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
5629 Alloc_Nam :=
5630 New_External_Name
5631 (Chars (Scope (Entity (Formal))),
5632 BIP_Formal_Suffix (BIP_Alloc_Form));
5633 end if;
5635 -- A match for BIPalloc => 2 has been found
5637 if Chars (Formal) = Alloc_Nam
5638 and then Nkind (Actual) = N_Integer_Literal
5639 and then Intval (Actual) = Uint_2
5640 then
5641 return True;
5642 end if;
5643 end if;
5645 Next (Param);
5646 end loop;
5647 end if;
5649 return False;
5650 end Is_Secondary_Stack_BIP_Func_Call;
5652 -------------------------------------
5653 -- Is_Tag_To_Class_Wide_Conversion --
5654 -------------------------------------
5656 function Is_Tag_To_Class_Wide_Conversion
5657 (Obj_Id : Entity_Id) return Boolean
5659 Expr : constant Node_Id := Expression (Parent (Obj_Id));
5661 begin
5662 return
5663 Is_Class_Wide_Type (Etype (Obj_Id))
5664 and then Present (Expr)
5665 and then Nkind (Expr) = N_Unchecked_Type_Conversion
5666 and then Etype (Expression (Expr)) = RTE (RE_Tag);
5667 end Is_Tag_To_Class_Wide_Conversion;
5669 ----------------------------
5670 -- Is_Untagged_Derivation --
5671 ----------------------------
5673 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
5674 begin
5675 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
5676 or else
5677 (Is_Private_Type (T) and then Present (Full_View (T))
5678 and then not Is_Tagged_Type (Full_View (T))
5679 and then Is_Derived_Type (Full_View (T))
5680 and then Etype (Full_View (T)) /= T);
5681 end Is_Untagged_Derivation;
5683 ---------------------------
5684 -- Is_Volatile_Reference --
5685 ---------------------------
5687 function Is_Volatile_Reference (N : Node_Id) return Boolean is
5688 begin
5689 -- Only source references are to be treated as volatile, internally
5690 -- generated stuff cannot have volatile external effects.
5692 if not Comes_From_Source (N) then
5693 return False;
5695 -- Never true for reference to a type
5697 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
5698 return False;
5700 -- True if object reference with volatile type
5702 elsif Is_Volatile_Object (N) then
5703 return True;
5705 -- True if reference to volatile entity
5707 elsif Is_Entity_Name (N) then
5708 return Treat_As_Volatile (Entity (N));
5710 -- True for slice of volatile array
5712 elsif Nkind (N) = N_Slice then
5713 return Is_Volatile_Reference (Prefix (N));
5715 -- True if volatile component
5717 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5718 if (Is_Entity_Name (Prefix (N))
5719 and then Has_Volatile_Components (Entity (Prefix (N))))
5720 or else (Present (Etype (Prefix (N)))
5721 and then Has_Volatile_Components (Etype (Prefix (N))))
5722 then
5723 return True;
5724 else
5725 return Is_Volatile_Reference (Prefix (N));
5726 end if;
5728 -- Otherwise false
5730 else
5731 return False;
5732 end if;
5733 end Is_Volatile_Reference;
5735 --------------------------
5736 -- Is_VM_By_Copy_Actual --
5737 --------------------------
5739 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
5740 begin
5741 return VM_Target /= No_VM
5742 and then (Nkind (N) = N_Slice
5743 or else
5744 (Nkind (N) = N_Identifier
5745 and then Present (Renamed_Object (Entity (N)))
5746 and then Nkind (Renamed_Object (Entity (N))) =
5747 N_Slice));
5748 end Is_VM_By_Copy_Actual;
5750 --------------------
5751 -- Kill_Dead_Code --
5752 --------------------
5754 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
5755 W : Boolean := Warn;
5756 -- Set False if warnings suppressed
5758 begin
5759 if Present (N) then
5760 Remove_Warning_Messages (N);
5762 -- Generate warning if appropriate
5764 if W then
5766 -- We suppress the warning if this code is under control of an
5767 -- if statement, whose condition is a simple identifier, and
5768 -- either we are in an instance, or warnings off is set for this
5769 -- identifier. The reason for killing it in the instance case is
5770 -- that it is common and reasonable for code to be deleted in
5771 -- instances for various reasons.
5773 -- Could we use Is_Statically_Unevaluated here???
5775 if Nkind (Parent (N)) = N_If_Statement then
5776 declare
5777 C : constant Node_Id := Condition (Parent (N));
5778 begin
5779 if Nkind (C) = N_Identifier
5780 and then
5781 (In_Instance
5782 or else (Present (Entity (C))
5783 and then Has_Warnings_Off (Entity (C))))
5784 then
5785 W := False;
5786 end if;
5787 end;
5788 end if;
5790 -- Generate warning if not suppressed
5792 if W then
5793 Error_Msg_F
5794 ("?t?this code can never be executed and has been deleted!",
5796 end if;
5797 end if;
5799 -- Recurse into block statements and bodies to process declarations
5800 -- and statements.
5802 if Nkind (N) = N_Block_Statement
5803 or else Nkind (N) = N_Subprogram_Body
5804 or else Nkind (N) = N_Package_Body
5805 then
5806 Kill_Dead_Code (Declarations (N), False);
5807 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
5809 if Nkind (N) = N_Subprogram_Body then
5810 Set_Is_Eliminated (Defining_Entity (N));
5811 end if;
5813 elsif Nkind (N) = N_Package_Declaration then
5814 Kill_Dead_Code (Visible_Declarations (Specification (N)));
5815 Kill_Dead_Code (Private_Declarations (Specification (N)));
5817 -- ??? After this point, Delete_Tree has been called on all
5818 -- declarations in Specification (N), so references to entities
5819 -- therein look suspicious.
5821 declare
5822 E : Entity_Id := First_Entity (Defining_Entity (N));
5824 begin
5825 while Present (E) loop
5826 if Ekind (E) = E_Operator then
5827 Set_Is_Eliminated (E);
5828 end if;
5830 Next_Entity (E);
5831 end loop;
5832 end;
5834 -- Recurse into composite statement to kill individual statements in
5835 -- particular instantiations.
5837 elsif Nkind (N) = N_If_Statement then
5838 Kill_Dead_Code (Then_Statements (N));
5839 Kill_Dead_Code (Elsif_Parts (N));
5840 Kill_Dead_Code (Else_Statements (N));
5842 elsif Nkind (N) = N_Loop_Statement then
5843 Kill_Dead_Code (Statements (N));
5845 elsif Nkind (N) = N_Case_Statement then
5846 declare
5847 Alt : Node_Id;
5848 begin
5849 Alt := First (Alternatives (N));
5850 while Present (Alt) loop
5851 Kill_Dead_Code (Statements (Alt));
5852 Next (Alt);
5853 end loop;
5854 end;
5856 elsif Nkind (N) = N_Case_Statement_Alternative then
5857 Kill_Dead_Code (Statements (N));
5859 -- Deal with dead instances caused by deleting instantiations
5861 elsif Nkind (N) in N_Generic_Instantiation then
5862 Remove_Dead_Instance (N);
5863 end if;
5864 end if;
5865 end Kill_Dead_Code;
5867 -- Case where argument is a list of nodes to be killed
5869 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
5870 N : Node_Id;
5871 W : Boolean;
5873 begin
5874 W := Warn;
5876 if Is_Non_Empty_List (L) then
5877 N := First (L);
5878 while Present (N) loop
5879 Kill_Dead_Code (N, W);
5880 W := False;
5881 Next (N);
5882 end loop;
5883 end if;
5884 end Kill_Dead_Code;
5886 ------------------------
5887 -- Known_Non_Negative --
5888 ------------------------
5890 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
5891 begin
5892 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
5893 return True;
5895 else
5896 declare
5897 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
5898 begin
5899 return
5900 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
5901 end;
5902 end if;
5903 end Known_Non_Negative;
5905 --------------------
5906 -- Known_Non_Null --
5907 --------------------
5909 function Known_Non_Null (N : Node_Id) return Boolean is
5910 begin
5911 -- Checks for case where N is an entity reference
5913 if Is_Entity_Name (N) and then Present (Entity (N)) then
5914 declare
5915 E : constant Entity_Id := Entity (N);
5916 Op : Node_Kind;
5917 Val : Node_Id;
5919 begin
5920 -- First check if we are in decisive conditional
5922 Get_Current_Value_Condition (N, Op, Val);
5924 if Known_Null (Val) then
5925 if Op = N_Op_Eq then
5926 return False;
5927 elsif Op = N_Op_Ne then
5928 return True;
5929 end if;
5930 end if;
5932 -- If OK to do replacement, test Is_Known_Non_Null flag
5934 if OK_To_Do_Constant_Replacement (E) then
5935 return Is_Known_Non_Null (E);
5937 -- Otherwise if not safe to do replacement, then say so
5939 else
5940 return False;
5941 end if;
5942 end;
5944 -- True if access attribute
5946 elsif Nkind (N) = N_Attribute_Reference
5947 and then Nam_In (Attribute_Name (N), Name_Access,
5948 Name_Unchecked_Access,
5949 Name_Unrestricted_Access)
5950 then
5951 return True;
5953 -- True if allocator
5955 elsif Nkind (N) = N_Allocator then
5956 return True;
5958 -- For a conversion, true if expression is known non-null
5960 elsif Nkind (N) = N_Type_Conversion then
5961 return Known_Non_Null (Expression (N));
5963 -- Above are all cases where the value could be determined to be
5964 -- non-null. In all other cases, we don't know, so return False.
5966 else
5967 return False;
5968 end if;
5969 end Known_Non_Null;
5971 ----------------
5972 -- Known_Null --
5973 ----------------
5975 function Known_Null (N : Node_Id) return Boolean is
5976 begin
5977 -- Checks for case where N is an entity reference
5979 if Is_Entity_Name (N) and then Present (Entity (N)) then
5980 declare
5981 E : constant Entity_Id := Entity (N);
5982 Op : Node_Kind;
5983 Val : Node_Id;
5985 begin
5986 -- Constant null value is for sure null
5988 if Ekind (E) = E_Constant
5989 and then Known_Null (Constant_Value (E))
5990 then
5991 return True;
5992 end if;
5994 -- First check if we are in decisive conditional
5996 Get_Current_Value_Condition (N, Op, Val);
5998 if Known_Null (Val) then
5999 if Op = N_Op_Eq then
6000 return True;
6001 elsif Op = N_Op_Ne then
6002 return False;
6003 end if;
6004 end if;
6006 -- If OK to do replacement, test Is_Known_Null flag
6008 if OK_To_Do_Constant_Replacement (E) then
6009 return Is_Known_Null (E);
6011 -- Otherwise if not safe to do replacement, then say so
6013 else
6014 return False;
6015 end if;
6016 end;
6018 -- True if explicit reference to null
6020 elsif Nkind (N) = N_Null then
6021 return True;
6023 -- For a conversion, true if expression is known null
6025 elsif Nkind (N) = N_Type_Conversion then
6026 return Known_Null (Expression (N));
6028 -- Above are all cases where the value could be determined to be null.
6029 -- In all other cases, we don't know, so return False.
6031 else
6032 return False;
6033 end if;
6034 end Known_Null;
6036 -----------------------------
6037 -- Make_CW_Equivalent_Type --
6038 -----------------------------
6040 -- Create a record type used as an equivalent of any member of the class
6041 -- which takes its size from exp.
6043 -- Generate the following code:
6045 -- type Equiv_T is record
6046 -- _parent : T (List of discriminant constraints taken from Exp);
6047 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
6048 -- end Equiv_T;
6050 -- ??? Note that this type does not guarantee same alignment as all
6051 -- derived types
6053 function Make_CW_Equivalent_Type
6054 (T : Entity_Id;
6055 E : Node_Id) return Entity_Id
6057 Loc : constant Source_Ptr := Sloc (E);
6058 Root_Typ : constant Entity_Id := Root_Type (T);
6059 List_Def : constant List_Id := Empty_List;
6060 Comp_List : constant List_Id := New_List;
6061 Equiv_Type : Entity_Id;
6062 Range_Type : Entity_Id;
6063 Str_Type : Entity_Id;
6064 Constr_Root : Entity_Id;
6065 Sizexpr : Node_Id;
6067 begin
6068 -- If the root type is already constrained, there are no discriminants
6069 -- in the expression.
6071 if not Has_Discriminants (Root_Typ)
6072 or else Is_Constrained (Root_Typ)
6073 then
6074 Constr_Root := Root_Typ;
6076 -- At this point in the expansion, non-limited view of the type
6077 -- must be available, otherwise the error will be reported later.
6079 if From_Limited_With (Constr_Root)
6080 and then Present (Non_Limited_View (Constr_Root))
6081 then
6082 Constr_Root := Non_Limited_View (Constr_Root);
6083 end if;
6085 else
6086 Constr_Root := Make_Temporary (Loc, 'R');
6088 -- subtype cstr__n is T (List of discr constraints taken from Exp)
6090 Append_To (List_Def,
6091 Make_Subtype_Declaration (Loc,
6092 Defining_Identifier => Constr_Root,
6093 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
6094 end if;
6096 -- Generate the range subtype declaration
6098 Range_Type := Make_Temporary (Loc, 'G');
6100 if not Is_Interface (Root_Typ) then
6102 -- subtype rg__xx is
6103 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
6105 Sizexpr :=
6106 Make_Op_Subtract (Loc,
6107 Left_Opnd =>
6108 Make_Attribute_Reference (Loc,
6109 Prefix =>
6110 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6111 Attribute_Name => Name_Size),
6112 Right_Opnd =>
6113 Make_Attribute_Reference (Loc,
6114 Prefix => New_Occurrence_Of (Constr_Root, Loc),
6115 Attribute_Name => Name_Object_Size));
6116 else
6117 -- subtype rg__xx is
6118 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
6120 Sizexpr :=
6121 Make_Attribute_Reference (Loc,
6122 Prefix =>
6123 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6124 Attribute_Name => Name_Size);
6125 end if;
6127 Set_Paren_Count (Sizexpr, 1);
6129 Append_To (List_Def,
6130 Make_Subtype_Declaration (Loc,
6131 Defining_Identifier => Range_Type,
6132 Subtype_Indication =>
6133 Make_Subtype_Indication (Loc,
6134 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
6135 Constraint => Make_Range_Constraint (Loc,
6136 Range_Expression =>
6137 Make_Range (Loc,
6138 Low_Bound => Make_Integer_Literal (Loc, 1),
6139 High_Bound =>
6140 Make_Op_Divide (Loc,
6141 Left_Opnd => Sizexpr,
6142 Right_Opnd => Make_Integer_Literal (Loc,
6143 Intval => System_Storage_Unit)))))));
6145 -- subtype str__nn is Storage_Array (rg__x);
6147 Str_Type := Make_Temporary (Loc, 'S');
6148 Append_To (List_Def,
6149 Make_Subtype_Declaration (Loc,
6150 Defining_Identifier => Str_Type,
6151 Subtype_Indication =>
6152 Make_Subtype_Indication (Loc,
6153 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
6154 Constraint =>
6155 Make_Index_Or_Discriminant_Constraint (Loc,
6156 Constraints =>
6157 New_List (New_Occurrence_Of (Range_Type, Loc))))));
6159 -- type Equiv_T is record
6160 -- [ _parent : Tnn; ]
6161 -- E : Str_Type;
6162 -- end Equiv_T;
6164 Equiv_Type := Make_Temporary (Loc, 'T');
6165 Set_Ekind (Equiv_Type, E_Record_Type);
6166 Set_Parent_Subtype (Equiv_Type, Constr_Root);
6168 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
6169 -- treatment for this type. In particular, even though _parent's type
6170 -- is a controlled type or contains controlled components, we do not
6171 -- want to set Has_Controlled_Component on it to avoid making it gain
6172 -- an unwanted _controller component.
6174 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
6176 -- A class-wide equivalent type does not require initialization
6178 Set_Suppress_Initialization (Equiv_Type);
6180 if not Is_Interface (Root_Typ) then
6181 Append_To (Comp_List,
6182 Make_Component_Declaration (Loc,
6183 Defining_Identifier =>
6184 Make_Defining_Identifier (Loc, Name_uParent),
6185 Component_Definition =>
6186 Make_Component_Definition (Loc,
6187 Aliased_Present => False,
6188 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
6189 end if;
6191 Append_To (Comp_List,
6192 Make_Component_Declaration (Loc,
6193 Defining_Identifier => Make_Temporary (Loc, 'C'),
6194 Component_Definition =>
6195 Make_Component_Definition (Loc,
6196 Aliased_Present => False,
6197 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
6199 Append_To (List_Def,
6200 Make_Full_Type_Declaration (Loc,
6201 Defining_Identifier => Equiv_Type,
6202 Type_Definition =>
6203 Make_Record_Definition (Loc,
6204 Component_List =>
6205 Make_Component_List (Loc,
6206 Component_Items => Comp_List,
6207 Variant_Part => Empty))));
6209 -- Suppress all checks during the analysis of the expanded code to avoid
6210 -- the generation of spurious warnings under ZFP run-time.
6212 Insert_Actions (E, List_Def, Suppress => All_Checks);
6213 return Equiv_Type;
6214 end Make_CW_Equivalent_Type;
6216 -------------------------
6217 -- Make_Invariant_Call --
6218 -------------------------
6220 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
6221 Loc : constant Source_Ptr := Sloc (Expr);
6222 Typ : Entity_Id;
6224 begin
6225 Typ := Etype (Expr);
6227 -- Subtypes may be subject to invariants coming from their respective
6228 -- base types. The subtype may be fully or partially private.
6230 if Ekind_In (Typ, E_Array_Subtype,
6231 E_Private_Subtype,
6232 E_Record_Subtype,
6233 E_Record_Subtype_With_Private)
6234 then
6235 Typ := Base_Type (Typ);
6236 end if;
6238 pragma Assert
6239 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
6241 return
6242 Make_Procedure_Call_Statement (Loc,
6243 Name =>
6244 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
6245 Parameter_Associations => New_List (Relocate_Node (Expr)));
6246 end Make_Invariant_Call;
6248 ------------------------
6249 -- Make_Literal_Range --
6250 ------------------------
6252 function Make_Literal_Range
6253 (Loc : Source_Ptr;
6254 Literal_Typ : Entity_Id) return Node_Id
6256 Lo : constant Node_Id :=
6257 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
6258 Index : constant Entity_Id := Etype (Lo);
6260 Hi : Node_Id;
6261 Length_Expr : constant Node_Id :=
6262 Make_Op_Subtract (Loc,
6263 Left_Opnd =>
6264 Make_Integer_Literal (Loc,
6265 Intval => String_Literal_Length (Literal_Typ)),
6266 Right_Opnd =>
6267 Make_Integer_Literal (Loc, 1));
6269 begin
6270 Set_Analyzed (Lo, False);
6272 if Is_Integer_Type (Index) then
6273 Hi :=
6274 Make_Op_Add (Loc,
6275 Left_Opnd => New_Copy_Tree (Lo),
6276 Right_Opnd => Length_Expr);
6277 else
6278 Hi :=
6279 Make_Attribute_Reference (Loc,
6280 Attribute_Name => Name_Val,
6281 Prefix => New_Occurrence_Of (Index, Loc),
6282 Expressions => New_List (
6283 Make_Op_Add (Loc,
6284 Left_Opnd =>
6285 Make_Attribute_Reference (Loc,
6286 Attribute_Name => Name_Pos,
6287 Prefix => New_Occurrence_Of (Index, Loc),
6288 Expressions => New_List (New_Copy_Tree (Lo))),
6289 Right_Opnd => Length_Expr)));
6290 end if;
6292 return
6293 Make_Range (Loc,
6294 Low_Bound => Lo,
6295 High_Bound => Hi);
6296 end Make_Literal_Range;
6298 --------------------------
6299 -- Make_Non_Empty_Check --
6300 --------------------------
6302 function Make_Non_Empty_Check
6303 (Loc : Source_Ptr;
6304 N : Node_Id) return Node_Id
6306 begin
6307 return
6308 Make_Op_Ne (Loc,
6309 Left_Opnd =>
6310 Make_Attribute_Reference (Loc,
6311 Attribute_Name => Name_Length,
6312 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
6313 Right_Opnd =>
6314 Make_Integer_Literal (Loc, 0));
6315 end Make_Non_Empty_Check;
6317 -------------------------
6318 -- Make_Predicate_Call --
6319 -------------------------
6321 function Make_Predicate_Call
6322 (Typ : Entity_Id;
6323 Expr : Node_Id;
6324 Mem : Boolean := False) return Node_Id
6326 Loc : constant Source_Ptr := Sloc (Expr);
6328 begin
6329 pragma Assert (Present (Predicate_Function (Typ)));
6331 -- Call special membership version if requested and available
6333 if Mem then
6334 declare
6335 PFM : constant Entity_Id := Predicate_Function_M (Typ);
6336 begin
6337 if Present (PFM) then
6338 return
6339 Make_Function_Call (Loc,
6340 Name => New_Occurrence_Of (PFM, Loc),
6341 Parameter_Associations => New_List (Relocate_Node (Expr)));
6342 end if;
6343 end;
6344 end if;
6346 -- Case of calling normal predicate function
6348 return
6349 Make_Function_Call (Loc,
6350 Name =>
6351 New_Occurrence_Of (Predicate_Function (Typ), Loc),
6352 Parameter_Associations => New_List (Relocate_Node (Expr)));
6353 end Make_Predicate_Call;
6355 --------------------------
6356 -- Make_Predicate_Check --
6357 --------------------------
6359 function Make_Predicate_Check
6360 (Typ : Entity_Id;
6361 Expr : Node_Id) return Node_Id
6363 Loc : constant Source_Ptr := Sloc (Expr);
6364 Nam : Name_Id;
6366 begin
6367 -- If predicate checks are suppressed, then return a null statement.
6368 -- For this call, we check only the scope setting. If the caller wants
6369 -- to check a specific entity's setting, they must do it manually.
6371 if Predicate_Checks_Suppressed (Empty) then
6372 return Make_Null_Statement (Loc);
6373 end if;
6375 -- Do not generate a check within an internal subprogram (stream
6376 -- functions and the like, including including predicate functions).
6378 if Within_Internal_Subprogram then
6379 return Make_Null_Statement (Loc);
6380 end if;
6382 -- Compute proper name to use, we need to get this right so that the
6383 -- right set of check policies apply to the Check pragma we are making.
6385 if Has_Dynamic_Predicate_Aspect (Typ) then
6386 Nam := Name_Dynamic_Predicate;
6387 elsif Has_Static_Predicate_Aspect (Typ) then
6388 Nam := Name_Static_Predicate;
6389 else
6390 Nam := Name_Predicate;
6391 end if;
6393 return
6394 Make_Pragma (Loc,
6395 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
6396 Pragma_Argument_Associations => New_List (
6397 Make_Pragma_Argument_Association (Loc,
6398 Expression => Make_Identifier (Loc, Nam)),
6399 Make_Pragma_Argument_Association (Loc,
6400 Expression => Make_Predicate_Call (Typ, Expr))));
6401 end Make_Predicate_Check;
6403 ----------------------------
6404 -- Make_Subtype_From_Expr --
6405 ----------------------------
6407 -- 1. If Expr is an unconstrained array expression, creates
6408 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
6410 -- 2. If Expr is a unconstrained discriminated type expression, creates
6411 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
6413 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
6415 function Make_Subtype_From_Expr
6416 (E : Node_Id;
6417 Unc_Typ : Entity_Id) return Node_Id
6419 List_Constr : constant List_Id := New_List;
6420 Loc : constant Source_Ptr := Sloc (E);
6421 D : Entity_Id;
6422 Full_Exp : Node_Id;
6423 Full_Subtyp : Entity_Id;
6424 High_Bound : Entity_Id;
6425 Index_Typ : Entity_Id;
6426 Low_Bound : Entity_Id;
6427 Priv_Subtyp : Entity_Id;
6428 Utyp : Entity_Id;
6430 begin
6431 if Is_Private_Type (Unc_Typ)
6432 and then Has_Unknown_Discriminants (Unc_Typ)
6433 then
6434 -- Prepare the subtype completion. Use the base type to find the
6435 -- underlying type because the type may be a generic actual or an
6436 -- explicit subtype.
6438 Utyp := Underlying_Type (Base_Type (Unc_Typ));
6439 Full_Subtyp := Make_Temporary (Loc, 'C');
6440 Full_Exp :=
6441 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
6442 Set_Parent (Full_Exp, Parent (E));
6444 Priv_Subtyp := Make_Temporary (Loc, 'P');
6446 Insert_Action (E,
6447 Make_Subtype_Declaration (Loc,
6448 Defining_Identifier => Full_Subtyp,
6449 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
6451 -- Define the dummy private subtype
6453 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
6454 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
6455 Set_Scope (Priv_Subtyp, Full_Subtyp);
6456 Set_Is_Constrained (Priv_Subtyp);
6457 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
6458 Set_Is_Itype (Priv_Subtyp);
6459 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
6461 if Is_Tagged_Type (Priv_Subtyp) then
6462 Set_Class_Wide_Type
6463 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
6464 Set_Direct_Primitive_Operations (Priv_Subtyp,
6465 Direct_Primitive_Operations (Unc_Typ));
6466 end if;
6468 Set_Full_View (Priv_Subtyp, Full_Subtyp);
6470 return New_Occurrence_Of (Priv_Subtyp, Loc);
6472 elsif Is_Array_Type (Unc_Typ) then
6473 Index_Typ := First_Index (Unc_Typ);
6474 for J in 1 .. Number_Dimensions (Unc_Typ) loop
6476 -- Capture the bounds of each index constraint in case the context
6477 -- is an object declaration of an unconstrained type initialized
6478 -- by a function call:
6480 -- Obj : Unconstr_Typ := Func_Call;
6482 -- This scenario requires secondary scope management and the index
6483 -- constraint cannot depend on the temporary used to capture the
6484 -- result of the function call.
6486 -- SS_Mark;
6487 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
6488 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
6489 -- Obj : S := Temp.all;
6490 -- SS_Release; -- Temp is gone at this point, bounds of S are
6491 -- -- non existent.
6493 -- Generate:
6494 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
6496 Low_Bound := Make_Temporary (Loc, 'B');
6497 Insert_Action (E,
6498 Make_Object_Declaration (Loc,
6499 Defining_Identifier => Low_Bound,
6500 Object_Definition =>
6501 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
6502 Constant_Present => True,
6503 Expression =>
6504 Make_Attribute_Reference (Loc,
6505 Prefix => Duplicate_Subexpr_No_Checks (E),
6506 Attribute_Name => Name_First,
6507 Expressions => New_List (
6508 Make_Integer_Literal (Loc, J)))));
6510 -- Generate:
6511 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
6513 High_Bound := Make_Temporary (Loc, 'B');
6514 Insert_Action (E,
6515 Make_Object_Declaration (Loc,
6516 Defining_Identifier => High_Bound,
6517 Object_Definition =>
6518 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
6519 Constant_Present => True,
6520 Expression =>
6521 Make_Attribute_Reference (Loc,
6522 Prefix => Duplicate_Subexpr_No_Checks (E),
6523 Attribute_Name => Name_Last,
6524 Expressions => New_List (
6525 Make_Integer_Literal (Loc, J)))));
6527 Append_To (List_Constr,
6528 Make_Range (Loc,
6529 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
6530 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
6532 Index_Typ := Next_Index (Index_Typ);
6533 end loop;
6535 elsif Is_Class_Wide_Type (Unc_Typ) then
6536 declare
6537 CW_Subtype : Entity_Id;
6538 EQ_Typ : Entity_Id := Empty;
6540 begin
6541 -- A class-wide equivalent type is not needed when VM_Target
6542 -- because the VM back-ends handle the class-wide object
6543 -- initialization itself (and doesn't need or want the
6544 -- additional intermediate type to handle the assignment).
6546 if Expander_Active and then Tagged_Type_Expansion then
6548 -- If this is the class-wide type of a completion that is a
6549 -- record subtype, set the type of the class-wide type to be
6550 -- the full base type, for use in the expanded code for the
6551 -- equivalent type. Should this be done earlier when the
6552 -- completion is analyzed ???
6554 if Is_Private_Type (Etype (Unc_Typ))
6555 and then
6556 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
6557 then
6558 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
6559 end if;
6561 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
6562 end if;
6564 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
6565 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
6566 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
6568 return New_Occurrence_Of (CW_Subtype, Loc);
6569 end;
6571 -- Indefinite record type with discriminants
6573 else
6574 D := First_Discriminant (Unc_Typ);
6575 while Present (D) loop
6576 Append_To (List_Constr,
6577 Make_Selected_Component (Loc,
6578 Prefix => Duplicate_Subexpr_No_Checks (E),
6579 Selector_Name => New_Occurrence_Of (D, Loc)));
6581 Next_Discriminant (D);
6582 end loop;
6583 end if;
6585 return
6586 Make_Subtype_Indication (Loc,
6587 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
6588 Constraint =>
6589 Make_Index_Or_Discriminant_Constraint (Loc,
6590 Constraints => List_Constr));
6591 end Make_Subtype_From_Expr;
6593 ----------------------------
6594 -- Matching_Standard_Type --
6595 ----------------------------
6597 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
6598 pragma Assert (Is_Scalar_Type (Typ));
6599 Siz : constant Uint := Esize (Typ);
6601 begin
6602 -- Floating-point cases
6604 if Is_Floating_Point_Type (Typ) then
6605 if Siz <= Esize (Standard_Short_Float) then
6606 return Standard_Short_Float;
6607 elsif Siz <= Esize (Standard_Float) then
6608 return Standard_Float;
6609 elsif Siz <= Esize (Standard_Long_Float) then
6610 return Standard_Long_Float;
6611 elsif Siz <= Esize (Standard_Long_Long_Float) then
6612 return Standard_Long_Long_Float;
6613 else
6614 raise Program_Error;
6615 end if;
6617 -- Integer cases (includes fixed-point types)
6619 -- Unsigned integer cases (includes normal enumeration types)
6621 elsif Is_Unsigned_Type (Typ) then
6622 if Siz <= Esize (Standard_Short_Short_Unsigned) then
6623 return Standard_Short_Short_Unsigned;
6624 elsif Siz <= Esize (Standard_Short_Unsigned) then
6625 return Standard_Short_Unsigned;
6626 elsif Siz <= Esize (Standard_Unsigned) then
6627 return Standard_Unsigned;
6628 elsif Siz <= Esize (Standard_Long_Unsigned) then
6629 return Standard_Long_Unsigned;
6630 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
6631 return Standard_Long_Long_Unsigned;
6632 else
6633 raise Program_Error;
6634 end if;
6636 -- Signed integer cases
6638 else
6639 if Siz <= Esize (Standard_Short_Short_Integer) then
6640 return Standard_Short_Short_Integer;
6641 elsif Siz <= Esize (Standard_Short_Integer) then
6642 return Standard_Short_Integer;
6643 elsif Siz <= Esize (Standard_Integer) then
6644 return Standard_Integer;
6645 elsif Siz <= Esize (Standard_Long_Integer) then
6646 return Standard_Long_Integer;
6647 elsif Siz <= Esize (Standard_Long_Long_Integer) then
6648 return Standard_Long_Long_Integer;
6649 else
6650 raise Program_Error;
6651 end if;
6652 end if;
6653 end Matching_Standard_Type;
6655 -----------------------------
6656 -- May_Generate_Large_Temp --
6657 -----------------------------
6659 -- At the current time, the only types that we return False for (i.e. where
6660 -- we decide we know they cannot generate large temps) are ones where we
6661 -- know the size is 256 bits or less at compile time, and we are still not
6662 -- doing a thorough job on arrays and records ???
6664 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
6665 begin
6666 if not Size_Known_At_Compile_Time (Typ) then
6667 return False;
6669 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
6670 return False;
6672 elsif Is_Array_Type (Typ)
6673 and then Present (Packed_Array_Impl_Type (Typ))
6674 then
6675 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
6677 -- We could do more here to find other small types ???
6679 else
6680 return True;
6681 end if;
6682 end May_Generate_Large_Temp;
6684 ------------------------
6685 -- Needs_Finalization --
6686 ------------------------
6688 function Needs_Finalization (T : Entity_Id) return Boolean is
6689 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
6690 -- If type is not frozen yet, check explicitly among its components,
6691 -- because the Has_Controlled_Component flag is not necessarily set.
6693 -----------------------------------
6694 -- Has_Some_Controlled_Component --
6695 -----------------------------------
6697 function Has_Some_Controlled_Component
6698 (Rec : Entity_Id) return Boolean
6700 Comp : Entity_Id;
6702 begin
6703 if Has_Controlled_Component (Rec) then
6704 return True;
6706 elsif not Is_Frozen (Rec) then
6707 if Is_Record_Type (Rec) then
6708 Comp := First_Entity (Rec);
6710 while Present (Comp) loop
6711 if not Is_Type (Comp)
6712 and then Needs_Finalization (Etype (Comp))
6713 then
6714 return True;
6715 end if;
6717 Next_Entity (Comp);
6718 end loop;
6720 return False;
6722 elsif Is_Array_Type (Rec) then
6723 return Needs_Finalization (Component_Type (Rec));
6725 else
6726 return Has_Controlled_Component (Rec);
6727 end if;
6728 else
6729 return False;
6730 end if;
6731 end Has_Some_Controlled_Component;
6733 -- Start of processing for Needs_Finalization
6735 begin
6736 -- Certain run-time configurations and targets do not provide support
6737 -- for controlled types.
6739 if Restriction_Active (No_Finalization) then
6740 return False;
6742 -- C++, CIL and Java types are not considered controlled. It is assumed
6743 -- that the non-Ada side will handle their clean up.
6745 elsif Convention (T) = Convention_CIL
6746 or else Convention (T) = Convention_CPP
6747 or else Convention (T) = Convention_Java
6748 then
6749 return False;
6751 else
6752 -- Class-wide types are treated as controlled because derivations
6753 -- from the root type can introduce controlled components.
6755 return
6756 Is_Class_Wide_Type (T)
6757 or else Is_Controlled (T)
6758 or else Has_Controlled_Component (T)
6759 or else Has_Some_Controlled_Component (T)
6760 or else
6761 (Is_Concurrent_Type (T)
6762 and then Present (Corresponding_Record_Type (T))
6763 and then Needs_Finalization (Corresponding_Record_Type (T)));
6764 end if;
6765 end Needs_Finalization;
6767 ----------------------------
6768 -- Needs_Constant_Address --
6769 ----------------------------
6771 function Needs_Constant_Address
6772 (Decl : Node_Id;
6773 Typ : Entity_Id) return Boolean
6775 begin
6777 -- If we have no initialization of any kind, then we don't need to place
6778 -- any restrictions on the address clause, because the object will be
6779 -- elaborated after the address clause is evaluated. This happens if the
6780 -- declaration has no initial expression, or the type has no implicit
6781 -- initialization, or the object is imported.
6783 -- The same holds for all initialized scalar types and all access types.
6784 -- Packed bit arrays of size up to 64 are represented using a modular
6785 -- type with an initialization (to zero) and can be processed like other
6786 -- initialized scalar types.
6788 -- If the type is controlled, code to attach the object to a
6789 -- finalization chain is generated at the point of declaration, and
6790 -- therefore the elaboration of the object cannot be delayed: the
6791 -- address expression must be a constant.
6793 if No (Expression (Decl))
6794 and then not Needs_Finalization (Typ)
6795 and then
6796 (not Has_Non_Null_Base_Init_Proc (Typ)
6797 or else Is_Imported (Defining_Identifier (Decl)))
6798 then
6799 return False;
6801 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
6802 or else Is_Access_Type (Typ)
6803 or else
6804 (Is_Bit_Packed_Array (Typ)
6805 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
6806 then
6807 return False;
6809 else
6811 -- Otherwise, we require the address clause to be constant because
6812 -- the call to the initialization procedure (or the attach code) has
6813 -- to happen at the point of the declaration.
6815 -- Actually the IP call has been moved to the freeze actions anyway,
6816 -- so maybe we can relax this restriction???
6818 return True;
6819 end if;
6820 end Needs_Constant_Address;
6822 ----------------------------
6823 -- New_Class_Wide_Subtype --
6824 ----------------------------
6826 function New_Class_Wide_Subtype
6827 (CW_Typ : Entity_Id;
6828 N : Node_Id) return Entity_Id
6830 Res : constant Entity_Id := Create_Itype (E_Void, N);
6831 Res_Name : constant Name_Id := Chars (Res);
6832 Res_Scope : constant Entity_Id := Scope (Res);
6834 begin
6835 Copy_Node (CW_Typ, Res);
6836 Set_Comes_From_Source (Res, False);
6837 Set_Sloc (Res, Sloc (N));
6838 Set_Is_Itype (Res);
6839 Set_Associated_Node_For_Itype (Res, N);
6840 Set_Is_Public (Res, False); -- By default, may be changed below.
6841 Set_Public_Status (Res);
6842 Set_Chars (Res, Res_Name);
6843 Set_Scope (Res, Res_Scope);
6844 Set_Ekind (Res, E_Class_Wide_Subtype);
6845 Set_Next_Entity (Res, Empty);
6846 Set_Etype (Res, Base_Type (CW_Typ));
6847 Set_Is_Frozen (Res, False);
6848 Set_Freeze_Node (Res, Empty);
6849 return (Res);
6850 end New_Class_Wide_Subtype;
6852 --------------------------------
6853 -- Non_Limited_Designated_Type --
6854 ---------------------------------
6856 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
6857 Desig : constant Entity_Id := Designated_Type (T);
6858 begin
6859 if Ekind (Desig) = E_Incomplete_Type
6860 and then Present (Non_Limited_View (Desig))
6861 then
6862 return Non_Limited_View (Desig);
6863 else
6864 return Desig;
6865 end if;
6866 end Non_Limited_Designated_Type;
6868 -----------------------------------
6869 -- OK_To_Do_Constant_Replacement --
6870 -----------------------------------
6872 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
6873 ES : constant Entity_Id := Scope (E);
6874 CS : Entity_Id;
6876 begin
6877 -- Do not replace statically allocated objects, because they may be
6878 -- modified outside the current scope.
6880 if Is_Statically_Allocated (E) then
6881 return False;
6883 -- Do not replace aliased or volatile objects, since we don't know what
6884 -- else might change the value.
6886 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
6887 return False;
6889 -- Debug flag -gnatdM disconnects this optimization
6891 elsif Debug_Flag_MM then
6892 return False;
6894 -- Otherwise check scopes
6896 else
6897 CS := Current_Scope;
6899 loop
6900 -- If we are in right scope, replacement is safe
6902 if CS = ES then
6903 return True;
6905 -- Packages do not affect the determination of safety
6907 elsif Ekind (CS) = E_Package then
6908 exit when CS = Standard_Standard;
6909 CS := Scope (CS);
6911 -- Blocks do not affect the determination of safety
6913 elsif Ekind (CS) = E_Block then
6914 CS := Scope (CS);
6916 -- Loops do not affect the determination of safety. Note that we
6917 -- kill all current values on entry to a loop, so we are just
6918 -- talking about processing within a loop here.
6920 elsif Ekind (CS) = E_Loop then
6921 CS := Scope (CS);
6923 -- Otherwise, the reference is dubious, and we cannot be sure that
6924 -- it is safe to do the replacement.
6926 else
6927 exit;
6928 end if;
6929 end loop;
6931 return False;
6932 end if;
6933 end OK_To_Do_Constant_Replacement;
6935 ------------------------------------
6936 -- Possible_Bit_Aligned_Component --
6937 ------------------------------------
6939 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
6940 begin
6941 -- Do not process an unanalyzed node because it is not yet decorated and
6942 -- most checks performed below will fail.
6944 if not Analyzed (N) then
6945 return False;
6946 end if;
6948 case Nkind (N) is
6950 -- Case of indexed component
6952 when N_Indexed_Component =>
6953 declare
6954 P : constant Node_Id := Prefix (N);
6955 Ptyp : constant Entity_Id := Etype (P);
6957 begin
6958 -- If we know the component size and it is less than 64, then
6959 -- we are definitely OK. The back end always does assignment of
6960 -- misaligned small objects correctly.
6962 if Known_Static_Component_Size (Ptyp)
6963 and then Component_Size (Ptyp) <= 64
6964 then
6965 return False;
6967 -- Otherwise, we need to test the prefix, to see if we are
6968 -- indexing from a possibly unaligned component.
6970 else
6971 return Possible_Bit_Aligned_Component (P);
6972 end if;
6973 end;
6975 -- Case of selected component
6977 when N_Selected_Component =>
6978 declare
6979 P : constant Node_Id := Prefix (N);
6980 Comp : constant Entity_Id := Entity (Selector_Name (N));
6982 begin
6983 -- If there is no component clause, then we are in the clear
6984 -- since the back end will never misalign a large component
6985 -- unless it is forced to do so. In the clear means we need
6986 -- only the recursive test on the prefix.
6988 if Component_May_Be_Bit_Aligned (Comp) then
6989 return True;
6990 else
6991 return Possible_Bit_Aligned_Component (P);
6992 end if;
6993 end;
6995 -- For a slice, test the prefix, if that is possibly misaligned,
6996 -- then for sure the slice is.
6998 when N_Slice =>
6999 return Possible_Bit_Aligned_Component (Prefix (N));
7001 -- For an unchecked conversion, check whether the expression may
7002 -- be bit-aligned.
7004 when N_Unchecked_Type_Conversion =>
7005 return Possible_Bit_Aligned_Component (Expression (N));
7007 -- If we have none of the above, it means that we have fallen off the
7008 -- top testing prefixes recursively, and we now have a stand alone
7009 -- object, where we don't have a problem, unless this is a renaming,
7010 -- in which case we need to look into the renamed object.
7012 when others =>
7013 if Is_Entity_Name (N)
7014 and then Present (Renamed_Object (Entity (N)))
7015 then
7016 return
7017 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
7018 else
7019 return False;
7020 end if;
7022 end case;
7023 end Possible_Bit_Aligned_Component;
7025 -----------------------------------------------
7026 -- Process_Statements_For_Controlled_Objects --
7027 -----------------------------------------------
7029 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
7030 Loc : constant Source_Ptr := Sloc (N);
7032 function Are_Wrapped (L : List_Id) return Boolean;
7033 -- Determine whether list L contains only one statement which is a block
7035 function Wrap_Statements_In_Block
7036 (L : List_Id;
7037 Scop : Entity_Id := Current_Scope) return Node_Id;
7038 -- Given a list of statements L, wrap it in a block statement and return
7039 -- the generated node. Scop is either the current scope or the scope of
7040 -- the context (if applicable).
7042 -----------------
7043 -- Are_Wrapped --
7044 -----------------
7046 function Are_Wrapped (L : List_Id) return Boolean is
7047 Stmt : constant Node_Id := First (L);
7048 begin
7049 return
7050 Present (Stmt)
7051 and then No (Next (Stmt))
7052 and then Nkind (Stmt) = N_Block_Statement;
7053 end Are_Wrapped;
7055 ------------------------------
7056 -- Wrap_Statements_In_Block --
7057 ------------------------------
7059 function Wrap_Statements_In_Block
7060 (L : List_Id;
7061 Scop : Entity_Id := Current_Scope) return Node_Id
7063 Block_Id : Entity_Id;
7064 Block_Nod : Node_Id;
7065 Iter_Loop : Entity_Id;
7067 begin
7068 Block_Nod :=
7069 Make_Block_Statement (Loc,
7070 Declarations => No_List,
7071 Handled_Statement_Sequence =>
7072 Make_Handled_Sequence_Of_Statements (Loc,
7073 Statements => L));
7075 -- Create a label for the block in case the block needs to manage the
7076 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
7078 Add_Block_Identifier (Block_Nod, Block_Id);
7080 -- When wrapping the statements of an iterator loop, check whether
7081 -- the loop requires secondary stack management and if so, propagate
7082 -- the appropriate flags to the block. This ensures that the cursor
7083 -- is properly cleaned up at each iteration of the loop.
7085 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
7087 if Present (Iter_Loop) then
7088 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
7090 -- Secondary stack reclamation is suppressed when the associated
7091 -- iterator loop contains a return statement which uses the stack.
7093 Set_Sec_Stack_Needed_For_Return
7094 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
7095 end if;
7097 return Block_Nod;
7098 end Wrap_Statements_In_Block;
7100 -- Local variables
7102 Block : Node_Id;
7104 -- Start of processing for Process_Statements_For_Controlled_Objects
7106 begin
7107 -- Whenever a non-handled statement list is wrapped in a block, the
7108 -- block must be explicitly analyzed to redecorate all entities in the
7109 -- list and ensure that a finalizer is properly built.
7111 case Nkind (N) is
7112 when N_Elsif_Part |
7113 N_If_Statement |
7114 N_Conditional_Entry_Call |
7115 N_Selective_Accept =>
7117 -- Check the "then statements" for elsif parts and if statements
7119 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
7120 and then not Is_Empty_List (Then_Statements (N))
7121 and then not Are_Wrapped (Then_Statements (N))
7122 and then Requires_Cleanup_Actions
7123 (Then_Statements (N), False, False)
7124 then
7125 Block := Wrap_Statements_In_Block (Then_Statements (N));
7126 Set_Then_Statements (N, New_List (Block));
7128 Analyze (Block);
7129 end if;
7131 -- Check the "else statements" for conditional entry calls, if
7132 -- statements and selective accepts.
7134 if Nkind_In (N, N_Conditional_Entry_Call,
7135 N_If_Statement,
7136 N_Selective_Accept)
7137 and then not Is_Empty_List (Else_Statements (N))
7138 and then not Are_Wrapped (Else_Statements (N))
7139 and then Requires_Cleanup_Actions
7140 (Else_Statements (N), False, False)
7141 then
7142 Block := Wrap_Statements_In_Block (Else_Statements (N));
7143 Set_Else_Statements (N, New_List (Block));
7145 Analyze (Block);
7146 end if;
7148 when N_Abortable_Part |
7149 N_Accept_Alternative |
7150 N_Case_Statement_Alternative |
7151 N_Delay_Alternative |
7152 N_Entry_Call_Alternative |
7153 N_Exception_Handler |
7154 N_Loop_Statement |
7155 N_Triggering_Alternative =>
7157 if not Is_Empty_List (Statements (N))
7158 and then not Are_Wrapped (Statements (N))
7159 and then Requires_Cleanup_Actions (Statements (N), False, False)
7160 then
7161 if Nkind (N) = N_Loop_Statement
7162 and then Present (Identifier (N))
7163 then
7164 Block :=
7165 Wrap_Statements_In_Block
7166 (L => Statements (N),
7167 Scop => Entity (Identifier (N)));
7168 else
7169 Block := Wrap_Statements_In_Block (Statements (N));
7170 end if;
7172 Set_Statements (N, New_List (Block));
7173 Analyze (Block);
7174 end if;
7176 when others =>
7177 null;
7178 end case;
7179 end Process_Statements_For_Controlled_Objects;
7181 ------------------
7182 -- Power_Of_Two --
7183 ------------------
7185 function Power_Of_Two (N : Node_Id) return Nat is
7186 Typ : constant Entity_Id := Etype (N);
7187 pragma Assert (Is_Integer_Type (Typ));
7189 Siz : constant Nat := UI_To_Int (Esize (Typ));
7190 Val : Uint;
7192 begin
7193 if not Compile_Time_Known_Value (N) then
7194 return 0;
7196 else
7197 Val := Expr_Value (N);
7198 for J in 1 .. Siz - 1 loop
7199 if Val = Uint_2 ** J then
7200 return J;
7201 end if;
7202 end loop;
7204 return 0;
7205 end if;
7206 end Power_Of_Two;
7208 ----------------------
7209 -- Remove_Init_Call --
7210 ----------------------
7212 function Remove_Init_Call
7213 (Var : Entity_Id;
7214 Rep_Clause : Node_Id) return Node_Id
7216 Par : constant Node_Id := Parent (Var);
7217 Typ : constant Entity_Id := Etype (Var);
7219 Init_Proc : Entity_Id;
7220 -- Initialization procedure for Typ
7222 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
7223 -- Look for init call for Var starting at From and scanning the
7224 -- enclosing list until Rep_Clause or the end of the list is reached.
7226 ----------------------------
7227 -- Find_Init_Call_In_List --
7228 ----------------------------
7230 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
7231 Init_Call : Node_Id;
7233 begin
7234 Init_Call := From;
7235 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
7236 if Nkind (Init_Call) = N_Procedure_Call_Statement
7237 and then Is_Entity_Name (Name (Init_Call))
7238 and then Entity (Name (Init_Call)) = Init_Proc
7239 then
7240 return Init_Call;
7241 end if;
7243 Next (Init_Call);
7244 end loop;
7246 return Empty;
7247 end Find_Init_Call_In_List;
7249 Init_Call : Node_Id;
7251 -- Start of processing for Find_Init_Call
7253 begin
7254 if Present (Initialization_Statements (Var)) then
7255 Init_Call := Initialization_Statements (Var);
7256 Set_Initialization_Statements (Var, Empty);
7258 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
7260 -- No init proc for the type, so obviously no call to be found
7262 return Empty;
7264 else
7265 -- We might be able to handle other cases below by just properly
7266 -- setting Initialization_Statements at the point where the init proc
7267 -- call is generated???
7269 Init_Proc := Base_Init_Proc (Typ);
7271 -- First scan the list containing the declaration of Var
7273 Init_Call := Find_Init_Call_In_List (From => Next (Par));
7275 -- If not found, also look on Var's freeze actions list, if any,
7276 -- since the init call may have been moved there (case of an address
7277 -- clause applying to Var).
7279 if No (Init_Call) and then Present (Freeze_Node (Var)) then
7280 Init_Call :=
7281 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
7282 end if;
7284 -- If the initialization call has actuals that use the secondary
7285 -- stack, the call may have been wrapped into a temporary block, in
7286 -- which case the block itself has to be removed.
7288 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
7289 declare
7290 Blk : constant Node_Id := Next (Par);
7291 begin
7292 if Present
7293 (Find_Init_Call_In_List
7294 (First (Statements (Handled_Statement_Sequence (Blk)))))
7295 then
7296 Init_Call := Blk;
7297 end if;
7298 end;
7299 end if;
7300 end if;
7302 if Present (Init_Call) then
7303 Remove (Init_Call);
7304 end if;
7305 return Init_Call;
7306 end Remove_Init_Call;
7308 -------------------------
7309 -- Remove_Side_Effects --
7310 -------------------------
7312 procedure Remove_Side_Effects
7313 (Exp : Node_Id;
7314 Name_Req : Boolean := False;
7315 Renaming_Req : Boolean := False;
7316 Variable_Ref : Boolean := False;
7317 Related_Id : Entity_Id := Empty;
7318 Is_Low_Bound : Boolean := False;
7319 Is_High_Bound : Boolean := False)
7321 function Build_Temporary
7322 (Loc : Source_Ptr;
7323 Id : Character;
7324 Related_Nod : Node_Id := Empty) return Entity_Id;
7325 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
7326 -- is present (xxx is taken from the Chars field of Related_Nod),
7327 -- otherwise it generates an internal temporary.
7329 ---------------------
7330 -- Build_Temporary --
7331 ---------------------
7333 function Build_Temporary
7334 (Loc : Source_Ptr;
7335 Id : Character;
7336 Related_Nod : Node_Id := Empty) return Entity_Id
7338 Temp_Nam : Name_Id;
7340 begin
7341 -- The context requires an external symbol
7343 if Present (Related_Id) then
7344 if Is_Low_Bound then
7345 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
7346 else pragma Assert (Is_High_Bound);
7347 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
7348 end if;
7350 return Make_Defining_Identifier (Loc, Temp_Nam);
7352 -- Otherwise generate an internal temporary
7354 else
7355 return Make_Temporary (Loc, Id, Related_Nod);
7356 end if;
7357 end Build_Temporary;
7359 -- Local variables
7361 Loc : constant Source_Ptr := Sloc (Exp);
7362 Exp_Type : constant Entity_Id := Etype (Exp);
7363 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
7364 Def_Id : Entity_Id;
7365 E : Node_Id;
7366 New_Exp : Node_Id;
7367 Ptr_Typ_Decl : Node_Id;
7368 Ref_Type : Entity_Id;
7369 Res : Node_Id;
7371 -- Start of processing for Remove_Side_Effects
7373 begin
7374 -- Handle cases in which there is nothing to do. In GNATprove mode,
7375 -- removal of side effects is useful for the light expansion of
7376 -- renamings. This removal should only occur when not inside a
7377 -- generic and not doing a pre-analysis.
7379 if not Expander_Active
7380 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
7381 then
7382 return;
7383 end if;
7385 -- Cannot generate temporaries if the invocation to remove side effects
7386 -- was issued too early and the type of the expression is not resolved
7387 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
7388 -- Remove_Side_Effects).
7390 if No (Exp_Type) or else Ekind (Exp_Type) = E_Access_Attribute_Type then
7391 return;
7393 -- No action needed for side-effect free expressions
7395 elsif Side_Effect_Free (Exp, Name_Req, Variable_Ref) then
7396 return;
7397 end if;
7399 -- The remaining procesaing is done with all checks suppressed
7401 -- Note: from now on, don't use return statements, instead do a goto
7402 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
7404 Scope_Suppress.Suppress := (others => True);
7406 -- If it is a scalar type and we need to capture the value, just make
7407 -- a copy. Likewise for a function call, an attribute reference, a
7408 -- conditional expression, an allocator, or an operator. And if we have
7409 -- a volatile reference and Name_Req is not set (see comments for
7410 -- Side_Effect_Free).
7412 if Is_Elementary_Type (Exp_Type)
7414 -- Note: this test is rather mysterious??? Why can't we just test ONLY
7415 -- Is_Elementary_Type and be done with it. If we try that approach, we
7416 -- get some failures (infinite recursions) from the Duplicate_Subexpr
7417 -- call at the end of Checks.Apply_Predicate_Check. To be
7418 -- investigated ???
7420 and then (Variable_Ref
7421 or else Nkind_In (Exp, N_Attribute_Reference,
7422 N_Allocator,
7423 N_Case_Expression,
7424 N_If_Expression,
7425 N_Function_Call)
7426 or else Nkind (Exp) in N_Op
7427 or else (not Name_Req
7428 and then Is_Volatile_Reference (Exp)))
7429 then
7430 Def_Id := Build_Temporary (Loc, 'R', Exp);
7431 Set_Etype (Def_Id, Exp_Type);
7432 Res := New_Occurrence_Of (Def_Id, Loc);
7434 -- If the expression is a packed reference, it must be reanalyzed and
7435 -- expanded, depending on context. This is the case for actuals where
7436 -- a constraint check may capture the actual before expansion of the
7437 -- call is complete.
7439 if Nkind (Exp) = N_Indexed_Component
7440 and then Is_Packed (Etype (Prefix (Exp)))
7441 then
7442 Set_Analyzed (Exp, False);
7443 Set_Analyzed (Prefix (Exp), False);
7444 end if;
7446 -- Generate:
7447 -- Rnn : Exp_Type renames Expr;
7449 if Renaming_Req then
7450 E :=
7451 Make_Object_Renaming_Declaration (Loc,
7452 Defining_Identifier => Def_Id,
7453 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7454 Name => Relocate_Node (Exp));
7456 -- Generate:
7457 -- Rnn : constant Exp_Type := Expr;
7459 else
7460 E :=
7461 Make_Object_Declaration (Loc,
7462 Defining_Identifier => Def_Id,
7463 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7464 Constant_Present => True,
7465 Expression => Relocate_Node (Exp));
7467 Set_Assignment_OK (E);
7468 end if;
7470 Insert_Action (Exp, E);
7472 -- If the expression has the form v.all then we can just capture the
7473 -- pointer, and then do an explicit dereference on the result, but
7474 -- this is not right if this is a volatile reference.
7476 elsif Nkind (Exp) = N_Explicit_Dereference
7477 and then not Is_Volatile_Reference (Exp)
7478 then
7479 Def_Id := Build_Temporary (Loc, 'R', Exp);
7480 Res :=
7481 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
7483 Insert_Action (Exp,
7484 Make_Object_Declaration (Loc,
7485 Defining_Identifier => Def_Id,
7486 Object_Definition =>
7487 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
7488 Constant_Present => True,
7489 Expression => Relocate_Node (Prefix (Exp))));
7491 -- Similar processing for an unchecked conversion of an expression of
7492 -- the form v.all, where we want the same kind of treatment.
7494 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7495 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
7496 then
7497 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
7498 goto Leave;
7500 -- If this is a type conversion, leave the type conversion and remove
7501 -- the side effects in the expression. This is important in several
7502 -- circumstances: for change of representations, and also when this is a
7503 -- view conversion to a smaller object, where gigi can end up creating
7504 -- its own temporary of the wrong size.
7506 elsif Nkind (Exp) = N_Type_Conversion then
7507 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
7508 goto Leave;
7510 -- If this is an unchecked conversion that Gigi can't handle, make
7511 -- a copy or a use a renaming to capture the value.
7513 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7514 and then not Safe_Unchecked_Type_Conversion (Exp)
7515 then
7516 if CW_Or_Has_Controlled_Part (Exp_Type) then
7518 -- Use a renaming to capture the expression, rather than create
7519 -- a controlled temporary.
7521 Def_Id := Build_Temporary (Loc, 'R', Exp);
7522 Res := New_Occurrence_Of (Def_Id, Loc);
7524 Insert_Action (Exp,
7525 Make_Object_Renaming_Declaration (Loc,
7526 Defining_Identifier => Def_Id,
7527 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7528 Name => Relocate_Node (Exp)));
7530 else
7531 Def_Id := Build_Temporary (Loc, 'R', Exp);
7532 Set_Etype (Def_Id, Exp_Type);
7533 Res := New_Occurrence_Of (Def_Id, Loc);
7535 E :=
7536 Make_Object_Declaration (Loc,
7537 Defining_Identifier => Def_Id,
7538 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7539 Constant_Present => not Is_Variable (Exp),
7540 Expression => Relocate_Node (Exp));
7542 Set_Assignment_OK (E);
7543 Insert_Action (Exp, E);
7544 end if;
7546 -- For expressions that denote objects, we can use a renaming scheme.
7547 -- This is needed for correctness in the case of a volatile object of
7548 -- a non-volatile type because the Make_Reference call of the "default"
7549 -- approach would generate an illegal access value (an access value
7550 -- cannot designate such an object - see Analyze_Reference).
7552 elsif Is_Object_Reference (Exp)
7553 and then Nkind (Exp) /= N_Function_Call
7555 -- In Ada 2012 a qualified expression is an object, but for purposes
7556 -- of removing side effects it still need to be transformed into a
7557 -- separate declaration, particularly in the case of an aggregate.
7559 and then Nkind (Exp) /= N_Qualified_Expression
7561 -- We skip using this scheme if we have an object of a volatile
7562 -- type and we do not have Name_Req set true (see comments for
7563 -- Side_Effect_Free).
7565 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
7566 then
7567 Def_Id := Build_Temporary (Loc, 'R', Exp);
7569 if Nkind (Exp) = N_Selected_Component
7570 and then Nkind (Prefix (Exp)) = N_Function_Call
7571 and then Is_Array_Type (Exp_Type)
7572 then
7573 -- Avoid generating a variable-sized temporary, by generating
7574 -- the renaming declaration just for the function call. The
7575 -- transformation could be refined to apply only when the array
7576 -- component is constrained by a discriminant???
7578 Res :=
7579 Make_Selected_Component (Loc,
7580 Prefix => New_Occurrence_Of (Def_Id, Loc),
7581 Selector_Name => Selector_Name (Exp));
7583 Insert_Action (Exp,
7584 Make_Object_Renaming_Declaration (Loc,
7585 Defining_Identifier => Def_Id,
7586 Subtype_Mark =>
7587 New_Occurrence_Of (Base_Type (Etype (Prefix (Exp))), Loc),
7588 Name => Relocate_Node (Prefix (Exp))));
7590 else
7591 Res := New_Occurrence_Of (Def_Id, Loc);
7593 Insert_Action (Exp,
7594 Make_Object_Renaming_Declaration (Loc,
7595 Defining_Identifier => Def_Id,
7596 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7597 Name => Relocate_Node (Exp)));
7598 end if;
7600 -- If this is a packed reference, or a selected component with
7601 -- a non-standard representation, a reference to the temporary
7602 -- will be replaced by a copy of the original expression (see
7603 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
7604 -- elaborated by gigi, and is of course not to be replaced in-line
7605 -- by the expression it renames, which would defeat the purpose of
7606 -- removing the side-effect.
7608 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
7609 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
7610 then
7611 null;
7612 else
7613 Set_Is_Renaming_Of_Object (Def_Id, False);
7614 end if;
7616 -- Otherwise we generate a reference to the value
7618 else
7619 -- An expression which is in SPARK mode is considered side effect
7620 -- free if the resulting value is captured by a variable or a
7621 -- constant.
7623 if GNATprove_Mode
7624 and then Nkind (Parent (Exp)) = N_Object_Declaration
7625 then
7626 goto Leave;
7627 end if;
7629 -- Special processing for function calls that return a limited type.
7630 -- We need to build a declaration that will enable build-in-place
7631 -- expansion of the call. This is not done if the context is already
7632 -- an object declaration, to prevent infinite recursion.
7634 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
7635 -- to accommodate functions returning limited objects by reference.
7637 if Ada_Version >= Ada_2005
7638 and then Nkind (Exp) = N_Function_Call
7639 and then Is_Limited_View (Etype (Exp))
7640 and then Nkind (Parent (Exp)) /= N_Object_Declaration
7641 then
7642 declare
7643 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
7644 Decl : Node_Id;
7646 begin
7647 Decl :=
7648 Make_Object_Declaration (Loc,
7649 Defining_Identifier => Obj,
7650 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7651 Expression => Relocate_Node (Exp));
7653 Insert_Action (Exp, Decl);
7654 Set_Etype (Obj, Exp_Type);
7655 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
7656 goto Leave;
7657 end;
7658 end if;
7660 Def_Id := Build_Temporary (Loc, 'R', Exp);
7662 -- The regular expansion of functions with side effects involves the
7663 -- generation of an access type to capture the return value found on
7664 -- the secondary stack. Since SPARK (and why) cannot process access
7665 -- types, use a different approach which ignores the secondary stack
7666 -- and "copies" the returned object.
7668 if GNATprove_Mode then
7669 Res := New_Occurrence_Of (Def_Id, Loc);
7670 Ref_Type := Exp_Type;
7672 -- Regular expansion utilizing an access type and 'reference
7674 else
7675 Res :=
7676 Make_Explicit_Dereference (Loc,
7677 Prefix => New_Occurrence_Of (Def_Id, Loc));
7679 -- Generate:
7680 -- type Ann is access all <Exp_Type>;
7682 Ref_Type := Make_Temporary (Loc, 'A');
7684 Ptr_Typ_Decl :=
7685 Make_Full_Type_Declaration (Loc,
7686 Defining_Identifier => Ref_Type,
7687 Type_Definition =>
7688 Make_Access_To_Object_Definition (Loc,
7689 All_Present => True,
7690 Subtype_Indication =>
7691 New_Occurrence_Of (Exp_Type, Loc)));
7693 Insert_Action (Exp, Ptr_Typ_Decl);
7694 end if;
7696 E := Exp;
7697 if Nkind (E) = N_Explicit_Dereference then
7698 New_Exp := Relocate_Node (Prefix (E));
7700 else
7701 E := Relocate_Node (E);
7703 -- Do not generate a 'reference in SPARK mode since the access
7704 -- type is not created in the first place.
7706 if GNATprove_Mode then
7707 New_Exp := E;
7709 -- Otherwise generate reference, marking the value as non-null
7710 -- since we know it cannot be null and we don't want a check.
7712 else
7713 New_Exp := Make_Reference (Loc, E);
7714 Set_Is_Known_Non_Null (Def_Id);
7715 end if;
7716 end if;
7718 if Is_Delayed_Aggregate (E) then
7720 -- The expansion of nested aggregates is delayed until the
7721 -- enclosing aggregate is expanded. As aggregates are often
7722 -- qualified, the predicate applies to qualified expressions as
7723 -- well, indicating that the enclosing aggregate has not been
7724 -- expanded yet. At this point the aggregate is part of a
7725 -- stand-alone declaration, and must be fully expanded.
7727 if Nkind (E) = N_Qualified_Expression then
7728 Set_Expansion_Delayed (Expression (E), False);
7729 Set_Analyzed (Expression (E), False);
7730 else
7731 Set_Expansion_Delayed (E, False);
7732 end if;
7734 Set_Analyzed (E, False);
7735 end if;
7737 Insert_Action (Exp,
7738 Make_Object_Declaration (Loc,
7739 Defining_Identifier => Def_Id,
7740 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
7741 Constant_Present => True,
7742 Expression => New_Exp));
7743 end if;
7745 -- Preserve the Assignment_OK flag in all copies, since at least one
7746 -- copy may be used in a context where this flag must be set (otherwise
7747 -- why would the flag be set in the first place).
7749 Set_Assignment_OK (Res, Assignment_OK (Exp));
7751 -- Finally rewrite the original expression and we are done
7753 Rewrite (Exp, Res);
7754 Analyze_And_Resolve (Exp, Exp_Type);
7756 <<Leave>>
7757 Scope_Suppress := Svg_Suppress;
7758 end Remove_Side_Effects;
7760 ---------------------------
7761 -- Represented_As_Scalar --
7762 ---------------------------
7764 function Represented_As_Scalar (T : Entity_Id) return Boolean is
7765 UT : constant Entity_Id := Underlying_Type (T);
7766 begin
7767 return Is_Scalar_Type (UT)
7768 or else (Is_Bit_Packed_Array (UT)
7769 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
7770 end Represented_As_Scalar;
7772 ------------------------------
7773 -- Requires_Cleanup_Actions --
7774 ------------------------------
7776 function Requires_Cleanup_Actions
7777 (N : Node_Id;
7778 Lib_Level : Boolean) return Boolean
7780 At_Lib_Level : constant Boolean :=
7781 Lib_Level
7782 and then Nkind_In (N, N_Package_Body,
7783 N_Package_Specification);
7784 -- N is at the library level if the top-most context is a package and
7785 -- the path taken to reach N does not inlcude non-package constructs.
7787 begin
7788 case Nkind (N) is
7789 when N_Accept_Statement |
7790 N_Block_Statement |
7791 N_Entry_Body |
7792 N_Package_Body |
7793 N_Protected_Body |
7794 N_Subprogram_Body |
7795 N_Task_Body =>
7796 return
7797 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
7798 or else
7799 (Present (Handled_Statement_Sequence (N))
7800 and then
7801 Requires_Cleanup_Actions
7802 (Statements (Handled_Statement_Sequence (N)),
7803 At_Lib_Level, True));
7805 when N_Package_Specification =>
7806 return
7807 Requires_Cleanup_Actions
7808 (Visible_Declarations (N), At_Lib_Level, True)
7809 or else
7810 Requires_Cleanup_Actions
7811 (Private_Declarations (N), At_Lib_Level, True);
7813 when others =>
7814 return False;
7815 end case;
7816 end Requires_Cleanup_Actions;
7818 ------------------------------
7819 -- Requires_Cleanup_Actions --
7820 ------------------------------
7822 function Requires_Cleanup_Actions
7823 (L : List_Id;
7824 Lib_Level : Boolean;
7825 Nested_Constructs : Boolean) return Boolean
7827 Decl : Node_Id;
7828 Expr : Node_Id;
7829 Obj_Id : Entity_Id;
7830 Obj_Typ : Entity_Id;
7831 Pack_Id : Entity_Id;
7832 Typ : Entity_Id;
7834 begin
7835 if No (L)
7836 or else Is_Empty_List (L)
7837 then
7838 return False;
7839 end if;
7841 Decl := First (L);
7842 while Present (Decl) loop
7844 -- Library-level tagged types
7846 if Nkind (Decl) = N_Full_Type_Declaration then
7847 Typ := Defining_Identifier (Decl);
7849 -- Ignored Ghost types do not need any cleanup actions because
7850 -- they will not appear in the final tree.
7852 if Is_Ignored_Ghost_Entity (Typ) then
7853 null;
7855 elsif Is_Tagged_Type (Typ)
7856 and then Is_Library_Level_Entity (Typ)
7857 and then Convention (Typ) = Convention_Ada
7858 and then Present (Access_Disp_Table (Typ))
7859 and then RTE_Available (RE_Unregister_Tag)
7860 and then not Is_Abstract_Type (Typ)
7861 and then not No_Run_Time_Mode
7862 then
7863 return True;
7864 end if;
7866 -- Regular object declarations
7868 elsif Nkind (Decl) = N_Object_Declaration then
7869 Obj_Id := Defining_Identifier (Decl);
7870 Obj_Typ := Base_Type (Etype (Obj_Id));
7871 Expr := Expression (Decl);
7873 -- Bypass any form of processing for objects which have their
7874 -- finalization disabled. This applies only to objects at the
7875 -- library level.
7877 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7878 null;
7880 -- Transient variables are treated separately in order to minimize
7881 -- the size of the generated code. See Exp_Ch7.Process_Transient_
7882 -- Objects.
7884 elsif Is_Processed_Transient (Obj_Id) then
7885 null;
7887 -- Ignored Ghost objects do not need any cleanup actions because
7888 -- they will not appear in the final tree.
7890 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
7891 null;
7893 -- The object is of the form:
7894 -- Obj : Typ [:= Expr];
7896 -- Do not process the incomplete view of a deferred constant. Do
7897 -- not consider tag-to-class-wide conversions.
7899 elsif not Is_Imported (Obj_Id)
7900 and then Needs_Finalization (Obj_Typ)
7901 and then not (Ekind (Obj_Id) = E_Constant
7902 and then not Has_Completion (Obj_Id))
7903 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
7904 then
7905 return True;
7907 -- The object is of the form:
7908 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
7910 -- Obj : Access_Typ :=
7911 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
7913 elsif Is_Access_Type (Obj_Typ)
7914 and then Needs_Finalization
7915 (Available_View (Designated_Type (Obj_Typ)))
7916 and then Present (Expr)
7917 and then
7918 (Is_Secondary_Stack_BIP_Func_Call (Expr)
7919 or else
7920 (Is_Non_BIP_Func_Call (Expr)
7921 and then not Is_Related_To_Func_Return (Obj_Id)))
7922 then
7923 return True;
7925 -- Processing for "hook" objects generated for controlled
7926 -- transients declared inside an Expression_With_Actions.
7928 elsif Is_Access_Type (Obj_Typ)
7929 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7930 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7931 N_Object_Declaration
7932 then
7933 return True;
7935 -- Processing for intermediate results of if expressions where
7936 -- one of the alternatives uses a controlled function call.
7938 elsif Is_Access_Type (Obj_Typ)
7939 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7940 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7941 N_Defining_Identifier
7942 and then Present (Expr)
7943 and then Nkind (Expr) = N_Null
7944 then
7945 return True;
7947 -- Simple protected objects which use type System.Tasking.
7948 -- Protected_Objects.Protection to manage their locks should be
7949 -- treated as controlled since they require manual cleanup.
7951 elsif Ekind (Obj_Id) = E_Variable
7952 and then (Is_Simple_Protected_Type (Obj_Typ)
7953 or else Has_Simple_Protected_Object (Obj_Typ))
7954 then
7955 return True;
7956 end if;
7958 -- Specific cases of object renamings
7960 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
7961 Obj_Id := Defining_Identifier (Decl);
7962 Obj_Typ := Base_Type (Etype (Obj_Id));
7964 -- Bypass any form of processing for objects which have their
7965 -- finalization disabled. This applies only to objects at the
7966 -- library level.
7968 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7969 null;
7971 -- Ignored Ghost object renamings do not need any cleanup actions
7972 -- because they will not appear in the final tree.
7974 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
7975 null;
7977 -- Return object of a build-in-place function. This case is
7978 -- recognized and marked by the expansion of an extended return
7979 -- statement (see Expand_N_Extended_Return_Statement).
7981 elsif Needs_Finalization (Obj_Typ)
7982 and then Is_Return_Object (Obj_Id)
7983 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7984 then
7985 return True;
7987 -- Detect a case where a source object has been initialized by
7988 -- a controlled function call or another object which was later
7989 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
7991 -- Obj1 : CW_Type := Src_Obj;
7992 -- Obj2 : CW_Type := Function_Call (...);
7994 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7995 -- Tmp : ... := Function_Call (...)'reference;
7996 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
7998 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
7999 return True;
8000 end if;
8002 -- Inspect the freeze node of an access-to-controlled type and look
8003 -- for a delayed finalization master. This case arises when the
8004 -- freeze actions are inserted at a later time than the expansion of
8005 -- the context. Since Build_Finalizer is never called on a single
8006 -- construct twice, the master will be ultimately left out and never
8007 -- finalized. This is also needed for freeze actions of designated
8008 -- types themselves, since in some cases the finalization master is
8009 -- associated with a designated type's freeze node rather than that
8010 -- of the access type (see handling for freeze actions in
8011 -- Build_Finalization_Master).
8013 elsif Nkind (Decl) = N_Freeze_Entity
8014 and then Present (Actions (Decl))
8015 then
8016 Typ := Entity (Decl);
8018 -- Freeze nodes for ignored Ghost types do not need cleanup
8019 -- actions because they will never appear in the final tree.
8021 if Is_Ignored_Ghost_Entity (Typ) then
8022 null;
8024 elsif ((Is_Access_Type (Typ)
8025 and then not Is_Access_Subprogram_Type (Typ)
8026 and then Needs_Finalization
8027 (Available_View (Designated_Type (Typ))))
8028 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
8029 and then Requires_Cleanup_Actions
8030 (Actions (Decl), Lib_Level, Nested_Constructs)
8031 then
8032 return True;
8033 end if;
8035 -- Nested package declarations
8037 elsif Nested_Constructs
8038 and then Nkind (Decl) = N_Package_Declaration
8039 then
8040 Pack_Id := Defining_Entity (Decl);
8042 -- Do not inspect an ignored Ghost package because all code found
8043 -- within will not appear in the final tree.
8045 if Is_Ignored_Ghost_Entity (Pack_Id) then
8046 null;
8048 elsif Ekind (Pack_Id) /= E_Generic_Package
8049 and then Requires_Cleanup_Actions
8050 (Specification (Decl), Lib_Level)
8051 then
8052 return True;
8053 end if;
8055 -- Nested package bodies
8057 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
8059 -- Do not inspect an ignored Ghost package body because all code
8060 -- found within will not appear in the final tree.
8062 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
8063 null;
8065 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
8066 and then Requires_Cleanup_Actions (Decl, Lib_Level)
8067 then
8068 return True;
8069 end if;
8071 elsif Nkind (Decl) = N_Block_Statement
8072 and then
8074 -- Handle a rare case caused by a controlled transient variable
8075 -- created as part of a record init proc. The variable is wrapped
8076 -- in a block, but the block is not associated with a transient
8077 -- scope.
8079 (Inside_Init_Proc
8081 -- Handle the case where the original context has been wrapped in
8082 -- a block to avoid interference between exception handlers and
8083 -- At_End handlers. Treat the block as transparent and process its
8084 -- contents.
8086 or else Is_Finalization_Wrapper (Decl))
8087 then
8088 if Requires_Cleanup_Actions (Decl, Lib_Level) then
8089 return True;
8090 end if;
8091 end if;
8093 Next (Decl);
8094 end loop;
8096 return False;
8097 end Requires_Cleanup_Actions;
8099 ------------------------------------
8100 -- Safe_Unchecked_Type_Conversion --
8101 ------------------------------------
8103 -- Note: this function knows quite a bit about the exact requirements of
8104 -- Gigi with respect to unchecked type conversions, and its code must be
8105 -- coordinated with any changes in Gigi in this area.
8107 -- The above requirements should be documented in Sinfo ???
8109 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
8110 Otyp : Entity_Id;
8111 Ityp : Entity_Id;
8112 Oalign : Uint;
8113 Ialign : Uint;
8114 Pexp : constant Node_Id := Parent (Exp);
8116 begin
8117 -- If the expression is the RHS of an assignment or object declaration
8118 -- we are always OK because there will always be a target.
8120 -- Object renaming declarations, (generated for view conversions of
8121 -- actuals in inlined calls), like object declarations, provide an
8122 -- explicit type, and are safe as well.
8124 if (Nkind (Pexp) = N_Assignment_Statement
8125 and then Expression (Pexp) = Exp)
8126 or else Nkind_In (Pexp, N_Object_Declaration,
8127 N_Object_Renaming_Declaration)
8128 then
8129 return True;
8131 -- If the expression is the prefix of an N_Selected_Component we should
8132 -- also be OK because GCC knows to look inside the conversion except if
8133 -- the type is discriminated. We assume that we are OK anyway if the
8134 -- type is not set yet or if it is controlled since we can't afford to
8135 -- introduce a temporary in this case.
8137 elsif Nkind (Pexp) = N_Selected_Component
8138 and then Prefix (Pexp) = Exp
8139 then
8140 if No (Etype (Pexp)) then
8141 return True;
8142 else
8143 return
8144 not Has_Discriminants (Etype (Pexp))
8145 or else Is_Constrained (Etype (Pexp));
8146 end if;
8147 end if;
8149 -- Set the output type, this comes from Etype if it is set, otherwise we
8150 -- take it from the subtype mark, which we assume was already fully
8151 -- analyzed.
8153 if Present (Etype (Exp)) then
8154 Otyp := Etype (Exp);
8155 else
8156 Otyp := Entity (Subtype_Mark (Exp));
8157 end if;
8159 -- The input type always comes from the expression, and we assume
8160 -- this is indeed always analyzed, so we can simply get the Etype.
8162 Ityp := Etype (Expression (Exp));
8164 -- Initialize alignments to unknown so far
8166 Oalign := No_Uint;
8167 Ialign := No_Uint;
8169 -- Replace a concurrent type by its corresponding record type and each
8170 -- type by its underlying type and do the tests on those. The original
8171 -- type may be a private type whose completion is a concurrent type, so
8172 -- find the underlying type first.
8174 if Present (Underlying_Type (Otyp)) then
8175 Otyp := Underlying_Type (Otyp);
8176 end if;
8178 if Present (Underlying_Type (Ityp)) then
8179 Ityp := Underlying_Type (Ityp);
8180 end if;
8182 if Is_Concurrent_Type (Otyp) then
8183 Otyp := Corresponding_Record_Type (Otyp);
8184 end if;
8186 if Is_Concurrent_Type (Ityp) then
8187 Ityp := Corresponding_Record_Type (Ityp);
8188 end if;
8190 -- If the base types are the same, we know there is no problem since
8191 -- this conversion will be a noop.
8193 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
8194 return True;
8196 -- Same if this is an upwards conversion of an untagged type, and there
8197 -- are no constraints involved (could be more general???)
8199 elsif Etype (Ityp) = Otyp
8200 and then not Is_Tagged_Type (Ityp)
8201 and then not Has_Discriminants (Ityp)
8202 and then No (First_Rep_Item (Base_Type (Ityp)))
8203 then
8204 return True;
8206 -- If the expression has an access type (object or subprogram) we assume
8207 -- that the conversion is safe, because the size of the target is safe,
8208 -- even if it is a record (which might be treated as having unknown size
8209 -- at this point).
8211 elsif Is_Access_Type (Ityp) then
8212 return True;
8214 -- If the size of output type is known at compile time, there is never
8215 -- a problem. Note that unconstrained records are considered to be of
8216 -- known size, but we can't consider them that way here, because we are
8217 -- talking about the actual size of the object.
8219 -- We also make sure that in addition to the size being known, we do not
8220 -- have a case which might generate an embarrassingly large temp in
8221 -- stack checking mode.
8223 elsif Size_Known_At_Compile_Time (Otyp)
8224 and then
8225 (not Stack_Checking_Enabled
8226 or else not May_Generate_Large_Temp (Otyp))
8227 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
8228 then
8229 return True;
8231 -- If either type is tagged, then we know the alignment is OK so
8232 -- Gigi will be able to use pointer punning.
8234 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
8235 return True;
8237 -- If either type is a limited record type, we cannot do a copy, so say
8238 -- safe since there's nothing else we can do.
8240 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
8241 return True;
8243 -- Conversions to and from packed array types are always ignored and
8244 -- hence are safe.
8246 elsif Is_Packed_Array_Impl_Type (Otyp)
8247 or else Is_Packed_Array_Impl_Type (Ityp)
8248 then
8249 return True;
8250 end if;
8252 -- The only other cases known to be safe is if the input type's
8253 -- alignment is known to be at least the maximum alignment for the
8254 -- target or if both alignments are known and the output type's
8255 -- alignment is no stricter than the input's. We can use the component
8256 -- type alignement for an array if a type is an unpacked array type.
8258 if Present (Alignment_Clause (Otyp)) then
8259 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
8261 elsif Is_Array_Type (Otyp)
8262 and then Present (Alignment_Clause (Component_Type (Otyp)))
8263 then
8264 Oalign := Expr_Value (Expression (Alignment_Clause
8265 (Component_Type (Otyp))));
8266 end if;
8268 if Present (Alignment_Clause (Ityp)) then
8269 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
8271 elsif Is_Array_Type (Ityp)
8272 and then Present (Alignment_Clause (Component_Type (Ityp)))
8273 then
8274 Ialign := Expr_Value (Expression (Alignment_Clause
8275 (Component_Type (Ityp))));
8276 end if;
8278 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
8279 return True;
8281 elsif Ialign /= No_Uint
8282 and then Oalign /= No_Uint
8283 and then Ialign <= Oalign
8284 then
8285 return True;
8287 -- Otherwise, Gigi cannot handle this and we must make a temporary
8289 else
8290 return False;
8291 end if;
8292 end Safe_Unchecked_Type_Conversion;
8294 ---------------------------------
8295 -- Set_Current_Value_Condition --
8296 ---------------------------------
8298 -- Note: the implementation of this procedure is very closely tied to the
8299 -- implementation of Get_Current_Value_Condition. Here we set required
8300 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
8301 -- them, so they must have a consistent view.
8303 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
8305 procedure Set_Entity_Current_Value (N : Node_Id);
8306 -- If N is an entity reference, where the entity is of an appropriate
8307 -- kind, then set the current value of this entity to Cnode, unless
8308 -- there is already a definite value set there.
8310 procedure Set_Expression_Current_Value (N : Node_Id);
8311 -- If N is of an appropriate form, sets an appropriate entry in current
8312 -- value fields of relevant entities. Multiple entities can be affected
8313 -- in the case of an AND or AND THEN.
8315 ------------------------------
8316 -- Set_Entity_Current_Value --
8317 ------------------------------
8319 procedure Set_Entity_Current_Value (N : Node_Id) is
8320 begin
8321 if Is_Entity_Name (N) then
8322 declare
8323 Ent : constant Entity_Id := Entity (N);
8325 begin
8326 -- Don't capture if not safe to do so
8328 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
8329 return;
8330 end if;
8332 -- Here we have a case where the Current_Value field may need
8333 -- to be set. We set it if it is not already set to a compile
8334 -- time expression value.
8336 -- Note that this represents a decision that one condition
8337 -- blots out another previous one. That's certainly right if
8338 -- they occur at the same level. If the second one is nested,
8339 -- then the decision is neither right nor wrong (it would be
8340 -- equally OK to leave the outer one in place, or take the new
8341 -- inner one. Really we should record both, but our data
8342 -- structures are not that elaborate.
8344 if Nkind (Current_Value (Ent)) not in N_Subexpr then
8345 Set_Current_Value (Ent, Cnode);
8346 end if;
8347 end;
8348 end if;
8349 end Set_Entity_Current_Value;
8351 ----------------------------------
8352 -- Set_Expression_Current_Value --
8353 ----------------------------------
8355 procedure Set_Expression_Current_Value (N : Node_Id) is
8356 Cond : Node_Id;
8358 begin
8359 Cond := N;
8361 -- Loop to deal with (ignore for now) any NOT operators present. The
8362 -- presence of NOT operators will be handled properly when we call
8363 -- Get_Current_Value_Condition.
8365 while Nkind (Cond) = N_Op_Not loop
8366 Cond := Right_Opnd (Cond);
8367 end loop;
8369 -- For an AND or AND THEN, recursively process operands
8371 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
8372 Set_Expression_Current_Value (Left_Opnd (Cond));
8373 Set_Expression_Current_Value (Right_Opnd (Cond));
8374 return;
8375 end if;
8377 -- Check possible relational operator
8379 if Nkind (Cond) in N_Op_Compare then
8380 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
8381 Set_Entity_Current_Value (Left_Opnd (Cond));
8382 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
8383 Set_Entity_Current_Value (Right_Opnd (Cond));
8384 end if;
8386 elsif Nkind_In (Cond,
8387 N_Type_Conversion,
8388 N_Qualified_Expression,
8389 N_Expression_With_Actions)
8390 then
8391 Set_Expression_Current_Value (Expression (Cond));
8393 -- Check possible boolean variable reference
8395 else
8396 Set_Entity_Current_Value (Cond);
8397 end if;
8398 end Set_Expression_Current_Value;
8400 -- Start of processing for Set_Current_Value_Condition
8402 begin
8403 Set_Expression_Current_Value (Condition (Cnode));
8404 end Set_Current_Value_Condition;
8406 --------------------------
8407 -- Set_Elaboration_Flag --
8408 --------------------------
8410 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
8411 Loc : constant Source_Ptr := Sloc (N);
8412 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
8413 Asn : Node_Id;
8415 begin
8416 if Present (Ent) then
8418 -- Nothing to do if at the compilation unit level, because in this
8419 -- case the flag is set by the binder generated elaboration routine.
8421 if Nkind (Parent (N)) = N_Compilation_Unit then
8422 null;
8424 -- Here we do need to generate an assignment statement
8426 else
8427 Check_Restriction (No_Elaboration_Code, N);
8428 Asn :=
8429 Make_Assignment_Statement (Loc,
8430 Name => New_Occurrence_Of (Ent, Loc),
8431 Expression => Make_Integer_Literal (Loc, Uint_1));
8433 if Nkind (Parent (N)) = N_Subunit then
8434 Insert_After (Corresponding_Stub (Parent (N)), Asn);
8435 else
8436 Insert_After (N, Asn);
8437 end if;
8439 Analyze (Asn);
8441 -- Kill current value indication. This is necessary because the
8442 -- tests of this flag are inserted out of sequence and must not
8443 -- pick up bogus indications of the wrong constant value.
8445 Set_Current_Value (Ent, Empty);
8447 -- If the subprogram is in the current declarative part and
8448 -- 'access has been applied to it, generate an elaboration
8449 -- check at the beginning of the declarations of the body.
8451 if Nkind (N) = N_Subprogram_Body
8452 and then Address_Taken (Spec_Id)
8453 and then
8454 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
8455 then
8456 declare
8457 Loc : constant Source_Ptr := Sloc (N);
8458 Decls : constant List_Id := Declarations (N);
8459 Chk : Node_Id;
8461 begin
8462 -- No need to generate this check if first entry in the
8463 -- declaration list is a raise of Program_Error now.
8465 if Present (Decls)
8466 and then Nkind (First (Decls)) = N_Raise_Program_Error
8467 then
8468 return;
8469 end if;
8471 -- Otherwise generate the check
8473 Chk :=
8474 Make_Raise_Program_Error (Loc,
8475 Condition =>
8476 Make_Op_Eq (Loc,
8477 Left_Opnd => New_Occurrence_Of (Ent, Loc),
8478 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
8479 Reason => PE_Access_Before_Elaboration);
8481 if No (Decls) then
8482 Set_Declarations (N, New_List (Chk));
8483 else
8484 Prepend (Chk, Decls);
8485 end if;
8487 Analyze (Chk);
8488 end;
8489 end if;
8490 end if;
8491 end if;
8492 end Set_Elaboration_Flag;
8494 ----------------------------
8495 -- Set_Renamed_Subprogram --
8496 ----------------------------
8498 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
8499 begin
8500 -- If input node is an identifier, we can just reset it
8502 if Nkind (N) = N_Identifier then
8503 Set_Chars (N, Chars (E));
8504 Set_Entity (N, E);
8506 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
8508 else
8509 declare
8510 CS : constant Boolean := Comes_From_Source (N);
8511 begin
8512 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
8513 Set_Entity (N, E);
8514 Set_Comes_From_Source (N, CS);
8515 Set_Analyzed (N, True);
8516 end;
8517 end if;
8518 end Set_Renamed_Subprogram;
8520 ----------------------
8521 -- Side_Effect_Free --
8522 ----------------------
8524 function Side_Effect_Free
8525 (N : Node_Id;
8526 Name_Req : Boolean := False;
8527 Variable_Ref : Boolean := False) return Boolean
8529 Typ : constant Entity_Id := Etype (N);
8530 -- Result type of the expression
8532 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
8533 -- The argument N is a construct where the Prefix is dereferenced if it
8534 -- is an access type and the result is a variable. The call returns True
8535 -- if the construct is side effect free (not considering side effects in
8536 -- other than the prefix which are to be tested by the caller).
8538 function Within_In_Parameter (N : Node_Id) return Boolean;
8539 -- Determines if N is a subcomponent of a composite in-parameter. If so,
8540 -- N is not side-effect free when the actual is global and modifiable
8541 -- indirectly from within a subprogram, because it may be passed by
8542 -- reference. The front-end must be conservative here and assume that
8543 -- this may happen with any array or record type. On the other hand, we
8544 -- cannot create temporaries for all expressions for which this
8545 -- condition is true, for various reasons that might require clearing up
8546 -- ??? For example, discriminant references that appear out of place, or
8547 -- spurious type errors with class-wide expressions. As a result, we
8548 -- limit the transformation to loop bounds, which is so far the only
8549 -- case that requires it.
8551 -----------------------------
8552 -- Safe_Prefixed_Reference --
8553 -----------------------------
8555 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
8556 begin
8557 -- If prefix is not side effect free, definitely not safe
8559 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
8560 return False;
8562 -- If the prefix is of an access type that is not access-to-constant,
8563 -- then this construct is a variable reference, which means it is to
8564 -- be considered to have side effects if Variable_Ref is set True.
8566 elsif Is_Access_Type (Etype (Prefix (N)))
8567 and then not Is_Access_Constant (Etype (Prefix (N)))
8568 and then Variable_Ref
8569 then
8570 -- Exception is a prefix that is the result of a previous removal
8571 -- of side-effects.
8573 return Is_Entity_Name (Prefix (N))
8574 and then not Comes_From_Source (Prefix (N))
8575 and then Ekind (Entity (Prefix (N))) = E_Constant
8576 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
8578 -- If the prefix is an explicit dereference then this construct is a
8579 -- variable reference, which means it is to be considered to have
8580 -- side effects if Variable_Ref is True.
8582 -- We do NOT exclude dereferences of access-to-constant types because
8583 -- we handle them as constant view of variables.
8585 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
8586 and then Variable_Ref
8587 then
8588 return False;
8590 -- Note: The following test is the simplest way of solving a complex
8591 -- problem uncovered by the following test (Side effect on loop bound
8592 -- that is a subcomponent of a global variable:
8594 -- with Text_Io; use Text_Io;
8595 -- procedure Tloop is
8596 -- type X is
8597 -- record
8598 -- V : Natural := 4;
8599 -- S : String (1..5) := (others => 'a');
8600 -- end record;
8601 -- X1 : X;
8603 -- procedure Modi;
8605 -- generic
8606 -- with procedure Action;
8607 -- procedure Loop_G (Arg : X; Msg : String)
8609 -- procedure Loop_G (Arg : X; Msg : String) is
8610 -- begin
8611 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
8612 -- & Natural'Image (Arg.V));
8613 -- for Index in 1 .. Arg.V loop
8614 -- Text_Io.Put_Line
8615 -- (Natural'Image (Index) & " " & Arg.S (Index));
8616 -- if Index > 2 then
8617 -- Modi;
8618 -- end if;
8619 -- end loop;
8620 -- Put_Line ("end loop_g " & Msg);
8621 -- end;
8623 -- procedure Loop1 is new Loop_G (Modi);
8624 -- procedure Modi is
8625 -- begin
8626 -- X1.V := 1;
8627 -- Loop1 (X1, "from modi");
8628 -- end;
8630 -- begin
8631 -- Loop1 (X1, "initial");
8632 -- end;
8634 -- The output of the above program should be:
8636 -- begin loop_g initial will loop till: 4
8637 -- 1 a
8638 -- 2 a
8639 -- 3 a
8640 -- begin loop_g from modi will loop till: 1
8641 -- 1 a
8642 -- end loop_g from modi
8643 -- 4 a
8644 -- begin loop_g from modi will loop till: 1
8645 -- 1 a
8646 -- end loop_g from modi
8647 -- end loop_g initial
8649 -- If a loop bound is a subcomponent of a global variable, a
8650 -- modification of that variable within the loop may incorrectly
8651 -- affect the execution of the loop.
8653 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
8654 and then Within_In_Parameter (Prefix (N))
8655 and then Variable_Ref
8656 then
8657 return False;
8659 -- All other cases are side effect free
8661 else
8662 return True;
8663 end if;
8664 end Safe_Prefixed_Reference;
8666 -------------------------
8667 -- Within_In_Parameter --
8668 -------------------------
8670 function Within_In_Parameter (N : Node_Id) return Boolean is
8671 begin
8672 if not Comes_From_Source (N) then
8673 return False;
8675 elsif Is_Entity_Name (N) then
8676 return Ekind (Entity (N)) = E_In_Parameter;
8678 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8679 return Within_In_Parameter (Prefix (N));
8681 else
8682 return False;
8683 end if;
8684 end Within_In_Parameter;
8686 -- Start of processing for Side_Effect_Free
8688 begin
8689 -- If volatile reference, always consider it to have side effects
8691 if Is_Volatile_Reference (N) then
8692 return False;
8693 end if;
8695 -- Note on checks that could raise Constraint_Error. Strictly, if we
8696 -- take advantage of 11.6, these checks do not count as side effects.
8697 -- However, we would prefer to consider that they are side effects,
8698 -- since the backend CSE does not work very well on expressions which
8699 -- can raise Constraint_Error. On the other hand if we don't consider
8700 -- them to be side effect free, then we get some awkward expansions
8701 -- in -gnato mode, resulting in code insertions at a point where we
8702 -- do not have a clear model for performing the insertions.
8704 -- Special handling for entity names
8706 if Is_Entity_Name (N) then
8708 -- A type reference is always side effect free
8710 if Is_Type (Entity (N)) then
8711 return True;
8713 -- Variables are considered to be a side effect if Variable_Ref
8714 -- is set or if we have a volatile reference and Name_Req is off.
8715 -- If Name_Req is True then we can't help returning a name which
8716 -- effectively allows multiple references in any case.
8718 elsif Is_Variable (N, Use_Original_Node => False) then
8719 return not Variable_Ref
8720 and then (not Is_Volatile_Reference (N) or else Name_Req);
8722 -- Any other entity (e.g. a subtype name) is definitely side
8723 -- effect free.
8725 else
8726 return True;
8727 end if;
8729 -- A value known at compile time is always side effect free
8731 elsif Compile_Time_Known_Value (N) then
8732 return True;
8734 -- A variable renaming is not side-effect free, because the renaming
8735 -- will function like a macro in the front-end in some cases, and an
8736 -- assignment can modify the component designated by N, so we need to
8737 -- create a temporary for it.
8739 -- The guard testing for Entity being present is needed at least in
8740 -- the case of rewritten predicate expressions, and may well also be
8741 -- appropriate elsewhere. Obviously we can't go testing the entity
8742 -- field if it does not exist, so it's reasonable to say that this is
8743 -- not the renaming case if it does not exist.
8745 elsif Is_Entity_Name (Original_Node (N))
8746 and then Present (Entity (Original_Node (N)))
8747 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
8748 and then Ekind (Entity (Original_Node (N))) /= E_Constant
8749 then
8750 declare
8751 RO : constant Node_Id :=
8752 Renamed_Object (Entity (Original_Node (N)));
8754 begin
8755 -- If the renamed object is an indexed component, or an
8756 -- explicit dereference, then the designated object could
8757 -- be modified by an assignment.
8759 if Nkind_In (RO, N_Indexed_Component,
8760 N_Explicit_Dereference)
8761 then
8762 return False;
8764 -- A selected component must have a safe prefix
8766 elsif Nkind (RO) = N_Selected_Component then
8767 return Safe_Prefixed_Reference (RO);
8769 -- In all other cases, designated object cannot be changed so
8770 -- we are side effect free.
8772 else
8773 return True;
8774 end if;
8775 end;
8777 -- Remove_Side_Effects generates an object renaming declaration to
8778 -- capture the expression of a class-wide expression. In VM targets
8779 -- the frontend performs no expansion for dispatching calls to
8780 -- class- wide types since they are handled by the VM. Hence, we must
8781 -- locate here if this node corresponds to a previous invocation of
8782 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
8784 elsif VM_Target /= No_VM
8785 and then not Comes_From_Source (N)
8786 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
8787 and then Is_Class_Wide_Type (Typ)
8788 then
8789 return True;
8790 end if;
8792 -- For other than entity names and compile time known values,
8793 -- check the node kind for special processing.
8795 case Nkind (N) is
8797 -- An attribute reference is side effect free if its expressions
8798 -- are side effect free and its prefix is side effect free or
8799 -- is an entity reference.
8801 -- Is this right? what about x'first where x is a variable???
8803 when N_Attribute_Reference =>
8804 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
8805 and then Attribute_Name (N) /= Name_Input
8806 and then (Is_Entity_Name (Prefix (N))
8807 or else Side_Effect_Free
8808 (Prefix (N), Name_Req, Variable_Ref));
8810 -- A binary operator is side effect free if and both operands are
8811 -- side effect free. For this purpose binary operators include
8812 -- membership tests and short circuit forms.
8814 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
8815 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
8816 and then
8817 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
8819 -- An explicit dereference is side effect free only if it is
8820 -- a side effect free prefixed reference.
8822 when N_Explicit_Dereference =>
8823 return Safe_Prefixed_Reference (N);
8825 -- An expression with action is side effect free if its expression
8826 -- is side effect free and it has no actions.
8828 when N_Expression_With_Actions =>
8829 return Is_Empty_List (Actions (N))
8830 and then
8831 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8833 -- A call to _rep_to_pos is side effect free, since we generate
8834 -- this pure function call ourselves. Moreover it is critically
8835 -- important to make this exception, since otherwise we can have
8836 -- discriminants in array components which don't look side effect
8837 -- free in the case of an array whose index type is an enumeration
8838 -- type with an enumeration rep clause.
8840 -- All other function calls are not side effect free
8842 when N_Function_Call =>
8843 return Nkind (Name (N)) = N_Identifier
8844 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
8845 and then
8846 Side_Effect_Free
8847 (First (Parameter_Associations (N)), Name_Req, Variable_Ref);
8849 -- An IF expression is side effect free if it's of a scalar type, and
8850 -- all its components are all side effect free (conditions and then
8851 -- actions and else actions). We restrict to scalar types, since it
8852 -- is annoying to deal with things like (if A then B else C)'First
8853 -- where the type involved is a string type.
8855 when N_If_Expression =>
8856 return Is_Scalar_Type (Typ)
8857 and then
8858 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref);
8860 -- An indexed component is side effect free if it is a side
8861 -- effect free prefixed reference and all the indexing
8862 -- expressions are side effect free.
8864 when N_Indexed_Component =>
8865 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
8866 and then Safe_Prefixed_Reference (N);
8868 -- A type qualification is side effect free if the expression
8869 -- is side effect free.
8871 when N_Qualified_Expression =>
8872 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8874 -- A selected component is side effect free only if it is a side
8875 -- effect free prefixed reference. If it designates a component
8876 -- with a rep. clause it must be treated has having a potential
8877 -- side effect, because it may be modified through a renaming, and
8878 -- a subsequent use of the renaming as a macro will yield the
8879 -- wrong value. This complex interaction between renaming and
8880 -- removing side effects is a reminder that the latter has become
8881 -- a headache to maintain, and that it should be removed in favor
8882 -- of the gcc mechanism to capture values ???
8884 when N_Selected_Component =>
8885 if Nkind (Parent (N)) = N_Explicit_Dereference
8886 and then Has_Non_Standard_Rep (Designated_Type (Typ))
8887 then
8888 return False;
8889 else
8890 return Safe_Prefixed_Reference (N);
8891 end if;
8893 -- A range is side effect free if the bounds are side effect free
8895 when N_Range =>
8896 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
8897 and then
8898 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
8900 -- A slice is side effect free if it is a side effect free
8901 -- prefixed reference and the bounds are side effect free.
8903 when N_Slice =>
8904 return Side_Effect_Free
8905 (Discrete_Range (N), Name_Req, Variable_Ref)
8906 and then Safe_Prefixed_Reference (N);
8908 -- A type conversion is side effect free if the expression to be
8909 -- converted is side effect free.
8911 when N_Type_Conversion =>
8912 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8914 -- A unary operator is side effect free if the operand
8915 -- is side effect free.
8917 when N_Unary_Op =>
8918 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
8920 -- An unchecked type conversion is side effect free only if it
8921 -- is safe and its argument is side effect free.
8923 when N_Unchecked_Type_Conversion =>
8924 return Safe_Unchecked_Type_Conversion (N)
8925 and then
8926 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8928 -- An unchecked expression is side effect free if its expression
8929 -- is side effect free.
8931 when N_Unchecked_Expression =>
8932 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8934 -- A literal is side effect free
8936 when N_Character_Literal |
8937 N_Integer_Literal |
8938 N_Real_Literal |
8939 N_String_Literal =>
8940 return True;
8942 -- We consider that anything else has side effects. This is a bit
8943 -- crude, but we are pretty close for most common cases, and we
8944 -- are certainly correct (i.e. we never return True when the
8945 -- answer should be False).
8947 when others =>
8948 return False;
8949 end case;
8950 end Side_Effect_Free;
8952 -- A list is side effect free if all elements of the list are side
8953 -- effect free.
8955 function Side_Effect_Free
8956 (L : List_Id;
8957 Name_Req : Boolean := False;
8958 Variable_Ref : Boolean := False) return Boolean
8960 N : Node_Id;
8962 begin
8963 if L = No_List or else L = Error_List then
8964 return True;
8966 else
8967 N := First (L);
8968 while Present (N) loop
8969 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
8970 return False;
8971 else
8972 Next (N);
8973 end if;
8974 end loop;
8976 return True;
8977 end if;
8978 end Side_Effect_Free;
8980 ----------------------------------
8981 -- Silly_Boolean_Array_Not_Test --
8982 ----------------------------------
8984 -- This procedure implements an odd and silly test. We explicitly check
8985 -- for the case where the 'First of the component type is equal to the
8986 -- 'Last of this component type, and if this is the case, we make sure
8987 -- that constraint error is raised. The reason is that the NOT is bound
8988 -- to cause CE in this case, and we will not otherwise catch it.
8990 -- No such check is required for AND and OR, since for both these cases
8991 -- False op False = False, and True op True = True. For the XOR case,
8992 -- see Silly_Boolean_Array_Xor_Test.
8994 -- Believe it or not, this was reported as a bug. Note that nearly always,
8995 -- the test will evaluate statically to False, so the code will be
8996 -- statically removed, and no extra overhead caused.
8998 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
8999 Loc : constant Source_Ptr := Sloc (N);
9000 CT : constant Entity_Id := Component_Type (T);
9002 begin
9003 -- The check we install is
9005 -- constraint_error when
9006 -- component_type'first = component_type'last
9007 -- and then array_type'Length /= 0)
9009 -- We need the last guard because we don't want to raise CE for empty
9010 -- arrays since no out of range values result. (Empty arrays with a
9011 -- component type of True .. True -- very useful -- even the ACATS
9012 -- does not test that marginal case).
9014 Insert_Action (N,
9015 Make_Raise_Constraint_Error (Loc,
9016 Condition =>
9017 Make_And_Then (Loc,
9018 Left_Opnd =>
9019 Make_Op_Eq (Loc,
9020 Left_Opnd =>
9021 Make_Attribute_Reference (Loc,
9022 Prefix => New_Occurrence_Of (CT, Loc),
9023 Attribute_Name => Name_First),
9025 Right_Opnd =>
9026 Make_Attribute_Reference (Loc,
9027 Prefix => New_Occurrence_Of (CT, Loc),
9028 Attribute_Name => Name_Last)),
9030 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
9031 Reason => CE_Range_Check_Failed));
9032 end Silly_Boolean_Array_Not_Test;
9034 ----------------------------------
9035 -- Silly_Boolean_Array_Xor_Test --
9036 ----------------------------------
9038 -- This procedure implements an odd and silly test. We explicitly check
9039 -- for the XOR case where the component type is True .. True, since this
9040 -- will raise constraint error. A special check is required since CE
9041 -- will not be generated otherwise (cf Expand_Packed_Not).
9043 -- No such check is required for AND and OR, since for both these cases
9044 -- False op False = False, and True op True = True, and no check is
9045 -- required for the case of False .. False, since False xor False = False.
9046 -- See also Silly_Boolean_Array_Not_Test
9048 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
9049 Loc : constant Source_Ptr := Sloc (N);
9050 CT : constant Entity_Id := Component_Type (T);
9052 begin
9053 -- The check we install is
9055 -- constraint_error when
9056 -- Boolean (component_type'First)
9057 -- and then Boolean (component_type'Last)
9058 -- and then array_type'Length /= 0)
9060 -- We need the last guard because we don't want to raise CE for empty
9061 -- arrays since no out of range values result (Empty arrays with a
9062 -- component type of True .. True -- very useful -- even the ACATS
9063 -- does not test that marginal case).
9065 Insert_Action (N,
9066 Make_Raise_Constraint_Error (Loc,
9067 Condition =>
9068 Make_And_Then (Loc,
9069 Left_Opnd =>
9070 Make_And_Then (Loc,
9071 Left_Opnd =>
9072 Convert_To (Standard_Boolean,
9073 Make_Attribute_Reference (Loc,
9074 Prefix => New_Occurrence_Of (CT, Loc),
9075 Attribute_Name => Name_First)),
9077 Right_Opnd =>
9078 Convert_To (Standard_Boolean,
9079 Make_Attribute_Reference (Loc,
9080 Prefix => New_Occurrence_Of (CT, Loc),
9081 Attribute_Name => Name_Last))),
9083 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
9084 Reason => CE_Range_Check_Failed));
9085 end Silly_Boolean_Array_Xor_Test;
9087 --------------------------
9088 -- Target_Has_Fixed_Ops --
9089 --------------------------
9091 Integer_Sized_Small : Ureal;
9092 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
9093 -- called (we don't want to compute it more than once).
9095 Long_Integer_Sized_Small : Ureal;
9096 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
9097 -- is called (we don't want to compute it more than once)
9099 First_Time_For_THFO : Boolean := True;
9100 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
9102 function Target_Has_Fixed_Ops
9103 (Left_Typ : Entity_Id;
9104 Right_Typ : Entity_Id;
9105 Result_Typ : Entity_Id) return Boolean
9107 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
9108 -- Return True if the given type is a fixed-point type with a small
9109 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
9110 -- an absolute value less than 1.0. This is currently limited to
9111 -- fixed-point types that map to Integer or Long_Integer.
9113 ------------------------
9114 -- Is_Fractional_Type --
9115 ------------------------
9117 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
9118 begin
9119 if Esize (Typ) = Standard_Integer_Size then
9120 return Small_Value (Typ) = Integer_Sized_Small;
9122 elsif Esize (Typ) = Standard_Long_Integer_Size then
9123 return Small_Value (Typ) = Long_Integer_Sized_Small;
9125 else
9126 return False;
9127 end if;
9128 end Is_Fractional_Type;
9130 -- Start of processing for Target_Has_Fixed_Ops
9132 begin
9133 -- Return False if Fractional_Fixed_Ops_On_Target is false
9135 if not Fractional_Fixed_Ops_On_Target then
9136 return False;
9137 end if;
9139 -- Here the target has Fractional_Fixed_Ops, if first time, compute
9140 -- standard constants used by Is_Fractional_Type.
9142 if First_Time_For_THFO then
9143 First_Time_For_THFO := False;
9145 Integer_Sized_Small :=
9146 UR_From_Components
9147 (Num => Uint_1,
9148 Den => UI_From_Int (Standard_Integer_Size - 1),
9149 Rbase => 2);
9151 Long_Integer_Sized_Small :=
9152 UR_From_Components
9153 (Num => Uint_1,
9154 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
9155 Rbase => 2);
9156 end if;
9158 -- Return True if target supports fixed-by-fixed multiply/divide for
9159 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
9160 -- and result types are equivalent fractional types.
9162 return Is_Fractional_Type (Base_Type (Left_Typ))
9163 and then Is_Fractional_Type (Base_Type (Right_Typ))
9164 and then Is_Fractional_Type (Base_Type (Result_Typ))
9165 and then Esize (Left_Typ) = Esize (Right_Typ)
9166 and then Esize (Left_Typ) = Esize (Result_Typ);
9167 end Target_Has_Fixed_Ops;
9169 ------------------------------------------
9170 -- Type_May_Have_Bit_Aligned_Components --
9171 ------------------------------------------
9173 function Type_May_Have_Bit_Aligned_Components
9174 (Typ : Entity_Id) return Boolean
9176 begin
9177 -- Array type, check component type
9179 if Is_Array_Type (Typ) then
9180 return
9181 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
9183 -- Record type, check components
9185 elsif Is_Record_Type (Typ) then
9186 declare
9187 E : Entity_Id;
9189 begin
9190 E := First_Component_Or_Discriminant (Typ);
9191 while Present (E) loop
9192 if Component_May_Be_Bit_Aligned (E)
9193 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
9194 then
9195 return True;
9196 end if;
9198 Next_Component_Or_Discriminant (E);
9199 end loop;
9201 return False;
9202 end;
9204 -- Type other than array or record is always OK
9206 else
9207 return False;
9208 end if;
9209 end Type_May_Have_Bit_Aligned_Components;
9211 ----------------------------------
9212 -- Within_Case_Or_If_Expression --
9213 ----------------------------------
9215 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
9216 Par : Node_Id;
9218 begin
9219 -- Locate an enclosing case or if expression. Note that these constructs
9220 -- can be expanded into Expression_With_Actions, hence the test of the
9221 -- original node.
9223 Par := Parent (N);
9224 while Present (Par) loop
9225 if Nkind_In (Original_Node (Par), N_Case_Expression,
9226 N_If_Expression)
9227 then
9228 return True;
9230 -- Prevent the search from going too far
9232 elsif Is_Body_Or_Package_Declaration (Par) then
9233 return False;
9234 end if;
9236 Par := Parent (Par);
9237 end loop;
9239 return False;
9240 end Within_Case_Or_If_Expression;
9242 --------------------------------
9243 -- Within_Internal_Subprogram --
9244 --------------------------------
9246 function Within_Internal_Subprogram return Boolean is
9247 S : Entity_Id;
9249 begin
9250 S := Current_Scope;
9251 while Present (S) and then not Is_Subprogram (S) loop
9252 S := Scope (S);
9253 end loop;
9255 return Present (S)
9256 and then Get_TSS_Name (S) /= TSS_Null
9257 and then not Is_Predicate_Function (S);
9258 end Within_Internal_Subprogram;
9260 ----------------------------
9261 -- Wrap_Cleanup_Procedure --
9262 ----------------------------
9264 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
9265 Loc : constant Source_Ptr := Sloc (N);
9266 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
9267 Stmts : constant List_Id := Statements (Stseq);
9268 begin
9269 if Abort_Allowed then
9270 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
9271 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
9272 end if;
9273 end Wrap_Cleanup_Procedure;
9275 end Exp_Util;