Concretize three gimple_return_ accessors
[official-gcc.git] / gcc / ada / exp_util.adb
blobd7f200f3b07538d19f24478e56019f5da721b9de
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 -- Containing_Package_With_Ext_Axioms --
1747 ----------------------------------------
1749 function Containing_Package_With_Ext_Axioms
1750 (E : Entity_Id) return Entity_Id
1752 Decl : Node_Id;
1754 begin
1755 if Ekind (E) = E_Package then
1756 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
1757 Decl := Parent (Parent (E));
1758 else
1759 Decl := Parent (E);
1760 end if;
1761 end if;
1763 -- E is the package or generic package which is externally axiomatized
1765 if Ekind_In (E, E_Package, E_Generic_Package)
1766 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
1767 then
1768 return E;
1769 end if;
1771 -- If E's scope is axiomatized, E is axiomatized.
1773 declare
1774 First_Ax_Parent_Scope : Entity_Id := Empty;
1776 begin
1777 if Present (Scope (E)) then
1778 First_Ax_Parent_Scope :=
1779 Containing_Package_With_Ext_Axioms (Scope (E));
1780 end if;
1782 if Present (First_Ax_Parent_Scope) then
1783 return First_Ax_Parent_Scope;
1784 end if;
1786 -- otherwise, if E is a package instance, it is axiomatized if the
1787 -- corresponding generic package is axiomatized.
1789 if Ekind (E) = E_Package
1790 and then Present (Generic_Parent (Decl))
1791 then
1792 return
1793 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
1794 else
1795 return Empty;
1796 end if;
1797 end;
1798 end Containing_Package_With_Ext_Axioms;
1800 -------------------------------
1801 -- Convert_To_Actual_Subtype --
1802 -------------------------------
1804 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1805 Act_ST : Entity_Id;
1807 begin
1808 Act_ST := Get_Actual_Subtype (Exp);
1810 if Act_ST = Etype (Exp) then
1811 return;
1812 else
1813 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
1814 Analyze_And_Resolve (Exp, Act_ST);
1815 end if;
1816 end Convert_To_Actual_Subtype;
1818 -----------------------------------
1819 -- Corresponding_Runtime_Package --
1820 -----------------------------------
1822 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
1823 Pkg_Id : RTU_Id := RTU_Null;
1825 begin
1826 pragma Assert (Is_Concurrent_Type (Typ));
1828 if Ekind (Typ) in Protected_Kind then
1829 if Has_Entries (Typ)
1831 -- A protected type without entries that covers an interface and
1832 -- overrides the abstract routines with protected procedures is
1833 -- considered equivalent to a protected type with entries in the
1834 -- context of dispatching select statements. It is sufficient to
1835 -- check for the presence of an interface list in the declaration
1836 -- node to recognize this case.
1838 or else Present (Interface_List (Parent (Typ)))
1840 -- Protected types with interrupt handlers (when not using a
1841 -- restricted profile) are also considered equivalent to
1842 -- protected types with entries. The types which are used
1843 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
1844 -- are derived from Protection_Entries.
1846 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
1847 or else Has_Interrupt_Handler (Typ)
1848 then
1849 if Abort_Allowed
1850 or else Restriction_Active (No_Entry_Queue) = False
1851 or else Restriction_Active (No_Select_Statements) = False
1852 or else Number_Entries (Typ) > 1
1853 or else (Has_Attach_Handler (Typ)
1854 and then not Restricted_Profile)
1855 then
1856 Pkg_Id := System_Tasking_Protected_Objects_Entries;
1857 else
1858 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
1859 end if;
1861 else
1862 Pkg_Id := System_Tasking_Protected_Objects;
1863 end if;
1864 end if;
1866 return Pkg_Id;
1867 end Corresponding_Runtime_Package;
1869 -----------------------------------
1870 -- Current_Sem_Unit_Declarations --
1871 -----------------------------------
1873 function Current_Sem_Unit_Declarations return List_Id is
1874 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
1875 Decls : List_Id;
1877 begin
1878 -- If the current unit is a package body, locate the visible
1879 -- declarations of the package spec.
1881 if Nkind (U) = N_Package_Body then
1882 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1883 end if;
1885 if Nkind (U) = N_Package_Declaration then
1886 U := Specification (U);
1887 Decls := Visible_Declarations (U);
1889 if No (Decls) then
1890 Decls := New_List;
1891 Set_Visible_Declarations (U, Decls);
1892 end if;
1894 else
1895 Decls := Declarations (U);
1897 if No (Decls) then
1898 Decls := New_List;
1899 Set_Declarations (U, Decls);
1900 end if;
1901 end if;
1903 return Decls;
1904 end Current_Sem_Unit_Declarations;
1906 -----------------------
1907 -- Duplicate_Subexpr --
1908 -----------------------
1910 function Duplicate_Subexpr
1911 (Exp : Node_Id;
1912 Name_Req : Boolean := False;
1913 Renaming_Req : Boolean := False) return Node_Id
1915 begin
1916 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1917 return New_Copy_Tree (Exp);
1918 end Duplicate_Subexpr;
1920 ---------------------------------
1921 -- Duplicate_Subexpr_No_Checks --
1922 ---------------------------------
1924 function Duplicate_Subexpr_No_Checks
1925 (Exp : Node_Id;
1926 Name_Req : Boolean := False;
1927 Renaming_Req : Boolean := False) return Node_Id
1929 New_Exp : Node_Id;
1931 begin
1932 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1933 New_Exp := New_Copy_Tree (Exp);
1934 Remove_Checks (New_Exp);
1935 return New_Exp;
1936 end Duplicate_Subexpr_No_Checks;
1938 -----------------------------------
1939 -- Duplicate_Subexpr_Move_Checks --
1940 -----------------------------------
1942 function Duplicate_Subexpr_Move_Checks
1943 (Exp : Node_Id;
1944 Name_Req : Boolean := False;
1945 Renaming_Req : Boolean := False) return Node_Id
1947 New_Exp : Node_Id;
1949 begin
1950 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
1951 New_Exp := New_Copy_Tree (Exp);
1952 Remove_Checks (Exp);
1953 return New_Exp;
1954 end Duplicate_Subexpr_Move_Checks;
1956 --------------------
1957 -- Ensure_Defined --
1958 --------------------
1960 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1961 IR : Node_Id;
1963 begin
1964 -- An itype reference must only be created if this is a local itype, so
1965 -- that gigi can elaborate it on the proper objstack.
1967 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
1968 IR := Make_Itype_Reference (Sloc (N));
1969 Set_Itype (IR, Typ);
1970 Insert_Action (N, IR);
1971 end if;
1972 end Ensure_Defined;
1974 --------------------
1975 -- Entry_Names_OK --
1976 --------------------
1978 function Entry_Names_OK return Boolean is
1979 begin
1980 return
1981 not Restricted_Profile
1982 and then not Global_Discard_Names
1983 and then not Restriction_Active (No_Implicit_Heap_Allocations)
1984 and then not Restriction_Active (No_Local_Allocators);
1985 end Entry_Names_OK;
1987 -------------------
1988 -- Evaluate_Name --
1989 -------------------
1991 procedure Evaluate_Name (Nam : Node_Id) is
1992 K : constant Node_Kind := Nkind (Nam);
1994 begin
1995 -- For an explicit dereference, we simply force the evaluation of the
1996 -- name expression. The dereference provides a value that is the address
1997 -- for the renamed object, and it is precisely this value that we want
1998 -- to preserve.
2000 if K = N_Explicit_Dereference then
2001 Force_Evaluation (Prefix (Nam));
2003 -- For a selected component, we simply evaluate the prefix
2005 elsif K = N_Selected_Component then
2006 Evaluate_Name (Prefix (Nam));
2008 -- For an indexed component, or an attribute reference, we evaluate the
2009 -- prefix, which is itself a name, recursively, and then force the
2010 -- evaluation of all the subscripts (or attribute expressions).
2012 elsif Nkind_In (K, N_Indexed_Component, N_Attribute_Reference) then
2013 Evaluate_Name (Prefix (Nam));
2015 declare
2016 E : Node_Id;
2018 begin
2019 E := First (Expressions (Nam));
2020 while Present (E) loop
2021 Force_Evaluation (E);
2023 if Original_Node (E) /= E then
2024 Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
2025 end if;
2027 Next (E);
2028 end loop;
2029 end;
2031 -- For a slice, we evaluate the prefix, as for the indexed component
2032 -- case and then, if there is a range present, either directly or as the
2033 -- constraint of a discrete subtype indication, we evaluate the two
2034 -- bounds of this range.
2036 elsif K = N_Slice then
2037 Evaluate_Name (Prefix (Nam));
2038 Evaluate_Slice_Bounds (Nam);
2040 -- For a type conversion, the expression of the conversion must be the
2041 -- name of an object, and we simply need to evaluate this name.
2043 elsif K = N_Type_Conversion then
2044 Evaluate_Name (Expression (Nam));
2046 -- For a function call, we evaluate the call
2048 elsif K = N_Function_Call then
2049 Force_Evaluation (Nam);
2051 -- The remaining cases are direct name, operator symbol and character
2052 -- literal. In all these cases, we do nothing, since we want to
2053 -- reevaluate each time the renamed object is used.
2055 else
2056 return;
2057 end if;
2058 end Evaluate_Name;
2060 ---------------------------
2061 -- Evaluate_Slice_Bounds --
2062 ---------------------------
2064 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
2065 DR : constant Node_Id := Discrete_Range (Slice);
2066 Constr : Node_Id;
2067 Rexpr : Node_Id;
2069 begin
2070 if Nkind (DR) = N_Range then
2071 Force_Evaluation (Low_Bound (DR));
2072 Force_Evaluation (High_Bound (DR));
2074 elsif Nkind (DR) = N_Subtype_Indication then
2075 Constr := Constraint (DR);
2077 if Nkind (Constr) = N_Range_Constraint then
2078 Rexpr := Range_Expression (Constr);
2080 Force_Evaluation (Low_Bound (Rexpr));
2081 Force_Evaluation (High_Bound (Rexpr));
2082 end if;
2083 end if;
2084 end Evaluate_Slice_Bounds;
2086 ---------------------
2087 -- Evolve_And_Then --
2088 ---------------------
2090 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
2091 begin
2092 if No (Cond) then
2093 Cond := Cond1;
2094 else
2095 Cond :=
2096 Make_And_Then (Sloc (Cond1),
2097 Left_Opnd => Cond,
2098 Right_Opnd => Cond1);
2099 end if;
2100 end Evolve_And_Then;
2102 --------------------
2103 -- Evolve_Or_Else --
2104 --------------------
2106 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
2107 begin
2108 if No (Cond) then
2109 Cond := Cond1;
2110 else
2111 Cond :=
2112 Make_Or_Else (Sloc (Cond1),
2113 Left_Opnd => Cond,
2114 Right_Opnd => Cond1);
2115 end if;
2116 end Evolve_Or_Else;
2118 -----------------------------------------
2119 -- Expand_Static_Predicates_In_Choices --
2120 -----------------------------------------
2122 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
2123 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
2125 Choices : constant List_Id := Discrete_Choices (N);
2127 Choice : Node_Id;
2128 Next_C : Node_Id;
2129 P : Node_Id;
2130 C : Node_Id;
2132 begin
2133 Choice := First (Choices);
2134 while Present (Choice) loop
2135 Next_C := Next (Choice);
2137 -- Check for name of subtype with static predicate
2139 if Is_Entity_Name (Choice)
2140 and then Is_Type (Entity (Choice))
2141 and then Has_Predicates (Entity (Choice))
2142 then
2143 -- Loop through entries in predicate list, converting to choices
2144 -- and inserting in the list before the current choice. Note that
2145 -- if the list is empty, corresponding to a False predicate, then
2146 -- no choices are inserted.
2148 P := First (Static_Discrete_Predicate (Entity (Choice)));
2149 while Present (P) loop
2151 -- If low bound and high bounds are equal, copy simple choice
2153 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
2154 C := New_Copy (Low_Bound (P));
2156 -- Otherwise copy a range
2158 else
2159 C := New_Copy (P);
2160 end if;
2162 -- Change Sloc to referencing choice (rather than the Sloc of
2163 -- the predicate declaration element itself).
2165 Set_Sloc (C, Sloc (Choice));
2166 Insert_Before (Choice, C);
2167 Next (P);
2168 end loop;
2170 -- Delete the predicated entry
2172 Remove (Choice);
2173 end if;
2175 -- Move to next choice to check
2177 Choice := Next_C;
2178 end loop;
2179 end Expand_Static_Predicates_In_Choices;
2181 ------------------------------
2182 -- Expand_Subtype_From_Expr --
2183 ------------------------------
2185 -- This function is applicable for both static and dynamic allocation of
2186 -- objects which are constrained by an initial expression. Basically it
2187 -- transforms an unconstrained subtype indication into a constrained one.
2189 -- The expression may also be transformed in certain cases in order to
2190 -- avoid multiple evaluation. In the static allocation case, the general
2191 -- scheme is:
2193 -- Val : T := Expr;
2195 -- is transformed into
2197 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
2199 -- Here are the main cases :
2201 -- <if Expr is a Slice>
2202 -- Val : T ([Index_Subtype (Expr)]) := Expr;
2204 -- <elsif Expr is a String Literal>
2205 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
2207 -- <elsif Expr is Constrained>
2208 -- subtype T is Type_Of_Expr
2209 -- Val : T := Expr;
2211 -- <elsif Expr is an entity_name>
2212 -- Val : T (constraints taken from Expr) := Expr;
2214 -- <else>
2215 -- type Axxx is access all T;
2216 -- Rval : Axxx := Expr'ref;
2217 -- Val : T (constraints taken from Rval) := Rval.all;
2219 -- ??? note: when the Expression is allocated in the secondary stack
2220 -- we could use it directly instead of copying it by declaring
2221 -- Val : T (...) renames Rval.all
2223 procedure Expand_Subtype_From_Expr
2224 (N : Node_Id;
2225 Unc_Type : Entity_Id;
2226 Subtype_Indic : Node_Id;
2227 Exp : Node_Id)
2229 Loc : constant Source_Ptr := Sloc (N);
2230 Exp_Typ : constant Entity_Id := Etype (Exp);
2231 T : Entity_Id;
2233 begin
2234 -- In general we cannot build the subtype if expansion is disabled,
2235 -- because internal entities may not have been defined. However, to
2236 -- avoid some cascaded errors, we try to continue when the expression is
2237 -- an array (or string), because it is safe to compute the bounds. It is
2238 -- in fact required to do so even in a generic context, because there
2239 -- may be constants that depend on the bounds of a string literal, both
2240 -- standard string types and more generally arrays of characters.
2242 -- In GNATprove mode, these extra subtypes are not needed
2244 if GNATprove_Mode then
2245 return;
2246 end if;
2248 if not Expander_Active
2249 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
2250 then
2251 return;
2252 end if;
2254 if Nkind (Exp) = N_Slice then
2255 declare
2256 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
2258 begin
2259 Rewrite (Subtype_Indic,
2260 Make_Subtype_Indication (Loc,
2261 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
2262 Constraint =>
2263 Make_Index_Or_Discriminant_Constraint (Loc,
2264 Constraints => New_List
2265 (New_Occurrence_Of (Slice_Type, Loc)))));
2267 -- This subtype indication may be used later for constraint checks
2268 -- we better make sure that if a variable was used as a bound of
2269 -- of the original slice, its value is frozen.
2271 Evaluate_Slice_Bounds (Exp);
2272 end;
2274 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
2275 Rewrite (Subtype_Indic,
2276 Make_Subtype_Indication (Loc,
2277 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
2278 Constraint =>
2279 Make_Index_Or_Discriminant_Constraint (Loc,
2280 Constraints => New_List (
2281 Make_Literal_Range (Loc,
2282 Literal_Typ => Exp_Typ)))));
2284 -- If the type of the expression is an internally generated type it
2285 -- may not be necessary to create a new subtype. However there are two
2286 -- exceptions: references to the current instances, and aliased array
2287 -- object declarations for which the backend needs to create a template.
2289 elsif Is_Constrained (Exp_Typ)
2290 and then not Is_Class_Wide_Type (Unc_Type)
2291 and then
2292 (Nkind (N) /= N_Object_Declaration
2293 or else not Is_Entity_Name (Expression (N))
2294 or else not Comes_From_Source (Entity (Expression (N)))
2295 or else not Is_Array_Type (Exp_Typ)
2296 or else not Aliased_Present (N))
2297 then
2298 if Is_Itype (Exp_Typ) then
2300 -- Within an initialization procedure, a selected component
2301 -- denotes a component of the enclosing record, and it appears as
2302 -- an actual in a call to its own initialization procedure. If
2303 -- this component depends on the outer discriminant, we must
2304 -- generate the proper actual subtype for it.
2306 if Nkind (Exp) = N_Selected_Component
2307 and then Within_Init_Proc
2308 then
2309 declare
2310 Decl : constant Node_Id :=
2311 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
2312 begin
2313 if Present (Decl) then
2314 Insert_Action (N, Decl);
2315 T := Defining_Identifier (Decl);
2316 else
2317 T := Exp_Typ;
2318 end if;
2319 end;
2321 -- No need to generate a new subtype
2323 else
2324 T := Exp_Typ;
2325 end if;
2327 else
2328 T := Make_Temporary (Loc, 'T');
2330 Insert_Action (N,
2331 Make_Subtype_Declaration (Loc,
2332 Defining_Identifier => T,
2333 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
2335 -- This type is marked as an itype even though it has an explicit
2336 -- declaration since otherwise Is_Generic_Actual_Type can get
2337 -- set, resulting in the generation of spurious errors. (See
2338 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
2340 Set_Is_Itype (T);
2341 Set_Associated_Node_For_Itype (T, Exp);
2342 end if;
2344 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
2346 -- Nothing needs to be done for private types with unknown discriminants
2347 -- if the underlying type is not an unconstrained composite type or it
2348 -- is an unchecked union.
2350 elsif Is_Private_Type (Unc_Type)
2351 and then Has_Unknown_Discriminants (Unc_Type)
2352 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
2353 or else Is_Constrained (Underlying_Type (Unc_Type))
2354 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
2355 then
2356 null;
2358 -- Case of derived type with unknown discriminants where the parent type
2359 -- also has unknown discriminants.
2361 elsif Is_Record_Type (Unc_Type)
2362 and then not Is_Class_Wide_Type (Unc_Type)
2363 and then Has_Unknown_Discriminants (Unc_Type)
2364 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
2365 then
2366 -- Nothing to be done if no underlying record view available
2368 if No (Underlying_Record_View (Unc_Type)) then
2369 null;
2371 -- Otherwise use the Underlying_Record_View to create the proper
2372 -- constrained subtype for an object of a derived type with unknown
2373 -- discriminants.
2375 else
2376 Remove_Side_Effects (Exp);
2377 Rewrite (Subtype_Indic,
2378 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
2379 end if;
2381 -- Renamings of class-wide interface types require no equivalent
2382 -- constrained type declarations because we only need to reference
2383 -- the tag component associated with the interface. The same is
2384 -- presumably true for class-wide types in general, so this test
2385 -- is broadened to include all class-wide renamings, which also
2386 -- avoids cases of unbounded recursion in Remove_Side_Effects.
2387 -- (Is this really correct, or are there some cases of class-wide
2388 -- renamings that require action in this procedure???)
2390 elsif Present (N)
2391 and then Nkind (N) = N_Object_Renaming_Declaration
2392 and then Is_Class_Wide_Type (Unc_Type)
2393 then
2394 null;
2396 -- In Ada 95 nothing to be done if the type of the expression is limited
2397 -- because in this case the expression cannot be copied, and its use can
2398 -- only be by reference.
2400 -- In Ada 2005 the context can be an object declaration whose expression
2401 -- is a function that returns in place. If the nominal subtype has
2402 -- unknown discriminants, the call still provides constraints on the
2403 -- object, and we have to create an actual subtype from it.
2405 -- If the type is class-wide, the expression is dynamically tagged and
2406 -- we do not create an actual subtype either. Ditto for an interface.
2407 -- For now this applies only if the type is immutably limited, and the
2408 -- function being called is build-in-place. This will have to be revised
2409 -- when build-in-place functions are generalized to other types.
2411 elsif Is_Limited_View (Exp_Typ)
2412 and then
2413 (Is_Class_Wide_Type (Exp_Typ)
2414 or else Is_Interface (Exp_Typ)
2415 or else not Has_Unknown_Discriminants (Exp_Typ)
2416 or else not Is_Composite_Type (Unc_Type))
2417 then
2418 null;
2420 -- For limited objects initialized with build in place function calls,
2421 -- nothing to be done; otherwise we prematurely introduce an N_Reference
2422 -- node in the expression initializing the object, which breaks the
2423 -- circuitry that detects and adds the additional arguments to the
2424 -- called function.
2426 elsif Is_Build_In_Place_Function_Call (Exp) then
2427 null;
2429 else
2430 Remove_Side_Effects (Exp);
2431 Rewrite (Subtype_Indic,
2432 Make_Subtype_From_Expr (Exp, Unc_Type));
2433 end if;
2434 end Expand_Subtype_From_Expr;
2436 ------------------------
2437 -- Find_Interface_ADT --
2438 ------------------------
2440 function Find_Interface_ADT
2441 (T : Entity_Id;
2442 Iface : Entity_Id) return Elmt_Id
2444 ADT : Elmt_Id;
2445 Typ : Entity_Id := T;
2447 begin
2448 pragma Assert (Is_Interface (Iface));
2450 -- Handle private types
2452 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2453 Typ := Full_View (Typ);
2454 end if;
2456 -- Handle access types
2458 if Is_Access_Type (Typ) then
2459 Typ := Designated_Type (Typ);
2460 end if;
2462 -- Handle task and protected types implementing interfaces
2464 if Is_Concurrent_Type (Typ) then
2465 Typ := Corresponding_Record_Type (Typ);
2466 end if;
2468 pragma Assert
2469 (not Is_Class_Wide_Type (Typ)
2470 and then Ekind (Typ) /= E_Incomplete_Type);
2472 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2473 return First_Elmt (Access_Disp_Table (Typ));
2475 else
2476 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
2477 while Present (ADT)
2478 and then Present (Related_Type (Node (ADT)))
2479 and then Related_Type (Node (ADT)) /= Iface
2480 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
2481 Use_Full_View => True)
2482 loop
2483 Next_Elmt (ADT);
2484 end loop;
2486 pragma Assert (Present (Related_Type (Node (ADT))));
2487 return ADT;
2488 end if;
2489 end Find_Interface_ADT;
2491 ------------------------
2492 -- Find_Interface_Tag --
2493 ------------------------
2495 function Find_Interface_Tag
2496 (T : Entity_Id;
2497 Iface : Entity_Id) return Entity_Id
2499 AI_Tag : Entity_Id;
2500 Found : Boolean := False;
2501 Typ : Entity_Id := T;
2503 procedure Find_Tag (Typ : Entity_Id);
2504 -- Internal subprogram used to recursively climb to the ancestors
2506 --------------
2507 -- Find_Tag --
2508 --------------
2510 procedure Find_Tag (Typ : Entity_Id) is
2511 AI_Elmt : Elmt_Id;
2512 AI : Node_Id;
2514 begin
2515 -- This routine does not handle the case in which the interface is an
2516 -- ancestor of Typ. That case is handled by the enclosing subprogram.
2518 pragma Assert (Typ /= Iface);
2520 -- Climb to the root type handling private types
2522 if Present (Full_View (Etype (Typ))) then
2523 if Full_View (Etype (Typ)) /= Typ then
2524 Find_Tag (Full_View (Etype (Typ)));
2525 end if;
2527 elsif Etype (Typ) /= Typ then
2528 Find_Tag (Etype (Typ));
2529 end if;
2531 -- Traverse the list of interfaces implemented by the type
2533 if not Found
2534 and then Present (Interfaces (Typ))
2535 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
2536 then
2537 -- Skip the tag associated with the primary table
2539 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2540 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
2541 pragma Assert (Present (AI_Tag));
2543 AI_Elmt := First_Elmt (Interfaces (Typ));
2544 while Present (AI_Elmt) loop
2545 AI := Node (AI_Elmt);
2547 if AI = Iface
2548 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
2549 then
2550 Found := True;
2551 return;
2552 end if;
2554 AI_Tag := Next_Tag_Component (AI_Tag);
2555 Next_Elmt (AI_Elmt);
2556 end loop;
2557 end if;
2558 end Find_Tag;
2560 -- Start of processing for Find_Interface_Tag
2562 begin
2563 pragma Assert (Is_Interface (Iface));
2565 -- Handle access types
2567 if Is_Access_Type (Typ) then
2568 Typ := Designated_Type (Typ);
2569 end if;
2571 -- Handle class-wide types
2573 if Is_Class_Wide_Type (Typ) then
2574 Typ := Root_Type (Typ);
2575 end if;
2577 -- Handle private types
2579 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
2580 Typ := Full_View (Typ);
2581 end if;
2583 -- Handle entities from the limited view
2585 if Ekind (Typ) = E_Incomplete_Type then
2586 pragma Assert (Present (Non_Limited_View (Typ)));
2587 Typ := Non_Limited_View (Typ);
2588 end if;
2590 -- Handle task and protected types implementing interfaces
2592 if Is_Concurrent_Type (Typ) then
2593 Typ := Corresponding_Record_Type (Typ);
2594 end if;
2596 -- If the interface is an ancestor of the type, then it shared the
2597 -- primary dispatch table.
2599 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
2600 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
2601 return First_Tag_Component (Typ);
2603 -- Otherwise we need to search for its associated tag component
2605 else
2606 Find_Tag (Typ);
2607 pragma Assert (Found);
2608 return AI_Tag;
2609 end if;
2610 end Find_Interface_Tag;
2612 ------------------
2613 -- Find_Prim_Op --
2614 ------------------
2616 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id is
2617 Prim : Elmt_Id;
2618 Typ : Entity_Id := T;
2619 Op : Entity_Id;
2621 begin
2622 if Is_Class_Wide_Type (Typ) then
2623 Typ := Root_Type (Typ);
2624 end if;
2626 Typ := Underlying_Type (Typ);
2628 -- Loop through primitive operations
2630 Prim := First_Elmt (Primitive_Operations (Typ));
2631 while Present (Prim) loop
2632 Op := Node (Prim);
2634 -- We can retrieve primitive operations by name if it is an internal
2635 -- name. For equality we must check that both of its operands have
2636 -- the same type, to avoid confusion with user-defined equalities
2637 -- than may have a non-symmetric signature.
2639 exit when Chars (Op) = Name
2640 and then
2641 (Name /= Name_Op_Eq
2642 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
2644 Next_Elmt (Prim);
2646 -- Raise Program_Error if no primitive found
2648 if No (Prim) then
2649 raise Program_Error;
2650 end if;
2651 end loop;
2653 return Node (Prim);
2654 end Find_Prim_Op;
2656 ------------------
2657 -- Find_Prim_Op --
2658 ------------------
2660 function Find_Prim_Op
2661 (T : Entity_Id;
2662 Name : TSS_Name_Type) return Entity_Id
2664 Inher_Op : Entity_Id := Empty;
2665 Own_Op : Entity_Id := Empty;
2666 Prim_Elmt : Elmt_Id;
2667 Prim_Id : Entity_Id;
2668 Typ : Entity_Id := T;
2670 begin
2671 if Is_Class_Wide_Type (Typ) then
2672 Typ := Root_Type (Typ);
2673 end if;
2675 Typ := Underlying_Type (Typ);
2677 -- This search is based on the assertion that the dispatching version
2678 -- of the TSS routine always precedes the real primitive.
2680 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
2681 while Present (Prim_Elmt) loop
2682 Prim_Id := Node (Prim_Elmt);
2684 if Is_TSS (Prim_Id, Name) then
2685 if Present (Alias (Prim_Id)) then
2686 Inher_Op := Prim_Id;
2687 else
2688 Own_Op := Prim_Id;
2689 end if;
2690 end if;
2692 Next_Elmt (Prim_Elmt);
2693 end loop;
2695 if Present (Own_Op) then
2696 return Own_Op;
2697 elsif Present (Inher_Op) then
2698 return Inher_Op;
2699 else
2700 raise Program_Error;
2701 end if;
2702 end Find_Prim_Op;
2704 ----------------------------
2705 -- Find_Protection_Object --
2706 ----------------------------
2708 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
2709 S : Entity_Id;
2711 begin
2712 S := Scop;
2713 while Present (S) loop
2714 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
2715 and then Present (Protection_Object (S))
2716 then
2717 return Protection_Object (S);
2718 end if;
2720 S := Scope (S);
2721 end loop;
2723 -- If we do not find a Protection object in the scope chain, then
2724 -- something has gone wrong, most likely the object was never created.
2726 raise Program_Error;
2727 end Find_Protection_Object;
2729 --------------------------
2730 -- Find_Protection_Type --
2731 --------------------------
2733 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
2734 Comp : Entity_Id;
2735 Typ : Entity_Id := Conc_Typ;
2737 begin
2738 if Is_Concurrent_Type (Typ) then
2739 Typ := Corresponding_Record_Type (Typ);
2740 end if;
2742 -- Since restriction violations are not considered serious errors, the
2743 -- expander remains active, but may leave the corresponding record type
2744 -- malformed. In such cases, component _object is not available so do
2745 -- not look for it.
2747 if not Analyzed (Typ) then
2748 return Empty;
2749 end if;
2751 Comp := First_Component (Typ);
2752 while Present (Comp) loop
2753 if Chars (Comp) = Name_uObject then
2754 return Base_Type (Etype (Comp));
2755 end if;
2757 Next_Component (Comp);
2758 end loop;
2760 -- The corresponding record of a protected type should always have an
2761 -- _object field.
2763 raise Program_Error;
2764 end Find_Protection_Type;
2766 -----------------------
2767 -- Find_Hook_Context --
2768 -----------------------
2770 function Find_Hook_Context (N : Node_Id) return Node_Id is
2771 Par : Node_Id;
2772 Top : Node_Id;
2774 Wrapped_Node : Node_Id;
2775 -- Note: if we are in a transient scope, we want to reuse it as
2776 -- the context for actions insertion, if possible. But if N is itself
2777 -- part of the stored actions for the current transient scope,
2778 -- then we need to insert at the appropriate (inner) location in
2779 -- the not as an action on Node_To_Be_Wrapped.
2781 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
2783 begin
2784 -- When the node is inside a case/if expression, the lifetime of any
2785 -- temporary controlled object is extended. Find a suitable insertion
2786 -- node by locating the topmost case or if expressions.
2788 if In_Cond_Expr then
2789 Par := N;
2790 Top := N;
2791 while Present (Par) loop
2792 if Nkind_In (Original_Node (Par), N_Case_Expression,
2793 N_If_Expression)
2794 then
2795 Top := Par;
2797 -- Prevent the search from going too far
2799 elsif Is_Body_Or_Package_Declaration (Par) then
2800 exit;
2801 end if;
2803 Par := Parent (Par);
2804 end loop;
2806 -- The topmost case or if expression is now recovered, but it may
2807 -- still not be the correct place to add generated code. Climb to
2808 -- find a parent that is part of a declarative or statement list,
2809 -- and is not a list of actuals in a call.
2811 Par := Top;
2812 while Present (Par) loop
2813 if Is_List_Member (Par)
2814 and then not Nkind_In (Par, N_Component_Association,
2815 N_Discriminant_Association,
2816 N_Parameter_Association,
2817 N_Pragma_Argument_Association)
2818 and then not Nkind_In
2819 (Parent (Par), N_Function_Call,
2820 N_Procedure_Call_Statement,
2821 N_Entry_Call_Statement)
2823 then
2824 return Par;
2826 -- Prevent the search from going too far
2828 elsif Is_Body_Or_Package_Declaration (Par) then
2829 exit;
2830 end if;
2832 Par := Parent (Par);
2833 end loop;
2835 return Par;
2837 else
2838 Par := N;
2839 while Present (Par) loop
2841 -- Keep climbing past various operators
2843 if Nkind (Parent (Par)) in N_Op
2844 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
2845 then
2846 Par := Parent (Par);
2847 else
2848 exit;
2849 end if;
2850 end loop;
2852 Top := Par;
2854 -- The node may be located in a pragma in which case return the
2855 -- pragma itself:
2857 -- pragma Precondition (... and then Ctrl_Func_Call ...);
2859 -- Similar case occurs when the node is related to an object
2860 -- declaration or assignment:
2862 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
2864 -- Another case to consider is when the node is part of a return
2865 -- statement:
2867 -- return ... and then Ctrl_Func_Call ...;
2869 -- Another case is when the node acts as a formal in a procedure
2870 -- call statement:
2872 -- Proc (... and then Ctrl_Func_Call ...);
2874 if Scope_Is_Transient then
2875 Wrapped_Node := Node_To_Be_Wrapped;
2876 else
2877 Wrapped_Node := Empty;
2878 end if;
2880 while Present (Par) loop
2881 if Par = Wrapped_Node
2882 or else Nkind_In (Par, N_Assignment_Statement,
2883 N_Object_Declaration,
2884 N_Pragma,
2885 N_Procedure_Call_Statement,
2886 N_Simple_Return_Statement)
2887 then
2888 return Par;
2890 -- Prevent the search from going too far
2892 elsif Is_Body_Or_Package_Declaration (Par) then
2893 exit;
2894 end if;
2896 Par := Parent (Par);
2897 end loop;
2899 -- Return the topmost short circuit operator
2901 return Top;
2902 end if;
2903 end Find_Hook_Context;
2905 ----------------------
2906 -- Force_Evaluation --
2907 ----------------------
2909 procedure Force_Evaluation (Exp : Node_Id; Name_Req : Boolean := False) is
2910 begin
2911 Remove_Side_Effects (Exp, Name_Req, Variable_Ref => True);
2912 end Force_Evaluation;
2914 ---------------------------------
2915 -- Fully_Qualified_Name_String --
2916 ---------------------------------
2918 function Fully_Qualified_Name_String
2919 (E : Entity_Id;
2920 Append_NUL : Boolean := True) return String_Id
2922 procedure Internal_Full_Qualified_Name (E : Entity_Id);
2923 -- Compute recursively the qualified name without NUL at the end, adding
2924 -- it to the currently started string being generated
2926 ----------------------------------
2927 -- Internal_Full_Qualified_Name --
2928 ----------------------------------
2930 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
2931 Ent : Entity_Id;
2933 begin
2934 -- Deal properly with child units
2936 if Nkind (E) = N_Defining_Program_Unit_Name then
2937 Ent := Defining_Identifier (E);
2938 else
2939 Ent := E;
2940 end if;
2942 -- Compute qualification recursively (only "Standard" has no scope)
2944 if Present (Scope (Scope (Ent))) then
2945 Internal_Full_Qualified_Name (Scope (Ent));
2946 Store_String_Char (Get_Char_Code ('.'));
2947 end if;
2949 -- Every entity should have a name except some expanded blocks
2950 -- don't bother about those.
2952 if Chars (Ent) = No_Name then
2953 return;
2954 end if;
2956 -- Generates the entity name in upper case
2958 Get_Decoded_Name_String (Chars (Ent));
2959 Set_All_Upper_Case;
2960 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2961 return;
2962 end Internal_Full_Qualified_Name;
2964 -- Start of processing for Full_Qualified_Name
2966 begin
2967 Start_String;
2968 Internal_Full_Qualified_Name (E);
2970 if Append_NUL then
2971 Store_String_Char (Get_Char_Code (ASCII.NUL));
2972 end if;
2974 return End_String;
2975 end Fully_Qualified_Name_String;
2977 ------------------------
2978 -- Generate_Poll_Call --
2979 ------------------------
2981 procedure Generate_Poll_Call (N : Node_Id) is
2982 begin
2983 -- No poll call if polling not active
2985 if not Polling_Required then
2986 return;
2988 -- Otherwise generate require poll call
2990 else
2991 Insert_Before_And_Analyze (N,
2992 Make_Procedure_Call_Statement (Sloc (N),
2993 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
2994 end if;
2995 end Generate_Poll_Call;
2997 ---------------------------------
2998 -- Get_Current_Value_Condition --
2999 ---------------------------------
3001 -- Note: the implementation of this procedure is very closely tied to the
3002 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
3003 -- interpret Current_Value fields set by the Set procedure, so the two
3004 -- procedures need to be closely coordinated.
3006 procedure Get_Current_Value_Condition
3007 (Var : Node_Id;
3008 Op : out Node_Kind;
3009 Val : out Node_Id)
3011 Loc : constant Source_Ptr := Sloc (Var);
3012 Ent : constant Entity_Id := Entity (Var);
3014 procedure Process_Current_Value_Condition
3015 (N : Node_Id;
3016 S : Boolean);
3017 -- N is an expression which holds either True (S = True) or False (S =
3018 -- False) in the condition. This procedure digs out the expression and
3019 -- if it refers to Ent, sets Op and Val appropriately.
3021 -------------------------------------
3022 -- Process_Current_Value_Condition --
3023 -------------------------------------
3025 procedure Process_Current_Value_Condition
3026 (N : Node_Id;
3027 S : Boolean)
3029 Cond : Node_Id;
3030 Prev_Cond : Node_Id;
3031 Sens : Boolean;
3033 begin
3034 Cond := N;
3035 Sens := S;
3037 loop
3038 Prev_Cond := Cond;
3040 -- Deal with NOT operators, inverting sense
3042 while Nkind (Cond) = N_Op_Not loop
3043 Cond := Right_Opnd (Cond);
3044 Sens := not Sens;
3045 end loop;
3047 -- Deal with conversions, qualifications, and expressions with
3048 -- actions.
3050 while Nkind_In (Cond,
3051 N_Type_Conversion,
3052 N_Qualified_Expression,
3053 N_Expression_With_Actions)
3054 loop
3055 Cond := Expression (Cond);
3056 end loop;
3058 exit when Cond = Prev_Cond;
3059 end loop;
3061 -- Deal with AND THEN and AND cases
3063 if Nkind_In (Cond, N_And_Then, N_Op_And) then
3065 -- Don't ever try to invert a condition that is of the form of an
3066 -- AND or AND THEN (since we are not doing sufficiently general
3067 -- processing to allow this).
3069 if Sens = False then
3070 Op := N_Empty;
3071 Val := Empty;
3072 return;
3073 end if;
3075 -- Recursively process AND and AND THEN branches
3077 Process_Current_Value_Condition (Left_Opnd (Cond), True);
3079 if Op /= N_Empty then
3080 return;
3081 end if;
3083 Process_Current_Value_Condition (Right_Opnd (Cond), True);
3084 return;
3086 -- Case of relational operator
3088 elsif Nkind (Cond) in N_Op_Compare then
3089 Op := Nkind (Cond);
3091 -- Invert sense of test if inverted test
3093 if Sens = False then
3094 case Op is
3095 when N_Op_Eq => Op := N_Op_Ne;
3096 when N_Op_Ne => Op := N_Op_Eq;
3097 when N_Op_Lt => Op := N_Op_Ge;
3098 when N_Op_Gt => Op := N_Op_Le;
3099 when N_Op_Le => Op := N_Op_Gt;
3100 when N_Op_Ge => Op := N_Op_Lt;
3101 when others => raise Program_Error;
3102 end case;
3103 end if;
3105 -- Case of entity op value
3107 if Is_Entity_Name (Left_Opnd (Cond))
3108 and then Ent = Entity (Left_Opnd (Cond))
3109 and then Compile_Time_Known_Value (Right_Opnd (Cond))
3110 then
3111 Val := Right_Opnd (Cond);
3113 -- Case of value op entity
3115 elsif Is_Entity_Name (Right_Opnd (Cond))
3116 and then Ent = Entity (Right_Opnd (Cond))
3117 and then Compile_Time_Known_Value (Left_Opnd (Cond))
3118 then
3119 Val := Left_Opnd (Cond);
3121 -- We are effectively swapping operands
3123 case Op is
3124 when N_Op_Eq => null;
3125 when N_Op_Ne => null;
3126 when N_Op_Lt => Op := N_Op_Gt;
3127 when N_Op_Gt => Op := N_Op_Lt;
3128 when N_Op_Le => Op := N_Op_Ge;
3129 when N_Op_Ge => Op := N_Op_Le;
3130 when others => raise Program_Error;
3131 end case;
3133 else
3134 Op := N_Empty;
3135 end if;
3137 return;
3139 elsif Nkind_In (Cond,
3140 N_Type_Conversion,
3141 N_Qualified_Expression,
3142 N_Expression_With_Actions)
3143 then
3144 Cond := Expression (Cond);
3146 -- Case of Boolean variable reference, return as though the
3147 -- reference had said var = True.
3149 else
3150 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
3151 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
3153 if Sens = False then
3154 Op := N_Op_Ne;
3155 else
3156 Op := N_Op_Eq;
3157 end if;
3158 end if;
3159 end if;
3160 end Process_Current_Value_Condition;
3162 -- Start of processing for Get_Current_Value_Condition
3164 begin
3165 Op := N_Empty;
3166 Val := Empty;
3168 -- Immediate return, nothing doing, if this is not an object
3170 if Ekind (Ent) not in Object_Kind then
3171 return;
3172 end if;
3174 -- Otherwise examine current value
3176 declare
3177 CV : constant Node_Id := Current_Value (Ent);
3178 Sens : Boolean;
3179 Stm : Node_Id;
3181 begin
3182 -- If statement. Condition is known true in THEN section, known False
3183 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
3185 if Nkind (CV) = N_If_Statement then
3187 -- Before start of IF statement
3189 if Loc < Sloc (CV) then
3190 return;
3192 -- After end of IF statement
3194 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
3195 return;
3196 end if;
3198 -- At this stage we know that we are within the IF statement, but
3199 -- unfortunately, the tree does not record the SLOC of the ELSE so
3200 -- we cannot use a simple SLOC comparison to distinguish between
3201 -- the then/else statements, so we have to climb the tree.
3203 declare
3204 N : Node_Id;
3206 begin
3207 N := Parent (Var);
3208 while Parent (N) /= CV loop
3209 N := Parent (N);
3211 -- If we fall off the top of the tree, then that's odd, but
3212 -- perhaps it could occur in some error situation, and the
3213 -- safest response is simply to assume that the outcome of
3214 -- the condition is unknown. No point in bombing during an
3215 -- attempt to optimize things.
3217 if No (N) then
3218 return;
3219 end if;
3220 end loop;
3222 -- Now we have N pointing to a node whose parent is the IF
3223 -- statement in question, so now we can tell if we are within
3224 -- the THEN statements.
3226 if Is_List_Member (N)
3227 and then List_Containing (N) = Then_Statements (CV)
3228 then
3229 Sens := True;
3231 -- If the variable reference does not come from source, we
3232 -- cannot reliably tell whether it appears in the else part.
3233 -- In particular, if it appears in generated code for a node
3234 -- that requires finalization, it may be attached to a list
3235 -- that has not been yet inserted into the code. For now,
3236 -- treat it as unknown.
3238 elsif not Comes_From_Source (N) then
3239 return;
3241 -- Otherwise we must be in ELSIF or ELSE part
3243 else
3244 Sens := False;
3245 end if;
3246 end;
3248 -- ELSIF part. Condition is known true within the referenced
3249 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
3250 -- and unknown before the ELSE part or after the IF statement.
3252 elsif Nkind (CV) = N_Elsif_Part then
3254 -- if the Elsif_Part had condition_actions, the elsif has been
3255 -- rewritten as a nested if, and the original elsif_part is
3256 -- detached from the tree, so there is no way to obtain useful
3257 -- information on the current value of the variable.
3258 -- Can this be improved ???
3260 if No (Parent (CV)) then
3261 return;
3262 end if;
3264 Stm := Parent (CV);
3266 -- Before start of ELSIF part
3268 if Loc < Sloc (CV) then
3269 return;
3271 -- After end of IF statement
3273 elsif Loc >= Sloc (Stm) +
3274 Text_Ptr (UI_To_Int (End_Span (Stm)))
3275 then
3276 return;
3277 end if;
3279 -- Again we lack the SLOC of the ELSE, so we need to climb the
3280 -- tree to see if we are within the ELSIF part in question.
3282 declare
3283 N : Node_Id;
3285 begin
3286 N := Parent (Var);
3287 while Parent (N) /= Stm loop
3288 N := Parent (N);
3290 -- If we fall off the top of the tree, then that's odd, but
3291 -- perhaps it could occur in some error situation, and the
3292 -- safest response is simply to assume that the outcome of
3293 -- the condition is unknown. No point in bombing during an
3294 -- attempt to optimize things.
3296 if No (N) then
3297 return;
3298 end if;
3299 end loop;
3301 -- Now we have N pointing to a node whose parent is the IF
3302 -- statement in question, so see if is the ELSIF part we want.
3303 -- the THEN statements.
3305 if N = CV then
3306 Sens := True;
3308 -- Otherwise we must be in subsequent ELSIF or ELSE part
3310 else
3311 Sens := False;
3312 end if;
3313 end;
3315 -- Iteration scheme of while loop. The condition is known to be
3316 -- true within the body of the loop.
3318 elsif Nkind (CV) = N_Iteration_Scheme then
3319 declare
3320 Loop_Stmt : constant Node_Id := Parent (CV);
3322 begin
3323 -- Before start of body of loop
3325 if Loc < Sloc (Loop_Stmt) then
3326 return;
3328 -- After end of LOOP statement
3330 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
3331 return;
3333 -- We are within the body of the loop
3335 else
3336 Sens := True;
3337 end if;
3338 end;
3340 -- All other cases of Current_Value settings
3342 else
3343 return;
3344 end if;
3346 -- If we fall through here, then we have a reportable condition, Sens
3347 -- is True if the condition is true and False if it needs inverting.
3349 Process_Current_Value_Condition (Condition (CV), Sens);
3350 end;
3351 end Get_Current_Value_Condition;
3353 ---------------------
3354 -- Get_Stream_Size --
3355 ---------------------
3357 function Get_Stream_Size (E : Entity_Id) return Uint is
3358 begin
3359 -- If we have a Stream_Size clause for this type use it
3361 if Has_Stream_Size_Clause (E) then
3362 return Static_Integer (Expression (Stream_Size_Clause (E)));
3364 -- Otherwise the Stream_Size if the size of the type
3366 else
3367 return Esize (E);
3368 end if;
3369 end Get_Stream_Size;
3371 ---------------------------
3372 -- Has_Access_Constraint --
3373 ---------------------------
3375 function Has_Access_Constraint (E : Entity_Id) return Boolean is
3376 Disc : Entity_Id;
3377 T : constant Entity_Id := Etype (E);
3379 begin
3380 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
3381 Disc := First_Discriminant (T);
3382 while Present (Disc) loop
3383 if Is_Access_Type (Etype (Disc)) then
3384 return True;
3385 end if;
3387 Next_Discriminant (Disc);
3388 end loop;
3390 return False;
3391 else
3392 return False;
3393 end if;
3394 end Has_Access_Constraint;
3396 -----------------------------------------------------
3397 -- Has_Annotate_Pragma_For_External_Axiomatization --
3398 -----------------------------------------------------
3400 function Has_Annotate_Pragma_For_External_Axiomatization
3401 (E : Entity_Id) return Boolean
3403 function Is_Annotate_Pragma_For_External_Axiomatization
3404 (N : Node_Id) return Boolean;
3405 -- Returns whether N is
3406 -- pragma Annotate (GNATprove, External_Axiomatization);
3408 ----------------------------------------------------
3409 -- Is_Annotate_Pragma_For_External_Axiomatization --
3410 ----------------------------------------------------
3412 -- The general form of pragma Annotate is
3414 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
3415 -- ARG ::= NAME | EXPRESSION
3417 -- The first two arguments are by convention intended to refer to an
3418 -- external tool and a tool-specific function. These arguments are
3419 -- not analyzed.
3421 -- The following is used to annotate a package specification which
3422 -- GNATprove should treat specially, because the axiomatization of
3423 -- this unit is given by the user instead of being automatically
3424 -- generated.
3426 -- pragma Annotate (GNATprove, External_Axiomatization);
3428 function Is_Annotate_Pragma_For_External_Axiomatization
3429 (N : Node_Id) return Boolean
3431 Name_GNATprove : constant String :=
3432 "gnatprove";
3433 Name_External_Axiomatization : constant String :=
3434 "external_axiomatization";
3435 -- Special names
3437 begin
3438 if Nkind (N) = N_Pragma
3439 and then Get_Pragma_Id (Pragma_Name (N)) = Pragma_Annotate
3440 and then List_Length (Pragma_Argument_Associations (N)) = 2
3441 then
3442 declare
3443 Arg1 : constant Node_Id :=
3444 First (Pragma_Argument_Associations (N));
3445 Arg2 : constant Node_Id := Next (Arg1);
3446 Nam1 : Name_Id;
3447 Nam2 : Name_Id;
3449 begin
3450 -- Fill in Name_Buffer with Name_GNATprove first, and then with
3451 -- Name_External_Axiomatization so that Name_Find returns the
3452 -- corresponding name. This takes care of all possible casings.
3454 Name_Len := 0;
3455 Add_Str_To_Name_Buffer (Name_GNATprove);
3456 Nam1 := Name_Find;
3458 Name_Len := 0;
3459 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
3460 Nam2 := Name_Find;
3462 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
3463 and then
3464 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
3465 end;
3467 else
3468 return False;
3469 end if;
3470 end Is_Annotate_Pragma_For_External_Axiomatization;
3472 -- Local variables
3474 Decl : Node_Id;
3475 Vis_Decls : List_Id;
3476 N : Node_Id;
3478 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
3480 begin
3481 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
3482 Decl := Parent (Parent (E));
3483 else
3484 Decl := Parent (E);
3485 end if;
3487 Vis_Decls := Visible_Declarations (Decl);
3489 N := First (Vis_Decls);
3490 while Present (N) loop
3492 -- Skip declarations generated by the frontend. Skip all pragmas
3493 -- that are not the desired Annotate pragma. Stop the search on
3494 -- the first non-pragma source declaration.
3496 if Comes_From_Source (N) then
3497 if Nkind (N) = N_Pragma then
3498 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
3499 return True;
3500 end if;
3501 else
3502 return False;
3503 end if;
3504 end if;
3506 Next (N);
3507 end loop;
3509 return False;
3510 end Has_Annotate_Pragma_For_External_Axiomatization;
3512 ----------------------------------
3513 -- Has_Following_Address_Clause --
3514 ----------------------------------
3516 -- Should this function check the private part in a package ???
3518 function Has_Following_Address_Clause (D : Node_Id) return Boolean is
3519 Id : constant Entity_Id := Defining_Identifier (D);
3520 Decl : Node_Id;
3522 begin
3523 Decl := Next (D);
3524 while Present (Decl) loop
3525 if Nkind (Decl) = N_At_Clause
3526 and then Chars (Identifier (Decl)) = Chars (Id)
3527 then
3528 return True;
3530 elsif Nkind (Decl) = N_Attribute_Definition_Clause
3531 and then Chars (Decl) = Name_Address
3532 and then Chars (Name (Decl)) = Chars (Id)
3533 then
3534 return True;
3535 end if;
3537 Next (Decl);
3538 end loop;
3540 return False;
3541 end Has_Following_Address_Clause;
3543 --------------------
3544 -- Homonym_Number --
3545 --------------------
3547 function Homonym_Number (Subp : Entity_Id) return Nat is
3548 Count : Nat;
3549 Hom : Entity_Id;
3551 begin
3552 Count := 1;
3553 Hom := Homonym (Subp);
3554 while Present (Hom) loop
3555 if Scope (Hom) = Scope (Subp) then
3556 Count := Count + 1;
3557 end if;
3559 Hom := Homonym (Hom);
3560 end loop;
3562 return Count;
3563 end Homonym_Number;
3565 -----------------------------------
3566 -- In_Library_Level_Package_Body --
3567 -----------------------------------
3569 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
3570 begin
3571 -- First determine whether the entity appears at the library level, then
3572 -- look at the containing unit.
3574 if Is_Library_Level_Entity (Id) then
3575 declare
3576 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
3578 begin
3579 return Nkind (Unit (Container)) = N_Package_Body;
3580 end;
3581 end if;
3583 return False;
3584 end In_Library_Level_Package_Body;
3586 ------------------------------
3587 -- In_Unconditional_Context --
3588 ------------------------------
3590 function In_Unconditional_Context (Node : Node_Id) return Boolean is
3591 P : Node_Id;
3593 begin
3594 P := Node;
3595 while Present (P) loop
3596 case Nkind (P) is
3597 when N_Subprogram_Body =>
3598 return True;
3600 when N_If_Statement =>
3601 return False;
3603 when N_Loop_Statement =>
3604 return False;
3606 when N_Case_Statement =>
3607 return False;
3609 when others =>
3610 P := Parent (P);
3611 end case;
3612 end loop;
3614 return False;
3615 end In_Unconditional_Context;
3617 -------------------
3618 -- Insert_Action --
3619 -------------------
3621 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
3622 begin
3623 if Present (Ins_Action) then
3624 Insert_Actions (Assoc_Node, New_List (Ins_Action));
3625 end if;
3626 end Insert_Action;
3628 -- Version with check(s) suppressed
3630 procedure Insert_Action
3631 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
3633 begin
3634 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
3635 end Insert_Action;
3637 -------------------------
3638 -- Insert_Action_After --
3639 -------------------------
3641 procedure Insert_Action_After
3642 (Assoc_Node : Node_Id;
3643 Ins_Action : Node_Id)
3645 begin
3646 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
3647 end Insert_Action_After;
3649 --------------------
3650 -- Insert_Actions --
3651 --------------------
3653 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
3654 N : Node_Id;
3655 P : Node_Id;
3657 Wrapped_Node : Node_Id := Empty;
3659 begin
3660 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
3661 return;
3662 end if;
3664 -- Ignore insert of actions from inside default expression (or other
3665 -- similar "spec expression") in the special spec-expression analyze
3666 -- mode. Any insertions at this point have no relevance, since we are
3667 -- only doing the analyze to freeze the types of any static expressions.
3668 -- See section "Handling of Default Expressions" in the spec of package
3669 -- Sem for further details.
3671 if In_Spec_Expression then
3672 return;
3673 end if;
3675 -- If the action derives from stuff inside a record, then the actions
3676 -- are attached to the current scope, to be inserted and analyzed on
3677 -- exit from the scope. The reason for this is that we may also be
3678 -- generating freeze actions at the same time, and they must eventually
3679 -- be elaborated in the correct order.
3681 if Is_Record_Type (Current_Scope)
3682 and then not Is_Frozen (Current_Scope)
3683 then
3684 if No (Scope_Stack.Table
3685 (Scope_Stack.Last).Pending_Freeze_Actions)
3686 then
3687 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
3688 Ins_Actions;
3689 else
3690 Append_List
3691 (Ins_Actions,
3692 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
3693 end if;
3695 return;
3696 end if;
3698 -- We now intend to climb up the tree to find the right point to
3699 -- insert the actions. We start at Assoc_Node, unless this node is a
3700 -- subexpression in which case we start with its parent. We do this for
3701 -- two reasons. First it speeds things up. Second, if Assoc_Node is
3702 -- itself one of the special nodes like N_And_Then, then we assume that
3703 -- an initial request to insert actions for such a node does not expect
3704 -- the actions to get deposited in the node for later handling when the
3705 -- node is expanded, since clearly the node is being dealt with by the
3706 -- caller. Note that in the subexpression case, N is always the child we
3707 -- came from.
3709 -- N_Raise_xxx_Error is an annoying special case, it is a statement if
3710 -- it has type Standard_Void_Type, and a subexpression otherwise.
3711 -- otherwise. Procedure calls, and similarly procedure attribute
3712 -- references, are also statements.
3714 if Nkind (Assoc_Node) in N_Subexpr
3715 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
3716 or else Etype (Assoc_Node) /= Standard_Void_Type)
3717 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
3718 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
3719 or else not Is_Procedure_Attribute_Name
3720 (Attribute_Name (Assoc_Node)))
3721 then
3722 N := Assoc_Node;
3723 P := Parent (Assoc_Node);
3725 -- Non-subexpression case. Note that N is initially Empty in this case
3726 -- (N is only guaranteed Non-Empty in the subexpr case).
3728 else
3729 N := Empty;
3730 P := Assoc_Node;
3731 end if;
3733 -- Capture root of the transient scope
3735 if Scope_Is_Transient then
3736 Wrapped_Node := Node_To_Be_Wrapped;
3737 end if;
3739 loop
3740 pragma Assert (Present (P));
3742 -- Make sure that inserted actions stay in the transient scope
3744 if Present (Wrapped_Node) and then N = Wrapped_Node then
3745 Store_Before_Actions_In_Scope (Ins_Actions);
3746 return;
3747 end if;
3749 case Nkind (P) is
3751 -- Case of right operand of AND THEN or OR ELSE. Put the actions
3752 -- in the Actions field of the right operand. They will be moved
3753 -- out further when the AND THEN or OR ELSE operator is expanded.
3754 -- Nothing special needs to be done for the left operand since
3755 -- in that case the actions are executed unconditionally.
3757 when N_Short_Circuit =>
3758 if N = Right_Opnd (P) then
3760 -- We are now going to either append the actions to the
3761 -- actions field of the short-circuit operation. We will
3762 -- also analyze the actions now.
3764 -- This analysis is really too early, the proper thing would
3765 -- be to just park them there now, and only analyze them if
3766 -- we find we really need them, and to it at the proper
3767 -- final insertion point. However attempting to this proved
3768 -- tricky, so for now we just kill current values before and
3769 -- after the analyze call to make sure we avoid peculiar
3770 -- optimizations from this out of order insertion.
3772 Kill_Current_Values;
3774 -- If P has already been expanded, we can't park new actions
3775 -- on it, so we need to expand them immediately, introducing
3776 -- an Expression_With_Actions. N can't be an expression
3777 -- with actions, or else then the actions would have been
3778 -- inserted at an inner level.
3780 if Analyzed (P) then
3781 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
3782 Rewrite (N,
3783 Make_Expression_With_Actions (Sloc (N),
3784 Actions => Ins_Actions,
3785 Expression => Relocate_Node (N)));
3786 Analyze_And_Resolve (N);
3788 elsif Present (Actions (P)) then
3789 Insert_List_After_And_Analyze
3790 (Last (Actions (P)), Ins_Actions);
3791 else
3792 Set_Actions (P, Ins_Actions);
3793 Analyze_List (Actions (P));
3794 end if;
3796 Kill_Current_Values;
3798 return;
3799 end if;
3801 -- Then or Else dependent expression of an if expression. Add
3802 -- actions to Then_Actions or Else_Actions field as appropriate.
3803 -- The actions will be moved further out when the if is expanded.
3805 when N_If_Expression =>
3806 declare
3807 ThenX : constant Node_Id := Next (First (Expressions (P)));
3808 ElseX : constant Node_Id := Next (ThenX);
3810 begin
3811 -- If the enclosing expression is already analyzed, as
3812 -- is the case for nested elaboration checks, insert the
3813 -- conditional further out.
3815 if Analyzed (P) then
3816 null;
3818 -- Actions belong to the then expression, temporarily place
3819 -- them as Then_Actions of the if expression. They will be
3820 -- moved to the proper place later when the if expression
3821 -- is expanded.
3823 elsif N = ThenX then
3824 if Present (Then_Actions (P)) then
3825 Insert_List_After_And_Analyze
3826 (Last (Then_Actions (P)), Ins_Actions);
3827 else
3828 Set_Then_Actions (P, Ins_Actions);
3829 Analyze_List (Then_Actions (P));
3830 end if;
3832 return;
3834 -- Actions belong to the else expression, temporarily place
3835 -- them as Else_Actions of the if expression. They will be
3836 -- moved to the proper place later when the if expression
3837 -- is expanded.
3839 elsif N = ElseX then
3840 if Present (Else_Actions (P)) then
3841 Insert_List_After_And_Analyze
3842 (Last (Else_Actions (P)), Ins_Actions);
3843 else
3844 Set_Else_Actions (P, Ins_Actions);
3845 Analyze_List (Else_Actions (P));
3846 end if;
3848 return;
3850 -- Actions belong to the condition. In this case they are
3851 -- unconditionally executed, and so we can continue the
3852 -- search for the proper insert point.
3854 else
3855 null;
3856 end if;
3857 end;
3859 -- Alternative of case expression, we place the action in the
3860 -- Actions field of the case expression alternative, this will
3861 -- be handled when the case expression is expanded.
3863 when N_Case_Expression_Alternative =>
3864 if Present (Actions (P)) then
3865 Insert_List_After_And_Analyze
3866 (Last (Actions (P)), Ins_Actions);
3867 else
3868 Set_Actions (P, Ins_Actions);
3869 Analyze_List (Actions (P));
3870 end if;
3872 return;
3874 -- Case of appearing within an Expressions_With_Actions node. When
3875 -- the new actions come from the expression of the expression with
3876 -- actions, they must be added to the existing actions. The other
3877 -- alternative is when the new actions are related to one of the
3878 -- existing actions of the expression with actions, and should
3879 -- never reach here: if actions are inserted on a statement
3880 -- within the Actions of an expression with actions, or on some
3881 -- sub-expression of such a statement, then the outermost proper
3882 -- insertion point is right before the statement, and we should
3883 -- never climb up as far as the N_Expression_With_Actions itself.
3885 when N_Expression_With_Actions =>
3886 if N = Expression (P) then
3887 if Is_Empty_List (Actions (P)) then
3888 Append_List_To (Actions (P), Ins_Actions);
3889 Analyze_List (Actions (P));
3890 else
3891 Insert_List_After_And_Analyze
3892 (Last (Actions (P)), Ins_Actions);
3893 end if;
3895 return;
3897 else
3898 raise Program_Error;
3899 end if;
3901 -- Case of appearing in the condition of a while expression or
3902 -- elsif. We insert the actions into the Condition_Actions field.
3903 -- They will be moved further out when the while loop or elsif
3904 -- is analyzed.
3906 when N_Iteration_Scheme |
3907 N_Elsif_Part
3909 if N = Condition (P) then
3910 if Present (Condition_Actions (P)) then
3911 Insert_List_After_And_Analyze
3912 (Last (Condition_Actions (P)), Ins_Actions);
3913 else
3914 Set_Condition_Actions (P, Ins_Actions);
3916 -- Set the parent of the insert actions explicitly. This
3917 -- is not a syntactic field, but we need the parent field
3918 -- set, in particular so that freeze can understand that
3919 -- it is dealing with condition actions, and properly
3920 -- insert the freezing actions.
3922 Set_Parent (Ins_Actions, P);
3923 Analyze_List (Condition_Actions (P));
3924 end if;
3926 return;
3927 end if;
3929 -- Statements, declarations, pragmas, representation clauses
3931 when
3932 -- Statements
3934 N_Procedure_Call_Statement |
3935 N_Statement_Other_Than_Procedure_Call |
3937 -- Pragmas
3939 N_Pragma |
3941 -- Representation_Clause
3943 N_At_Clause |
3944 N_Attribute_Definition_Clause |
3945 N_Enumeration_Representation_Clause |
3946 N_Record_Representation_Clause |
3948 -- Declarations
3950 N_Abstract_Subprogram_Declaration |
3951 N_Entry_Body |
3952 N_Exception_Declaration |
3953 N_Exception_Renaming_Declaration |
3954 N_Expression_Function |
3955 N_Formal_Abstract_Subprogram_Declaration |
3956 N_Formal_Concrete_Subprogram_Declaration |
3957 N_Formal_Object_Declaration |
3958 N_Formal_Type_Declaration |
3959 N_Full_Type_Declaration |
3960 N_Function_Instantiation |
3961 N_Generic_Function_Renaming_Declaration |
3962 N_Generic_Package_Declaration |
3963 N_Generic_Package_Renaming_Declaration |
3964 N_Generic_Procedure_Renaming_Declaration |
3965 N_Generic_Subprogram_Declaration |
3966 N_Implicit_Label_Declaration |
3967 N_Incomplete_Type_Declaration |
3968 N_Number_Declaration |
3969 N_Object_Declaration |
3970 N_Object_Renaming_Declaration |
3971 N_Package_Body |
3972 N_Package_Body_Stub |
3973 N_Package_Declaration |
3974 N_Package_Instantiation |
3975 N_Package_Renaming_Declaration |
3976 N_Private_Extension_Declaration |
3977 N_Private_Type_Declaration |
3978 N_Procedure_Instantiation |
3979 N_Protected_Body |
3980 N_Protected_Body_Stub |
3981 N_Protected_Type_Declaration |
3982 N_Single_Task_Declaration |
3983 N_Subprogram_Body |
3984 N_Subprogram_Body_Stub |
3985 N_Subprogram_Declaration |
3986 N_Subprogram_Renaming_Declaration |
3987 N_Subtype_Declaration |
3988 N_Task_Body |
3989 N_Task_Body_Stub |
3990 N_Task_Type_Declaration |
3992 -- Use clauses can appear in lists of declarations
3994 N_Use_Package_Clause |
3995 N_Use_Type_Clause |
3997 -- Freeze entity behaves like a declaration or statement
3999 N_Freeze_Entity |
4000 N_Freeze_Generic_Entity
4002 -- Do not insert here if the item is not a list member (this
4003 -- happens for example with a triggering statement, and the
4004 -- proper approach is to insert before the entire select).
4006 if not Is_List_Member (P) then
4007 null;
4009 -- Do not insert if parent of P is an N_Component_Association
4010 -- node (i.e. we are in the context of an N_Aggregate or
4011 -- N_Extension_Aggregate node. In this case we want to insert
4012 -- before the entire aggregate.
4014 elsif Nkind (Parent (P)) = N_Component_Association then
4015 null;
4017 -- Do not insert if the parent of P is either an N_Variant node
4018 -- or an N_Record_Definition node, meaning in either case that
4019 -- P is a member of a component list, and that therefore the
4020 -- actions should be inserted outside the complete record
4021 -- declaration.
4023 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
4024 null;
4026 -- Do not insert freeze nodes within the loop generated for
4027 -- an aggregate, because they may be elaborated too late for
4028 -- subsequent use in the back end: within a package spec the
4029 -- loop is part of the elaboration procedure and is only
4030 -- elaborated during the second pass.
4032 -- If the loop comes from source, or the entity is local to the
4033 -- loop itself it must remain within.
4035 elsif Nkind (Parent (P)) = N_Loop_Statement
4036 and then not Comes_From_Source (Parent (P))
4037 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
4038 and then
4039 Scope (Entity (First (Ins_Actions))) /= Current_Scope
4040 then
4041 null;
4043 -- Otherwise we can go ahead and do the insertion
4045 elsif P = Wrapped_Node then
4046 Store_Before_Actions_In_Scope (Ins_Actions);
4047 return;
4049 else
4050 Insert_List_Before_And_Analyze (P, Ins_Actions);
4051 return;
4052 end if;
4054 -- A special case, N_Raise_xxx_Error can act either as a statement
4055 -- or a subexpression. We tell the difference by looking at the
4056 -- Etype. It is set to Standard_Void_Type in the statement case.
4058 when
4059 N_Raise_xxx_Error =>
4060 if Etype (P) = Standard_Void_Type then
4061 if P = Wrapped_Node then
4062 Store_Before_Actions_In_Scope (Ins_Actions);
4063 else
4064 Insert_List_Before_And_Analyze (P, Ins_Actions);
4065 end if;
4067 return;
4069 -- In the subexpression case, keep climbing
4071 else
4072 null;
4073 end if;
4075 -- If a component association appears within a loop created for
4076 -- an array aggregate, attach the actions to the association so
4077 -- they can be subsequently inserted within the loop. For other
4078 -- component associations insert outside of the aggregate. For
4079 -- an association that will generate a loop, its Loop_Actions
4080 -- attribute is already initialized (see exp_aggr.adb).
4082 -- The list of loop_actions can in turn generate additional ones,
4083 -- that are inserted before the associated node. If the associated
4084 -- node is outside the aggregate, the new actions are collected
4085 -- at the end of the loop actions, to respect the order in which
4086 -- they are to be elaborated.
4088 when
4089 N_Component_Association =>
4090 if Nkind (Parent (P)) = N_Aggregate
4091 and then Present (Loop_Actions (P))
4092 then
4093 if Is_Empty_List (Loop_Actions (P)) then
4094 Set_Loop_Actions (P, Ins_Actions);
4095 Analyze_List (Ins_Actions);
4097 else
4098 declare
4099 Decl : Node_Id;
4101 begin
4102 -- Check whether these actions were generated by a
4103 -- declaration that is part of the loop_ actions
4104 -- for the component_association.
4106 Decl := Assoc_Node;
4107 while Present (Decl) loop
4108 exit when Parent (Decl) = P
4109 and then Is_List_Member (Decl)
4110 and then
4111 List_Containing (Decl) = Loop_Actions (P);
4112 Decl := Parent (Decl);
4113 end loop;
4115 if Present (Decl) then
4116 Insert_List_Before_And_Analyze
4117 (Decl, Ins_Actions);
4118 else
4119 Insert_List_After_And_Analyze
4120 (Last (Loop_Actions (P)), Ins_Actions);
4121 end if;
4122 end;
4123 end if;
4125 return;
4127 else
4128 null;
4129 end if;
4131 -- Another special case, an attribute denoting a procedure call
4133 when
4134 N_Attribute_Reference =>
4135 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
4136 if P = Wrapped_Node then
4137 Store_Before_Actions_In_Scope (Ins_Actions);
4138 else
4139 Insert_List_Before_And_Analyze (P, Ins_Actions);
4140 end if;
4142 return;
4144 -- In the subexpression case, keep climbing
4146 else
4147 null;
4148 end if;
4150 -- A contract node should not belong to the tree
4152 when N_Contract =>
4153 raise Program_Error;
4155 -- For all other node types, keep climbing tree
4157 when
4158 N_Abortable_Part |
4159 N_Accept_Alternative |
4160 N_Access_Definition |
4161 N_Access_Function_Definition |
4162 N_Access_Procedure_Definition |
4163 N_Access_To_Object_Definition |
4164 N_Aggregate |
4165 N_Allocator |
4166 N_Aspect_Specification |
4167 N_Case_Expression |
4168 N_Case_Statement_Alternative |
4169 N_Character_Literal |
4170 N_Compilation_Unit |
4171 N_Compilation_Unit_Aux |
4172 N_Component_Clause |
4173 N_Component_Declaration |
4174 N_Component_Definition |
4175 N_Component_List |
4176 N_Constrained_Array_Definition |
4177 N_Decimal_Fixed_Point_Definition |
4178 N_Defining_Character_Literal |
4179 N_Defining_Identifier |
4180 N_Defining_Operator_Symbol |
4181 N_Defining_Program_Unit_Name |
4182 N_Delay_Alternative |
4183 N_Delta_Constraint |
4184 N_Derived_Type_Definition |
4185 N_Designator |
4186 N_Digits_Constraint |
4187 N_Discriminant_Association |
4188 N_Discriminant_Specification |
4189 N_Empty |
4190 N_Entry_Body_Formal_Part |
4191 N_Entry_Call_Alternative |
4192 N_Entry_Declaration |
4193 N_Entry_Index_Specification |
4194 N_Enumeration_Type_Definition |
4195 N_Error |
4196 N_Exception_Handler |
4197 N_Expanded_Name |
4198 N_Explicit_Dereference |
4199 N_Extension_Aggregate |
4200 N_Floating_Point_Definition |
4201 N_Formal_Decimal_Fixed_Point_Definition |
4202 N_Formal_Derived_Type_Definition |
4203 N_Formal_Discrete_Type_Definition |
4204 N_Formal_Floating_Point_Definition |
4205 N_Formal_Modular_Type_Definition |
4206 N_Formal_Ordinary_Fixed_Point_Definition |
4207 N_Formal_Package_Declaration |
4208 N_Formal_Private_Type_Definition |
4209 N_Formal_Incomplete_Type_Definition |
4210 N_Formal_Signed_Integer_Type_Definition |
4211 N_Function_Call |
4212 N_Function_Specification |
4213 N_Generic_Association |
4214 N_Handled_Sequence_Of_Statements |
4215 N_Identifier |
4216 N_In |
4217 N_Index_Or_Discriminant_Constraint |
4218 N_Indexed_Component |
4219 N_Integer_Literal |
4220 N_Iterator_Specification |
4221 N_Itype_Reference |
4222 N_Label |
4223 N_Loop_Parameter_Specification |
4224 N_Mod_Clause |
4225 N_Modular_Type_Definition |
4226 N_Not_In |
4227 N_Null |
4228 N_Op_Abs |
4229 N_Op_Add |
4230 N_Op_And |
4231 N_Op_Concat |
4232 N_Op_Divide |
4233 N_Op_Eq |
4234 N_Op_Expon |
4235 N_Op_Ge |
4236 N_Op_Gt |
4237 N_Op_Le |
4238 N_Op_Lt |
4239 N_Op_Minus |
4240 N_Op_Mod |
4241 N_Op_Multiply |
4242 N_Op_Ne |
4243 N_Op_Not |
4244 N_Op_Or |
4245 N_Op_Plus |
4246 N_Op_Rem |
4247 N_Op_Rotate_Left |
4248 N_Op_Rotate_Right |
4249 N_Op_Shift_Left |
4250 N_Op_Shift_Right |
4251 N_Op_Shift_Right_Arithmetic |
4252 N_Op_Subtract |
4253 N_Op_Xor |
4254 N_Operator_Symbol |
4255 N_Ordinary_Fixed_Point_Definition |
4256 N_Others_Choice |
4257 N_Package_Specification |
4258 N_Parameter_Association |
4259 N_Parameter_Specification |
4260 N_Pop_Constraint_Error_Label |
4261 N_Pop_Program_Error_Label |
4262 N_Pop_Storage_Error_Label |
4263 N_Pragma_Argument_Association |
4264 N_Procedure_Specification |
4265 N_Protected_Definition |
4266 N_Push_Constraint_Error_Label |
4267 N_Push_Program_Error_Label |
4268 N_Push_Storage_Error_Label |
4269 N_Qualified_Expression |
4270 N_Quantified_Expression |
4271 N_Raise_Expression |
4272 N_Range |
4273 N_Range_Constraint |
4274 N_Real_Literal |
4275 N_Real_Range_Specification |
4276 N_Record_Definition |
4277 N_Reference |
4278 N_SCIL_Dispatch_Table_Tag_Init |
4279 N_SCIL_Dispatching_Call |
4280 N_SCIL_Membership_Test |
4281 N_Selected_Component |
4282 N_Signed_Integer_Type_Definition |
4283 N_Single_Protected_Declaration |
4284 N_Slice |
4285 N_String_Literal |
4286 N_Subtype_Indication |
4287 N_Subunit |
4288 N_Task_Definition |
4289 N_Terminate_Alternative |
4290 N_Triggering_Alternative |
4291 N_Type_Conversion |
4292 N_Unchecked_Expression |
4293 N_Unchecked_Type_Conversion |
4294 N_Unconstrained_Array_Definition |
4295 N_Unused_At_End |
4296 N_Unused_At_Start |
4297 N_Variant |
4298 N_Variant_Part |
4299 N_Validate_Unchecked_Conversion |
4300 N_With_Clause
4302 null;
4304 end case;
4306 -- If we fall through above tests, keep climbing tree
4308 N := P;
4310 if Nkind (Parent (N)) = N_Subunit then
4312 -- This is the proper body corresponding to a stub. Insertion must
4313 -- be done at the point of the stub, which is in the declarative
4314 -- part of the parent unit.
4316 P := Corresponding_Stub (Parent (N));
4318 else
4319 P := Parent (N);
4320 end if;
4321 end loop;
4322 end Insert_Actions;
4324 -- Version with check(s) suppressed
4326 procedure Insert_Actions
4327 (Assoc_Node : Node_Id;
4328 Ins_Actions : List_Id;
4329 Suppress : Check_Id)
4331 begin
4332 if Suppress = All_Checks then
4333 declare
4334 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
4335 begin
4336 Scope_Suppress.Suppress := (others => True);
4337 Insert_Actions (Assoc_Node, Ins_Actions);
4338 Scope_Suppress.Suppress := Sva;
4339 end;
4341 else
4342 declare
4343 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
4344 begin
4345 Scope_Suppress.Suppress (Suppress) := True;
4346 Insert_Actions (Assoc_Node, Ins_Actions);
4347 Scope_Suppress.Suppress (Suppress) := Svg;
4348 end;
4349 end if;
4350 end Insert_Actions;
4352 --------------------------
4353 -- Insert_Actions_After --
4354 --------------------------
4356 procedure Insert_Actions_After
4357 (Assoc_Node : Node_Id;
4358 Ins_Actions : List_Id)
4360 begin
4361 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
4362 Store_After_Actions_In_Scope (Ins_Actions);
4363 else
4364 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
4365 end if;
4366 end Insert_Actions_After;
4368 ------------------------
4369 -- Insert_Declaration --
4370 ------------------------
4372 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
4373 P : Node_Id;
4375 begin
4376 pragma Assert (Nkind (N) in N_Subexpr);
4378 -- Climb until we find a procedure or a package
4380 P := N;
4381 loop
4382 pragma Assert (Present (Parent (P)));
4383 P := Parent (P);
4385 if Is_List_Member (P) then
4386 exit when Nkind_In (Parent (P), N_Package_Specification,
4387 N_Subprogram_Body);
4389 -- Special handling for handled sequence of statements, we must
4390 -- insert in the statements not the exception handlers!
4392 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
4393 P := First (Statements (Parent (P)));
4394 exit;
4395 end if;
4396 end if;
4397 end loop;
4399 -- Now do the insertion
4401 Insert_Before (P, Decl);
4402 Analyze (Decl);
4403 end Insert_Declaration;
4405 ---------------------------------
4406 -- Insert_Library_Level_Action --
4407 ---------------------------------
4409 procedure Insert_Library_Level_Action (N : Node_Id) is
4410 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4412 begin
4413 Push_Scope (Cunit_Entity (Main_Unit));
4414 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
4416 if No (Actions (Aux)) then
4417 Set_Actions (Aux, New_List (N));
4418 else
4419 Append (N, Actions (Aux));
4420 end if;
4422 Analyze (N);
4423 Pop_Scope;
4424 end Insert_Library_Level_Action;
4426 ----------------------------------
4427 -- Insert_Library_Level_Actions --
4428 ----------------------------------
4430 procedure Insert_Library_Level_Actions (L : List_Id) is
4431 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
4433 begin
4434 if Is_Non_Empty_List (L) then
4435 Push_Scope (Cunit_Entity (Main_Unit));
4436 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
4438 if No (Actions (Aux)) then
4439 Set_Actions (Aux, L);
4440 Analyze_List (L);
4441 else
4442 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
4443 end if;
4445 Pop_Scope;
4446 end if;
4447 end Insert_Library_Level_Actions;
4449 ----------------------
4450 -- Inside_Init_Proc --
4451 ----------------------
4453 function Inside_Init_Proc return Boolean is
4454 S : Entity_Id;
4456 begin
4457 S := Current_Scope;
4458 while Present (S) and then S /= Standard_Standard loop
4459 if Is_Init_Proc (S) then
4460 return True;
4461 else
4462 S := Scope (S);
4463 end if;
4464 end loop;
4466 return False;
4467 end Inside_Init_Proc;
4469 ----------------------------
4470 -- Is_All_Null_Statements --
4471 ----------------------------
4473 function Is_All_Null_Statements (L : List_Id) return Boolean is
4474 Stm : Node_Id;
4476 begin
4477 Stm := First (L);
4478 while Present (Stm) loop
4479 if Nkind (Stm) /= N_Null_Statement then
4480 return False;
4481 end if;
4483 Next (Stm);
4484 end loop;
4486 return True;
4487 end Is_All_Null_Statements;
4489 --------------------------------------------------
4490 -- Is_Displacement_Of_Object_Or_Function_Result --
4491 --------------------------------------------------
4493 function Is_Displacement_Of_Object_Or_Function_Result
4494 (Obj_Id : Entity_Id) return Boolean
4496 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
4497 -- Determine if particular node denotes a controlled function call. The
4498 -- call may have been heavily expanded.
4500 function Is_Displace_Call (N : Node_Id) return Boolean;
4501 -- Determine whether a particular node is a call to Ada.Tags.Displace.
4502 -- The call might be nested within other actions such as conversions.
4504 function Is_Source_Object (N : Node_Id) return Boolean;
4505 -- Determine whether a particular node denotes a source object
4507 ---------------------------------
4508 -- Is_Controlled_Function_Call --
4509 ---------------------------------
4511 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
4512 Expr : Node_Id := Original_Node (N);
4514 begin
4515 if Nkind (Expr) = N_Function_Call then
4516 Expr := Name (Expr);
4518 -- When a function call appears in Object.Operation format, the
4519 -- original representation has two possible forms depending on the
4520 -- availability of actual parameters:
4522 -- Obj.Func_Call N_Selected_Component
4523 -- Obj.Func_Call (Param) N_Indexed_Component
4525 else
4526 if Nkind (Expr) = N_Indexed_Component then
4527 Expr := Prefix (Expr);
4528 end if;
4530 if Nkind (Expr) = N_Selected_Component then
4531 Expr := Selector_Name (Expr);
4532 end if;
4533 end if;
4535 return
4536 Nkind_In (Expr, N_Expanded_Name, N_Identifier)
4537 and then Ekind (Entity (Expr)) = E_Function
4538 and then Needs_Finalization (Etype (Entity (Expr)));
4539 end Is_Controlled_Function_Call;
4541 ----------------------
4542 -- Is_Displace_Call --
4543 ----------------------
4545 function Is_Displace_Call (N : Node_Id) return Boolean is
4546 Call : Node_Id := N;
4548 begin
4549 -- Strip various actions which may precede a call to Displace
4551 loop
4552 if Nkind (Call) = N_Explicit_Dereference then
4553 Call := Prefix (Call);
4555 elsif Nkind_In (Call, N_Type_Conversion,
4556 N_Unchecked_Type_Conversion)
4557 then
4558 Call := Expression (Call);
4560 else
4561 exit;
4562 end if;
4563 end loop;
4565 return
4566 Present (Call)
4567 and then Nkind (Call) = N_Function_Call
4568 and then Is_RTE (Entity (Name (Call)), RE_Displace);
4569 end Is_Displace_Call;
4571 ----------------------
4572 -- Is_Source_Object --
4573 ----------------------
4575 function Is_Source_Object (N : Node_Id) return Boolean is
4576 begin
4577 return
4578 Present (N)
4579 and then Nkind (N) in N_Has_Entity
4580 and then Is_Object (Entity (N))
4581 and then Comes_From_Source (N);
4582 end Is_Source_Object;
4584 -- Local variables
4586 Decl : constant Node_Id := Parent (Obj_Id);
4587 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4588 Orig_Decl : constant Node_Id := Original_Node (Decl);
4590 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
4592 begin
4593 -- Case 1:
4595 -- Obj : CW_Type := Function_Call (...);
4597 -- rewritten into:
4599 -- Tmp : ... := Function_Call (...)'reference;
4600 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
4602 -- where the return type of the function and the class-wide type require
4603 -- dispatch table pointer displacement.
4605 -- Case 2:
4607 -- Obj : CW_Type := Src_Obj;
4609 -- rewritten into:
4611 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
4613 -- where the type of the source object and the class-wide type require
4614 -- dispatch table pointer displacement.
4616 return
4617 Nkind (Decl) = N_Object_Renaming_Declaration
4618 and then Nkind (Orig_Decl) = N_Object_Declaration
4619 and then Comes_From_Source (Orig_Decl)
4620 and then Is_Class_Wide_Type (Obj_Typ)
4621 and then Is_Displace_Call (Renamed_Object (Obj_Id))
4622 and then
4623 (Is_Controlled_Function_Call (Expression (Orig_Decl))
4624 or else Is_Source_Object (Expression (Orig_Decl)));
4625 end Is_Displacement_Of_Object_Or_Function_Result;
4627 ------------------------------
4628 -- Is_Finalizable_Transient --
4629 ------------------------------
4631 function Is_Finalizable_Transient
4632 (Decl : Node_Id;
4633 Rel_Node : Node_Id) return Boolean
4635 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4636 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4637 Desig : Entity_Id := Obj_Typ;
4639 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
4640 -- Determine whether transient object Trans_Id is initialized either
4641 -- by a function call which returns an access type or simply renames
4642 -- another pointer.
4644 function Initialized_By_Aliased_BIP_Func_Call
4645 (Trans_Id : Entity_Id) return Boolean;
4646 -- Determine whether transient object Trans_Id is initialized by a
4647 -- build-in-place function call where the BIPalloc parameter is of
4648 -- value 1 and BIPaccess is not null. This case creates an aliasing
4649 -- between the returned value and the value denoted by BIPaccess.
4651 function Is_Aliased
4652 (Trans_Id : Entity_Id;
4653 First_Stmt : Node_Id) return Boolean;
4654 -- Determine whether transient object Trans_Id has been renamed or
4655 -- aliased through 'reference in the statement list starting from
4656 -- First_Stmt.
4658 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
4659 -- Determine whether transient object Trans_Id is allocated on the heap
4661 function Is_Iterated_Container
4662 (Trans_Id : Entity_Id;
4663 First_Stmt : Node_Id) return Boolean;
4664 -- Determine whether transient object Trans_Id denotes a container which
4665 -- is in the process of being iterated in the statement list starting
4666 -- from First_Stmt.
4668 ---------------------------
4669 -- Initialized_By_Access --
4670 ---------------------------
4672 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
4673 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4675 begin
4676 return
4677 Present (Expr)
4678 and then Nkind (Expr) /= N_Reference
4679 and then Is_Access_Type (Etype (Expr));
4680 end Initialized_By_Access;
4682 ------------------------------------------
4683 -- Initialized_By_Aliased_BIP_Func_Call --
4684 ------------------------------------------
4686 function Initialized_By_Aliased_BIP_Func_Call
4687 (Trans_Id : Entity_Id) return Boolean
4689 Call : Node_Id := Expression (Parent (Trans_Id));
4691 begin
4692 -- Build-in-place calls usually appear in 'reference format
4694 if Nkind (Call) = N_Reference then
4695 Call := Prefix (Call);
4696 end if;
4698 if Is_Build_In_Place_Function_Call (Call) then
4699 declare
4700 Access_Nam : Name_Id := No_Name;
4701 Access_OK : Boolean := False;
4702 Actual : Node_Id;
4703 Alloc_Nam : Name_Id := No_Name;
4704 Alloc_OK : Boolean := False;
4705 Formal : Node_Id;
4706 Func_Id : Entity_Id;
4707 Param : Node_Id;
4709 begin
4710 -- Examine all parameter associations of the function call
4712 Param := First (Parameter_Associations (Call));
4713 while Present (Param) loop
4714 if Nkind (Param) = N_Parameter_Association
4715 and then Nkind (Selector_Name (Param)) = N_Identifier
4716 then
4717 Actual := Explicit_Actual_Parameter (Param);
4718 Formal := Selector_Name (Param);
4720 -- Construct the names of formals BIPaccess and BIPalloc
4721 -- using the function name retrieved from an arbitrary
4722 -- formal.
4724 if Access_Nam = No_Name
4725 and then Alloc_Nam = No_Name
4726 and then Present (Entity (Formal))
4727 then
4728 Func_Id := Scope (Entity (Formal));
4730 Access_Nam :=
4731 New_External_Name (Chars (Func_Id),
4732 BIP_Formal_Suffix (BIP_Object_Access));
4734 Alloc_Nam :=
4735 New_External_Name (Chars (Func_Id),
4736 BIP_Formal_Suffix (BIP_Alloc_Form));
4737 end if;
4739 -- A match for BIPaccess => Temp has been found
4741 if Chars (Formal) = Access_Nam
4742 and then Nkind (Actual) /= N_Null
4743 then
4744 Access_OK := True;
4745 end if;
4747 -- A match for BIPalloc => 1 has been found
4749 if Chars (Formal) = Alloc_Nam
4750 and then Nkind (Actual) = N_Integer_Literal
4751 and then Intval (Actual) = Uint_1
4752 then
4753 Alloc_OK := True;
4754 end if;
4755 end if;
4757 Next (Param);
4758 end loop;
4760 return Access_OK and Alloc_OK;
4761 end;
4762 end if;
4764 return False;
4765 end Initialized_By_Aliased_BIP_Func_Call;
4767 ----------------
4768 -- Is_Aliased --
4769 ----------------
4771 function Is_Aliased
4772 (Trans_Id : Entity_Id;
4773 First_Stmt : Node_Id) return Boolean
4775 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
4776 -- Given an object renaming declaration, retrieve the entity of the
4777 -- renamed name. Return Empty if the renamed name is anything other
4778 -- than a variable or a constant.
4780 -------------------------
4781 -- Find_Renamed_Object --
4782 -------------------------
4784 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
4785 Ren_Obj : Node_Id := Empty;
4787 function Find_Object (N : Node_Id) return Traverse_Result;
4788 -- Try to detect an object which is either a constant or a
4789 -- variable.
4791 -----------------
4792 -- Find_Object --
4793 -----------------
4795 function Find_Object (N : Node_Id) return Traverse_Result is
4796 begin
4797 -- Stop the search once a constant or a variable has been
4798 -- detected.
4800 if Nkind (N) = N_Identifier
4801 and then Present (Entity (N))
4802 and then Ekind_In (Entity (N), E_Constant, E_Variable)
4803 then
4804 Ren_Obj := Entity (N);
4805 return Abandon;
4806 end if;
4808 return OK;
4809 end Find_Object;
4811 procedure Search is new Traverse_Proc (Find_Object);
4813 -- Local variables
4815 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
4817 -- Start of processing for Find_Renamed_Object
4819 begin
4820 -- Actions related to dispatching calls may appear as renamings of
4821 -- tags. Do not process this type of renaming because it does not
4822 -- use the actual value of the object.
4824 if not Is_RTE (Typ, RE_Tag_Ptr) then
4825 Search (Name (Ren_Decl));
4826 end if;
4828 return Ren_Obj;
4829 end Find_Renamed_Object;
4831 -- Local variables
4833 Expr : Node_Id;
4834 Ren_Obj : Entity_Id;
4835 Stmt : Node_Id;
4837 -- Start of processing for Is_Aliased
4839 begin
4840 Stmt := First_Stmt;
4841 while Present (Stmt) loop
4842 if Nkind (Stmt) = N_Object_Declaration then
4843 Expr := Expression (Stmt);
4845 if Present (Expr)
4846 and then Nkind (Expr) = N_Reference
4847 and then Nkind (Prefix (Expr)) = N_Identifier
4848 and then Entity (Prefix (Expr)) = Trans_Id
4849 then
4850 return True;
4851 end if;
4853 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
4854 Ren_Obj := Find_Renamed_Object (Stmt);
4856 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
4857 return True;
4858 end if;
4859 end if;
4861 Next (Stmt);
4862 end loop;
4864 return False;
4865 end Is_Aliased;
4867 ------------------
4868 -- Is_Allocated --
4869 ------------------
4871 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
4872 Expr : constant Node_Id := Expression (Parent (Trans_Id));
4873 begin
4874 return
4875 Is_Access_Type (Etype (Trans_Id))
4876 and then Present (Expr)
4877 and then Nkind (Expr) = N_Allocator;
4878 end Is_Allocated;
4880 ---------------------------
4881 -- Is_Iterated_Container --
4882 ---------------------------
4884 function Is_Iterated_Container
4885 (Trans_Id : Entity_Id;
4886 First_Stmt : Node_Id) return Boolean
4888 Aspect : Node_Id;
4889 Call : Node_Id;
4890 Iter : Entity_Id;
4891 Param : Node_Id;
4892 Stmt : Node_Id;
4893 Typ : Entity_Id;
4895 begin
4896 -- It is not possible to iterate over containers in non-Ada 2012 code
4898 if Ada_Version < Ada_2012 then
4899 return False;
4900 end if;
4902 Typ := Etype (Trans_Id);
4904 -- Handle access type created for secondary stack use
4906 if Is_Access_Type (Typ) then
4907 Typ := Designated_Type (Typ);
4908 end if;
4910 -- Look for aspect Default_Iterator. It may be part of a type
4911 -- declaration for a container, or inherited from a base type
4912 -- or parent type.
4914 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
4916 if Present (Aspect) then
4917 Iter := Entity (Aspect);
4919 -- Examine the statements following the container object and
4920 -- look for a call to the default iterate routine where the
4921 -- first parameter is the transient. Such a call appears as:
4923 -- It : Access_To_CW_Iterator :=
4924 -- Iterate (Tran_Id.all, ...)'reference;
4926 Stmt := First_Stmt;
4927 while Present (Stmt) loop
4929 -- Detect an object declaration which is initialized by a
4930 -- secondary stack function call.
4932 if Nkind (Stmt) = N_Object_Declaration
4933 and then Present (Expression (Stmt))
4934 and then Nkind (Expression (Stmt)) = N_Reference
4935 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
4936 then
4937 Call := Prefix (Expression (Stmt));
4939 -- The call must invoke the default iterate routine of
4940 -- the container and the transient object must appear as
4941 -- the first actual parameter. Skip any calls whose names
4942 -- are not entities.
4944 if Is_Entity_Name (Name (Call))
4945 and then Entity (Name (Call)) = Iter
4946 and then Present (Parameter_Associations (Call))
4947 then
4948 Param := First (Parameter_Associations (Call));
4950 if Nkind (Param) = N_Explicit_Dereference
4951 and then Entity (Prefix (Param)) = Trans_Id
4952 then
4953 return True;
4954 end if;
4955 end if;
4956 end if;
4958 Next (Stmt);
4959 end loop;
4960 end if;
4962 return False;
4963 end Is_Iterated_Container;
4965 -- Start of processing for Is_Finalizable_Transient
4967 begin
4968 -- Handle access types
4970 if Is_Access_Type (Desig) then
4971 Desig := Available_View (Designated_Type (Desig));
4972 end if;
4974 return
4975 Ekind_In (Obj_Id, E_Constant, E_Variable)
4976 and then Needs_Finalization (Desig)
4977 and then Requires_Transient_Scope (Desig)
4978 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
4980 -- Do not consider renamed or 'reference-d transient objects because
4981 -- the act of renaming extends the object's lifetime.
4983 and then not Is_Aliased (Obj_Id, Decl)
4985 -- Do not consider transient objects allocated on the heap since
4986 -- they are attached to a finalization master.
4988 and then not Is_Allocated (Obj_Id)
4990 -- If the transient object is a pointer, check that it is not
4991 -- initialized by a function which returns a pointer or acts as a
4992 -- renaming of another pointer.
4994 and then
4995 (not Is_Access_Type (Obj_Typ)
4996 or else not Initialized_By_Access (Obj_Id))
4998 -- Do not consider transient objects which act as indirect aliases
4999 -- of build-in-place function results.
5001 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
5003 -- Do not consider conversions of tags to class-wide types
5005 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
5007 -- Do not consider containers in the context of iterator loops. Such
5008 -- transient objects must exist for as long as the loop is around,
5009 -- otherwise any operation carried out by the iterator will fail.
5011 and then not Is_Iterated_Container (Obj_Id, Decl);
5012 end Is_Finalizable_Transient;
5014 ---------------------------------
5015 -- Is_Fully_Repped_Tagged_Type --
5016 ---------------------------------
5018 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
5019 U : constant Entity_Id := Underlying_Type (T);
5020 Comp : Entity_Id;
5022 begin
5023 if No (U) or else not Is_Tagged_Type (U) then
5024 return False;
5025 elsif Has_Discriminants (U) then
5026 return False;
5027 elsif not Has_Specified_Layout (U) then
5028 return False;
5029 end if;
5031 -- Here we have a tagged type, see if it has any unlayed out fields
5032 -- other than a possible tag and parent fields. If so, we return False.
5034 Comp := First_Component (U);
5035 while Present (Comp) loop
5036 if not Is_Tag (Comp)
5037 and then Chars (Comp) /= Name_uParent
5038 and then No (Component_Clause (Comp))
5039 then
5040 return False;
5041 else
5042 Next_Component (Comp);
5043 end if;
5044 end loop;
5046 -- All components are layed out
5048 return True;
5049 end Is_Fully_Repped_Tagged_Type;
5051 ----------------------------------
5052 -- Is_Library_Level_Tagged_Type --
5053 ----------------------------------
5055 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
5056 begin
5057 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
5058 end Is_Library_Level_Tagged_Type;
5060 --------------------------
5061 -- Is_Non_BIP_Func_Call --
5062 --------------------------
5064 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
5065 begin
5066 -- The expected call is of the format
5068 -- Func_Call'reference
5070 return
5071 Nkind (Expr) = N_Reference
5072 and then Nkind (Prefix (Expr)) = N_Function_Call
5073 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
5074 end Is_Non_BIP_Func_Call;
5076 ------------------------------------
5077 -- Is_Object_Access_BIP_Func_Call --
5078 ------------------------------------
5080 function Is_Object_Access_BIP_Func_Call
5081 (Expr : Node_Id;
5082 Obj_Id : Entity_Id) return Boolean
5084 Access_Nam : Name_Id := No_Name;
5085 Actual : Node_Id;
5086 Call : Node_Id;
5087 Formal : Node_Id;
5088 Param : Node_Id;
5090 begin
5091 -- Build-in-place calls usually appear in 'reference format. Note that
5092 -- the accessibility check machinery may add an extra 'reference due to
5093 -- side effect removal.
5095 Call := Expr;
5096 while Nkind (Call) = N_Reference loop
5097 Call := Prefix (Call);
5098 end loop;
5100 if Nkind_In (Call, N_Qualified_Expression,
5101 N_Unchecked_Type_Conversion)
5102 then
5103 Call := Expression (Call);
5104 end if;
5106 if Is_Build_In_Place_Function_Call (Call) then
5108 -- Examine all parameter associations of the function call
5110 Param := First (Parameter_Associations (Call));
5111 while Present (Param) loop
5112 if Nkind (Param) = N_Parameter_Association
5113 and then Nkind (Selector_Name (Param)) = N_Identifier
5114 then
5115 Formal := Selector_Name (Param);
5116 Actual := Explicit_Actual_Parameter (Param);
5118 -- Construct the name of formal BIPaccess. It is much easier to
5119 -- extract the name of the function using an arbitrary formal's
5120 -- scope rather than the Name field of Call.
5122 if Access_Nam = No_Name and then Present (Entity (Formal)) then
5123 Access_Nam :=
5124 New_External_Name
5125 (Chars (Scope (Entity (Formal))),
5126 BIP_Formal_Suffix (BIP_Object_Access));
5127 end if;
5129 -- A match for BIPaccess => Obj_Id'Unrestricted_Access has been
5130 -- found.
5132 if Chars (Formal) = Access_Nam
5133 and then Nkind (Actual) = N_Attribute_Reference
5134 and then Attribute_Name (Actual) = Name_Unrestricted_Access
5135 and then Nkind (Prefix (Actual)) = N_Identifier
5136 and then Entity (Prefix (Actual)) = Obj_Id
5137 then
5138 return True;
5139 end if;
5140 end if;
5142 Next (Param);
5143 end loop;
5144 end if;
5146 return False;
5147 end Is_Object_Access_BIP_Func_Call;
5149 ----------------------------------
5150 -- Is_Possibly_Unaligned_Object --
5151 ----------------------------------
5153 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
5154 T : constant Entity_Id := Etype (N);
5156 begin
5157 -- Objects are never unaligned on VMs
5159 if VM_Target /= No_VM then
5160 return False;
5161 end if;
5163 -- If renamed object, apply test to underlying object
5165 if Is_Entity_Name (N)
5166 and then Is_Object (Entity (N))
5167 and then Present (Renamed_Object (Entity (N)))
5168 then
5169 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
5170 end if;
5172 -- Tagged and controlled types and aliased types are always aligned, as
5173 -- are concurrent types.
5175 if Is_Aliased (T)
5176 or else Has_Controlled_Component (T)
5177 or else Is_Concurrent_Type (T)
5178 or else Is_Tagged_Type (T)
5179 or else Is_Controlled (T)
5180 then
5181 return False;
5182 end if;
5184 -- If this is an element of a packed array, may be unaligned
5186 if Is_Ref_To_Bit_Packed_Array (N) then
5187 return True;
5188 end if;
5190 -- Case of indexed component reference: test whether prefix is unaligned
5192 if Nkind (N) = N_Indexed_Component then
5193 return Is_Possibly_Unaligned_Object (Prefix (N));
5195 -- Case of selected component reference
5197 elsif Nkind (N) = N_Selected_Component then
5198 declare
5199 P : constant Node_Id := Prefix (N);
5200 C : constant Entity_Id := Entity (Selector_Name (N));
5201 M : Nat;
5202 S : Nat;
5204 begin
5205 -- If component reference is for an array with non-static bounds,
5206 -- then it is always aligned: we can only process unaligned arrays
5207 -- with static bounds (more precisely compile time known bounds).
5209 if Is_Array_Type (T)
5210 and then not Compile_Time_Known_Bounds (T)
5211 then
5212 return False;
5213 end if;
5215 -- If component is aliased, it is definitely properly aligned
5217 if Is_Aliased (C) then
5218 return False;
5219 end if;
5221 -- If component is for a type implemented as a scalar, and the
5222 -- record is packed, and the component is other than the first
5223 -- component of the record, then the component may be unaligned.
5225 if Is_Packed (Etype (P))
5226 and then Represented_As_Scalar (Etype (C))
5227 and then First_Entity (Scope (C)) /= C
5228 then
5229 return True;
5230 end if;
5232 -- Compute maximum possible alignment for T
5234 -- If alignment is known, then that settles things
5236 if Known_Alignment (T) then
5237 M := UI_To_Int (Alignment (T));
5239 -- If alignment is not known, tentatively set max alignment
5241 else
5242 M := Ttypes.Maximum_Alignment;
5244 -- We can reduce this if the Esize is known since the default
5245 -- alignment will never be more than the smallest power of 2
5246 -- that does not exceed this Esize value.
5248 if Known_Esize (T) then
5249 S := UI_To_Int (Esize (T));
5251 while (M / 2) >= S loop
5252 M := M / 2;
5253 end loop;
5254 end if;
5255 end if;
5257 -- The following code is historical, it used to be present but it
5258 -- is too cautious, because the front-end does not know the proper
5259 -- default alignments for the target. Also, if the alignment is
5260 -- not known, the front end can't know in any case. If a copy is
5261 -- needed, the back-end will take care of it. This whole section
5262 -- including this comment can be removed later ???
5264 -- If the component reference is for a record that has a specified
5265 -- alignment, and we either know it is too small, or cannot tell,
5266 -- then the component may be unaligned.
5268 -- What is the following commented out code ???
5270 -- if Known_Alignment (Etype (P))
5271 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
5272 -- and then M > Alignment (Etype (P))
5273 -- then
5274 -- return True;
5275 -- end if;
5277 -- Case of component clause present which may specify an
5278 -- unaligned position.
5280 if Present (Component_Clause (C)) then
5282 -- Otherwise we can do a test to make sure that the actual
5283 -- start position in the record, and the length, are both
5284 -- consistent with the required alignment. If not, we know
5285 -- that we are unaligned.
5287 declare
5288 Align_In_Bits : constant Nat := M * System_Storage_Unit;
5289 begin
5290 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
5291 or else Esize (C) mod Align_In_Bits /= 0
5292 then
5293 return True;
5294 end if;
5295 end;
5296 end if;
5298 -- Otherwise, for a component reference, test prefix
5300 return Is_Possibly_Unaligned_Object (P);
5301 end;
5303 -- If not a component reference, must be aligned
5305 else
5306 return False;
5307 end if;
5308 end Is_Possibly_Unaligned_Object;
5310 ---------------------------------
5311 -- Is_Possibly_Unaligned_Slice --
5312 ---------------------------------
5314 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
5315 begin
5316 -- Go to renamed object
5318 if Is_Entity_Name (N)
5319 and then Is_Object (Entity (N))
5320 and then Present (Renamed_Object (Entity (N)))
5321 then
5322 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
5323 end if;
5325 -- The reference must be a slice
5327 if Nkind (N) /= N_Slice then
5328 return False;
5329 end if;
5331 -- We only need to worry if the target has strict alignment
5333 if not Target_Strict_Alignment then
5334 return False;
5335 end if;
5337 -- If it is a slice, then look at the array type being sliced
5339 declare
5340 Sarr : constant Node_Id := Prefix (N);
5341 -- Prefix of the slice, i.e. the array being sliced
5343 Styp : constant Entity_Id := Etype (Prefix (N));
5344 -- Type of the array being sliced
5346 Pref : Node_Id;
5347 Ptyp : Entity_Id;
5349 begin
5350 -- The problems arise if the array object that is being sliced
5351 -- is a component of a record or array, and we cannot guarantee
5352 -- the alignment of the array within its containing object.
5354 -- To investigate this, we look at successive prefixes to see
5355 -- if we have a worrisome indexed or selected component.
5357 Pref := Sarr;
5358 loop
5359 -- Case of array is part of an indexed component reference
5361 if Nkind (Pref) = N_Indexed_Component then
5362 Ptyp := Etype (Prefix (Pref));
5364 -- The only problematic case is when the array is packed, in
5365 -- which case we really know nothing about the alignment of
5366 -- individual components.
5368 if Is_Bit_Packed_Array (Ptyp) then
5369 return True;
5370 end if;
5372 -- Case of array is part of a selected component reference
5374 elsif Nkind (Pref) = N_Selected_Component then
5375 Ptyp := Etype (Prefix (Pref));
5377 -- We are definitely in trouble if the record in question
5378 -- has an alignment, and either we know this alignment is
5379 -- inconsistent with the alignment of the slice, or we don't
5380 -- know what the alignment of the slice should be.
5382 if Known_Alignment (Ptyp)
5383 and then (Unknown_Alignment (Styp)
5384 or else Alignment (Styp) > Alignment (Ptyp))
5385 then
5386 return True;
5387 end if;
5389 -- We are in potential trouble if the record type is packed.
5390 -- We could special case when we know that the array is the
5391 -- first component, but that's not such a simple case ???
5393 if Is_Packed (Ptyp) then
5394 return True;
5395 end if;
5397 -- We are in trouble if there is a component clause, and
5398 -- either we do not know the alignment of the slice, or
5399 -- the alignment of the slice is inconsistent with the
5400 -- bit position specified by the component clause.
5402 declare
5403 Field : constant Entity_Id := Entity (Selector_Name (Pref));
5404 begin
5405 if Present (Component_Clause (Field))
5406 and then
5407 (Unknown_Alignment (Styp)
5408 or else
5409 (Component_Bit_Offset (Field) mod
5410 (System_Storage_Unit * Alignment (Styp))) /= 0)
5411 then
5412 return True;
5413 end if;
5414 end;
5416 -- For cases other than selected or indexed components we know we
5417 -- are OK, since no issues arise over alignment.
5419 else
5420 return False;
5421 end if;
5423 -- We processed an indexed component or selected component
5424 -- reference that looked safe, so keep checking prefixes.
5426 Pref := Prefix (Pref);
5427 end loop;
5428 end;
5429 end Is_Possibly_Unaligned_Slice;
5431 -------------------------------
5432 -- Is_Related_To_Func_Return --
5433 -------------------------------
5435 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
5436 Expr : constant Node_Id := Related_Expression (Id);
5437 begin
5438 return
5439 Present (Expr)
5440 and then Nkind (Expr) = N_Explicit_Dereference
5441 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
5442 end Is_Related_To_Func_Return;
5444 --------------------------------
5445 -- Is_Ref_To_Bit_Packed_Array --
5446 --------------------------------
5448 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
5449 Result : Boolean;
5450 Expr : Node_Id;
5452 begin
5453 if Is_Entity_Name (N)
5454 and then Is_Object (Entity (N))
5455 and then Present (Renamed_Object (Entity (N)))
5456 then
5457 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
5458 end if;
5460 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5461 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
5462 Result := True;
5463 else
5464 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
5465 end if;
5467 if Result and then Nkind (N) = N_Indexed_Component then
5468 Expr := First (Expressions (N));
5469 while Present (Expr) loop
5470 Force_Evaluation (Expr);
5471 Next (Expr);
5472 end loop;
5473 end if;
5475 return Result;
5477 else
5478 return False;
5479 end if;
5480 end Is_Ref_To_Bit_Packed_Array;
5482 --------------------------------
5483 -- Is_Ref_To_Bit_Packed_Slice --
5484 --------------------------------
5486 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
5487 begin
5488 if Nkind (N) = N_Type_Conversion then
5489 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
5491 elsif Is_Entity_Name (N)
5492 and then Is_Object (Entity (N))
5493 and then Present (Renamed_Object (Entity (N)))
5494 then
5495 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
5497 elsif Nkind (N) = N_Slice
5498 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
5499 then
5500 return True;
5502 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5503 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
5505 else
5506 return False;
5507 end if;
5508 end Is_Ref_To_Bit_Packed_Slice;
5510 -----------------------
5511 -- Is_Renamed_Object --
5512 -----------------------
5514 function Is_Renamed_Object (N : Node_Id) return Boolean is
5515 Pnod : constant Node_Id := Parent (N);
5516 Kind : constant Node_Kind := Nkind (Pnod);
5517 begin
5518 if Kind = N_Object_Renaming_Declaration then
5519 return True;
5520 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
5521 return Is_Renamed_Object (Pnod);
5522 else
5523 return False;
5524 end if;
5525 end Is_Renamed_Object;
5527 --------------------------------------
5528 -- Is_Secondary_Stack_BIP_Func_Call --
5529 --------------------------------------
5531 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
5532 Alloc_Nam : Name_Id := No_Name;
5533 Actual : Node_Id;
5534 Call : Node_Id := Expr;
5535 Formal : Node_Id;
5536 Param : Node_Id;
5538 begin
5539 -- Build-in-place calls usually appear in 'reference format. Note that
5540 -- the accessibility check machinery may add an extra 'reference due to
5541 -- side effect removal.
5543 while Nkind (Call) = N_Reference loop
5544 Call := Prefix (Call);
5545 end loop;
5547 if Nkind_In (Call, N_Qualified_Expression,
5548 N_Unchecked_Type_Conversion)
5549 then
5550 Call := Expression (Call);
5551 end if;
5553 if Is_Build_In_Place_Function_Call (Call) then
5555 -- Examine all parameter associations of the function call
5557 Param := First (Parameter_Associations (Call));
5558 while Present (Param) loop
5559 if Nkind (Param) = N_Parameter_Association
5560 and then Nkind (Selector_Name (Param)) = N_Identifier
5561 then
5562 Formal := Selector_Name (Param);
5563 Actual := Explicit_Actual_Parameter (Param);
5565 -- Construct the name of formal BIPalloc. It is much easier to
5566 -- extract the name of the function using an arbitrary formal's
5567 -- scope rather than the Name field of Call.
5569 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
5570 Alloc_Nam :=
5571 New_External_Name
5572 (Chars (Scope (Entity (Formal))),
5573 BIP_Formal_Suffix (BIP_Alloc_Form));
5574 end if;
5576 -- A match for BIPalloc => 2 has been found
5578 if Chars (Formal) = Alloc_Nam
5579 and then Nkind (Actual) = N_Integer_Literal
5580 and then Intval (Actual) = Uint_2
5581 then
5582 return True;
5583 end if;
5584 end if;
5586 Next (Param);
5587 end loop;
5588 end if;
5590 return False;
5591 end Is_Secondary_Stack_BIP_Func_Call;
5593 -------------------------------------
5594 -- Is_Tag_To_Class_Wide_Conversion --
5595 -------------------------------------
5597 function Is_Tag_To_Class_Wide_Conversion
5598 (Obj_Id : Entity_Id) return Boolean
5600 Expr : constant Node_Id := Expression (Parent (Obj_Id));
5602 begin
5603 return
5604 Is_Class_Wide_Type (Etype (Obj_Id))
5605 and then Present (Expr)
5606 and then Nkind (Expr) = N_Unchecked_Type_Conversion
5607 and then Etype (Expression (Expr)) = RTE (RE_Tag);
5608 end Is_Tag_To_Class_Wide_Conversion;
5610 ----------------------------
5611 -- Is_Untagged_Derivation --
5612 ----------------------------
5614 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
5615 begin
5616 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
5617 or else
5618 (Is_Private_Type (T) and then Present (Full_View (T))
5619 and then not Is_Tagged_Type (Full_View (T))
5620 and then Is_Derived_Type (Full_View (T))
5621 and then Etype (Full_View (T)) /= T);
5622 end Is_Untagged_Derivation;
5624 ---------------------------
5625 -- Is_Volatile_Reference --
5626 ---------------------------
5628 function Is_Volatile_Reference (N : Node_Id) return Boolean is
5629 begin
5630 -- Only source references are to be treated as volatile, internally
5631 -- generated stuff cannot have volatile external effects.
5633 if not Comes_From_Source (N) then
5634 return False;
5636 -- Never true for reference to a type
5638 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
5639 return False;
5641 -- True if object reference with volatile type
5643 elsif Is_Volatile_Object (N) then
5644 return True;
5646 -- True if reference to volatile entity
5648 elsif Is_Entity_Name (N) then
5649 return Treat_As_Volatile (Entity (N));
5651 -- True for slice of volatile array
5653 elsif Nkind (N) = N_Slice then
5654 return Is_Volatile_Reference (Prefix (N));
5656 -- True if volatile component
5658 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
5659 if (Is_Entity_Name (Prefix (N))
5660 and then Has_Volatile_Components (Entity (Prefix (N))))
5661 or else (Present (Etype (Prefix (N)))
5662 and then Has_Volatile_Components (Etype (Prefix (N))))
5663 then
5664 return True;
5665 else
5666 return Is_Volatile_Reference (Prefix (N));
5667 end if;
5669 -- Otherwise false
5671 else
5672 return False;
5673 end if;
5674 end Is_Volatile_Reference;
5676 --------------------------
5677 -- Is_VM_By_Copy_Actual --
5678 --------------------------
5680 function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean is
5681 begin
5682 return VM_Target /= No_VM
5683 and then (Nkind (N) = N_Slice
5684 or else
5685 (Nkind (N) = N_Identifier
5686 and then Present (Renamed_Object (Entity (N)))
5687 and then Nkind (Renamed_Object (Entity (N))) =
5688 N_Slice));
5689 end Is_VM_By_Copy_Actual;
5691 --------------------
5692 -- Kill_Dead_Code --
5693 --------------------
5695 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
5696 W : Boolean := Warn;
5697 -- Set False if warnings suppressed
5699 begin
5700 if Present (N) then
5701 Remove_Warning_Messages (N);
5703 -- Generate warning if appropriate
5705 if W then
5707 -- We suppress the warning if this code is under control of an
5708 -- if statement, whose condition is a simple identifier, and
5709 -- either we are in an instance, or warnings off is set for this
5710 -- identifier. The reason for killing it in the instance case is
5711 -- that it is common and reasonable for code to be deleted in
5712 -- instances for various reasons.
5714 -- Could we use Is_Statically_Unevaluated here???
5716 if Nkind (Parent (N)) = N_If_Statement then
5717 declare
5718 C : constant Node_Id := Condition (Parent (N));
5719 begin
5720 if Nkind (C) = N_Identifier
5721 and then
5722 (In_Instance
5723 or else (Present (Entity (C))
5724 and then Has_Warnings_Off (Entity (C))))
5725 then
5726 W := False;
5727 end if;
5728 end;
5729 end if;
5731 -- Generate warning if not suppressed
5733 if W then
5734 Error_Msg_F
5735 ("?t?this code can never be executed and has been deleted!",
5737 end if;
5738 end if;
5740 -- Recurse into block statements and bodies to process declarations
5741 -- and statements.
5743 if Nkind (N) = N_Block_Statement
5744 or else Nkind (N) = N_Subprogram_Body
5745 or else Nkind (N) = N_Package_Body
5746 then
5747 Kill_Dead_Code (Declarations (N), False);
5748 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
5750 if Nkind (N) = N_Subprogram_Body then
5751 Set_Is_Eliminated (Defining_Entity (N));
5752 end if;
5754 elsif Nkind (N) = N_Package_Declaration then
5755 Kill_Dead_Code (Visible_Declarations (Specification (N)));
5756 Kill_Dead_Code (Private_Declarations (Specification (N)));
5758 -- ??? After this point, Delete_Tree has been called on all
5759 -- declarations in Specification (N), so references to entities
5760 -- therein look suspicious.
5762 declare
5763 E : Entity_Id := First_Entity (Defining_Entity (N));
5765 begin
5766 while Present (E) loop
5767 if Ekind (E) = E_Operator then
5768 Set_Is_Eliminated (E);
5769 end if;
5771 Next_Entity (E);
5772 end loop;
5773 end;
5775 -- Recurse into composite statement to kill individual statements in
5776 -- particular instantiations.
5778 elsif Nkind (N) = N_If_Statement then
5779 Kill_Dead_Code (Then_Statements (N));
5780 Kill_Dead_Code (Elsif_Parts (N));
5781 Kill_Dead_Code (Else_Statements (N));
5783 elsif Nkind (N) = N_Loop_Statement then
5784 Kill_Dead_Code (Statements (N));
5786 elsif Nkind (N) = N_Case_Statement then
5787 declare
5788 Alt : Node_Id;
5789 begin
5790 Alt := First (Alternatives (N));
5791 while Present (Alt) loop
5792 Kill_Dead_Code (Statements (Alt));
5793 Next (Alt);
5794 end loop;
5795 end;
5797 elsif Nkind (N) = N_Case_Statement_Alternative then
5798 Kill_Dead_Code (Statements (N));
5800 -- Deal with dead instances caused by deleting instantiations
5802 elsif Nkind (N) in N_Generic_Instantiation then
5803 Remove_Dead_Instance (N);
5804 end if;
5805 end if;
5806 end Kill_Dead_Code;
5808 -- Case where argument is a list of nodes to be killed
5810 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
5811 N : Node_Id;
5812 W : Boolean;
5814 begin
5815 W := Warn;
5817 if Is_Non_Empty_List (L) then
5818 N := First (L);
5819 while Present (N) loop
5820 Kill_Dead_Code (N, W);
5821 W := False;
5822 Next (N);
5823 end loop;
5824 end if;
5825 end Kill_Dead_Code;
5827 ------------------------
5828 -- Known_Non_Negative --
5829 ------------------------
5831 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
5832 begin
5833 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
5834 return True;
5836 else
5837 declare
5838 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
5839 begin
5840 return
5841 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
5842 end;
5843 end if;
5844 end Known_Non_Negative;
5846 --------------------
5847 -- Known_Non_Null --
5848 --------------------
5850 function Known_Non_Null (N : Node_Id) return Boolean is
5851 begin
5852 -- Checks for case where N is an entity reference
5854 if Is_Entity_Name (N) and then Present (Entity (N)) then
5855 declare
5856 E : constant Entity_Id := Entity (N);
5857 Op : Node_Kind;
5858 Val : Node_Id;
5860 begin
5861 -- First check if we are in decisive conditional
5863 Get_Current_Value_Condition (N, Op, Val);
5865 if Known_Null (Val) then
5866 if Op = N_Op_Eq then
5867 return False;
5868 elsif Op = N_Op_Ne then
5869 return True;
5870 end if;
5871 end if;
5873 -- If OK to do replacement, test Is_Known_Non_Null flag
5875 if OK_To_Do_Constant_Replacement (E) then
5876 return Is_Known_Non_Null (E);
5878 -- Otherwise if not safe to do replacement, then say so
5880 else
5881 return False;
5882 end if;
5883 end;
5885 -- True if access attribute
5887 elsif Nkind (N) = N_Attribute_Reference
5888 and then Nam_In (Attribute_Name (N), Name_Access,
5889 Name_Unchecked_Access,
5890 Name_Unrestricted_Access)
5891 then
5892 return True;
5894 -- True if allocator
5896 elsif Nkind (N) = N_Allocator then
5897 return True;
5899 -- For a conversion, true if expression is known non-null
5901 elsif Nkind (N) = N_Type_Conversion then
5902 return Known_Non_Null (Expression (N));
5904 -- Above are all cases where the value could be determined to be
5905 -- non-null. In all other cases, we don't know, so return False.
5907 else
5908 return False;
5909 end if;
5910 end Known_Non_Null;
5912 ----------------
5913 -- Known_Null --
5914 ----------------
5916 function Known_Null (N : Node_Id) return Boolean is
5917 begin
5918 -- Checks for case where N is an entity reference
5920 if Is_Entity_Name (N) and then Present (Entity (N)) then
5921 declare
5922 E : constant Entity_Id := Entity (N);
5923 Op : Node_Kind;
5924 Val : Node_Id;
5926 begin
5927 -- Constant null value is for sure null
5929 if Ekind (E) = E_Constant
5930 and then Known_Null (Constant_Value (E))
5931 then
5932 return True;
5933 end if;
5935 -- First check if we are in decisive conditional
5937 Get_Current_Value_Condition (N, Op, Val);
5939 if Known_Null (Val) then
5940 if Op = N_Op_Eq then
5941 return True;
5942 elsif Op = N_Op_Ne then
5943 return False;
5944 end if;
5945 end if;
5947 -- If OK to do replacement, test Is_Known_Null flag
5949 if OK_To_Do_Constant_Replacement (E) then
5950 return Is_Known_Null (E);
5952 -- Otherwise if not safe to do replacement, then say so
5954 else
5955 return False;
5956 end if;
5957 end;
5959 -- True if explicit reference to null
5961 elsif Nkind (N) = N_Null then
5962 return True;
5964 -- For a conversion, true if expression is known null
5966 elsif Nkind (N) = N_Type_Conversion then
5967 return Known_Null (Expression (N));
5969 -- Above are all cases where the value could be determined to be null.
5970 -- In all other cases, we don't know, so return False.
5972 else
5973 return False;
5974 end if;
5975 end Known_Null;
5977 -----------------------------
5978 -- Make_CW_Equivalent_Type --
5979 -----------------------------
5981 -- Create a record type used as an equivalent of any member of the class
5982 -- which takes its size from exp.
5984 -- Generate the following code:
5986 -- type Equiv_T is record
5987 -- _parent : T (List of discriminant constraints taken from Exp);
5988 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
5989 -- end Equiv_T;
5991 -- ??? Note that this type does not guarantee same alignment as all
5992 -- derived types
5994 function Make_CW_Equivalent_Type
5995 (T : Entity_Id;
5996 E : Node_Id) return Entity_Id
5998 Loc : constant Source_Ptr := Sloc (E);
5999 Root_Typ : constant Entity_Id := Root_Type (T);
6000 List_Def : constant List_Id := Empty_List;
6001 Comp_List : constant List_Id := New_List;
6002 Equiv_Type : Entity_Id;
6003 Range_Type : Entity_Id;
6004 Str_Type : Entity_Id;
6005 Constr_Root : Entity_Id;
6006 Sizexpr : Node_Id;
6008 begin
6009 -- If the root type is already constrained, there are no discriminants
6010 -- in the expression.
6012 if not Has_Discriminants (Root_Typ)
6013 or else Is_Constrained (Root_Typ)
6014 then
6015 Constr_Root := Root_Typ;
6016 else
6017 Constr_Root := Make_Temporary (Loc, 'R');
6019 -- subtype cstr__n is T (List of discr constraints taken from Exp)
6021 Append_To (List_Def,
6022 Make_Subtype_Declaration (Loc,
6023 Defining_Identifier => Constr_Root,
6024 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
6025 end if;
6027 -- Generate the range subtype declaration
6029 Range_Type := Make_Temporary (Loc, 'G');
6031 if not Is_Interface (Root_Typ) then
6033 -- subtype rg__xx is
6034 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
6036 Sizexpr :=
6037 Make_Op_Subtract (Loc,
6038 Left_Opnd =>
6039 Make_Attribute_Reference (Loc,
6040 Prefix =>
6041 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6042 Attribute_Name => Name_Size),
6043 Right_Opnd =>
6044 Make_Attribute_Reference (Loc,
6045 Prefix => New_Occurrence_Of (Constr_Root, Loc),
6046 Attribute_Name => Name_Object_Size));
6047 else
6048 -- subtype rg__xx is
6049 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
6051 Sizexpr :=
6052 Make_Attribute_Reference (Loc,
6053 Prefix =>
6054 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
6055 Attribute_Name => Name_Size);
6056 end if;
6058 Set_Paren_Count (Sizexpr, 1);
6060 Append_To (List_Def,
6061 Make_Subtype_Declaration (Loc,
6062 Defining_Identifier => Range_Type,
6063 Subtype_Indication =>
6064 Make_Subtype_Indication (Loc,
6065 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
6066 Constraint => Make_Range_Constraint (Loc,
6067 Range_Expression =>
6068 Make_Range (Loc,
6069 Low_Bound => Make_Integer_Literal (Loc, 1),
6070 High_Bound =>
6071 Make_Op_Divide (Loc,
6072 Left_Opnd => Sizexpr,
6073 Right_Opnd => Make_Integer_Literal (Loc,
6074 Intval => System_Storage_Unit)))))));
6076 -- subtype str__nn is Storage_Array (rg__x);
6078 Str_Type := Make_Temporary (Loc, 'S');
6079 Append_To (List_Def,
6080 Make_Subtype_Declaration (Loc,
6081 Defining_Identifier => Str_Type,
6082 Subtype_Indication =>
6083 Make_Subtype_Indication (Loc,
6084 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
6085 Constraint =>
6086 Make_Index_Or_Discriminant_Constraint (Loc,
6087 Constraints =>
6088 New_List (New_Occurrence_Of (Range_Type, Loc))))));
6090 -- type Equiv_T is record
6091 -- [ _parent : Tnn; ]
6092 -- E : Str_Type;
6093 -- end Equiv_T;
6095 Equiv_Type := Make_Temporary (Loc, 'T');
6096 Set_Ekind (Equiv_Type, E_Record_Type);
6097 Set_Parent_Subtype (Equiv_Type, Constr_Root);
6099 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
6100 -- treatment for this type. In particular, even though _parent's type
6101 -- is a controlled type or contains controlled components, we do not
6102 -- want to set Has_Controlled_Component on it to avoid making it gain
6103 -- an unwanted _controller component.
6105 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
6107 -- A class-wide equivalent type does not require initialization
6109 Set_Suppress_Initialization (Equiv_Type);
6111 if not Is_Interface (Root_Typ) then
6112 Append_To (Comp_List,
6113 Make_Component_Declaration (Loc,
6114 Defining_Identifier =>
6115 Make_Defining_Identifier (Loc, Name_uParent),
6116 Component_Definition =>
6117 Make_Component_Definition (Loc,
6118 Aliased_Present => False,
6119 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
6120 end if;
6122 Append_To (Comp_List,
6123 Make_Component_Declaration (Loc,
6124 Defining_Identifier => Make_Temporary (Loc, 'C'),
6125 Component_Definition =>
6126 Make_Component_Definition (Loc,
6127 Aliased_Present => False,
6128 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
6130 Append_To (List_Def,
6131 Make_Full_Type_Declaration (Loc,
6132 Defining_Identifier => Equiv_Type,
6133 Type_Definition =>
6134 Make_Record_Definition (Loc,
6135 Component_List =>
6136 Make_Component_List (Loc,
6137 Component_Items => Comp_List,
6138 Variant_Part => Empty))));
6140 -- Suppress all checks during the analysis of the expanded code to avoid
6141 -- the generation of spurious warnings under ZFP run-time.
6143 Insert_Actions (E, List_Def, Suppress => All_Checks);
6144 return Equiv_Type;
6145 end Make_CW_Equivalent_Type;
6147 -------------------------
6148 -- Make_Invariant_Call --
6149 -------------------------
6151 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
6152 Loc : constant Source_Ptr := Sloc (Expr);
6153 Typ : Entity_Id;
6155 begin
6156 Typ := Etype (Expr);
6158 -- Subtypes may be subject to invariants coming from their respective
6159 -- base types. The subtype may be fully or partially private.
6161 if Ekind_In (Typ, E_Array_Subtype,
6162 E_Private_Subtype,
6163 E_Record_Subtype,
6164 E_Record_Subtype_With_Private)
6165 then
6166 Typ := Base_Type (Typ);
6167 end if;
6169 pragma Assert
6170 (Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)));
6172 return
6173 Make_Procedure_Call_Statement (Loc,
6174 Name =>
6175 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
6176 Parameter_Associations => New_List (Relocate_Node (Expr)));
6177 end Make_Invariant_Call;
6179 ------------------------
6180 -- Make_Literal_Range --
6181 ------------------------
6183 function Make_Literal_Range
6184 (Loc : Source_Ptr;
6185 Literal_Typ : Entity_Id) return Node_Id
6187 Lo : constant Node_Id :=
6188 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
6189 Index : constant Entity_Id := Etype (Lo);
6191 Hi : Node_Id;
6192 Length_Expr : constant Node_Id :=
6193 Make_Op_Subtract (Loc,
6194 Left_Opnd =>
6195 Make_Integer_Literal (Loc,
6196 Intval => String_Literal_Length (Literal_Typ)),
6197 Right_Opnd =>
6198 Make_Integer_Literal (Loc, 1));
6200 begin
6201 Set_Analyzed (Lo, False);
6203 if Is_Integer_Type (Index) then
6204 Hi :=
6205 Make_Op_Add (Loc,
6206 Left_Opnd => New_Copy_Tree (Lo),
6207 Right_Opnd => Length_Expr);
6208 else
6209 Hi :=
6210 Make_Attribute_Reference (Loc,
6211 Attribute_Name => Name_Val,
6212 Prefix => New_Occurrence_Of (Index, Loc),
6213 Expressions => New_List (
6214 Make_Op_Add (Loc,
6215 Left_Opnd =>
6216 Make_Attribute_Reference (Loc,
6217 Attribute_Name => Name_Pos,
6218 Prefix => New_Occurrence_Of (Index, Loc),
6219 Expressions => New_List (New_Copy_Tree (Lo))),
6220 Right_Opnd => Length_Expr)));
6221 end if;
6223 return
6224 Make_Range (Loc,
6225 Low_Bound => Lo,
6226 High_Bound => Hi);
6227 end Make_Literal_Range;
6229 --------------------------
6230 -- Make_Non_Empty_Check --
6231 --------------------------
6233 function Make_Non_Empty_Check
6234 (Loc : Source_Ptr;
6235 N : Node_Id) return Node_Id
6237 begin
6238 return
6239 Make_Op_Ne (Loc,
6240 Left_Opnd =>
6241 Make_Attribute_Reference (Loc,
6242 Attribute_Name => Name_Length,
6243 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
6244 Right_Opnd =>
6245 Make_Integer_Literal (Loc, 0));
6246 end Make_Non_Empty_Check;
6248 -------------------------
6249 -- Make_Predicate_Call --
6250 -------------------------
6252 function Make_Predicate_Call
6253 (Typ : Entity_Id;
6254 Expr : Node_Id;
6255 Mem : Boolean := False) return Node_Id
6257 Loc : constant Source_Ptr := Sloc (Expr);
6259 begin
6260 pragma Assert (Present (Predicate_Function (Typ)));
6262 -- Call special membership version if requested and available
6264 if Mem then
6265 declare
6266 PFM : constant Entity_Id := Predicate_Function_M (Typ);
6267 begin
6268 if Present (PFM) then
6269 return
6270 Make_Function_Call (Loc,
6271 Name => New_Occurrence_Of (PFM, Loc),
6272 Parameter_Associations => New_List (Relocate_Node (Expr)));
6273 end if;
6274 end;
6275 end if;
6277 -- Case of calling normal predicate function
6279 return
6280 Make_Function_Call (Loc,
6281 Name =>
6282 New_Occurrence_Of (Predicate_Function (Typ), Loc),
6283 Parameter_Associations => New_List (Relocate_Node (Expr)));
6284 end Make_Predicate_Call;
6286 --------------------------
6287 -- Make_Predicate_Check --
6288 --------------------------
6290 function Make_Predicate_Check
6291 (Typ : Entity_Id;
6292 Expr : Node_Id) return Node_Id
6294 Loc : constant Source_Ptr := Sloc (Expr);
6295 Nam : Name_Id;
6297 begin
6298 -- If predicate checks are suppressed, then return a null statement.
6299 -- For this call, we check only the scope setting. If the caller wants
6300 -- to check a specific entity's setting, they must do it manually.
6302 if Predicate_Checks_Suppressed (Empty) then
6303 return Make_Null_Statement (Loc);
6304 end if;
6306 -- Do not generate a check within an internal subprogram (stream
6307 -- functions and the like, including including predicate functions).
6309 if Within_Internal_Subprogram then
6310 return Make_Null_Statement (Loc);
6311 end if;
6313 -- Compute proper name to use, we need to get this right so that the
6314 -- right set of check policies apply to the Check pragma we are making.
6316 if Has_Dynamic_Predicate_Aspect (Typ) then
6317 Nam := Name_Dynamic_Predicate;
6318 elsif Has_Static_Predicate_Aspect (Typ) then
6319 Nam := Name_Static_Predicate;
6320 else
6321 Nam := Name_Predicate;
6322 end if;
6324 return
6325 Make_Pragma (Loc,
6326 Pragma_Identifier => Make_Identifier (Loc, Name_Check),
6327 Pragma_Argument_Associations => New_List (
6328 Make_Pragma_Argument_Association (Loc,
6329 Expression => Make_Identifier (Loc, Nam)),
6330 Make_Pragma_Argument_Association (Loc,
6331 Expression => Make_Predicate_Call (Typ, Expr))));
6332 end Make_Predicate_Check;
6334 ----------------------------
6335 -- Make_Subtype_From_Expr --
6336 ----------------------------
6338 -- 1. If Expr is an unconstrained array expression, creates
6339 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
6341 -- 2. If Expr is a unconstrained discriminated type expression, creates
6342 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
6344 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
6346 function Make_Subtype_From_Expr
6347 (E : Node_Id;
6348 Unc_Typ : Entity_Id) return Node_Id
6350 Loc : constant Source_Ptr := Sloc (E);
6351 List_Constr : constant List_Id := New_List;
6352 D : Entity_Id;
6354 Full_Subtyp : Entity_Id;
6355 Priv_Subtyp : Entity_Id;
6356 Utyp : Entity_Id;
6357 Full_Exp : Node_Id;
6359 begin
6360 if Is_Private_Type (Unc_Typ)
6361 and then Has_Unknown_Discriminants (Unc_Typ)
6362 then
6363 -- Prepare the subtype completion, Go to base type to
6364 -- find underlying type, because the type may be a generic
6365 -- actual or an explicit subtype.
6367 Utyp := Underlying_Type (Base_Type (Unc_Typ));
6368 Full_Subtyp := Make_Temporary (Loc, 'C');
6369 Full_Exp :=
6370 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
6371 Set_Parent (Full_Exp, Parent (E));
6373 Priv_Subtyp := Make_Temporary (Loc, 'P');
6375 Insert_Action (E,
6376 Make_Subtype_Declaration (Loc,
6377 Defining_Identifier => Full_Subtyp,
6378 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
6380 -- Define the dummy private subtype
6382 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
6383 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
6384 Set_Scope (Priv_Subtyp, Full_Subtyp);
6385 Set_Is_Constrained (Priv_Subtyp);
6386 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
6387 Set_Is_Itype (Priv_Subtyp);
6388 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
6390 if Is_Tagged_Type (Priv_Subtyp) then
6391 Set_Class_Wide_Type
6392 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
6393 Set_Direct_Primitive_Operations (Priv_Subtyp,
6394 Direct_Primitive_Operations (Unc_Typ));
6395 end if;
6397 Set_Full_View (Priv_Subtyp, Full_Subtyp);
6399 return New_Occurrence_Of (Priv_Subtyp, Loc);
6401 elsif Is_Array_Type (Unc_Typ) then
6402 for J in 1 .. Number_Dimensions (Unc_Typ) loop
6403 Append_To (List_Constr,
6404 Make_Range (Loc,
6405 Low_Bound =>
6406 Make_Attribute_Reference (Loc,
6407 Prefix => Duplicate_Subexpr_No_Checks (E),
6408 Attribute_Name => Name_First,
6409 Expressions => New_List (
6410 Make_Integer_Literal (Loc, J))),
6412 High_Bound =>
6413 Make_Attribute_Reference (Loc,
6414 Prefix => Duplicate_Subexpr_No_Checks (E),
6415 Attribute_Name => Name_Last,
6416 Expressions => New_List (
6417 Make_Integer_Literal (Loc, J)))));
6418 end loop;
6420 elsif Is_Class_Wide_Type (Unc_Typ) then
6421 declare
6422 CW_Subtype : Entity_Id;
6423 EQ_Typ : Entity_Id := Empty;
6425 begin
6426 -- A class-wide equivalent type is not needed when VM_Target
6427 -- because the VM back-ends handle the class-wide object
6428 -- initialization itself (and doesn't need or want the
6429 -- additional intermediate type to handle the assignment).
6431 if Expander_Active and then Tagged_Type_Expansion then
6433 -- If this is the class-wide type of a completion that is a
6434 -- record subtype, set the type of the class-wide type to be
6435 -- the full base type, for use in the expanded code for the
6436 -- equivalent type. Should this be done earlier when the
6437 -- completion is analyzed ???
6439 if Is_Private_Type (Etype (Unc_Typ))
6440 and then
6441 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
6442 then
6443 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
6444 end if;
6446 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
6447 end if;
6449 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
6450 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
6451 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
6453 return New_Occurrence_Of (CW_Subtype, Loc);
6454 end;
6456 -- Indefinite record type with discriminants
6458 else
6459 D := First_Discriminant (Unc_Typ);
6460 while Present (D) loop
6461 Append_To (List_Constr,
6462 Make_Selected_Component (Loc,
6463 Prefix => Duplicate_Subexpr_No_Checks (E),
6464 Selector_Name => New_Occurrence_Of (D, Loc)));
6466 Next_Discriminant (D);
6467 end loop;
6468 end if;
6470 return
6471 Make_Subtype_Indication (Loc,
6472 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
6473 Constraint =>
6474 Make_Index_Or_Discriminant_Constraint (Loc,
6475 Constraints => List_Constr));
6476 end Make_Subtype_From_Expr;
6478 ----------------------------
6479 -- Matching_Standard_Type --
6480 ----------------------------
6482 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
6483 pragma Assert (Is_Scalar_Type (Typ));
6484 Siz : constant Uint := Esize (Typ);
6486 begin
6487 -- Floating-point cases
6489 if Is_Floating_Point_Type (Typ) then
6490 if Siz <= Esize (Standard_Short_Float) then
6491 return Standard_Short_Float;
6492 elsif Siz <= Esize (Standard_Float) then
6493 return Standard_Float;
6494 elsif Siz <= Esize (Standard_Long_Float) then
6495 return Standard_Long_Float;
6496 elsif Siz <= Esize (Standard_Long_Long_Float) then
6497 return Standard_Long_Long_Float;
6498 else
6499 raise Program_Error;
6500 end if;
6502 -- Integer cases (includes fixed-point types)
6504 -- Unsigned integer cases (includes normal enumeration types)
6506 elsif Is_Unsigned_Type (Typ) then
6507 if Siz <= Esize (Standard_Short_Short_Unsigned) then
6508 return Standard_Short_Short_Unsigned;
6509 elsif Siz <= Esize (Standard_Short_Unsigned) then
6510 return Standard_Short_Unsigned;
6511 elsif Siz <= Esize (Standard_Unsigned) then
6512 return Standard_Unsigned;
6513 elsif Siz <= Esize (Standard_Long_Unsigned) then
6514 return Standard_Long_Unsigned;
6515 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
6516 return Standard_Long_Long_Unsigned;
6517 else
6518 raise Program_Error;
6519 end if;
6521 -- Signed integer cases
6523 else
6524 if Siz <= Esize (Standard_Short_Short_Integer) then
6525 return Standard_Short_Short_Integer;
6526 elsif Siz <= Esize (Standard_Short_Integer) then
6527 return Standard_Short_Integer;
6528 elsif Siz <= Esize (Standard_Integer) then
6529 return Standard_Integer;
6530 elsif Siz <= Esize (Standard_Long_Integer) then
6531 return Standard_Long_Integer;
6532 elsif Siz <= Esize (Standard_Long_Long_Integer) then
6533 return Standard_Long_Long_Integer;
6534 else
6535 raise Program_Error;
6536 end if;
6537 end if;
6538 end Matching_Standard_Type;
6540 -----------------------------
6541 -- May_Generate_Large_Temp --
6542 -----------------------------
6544 -- At the current time, the only types that we return False for (i.e. where
6545 -- we decide we know they cannot generate large temps) are ones where we
6546 -- know the size is 256 bits or less at compile time, and we are still not
6547 -- doing a thorough job on arrays and records ???
6549 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
6550 begin
6551 if not Size_Known_At_Compile_Time (Typ) then
6552 return False;
6554 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
6555 return False;
6557 elsif Is_Array_Type (Typ)
6558 and then Present (Packed_Array_Impl_Type (Typ))
6559 then
6560 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
6562 -- We could do more here to find other small types ???
6564 else
6565 return True;
6566 end if;
6567 end May_Generate_Large_Temp;
6569 ------------------------
6570 -- Needs_Finalization --
6571 ------------------------
6573 function Needs_Finalization (T : Entity_Id) return Boolean is
6574 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
6575 -- If type is not frozen yet, check explicitly among its components,
6576 -- because the Has_Controlled_Component flag is not necessarily set.
6578 -----------------------------------
6579 -- Has_Some_Controlled_Component --
6580 -----------------------------------
6582 function Has_Some_Controlled_Component
6583 (Rec : Entity_Id) return Boolean
6585 Comp : Entity_Id;
6587 begin
6588 if Has_Controlled_Component (Rec) then
6589 return True;
6591 elsif not Is_Frozen (Rec) then
6592 if Is_Record_Type (Rec) then
6593 Comp := First_Entity (Rec);
6595 while Present (Comp) loop
6596 if not Is_Type (Comp)
6597 and then Needs_Finalization (Etype (Comp))
6598 then
6599 return True;
6600 end if;
6602 Next_Entity (Comp);
6603 end loop;
6605 return False;
6607 elsif Is_Array_Type (Rec) then
6608 return Needs_Finalization (Component_Type (Rec));
6610 else
6611 return Has_Controlled_Component (Rec);
6612 end if;
6613 else
6614 return False;
6615 end if;
6616 end Has_Some_Controlled_Component;
6618 -- Start of processing for Needs_Finalization
6620 begin
6621 -- Certain run-time configurations and targets do not provide support
6622 -- for controlled types.
6624 if Restriction_Active (No_Finalization) then
6625 return False;
6627 -- C++, CIL and Java types are not considered controlled. It is assumed
6628 -- that the non-Ada side will handle their clean up.
6630 elsif Convention (T) = Convention_CIL
6631 or else Convention (T) = Convention_CPP
6632 or else Convention (T) = Convention_Java
6633 then
6634 return False;
6636 else
6637 -- Class-wide types are treated as controlled because derivations
6638 -- from the root type can introduce controlled components.
6640 return
6641 Is_Class_Wide_Type (T)
6642 or else Is_Controlled (T)
6643 or else Has_Controlled_Component (T)
6644 or else Has_Some_Controlled_Component (T)
6645 or else
6646 (Is_Concurrent_Type (T)
6647 and then Present (Corresponding_Record_Type (T))
6648 and then Needs_Finalization (Corresponding_Record_Type (T)));
6649 end if;
6650 end Needs_Finalization;
6652 ----------------------------
6653 -- Needs_Constant_Address --
6654 ----------------------------
6656 function Needs_Constant_Address
6657 (Decl : Node_Id;
6658 Typ : Entity_Id) return Boolean
6660 begin
6662 -- If we have no initialization of any kind, then we don't need to place
6663 -- any restrictions on the address clause, because the object will be
6664 -- elaborated after the address clause is evaluated. This happens if the
6665 -- declaration has no initial expression, or the type has no implicit
6666 -- initialization, or the object is imported.
6668 -- The same holds for all initialized scalar types and all access types.
6669 -- Packed bit arrays of size up to 64 are represented using a modular
6670 -- type with an initialization (to zero) and can be processed like other
6671 -- initialized scalar types.
6673 -- If the type is controlled, code to attach the object to a
6674 -- finalization chain is generated at the point of declaration, and
6675 -- therefore the elaboration of the object cannot be delayed: the
6676 -- address expression must be a constant.
6678 if No (Expression (Decl))
6679 and then not Needs_Finalization (Typ)
6680 and then
6681 (not Has_Non_Null_Base_Init_Proc (Typ)
6682 or else Is_Imported (Defining_Identifier (Decl)))
6683 then
6684 return False;
6686 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
6687 or else Is_Access_Type (Typ)
6688 or else
6689 (Is_Bit_Packed_Array (Typ)
6690 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
6691 then
6692 return False;
6694 else
6696 -- Otherwise, we require the address clause to be constant because
6697 -- the call to the initialization procedure (or the attach code) has
6698 -- to happen at the point of the declaration.
6700 -- Actually the IP call has been moved to the freeze actions anyway,
6701 -- so maybe we can relax this restriction???
6703 return True;
6704 end if;
6705 end Needs_Constant_Address;
6707 ----------------------------
6708 -- New_Class_Wide_Subtype --
6709 ----------------------------
6711 function New_Class_Wide_Subtype
6712 (CW_Typ : Entity_Id;
6713 N : Node_Id) return Entity_Id
6715 Res : constant Entity_Id := Create_Itype (E_Void, N);
6716 Res_Name : constant Name_Id := Chars (Res);
6717 Res_Scope : constant Entity_Id := Scope (Res);
6719 begin
6720 Copy_Node (CW_Typ, Res);
6721 Set_Comes_From_Source (Res, False);
6722 Set_Sloc (Res, Sloc (N));
6723 Set_Is_Itype (Res);
6724 Set_Associated_Node_For_Itype (Res, N);
6725 Set_Is_Public (Res, False); -- By default, may be changed below.
6726 Set_Public_Status (Res);
6727 Set_Chars (Res, Res_Name);
6728 Set_Scope (Res, Res_Scope);
6729 Set_Ekind (Res, E_Class_Wide_Subtype);
6730 Set_Next_Entity (Res, Empty);
6731 Set_Etype (Res, Base_Type (CW_Typ));
6732 Set_Is_Frozen (Res, False);
6733 Set_Freeze_Node (Res, Empty);
6734 return (Res);
6735 end New_Class_Wide_Subtype;
6737 --------------------------------
6738 -- Non_Limited_Designated_Type --
6739 ---------------------------------
6741 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
6742 Desig : constant Entity_Id := Designated_Type (T);
6743 begin
6744 if Ekind (Desig) = E_Incomplete_Type
6745 and then Present (Non_Limited_View (Desig))
6746 then
6747 return Non_Limited_View (Desig);
6748 else
6749 return Desig;
6750 end if;
6751 end Non_Limited_Designated_Type;
6753 -----------------------------------
6754 -- OK_To_Do_Constant_Replacement --
6755 -----------------------------------
6757 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
6758 ES : constant Entity_Id := Scope (E);
6759 CS : Entity_Id;
6761 begin
6762 -- Do not replace statically allocated objects, because they may be
6763 -- modified outside the current scope.
6765 if Is_Statically_Allocated (E) then
6766 return False;
6768 -- Do not replace aliased or volatile objects, since we don't know what
6769 -- else might change the value.
6771 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
6772 return False;
6774 -- Debug flag -gnatdM disconnects this optimization
6776 elsif Debug_Flag_MM then
6777 return False;
6779 -- Otherwise check scopes
6781 else
6782 CS := Current_Scope;
6784 loop
6785 -- If we are in right scope, replacement is safe
6787 if CS = ES then
6788 return True;
6790 -- Packages do not affect the determination of safety
6792 elsif Ekind (CS) = E_Package then
6793 exit when CS = Standard_Standard;
6794 CS := Scope (CS);
6796 -- Blocks do not affect the determination of safety
6798 elsif Ekind (CS) = E_Block then
6799 CS := Scope (CS);
6801 -- Loops do not affect the determination of safety. Note that we
6802 -- kill all current values on entry to a loop, so we are just
6803 -- talking about processing within a loop here.
6805 elsif Ekind (CS) = E_Loop then
6806 CS := Scope (CS);
6808 -- Otherwise, the reference is dubious, and we cannot be sure that
6809 -- it is safe to do the replacement.
6811 else
6812 exit;
6813 end if;
6814 end loop;
6816 return False;
6817 end if;
6818 end OK_To_Do_Constant_Replacement;
6820 ------------------------------------
6821 -- Possible_Bit_Aligned_Component --
6822 ------------------------------------
6824 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
6825 begin
6826 case Nkind (N) is
6828 -- Case of indexed component
6830 when N_Indexed_Component =>
6831 declare
6832 P : constant Node_Id := Prefix (N);
6833 Ptyp : constant Entity_Id := Etype (P);
6835 begin
6836 -- If we know the component size and it is less than 64, then
6837 -- we are definitely OK. The back end always does assignment of
6838 -- misaligned small objects correctly.
6840 if Known_Static_Component_Size (Ptyp)
6841 and then Component_Size (Ptyp) <= 64
6842 then
6843 return False;
6845 -- Otherwise, we need to test the prefix, to see if we are
6846 -- indexing from a possibly unaligned component.
6848 else
6849 return Possible_Bit_Aligned_Component (P);
6850 end if;
6851 end;
6853 -- Case of selected component
6855 when N_Selected_Component =>
6856 declare
6857 P : constant Node_Id := Prefix (N);
6858 Comp : constant Entity_Id := Entity (Selector_Name (N));
6860 begin
6861 -- If there is no component clause, then we are in the clear
6862 -- since the back end will never misalign a large component
6863 -- unless it is forced to do so. In the clear means we need
6864 -- only the recursive test on the prefix.
6866 if Component_May_Be_Bit_Aligned (Comp) then
6867 return True;
6868 else
6869 return Possible_Bit_Aligned_Component (P);
6870 end if;
6871 end;
6873 -- For a slice, test the prefix, if that is possibly misaligned,
6874 -- then for sure the slice is.
6876 when N_Slice =>
6877 return Possible_Bit_Aligned_Component (Prefix (N));
6879 -- For an unchecked conversion, check whether the expression may
6880 -- be bit-aligned.
6882 when N_Unchecked_Type_Conversion =>
6883 return Possible_Bit_Aligned_Component (Expression (N));
6885 -- If we have none of the above, it means that we have fallen off the
6886 -- top testing prefixes recursively, and we now have a stand alone
6887 -- object, where we don't have a problem.
6889 when others =>
6890 return False;
6892 end case;
6893 end Possible_Bit_Aligned_Component;
6895 -----------------------------------------------
6896 -- Process_Statements_For_Controlled_Objects --
6897 -----------------------------------------------
6899 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
6900 Loc : constant Source_Ptr := Sloc (N);
6902 function Are_Wrapped (L : List_Id) return Boolean;
6903 -- Determine whether list L contains only one statement which is a block
6905 function Wrap_Statements_In_Block
6906 (L : List_Id;
6907 Scop : Entity_Id := Current_Scope) return Node_Id;
6908 -- Given a list of statements L, wrap it in a block statement and return
6909 -- the generated node. Scop is either the current scope or the scope of
6910 -- the context (if applicable).
6912 -----------------
6913 -- Are_Wrapped --
6914 -----------------
6916 function Are_Wrapped (L : List_Id) return Boolean is
6917 Stmt : constant Node_Id := First (L);
6918 begin
6919 return
6920 Present (Stmt)
6921 and then No (Next (Stmt))
6922 and then Nkind (Stmt) = N_Block_Statement;
6923 end Are_Wrapped;
6925 ------------------------------
6926 -- Wrap_Statements_In_Block --
6927 ------------------------------
6929 function Wrap_Statements_In_Block
6930 (L : List_Id;
6931 Scop : Entity_Id := Current_Scope) return Node_Id
6933 Block_Id : Entity_Id;
6934 Block_Nod : Node_Id;
6935 Iter_Loop : Entity_Id;
6937 begin
6938 Block_Nod :=
6939 Make_Block_Statement (Loc,
6940 Declarations => No_List,
6941 Handled_Statement_Sequence =>
6942 Make_Handled_Sequence_Of_Statements (Loc,
6943 Statements => L));
6945 -- Create a label for the block in case the block needs to manage the
6946 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
6948 Add_Block_Identifier (Block_Nod, Block_Id);
6950 -- When wrapping the statements of an iterator loop, check whether
6951 -- the loop requires secondary stack management and if so, propagate
6952 -- the appropriate flags to the block. This ensures that the cursor
6953 -- is properly cleaned up at each iteration of the loop.
6955 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
6957 if Present (Iter_Loop) then
6958 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
6960 -- Secondary stack reclamation is suppressed when the associated
6961 -- iterator loop contains a return statement which uses the stack.
6963 Set_Sec_Stack_Needed_For_Return
6964 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
6965 end if;
6967 return Block_Nod;
6968 end Wrap_Statements_In_Block;
6970 -- Local variables
6972 Block : Node_Id;
6974 -- Start of processing for Process_Statements_For_Controlled_Objects
6976 begin
6977 -- Whenever a non-handled statement list is wrapped in a block, the
6978 -- block must be explicitly analyzed to redecorate all entities in the
6979 -- list and ensure that a finalizer is properly built.
6981 case Nkind (N) is
6982 when N_Elsif_Part |
6983 N_If_Statement |
6984 N_Conditional_Entry_Call |
6985 N_Selective_Accept =>
6987 -- Check the "then statements" for elsif parts and if statements
6989 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
6990 and then not Is_Empty_List (Then_Statements (N))
6991 and then not Are_Wrapped (Then_Statements (N))
6992 and then Requires_Cleanup_Actions
6993 (Then_Statements (N), False, False)
6994 then
6995 Block := Wrap_Statements_In_Block (Then_Statements (N));
6996 Set_Then_Statements (N, New_List (Block));
6998 Analyze (Block);
6999 end if;
7001 -- Check the "else statements" for conditional entry calls, if
7002 -- statements and selective accepts.
7004 if Nkind_In (N, N_Conditional_Entry_Call,
7005 N_If_Statement,
7006 N_Selective_Accept)
7007 and then not Is_Empty_List (Else_Statements (N))
7008 and then not Are_Wrapped (Else_Statements (N))
7009 and then Requires_Cleanup_Actions
7010 (Else_Statements (N), False, False)
7011 then
7012 Block := Wrap_Statements_In_Block (Else_Statements (N));
7013 Set_Else_Statements (N, New_List (Block));
7015 Analyze (Block);
7016 end if;
7018 when N_Abortable_Part |
7019 N_Accept_Alternative |
7020 N_Case_Statement_Alternative |
7021 N_Delay_Alternative |
7022 N_Entry_Call_Alternative |
7023 N_Exception_Handler |
7024 N_Loop_Statement |
7025 N_Triggering_Alternative =>
7027 if not Is_Empty_List (Statements (N))
7028 and then not Are_Wrapped (Statements (N))
7029 and then Requires_Cleanup_Actions (Statements (N), False, False)
7030 then
7031 if Nkind (N) = N_Loop_Statement
7032 and then Present (Identifier (N))
7033 then
7034 Block :=
7035 Wrap_Statements_In_Block
7036 (L => Statements (N),
7037 Scop => Entity (Identifier (N)));
7038 else
7039 Block := Wrap_Statements_In_Block (Statements (N));
7040 end if;
7042 Set_Statements (N, New_List (Block));
7043 Analyze (Block);
7044 end if;
7046 when others =>
7047 null;
7048 end case;
7049 end Process_Statements_For_Controlled_Objects;
7051 ------------------
7052 -- Power_Of_Two --
7053 ------------------
7055 function Power_Of_Two (N : Node_Id) return Nat is
7056 Typ : constant Entity_Id := Etype (N);
7057 pragma Assert (Is_Integer_Type (Typ));
7059 Siz : constant Nat := UI_To_Int (Esize (Typ));
7060 Val : Uint;
7062 begin
7063 if not Compile_Time_Known_Value (N) then
7064 return 0;
7066 else
7067 Val := Expr_Value (N);
7068 for J in 1 .. Siz - 1 loop
7069 if Val = Uint_2 ** J then
7070 return J;
7071 end if;
7072 end loop;
7074 return 0;
7075 end if;
7076 end Power_Of_Two;
7078 ----------------------
7079 -- Remove_Init_Call --
7080 ----------------------
7082 function Remove_Init_Call
7083 (Var : Entity_Id;
7084 Rep_Clause : Node_Id) return Node_Id
7086 Par : constant Node_Id := Parent (Var);
7087 Typ : constant Entity_Id := Etype (Var);
7089 Init_Proc : Entity_Id;
7090 -- Initialization procedure for Typ
7092 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
7093 -- Look for init call for Var starting at From and scanning the
7094 -- enclosing list until Rep_Clause or the end of the list is reached.
7096 ----------------------------
7097 -- Find_Init_Call_In_List --
7098 ----------------------------
7100 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
7101 Init_Call : Node_Id;
7103 begin
7104 Init_Call := From;
7105 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
7106 if Nkind (Init_Call) = N_Procedure_Call_Statement
7107 and then Is_Entity_Name (Name (Init_Call))
7108 and then Entity (Name (Init_Call)) = Init_Proc
7109 then
7110 return Init_Call;
7111 end if;
7113 Next (Init_Call);
7114 end loop;
7116 return Empty;
7117 end Find_Init_Call_In_List;
7119 Init_Call : Node_Id;
7121 -- Start of processing for Find_Init_Call
7123 begin
7124 if Present (Initialization_Statements (Var)) then
7125 Init_Call := Initialization_Statements (Var);
7126 Set_Initialization_Statements (Var, Empty);
7128 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
7130 -- No init proc for the type, so obviously no call to be found
7132 return Empty;
7134 else
7135 -- We might be able to handle other cases below by just properly
7136 -- setting Initialization_Statements at the point where the init proc
7137 -- call is generated???
7139 Init_Proc := Base_Init_Proc (Typ);
7141 -- First scan the list containing the declaration of Var
7143 Init_Call := Find_Init_Call_In_List (From => Next (Par));
7145 -- If not found, also look on Var's freeze actions list, if any,
7146 -- since the init call may have been moved there (case of an address
7147 -- clause applying to Var).
7149 if No (Init_Call) and then Present (Freeze_Node (Var)) then
7150 Init_Call :=
7151 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
7152 end if;
7154 -- If the initialization call has actuals that use the secondary
7155 -- stack, the call may have been wrapped into a temporary block, in
7156 -- which case the block itself has to be removed.
7158 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
7159 declare
7160 Blk : constant Node_Id := Next (Par);
7161 begin
7162 if Present
7163 (Find_Init_Call_In_List
7164 (First (Statements (Handled_Statement_Sequence (Blk)))))
7165 then
7166 Init_Call := Blk;
7167 end if;
7168 end;
7169 end if;
7170 end if;
7172 if Present (Init_Call) then
7173 Remove (Init_Call);
7174 end if;
7175 return Init_Call;
7176 end Remove_Init_Call;
7178 -------------------------
7179 -- Remove_Side_Effects --
7180 -------------------------
7182 procedure Remove_Side_Effects
7183 (Exp : Node_Id;
7184 Name_Req : Boolean := False;
7185 Renaming_Req : Boolean := False;
7186 Variable_Ref : Boolean := False)
7188 Loc : constant Source_Ptr := Sloc (Exp);
7189 Exp_Type : constant Entity_Id := Etype (Exp);
7190 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
7191 Def_Id : Entity_Id;
7192 E : Node_Id;
7193 New_Exp : Node_Id;
7194 Ptr_Typ_Decl : Node_Id;
7195 Ref_Type : Entity_Id;
7196 Res : Node_Id;
7198 begin
7199 -- Handle cases in which there is nothing to do. In GNATprove mode,
7200 -- removal of side effects is useful for the light expansion of
7201 -- renamings. This removal should only occur when not inside a
7202 -- generic and not doing a pre-analysis.
7204 if not Expander_Active
7205 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
7206 then
7207 return;
7208 end if;
7210 -- Cannot generate temporaries if the invocation to remove side effects
7211 -- was issued too early and the type of the expression is not resolved
7212 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
7213 -- Remove_Side_Effects).
7215 if No (Exp_Type) or else Ekind (Exp_Type) = E_Access_Attribute_Type then
7216 return;
7218 -- No action needed for side-effect free expressions
7220 elsif Side_Effect_Free (Exp, Name_Req, Variable_Ref) then
7221 return;
7222 end if;
7224 -- The remaining procesaing is done with all checks suppressed
7226 -- Note: from now on, don't use return statements, instead do a goto
7227 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
7229 Scope_Suppress.Suppress := (others => True);
7231 -- If it is a scalar type and we need to capture the value, just make
7232 -- a copy. Likewise for a function call, an attribute reference, a
7233 -- conditional expression, an allocator, or an operator. And if we have
7234 -- a volatile reference and Name_Req is not set (see comments for
7235 -- Side_Effect_Free).
7237 if Is_Elementary_Type (Exp_Type)
7239 -- Note: this test is rather mysterious??? Why can't we just test ONLY
7240 -- Is_Elementary_Type and be done with it. If we try that approach, we
7241 -- get some failures (infinite recursions) from the Duplicate_Subexpr
7242 -- call at the end of Checks.Apply_Predicate_Check. To be
7243 -- investigated ???
7245 and then (Variable_Ref
7246 or else Nkind_In (Exp, N_Attribute_Reference,
7247 N_Allocator,
7248 N_Case_Expression,
7249 N_If_Expression,
7250 N_Function_Call)
7251 or else Nkind (Exp) in N_Op
7252 or else (not Name_Req
7253 and then Is_Volatile_Reference (Exp)))
7254 then
7255 Def_Id := Make_Temporary (Loc, 'R', Exp);
7256 Set_Etype (Def_Id, Exp_Type);
7257 Res := New_Occurrence_Of (Def_Id, Loc);
7259 -- If the expression is a packed reference, it must be reanalyzed and
7260 -- expanded, depending on context. This is the case for actuals where
7261 -- a constraint check may capture the actual before expansion of the
7262 -- call is complete.
7264 if Nkind (Exp) = N_Indexed_Component
7265 and then Is_Packed (Etype (Prefix (Exp)))
7266 then
7267 Set_Analyzed (Exp, False);
7268 Set_Analyzed (Prefix (Exp), False);
7269 end if;
7271 -- Generate:
7272 -- Rnn : Exp_Type renames Expr;
7274 if Renaming_Req then
7275 E :=
7276 Make_Object_Renaming_Declaration (Loc,
7277 Defining_Identifier => Def_Id,
7278 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7279 Name => Relocate_Node (Exp));
7281 -- Generate:
7282 -- Rnn : constant Exp_Type := Expr;
7284 else
7285 E :=
7286 Make_Object_Declaration (Loc,
7287 Defining_Identifier => Def_Id,
7288 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7289 Constant_Present => True,
7290 Expression => Relocate_Node (Exp));
7292 Set_Assignment_OK (E);
7293 end if;
7295 Insert_Action (Exp, E);
7297 -- If the expression has the form v.all then we can just capture the
7298 -- pointer, and then do an explicit dereference on the result, but
7299 -- this is not right if this is a volatile reference.
7301 elsif Nkind (Exp) = N_Explicit_Dereference
7302 and then not Is_Volatile_Reference (Exp)
7303 then
7304 Def_Id := Make_Temporary (Loc, 'R', Exp);
7305 Res :=
7306 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
7308 Insert_Action (Exp,
7309 Make_Object_Declaration (Loc,
7310 Defining_Identifier => Def_Id,
7311 Object_Definition =>
7312 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
7313 Constant_Present => True,
7314 Expression => Relocate_Node (Prefix (Exp))));
7316 -- Similar processing for an unchecked conversion of an expression of
7317 -- the form v.all, where we want the same kind of treatment.
7319 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7320 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
7321 then
7322 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
7323 goto Leave;
7325 -- If this is a type conversion, leave the type conversion and remove
7326 -- the side effects in the expression. This is important in several
7327 -- circumstances: for change of representations, and also when this is a
7328 -- view conversion to a smaller object, where gigi can end up creating
7329 -- its own temporary of the wrong size.
7331 elsif Nkind (Exp) = N_Type_Conversion then
7332 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
7333 goto Leave;
7335 -- If this is an unchecked conversion that Gigi can't handle, make
7336 -- a copy or a use a renaming to capture the value.
7338 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
7339 and then not Safe_Unchecked_Type_Conversion (Exp)
7340 then
7341 if CW_Or_Has_Controlled_Part (Exp_Type) then
7343 -- Use a renaming to capture the expression, rather than create
7344 -- a controlled temporary.
7346 Def_Id := Make_Temporary (Loc, 'R', Exp);
7347 Res := New_Occurrence_Of (Def_Id, Loc);
7349 Insert_Action (Exp,
7350 Make_Object_Renaming_Declaration (Loc,
7351 Defining_Identifier => Def_Id,
7352 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7353 Name => Relocate_Node (Exp)));
7355 else
7356 Def_Id := Make_Temporary (Loc, 'R', Exp);
7357 Set_Etype (Def_Id, Exp_Type);
7358 Res := New_Occurrence_Of (Def_Id, Loc);
7360 E :=
7361 Make_Object_Declaration (Loc,
7362 Defining_Identifier => Def_Id,
7363 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7364 Constant_Present => not Is_Variable (Exp),
7365 Expression => Relocate_Node (Exp));
7367 Set_Assignment_OK (E);
7368 Insert_Action (Exp, E);
7369 end if;
7371 -- For expressions that denote objects, we can use a renaming scheme.
7372 -- This is needed for correctness in the case of a volatile object of
7373 -- a non-volatile type because the Make_Reference call of the "default"
7374 -- approach would generate an illegal access value (an access value
7375 -- cannot designate such an object - see Analyze_Reference).
7377 elsif Is_Object_Reference (Exp)
7378 and then Nkind (Exp) /= N_Function_Call
7380 -- In Ada 2012 a qualified expression is an object, but for purposes
7381 -- of removing side effects it still need to be transformed into a
7382 -- separate declaration, particularly in the case of an aggregate.
7384 and then Nkind (Exp) /= N_Qualified_Expression
7386 -- We skip using this scheme if we have an object of a volatile
7387 -- type and we do not have Name_Req set true (see comments for
7388 -- Side_Effect_Free).
7390 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
7391 then
7392 Def_Id := Make_Temporary (Loc, 'R', Exp);
7394 if Nkind (Exp) = N_Selected_Component
7395 and then Nkind (Prefix (Exp)) = N_Function_Call
7396 and then Is_Array_Type (Exp_Type)
7397 then
7398 -- Avoid generating a variable-sized temporary, by generating
7399 -- the renaming declaration just for the function call. The
7400 -- transformation could be refined to apply only when the array
7401 -- component is constrained by a discriminant???
7403 Res :=
7404 Make_Selected_Component (Loc,
7405 Prefix => New_Occurrence_Of (Def_Id, Loc),
7406 Selector_Name => Selector_Name (Exp));
7408 Insert_Action (Exp,
7409 Make_Object_Renaming_Declaration (Loc,
7410 Defining_Identifier => Def_Id,
7411 Subtype_Mark =>
7412 New_Occurrence_Of (Base_Type (Etype (Prefix (Exp))), Loc),
7413 Name => Relocate_Node (Prefix (Exp))));
7415 else
7416 Res := New_Occurrence_Of (Def_Id, Loc);
7418 Insert_Action (Exp,
7419 Make_Object_Renaming_Declaration (Loc,
7420 Defining_Identifier => Def_Id,
7421 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
7422 Name => Relocate_Node (Exp)));
7423 end if;
7425 -- If this is a packed reference, or a selected component with
7426 -- a non-standard representation, a reference to the temporary
7427 -- will be replaced by a copy of the original expression (see
7428 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
7429 -- elaborated by gigi, and is of course not to be replaced in-line
7430 -- by the expression it renames, which would defeat the purpose of
7431 -- removing the side-effect.
7433 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
7434 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
7435 then
7436 null;
7437 else
7438 Set_Is_Renaming_Of_Object (Def_Id, False);
7439 end if;
7441 -- Otherwise we generate a reference to the value
7443 else
7444 -- An expression which is in SPARK mode is considered side effect
7445 -- free if the resulting value is captured by a variable or a
7446 -- constant.
7448 if GNATprove_Mode
7449 and then Nkind (Parent (Exp)) = N_Object_Declaration
7450 then
7451 goto Leave;
7452 end if;
7454 -- Special processing for function calls that return a limited type.
7455 -- We need to build a declaration that will enable build-in-place
7456 -- expansion of the call. This is not done if the context is already
7457 -- an object declaration, to prevent infinite recursion.
7459 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
7460 -- to accommodate functions returning limited objects by reference.
7462 if Ada_Version >= Ada_2005
7463 and then Nkind (Exp) = N_Function_Call
7464 and then Is_Limited_View (Etype (Exp))
7465 and then Nkind (Parent (Exp)) /= N_Object_Declaration
7466 then
7467 declare
7468 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
7469 Decl : Node_Id;
7471 begin
7472 Decl :=
7473 Make_Object_Declaration (Loc,
7474 Defining_Identifier => Obj,
7475 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
7476 Expression => Relocate_Node (Exp));
7478 Insert_Action (Exp, Decl);
7479 Set_Etype (Obj, Exp_Type);
7480 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
7481 goto Leave;
7482 end;
7483 end if;
7485 Def_Id := Make_Temporary (Loc, 'R', Exp);
7487 -- The regular expansion of functions with side effects involves the
7488 -- generation of an access type to capture the return value found on
7489 -- the secondary stack. Since SPARK (and why) cannot process access
7490 -- types, use a different approach which ignores the secondary stack
7491 -- and "copies" the returned object.
7493 if GNATprove_Mode then
7494 Res := New_Occurrence_Of (Def_Id, Loc);
7495 Ref_Type := Exp_Type;
7497 -- Regular expansion utilizing an access type and 'reference
7499 else
7500 Res :=
7501 Make_Explicit_Dereference (Loc,
7502 Prefix => New_Occurrence_Of (Def_Id, Loc));
7504 -- Generate:
7505 -- type Ann is access all <Exp_Type>;
7507 Ref_Type := Make_Temporary (Loc, 'A');
7509 Ptr_Typ_Decl :=
7510 Make_Full_Type_Declaration (Loc,
7511 Defining_Identifier => Ref_Type,
7512 Type_Definition =>
7513 Make_Access_To_Object_Definition (Loc,
7514 All_Present => True,
7515 Subtype_Indication =>
7516 New_Occurrence_Of (Exp_Type, Loc)));
7518 Insert_Action (Exp, Ptr_Typ_Decl);
7519 end if;
7521 E := Exp;
7522 if Nkind (E) = N_Explicit_Dereference then
7523 New_Exp := Relocate_Node (Prefix (E));
7525 else
7526 E := Relocate_Node (E);
7528 -- Do not generate a 'reference in SPARK mode since the access
7529 -- type is not created in the first place.
7531 if GNATprove_Mode then
7532 New_Exp := E;
7534 -- Otherwise generate reference, marking the value as non-null
7535 -- since we know it cannot be null and we don't want a check.
7537 else
7538 New_Exp := Make_Reference (Loc, E);
7539 Set_Is_Known_Non_Null (Def_Id);
7540 end if;
7541 end if;
7543 if Is_Delayed_Aggregate (E) then
7545 -- The expansion of nested aggregates is delayed until the
7546 -- enclosing aggregate is expanded. As aggregates are often
7547 -- qualified, the predicate applies to qualified expressions as
7548 -- well, indicating that the enclosing aggregate has not been
7549 -- expanded yet. At this point the aggregate is part of a
7550 -- stand-alone declaration, and must be fully expanded.
7552 if Nkind (E) = N_Qualified_Expression then
7553 Set_Expansion_Delayed (Expression (E), False);
7554 Set_Analyzed (Expression (E), False);
7555 else
7556 Set_Expansion_Delayed (E, False);
7557 end if;
7559 Set_Analyzed (E, False);
7560 end if;
7562 Insert_Action (Exp,
7563 Make_Object_Declaration (Loc,
7564 Defining_Identifier => Def_Id,
7565 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
7566 Constant_Present => True,
7567 Expression => New_Exp));
7568 end if;
7570 -- Preserve the Assignment_OK flag in all copies, since at least one
7571 -- copy may be used in a context where this flag must be set (otherwise
7572 -- why would the flag be set in the first place).
7574 Set_Assignment_OK (Res, Assignment_OK (Exp));
7576 -- Finally rewrite the original expression and we are done
7578 Rewrite (Exp, Res);
7579 Analyze_And_Resolve (Exp, Exp_Type);
7581 <<Leave>>
7582 Scope_Suppress := Svg_Suppress;
7583 end Remove_Side_Effects;
7585 ---------------------------
7586 -- Represented_As_Scalar --
7587 ---------------------------
7589 function Represented_As_Scalar (T : Entity_Id) return Boolean is
7590 UT : constant Entity_Id := Underlying_Type (T);
7591 begin
7592 return Is_Scalar_Type (UT)
7593 or else (Is_Bit_Packed_Array (UT)
7594 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
7595 end Represented_As_Scalar;
7597 ------------------------------
7598 -- Requires_Cleanup_Actions --
7599 ------------------------------
7601 function Requires_Cleanup_Actions
7602 (N : Node_Id;
7603 Lib_Level : Boolean) return Boolean
7605 At_Lib_Level : constant Boolean :=
7606 Lib_Level
7607 and then Nkind_In (N, N_Package_Body,
7608 N_Package_Specification);
7609 -- N is at the library level if the top-most context is a package and
7610 -- the path taken to reach N does not inlcude non-package constructs.
7612 begin
7613 case Nkind (N) is
7614 when N_Accept_Statement |
7615 N_Block_Statement |
7616 N_Entry_Body |
7617 N_Package_Body |
7618 N_Protected_Body |
7619 N_Subprogram_Body |
7620 N_Task_Body =>
7621 return
7622 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
7623 or else
7624 (Present (Handled_Statement_Sequence (N))
7625 and then
7626 Requires_Cleanup_Actions
7627 (Statements (Handled_Statement_Sequence (N)),
7628 At_Lib_Level, True));
7630 when N_Package_Specification =>
7631 return
7632 Requires_Cleanup_Actions
7633 (Visible_Declarations (N), At_Lib_Level, True)
7634 or else
7635 Requires_Cleanup_Actions
7636 (Private_Declarations (N), At_Lib_Level, True);
7638 when others =>
7639 return False;
7640 end case;
7641 end Requires_Cleanup_Actions;
7643 ------------------------------
7644 -- Requires_Cleanup_Actions --
7645 ------------------------------
7647 function Requires_Cleanup_Actions
7648 (L : List_Id;
7649 Lib_Level : Boolean;
7650 Nested_Constructs : Boolean) return Boolean
7652 Decl : Node_Id;
7653 Expr : Node_Id;
7654 Obj_Id : Entity_Id;
7655 Obj_Typ : Entity_Id;
7656 Pack_Id : Entity_Id;
7657 Typ : Entity_Id;
7659 begin
7660 if No (L)
7661 or else Is_Empty_List (L)
7662 then
7663 return False;
7664 end if;
7666 Decl := First (L);
7667 while Present (Decl) loop
7669 -- Library-level tagged types
7671 if Nkind (Decl) = N_Full_Type_Declaration then
7672 Typ := Defining_Identifier (Decl);
7674 if Is_Tagged_Type (Typ)
7675 and then Is_Library_Level_Entity (Typ)
7676 and then Convention (Typ) = Convention_Ada
7677 and then Present (Access_Disp_Table (Typ))
7678 and then RTE_Available (RE_Unregister_Tag)
7679 and then not No_Run_Time_Mode
7680 and then not Is_Abstract_Type (Typ)
7681 then
7682 return True;
7683 end if;
7685 -- Regular object declarations
7687 elsif Nkind (Decl) = N_Object_Declaration then
7688 Obj_Id := Defining_Identifier (Decl);
7689 Obj_Typ := Base_Type (Etype (Obj_Id));
7690 Expr := Expression (Decl);
7692 -- Bypass any form of processing for objects which have their
7693 -- finalization disabled. This applies only to objects at the
7694 -- library level.
7696 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7697 null;
7699 -- Transient variables are treated separately in order to minimize
7700 -- the size of the generated code. See Exp_Ch7.Process_Transient_
7701 -- Objects.
7703 elsif Is_Processed_Transient (Obj_Id) then
7704 null;
7706 -- The object is of the form:
7707 -- Obj : Typ [:= Expr];
7709 -- Do not process the incomplete view of a deferred constant. Do
7710 -- not consider tag-to-class-wide conversions.
7712 elsif not Is_Imported (Obj_Id)
7713 and then Needs_Finalization (Obj_Typ)
7714 and then not (Ekind (Obj_Id) = E_Constant
7715 and then not Has_Completion (Obj_Id))
7716 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
7717 then
7718 return True;
7720 -- The object is of the form:
7721 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
7723 -- Obj : Access_Typ :=
7724 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
7726 elsif Is_Access_Type (Obj_Typ)
7727 and then Needs_Finalization
7728 (Available_View (Designated_Type (Obj_Typ)))
7729 and then Present (Expr)
7730 and then
7731 (Is_Secondary_Stack_BIP_Func_Call (Expr)
7732 or else
7733 (Is_Non_BIP_Func_Call (Expr)
7734 and then not Is_Related_To_Func_Return (Obj_Id)))
7735 then
7736 return True;
7738 -- Processing for "hook" objects generated for controlled
7739 -- transients declared inside an Expression_With_Actions.
7741 elsif Is_Access_Type (Obj_Typ)
7742 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7743 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7744 N_Object_Declaration
7745 then
7746 return True;
7748 -- Processing for intermediate results of if expressions where
7749 -- one of the alternatives uses a controlled function call.
7751 elsif Is_Access_Type (Obj_Typ)
7752 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7753 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
7754 N_Defining_Identifier
7755 and then Present (Expr)
7756 and then Nkind (Expr) = N_Null
7757 then
7758 return True;
7760 -- Simple protected objects which use type System.Tasking.
7761 -- Protected_Objects.Protection to manage their locks should be
7762 -- treated as controlled since they require manual cleanup.
7764 elsif Ekind (Obj_Id) = E_Variable
7765 and then (Is_Simple_Protected_Type (Obj_Typ)
7766 or else Has_Simple_Protected_Object (Obj_Typ))
7767 then
7768 return True;
7769 end if;
7771 -- Specific cases of object renamings
7773 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
7774 Obj_Id := Defining_Identifier (Decl);
7775 Obj_Typ := Base_Type (Etype (Obj_Id));
7777 -- Bypass any form of processing for objects which have their
7778 -- finalization disabled. This applies only to objects at the
7779 -- library level.
7781 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
7782 null;
7784 -- Return object of a build-in-place function. This case is
7785 -- recognized and marked by the expansion of an extended return
7786 -- statement (see Expand_N_Extended_Return_Statement).
7788 elsif Needs_Finalization (Obj_Typ)
7789 and then Is_Return_Object (Obj_Id)
7790 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
7791 then
7792 return True;
7794 -- Detect a case where a source object has been initialized by
7795 -- a controlled function call or another object which was later
7796 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
7798 -- Obj1 : CW_Type := Src_Obj;
7799 -- Obj2 : CW_Type := Function_Call (...);
7801 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7802 -- Tmp : ... := Function_Call (...)'reference;
7803 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
7805 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
7806 return True;
7807 end if;
7809 -- Inspect the freeze node of an access-to-controlled type and look
7810 -- for a delayed finalization master. This case arises when the
7811 -- freeze actions are inserted at a later time than the expansion of
7812 -- the context. Since Build_Finalizer is never called on a single
7813 -- construct twice, the master will be ultimately left out and never
7814 -- finalized. This is also needed for freeze actions of designated
7815 -- types themselves, since in some cases the finalization master is
7816 -- associated with a designated type's freeze node rather than that
7817 -- of the access type (see handling for freeze actions in
7818 -- Build_Finalization_Master).
7820 elsif Nkind (Decl) = N_Freeze_Entity
7821 and then Present (Actions (Decl))
7822 then
7823 Typ := Entity (Decl);
7825 if ((Is_Access_Type (Typ)
7826 and then not Is_Access_Subprogram_Type (Typ)
7827 and then Needs_Finalization
7828 (Available_View (Designated_Type (Typ))))
7829 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
7830 and then Requires_Cleanup_Actions
7831 (Actions (Decl), Lib_Level, Nested_Constructs)
7832 then
7833 return True;
7834 end if;
7836 -- Nested package declarations
7838 elsif Nested_Constructs
7839 and then Nkind (Decl) = N_Package_Declaration
7840 then
7841 Pack_Id := Defining_Unit_Name (Specification (Decl));
7843 if Nkind (Pack_Id) = N_Defining_Program_Unit_Name then
7844 Pack_Id := Defining_Identifier (Pack_Id);
7845 end if;
7847 if Ekind (Pack_Id) /= E_Generic_Package
7848 and then
7849 Requires_Cleanup_Actions (Specification (Decl), Lib_Level)
7850 then
7851 return True;
7852 end if;
7854 -- Nested package bodies
7856 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
7857 Pack_Id := Corresponding_Spec (Decl);
7859 if Ekind (Pack_Id) /= E_Generic_Package
7860 and then Requires_Cleanup_Actions (Decl, Lib_Level)
7861 then
7862 return True;
7863 end if;
7864 end if;
7866 Next (Decl);
7867 end loop;
7869 return False;
7870 end Requires_Cleanup_Actions;
7872 ------------------------------------
7873 -- Safe_Unchecked_Type_Conversion --
7874 ------------------------------------
7876 -- Note: this function knows quite a bit about the exact requirements of
7877 -- Gigi with respect to unchecked type conversions, and its code must be
7878 -- coordinated with any changes in Gigi in this area.
7880 -- The above requirements should be documented in Sinfo ???
7882 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
7883 Otyp : Entity_Id;
7884 Ityp : Entity_Id;
7885 Oalign : Uint;
7886 Ialign : Uint;
7887 Pexp : constant Node_Id := Parent (Exp);
7889 begin
7890 -- If the expression is the RHS of an assignment or object declaration
7891 -- we are always OK because there will always be a target.
7893 -- Object renaming declarations, (generated for view conversions of
7894 -- actuals in inlined calls), like object declarations, provide an
7895 -- explicit type, and are safe as well.
7897 if (Nkind (Pexp) = N_Assignment_Statement
7898 and then Expression (Pexp) = Exp)
7899 or else Nkind_In (Pexp, N_Object_Declaration,
7900 N_Object_Renaming_Declaration)
7901 then
7902 return True;
7904 -- If the expression is the prefix of an N_Selected_Component we should
7905 -- also be OK because GCC knows to look inside the conversion except if
7906 -- the type is discriminated. We assume that we are OK anyway if the
7907 -- type is not set yet or if it is controlled since we can't afford to
7908 -- introduce a temporary in this case.
7910 elsif Nkind (Pexp) = N_Selected_Component
7911 and then Prefix (Pexp) = Exp
7912 then
7913 if No (Etype (Pexp)) then
7914 return True;
7915 else
7916 return
7917 not Has_Discriminants (Etype (Pexp))
7918 or else Is_Constrained (Etype (Pexp));
7919 end if;
7920 end if;
7922 -- Set the output type, this comes from Etype if it is set, otherwise we
7923 -- take it from the subtype mark, which we assume was already fully
7924 -- analyzed.
7926 if Present (Etype (Exp)) then
7927 Otyp := Etype (Exp);
7928 else
7929 Otyp := Entity (Subtype_Mark (Exp));
7930 end if;
7932 -- The input type always comes from the expression, and we assume
7933 -- this is indeed always analyzed, so we can simply get the Etype.
7935 Ityp := Etype (Expression (Exp));
7937 -- Initialize alignments to unknown so far
7939 Oalign := No_Uint;
7940 Ialign := No_Uint;
7942 -- Replace a concurrent type by its corresponding record type and each
7943 -- type by its underlying type and do the tests on those. The original
7944 -- type may be a private type whose completion is a concurrent type, so
7945 -- find the underlying type first.
7947 if Present (Underlying_Type (Otyp)) then
7948 Otyp := Underlying_Type (Otyp);
7949 end if;
7951 if Present (Underlying_Type (Ityp)) then
7952 Ityp := Underlying_Type (Ityp);
7953 end if;
7955 if Is_Concurrent_Type (Otyp) then
7956 Otyp := Corresponding_Record_Type (Otyp);
7957 end if;
7959 if Is_Concurrent_Type (Ityp) then
7960 Ityp := Corresponding_Record_Type (Ityp);
7961 end if;
7963 -- If the base types are the same, we know there is no problem since
7964 -- this conversion will be a noop.
7966 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
7967 return True;
7969 -- Same if this is an upwards conversion of an untagged type, and there
7970 -- are no constraints involved (could be more general???)
7972 elsif Etype (Ityp) = Otyp
7973 and then not Is_Tagged_Type (Ityp)
7974 and then not Has_Discriminants (Ityp)
7975 and then No (First_Rep_Item (Base_Type (Ityp)))
7976 then
7977 return True;
7979 -- If the expression has an access type (object or subprogram) we assume
7980 -- that the conversion is safe, because the size of the target is safe,
7981 -- even if it is a record (which might be treated as having unknown size
7982 -- at this point).
7984 elsif Is_Access_Type (Ityp) then
7985 return True;
7987 -- If the size of output type is known at compile time, there is never
7988 -- a problem. Note that unconstrained records are considered to be of
7989 -- known size, but we can't consider them that way here, because we are
7990 -- talking about the actual size of the object.
7992 -- We also make sure that in addition to the size being known, we do not
7993 -- have a case which might generate an embarrassingly large temp in
7994 -- stack checking mode.
7996 elsif Size_Known_At_Compile_Time (Otyp)
7997 and then
7998 (not Stack_Checking_Enabled
7999 or else not May_Generate_Large_Temp (Otyp))
8000 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
8001 then
8002 return True;
8004 -- If either type is tagged, then we know the alignment is OK so
8005 -- Gigi will be able to use pointer punning.
8007 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
8008 return True;
8010 -- If either type is a limited record type, we cannot do a copy, so say
8011 -- safe since there's nothing else we can do.
8013 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
8014 return True;
8016 -- Conversions to and from packed array types are always ignored and
8017 -- hence are safe.
8019 elsif Is_Packed_Array_Impl_Type (Otyp)
8020 or else Is_Packed_Array_Impl_Type (Ityp)
8021 then
8022 return True;
8023 end if;
8025 -- The only other cases known to be safe is if the input type's
8026 -- alignment is known to be at least the maximum alignment for the
8027 -- target or if both alignments are known and the output type's
8028 -- alignment is no stricter than the input's. We can use the component
8029 -- type alignement for an array if a type is an unpacked array type.
8031 if Present (Alignment_Clause (Otyp)) then
8032 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
8034 elsif Is_Array_Type (Otyp)
8035 and then Present (Alignment_Clause (Component_Type (Otyp)))
8036 then
8037 Oalign := Expr_Value (Expression (Alignment_Clause
8038 (Component_Type (Otyp))));
8039 end if;
8041 if Present (Alignment_Clause (Ityp)) then
8042 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
8044 elsif Is_Array_Type (Ityp)
8045 and then Present (Alignment_Clause (Component_Type (Ityp)))
8046 then
8047 Ialign := Expr_Value (Expression (Alignment_Clause
8048 (Component_Type (Ityp))));
8049 end if;
8051 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
8052 return True;
8054 elsif Ialign /= No_Uint
8055 and then Oalign /= No_Uint
8056 and then Ialign <= Oalign
8057 then
8058 return True;
8060 -- Otherwise, Gigi cannot handle this and we must make a temporary
8062 else
8063 return False;
8064 end if;
8065 end Safe_Unchecked_Type_Conversion;
8067 ---------------------------------
8068 -- Set_Current_Value_Condition --
8069 ---------------------------------
8071 -- Note: the implementation of this procedure is very closely tied to the
8072 -- implementation of Get_Current_Value_Condition. Here we set required
8073 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
8074 -- them, so they must have a consistent view.
8076 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
8078 procedure Set_Entity_Current_Value (N : Node_Id);
8079 -- If N is an entity reference, where the entity is of an appropriate
8080 -- kind, then set the current value of this entity to Cnode, unless
8081 -- there is already a definite value set there.
8083 procedure Set_Expression_Current_Value (N : Node_Id);
8084 -- If N is of an appropriate form, sets an appropriate entry in current
8085 -- value fields of relevant entities. Multiple entities can be affected
8086 -- in the case of an AND or AND THEN.
8088 ------------------------------
8089 -- Set_Entity_Current_Value --
8090 ------------------------------
8092 procedure Set_Entity_Current_Value (N : Node_Id) is
8093 begin
8094 if Is_Entity_Name (N) then
8095 declare
8096 Ent : constant Entity_Id := Entity (N);
8098 begin
8099 -- Don't capture if not safe to do so
8101 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
8102 return;
8103 end if;
8105 -- Here we have a case where the Current_Value field may need
8106 -- to be set. We set it if it is not already set to a compile
8107 -- time expression value.
8109 -- Note that this represents a decision that one condition
8110 -- blots out another previous one. That's certainly right if
8111 -- they occur at the same level. If the second one is nested,
8112 -- then the decision is neither right nor wrong (it would be
8113 -- equally OK to leave the outer one in place, or take the new
8114 -- inner one. Really we should record both, but our data
8115 -- structures are not that elaborate.
8117 if Nkind (Current_Value (Ent)) not in N_Subexpr then
8118 Set_Current_Value (Ent, Cnode);
8119 end if;
8120 end;
8121 end if;
8122 end Set_Entity_Current_Value;
8124 ----------------------------------
8125 -- Set_Expression_Current_Value --
8126 ----------------------------------
8128 procedure Set_Expression_Current_Value (N : Node_Id) is
8129 Cond : Node_Id;
8131 begin
8132 Cond := N;
8134 -- Loop to deal with (ignore for now) any NOT operators present. The
8135 -- presence of NOT operators will be handled properly when we call
8136 -- Get_Current_Value_Condition.
8138 while Nkind (Cond) = N_Op_Not loop
8139 Cond := Right_Opnd (Cond);
8140 end loop;
8142 -- For an AND or AND THEN, recursively process operands
8144 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
8145 Set_Expression_Current_Value (Left_Opnd (Cond));
8146 Set_Expression_Current_Value (Right_Opnd (Cond));
8147 return;
8148 end if;
8150 -- Check possible relational operator
8152 if Nkind (Cond) in N_Op_Compare then
8153 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
8154 Set_Entity_Current_Value (Left_Opnd (Cond));
8155 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
8156 Set_Entity_Current_Value (Right_Opnd (Cond));
8157 end if;
8159 elsif Nkind_In (Cond,
8160 N_Type_Conversion,
8161 N_Qualified_Expression,
8162 N_Expression_With_Actions)
8163 then
8164 Set_Expression_Current_Value (Expression (Cond));
8166 -- Check possible boolean variable reference
8168 else
8169 Set_Entity_Current_Value (Cond);
8170 end if;
8171 end Set_Expression_Current_Value;
8173 -- Start of processing for Set_Current_Value_Condition
8175 begin
8176 Set_Expression_Current_Value (Condition (Cnode));
8177 end Set_Current_Value_Condition;
8179 --------------------------
8180 -- Set_Elaboration_Flag --
8181 --------------------------
8183 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
8184 Loc : constant Source_Ptr := Sloc (N);
8185 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
8186 Asn : Node_Id;
8188 begin
8189 if Present (Ent) then
8191 -- Nothing to do if at the compilation unit level, because in this
8192 -- case the flag is set by the binder generated elaboration routine.
8194 if Nkind (Parent (N)) = N_Compilation_Unit then
8195 null;
8197 -- Here we do need to generate an assignment statement
8199 else
8200 Check_Restriction (No_Elaboration_Code, N);
8201 Asn :=
8202 Make_Assignment_Statement (Loc,
8203 Name => New_Occurrence_Of (Ent, Loc),
8204 Expression => Make_Integer_Literal (Loc, Uint_1));
8206 if Nkind (Parent (N)) = N_Subunit then
8207 Insert_After (Corresponding_Stub (Parent (N)), Asn);
8208 else
8209 Insert_After (N, Asn);
8210 end if;
8212 Analyze (Asn);
8214 -- Kill current value indication. This is necessary because the
8215 -- tests of this flag are inserted out of sequence and must not
8216 -- pick up bogus indications of the wrong constant value.
8218 Set_Current_Value (Ent, Empty);
8220 -- If the subprogram is in the current declarative part and
8221 -- 'access has been applied to it, generate an elaboration
8222 -- check at the beginning of the declarations of the body.
8224 if Nkind (N) = N_Subprogram_Body
8225 and then Address_Taken (Spec_Id)
8226 and then
8227 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
8228 then
8229 declare
8230 Loc : constant Source_Ptr := Sloc (N);
8231 Decls : constant List_Id := Declarations (N);
8232 Chk : Node_Id;
8234 begin
8235 -- No need to generate this check if first entry in the
8236 -- declaration list is a raise of Program_Error now.
8238 if Present (Decls)
8239 and then Nkind (First (Decls)) = N_Raise_Program_Error
8240 then
8241 return;
8242 end if;
8244 -- Otherwise generate the check
8246 Chk :=
8247 Make_Raise_Program_Error (Loc,
8248 Condition =>
8249 Make_Op_Eq (Loc,
8250 Left_Opnd => New_Occurrence_Of (Ent, Loc),
8251 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
8252 Reason => PE_Access_Before_Elaboration);
8254 if No (Decls) then
8255 Set_Declarations (N, New_List (Chk));
8256 else
8257 Prepend (Chk, Decls);
8258 end if;
8260 Analyze (Chk);
8261 end;
8262 end if;
8263 end if;
8264 end if;
8265 end Set_Elaboration_Flag;
8267 ----------------------------
8268 -- Set_Renamed_Subprogram --
8269 ----------------------------
8271 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
8272 begin
8273 -- If input node is an identifier, we can just reset it
8275 if Nkind (N) = N_Identifier then
8276 Set_Chars (N, Chars (E));
8277 Set_Entity (N, E);
8279 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
8281 else
8282 declare
8283 CS : constant Boolean := Comes_From_Source (N);
8284 begin
8285 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
8286 Set_Entity (N, E);
8287 Set_Comes_From_Source (N, CS);
8288 Set_Analyzed (N, True);
8289 end;
8290 end if;
8291 end Set_Renamed_Subprogram;
8293 ----------------------
8294 -- Side_Effect_Free --
8295 ----------------------
8297 function Side_Effect_Free
8298 (N : Node_Id;
8299 Name_Req : Boolean := False;
8300 Variable_Ref : Boolean := False) return Boolean
8302 Typ : constant Entity_Id := Etype (N);
8303 -- Result type of the expression
8305 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
8306 -- The argument N is a construct where the Prefix is dereferenced if it
8307 -- is an access type and the result is a variable. The call returns True
8308 -- if the construct is side effect free (not considering side effects in
8309 -- other than the prefix which are to be tested by the caller).
8311 function Within_In_Parameter (N : Node_Id) return Boolean;
8312 -- Determines if N is a subcomponent of a composite in-parameter. If so,
8313 -- N is not side-effect free when the actual is global and modifiable
8314 -- indirectly from within a subprogram, because it may be passed by
8315 -- reference. The front-end must be conservative here and assume that
8316 -- this may happen with any array or record type. On the other hand, we
8317 -- cannot create temporaries for all expressions for which this
8318 -- condition is true, for various reasons that might require clearing up
8319 -- ??? For example, discriminant references that appear out of place, or
8320 -- spurious type errors with class-wide expressions. As a result, we
8321 -- limit the transformation to loop bounds, which is so far the only
8322 -- case that requires it.
8324 -----------------------------
8325 -- Safe_Prefixed_Reference --
8326 -----------------------------
8328 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
8329 begin
8330 -- If prefix is not side effect free, definitely not safe
8332 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
8333 return False;
8335 -- If the prefix is of an access type that is not access-to-constant,
8336 -- then this construct is a variable reference, which means it is to
8337 -- be considered to have side effects if Variable_Ref is set True.
8339 elsif Is_Access_Type (Etype (Prefix (N)))
8340 and then not Is_Access_Constant (Etype (Prefix (N)))
8341 and then Variable_Ref
8342 then
8343 -- Exception is a prefix that is the result of a previous removal
8344 -- of side-effects.
8346 return Is_Entity_Name (Prefix (N))
8347 and then not Comes_From_Source (Prefix (N))
8348 and then Ekind (Entity (Prefix (N))) = E_Constant
8349 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
8351 -- If the prefix is an explicit dereference then this construct is a
8352 -- variable reference, which means it is to be considered to have
8353 -- side effects if Variable_Ref is True.
8355 -- We do NOT exclude dereferences of access-to-constant types because
8356 -- we handle them as constant view of variables.
8358 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
8359 and then Variable_Ref
8360 then
8361 return False;
8363 -- Note: The following test is the simplest way of solving a complex
8364 -- problem uncovered by the following test (Side effect on loop bound
8365 -- that is a subcomponent of a global variable:
8367 -- with Text_Io; use Text_Io;
8368 -- procedure Tloop is
8369 -- type X is
8370 -- record
8371 -- V : Natural := 4;
8372 -- S : String (1..5) := (others => 'a');
8373 -- end record;
8374 -- X1 : X;
8376 -- procedure Modi;
8378 -- generic
8379 -- with procedure Action;
8380 -- procedure Loop_G (Arg : X; Msg : String)
8382 -- procedure Loop_G (Arg : X; Msg : String) is
8383 -- begin
8384 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
8385 -- & Natural'Image (Arg.V));
8386 -- for Index in 1 .. Arg.V loop
8387 -- Text_Io.Put_Line
8388 -- (Natural'Image (Index) & " " & Arg.S (Index));
8389 -- if Index > 2 then
8390 -- Modi;
8391 -- end if;
8392 -- end loop;
8393 -- Put_Line ("end loop_g " & Msg);
8394 -- end;
8396 -- procedure Loop1 is new Loop_G (Modi);
8397 -- procedure Modi is
8398 -- begin
8399 -- X1.V := 1;
8400 -- Loop1 (X1, "from modi");
8401 -- end;
8403 -- begin
8404 -- Loop1 (X1, "initial");
8405 -- end;
8407 -- The output of the above program should be:
8409 -- begin loop_g initial will loop till: 4
8410 -- 1 a
8411 -- 2 a
8412 -- 3 a
8413 -- begin loop_g from modi will loop till: 1
8414 -- 1 a
8415 -- end loop_g from modi
8416 -- 4 a
8417 -- begin loop_g from modi will loop till: 1
8418 -- 1 a
8419 -- end loop_g from modi
8420 -- end loop_g initial
8422 -- If a loop bound is a subcomponent of a global variable, a
8423 -- modification of that variable within the loop may incorrectly
8424 -- affect the execution of the loop.
8426 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
8427 and then Within_In_Parameter (Prefix (N))
8428 and then Variable_Ref
8429 then
8430 return False;
8432 -- All other cases are side effect free
8434 else
8435 return True;
8436 end if;
8437 end Safe_Prefixed_Reference;
8439 -------------------------
8440 -- Within_In_Parameter --
8441 -------------------------
8443 function Within_In_Parameter (N : Node_Id) return Boolean is
8444 begin
8445 if not Comes_From_Source (N) then
8446 return False;
8448 elsif Is_Entity_Name (N) then
8449 return Ekind (Entity (N)) = E_In_Parameter;
8451 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8452 return Within_In_Parameter (Prefix (N));
8454 else
8455 return False;
8456 end if;
8457 end Within_In_Parameter;
8459 -- Start of processing for Side_Effect_Free
8461 begin
8462 -- If volatile reference, always consider it to have side effects
8464 if Is_Volatile_Reference (N) then
8465 return False;
8466 end if;
8468 -- Note on checks that could raise Constraint_Error. Strictly, if we
8469 -- take advantage of 11.6, these checks do not count as side effects.
8470 -- However, we would prefer to consider that they are side effects,
8471 -- since the backend CSE does not work very well on expressions which
8472 -- can raise Constraint_Error. On the other hand if we don't consider
8473 -- them to be side effect free, then we get some awkward expansions
8474 -- in -gnato mode, resulting in code insertions at a point where we
8475 -- do not have a clear model for performing the insertions.
8477 -- Special handling for entity names
8479 if Is_Entity_Name (N) then
8481 -- A type reference is always side effect free
8483 if Is_Type (Entity (N)) then
8484 return True;
8486 -- Variables are considered to be a side effect if Variable_Ref
8487 -- is set or if we have a volatile reference and Name_Req is off.
8488 -- If Name_Req is True then we can't help returning a name which
8489 -- effectively allows multiple references in any case.
8491 elsif Is_Variable (N, Use_Original_Node => False) then
8492 return not Variable_Ref
8493 and then (not Is_Volatile_Reference (N) or else Name_Req);
8495 -- Any other entity (e.g. a subtype name) is definitely side
8496 -- effect free.
8498 else
8499 return True;
8500 end if;
8502 -- A value known at compile time is always side effect free
8504 elsif Compile_Time_Known_Value (N) then
8505 return True;
8507 -- A variable renaming is not side-effect free, because the renaming
8508 -- will function like a macro in the front-end in some cases, and an
8509 -- assignment can modify the component designated by N, so we need to
8510 -- create a temporary for it.
8512 -- The guard testing for Entity being present is needed at least in
8513 -- the case of rewritten predicate expressions, and may well also be
8514 -- appropriate elsewhere. Obviously we can't go testing the entity
8515 -- field if it does not exist, so it's reasonable to say that this is
8516 -- not the renaming case if it does not exist.
8518 elsif Is_Entity_Name (Original_Node (N))
8519 and then Present (Entity (Original_Node (N)))
8520 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
8521 and then Ekind (Entity (Original_Node (N))) /= E_Constant
8522 then
8523 declare
8524 RO : constant Node_Id :=
8525 Renamed_Object (Entity (Original_Node (N)));
8527 begin
8528 -- If the renamed object is an indexed component, or an
8529 -- explicit dereference, then the designated object could
8530 -- be modified by an assignment.
8532 if Nkind_In (RO, N_Indexed_Component,
8533 N_Explicit_Dereference)
8534 then
8535 return False;
8537 -- A selected component must have a safe prefix
8539 elsif Nkind (RO) = N_Selected_Component then
8540 return Safe_Prefixed_Reference (RO);
8542 -- In all other cases, designated object cannot be changed so
8543 -- we are side effect free.
8545 else
8546 return True;
8547 end if;
8548 end;
8550 -- Remove_Side_Effects generates an object renaming declaration to
8551 -- capture the expression of a class-wide expression. In VM targets
8552 -- the frontend performs no expansion for dispatching calls to
8553 -- class- wide types since they are handled by the VM. Hence, we must
8554 -- locate here if this node corresponds to a previous invocation of
8555 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
8557 elsif VM_Target /= No_VM
8558 and then not Comes_From_Source (N)
8559 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
8560 and then Is_Class_Wide_Type (Typ)
8561 then
8562 return True;
8563 end if;
8565 -- For other than entity names and compile time known values,
8566 -- check the node kind for special processing.
8568 case Nkind (N) is
8570 -- An attribute reference is side effect free if its expressions
8571 -- are side effect free and its prefix is side effect free or
8572 -- is an entity reference.
8574 -- Is this right? what about x'first where x is a variable???
8576 when N_Attribute_Reference =>
8577 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
8578 and then Attribute_Name (N) /= Name_Input
8579 and then (Is_Entity_Name (Prefix (N))
8580 or else Side_Effect_Free
8581 (Prefix (N), Name_Req, Variable_Ref));
8583 -- A binary operator is side effect free if and both operands are
8584 -- side effect free. For this purpose binary operators include
8585 -- membership tests and short circuit forms.
8587 when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
8588 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
8589 and then
8590 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
8592 -- An explicit dereference is side effect free only if it is
8593 -- a side effect free prefixed reference.
8595 when N_Explicit_Dereference =>
8596 return Safe_Prefixed_Reference (N);
8598 -- An expression with action is side effect free if its expression
8599 -- is side effect free and it has no actions.
8601 when N_Expression_With_Actions =>
8602 return Is_Empty_List (Actions (N))
8603 and then
8604 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8606 -- A call to _rep_to_pos is side effect free, since we generate
8607 -- this pure function call ourselves. Moreover it is critically
8608 -- important to make this exception, since otherwise we can have
8609 -- discriminants in array components which don't look side effect
8610 -- free in the case of an array whose index type is an enumeration
8611 -- type with an enumeration rep clause.
8613 -- All other function calls are not side effect free
8615 when N_Function_Call =>
8616 return Nkind (Name (N)) = N_Identifier
8617 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
8618 and then
8619 Side_Effect_Free
8620 (First (Parameter_Associations (N)), Name_Req, Variable_Ref);
8622 -- An IF expression is side effect free if it's of a scalar type, and
8623 -- all its components are all side effect free (conditions and then
8624 -- actions and else actions). We restrict to scalar types, since it
8625 -- is annoying to deal with things like (if A then B else C)'First
8626 -- where the type involved is a string type.
8628 when N_If_Expression =>
8629 return Is_Scalar_Type (Typ)
8630 and then
8631 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref);
8633 -- An indexed component is side effect free if it is a side
8634 -- effect free prefixed reference and all the indexing
8635 -- expressions are side effect free.
8637 when N_Indexed_Component =>
8638 return Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
8639 and then Safe_Prefixed_Reference (N);
8641 -- A type qualification is side effect free if the expression
8642 -- is side effect free.
8644 when N_Qualified_Expression =>
8645 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8647 -- A selected component is side effect free only if it is a side
8648 -- effect free prefixed reference. If it designates a component
8649 -- with a rep. clause it must be treated has having a potential
8650 -- side effect, because it may be modified through a renaming, and
8651 -- a subsequent use of the renaming as a macro will yield the
8652 -- wrong value. This complex interaction between renaming and
8653 -- removing side effects is a reminder that the latter has become
8654 -- a headache to maintain, and that it should be removed in favor
8655 -- of the gcc mechanism to capture values ???
8657 when N_Selected_Component =>
8658 if Nkind (Parent (N)) = N_Explicit_Dereference
8659 and then Has_Non_Standard_Rep (Designated_Type (Typ))
8660 then
8661 return False;
8662 else
8663 return Safe_Prefixed_Reference (N);
8664 end if;
8666 -- A range is side effect free if the bounds are side effect free
8668 when N_Range =>
8669 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
8670 and then
8671 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
8673 -- A slice is side effect free if it is a side effect free
8674 -- prefixed reference and the bounds are side effect free.
8676 when N_Slice =>
8677 return Side_Effect_Free
8678 (Discrete_Range (N), Name_Req, Variable_Ref)
8679 and then Safe_Prefixed_Reference (N);
8681 -- A type conversion is side effect free if the expression to be
8682 -- converted is side effect free.
8684 when N_Type_Conversion =>
8685 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8687 -- A unary operator is side effect free if the operand
8688 -- is side effect free.
8690 when N_Unary_Op =>
8691 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
8693 -- An unchecked type conversion is side effect free only if it
8694 -- is safe and its argument is side effect free.
8696 when N_Unchecked_Type_Conversion =>
8697 return Safe_Unchecked_Type_Conversion (N)
8698 and then
8699 Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8701 -- An unchecked expression is side effect free if its expression
8702 -- is side effect free.
8704 when N_Unchecked_Expression =>
8705 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
8707 -- A literal is side effect free
8709 when N_Character_Literal |
8710 N_Integer_Literal |
8711 N_Real_Literal |
8712 N_String_Literal =>
8713 return True;
8715 -- We consider that anything else has side effects. This is a bit
8716 -- crude, but we are pretty close for most common cases, and we
8717 -- are certainly correct (i.e. we never return True when the
8718 -- answer should be False).
8720 when others =>
8721 return False;
8722 end case;
8723 end Side_Effect_Free;
8725 -- A list is side effect free if all elements of the list are side
8726 -- effect free.
8728 function Side_Effect_Free
8729 (L : List_Id;
8730 Name_Req : Boolean := False;
8731 Variable_Ref : Boolean := False) return Boolean
8733 N : Node_Id;
8735 begin
8736 if L = No_List or else L = Error_List then
8737 return True;
8739 else
8740 N := First (L);
8741 while Present (N) loop
8742 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
8743 return False;
8744 else
8745 Next (N);
8746 end if;
8747 end loop;
8749 return True;
8750 end if;
8751 end Side_Effect_Free;
8753 ----------------------------------
8754 -- Silly_Boolean_Array_Not_Test --
8755 ----------------------------------
8757 -- This procedure implements an odd and silly test. We explicitly check
8758 -- for the case where the 'First of the component type is equal to the
8759 -- 'Last of this component type, and if this is the case, we make sure
8760 -- that constraint error is raised. The reason is that the NOT is bound
8761 -- to cause CE in this case, and we will not otherwise catch it.
8763 -- No such check is required for AND and OR, since for both these cases
8764 -- False op False = False, and True op True = True. For the XOR case,
8765 -- see Silly_Boolean_Array_Xor_Test.
8767 -- Believe it or not, this was reported as a bug. Note that nearly always,
8768 -- the test will evaluate statically to False, so the code will be
8769 -- statically removed, and no extra overhead caused.
8771 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
8772 Loc : constant Source_Ptr := Sloc (N);
8773 CT : constant Entity_Id := Component_Type (T);
8775 begin
8776 -- The check we install is
8778 -- constraint_error when
8779 -- component_type'first = component_type'last
8780 -- and then array_type'Length /= 0)
8782 -- We need the last guard because we don't want to raise CE for empty
8783 -- arrays since no out of range values result. (Empty arrays with a
8784 -- component type of True .. True -- very useful -- even the ACATS
8785 -- does not test that marginal case).
8787 Insert_Action (N,
8788 Make_Raise_Constraint_Error (Loc,
8789 Condition =>
8790 Make_And_Then (Loc,
8791 Left_Opnd =>
8792 Make_Op_Eq (Loc,
8793 Left_Opnd =>
8794 Make_Attribute_Reference (Loc,
8795 Prefix => New_Occurrence_Of (CT, Loc),
8796 Attribute_Name => Name_First),
8798 Right_Opnd =>
8799 Make_Attribute_Reference (Loc,
8800 Prefix => New_Occurrence_Of (CT, Loc),
8801 Attribute_Name => Name_Last)),
8803 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
8804 Reason => CE_Range_Check_Failed));
8805 end Silly_Boolean_Array_Not_Test;
8807 ----------------------------------
8808 -- Silly_Boolean_Array_Xor_Test --
8809 ----------------------------------
8811 -- This procedure implements an odd and silly test. We explicitly check
8812 -- for the XOR case where the component type is True .. True, since this
8813 -- will raise constraint error. A special check is required since CE
8814 -- will not be generated otherwise (cf Expand_Packed_Not).
8816 -- No such check is required for AND and OR, since for both these cases
8817 -- False op False = False, and True op True = True, and no check is
8818 -- required for the case of False .. False, since False xor False = False.
8819 -- See also Silly_Boolean_Array_Not_Test
8821 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
8822 Loc : constant Source_Ptr := Sloc (N);
8823 CT : constant Entity_Id := Component_Type (T);
8825 begin
8826 -- The check we install is
8828 -- constraint_error when
8829 -- Boolean (component_type'First)
8830 -- and then Boolean (component_type'Last)
8831 -- and then array_type'Length /= 0)
8833 -- We need the last guard because we don't want to raise CE for empty
8834 -- arrays since no out of range values result (Empty arrays with a
8835 -- component type of True .. True -- very useful -- even the ACATS
8836 -- does not test that marginal case).
8838 Insert_Action (N,
8839 Make_Raise_Constraint_Error (Loc,
8840 Condition =>
8841 Make_And_Then (Loc,
8842 Left_Opnd =>
8843 Make_And_Then (Loc,
8844 Left_Opnd =>
8845 Convert_To (Standard_Boolean,
8846 Make_Attribute_Reference (Loc,
8847 Prefix => New_Occurrence_Of (CT, Loc),
8848 Attribute_Name => Name_First)),
8850 Right_Opnd =>
8851 Convert_To (Standard_Boolean,
8852 Make_Attribute_Reference (Loc,
8853 Prefix => New_Occurrence_Of (CT, Loc),
8854 Attribute_Name => Name_Last))),
8856 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
8857 Reason => CE_Range_Check_Failed));
8858 end Silly_Boolean_Array_Xor_Test;
8860 --------------------------
8861 -- Target_Has_Fixed_Ops --
8862 --------------------------
8864 Integer_Sized_Small : Ureal;
8865 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
8866 -- called (we don't want to compute it more than once).
8868 Long_Integer_Sized_Small : Ureal;
8869 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
8870 -- is called (we don't want to compute it more than once)
8872 First_Time_For_THFO : Boolean := True;
8873 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
8875 function Target_Has_Fixed_Ops
8876 (Left_Typ : Entity_Id;
8877 Right_Typ : Entity_Id;
8878 Result_Typ : Entity_Id) return Boolean
8880 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
8881 -- Return True if the given type is a fixed-point type with a small
8882 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
8883 -- an absolute value less than 1.0. This is currently limited to
8884 -- fixed-point types that map to Integer or Long_Integer.
8886 ------------------------
8887 -- Is_Fractional_Type --
8888 ------------------------
8890 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
8891 begin
8892 if Esize (Typ) = Standard_Integer_Size then
8893 return Small_Value (Typ) = Integer_Sized_Small;
8895 elsif Esize (Typ) = Standard_Long_Integer_Size then
8896 return Small_Value (Typ) = Long_Integer_Sized_Small;
8898 else
8899 return False;
8900 end if;
8901 end Is_Fractional_Type;
8903 -- Start of processing for Target_Has_Fixed_Ops
8905 begin
8906 -- Return False if Fractional_Fixed_Ops_On_Target is false
8908 if not Fractional_Fixed_Ops_On_Target then
8909 return False;
8910 end if;
8912 -- Here the target has Fractional_Fixed_Ops, if first time, compute
8913 -- standard constants used by Is_Fractional_Type.
8915 if First_Time_For_THFO then
8916 First_Time_For_THFO := False;
8918 Integer_Sized_Small :=
8919 UR_From_Components
8920 (Num => Uint_1,
8921 Den => UI_From_Int (Standard_Integer_Size - 1),
8922 Rbase => 2);
8924 Long_Integer_Sized_Small :=
8925 UR_From_Components
8926 (Num => Uint_1,
8927 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
8928 Rbase => 2);
8929 end if;
8931 -- Return True if target supports fixed-by-fixed multiply/divide for
8932 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
8933 -- and result types are equivalent fractional types.
8935 return Is_Fractional_Type (Base_Type (Left_Typ))
8936 and then Is_Fractional_Type (Base_Type (Right_Typ))
8937 and then Is_Fractional_Type (Base_Type (Result_Typ))
8938 and then Esize (Left_Typ) = Esize (Right_Typ)
8939 and then Esize (Left_Typ) = Esize (Result_Typ);
8940 end Target_Has_Fixed_Ops;
8942 ------------------------------------------
8943 -- Type_May_Have_Bit_Aligned_Components --
8944 ------------------------------------------
8946 function Type_May_Have_Bit_Aligned_Components
8947 (Typ : Entity_Id) return Boolean
8949 begin
8950 -- Array type, check component type
8952 if Is_Array_Type (Typ) then
8953 return
8954 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
8956 -- Record type, check components
8958 elsif Is_Record_Type (Typ) then
8959 declare
8960 E : Entity_Id;
8962 begin
8963 E := First_Component_Or_Discriminant (Typ);
8964 while Present (E) loop
8965 if Component_May_Be_Bit_Aligned (E)
8966 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
8967 then
8968 return True;
8969 end if;
8971 Next_Component_Or_Discriminant (E);
8972 end loop;
8974 return False;
8975 end;
8977 -- Type other than array or record is always OK
8979 else
8980 return False;
8981 end if;
8982 end Type_May_Have_Bit_Aligned_Components;
8984 ----------------------------------
8985 -- Within_Case_Or_If_Expression --
8986 ----------------------------------
8988 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
8989 Par : Node_Id;
8991 begin
8992 -- Locate an enclosing case or if expression. Note that these constructs
8993 -- can be expanded into Expression_With_Actions, hence the test of the
8994 -- original node.
8996 Par := Parent (N);
8997 while Present (Par) loop
8998 if Nkind_In (Original_Node (Par), N_Case_Expression,
8999 N_If_Expression)
9000 then
9001 return True;
9003 -- Prevent the search from going too far
9005 elsif Is_Body_Or_Package_Declaration (Par) then
9006 return False;
9007 end if;
9009 Par := Parent (Par);
9010 end loop;
9012 return False;
9013 end Within_Case_Or_If_Expression;
9015 --------------------------------
9016 -- Within_Internal_Subprogram --
9017 --------------------------------
9019 function Within_Internal_Subprogram return Boolean is
9020 S : Entity_Id;
9022 begin
9023 S := Current_Scope;
9024 while Present (S) and then not Is_Subprogram (S) loop
9025 S := Scope (S);
9026 end loop;
9028 return Present (S)
9029 and then Get_TSS_Name (S) /= TSS_Null
9030 and then not Is_Predicate_Function (S);
9031 end Within_Internal_Subprogram;
9033 ----------------------------
9034 -- Wrap_Cleanup_Procedure --
9035 ----------------------------
9037 procedure Wrap_Cleanup_Procedure (N : Node_Id) is
9038 Loc : constant Source_Ptr := Sloc (N);
9039 Stseq : constant Node_Id := Handled_Statement_Sequence (N);
9040 Stmts : constant List_Id := Statements (Stseq);
9041 begin
9042 if Abort_Allowed then
9043 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
9044 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
9045 end if;
9046 end Wrap_Cleanup_Procedure;
9048 end Exp_Util;