PR rtl-optimization/82913
[official-gcc.git] / gcc / ada / exp_util.adb
blob6ebcc4c9794b19746ec6139f54ecf91c313b1fd5
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-2017, 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 Exp_Ch11; use Exp_Ch11;
38 with Ghost; use Ghost;
39 with Inline; use Inline;
40 with Itypes; use Itypes;
41 with Lib; use Lib;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Ttypes; use Ttypes;
66 with Urealp; use Urealp;
67 with Validsw; use Validsw;
69 with GNAT.HTable;
70 package body Exp_Util is
72 ---------------------------------------------------------
73 -- Handling of inherited class-wide pre/postconditions --
74 ---------------------------------------------------------
76 -- Following AI12-0113, the expression for a class-wide condition is
77 -- transformed for a subprogram that inherits it, by replacing calls
78 -- to primitive operations of the original controlling type into the
79 -- corresponding overriding operations of the derived type. The following
80 -- hash table manages this mapping, and is expanded on demand whenever
81 -- such inherited expression needs to be constructed.
83 -- The mapping is also used to check whether an inherited operation has
84 -- a condition that depends on overridden operations. For such an
85 -- operation we must create a wrapper that is then treated as a normal
86 -- overriding. In SPARK mode such operations are illegal.
88 -- For a given root type there may be several type extensions with their
89 -- own overriding operations, so at various times a given operation of
90 -- the root will be mapped into different overridings. The root type is
91 -- also mapped into the current type extension to indicate that its
92 -- operations are mapped into the overriding operations of that current
93 -- type extension.
95 -- The contents of the map are as follows:
97 -- Key Value
99 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
100 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
101 -- Discriminant (Entity_Id) Expression (Node_Id)
102 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
103 -- Type (Entity_Id) Type (Entity_Id)
105 Type_Map_Size : constant := 511;
107 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
108 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
110 package Type_Map is new GNAT.HTable.Simple_HTable
111 (Header_Num => Type_Map_Header,
112 Key => Entity_Id,
113 Element => Node_Or_Entity_Id,
114 No_element => Empty,
115 Hash => Type_Map_Hash,
116 Equal => "=");
118 -----------------------
119 -- Local Subprograms --
120 -----------------------
122 function Build_Task_Array_Image
123 (Loc : Source_Ptr;
124 Id_Ref : Node_Id;
125 A_Type : Entity_Id;
126 Dyn : Boolean := False) return Node_Id;
127 -- Build function to generate the image string for a task that is an array
128 -- component, concatenating the images of each index. To avoid storage
129 -- leaks, the string is built with successive slice assignments. The flag
130 -- Dyn indicates whether this is called for the initialization procedure of
131 -- an array of tasks, or for the name of a dynamically created task that is
132 -- assigned to an indexed component.
134 function Build_Task_Image_Function
135 (Loc : Source_Ptr;
136 Decls : List_Id;
137 Stats : List_Id;
138 Res : Entity_Id) return Node_Id;
139 -- Common processing for Task_Array_Image and Task_Record_Image. Build
140 -- function body that computes image.
142 procedure Build_Task_Image_Prefix
143 (Loc : Source_Ptr;
144 Len : out Entity_Id;
145 Res : out Entity_Id;
146 Pos : out Entity_Id;
147 Prefix : Entity_Id;
148 Sum : Node_Id;
149 Decls : List_Id;
150 Stats : List_Id);
151 -- Common processing for Task_Array_Image and Task_Record_Image. Create
152 -- local variables and assign prefix of name to result string.
154 function Build_Task_Record_Image
155 (Loc : Source_Ptr;
156 Id_Ref : Node_Id;
157 Dyn : Boolean := False) return Node_Id;
158 -- Build function to generate the image string for a task that is a record
159 -- component. Concatenate name of variable with that of selector. The flag
160 -- Dyn indicates whether this is called for the initialization procedure of
161 -- record with task components, or for a dynamically created task that is
162 -- assigned to a selected component.
164 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
165 -- Force evaluation of bounds of a slice, which may be given by a range
166 -- or by a subtype indication with or without a constraint.
168 function Make_CW_Equivalent_Type
169 (T : Entity_Id;
170 E : Node_Id) return Entity_Id;
171 -- T is a class-wide type entity, E is the initial expression node that
172 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
173 -- returns the entity of the Equivalent type and inserts on the fly the
174 -- necessary declaration such as:
176 -- type anon is record
177 -- _parent : Root_Type (T); constrained with E discriminants (if any)
178 -- Extension : String (1 .. expr to match size of E);
179 -- end record;
181 -- This record is compatible with any object of the class of T thanks to
182 -- the first field and has the same size as E thanks to the second.
184 function Make_Literal_Range
185 (Loc : Source_Ptr;
186 Literal_Typ : Entity_Id) return Node_Id;
187 -- Produce a Range node whose bounds are:
188 -- Low_Bound (Literal_Type) ..
189 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
190 -- this is used for expanding declarations like X : String := "sdfgdfg";
192 -- If the index type of the target array is not integer, we generate:
193 -- Low_Bound (Literal_Type) ..
194 -- Literal_Type'Val
195 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
196 -- + (Length (Literal_Typ) -1))
198 function Make_Non_Empty_Check
199 (Loc : Source_Ptr;
200 N : Node_Id) return Node_Id;
201 -- Produce a boolean expression checking that the unidimensional array
202 -- node N is not empty.
204 function New_Class_Wide_Subtype
205 (CW_Typ : Entity_Id;
206 N : Node_Id) return Entity_Id;
207 -- Create an implicit subtype of CW_Typ attached to node N
209 function Requires_Cleanup_Actions
210 (L : List_Id;
211 Lib_Level : Boolean;
212 Nested_Constructs : Boolean) return Boolean;
213 -- Given a list L, determine whether it contains one of the following:
215 -- 1) controlled objects
216 -- 2) library-level tagged types
218 -- Lib_Level is True when the list comes from a construct at the library
219 -- level, and False otherwise. Nested_Constructs is True when any nested
220 -- packages declared in L must be processed, and False otherwise.
222 -------------------------------------
223 -- Activate_Atomic_Synchronization --
224 -------------------------------------
226 procedure Activate_Atomic_Synchronization (N : Node_Id) is
227 Msg_Node : Node_Id;
229 begin
230 case Nkind (Parent (N)) is
232 -- Check for cases of appearing in the prefix of a construct where we
233 -- don't need atomic synchronization for this kind of usage.
235 when
236 -- Nothing to do if we are the prefix of an attribute, since we
237 -- do not want an atomic sync operation for things like 'Size.
239 N_Attribute_Reference
241 -- The N_Reference node is like an attribute
243 | N_Reference
245 -- Nothing to do for a reference to a component (or components)
246 -- of a composite object. Only reads and updates of the object
247 -- as a whole require atomic synchronization (RM C.6 (15)).
249 | N_Indexed_Component
250 | N_Selected_Component
251 | N_Slice
253 -- For all the above cases, nothing to do if we are the prefix
255 if Prefix (Parent (N)) = N then
256 return;
257 end if;
259 when others =>
260 null;
261 end case;
263 -- Nothing to do for the identifier in an object renaming declaration,
264 -- the renaming itself does not need atomic synchronization.
266 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
267 return;
268 end if;
270 -- Go ahead and set the flag
272 Set_Atomic_Sync_Required (N);
274 -- Generate info message if requested
276 if Warn_On_Atomic_Synchronization then
277 case Nkind (N) is
278 when N_Identifier =>
279 Msg_Node := N;
281 when N_Expanded_Name
282 | N_Selected_Component
284 Msg_Node := Selector_Name (N);
286 when N_Explicit_Dereference
287 | N_Indexed_Component
289 Msg_Node := Empty;
291 when others =>
292 pragma Assert (False);
293 return;
294 end case;
296 if Present (Msg_Node) then
297 Error_Msg_N
298 ("info: atomic synchronization set for &?N?", Msg_Node);
299 else
300 Error_Msg_N
301 ("info: atomic synchronization set?N?", N);
302 end if;
303 end if;
304 end Activate_Atomic_Synchronization;
306 ----------------------
307 -- Adjust_Condition --
308 ----------------------
310 procedure Adjust_Condition (N : Node_Id) is
311 begin
312 if No (N) then
313 return;
314 end if;
316 declare
317 Loc : constant Source_Ptr := Sloc (N);
318 T : constant Entity_Id := Etype (N);
319 Ti : Entity_Id;
321 begin
322 -- Defend against a call where the argument has no type, or has a
323 -- type that is not Boolean. This can occur because of prior errors.
325 if No (T) or else not Is_Boolean_Type (T) then
326 return;
327 end if;
329 -- Apply validity checking if needed
331 if Validity_Checks_On and Validity_Check_Tests then
332 Ensure_Valid (N);
333 end if;
335 -- Immediate return if standard boolean, the most common case,
336 -- where nothing needs to be done.
338 if Base_Type (T) = Standard_Boolean then
339 return;
340 end if;
342 -- Case of zero/non-zero semantics or non-standard enumeration
343 -- representation. In each case, we rewrite the node as:
345 -- ityp!(N) /= False'Enum_Rep
347 -- where ityp is an integer type with large enough size to hold any
348 -- value of type T.
350 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
351 if Esize (T) <= Esize (Standard_Integer) then
352 Ti := Standard_Integer;
353 else
354 Ti := Standard_Long_Long_Integer;
355 end if;
357 Rewrite (N,
358 Make_Op_Ne (Loc,
359 Left_Opnd => Unchecked_Convert_To (Ti, N),
360 Right_Opnd =>
361 Make_Attribute_Reference (Loc,
362 Attribute_Name => Name_Enum_Rep,
363 Prefix =>
364 New_Occurrence_Of (First_Literal (T), Loc))));
365 Analyze_And_Resolve (N, Standard_Boolean);
367 else
368 Rewrite (N, Convert_To (Standard_Boolean, N));
369 Analyze_And_Resolve (N, Standard_Boolean);
370 end if;
371 end;
372 end Adjust_Condition;
374 ------------------------
375 -- Adjust_Result_Type --
376 ------------------------
378 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
379 begin
380 -- Ignore call if current type is not Standard.Boolean
382 if Etype (N) /= Standard_Boolean then
383 return;
384 end if;
386 -- If result is already of correct type, nothing to do. Note that
387 -- this will get the most common case where everything has a type
388 -- of Standard.Boolean.
390 if Base_Type (T) = Standard_Boolean then
391 return;
393 else
394 declare
395 KP : constant Node_Kind := Nkind (Parent (N));
397 begin
398 -- If result is to be used as a Condition in the syntax, no need
399 -- to convert it back, since if it was changed to Standard.Boolean
400 -- using Adjust_Condition, that is just fine for this usage.
402 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
403 return;
405 -- If result is an operand of another logical operation, no need
406 -- to reset its type, since Standard.Boolean is just fine, and
407 -- such operations always do Adjust_Condition on their operands.
409 elsif KP in N_Op_Boolean
410 or else KP in N_Short_Circuit
411 or else KP = N_Op_Not
412 then
413 return;
415 -- Otherwise we perform a conversion from the current type, which
416 -- must be Standard.Boolean, to the desired type. Use the base
417 -- type to prevent spurious constraint checks that are extraneous
418 -- to the transformation. The type and its base have the same
419 -- representation, standard or otherwise.
421 else
422 Set_Analyzed (N);
423 Rewrite (N, Convert_To (Base_Type (T), N));
424 Analyze_And_Resolve (N, Base_Type (T));
425 end if;
426 end;
427 end if;
428 end Adjust_Result_Type;
430 --------------------------
431 -- Append_Freeze_Action --
432 --------------------------
434 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
435 Fnode : Node_Id;
437 begin
438 Ensure_Freeze_Node (T);
439 Fnode := Freeze_Node (T);
441 if No (Actions (Fnode)) then
442 Set_Actions (Fnode, New_List (N));
443 else
444 Append (N, Actions (Fnode));
445 end if;
447 end Append_Freeze_Action;
449 ---------------------------
450 -- Append_Freeze_Actions --
451 ---------------------------
453 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
454 Fnode : Node_Id;
456 begin
457 if No (L) then
458 return;
459 end if;
461 Ensure_Freeze_Node (T);
462 Fnode := Freeze_Node (T);
464 if No (Actions (Fnode)) then
465 Set_Actions (Fnode, L);
466 else
467 Append_List (L, Actions (Fnode));
468 end if;
469 end Append_Freeze_Actions;
471 ------------------------------------
472 -- Build_Allocate_Deallocate_Proc --
473 ------------------------------------
475 procedure Build_Allocate_Deallocate_Proc
476 (N : Node_Id;
477 Is_Allocate : Boolean)
479 function Find_Object (E : Node_Id) return Node_Id;
480 -- Given an arbitrary expression of an allocator, try to find an object
481 -- reference in it, otherwise return the original expression.
483 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
484 -- Determine whether subprogram Subp denotes a custom allocate or
485 -- deallocate.
487 -----------------
488 -- Find_Object --
489 -----------------
491 function Find_Object (E : Node_Id) return Node_Id is
492 Expr : Node_Id;
494 begin
495 pragma Assert (Is_Allocate);
497 Expr := E;
498 loop
499 if Nkind (Expr) = N_Explicit_Dereference then
500 Expr := Prefix (Expr);
502 elsif Nkind (Expr) = N_Qualified_Expression then
503 Expr := Expression (Expr);
505 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
507 -- When interface class-wide types are involved in allocation,
508 -- the expander introduces several levels of address arithmetic
509 -- to perform dispatch table displacement. In this scenario the
510 -- object appears as:
512 -- Tag_Ptr (Base_Address (<object>'Address))
514 -- Detect this case and utilize the whole expression as the
515 -- "object" since it now points to the proper dispatch table.
517 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
518 exit;
520 -- Continue to strip the object
522 else
523 Expr := Expression (Expr);
524 end if;
526 else
527 exit;
528 end if;
529 end loop;
531 return Expr;
532 end Find_Object;
534 ---------------------------------
535 -- Is_Allocate_Deallocate_Proc --
536 ---------------------------------
538 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
539 begin
540 -- Look for a subprogram body with only one statement which is a
541 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
543 if Ekind (Subp) = E_Procedure
544 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
545 then
546 declare
547 HSS : constant Node_Id :=
548 Handled_Statement_Sequence (Parent (Parent (Subp)));
549 Proc : Entity_Id;
551 begin
552 if Present (Statements (HSS))
553 and then Nkind (First (Statements (HSS))) =
554 N_Procedure_Call_Statement
555 then
556 Proc := Entity (Name (First (Statements (HSS))));
558 return
559 Is_RTE (Proc, RE_Allocate_Any_Controlled)
560 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
561 end if;
562 end;
563 end if;
565 return False;
566 end Is_Allocate_Deallocate_Proc;
568 -- Local variables
570 Desig_Typ : Entity_Id;
571 Expr : Node_Id;
572 Needs_Fin : Boolean;
573 Pool_Id : Entity_Id;
574 Proc_To_Call : Node_Id := Empty;
575 Ptr_Typ : Entity_Id;
577 -- Start of processing for Build_Allocate_Deallocate_Proc
579 begin
580 -- Obtain the attributes of the allocation / deallocation
582 if Nkind (N) = N_Free_Statement then
583 Expr := Expression (N);
584 Ptr_Typ := Base_Type (Etype (Expr));
585 Proc_To_Call := Procedure_To_Call (N);
587 else
588 if Nkind (N) = N_Object_Declaration then
589 Expr := Expression (N);
590 else
591 Expr := N;
592 end if;
594 -- In certain cases an allocator with a qualified expression may
595 -- be relocated and used as the initialization expression of a
596 -- temporary:
598 -- before:
599 -- Obj : Ptr_Typ := new Desig_Typ'(...);
601 -- after:
602 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
603 -- Obj : Ptr_Typ := Tmp;
605 -- Since the allocator is always marked as analyzed to avoid infinite
606 -- expansion, it will never be processed by this routine given that
607 -- the designated type needs finalization actions. Detect this case
608 -- and complete the expansion of the allocator.
610 if Nkind (Expr) = N_Identifier
611 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
612 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
613 then
614 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
615 return;
616 end if;
618 -- The allocator may have been rewritten into something else in which
619 -- case the expansion performed by this routine does not apply.
621 if Nkind (Expr) /= N_Allocator then
622 return;
623 end if;
625 Ptr_Typ := Base_Type (Etype (Expr));
626 Proc_To_Call := Procedure_To_Call (Expr);
627 end if;
629 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
630 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
632 -- Handle concurrent types
634 if Is_Concurrent_Type (Desig_Typ)
635 and then Present (Corresponding_Record_Type (Desig_Typ))
636 then
637 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
638 end if;
640 -- Do not process allocations / deallocations without a pool
642 if No (Pool_Id) then
643 return;
645 -- Do not process allocations on / deallocations from the secondary
646 -- stack.
648 elsif Is_RTE (Pool_Id, RE_SS_Pool)
649 or else (Nkind (Expr) = N_Allocator
650 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool))
651 then
652 return;
654 -- Optimize the case where we are using the default Global_Pool_Object,
655 -- and we don't need the heavy finalization machinery.
657 elsif Pool_Id = RTE (RE_Global_Pool_Object)
658 and then not Needs_Finalization (Desig_Typ)
659 then
660 return;
662 -- Do not replicate the machinery if the allocator / free has already
663 -- been expanded and has a custom Allocate / Deallocate.
665 elsif Present (Proc_To_Call)
666 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
667 then
668 return;
669 end if;
671 -- Finalization actions are required when the object to be allocated or
672 -- deallocated needs these actions and the associated access type is not
673 -- subject to pragma No_Heap_Finalization.
675 Needs_Fin :=
676 Needs_Finalization (Desig_Typ)
677 and then not No_Heap_Finalization (Ptr_Typ);
679 if Needs_Fin then
681 -- Certain run-time configurations and targets do not provide support
682 -- for controlled types.
684 if Restriction_Active (No_Finalization) then
685 return;
687 -- Do nothing if the access type may never allocate / deallocate
688 -- objects.
690 elsif No_Pool_Assigned (Ptr_Typ) then
691 return;
692 end if;
694 -- The allocation / deallocation of a controlled object must be
695 -- chained on / detached from a finalization master.
697 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
699 -- The only other kind of allocation / deallocation supported by this
700 -- routine is on / from a subpool.
702 elsif Nkind (Expr) = N_Allocator
703 and then No (Subpool_Handle_Name (Expr))
704 then
705 return;
706 end if;
708 declare
709 Loc : constant Source_Ptr := Sloc (N);
710 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
711 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
712 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
713 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
715 Actuals : List_Id;
716 Fin_Addr_Id : Entity_Id;
717 Fin_Mas_Act : Node_Id;
718 Fin_Mas_Id : Entity_Id;
719 Proc_To_Call : Entity_Id;
720 Subpool : Node_Id := Empty;
722 begin
723 -- Step 1: Construct all the actuals for the call to library routine
724 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
726 -- a) Storage pool
728 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
730 if Is_Allocate then
732 -- b) Subpool
734 if Nkind (Expr) = N_Allocator then
735 Subpool := Subpool_Handle_Name (Expr);
736 end if;
738 -- If a subpool is present it can be an arbitrary name, so make
739 -- the actual by copying the tree.
741 if Present (Subpool) then
742 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
743 else
744 Append_To (Actuals, Make_Null (Loc));
745 end if;
747 -- c) Finalization master
749 if Needs_Fin then
750 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
751 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
753 -- Handle the case where the master is actually a pointer to a
754 -- master. This case arises in build-in-place functions.
756 if Is_Access_Type (Etype (Fin_Mas_Id)) then
757 Append_To (Actuals, Fin_Mas_Act);
758 else
759 Append_To (Actuals,
760 Make_Attribute_Reference (Loc,
761 Prefix => Fin_Mas_Act,
762 Attribute_Name => Name_Unrestricted_Access));
763 end if;
764 else
765 Append_To (Actuals, Make_Null (Loc));
766 end if;
768 -- d) Finalize_Address
770 -- Primitive Finalize_Address is never generated in CodePeer mode
771 -- since it contains an Unchecked_Conversion.
773 if Needs_Fin and then not CodePeer_Mode then
774 Fin_Addr_Id := Finalize_Address (Desig_Typ);
775 pragma Assert (Present (Fin_Addr_Id));
777 Append_To (Actuals,
778 Make_Attribute_Reference (Loc,
779 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
780 Attribute_Name => Name_Unrestricted_Access));
781 else
782 Append_To (Actuals, Make_Null (Loc));
783 end if;
784 end if;
786 -- e) Address
787 -- f) Storage_Size
788 -- g) Alignment
790 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
791 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
793 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
794 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
796 -- For deallocation of class-wide types we obtain the value of
797 -- alignment from the Type Specific Record of the deallocated object.
798 -- This is needed because the frontend expansion of class-wide types
799 -- into equivalent types confuses the back end.
801 else
802 -- Generate:
803 -- Obj.all'Alignment
805 -- ... because 'Alignment applied to class-wide types is expanded
806 -- into the code that reads the value of alignment from the TSD
807 -- (see Expand_N_Attribute_Reference)
809 Append_To (Actuals,
810 Unchecked_Convert_To (RTE (RE_Storage_Offset),
811 Make_Attribute_Reference (Loc,
812 Prefix =>
813 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
814 Attribute_Name => Name_Alignment)));
815 end if;
817 -- h) Is_Controlled
819 if Needs_Fin then
820 Is_Controlled : declare
821 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
822 Flag_Expr : Node_Id;
823 Param : Node_Id;
824 Pref : Node_Id;
825 Temp : Node_Id;
827 begin
828 if Is_Allocate then
829 Temp := Find_Object (Expression (Expr));
830 else
831 Temp := Expr;
832 end if;
834 -- Processing for allocations where the expression is a subtype
835 -- indication.
837 if Is_Allocate
838 and then Is_Entity_Name (Temp)
839 and then Is_Type (Entity (Temp))
840 then
841 Flag_Expr :=
842 New_Occurrence_Of
843 (Boolean_Literals
844 (Needs_Finalization (Entity (Temp))), Loc);
846 -- The allocation / deallocation of a class-wide object relies
847 -- on a runtime check to determine whether the object is truly
848 -- controlled or not. Depending on this check, the finalization
849 -- machinery will request or reclaim extra storage reserved for
850 -- a list header.
852 elsif Is_Class_Wide_Type (Desig_Typ) then
854 -- Detect a special case where interface class-wide types
855 -- are involved as the object appears as:
857 -- Tag_Ptr (Base_Address (<object>'Address))
859 -- The expression already yields the proper tag, generate:
861 -- Temp.all
863 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
864 Param :=
865 Make_Explicit_Dereference (Loc,
866 Prefix => Relocate_Node (Temp));
868 -- In the default case, obtain the tag of the object about
869 -- to be allocated / deallocated. Generate:
871 -- Temp'Tag
873 -- If the object is an unchecked conversion (typically to
874 -- an access to class-wide type), we must preserve the
875 -- conversion to ensure that the object is seen as tagged
876 -- in the code that follows.
878 else
879 Pref := Temp;
881 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
882 then
883 Pref := Parent (Pref);
884 end if;
886 Param :=
887 Make_Attribute_Reference (Loc,
888 Prefix => Relocate_Node (Pref),
889 Attribute_Name => Name_Tag);
890 end if;
892 -- Generate:
893 -- Needs_Finalization (<Param>)
895 Flag_Expr :=
896 Make_Function_Call (Loc,
897 Name =>
898 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
899 Parameter_Associations => New_List (Param));
901 -- Processing for generic actuals
903 elsif Is_Generic_Actual_Type (Desig_Typ) then
904 Flag_Expr :=
905 New_Occurrence_Of (Boolean_Literals
906 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
908 -- The object does not require any specialized checks, it is
909 -- known to be controlled.
911 else
912 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
913 end if;
915 -- Create the temporary which represents the finalization state
916 -- of the expression. Generate:
918 -- F : constant Boolean := <Flag_Expr>;
920 Insert_Action (N,
921 Make_Object_Declaration (Loc,
922 Defining_Identifier => Flag_Id,
923 Constant_Present => True,
924 Object_Definition =>
925 New_Occurrence_Of (Standard_Boolean, Loc),
926 Expression => Flag_Expr));
928 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
929 end Is_Controlled;
931 -- The object is not controlled
933 else
934 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
935 end if;
937 -- i) On_Subpool
939 if Is_Allocate then
940 Append_To (Actuals,
941 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
942 end if;
944 -- Step 2: Build a wrapper Allocate / Deallocate which internally
945 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
947 -- Select the proper routine to call
949 if Is_Allocate then
950 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
951 else
952 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
953 end if;
955 -- Create a custom Allocate / Deallocate routine which has identical
956 -- profile to that of System.Storage_Pools.
958 Insert_Action (N,
959 Make_Subprogram_Body (Loc,
960 Specification =>
962 -- procedure Pnn
964 Make_Procedure_Specification (Loc,
965 Defining_Unit_Name => Proc_Id,
966 Parameter_Specifications => New_List (
968 -- P : Root_Storage_Pool
970 Make_Parameter_Specification (Loc,
971 Defining_Identifier => Make_Temporary (Loc, 'P'),
972 Parameter_Type =>
973 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
975 -- A : [out] Address
977 Make_Parameter_Specification (Loc,
978 Defining_Identifier => Addr_Id,
979 Out_Present => Is_Allocate,
980 Parameter_Type =>
981 New_Occurrence_Of (RTE (RE_Address), Loc)),
983 -- S : Storage_Count
985 Make_Parameter_Specification (Loc,
986 Defining_Identifier => Size_Id,
987 Parameter_Type =>
988 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
990 -- L : Storage_Count
992 Make_Parameter_Specification (Loc,
993 Defining_Identifier => Alig_Id,
994 Parameter_Type =>
995 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
997 Declarations => No_List,
999 Handled_Statement_Sequence =>
1000 Make_Handled_Sequence_Of_Statements (Loc,
1001 Statements => New_List (
1002 Make_Procedure_Call_Statement (Loc,
1003 Name =>
1004 New_Occurrence_Of (Proc_To_Call, Loc),
1005 Parameter_Associations => Actuals)))),
1006 Suppress => All_Checks);
1008 -- The newly generated Allocate / Deallocate becomes the default
1009 -- procedure to call when the back end processes the allocation /
1010 -- deallocation.
1012 if Is_Allocate then
1013 Set_Procedure_To_Call (Expr, Proc_Id);
1014 else
1015 Set_Procedure_To_Call (N, Proc_Id);
1016 end if;
1017 end;
1018 end Build_Allocate_Deallocate_Proc;
1020 -------------------------------
1021 -- Build_Abort_Undefer_Block --
1022 -------------------------------
1024 function Build_Abort_Undefer_Block
1025 (Loc : Source_Ptr;
1026 Stmts : List_Id;
1027 Context : Node_Id) return Node_Id
1029 Exceptions_OK : constant Boolean :=
1030 not Restriction_Active (No_Exception_Propagation);
1032 AUD : Entity_Id;
1033 Blk : Node_Id;
1034 Blk_Id : Entity_Id;
1035 HSS : Node_Id;
1037 begin
1038 -- The block should be generated only when undeferring abort in the
1039 -- context of a potential exception.
1041 pragma Assert (Abort_Allowed and Exceptions_OK);
1043 -- Generate:
1044 -- begin
1045 -- <Stmts>
1046 -- at end
1047 -- Abort_Undefer_Direct;
1048 -- end;
1050 AUD := RTE (RE_Abort_Undefer_Direct);
1052 HSS :=
1053 Make_Handled_Sequence_Of_Statements (Loc,
1054 Statements => Stmts,
1055 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1057 Blk :=
1058 Make_Block_Statement (Loc,
1059 Handled_Statement_Sequence => HSS);
1060 Set_Is_Abort_Block (Blk);
1062 Add_Block_Identifier (Blk, Blk_Id);
1063 Expand_At_End_Handler (HSS, Blk_Id);
1065 -- Present the Abort_Undefer_Direct function to the back end to inline
1066 -- the call to the routine.
1068 Add_Inlined_Body (AUD, Context);
1070 return Blk;
1071 end Build_Abort_Undefer_Block;
1073 ---------------------------------
1074 -- Build_Class_Wide_Expression --
1075 ---------------------------------
1077 procedure Build_Class_Wide_Expression
1078 (Prag : Node_Id;
1079 Subp : Entity_Id;
1080 Par_Subp : Entity_Id;
1081 Adjust_Sloc : Boolean;
1082 Needs_Wrapper : out Boolean)
1084 function Replace_Entity (N : Node_Id) return Traverse_Result;
1085 -- Replace reference to formal of inherited operation or to primitive
1086 -- operation of root type, with corresponding entity for derived type,
1087 -- when constructing the class-wide condition of an overriding
1088 -- subprogram.
1090 --------------------
1091 -- Replace_Entity --
1092 --------------------
1094 function Replace_Entity (N : Node_Id) return Traverse_Result is
1095 New_E : Entity_Id;
1097 begin
1098 if Adjust_Sloc then
1099 Adjust_Inherited_Pragma_Sloc (N);
1100 end if;
1102 if Nkind (N) = N_Identifier
1103 and then Present (Entity (N))
1104 and then
1105 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1106 and then
1107 (Nkind (Parent (N)) /= N_Attribute_Reference
1108 or else Attribute_Name (Parent (N)) /= Name_Class)
1109 then
1110 -- The replacement does not apply to dispatching calls within the
1111 -- condition, but only to calls whose static tag is that of the
1112 -- parent type.
1114 if Is_Subprogram (Entity (N))
1115 and then Nkind (Parent (N)) = N_Function_Call
1116 and then Present (Controlling_Argument (Parent (N)))
1117 then
1118 return OK;
1119 end if;
1121 -- Determine whether entity has a renaming
1123 New_E := Type_Map.Get (Entity (N));
1125 if Present (New_E) then
1126 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1128 -- If the entity is an overridden primitive and we are not
1129 -- in GNATprove mode, we must build a wrapper for the current
1130 -- inherited operation. If the reference is the prefix of an
1131 -- attribute such as 'Result (or others ???) there is no need
1132 -- for a wrapper: the condition is just rewritten in terms of
1133 -- the inherited subprogram.
1135 if Is_Subprogram (New_E)
1136 and then Nkind (Parent (N)) /= N_Attribute_Reference
1137 and then not GNATprove_Mode
1138 then
1139 Needs_Wrapper := True;
1140 end if;
1141 end if;
1143 -- Check that there are no calls left to abstract operations if
1144 -- the current subprogram is not abstract.
1146 if Nkind (Parent (N)) = N_Function_Call
1147 and then N = Name (Parent (N))
1148 then
1149 if not Is_Abstract_Subprogram (Subp)
1150 and then Is_Abstract_Subprogram (Entity (N))
1151 then
1152 Error_Msg_Sloc := Sloc (Current_Scope);
1153 Error_Msg_Node_2 := Subp;
1154 if Comes_From_Source (Subp) then
1155 Error_Msg_NE
1156 ("cannot call abstract subprogram & in inherited "
1157 & "condition for&#", Subp, Entity (N));
1158 else
1159 Error_Msg_NE
1160 ("cannot call abstract subprogram & in inherited "
1161 & "condition for inherited&#", Subp, Entity (N));
1162 end if;
1164 -- In SPARK mode, reject an inherited condition for an
1165 -- inherited operation if it contains a call to an overriding
1166 -- operation, because this implies that the pre/postconditions
1167 -- of the inherited operation have changed silently.
1169 elsif SPARK_Mode = On
1170 and then Warn_On_Suspicious_Contract
1171 and then Present (Alias (Subp))
1172 and then Present (New_E)
1173 and then Comes_From_Source (New_E)
1174 then
1175 Error_Msg_N
1176 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1177 Parent (Subp));
1178 Error_Msg_Sloc := Sloc (New_E);
1179 Error_Msg_Node_2 := Subp;
1180 Error_Msg_NE
1181 ("\overriding of&# forces overriding of&",
1182 Parent (Subp), New_E);
1183 end if;
1184 end if;
1186 -- Update type of function call node, which should be the same as
1187 -- the function's return type.
1189 if Is_Subprogram (Entity (N))
1190 and then Nkind (Parent (N)) = N_Function_Call
1191 then
1192 Set_Etype (Parent (N), Etype (Entity (N)));
1193 end if;
1195 -- The whole expression will be reanalyzed
1197 elsif Nkind (N) in N_Has_Etype then
1198 Set_Analyzed (N, False);
1199 end if;
1201 return OK;
1202 end Replace_Entity;
1204 procedure Replace_Condition_Entities is
1205 new Traverse_Proc (Replace_Entity);
1207 -- Local variables
1209 Par_Formal : Entity_Id;
1210 Subp_Formal : Entity_Id;
1212 -- Start of processing for Build_Class_Wide_Expression
1214 begin
1215 Needs_Wrapper := False;
1217 -- Add mapping from old formals to new formals
1219 Par_Formal := First_Formal (Par_Subp);
1220 Subp_Formal := First_Formal (Subp);
1222 while Present (Par_Formal) and then Present (Subp_Formal) loop
1223 Type_Map.Set (Par_Formal, Subp_Formal);
1224 Next_Formal (Par_Formal);
1225 Next_Formal (Subp_Formal);
1226 end loop;
1228 Replace_Condition_Entities (Prag);
1229 end Build_Class_Wide_Expression;
1231 --------------------
1232 -- Build_DIC_Call --
1233 --------------------
1235 function Build_DIC_Call
1236 (Loc : Source_Ptr;
1237 Obj_Id : Entity_Id;
1238 Typ : Entity_Id) return Node_Id
1240 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1241 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1243 begin
1244 return
1245 Make_Procedure_Call_Statement (Loc,
1246 Name => New_Occurrence_Of (Proc_Id, Loc),
1247 Parameter_Associations => New_List (
1248 Make_Unchecked_Type_Conversion (Loc,
1249 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1250 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1251 end Build_DIC_Call;
1253 ------------------------------
1254 -- Build_DIC_Procedure_Body --
1255 ------------------------------
1257 -- WARNING: This routine manages Ghost regions. Return statements must be
1258 -- replaced by gotos which jump to the end of the routine and restore the
1259 -- Ghost mode.
1261 procedure Build_DIC_Procedure_Body
1262 (Typ : Entity_Id;
1263 For_Freeze : Boolean := False)
1265 procedure Add_DIC_Check
1266 (DIC_Prag : Node_Id;
1267 DIC_Expr : Node_Id;
1268 Stmts : in out List_Id);
1269 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1270 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1271 -- is added to list Stmts.
1273 procedure Add_Inherited_DIC
1274 (DIC_Prag : Node_Id;
1275 Par_Typ : Entity_Id;
1276 Deriv_Typ : Entity_Id;
1277 Stmts : in out List_Id);
1278 -- Add a runtime check to verify the assertion expression of inherited
1279 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1280 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1281 -- pragma. All generated code is added to list Stmts.
1283 procedure Add_Inherited_Tagged_DIC
1284 (DIC_Prag : Node_Id;
1285 Par_Typ : Entity_Id;
1286 Deriv_Typ : Entity_Id;
1287 Stmts : in out List_Id);
1288 -- Add a runtime check to verify assertion expression DIC_Expr of
1289 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1290 -- postcondition-like runtime semantics to the check. Par_Typ is the
1291 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1292 -- derived type inheriting the DIC pragma. All generated code is added
1293 -- to list Stmts.
1295 procedure Add_Own_DIC
1296 (DIC_Prag : Node_Id;
1297 DIC_Typ : Entity_Id;
1298 Stmts : in out List_Id);
1299 -- Add a runtime check to verify the assertion expression of pragma
1300 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1301 -- is added to list Stmts.
1303 -------------------
1304 -- Add_DIC_Check --
1305 -------------------
1307 procedure Add_DIC_Check
1308 (DIC_Prag : Node_Id;
1309 DIC_Expr : Node_Id;
1310 Stmts : in out List_Id)
1312 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1313 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1315 begin
1316 -- The DIC pragma is ignored, nothing left to do
1318 if Is_Ignored (DIC_Prag) then
1319 null;
1321 -- Otherwise the DIC expression must be checked at run time.
1322 -- Generate:
1324 -- pragma Check (<Nam>, <DIC_Expr>);
1326 else
1327 Append_New_To (Stmts,
1328 Make_Pragma (Loc,
1329 Pragma_Identifier =>
1330 Make_Identifier (Loc, Name_Check),
1332 Pragma_Argument_Associations => New_List (
1333 Make_Pragma_Argument_Association (Loc,
1334 Expression => Make_Identifier (Loc, Nam)),
1336 Make_Pragma_Argument_Association (Loc,
1337 Expression => DIC_Expr))));
1338 end if;
1339 end Add_DIC_Check;
1341 -----------------------
1342 -- Add_Inherited_DIC --
1343 -----------------------
1345 procedure Add_Inherited_DIC
1346 (DIC_Prag : Node_Id;
1347 Par_Typ : Entity_Id;
1348 Deriv_Typ : Entity_Id;
1349 Stmts : in out List_Id)
1351 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1352 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1353 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1354 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1355 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1357 begin
1358 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1360 -- Verify the inherited DIC assertion expression by calling the DIC
1361 -- procedure of the parent type.
1363 -- Generate:
1364 -- <Par_Typ>DIC (Par_Typ (_object));
1366 Append_New_To (Stmts,
1367 Make_Procedure_Call_Statement (Loc,
1368 Name => New_Occurrence_Of (Par_Proc, Loc),
1369 Parameter_Associations => New_List (
1370 Convert_To
1371 (Typ => Etype (Par_Obj),
1372 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1373 end Add_Inherited_DIC;
1375 ------------------------------
1376 -- Add_Inherited_Tagged_DIC --
1377 ------------------------------
1379 procedure Add_Inherited_Tagged_DIC
1380 (DIC_Prag : Node_Id;
1381 Par_Typ : Entity_Id;
1382 Deriv_Typ : Entity_Id;
1383 Stmts : in out List_Id)
1385 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1386 DIC_Args : constant List_Id :=
1387 Pragma_Argument_Associations (DIC_Prag);
1388 DIC_Arg : constant Node_Id := First (DIC_Args);
1389 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1390 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1392 Expr : Node_Id;
1394 begin
1395 -- The processing of an inherited DIC assertion expression starts off
1396 -- with a copy of the original parent expression where all references
1397 -- to the parent type have already been replaced with references to
1398 -- the _object formal parameter of the parent type's DIC procedure.
1400 pragma Assert (Present (DIC_Expr));
1401 Expr := New_Copy_Tree (DIC_Expr);
1403 -- Perform the following substitutions:
1405 -- * Replace a reference to the _object parameter of the parent
1406 -- type's DIC procedure with a reference to the _object parameter
1407 -- of the derived types' DIC procedure.
1409 -- * Replace a reference to a discriminant of the parent type with
1410 -- a suitable value from the point of view of the derived type.
1412 -- * Replace a call to an overridden parent primitive with a call
1413 -- to the overriding derived type primitive.
1415 -- * Replace a call to an inherited parent primitive with a call to
1416 -- the internally-generated inherited derived type primitive.
1418 -- Note that primitives defined in the private part are automatically
1419 -- handled by the overriding/inheritance mechanism and do not require
1420 -- an extra replacement pass.
1422 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1424 Replace_References
1425 (Expr => Expr,
1426 Par_Typ => Par_Typ,
1427 Deriv_Typ => Deriv_Typ,
1428 Par_Obj => First_Formal (Par_Proc),
1429 Deriv_Obj => First_Formal (Deriv_Proc));
1431 -- Once the DIC assertion expression is fully processed, add a check
1432 -- to the statements of the DIC procedure.
1434 Add_DIC_Check
1435 (DIC_Prag => DIC_Prag,
1436 DIC_Expr => Expr,
1437 Stmts => Stmts);
1438 end Add_Inherited_Tagged_DIC;
1440 -----------------
1441 -- Add_Own_DIC --
1442 -----------------
1444 procedure Add_Own_DIC
1445 (DIC_Prag : Node_Id;
1446 DIC_Typ : Entity_Id;
1447 Stmts : in out List_Id)
1449 DIC_Args : constant List_Id :=
1450 Pragma_Argument_Associations (DIC_Prag);
1451 DIC_Arg : constant Node_Id := First (DIC_Args);
1452 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1453 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1454 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1455 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1457 procedure Preanalyze_Own_DIC_For_ASIS;
1458 -- Preanalyze the original DIC expression of an aspect or a source
1459 -- pragma for ASIS.
1461 ---------------------------------
1462 -- Preanalyze_Own_DIC_For_ASIS --
1463 ---------------------------------
1465 procedure Preanalyze_Own_DIC_For_ASIS is
1466 Expr : Node_Id := Empty;
1468 begin
1469 -- The DIC pragma is a source construct, preanalyze the original
1470 -- expression of the pragma.
1472 if Comes_From_Source (DIC_Prag) then
1473 Expr := DIC_Expr;
1475 -- Otherwise preanalyze the expression of the corresponding aspect
1477 elsif Present (DIC_Asp) then
1478 Expr := Expression (DIC_Asp);
1479 end if;
1481 -- The expression must be subjected to the same substitutions as
1482 -- the copy used in the generation of the runtime check.
1484 if Present (Expr) then
1485 Replace_Type_References
1486 (Expr => Expr,
1487 Typ => DIC_Typ,
1488 Obj_Id => Obj_Id);
1490 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1491 end if;
1492 end Preanalyze_Own_DIC_For_ASIS;
1494 -- Local variables
1496 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1498 Expr : Node_Id;
1500 -- Start of processing for Add_Own_DIC
1502 begin
1503 Expr := New_Copy_Tree (DIC_Expr);
1505 -- Perform the following substitution:
1507 -- * Replace the current instance of DIC_Typ with a reference to
1508 -- the _object formal parameter of the DIC procedure.
1510 Replace_Type_References
1511 (Expr => Expr,
1512 Typ => DIC_Typ,
1513 Obj_Id => Obj_Id);
1515 -- Preanalyze the DIC expression to detect errors and at the same
1516 -- time capture the visibility of the proper package part.
1518 Set_Parent (Expr, Typ_Decl);
1519 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1521 -- Save a copy of the expression with all replacements and analysis
1522 -- already taken place in case a derived type inherits the pragma.
1523 -- The copy will be used as the foundation of the derived type's own
1524 -- version of the DIC assertion expression.
1526 if Is_Tagged_Type (DIC_Typ) then
1527 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1528 end if;
1530 -- If the pragma comes from an aspect specification, replace the
1531 -- saved expression because all type references must be substituted
1532 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1533 -- routines.
1535 if Present (DIC_Asp) then
1536 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1537 end if;
1539 -- Preanalyze the original DIC expression for ASIS
1541 if ASIS_Mode then
1542 Preanalyze_Own_DIC_For_ASIS;
1543 end if;
1545 -- Once the DIC assertion expression is fully processed, add a check
1546 -- to the statements of the DIC procedure.
1548 Add_DIC_Check
1549 (DIC_Prag => DIC_Prag,
1550 DIC_Expr => Expr,
1551 Stmts => Stmts);
1552 end Add_Own_DIC;
1554 -- Local variables
1556 Loc : constant Source_Ptr := Sloc (Typ);
1558 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1559 -- Save the Ghost mode to restore on exit
1561 DIC_Prag : Node_Id;
1562 DIC_Typ : Entity_Id;
1563 Dummy_1 : Entity_Id;
1564 Dummy_2 : Entity_Id;
1565 Proc_Body : Node_Id;
1566 Proc_Body_Id : Entity_Id;
1567 Proc_Decl : Node_Id;
1568 Proc_Id : Entity_Id;
1569 Stmts : List_Id := No_List;
1571 Build_Body : Boolean := False;
1572 -- Flag set when the type requires a DIC procedure body to be built
1574 Work_Typ : Entity_Id;
1575 -- The working type
1577 -- Start of processing for Build_DIC_Procedure_Body
1579 begin
1580 Work_Typ := Base_Type (Typ);
1582 -- Do not process class-wide types as these are Itypes, but lack a first
1583 -- subtype (see below).
1585 if Is_Class_Wide_Type (Work_Typ) then
1586 return;
1588 -- Do not process the underlying full view of a private type. There is
1589 -- no way to get back to the partial view, plus the body will be built
1590 -- by the full view or the base type.
1592 elsif Is_Underlying_Full_View (Work_Typ) then
1593 return;
1595 -- Use the first subtype when dealing with various base types
1597 elsif Is_Itype (Work_Typ) then
1598 Work_Typ := First_Subtype (Work_Typ);
1600 -- The input denotes the corresponding record type of a protected or a
1601 -- task type. Work with the concurrent type because the corresponding
1602 -- record type may not be visible to clients of the type.
1604 elsif Ekind (Work_Typ) = E_Record_Type
1605 and then Is_Concurrent_Record_Type (Work_Typ)
1606 then
1607 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1608 end if;
1610 -- The working type may be subject to pragma Ghost. Set the mode now to
1611 -- ensure that the DIC procedure is properly marked as Ghost.
1613 Set_Ghost_Mode (Work_Typ);
1615 -- The working type must be either define a DIC pragma of its own or
1616 -- inherit one from a parent type.
1618 pragma Assert (Has_DIC (Work_Typ));
1620 -- Recover the type which defines the DIC pragma. This is either the
1621 -- working type itself or a parent type when the pragma is inherited.
1623 DIC_Typ := Find_DIC_Type (Work_Typ);
1624 pragma Assert (Present (DIC_Typ));
1626 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1627 pragma Assert (Present (DIC_Prag));
1629 -- Nothing to do if pragma DIC appears without an argument or its sole
1630 -- argument is "null".
1632 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1633 goto Leave;
1634 end if;
1636 -- The working type may lack a DIC procedure declaration. This may be
1637 -- due to several reasons:
1639 -- * The working type's own DIC pragma does not contain a verifiable
1640 -- assertion expression. In this case there is no need to build a
1641 -- DIC procedure because there is nothing to check.
1643 -- * The working type derives from a parent type. In this case a DIC
1644 -- procedure should be built only when the inherited DIC pragma has
1645 -- a verifiable assertion expression.
1647 Proc_Id := DIC_Procedure (Work_Typ);
1649 -- Build a DIC procedure declaration when the working type derives from
1650 -- a parent type.
1652 if No (Proc_Id) then
1653 Build_DIC_Procedure_Declaration (Work_Typ);
1654 Proc_Id := DIC_Procedure (Work_Typ);
1655 end if;
1657 -- At this point there should be a DIC procedure declaration
1659 pragma Assert (Present (Proc_Id));
1660 Proc_Decl := Unit_Declaration_Node (Proc_Id);
1662 -- Nothing to do if the DIC procedure already has a body
1664 if Present (Corresponding_Body (Proc_Decl)) then
1665 goto Leave;
1666 end if;
1668 -- Emulate the environment of the DIC procedure by installing its scope
1669 -- and formal parameters.
1671 Push_Scope (Proc_Id);
1672 Install_Formals (Proc_Id);
1674 -- The working type defines its own DIC pragma. Replace the current
1675 -- instance of the working type with the formal of the DIC procedure.
1676 -- Note that there is no need to consider inherited DIC pragmas from
1677 -- parent types because the working type's DIC pragma "hides" all
1678 -- inherited DIC pragmas.
1680 if Has_Own_DIC (Work_Typ) then
1681 pragma Assert (DIC_Typ = Work_Typ);
1683 Add_Own_DIC
1684 (DIC_Prag => DIC_Prag,
1685 DIC_Typ => DIC_Typ,
1686 Stmts => Stmts);
1688 Build_Body := True;
1690 -- Otherwise the working type inherits a DIC pragma from a parent type.
1691 -- This processing is carried out when the type is frozen because the
1692 -- state of all parent discriminants is known at that point. Note that
1693 -- it is semantically sound to delay the creation of the DIC procedure
1694 -- body till the freeze point. If the type has a DIC pragma of its own,
1695 -- then the DIC procedure body would have already been constructed at
1696 -- the end of the visible declarations and all parent DIC pragmas are
1697 -- effectively "hidden" and irrelevant.
1699 elsif For_Freeze then
1700 pragma Assert (Has_Inherited_DIC (Work_Typ));
1701 pragma Assert (DIC_Typ /= Work_Typ);
1703 -- The working type is tagged. The verification of the assertion
1704 -- expression is subject to the same semantics as class-wide pre-
1705 -- and postconditions.
1707 if Is_Tagged_Type (Work_Typ) then
1708 Add_Inherited_Tagged_DIC
1709 (DIC_Prag => DIC_Prag,
1710 Par_Typ => DIC_Typ,
1711 Deriv_Typ => Work_Typ,
1712 Stmts => Stmts);
1714 -- Otherwise the working type is not tagged. Verify the assertion
1715 -- expression of the inherited DIC pragma by directly calling the
1716 -- DIC procedure of the parent type.
1718 else
1719 Add_Inherited_DIC
1720 (DIC_Prag => DIC_Prag,
1721 Par_Typ => DIC_Typ,
1722 Deriv_Typ => Work_Typ,
1723 Stmts => Stmts);
1724 end if;
1726 Build_Body := True;
1727 end if;
1729 End_Scope;
1731 if Build_Body then
1733 -- Produce an empty completing body in the following cases:
1734 -- * Assertions are disabled
1735 -- * The DIC Assertion_Policy is Ignore
1736 -- * Pragma DIC appears without an argument
1737 -- * Pragma DIC appears with argument "null"
1739 if No (Stmts) then
1740 Stmts := New_List (Make_Null_Statement (Loc));
1741 end if;
1743 -- Generate:
1744 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1745 -- begin
1746 -- <Stmts>
1747 -- end <Work_Typ>DIC;
1749 Proc_Body :=
1750 Make_Subprogram_Body (Loc,
1751 Specification =>
1752 Copy_Subprogram_Spec (Parent (Proc_Id)),
1753 Declarations => Empty_List,
1754 Handled_Statement_Sequence =>
1755 Make_Handled_Sequence_Of_Statements (Loc,
1756 Statements => Stmts));
1757 Proc_Body_Id := Defining_Entity (Proc_Body);
1759 -- Perform minor decoration in case the body is not analyzed
1761 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1762 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1763 Set_Scope (Proc_Body_Id, Current_Scope);
1764 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
1765 Set_SPARK_Pragma_Inherited
1766 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
1768 -- Link both spec and body to avoid generating duplicates
1770 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1771 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1773 -- The body should not be inserted into the tree when the context
1774 -- is ASIS or a generic unit because it is not part of the template.
1775 -- Note that the body must still be generated in order to resolve the
1776 -- DIC assertion expression.
1778 if ASIS_Mode or Inside_A_Generic then
1779 null;
1781 -- Semi-insert the body into the tree for GNATprove by setting its
1782 -- Parent field. This allows for proper upstream tree traversals.
1784 elsif GNATprove_Mode then
1785 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1787 -- Otherwise the body is part of the freezing actions of the working
1788 -- type.
1790 else
1791 Append_Freeze_Action (Work_Typ, Proc_Body);
1792 end if;
1793 end if;
1795 <<Leave>>
1796 Restore_Ghost_Mode (Saved_GM);
1797 end Build_DIC_Procedure_Body;
1799 -------------------------------------
1800 -- Build_DIC_Procedure_Declaration --
1801 -------------------------------------
1803 -- WARNING: This routine manages Ghost regions. Return statements must be
1804 -- replaced by gotos which jump to the end of the routine and restore the
1805 -- Ghost mode.
1807 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1808 Loc : constant Source_Ptr := Sloc (Typ);
1810 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1811 -- Save the Ghost mode to restore on exit
1813 DIC_Prag : Node_Id;
1814 DIC_Typ : Entity_Id;
1815 Proc_Decl : Node_Id;
1816 Proc_Id : Entity_Id;
1817 Typ_Decl : Node_Id;
1819 CRec_Typ : Entity_Id;
1820 -- The corresponding record type of Full_Typ
1822 Full_Base : Entity_Id;
1823 -- The base type of Full_Typ
1825 Full_Typ : Entity_Id;
1826 -- The full view of working type
1828 Obj_Id : Entity_Id;
1829 -- The _object formal parameter of the DIC procedure
1831 Priv_Typ : Entity_Id;
1832 -- The partial view of working type
1834 Work_Typ : Entity_Id;
1835 -- The working type
1837 begin
1838 Work_Typ := Base_Type (Typ);
1840 -- Do not process class-wide types as these are Itypes, but lack a first
1841 -- subtype (see below).
1843 if Is_Class_Wide_Type (Work_Typ) then
1844 return;
1846 -- Do not process the underlying full view of a private type. There is
1847 -- no way to get back to the partial view, plus the body will be built
1848 -- by the full view or the base type.
1850 elsif Is_Underlying_Full_View (Work_Typ) then
1851 return;
1853 -- Use the first subtype when dealing with various base types
1855 elsif Is_Itype (Work_Typ) then
1856 Work_Typ := First_Subtype (Work_Typ);
1858 -- The input denotes the corresponding record type of a protected or a
1859 -- task type. Work with the concurrent type because the corresponding
1860 -- record type may not be visible to clients of the type.
1862 elsif Ekind (Work_Typ) = E_Record_Type
1863 and then Is_Concurrent_Record_Type (Work_Typ)
1864 then
1865 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1866 end if;
1868 -- The working type may be subject to pragma Ghost. Set the mode now to
1869 -- ensure that the DIC procedure is properly marked as Ghost.
1871 Set_Ghost_Mode (Work_Typ);
1873 -- The type must be either subject to a DIC pragma or inherit one from a
1874 -- parent type.
1876 pragma Assert (Has_DIC (Work_Typ));
1878 -- Recover the type which defines the DIC pragma. This is either the
1879 -- working type itself or a parent type when the pragma is inherited.
1881 DIC_Typ := Find_DIC_Type (Work_Typ);
1882 pragma Assert (Present (DIC_Typ));
1884 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1885 pragma Assert (Present (DIC_Prag));
1887 -- Nothing to do if pragma DIC appears without an argument or its sole
1888 -- argument is "null".
1890 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1891 goto Leave;
1893 -- Nothing to do if the type already has a DIC procedure
1895 elsif Present (DIC_Procedure (Work_Typ)) then
1896 goto Leave;
1897 end if;
1899 Proc_Id :=
1900 Make_Defining_Identifier (Loc,
1901 Chars =>
1902 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1904 -- Perform minor decoration in case the declaration is not analyzed
1906 Set_Ekind (Proc_Id, E_Procedure);
1907 Set_Etype (Proc_Id, Standard_Void_Type);
1908 Set_Is_DIC_Procedure (Proc_Id);
1909 Set_Scope (Proc_Id, Current_Scope);
1910 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
1911 Set_SPARK_Pragma_Inherited (Proc_Id);
1913 Set_DIC_Procedure (Work_Typ, Proc_Id);
1915 -- The DIC procedure requires debug info when the assertion expression
1916 -- is subject to Source Coverage Obligations.
1918 if Generate_SCO then
1919 Set_Needs_Debug_Info (Proc_Id);
1920 end if;
1922 -- Obtain all views of the input type
1924 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1926 -- Associate the DIC procedure and various relevant flags with all views
1928 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1929 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1930 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1931 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1933 -- The declaration of the DIC procedure must be inserted after the
1934 -- declaration of the partial view as this allows for proper external
1935 -- visibility.
1937 if Present (Priv_Typ) then
1938 Typ_Decl := Declaration_Node (Priv_Typ);
1940 -- Derived types with the full view as parent do not have a partial
1941 -- view. Insert the DIC procedure after the derived type.
1943 else
1944 Typ_Decl := Declaration_Node (Full_Typ);
1945 end if;
1947 -- The type should have a declarative node
1949 pragma Assert (Present (Typ_Decl));
1951 -- Create the formal parameter which emulates the variable-like behavior
1952 -- of the type's current instance.
1954 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1956 -- Perform minor decoration in case the declaration is not analyzed
1958 Set_Ekind (Obj_Id, E_In_Parameter);
1959 Set_Etype (Obj_Id, Work_Typ);
1960 Set_Scope (Obj_Id, Proc_Id);
1962 Set_First_Entity (Proc_Id, Obj_Id);
1964 -- Generate:
1965 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1967 Proc_Decl :=
1968 Make_Subprogram_Declaration (Loc,
1969 Specification =>
1970 Make_Procedure_Specification (Loc,
1971 Defining_Unit_Name => Proc_Id,
1972 Parameter_Specifications => New_List (
1973 Make_Parameter_Specification (Loc,
1974 Defining_Identifier => Obj_Id,
1975 Parameter_Type =>
1976 New_Occurrence_Of (Work_Typ, Loc)))));
1978 -- The declaration should not be inserted into the tree when the context
1979 -- is ASIS or a generic unit because it is not part of the template.
1981 if ASIS_Mode or Inside_A_Generic then
1982 null;
1984 -- Semi-insert the declaration into the tree for GNATprove by setting
1985 -- its Parent field. This allows for proper upstream tree traversals.
1987 elsif GNATprove_Mode then
1988 Set_Parent (Proc_Decl, Parent (Typ_Decl));
1990 -- Otherwise insert the declaration
1992 else
1993 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
1994 end if;
1996 <<Leave>>
1997 Restore_Ghost_Mode (Saved_GM);
1998 end Build_DIC_Procedure_Declaration;
2000 ------------------------------------
2001 -- Build_Invariant_Procedure_Body --
2002 ------------------------------------
2004 -- WARNING: This routine manages Ghost regions. Return statements must be
2005 -- replaced by gotos which jump to the end of the routine and restore the
2006 -- Ghost mode.
2008 procedure Build_Invariant_Procedure_Body
2009 (Typ : Entity_Id;
2010 Partial_Invariant : Boolean := False)
2012 Loc : constant Source_Ptr := Sloc (Typ);
2014 Pragmas_Seen : Elist_Id := No_Elist;
2015 -- This list contains all invariant pragmas processed so far. The list
2016 -- is used to avoid generating redundant invariant checks.
2018 Produced_Check : Boolean := False;
2019 -- This flag tracks whether the type has produced at least one invariant
2020 -- check. The flag is used as a sanity check at the end of the routine.
2022 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2023 -- intentionally unnested to avoid deep indentation of code.
2025 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2026 -- they emit checks, loops (for arrays) and case statements (for record
2027 -- variant parts) only when there are invariants to verify. This keeps
2028 -- the body of the invariant procedure free of useless code.
2030 procedure Add_Array_Component_Invariants
2031 (T : Entity_Id;
2032 Obj_Id : Entity_Id;
2033 Checks : in out List_Id);
2034 -- Generate an invariant check for each component of array type T.
2035 -- Obj_Id denotes the entity of the _object formal parameter of the
2036 -- invariant procedure. All created checks are added to list Checks.
2038 procedure Add_Inherited_Invariants
2039 (T : Entity_Id;
2040 Priv_Typ : Entity_Id;
2041 Full_Typ : Entity_Id;
2042 Obj_Id : Entity_Id;
2043 Checks : in out List_Id);
2044 -- Generate an invariant check for each inherited class-wide invariant
2045 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2046 -- the partial and full view of the parent type. Obj_Id denotes the
2047 -- entity of the _object formal parameter of the invariant procedure.
2048 -- All created checks are added to list Checks.
2050 procedure Add_Interface_Invariants
2051 (T : Entity_Id;
2052 Obj_Id : Entity_Id;
2053 Checks : in out List_Id);
2054 -- Generate an invariant check for each inherited class-wide invariant
2055 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2056 -- entity of the _object formal parameter of the invariant procedure.
2057 -- All created checks are added to list Checks.
2059 procedure Add_Invariant_Check
2060 (Prag : Node_Id;
2061 Expr : Node_Id;
2062 Checks : in out List_Id;
2063 Inherited : Boolean := False);
2064 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2065 -- verify assertion expression Expr of pragma Prag. All generated code
2066 -- is added to list Checks. Flag Inherited should be set when the pragma
2067 -- is inherited from a parent or interface type.
2069 procedure Add_Own_Invariants
2070 (T : Entity_Id;
2071 Obj_Id : Entity_Id;
2072 Checks : in out List_Id;
2073 Priv_Item : Node_Id := Empty);
2074 -- Generate an invariant check for each invariant found for type T.
2075 -- Obj_Id denotes the entity of the _object formal parameter of the
2076 -- invariant procedure. All created checks are added to list Checks.
2077 -- Priv_Item denotes the first rep item of the private type.
2079 procedure Add_Parent_Invariants
2080 (T : Entity_Id;
2081 Obj_Id : Entity_Id;
2082 Checks : in out List_Id);
2083 -- Generate an invariant check for each inherited class-wide invariant
2084 -- coming from all parent types of type T. Obj_Id denotes the entity of
2085 -- the _object formal parameter of the invariant procedure. All created
2086 -- checks are added to list Checks.
2088 procedure Add_Record_Component_Invariants
2089 (T : Entity_Id;
2090 Obj_Id : Entity_Id;
2091 Checks : in out List_Id);
2092 -- Generate an invariant check for each component of record type T.
2093 -- Obj_Id denotes the entity of the _object formal parameter of the
2094 -- invariant procedure. All created checks are added to list Checks.
2096 ------------------------------------
2097 -- Add_Array_Component_Invariants --
2098 ------------------------------------
2100 procedure Add_Array_Component_Invariants
2101 (T : Entity_Id;
2102 Obj_Id : Entity_Id;
2103 Checks : in out List_Id)
2105 Comp_Typ : constant Entity_Id := Component_Type (T);
2106 Dims : constant Pos := Number_Dimensions (T);
2108 procedure Process_Array_Component
2109 (Indices : List_Id;
2110 Comp_Checks : in out List_Id);
2111 -- Generate an invariant check for an array component identified by
2112 -- the indices in list Indices. All created checks are added to list
2113 -- Comp_Checks.
2115 procedure Process_One_Dimension
2116 (Dim : Pos;
2117 Indices : List_Id;
2118 Dim_Checks : in out List_Id);
2119 -- Generate a loop over the Nth dimension Dim of an array type. List
2120 -- Indices contains all array indices for the dimension. All created
2121 -- checks are added to list Dim_Checks.
2123 -----------------------------
2124 -- Process_Array_Component --
2125 -----------------------------
2127 procedure Process_Array_Component
2128 (Indices : List_Id;
2129 Comp_Checks : in out List_Id)
2131 Proc_Id : Entity_Id;
2133 begin
2134 if Has_Invariants (Comp_Typ) then
2136 -- In GNATprove mode, the component invariants are checked by
2137 -- other means. They should not be added to the array type
2138 -- invariant procedure, so that the procedure can be used to
2139 -- check the array type invariants if any.
2141 if GNATprove_Mode then
2142 null;
2144 else
2145 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2147 -- The component type should have an invariant procedure
2148 -- if it has invariants of its own or inherits class-wide
2149 -- invariants from parent or interface types.
2151 pragma Assert (Present (Proc_Id));
2153 -- Generate:
2154 -- <Comp_Typ>Invariant (_object (<Indices>));
2156 -- Note that the invariant procedure may have a null body if
2157 -- assertions are disabled or Assertion_Policy Ignore is in
2158 -- effect.
2160 if not Has_Null_Body (Proc_Id) then
2161 Append_New_To (Comp_Checks,
2162 Make_Procedure_Call_Statement (Loc,
2163 Name =>
2164 New_Occurrence_Of (Proc_Id, Loc),
2165 Parameter_Associations => New_List (
2166 Make_Indexed_Component (Loc,
2167 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2168 Expressions => New_Copy_List (Indices)))));
2169 end if;
2170 end if;
2172 Produced_Check := True;
2173 end if;
2174 end Process_Array_Component;
2176 ---------------------------
2177 -- Process_One_Dimension --
2178 ---------------------------
2180 procedure Process_One_Dimension
2181 (Dim : Pos;
2182 Indices : List_Id;
2183 Dim_Checks : in out List_Id)
2185 Comp_Checks : List_Id := No_List;
2186 Index : Entity_Id;
2188 begin
2189 -- Generate the invariant checks for the array component after all
2190 -- dimensions have produced their respective loops.
2192 if Dim > Dims then
2193 Process_Array_Component
2194 (Indices => Indices,
2195 Comp_Checks => Dim_Checks);
2197 -- Otherwise create a loop for the current dimension
2199 else
2200 -- Create a new loop variable for each dimension
2202 Index :=
2203 Make_Defining_Identifier (Loc,
2204 Chars => New_External_Name ('I', Dim));
2205 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2207 Process_One_Dimension
2208 (Dim => Dim + 1,
2209 Indices => Indices,
2210 Dim_Checks => Comp_Checks);
2212 -- Generate:
2213 -- for I<Dim> in _object'Range (<Dim>) loop
2214 -- <Comp_Checks>
2215 -- end loop;
2217 -- Note that the invariant procedure may have a null body if
2218 -- assertions are disabled or Assertion_Policy Ignore is in
2219 -- effect.
2221 if Present (Comp_Checks) then
2222 Append_New_To (Dim_Checks,
2223 Make_Implicit_Loop_Statement (T,
2224 Identifier => Empty,
2225 Iteration_Scheme =>
2226 Make_Iteration_Scheme (Loc,
2227 Loop_Parameter_Specification =>
2228 Make_Loop_Parameter_Specification (Loc,
2229 Defining_Identifier => Index,
2230 Discrete_Subtype_Definition =>
2231 Make_Attribute_Reference (Loc,
2232 Prefix =>
2233 New_Occurrence_Of (Obj_Id, Loc),
2234 Attribute_Name => Name_Range,
2235 Expressions => New_List (
2236 Make_Integer_Literal (Loc, Dim))))),
2237 Statements => Comp_Checks));
2238 end if;
2239 end if;
2240 end Process_One_Dimension;
2242 -- Start of processing for Add_Array_Component_Invariants
2244 begin
2245 Process_One_Dimension
2246 (Dim => 1,
2247 Indices => New_List,
2248 Dim_Checks => Checks);
2249 end Add_Array_Component_Invariants;
2251 ------------------------------
2252 -- Add_Inherited_Invariants --
2253 ------------------------------
2255 procedure Add_Inherited_Invariants
2256 (T : Entity_Id;
2257 Priv_Typ : Entity_Id;
2258 Full_Typ : Entity_Id;
2259 Obj_Id : Entity_Id;
2260 Checks : in out List_Id)
2262 Deriv_Typ : Entity_Id;
2263 Expr : Node_Id;
2264 Prag : Node_Id;
2265 Prag_Expr : Node_Id;
2266 Prag_Expr_Arg : Node_Id;
2267 Prag_Typ : Node_Id;
2268 Prag_Typ_Arg : Node_Id;
2270 Par_Proc : Entity_Id;
2271 -- The "partial" invariant procedure of Par_Typ
2273 Par_Typ : Entity_Id;
2274 -- The suitable view of the parent type used in the substitution of
2275 -- type attributes.
2277 begin
2278 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2279 return;
2280 end if;
2282 -- When the type inheriting the class-wide invariant is a concurrent
2283 -- type, use the corresponding record type because it contains all
2284 -- primitive operations of the concurrent type and allows for proper
2285 -- substitution.
2287 if Is_Concurrent_Type (T) then
2288 Deriv_Typ := Corresponding_Record_Type (T);
2289 else
2290 Deriv_Typ := T;
2291 end if;
2293 pragma Assert (Present (Deriv_Typ));
2295 -- Determine which rep item chain to use. Precedence is given to that
2296 -- of the parent type's partial view since it usually carries all the
2297 -- class-wide invariants.
2299 if Present (Priv_Typ) then
2300 Prag := First_Rep_Item (Priv_Typ);
2301 else
2302 Prag := First_Rep_Item (Full_Typ);
2303 end if;
2305 while Present (Prag) loop
2306 if Nkind (Prag) = N_Pragma
2307 and then Pragma_Name (Prag) = Name_Invariant
2308 then
2309 -- Nothing to do if the pragma was already processed
2311 if Contains (Pragmas_Seen, Prag) then
2312 return;
2314 -- Nothing to do when the caller requests the processing of all
2315 -- inherited class-wide invariants, but the pragma does not
2316 -- fall in this category.
2318 elsif not Class_Present (Prag) then
2319 return;
2320 end if;
2322 -- Extract the arguments of the invariant pragma
2324 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2325 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2326 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2327 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2329 -- The pragma applies to the partial view of the parent type
2331 if Present (Priv_Typ)
2332 and then Entity (Prag_Typ) = Priv_Typ
2333 then
2334 Par_Typ := Priv_Typ;
2336 -- The pragma applies to the full view of the parent type
2338 elsif Present (Full_Typ)
2339 and then Entity (Prag_Typ) = Full_Typ
2340 then
2341 Par_Typ := Full_Typ;
2343 -- Otherwise the pragma does not belong to the parent type and
2344 -- should not be considered.
2346 else
2347 return;
2348 end if;
2350 -- Perform the following substitutions:
2352 -- * Replace a reference to the _object parameter of the
2353 -- parent type's partial invariant procedure with a
2354 -- reference to the _object parameter of the derived
2355 -- type's full invariant procedure.
2357 -- * Replace a reference to a discriminant of the parent type
2358 -- with a suitable value from the point of view of the
2359 -- derived type.
2361 -- * Replace a call to an overridden parent primitive with a
2362 -- call to the overriding derived type primitive.
2364 -- * Replace a call to an inherited parent primitive with a
2365 -- call to the internally-generated inherited derived type
2366 -- primitive.
2368 Expr := New_Copy_Tree (Prag_Expr);
2370 -- The parent type must have a "partial" invariant procedure
2371 -- because class-wide invariants are captured exclusively by
2372 -- it.
2374 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2375 pragma Assert (Present (Par_Proc));
2377 Replace_References
2378 (Expr => Expr,
2379 Par_Typ => Par_Typ,
2380 Deriv_Typ => Deriv_Typ,
2381 Par_Obj => First_Formal (Par_Proc),
2382 Deriv_Obj => Obj_Id);
2384 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2385 end if;
2387 Next_Rep_Item (Prag);
2388 end loop;
2389 end Add_Inherited_Invariants;
2391 ------------------------------
2392 -- Add_Interface_Invariants --
2393 ------------------------------
2395 procedure Add_Interface_Invariants
2396 (T : Entity_Id;
2397 Obj_Id : Entity_Id;
2398 Checks : in out List_Id)
2400 Iface_Elmt : Elmt_Id;
2401 Ifaces : Elist_Id;
2403 begin
2404 -- Generate an invariant check for each class-wide invariant coming
2405 -- from all interfaces implemented by type T.
2407 if Is_Tagged_Type (T) then
2408 Collect_Interfaces (T, Ifaces);
2410 -- Process the class-wide invariants of all implemented interfaces
2412 Iface_Elmt := First_Elmt (Ifaces);
2413 while Present (Iface_Elmt) loop
2415 -- The Full_Typ parameter is intentionally left Empty because
2416 -- interfaces are treated as the partial view of a private type
2417 -- in order to achieve uniformity with the general case.
2419 Add_Inherited_Invariants
2420 (T => T,
2421 Priv_Typ => Node (Iface_Elmt),
2422 Full_Typ => Empty,
2423 Obj_Id => Obj_Id,
2424 Checks => Checks);
2426 Next_Elmt (Iface_Elmt);
2427 end loop;
2428 end if;
2429 end Add_Interface_Invariants;
2431 -------------------------
2432 -- Add_Invariant_Check --
2433 -------------------------
2435 procedure Add_Invariant_Check
2436 (Prag : Node_Id;
2437 Expr : Node_Id;
2438 Checks : in out List_Id;
2439 Inherited : Boolean := False)
2441 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2442 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2443 Ploc : constant Source_Ptr := Sloc (Prag);
2444 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2446 Assoc : List_Id;
2447 Str : String_Id;
2449 begin
2450 -- The invariant is ignored, nothing left to do
2452 if Is_Ignored (Prag) then
2453 null;
2455 -- Otherwise the invariant is checked. Build a pragma Check to verify
2456 -- the expression at run time.
2458 else
2459 Assoc := New_List (
2460 Make_Pragma_Argument_Association (Ploc,
2461 Expression => Make_Identifier (Ploc, Nam)),
2462 Make_Pragma_Argument_Association (Ploc,
2463 Expression => Expr));
2465 -- Handle the String argument (if any)
2467 if Present (Str_Arg) then
2468 Str := Strval (Get_Pragma_Arg (Str_Arg));
2470 -- When inheriting an invariant, modify the message from
2471 -- "failed invariant" to "failed inherited invariant".
2473 if Inherited then
2474 String_To_Name_Buffer (Str);
2476 if Name_Buffer (1 .. 16) = "failed invariant" then
2477 Insert_Str_In_Name_Buffer ("inherited ", 8);
2478 Str := String_From_Name_Buffer;
2479 end if;
2480 end if;
2482 Append_To (Assoc,
2483 Make_Pragma_Argument_Association (Ploc,
2484 Expression => Make_String_Literal (Ploc, Str)));
2485 end if;
2487 -- Generate:
2488 -- pragma Check (<Nam>, <Expr>, <Str>);
2490 Append_New_To (Checks,
2491 Make_Pragma (Ploc,
2492 Chars => Name_Check,
2493 Pragma_Argument_Associations => Assoc));
2494 end if;
2496 -- Output an info message when inheriting an invariant and the
2497 -- listing option is enabled.
2499 if Inherited and Opt.List_Inherited_Aspects then
2500 Error_Msg_Sloc := Sloc (Prag);
2501 Error_Msg_N
2502 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2503 end if;
2505 -- Add the pragma to the list of processed pragmas
2507 Append_New_Elmt (Prag, Pragmas_Seen);
2508 Produced_Check := True;
2509 end Add_Invariant_Check;
2511 ---------------------------
2512 -- Add_Parent_Invariants --
2513 ---------------------------
2515 procedure Add_Parent_Invariants
2516 (T : Entity_Id;
2517 Obj_Id : Entity_Id;
2518 Checks : in out List_Id)
2520 Dummy_1 : Entity_Id;
2521 Dummy_2 : Entity_Id;
2523 Curr_Typ : Entity_Id;
2524 -- The entity of the current type being examined
2526 Full_Typ : Entity_Id;
2527 -- The full view of Par_Typ
2529 Par_Typ : Entity_Id;
2530 -- The entity of the parent type
2532 Priv_Typ : Entity_Id;
2533 -- The partial view of Par_Typ
2535 begin
2536 -- Do not process array types because they cannot have true parent
2537 -- types. This also prevents the generation of a duplicate invariant
2538 -- check when the input type is an array base type because its Etype
2539 -- denotes the first subtype, both of which share the same component
2540 -- type.
2542 if Is_Array_Type (T) then
2543 return;
2544 end if;
2546 -- Climb the parent type chain
2548 Curr_Typ := T;
2549 loop
2550 -- Do not consider subtypes as they inherit the invariants
2551 -- from their base types.
2553 Par_Typ := Base_Type (Etype (Curr_Typ));
2555 -- Stop the climb once the root of the parent chain is
2556 -- reached.
2558 exit when Curr_Typ = Par_Typ;
2560 -- Process the class-wide invariants of the parent type
2562 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2564 -- Process the elements of an array type
2566 if Is_Array_Type (Full_Typ) then
2567 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2569 -- Process the components of a record type
2571 elsif Ekind (Full_Typ) = E_Record_Type then
2572 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2573 end if;
2575 Add_Inherited_Invariants
2576 (T => T,
2577 Priv_Typ => Priv_Typ,
2578 Full_Typ => Full_Typ,
2579 Obj_Id => Obj_Id,
2580 Checks => Checks);
2582 Curr_Typ := Par_Typ;
2583 end loop;
2584 end Add_Parent_Invariants;
2586 ------------------------
2587 -- Add_Own_Invariants --
2588 ------------------------
2590 procedure Add_Own_Invariants
2591 (T : Entity_Id;
2592 Obj_Id : Entity_Id;
2593 Checks : in out List_Id;
2594 Priv_Item : Node_Id := Empty)
2596 ASIS_Expr : Node_Id;
2597 Expr : Node_Id;
2598 Prag : Node_Id;
2599 Prag_Asp : Node_Id;
2600 Prag_Expr : Node_Id;
2601 Prag_Expr_Arg : Node_Id;
2602 Prag_Typ : Node_Id;
2603 Prag_Typ_Arg : Node_Id;
2605 begin
2606 if not Present (T) then
2607 return;
2608 end if;
2610 Prag := First_Rep_Item (T);
2611 while Present (Prag) loop
2612 if Nkind (Prag) = N_Pragma
2613 and then Pragma_Name (Prag) = Name_Invariant
2614 then
2615 -- Stop the traversal of the rep item chain once a specific
2616 -- item is encountered.
2618 if Present (Priv_Item) and then Prag = Priv_Item then
2619 exit;
2620 end if;
2622 -- Nothing to do if the pragma was already processed
2624 if Contains (Pragmas_Seen, Prag) then
2625 return;
2626 end if;
2628 -- Extract the arguments of the invariant pragma
2630 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2631 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2632 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2633 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2634 Prag_Asp := Corresponding_Aspect (Prag);
2636 -- Verify the pragma belongs to T, otherwise the pragma applies
2637 -- to a parent type in which case it will be processed later by
2638 -- Add_Parent_Invariants or Add_Interface_Invariants.
2640 if Entity (Prag_Typ) /= T then
2641 return;
2642 end if;
2644 Expr := New_Copy_Tree (Prag_Expr);
2646 -- Substitute all references to type T with references to the
2647 -- _object formal parameter.
2649 Replace_Type_References (Expr, T, Obj_Id);
2651 -- Preanalyze the invariant expression to detect errors and at
2652 -- the same time capture the visibility of the proper package
2653 -- part.
2655 Set_Parent (Expr, Parent (Prag_Expr));
2656 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2658 -- Save a copy of the expression when T is tagged to detect
2659 -- errors and capture the visibility of the proper package part
2660 -- for the generation of inherited type invariants.
2662 if Is_Tagged_Type (T) then
2663 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2664 end if;
2666 -- If the pragma comes from an aspect specification, replace
2667 -- the saved expression because all type references must be
2668 -- substituted for the call to Preanalyze_Spec_Expression in
2669 -- Check_Aspect_At_xxx routines.
2671 if Present (Prag_Asp) then
2672 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2673 end if;
2675 -- Analyze the original invariant expression for ASIS
2677 if ASIS_Mode then
2678 ASIS_Expr := Empty;
2680 if Comes_From_Source (Prag) then
2681 ASIS_Expr := Prag_Expr;
2682 elsif Present (Prag_Asp) then
2683 ASIS_Expr := Expression (Prag_Asp);
2684 end if;
2686 if Present (ASIS_Expr) then
2687 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2688 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2689 end if;
2690 end if;
2692 Add_Invariant_Check (Prag, Expr, Checks);
2693 end if;
2695 Next_Rep_Item (Prag);
2696 end loop;
2697 end Add_Own_Invariants;
2699 -------------------------------------
2700 -- Add_Record_Component_Invariants --
2701 -------------------------------------
2703 procedure Add_Record_Component_Invariants
2704 (T : Entity_Id;
2705 Obj_Id : Entity_Id;
2706 Checks : in out List_Id)
2708 procedure Process_Component_List
2709 (Comp_List : Node_Id;
2710 CL_Checks : in out List_Id);
2711 -- Generate invariant checks for all record components found in
2712 -- component list Comp_List, including variant parts. All created
2713 -- checks are added to list CL_Checks.
2715 procedure Process_Record_Component
2716 (Comp_Id : Entity_Id;
2717 Comp_Checks : in out List_Id);
2718 -- Generate an invariant check for a record component identified by
2719 -- Comp_Id. All created checks are added to list Comp_Checks.
2721 ----------------------------
2722 -- Process_Component_List --
2723 ----------------------------
2725 procedure Process_Component_List
2726 (Comp_List : Node_Id;
2727 CL_Checks : in out List_Id)
2729 Comp : Node_Id;
2730 Var : Node_Id;
2731 Var_Alts : List_Id := No_List;
2732 Var_Checks : List_Id := No_List;
2733 Var_Stmts : List_Id;
2735 Produced_Variant_Check : Boolean := False;
2736 -- This flag tracks whether the component has produced at least
2737 -- one invariant check.
2739 begin
2740 -- Traverse the component items
2742 Comp := First (Component_Items (Comp_List));
2743 while Present (Comp) loop
2744 if Nkind (Comp) = N_Component_Declaration then
2746 -- Generate the component invariant check
2748 Process_Record_Component
2749 (Comp_Id => Defining_Entity (Comp),
2750 Comp_Checks => CL_Checks);
2751 end if;
2753 Next (Comp);
2754 end loop;
2756 -- Traverse the variant part
2758 if Present (Variant_Part (Comp_List)) then
2759 Var := First (Variants (Variant_Part (Comp_List)));
2760 while Present (Var) loop
2761 Var_Checks := No_List;
2763 -- Generate invariant checks for all components and variant
2764 -- parts that qualify.
2766 Process_Component_List
2767 (Comp_List => Component_List (Var),
2768 CL_Checks => Var_Checks);
2770 -- The components of the current variant produced at least
2771 -- one invariant check.
2773 if Present (Var_Checks) then
2774 Var_Stmts := Var_Checks;
2775 Produced_Variant_Check := True;
2777 -- Otherwise there are either no components with invariants,
2778 -- assertions are disabled, or Assertion_Policy Ignore is in
2779 -- effect.
2781 else
2782 Var_Stmts := New_List (Make_Null_Statement (Loc));
2783 end if;
2785 Append_New_To (Var_Alts,
2786 Make_Case_Statement_Alternative (Loc,
2787 Discrete_Choices =>
2788 New_Copy_List (Discrete_Choices (Var)),
2789 Statements => Var_Stmts));
2791 Next (Var);
2792 end loop;
2794 -- Create a case statement which verifies the invariant checks
2795 -- of a particular component list depending on the discriminant
2796 -- values only when there is at least one real invariant check.
2798 if Produced_Variant_Check then
2799 Append_New_To (CL_Checks,
2800 Make_Case_Statement (Loc,
2801 Expression =>
2802 Make_Selected_Component (Loc,
2803 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2804 Selector_Name =>
2805 New_Occurrence_Of
2806 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2807 Alternatives => Var_Alts));
2808 end if;
2809 end if;
2810 end Process_Component_List;
2812 ------------------------------
2813 -- Process_Record_Component --
2814 ------------------------------
2816 procedure Process_Record_Component
2817 (Comp_Id : Entity_Id;
2818 Comp_Checks : in out List_Id)
2820 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2821 Proc_Id : Entity_Id;
2823 Produced_Component_Check : Boolean := False;
2824 -- This flag tracks whether the component has produced at least
2825 -- one invariant check.
2827 begin
2828 -- Nothing to do for internal component _parent. Note that it is
2829 -- not desirable to check whether the component comes from source
2830 -- because protected type components are relocated to an internal
2831 -- corresponding record, but still need processing.
2833 if Chars (Comp_Id) = Name_uParent then
2834 return;
2835 end if;
2837 -- Verify the invariant of the component. Note that an access
2838 -- type may have an invariant when it acts as the full view of a
2839 -- private type and the invariant appears on the partial view. In
2840 -- this case verify the access value itself.
2842 if Has_Invariants (Comp_Typ) then
2844 -- In GNATprove mode, the component invariants are checked by
2845 -- other means. They should not be added to the record type
2846 -- invariant procedure, so that the procedure can be used to
2847 -- check the record type invariants if any.
2849 if GNATprove_Mode then
2850 null;
2852 else
2853 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2855 -- The component type should have an invariant procedure
2856 -- if it has invariants of its own or inherits class-wide
2857 -- invariants from parent or interface types.
2859 pragma Assert (Present (Proc_Id));
2861 -- Generate:
2862 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2864 -- Note that the invariant procedure may have a null body if
2865 -- assertions are disabled or Assertion_Policy Ignore is in
2866 -- effect.
2868 if not Has_Null_Body (Proc_Id) then
2869 Append_New_To (Comp_Checks,
2870 Make_Procedure_Call_Statement (Loc,
2871 Name =>
2872 New_Occurrence_Of (Proc_Id, Loc),
2873 Parameter_Associations => New_List (
2874 Make_Selected_Component (Loc,
2875 Prefix =>
2876 Unchecked_Convert_To
2877 (T, New_Occurrence_Of (Obj_Id, Loc)),
2878 Selector_Name =>
2879 New_Occurrence_Of (Comp_Id, Loc)))));
2880 end if;
2881 end if;
2883 Produced_Check := True;
2884 Produced_Component_Check := True;
2885 end if;
2887 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2888 Error_Msg_NE
2889 ("invariants cannot be checked on components of "
2890 & "unchecked_union type &?", Comp_Id, T);
2891 end if;
2892 end Process_Record_Component;
2894 -- Local variables
2896 Comps : Node_Id;
2897 Def : Node_Id;
2899 -- Start of processing for Add_Record_Component_Invariants
2901 begin
2902 -- An untagged derived type inherits the components of its parent
2903 -- type. In order to avoid creating redundant invariant checks, do
2904 -- not process the components now. Instead wait until the ultimate
2905 -- parent of the untagged derivation chain is reached.
2907 if not Is_Untagged_Derivation (T) then
2908 Def := Type_Definition (Parent (T));
2910 if Nkind (Def) = N_Derived_Type_Definition then
2911 Def := Record_Extension_Part (Def);
2912 end if;
2914 pragma Assert (Nkind (Def) = N_Record_Definition);
2915 Comps := Component_List (Def);
2917 if Present (Comps) then
2918 Process_Component_List
2919 (Comp_List => Comps,
2920 CL_Checks => Checks);
2921 end if;
2922 end if;
2923 end Add_Record_Component_Invariants;
2925 -- Local variables
2927 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2928 -- Save the Ghost mode to restore on exit
2930 Dummy : Entity_Id;
2931 Priv_Item : Node_Id;
2932 Proc_Body : Node_Id;
2933 Proc_Body_Id : Entity_Id;
2934 Proc_Decl : Node_Id;
2935 Proc_Id : Entity_Id;
2936 Stmts : List_Id := No_List;
2938 CRec_Typ : Entity_Id := Empty;
2939 -- The corresponding record type of Full_Typ
2941 Full_Proc : Entity_Id := Empty;
2942 -- The entity of the "full" invariant procedure
2944 Full_Typ : Entity_Id := Empty;
2945 -- The full view of the working type
2947 Obj_Id : Entity_Id := Empty;
2948 -- The _object formal parameter of the invariant procedure
2950 Part_Proc : Entity_Id := Empty;
2951 -- The entity of the "partial" invariant procedure
2953 Priv_Typ : Entity_Id := Empty;
2954 -- The partial view of the working type
2956 Work_Typ : Entity_Id := Empty;
2957 -- The working type
2959 -- Start of processing for Build_Invariant_Procedure_Body
2961 begin
2962 Work_Typ := Typ;
2964 -- The input type denotes the implementation base type of a constrained
2965 -- array type. Work with the first subtype as all invariant pragmas are
2966 -- on its rep item chain.
2968 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2969 Work_Typ := First_Subtype (Work_Typ);
2971 -- The input type denotes the corresponding record type of a protected
2972 -- or task type. Work with the concurrent type because the corresponding
2973 -- record type may not be visible to clients of the type.
2975 elsif Ekind (Work_Typ) = E_Record_Type
2976 and then Is_Concurrent_Record_Type (Work_Typ)
2977 then
2978 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2979 end if;
2981 -- The working type may be subject to pragma Ghost. Set the mode now to
2982 -- ensure that the invariant procedure is properly marked as Ghost.
2984 Set_Ghost_Mode (Work_Typ);
2986 -- The type must either have invariants of its own, inherit class-wide
2987 -- invariants from parent types or interfaces, or be an array or record
2988 -- type whose components have invariants.
2990 pragma Assert (Has_Invariants (Work_Typ));
2992 -- Interfaces are treated as the partial view of a private type in order
2993 -- to achieve uniformity with the general case.
2995 if Is_Interface (Work_Typ) then
2996 Priv_Typ := Work_Typ;
2998 -- Otherwise obtain both views of the type
3000 else
3001 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3002 end if;
3004 -- The caller requests a body for the partial invariant procedure
3006 if Partial_Invariant then
3007 Full_Proc := Invariant_Procedure (Work_Typ);
3008 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3010 -- The "full" invariant procedure body was already created
3012 if Present (Full_Proc)
3013 and then Present
3014 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3015 then
3016 -- This scenario happens only when the type is an untagged
3017 -- derivation from a private parent and the underlying full
3018 -- view was processed before the partial view.
3020 pragma Assert
3021 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3023 -- Nothing to do because the processing of the underlying full
3024 -- view already checked the invariants of the partial view.
3026 goto Leave;
3027 end if;
3029 -- Create a declaration for the "partial" invariant procedure if it
3030 -- is not available.
3032 if No (Proc_Id) then
3033 Build_Invariant_Procedure_Declaration
3034 (Typ => Work_Typ,
3035 Partial_Invariant => True);
3037 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3038 end if;
3040 -- The caller requests a body for the "full" invariant procedure
3042 else
3043 Proc_Id := Invariant_Procedure (Work_Typ);
3044 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3046 -- Create a declaration for the "full" invariant procedure if it is
3047 -- not available.
3049 if No (Proc_Id) then
3050 Build_Invariant_Procedure_Declaration (Work_Typ);
3051 Proc_Id := Invariant_Procedure (Work_Typ);
3052 end if;
3053 end if;
3055 -- At this point there should be an invariant procedure declaration
3057 pragma Assert (Present (Proc_Id));
3058 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3060 -- Nothing to do if the invariant procedure already has a body
3062 if Present (Corresponding_Body (Proc_Decl)) then
3063 goto Leave;
3064 end if;
3066 -- Emulate the environment of the invariant procedure by installing its
3067 -- scope and formal parameters. Note that this is not needed, but having
3068 -- the scope installed helps with the detection of invariant-related
3069 -- errors.
3071 Push_Scope (Proc_Id);
3072 Install_Formals (Proc_Id);
3074 Obj_Id := First_Formal (Proc_Id);
3075 pragma Assert (Present (Obj_Id));
3077 -- The "partial" invariant procedure verifies the invariants of the
3078 -- partial view only.
3080 if Partial_Invariant then
3081 pragma Assert (Present (Priv_Typ));
3083 Add_Own_Invariants
3084 (T => Priv_Typ,
3085 Obj_Id => Obj_Id,
3086 Checks => Stmts);
3088 -- Otherwise the "full" invariant procedure verifies the invariants of
3089 -- the full view, all array or record components, as well as class-wide
3090 -- invariants inherited from parent types or interfaces. In addition, it
3091 -- indirectly verifies the invariants of the partial view by calling the
3092 -- "partial" invariant procedure.
3094 else
3095 pragma Assert (Present (Full_Typ));
3097 -- Check the invariants of the partial view by calling the "partial"
3098 -- invariant procedure. Generate:
3100 -- <Work_Typ>Partial_Invariant (_object);
3102 if Present (Part_Proc) then
3103 Append_New_To (Stmts,
3104 Make_Procedure_Call_Statement (Loc,
3105 Name => New_Occurrence_Of (Part_Proc, Loc),
3106 Parameter_Associations => New_List (
3107 New_Occurrence_Of (Obj_Id, Loc))));
3109 Produced_Check := True;
3110 end if;
3112 Priv_Item := Empty;
3114 -- Derived subtypes do not have a partial view
3116 if Present (Priv_Typ) then
3118 -- The processing of the "full" invariant procedure intentionally
3119 -- skips the partial view because a) this may result in changes of
3120 -- visibility and b) lead to duplicate checks. However, when the
3121 -- full view is the underlying full view of an untagged derived
3122 -- type whose parent type is private, partial invariants appear on
3123 -- the rep item chain of the partial view only.
3125 -- package Pack_1 is
3126 -- type Root ... is private;
3127 -- private
3128 -- <full view of Root>
3129 -- end Pack_1;
3131 -- with Pack_1;
3132 -- package Pack_2 is
3133 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3134 -- <underlying full view of Child>
3135 -- end Pack_2;
3137 -- As a result, the processing of the full view must also consider
3138 -- all invariants of the partial view.
3140 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3141 null;
3143 -- Otherwise the invariants of the partial view are ignored
3145 else
3146 -- Note that the rep item chain is shared between the partial
3147 -- and full views of a type. To avoid processing the invariants
3148 -- of the partial view, signal the logic to stop when the first
3149 -- rep item of the partial view has been reached.
3151 Priv_Item := First_Rep_Item (Priv_Typ);
3153 -- Ignore the invariants of the partial view by eliminating the
3154 -- view.
3156 Priv_Typ := Empty;
3157 end if;
3158 end if;
3160 -- Process the invariants of the full view and in certain cases those
3161 -- of the partial view. This also handles any invariants on array or
3162 -- record components.
3164 Add_Own_Invariants
3165 (T => Priv_Typ,
3166 Obj_Id => Obj_Id,
3167 Checks => Stmts,
3168 Priv_Item => Priv_Item);
3170 Add_Own_Invariants
3171 (T => Full_Typ,
3172 Obj_Id => Obj_Id,
3173 Checks => Stmts,
3174 Priv_Item => Priv_Item);
3176 -- Process the elements of an array type
3178 if Is_Array_Type (Full_Typ) then
3179 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3181 -- Process the components of a record type
3183 elsif Ekind (Full_Typ) = E_Record_Type then
3184 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3186 -- Process the components of a corresponding record
3188 elsif Present (CRec_Typ) then
3189 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3190 end if;
3192 -- Process the inherited class-wide invariants of all parent types.
3193 -- This also handles any invariants on record components.
3195 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3197 -- Process the inherited class-wide invariants of all implemented
3198 -- interface types.
3200 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3201 end if;
3203 End_Scope;
3205 -- At this point there should be at least one invariant check. If this
3206 -- is not the case, then the invariant-related flags were not properly
3207 -- set, or there is a missing invariant procedure on one of the array
3208 -- or record components.
3210 pragma Assert (Produced_Check);
3212 -- Account for the case where assertions are disabled or all invariant
3213 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3214 -- empty body.
3216 if No (Stmts) then
3217 Stmts := New_List (Make_Null_Statement (Loc));
3218 end if;
3220 -- Generate:
3221 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3222 -- begin
3223 -- <Stmts>
3224 -- end <Work_Typ>[Partial_]Invariant;
3226 Proc_Body :=
3227 Make_Subprogram_Body (Loc,
3228 Specification =>
3229 Copy_Subprogram_Spec (Parent (Proc_Id)),
3230 Declarations => Empty_List,
3231 Handled_Statement_Sequence =>
3232 Make_Handled_Sequence_Of_Statements (Loc,
3233 Statements => Stmts));
3234 Proc_Body_Id := Defining_Entity (Proc_Body);
3236 -- Perform minor decoration in case the body is not analyzed
3238 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3239 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3240 Set_Scope (Proc_Body_Id, Current_Scope);
3242 -- Link both spec and body to avoid generating duplicates
3244 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3245 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3247 -- The body should not be inserted into the tree when the context is
3248 -- ASIS or a generic unit because it is not part of the template. Note
3249 -- that the body must still be generated in order to resolve the
3250 -- invariants.
3252 if ASIS_Mode or Inside_A_Generic then
3253 null;
3255 -- Semi-insert the body into the tree for GNATprove by setting its
3256 -- Parent field. This allows for proper upstream tree traversals.
3258 elsif GNATprove_Mode then
3259 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3261 -- Otherwise the body is part of the freezing actions of the type
3263 else
3264 Append_Freeze_Action (Work_Typ, Proc_Body);
3265 end if;
3267 <<Leave>>
3268 Restore_Ghost_Mode (Saved_GM);
3269 end Build_Invariant_Procedure_Body;
3271 -------------------------------------------
3272 -- Build_Invariant_Procedure_Declaration --
3273 -------------------------------------------
3275 -- WARNING: This routine manages Ghost regions. Return statements must be
3276 -- replaced by gotos which jump to the end of the routine and restore the
3277 -- Ghost mode.
3279 procedure Build_Invariant_Procedure_Declaration
3280 (Typ : Entity_Id;
3281 Partial_Invariant : Boolean := False)
3283 Loc : constant Source_Ptr := Sloc (Typ);
3285 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3286 -- Save the Ghost mode to restore on exit
3288 Proc_Decl : Node_Id;
3289 Proc_Id : Entity_Id;
3290 Proc_Nam : Name_Id;
3291 Typ_Decl : Node_Id;
3293 CRec_Typ : Entity_Id;
3294 -- The corresponding record type of Full_Typ
3296 Full_Base : Entity_Id;
3297 -- The base type of Full_Typ
3299 Full_Typ : Entity_Id;
3300 -- The full view of working type
3302 Obj_Id : Entity_Id;
3303 -- The _object formal parameter of the invariant procedure
3305 Obj_Typ : Entity_Id;
3306 -- The type of the _object formal parameter
3308 Priv_Typ : Entity_Id;
3309 -- The partial view of working type
3311 Work_Typ : Entity_Id;
3312 -- The working type
3314 begin
3315 Work_Typ := Typ;
3317 -- The input type denotes the implementation base type of a constrained
3318 -- array type. Work with the first subtype as all invariant pragmas are
3319 -- on its rep item chain.
3321 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3322 Work_Typ := First_Subtype (Work_Typ);
3324 -- The input denotes the corresponding record type of a protected or a
3325 -- task type. Work with the concurrent type because the corresponding
3326 -- record type may not be visible to clients of the type.
3328 elsif Ekind (Work_Typ) = E_Record_Type
3329 and then Is_Concurrent_Record_Type (Work_Typ)
3330 then
3331 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3332 end if;
3334 -- The working type may be subject to pragma Ghost. Set the mode now to
3335 -- ensure that the invariant procedure is properly marked as Ghost.
3337 Set_Ghost_Mode (Work_Typ);
3339 -- The type must either have invariants of its own, inherit class-wide
3340 -- invariants from parent or interface types, or be an array or record
3341 -- type whose components have invariants.
3343 pragma Assert (Has_Invariants (Work_Typ));
3345 -- Nothing to do if the type already has a "partial" invariant procedure
3347 if Partial_Invariant then
3348 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3349 goto Leave;
3350 end if;
3352 -- Nothing to do if the type already has a "full" invariant procedure
3354 elsif Present (Invariant_Procedure (Work_Typ)) then
3355 goto Leave;
3356 end if;
3358 -- The caller requests the declaration of the "partial" invariant
3359 -- procedure.
3361 if Partial_Invariant then
3362 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3364 -- Otherwise the caller requests the declaration of the "full" invariant
3365 -- procedure.
3367 else
3368 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3369 end if;
3371 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3373 -- Perform minor decoration in case the declaration is not analyzed
3375 Set_Ekind (Proc_Id, E_Procedure);
3376 Set_Etype (Proc_Id, Standard_Void_Type);
3377 Set_Scope (Proc_Id, Current_Scope);
3379 if Partial_Invariant then
3380 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3381 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3382 else
3383 Set_Is_Invariant_Procedure (Proc_Id);
3384 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3385 end if;
3387 -- The invariant procedure requires debug info when the invariants are
3388 -- subject to Source Coverage Obligations.
3390 if Generate_SCO then
3391 Set_Needs_Debug_Info (Proc_Id);
3392 end if;
3394 -- Obtain all views of the input type
3396 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3398 -- Associate the invariant procedure with all views
3400 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3401 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3402 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3403 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3405 -- The declaration of the invariant procedure is inserted after the
3406 -- declaration of the partial view as this allows for proper external
3407 -- visibility.
3409 if Present (Priv_Typ) then
3410 Typ_Decl := Declaration_Node (Priv_Typ);
3412 -- Anonymous arrays in object declarations have no explicit declaration
3413 -- so use the related object declaration as the insertion point.
3415 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3416 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3418 -- Derived types with the full view as parent do not have a partial
3419 -- view. Insert the invariant procedure after the derived type.
3421 else
3422 Typ_Decl := Declaration_Node (Full_Typ);
3423 end if;
3425 -- The type should have a declarative node
3427 pragma Assert (Present (Typ_Decl));
3429 -- Create the formal parameter which emulates the variable-like behavior
3430 -- of the current type instance.
3432 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3434 -- When generating an invariant procedure declaration for an abstract
3435 -- type (including interfaces), use the class-wide type as the _object
3436 -- type. This has several desirable effects:
3438 -- * The invariant procedure does not become a primitive of the type.
3439 -- This eliminates the need to either special case the treatment of
3440 -- invariant procedures, or to make it a predefined primitive and
3441 -- force every derived type to potentially provide an empty body.
3443 -- * The invariant procedure does not need to be declared as abstract.
3444 -- This allows for a proper body, which in turn avoids redundant
3445 -- processing of the same invariants for types with multiple views.
3447 -- * The class-wide type allows for calls to abstract primitives
3448 -- within a nonabstract subprogram. The calls are treated as
3449 -- dispatching and require additional processing when they are
3450 -- remapped to call primitives of derived types. See routine
3451 -- Replace_References for details.
3453 if Is_Abstract_Type (Work_Typ) then
3454 Obj_Typ := Class_Wide_Type (Work_Typ);
3455 else
3456 Obj_Typ := Work_Typ;
3457 end if;
3459 -- Perform minor decoration in case the declaration is not analyzed
3461 Set_Ekind (Obj_Id, E_In_Parameter);
3462 Set_Etype (Obj_Id, Obj_Typ);
3463 Set_Scope (Obj_Id, Proc_Id);
3465 Set_First_Entity (Proc_Id, Obj_Id);
3467 -- Generate:
3468 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3470 Proc_Decl :=
3471 Make_Subprogram_Declaration (Loc,
3472 Specification =>
3473 Make_Procedure_Specification (Loc,
3474 Defining_Unit_Name => Proc_Id,
3475 Parameter_Specifications => New_List (
3476 Make_Parameter_Specification (Loc,
3477 Defining_Identifier => Obj_Id,
3478 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3480 -- The declaration should not be inserted into the tree when the context
3481 -- is ASIS or a generic unit because it is not part of the template.
3483 if ASIS_Mode or Inside_A_Generic then
3484 null;
3486 -- Semi-insert the declaration into the tree for GNATprove by setting
3487 -- its Parent field. This allows for proper upstream tree traversals.
3489 elsif GNATprove_Mode then
3490 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3492 -- Otherwise insert the declaration
3494 else
3495 pragma Assert (Present (Typ_Decl));
3496 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3497 end if;
3499 <<Leave>>
3500 Restore_Ghost_Mode (Saved_GM);
3501 end Build_Invariant_Procedure_Declaration;
3503 --------------------------
3504 -- Build_Procedure_Form --
3505 --------------------------
3507 procedure Build_Procedure_Form (N : Node_Id) is
3508 Loc : constant Source_Ptr := Sloc (N);
3509 Subp : constant Entity_Id := Defining_Entity (N);
3511 Func_Formal : Entity_Id;
3512 Proc_Formals : List_Id;
3513 Proc_Decl : Node_Id;
3515 begin
3516 -- No action needed if this transformation was already done, or in case
3517 -- of subprogram renaming declarations.
3519 if Nkind (Specification (N)) = N_Procedure_Specification
3520 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3521 then
3522 return;
3523 end if;
3525 -- Ditto when dealing with an expression function, where both the
3526 -- original expression and the generated declaration end up being
3527 -- expanded here.
3529 if Rewritten_For_C (Subp) then
3530 return;
3531 end if;
3533 Proc_Formals := New_List;
3535 -- Create a list of formal parameters with the same types as the
3536 -- function.
3538 Func_Formal := First_Formal (Subp);
3539 while Present (Func_Formal) loop
3540 Append_To (Proc_Formals,
3541 Make_Parameter_Specification (Loc,
3542 Defining_Identifier =>
3543 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3544 Parameter_Type =>
3545 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3547 Next_Formal (Func_Formal);
3548 end loop;
3550 -- Add an extra out parameter to carry the function result
3552 Name_Len := 6;
3553 Name_Buffer (1 .. Name_Len) := "RESULT";
3554 Append_To (Proc_Formals,
3555 Make_Parameter_Specification (Loc,
3556 Defining_Identifier =>
3557 Make_Defining_Identifier (Loc, Chars => Name_Find),
3558 Out_Present => True,
3559 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3561 -- The new procedure declaration is inserted immediately after the
3562 -- function declaration. The processing in Build_Procedure_Body_Form
3563 -- relies on this order.
3565 Proc_Decl :=
3566 Make_Subprogram_Declaration (Loc,
3567 Specification =>
3568 Make_Procedure_Specification (Loc,
3569 Defining_Unit_Name =>
3570 Make_Defining_Identifier (Loc, Chars (Subp)),
3571 Parameter_Specifications => Proc_Formals));
3573 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3575 -- Entity of procedure must remain invisible so that it does not
3576 -- overload subsequent references to the original function.
3578 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3580 -- Mark the function as having a procedure form and link the function
3581 -- and its internally built procedure.
3583 Set_Rewritten_For_C (Subp);
3584 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3585 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3586 end Build_Procedure_Form;
3588 ------------------------
3589 -- Build_Runtime_Call --
3590 ------------------------
3592 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3593 begin
3594 -- If entity is not available, we can skip making the call (this avoids
3595 -- junk duplicated error messages in a number of cases).
3597 if not RTE_Available (RE) then
3598 return Make_Null_Statement (Loc);
3599 else
3600 return
3601 Make_Procedure_Call_Statement (Loc,
3602 Name => New_Occurrence_Of (RTE (RE), Loc));
3603 end if;
3604 end Build_Runtime_Call;
3606 ------------------------
3607 -- Build_SS_Mark_Call --
3608 ------------------------
3610 function Build_SS_Mark_Call
3611 (Loc : Source_Ptr;
3612 Mark : Entity_Id) return Node_Id
3614 begin
3615 -- Generate:
3616 -- Mark : constant Mark_Id := SS_Mark;
3618 return
3619 Make_Object_Declaration (Loc,
3620 Defining_Identifier => Mark,
3621 Constant_Present => True,
3622 Object_Definition =>
3623 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3624 Expression =>
3625 Make_Function_Call (Loc,
3626 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3627 end Build_SS_Mark_Call;
3629 ---------------------------
3630 -- Build_SS_Release_Call --
3631 ---------------------------
3633 function Build_SS_Release_Call
3634 (Loc : Source_Ptr;
3635 Mark : Entity_Id) return Node_Id
3637 begin
3638 -- Generate:
3639 -- SS_Release (Mark);
3641 return
3642 Make_Procedure_Call_Statement (Loc,
3643 Name =>
3644 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3645 Parameter_Associations => New_List (
3646 New_Occurrence_Of (Mark, Loc)));
3647 end Build_SS_Release_Call;
3649 ----------------------------
3650 -- Build_Task_Array_Image --
3651 ----------------------------
3653 -- This function generates the body for a function that constructs the
3654 -- image string for a task that is an array component. The function is
3655 -- local to the init proc for the array type, and is called for each one
3656 -- of the components. The constructed image has the form of an indexed
3657 -- component, whose prefix is the outer variable of the array type.
3658 -- The n-dimensional array type has known indexes Index, Index2...
3660 -- Id_Ref is an indexed component form created by the enclosing init proc.
3661 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3662 -- in the loops that call the individual task init proc on each component.
3664 -- The generated function has the following structure:
3666 -- function F return String is
3667 -- Pref : string renames Task_Name;
3668 -- T1 : String := Index1'Image (Val1);
3669 -- ...
3670 -- Tn : String := indexn'image (Valn);
3671 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3672 -- -- Len includes commas and the end parentheses.
3673 -- Res : String (1..Len);
3674 -- Pos : Integer := Pref'Length;
3676 -- begin
3677 -- Res (1 .. Pos) := Pref;
3678 -- Pos := Pos + 1;
3679 -- Res (Pos) := '(';
3680 -- Pos := Pos + 1;
3681 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3682 -- Pos := Pos + T1'Length;
3683 -- Res (Pos) := '.';
3684 -- Pos := Pos + 1;
3685 -- ...
3686 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3687 -- Res (Len) := ')';
3689 -- return Res;
3690 -- end F;
3692 -- Needless to say, multidimensional arrays of tasks are rare enough that
3693 -- the bulkiness of this code is not really a concern.
3695 function Build_Task_Array_Image
3696 (Loc : Source_Ptr;
3697 Id_Ref : Node_Id;
3698 A_Type : Entity_Id;
3699 Dyn : Boolean := False) return Node_Id
3701 Dims : constant Nat := Number_Dimensions (A_Type);
3702 -- Number of dimensions for array of tasks
3704 Temps : array (1 .. Dims) of Entity_Id;
3705 -- Array of temporaries to hold string for each index
3707 Indx : Node_Id;
3708 -- Index expression
3710 Len : Entity_Id;
3711 -- Total length of generated name
3713 Pos : Entity_Id;
3714 -- Running index for substring assignments
3716 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3717 -- Name of enclosing variable, prefix of resulting name
3719 Res : Entity_Id;
3720 -- String to hold result
3722 Val : Node_Id;
3723 -- Value of successive indexes
3725 Sum : Node_Id;
3726 -- Expression to compute total size of string
3728 T : Entity_Id;
3729 -- Entity for name at one index position
3731 Decls : constant List_Id := New_List;
3732 Stats : constant List_Id := New_List;
3734 begin
3735 -- For a dynamic task, the name comes from the target variable. For a
3736 -- static one it is a formal of the enclosing init proc.
3738 if Dyn then
3739 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3740 Append_To (Decls,
3741 Make_Object_Declaration (Loc,
3742 Defining_Identifier => Pref,
3743 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3744 Expression =>
3745 Make_String_Literal (Loc,
3746 Strval => String_From_Name_Buffer)));
3748 else
3749 Append_To (Decls,
3750 Make_Object_Renaming_Declaration (Loc,
3751 Defining_Identifier => Pref,
3752 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3753 Name => Make_Identifier (Loc, Name_uTask_Name)));
3754 end if;
3756 Indx := First_Index (A_Type);
3757 Val := First (Expressions (Id_Ref));
3759 for J in 1 .. Dims loop
3760 T := Make_Temporary (Loc, 'T');
3761 Temps (J) := T;
3763 Append_To (Decls,
3764 Make_Object_Declaration (Loc,
3765 Defining_Identifier => T,
3766 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3767 Expression =>
3768 Make_Attribute_Reference (Loc,
3769 Attribute_Name => Name_Image,
3770 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3771 Expressions => New_List (New_Copy_Tree (Val)))));
3773 Next_Index (Indx);
3774 Next (Val);
3775 end loop;
3777 Sum := Make_Integer_Literal (Loc, Dims + 1);
3779 Sum :=
3780 Make_Op_Add (Loc,
3781 Left_Opnd => Sum,
3782 Right_Opnd =>
3783 Make_Attribute_Reference (Loc,
3784 Attribute_Name => Name_Length,
3785 Prefix => New_Occurrence_Of (Pref, Loc),
3786 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3788 for J in 1 .. Dims loop
3789 Sum :=
3790 Make_Op_Add (Loc,
3791 Left_Opnd => Sum,
3792 Right_Opnd =>
3793 Make_Attribute_Reference (Loc,
3794 Attribute_Name => Name_Length,
3795 Prefix =>
3796 New_Occurrence_Of (Temps (J), Loc),
3797 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3798 end loop;
3800 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3802 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3804 Append_To (Stats,
3805 Make_Assignment_Statement (Loc,
3806 Name =>
3807 Make_Indexed_Component (Loc,
3808 Prefix => New_Occurrence_Of (Res, Loc),
3809 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3810 Expression =>
3811 Make_Character_Literal (Loc,
3812 Chars => Name_Find,
3813 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3815 Append_To (Stats,
3816 Make_Assignment_Statement (Loc,
3817 Name => New_Occurrence_Of (Pos, Loc),
3818 Expression =>
3819 Make_Op_Add (Loc,
3820 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3821 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3823 for J in 1 .. Dims loop
3825 Append_To (Stats,
3826 Make_Assignment_Statement (Loc,
3827 Name =>
3828 Make_Slice (Loc,
3829 Prefix => New_Occurrence_Of (Res, Loc),
3830 Discrete_Range =>
3831 Make_Range (Loc,
3832 Low_Bound => New_Occurrence_Of (Pos, Loc),
3833 High_Bound =>
3834 Make_Op_Subtract (Loc,
3835 Left_Opnd =>
3836 Make_Op_Add (Loc,
3837 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3838 Right_Opnd =>
3839 Make_Attribute_Reference (Loc,
3840 Attribute_Name => Name_Length,
3841 Prefix =>
3842 New_Occurrence_Of (Temps (J), Loc),
3843 Expressions =>
3844 New_List (Make_Integer_Literal (Loc, 1)))),
3845 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3847 Expression => New_Occurrence_Of (Temps (J), Loc)));
3849 if J < Dims then
3850 Append_To (Stats,
3851 Make_Assignment_Statement (Loc,
3852 Name => New_Occurrence_Of (Pos, Loc),
3853 Expression =>
3854 Make_Op_Add (Loc,
3855 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3856 Right_Opnd =>
3857 Make_Attribute_Reference (Loc,
3858 Attribute_Name => Name_Length,
3859 Prefix => New_Occurrence_Of (Temps (J), Loc),
3860 Expressions =>
3861 New_List (Make_Integer_Literal (Loc, 1))))));
3863 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3865 Append_To (Stats,
3866 Make_Assignment_Statement (Loc,
3867 Name => Make_Indexed_Component (Loc,
3868 Prefix => New_Occurrence_Of (Res, Loc),
3869 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3870 Expression =>
3871 Make_Character_Literal (Loc,
3872 Chars => Name_Find,
3873 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3875 Append_To (Stats,
3876 Make_Assignment_Statement (Loc,
3877 Name => New_Occurrence_Of (Pos, Loc),
3878 Expression =>
3879 Make_Op_Add (Loc,
3880 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3881 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3882 end if;
3883 end loop;
3885 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3887 Append_To (Stats,
3888 Make_Assignment_Statement (Loc,
3889 Name =>
3890 Make_Indexed_Component (Loc,
3891 Prefix => New_Occurrence_Of (Res, Loc),
3892 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3893 Expression =>
3894 Make_Character_Literal (Loc,
3895 Chars => Name_Find,
3896 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3897 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3898 end Build_Task_Array_Image;
3900 ----------------------------
3901 -- Build_Task_Image_Decls --
3902 ----------------------------
3904 function Build_Task_Image_Decls
3905 (Loc : Source_Ptr;
3906 Id_Ref : Node_Id;
3907 A_Type : Entity_Id;
3908 In_Init_Proc : Boolean := False) return List_Id
3910 Decls : constant List_Id := New_List;
3911 T_Id : Entity_Id := Empty;
3912 Decl : Node_Id;
3913 Expr : Node_Id := Empty;
3914 Fun : Node_Id := Empty;
3915 Is_Dyn : constant Boolean :=
3916 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3917 and then
3918 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3920 begin
3921 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3922 -- generate a dummy declaration only.
3924 if Restriction_Active (No_Implicit_Heap_Allocations)
3925 or else Global_Discard_Names
3926 then
3927 T_Id := Make_Temporary (Loc, 'J');
3928 Name_Len := 0;
3930 return
3931 New_List (
3932 Make_Object_Declaration (Loc,
3933 Defining_Identifier => T_Id,
3934 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3935 Expression =>
3936 Make_String_Literal (Loc,
3937 Strval => String_From_Name_Buffer)));
3939 else
3940 if Nkind (Id_Ref) = N_Identifier
3941 or else Nkind (Id_Ref) = N_Defining_Identifier
3942 then
3943 -- For a simple variable, the image of the task is built from
3944 -- the name of the variable. To avoid possible conflict with the
3945 -- anonymous type created for a single protected object, add a
3946 -- numeric suffix.
3948 T_Id :=
3949 Make_Defining_Identifier (Loc,
3950 New_External_Name (Chars (Id_Ref), 'T', 1));
3952 Get_Name_String (Chars (Id_Ref));
3954 Expr :=
3955 Make_String_Literal (Loc,
3956 Strval => String_From_Name_Buffer);
3958 elsif Nkind (Id_Ref) = N_Selected_Component then
3959 T_Id :=
3960 Make_Defining_Identifier (Loc,
3961 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3962 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3964 elsif Nkind (Id_Ref) = N_Indexed_Component then
3965 T_Id :=
3966 Make_Defining_Identifier (Loc,
3967 New_External_Name (Chars (A_Type), 'N'));
3969 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3970 end if;
3971 end if;
3973 if Present (Fun) then
3974 Append (Fun, Decls);
3975 Expr := Make_Function_Call (Loc,
3976 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
3978 if not In_Init_Proc then
3979 Set_Uses_Sec_Stack (Defining_Entity (Fun));
3980 end if;
3981 end if;
3983 Decl := Make_Object_Declaration (Loc,
3984 Defining_Identifier => T_Id,
3985 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3986 Constant_Present => True,
3987 Expression => Expr);
3989 Append (Decl, Decls);
3990 return Decls;
3991 end Build_Task_Image_Decls;
3993 -------------------------------
3994 -- Build_Task_Image_Function --
3995 -------------------------------
3997 function Build_Task_Image_Function
3998 (Loc : Source_Ptr;
3999 Decls : List_Id;
4000 Stats : List_Id;
4001 Res : Entity_Id) return Node_Id
4003 Spec : Node_Id;
4005 begin
4006 Append_To (Stats,
4007 Make_Simple_Return_Statement (Loc,
4008 Expression => New_Occurrence_Of (Res, Loc)));
4010 Spec := Make_Function_Specification (Loc,
4011 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4012 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4014 -- Calls to 'Image use the secondary stack, which must be cleaned up
4015 -- after the task name is built.
4017 return Make_Subprogram_Body (Loc,
4018 Specification => Spec,
4019 Declarations => Decls,
4020 Handled_Statement_Sequence =>
4021 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4022 end Build_Task_Image_Function;
4024 -----------------------------
4025 -- Build_Task_Image_Prefix --
4026 -----------------------------
4028 procedure Build_Task_Image_Prefix
4029 (Loc : Source_Ptr;
4030 Len : out Entity_Id;
4031 Res : out Entity_Id;
4032 Pos : out Entity_Id;
4033 Prefix : Entity_Id;
4034 Sum : Node_Id;
4035 Decls : List_Id;
4036 Stats : List_Id)
4038 begin
4039 Len := Make_Temporary (Loc, 'L', Sum);
4041 Append_To (Decls,
4042 Make_Object_Declaration (Loc,
4043 Defining_Identifier => Len,
4044 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4045 Expression => Sum));
4047 Res := Make_Temporary (Loc, 'R');
4049 Append_To (Decls,
4050 Make_Object_Declaration (Loc,
4051 Defining_Identifier => Res,
4052 Object_Definition =>
4053 Make_Subtype_Indication (Loc,
4054 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4055 Constraint =>
4056 Make_Index_Or_Discriminant_Constraint (Loc,
4057 Constraints =>
4058 New_List (
4059 Make_Range (Loc,
4060 Low_Bound => Make_Integer_Literal (Loc, 1),
4061 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4063 -- Indicate that the result is an internal temporary, so it does not
4064 -- receive a bogus initialization when declaration is expanded. This
4065 -- is both efficient, and prevents anomalies in the handling of
4066 -- dynamic objects on the secondary stack.
4068 Set_Is_Internal (Res);
4069 Pos := Make_Temporary (Loc, 'P');
4071 Append_To (Decls,
4072 Make_Object_Declaration (Loc,
4073 Defining_Identifier => Pos,
4074 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4076 -- Pos := Prefix'Length;
4078 Append_To (Stats,
4079 Make_Assignment_Statement (Loc,
4080 Name => New_Occurrence_Of (Pos, Loc),
4081 Expression =>
4082 Make_Attribute_Reference (Loc,
4083 Attribute_Name => Name_Length,
4084 Prefix => New_Occurrence_Of (Prefix, Loc),
4085 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4087 -- Res (1 .. Pos) := Prefix;
4089 Append_To (Stats,
4090 Make_Assignment_Statement (Loc,
4091 Name =>
4092 Make_Slice (Loc,
4093 Prefix => New_Occurrence_Of (Res, Loc),
4094 Discrete_Range =>
4095 Make_Range (Loc,
4096 Low_Bound => Make_Integer_Literal (Loc, 1),
4097 High_Bound => New_Occurrence_Of (Pos, Loc))),
4099 Expression => New_Occurrence_Of (Prefix, Loc)));
4101 Append_To (Stats,
4102 Make_Assignment_Statement (Loc,
4103 Name => New_Occurrence_Of (Pos, Loc),
4104 Expression =>
4105 Make_Op_Add (Loc,
4106 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4107 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4108 end Build_Task_Image_Prefix;
4110 -----------------------------
4111 -- Build_Task_Record_Image --
4112 -----------------------------
4114 function Build_Task_Record_Image
4115 (Loc : Source_Ptr;
4116 Id_Ref : Node_Id;
4117 Dyn : Boolean := False) return Node_Id
4119 Len : Entity_Id;
4120 -- Total length of generated name
4122 Pos : Entity_Id;
4123 -- Index into result
4125 Res : Entity_Id;
4126 -- String to hold result
4128 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4129 -- Name of enclosing variable, prefix of resulting name
4131 Sum : Node_Id;
4132 -- Expression to compute total size of string
4134 Sel : Entity_Id;
4135 -- Entity for selector name
4137 Decls : constant List_Id := New_List;
4138 Stats : constant List_Id := New_List;
4140 begin
4141 -- For a dynamic task, the name comes from the target variable. For a
4142 -- static one it is a formal of the enclosing init proc.
4144 if Dyn then
4145 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4146 Append_To (Decls,
4147 Make_Object_Declaration (Loc,
4148 Defining_Identifier => Pref,
4149 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4150 Expression =>
4151 Make_String_Literal (Loc,
4152 Strval => String_From_Name_Buffer)));
4154 else
4155 Append_To (Decls,
4156 Make_Object_Renaming_Declaration (Loc,
4157 Defining_Identifier => Pref,
4158 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4159 Name => Make_Identifier (Loc, Name_uTask_Name)));
4160 end if;
4162 Sel := Make_Temporary (Loc, 'S');
4164 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4166 Append_To (Decls,
4167 Make_Object_Declaration (Loc,
4168 Defining_Identifier => Sel,
4169 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4170 Expression =>
4171 Make_String_Literal (Loc,
4172 Strval => String_From_Name_Buffer)));
4174 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4176 Sum :=
4177 Make_Op_Add (Loc,
4178 Left_Opnd => Sum,
4179 Right_Opnd =>
4180 Make_Attribute_Reference (Loc,
4181 Attribute_Name => Name_Length,
4182 Prefix =>
4183 New_Occurrence_Of (Pref, Loc),
4184 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4186 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4188 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4190 -- Res (Pos) := '.';
4192 Append_To (Stats,
4193 Make_Assignment_Statement (Loc,
4194 Name => Make_Indexed_Component (Loc,
4195 Prefix => New_Occurrence_Of (Res, Loc),
4196 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4197 Expression =>
4198 Make_Character_Literal (Loc,
4199 Chars => Name_Find,
4200 Char_Literal_Value =>
4201 UI_From_Int (Character'Pos ('.')))));
4203 Append_To (Stats,
4204 Make_Assignment_Statement (Loc,
4205 Name => New_Occurrence_Of (Pos, Loc),
4206 Expression =>
4207 Make_Op_Add (Loc,
4208 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4209 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4211 -- Res (Pos .. Len) := Selector;
4213 Append_To (Stats,
4214 Make_Assignment_Statement (Loc,
4215 Name => Make_Slice (Loc,
4216 Prefix => New_Occurrence_Of (Res, Loc),
4217 Discrete_Range =>
4218 Make_Range (Loc,
4219 Low_Bound => New_Occurrence_Of (Pos, Loc),
4220 High_Bound => New_Occurrence_Of (Len, Loc))),
4221 Expression => New_Occurrence_Of (Sel, Loc)));
4223 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4224 end Build_Task_Record_Image;
4226 ---------------------------------------
4227 -- Build_Transient_Object_Statements --
4228 ---------------------------------------
4230 procedure Build_Transient_Object_Statements
4231 (Obj_Decl : Node_Id;
4232 Fin_Call : out Node_Id;
4233 Hook_Assign : out Node_Id;
4234 Hook_Clear : out Node_Id;
4235 Hook_Decl : out Node_Id;
4236 Ptr_Decl : out Node_Id;
4237 Finalize_Obj : Boolean := True)
4239 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4240 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4241 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4243 Desig_Typ : Entity_Id;
4244 Hook_Expr : Node_Id;
4245 Hook_Id : Entity_Id;
4246 Obj_Ref : Node_Id;
4247 Ptr_Typ : Entity_Id;
4249 begin
4250 -- Recover the type of the object
4252 Desig_Typ := Obj_Typ;
4254 if Is_Access_Type (Desig_Typ) then
4255 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4256 end if;
4258 -- Create an access type which provides a reference to the transient
4259 -- object. Generate:
4261 -- type Ptr_Typ is access all Desig_Typ;
4263 Ptr_Typ := Make_Temporary (Loc, 'A');
4264 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4265 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4267 Ptr_Decl :=
4268 Make_Full_Type_Declaration (Loc,
4269 Defining_Identifier => Ptr_Typ,
4270 Type_Definition =>
4271 Make_Access_To_Object_Definition (Loc,
4272 All_Present => True,
4273 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4275 -- Create a temporary check which acts as a hook to the transient
4276 -- object. Generate:
4278 -- Hook : Ptr_Typ := null;
4280 Hook_Id := Make_Temporary (Loc, 'T');
4281 Set_Ekind (Hook_Id, E_Variable);
4282 Set_Etype (Hook_Id, Ptr_Typ);
4284 Hook_Decl :=
4285 Make_Object_Declaration (Loc,
4286 Defining_Identifier => Hook_Id,
4287 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4288 Expression => Make_Null (Loc));
4290 -- Mark the temporary as a hook. This signals the machinery in
4291 -- Build_Finalizer to recognize this special case.
4293 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4295 -- Hook the transient object to the temporary. Generate:
4297 -- Hook := Ptr_Typ (Obj_Id);
4298 -- <or>
4299 -- Hool := Obj_Id'Unrestricted_Access;
4301 if Is_Access_Type (Obj_Typ) then
4302 Hook_Expr :=
4303 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4304 else
4305 Hook_Expr :=
4306 Make_Attribute_Reference (Loc,
4307 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4308 Attribute_Name => Name_Unrestricted_Access);
4309 end if;
4311 Hook_Assign :=
4312 Make_Assignment_Statement (Loc,
4313 Name => New_Occurrence_Of (Hook_Id, Loc),
4314 Expression => Hook_Expr);
4316 -- Crear the hook prior to finalizing the object. Generate:
4318 -- Hook := null;
4320 Hook_Clear :=
4321 Make_Assignment_Statement (Loc,
4322 Name => New_Occurrence_Of (Hook_Id, Loc),
4323 Expression => Make_Null (Loc));
4325 -- Finalize the object. Generate:
4327 -- [Deep_]Finalize (Obj_Ref[.all]);
4329 if Finalize_Obj then
4330 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4332 if Is_Access_Type (Obj_Typ) then
4333 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4334 Set_Etype (Obj_Ref, Desig_Typ);
4335 end if;
4337 Fin_Call :=
4338 Make_Final_Call
4339 (Obj_Ref => Obj_Ref,
4340 Typ => Desig_Typ);
4342 -- Otherwise finalize the hook. Generate:
4344 -- [Deep_]Finalize (Hook.all);
4346 else
4347 Fin_Call :=
4348 Make_Final_Call (
4349 Obj_Ref =>
4350 Make_Explicit_Dereference (Loc,
4351 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4352 Typ => Desig_Typ);
4353 end if;
4354 end Build_Transient_Object_Statements;
4356 -----------------------------
4357 -- Check_Float_Op_Overflow --
4358 -----------------------------
4360 procedure Check_Float_Op_Overflow (N : Node_Id) is
4361 begin
4362 -- Return if no check needed
4364 if not Is_Floating_Point_Type (Etype (N))
4365 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4367 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4368 -- and do not expand the code for float overflow checking.
4370 or else CodePeer_Mode
4371 then
4372 return;
4373 end if;
4375 -- Otherwise we replace the expression by
4377 -- do Tnn : constant ftype := expression;
4378 -- constraint_error when not Tnn'Valid;
4379 -- in Tnn;
4381 declare
4382 Loc : constant Source_Ptr := Sloc (N);
4383 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4384 Typ : constant Entity_Id := Etype (N);
4386 begin
4387 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4388 -- right here. We also set the node as analyzed to prevent infinite
4389 -- recursion from repeating the operation in the expansion.
4391 Set_Do_Overflow_Check (N, False);
4392 Set_Analyzed (N, True);
4394 -- Do the rewrite to include the check
4396 Rewrite (N,
4397 Make_Expression_With_Actions (Loc,
4398 Actions => New_List (
4399 Make_Object_Declaration (Loc,
4400 Defining_Identifier => Tnn,
4401 Object_Definition => New_Occurrence_Of (Typ, Loc),
4402 Constant_Present => True,
4403 Expression => Relocate_Node (N)),
4404 Make_Raise_Constraint_Error (Loc,
4405 Condition =>
4406 Make_Op_Not (Loc,
4407 Right_Opnd =>
4408 Make_Attribute_Reference (Loc,
4409 Prefix => New_Occurrence_Of (Tnn, Loc),
4410 Attribute_Name => Name_Valid)),
4411 Reason => CE_Overflow_Check_Failed)),
4412 Expression => New_Occurrence_Of (Tnn, Loc)));
4414 Analyze_And_Resolve (N, Typ);
4415 end;
4416 end Check_Float_Op_Overflow;
4418 ----------------------------------
4419 -- Component_May_Be_Bit_Aligned --
4420 ----------------------------------
4422 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4423 UT : Entity_Id;
4425 begin
4426 -- If no component clause, then everything is fine, since the back end
4427 -- never bit-misaligns by default, even if there is a pragma Packed for
4428 -- the record.
4430 if No (Comp) or else No (Component_Clause (Comp)) then
4431 return False;
4432 end if;
4434 UT := Underlying_Type (Etype (Comp));
4436 -- It is only array and record types that cause trouble
4438 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4439 return False;
4441 -- If we know that we have a small (64 bits or less) record or small
4442 -- bit-packed array, then everything is fine, since the back end can
4443 -- handle these cases correctly.
4445 elsif Esize (Comp) <= 64
4446 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4447 then
4448 return False;
4450 -- Otherwise if the component is not byte aligned, we know we have the
4451 -- nasty unaligned case.
4453 elsif Normalized_First_Bit (Comp) /= Uint_0
4454 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4455 then
4456 return True;
4458 -- If we are large and byte aligned, then OK at this level
4460 else
4461 return False;
4462 end if;
4463 end Component_May_Be_Bit_Aligned;
4465 ----------------------------------------
4466 -- Containing_Package_With_Ext_Axioms --
4467 ----------------------------------------
4469 function Containing_Package_With_Ext_Axioms
4470 (E : Entity_Id) return Entity_Id
4472 begin
4473 -- E is the package or generic package which is externally axiomatized
4475 if Ekind_In (E, E_Generic_Package, E_Package)
4476 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4477 then
4478 return E;
4479 end if;
4481 -- If E's scope is axiomatized, E is axiomatized
4483 if Present (Scope (E)) then
4484 declare
4485 First_Ax_Parent_Scope : constant Entity_Id :=
4486 Containing_Package_With_Ext_Axioms (Scope (E));
4487 begin
4488 if Present (First_Ax_Parent_Scope) then
4489 return First_Ax_Parent_Scope;
4490 end if;
4491 end;
4492 end if;
4494 -- Otherwise, if E is a package instance, it is axiomatized if the
4495 -- corresponding generic package is axiomatized.
4497 if Ekind (E) = E_Package then
4498 declare
4499 Par : constant Node_Id := Parent (E);
4500 Decl : Node_Id;
4502 begin
4503 if Nkind (Par) = N_Defining_Program_Unit_Name then
4504 Decl := Parent (Par);
4505 else
4506 Decl := Par;
4507 end if;
4509 if Present (Generic_Parent (Decl)) then
4510 return
4511 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4512 end if;
4513 end;
4514 end if;
4516 return Empty;
4517 end Containing_Package_With_Ext_Axioms;
4519 -------------------------------
4520 -- Convert_To_Actual_Subtype --
4521 -------------------------------
4523 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4524 Act_ST : Entity_Id;
4526 begin
4527 Act_ST := Get_Actual_Subtype (Exp);
4529 if Act_ST = Etype (Exp) then
4530 return;
4531 else
4532 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4533 Analyze_And_Resolve (Exp, Act_ST);
4534 end if;
4535 end Convert_To_Actual_Subtype;
4537 -----------------------------------
4538 -- Corresponding_Runtime_Package --
4539 -----------------------------------
4541 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4542 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4543 -- Return True if protected type T has one entry and the maximum queue
4544 -- length is one.
4546 --------------------------------
4547 -- Has_One_Entry_And_No_Queue --
4548 --------------------------------
4550 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4551 Item : Entity_Id;
4552 Is_First : Boolean := True;
4554 begin
4555 Item := First_Entity (T);
4556 while Present (Item) loop
4557 if Is_Entry (Item) then
4559 -- The protected type has more than one entry
4561 if not Is_First then
4562 return False;
4563 end if;
4565 -- The queue length is not one
4567 if not Restriction_Active (No_Entry_Queue)
4568 and then Get_Max_Queue_Length (Item) /= Uint_1
4569 then
4570 return False;
4571 end if;
4573 Is_First := False;
4574 end if;
4576 Next_Entity (Item);
4577 end loop;
4579 return True;
4580 end Has_One_Entry_And_No_Queue;
4582 -- Local variables
4584 Pkg_Id : RTU_Id := RTU_Null;
4586 -- Start of processing for Corresponding_Runtime_Package
4588 begin
4589 pragma Assert (Is_Concurrent_Type (Typ));
4591 if Ekind (Typ) in Protected_Kind then
4592 if Has_Entries (Typ)
4594 -- A protected type without entries that covers an interface and
4595 -- overrides the abstract routines with protected procedures is
4596 -- considered equivalent to a protected type with entries in the
4597 -- context of dispatching select statements. It is sufficient to
4598 -- check for the presence of an interface list in the declaration
4599 -- node to recognize this case.
4601 or else Present (Interface_List (Parent (Typ)))
4603 -- Protected types with interrupt handlers (when not using a
4604 -- restricted profile) are also considered equivalent to
4605 -- protected types with entries. The types which are used
4606 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4607 -- are derived from Protection_Entries.
4609 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4610 or else Has_Interrupt_Handler (Typ)
4611 then
4612 if Abort_Allowed
4613 or else Restriction_Active (No_Select_Statements) = False
4614 or else not Has_One_Entry_And_No_Queue (Typ)
4615 or else (Has_Attach_Handler (Typ)
4616 and then not Restricted_Profile)
4617 then
4618 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4619 else
4620 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4621 end if;
4623 else
4624 Pkg_Id := System_Tasking_Protected_Objects;
4625 end if;
4626 end if;
4628 return Pkg_Id;
4629 end Corresponding_Runtime_Package;
4631 -----------------------------------
4632 -- Current_Sem_Unit_Declarations --
4633 -----------------------------------
4635 function Current_Sem_Unit_Declarations return List_Id is
4636 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4637 Decls : List_Id;
4639 begin
4640 -- If the current unit is a package body, locate the visible
4641 -- declarations of the package spec.
4643 if Nkind (U) = N_Package_Body then
4644 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4645 end if;
4647 if Nkind (U) = N_Package_Declaration then
4648 U := Specification (U);
4649 Decls := Visible_Declarations (U);
4651 if No (Decls) then
4652 Decls := New_List;
4653 Set_Visible_Declarations (U, Decls);
4654 end if;
4656 else
4657 Decls := Declarations (U);
4659 if No (Decls) then
4660 Decls := New_List;
4661 Set_Declarations (U, Decls);
4662 end if;
4663 end if;
4665 return Decls;
4666 end Current_Sem_Unit_Declarations;
4668 -----------------------
4669 -- Duplicate_Subexpr --
4670 -----------------------
4672 function Duplicate_Subexpr
4673 (Exp : Node_Id;
4674 Name_Req : Boolean := False;
4675 Renaming_Req : Boolean := False) return Node_Id
4677 begin
4678 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4679 return New_Copy_Tree (Exp);
4680 end Duplicate_Subexpr;
4682 ---------------------------------
4683 -- Duplicate_Subexpr_No_Checks --
4684 ---------------------------------
4686 function Duplicate_Subexpr_No_Checks
4687 (Exp : Node_Id;
4688 Name_Req : Boolean := False;
4689 Renaming_Req : Boolean := False;
4690 Related_Id : Entity_Id := Empty;
4691 Is_Low_Bound : Boolean := False;
4692 Is_High_Bound : Boolean := False) return Node_Id
4694 New_Exp : Node_Id;
4696 begin
4697 Remove_Side_Effects
4698 (Exp => Exp,
4699 Name_Req => Name_Req,
4700 Renaming_Req => Renaming_Req,
4701 Related_Id => Related_Id,
4702 Is_Low_Bound => Is_Low_Bound,
4703 Is_High_Bound => Is_High_Bound);
4705 New_Exp := New_Copy_Tree (Exp);
4706 Remove_Checks (New_Exp);
4707 return New_Exp;
4708 end Duplicate_Subexpr_No_Checks;
4710 -----------------------------------
4711 -- Duplicate_Subexpr_Move_Checks --
4712 -----------------------------------
4714 function Duplicate_Subexpr_Move_Checks
4715 (Exp : Node_Id;
4716 Name_Req : Boolean := False;
4717 Renaming_Req : Boolean := False) return Node_Id
4719 New_Exp : Node_Id;
4721 begin
4722 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4723 New_Exp := New_Copy_Tree (Exp);
4724 Remove_Checks (Exp);
4725 return New_Exp;
4726 end Duplicate_Subexpr_Move_Checks;
4728 --------------------
4729 -- Ensure_Defined --
4730 --------------------
4732 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4733 IR : Node_Id;
4735 begin
4736 -- An itype reference must only be created if this is a local itype, so
4737 -- that gigi can elaborate it on the proper objstack.
4739 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4740 IR := Make_Itype_Reference (Sloc (N));
4741 Set_Itype (IR, Typ);
4742 Insert_Action (N, IR);
4743 end if;
4744 end Ensure_Defined;
4746 --------------------
4747 -- Entry_Names_OK --
4748 --------------------
4750 function Entry_Names_OK return Boolean is
4751 begin
4752 return
4753 not Restricted_Profile
4754 and then not Global_Discard_Names
4755 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4756 and then not Restriction_Active (No_Local_Allocators);
4757 end Entry_Names_OK;
4759 -------------------
4760 -- Evaluate_Name --
4761 -------------------
4763 procedure Evaluate_Name (Nam : Node_Id) is
4764 begin
4765 -- For an attribute reference or an indexed component, evaluate the
4766 -- prefix, which is itself a name, recursively, and then force the
4767 -- evaluation of all the subscripts (or attribute expressions).
4769 case Nkind (Nam) is
4770 when N_Attribute_Reference
4771 | N_Indexed_Component
4773 Evaluate_Name (Prefix (Nam));
4775 declare
4776 E : Node_Id;
4778 begin
4779 E := First (Expressions (Nam));
4780 while Present (E) loop
4781 Force_Evaluation (E);
4783 if Original_Node (E) /= E then
4784 Set_Do_Range_Check
4785 (E, Do_Range_Check (Original_Node (E)));
4786 end if;
4788 Next (E);
4789 end loop;
4790 end;
4792 -- For an explicit dereference, we simply force the evaluation of
4793 -- the name expression. The dereference provides a value that is the
4794 -- address for the renamed object, and it is precisely this value
4795 -- that we want to preserve.
4797 when N_Explicit_Dereference =>
4798 Force_Evaluation (Prefix (Nam));
4800 -- For a function call, we evaluate the call
4802 when N_Function_Call =>
4803 Force_Evaluation (Nam);
4805 -- For a qualified expression, we evaluate the underlying object
4806 -- name if any, otherwise we force the evaluation of the underlying
4807 -- expression.
4809 when N_Qualified_Expression =>
4810 if Is_Object_Reference (Expression (Nam)) then
4811 Evaluate_Name (Expression (Nam));
4812 else
4813 Force_Evaluation (Expression (Nam));
4814 end if;
4816 -- For a selected component, we simply evaluate the prefix
4818 when N_Selected_Component =>
4819 Evaluate_Name (Prefix (Nam));
4821 -- For a slice, we evaluate the prefix, as for the indexed component
4822 -- case and then, if there is a range present, either directly or as
4823 -- the constraint of a discrete subtype indication, we evaluate the
4824 -- two bounds of this range.
4826 when N_Slice =>
4827 Evaluate_Name (Prefix (Nam));
4828 Evaluate_Slice_Bounds (Nam);
4830 -- For a type conversion, the expression of the conversion must be
4831 -- the name of an object, and we simply need to evaluate this name.
4833 when N_Type_Conversion =>
4834 Evaluate_Name (Expression (Nam));
4836 -- The remaining cases are direct name, operator symbol and character
4837 -- literal. In all these cases, we do nothing, since we want to
4838 -- reevaluate each time the renamed object is used.
4840 when others =>
4841 null;
4842 end case;
4843 end Evaluate_Name;
4845 ---------------------------
4846 -- Evaluate_Slice_Bounds --
4847 ---------------------------
4849 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4850 DR : constant Node_Id := Discrete_Range (Slice);
4851 Constr : Node_Id;
4852 Rexpr : Node_Id;
4854 begin
4855 if Nkind (DR) = N_Range then
4856 Force_Evaluation (Low_Bound (DR));
4857 Force_Evaluation (High_Bound (DR));
4859 elsif Nkind (DR) = N_Subtype_Indication then
4860 Constr := Constraint (DR);
4862 if Nkind (Constr) = N_Range_Constraint then
4863 Rexpr := Range_Expression (Constr);
4865 Force_Evaluation (Low_Bound (Rexpr));
4866 Force_Evaluation (High_Bound (Rexpr));
4867 end if;
4868 end if;
4869 end Evaluate_Slice_Bounds;
4871 ---------------------
4872 -- Evolve_And_Then --
4873 ---------------------
4875 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4876 begin
4877 if No (Cond) then
4878 Cond := Cond1;
4879 else
4880 Cond :=
4881 Make_And_Then (Sloc (Cond1),
4882 Left_Opnd => Cond,
4883 Right_Opnd => Cond1);
4884 end if;
4885 end Evolve_And_Then;
4887 --------------------
4888 -- Evolve_Or_Else --
4889 --------------------
4891 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4892 begin
4893 if No (Cond) then
4894 Cond := Cond1;
4895 else
4896 Cond :=
4897 Make_Or_Else (Sloc (Cond1),
4898 Left_Opnd => Cond,
4899 Right_Opnd => Cond1);
4900 end if;
4901 end Evolve_Or_Else;
4903 -----------------------------------
4904 -- Exceptions_In_Finalization_OK --
4905 -----------------------------------
4907 function Exceptions_In_Finalization_OK return Boolean is
4908 begin
4909 return
4910 not (Restriction_Active (No_Exception_Handlers) or else
4911 Restriction_Active (No_Exception_Propagation) or else
4912 Restriction_Active (No_Exceptions));
4913 end Exceptions_In_Finalization_OK;
4915 -----------------------------------------
4916 -- Expand_Static_Predicates_In_Choices --
4917 -----------------------------------------
4919 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4920 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4922 Choices : constant List_Id := Discrete_Choices (N);
4924 Choice : Node_Id;
4925 Next_C : Node_Id;
4926 P : Node_Id;
4927 C : Node_Id;
4929 begin
4930 Choice := First (Choices);
4931 while Present (Choice) loop
4932 Next_C := Next (Choice);
4934 -- Check for name of subtype with static predicate
4936 if Is_Entity_Name (Choice)
4937 and then Is_Type (Entity (Choice))
4938 and then Has_Predicates (Entity (Choice))
4939 then
4940 -- Loop through entries in predicate list, converting to choices
4941 -- and inserting in the list before the current choice. Note that
4942 -- if the list is empty, corresponding to a False predicate, then
4943 -- no choices are inserted.
4945 P := First (Static_Discrete_Predicate (Entity (Choice)));
4946 while Present (P) loop
4948 -- If low bound and high bounds are equal, copy simple choice
4950 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4951 C := New_Copy (Low_Bound (P));
4953 -- Otherwise copy a range
4955 else
4956 C := New_Copy (P);
4957 end if;
4959 -- Change Sloc to referencing choice (rather than the Sloc of
4960 -- the predicate declaration element itself).
4962 Set_Sloc (C, Sloc (Choice));
4963 Insert_Before (Choice, C);
4964 Next (P);
4965 end loop;
4967 -- Delete the predicated entry
4969 Remove (Choice);
4970 end if;
4972 -- Move to next choice to check
4974 Choice := Next_C;
4975 end loop;
4976 end Expand_Static_Predicates_In_Choices;
4978 ------------------------------
4979 -- Expand_Subtype_From_Expr --
4980 ------------------------------
4982 -- This function is applicable for both static and dynamic allocation of
4983 -- objects which are constrained by an initial expression. Basically it
4984 -- transforms an unconstrained subtype indication into a constrained one.
4986 -- The expression may also be transformed in certain cases in order to
4987 -- avoid multiple evaluation. In the static allocation case, the general
4988 -- scheme is:
4990 -- Val : T := Expr;
4992 -- is transformed into
4994 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
4996 -- Here are the main cases :
4998 -- <if Expr is a Slice>
4999 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5001 -- <elsif Expr is a String Literal>
5002 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5004 -- <elsif Expr is Constrained>
5005 -- subtype T is Type_Of_Expr
5006 -- Val : T := Expr;
5008 -- <elsif Expr is an entity_name>
5009 -- Val : T (constraints taken from Expr) := Expr;
5011 -- <else>
5012 -- type Axxx is access all T;
5013 -- Rval : Axxx := Expr'ref;
5014 -- Val : T (constraints taken from Rval) := Rval.all;
5016 -- ??? note: when the Expression is allocated in the secondary stack
5017 -- we could use it directly instead of copying it by declaring
5018 -- Val : T (...) renames Rval.all
5020 procedure Expand_Subtype_From_Expr
5021 (N : Node_Id;
5022 Unc_Type : Entity_Id;
5023 Subtype_Indic : Node_Id;
5024 Exp : Node_Id;
5025 Related_Id : Entity_Id := Empty)
5027 Loc : constant Source_Ptr := Sloc (N);
5028 Exp_Typ : constant Entity_Id := Etype (Exp);
5029 T : Entity_Id;
5031 begin
5032 -- In general we cannot build the subtype if expansion is disabled,
5033 -- because internal entities may not have been defined. However, to
5034 -- avoid some cascaded errors, we try to continue when the expression is
5035 -- an array (or string), because it is safe to compute the bounds. It is
5036 -- in fact required to do so even in a generic context, because there
5037 -- may be constants that depend on the bounds of a string literal, both
5038 -- standard string types and more generally arrays of characters.
5040 -- In GNATprove mode, these extra subtypes are not needed
5042 if GNATprove_Mode then
5043 return;
5044 end if;
5046 if not Expander_Active
5047 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5048 then
5049 return;
5050 end if;
5052 if Nkind (Exp) = N_Slice then
5053 declare
5054 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5056 begin
5057 Rewrite (Subtype_Indic,
5058 Make_Subtype_Indication (Loc,
5059 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5060 Constraint =>
5061 Make_Index_Or_Discriminant_Constraint (Loc,
5062 Constraints => New_List
5063 (New_Occurrence_Of (Slice_Type, Loc)))));
5065 -- This subtype indication may be used later for constraint checks
5066 -- we better make sure that if a variable was used as a bound of
5067 -- of the original slice, its value is frozen.
5069 Evaluate_Slice_Bounds (Exp);
5070 end;
5072 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5073 Rewrite (Subtype_Indic,
5074 Make_Subtype_Indication (Loc,
5075 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5076 Constraint =>
5077 Make_Index_Or_Discriminant_Constraint (Loc,
5078 Constraints => New_List (
5079 Make_Literal_Range (Loc,
5080 Literal_Typ => Exp_Typ)))));
5082 -- If the type of the expression is an internally generated type it
5083 -- may not be necessary to create a new subtype. However there are two
5084 -- exceptions: references to the current instances, and aliased array
5085 -- object declarations for which the back end has to create a template.
5087 elsif Is_Constrained (Exp_Typ)
5088 and then not Is_Class_Wide_Type (Unc_Type)
5089 and then
5090 (Nkind (N) /= N_Object_Declaration
5091 or else not Is_Entity_Name (Expression (N))
5092 or else not Comes_From_Source (Entity (Expression (N)))
5093 or else not Is_Array_Type (Exp_Typ)
5094 or else not Aliased_Present (N))
5095 then
5096 if Is_Itype (Exp_Typ) then
5098 -- Within an initialization procedure, a selected component
5099 -- denotes a component of the enclosing record, and it appears as
5100 -- an actual in a call to its own initialization procedure. If
5101 -- this component depends on the outer discriminant, we must
5102 -- generate the proper actual subtype for it.
5104 if Nkind (Exp) = N_Selected_Component
5105 and then Within_Init_Proc
5106 then
5107 declare
5108 Decl : constant Node_Id :=
5109 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5110 begin
5111 if Present (Decl) then
5112 Insert_Action (N, Decl);
5113 T := Defining_Identifier (Decl);
5114 else
5115 T := Exp_Typ;
5116 end if;
5117 end;
5119 -- No need to generate a new subtype
5121 else
5122 T := Exp_Typ;
5123 end if;
5125 else
5126 T := Make_Temporary (Loc, 'T');
5128 Insert_Action (N,
5129 Make_Subtype_Declaration (Loc,
5130 Defining_Identifier => T,
5131 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5133 -- This type is marked as an itype even though it has an explicit
5134 -- declaration since otherwise Is_Generic_Actual_Type can get
5135 -- set, resulting in the generation of spurious errors. (See
5136 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5138 Set_Is_Itype (T);
5139 Set_Associated_Node_For_Itype (T, Exp);
5140 end if;
5142 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5144 -- Nothing needs to be done for private types with unknown discriminants
5145 -- if the underlying type is not an unconstrained composite type or it
5146 -- is an unchecked union.
5148 elsif Is_Private_Type (Unc_Type)
5149 and then Has_Unknown_Discriminants (Unc_Type)
5150 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5151 or else Is_Constrained (Underlying_Type (Unc_Type))
5152 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5153 then
5154 null;
5156 -- Case of derived type with unknown discriminants where the parent type
5157 -- also has unknown discriminants.
5159 elsif Is_Record_Type (Unc_Type)
5160 and then not Is_Class_Wide_Type (Unc_Type)
5161 and then Has_Unknown_Discriminants (Unc_Type)
5162 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5163 then
5164 -- Nothing to be done if no underlying record view available
5166 -- If this is a limited type derived from a type with unknown
5167 -- discriminants, do not expand either, so that subsequent expansion
5168 -- of the call can add build-in-place parameters to call.
5170 if No (Underlying_Record_View (Unc_Type))
5171 or else Is_Limited_Type (Unc_Type)
5172 then
5173 null;
5175 -- Otherwise use the Underlying_Record_View to create the proper
5176 -- constrained subtype for an object of a derived type with unknown
5177 -- discriminants.
5179 else
5180 Remove_Side_Effects (Exp);
5181 Rewrite (Subtype_Indic,
5182 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5183 end if;
5185 -- Renamings of class-wide interface types require no equivalent
5186 -- constrained type declarations because we only need to reference
5187 -- the tag component associated with the interface. The same is
5188 -- presumably true for class-wide types in general, so this test
5189 -- is broadened to include all class-wide renamings, which also
5190 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5191 -- (Is this really correct, or are there some cases of class-wide
5192 -- renamings that require action in this procedure???)
5194 elsif Present (N)
5195 and then Nkind (N) = N_Object_Renaming_Declaration
5196 and then Is_Class_Wide_Type (Unc_Type)
5197 then
5198 null;
5200 -- In Ada 95 nothing to be done if the type of the expression is limited
5201 -- because in this case the expression cannot be copied, and its use can
5202 -- only be by reference.
5204 -- In Ada 2005 the context can be an object declaration whose expression
5205 -- is a function that returns in place. If the nominal subtype has
5206 -- unknown discriminants, the call still provides constraints on the
5207 -- object, and we have to create an actual subtype from it.
5209 -- If the type is class-wide, the expression is dynamically tagged and
5210 -- we do not create an actual subtype either. Ditto for an interface.
5211 -- For now this applies only if the type is immutably limited, and the
5212 -- function being called is build-in-place. This will have to be revised
5213 -- when build-in-place functions are generalized to other types.
5215 elsif Is_Limited_View (Exp_Typ)
5216 and then
5217 (Is_Class_Wide_Type (Exp_Typ)
5218 or else Is_Interface (Exp_Typ)
5219 or else not Has_Unknown_Discriminants (Exp_Typ)
5220 or else not Is_Composite_Type (Unc_Type))
5221 then
5222 null;
5224 -- For limited objects initialized with build in place function calls,
5225 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5226 -- node in the expression initializing the object, which breaks the
5227 -- circuitry that detects and adds the additional arguments to the
5228 -- called function.
5230 elsif Is_Build_In_Place_Function_Call (Exp) then
5231 null;
5233 else
5234 Remove_Side_Effects (Exp);
5235 Rewrite (Subtype_Indic,
5236 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5237 end if;
5238 end Expand_Subtype_From_Expr;
5240 ---------------------------------------------
5241 -- Expression_Contains_Primitives_Calls_Of --
5242 ---------------------------------------------
5244 function Expression_Contains_Primitives_Calls_Of
5245 (Expr : Node_Id;
5246 Typ : Entity_Id) return Boolean
5248 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5250 Calls_OK : Boolean := False;
5251 -- This flag is set to True when expression Expr contains at least one
5252 -- call to a nondispatching primitive function of Typ.
5254 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5255 -- Search for nondispatching calls to primitive functions of type Typ
5257 ----------------------------
5258 -- Search_Primitive_Calls --
5259 ----------------------------
5261 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5262 Disp_Typ : Entity_Id;
5263 Subp : Entity_Id;
5265 begin
5266 -- Detect a function call that could denote a nondispatching
5267 -- primitive of the input type.
5269 if Nkind (N) = N_Function_Call
5270 and then Is_Entity_Name (Name (N))
5271 then
5272 Subp := Entity (Name (N));
5274 -- Do not consider function calls with a controlling argument, as
5275 -- those are always dispatching calls.
5277 if Is_Dispatching_Operation (Subp)
5278 and then No (Controlling_Argument (N))
5279 then
5280 Disp_Typ := Find_Dispatching_Type (Subp);
5282 -- To qualify as a suitable primitive, the dispatching type of
5283 -- the function must be the input type.
5285 if Present (Disp_Typ)
5286 and then Unique_Entity (Disp_Typ) = U_Typ
5287 then
5288 Calls_OK := True;
5290 -- There is no need to continue the traversal, as one such
5291 -- call suffices.
5293 return Abandon;
5294 end if;
5295 end if;
5296 end if;
5298 return OK;
5299 end Search_Primitive_Calls;
5301 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5303 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5305 begin
5306 Search_Calls (Expr);
5307 return Calls_OK;
5308 end Expression_Contains_Primitives_Calls_Of;
5310 ----------------------
5311 -- Finalize_Address --
5312 ----------------------
5314 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5315 Utyp : Entity_Id := Typ;
5317 begin
5318 -- Handle protected class-wide or task class-wide types
5320 if Is_Class_Wide_Type (Utyp) then
5321 if Is_Concurrent_Type (Root_Type (Utyp)) then
5322 Utyp := Root_Type (Utyp);
5324 elsif Is_Private_Type (Root_Type (Utyp))
5325 and then Present (Full_View (Root_Type (Utyp)))
5326 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5327 then
5328 Utyp := Full_View (Root_Type (Utyp));
5329 end if;
5330 end if;
5332 -- Handle private types
5334 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5335 Utyp := Full_View (Utyp);
5336 end if;
5338 -- Handle protected and task types
5340 if Is_Concurrent_Type (Utyp)
5341 and then Present (Corresponding_Record_Type (Utyp))
5342 then
5343 Utyp := Corresponding_Record_Type (Utyp);
5344 end if;
5346 Utyp := Underlying_Type (Base_Type (Utyp));
5348 -- Deal with untagged derivation of private views. If the parent is
5349 -- now known to be protected, the finalization routine is the one
5350 -- defined on the corresponding record of the ancestor (corresponding
5351 -- records do not automatically inherit operations, but maybe they
5352 -- should???)
5354 if Is_Untagged_Derivation (Typ) then
5355 if Is_Protected_Type (Typ) then
5356 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5358 else
5359 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5361 if Is_Protected_Type (Utyp) then
5362 Utyp := Corresponding_Record_Type (Utyp);
5363 end if;
5364 end if;
5365 end if;
5367 -- If the underlying_type is a subtype, we are dealing with the
5368 -- completion of a private type. We need to access the base type and
5369 -- generate a conversion to it.
5371 if Utyp /= Base_Type (Utyp) then
5372 pragma Assert (Is_Private_Type (Typ));
5374 Utyp := Base_Type (Utyp);
5375 end if;
5377 -- When dealing with an internally built full view for a type with
5378 -- unknown discriminants, use the original record type.
5380 if Is_Underlying_Record_View (Utyp) then
5381 Utyp := Etype (Utyp);
5382 end if;
5384 return TSS (Utyp, TSS_Finalize_Address);
5385 end Finalize_Address;
5387 ------------------------
5388 -- Find_Interface_ADT --
5389 ------------------------
5391 function Find_Interface_ADT
5392 (T : Entity_Id;
5393 Iface : Entity_Id) return Elmt_Id
5395 ADT : Elmt_Id;
5396 Typ : Entity_Id := T;
5398 begin
5399 pragma Assert (Is_Interface (Iface));
5401 -- Handle private types
5403 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5404 Typ := Full_View (Typ);
5405 end if;
5407 -- Handle access types
5409 if Is_Access_Type (Typ) then
5410 Typ := Designated_Type (Typ);
5411 end if;
5413 -- Handle task and protected types implementing interfaces
5415 if Is_Concurrent_Type (Typ) then
5416 Typ := Corresponding_Record_Type (Typ);
5417 end if;
5419 pragma Assert
5420 (not Is_Class_Wide_Type (Typ)
5421 and then Ekind (Typ) /= E_Incomplete_Type);
5423 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5424 return First_Elmt (Access_Disp_Table (Typ));
5426 else
5427 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5428 while Present (ADT)
5429 and then Present (Related_Type (Node (ADT)))
5430 and then Related_Type (Node (ADT)) /= Iface
5431 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5432 Use_Full_View => True)
5433 loop
5434 Next_Elmt (ADT);
5435 end loop;
5437 pragma Assert (Present (Related_Type (Node (ADT))));
5438 return ADT;
5439 end if;
5440 end Find_Interface_ADT;
5442 ------------------------
5443 -- Find_Interface_Tag --
5444 ------------------------
5446 function Find_Interface_Tag
5447 (T : Entity_Id;
5448 Iface : Entity_Id) return Entity_Id
5450 AI_Tag : Entity_Id := Empty;
5451 Found : Boolean := False;
5452 Typ : Entity_Id := T;
5454 procedure Find_Tag (Typ : Entity_Id);
5455 -- Internal subprogram used to recursively climb to the ancestors
5457 --------------
5458 -- Find_Tag --
5459 --------------
5461 procedure Find_Tag (Typ : Entity_Id) is
5462 AI_Elmt : Elmt_Id;
5463 AI : Node_Id;
5465 begin
5466 -- This routine does not handle the case in which the interface is an
5467 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5469 pragma Assert (Typ /= Iface);
5471 -- Climb to the root type handling private types
5473 if Present (Full_View (Etype (Typ))) then
5474 if Full_View (Etype (Typ)) /= Typ then
5475 Find_Tag (Full_View (Etype (Typ)));
5476 end if;
5478 elsif Etype (Typ) /= Typ then
5479 Find_Tag (Etype (Typ));
5480 end if;
5482 -- Traverse the list of interfaces implemented by the type
5484 if not Found
5485 and then Present (Interfaces (Typ))
5486 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5487 then
5488 -- Skip the tag associated with the primary table
5490 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5491 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5492 pragma Assert (Present (AI_Tag));
5494 AI_Elmt := First_Elmt (Interfaces (Typ));
5495 while Present (AI_Elmt) loop
5496 AI := Node (AI_Elmt);
5498 if AI = Iface
5499 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5500 then
5501 Found := True;
5502 return;
5503 end if;
5505 AI_Tag := Next_Tag_Component (AI_Tag);
5506 Next_Elmt (AI_Elmt);
5507 end loop;
5508 end if;
5509 end Find_Tag;
5511 -- Start of processing for Find_Interface_Tag
5513 begin
5514 pragma Assert (Is_Interface (Iface));
5516 -- Handle access types
5518 if Is_Access_Type (Typ) then
5519 Typ := Designated_Type (Typ);
5520 end if;
5522 -- Handle class-wide types
5524 if Is_Class_Wide_Type (Typ) then
5525 Typ := Root_Type (Typ);
5526 end if;
5528 -- Handle private types
5530 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5531 Typ := Full_View (Typ);
5532 end if;
5534 -- Handle entities from the limited view
5536 if Ekind (Typ) = E_Incomplete_Type then
5537 pragma Assert (Present (Non_Limited_View (Typ)));
5538 Typ := Non_Limited_View (Typ);
5539 end if;
5541 -- Handle task and protected types implementing interfaces
5543 if Is_Concurrent_Type (Typ) then
5544 Typ := Corresponding_Record_Type (Typ);
5545 end if;
5547 -- If the interface is an ancestor of the type, then it shared the
5548 -- primary dispatch table.
5550 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5551 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5552 return First_Tag_Component (Typ);
5554 -- Otherwise we need to search for its associated tag component
5556 else
5557 Find_Tag (Typ);
5558 pragma Assert (Found);
5559 return AI_Tag;
5560 end if;
5561 end Find_Interface_Tag;
5563 ---------------------------
5564 -- Find_Optional_Prim_Op --
5565 ---------------------------
5567 function Find_Optional_Prim_Op
5568 (T : Entity_Id; Name : Name_Id) return Entity_Id
5570 Prim : Elmt_Id;
5571 Typ : Entity_Id := T;
5572 Op : Entity_Id;
5574 begin
5575 if Is_Class_Wide_Type (Typ) then
5576 Typ := Root_Type (Typ);
5577 end if;
5579 Typ := Underlying_Type (Typ);
5581 -- Loop through primitive operations
5583 Prim := First_Elmt (Primitive_Operations (Typ));
5584 while Present (Prim) loop
5585 Op := Node (Prim);
5587 -- We can retrieve primitive operations by name if it is an internal
5588 -- name. For equality we must check that both of its operands have
5589 -- the same type, to avoid confusion with user-defined equalities
5590 -- than may have a non-symmetric signature.
5592 exit when Chars (Op) = Name
5593 and then
5594 (Name /= Name_Op_Eq
5595 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5597 Next_Elmt (Prim);
5598 end loop;
5600 return Node (Prim); -- Empty if not found
5601 end Find_Optional_Prim_Op;
5603 ---------------------------
5604 -- Find_Optional_Prim_Op --
5605 ---------------------------
5607 function Find_Optional_Prim_Op
5608 (T : Entity_Id;
5609 Name : TSS_Name_Type) return Entity_Id
5611 Inher_Op : Entity_Id := Empty;
5612 Own_Op : Entity_Id := Empty;
5613 Prim_Elmt : Elmt_Id;
5614 Prim_Id : Entity_Id;
5615 Typ : Entity_Id := T;
5617 begin
5618 if Is_Class_Wide_Type (Typ) then
5619 Typ := Root_Type (Typ);
5620 end if;
5622 Typ := Underlying_Type (Typ);
5624 -- This search is based on the assertion that the dispatching version
5625 -- of the TSS routine always precedes the real primitive.
5627 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5628 while Present (Prim_Elmt) loop
5629 Prim_Id := Node (Prim_Elmt);
5631 if Is_TSS (Prim_Id, Name) then
5632 if Present (Alias (Prim_Id)) then
5633 Inher_Op := Prim_Id;
5634 else
5635 Own_Op := Prim_Id;
5636 end if;
5637 end if;
5639 Next_Elmt (Prim_Elmt);
5640 end loop;
5642 if Present (Own_Op) then
5643 return Own_Op;
5644 elsif Present (Inher_Op) then
5645 return Inher_Op;
5646 else
5647 return Empty;
5648 end if;
5649 end Find_Optional_Prim_Op;
5651 ------------------
5652 -- Find_Prim_Op --
5653 ------------------
5655 function Find_Prim_Op
5656 (T : Entity_Id; Name : Name_Id) return Entity_Id
5658 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5659 begin
5660 if No (Result) then
5661 raise Program_Error;
5662 end if;
5664 return Result;
5665 end Find_Prim_Op;
5667 ------------------
5668 -- Find_Prim_Op --
5669 ------------------
5671 function Find_Prim_Op
5672 (T : Entity_Id;
5673 Name : TSS_Name_Type) return Entity_Id
5675 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5676 begin
5677 if No (Result) then
5678 raise Program_Error;
5679 end if;
5681 return Result;
5682 end Find_Prim_Op;
5684 ----------------------------
5685 -- Find_Protection_Object --
5686 ----------------------------
5688 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5689 S : Entity_Id;
5691 begin
5692 S := Scop;
5693 while Present (S) loop
5694 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5695 and then Present (Protection_Object (S))
5696 then
5697 return Protection_Object (S);
5698 end if;
5700 S := Scope (S);
5701 end loop;
5703 -- If we do not find a Protection object in the scope chain, then
5704 -- something has gone wrong, most likely the object was never created.
5706 raise Program_Error;
5707 end Find_Protection_Object;
5709 --------------------------
5710 -- Find_Protection_Type --
5711 --------------------------
5713 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5714 Comp : Entity_Id;
5715 Typ : Entity_Id := Conc_Typ;
5717 begin
5718 if Is_Concurrent_Type (Typ) then
5719 Typ := Corresponding_Record_Type (Typ);
5720 end if;
5722 -- Since restriction violations are not considered serious errors, the
5723 -- expander remains active, but may leave the corresponding record type
5724 -- malformed. In such cases, component _object is not available so do
5725 -- not look for it.
5727 if not Analyzed (Typ) then
5728 return Empty;
5729 end if;
5731 Comp := First_Component (Typ);
5732 while Present (Comp) loop
5733 if Chars (Comp) = Name_uObject then
5734 return Base_Type (Etype (Comp));
5735 end if;
5737 Next_Component (Comp);
5738 end loop;
5740 -- The corresponding record of a protected type should always have an
5741 -- _object field.
5743 raise Program_Error;
5744 end Find_Protection_Type;
5746 -----------------------
5747 -- Find_Hook_Context --
5748 -----------------------
5750 function Find_Hook_Context (N : Node_Id) return Node_Id is
5751 Par : Node_Id;
5752 Top : Node_Id;
5754 Wrapped_Node : Node_Id;
5755 -- Note: if we are in a transient scope, we want to reuse it as
5756 -- the context for actions insertion, if possible. But if N is itself
5757 -- part of the stored actions for the current transient scope,
5758 -- then we need to insert at the appropriate (inner) location in
5759 -- the not as an action on Node_To_Be_Wrapped.
5761 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5763 begin
5764 -- When the node is inside a case/if expression, the lifetime of any
5765 -- temporary controlled object is extended. Find a suitable insertion
5766 -- node by locating the topmost case or if expressions.
5768 if In_Cond_Expr then
5769 Par := N;
5770 Top := N;
5771 while Present (Par) loop
5772 if Nkind_In (Original_Node (Par), N_Case_Expression,
5773 N_If_Expression)
5774 then
5775 Top := Par;
5777 -- Prevent the search from going too far
5779 elsif Is_Body_Or_Package_Declaration (Par) then
5780 exit;
5781 end if;
5783 Par := Parent (Par);
5784 end loop;
5786 -- The topmost case or if expression is now recovered, but it may
5787 -- still not be the correct place to add generated code. Climb to
5788 -- find a parent that is part of a declarative or statement list,
5789 -- and is not a list of actuals in a call.
5791 Par := Top;
5792 while Present (Par) loop
5793 if Is_List_Member (Par)
5794 and then not Nkind_In (Par, N_Component_Association,
5795 N_Discriminant_Association,
5796 N_Parameter_Association,
5797 N_Pragma_Argument_Association)
5798 and then not Nkind_In (Parent (Par), N_Function_Call,
5799 N_Procedure_Call_Statement,
5800 N_Entry_Call_Statement)
5802 then
5803 return Par;
5805 -- Prevent the search from going too far
5807 elsif Is_Body_Or_Package_Declaration (Par) then
5808 exit;
5809 end if;
5811 Par := Parent (Par);
5812 end loop;
5814 return Par;
5816 else
5817 Par := N;
5818 while Present (Par) loop
5820 -- Keep climbing past various operators
5822 if Nkind (Parent (Par)) in N_Op
5823 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5824 then
5825 Par := Parent (Par);
5826 else
5827 exit;
5828 end if;
5829 end loop;
5831 Top := Par;
5833 -- The node may be located in a pragma in which case return the
5834 -- pragma itself:
5836 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5838 -- Similar case occurs when the node is related to an object
5839 -- declaration or assignment:
5841 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5843 -- Another case to consider is when the node is part of a return
5844 -- statement:
5846 -- return ... and then Ctrl_Func_Call ...;
5848 -- Another case is when the node acts as a formal in a procedure
5849 -- call statement:
5851 -- Proc (... and then Ctrl_Func_Call ...);
5853 if Scope_Is_Transient then
5854 Wrapped_Node := Node_To_Be_Wrapped;
5855 else
5856 Wrapped_Node := Empty;
5857 end if;
5859 while Present (Par) loop
5860 if Par = Wrapped_Node
5861 or else Nkind_In (Par, N_Assignment_Statement,
5862 N_Object_Declaration,
5863 N_Pragma,
5864 N_Procedure_Call_Statement,
5865 N_Simple_Return_Statement)
5866 then
5867 return Par;
5869 -- Prevent the search from going too far
5871 elsif Is_Body_Or_Package_Declaration (Par) then
5872 exit;
5873 end if;
5875 Par := Parent (Par);
5876 end loop;
5878 -- Return the topmost short circuit operator
5880 return Top;
5881 end if;
5882 end Find_Hook_Context;
5884 ------------------------------
5885 -- Following_Address_Clause --
5886 ------------------------------
5888 function Following_Address_Clause (D : Node_Id) return Node_Id is
5889 Id : constant Entity_Id := Defining_Identifier (D);
5890 Result : Node_Id;
5891 Par : Node_Id;
5893 function Check_Decls (D : Node_Id) return Node_Id;
5894 -- This internal function differs from the main function in that it
5895 -- gets called to deal with a following package private part, and
5896 -- it checks declarations starting with D (the main function checks
5897 -- declarations following D). If D is Empty, then Empty is returned.
5899 -----------------
5900 -- Check_Decls --
5901 -----------------
5903 function Check_Decls (D : Node_Id) return Node_Id is
5904 Decl : Node_Id;
5906 begin
5907 Decl := D;
5908 while Present (Decl) loop
5909 if Nkind (Decl) = N_At_Clause
5910 and then Chars (Identifier (Decl)) = Chars (Id)
5911 then
5912 return Decl;
5914 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5915 and then Chars (Decl) = Name_Address
5916 and then Chars (Name (Decl)) = Chars (Id)
5917 then
5918 return Decl;
5919 end if;
5921 Next (Decl);
5922 end loop;
5924 -- Otherwise not found, return Empty
5926 return Empty;
5927 end Check_Decls;
5929 -- Start of processing for Following_Address_Clause
5931 begin
5932 -- If parser detected no address clause for the identifier in question,
5933 -- then the answer is a quick NO, without the need for a search.
5935 if not Get_Name_Table_Boolean1 (Chars (Id)) then
5936 return Empty;
5937 end if;
5939 -- Otherwise search current declarative unit
5941 Result := Check_Decls (Next (D));
5943 if Present (Result) then
5944 return Result;
5945 end if;
5947 -- Check for possible package private part following
5949 Par := Parent (D);
5951 if Nkind (Par) = N_Package_Specification
5952 and then Visible_Declarations (Par) = List_Containing (D)
5953 and then Present (Private_Declarations (Par))
5954 then
5955 -- Private part present, check declarations there
5957 return Check_Decls (First (Private_Declarations (Par)));
5959 else
5960 -- No private part, clause not found, return Empty
5962 return Empty;
5963 end if;
5964 end Following_Address_Clause;
5966 ----------------------
5967 -- Force_Evaluation --
5968 ----------------------
5970 procedure Force_Evaluation
5971 (Exp : Node_Id;
5972 Name_Req : Boolean := False;
5973 Related_Id : Entity_Id := Empty;
5974 Is_Low_Bound : Boolean := False;
5975 Is_High_Bound : Boolean := False;
5976 Mode : Force_Evaluation_Mode := Relaxed)
5978 begin
5979 Remove_Side_Effects
5980 (Exp => Exp,
5981 Name_Req => Name_Req,
5982 Variable_Ref => True,
5983 Renaming_Req => False,
5984 Related_Id => Related_Id,
5985 Is_Low_Bound => Is_Low_Bound,
5986 Is_High_Bound => Is_High_Bound,
5987 Check_Side_Effects =>
5988 Is_Static_Expression (Exp)
5989 or else Mode = Relaxed);
5990 end Force_Evaluation;
5992 ---------------------------------
5993 -- Fully_Qualified_Name_String --
5994 ---------------------------------
5996 function Fully_Qualified_Name_String
5997 (E : Entity_Id;
5998 Append_NUL : Boolean := True) return String_Id
6000 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6001 -- Compute recursively the qualified name without NUL at the end, adding
6002 -- it to the currently started string being generated
6004 ----------------------------------
6005 -- Internal_Full_Qualified_Name --
6006 ----------------------------------
6008 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6009 Ent : Entity_Id;
6011 begin
6012 -- Deal properly with child units
6014 if Nkind (E) = N_Defining_Program_Unit_Name then
6015 Ent := Defining_Identifier (E);
6016 else
6017 Ent := E;
6018 end if;
6020 -- Compute qualification recursively (only "Standard" has no scope)
6022 if Present (Scope (Scope (Ent))) then
6023 Internal_Full_Qualified_Name (Scope (Ent));
6024 Store_String_Char (Get_Char_Code ('.'));
6025 end if;
6027 -- Every entity should have a name except some expanded blocks
6028 -- don't bother about those.
6030 if Chars (Ent) = No_Name then
6031 return;
6032 end if;
6034 -- Generates the entity name in upper case
6036 Get_Decoded_Name_String (Chars (Ent));
6037 Set_All_Upper_Case;
6038 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6039 return;
6040 end Internal_Full_Qualified_Name;
6042 -- Start of processing for Full_Qualified_Name
6044 begin
6045 Start_String;
6046 Internal_Full_Qualified_Name (E);
6048 if Append_NUL then
6049 Store_String_Char (Get_Char_Code (ASCII.NUL));
6050 end if;
6052 return End_String;
6053 end Fully_Qualified_Name_String;
6055 ------------------------
6056 -- Generate_Poll_Call --
6057 ------------------------
6059 procedure Generate_Poll_Call (N : Node_Id) is
6060 begin
6061 -- No poll call if polling not active
6063 if not Polling_Required then
6064 return;
6066 -- Otherwise generate require poll call
6068 else
6069 Insert_Before_And_Analyze (N,
6070 Make_Procedure_Call_Statement (Sloc (N),
6071 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6072 end if;
6073 end Generate_Poll_Call;
6075 ---------------------------------
6076 -- Get_Current_Value_Condition --
6077 ---------------------------------
6079 -- Note: the implementation of this procedure is very closely tied to the
6080 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6081 -- interpret Current_Value fields set by the Set procedure, so the two
6082 -- procedures need to be closely coordinated.
6084 procedure Get_Current_Value_Condition
6085 (Var : Node_Id;
6086 Op : out Node_Kind;
6087 Val : out Node_Id)
6089 Loc : constant Source_Ptr := Sloc (Var);
6090 Ent : constant Entity_Id := Entity (Var);
6092 procedure Process_Current_Value_Condition
6093 (N : Node_Id;
6094 S : Boolean);
6095 -- N is an expression which holds either True (S = True) or False (S =
6096 -- False) in the condition. This procedure digs out the expression and
6097 -- if it refers to Ent, sets Op and Val appropriately.
6099 -------------------------------------
6100 -- Process_Current_Value_Condition --
6101 -------------------------------------
6103 procedure Process_Current_Value_Condition
6104 (N : Node_Id;
6105 S : Boolean)
6107 Cond : Node_Id;
6108 Prev_Cond : Node_Id;
6109 Sens : Boolean;
6111 begin
6112 Cond := N;
6113 Sens := S;
6115 loop
6116 Prev_Cond := Cond;
6118 -- Deal with NOT operators, inverting sense
6120 while Nkind (Cond) = N_Op_Not loop
6121 Cond := Right_Opnd (Cond);
6122 Sens := not Sens;
6123 end loop;
6125 -- Deal with conversions, qualifications, and expressions with
6126 -- actions.
6128 while Nkind_In (Cond,
6129 N_Type_Conversion,
6130 N_Qualified_Expression,
6131 N_Expression_With_Actions)
6132 loop
6133 Cond := Expression (Cond);
6134 end loop;
6136 exit when Cond = Prev_Cond;
6137 end loop;
6139 -- Deal with AND THEN and AND cases
6141 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6143 -- Don't ever try to invert a condition that is of the form of an
6144 -- AND or AND THEN (since we are not doing sufficiently general
6145 -- processing to allow this).
6147 if Sens = False then
6148 Op := N_Empty;
6149 Val := Empty;
6150 return;
6151 end if;
6153 -- Recursively process AND and AND THEN branches
6155 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6157 if Op /= N_Empty then
6158 return;
6159 end if;
6161 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6162 return;
6164 -- Case of relational operator
6166 elsif Nkind (Cond) in N_Op_Compare then
6167 Op := Nkind (Cond);
6169 -- Invert sense of test if inverted test
6171 if Sens = False then
6172 case Op is
6173 when N_Op_Eq => Op := N_Op_Ne;
6174 when N_Op_Ne => Op := N_Op_Eq;
6175 when N_Op_Lt => Op := N_Op_Ge;
6176 when N_Op_Gt => Op := N_Op_Le;
6177 when N_Op_Le => Op := N_Op_Gt;
6178 when N_Op_Ge => Op := N_Op_Lt;
6179 when others => raise Program_Error;
6180 end case;
6181 end if;
6183 -- Case of entity op value
6185 if Is_Entity_Name (Left_Opnd (Cond))
6186 and then Ent = Entity (Left_Opnd (Cond))
6187 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6188 then
6189 Val := Right_Opnd (Cond);
6191 -- Case of value op entity
6193 elsif Is_Entity_Name (Right_Opnd (Cond))
6194 and then Ent = Entity (Right_Opnd (Cond))
6195 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6196 then
6197 Val := Left_Opnd (Cond);
6199 -- We are effectively swapping operands
6201 case Op is
6202 when N_Op_Eq => null;
6203 when N_Op_Ne => null;
6204 when N_Op_Lt => Op := N_Op_Gt;
6205 when N_Op_Gt => Op := N_Op_Lt;
6206 when N_Op_Le => Op := N_Op_Ge;
6207 when N_Op_Ge => Op := N_Op_Le;
6208 when others => raise Program_Error;
6209 end case;
6211 else
6212 Op := N_Empty;
6213 end if;
6215 return;
6217 elsif Nkind_In (Cond,
6218 N_Type_Conversion,
6219 N_Qualified_Expression,
6220 N_Expression_With_Actions)
6221 then
6222 Cond := Expression (Cond);
6224 -- Case of Boolean variable reference, return as though the
6225 -- reference had said var = True.
6227 else
6228 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6229 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6231 if Sens = False then
6232 Op := N_Op_Ne;
6233 else
6234 Op := N_Op_Eq;
6235 end if;
6236 end if;
6237 end if;
6238 end Process_Current_Value_Condition;
6240 -- Start of processing for Get_Current_Value_Condition
6242 begin
6243 Op := N_Empty;
6244 Val := Empty;
6246 -- Immediate return, nothing doing, if this is not an object
6248 if Ekind (Ent) not in Object_Kind then
6249 return;
6250 end if;
6252 -- Otherwise examine current value
6254 declare
6255 CV : constant Node_Id := Current_Value (Ent);
6256 Sens : Boolean;
6257 Stm : Node_Id;
6259 begin
6260 -- If statement. Condition is known true in THEN section, known False
6261 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6263 if Nkind (CV) = N_If_Statement then
6265 -- Before start of IF statement
6267 if Loc < Sloc (CV) then
6268 return;
6270 -- After end of IF statement
6272 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6273 return;
6274 end if;
6276 -- At this stage we know that we are within the IF statement, but
6277 -- unfortunately, the tree does not record the SLOC of the ELSE so
6278 -- we cannot use a simple SLOC comparison to distinguish between
6279 -- the then/else statements, so we have to climb the tree.
6281 declare
6282 N : Node_Id;
6284 begin
6285 N := Parent (Var);
6286 while Parent (N) /= CV loop
6287 N := Parent (N);
6289 -- If we fall off the top of the tree, then that's odd, but
6290 -- perhaps it could occur in some error situation, and the
6291 -- safest response is simply to assume that the outcome of
6292 -- the condition is unknown. No point in bombing during an
6293 -- attempt to optimize things.
6295 if No (N) then
6296 return;
6297 end if;
6298 end loop;
6300 -- Now we have N pointing to a node whose parent is the IF
6301 -- statement in question, so now we can tell if we are within
6302 -- the THEN statements.
6304 if Is_List_Member (N)
6305 and then List_Containing (N) = Then_Statements (CV)
6306 then
6307 Sens := True;
6309 -- If the variable reference does not come from source, we
6310 -- cannot reliably tell whether it appears in the else part.
6311 -- In particular, if it appears in generated code for a node
6312 -- that requires finalization, it may be attached to a list
6313 -- that has not been yet inserted into the code. For now,
6314 -- treat it as unknown.
6316 elsif not Comes_From_Source (N) then
6317 return;
6319 -- Otherwise we must be in ELSIF or ELSE part
6321 else
6322 Sens := False;
6323 end if;
6324 end;
6326 -- ELSIF part. Condition is known true within the referenced
6327 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6328 -- and unknown before the ELSE part or after the IF statement.
6330 elsif Nkind (CV) = N_Elsif_Part then
6332 -- if the Elsif_Part had condition_actions, the elsif has been
6333 -- rewritten as a nested if, and the original elsif_part is
6334 -- detached from the tree, so there is no way to obtain useful
6335 -- information on the current value of the variable.
6336 -- Can this be improved ???
6338 if No (Parent (CV)) then
6339 return;
6340 end if;
6342 Stm := Parent (CV);
6344 -- If the tree has been otherwise rewritten there is nothing
6345 -- else to be done either.
6347 if Nkind (Stm) /= N_If_Statement then
6348 return;
6349 end if;
6351 -- Before start of ELSIF part
6353 if Loc < Sloc (CV) then
6354 return;
6356 -- After end of IF statement
6358 elsif Loc >= Sloc (Stm) +
6359 Text_Ptr (UI_To_Int (End_Span (Stm)))
6360 then
6361 return;
6362 end if;
6364 -- Again we lack the SLOC of the ELSE, so we need to climb the
6365 -- tree to see if we are within the ELSIF part in question.
6367 declare
6368 N : Node_Id;
6370 begin
6371 N := Parent (Var);
6372 while Parent (N) /= Stm loop
6373 N := Parent (N);
6375 -- If we fall off the top of the tree, then that's odd, but
6376 -- perhaps it could occur in some error situation, and the
6377 -- safest response is simply to assume that the outcome of
6378 -- the condition is unknown. No point in bombing during an
6379 -- attempt to optimize things.
6381 if No (N) then
6382 return;
6383 end if;
6384 end loop;
6386 -- Now we have N pointing to a node whose parent is the IF
6387 -- statement in question, so see if is the ELSIF part we want.
6388 -- the THEN statements.
6390 if N = CV then
6391 Sens := True;
6393 -- Otherwise we must be in subsequent ELSIF or ELSE part
6395 else
6396 Sens := False;
6397 end if;
6398 end;
6400 -- Iteration scheme of while loop. The condition is known to be
6401 -- true within the body of the loop.
6403 elsif Nkind (CV) = N_Iteration_Scheme then
6404 declare
6405 Loop_Stmt : constant Node_Id := Parent (CV);
6407 begin
6408 -- Before start of body of loop
6410 if Loc < Sloc (Loop_Stmt) then
6411 return;
6413 -- After end of LOOP statement
6415 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6416 return;
6418 -- We are within the body of the loop
6420 else
6421 Sens := True;
6422 end if;
6423 end;
6425 -- All other cases of Current_Value settings
6427 else
6428 return;
6429 end if;
6431 -- If we fall through here, then we have a reportable condition, Sens
6432 -- is True if the condition is true and False if it needs inverting.
6434 Process_Current_Value_Condition (Condition (CV), Sens);
6435 end;
6436 end Get_Current_Value_Condition;
6438 ---------------------
6439 -- Get_Stream_Size --
6440 ---------------------
6442 function Get_Stream_Size (E : Entity_Id) return Uint is
6443 begin
6444 -- If we have a Stream_Size clause for this type use it
6446 if Has_Stream_Size_Clause (E) then
6447 return Static_Integer (Expression (Stream_Size_Clause (E)));
6449 -- Otherwise the Stream_Size if the size of the type
6451 else
6452 return Esize (E);
6453 end if;
6454 end Get_Stream_Size;
6456 ---------------------------
6457 -- Has_Access_Constraint --
6458 ---------------------------
6460 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6461 Disc : Entity_Id;
6462 T : constant Entity_Id := Etype (E);
6464 begin
6465 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6466 Disc := First_Discriminant (T);
6467 while Present (Disc) loop
6468 if Is_Access_Type (Etype (Disc)) then
6469 return True;
6470 end if;
6472 Next_Discriminant (Disc);
6473 end loop;
6475 return False;
6476 else
6477 return False;
6478 end if;
6479 end Has_Access_Constraint;
6481 -----------------------------------------------------
6482 -- Has_Annotate_Pragma_For_External_Axiomatization --
6483 -----------------------------------------------------
6485 function Has_Annotate_Pragma_For_External_Axiomatization
6486 (E : Entity_Id) return Boolean
6488 function Is_Annotate_Pragma_For_External_Axiomatization
6489 (N : Node_Id) return Boolean;
6490 -- Returns whether N is
6491 -- pragma Annotate (GNATprove, External_Axiomatization);
6493 ----------------------------------------------------
6494 -- Is_Annotate_Pragma_For_External_Axiomatization --
6495 ----------------------------------------------------
6497 -- The general form of pragma Annotate is
6499 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6500 -- ARG ::= NAME | EXPRESSION
6502 -- The first two arguments are by convention intended to refer to an
6503 -- external tool and a tool-specific function. These arguments are
6504 -- not analyzed.
6506 -- The following is used to annotate a package specification which
6507 -- GNATprove should treat specially, because the axiomatization of
6508 -- this unit is given by the user instead of being automatically
6509 -- generated.
6511 -- pragma Annotate (GNATprove, External_Axiomatization);
6513 function Is_Annotate_Pragma_For_External_Axiomatization
6514 (N : Node_Id) return Boolean
6516 Name_GNATprove : constant String :=
6517 "gnatprove";
6518 Name_External_Axiomatization : constant String :=
6519 "external_axiomatization";
6520 -- Special names
6522 begin
6523 if Nkind (N) = N_Pragma
6524 and then Get_Pragma_Id (N) = Pragma_Annotate
6525 and then List_Length (Pragma_Argument_Associations (N)) = 2
6526 then
6527 declare
6528 Arg1 : constant Node_Id :=
6529 First (Pragma_Argument_Associations (N));
6530 Arg2 : constant Node_Id := Next (Arg1);
6531 Nam1 : Name_Id;
6532 Nam2 : Name_Id;
6534 begin
6535 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6536 -- Name_External_Axiomatization so that Name_Find returns the
6537 -- corresponding name. This takes care of all possible casings.
6539 Name_Len := 0;
6540 Add_Str_To_Name_Buffer (Name_GNATprove);
6541 Nam1 := Name_Find;
6543 Name_Len := 0;
6544 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6545 Nam2 := Name_Find;
6547 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6548 and then
6549 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6550 end;
6552 else
6553 return False;
6554 end if;
6555 end Is_Annotate_Pragma_For_External_Axiomatization;
6557 -- Local variables
6559 Decl : Node_Id;
6560 Vis_Decls : List_Id;
6561 N : Node_Id;
6563 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6565 begin
6566 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6567 Decl := Parent (Parent (E));
6568 else
6569 Decl := Parent (E);
6570 end if;
6572 Vis_Decls := Visible_Declarations (Decl);
6574 N := First (Vis_Decls);
6575 while Present (N) loop
6577 -- Skip declarations generated by the frontend. Skip all pragmas
6578 -- that are not the desired Annotate pragma. Stop the search on
6579 -- the first non-pragma source declaration.
6581 if Comes_From_Source (N) then
6582 if Nkind (N) = N_Pragma then
6583 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6584 return True;
6585 end if;
6586 else
6587 return False;
6588 end if;
6589 end if;
6591 Next (N);
6592 end loop;
6594 return False;
6595 end Has_Annotate_Pragma_For_External_Axiomatization;
6597 --------------------
6598 -- Homonym_Number --
6599 --------------------
6601 function Homonym_Number (Subp : Entity_Id) return Nat is
6602 Count : Nat;
6603 Hom : Entity_Id;
6605 begin
6606 Count := 1;
6607 Hom := Homonym (Subp);
6608 while Present (Hom) loop
6609 if Scope (Hom) = Scope (Subp) then
6610 Count := Count + 1;
6611 end if;
6613 Hom := Homonym (Hom);
6614 end loop;
6616 return Count;
6617 end Homonym_Number;
6619 -----------------------------------
6620 -- In_Library_Level_Package_Body --
6621 -----------------------------------
6623 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6624 begin
6625 -- First determine whether the entity appears at the library level, then
6626 -- look at the containing unit.
6628 if Is_Library_Level_Entity (Id) then
6629 declare
6630 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6632 begin
6633 return Nkind (Unit (Container)) = N_Package_Body;
6634 end;
6635 end if;
6637 return False;
6638 end In_Library_Level_Package_Body;
6640 ------------------------------
6641 -- In_Unconditional_Context --
6642 ------------------------------
6644 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6645 P : Node_Id;
6647 begin
6648 P := Node;
6649 while Present (P) loop
6650 case Nkind (P) is
6651 when N_Subprogram_Body => return True;
6652 when N_If_Statement => return False;
6653 when N_Loop_Statement => return False;
6654 when N_Case_Statement => return False;
6655 when others => P := Parent (P);
6656 end case;
6657 end loop;
6659 return False;
6660 end In_Unconditional_Context;
6662 -------------------
6663 -- Insert_Action --
6664 -------------------
6666 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6667 begin
6668 if Present (Ins_Action) then
6669 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6670 end if;
6671 end Insert_Action;
6673 -- Version with check(s) suppressed
6675 procedure Insert_Action
6676 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6678 begin
6679 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6680 end Insert_Action;
6682 -------------------------
6683 -- Insert_Action_After --
6684 -------------------------
6686 procedure Insert_Action_After
6687 (Assoc_Node : Node_Id;
6688 Ins_Action : Node_Id)
6690 begin
6691 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6692 end Insert_Action_After;
6694 --------------------
6695 -- Insert_Actions --
6696 --------------------
6698 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6699 N : Node_Id;
6700 P : Node_Id;
6702 Wrapped_Node : Node_Id := Empty;
6704 begin
6705 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6706 return;
6707 end if;
6709 -- Ignore insert of actions from inside default expression (or other
6710 -- similar "spec expression") in the special spec-expression analyze
6711 -- mode. Any insertions at this point have no relevance, since we are
6712 -- only doing the analyze to freeze the types of any static expressions.
6713 -- See section "Handling of Default Expressions" in the spec of package
6714 -- Sem for further details.
6716 if In_Spec_Expression then
6717 return;
6718 end if;
6720 -- If the action derives from stuff inside a record, then the actions
6721 -- are attached to the current scope, to be inserted and analyzed on
6722 -- exit from the scope. The reason for this is that we may also be
6723 -- generating freeze actions at the same time, and they must eventually
6724 -- be elaborated in the correct order.
6726 if Is_Record_Type (Current_Scope)
6727 and then not Is_Frozen (Current_Scope)
6728 then
6729 if No (Scope_Stack.Table
6730 (Scope_Stack.Last).Pending_Freeze_Actions)
6731 then
6732 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6733 Ins_Actions;
6734 else
6735 Append_List
6736 (Ins_Actions,
6737 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6738 end if;
6740 return;
6741 end if;
6743 -- We now intend to climb up the tree to find the right point to
6744 -- insert the actions. We start at Assoc_Node, unless this node is a
6745 -- subexpression in which case we start with its parent. We do this for
6746 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6747 -- itself one of the special nodes like N_And_Then, then we assume that
6748 -- an initial request to insert actions for such a node does not expect
6749 -- the actions to get deposited in the node for later handling when the
6750 -- node is expanded, since clearly the node is being dealt with by the
6751 -- caller. Note that in the subexpression case, N is always the child we
6752 -- came from.
6754 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6755 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6756 -- Procedure calls, and similarly procedure attribute references, are
6757 -- also statements.
6759 if Nkind (Assoc_Node) in N_Subexpr
6760 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6761 or else Etype (Assoc_Node) /= Standard_Void_Type)
6762 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6763 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6764 or else not Is_Procedure_Attribute_Name
6765 (Attribute_Name (Assoc_Node)))
6766 then
6767 N := Assoc_Node;
6768 P := Parent (Assoc_Node);
6770 -- Non-subexpression case. Note that N is initially Empty in this case
6771 -- (N is only guaranteed Non-Empty in the subexpr case).
6773 else
6774 N := Empty;
6775 P := Assoc_Node;
6776 end if;
6778 -- Capture root of the transient scope
6780 if Scope_Is_Transient then
6781 Wrapped_Node := Node_To_Be_Wrapped;
6782 end if;
6784 loop
6785 pragma Assert (Present (P));
6787 -- Make sure that inserted actions stay in the transient scope
6789 if Present (Wrapped_Node) and then N = Wrapped_Node then
6790 Store_Before_Actions_In_Scope (Ins_Actions);
6791 return;
6792 end if;
6794 case Nkind (P) is
6796 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6797 -- in the Actions field of the right operand. They will be moved
6798 -- out further when the AND THEN or OR ELSE operator is expanded.
6799 -- Nothing special needs to be done for the left operand since
6800 -- in that case the actions are executed unconditionally.
6802 when N_Short_Circuit =>
6803 if N = Right_Opnd (P) then
6805 -- We are now going to either append the actions to the
6806 -- actions field of the short-circuit operation. We will
6807 -- also analyze the actions now.
6809 -- This analysis is really too early, the proper thing would
6810 -- be to just park them there now, and only analyze them if
6811 -- we find we really need them, and to it at the proper
6812 -- final insertion point. However attempting to this proved
6813 -- tricky, so for now we just kill current values before and
6814 -- after the analyze call to make sure we avoid peculiar
6815 -- optimizations from this out of order insertion.
6817 Kill_Current_Values;
6819 -- If P has already been expanded, we can't park new actions
6820 -- on it, so we need to expand them immediately, introducing
6821 -- an Expression_With_Actions. N can't be an expression
6822 -- with actions, or else then the actions would have been
6823 -- inserted at an inner level.
6825 if Analyzed (P) then
6826 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6827 Rewrite (N,
6828 Make_Expression_With_Actions (Sloc (N),
6829 Actions => Ins_Actions,
6830 Expression => Relocate_Node (N)));
6831 Analyze_And_Resolve (N);
6833 elsif Present (Actions (P)) then
6834 Insert_List_After_And_Analyze
6835 (Last (Actions (P)), Ins_Actions);
6836 else
6837 Set_Actions (P, Ins_Actions);
6838 Analyze_List (Actions (P));
6839 end if;
6841 Kill_Current_Values;
6843 return;
6844 end if;
6846 -- Then or Else dependent expression of an if expression. Add
6847 -- actions to Then_Actions or Else_Actions field as appropriate.
6848 -- The actions will be moved further out when the if is expanded.
6850 when N_If_Expression =>
6851 declare
6852 ThenX : constant Node_Id := Next (First (Expressions (P)));
6853 ElseX : constant Node_Id := Next (ThenX);
6855 begin
6856 -- If the enclosing expression is already analyzed, as
6857 -- is the case for nested elaboration checks, insert the
6858 -- conditional further out.
6860 if Analyzed (P) then
6861 null;
6863 -- Actions belong to the then expression, temporarily place
6864 -- them as Then_Actions of the if expression. They will be
6865 -- moved to the proper place later when the if expression
6866 -- is expanded.
6868 elsif N = ThenX then
6869 if Present (Then_Actions (P)) then
6870 Insert_List_After_And_Analyze
6871 (Last (Then_Actions (P)), Ins_Actions);
6872 else
6873 Set_Then_Actions (P, Ins_Actions);
6874 Analyze_List (Then_Actions (P));
6875 end if;
6877 return;
6879 -- Actions belong to the else expression, temporarily place
6880 -- them as Else_Actions of the if expression. They will be
6881 -- moved to the proper place later when the if expression
6882 -- is expanded.
6884 elsif N = ElseX then
6885 if Present (Else_Actions (P)) then
6886 Insert_List_After_And_Analyze
6887 (Last (Else_Actions (P)), Ins_Actions);
6888 else
6889 Set_Else_Actions (P, Ins_Actions);
6890 Analyze_List (Else_Actions (P));
6891 end if;
6893 return;
6895 -- Actions belong to the condition. In this case they are
6896 -- unconditionally executed, and so we can continue the
6897 -- search for the proper insert point.
6899 else
6900 null;
6901 end if;
6902 end;
6904 -- Alternative of case expression, we place the action in the
6905 -- Actions field of the case expression alternative, this will
6906 -- be handled when the case expression is expanded.
6908 when N_Case_Expression_Alternative =>
6909 if Present (Actions (P)) then
6910 Insert_List_After_And_Analyze
6911 (Last (Actions (P)), Ins_Actions);
6912 else
6913 Set_Actions (P, Ins_Actions);
6914 Analyze_List (Actions (P));
6915 end if;
6917 return;
6919 -- Case of appearing within an Expressions_With_Actions node. When
6920 -- the new actions come from the expression of the expression with
6921 -- actions, they must be added to the existing actions. The other
6922 -- alternative is when the new actions are related to one of the
6923 -- existing actions of the expression with actions, and should
6924 -- never reach here: if actions are inserted on a statement
6925 -- within the Actions of an expression with actions, or on some
6926 -- subexpression of such a statement, then the outermost proper
6927 -- insertion point is right before the statement, and we should
6928 -- never climb up as far as the N_Expression_With_Actions itself.
6930 when N_Expression_With_Actions =>
6931 if N = Expression (P) then
6932 if Is_Empty_List (Actions (P)) then
6933 Append_List_To (Actions (P), Ins_Actions);
6934 Analyze_List (Actions (P));
6935 else
6936 Insert_List_After_And_Analyze
6937 (Last (Actions (P)), Ins_Actions);
6938 end if;
6940 return;
6942 else
6943 raise Program_Error;
6944 end if;
6946 -- Case of appearing in the condition of a while expression or
6947 -- elsif. We insert the actions into the Condition_Actions field.
6948 -- They will be moved further out when the while loop or elsif
6949 -- is analyzed.
6951 when N_Elsif_Part
6952 | N_Iteration_Scheme
6954 if N = Condition (P) then
6955 if Present (Condition_Actions (P)) then
6956 Insert_List_After_And_Analyze
6957 (Last (Condition_Actions (P)), Ins_Actions);
6958 else
6959 Set_Condition_Actions (P, Ins_Actions);
6961 -- Set the parent of the insert actions explicitly. This
6962 -- is not a syntactic field, but we need the parent field
6963 -- set, in particular so that freeze can understand that
6964 -- it is dealing with condition actions, and properly
6965 -- insert the freezing actions.
6967 Set_Parent (Ins_Actions, P);
6968 Analyze_List (Condition_Actions (P));
6969 end if;
6971 return;
6972 end if;
6974 -- Statements, declarations, pragmas, representation clauses
6976 when
6977 -- Statements
6979 N_Procedure_Call_Statement
6980 | N_Statement_Other_Than_Procedure_Call
6982 -- Pragmas
6984 | N_Pragma
6986 -- Representation_Clause
6988 | N_At_Clause
6989 | N_Attribute_Definition_Clause
6990 | N_Enumeration_Representation_Clause
6991 | N_Record_Representation_Clause
6993 -- Declarations
6995 | N_Abstract_Subprogram_Declaration
6996 | N_Entry_Body
6997 | N_Exception_Declaration
6998 | N_Exception_Renaming_Declaration
6999 | N_Expression_Function
7000 | N_Formal_Abstract_Subprogram_Declaration
7001 | N_Formal_Concrete_Subprogram_Declaration
7002 | N_Formal_Object_Declaration
7003 | N_Formal_Type_Declaration
7004 | N_Full_Type_Declaration
7005 | N_Function_Instantiation
7006 | N_Generic_Function_Renaming_Declaration
7007 | N_Generic_Package_Declaration
7008 | N_Generic_Package_Renaming_Declaration
7009 | N_Generic_Procedure_Renaming_Declaration
7010 | N_Generic_Subprogram_Declaration
7011 | N_Implicit_Label_Declaration
7012 | N_Incomplete_Type_Declaration
7013 | N_Number_Declaration
7014 | N_Object_Declaration
7015 | N_Object_Renaming_Declaration
7016 | N_Package_Body
7017 | N_Package_Body_Stub
7018 | N_Package_Declaration
7019 | N_Package_Instantiation
7020 | N_Package_Renaming_Declaration
7021 | N_Private_Extension_Declaration
7022 | N_Private_Type_Declaration
7023 | N_Procedure_Instantiation
7024 | N_Protected_Body
7025 | N_Protected_Body_Stub
7026 | N_Protected_Type_Declaration
7027 | N_Single_Task_Declaration
7028 | N_Subprogram_Body
7029 | N_Subprogram_Body_Stub
7030 | N_Subprogram_Declaration
7031 | N_Subprogram_Renaming_Declaration
7032 | N_Subtype_Declaration
7033 | N_Task_Body
7034 | N_Task_Body_Stub
7035 | N_Task_Type_Declaration
7037 -- Use clauses can appear in lists of declarations
7039 | N_Use_Package_Clause
7040 | N_Use_Type_Clause
7042 -- Freeze entity behaves like a declaration or statement
7044 | N_Freeze_Entity
7045 | N_Freeze_Generic_Entity
7047 -- Do not insert here if the item is not a list member (this
7048 -- happens for example with a triggering statement, and the
7049 -- proper approach is to insert before the entire select).
7051 if not Is_List_Member (P) then
7052 null;
7054 -- Do not insert if parent of P is an N_Component_Association
7055 -- node (i.e. we are in the context of an N_Aggregate or
7056 -- N_Extension_Aggregate node. In this case we want to insert
7057 -- before the entire aggregate.
7059 elsif Nkind (Parent (P)) = N_Component_Association then
7060 null;
7062 -- Do not insert if the parent of P is either an N_Variant node
7063 -- or an N_Record_Definition node, meaning in either case that
7064 -- P is a member of a component list, and that therefore the
7065 -- actions should be inserted outside the complete record
7066 -- declaration.
7068 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7069 null;
7071 -- Do not insert freeze nodes within the loop generated for
7072 -- an aggregate, because they may be elaborated too late for
7073 -- subsequent use in the back end: within a package spec the
7074 -- loop is part of the elaboration procedure and is only
7075 -- elaborated during the second pass.
7077 -- If the loop comes from source, or the entity is local to the
7078 -- loop itself it must remain within.
7080 elsif Nkind (Parent (P)) = N_Loop_Statement
7081 and then not Comes_From_Source (Parent (P))
7082 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7083 and then
7084 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7085 then
7086 null;
7088 -- Otherwise we can go ahead and do the insertion
7090 elsif P = Wrapped_Node then
7091 Store_Before_Actions_In_Scope (Ins_Actions);
7092 return;
7094 else
7095 Insert_List_Before_And_Analyze (P, Ins_Actions);
7096 return;
7097 end if;
7099 -- A special case, N_Raise_xxx_Error can act either as a statement
7100 -- or a subexpression. We tell the difference by looking at the
7101 -- Etype. It is set to Standard_Void_Type in the statement case.
7103 when N_Raise_xxx_Error =>
7104 if Etype (P) = Standard_Void_Type then
7105 if P = Wrapped_Node then
7106 Store_Before_Actions_In_Scope (Ins_Actions);
7107 else
7108 Insert_List_Before_And_Analyze (P, Ins_Actions);
7109 end if;
7111 return;
7113 -- In the subexpression case, keep climbing
7115 else
7116 null;
7117 end if;
7119 -- If a component association appears within a loop created for
7120 -- an array aggregate, attach the actions to the association so
7121 -- they can be subsequently inserted within the loop. For other
7122 -- component associations insert outside of the aggregate. For
7123 -- an association that will generate a loop, its Loop_Actions
7124 -- attribute is already initialized (see exp_aggr.adb).
7126 -- The list of Loop_Actions can in turn generate additional ones,
7127 -- that are inserted before the associated node. If the associated
7128 -- node is outside the aggregate, the new actions are collected
7129 -- at the end of the Loop_Actions, to respect the order in which
7130 -- they are to be elaborated.
7132 when N_Component_Association
7133 | N_Iterated_Component_Association
7135 if Nkind (Parent (P)) = N_Aggregate
7136 and then Present (Loop_Actions (P))
7137 then
7138 if Is_Empty_List (Loop_Actions (P)) then
7139 Set_Loop_Actions (P, Ins_Actions);
7140 Analyze_List (Ins_Actions);
7141 else
7142 declare
7143 Decl : Node_Id;
7145 begin
7146 -- Check whether these actions were generated by a
7147 -- declaration that is part of the Loop_Actions for
7148 -- the component_association.
7150 Decl := Assoc_Node;
7151 while Present (Decl) loop
7152 exit when Parent (Decl) = P
7153 and then Is_List_Member (Decl)
7154 and then
7155 List_Containing (Decl) = Loop_Actions (P);
7156 Decl := Parent (Decl);
7157 end loop;
7159 if Present (Decl) then
7160 Insert_List_Before_And_Analyze
7161 (Decl, Ins_Actions);
7162 else
7163 Insert_List_After_And_Analyze
7164 (Last (Loop_Actions (P)), Ins_Actions);
7165 end if;
7166 end;
7167 end if;
7169 return;
7171 else
7172 null;
7173 end if;
7175 -- Special case: an attribute denoting a procedure call
7177 when N_Attribute_Reference =>
7178 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7179 if P = Wrapped_Node then
7180 Store_Before_Actions_In_Scope (Ins_Actions);
7181 else
7182 Insert_List_Before_And_Analyze (P, Ins_Actions);
7183 end if;
7185 return;
7187 -- In the subexpression case, keep climbing
7189 else
7190 null;
7191 end if;
7193 -- Special case: a marker
7195 when N_Call_Marker
7196 | N_Variable_Reference_Marker
7198 if Is_List_Member (P) then
7199 Insert_List_Before_And_Analyze (P, Ins_Actions);
7200 return;
7201 end if;
7203 -- A contract node should not belong to the tree
7205 when N_Contract =>
7206 raise Program_Error;
7208 -- For all other node types, keep climbing tree
7210 when N_Abortable_Part
7211 | N_Accept_Alternative
7212 | N_Access_Definition
7213 | N_Access_Function_Definition
7214 | N_Access_Procedure_Definition
7215 | N_Access_To_Object_Definition
7216 | N_Aggregate
7217 | N_Allocator
7218 | N_Aspect_Specification
7219 | N_Case_Expression
7220 | N_Case_Statement_Alternative
7221 | N_Character_Literal
7222 | N_Compilation_Unit
7223 | N_Compilation_Unit_Aux
7224 | N_Component_Clause
7225 | N_Component_Declaration
7226 | N_Component_Definition
7227 | N_Component_List
7228 | N_Constrained_Array_Definition
7229 | N_Decimal_Fixed_Point_Definition
7230 | N_Defining_Character_Literal
7231 | N_Defining_Identifier
7232 | N_Defining_Operator_Symbol
7233 | N_Defining_Program_Unit_Name
7234 | N_Delay_Alternative
7235 | N_Delta_Aggregate
7236 | N_Delta_Constraint
7237 | N_Derived_Type_Definition
7238 | N_Designator
7239 | N_Digits_Constraint
7240 | N_Discriminant_Association
7241 | N_Discriminant_Specification
7242 | N_Empty
7243 | N_Entry_Body_Formal_Part
7244 | N_Entry_Call_Alternative
7245 | N_Entry_Declaration
7246 | N_Entry_Index_Specification
7247 | N_Enumeration_Type_Definition
7248 | N_Error
7249 | N_Exception_Handler
7250 | N_Expanded_Name
7251 | N_Explicit_Dereference
7252 | N_Extension_Aggregate
7253 | N_Floating_Point_Definition
7254 | N_Formal_Decimal_Fixed_Point_Definition
7255 | N_Formal_Derived_Type_Definition
7256 | N_Formal_Discrete_Type_Definition
7257 | N_Formal_Floating_Point_Definition
7258 | N_Formal_Modular_Type_Definition
7259 | N_Formal_Ordinary_Fixed_Point_Definition
7260 | N_Formal_Package_Declaration
7261 | N_Formal_Private_Type_Definition
7262 | N_Formal_Incomplete_Type_Definition
7263 | N_Formal_Signed_Integer_Type_Definition
7264 | N_Function_Call
7265 | N_Function_Specification
7266 | N_Generic_Association
7267 | N_Handled_Sequence_Of_Statements
7268 | N_Identifier
7269 | N_In
7270 | N_Index_Or_Discriminant_Constraint
7271 | N_Indexed_Component
7272 | N_Integer_Literal
7273 | N_Iterator_Specification
7274 | N_Itype_Reference
7275 | N_Label
7276 | N_Loop_Parameter_Specification
7277 | N_Mod_Clause
7278 | N_Modular_Type_Definition
7279 | N_Not_In
7280 | N_Null
7281 | N_Op_Abs
7282 | N_Op_Add
7283 | N_Op_And
7284 | N_Op_Concat
7285 | N_Op_Divide
7286 | N_Op_Eq
7287 | N_Op_Expon
7288 | N_Op_Ge
7289 | N_Op_Gt
7290 | N_Op_Le
7291 | N_Op_Lt
7292 | N_Op_Minus
7293 | N_Op_Mod
7294 | N_Op_Multiply
7295 | N_Op_Ne
7296 | N_Op_Not
7297 | N_Op_Or
7298 | N_Op_Plus
7299 | N_Op_Rem
7300 | N_Op_Rotate_Left
7301 | N_Op_Rotate_Right
7302 | N_Op_Shift_Left
7303 | N_Op_Shift_Right
7304 | N_Op_Shift_Right_Arithmetic
7305 | N_Op_Subtract
7306 | N_Op_Xor
7307 | N_Operator_Symbol
7308 | N_Ordinary_Fixed_Point_Definition
7309 | N_Others_Choice
7310 | N_Package_Specification
7311 | N_Parameter_Association
7312 | N_Parameter_Specification
7313 | N_Pop_Constraint_Error_Label
7314 | N_Pop_Program_Error_Label
7315 | N_Pop_Storage_Error_Label
7316 | N_Pragma_Argument_Association
7317 | N_Procedure_Specification
7318 | N_Protected_Definition
7319 | N_Push_Constraint_Error_Label
7320 | N_Push_Program_Error_Label
7321 | N_Push_Storage_Error_Label
7322 | N_Qualified_Expression
7323 | N_Quantified_Expression
7324 | N_Raise_Expression
7325 | N_Range
7326 | N_Range_Constraint
7327 | N_Real_Literal
7328 | N_Real_Range_Specification
7329 | N_Record_Definition
7330 | N_Reference
7331 | N_SCIL_Dispatch_Table_Tag_Init
7332 | N_SCIL_Dispatching_Call
7333 | N_SCIL_Membership_Test
7334 | N_Selected_Component
7335 | N_Signed_Integer_Type_Definition
7336 | N_Single_Protected_Declaration
7337 | N_Slice
7338 | N_String_Literal
7339 | N_Subtype_Indication
7340 | N_Subunit
7341 | N_Target_Name
7342 | N_Task_Definition
7343 | N_Terminate_Alternative
7344 | N_Triggering_Alternative
7345 | N_Type_Conversion
7346 | N_Unchecked_Expression
7347 | N_Unchecked_Type_Conversion
7348 | N_Unconstrained_Array_Definition
7349 | N_Unused_At_End
7350 | N_Unused_At_Start
7351 | N_Variant
7352 | N_Variant_Part
7353 | N_Validate_Unchecked_Conversion
7354 | N_With_Clause
7356 null;
7357 end case;
7359 -- If we fall through above tests, keep climbing tree
7361 N := P;
7363 if Nkind (Parent (N)) = N_Subunit then
7365 -- This is the proper body corresponding to a stub. Insertion must
7366 -- be done at the point of the stub, which is in the declarative
7367 -- part of the parent unit.
7369 P := Corresponding_Stub (Parent (N));
7371 else
7372 P := Parent (N);
7373 end if;
7374 end loop;
7375 end Insert_Actions;
7377 -- Version with check(s) suppressed
7379 procedure Insert_Actions
7380 (Assoc_Node : Node_Id;
7381 Ins_Actions : List_Id;
7382 Suppress : Check_Id)
7384 begin
7385 if Suppress = All_Checks then
7386 declare
7387 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7388 begin
7389 Scope_Suppress.Suppress := (others => True);
7390 Insert_Actions (Assoc_Node, Ins_Actions);
7391 Scope_Suppress.Suppress := Sva;
7392 end;
7394 else
7395 declare
7396 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7397 begin
7398 Scope_Suppress.Suppress (Suppress) := True;
7399 Insert_Actions (Assoc_Node, Ins_Actions);
7400 Scope_Suppress.Suppress (Suppress) := Svg;
7401 end;
7402 end if;
7403 end Insert_Actions;
7405 --------------------------
7406 -- Insert_Actions_After --
7407 --------------------------
7409 procedure Insert_Actions_After
7410 (Assoc_Node : Node_Id;
7411 Ins_Actions : List_Id)
7413 begin
7414 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7415 Store_After_Actions_In_Scope (Ins_Actions);
7416 else
7417 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7418 end if;
7419 end Insert_Actions_After;
7421 ------------------------
7422 -- Insert_Declaration --
7423 ------------------------
7425 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7426 P : Node_Id;
7428 begin
7429 pragma Assert (Nkind (N) in N_Subexpr);
7431 -- Climb until we find a procedure or a package
7433 P := N;
7434 loop
7435 pragma Assert (Present (Parent (P)));
7436 P := Parent (P);
7438 if Is_List_Member (P) then
7439 exit when Nkind_In (Parent (P), N_Package_Specification,
7440 N_Subprogram_Body);
7442 -- Special handling for handled sequence of statements, we must
7443 -- insert in the statements not the exception handlers!
7445 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7446 P := First (Statements (Parent (P)));
7447 exit;
7448 end if;
7449 end if;
7450 end loop;
7452 -- Now do the insertion
7454 Insert_Before (P, Decl);
7455 Analyze (Decl);
7456 end Insert_Declaration;
7458 ---------------------------------
7459 -- Insert_Library_Level_Action --
7460 ---------------------------------
7462 procedure Insert_Library_Level_Action (N : Node_Id) is
7463 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7465 begin
7466 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7467 -- And not Main_Unit as previously. If the main unit is a body,
7468 -- the scope needed to analyze the actions is the entity of the
7469 -- corresponding declaration.
7471 if No (Actions (Aux)) then
7472 Set_Actions (Aux, New_List (N));
7473 else
7474 Append (N, Actions (Aux));
7475 end if;
7477 Analyze (N);
7478 Pop_Scope;
7479 end Insert_Library_Level_Action;
7481 ----------------------------------
7482 -- Insert_Library_Level_Actions --
7483 ----------------------------------
7485 procedure Insert_Library_Level_Actions (L : List_Id) is
7486 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7488 begin
7489 if Is_Non_Empty_List (L) then
7490 Push_Scope (Cunit_Entity (Main_Unit));
7491 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7493 if No (Actions (Aux)) then
7494 Set_Actions (Aux, L);
7495 Analyze_List (L);
7496 else
7497 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7498 end if;
7500 Pop_Scope;
7501 end if;
7502 end Insert_Library_Level_Actions;
7504 ----------------------
7505 -- Inside_Init_Proc --
7506 ----------------------
7508 function Inside_Init_Proc return Boolean is
7509 S : Entity_Id;
7511 begin
7512 S := Current_Scope;
7513 while Present (S) and then S /= Standard_Standard loop
7514 if Is_Init_Proc (S) then
7515 return True;
7516 else
7517 S := Scope (S);
7518 end if;
7519 end loop;
7521 return False;
7522 end Inside_Init_Proc;
7524 ----------------------------
7525 -- Is_All_Null_Statements --
7526 ----------------------------
7528 function Is_All_Null_Statements (L : List_Id) return Boolean is
7529 Stm : Node_Id;
7531 begin
7532 Stm := First (L);
7533 while Present (Stm) loop
7534 if Nkind (Stm) /= N_Null_Statement then
7535 return False;
7536 end if;
7538 Next (Stm);
7539 end loop;
7541 return True;
7542 end Is_All_Null_Statements;
7544 --------------------------------------------------
7545 -- Is_Displacement_Of_Object_Or_Function_Result --
7546 --------------------------------------------------
7548 function Is_Displacement_Of_Object_Or_Function_Result
7549 (Obj_Id : Entity_Id) return Boolean
7551 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7552 -- Determine whether node N denotes a controlled function call
7554 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
7555 -- Determine whether node N denotes a generalized indexing form which
7556 -- involves a controlled result.
7558 function Is_Displace_Call (N : Node_Id) return Boolean;
7559 -- Determine whether node N denotes a call to Ada.Tags.Displace
7561 function Is_Source_Object (N : Node_Id) return Boolean;
7562 -- Determine whether a particular node denotes a source object
7564 function Strip (N : Node_Id) return Node_Id;
7565 -- Examine arbitrary node N by stripping various indirections and return
7566 -- the "real" node.
7568 ---------------------------------
7569 -- Is_Controlled_Function_Call --
7570 ---------------------------------
7572 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7573 Expr : Node_Id;
7575 begin
7576 -- When a function call appears in Object.Operation format, the
7577 -- original representation has several possible forms depending on
7578 -- the availability and form of actual parameters:
7580 -- Obj.Func N_Selected_Component
7581 -- Obj.Func (Actual) N_Indexed_Component
7582 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7583 -- N_Selected_Component
7585 Expr := Original_Node (N);
7586 loop
7587 if Nkind (Expr) = N_Function_Call then
7588 Expr := Name (Expr);
7590 -- "Obj.Func (Actual)" case
7592 elsif Nkind (Expr) = N_Indexed_Component then
7593 Expr := Prefix (Expr);
7595 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7597 elsif Nkind (Expr) = N_Selected_Component then
7598 Expr := Selector_Name (Expr);
7600 else
7601 exit;
7602 end if;
7603 end loop;
7605 return
7606 Nkind (Expr) in N_Has_Entity
7607 and then Present (Entity (Expr))
7608 and then Ekind (Entity (Expr)) = E_Function
7609 and then Needs_Finalization (Etype (Entity (Expr)));
7610 end Is_Controlled_Function_Call;
7612 ----------------------------
7613 -- Is_Controlled_Indexing --
7614 ----------------------------
7616 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
7617 Expr : constant Node_Id := Original_Node (N);
7619 begin
7620 return
7621 Nkind (Expr) = N_Indexed_Component
7622 and then Present (Generalized_Indexing (Expr))
7623 and then Needs_Finalization (Etype (Expr));
7624 end Is_Controlled_Indexing;
7626 ----------------------
7627 -- Is_Displace_Call --
7628 ----------------------
7630 function Is_Displace_Call (N : Node_Id) return Boolean is
7631 Call : constant Node_Id := Strip (N);
7633 begin
7634 return
7635 Present (Call)
7636 and then Nkind (Call) = N_Function_Call
7637 and then Nkind (Name (Call)) in N_Has_Entity
7638 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7639 end Is_Displace_Call;
7641 ----------------------
7642 -- Is_Source_Object --
7643 ----------------------
7645 function Is_Source_Object (N : Node_Id) return Boolean is
7646 Obj : constant Node_Id := Strip (N);
7648 begin
7649 return
7650 Present (Obj)
7651 and then Comes_From_Source (Obj)
7652 and then Nkind (Obj) in N_Has_Entity
7653 and then Is_Object (Entity (Obj));
7654 end Is_Source_Object;
7656 -----------
7657 -- Strip --
7658 -----------
7660 function Strip (N : Node_Id) return Node_Id is
7661 Result : Node_Id;
7663 begin
7664 Result := N;
7665 loop
7666 if Nkind (Result) = N_Explicit_Dereference then
7667 Result := Prefix (Result);
7669 elsif Nkind_In (Result, N_Type_Conversion,
7670 N_Unchecked_Type_Conversion)
7671 then
7672 Result := Expression (Result);
7674 else
7675 exit;
7676 end if;
7677 end loop;
7679 return Result;
7680 end Strip;
7682 -- Local variables
7684 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
7685 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7686 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
7687 Orig_Expr : Node_Id;
7689 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7691 begin
7692 -- Case 1:
7694 -- Obj : CW_Type := Function_Call (...);
7696 -- is rewritten into:
7698 -- Temp : ... := Function_Call (...)'reference;
7699 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7701 -- where the return type of the function and the class-wide type require
7702 -- dispatch table pointer displacement.
7704 -- Case 2:
7706 -- Obj : CW_Type := Container (...);
7708 -- is rewritten into:
7710 -- Temp : ... := Function_Call (Container, ...)'reference;
7711 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7713 -- where the container element type and the class-wide type require
7714 -- dispatch table pointer dispacement.
7716 -- Case 3:
7718 -- Obj : CW_Type := Src_Obj;
7720 -- is rewritten into:
7722 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7724 -- where the type of the source object and the class-wide type require
7725 -- dispatch table pointer displacement.
7727 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
7728 and then Is_Class_Wide_Type (Obj_Typ)
7729 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7730 and then Nkind (Orig_Decl) = N_Object_Declaration
7731 and then Comes_From_Source (Orig_Decl)
7732 then
7733 Orig_Expr := Expression (Orig_Decl);
7735 return
7736 Is_Controlled_Function_Call (Orig_Expr)
7737 or else Is_Controlled_Indexing (Orig_Expr)
7738 or else Is_Source_Object (Orig_Expr);
7739 end if;
7741 return False;
7742 end Is_Displacement_Of_Object_Or_Function_Result;
7744 ------------------------------
7745 -- Is_Finalizable_Transient --
7746 ------------------------------
7748 function Is_Finalizable_Transient
7749 (Decl : Node_Id;
7750 Rel_Node : Node_Id) return Boolean
7752 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7753 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7755 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7756 -- Determine whether transient object Trans_Id is initialized either
7757 -- by a function call which returns an access type or simply renames
7758 -- another pointer.
7760 function Initialized_By_Aliased_BIP_Func_Call
7761 (Trans_Id : Entity_Id) return Boolean;
7762 -- Determine whether transient object Trans_Id is initialized by a
7763 -- build-in-place function call where the BIPalloc parameter is of
7764 -- value 1 and BIPaccess is not null. This case creates an aliasing
7765 -- between the returned value and the value denoted by BIPaccess.
7767 function Is_Aliased
7768 (Trans_Id : Entity_Id;
7769 First_Stmt : Node_Id) return Boolean;
7770 -- Determine whether transient object Trans_Id has been renamed or
7771 -- aliased through 'reference in the statement list starting from
7772 -- First_Stmt.
7774 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7775 -- Determine whether transient object Trans_Id is allocated on the heap
7777 function Is_Iterated_Container
7778 (Trans_Id : Entity_Id;
7779 First_Stmt : Node_Id) return Boolean;
7780 -- Determine whether transient object Trans_Id denotes a container which
7781 -- is in the process of being iterated in the statement list starting
7782 -- from First_Stmt.
7784 ---------------------------
7785 -- Initialized_By_Access --
7786 ---------------------------
7788 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7789 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7791 begin
7792 return
7793 Present (Expr)
7794 and then Nkind (Expr) /= N_Reference
7795 and then Is_Access_Type (Etype (Expr));
7796 end Initialized_By_Access;
7798 ------------------------------------------
7799 -- Initialized_By_Aliased_BIP_Func_Call --
7800 ------------------------------------------
7802 function Initialized_By_Aliased_BIP_Func_Call
7803 (Trans_Id : Entity_Id) return Boolean
7805 Call : Node_Id := Expression (Parent (Trans_Id));
7807 begin
7808 -- Build-in-place calls usually appear in 'reference format
7810 if Nkind (Call) = N_Reference then
7811 Call := Prefix (Call);
7812 end if;
7814 Call := Unqual_Conv (Call);
7816 if Is_Build_In_Place_Function_Call (Call) then
7817 declare
7818 Access_Nam : Name_Id := No_Name;
7819 Access_OK : Boolean := False;
7820 Actual : Node_Id;
7821 Alloc_Nam : Name_Id := No_Name;
7822 Alloc_OK : Boolean := False;
7823 Formal : Node_Id;
7824 Func_Id : Entity_Id;
7825 Param : Node_Id;
7827 begin
7828 -- Examine all parameter associations of the function call
7830 Param := First (Parameter_Associations (Call));
7831 while Present (Param) loop
7832 if Nkind (Param) = N_Parameter_Association
7833 and then Nkind (Selector_Name (Param)) = N_Identifier
7834 then
7835 Actual := Explicit_Actual_Parameter (Param);
7836 Formal := Selector_Name (Param);
7838 -- Construct the names of formals BIPaccess and BIPalloc
7839 -- using the function name retrieved from an arbitrary
7840 -- formal.
7842 if Access_Nam = No_Name
7843 and then Alloc_Nam = No_Name
7844 and then Present (Entity (Formal))
7845 then
7846 Func_Id := Scope (Entity (Formal));
7848 Access_Nam :=
7849 New_External_Name (Chars (Func_Id),
7850 BIP_Formal_Suffix (BIP_Object_Access));
7852 Alloc_Nam :=
7853 New_External_Name (Chars (Func_Id),
7854 BIP_Formal_Suffix (BIP_Alloc_Form));
7855 end if;
7857 -- A match for BIPaccess => Temp has been found
7859 if Chars (Formal) = Access_Nam
7860 and then Nkind (Actual) /= N_Null
7861 then
7862 Access_OK := True;
7863 end if;
7865 -- A match for BIPalloc => 1 has been found
7867 if Chars (Formal) = Alloc_Nam
7868 and then Nkind (Actual) = N_Integer_Literal
7869 and then Intval (Actual) = Uint_1
7870 then
7871 Alloc_OK := True;
7872 end if;
7873 end if;
7875 Next (Param);
7876 end loop;
7878 return Access_OK and Alloc_OK;
7879 end;
7880 end if;
7882 return False;
7883 end Initialized_By_Aliased_BIP_Func_Call;
7885 ----------------
7886 -- Is_Aliased --
7887 ----------------
7889 function Is_Aliased
7890 (Trans_Id : Entity_Id;
7891 First_Stmt : Node_Id) return Boolean
7893 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7894 -- Given an object renaming declaration, retrieve the entity of the
7895 -- renamed name. Return Empty if the renamed name is anything other
7896 -- than a variable or a constant.
7898 -------------------------
7899 -- Find_Renamed_Object --
7900 -------------------------
7902 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7903 Ren_Obj : Node_Id := Empty;
7905 function Find_Object (N : Node_Id) return Traverse_Result;
7906 -- Try to detect an object which is either a constant or a
7907 -- variable.
7909 -----------------
7910 -- Find_Object --
7911 -----------------
7913 function Find_Object (N : Node_Id) return Traverse_Result is
7914 begin
7915 -- Stop the search once a constant or a variable has been
7916 -- detected.
7918 if Nkind (N) = N_Identifier
7919 and then Present (Entity (N))
7920 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7921 then
7922 Ren_Obj := Entity (N);
7923 return Abandon;
7924 end if;
7926 return OK;
7927 end Find_Object;
7929 procedure Search is new Traverse_Proc (Find_Object);
7931 -- Local variables
7933 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7935 -- Start of processing for Find_Renamed_Object
7937 begin
7938 -- Actions related to dispatching calls may appear as renamings of
7939 -- tags. Do not process this type of renaming because it does not
7940 -- use the actual value of the object.
7942 if not Is_RTE (Typ, RE_Tag_Ptr) then
7943 Search (Name (Ren_Decl));
7944 end if;
7946 return Ren_Obj;
7947 end Find_Renamed_Object;
7949 -- Local variables
7951 Expr : Node_Id;
7952 Ren_Obj : Entity_Id;
7953 Stmt : Node_Id;
7955 -- Start of processing for Is_Aliased
7957 begin
7958 -- A controlled transient object is not considered aliased when it
7959 -- appears inside an expression_with_actions node even when there are
7960 -- explicit aliases of it:
7962 -- do
7963 -- Trans_Id : Ctrl_Typ ...; -- transient object
7964 -- Alias : ... := Trans_Id; -- object is aliased
7965 -- Val : constant Boolean :=
7966 -- ... Alias ...; -- aliasing ends
7967 -- <finalize Trans_Id> -- object safe to finalize
7968 -- in Val end;
7970 -- Expansion ensures that all aliases are encapsulated in the actions
7971 -- list and do not leak to the expression by forcing the evaluation
7972 -- of the expression.
7974 if Nkind (Rel_Node) = N_Expression_With_Actions then
7975 return False;
7977 -- Otherwise examine the statements after the controlled transient
7978 -- object and look for various forms of aliasing.
7980 else
7981 Stmt := First_Stmt;
7982 while Present (Stmt) loop
7983 if Nkind (Stmt) = N_Object_Declaration then
7984 Expr := Expression (Stmt);
7986 -- Aliasing of the form:
7987 -- Obj : ... := Trans_Id'reference;
7989 if Present (Expr)
7990 and then Nkind (Expr) = N_Reference
7991 and then Nkind (Prefix (Expr)) = N_Identifier
7992 and then Entity (Prefix (Expr)) = Trans_Id
7993 then
7994 return True;
7995 end if;
7997 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
7998 Ren_Obj := Find_Renamed_Object (Stmt);
8000 -- Aliasing of the form:
8001 -- Obj : ... renames ... Trans_Id ...;
8003 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8004 return True;
8005 end if;
8006 end if;
8008 Next (Stmt);
8009 end loop;
8011 return False;
8012 end if;
8013 end Is_Aliased;
8015 ------------------
8016 -- Is_Allocated --
8017 ------------------
8019 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8020 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8021 begin
8022 return
8023 Is_Access_Type (Etype (Trans_Id))
8024 and then Present (Expr)
8025 and then Nkind (Expr) = N_Allocator;
8026 end Is_Allocated;
8028 ---------------------------
8029 -- Is_Iterated_Container --
8030 ---------------------------
8032 function Is_Iterated_Container
8033 (Trans_Id : Entity_Id;
8034 First_Stmt : Node_Id) return Boolean
8036 Aspect : Node_Id;
8037 Call : Node_Id;
8038 Iter : Entity_Id;
8039 Param : Node_Id;
8040 Stmt : Node_Id;
8041 Typ : Entity_Id;
8043 begin
8044 -- It is not possible to iterate over containers in non-Ada 2012 code
8046 if Ada_Version < Ada_2012 then
8047 return False;
8048 end if;
8050 Typ := Etype (Trans_Id);
8052 -- Handle access type created for secondary stack use
8054 if Is_Access_Type (Typ) then
8055 Typ := Designated_Type (Typ);
8056 end if;
8058 -- Look for aspect Default_Iterator. It may be part of a type
8059 -- declaration for a container, or inherited from a base type
8060 -- or parent type.
8062 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8064 if Present (Aspect) then
8065 Iter := Entity (Aspect);
8067 -- Examine the statements following the container object and
8068 -- look for a call to the default iterate routine where the
8069 -- first parameter is the transient. Such a call appears as:
8071 -- It : Access_To_CW_Iterator :=
8072 -- Iterate (Tran_Id.all, ...)'reference;
8074 Stmt := First_Stmt;
8075 while Present (Stmt) loop
8077 -- Detect an object declaration which is initialized by a
8078 -- secondary stack function call.
8080 if Nkind (Stmt) = N_Object_Declaration
8081 and then Present (Expression (Stmt))
8082 and then Nkind (Expression (Stmt)) = N_Reference
8083 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8084 then
8085 Call := Prefix (Expression (Stmt));
8087 -- The call must invoke the default iterate routine of
8088 -- the container and the transient object must appear as
8089 -- the first actual parameter. Skip any calls whose names
8090 -- are not entities.
8092 if Is_Entity_Name (Name (Call))
8093 and then Entity (Name (Call)) = Iter
8094 and then Present (Parameter_Associations (Call))
8095 then
8096 Param := First (Parameter_Associations (Call));
8098 if Nkind (Param) = N_Explicit_Dereference
8099 and then Entity (Prefix (Param)) = Trans_Id
8100 then
8101 return True;
8102 end if;
8103 end if;
8104 end if;
8106 Next (Stmt);
8107 end loop;
8108 end if;
8110 return False;
8111 end Is_Iterated_Container;
8113 -- Local variables
8115 Desig : Entity_Id := Obj_Typ;
8117 -- Start of processing for Is_Finalizable_Transient
8119 begin
8120 -- Handle access types
8122 if Is_Access_Type (Desig) then
8123 Desig := Available_View (Designated_Type (Desig));
8124 end if;
8126 return
8127 Ekind_In (Obj_Id, E_Constant, E_Variable)
8128 and then Needs_Finalization (Desig)
8129 and then Requires_Transient_Scope (Desig)
8130 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8132 -- Do not consider a transient object that was already processed
8134 and then not Is_Finalized_Transient (Obj_Id)
8136 -- Do not consider renamed or 'reference-d transient objects because
8137 -- the act of renaming extends the object's lifetime.
8139 and then not Is_Aliased (Obj_Id, Decl)
8141 -- Do not consider transient objects allocated on the heap since
8142 -- they are attached to a finalization master.
8144 and then not Is_Allocated (Obj_Id)
8146 -- If the transient object is a pointer, check that it is not
8147 -- initialized by a function that returns a pointer or acts as a
8148 -- renaming of another pointer.
8150 and then
8151 (not Is_Access_Type (Obj_Typ)
8152 or else not Initialized_By_Access (Obj_Id))
8154 -- Do not consider transient objects which act as indirect aliases
8155 -- of build-in-place function results.
8157 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8159 -- Do not consider conversions of tags to class-wide types
8161 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8163 -- Do not consider iterators because those are treated as normal
8164 -- controlled objects and are processed by the usual finalization
8165 -- machinery. This avoids the double finalization of an iterator.
8167 and then not Is_Iterator (Desig)
8169 -- Do not consider containers in the context of iterator loops. Such
8170 -- transient objects must exist for as long as the loop is around,
8171 -- otherwise any operation carried out by the iterator will fail.
8173 and then not Is_Iterated_Container (Obj_Id, Decl);
8174 end Is_Finalizable_Transient;
8176 ---------------------------------
8177 -- Is_Fully_Repped_Tagged_Type --
8178 ---------------------------------
8180 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8181 U : constant Entity_Id := Underlying_Type (T);
8182 Comp : Entity_Id;
8184 begin
8185 if No (U) or else not Is_Tagged_Type (U) then
8186 return False;
8187 elsif Has_Discriminants (U) then
8188 return False;
8189 elsif not Has_Specified_Layout (U) then
8190 return False;
8191 end if;
8193 -- Here we have a tagged type, see if it has any unlayed out fields
8194 -- other than a possible tag and parent fields. If so, we return False.
8196 Comp := First_Component (U);
8197 while Present (Comp) loop
8198 if not Is_Tag (Comp)
8199 and then Chars (Comp) /= Name_uParent
8200 and then No (Component_Clause (Comp))
8201 then
8202 return False;
8203 else
8204 Next_Component (Comp);
8205 end if;
8206 end loop;
8208 -- All components are layed out
8210 return True;
8211 end Is_Fully_Repped_Tagged_Type;
8213 ----------------------------------
8214 -- Is_Library_Level_Tagged_Type --
8215 ----------------------------------
8217 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8218 begin
8219 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8220 end Is_Library_Level_Tagged_Type;
8222 --------------------------
8223 -- Is_Non_BIP_Func_Call --
8224 --------------------------
8226 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8227 begin
8228 -- The expected call is of the format
8230 -- Func_Call'reference
8232 return
8233 Nkind (Expr) = N_Reference
8234 and then Nkind (Prefix (Expr)) = N_Function_Call
8235 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8236 end Is_Non_BIP_Func_Call;
8238 ----------------------------------
8239 -- Is_Possibly_Unaligned_Object --
8240 ----------------------------------
8242 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8243 T : constant Entity_Id := Etype (N);
8245 begin
8246 -- If renamed object, apply test to underlying object
8248 if Is_Entity_Name (N)
8249 and then Is_Object (Entity (N))
8250 and then Present (Renamed_Object (Entity (N)))
8251 then
8252 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8253 end if;
8255 -- Tagged and controlled types and aliased types are always aligned, as
8256 -- are concurrent types.
8258 if Is_Aliased (T)
8259 or else Has_Controlled_Component (T)
8260 or else Is_Concurrent_Type (T)
8261 or else Is_Tagged_Type (T)
8262 or else Is_Controlled (T)
8263 then
8264 return False;
8265 end if;
8267 -- If this is an element of a packed array, may be unaligned
8269 if Is_Ref_To_Bit_Packed_Array (N) then
8270 return True;
8271 end if;
8273 -- Case of indexed component reference: test whether prefix is unaligned
8275 if Nkind (N) = N_Indexed_Component then
8276 return Is_Possibly_Unaligned_Object (Prefix (N));
8278 -- Case of selected component reference
8280 elsif Nkind (N) = N_Selected_Component then
8281 declare
8282 P : constant Node_Id := Prefix (N);
8283 C : constant Entity_Id := Entity (Selector_Name (N));
8284 M : Nat;
8285 S : Nat;
8287 begin
8288 -- If component reference is for an array with non-static bounds,
8289 -- then it is always aligned: we can only process unaligned arrays
8290 -- with static bounds (more precisely compile time known bounds).
8292 if Is_Array_Type (T)
8293 and then not Compile_Time_Known_Bounds (T)
8294 then
8295 return False;
8296 end if;
8298 -- If component is aliased, it is definitely properly aligned
8300 if Is_Aliased (C) then
8301 return False;
8302 end if;
8304 -- If component is for a type implemented as a scalar, and the
8305 -- record is packed, and the component is other than the first
8306 -- component of the record, then the component may be unaligned.
8308 if Is_Packed (Etype (P))
8309 and then Represented_As_Scalar (Etype (C))
8310 and then First_Entity (Scope (C)) /= C
8311 then
8312 return True;
8313 end if;
8315 -- Compute maximum possible alignment for T
8317 -- If alignment is known, then that settles things
8319 if Known_Alignment (T) then
8320 M := UI_To_Int (Alignment (T));
8322 -- If alignment is not known, tentatively set max alignment
8324 else
8325 M := Ttypes.Maximum_Alignment;
8327 -- We can reduce this if the Esize is known since the default
8328 -- alignment will never be more than the smallest power of 2
8329 -- that does not exceed this Esize value.
8331 if Known_Esize (T) then
8332 S := UI_To_Int (Esize (T));
8334 while (M / 2) >= S loop
8335 M := M / 2;
8336 end loop;
8337 end if;
8338 end if;
8340 -- The following code is historical, it used to be present but it
8341 -- is too cautious, because the front-end does not know the proper
8342 -- default alignments for the target. Also, if the alignment is
8343 -- not known, the front end can't know in any case. If a copy is
8344 -- needed, the back-end will take care of it. This whole section
8345 -- including this comment can be removed later ???
8347 -- If the component reference is for a record that has a specified
8348 -- alignment, and we either know it is too small, or cannot tell,
8349 -- then the component may be unaligned.
8351 -- What is the following commented out code ???
8353 -- if Known_Alignment (Etype (P))
8354 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8355 -- and then M > Alignment (Etype (P))
8356 -- then
8357 -- return True;
8358 -- end if;
8360 -- Case of component clause present which may specify an
8361 -- unaligned position.
8363 if Present (Component_Clause (C)) then
8365 -- Otherwise we can do a test to make sure that the actual
8366 -- start position in the record, and the length, are both
8367 -- consistent with the required alignment. If not, we know
8368 -- that we are unaligned.
8370 declare
8371 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8372 begin
8373 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
8374 or else Esize (C) mod Align_In_Bits /= 0
8375 then
8376 return True;
8377 end if;
8378 end;
8379 end if;
8381 -- Otherwise, for a component reference, test prefix
8383 return Is_Possibly_Unaligned_Object (P);
8384 end;
8386 -- If not a component reference, must be aligned
8388 else
8389 return False;
8390 end if;
8391 end Is_Possibly_Unaligned_Object;
8393 ---------------------------------
8394 -- Is_Possibly_Unaligned_Slice --
8395 ---------------------------------
8397 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8398 begin
8399 -- Go to renamed object
8401 if Is_Entity_Name (N)
8402 and then Is_Object (Entity (N))
8403 and then Present (Renamed_Object (Entity (N)))
8404 then
8405 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8406 end if;
8408 -- The reference must be a slice
8410 if Nkind (N) /= N_Slice then
8411 return False;
8412 end if;
8414 -- We only need to worry if the target has strict alignment
8416 if not Target_Strict_Alignment then
8417 return False;
8418 end if;
8420 -- If it is a slice, then look at the array type being sliced
8422 declare
8423 Sarr : constant Node_Id := Prefix (N);
8424 -- Prefix of the slice, i.e. the array being sliced
8426 Styp : constant Entity_Id := Etype (Prefix (N));
8427 -- Type of the array being sliced
8429 Pref : Node_Id;
8430 Ptyp : Entity_Id;
8432 begin
8433 -- The problems arise if the array object that is being sliced
8434 -- is a component of a record or array, and we cannot guarantee
8435 -- the alignment of the array within its containing object.
8437 -- To investigate this, we look at successive prefixes to see
8438 -- if we have a worrisome indexed or selected component.
8440 Pref := Sarr;
8441 loop
8442 -- Case of array is part of an indexed component reference
8444 if Nkind (Pref) = N_Indexed_Component then
8445 Ptyp := Etype (Prefix (Pref));
8447 -- The only problematic case is when the array is packed, in
8448 -- which case we really know nothing about the alignment of
8449 -- individual components.
8451 if Is_Bit_Packed_Array (Ptyp) then
8452 return True;
8453 end if;
8455 -- Case of array is part of a selected component reference
8457 elsif Nkind (Pref) = N_Selected_Component then
8458 Ptyp := Etype (Prefix (Pref));
8460 -- We are definitely in trouble if the record in question
8461 -- has an alignment, and either we know this alignment is
8462 -- inconsistent with the alignment of the slice, or we don't
8463 -- know what the alignment of the slice should be.
8465 if Known_Alignment (Ptyp)
8466 and then (Unknown_Alignment (Styp)
8467 or else Alignment (Styp) > Alignment (Ptyp))
8468 then
8469 return True;
8470 end if;
8472 -- We are in potential trouble if the record type is packed.
8473 -- We could special case when we know that the array is the
8474 -- first component, but that's not such a simple case ???
8476 if Is_Packed (Ptyp) then
8477 return True;
8478 end if;
8480 -- We are in trouble if there is a component clause, and
8481 -- either we do not know the alignment of the slice, or
8482 -- the alignment of the slice is inconsistent with the
8483 -- bit position specified by the component clause.
8485 declare
8486 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8487 begin
8488 if Present (Component_Clause (Field))
8489 and then
8490 (Unknown_Alignment (Styp)
8491 or else
8492 (Component_Bit_Offset (Field) mod
8493 (System_Storage_Unit * Alignment (Styp))) /= 0)
8494 then
8495 return True;
8496 end if;
8497 end;
8499 -- For cases other than selected or indexed components we know we
8500 -- are OK, since no issues arise over alignment.
8502 else
8503 return False;
8504 end if;
8506 -- We processed an indexed component or selected component
8507 -- reference that looked safe, so keep checking prefixes.
8509 Pref := Prefix (Pref);
8510 end loop;
8511 end;
8512 end Is_Possibly_Unaligned_Slice;
8514 -------------------------------
8515 -- Is_Related_To_Func_Return --
8516 -------------------------------
8518 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8519 Expr : constant Node_Id := Related_Expression (Id);
8520 begin
8521 return
8522 Present (Expr)
8523 and then Nkind (Expr) = N_Explicit_Dereference
8524 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8525 end Is_Related_To_Func_Return;
8527 --------------------------------
8528 -- Is_Ref_To_Bit_Packed_Array --
8529 --------------------------------
8531 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8532 Result : Boolean;
8533 Expr : Node_Id;
8535 begin
8536 if Is_Entity_Name (N)
8537 and then Is_Object (Entity (N))
8538 and then Present (Renamed_Object (Entity (N)))
8539 then
8540 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8541 end if;
8543 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8544 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8545 Result := True;
8546 else
8547 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8548 end if;
8550 if Result and then Nkind (N) = N_Indexed_Component then
8551 Expr := First (Expressions (N));
8552 while Present (Expr) loop
8553 Force_Evaluation (Expr);
8554 Next (Expr);
8555 end loop;
8556 end if;
8558 return Result;
8560 else
8561 return False;
8562 end if;
8563 end Is_Ref_To_Bit_Packed_Array;
8565 --------------------------------
8566 -- Is_Ref_To_Bit_Packed_Slice --
8567 --------------------------------
8569 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8570 begin
8571 if Nkind (N) = N_Type_Conversion then
8572 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8574 elsif Is_Entity_Name (N)
8575 and then Is_Object (Entity (N))
8576 and then Present (Renamed_Object (Entity (N)))
8577 then
8578 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8580 elsif Nkind (N) = N_Slice
8581 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8582 then
8583 return True;
8585 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8586 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8588 else
8589 return False;
8590 end if;
8591 end Is_Ref_To_Bit_Packed_Slice;
8593 -----------------------
8594 -- Is_Renamed_Object --
8595 -----------------------
8597 function Is_Renamed_Object (N : Node_Id) return Boolean is
8598 Pnod : constant Node_Id := Parent (N);
8599 Kind : constant Node_Kind := Nkind (Pnod);
8600 begin
8601 if Kind = N_Object_Renaming_Declaration then
8602 return True;
8603 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8604 return Is_Renamed_Object (Pnod);
8605 else
8606 return False;
8607 end if;
8608 end Is_Renamed_Object;
8610 --------------------------------------
8611 -- Is_Secondary_Stack_BIP_Func_Call --
8612 --------------------------------------
8614 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8615 Alloc_Nam : Name_Id := No_Name;
8616 Actual : Node_Id;
8617 Call : Node_Id := Expr;
8618 Formal : Node_Id;
8619 Param : Node_Id;
8621 begin
8622 -- Build-in-place calls usually appear in 'reference format. Note that
8623 -- the accessibility check machinery may add an extra 'reference due to
8624 -- side effect removal.
8626 while Nkind (Call) = N_Reference loop
8627 Call := Prefix (Call);
8628 end loop;
8630 Call := Unqual_Conv (Call);
8632 if Is_Build_In_Place_Function_Call (Call) then
8634 -- Examine all parameter associations of the function call
8636 Param := First (Parameter_Associations (Call));
8637 while Present (Param) loop
8638 if Nkind (Param) = N_Parameter_Association then
8639 Formal := Selector_Name (Param);
8640 Actual := Explicit_Actual_Parameter (Param);
8642 -- Construct the name of formal BIPalloc. It is much easier to
8643 -- extract the name of the function using an arbitrary formal's
8644 -- scope rather than the Name field of Call.
8646 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8647 Alloc_Nam :=
8648 New_External_Name
8649 (Chars (Scope (Entity (Formal))),
8650 BIP_Formal_Suffix (BIP_Alloc_Form));
8651 end if;
8653 -- A match for BIPalloc => 2 has been found
8655 if Chars (Formal) = Alloc_Nam
8656 and then Nkind (Actual) = N_Integer_Literal
8657 and then Intval (Actual) = Uint_2
8658 then
8659 return True;
8660 end if;
8661 end if;
8663 Next (Param);
8664 end loop;
8665 end if;
8667 return False;
8668 end Is_Secondary_Stack_BIP_Func_Call;
8670 -------------------------------------
8671 -- Is_Tag_To_Class_Wide_Conversion --
8672 -------------------------------------
8674 function Is_Tag_To_Class_Wide_Conversion
8675 (Obj_Id : Entity_Id) return Boolean
8677 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8679 begin
8680 return
8681 Is_Class_Wide_Type (Etype (Obj_Id))
8682 and then Present (Expr)
8683 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8684 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8685 end Is_Tag_To_Class_Wide_Conversion;
8687 ----------------------------
8688 -- Is_Untagged_Derivation --
8689 ----------------------------
8691 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8692 begin
8693 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8694 or else
8695 (Is_Private_Type (T) and then Present (Full_View (T))
8696 and then not Is_Tagged_Type (Full_View (T))
8697 and then Is_Derived_Type (Full_View (T))
8698 and then Etype (Full_View (T)) /= T);
8699 end Is_Untagged_Derivation;
8701 ------------------------------------
8702 -- Is_Untagged_Private_Derivation --
8703 ------------------------------------
8705 function Is_Untagged_Private_Derivation
8706 (Priv_Typ : Entity_Id;
8707 Full_Typ : Entity_Id) return Boolean
8709 begin
8710 return
8711 Present (Priv_Typ)
8712 and then Is_Untagged_Derivation (Priv_Typ)
8713 and then Is_Private_Type (Etype (Priv_Typ))
8714 and then Present (Full_Typ)
8715 and then Is_Itype (Full_Typ);
8716 end Is_Untagged_Private_Derivation;
8718 ---------------------------
8719 -- Is_Volatile_Reference --
8720 ---------------------------
8722 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8723 begin
8724 -- Only source references are to be treated as volatile, internally
8725 -- generated stuff cannot have volatile external effects.
8727 if not Comes_From_Source (N) then
8728 return False;
8730 -- Never true for reference to a type
8732 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8733 return False;
8735 -- Never true for a compile time known constant
8737 elsif Compile_Time_Known_Value (N) then
8738 return False;
8740 -- True if object reference with volatile type
8742 elsif Is_Volatile_Object (N) then
8743 return True;
8745 -- True if reference to volatile entity
8747 elsif Is_Entity_Name (N) then
8748 return Treat_As_Volatile (Entity (N));
8750 -- True for slice of volatile array
8752 elsif Nkind (N) = N_Slice then
8753 return Is_Volatile_Reference (Prefix (N));
8755 -- True if volatile component
8757 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8758 if (Is_Entity_Name (Prefix (N))
8759 and then Has_Volatile_Components (Entity (Prefix (N))))
8760 or else (Present (Etype (Prefix (N)))
8761 and then Has_Volatile_Components (Etype (Prefix (N))))
8762 then
8763 return True;
8764 else
8765 return Is_Volatile_Reference (Prefix (N));
8766 end if;
8768 -- Otherwise false
8770 else
8771 return False;
8772 end if;
8773 end Is_Volatile_Reference;
8775 --------------------
8776 -- Kill_Dead_Code --
8777 --------------------
8779 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8780 W : Boolean := Warn;
8781 -- Set False if warnings suppressed
8783 begin
8784 if Present (N) then
8785 Remove_Warning_Messages (N);
8787 -- Update the internal structures of the ABE mechanism in case the
8788 -- dead node is an elaboration scenario.
8790 Kill_Elaboration_Scenario (N);
8792 -- Generate warning if appropriate
8794 if W then
8796 -- We suppress the warning if this code is under control of an
8797 -- if statement, whose condition is a simple identifier, and
8798 -- either we are in an instance, or warnings off is set for this
8799 -- identifier. The reason for killing it in the instance case is
8800 -- that it is common and reasonable for code to be deleted in
8801 -- instances for various reasons.
8803 -- Could we use Is_Statically_Unevaluated here???
8805 if Nkind (Parent (N)) = N_If_Statement then
8806 declare
8807 C : constant Node_Id := Condition (Parent (N));
8808 begin
8809 if Nkind (C) = N_Identifier
8810 and then
8811 (In_Instance
8812 or else (Present (Entity (C))
8813 and then Has_Warnings_Off (Entity (C))))
8814 then
8815 W := False;
8816 end if;
8817 end;
8818 end if;
8820 -- Generate warning if not suppressed
8822 if W then
8823 Error_Msg_F
8824 ("?t?this code can never be executed and has been deleted!",
8826 end if;
8827 end if;
8829 -- Recurse into block statements and bodies to process declarations
8830 -- and statements.
8832 if Nkind (N) = N_Block_Statement
8833 or else Nkind (N) = N_Subprogram_Body
8834 or else Nkind (N) = N_Package_Body
8835 then
8836 Kill_Dead_Code (Declarations (N), False);
8837 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8839 if Nkind (N) = N_Subprogram_Body then
8840 Set_Is_Eliminated (Defining_Entity (N));
8841 end if;
8843 elsif Nkind (N) = N_Package_Declaration then
8844 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8845 Kill_Dead_Code (Private_Declarations (Specification (N)));
8847 -- ??? After this point, Delete_Tree has been called on all
8848 -- declarations in Specification (N), so references to entities
8849 -- therein look suspicious.
8851 declare
8852 E : Entity_Id := First_Entity (Defining_Entity (N));
8854 begin
8855 while Present (E) loop
8856 if Ekind (E) = E_Operator then
8857 Set_Is_Eliminated (E);
8858 end if;
8860 Next_Entity (E);
8861 end loop;
8862 end;
8864 -- Recurse into composite statement to kill individual statements in
8865 -- particular instantiations.
8867 elsif Nkind (N) = N_If_Statement then
8868 Kill_Dead_Code (Then_Statements (N));
8869 Kill_Dead_Code (Elsif_Parts (N));
8870 Kill_Dead_Code (Else_Statements (N));
8872 elsif Nkind (N) = N_Loop_Statement then
8873 Kill_Dead_Code (Statements (N));
8875 elsif Nkind (N) = N_Case_Statement then
8876 declare
8877 Alt : Node_Id;
8878 begin
8879 Alt := First (Alternatives (N));
8880 while Present (Alt) loop
8881 Kill_Dead_Code (Statements (Alt));
8882 Next (Alt);
8883 end loop;
8884 end;
8886 elsif Nkind (N) = N_Case_Statement_Alternative then
8887 Kill_Dead_Code (Statements (N));
8889 -- Deal with dead instances caused by deleting instantiations
8891 elsif Nkind (N) in N_Generic_Instantiation then
8892 Remove_Dead_Instance (N);
8893 end if;
8894 end if;
8895 end Kill_Dead_Code;
8897 -- Case where argument is a list of nodes to be killed
8899 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8900 N : Node_Id;
8901 W : Boolean;
8903 begin
8904 W := Warn;
8906 if Is_Non_Empty_List (L) then
8907 N := First (L);
8908 while Present (N) loop
8909 Kill_Dead_Code (N, W);
8910 W := False;
8911 Next (N);
8912 end loop;
8913 end if;
8914 end Kill_Dead_Code;
8916 ------------------------
8917 -- Known_Non_Negative --
8918 ------------------------
8920 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8921 begin
8922 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8923 return True;
8925 else
8926 declare
8927 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8928 begin
8929 return
8930 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8931 end;
8932 end if;
8933 end Known_Non_Negative;
8935 -----------------------------
8936 -- Make_CW_Equivalent_Type --
8937 -----------------------------
8939 -- Create a record type used as an equivalent of any member of the class
8940 -- which takes its size from exp.
8942 -- Generate the following code:
8944 -- type Equiv_T is record
8945 -- _parent : T (List of discriminant constraints taken from Exp);
8946 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8947 -- end Equiv_T;
8949 -- ??? Note that this type does not guarantee same alignment as all
8950 -- derived types
8952 function Make_CW_Equivalent_Type
8953 (T : Entity_Id;
8954 E : Node_Id) return Entity_Id
8956 Loc : constant Source_Ptr := Sloc (E);
8957 Root_Typ : constant Entity_Id := Root_Type (T);
8958 List_Def : constant List_Id := Empty_List;
8959 Comp_List : constant List_Id := New_List;
8960 Equiv_Type : Entity_Id;
8961 Range_Type : Entity_Id;
8962 Str_Type : Entity_Id;
8963 Constr_Root : Entity_Id;
8964 Sizexpr : Node_Id;
8966 begin
8967 -- If the root type is already constrained, there are no discriminants
8968 -- in the expression.
8970 if not Has_Discriminants (Root_Typ)
8971 or else Is_Constrained (Root_Typ)
8972 then
8973 Constr_Root := Root_Typ;
8975 -- At this point in the expansion, non-limited view of the type
8976 -- must be available, otherwise the error will be reported later.
8978 if From_Limited_With (Constr_Root)
8979 and then Present (Non_Limited_View (Constr_Root))
8980 then
8981 Constr_Root := Non_Limited_View (Constr_Root);
8982 end if;
8984 else
8985 Constr_Root := Make_Temporary (Loc, 'R');
8987 -- subtype cstr__n is T (List of discr constraints taken from Exp)
8989 Append_To (List_Def,
8990 Make_Subtype_Declaration (Loc,
8991 Defining_Identifier => Constr_Root,
8992 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
8993 end if;
8995 -- Generate the range subtype declaration
8997 Range_Type := Make_Temporary (Loc, 'G');
8999 if not Is_Interface (Root_Typ) then
9001 -- subtype rg__xx is
9002 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9004 Sizexpr :=
9005 Make_Op_Subtract (Loc,
9006 Left_Opnd =>
9007 Make_Attribute_Reference (Loc,
9008 Prefix =>
9009 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9010 Attribute_Name => Name_Size),
9011 Right_Opnd =>
9012 Make_Attribute_Reference (Loc,
9013 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9014 Attribute_Name => Name_Object_Size));
9015 else
9016 -- subtype rg__xx is
9017 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9019 Sizexpr :=
9020 Make_Attribute_Reference (Loc,
9021 Prefix =>
9022 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9023 Attribute_Name => Name_Size);
9024 end if;
9026 Set_Paren_Count (Sizexpr, 1);
9028 Append_To (List_Def,
9029 Make_Subtype_Declaration (Loc,
9030 Defining_Identifier => Range_Type,
9031 Subtype_Indication =>
9032 Make_Subtype_Indication (Loc,
9033 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9034 Constraint => Make_Range_Constraint (Loc,
9035 Range_Expression =>
9036 Make_Range (Loc,
9037 Low_Bound => Make_Integer_Literal (Loc, 1),
9038 High_Bound =>
9039 Make_Op_Divide (Loc,
9040 Left_Opnd => Sizexpr,
9041 Right_Opnd => Make_Integer_Literal (Loc,
9042 Intval => System_Storage_Unit)))))));
9044 -- subtype str__nn is Storage_Array (rg__x);
9046 Str_Type := Make_Temporary (Loc, 'S');
9047 Append_To (List_Def,
9048 Make_Subtype_Declaration (Loc,
9049 Defining_Identifier => Str_Type,
9050 Subtype_Indication =>
9051 Make_Subtype_Indication (Loc,
9052 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9053 Constraint =>
9054 Make_Index_Or_Discriminant_Constraint (Loc,
9055 Constraints =>
9056 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9058 -- type Equiv_T is record
9059 -- [ _parent : Tnn; ]
9060 -- E : Str_Type;
9061 -- end Equiv_T;
9063 Equiv_Type := Make_Temporary (Loc, 'T');
9064 Set_Ekind (Equiv_Type, E_Record_Type);
9065 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9067 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9068 -- treatment for this type. In particular, even though _parent's type
9069 -- is a controlled type or contains controlled components, we do not
9070 -- want to set Has_Controlled_Component on it to avoid making it gain
9071 -- an unwanted _controller component.
9073 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9075 -- A class-wide equivalent type does not require initialization
9077 Set_Suppress_Initialization (Equiv_Type);
9079 if not Is_Interface (Root_Typ) then
9080 Append_To (Comp_List,
9081 Make_Component_Declaration (Loc,
9082 Defining_Identifier =>
9083 Make_Defining_Identifier (Loc, Name_uParent),
9084 Component_Definition =>
9085 Make_Component_Definition (Loc,
9086 Aliased_Present => False,
9087 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9088 end if;
9090 Append_To (Comp_List,
9091 Make_Component_Declaration (Loc,
9092 Defining_Identifier => Make_Temporary (Loc, 'C'),
9093 Component_Definition =>
9094 Make_Component_Definition (Loc,
9095 Aliased_Present => False,
9096 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9098 Append_To (List_Def,
9099 Make_Full_Type_Declaration (Loc,
9100 Defining_Identifier => Equiv_Type,
9101 Type_Definition =>
9102 Make_Record_Definition (Loc,
9103 Component_List =>
9104 Make_Component_List (Loc,
9105 Component_Items => Comp_List,
9106 Variant_Part => Empty))));
9108 -- Suppress all checks during the analysis of the expanded code to avoid
9109 -- the generation of spurious warnings under ZFP run-time.
9111 Insert_Actions (E, List_Def, Suppress => All_Checks);
9112 return Equiv_Type;
9113 end Make_CW_Equivalent_Type;
9115 -------------------------
9116 -- Make_Invariant_Call --
9117 -------------------------
9119 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9120 Loc : constant Source_Ptr := Sloc (Expr);
9121 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9123 Proc_Id : Entity_Id;
9125 begin
9126 pragma Assert (Has_Invariants (Typ));
9128 Proc_Id := Invariant_Procedure (Typ);
9129 pragma Assert (Present (Proc_Id));
9131 return
9132 Make_Procedure_Call_Statement (Loc,
9133 Name => New_Occurrence_Of (Proc_Id, Loc),
9134 Parameter_Associations => New_List (Relocate_Node (Expr)));
9135 end Make_Invariant_Call;
9137 ------------------------
9138 -- Make_Literal_Range --
9139 ------------------------
9141 function Make_Literal_Range
9142 (Loc : Source_Ptr;
9143 Literal_Typ : Entity_Id) return Node_Id
9145 Lo : constant Node_Id :=
9146 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9147 Index : constant Entity_Id := Etype (Lo);
9148 Length_Expr : constant Node_Id :=
9149 Make_Op_Subtract (Loc,
9150 Left_Opnd =>
9151 Make_Integer_Literal (Loc,
9152 Intval => String_Literal_Length (Literal_Typ)),
9153 Right_Opnd => Make_Integer_Literal (Loc, 1));
9155 Hi : Node_Id;
9157 begin
9158 Set_Analyzed (Lo, False);
9160 if Is_Integer_Type (Index) then
9161 Hi :=
9162 Make_Op_Add (Loc,
9163 Left_Opnd => New_Copy_Tree (Lo),
9164 Right_Opnd => Length_Expr);
9165 else
9166 Hi :=
9167 Make_Attribute_Reference (Loc,
9168 Attribute_Name => Name_Val,
9169 Prefix => New_Occurrence_Of (Index, Loc),
9170 Expressions => New_List (
9171 Make_Op_Add (Loc,
9172 Left_Opnd =>
9173 Make_Attribute_Reference (Loc,
9174 Attribute_Name => Name_Pos,
9175 Prefix => New_Occurrence_Of (Index, Loc),
9176 Expressions => New_List (New_Copy_Tree (Lo))),
9177 Right_Opnd => Length_Expr)));
9178 end if;
9180 return
9181 Make_Range (Loc,
9182 Low_Bound => Lo,
9183 High_Bound => Hi);
9184 end Make_Literal_Range;
9186 --------------------------
9187 -- Make_Non_Empty_Check --
9188 --------------------------
9190 function Make_Non_Empty_Check
9191 (Loc : Source_Ptr;
9192 N : Node_Id) return Node_Id
9194 begin
9195 return
9196 Make_Op_Ne (Loc,
9197 Left_Opnd =>
9198 Make_Attribute_Reference (Loc,
9199 Attribute_Name => Name_Length,
9200 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9201 Right_Opnd =>
9202 Make_Integer_Literal (Loc, 0));
9203 end Make_Non_Empty_Check;
9205 -------------------------
9206 -- Make_Predicate_Call --
9207 -------------------------
9209 -- WARNING: This routine manages Ghost regions. Return statements must be
9210 -- replaced by gotos which jump to the end of the routine and restore the
9211 -- Ghost mode.
9213 function Make_Predicate_Call
9214 (Typ : Entity_Id;
9215 Expr : Node_Id;
9216 Mem : Boolean := False) return Node_Id
9218 Loc : constant Source_Ptr := Sloc (Expr);
9220 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9221 -- Save the Ghost mode to restore on exit
9223 Call : Node_Id;
9224 Func_Id : Entity_Id;
9226 begin
9227 pragma Assert (Present (Predicate_Function (Typ)));
9229 -- The related type may be subject to pragma Ghost. Set the mode now to
9230 -- ensure that the call is properly marked as Ghost.
9232 Set_Ghost_Mode (Typ);
9234 -- Call special membership version if requested and available
9236 if Mem and then Present (Predicate_Function_M (Typ)) then
9237 Func_Id := Predicate_Function_M (Typ);
9238 else
9239 Func_Id := Predicate_Function (Typ);
9240 end if;
9242 -- Case of calling normal predicate function
9244 -- If the type is tagged, the expression may be class-wide, in which
9245 -- case it has to be converted to its root type, given that the
9246 -- generated predicate function is not dispatching.
9248 if Is_Tagged_Type (Typ) then
9249 Call :=
9250 Make_Function_Call (Loc,
9251 Name => New_Occurrence_Of (Func_Id, Loc),
9252 Parameter_Associations =>
9253 New_List (Convert_To (Typ, Relocate_Node (Expr))));
9254 else
9255 Call :=
9256 Make_Function_Call (Loc,
9257 Name => New_Occurrence_Of (Func_Id, Loc),
9258 Parameter_Associations => New_List (Relocate_Node (Expr)));
9259 end if;
9261 Restore_Ghost_Mode (Saved_GM);
9263 return Call;
9264 end Make_Predicate_Call;
9266 --------------------------
9267 -- Make_Predicate_Check --
9268 --------------------------
9270 function Make_Predicate_Check
9271 (Typ : Entity_Id;
9272 Expr : Node_Id) return Node_Id
9274 procedure Replace_Subtype_Reference (N : Node_Id);
9275 -- Replace current occurrences of the subtype to which a dynamic
9276 -- predicate applies, by the expression that triggers a predicate
9277 -- check. This is needed for aspect Predicate_Failure, for which
9278 -- we do not generate a wrapper procedure, but simply modify the
9279 -- expression for the pragma of the predicate check.
9281 --------------------------------
9282 -- Replace_Subtype_Reference --
9283 --------------------------------
9285 procedure Replace_Subtype_Reference (N : Node_Id) is
9286 begin
9287 Rewrite (N, New_Copy_Tree (Expr));
9289 -- We want to treat the node as if it comes from source, so
9290 -- that ASIS will not ignore it.
9292 Set_Comes_From_Source (N, True);
9293 end Replace_Subtype_Reference;
9295 procedure Replace_Subtype_References is
9296 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9298 -- Local variables
9300 Loc : constant Source_Ptr := Sloc (Expr);
9301 Arg_List : List_Id;
9302 Fail_Expr : Node_Id;
9303 Nam : Name_Id;
9305 -- Start of processing for Make_Predicate_Check
9307 begin
9308 -- If predicate checks are suppressed, then return a null statement. For
9309 -- this call, we check only the scope setting. If the caller wants to
9310 -- check a specific entity's setting, they must do it manually.
9312 if Predicate_Checks_Suppressed (Empty) then
9313 return Make_Null_Statement (Loc);
9314 end if;
9316 -- Do not generate a check within an internal subprogram (stream
9317 -- functions and the like, including including predicate functions).
9319 if Within_Internal_Subprogram then
9320 return Make_Null_Statement (Loc);
9321 end if;
9323 -- Compute proper name to use, we need to get this right so that the
9324 -- right set of check policies apply to the Check pragma we are making.
9326 if Has_Dynamic_Predicate_Aspect (Typ) then
9327 Nam := Name_Dynamic_Predicate;
9328 elsif Has_Static_Predicate_Aspect (Typ) then
9329 Nam := Name_Static_Predicate;
9330 else
9331 Nam := Name_Predicate;
9332 end if;
9334 Arg_List := New_List (
9335 Make_Pragma_Argument_Association (Loc,
9336 Expression => Make_Identifier (Loc, Nam)),
9337 Make_Pragma_Argument_Association (Loc,
9338 Expression => Make_Predicate_Call (Typ, Expr)));
9340 -- If subtype has Predicate_Failure defined, add the correponding
9341 -- expression as an additional pragma parameter, after replacing
9342 -- current instances with the expression being checked.
9344 if Has_Aspect (Typ, Aspect_Predicate_Failure) then
9345 Fail_Expr :=
9346 New_Copy_Tree
9347 (Expression (Find_Aspect (Typ, Aspect_Predicate_Failure)));
9348 Replace_Subtype_References (Fail_Expr, Typ);
9350 Append_To (Arg_List,
9351 Make_Pragma_Argument_Association (Loc,
9352 Expression => Fail_Expr));
9353 end if;
9355 return
9356 Make_Pragma (Loc,
9357 Chars => Name_Check,
9358 Pragma_Argument_Associations => Arg_List);
9359 end Make_Predicate_Check;
9361 ----------------------------
9362 -- Make_Subtype_From_Expr --
9363 ----------------------------
9365 -- 1. If Expr is an unconstrained array expression, creates
9366 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9368 -- 2. If Expr is a unconstrained discriminated type expression, creates
9369 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9371 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9373 function Make_Subtype_From_Expr
9374 (E : Node_Id;
9375 Unc_Typ : Entity_Id;
9376 Related_Id : Entity_Id := Empty) return Node_Id
9378 List_Constr : constant List_Id := New_List;
9379 Loc : constant Source_Ptr := Sloc (E);
9380 D : Entity_Id;
9381 Full_Exp : Node_Id;
9382 Full_Subtyp : Entity_Id;
9383 High_Bound : Entity_Id;
9384 Index_Typ : Entity_Id;
9385 Low_Bound : Entity_Id;
9386 Priv_Subtyp : Entity_Id;
9387 Utyp : Entity_Id;
9389 begin
9390 if Is_Private_Type (Unc_Typ)
9391 and then Has_Unknown_Discriminants (Unc_Typ)
9392 then
9393 -- The caller requests a unique external name for both the private
9394 -- and the full subtype.
9396 if Present (Related_Id) then
9397 Full_Subtyp :=
9398 Make_Defining_Identifier (Loc,
9399 Chars => New_External_Name (Chars (Related_Id), 'C'));
9400 Priv_Subtyp :=
9401 Make_Defining_Identifier (Loc,
9402 Chars => New_External_Name (Chars (Related_Id), 'P'));
9404 else
9405 Full_Subtyp := Make_Temporary (Loc, 'C');
9406 Priv_Subtyp := Make_Temporary (Loc, 'P');
9407 end if;
9409 -- Prepare the subtype completion. Use the base type to find the
9410 -- underlying type because the type may be a generic actual or an
9411 -- explicit subtype.
9413 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9415 Full_Exp :=
9416 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9417 Set_Parent (Full_Exp, Parent (E));
9419 Insert_Action (E,
9420 Make_Subtype_Declaration (Loc,
9421 Defining_Identifier => Full_Subtyp,
9422 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9424 -- Define the dummy private subtype
9426 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9427 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9428 Set_Scope (Priv_Subtyp, Full_Subtyp);
9429 Set_Is_Constrained (Priv_Subtyp);
9430 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9431 Set_Is_Itype (Priv_Subtyp);
9432 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9434 if Is_Tagged_Type (Priv_Subtyp) then
9435 Set_Class_Wide_Type
9436 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9437 Set_Direct_Primitive_Operations (Priv_Subtyp,
9438 Direct_Primitive_Operations (Unc_Typ));
9439 end if;
9441 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9443 return New_Occurrence_Of (Priv_Subtyp, Loc);
9445 elsif Is_Array_Type (Unc_Typ) then
9446 Index_Typ := First_Index (Unc_Typ);
9447 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9449 -- Capture the bounds of each index constraint in case the context
9450 -- is an object declaration of an unconstrained type initialized
9451 -- by a function call:
9453 -- Obj : Unconstr_Typ := Func_Call;
9455 -- This scenario requires secondary scope management and the index
9456 -- constraint cannot depend on the temporary used to capture the
9457 -- result of the function call.
9459 -- SS_Mark;
9460 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9461 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9462 -- Obj : S := Temp.all;
9463 -- SS_Release; -- Temp is gone at this point, bounds of S are
9464 -- -- non existent.
9466 -- Generate:
9467 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9469 Low_Bound := Make_Temporary (Loc, 'B');
9470 Insert_Action (E,
9471 Make_Object_Declaration (Loc,
9472 Defining_Identifier => Low_Bound,
9473 Object_Definition =>
9474 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9475 Constant_Present => True,
9476 Expression =>
9477 Make_Attribute_Reference (Loc,
9478 Prefix => Duplicate_Subexpr_No_Checks (E),
9479 Attribute_Name => Name_First,
9480 Expressions => New_List (
9481 Make_Integer_Literal (Loc, J)))));
9483 -- Generate:
9484 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9486 High_Bound := Make_Temporary (Loc, 'B');
9487 Insert_Action (E,
9488 Make_Object_Declaration (Loc,
9489 Defining_Identifier => High_Bound,
9490 Object_Definition =>
9491 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9492 Constant_Present => True,
9493 Expression =>
9494 Make_Attribute_Reference (Loc,
9495 Prefix => Duplicate_Subexpr_No_Checks (E),
9496 Attribute_Name => Name_Last,
9497 Expressions => New_List (
9498 Make_Integer_Literal (Loc, J)))));
9500 Append_To (List_Constr,
9501 Make_Range (Loc,
9502 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9503 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9505 Index_Typ := Next_Index (Index_Typ);
9506 end loop;
9508 elsif Is_Class_Wide_Type (Unc_Typ) then
9509 declare
9510 CW_Subtype : Entity_Id;
9511 EQ_Typ : Entity_Id := Empty;
9513 begin
9514 -- A class-wide equivalent type is not needed on VM targets
9515 -- because the VM back-ends handle the class-wide object
9516 -- initialization itself (and doesn't need or want the
9517 -- additional intermediate type to handle the assignment).
9519 if Expander_Active and then Tagged_Type_Expansion then
9521 -- If this is the class-wide type of a completion that is a
9522 -- record subtype, set the type of the class-wide type to be
9523 -- the full base type, for use in the expanded code for the
9524 -- equivalent type. Should this be done earlier when the
9525 -- completion is analyzed ???
9527 if Is_Private_Type (Etype (Unc_Typ))
9528 and then
9529 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9530 then
9531 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9532 end if;
9534 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9535 end if;
9537 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9538 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9539 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9541 return New_Occurrence_Of (CW_Subtype, Loc);
9542 end;
9544 -- Indefinite record type with discriminants
9546 else
9547 D := First_Discriminant (Unc_Typ);
9548 while Present (D) loop
9549 Append_To (List_Constr,
9550 Make_Selected_Component (Loc,
9551 Prefix => Duplicate_Subexpr_No_Checks (E),
9552 Selector_Name => New_Occurrence_Of (D, Loc)));
9554 Next_Discriminant (D);
9555 end loop;
9556 end if;
9558 return
9559 Make_Subtype_Indication (Loc,
9560 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9561 Constraint =>
9562 Make_Index_Or_Discriminant_Constraint (Loc,
9563 Constraints => List_Constr));
9564 end Make_Subtype_From_Expr;
9566 ---------------
9567 -- Map_Types --
9568 ---------------
9570 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9572 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9573 -- avoid deep indentation of code.
9575 -- NOTE: Routines which deal with discriminant mapping operate on the
9576 -- [underlying/record] full view of various types because those views
9577 -- contain all discriminants and stored constraints.
9579 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9580 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9581 -- overriding chain starting from Prim whose dispatching type is parent
9582 -- type Par_Typ and add a mapping between the result and primitive Prim.
9584 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9585 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9586 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9587 -- if no such primitive is available.
9589 function Build_Chain
9590 (Par_Typ : Entity_Id;
9591 Deriv_Typ : Entity_Id) return Elist_Id;
9592 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9593 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9594 -- list has the form:
9596 -- head tail
9597 -- v v
9598 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9600 -- Note that Par_Typ is not part of the resulting derivation chain
9602 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9603 -- Return the view of type Typ which could potentially contains either
9604 -- the discriminants or stored constraints of the type.
9606 function Find_Discriminant_Value
9607 (Discr : Entity_Id;
9608 Par_Typ : Entity_Id;
9609 Deriv_Typ : Entity_Id;
9610 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9611 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9612 -- in the derivation chain starting from parent type Par_Typ leading to
9613 -- derived type Deriv_Typ. The returned value is one of the following:
9615 -- * An entity which is either a discriminant or a non-discriminant
9616 -- name, and renames/constraints Discr.
9618 -- * An expression which constraints Discr
9620 -- Typ_Elmt is an element of the derivation chain created by routine
9621 -- Build_Chain and denotes the current ancestor being examined.
9623 procedure Map_Discriminants
9624 (Par_Typ : Entity_Id;
9625 Deriv_Typ : Entity_Id);
9626 -- Map each discriminant of type Par_Typ to a meaningful constraint
9627 -- from the point of view of type Deriv_Typ.
9629 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9630 -- Map each primitive of type Par_Typ to a corresponding primitive of
9631 -- type Deriv_Typ.
9633 -------------------
9634 -- Add_Primitive --
9635 -------------------
9637 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9638 Par_Prim : Entity_Id;
9640 begin
9641 -- Inspect the inheritance chain through the Alias attribute and the
9642 -- overriding chain through the Overridden_Operation looking for an
9643 -- ancestor primitive with the appropriate dispatching type.
9645 Par_Prim := Prim;
9646 while Present (Par_Prim) loop
9647 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9648 Par_Prim := Ancestor_Primitive (Par_Prim);
9649 end loop;
9651 -- Create a mapping of the form:
9653 -- parent type primitive -> derived type primitive
9655 if Present (Par_Prim) then
9656 Type_Map.Set (Par_Prim, Prim);
9657 end if;
9658 end Add_Primitive;
9660 ------------------------
9661 -- Ancestor_Primitive --
9662 ------------------------
9664 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9665 Inher_Prim : constant Entity_Id := Alias (Subp);
9666 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9668 begin
9669 -- The current subprogram overrides an ancestor primitive
9671 if Present (Over_Prim) then
9672 return Over_Prim;
9674 -- The current subprogram is an internally generated alias of an
9675 -- inherited ancestor primitive.
9677 elsif Present (Inher_Prim) then
9678 return Inher_Prim;
9680 -- Otherwise the current subprogram is the root of the inheritance or
9681 -- overriding chain.
9683 else
9684 return Empty;
9685 end if;
9686 end Ancestor_Primitive;
9688 -----------------
9689 -- Build_Chain --
9690 -----------------
9692 function Build_Chain
9693 (Par_Typ : Entity_Id;
9694 Deriv_Typ : Entity_Id) return Elist_Id
9696 Anc_Typ : Entity_Id;
9697 Chain : Elist_Id;
9698 Curr_Typ : Entity_Id;
9700 begin
9701 Chain := New_Elmt_List;
9703 -- Add the derived type to the derivation chain
9705 Prepend_Elmt (Deriv_Typ, Chain);
9707 -- Examine all ancestors starting from the derived type climbing
9708 -- towards parent type Par_Typ.
9710 Curr_Typ := Deriv_Typ;
9711 loop
9712 -- Handle the case where the current type is a record which
9713 -- derives from a subtype.
9715 -- subtype Sub_Typ is Par_Typ ...
9716 -- type Deriv_Typ is Sub_Typ ...
9718 if Ekind (Curr_Typ) = E_Record_Type
9719 and then Present (Parent_Subtype (Curr_Typ))
9720 then
9721 Anc_Typ := Parent_Subtype (Curr_Typ);
9723 -- Handle the case where the current type is a record subtype of
9724 -- another subtype.
9726 -- subtype Sub_Typ1 is Par_Typ ...
9727 -- subtype Sub_Typ2 is Sub_Typ1 ...
9729 elsif Ekind (Curr_Typ) = E_Record_Subtype
9730 and then Present (Cloned_Subtype (Curr_Typ))
9731 then
9732 Anc_Typ := Cloned_Subtype (Curr_Typ);
9734 -- Otherwise use the direct parent type
9736 else
9737 Anc_Typ := Etype (Curr_Typ);
9738 end if;
9740 -- Use the first subtype when dealing with itypes
9742 if Is_Itype (Anc_Typ) then
9743 Anc_Typ := First_Subtype (Anc_Typ);
9744 end if;
9746 -- Work with the view which contains the discriminants and stored
9747 -- constraints.
9749 Anc_Typ := Discriminated_View (Anc_Typ);
9751 -- Stop the climb when either the parent type has been reached or
9752 -- there are no more ancestors left to examine.
9754 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9756 Prepend_Unique_Elmt (Anc_Typ, Chain);
9757 Curr_Typ := Anc_Typ;
9758 end loop;
9760 return Chain;
9761 end Build_Chain;
9763 ------------------------
9764 -- Discriminated_View --
9765 ------------------------
9767 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9768 T : Entity_Id;
9770 begin
9771 T := Typ;
9773 -- Use the [underlying] full view when dealing with private types
9774 -- because the view contains all inherited discriminants or stored
9775 -- constraints.
9777 if Is_Private_Type (T) then
9778 if Present (Underlying_Full_View (T)) then
9779 T := Underlying_Full_View (T);
9781 elsif Present (Full_View (T)) then
9782 T := Full_View (T);
9783 end if;
9784 end if;
9786 -- Use the underlying record view when the type is an extenstion of
9787 -- a parent type with unknown discriminants because the view contains
9788 -- all inherited discriminants or stored constraints.
9790 if Ekind (T) = E_Record_Type
9791 and then Present (Underlying_Record_View (T))
9792 then
9793 T := Underlying_Record_View (T);
9794 end if;
9796 return T;
9797 end Discriminated_View;
9799 -----------------------------
9800 -- Find_Discriminant_Value --
9801 -----------------------------
9803 function Find_Discriminant_Value
9804 (Discr : Entity_Id;
9805 Par_Typ : Entity_Id;
9806 Deriv_Typ : Entity_Id;
9807 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9809 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9810 Typ : constant Entity_Id := Node (Typ_Elmt);
9812 function Find_Constraint_Value
9813 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
9814 -- Given constraint Constr, find what it denotes. This is either:
9816 -- * An entity which is either a discriminant or a name
9818 -- * An expression
9820 ---------------------------
9821 -- Find_Constraint_Value --
9822 ---------------------------
9824 function Find_Constraint_Value
9825 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
9827 begin
9828 if Nkind (Constr) in N_Entity then
9830 -- The constraint denotes a discriminant of the curren type
9831 -- which renames the ancestor discriminant:
9833 -- vv
9834 -- type Typ (D1 : ...; DN : ...) is
9835 -- new Anc (Discr => D1) with ...
9836 -- ^^
9838 if Ekind (Constr) = E_Discriminant then
9840 -- The discriminant belongs to derived type Deriv_Typ. This
9841 -- is the final value for the ancestor discriminant as the
9842 -- derivations chain has been fully exhausted.
9844 if Typ = Deriv_Typ then
9845 return Constr;
9847 -- Otherwise the discriminant may be renamed or constrained
9848 -- at a lower level. Continue looking down the derivation
9849 -- chain.
9851 else
9852 return
9853 Find_Discriminant_Value
9854 (Discr => Constr,
9855 Par_Typ => Par_Typ,
9856 Deriv_Typ => Deriv_Typ,
9857 Typ_Elmt => Next_Elmt (Typ_Elmt));
9858 end if;
9860 -- Otherwise the constraint denotes a reference to some name
9861 -- which results in a Girder discriminant:
9863 -- vvvv
9864 -- Name : ...;
9865 -- type Typ (D1 : ...; DN : ...) is
9866 -- new Anc (Discr => Name) with ...
9867 -- ^^^^
9869 -- Return the name as this is the proper constraint of the
9870 -- discriminant.
9872 else
9873 return Constr;
9874 end if;
9876 -- The constraint denotes a reference to a name
9878 elsif Is_Entity_Name (Constr) then
9879 return Find_Constraint_Value (Entity (Constr));
9881 -- Otherwise the current constraint is an expression which yields
9882 -- a Girder discriminant:
9884 -- type Typ (D1 : ...; DN : ...) is
9885 -- new Anc (Discr => <expression>) with ...
9886 -- ^^^^^^^^^^
9888 -- Return the expression as this is the proper constraint of the
9889 -- discriminant.
9891 else
9892 return Constr;
9893 end if;
9894 end Find_Constraint_Value;
9896 -- Local variables
9898 Constrs : constant Elist_Id := Stored_Constraint (Typ);
9900 Constr_Elmt : Elmt_Id;
9901 Pos : Uint;
9902 Typ_Discr : Entity_Id;
9904 -- Start of processing for Find_Discriminant_Value
9906 begin
9907 -- The algorithm for finding the value of a discriminant works as
9908 -- follows. First, it recreates the derivation chain from Par_Typ
9909 -- to Deriv_Typ as a list:
9911 -- Par_Typ (shown for completeness)
9912 -- v
9913 -- Ancestor_N <-- head of chain
9914 -- v
9915 -- Ancestor_1
9916 -- v
9917 -- Deriv_Typ <-- tail of chain
9919 -- The algorithm then traces the fate of a parent discriminant down
9920 -- the derivation chain. At each derivation level, the discriminant
9921 -- may be either inherited or constrained.
9923 -- 1) Discriminant is inherited: there are two cases, depending on
9924 -- which type is inheriting.
9926 -- 1.1) Deriv_Typ is inheriting:
9928 -- type Ancestor (D_1 : ...) is tagged ...
9929 -- type Deriv_Typ is new Ancestor ...
9931 -- In this case the inherited discriminant is the final value of
9932 -- the parent discriminant because the end of the derivation chain
9933 -- has been reached.
9935 -- 1.2) Some other type is inheriting:
9937 -- type Ancestor_1 (D_1 : ...) is tagged ...
9938 -- type Ancestor_2 is new Ancestor_1 ...
9940 -- In this case the algorithm continues to trace the fate of the
9941 -- inherited discriminant down the derivation chain because it may
9942 -- be further inherited or constrained.
9944 -- 2) Discriminant is constrained: there are three cases, depending
9945 -- on what the constraint is.
9947 -- 2.1) The constraint is another discriminant (aka renaming):
9949 -- type Ancestor_1 (D_1 : ...) is tagged ...
9950 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
9952 -- In this case the constraining discriminant becomes the one to
9953 -- track down the derivation chain. The algorithm already knows
9954 -- that D_2 constrains D_1, therefore if the algorithm finds the
9955 -- value of D_2, then this would also be the value for D_1.
9957 -- 2.2) The constraint is a name (aka Girder):
9959 -- Name : ...
9960 -- type Ancestor_1 (D_1 : ...) is tagged ...
9961 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
9963 -- In this case the name is the final value of D_1 because the
9964 -- discriminant cannot be further constrained.
9966 -- 2.3) The constraint is an expression (aka Girder):
9968 -- type Ancestor_1 (D_1 : ...) is tagged ...
9969 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
9971 -- Similar to 2.2, the expression is the final value of D_1
9973 Pos := Uint_1;
9975 -- When a derived type constrains its parent type, all constaints
9976 -- appear in the Stored_Constraint list. Examine the list looking
9977 -- for a positional match.
9979 if Present (Constrs) then
9980 Constr_Elmt := First_Elmt (Constrs);
9981 while Present (Constr_Elmt) loop
9983 -- The position of the current constraint matches that of the
9984 -- ancestor discriminant.
9986 if Pos = Discr_Pos then
9987 return Find_Constraint_Value (Node (Constr_Elmt));
9988 end if;
9990 Next_Elmt (Constr_Elmt);
9991 Pos := Pos + 1;
9992 end loop;
9994 -- Otherwise the derived type does not constraint its parent type in
9995 -- which case it inherits the parent discriminants.
9997 else
9998 Typ_Discr := First_Discriminant (Typ);
9999 while Present (Typ_Discr) loop
10001 -- The position of the current discriminant matches that of the
10002 -- ancestor discriminant.
10004 if Pos = Discr_Pos then
10005 return Find_Constraint_Value (Typ_Discr);
10006 end if;
10008 Next_Discriminant (Typ_Discr);
10009 Pos := Pos + 1;
10010 end loop;
10011 end if;
10013 -- A discriminant must always have a corresponding value. This is
10014 -- either another discriminant, a name, or an expression. If this
10015 -- point is reached, them most likely the derivation chain employs
10016 -- the wrong views of types.
10018 pragma Assert (False);
10020 return Empty;
10021 end Find_Discriminant_Value;
10023 -----------------------
10024 -- Map_Discriminants --
10025 -----------------------
10027 procedure Map_Discriminants
10028 (Par_Typ : Entity_Id;
10029 Deriv_Typ : Entity_Id)
10031 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10033 Discr : Entity_Id;
10034 Discr_Val : Node_Or_Entity_Id;
10036 begin
10037 -- Examine each discriminant of parent type Par_Typ and find a
10038 -- suitable value for it from the point of view of derived type
10039 -- Deriv_Typ.
10041 if Has_Discriminants (Par_Typ) then
10042 Discr := First_Discriminant (Par_Typ);
10043 while Present (Discr) loop
10044 Discr_Val :=
10045 Find_Discriminant_Value
10046 (Discr => Discr,
10047 Par_Typ => Par_Typ,
10048 Deriv_Typ => Deriv_Typ,
10049 Typ_Elmt => First_Elmt (Deriv_Chain));
10051 -- Create a mapping of the form:
10053 -- parent type discriminant -> value
10055 Type_Map.Set (Discr, Discr_Val);
10057 Next_Discriminant (Discr);
10058 end loop;
10059 end if;
10060 end Map_Discriminants;
10062 --------------------
10063 -- Map_Primitives --
10064 --------------------
10066 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10067 Deriv_Prim : Entity_Id;
10068 Par_Prim : Entity_Id;
10069 Par_Prims : Elist_Id;
10070 Prim_Elmt : Elmt_Id;
10072 begin
10073 -- Inspect the primitives of the derived type and determine whether
10074 -- they relate to the primitives of the parent type. If there is a
10075 -- meaningful relation, create a mapping of the form:
10077 -- parent type primitive -> perived type primitive
10079 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10080 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10081 while Present (Prim_Elmt) loop
10082 Deriv_Prim := Node (Prim_Elmt);
10084 if Is_Subprogram (Deriv_Prim)
10085 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10086 then
10087 Add_Primitive (Deriv_Prim, Par_Typ);
10088 end if;
10090 Next_Elmt (Prim_Elmt);
10091 end loop;
10092 end if;
10094 -- If the parent operation is an interface operation, the overriding
10095 -- indicator is not present. Instead, we get from the interface
10096 -- operation the primitive of the current type that implements it.
10098 if Is_Interface (Par_Typ) then
10099 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10101 if Present (Par_Prims) then
10102 Prim_Elmt := First_Elmt (Par_Prims);
10104 while Present (Prim_Elmt) loop
10105 Par_Prim := Node (Prim_Elmt);
10106 Deriv_Prim :=
10107 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10109 if Present (Deriv_Prim) then
10110 Type_Map.Set (Par_Prim, Deriv_Prim);
10111 end if;
10113 Next_Elmt (Prim_Elmt);
10114 end loop;
10115 end if;
10116 end if;
10117 end Map_Primitives;
10119 -- Start of processing for Map_Types
10121 begin
10122 -- Nothing to do if there are no types to work with
10124 if No (Parent_Type) or else No (Derived_Type) then
10125 return;
10127 -- Nothing to do if the mapping already exists
10129 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10130 return;
10132 -- Nothing to do if both types are not tagged. Note that untagged types
10133 -- do not have primitive operations and their discriminants are already
10134 -- handled by gigi.
10136 elsif not Is_Tagged_Type (Parent_Type)
10137 or else not Is_Tagged_Type (Derived_Type)
10138 then
10139 return;
10140 end if;
10142 -- Create a mapping of the form
10144 -- parent type -> derived type
10146 -- to prevent any subsequent attempts to produce the same relations
10148 Type_Map.Set (Parent_Type, Derived_Type);
10150 -- Create mappings of the form
10152 -- parent type discriminant -> derived type discriminant
10153 -- <or>
10154 -- parent type discriminant -> constraint
10156 -- Note that mapping of discriminants breaks privacy because it needs to
10157 -- work with those views which contains the discriminants and any stored
10158 -- constraints.
10160 Map_Discriminants
10161 (Par_Typ => Discriminated_View (Parent_Type),
10162 Deriv_Typ => Discriminated_View (Derived_Type));
10164 -- Create mappings of the form
10166 -- parent type primitive -> derived type primitive
10168 Map_Primitives
10169 (Par_Typ => Parent_Type,
10170 Deriv_Typ => Derived_Type);
10171 end Map_Types;
10173 ----------------------------
10174 -- Matching_Standard_Type --
10175 ----------------------------
10177 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10178 pragma Assert (Is_Scalar_Type (Typ));
10179 Siz : constant Uint := Esize (Typ);
10181 begin
10182 -- Floating-point cases
10184 if Is_Floating_Point_Type (Typ) then
10185 if Siz <= Esize (Standard_Short_Float) then
10186 return Standard_Short_Float;
10187 elsif Siz <= Esize (Standard_Float) then
10188 return Standard_Float;
10189 elsif Siz <= Esize (Standard_Long_Float) then
10190 return Standard_Long_Float;
10191 elsif Siz <= Esize (Standard_Long_Long_Float) then
10192 return Standard_Long_Long_Float;
10193 else
10194 raise Program_Error;
10195 end if;
10197 -- Integer cases (includes fixed-point types)
10199 -- Unsigned integer cases (includes normal enumeration types)
10201 elsif Is_Unsigned_Type (Typ) then
10202 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10203 return Standard_Short_Short_Unsigned;
10204 elsif Siz <= Esize (Standard_Short_Unsigned) then
10205 return Standard_Short_Unsigned;
10206 elsif Siz <= Esize (Standard_Unsigned) then
10207 return Standard_Unsigned;
10208 elsif Siz <= Esize (Standard_Long_Unsigned) then
10209 return Standard_Long_Unsigned;
10210 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10211 return Standard_Long_Long_Unsigned;
10212 else
10213 raise Program_Error;
10214 end if;
10216 -- Signed integer cases
10218 else
10219 if Siz <= Esize (Standard_Short_Short_Integer) then
10220 return Standard_Short_Short_Integer;
10221 elsif Siz <= Esize (Standard_Short_Integer) then
10222 return Standard_Short_Integer;
10223 elsif Siz <= Esize (Standard_Integer) then
10224 return Standard_Integer;
10225 elsif Siz <= Esize (Standard_Long_Integer) then
10226 return Standard_Long_Integer;
10227 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10228 return Standard_Long_Long_Integer;
10229 else
10230 raise Program_Error;
10231 end if;
10232 end if;
10233 end Matching_Standard_Type;
10235 -----------------------------
10236 -- May_Generate_Large_Temp --
10237 -----------------------------
10239 -- At the current time, the only types that we return False for (i.e. where
10240 -- we decide we know they cannot generate large temps) are ones where we
10241 -- know the size is 256 bits or less at compile time, and we are still not
10242 -- doing a thorough job on arrays and records ???
10244 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10245 begin
10246 if not Size_Known_At_Compile_Time (Typ) then
10247 return False;
10249 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10250 return False;
10252 elsif Is_Array_Type (Typ)
10253 and then Present (Packed_Array_Impl_Type (Typ))
10254 then
10255 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10257 -- We could do more here to find other small types ???
10259 else
10260 return True;
10261 end if;
10262 end May_Generate_Large_Temp;
10264 ------------------------
10265 -- Needs_Finalization --
10266 ------------------------
10268 function Needs_Finalization (Typ : Entity_Id) return Boolean is
10269 function Has_Some_Controlled_Component
10270 (Input_Typ : Entity_Id) return Boolean;
10271 -- Determine whether type Input_Typ has at least one controlled
10272 -- component.
10274 -----------------------------------
10275 -- Has_Some_Controlled_Component --
10276 -----------------------------------
10278 function Has_Some_Controlled_Component
10279 (Input_Typ : Entity_Id) return Boolean
10281 Comp : Entity_Id;
10283 begin
10284 -- When a type is already frozen and has at least one controlled
10285 -- component, or is manually decorated, it is sufficient to inspect
10286 -- flag Has_Controlled_Component.
10288 if Has_Controlled_Component (Input_Typ) then
10289 return True;
10291 -- Otherwise inspect the internals of the type
10293 elsif not Is_Frozen (Input_Typ) then
10294 if Is_Array_Type (Input_Typ) then
10295 return Needs_Finalization (Component_Type (Input_Typ));
10297 elsif Is_Record_Type (Input_Typ) then
10298 Comp := First_Component (Input_Typ);
10299 while Present (Comp) loop
10300 if Needs_Finalization (Etype (Comp)) then
10301 return True;
10302 end if;
10304 Next_Component (Comp);
10305 end loop;
10306 end if;
10307 end if;
10309 return False;
10310 end Has_Some_Controlled_Component;
10312 -- Start of processing for Needs_Finalization
10314 begin
10315 -- Certain run-time configurations and targets do not provide support
10316 -- for controlled types.
10318 if Restriction_Active (No_Finalization) then
10319 return False;
10321 -- C++ types are not considered controlled. It is assumed that the non-
10322 -- Ada side will handle their clean up.
10324 elsif Convention (Typ) = Convention_CPP then
10325 return False;
10327 -- Class-wide types are treated as controlled because derivations from
10328 -- the root type may introduce controlled components.
10330 elsif Is_Class_Wide_Type (Typ) then
10331 return True;
10333 -- Concurrent types are controlled as long as their corresponding record
10334 -- is controlled.
10336 elsif Is_Concurrent_Type (Typ)
10337 and then Present (Corresponding_Record_Type (Typ))
10338 and then Needs_Finalization (Corresponding_Record_Type (Typ))
10339 then
10340 return True;
10342 -- Otherwise the type is controlled when it is either derived from type
10343 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10344 -- contains at least one controlled component.
10346 else
10347 return
10348 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
10349 end if;
10350 end Needs_Finalization;
10352 ----------------------------
10353 -- Needs_Constant_Address --
10354 ----------------------------
10356 function Needs_Constant_Address
10357 (Decl : Node_Id;
10358 Typ : Entity_Id) return Boolean
10360 begin
10361 -- If we have no initialization of any kind, then we don't need to place
10362 -- any restrictions on the address clause, because the object will be
10363 -- elaborated after the address clause is evaluated. This happens if the
10364 -- declaration has no initial expression, or the type has no implicit
10365 -- initialization, or the object is imported.
10367 -- The same holds for all initialized scalar types and all access types.
10368 -- Packed bit arrays of size up to 64 are represented using a modular
10369 -- type with an initialization (to zero) and can be processed like other
10370 -- initialized scalar types.
10372 -- If the type is controlled, code to attach the object to a
10373 -- finalization chain is generated at the point of declaration, and
10374 -- therefore the elaboration of the object cannot be delayed: the
10375 -- address expression must be a constant.
10377 if No (Expression (Decl))
10378 and then not Needs_Finalization (Typ)
10379 and then
10380 (not Has_Non_Null_Base_Init_Proc (Typ)
10381 or else Is_Imported (Defining_Identifier (Decl)))
10382 then
10383 return False;
10385 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10386 or else Is_Access_Type (Typ)
10387 or else
10388 (Is_Bit_Packed_Array (Typ)
10389 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10390 then
10391 return False;
10393 else
10395 -- Otherwise, we require the address clause to be constant because
10396 -- the call to the initialization procedure (or the attach code) has
10397 -- to happen at the point of the declaration.
10399 -- Actually the IP call has been moved to the freeze actions anyway,
10400 -- so maybe we can relax this restriction???
10402 return True;
10403 end if;
10404 end Needs_Constant_Address;
10406 ----------------------------
10407 -- New_Class_Wide_Subtype --
10408 ----------------------------
10410 function New_Class_Wide_Subtype
10411 (CW_Typ : Entity_Id;
10412 N : Node_Id) return Entity_Id
10414 Res : constant Entity_Id := Create_Itype (E_Void, N);
10415 Res_Name : constant Name_Id := Chars (Res);
10416 Res_Scope : constant Entity_Id := Scope (Res);
10418 begin
10419 Copy_Node (CW_Typ, Res);
10420 Set_Comes_From_Source (Res, False);
10421 Set_Sloc (Res, Sloc (N));
10422 Set_Is_Itype (Res);
10423 Set_Associated_Node_For_Itype (Res, N);
10424 Set_Is_Public (Res, False); -- By default, may be changed below.
10425 Set_Public_Status (Res);
10426 Set_Chars (Res, Res_Name);
10427 Set_Scope (Res, Res_Scope);
10428 Set_Ekind (Res, E_Class_Wide_Subtype);
10429 Set_Next_Entity (Res, Empty);
10430 Set_Etype (Res, Base_Type (CW_Typ));
10431 Set_Is_Frozen (Res, False);
10432 Set_Freeze_Node (Res, Empty);
10433 return (Res);
10434 end New_Class_Wide_Subtype;
10436 --------------------------------
10437 -- Non_Limited_Designated_Type --
10438 ---------------------------------
10440 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10441 Desig : constant Entity_Id := Designated_Type (T);
10442 begin
10443 if Has_Non_Limited_View (Desig) then
10444 return Non_Limited_View (Desig);
10445 else
10446 return Desig;
10447 end if;
10448 end Non_Limited_Designated_Type;
10450 -----------------------------------
10451 -- OK_To_Do_Constant_Replacement --
10452 -----------------------------------
10454 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10455 ES : constant Entity_Id := Scope (E);
10456 CS : Entity_Id;
10458 begin
10459 -- Do not replace statically allocated objects, because they may be
10460 -- modified outside the current scope.
10462 if Is_Statically_Allocated (E) then
10463 return False;
10465 -- Do not replace aliased or volatile objects, since we don't know what
10466 -- else might change the value.
10468 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10469 return False;
10471 -- Debug flag -gnatdM disconnects this optimization
10473 elsif Debug_Flag_MM then
10474 return False;
10476 -- Otherwise check scopes
10478 else
10479 CS := Current_Scope;
10481 loop
10482 -- If we are in right scope, replacement is safe
10484 if CS = ES then
10485 return True;
10487 -- Packages do not affect the determination of safety
10489 elsif Ekind (CS) = E_Package then
10490 exit when CS = Standard_Standard;
10491 CS := Scope (CS);
10493 -- Blocks do not affect the determination of safety
10495 elsif Ekind (CS) = E_Block then
10496 CS := Scope (CS);
10498 -- Loops do not affect the determination of safety. Note that we
10499 -- kill all current values on entry to a loop, so we are just
10500 -- talking about processing within a loop here.
10502 elsif Ekind (CS) = E_Loop then
10503 CS := Scope (CS);
10505 -- Otherwise, the reference is dubious, and we cannot be sure that
10506 -- it is safe to do the replacement.
10508 else
10509 exit;
10510 end if;
10511 end loop;
10513 return False;
10514 end if;
10515 end OK_To_Do_Constant_Replacement;
10517 ------------------------------------
10518 -- Possible_Bit_Aligned_Component --
10519 ------------------------------------
10521 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10522 begin
10523 -- Do not process an unanalyzed node because it is not yet decorated and
10524 -- most checks performed below will fail.
10526 if not Analyzed (N) then
10527 return False;
10528 end if;
10530 case Nkind (N) is
10532 -- Case of indexed component
10534 when N_Indexed_Component =>
10535 declare
10536 P : constant Node_Id := Prefix (N);
10537 Ptyp : constant Entity_Id := Etype (P);
10539 begin
10540 -- If we know the component size and it is less than 64, then
10541 -- we are definitely OK. The back end always does assignment of
10542 -- misaligned small objects correctly.
10544 if Known_Static_Component_Size (Ptyp)
10545 and then Component_Size (Ptyp) <= 64
10546 then
10547 return False;
10549 -- Otherwise, we need to test the prefix, to see if we are
10550 -- indexing from a possibly unaligned component.
10552 else
10553 return Possible_Bit_Aligned_Component (P);
10554 end if;
10555 end;
10557 -- Case of selected component
10559 when N_Selected_Component =>
10560 declare
10561 P : constant Node_Id := Prefix (N);
10562 Comp : constant Entity_Id := Entity (Selector_Name (N));
10564 begin
10565 -- If there is no component clause, then we are in the clear
10566 -- since the back end will never misalign a large component
10567 -- unless it is forced to do so. In the clear means we need
10568 -- only the recursive test on the prefix.
10570 if Component_May_Be_Bit_Aligned (Comp) then
10571 return True;
10572 else
10573 return Possible_Bit_Aligned_Component (P);
10574 end if;
10575 end;
10577 -- For a slice, test the prefix, if that is possibly misaligned,
10578 -- then for sure the slice is.
10580 when N_Slice =>
10581 return Possible_Bit_Aligned_Component (Prefix (N));
10583 -- For an unchecked conversion, check whether the expression may
10584 -- be bit-aligned.
10586 when N_Unchecked_Type_Conversion =>
10587 return Possible_Bit_Aligned_Component (Expression (N));
10589 -- If we have none of the above, it means that we have fallen off the
10590 -- top testing prefixes recursively, and we now have a stand alone
10591 -- object, where we don't have a problem, unless this is a renaming,
10592 -- in which case we need to look into the renamed object.
10594 when others =>
10595 if Is_Entity_Name (N)
10596 and then Present (Renamed_Object (Entity (N)))
10597 then
10598 return
10599 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10600 else
10601 return False;
10602 end if;
10603 end case;
10604 end Possible_Bit_Aligned_Component;
10606 -----------------------------------------------
10607 -- Process_Statements_For_Controlled_Objects --
10608 -----------------------------------------------
10610 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10611 Loc : constant Source_Ptr := Sloc (N);
10613 function Are_Wrapped (L : List_Id) return Boolean;
10614 -- Determine whether list L contains only one statement which is a block
10616 function Wrap_Statements_In_Block
10617 (L : List_Id;
10618 Scop : Entity_Id := Current_Scope) return Node_Id;
10619 -- Given a list of statements L, wrap it in a block statement and return
10620 -- the generated node. Scop is either the current scope or the scope of
10621 -- the context (if applicable).
10623 -----------------
10624 -- Are_Wrapped --
10625 -----------------
10627 function Are_Wrapped (L : List_Id) return Boolean is
10628 Stmt : constant Node_Id := First (L);
10629 begin
10630 return
10631 Present (Stmt)
10632 and then No (Next (Stmt))
10633 and then Nkind (Stmt) = N_Block_Statement;
10634 end Are_Wrapped;
10636 ------------------------------
10637 -- Wrap_Statements_In_Block --
10638 ------------------------------
10640 function Wrap_Statements_In_Block
10641 (L : List_Id;
10642 Scop : Entity_Id := Current_Scope) return Node_Id
10644 Block_Id : Entity_Id;
10645 Block_Nod : Node_Id;
10646 Iter_Loop : Entity_Id;
10648 begin
10649 Block_Nod :=
10650 Make_Block_Statement (Loc,
10651 Declarations => No_List,
10652 Handled_Statement_Sequence =>
10653 Make_Handled_Sequence_Of_Statements (Loc,
10654 Statements => L));
10656 -- Create a label for the block in case the block needs to manage the
10657 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10659 Add_Block_Identifier (Block_Nod, Block_Id);
10661 -- When wrapping the statements of an iterator loop, check whether
10662 -- the loop requires secondary stack management and if so, propagate
10663 -- the appropriate flags to the block. This ensures that the cursor
10664 -- is properly cleaned up at each iteration of the loop.
10666 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10668 if Present (Iter_Loop) then
10669 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10671 -- Secondary stack reclamation is suppressed when the associated
10672 -- iterator loop contains a return statement which uses the stack.
10674 Set_Sec_Stack_Needed_For_Return
10675 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10676 end if;
10678 return Block_Nod;
10679 end Wrap_Statements_In_Block;
10681 -- Local variables
10683 Block : Node_Id;
10685 -- Start of processing for Process_Statements_For_Controlled_Objects
10687 begin
10688 -- Whenever a non-handled statement list is wrapped in a block, the
10689 -- block must be explicitly analyzed to redecorate all entities in the
10690 -- list and ensure that a finalizer is properly built.
10692 case Nkind (N) is
10693 when N_Conditional_Entry_Call
10694 | N_Elsif_Part
10695 | N_If_Statement
10696 | N_Selective_Accept
10698 -- Check the "then statements" for elsif parts and if statements
10700 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10701 and then not Is_Empty_List (Then_Statements (N))
10702 and then not Are_Wrapped (Then_Statements (N))
10703 and then Requires_Cleanup_Actions
10704 (Then_Statements (N), False, False)
10705 then
10706 Block := Wrap_Statements_In_Block (Then_Statements (N));
10707 Set_Then_Statements (N, New_List (Block));
10709 Analyze (Block);
10710 end if;
10712 -- Check the "else statements" for conditional entry calls, if
10713 -- statements and selective accepts.
10715 if Nkind_In (N, N_Conditional_Entry_Call,
10716 N_If_Statement,
10717 N_Selective_Accept)
10718 and then not Is_Empty_List (Else_Statements (N))
10719 and then not Are_Wrapped (Else_Statements (N))
10720 and then Requires_Cleanup_Actions
10721 (Else_Statements (N), False, False)
10722 then
10723 Block := Wrap_Statements_In_Block (Else_Statements (N));
10724 Set_Else_Statements (N, New_List (Block));
10726 Analyze (Block);
10727 end if;
10729 when N_Abortable_Part
10730 | N_Accept_Alternative
10731 | N_Case_Statement_Alternative
10732 | N_Delay_Alternative
10733 | N_Entry_Call_Alternative
10734 | N_Exception_Handler
10735 | N_Loop_Statement
10736 | N_Triggering_Alternative
10738 if not Is_Empty_List (Statements (N))
10739 and then not Are_Wrapped (Statements (N))
10740 and then Requires_Cleanup_Actions (Statements (N), False, False)
10741 then
10742 if Nkind (N) = N_Loop_Statement
10743 and then Present (Identifier (N))
10744 then
10745 Block :=
10746 Wrap_Statements_In_Block
10747 (L => Statements (N),
10748 Scop => Entity (Identifier (N)));
10749 else
10750 Block := Wrap_Statements_In_Block (Statements (N));
10751 end if;
10753 Set_Statements (N, New_List (Block));
10754 Analyze (Block);
10755 end if;
10757 -- Could be e.g. a loop that was transformed into a block or null
10758 -- statement. Do nothing for terminate alternatives.
10760 when N_Block_Statement
10761 | N_Null_Statement
10762 | N_Terminate_Alternative
10764 null;
10766 when others =>
10767 raise Program_Error;
10768 end case;
10769 end Process_Statements_For_Controlled_Objects;
10771 ------------------
10772 -- Power_Of_Two --
10773 ------------------
10775 function Power_Of_Two (N : Node_Id) return Nat is
10776 Typ : constant Entity_Id := Etype (N);
10777 pragma Assert (Is_Integer_Type (Typ));
10779 Siz : constant Nat := UI_To_Int (Esize (Typ));
10780 Val : Uint;
10782 begin
10783 if not Compile_Time_Known_Value (N) then
10784 return 0;
10786 else
10787 Val := Expr_Value (N);
10788 for J in 1 .. Siz - 1 loop
10789 if Val = Uint_2 ** J then
10790 return J;
10791 end if;
10792 end loop;
10794 return 0;
10795 end if;
10796 end Power_Of_Two;
10798 ----------------------
10799 -- Remove_Init_Call --
10800 ----------------------
10802 function Remove_Init_Call
10803 (Var : Entity_Id;
10804 Rep_Clause : Node_Id) return Node_Id
10806 Par : constant Node_Id := Parent (Var);
10807 Typ : constant Entity_Id := Etype (Var);
10809 Init_Proc : Entity_Id;
10810 -- Initialization procedure for Typ
10812 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
10813 -- Look for init call for Var starting at From and scanning the
10814 -- enclosing list until Rep_Clause or the end of the list is reached.
10816 ----------------------------
10817 -- Find_Init_Call_In_List --
10818 ----------------------------
10820 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
10821 Init_Call : Node_Id;
10823 begin
10824 Init_Call := From;
10825 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
10826 if Nkind (Init_Call) = N_Procedure_Call_Statement
10827 and then Is_Entity_Name (Name (Init_Call))
10828 and then Entity (Name (Init_Call)) = Init_Proc
10829 then
10830 return Init_Call;
10831 end if;
10833 Next (Init_Call);
10834 end loop;
10836 return Empty;
10837 end Find_Init_Call_In_List;
10839 Init_Call : Node_Id;
10841 -- Start of processing for Find_Init_Call
10843 begin
10844 if Present (Initialization_Statements (Var)) then
10845 Init_Call := Initialization_Statements (Var);
10846 Set_Initialization_Statements (Var, Empty);
10848 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
10850 -- No init proc for the type, so obviously no call to be found
10852 return Empty;
10854 else
10855 -- We might be able to handle other cases below by just properly
10856 -- setting Initialization_Statements at the point where the init proc
10857 -- call is generated???
10859 Init_Proc := Base_Init_Proc (Typ);
10861 -- First scan the list containing the declaration of Var
10863 Init_Call := Find_Init_Call_In_List (From => Next (Par));
10865 -- If not found, also look on Var's freeze actions list, if any,
10866 -- since the init call may have been moved there (case of an address
10867 -- clause applying to Var).
10869 if No (Init_Call) and then Present (Freeze_Node (Var)) then
10870 Init_Call :=
10871 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
10872 end if;
10874 -- If the initialization call has actuals that use the secondary
10875 -- stack, the call may have been wrapped into a temporary block, in
10876 -- which case the block itself has to be removed.
10878 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
10879 declare
10880 Blk : constant Node_Id := Next (Par);
10881 begin
10882 if Present
10883 (Find_Init_Call_In_List
10884 (First (Statements (Handled_Statement_Sequence (Blk)))))
10885 then
10886 Init_Call := Blk;
10887 end if;
10888 end;
10889 end if;
10890 end if;
10892 if Present (Init_Call) then
10893 Remove (Init_Call);
10894 end if;
10895 return Init_Call;
10896 end Remove_Init_Call;
10898 -------------------------
10899 -- Remove_Side_Effects --
10900 -------------------------
10902 procedure Remove_Side_Effects
10903 (Exp : Node_Id;
10904 Name_Req : Boolean := False;
10905 Renaming_Req : Boolean := False;
10906 Variable_Ref : Boolean := False;
10907 Related_Id : Entity_Id := Empty;
10908 Is_Low_Bound : Boolean := False;
10909 Is_High_Bound : Boolean := False;
10910 Check_Side_Effects : Boolean := True)
10912 function Build_Temporary
10913 (Loc : Source_Ptr;
10914 Id : Character;
10915 Related_Nod : Node_Id := Empty) return Entity_Id;
10916 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
10917 -- is present (xxx is taken from the Chars field of Related_Nod),
10918 -- otherwise it generates an internal temporary. The created temporary
10919 -- entity is marked as internal.
10921 ---------------------
10922 -- Build_Temporary --
10923 ---------------------
10925 function Build_Temporary
10926 (Loc : Source_Ptr;
10927 Id : Character;
10928 Related_Nod : Node_Id := Empty) return Entity_Id
10930 Temp_Id : Entity_Id;
10931 Temp_Nam : Name_Id;
10933 begin
10934 -- The context requires an external symbol
10936 if Present (Related_Id) then
10937 if Is_Low_Bound then
10938 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
10939 else pragma Assert (Is_High_Bound);
10940 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
10941 end if;
10943 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
10945 -- Otherwise generate an internal temporary
10947 else
10948 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
10949 end if;
10951 Set_Is_Internal (Temp_Id);
10953 return Temp_Id;
10954 end Build_Temporary;
10956 -- Local variables
10958 Loc : constant Source_Ptr := Sloc (Exp);
10959 Exp_Type : constant Entity_Id := Etype (Exp);
10960 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
10961 Def_Id : Entity_Id;
10962 E : Node_Id;
10963 New_Exp : Node_Id;
10964 Ptr_Typ_Decl : Node_Id;
10965 Ref_Type : Entity_Id;
10966 Res : Node_Id;
10968 -- Start of processing for Remove_Side_Effects
10970 begin
10971 -- Handle cases in which there is nothing to do. In GNATprove mode,
10972 -- removal of side effects is useful for the light expansion of
10973 -- renamings. This removal should only occur when not inside a
10974 -- generic and not doing a pre-analysis.
10976 if not Expander_Active
10977 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
10978 then
10979 return;
10981 -- Cannot generate temporaries if the invocation to remove side effects
10982 -- was issued too early and the type of the expression is not resolved
10983 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
10984 -- Remove_Side_Effects).
10986 elsif No (Exp_Type)
10987 or else Ekind (Exp_Type) = E_Access_Attribute_Type
10988 then
10989 return;
10991 -- Nothing to do if prior expansion determined that a function call does
10992 -- not require side effect removal.
10994 elsif Nkind (Exp) = N_Function_Call
10995 and then No_Side_Effect_Removal (Exp)
10996 then
10997 return;
10999 -- No action needed for side-effect free expressions
11001 elsif Check_Side_Effects
11002 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11003 then
11004 return;
11005 end if;
11007 -- The remaining processing is done with all checks suppressed
11009 -- Note: from now on, don't use return statements, instead do a goto
11010 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11012 Scope_Suppress.Suppress := (others => True);
11014 -- If this is an elementary or a small not-by-reference record type, and
11015 -- we need to capture the value, just make a constant; this is cheap and
11016 -- objects of both kinds of types can be bit aligned, so it might not be
11017 -- possible to generate a reference to them. Likewise if this is not a
11018 -- name reference, except for a type conversion, because we would enter
11019 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11020 -- type has predicates (and type conversions need a specific treatment
11021 -- anyway, see below). Also do it if we have a volatile reference and
11022 -- Name_Req is not set (see comments for Side_Effect_Free).
11024 if (Is_Elementary_Type (Exp_Type)
11025 or else (Is_Record_Type (Exp_Type)
11026 and then Known_Static_RM_Size (Exp_Type)
11027 and then RM_Size (Exp_Type) <= 64
11028 and then not Has_Discriminants (Exp_Type)
11029 and then not Is_By_Reference_Type (Exp_Type)))
11030 and then (Variable_Ref
11031 or else (not Is_Name_Reference (Exp)
11032 and then Nkind (Exp) /= N_Type_Conversion)
11033 or else (not Name_Req
11034 and then Is_Volatile_Reference (Exp)))
11035 then
11036 Def_Id := Build_Temporary (Loc, 'R', Exp);
11037 Set_Etype (Def_Id, Exp_Type);
11038 Res := New_Occurrence_Of (Def_Id, Loc);
11040 -- If the expression is a packed reference, it must be reanalyzed and
11041 -- expanded, depending on context. This is the case for actuals where
11042 -- a constraint check may capture the actual before expansion of the
11043 -- call is complete.
11045 if Nkind (Exp) = N_Indexed_Component
11046 and then Is_Packed (Etype (Prefix (Exp)))
11047 then
11048 Set_Analyzed (Exp, False);
11049 Set_Analyzed (Prefix (Exp), False);
11050 end if;
11052 -- Generate:
11053 -- Rnn : Exp_Type renames Expr;
11055 if Renaming_Req then
11056 E :=
11057 Make_Object_Renaming_Declaration (Loc,
11058 Defining_Identifier => Def_Id,
11059 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11060 Name => Relocate_Node (Exp));
11062 -- Generate:
11063 -- Rnn : constant Exp_Type := Expr;
11065 else
11066 E :=
11067 Make_Object_Declaration (Loc,
11068 Defining_Identifier => Def_Id,
11069 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11070 Constant_Present => True,
11071 Expression => Relocate_Node (Exp));
11073 Set_Assignment_OK (E);
11074 end if;
11076 Insert_Action (Exp, E);
11078 -- If the expression has the form v.all then we can just capture the
11079 -- pointer, and then do an explicit dereference on the result, but
11080 -- this is not right if this is a volatile reference.
11082 elsif Nkind (Exp) = N_Explicit_Dereference
11083 and then not Is_Volatile_Reference (Exp)
11084 then
11085 Def_Id := Build_Temporary (Loc, 'R', Exp);
11086 Res :=
11087 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11089 Insert_Action (Exp,
11090 Make_Object_Declaration (Loc,
11091 Defining_Identifier => Def_Id,
11092 Object_Definition =>
11093 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11094 Constant_Present => True,
11095 Expression => Relocate_Node (Prefix (Exp))));
11097 -- Similar processing for an unchecked conversion of an expression of
11098 -- the form v.all, where we want the same kind of treatment.
11100 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11101 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11102 then
11103 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11104 goto Leave;
11106 -- If this is a type conversion, leave the type conversion and remove
11107 -- the side effects in the expression. This is important in several
11108 -- circumstances: for change of representations, and also when this is a
11109 -- view conversion to a smaller object, where gigi can end up creating
11110 -- its own temporary of the wrong size.
11112 elsif Nkind (Exp) = N_Type_Conversion then
11113 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11115 -- Generating C code the type conversion of an access to constrained
11116 -- array type into an access to unconstrained array type involves
11117 -- initializing a fat pointer and the expression must be free of
11118 -- side effects to safely compute its bounds.
11120 if Modify_Tree_For_C
11121 and then Is_Access_Type (Etype (Exp))
11122 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11123 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11124 then
11125 Def_Id := Build_Temporary (Loc, 'R', Exp);
11126 Set_Etype (Def_Id, Exp_Type);
11127 Res := New_Occurrence_Of (Def_Id, Loc);
11129 Insert_Action (Exp,
11130 Make_Object_Declaration (Loc,
11131 Defining_Identifier => Def_Id,
11132 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11133 Constant_Present => True,
11134 Expression => Relocate_Node (Exp)));
11135 else
11136 goto Leave;
11137 end if;
11139 -- If this is an unchecked conversion that Gigi can't handle, make
11140 -- a copy or a use a renaming to capture the value.
11142 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11143 and then not Safe_Unchecked_Type_Conversion (Exp)
11144 then
11145 if CW_Or_Has_Controlled_Part (Exp_Type) then
11147 -- Use a renaming to capture the expression, rather than create
11148 -- a controlled temporary.
11150 Def_Id := Build_Temporary (Loc, 'R', Exp);
11151 Res := New_Occurrence_Of (Def_Id, Loc);
11153 Insert_Action (Exp,
11154 Make_Object_Renaming_Declaration (Loc,
11155 Defining_Identifier => Def_Id,
11156 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11157 Name => Relocate_Node (Exp)));
11159 else
11160 Def_Id := Build_Temporary (Loc, 'R', Exp);
11161 Set_Etype (Def_Id, Exp_Type);
11162 Res := New_Occurrence_Of (Def_Id, Loc);
11164 E :=
11165 Make_Object_Declaration (Loc,
11166 Defining_Identifier => Def_Id,
11167 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11168 Constant_Present => not Is_Variable (Exp),
11169 Expression => Relocate_Node (Exp));
11171 Set_Assignment_OK (E);
11172 Insert_Action (Exp, E);
11173 end if;
11175 -- For expressions that denote names, we can use a renaming scheme.
11176 -- This is needed for correctness in the case of a volatile object of
11177 -- a non-volatile type because the Make_Reference call of the "default"
11178 -- approach would generate an illegal access value (an access value
11179 -- cannot designate such an object - see Analyze_Reference).
11181 elsif Is_Name_Reference (Exp)
11183 -- We skip using this scheme if we have an object of a volatile
11184 -- type and we do not have Name_Req set true (see comments for
11185 -- Side_Effect_Free).
11187 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11188 then
11189 Def_Id := Build_Temporary (Loc, 'R', Exp);
11190 Res := New_Occurrence_Of (Def_Id, Loc);
11192 Insert_Action (Exp,
11193 Make_Object_Renaming_Declaration (Loc,
11194 Defining_Identifier => Def_Id,
11195 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11196 Name => Relocate_Node (Exp)));
11198 -- If this is a packed reference, or a selected component with
11199 -- a non-standard representation, a reference to the temporary
11200 -- will be replaced by a copy of the original expression (see
11201 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11202 -- elaborated by gigi, and is of course not to be replaced in-line
11203 -- by the expression it renames, which would defeat the purpose of
11204 -- removing the side effect.
11206 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11207 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11208 then
11209 null;
11210 else
11211 Set_Is_Renaming_Of_Object (Def_Id, False);
11212 end if;
11214 -- Avoid generating a variable-sized temporary, by generating the
11215 -- reference just for the function call. The transformation could be
11216 -- refined to apply only when the array component is constrained by a
11217 -- discriminant???
11219 elsif Nkind (Exp) = N_Selected_Component
11220 and then Nkind (Prefix (Exp)) = N_Function_Call
11221 and then Is_Array_Type (Exp_Type)
11222 then
11223 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11224 goto Leave;
11226 -- Otherwise we generate a reference to the expression
11228 else
11229 -- An expression which is in SPARK mode is considered side effect
11230 -- free if the resulting value is captured by a variable or a
11231 -- constant.
11233 if GNATprove_Mode
11234 and then Nkind (Parent (Exp)) = N_Object_Declaration
11235 then
11236 goto Leave;
11238 -- When generating C code we cannot consider side effect free object
11239 -- declarations that have discriminants and are initialized by means
11240 -- of a function call since on this target there is no secondary
11241 -- stack to store the return value and the expander may generate an
11242 -- extra call to the function to compute the discriminant value. In
11243 -- addition, for targets that have secondary stack, the expansion of
11244 -- functions with side effects involves the generation of an access
11245 -- type to capture the return value stored in the secondary stack;
11246 -- by contrast when generating C code such expansion generates an
11247 -- internal object declaration (no access type involved) which must
11248 -- be identified here to avoid entering into a never-ending loop
11249 -- generating internal object declarations.
11251 elsif Modify_Tree_For_C
11252 and then Nkind (Parent (Exp)) = N_Object_Declaration
11253 and then
11254 (Nkind (Exp) /= N_Function_Call
11255 or else not Has_Discriminants (Exp_Type)
11256 or else Is_Internal_Name
11257 (Chars (Defining_Identifier (Parent (Exp)))))
11258 then
11259 goto Leave;
11260 end if;
11262 -- Special processing for function calls that return a limited type.
11263 -- We need to build a declaration that will enable build-in-place
11264 -- expansion of the call. This is not done if the context is already
11265 -- an object declaration, to prevent infinite recursion.
11267 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11268 -- to accommodate functions returning limited objects by reference.
11270 if Ada_Version >= Ada_2005
11271 and then Nkind (Exp) = N_Function_Call
11272 and then Is_Limited_View (Etype (Exp))
11273 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11274 then
11275 declare
11276 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11277 Decl : Node_Id;
11279 begin
11280 Decl :=
11281 Make_Object_Declaration (Loc,
11282 Defining_Identifier => Obj,
11283 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11284 Expression => Relocate_Node (Exp));
11286 Insert_Action (Exp, Decl);
11287 Set_Etype (Obj, Exp_Type);
11288 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11289 goto Leave;
11290 end;
11291 end if;
11293 Def_Id := Build_Temporary (Loc, 'R', Exp);
11295 -- The regular expansion of functions with side effects involves the
11296 -- generation of an access type to capture the return value found on
11297 -- the secondary stack. Since SPARK (and why) cannot process access
11298 -- types, use a different approach which ignores the secondary stack
11299 -- and "copies" the returned object.
11300 -- When generating C code, no need for a 'reference since the
11301 -- secondary stack is not supported.
11303 if GNATprove_Mode or Modify_Tree_For_C then
11304 Res := New_Occurrence_Of (Def_Id, Loc);
11305 Ref_Type := Exp_Type;
11307 -- Regular expansion utilizing an access type and 'reference
11309 else
11310 Res :=
11311 Make_Explicit_Dereference (Loc,
11312 Prefix => New_Occurrence_Of (Def_Id, Loc));
11314 -- Generate:
11315 -- type Ann is access all <Exp_Type>;
11317 Ref_Type := Make_Temporary (Loc, 'A');
11319 Ptr_Typ_Decl :=
11320 Make_Full_Type_Declaration (Loc,
11321 Defining_Identifier => Ref_Type,
11322 Type_Definition =>
11323 Make_Access_To_Object_Definition (Loc,
11324 All_Present => True,
11325 Subtype_Indication =>
11326 New_Occurrence_Of (Exp_Type, Loc)));
11328 Insert_Action (Exp, Ptr_Typ_Decl);
11329 end if;
11331 E := Exp;
11332 if Nkind (E) = N_Explicit_Dereference then
11333 New_Exp := Relocate_Node (Prefix (E));
11335 else
11336 E := Relocate_Node (E);
11338 -- Do not generate a 'reference in SPARK mode or C generation
11339 -- since the access type is not created in the first place.
11341 if GNATprove_Mode or Modify_Tree_For_C then
11342 New_Exp := E;
11344 -- Otherwise generate reference, marking the value as non-null
11345 -- since we know it cannot be null and we don't want a check.
11347 else
11348 New_Exp := Make_Reference (Loc, E);
11349 Set_Is_Known_Non_Null (Def_Id);
11350 end if;
11351 end if;
11353 if Is_Delayed_Aggregate (E) then
11355 -- The expansion of nested aggregates is delayed until the
11356 -- enclosing aggregate is expanded. As aggregates are often
11357 -- qualified, the predicate applies to qualified expressions as
11358 -- well, indicating that the enclosing aggregate has not been
11359 -- expanded yet. At this point the aggregate is part of a
11360 -- stand-alone declaration, and must be fully expanded.
11362 if Nkind (E) = N_Qualified_Expression then
11363 Set_Expansion_Delayed (Expression (E), False);
11364 Set_Analyzed (Expression (E), False);
11365 else
11366 Set_Expansion_Delayed (E, False);
11367 end if;
11369 Set_Analyzed (E, False);
11370 end if;
11372 -- Generating C code of object declarations that have discriminants
11373 -- and are initialized by means of a function call we propagate the
11374 -- discriminants of the parent type to the internally built object.
11375 -- This is needed to avoid generating an extra call to the called
11376 -- function.
11378 -- For example, if we generate here the following declaration, it
11379 -- will be expanded later adding an extra call to evaluate the value
11380 -- of the discriminant (needed to compute the size of the object).
11382 -- type Rec (D : Integer) is ...
11383 -- Obj : constant Rec := SomeFunc;
11385 if Modify_Tree_For_C
11386 and then Nkind (Parent (Exp)) = N_Object_Declaration
11387 and then Has_Discriminants (Exp_Type)
11388 and then Nkind (Exp) = N_Function_Call
11389 then
11390 Insert_Action (Exp,
11391 Make_Object_Declaration (Loc,
11392 Defining_Identifier => Def_Id,
11393 Object_Definition => New_Copy_Tree
11394 (Object_Definition (Parent (Exp))),
11395 Constant_Present => True,
11396 Expression => New_Exp));
11397 else
11398 Insert_Action (Exp,
11399 Make_Object_Declaration (Loc,
11400 Defining_Identifier => Def_Id,
11401 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11402 Constant_Present => True,
11403 Expression => New_Exp));
11404 end if;
11405 end if;
11407 -- Preserve the Assignment_OK flag in all copies, since at least one
11408 -- copy may be used in a context where this flag must be set (otherwise
11409 -- why would the flag be set in the first place).
11411 Set_Assignment_OK (Res, Assignment_OK (Exp));
11413 -- Finally rewrite the original expression and we are done
11415 Rewrite (Exp, Res);
11416 Analyze_And_Resolve (Exp, Exp_Type);
11418 <<Leave>>
11419 Scope_Suppress := Svg_Suppress;
11420 end Remove_Side_Effects;
11422 ------------------------
11423 -- Replace_References --
11424 ------------------------
11426 procedure Replace_References
11427 (Expr : Node_Id;
11428 Par_Typ : Entity_Id;
11429 Deriv_Typ : Entity_Id;
11430 Par_Obj : Entity_Id := Empty;
11431 Deriv_Obj : Entity_Id := Empty)
11433 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11434 -- Determine whether node Ref denotes some component of Deriv_Obj
11436 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11437 -- Substitute a reference to an entity with the corresponding value
11438 -- stored in table Type_Map.
11440 function Type_Of_Formal
11441 (Call : Node_Id;
11442 Actual : Node_Id) return Entity_Id;
11443 -- Find the type of the formal parameter which corresponds to actual
11444 -- parameter Actual in subprogram call Call.
11446 ----------------------
11447 -- Is_Deriv_Obj_Ref --
11448 ----------------------
11450 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11451 Par : constant Node_Id := Parent (Ref);
11453 begin
11454 -- Detect the folowing selected component form:
11456 -- Deriv_Obj.(something)
11458 return
11459 Nkind (Par) = N_Selected_Component
11460 and then Is_Entity_Name (Prefix (Par))
11461 and then Entity (Prefix (Par)) = Deriv_Obj;
11462 end Is_Deriv_Obj_Ref;
11464 -----------------
11465 -- Replace_Ref --
11466 -----------------
11468 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11469 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11470 -- Reset the Controlling_Argument of all function calls that
11471 -- encapsulate node From_Arg.
11473 ----------------------------------
11474 -- Remove_Controlling_Arguments --
11475 ----------------------------------
11477 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11478 Par : Node_Id;
11480 begin
11481 Par := From_Arg;
11482 while Present (Par) loop
11483 if Nkind (Par) = N_Function_Call
11484 and then Present (Controlling_Argument (Par))
11485 then
11486 Set_Controlling_Argument (Par, Empty);
11488 -- Prevent the search from going too far
11490 elsif Is_Body_Or_Package_Declaration (Par) then
11491 exit;
11492 end if;
11494 Par := Parent (Par);
11495 end loop;
11496 end Remove_Controlling_Arguments;
11498 -- Local variables
11500 Context : constant Node_Id := Parent (Ref);
11501 Loc : constant Source_Ptr := Sloc (Ref);
11502 Ref_Id : Entity_Id;
11503 Result : Traverse_Result;
11505 New_Ref : Node_Id;
11506 -- The new reference which is intended to substitute the old one
11508 Old_Ref : Node_Id;
11509 -- The reference designated for replacement. In certain cases this
11510 -- may be a node other than Ref.
11512 Val : Node_Or_Entity_Id;
11513 -- The corresponding value of Ref from the type map
11515 -- Start of processing for Replace_Ref
11517 begin
11518 -- Assume that the input reference is to be replaced and that the
11519 -- traversal should examine the children of the reference.
11521 Old_Ref := Ref;
11522 Result := OK;
11524 -- The input denotes a meaningful reference
11526 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11527 Ref_Id := Entity (Ref);
11528 Val := Type_Map.Get (Ref_Id);
11530 -- The reference has a corresponding value in the type map, a
11531 -- substitution is possible.
11533 if Present (Val) then
11535 -- The reference denotes a discriminant
11537 if Ekind (Ref_Id) = E_Discriminant then
11538 if Nkind (Val) in N_Entity then
11540 -- The value denotes another discriminant. Replace as
11541 -- follows:
11543 -- _object.Discr -> _object.Val
11545 if Ekind (Val) = E_Discriminant then
11546 New_Ref := New_Occurrence_Of (Val, Loc);
11548 -- Otherwise the value denotes the entity of a name which
11549 -- constraints the discriminant. Replace as follows:
11551 -- _object.Discr -> Val
11553 else
11554 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11556 New_Ref := New_Occurrence_Of (Val, Loc);
11557 Old_Ref := Parent (Old_Ref);
11558 end if;
11560 -- Otherwise the value denotes an arbitrary expression which
11561 -- constraints the discriminant. Replace as follows:
11563 -- _object.Discr -> Val
11565 else
11566 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11568 New_Ref := New_Copy_Tree (Val);
11569 Old_Ref := Parent (Old_Ref);
11570 end if;
11572 -- Otherwise the reference denotes a primitive. Replace as
11573 -- follows:
11575 -- Primitive -> Val
11577 else
11578 pragma Assert (Nkind (Val) in N_Entity);
11579 New_Ref := New_Occurrence_Of (Val, Loc);
11580 end if;
11582 -- The reference mentions the _object parameter of the parent
11583 -- type's DIC or type invariant procedure. Replace as follows:
11585 -- _object -> _object
11587 elsif Present (Par_Obj)
11588 and then Present (Deriv_Obj)
11589 and then Ref_Id = Par_Obj
11590 then
11591 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11593 -- The type of the _object parameter is class-wide when the
11594 -- expression comes from an assertion pragma that applies to
11595 -- an abstract parent type or an interface. The class-wide type
11596 -- facilitates the preanalysis of the expression by treating
11597 -- calls to abstract primitives that mention the current
11598 -- instance of the type as dispatching. Once the calls are
11599 -- remapped to invoke overriding or inherited primitives, the
11600 -- calls no longer need to be dispatching. Examine all function
11601 -- calls that encapsulate the _object parameter and reset their
11602 -- Controlling_Argument attribute.
11604 if Is_Class_Wide_Type (Etype (Par_Obj))
11605 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11606 then
11607 Remove_Controlling_Arguments (Old_Ref);
11608 end if;
11610 -- The reference to _object acts as an actual parameter in a
11611 -- subprogram call which may be invoking a primitive of the
11612 -- parent type:
11614 -- Primitive (... _object ...);
11616 -- The parent type primitive may not be overridden nor
11617 -- inherited when it is declared after the derived type
11618 -- definition:
11620 -- type Parent is tagged private;
11621 -- type Child is new Parent with private;
11622 -- procedure Primitive (Obj : Parent);
11624 -- In this scenario the _object parameter is converted to the
11625 -- parent type. Due to complications with partial/full views
11626 -- and view swaps, the parent type is taken from the formal
11627 -- parameter of the subprogram being called.
11629 if Nkind_In (Context, N_Function_Call,
11630 N_Procedure_Call_Statement)
11631 and then No (Type_Map.Get (Entity (Name (Context))))
11632 then
11633 New_Ref :=
11634 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11636 -- Do not process the generated type conversion because
11637 -- both the parent type and the derived type are in the
11638 -- Type_Map table. This will clobber the type conversion
11639 -- by resetting its subtype mark.
11641 Result := Skip;
11642 end if;
11644 -- Otherwise there is nothing to replace
11646 else
11647 New_Ref := Empty;
11648 end if;
11650 if Present (New_Ref) then
11651 Rewrite (Old_Ref, New_Ref);
11653 -- Update the return type when the context of the reference
11654 -- acts as the name of a function call. Note that the update
11655 -- should not be performed when the reference appears as an
11656 -- actual in the call.
11658 if Nkind (Context) = N_Function_Call
11659 and then Name (Context) = Old_Ref
11660 then
11661 Set_Etype (Context, Etype (Val));
11662 end if;
11663 end if;
11664 end if;
11666 -- Reanalyze the reference due to potential replacements
11668 if Nkind (Old_Ref) in N_Has_Etype then
11669 Set_Analyzed (Old_Ref, False);
11670 end if;
11672 return Result;
11673 end Replace_Ref;
11675 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11677 --------------------
11678 -- Type_Of_Formal --
11679 --------------------
11681 function Type_Of_Formal
11682 (Call : Node_Id;
11683 Actual : Node_Id) return Entity_Id
11685 A : Node_Id;
11686 F : Entity_Id;
11688 begin
11689 -- Examine the list of actual and formal parameters in parallel
11691 A := First (Parameter_Associations (Call));
11692 F := First_Formal (Entity (Name (Call)));
11693 while Present (A) and then Present (F) loop
11694 if A = Actual then
11695 return Etype (F);
11696 end if;
11698 Next (A);
11699 Next_Formal (F);
11700 end loop;
11702 -- The actual parameter must always have a corresponding formal
11704 pragma Assert (False);
11706 return Empty;
11707 end Type_Of_Formal;
11709 -- Start of processing for Replace_References
11711 begin
11712 -- Map the attributes of the parent type to the proper corresponding
11713 -- attributes of the derived type.
11715 Map_Types
11716 (Parent_Type => Par_Typ,
11717 Derived_Type => Deriv_Typ);
11719 -- Inspect the input expression and perform substitutions where
11720 -- necessary.
11722 Replace_Refs (Expr);
11723 end Replace_References;
11725 -----------------------------
11726 -- Replace_Type_References --
11727 -----------------------------
11729 procedure Replace_Type_References
11730 (Expr : Node_Id;
11731 Typ : Entity_Id;
11732 Obj_Id : Entity_Id)
11734 procedure Replace_Type_Ref (N : Node_Id);
11735 -- Substitute a single reference of the current instance of type Typ
11736 -- with a reference to Obj_Id.
11738 ----------------------
11739 -- Replace_Type_Ref --
11740 ----------------------
11742 procedure Replace_Type_Ref (N : Node_Id) is
11743 begin
11744 -- Decorate the reference to Typ even though it may be rewritten
11745 -- further down. This is done for two reasons:
11747 -- * ASIS has all necessary semantic information in the original
11748 -- tree.
11750 -- * Routines which examine properties of the Original_Node have
11751 -- some semantic information.
11753 if Nkind (N) = N_Identifier then
11754 Set_Entity (N, Typ);
11755 Set_Etype (N, Typ);
11757 elsif Nkind (N) = N_Selected_Component then
11758 Analyze (Prefix (N));
11759 Set_Entity (Selector_Name (N), Typ);
11760 Set_Etype (Selector_Name (N), Typ);
11761 end if;
11763 -- Perform the following substitution:
11765 -- Typ --> _object
11767 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
11768 Set_Comes_From_Source (N, True);
11769 end Replace_Type_Ref;
11771 procedure Replace_Type_Refs is
11772 new Replace_Type_References_Generic (Replace_Type_Ref);
11774 -- Start of processing for Replace_Type_References
11776 begin
11777 Replace_Type_Refs (Expr, Typ);
11778 end Replace_Type_References;
11780 ---------------------------
11781 -- Represented_As_Scalar --
11782 ---------------------------
11784 function Represented_As_Scalar (T : Entity_Id) return Boolean is
11785 UT : constant Entity_Id := Underlying_Type (T);
11786 begin
11787 return Is_Scalar_Type (UT)
11788 or else (Is_Bit_Packed_Array (UT)
11789 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
11790 end Represented_As_Scalar;
11792 ------------------------------
11793 -- Requires_Cleanup_Actions --
11794 ------------------------------
11796 function Requires_Cleanup_Actions
11797 (N : Node_Id;
11798 Lib_Level : Boolean) return Boolean
11800 At_Lib_Level : constant Boolean :=
11801 Lib_Level
11802 and then Nkind_In (N, N_Package_Body,
11803 N_Package_Specification);
11804 -- N is at the library level if the top-most context is a package and
11805 -- the path taken to reach N does not inlcude non-package constructs.
11807 begin
11808 case Nkind (N) is
11809 when N_Accept_Statement
11810 | N_Block_Statement
11811 | N_Entry_Body
11812 | N_Package_Body
11813 | N_Protected_Body
11814 | N_Subprogram_Body
11815 | N_Task_Body
11817 return
11818 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
11819 or else
11820 (Present (Handled_Statement_Sequence (N))
11821 and then
11822 Requires_Cleanup_Actions
11823 (Statements (Handled_Statement_Sequence (N)),
11824 At_Lib_Level, True));
11826 when N_Package_Specification =>
11827 return
11828 Requires_Cleanup_Actions
11829 (Visible_Declarations (N), At_Lib_Level, True)
11830 or else
11831 Requires_Cleanup_Actions
11832 (Private_Declarations (N), At_Lib_Level, True);
11834 when others =>
11835 return False;
11836 end case;
11837 end Requires_Cleanup_Actions;
11839 ------------------------------
11840 -- Requires_Cleanup_Actions --
11841 ------------------------------
11843 function Requires_Cleanup_Actions
11844 (L : List_Id;
11845 Lib_Level : Boolean;
11846 Nested_Constructs : Boolean) return Boolean
11848 Decl : Node_Id;
11849 Expr : Node_Id;
11850 Obj_Id : Entity_Id;
11851 Obj_Typ : Entity_Id;
11852 Pack_Id : Entity_Id;
11853 Typ : Entity_Id;
11855 begin
11856 if No (L)
11857 or else Is_Empty_List (L)
11858 then
11859 return False;
11860 end if;
11862 Decl := First (L);
11863 while Present (Decl) loop
11865 -- Library-level tagged types
11867 if Nkind (Decl) = N_Full_Type_Declaration then
11868 Typ := Defining_Identifier (Decl);
11870 -- Ignored Ghost types do not need any cleanup actions because
11871 -- they will not appear in the final tree.
11873 if Is_Ignored_Ghost_Entity (Typ) then
11874 null;
11876 elsif Is_Tagged_Type (Typ)
11877 and then Is_Library_Level_Entity (Typ)
11878 and then Convention (Typ) = Convention_Ada
11879 and then Present (Access_Disp_Table (Typ))
11880 and then RTE_Available (RE_Unregister_Tag)
11881 and then not Is_Abstract_Type (Typ)
11882 and then not No_Run_Time_Mode
11883 then
11884 return True;
11885 end if;
11887 -- Regular object declarations
11889 elsif Nkind (Decl) = N_Object_Declaration then
11890 Obj_Id := Defining_Identifier (Decl);
11891 Obj_Typ := Base_Type (Etype (Obj_Id));
11892 Expr := Expression (Decl);
11894 -- Bypass any form of processing for objects which have their
11895 -- finalization disabled. This applies only to objects at the
11896 -- library level.
11898 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
11899 null;
11901 -- Finalization of transient objects are treated separately in
11902 -- order to handle sensitive cases. These include:
11904 -- * Aggregate expansion
11905 -- * If, case, and expression with actions expansion
11906 -- * Transient scopes
11908 -- If one of those contexts has marked the transient object as
11909 -- ignored, do not generate finalization actions for it.
11911 elsif Is_Finalized_Transient (Obj_Id)
11912 or else Is_Ignored_Transient (Obj_Id)
11913 then
11914 null;
11916 -- Ignored Ghost objects do not need any cleanup actions because
11917 -- they will not appear in the final tree.
11919 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
11920 null;
11922 -- The object is of the form:
11923 -- Obj : [constant] Typ [:= Expr];
11925 -- Do not process tag-to-class-wide conversions because they do
11926 -- not yield an object. Do not process the incomplete view of a
11927 -- deferred constant. Note that an object initialized by means
11928 -- of a build-in-place function call may appear as a deferred
11929 -- constant after expansion activities. These kinds of objects
11930 -- must be finalized.
11932 elsif not Is_Imported (Obj_Id)
11933 and then Needs_Finalization (Obj_Typ)
11934 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
11935 and then not (Ekind (Obj_Id) = E_Constant
11936 and then not Has_Completion (Obj_Id)
11937 and then No (BIP_Initialization_Call (Obj_Id)))
11938 then
11939 return True;
11941 -- The object is of the form:
11942 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
11944 -- Obj : Access_Typ :=
11945 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
11947 elsif Is_Access_Type (Obj_Typ)
11948 and then Needs_Finalization
11949 (Available_View (Designated_Type (Obj_Typ)))
11950 and then Present (Expr)
11951 and then
11952 (Is_Secondary_Stack_BIP_Func_Call (Expr)
11953 or else
11954 (Is_Non_BIP_Func_Call (Expr)
11955 and then not Is_Related_To_Func_Return (Obj_Id)))
11956 then
11957 return True;
11959 -- Processing for "hook" objects generated for transient objects
11960 -- declared inside an Expression_With_Actions.
11962 elsif Is_Access_Type (Obj_Typ)
11963 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
11964 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
11965 N_Object_Declaration
11966 then
11967 return True;
11969 -- Processing for intermediate results of if expressions where
11970 -- one of the alternatives uses a controlled function call.
11972 elsif Is_Access_Type (Obj_Typ)
11973 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
11974 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
11975 N_Defining_Identifier
11976 and then Present (Expr)
11977 and then Nkind (Expr) = N_Null
11978 then
11979 return True;
11981 -- Simple protected objects which use type System.Tasking.
11982 -- Protected_Objects.Protection to manage their locks should be
11983 -- treated as controlled since they require manual cleanup.
11985 elsif Ekind (Obj_Id) = E_Variable
11986 and then (Is_Simple_Protected_Type (Obj_Typ)
11987 or else Has_Simple_Protected_Object (Obj_Typ))
11988 then
11989 return True;
11990 end if;
11992 -- Specific cases of object renamings
11994 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
11995 Obj_Id := Defining_Identifier (Decl);
11996 Obj_Typ := Base_Type (Etype (Obj_Id));
11998 -- Bypass any form of processing for objects which have their
11999 -- finalization disabled. This applies only to objects at the
12000 -- library level.
12002 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12003 null;
12005 -- Ignored Ghost object renamings do not need any cleanup actions
12006 -- because they will not appear in the final tree.
12008 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12009 null;
12011 -- Return object of a build-in-place function. This case is
12012 -- recognized and marked by the expansion of an extended return
12013 -- statement (see Expand_N_Extended_Return_Statement).
12015 elsif Needs_Finalization (Obj_Typ)
12016 and then Is_Return_Object (Obj_Id)
12017 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12018 then
12019 return True;
12021 -- Detect a case where a source object has been initialized by
12022 -- a controlled function call or another object which was later
12023 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12025 -- Obj1 : CW_Type := Src_Obj;
12026 -- Obj2 : CW_Type := Function_Call (...);
12028 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12029 -- Tmp : ... := Function_Call (...)'reference;
12030 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12032 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12033 return True;
12034 end if;
12036 -- Inspect the freeze node of an access-to-controlled type and look
12037 -- for a delayed finalization master. This case arises when the
12038 -- freeze actions are inserted at a later time than the expansion of
12039 -- the context. Since Build_Finalizer is never called on a single
12040 -- construct twice, the master will be ultimately left out and never
12041 -- finalized. This is also needed for freeze actions of designated
12042 -- types themselves, since in some cases the finalization master is
12043 -- associated with a designated type's freeze node rather than that
12044 -- of the access type (see handling for freeze actions in
12045 -- Build_Finalization_Master).
12047 elsif Nkind (Decl) = N_Freeze_Entity
12048 and then Present (Actions (Decl))
12049 then
12050 Typ := Entity (Decl);
12052 -- Freeze nodes for ignored Ghost types do not need cleanup
12053 -- actions because they will never appear in the final tree.
12055 if Is_Ignored_Ghost_Entity (Typ) then
12056 null;
12058 elsif ((Is_Access_Type (Typ)
12059 and then not Is_Access_Subprogram_Type (Typ)
12060 and then Needs_Finalization
12061 (Available_View (Designated_Type (Typ))))
12062 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12063 and then Requires_Cleanup_Actions
12064 (Actions (Decl), Lib_Level, Nested_Constructs)
12065 then
12066 return True;
12067 end if;
12069 -- Nested package declarations
12071 elsif Nested_Constructs
12072 and then Nkind (Decl) = N_Package_Declaration
12073 then
12074 Pack_Id := Defining_Entity (Decl);
12076 -- Do not inspect an ignored Ghost package because all code found
12077 -- within will not appear in the final tree.
12079 if Is_Ignored_Ghost_Entity (Pack_Id) then
12080 null;
12082 elsif Ekind (Pack_Id) /= E_Generic_Package
12083 and then Requires_Cleanup_Actions
12084 (Specification (Decl), Lib_Level)
12085 then
12086 return True;
12087 end if;
12089 -- Nested package bodies
12091 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12093 -- Do not inspect an ignored Ghost package body because all code
12094 -- found within will not appear in the final tree.
12096 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12097 null;
12099 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12100 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12101 then
12102 return True;
12103 end if;
12105 elsif Nkind (Decl) = N_Block_Statement
12106 and then
12108 -- Handle a rare case caused by a controlled transient object
12109 -- created as part of a record init proc. The variable is wrapped
12110 -- in a block, but the block is not associated with a transient
12111 -- scope.
12113 (Inside_Init_Proc
12115 -- Handle the case where the original context has been wrapped in
12116 -- a block to avoid interference between exception handlers and
12117 -- At_End handlers. Treat the block as transparent and process its
12118 -- contents.
12120 or else Is_Finalization_Wrapper (Decl))
12121 then
12122 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12123 return True;
12124 end if;
12125 end if;
12127 Next (Decl);
12128 end loop;
12130 return False;
12131 end Requires_Cleanup_Actions;
12133 ------------------------------------
12134 -- Safe_Unchecked_Type_Conversion --
12135 ------------------------------------
12137 -- Note: this function knows quite a bit about the exact requirements of
12138 -- Gigi with respect to unchecked type conversions, and its code must be
12139 -- coordinated with any changes in Gigi in this area.
12141 -- The above requirements should be documented in Sinfo ???
12143 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12144 Otyp : Entity_Id;
12145 Ityp : Entity_Id;
12146 Oalign : Uint;
12147 Ialign : Uint;
12148 Pexp : constant Node_Id := Parent (Exp);
12150 begin
12151 -- If the expression is the RHS of an assignment or object declaration
12152 -- we are always OK because there will always be a target.
12154 -- Object renaming declarations, (generated for view conversions of
12155 -- actuals in inlined calls), like object declarations, provide an
12156 -- explicit type, and are safe as well.
12158 if (Nkind (Pexp) = N_Assignment_Statement
12159 and then Expression (Pexp) = Exp)
12160 or else Nkind_In (Pexp, N_Object_Declaration,
12161 N_Object_Renaming_Declaration)
12162 then
12163 return True;
12165 -- If the expression is the prefix of an N_Selected_Component we should
12166 -- also be OK because GCC knows to look inside the conversion except if
12167 -- the type is discriminated. We assume that we are OK anyway if the
12168 -- type is not set yet or if it is controlled since we can't afford to
12169 -- introduce a temporary in this case.
12171 elsif Nkind (Pexp) = N_Selected_Component
12172 and then Prefix (Pexp) = Exp
12173 then
12174 if No (Etype (Pexp)) then
12175 return True;
12176 else
12177 return
12178 not Has_Discriminants (Etype (Pexp))
12179 or else Is_Constrained (Etype (Pexp));
12180 end if;
12181 end if;
12183 -- Set the output type, this comes from Etype if it is set, otherwise we
12184 -- take it from the subtype mark, which we assume was already fully
12185 -- analyzed.
12187 if Present (Etype (Exp)) then
12188 Otyp := Etype (Exp);
12189 else
12190 Otyp := Entity (Subtype_Mark (Exp));
12191 end if;
12193 -- The input type always comes from the expression, and we assume this
12194 -- is indeed always analyzed, so we can simply get the Etype.
12196 Ityp := Etype (Expression (Exp));
12198 -- Initialize alignments to unknown so far
12200 Oalign := No_Uint;
12201 Ialign := No_Uint;
12203 -- Replace a concurrent type by its corresponding record type and each
12204 -- type by its underlying type and do the tests on those. The original
12205 -- type may be a private type whose completion is a concurrent type, so
12206 -- find the underlying type first.
12208 if Present (Underlying_Type (Otyp)) then
12209 Otyp := Underlying_Type (Otyp);
12210 end if;
12212 if Present (Underlying_Type (Ityp)) then
12213 Ityp := Underlying_Type (Ityp);
12214 end if;
12216 if Is_Concurrent_Type (Otyp) then
12217 Otyp := Corresponding_Record_Type (Otyp);
12218 end if;
12220 if Is_Concurrent_Type (Ityp) then
12221 Ityp := Corresponding_Record_Type (Ityp);
12222 end if;
12224 -- If the base types are the same, we know there is no problem since
12225 -- this conversion will be a noop.
12227 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12228 return True;
12230 -- Same if this is an upwards conversion of an untagged type, and there
12231 -- are no constraints involved (could be more general???)
12233 elsif Etype (Ityp) = Otyp
12234 and then not Is_Tagged_Type (Ityp)
12235 and then not Has_Discriminants (Ityp)
12236 and then No (First_Rep_Item (Base_Type (Ityp)))
12237 then
12238 return True;
12240 -- If the expression has an access type (object or subprogram) we assume
12241 -- that the conversion is safe, because the size of the target is safe,
12242 -- even if it is a record (which might be treated as having unknown size
12243 -- at this point).
12245 elsif Is_Access_Type (Ityp) then
12246 return True;
12248 -- If the size of output type is known at compile time, there is never
12249 -- a problem. Note that unconstrained records are considered to be of
12250 -- known size, but we can't consider them that way here, because we are
12251 -- talking about the actual size of the object.
12253 -- We also make sure that in addition to the size being known, we do not
12254 -- have a case which might generate an embarrassingly large temp in
12255 -- stack checking mode.
12257 elsif Size_Known_At_Compile_Time (Otyp)
12258 and then
12259 (not Stack_Checking_Enabled
12260 or else not May_Generate_Large_Temp (Otyp))
12261 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12262 then
12263 return True;
12265 -- If either type is tagged, then we know the alignment is OK so Gigi
12266 -- will be able to use pointer punning.
12268 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12269 return True;
12271 -- If either type is a limited record type, we cannot do a copy, so say
12272 -- safe since there's nothing else we can do.
12274 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12275 return True;
12277 -- Conversions to and from packed array types are always ignored and
12278 -- hence are safe.
12280 elsif Is_Packed_Array_Impl_Type (Otyp)
12281 or else Is_Packed_Array_Impl_Type (Ityp)
12282 then
12283 return True;
12284 end if;
12286 -- The only other cases known to be safe is if the input type's
12287 -- alignment is known to be at least the maximum alignment for the
12288 -- target or if both alignments are known and the output type's
12289 -- alignment is no stricter than the input's. We can use the component
12290 -- type alignment for an array if a type is an unpacked array type.
12292 if Present (Alignment_Clause (Otyp)) then
12293 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12295 elsif Is_Array_Type (Otyp)
12296 and then Present (Alignment_Clause (Component_Type (Otyp)))
12297 then
12298 Oalign := Expr_Value (Expression (Alignment_Clause
12299 (Component_Type (Otyp))));
12300 end if;
12302 if Present (Alignment_Clause (Ityp)) then
12303 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12305 elsif Is_Array_Type (Ityp)
12306 and then Present (Alignment_Clause (Component_Type (Ityp)))
12307 then
12308 Ialign := Expr_Value (Expression (Alignment_Clause
12309 (Component_Type (Ityp))));
12310 end if;
12312 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12313 return True;
12315 elsif Ialign /= No_Uint
12316 and then Oalign /= No_Uint
12317 and then Ialign <= Oalign
12318 then
12319 return True;
12321 -- Otherwise, Gigi cannot handle this and we must make a temporary
12323 else
12324 return False;
12325 end if;
12326 end Safe_Unchecked_Type_Conversion;
12328 ---------------------------------
12329 -- Set_Current_Value_Condition --
12330 ---------------------------------
12332 -- Note: the implementation of this procedure is very closely tied to the
12333 -- implementation of Get_Current_Value_Condition. Here we set required
12334 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12335 -- them, so they must have a consistent view.
12337 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12339 procedure Set_Entity_Current_Value (N : Node_Id);
12340 -- If N is an entity reference, where the entity is of an appropriate
12341 -- kind, then set the current value of this entity to Cnode, unless
12342 -- there is already a definite value set there.
12344 procedure Set_Expression_Current_Value (N : Node_Id);
12345 -- If N is of an appropriate form, sets an appropriate entry in current
12346 -- value fields of relevant entities. Multiple entities can be affected
12347 -- in the case of an AND or AND THEN.
12349 ------------------------------
12350 -- Set_Entity_Current_Value --
12351 ------------------------------
12353 procedure Set_Entity_Current_Value (N : Node_Id) is
12354 begin
12355 if Is_Entity_Name (N) then
12356 declare
12357 Ent : constant Entity_Id := Entity (N);
12359 begin
12360 -- Don't capture if not safe to do so
12362 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12363 return;
12364 end if;
12366 -- Here we have a case where the Current_Value field may need
12367 -- to be set. We set it if it is not already set to a compile
12368 -- time expression value.
12370 -- Note that this represents a decision that one condition
12371 -- blots out another previous one. That's certainly right if
12372 -- they occur at the same level. If the second one is nested,
12373 -- then the decision is neither right nor wrong (it would be
12374 -- equally OK to leave the outer one in place, or take the new
12375 -- inner one. Really we should record both, but our data
12376 -- structures are not that elaborate.
12378 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12379 Set_Current_Value (Ent, Cnode);
12380 end if;
12381 end;
12382 end if;
12383 end Set_Entity_Current_Value;
12385 ----------------------------------
12386 -- Set_Expression_Current_Value --
12387 ----------------------------------
12389 procedure Set_Expression_Current_Value (N : Node_Id) is
12390 Cond : Node_Id;
12392 begin
12393 Cond := N;
12395 -- Loop to deal with (ignore for now) any NOT operators present. The
12396 -- presence of NOT operators will be handled properly when we call
12397 -- Get_Current_Value_Condition.
12399 while Nkind (Cond) = N_Op_Not loop
12400 Cond := Right_Opnd (Cond);
12401 end loop;
12403 -- For an AND or AND THEN, recursively process operands
12405 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12406 Set_Expression_Current_Value (Left_Opnd (Cond));
12407 Set_Expression_Current_Value (Right_Opnd (Cond));
12408 return;
12409 end if;
12411 -- Check possible relational operator
12413 if Nkind (Cond) in N_Op_Compare then
12414 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12415 Set_Entity_Current_Value (Left_Opnd (Cond));
12416 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12417 Set_Entity_Current_Value (Right_Opnd (Cond));
12418 end if;
12420 elsif Nkind_In (Cond,
12421 N_Type_Conversion,
12422 N_Qualified_Expression,
12423 N_Expression_With_Actions)
12424 then
12425 Set_Expression_Current_Value (Expression (Cond));
12427 -- Check possible boolean variable reference
12429 else
12430 Set_Entity_Current_Value (Cond);
12431 end if;
12432 end Set_Expression_Current_Value;
12434 -- Start of processing for Set_Current_Value_Condition
12436 begin
12437 Set_Expression_Current_Value (Condition (Cnode));
12438 end Set_Current_Value_Condition;
12440 --------------------------
12441 -- Set_Elaboration_Flag --
12442 --------------------------
12444 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12445 Loc : constant Source_Ptr := Sloc (N);
12446 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12447 Asn : Node_Id;
12449 begin
12450 if Present (Ent) then
12452 -- Nothing to do if at the compilation unit level, because in this
12453 -- case the flag is set by the binder generated elaboration routine.
12455 if Nkind (Parent (N)) = N_Compilation_Unit then
12456 null;
12458 -- Here we do need to generate an assignment statement
12460 else
12461 Check_Restriction (No_Elaboration_Code, N);
12462 Asn :=
12463 Make_Assignment_Statement (Loc,
12464 Name => New_Occurrence_Of (Ent, Loc),
12465 Expression => Make_Integer_Literal (Loc, Uint_1));
12467 if Nkind (Parent (N)) = N_Subunit then
12468 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12469 else
12470 Insert_After (N, Asn);
12471 end if;
12473 Analyze (Asn);
12475 -- Kill current value indication. This is necessary because the
12476 -- tests of this flag are inserted out of sequence and must not
12477 -- pick up bogus indications of the wrong constant value.
12479 Set_Current_Value (Ent, Empty);
12481 -- If the subprogram is in the current declarative part and
12482 -- 'access has been applied to it, generate an elaboration
12483 -- check at the beginning of the declarations of the body.
12485 if Nkind (N) = N_Subprogram_Body
12486 and then Address_Taken (Spec_Id)
12487 and then
12488 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12489 then
12490 declare
12491 Loc : constant Source_Ptr := Sloc (N);
12492 Decls : constant List_Id := Declarations (N);
12493 Chk : Node_Id;
12495 begin
12496 -- No need to generate this check if first entry in the
12497 -- declaration list is a raise of Program_Error now.
12499 if Present (Decls)
12500 and then Nkind (First (Decls)) = N_Raise_Program_Error
12501 then
12502 return;
12503 end if;
12505 -- Otherwise generate the check
12507 Chk :=
12508 Make_Raise_Program_Error (Loc,
12509 Condition =>
12510 Make_Op_Eq (Loc,
12511 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12512 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12513 Reason => PE_Access_Before_Elaboration);
12515 if No (Decls) then
12516 Set_Declarations (N, New_List (Chk));
12517 else
12518 Prepend (Chk, Decls);
12519 end if;
12521 Analyze (Chk);
12522 end;
12523 end if;
12524 end if;
12525 end if;
12526 end Set_Elaboration_Flag;
12528 ----------------------------
12529 -- Set_Renamed_Subprogram --
12530 ----------------------------
12532 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12533 begin
12534 -- If input node is an identifier, we can just reset it
12536 if Nkind (N) = N_Identifier then
12537 Set_Chars (N, Chars (E));
12538 Set_Entity (N, E);
12540 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12542 else
12543 declare
12544 CS : constant Boolean := Comes_From_Source (N);
12545 begin
12546 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12547 Set_Entity (N, E);
12548 Set_Comes_From_Source (N, CS);
12549 Set_Analyzed (N, True);
12550 end;
12551 end if;
12552 end Set_Renamed_Subprogram;
12554 ----------------------
12555 -- Side_Effect_Free --
12556 ----------------------
12558 function Side_Effect_Free
12559 (N : Node_Id;
12560 Name_Req : Boolean := False;
12561 Variable_Ref : Boolean := False) return Boolean
12563 Typ : constant Entity_Id := Etype (N);
12564 -- Result type of the expression
12566 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12567 -- The argument N is a construct where the Prefix is dereferenced if it
12568 -- is an access type and the result is a variable. The call returns True
12569 -- if the construct is side effect free (not considering side effects in
12570 -- other than the prefix which are to be tested by the caller).
12572 function Within_In_Parameter (N : Node_Id) return Boolean;
12573 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12574 -- N is not side-effect free when the actual is global and modifiable
12575 -- indirectly from within a subprogram, because it may be passed by
12576 -- reference. The front-end must be conservative here and assume that
12577 -- this may happen with any array or record type. On the other hand, we
12578 -- cannot create temporaries for all expressions for which this
12579 -- condition is true, for various reasons that might require clearing up
12580 -- ??? For example, discriminant references that appear out of place, or
12581 -- spurious type errors with class-wide expressions. As a result, we
12582 -- limit the transformation to loop bounds, which is so far the only
12583 -- case that requires it.
12585 -----------------------------
12586 -- Safe_Prefixed_Reference --
12587 -----------------------------
12589 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12590 begin
12591 -- If prefix is not side effect free, definitely not safe
12593 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12594 return False;
12596 -- If the prefix is of an access type that is not access-to-constant,
12597 -- then this construct is a variable reference, which means it is to
12598 -- be considered to have side effects if Variable_Ref is set True.
12600 elsif Is_Access_Type (Etype (Prefix (N)))
12601 and then not Is_Access_Constant (Etype (Prefix (N)))
12602 and then Variable_Ref
12603 then
12604 -- Exception is a prefix that is the result of a previous removal
12605 -- of side effects.
12607 return Is_Entity_Name (Prefix (N))
12608 and then not Comes_From_Source (Prefix (N))
12609 and then Ekind (Entity (Prefix (N))) = E_Constant
12610 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12612 -- If the prefix is an explicit dereference then this construct is a
12613 -- variable reference, which means it is to be considered to have
12614 -- side effects if Variable_Ref is True.
12616 -- We do NOT exclude dereferences of access-to-constant types because
12617 -- we handle them as constant view of variables.
12619 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12620 and then Variable_Ref
12621 then
12622 return False;
12624 -- Note: The following test is the simplest way of solving a complex
12625 -- problem uncovered by the following test (Side effect on loop bound
12626 -- that is a subcomponent of a global variable:
12628 -- with Text_Io; use Text_Io;
12629 -- procedure Tloop is
12630 -- type X is
12631 -- record
12632 -- V : Natural := 4;
12633 -- S : String (1..5) := (others => 'a');
12634 -- end record;
12635 -- X1 : X;
12637 -- procedure Modi;
12639 -- generic
12640 -- with procedure Action;
12641 -- procedure Loop_G (Arg : X; Msg : String)
12643 -- procedure Loop_G (Arg : X; Msg : String) is
12644 -- begin
12645 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12646 -- & Natural'Image (Arg.V));
12647 -- for Index in 1 .. Arg.V loop
12648 -- Text_Io.Put_Line
12649 -- (Natural'Image (Index) & " " & Arg.S (Index));
12650 -- if Index > 2 then
12651 -- Modi;
12652 -- end if;
12653 -- end loop;
12654 -- Put_Line ("end loop_g " & Msg);
12655 -- end;
12657 -- procedure Loop1 is new Loop_G (Modi);
12658 -- procedure Modi is
12659 -- begin
12660 -- X1.V := 1;
12661 -- Loop1 (X1, "from modi");
12662 -- end;
12664 -- begin
12665 -- Loop1 (X1, "initial");
12666 -- end;
12668 -- The output of the above program should be:
12670 -- begin loop_g initial will loop till: 4
12671 -- 1 a
12672 -- 2 a
12673 -- 3 a
12674 -- begin loop_g from modi will loop till: 1
12675 -- 1 a
12676 -- end loop_g from modi
12677 -- 4 a
12678 -- begin loop_g from modi will loop till: 1
12679 -- 1 a
12680 -- end loop_g from modi
12681 -- end loop_g initial
12683 -- If a loop bound is a subcomponent of a global variable, a
12684 -- modification of that variable within the loop may incorrectly
12685 -- affect the execution of the loop.
12687 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12688 and then Within_In_Parameter (Prefix (N))
12689 and then Variable_Ref
12690 then
12691 return False;
12693 -- All other cases are side effect free
12695 else
12696 return True;
12697 end if;
12698 end Safe_Prefixed_Reference;
12700 -------------------------
12701 -- Within_In_Parameter --
12702 -------------------------
12704 function Within_In_Parameter (N : Node_Id) return Boolean is
12705 begin
12706 if not Comes_From_Source (N) then
12707 return False;
12709 elsif Is_Entity_Name (N) then
12710 return Ekind (Entity (N)) = E_In_Parameter;
12712 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12713 return Within_In_Parameter (Prefix (N));
12715 else
12716 return False;
12717 end if;
12718 end Within_In_Parameter;
12720 -- Start of processing for Side_Effect_Free
12722 begin
12723 -- If volatile reference, always consider it to have side effects
12725 if Is_Volatile_Reference (N) then
12726 return False;
12727 end if;
12729 -- Note on checks that could raise Constraint_Error. Strictly, if we
12730 -- take advantage of 11.6, these checks do not count as side effects.
12731 -- However, we would prefer to consider that they are side effects,
12732 -- since the back end CSE does not work very well on expressions which
12733 -- can raise Constraint_Error. On the other hand if we don't consider
12734 -- them to be side effect free, then we get some awkward expansions
12735 -- in -gnato mode, resulting in code insertions at a point where we
12736 -- do not have a clear model for performing the insertions.
12738 -- Special handling for entity names
12740 if Is_Entity_Name (N) then
12742 -- A type reference is always side effect free
12744 if Is_Type (Entity (N)) then
12745 return True;
12747 -- Variables are considered to be a side effect if Variable_Ref
12748 -- is set or if we have a volatile reference and Name_Req is off.
12749 -- If Name_Req is True then we can't help returning a name which
12750 -- effectively allows multiple references in any case.
12752 elsif Is_Variable (N, Use_Original_Node => False) then
12753 return not Variable_Ref
12754 and then (not Is_Volatile_Reference (N) or else Name_Req);
12756 -- Any other entity (e.g. a subtype name) is definitely side
12757 -- effect free.
12759 else
12760 return True;
12761 end if;
12763 -- A value known at compile time is always side effect free
12765 elsif Compile_Time_Known_Value (N) then
12766 return True;
12768 -- A variable renaming is not side-effect free, because the renaming
12769 -- will function like a macro in the front-end in some cases, and an
12770 -- assignment can modify the component designated by N, so we need to
12771 -- create a temporary for it.
12773 -- The guard testing for Entity being present is needed at least in
12774 -- the case of rewritten predicate expressions, and may well also be
12775 -- appropriate elsewhere. Obviously we can't go testing the entity
12776 -- field if it does not exist, so it's reasonable to say that this is
12777 -- not the renaming case if it does not exist.
12779 elsif Is_Entity_Name (Original_Node (N))
12780 and then Present (Entity (Original_Node (N)))
12781 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
12782 and then Ekind (Entity (Original_Node (N))) /= E_Constant
12783 then
12784 declare
12785 RO : constant Node_Id :=
12786 Renamed_Object (Entity (Original_Node (N)));
12788 begin
12789 -- If the renamed object is an indexed component, or an
12790 -- explicit dereference, then the designated object could
12791 -- be modified by an assignment.
12793 if Nkind_In (RO, N_Indexed_Component,
12794 N_Explicit_Dereference)
12795 then
12796 return False;
12798 -- A selected component must have a safe prefix
12800 elsif Nkind (RO) = N_Selected_Component then
12801 return Safe_Prefixed_Reference (RO);
12803 -- In all other cases, designated object cannot be changed so
12804 -- we are side effect free.
12806 else
12807 return True;
12808 end if;
12809 end;
12811 -- Remove_Side_Effects generates an object renaming declaration to
12812 -- capture the expression of a class-wide expression. In VM targets
12813 -- the frontend performs no expansion for dispatching calls to
12814 -- class- wide types since they are handled by the VM. Hence, we must
12815 -- locate here if this node corresponds to a previous invocation of
12816 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
12818 elsif not Tagged_Type_Expansion
12819 and then not Comes_From_Source (N)
12820 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
12821 and then Is_Class_Wide_Type (Typ)
12822 then
12823 return True;
12825 -- Generating C the type conversion of an access to constrained array
12826 -- type into an access to unconstrained array type involves initializing
12827 -- a fat pointer and the expression cannot be assumed to be free of side
12828 -- effects since it must referenced several times to compute its bounds.
12830 elsif Modify_Tree_For_C
12831 and then Nkind (N) = N_Type_Conversion
12832 and then Is_Access_Type (Typ)
12833 and then Is_Array_Type (Designated_Type (Typ))
12834 and then not Is_Constrained (Designated_Type (Typ))
12835 then
12836 return False;
12837 end if;
12839 -- For other than entity names and compile time known values,
12840 -- check the node kind for special processing.
12842 case Nkind (N) is
12844 -- An attribute reference is side effect free if its expressions
12845 -- are side effect free and its prefix is side effect free or
12846 -- is an entity reference.
12848 -- Is this right? what about x'first where x is a variable???
12850 when N_Attribute_Reference =>
12851 Attribute_Reference : declare
12853 function Side_Effect_Free_Attribute
12854 (Attribute_Name : Name_Id) return Boolean;
12855 -- Returns True if evaluation of the given attribute is
12856 -- considered side-effect free (independent of prefix and
12857 -- arguments).
12859 --------------------------------
12860 -- Side_Effect_Free_Attribute --
12861 --------------------------------
12863 function Side_Effect_Free_Attribute
12864 (Attribute_Name : Name_Id) return Boolean
12866 begin
12867 case Attribute_Name is
12868 when Name_Input =>
12869 return False;
12871 when Name_Image
12872 | Name_Img
12873 | Name_Wide_Image
12874 | Name_Wide_Wide_Image
12876 -- CodePeer doesn't want to see replicated copies of
12877 -- 'Image calls.
12879 return not CodePeer_Mode;
12881 when others =>
12882 return True;
12883 end case;
12884 end Side_Effect_Free_Attribute;
12886 -- Start of processing for Attribute_Reference
12888 begin
12889 return
12890 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12891 and then Side_Effect_Free_Attribute (Attribute_Name (N))
12892 and then (Is_Entity_Name (Prefix (N))
12893 or else Side_Effect_Free
12894 (Prefix (N), Name_Req, Variable_Ref));
12895 end Attribute_Reference;
12897 -- A binary operator is side effect free if and both operands are
12898 -- side effect free. For this purpose binary operators include
12899 -- membership tests and short circuit forms.
12901 when N_Binary_Op
12902 | N_Membership_Test
12903 | N_Short_Circuit
12905 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
12906 and then
12907 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
12909 -- An explicit dereference is side effect free only if it is
12910 -- a side effect free prefixed reference.
12912 when N_Explicit_Dereference =>
12913 return Safe_Prefixed_Reference (N);
12915 -- An expression with action is side effect free if its expression
12916 -- is side effect free and it has no actions.
12918 when N_Expression_With_Actions =>
12919 return
12920 Is_Empty_List (Actions (N))
12921 and then Side_Effect_Free
12922 (Expression (N), Name_Req, Variable_Ref);
12924 -- A call to _rep_to_pos is side effect free, since we generate
12925 -- this pure function call ourselves. Moreover it is critically
12926 -- important to make this exception, since otherwise we can have
12927 -- discriminants in array components which don't look side effect
12928 -- free in the case of an array whose index type is an enumeration
12929 -- type with an enumeration rep clause.
12931 -- All other function calls are not side effect free
12933 when N_Function_Call =>
12934 return
12935 Nkind (Name (N)) = N_Identifier
12936 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
12937 and then Side_Effect_Free
12938 (First (Parameter_Associations (N)),
12939 Name_Req, Variable_Ref);
12941 -- An IF expression is side effect free if it's of a scalar type, and
12942 -- all its components are all side effect free (conditions and then
12943 -- actions and else actions). We restrict to scalar types, since it
12944 -- is annoying to deal with things like (if A then B else C)'First
12945 -- where the type involved is a string type.
12947 when N_If_Expression =>
12948 return
12949 Is_Scalar_Type (Typ)
12950 and then Side_Effect_Free
12951 (Expressions (N), Name_Req, Variable_Ref);
12953 -- An indexed component is side effect free if it is a side
12954 -- effect free prefixed reference and all the indexing
12955 -- expressions are side effect free.
12957 when N_Indexed_Component =>
12958 return
12959 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12960 and then Safe_Prefixed_Reference (N);
12962 -- A type qualification, type conversion, or unchecked expression is
12963 -- side effect free if the expression is side effect free.
12965 when N_Qualified_Expression
12966 | N_Type_Conversion
12967 | N_Unchecked_Expression
12969 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
12971 -- A selected component is side effect free only if it is a side
12972 -- effect free prefixed reference.
12974 when N_Selected_Component =>
12975 return Safe_Prefixed_Reference (N);
12977 -- A range is side effect free if the bounds are side effect free
12979 when N_Range =>
12980 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
12981 and then
12982 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
12984 -- A slice is side effect free if it is a side effect free
12985 -- prefixed reference and the bounds are side effect free.
12987 when N_Slice =>
12988 return
12989 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
12990 and then Safe_Prefixed_Reference (N);
12992 -- A unary operator is side effect free if the operand
12993 -- is side effect free.
12995 when N_Unary_Op =>
12996 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
12998 -- An unchecked type conversion is side effect free only if it
12999 -- is safe and its argument is side effect free.
13001 when N_Unchecked_Type_Conversion =>
13002 return
13003 Safe_Unchecked_Type_Conversion (N)
13004 and then Side_Effect_Free
13005 (Expression (N), Name_Req, Variable_Ref);
13007 -- A literal is side effect free
13009 when N_Character_Literal
13010 | N_Integer_Literal
13011 | N_Real_Literal
13012 | N_String_Literal
13014 return True;
13016 -- We consider that anything else has side effects. This is a bit
13017 -- crude, but we are pretty close for most common cases, and we
13018 -- are certainly correct (i.e. we never return True when the
13019 -- answer should be False).
13021 when others =>
13022 return False;
13023 end case;
13024 end Side_Effect_Free;
13026 -- A list is side effect free if all elements of the list are side
13027 -- effect free.
13029 function Side_Effect_Free
13030 (L : List_Id;
13031 Name_Req : Boolean := False;
13032 Variable_Ref : Boolean := False) return Boolean
13034 N : Node_Id;
13036 begin
13037 if L = No_List or else L = Error_List then
13038 return True;
13040 else
13041 N := First (L);
13042 while Present (N) loop
13043 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13044 return False;
13045 else
13046 Next (N);
13047 end if;
13048 end loop;
13050 return True;
13051 end if;
13052 end Side_Effect_Free;
13054 ----------------------------------
13055 -- Silly_Boolean_Array_Not_Test --
13056 ----------------------------------
13058 -- This procedure implements an odd and silly test. We explicitly check
13059 -- for the case where the 'First of the component type is equal to the
13060 -- 'Last of this component type, and if this is the case, we make sure
13061 -- that constraint error is raised. The reason is that the NOT is bound
13062 -- to cause CE in this case, and we will not otherwise catch it.
13064 -- No such check is required for AND and OR, since for both these cases
13065 -- False op False = False, and True op True = True. For the XOR case,
13066 -- see Silly_Boolean_Array_Xor_Test.
13068 -- Believe it or not, this was reported as a bug. Note that nearly always,
13069 -- the test will evaluate statically to False, so the code will be
13070 -- statically removed, and no extra overhead caused.
13072 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13073 Loc : constant Source_Ptr := Sloc (N);
13074 CT : constant Entity_Id := Component_Type (T);
13076 begin
13077 -- The check we install is
13079 -- constraint_error when
13080 -- component_type'first = component_type'last
13081 -- and then array_type'Length /= 0)
13083 -- We need the last guard because we don't want to raise CE for empty
13084 -- arrays since no out of range values result. (Empty arrays with a
13085 -- component type of True .. True -- very useful -- even the ACATS
13086 -- does not test that marginal case).
13088 Insert_Action (N,
13089 Make_Raise_Constraint_Error (Loc,
13090 Condition =>
13091 Make_And_Then (Loc,
13092 Left_Opnd =>
13093 Make_Op_Eq (Loc,
13094 Left_Opnd =>
13095 Make_Attribute_Reference (Loc,
13096 Prefix => New_Occurrence_Of (CT, Loc),
13097 Attribute_Name => Name_First),
13099 Right_Opnd =>
13100 Make_Attribute_Reference (Loc,
13101 Prefix => New_Occurrence_Of (CT, Loc),
13102 Attribute_Name => Name_Last)),
13104 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13105 Reason => CE_Range_Check_Failed));
13106 end Silly_Boolean_Array_Not_Test;
13108 ----------------------------------
13109 -- Silly_Boolean_Array_Xor_Test --
13110 ----------------------------------
13112 -- This procedure implements an odd and silly test. We explicitly check
13113 -- for the XOR case where the component type is True .. True, since this
13114 -- will raise constraint error. A special check is required since CE
13115 -- will not be generated otherwise (cf Expand_Packed_Not).
13117 -- No such check is required for AND and OR, since for both these cases
13118 -- False op False = False, and True op True = True, and no check is
13119 -- required for the case of False .. False, since False xor False = False.
13120 -- See also Silly_Boolean_Array_Not_Test
13122 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13123 Loc : constant Source_Ptr := Sloc (N);
13124 CT : constant Entity_Id := Component_Type (T);
13126 begin
13127 -- The check we install is
13129 -- constraint_error when
13130 -- Boolean (component_type'First)
13131 -- and then Boolean (component_type'Last)
13132 -- and then array_type'Length /= 0)
13134 -- We need the last guard because we don't want to raise CE for empty
13135 -- arrays since no out of range values result (Empty arrays with a
13136 -- component type of True .. True -- very useful -- even the ACATS
13137 -- does not test that marginal case).
13139 Insert_Action (N,
13140 Make_Raise_Constraint_Error (Loc,
13141 Condition =>
13142 Make_And_Then (Loc,
13143 Left_Opnd =>
13144 Make_And_Then (Loc,
13145 Left_Opnd =>
13146 Convert_To (Standard_Boolean,
13147 Make_Attribute_Reference (Loc,
13148 Prefix => New_Occurrence_Of (CT, Loc),
13149 Attribute_Name => Name_First)),
13151 Right_Opnd =>
13152 Convert_To (Standard_Boolean,
13153 Make_Attribute_Reference (Loc,
13154 Prefix => New_Occurrence_Of (CT, Loc),
13155 Attribute_Name => Name_Last))),
13157 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13158 Reason => CE_Range_Check_Failed));
13159 end Silly_Boolean_Array_Xor_Test;
13161 --------------------------
13162 -- Target_Has_Fixed_Ops --
13163 --------------------------
13165 Integer_Sized_Small : Ureal;
13166 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13167 -- called (we don't want to compute it more than once).
13169 Long_Integer_Sized_Small : Ureal;
13170 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13171 -- is called (we don't want to compute it more than once)
13173 First_Time_For_THFO : Boolean := True;
13174 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13176 function Target_Has_Fixed_Ops
13177 (Left_Typ : Entity_Id;
13178 Right_Typ : Entity_Id;
13179 Result_Typ : Entity_Id) return Boolean
13181 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13182 -- Return True if the given type is a fixed-point type with a small
13183 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13184 -- an absolute value less than 1.0. This is currently limited to
13185 -- fixed-point types that map to Integer or Long_Integer.
13187 ------------------------
13188 -- Is_Fractional_Type --
13189 ------------------------
13191 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13192 begin
13193 if Esize (Typ) = Standard_Integer_Size then
13194 return Small_Value (Typ) = Integer_Sized_Small;
13196 elsif Esize (Typ) = Standard_Long_Integer_Size then
13197 return Small_Value (Typ) = Long_Integer_Sized_Small;
13199 else
13200 return False;
13201 end if;
13202 end Is_Fractional_Type;
13204 -- Start of processing for Target_Has_Fixed_Ops
13206 begin
13207 -- Return False if Fractional_Fixed_Ops_On_Target is false
13209 if not Fractional_Fixed_Ops_On_Target then
13210 return False;
13211 end if;
13213 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13214 -- standard constants used by Is_Fractional_Type.
13216 if First_Time_For_THFO then
13217 First_Time_For_THFO := False;
13219 Integer_Sized_Small :=
13220 UR_From_Components
13221 (Num => Uint_1,
13222 Den => UI_From_Int (Standard_Integer_Size - 1),
13223 Rbase => 2);
13225 Long_Integer_Sized_Small :=
13226 UR_From_Components
13227 (Num => Uint_1,
13228 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13229 Rbase => 2);
13230 end if;
13232 -- Return True if target supports fixed-by-fixed multiply/divide for
13233 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13234 -- and result types are equivalent fractional types.
13236 return Is_Fractional_Type (Base_Type (Left_Typ))
13237 and then Is_Fractional_Type (Base_Type (Right_Typ))
13238 and then Is_Fractional_Type (Base_Type (Result_Typ))
13239 and then Esize (Left_Typ) = Esize (Right_Typ)
13240 and then Esize (Left_Typ) = Esize (Result_Typ);
13241 end Target_Has_Fixed_Ops;
13243 -------------------
13244 -- Type_Map_Hash --
13245 -------------------
13247 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13248 begin
13249 return Type_Map_Header (Id mod Type_Map_Size);
13250 end Type_Map_Hash;
13252 ------------------------------------------
13253 -- Type_May_Have_Bit_Aligned_Components --
13254 ------------------------------------------
13256 function Type_May_Have_Bit_Aligned_Components
13257 (Typ : Entity_Id) return Boolean
13259 begin
13260 -- Array type, check component type
13262 if Is_Array_Type (Typ) then
13263 return
13264 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13266 -- Record type, check components
13268 elsif Is_Record_Type (Typ) then
13269 declare
13270 E : Entity_Id;
13272 begin
13273 E := First_Component_Or_Discriminant (Typ);
13274 while Present (E) loop
13275 if Component_May_Be_Bit_Aligned (E)
13276 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13277 then
13278 return True;
13279 end if;
13281 Next_Component_Or_Discriminant (E);
13282 end loop;
13284 return False;
13285 end;
13287 -- Type other than array or record is always OK
13289 else
13290 return False;
13291 end if;
13292 end Type_May_Have_Bit_Aligned_Components;
13294 -------------------------------
13295 -- Update_Primitives_Mapping --
13296 -------------------------------
13298 procedure Update_Primitives_Mapping
13299 (Inher_Id : Entity_Id;
13300 Subp_Id : Entity_Id)
13302 begin
13303 Map_Types
13304 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13305 Derived_Type => Find_Dispatching_Type (Subp_Id));
13306 end Update_Primitives_Mapping;
13308 ----------------------------------
13309 -- Within_Case_Or_If_Expression --
13310 ----------------------------------
13312 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13313 Par : Node_Id;
13315 begin
13316 -- Locate an enclosing case or if expression. Note that these constructs
13317 -- can be expanded into Expression_With_Actions, hence the test of the
13318 -- original node.
13320 Par := Parent (N);
13321 while Present (Par) loop
13322 if Nkind_In (Original_Node (Par), N_Case_Expression,
13323 N_If_Expression)
13324 then
13325 return True;
13327 -- Prevent the search from going too far
13329 elsif Is_Body_Or_Package_Declaration (Par) then
13330 return False;
13331 end if;
13333 Par := Parent (Par);
13334 end loop;
13336 return False;
13337 end Within_Case_Or_If_Expression;
13339 --------------------------------
13340 -- Within_Internal_Subprogram --
13341 --------------------------------
13343 function Within_Internal_Subprogram return Boolean is
13344 S : Entity_Id;
13346 begin
13347 S := Current_Scope;
13348 while Present (S) and then not Is_Subprogram (S) loop
13349 S := Scope (S);
13350 end loop;
13352 return Present (S)
13353 and then Get_TSS_Name (S) /= TSS_Null
13354 and then not Is_Predicate_Function (S)
13355 and then not Is_Predicate_Function_M (S);
13356 end Within_Internal_Subprogram;
13358 end Exp_Util;