Rebase.
[official-gcc.git] / gcc / ada / exp_util.adb
blobd5d269c28ca98c7f376ddf00abf986d0805baa90
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-2014, 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_Finalize_Address (Typ : Entity_Id) return Entity_Id;
416 -- Locate TSS primitive Finalize_Address in type Typ
418 function Find_Object (E : Node_Id) return Node_Id;
419 -- Given an arbitrary expression of an allocator, try to find an object
420 -- reference in it, otherwise return the original expression.
422 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
423 -- Determine whether subprogram Subp denotes a custom allocate or
424 -- deallocate.
426 ---------------------------
427 -- Find_Finalize_Address --
428 ---------------------------
430 function Find_Finalize_Address (Typ : Entity_Id) return Entity_Id is
431 Utyp : Entity_Id := Typ;
433 begin
434 -- Handle protected class-wide or task class-wide types
436 if Is_Class_Wide_Type (Utyp) then
437 if Is_Concurrent_Type (Root_Type (Utyp)) then
438 Utyp := Root_Type (Utyp);
440 elsif Is_Private_Type (Root_Type (Utyp))
441 and then Present (Full_View (Root_Type (Utyp)))
442 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
443 then
444 Utyp := Full_View (Root_Type (Utyp));
445 end if;
446 end if;
448 -- Handle private types
450 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
451 Utyp := Full_View (Utyp);
452 end if;
454 -- Handle protected and task types
456 if Is_Concurrent_Type (Utyp)
457 and then Present (Corresponding_Record_Type (Utyp))
458 then
459 Utyp := Corresponding_Record_Type (Utyp);
460 end if;
462 Utyp := Underlying_Type (Base_Type (Utyp));
464 -- Deal with untagged derivation of private views. If the parent is
465 -- now known to be protected, the finalization routine is the one
466 -- defined on the corresponding record of the ancestor (corresponding
467 -- records do not automatically inherit operations, but maybe they
468 -- should???)
470 if Is_Untagged_Derivation (Typ) then
471 if Is_Protected_Type (Typ) then
472 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
473 else
474 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
476 if Is_Protected_Type (Utyp) then
477 Utyp := Corresponding_Record_Type (Utyp);
478 end if;
479 end if;
480 end if;
482 -- If the underlying_type is a subtype, we are dealing with the
483 -- completion of a private type. We need to access the base type and
484 -- generate a conversion to it.
486 if Utyp /= Base_Type (Utyp) then
487 pragma Assert (Is_Private_Type (Typ));
489 Utyp := Base_Type (Utyp);
490 end if;
492 -- When dealing with an internally built full view for a type with
493 -- unknown discriminants, use the original record type.
495 if Is_Underlying_Record_View (Utyp) then
496 Utyp := Etype (Utyp);
497 end if;
499 return TSS (Utyp, TSS_Finalize_Address);
500 end Find_Finalize_Address;
502 -----------------
503 -- Find_Object --
504 -----------------
506 function Find_Object (E : Node_Id) return Node_Id is
507 Expr : Node_Id;
509 begin
510 pragma Assert (Is_Allocate);
512 Expr := E;
513 loop
514 if Nkind (Expr) = N_Explicit_Dereference then
515 Expr := Prefix (Expr);
517 elsif Nkind (Expr) = N_Qualified_Expression then
518 Expr := Expression (Expr);
520 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
522 -- When interface class-wide types are involved in allocation,
523 -- the expander introduces several levels of address arithmetic
524 -- to perform dispatch table displacement. In this scenario the
525 -- object appears as:
527 -- Tag_Ptr (Base_Address (<object>'Address))
529 -- Detect this case and utilize the whole expression as the
530 -- "object" since it now points to the proper dispatch table.
532 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
533 exit;
535 -- Continue to strip the object
537 else
538 Expr := Expression (Expr);
539 end if;
541 else
542 exit;
543 end if;
544 end loop;
546 return Expr;
547 end Find_Object;
549 ---------------------------------
550 -- Is_Allocate_Deallocate_Proc --
551 ---------------------------------
553 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
554 begin
555 -- Look for a subprogram body with only one statement which is a
556 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
558 if Ekind (Subp) = E_Procedure
559 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
560 then
561 declare
562 HSS : constant Node_Id :=
563 Handled_Statement_Sequence (Parent (Parent (Subp)));
564 Proc : Entity_Id;
566 begin
567 if Present (Statements (HSS))
568 and then Nkind (First (Statements (HSS))) =
569 N_Procedure_Call_Statement
570 then
571 Proc := Entity (Name (First (Statements (HSS))));
573 return
574 Is_RTE (Proc, RE_Allocate_Any_Controlled)
575 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
576 end if;
577 end;
578 end if;
580 return False;
581 end Is_Allocate_Deallocate_Proc;
583 -- Start of processing for Build_Allocate_Deallocate_Proc
585 begin
586 -- Obtain the attributes of the allocation / deallocation
588 if Nkind (N) = N_Free_Statement then
589 Expr := Expression (N);
590 Ptr_Typ := Base_Type (Etype (Expr));
591 Proc_To_Call := Procedure_To_Call (N);
593 else
594 if Nkind (N) = N_Object_Declaration then
595 Expr := Expression (N);
596 else
597 Expr := N;
598 end if;
600 -- In certain cases an allocator with a qualified expression may
601 -- be relocated and used as the initialization expression of a
602 -- temporary:
604 -- before:
605 -- Obj : Ptr_Typ := new Desig_Typ'(...);
607 -- after:
608 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
609 -- Obj : Ptr_Typ := Tmp;
611 -- Since the allocator is always marked as analyzed to avoid infinite
612 -- expansion, it will never be processed by this routine given that
613 -- the designated type needs finalization actions. Detect this case
614 -- and complete the expansion of the allocator.
616 if Nkind (Expr) = N_Identifier
617 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
618 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
619 then
620 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
621 return;
622 end if;
624 -- The allocator may have been rewritten into something else in which
625 -- case the expansion performed by this routine does not apply.
627 if Nkind (Expr) /= N_Allocator then
628 return;
629 end if;
631 Ptr_Typ := Base_Type (Etype (Expr));
632 Proc_To_Call := Procedure_To_Call (Expr);
633 end if;
635 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
636 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
638 -- Handle concurrent types
640 if Is_Concurrent_Type (Desig_Typ)
641 and then Present (Corresponding_Record_Type (Desig_Typ))
642 then
643 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
644 end if;
646 -- Do not process allocations / deallocations without a pool
648 if No (Pool_Id) then
649 return;
651 -- Do not process allocations on / deallocations from the secondary
652 -- stack.
654 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
655 return;
657 -- Do not replicate the machinery if the allocator / free has already
658 -- been expanded and has a custom Allocate / Deallocate.
660 elsif Present (Proc_To_Call)
661 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
662 then
663 return;
664 end if;
666 if Needs_Finalization (Desig_Typ) then
668 -- Certain run-time configurations and targets do not provide support
669 -- for controlled types.
671 if Restriction_Active (No_Finalization) then
672 return;
674 -- Do nothing if the access type may never allocate / deallocate
675 -- objects.
677 elsif No_Pool_Assigned (Ptr_Typ) then
678 return;
680 -- Access-to-controlled types are not supported on .NET/JVM since
681 -- these targets cannot support pools and address arithmetic.
683 elsif VM_Target /= No_VM then
684 return;
685 end if;
687 -- The allocation / deallocation of a controlled object must be
688 -- chained on / detached from a finalization master.
690 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
692 -- The only other kind of allocation / deallocation supported by this
693 -- routine is on / from a subpool.
695 elsif Nkind (Expr) = N_Allocator
696 and then No (Subpool_Handle_Name (Expr))
697 then
698 return;
699 end if;
701 declare
702 Loc : constant Source_Ptr := Sloc (N);
703 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
704 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
705 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
706 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
708 Actuals : List_Id;
709 Fin_Addr_Id : Entity_Id;
710 Fin_Mas_Act : Node_Id;
711 Fin_Mas_Id : Entity_Id;
712 Proc_To_Call : Entity_Id;
713 Subpool : Node_Id := Empty;
715 begin
716 -- Step 1: Construct all the actuals for the call to library routine
717 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
719 -- a) Storage pool
721 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
723 if Is_Allocate then
725 -- b) Subpool
727 if Nkind (Expr) = N_Allocator then
728 Subpool := Subpool_Handle_Name (Expr);
729 end if;
731 -- If a subpool is present it can be an arbitrary name, so make
732 -- the actual by copying the tree.
734 if Present (Subpool) then
735 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
736 else
737 Append_To (Actuals, Make_Null (Loc));
738 end if;
740 -- c) Finalization master
742 if Needs_Finalization (Desig_Typ) then
743 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
744 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
746 -- Handle the case where the master is actually a pointer to a
747 -- master. This case arises in build-in-place functions.
749 if Is_Access_Type (Etype (Fin_Mas_Id)) then
750 Append_To (Actuals, Fin_Mas_Act);
751 else
752 Append_To (Actuals,
753 Make_Attribute_Reference (Loc,
754 Prefix => Fin_Mas_Act,
755 Attribute_Name => Name_Unrestricted_Access));
756 end if;
757 else
758 Append_To (Actuals, Make_Null (Loc));
759 end if;
761 -- d) Finalize_Address
763 -- Primitive Finalize_Address is never generated in CodePeer mode
764 -- since it contains an Unchecked_Conversion.
766 if Needs_Finalization (Desig_Typ) and then not CodePeer_Mode then
767 Fin_Addr_Id := Find_Finalize_Address (Desig_Typ);
768 pragma Assert (Present (Fin_Addr_Id));
770 Append_To (Actuals,
771 Make_Attribute_Reference (Loc,
772 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
773 Attribute_Name => Name_Unrestricted_Access));
774 else
775 Append_To (Actuals, Make_Null (Loc));
776 end if;
777 end if;
779 -- e) Address
780 -- f) Storage_Size
781 -- g) Alignment
783 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
784 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
786 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
787 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
789 -- For deallocation of class-wide types we obtain the value of
790 -- alignment from the Type Specific Record of the deallocated object.
791 -- This is needed because the frontend expansion of class-wide types
792 -- into equivalent types confuses the backend.
794 else
795 -- Generate:
796 -- Obj.all'Alignment
798 -- ... because 'Alignment applied to class-wide types is expanded
799 -- into the code that reads the value of alignment from the TSD
800 -- (see Expand_N_Attribute_Reference)
802 Append_To (Actuals,
803 Unchecked_Convert_To (RTE (RE_Storage_Offset),
804 Make_Attribute_Reference (Loc,
805 Prefix =>
806 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
807 Attribute_Name => Name_Alignment)));
808 end if;
810 -- h) Is_Controlled
812 if Needs_Finalization (Desig_Typ) then
813 declare
814 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
815 Flag_Expr : Node_Id;
816 Param : Node_Id;
817 Temp : Node_Id;
819 begin
820 if Is_Allocate then
821 Temp := Find_Object (Expression (Expr));
822 else
823 Temp := Expr;
824 end if;
826 -- Processing for allocations where the expression is a subtype
827 -- indication.
829 if Is_Allocate
830 and then Is_Entity_Name (Temp)
831 and then Is_Type (Entity (Temp))
832 then
833 Flag_Expr :=
834 New_Occurrence_Of
835 (Boolean_Literals
836 (Needs_Finalization (Entity (Temp))), Loc);
838 -- The allocation / deallocation of a class-wide object relies
839 -- on a runtime check to determine whether the object is truly
840 -- controlled or not. Depending on this check, the finalization
841 -- machinery will request or reclaim extra storage reserved for
842 -- a list header.
844 elsif Is_Class_Wide_Type (Desig_Typ) then
846 -- Detect a special case where interface class-wide types
847 -- are involved as the object appears as:
849 -- Tag_Ptr (Base_Address (<object>'Address))
851 -- The expression already yields the proper tag, generate:
853 -- Temp.all
855 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
856 Param :=
857 Make_Explicit_Dereference (Loc,
858 Prefix => Relocate_Node (Temp));
860 -- In the default case, obtain the tag of the object about
861 -- to be allocated / deallocated. Generate:
863 -- Temp'Tag
865 else
866 Param :=
867 Make_Attribute_Reference (Loc,
868 Prefix => Relocate_Node (Temp),
869 Attribute_Name => Name_Tag);
870 end if;
872 -- Generate:
873 -- Needs_Finalization (<Param>)
875 Flag_Expr :=
876 Make_Function_Call (Loc,
877 Name =>
878 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
879 Parameter_Associations => New_List (Param));
881 -- Processing for generic actuals
883 elsif Is_Generic_Actual_Type (Desig_Typ) then
884 Flag_Expr :=
885 New_Occurrence_Of (Boolean_Literals
886 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
888 -- The object does not require any specialized checks, it is
889 -- known to be controlled.
891 else
892 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
893 end if;
895 -- Create the temporary which represents the finalization state
896 -- of the expression. Generate:
898 -- F : constant Boolean := <Flag_Expr>;
900 Insert_Action (N,
901 Make_Object_Declaration (Loc,
902 Defining_Identifier => Flag_Id,
903 Constant_Present => True,
904 Object_Definition =>
905 New_Occurrence_Of (Standard_Boolean, Loc),
906 Expression => Flag_Expr));
908 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
909 end;
911 -- The object is not controlled
913 else
914 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
915 end if;
917 -- i) On_Subpool
919 if Is_Allocate then
920 Append_To (Actuals,
921 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
922 end if;
924 -- Step 2: Build a wrapper Allocate / Deallocate which internally
925 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
927 -- Select the proper routine to call
929 if Is_Allocate then
930 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
931 else
932 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
933 end if;
935 -- Create a custom Allocate / Deallocate routine which has identical
936 -- profile to that of System.Storage_Pools.
938 Insert_Action (N,
939 Make_Subprogram_Body (Loc,
940 Specification =>
942 -- procedure Pnn
944 Make_Procedure_Specification (Loc,
945 Defining_Unit_Name => Proc_Id,
946 Parameter_Specifications => New_List (
948 -- P : Root_Storage_Pool
950 Make_Parameter_Specification (Loc,
951 Defining_Identifier => Make_Temporary (Loc, 'P'),
952 Parameter_Type =>
953 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
955 -- A : [out] Address
957 Make_Parameter_Specification (Loc,
958 Defining_Identifier => Addr_Id,
959 Out_Present => Is_Allocate,
960 Parameter_Type =>
961 New_Occurrence_Of (RTE (RE_Address), Loc)),
963 -- S : Storage_Count
965 Make_Parameter_Specification (Loc,
966 Defining_Identifier => Size_Id,
967 Parameter_Type =>
968 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
970 -- L : Storage_Count
972 Make_Parameter_Specification (Loc,
973 Defining_Identifier => Alig_Id,
974 Parameter_Type =>
975 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
977 Declarations => No_List,
979 Handled_Statement_Sequence =>
980 Make_Handled_Sequence_Of_Statements (Loc,
981 Statements => New_List (
982 Make_Procedure_Call_Statement (Loc,
983 Name => New_Occurrence_Of (Proc_To_Call, Loc),
984 Parameter_Associations => Actuals)))));
986 -- The newly generated Allocate / Deallocate becomes the default
987 -- procedure to call when the back end processes the allocation /
988 -- deallocation.
990 if Is_Allocate then
991 Set_Procedure_To_Call (Expr, Proc_Id);
992 else
993 Set_Procedure_To_Call (N, Proc_Id);
994 end if;
995 end;
996 end Build_Allocate_Deallocate_Proc;
998 ------------------------
999 -- Build_Runtime_Call --
1000 ------------------------
1002 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
1003 begin
1004 -- If entity is not available, we can skip making the call (this avoids
1005 -- junk duplicated error messages in a number of cases).
1007 if not RTE_Available (RE) then
1008 return Make_Null_Statement (Loc);
1009 else
1010 return
1011 Make_Procedure_Call_Statement (Loc,
1012 Name => New_Occurrence_Of (RTE (RE), Loc));
1013 end if;
1014 end Build_Runtime_Call;
1016 ------------------------
1017 -- Build_SS_Mark_Call --
1018 ------------------------
1020 function Build_SS_Mark_Call
1021 (Loc : Source_Ptr;
1022 Mark : Entity_Id) return Node_Id
1024 begin
1025 -- Generate:
1026 -- Mark : constant Mark_Id := SS_Mark;
1028 return
1029 Make_Object_Declaration (Loc,
1030 Defining_Identifier => Mark,
1031 Constant_Present => True,
1032 Object_Definition =>
1033 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
1034 Expression =>
1035 Make_Function_Call (Loc,
1036 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
1037 end Build_SS_Mark_Call;
1039 ---------------------------
1040 -- Build_SS_Release_Call --
1041 ---------------------------
1043 function Build_SS_Release_Call
1044 (Loc : Source_Ptr;
1045 Mark : Entity_Id) return Node_Id
1047 begin
1048 -- Generate:
1049 -- SS_Release (Mark);
1051 return
1052 Make_Procedure_Call_Statement (Loc,
1053 Name =>
1054 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
1055 Parameter_Associations => New_List (
1056 New_Occurrence_Of (Mark, Loc)));
1057 end Build_SS_Release_Call;
1059 ----------------------------
1060 -- Build_Task_Array_Image --
1061 ----------------------------
1063 -- This function generates the body for a function that constructs the
1064 -- image string for a task that is an array component. The function is
1065 -- local to the init proc for the array type, and is called for each one
1066 -- of the components. The constructed image has the form of an indexed
1067 -- component, whose prefix is the outer variable of the array type.
1068 -- The n-dimensional array type has known indexes Index, Index2...
1070 -- Id_Ref is an indexed component form created by the enclosing init proc.
1071 -- Its successive indexes are Val1, Val2, ... which are the loop variables
1072 -- in the loops that call the individual task init proc on each component.
1074 -- The generated function has the following structure:
1076 -- function F return String is
1077 -- Pref : string renames Task_Name;
1078 -- T1 : String := Index1'Image (Val1);
1079 -- ...
1080 -- Tn : String := indexn'image (Valn);
1081 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
1082 -- -- Len includes commas and the end parentheses.
1083 -- Res : String (1..Len);
1084 -- Pos : Integer := Pref'Length;
1086 -- begin
1087 -- Res (1 .. Pos) := Pref;
1088 -- Pos := Pos + 1;
1089 -- Res (Pos) := '(';
1090 -- Pos := Pos + 1;
1091 -- Res (Pos .. Pos + T1'Length - 1) := T1;
1092 -- Pos := Pos + T1'Length;
1093 -- Res (Pos) := '.';
1094 -- Pos := Pos + 1;
1095 -- ...
1096 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
1097 -- Res (Len) := ')';
1099 -- return Res;
1100 -- end F;
1102 -- Needless to say, multidimensional arrays of tasks are rare enough that
1103 -- the bulkiness of this code is not really a concern.
1105 function Build_Task_Array_Image
1106 (Loc : Source_Ptr;
1107 Id_Ref : Node_Id;
1108 A_Type : Entity_Id;
1109 Dyn : Boolean := False) return Node_Id
1111 Dims : constant Nat := Number_Dimensions (A_Type);
1112 -- Number of dimensions for array of tasks
1114 Temps : array (1 .. Dims) of Entity_Id;
1115 -- Array of temporaries to hold string for each index
1117 Indx : Node_Id;
1118 -- Index expression
1120 Len : Entity_Id;
1121 -- Total length of generated name
1123 Pos : Entity_Id;
1124 -- Running index for substring assignments
1126 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1127 -- Name of enclosing variable, prefix of resulting name
1129 Res : Entity_Id;
1130 -- String to hold result
1132 Val : Node_Id;
1133 -- Value of successive indexes
1135 Sum : Node_Id;
1136 -- Expression to compute total size of string
1138 T : Entity_Id;
1139 -- Entity for name at one index position
1141 Decls : constant List_Id := New_List;
1142 Stats : constant List_Id := New_List;
1144 begin
1145 -- For a dynamic task, the name comes from the target variable. For a
1146 -- static one it is a formal of the enclosing init proc.
1148 if Dyn then
1149 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1150 Append_To (Decls,
1151 Make_Object_Declaration (Loc,
1152 Defining_Identifier => Pref,
1153 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1154 Expression =>
1155 Make_String_Literal (Loc,
1156 Strval => String_From_Name_Buffer)));
1158 else
1159 Append_To (Decls,
1160 Make_Object_Renaming_Declaration (Loc,
1161 Defining_Identifier => Pref,
1162 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1163 Name => Make_Identifier (Loc, Name_uTask_Name)));
1164 end if;
1166 Indx := First_Index (A_Type);
1167 Val := First (Expressions (Id_Ref));
1169 for J in 1 .. Dims loop
1170 T := Make_Temporary (Loc, 'T');
1171 Temps (J) := T;
1173 Append_To (Decls,
1174 Make_Object_Declaration (Loc,
1175 Defining_Identifier => T,
1176 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1177 Expression =>
1178 Make_Attribute_Reference (Loc,
1179 Attribute_Name => Name_Image,
1180 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
1181 Expressions => New_List (New_Copy_Tree (Val)))));
1183 Next_Index (Indx);
1184 Next (Val);
1185 end loop;
1187 Sum := Make_Integer_Literal (Loc, Dims + 1);
1189 Sum :=
1190 Make_Op_Add (Loc,
1191 Left_Opnd => Sum,
1192 Right_Opnd =>
1193 Make_Attribute_Reference (Loc,
1194 Attribute_Name => Name_Length,
1195 Prefix => New_Occurrence_Of (Pref, Loc),
1196 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1198 for J in 1 .. Dims loop
1199 Sum :=
1200 Make_Op_Add (Loc,
1201 Left_Opnd => Sum,
1202 Right_Opnd =>
1203 Make_Attribute_Reference (Loc,
1204 Attribute_Name => Name_Length,
1205 Prefix =>
1206 New_Occurrence_Of (Temps (J), Loc),
1207 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1208 end loop;
1210 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1212 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
1214 Append_To (Stats,
1215 Make_Assignment_Statement (Loc,
1216 Name =>
1217 Make_Indexed_Component (Loc,
1218 Prefix => New_Occurrence_Of (Res, Loc),
1219 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1220 Expression =>
1221 Make_Character_Literal (Loc,
1222 Chars => Name_Find,
1223 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
1225 Append_To (Stats,
1226 Make_Assignment_Statement (Loc,
1227 Name => New_Occurrence_Of (Pos, Loc),
1228 Expression =>
1229 Make_Op_Add (Loc,
1230 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1231 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1233 for J in 1 .. Dims loop
1235 Append_To (Stats,
1236 Make_Assignment_Statement (Loc,
1237 Name =>
1238 Make_Slice (Loc,
1239 Prefix => New_Occurrence_Of (Res, Loc),
1240 Discrete_Range =>
1241 Make_Range (Loc,
1242 Low_Bound => New_Occurrence_Of (Pos, Loc),
1243 High_Bound =>
1244 Make_Op_Subtract (Loc,
1245 Left_Opnd =>
1246 Make_Op_Add (Loc,
1247 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1248 Right_Opnd =>
1249 Make_Attribute_Reference (Loc,
1250 Attribute_Name => Name_Length,
1251 Prefix =>
1252 New_Occurrence_Of (Temps (J), Loc),
1253 Expressions =>
1254 New_List (Make_Integer_Literal (Loc, 1)))),
1255 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
1257 Expression => New_Occurrence_Of (Temps (J), Loc)));
1259 if J < Dims then
1260 Append_To (Stats,
1261 Make_Assignment_Statement (Loc,
1262 Name => New_Occurrence_Of (Pos, Loc),
1263 Expression =>
1264 Make_Op_Add (Loc,
1265 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1266 Right_Opnd =>
1267 Make_Attribute_Reference (Loc,
1268 Attribute_Name => Name_Length,
1269 Prefix => New_Occurrence_Of (Temps (J), Loc),
1270 Expressions =>
1271 New_List (Make_Integer_Literal (Loc, 1))))));
1273 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
1275 Append_To (Stats,
1276 Make_Assignment_Statement (Loc,
1277 Name => Make_Indexed_Component (Loc,
1278 Prefix => New_Occurrence_Of (Res, Loc),
1279 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1280 Expression =>
1281 Make_Character_Literal (Loc,
1282 Chars => Name_Find,
1283 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
1285 Append_To (Stats,
1286 Make_Assignment_Statement (Loc,
1287 Name => New_Occurrence_Of (Pos, Loc),
1288 Expression =>
1289 Make_Op_Add (Loc,
1290 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1291 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1292 end if;
1293 end loop;
1295 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
1297 Append_To (Stats,
1298 Make_Assignment_Statement (Loc,
1299 Name =>
1300 Make_Indexed_Component (Loc,
1301 Prefix => New_Occurrence_Of (Res, Loc),
1302 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
1303 Expression =>
1304 Make_Character_Literal (Loc,
1305 Chars => Name_Find,
1306 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
1307 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1308 end Build_Task_Array_Image;
1310 ----------------------------
1311 -- Build_Task_Image_Decls --
1312 ----------------------------
1314 function Build_Task_Image_Decls
1315 (Loc : Source_Ptr;
1316 Id_Ref : Node_Id;
1317 A_Type : Entity_Id;
1318 In_Init_Proc : Boolean := False) return List_Id
1320 Decls : constant List_Id := New_List;
1321 T_Id : Entity_Id := Empty;
1322 Decl : Node_Id;
1323 Expr : Node_Id := Empty;
1324 Fun : Node_Id := Empty;
1325 Is_Dyn : constant Boolean :=
1326 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
1327 and then
1328 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
1330 begin
1331 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
1332 -- generate a dummy declaration only.
1334 if Restriction_Active (No_Implicit_Heap_Allocations)
1335 or else Global_Discard_Names
1336 then
1337 T_Id := Make_Temporary (Loc, 'J');
1338 Name_Len := 0;
1340 return
1341 New_List (
1342 Make_Object_Declaration (Loc,
1343 Defining_Identifier => T_Id,
1344 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1345 Expression =>
1346 Make_String_Literal (Loc,
1347 Strval => String_From_Name_Buffer)));
1349 else
1350 if Nkind (Id_Ref) = N_Identifier
1351 or else Nkind (Id_Ref) = N_Defining_Identifier
1352 then
1353 -- For a simple variable, the image of the task is built from
1354 -- the name of the variable. To avoid possible conflict with the
1355 -- anonymous type created for a single protected object, add a
1356 -- numeric suffix.
1358 T_Id :=
1359 Make_Defining_Identifier (Loc,
1360 New_External_Name (Chars (Id_Ref), 'T', 1));
1362 Get_Name_String (Chars (Id_Ref));
1364 Expr :=
1365 Make_String_Literal (Loc,
1366 Strval => String_From_Name_Buffer);
1368 elsif Nkind (Id_Ref) = N_Selected_Component then
1369 T_Id :=
1370 Make_Defining_Identifier (Loc,
1371 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
1372 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
1374 elsif Nkind (Id_Ref) = N_Indexed_Component then
1375 T_Id :=
1376 Make_Defining_Identifier (Loc,
1377 New_External_Name (Chars (A_Type), 'N'));
1379 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
1380 end if;
1381 end if;
1383 if Present (Fun) then
1384 Append (Fun, Decls);
1385 Expr := Make_Function_Call (Loc,
1386 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
1388 if not In_Init_Proc and then VM_Target = No_VM then
1389 Set_Uses_Sec_Stack (Defining_Entity (Fun));
1390 end if;
1391 end if;
1393 Decl := Make_Object_Declaration (Loc,
1394 Defining_Identifier => T_Id,
1395 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1396 Constant_Present => True,
1397 Expression => Expr);
1399 Append (Decl, Decls);
1400 return Decls;
1401 end Build_Task_Image_Decls;
1403 -------------------------------
1404 -- Build_Task_Image_Function --
1405 -------------------------------
1407 function Build_Task_Image_Function
1408 (Loc : Source_Ptr;
1409 Decls : List_Id;
1410 Stats : List_Id;
1411 Res : Entity_Id) return Node_Id
1413 Spec : Node_Id;
1415 begin
1416 Append_To (Stats,
1417 Make_Simple_Return_Statement (Loc,
1418 Expression => New_Occurrence_Of (Res, Loc)));
1420 Spec := Make_Function_Specification (Loc,
1421 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
1422 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
1424 -- Calls to 'Image use the secondary stack, which must be cleaned up
1425 -- after the task name is built.
1427 return Make_Subprogram_Body (Loc,
1428 Specification => Spec,
1429 Declarations => Decls,
1430 Handled_Statement_Sequence =>
1431 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
1432 end Build_Task_Image_Function;
1434 -----------------------------
1435 -- Build_Task_Image_Prefix --
1436 -----------------------------
1438 procedure Build_Task_Image_Prefix
1439 (Loc : Source_Ptr;
1440 Len : out Entity_Id;
1441 Res : out Entity_Id;
1442 Pos : out Entity_Id;
1443 Prefix : Entity_Id;
1444 Sum : Node_Id;
1445 Decls : List_Id;
1446 Stats : List_Id)
1448 begin
1449 Len := Make_Temporary (Loc, 'L', Sum);
1451 Append_To (Decls,
1452 Make_Object_Declaration (Loc,
1453 Defining_Identifier => Len,
1454 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
1455 Expression => Sum));
1457 Res := Make_Temporary (Loc, 'R');
1459 Append_To (Decls,
1460 Make_Object_Declaration (Loc,
1461 Defining_Identifier => Res,
1462 Object_Definition =>
1463 Make_Subtype_Indication (Loc,
1464 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1465 Constraint =>
1466 Make_Index_Or_Discriminant_Constraint (Loc,
1467 Constraints =>
1468 New_List (
1469 Make_Range (Loc,
1470 Low_Bound => Make_Integer_Literal (Loc, 1),
1471 High_Bound => New_Occurrence_Of (Len, Loc)))))));
1473 -- Indicate that the result is an internal temporary, so it does not
1474 -- receive a bogus initialization when declaration is expanded. This
1475 -- is both efficient, and prevents anomalies in the handling of
1476 -- dynamic objects on the secondary stack.
1478 Set_Is_Internal (Res);
1479 Pos := Make_Temporary (Loc, 'P');
1481 Append_To (Decls,
1482 Make_Object_Declaration (Loc,
1483 Defining_Identifier => Pos,
1484 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
1486 -- Pos := Prefix'Length;
1488 Append_To (Stats,
1489 Make_Assignment_Statement (Loc,
1490 Name => New_Occurrence_Of (Pos, Loc),
1491 Expression =>
1492 Make_Attribute_Reference (Loc,
1493 Attribute_Name => Name_Length,
1494 Prefix => New_Occurrence_Of (Prefix, Loc),
1495 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
1497 -- Res (1 .. Pos) := Prefix;
1499 Append_To (Stats,
1500 Make_Assignment_Statement (Loc,
1501 Name =>
1502 Make_Slice (Loc,
1503 Prefix => New_Occurrence_Of (Res, Loc),
1504 Discrete_Range =>
1505 Make_Range (Loc,
1506 Low_Bound => Make_Integer_Literal (Loc, 1),
1507 High_Bound => New_Occurrence_Of (Pos, Loc))),
1509 Expression => New_Occurrence_Of (Prefix, Loc)));
1511 Append_To (Stats,
1512 Make_Assignment_Statement (Loc,
1513 Name => New_Occurrence_Of (Pos, Loc),
1514 Expression =>
1515 Make_Op_Add (Loc,
1516 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1517 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1518 end Build_Task_Image_Prefix;
1520 -----------------------------
1521 -- Build_Task_Record_Image --
1522 -----------------------------
1524 function Build_Task_Record_Image
1525 (Loc : Source_Ptr;
1526 Id_Ref : Node_Id;
1527 Dyn : Boolean := False) return Node_Id
1529 Len : Entity_Id;
1530 -- Total length of generated name
1532 Pos : Entity_Id;
1533 -- Index into result
1535 Res : Entity_Id;
1536 -- String to hold result
1538 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
1539 -- Name of enclosing variable, prefix of resulting name
1541 Sum : Node_Id;
1542 -- Expression to compute total size of string
1544 Sel : Entity_Id;
1545 -- Entity for selector name
1547 Decls : constant List_Id := New_List;
1548 Stats : constant List_Id := New_List;
1550 begin
1551 -- For a dynamic task, the name comes from the target variable. For a
1552 -- static one it is a formal of the enclosing init proc.
1554 if Dyn then
1555 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
1556 Append_To (Decls,
1557 Make_Object_Declaration (Loc,
1558 Defining_Identifier => Pref,
1559 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1560 Expression =>
1561 Make_String_Literal (Loc,
1562 Strval => String_From_Name_Buffer)));
1564 else
1565 Append_To (Decls,
1566 Make_Object_Renaming_Declaration (Loc,
1567 Defining_Identifier => Pref,
1568 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
1569 Name => Make_Identifier (Loc, Name_uTask_Name)));
1570 end if;
1572 Sel := Make_Temporary (Loc, 'S');
1574 Get_Name_String (Chars (Selector_Name (Id_Ref)));
1576 Append_To (Decls,
1577 Make_Object_Declaration (Loc,
1578 Defining_Identifier => Sel,
1579 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1580 Expression =>
1581 Make_String_Literal (Loc,
1582 Strval => String_From_Name_Buffer)));
1584 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
1586 Sum :=
1587 Make_Op_Add (Loc,
1588 Left_Opnd => Sum,
1589 Right_Opnd =>
1590 Make_Attribute_Reference (Loc,
1591 Attribute_Name => Name_Length,
1592 Prefix =>
1593 New_Occurrence_Of (Pref, Loc),
1594 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
1596 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
1598 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
1600 -- Res (Pos) := '.';
1602 Append_To (Stats,
1603 Make_Assignment_Statement (Loc,
1604 Name => Make_Indexed_Component (Loc,
1605 Prefix => New_Occurrence_Of (Res, Loc),
1606 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
1607 Expression =>
1608 Make_Character_Literal (Loc,
1609 Chars => Name_Find,
1610 Char_Literal_Value =>
1611 UI_From_Int (Character'Pos ('.')))));
1613 Append_To (Stats,
1614 Make_Assignment_Statement (Loc,
1615 Name => New_Occurrence_Of (Pos, Loc),
1616 Expression =>
1617 Make_Op_Add (Loc,
1618 Left_Opnd => New_Occurrence_Of (Pos, Loc),
1619 Right_Opnd => Make_Integer_Literal (Loc, 1))));
1621 -- Res (Pos .. Len) := Selector;
1623 Append_To (Stats,
1624 Make_Assignment_Statement (Loc,
1625 Name => Make_Slice (Loc,
1626 Prefix => New_Occurrence_Of (Res, Loc),
1627 Discrete_Range =>
1628 Make_Range (Loc,
1629 Low_Bound => New_Occurrence_Of (Pos, Loc),
1630 High_Bound => New_Occurrence_Of (Len, Loc))),
1631 Expression => New_Occurrence_Of (Sel, Loc)));
1633 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
1634 end Build_Task_Record_Image;
1636 -----------------------------
1637 -- Check_Float_Op_Overflow --
1638 -----------------------------
1640 procedure Check_Float_Op_Overflow (N : Node_Id) is
1641 begin
1642 -- Return if no check needed
1644 if not Is_Floating_Point_Type (Etype (N))
1645 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
1647 -- In CodePeer_Mode, rely on the overflow check flag being set instead
1648 -- and do not expand the code for float overflow checking.
1650 or else CodePeer_Mode
1651 then
1652 return;
1653 end if;
1655 -- Otherwise we replace the expression by
1657 -- do Tnn : constant ftype := expression;
1658 -- constraint_error when not Tnn'Valid;
1659 -- in Tnn;
1661 declare
1662 Loc : constant Source_Ptr := Sloc (N);
1663 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
1664 Typ : constant Entity_Id := Etype (N);
1666 begin
1667 -- Turn off the Do_Overflow_Check flag, since we are doing that work
1668 -- right here. We also set the node as analyzed to prevent infinite
1669 -- recursion from repeating the operation in the expansion.
1671 Set_Do_Overflow_Check (N, False);
1672 Set_Analyzed (N, True);
1674 -- Do the rewrite to include the check
1676 Rewrite (N,
1677 Make_Expression_With_Actions (Loc,
1678 Actions => New_List (
1679 Make_Object_Declaration (Loc,
1680 Defining_Identifier => Tnn,
1681 Object_Definition => New_Occurrence_Of (Typ, Loc),
1682 Constant_Present => True,
1683 Expression => Relocate_Node (N)),
1684 Make_Raise_Constraint_Error (Loc,
1685 Condition =>
1686 Make_Op_Not (Loc,
1687 Right_Opnd =>
1688 Make_Attribute_Reference (Loc,
1689 Prefix => New_Occurrence_Of (Tnn, Loc),
1690 Attribute_Name => Name_Valid)),
1691 Reason => CE_Overflow_Check_Failed)),
1692 Expression => New_Occurrence_Of (Tnn, Loc)));
1694 Analyze_And_Resolve (N, Typ);
1695 end;
1696 end Check_Float_Op_Overflow;
1698 ----------------------------------
1699 -- Component_May_Be_Bit_Aligned --
1700 ----------------------------------
1702 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
1703 UT : Entity_Id;
1705 begin
1706 -- If no component clause, then everything is fine, since the back end
1707 -- never bit-misaligns by default, even if there is a pragma Packed for
1708 -- the record.
1710 if No (Comp) or else No (Component_Clause (Comp)) then
1711 return False;
1712 end if;
1714 UT := Underlying_Type (Etype (Comp));
1716 -- It is only array and record types that cause trouble
1718 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
1719 return False;
1721 -- If we know that we have a small (64 bits or less) record or small
1722 -- bit-packed array, then everything is fine, since the back end can
1723 -- handle these cases correctly.
1725 elsif Esize (Comp) <= 64
1726 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
1727 then
1728 return False;
1730 -- Otherwise if the component is not byte aligned, we know we have the
1731 -- nasty unaligned case.
1733 elsif Normalized_First_Bit (Comp) /= Uint_0
1734 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
1735 then
1736 return True;
1738 -- If we are large and byte aligned, then OK at this level
1740 else
1741 return False;
1742 end if;
1743 end Component_May_Be_Bit_Aligned;
1745 -----------------------------------
1746 -- Corresponding_Runtime_Package --
1747 -----------------------------------
1749 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1750 Pkg_Id : RTU_Id := RTU_Null;
1752 begin
1753 pragma Assert (Is_Concurrent_Type (Typ));
1755 if Ekind (Typ) in Protected_Kind then
1756 if Has_Entries (Typ)
1758 -- A protected type without entries that covers an interface and
1759 -- overrides the abstract routines with protected procedures is
1760 -- considered equivalent to a protected type with entries in the
1761 -- context of dispatching select statements. It is sufficient to
1762 -- check for the presence of an interface list in the declaration
1763 -- node to recognize this case.
1765 or else Present (Interface_List (Parent (Typ)))
1767 -- Protected types with interrupt handlers (when not using a
1768 -- restricted profile) are also considered equivalent to
1769 -- protected types with entries. The types which are used
1770 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
1771 -- are derived from Protection_Entries.
1773 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
1774 or else Has_Interrupt_Handler (Typ)
1775 then
1776 if Abort_Allowed
1777 or else Restriction_Active (No_Entry_Queue) = False
1778 or else Restriction_Active (No_Select_Statements) = False
1779 or else Number_Entries (Typ) > 1
1780 or else (Has_Attach_Handler (Typ)
1781 and then not Restricted_Profile)
1782 then
1783 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1784 else
1785 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1786 end if;
1788 else
1789 Pkg_Id := System_Tasking_Protected_Objects;
1790 end if;
1791 end if;
1793 return Pkg_Id;
1794 end Corresponding_Runtime_Package;
1796 -------------------------------
1797 -- Convert_To_Actual_Subtype --
1798 -------------------------------
1800 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1801 Act_ST : Entity_Id;
1803 begin
1804 Act_ST := Get_Actual_Subtype (Exp);
1806 if Act_ST = Etype (Exp) then
1807 return;
1808 else
1809 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
1810 Analyze_And_Resolve (Exp, Act_ST);
1811 end if;
1812 end Convert_To_Actual_Subtype;
1814 -----------------------------------
1815 -- Current_Sem_Unit_Declarations --
1816 -----------------------------------
1818 function Current_Sem_Unit_Declarations return List_Id is
1819 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1820 Decls : List_Id;
1822 begin
1823 -- If the current unit is a package body, locate the visible
1824 -- declarations of the package spec.
1826 if Nkind (U) = N_Package_Body then
1827 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1828 end if;
1830 if Nkind (U) = N_Package_Declaration then
1831 U := Specification (U);
1832 Decls := Visible_Declarations (U);
1834 if No (Decls) then
1835 Decls := New_List;
1836 Set_Visible_Declarations (U, Decls);
1837 end if;
1839 else
1840 Decls := Declarations (U);
1842 if No (Decls) then
1843 Decls := New_List;
1844 Set_Declarations (U, Decls);
1845 end if;
1846 end if;
1848 return Decls;
1849 end Current_Sem_Unit_Declarations;
1851 -----------------------
1852 -- Duplicate_Subexpr --
1853 -----------------------
1855 function Duplicate_Subexpr
1856 (Exp : Node_Id;
1857 Name_Req : Boolean := False;
1858 Renaming_Req : Boolean := False) return Node_Id
1860 begin
1861 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1862 return New_Copy_Tree (Exp);
1863 end Duplicate_Subexpr;
1865 ---------------------------------
1866 -- Duplicate_Subexpr_No_Checks --
1867 ---------------------------------
1869 function Duplicate_Subexpr_No_Checks
1870 (Exp : Node_Id;
1871 Name_Req : Boolean := False;
1872 Renaming_Req : Boolean := False) return Node_Id
1874 New_Exp : Node_Id;
1876 begin
1877 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1878 New_Exp := New_Copy_Tree (Exp);
1879 Remove_Checks (New_Exp);
1880 return New_Exp;
1881 end Duplicate_Subexpr_No_Checks;
1883 -----------------------------------
1884 -- Duplicate_Subexpr_Move_Checks --
1885 -----------------------------------
1887 function Duplicate_Subexpr_Move_Checks
1888 (Exp : Node_Id;
1889 Name_Req : Boolean := False;
1890 Renaming_Req : Boolean := False) return Node_Id
1892 New_Exp : Node_Id;
1894 begin
1895 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1896 New_Exp := New_Copy_Tree (Exp);
1897 Remove_Checks (Exp);
1898 return New_Exp;
1899 end Duplicate_Subexpr_Move_Checks;
1901 --------------------
1902 -- Ensure_Defined --
1903 --------------------
1905 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1906 IR : Node_Id;
1908 begin
1909 -- An itype reference must only be created if this is a local itype, so
1910 -- that gigi can elaborate it on the proper objstack.
1912 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
1913 IR := Make_Itype_Reference (Sloc (N));
1914 Set_Itype (IR, Typ);
1915 Insert_Action (N, IR);
1916 end if;
1917 end Ensure_Defined;
1919 --------------------
1920 -- Entry_Names_OK --
1921 --------------------
1923 function Entry_Names_OK return Boolean is
1924 begin
1925 return
1926 not Restricted_Profile
1927 and then not Global_Discard_Names
1928 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1929 and then not Restriction_Active (No_Local_Allocators);
1930 end Entry_Names_OK;
1932 -------------------
1933 -- Evaluate_Name --
1934 -------------------
1936 procedure Evaluate_Name (Nam : Node_Id) is
1937 K : constant Node_Kind := Nkind (Nam);
1939 begin
1940 -- For an explicit dereference, we simply force the evaluation of the
1941 -- name expression. The dereference provides a value that is the address
1942 -- for the renamed object, and it is precisely this value that we want
1943 -- to preserve.
1945 if K = N_Explicit_Dereference then
1946 Force_Evaluation (Prefix (Nam));
1948 -- For a selected component, we simply evaluate the prefix
1950 elsif K = N_Selected_Component then
1951 Evaluate_Name (Prefix (Nam));
1953 -- For an indexed component, or an attribute reference, we evaluate the
1954 -- prefix, which is itself a name, recursively, and then force the
1955 -- evaluation of all the subscripts (or attribute expressions).
1957 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
1958 Evaluate_Name (Prefix (Nam));
1960 declare
1961 E : Node_Id;
1963 begin
1964 E := First (Expressions (Nam));
1965 while Present (E) loop
1966 Force_Evaluation (E);
1968 if Original_Node (E) /= E then
1969 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
1970 end if;
1972 Next (E);
1973 end loop;
1974 end;
1976 -- For a slice, we evaluate the prefix, as for the indexed component
1977 -- case and then, if there is a range present, either directly or as the
1978 -- constraint of a discrete subtype indication, we evaluate the two
1979 -- bounds of this range.
1981 elsif K = N_Slice then
1982 Evaluate_Name (Prefix (Nam));
1983 Evaluate_Slice_Bounds (Nam);
1985 -- For a type conversion, the expression of the conversion must be the
1986 -- name of an object, and we simply need to evaluate this name.
1988 elsif K = N_Type_Conversion then
1989 Evaluate_Name (Expression (Nam));
1991 -- For a function call, we evaluate the call
1993 elsif K = N_Function_Call then
1994 Force_Evaluation (Nam);
1996 -- The remaining cases are direct name, operator symbol and character
1997 -- literal. In all these cases, we do nothing, since we want to
1998 -- reevaluate each time the renamed object is used.
2000 else
2001 return;
2002 end if;
2003 end Evaluate_Name;
2005 ---------------------------
2006 -- Evaluate_Slice_Bounds --
2007 ---------------------------
2009 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
2010 DR : constant Node_Id := Discrete_Range (Slice);
2011 Constr : Node_Id;
2012 Rexpr : Node_Id;
2014 begin
2015 if Nkind (DR) = N_Range then
2016 Force_Evaluation (Low_Bound (DR));
2017 Force_Evaluation (High_Bound (DR));
2019 elsif Nkind (DR) = N_Subtype_Indication then
2020 Constr := Constraint (DR);
2022 if Nkind (Constr) = N_Range_Constraint then
2023 Rexpr := Range_Expression (Constr);
2025 Force_Evaluation (Low_Bound (Rexpr));
2026 Force_Evaluation (High_Bound (Rexpr));
2027 end if;
2028 end if;
2029 end Evaluate_Slice_Bounds;
2031 ---------------------
2032 -- Evolve_And_Then --
2033 ---------------------
2035 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
2036 begin
2037 if No (Cond) then
2038 Cond := Cond1;
2039 else
2040 Cond :=
2041 Make_And_Then (Sloc (Cond1),
2042 Left_Opnd => Cond,
2043 Right_Opnd => Cond1);
2044 end if;
2045 end Evolve_And_Then;
2047 --------------------
2048 -- Evolve_Or_Else --
2049 --------------------
2051 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
2052 begin
2053 if No (Cond) then
2054 Cond := Cond1;
2055 else
2056 Cond :=
2057 Make_Or_Else (Sloc (Cond1),
2058 Left_Opnd => Cond,
2059 Right_Opnd => Cond1);
2060 end if;
2061 end Evolve_Or_Else;
2063 -----------------------------------------
2064 -- Expand_Static_Predicates_In_Choices --
2065 -----------------------------------------
2067 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
2068 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
2070 Choices : constant List_Id := Discrete_Choices (N);
2072 Choice : Node_Id;
2073 Next_C : Node_Id;
2074 P : Node_Id;
2075 C : Node_Id;
2077 begin
2078 Choice := First (Choices);
2079 while Present (Choice) loop
2080 Next_C := Next (Choice);
2082 -- Check for name of subtype with static predicate
2084 if Is_Entity_Name (Choice)
2085 and then Is_Type (Entity (Choice))
2086 and then Has_Predicates (Entity (Choice))
2087 then
2088 -- Loop through entries in predicate list, converting to choices
2089 -- and inserting in the list before the current choice. Note that
2090 -- if the list is empty, corresponding to a False predicate, then
2091 -- no choices are inserted.
2093 P := First (Static_Discrete_Predicate (Entity (Choice)));
2094 while Present (P) loop
2096 -- If low bound and high bounds are equal, copy simple choice
2098 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
2099 C := New_Copy (Low_Bound (P));
2101 -- Otherwise copy a range
2103 else
2104 C := New_Copy (P);
2105 end if;
2107 -- Change Sloc to referencing choice (rather than the Sloc of
2108 -- the predicate declaration element itself).
2110 Set_Sloc (C, Sloc (Choice));
2111 Insert_Before (Choice, C);
2112 Next (P);
2113 end loop;
2115 -- Delete the predicated entry
2117 Remove (Choice);
2118 end if;
2120 -- Move to next choice to check
2122 Choice := Next_C;
2123 end loop;
2124 end Expand_Static_Predicates_In_Choices;
2126 ------------------------------
2127 -- Expand_Subtype_From_Expr --
2128 ------------------------------
2130 -- This function is applicable for both static and dynamic allocation of
2131 -- objects which are constrained by an initial expression. Basically it
2132 -- transforms an unconstrained subtype indication into a constrained one.
2134 -- The expression may also be transformed in certain cases in order to
2135 -- avoid multiple evaluation. In the static allocation case, the general
2136 -- scheme is:
2138 -- Val : T := Expr;
2140 -- is transformed into
2142 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
2144 -- Here are the main cases :
2146 -- <if Expr is a Slice>
2147 -- Val : T ([Index_Subtype (Expr)]) := Expr;
2149 -- <elsif Expr is a String Literal>
2150 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
2152 -- <elsif Expr is Constrained>
2153 -- subtype T is Type_Of_Expr
2154 -- Val : T := Expr;
2156 -- <elsif Expr is an entity_name>
2157 -- Val : T (constraints taken from Expr) := Expr;
2159 -- <else>
2160 -- type Axxx is access all T;
2161 -- Rval : Axxx := Expr'ref;
2162 -- Val : T (constraints taken from Rval) := Rval.all;
2164 -- ??? note: when the Expression is allocated in the secondary stack
2165 -- we could use it directly instead of copying it by declaring
2166 -- Val : T (...) renames Rval.all
2168 procedure Expand_Subtype_From_Expr
2169 (N : Node_Id;
2170 Unc_Type : Entity_Id;
2171 Subtype_Indic : Node_Id;
2172 Exp : Node_Id)
2174 Loc : constant Source_Ptr := Sloc (N);
2175 Exp_Typ : constant Entity_Id := Etype (Exp);
2176 T : Entity_Id;
2178 begin
2179 -- In general we cannot build the subtype if expansion is disabled,
2180 -- because internal entities may not have been defined. However, to
2181 -- avoid some cascaded errors, we try to continue when the expression is
2182 -- an array (or string), because it is safe to compute the bounds. It is
2183 -- in fact required to do so even in a generic context, because there
2184 -- may be constants that depend on the bounds of a string literal, both
2185 -- standard string types and more generally arrays of characters.
2187 -- In GNATprove mode, these extra subtypes are not needed
2189 if GNATprove_Mode then
2190 return;
2191 end if;
2193 if not Expander_Active
2194 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
2195 then
2196 return;
2197 end if;
2199 if Nkind (Exp) = N_Slice then
2200 declare
2201 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
2203 begin
2204 Rewrite (Subtype_Indic,
2205 Make_Subtype_Indication (Loc,
2206 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
2207 Constraint =>
2208 Make_Index_Or_Discriminant_Constraint (Loc,
2209 Constraints => New_List
2210 (New_Occurrence_Of (Slice_Type, Loc)))));
2212 -- This subtype indication may be used later for constraint checks
2213 -- we better make sure that if a variable was used as a bound of
2214 -- of the original slice, its value is frozen.
2216 Evaluate_Slice_Bounds (Exp);
2217 end;
2219 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2220 Rewrite (Subtype_Indic,
2221 Make_Subtype_Indication (Loc,
2222 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
2223 Constraint =>
2224 Make_Index_Or_Discriminant_Constraint (Loc,
2225 Constraints => New_List (
2226 Make_Literal_Range (Loc,
2227 Literal_Typ => Exp_Typ)))));
2229 -- If the type of the expression is an internally generated type it
2230 -- may not be necessary to create a new subtype. However there are two
2231 -- exceptions: references to the current instances, and aliased array
2232 -- object declarations for which the backend needs to create a template.
2234 elsif Is_Constrained (Exp_Typ)
2235 and then not Is_Class_Wide_Type (Unc_Type)
2236 and then
2237 (Nkind (N) /= N_Object_Declaration
2238 or else not Is_Entity_Name (Expression (N))
2239 or else not Comes_From_Source (Entity (Expression (N)))
2240 or else not Is_Array_Type (Exp_Typ)
2241 or else not Aliased_Present (N))
2242 then
2243 if Is_Itype (Exp_Typ) then
2245 -- Within an initialization procedure, a selected component
2246 -- denotes a component of the enclosing record, and it appears as
2247 -- an actual in a call to its own initialization procedure. If
2248 -- this component depends on the outer discriminant, we must
2249 -- generate the proper actual subtype for it.
2251 if Nkind (Exp) = N_Selected_Component
2252 and then Within_Init_Proc
2253 then
2254 declare
2255 Decl : constant Node_Id :=
2256 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2257 begin
2258 if Present (Decl) then
2259 Insert_Action (N, Decl);
2260 T := Defining_Identifier (Decl);
2261 else
2262 T := Exp_Typ;
2263 end if;
2264 end;
2266 -- No need to generate a new subtype
2268 else
2269 T := Exp_Typ;
2270 end if;
2272 else
2273 T := Make_Temporary (Loc, 'T');
2275 Insert_Action (N,
2276 Make_Subtype_Declaration (Loc,
2277 Defining_Identifier => T,
2278 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
2280 -- This type is marked as an itype even though it has an explicit
2281 -- declaration since otherwise Is_Generic_Actual_Type can get
2282 -- set, resulting in the generation of spurious errors. (See
2283 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
2285 Set_Is_Itype (T);
2286 Set_Associated_Node_For_Itype (T, Exp);
2287 end if;
2289 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
2291 -- Nothing needs to be done for private types with unknown discriminants
2292 -- if the underlying type is not an unconstrained composite type or it
2293 -- is an unchecked union.
2295 elsif Is_Private_Type (Unc_Type)
2296 and then Has_Unknown_Discriminants (Unc_Type)
2297 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
2298 or else Is_Constrained (Underlying_Type (Unc_Type))
2299 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
2300 then
2301 null;
2303 -- Case of derived type with unknown discriminants where the parent type
2304 -- also has unknown discriminants.
2306 elsif Is_Record_Type (Unc_Type)
2307 and then not Is_Class_Wide_Type (Unc_Type)
2308 and then Has_Unknown_Discriminants (Unc_Type)
2309 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2310 then
2311 -- Nothing to be done if no underlying record view available
2313 if No (Underlying_Record_View (Unc_Type)) then
2314 null;
2316 -- Otherwise use the Underlying_Record_View to create the proper
2317 -- constrained subtype for an object of a derived type with unknown
2318 -- discriminants.
2320 else
2321 Remove_Side_Effects (Exp);
2322 Rewrite (Subtype_Indic,
2323 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2324 end if;
2326 -- Renamings of class-wide interface types require no equivalent
2327 -- constrained type declarations because we only need to reference
2328 -- the tag component associated with the interface. The same is
2329 -- presumably true for class-wide types in general, so this test
2330 -- is broadened to include all class-wide renamings, which also
2331 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2332 -- (Is this really correct, or are there some cases of class-wide
2333 -- renamings that require action in this procedure???)
2335 elsif Present (N)
2336 and then Nkind (N) = N_Object_Renaming_Declaration
2337 and then Is_Class_Wide_Type (Unc_Type)
2338 then
2339 null;
2341 -- In Ada 95 nothing to be done if the type of the expression is limited
2342 -- because in this case the expression cannot be copied, and its use can
2343 -- only be by reference.
2345 -- In Ada 2005 the context can be an object declaration whose expression
2346 -- is a function that returns in place. If the nominal subtype has
2347 -- unknown discriminants, the call still provides constraints on the
2348 -- object, and we have to create an actual subtype from it.
2350 -- If the type is class-wide, the expression is dynamically tagged and
2351 -- we do not create an actual subtype either. Ditto for an interface.
2352 -- For now this applies only if the type is immutably limited, and the
2353 -- function being called is build-in-place. This will have to be revised
2354 -- when build-in-place functions are generalized to other types.
2356 elsif Is_Limited_View (Exp_Typ)
2357 and then
2358 (Is_Class_Wide_Type (Exp_Typ)
2359 or else Is_Interface (Exp_Typ)
2360 or else not Has_Unknown_Discriminants (Exp_Typ)
2361 or else not Is_Composite_Type (Unc_Type))
2362 then
2363 null;
2365 -- For limited objects initialized with build in place function calls,
2366 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2367 -- node in the expression initializing the object, which breaks the
2368 -- circuitry that detects and adds the additional arguments to the
2369 -- called function.
2371 elsif Is_Build_In_Place_Function_Call (Exp) then
2372 null;
2374 else
2375 Remove_Side_Effects (Exp);
2376 Rewrite (Subtype_Indic,
2377 Make_Subtype_From_Expr (Exp, Unc_Type));
2378 end if;
2379 end Expand_Subtype_From_Expr;
2381 ------------------------
2382 -- Find_Interface_ADT --
2383 ------------------------
2385 function Find_Interface_ADT
2386 (T : Entity_Id;
2387 Iface : Entity_Id) return Elmt_Id
2389 ADT : Elmt_Id;
2390 Typ : Entity_Id := T;
2392 begin
2393 pragma Assert (Is_Interface (Iface));
2395 -- Handle private types
2397 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2398 Typ := Full_View (Typ);
2399 end if;
2401 -- Handle access types
2403 if Is_Access_Type (Typ) then
2404 Typ := Designated_Type (Typ);
2405 end if;
2407 -- Handle task and protected types implementing interfaces
2409 if Is_Concurrent_Type (Typ) then
2410 Typ := Corresponding_Record_Type (Typ);
2411 end if;
2413 pragma Assert
2414 (not Is_Class_Wide_Type (Typ)
2415 and then Ekind (Typ) /= E_Incomplete_Type);
2417 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2418 return First_Elmt (Access_Disp_Table (Typ));
2420 else
2421 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
2422 while Present (ADT)
2423 and then Present (Related_Type (Node (ADT)))
2424 and then Related_Type (Node (ADT)) /= Iface
2425 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2426 Use_Full_View => True)
2427 loop
2428 Next_Elmt (ADT);
2429 end loop;
2431 pragma Assert (Present (Related_Type (Node (ADT))));
2432 return ADT;
2433 end if;
2434 end Find_Interface_ADT;
2436 ------------------------
2437 -- Find_Interface_Tag --
2438 ------------------------
2440 function Find_Interface_Tag
2441 (T : Entity_Id;
2442 Iface : Entity_Id) return Entity_Id
2444 AI_Tag : Entity_Id;
2445 Found : Boolean := False;
2446 Typ : Entity_Id := T;
2448 procedure Find_Tag (Typ : Entity_Id);
2449 -- Internal subprogram used to recursively climb to the ancestors
2451 --------------
2452 -- Find_Tag --
2453 --------------
2455 procedure Find_Tag (Typ : Entity_Id) is
2456 AI_Elmt : Elmt_Id;
2457 AI : Node_Id;
2459 begin
2460 -- This routine does not handle the case in which the interface is an
2461 -- ancestor of Typ. That case is handled by the enclosing subprogram.
2463 pragma Assert (Typ /= Iface);
2465 -- Climb to the root type handling private types
2467 if Present (Full_View (Etype (Typ))) then
2468 if Full_View (Etype (Typ)) /= Typ then
2469 Find_Tag (Full_View (Etype (Typ)));
2470 end if;
2472 elsif Etype (Typ) /= Typ then
2473 Find_Tag (Etype (Typ));
2474 end if;
2476 -- Traverse the list of interfaces implemented by the type
2478 if not Found
2479 and then Present (Interfaces (Typ))
2480 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
2481 then
2482 -- Skip the tag associated with the primary table
2484 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2485 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2486 pragma Assert (Present (AI_Tag));
2488 AI_Elmt := First_Elmt (Interfaces (Typ));
2489 while Present (AI_Elmt) loop
2490 AI := Node (AI_Elmt);
2492 if AI = Iface
2493 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2494 then
2495 Found := True;
2496 return;
2497 end if;
2499 AI_Tag := Next_Tag_Component (AI_Tag);
2500 Next_Elmt (AI_Elmt);
2501 end loop;
2502 end if;
2503 end Find_Tag;
2505 -- Start of processing for Find_Interface_Tag
2507 begin
2508 pragma Assert (Is_Interface (Iface));
2510 -- Handle access types
2512 if Is_Access_Type (Typ) then
2513 Typ := Designated_Type (Typ);
2514 end if;
2516 -- Handle class-wide types
2518 if Is_Class_Wide_Type (Typ) then
2519 Typ := Root_Type (Typ);
2520 end if;
2522 -- Handle private types
2524 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2525 Typ := Full_View (Typ);
2526 end if;
2528 -- Handle entities from the limited view
2530 if Ekind (Typ) = E_Incomplete_Type then
2531 pragma Assert (Present (Non_Limited_View (Typ)));
2532 Typ := Non_Limited_View (Typ);
2533 end if;
2535 -- Handle task and protected types implementing interfaces
2537 if Is_Concurrent_Type (Typ) then
2538 Typ := Corresponding_Record_Type (Typ);
2539 end if;
2541 -- If the interface is an ancestor of the type, then it shared the
2542 -- primary dispatch table.
2544 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2545 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2546 return First_Tag_Component (Typ);
2548 -- Otherwise we need to search for its associated tag component
2550 else
2551 Find_Tag (Typ);
2552 pragma Assert (Found);
2553 return AI_Tag;
2554 end if;
2555 end Find_Interface_Tag;
2557 ------------------
2558 -- Find_Prim_Op --
2559 ------------------
2561 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
2562 Prim : Elmt_Id;
2563 Typ : Entity_Id := T;
2564 Op : Entity_Id;
2566 begin
2567 if Is_Class_Wide_Type (Typ) then
2568 Typ := Root_Type (Typ);
2569 end if;
2571 Typ := Underlying_Type (Typ);
2573 -- Loop through primitive operations
2575 Prim := First_Elmt (Primitive_Operations (Typ));
2576 while Present (Prim) loop
2577 Op := Node (Prim);
2579 -- We can retrieve primitive operations by name if it is an internal
2580 -- name. For equality we must check that both of its operands have
2581 -- the same type, to avoid confusion with user-defined equalities
2582 -- than may have a non-symmetric signature.
2584 exit when Chars (Op) = Name
2585 and then
2586 (Name /= Name_Op_Eq
2587 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
2589 Next_Elmt (Prim);
2591 -- Raise Program_Error if no primitive found
2593 if No (Prim) then
2594 raise Program_Error;
2595 end if;
2596 end loop;
2598 return Node (Prim);
2599 end Find_Prim_Op;
2601 ------------------
2602 -- Find_Prim_Op --
2603 ------------------
2605 function Find_Prim_Op
2606 (T : Entity_Id;
2607 Name : TSS_Name_Type) return Entity_Id
2609 Inher_Op : Entity_Id := Empty;
2610 Own_Op : Entity_Id := Empty;
2611 Prim_Elmt : Elmt_Id;
2612 Prim_Id : Entity_Id;
2613 Typ : Entity_Id := T;
2615 begin
2616 if Is_Class_Wide_Type (Typ) then
2617 Typ := Root_Type (Typ);
2618 end if;
2620 Typ := Underlying_Type (Typ);
2622 -- This search is based on the assertion that the dispatching version
2623 -- of the TSS routine always precedes the real primitive.
2625 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2626 while Present (Prim_Elmt) loop
2627 Prim_Id := Node (Prim_Elmt);
2629 if Is_TSS (Prim_Id, Name) then
2630 if Present (Alias (Prim_Id)) then
2631 Inher_Op := Prim_Id;
2632 else
2633 Own_Op := Prim_Id;
2634 end if;
2635 end if;
2637 Next_Elmt (Prim_Elmt);
2638 end loop;
2640 if Present (Own_Op) then
2641 return Own_Op;
2642 elsif Present (Inher_Op) then
2643 return Inher_Op;
2644 else
2645 raise Program_Error;
2646 end if;
2647 end Find_Prim_Op;
2649 ----------------------------
2650 -- Find_Protection_Object --
2651 ----------------------------
2653 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2654 S : Entity_Id;
2656 begin
2657 S := Scop;
2658 while Present (S) loop
2659 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
2660 and then Present (Protection_Object (S))
2661 then
2662 return Protection_Object (S);
2663 end if;
2665 S := Scope (S);
2666 end loop;
2668 -- If we do not find a Protection object in the scope chain, then
2669 -- something has gone wrong, most likely the object was never created.
2671 raise Program_Error;
2672 end Find_Protection_Object;
2674 --------------------------
2675 -- Find_Protection_Type --
2676 --------------------------
2678 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2679 Comp : Entity_Id;
2680 Typ : Entity_Id := Conc_Typ;
2682 begin
2683 if Is_Concurrent_Type (Typ) then
2684 Typ := Corresponding_Record_Type (Typ);
2685 end if;
2687 -- Since restriction violations are not considered serious errors, the
2688 -- expander remains active, but may leave the corresponding record type
2689 -- malformed. In such cases, component _object is not available so do
2690 -- not look for it.
2692 if not Analyzed (Typ) then
2693 return Empty;
2694 end if;
2696 Comp := First_Component (Typ);
2697 while Present (Comp) loop
2698 if Chars (Comp) = Name_uObject then
2699 return Base_Type (Etype (Comp));
2700 end if;
2702 Next_Component (Comp);
2703 end loop;
2705 -- The corresponding record of a protected type should always have an
2706 -- _object field.
2708 raise Program_Error;
2709 end Find_Protection_Type;
2711 -----------------------
2712 -- Find_Hook_Context --
2713 -----------------------
2715 function Find_Hook_Context (N : Node_Id) return Node_Id is
2716 Par : Node_Id;
2717 Top : Node_Id;
2719 Wrapped_Node : Node_Id;
2720 -- Note: if we are in a transient scope, we want to reuse it as
2721 -- the context for actions insertion, if possible. But if N is itself
2722 -- part of the stored actions for the current transient scope,
2723 -- then we need to insert at the appropriate (inner) location in
2724 -- the not as an action on Node_To_Be_Wrapped.
2726 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
2728 begin
2729 -- When the node is inside a case/if expression, the lifetime of any
2730 -- temporary controlled object is extended. Find a suitable insertion
2731 -- node by locating the topmost case or if expressions.
2733 if In_Cond_Expr then
2734 Par := N;
2735 Top := N;
2736 while Present (Par) loop
2737 if Nkind_In (Original_Node (Par), N_Case_Expression,
2738 N_If_Expression)
2739 then
2740 Top := Par;
2742 -- Prevent the search from going too far
2744 elsif Is_Body_Or_Package_Declaration (Par) then
2745 exit;
2746 end if;
2748 Par := Parent (Par);
2749 end loop;
2751 -- The topmost case or if expression is now recovered, but it may
2752 -- still not be the correct place to add generated code. Climb to
2753 -- find a parent that is part of a declarative or statement list,
2754 -- and is not a list of actuals in a call.
2756 Par := Top;
2757 while Present (Par) loop
2758 if Is_List_Member (Par)
2759 and then not Nkind_In (Par, N_Component_Association,
2760 N_Discriminant_Association,
2761 N_Parameter_Association,
2762 N_Pragma_Argument_Association)
2763 and then not Nkind_In
2764 (Parent (Par), N_Function_Call,
2765 N_Procedure_Call_Statement,
2766 N_Entry_Call_Statement)
2768 then
2769 return Par;
2771 -- Prevent the search from going too far
2773 elsif Is_Body_Or_Package_Declaration (Par) then
2774 exit;
2775 end if;
2777 Par := Parent (Par);
2778 end loop;
2780 return Par;
2782 else
2783 Par := N;
2784 while Present (Par) loop
2786 -- Keep climbing past various operators
2788 if Nkind (Parent (Par)) in N_Op
2789 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
2790 then
2791 Par := Parent (Par);
2792 else
2793 exit;
2794 end if;
2795 end loop;
2797 Top := Par;
2799 -- The node may be located in a pragma in which case return the
2800 -- pragma itself:
2802 -- pragma Precondition (... and then Ctrl_Func_Call ...);
2804 -- Similar case occurs when the node is related to an object
2805 -- declaration or assignment:
2807 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
2809 -- Another case to consider is when the node is part of a return
2810 -- statement:
2812 -- return ... and then Ctrl_Func_Call ...;
2814 -- Another case is when the node acts as a formal in a procedure
2815 -- call statement:
2817 -- Proc (... and then Ctrl_Func_Call ...);
2819 if Scope_Is_Transient then
2820 Wrapped_Node := Node_To_Be_Wrapped;
2821 else
2822 Wrapped_Node := Empty;
2823 end if;
2825 while Present (Par) loop
2826 if Par = Wrapped_Node
2827 or else Nkind_In (Par, N_Assignment_Statement,
2828 N_Object_Declaration,
2829 N_Pragma,
2830 N_Procedure_Call_Statement,
2831 N_Simple_Return_Statement)
2832 then
2833 return Par;
2835 -- Prevent the search from going too far
2837 elsif Is_Body_Or_Package_Declaration (Par) then
2838 exit;
2839 end if;
2841 Par := Parent (Par);
2842 end loop;
2844 -- Return the topmost short circuit operator
2846 return Top;
2847 end if;
2848 end Find_Hook_Context;
2850 ----------------------
2851 -- Force_Evaluation --
2852 ----------------------
2854 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
2855 begin
2856 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
2857 end Force_Evaluation;
2859 ---------------------------------
2860 -- Fully_Qualified_Name_String --
2861 ---------------------------------
2863 function Fully_Qualified_Name_String
2864 (E : Entity_Id;
2865 Append_NUL : Boolean := True) return String_Id
2867 procedure Internal_Full_Qualified_Name (E : Entity_Id);
2868 -- Compute recursively the qualified name without NUL at the end, adding
2869 -- it to the currently started string being generated
2871 ----------------------------------
2872 -- Internal_Full_Qualified_Name --
2873 ----------------------------------
2875 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
2876 Ent : Entity_Id;
2878 begin
2879 -- Deal properly with child units
2881 if Nkind (E) = N_Defining_Program_Unit_Name then
2882 Ent := Defining_Identifier (E);
2883 else
2884 Ent := E;
2885 end if;
2887 -- Compute qualification recursively (only "Standard" has no scope)
2889 if Present (Scope (Scope (Ent))) then
2890 Internal_Full_Qualified_Name (Scope (Ent));
2891 Store_String_Char (Get_Char_Code ('.'));
2892 end if;
2894 -- Every entity should have a name except some expanded blocks
2895 -- don't bother about those.
2897 if Chars (Ent) = No_Name then
2898 return;
2899 end if;
2901 -- Generates the entity name in upper case
2903 Get_Decoded_Name_String (Chars (Ent));
2904 Set_All_Upper_Case;
2905 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2906 return;
2907 end Internal_Full_Qualified_Name;
2909 -- Start of processing for Full_Qualified_Name
2911 begin
2912 Start_String;
2913 Internal_Full_Qualified_Name (E);
2915 if Append_NUL then
2916 Store_String_Char (Get_Char_Code (ASCII.NUL));
2917 end if;
2919 return End_String;
2920 end Fully_Qualified_Name_String;
2922 ------------------------
2923 -- Generate_Poll_Call --
2924 ------------------------
2926 procedure Generate_Poll_Call (N : Node_Id) is
2927 begin
2928 -- No poll call if polling not active
2930 if not Polling_Required then
2931 return;
2933 -- Otherwise generate require poll call
2935 else
2936 Insert_Before_And_Analyze (N,
2937 Make_Procedure_Call_Statement (Sloc (N),
2938 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
2939 end if;
2940 end Generate_Poll_Call;
2942 ---------------------------------
2943 -- Get_Current_Value_Condition --
2944 ---------------------------------
2946 -- Note: the implementation of this procedure is very closely tied to the
2947 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
2948 -- interpret Current_Value fields set by the Set procedure, so the two
2949 -- procedures need to be closely coordinated.
2951 procedure Get_Current_Value_Condition
2952 (Var : Node_Id;
2953 Op : out Node_Kind;
2954 Val : out Node_Id)
2956 Loc : constant Source_Ptr := Sloc (Var);
2957 Ent : constant Entity_Id := Entity (Var);
2959 procedure Process_Current_Value_Condition
2960 (N : Node_Id;
2961 S : Boolean);
2962 -- N is an expression which holds either True (S = True) or False (S =
2963 -- False) in the condition. This procedure digs out the expression and
2964 -- if it refers to Ent, sets Op and Val appropriately.
2966 -------------------------------------
2967 -- Process_Current_Value_Condition --
2968 -------------------------------------
2970 procedure Process_Current_Value_Condition
2971 (N : Node_Id;
2972 S : Boolean)
2974 Cond : Node_Id;
2975 Prev_Cond : Node_Id;
2976 Sens : Boolean;
2978 begin
2979 Cond := N;
2980 Sens := S;
2982 loop
2983 Prev_Cond := Cond;
2985 -- Deal with NOT operators, inverting sense
2987 while Nkind (Cond) = N_Op_Not loop
2988 Cond := Right_Opnd (Cond);
2989 Sens := not Sens;
2990 end loop;
2992 -- Deal with conversions, qualifications, and expressions with
2993 -- actions.
2995 while Nkind_In (Cond,
2996 N_Type_Conversion,
2997 N_Qualified_Expression,
2998 N_Expression_With_Actions)
2999 loop
3000 Cond := Expression (Cond);
3001 end loop;
3003 exit when Cond = Prev_Cond;
3004 end loop;
3006 -- Deal with AND THEN and AND cases
3008 if Nkind_In (Cond, N_And_Then, N_Op_And) then
3010 -- Don't ever try to invert a condition that is of the form of an
3011 -- AND or AND THEN (since we are not doing sufficiently general
3012 -- processing to allow this).
3014 if Sens = False then
3015 Op := N_Empty;
3016 Val := Empty;
3017 return;
3018 end if;
3020 -- Recursively process AND and AND THEN branches
3022 Process_Current_Value_Condition (Left_Opnd (Cond), True);
3024 if Op /= N_Empty then
3025 return;
3026 end if;
3028 Process_Current_Value_Condition (Right_Opnd (Cond), True);
3029 return;
3031 -- Case of relational operator
3033 elsif Nkind (Cond) in N_Op_Compare then
3034 Op := Nkind (Cond);
3036 -- Invert sense of test if inverted test
3038 if Sens = False then
3039 case Op is
3040 when N_Op_Eq => Op := N_Op_Ne;
3041 when N_Op_Ne => Op := N_Op_Eq;
3042 when N_Op_Lt => Op := N_Op_Ge;
3043 when N_Op_Gt => Op := N_Op_Le;
3044 when N_Op_Le => Op := N_Op_Gt;
3045 when N_Op_Ge => Op := N_Op_Lt;
3046 when others => raise Program_Error;
3047 end case;
3048 end if;
3050 -- Case of entity op value
3052 if Is_Entity_Name (Left_Opnd (Cond))
3053 and then Ent = Entity (Left_Opnd (Cond))
3054 and then Compile_Time_Known_Value (Right_Opnd (Cond))
3055 then
3056 Val := Right_Opnd (Cond);
3058 -- Case of value op entity
3060 elsif Is_Entity_Name (Right_Opnd (Cond))
3061 and then Ent = Entity (Right_Opnd (Cond))
3062 and then Compile_Time_Known_Value (Left_Opnd (Cond))
3063 then
3064 Val := Left_Opnd (Cond);
3066 -- We are effectively swapping operands
3068 case Op is
3069 when N_Op_Eq => null;
3070 when N_Op_Ne => null;
3071 when N_Op_Lt => Op := N_Op_Gt;
3072 when N_Op_Gt => Op := N_Op_Lt;
3073 when N_Op_Le => Op := N_Op_Ge;
3074 when N_Op_Ge => Op := N_Op_Le;
3075 when others => raise Program_Error;
3076 end case;
3078 else
3079 Op := N_Empty;
3080 end if;
3082 return;
3084 elsif Nkind_In (Cond,
3085 N_Type_Conversion,
3086 N_Qualified_Expression,
3087 N_Expression_With_Actions)
3088 then
3089 Cond := Expression (Cond);
3091 -- Case of Boolean variable reference, return as though the
3092 -- reference had said var = True.
3094 else
3095 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
3096 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
3098 if Sens = False then
3099 Op := N_Op_Ne;
3100 else
3101 Op := N_Op_Eq;
3102 end if;
3103 end if;
3104 end if;
3105 end Process_Current_Value_Condition;
3107 -- Start of processing for Get_Current_Value_Condition
3109 begin
3110 Op := N_Empty;
3111 Val := Empty;
3113 -- Immediate return, nothing doing, if this is not an object
3115 if Ekind (Ent) not in Object_Kind then
3116 return;
3117 end if;
3119 -- Otherwise examine current value
3121 declare
3122 CV : constant Node_Id := Current_Value (Ent);
3123 Sens : Boolean;
3124 Stm : Node_Id;
3126 begin
3127 -- If statement. Condition is known true in THEN section, known False
3128 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
3130 if Nkind (CV) = N_If_Statement then
3132 -- Before start of IF statement
3134 if Loc < Sloc (CV) then
3135 return;
3137 -- After end of IF statement
3139 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
3140 return;
3141 end if;
3143 -- At this stage we know that we are within the IF statement, but
3144 -- unfortunately, the tree does not record the SLOC of the ELSE so
3145 -- we cannot use a simple SLOC comparison to distinguish between
3146 -- the then/else statements, so we have to climb the tree.
3148 declare
3149 N : Node_Id;
3151 begin
3152 N := Parent (Var);
3153 while Parent (N) /= CV loop
3154 N := Parent (N);
3156 -- If we fall off the top of the tree, then that's odd, but
3157 -- perhaps it could occur in some error situation, and the
3158 -- safest response is simply to assume that the outcome of
3159 -- the condition is unknown. No point in bombing during an
3160 -- attempt to optimize things.
3162 if No (N) then
3163 return;
3164 end if;
3165 end loop;
3167 -- Now we have N pointing to a node whose parent is the IF
3168 -- statement in question, so now we can tell if we are within
3169 -- the THEN statements.
3171 if Is_List_Member (N)
3172 and then List_Containing (N) = Then_Statements (CV)
3173 then
3174 Sens := True;
3176 -- If the variable reference does not come from source, we
3177 -- cannot reliably tell whether it appears in the else part.
3178 -- In particular, if it appears in generated code for a node
3179 -- that requires finalization, it may be attached to a list
3180 -- that has not been yet inserted into the code. For now,
3181 -- treat it as unknown.
3183 elsif not Comes_From_Source (N) then
3184 return;
3186 -- Otherwise we must be in ELSIF or ELSE part
3188 else
3189 Sens := False;
3190 end if;
3191 end;
3193 -- ELSIF part. Condition is known true within the referenced
3194 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
3195 -- and unknown before the ELSE part or after the IF statement.
3197 elsif Nkind (CV) = N_Elsif_Part then
3199 -- if the Elsif_Part had condition_actions, the elsif has been
3200 -- rewritten as a nested if, and the original elsif_part is
3201 -- detached from the tree, so there is no way to obtain useful
3202 -- information on the current value of the variable.
3203 -- Can this be improved ???
3205 if No (Parent (CV)) then
3206 return;
3207 end if;
3209 Stm := Parent (CV);
3211 -- Before start of ELSIF part
3213 if Loc < Sloc (CV) then
3214 return;
3216 -- After end of IF statement
3218 elsif Loc >= Sloc (Stm) +
3219 Text_Ptr (UI_To_Int (End_Span (Stm)))
3220 then
3221 return;
3222 end if;
3224 -- Again we lack the SLOC of the ELSE, so we need to climb the
3225 -- tree to see if we are within the ELSIF part in question.
3227 declare
3228 N : Node_Id;
3230 begin
3231 N := Parent (Var);
3232 while Parent (N) /= Stm loop
3233 N := Parent (N);
3235 -- If we fall off the top of the tree, then that's odd, but
3236 -- perhaps it could occur in some error situation, and the
3237 -- safest response is simply to assume that the outcome of
3238 -- the condition is unknown. No point in bombing during an
3239 -- attempt to optimize things.
3241 if No (N) then
3242 return;
3243 end if;
3244 end loop;
3246 -- Now we have N pointing to a node whose parent is the IF
3247 -- statement in question, so see if is the ELSIF part we want.
3248 -- the THEN statements.
3250 if N = CV then
3251 Sens := True;
3253 -- Otherwise we must be in subsequent ELSIF or ELSE part
3255 else
3256 Sens := False;
3257 end if;
3258 end;
3260 -- Iteration scheme of while loop. The condition is known to be
3261 -- true within the body of the loop.
3263 elsif Nkind (CV) = N_Iteration_Scheme then
3264 declare
3265 Loop_Stmt : constant Node_Id := Parent (CV);
3267 begin
3268 -- Before start of body of loop
3270 if Loc < Sloc (Loop_Stmt) then
3271 return;
3273 -- After end of LOOP statement
3275 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
3276 return;
3278 -- We are within the body of the loop
3280 else
3281 Sens := True;
3282 end if;
3283 end;
3285 -- All other cases of Current_Value settings
3287 else
3288 return;
3289 end if;
3291 -- If we fall through here, then we have a reportable condition, Sens
3292 -- is True if the condition is true and False if it needs inverting.
3294 Process_Current_Value_Condition (Condition (CV), Sens);
3295 end;
3296 end Get_Current_Value_Condition;
3298 -------------------------------------------------
3299 -- Get_First_Parent_With_Ext_Axioms_For_Entity --
3300 -------------------------------------------------
3302 function Get_First_Parent_With_Ext_Axioms_For_Entity
3303 (E : Entity_Id) return Entity_Id
3305 Decl : Node_Id;
3307 begin
3308 if Ekind (E) = E_Package then
3309 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
3310 Decl := Parent (Parent (E));
3311 else
3312 Decl := Parent (E);
3313 end if;
3314 end if;
3316 -- E is the package or generic package which is externally axiomatized
3318 if Ekind_In (E, E_Package, E_Generic_Package)
3319 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
3320 then
3321 return E;
3322 end if;
3324 -- If E's scope is axiomatized, E is axiomatized.
3326 declare
3327 First_Ax_Parent_Scope : Entity_Id := Empty;
3329 begin
3330 if Present (Scope (E)) then
3331 First_Ax_Parent_Scope :=
3332 Get_First_Parent_With_Ext_Axioms_For_Entity (Scope (E));
3333 end if;
3335 if Present (First_Ax_Parent_Scope) then
3336 return First_Ax_Parent_Scope;
3337 end if;
3339 -- otherwise, if E is a package instance, it is axiomatized if the
3340 -- corresponding generic package is axiomatized.
3342 if Ekind (E) = E_Package
3343 and then Present (Generic_Parent (Decl))
3344 then
3345 return
3346 Get_First_Parent_With_Ext_Axioms_For_Entity
3347 (Generic_Parent (Decl));
3348 else
3349 return Empty;
3350 end if;
3351 end;
3352 end Get_First_Parent_With_Ext_Axioms_For_Entity;
3354 ---------------------
3355 -- Get_Stream_Size --
3356 ---------------------
3358 function Get_Stream_Size (E : Entity_Id) return Uint is
3359 begin
3360 -- If we have a Stream_Size clause for this type use it
3362 if Has_Stream_Size_Clause (E) then
3363 return Static_Integer (Expression (Stream_Size_Clause (E)));
3365 -- Otherwise the Stream_Size if the size of the type
3367 else
3368 return Esize (E);
3369 end if;
3370 end Get_Stream_Size;
3372 ---------------------------
3373 -- Has_Access_Constraint --
3374 ---------------------------
3376 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3377 Disc : Entity_Id;
3378 T : constant Entity_Id := Etype (E);
3380 begin
3381 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
3382 Disc := First_Discriminant (T);
3383 while Present (Disc) loop
3384 if Is_Access_Type (Etype (Disc)) then
3385 return True;
3386 end if;
3388 Next_Discriminant (Disc);
3389 end loop;
3391 return False;
3392 else
3393 return False;
3394 end if;
3395 end Has_Access_Constraint;
3397 -----------------------------------------------------
3398 -- Has_Annotate_Pragma_For_External_Axiomatization --
3399 -----------------------------------------------------
3401 function Has_Annotate_Pragma_For_External_Axiomatization
3402 (E : Entity_Id) return Boolean
3404 function Is_Annotate_Pragma_For_External_Axiomatization
3405 (N : Node_Id) return Boolean;
3406 -- Returns whether N is
3407 -- pragma Annotate (GNATprove, External_Axiomatization);
3409 ----------------------------------------------------
3410 -- Is_Annotate_Pragma_For_External_Axiomatization --
3411 ----------------------------------------------------
3413 -- The general form of pragma Annotate is
3415 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
3416 -- ARG ::= NAME | EXPRESSION
3418 -- The first two arguments are by convention intended to refer to an
3419 -- external tool and a tool-specific function. These arguments are
3420 -- not analyzed.
3422 -- The following is used to annotate a package specification which
3423 -- GNATprove should treat specially, because the axiomatization of
3424 -- this unit is given by the user instead of being automatically
3425 -- generated.
3427 -- pragma Annotate (GNATprove, External_Axiomatization);
3429 function Is_Annotate_Pragma_For_External_Axiomatization
3430 (N : Node_Id) return Boolean
3432 Name_GNATprove : constant String :=
3433 "gnatprove";
3434 Name_External_Axiomatization : constant String :=
3435 "external_axiomatization";
3436 -- Special names
3438 begin
3439 if Nkind (N) = N_Pragma
3440 and then Get_Pragma_Id (Pragma_Name (N)) = Pragma_Annotate
3441 and then List_Length (Pragma_Argument_Associations (N)) = 2
3442 then
3443 declare
3444 Arg1 : constant Node_Id :=
3445 First (Pragma_Argument_Associations (N));
3446 Arg2 : constant Node_Id := Next (Arg1);
3447 Nam1 : Name_Id;
3448 Nam2 : Name_Id;
3450 begin
3451 -- Fill in Name_Buffer with Name_GNATprove first, and then with
3452 -- Name_External_Axiomatization so that Name_Find returns the
3453 -- corresponding name. This takes care of all possible casings.
3455 Name_Len := 0;
3456 Add_Str_To_Name_Buffer (Name_GNATprove);
3457 Nam1 := Name_Find;
3459 Name_Len := 0;
3460 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
3461 Nam2 := Name_Find;
3463 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
3464 and then
3465 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
3466 end;
3468 else
3469 return False;
3470 end if;
3471 end Is_Annotate_Pragma_For_External_Axiomatization;
3473 -- Local variables
3475 Decl : Node_Id;
3476 Vis_Decls : List_Id;
3477 N : Node_Id;
3479 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
3481 begin
3482 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
3483 Decl := Parent (Parent (E));
3484 else
3485 Decl := Parent (E);
3486 end if;
3488 Vis_Decls := Visible_Declarations (Decl);
3490 N := First (Vis_Decls);
3491 while Present (N) loop
3493 -- Skip declarations generated by the frontend. Skip all pragmas
3494 -- that are not the desired Annotate pragma. Stop the search on
3495 -- the first non-pragma source declaration.
3497 if Comes_From_Source (N) then
3498 if Nkind (N) = N_Pragma then
3499 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
3500 return True;
3501 end if;
3502 else
3503 return False;
3504 end if;
3505 end if;
3507 Next (N);
3508 end loop;
3510 return False;
3511 end Has_Annotate_Pragma_For_External_Axiomatization;
3513 ----------------------------------
3514 -- Has_Following_Address_Clause --
3515 ----------------------------------
3517 -- Should this function check the private part in a package ???
3519 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
3520 Id : constant Entity_Id := Defining_Identifier (D);
3521 Decl : Node_Id;
3523 begin
3524 Decl := Next (D);
3525 while Present (Decl) loop
3526 if Nkind (Decl) = N_At_Clause
3527 and then Chars (Identifier (Decl)) = Chars (Id)
3528 then
3529 return True;
3531 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3532 and then Chars (Decl) = Name_Address
3533 and then Chars (Name (Decl)) = Chars (Id)
3534 then
3535 return True;
3536 end if;
3538 Next (Decl);
3539 end loop;
3541 return False;
3542 end Has_Following_Address_Clause;
3544 --------------------
3545 -- Homonym_Number --
3546 --------------------
3548 function Homonym_Number (Subp : Entity_Id) return Nat is
3549 Count : Nat;
3550 Hom : Entity_Id;
3552 begin
3553 Count := 1;
3554 Hom := Homonym (Subp);
3555 while Present (Hom) loop
3556 if Scope (Hom) = Scope (Subp) then
3557 Count := Count + 1;
3558 end if;
3560 Hom := Homonym (Hom);
3561 end loop;
3563 return Count;
3564 end Homonym_Number;
3566 -----------------------------------
3567 -- In_Library_Level_Package_Body --
3568 -----------------------------------
3570 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3571 begin
3572 -- First determine whether the entity appears at the library level, then
3573 -- look at the containing unit.
3575 if Is_Library_Level_Entity (Id) then
3576 declare
3577 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3579 begin
3580 return Nkind (Unit (Container)) = N_Package_Body;
3581 end;
3582 end if;
3584 return False;
3585 end In_Library_Level_Package_Body;
3587 ------------------------------
3588 -- In_Unconditional_Context --
3589 ------------------------------
3591 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3592 P : Node_Id;
3594 begin
3595 P := Node;
3596 while Present (P) loop
3597 case Nkind (P) is
3598 when N_Subprogram_Body =>
3599 return True;
3601 when N_If_Statement =>
3602 return False;
3604 when N_Loop_Statement =>
3605 return False;
3607 when N_Case_Statement =>
3608 return False;
3610 when others =>
3611 P := Parent (P);
3612 end case;
3613 end loop;
3615 return False;
3616 end In_Unconditional_Context;
3618 -------------------
3619 -- Insert_Action --
3620 -------------------
3622 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3623 begin
3624 if Present (Ins_Action) then
3625 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3626 end if;
3627 end Insert_Action;
3629 -- Version with check(s) suppressed
3631 procedure Insert_Action
3632 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3634 begin
3635 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3636 end Insert_Action;
3638 -------------------------
3639 -- Insert_Action_After --
3640 -------------------------
3642 procedure Insert_Action_After
3643 (Assoc_Node : Node_Id;
3644 Ins_Action : Node_Id)
3646 begin
3647 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3648 end Insert_Action_After;
3650 --------------------
3651 -- Insert_Actions --
3652 --------------------
3654 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3655 N : Node_Id;
3656 P : Node_Id;
3658 Wrapped_Node : Node_Id := Empty;
3660 begin
3661 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3662 return;
3663 end if;
3665 -- Ignore insert of actions from inside default expression (or other
3666 -- similar "spec expression") in the special spec-expression analyze
3667 -- mode. Any insertions at this point have no relevance, since we are
3668 -- only doing the analyze to freeze the types of any static expressions.
3669 -- See section "Handling of Default Expressions" in the spec of package
3670 -- Sem for further details.
3672 if In_Spec_Expression then
3673 return;
3674 end if;
3676 -- If the action derives from stuff inside a record, then the actions
3677 -- are attached to the current scope, to be inserted and analyzed on
3678 -- exit from the scope. The reason for this is that we may also be
3679 -- generating freeze actions at the same time, and they must eventually
3680 -- be elaborated in the correct order.
3682 if Is_Record_Type (Current_Scope)
3683 and then not Is_Frozen (Current_Scope)
3684 then
3685 if No (Scope_Stack.Table
3686 (Scope_Stack.Last).Pending_Freeze_Actions)
3687 then
3688 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3689 Ins_Actions;
3690 else
3691 Append_List
3692 (Ins_Actions,
3693 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3694 end if;
3696 return;
3697 end if;
3699 -- We now intend to climb up the tree to find the right point to
3700 -- insert the actions. We start at Assoc_Node, unless this node is a
3701 -- subexpression in which case we start with its parent. We do this for
3702 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3703 -- itself one of the special nodes like N_And_Then, then we assume that
3704 -- an initial request to insert actions for such a node does not expect
3705 -- the actions to get deposited in the node for later handling when the
3706 -- node is expanded, since clearly the node is being dealt with by the
3707 -- caller. Note that in the subexpression case, N is always the child we
3708 -- came from.
3710 -- N_Raise_xxx_Error is an annoying special case, it is a statement if
3711 -- it has type Standard_Void_Type, and a subexpression otherwise.
3712 -- otherwise. Procedure calls, and similarly procedure attribute
3713 -- references, are also statements.
3715 if Nkind (Assoc_Node) in N_Subexpr
3716 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
3717 or else Etype (Assoc_Node) /= Standard_Void_Type)
3718 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
3719 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
3720 or else not Is_Procedure_Attribute_Name
3721 (Attribute_Name (Assoc_Node)))
3722 then
3723 N := Assoc_Node;
3724 P := Parent (Assoc_Node);
3726 -- Non-subexpression case. Note that N is initially Empty in this case
3727 -- (N is only guaranteed Non-Empty in the subexpr case).
3729 else
3730 N := Empty;
3731 P := Assoc_Node;
3732 end if;
3734 -- Capture root of the transient scope
3736 if Scope_Is_Transient then
3737 Wrapped_Node := Node_To_Be_Wrapped;
3738 end if;
3740 loop
3741 pragma Assert (Present (P));
3743 -- Make sure that inserted actions stay in the transient scope
3745 if Present (Wrapped_Node) and then N = Wrapped_Node then
3746 Store_Before_Actions_In_Scope (Ins_Actions);
3747 return;
3748 end if;
3750 case Nkind (P) is
3752 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3753 -- in the Actions field of the right operand. They will be moved
3754 -- out further when the AND THEN or OR ELSE operator is expanded.
3755 -- Nothing special needs to be done for the left operand since
3756 -- in that case the actions are executed unconditionally.
3758 when N_Short_Circuit =>
3759 if N = Right_Opnd (P) then
3761 -- We are now going to either append the actions to the
3762 -- actions field of the short-circuit operation. We will
3763 -- also analyze the actions now.
3765 -- This analysis is really too early, the proper thing would
3766 -- be to just park them there now, and only analyze them if
3767 -- we find we really need them, and to it at the proper
3768 -- final insertion point. However attempting to this proved
3769 -- tricky, so for now we just kill current values before and
3770 -- after the analyze call to make sure we avoid peculiar
3771 -- optimizations from this out of order insertion.
3773 Kill_Current_Values;
3775 -- If P has already been expanded, we can't park new actions
3776 -- on it, so we need to expand them immediately, introducing
3777 -- an Expression_With_Actions. N can't be an expression
3778 -- with actions, or else then the actions would have been
3779 -- inserted at an inner level.
3781 if Analyzed (P) then
3782 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
3783 Rewrite (N,
3784 Make_Expression_With_Actions (Sloc (N),
3785 Actions => Ins_Actions,
3786 Expression => Relocate_Node (N)));
3787 Analyze_And_Resolve (N);
3789 elsif Present (Actions (P)) then
3790 Insert_List_After_And_Analyze
3791 (Last (Actions (P)), Ins_Actions);
3792 else
3793 Set_Actions (P, Ins_Actions);
3794 Analyze_List (Actions (P));
3795 end if;
3797 Kill_Current_Values;
3799 return;
3800 end if;
3802 -- Then or Else dependent expression of an if expression. Add
3803 -- actions to Then_Actions or Else_Actions field as appropriate.
3804 -- The actions will be moved further out when the if is expanded.
3806 when N_If_Expression =>
3807 declare
3808 ThenX : constant Node_Id := Next (First (Expressions (P)));
3809 ElseX : constant Node_Id := Next (ThenX);
3811 begin
3812 -- If the enclosing expression is already analyzed, as
3813 -- is the case for nested elaboration checks, insert the
3814 -- conditional further out.
3816 if Analyzed (P) then
3817 null;
3819 -- Actions belong to the then expression, temporarily place
3820 -- them as Then_Actions of the if expression. They will be
3821 -- moved to the proper place later when the if expression
3822 -- is expanded.
3824 elsif N = ThenX then
3825 if Present (Then_Actions (P)) then
3826 Insert_List_After_And_Analyze
3827 (Last (Then_Actions (P)), Ins_Actions);
3828 else
3829 Set_Then_Actions (P, Ins_Actions);
3830 Analyze_List (Then_Actions (P));
3831 end if;
3833 return;
3835 -- Actions belong to the else expression, temporarily place
3836 -- them as Else_Actions of the if expression. They will be
3837 -- moved to the proper place later when the if expression
3838 -- is expanded.
3840 elsif N = ElseX then
3841 if Present (Else_Actions (P)) then
3842 Insert_List_After_And_Analyze
3843 (Last (Else_Actions (P)), Ins_Actions);
3844 else
3845 Set_Else_Actions (P, Ins_Actions);
3846 Analyze_List (Else_Actions (P));
3847 end if;
3849 return;
3851 -- Actions belong to the condition. In this case they are
3852 -- unconditionally executed, and so we can continue the
3853 -- search for the proper insert point.
3855 else
3856 null;
3857 end if;
3858 end;
3860 -- Alternative of case expression, we place the action in the
3861 -- Actions field of the case expression alternative, this will
3862 -- be handled when the case expression is expanded.
3864 when N_Case_Expression_Alternative =>
3865 if Present (Actions (P)) then
3866 Insert_List_After_And_Analyze
3867 (Last (Actions (P)), Ins_Actions);
3868 else
3869 Set_Actions (P, Ins_Actions);
3870 Analyze_List (Actions (P));
3871 end if;
3873 return;
3875 -- Case of appearing within an Expressions_With_Actions node. When
3876 -- the new actions come from the expression of the expression with
3877 -- actions, they must be added to the existing actions. The other
3878 -- alternative is when the new actions are related to one of the
3879 -- existing actions of the expression with actions, and should
3880 -- never reach here: if actions are inserted on a statement
3881 -- within the Actions of an expression with actions, or on some
3882 -- sub-expression of such a statement, then the outermost proper
3883 -- insertion point is right before the statement, and we should
3884 -- never climb up as far as the N_Expression_With_Actions itself.
3886 when N_Expression_With_Actions =>
3887 if N = Expression (P) then
3888 if Is_Empty_List (Actions (P)) then
3889 Append_List_To (Actions (P), Ins_Actions);
3890 Analyze_List (Actions (P));
3891 else
3892 Insert_List_After_And_Analyze
3893 (Last (Actions (P)), Ins_Actions);
3894 end if;
3896 return;
3898 else
3899 raise Program_Error;
3900 end if;
3902 -- Case of appearing in the condition of a while expression or
3903 -- elsif. We insert the actions into the Condition_Actions field.
3904 -- They will be moved further out when the while loop or elsif
3905 -- is analyzed.
3907 when N_Iteration_Scheme |
3908 N_Elsif_Part
3910 if N = Condition (P) then
3911 if Present (Condition_Actions (P)) then
3912 Insert_List_After_And_Analyze
3913 (Last (Condition_Actions (P)), Ins_Actions);
3914 else
3915 Set_Condition_Actions (P, Ins_Actions);
3917 -- Set the parent of the insert actions explicitly. This
3918 -- is not a syntactic field, but we need the parent field
3919 -- set, in particular so that freeze can understand that
3920 -- it is dealing with condition actions, and properly
3921 -- insert the freezing actions.
3923 Set_Parent (Ins_Actions, P);
3924 Analyze_List (Condition_Actions (P));
3925 end if;
3927 return;
3928 end if;
3930 -- Statements, declarations, pragmas, representation clauses
3932 when
3933 -- Statements
3935 N_Procedure_Call_Statement |
3936 N_Statement_Other_Than_Procedure_Call |
3938 -- Pragmas
3940 N_Pragma |
3942 -- Representation_Clause
3944 N_At_Clause |
3945 N_Attribute_Definition_Clause |
3946 N_Enumeration_Representation_Clause |
3947 N_Record_Representation_Clause |
3949 -- Declarations
3951 N_Abstract_Subprogram_Declaration |
3952 N_Entry_Body |
3953 N_Exception_Declaration |
3954 N_Exception_Renaming_Declaration |
3955 N_Expression_Function |
3956 N_Formal_Abstract_Subprogram_Declaration |
3957 N_Formal_Concrete_Subprogram_Declaration |
3958 N_Formal_Object_Declaration |
3959 N_Formal_Type_Declaration |
3960 N_Full_Type_Declaration |
3961 N_Function_Instantiation |
3962 N_Generic_Function_Renaming_Declaration |
3963 N_Generic_Package_Declaration |
3964 N_Generic_Package_Renaming_Declaration |
3965 N_Generic_Procedure_Renaming_Declaration |
3966 N_Generic_Subprogram_Declaration |
3967 N_Implicit_Label_Declaration |
3968 N_Incomplete_Type_Declaration |
3969 N_Number_Declaration |
3970 N_Object_Declaration |
3971 N_Object_Renaming_Declaration |
3972 N_Package_Body |
3973 N_Package_Body_Stub |
3974 N_Package_Declaration |
3975 N_Package_Instantiation |
3976 N_Package_Renaming_Declaration |
3977 N_Private_Extension_Declaration |
3978 N_Private_Type_Declaration |
3979 N_Procedure_Instantiation |
3980 N_Protected_Body |
3981 N_Protected_Body_Stub |
3982 N_Protected_Type_Declaration |
3983 N_Single_Task_Declaration |
3984 N_Subprogram_Body |
3985 N_Subprogram_Body_Stub |
3986 N_Subprogram_Declaration |
3987 N_Subprogram_Renaming_Declaration |
3988 N_Subtype_Declaration |
3989 N_Task_Body |
3990 N_Task_Body_Stub |
3991 N_Task_Type_Declaration |
3993 -- Use clauses can appear in lists of declarations
3995 N_Use_Package_Clause |
3996 N_Use_Type_Clause |
3998 -- Freeze entity behaves like a declaration or statement
4000 N_Freeze_Entity |
4001 N_Freeze_Generic_Entity
4003 -- Do not insert here if the item is not a list member (this
4004 -- happens for example with a triggering statement, and the
4005 -- proper approach is to insert before the entire select).
4007 if not Is_List_Member (P) then
4008 null;
4010 -- Do not insert if parent of P is an N_Component_Association
4011 -- node (i.e. we are in the context of an N_Aggregate or
4012 -- N_Extension_Aggregate node. In this case we want to insert
4013 -- before the entire aggregate.
4015 elsif Nkind (Parent (P)) = N_Component_Association then
4016 null;
4018 -- Do not insert if the parent of P is either an N_Variant node
4019 -- or an N_Record_Definition node, meaning in either case that
4020 -- P is a member of a component list, and that therefore the
4021 -- actions should be inserted outside the complete record
4022 -- declaration.
4024 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
4025 null;
4027 -- Do not insert freeze nodes within the loop generated for
4028 -- an aggregate, because they may be elaborated too late for
4029 -- subsequent use in the back end: within a package spec the
4030 -- loop is part of the elaboration procedure and is only
4031 -- elaborated during the second pass.
4033 -- If the loop comes from source, or the entity is local to the
4034 -- loop itself it must remain within.
4036 elsif Nkind (Parent (P)) = N_Loop_Statement
4037 and then not Comes_From_Source (Parent (P))
4038 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
4039 and then
4040 Scope (Entity (First (Ins_Actions))) /= Current_Scope
4041 then
4042 null;
4044 -- Otherwise we can go ahead and do the insertion
4046 elsif P = Wrapped_Node then
4047 Store_Before_Actions_In_Scope (Ins_Actions);
4048 return;
4050 else
4051 Insert_List_Before_And_Analyze (P, Ins_Actions);
4052 return;
4053 end if;
4055 -- A special case, N_Raise_xxx_Error can act either as a statement
4056 -- or a subexpression. We tell the difference by looking at the
4057 -- Etype. It is set to Standard_Void_Type in the statement case.
4059 when
4060 N_Raise_xxx_Error =>
4061 if Etype (P) = Standard_Void_Type then
4062 if P = Wrapped_Node then
4063 Store_Before_Actions_In_Scope (Ins_Actions);
4064 else
4065 Insert_List_Before_And_Analyze (P, Ins_Actions);
4066 end if;
4068 return;
4070 -- In the subexpression case, keep climbing
4072 else
4073 null;
4074 end if;
4076 -- If a component association appears within a loop created for
4077 -- an array aggregate, attach the actions to the association so
4078 -- they can be subsequently inserted within the loop. For other
4079 -- component associations insert outside of the aggregate. For
4080 -- an association that will generate a loop, its Loop_Actions
4081 -- attribute is already initialized (see exp_aggr.adb).
4083 -- The list of loop_actions can in turn generate additional ones,
4084 -- that are inserted before the associated node. If the associated
4085 -- node is outside the aggregate, the new actions are collected
4086 -- at the end of the loop actions, to respect the order in which
4087 -- they are to be elaborated.
4089 when
4090 N_Component_Association =>
4091 if Nkind (Parent (P)) = N_Aggregate
4092 and then Present (Loop_Actions (P))
4093 then
4094 if Is_Empty_List (Loop_Actions (P)) then
4095 Set_Loop_Actions (P, Ins_Actions);
4096 Analyze_List (Ins_Actions);
4098 else
4099 declare
4100 Decl : Node_Id;
4102 begin
4103 -- Check whether these actions were generated by a
4104 -- declaration that is part of the loop_ actions
4105 -- for the component_association.
4107 Decl := Assoc_Node;
4108 while Present (Decl) loop
4109 exit when Parent (Decl) = P
4110 and then Is_List_Member (Decl)
4111 and then
4112 List_Containing (Decl) = Loop_Actions (P);
4113 Decl := Parent (Decl);
4114 end loop;
4116 if Present (Decl) then
4117 Insert_List_Before_And_Analyze
4118 (Decl, Ins_Actions);
4119 else
4120 Insert_List_After_And_Analyze
4121 (Last (Loop_Actions (P)), Ins_Actions);
4122 end if;
4123 end;
4124 end if;
4126 return;
4128 else
4129 null;
4130 end if;
4132 -- Another special case, an attribute denoting a procedure call
4134 when
4135 N_Attribute_Reference =>
4136 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
4137 if P = Wrapped_Node then
4138 Store_Before_Actions_In_Scope (Ins_Actions);
4139 else
4140 Insert_List_Before_And_Analyze (P, Ins_Actions);
4141 end if;
4143 return;
4145 -- In the subexpression case, keep climbing
4147 else
4148 null;
4149 end if;
4151 -- A contract node should not belong to the tree
4153 when N_Contract =>
4154 raise Program_Error;
4156 -- For all other node types, keep climbing tree
4158 when
4159 N_Abortable_Part |
4160 N_Accept_Alternative |
4161 N_Access_Definition |
4162 N_Access_Function_Definition |
4163 N_Access_Procedure_Definition |
4164 N_Access_To_Object_Definition |
4165 N_Aggregate |
4166 N_Allocator |
4167 N_Aspect_Specification |
4168 N_Case_Expression |
4169 N_Case_Statement_Alternative |
4170 N_Character_Literal |
4171 N_Compilation_Unit |
4172 N_Compilation_Unit_Aux |
4173 N_Component_Clause |
4174 N_Component_Declaration |
4175 N_Component_Definition |
4176 N_Component_List |
4177 N_Constrained_Array_Definition |
4178 N_Decimal_Fixed_Point_Definition |
4179 N_Defining_Character_Literal |
4180 N_Defining_Identifier |
4181 N_Defining_Operator_Symbol |
4182 N_Defining_Program_Unit_Name |
4183 N_Delay_Alternative |
4184 N_Delta_Constraint |
4185 N_Derived_Type_Definition |
4186 N_Designator |
4187 N_Digits_Constraint |
4188 N_Discriminant_Association |
4189 N_Discriminant_Specification |
4190 N_Empty |
4191 N_Entry_Body_Formal_Part |
4192 N_Entry_Call_Alternative |
4193 N_Entry_Declaration |
4194 N_Entry_Index_Specification |
4195 N_Enumeration_Type_Definition |
4196 N_Error |
4197 N_Exception_Handler |
4198 N_Expanded_Name |
4199 N_Explicit_Dereference |
4200 N_Extension_Aggregate |
4201 N_Floating_Point_Definition |
4202 N_Formal_Decimal_Fixed_Point_Definition |
4203 N_Formal_Derived_Type_Definition |
4204 N_Formal_Discrete_Type_Definition |
4205 N_Formal_Floating_Point_Definition |
4206 N_Formal_Modular_Type_Definition |
4207 N_Formal_Ordinary_Fixed_Point_Definition |
4208 N_Formal_Package_Declaration |
4209 N_Formal_Private_Type_Definition |
4210 N_Formal_Incomplete_Type_Definition |
4211 N_Formal_Signed_Integer_Type_Definition |
4212 N_Function_Call |
4213 N_Function_Specification |
4214 N_Generic_Association |
4215 N_Handled_Sequence_Of_Statements |
4216 N_Identifier |
4217 N_In |
4218 N_Index_Or_Discriminant_Constraint |
4219 N_Indexed_Component |
4220 N_Integer_Literal |
4221 N_Iterator_Specification |
4222 N_Itype_Reference |
4223 N_Label |
4224 N_Loop_Parameter_Specification |
4225 N_Mod_Clause |
4226 N_Modular_Type_Definition |
4227 N_Not_In |
4228 N_Null |
4229 N_Op_Abs |
4230 N_Op_Add |
4231 N_Op_And |
4232 N_Op_Concat |
4233 N_Op_Divide |
4234 N_Op_Eq |
4235 N_Op_Expon |
4236 N_Op_Ge |
4237 N_Op_Gt |
4238 N_Op_Le |
4239 N_Op_Lt |
4240 N_Op_Minus |
4241 N_Op_Mod |
4242 N_Op_Multiply |
4243 N_Op_Ne |
4244 N_Op_Not |
4245 N_Op_Or |
4246 N_Op_Plus |
4247 N_Op_Rem |
4248 N_Op_Rotate_Left |
4249 N_Op_Rotate_Right |
4250 N_Op_Shift_Left |
4251 N_Op_Shift_Right |
4252 N_Op_Shift_Right_Arithmetic |
4253 N_Op_Subtract |
4254 N_Op_Xor |
4255 N_Operator_Symbol |
4256 N_Ordinary_Fixed_Point_Definition |
4257 N_Others_Choice |
4258 N_Package_Specification |
4259 N_Parameter_Association |
4260 N_Parameter_Specification |
4261 N_Pop_Constraint_Error_Label |
4262 N_Pop_Program_Error_Label |
4263 N_Pop_Storage_Error_Label |
4264 N_Pragma_Argument_Association |
4265 N_Procedure_Specification |
4266 N_Protected_Definition |
4267 N_Push_Constraint_Error_Label |
4268 N_Push_Program_Error_Label |
4269 N_Push_Storage_Error_Label |
4270 N_Qualified_Expression |
4271 N_Quantified_Expression |
4272 N_Raise_Expression |
4273 N_Range |
4274 N_Range_Constraint |
4275 N_Real_Literal |
4276 N_Real_Range_Specification |
4277 N_Record_Definition |
4278 N_Reference |
4279 N_SCIL_Dispatch_Table_Tag_Init |
4280 N_SCIL_Dispatching_Call |
4281 N_SCIL_Membership_Test |
4282 N_Selected_Component |
4283 N_Signed_Integer_Type_Definition |
4284 N_Single_Protected_Declaration |
4285 N_Slice |
4286 N_String_Literal |
4287 N_Subtype_Indication |
4288 N_Subunit |
4289 N_Task_Definition |
4290 N_Terminate_Alternative |
4291 N_Triggering_Alternative |
4292 N_Type_Conversion |
4293 N_Unchecked_Expression |
4294 N_Unchecked_Type_Conversion |
4295 N_Unconstrained_Array_Definition |
4296 N_Unused_At_End |
4297 N_Unused_At_Start |
4298 N_Variant |
4299 N_Variant_Part |
4300 N_Validate_Unchecked_Conversion |
4301 N_With_Clause
4303 null;
4305 end case;
4307 -- If we fall through above tests, keep climbing tree
4309 N := P;
4311 if Nkind (Parent (N)) = N_Subunit then
4313 -- This is the proper body corresponding to a stub. Insertion must
4314 -- be done at the point of the stub, which is in the declarative
4315 -- part of the parent unit.
4317 P := Corresponding_Stub (Parent (N));
4319 else
4320 P := Parent (N);
4321 end if;
4322 end loop;
4323 end Insert_Actions;
4325 -- Version with check(s) suppressed
4327 procedure Insert_Actions
4328 (Assoc_Node : Node_Id;
4329 Ins_Actions : List_Id;
4330 Suppress : Check_Id)
4332 begin
4333 if Suppress = All_Checks then
4334 declare
4335 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
4336 begin
4337 Scope_Suppress.Suppress := (others => True);
4338 Insert_Actions (Assoc_Node, Ins_Actions);
4339 Scope_Suppress.Suppress := Sva;
4340 end;
4342 else
4343 declare
4344 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
4345 begin
4346 Scope_Suppress.Suppress (Suppress) := True;
4347 Insert_Actions (Assoc_Node, Ins_Actions);
4348 Scope_Suppress.Suppress (Suppress) := Svg;
4349 end;
4350 end if;
4351 end Insert_Actions;
4353 --------------------------
4354 -- Insert_Actions_After --
4355 --------------------------
4357 procedure Insert_Actions_After
4358 (Assoc_Node : Node_Id;
4359 Ins_Actions : List_Id)
4361 begin
4362 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
4363 Store_After_Actions_In_Scope (Ins_Actions);
4364 else
4365 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
4366 end if;
4367 end Insert_Actions_After;
4369 ------------------------
4370 -- Insert_Declaration --
4371 ------------------------
4373 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
4374 P : Node_Id;
4376 begin
4377 pragma Assert (Nkind (N) in N_Subexpr);
4379 -- Climb until we find a procedure or a package
4381 P := N;
4382 loop
4383 pragma Assert (Present (Parent (P)));
4384 P := Parent (P);
4386 if Is_List_Member (P) then
4387 exit when Nkind_In (Parent (P), N_Package_Specification,
4388 N_Subprogram_Body);
4390 -- Special handling for handled sequence of statements, we must
4391 -- insert in the statements not the exception handlers!
4393 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
4394 P := First (Statements (Parent (P)));
4395 exit;
4396 end if;
4397 end if;
4398 end loop;
4400 -- Now do the insertion
4402 Insert_Before (P, Decl);
4403 Analyze (Decl);
4404 end Insert_Declaration;
4406 ---------------------------------
4407 -- Insert_Library_Level_Action --
4408 ---------------------------------
4410 procedure Insert_Library_Level_Action (N : Node_Id) is
4411 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4413 begin
4414 Push_Scope (Cunit_Entity (Main_Unit));
4415 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
4417 if No (Actions (Aux)) then
4418 Set_Actions (Aux, New_List (N));
4419 else
4420 Append (N, Actions (Aux));
4421 end if;
4423 Analyze (N);
4424 Pop_Scope;
4425 end Insert_Library_Level_Action;
4427 ----------------------------------
4428 -- Insert_Library_Level_Actions --
4429 ----------------------------------
4431 procedure Insert_Library_Level_Actions (L : List_Id) is
4432 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4434 begin
4435 if Is_Non_Empty_List (L) then
4436 Push_Scope (Cunit_Entity (Main_Unit));
4437 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
4439 if No (Actions (Aux)) then
4440 Set_Actions (Aux, L);
4441 Analyze_List (L);
4442 else
4443 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
4444 end if;
4446 Pop_Scope;
4447 end if;
4448 end Insert_Library_Level_Actions;
4450 ----------------------
4451 -- Inside_Init_Proc --
4452 ----------------------
4454 function Inside_Init_Proc return Boolean is
4455 S : Entity_Id;
4457 begin
4458 S := Current_Scope;
4459 while Present (S) and then S /= Standard_Standard loop
4460 if Is_Init_Proc (S) then
4461 return True;
4462 else
4463 S := Scope (S);
4464 end if;
4465 end loop;
4467 return False;
4468 end Inside_Init_Proc;
4470 ----------------------------
4471 -- Is_All_Null_Statements --
4472 ----------------------------
4474 function Is_All_Null_Statements (L : List_Id) return Boolean is
4475 Stm : Node_Id;
4477 begin
4478 Stm := First (L);
4479 while Present (Stm) loop
4480 if Nkind (Stm) /= N_Null_Statement then
4481 return False;
4482 end if;
4484 Next (Stm);
4485 end loop;
4487 return True;
4488 end Is_All_Null_Statements;
4490 --------------------------------------------------
4491 -- Is_Displacement_Of_Object_Or_Function_Result --
4492 --------------------------------------------------
4494 function Is_Displacement_Of_Object_Or_Function_Result
4495 (Obj_Id : Entity_Id) return Boolean
4497 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
4498 -- Determine if particular node denotes a controlled function call. The
4499 -- call may have been heavily expanded.
4501 function Is_Displace_Call (N : Node_Id) return Boolean;
4502 -- Determine whether a particular node is a call to Ada.Tags.Displace.
4503 -- The call might be nested within other actions such as conversions.
4505 function Is_Source_Object (N : Node_Id) return Boolean;
4506 -- Determine whether a particular node denotes a source object
4508 ---------------------------------
4509 -- Is_Controlled_Function_Call --
4510 ---------------------------------
4512 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
4513 Expr : Node_Id := Original_Node (N);
4515 begin
4516 if Nkind (Expr) = N_Function_Call then
4517 Expr := Name (Expr);
4519 -- When a function call appears in Object.Operation format, the
4520 -- original representation has two possible forms depending on the
4521 -- availability of actual parameters:
4523 -- Obj.Func_Call N_Selected_Component
4524 -- Obj.Func_Call (Param) N_Indexed_Component
4526 else
4527 if Nkind (Expr) = N_Indexed_Component then
4528 Expr := Prefix (Expr);
4529 end if;
4531 if Nkind (Expr) = N_Selected_Component then
4532 Expr := Selector_Name (Expr);
4533 end if;
4534 end if;
4536 return
4537 Nkind_In (Expr, N_Expanded_Name, N_Identifier)
4538 and then Ekind (Entity (Expr)) = E_Function
4539 and then Needs_Finalization (Etype (Entity (Expr)));
4540 end Is_Controlled_Function_Call;
4542 ----------------------
4543 -- Is_Displace_Call --
4544 ----------------------
4546 function Is_Displace_Call (N : Node_Id) return Boolean is
4547 Call : Node_Id := N;
4549 begin
4550 -- Strip various actions which may precede a call to Displace
4552 loop
4553 if Nkind (Call) = N_Explicit_Dereference then
4554 Call := Prefix (Call);
4556 elsif Nkind_In (Call, N_Type_Conversion,
4557 N_Unchecked_Type_Conversion)
4558 then
4559 Call := Expression (Call);
4561 else
4562 exit;
4563 end if;
4564 end loop;
4566 return
4567 Present (Call)
4568 and then Nkind (Call) = N_Function_Call
4569 and then Is_RTE (Entity (Name (Call)), RE_Displace);
4570 end Is_Displace_Call;
4572 ----------------------
4573 -- Is_Source_Object --
4574 ----------------------
4576 function Is_Source_Object (N : Node_Id) return Boolean is
4577 begin
4578 return
4579 Present (N)
4580 and then Nkind (N) in N_Has_Entity
4581 and then Is_Object (Entity (N))
4582 and then Comes_From_Source (N);
4583 end Is_Source_Object;
4585 -- Local variables
4587 Decl : constant Node_Id := Parent (Obj_Id);
4588 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4589 Orig_Decl : constant Node_Id := Original_Node (Decl);
4591 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
4593 begin
4594 -- Case 1:
4596 -- Obj : CW_Type := Function_Call (...);
4598 -- rewritten into:
4600 -- Tmp : ... := Function_Call (...)'reference;
4601 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
4603 -- where the return type of the function and the class-wide type require
4604 -- dispatch table pointer displacement.
4606 -- Case 2:
4608 -- Obj : CW_Type := Src_Obj;
4610 -- rewritten into:
4612 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
4614 -- where the type of the source object and the class-wide type require
4615 -- dispatch table pointer displacement.
4617 return
4618 Nkind (Decl) = N_Object_Renaming_Declaration
4619 and then Nkind (Orig_Decl) = N_Object_Declaration
4620 and then Comes_From_Source (Orig_Decl)
4621 and then Is_Class_Wide_Type (Obj_Typ)
4622 and then Is_Displace_Call (Renamed_Object (Obj_Id))
4623 and then
4624 (Is_Controlled_Function_Call (Expression (Orig_Decl))
4625 or else Is_Source_Object (Expression (Orig_Decl)));
4626 end Is_Displacement_Of_Object_Or_Function_Result;
4628 ------------------------------
4629 -- Is_Finalizable_Transient --
4630 ------------------------------
4632 function Is_Finalizable_Transient
4633 (Decl : Node_Id;
4634 Rel_Node : Node_Id) return Boolean
4636 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4637 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4638 Desig : Entity_Id := Obj_Typ;
4640 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
4641 -- Determine whether transient object Trans_Id is initialized either
4642 -- by a function call which returns an access type or simply renames
4643 -- another pointer.
4645 function Initialized_By_Aliased_BIP_Func_Call
4646 (Trans_Id : Entity_Id) return Boolean;
4647 -- Determine whether transient object Trans_Id is initialized by a
4648 -- build-in-place function call where the BIPalloc parameter is of
4649 -- value 1 and BIPaccess is not null. This case creates an aliasing
4650 -- between the returned value and the value denoted by BIPaccess.
4652 function Is_Aliased
4653 (Trans_Id : Entity_Id;
4654 First_Stmt : Node_Id) return Boolean;
4655 -- Determine whether transient object Trans_Id has been renamed or
4656 -- aliased through 'reference in the statement list starting from
4657 -- First_Stmt.
4659 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4660 -- Determine whether transient object Trans_Id is allocated on the heap
4662 function Is_Iterated_Container
4663 (Trans_Id : Entity_Id;
4664 First_Stmt : Node_Id) return Boolean;
4665 -- Determine whether transient object Trans_Id denotes a container which
4666 -- is in the process of being iterated in the statement list starting
4667 -- from First_Stmt.
4669 ---------------------------
4670 -- Initialized_By_Access --
4671 ---------------------------
4673 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4674 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4676 begin
4677 return
4678 Present (Expr)
4679 and then Nkind (Expr) /= N_Reference
4680 and then Is_Access_Type (Etype (Expr));
4681 end Initialized_By_Access;
4683 ------------------------------------------
4684 -- Initialized_By_Aliased_BIP_Func_Call --
4685 ------------------------------------------
4687 function Initialized_By_Aliased_BIP_Func_Call
4688 (Trans_Id : Entity_Id) return Boolean
4690 Call : Node_Id := Expression (Parent (Trans_Id));
4692 begin
4693 -- Build-in-place calls usually appear in 'reference format
4695 if Nkind (Call) = N_Reference then
4696 Call := Prefix (Call);
4697 end if;
4699 if Is_Build_In_Place_Function_Call (Call) then
4700 declare
4701 Access_Nam : Name_Id := No_Name;
4702 Access_OK : Boolean := False;
4703 Actual : Node_Id;
4704 Alloc_Nam : Name_Id := No_Name;
4705 Alloc_OK : Boolean := False;
4706 Formal : Node_Id;
4707 Func_Id : Entity_Id;
4708 Param : Node_Id;
4710 begin
4711 -- Examine all parameter associations of the function call
4713 Param := First (Parameter_Associations (Call));
4714 while Present (Param) loop
4715 if Nkind (Param) = N_Parameter_Association
4716 and then Nkind (Selector_Name (Param)) = N_Identifier
4717 then
4718 Actual := Explicit_Actual_Parameter (Param);
4719 Formal := Selector_Name (Param);
4721 -- Construct the names of formals BIPaccess and BIPalloc
4722 -- using the function name retrieved from an arbitrary
4723 -- formal.
4725 if Access_Nam = No_Name
4726 and then Alloc_Nam = No_Name
4727 and then Present (Entity (Formal))
4728 then
4729 Func_Id := Scope (Entity (Formal));
4731 Access_Nam :=
4732 New_External_Name (Chars (Func_Id),
4733 BIP_Formal_Suffix (BIP_Object_Access));
4735 Alloc_Nam :=
4736 New_External_Name (Chars (Func_Id),
4737 BIP_Formal_Suffix (BIP_Alloc_Form));
4738 end if;
4740 -- A match for BIPaccess => Temp has been found
4742 if Chars (Formal) = Access_Nam
4743 and then Nkind (Actual) /= N_Null
4744 then
4745 Access_OK := True;
4746 end if;
4748 -- A match for BIPalloc => 1 has been found
4750 if Chars (Formal) = Alloc_Nam
4751 and then Nkind (Actual) = N_Integer_Literal
4752 and then Intval (Actual) = Uint_1
4753 then
4754 Alloc_OK := True;
4755 end if;
4756 end if;
4758 Next (Param);
4759 end loop;
4761 return Access_OK and Alloc_OK;
4762 end;
4763 end if;
4765 return False;
4766 end Initialized_By_Aliased_BIP_Func_Call;
4768 ----------------
4769 -- Is_Aliased --
4770 ----------------
4772 function Is_Aliased
4773 (Trans_Id : Entity_Id;
4774 First_Stmt : Node_Id) return Boolean
4776 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
4777 -- Given an object renaming declaration, retrieve the entity of the
4778 -- renamed name. Return Empty if the renamed name is anything other
4779 -- than a variable or a constant.
4781 -------------------------
4782 -- Find_Renamed_Object --
4783 -------------------------
4785 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
4786 Ren_Obj : Node_Id := Empty;
4788 function Find_Object (N : Node_Id) return Traverse_Result;
4789 -- Try to detect an object which is either a constant or a
4790 -- variable.
4792 -----------------
4793 -- Find_Object --
4794 -----------------
4796 function Find_Object (N : Node_Id) return Traverse_Result is
4797 begin
4798 -- Stop the search once a constant or a variable has been
4799 -- detected.
4801 if Nkind (N) = N_Identifier
4802 and then Present (Entity (N))
4803 and then Ekind_In (Entity (N), E_Constant, E_Variable)
4804 then
4805 Ren_Obj := Entity (N);
4806 return Abandon;
4807 end if;
4809 return OK;
4810 end Find_Object;
4812 procedure Search is new Traverse_Proc (Find_Object);
4814 -- Local variables
4816 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
4818 -- Start of processing for Find_Renamed_Object
4820 begin
4821 -- Actions related to dispatching calls may appear as renamings of
4822 -- tags. Do not process this type of renaming because it does not
4823 -- use the actual value of the object.
4825 if not Is_RTE (Typ, RE_Tag_Ptr) then
4826 Search (Name (Ren_Decl));
4827 end if;
4829 return Ren_Obj;
4830 end Find_Renamed_Object;
4832 -- Local variables
4834 Expr : Node_Id;
4835 Ren_Obj : Entity_Id;
4836 Stmt : Node_Id;
4838 -- Start of processing for Is_Aliased
4840 begin
4841 Stmt := First_Stmt;
4842 while Present (Stmt) loop
4843 if Nkind (Stmt) = N_Object_Declaration then
4844 Expr := Expression (Stmt);
4846 if Present (Expr)
4847 and then Nkind (Expr) = N_Reference
4848 and then Nkind (Prefix (Expr)) = N_Identifier
4849 and then Entity (Prefix (Expr)) = Trans_Id
4850 then
4851 return True;
4852 end if;
4854 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
4855 Ren_Obj := Find_Renamed_Object (Stmt);
4857 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
4858 return True;
4859 end if;
4860 end if;
4862 Next (Stmt);
4863 end loop;
4865 return False;
4866 end Is_Aliased;
4868 ------------------
4869 -- Is_Allocated --
4870 ------------------
4872 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
4873 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4874 begin
4875 return
4876 Is_Access_Type (Etype (Trans_Id))
4877 and then Present (Expr)
4878 and then Nkind (Expr) = N_Allocator;
4879 end Is_Allocated;
4881 ---------------------------
4882 -- Is_Iterated_Container --
4883 ---------------------------
4885 function Is_Iterated_Container
4886 (Trans_Id : Entity_Id;
4887 First_Stmt : Node_Id) return Boolean
4889 Aspect : Node_Id;
4890 Call : Node_Id;
4891 Iter : Entity_Id;
4892 Param : Node_Id;
4893 Stmt : Node_Id;
4894 Typ : Entity_Id;
4896 begin
4897 -- It is not possible to iterate over containers in non-Ada 2012 code
4899 if Ada_Version < Ada_2012 then
4900 return False;
4901 end if;
4903 Typ := Etype (Trans_Id);
4905 -- Handle access type created for secondary stack use
4907 if Is_Access_Type (Typ) then
4908 Typ := Designated_Type (Typ);
4909 end if;
4911 -- Look for aspect Default_Iterator. It may be part of a type
4912 -- declaration for a container, or inherited from a base type
4913 -- or parent type.
4915 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
4917 if Present (Aspect) then
4918 Iter := Entity (Aspect);
4920 -- Examine the statements following the container object and
4921 -- look for a call to the default iterate routine where the
4922 -- first parameter is the transient. Such a call appears as:
4924 -- It : Access_To_CW_Iterator :=
4925 -- Iterate (Tran_Id.all, ...)'reference;
4927 Stmt := First_Stmt;
4928 while Present (Stmt) loop
4930 -- Detect an object declaration which is initialized by a
4931 -- secondary stack function call.
4933 if Nkind (Stmt) = N_Object_Declaration
4934 and then Present (Expression (Stmt))
4935 and then Nkind (Expression (Stmt)) = N_Reference
4936 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
4937 then
4938 Call := Prefix (Expression (Stmt));
4940 -- The call must invoke the default iterate routine of
4941 -- the container and the transient object must appear as
4942 -- the first actual parameter. Skip any calls whose names
4943 -- are not entities.
4945 if Is_Entity_Name (Name (Call))
4946 and then Entity (Name (Call)) = Iter
4947 and then Present (Parameter_Associations (Call))
4948 then
4949 Param := First (Parameter_Associations (Call));
4951 if Nkind (Param) = N_Explicit_Dereference
4952 and then Entity (Prefix (Param)) = Trans_Id
4953 then
4954 return True;
4955 end if;
4956 end if;
4957 end if;
4959 Next (Stmt);
4960 end loop;
4961 end if;
4963 return False;
4964 end Is_Iterated_Container;
4966 -- Start of processing for Is_Finalizable_Transient
4968 begin
4969 -- Handle access types
4971 if Is_Access_Type (Desig) then
4972 Desig := Available_View (Designated_Type (Desig));
4973 end if;
4975 return
4976 Ekind_In (Obj_Id, E_Constant, E_Variable)
4977 and then Needs_Finalization (Desig)
4978 and then Requires_Transient_Scope (Desig)
4979 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
4981 -- Do not consider renamed or 'reference-d transient objects because
4982 -- the act of renaming extends the object's lifetime.
4984 and then not Is_Aliased (Obj_Id, Decl)
4986 -- Do not consider transient objects allocated on the heap since
4987 -- they are attached to a finalization master.
4989 and then not Is_Allocated (Obj_Id)
4991 -- If the transient object is a pointer, check that it is not
4992 -- initialized by a function which returns a pointer or acts as a
4993 -- renaming of another pointer.
4995 and then
4996 (not Is_Access_Type (Obj_Typ)
4997 or else not Initialized_By_Access (Obj_Id))
4999 -- Do not consider transient objects which act as indirect aliases
5000 -- of build-in-place function results.
5002 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
5004 -- Do not consider conversions of tags to class-wide types
5006 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
5008 -- Do not consider containers in the context of iterator loops. Such
5009 -- transient objects must exist for as long as the loop is around,
5010 -- otherwise any operation carried out by the iterator will fail.
5012 and then not Is_Iterated_Container (Obj_Id, Decl);
5013 end Is_Finalizable_Transient;
5015 ---------------------------------
5016 -- Is_Fully_Repped_Tagged_Type --
5017 ---------------------------------
5019 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
5020 U : constant Entity_Id := Underlying_Type (T);
5021 Comp : Entity_Id;
5023 begin
5024 if No (U) or else not Is_Tagged_Type (U) then
5025 return False;
5026 elsif Has_Discriminants (U) then
5027 return False;
5028 elsif not Has_Specified_Layout (U) then
5029 return False;
5030 end if;
5032 -- Here we have a tagged type, see if it has any unlayed out fields
5033 -- other than a possible tag and parent fields. If so, we return False.
5035 Comp := First_Component (U);
5036 while Present (Comp) loop
5037 if not Is_Tag (Comp)
5038 and then Chars (Comp) /= Name_uParent
5039 and then No (Component_Clause (Comp))
5040 then
5041 return False;
5042 else
5043 Next_Component (Comp);
5044 end if;
5045 end loop;
5047 -- All components are layed out
5049 return True;
5050 end Is_Fully_Repped_Tagged_Type;
5052 ----------------------------------
5053 -- Is_Library_Level_Tagged_Type --
5054 ----------------------------------
5056 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
5057 begin
5058 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
5059 end Is_Library_Level_Tagged_Type;
5061 --------------------------
5062 -- Is_Non_BIP_Func_Call --
5063 --------------------------
5065 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
5066 begin
5067 -- The expected call is of the format
5069 -- Func_Call'reference
5071 return
5072 Nkind (Expr) = N_Reference
5073 and then Nkind (Prefix (Expr)) = N_Function_Call
5074 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
5075 end Is_Non_BIP_Func_Call;
5077 ------------------------------------
5078 -- Is_Object_Access_BIP_Func_Call --
5079 ------------------------------------
5081 function Is_Object_Access_BIP_Func_Call
5082 (Expr : Node_Id;
5083 Obj_Id : Entity_Id) return Boolean
5085 Access_Nam : Name_Id := No_Name;
5086 Actual : Node_Id;
5087 Call : Node_Id;
5088 Formal : Node_Id;
5089 Param : Node_Id;
5091 begin
5092 -- Build-in-place calls usually appear in 'reference format. Note that
5093 -- the accessibility check machinery may add an extra 'reference due to
5094 -- side effect removal.
5096 Call := Expr;
5097 while Nkind (Call) = N_Reference loop
5098 Call := Prefix (Call);
5099 end loop;
5101 if Nkind_In (Call, N_Qualified_Expression,
5102 N_Unchecked_Type_Conversion)
5103 then
5104 Call := Expression (Call);
5105 end if;
5107 if Is_Build_In_Place_Function_Call (Call) then
5109 -- Examine all parameter associations of the function call
5111 Param := First (Parameter_Associations (Call));
5112 while Present (Param) loop
5113 if Nkind (Param) = N_Parameter_Association
5114 and then Nkind (Selector_Name (Param)) = N_Identifier
5115 then
5116 Formal := Selector_Name (Param);
5117 Actual := Explicit_Actual_Parameter (Param);
5119 -- Construct the name of formal BIPaccess. It is much easier to
5120 -- extract the name of the function using an arbitrary formal's
5121 -- scope rather than the Name field of Call.
5123 if Access_Nam = No_Name and then Present (Entity (Formal)) then
5124 Access_Nam :=
5125 New_External_Name
5126 (Chars (Scope (Entity (Formal))),
5127 BIP_Formal_Suffix (BIP_Object_Access));
5128 end if;
5130 -- A match for BIPaccess => Obj_Id'Unrestricted_Access has been
5131 -- found.
5133 if Chars (Formal) = Access_Nam
5134 and then Nkind (Actual) = N_Attribute_Reference
5135 and then Attribute_Name (Actual) = Name_Unrestricted_Access
5136 and then Nkind (Prefix (Actual)) = N_Identifier
5137 and then Entity (Prefix (Actual)) = Obj_Id
5138 then
5139 return True;
5140 end if;
5141 end if;
5143 Next (Param);
5144 end loop;
5145 end if;
5147 return False;
5148 end Is_Object_Access_BIP_Func_Call;
5150 ----------------------------------
5151 -- Is_Possibly_Unaligned_Object --
5152 ----------------------------------
5154 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
5155 T : constant Entity_Id := Etype (N);
5157 begin
5158 -- Objects are never unaligned on VMs
5160 if VM_Target /= No_VM then
5161 return False;
5162 end if;
5164 -- If renamed object, apply test to underlying object
5166 if Is_Entity_Name (N)
5167 and then Is_Object (Entity (N))
5168 and then Present (Renamed_Object (Entity (N)))
5169 then
5170 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
5171 end if;
5173 -- Tagged and controlled types and aliased types are always aligned, as
5174 -- are concurrent types.
5176 if Is_Aliased (T)
5177 or else Has_Controlled_Component (T)
5178 or else Is_Concurrent_Type (T)
5179 or else Is_Tagged_Type (T)
5180 or else Is_Controlled (T)
5181 then
5182 return False;
5183 end if;
5185 -- If this is an element of a packed array, may be unaligned
5187 if Is_Ref_To_Bit_Packed_Array (N) then
5188 return True;
5189 end if;
5191 -- Case of indexed component reference: test whether prefix is unaligned
5193 if Nkind (N) = N_Indexed_Component then
5194 return Is_Possibly_Unaligned_Object (Prefix (N));
5196 -- Case of selected component reference
5198 elsif Nkind (N) = N_Selected_Component then
5199 declare
5200 P : constant Node_Id := Prefix (N);
5201 C : constant Entity_Id := Entity (Selector_Name (N));
5202 M : Nat;
5203 S : Nat;
5205 begin
5206 -- If component reference is for an array with non-static bounds,
5207 -- then it is always aligned: we can only process unaligned arrays
5208 -- with static bounds (more precisely compile time known bounds).
5210 if Is_Array_Type (T)
5211 and then not Compile_Time_Known_Bounds (T)
5212 then
5213 return False;
5214 end if;
5216 -- If component is aliased, it is definitely properly aligned
5218 if Is_Aliased (C) then
5219 return False;
5220 end if;
5222 -- If component is for a type implemented as a scalar, and the
5223 -- record is packed, and the component is other than the first
5224 -- component of the record, then the component may be unaligned.
5226 if Is_Packed (Etype (P))
5227 and then Represented_As_Scalar (Etype (C))
5228 and then First_Entity (Scope (C)) /= C
5229 then
5230 return True;
5231 end if;
5233 -- Compute maximum possible alignment for T
5235 -- If alignment is known, then that settles things
5237 if Known_Alignment (T) then
5238 M := UI_To_Int (Alignment (T));
5240 -- If alignment is not known, tentatively set max alignment
5242 else
5243 M := Ttypes.Maximum_Alignment;
5245 -- We can reduce this if the Esize is known since the default
5246 -- alignment will never be more than the smallest power of 2
5247 -- that does not exceed this Esize value.
5249 if Known_Esize (T) then
5250 S := UI_To_Int (Esize (T));
5252 while (M / 2) >= S loop
5253 M := M / 2;
5254 end loop;
5255 end if;
5256 end if;
5258 -- The following code is historical, it used to be present but it
5259 -- is too cautious, because the front-end does not know the proper
5260 -- default alignments for the target. Also, if the alignment is
5261 -- not known, the front end can't know in any case. If a copy is
5262 -- needed, the back-end will take care of it. This whole section
5263 -- including this comment can be removed later ???
5265 -- If the component reference is for a record that has a specified
5266 -- alignment, and we either know it is too small, or cannot tell,
5267 -- then the component may be unaligned.
5269 -- What is the following commented out code ???
5271 -- if Known_Alignment (Etype (P))
5272 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
5273 -- and then M > Alignment (Etype (P))
5274 -- then
5275 -- return True;
5276 -- end if;
5278 -- Case of component clause present which may specify an
5279 -- unaligned position.
5281 if Present (Component_Clause (C)) then
5283 -- Otherwise we can do a test to make sure that the actual
5284 -- start position in the record, and the length, are both
5285 -- consistent with the required alignment. If not, we know
5286 -- that we are unaligned.
5288 declare
5289 Align_In_Bits : constant Nat := M * System_Storage_Unit;
5290 begin
5291 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
5292 or else Esize (C) mod Align_In_Bits /= 0
5293 then
5294 return True;
5295 end if;
5296 end;
5297 end if;
5299 -- Otherwise, for a component reference, test prefix
5301 return Is_Possibly_Unaligned_Object (P);
5302 end;
5304 -- If not a component reference, must be aligned
5306 else
5307 return False;
5308 end if;
5309 end Is_Possibly_Unaligned_Object;
5311 ---------------------------------
5312 -- Is_Possibly_Unaligned_Slice --
5313 ---------------------------------
5315 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
5316 begin
5317 -- Go to renamed object
5319 if Is_Entity_Name (N)
5320 and then Is_Object (Entity (N))
5321 and then Present (Renamed_Object (Entity (N)))
5322 then
5323 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
5324 end if;
5326 -- The reference must be a slice
5328 if Nkind (N) /= N_Slice then
5329 return False;
5330 end if;
5332 -- We only need to worry if the target has strict alignment
5334 if not Target_Strict_Alignment then
5335 return False;
5336 end if;
5338 -- If it is a slice, then look at the array type being sliced
5340 declare
5341 Sarr : constant Node_Id := Prefix (N);
5342 -- Prefix of the slice, i.e. the array being sliced
5344 Styp : constant Entity_Id := Etype (Prefix (N));
5345 -- Type of the array being sliced
5347 Pref : Node_Id;
5348 Ptyp : Entity_Id;
5350 begin
5351 -- The problems arise if the array object that is being sliced
5352 -- is a component of a record or array, and we cannot guarantee
5353 -- the alignment of the array within its containing object.
5355 -- To investigate this, we look at successive prefixes to see
5356 -- if we have a worrisome indexed or selected component.
5358 Pref := Sarr;
5359 loop
5360 -- Case of array is part of an indexed component reference
5362 if Nkind (Pref) = N_Indexed_Component then
5363 Ptyp := Etype (Prefix (Pref));
5365 -- The only problematic case is when the array is packed, in
5366 -- which case we really know nothing about the alignment of
5367 -- individual components.
5369 if Is_Bit_Packed_Array (Ptyp) then
5370 return True;
5371 end if;
5373 -- Case of array is part of a selected component reference
5375 elsif Nkind (Pref) = N_Selected_Component then
5376 Ptyp := Etype (Prefix (Pref));
5378 -- We are definitely in trouble if the record in question
5379 -- has an alignment, and either we know this alignment is
5380 -- inconsistent with the alignment of the slice, or we don't
5381 -- know what the alignment of the slice should be.
5383 if Known_Alignment (Ptyp)
5384 and then (Unknown_Alignment (Styp)
5385 or else Alignment (Styp) > Alignment (Ptyp))
5386 then
5387 return True;
5388 end if;
5390 -- We are in potential trouble if the record type is packed.
5391 -- We could special case when we know that the array is the
5392 -- first component, but that's not such a simple case ???
5394 if Is_Packed (Ptyp) then
5395 return True;
5396 end if;
5398 -- We are in trouble if there is a component clause, and
5399 -- either we do not know the alignment of the slice, or
5400 -- the alignment of the slice is inconsistent with the
5401 -- bit position specified by the component clause.
5403 declare
5404 Field : constant Entity_Id := Entity (Selector_Name (Pref));
5405 begin
5406 if Present (Component_Clause (Field))
5407 and then
5408 (Unknown_Alignment (Styp)
5409 or else
5410 (Component_Bit_Offset (Field) mod
5411 (System_Storage_Unit * Alignment (Styp))) /= 0)
5412 then
5413 return True;
5414 end if;
5415 end;
5417 -- For cases other than selected or indexed components we know we
5418 -- are OK, since no issues arise over alignment.
5420 else
5421 return False;
5422 end if;
5424 -- We processed an indexed component or selected component
5425 -- reference that looked safe, so keep checking prefixes.
5427 Pref := Prefix (Pref);
5428 end loop;
5429 end;
5430 end Is_Possibly_Unaligned_Slice;
5432 -------------------------------
5433 -- Is_Related_To_Func_Return --
5434 -------------------------------
5436 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
5437 Expr : constant Node_Id := Related_Expression (Id);
5438 begin
5439 return
5440 Present (Expr)
5441 and then Nkind (Expr) = N_Explicit_Dereference
5442 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
5443 end Is_Related_To_Func_Return;
5445 --------------------------------
5446 -- Is_Ref_To_Bit_Packed_Array --
5447 --------------------------------
5449 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
5450 Result : Boolean;
5451 Expr : Node_Id;
5453 begin
5454 if Is_Entity_Name (N)
5455 and then Is_Object (Entity (N))
5456 and then Present (Renamed_Object (Entity (N)))
5457 then
5458 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
5459 end if;
5461 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5462 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
5463 Result := True;
5464 else
5465 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
5466 end if;
5468 if Result and then Nkind (N) = N_Indexed_Component then
5469 Expr := First (Expressions (N));
5470 while Present (Expr) loop
5471 Force_Evaluation (Expr);
5472 Next (Expr);
5473 end loop;
5474 end if;
5476 return Result;
5478 else
5479 return False;
5480 end if;
5481 end Is_Ref_To_Bit_Packed_Array;
5483 --------------------------------
5484 -- Is_Ref_To_Bit_Packed_Slice --
5485 --------------------------------
5487 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
5488 begin
5489 if Nkind (N) = N_Type_Conversion then
5490 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
5492 elsif Is_Entity_Name (N)
5493 and then Is_Object (Entity (N))
5494 and then Present (Renamed_Object (Entity (N)))
5495 then
5496 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
5498 elsif Nkind (N) = N_Slice
5499 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
5500 then
5501 return True;
5503 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5504 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
5506 else
5507 return False;
5508 end if;
5509 end Is_Ref_To_Bit_Packed_Slice;
5511 -----------------------
5512 -- Is_Renamed_Object --
5513 -----------------------
5515 function Is_Renamed_Object (N : Node_Id) return Boolean is
5516 Pnod : constant Node_Id := Parent (N);
5517 Kind : constant Node_Kind := Nkind (Pnod);
5518 begin
5519 if Kind = N_Object_Renaming_Declaration then
5520 return True;
5521 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
5522 return Is_Renamed_Object (Pnod);
5523 else
5524 return False;
5525 end if;
5526 end Is_Renamed_Object;
5528 --------------------------------------
5529 -- Is_Secondary_Stack_BIP_Func_Call --
5530 --------------------------------------
5532 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
5533 Alloc_Nam : Name_Id := No_Name;
5534 Actual : Node_Id;
5535 Call : Node_Id := Expr;
5536 Formal : Node_Id;
5537 Param : Node_Id;
5539 begin
5540 -- Build-in-place calls usually appear in 'reference format. Note that
5541 -- the accessibility check machinery may add an extra 'reference due to
5542 -- side effect removal.
5544 while Nkind (Call) = N_Reference loop
5545 Call := Prefix (Call);
5546 end loop;
5548 if Nkind_In (Call, N_Qualified_Expression,
5549 N_Unchecked_Type_Conversion)
5550 then
5551 Call := Expression (Call);
5552 end if;
5554 if Is_Build_In_Place_Function_Call (Call) then
5556 -- Examine all parameter associations of the function call
5558 Param := First (Parameter_Associations (Call));
5559 while Present (Param) loop
5560 if Nkind (Param) = N_Parameter_Association
5561 and then Nkind (Selector_Name (Param)) = N_Identifier
5562 then
5563 Formal := Selector_Name (Param);
5564 Actual := Explicit_Actual_Parameter (Param);
5566 -- Construct the name of formal BIPalloc. It is much easier to
5567 -- extract the name of the function using an arbitrary formal's
5568 -- scope rather than the Name field of Call.
5570 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
5571 Alloc_Nam :=
5572 New_External_Name
5573 (Chars (Scope (Entity (Formal))),
5574 BIP_Formal_Suffix (BIP_Alloc_Form));
5575 end if;
5577 -- A match for BIPalloc => 2 has been found
5579 if Chars (Formal) = Alloc_Nam
5580 and then Nkind (Actual) = N_Integer_Literal
5581 and then Intval (Actual) = Uint_2
5582 then
5583 return True;
5584 end if;
5585 end if;
5587 Next (Param);
5588 end loop;
5589 end if;
5591 return False;
5592 end Is_Secondary_Stack_BIP_Func_Call;
5594 -------------------------------------
5595 -- Is_Tag_To_Class_Wide_Conversion --
5596 -------------------------------------
5598 function Is_Tag_To_Class_Wide_Conversion
5599 (Obj_Id : Entity_Id) return Boolean
5601 Expr : constant Node_Id := Expression (Parent (Obj_Id));
5603 begin
5604 return
5605 Is_Class_Wide_Type (Etype (Obj_Id))
5606 and then Present (Expr)
5607 and then Nkind (Expr) = N_Unchecked_Type_Conversion
5608 and then Etype (Expression (Expr)) = RTE (RE_Tag);
5609 end Is_Tag_To_Class_Wide_Conversion;
5611 ----------------------------
5612 -- Is_Untagged_Derivation --
5613 ----------------------------
5615 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
5616 begin
5617 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
5618 or else
5619 (Is_Private_Type (T) and then Present (Full_View (T))
5620 and then not Is_Tagged_Type (Full_View (T))
5621 and then Is_Derived_Type (Full_View (T))
5622 and then Etype (Full_View (T)) /= T);
5623 end Is_Untagged_Derivation;
5625 ---------------------------
5626 -- Is_Volatile_Reference --
5627 ---------------------------
5629 function Is_Volatile_Reference (N : Node_Id) return Boolean is
5630 begin
5631 -- Only source references are to be treated as volatile, internally
5632 -- generated stuff cannot have volatile external effects.
5634 if not Comes_From_Source (N) then
5635 return False;
5637 -- Never true for reference to a type
5639 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
5640 return False;
5642 -- True if object reference with volatile type
5644 elsif Is_Volatile_Object (N) then
5645 return True;
5647 -- True if reference to volatile entity
5649 elsif Is_Entity_Name (N) then
5650 return Treat_As_Volatile (Entity (N));
5652 -- True for slice of volatile array
5654 elsif Nkind (N) = N_Slice then
5655 return Is_Volatile_Reference (Prefix (N));
5657 -- True if volatile component
5659 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5660 if (Is_Entity_Name (Prefix (N))
5661 and then Has_Volatile_Components (Entity (Prefix (N))))
5662 or else (Present (Etype (Prefix (N)))
5663 and then Has_Volatile_Components (Etype (Prefix (N))))
5664 then
5665 return True;
5666 else
5667 return Is_Volatile_Reference (Prefix (N));
5668 end if;
5670 -- Otherwise false
5672 else
5673 return False;
5674 end if;
5675 end Is_Volatile_Reference;
5677 --------------------------
5678 -- Is_VM_By_Copy_Actual --
5679 --------------------------
5681 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
5682 begin
5683 return VM_Target /= No_VM
5684 and then (Nkind (N) = N_Slice
5685 or else
5686 (Nkind (N) = N_Identifier
5687 and then Present (Renamed_Object (Entity (N)))
5688 and then Nkind (Renamed_Object (Entity (N))) =
5689 N_Slice));
5690 end Is_VM_By_Copy_Actual;
5692 --------------------
5693 -- Kill_Dead_Code --
5694 --------------------
5696 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
5697 W : Boolean := Warn;
5698 -- Set False if warnings suppressed
5700 begin
5701 if Present (N) then
5702 Remove_Warning_Messages (N);
5704 -- Generate warning if appropriate
5706 if W then
5708 -- We suppress the warning if this code is under control of an
5709 -- if statement, whose condition is a simple identifier, and
5710 -- either we are in an instance, or warnings off is set for this
5711 -- identifier. The reason for killing it in the instance case is
5712 -- that it is common and reasonable for code to be deleted in
5713 -- instances for various reasons.
5715 -- Could we use Is_Statically_Unevaluated here???
5717 if Nkind (Parent (N)) = N_If_Statement then
5718 declare
5719 C : constant Node_Id := Condition (Parent (N));
5720 begin
5721 if Nkind (C) = N_Identifier
5722 and then
5723 (In_Instance
5724 or else (Present (Entity (C))
5725 and then Has_Warnings_Off (Entity (C))))
5726 then
5727 W := False;
5728 end if;
5729 end;
5730 end if;
5732 -- Generate warning if not suppressed
5734 if W then
5735 Error_Msg_F
5736 ("?t?this code can never be executed and has been deleted!",
5738 end if;
5739 end if;
5741 -- Recurse into block statements and bodies to process declarations
5742 -- and statements.
5744 if Nkind (N) = N_Block_Statement
5745 or else Nkind (N) = N_Subprogram_Body
5746 or else Nkind (N) = N_Package_Body
5747 then
5748 Kill_Dead_Code (Declarations (N), False);
5749 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
5751 if Nkind (N) = N_Subprogram_Body then
5752 Set_Is_Eliminated (Defining_Entity (N));
5753 end if;
5755 elsif Nkind (N) = N_Package_Declaration then
5756 Kill_Dead_Code (Visible_Declarations (Specification (N)));
5757 Kill_Dead_Code (Private_Declarations (Specification (N)));
5759 -- ??? After this point, Delete_Tree has been called on all
5760 -- declarations in Specification (N), so references to entities
5761 -- therein look suspicious.
5763 declare
5764 E : Entity_Id := First_Entity (Defining_Entity (N));
5766 begin
5767 while Present (E) loop
5768 if Ekind (E) = E_Operator then
5769 Set_Is_Eliminated (E);
5770 end if;
5772 Next_Entity (E);
5773 end loop;
5774 end;
5776 -- Recurse into composite statement to kill individual statements in
5777 -- particular instantiations.
5779 elsif Nkind (N) = N_If_Statement then
5780 Kill_Dead_Code (Then_Statements (N));
5781 Kill_Dead_Code (Elsif_Parts (N));
5782 Kill_Dead_Code (Else_Statements (N));
5784 elsif Nkind (N) = N_Loop_Statement then
5785 Kill_Dead_Code (Statements (N));
5787 elsif Nkind (N) = N_Case_Statement then
5788 declare
5789 Alt : Node_Id;
5790 begin
5791 Alt := First (Alternatives (N));
5792 while Present (Alt) loop
5793 Kill_Dead_Code (Statements (Alt));
5794 Next (Alt);
5795 end loop;
5796 end;
5798 elsif Nkind (N) = N_Case_Statement_Alternative then
5799 Kill_Dead_Code (Statements (N));
5801 -- Deal with dead instances caused by deleting instantiations
5803 elsif Nkind (N) in N_Generic_Instantiation then
5804 Remove_Dead_Instance (N);
5805 end if;
5806 end if;
5807 end Kill_Dead_Code;
5809 -- Case where argument is a list of nodes to be killed
5811 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
5812 N : Node_Id;
5813 W : Boolean;
5815 begin
5816 W := Warn;
5818 if Is_Non_Empty_List (L) then
5819 N := First (L);
5820 while Present (N) loop
5821 Kill_Dead_Code (N, W);
5822 W := False;
5823 Next (N);
5824 end loop;
5825 end if;
5826 end Kill_Dead_Code;
5828 ------------------------
5829 -- Known_Non_Negative --
5830 ------------------------
5832 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
5833 begin
5834 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
5835 return True;
5837 else
5838 declare
5839 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
5840 begin
5841 return
5842 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
5843 end;
5844 end if;
5845 end Known_Non_Negative;
5847 --------------------
5848 -- Known_Non_Null --
5849 --------------------
5851 function Known_Non_Null (N : Node_Id) return Boolean is
5852 begin
5853 -- Checks for case where N is an entity reference
5855 if Is_Entity_Name (N) and then Present (Entity (N)) then
5856 declare
5857 E : constant Entity_Id := Entity (N);
5858 Op : Node_Kind;
5859 Val : Node_Id;
5861 begin
5862 -- First check if we are in decisive conditional
5864 Get_Current_Value_Condition (N, Op, Val);
5866 if Known_Null (Val) then
5867 if Op = N_Op_Eq then
5868 return False;
5869 elsif Op = N_Op_Ne then
5870 return True;
5871 end if;
5872 end if;
5874 -- If OK to do replacement, test Is_Known_Non_Null flag
5876 if OK_To_Do_Constant_Replacement (E) then
5877 return Is_Known_Non_Null (E);
5879 -- Otherwise if not safe to do replacement, then say so
5881 else
5882 return False;
5883 end if;
5884 end;
5886 -- True if access attribute
5888 elsif Nkind (N) = N_Attribute_Reference
5889 and then Nam_In (Attribute_Name (N), Name_Access,
5890 Name_Unchecked_Access,
5891 Name_Unrestricted_Access)
5892 then
5893 return True;
5895 -- True if allocator
5897 elsif Nkind (N) = N_Allocator then
5898 return True;
5900 -- For a conversion, true if expression is known non-null
5902 elsif Nkind (N) = N_Type_Conversion then
5903 return Known_Non_Null (Expression (N));
5905 -- Above are all cases where the value could be determined to be
5906 -- non-null. In all other cases, we don't know, so return False.
5908 else
5909 return False;
5910 end if;
5911 end Known_Non_Null;
5913 ----------------
5914 -- Known_Null --
5915 ----------------
5917 function Known_Null (N : Node_Id) return Boolean is
5918 begin
5919 -- Checks for case where N is an entity reference
5921 if Is_Entity_Name (N) and then Present (Entity (N)) then
5922 declare
5923 E : constant Entity_Id := Entity (N);
5924 Op : Node_Kind;
5925 Val : Node_Id;
5927 begin
5928 -- Constant null value is for sure null
5930 if Ekind (E) = E_Constant
5931 and then Known_Null (Constant_Value (E))
5932 then
5933 return True;
5934 end if;
5936 -- First check if we are in decisive conditional
5938 Get_Current_Value_Condition (N, Op, Val);
5940 if Known_Null (Val) then
5941 if Op = N_Op_Eq then
5942 return True;
5943 elsif Op = N_Op_Ne then
5944 return False;
5945 end if;
5946 end if;
5948 -- If OK to do replacement, test Is_Known_Null flag
5950 if OK_To_Do_Constant_Replacement (E) then
5951 return Is_Known_Null (E);
5953 -- Otherwise if not safe to do replacement, then say so
5955 else
5956 return False;
5957 end if;
5958 end;
5960 -- True if explicit reference to null
5962 elsif Nkind (N) = N_Null then
5963 return True;
5965 -- For a conversion, true if expression is known null
5967 elsif Nkind (N) = N_Type_Conversion then
5968 return Known_Null (Expression (N));
5970 -- Above are all cases where the value could be determined to be null.
5971 -- In all other cases, we don't know, so return False.
5973 else
5974 return False;
5975 end if;
5976 end Known_Null;
5978 -----------------------------
5979 -- Make_CW_Equivalent_Type --
5980 -----------------------------
5982 -- Create a record type used as an equivalent of any member of the class
5983 -- which takes its size from exp.
5985 -- Generate the following code:
5987 -- type Equiv_T is record
5988 -- _parent : T (List of discriminant constraints taken from Exp);
5989 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
5990 -- end Equiv_T;
5992 -- ??? Note that this type does not guarantee same alignment as all
5993 -- derived types
5995 function Make_CW_Equivalent_Type
5996 (T : Entity_Id;
5997 E : Node_Id) return Entity_Id
5999 Loc : constant Source_Ptr := Sloc (E);
6000 Root_Typ : constant Entity_Id := Root_Type (T);
6001 List_Def : constant List_Id := Empty_List;
6002 Comp_List : constant List_Id := New_List;
6003 Equiv_Type : Entity_Id;
6004 Range_Type : Entity_Id;
6005 Str_Type : Entity_Id;
6006 Constr_Root : Entity_Id;
6007 Sizexpr : Node_Id;
6009 begin
6010 -- If the root type is already constrained, there are no discriminants
6011 -- in the expression.
6013 if not Has_Discriminants (Root_Typ)
6014 or else Is_Constrained (Root_Typ)
6015 then
6016 Constr_Root := Root_Typ;
6017 else
6018 Constr_Root := Make_Temporary (Loc, 'R');
6020 -- subtype cstr__n is T (List of discr constraints taken from Exp)
6022 Append_To (List_Def,
6023 Make_Subtype_Declaration (Loc,
6024 Defining_Identifier => Constr_Root,
6025 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
6026 end if;
6028 -- Generate the range subtype declaration
6030 Range_Type := Make_Temporary (Loc, 'G');
6032 if not Is_Interface (Root_Typ) then
6034 -- subtype rg__xx is
6035 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
6037 Sizexpr :=
6038 Make_Op_Subtract (Loc,
6039 Left_Opnd =>
6040 Make_Attribute_Reference (Loc,
6041 Prefix =>
6042 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6043 Attribute_Name => Name_Size),
6044 Right_Opnd =>
6045 Make_Attribute_Reference (Loc,
6046 Prefix => New_Occurrence_Of (Constr_Root, Loc),
6047 Attribute_Name => Name_Object_Size));
6048 else
6049 -- subtype rg__xx is
6050 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
6052 Sizexpr :=
6053 Make_Attribute_Reference (Loc,
6054 Prefix =>
6055 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6056 Attribute_Name => Name_Size);
6057 end if;
6059 Set_Paren_Count (Sizexpr, 1);
6061 Append_To (List_Def,
6062 Make_Subtype_Declaration (Loc,
6063 Defining_Identifier => Range_Type,
6064 Subtype_Indication =>
6065 Make_Subtype_Indication (Loc,
6066 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
6067 Constraint => Make_Range_Constraint (Loc,
6068 Range_Expression =>
6069 Make_Range (Loc,
6070 Low_Bound => Make_Integer_Literal (Loc, 1),
6071 High_Bound =>
6072 Make_Op_Divide (Loc,
6073 Left_Opnd => Sizexpr,
6074 Right_Opnd => Make_Integer_Literal (Loc,
6075 Intval => System_Storage_Unit)))))));
6077 -- subtype str__nn is Storage_Array (rg__x);
6079 Str_Type := Make_Temporary (Loc, 'S');
6080 Append_To (List_Def,
6081 Make_Subtype_Declaration (Loc,
6082 Defining_Identifier => Str_Type,
6083 Subtype_Indication =>
6084 Make_Subtype_Indication (Loc,
6085 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
6086 Constraint =>
6087 Make_Index_Or_Discriminant_Constraint (Loc,
6088 Constraints =>
6089 New_List (New_Occurrence_Of (Range_Type, Loc))))));
6091 -- type Equiv_T is record
6092 -- [ _parent : Tnn; ]
6093 -- E : Str_Type;
6094 -- end Equiv_T;
6096 Equiv_Type := Make_Temporary (Loc, 'T');
6097 Set_Ekind (Equiv_Type, E_Record_Type);
6098 Set_Parent_Subtype (Equiv_Type, Constr_Root);
6100 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
6101 -- treatment for this type. In particular, even though _parent's type
6102 -- is a controlled type or contains controlled components, we do not
6103 -- want to set Has_Controlled_Component on it to avoid making it gain
6104 -- an unwanted _controller component.
6106 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
6108 -- A class-wide equivalent type does not require initialization
6110 Set_Suppress_Initialization (Equiv_Type);
6112 if not Is_Interface (Root_Typ) then
6113 Append_To (Comp_List,
6114 Make_Component_Declaration (Loc,
6115 Defining_Identifier =>
6116 Make_Defining_Identifier (Loc, Name_uParent),
6117 Component_Definition =>
6118 Make_Component_Definition (Loc,
6119 Aliased_Present => False,
6120 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
6121 end if;
6123 Append_To (Comp_List,
6124 Make_Component_Declaration (Loc,
6125 Defining_Identifier => Make_Temporary (Loc, 'C'),
6126 Component_Definition =>
6127 Make_Component_Definition (Loc,
6128 Aliased_Present => False,
6129 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
6131 Append_To (List_Def,
6132 Make_Full_Type_Declaration (Loc,
6133 Defining_Identifier => Equiv_Type,
6134 Type_Definition =>
6135 Make_Record_Definition (Loc,
6136 Component_List =>
6137 Make_Component_List (Loc,
6138 Component_Items => Comp_List,
6139 Variant_Part => Empty))));
6141 -- Suppress all checks during the analysis of the expanded code to avoid
6142 -- the generation of spurious warnings under ZFP run-time.
6144 Insert_Actions (E, List_Def, Suppress => All_Checks);
6145 return Equiv_Type;
6146 end Make_CW_Equivalent_Type;
6148 -------------------------
6149 -- Make_Invariant_Call --
6150 -------------------------
6152 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
6153 Loc : constant Source_Ptr := Sloc (Expr);
6154 Typ : Entity_Id;
6156 begin
6157 Typ := Etype (Expr);
6159 -- Subtypes may be subject to invariants coming from their respective
6160 -- base types. The subtype may be fully or partially private.
6162 if Ekind_In (Typ, E_Array_Subtype,
6163 E_Private_Subtype,
6164 E_Record_Subtype,
6165 E_Record_Subtype_With_Private)
6166 then
6167 Typ := Base_Type (Typ);
6168 end if;
6170 pragma Assert
6171 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
6173 return
6174 Make_Procedure_Call_Statement (Loc,
6175 Name =>
6176 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
6177 Parameter_Associations => New_List (Relocate_Node (Expr)));
6178 end Make_Invariant_Call;
6180 ------------------------
6181 -- Make_Literal_Range --
6182 ------------------------
6184 function Make_Literal_Range
6185 (Loc : Source_Ptr;
6186 Literal_Typ : Entity_Id) return Node_Id
6188 Lo : constant Node_Id :=
6189 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
6190 Index : constant Entity_Id := Etype (Lo);
6192 Hi : Node_Id;
6193 Length_Expr : constant Node_Id :=
6194 Make_Op_Subtract (Loc,
6195 Left_Opnd =>
6196 Make_Integer_Literal (Loc,
6197 Intval => String_Literal_Length (Literal_Typ)),
6198 Right_Opnd =>
6199 Make_Integer_Literal (Loc, 1));
6201 begin
6202 Set_Analyzed (Lo, False);
6204 if Is_Integer_Type (Index) then
6205 Hi :=
6206 Make_Op_Add (Loc,
6207 Left_Opnd => New_Copy_Tree (Lo),
6208 Right_Opnd => Length_Expr);
6209 else
6210 Hi :=
6211 Make_Attribute_Reference (Loc,
6212 Attribute_Name => Name_Val,
6213 Prefix => New_Occurrence_Of (Index, Loc),
6214 Expressions => New_List (
6215 Make_Op_Add (Loc,
6216 Left_Opnd =>
6217 Make_Attribute_Reference (Loc,
6218 Attribute_Name => Name_Pos,
6219 Prefix => New_Occurrence_Of (Index, Loc),
6220 Expressions => New_List (New_Copy_Tree (Lo))),
6221 Right_Opnd => Length_Expr)));
6222 end if;
6224 return
6225 Make_Range (Loc,
6226 Low_Bound => Lo,
6227 High_Bound => Hi);
6228 end Make_Literal_Range;
6230 --------------------------
6231 -- Make_Non_Empty_Check --
6232 --------------------------
6234 function Make_Non_Empty_Check
6235 (Loc : Source_Ptr;
6236 N : Node_Id) return Node_Id
6238 begin
6239 return
6240 Make_Op_Ne (Loc,
6241 Left_Opnd =>
6242 Make_Attribute_Reference (Loc,
6243 Attribute_Name => Name_Length,
6244 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
6245 Right_Opnd =>
6246 Make_Integer_Literal (Loc, 0));
6247 end Make_Non_Empty_Check;
6249 -------------------------
6250 -- Make_Predicate_Call --
6251 -------------------------
6253 function Make_Predicate_Call
6254 (Typ : Entity_Id;
6255 Expr : Node_Id;
6256 Mem : Boolean := False) return Node_Id
6258 Loc : constant Source_Ptr := Sloc (Expr);
6260 begin
6261 pragma Assert (Present (Predicate_Function (Typ)));
6263 -- Call special membership version if requested and available
6265 if Mem then
6266 declare
6267 PFM : constant Entity_Id := Predicate_Function_M (Typ);
6268 begin
6269 if Present (PFM) then
6270 return
6271 Make_Function_Call (Loc,
6272 Name => New_Occurrence_Of (PFM, Loc),
6273 Parameter_Associations => New_List (Relocate_Node (Expr)));
6274 end if;
6275 end;
6276 end if;
6278 -- Case of calling normal predicate function
6280 return
6281 Make_Function_Call (Loc,
6282 Name =>
6283 New_Occurrence_Of (Predicate_Function (Typ), Loc),
6284 Parameter_Associations => New_List (Relocate_Node (Expr)));
6285 end Make_Predicate_Call;
6287 --------------------------
6288 -- Make_Predicate_Check --
6289 --------------------------
6291 function Make_Predicate_Check
6292 (Typ : Entity_Id;
6293 Expr : Node_Id) return Node_Id
6295 Loc : constant Source_Ptr := Sloc (Expr);
6296 Nam : Name_Id;
6298 begin
6299 -- If predicate checks are suppressed, then return a null statement.
6300 -- For this call, we check only the scope setting. If the caller wants
6301 -- to check a specific entity's setting, they must do it manually.
6303 if Predicate_Checks_Suppressed (Empty) then
6304 return Make_Null_Statement (Loc);
6305 end if;
6307 -- Do not generate a check within an internal subprogram (stream
6308 -- functions and the like, including including predicate functions).
6310 if Within_Internal_Subprogram then
6311 return Make_Null_Statement (Loc);
6312 end if;
6314 -- Compute proper name to use, we need to get this right so that the
6315 -- right set of check policies apply to the Check pragma we are making.
6317 if Has_Dynamic_Predicate_Aspect (Typ) then
6318 Nam := Name_Dynamic_Predicate;
6319 elsif Has_Static_Predicate_Aspect (Typ) then
6320 Nam := Name_Static_Predicate;
6321 else
6322 Nam := Name_Predicate;
6323 end if;
6325 return
6326 Make_Pragma (Loc,
6327 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
6328 Pragma_Argument_Associations => New_List (
6329 Make_Pragma_Argument_Association (Loc,
6330 Expression => Make_Identifier (Loc, Nam)),
6331 Make_Pragma_Argument_Association (Loc,
6332 Expression => Make_Predicate_Call (Typ, Expr))));
6333 end Make_Predicate_Check;
6335 ----------------------------
6336 -- Make_Subtype_From_Expr --
6337 ----------------------------
6339 -- 1. If Expr is an unconstrained array expression, creates
6340 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
6342 -- 2. If Expr is a unconstrained discriminated type expression, creates
6343 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
6345 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
6347 function Make_Subtype_From_Expr
6348 (E : Node_Id;
6349 Unc_Typ : Entity_Id) return Node_Id
6351 Loc : constant Source_Ptr := Sloc (E);
6352 List_Constr : constant List_Id := New_List;
6353 D : Entity_Id;
6355 Full_Subtyp : Entity_Id;
6356 Priv_Subtyp : Entity_Id;
6357 Utyp : Entity_Id;
6358 Full_Exp : Node_Id;
6360 begin
6361 if Is_Private_Type (Unc_Typ)
6362 and then Has_Unknown_Discriminants (Unc_Typ)
6363 then
6364 -- Prepare the subtype completion, Go to base type to
6365 -- find underlying type, because the type may be a generic
6366 -- actual or an explicit subtype.
6368 Utyp := Underlying_Type (Base_Type (Unc_Typ));
6369 Full_Subtyp := Make_Temporary (Loc, 'C');
6370 Full_Exp :=
6371 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
6372 Set_Parent (Full_Exp, Parent (E));
6374 Priv_Subtyp := Make_Temporary (Loc, 'P');
6376 Insert_Action (E,
6377 Make_Subtype_Declaration (Loc,
6378 Defining_Identifier => Full_Subtyp,
6379 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
6381 -- Define the dummy private subtype
6383 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
6384 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
6385 Set_Scope (Priv_Subtyp, Full_Subtyp);
6386 Set_Is_Constrained (Priv_Subtyp);
6387 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
6388 Set_Is_Itype (Priv_Subtyp);
6389 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
6391 if Is_Tagged_Type (Priv_Subtyp) then
6392 Set_Class_Wide_Type
6393 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
6394 Set_Direct_Primitive_Operations (Priv_Subtyp,
6395 Direct_Primitive_Operations (Unc_Typ));
6396 end if;
6398 Set_Full_View (Priv_Subtyp, Full_Subtyp);
6400 return New_Occurrence_Of (Priv_Subtyp, Loc);
6402 elsif Is_Array_Type (Unc_Typ) then
6403 for J in 1 .. Number_Dimensions (Unc_Typ) loop
6404 Append_To (List_Constr,
6405 Make_Range (Loc,
6406 Low_Bound =>
6407 Make_Attribute_Reference (Loc,
6408 Prefix => Duplicate_Subexpr_No_Checks (E),
6409 Attribute_Name => Name_First,
6410 Expressions => New_List (
6411 Make_Integer_Literal (Loc, J))),
6413 High_Bound =>
6414 Make_Attribute_Reference (Loc,
6415 Prefix => Duplicate_Subexpr_No_Checks (E),
6416 Attribute_Name => Name_Last,
6417 Expressions => New_List (
6418 Make_Integer_Literal (Loc, J)))));
6419 end loop;
6421 elsif Is_Class_Wide_Type (Unc_Typ) then
6422 declare
6423 CW_Subtype : Entity_Id;
6424 EQ_Typ : Entity_Id := Empty;
6426 begin
6427 -- A class-wide equivalent type is not needed when VM_Target
6428 -- because the VM back-ends handle the class-wide object
6429 -- initialization itself (and doesn't need or want the
6430 -- additional intermediate type to handle the assignment).
6432 if Expander_Active and then Tagged_Type_Expansion then
6434 -- If this is the class-wide type of a completion that is a
6435 -- record subtype, set the type of the class-wide type to be
6436 -- the full base type, for use in the expanded code for the
6437 -- equivalent type. Should this be done earlier when the
6438 -- completion is analyzed ???
6440 if Is_Private_Type (Etype (Unc_Typ))
6441 and then
6442 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
6443 then
6444 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
6445 end if;
6447 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
6448 end if;
6450 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
6451 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
6452 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
6454 return New_Occurrence_Of (CW_Subtype, Loc);
6455 end;
6457 -- Indefinite record type with discriminants
6459 else
6460 D := First_Discriminant (Unc_Typ);
6461 while Present (D) loop
6462 Append_To (List_Constr,
6463 Make_Selected_Component (Loc,
6464 Prefix => Duplicate_Subexpr_No_Checks (E),
6465 Selector_Name => New_Occurrence_Of (D, Loc)));
6467 Next_Discriminant (D);
6468 end loop;
6469 end if;
6471 return
6472 Make_Subtype_Indication (Loc,
6473 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
6474 Constraint =>
6475 Make_Index_Or_Discriminant_Constraint (Loc,
6476 Constraints => List_Constr));
6477 end Make_Subtype_From_Expr;
6479 ----------------------------
6480 -- Matching_Standard_Type --
6481 ----------------------------
6483 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
6484 pragma Assert (Is_Scalar_Type (Typ));
6485 Siz : constant Uint := Esize (Typ);
6487 begin
6488 -- Floating-point cases
6490 if Is_Floating_Point_Type (Typ) then
6491 if Siz <= Esize (Standard_Short_Float) then
6492 return Standard_Short_Float;
6493 elsif Siz <= Esize (Standard_Float) then
6494 return Standard_Float;
6495 elsif Siz <= Esize (Standard_Long_Float) then
6496 return Standard_Long_Float;
6497 elsif Siz <= Esize (Standard_Long_Long_Float) then
6498 return Standard_Long_Long_Float;
6499 else
6500 raise Program_Error;
6501 end if;
6503 -- Integer cases (includes fixed-point types)
6505 -- Unsigned integer cases (includes normal enumeration types)
6507 elsif Is_Unsigned_Type (Typ) then
6508 if Siz <= Esize (Standard_Short_Short_Unsigned) then
6509 return Standard_Short_Short_Unsigned;
6510 elsif Siz <= Esize (Standard_Short_Unsigned) then
6511 return Standard_Short_Unsigned;
6512 elsif Siz <= Esize (Standard_Unsigned) then
6513 return Standard_Unsigned;
6514 elsif Siz <= Esize (Standard_Long_Unsigned) then
6515 return Standard_Long_Unsigned;
6516 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
6517 return Standard_Long_Long_Unsigned;
6518 else
6519 raise Program_Error;
6520 end if;
6522 -- Signed integer cases
6524 else
6525 if Siz <= Esize (Standard_Short_Short_Integer) then
6526 return Standard_Short_Short_Integer;
6527 elsif Siz <= Esize (Standard_Short_Integer) then
6528 return Standard_Short_Integer;
6529 elsif Siz <= Esize (Standard_Integer) then
6530 return Standard_Integer;
6531 elsif Siz <= Esize (Standard_Long_Integer) then
6532 return Standard_Long_Integer;
6533 elsif Siz <= Esize (Standard_Long_Long_Integer) then
6534 return Standard_Long_Long_Integer;
6535 else
6536 raise Program_Error;
6537 end if;
6538 end if;
6539 end Matching_Standard_Type;
6541 -----------------------------
6542 -- May_Generate_Large_Temp --
6543 -----------------------------
6545 -- At the current time, the only types that we return False for (i.e. where
6546 -- we decide we know they cannot generate large temps) are ones where we
6547 -- know the size is 256 bits or less at compile time, and we are still not
6548 -- doing a thorough job on arrays and records ???
6550 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
6551 begin
6552 if not Size_Known_At_Compile_Time (Typ) then
6553 return False;
6555 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
6556 return False;
6558 elsif Is_Array_Type (Typ)
6559 and then Present (Packed_Array_Impl_Type (Typ))
6560 then
6561 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
6563 -- We could do more here to find other small types ???
6565 else
6566 return True;
6567 end if;
6568 end May_Generate_Large_Temp;
6570 ------------------------
6571 -- Needs_Finalization --
6572 ------------------------
6574 function Needs_Finalization (T : Entity_Id) return Boolean is
6575 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
6576 -- If type is not frozen yet, check explicitly among its components,
6577 -- because the Has_Controlled_Component flag is not necessarily set.
6579 -----------------------------------
6580 -- Has_Some_Controlled_Component --
6581 -----------------------------------
6583 function Has_Some_Controlled_Component
6584 (Rec : Entity_Id) return Boolean
6586 Comp : Entity_Id;
6588 begin
6589 if Has_Controlled_Component (Rec) then
6590 return True;
6592 elsif not Is_Frozen (Rec) then
6593 if Is_Record_Type (Rec) then
6594 Comp := First_Entity (Rec);
6596 while Present (Comp) loop
6597 if not Is_Type (Comp)
6598 and then Needs_Finalization (Etype (Comp))
6599 then
6600 return True;
6601 end if;
6603 Next_Entity (Comp);
6604 end loop;
6606 return False;
6608 elsif Is_Array_Type (Rec) then
6609 return Needs_Finalization (Component_Type (Rec));
6611 else
6612 return Has_Controlled_Component (Rec);
6613 end if;
6614 else
6615 return False;
6616 end if;
6617 end Has_Some_Controlled_Component;
6619 -- Start of processing for Needs_Finalization
6621 begin
6622 -- Certain run-time configurations and targets do not provide support
6623 -- for controlled types.
6625 if Restriction_Active (No_Finalization) then
6626 return False;
6628 -- C++, CIL and Java types are not considered controlled. It is assumed
6629 -- that the non-Ada side will handle their clean up.
6631 elsif Convention (T) = Convention_CIL
6632 or else Convention (T) = Convention_CPP
6633 or else Convention (T) = Convention_Java
6634 then
6635 return False;
6637 else
6638 -- Class-wide types are treated as controlled because derivations
6639 -- from the root type can introduce controlled components.
6641 return
6642 Is_Class_Wide_Type (T)
6643 or else Is_Controlled (T)
6644 or else Has_Controlled_Component (T)
6645 or else Has_Some_Controlled_Component (T)
6646 or else
6647 (Is_Concurrent_Type (T)
6648 and then Present (Corresponding_Record_Type (T))
6649 and then Needs_Finalization (Corresponding_Record_Type (T)));
6650 end if;
6651 end Needs_Finalization;
6653 ----------------------------
6654 -- Needs_Constant_Address --
6655 ----------------------------
6657 function Needs_Constant_Address
6658 (Decl : Node_Id;
6659 Typ : Entity_Id) return Boolean
6661 begin
6663 -- If we have no initialization of any kind, then we don't need to place
6664 -- any restrictions on the address clause, because the object will be
6665 -- elaborated after the address clause is evaluated. This happens if the
6666 -- declaration has no initial expression, or the type has no implicit
6667 -- initialization, or the object is imported.
6669 -- The same holds for all initialized scalar types and all access types.
6670 -- Packed bit arrays of size up to 64 are represented using a modular
6671 -- type with an initialization (to zero) and can be processed like other
6672 -- initialized scalar types.
6674 -- If the type is controlled, code to attach the object to a
6675 -- finalization chain is generated at the point of declaration, and
6676 -- therefore the elaboration of the object cannot be delayed: the
6677 -- address expression must be a constant.
6679 if No (Expression (Decl))
6680 and then not Needs_Finalization (Typ)
6681 and then
6682 (not Has_Non_Null_Base_Init_Proc (Typ)
6683 or else Is_Imported (Defining_Identifier (Decl)))
6684 then
6685 return False;
6687 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
6688 or else Is_Access_Type (Typ)
6689 or else
6690 (Is_Bit_Packed_Array (Typ)
6691 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
6692 then
6693 return False;
6695 else
6697 -- Otherwise, we require the address clause to be constant because
6698 -- the call to the initialization procedure (or the attach code) has
6699 -- to happen at the point of the declaration.
6701 -- Actually the IP call has been moved to the freeze actions anyway,
6702 -- so maybe we can relax this restriction???
6704 return True;
6705 end if;
6706 end Needs_Constant_Address;
6708 ----------------------------
6709 -- New_Class_Wide_Subtype --
6710 ----------------------------
6712 function New_Class_Wide_Subtype
6713 (CW_Typ : Entity_Id;
6714 N : Node_Id) return Entity_Id
6716 Res : constant Entity_Id := Create_Itype (E_Void, N);
6717 Res_Name : constant Name_Id := Chars (Res);
6718 Res_Scope : constant Entity_Id := Scope (Res);
6720 begin
6721 Copy_Node (CW_Typ, Res);
6722 Set_Comes_From_Source (Res, False);
6723 Set_Sloc (Res, Sloc (N));
6724 Set_Is_Itype (Res);
6725 Set_Associated_Node_For_Itype (Res, N);
6726 Set_Is_Public (Res, False); -- By default, may be changed below.
6727 Set_Public_Status (Res);
6728 Set_Chars (Res, Res_Name);
6729 Set_Scope (Res, Res_Scope);
6730 Set_Ekind (Res, E_Class_Wide_Subtype);
6731 Set_Next_Entity (Res, Empty);
6732 Set_Etype (Res, Base_Type (CW_Typ));
6733 Set_Is_Frozen (Res, False);
6734 Set_Freeze_Node (Res, Empty);
6735 return (Res);
6736 end New_Class_Wide_Subtype;
6738 --------------------------------
6739 -- Non_Limited_Designated_Type --
6740 ---------------------------------
6742 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
6743 Desig : constant Entity_Id := Designated_Type (T);
6744 begin
6745 if Ekind (Desig) = E_Incomplete_Type
6746 and then Present (Non_Limited_View (Desig))
6747 then
6748 return Non_Limited_View (Desig);
6749 else
6750 return Desig;
6751 end if;
6752 end Non_Limited_Designated_Type;
6754 -----------------------------------
6755 -- OK_To_Do_Constant_Replacement --
6756 -----------------------------------
6758 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
6759 ES : constant Entity_Id := Scope (E);
6760 CS : Entity_Id;
6762 begin
6763 -- Do not replace statically allocated objects, because they may be
6764 -- modified outside the current scope.
6766 if Is_Statically_Allocated (E) then
6767 return False;
6769 -- Do not replace aliased or volatile objects, since we don't know what
6770 -- else might change the value.
6772 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
6773 return False;
6775 -- Debug flag -gnatdM disconnects this optimization
6777 elsif Debug_Flag_MM then
6778 return False;
6780 -- Otherwise check scopes
6782 else
6783 CS := Current_Scope;
6785 loop
6786 -- If we are in right scope, replacement is safe
6788 if CS = ES then
6789 return True;
6791 -- Packages do not affect the determination of safety
6793 elsif Ekind (CS) = E_Package then
6794 exit when CS = Standard_Standard;
6795 CS := Scope (CS);
6797 -- Blocks do not affect the determination of safety
6799 elsif Ekind (CS) = E_Block then
6800 CS := Scope (CS);
6802 -- Loops do not affect the determination of safety. Note that we
6803 -- kill all current values on entry to a loop, so we are just
6804 -- talking about processing within a loop here.
6806 elsif Ekind (CS) = E_Loop then
6807 CS := Scope (CS);
6809 -- Otherwise, the reference is dubious, and we cannot be sure that
6810 -- it is safe to do the replacement.
6812 else
6813 exit;
6814 end if;
6815 end loop;
6817 return False;
6818 end if;
6819 end OK_To_Do_Constant_Replacement;
6821 ------------------------------------
6822 -- Possible_Bit_Aligned_Component --
6823 ------------------------------------
6825 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
6826 begin
6827 case Nkind (N) is
6829 -- Case of indexed component
6831 when N_Indexed_Component =>
6832 declare
6833 P : constant Node_Id := Prefix (N);
6834 Ptyp : constant Entity_Id := Etype (P);
6836 begin
6837 -- If we know the component size and it is less than 64, then
6838 -- we are definitely OK. The back end always does assignment of
6839 -- misaligned small objects correctly.
6841 if Known_Static_Component_Size (Ptyp)
6842 and then Component_Size (Ptyp) <= 64
6843 then
6844 return False;
6846 -- Otherwise, we need to test the prefix, to see if we are
6847 -- indexing from a possibly unaligned component.
6849 else
6850 return Possible_Bit_Aligned_Component (P);
6851 end if;
6852 end;
6854 -- Case of selected component
6856 when N_Selected_Component =>
6857 declare
6858 P : constant Node_Id := Prefix (N);
6859 Comp : constant Entity_Id := Entity (Selector_Name (N));
6861 begin
6862 -- If there is no component clause, then we are in the clear
6863 -- since the back end will never misalign a large component
6864 -- unless it is forced to do so. In the clear means we need
6865 -- only the recursive test on the prefix.
6867 if Component_May_Be_Bit_Aligned (Comp) then
6868 return True;
6869 else
6870 return Possible_Bit_Aligned_Component (P);
6871 end if;
6872 end;
6874 -- For a slice, test the prefix, if that is possibly misaligned,
6875 -- then for sure the slice is.
6877 when N_Slice =>
6878 return Possible_Bit_Aligned_Component (Prefix (N));
6880 -- For an unchecked conversion, check whether the expression may
6881 -- be bit-aligned.
6883 when N_Unchecked_Type_Conversion =>
6884 return Possible_Bit_Aligned_Component (Expression (N));
6886 -- If we have none of the above, it means that we have fallen off the
6887 -- top testing prefixes recursively, and we now have a stand alone
6888 -- object, where we don't have a problem.
6890 when others =>
6891 return False;
6893 end case;
6894 end Possible_Bit_Aligned_Component;
6896 -----------------------------------------------
6897 -- Process_Statements_For_Controlled_Objects --
6898 -----------------------------------------------
6900 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
6901 Loc : constant Source_Ptr := Sloc (N);
6903 function Are_Wrapped (L : List_Id) return Boolean;
6904 -- Determine whether list L contains only one statement which is a block
6906 function Wrap_Statements_In_Block
6907 (L : List_Id;
6908 Scop : Entity_Id := Current_Scope) return Node_Id;
6909 -- Given a list of statements L, wrap it in a block statement and return
6910 -- the generated node. Scop is either the current scope or the scope of
6911 -- the context (if applicable).
6913 -----------------
6914 -- Are_Wrapped --
6915 -----------------
6917 function Are_Wrapped (L : List_Id) return Boolean is
6918 Stmt : constant Node_Id := First (L);
6919 begin
6920 return
6921 Present (Stmt)
6922 and then No (Next (Stmt))
6923 and then Nkind (Stmt) = N_Block_Statement;
6924 end Are_Wrapped;
6926 ------------------------------
6927 -- Wrap_Statements_In_Block --
6928 ------------------------------
6930 function Wrap_Statements_In_Block
6931 (L : List_Id;
6932 Scop : Entity_Id := Current_Scope) return Node_Id
6934 Block_Id : Entity_Id;
6935 Block_Nod : Node_Id;
6936 Iter_Loop : Entity_Id;
6938 begin
6939 Block_Nod :=
6940 Make_Block_Statement (Loc,
6941 Declarations => No_List,
6942 Handled_Statement_Sequence =>
6943 Make_Handled_Sequence_Of_Statements (Loc,
6944 Statements => L));
6946 -- Create a label for the block in case the block needs to manage the
6947 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
6949 Add_Block_Identifier (Block_Nod, Block_Id);
6951 -- When wrapping the statements of an iterator loop, check whether
6952 -- the loop requires secondary stack management and if so, propagate
6953 -- the appropriate flags to the block. This ensures that the cursor
6954 -- is properly cleaned up at each iteration of the loop.
6956 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
6958 if Present (Iter_Loop) then
6959 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
6961 -- Secondary stack reclamation is suppressed when the associated
6962 -- iterator loop contains a return statement which uses the stack.
6964 Set_Sec_Stack_Needed_For_Return
6965 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
6966 end if;
6968 return Block_Nod;
6969 end Wrap_Statements_In_Block;
6971 -- Local variables
6973 Block : Node_Id;
6975 -- Start of processing for Process_Statements_For_Controlled_Objects
6977 begin
6978 -- Whenever a non-handled statement list is wrapped in a block, the
6979 -- block must be explicitly analyzed to redecorate all entities in the
6980 -- list and ensure that a finalizer is properly built.
6982 case Nkind (N) is
6983 when N_Elsif_Part |
6984 N_If_Statement |
6985 N_Conditional_Entry_Call |
6986 N_Selective_Accept =>
6988 -- Check the "then statements" for elsif parts and if statements
6990 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
6991 and then not Is_Empty_List (Then_Statements (N))
6992 and then not Are_Wrapped (Then_Statements (N))
6993 and then Requires_Cleanup_Actions
6994 (Then_Statements (N), False, False)
6995 then
6996 Block := Wrap_Statements_In_Block (Then_Statements (N));
6997 Set_Then_Statements (N, New_List (Block));
6999 Analyze (Block);
7000 end if;
7002 -- Check the "else statements" for conditional entry calls, if
7003 -- statements and selective accepts.
7005 if Nkind_In (N, N_Conditional_Entry_Call,
7006 N_If_Statement,
7007 N_Selective_Accept)
7008 and then not Is_Empty_List (Else_Statements (N))
7009 and then not Are_Wrapped (Else_Statements (N))
7010 and then Requires_Cleanup_Actions
7011 (Else_Statements (N), False, False)
7012 then
7013 Block := Wrap_Statements_In_Block (Else_Statements (N));
7014 Set_Else_Statements (N, New_List (Block));
7016 Analyze (Block);
7017 end if;
7019 when N_Abortable_Part |
7020 N_Accept_Alternative |
7021 N_Case_Statement_Alternative |
7022 N_Delay_Alternative |
7023 N_Entry_Call_Alternative |
7024 N_Exception_Handler |
7025 N_Loop_Statement |
7026 N_Triggering_Alternative =>
7028 if not Is_Empty_List (Statements (N))
7029 and then not Are_Wrapped (Statements (N))
7030 and then Requires_Cleanup_Actions (Statements (N), False, False)
7031 then
7032 if Nkind (N) = N_Loop_Statement
7033 and then Present (Identifier (N))
7034 then
7035 Block :=
7036 Wrap_Statements_In_Block
7037 (L => Statements (N),
7038 Scop => Entity (Identifier (N)));
7039 else
7040 Block := Wrap_Statements_In_Block (Statements (N));
7041 end if;
7043 Set_Statements (N, New_List (Block));
7044 Analyze (Block);
7045 end if;
7047 when others =>
7048 null;
7049 end case;
7050 end Process_Statements_For_Controlled_Objects;
7052 ------------------
7053 -- Power_Of_Two --
7054 ------------------
7056 function Power_Of_Two (N : Node_Id) return Nat is
7057 Typ : constant Entity_Id := Etype (N);
7058 pragma Assert (Is_Integer_Type (Typ));
7060 Siz : constant Nat := UI_To_Int (Esize (Typ));
7061 Val : Uint;
7063 begin
7064 if not Compile_Time_Known_Value (N) then
7065 return 0;
7067 else
7068 Val := Expr_Value (N);
7069 for J in 1 .. Siz - 1 loop
7070 if Val = Uint_2 ** J then
7071 return J;
7072 end if;
7073 end loop;
7075 return 0;
7076 end if;
7077 end Power_Of_Two;
7079 ----------------------
7080 -- Remove_Init_Call --
7081 ----------------------
7083 function Remove_Init_Call
7084 (Var : Entity_Id;
7085 Rep_Clause : Node_Id) return Node_Id
7087 Par : constant Node_Id := Parent (Var);
7088 Typ : constant Entity_Id := Etype (Var);
7090 Init_Proc : Entity_Id;
7091 -- Initialization procedure for Typ
7093 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
7094 -- Look for init call for Var starting at From and scanning the
7095 -- enclosing list until Rep_Clause or the end of the list is reached.
7097 ----------------------------
7098 -- Find_Init_Call_In_List --
7099 ----------------------------
7101 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
7102 Init_Call : Node_Id;
7104 begin
7105 Init_Call := From;
7106 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
7107 if Nkind (Init_Call) = N_Procedure_Call_Statement
7108 and then Is_Entity_Name (Name (Init_Call))
7109 and then Entity (Name (Init_Call)) = Init_Proc
7110 then
7111 return Init_Call;
7112 end if;
7114 Next (Init_Call);
7115 end loop;
7117 return Empty;
7118 end Find_Init_Call_In_List;
7120 Init_Call : Node_Id;
7122 -- Start of processing for Find_Init_Call
7124 begin
7125 if Present (Initialization_Statements (Var)) then
7126 Init_Call := Initialization_Statements (Var);
7127 Set_Initialization_Statements (Var, Empty);
7129 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
7131 -- No init proc for the type, so obviously no call to be found
7133 return Empty;
7135 else
7136 -- We might be able to handle other cases below by just properly
7137 -- setting Initialization_Statements at the point where the init proc
7138 -- call is generated???
7140 Init_Proc := Base_Init_Proc (Typ);
7142 -- First scan the list containing the declaration of Var
7144 Init_Call := Find_Init_Call_In_List (From => Next (Par));
7146 -- If not found, also look on Var's freeze actions list, if any,
7147 -- since the init call may have been moved there (case of an address
7148 -- clause applying to Var).
7150 if No (Init_Call) and then Present (Freeze_Node (Var)) then
7151 Init_Call :=
7152 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
7153 end if;
7155 -- If the initialization call has actuals that use the secondary
7156 -- stack, the call may have been wrapped into a temporary block, in
7157 -- which case the block itself has to be removed.
7159 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
7160 declare
7161 Blk : constant Node_Id := Next (Par);
7162 begin
7163 if Present
7164 (Find_Init_Call_In_List
7165 (First (Statements (Handled_Statement_Sequence (Blk)))))
7166 then
7167 Init_Call := Blk;
7168 end if;
7169 end;
7170 end if;
7171 end if;
7173 if Present (Init_Call) then
7174 Remove (Init_Call);
7175 end if;
7176 return Init_Call;
7177 end Remove_Init_Call;
7179 -------------------------
7180 -- Remove_Side_Effects --
7181 -------------------------
7183 procedure Remove_Side_Effects
7184 (Exp : Node_Id;
7185 Name_Req : Boolean := False;
7186 Renaming_Req : Boolean := False;
7187 Variable_Ref : Boolean := False)
7189 Loc : constant Source_Ptr := Sloc (Exp);
7190 Exp_Type : constant Entity_Id := Etype (Exp);
7191 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
7192 Def_Id : Entity_Id;
7193 E : Node_Id;
7194 New_Exp : Node_Id;
7195 Ptr_Typ_Decl : Node_Id;
7196 Ref_Type : Entity_Id;
7197 Res : Node_Id;
7199 begin
7200 -- Handle cases in which there is nothing to do. In GNATprove mode,
7201 -- removal of side effects is useful for the light expansion of
7202 -- renamings. This removal should only occur when not inside a
7203 -- generic and not doing a pre-analysis.
7205 if not Expander_Active
7206 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
7207 then
7208 return;
7209 end if;
7211 -- Cannot generate temporaries if the invocation to remove side effects
7212 -- was issued too early and the type of the expression is not resolved
7213 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
7214 -- Remove_Side_Effects).
7216 if No (Exp_Type) or else Ekind (Exp_Type) = E_Access_Attribute_Type then
7217 return;
7219 -- No action needed for side-effect free expressions
7221 elsif Side_Effect_Free (Exp, Name_Req, Variable_Ref) then
7222 return;
7223 end if;
7225 -- The remaining procesaing is done with all checks suppressed
7227 -- Note: from now on, don't use return statements, instead do a goto
7228 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
7230 Scope_Suppress.Suppress := (others => True);
7232 -- If it is a scalar type and we need to capture the value, just make
7233 -- a copy. Likewise for a function call, an attribute reference, a
7234 -- conditional expression, an allocator, or an operator. And if we have
7235 -- a volatile reference and Name_Req is not set (see comments for
7236 -- Side_Effect_Free).
7238 if Is_Elementary_Type (Exp_Type)
7240 -- Note: this test is rather mysterious??? Why can't we just test ONLY
7241 -- Is_Elementary_Type and be done with it. If we try that approach, we
7242 -- get some failures (infinite recursions) from the Duplicate_Subexpr
7243 -- call at the end of Checks.Apply_Predicate_Check. To be
7244 -- investigated ???
7246 and then (Variable_Ref
7247 or else Nkind_In (Exp, N_Attribute_Reference,
7248 N_Allocator,
7249 N_Case_Expression,
7250 N_If_Expression,
7251 N_Function_Call)
7252 or else Nkind (Exp) in N_Op
7253 or else (not Name_Req
7254 and then Is_Volatile_Reference (Exp)))
7255 then
7256 Def_Id := Make_Temporary (Loc, 'R', Exp);
7257 Set_Etype (Def_Id, Exp_Type);
7258 Res := New_Occurrence_Of (Def_Id, Loc);
7260 -- If the expression is a packed reference, it must be reanalyzed and
7261 -- expanded, depending on context. This is the case for actuals where
7262 -- a constraint check may capture the actual before expansion of the
7263 -- call is complete.
7265 if Nkind (Exp) = N_Indexed_Component
7266 and then Is_Packed (Etype (Prefix (Exp)))
7267 then
7268 Set_Analyzed (Exp, False);
7269 Set_Analyzed (Prefix (Exp), False);
7270 end if;
7272 -- Generate:
7273 -- Rnn : Exp_Type renames Expr;
7275 if Renaming_Req then
7276 E :=
7277 Make_Object_Renaming_Declaration (Loc,
7278 Defining_Identifier => Def_Id,
7279 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7280 Name => Relocate_Node (Exp));
7282 -- Generate:
7283 -- Rnn : constant Exp_Type := Expr;
7285 else
7286 E :=
7287 Make_Object_Declaration (Loc,
7288 Defining_Identifier => Def_Id,
7289 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7290 Constant_Present => True,
7291 Expression => Relocate_Node (Exp));
7293 Set_Assignment_OK (E);
7294 end if;
7296 Insert_Action (Exp, E);
7298 -- If the expression has the form v.all then we can just capture the
7299 -- pointer, and then do an explicit dereference on the result, but
7300 -- this is not right if this is a volatile reference.
7302 elsif Nkind (Exp) = N_Explicit_Dereference
7303 and then not Is_Volatile_Reference (Exp)
7304 then
7305 Def_Id := Make_Temporary (Loc, 'R', Exp);
7306 Res :=
7307 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
7309 Insert_Action (Exp,
7310 Make_Object_Declaration (Loc,
7311 Defining_Identifier => Def_Id,
7312 Object_Definition =>
7313 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
7314 Constant_Present => True,
7315 Expression => Relocate_Node (Prefix (Exp))));
7317 -- Similar processing for an unchecked conversion of an expression of
7318 -- the form v.all, where we want the same kind of treatment.
7320 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7321 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
7322 then
7323 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
7324 goto Leave;
7326 -- If this is a type conversion, leave the type conversion and remove
7327 -- the side effects in the expression. This is important in several
7328 -- circumstances: for change of representations, and also when this is a
7329 -- view conversion to a smaller object, where gigi can end up creating
7330 -- its own temporary of the wrong size.
7332 elsif Nkind (Exp) = N_Type_Conversion then
7333 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
7334 goto Leave;
7336 -- If this is an unchecked conversion that Gigi can't handle, make
7337 -- a copy or a use a renaming to capture the value.
7339 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7340 and then not Safe_Unchecked_Type_Conversion (Exp)
7341 then
7342 if CW_Or_Has_Controlled_Part (Exp_Type) then
7344 -- Use a renaming to capture the expression, rather than create
7345 -- a controlled temporary.
7347 Def_Id := Make_Temporary (Loc, 'R', Exp);
7348 Res := New_Occurrence_Of (Def_Id, Loc);
7350 Insert_Action (Exp,
7351 Make_Object_Renaming_Declaration (Loc,
7352 Defining_Identifier => Def_Id,
7353 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7354 Name => Relocate_Node (Exp)));
7356 else
7357 Def_Id := Make_Temporary (Loc, 'R', Exp);
7358 Set_Etype (Def_Id, Exp_Type);
7359 Res := New_Occurrence_Of (Def_Id, Loc);
7361 E :=
7362 Make_Object_Declaration (Loc,
7363 Defining_Identifier => Def_Id,
7364 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7365 Constant_Present => not Is_Variable (Exp),
7366 Expression => Relocate_Node (Exp));
7368 Set_Assignment_OK (E);
7369 Insert_Action (Exp, E);
7370 end if;
7372 -- For expressions that denote objects, we can use a renaming scheme.
7373 -- This is needed for correctness in the case of a volatile object of
7374 -- a non-volatile type because the Make_Reference call of the "default"
7375 -- approach would generate an illegal access value (an access value
7376 -- cannot designate such an object - see Analyze_Reference).
7378 elsif Is_Object_Reference (Exp)
7379 and then Nkind (Exp) /= N_Function_Call
7381 -- In Ada 2012 a qualified expression is an object, but for purposes
7382 -- of removing side effects it still need to be transformed into a
7383 -- separate declaration, particularly in the case of an aggregate.
7385 and then Nkind (Exp) /= N_Qualified_Expression
7387 -- We skip using this scheme if we have an object of a volatile
7388 -- type and we do not have Name_Req set true (see comments for
7389 -- Side_Effect_Free).
7391 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
7392 then
7393 Def_Id := Make_Temporary (Loc, 'R', Exp);
7395 if Nkind (Exp) = N_Selected_Component
7396 and then Nkind (Prefix (Exp)) = N_Function_Call
7397 and then Is_Array_Type (Exp_Type)
7398 then
7399 -- Avoid generating a variable-sized temporary, by generating
7400 -- the renaming declaration just for the function call. The
7401 -- transformation could be refined to apply only when the array
7402 -- component is constrained by a discriminant???
7404 Res :=
7405 Make_Selected_Component (Loc,
7406 Prefix => New_Occurrence_Of (Def_Id, Loc),
7407 Selector_Name => Selector_Name (Exp));
7409 Insert_Action (Exp,
7410 Make_Object_Renaming_Declaration (Loc,
7411 Defining_Identifier => Def_Id,
7412 Subtype_Mark =>
7413 New_Occurrence_Of (Base_Type (Etype (Prefix (Exp))), Loc),
7414 Name => Relocate_Node (Prefix (Exp))));
7416 else
7417 Res := New_Occurrence_Of (Def_Id, Loc);
7419 Insert_Action (Exp,
7420 Make_Object_Renaming_Declaration (Loc,
7421 Defining_Identifier => Def_Id,
7422 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7423 Name => Relocate_Node (Exp)));
7424 end if;
7426 -- If this is a packed reference, or a selected component with
7427 -- a non-standard representation, a reference to the temporary
7428 -- will be replaced by a copy of the original expression (see
7429 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
7430 -- elaborated by gigi, and is of course not to be replaced in-line
7431 -- by the expression it renames, which would defeat the purpose of
7432 -- removing the side-effect.
7434 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
7435 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
7436 then
7437 null;
7438 else
7439 Set_Is_Renaming_Of_Object (Def_Id, False);
7440 end if;
7442 -- Otherwise we generate a reference to the value
7444 else
7445 -- An expression which is in SPARK mode is considered side effect
7446 -- free if the resulting value is captured by a variable or a
7447 -- constant.
7449 if GNATprove_Mode
7450 and then Nkind (Parent (Exp)) = N_Object_Declaration
7451 then
7452 goto Leave;
7453 end if;
7455 -- Special processing for function calls that return a limited type.
7456 -- We need to build a declaration that will enable build-in-place
7457 -- expansion of the call. This is not done if the context is already
7458 -- an object declaration, to prevent infinite recursion.
7460 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
7461 -- to accommodate functions returning limited objects by reference.
7463 if Ada_Version >= Ada_2005
7464 and then Nkind (Exp) = N_Function_Call
7465 and then Is_Limited_View (Etype (Exp))
7466 and then Nkind (Parent (Exp)) /= N_Object_Declaration
7467 then
7468 declare
7469 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
7470 Decl : Node_Id;
7472 begin
7473 Decl :=
7474 Make_Object_Declaration (Loc,
7475 Defining_Identifier => Obj,
7476 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7477 Expression => Relocate_Node (Exp));
7479 Insert_Action (Exp, Decl);
7480 Set_Etype (Obj, Exp_Type);
7481 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
7482 goto Leave;
7483 end;
7484 end if;
7486 Def_Id := Make_Temporary (Loc, 'R', Exp);
7488 -- The regular expansion of functions with side effects involves the
7489 -- generation of an access type to capture the return value found on
7490 -- the secondary stack. Since SPARK (and why) cannot process access
7491 -- types, use a different approach which ignores the secondary stack
7492 -- and "copies" the returned object.
7494 if GNATprove_Mode then
7495 Res := New_Occurrence_Of (Def_Id, Loc);
7496 Ref_Type := Exp_Type;
7498 -- Regular expansion utilizing an access type and 'reference
7500 else
7501 Res :=
7502 Make_Explicit_Dereference (Loc,
7503 Prefix => New_Occurrence_Of (Def_Id, Loc));
7505 -- Generate:
7506 -- type Ann is access all <Exp_Type>;
7508 Ref_Type := Make_Temporary (Loc, 'A');
7510 Ptr_Typ_Decl :=
7511 Make_Full_Type_Declaration (Loc,
7512 Defining_Identifier => Ref_Type,
7513 Type_Definition =>
7514 Make_Access_To_Object_Definition (Loc,
7515 All_Present => True,
7516 Subtype_Indication =>
7517 New_Occurrence_Of (Exp_Type, Loc)));
7519 Insert_Action (Exp, Ptr_Typ_Decl);
7520 end if;
7522 E := Exp;
7523 if Nkind (E) = N_Explicit_Dereference then
7524 New_Exp := Relocate_Node (Prefix (E));
7526 else
7527 E := Relocate_Node (E);
7529 -- Do not generate a 'reference in SPARK mode since the access
7530 -- type is not created in the first place.
7532 if GNATprove_Mode then
7533 New_Exp := E;
7535 -- Otherwise generate reference, marking the value as non-null
7536 -- since we know it cannot be null and we don't want a check.
7538 else
7539 New_Exp := Make_Reference (Loc, E);
7540 Set_Is_Known_Non_Null (Def_Id);
7541 end if;
7542 end if;
7544 if Is_Delayed_Aggregate (E) then
7546 -- The expansion of nested aggregates is delayed until the
7547 -- enclosing aggregate is expanded. As aggregates are often
7548 -- qualified, the predicate applies to qualified expressions as
7549 -- well, indicating that the enclosing aggregate has not been
7550 -- expanded yet. At this point the aggregate is part of a
7551 -- stand-alone declaration, and must be fully expanded.
7553 if Nkind (E) = N_Qualified_Expression then
7554 Set_Expansion_Delayed (Expression (E), False);
7555 Set_Analyzed (Expression (E), False);
7556 else
7557 Set_Expansion_Delayed (E, False);
7558 end if;
7560 Set_Analyzed (E, False);
7561 end if;
7563 Insert_Action (Exp,
7564 Make_Object_Declaration (Loc,
7565 Defining_Identifier => Def_Id,
7566 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
7567 Constant_Present => True,
7568 Expression => New_Exp));
7569 end if;
7571 -- Preserve the Assignment_OK flag in all copies, since at least one
7572 -- copy may be used in a context where this flag must be set (otherwise
7573 -- why would the flag be set in the first place).
7575 Set_Assignment_OK (Res, Assignment_OK (Exp));
7577 -- Finally rewrite the original expression and we are done
7579 Rewrite (Exp, Res);
7580 Analyze_And_Resolve (Exp, Exp_Type);
7582 <<Leave>>
7583 Scope_Suppress := Svg_Suppress;
7584 end Remove_Side_Effects;
7586 ---------------------------
7587 -- Represented_As_Scalar --
7588 ---------------------------
7590 function Represented_As_Scalar (T : Entity_Id) return Boolean is
7591 UT : constant Entity_Id := Underlying_Type (T);
7592 begin
7593 return Is_Scalar_Type (UT)
7594 or else (Is_Bit_Packed_Array (UT)
7595 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
7596 end Represented_As_Scalar;
7598 ------------------------------
7599 -- Requires_Cleanup_Actions --
7600 ------------------------------
7602 function Requires_Cleanup_Actions
7603 (N : Node_Id;
7604 Lib_Level : Boolean) return Boolean
7606 At_Lib_Level : constant Boolean :=
7607 Lib_Level
7608 and then Nkind_In (N, N_Package_Body,
7609 N_Package_Specification);
7610 -- N is at the library level if the top-most context is a package and
7611 -- the path taken to reach N does not inlcude non-package constructs.
7613 begin
7614 case Nkind (N) is
7615 when N_Accept_Statement |
7616 N_Block_Statement |
7617 N_Entry_Body |
7618 N_Package_Body |
7619 N_Protected_Body |
7620 N_Subprogram_Body |
7621 N_Task_Body =>
7622 return
7623 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
7624 or else
7625 (Present (Handled_Statement_Sequence (N))
7626 and then
7627 Requires_Cleanup_Actions
7628 (Statements (Handled_Statement_Sequence (N)),
7629 At_Lib_Level, True));
7631 when N_Package_Specification =>
7632 return
7633 Requires_Cleanup_Actions
7634 (Visible_Declarations (N), At_Lib_Level, True)
7635 or else
7636 Requires_Cleanup_Actions
7637 (Private_Declarations (N), At_Lib_Level, True);
7639 when others =>
7640 return False;
7641 end case;
7642 end Requires_Cleanup_Actions;
7644 ------------------------------
7645 -- Requires_Cleanup_Actions --
7646 ------------------------------
7648 function Requires_Cleanup_Actions
7649 (L : List_Id;
7650 Lib_Level : Boolean;
7651 Nested_Constructs : Boolean) return Boolean
7653 Decl : Node_Id;
7654 Expr : Node_Id;
7655 Obj_Id : Entity_Id;
7656 Obj_Typ : Entity_Id;
7657 Pack_Id : Entity_Id;
7658 Typ : Entity_Id;
7660 begin
7661 if No (L)
7662 or else Is_Empty_List (L)
7663 then
7664 return False;
7665 end if;
7667 Decl := First (L);
7668 while Present (Decl) loop
7670 -- Library-level tagged types
7672 if Nkind (Decl) = N_Full_Type_Declaration then
7673 Typ := Defining_Identifier (Decl);
7675 if Is_Tagged_Type (Typ)
7676 and then Is_Library_Level_Entity (Typ)
7677 and then Convention (Typ) = Convention_Ada
7678 and then Present (Access_Disp_Table (Typ))
7679 and then RTE_Available (RE_Unregister_Tag)
7680 and then not No_Run_Time_Mode
7681 and then not Is_Abstract_Type (Typ)
7682 then
7683 return True;
7684 end if;
7686 -- Regular object declarations
7688 elsif Nkind (Decl) = N_Object_Declaration then
7689 Obj_Id := Defining_Identifier (Decl);
7690 Obj_Typ := Base_Type (Etype (Obj_Id));
7691 Expr := Expression (Decl);
7693 -- Bypass any form of processing for objects which have their
7694 -- finalization disabled. This applies only to objects at the
7695 -- library level.
7697 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7698 null;
7700 -- Transient variables are treated separately in order to minimize
7701 -- the size of the generated code. See Exp_Ch7.Process_Transient_
7702 -- Objects.
7704 elsif Is_Processed_Transient (Obj_Id) then
7705 null;
7707 -- The object is of the form:
7708 -- Obj : Typ [:= Expr];
7710 -- Do not process the incomplete view of a deferred constant. Do
7711 -- not consider tag-to-class-wide conversions.
7713 elsif not Is_Imported (Obj_Id)
7714 and then Needs_Finalization (Obj_Typ)
7715 and then not (Ekind (Obj_Id) = E_Constant
7716 and then not Has_Completion (Obj_Id))
7717 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
7718 then
7719 return True;
7721 -- The object is of the form:
7722 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
7724 -- Obj : Access_Typ :=
7725 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
7727 elsif Is_Access_Type (Obj_Typ)
7728 and then Needs_Finalization
7729 (Available_View (Designated_Type (Obj_Typ)))
7730 and then Present (Expr)
7731 and then
7732 (Is_Secondary_Stack_BIP_Func_Call (Expr)
7733 or else
7734 (Is_Non_BIP_Func_Call (Expr)
7735 and then not Is_Related_To_Func_Return (Obj_Id)))
7736 then
7737 return True;
7739 -- Processing for "hook" objects generated for controlled
7740 -- transients declared inside an Expression_With_Actions.
7742 elsif Is_Access_Type (Obj_Typ)
7743 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7744 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7745 N_Object_Declaration
7746 then
7747 return True;
7749 -- Processing for intermediate results of if expressions where
7750 -- one of the alternatives uses a controlled function call.
7752 elsif Is_Access_Type (Obj_Typ)
7753 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7754 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7755 N_Defining_Identifier
7756 and then Present (Expr)
7757 and then Nkind (Expr) = N_Null
7758 then
7759 return True;
7761 -- Simple protected objects which use type System.Tasking.
7762 -- Protected_Objects.Protection to manage their locks should be
7763 -- treated as controlled since they require manual cleanup.
7765 elsif Ekind (Obj_Id) = E_Variable
7766 and then (Is_Simple_Protected_Type (Obj_Typ)
7767 or else Has_Simple_Protected_Object (Obj_Typ))
7768 then
7769 return True;
7770 end if;
7772 -- Specific cases of object renamings
7774 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
7775 Obj_Id := Defining_Identifier (Decl);
7776 Obj_Typ := Base_Type (Etype (Obj_Id));
7778 -- Bypass any form of processing for objects which have their
7779 -- finalization disabled. This applies only to objects at the
7780 -- library level.
7782 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7783 null;
7785 -- Return object of a build-in-place function. This case is
7786 -- recognized and marked by the expansion of an extended return
7787 -- statement (see Expand_N_Extended_Return_Statement).
7789 elsif Needs_Finalization (Obj_Typ)
7790 and then Is_Return_Object (Obj_Id)
7791 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7792 then
7793 return True;
7795 -- Detect a case where a source object has been initialized by
7796 -- a controlled function call or another object which was later
7797 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
7799 -- Obj1 : CW_Type := Src_Obj;
7800 -- Obj2 : CW_Type := Function_Call (...);
7802 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7803 -- Tmp : ... := Function_Call (...)'reference;
7804 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
7806 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
7807 return True;
7808 end if;
7810 -- Inspect the freeze node of an access-to-controlled type and look
7811 -- for a delayed finalization master. This case arises when the
7812 -- freeze actions are inserted at a later time than the expansion of
7813 -- the context. Since Build_Finalizer is never called on a single
7814 -- construct twice, the master will be ultimately left out and never
7815 -- finalized. This is also needed for freeze actions of designated
7816 -- types themselves, since in some cases the finalization master is
7817 -- associated with a designated type's freeze node rather than that
7818 -- of the access type (see handling for freeze actions in
7819 -- Build_Finalization_Master).
7821 elsif Nkind (Decl) = N_Freeze_Entity
7822 and then Present (Actions (Decl))
7823 then
7824 Typ := Entity (Decl);
7826 if ((Is_Access_Type (Typ)
7827 and then not Is_Access_Subprogram_Type (Typ)
7828 and then Needs_Finalization
7829 (Available_View (Designated_Type (Typ))))
7830 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
7831 and then Requires_Cleanup_Actions
7832 (Actions (Decl), Lib_Level, Nested_Constructs)
7833 then
7834 return True;
7835 end if;
7837 -- Nested package declarations
7839 elsif Nested_Constructs
7840 and then Nkind (Decl) = N_Package_Declaration
7841 then
7842 Pack_Id := Defining_Unit_Name (Specification (Decl));
7844 if Nkind (Pack_Id) = N_Defining_Program_Unit_Name then
7845 Pack_Id := Defining_Identifier (Pack_Id);
7846 end if;
7848 if Ekind (Pack_Id) /= E_Generic_Package
7849 and then
7850 Requires_Cleanup_Actions (Specification (Decl), Lib_Level)
7851 then
7852 return True;
7853 end if;
7855 -- Nested package bodies
7857 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
7858 Pack_Id := Corresponding_Spec (Decl);
7860 if Ekind (Pack_Id) /= E_Generic_Package
7861 and then Requires_Cleanup_Actions (Decl, Lib_Level)
7862 then
7863 return True;
7864 end if;
7865 end if;
7867 Next (Decl);
7868 end loop;
7870 return False;
7871 end Requires_Cleanup_Actions;
7873 ------------------------------------
7874 -- Safe_Unchecked_Type_Conversion --
7875 ------------------------------------
7877 -- Note: this function knows quite a bit about the exact requirements of
7878 -- Gigi with respect to unchecked type conversions, and its code must be
7879 -- coordinated with any changes in Gigi in this area.
7881 -- The above requirements should be documented in Sinfo ???
7883 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
7884 Otyp : Entity_Id;
7885 Ityp : Entity_Id;
7886 Oalign : Uint;
7887 Ialign : Uint;
7888 Pexp : constant Node_Id := Parent (Exp);
7890 begin
7891 -- If the expression is the RHS of an assignment or object declaration
7892 -- we are always OK because there will always be a target.
7894 -- Object renaming declarations, (generated for view conversions of
7895 -- actuals in inlined calls), like object declarations, provide an
7896 -- explicit type, and are safe as well.
7898 if (Nkind (Pexp) = N_Assignment_Statement
7899 and then Expression (Pexp) = Exp)
7900 or else Nkind_In (Pexp, N_Object_Declaration,
7901 N_Object_Renaming_Declaration)
7902 then
7903 return True;
7905 -- If the expression is the prefix of an N_Selected_Component we should
7906 -- also be OK because GCC knows to look inside the conversion except if
7907 -- the type is discriminated. We assume that we are OK anyway if the
7908 -- type is not set yet or if it is controlled since we can't afford to
7909 -- introduce a temporary in this case.
7911 elsif Nkind (Pexp) = N_Selected_Component
7912 and then Prefix (Pexp) = Exp
7913 then
7914 if No (Etype (Pexp)) then
7915 return True;
7916 else
7917 return
7918 not Has_Discriminants (Etype (Pexp))
7919 or else Is_Constrained (Etype (Pexp));
7920 end if;
7921 end if;
7923 -- Set the output type, this comes from Etype if it is set, otherwise we
7924 -- take it from the subtype mark, which we assume was already fully
7925 -- analyzed.
7927 if Present (Etype (Exp)) then
7928 Otyp := Etype (Exp);
7929 else
7930 Otyp := Entity (Subtype_Mark (Exp));
7931 end if;
7933 -- The input type always comes from the expression, and we assume
7934 -- this is indeed always analyzed, so we can simply get the Etype.
7936 Ityp := Etype (Expression (Exp));
7938 -- Initialize alignments to unknown so far
7940 Oalign := No_Uint;
7941 Ialign := No_Uint;
7943 -- Replace a concurrent type by its corresponding record type and each
7944 -- type by its underlying type and do the tests on those. The original
7945 -- type may be a private type whose completion is a concurrent type, so
7946 -- find the underlying type first.
7948 if Present (Underlying_Type (Otyp)) then
7949 Otyp := Underlying_Type (Otyp);
7950 end if;
7952 if Present (Underlying_Type (Ityp)) then
7953 Ityp := Underlying_Type (Ityp);
7954 end if;
7956 if Is_Concurrent_Type (Otyp) then
7957 Otyp := Corresponding_Record_Type (Otyp);
7958 end if;
7960 if Is_Concurrent_Type (Ityp) then
7961 Ityp := Corresponding_Record_Type (Ityp);
7962 end if;
7964 -- If the base types are the same, we know there is no problem since
7965 -- this conversion will be a noop.
7967 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
7968 return True;
7970 -- Same if this is an upwards conversion of an untagged type, and there
7971 -- are no constraints involved (could be more general???)
7973 elsif Etype (Ityp) = Otyp
7974 and then not Is_Tagged_Type (Ityp)
7975 and then not Has_Discriminants (Ityp)
7976 and then No (First_Rep_Item (Base_Type (Ityp)))
7977 then
7978 return True;
7980 -- If the expression has an access type (object or subprogram) we assume
7981 -- that the conversion is safe, because the size of the target is safe,
7982 -- even if it is a record (which might be treated as having unknown size
7983 -- at this point).
7985 elsif Is_Access_Type (Ityp) then
7986 return True;
7988 -- If the size of output type is known at compile time, there is never
7989 -- a problem. Note that unconstrained records are considered to be of
7990 -- known size, but we can't consider them that way here, because we are
7991 -- talking about the actual size of the object.
7993 -- We also make sure that in addition to the size being known, we do not
7994 -- have a case which might generate an embarrassingly large temp in
7995 -- stack checking mode.
7997 elsif Size_Known_At_Compile_Time (Otyp)
7998 and then
7999 (not Stack_Checking_Enabled
8000 or else not May_Generate_Large_Temp (Otyp))
8001 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
8002 then
8003 return True;
8005 -- If either type is tagged, then we know the alignment is OK so
8006 -- Gigi will be able to use pointer punning.
8008 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
8009 return True;
8011 -- If either type is a limited record type, we cannot do a copy, so say
8012 -- safe since there's nothing else we can do.
8014 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
8015 return True;
8017 -- Conversions to and from packed array types are always ignored and
8018 -- hence are safe.
8020 elsif Is_Packed_Array_Impl_Type (Otyp)
8021 or else Is_Packed_Array_Impl_Type (Ityp)
8022 then
8023 return True;
8024 end if;
8026 -- The only other cases known to be safe is if the input type's
8027 -- alignment is known to be at least the maximum alignment for the
8028 -- target or if both alignments are known and the output type's
8029 -- alignment is no stricter than the input's. We can use the component
8030 -- type alignement for an array if a type is an unpacked array type.
8032 if Present (Alignment_Clause (Otyp)) then
8033 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
8035 elsif Is_Array_Type (Otyp)
8036 and then Present (Alignment_Clause (Component_Type (Otyp)))
8037 then
8038 Oalign := Expr_Value (Expression (Alignment_Clause
8039 (Component_Type (Otyp))));
8040 end if;
8042 if Present (Alignment_Clause (Ityp)) then
8043 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
8045 elsif Is_Array_Type (Ityp)
8046 and then Present (Alignment_Clause (Component_Type (Ityp)))
8047 then
8048 Ialign := Expr_Value (Expression (Alignment_Clause
8049 (Component_Type (Ityp))));
8050 end if;
8052 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
8053 return True;
8055 elsif Ialign /= No_Uint
8056 and then Oalign /= No_Uint
8057 and then Ialign <= Oalign
8058 then
8059 return True;
8061 -- Otherwise, Gigi cannot handle this and we must make a temporary
8063 else
8064 return False;
8065 end if;
8066 end Safe_Unchecked_Type_Conversion;
8068 ---------------------------------
8069 -- Set_Current_Value_Condition --
8070 ---------------------------------
8072 -- Note: the implementation of this procedure is very closely tied to the
8073 -- implementation of Get_Current_Value_Condition. Here we set required
8074 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
8075 -- them, so they must have a consistent view.
8077 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
8079 procedure Set_Entity_Current_Value (N : Node_Id);
8080 -- If N is an entity reference, where the entity is of an appropriate
8081 -- kind, then set the current value of this entity to Cnode, unless
8082 -- there is already a definite value set there.
8084 procedure Set_Expression_Current_Value (N : Node_Id);
8085 -- If N is of an appropriate form, sets an appropriate entry in current
8086 -- value fields of relevant entities. Multiple entities can be affected
8087 -- in the case of an AND or AND THEN.
8089 ------------------------------
8090 -- Set_Entity_Current_Value --
8091 ------------------------------
8093 procedure Set_Entity_Current_Value (N : Node_Id) is
8094 begin
8095 if Is_Entity_Name (N) then
8096 declare
8097 Ent : constant Entity_Id := Entity (N);
8099 begin
8100 -- Don't capture if not safe to do so
8102 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
8103 return;
8104 end if;
8106 -- Here we have a case where the Current_Value field may need
8107 -- to be set. We set it if it is not already set to a compile
8108 -- time expression value.
8110 -- Note that this represents a decision that one condition
8111 -- blots out another previous one. That's certainly right if
8112 -- they occur at the same level. If the second one is nested,
8113 -- then the decision is neither right nor wrong (it would be
8114 -- equally OK to leave the outer one in place, or take the new
8115 -- inner one. Really we should record both, but our data
8116 -- structures are not that elaborate.
8118 if Nkind (Current_Value (Ent)) not in N_Subexpr then
8119 Set_Current_Value (Ent, Cnode);
8120 end if;
8121 end;
8122 end if;
8123 end Set_Entity_Current_Value;
8125 ----------------------------------
8126 -- Set_Expression_Current_Value --
8127 ----------------------------------
8129 procedure Set_Expression_Current_Value (N : Node_Id) is
8130 Cond : Node_Id;
8132 begin
8133 Cond := N;
8135 -- Loop to deal with (ignore for now) any NOT operators present. The
8136 -- presence of NOT operators will be handled properly when we call
8137 -- Get_Current_Value_Condition.
8139 while Nkind (Cond) = N_Op_Not loop
8140 Cond := Right_Opnd (Cond);
8141 end loop;
8143 -- For an AND or AND THEN, recursively process operands
8145 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
8146 Set_Expression_Current_Value (Left_Opnd (Cond));
8147 Set_Expression_Current_Value (Right_Opnd (Cond));
8148 return;
8149 end if;
8151 -- Check possible relational operator
8153 if Nkind (Cond) in N_Op_Compare then
8154 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
8155 Set_Entity_Current_Value (Left_Opnd (Cond));
8156 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
8157 Set_Entity_Current_Value (Right_Opnd (Cond));
8158 end if;
8160 elsif Nkind_In (Cond,
8161 N_Type_Conversion,
8162 N_Qualified_Expression,
8163 N_Expression_With_Actions)
8164 then
8165 Set_Expression_Current_Value (Expression (Cond));
8167 -- Check possible boolean variable reference
8169 else
8170 Set_Entity_Current_Value (Cond);
8171 end if;
8172 end Set_Expression_Current_Value;
8174 -- Start of processing for Set_Current_Value_Condition
8176 begin
8177 Set_Expression_Current_Value (Condition (Cnode));
8178 end Set_Current_Value_Condition;
8180 --------------------------
8181 -- Set_Elaboration_Flag --
8182 --------------------------
8184 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
8185 Loc : constant Source_Ptr := Sloc (N);
8186 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
8187 Asn : Node_Id;
8189 begin
8190 if Present (Ent) then
8192 -- Nothing to do if at the compilation unit level, because in this
8193 -- case the flag is set by the binder generated elaboration routine.
8195 if Nkind (Parent (N)) = N_Compilation_Unit then
8196 null;
8198 -- Here we do need to generate an assignment statement
8200 else
8201 Check_Restriction (No_Elaboration_Code, N);
8202 Asn :=
8203 Make_Assignment_Statement (Loc,
8204 Name => New_Occurrence_Of (Ent, Loc),
8205 Expression => Make_Integer_Literal (Loc, Uint_1));
8207 if Nkind (Parent (N)) = N_Subunit then
8208 Insert_After (Corresponding_Stub (Parent (N)), Asn);
8209 else
8210 Insert_After (N, Asn);
8211 end if;
8213 Analyze (Asn);
8215 -- Kill current value indication. This is necessary because the
8216 -- tests of this flag are inserted out of sequence and must not
8217 -- pick up bogus indications of the wrong constant value.
8219 Set_Current_Value (Ent, Empty);
8221 -- If the subprogram is in the current declarative part and
8222 -- 'access has been applied to it, generate an elaboration
8223 -- check at the beginning of the declarations of the body.
8225 if Nkind (N) = N_Subprogram_Body
8226 and then Address_Taken (Spec_Id)
8227 and then
8228 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
8229 then
8230 declare
8231 Loc : constant Source_Ptr := Sloc (N);
8232 Decls : constant List_Id := Declarations (N);
8233 Chk : Node_Id;
8235 begin
8236 -- No need to generate this check if first entry in the
8237 -- declaration list is a raise of Program_Error now.
8239 if Present (Decls)
8240 and then Nkind (First (Decls)) = N_Raise_Program_Error
8241 then
8242 return;
8243 end if;
8245 -- Otherwise generate the check
8247 Chk :=
8248 Make_Raise_Program_Error (Loc,
8249 Condition =>
8250 Make_Op_Eq (Loc,
8251 Left_Opnd => New_Occurrence_Of (Ent, Loc),
8252 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
8253 Reason => PE_Access_Before_Elaboration);
8255 if No (Decls) then
8256 Set_Declarations (N, New_List (Chk));
8257 else
8258 Prepend (Chk, Decls);
8259 end if;
8261 Analyze (Chk);
8262 end;
8263 end if;
8264 end if;
8265 end if;
8266 end Set_Elaboration_Flag;
8268 ----------------------------
8269 -- Set_Renamed_Subprogram --
8270 ----------------------------
8272 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
8273 begin
8274 -- If input node is an identifier, we can just reset it
8276 if Nkind (N) = N_Identifier then
8277 Set_Chars (N, Chars (E));
8278 Set_Entity (N, E);
8280 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
8282 else
8283 declare
8284 CS : constant Boolean := Comes_From_Source (N);
8285 begin
8286 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
8287 Set_Entity (N, E);
8288 Set_Comes_From_Source (N, CS);
8289 Set_Analyzed (N, True);
8290 end;
8291 end if;
8292 end Set_Renamed_Subprogram;
8294 ----------------------
8295 -- Side_Effect_Free --
8296 ----------------------
8298 function Side_Effect_Free
8299 (N : Node_Id;
8300 Name_Req : Boolean := False;
8301 Variable_Ref : Boolean := False) return Boolean
8303 Typ : constant Entity_Id := Etype (N);
8304 -- Result type of the expression
8306 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
8307 -- The argument N is a construct where the Prefix is dereferenced if it
8308 -- is an access type and the result is a variable. The call returns True
8309 -- if the construct is side effect free (not considering side effects in
8310 -- other than the prefix which are to be tested by the caller).
8312 function Within_In_Parameter (N : Node_Id) return Boolean;
8313 -- Determines if N is a subcomponent of a composite in-parameter. If so,
8314 -- N is not side-effect free when the actual is global and modifiable
8315 -- indirectly from within a subprogram, because it may be passed by
8316 -- reference. The front-end must be conservative here and assume that
8317 -- this may happen with any array or record type. On the other hand, we
8318 -- cannot create temporaries for all expressions for which this
8319 -- condition is true, for various reasons that might require clearing up
8320 -- ??? For example, discriminant references that appear out of place, or
8321 -- spurious type errors with class-wide expressions. As a result, we
8322 -- limit the transformation to loop bounds, which is so far the only
8323 -- case that requires it.
8325 -----------------------------
8326 -- Safe_Prefixed_Reference --
8327 -----------------------------
8329 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
8330 begin
8331 -- If prefix is not side effect free, definitely not safe
8333 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
8334 return False;
8336 -- If the prefix is of an access type that is not access-to-constant,
8337 -- then this construct is a variable reference, which means it is to
8338 -- be considered to have side effects if Variable_Ref is set True.
8340 elsif Is_Access_Type (Etype (Prefix (N)))
8341 and then not Is_Access_Constant (Etype (Prefix (N)))
8342 and then Variable_Ref
8343 then
8344 -- Exception is a prefix that is the result of a previous removal
8345 -- of side-effects.
8347 return Is_Entity_Name (Prefix (N))
8348 and then not Comes_From_Source (Prefix (N))
8349 and then Ekind (Entity (Prefix (N))) = E_Constant
8350 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
8352 -- If the prefix is an explicit dereference then this construct is a
8353 -- variable reference, which means it is to be considered to have
8354 -- side effects if Variable_Ref is True.
8356 -- We do NOT exclude dereferences of access-to-constant types because
8357 -- we handle them as constant view of variables.
8359 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
8360 and then Variable_Ref
8361 then
8362 return False;
8364 -- Note: The following test is the simplest way of solving a complex
8365 -- problem uncovered by the following test (Side effect on loop bound
8366 -- that is a subcomponent of a global variable:
8368 -- with Text_Io; use Text_Io;
8369 -- procedure Tloop is
8370 -- type X is
8371 -- record
8372 -- V : Natural := 4;
8373 -- S : String (1..5) := (others => 'a');
8374 -- end record;
8375 -- X1 : X;
8377 -- procedure Modi;
8379 -- generic
8380 -- with procedure Action;
8381 -- procedure Loop_G (Arg : X; Msg : String)
8383 -- procedure Loop_G (Arg : X; Msg : String) is
8384 -- begin
8385 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
8386 -- & Natural'Image (Arg.V));
8387 -- for Index in 1 .. Arg.V loop
8388 -- Text_Io.Put_Line
8389 -- (Natural'Image (Index) & " " & Arg.S (Index));
8390 -- if Index > 2 then
8391 -- Modi;
8392 -- end if;
8393 -- end loop;
8394 -- Put_Line ("end loop_g " & Msg);
8395 -- end;
8397 -- procedure Loop1 is new Loop_G (Modi);
8398 -- procedure Modi is
8399 -- begin
8400 -- X1.V := 1;
8401 -- Loop1 (X1, "from modi");
8402 -- end;
8404 -- begin
8405 -- Loop1 (X1, "initial");
8406 -- end;
8408 -- The output of the above program should be:
8410 -- begin loop_g initial will loop till: 4
8411 -- 1 a
8412 -- 2 a
8413 -- 3 a
8414 -- begin loop_g from modi will loop till: 1
8415 -- 1 a
8416 -- end loop_g from modi
8417 -- 4 a
8418 -- begin loop_g from modi will loop till: 1
8419 -- 1 a
8420 -- end loop_g from modi
8421 -- end loop_g initial
8423 -- If a loop bound is a subcomponent of a global variable, a
8424 -- modification of that variable within the loop may incorrectly
8425 -- affect the execution of the loop.
8427 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
8428 and then Within_In_Parameter (Prefix (N))
8429 and then Variable_Ref
8430 then
8431 return False;
8433 -- All other cases are side effect free
8435 else
8436 return True;
8437 end if;
8438 end Safe_Prefixed_Reference;
8440 -------------------------
8441 -- Within_In_Parameter --
8442 -------------------------
8444 function Within_In_Parameter (N : Node_Id) return Boolean is
8445 begin
8446 if not Comes_From_Source (N) then
8447 return False;
8449 elsif Is_Entity_Name (N) then
8450 return Ekind (Entity (N)) = E_In_Parameter;
8452 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8453 return Within_In_Parameter (Prefix (N));
8455 else
8456 return False;
8457 end if;
8458 end Within_In_Parameter;
8460 -- Start of processing for Side_Effect_Free
8462 begin
8463 -- If volatile reference, always consider it to have side effects
8465 if Is_Volatile_Reference (N) then
8466 return False;
8467 end if;
8469 -- Note on checks that could raise Constraint_Error. Strictly, if we
8470 -- take advantage of 11.6, these checks do not count as side effects.
8471 -- However, we would prefer to consider that they are side effects,
8472 -- since the backend CSE does not work very well on expressions which
8473 -- can raise Constraint_Error. On the other hand if we don't consider
8474 -- them to be side effect free, then we get some awkward expansions
8475 -- in -gnato mode, resulting in code insertions at a point where we
8476 -- do not have a clear model for performing the insertions.
8478 -- Special handling for entity names
8480 if Is_Entity_Name (N) then
8482 -- A type reference is always side effect free
8484 if Is_Type (Entity (N)) then
8485 return True;
8487 -- Variables are considered to be a side effect if Variable_Ref
8488 -- is set or if we have a volatile reference and Name_Req is off.
8489 -- If Name_Req is True then we can't help returning a name which
8490 -- effectively allows multiple references in any case.
8492 elsif Is_Variable (N, Use_Original_Node => False) then
8493 return not Variable_Ref
8494 and then (not Is_Volatile_Reference (N) or else Name_Req);
8496 -- Any other entity (e.g. a subtype name) is definitely side
8497 -- effect free.
8499 else
8500 return True;
8501 end if;
8503 -- A value known at compile time is always side effect free
8505 elsif Compile_Time_Known_Value (N) then
8506 return True;
8508 -- A variable renaming is not side-effect free, because the renaming
8509 -- will function like a macro in the front-end in some cases, and an
8510 -- assignment can modify the component designated by N, so we need to
8511 -- create a temporary for it.
8513 -- The guard testing for Entity being present is needed at least in
8514 -- the case of rewritten predicate expressions, and may well also be
8515 -- appropriate elsewhere. Obviously we can't go testing the entity
8516 -- field if it does not exist, so it's reasonable to say that this is
8517 -- not the renaming case if it does not exist.
8519 elsif Is_Entity_Name (Original_Node (N))
8520 and then Present (Entity (Original_Node (N)))
8521 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
8522 and then Ekind (Entity (Original_Node (N))) /= E_Constant
8523 then
8524 declare
8525 RO : constant Node_Id :=
8526 Renamed_Object (Entity (Original_Node (N)));
8528 begin
8529 -- If the renamed object is an indexed component, or an
8530 -- explicit dereference, then the designated object could
8531 -- be modified by an assignment.
8533 if Nkind_In (RO, N_Indexed_Component,
8534 N_Explicit_Dereference)
8535 then
8536 return False;
8538 -- A selected component must have a safe prefix
8540 elsif Nkind (RO) = N_Selected_Component then
8541 return Safe_Prefixed_Reference (RO);
8543 -- In all other cases, designated object cannot be changed so
8544 -- we are side effect free.
8546 else
8547 return True;
8548 end if;
8549 end;
8551 -- Remove_Side_Effects generates an object renaming declaration to
8552 -- capture the expression of a class-wide expression. In VM targets
8553 -- the frontend performs no expansion for dispatching calls to
8554 -- class- wide types since they are handled by the VM. Hence, we must
8555 -- locate here if this node corresponds to a previous invocation of
8556 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
8558 elsif VM_Target /= No_VM
8559 and then not Comes_From_Source (N)
8560 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
8561 and then Is_Class_Wide_Type (Typ)
8562 then
8563 return True;
8564 end if;
8566 -- For other than entity names and compile time known values,
8567 -- check the node kind for special processing.
8569 case Nkind (N) is
8571 -- An attribute reference is side effect free if its expressions
8572 -- are side effect free and its prefix is side effect free or
8573 -- is an entity reference.
8575 -- Is this right? what about x'first where x is a variable???
8577 when N_Attribute_Reference =>
8578 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
8579 and then Attribute_Name (N) /= Name_Input
8580 and then (Is_Entity_Name (Prefix (N))
8581 or else Side_Effect_Free
8582 (Prefix (N), Name_Req, Variable_Ref));
8584 -- A binary operator is side effect free if and both operands are
8585 -- side effect free. For this purpose binary operators include
8586 -- membership tests and short circuit forms.
8588 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
8589 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
8590 and then
8591 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
8593 -- An explicit dereference is side effect free only if it is
8594 -- a side effect free prefixed reference.
8596 when N_Explicit_Dereference =>
8597 return Safe_Prefixed_Reference (N);
8599 -- An expression with action is side effect free if its expression
8600 -- is side effect free and it has no actions.
8602 when N_Expression_With_Actions =>
8603 return Is_Empty_List (Actions (N))
8604 and then
8605 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8607 -- A call to _rep_to_pos is side effect free, since we generate
8608 -- this pure function call ourselves. Moreover it is critically
8609 -- important to make this exception, since otherwise we can have
8610 -- discriminants in array components which don't look side effect
8611 -- free in the case of an array whose index type is an enumeration
8612 -- type with an enumeration rep clause.
8614 -- All other function calls are not side effect free
8616 when N_Function_Call =>
8617 return Nkind (Name (N)) = N_Identifier
8618 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
8619 and then
8620 Side_Effect_Free
8621 (First (Parameter_Associations (N)), Name_Req, Variable_Ref);
8623 -- An IF expression is side effect free if it's of a scalar type, and
8624 -- all its components are all side effect free (conditions and then
8625 -- actions and else actions). We restrict to scalar types, since it
8626 -- is annoying to deal with things like (if A then B else C)'First
8627 -- where the type involved is a string type.
8629 when N_If_Expression =>
8630 return Is_Scalar_Type (Typ)
8631 and then
8632 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref);
8634 -- An indexed component is side effect free if it is a side
8635 -- effect free prefixed reference and all the indexing
8636 -- expressions are side effect free.
8638 when N_Indexed_Component =>
8639 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
8640 and then Safe_Prefixed_Reference (N);
8642 -- A type qualification is side effect free if the expression
8643 -- is side effect free.
8645 when N_Qualified_Expression =>
8646 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8648 -- A selected component is side effect free only if it is a side
8649 -- effect free prefixed reference. If it designates a component
8650 -- with a rep. clause it must be treated has having a potential
8651 -- side effect, because it may be modified through a renaming, and
8652 -- a subsequent use of the renaming as a macro will yield the
8653 -- wrong value. This complex interaction between renaming and
8654 -- removing side effects is a reminder that the latter has become
8655 -- a headache to maintain, and that it should be removed in favor
8656 -- of the gcc mechanism to capture values ???
8658 when N_Selected_Component =>
8659 if Nkind (Parent (N)) = N_Explicit_Dereference
8660 and then Has_Non_Standard_Rep (Designated_Type (Typ))
8661 then
8662 return False;
8663 else
8664 return Safe_Prefixed_Reference (N);
8665 end if;
8667 -- A range is side effect free if the bounds are side effect free
8669 when N_Range =>
8670 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
8671 and then
8672 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
8674 -- A slice is side effect free if it is a side effect free
8675 -- prefixed reference and the bounds are side effect free.
8677 when N_Slice =>
8678 return Side_Effect_Free
8679 (Discrete_Range (N), Name_Req, Variable_Ref)
8680 and then Safe_Prefixed_Reference (N);
8682 -- A type conversion is side effect free if the expression to be
8683 -- converted is side effect free.
8685 when N_Type_Conversion =>
8686 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8688 -- A unary operator is side effect free if the operand
8689 -- is side effect free.
8691 when N_Unary_Op =>
8692 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
8694 -- An unchecked type conversion is side effect free only if it
8695 -- is safe and its argument is side effect free.
8697 when N_Unchecked_Type_Conversion =>
8698 return Safe_Unchecked_Type_Conversion (N)
8699 and then
8700 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8702 -- An unchecked expression is side effect free if its expression
8703 -- is side effect free.
8705 when N_Unchecked_Expression =>
8706 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8708 -- A literal is side effect free
8710 when N_Character_Literal |
8711 N_Integer_Literal |
8712 N_Real_Literal |
8713 N_String_Literal =>
8714 return True;
8716 -- We consider that anything else has side effects. This is a bit
8717 -- crude, but we are pretty close for most common cases, and we
8718 -- are certainly correct (i.e. we never return True when the
8719 -- answer should be False).
8721 when others =>
8722 return False;
8723 end case;
8724 end Side_Effect_Free;
8726 -- A list is side effect free if all elements of the list are side
8727 -- effect free.
8729 function Side_Effect_Free
8730 (L : List_Id;
8731 Name_Req : Boolean := False;
8732 Variable_Ref : Boolean := False) return Boolean
8734 N : Node_Id;
8736 begin
8737 if L = No_List or else L = Error_List then
8738 return True;
8740 else
8741 N := First (L);
8742 while Present (N) loop
8743 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
8744 return False;
8745 else
8746 Next (N);
8747 end if;
8748 end loop;
8750 return True;
8751 end if;
8752 end Side_Effect_Free;
8754 ----------------------------------
8755 -- Silly_Boolean_Array_Not_Test --
8756 ----------------------------------
8758 -- This procedure implements an odd and silly test. We explicitly check
8759 -- for the case where the 'First of the component type is equal to the
8760 -- 'Last of this component type, and if this is the case, we make sure
8761 -- that constraint error is raised. The reason is that the NOT is bound
8762 -- to cause CE in this case, and we will not otherwise catch it.
8764 -- No such check is required for AND and OR, since for both these cases
8765 -- False op False = False, and True op True = True. For the XOR case,
8766 -- see Silly_Boolean_Array_Xor_Test.
8768 -- Believe it or not, this was reported as a bug. Note that nearly always,
8769 -- the test will evaluate statically to False, so the code will be
8770 -- statically removed, and no extra overhead caused.
8772 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
8773 Loc : constant Source_Ptr := Sloc (N);
8774 CT : constant Entity_Id := Component_Type (T);
8776 begin
8777 -- The check we install is
8779 -- constraint_error when
8780 -- component_type'first = component_type'last
8781 -- and then array_type'Length /= 0)
8783 -- We need the last guard because we don't want to raise CE for empty
8784 -- arrays since no out of range values result. (Empty arrays with a
8785 -- component type of True .. True -- very useful -- even the ACATS
8786 -- does not test that marginal case).
8788 Insert_Action (N,
8789 Make_Raise_Constraint_Error (Loc,
8790 Condition =>
8791 Make_And_Then (Loc,
8792 Left_Opnd =>
8793 Make_Op_Eq (Loc,
8794 Left_Opnd =>
8795 Make_Attribute_Reference (Loc,
8796 Prefix => New_Occurrence_Of (CT, Loc),
8797 Attribute_Name => Name_First),
8799 Right_Opnd =>
8800 Make_Attribute_Reference (Loc,
8801 Prefix => New_Occurrence_Of (CT, Loc),
8802 Attribute_Name => Name_Last)),
8804 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
8805 Reason => CE_Range_Check_Failed));
8806 end Silly_Boolean_Array_Not_Test;
8808 ----------------------------------
8809 -- Silly_Boolean_Array_Xor_Test --
8810 ----------------------------------
8812 -- This procedure implements an odd and silly test. We explicitly check
8813 -- for the XOR case where the component type is True .. True, since this
8814 -- will raise constraint error. A special check is required since CE
8815 -- will not be generated otherwise (cf Expand_Packed_Not).
8817 -- No such check is required for AND and OR, since for both these cases
8818 -- False op False = False, and True op True = True, and no check is
8819 -- required for the case of False .. False, since False xor False = False.
8820 -- See also Silly_Boolean_Array_Not_Test
8822 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
8823 Loc : constant Source_Ptr := Sloc (N);
8824 CT : constant Entity_Id := Component_Type (T);
8826 begin
8827 -- The check we install is
8829 -- constraint_error when
8830 -- Boolean (component_type'First)
8831 -- and then Boolean (component_type'Last)
8832 -- and then array_type'Length /= 0)
8834 -- We need the last guard because we don't want to raise CE for empty
8835 -- arrays since no out of range values result (Empty arrays with a
8836 -- component type of True .. True -- very useful -- even the ACATS
8837 -- does not test that marginal case).
8839 Insert_Action (N,
8840 Make_Raise_Constraint_Error (Loc,
8841 Condition =>
8842 Make_And_Then (Loc,
8843 Left_Opnd =>
8844 Make_And_Then (Loc,
8845 Left_Opnd =>
8846 Convert_To (Standard_Boolean,
8847 Make_Attribute_Reference (Loc,
8848 Prefix => New_Occurrence_Of (CT, Loc),
8849 Attribute_Name => Name_First)),
8851 Right_Opnd =>
8852 Convert_To (Standard_Boolean,
8853 Make_Attribute_Reference (Loc,
8854 Prefix => New_Occurrence_Of (CT, Loc),
8855 Attribute_Name => Name_Last))),
8857 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
8858 Reason => CE_Range_Check_Failed));
8859 end Silly_Boolean_Array_Xor_Test;
8861 --------------------------
8862 -- Target_Has_Fixed_Ops --
8863 --------------------------
8865 Integer_Sized_Small : Ureal;
8866 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
8867 -- called (we don't want to compute it more than once).
8869 Long_Integer_Sized_Small : Ureal;
8870 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
8871 -- is called (we don't want to compute it more than once)
8873 First_Time_For_THFO : Boolean := True;
8874 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
8876 function Target_Has_Fixed_Ops
8877 (Left_Typ : Entity_Id;
8878 Right_Typ : Entity_Id;
8879 Result_Typ : Entity_Id) return Boolean
8881 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
8882 -- Return True if the given type is a fixed-point type with a small
8883 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
8884 -- an absolute value less than 1.0. This is currently limited to
8885 -- fixed-point types that map to Integer or Long_Integer.
8887 ------------------------
8888 -- Is_Fractional_Type --
8889 ------------------------
8891 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
8892 begin
8893 if Esize (Typ) = Standard_Integer_Size then
8894 return Small_Value (Typ) = Integer_Sized_Small;
8896 elsif Esize (Typ) = Standard_Long_Integer_Size then
8897 return Small_Value (Typ) = Long_Integer_Sized_Small;
8899 else
8900 return False;
8901 end if;
8902 end Is_Fractional_Type;
8904 -- Start of processing for Target_Has_Fixed_Ops
8906 begin
8907 -- Return False if Fractional_Fixed_Ops_On_Target is false
8909 if not Fractional_Fixed_Ops_On_Target then
8910 return False;
8911 end if;
8913 -- Here the target has Fractional_Fixed_Ops, if first time, compute
8914 -- standard constants used by Is_Fractional_Type.
8916 if First_Time_For_THFO then
8917 First_Time_For_THFO := False;
8919 Integer_Sized_Small :=
8920 UR_From_Components
8921 (Num => Uint_1,
8922 Den => UI_From_Int (Standard_Integer_Size - 1),
8923 Rbase => 2);
8925 Long_Integer_Sized_Small :=
8926 UR_From_Components
8927 (Num => Uint_1,
8928 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
8929 Rbase => 2);
8930 end if;
8932 -- Return True if target supports fixed-by-fixed multiply/divide for
8933 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
8934 -- and result types are equivalent fractional types.
8936 return Is_Fractional_Type (Base_Type (Left_Typ))
8937 and then Is_Fractional_Type (Base_Type (Right_Typ))
8938 and then Is_Fractional_Type (Base_Type (Result_Typ))
8939 and then Esize (Left_Typ) = Esize (Right_Typ)
8940 and then Esize (Left_Typ) = Esize (Result_Typ);
8941 end Target_Has_Fixed_Ops;
8943 ------------------------------------------
8944 -- Type_May_Have_Bit_Aligned_Components --
8945 ------------------------------------------
8947 function Type_May_Have_Bit_Aligned_Components
8948 (Typ : Entity_Id) return Boolean
8950 begin
8951 -- Array type, check component type
8953 if Is_Array_Type (Typ) then
8954 return
8955 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
8957 -- Record type, check components
8959 elsif Is_Record_Type (Typ) then
8960 declare
8961 E : Entity_Id;
8963 begin
8964 E := First_Component_Or_Discriminant (Typ);
8965 while Present (E) loop
8966 if Component_May_Be_Bit_Aligned (E)
8967 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
8968 then
8969 return True;
8970 end if;
8972 Next_Component_Or_Discriminant (E);
8973 end loop;
8975 return False;
8976 end;
8978 -- Type other than array or record is always OK
8980 else
8981 return False;
8982 end if;
8983 end Type_May_Have_Bit_Aligned_Components;
8985 ----------------------------------
8986 -- Within_Case_Or_If_Expression --
8987 ----------------------------------
8989 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
8990 Par : Node_Id;
8992 begin
8993 -- Locate an enclosing case or if expression. Note that these constructs
8994 -- can be expanded into Expression_With_Actions, hence the test of the
8995 -- original node.
8997 Par := Parent (N);
8998 while Present (Par) loop
8999 if Nkind_In (Original_Node (Par), N_Case_Expression,
9000 N_If_Expression)
9001 then
9002 return True;
9004 -- Prevent the search from going too far
9006 elsif Is_Body_Or_Package_Declaration (Par) then
9007 return False;
9008 end if;
9010 Par := Parent (Par);
9011 end loop;
9013 return False;
9014 end Within_Case_Or_If_Expression;
9016 --------------------------------
9017 -- Within_Internal_Subprogram --
9018 --------------------------------
9020 function Within_Internal_Subprogram return Boolean is
9021 S : Entity_Id;
9023 begin
9024 S := Current_Scope;
9025 while Present (S) and then not Is_Subprogram (S) loop
9026 S := Scope (S);
9027 end loop;
9029 return Present (S)
9030 and then Get_TSS_Name (S) /= TSS_Null
9031 and then not Is_Predicate_Function (S);
9032 end Within_Internal_Subprogram;
9034 ----------------------------
9035 -- Wrap_Cleanup_Procedure --
9036 ----------------------------
9038 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
9039 Loc : constant Source_Ptr := Sloc (N);
9040 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
9041 Stmts : constant List_Id := Statements (Stseq);
9042 begin
9043 if Abort_Allowed then
9044 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
9045 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
9046 end if;
9047 end Wrap_Cleanup_Procedure;
9049 end Exp_Util;