Small ChangeLog tweak.
[official-gcc.git] / gcc / ada / exp_util.adb
blob8270ea5499c64d277742d9bb3a70abe4c900b5a1
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_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Stringt; use Stringt;
62 with Targparm; use Targparm;
63 with Tbuild; use Tbuild;
64 with Ttypes; use Ttypes;
65 with Urealp; use Urealp;
66 with Validsw; use Validsw;
68 with GNAT.HTable; use 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 Find_DIC_Type (Typ : Entity_Id) return Entity_Id;
169 -- Subsidiary to all Build_DIC_Procedure_xxx routines. Find the type which
170 -- defines the Default_Initial_Condition pragma of type Typ. This is either
171 -- Typ itself or a parent type when the pragma is inherited.
173 function Make_CW_Equivalent_Type
174 (T : Entity_Id;
175 E : Node_Id) return Entity_Id;
176 -- T is a class-wide type entity, E is the initial expression node that
177 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
178 -- returns the entity of the Equivalent type and inserts on the fly the
179 -- necessary declaration such as:
181 -- type anon is record
182 -- _parent : Root_Type (T); constrained with E discriminants (if any)
183 -- Extension : String (1 .. expr to match size of E);
184 -- end record;
186 -- This record is compatible with any object of the class of T thanks to
187 -- the first field and has the same size as E thanks to the second.
189 function Make_Literal_Range
190 (Loc : Source_Ptr;
191 Literal_Typ : Entity_Id) return Node_Id;
192 -- Produce a Range node whose bounds are:
193 -- Low_Bound (Literal_Type) ..
194 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
195 -- this is used for expanding declarations like X : String := "sdfgdfg";
197 -- If the index type of the target array is not integer, we generate:
198 -- Low_Bound (Literal_Type) ..
199 -- Literal_Type'Val
200 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
201 -- + (Length (Literal_Typ) -1))
203 function Make_Non_Empty_Check
204 (Loc : Source_Ptr;
205 N : Node_Id) return Node_Id;
206 -- Produce a boolean expression checking that the unidimensional array
207 -- node N is not empty.
209 function New_Class_Wide_Subtype
210 (CW_Typ : Entity_Id;
211 N : Node_Id) return Entity_Id;
212 -- Create an implicit subtype of CW_Typ attached to node N
214 function Requires_Cleanup_Actions
215 (L : List_Id;
216 Lib_Level : Boolean;
217 Nested_Constructs : Boolean) return Boolean;
218 -- Given a list L, determine whether it contains one of the following:
220 -- 1) controlled objects
221 -- 2) library-level tagged types
223 -- Lib_Level is True when the list comes from a construct at the library
224 -- level, and False otherwise. Nested_Constructs is True when any nested
225 -- packages declared in L must be processed, and False otherwise.
227 -------------------------------------
228 -- Activate_Atomic_Synchronization --
229 -------------------------------------
231 procedure Activate_Atomic_Synchronization (N : Node_Id) is
232 Msg_Node : Node_Id;
234 begin
235 case Nkind (Parent (N)) is
237 -- Check for cases of appearing in the prefix of a construct where we
238 -- don't need atomic synchronization for this kind of usage.
240 when
241 -- Nothing to do if we are the prefix of an attribute, since we
242 -- do not want an atomic sync operation for things like 'Size.
244 N_Attribute_Reference
246 -- The N_Reference node is like an attribute
248 | N_Reference
250 -- Nothing to do for a reference to a component (or components)
251 -- of a composite object. Only reads and updates of the object
252 -- as a whole require atomic synchronization (RM C.6 (15)).
254 | N_Indexed_Component
255 | N_Selected_Component
256 | N_Slice
258 -- For all the above cases, nothing to do if we are the prefix
260 if Prefix (Parent (N)) = N then
261 return;
262 end if;
264 when others =>
265 null;
266 end case;
268 -- Nothing to do for the identifier in an object renaming declaration,
269 -- the renaming itself does not need atomic synchronization.
271 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
272 return;
273 end if;
275 -- Go ahead and set the flag
277 Set_Atomic_Sync_Required (N);
279 -- Generate info message if requested
281 if Warn_On_Atomic_Synchronization then
282 case Nkind (N) is
283 when N_Identifier =>
284 Msg_Node := N;
286 when N_Expanded_Name
287 | N_Selected_Component
289 Msg_Node := Selector_Name (N);
291 when N_Explicit_Dereference
292 | N_Indexed_Component
294 Msg_Node := Empty;
296 when others =>
297 pragma Assert (False);
298 return;
299 end case;
301 if Present (Msg_Node) then
302 Error_Msg_N
303 ("info: atomic synchronization set for &?N?", Msg_Node);
304 else
305 Error_Msg_N
306 ("info: atomic synchronization set?N?", N);
307 end if;
308 end if;
309 end Activate_Atomic_Synchronization;
311 ----------------------
312 -- Adjust_Condition --
313 ----------------------
315 procedure Adjust_Condition (N : Node_Id) is
316 begin
317 if No (N) then
318 return;
319 end if;
321 declare
322 Loc : constant Source_Ptr := Sloc (N);
323 T : constant Entity_Id := Etype (N);
324 Ti : Entity_Id;
326 begin
327 -- Defend against a call where the argument has no type, or has a
328 -- type that is not Boolean. This can occur because of prior errors.
330 if No (T) or else not Is_Boolean_Type (T) then
331 return;
332 end if;
334 -- Apply validity checking if needed
336 if Validity_Checks_On and Validity_Check_Tests then
337 Ensure_Valid (N);
338 end if;
340 -- Immediate return if standard boolean, the most common case,
341 -- where nothing needs to be done.
343 if Base_Type (T) = Standard_Boolean then
344 return;
345 end if;
347 -- Case of zero/non-zero semantics or non-standard enumeration
348 -- representation. In each case, we rewrite the node as:
350 -- ityp!(N) /= False'Enum_Rep
352 -- where ityp is an integer type with large enough size to hold any
353 -- value of type T.
355 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
356 if Esize (T) <= Esize (Standard_Integer) then
357 Ti := Standard_Integer;
358 else
359 Ti := Standard_Long_Long_Integer;
360 end if;
362 Rewrite (N,
363 Make_Op_Ne (Loc,
364 Left_Opnd => Unchecked_Convert_To (Ti, N),
365 Right_Opnd =>
366 Make_Attribute_Reference (Loc,
367 Attribute_Name => Name_Enum_Rep,
368 Prefix =>
369 New_Occurrence_Of (First_Literal (T), Loc))));
370 Analyze_And_Resolve (N, Standard_Boolean);
372 else
373 Rewrite (N, Convert_To (Standard_Boolean, N));
374 Analyze_And_Resolve (N, Standard_Boolean);
375 end if;
376 end;
377 end Adjust_Condition;
379 ------------------------
380 -- Adjust_Result_Type --
381 ------------------------
383 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
384 begin
385 -- Ignore call if current type is not Standard.Boolean
387 if Etype (N) /= Standard_Boolean then
388 return;
389 end if;
391 -- If result is already of correct type, nothing to do. Note that
392 -- this will get the most common case where everything has a type
393 -- of Standard.Boolean.
395 if Base_Type (T) = Standard_Boolean then
396 return;
398 else
399 declare
400 KP : constant Node_Kind := Nkind (Parent (N));
402 begin
403 -- If result is to be used as a Condition in the syntax, no need
404 -- to convert it back, since if it was changed to Standard.Boolean
405 -- using Adjust_Condition, that is just fine for this usage.
407 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
408 return;
410 -- If result is an operand of another logical operation, no need
411 -- to reset its type, since Standard.Boolean is just fine, and
412 -- such operations always do Adjust_Condition on their operands.
414 elsif KP in N_Op_Boolean
415 or else KP in N_Short_Circuit
416 or else KP = N_Op_Not
417 then
418 return;
420 -- Otherwise we perform a conversion from the current type, which
421 -- must be Standard.Boolean, to the desired type. Use the base
422 -- type to prevent spurious constraint checks that are extraneous
423 -- to the transformation. The type and its base have the same
424 -- representation, standard or otherwise.
426 else
427 Set_Analyzed (N);
428 Rewrite (N, Convert_To (Base_Type (T), N));
429 Analyze_And_Resolve (N, Base_Type (T));
430 end if;
431 end;
432 end if;
433 end Adjust_Result_Type;
435 --------------------------
436 -- Append_Freeze_Action --
437 --------------------------
439 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
440 Fnode : Node_Id;
442 begin
443 Ensure_Freeze_Node (T);
444 Fnode := Freeze_Node (T);
446 if No (Actions (Fnode)) then
447 Set_Actions (Fnode, New_List (N));
448 else
449 Append (N, Actions (Fnode));
450 end if;
452 end Append_Freeze_Action;
454 ---------------------------
455 -- Append_Freeze_Actions --
456 ---------------------------
458 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
459 Fnode : Node_Id;
461 begin
462 if No (L) then
463 return;
464 end if;
466 Ensure_Freeze_Node (T);
467 Fnode := Freeze_Node (T);
469 if No (Actions (Fnode)) then
470 Set_Actions (Fnode, L);
471 else
472 Append_List (L, Actions (Fnode));
473 end if;
474 end Append_Freeze_Actions;
476 ------------------------------------
477 -- Build_Allocate_Deallocate_Proc --
478 ------------------------------------
480 procedure Build_Allocate_Deallocate_Proc
481 (N : Node_Id;
482 Is_Allocate : Boolean)
484 function Find_Object (E : Node_Id) return Node_Id;
485 -- Given an arbitrary expression of an allocator, try to find an object
486 -- reference in it, otherwise return the original expression.
488 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
489 -- Determine whether subprogram Subp denotes a custom allocate or
490 -- deallocate.
492 -----------------
493 -- Find_Object --
494 -----------------
496 function Find_Object (E : Node_Id) return Node_Id is
497 Expr : Node_Id;
499 begin
500 pragma Assert (Is_Allocate);
502 Expr := E;
503 loop
504 if Nkind (Expr) = N_Explicit_Dereference then
505 Expr := Prefix (Expr);
507 elsif Nkind (Expr) = N_Qualified_Expression then
508 Expr := Expression (Expr);
510 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
512 -- When interface class-wide types are involved in allocation,
513 -- the expander introduces several levels of address arithmetic
514 -- to perform dispatch table displacement. In this scenario the
515 -- object appears as:
517 -- Tag_Ptr (Base_Address (<object>'Address))
519 -- Detect this case and utilize the whole expression as the
520 -- "object" since it now points to the proper dispatch table.
522 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
523 exit;
525 -- Continue to strip the object
527 else
528 Expr := Expression (Expr);
529 end if;
531 else
532 exit;
533 end if;
534 end loop;
536 return Expr;
537 end Find_Object;
539 ---------------------------------
540 -- Is_Allocate_Deallocate_Proc --
541 ---------------------------------
543 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
544 begin
545 -- Look for a subprogram body with only one statement which is a
546 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
548 if Ekind (Subp) = E_Procedure
549 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
550 then
551 declare
552 HSS : constant Node_Id :=
553 Handled_Statement_Sequence (Parent (Parent (Subp)));
554 Proc : Entity_Id;
556 begin
557 if Present (Statements (HSS))
558 and then Nkind (First (Statements (HSS))) =
559 N_Procedure_Call_Statement
560 then
561 Proc := Entity (Name (First (Statements (HSS))));
563 return
564 Is_RTE (Proc, RE_Allocate_Any_Controlled)
565 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
566 end if;
567 end;
568 end if;
570 return False;
571 end Is_Allocate_Deallocate_Proc;
573 -- Local variables
575 Desig_Typ : Entity_Id;
576 Expr : Node_Id;
577 Needs_Fin : Boolean;
578 Pool_Id : Entity_Id;
579 Proc_To_Call : Node_Id := Empty;
580 Ptr_Typ : Entity_Id;
582 -- Start of processing for Build_Allocate_Deallocate_Proc
584 begin
585 -- Obtain the attributes of the allocation / deallocation
587 if Nkind (N) = N_Free_Statement then
588 Expr := Expression (N);
589 Ptr_Typ := Base_Type (Etype (Expr));
590 Proc_To_Call := Procedure_To_Call (N);
592 else
593 if Nkind (N) = N_Object_Declaration then
594 Expr := Expression (N);
595 else
596 Expr := N;
597 end if;
599 -- In certain cases an allocator with a qualified expression may
600 -- be relocated and used as the initialization expression of a
601 -- temporary:
603 -- before:
604 -- Obj : Ptr_Typ := new Desig_Typ'(...);
606 -- after:
607 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
608 -- Obj : Ptr_Typ := Tmp;
610 -- Since the allocator is always marked as analyzed to avoid infinite
611 -- expansion, it will never be processed by this routine given that
612 -- the designated type needs finalization actions. Detect this case
613 -- and complete the expansion of the allocator.
615 if Nkind (Expr) = N_Identifier
616 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
617 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
618 then
619 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
620 return;
621 end if;
623 -- The allocator may have been rewritten into something else in which
624 -- case the expansion performed by this routine does not apply.
626 if Nkind (Expr) /= N_Allocator then
627 return;
628 end if;
630 Ptr_Typ := Base_Type (Etype (Expr));
631 Proc_To_Call := Procedure_To_Call (Expr);
632 end if;
634 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
635 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
637 -- Handle concurrent types
639 if Is_Concurrent_Type (Desig_Typ)
640 and then Present (Corresponding_Record_Type (Desig_Typ))
641 then
642 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
643 end if;
645 -- Do not process allocations / deallocations without a pool
647 if No (Pool_Id) then
648 return;
650 -- Do not process allocations on / deallocations from the secondary
651 -- stack.
653 elsif Is_RTE (Pool_Id, RE_SS_Pool) then
654 return;
656 -- Optimize the case where we are using the default Global_Pool_Object,
657 -- and we don't need the heavy finalization machinery.
659 elsif Pool_Id = RTE (RE_Global_Pool_Object)
660 and then not Needs_Finalization (Desig_Typ)
661 then
662 return;
664 -- Do not replicate the machinery if the allocator / free has already
665 -- been expanded and has a custom Allocate / Deallocate.
667 elsif Present (Proc_To_Call)
668 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
669 then
670 return;
671 end if;
673 -- Finalization actions are required when the object to be allocated or
674 -- deallocated needs these actions and the associated access type is not
675 -- subject to pragma No_Heap_Finalization.
677 Needs_Fin :=
678 Needs_Finalization (Desig_Typ)
679 and then not No_Heap_Finalization (Ptr_Typ);
681 if Needs_Fin then
683 -- Certain run-time configurations and targets do not provide support
684 -- for controlled types.
686 if Restriction_Active (No_Finalization) then
687 return;
689 -- Do nothing if the access type may never allocate / deallocate
690 -- objects.
692 elsif No_Pool_Assigned (Ptr_Typ) then
693 return;
694 end if;
696 -- The allocation / deallocation of a controlled object must be
697 -- chained on / detached from a finalization master.
699 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
701 -- The only other kind of allocation / deallocation supported by this
702 -- routine is on / from a subpool.
704 elsif Nkind (Expr) = N_Allocator
705 and then No (Subpool_Handle_Name (Expr))
706 then
707 return;
708 end if;
710 declare
711 Loc : constant Source_Ptr := Sloc (N);
712 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
713 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
714 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
715 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
717 Actuals : List_Id;
718 Fin_Addr_Id : Entity_Id;
719 Fin_Mas_Act : Node_Id;
720 Fin_Mas_Id : Entity_Id;
721 Proc_To_Call : Entity_Id;
722 Subpool : Node_Id := Empty;
724 begin
725 -- Step 1: Construct all the actuals for the call to library routine
726 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
728 -- a) Storage pool
730 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
732 if Is_Allocate then
734 -- b) Subpool
736 if Nkind (Expr) = N_Allocator then
737 Subpool := Subpool_Handle_Name (Expr);
738 end if;
740 -- If a subpool is present it can be an arbitrary name, so make
741 -- the actual by copying the tree.
743 if Present (Subpool) then
744 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
745 else
746 Append_To (Actuals, Make_Null (Loc));
747 end if;
749 -- c) Finalization master
751 if Needs_Fin then
752 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
753 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
755 -- Handle the case where the master is actually a pointer to a
756 -- master. This case arises in build-in-place functions.
758 if Is_Access_Type (Etype (Fin_Mas_Id)) then
759 Append_To (Actuals, Fin_Mas_Act);
760 else
761 Append_To (Actuals,
762 Make_Attribute_Reference (Loc,
763 Prefix => Fin_Mas_Act,
764 Attribute_Name => Name_Unrestricted_Access));
765 end if;
766 else
767 Append_To (Actuals, Make_Null (Loc));
768 end if;
770 -- d) Finalize_Address
772 -- Primitive Finalize_Address is never generated in CodePeer mode
773 -- since it contains an Unchecked_Conversion.
775 if Needs_Fin and then not CodePeer_Mode then
776 Fin_Addr_Id := Finalize_Address (Desig_Typ);
777 pragma Assert (Present (Fin_Addr_Id));
779 Append_To (Actuals,
780 Make_Attribute_Reference (Loc,
781 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
782 Attribute_Name => Name_Unrestricted_Access));
783 else
784 Append_To (Actuals, Make_Null (Loc));
785 end if;
786 end if;
788 -- e) Address
789 -- f) Storage_Size
790 -- g) Alignment
792 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
793 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
795 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
796 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
798 -- For deallocation of class-wide types we obtain the value of
799 -- alignment from the Type Specific Record of the deallocated object.
800 -- This is needed because the frontend expansion of class-wide types
801 -- into equivalent types confuses the back end.
803 else
804 -- Generate:
805 -- Obj.all'Alignment
807 -- ... because 'Alignment applied to class-wide types is expanded
808 -- into the code that reads the value of alignment from the TSD
809 -- (see Expand_N_Attribute_Reference)
811 Append_To (Actuals,
812 Unchecked_Convert_To (RTE (RE_Storage_Offset),
813 Make_Attribute_Reference (Loc,
814 Prefix =>
815 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
816 Attribute_Name => Name_Alignment)));
817 end if;
819 -- h) Is_Controlled
821 if Needs_Fin then
822 Is_Controlled : declare
823 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
824 Flag_Expr : Node_Id;
825 Param : Node_Id;
826 Temp : Node_Id;
828 begin
829 if Is_Allocate then
830 Temp := Find_Object (Expression (Expr));
831 else
832 Temp := Expr;
833 end if;
835 -- Processing for allocations where the expression is a subtype
836 -- indication.
838 if Is_Allocate
839 and then Is_Entity_Name (Temp)
840 and then Is_Type (Entity (Temp))
841 then
842 Flag_Expr :=
843 New_Occurrence_Of
844 (Boolean_Literals
845 (Needs_Finalization (Entity (Temp))), Loc);
847 -- The allocation / deallocation of a class-wide object relies
848 -- on a runtime check to determine whether the object is truly
849 -- controlled or not. Depending on this check, the finalization
850 -- machinery will request or reclaim extra storage reserved for
851 -- a list header.
853 elsif Is_Class_Wide_Type (Desig_Typ) then
855 -- Detect a special case where interface class-wide types
856 -- are involved as the object appears as:
858 -- Tag_Ptr (Base_Address (<object>'Address))
860 -- The expression already yields the proper tag, generate:
862 -- Temp.all
864 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
865 Param :=
866 Make_Explicit_Dereference (Loc,
867 Prefix => Relocate_Node (Temp));
869 -- In the default case, obtain the tag of the object about
870 -- to be allocated / deallocated. Generate:
872 -- Temp'Tag
874 else
875 Param :=
876 Make_Attribute_Reference (Loc,
877 Prefix => Relocate_Node (Temp),
878 Attribute_Name => Name_Tag);
879 end if;
881 -- Generate:
882 -- Needs_Finalization (<Param>)
884 Flag_Expr :=
885 Make_Function_Call (Loc,
886 Name =>
887 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
888 Parameter_Associations => New_List (Param));
890 -- Processing for generic actuals
892 elsif Is_Generic_Actual_Type (Desig_Typ) then
893 Flag_Expr :=
894 New_Occurrence_Of (Boolean_Literals
895 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
897 -- The object does not require any specialized checks, it is
898 -- known to be controlled.
900 else
901 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
902 end if;
904 -- Create the temporary which represents the finalization state
905 -- of the expression. Generate:
907 -- F : constant Boolean := <Flag_Expr>;
909 Insert_Action (N,
910 Make_Object_Declaration (Loc,
911 Defining_Identifier => Flag_Id,
912 Constant_Present => True,
913 Object_Definition =>
914 New_Occurrence_Of (Standard_Boolean, Loc),
915 Expression => Flag_Expr));
917 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
918 end Is_Controlled;
920 -- The object is not controlled
922 else
923 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
924 end if;
926 -- i) On_Subpool
928 if Is_Allocate then
929 Append_To (Actuals,
930 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
931 end if;
933 -- Step 2: Build a wrapper Allocate / Deallocate which internally
934 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
936 -- Select the proper routine to call
938 if Is_Allocate then
939 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
940 else
941 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
942 end if;
944 -- Create a custom Allocate / Deallocate routine which has identical
945 -- profile to that of System.Storage_Pools.
947 Insert_Action (N,
948 Make_Subprogram_Body (Loc,
949 Specification =>
951 -- procedure Pnn
953 Make_Procedure_Specification (Loc,
954 Defining_Unit_Name => Proc_Id,
955 Parameter_Specifications => New_List (
957 -- P : Root_Storage_Pool
959 Make_Parameter_Specification (Loc,
960 Defining_Identifier => Make_Temporary (Loc, 'P'),
961 Parameter_Type =>
962 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
964 -- A : [out] Address
966 Make_Parameter_Specification (Loc,
967 Defining_Identifier => Addr_Id,
968 Out_Present => Is_Allocate,
969 Parameter_Type =>
970 New_Occurrence_Of (RTE (RE_Address), Loc)),
972 -- S : Storage_Count
974 Make_Parameter_Specification (Loc,
975 Defining_Identifier => Size_Id,
976 Parameter_Type =>
977 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
979 -- L : Storage_Count
981 Make_Parameter_Specification (Loc,
982 Defining_Identifier => Alig_Id,
983 Parameter_Type =>
984 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
986 Declarations => No_List,
988 Handled_Statement_Sequence =>
989 Make_Handled_Sequence_Of_Statements (Loc,
990 Statements => New_List (
991 Make_Procedure_Call_Statement (Loc,
992 Name =>
993 New_Occurrence_Of (Proc_To_Call, Loc),
994 Parameter_Associations => Actuals)))),
995 Suppress => All_Checks);
997 -- The newly generated Allocate / Deallocate becomes the default
998 -- procedure to call when the back end processes the allocation /
999 -- deallocation.
1001 if Is_Allocate then
1002 Set_Procedure_To_Call (Expr, Proc_Id);
1003 else
1004 Set_Procedure_To_Call (N, Proc_Id);
1005 end if;
1006 end;
1007 end Build_Allocate_Deallocate_Proc;
1009 -------------------------------
1010 -- Build_Abort_Undefer_Block --
1011 -------------------------------
1013 function Build_Abort_Undefer_Block
1014 (Loc : Source_Ptr;
1015 Stmts : List_Id;
1016 Context : Node_Id) return Node_Id
1018 Exceptions_OK : constant Boolean :=
1019 not Restriction_Active (No_Exception_Propagation);
1021 AUD : Entity_Id;
1022 Blk : Node_Id;
1023 Blk_Id : Entity_Id;
1024 HSS : Node_Id;
1026 begin
1027 -- The block should be generated only when undeferring abort in the
1028 -- context of a potential exception.
1030 pragma Assert (Abort_Allowed and Exceptions_OK);
1032 -- Generate:
1033 -- begin
1034 -- <Stmts>
1035 -- at end
1036 -- Abort_Undefer_Direct;
1037 -- end;
1039 AUD := RTE (RE_Abort_Undefer_Direct);
1041 HSS :=
1042 Make_Handled_Sequence_Of_Statements (Loc,
1043 Statements => Stmts,
1044 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1046 Blk :=
1047 Make_Block_Statement (Loc,
1048 Handled_Statement_Sequence => HSS);
1049 Set_Is_Abort_Block (Blk);
1051 Add_Block_Identifier (Blk, Blk_Id);
1052 Expand_At_End_Handler (HSS, Blk_Id);
1054 -- Present the Abort_Undefer_Direct function to the back end to inline
1055 -- the call to the routine.
1057 Add_Inlined_Body (AUD, Context);
1059 return Blk;
1060 end Build_Abort_Undefer_Block;
1062 ---------------------------------
1063 -- Build_Class_Wide_Expression --
1064 ---------------------------------
1066 procedure Build_Class_Wide_Expression
1067 (Prag : Node_Id;
1068 Subp : Entity_Id;
1069 Par_Subp : Entity_Id;
1070 Adjust_Sloc : Boolean;
1071 Needs_Wrapper : out Boolean)
1073 function Replace_Entity (N : Node_Id) return Traverse_Result;
1074 -- Replace reference to formal of inherited operation or to primitive
1075 -- operation of root type, with corresponding entity for derived type,
1076 -- when constructing the class-wide condition of an overriding
1077 -- subprogram.
1079 --------------------
1080 -- Replace_Entity --
1081 --------------------
1083 function Replace_Entity (N : Node_Id) return Traverse_Result is
1084 New_E : Entity_Id;
1086 begin
1087 if Adjust_Sloc then
1088 Adjust_Inherited_Pragma_Sloc (N);
1089 end if;
1091 if Nkind (N) = N_Identifier
1092 and then Present (Entity (N))
1093 and then
1094 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1095 and then
1096 (Nkind (Parent (N)) /= N_Attribute_Reference
1097 or else Attribute_Name (Parent (N)) /= Name_Class)
1098 then
1099 -- The replacement does not apply to dispatching calls within the
1100 -- condition, but only to calls whose static tag is that of the
1101 -- parent type.
1103 if Is_Subprogram (Entity (N))
1104 and then Nkind (Parent (N)) = N_Function_Call
1105 and then Present (Controlling_Argument (Parent (N)))
1106 then
1107 return OK;
1108 end if;
1110 -- Determine whether entity has a renaming
1112 New_E := Type_Map.Get (Entity (N));
1114 if Present (New_E) then
1115 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1117 -- If the entity is an overridden primitive and we are not
1118 -- in GNATprove mode, we must build a wrapper for the current
1119 -- inherited operation. If the reference is the prefix of an
1120 -- attribute such as 'Result (or others ???) there is no need
1121 -- for a wrapper: the condition is just rewritten in terms of
1122 -- the inherited subprogram.
1124 if Is_Subprogram (New_E)
1125 and then Nkind (Parent (N)) /= N_Attribute_Reference
1126 and then not GNATprove_Mode
1127 then
1128 Needs_Wrapper := True;
1129 end if;
1130 end if;
1132 -- Check that there are no calls left to abstract operations if
1133 -- the current subprogram is not abstract.
1135 if Nkind (Parent (N)) = N_Function_Call
1136 and then N = Name (Parent (N))
1137 then
1138 if not Is_Abstract_Subprogram (Subp)
1139 and then Is_Abstract_Subprogram (Entity (N))
1140 then
1141 Error_Msg_Sloc := Sloc (Current_Scope);
1142 Error_Msg_Node_2 := Subp;
1143 if Comes_From_Source (Subp) then
1144 Error_Msg_NE
1145 ("cannot call abstract subprogram & in inherited "
1146 & "condition for&#", Subp, Entity (N));
1147 else
1148 Error_Msg_NE
1149 ("cannot call abstract subprogram & in inherited "
1150 & "condition for inherited&#", Subp, Entity (N));
1151 end if;
1153 -- In SPARK mode, reject an inherited condition for an
1154 -- inherited operation if it contains a call to an overriding
1155 -- operation, because this implies that the pre/postconditions
1156 -- of the inherited operation have changed silently.
1158 elsif SPARK_Mode = On
1159 and then Warn_On_Suspicious_Contract
1160 and then Present (Alias (Subp))
1161 and then Present (New_E)
1162 and then Comes_From_Source (New_E)
1163 then
1164 Error_Msg_N
1165 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1166 Parent (Subp));
1167 Error_Msg_Sloc := Sloc (New_E);
1168 Error_Msg_Node_2 := Subp;
1169 Error_Msg_NE
1170 ("\overriding of&# forces overriding of&",
1171 Parent (Subp), New_E);
1172 end if;
1173 end if;
1175 -- Update type of function call node, which should be the same as
1176 -- the function's return type.
1178 if Is_Subprogram (Entity (N))
1179 and then Nkind (Parent (N)) = N_Function_Call
1180 then
1181 Set_Etype (Parent (N), Etype (Entity (N)));
1182 end if;
1184 -- The whole expression will be reanalyzed
1186 elsif Nkind (N) in N_Has_Etype then
1187 Set_Analyzed (N, False);
1188 end if;
1190 return OK;
1191 end Replace_Entity;
1193 procedure Replace_Condition_Entities is
1194 new Traverse_Proc (Replace_Entity);
1196 -- Local variables
1198 Par_Formal : Entity_Id;
1199 Subp_Formal : Entity_Id;
1201 -- Start of processing for Build_Class_Wide_Expression
1203 begin
1204 Needs_Wrapper := False;
1206 -- Add mapping from old formals to new formals
1208 Par_Formal := First_Formal (Par_Subp);
1209 Subp_Formal := First_Formal (Subp);
1211 while Present (Par_Formal) and then Present (Subp_Formal) loop
1212 Type_Map.Set (Par_Formal, Subp_Formal);
1213 Next_Formal (Par_Formal);
1214 Next_Formal (Subp_Formal);
1215 end loop;
1217 Replace_Condition_Entities (Prag);
1218 end Build_Class_Wide_Expression;
1220 --------------------
1221 -- Build_DIC_Call --
1222 --------------------
1224 function Build_DIC_Call
1225 (Loc : Source_Ptr;
1226 Obj_Id : Entity_Id;
1227 Typ : Entity_Id) return Node_Id
1229 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1230 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1232 begin
1233 return
1234 Make_Procedure_Call_Statement (Loc,
1235 Name => New_Occurrence_Of (Proc_Id, Loc),
1236 Parameter_Associations => New_List (
1237 Make_Unchecked_Type_Conversion (Loc,
1238 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1239 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1240 end Build_DIC_Call;
1242 ------------------------------
1243 -- Build_DIC_Procedure_Body --
1244 ------------------------------
1246 -- WARNING: This routine manages Ghost regions. Return statements must be
1247 -- replaced by gotos which jump to the end of the routine and restore the
1248 -- Ghost mode.
1250 procedure Build_DIC_Procedure_Body
1251 (Typ : Entity_Id;
1252 For_Freeze : Boolean := False)
1254 procedure Add_DIC_Check
1255 (DIC_Prag : Node_Id;
1256 DIC_Expr : Node_Id;
1257 Stmts : in out List_Id);
1258 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1259 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1260 -- is added to list Stmts.
1262 procedure Add_Inherited_DIC
1263 (DIC_Prag : Node_Id;
1264 Par_Typ : Entity_Id;
1265 Deriv_Typ : Entity_Id;
1266 Stmts : in out List_Id);
1267 -- Add a runtime check to verify the assertion expression of inherited
1268 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1269 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1270 -- pragma. All generated code is added to list Stmts.
1272 procedure Add_Inherited_Tagged_DIC
1273 (DIC_Prag : Node_Id;
1274 Par_Typ : Entity_Id;
1275 Deriv_Typ : Entity_Id;
1276 Stmts : in out List_Id);
1277 -- Add a runtime check to verify assertion expression DIC_Expr of
1278 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1279 -- postcondition-like runtime semantics to the check. Par_Typ is the
1280 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1281 -- derived type inheriting the DIC pragma. All generated code is added
1282 -- to list Stmts.
1284 procedure Add_Own_DIC
1285 (DIC_Prag : Node_Id;
1286 DIC_Typ : Entity_Id;
1287 Stmts : in out List_Id);
1288 -- Add a runtime check to verify the assertion expression of pragma
1289 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1290 -- is added to list Stmts.
1292 -------------------
1293 -- Add_DIC_Check --
1294 -------------------
1296 procedure Add_DIC_Check
1297 (DIC_Prag : Node_Id;
1298 DIC_Expr : Node_Id;
1299 Stmts : in out List_Id)
1301 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1302 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1304 begin
1305 -- The DIC pragma is ignored, nothing left to do
1307 if Is_Ignored (DIC_Prag) then
1308 null;
1310 -- Otherwise the DIC expression must be checked at run time.
1311 -- Generate:
1313 -- pragma Check (<Nam>, <DIC_Expr>);
1315 else
1316 Append_New_To (Stmts,
1317 Make_Pragma (Loc,
1318 Pragma_Identifier =>
1319 Make_Identifier (Loc, Name_Check),
1321 Pragma_Argument_Associations => New_List (
1322 Make_Pragma_Argument_Association (Loc,
1323 Expression => Make_Identifier (Loc, Nam)),
1325 Make_Pragma_Argument_Association (Loc,
1326 Expression => DIC_Expr))));
1327 end if;
1328 end Add_DIC_Check;
1330 -----------------------
1331 -- Add_Inherited_DIC --
1332 -----------------------
1334 procedure Add_Inherited_DIC
1335 (DIC_Prag : Node_Id;
1336 Par_Typ : Entity_Id;
1337 Deriv_Typ : Entity_Id;
1338 Stmts : in out List_Id)
1340 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1341 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1342 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1343 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1344 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1346 begin
1347 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1349 -- Verify the inherited DIC assertion expression by calling the DIC
1350 -- procedure of the parent type.
1352 -- Generate:
1353 -- <Par_Typ>DIC (Par_Typ (_object));
1355 Append_New_To (Stmts,
1356 Make_Procedure_Call_Statement (Loc,
1357 Name => New_Occurrence_Of (Par_Proc, Loc),
1358 Parameter_Associations => New_List (
1359 Convert_To
1360 (Typ => Etype (Par_Obj),
1361 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1362 end Add_Inherited_DIC;
1364 ------------------------------
1365 -- Add_Inherited_Tagged_DIC --
1366 ------------------------------
1368 procedure Add_Inherited_Tagged_DIC
1369 (DIC_Prag : Node_Id;
1370 Par_Typ : Entity_Id;
1371 Deriv_Typ : Entity_Id;
1372 Stmts : in out List_Id)
1374 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1375 DIC_Args : constant List_Id :=
1376 Pragma_Argument_Associations (DIC_Prag);
1377 DIC_Arg : constant Node_Id := First (DIC_Args);
1378 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1379 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1381 Expr : Node_Id;
1383 begin
1384 -- The processing of an inherited DIC assertion expression starts off
1385 -- with a copy of the original parent expression where all references
1386 -- to the parent type have already been replaced with references to
1387 -- the _object formal parameter of the parent type's DIC procedure.
1389 pragma Assert (Present (DIC_Expr));
1390 Expr := New_Copy_Tree (DIC_Expr);
1392 -- Perform the following substitutions:
1394 -- * Replace a reference to the _object parameter of the parent
1395 -- type's DIC procedure with a reference to the _object parameter
1396 -- of the derived types' DIC procedure.
1398 -- * Replace a reference to a discriminant of the parent type with
1399 -- a suitable value from the point of view of the derived type.
1401 -- * Replace a call to an overridden parent primitive with a call
1402 -- to the overriding derived type primitive.
1404 -- * Replace a call to an inherited parent primitive with a call to
1405 -- the internally-generated inherited derived type primitive.
1407 -- Note that primitives defined in the private part are automatically
1408 -- handled by the overriding/inheritance mechanism and do not require
1409 -- an extra replacement pass.
1411 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1413 Replace_References
1414 (Expr => Expr,
1415 Par_Typ => Par_Typ,
1416 Deriv_Typ => Deriv_Typ,
1417 Par_Obj => First_Formal (Par_Proc),
1418 Deriv_Obj => First_Formal (Deriv_Proc));
1420 -- Once the DIC assertion expression is fully processed, add a check
1421 -- to the statements of the DIC procedure.
1423 Add_DIC_Check
1424 (DIC_Prag => DIC_Prag,
1425 DIC_Expr => Expr,
1426 Stmts => Stmts);
1427 end Add_Inherited_Tagged_DIC;
1429 -----------------
1430 -- Add_Own_DIC --
1431 -----------------
1433 procedure Add_Own_DIC
1434 (DIC_Prag : Node_Id;
1435 DIC_Typ : Entity_Id;
1436 Stmts : in out List_Id)
1438 DIC_Args : constant List_Id :=
1439 Pragma_Argument_Associations (DIC_Prag);
1440 DIC_Arg : constant Node_Id := First (DIC_Args);
1441 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1442 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1443 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1444 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1446 procedure Preanalyze_Own_DIC_For_ASIS;
1447 -- Preanalyze the original DIC expression of an aspect or a source
1448 -- pragma for ASIS.
1450 ---------------------------------
1451 -- Preanalyze_Own_DIC_For_ASIS --
1452 ---------------------------------
1454 procedure Preanalyze_Own_DIC_For_ASIS is
1455 Expr : Node_Id := Empty;
1457 begin
1458 -- The DIC pragma is a source construct, preanalyze the original
1459 -- expression of the pragma.
1461 if Comes_From_Source (DIC_Prag) then
1462 Expr := DIC_Expr;
1464 -- Otherwise preanalyze the expression of the corresponding aspect
1466 elsif Present (DIC_Asp) then
1467 Expr := Expression (DIC_Asp);
1468 end if;
1470 -- The expression must be subjected to the same substitutions as
1471 -- the copy used in the generation of the runtime check.
1473 if Present (Expr) then
1474 Replace_Type_References
1475 (Expr => Expr,
1476 Typ => DIC_Typ,
1477 Obj_Id => Obj_Id);
1479 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1480 end if;
1481 end Preanalyze_Own_DIC_For_ASIS;
1483 -- Local variables
1485 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1487 Expr : Node_Id;
1489 -- Start of processing for Add_Own_DIC
1491 begin
1492 Expr := New_Copy_Tree (DIC_Expr);
1494 -- Perform the following substitution:
1496 -- * Replace the current instance of DIC_Typ with a reference to
1497 -- the _object formal parameter of the DIC procedure.
1499 Replace_Type_References
1500 (Expr => Expr,
1501 Typ => DIC_Typ,
1502 Obj_Id => Obj_Id);
1504 -- Preanalyze the DIC expression to detect errors and at the same
1505 -- time capture the visibility of the proper package part.
1507 Set_Parent (Expr, Typ_Decl);
1508 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1510 -- Save a copy of the expression with all replacements and analysis
1511 -- already taken place in case a derived type inherits the pragma.
1512 -- The copy will be used as the foundation of the derived type's own
1513 -- version of the DIC assertion expression.
1515 if Is_Tagged_Type (DIC_Typ) then
1516 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1517 end if;
1519 -- If the pragma comes from an aspect specification, replace the
1520 -- saved expression because all type references must be substituted
1521 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1522 -- routines.
1524 if Present (DIC_Asp) then
1525 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1526 end if;
1528 -- Preanalyze the original DIC expression for ASIS
1530 if ASIS_Mode then
1531 Preanalyze_Own_DIC_For_ASIS;
1532 end if;
1534 -- Once the DIC assertion expression is fully processed, add a check
1535 -- to the statements of the DIC procedure.
1537 Add_DIC_Check
1538 (DIC_Prag => DIC_Prag,
1539 DIC_Expr => Expr,
1540 Stmts => Stmts);
1541 end Add_Own_DIC;
1543 -- Local variables
1545 Loc : constant Source_Ptr := Sloc (Typ);
1547 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1548 -- Save the Ghost mode to restore on exit
1550 DIC_Prag : Node_Id;
1551 DIC_Typ : Entity_Id;
1552 Dummy_1 : Entity_Id;
1553 Dummy_2 : Entity_Id;
1554 Proc_Body : Node_Id;
1555 Proc_Body_Id : Entity_Id;
1556 Proc_Decl : Node_Id;
1557 Proc_Id : Entity_Id;
1558 Stmts : List_Id := No_List;
1560 Build_Body : Boolean := False;
1561 -- Flag set when the type requires a DIC procedure body to be built
1563 Work_Typ : Entity_Id;
1564 -- The working type
1566 -- Start of processing for Build_DIC_Procedure_Body
1568 begin
1569 Work_Typ := Base_Type (Typ);
1571 -- Do not process class-wide types as these are Itypes, but lack a first
1572 -- subtype (see below).
1574 if Is_Class_Wide_Type (Work_Typ) then
1575 return;
1577 -- Do not process the underlying full view of a private type. There is
1578 -- no way to get back to the partial view, plus the body will be built
1579 -- by the full view or the base type.
1581 elsif Is_Underlying_Full_View (Work_Typ) then
1582 return;
1584 -- Use the first subtype when dealing with various base types
1586 elsif Is_Itype (Work_Typ) then
1587 Work_Typ := First_Subtype (Work_Typ);
1589 -- The input denotes the corresponding record type of a protected or a
1590 -- task type. Work with the concurrent type because the corresponding
1591 -- record type may not be visible to clients of the type.
1593 elsif Ekind (Work_Typ) = E_Record_Type
1594 and then Is_Concurrent_Record_Type (Work_Typ)
1595 then
1596 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1597 end if;
1599 -- The working type may be subject to pragma Ghost. Set the mode now to
1600 -- ensure that the DIC procedure is properly marked as Ghost.
1602 Set_Ghost_Mode (Work_Typ);
1604 -- The working type must be either define a DIC pragma of its own or
1605 -- inherit one from a parent type.
1607 pragma Assert (Has_DIC (Work_Typ));
1609 -- Recover the type which defines the DIC pragma. This is either the
1610 -- working type itself or a parent type when the pragma is inherited.
1612 DIC_Typ := Find_DIC_Type (Work_Typ);
1613 pragma Assert (Present (DIC_Typ));
1615 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1616 pragma Assert (Present (DIC_Prag));
1618 -- Nothing to do if pragma DIC appears without an argument or its sole
1619 -- argument is "null".
1621 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1622 goto Leave;
1623 end if;
1625 -- The working type may lack a DIC procedure declaration. This may be
1626 -- due to several reasons:
1628 -- * The working type's own DIC pragma does not contain a verifiable
1629 -- assertion expression. In this case there is no need to build a
1630 -- DIC procedure because there is nothing to check.
1632 -- * The working type derives from a parent type. In this case a DIC
1633 -- procedure should be built only when the inherited DIC pragma has
1634 -- a verifiable assertion expression.
1636 Proc_Id := DIC_Procedure (Work_Typ);
1638 -- Build a DIC procedure declaration when the working type derives from
1639 -- a parent type.
1641 if No (Proc_Id) then
1642 Build_DIC_Procedure_Declaration (Work_Typ);
1643 Proc_Id := DIC_Procedure (Work_Typ);
1644 end if;
1646 -- At this point there should be a DIC procedure declaration
1648 pragma Assert (Present (Proc_Id));
1649 Proc_Decl := Unit_Declaration_Node (Proc_Id);
1651 -- Nothing to do if the DIC procedure already has a body
1653 if Present (Corresponding_Body (Proc_Decl)) then
1654 goto Leave;
1655 end if;
1657 -- Emulate the environment of the DIC procedure by installing its scope
1658 -- and formal parameters.
1660 Push_Scope (Proc_Id);
1661 Install_Formals (Proc_Id);
1663 -- The working type defines its own DIC pragma. Replace the current
1664 -- instance of the working type with the formal of the DIC procedure.
1665 -- Note that there is no need to consider inherited DIC pragmas from
1666 -- parent types because the working type's DIC pragma "hides" all
1667 -- inherited DIC pragmas.
1669 if Has_Own_DIC (Work_Typ) then
1670 pragma Assert (DIC_Typ = Work_Typ);
1672 Add_Own_DIC
1673 (DIC_Prag => DIC_Prag,
1674 DIC_Typ => DIC_Typ,
1675 Stmts => Stmts);
1677 Build_Body := True;
1679 -- Otherwise the working type inherits a DIC pragma from a parent type.
1680 -- This processing is carried out when the type is frozen because the
1681 -- state of all parent discriminants is known at that point. Note that
1682 -- it is semantically sound to delay the creation of the DIC procedure
1683 -- body till the freeze point. If the type has a DIC pragma of its own,
1684 -- then the DIC procedure body would have already been constructed at
1685 -- the end of the visible declarations and all parent DIC pragmas are
1686 -- effectively "hidden" and irrelevant.
1688 elsif For_Freeze then
1689 pragma Assert (Has_Inherited_DIC (Work_Typ));
1690 pragma Assert (DIC_Typ /= Work_Typ);
1692 -- The working type is tagged. The verification of the assertion
1693 -- expression is subject to the same semantics as class-wide pre-
1694 -- and postconditions.
1696 if Is_Tagged_Type (Work_Typ) then
1697 Add_Inherited_Tagged_DIC
1698 (DIC_Prag => DIC_Prag,
1699 Par_Typ => DIC_Typ,
1700 Deriv_Typ => Work_Typ,
1701 Stmts => Stmts);
1703 -- Otherwise the working type is not tagged. Verify the assertion
1704 -- expression of the inherited DIC pragma by directly calling the
1705 -- DIC procedure of the parent type.
1707 else
1708 Add_Inherited_DIC
1709 (DIC_Prag => DIC_Prag,
1710 Par_Typ => DIC_Typ,
1711 Deriv_Typ => Work_Typ,
1712 Stmts => Stmts);
1713 end if;
1715 Build_Body := True;
1716 end if;
1718 End_Scope;
1720 if Build_Body then
1722 -- Produce an empty completing body in the following cases:
1723 -- * Assertions are disabled
1724 -- * The DIC Assertion_Policy is Ignore
1725 -- * Pragma DIC appears without an argument
1726 -- * Pragma DIC appears with argument "null"
1728 if No (Stmts) then
1729 Stmts := New_List (Make_Null_Statement (Loc));
1730 end if;
1732 -- Generate:
1733 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1734 -- begin
1735 -- <Stmts>
1736 -- end <Work_Typ>DIC;
1738 Proc_Body :=
1739 Make_Subprogram_Body (Loc,
1740 Specification =>
1741 Copy_Subprogram_Spec (Parent (Proc_Id)),
1742 Declarations => Empty_List,
1743 Handled_Statement_Sequence =>
1744 Make_Handled_Sequence_Of_Statements (Loc,
1745 Statements => Stmts));
1746 Proc_Body_Id := Defining_Entity (Proc_Body);
1748 -- Perform minor decoration in case the body is not analyzed
1750 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1751 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1752 Set_Scope (Proc_Body_Id, Current_Scope);
1754 -- Link both spec and body to avoid generating duplicates
1756 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1757 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1759 -- The body should not be inserted into the tree when the context
1760 -- is ASIS or a generic unit because it is not part of the template.
1761 -- Note that the body must still be generated in order to resolve the
1762 -- DIC assertion expression.
1764 if ASIS_Mode or Inside_A_Generic then
1765 null;
1767 -- Semi-insert the body into the tree for GNATprove by setting its
1768 -- Parent field. This allows for proper upstream tree traversals.
1770 elsif GNATprove_Mode then
1771 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1773 -- Otherwise the body is part of the freezing actions of the working
1774 -- type.
1776 else
1777 Append_Freeze_Action (Work_Typ, Proc_Body);
1778 end if;
1779 end if;
1781 <<Leave>>
1782 Restore_Ghost_Mode (Saved_GM);
1783 end Build_DIC_Procedure_Body;
1785 -------------------------------------
1786 -- Build_DIC_Procedure_Declaration --
1787 -------------------------------------
1789 -- WARNING: This routine manages Ghost regions. Return statements must be
1790 -- replaced by gotos which jump to the end of the routine and restore the
1791 -- Ghost mode.
1793 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1794 Loc : constant Source_Ptr := Sloc (Typ);
1796 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1797 -- Save the Ghost mode to restore on exit
1799 DIC_Prag : Node_Id;
1800 DIC_Typ : Entity_Id;
1801 Proc_Decl : Node_Id;
1802 Proc_Id : Entity_Id;
1803 Typ_Decl : Node_Id;
1805 CRec_Typ : Entity_Id;
1806 -- The corresponding record type of Full_Typ
1808 Full_Base : Entity_Id;
1809 -- The base type of Full_Typ
1811 Full_Typ : Entity_Id;
1812 -- The full view of working type
1814 Obj_Id : Entity_Id;
1815 -- The _object formal parameter of the DIC procedure
1817 Priv_Typ : Entity_Id;
1818 -- The partial view of working type
1820 Work_Typ : Entity_Id;
1821 -- The working type
1823 begin
1824 Work_Typ := Base_Type (Typ);
1826 -- Do not process class-wide types as these are Itypes, but lack a first
1827 -- subtype (see below).
1829 if Is_Class_Wide_Type (Work_Typ) then
1830 return;
1832 -- Do not process the underlying full view of a private type. There is
1833 -- no way to get back to the partial view, plus the body will be built
1834 -- by the full view or the base type.
1836 elsif Is_Underlying_Full_View (Work_Typ) then
1837 return;
1839 -- Use the first subtype when dealing with various base types
1841 elsif Is_Itype (Work_Typ) then
1842 Work_Typ := First_Subtype (Work_Typ);
1844 -- The input denotes the corresponding record type of a protected or a
1845 -- task type. Work with the concurrent type because the corresponding
1846 -- record type may not be visible to clients of the type.
1848 elsif Ekind (Work_Typ) = E_Record_Type
1849 and then Is_Concurrent_Record_Type (Work_Typ)
1850 then
1851 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1852 end if;
1854 -- The working type may be subject to pragma Ghost. Set the mode now to
1855 -- ensure that the DIC procedure is properly marked as Ghost.
1857 Set_Ghost_Mode (Work_Typ);
1859 -- The type must be either subject to a DIC pragma or inherit one from a
1860 -- parent type.
1862 pragma Assert (Has_DIC (Work_Typ));
1864 -- Recover the type which defines the DIC pragma. This is either the
1865 -- working type itself or a parent type when the pragma is inherited.
1867 DIC_Typ := Find_DIC_Type (Work_Typ);
1868 pragma Assert (Present (DIC_Typ));
1870 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1871 pragma Assert (Present (DIC_Prag));
1873 -- Nothing to do if pragma DIC appears without an argument or its sole
1874 -- argument is "null".
1876 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1877 goto Leave;
1879 -- Nothing to do if the type already has a DIC procedure
1881 elsif Present (DIC_Procedure (Work_Typ)) then
1882 goto Leave;
1883 end if;
1885 Proc_Id :=
1886 Make_Defining_Identifier (Loc,
1887 Chars =>
1888 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1890 -- Perform minor decoration in case the declaration is not analyzed
1892 Set_Ekind (Proc_Id, E_Procedure);
1893 Set_Etype (Proc_Id, Standard_Void_Type);
1894 Set_Scope (Proc_Id, Current_Scope);
1896 Set_Is_DIC_Procedure (Proc_Id);
1897 Set_DIC_Procedure (Work_Typ, Proc_Id);
1899 -- The DIC procedure requires debug info when the assertion expression
1900 -- is subject to Source Coverage Obligations.
1902 if Opt.Generate_SCO then
1903 Set_Needs_Debug_Info (Proc_Id);
1904 end if;
1906 -- Obtain all views of the input type
1908 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1910 -- Associate the DIC procedure and various relevant flags with all views
1912 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1913 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1914 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1915 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1917 -- The declaration of the DIC procedure must be inserted after the
1918 -- declaration of the partial view as this allows for proper external
1919 -- visibility.
1921 if Present (Priv_Typ) then
1922 Typ_Decl := Declaration_Node (Priv_Typ);
1924 -- Derived types with the full view as parent do not have a partial
1925 -- view. Insert the DIC procedure after the derived type.
1927 else
1928 Typ_Decl := Declaration_Node (Full_Typ);
1929 end if;
1931 -- The type should have a declarative node
1933 pragma Assert (Present (Typ_Decl));
1935 -- Create the formal parameter which emulates the variable-like behavior
1936 -- of the type's current instance.
1938 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1940 -- Perform minor decoration in case the declaration is not analyzed
1942 Set_Ekind (Obj_Id, E_In_Parameter);
1943 Set_Etype (Obj_Id, Work_Typ);
1944 Set_Scope (Obj_Id, Proc_Id);
1946 Set_First_Entity (Proc_Id, Obj_Id);
1948 -- Generate:
1949 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1951 Proc_Decl :=
1952 Make_Subprogram_Declaration (Loc,
1953 Specification =>
1954 Make_Procedure_Specification (Loc,
1955 Defining_Unit_Name => Proc_Id,
1956 Parameter_Specifications => New_List (
1957 Make_Parameter_Specification (Loc,
1958 Defining_Identifier => Obj_Id,
1959 Parameter_Type =>
1960 New_Occurrence_Of (Work_Typ, Loc)))));
1962 -- The declaration should not be inserted into the tree when the context
1963 -- is ASIS or a generic unit because it is not part of the template.
1965 if ASIS_Mode or Inside_A_Generic then
1966 null;
1968 -- Semi-insert the declaration into the tree for GNATprove by setting
1969 -- its Parent field. This allows for proper upstream tree traversals.
1971 elsif GNATprove_Mode then
1972 Set_Parent (Proc_Decl, Parent (Typ_Decl));
1974 -- Otherwise insert the declaration
1976 else
1977 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
1978 end if;
1980 <<Leave>>
1981 Restore_Ghost_Mode (Saved_GM);
1982 end Build_DIC_Procedure_Declaration;
1984 ------------------------------------
1985 -- Build_Invariant_Procedure_Body --
1986 ------------------------------------
1988 -- WARNING: This routine manages Ghost regions. Return statements must be
1989 -- replaced by gotos which jump to the end of the routine and restore the
1990 -- Ghost mode.
1992 procedure Build_Invariant_Procedure_Body
1993 (Typ : Entity_Id;
1994 Partial_Invariant : Boolean := False)
1996 Loc : constant Source_Ptr := Sloc (Typ);
1998 Pragmas_Seen : Elist_Id := No_Elist;
1999 -- This list contains all invariant pragmas processed so far. The list
2000 -- is used to avoid generating redundant invariant checks.
2002 Produced_Check : Boolean := False;
2003 -- This flag tracks whether the type has produced at least one invariant
2004 -- check. The flag is used as a sanity check at the end of the routine.
2006 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2007 -- intentionally unnested to avoid deep indentation of code.
2009 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2010 -- they emit checks, loops (for arrays) and case statements (for record
2011 -- variant parts) only when there are invariants to verify. This keeps
2012 -- the body of the invariant procedure free of useless code.
2014 procedure Add_Array_Component_Invariants
2015 (T : Entity_Id;
2016 Obj_Id : Entity_Id;
2017 Checks : in out List_Id);
2018 -- Generate an invariant check for each component of array type T.
2019 -- Obj_Id denotes the entity of the _object formal parameter of the
2020 -- invariant procedure. All created checks are added to list Checks.
2022 procedure Add_Inherited_Invariants
2023 (T : Entity_Id;
2024 Priv_Typ : Entity_Id;
2025 Full_Typ : Entity_Id;
2026 Obj_Id : Entity_Id;
2027 Checks : in out List_Id);
2028 -- Generate an invariant check for each inherited class-wide invariant
2029 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2030 -- the partial and full view of the parent type. Obj_Id denotes the
2031 -- entity of the _object formal parameter of the invariant procedure.
2032 -- All created checks are added to list Checks.
2034 procedure Add_Interface_Invariants
2035 (T : Entity_Id;
2036 Obj_Id : Entity_Id;
2037 Checks : in out List_Id);
2038 -- Generate an invariant check for each inherited class-wide invariant
2039 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2040 -- entity of the _object formal parameter of the invariant procedure.
2041 -- All created checks are added to list Checks.
2043 procedure Add_Invariant_Check
2044 (Prag : Node_Id;
2045 Expr : Node_Id;
2046 Checks : in out List_Id;
2047 Inherited : Boolean := False);
2048 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2049 -- verify assertion expression Expr of pragma Prag. All generated code
2050 -- is added to list Checks. Flag Inherited should be set when the pragma
2051 -- is inherited from a parent or interface type.
2053 procedure Add_Own_Invariants
2054 (T : Entity_Id;
2055 Obj_Id : Entity_Id;
2056 Checks : in out List_Id;
2057 Priv_Item : Node_Id := Empty);
2058 -- Generate an invariant check for each invariant found for type T.
2059 -- Obj_Id denotes the entity of the _object formal parameter of the
2060 -- invariant procedure. All created checks are added to list Checks.
2061 -- Priv_Item denotes the first rep item of the private type.
2063 procedure Add_Parent_Invariants
2064 (T : Entity_Id;
2065 Obj_Id : Entity_Id;
2066 Checks : in out List_Id);
2067 -- Generate an invariant check for each inherited class-wide invariant
2068 -- coming from all parent types of type T. Obj_Id denotes the entity of
2069 -- the _object formal parameter of the invariant procedure. All created
2070 -- checks are added to list Checks.
2072 procedure Add_Record_Component_Invariants
2073 (T : Entity_Id;
2074 Obj_Id : Entity_Id;
2075 Checks : in out List_Id);
2076 -- Generate an invariant check for each component of record type T.
2077 -- Obj_Id denotes the entity of the _object formal parameter of the
2078 -- invariant procedure. All created checks are added to list Checks.
2080 ------------------------------------
2081 -- Add_Array_Component_Invariants --
2082 ------------------------------------
2084 procedure Add_Array_Component_Invariants
2085 (T : Entity_Id;
2086 Obj_Id : Entity_Id;
2087 Checks : in out List_Id)
2089 Comp_Typ : constant Entity_Id := Component_Type (T);
2090 Dims : constant Pos := Number_Dimensions (T);
2092 procedure Process_Array_Component
2093 (Indices : List_Id;
2094 Comp_Checks : in out List_Id);
2095 -- Generate an invariant check for an array component identified by
2096 -- the indices in list Indices. All created checks are added to list
2097 -- Comp_Checks.
2099 procedure Process_One_Dimension
2100 (Dim : Pos;
2101 Indices : List_Id;
2102 Dim_Checks : in out List_Id);
2103 -- Generate a loop over the Nth dimension Dim of an array type. List
2104 -- Indices contains all array indices for the dimension. All created
2105 -- checks are added to list Dim_Checks.
2107 -----------------------------
2108 -- Process_Array_Component --
2109 -----------------------------
2111 procedure Process_Array_Component
2112 (Indices : List_Id;
2113 Comp_Checks : in out List_Id)
2115 Proc_Id : Entity_Id;
2117 begin
2118 if Has_Invariants (Comp_Typ) then
2120 -- In GNATprove mode, the component invariants are checked by
2121 -- other means. They should not be added to the array type
2122 -- invariant procedure, so that the procedure can be used to
2123 -- check the array type invariants if any.
2125 if GNATprove_Mode then
2126 null;
2128 else
2129 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2131 -- The component type should have an invariant procedure
2132 -- if it has invariants of its own or inherits class-wide
2133 -- invariants from parent or interface types.
2135 pragma Assert (Present (Proc_Id));
2137 -- Generate:
2138 -- <Comp_Typ>Invariant (_object (<Indices>));
2140 -- Note that the invariant procedure may have a null body if
2141 -- assertions are disabled or Assertion_Policy Ignore is in
2142 -- effect.
2144 if not Has_Null_Body (Proc_Id) then
2145 Append_New_To (Comp_Checks,
2146 Make_Procedure_Call_Statement (Loc,
2147 Name =>
2148 New_Occurrence_Of (Proc_Id, Loc),
2149 Parameter_Associations => New_List (
2150 Make_Indexed_Component (Loc,
2151 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2152 Expressions => New_Copy_List (Indices)))));
2153 end if;
2154 end if;
2156 Produced_Check := True;
2157 end if;
2158 end Process_Array_Component;
2160 ---------------------------
2161 -- Process_One_Dimension --
2162 ---------------------------
2164 procedure Process_One_Dimension
2165 (Dim : Pos;
2166 Indices : List_Id;
2167 Dim_Checks : in out List_Id)
2169 Comp_Checks : List_Id := No_List;
2170 Index : Entity_Id;
2172 begin
2173 -- Generate the invariant checks for the array component after all
2174 -- dimensions have produced their respective loops.
2176 if Dim > Dims then
2177 Process_Array_Component
2178 (Indices => Indices,
2179 Comp_Checks => Dim_Checks);
2181 -- Otherwise create a loop for the current dimension
2183 else
2184 -- Create a new loop variable for each dimension
2186 Index :=
2187 Make_Defining_Identifier (Loc,
2188 Chars => New_External_Name ('I', Dim));
2189 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2191 Process_One_Dimension
2192 (Dim => Dim + 1,
2193 Indices => Indices,
2194 Dim_Checks => Comp_Checks);
2196 -- Generate:
2197 -- for I<Dim> in _object'Range (<Dim>) loop
2198 -- <Comp_Checks>
2199 -- end loop;
2201 -- Note that the invariant procedure may have a null body if
2202 -- assertions are disabled or Assertion_Policy Ignore is in
2203 -- effect.
2205 if Present (Comp_Checks) then
2206 Append_New_To (Dim_Checks,
2207 Make_Implicit_Loop_Statement (T,
2208 Identifier => Empty,
2209 Iteration_Scheme =>
2210 Make_Iteration_Scheme (Loc,
2211 Loop_Parameter_Specification =>
2212 Make_Loop_Parameter_Specification (Loc,
2213 Defining_Identifier => Index,
2214 Discrete_Subtype_Definition =>
2215 Make_Attribute_Reference (Loc,
2216 Prefix =>
2217 New_Occurrence_Of (Obj_Id, Loc),
2218 Attribute_Name => Name_Range,
2219 Expressions => New_List (
2220 Make_Integer_Literal (Loc, Dim))))),
2221 Statements => Comp_Checks));
2222 end if;
2223 end if;
2224 end Process_One_Dimension;
2226 -- Start of processing for Add_Array_Component_Invariants
2228 begin
2229 Process_One_Dimension
2230 (Dim => 1,
2231 Indices => New_List,
2232 Dim_Checks => Checks);
2233 end Add_Array_Component_Invariants;
2235 ------------------------------
2236 -- Add_Inherited_Invariants --
2237 ------------------------------
2239 procedure Add_Inherited_Invariants
2240 (T : Entity_Id;
2241 Priv_Typ : Entity_Id;
2242 Full_Typ : Entity_Id;
2243 Obj_Id : Entity_Id;
2244 Checks : in out List_Id)
2246 Deriv_Typ : Entity_Id;
2247 Expr : Node_Id;
2248 Prag : Node_Id;
2249 Prag_Expr : Node_Id;
2250 Prag_Expr_Arg : Node_Id;
2251 Prag_Typ : Node_Id;
2252 Prag_Typ_Arg : Node_Id;
2254 Par_Proc : Entity_Id;
2255 -- The "partial" invariant procedure of Par_Typ
2257 Par_Typ : Entity_Id;
2258 -- The suitable view of the parent type used in the substitution of
2259 -- type attributes.
2261 begin
2262 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2263 return;
2264 end if;
2266 -- When the type inheriting the class-wide invariant is a concurrent
2267 -- type, use the corresponding record type because it contains all
2268 -- primitive operations of the concurrent type and allows for proper
2269 -- substitution.
2271 if Is_Concurrent_Type (T) then
2272 Deriv_Typ := Corresponding_Record_Type (T);
2273 else
2274 Deriv_Typ := T;
2275 end if;
2277 pragma Assert (Present (Deriv_Typ));
2279 -- Determine which rep item chain to use. Precedence is given to that
2280 -- of the parent type's partial view since it usually carries all the
2281 -- class-wide invariants.
2283 if Present (Priv_Typ) then
2284 Prag := First_Rep_Item (Priv_Typ);
2285 else
2286 Prag := First_Rep_Item (Full_Typ);
2287 end if;
2289 while Present (Prag) loop
2290 if Nkind (Prag) = N_Pragma
2291 and then Pragma_Name (Prag) = Name_Invariant
2292 then
2293 -- Nothing to do if the pragma was already processed
2295 if Contains (Pragmas_Seen, Prag) then
2296 return;
2298 -- Nothing to do when the caller requests the processing of all
2299 -- inherited class-wide invariants, but the pragma does not
2300 -- fall in this category.
2302 elsif not Class_Present (Prag) then
2303 return;
2304 end if;
2306 -- Extract the arguments of the invariant pragma
2308 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2309 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2310 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2311 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2313 -- The pragma applies to the partial view of the parent type
2315 if Present (Priv_Typ)
2316 and then Entity (Prag_Typ) = Priv_Typ
2317 then
2318 Par_Typ := Priv_Typ;
2320 -- The pragma applies to the full view of the parent type
2322 elsif Present (Full_Typ)
2323 and then Entity (Prag_Typ) = Full_Typ
2324 then
2325 Par_Typ := Full_Typ;
2327 -- Otherwise the pragma does not belong to the parent type and
2328 -- should not be considered.
2330 else
2331 return;
2332 end if;
2334 -- Perform the following substitutions:
2336 -- * Replace a reference to the _object parameter of the
2337 -- parent type's partial invariant procedure with a
2338 -- reference to the _object parameter of the derived
2339 -- type's full invariant procedure.
2341 -- * Replace a reference to a discriminant of the parent type
2342 -- with a suitable value from the point of view of the
2343 -- derived type.
2345 -- * Replace a call to an overridden parent primitive with a
2346 -- call to the overriding derived type primitive.
2348 -- * Replace a call to an inherited parent primitive with a
2349 -- call to the internally-generated inherited derived type
2350 -- primitive.
2352 Expr := New_Copy_Tree (Prag_Expr);
2354 -- The parent type must have a "partial" invariant procedure
2355 -- because class-wide invariants are captured exclusively by
2356 -- it.
2358 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2359 pragma Assert (Present (Par_Proc));
2361 Replace_References
2362 (Expr => Expr,
2363 Par_Typ => Par_Typ,
2364 Deriv_Typ => Deriv_Typ,
2365 Par_Obj => First_Formal (Par_Proc),
2366 Deriv_Obj => Obj_Id);
2368 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2369 end if;
2371 Next_Rep_Item (Prag);
2372 end loop;
2373 end Add_Inherited_Invariants;
2375 ------------------------------
2376 -- Add_Interface_Invariants --
2377 ------------------------------
2379 procedure Add_Interface_Invariants
2380 (T : Entity_Id;
2381 Obj_Id : Entity_Id;
2382 Checks : in out List_Id)
2384 Iface_Elmt : Elmt_Id;
2385 Ifaces : Elist_Id;
2387 begin
2388 -- Generate an invariant check for each class-wide invariant coming
2389 -- from all interfaces implemented by type T.
2391 if Is_Tagged_Type (T) then
2392 Collect_Interfaces (T, Ifaces);
2394 -- Process the class-wide invariants of all implemented interfaces
2396 Iface_Elmt := First_Elmt (Ifaces);
2397 while Present (Iface_Elmt) loop
2399 -- The Full_Typ parameter is intentionally left Empty because
2400 -- interfaces are treated as the partial view of a private type
2401 -- in order to achieve uniformity with the general case.
2403 Add_Inherited_Invariants
2404 (T => T,
2405 Priv_Typ => Node (Iface_Elmt),
2406 Full_Typ => Empty,
2407 Obj_Id => Obj_Id,
2408 Checks => Checks);
2410 Next_Elmt (Iface_Elmt);
2411 end loop;
2412 end if;
2413 end Add_Interface_Invariants;
2415 -------------------------
2416 -- Add_Invariant_Check --
2417 -------------------------
2419 procedure Add_Invariant_Check
2420 (Prag : Node_Id;
2421 Expr : Node_Id;
2422 Checks : in out List_Id;
2423 Inherited : Boolean := False)
2425 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2426 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2427 Ploc : constant Source_Ptr := Sloc (Prag);
2428 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2430 Assoc : List_Id;
2431 Str : String_Id;
2433 begin
2434 -- The invariant is ignored, nothing left to do
2436 if Is_Ignored (Prag) then
2437 null;
2439 -- Otherwise the invariant is checked. Build a pragma Check to verify
2440 -- the expression at run time.
2442 else
2443 Assoc := New_List (
2444 Make_Pragma_Argument_Association (Ploc,
2445 Expression => Make_Identifier (Ploc, Nam)),
2446 Make_Pragma_Argument_Association (Ploc,
2447 Expression => Expr));
2449 -- Handle the String argument (if any)
2451 if Present (Str_Arg) then
2452 Str := Strval (Get_Pragma_Arg (Str_Arg));
2454 -- When inheriting an invariant, modify the message from
2455 -- "failed invariant" to "failed inherited invariant".
2457 if Inherited then
2458 String_To_Name_Buffer (Str);
2460 if Name_Buffer (1 .. 16) = "failed invariant" then
2461 Insert_Str_In_Name_Buffer ("inherited ", 8);
2462 Str := String_From_Name_Buffer;
2463 end if;
2464 end if;
2466 Append_To (Assoc,
2467 Make_Pragma_Argument_Association (Ploc,
2468 Expression => Make_String_Literal (Ploc, Str)));
2469 end if;
2471 -- Generate:
2472 -- pragma Check (<Nam>, <Expr>, <Str>);
2474 Append_New_To (Checks,
2475 Make_Pragma (Ploc,
2476 Chars => Name_Check,
2477 Pragma_Argument_Associations => Assoc));
2478 end if;
2480 -- Output an info message when inheriting an invariant and the
2481 -- listing option is enabled.
2483 if Inherited and Opt.List_Inherited_Aspects then
2484 Error_Msg_Sloc := Sloc (Prag);
2485 Error_Msg_N
2486 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2487 end if;
2489 -- Add the pragma to the list of processed pragmas
2491 Append_New_Elmt (Prag, Pragmas_Seen);
2492 Produced_Check := True;
2493 end Add_Invariant_Check;
2495 ---------------------------
2496 -- Add_Parent_Invariants --
2497 ---------------------------
2499 procedure Add_Parent_Invariants
2500 (T : Entity_Id;
2501 Obj_Id : Entity_Id;
2502 Checks : in out List_Id)
2504 Dummy_1 : Entity_Id;
2505 Dummy_2 : Entity_Id;
2507 Curr_Typ : Entity_Id;
2508 -- The entity of the current type being examined
2510 Full_Typ : Entity_Id;
2511 -- The full view of Par_Typ
2513 Par_Typ : Entity_Id;
2514 -- The entity of the parent type
2516 Priv_Typ : Entity_Id;
2517 -- The partial view of Par_Typ
2519 begin
2520 -- Do not process array types because they cannot have true parent
2521 -- types. This also prevents the generation of a duplicate invariant
2522 -- check when the input type is an array base type because its Etype
2523 -- denotes the first subtype, both of which share the same component
2524 -- type.
2526 if Is_Array_Type (T) then
2527 return;
2528 end if;
2530 -- Climb the parent type chain
2532 Curr_Typ := T;
2533 loop
2534 -- Do not consider subtypes as they inherit the invariants
2535 -- from their base types.
2537 Par_Typ := Base_Type (Etype (Curr_Typ));
2539 -- Stop the climb once the root of the parent chain is
2540 -- reached.
2542 exit when Curr_Typ = Par_Typ;
2544 -- Process the class-wide invariants of the parent type
2546 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2548 -- Process the elements of an array type
2550 if Is_Array_Type (Full_Typ) then
2551 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2553 -- Process the components of a record type
2555 elsif Ekind (Full_Typ) = E_Record_Type then
2556 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2557 end if;
2559 Add_Inherited_Invariants
2560 (T => T,
2561 Priv_Typ => Priv_Typ,
2562 Full_Typ => Full_Typ,
2563 Obj_Id => Obj_Id,
2564 Checks => Checks);
2566 Curr_Typ := Par_Typ;
2567 end loop;
2568 end Add_Parent_Invariants;
2570 ------------------------
2571 -- Add_Own_Invariants --
2572 ------------------------
2574 procedure Add_Own_Invariants
2575 (T : Entity_Id;
2576 Obj_Id : Entity_Id;
2577 Checks : in out List_Id;
2578 Priv_Item : Node_Id := Empty)
2580 ASIS_Expr : Node_Id;
2581 Expr : Node_Id;
2582 Prag : Node_Id;
2583 Prag_Asp : Node_Id;
2584 Prag_Expr : Node_Id;
2585 Prag_Expr_Arg : Node_Id;
2586 Prag_Typ : Node_Id;
2587 Prag_Typ_Arg : Node_Id;
2589 begin
2590 if not Present (T) then
2591 return;
2592 end if;
2594 Prag := First_Rep_Item (T);
2595 while Present (Prag) loop
2596 if Nkind (Prag) = N_Pragma
2597 and then Pragma_Name (Prag) = Name_Invariant
2598 then
2599 -- Stop the traversal of the rep item chain once a specific
2600 -- item is encountered.
2602 if Present (Priv_Item) and then Prag = Priv_Item then
2603 exit;
2604 end if;
2606 -- Nothing to do if the pragma was already processed
2608 if Contains (Pragmas_Seen, Prag) then
2609 return;
2610 end if;
2612 -- Extract the arguments of the invariant pragma
2614 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2615 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2616 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2617 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2618 Prag_Asp := Corresponding_Aspect (Prag);
2620 -- Verify the pragma belongs to T, otherwise the pragma applies
2621 -- to a parent type in which case it will be processed later by
2622 -- Add_Parent_Invariants or Add_Interface_Invariants.
2624 if Entity (Prag_Typ) /= T then
2625 return;
2626 end if;
2628 Expr := New_Copy_Tree (Prag_Expr);
2630 -- Substitute all references to type T with references to the
2631 -- _object formal parameter.
2633 Replace_Type_References (Expr, T, Obj_Id);
2635 -- Preanalyze the invariant expression to detect errors and at
2636 -- the same time capture the visibility of the proper package
2637 -- part.
2639 Set_Parent (Expr, Parent (Prag_Expr));
2640 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2642 -- Save a copy of the expression when T is tagged to detect
2643 -- errors and capture the visibility of the proper package part
2644 -- for the generation of inherited type invariants.
2646 if Is_Tagged_Type (T) then
2647 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2648 end if;
2650 -- If the pragma comes from an aspect specification, replace
2651 -- the saved expression because all type references must be
2652 -- substituted for the call to Preanalyze_Spec_Expression in
2653 -- Check_Aspect_At_xxx routines.
2655 if Present (Prag_Asp) then
2656 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2657 end if;
2659 -- Analyze the original invariant expression for ASIS
2661 if ASIS_Mode then
2662 ASIS_Expr := Empty;
2664 if Comes_From_Source (Prag) then
2665 ASIS_Expr := Prag_Expr;
2666 elsif Present (Prag_Asp) then
2667 ASIS_Expr := Expression (Prag_Asp);
2668 end if;
2670 if Present (ASIS_Expr) then
2671 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2672 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2673 end if;
2674 end if;
2676 Add_Invariant_Check (Prag, Expr, Checks);
2677 end if;
2679 Next_Rep_Item (Prag);
2680 end loop;
2681 end Add_Own_Invariants;
2683 -------------------------------------
2684 -- Add_Record_Component_Invariants --
2685 -------------------------------------
2687 procedure Add_Record_Component_Invariants
2688 (T : Entity_Id;
2689 Obj_Id : Entity_Id;
2690 Checks : in out List_Id)
2692 procedure Process_Component_List
2693 (Comp_List : Node_Id;
2694 CL_Checks : in out List_Id);
2695 -- Generate invariant checks for all record components found in
2696 -- component list Comp_List, including variant parts. All created
2697 -- checks are added to list CL_Checks.
2699 procedure Process_Record_Component
2700 (Comp_Id : Entity_Id;
2701 Comp_Checks : in out List_Id);
2702 -- Generate an invariant check for a record component identified by
2703 -- Comp_Id. All created checks are added to list Comp_Checks.
2705 ----------------------------
2706 -- Process_Component_List --
2707 ----------------------------
2709 procedure Process_Component_List
2710 (Comp_List : Node_Id;
2711 CL_Checks : in out List_Id)
2713 Comp : Node_Id;
2714 Var : Node_Id;
2715 Var_Alts : List_Id := No_List;
2716 Var_Checks : List_Id := No_List;
2717 Var_Stmts : List_Id;
2719 Produced_Variant_Check : Boolean := False;
2720 -- This flag tracks whether the component has produced at least
2721 -- one invariant check.
2723 begin
2724 -- Traverse the component items
2726 Comp := First (Component_Items (Comp_List));
2727 while Present (Comp) loop
2728 if Nkind (Comp) = N_Component_Declaration then
2730 -- Generate the component invariant check
2732 Process_Record_Component
2733 (Comp_Id => Defining_Entity (Comp),
2734 Comp_Checks => CL_Checks);
2735 end if;
2737 Next (Comp);
2738 end loop;
2740 -- Traverse the variant part
2742 if Present (Variant_Part (Comp_List)) then
2743 Var := First (Variants (Variant_Part (Comp_List)));
2744 while Present (Var) loop
2745 Var_Checks := No_List;
2747 -- Generate invariant checks for all components and variant
2748 -- parts that qualify.
2750 Process_Component_List
2751 (Comp_List => Component_List (Var),
2752 CL_Checks => Var_Checks);
2754 -- The components of the current variant produced at least
2755 -- one invariant check.
2757 if Present (Var_Checks) then
2758 Var_Stmts := Var_Checks;
2759 Produced_Variant_Check := True;
2761 -- Otherwise there are either no components with invariants,
2762 -- assertions are disabled, or Assertion_Policy Ignore is in
2763 -- effect.
2765 else
2766 Var_Stmts := New_List (Make_Null_Statement (Loc));
2767 end if;
2769 Append_New_To (Var_Alts,
2770 Make_Case_Statement_Alternative (Loc,
2771 Discrete_Choices =>
2772 New_Copy_List (Discrete_Choices (Var)),
2773 Statements => Var_Stmts));
2775 Next (Var);
2776 end loop;
2778 -- Create a case statement which verifies the invariant checks
2779 -- of a particular component list depending on the discriminant
2780 -- values only when there is at least one real invariant check.
2782 if Produced_Variant_Check then
2783 Append_New_To (CL_Checks,
2784 Make_Case_Statement (Loc,
2785 Expression =>
2786 Make_Selected_Component (Loc,
2787 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2788 Selector_Name =>
2789 New_Occurrence_Of
2790 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2791 Alternatives => Var_Alts));
2792 end if;
2793 end if;
2794 end Process_Component_List;
2796 ------------------------------
2797 -- Process_Record_Component --
2798 ------------------------------
2800 procedure Process_Record_Component
2801 (Comp_Id : Entity_Id;
2802 Comp_Checks : in out List_Id)
2804 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2805 Proc_Id : Entity_Id;
2807 Produced_Component_Check : Boolean := False;
2808 -- This flag tracks whether the component has produced at least
2809 -- one invariant check.
2811 begin
2812 -- Nothing to do for internal component _parent. Note that it is
2813 -- not desirable to check whether the component comes from source
2814 -- because protected type components are relocated to an internal
2815 -- corresponding record, but still need processing.
2817 if Chars (Comp_Id) = Name_uParent then
2818 return;
2819 end if;
2821 -- Verify the invariant of the component. Note that an access
2822 -- type may have an invariant when it acts as the full view of a
2823 -- private type and the invariant appears on the partial view. In
2824 -- this case verify the access value itself.
2826 if Has_Invariants (Comp_Typ) then
2828 -- In GNATprove mode, the component invariants are checked by
2829 -- other means. They should not be added to the record type
2830 -- invariant procedure, so that the procedure can be used to
2831 -- check the record type invariants if any.
2833 if GNATprove_Mode then
2834 null;
2836 else
2837 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2839 -- The component type should have an invariant procedure
2840 -- if it has invariants of its own or inherits class-wide
2841 -- invariants from parent or interface types.
2843 pragma Assert (Present (Proc_Id));
2845 -- Generate:
2846 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2848 -- Note that the invariant procedure may have a null body if
2849 -- assertions are disabled or Assertion_Policy Ignore is in
2850 -- effect.
2852 if not Has_Null_Body (Proc_Id) then
2853 Append_New_To (Comp_Checks,
2854 Make_Procedure_Call_Statement (Loc,
2855 Name =>
2856 New_Occurrence_Of (Proc_Id, Loc),
2857 Parameter_Associations => New_List (
2858 Make_Selected_Component (Loc,
2859 Prefix =>
2860 Unchecked_Convert_To
2861 (T, New_Occurrence_Of (Obj_Id, Loc)),
2862 Selector_Name =>
2863 New_Occurrence_Of (Comp_Id, Loc)))));
2864 end if;
2865 end if;
2867 Produced_Check := True;
2868 Produced_Component_Check := True;
2869 end if;
2871 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2872 Error_Msg_NE
2873 ("invariants cannot be checked on components of "
2874 & "unchecked_union type &?", Comp_Id, T);
2875 end if;
2876 end Process_Record_Component;
2878 -- Local variables
2880 Comps : Node_Id;
2881 Def : Node_Id;
2883 -- Start of processing for Add_Record_Component_Invariants
2885 begin
2886 -- An untagged derived type inherits the components of its parent
2887 -- type. In order to avoid creating redundant invariant checks, do
2888 -- not process the components now. Instead wait until the ultimate
2889 -- parent of the untagged derivation chain is reached.
2891 if not Is_Untagged_Derivation (T) then
2892 Def := Type_Definition (Parent (T));
2894 if Nkind (Def) = N_Derived_Type_Definition then
2895 Def := Record_Extension_Part (Def);
2896 end if;
2898 pragma Assert (Nkind (Def) = N_Record_Definition);
2899 Comps := Component_List (Def);
2901 if Present (Comps) then
2902 Process_Component_List
2903 (Comp_List => Comps,
2904 CL_Checks => Checks);
2905 end if;
2906 end if;
2907 end Add_Record_Component_Invariants;
2909 -- Local variables
2911 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2912 -- Save the Ghost mode to restore on exit
2914 Dummy : Entity_Id;
2915 Priv_Item : Node_Id;
2916 Proc_Body : Node_Id;
2917 Proc_Body_Id : Entity_Id;
2918 Proc_Decl : Node_Id;
2919 Proc_Id : Entity_Id;
2920 Stmts : List_Id := No_List;
2922 CRec_Typ : Entity_Id := Empty;
2923 -- The corresponding record type of Full_Typ
2925 Full_Proc : Entity_Id := Empty;
2926 -- The entity of the "full" invariant procedure
2928 Full_Typ : Entity_Id := Empty;
2929 -- The full view of the working type
2931 Obj_Id : Entity_Id := Empty;
2932 -- The _object formal parameter of the invariant procedure
2934 Part_Proc : Entity_Id := Empty;
2935 -- The entity of the "partial" invariant procedure
2937 Priv_Typ : Entity_Id := Empty;
2938 -- The partial view of the working type
2940 Work_Typ : Entity_Id := Empty;
2941 -- The working type
2943 -- Start of processing for Build_Invariant_Procedure_Body
2945 begin
2946 Work_Typ := Typ;
2948 -- The input type denotes the implementation base type of a constrained
2949 -- array type. Work with the first subtype as all invariant pragmas are
2950 -- on its rep item chain.
2952 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2953 Work_Typ := First_Subtype (Work_Typ);
2955 -- The input type denotes the corresponding record type of a protected
2956 -- or task type. Work with the concurrent type because the corresponding
2957 -- record type may not be visible to clients of the type.
2959 elsif Ekind (Work_Typ) = E_Record_Type
2960 and then Is_Concurrent_Record_Type (Work_Typ)
2961 then
2962 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2963 end if;
2965 -- The working type may be subject to pragma Ghost. Set the mode now to
2966 -- ensure that the invariant procedure is properly marked as Ghost.
2968 Set_Ghost_Mode (Work_Typ);
2970 -- The type must either have invariants of its own, inherit class-wide
2971 -- invariants from parent types or interfaces, or be an array or record
2972 -- type whose components have invariants.
2974 pragma Assert (Has_Invariants (Work_Typ));
2976 -- Interfaces are treated as the partial view of a private type in order
2977 -- to achieve uniformity with the general case.
2979 if Is_Interface (Work_Typ) then
2980 Priv_Typ := Work_Typ;
2982 -- Otherwise obtain both views of the type
2984 else
2985 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
2986 end if;
2988 -- The caller requests a body for the partial invariant procedure
2990 if Partial_Invariant then
2991 Full_Proc := Invariant_Procedure (Work_Typ);
2992 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
2994 -- The "full" invariant procedure body was already created
2996 if Present (Full_Proc)
2997 and then Present
2998 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
2999 then
3000 -- This scenario happens only when the type is an untagged
3001 -- derivation from a private parent and the underlying full
3002 -- view was processed before the partial view.
3004 pragma Assert
3005 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3007 -- Nothing to do because the processing of the underlying full
3008 -- view already checked the invariants of the partial view.
3010 goto Leave;
3011 end if;
3013 -- Create a declaration for the "partial" invariant procedure if it
3014 -- is not available.
3016 if No (Proc_Id) then
3017 Build_Invariant_Procedure_Declaration
3018 (Typ => Work_Typ,
3019 Partial_Invariant => True);
3021 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3022 end if;
3024 -- The caller requests a body for the "full" invariant procedure
3026 else
3027 Proc_Id := Invariant_Procedure (Work_Typ);
3028 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3030 -- Create a declaration for the "full" invariant procedure if it is
3031 -- not available.
3033 if No (Proc_Id) then
3034 Build_Invariant_Procedure_Declaration (Work_Typ);
3035 Proc_Id := Invariant_Procedure (Work_Typ);
3036 end if;
3037 end if;
3039 -- At this point there should be an invariant procedure declaration
3041 pragma Assert (Present (Proc_Id));
3042 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3044 -- Nothing to do if the invariant procedure already has a body
3046 if Present (Corresponding_Body (Proc_Decl)) then
3047 goto Leave;
3048 end if;
3050 -- Emulate the environment of the invariant procedure by installing its
3051 -- scope and formal parameters. Note that this is not needed, but having
3052 -- the scope installed helps with the detection of invariant-related
3053 -- errors.
3055 Push_Scope (Proc_Id);
3056 Install_Formals (Proc_Id);
3058 Obj_Id := First_Formal (Proc_Id);
3059 pragma Assert (Present (Obj_Id));
3061 -- The "partial" invariant procedure verifies the invariants of the
3062 -- partial view only.
3064 if Partial_Invariant then
3065 pragma Assert (Present (Priv_Typ));
3067 Add_Own_Invariants
3068 (T => Priv_Typ,
3069 Obj_Id => Obj_Id,
3070 Checks => Stmts);
3072 -- Otherwise the "full" invariant procedure verifies the invariants of
3073 -- the full view, all array or record components, as well as class-wide
3074 -- invariants inherited from parent types or interfaces. In addition, it
3075 -- indirectly verifies the invariants of the partial view by calling the
3076 -- "partial" invariant procedure.
3078 else
3079 pragma Assert (Present (Full_Typ));
3081 -- Check the invariants of the partial view by calling the "partial"
3082 -- invariant procedure. Generate:
3084 -- <Work_Typ>Partial_Invariant (_object);
3086 if Present (Part_Proc) then
3087 Append_New_To (Stmts,
3088 Make_Procedure_Call_Statement (Loc,
3089 Name => New_Occurrence_Of (Part_Proc, Loc),
3090 Parameter_Associations => New_List (
3091 New_Occurrence_Of (Obj_Id, Loc))));
3093 Produced_Check := True;
3094 end if;
3096 Priv_Item := Empty;
3098 -- Derived subtypes do not have a partial view
3100 if Present (Priv_Typ) then
3102 -- The processing of the "full" invariant procedure intentionally
3103 -- skips the partial view because a) this may result in changes of
3104 -- visibility and b) lead to duplicate checks. However, when the
3105 -- full view is the underlying full view of an untagged derived
3106 -- type whose parent type is private, partial invariants appear on
3107 -- the rep item chain of the partial view only.
3109 -- package Pack_1 is
3110 -- type Root ... is private;
3111 -- private
3112 -- <full view of Root>
3113 -- end Pack_1;
3115 -- with Pack_1;
3116 -- package Pack_2 is
3117 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3118 -- <underlying full view of Child>
3119 -- end Pack_2;
3121 -- As a result, the processing of the full view must also consider
3122 -- all invariants of the partial view.
3124 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3125 null;
3127 -- Otherwise the invariants of the partial view are ignored
3129 else
3130 -- Note that the rep item chain is shared between the partial
3131 -- and full views of a type. To avoid processing the invariants
3132 -- of the partial view, signal the logic to stop when the first
3133 -- rep item of the partial view has been reached.
3135 Priv_Item := First_Rep_Item (Priv_Typ);
3137 -- Ignore the invariants of the partial view by eliminating the
3138 -- view.
3140 Priv_Typ := Empty;
3141 end if;
3142 end if;
3144 -- Process the invariants of the full view and in certain cases those
3145 -- of the partial view. This also handles any invariants on array or
3146 -- record components.
3148 Add_Own_Invariants
3149 (T => Priv_Typ,
3150 Obj_Id => Obj_Id,
3151 Checks => Stmts,
3152 Priv_Item => Priv_Item);
3154 Add_Own_Invariants
3155 (T => Full_Typ,
3156 Obj_Id => Obj_Id,
3157 Checks => Stmts,
3158 Priv_Item => Priv_Item);
3160 -- Process the elements of an array type
3162 if Is_Array_Type (Full_Typ) then
3163 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3165 -- Process the components of a record type
3167 elsif Ekind (Full_Typ) = E_Record_Type then
3168 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3170 -- Process the components of a corresponding record
3172 elsif Present (CRec_Typ) then
3173 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3174 end if;
3176 -- Process the inherited class-wide invariants of all parent types.
3177 -- This also handles any invariants on record components.
3179 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3181 -- Process the inherited class-wide invariants of all implemented
3182 -- interface types.
3184 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3185 end if;
3187 End_Scope;
3189 -- At this point there should be at least one invariant check. If this
3190 -- is not the case, then the invariant-related flags were not properly
3191 -- set, or there is a missing invariant procedure on one of the array
3192 -- or record components.
3194 pragma Assert (Produced_Check);
3196 -- Account for the case where assertions are disabled or all invariant
3197 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3198 -- empty body.
3200 if No (Stmts) then
3201 Stmts := New_List (Make_Null_Statement (Loc));
3202 end if;
3204 -- Generate:
3205 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3206 -- begin
3207 -- <Stmts>
3208 -- end <Work_Typ>[Partial_]Invariant;
3210 Proc_Body :=
3211 Make_Subprogram_Body (Loc,
3212 Specification =>
3213 Copy_Subprogram_Spec (Parent (Proc_Id)),
3214 Declarations => Empty_List,
3215 Handled_Statement_Sequence =>
3216 Make_Handled_Sequence_Of_Statements (Loc,
3217 Statements => Stmts));
3218 Proc_Body_Id := Defining_Entity (Proc_Body);
3220 -- Perform minor decoration in case the body is not analyzed
3222 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3223 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3224 Set_Scope (Proc_Body_Id, Current_Scope);
3226 -- Link both spec and body to avoid generating duplicates
3228 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3229 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3231 -- The body should not be inserted into the tree when the context is
3232 -- ASIS or a generic unit because it is not part of the template. Note
3233 -- that the body must still be generated in order to resolve the
3234 -- invariants.
3236 if ASIS_Mode or Inside_A_Generic then
3237 null;
3239 -- Semi-insert the body into the tree for GNATprove by setting its
3240 -- Parent field. This allows for proper upstream tree traversals.
3242 elsif GNATprove_Mode then
3243 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3245 -- Otherwise the body is part of the freezing actions of the type
3247 else
3248 Append_Freeze_Action (Work_Typ, Proc_Body);
3249 end if;
3251 <<Leave>>
3252 Restore_Ghost_Mode (Saved_GM);
3253 end Build_Invariant_Procedure_Body;
3255 -------------------------------------------
3256 -- Build_Invariant_Procedure_Declaration --
3257 -------------------------------------------
3259 -- WARNING: This routine manages Ghost regions. Return statements must be
3260 -- replaced by gotos which jump to the end of the routine and restore the
3261 -- Ghost mode.
3263 procedure Build_Invariant_Procedure_Declaration
3264 (Typ : Entity_Id;
3265 Partial_Invariant : Boolean := False)
3267 Loc : constant Source_Ptr := Sloc (Typ);
3269 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3270 -- Save the Ghost mode to restore on exit
3272 Proc_Decl : Node_Id;
3273 Proc_Id : Entity_Id;
3274 Proc_Nam : Name_Id;
3275 Typ_Decl : Node_Id;
3277 CRec_Typ : Entity_Id;
3278 -- The corresponding record type of Full_Typ
3280 Full_Base : Entity_Id;
3281 -- The base type of Full_Typ
3283 Full_Typ : Entity_Id;
3284 -- The full view of working type
3286 Obj_Id : Entity_Id;
3287 -- The _object formal parameter of the invariant procedure
3289 Obj_Typ : Entity_Id;
3290 -- The type of the _object formal parameter
3292 Priv_Typ : Entity_Id;
3293 -- The partial view of working type
3295 Work_Typ : Entity_Id;
3296 -- The working type
3298 begin
3299 Work_Typ := Typ;
3301 -- The input type denotes the implementation base type of a constrained
3302 -- array type. Work with the first subtype as all invariant pragmas are
3303 -- on its rep item chain.
3305 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3306 Work_Typ := First_Subtype (Work_Typ);
3308 -- The input denotes the corresponding record type of a protected or a
3309 -- task type. Work with the concurrent type because the corresponding
3310 -- record type may not be visible to clients of the type.
3312 elsif Ekind (Work_Typ) = E_Record_Type
3313 and then Is_Concurrent_Record_Type (Work_Typ)
3314 then
3315 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3316 end if;
3318 -- The working type may be subject to pragma Ghost. Set the mode now to
3319 -- ensure that the invariant procedure is properly marked as Ghost.
3321 Set_Ghost_Mode (Work_Typ);
3323 -- The type must either have invariants of its own, inherit class-wide
3324 -- invariants from parent or interface types, or be an array or record
3325 -- type whose components have invariants.
3327 pragma Assert (Has_Invariants (Work_Typ));
3329 -- Nothing to do if the type already has a "partial" invariant procedure
3331 if Partial_Invariant then
3332 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3333 goto Leave;
3334 end if;
3336 -- Nothing to do if the type already has a "full" invariant procedure
3338 elsif Present (Invariant_Procedure (Work_Typ)) then
3339 goto Leave;
3340 end if;
3342 -- The caller requests the declaration of the "partial" invariant
3343 -- procedure.
3345 if Partial_Invariant then
3346 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3348 -- Otherwise the caller requests the declaration of the "full" invariant
3349 -- procedure.
3351 else
3352 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3353 end if;
3355 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3357 -- Perform minor decoration in case the declaration is not analyzed
3359 Set_Ekind (Proc_Id, E_Procedure);
3360 Set_Etype (Proc_Id, Standard_Void_Type);
3361 Set_Scope (Proc_Id, Current_Scope);
3363 if Partial_Invariant then
3364 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3365 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3366 else
3367 Set_Is_Invariant_Procedure (Proc_Id);
3368 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3369 end if;
3371 -- The invariant procedure requires debug info when the invariants are
3372 -- subject to Source Coverage Obligations.
3374 if Opt.Generate_SCO then
3375 Set_Needs_Debug_Info (Proc_Id);
3376 end if;
3378 -- Obtain all views of the input type
3380 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3382 -- Associate the invariant procedure with all views
3384 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3385 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3386 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3387 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3389 -- The declaration of the invariant procedure is inserted after the
3390 -- declaration of the partial view as this allows for proper external
3391 -- visibility.
3393 if Present (Priv_Typ) then
3394 Typ_Decl := Declaration_Node (Priv_Typ);
3396 -- Derived types with the full view as parent do not have a partial
3397 -- view. Insert the invariant procedure after the derived type.
3399 else
3400 Typ_Decl := Declaration_Node (Full_Typ);
3401 end if;
3403 -- The type should have a declarative node
3405 pragma Assert (Present (Typ_Decl));
3407 -- Create the formal parameter which emulates the variable-like behavior
3408 -- of the current type instance.
3410 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3412 -- When generating an invariant procedure declaration for an abstract
3413 -- type (including interfaces), use the class-wide type as the _object
3414 -- type. This has several desirable effects:
3416 -- * The invariant procedure does not become a primitive of the type.
3417 -- This eliminates the need to either special case the treatment of
3418 -- invariant procedures, or to make it a predefined primitive and
3419 -- force every derived type to potentially provide an empty body.
3421 -- * The invariant procedure does not need to be declared as abstract.
3422 -- This allows for a proper body, which in turn avoids redundant
3423 -- processing of the same invariants for types with multiple views.
3425 -- * The class-wide type allows for calls to abstract primitives
3426 -- within a nonabstract subprogram. The calls are treated as
3427 -- dispatching and require additional processing when they are
3428 -- remapped to call primitives of derived types. See routine
3429 -- Replace_References for details.
3431 if Is_Abstract_Type (Work_Typ) then
3432 Obj_Typ := Class_Wide_Type (Work_Typ);
3433 else
3434 Obj_Typ := Work_Typ;
3435 end if;
3437 -- Perform minor decoration in case the declaration is not analyzed
3439 Set_Ekind (Obj_Id, E_In_Parameter);
3440 Set_Etype (Obj_Id, Obj_Typ);
3441 Set_Scope (Obj_Id, Proc_Id);
3443 Set_First_Entity (Proc_Id, Obj_Id);
3445 -- Generate:
3446 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3448 Proc_Decl :=
3449 Make_Subprogram_Declaration (Loc,
3450 Specification =>
3451 Make_Procedure_Specification (Loc,
3452 Defining_Unit_Name => Proc_Id,
3453 Parameter_Specifications => New_List (
3454 Make_Parameter_Specification (Loc,
3455 Defining_Identifier => Obj_Id,
3456 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3458 -- The declaration should not be inserted into the tree when the context
3459 -- is ASIS or a generic unit because it is not part of the template.
3461 if ASIS_Mode or Inside_A_Generic then
3462 null;
3464 -- Semi-insert the declaration into the tree for GNATprove by setting
3465 -- its Parent field. This allows for proper upstream tree traversals.
3467 elsif GNATprove_Mode then
3468 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3470 -- Otherwise insert the declaration
3472 else
3473 pragma Assert (Present (Typ_Decl));
3474 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3475 end if;
3477 <<Leave>>
3478 Restore_Ghost_Mode (Saved_GM);
3479 end Build_Invariant_Procedure_Declaration;
3481 --------------------------
3482 -- Build_Procedure_Form --
3483 --------------------------
3485 procedure Build_Procedure_Form (N : Node_Id) is
3486 Loc : constant Source_Ptr := Sloc (N);
3487 Subp : constant Entity_Id := Defining_Entity (N);
3489 Func_Formal : Entity_Id;
3490 Proc_Formals : List_Id;
3491 Proc_Decl : Node_Id;
3493 begin
3494 -- No action needed if this transformation was already done, or in case
3495 -- of subprogram renaming declarations.
3497 if Nkind (Specification (N)) = N_Procedure_Specification
3498 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3499 then
3500 return;
3501 end if;
3503 -- Ditto when dealing with an expression function, where both the
3504 -- original expression and the generated declaration end up being
3505 -- expanded here.
3507 if Rewritten_For_C (Subp) then
3508 return;
3509 end if;
3511 Proc_Formals := New_List;
3513 -- Create a list of formal parameters with the same types as the
3514 -- function.
3516 Func_Formal := First_Formal (Subp);
3517 while Present (Func_Formal) loop
3518 Append_To (Proc_Formals,
3519 Make_Parameter_Specification (Loc,
3520 Defining_Identifier =>
3521 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3522 Parameter_Type =>
3523 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3525 Next_Formal (Func_Formal);
3526 end loop;
3528 -- Add an extra out parameter to carry the function result
3530 Name_Len := 6;
3531 Name_Buffer (1 .. Name_Len) := "RESULT";
3532 Append_To (Proc_Formals,
3533 Make_Parameter_Specification (Loc,
3534 Defining_Identifier =>
3535 Make_Defining_Identifier (Loc, Chars => Name_Find),
3536 Out_Present => True,
3537 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3539 -- The new procedure declaration is inserted immediately after the
3540 -- function declaration. The processing in Build_Procedure_Body_Form
3541 -- relies on this order.
3543 Proc_Decl :=
3544 Make_Subprogram_Declaration (Loc,
3545 Specification =>
3546 Make_Procedure_Specification (Loc,
3547 Defining_Unit_Name =>
3548 Make_Defining_Identifier (Loc, Chars (Subp)),
3549 Parameter_Specifications => Proc_Formals));
3551 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3553 -- Entity of procedure must remain invisible so that it does not
3554 -- overload subsequent references to the original function.
3556 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3558 -- Mark the function as having a procedure form and link the function
3559 -- and its internally built procedure.
3561 Set_Rewritten_For_C (Subp);
3562 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3563 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3564 end Build_Procedure_Form;
3566 ------------------------
3567 -- Build_Runtime_Call --
3568 ------------------------
3570 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3571 begin
3572 -- If entity is not available, we can skip making the call (this avoids
3573 -- junk duplicated error messages in a number of cases).
3575 if not RTE_Available (RE) then
3576 return Make_Null_Statement (Loc);
3577 else
3578 return
3579 Make_Procedure_Call_Statement (Loc,
3580 Name => New_Occurrence_Of (RTE (RE), Loc));
3581 end if;
3582 end Build_Runtime_Call;
3584 ------------------------
3585 -- Build_SS_Mark_Call --
3586 ------------------------
3588 function Build_SS_Mark_Call
3589 (Loc : Source_Ptr;
3590 Mark : Entity_Id) return Node_Id
3592 begin
3593 -- Generate:
3594 -- Mark : constant Mark_Id := SS_Mark;
3596 return
3597 Make_Object_Declaration (Loc,
3598 Defining_Identifier => Mark,
3599 Constant_Present => True,
3600 Object_Definition =>
3601 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3602 Expression =>
3603 Make_Function_Call (Loc,
3604 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3605 end Build_SS_Mark_Call;
3607 ---------------------------
3608 -- Build_SS_Release_Call --
3609 ---------------------------
3611 function Build_SS_Release_Call
3612 (Loc : Source_Ptr;
3613 Mark : Entity_Id) return Node_Id
3615 begin
3616 -- Generate:
3617 -- SS_Release (Mark);
3619 return
3620 Make_Procedure_Call_Statement (Loc,
3621 Name =>
3622 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3623 Parameter_Associations => New_List (
3624 New_Occurrence_Of (Mark, Loc)));
3625 end Build_SS_Release_Call;
3627 ----------------------------
3628 -- Build_Task_Array_Image --
3629 ----------------------------
3631 -- This function generates the body for a function that constructs the
3632 -- image string for a task that is an array component. The function is
3633 -- local to the init proc for the array type, and is called for each one
3634 -- of the components. The constructed image has the form of an indexed
3635 -- component, whose prefix is the outer variable of the array type.
3636 -- The n-dimensional array type has known indexes Index, Index2...
3638 -- Id_Ref is an indexed component form created by the enclosing init proc.
3639 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3640 -- in the loops that call the individual task init proc on each component.
3642 -- The generated function has the following structure:
3644 -- function F return String is
3645 -- Pref : string renames Task_Name;
3646 -- T1 : String := Index1'Image (Val1);
3647 -- ...
3648 -- Tn : String := indexn'image (Valn);
3649 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3650 -- -- Len includes commas and the end parentheses.
3651 -- Res : String (1..Len);
3652 -- Pos : Integer := Pref'Length;
3654 -- begin
3655 -- Res (1 .. Pos) := Pref;
3656 -- Pos := Pos + 1;
3657 -- Res (Pos) := '(';
3658 -- Pos := Pos + 1;
3659 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3660 -- Pos := Pos + T1'Length;
3661 -- Res (Pos) := '.';
3662 -- Pos := Pos + 1;
3663 -- ...
3664 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3665 -- Res (Len) := ')';
3667 -- return Res;
3668 -- end F;
3670 -- Needless to say, multidimensional arrays of tasks are rare enough that
3671 -- the bulkiness of this code is not really a concern.
3673 function Build_Task_Array_Image
3674 (Loc : Source_Ptr;
3675 Id_Ref : Node_Id;
3676 A_Type : Entity_Id;
3677 Dyn : Boolean := False) return Node_Id
3679 Dims : constant Nat := Number_Dimensions (A_Type);
3680 -- Number of dimensions for array of tasks
3682 Temps : array (1 .. Dims) of Entity_Id;
3683 -- Array of temporaries to hold string for each index
3685 Indx : Node_Id;
3686 -- Index expression
3688 Len : Entity_Id;
3689 -- Total length of generated name
3691 Pos : Entity_Id;
3692 -- Running index for substring assignments
3694 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3695 -- Name of enclosing variable, prefix of resulting name
3697 Res : Entity_Id;
3698 -- String to hold result
3700 Val : Node_Id;
3701 -- Value of successive indexes
3703 Sum : Node_Id;
3704 -- Expression to compute total size of string
3706 T : Entity_Id;
3707 -- Entity for name at one index position
3709 Decls : constant List_Id := New_List;
3710 Stats : constant List_Id := New_List;
3712 begin
3713 -- For a dynamic task, the name comes from the target variable. For a
3714 -- static one it is a formal of the enclosing init proc.
3716 if Dyn then
3717 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3718 Append_To (Decls,
3719 Make_Object_Declaration (Loc,
3720 Defining_Identifier => Pref,
3721 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3722 Expression =>
3723 Make_String_Literal (Loc,
3724 Strval => String_From_Name_Buffer)));
3726 else
3727 Append_To (Decls,
3728 Make_Object_Renaming_Declaration (Loc,
3729 Defining_Identifier => Pref,
3730 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3731 Name => Make_Identifier (Loc, Name_uTask_Name)));
3732 end if;
3734 Indx := First_Index (A_Type);
3735 Val := First (Expressions (Id_Ref));
3737 for J in 1 .. Dims loop
3738 T := Make_Temporary (Loc, 'T');
3739 Temps (J) := T;
3741 Append_To (Decls,
3742 Make_Object_Declaration (Loc,
3743 Defining_Identifier => T,
3744 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3745 Expression =>
3746 Make_Attribute_Reference (Loc,
3747 Attribute_Name => Name_Image,
3748 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3749 Expressions => New_List (New_Copy_Tree (Val)))));
3751 Next_Index (Indx);
3752 Next (Val);
3753 end loop;
3755 Sum := Make_Integer_Literal (Loc, Dims + 1);
3757 Sum :=
3758 Make_Op_Add (Loc,
3759 Left_Opnd => Sum,
3760 Right_Opnd =>
3761 Make_Attribute_Reference (Loc,
3762 Attribute_Name => Name_Length,
3763 Prefix => New_Occurrence_Of (Pref, Loc),
3764 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3766 for J in 1 .. Dims loop
3767 Sum :=
3768 Make_Op_Add (Loc,
3769 Left_Opnd => Sum,
3770 Right_Opnd =>
3771 Make_Attribute_Reference (Loc,
3772 Attribute_Name => Name_Length,
3773 Prefix =>
3774 New_Occurrence_Of (Temps (J), Loc),
3775 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3776 end loop;
3778 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3780 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3782 Append_To (Stats,
3783 Make_Assignment_Statement (Loc,
3784 Name =>
3785 Make_Indexed_Component (Loc,
3786 Prefix => New_Occurrence_Of (Res, Loc),
3787 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3788 Expression =>
3789 Make_Character_Literal (Loc,
3790 Chars => Name_Find,
3791 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3793 Append_To (Stats,
3794 Make_Assignment_Statement (Loc,
3795 Name => New_Occurrence_Of (Pos, Loc),
3796 Expression =>
3797 Make_Op_Add (Loc,
3798 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3799 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3801 for J in 1 .. Dims loop
3803 Append_To (Stats,
3804 Make_Assignment_Statement (Loc,
3805 Name =>
3806 Make_Slice (Loc,
3807 Prefix => New_Occurrence_Of (Res, Loc),
3808 Discrete_Range =>
3809 Make_Range (Loc,
3810 Low_Bound => New_Occurrence_Of (Pos, Loc),
3811 High_Bound =>
3812 Make_Op_Subtract (Loc,
3813 Left_Opnd =>
3814 Make_Op_Add (Loc,
3815 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3816 Right_Opnd =>
3817 Make_Attribute_Reference (Loc,
3818 Attribute_Name => Name_Length,
3819 Prefix =>
3820 New_Occurrence_Of (Temps (J), Loc),
3821 Expressions =>
3822 New_List (Make_Integer_Literal (Loc, 1)))),
3823 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3825 Expression => New_Occurrence_Of (Temps (J), Loc)));
3827 if J < Dims then
3828 Append_To (Stats,
3829 Make_Assignment_Statement (Loc,
3830 Name => New_Occurrence_Of (Pos, Loc),
3831 Expression =>
3832 Make_Op_Add (Loc,
3833 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3834 Right_Opnd =>
3835 Make_Attribute_Reference (Loc,
3836 Attribute_Name => Name_Length,
3837 Prefix => New_Occurrence_Of (Temps (J), Loc),
3838 Expressions =>
3839 New_List (Make_Integer_Literal (Loc, 1))))));
3841 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3843 Append_To (Stats,
3844 Make_Assignment_Statement (Loc,
3845 Name => Make_Indexed_Component (Loc,
3846 Prefix => New_Occurrence_Of (Res, Loc),
3847 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3848 Expression =>
3849 Make_Character_Literal (Loc,
3850 Chars => Name_Find,
3851 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3853 Append_To (Stats,
3854 Make_Assignment_Statement (Loc,
3855 Name => New_Occurrence_Of (Pos, Loc),
3856 Expression =>
3857 Make_Op_Add (Loc,
3858 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3859 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3860 end if;
3861 end loop;
3863 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3865 Append_To (Stats,
3866 Make_Assignment_Statement (Loc,
3867 Name =>
3868 Make_Indexed_Component (Loc,
3869 Prefix => New_Occurrence_Of (Res, Loc),
3870 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3871 Expression =>
3872 Make_Character_Literal (Loc,
3873 Chars => Name_Find,
3874 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3875 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3876 end Build_Task_Array_Image;
3878 ----------------------------
3879 -- Build_Task_Image_Decls --
3880 ----------------------------
3882 function Build_Task_Image_Decls
3883 (Loc : Source_Ptr;
3884 Id_Ref : Node_Id;
3885 A_Type : Entity_Id;
3886 In_Init_Proc : Boolean := False) return List_Id
3888 Decls : constant List_Id := New_List;
3889 T_Id : Entity_Id := Empty;
3890 Decl : Node_Id;
3891 Expr : Node_Id := Empty;
3892 Fun : Node_Id := Empty;
3893 Is_Dyn : constant Boolean :=
3894 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3895 and then
3896 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3898 begin
3899 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3900 -- generate a dummy declaration only.
3902 if Restriction_Active (No_Implicit_Heap_Allocations)
3903 or else Global_Discard_Names
3904 then
3905 T_Id := Make_Temporary (Loc, 'J');
3906 Name_Len := 0;
3908 return
3909 New_List (
3910 Make_Object_Declaration (Loc,
3911 Defining_Identifier => T_Id,
3912 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3913 Expression =>
3914 Make_String_Literal (Loc,
3915 Strval => String_From_Name_Buffer)));
3917 else
3918 if Nkind (Id_Ref) = N_Identifier
3919 or else Nkind (Id_Ref) = N_Defining_Identifier
3920 then
3921 -- For a simple variable, the image of the task is built from
3922 -- the name of the variable. To avoid possible conflict with the
3923 -- anonymous type created for a single protected object, add a
3924 -- numeric suffix.
3926 T_Id :=
3927 Make_Defining_Identifier (Loc,
3928 New_External_Name (Chars (Id_Ref), 'T', 1));
3930 Get_Name_String (Chars (Id_Ref));
3932 Expr :=
3933 Make_String_Literal (Loc,
3934 Strval => String_From_Name_Buffer);
3936 elsif Nkind (Id_Ref) = N_Selected_Component then
3937 T_Id :=
3938 Make_Defining_Identifier (Loc,
3939 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3940 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3942 elsif Nkind (Id_Ref) = N_Indexed_Component then
3943 T_Id :=
3944 Make_Defining_Identifier (Loc,
3945 New_External_Name (Chars (A_Type), 'N'));
3947 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3948 end if;
3949 end if;
3951 if Present (Fun) then
3952 Append (Fun, Decls);
3953 Expr := Make_Function_Call (Loc,
3954 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
3956 if not In_Init_Proc then
3957 Set_Uses_Sec_Stack (Defining_Entity (Fun));
3958 end if;
3959 end if;
3961 Decl := Make_Object_Declaration (Loc,
3962 Defining_Identifier => T_Id,
3963 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3964 Constant_Present => True,
3965 Expression => Expr);
3967 Append (Decl, Decls);
3968 return Decls;
3969 end Build_Task_Image_Decls;
3971 -------------------------------
3972 -- Build_Task_Image_Function --
3973 -------------------------------
3975 function Build_Task_Image_Function
3976 (Loc : Source_Ptr;
3977 Decls : List_Id;
3978 Stats : List_Id;
3979 Res : Entity_Id) return Node_Id
3981 Spec : Node_Id;
3983 begin
3984 Append_To (Stats,
3985 Make_Simple_Return_Statement (Loc,
3986 Expression => New_Occurrence_Of (Res, Loc)));
3988 Spec := Make_Function_Specification (Loc,
3989 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
3990 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
3992 -- Calls to 'Image use the secondary stack, which must be cleaned up
3993 -- after the task name is built.
3995 return Make_Subprogram_Body (Loc,
3996 Specification => Spec,
3997 Declarations => Decls,
3998 Handled_Statement_Sequence =>
3999 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4000 end Build_Task_Image_Function;
4002 -----------------------------
4003 -- Build_Task_Image_Prefix --
4004 -----------------------------
4006 procedure Build_Task_Image_Prefix
4007 (Loc : Source_Ptr;
4008 Len : out Entity_Id;
4009 Res : out Entity_Id;
4010 Pos : out Entity_Id;
4011 Prefix : Entity_Id;
4012 Sum : Node_Id;
4013 Decls : List_Id;
4014 Stats : List_Id)
4016 begin
4017 Len := Make_Temporary (Loc, 'L', Sum);
4019 Append_To (Decls,
4020 Make_Object_Declaration (Loc,
4021 Defining_Identifier => Len,
4022 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4023 Expression => Sum));
4025 Res := Make_Temporary (Loc, 'R');
4027 Append_To (Decls,
4028 Make_Object_Declaration (Loc,
4029 Defining_Identifier => Res,
4030 Object_Definition =>
4031 Make_Subtype_Indication (Loc,
4032 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4033 Constraint =>
4034 Make_Index_Or_Discriminant_Constraint (Loc,
4035 Constraints =>
4036 New_List (
4037 Make_Range (Loc,
4038 Low_Bound => Make_Integer_Literal (Loc, 1),
4039 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4041 -- Indicate that the result is an internal temporary, so it does not
4042 -- receive a bogus initialization when declaration is expanded. This
4043 -- is both efficient, and prevents anomalies in the handling of
4044 -- dynamic objects on the secondary stack.
4046 Set_Is_Internal (Res);
4047 Pos := Make_Temporary (Loc, 'P');
4049 Append_To (Decls,
4050 Make_Object_Declaration (Loc,
4051 Defining_Identifier => Pos,
4052 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4054 -- Pos := Prefix'Length;
4056 Append_To (Stats,
4057 Make_Assignment_Statement (Loc,
4058 Name => New_Occurrence_Of (Pos, Loc),
4059 Expression =>
4060 Make_Attribute_Reference (Loc,
4061 Attribute_Name => Name_Length,
4062 Prefix => New_Occurrence_Of (Prefix, Loc),
4063 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4065 -- Res (1 .. Pos) := Prefix;
4067 Append_To (Stats,
4068 Make_Assignment_Statement (Loc,
4069 Name =>
4070 Make_Slice (Loc,
4071 Prefix => New_Occurrence_Of (Res, Loc),
4072 Discrete_Range =>
4073 Make_Range (Loc,
4074 Low_Bound => Make_Integer_Literal (Loc, 1),
4075 High_Bound => New_Occurrence_Of (Pos, Loc))),
4077 Expression => New_Occurrence_Of (Prefix, Loc)));
4079 Append_To (Stats,
4080 Make_Assignment_Statement (Loc,
4081 Name => New_Occurrence_Of (Pos, Loc),
4082 Expression =>
4083 Make_Op_Add (Loc,
4084 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4085 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4086 end Build_Task_Image_Prefix;
4088 -----------------------------
4089 -- Build_Task_Record_Image --
4090 -----------------------------
4092 function Build_Task_Record_Image
4093 (Loc : Source_Ptr;
4094 Id_Ref : Node_Id;
4095 Dyn : Boolean := False) return Node_Id
4097 Len : Entity_Id;
4098 -- Total length of generated name
4100 Pos : Entity_Id;
4101 -- Index into result
4103 Res : Entity_Id;
4104 -- String to hold result
4106 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4107 -- Name of enclosing variable, prefix of resulting name
4109 Sum : Node_Id;
4110 -- Expression to compute total size of string
4112 Sel : Entity_Id;
4113 -- Entity for selector name
4115 Decls : constant List_Id := New_List;
4116 Stats : constant List_Id := New_List;
4118 begin
4119 -- For a dynamic task, the name comes from the target variable. For a
4120 -- static one it is a formal of the enclosing init proc.
4122 if Dyn then
4123 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4124 Append_To (Decls,
4125 Make_Object_Declaration (Loc,
4126 Defining_Identifier => Pref,
4127 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4128 Expression =>
4129 Make_String_Literal (Loc,
4130 Strval => String_From_Name_Buffer)));
4132 else
4133 Append_To (Decls,
4134 Make_Object_Renaming_Declaration (Loc,
4135 Defining_Identifier => Pref,
4136 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4137 Name => Make_Identifier (Loc, Name_uTask_Name)));
4138 end if;
4140 Sel := Make_Temporary (Loc, 'S');
4142 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4144 Append_To (Decls,
4145 Make_Object_Declaration (Loc,
4146 Defining_Identifier => Sel,
4147 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4148 Expression =>
4149 Make_String_Literal (Loc,
4150 Strval => String_From_Name_Buffer)));
4152 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4154 Sum :=
4155 Make_Op_Add (Loc,
4156 Left_Opnd => Sum,
4157 Right_Opnd =>
4158 Make_Attribute_Reference (Loc,
4159 Attribute_Name => Name_Length,
4160 Prefix =>
4161 New_Occurrence_Of (Pref, Loc),
4162 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4164 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4166 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4168 -- Res (Pos) := '.';
4170 Append_To (Stats,
4171 Make_Assignment_Statement (Loc,
4172 Name => Make_Indexed_Component (Loc,
4173 Prefix => New_Occurrence_Of (Res, Loc),
4174 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4175 Expression =>
4176 Make_Character_Literal (Loc,
4177 Chars => Name_Find,
4178 Char_Literal_Value =>
4179 UI_From_Int (Character'Pos ('.')))));
4181 Append_To (Stats,
4182 Make_Assignment_Statement (Loc,
4183 Name => New_Occurrence_Of (Pos, Loc),
4184 Expression =>
4185 Make_Op_Add (Loc,
4186 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4187 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4189 -- Res (Pos .. Len) := Selector;
4191 Append_To (Stats,
4192 Make_Assignment_Statement (Loc,
4193 Name => Make_Slice (Loc,
4194 Prefix => New_Occurrence_Of (Res, Loc),
4195 Discrete_Range =>
4196 Make_Range (Loc,
4197 Low_Bound => New_Occurrence_Of (Pos, Loc),
4198 High_Bound => New_Occurrence_Of (Len, Loc))),
4199 Expression => New_Occurrence_Of (Sel, Loc)));
4201 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4202 end Build_Task_Record_Image;
4204 ---------------------------------------
4205 -- Build_Transient_Object_Statements --
4206 ---------------------------------------
4208 procedure Build_Transient_Object_Statements
4209 (Obj_Decl : Node_Id;
4210 Fin_Call : out Node_Id;
4211 Hook_Assign : out Node_Id;
4212 Hook_Clear : out Node_Id;
4213 Hook_Decl : out Node_Id;
4214 Ptr_Decl : out Node_Id;
4215 Finalize_Obj : Boolean := True)
4217 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4218 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4219 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4221 Desig_Typ : Entity_Id;
4222 Hook_Expr : Node_Id;
4223 Hook_Id : Entity_Id;
4224 Obj_Ref : Node_Id;
4225 Ptr_Typ : Entity_Id;
4227 begin
4228 -- Recover the type of the object
4230 Desig_Typ := Obj_Typ;
4232 if Is_Access_Type (Desig_Typ) then
4233 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4234 end if;
4236 -- Create an access type which provides a reference to the transient
4237 -- object. Generate:
4239 -- type Ptr_Typ is access all Desig_Typ;
4241 Ptr_Typ := Make_Temporary (Loc, 'A');
4242 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4243 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4245 Ptr_Decl :=
4246 Make_Full_Type_Declaration (Loc,
4247 Defining_Identifier => Ptr_Typ,
4248 Type_Definition =>
4249 Make_Access_To_Object_Definition (Loc,
4250 All_Present => True,
4251 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4253 -- Create a temporary check which acts as a hook to the transient
4254 -- object. Generate:
4256 -- Hook : Ptr_Typ := null;
4258 Hook_Id := Make_Temporary (Loc, 'T');
4259 Set_Ekind (Hook_Id, E_Variable);
4260 Set_Etype (Hook_Id, Ptr_Typ);
4262 Hook_Decl :=
4263 Make_Object_Declaration (Loc,
4264 Defining_Identifier => Hook_Id,
4265 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4266 Expression => Make_Null (Loc));
4268 -- Mark the temporary as a hook. This signals the machinery in
4269 -- Build_Finalizer to recognize this special case.
4271 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4273 -- Hook the transient object to the temporary. Generate:
4275 -- Hook := Ptr_Typ (Obj_Id);
4276 -- <or>
4277 -- Hool := Obj_Id'Unrestricted_Access;
4279 if Is_Access_Type (Obj_Typ) then
4280 Hook_Expr :=
4281 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4282 else
4283 Hook_Expr :=
4284 Make_Attribute_Reference (Loc,
4285 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4286 Attribute_Name => Name_Unrestricted_Access);
4287 end if;
4289 Hook_Assign :=
4290 Make_Assignment_Statement (Loc,
4291 Name => New_Occurrence_Of (Hook_Id, Loc),
4292 Expression => Hook_Expr);
4294 -- Crear the hook prior to finalizing the object. Generate:
4296 -- Hook := null;
4298 Hook_Clear :=
4299 Make_Assignment_Statement (Loc,
4300 Name => New_Occurrence_Of (Hook_Id, Loc),
4301 Expression => Make_Null (Loc));
4303 -- Finalize the object. Generate:
4305 -- [Deep_]Finalize (Obj_Ref[.all]);
4307 if Finalize_Obj then
4308 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4310 if Is_Access_Type (Obj_Typ) then
4311 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4312 Set_Etype (Obj_Ref, Desig_Typ);
4313 end if;
4315 Fin_Call :=
4316 Make_Final_Call
4317 (Obj_Ref => Obj_Ref,
4318 Typ => Desig_Typ);
4320 -- Otherwise finalize the hook. Generate:
4322 -- [Deep_]Finalize (Hook.all);
4324 else
4325 Fin_Call :=
4326 Make_Final_Call (
4327 Obj_Ref =>
4328 Make_Explicit_Dereference (Loc,
4329 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4330 Typ => Desig_Typ);
4331 end if;
4332 end Build_Transient_Object_Statements;
4334 -----------------------------
4335 -- Check_Float_Op_Overflow --
4336 -----------------------------
4338 procedure Check_Float_Op_Overflow (N : Node_Id) is
4339 begin
4340 -- Return if no check needed
4342 if not Is_Floating_Point_Type (Etype (N))
4343 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4345 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4346 -- and do not expand the code for float overflow checking.
4348 or else CodePeer_Mode
4349 then
4350 return;
4351 end if;
4353 -- Otherwise we replace the expression by
4355 -- do Tnn : constant ftype := expression;
4356 -- constraint_error when not Tnn'Valid;
4357 -- in Tnn;
4359 declare
4360 Loc : constant Source_Ptr := Sloc (N);
4361 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4362 Typ : constant Entity_Id := Etype (N);
4364 begin
4365 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4366 -- right here. We also set the node as analyzed to prevent infinite
4367 -- recursion from repeating the operation in the expansion.
4369 Set_Do_Overflow_Check (N, False);
4370 Set_Analyzed (N, True);
4372 -- Do the rewrite to include the check
4374 Rewrite (N,
4375 Make_Expression_With_Actions (Loc,
4376 Actions => New_List (
4377 Make_Object_Declaration (Loc,
4378 Defining_Identifier => Tnn,
4379 Object_Definition => New_Occurrence_Of (Typ, Loc),
4380 Constant_Present => True,
4381 Expression => Relocate_Node (N)),
4382 Make_Raise_Constraint_Error (Loc,
4383 Condition =>
4384 Make_Op_Not (Loc,
4385 Right_Opnd =>
4386 Make_Attribute_Reference (Loc,
4387 Prefix => New_Occurrence_Of (Tnn, Loc),
4388 Attribute_Name => Name_Valid)),
4389 Reason => CE_Overflow_Check_Failed)),
4390 Expression => New_Occurrence_Of (Tnn, Loc)));
4392 Analyze_And_Resolve (N, Typ);
4393 end;
4394 end Check_Float_Op_Overflow;
4396 ----------------------------------
4397 -- Component_May_Be_Bit_Aligned --
4398 ----------------------------------
4400 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4401 UT : Entity_Id;
4403 begin
4404 -- If no component clause, then everything is fine, since the back end
4405 -- never bit-misaligns by default, even if there is a pragma Packed for
4406 -- the record.
4408 if No (Comp) or else No (Component_Clause (Comp)) then
4409 return False;
4410 end if;
4412 UT := Underlying_Type (Etype (Comp));
4414 -- It is only array and record types that cause trouble
4416 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4417 return False;
4419 -- If we know that we have a small (64 bits or less) record or small
4420 -- bit-packed array, then everything is fine, since the back end can
4421 -- handle these cases correctly.
4423 elsif Esize (Comp) <= 64
4424 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4425 then
4426 return False;
4428 -- Otherwise if the component is not byte aligned, we know we have the
4429 -- nasty unaligned case.
4431 elsif Normalized_First_Bit (Comp) /= Uint_0
4432 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4433 then
4434 return True;
4436 -- If we are large and byte aligned, then OK at this level
4438 else
4439 return False;
4440 end if;
4441 end Component_May_Be_Bit_Aligned;
4443 ----------------------------------------
4444 -- Containing_Package_With_Ext_Axioms --
4445 ----------------------------------------
4447 function Containing_Package_With_Ext_Axioms
4448 (E : Entity_Id) return Entity_Id
4450 begin
4451 -- E is the package or generic package which is externally axiomatized
4453 if Ekind_In (E, E_Generic_Package, E_Package)
4454 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4455 then
4456 return E;
4457 end if;
4459 -- If E's scope is axiomatized, E is axiomatized
4461 if Present (Scope (E)) then
4462 declare
4463 First_Ax_Parent_Scope : constant Entity_Id :=
4464 Containing_Package_With_Ext_Axioms (Scope (E));
4465 begin
4466 if Present (First_Ax_Parent_Scope) then
4467 return First_Ax_Parent_Scope;
4468 end if;
4469 end;
4470 end if;
4472 -- Otherwise, if E is a package instance, it is axiomatized if the
4473 -- corresponding generic package is axiomatized.
4475 if Ekind (E) = E_Package then
4476 declare
4477 Par : constant Node_Id := Parent (E);
4478 Decl : Node_Id;
4480 begin
4481 if Nkind (Par) = N_Defining_Program_Unit_Name then
4482 Decl := Parent (Par);
4483 else
4484 Decl := Par;
4485 end if;
4487 if Present (Generic_Parent (Decl)) then
4488 return
4489 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4490 end if;
4491 end;
4492 end if;
4494 return Empty;
4495 end Containing_Package_With_Ext_Axioms;
4497 -------------------------------
4498 -- Convert_To_Actual_Subtype --
4499 -------------------------------
4501 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4502 Act_ST : Entity_Id;
4504 begin
4505 Act_ST := Get_Actual_Subtype (Exp);
4507 if Act_ST = Etype (Exp) then
4508 return;
4509 else
4510 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4511 Analyze_And_Resolve (Exp, Act_ST);
4512 end if;
4513 end Convert_To_Actual_Subtype;
4515 -----------------------------------
4516 -- Corresponding_Runtime_Package --
4517 -----------------------------------
4519 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4520 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4521 -- Return True if protected type T has one entry and the maximum queue
4522 -- length is one.
4524 --------------------------------
4525 -- Has_One_Entry_And_No_Queue --
4526 --------------------------------
4528 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4529 Item : Entity_Id;
4530 Is_First : Boolean := True;
4532 begin
4533 Item := First_Entity (T);
4534 while Present (Item) loop
4535 if Is_Entry (Item) then
4537 -- The protected type has more than one entry
4539 if not Is_First then
4540 return False;
4541 end if;
4543 -- The queue length is not one
4545 if not Restriction_Active (No_Entry_Queue)
4546 and then Get_Max_Queue_Length (Item) /= Uint_1
4547 then
4548 return False;
4549 end if;
4551 Is_First := False;
4552 end if;
4554 Next_Entity (Item);
4555 end loop;
4557 return True;
4558 end Has_One_Entry_And_No_Queue;
4560 -- Local variables
4562 Pkg_Id : RTU_Id := RTU_Null;
4564 -- Start of processing for Corresponding_Runtime_Package
4566 begin
4567 pragma Assert (Is_Concurrent_Type (Typ));
4569 if Ekind (Typ) in Protected_Kind then
4570 if Has_Entries (Typ)
4572 -- A protected type without entries that covers an interface and
4573 -- overrides the abstract routines with protected procedures is
4574 -- considered equivalent to a protected type with entries in the
4575 -- context of dispatching select statements. It is sufficient to
4576 -- check for the presence of an interface list in the declaration
4577 -- node to recognize this case.
4579 or else Present (Interface_List (Parent (Typ)))
4581 -- Protected types with interrupt handlers (when not using a
4582 -- restricted profile) are also considered equivalent to
4583 -- protected types with entries. The types which are used
4584 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4585 -- are derived from Protection_Entries.
4587 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4588 or else Has_Interrupt_Handler (Typ)
4589 then
4590 if Abort_Allowed
4591 or else Restriction_Active (No_Select_Statements) = False
4592 or else not Has_One_Entry_And_No_Queue (Typ)
4593 or else (Has_Attach_Handler (Typ)
4594 and then not Restricted_Profile)
4595 then
4596 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4597 else
4598 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4599 end if;
4601 else
4602 Pkg_Id := System_Tasking_Protected_Objects;
4603 end if;
4604 end if;
4606 return Pkg_Id;
4607 end Corresponding_Runtime_Package;
4609 -----------------------------------
4610 -- Current_Sem_Unit_Declarations --
4611 -----------------------------------
4613 function Current_Sem_Unit_Declarations return List_Id is
4614 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4615 Decls : List_Id;
4617 begin
4618 -- If the current unit is a package body, locate the visible
4619 -- declarations of the package spec.
4621 if Nkind (U) = N_Package_Body then
4622 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4623 end if;
4625 if Nkind (U) = N_Package_Declaration then
4626 U := Specification (U);
4627 Decls := Visible_Declarations (U);
4629 if No (Decls) then
4630 Decls := New_List;
4631 Set_Visible_Declarations (U, Decls);
4632 end if;
4634 else
4635 Decls := Declarations (U);
4637 if No (Decls) then
4638 Decls := New_List;
4639 Set_Declarations (U, Decls);
4640 end if;
4641 end if;
4643 return Decls;
4644 end Current_Sem_Unit_Declarations;
4646 -----------------------
4647 -- Duplicate_Subexpr --
4648 -----------------------
4650 function Duplicate_Subexpr
4651 (Exp : Node_Id;
4652 Name_Req : Boolean := False;
4653 Renaming_Req : Boolean := False) return Node_Id
4655 begin
4656 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4657 return New_Copy_Tree (Exp);
4658 end Duplicate_Subexpr;
4660 ---------------------------------
4661 -- Duplicate_Subexpr_No_Checks --
4662 ---------------------------------
4664 function Duplicate_Subexpr_No_Checks
4665 (Exp : Node_Id;
4666 Name_Req : Boolean := False;
4667 Renaming_Req : Boolean := False;
4668 Related_Id : Entity_Id := Empty;
4669 Is_Low_Bound : Boolean := False;
4670 Is_High_Bound : Boolean := False) return Node_Id
4672 New_Exp : Node_Id;
4674 begin
4675 Remove_Side_Effects
4676 (Exp => Exp,
4677 Name_Req => Name_Req,
4678 Renaming_Req => Renaming_Req,
4679 Related_Id => Related_Id,
4680 Is_Low_Bound => Is_Low_Bound,
4681 Is_High_Bound => Is_High_Bound);
4683 New_Exp := New_Copy_Tree (Exp);
4684 Remove_Checks (New_Exp);
4685 return New_Exp;
4686 end Duplicate_Subexpr_No_Checks;
4688 -----------------------------------
4689 -- Duplicate_Subexpr_Move_Checks --
4690 -----------------------------------
4692 function Duplicate_Subexpr_Move_Checks
4693 (Exp : Node_Id;
4694 Name_Req : Boolean := False;
4695 Renaming_Req : Boolean := False) return Node_Id
4697 New_Exp : Node_Id;
4699 begin
4700 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4701 New_Exp := New_Copy_Tree (Exp);
4702 Remove_Checks (Exp);
4703 return New_Exp;
4704 end Duplicate_Subexpr_Move_Checks;
4706 --------------------
4707 -- Ensure_Defined --
4708 --------------------
4710 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4711 IR : Node_Id;
4713 begin
4714 -- An itype reference must only be created if this is a local itype, so
4715 -- that gigi can elaborate it on the proper objstack.
4717 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4718 IR := Make_Itype_Reference (Sloc (N));
4719 Set_Itype (IR, Typ);
4720 Insert_Action (N, IR);
4721 end if;
4722 end Ensure_Defined;
4724 --------------------
4725 -- Entry_Names_OK --
4726 --------------------
4728 function Entry_Names_OK return Boolean is
4729 begin
4730 return
4731 not Restricted_Profile
4732 and then not Global_Discard_Names
4733 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4734 and then not Restriction_Active (No_Local_Allocators);
4735 end Entry_Names_OK;
4737 -------------------
4738 -- Evaluate_Name --
4739 -------------------
4741 procedure Evaluate_Name (Nam : Node_Id) is
4742 begin
4743 -- For an attribute reference or an indexed component, evaluate the
4744 -- prefix, which is itself a name, recursively, and then force the
4745 -- evaluation of all the subscripts (or attribute expressions).
4747 case Nkind (Nam) is
4748 when N_Attribute_Reference
4749 | N_Indexed_Component
4751 Evaluate_Name (Prefix (Nam));
4753 declare
4754 E : Node_Id;
4756 begin
4757 E := First (Expressions (Nam));
4758 while Present (E) loop
4759 Force_Evaluation (E);
4761 if Original_Node (E) /= E then
4762 Set_Do_Range_Check
4763 (E, Do_Range_Check (Original_Node (E)));
4764 end if;
4766 Next (E);
4767 end loop;
4768 end;
4770 -- For an explicit dereference, we simply force the evaluation of
4771 -- the name expression. The dereference provides a value that is the
4772 -- address for the renamed object, and it is precisely this value
4773 -- that we want to preserve.
4775 when N_Explicit_Dereference =>
4776 Force_Evaluation (Prefix (Nam));
4778 -- For a function call, we evaluate the call
4780 when N_Function_Call =>
4781 Force_Evaluation (Nam);
4783 -- For a qualified expression, we evaluate the underlying object
4784 -- name if any, otherwise we force the evaluation of the underlying
4785 -- expression.
4787 when N_Qualified_Expression =>
4788 if Is_Object_Reference (Expression (Nam)) then
4789 Evaluate_Name (Expression (Nam));
4790 else
4791 Force_Evaluation (Expression (Nam));
4792 end if;
4794 -- For a selected component, we simply evaluate the prefix
4796 when N_Selected_Component =>
4797 Evaluate_Name (Prefix (Nam));
4799 -- For a slice, we evaluate the prefix, as for the indexed component
4800 -- case and then, if there is a range present, either directly or as
4801 -- the constraint of a discrete subtype indication, we evaluate the
4802 -- two bounds of this range.
4804 when N_Slice =>
4805 Evaluate_Name (Prefix (Nam));
4806 Evaluate_Slice_Bounds (Nam);
4808 -- For a type conversion, the expression of the conversion must be
4809 -- the name of an object, and we simply need to evaluate this name.
4811 when N_Type_Conversion =>
4812 Evaluate_Name (Expression (Nam));
4814 -- The remaining cases are direct name, operator symbol and character
4815 -- literal. In all these cases, we do nothing, since we want to
4816 -- reevaluate each time the renamed object is used.
4818 when others =>
4819 null;
4820 end case;
4821 end Evaluate_Name;
4823 ---------------------------
4824 -- Evaluate_Slice_Bounds --
4825 ---------------------------
4827 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4828 DR : constant Node_Id := Discrete_Range (Slice);
4829 Constr : Node_Id;
4830 Rexpr : Node_Id;
4832 begin
4833 if Nkind (DR) = N_Range then
4834 Force_Evaluation (Low_Bound (DR));
4835 Force_Evaluation (High_Bound (DR));
4837 elsif Nkind (DR) = N_Subtype_Indication then
4838 Constr := Constraint (DR);
4840 if Nkind (Constr) = N_Range_Constraint then
4841 Rexpr := Range_Expression (Constr);
4843 Force_Evaluation (Low_Bound (Rexpr));
4844 Force_Evaluation (High_Bound (Rexpr));
4845 end if;
4846 end if;
4847 end Evaluate_Slice_Bounds;
4849 ---------------------
4850 -- Evolve_And_Then --
4851 ---------------------
4853 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4854 begin
4855 if No (Cond) then
4856 Cond := Cond1;
4857 else
4858 Cond :=
4859 Make_And_Then (Sloc (Cond1),
4860 Left_Opnd => Cond,
4861 Right_Opnd => Cond1);
4862 end if;
4863 end Evolve_And_Then;
4865 --------------------
4866 -- Evolve_Or_Else --
4867 --------------------
4869 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4870 begin
4871 if No (Cond) then
4872 Cond := Cond1;
4873 else
4874 Cond :=
4875 Make_Or_Else (Sloc (Cond1),
4876 Left_Opnd => Cond,
4877 Right_Opnd => Cond1);
4878 end if;
4879 end Evolve_Or_Else;
4881 -----------------------------------
4882 -- Exceptions_In_Finalization_OK --
4883 -----------------------------------
4885 function Exceptions_In_Finalization_OK return Boolean is
4886 begin
4887 return
4888 not (Restriction_Active (No_Exception_Handlers) or else
4889 Restriction_Active (No_Exception_Propagation) or else
4890 Restriction_Active (No_Exceptions));
4891 end Exceptions_In_Finalization_OK;
4893 -----------------------------------------
4894 -- Expand_Static_Predicates_In_Choices --
4895 -----------------------------------------
4897 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4898 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4900 Choices : constant List_Id := Discrete_Choices (N);
4902 Choice : Node_Id;
4903 Next_C : Node_Id;
4904 P : Node_Id;
4905 C : Node_Id;
4907 begin
4908 Choice := First (Choices);
4909 while Present (Choice) loop
4910 Next_C := Next (Choice);
4912 -- Check for name of subtype with static predicate
4914 if Is_Entity_Name (Choice)
4915 and then Is_Type (Entity (Choice))
4916 and then Has_Predicates (Entity (Choice))
4917 then
4918 -- Loop through entries in predicate list, converting to choices
4919 -- and inserting in the list before the current choice. Note that
4920 -- if the list is empty, corresponding to a False predicate, then
4921 -- no choices are inserted.
4923 P := First (Static_Discrete_Predicate (Entity (Choice)));
4924 while Present (P) loop
4926 -- If low bound and high bounds are equal, copy simple choice
4928 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4929 C := New_Copy (Low_Bound (P));
4931 -- Otherwise copy a range
4933 else
4934 C := New_Copy (P);
4935 end if;
4937 -- Change Sloc to referencing choice (rather than the Sloc of
4938 -- the predicate declaration element itself).
4940 Set_Sloc (C, Sloc (Choice));
4941 Insert_Before (Choice, C);
4942 Next (P);
4943 end loop;
4945 -- Delete the predicated entry
4947 Remove (Choice);
4948 end if;
4950 -- Move to next choice to check
4952 Choice := Next_C;
4953 end loop;
4954 end Expand_Static_Predicates_In_Choices;
4956 ------------------------------
4957 -- Expand_Subtype_From_Expr --
4958 ------------------------------
4960 -- This function is applicable for both static and dynamic allocation of
4961 -- objects which are constrained by an initial expression. Basically it
4962 -- transforms an unconstrained subtype indication into a constrained one.
4964 -- The expression may also be transformed in certain cases in order to
4965 -- avoid multiple evaluation. In the static allocation case, the general
4966 -- scheme is:
4968 -- Val : T := Expr;
4970 -- is transformed into
4972 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
4974 -- Here are the main cases :
4976 -- <if Expr is a Slice>
4977 -- Val : T ([Index_Subtype (Expr)]) := Expr;
4979 -- <elsif Expr is a String Literal>
4980 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
4982 -- <elsif Expr is Constrained>
4983 -- subtype T is Type_Of_Expr
4984 -- Val : T := Expr;
4986 -- <elsif Expr is an entity_name>
4987 -- Val : T (constraints taken from Expr) := Expr;
4989 -- <else>
4990 -- type Axxx is access all T;
4991 -- Rval : Axxx := Expr'ref;
4992 -- Val : T (constraints taken from Rval) := Rval.all;
4994 -- ??? note: when the Expression is allocated in the secondary stack
4995 -- we could use it directly instead of copying it by declaring
4996 -- Val : T (...) renames Rval.all
4998 procedure Expand_Subtype_From_Expr
4999 (N : Node_Id;
5000 Unc_Type : Entity_Id;
5001 Subtype_Indic : Node_Id;
5002 Exp : Node_Id;
5003 Related_Id : Entity_Id := Empty)
5005 Loc : constant Source_Ptr := Sloc (N);
5006 Exp_Typ : constant Entity_Id := Etype (Exp);
5007 T : Entity_Id;
5009 begin
5010 -- In general we cannot build the subtype if expansion is disabled,
5011 -- because internal entities may not have been defined. However, to
5012 -- avoid some cascaded errors, we try to continue when the expression is
5013 -- an array (or string), because it is safe to compute the bounds. It is
5014 -- in fact required to do so even in a generic context, because there
5015 -- may be constants that depend on the bounds of a string literal, both
5016 -- standard string types and more generally arrays of characters.
5018 -- In GNATprove mode, these extra subtypes are not needed
5020 if GNATprove_Mode then
5021 return;
5022 end if;
5024 if not Expander_Active
5025 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5026 then
5027 return;
5028 end if;
5030 if Nkind (Exp) = N_Slice then
5031 declare
5032 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5034 begin
5035 Rewrite (Subtype_Indic,
5036 Make_Subtype_Indication (Loc,
5037 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5038 Constraint =>
5039 Make_Index_Or_Discriminant_Constraint (Loc,
5040 Constraints => New_List
5041 (New_Occurrence_Of (Slice_Type, Loc)))));
5043 -- This subtype indication may be used later for constraint checks
5044 -- we better make sure that if a variable was used as a bound of
5045 -- of the original slice, its value is frozen.
5047 Evaluate_Slice_Bounds (Exp);
5048 end;
5050 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5051 Rewrite (Subtype_Indic,
5052 Make_Subtype_Indication (Loc,
5053 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5054 Constraint =>
5055 Make_Index_Or_Discriminant_Constraint (Loc,
5056 Constraints => New_List (
5057 Make_Literal_Range (Loc,
5058 Literal_Typ => Exp_Typ)))));
5060 -- If the type of the expression is an internally generated type it
5061 -- may not be necessary to create a new subtype. However there are two
5062 -- exceptions: references to the current instances, and aliased array
5063 -- object declarations for which the back end has to create a template.
5065 elsif Is_Constrained (Exp_Typ)
5066 and then not Is_Class_Wide_Type (Unc_Type)
5067 and then
5068 (Nkind (N) /= N_Object_Declaration
5069 or else not Is_Entity_Name (Expression (N))
5070 or else not Comes_From_Source (Entity (Expression (N)))
5071 or else not Is_Array_Type (Exp_Typ)
5072 or else not Aliased_Present (N))
5073 then
5074 if Is_Itype (Exp_Typ) then
5076 -- Within an initialization procedure, a selected component
5077 -- denotes a component of the enclosing record, and it appears as
5078 -- an actual in a call to its own initialization procedure. If
5079 -- this component depends on the outer discriminant, we must
5080 -- generate the proper actual subtype for it.
5082 if Nkind (Exp) = N_Selected_Component
5083 and then Within_Init_Proc
5084 then
5085 declare
5086 Decl : constant Node_Id :=
5087 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5088 begin
5089 if Present (Decl) then
5090 Insert_Action (N, Decl);
5091 T := Defining_Identifier (Decl);
5092 else
5093 T := Exp_Typ;
5094 end if;
5095 end;
5097 -- No need to generate a new subtype
5099 else
5100 T := Exp_Typ;
5101 end if;
5103 else
5104 T := Make_Temporary (Loc, 'T');
5106 Insert_Action (N,
5107 Make_Subtype_Declaration (Loc,
5108 Defining_Identifier => T,
5109 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5111 -- This type is marked as an itype even though it has an explicit
5112 -- declaration since otherwise Is_Generic_Actual_Type can get
5113 -- set, resulting in the generation of spurious errors. (See
5114 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5116 Set_Is_Itype (T);
5117 Set_Associated_Node_For_Itype (T, Exp);
5118 end if;
5120 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5122 -- Nothing needs to be done for private types with unknown discriminants
5123 -- if the underlying type is not an unconstrained composite type or it
5124 -- is an unchecked union.
5126 elsif Is_Private_Type (Unc_Type)
5127 and then Has_Unknown_Discriminants (Unc_Type)
5128 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5129 or else Is_Constrained (Underlying_Type (Unc_Type))
5130 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5131 then
5132 null;
5134 -- Case of derived type with unknown discriminants where the parent type
5135 -- also has unknown discriminants.
5137 elsif Is_Record_Type (Unc_Type)
5138 and then not Is_Class_Wide_Type (Unc_Type)
5139 and then Has_Unknown_Discriminants (Unc_Type)
5140 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5141 then
5142 -- Nothing to be done if no underlying record view available
5144 -- If this is a limited type derived from a type with unknown
5145 -- discriminants, do not expand either, so that subsequent expansion
5146 -- of the call can add build-in-place parameters to call.
5148 if No (Underlying_Record_View (Unc_Type))
5149 or else Is_Limited_Type (Unc_Type)
5150 then
5151 null;
5153 -- Otherwise use the Underlying_Record_View to create the proper
5154 -- constrained subtype for an object of a derived type with unknown
5155 -- discriminants.
5157 else
5158 Remove_Side_Effects (Exp);
5159 Rewrite (Subtype_Indic,
5160 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5161 end if;
5163 -- Renamings of class-wide interface types require no equivalent
5164 -- constrained type declarations because we only need to reference
5165 -- the tag component associated with the interface. The same is
5166 -- presumably true for class-wide types in general, so this test
5167 -- is broadened to include all class-wide renamings, which also
5168 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5169 -- (Is this really correct, or are there some cases of class-wide
5170 -- renamings that require action in this procedure???)
5172 elsif Present (N)
5173 and then Nkind (N) = N_Object_Renaming_Declaration
5174 and then Is_Class_Wide_Type (Unc_Type)
5175 then
5176 null;
5178 -- In Ada 95 nothing to be done if the type of the expression is limited
5179 -- because in this case the expression cannot be copied, and its use can
5180 -- only be by reference.
5182 -- In Ada 2005 the context can be an object declaration whose expression
5183 -- is a function that returns in place. If the nominal subtype has
5184 -- unknown discriminants, the call still provides constraints on the
5185 -- object, and we have to create an actual subtype from it.
5187 -- If the type is class-wide, the expression is dynamically tagged and
5188 -- we do not create an actual subtype either. Ditto for an interface.
5189 -- For now this applies only if the type is immutably limited, and the
5190 -- function being called is build-in-place. This will have to be revised
5191 -- when build-in-place functions are generalized to other types.
5193 elsif Is_Limited_View (Exp_Typ)
5194 and then
5195 (Is_Class_Wide_Type (Exp_Typ)
5196 or else Is_Interface (Exp_Typ)
5197 or else not Has_Unknown_Discriminants (Exp_Typ)
5198 or else not Is_Composite_Type (Unc_Type))
5199 then
5200 null;
5202 -- For limited objects initialized with build in place function calls,
5203 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5204 -- node in the expression initializing the object, which breaks the
5205 -- circuitry that detects and adds the additional arguments to the
5206 -- called function.
5208 elsif Is_Build_In_Place_Function_Call (Exp) then
5209 null;
5211 else
5212 Remove_Side_Effects (Exp);
5213 Rewrite (Subtype_Indic,
5214 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5215 end if;
5216 end Expand_Subtype_From_Expr;
5218 ---------------------------------------------
5219 -- Expression_Contains_Primitives_Calls_Of --
5220 ---------------------------------------------
5222 function Expression_Contains_Primitives_Calls_Of
5223 (Expr : Node_Id;
5224 Typ : Entity_Id) return Boolean
5226 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5228 Calls_OK : Boolean := False;
5229 -- This flag is set to True when expression Expr contains at least one
5230 -- call to a nondispatching primitive function of Typ.
5232 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5233 -- Search for nondispatching calls to primitive functions of type Typ
5235 ----------------------------
5236 -- Search_Primitive_Calls --
5237 ----------------------------
5239 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5240 Disp_Typ : Entity_Id;
5241 Subp : Entity_Id;
5243 begin
5244 -- Detect a function call that could denote a nondispatching
5245 -- primitive of the input type.
5247 if Nkind (N) = N_Function_Call
5248 and then Is_Entity_Name (Name (N))
5249 then
5250 Subp := Entity (Name (N));
5252 -- Do not consider function calls with a controlling argument, as
5253 -- those are always dispatching calls.
5255 if Is_Dispatching_Operation (Subp)
5256 and then No (Controlling_Argument (N))
5257 then
5258 Disp_Typ := Find_Dispatching_Type (Subp);
5260 -- To qualify as a suitable primitive, the dispatching type of
5261 -- the function must be the input type.
5263 if Present (Disp_Typ)
5264 and then Unique_Entity (Disp_Typ) = U_Typ
5265 then
5266 Calls_OK := True;
5268 -- There is no need to continue the traversal, as one such
5269 -- call suffices.
5271 return Abandon;
5272 end if;
5273 end if;
5274 end if;
5276 return OK;
5277 end Search_Primitive_Calls;
5279 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5281 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5283 begin
5284 Search_Calls (Expr);
5285 return Calls_OK;
5286 end Expression_Contains_Primitives_Calls_Of;
5288 ----------------------
5289 -- Finalize_Address --
5290 ----------------------
5292 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5293 Utyp : Entity_Id := Typ;
5295 begin
5296 -- Handle protected class-wide or task class-wide types
5298 if Is_Class_Wide_Type (Utyp) then
5299 if Is_Concurrent_Type (Root_Type (Utyp)) then
5300 Utyp := Root_Type (Utyp);
5302 elsif Is_Private_Type (Root_Type (Utyp))
5303 and then Present (Full_View (Root_Type (Utyp)))
5304 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5305 then
5306 Utyp := Full_View (Root_Type (Utyp));
5307 end if;
5308 end if;
5310 -- Handle private types
5312 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5313 Utyp := Full_View (Utyp);
5314 end if;
5316 -- Handle protected and task types
5318 if Is_Concurrent_Type (Utyp)
5319 and then Present (Corresponding_Record_Type (Utyp))
5320 then
5321 Utyp := Corresponding_Record_Type (Utyp);
5322 end if;
5324 Utyp := Underlying_Type (Base_Type (Utyp));
5326 -- Deal with untagged derivation of private views. If the parent is
5327 -- now known to be protected, the finalization routine is the one
5328 -- defined on the corresponding record of the ancestor (corresponding
5329 -- records do not automatically inherit operations, but maybe they
5330 -- should???)
5332 if Is_Untagged_Derivation (Typ) then
5333 if Is_Protected_Type (Typ) then
5334 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5336 else
5337 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5339 if Is_Protected_Type (Utyp) then
5340 Utyp := Corresponding_Record_Type (Utyp);
5341 end if;
5342 end if;
5343 end if;
5345 -- If the underlying_type is a subtype, we are dealing with the
5346 -- completion of a private type. We need to access the base type and
5347 -- generate a conversion to it.
5349 if Utyp /= Base_Type (Utyp) then
5350 pragma Assert (Is_Private_Type (Typ));
5352 Utyp := Base_Type (Utyp);
5353 end if;
5355 -- When dealing with an internally built full view for a type with
5356 -- unknown discriminants, use the original record type.
5358 if Is_Underlying_Record_View (Utyp) then
5359 Utyp := Etype (Utyp);
5360 end if;
5362 return TSS (Utyp, TSS_Finalize_Address);
5363 end Finalize_Address;
5365 -------------------
5366 -- Find_DIC_Type --
5367 -------------------
5369 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id is
5370 Curr_Typ : Entity_Id;
5371 -- The current type being examined in the parent hierarchy traversal
5373 DIC_Typ : Entity_Id;
5374 -- The type which carries the DIC pragma. This variable denotes the
5375 -- partial view when private types are involved.
5377 Par_Typ : Entity_Id;
5378 -- The parent type of the current type. This variable denotes the full
5379 -- view when private types are involved.
5381 begin
5382 -- The input type defines its own DIC pragma, therefore it is the owner
5384 if Has_Own_DIC (Typ) then
5385 DIC_Typ := Typ;
5387 -- Otherwise the DIC pragma is inherited from a parent type
5389 else
5390 pragma Assert (Has_Inherited_DIC (Typ));
5392 -- Climb the parent chain
5394 Curr_Typ := Typ;
5395 loop
5396 -- Inspect the parent type. Do not consider subtypes as they
5397 -- inherit the DIC attributes from their base types.
5399 DIC_Typ := Base_Type (Etype (Curr_Typ));
5401 -- Look at the full view of a private type because the type may
5402 -- have a hidden parent introduced in the full view.
5404 Par_Typ := DIC_Typ;
5406 if Is_Private_Type (Par_Typ)
5407 and then Present (Full_View (Par_Typ))
5408 then
5409 Par_Typ := Full_View (Par_Typ);
5410 end if;
5412 -- Stop the climb once the nearest parent type which defines a DIC
5413 -- pragma of its own is encountered or when the root of the parent
5414 -- chain is reached.
5416 exit when Has_Own_DIC (DIC_Typ) or else Curr_Typ = Par_Typ;
5418 Curr_Typ := Par_Typ;
5419 end loop;
5420 end if;
5422 return DIC_Typ;
5423 end Find_DIC_Type;
5425 ------------------------
5426 -- Find_Interface_ADT --
5427 ------------------------
5429 function Find_Interface_ADT
5430 (T : Entity_Id;
5431 Iface : Entity_Id) return Elmt_Id
5433 ADT : Elmt_Id;
5434 Typ : Entity_Id := T;
5436 begin
5437 pragma Assert (Is_Interface (Iface));
5439 -- Handle private types
5441 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5442 Typ := Full_View (Typ);
5443 end if;
5445 -- Handle access types
5447 if Is_Access_Type (Typ) then
5448 Typ := Designated_Type (Typ);
5449 end if;
5451 -- Handle task and protected types implementing interfaces
5453 if Is_Concurrent_Type (Typ) then
5454 Typ := Corresponding_Record_Type (Typ);
5455 end if;
5457 pragma Assert
5458 (not Is_Class_Wide_Type (Typ)
5459 and then Ekind (Typ) /= E_Incomplete_Type);
5461 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5462 return First_Elmt (Access_Disp_Table (Typ));
5464 else
5465 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5466 while Present (ADT)
5467 and then Present (Related_Type (Node (ADT)))
5468 and then Related_Type (Node (ADT)) /= Iface
5469 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5470 Use_Full_View => True)
5471 loop
5472 Next_Elmt (ADT);
5473 end loop;
5475 pragma Assert (Present (Related_Type (Node (ADT))));
5476 return ADT;
5477 end if;
5478 end Find_Interface_ADT;
5480 ------------------------
5481 -- Find_Interface_Tag --
5482 ------------------------
5484 function Find_Interface_Tag
5485 (T : Entity_Id;
5486 Iface : Entity_Id) return Entity_Id
5488 AI_Tag : Entity_Id;
5489 Found : Boolean := False;
5490 Typ : Entity_Id := T;
5492 procedure Find_Tag (Typ : Entity_Id);
5493 -- Internal subprogram used to recursively climb to the ancestors
5495 --------------
5496 -- Find_Tag --
5497 --------------
5499 procedure Find_Tag (Typ : Entity_Id) is
5500 AI_Elmt : Elmt_Id;
5501 AI : Node_Id;
5503 begin
5504 -- This routine does not handle the case in which the interface is an
5505 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5507 pragma Assert (Typ /= Iface);
5509 -- Climb to the root type handling private types
5511 if Present (Full_View (Etype (Typ))) then
5512 if Full_View (Etype (Typ)) /= Typ then
5513 Find_Tag (Full_View (Etype (Typ)));
5514 end if;
5516 elsif Etype (Typ) /= Typ then
5517 Find_Tag (Etype (Typ));
5518 end if;
5520 -- Traverse the list of interfaces implemented by the type
5522 if not Found
5523 and then Present (Interfaces (Typ))
5524 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5525 then
5526 -- Skip the tag associated with the primary table
5528 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5529 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5530 pragma Assert (Present (AI_Tag));
5532 AI_Elmt := First_Elmt (Interfaces (Typ));
5533 while Present (AI_Elmt) loop
5534 AI := Node (AI_Elmt);
5536 if AI = Iface
5537 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5538 then
5539 Found := True;
5540 return;
5541 end if;
5543 AI_Tag := Next_Tag_Component (AI_Tag);
5544 Next_Elmt (AI_Elmt);
5545 end loop;
5546 end if;
5547 end Find_Tag;
5549 -- Start of processing for Find_Interface_Tag
5551 begin
5552 pragma Assert (Is_Interface (Iface));
5554 -- Handle access types
5556 if Is_Access_Type (Typ) then
5557 Typ := Designated_Type (Typ);
5558 end if;
5560 -- Handle class-wide types
5562 if Is_Class_Wide_Type (Typ) then
5563 Typ := Root_Type (Typ);
5564 end if;
5566 -- Handle private types
5568 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5569 Typ := Full_View (Typ);
5570 end if;
5572 -- Handle entities from the limited view
5574 if Ekind (Typ) = E_Incomplete_Type then
5575 pragma Assert (Present (Non_Limited_View (Typ)));
5576 Typ := Non_Limited_View (Typ);
5577 end if;
5579 -- Handle task and protected types implementing interfaces
5581 if Is_Concurrent_Type (Typ) then
5582 Typ := Corresponding_Record_Type (Typ);
5583 end if;
5585 -- If the interface is an ancestor of the type, then it shared the
5586 -- primary dispatch table.
5588 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5589 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5590 return First_Tag_Component (Typ);
5592 -- Otherwise we need to search for its associated tag component
5594 else
5595 Find_Tag (Typ);
5596 pragma Assert (Found);
5597 return AI_Tag;
5598 end if;
5599 end Find_Interface_Tag;
5601 ---------------------------
5602 -- Find_Optional_Prim_Op --
5603 ---------------------------
5605 function Find_Optional_Prim_Op
5606 (T : Entity_Id; Name : Name_Id) return Entity_Id
5608 Prim : Elmt_Id;
5609 Typ : Entity_Id := T;
5610 Op : Entity_Id;
5612 begin
5613 if Is_Class_Wide_Type (Typ) then
5614 Typ := Root_Type (Typ);
5615 end if;
5617 Typ := Underlying_Type (Typ);
5619 -- Loop through primitive operations
5621 Prim := First_Elmt (Primitive_Operations (Typ));
5622 while Present (Prim) loop
5623 Op := Node (Prim);
5625 -- We can retrieve primitive operations by name if it is an internal
5626 -- name. For equality we must check that both of its operands have
5627 -- the same type, to avoid confusion with user-defined equalities
5628 -- than may have a non-symmetric signature.
5630 exit when Chars (Op) = Name
5631 and then
5632 (Name /= Name_Op_Eq
5633 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5635 Next_Elmt (Prim);
5636 end loop;
5638 return Node (Prim); -- Empty if not found
5639 end Find_Optional_Prim_Op;
5641 ---------------------------
5642 -- Find_Optional_Prim_Op --
5643 ---------------------------
5645 function Find_Optional_Prim_Op
5646 (T : Entity_Id;
5647 Name : TSS_Name_Type) return Entity_Id
5649 Inher_Op : Entity_Id := Empty;
5650 Own_Op : Entity_Id := Empty;
5651 Prim_Elmt : Elmt_Id;
5652 Prim_Id : Entity_Id;
5653 Typ : Entity_Id := T;
5655 begin
5656 if Is_Class_Wide_Type (Typ) then
5657 Typ := Root_Type (Typ);
5658 end if;
5660 Typ := Underlying_Type (Typ);
5662 -- This search is based on the assertion that the dispatching version
5663 -- of the TSS routine always precedes the real primitive.
5665 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5666 while Present (Prim_Elmt) loop
5667 Prim_Id := Node (Prim_Elmt);
5669 if Is_TSS (Prim_Id, Name) then
5670 if Present (Alias (Prim_Id)) then
5671 Inher_Op := Prim_Id;
5672 else
5673 Own_Op := Prim_Id;
5674 end if;
5675 end if;
5677 Next_Elmt (Prim_Elmt);
5678 end loop;
5680 if Present (Own_Op) then
5681 return Own_Op;
5682 elsif Present (Inher_Op) then
5683 return Inher_Op;
5684 else
5685 return Empty;
5686 end if;
5687 end Find_Optional_Prim_Op;
5689 ------------------
5690 -- Find_Prim_Op --
5691 ------------------
5693 function Find_Prim_Op
5694 (T : Entity_Id; Name : Name_Id) return Entity_Id
5696 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5697 begin
5698 if No (Result) then
5699 raise Program_Error;
5700 end if;
5702 return Result;
5703 end Find_Prim_Op;
5705 ------------------
5706 -- Find_Prim_Op --
5707 ------------------
5709 function Find_Prim_Op
5710 (T : Entity_Id;
5711 Name : TSS_Name_Type) return Entity_Id
5713 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5714 begin
5715 if No (Result) then
5716 raise Program_Error;
5717 end if;
5719 return Result;
5720 end Find_Prim_Op;
5722 ----------------------------
5723 -- Find_Protection_Object --
5724 ----------------------------
5726 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5727 S : Entity_Id;
5729 begin
5730 S := Scop;
5731 while Present (S) loop
5732 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5733 and then Present (Protection_Object (S))
5734 then
5735 return Protection_Object (S);
5736 end if;
5738 S := Scope (S);
5739 end loop;
5741 -- If we do not find a Protection object in the scope chain, then
5742 -- something has gone wrong, most likely the object was never created.
5744 raise Program_Error;
5745 end Find_Protection_Object;
5747 --------------------------
5748 -- Find_Protection_Type --
5749 --------------------------
5751 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5752 Comp : Entity_Id;
5753 Typ : Entity_Id := Conc_Typ;
5755 begin
5756 if Is_Concurrent_Type (Typ) then
5757 Typ := Corresponding_Record_Type (Typ);
5758 end if;
5760 -- Since restriction violations are not considered serious errors, the
5761 -- expander remains active, but may leave the corresponding record type
5762 -- malformed. In such cases, component _object is not available so do
5763 -- not look for it.
5765 if not Analyzed (Typ) then
5766 return Empty;
5767 end if;
5769 Comp := First_Component (Typ);
5770 while Present (Comp) loop
5771 if Chars (Comp) = Name_uObject then
5772 return Base_Type (Etype (Comp));
5773 end if;
5775 Next_Component (Comp);
5776 end loop;
5778 -- The corresponding record of a protected type should always have an
5779 -- _object field.
5781 raise Program_Error;
5782 end Find_Protection_Type;
5784 -----------------------
5785 -- Find_Hook_Context --
5786 -----------------------
5788 function Find_Hook_Context (N : Node_Id) return Node_Id is
5789 Par : Node_Id;
5790 Top : Node_Id;
5792 Wrapped_Node : Node_Id;
5793 -- Note: if we are in a transient scope, we want to reuse it as
5794 -- the context for actions insertion, if possible. But if N is itself
5795 -- part of the stored actions for the current transient scope,
5796 -- then we need to insert at the appropriate (inner) location in
5797 -- the not as an action on Node_To_Be_Wrapped.
5799 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5801 begin
5802 -- When the node is inside a case/if expression, the lifetime of any
5803 -- temporary controlled object is extended. Find a suitable insertion
5804 -- node by locating the topmost case or if expressions.
5806 if In_Cond_Expr then
5807 Par := N;
5808 Top := N;
5809 while Present (Par) loop
5810 if Nkind_In (Original_Node (Par), N_Case_Expression,
5811 N_If_Expression)
5812 then
5813 Top := Par;
5815 -- Prevent the search from going too far
5817 elsif Is_Body_Or_Package_Declaration (Par) then
5818 exit;
5819 end if;
5821 Par := Parent (Par);
5822 end loop;
5824 -- The topmost case or if expression is now recovered, but it may
5825 -- still not be the correct place to add generated code. Climb to
5826 -- find a parent that is part of a declarative or statement list,
5827 -- and is not a list of actuals in a call.
5829 Par := Top;
5830 while Present (Par) loop
5831 if Is_List_Member (Par)
5832 and then not Nkind_In (Par, N_Component_Association,
5833 N_Discriminant_Association,
5834 N_Parameter_Association,
5835 N_Pragma_Argument_Association)
5836 and then not Nkind_In (Parent (Par), N_Function_Call,
5837 N_Procedure_Call_Statement,
5838 N_Entry_Call_Statement)
5840 then
5841 return Par;
5843 -- Prevent the search from going too far
5845 elsif Is_Body_Or_Package_Declaration (Par) then
5846 exit;
5847 end if;
5849 Par := Parent (Par);
5850 end loop;
5852 return Par;
5854 else
5855 Par := N;
5856 while Present (Par) loop
5858 -- Keep climbing past various operators
5860 if Nkind (Parent (Par)) in N_Op
5861 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5862 then
5863 Par := Parent (Par);
5864 else
5865 exit;
5866 end if;
5867 end loop;
5869 Top := Par;
5871 -- The node may be located in a pragma in which case return the
5872 -- pragma itself:
5874 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5876 -- Similar case occurs when the node is related to an object
5877 -- declaration or assignment:
5879 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5881 -- Another case to consider is when the node is part of a return
5882 -- statement:
5884 -- return ... and then Ctrl_Func_Call ...;
5886 -- Another case is when the node acts as a formal in a procedure
5887 -- call statement:
5889 -- Proc (... and then Ctrl_Func_Call ...);
5891 if Scope_Is_Transient then
5892 Wrapped_Node := Node_To_Be_Wrapped;
5893 else
5894 Wrapped_Node := Empty;
5895 end if;
5897 while Present (Par) loop
5898 if Par = Wrapped_Node
5899 or else Nkind_In (Par, N_Assignment_Statement,
5900 N_Object_Declaration,
5901 N_Pragma,
5902 N_Procedure_Call_Statement,
5903 N_Simple_Return_Statement)
5904 then
5905 return Par;
5907 -- Prevent the search from going too far
5909 elsif Is_Body_Or_Package_Declaration (Par) then
5910 exit;
5911 end if;
5913 Par := Parent (Par);
5914 end loop;
5916 -- Return the topmost short circuit operator
5918 return Top;
5919 end if;
5920 end Find_Hook_Context;
5922 ------------------------------
5923 -- Following_Address_Clause --
5924 ------------------------------
5926 function Following_Address_Clause (D : Node_Id) return Node_Id is
5927 Id : constant Entity_Id := Defining_Identifier (D);
5928 Result : Node_Id;
5929 Par : Node_Id;
5931 function Check_Decls (D : Node_Id) return Node_Id;
5932 -- This internal function differs from the main function in that it
5933 -- gets called to deal with a following package private part, and
5934 -- it checks declarations starting with D (the main function checks
5935 -- declarations following D). If D is Empty, then Empty is returned.
5937 -----------------
5938 -- Check_Decls --
5939 -----------------
5941 function Check_Decls (D : Node_Id) return Node_Id is
5942 Decl : Node_Id;
5944 begin
5945 Decl := D;
5946 while Present (Decl) loop
5947 if Nkind (Decl) = N_At_Clause
5948 and then Chars (Identifier (Decl)) = Chars (Id)
5949 then
5950 return Decl;
5952 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5953 and then Chars (Decl) = Name_Address
5954 and then Chars (Name (Decl)) = Chars (Id)
5955 then
5956 return Decl;
5957 end if;
5959 Next (Decl);
5960 end loop;
5962 -- Otherwise not found, return Empty
5964 return Empty;
5965 end Check_Decls;
5967 -- Start of processing for Following_Address_Clause
5969 begin
5970 -- If parser detected no address clause for the identifier in question,
5971 -- then the answer is a quick NO, without the need for a search.
5973 if not Get_Name_Table_Boolean1 (Chars (Id)) then
5974 return Empty;
5975 end if;
5977 -- Otherwise search current declarative unit
5979 Result := Check_Decls (Next (D));
5981 if Present (Result) then
5982 return Result;
5983 end if;
5985 -- Check for possible package private part following
5987 Par := Parent (D);
5989 if Nkind (Par) = N_Package_Specification
5990 and then Visible_Declarations (Par) = List_Containing (D)
5991 and then Present (Private_Declarations (Par))
5992 then
5993 -- Private part present, check declarations there
5995 return Check_Decls (First (Private_Declarations (Par)));
5997 else
5998 -- No private part, clause not found, return Empty
6000 return Empty;
6001 end if;
6002 end Following_Address_Clause;
6004 ----------------------
6005 -- Force_Evaluation --
6006 ----------------------
6008 procedure Force_Evaluation
6009 (Exp : Node_Id;
6010 Name_Req : Boolean := False;
6011 Related_Id : Entity_Id := Empty;
6012 Is_Low_Bound : Boolean := False;
6013 Is_High_Bound : Boolean := False;
6014 Mode : Force_Evaluation_Mode := Relaxed)
6016 begin
6017 Remove_Side_Effects
6018 (Exp => Exp,
6019 Name_Req => Name_Req,
6020 Variable_Ref => True,
6021 Renaming_Req => False,
6022 Related_Id => Related_Id,
6023 Is_Low_Bound => Is_Low_Bound,
6024 Is_High_Bound => Is_High_Bound,
6025 Check_Side_Effects =>
6026 Is_Static_Expression (Exp)
6027 or else Mode = Relaxed);
6028 end Force_Evaluation;
6030 ---------------------------------
6031 -- Fully_Qualified_Name_String --
6032 ---------------------------------
6034 function Fully_Qualified_Name_String
6035 (E : Entity_Id;
6036 Append_NUL : Boolean := True) return String_Id
6038 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6039 -- Compute recursively the qualified name without NUL at the end, adding
6040 -- it to the currently started string being generated
6042 ----------------------------------
6043 -- Internal_Full_Qualified_Name --
6044 ----------------------------------
6046 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6047 Ent : Entity_Id;
6049 begin
6050 -- Deal properly with child units
6052 if Nkind (E) = N_Defining_Program_Unit_Name then
6053 Ent := Defining_Identifier (E);
6054 else
6055 Ent := E;
6056 end if;
6058 -- Compute qualification recursively (only "Standard" has no scope)
6060 if Present (Scope (Scope (Ent))) then
6061 Internal_Full_Qualified_Name (Scope (Ent));
6062 Store_String_Char (Get_Char_Code ('.'));
6063 end if;
6065 -- Every entity should have a name except some expanded blocks
6066 -- don't bother about those.
6068 if Chars (Ent) = No_Name then
6069 return;
6070 end if;
6072 -- Generates the entity name in upper case
6074 Get_Decoded_Name_String (Chars (Ent));
6075 Set_All_Upper_Case;
6076 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6077 return;
6078 end Internal_Full_Qualified_Name;
6080 -- Start of processing for Full_Qualified_Name
6082 begin
6083 Start_String;
6084 Internal_Full_Qualified_Name (E);
6086 if Append_NUL then
6087 Store_String_Char (Get_Char_Code (ASCII.NUL));
6088 end if;
6090 return End_String;
6091 end Fully_Qualified_Name_String;
6093 ------------------------
6094 -- Generate_Poll_Call --
6095 ------------------------
6097 procedure Generate_Poll_Call (N : Node_Id) is
6098 begin
6099 -- No poll call if polling not active
6101 if not Polling_Required then
6102 return;
6104 -- Otherwise generate require poll call
6106 else
6107 Insert_Before_And_Analyze (N,
6108 Make_Procedure_Call_Statement (Sloc (N),
6109 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6110 end if;
6111 end Generate_Poll_Call;
6113 ---------------------------------
6114 -- Get_Current_Value_Condition --
6115 ---------------------------------
6117 -- Note: the implementation of this procedure is very closely tied to the
6118 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6119 -- interpret Current_Value fields set by the Set procedure, so the two
6120 -- procedures need to be closely coordinated.
6122 procedure Get_Current_Value_Condition
6123 (Var : Node_Id;
6124 Op : out Node_Kind;
6125 Val : out Node_Id)
6127 Loc : constant Source_Ptr := Sloc (Var);
6128 Ent : constant Entity_Id := Entity (Var);
6130 procedure Process_Current_Value_Condition
6131 (N : Node_Id;
6132 S : Boolean);
6133 -- N is an expression which holds either True (S = True) or False (S =
6134 -- False) in the condition. This procedure digs out the expression and
6135 -- if it refers to Ent, sets Op and Val appropriately.
6137 -------------------------------------
6138 -- Process_Current_Value_Condition --
6139 -------------------------------------
6141 procedure Process_Current_Value_Condition
6142 (N : Node_Id;
6143 S : Boolean)
6145 Cond : Node_Id;
6146 Prev_Cond : Node_Id;
6147 Sens : Boolean;
6149 begin
6150 Cond := N;
6151 Sens := S;
6153 loop
6154 Prev_Cond := Cond;
6156 -- Deal with NOT operators, inverting sense
6158 while Nkind (Cond) = N_Op_Not loop
6159 Cond := Right_Opnd (Cond);
6160 Sens := not Sens;
6161 end loop;
6163 -- Deal with conversions, qualifications, and expressions with
6164 -- actions.
6166 while Nkind_In (Cond,
6167 N_Type_Conversion,
6168 N_Qualified_Expression,
6169 N_Expression_With_Actions)
6170 loop
6171 Cond := Expression (Cond);
6172 end loop;
6174 exit when Cond = Prev_Cond;
6175 end loop;
6177 -- Deal with AND THEN and AND cases
6179 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6181 -- Don't ever try to invert a condition that is of the form of an
6182 -- AND or AND THEN (since we are not doing sufficiently general
6183 -- processing to allow this).
6185 if Sens = False then
6186 Op := N_Empty;
6187 Val := Empty;
6188 return;
6189 end if;
6191 -- Recursively process AND and AND THEN branches
6193 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6195 if Op /= N_Empty then
6196 return;
6197 end if;
6199 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6200 return;
6202 -- Case of relational operator
6204 elsif Nkind (Cond) in N_Op_Compare then
6205 Op := Nkind (Cond);
6207 -- Invert sense of test if inverted test
6209 if Sens = False then
6210 case Op is
6211 when N_Op_Eq => Op := N_Op_Ne;
6212 when N_Op_Ne => Op := N_Op_Eq;
6213 when N_Op_Lt => Op := N_Op_Ge;
6214 when N_Op_Gt => Op := N_Op_Le;
6215 when N_Op_Le => Op := N_Op_Gt;
6216 when N_Op_Ge => Op := N_Op_Lt;
6217 when others => raise Program_Error;
6218 end case;
6219 end if;
6221 -- Case of entity op value
6223 if Is_Entity_Name (Left_Opnd (Cond))
6224 and then Ent = Entity (Left_Opnd (Cond))
6225 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6226 then
6227 Val := Right_Opnd (Cond);
6229 -- Case of value op entity
6231 elsif Is_Entity_Name (Right_Opnd (Cond))
6232 and then Ent = Entity (Right_Opnd (Cond))
6233 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6234 then
6235 Val := Left_Opnd (Cond);
6237 -- We are effectively swapping operands
6239 case Op is
6240 when N_Op_Eq => null;
6241 when N_Op_Ne => null;
6242 when N_Op_Lt => Op := N_Op_Gt;
6243 when N_Op_Gt => Op := N_Op_Lt;
6244 when N_Op_Le => Op := N_Op_Ge;
6245 when N_Op_Ge => Op := N_Op_Le;
6246 when others => raise Program_Error;
6247 end case;
6249 else
6250 Op := N_Empty;
6251 end if;
6253 return;
6255 elsif Nkind_In (Cond,
6256 N_Type_Conversion,
6257 N_Qualified_Expression,
6258 N_Expression_With_Actions)
6259 then
6260 Cond := Expression (Cond);
6262 -- Case of Boolean variable reference, return as though the
6263 -- reference had said var = True.
6265 else
6266 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6267 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6269 if Sens = False then
6270 Op := N_Op_Ne;
6271 else
6272 Op := N_Op_Eq;
6273 end if;
6274 end if;
6275 end if;
6276 end Process_Current_Value_Condition;
6278 -- Start of processing for Get_Current_Value_Condition
6280 begin
6281 Op := N_Empty;
6282 Val := Empty;
6284 -- Immediate return, nothing doing, if this is not an object
6286 if Ekind (Ent) not in Object_Kind then
6287 return;
6288 end if;
6290 -- Otherwise examine current value
6292 declare
6293 CV : constant Node_Id := Current_Value (Ent);
6294 Sens : Boolean;
6295 Stm : Node_Id;
6297 begin
6298 -- If statement. Condition is known true in THEN section, known False
6299 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6301 if Nkind (CV) = N_If_Statement then
6303 -- Before start of IF statement
6305 if Loc < Sloc (CV) then
6306 return;
6308 -- After end of IF statement
6310 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6311 return;
6312 end if;
6314 -- At this stage we know that we are within the IF statement, but
6315 -- unfortunately, the tree does not record the SLOC of the ELSE so
6316 -- we cannot use a simple SLOC comparison to distinguish between
6317 -- the then/else statements, so we have to climb the tree.
6319 declare
6320 N : Node_Id;
6322 begin
6323 N := Parent (Var);
6324 while Parent (N) /= CV loop
6325 N := Parent (N);
6327 -- If we fall off the top of the tree, then that's odd, but
6328 -- perhaps it could occur in some error situation, and the
6329 -- safest response is simply to assume that the outcome of
6330 -- the condition is unknown. No point in bombing during an
6331 -- attempt to optimize things.
6333 if No (N) then
6334 return;
6335 end if;
6336 end loop;
6338 -- Now we have N pointing to a node whose parent is the IF
6339 -- statement in question, so now we can tell if we are within
6340 -- the THEN statements.
6342 if Is_List_Member (N)
6343 and then List_Containing (N) = Then_Statements (CV)
6344 then
6345 Sens := True;
6347 -- If the variable reference does not come from source, we
6348 -- cannot reliably tell whether it appears in the else part.
6349 -- In particular, if it appears in generated code for a node
6350 -- that requires finalization, it may be attached to a list
6351 -- that has not been yet inserted into the code. For now,
6352 -- treat it as unknown.
6354 elsif not Comes_From_Source (N) then
6355 return;
6357 -- Otherwise we must be in ELSIF or ELSE part
6359 else
6360 Sens := False;
6361 end if;
6362 end;
6364 -- ELSIF part. Condition is known true within the referenced
6365 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6366 -- and unknown before the ELSE part or after the IF statement.
6368 elsif Nkind (CV) = N_Elsif_Part then
6370 -- if the Elsif_Part had condition_actions, the elsif has been
6371 -- rewritten as a nested if, and the original elsif_part is
6372 -- detached from the tree, so there is no way to obtain useful
6373 -- information on the current value of the variable.
6374 -- Can this be improved ???
6376 if No (Parent (CV)) then
6377 return;
6378 end if;
6380 Stm := Parent (CV);
6382 -- If the tree has been otherwise rewritten there is nothing
6383 -- else to be done either.
6385 if Nkind (Stm) /= N_If_Statement then
6386 return;
6387 end if;
6389 -- Before start of ELSIF part
6391 if Loc < Sloc (CV) then
6392 return;
6394 -- After end of IF statement
6396 elsif Loc >= Sloc (Stm) +
6397 Text_Ptr (UI_To_Int (End_Span (Stm)))
6398 then
6399 return;
6400 end if;
6402 -- Again we lack the SLOC of the ELSE, so we need to climb the
6403 -- tree to see if we are within the ELSIF part in question.
6405 declare
6406 N : Node_Id;
6408 begin
6409 N := Parent (Var);
6410 while Parent (N) /= Stm loop
6411 N := Parent (N);
6413 -- If we fall off the top of the tree, then that's odd, but
6414 -- perhaps it could occur in some error situation, and the
6415 -- safest response is simply to assume that the outcome of
6416 -- the condition is unknown. No point in bombing during an
6417 -- attempt to optimize things.
6419 if No (N) then
6420 return;
6421 end if;
6422 end loop;
6424 -- Now we have N pointing to a node whose parent is the IF
6425 -- statement in question, so see if is the ELSIF part we want.
6426 -- the THEN statements.
6428 if N = CV then
6429 Sens := True;
6431 -- Otherwise we must be in subsequent ELSIF or ELSE part
6433 else
6434 Sens := False;
6435 end if;
6436 end;
6438 -- Iteration scheme of while loop. The condition is known to be
6439 -- true within the body of the loop.
6441 elsif Nkind (CV) = N_Iteration_Scheme then
6442 declare
6443 Loop_Stmt : constant Node_Id := Parent (CV);
6445 begin
6446 -- Before start of body of loop
6448 if Loc < Sloc (Loop_Stmt) then
6449 return;
6451 -- After end of LOOP statement
6453 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6454 return;
6456 -- We are within the body of the loop
6458 else
6459 Sens := True;
6460 end if;
6461 end;
6463 -- All other cases of Current_Value settings
6465 else
6466 return;
6467 end if;
6469 -- If we fall through here, then we have a reportable condition, Sens
6470 -- is True if the condition is true and False if it needs inverting.
6472 Process_Current_Value_Condition (Condition (CV), Sens);
6473 end;
6474 end Get_Current_Value_Condition;
6476 ---------------------
6477 -- Get_Stream_Size --
6478 ---------------------
6480 function Get_Stream_Size (E : Entity_Id) return Uint is
6481 begin
6482 -- If we have a Stream_Size clause for this type use it
6484 if Has_Stream_Size_Clause (E) then
6485 return Static_Integer (Expression (Stream_Size_Clause (E)));
6487 -- Otherwise the Stream_Size if the size of the type
6489 else
6490 return Esize (E);
6491 end if;
6492 end Get_Stream_Size;
6494 ---------------------------
6495 -- Has_Access_Constraint --
6496 ---------------------------
6498 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6499 Disc : Entity_Id;
6500 T : constant Entity_Id := Etype (E);
6502 begin
6503 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6504 Disc := First_Discriminant (T);
6505 while Present (Disc) loop
6506 if Is_Access_Type (Etype (Disc)) then
6507 return True;
6508 end if;
6510 Next_Discriminant (Disc);
6511 end loop;
6513 return False;
6514 else
6515 return False;
6516 end if;
6517 end Has_Access_Constraint;
6519 -----------------------------------------------------
6520 -- Has_Annotate_Pragma_For_External_Axiomatization --
6521 -----------------------------------------------------
6523 function Has_Annotate_Pragma_For_External_Axiomatization
6524 (E : Entity_Id) return Boolean
6526 function Is_Annotate_Pragma_For_External_Axiomatization
6527 (N : Node_Id) return Boolean;
6528 -- Returns whether N is
6529 -- pragma Annotate (GNATprove, External_Axiomatization);
6531 ----------------------------------------------------
6532 -- Is_Annotate_Pragma_For_External_Axiomatization --
6533 ----------------------------------------------------
6535 -- The general form of pragma Annotate is
6537 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6538 -- ARG ::= NAME | EXPRESSION
6540 -- The first two arguments are by convention intended to refer to an
6541 -- external tool and a tool-specific function. These arguments are
6542 -- not analyzed.
6544 -- The following is used to annotate a package specification which
6545 -- GNATprove should treat specially, because the axiomatization of
6546 -- this unit is given by the user instead of being automatically
6547 -- generated.
6549 -- pragma Annotate (GNATprove, External_Axiomatization);
6551 function Is_Annotate_Pragma_For_External_Axiomatization
6552 (N : Node_Id) return Boolean
6554 Name_GNATprove : constant String :=
6555 "gnatprove";
6556 Name_External_Axiomatization : constant String :=
6557 "external_axiomatization";
6558 -- Special names
6560 begin
6561 if Nkind (N) = N_Pragma
6562 and then Get_Pragma_Id (N) = Pragma_Annotate
6563 and then List_Length (Pragma_Argument_Associations (N)) = 2
6564 then
6565 declare
6566 Arg1 : constant Node_Id :=
6567 First (Pragma_Argument_Associations (N));
6568 Arg2 : constant Node_Id := Next (Arg1);
6569 Nam1 : Name_Id;
6570 Nam2 : Name_Id;
6572 begin
6573 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6574 -- Name_External_Axiomatization so that Name_Find returns the
6575 -- corresponding name. This takes care of all possible casings.
6577 Name_Len := 0;
6578 Add_Str_To_Name_Buffer (Name_GNATprove);
6579 Nam1 := Name_Find;
6581 Name_Len := 0;
6582 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6583 Nam2 := Name_Find;
6585 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6586 and then
6587 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6588 end;
6590 else
6591 return False;
6592 end if;
6593 end Is_Annotate_Pragma_For_External_Axiomatization;
6595 -- Local variables
6597 Decl : Node_Id;
6598 Vis_Decls : List_Id;
6599 N : Node_Id;
6601 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6603 begin
6604 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6605 Decl := Parent (Parent (E));
6606 else
6607 Decl := Parent (E);
6608 end if;
6610 Vis_Decls := Visible_Declarations (Decl);
6612 N := First (Vis_Decls);
6613 while Present (N) loop
6615 -- Skip declarations generated by the frontend. Skip all pragmas
6616 -- that are not the desired Annotate pragma. Stop the search on
6617 -- the first non-pragma source declaration.
6619 if Comes_From_Source (N) then
6620 if Nkind (N) = N_Pragma then
6621 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6622 return True;
6623 end if;
6624 else
6625 return False;
6626 end if;
6627 end if;
6629 Next (N);
6630 end loop;
6632 return False;
6633 end Has_Annotate_Pragma_For_External_Axiomatization;
6635 --------------------
6636 -- Homonym_Number --
6637 --------------------
6639 function Homonym_Number (Subp : Entity_Id) return Nat is
6640 Count : Nat;
6641 Hom : Entity_Id;
6643 begin
6644 Count := 1;
6645 Hom := Homonym (Subp);
6646 while Present (Hom) loop
6647 if Scope (Hom) = Scope (Subp) then
6648 Count := Count + 1;
6649 end if;
6651 Hom := Homonym (Hom);
6652 end loop;
6654 return Count;
6655 end Homonym_Number;
6657 -----------------------------------
6658 -- In_Library_Level_Package_Body --
6659 -----------------------------------
6661 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6662 begin
6663 -- First determine whether the entity appears at the library level, then
6664 -- look at the containing unit.
6666 if Is_Library_Level_Entity (Id) then
6667 declare
6668 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6670 begin
6671 return Nkind (Unit (Container)) = N_Package_Body;
6672 end;
6673 end if;
6675 return False;
6676 end In_Library_Level_Package_Body;
6678 ------------------------------
6679 -- In_Unconditional_Context --
6680 ------------------------------
6682 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6683 P : Node_Id;
6685 begin
6686 P := Node;
6687 while Present (P) loop
6688 case Nkind (P) is
6689 when N_Subprogram_Body => return True;
6690 when N_If_Statement => return False;
6691 when N_Loop_Statement => return False;
6692 when N_Case_Statement => return False;
6693 when others => P := Parent (P);
6694 end case;
6695 end loop;
6697 return False;
6698 end In_Unconditional_Context;
6700 -------------------
6701 -- Insert_Action --
6702 -------------------
6704 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6705 begin
6706 if Present (Ins_Action) then
6707 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6708 end if;
6709 end Insert_Action;
6711 -- Version with check(s) suppressed
6713 procedure Insert_Action
6714 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6716 begin
6717 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6718 end Insert_Action;
6720 -------------------------
6721 -- Insert_Action_After --
6722 -------------------------
6724 procedure Insert_Action_After
6725 (Assoc_Node : Node_Id;
6726 Ins_Action : Node_Id)
6728 begin
6729 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6730 end Insert_Action_After;
6732 --------------------
6733 -- Insert_Actions --
6734 --------------------
6736 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6737 N : Node_Id;
6738 P : Node_Id;
6740 Wrapped_Node : Node_Id := Empty;
6742 begin
6743 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6744 return;
6745 end if;
6747 -- Ignore insert of actions from inside default expression (or other
6748 -- similar "spec expression") in the special spec-expression analyze
6749 -- mode. Any insertions at this point have no relevance, since we are
6750 -- only doing the analyze to freeze the types of any static expressions.
6751 -- See section "Handling of Default Expressions" in the spec of package
6752 -- Sem for further details.
6754 if In_Spec_Expression then
6755 return;
6756 end if;
6758 -- If the action derives from stuff inside a record, then the actions
6759 -- are attached to the current scope, to be inserted and analyzed on
6760 -- exit from the scope. The reason for this is that we may also be
6761 -- generating freeze actions at the same time, and they must eventually
6762 -- be elaborated in the correct order.
6764 if Is_Record_Type (Current_Scope)
6765 and then not Is_Frozen (Current_Scope)
6766 then
6767 if No (Scope_Stack.Table
6768 (Scope_Stack.Last).Pending_Freeze_Actions)
6769 then
6770 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6771 Ins_Actions;
6772 else
6773 Append_List
6774 (Ins_Actions,
6775 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6776 end if;
6778 return;
6779 end if;
6781 -- We now intend to climb up the tree to find the right point to
6782 -- insert the actions. We start at Assoc_Node, unless this node is a
6783 -- subexpression in which case we start with its parent. We do this for
6784 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6785 -- itself one of the special nodes like N_And_Then, then we assume that
6786 -- an initial request to insert actions for such a node does not expect
6787 -- the actions to get deposited in the node for later handling when the
6788 -- node is expanded, since clearly the node is being dealt with by the
6789 -- caller. Note that in the subexpression case, N is always the child we
6790 -- came from.
6792 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6793 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6794 -- Procedure calls, and similarly procedure attribute references, are
6795 -- also statements.
6797 if Nkind (Assoc_Node) in N_Subexpr
6798 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6799 or else Etype (Assoc_Node) /= Standard_Void_Type)
6800 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6801 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6802 or else not Is_Procedure_Attribute_Name
6803 (Attribute_Name (Assoc_Node)))
6804 then
6805 N := Assoc_Node;
6806 P := Parent (Assoc_Node);
6808 -- Non-subexpression case. Note that N is initially Empty in this case
6809 -- (N is only guaranteed Non-Empty in the subexpr case).
6811 else
6812 N := Empty;
6813 P := Assoc_Node;
6814 end if;
6816 -- Capture root of the transient scope
6818 if Scope_Is_Transient then
6819 Wrapped_Node := Node_To_Be_Wrapped;
6820 end if;
6822 loop
6823 pragma Assert (Present (P));
6825 -- Make sure that inserted actions stay in the transient scope
6827 if Present (Wrapped_Node) and then N = Wrapped_Node then
6828 Store_Before_Actions_In_Scope (Ins_Actions);
6829 return;
6830 end if;
6832 case Nkind (P) is
6834 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6835 -- in the Actions field of the right operand. They will be moved
6836 -- out further when the AND THEN or OR ELSE operator is expanded.
6837 -- Nothing special needs to be done for the left operand since
6838 -- in that case the actions are executed unconditionally.
6840 when N_Short_Circuit =>
6841 if N = Right_Opnd (P) then
6843 -- We are now going to either append the actions to the
6844 -- actions field of the short-circuit operation. We will
6845 -- also analyze the actions now.
6847 -- This analysis is really too early, the proper thing would
6848 -- be to just park them there now, and only analyze them if
6849 -- we find we really need them, and to it at the proper
6850 -- final insertion point. However attempting to this proved
6851 -- tricky, so for now we just kill current values before and
6852 -- after the analyze call to make sure we avoid peculiar
6853 -- optimizations from this out of order insertion.
6855 Kill_Current_Values;
6857 -- If P has already been expanded, we can't park new actions
6858 -- on it, so we need to expand them immediately, introducing
6859 -- an Expression_With_Actions. N can't be an expression
6860 -- with actions, or else then the actions would have been
6861 -- inserted at an inner level.
6863 if Analyzed (P) then
6864 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6865 Rewrite (N,
6866 Make_Expression_With_Actions (Sloc (N),
6867 Actions => Ins_Actions,
6868 Expression => Relocate_Node (N)));
6869 Analyze_And_Resolve (N);
6871 elsif Present (Actions (P)) then
6872 Insert_List_After_And_Analyze
6873 (Last (Actions (P)), Ins_Actions);
6874 else
6875 Set_Actions (P, Ins_Actions);
6876 Analyze_List (Actions (P));
6877 end if;
6879 Kill_Current_Values;
6881 return;
6882 end if;
6884 -- Then or Else dependent expression of an if expression. Add
6885 -- actions to Then_Actions or Else_Actions field as appropriate.
6886 -- The actions will be moved further out when the if is expanded.
6888 when N_If_Expression =>
6889 declare
6890 ThenX : constant Node_Id := Next (First (Expressions (P)));
6891 ElseX : constant Node_Id := Next (ThenX);
6893 begin
6894 -- If the enclosing expression is already analyzed, as
6895 -- is the case for nested elaboration checks, insert the
6896 -- conditional further out.
6898 if Analyzed (P) then
6899 null;
6901 -- Actions belong to the then expression, temporarily place
6902 -- them as Then_Actions of the if expression. They will be
6903 -- moved to the proper place later when the if expression
6904 -- is expanded.
6906 elsif N = ThenX then
6907 if Present (Then_Actions (P)) then
6908 Insert_List_After_And_Analyze
6909 (Last (Then_Actions (P)), Ins_Actions);
6910 else
6911 Set_Then_Actions (P, Ins_Actions);
6912 Analyze_List (Then_Actions (P));
6913 end if;
6915 return;
6917 -- Actions belong to the else expression, temporarily place
6918 -- them as Else_Actions of the if expression. They will be
6919 -- moved to the proper place later when the if expression
6920 -- is expanded.
6922 elsif N = ElseX then
6923 if Present (Else_Actions (P)) then
6924 Insert_List_After_And_Analyze
6925 (Last (Else_Actions (P)), Ins_Actions);
6926 else
6927 Set_Else_Actions (P, Ins_Actions);
6928 Analyze_List (Else_Actions (P));
6929 end if;
6931 return;
6933 -- Actions belong to the condition. In this case they are
6934 -- unconditionally executed, and so we can continue the
6935 -- search for the proper insert point.
6937 else
6938 null;
6939 end if;
6940 end;
6942 -- Alternative of case expression, we place the action in the
6943 -- Actions field of the case expression alternative, this will
6944 -- be handled when the case expression is expanded.
6946 when N_Case_Expression_Alternative =>
6947 if Present (Actions (P)) then
6948 Insert_List_After_And_Analyze
6949 (Last (Actions (P)), Ins_Actions);
6950 else
6951 Set_Actions (P, Ins_Actions);
6952 Analyze_List (Actions (P));
6953 end if;
6955 return;
6957 -- Case of appearing within an Expressions_With_Actions node. When
6958 -- the new actions come from the expression of the expression with
6959 -- actions, they must be added to the existing actions. The other
6960 -- alternative is when the new actions are related to one of the
6961 -- existing actions of the expression with actions, and should
6962 -- never reach here: if actions are inserted on a statement
6963 -- within the Actions of an expression with actions, or on some
6964 -- subexpression of such a statement, then the outermost proper
6965 -- insertion point is right before the statement, and we should
6966 -- never climb up as far as the N_Expression_With_Actions itself.
6968 when N_Expression_With_Actions =>
6969 if N = Expression (P) then
6970 if Is_Empty_List (Actions (P)) then
6971 Append_List_To (Actions (P), Ins_Actions);
6972 Analyze_List (Actions (P));
6973 else
6974 Insert_List_After_And_Analyze
6975 (Last (Actions (P)), Ins_Actions);
6976 end if;
6978 return;
6980 else
6981 raise Program_Error;
6982 end if;
6984 -- Case of appearing in the condition of a while expression or
6985 -- elsif. We insert the actions into the Condition_Actions field.
6986 -- They will be moved further out when the while loop or elsif
6987 -- is analyzed.
6989 when N_Elsif_Part
6990 | N_Iteration_Scheme
6992 if N = Condition (P) then
6993 if Present (Condition_Actions (P)) then
6994 Insert_List_After_And_Analyze
6995 (Last (Condition_Actions (P)), Ins_Actions);
6996 else
6997 Set_Condition_Actions (P, Ins_Actions);
6999 -- Set the parent of the insert actions explicitly. This
7000 -- is not a syntactic field, but we need the parent field
7001 -- set, in particular so that freeze can understand that
7002 -- it is dealing with condition actions, and properly
7003 -- insert the freezing actions.
7005 Set_Parent (Ins_Actions, P);
7006 Analyze_List (Condition_Actions (P));
7007 end if;
7009 return;
7010 end if;
7012 -- Statements, declarations, pragmas, representation clauses
7014 when
7015 -- Statements
7017 N_Procedure_Call_Statement
7018 | N_Statement_Other_Than_Procedure_Call
7020 -- Pragmas
7022 | N_Pragma
7024 -- Representation_Clause
7026 | N_At_Clause
7027 | N_Attribute_Definition_Clause
7028 | N_Enumeration_Representation_Clause
7029 | N_Record_Representation_Clause
7031 -- Declarations
7033 | N_Abstract_Subprogram_Declaration
7034 | N_Entry_Body
7035 | N_Exception_Declaration
7036 | N_Exception_Renaming_Declaration
7037 | N_Expression_Function
7038 | N_Formal_Abstract_Subprogram_Declaration
7039 | N_Formal_Concrete_Subprogram_Declaration
7040 | N_Formal_Object_Declaration
7041 | N_Formal_Type_Declaration
7042 | N_Full_Type_Declaration
7043 | N_Function_Instantiation
7044 | N_Generic_Function_Renaming_Declaration
7045 | N_Generic_Package_Declaration
7046 | N_Generic_Package_Renaming_Declaration
7047 | N_Generic_Procedure_Renaming_Declaration
7048 | N_Generic_Subprogram_Declaration
7049 | N_Implicit_Label_Declaration
7050 | N_Incomplete_Type_Declaration
7051 | N_Number_Declaration
7052 | N_Object_Declaration
7053 | N_Object_Renaming_Declaration
7054 | N_Package_Body
7055 | N_Package_Body_Stub
7056 | N_Package_Declaration
7057 | N_Package_Instantiation
7058 | N_Package_Renaming_Declaration
7059 | N_Private_Extension_Declaration
7060 | N_Private_Type_Declaration
7061 | N_Procedure_Instantiation
7062 | N_Protected_Body
7063 | N_Protected_Body_Stub
7064 | N_Protected_Type_Declaration
7065 | N_Single_Task_Declaration
7066 | N_Subprogram_Body
7067 | N_Subprogram_Body_Stub
7068 | N_Subprogram_Declaration
7069 | N_Subprogram_Renaming_Declaration
7070 | N_Subtype_Declaration
7071 | N_Task_Body
7072 | N_Task_Body_Stub
7073 | N_Task_Type_Declaration
7075 -- Use clauses can appear in lists of declarations
7077 | N_Use_Package_Clause
7078 | N_Use_Type_Clause
7080 -- Freeze entity behaves like a declaration or statement
7082 | N_Freeze_Entity
7083 | N_Freeze_Generic_Entity
7085 -- Do not insert here if the item is not a list member (this
7086 -- happens for example with a triggering statement, and the
7087 -- proper approach is to insert before the entire select).
7089 if not Is_List_Member (P) then
7090 null;
7092 -- Do not insert if parent of P is an N_Component_Association
7093 -- node (i.e. we are in the context of an N_Aggregate or
7094 -- N_Extension_Aggregate node. In this case we want to insert
7095 -- before the entire aggregate.
7097 elsif Nkind (Parent (P)) = N_Component_Association then
7098 null;
7100 -- Do not insert if the parent of P is either an N_Variant node
7101 -- or an N_Record_Definition node, meaning in either case that
7102 -- P is a member of a component list, and that therefore the
7103 -- actions should be inserted outside the complete record
7104 -- declaration.
7106 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7107 null;
7109 -- Do not insert freeze nodes within the loop generated for
7110 -- an aggregate, because they may be elaborated too late for
7111 -- subsequent use in the back end: within a package spec the
7112 -- loop is part of the elaboration procedure and is only
7113 -- elaborated during the second pass.
7115 -- If the loop comes from source, or the entity is local to the
7116 -- loop itself it must remain within.
7118 elsif Nkind (Parent (P)) = N_Loop_Statement
7119 and then not Comes_From_Source (Parent (P))
7120 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7121 and then
7122 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7123 then
7124 null;
7126 -- Otherwise we can go ahead and do the insertion
7128 elsif P = Wrapped_Node then
7129 Store_Before_Actions_In_Scope (Ins_Actions);
7130 return;
7132 else
7133 Insert_List_Before_And_Analyze (P, Ins_Actions);
7134 return;
7135 end if;
7137 -- A special case, N_Raise_xxx_Error can act either as a statement
7138 -- or a subexpression. We tell the difference by looking at the
7139 -- Etype. It is set to Standard_Void_Type in the statement case.
7141 when N_Raise_xxx_Error =>
7142 if Etype (P) = Standard_Void_Type then
7143 if P = Wrapped_Node then
7144 Store_Before_Actions_In_Scope (Ins_Actions);
7145 else
7146 Insert_List_Before_And_Analyze (P, Ins_Actions);
7147 end if;
7149 return;
7151 -- In the subexpression case, keep climbing
7153 else
7154 null;
7155 end if;
7157 -- If a component association appears within a loop created for
7158 -- an array aggregate, attach the actions to the association so
7159 -- they can be subsequently inserted within the loop. For other
7160 -- component associations insert outside of the aggregate. For
7161 -- an association that will generate a loop, its Loop_Actions
7162 -- attribute is already initialized (see exp_aggr.adb).
7164 -- The list of Loop_Actions can in turn generate additional ones,
7165 -- that are inserted before the associated node. If the associated
7166 -- node is outside the aggregate, the new actions are collected
7167 -- at the end of the Loop_Actions, to respect the order in which
7168 -- they are to be elaborated.
7170 when N_Component_Association
7171 | N_Iterated_Component_Association
7173 if Nkind (Parent (P)) = N_Aggregate
7174 and then Present (Loop_Actions (P))
7175 then
7176 if Is_Empty_List (Loop_Actions (P)) then
7177 Set_Loop_Actions (P, Ins_Actions);
7178 Analyze_List (Ins_Actions);
7179 else
7180 declare
7181 Decl : Node_Id;
7183 begin
7184 -- Check whether these actions were generated by a
7185 -- declaration that is part of the Loop_Actions for
7186 -- the component_association.
7188 Decl := Assoc_Node;
7189 while Present (Decl) loop
7190 exit when Parent (Decl) = P
7191 and then Is_List_Member (Decl)
7192 and then
7193 List_Containing (Decl) = Loop_Actions (P);
7194 Decl := Parent (Decl);
7195 end loop;
7197 if Present (Decl) then
7198 Insert_List_Before_And_Analyze
7199 (Decl, Ins_Actions);
7200 else
7201 Insert_List_After_And_Analyze
7202 (Last (Loop_Actions (P)), Ins_Actions);
7203 end if;
7204 end;
7205 end if;
7207 return;
7209 else
7210 null;
7211 end if;
7213 -- Another special case, an attribute denoting a procedure call
7215 when N_Attribute_Reference =>
7216 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7217 if P = Wrapped_Node then
7218 Store_Before_Actions_In_Scope (Ins_Actions);
7219 else
7220 Insert_List_Before_And_Analyze (P, Ins_Actions);
7221 end if;
7223 return;
7225 -- In the subexpression case, keep climbing
7227 else
7228 null;
7229 end if;
7231 -- A contract node should not belong to the tree
7233 when N_Contract =>
7234 raise Program_Error;
7236 -- For all other node types, keep climbing tree
7238 when N_Abortable_Part
7239 | N_Accept_Alternative
7240 | N_Access_Definition
7241 | N_Access_Function_Definition
7242 | N_Access_Procedure_Definition
7243 | N_Access_To_Object_Definition
7244 | N_Aggregate
7245 | N_Allocator
7246 | N_Aspect_Specification
7247 | N_Case_Expression
7248 | N_Case_Statement_Alternative
7249 | N_Character_Literal
7250 | N_Compilation_Unit
7251 | N_Compilation_Unit_Aux
7252 | N_Component_Clause
7253 | N_Component_Declaration
7254 | N_Component_Definition
7255 | N_Component_List
7256 | N_Constrained_Array_Definition
7257 | N_Decimal_Fixed_Point_Definition
7258 | N_Defining_Character_Literal
7259 | N_Defining_Identifier
7260 | N_Defining_Operator_Symbol
7261 | N_Defining_Program_Unit_Name
7262 | N_Delay_Alternative
7263 | N_Delta_Aggregate
7264 | N_Delta_Constraint
7265 | N_Derived_Type_Definition
7266 | N_Designator
7267 | N_Digits_Constraint
7268 | N_Discriminant_Association
7269 | N_Discriminant_Specification
7270 | N_Empty
7271 | N_Entry_Body_Formal_Part
7272 | N_Entry_Call_Alternative
7273 | N_Entry_Declaration
7274 | N_Entry_Index_Specification
7275 | N_Enumeration_Type_Definition
7276 | N_Error
7277 | N_Exception_Handler
7278 | N_Expanded_Name
7279 | N_Explicit_Dereference
7280 | N_Extension_Aggregate
7281 | N_Floating_Point_Definition
7282 | N_Formal_Decimal_Fixed_Point_Definition
7283 | N_Formal_Derived_Type_Definition
7284 | N_Formal_Discrete_Type_Definition
7285 | N_Formal_Floating_Point_Definition
7286 | N_Formal_Modular_Type_Definition
7287 | N_Formal_Ordinary_Fixed_Point_Definition
7288 | N_Formal_Package_Declaration
7289 | N_Formal_Private_Type_Definition
7290 | N_Formal_Incomplete_Type_Definition
7291 | N_Formal_Signed_Integer_Type_Definition
7292 | N_Function_Call
7293 | N_Function_Specification
7294 | N_Generic_Association
7295 | N_Handled_Sequence_Of_Statements
7296 | N_Identifier
7297 | N_In
7298 | N_Index_Or_Discriminant_Constraint
7299 | N_Indexed_Component
7300 | N_Integer_Literal
7301 | N_Iterator_Specification
7302 | N_Itype_Reference
7303 | N_Label
7304 | N_Loop_Parameter_Specification
7305 | N_Mod_Clause
7306 | N_Modular_Type_Definition
7307 | N_Not_In
7308 | N_Null
7309 | N_Op_Abs
7310 | N_Op_Add
7311 | N_Op_And
7312 | N_Op_Concat
7313 | N_Op_Divide
7314 | N_Op_Eq
7315 | N_Op_Expon
7316 | N_Op_Ge
7317 | N_Op_Gt
7318 | N_Op_Le
7319 | N_Op_Lt
7320 | N_Op_Minus
7321 | N_Op_Mod
7322 | N_Op_Multiply
7323 | N_Op_Ne
7324 | N_Op_Not
7325 | N_Op_Or
7326 | N_Op_Plus
7327 | N_Op_Rem
7328 | N_Op_Rotate_Left
7329 | N_Op_Rotate_Right
7330 | N_Op_Shift_Left
7331 | N_Op_Shift_Right
7332 | N_Op_Shift_Right_Arithmetic
7333 | N_Op_Subtract
7334 | N_Op_Xor
7335 | N_Operator_Symbol
7336 | N_Ordinary_Fixed_Point_Definition
7337 | N_Others_Choice
7338 | N_Package_Specification
7339 | N_Parameter_Association
7340 | N_Parameter_Specification
7341 | N_Pop_Constraint_Error_Label
7342 | N_Pop_Program_Error_Label
7343 | N_Pop_Storage_Error_Label
7344 | N_Pragma_Argument_Association
7345 | N_Procedure_Specification
7346 | N_Protected_Definition
7347 | N_Push_Constraint_Error_Label
7348 | N_Push_Program_Error_Label
7349 | N_Push_Storage_Error_Label
7350 | N_Qualified_Expression
7351 | N_Quantified_Expression
7352 | N_Raise_Expression
7353 | N_Range
7354 | N_Range_Constraint
7355 | N_Real_Literal
7356 | N_Real_Range_Specification
7357 | N_Record_Definition
7358 | N_Reference
7359 | N_SCIL_Dispatch_Table_Tag_Init
7360 | N_SCIL_Dispatching_Call
7361 | N_SCIL_Membership_Test
7362 | N_Selected_Component
7363 | N_Signed_Integer_Type_Definition
7364 | N_Single_Protected_Declaration
7365 | N_Slice
7366 | N_String_Literal
7367 | N_Subtype_Indication
7368 | N_Subunit
7369 | N_Target_Name
7370 | N_Task_Definition
7371 | N_Terminate_Alternative
7372 | N_Triggering_Alternative
7373 | N_Type_Conversion
7374 | N_Unchecked_Expression
7375 | N_Unchecked_Type_Conversion
7376 | N_Unconstrained_Array_Definition
7377 | N_Unused_At_End
7378 | N_Unused_At_Start
7379 | N_Variant
7380 | N_Variant_Part
7381 | N_Validate_Unchecked_Conversion
7382 | N_With_Clause
7384 null;
7385 end case;
7387 -- If we fall through above tests, keep climbing tree
7389 N := P;
7391 if Nkind (Parent (N)) = N_Subunit then
7393 -- This is the proper body corresponding to a stub. Insertion must
7394 -- be done at the point of the stub, which is in the declarative
7395 -- part of the parent unit.
7397 P := Corresponding_Stub (Parent (N));
7399 else
7400 P := Parent (N);
7401 end if;
7402 end loop;
7403 end Insert_Actions;
7405 -- Version with check(s) suppressed
7407 procedure Insert_Actions
7408 (Assoc_Node : Node_Id;
7409 Ins_Actions : List_Id;
7410 Suppress : Check_Id)
7412 begin
7413 if Suppress = All_Checks then
7414 declare
7415 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7416 begin
7417 Scope_Suppress.Suppress := (others => True);
7418 Insert_Actions (Assoc_Node, Ins_Actions);
7419 Scope_Suppress.Suppress := Sva;
7420 end;
7422 else
7423 declare
7424 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7425 begin
7426 Scope_Suppress.Suppress (Suppress) := True;
7427 Insert_Actions (Assoc_Node, Ins_Actions);
7428 Scope_Suppress.Suppress (Suppress) := Svg;
7429 end;
7430 end if;
7431 end Insert_Actions;
7433 --------------------------
7434 -- Insert_Actions_After --
7435 --------------------------
7437 procedure Insert_Actions_After
7438 (Assoc_Node : Node_Id;
7439 Ins_Actions : List_Id)
7441 begin
7442 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7443 Store_After_Actions_In_Scope (Ins_Actions);
7444 else
7445 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7446 end if;
7447 end Insert_Actions_After;
7449 ------------------------
7450 -- Insert_Declaration --
7451 ------------------------
7453 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7454 P : Node_Id;
7456 begin
7457 pragma Assert (Nkind (N) in N_Subexpr);
7459 -- Climb until we find a procedure or a package
7461 P := N;
7462 loop
7463 pragma Assert (Present (Parent (P)));
7464 P := Parent (P);
7466 if Is_List_Member (P) then
7467 exit when Nkind_In (Parent (P), N_Package_Specification,
7468 N_Subprogram_Body);
7470 -- Special handling for handled sequence of statements, we must
7471 -- insert in the statements not the exception handlers!
7473 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7474 P := First (Statements (Parent (P)));
7475 exit;
7476 end if;
7477 end if;
7478 end loop;
7480 -- Now do the insertion
7482 Insert_Before (P, Decl);
7483 Analyze (Decl);
7484 end Insert_Declaration;
7486 ---------------------------------
7487 -- Insert_Library_Level_Action --
7488 ---------------------------------
7490 procedure Insert_Library_Level_Action (N : Node_Id) is
7491 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7493 begin
7494 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7495 -- And not Main_Unit as previously. If the main unit is a body,
7496 -- the scope needed to analyze the actions is the entity of the
7497 -- corresponding declaration.
7499 if No (Actions (Aux)) then
7500 Set_Actions (Aux, New_List (N));
7501 else
7502 Append (N, Actions (Aux));
7503 end if;
7505 Analyze (N);
7506 Pop_Scope;
7507 end Insert_Library_Level_Action;
7509 ----------------------------------
7510 -- Insert_Library_Level_Actions --
7511 ----------------------------------
7513 procedure Insert_Library_Level_Actions (L : List_Id) is
7514 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7516 begin
7517 if Is_Non_Empty_List (L) then
7518 Push_Scope (Cunit_Entity (Main_Unit));
7519 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7521 if No (Actions (Aux)) then
7522 Set_Actions (Aux, L);
7523 Analyze_List (L);
7524 else
7525 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7526 end if;
7528 Pop_Scope;
7529 end if;
7530 end Insert_Library_Level_Actions;
7532 ----------------------
7533 -- Inside_Init_Proc --
7534 ----------------------
7536 function Inside_Init_Proc return Boolean is
7537 S : Entity_Id;
7539 begin
7540 S := Current_Scope;
7541 while Present (S) and then S /= Standard_Standard loop
7542 if Is_Init_Proc (S) then
7543 return True;
7544 else
7545 S := Scope (S);
7546 end if;
7547 end loop;
7549 return False;
7550 end Inside_Init_Proc;
7552 ----------------------------
7553 -- Is_All_Null_Statements --
7554 ----------------------------
7556 function Is_All_Null_Statements (L : List_Id) return Boolean is
7557 Stm : Node_Id;
7559 begin
7560 Stm := First (L);
7561 while Present (Stm) loop
7562 if Nkind (Stm) /= N_Null_Statement then
7563 return False;
7564 end if;
7566 Next (Stm);
7567 end loop;
7569 return True;
7570 end Is_All_Null_Statements;
7572 --------------------------------------------------
7573 -- Is_Displacement_Of_Object_Or_Function_Result --
7574 --------------------------------------------------
7576 function Is_Displacement_Of_Object_Or_Function_Result
7577 (Obj_Id : Entity_Id) return Boolean
7579 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7580 -- Determine if particular node denotes a controlled function call. The
7581 -- call may have been heavily expanded.
7583 function Is_Displace_Call (N : Node_Id) return Boolean;
7584 -- Determine whether a particular node is a call to Ada.Tags.Displace.
7585 -- The call might be nested within other actions such as conversions.
7587 function Is_Source_Object (N : Node_Id) return Boolean;
7588 -- Determine whether a particular node denotes a source object
7590 ---------------------------------
7591 -- Is_Controlled_Function_Call --
7592 ---------------------------------
7594 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7595 Expr : Node_Id := Original_Node (N);
7597 begin
7598 -- When a function call appears in Object.Operation format, the
7599 -- original representation has several possible forms depending on
7600 -- the availability and form of actual parameters:
7602 -- Obj.Func N_Selected_Component
7603 -- Obj.Func (Actual) N_Indexed_Component
7604 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7605 -- N_Selected_Component
7607 loop
7608 if Nkind (Expr) = N_Function_Call then
7609 Expr := Name (Expr);
7611 -- "Obj.Func (Actual)" case
7613 elsif Nkind (Expr) = N_Indexed_Component then
7614 Expr := Prefix (Expr);
7616 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7618 elsif Nkind (Expr) = N_Selected_Component then
7619 Expr := Selector_Name (Expr);
7621 else
7622 exit;
7623 end if;
7624 end loop;
7626 return
7627 Nkind (Expr) in N_Has_Entity
7628 and then Present (Entity (Expr))
7629 and then Ekind (Entity (Expr)) = E_Function
7630 and then Needs_Finalization (Etype (Entity (Expr)));
7631 end Is_Controlled_Function_Call;
7633 ----------------------
7634 -- Is_Displace_Call --
7635 ----------------------
7637 function Is_Displace_Call (N : Node_Id) return Boolean is
7638 Call : Node_Id := N;
7640 begin
7641 -- Strip various actions which may precede a call to Displace
7643 loop
7644 if Nkind (Call) = N_Explicit_Dereference then
7645 Call := Prefix (Call);
7647 elsif Nkind_In (Call, N_Type_Conversion,
7648 N_Unchecked_Type_Conversion)
7649 then
7650 Call := Expression (Call);
7652 else
7653 exit;
7654 end if;
7655 end loop;
7657 return
7658 Present (Call)
7659 and then Nkind (Call) = N_Function_Call
7660 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7661 end Is_Displace_Call;
7663 ----------------------
7664 -- Is_Source_Object --
7665 ----------------------
7667 function Is_Source_Object (N : Node_Id) return Boolean is
7668 begin
7669 return
7670 Present (N)
7671 and then Nkind (N) in N_Has_Entity
7672 and then Is_Object (Entity (N))
7673 and then Comes_From_Source (N);
7674 end Is_Source_Object;
7676 -- Local variables
7678 Decl : constant Node_Id := Parent (Obj_Id);
7679 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7680 Orig_Decl : constant Node_Id := Original_Node (Decl);
7682 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7684 begin
7685 -- Case 1:
7687 -- Obj : CW_Type := Function_Call (...);
7689 -- rewritten into:
7691 -- Tmp : ... := Function_Call (...)'reference;
7692 -- Obj : CW_Type renames (... Ada.Tags.Displace (Tmp));
7694 -- where the return type of the function and the class-wide type require
7695 -- dispatch table pointer displacement.
7697 -- Case 2:
7699 -- Obj : CW_Type := Src_Obj;
7701 -- rewritten into:
7703 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7705 -- where the type of the source object and the class-wide type require
7706 -- dispatch table pointer displacement.
7708 return
7709 Nkind (Decl) = N_Object_Renaming_Declaration
7710 and then Nkind (Orig_Decl) = N_Object_Declaration
7711 and then Comes_From_Source (Orig_Decl)
7712 and then Is_Class_Wide_Type (Obj_Typ)
7713 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7714 and then
7715 (Is_Controlled_Function_Call (Expression (Orig_Decl))
7716 or else Is_Source_Object (Expression (Orig_Decl)));
7717 end Is_Displacement_Of_Object_Or_Function_Result;
7719 ------------------------------
7720 -- Is_Finalizable_Transient --
7721 ------------------------------
7723 function Is_Finalizable_Transient
7724 (Decl : Node_Id;
7725 Rel_Node : Node_Id) return Boolean
7727 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7728 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7730 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7731 -- Determine whether transient object Trans_Id is initialized either
7732 -- by a function call which returns an access type or simply renames
7733 -- another pointer.
7735 function Initialized_By_Aliased_BIP_Func_Call
7736 (Trans_Id : Entity_Id) return Boolean;
7737 -- Determine whether transient object Trans_Id is initialized by a
7738 -- build-in-place function call where the BIPalloc parameter is of
7739 -- value 1 and BIPaccess is not null. This case creates an aliasing
7740 -- between the returned value and the value denoted by BIPaccess.
7742 function Is_Aliased
7743 (Trans_Id : Entity_Id;
7744 First_Stmt : Node_Id) return Boolean;
7745 -- Determine whether transient object Trans_Id has been renamed or
7746 -- aliased through 'reference in the statement list starting from
7747 -- First_Stmt.
7749 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7750 -- Determine whether transient object Trans_Id is allocated on the heap
7752 function Is_Iterated_Container
7753 (Trans_Id : Entity_Id;
7754 First_Stmt : Node_Id) return Boolean;
7755 -- Determine whether transient object Trans_Id denotes a container which
7756 -- is in the process of being iterated in the statement list starting
7757 -- from First_Stmt.
7759 ---------------------------
7760 -- Initialized_By_Access --
7761 ---------------------------
7763 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7764 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7766 begin
7767 return
7768 Present (Expr)
7769 and then Nkind (Expr) /= N_Reference
7770 and then Is_Access_Type (Etype (Expr));
7771 end Initialized_By_Access;
7773 ------------------------------------------
7774 -- Initialized_By_Aliased_BIP_Func_Call --
7775 ------------------------------------------
7777 function Initialized_By_Aliased_BIP_Func_Call
7778 (Trans_Id : Entity_Id) return Boolean
7780 Call : Node_Id := Expression (Parent (Trans_Id));
7782 begin
7783 -- Build-in-place calls usually appear in 'reference format
7785 if Nkind (Call) = N_Reference then
7786 Call := Prefix (Call);
7787 end if;
7789 if Is_Build_In_Place_Function_Call (Call) then
7790 declare
7791 Access_Nam : Name_Id := No_Name;
7792 Access_OK : Boolean := False;
7793 Actual : Node_Id;
7794 Alloc_Nam : Name_Id := No_Name;
7795 Alloc_OK : Boolean := False;
7796 Formal : Node_Id;
7797 Func_Id : Entity_Id;
7798 Param : Node_Id;
7800 begin
7801 -- Examine all parameter associations of the function call
7803 Param := First (Parameter_Associations (Call));
7804 while Present (Param) loop
7805 if Nkind (Param) = N_Parameter_Association
7806 and then Nkind (Selector_Name (Param)) = N_Identifier
7807 then
7808 Actual := Explicit_Actual_Parameter (Param);
7809 Formal := Selector_Name (Param);
7811 -- Construct the names of formals BIPaccess and BIPalloc
7812 -- using the function name retrieved from an arbitrary
7813 -- formal.
7815 if Access_Nam = No_Name
7816 and then Alloc_Nam = No_Name
7817 and then Present (Entity (Formal))
7818 then
7819 Func_Id := Scope (Entity (Formal));
7821 Access_Nam :=
7822 New_External_Name (Chars (Func_Id),
7823 BIP_Formal_Suffix (BIP_Object_Access));
7825 Alloc_Nam :=
7826 New_External_Name (Chars (Func_Id),
7827 BIP_Formal_Suffix (BIP_Alloc_Form));
7828 end if;
7830 -- A match for BIPaccess => Temp has been found
7832 if Chars (Formal) = Access_Nam
7833 and then Nkind (Actual) /= N_Null
7834 then
7835 Access_OK := True;
7836 end if;
7838 -- A match for BIPalloc => 1 has been found
7840 if Chars (Formal) = Alloc_Nam
7841 and then Nkind (Actual) = N_Integer_Literal
7842 and then Intval (Actual) = Uint_1
7843 then
7844 Alloc_OK := True;
7845 end if;
7846 end if;
7848 Next (Param);
7849 end loop;
7851 return Access_OK and Alloc_OK;
7852 end;
7853 end if;
7855 return False;
7856 end Initialized_By_Aliased_BIP_Func_Call;
7858 ----------------
7859 -- Is_Aliased --
7860 ----------------
7862 function Is_Aliased
7863 (Trans_Id : Entity_Id;
7864 First_Stmt : Node_Id) return Boolean
7866 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7867 -- Given an object renaming declaration, retrieve the entity of the
7868 -- renamed name. Return Empty if the renamed name is anything other
7869 -- than a variable or a constant.
7871 -------------------------
7872 -- Find_Renamed_Object --
7873 -------------------------
7875 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7876 Ren_Obj : Node_Id := Empty;
7878 function Find_Object (N : Node_Id) return Traverse_Result;
7879 -- Try to detect an object which is either a constant or a
7880 -- variable.
7882 -----------------
7883 -- Find_Object --
7884 -----------------
7886 function Find_Object (N : Node_Id) return Traverse_Result is
7887 begin
7888 -- Stop the search once a constant or a variable has been
7889 -- detected.
7891 if Nkind (N) = N_Identifier
7892 and then Present (Entity (N))
7893 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7894 then
7895 Ren_Obj := Entity (N);
7896 return Abandon;
7897 end if;
7899 return OK;
7900 end Find_Object;
7902 procedure Search is new Traverse_Proc (Find_Object);
7904 -- Local variables
7906 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7908 -- Start of processing for Find_Renamed_Object
7910 begin
7911 -- Actions related to dispatching calls may appear as renamings of
7912 -- tags. Do not process this type of renaming because it does not
7913 -- use the actual value of the object.
7915 if not Is_RTE (Typ, RE_Tag_Ptr) then
7916 Search (Name (Ren_Decl));
7917 end if;
7919 return Ren_Obj;
7920 end Find_Renamed_Object;
7922 -- Local variables
7924 Expr : Node_Id;
7925 Ren_Obj : Entity_Id;
7926 Stmt : Node_Id;
7928 -- Start of processing for Is_Aliased
7930 begin
7931 -- A controlled transient object is not considered aliased when it
7932 -- appears inside an expression_with_actions node even when there are
7933 -- explicit aliases of it:
7935 -- do
7936 -- Trans_Id : Ctrl_Typ ...; -- transient object
7937 -- Alias : ... := Trans_Id; -- object is aliased
7938 -- Val : constant Boolean :=
7939 -- ... Alias ...; -- aliasing ends
7940 -- <finalize Trans_Id> -- object safe to finalize
7941 -- in Val end;
7943 -- Expansion ensures that all aliases are encapsulated in the actions
7944 -- list and do not leak to the expression by forcing the evaluation
7945 -- of the expression.
7947 if Nkind (Rel_Node) = N_Expression_With_Actions then
7948 return False;
7950 -- Otherwise examine the statements after the controlled transient
7951 -- object and look for various forms of aliasing.
7953 else
7954 Stmt := First_Stmt;
7955 while Present (Stmt) loop
7956 if Nkind (Stmt) = N_Object_Declaration then
7957 Expr := Expression (Stmt);
7959 -- Aliasing of the form:
7960 -- Obj : ... := Trans_Id'reference;
7962 if Present (Expr)
7963 and then Nkind (Expr) = N_Reference
7964 and then Nkind (Prefix (Expr)) = N_Identifier
7965 and then Entity (Prefix (Expr)) = Trans_Id
7966 then
7967 return True;
7968 end if;
7970 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
7971 Ren_Obj := Find_Renamed_Object (Stmt);
7973 -- Aliasing of the form:
7974 -- Obj : ... renames ... Trans_Id ...;
7976 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
7977 return True;
7978 end if;
7979 end if;
7981 Next (Stmt);
7982 end loop;
7984 return False;
7985 end if;
7986 end Is_Aliased;
7988 ------------------
7989 -- Is_Allocated --
7990 ------------------
7992 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
7993 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7994 begin
7995 return
7996 Is_Access_Type (Etype (Trans_Id))
7997 and then Present (Expr)
7998 and then Nkind (Expr) = N_Allocator;
7999 end Is_Allocated;
8001 ---------------------------
8002 -- Is_Iterated_Container --
8003 ---------------------------
8005 function Is_Iterated_Container
8006 (Trans_Id : Entity_Id;
8007 First_Stmt : Node_Id) return Boolean
8009 Aspect : Node_Id;
8010 Call : Node_Id;
8011 Iter : Entity_Id;
8012 Param : Node_Id;
8013 Stmt : Node_Id;
8014 Typ : Entity_Id;
8016 begin
8017 -- It is not possible to iterate over containers in non-Ada 2012 code
8019 if Ada_Version < Ada_2012 then
8020 return False;
8021 end if;
8023 Typ := Etype (Trans_Id);
8025 -- Handle access type created for secondary stack use
8027 if Is_Access_Type (Typ) then
8028 Typ := Designated_Type (Typ);
8029 end if;
8031 -- Look for aspect Default_Iterator. It may be part of a type
8032 -- declaration for a container, or inherited from a base type
8033 -- or parent type.
8035 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8037 if Present (Aspect) then
8038 Iter := Entity (Aspect);
8040 -- Examine the statements following the container object and
8041 -- look for a call to the default iterate routine where the
8042 -- first parameter is the transient. Such a call appears as:
8044 -- It : Access_To_CW_Iterator :=
8045 -- Iterate (Tran_Id.all, ...)'reference;
8047 Stmt := First_Stmt;
8048 while Present (Stmt) loop
8050 -- Detect an object declaration which is initialized by a
8051 -- secondary stack function call.
8053 if Nkind (Stmt) = N_Object_Declaration
8054 and then Present (Expression (Stmt))
8055 and then Nkind (Expression (Stmt)) = N_Reference
8056 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8057 then
8058 Call := Prefix (Expression (Stmt));
8060 -- The call must invoke the default iterate routine of
8061 -- the container and the transient object must appear as
8062 -- the first actual parameter. Skip any calls whose names
8063 -- are not entities.
8065 if Is_Entity_Name (Name (Call))
8066 and then Entity (Name (Call)) = Iter
8067 and then Present (Parameter_Associations (Call))
8068 then
8069 Param := First (Parameter_Associations (Call));
8071 if Nkind (Param) = N_Explicit_Dereference
8072 and then Entity (Prefix (Param)) = Trans_Id
8073 then
8074 return True;
8075 end if;
8076 end if;
8077 end if;
8079 Next (Stmt);
8080 end loop;
8081 end if;
8083 return False;
8084 end Is_Iterated_Container;
8086 -- Local variables
8088 Desig : Entity_Id := Obj_Typ;
8090 -- Start of processing for Is_Finalizable_Transient
8092 begin
8093 -- Handle access types
8095 if Is_Access_Type (Desig) then
8096 Desig := Available_View (Designated_Type (Desig));
8097 end if;
8099 return
8100 Ekind_In (Obj_Id, E_Constant, E_Variable)
8101 and then Needs_Finalization (Desig)
8102 and then Requires_Transient_Scope (Desig)
8103 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8105 -- Do not consider a transient object that was already processed
8107 and then not Is_Finalized_Transient (Obj_Id)
8109 -- Do not consider renamed or 'reference-d transient objects because
8110 -- the act of renaming extends the object's lifetime.
8112 and then not Is_Aliased (Obj_Id, Decl)
8114 -- Do not consider transient objects allocated on the heap since
8115 -- they are attached to a finalization master.
8117 and then not Is_Allocated (Obj_Id)
8119 -- If the transient object is a pointer, check that it is not
8120 -- initialized by a function that returns a pointer or acts as a
8121 -- renaming of another pointer.
8123 and then
8124 (not Is_Access_Type (Obj_Typ)
8125 or else not Initialized_By_Access (Obj_Id))
8127 -- Do not consider transient objects which act as indirect aliases
8128 -- of build-in-place function results.
8130 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8132 -- Do not consider conversions of tags to class-wide types
8134 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8136 -- Do not consider iterators because those are treated as normal
8137 -- controlled objects and are processed by the usual finalization
8138 -- machinery. This avoids the double finalization of an iterator.
8140 and then not Is_Iterator (Desig)
8142 -- Do not consider containers in the context of iterator loops. Such
8143 -- transient objects must exist for as long as the loop is around,
8144 -- otherwise any operation carried out by the iterator will fail.
8146 and then not Is_Iterated_Container (Obj_Id, Decl);
8147 end Is_Finalizable_Transient;
8149 ---------------------------------
8150 -- Is_Fully_Repped_Tagged_Type --
8151 ---------------------------------
8153 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8154 U : constant Entity_Id := Underlying_Type (T);
8155 Comp : Entity_Id;
8157 begin
8158 if No (U) or else not Is_Tagged_Type (U) then
8159 return False;
8160 elsif Has_Discriminants (U) then
8161 return False;
8162 elsif not Has_Specified_Layout (U) then
8163 return False;
8164 end if;
8166 -- Here we have a tagged type, see if it has any unlayed out fields
8167 -- other than a possible tag and parent fields. If so, we return False.
8169 Comp := First_Component (U);
8170 while Present (Comp) loop
8171 if not Is_Tag (Comp)
8172 and then Chars (Comp) /= Name_uParent
8173 and then No (Component_Clause (Comp))
8174 then
8175 return False;
8176 else
8177 Next_Component (Comp);
8178 end if;
8179 end loop;
8181 -- All components are layed out
8183 return True;
8184 end Is_Fully_Repped_Tagged_Type;
8186 ----------------------------------
8187 -- Is_Library_Level_Tagged_Type --
8188 ----------------------------------
8190 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8191 begin
8192 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8193 end Is_Library_Level_Tagged_Type;
8195 --------------------------
8196 -- Is_Non_BIP_Func_Call --
8197 --------------------------
8199 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8200 begin
8201 -- The expected call is of the format
8203 -- Func_Call'reference
8205 return
8206 Nkind (Expr) = N_Reference
8207 and then Nkind (Prefix (Expr)) = N_Function_Call
8208 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8209 end Is_Non_BIP_Func_Call;
8211 ------------------------------------
8212 -- Is_Object_Access_BIP_Func_Call --
8213 ------------------------------------
8215 function Is_Object_Access_BIP_Func_Call
8216 (Expr : Node_Id;
8217 Obj_Id : Entity_Id) return Boolean
8219 Access_Nam : Name_Id := No_Name;
8220 Actual : Node_Id;
8221 Call : Node_Id;
8222 Formal : Node_Id;
8223 Param : Node_Id;
8225 begin
8226 -- Build-in-place calls usually appear in 'reference format. Note that
8227 -- the accessibility check machinery may add an extra 'reference due to
8228 -- side effect removal.
8230 Call := Expr;
8231 while Nkind (Call) = N_Reference loop
8232 Call := Prefix (Call);
8233 end loop;
8235 if Nkind_In (Call, N_Qualified_Expression,
8236 N_Unchecked_Type_Conversion)
8237 then
8238 Call := Expression (Call);
8239 end if;
8241 if Is_Build_In_Place_Function_Call (Call) then
8243 -- Examine all parameter associations of the function call
8245 Param := First (Parameter_Associations (Call));
8246 while Present (Param) loop
8247 if Nkind (Param) = N_Parameter_Association
8248 and then Nkind (Selector_Name (Param)) = N_Identifier
8249 then
8250 Formal := Selector_Name (Param);
8251 Actual := Explicit_Actual_Parameter (Param);
8253 -- Construct the name of formal BIPaccess. It is much easier to
8254 -- extract the name of the function using an arbitrary formal's
8255 -- scope rather than the Name field of Call.
8257 if Access_Nam = No_Name and then Present (Entity (Formal)) then
8258 Access_Nam :=
8259 New_External_Name
8260 (Chars (Scope (Entity (Formal))),
8261 BIP_Formal_Suffix (BIP_Object_Access));
8262 end if;
8264 -- A match for BIPaccess => Obj_Id'Unrestricted_Access has been
8265 -- found.
8267 if Chars (Formal) = Access_Nam
8268 and then Nkind (Actual) = N_Attribute_Reference
8269 and then Attribute_Name (Actual) = Name_Unrestricted_Access
8270 and then Nkind (Prefix (Actual)) = N_Identifier
8271 and then Entity (Prefix (Actual)) = Obj_Id
8272 then
8273 return True;
8274 end if;
8275 end if;
8277 Next (Param);
8278 end loop;
8279 end if;
8281 return False;
8282 end Is_Object_Access_BIP_Func_Call;
8284 ----------------------------------
8285 -- Is_Possibly_Unaligned_Object --
8286 ----------------------------------
8288 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8289 T : constant Entity_Id := Etype (N);
8291 begin
8292 -- If renamed object, apply test to underlying object
8294 if Is_Entity_Name (N)
8295 and then Is_Object (Entity (N))
8296 and then Present (Renamed_Object (Entity (N)))
8297 then
8298 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8299 end if;
8301 -- Tagged and controlled types and aliased types are always aligned, as
8302 -- are concurrent types.
8304 if Is_Aliased (T)
8305 or else Has_Controlled_Component (T)
8306 or else Is_Concurrent_Type (T)
8307 or else Is_Tagged_Type (T)
8308 or else Is_Controlled (T)
8309 then
8310 return False;
8311 end if;
8313 -- If this is an element of a packed array, may be unaligned
8315 if Is_Ref_To_Bit_Packed_Array (N) then
8316 return True;
8317 end if;
8319 -- Case of indexed component reference: test whether prefix is unaligned
8321 if Nkind (N) = N_Indexed_Component then
8322 return Is_Possibly_Unaligned_Object (Prefix (N));
8324 -- Case of selected component reference
8326 elsif Nkind (N) = N_Selected_Component then
8327 declare
8328 P : constant Node_Id := Prefix (N);
8329 C : constant Entity_Id := Entity (Selector_Name (N));
8330 M : Nat;
8331 S : Nat;
8333 begin
8334 -- If component reference is for an array with non-static bounds,
8335 -- then it is always aligned: we can only process unaligned arrays
8336 -- with static bounds (more precisely compile time known bounds).
8338 if Is_Array_Type (T)
8339 and then not Compile_Time_Known_Bounds (T)
8340 then
8341 return False;
8342 end if;
8344 -- If component is aliased, it is definitely properly aligned
8346 if Is_Aliased (C) then
8347 return False;
8348 end if;
8350 -- If component is for a type implemented as a scalar, and the
8351 -- record is packed, and the component is other than the first
8352 -- component of the record, then the component may be unaligned.
8354 if Is_Packed (Etype (P))
8355 and then Represented_As_Scalar (Etype (C))
8356 and then First_Entity (Scope (C)) /= C
8357 then
8358 return True;
8359 end if;
8361 -- Compute maximum possible alignment for T
8363 -- If alignment is known, then that settles things
8365 if Known_Alignment (T) then
8366 M := UI_To_Int (Alignment (T));
8368 -- If alignment is not known, tentatively set max alignment
8370 else
8371 M := Ttypes.Maximum_Alignment;
8373 -- We can reduce this if the Esize is known since the default
8374 -- alignment will never be more than the smallest power of 2
8375 -- that does not exceed this Esize value.
8377 if Known_Esize (T) then
8378 S := UI_To_Int (Esize (T));
8380 while (M / 2) >= S loop
8381 M := M / 2;
8382 end loop;
8383 end if;
8384 end if;
8386 -- The following code is historical, it used to be present but it
8387 -- is too cautious, because the front-end does not know the proper
8388 -- default alignments for the target. Also, if the alignment is
8389 -- not known, the front end can't know in any case. If a copy is
8390 -- needed, the back-end will take care of it. This whole section
8391 -- including this comment can be removed later ???
8393 -- If the component reference is for a record that has a specified
8394 -- alignment, and we either know it is too small, or cannot tell,
8395 -- then the component may be unaligned.
8397 -- What is the following commented out code ???
8399 -- if Known_Alignment (Etype (P))
8400 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8401 -- and then M > Alignment (Etype (P))
8402 -- then
8403 -- return True;
8404 -- end if;
8406 -- Case of component clause present which may specify an
8407 -- unaligned position.
8409 if Present (Component_Clause (C)) then
8411 -- Otherwise we can do a test to make sure that the actual
8412 -- start position in the record, and the length, are both
8413 -- consistent with the required alignment. If not, we know
8414 -- that we are unaligned.
8416 declare
8417 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8418 begin
8419 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
8420 or else Esize (C) mod Align_In_Bits /= 0
8421 then
8422 return True;
8423 end if;
8424 end;
8425 end if;
8427 -- Otherwise, for a component reference, test prefix
8429 return Is_Possibly_Unaligned_Object (P);
8430 end;
8432 -- If not a component reference, must be aligned
8434 else
8435 return False;
8436 end if;
8437 end Is_Possibly_Unaligned_Object;
8439 ---------------------------------
8440 -- Is_Possibly_Unaligned_Slice --
8441 ---------------------------------
8443 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8444 begin
8445 -- Go to renamed object
8447 if Is_Entity_Name (N)
8448 and then Is_Object (Entity (N))
8449 and then Present (Renamed_Object (Entity (N)))
8450 then
8451 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8452 end if;
8454 -- The reference must be a slice
8456 if Nkind (N) /= N_Slice then
8457 return False;
8458 end if;
8460 -- We only need to worry if the target has strict alignment
8462 if not Target_Strict_Alignment then
8463 return False;
8464 end if;
8466 -- If it is a slice, then look at the array type being sliced
8468 declare
8469 Sarr : constant Node_Id := Prefix (N);
8470 -- Prefix of the slice, i.e. the array being sliced
8472 Styp : constant Entity_Id := Etype (Prefix (N));
8473 -- Type of the array being sliced
8475 Pref : Node_Id;
8476 Ptyp : Entity_Id;
8478 begin
8479 -- The problems arise if the array object that is being sliced
8480 -- is a component of a record or array, and we cannot guarantee
8481 -- the alignment of the array within its containing object.
8483 -- To investigate this, we look at successive prefixes to see
8484 -- if we have a worrisome indexed or selected component.
8486 Pref := Sarr;
8487 loop
8488 -- Case of array is part of an indexed component reference
8490 if Nkind (Pref) = N_Indexed_Component then
8491 Ptyp := Etype (Prefix (Pref));
8493 -- The only problematic case is when the array is packed, in
8494 -- which case we really know nothing about the alignment of
8495 -- individual components.
8497 if Is_Bit_Packed_Array (Ptyp) then
8498 return True;
8499 end if;
8501 -- Case of array is part of a selected component reference
8503 elsif Nkind (Pref) = N_Selected_Component then
8504 Ptyp := Etype (Prefix (Pref));
8506 -- We are definitely in trouble if the record in question
8507 -- has an alignment, and either we know this alignment is
8508 -- inconsistent with the alignment of the slice, or we don't
8509 -- know what the alignment of the slice should be.
8511 if Known_Alignment (Ptyp)
8512 and then (Unknown_Alignment (Styp)
8513 or else Alignment (Styp) > Alignment (Ptyp))
8514 then
8515 return True;
8516 end if;
8518 -- We are in potential trouble if the record type is packed.
8519 -- We could special case when we know that the array is the
8520 -- first component, but that's not such a simple case ???
8522 if Is_Packed (Ptyp) then
8523 return True;
8524 end if;
8526 -- We are in trouble if there is a component clause, and
8527 -- either we do not know the alignment of the slice, or
8528 -- the alignment of the slice is inconsistent with the
8529 -- bit position specified by the component clause.
8531 declare
8532 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8533 begin
8534 if Present (Component_Clause (Field))
8535 and then
8536 (Unknown_Alignment (Styp)
8537 or else
8538 (Component_Bit_Offset (Field) mod
8539 (System_Storage_Unit * Alignment (Styp))) /= 0)
8540 then
8541 return True;
8542 end if;
8543 end;
8545 -- For cases other than selected or indexed components we know we
8546 -- are OK, since no issues arise over alignment.
8548 else
8549 return False;
8550 end if;
8552 -- We processed an indexed component or selected component
8553 -- reference that looked safe, so keep checking prefixes.
8555 Pref := Prefix (Pref);
8556 end loop;
8557 end;
8558 end Is_Possibly_Unaligned_Slice;
8560 -------------------------------
8561 -- Is_Related_To_Func_Return --
8562 -------------------------------
8564 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8565 Expr : constant Node_Id := Related_Expression (Id);
8566 begin
8567 return
8568 Present (Expr)
8569 and then Nkind (Expr) = N_Explicit_Dereference
8570 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8571 end Is_Related_To_Func_Return;
8573 --------------------------------
8574 -- Is_Ref_To_Bit_Packed_Array --
8575 --------------------------------
8577 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8578 Result : Boolean;
8579 Expr : Node_Id;
8581 begin
8582 if Is_Entity_Name (N)
8583 and then Is_Object (Entity (N))
8584 and then Present (Renamed_Object (Entity (N)))
8585 then
8586 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8587 end if;
8589 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8590 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8591 Result := True;
8592 else
8593 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8594 end if;
8596 if Result and then Nkind (N) = N_Indexed_Component then
8597 Expr := First (Expressions (N));
8598 while Present (Expr) loop
8599 Force_Evaluation (Expr);
8600 Next (Expr);
8601 end loop;
8602 end if;
8604 return Result;
8606 else
8607 return False;
8608 end if;
8609 end Is_Ref_To_Bit_Packed_Array;
8611 --------------------------------
8612 -- Is_Ref_To_Bit_Packed_Slice --
8613 --------------------------------
8615 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8616 begin
8617 if Nkind (N) = N_Type_Conversion then
8618 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8620 elsif Is_Entity_Name (N)
8621 and then Is_Object (Entity (N))
8622 and then Present (Renamed_Object (Entity (N)))
8623 then
8624 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8626 elsif Nkind (N) = N_Slice
8627 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8628 then
8629 return True;
8631 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8632 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8634 else
8635 return False;
8636 end if;
8637 end Is_Ref_To_Bit_Packed_Slice;
8639 -----------------------
8640 -- Is_Renamed_Object --
8641 -----------------------
8643 function Is_Renamed_Object (N : Node_Id) return Boolean is
8644 Pnod : constant Node_Id := Parent (N);
8645 Kind : constant Node_Kind := Nkind (Pnod);
8646 begin
8647 if Kind = N_Object_Renaming_Declaration then
8648 return True;
8649 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8650 return Is_Renamed_Object (Pnod);
8651 else
8652 return False;
8653 end if;
8654 end Is_Renamed_Object;
8656 --------------------------------------
8657 -- Is_Secondary_Stack_BIP_Func_Call --
8658 --------------------------------------
8660 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8661 Alloc_Nam : Name_Id := No_Name;
8662 Actual : Node_Id;
8663 Call : Node_Id := Expr;
8664 Formal : Node_Id;
8665 Param : Node_Id;
8667 begin
8668 -- Build-in-place calls usually appear in 'reference format. Note that
8669 -- the accessibility check machinery may add an extra 'reference due to
8670 -- side effect removal.
8672 while Nkind (Call) = N_Reference loop
8673 Call := Prefix (Call);
8674 end loop;
8676 if Nkind_In (Call, N_Qualified_Expression,
8677 N_Unchecked_Type_Conversion)
8678 then
8679 Call := Expression (Call);
8680 end if;
8682 if Is_Build_In_Place_Function_Call (Call) then
8684 -- Examine all parameter associations of the function call
8686 Param := First (Parameter_Associations (Call));
8687 while Present (Param) loop
8688 if Nkind (Param) = N_Parameter_Association
8689 and then Nkind (Selector_Name (Param)) = N_Identifier
8690 then
8691 Formal := Selector_Name (Param);
8692 Actual := Explicit_Actual_Parameter (Param);
8694 -- Construct the name of formal BIPalloc. It is much easier to
8695 -- extract the name of the function using an arbitrary formal's
8696 -- scope rather than the Name field of Call.
8698 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8699 Alloc_Nam :=
8700 New_External_Name
8701 (Chars (Scope (Entity (Formal))),
8702 BIP_Formal_Suffix (BIP_Alloc_Form));
8703 end if;
8705 -- A match for BIPalloc => 2 has been found
8707 if Chars (Formal) = Alloc_Nam
8708 and then Nkind (Actual) = N_Integer_Literal
8709 and then Intval (Actual) = Uint_2
8710 then
8711 return True;
8712 end if;
8713 end if;
8715 Next (Param);
8716 end loop;
8717 end if;
8719 return False;
8720 end Is_Secondary_Stack_BIP_Func_Call;
8722 -------------------------------------
8723 -- Is_Tag_To_Class_Wide_Conversion --
8724 -------------------------------------
8726 function Is_Tag_To_Class_Wide_Conversion
8727 (Obj_Id : Entity_Id) return Boolean
8729 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8731 begin
8732 return
8733 Is_Class_Wide_Type (Etype (Obj_Id))
8734 and then Present (Expr)
8735 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8736 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8737 end Is_Tag_To_Class_Wide_Conversion;
8739 ----------------------------
8740 -- Is_Untagged_Derivation --
8741 ----------------------------
8743 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8744 begin
8745 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8746 or else
8747 (Is_Private_Type (T) and then Present (Full_View (T))
8748 and then not Is_Tagged_Type (Full_View (T))
8749 and then Is_Derived_Type (Full_View (T))
8750 and then Etype (Full_View (T)) /= T);
8751 end Is_Untagged_Derivation;
8753 ------------------------------------
8754 -- Is_Untagged_Private_Derivation --
8755 ------------------------------------
8757 function Is_Untagged_Private_Derivation
8758 (Priv_Typ : Entity_Id;
8759 Full_Typ : Entity_Id) return Boolean
8761 begin
8762 return
8763 Present (Priv_Typ)
8764 and then Is_Untagged_Derivation (Priv_Typ)
8765 and then Is_Private_Type (Etype (Priv_Typ))
8766 and then Present (Full_Typ)
8767 and then Is_Itype (Full_Typ);
8768 end Is_Untagged_Private_Derivation;
8770 ---------------------------
8771 -- Is_Volatile_Reference --
8772 ---------------------------
8774 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8775 begin
8776 -- Only source references are to be treated as volatile, internally
8777 -- generated stuff cannot have volatile external effects.
8779 if not Comes_From_Source (N) then
8780 return False;
8782 -- Never true for reference to a type
8784 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8785 return False;
8787 -- Never true for a compile time known constant
8789 elsif Compile_Time_Known_Value (N) then
8790 return False;
8792 -- True if object reference with volatile type
8794 elsif Is_Volatile_Object (N) then
8795 return True;
8797 -- True if reference to volatile entity
8799 elsif Is_Entity_Name (N) then
8800 return Treat_As_Volatile (Entity (N));
8802 -- True for slice of volatile array
8804 elsif Nkind (N) = N_Slice then
8805 return Is_Volatile_Reference (Prefix (N));
8807 -- True if volatile component
8809 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8810 if (Is_Entity_Name (Prefix (N))
8811 and then Has_Volatile_Components (Entity (Prefix (N))))
8812 or else (Present (Etype (Prefix (N)))
8813 and then Has_Volatile_Components (Etype (Prefix (N))))
8814 then
8815 return True;
8816 else
8817 return Is_Volatile_Reference (Prefix (N));
8818 end if;
8820 -- Otherwise false
8822 else
8823 return False;
8824 end if;
8825 end Is_Volatile_Reference;
8827 --------------------
8828 -- Kill_Dead_Code --
8829 --------------------
8831 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8832 W : Boolean := Warn;
8833 -- Set False if warnings suppressed
8835 begin
8836 if Present (N) then
8837 Remove_Warning_Messages (N);
8839 -- Generate warning if appropriate
8841 if W then
8843 -- We suppress the warning if this code is under control of an
8844 -- if statement, whose condition is a simple identifier, and
8845 -- either we are in an instance, or warnings off is set for this
8846 -- identifier. The reason for killing it in the instance case is
8847 -- that it is common and reasonable for code to be deleted in
8848 -- instances for various reasons.
8850 -- Could we use Is_Statically_Unevaluated here???
8852 if Nkind (Parent (N)) = N_If_Statement then
8853 declare
8854 C : constant Node_Id := Condition (Parent (N));
8855 begin
8856 if Nkind (C) = N_Identifier
8857 and then
8858 (In_Instance
8859 or else (Present (Entity (C))
8860 and then Has_Warnings_Off (Entity (C))))
8861 then
8862 W := False;
8863 end if;
8864 end;
8865 end if;
8867 -- Generate warning if not suppressed
8869 if W then
8870 Error_Msg_F
8871 ("?t?this code can never be executed and has been deleted!",
8873 end if;
8874 end if;
8876 -- Recurse into block statements and bodies to process declarations
8877 -- and statements.
8879 if Nkind (N) = N_Block_Statement
8880 or else Nkind (N) = N_Subprogram_Body
8881 or else Nkind (N) = N_Package_Body
8882 then
8883 Kill_Dead_Code (Declarations (N), False);
8884 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8886 if Nkind (N) = N_Subprogram_Body then
8887 Set_Is_Eliminated (Defining_Entity (N));
8888 end if;
8890 elsif Nkind (N) = N_Package_Declaration then
8891 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8892 Kill_Dead_Code (Private_Declarations (Specification (N)));
8894 -- ??? After this point, Delete_Tree has been called on all
8895 -- declarations in Specification (N), so references to entities
8896 -- therein look suspicious.
8898 declare
8899 E : Entity_Id := First_Entity (Defining_Entity (N));
8901 begin
8902 while Present (E) loop
8903 if Ekind (E) = E_Operator then
8904 Set_Is_Eliminated (E);
8905 end if;
8907 Next_Entity (E);
8908 end loop;
8909 end;
8911 -- Recurse into composite statement to kill individual statements in
8912 -- particular instantiations.
8914 elsif Nkind (N) = N_If_Statement then
8915 Kill_Dead_Code (Then_Statements (N));
8916 Kill_Dead_Code (Elsif_Parts (N));
8917 Kill_Dead_Code (Else_Statements (N));
8919 elsif Nkind (N) = N_Loop_Statement then
8920 Kill_Dead_Code (Statements (N));
8922 elsif Nkind (N) = N_Case_Statement then
8923 declare
8924 Alt : Node_Id;
8925 begin
8926 Alt := First (Alternatives (N));
8927 while Present (Alt) loop
8928 Kill_Dead_Code (Statements (Alt));
8929 Next (Alt);
8930 end loop;
8931 end;
8933 elsif Nkind (N) = N_Case_Statement_Alternative then
8934 Kill_Dead_Code (Statements (N));
8936 -- Deal with dead instances caused by deleting instantiations
8938 elsif Nkind (N) in N_Generic_Instantiation then
8939 Remove_Dead_Instance (N);
8940 end if;
8941 end if;
8942 end Kill_Dead_Code;
8944 -- Case where argument is a list of nodes to be killed
8946 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8947 N : Node_Id;
8948 W : Boolean;
8950 begin
8951 W := Warn;
8953 if Is_Non_Empty_List (L) then
8954 N := First (L);
8955 while Present (N) loop
8956 Kill_Dead_Code (N, W);
8957 W := False;
8958 Next (N);
8959 end loop;
8960 end if;
8961 end Kill_Dead_Code;
8963 ------------------------
8964 -- Known_Non_Negative --
8965 ------------------------
8967 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8968 begin
8969 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8970 return True;
8972 else
8973 declare
8974 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8975 begin
8976 return
8977 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8978 end;
8979 end if;
8980 end Known_Non_Negative;
8982 -----------------------------
8983 -- Make_CW_Equivalent_Type --
8984 -----------------------------
8986 -- Create a record type used as an equivalent of any member of the class
8987 -- which takes its size from exp.
8989 -- Generate the following code:
8991 -- type Equiv_T is record
8992 -- _parent : T (List of discriminant constraints taken from Exp);
8993 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8994 -- end Equiv_T;
8996 -- ??? Note that this type does not guarantee same alignment as all
8997 -- derived types
8999 function Make_CW_Equivalent_Type
9000 (T : Entity_Id;
9001 E : Node_Id) return Entity_Id
9003 Loc : constant Source_Ptr := Sloc (E);
9004 Root_Typ : constant Entity_Id := Root_Type (T);
9005 List_Def : constant List_Id := Empty_List;
9006 Comp_List : constant List_Id := New_List;
9007 Equiv_Type : Entity_Id;
9008 Range_Type : Entity_Id;
9009 Str_Type : Entity_Id;
9010 Constr_Root : Entity_Id;
9011 Sizexpr : Node_Id;
9013 begin
9014 -- If the root type is already constrained, there are no discriminants
9015 -- in the expression.
9017 if not Has_Discriminants (Root_Typ)
9018 or else Is_Constrained (Root_Typ)
9019 then
9020 Constr_Root := Root_Typ;
9022 -- At this point in the expansion, non-limited view of the type
9023 -- must be available, otherwise the error will be reported later.
9025 if From_Limited_With (Constr_Root)
9026 and then Present (Non_Limited_View (Constr_Root))
9027 then
9028 Constr_Root := Non_Limited_View (Constr_Root);
9029 end if;
9031 else
9032 Constr_Root := Make_Temporary (Loc, 'R');
9034 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9036 Append_To (List_Def,
9037 Make_Subtype_Declaration (Loc,
9038 Defining_Identifier => Constr_Root,
9039 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9040 end if;
9042 -- Generate the range subtype declaration
9044 Range_Type := Make_Temporary (Loc, 'G');
9046 if not Is_Interface (Root_Typ) then
9048 -- subtype rg__xx is
9049 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9051 Sizexpr :=
9052 Make_Op_Subtract (Loc,
9053 Left_Opnd =>
9054 Make_Attribute_Reference (Loc,
9055 Prefix =>
9056 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9057 Attribute_Name => Name_Size),
9058 Right_Opnd =>
9059 Make_Attribute_Reference (Loc,
9060 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9061 Attribute_Name => Name_Object_Size));
9062 else
9063 -- subtype rg__xx is
9064 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9066 Sizexpr :=
9067 Make_Attribute_Reference (Loc,
9068 Prefix =>
9069 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9070 Attribute_Name => Name_Size);
9071 end if;
9073 Set_Paren_Count (Sizexpr, 1);
9075 Append_To (List_Def,
9076 Make_Subtype_Declaration (Loc,
9077 Defining_Identifier => Range_Type,
9078 Subtype_Indication =>
9079 Make_Subtype_Indication (Loc,
9080 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9081 Constraint => Make_Range_Constraint (Loc,
9082 Range_Expression =>
9083 Make_Range (Loc,
9084 Low_Bound => Make_Integer_Literal (Loc, 1),
9085 High_Bound =>
9086 Make_Op_Divide (Loc,
9087 Left_Opnd => Sizexpr,
9088 Right_Opnd => Make_Integer_Literal (Loc,
9089 Intval => System_Storage_Unit)))))));
9091 -- subtype str__nn is Storage_Array (rg__x);
9093 Str_Type := Make_Temporary (Loc, 'S');
9094 Append_To (List_Def,
9095 Make_Subtype_Declaration (Loc,
9096 Defining_Identifier => Str_Type,
9097 Subtype_Indication =>
9098 Make_Subtype_Indication (Loc,
9099 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9100 Constraint =>
9101 Make_Index_Or_Discriminant_Constraint (Loc,
9102 Constraints =>
9103 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9105 -- type Equiv_T is record
9106 -- [ _parent : Tnn; ]
9107 -- E : Str_Type;
9108 -- end Equiv_T;
9110 Equiv_Type := Make_Temporary (Loc, 'T');
9111 Set_Ekind (Equiv_Type, E_Record_Type);
9112 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9114 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9115 -- treatment for this type. In particular, even though _parent's type
9116 -- is a controlled type or contains controlled components, we do not
9117 -- want to set Has_Controlled_Component on it to avoid making it gain
9118 -- an unwanted _controller component.
9120 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9122 -- A class-wide equivalent type does not require initialization
9124 Set_Suppress_Initialization (Equiv_Type);
9126 if not Is_Interface (Root_Typ) then
9127 Append_To (Comp_List,
9128 Make_Component_Declaration (Loc,
9129 Defining_Identifier =>
9130 Make_Defining_Identifier (Loc, Name_uParent),
9131 Component_Definition =>
9132 Make_Component_Definition (Loc,
9133 Aliased_Present => False,
9134 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9135 end if;
9137 Append_To (Comp_List,
9138 Make_Component_Declaration (Loc,
9139 Defining_Identifier => Make_Temporary (Loc, 'C'),
9140 Component_Definition =>
9141 Make_Component_Definition (Loc,
9142 Aliased_Present => False,
9143 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9145 Append_To (List_Def,
9146 Make_Full_Type_Declaration (Loc,
9147 Defining_Identifier => Equiv_Type,
9148 Type_Definition =>
9149 Make_Record_Definition (Loc,
9150 Component_List =>
9151 Make_Component_List (Loc,
9152 Component_Items => Comp_List,
9153 Variant_Part => Empty))));
9155 -- Suppress all checks during the analysis of the expanded code to avoid
9156 -- the generation of spurious warnings under ZFP run-time.
9158 Insert_Actions (E, List_Def, Suppress => All_Checks);
9159 return Equiv_Type;
9160 end Make_CW_Equivalent_Type;
9162 -------------------------
9163 -- Make_Invariant_Call --
9164 -------------------------
9166 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9167 Loc : constant Source_Ptr := Sloc (Expr);
9168 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9170 Proc_Id : Entity_Id;
9172 begin
9173 pragma Assert (Has_Invariants (Typ));
9175 Proc_Id := Invariant_Procedure (Typ);
9176 pragma Assert (Present (Proc_Id));
9178 return
9179 Make_Procedure_Call_Statement (Loc,
9180 Name => New_Occurrence_Of (Proc_Id, Loc),
9181 Parameter_Associations => New_List (Relocate_Node (Expr)));
9182 end Make_Invariant_Call;
9184 ------------------------
9185 -- Make_Literal_Range --
9186 ------------------------
9188 function Make_Literal_Range
9189 (Loc : Source_Ptr;
9190 Literal_Typ : Entity_Id) return Node_Id
9192 Lo : constant Node_Id :=
9193 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9194 Index : constant Entity_Id := Etype (Lo);
9196 Hi : Node_Id;
9197 Length_Expr : constant Node_Id :=
9198 Make_Op_Subtract (Loc,
9199 Left_Opnd =>
9200 Make_Integer_Literal (Loc,
9201 Intval => String_Literal_Length (Literal_Typ)),
9202 Right_Opnd =>
9203 Make_Integer_Literal (Loc, 1));
9205 begin
9206 Set_Analyzed (Lo, False);
9208 if Is_Integer_Type (Index) then
9209 Hi :=
9210 Make_Op_Add (Loc,
9211 Left_Opnd => New_Copy_Tree (Lo),
9212 Right_Opnd => Length_Expr);
9213 else
9214 Hi :=
9215 Make_Attribute_Reference (Loc,
9216 Attribute_Name => Name_Val,
9217 Prefix => New_Occurrence_Of (Index, Loc),
9218 Expressions => New_List (
9219 Make_Op_Add (Loc,
9220 Left_Opnd =>
9221 Make_Attribute_Reference (Loc,
9222 Attribute_Name => Name_Pos,
9223 Prefix => New_Occurrence_Of (Index, Loc),
9224 Expressions => New_List (New_Copy_Tree (Lo))),
9225 Right_Opnd => Length_Expr)));
9226 end if;
9228 return
9229 Make_Range (Loc,
9230 Low_Bound => Lo,
9231 High_Bound => Hi);
9232 end Make_Literal_Range;
9234 --------------------------
9235 -- Make_Non_Empty_Check --
9236 --------------------------
9238 function Make_Non_Empty_Check
9239 (Loc : Source_Ptr;
9240 N : Node_Id) return Node_Id
9242 begin
9243 return
9244 Make_Op_Ne (Loc,
9245 Left_Opnd =>
9246 Make_Attribute_Reference (Loc,
9247 Attribute_Name => Name_Length,
9248 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9249 Right_Opnd =>
9250 Make_Integer_Literal (Loc, 0));
9251 end Make_Non_Empty_Check;
9253 -------------------------
9254 -- Make_Predicate_Call --
9255 -------------------------
9257 -- WARNING: This routine manages Ghost regions. Return statements must be
9258 -- replaced by gotos which jump to the end of the routine and restore the
9259 -- Ghost mode.
9261 function Make_Predicate_Call
9262 (Typ : Entity_Id;
9263 Expr : Node_Id;
9264 Mem : Boolean := False) return Node_Id
9266 Loc : constant Source_Ptr := Sloc (Expr);
9268 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9269 -- Save the Ghost mode to restore on exit
9271 Call : Node_Id;
9272 Func_Id : Entity_Id;
9274 begin
9275 pragma Assert (Present (Predicate_Function (Typ)));
9277 -- The related type may be subject to pragma Ghost. Set the mode now to
9278 -- ensure that the call is properly marked as Ghost.
9280 Set_Ghost_Mode (Typ);
9282 -- Call special membership version if requested and available
9284 if Mem and then Present (Predicate_Function_M (Typ)) then
9285 Func_Id := Predicate_Function_M (Typ);
9286 else
9287 Func_Id := Predicate_Function (Typ);
9288 end if;
9290 -- Case of calling normal predicate function
9292 Call :=
9293 Make_Function_Call (Loc,
9294 Name => New_Occurrence_Of (Func_Id, Loc),
9295 Parameter_Associations => New_List (Relocate_Node (Expr)));
9297 Restore_Ghost_Mode (Saved_GM);
9299 return Call;
9300 end Make_Predicate_Call;
9302 --------------------------
9303 -- Make_Predicate_Check --
9304 --------------------------
9306 function Make_Predicate_Check
9307 (Typ : Entity_Id;
9308 Expr : Node_Id) return Node_Id
9310 procedure Replace_Subtype_Reference (N : Node_Id);
9311 -- Replace current occurrences of the subtype to which a dynamic
9312 -- predicate applies, by the expression that triggers a predicate
9313 -- check. This is needed for aspect Predicate_Failure, for which
9314 -- we do not generate a wrapper procedure, but simply modify the
9315 -- expression for the pragma of the predicate check.
9317 --------------------------------
9318 -- Replace_Subtype_Reference --
9319 --------------------------------
9321 procedure Replace_Subtype_Reference (N : Node_Id) is
9322 begin
9323 Rewrite (N, New_Copy_Tree (Expr));
9325 -- We want to treat the node as if it comes from source, so
9326 -- that ASIS will not ignore it.
9328 Set_Comes_From_Source (N, True);
9329 end Replace_Subtype_Reference;
9331 procedure Replace_Subtype_References is
9332 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9334 -- Local variables
9336 Loc : constant Source_Ptr := Sloc (Expr);
9337 Arg_List : List_Id;
9338 Fail_Expr : Node_Id;
9339 Nam : Name_Id;
9341 -- Start of processing for Make_Predicate_Check
9343 begin
9344 -- If predicate checks are suppressed, then return a null statement. For
9345 -- this call, we check only the scope setting. If the caller wants to
9346 -- check a specific entity's setting, they must do it manually.
9348 if Predicate_Checks_Suppressed (Empty) then
9349 return Make_Null_Statement (Loc);
9350 end if;
9352 -- Do not generate a check within an internal subprogram (stream
9353 -- functions and the like, including including predicate functions).
9355 if Within_Internal_Subprogram then
9356 return Make_Null_Statement (Loc);
9357 end if;
9359 -- Compute proper name to use, we need to get this right so that the
9360 -- right set of check policies apply to the Check pragma we are making.
9362 if Has_Dynamic_Predicate_Aspect (Typ) then
9363 Nam := Name_Dynamic_Predicate;
9364 elsif Has_Static_Predicate_Aspect (Typ) then
9365 Nam := Name_Static_Predicate;
9366 else
9367 Nam := Name_Predicate;
9368 end if;
9370 Arg_List := New_List (
9371 Make_Pragma_Argument_Association (Loc,
9372 Expression => Make_Identifier (Loc, Nam)),
9373 Make_Pragma_Argument_Association (Loc,
9374 Expression => Make_Predicate_Call (Typ, Expr)));
9376 -- If subtype has Predicate_Failure defined, add the correponding
9377 -- expression as an additional pragma parameter, after replacing
9378 -- current instances with the expression being checked.
9380 if Has_Aspect (Typ, Aspect_Predicate_Failure) then
9381 Fail_Expr :=
9382 New_Copy_Tree
9383 (Expression (Find_Aspect (Typ, Aspect_Predicate_Failure)));
9384 Replace_Subtype_References (Fail_Expr, Typ);
9386 Append_To (Arg_List,
9387 Make_Pragma_Argument_Association (Loc,
9388 Expression => Fail_Expr));
9389 end if;
9391 return
9392 Make_Pragma (Loc,
9393 Chars => Name_Check,
9394 Pragma_Argument_Associations => Arg_List);
9395 end Make_Predicate_Check;
9397 ----------------------------
9398 -- Make_Subtype_From_Expr --
9399 ----------------------------
9401 -- 1. If Expr is an unconstrained array expression, creates
9402 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9404 -- 2. If Expr is a unconstrained discriminated type expression, creates
9405 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9407 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9409 function Make_Subtype_From_Expr
9410 (E : Node_Id;
9411 Unc_Typ : Entity_Id;
9412 Related_Id : Entity_Id := Empty) return Node_Id
9414 List_Constr : constant List_Id := New_List;
9415 Loc : constant Source_Ptr := Sloc (E);
9416 D : Entity_Id;
9417 Full_Exp : Node_Id;
9418 Full_Subtyp : Entity_Id;
9419 High_Bound : Entity_Id;
9420 Index_Typ : Entity_Id;
9421 Low_Bound : Entity_Id;
9422 Priv_Subtyp : Entity_Id;
9423 Utyp : Entity_Id;
9425 begin
9426 if Is_Private_Type (Unc_Typ)
9427 and then Has_Unknown_Discriminants (Unc_Typ)
9428 then
9429 -- The caller requests a unique external name for both the private
9430 -- and the full subtype.
9432 if Present (Related_Id) then
9433 Full_Subtyp :=
9434 Make_Defining_Identifier (Loc,
9435 Chars => New_External_Name (Chars (Related_Id), 'C'));
9436 Priv_Subtyp :=
9437 Make_Defining_Identifier (Loc,
9438 Chars => New_External_Name (Chars (Related_Id), 'P'));
9440 else
9441 Full_Subtyp := Make_Temporary (Loc, 'C');
9442 Priv_Subtyp := Make_Temporary (Loc, 'P');
9443 end if;
9445 -- Prepare the subtype completion. Use the base type to find the
9446 -- underlying type because the type may be a generic actual or an
9447 -- explicit subtype.
9449 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9451 Full_Exp :=
9452 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9453 Set_Parent (Full_Exp, Parent (E));
9455 Insert_Action (E,
9456 Make_Subtype_Declaration (Loc,
9457 Defining_Identifier => Full_Subtyp,
9458 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9460 -- Define the dummy private subtype
9462 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9463 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9464 Set_Scope (Priv_Subtyp, Full_Subtyp);
9465 Set_Is_Constrained (Priv_Subtyp);
9466 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9467 Set_Is_Itype (Priv_Subtyp);
9468 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9470 if Is_Tagged_Type (Priv_Subtyp) then
9471 Set_Class_Wide_Type
9472 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9473 Set_Direct_Primitive_Operations (Priv_Subtyp,
9474 Direct_Primitive_Operations (Unc_Typ));
9475 end if;
9477 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9479 return New_Occurrence_Of (Priv_Subtyp, Loc);
9481 elsif Is_Array_Type (Unc_Typ) then
9482 Index_Typ := First_Index (Unc_Typ);
9483 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9485 -- Capture the bounds of each index constraint in case the context
9486 -- is an object declaration of an unconstrained type initialized
9487 -- by a function call:
9489 -- Obj : Unconstr_Typ := Func_Call;
9491 -- This scenario requires secondary scope management and the index
9492 -- constraint cannot depend on the temporary used to capture the
9493 -- result of the function call.
9495 -- SS_Mark;
9496 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9497 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9498 -- Obj : S := Temp.all;
9499 -- SS_Release; -- Temp is gone at this point, bounds of S are
9500 -- -- non existent.
9502 -- Generate:
9503 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9505 Low_Bound := Make_Temporary (Loc, 'B');
9506 Insert_Action (E,
9507 Make_Object_Declaration (Loc,
9508 Defining_Identifier => Low_Bound,
9509 Object_Definition =>
9510 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9511 Constant_Present => True,
9512 Expression =>
9513 Make_Attribute_Reference (Loc,
9514 Prefix => Duplicate_Subexpr_No_Checks (E),
9515 Attribute_Name => Name_First,
9516 Expressions => New_List (
9517 Make_Integer_Literal (Loc, J)))));
9519 -- Generate:
9520 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9522 High_Bound := Make_Temporary (Loc, 'B');
9523 Insert_Action (E,
9524 Make_Object_Declaration (Loc,
9525 Defining_Identifier => High_Bound,
9526 Object_Definition =>
9527 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9528 Constant_Present => True,
9529 Expression =>
9530 Make_Attribute_Reference (Loc,
9531 Prefix => Duplicate_Subexpr_No_Checks (E),
9532 Attribute_Name => Name_Last,
9533 Expressions => New_List (
9534 Make_Integer_Literal (Loc, J)))));
9536 Append_To (List_Constr,
9537 Make_Range (Loc,
9538 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9539 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9541 Index_Typ := Next_Index (Index_Typ);
9542 end loop;
9544 elsif Is_Class_Wide_Type (Unc_Typ) then
9545 declare
9546 CW_Subtype : Entity_Id;
9547 EQ_Typ : Entity_Id := Empty;
9549 begin
9550 -- A class-wide equivalent type is not needed on VM targets
9551 -- because the VM back-ends handle the class-wide object
9552 -- initialization itself (and doesn't need or want the
9553 -- additional intermediate type to handle the assignment).
9555 if Expander_Active and then Tagged_Type_Expansion then
9557 -- If this is the class-wide type of a completion that is a
9558 -- record subtype, set the type of the class-wide type to be
9559 -- the full base type, for use in the expanded code for the
9560 -- equivalent type. Should this be done earlier when the
9561 -- completion is analyzed ???
9563 if Is_Private_Type (Etype (Unc_Typ))
9564 and then
9565 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9566 then
9567 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9568 end if;
9570 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9571 end if;
9573 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9574 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9575 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9577 return New_Occurrence_Of (CW_Subtype, Loc);
9578 end;
9580 -- Indefinite record type with discriminants
9582 else
9583 D := First_Discriminant (Unc_Typ);
9584 while Present (D) loop
9585 Append_To (List_Constr,
9586 Make_Selected_Component (Loc,
9587 Prefix => Duplicate_Subexpr_No_Checks (E),
9588 Selector_Name => New_Occurrence_Of (D, Loc)));
9590 Next_Discriminant (D);
9591 end loop;
9592 end if;
9594 return
9595 Make_Subtype_Indication (Loc,
9596 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9597 Constraint =>
9598 Make_Index_Or_Discriminant_Constraint (Loc,
9599 Constraints => List_Constr));
9600 end Make_Subtype_From_Expr;
9602 ---------------
9603 -- Map_Types --
9604 ---------------
9606 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9608 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9609 -- avoid deep indentation of code.
9611 -- NOTE: Routines which deal with discriminant mapping operate on the
9612 -- [underlying/record] full view of various types because those views
9613 -- contain all discriminants and stored constraints.
9615 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9616 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9617 -- overriding chain starting from Prim whose dispatching type is parent
9618 -- type Par_Typ and add a mapping between the result and primitive Prim.
9620 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9621 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9622 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9623 -- if no such primitive is available.
9625 function Build_Chain
9626 (Par_Typ : Entity_Id;
9627 Deriv_Typ : Entity_Id) return Elist_Id;
9628 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9629 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9630 -- list has the form:
9632 -- head tail
9633 -- v v
9634 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9636 -- Note that Par_Typ is not part of the resulting derivation chain
9638 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9639 -- Return the view of type Typ which could potentially contains either
9640 -- the discriminants or stored constraints of the type.
9642 function Find_Discriminant_Value
9643 (Discr : Entity_Id;
9644 Par_Typ : Entity_Id;
9645 Deriv_Typ : Entity_Id;
9646 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9647 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9648 -- in the derivation chain starting from parent type Par_Typ leading to
9649 -- derived type Deriv_Typ. The returned value is one of the following:
9651 -- * An entity which is either a discriminant or a non-discriminant
9652 -- name, and renames/constraints Discr.
9654 -- * An expression which constraints Discr
9656 -- Typ_Elmt is an element of the derivation chain created by routine
9657 -- Build_Chain and denotes the current ancestor being examined.
9659 procedure Map_Discriminants
9660 (Par_Typ : Entity_Id;
9661 Deriv_Typ : Entity_Id);
9662 -- Map each discriminant of type Par_Typ to a meaningful constraint
9663 -- from the point of view of type Deriv_Typ.
9665 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9666 -- Map each primitive of type Par_Typ to a corresponding primitive of
9667 -- type Deriv_Typ.
9669 -------------------
9670 -- Add_Primitive --
9671 -------------------
9673 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9674 Par_Prim : Entity_Id;
9676 begin
9677 -- Inspect the inheritance chain through the Alias attribute and the
9678 -- overriding chain through the Overridden_Operation looking for an
9679 -- ancestor primitive with the appropriate dispatching type.
9681 Par_Prim := Prim;
9682 while Present (Par_Prim) loop
9683 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9684 Par_Prim := Ancestor_Primitive (Par_Prim);
9685 end loop;
9687 -- Create a mapping of the form:
9689 -- parent type primitive -> derived type primitive
9691 if Present (Par_Prim) then
9692 Type_Map.Set (Par_Prim, Prim);
9693 end if;
9694 end Add_Primitive;
9696 ------------------------
9697 -- Ancestor_Primitive --
9698 ------------------------
9700 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9701 Inher_Prim : constant Entity_Id := Alias (Subp);
9702 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9704 begin
9705 -- The current subprogram overrides an ancestor primitive
9707 if Present (Over_Prim) then
9708 return Over_Prim;
9710 -- The current subprogram is an internally generated alias of an
9711 -- inherited ancestor primitive.
9713 elsif Present (Inher_Prim) then
9714 return Inher_Prim;
9716 -- Otherwise the current subprogram is the root of the inheritance or
9717 -- overriding chain.
9719 else
9720 return Empty;
9721 end if;
9722 end Ancestor_Primitive;
9724 -----------------
9725 -- Build_Chain --
9726 -----------------
9728 function Build_Chain
9729 (Par_Typ : Entity_Id;
9730 Deriv_Typ : Entity_Id) return Elist_Id
9732 Anc_Typ : Entity_Id;
9733 Chain : Elist_Id;
9734 Curr_Typ : Entity_Id;
9736 begin
9737 Chain := New_Elmt_List;
9739 -- Add the derived type to the derivation chain
9741 Prepend_Elmt (Deriv_Typ, Chain);
9743 -- Examine all ancestors starting from the derived type climbing
9744 -- towards parent type Par_Typ.
9746 Curr_Typ := Deriv_Typ;
9747 loop
9748 -- Handle the case where the current type is a record which
9749 -- derives from a subtype.
9751 -- subtype Sub_Typ is Par_Typ ...
9752 -- type Deriv_Typ is Sub_Typ ...
9754 if Ekind (Curr_Typ) = E_Record_Type
9755 and then Present (Parent_Subtype (Curr_Typ))
9756 then
9757 Anc_Typ := Parent_Subtype (Curr_Typ);
9759 -- Handle the case where the current type is a record subtype of
9760 -- another subtype.
9762 -- subtype Sub_Typ1 is Par_Typ ...
9763 -- subtype Sub_Typ2 is Sub_Typ1 ...
9765 elsif Ekind (Curr_Typ) = E_Record_Subtype
9766 and then Present (Cloned_Subtype (Curr_Typ))
9767 then
9768 Anc_Typ := Cloned_Subtype (Curr_Typ);
9770 -- Otherwise use the direct parent type
9772 else
9773 Anc_Typ := Etype (Curr_Typ);
9774 end if;
9776 -- Use the first subtype when dealing with itypes
9778 if Is_Itype (Anc_Typ) then
9779 Anc_Typ := First_Subtype (Anc_Typ);
9780 end if;
9782 -- Work with the view which contains the discriminants and stored
9783 -- constraints.
9785 Anc_Typ := Discriminated_View (Anc_Typ);
9787 -- Stop the climb when either the parent type has been reached or
9788 -- there are no more ancestors left to examine.
9790 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9792 Prepend_Unique_Elmt (Anc_Typ, Chain);
9793 Curr_Typ := Anc_Typ;
9794 end loop;
9796 return Chain;
9797 end Build_Chain;
9799 ------------------------
9800 -- Discriminated_View --
9801 ------------------------
9803 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9804 T : Entity_Id;
9806 begin
9807 T := Typ;
9809 -- Use the [underlying] full view when dealing with private types
9810 -- because the view contains all inherited discriminants or stored
9811 -- constraints.
9813 if Is_Private_Type (T) then
9814 if Present (Underlying_Full_View (T)) then
9815 T := Underlying_Full_View (T);
9817 elsif Present (Full_View (T)) then
9818 T := Full_View (T);
9819 end if;
9820 end if;
9822 -- Use the underlying record view when the type is an extenstion of
9823 -- a parent type with unknown discriminants because the view contains
9824 -- all inherited discriminants or stored constraints.
9826 if Ekind (T) = E_Record_Type
9827 and then Present (Underlying_Record_View (T))
9828 then
9829 T := Underlying_Record_View (T);
9830 end if;
9832 return T;
9833 end Discriminated_View;
9835 -----------------------------
9836 -- Find_Discriminant_Value --
9837 -----------------------------
9839 function Find_Discriminant_Value
9840 (Discr : Entity_Id;
9841 Par_Typ : Entity_Id;
9842 Deriv_Typ : Entity_Id;
9843 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9845 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9846 Typ : constant Entity_Id := Node (Typ_Elmt);
9848 function Find_Constraint_Value
9849 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
9850 -- Given constraint Constr, find what it denotes. This is either:
9852 -- * An entity which is either a discriminant or a name
9854 -- * An expression
9856 ---------------------------
9857 -- Find_Constraint_Value --
9858 ---------------------------
9860 function Find_Constraint_Value
9861 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
9863 begin
9864 if Nkind (Constr) in N_Entity then
9866 -- The constraint denotes a discriminant of the curren type
9867 -- which renames the ancestor discriminant:
9869 -- vv
9870 -- type Typ (D1 : ...; DN : ...) is
9871 -- new Anc (Discr => D1) with ...
9872 -- ^^
9874 if Ekind (Constr) = E_Discriminant then
9876 -- The discriminant belongs to derived type Deriv_Typ. This
9877 -- is the final value for the ancestor discriminant as the
9878 -- derivations chain has been fully exhausted.
9880 if Typ = Deriv_Typ then
9881 return Constr;
9883 -- Otherwise the discriminant may be renamed or constrained
9884 -- at a lower level. Continue looking down the derivation
9885 -- chain.
9887 else
9888 return
9889 Find_Discriminant_Value
9890 (Discr => Constr,
9891 Par_Typ => Par_Typ,
9892 Deriv_Typ => Deriv_Typ,
9893 Typ_Elmt => Next_Elmt (Typ_Elmt));
9894 end if;
9896 -- Otherwise the constraint denotes a reference to some name
9897 -- which results in a Girder discriminant:
9899 -- vvvv
9900 -- Name : ...;
9901 -- type Typ (D1 : ...; DN : ...) is
9902 -- new Anc (Discr => Name) with ...
9903 -- ^^^^
9905 -- Return the name as this is the proper constraint of the
9906 -- discriminant.
9908 else
9909 return Constr;
9910 end if;
9912 -- The constraint denotes a reference to a name
9914 elsif Is_Entity_Name (Constr) then
9915 return Find_Constraint_Value (Entity (Constr));
9917 -- Otherwise the current constraint is an expression which yields
9918 -- a Girder discriminant:
9920 -- type Typ (D1 : ...; DN : ...) is
9921 -- new Anc (Discr => <expression>) with ...
9922 -- ^^^^^^^^^^
9924 -- Return the expression as this is the proper constraint of the
9925 -- discriminant.
9927 else
9928 return Constr;
9929 end if;
9930 end Find_Constraint_Value;
9932 -- Local variables
9934 Constrs : constant Elist_Id := Stored_Constraint (Typ);
9936 Constr_Elmt : Elmt_Id;
9937 Pos : Uint;
9938 Typ_Discr : Entity_Id;
9940 -- Start of processing for Find_Discriminant_Value
9942 begin
9943 -- The algorithm for finding the value of a discriminant works as
9944 -- follows. First, it recreates the derivation chain from Par_Typ
9945 -- to Deriv_Typ as a list:
9947 -- Par_Typ (shown for completeness)
9948 -- v
9949 -- Ancestor_N <-- head of chain
9950 -- v
9951 -- Ancestor_1
9952 -- v
9953 -- Deriv_Typ <-- tail of chain
9955 -- The algorithm then traces the fate of a parent discriminant down
9956 -- the derivation chain. At each derivation level, the discriminant
9957 -- may be either inherited or constrained.
9959 -- 1) Discriminant is inherited: there are two cases, depending on
9960 -- which type is inheriting.
9962 -- 1.1) Deriv_Typ is inheriting:
9964 -- type Ancestor (D_1 : ...) is tagged ...
9965 -- type Deriv_Typ is new Ancestor ...
9967 -- In this case the inherited discriminant is the final value of
9968 -- the parent discriminant because the end of the derivation chain
9969 -- has been reached.
9971 -- 1.2) Some other type is inheriting:
9973 -- type Ancestor_1 (D_1 : ...) is tagged ...
9974 -- type Ancestor_2 is new Ancestor_1 ...
9976 -- In this case the algorithm continues to trace the fate of the
9977 -- inherited discriminant down the derivation chain because it may
9978 -- be further inherited or constrained.
9980 -- 2) Discriminant is constrained: there are three cases, depending
9981 -- on what the constraint is.
9983 -- 2.1) The constraint is another discriminant (aka renaming):
9985 -- type Ancestor_1 (D_1 : ...) is tagged ...
9986 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
9988 -- In this case the constraining discriminant becomes the one to
9989 -- track down the derivation chain. The algorithm already knows
9990 -- that D_2 constrains D_1, therefore if the algorithm finds the
9991 -- value of D_2, then this would also be the value for D_1.
9993 -- 2.2) The constraint is a name (aka Girder):
9995 -- Name : ...
9996 -- type Ancestor_1 (D_1 : ...) is tagged ...
9997 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
9999 -- In this case the name is the final value of D_1 because the
10000 -- discriminant cannot be further constrained.
10002 -- 2.3) The constraint is an expression (aka Girder):
10004 -- type Ancestor_1 (D_1 : ...) is tagged ...
10005 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10007 -- Similar to 2.2, the expression is the final value of D_1
10009 Pos := Uint_1;
10011 -- When a derived type constrains its parent type, all constaints
10012 -- appear in the Stored_Constraint list. Examine the list looking
10013 -- for a positional match.
10015 if Present (Constrs) then
10016 Constr_Elmt := First_Elmt (Constrs);
10017 while Present (Constr_Elmt) loop
10019 -- The position of the current constraint matches that of the
10020 -- ancestor discriminant.
10022 if Pos = Discr_Pos then
10023 return Find_Constraint_Value (Node (Constr_Elmt));
10024 end if;
10026 Next_Elmt (Constr_Elmt);
10027 Pos := Pos + 1;
10028 end loop;
10030 -- Otherwise the derived type does not constraint its parent type in
10031 -- which case it inherits the parent discriminants.
10033 else
10034 Typ_Discr := First_Discriminant (Typ);
10035 while Present (Typ_Discr) loop
10037 -- The position of the current discriminant matches that of the
10038 -- ancestor discriminant.
10040 if Pos = Discr_Pos then
10041 return Find_Constraint_Value (Typ_Discr);
10042 end if;
10044 Next_Discriminant (Typ_Discr);
10045 Pos := Pos + 1;
10046 end loop;
10047 end if;
10049 -- A discriminant must always have a corresponding value. This is
10050 -- either another discriminant, a name, or an expression. If this
10051 -- point is reached, them most likely the derivation chain employs
10052 -- the wrong views of types.
10054 pragma Assert (False);
10056 return Empty;
10057 end Find_Discriminant_Value;
10059 -----------------------
10060 -- Map_Discriminants --
10061 -----------------------
10063 procedure Map_Discriminants
10064 (Par_Typ : Entity_Id;
10065 Deriv_Typ : Entity_Id)
10067 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10069 Discr : Entity_Id;
10070 Discr_Val : Node_Or_Entity_Id;
10072 begin
10073 -- Examine each discriminant of parent type Par_Typ and find a
10074 -- suitable value for it from the point of view of derived type
10075 -- Deriv_Typ.
10077 if Has_Discriminants (Par_Typ) then
10078 Discr := First_Discriminant (Par_Typ);
10079 while Present (Discr) loop
10080 Discr_Val :=
10081 Find_Discriminant_Value
10082 (Discr => Discr,
10083 Par_Typ => Par_Typ,
10084 Deriv_Typ => Deriv_Typ,
10085 Typ_Elmt => First_Elmt (Deriv_Chain));
10087 -- Create a mapping of the form:
10089 -- parent type discriminant -> value
10091 Type_Map.Set (Discr, Discr_Val);
10093 Next_Discriminant (Discr);
10094 end loop;
10095 end if;
10096 end Map_Discriminants;
10098 --------------------
10099 -- Map_Primitives --
10100 --------------------
10102 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10103 Deriv_Prim : Entity_Id;
10104 Par_Prim : Entity_Id;
10105 Par_Prims : Elist_Id;
10106 Prim_Elmt : Elmt_Id;
10108 begin
10109 -- Inspect the primitives of the derived type and determine whether
10110 -- they relate to the primitives of the parent type. If there is a
10111 -- meaningful relation, create a mapping of the form:
10113 -- parent type primitive -> perived type primitive
10115 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10116 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10117 while Present (Prim_Elmt) loop
10118 Deriv_Prim := Node (Prim_Elmt);
10120 if Is_Subprogram (Deriv_Prim)
10121 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10122 then
10123 Add_Primitive (Deriv_Prim, Par_Typ);
10124 end if;
10126 Next_Elmt (Prim_Elmt);
10127 end loop;
10128 end if;
10130 -- If the parent operation is an interface operation, the overriding
10131 -- indicator is not present. Instead, we get from the interface
10132 -- operation the primitive of the current type that implements it.
10134 if Is_Interface (Par_Typ) then
10135 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10137 if Present (Par_Prims) then
10138 Prim_Elmt := First_Elmt (Par_Prims);
10140 while Present (Prim_Elmt) loop
10141 Par_Prim := Node (Prim_Elmt);
10142 Deriv_Prim :=
10143 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10145 if Present (Deriv_Prim) then
10146 Type_Map.Set (Par_Prim, Deriv_Prim);
10147 end if;
10149 Next_Elmt (Prim_Elmt);
10150 end loop;
10151 end if;
10152 end if;
10153 end Map_Primitives;
10155 -- Start of processing for Map_Types
10157 begin
10158 -- Nothing to do if there are no types to work with
10160 if No (Parent_Type) or else No (Derived_Type) then
10161 return;
10163 -- Nothing to do if the mapping already exists
10165 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10166 return;
10168 -- Nothing to do if both types are not tagged. Note that untagged types
10169 -- do not have primitive operations and their discriminants are already
10170 -- handled by gigi.
10172 elsif not Is_Tagged_Type (Parent_Type)
10173 or else not Is_Tagged_Type (Derived_Type)
10174 then
10175 return;
10176 end if;
10178 -- Create a mapping of the form
10180 -- parent type -> derived type
10182 -- to prevent any subsequent attempts to produce the same relations
10184 Type_Map.Set (Parent_Type, Derived_Type);
10186 -- Create mappings of the form
10188 -- parent type discriminant -> derived type discriminant
10189 -- <or>
10190 -- parent type discriminant -> constraint
10192 -- Note that mapping of discriminants breaks privacy because it needs to
10193 -- work with those views which contains the discriminants and any stored
10194 -- constraints.
10196 Map_Discriminants
10197 (Par_Typ => Discriminated_View (Parent_Type),
10198 Deriv_Typ => Discriminated_View (Derived_Type));
10200 -- Create mappings of the form
10202 -- parent type primitive -> derived type primitive
10204 Map_Primitives
10205 (Par_Typ => Parent_Type,
10206 Deriv_Typ => Derived_Type);
10207 end Map_Types;
10209 ----------------------------
10210 -- Matching_Standard_Type --
10211 ----------------------------
10213 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10214 pragma Assert (Is_Scalar_Type (Typ));
10215 Siz : constant Uint := Esize (Typ);
10217 begin
10218 -- Floating-point cases
10220 if Is_Floating_Point_Type (Typ) then
10221 if Siz <= Esize (Standard_Short_Float) then
10222 return Standard_Short_Float;
10223 elsif Siz <= Esize (Standard_Float) then
10224 return Standard_Float;
10225 elsif Siz <= Esize (Standard_Long_Float) then
10226 return Standard_Long_Float;
10227 elsif Siz <= Esize (Standard_Long_Long_Float) then
10228 return Standard_Long_Long_Float;
10229 else
10230 raise Program_Error;
10231 end if;
10233 -- Integer cases (includes fixed-point types)
10235 -- Unsigned integer cases (includes normal enumeration types)
10237 elsif Is_Unsigned_Type (Typ) then
10238 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10239 return Standard_Short_Short_Unsigned;
10240 elsif Siz <= Esize (Standard_Short_Unsigned) then
10241 return Standard_Short_Unsigned;
10242 elsif Siz <= Esize (Standard_Unsigned) then
10243 return Standard_Unsigned;
10244 elsif Siz <= Esize (Standard_Long_Unsigned) then
10245 return Standard_Long_Unsigned;
10246 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10247 return Standard_Long_Long_Unsigned;
10248 else
10249 raise Program_Error;
10250 end if;
10252 -- Signed integer cases
10254 else
10255 if Siz <= Esize (Standard_Short_Short_Integer) then
10256 return Standard_Short_Short_Integer;
10257 elsif Siz <= Esize (Standard_Short_Integer) then
10258 return Standard_Short_Integer;
10259 elsif Siz <= Esize (Standard_Integer) then
10260 return Standard_Integer;
10261 elsif Siz <= Esize (Standard_Long_Integer) then
10262 return Standard_Long_Integer;
10263 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10264 return Standard_Long_Long_Integer;
10265 else
10266 raise Program_Error;
10267 end if;
10268 end if;
10269 end Matching_Standard_Type;
10271 -----------------------------
10272 -- May_Generate_Large_Temp --
10273 -----------------------------
10275 -- At the current time, the only types that we return False for (i.e. where
10276 -- we decide we know they cannot generate large temps) are ones where we
10277 -- know the size is 256 bits or less at compile time, and we are still not
10278 -- doing a thorough job on arrays and records ???
10280 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10281 begin
10282 if not Size_Known_At_Compile_Time (Typ) then
10283 return False;
10285 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10286 return False;
10288 elsif Is_Array_Type (Typ)
10289 and then Present (Packed_Array_Impl_Type (Typ))
10290 then
10291 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10293 -- We could do more here to find other small types ???
10295 else
10296 return True;
10297 end if;
10298 end May_Generate_Large_Temp;
10300 ------------------------
10301 -- Needs_Finalization --
10302 ------------------------
10304 function Needs_Finalization (T : Entity_Id) return Boolean is
10305 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
10306 -- If type is not frozen yet, check explicitly among its components,
10307 -- because the Has_Controlled_Component flag is not necessarily set.
10309 -----------------------------------
10310 -- Has_Some_Controlled_Component --
10311 -----------------------------------
10313 function Has_Some_Controlled_Component
10314 (Rec : Entity_Id) return Boolean
10316 Comp : Entity_Id;
10318 begin
10319 if Has_Controlled_Component (Rec) then
10320 return True;
10322 elsif not Is_Frozen (Rec) then
10323 if Is_Record_Type (Rec) then
10324 Comp := First_Entity (Rec);
10326 while Present (Comp) loop
10327 if not Is_Type (Comp)
10328 and then Needs_Finalization (Etype (Comp))
10329 then
10330 return True;
10331 end if;
10333 Next_Entity (Comp);
10334 end loop;
10336 return False;
10338 else
10339 return
10340 Is_Array_Type (Rec)
10341 and then Needs_Finalization (Component_Type (Rec));
10342 end if;
10343 else
10344 return False;
10345 end if;
10346 end Has_Some_Controlled_Component;
10348 -- Start of processing for Needs_Finalization
10350 begin
10351 -- Certain run-time configurations and targets do not provide support
10352 -- for controlled types.
10354 if Restriction_Active (No_Finalization) then
10355 return False;
10357 -- C++ types are not considered controlled. It is assumed that the
10358 -- non-Ada side will handle their clean up.
10360 elsif Convention (T) = Convention_CPP then
10361 return False;
10363 -- Never needs finalization if Disable_Controlled set
10365 elsif Disable_Controlled (T) then
10366 return False;
10368 elsif Is_Class_Wide_Type (T) and then Disable_Controlled (Etype (T)) then
10369 return False;
10371 else
10372 -- Class-wide types are treated as controlled because derivations
10373 -- from the root type can introduce controlled components.
10375 return
10376 Is_Class_Wide_Type (T)
10377 or else Is_Controlled (T)
10378 or else Has_Some_Controlled_Component (T)
10379 or else
10380 (Is_Concurrent_Type (T)
10381 and then Present (Corresponding_Record_Type (T))
10382 and then Needs_Finalization (Corresponding_Record_Type (T)));
10383 end if;
10384 end Needs_Finalization;
10386 ----------------------------
10387 -- Needs_Constant_Address --
10388 ----------------------------
10390 function Needs_Constant_Address
10391 (Decl : Node_Id;
10392 Typ : Entity_Id) return Boolean
10394 begin
10396 -- If we have no initialization of any kind, then we don't need to place
10397 -- any restrictions on the address clause, because the object will be
10398 -- elaborated after the address clause is evaluated. This happens if the
10399 -- declaration has no initial expression, or the type has no implicit
10400 -- initialization, or the object is imported.
10402 -- The same holds for all initialized scalar types and all access types.
10403 -- Packed bit arrays of size up to 64 are represented using a modular
10404 -- type with an initialization (to zero) and can be processed like other
10405 -- initialized scalar types.
10407 -- If the type is controlled, code to attach the object to a
10408 -- finalization chain is generated at the point of declaration, and
10409 -- therefore the elaboration of the object cannot be delayed: the
10410 -- address expression must be a constant.
10412 if No (Expression (Decl))
10413 and then not Needs_Finalization (Typ)
10414 and then
10415 (not Has_Non_Null_Base_Init_Proc (Typ)
10416 or else Is_Imported (Defining_Identifier (Decl)))
10417 then
10418 return False;
10420 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10421 or else Is_Access_Type (Typ)
10422 or else
10423 (Is_Bit_Packed_Array (Typ)
10424 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10425 then
10426 return False;
10428 else
10430 -- Otherwise, we require the address clause to be constant because
10431 -- the call to the initialization procedure (or the attach code) has
10432 -- to happen at the point of the declaration.
10434 -- Actually the IP call has been moved to the freeze actions anyway,
10435 -- so maybe we can relax this restriction???
10437 return True;
10438 end if;
10439 end Needs_Constant_Address;
10441 ----------------------------
10442 -- New_Class_Wide_Subtype --
10443 ----------------------------
10445 function New_Class_Wide_Subtype
10446 (CW_Typ : Entity_Id;
10447 N : Node_Id) return Entity_Id
10449 Res : constant Entity_Id := Create_Itype (E_Void, N);
10450 Res_Name : constant Name_Id := Chars (Res);
10451 Res_Scope : constant Entity_Id := Scope (Res);
10453 begin
10454 Copy_Node (CW_Typ, Res);
10455 Set_Comes_From_Source (Res, False);
10456 Set_Sloc (Res, Sloc (N));
10457 Set_Is_Itype (Res);
10458 Set_Associated_Node_For_Itype (Res, N);
10459 Set_Is_Public (Res, False); -- By default, may be changed below.
10460 Set_Public_Status (Res);
10461 Set_Chars (Res, Res_Name);
10462 Set_Scope (Res, Res_Scope);
10463 Set_Ekind (Res, E_Class_Wide_Subtype);
10464 Set_Next_Entity (Res, Empty);
10465 Set_Etype (Res, Base_Type (CW_Typ));
10466 Set_Is_Frozen (Res, False);
10467 Set_Freeze_Node (Res, Empty);
10468 return (Res);
10469 end New_Class_Wide_Subtype;
10471 --------------------------------
10472 -- Non_Limited_Designated_Type --
10473 ---------------------------------
10475 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10476 Desig : constant Entity_Id := Designated_Type (T);
10477 begin
10478 if Has_Non_Limited_View (Desig) then
10479 return Non_Limited_View (Desig);
10480 else
10481 return Desig;
10482 end if;
10483 end Non_Limited_Designated_Type;
10485 -----------------------------------
10486 -- OK_To_Do_Constant_Replacement --
10487 -----------------------------------
10489 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10490 ES : constant Entity_Id := Scope (E);
10491 CS : Entity_Id;
10493 begin
10494 -- Do not replace statically allocated objects, because they may be
10495 -- modified outside the current scope.
10497 if Is_Statically_Allocated (E) then
10498 return False;
10500 -- Do not replace aliased or volatile objects, since we don't know what
10501 -- else might change the value.
10503 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10504 return False;
10506 -- Debug flag -gnatdM disconnects this optimization
10508 elsif Debug_Flag_MM then
10509 return False;
10511 -- Otherwise check scopes
10513 else
10514 CS := Current_Scope;
10516 loop
10517 -- If we are in right scope, replacement is safe
10519 if CS = ES then
10520 return True;
10522 -- Packages do not affect the determination of safety
10524 elsif Ekind (CS) = E_Package then
10525 exit when CS = Standard_Standard;
10526 CS := Scope (CS);
10528 -- Blocks do not affect the determination of safety
10530 elsif Ekind (CS) = E_Block then
10531 CS := Scope (CS);
10533 -- Loops do not affect the determination of safety. Note that we
10534 -- kill all current values on entry to a loop, so we are just
10535 -- talking about processing within a loop here.
10537 elsif Ekind (CS) = E_Loop then
10538 CS := Scope (CS);
10540 -- Otherwise, the reference is dubious, and we cannot be sure that
10541 -- it is safe to do the replacement.
10543 else
10544 exit;
10545 end if;
10546 end loop;
10548 return False;
10549 end if;
10550 end OK_To_Do_Constant_Replacement;
10552 ------------------------------------
10553 -- Possible_Bit_Aligned_Component --
10554 ------------------------------------
10556 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10557 begin
10558 -- Do not process an unanalyzed node because it is not yet decorated and
10559 -- most checks performed below will fail.
10561 if not Analyzed (N) then
10562 return False;
10563 end if;
10565 case Nkind (N) is
10567 -- Case of indexed component
10569 when N_Indexed_Component =>
10570 declare
10571 P : constant Node_Id := Prefix (N);
10572 Ptyp : constant Entity_Id := Etype (P);
10574 begin
10575 -- If we know the component size and it is less than 64, then
10576 -- we are definitely OK. The back end always does assignment of
10577 -- misaligned small objects correctly.
10579 if Known_Static_Component_Size (Ptyp)
10580 and then Component_Size (Ptyp) <= 64
10581 then
10582 return False;
10584 -- Otherwise, we need to test the prefix, to see if we are
10585 -- indexing from a possibly unaligned component.
10587 else
10588 return Possible_Bit_Aligned_Component (P);
10589 end if;
10590 end;
10592 -- Case of selected component
10594 when N_Selected_Component =>
10595 declare
10596 P : constant Node_Id := Prefix (N);
10597 Comp : constant Entity_Id := Entity (Selector_Name (N));
10599 begin
10600 -- If there is no component clause, then we are in the clear
10601 -- since the back end will never misalign a large component
10602 -- unless it is forced to do so. In the clear means we need
10603 -- only the recursive test on the prefix.
10605 if Component_May_Be_Bit_Aligned (Comp) then
10606 return True;
10607 else
10608 return Possible_Bit_Aligned_Component (P);
10609 end if;
10610 end;
10612 -- For a slice, test the prefix, if that is possibly misaligned,
10613 -- then for sure the slice is.
10615 when N_Slice =>
10616 return Possible_Bit_Aligned_Component (Prefix (N));
10618 -- For an unchecked conversion, check whether the expression may
10619 -- be bit-aligned.
10621 when N_Unchecked_Type_Conversion =>
10622 return Possible_Bit_Aligned_Component (Expression (N));
10624 -- If we have none of the above, it means that we have fallen off the
10625 -- top testing prefixes recursively, and we now have a stand alone
10626 -- object, where we don't have a problem, unless this is a renaming,
10627 -- in which case we need to look into the renamed object.
10629 when others =>
10630 if Is_Entity_Name (N)
10631 and then Present (Renamed_Object (Entity (N)))
10632 then
10633 return
10634 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10635 else
10636 return False;
10637 end if;
10638 end case;
10639 end Possible_Bit_Aligned_Component;
10641 -----------------------------------------------
10642 -- Process_Statements_For_Controlled_Objects --
10643 -----------------------------------------------
10645 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10646 Loc : constant Source_Ptr := Sloc (N);
10648 function Are_Wrapped (L : List_Id) return Boolean;
10649 -- Determine whether list L contains only one statement which is a block
10651 function Wrap_Statements_In_Block
10652 (L : List_Id;
10653 Scop : Entity_Id := Current_Scope) return Node_Id;
10654 -- Given a list of statements L, wrap it in a block statement and return
10655 -- the generated node. Scop is either the current scope or the scope of
10656 -- the context (if applicable).
10658 -----------------
10659 -- Are_Wrapped --
10660 -----------------
10662 function Are_Wrapped (L : List_Id) return Boolean is
10663 Stmt : constant Node_Id := First (L);
10664 begin
10665 return
10666 Present (Stmt)
10667 and then No (Next (Stmt))
10668 and then Nkind (Stmt) = N_Block_Statement;
10669 end Are_Wrapped;
10671 ------------------------------
10672 -- Wrap_Statements_In_Block --
10673 ------------------------------
10675 function Wrap_Statements_In_Block
10676 (L : List_Id;
10677 Scop : Entity_Id := Current_Scope) return Node_Id
10679 Block_Id : Entity_Id;
10680 Block_Nod : Node_Id;
10681 Iter_Loop : Entity_Id;
10683 begin
10684 Block_Nod :=
10685 Make_Block_Statement (Loc,
10686 Declarations => No_List,
10687 Handled_Statement_Sequence =>
10688 Make_Handled_Sequence_Of_Statements (Loc,
10689 Statements => L));
10691 -- Create a label for the block in case the block needs to manage the
10692 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10694 Add_Block_Identifier (Block_Nod, Block_Id);
10696 -- When wrapping the statements of an iterator loop, check whether
10697 -- the loop requires secondary stack management and if so, propagate
10698 -- the appropriate flags to the block. This ensures that the cursor
10699 -- is properly cleaned up at each iteration of the loop.
10701 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10703 if Present (Iter_Loop) then
10704 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10706 -- Secondary stack reclamation is suppressed when the associated
10707 -- iterator loop contains a return statement which uses the stack.
10709 Set_Sec_Stack_Needed_For_Return
10710 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10711 end if;
10713 return Block_Nod;
10714 end Wrap_Statements_In_Block;
10716 -- Local variables
10718 Block : Node_Id;
10720 -- Start of processing for Process_Statements_For_Controlled_Objects
10722 begin
10723 -- Whenever a non-handled statement list is wrapped in a block, the
10724 -- block must be explicitly analyzed to redecorate all entities in the
10725 -- list and ensure that a finalizer is properly built.
10727 case Nkind (N) is
10728 when N_Conditional_Entry_Call
10729 | N_Elsif_Part
10730 | N_If_Statement
10731 | N_Selective_Accept
10733 -- Check the "then statements" for elsif parts and if statements
10735 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10736 and then not Is_Empty_List (Then_Statements (N))
10737 and then not Are_Wrapped (Then_Statements (N))
10738 and then Requires_Cleanup_Actions
10739 (Then_Statements (N), False, False)
10740 then
10741 Block := Wrap_Statements_In_Block (Then_Statements (N));
10742 Set_Then_Statements (N, New_List (Block));
10744 Analyze (Block);
10745 end if;
10747 -- Check the "else statements" for conditional entry calls, if
10748 -- statements and selective accepts.
10750 if Nkind_In (N, N_Conditional_Entry_Call,
10751 N_If_Statement,
10752 N_Selective_Accept)
10753 and then not Is_Empty_List (Else_Statements (N))
10754 and then not Are_Wrapped (Else_Statements (N))
10755 and then Requires_Cleanup_Actions
10756 (Else_Statements (N), False, False)
10757 then
10758 Block := Wrap_Statements_In_Block (Else_Statements (N));
10759 Set_Else_Statements (N, New_List (Block));
10761 Analyze (Block);
10762 end if;
10764 when N_Abortable_Part
10765 | N_Accept_Alternative
10766 | N_Case_Statement_Alternative
10767 | N_Delay_Alternative
10768 | N_Entry_Call_Alternative
10769 | N_Exception_Handler
10770 | N_Loop_Statement
10771 | N_Triggering_Alternative
10773 if not Is_Empty_List (Statements (N))
10774 and then not Are_Wrapped (Statements (N))
10775 and then Requires_Cleanup_Actions (Statements (N), False, False)
10776 then
10777 if Nkind (N) = N_Loop_Statement
10778 and then Present (Identifier (N))
10779 then
10780 Block :=
10781 Wrap_Statements_In_Block
10782 (L => Statements (N),
10783 Scop => Entity (Identifier (N)));
10784 else
10785 Block := Wrap_Statements_In_Block (Statements (N));
10786 end if;
10788 Set_Statements (N, New_List (Block));
10789 Analyze (Block);
10790 end if;
10792 when others =>
10793 null;
10794 end case;
10795 end Process_Statements_For_Controlled_Objects;
10797 ------------------
10798 -- Power_Of_Two --
10799 ------------------
10801 function Power_Of_Two (N : Node_Id) return Nat is
10802 Typ : constant Entity_Id := Etype (N);
10803 pragma Assert (Is_Integer_Type (Typ));
10805 Siz : constant Nat := UI_To_Int (Esize (Typ));
10806 Val : Uint;
10808 begin
10809 if not Compile_Time_Known_Value (N) then
10810 return 0;
10812 else
10813 Val := Expr_Value (N);
10814 for J in 1 .. Siz - 1 loop
10815 if Val = Uint_2 ** J then
10816 return J;
10817 end if;
10818 end loop;
10820 return 0;
10821 end if;
10822 end Power_Of_Two;
10824 ----------------------
10825 -- Remove_Init_Call --
10826 ----------------------
10828 function Remove_Init_Call
10829 (Var : Entity_Id;
10830 Rep_Clause : Node_Id) return Node_Id
10832 Par : constant Node_Id := Parent (Var);
10833 Typ : constant Entity_Id := Etype (Var);
10835 Init_Proc : Entity_Id;
10836 -- Initialization procedure for Typ
10838 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
10839 -- Look for init call for Var starting at From and scanning the
10840 -- enclosing list until Rep_Clause or the end of the list is reached.
10842 ----------------------------
10843 -- Find_Init_Call_In_List --
10844 ----------------------------
10846 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
10847 Init_Call : Node_Id;
10849 begin
10850 Init_Call := From;
10851 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
10852 if Nkind (Init_Call) = N_Procedure_Call_Statement
10853 and then Is_Entity_Name (Name (Init_Call))
10854 and then Entity (Name (Init_Call)) = Init_Proc
10855 then
10856 return Init_Call;
10857 end if;
10859 Next (Init_Call);
10860 end loop;
10862 return Empty;
10863 end Find_Init_Call_In_List;
10865 Init_Call : Node_Id;
10867 -- Start of processing for Find_Init_Call
10869 begin
10870 if Present (Initialization_Statements (Var)) then
10871 Init_Call := Initialization_Statements (Var);
10872 Set_Initialization_Statements (Var, Empty);
10874 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
10876 -- No init proc for the type, so obviously no call to be found
10878 return Empty;
10880 else
10881 -- We might be able to handle other cases below by just properly
10882 -- setting Initialization_Statements at the point where the init proc
10883 -- call is generated???
10885 Init_Proc := Base_Init_Proc (Typ);
10887 -- First scan the list containing the declaration of Var
10889 Init_Call := Find_Init_Call_In_List (From => Next (Par));
10891 -- If not found, also look on Var's freeze actions list, if any,
10892 -- since the init call may have been moved there (case of an address
10893 -- clause applying to Var).
10895 if No (Init_Call) and then Present (Freeze_Node (Var)) then
10896 Init_Call :=
10897 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
10898 end if;
10900 -- If the initialization call has actuals that use the secondary
10901 -- stack, the call may have been wrapped into a temporary block, in
10902 -- which case the block itself has to be removed.
10904 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
10905 declare
10906 Blk : constant Node_Id := Next (Par);
10907 begin
10908 if Present
10909 (Find_Init_Call_In_List
10910 (First (Statements (Handled_Statement_Sequence (Blk)))))
10911 then
10912 Init_Call := Blk;
10913 end if;
10914 end;
10915 end if;
10916 end if;
10918 if Present (Init_Call) then
10919 Remove (Init_Call);
10920 end if;
10921 return Init_Call;
10922 end Remove_Init_Call;
10924 -------------------------
10925 -- Remove_Side_Effects --
10926 -------------------------
10928 procedure Remove_Side_Effects
10929 (Exp : Node_Id;
10930 Name_Req : Boolean := False;
10931 Renaming_Req : Boolean := False;
10932 Variable_Ref : Boolean := False;
10933 Related_Id : Entity_Id := Empty;
10934 Is_Low_Bound : Boolean := False;
10935 Is_High_Bound : Boolean := False;
10936 Check_Side_Effects : Boolean := True)
10938 function Build_Temporary
10939 (Loc : Source_Ptr;
10940 Id : Character;
10941 Related_Nod : Node_Id := Empty) return Entity_Id;
10942 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
10943 -- is present (xxx is taken from the Chars field of Related_Nod),
10944 -- otherwise it generates an internal temporary.
10946 ---------------------
10947 -- Build_Temporary --
10948 ---------------------
10950 function Build_Temporary
10951 (Loc : Source_Ptr;
10952 Id : Character;
10953 Related_Nod : Node_Id := Empty) return Entity_Id
10955 Temp_Nam : Name_Id;
10957 begin
10958 -- The context requires an external symbol
10960 if Present (Related_Id) then
10961 if Is_Low_Bound then
10962 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
10963 else pragma Assert (Is_High_Bound);
10964 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
10965 end if;
10967 return Make_Defining_Identifier (Loc, Temp_Nam);
10969 -- Otherwise generate an internal temporary
10971 else
10972 return Make_Temporary (Loc, Id, Related_Nod);
10973 end if;
10974 end Build_Temporary;
10976 -- Local variables
10978 Loc : constant Source_Ptr := Sloc (Exp);
10979 Exp_Type : constant Entity_Id := Etype (Exp);
10980 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
10981 Def_Id : Entity_Id;
10982 E : Node_Id;
10983 New_Exp : Node_Id;
10984 Ptr_Typ_Decl : Node_Id;
10985 Ref_Type : Entity_Id;
10986 Res : Node_Id;
10988 -- Start of processing for Remove_Side_Effects
10990 begin
10991 -- Handle cases in which there is nothing to do. In GNATprove mode,
10992 -- removal of side effects is useful for the light expansion of
10993 -- renamings. This removal should only occur when not inside a
10994 -- generic and not doing a pre-analysis.
10996 if not Expander_Active
10997 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
10998 then
10999 return;
11001 -- Cannot generate temporaries if the invocation to remove side effects
11002 -- was issued too early and the type of the expression is not resolved
11003 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11004 -- Remove_Side_Effects).
11006 elsif No (Exp_Type)
11007 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11008 then
11009 return;
11011 -- Nothing to do if prior expansion determined that a function call does
11012 -- not require side effect removal.
11014 elsif Nkind (Exp) = N_Function_Call
11015 and then No_Side_Effect_Removal (Exp)
11016 then
11017 return;
11019 -- No action needed for side-effect free expressions
11021 elsif Check_Side_Effects
11022 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11023 then
11024 return;
11025 end if;
11027 -- The remaining processing is done with all checks suppressed
11029 -- Note: from now on, don't use return statements, instead do a goto
11030 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11032 Scope_Suppress.Suppress := (others => True);
11034 -- If this is an elementary or a small not by-reference record type, and
11035 -- we need to capture the value, just make a constant; this is cheap and
11036 -- objects of both kinds of types can be bit aligned, so it might not be
11037 -- possible to generate a reference to them. Likewise if this is not a
11038 -- name reference, except for a type conversion because we would enter
11039 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11040 -- type has predicates (and type conversions need a specific treatment
11041 -- anyway, see below). Also do it if we have a volatile reference and
11042 -- Name_Req is not set (see comments for Side_Effect_Free).
11044 if (Is_Elementary_Type (Exp_Type)
11045 or else (Is_Record_Type (Exp_Type)
11046 and then Known_Static_RM_Size (Exp_Type)
11047 and then RM_Size (Exp_Type) <= 64
11048 and then not Has_Discriminants (Exp_Type)
11049 and then not Is_By_Reference_Type (Exp_Type)))
11050 and then (Variable_Ref
11051 or else (not Is_Name_Reference (Exp)
11052 and then Nkind (Exp) /= N_Type_Conversion)
11053 or else (not Name_Req
11054 and then Is_Volatile_Reference (Exp)))
11055 then
11056 Def_Id := Build_Temporary (Loc, 'R', Exp);
11057 Set_Etype (Def_Id, Exp_Type);
11058 Res := New_Occurrence_Of (Def_Id, Loc);
11060 -- If the expression is a packed reference, it must be reanalyzed and
11061 -- expanded, depending on context. This is the case for actuals where
11062 -- a constraint check may capture the actual before expansion of the
11063 -- call is complete.
11065 if Nkind (Exp) = N_Indexed_Component
11066 and then Is_Packed (Etype (Prefix (Exp)))
11067 then
11068 Set_Analyzed (Exp, False);
11069 Set_Analyzed (Prefix (Exp), False);
11070 end if;
11072 -- Generate:
11073 -- Rnn : Exp_Type renames Expr;
11075 if Renaming_Req then
11076 E :=
11077 Make_Object_Renaming_Declaration (Loc,
11078 Defining_Identifier => Def_Id,
11079 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11080 Name => Relocate_Node (Exp));
11082 -- Generate:
11083 -- Rnn : constant Exp_Type := Expr;
11085 else
11086 E :=
11087 Make_Object_Declaration (Loc,
11088 Defining_Identifier => Def_Id,
11089 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11090 Constant_Present => True,
11091 Expression => Relocate_Node (Exp));
11093 Set_Assignment_OK (E);
11094 end if;
11096 Insert_Action (Exp, E);
11098 -- If the expression has the form v.all then we can just capture the
11099 -- pointer, and then do an explicit dereference on the result, but
11100 -- this is not right if this is a volatile reference.
11102 elsif Nkind (Exp) = N_Explicit_Dereference
11103 and then not Is_Volatile_Reference (Exp)
11104 then
11105 Def_Id := Build_Temporary (Loc, 'R', Exp);
11106 Res :=
11107 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11109 Insert_Action (Exp,
11110 Make_Object_Declaration (Loc,
11111 Defining_Identifier => Def_Id,
11112 Object_Definition =>
11113 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11114 Constant_Present => True,
11115 Expression => Relocate_Node (Prefix (Exp))));
11117 -- Similar processing for an unchecked conversion of an expression of
11118 -- the form v.all, where we want the same kind of treatment.
11120 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11121 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11122 then
11123 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11124 goto Leave;
11126 -- If this is a type conversion, leave the type conversion and remove
11127 -- the side effects in the expression. This is important in several
11128 -- circumstances: for change of representations, and also when this is a
11129 -- view conversion to a smaller object, where gigi can end up creating
11130 -- its own temporary of the wrong size.
11132 elsif Nkind (Exp) = N_Type_Conversion then
11133 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11135 -- Generating C code the type conversion of an access to constrained
11136 -- array type into an access to unconstrained array type involves
11137 -- initializing a fat pointer and the expression must be free of
11138 -- side effects to safely compute its bounds.
11140 if Modify_Tree_For_C
11141 and then Is_Access_Type (Etype (Exp))
11142 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11143 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11144 then
11145 Def_Id := Build_Temporary (Loc, 'R', Exp);
11146 Set_Etype (Def_Id, Exp_Type);
11147 Res := New_Occurrence_Of (Def_Id, Loc);
11149 Insert_Action (Exp,
11150 Make_Object_Declaration (Loc,
11151 Defining_Identifier => Def_Id,
11152 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11153 Constant_Present => True,
11154 Expression => Relocate_Node (Exp)));
11155 else
11156 goto Leave;
11157 end if;
11159 -- If this is an unchecked conversion that Gigi can't handle, make
11160 -- a copy or a use a renaming to capture the value.
11162 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11163 and then not Safe_Unchecked_Type_Conversion (Exp)
11164 then
11165 if CW_Or_Has_Controlled_Part (Exp_Type) then
11167 -- Use a renaming to capture the expression, rather than create
11168 -- a controlled temporary.
11170 Def_Id := Build_Temporary (Loc, 'R', Exp);
11171 Res := New_Occurrence_Of (Def_Id, Loc);
11173 Insert_Action (Exp,
11174 Make_Object_Renaming_Declaration (Loc,
11175 Defining_Identifier => Def_Id,
11176 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11177 Name => Relocate_Node (Exp)));
11179 else
11180 Def_Id := Build_Temporary (Loc, 'R', Exp);
11181 Set_Etype (Def_Id, Exp_Type);
11182 Res := New_Occurrence_Of (Def_Id, Loc);
11184 E :=
11185 Make_Object_Declaration (Loc,
11186 Defining_Identifier => Def_Id,
11187 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11188 Constant_Present => not Is_Variable (Exp),
11189 Expression => Relocate_Node (Exp));
11191 Set_Assignment_OK (E);
11192 Insert_Action (Exp, E);
11193 end if;
11195 -- For expressions that denote names, we can use a renaming scheme.
11196 -- This is needed for correctness in the case of a volatile object of
11197 -- a non-volatile type because the Make_Reference call of the "default"
11198 -- approach would generate an illegal access value (an access value
11199 -- cannot designate such an object - see Analyze_Reference).
11201 elsif Is_Name_Reference (Exp)
11203 -- We skip using this scheme if we have an object of a volatile
11204 -- type and we do not have Name_Req set true (see comments for
11205 -- Side_Effect_Free).
11207 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11208 then
11209 Def_Id := Build_Temporary (Loc, 'R', Exp);
11210 Res := New_Occurrence_Of (Def_Id, Loc);
11212 Insert_Action (Exp,
11213 Make_Object_Renaming_Declaration (Loc,
11214 Defining_Identifier => Def_Id,
11215 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11216 Name => Relocate_Node (Exp)));
11218 -- If this is a packed reference, or a selected component with
11219 -- a non-standard representation, a reference to the temporary
11220 -- will be replaced by a copy of the original expression (see
11221 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11222 -- elaborated by gigi, and is of course not to be replaced in-line
11223 -- by the expression it renames, which would defeat the purpose of
11224 -- removing the side-effect.
11226 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11227 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11228 then
11229 null;
11230 else
11231 Set_Is_Renaming_Of_Object (Def_Id, False);
11232 end if;
11234 -- Avoid generating a variable-sized temporary, by generating the
11235 -- reference just for the function call. The transformation could be
11236 -- refined to apply only when the array component is constrained by a
11237 -- discriminant???
11239 elsif Nkind (Exp) = N_Selected_Component
11240 and then Nkind (Prefix (Exp)) = N_Function_Call
11241 and then Is_Array_Type (Exp_Type)
11242 then
11243 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11244 goto Leave;
11246 -- Otherwise we generate a reference to the expression
11248 else
11249 -- An expression which is in SPARK mode is considered side effect
11250 -- free if the resulting value is captured by a variable or a
11251 -- constant.
11253 if GNATprove_Mode
11254 and then Nkind (Parent (Exp)) = N_Object_Declaration
11255 then
11256 goto Leave;
11258 -- When generating C code we cannot consider side effect free object
11259 -- declarations that have discriminants and are initialized by means
11260 -- of a function call since on this target there is no secondary
11261 -- stack to store the return value and the expander may generate an
11262 -- extra call to the function to compute the discriminant value. In
11263 -- addition, for targets that have secondary stack, the expansion of
11264 -- functions with side effects involves the generation of an access
11265 -- type to capture the return value stored in the secondary stack;
11266 -- by contrast when generating C code such expansion generates an
11267 -- internal object declaration (no access type involved) which must
11268 -- be identified here to avoid entering into a never-ending loop
11269 -- generating internal object declarations.
11271 elsif Modify_Tree_For_C
11272 and then Nkind (Parent (Exp)) = N_Object_Declaration
11273 and then
11274 (Nkind (Exp) /= N_Function_Call
11275 or else not Has_Discriminants (Exp_Type)
11276 or else Is_Internal_Name
11277 (Chars (Defining_Identifier (Parent (Exp)))))
11278 then
11279 goto Leave;
11280 end if;
11282 -- Special processing for function calls that return a limited type.
11283 -- We need to build a declaration that will enable build-in-place
11284 -- expansion of the call. This is not done if the context is already
11285 -- an object declaration, to prevent infinite recursion.
11287 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11288 -- to accommodate functions returning limited objects by reference.
11290 if Ada_Version >= Ada_2005
11291 and then Nkind (Exp) = N_Function_Call
11292 and then Is_Limited_View (Etype (Exp))
11293 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11294 then
11295 declare
11296 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11297 Decl : Node_Id;
11299 begin
11300 Decl :=
11301 Make_Object_Declaration (Loc,
11302 Defining_Identifier => Obj,
11303 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11304 Expression => Relocate_Node (Exp));
11306 Insert_Action (Exp, Decl);
11307 Set_Etype (Obj, Exp_Type);
11308 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11309 goto Leave;
11310 end;
11311 end if;
11313 Def_Id := Build_Temporary (Loc, 'R', Exp);
11315 -- The regular expansion of functions with side effects involves the
11316 -- generation of an access type to capture the return value found on
11317 -- the secondary stack. Since SPARK (and why) cannot process access
11318 -- types, use a different approach which ignores the secondary stack
11319 -- and "copies" the returned object.
11320 -- When generating C code, no need for a 'reference since the
11321 -- secondary stack is not supported.
11323 if GNATprove_Mode or Modify_Tree_For_C then
11324 Res := New_Occurrence_Of (Def_Id, Loc);
11325 Ref_Type := Exp_Type;
11327 -- Regular expansion utilizing an access type and 'reference
11329 else
11330 Res :=
11331 Make_Explicit_Dereference (Loc,
11332 Prefix => New_Occurrence_Of (Def_Id, Loc));
11334 -- Generate:
11335 -- type Ann is access all <Exp_Type>;
11337 Ref_Type := Make_Temporary (Loc, 'A');
11339 Ptr_Typ_Decl :=
11340 Make_Full_Type_Declaration (Loc,
11341 Defining_Identifier => Ref_Type,
11342 Type_Definition =>
11343 Make_Access_To_Object_Definition (Loc,
11344 All_Present => True,
11345 Subtype_Indication =>
11346 New_Occurrence_Of (Exp_Type, Loc)));
11348 Insert_Action (Exp, Ptr_Typ_Decl);
11349 end if;
11351 E := Exp;
11352 if Nkind (E) = N_Explicit_Dereference then
11353 New_Exp := Relocate_Node (Prefix (E));
11355 else
11356 E := Relocate_Node (E);
11358 -- Do not generate a 'reference in SPARK mode or C generation
11359 -- since the access type is not created in the first place.
11361 if GNATprove_Mode or Modify_Tree_For_C then
11362 New_Exp := E;
11364 -- Otherwise generate reference, marking the value as non-null
11365 -- since we know it cannot be null and we don't want a check.
11367 else
11368 New_Exp := Make_Reference (Loc, E);
11369 Set_Is_Known_Non_Null (Def_Id);
11370 end if;
11371 end if;
11373 if Is_Delayed_Aggregate (E) then
11375 -- The expansion of nested aggregates is delayed until the
11376 -- enclosing aggregate is expanded. As aggregates are often
11377 -- qualified, the predicate applies to qualified expressions as
11378 -- well, indicating that the enclosing aggregate has not been
11379 -- expanded yet. At this point the aggregate is part of a
11380 -- stand-alone declaration, and must be fully expanded.
11382 if Nkind (E) = N_Qualified_Expression then
11383 Set_Expansion_Delayed (Expression (E), False);
11384 Set_Analyzed (Expression (E), False);
11385 else
11386 Set_Expansion_Delayed (E, False);
11387 end if;
11389 Set_Analyzed (E, False);
11390 end if;
11392 -- Generating C code of object declarations that have discriminants
11393 -- and are initialized by means of a function call we propagate the
11394 -- discriminants of the parent type to the internally built object.
11395 -- This is needed to avoid generating an extra call to the called
11396 -- function.
11398 -- For example, if we generate here the following declaration, it
11399 -- will be expanded later adding an extra call to evaluate the value
11400 -- of the discriminant (needed to compute the size of the object).
11402 -- type Rec (D : Integer) is ...
11403 -- Obj : constant Rec := SomeFunc;
11405 if Modify_Tree_For_C
11406 and then Nkind (Parent (Exp)) = N_Object_Declaration
11407 and then Has_Discriminants (Exp_Type)
11408 and then Nkind (Exp) = N_Function_Call
11409 then
11410 Insert_Action (Exp,
11411 Make_Object_Declaration (Loc,
11412 Defining_Identifier => Def_Id,
11413 Object_Definition => New_Copy_Tree
11414 (Object_Definition (Parent (Exp))),
11415 Constant_Present => True,
11416 Expression => New_Exp));
11417 else
11418 Insert_Action (Exp,
11419 Make_Object_Declaration (Loc,
11420 Defining_Identifier => Def_Id,
11421 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11422 Constant_Present => True,
11423 Expression => New_Exp));
11424 end if;
11425 end if;
11427 -- Preserve the Assignment_OK flag in all copies, since at least one
11428 -- copy may be used in a context where this flag must be set (otherwise
11429 -- why would the flag be set in the first place).
11431 Set_Assignment_OK (Res, Assignment_OK (Exp));
11433 -- Finally rewrite the original expression and we are done
11435 Rewrite (Exp, Res);
11436 Analyze_And_Resolve (Exp, Exp_Type);
11438 <<Leave>>
11439 Scope_Suppress := Svg_Suppress;
11440 end Remove_Side_Effects;
11442 ------------------------
11443 -- Replace_References --
11444 ------------------------
11446 procedure Replace_References
11447 (Expr : Node_Id;
11448 Par_Typ : Entity_Id;
11449 Deriv_Typ : Entity_Id;
11450 Par_Obj : Entity_Id := Empty;
11451 Deriv_Obj : Entity_Id := Empty)
11453 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11454 -- Determine whether node Ref denotes some component of Deriv_Obj
11456 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11457 -- Substitute a reference to an entity with the corresponding value
11458 -- stored in table Type_Map.
11460 function Type_Of_Formal
11461 (Call : Node_Id;
11462 Actual : Node_Id) return Entity_Id;
11463 -- Find the type of the formal parameter which corresponds to actual
11464 -- parameter Actual in subprogram call Call.
11466 ----------------------
11467 -- Is_Deriv_Obj_Ref --
11468 ----------------------
11470 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11471 Par : constant Node_Id := Parent (Ref);
11473 begin
11474 -- Detect the folowing selected component form:
11476 -- Deriv_Obj.(something)
11478 return
11479 Nkind (Par) = N_Selected_Component
11480 and then Is_Entity_Name (Prefix (Par))
11481 and then Entity (Prefix (Par)) = Deriv_Obj;
11482 end Is_Deriv_Obj_Ref;
11484 -----------------
11485 -- Replace_Ref --
11486 -----------------
11488 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11489 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11490 -- Reset the Controlling_Argument of all function calls that
11491 -- encapsulate node From_Arg.
11493 ----------------------------------
11494 -- Remove_Controlling_Arguments --
11495 ----------------------------------
11497 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11498 Par : Node_Id;
11500 begin
11501 Par := From_Arg;
11502 while Present (Par) loop
11503 if Nkind (Par) = N_Function_Call
11504 and then Present (Controlling_Argument (Par))
11505 then
11506 Set_Controlling_Argument (Par, Empty);
11508 -- Prevent the search from going too far
11510 elsif Is_Body_Or_Package_Declaration (Par) then
11511 exit;
11512 end if;
11514 Par := Parent (Par);
11515 end loop;
11516 end Remove_Controlling_Arguments;
11518 -- Local variables
11520 Context : constant Node_Id := Parent (Ref);
11521 Loc : constant Source_Ptr := Sloc (Ref);
11522 Ref_Id : Entity_Id;
11523 Result : Traverse_Result;
11525 New_Ref : Node_Id;
11526 -- The new reference which is intended to substitute the old one
11528 Old_Ref : Node_Id;
11529 -- The reference designated for replacement. In certain cases this
11530 -- may be a node other than Ref.
11532 Val : Node_Or_Entity_Id;
11533 -- The corresponding value of Ref from the type map
11535 -- Start of processing for Replace_Ref
11537 begin
11538 -- Assume that the input reference is to be replaced and that the
11539 -- traversal should examine the children of the reference.
11541 Old_Ref := Ref;
11542 Result := OK;
11544 -- The input denotes a meaningful reference
11546 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11547 Ref_Id := Entity (Ref);
11548 Val := Type_Map.Get (Ref_Id);
11550 -- The reference has a corresponding value in the type map, a
11551 -- substitution is possible.
11553 if Present (Val) then
11555 -- The reference denotes a discriminant
11557 if Ekind (Ref_Id) = E_Discriminant then
11558 if Nkind (Val) in N_Entity then
11560 -- The value denotes another discriminant. Replace as
11561 -- follows:
11563 -- _object.Discr -> _object.Val
11565 if Ekind (Val) = E_Discriminant then
11566 New_Ref := New_Occurrence_Of (Val, Loc);
11568 -- Otherwise the value denotes the entity of a name which
11569 -- constraints the discriminant. Replace as follows:
11571 -- _object.Discr -> Val
11573 else
11574 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11576 New_Ref := New_Occurrence_Of (Val, Loc);
11577 Old_Ref := Parent (Old_Ref);
11578 end if;
11580 -- Otherwise the value denotes an arbitrary expression which
11581 -- constraints the discriminant. Replace as follows:
11583 -- _object.Discr -> Val
11585 else
11586 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11588 New_Ref := New_Copy_Tree (Val);
11589 Old_Ref := Parent (Old_Ref);
11590 end if;
11592 -- Otherwise the reference denotes a primitive. Replace as
11593 -- follows:
11595 -- Primitive -> Val
11597 else
11598 pragma Assert (Nkind (Val) in N_Entity);
11599 New_Ref := New_Occurrence_Of (Val, Loc);
11600 end if;
11602 -- The reference mentions the _object parameter of the parent
11603 -- type's DIC or type invariant procedure. Replace as follows:
11605 -- _object -> _object
11607 elsif Present (Par_Obj)
11608 and then Present (Deriv_Obj)
11609 and then Ref_Id = Par_Obj
11610 then
11611 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11613 -- The type of the _object parameter is class-wide when the
11614 -- expression comes from an assertion pragma that applies to
11615 -- an abstract parent type or an interface. The class-wide type
11616 -- facilitates the preanalysis of the expression by treating
11617 -- calls to abstract primitives that mention the current
11618 -- instance of the type as dispatching. Once the calls are
11619 -- remapped to invoke overriding or inherited primitives, the
11620 -- calls no longer need to be dispatching. Examine all function
11621 -- calls that encapsulate the _object parameter and reset their
11622 -- Controlling_Argument attribute.
11624 if Is_Class_Wide_Type (Etype (Par_Obj))
11625 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11626 then
11627 Remove_Controlling_Arguments (Old_Ref);
11628 end if;
11630 -- The reference to _object acts as an actual parameter in a
11631 -- subprogram call which may be invoking a primitive of the
11632 -- parent type:
11634 -- Primitive (... _object ...);
11636 -- The parent type primitive may not be overridden nor
11637 -- inherited when it is declared after the derived type
11638 -- definition:
11640 -- type Parent is tagged private;
11641 -- type Child is new Parent with private;
11642 -- procedure Primitive (Obj : Parent);
11644 -- In this scenario the _object parameter is converted to the
11645 -- parent type. Due to complications with partial/full views
11646 -- and view swaps, the parent type is taken from the formal
11647 -- parameter of the subprogram being called.
11649 if Nkind_In (Context, N_Function_Call,
11650 N_Procedure_Call_Statement)
11651 and then No (Type_Map.Get (Entity (Name (Context))))
11652 then
11653 New_Ref :=
11654 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11656 -- Do not process the generated type conversion because
11657 -- both the parent type and the derived type are in the
11658 -- Type_Map table. This will clobber the type conversion
11659 -- by resetting its subtype mark.
11661 Result := Skip;
11662 end if;
11664 -- Otherwise there is nothing to replace
11666 else
11667 New_Ref := Empty;
11668 end if;
11670 if Present (New_Ref) then
11671 Rewrite (Old_Ref, New_Ref);
11673 -- Update the return type when the context of the reference
11674 -- acts as the name of a function call. Note that the update
11675 -- should not be performed when the reference appears as an
11676 -- actual in the call.
11678 if Nkind (Context) = N_Function_Call
11679 and then Name (Context) = Old_Ref
11680 then
11681 Set_Etype (Context, Etype (Val));
11682 end if;
11683 end if;
11684 end if;
11686 -- Reanalyze the reference due to potential replacements
11688 if Nkind (Old_Ref) in N_Has_Etype then
11689 Set_Analyzed (Old_Ref, False);
11690 end if;
11692 return Result;
11693 end Replace_Ref;
11695 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11697 --------------------
11698 -- Type_Of_Formal --
11699 --------------------
11701 function Type_Of_Formal
11702 (Call : Node_Id;
11703 Actual : Node_Id) return Entity_Id
11705 A : Node_Id;
11706 F : Entity_Id;
11708 begin
11709 -- Examine the list of actual and formal parameters in parallel
11711 A := First (Parameter_Associations (Call));
11712 F := First_Formal (Entity (Name (Call)));
11713 while Present (A) and then Present (F) loop
11714 if A = Actual then
11715 return Etype (F);
11716 end if;
11718 Next (A);
11719 Next_Formal (F);
11720 end loop;
11722 -- The actual parameter must always have a corresponding formal
11724 pragma Assert (False);
11726 return Empty;
11727 end Type_Of_Formal;
11729 -- Start of processing for Replace_References
11731 begin
11732 -- Map the attributes of the parent type to the proper corresponding
11733 -- attributes of the derived type.
11735 Map_Types
11736 (Parent_Type => Par_Typ,
11737 Derived_Type => Deriv_Typ);
11739 -- Inspect the input expression and perform substitutions where
11740 -- necessary.
11742 Replace_Refs (Expr);
11743 end Replace_References;
11745 -----------------------------
11746 -- Replace_Type_References --
11747 -----------------------------
11749 procedure Replace_Type_References
11750 (Expr : Node_Id;
11751 Typ : Entity_Id;
11752 Obj_Id : Entity_Id)
11754 procedure Replace_Type_Ref (N : Node_Id);
11755 -- Substitute a single reference of the current instance of type Typ
11756 -- with a reference to Obj_Id.
11758 ----------------------
11759 -- Replace_Type_Ref --
11760 ----------------------
11762 procedure Replace_Type_Ref (N : Node_Id) is
11763 begin
11764 -- Decorate the reference to Typ even though it may be rewritten
11765 -- further down. This is done for two reasons:
11767 -- * ASIS has all necessary semantic information in the original
11768 -- tree.
11770 -- * Routines which examine properties of the Original_Node have
11771 -- some semantic information.
11773 if Nkind (N) = N_Identifier then
11774 Set_Entity (N, Typ);
11775 Set_Etype (N, Typ);
11777 elsif Nkind (N) = N_Selected_Component then
11778 Analyze (Prefix (N));
11779 Set_Entity (Selector_Name (N), Typ);
11780 Set_Etype (Selector_Name (N), Typ);
11781 end if;
11783 -- Perform the following substitution:
11785 -- Typ --> _object
11787 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
11788 Set_Comes_From_Source (N, True);
11789 end Replace_Type_Ref;
11791 procedure Replace_Type_Refs is
11792 new Replace_Type_References_Generic (Replace_Type_Ref);
11794 -- Start of processing for Replace_Type_References
11796 begin
11797 Replace_Type_Refs (Expr, Typ);
11798 end Replace_Type_References;
11800 ---------------------------
11801 -- Represented_As_Scalar --
11802 ---------------------------
11804 function Represented_As_Scalar (T : Entity_Id) return Boolean is
11805 UT : constant Entity_Id := Underlying_Type (T);
11806 begin
11807 return Is_Scalar_Type (UT)
11808 or else (Is_Bit_Packed_Array (UT)
11809 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
11810 end Represented_As_Scalar;
11812 ------------------------------
11813 -- Requires_Cleanup_Actions --
11814 ------------------------------
11816 function Requires_Cleanup_Actions
11817 (N : Node_Id;
11818 Lib_Level : Boolean) return Boolean
11820 At_Lib_Level : constant Boolean :=
11821 Lib_Level
11822 and then Nkind_In (N, N_Package_Body,
11823 N_Package_Specification);
11824 -- N is at the library level if the top-most context is a package and
11825 -- the path taken to reach N does not inlcude non-package constructs.
11827 begin
11828 case Nkind (N) is
11829 when N_Accept_Statement
11830 | N_Block_Statement
11831 | N_Entry_Body
11832 | N_Package_Body
11833 | N_Protected_Body
11834 | N_Subprogram_Body
11835 | N_Task_Body
11837 return
11838 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
11839 or else
11840 (Present (Handled_Statement_Sequence (N))
11841 and then
11842 Requires_Cleanup_Actions
11843 (Statements (Handled_Statement_Sequence (N)),
11844 At_Lib_Level, True));
11846 when N_Package_Specification =>
11847 return
11848 Requires_Cleanup_Actions
11849 (Visible_Declarations (N), At_Lib_Level, True)
11850 or else
11851 Requires_Cleanup_Actions
11852 (Private_Declarations (N), At_Lib_Level, True);
11854 when others =>
11855 return False;
11856 end case;
11857 end Requires_Cleanup_Actions;
11859 ------------------------------
11860 -- Requires_Cleanup_Actions --
11861 ------------------------------
11863 function Requires_Cleanup_Actions
11864 (L : List_Id;
11865 Lib_Level : Boolean;
11866 Nested_Constructs : Boolean) return Boolean
11868 Decl : Node_Id;
11869 Expr : Node_Id;
11870 Obj_Id : Entity_Id;
11871 Obj_Typ : Entity_Id;
11872 Pack_Id : Entity_Id;
11873 Typ : Entity_Id;
11875 begin
11876 if No (L)
11877 or else Is_Empty_List (L)
11878 then
11879 return False;
11880 end if;
11882 Decl := First (L);
11883 while Present (Decl) loop
11885 -- Library-level tagged types
11887 if Nkind (Decl) = N_Full_Type_Declaration then
11888 Typ := Defining_Identifier (Decl);
11890 -- Ignored Ghost types do not need any cleanup actions because
11891 -- they will not appear in the final tree.
11893 if Is_Ignored_Ghost_Entity (Typ) then
11894 null;
11896 elsif Is_Tagged_Type (Typ)
11897 and then Is_Library_Level_Entity (Typ)
11898 and then Convention (Typ) = Convention_Ada
11899 and then Present (Access_Disp_Table (Typ))
11900 and then RTE_Available (RE_Unregister_Tag)
11901 and then not Is_Abstract_Type (Typ)
11902 and then not No_Run_Time_Mode
11903 then
11904 return True;
11905 end if;
11907 -- Regular object declarations
11909 elsif Nkind (Decl) = N_Object_Declaration then
11910 Obj_Id := Defining_Identifier (Decl);
11911 Obj_Typ := Base_Type (Etype (Obj_Id));
11912 Expr := Expression (Decl);
11914 -- Bypass any form of processing for objects which have their
11915 -- finalization disabled. This applies only to objects at the
11916 -- library level.
11918 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
11919 null;
11921 -- Finalization of transient objects are treated separately in
11922 -- order to handle sensitive cases. These include:
11924 -- * Aggregate expansion
11925 -- * If, case, and expression with actions expansion
11926 -- * Transient scopes
11928 -- If one of those contexts has marked the transient object as
11929 -- ignored, do not generate finalization actions for it.
11931 elsif Is_Finalized_Transient (Obj_Id)
11932 or else Is_Ignored_Transient (Obj_Id)
11933 then
11934 null;
11936 -- Ignored Ghost objects do not need any cleanup actions because
11937 -- they will not appear in the final tree.
11939 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
11940 null;
11942 -- The expansion of iterator loops generates an object declaration
11943 -- where the Ekind is explicitly set to loop parameter. This is to
11944 -- ensure that the loop parameter behaves as a constant from user
11945 -- code point of view. Such object are never controlled and do not
11946 -- require cleanup actions. An iterator loop over a container of
11947 -- controlled objects does not produce such object declarations.
11949 elsif Ekind (Obj_Id) = E_Loop_Parameter then
11950 return False;
11952 -- The object is of the form:
11953 -- Obj : [constant] Typ [:= Expr];
11955 -- Do not process tag-to-class-wide conversions because they do
11956 -- not yield an object. Do not process the incomplete view of a
11957 -- deferred constant. Note that an object initialized by means
11958 -- of a build-in-place function call may appear as a deferred
11959 -- constant after expansion activities. These kinds of objects
11960 -- must be finalized.
11962 elsif not Is_Imported (Obj_Id)
11963 and then Needs_Finalization (Obj_Typ)
11964 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
11965 and then not (Ekind (Obj_Id) = E_Constant
11966 and then not Has_Completion (Obj_Id)
11967 and then No (BIP_Initialization_Call (Obj_Id)))
11968 then
11969 return True;
11971 -- The object is of the form:
11972 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
11974 -- Obj : Access_Typ :=
11975 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
11977 elsif Is_Access_Type (Obj_Typ)
11978 and then Needs_Finalization
11979 (Available_View (Designated_Type (Obj_Typ)))
11980 and then Present (Expr)
11981 and then
11982 (Is_Secondary_Stack_BIP_Func_Call (Expr)
11983 or else
11984 (Is_Non_BIP_Func_Call (Expr)
11985 and then not Is_Related_To_Func_Return (Obj_Id)))
11986 then
11987 return True;
11989 -- Processing for "hook" objects generated for transient objects
11990 -- declared inside an Expression_With_Actions.
11992 elsif Is_Access_Type (Obj_Typ)
11993 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
11994 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
11995 N_Object_Declaration
11996 then
11997 return True;
11999 -- Processing for intermediate results of if expressions where
12000 -- one of the alternatives uses a controlled function call.
12002 elsif Is_Access_Type (Obj_Typ)
12003 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12004 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12005 N_Defining_Identifier
12006 and then Present (Expr)
12007 and then Nkind (Expr) = N_Null
12008 then
12009 return True;
12011 -- Simple protected objects which use type System.Tasking.
12012 -- Protected_Objects.Protection to manage their locks should be
12013 -- treated as controlled since they require manual cleanup.
12015 elsif Ekind (Obj_Id) = E_Variable
12016 and then (Is_Simple_Protected_Type (Obj_Typ)
12017 or else Has_Simple_Protected_Object (Obj_Typ))
12018 then
12019 return True;
12020 end if;
12022 -- Specific cases of object renamings
12024 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12025 Obj_Id := Defining_Identifier (Decl);
12026 Obj_Typ := Base_Type (Etype (Obj_Id));
12028 -- Bypass any form of processing for objects which have their
12029 -- finalization disabled. This applies only to objects at the
12030 -- library level.
12032 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12033 null;
12035 -- Ignored Ghost object renamings do not need any cleanup actions
12036 -- because they will not appear in the final tree.
12038 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12039 null;
12041 -- Return object of a build-in-place function. This case is
12042 -- recognized and marked by the expansion of an extended return
12043 -- statement (see Expand_N_Extended_Return_Statement).
12045 elsif Needs_Finalization (Obj_Typ)
12046 and then Is_Return_Object (Obj_Id)
12047 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12048 then
12049 return True;
12051 -- Detect a case where a source object has been initialized by
12052 -- a controlled function call or another object which was later
12053 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12055 -- Obj1 : CW_Type := Src_Obj;
12056 -- Obj2 : CW_Type := Function_Call (...);
12058 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12059 -- Tmp : ... := Function_Call (...)'reference;
12060 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12062 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12063 return True;
12064 end if;
12066 -- Inspect the freeze node of an access-to-controlled type and look
12067 -- for a delayed finalization master. This case arises when the
12068 -- freeze actions are inserted at a later time than the expansion of
12069 -- the context. Since Build_Finalizer is never called on a single
12070 -- construct twice, the master will be ultimately left out and never
12071 -- finalized. This is also needed for freeze actions of designated
12072 -- types themselves, since in some cases the finalization master is
12073 -- associated with a designated type's freeze node rather than that
12074 -- of the access type (see handling for freeze actions in
12075 -- Build_Finalization_Master).
12077 elsif Nkind (Decl) = N_Freeze_Entity
12078 and then Present (Actions (Decl))
12079 then
12080 Typ := Entity (Decl);
12082 -- Freeze nodes for ignored Ghost types do not need cleanup
12083 -- actions because they will never appear in the final tree.
12085 if Is_Ignored_Ghost_Entity (Typ) then
12086 null;
12088 elsif ((Is_Access_Type (Typ)
12089 and then not Is_Access_Subprogram_Type (Typ)
12090 and then Needs_Finalization
12091 (Available_View (Designated_Type (Typ))))
12092 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12093 and then Requires_Cleanup_Actions
12094 (Actions (Decl), Lib_Level, Nested_Constructs)
12095 then
12096 return True;
12097 end if;
12099 -- Nested package declarations
12101 elsif Nested_Constructs
12102 and then Nkind (Decl) = N_Package_Declaration
12103 then
12104 Pack_Id := Defining_Entity (Decl);
12106 -- Do not inspect an ignored Ghost package because all code found
12107 -- within will not appear in the final tree.
12109 if Is_Ignored_Ghost_Entity (Pack_Id) then
12110 null;
12112 elsif Ekind (Pack_Id) /= E_Generic_Package
12113 and then Requires_Cleanup_Actions
12114 (Specification (Decl), Lib_Level)
12115 then
12116 return True;
12117 end if;
12119 -- Nested package bodies
12121 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12123 -- Do not inspect an ignored Ghost package body because all code
12124 -- found within will not appear in the final tree.
12126 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12127 null;
12129 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12130 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12131 then
12132 return True;
12133 end if;
12135 elsif Nkind (Decl) = N_Block_Statement
12136 and then
12138 -- Handle a rare case caused by a controlled transient object
12139 -- created as part of a record init proc. The variable is wrapped
12140 -- in a block, but the block is not associated with a transient
12141 -- scope.
12143 (Inside_Init_Proc
12145 -- Handle the case where the original context has been wrapped in
12146 -- a block to avoid interference between exception handlers and
12147 -- At_End handlers. Treat the block as transparent and process its
12148 -- contents.
12150 or else Is_Finalization_Wrapper (Decl))
12151 then
12152 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12153 return True;
12154 end if;
12155 end if;
12157 Next (Decl);
12158 end loop;
12160 return False;
12161 end Requires_Cleanup_Actions;
12163 ------------------------------------
12164 -- Safe_Unchecked_Type_Conversion --
12165 ------------------------------------
12167 -- Note: this function knows quite a bit about the exact requirements of
12168 -- Gigi with respect to unchecked type conversions, and its code must be
12169 -- coordinated with any changes in Gigi in this area.
12171 -- The above requirements should be documented in Sinfo ???
12173 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12174 Otyp : Entity_Id;
12175 Ityp : Entity_Id;
12176 Oalign : Uint;
12177 Ialign : Uint;
12178 Pexp : constant Node_Id := Parent (Exp);
12180 begin
12181 -- If the expression is the RHS of an assignment or object declaration
12182 -- we are always OK because there will always be a target.
12184 -- Object renaming declarations, (generated for view conversions of
12185 -- actuals in inlined calls), like object declarations, provide an
12186 -- explicit type, and are safe as well.
12188 if (Nkind (Pexp) = N_Assignment_Statement
12189 and then Expression (Pexp) = Exp)
12190 or else Nkind_In (Pexp, N_Object_Declaration,
12191 N_Object_Renaming_Declaration)
12192 then
12193 return True;
12195 -- If the expression is the prefix of an N_Selected_Component we should
12196 -- also be OK because GCC knows to look inside the conversion except if
12197 -- the type is discriminated. We assume that we are OK anyway if the
12198 -- type is not set yet or if it is controlled since we can't afford to
12199 -- introduce a temporary in this case.
12201 elsif Nkind (Pexp) = N_Selected_Component
12202 and then Prefix (Pexp) = Exp
12203 then
12204 if No (Etype (Pexp)) then
12205 return True;
12206 else
12207 return
12208 not Has_Discriminants (Etype (Pexp))
12209 or else Is_Constrained (Etype (Pexp));
12210 end if;
12211 end if;
12213 -- Set the output type, this comes from Etype if it is set, otherwise we
12214 -- take it from the subtype mark, which we assume was already fully
12215 -- analyzed.
12217 if Present (Etype (Exp)) then
12218 Otyp := Etype (Exp);
12219 else
12220 Otyp := Entity (Subtype_Mark (Exp));
12221 end if;
12223 -- The input type always comes from the expression, and we assume this
12224 -- is indeed always analyzed, so we can simply get the Etype.
12226 Ityp := Etype (Expression (Exp));
12228 -- Initialize alignments to unknown so far
12230 Oalign := No_Uint;
12231 Ialign := No_Uint;
12233 -- Replace a concurrent type by its corresponding record type and each
12234 -- type by its underlying type and do the tests on those. The original
12235 -- type may be a private type whose completion is a concurrent type, so
12236 -- find the underlying type first.
12238 if Present (Underlying_Type (Otyp)) then
12239 Otyp := Underlying_Type (Otyp);
12240 end if;
12242 if Present (Underlying_Type (Ityp)) then
12243 Ityp := Underlying_Type (Ityp);
12244 end if;
12246 if Is_Concurrent_Type (Otyp) then
12247 Otyp := Corresponding_Record_Type (Otyp);
12248 end if;
12250 if Is_Concurrent_Type (Ityp) then
12251 Ityp := Corresponding_Record_Type (Ityp);
12252 end if;
12254 -- If the base types are the same, we know there is no problem since
12255 -- this conversion will be a noop.
12257 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12258 return True;
12260 -- Same if this is an upwards conversion of an untagged type, and there
12261 -- are no constraints involved (could be more general???)
12263 elsif Etype (Ityp) = Otyp
12264 and then not Is_Tagged_Type (Ityp)
12265 and then not Has_Discriminants (Ityp)
12266 and then No (First_Rep_Item (Base_Type (Ityp)))
12267 then
12268 return True;
12270 -- If the expression has an access type (object or subprogram) we assume
12271 -- that the conversion is safe, because the size of the target is safe,
12272 -- even if it is a record (which might be treated as having unknown size
12273 -- at this point).
12275 elsif Is_Access_Type (Ityp) then
12276 return True;
12278 -- If the size of output type is known at compile time, there is never
12279 -- a problem. Note that unconstrained records are considered to be of
12280 -- known size, but we can't consider them that way here, because we are
12281 -- talking about the actual size of the object.
12283 -- We also make sure that in addition to the size being known, we do not
12284 -- have a case which might generate an embarrassingly large temp in
12285 -- stack checking mode.
12287 elsif Size_Known_At_Compile_Time (Otyp)
12288 and then
12289 (not Stack_Checking_Enabled
12290 or else not May_Generate_Large_Temp (Otyp))
12291 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12292 then
12293 return True;
12295 -- If either type is tagged, then we know the alignment is OK so Gigi
12296 -- will be able to use pointer punning.
12298 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12299 return True;
12301 -- If either type is a limited record type, we cannot do a copy, so say
12302 -- safe since there's nothing else we can do.
12304 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12305 return True;
12307 -- Conversions to and from packed array types are always ignored and
12308 -- hence are safe.
12310 elsif Is_Packed_Array_Impl_Type (Otyp)
12311 or else Is_Packed_Array_Impl_Type (Ityp)
12312 then
12313 return True;
12314 end if;
12316 -- The only other cases known to be safe is if the input type's
12317 -- alignment is known to be at least the maximum alignment for the
12318 -- target or if both alignments are known and the output type's
12319 -- alignment is no stricter than the input's. We can use the component
12320 -- type alignment for an array if a type is an unpacked array type.
12322 if Present (Alignment_Clause (Otyp)) then
12323 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12325 elsif Is_Array_Type (Otyp)
12326 and then Present (Alignment_Clause (Component_Type (Otyp)))
12327 then
12328 Oalign := Expr_Value (Expression (Alignment_Clause
12329 (Component_Type (Otyp))));
12330 end if;
12332 if Present (Alignment_Clause (Ityp)) then
12333 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12335 elsif Is_Array_Type (Ityp)
12336 and then Present (Alignment_Clause (Component_Type (Ityp)))
12337 then
12338 Ialign := Expr_Value (Expression (Alignment_Clause
12339 (Component_Type (Ityp))));
12340 end if;
12342 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12343 return True;
12345 elsif Ialign /= No_Uint
12346 and then Oalign /= No_Uint
12347 and then Ialign <= Oalign
12348 then
12349 return True;
12351 -- Otherwise, Gigi cannot handle this and we must make a temporary
12353 else
12354 return False;
12355 end if;
12356 end Safe_Unchecked_Type_Conversion;
12358 ---------------------------------
12359 -- Set_Current_Value_Condition --
12360 ---------------------------------
12362 -- Note: the implementation of this procedure is very closely tied to the
12363 -- implementation of Get_Current_Value_Condition. Here we set required
12364 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12365 -- them, so they must have a consistent view.
12367 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12369 procedure Set_Entity_Current_Value (N : Node_Id);
12370 -- If N is an entity reference, where the entity is of an appropriate
12371 -- kind, then set the current value of this entity to Cnode, unless
12372 -- there is already a definite value set there.
12374 procedure Set_Expression_Current_Value (N : Node_Id);
12375 -- If N is of an appropriate form, sets an appropriate entry in current
12376 -- value fields of relevant entities. Multiple entities can be affected
12377 -- in the case of an AND or AND THEN.
12379 ------------------------------
12380 -- Set_Entity_Current_Value --
12381 ------------------------------
12383 procedure Set_Entity_Current_Value (N : Node_Id) is
12384 begin
12385 if Is_Entity_Name (N) then
12386 declare
12387 Ent : constant Entity_Id := Entity (N);
12389 begin
12390 -- Don't capture if not safe to do so
12392 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12393 return;
12394 end if;
12396 -- Here we have a case where the Current_Value field may need
12397 -- to be set. We set it if it is not already set to a compile
12398 -- time expression value.
12400 -- Note that this represents a decision that one condition
12401 -- blots out another previous one. That's certainly right if
12402 -- they occur at the same level. If the second one is nested,
12403 -- then the decision is neither right nor wrong (it would be
12404 -- equally OK to leave the outer one in place, or take the new
12405 -- inner one. Really we should record both, but our data
12406 -- structures are not that elaborate.
12408 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12409 Set_Current_Value (Ent, Cnode);
12410 end if;
12411 end;
12412 end if;
12413 end Set_Entity_Current_Value;
12415 ----------------------------------
12416 -- Set_Expression_Current_Value --
12417 ----------------------------------
12419 procedure Set_Expression_Current_Value (N : Node_Id) is
12420 Cond : Node_Id;
12422 begin
12423 Cond := N;
12425 -- Loop to deal with (ignore for now) any NOT operators present. The
12426 -- presence of NOT operators will be handled properly when we call
12427 -- Get_Current_Value_Condition.
12429 while Nkind (Cond) = N_Op_Not loop
12430 Cond := Right_Opnd (Cond);
12431 end loop;
12433 -- For an AND or AND THEN, recursively process operands
12435 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12436 Set_Expression_Current_Value (Left_Opnd (Cond));
12437 Set_Expression_Current_Value (Right_Opnd (Cond));
12438 return;
12439 end if;
12441 -- Check possible relational operator
12443 if Nkind (Cond) in N_Op_Compare then
12444 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12445 Set_Entity_Current_Value (Left_Opnd (Cond));
12446 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12447 Set_Entity_Current_Value (Right_Opnd (Cond));
12448 end if;
12450 elsif Nkind_In (Cond,
12451 N_Type_Conversion,
12452 N_Qualified_Expression,
12453 N_Expression_With_Actions)
12454 then
12455 Set_Expression_Current_Value (Expression (Cond));
12457 -- Check possible boolean variable reference
12459 else
12460 Set_Entity_Current_Value (Cond);
12461 end if;
12462 end Set_Expression_Current_Value;
12464 -- Start of processing for Set_Current_Value_Condition
12466 begin
12467 Set_Expression_Current_Value (Condition (Cnode));
12468 end Set_Current_Value_Condition;
12470 --------------------------
12471 -- Set_Elaboration_Flag --
12472 --------------------------
12474 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12475 Loc : constant Source_Ptr := Sloc (N);
12476 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12477 Asn : Node_Id;
12479 begin
12480 if Present (Ent) then
12482 -- Nothing to do if at the compilation unit level, because in this
12483 -- case the flag is set by the binder generated elaboration routine.
12485 if Nkind (Parent (N)) = N_Compilation_Unit then
12486 null;
12488 -- Here we do need to generate an assignment statement
12490 else
12491 Check_Restriction (No_Elaboration_Code, N);
12492 Asn :=
12493 Make_Assignment_Statement (Loc,
12494 Name => New_Occurrence_Of (Ent, Loc),
12495 Expression => Make_Integer_Literal (Loc, Uint_1));
12497 if Nkind (Parent (N)) = N_Subunit then
12498 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12499 else
12500 Insert_After (N, Asn);
12501 end if;
12503 Analyze (Asn);
12505 -- Kill current value indication. This is necessary because the
12506 -- tests of this flag are inserted out of sequence and must not
12507 -- pick up bogus indications of the wrong constant value.
12509 Set_Current_Value (Ent, Empty);
12511 -- If the subprogram is in the current declarative part and
12512 -- 'access has been applied to it, generate an elaboration
12513 -- check at the beginning of the declarations of the body.
12515 if Nkind (N) = N_Subprogram_Body
12516 and then Address_Taken (Spec_Id)
12517 and then
12518 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12519 then
12520 declare
12521 Loc : constant Source_Ptr := Sloc (N);
12522 Decls : constant List_Id := Declarations (N);
12523 Chk : Node_Id;
12525 begin
12526 -- No need to generate this check if first entry in the
12527 -- declaration list is a raise of Program_Error now.
12529 if Present (Decls)
12530 and then Nkind (First (Decls)) = N_Raise_Program_Error
12531 then
12532 return;
12533 end if;
12535 -- Otherwise generate the check
12537 Chk :=
12538 Make_Raise_Program_Error (Loc,
12539 Condition =>
12540 Make_Op_Eq (Loc,
12541 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12542 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12543 Reason => PE_Access_Before_Elaboration);
12545 if No (Decls) then
12546 Set_Declarations (N, New_List (Chk));
12547 else
12548 Prepend (Chk, Decls);
12549 end if;
12551 Analyze (Chk);
12552 end;
12553 end if;
12554 end if;
12555 end if;
12556 end Set_Elaboration_Flag;
12558 ----------------------------
12559 -- Set_Renamed_Subprogram --
12560 ----------------------------
12562 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12563 begin
12564 -- If input node is an identifier, we can just reset it
12566 if Nkind (N) = N_Identifier then
12567 Set_Chars (N, Chars (E));
12568 Set_Entity (N, E);
12570 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12572 else
12573 declare
12574 CS : constant Boolean := Comes_From_Source (N);
12575 begin
12576 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12577 Set_Entity (N, E);
12578 Set_Comes_From_Source (N, CS);
12579 Set_Analyzed (N, True);
12580 end;
12581 end if;
12582 end Set_Renamed_Subprogram;
12584 ----------------------
12585 -- Side_Effect_Free --
12586 ----------------------
12588 function Side_Effect_Free
12589 (N : Node_Id;
12590 Name_Req : Boolean := False;
12591 Variable_Ref : Boolean := False) return Boolean
12593 Typ : constant Entity_Id := Etype (N);
12594 -- Result type of the expression
12596 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12597 -- The argument N is a construct where the Prefix is dereferenced if it
12598 -- is an access type and the result is a variable. The call returns True
12599 -- if the construct is side effect free (not considering side effects in
12600 -- other than the prefix which are to be tested by the caller).
12602 function Within_In_Parameter (N : Node_Id) return Boolean;
12603 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12604 -- N is not side-effect free when the actual is global and modifiable
12605 -- indirectly from within a subprogram, because it may be passed by
12606 -- reference. The front-end must be conservative here and assume that
12607 -- this may happen with any array or record type. On the other hand, we
12608 -- cannot create temporaries for all expressions for which this
12609 -- condition is true, for various reasons that might require clearing up
12610 -- ??? For example, discriminant references that appear out of place, or
12611 -- spurious type errors with class-wide expressions. As a result, we
12612 -- limit the transformation to loop bounds, which is so far the only
12613 -- case that requires it.
12615 -----------------------------
12616 -- Safe_Prefixed_Reference --
12617 -----------------------------
12619 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12620 begin
12621 -- If prefix is not side effect free, definitely not safe
12623 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12624 return False;
12626 -- If the prefix is of an access type that is not access-to-constant,
12627 -- then this construct is a variable reference, which means it is to
12628 -- be considered to have side effects if Variable_Ref is set True.
12630 elsif Is_Access_Type (Etype (Prefix (N)))
12631 and then not Is_Access_Constant (Etype (Prefix (N)))
12632 and then Variable_Ref
12633 then
12634 -- Exception is a prefix that is the result of a previous removal
12635 -- of side-effects.
12637 return Is_Entity_Name (Prefix (N))
12638 and then not Comes_From_Source (Prefix (N))
12639 and then Ekind (Entity (Prefix (N))) = E_Constant
12640 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12642 -- If the prefix is an explicit dereference then this construct is a
12643 -- variable reference, which means it is to be considered to have
12644 -- side effects if Variable_Ref is True.
12646 -- We do NOT exclude dereferences of access-to-constant types because
12647 -- we handle them as constant view of variables.
12649 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12650 and then Variable_Ref
12651 then
12652 return False;
12654 -- Note: The following test is the simplest way of solving a complex
12655 -- problem uncovered by the following test (Side effect on loop bound
12656 -- that is a subcomponent of a global variable:
12658 -- with Text_Io; use Text_Io;
12659 -- procedure Tloop is
12660 -- type X is
12661 -- record
12662 -- V : Natural := 4;
12663 -- S : String (1..5) := (others => 'a');
12664 -- end record;
12665 -- X1 : X;
12667 -- procedure Modi;
12669 -- generic
12670 -- with procedure Action;
12671 -- procedure Loop_G (Arg : X; Msg : String)
12673 -- procedure Loop_G (Arg : X; Msg : String) is
12674 -- begin
12675 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12676 -- & Natural'Image (Arg.V));
12677 -- for Index in 1 .. Arg.V loop
12678 -- Text_Io.Put_Line
12679 -- (Natural'Image (Index) & " " & Arg.S (Index));
12680 -- if Index > 2 then
12681 -- Modi;
12682 -- end if;
12683 -- end loop;
12684 -- Put_Line ("end loop_g " & Msg);
12685 -- end;
12687 -- procedure Loop1 is new Loop_G (Modi);
12688 -- procedure Modi is
12689 -- begin
12690 -- X1.V := 1;
12691 -- Loop1 (X1, "from modi");
12692 -- end;
12694 -- begin
12695 -- Loop1 (X1, "initial");
12696 -- end;
12698 -- The output of the above program should be:
12700 -- begin loop_g initial will loop till: 4
12701 -- 1 a
12702 -- 2 a
12703 -- 3 a
12704 -- begin loop_g from modi will loop till: 1
12705 -- 1 a
12706 -- end loop_g from modi
12707 -- 4 a
12708 -- begin loop_g from modi will loop till: 1
12709 -- 1 a
12710 -- end loop_g from modi
12711 -- end loop_g initial
12713 -- If a loop bound is a subcomponent of a global variable, a
12714 -- modification of that variable within the loop may incorrectly
12715 -- affect the execution of the loop.
12717 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12718 and then Within_In_Parameter (Prefix (N))
12719 and then Variable_Ref
12720 then
12721 return False;
12723 -- All other cases are side effect free
12725 else
12726 return True;
12727 end if;
12728 end Safe_Prefixed_Reference;
12730 -------------------------
12731 -- Within_In_Parameter --
12732 -------------------------
12734 function Within_In_Parameter (N : Node_Id) return Boolean is
12735 begin
12736 if not Comes_From_Source (N) then
12737 return False;
12739 elsif Is_Entity_Name (N) then
12740 return Ekind (Entity (N)) = E_In_Parameter;
12742 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12743 return Within_In_Parameter (Prefix (N));
12745 else
12746 return False;
12747 end if;
12748 end Within_In_Parameter;
12750 -- Start of processing for Side_Effect_Free
12752 begin
12753 -- If volatile reference, always consider it to have side effects
12755 if Is_Volatile_Reference (N) then
12756 return False;
12757 end if;
12759 -- Note on checks that could raise Constraint_Error. Strictly, if we
12760 -- take advantage of 11.6, these checks do not count as side effects.
12761 -- However, we would prefer to consider that they are side effects,
12762 -- since the back end CSE does not work very well on expressions which
12763 -- can raise Constraint_Error. On the other hand if we don't consider
12764 -- them to be side effect free, then we get some awkward expansions
12765 -- in -gnato mode, resulting in code insertions at a point where we
12766 -- do not have a clear model for performing the insertions.
12768 -- Special handling for entity names
12770 if Is_Entity_Name (N) then
12772 -- A type reference is always side effect free
12774 if Is_Type (Entity (N)) then
12775 return True;
12777 -- Variables are considered to be a side effect if Variable_Ref
12778 -- is set or if we have a volatile reference and Name_Req is off.
12779 -- If Name_Req is True then we can't help returning a name which
12780 -- effectively allows multiple references in any case.
12782 elsif Is_Variable (N, Use_Original_Node => False) then
12783 return not Variable_Ref
12784 and then (not Is_Volatile_Reference (N) or else Name_Req);
12786 -- Any other entity (e.g. a subtype name) is definitely side
12787 -- effect free.
12789 else
12790 return True;
12791 end if;
12793 -- A value known at compile time is always side effect free
12795 elsif Compile_Time_Known_Value (N) then
12796 return True;
12798 -- A variable renaming is not side-effect free, because the renaming
12799 -- will function like a macro in the front-end in some cases, and an
12800 -- assignment can modify the component designated by N, so we need to
12801 -- create a temporary for it.
12803 -- The guard testing for Entity being present is needed at least in
12804 -- the case of rewritten predicate expressions, and may well also be
12805 -- appropriate elsewhere. Obviously we can't go testing the entity
12806 -- field if it does not exist, so it's reasonable to say that this is
12807 -- not the renaming case if it does not exist.
12809 elsif Is_Entity_Name (Original_Node (N))
12810 and then Present (Entity (Original_Node (N)))
12811 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
12812 and then Ekind (Entity (Original_Node (N))) /= E_Constant
12813 then
12814 declare
12815 RO : constant Node_Id :=
12816 Renamed_Object (Entity (Original_Node (N)));
12818 begin
12819 -- If the renamed object is an indexed component, or an
12820 -- explicit dereference, then the designated object could
12821 -- be modified by an assignment.
12823 if Nkind_In (RO, N_Indexed_Component,
12824 N_Explicit_Dereference)
12825 then
12826 return False;
12828 -- A selected component must have a safe prefix
12830 elsif Nkind (RO) = N_Selected_Component then
12831 return Safe_Prefixed_Reference (RO);
12833 -- In all other cases, designated object cannot be changed so
12834 -- we are side effect free.
12836 else
12837 return True;
12838 end if;
12839 end;
12841 -- Remove_Side_Effects generates an object renaming declaration to
12842 -- capture the expression of a class-wide expression. In VM targets
12843 -- the frontend performs no expansion for dispatching calls to
12844 -- class- wide types since they are handled by the VM. Hence, we must
12845 -- locate here if this node corresponds to a previous invocation of
12846 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
12848 elsif not Tagged_Type_Expansion
12849 and then not Comes_From_Source (N)
12850 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
12851 and then Is_Class_Wide_Type (Typ)
12852 then
12853 return True;
12855 -- Generating C the type conversion of an access to constrained array
12856 -- type into an access to unconstrained array type involves initializing
12857 -- a fat pointer and the expression cannot be assumed to be free of side
12858 -- effects since it must referenced several times to compute its bounds.
12860 elsif Modify_Tree_For_C
12861 and then Nkind (N) = N_Type_Conversion
12862 and then Is_Access_Type (Typ)
12863 and then Is_Array_Type (Designated_Type (Typ))
12864 and then not Is_Constrained (Designated_Type (Typ))
12865 then
12866 return False;
12867 end if;
12869 -- For other than entity names and compile time known values,
12870 -- check the node kind for special processing.
12872 case Nkind (N) is
12874 -- An attribute reference is side effect free if its expressions
12875 -- are side effect free and its prefix is side effect free or
12876 -- is an entity reference.
12878 -- Is this right? what about x'first where x is a variable???
12880 when N_Attribute_Reference =>
12881 return
12882 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12883 and then Attribute_Name (N) /= Name_Input
12884 and then (Is_Entity_Name (Prefix (N))
12885 or else Side_Effect_Free
12886 (Prefix (N), Name_Req, Variable_Ref));
12888 -- A binary operator is side effect free if and both operands are
12889 -- side effect free. For this purpose binary operators include
12890 -- membership tests and short circuit forms.
12892 when N_Binary_Op
12893 | N_Membership_Test
12894 | N_Short_Circuit
12896 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
12897 and then
12898 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
12900 -- An explicit dereference is side effect free only if it is
12901 -- a side effect free prefixed reference.
12903 when N_Explicit_Dereference =>
12904 return Safe_Prefixed_Reference (N);
12906 -- An expression with action is side effect free if its expression
12907 -- is side effect free and it has no actions.
12909 when N_Expression_With_Actions =>
12910 return
12911 Is_Empty_List (Actions (N))
12912 and then Side_Effect_Free
12913 (Expression (N), Name_Req, Variable_Ref);
12915 -- A call to _rep_to_pos is side effect free, since we generate
12916 -- this pure function call ourselves. Moreover it is critically
12917 -- important to make this exception, since otherwise we can have
12918 -- discriminants in array components which don't look side effect
12919 -- free in the case of an array whose index type is an enumeration
12920 -- type with an enumeration rep clause.
12922 -- All other function calls are not side effect free
12924 when N_Function_Call =>
12925 return
12926 Nkind (Name (N)) = N_Identifier
12927 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
12928 and then Side_Effect_Free
12929 (First (Parameter_Associations (N)),
12930 Name_Req, Variable_Ref);
12932 -- An IF expression is side effect free if it's of a scalar type, and
12933 -- all its components are all side effect free (conditions and then
12934 -- actions and else actions). We restrict to scalar types, since it
12935 -- is annoying to deal with things like (if A then B else C)'First
12936 -- where the type involved is a string type.
12938 when N_If_Expression =>
12939 return
12940 Is_Scalar_Type (Typ)
12941 and then Side_Effect_Free
12942 (Expressions (N), Name_Req, Variable_Ref);
12944 -- An indexed component is side effect free if it is a side
12945 -- effect free prefixed reference and all the indexing
12946 -- expressions are side effect free.
12948 when N_Indexed_Component =>
12949 return
12950 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12951 and then Safe_Prefixed_Reference (N);
12953 -- A type qualification, type conversion, or unchecked expression is
12954 -- side effect free if the expression is side effect free.
12956 when N_Qualified_Expression
12957 | N_Type_Conversion
12958 | N_Unchecked_Expression
12960 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
12962 -- A selected component is side effect free only if it is a side
12963 -- effect free prefixed reference.
12965 when N_Selected_Component =>
12966 return Safe_Prefixed_Reference (N);
12968 -- A range is side effect free if the bounds are side effect free
12970 when N_Range =>
12971 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
12972 and then
12973 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
12975 -- A slice is side effect free if it is a side effect free
12976 -- prefixed reference and the bounds are side effect free.
12978 when N_Slice =>
12979 return
12980 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
12981 and then Safe_Prefixed_Reference (N);
12983 -- A unary operator is side effect free if the operand
12984 -- is side effect free.
12986 when N_Unary_Op =>
12987 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
12989 -- An unchecked type conversion is side effect free only if it
12990 -- is safe and its argument is side effect free.
12992 when N_Unchecked_Type_Conversion =>
12993 return
12994 Safe_Unchecked_Type_Conversion (N)
12995 and then Side_Effect_Free
12996 (Expression (N), Name_Req, Variable_Ref);
12998 -- A literal is side effect free
13000 when N_Character_Literal
13001 | N_Integer_Literal
13002 | N_Real_Literal
13003 | N_String_Literal
13005 return True;
13007 -- We consider that anything else has side effects. This is a bit
13008 -- crude, but we are pretty close for most common cases, and we
13009 -- are certainly correct (i.e. we never return True when the
13010 -- answer should be False).
13012 when others =>
13013 return False;
13014 end case;
13015 end Side_Effect_Free;
13017 -- A list is side effect free if all elements of the list are side
13018 -- effect free.
13020 function Side_Effect_Free
13021 (L : List_Id;
13022 Name_Req : Boolean := False;
13023 Variable_Ref : Boolean := False) return Boolean
13025 N : Node_Id;
13027 begin
13028 if L = No_List or else L = Error_List then
13029 return True;
13031 else
13032 N := First (L);
13033 while Present (N) loop
13034 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13035 return False;
13036 else
13037 Next (N);
13038 end if;
13039 end loop;
13041 return True;
13042 end if;
13043 end Side_Effect_Free;
13045 ----------------------------------
13046 -- Silly_Boolean_Array_Not_Test --
13047 ----------------------------------
13049 -- This procedure implements an odd and silly test. We explicitly check
13050 -- for the case where the 'First of the component type is equal to the
13051 -- 'Last of this component type, and if this is the case, we make sure
13052 -- that constraint error is raised. The reason is that the NOT is bound
13053 -- to cause CE in this case, and we will not otherwise catch it.
13055 -- No such check is required for AND and OR, since for both these cases
13056 -- False op False = False, and True op True = True. For the XOR case,
13057 -- see Silly_Boolean_Array_Xor_Test.
13059 -- Believe it or not, this was reported as a bug. Note that nearly always,
13060 -- the test will evaluate statically to False, so the code will be
13061 -- statically removed, and no extra overhead caused.
13063 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13064 Loc : constant Source_Ptr := Sloc (N);
13065 CT : constant Entity_Id := Component_Type (T);
13067 begin
13068 -- The check we install is
13070 -- constraint_error when
13071 -- component_type'first = component_type'last
13072 -- and then array_type'Length /= 0)
13074 -- We need the last guard because we don't want to raise CE for empty
13075 -- arrays since no out of range values result. (Empty arrays with a
13076 -- component type of True .. True -- very useful -- even the ACATS
13077 -- does not test that marginal case).
13079 Insert_Action (N,
13080 Make_Raise_Constraint_Error (Loc,
13081 Condition =>
13082 Make_And_Then (Loc,
13083 Left_Opnd =>
13084 Make_Op_Eq (Loc,
13085 Left_Opnd =>
13086 Make_Attribute_Reference (Loc,
13087 Prefix => New_Occurrence_Of (CT, Loc),
13088 Attribute_Name => Name_First),
13090 Right_Opnd =>
13091 Make_Attribute_Reference (Loc,
13092 Prefix => New_Occurrence_Of (CT, Loc),
13093 Attribute_Name => Name_Last)),
13095 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13096 Reason => CE_Range_Check_Failed));
13097 end Silly_Boolean_Array_Not_Test;
13099 ----------------------------------
13100 -- Silly_Boolean_Array_Xor_Test --
13101 ----------------------------------
13103 -- This procedure implements an odd and silly test. We explicitly check
13104 -- for the XOR case where the component type is True .. True, since this
13105 -- will raise constraint error. A special check is required since CE
13106 -- will not be generated otherwise (cf Expand_Packed_Not).
13108 -- No such check is required for AND and OR, since for both these cases
13109 -- False op False = False, and True op True = True, and no check is
13110 -- required for the case of False .. False, since False xor False = False.
13111 -- See also Silly_Boolean_Array_Not_Test
13113 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13114 Loc : constant Source_Ptr := Sloc (N);
13115 CT : constant Entity_Id := Component_Type (T);
13117 begin
13118 -- The check we install is
13120 -- constraint_error when
13121 -- Boolean (component_type'First)
13122 -- and then Boolean (component_type'Last)
13123 -- and then array_type'Length /= 0)
13125 -- We need the last guard because we don't want to raise CE for empty
13126 -- arrays since no out of range values result (Empty arrays with a
13127 -- component type of True .. True -- very useful -- even the ACATS
13128 -- does not test that marginal case).
13130 Insert_Action (N,
13131 Make_Raise_Constraint_Error (Loc,
13132 Condition =>
13133 Make_And_Then (Loc,
13134 Left_Opnd =>
13135 Make_And_Then (Loc,
13136 Left_Opnd =>
13137 Convert_To (Standard_Boolean,
13138 Make_Attribute_Reference (Loc,
13139 Prefix => New_Occurrence_Of (CT, Loc),
13140 Attribute_Name => Name_First)),
13142 Right_Opnd =>
13143 Convert_To (Standard_Boolean,
13144 Make_Attribute_Reference (Loc,
13145 Prefix => New_Occurrence_Of (CT, Loc),
13146 Attribute_Name => Name_Last))),
13148 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13149 Reason => CE_Range_Check_Failed));
13150 end Silly_Boolean_Array_Xor_Test;
13152 --------------------------
13153 -- Target_Has_Fixed_Ops --
13154 --------------------------
13156 Integer_Sized_Small : Ureal;
13157 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13158 -- called (we don't want to compute it more than once).
13160 Long_Integer_Sized_Small : Ureal;
13161 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13162 -- is called (we don't want to compute it more than once)
13164 First_Time_For_THFO : Boolean := True;
13165 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13167 function Target_Has_Fixed_Ops
13168 (Left_Typ : Entity_Id;
13169 Right_Typ : Entity_Id;
13170 Result_Typ : Entity_Id) return Boolean
13172 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13173 -- Return True if the given type is a fixed-point type with a small
13174 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13175 -- an absolute value less than 1.0. This is currently limited to
13176 -- fixed-point types that map to Integer or Long_Integer.
13178 ------------------------
13179 -- Is_Fractional_Type --
13180 ------------------------
13182 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13183 begin
13184 if Esize (Typ) = Standard_Integer_Size then
13185 return Small_Value (Typ) = Integer_Sized_Small;
13187 elsif Esize (Typ) = Standard_Long_Integer_Size then
13188 return Small_Value (Typ) = Long_Integer_Sized_Small;
13190 else
13191 return False;
13192 end if;
13193 end Is_Fractional_Type;
13195 -- Start of processing for Target_Has_Fixed_Ops
13197 begin
13198 -- Return False if Fractional_Fixed_Ops_On_Target is false
13200 if not Fractional_Fixed_Ops_On_Target then
13201 return False;
13202 end if;
13204 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13205 -- standard constants used by Is_Fractional_Type.
13207 if First_Time_For_THFO then
13208 First_Time_For_THFO := False;
13210 Integer_Sized_Small :=
13211 UR_From_Components
13212 (Num => Uint_1,
13213 Den => UI_From_Int (Standard_Integer_Size - 1),
13214 Rbase => 2);
13216 Long_Integer_Sized_Small :=
13217 UR_From_Components
13218 (Num => Uint_1,
13219 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13220 Rbase => 2);
13221 end if;
13223 -- Return True if target supports fixed-by-fixed multiply/divide for
13224 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13225 -- and result types are equivalent fractional types.
13227 return Is_Fractional_Type (Base_Type (Left_Typ))
13228 and then Is_Fractional_Type (Base_Type (Right_Typ))
13229 and then Is_Fractional_Type (Base_Type (Result_Typ))
13230 and then Esize (Left_Typ) = Esize (Right_Typ)
13231 and then Esize (Left_Typ) = Esize (Result_Typ);
13232 end Target_Has_Fixed_Ops;
13234 -------------------
13235 -- Type_Map_Hash --
13236 -------------------
13238 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13239 begin
13240 return Type_Map_Header (Id mod Type_Map_Size);
13241 end Type_Map_Hash;
13243 ------------------------------------------
13244 -- Type_May_Have_Bit_Aligned_Components --
13245 ------------------------------------------
13247 function Type_May_Have_Bit_Aligned_Components
13248 (Typ : Entity_Id) return Boolean
13250 begin
13251 -- Array type, check component type
13253 if Is_Array_Type (Typ) then
13254 return
13255 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13257 -- Record type, check components
13259 elsif Is_Record_Type (Typ) then
13260 declare
13261 E : Entity_Id;
13263 begin
13264 E := First_Component_Or_Discriminant (Typ);
13265 while Present (E) loop
13266 if Component_May_Be_Bit_Aligned (E)
13267 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13268 then
13269 return True;
13270 end if;
13272 Next_Component_Or_Discriminant (E);
13273 end loop;
13275 return False;
13276 end;
13278 -- Type other than array or record is always OK
13280 else
13281 return False;
13282 end if;
13283 end Type_May_Have_Bit_Aligned_Components;
13285 -------------------------------
13286 -- Update_Primitives_Mapping --
13287 -------------------------------
13289 procedure Update_Primitives_Mapping
13290 (Inher_Id : Entity_Id;
13291 Subp_Id : Entity_Id)
13293 begin
13294 Map_Types
13295 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13296 Derived_Type => Find_Dispatching_Type (Subp_Id));
13297 end Update_Primitives_Mapping;
13299 ----------------------------------
13300 -- Within_Case_Or_If_Expression --
13301 ----------------------------------
13303 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13304 Par : Node_Id;
13306 begin
13307 -- Locate an enclosing case or if expression. Note that these constructs
13308 -- can be expanded into Expression_With_Actions, hence the test of the
13309 -- original node.
13311 Par := Parent (N);
13312 while Present (Par) loop
13313 if Nkind_In (Original_Node (Par), N_Case_Expression,
13314 N_If_Expression)
13315 then
13316 return True;
13318 -- Prevent the search from going too far
13320 elsif Is_Body_Or_Package_Declaration (Par) then
13321 return False;
13322 end if;
13324 Par := Parent (Par);
13325 end loop;
13327 return False;
13328 end Within_Case_Or_If_Expression;
13330 --------------------------------
13331 -- Within_Internal_Subprogram --
13332 --------------------------------
13334 function Within_Internal_Subprogram return Boolean is
13335 S : Entity_Id;
13337 begin
13338 S := Current_Scope;
13339 while Present (S) and then not Is_Subprogram (S) loop
13340 S := Scope (S);
13341 end loop;
13343 return Present (S)
13344 and then Get_TSS_Name (S) /= TSS_Null
13345 and then not Is_Predicate_Function (S)
13346 and then not Is_Predicate_Function_M (S);
13347 end Within_Internal_Subprogram;
13349 end Exp_Util;