* builtins.def (BUILT_IN_SETJMP): Revert latest change.
[official-gcc.git] / gcc / ada / exp_util.adb
blob1d64a3add3453dcea4d32e54e217afc5da17c847
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;
69 package body Exp_Util is
71 ---------------------------------------------------------
72 -- Handling of inherited class-wide pre/postconditions --
73 ---------------------------------------------------------
75 -- Following AI12-0113, the expression for a class-wide condition is
76 -- transformed for a subprogram that inherits it, by replacing calls
77 -- to primitive operations of the original controlling type into the
78 -- corresponding overriding operations of the derived type. The following
79 -- hash table manages this mapping, and is expanded on demand whenever
80 -- such inherited expression needs to be constructed.
82 -- The mapping is also used to check whether an inherited operation has
83 -- a condition that depends on overridden operations. For such an
84 -- operation we must create a wrapper that is then treated as a normal
85 -- overriding. In SPARK mode such operations are illegal.
87 -- For a given root type there may be several type extensions with their
88 -- own overriding operations, so at various times a given operation of
89 -- the root will be mapped into different overridings. The root type is
90 -- also mapped into the current type extension to indicate that its
91 -- operations are mapped into the overriding operations of that current
92 -- type extension.
94 -- The contents of the map are as follows:
96 -- Key Value
98 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
99 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
100 -- Discriminant (Entity_Id) Expression (Node_Id)
101 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
102 -- Type (Entity_Id) Type (Entity_Id)
104 Type_Map_Size : constant := 511;
106 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
107 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
109 package Type_Map is new GNAT.HTable.Simple_HTable
110 (Header_Num => Type_Map_Header,
111 Key => Entity_Id,
112 Element => Node_Or_Entity_Id,
113 No_element => Empty,
114 Hash => Type_Map_Hash,
115 Equal => "=");
117 -----------------------
118 -- Local Subprograms --
119 -----------------------
121 function Build_Task_Array_Image
122 (Loc : Source_Ptr;
123 Id_Ref : Node_Id;
124 A_Type : Entity_Id;
125 Dyn : Boolean := False) return Node_Id;
126 -- Build function to generate the image string for a task that is an array
127 -- component, concatenating the images of each index. To avoid storage
128 -- leaks, the string is built with successive slice assignments. The flag
129 -- Dyn indicates whether this is called for the initialization procedure of
130 -- an array of tasks, or for the name of a dynamically created task that is
131 -- assigned to an indexed component.
133 function Build_Task_Image_Function
134 (Loc : Source_Ptr;
135 Decls : List_Id;
136 Stats : List_Id;
137 Res : Entity_Id) return Node_Id;
138 -- Common processing for Task_Array_Image and Task_Record_Image. Build
139 -- function body that computes image.
141 procedure Build_Task_Image_Prefix
142 (Loc : Source_Ptr;
143 Len : out Entity_Id;
144 Res : out Entity_Id;
145 Pos : out Entity_Id;
146 Prefix : Entity_Id;
147 Sum : Node_Id;
148 Decls : List_Id;
149 Stats : List_Id);
150 -- Common processing for Task_Array_Image and Task_Record_Image. Create
151 -- local variables and assign prefix of name to result string.
153 function Build_Task_Record_Image
154 (Loc : Source_Ptr;
155 Id_Ref : Node_Id;
156 Dyn : Boolean := False) return Node_Id;
157 -- Build function to generate the image string for a task that is a record
158 -- component. Concatenate name of variable with that of selector. The flag
159 -- Dyn indicates whether this is called for the initialization procedure of
160 -- record with task components, or for a dynamically created task that is
161 -- assigned to a selected component.
163 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
164 -- Force evaluation of bounds of a slice, which may be given by a range
165 -- or by a subtype indication with or without a constraint.
167 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id;
168 -- Subsidiary to all Build_DIC_Procedure_xxx routines. Find the type which
169 -- defines the Default_Initial_Condition pragma of type Typ. This is either
170 -- Typ itself or a parent type when the pragma is inherited.
172 function Make_CW_Equivalent_Type
173 (T : Entity_Id;
174 E : Node_Id) return Entity_Id;
175 -- T is a class-wide type entity, E is the initial expression node that
176 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
177 -- returns the entity of the Equivalent type and inserts on the fly the
178 -- necessary declaration such as:
180 -- type anon is record
181 -- _parent : Root_Type (T); constrained with E discriminants (if any)
182 -- Extension : String (1 .. expr to match size of E);
183 -- end record;
185 -- This record is compatible with any object of the class of T thanks to
186 -- the first field and has the same size as E thanks to the second.
188 function Make_Literal_Range
189 (Loc : Source_Ptr;
190 Literal_Typ : Entity_Id) return Node_Id;
191 -- Produce a Range node whose bounds are:
192 -- Low_Bound (Literal_Type) ..
193 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
194 -- this is used for expanding declarations like X : String := "sdfgdfg";
196 -- If the index type of the target array is not integer, we generate:
197 -- Low_Bound (Literal_Type) ..
198 -- Literal_Type'Val
199 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
200 -- + (Length (Literal_Typ) -1))
202 function Make_Non_Empty_Check
203 (Loc : Source_Ptr;
204 N : Node_Id) return Node_Id;
205 -- Produce a boolean expression checking that the unidimensional array
206 -- node N is not empty.
208 function New_Class_Wide_Subtype
209 (CW_Typ : Entity_Id;
210 N : Node_Id) return Entity_Id;
211 -- Create an implicit subtype of CW_Typ attached to node N
213 function Requires_Cleanup_Actions
214 (L : List_Id;
215 Lib_Level : Boolean;
216 Nested_Constructs : Boolean) return Boolean;
217 -- Given a list L, determine whether it contains one of the following:
219 -- 1) controlled objects
220 -- 2) library-level tagged types
222 -- Lib_Level is True when the list comes from a construct at the library
223 -- level, and False otherwise. Nested_Constructs is True when any nested
224 -- packages declared in L must be processed, and False otherwise.
226 -------------------------------------
227 -- Activate_Atomic_Synchronization --
228 -------------------------------------
230 procedure Activate_Atomic_Synchronization (N : Node_Id) is
231 Msg_Node : Node_Id;
233 begin
234 case Nkind (Parent (N)) is
236 -- Check for cases of appearing in the prefix of a construct where we
237 -- don't need atomic synchronization for this kind of usage.
239 when
240 -- Nothing to do if we are the prefix of an attribute, since we
241 -- do not want an atomic sync operation for things like 'Size.
243 N_Attribute_Reference
245 -- The N_Reference node is like an attribute
247 | N_Reference
249 -- Nothing to do for a reference to a component (or components)
250 -- of a composite object. Only reads and updates of the object
251 -- as a whole require atomic synchronization (RM C.6 (15)).
253 | N_Indexed_Component
254 | N_Selected_Component
255 | N_Slice
257 -- For all the above cases, nothing to do if we are the prefix
259 if Prefix (Parent (N)) = N then
260 return;
261 end if;
263 when others =>
264 null;
265 end case;
267 -- Nothing to do for the identifier in an object renaming declaration,
268 -- the renaming itself does not need atomic synchronization.
270 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
271 return;
272 end if;
274 -- Go ahead and set the flag
276 Set_Atomic_Sync_Required (N);
278 -- Generate info message if requested
280 if Warn_On_Atomic_Synchronization then
281 case Nkind (N) is
282 when N_Identifier =>
283 Msg_Node := N;
285 when N_Expanded_Name
286 | N_Selected_Component
288 Msg_Node := Selector_Name (N);
290 when N_Explicit_Dereference
291 | N_Indexed_Component
293 Msg_Node := Empty;
295 when others =>
296 pragma Assert (False);
297 return;
298 end case;
300 if Present (Msg_Node) then
301 Error_Msg_N
302 ("info: atomic synchronization set for &?N?", Msg_Node);
303 else
304 Error_Msg_N
305 ("info: atomic synchronization set?N?", N);
306 end if;
307 end if;
308 end Activate_Atomic_Synchronization;
310 ----------------------
311 -- Adjust_Condition --
312 ----------------------
314 procedure Adjust_Condition (N : Node_Id) is
315 begin
316 if No (N) then
317 return;
318 end if;
320 declare
321 Loc : constant Source_Ptr := Sloc (N);
322 T : constant Entity_Id := Etype (N);
323 Ti : Entity_Id;
325 begin
326 -- Defend against a call where the argument has no type, or has a
327 -- type that is not Boolean. This can occur because of prior errors.
329 if No (T) or else not Is_Boolean_Type (T) then
330 return;
331 end if;
333 -- Apply validity checking if needed
335 if Validity_Checks_On and Validity_Check_Tests then
336 Ensure_Valid (N);
337 end if;
339 -- Immediate return if standard boolean, the most common case,
340 -- where nothing needs to be done.
342 if Base_Type (T) = Standard_Boolean then
343 return;
344 end if;
346 -- Case of zero/non-zero semantics or non-standard enumeration
347 -- representation. In each case, we rewrite the node as:
349 -- ityp!(N) /= False'Enum_Rep
351 -- where ityp is an integer type with large enough size to hold any
352 -- value of type T.
354 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
355 if Esize (T) <= Esize (Standard_Integer) then
356 Ti := Standard_Integer;
357 else
358 Ti := Standard_Long_Long_Integer;
359 end if;
361 Rewrite (N,
362 Make_Op_Ne (Loc,
363 Left_Opnd => Unchecked_Convert_To (Ti, N),
364 Right_Opnd =>
365 Make_Attribute_Reference (Loc,
366 Attribute_Name => Name_Enum_Rep,
367 Prefix =>
368 New_Occurrence_Of (First_Literal (T), Loc))));
369 Analyze_And_Resolve (N, Standard_Boolean);
371 else
372 Rewrite (N, Convert_To (Standard_Boolean, N));
373 Analyze_And_Resolve (N, Standard_Boolean);
374 end if;
375 end;
376 end Adjust_Condition;
378 ------------------------
379 -- Adjust_Result_Type --
380 ------------------------
382 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
383 begin
384 -- Ignore call if current type is not Standard.Boolean
386 if Etype (N) /= Standard_Boolean then
387 return;
388 end if;
390 -- If result is already of correct type, nothing to do. Note that
391 -- this will get the most common case where everything has a type
392 -- of Standard.Boolean.
394 if Base_Type (T) = Standard_Boolean then
395 return;
397 else
398 declare
399 KP : constant Node_Kind := Nkind (Parent (N));
401 begin
402 -- If result is to be used as a Condition in the syntax, no need
403 -- to convert it back, since if it was changed to Standard.Boolean
404 -- using Adjust_Condition, that is just fine for this usage.
406 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
407 return;
409 -- If result is an operand of another logical operation, no need
410 -- to reset its type, since Standard.Boolean is just fine, and
411 -- such operations always do Adjust_Condition on their operands.
413 elsif KP in N_Op_Boolean
414 or else KP in N_Short_Circuit
415 or else KP = N_Op_Not
416 then
417 return;
419 -- Otherwise we perform a conversion from the current type, which
420 -- must be Standard.Boolean, to the desired type. Use the base
421 -- type to prevent spurious constraint checks that are extraneous
422 -- to the transformation. The type and its base have the same
423 -- representation, standard or otherwise.
425 else
426 Set_Analyzed (N);
427 Rewrite (N, Convert_To (Base_Type (T), N));
428 Analyze_And_Resolve (N, Base_Type (T));
429 end if;
430 end;
431 end if;
432 end Adjust_Result_Type;
434 --------------------------
435 -- Append_Freeze_Action --
436 --------------------------
438 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
439 Fnode : Node_Id;
441 begin
442 Ensure_Freeze_Node (T);
443 Fnode := Freeze_Node (T);
445 if No (Actions (Fnode)) then
446 Set_Actions (Fnode, New_List (N));
447 else
448 Append (N, Actions (Fnode));
449 end if;
451 end Append_Freeze_Action;
453 ---------------------------
454 -- Append_Freeze_Actions --
455 ---------------------------
457 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
458 Fnode : Node_Id;
460 begin
461 if No (L) then
462 return;
463 end if;
465 Ensure_Freeze_Node (T);
466 Fnode := Freeze_Node (T);
468 if No (Actions (Fnode)) then
469 Set_Actions (Fnode, L);
470 else
471 Append_List (L, Actions (Fnode));
472 end if;
473 end Append_Freeze_Actions;
475 ------------------------------------
476 -- Build_Allocate_Deallocate_Proc --
477 ------------------------------------
479 procedure Build_Allocate_Deallocate_Proc
480 (N : Node_Id;
481 Is_Allocate : Boolean)
483 function Find_Object (E : Node_Id) return Node_Id;
484 -- Given an arbitrary expression of an allocator, try to find an object
485 -- reference in it, otherwise return the original expression.
487 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
488 -- Determine whether subprogram Subp denotes a custom allocate or
489 -- deallocate.
491 -----------------
492 -- Find_Object --
493 -----------------
495 function Find_Object (E : Node_Id) return Node_Id is
496 Expr : Node_Id;
498 begin
499 pragma Assert (Is_Allocate);
501 Expr := E;
502 loop
503 if Nkind (Expr) = N_Explicit_Dereference then
504 Expr := Prefix (Expr);
506 elsif Nkind (Expr) = N_Qualified_Expression then
507 Expr := Expression (Expr);
509 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
511 -- When interface class-wide types are involved in allocation,
512 -- the expander introduces several levels of address arithmetic
513 -- to perform dispatch table displacement. In this scenario the
514 -- object appears as:
516 -- Tag_Ptr (Base_Address (<object>'Address))
518 -- Detect this case and utilize the whole expression as the
519 -- "object" since it now points to the proper dispatch table.
521 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
522 exit;
524 -- Continue to strip the object
526 else
527 Expr := Expression (Expr);
528 end if;
530 else
531 exit;
532 end if;
533 end loop;
535 return Expr;
536 end Find_Object;
538 ---------------------------------
539 -- Is_Allocate_Deallocate_Proc --
540 ---------------------------------
542 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
543 begin
544 -- Look for a subprogram body with only one statement which is a
545 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
547 if Ekind (Subp) = E_Procedure
548 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
549 then
550 declare
551 HSS : constant Node_Id :=
552 Handled_Statement_Sequence (Parent (Parent (Subp)));
553 Proc : Entity_Id;
555 begin
556 if Present (Statements (HSS))
557 and then Nkind (First (Statements (HSS))) =
558 N_Procedure_Call_Statement
559 then
560 Proc := Entity (Name (First (Statements (HSS))));
562 return
563 Is_RTE (Proc, RE_Allocate_Any_Controlled)
564 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
565 end if;
566 end;
567 end if;
569 return False;
570 end Is_Allocate_Deallocate_Proc;
572 -- Local variables
574 Desig_Typ : Entity_Id;
575 Expr : Node_Id;
576 Needs_Fin : Boolean;
577 Pool_Id : Entity_Id;
578 Proc_To_Call : Node_Id := Empty;
579 Ptr_Typ : Entity_Id;
581 -- Start of processing for Build_Allocate_Deallocate_Proc
583 begin
584 -- Obtain the attributes of the allocation / deallocation
586 if Nkind (N) = N_Free_Statement then
587 Expr := Expression (N);
588 Ptr_Typ := Base_Type (Etype (Expr));
589 Proc_To_Call := Procedure_To_Call (N);
591 else
592 if Nkind (N) = N_Object_Declaration then
593 Expr := Expression (N);
594 else
595 Expr := N;
596 end if;
598 -- In certain cases an allocator with a qualified expression may
599 -- be relocated and used as the initialization expression of a
600 -- temporary:
602 -- before:
603 -- Obj : Ptr_Typ := new Desig_Typ'(...);
605 -- after:
606 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
607 -- Obj : Ptr_Typ := Tmp;
609 -- Since the allocator is always marked as analyzed to avoid infinite
610 -- expansion, it will never be processed by this routine given that
611 -- the designated type needs finalization actions. Detect this case
612 -- and complete the expansion of the allocator.
614 if Nkind (Expr) = N_Identifier
615 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
616 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
617 then
618 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
619 return;
620 end if;
622 -- The allocator may have been rewritten into something else in which
623 -- case the expansion performed by this routine does not apply.
625 if Nkind (Expr) /= N_Allocator then
626 return;
627 end if;
629 Ptr_Typ := Base_Type (Etype (Expr));
630 Proc_To_Call := Procedure_To_Call (Expr);
631 end if;
633 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
634 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
636 -- Handle concurrent types
638 if Is_Concurrent_Type (Desig_Typ)
639 and then Present (Corresponding_Record_Type (Desig_Typ))
640 then
641 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
642 end if;
644 -- Do not process allocations / deallocations without a pool
646 if No (Pool_Id) then
647 return;
649 -- Do not process allocations on / deallocations from the secondary
650 -- stack.
652 elsif Is_RTE (Pool_Id, RE_SS_Pool)
653 or else
654 (Nkind (Expr) = N_Allocator
655 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool))
656 then
657 return;
659 -- Optimize the case where we are using the default Global_Pool_Object,
660 -- and we don't need the heavy finalization machinery.
662 elsif Pool_Id = RTE (RE_Global_Pool_Object)
663 and then not Needs_Finalization (Desig_Typ)
664 then
665 return;
667 -- Do not replicate the machinery if the allocator / free has already
668 -- been expanded and has a custom Allocate / Deallocate.
670 elsif Present (Proc_To_Call)
671 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
672 then
673 return;
674 end if;
676 -- Finalization actions are required when the object to be allocated or
677 -- deallocated needs these actions and the associated access type is not
678 -- subject to pragma No_Heap_Finalization.
680 Needs_Fin :=
681 Needs_Finalization (Desig_Typ)
682 and then not No_Heap_Finalization (Ptr_Typ);
684 if Needs_Fin then
686 -- Certain run-time configurations and targets do not provide support
687 -- for controlled types.
689 if Restriction_Active (No_Finalization) then
690 return;
692 -- Do nothing if the access type may never allocate / deallocate
693 -- objects.
695 elsif No_Pool_Assigned (Ptr_Typ) then
696 return;
697 end if;
699 -- The allocation / deallocation of a controlled object must be
700 -- chained on / detached from a finalization master.
702 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
704 -- The only other kind of allocation / deallocation supported by this
705 -- routine is on / from a subpool.
707 elsif Nkind (Expr) = N_Allocator
708 and then No (Subpool_Handle_Name (Expr))
709 then
710 return;
711 end if;
713 declare
714 Loc : constant Source_Ptr := Sloc (N);
715 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
716 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
717 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
718 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
720 Actuals : List_Id;
721 Fin_Addr_Id : Entity_Id;
722 Fin_Mas_Act : Node_Id;
723 Fin_Mas_Id : Entity_Id;
724 Proc_To_Call : Entity_Id;
725 Subpool : Node_Id := Empty;
727 begin
728 -- Step 1: Construct all the actuals for the call to library routine
729 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
731 -- a) Storage pool
733 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
735 if Is_Allocate then
737 -- b) Subpool
739 if Nkind (Expr) = N_Allocator then
740 Subpool := Subpool_Handle_Name (Expr);
741 end if;
743 -- If a subpool is present it can be an arbitrary name, so make
744 -- the actual by copying the tree.
746 if Present (Subpool) then
747 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
748 else
749 Append_To (Actuals, Make_Null (Loc));
750 end if;
752 -- c) Finalization master
754 if Needs_Fin then
755 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
756 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
758 -- Handle the case where the master is actually a pointer to a
759 -- master. This case arises in build-in-place functions.
761 if Is_Access_Type (Etype (Fin_Mas_Id)) then
762 Append_To (Actuals, Fin_Mas_Act);
763 else
764 Append_To (Actuals,
765 Make_Attribute_Reference (Loc,
766 Prefix => Fin_Mas_Act,
767 Attribute_Name => Name_Unrestricted_Access));
768 end if;
769 else
770 Append_To (Actuals, Make_Null (Loc));
771 end if;
773 -- d) Finalize_Address
775 -- Primitive Finalize_Address is never generated in CodePeer mode
776 -- since it contains an Unchecked_Conversion.
778 if Needs_Fin and then not CodePeer_Mode then
779 Fin_Addr_Id := Finalize_Address (Desig_Typ);
780 pragma Assert (Present (Fin_Addr_Id));
782 Append_To (Actuals,
783 Make_Attribute_Reference (Loc,
784 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
785 Attribute_Name => Name_Unrestricted_Access));
786 else
787 Append_To (Actuals, Make_Null (Loc));
788 end if;
789 end if;
791 -- e) Address
792 -- f) Storage_Size
793 -- g) Alignment
795 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
796 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
798 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
799 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
801 -- For deallocation of class-wide types we obtain the value of
802 -- alignment from the Type Specific Record of the deallocated object.
803 -- This is needed because the frontend expansion of class-wide types
804 -- into equivalent types confuses the back end.
806 else
807 -- Generate:
808 -- Obj.all'Alignment
810 -- ... because 'Alignment applied to class-wide types is expanded
811 -- into the code that reads the value of alignment from the TSD
812 -- (see Expand_N_Attribute_Reference)
814 Append_To (Actuals,
815 Unchecked_Convert_To (RTE (RE_Storage_Offset),
816 Make_Attribute_Reference (Loc,
817 Prefix =>
818 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
819 Attribute_Name => Name_Alignment)));
820 end if;
822 -- h) Is_Controlled
824 if Needs_Fin then
825 Is_Controlled : declare
826 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
827 Flag_Expr : Node_Id;
828 Param : Node_Id;
829 Pref : Node_Id;
830 Temp : Node_Id;
832 begin
833 if Is_Allocate then
834 Temp := Find_Object (Expression (Expr));
835 else
836 Temp := Expr;
837 end if;
839 -- Processing for allocations where the expression is a subtype
840 -- indication.
842 if Is_Allocate
843 and then Is_Entity_Name (Temp)
844 and then Is_Type (Entity (Temp))
845 then
846 Flag_Expr :=
847 New_Occurrence_Of
848 (Boolean_Literals
849 (Needs_Finalization (Entity (Temp))), Loc);
851 -- The allocation / deallocation of a class-wide object relies
852 -- on a runtime check to determine whether the object is truly
853 -- controlled or not. Depending on this check, the finalization
854 -- machinery will request or reclaim extra storage reserved for
855 -- a list header.
857 elsif Is_Class_Wide_Type (Desig_Typ) then
859 -- Detect a special case where interface class-wide types
860 -- are involved as the object appears as:
862 -- Tag_Ptr (Base_Address (<object>'Address))
864 -- The expression already yields the proper tag, generate:
866 -- Temp.all
868 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
869 Param :=
870 Make_Explicit_Dereference (Loc,
871 Prefix => Relocate_Node (Temp));
873 -- In the default case, obtain the tag of the object about
874 -- to be allocated / deallocated. Generate:
876 -- Temp'Tag
878 -- If the object is an unchecked conversion (typically to
879 -- an access to class-wide type), we must preserve the
880 -- conversion to ensure that the object is seen as tagged
881 -- in the code that follows.
883 else
884 Pref := Temp;
886 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
887 then
888 Pref := Parent (Pref);
889 end if;
891 Param :=
892 Make_Attribute_Reference (Loc,
893 Prefix => Relocate_Node (Pref),
894 Attribute_Name => Name_Tag);
895 end if;
897 -- Generate:
898 -- Needs_Finalization (<Param>)
900 Flag_Expr :=
901 Make_Function_Call (Loc,
902 Name =>
903 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
904 Parameter_Associations => New_List (Param));
906 -- Processing for generic actuals
908 elsif Is_Generic_Actual_Type (Desig_Typ) then
909 Flag_Expr :=
910 New_Occurrence_Of (Boolean_Literals
911 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
913 -- The object does not require any specialized checks, it is
914 -- known to be controlled.
916 else
917 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
918 end if;
920 -- Create the temporary which represents the finalization state
921 -- of the expression. Generate:
923 -- F : constant Boolean := <Flag_Expr>;
925 Insert_Action (N,
926 Make_Object_Declaration (Loc,
927 Defining_Identifier => Flag_Id,
928 Constant_Present => True,
929 Object_Definition =>
930 New_Occurrence_Of (Standard_Boolean, Loc),
931 Expression => Flag_Expr));
933 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
934 end Is_Controlled;
936 -- The object is not controlled
938 else
939 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
940 end if;
942 -- i) On_Subpool
944 if Is_Allocate then
945 Append_To (Actuals,
946 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
947 end if;
949 -- Step 2: Build a wrapper Allocate / Deallocate which internally
950 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
952 -- Select the proper routine to call
954 if Is_Allocate then
955 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
956 else
957 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
958 end if;
960 -- Create a custom Allocate / Deallocate routine which has identical
961 -- profile to that of System.Storage_Pools.
963 Insert_Action (N,
964 Make_Subprogram_Body (Loc,
965 Specification =>
967 -- procedure Pnn
969 Make_Procedure_Specification (Loc,
970 Defining_Unit_Name => Proc_Id,
971 Parameter_Specifications => New_List (
973 -- P : Root_Storage_Pool
975 Make_Parameter_Specification (Loc,
976 Defining_Identifier => Make_Temporary (Loc, 'P'),
977 Parameter_Type =>
978 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
980 -- A : [out] Address
982 Make_Parameter_Specification (Loc,
983 Defining_Identifier => Addr_Id,
984 Out_Present => Is_Allocate,
985 Parameter_Type =>
986 New_Occurrence_Of (RTE (RE_Address), Loc)),
988 -- S : Storage_Count
990 Make_Parameter_Specification (Loc,
991 Defining_Identifier => Size_Id,
992 Parameter_Type =>
993 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
995 -- L : Storage_Count
997 Make_Parameter_Specification (Loc,
998 Defining_Identifier => Alig_Id,
999 Parameter_Type =>
1000 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
1002 Declarations => No_List,
1004 Handled_Statement_Sequence =>
1005 Make_Handled_Sequence_Of_Statements (Loc,
1006 Statements => New_List (
1007 Make_Procedure_Call_Statement (Loc,
1008 Name =>
1009 New_Occurrence_Of (Proc_To_Call, Loc),
1010 Parameter_Associations => Actuals)))),
1011 Suppress => All_Checks);
1013 -- The newly generated Allocate / Deallocate becomes the default
1014 -- procedure to call when the back end processes the allocation /
1015 -- deallocation.
1017 if Is_Allocate then
1018 Set_Procedure_To_Call (Expr, Proc_Id);
1019 else
1020 Set_Procedure_To_Call (N, Proc_Id);
1021 end if;
1022 end;
1023 end Build_Allocate_Deallocate_Proc;
1025 -------------------------------
1026 -- Build_Abort_Undefer_Block --
1027 -------------------------------
1029 function Build_Abort_Undefer_Block
1030 (Loc : Source_Ptr;
1031 Stmts : List_Id;
1032 Context : Node_Id) return Node_Id
1034 Exceptions_OK : constant Boolean :=
1035 not Restriction_Active (No_Exception_Propagation);
1037 AUD : Entity_Id;
1038 Blk : Node_Id;
1039 Blk_Id : Entity_Id;
1040 HSS : Node_Id;
1042 begin
1043 -- The block should be generated only when undeferring abort in the
1044 -- context of a potential exception.
1046 pragma Assert (Abort_Allowed and Exceptions_OK);
1048 -- Generate:
1049 -- begin
1050 -- <Stmts>
1051 -- at end
1052 -- Abort_Undefer_Direct;
1053 -- end;
1055 AUD := RTE (RE_Abort_Undefer_Direct);
1057 HSS :=
1058 Make_Handled_Sequence_Of_Statements (Loc,
1059 Statements => Stmts,
1060 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1062 Blk :=
1063 Make_Block_Statement (Loc,
1064 Handled_Statement_Sequence => HSS);
1065 Set_Is_Abort_Block (Blk);
1067 Add_Block_Identifier (Blk, Blk_Id);
1068 Expand_At_End_Handler (HSS, Blk_Id);
1070 -- Present the Abort_Undefer_Direct function to the back end to inline
1071 -- the call to the routine.
1073 Add_Inlined_Body (AUD, Context);
1075 return Blk;
1076 end Build_Abort_Undefer_Block;
1078 ---------------------------------
1079 -- Build_Class_Wide_Expression --
1080 ---------------------------------
1082 procedure Build_Class_Wide_Expression
1083 (Prag : Node_Id;
1084 Subp : Entity_Id;
1085 Par_Subp : Entity_Id;
1086 Adjust_Sloc : Boolean;
1087 Needs_Wrapper : out Boolean)
1089 function Replace_Entity (N : Node_Id) return Traverse_Result;
1090 -- Replace reference to formal of inherited operation or to primitive
1091 -- operation of root type, with corresponding entity for derived type,
1092 -- when constructing the class-wide condition of an overriding
1093 -- subprogram.
1095 --------------------
1096 -- Replace_Entity --
1097 --------------------
1099 function Replace_Entity (N : Node_Id) return Traverse_Result is
1100 New_E : Entity_Id;
1102 begin
1103 if Adjust_Sloc then
1104 Adjust_Inherited_Pragma_Sloc (N);
1105 end if;
1107 if Nkind (N) = N_Identifier
1108 and then Present (Entity (N))
1109 and then
1110 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1111 and then
1112 (Nkind (Parent (N)) /= N_Attribute_Reference
1113 or else Attribute_Name (Parent (N)) /= Name_Class)
1114 then
1115 -- The replacement does not apply to dispatching calls within the
1116 -- condition, but only to calls whose static tag is that of the
1117 -- parent type.
1119 if Is_Subprogram (Entity (N))
1120 and then Nkind (Parent (N)) = N_Function_Call
1121 and then Present (Controlling_Argument (Parent (N)))
1122 then
1123 return OK;
1124 end if;
1126 -- Determine whether entity has a renaming
1128 New_E := Type_Map.Get (Entity (N));
1130 if Present (New_E) then
1131 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1133 -- If the entity is an overridden primitive and we are not
1134 -- in GNATprove mode, we must build a wrapper for the current
1135 -- inherited operation. If the reference is the prefix of an
1136 -- attribute such as 'Result (or others ???) there is no need
1137 -- for a wrapper: the condition is just rewritten in terms of
1138 -- the inherited subprogram.
1140 if Is_Subprogram (New_E)
1141 and then Nkind (Parent (N)) /= N_Attribute_Reference
1142 and then not GNATprove_Mode
1143 then
1144 Needs_Wrapper := True;
1145 end if;
1146 end if;
1148 -- Check that there are no calls left to abstract operations if
1149 -- the current subprogram is not abstract.
1151 if Nkind (Parent (N)) = N_Function_Call
1152 and then N = Name (Parent (N))
1153 then
1154 if not Is_Abstract_Subprogram (Subp)
1155 and then Is_Abstract_Subprogram (Entity (N))
1156 then
1157 Error_Msg_Sloc := Sloc (Current_Scope);
1158 Error_Msg_Node_2 := Subp;
1159 if Comes_From_Source (Subp) then
1160 Error_Msg_NE
1161 ("cannot call abstract subprogram & in inherited "
1162 & "condition for&#", Subp, Entity (N));
1163 else
1164 Error_Msg_NE
1165 ("cannot call abstract subprogram & in inherited "
1166 & "condition for inherited&#", Subp, Entity (N));
1167 end if;
1169 -- In SPARK mode, reject an inherited condition for an
1170 -- inherited operation if it contains a call to an overriding
1171 -- operation, because this implies that the pre/postconditions
1172 -- of the inherited operation have changed silently.
1174 elsif SPARK_Mode = On
1175 and then Warn_On_Suspicious_Contract
1176 and then Present (Alias (Subp))
1177 and then Present (New_E)
1178 and then Comes_From_Source (New_E)
1179 then
1180 Error_Msg_N
1181 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1182 Parent (Subp));
1183 Error_Msg_Sloc := Sloc (New_E);
1184 Error_Msg_Node_2 := Subp;
1185 Error_Msg_NE
1186 ("\overriding of&# forces overriding of&",
1187 Parent (Subp), New_E);
1188 end if;
1189 end if;
1191 -- Update type of function call node, which should be the same as
1192 -- the function's return type.
1194 if Is_Subprogram (Entity (N))
1195 and then Nkind (Parent (N)) = N_Function_Call
1196 then
1197 Set_Etype (Parent (N), Etype (Entity (N)));
1198 end if;
1200 -- The whole expression will be reanalyzed
1202 elsif Nkind (N) in N_Has_Etype then
1203 Set_Analyzed (N, False);
1204 end if;
1206 return OK;
1207 end Replace_Entity;
1209 procedure Replace_Condition_Entities is
1210 new Traverse_Proc (Replace_Entity);
1212 -- Local variables
1214 Par_Formal : Entity_Id;
1215 Subp_Formal : Entity_Id;
1217 -- Start of processing for Build_Class_Wide_Expression
1219 begin
1220 Needs_Wrapper := False;
1222 -- Add mapping from old formals to new formals
1224 Par_Formal := First_Formal (Par_Subp);
1225 Subp_Formal := First_Formal (Subp);
1227 while Present (Par_Formal) and then Present (Subp_Formal) loop
1228 Type_Map.Set (Par_Formal, Subp_Formal);
1229 Next_Formal (Par_Formal);
1230 Next_Formal (Subp_Formal);
1231 end loop;
1233 Replace_Condition_Entities (Prag);
1234 end Build_Class_Wide_Expression;
1236 --------------------
1237 -- Build_DIC_Call --
1238 --------------------
1240 function Build_DIC_Call
1241 (Loc : Source_Ptr;
1242 Obj_Id : Entity_Id;
1243 Typ : Entity_Id) return Node_Id
1245 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1246 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1248 begin
1249 return
1250 Make_Procedure_Call_Statement (Loc,
1251 Name => New_Occurrence_Of (Proc_Id, Loc),
1252 Parameter_Associations => New_List (
1253 Make_Unchecked_Type_Conversion (Loc,
1254 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1255 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1256 end Build_DIC_Call;
1258 ------------------------------
1259 -- Build_DIC_Procedure_Body --
1260 ------------------------------
1262 -- WARNING: This routine manages Ghost regions. Return statements must be
1263 -- replaced by gotos which jump to the end of the routine and restore the
1264 -- Ghost mode.
1266 procedure Build_DIC_Procedure_Body
1267 (Typ : Entity_Id;
1268 For_Freeze : Boolean := False)
1270 procedure Add_DIC_Check
1271 (DIC_Prag : Node_Id;
1272 DIC_Expr : Node_Id;
1273 Stmts : in out List_Id);
1274 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1275 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1276 -- is added to list Stmts.
1278 procedure Add_Inherited_DIC
1279 (DIC_Prag : Node_Id;
1280 Par_Typ : Entity_Id;
1281 Deriv_Typ : Entity_Id;
1282 Stmts : in out List_Id);
1283 -- Add a runtime check to verify the assertion expression of inherited
1284 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1285 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1286 -- pragma. All generated code is added to list Stmts.
1288 procedure Add_Inherited_Tagged_DIC
1289 (DIC_Prag : Node_Id;
1290 Par_Typ : Entity_Id;
1291 Deriv_Typ : Entity_Id;
1292 Stmts : in out List_Id);
1293 -- Add a runtime check to verify assertion expression DIC_Expr of
1294 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1295 -- postcondition-like runtime semantics to the check. Par_Typ is the
1296 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1297 -- derived type inheriting the DIC pragma. All generated code is added
1298 -- to list Stmts.
1300 procedure Add_Own_DIC
1301 (DIC_Prag : Node_Id;
1302 DIC_Typ : Entity_Id;
1303 Stmts : in out List_Id);
1304 -- Add a runtime check to verify the assertion expression of pragma
1305 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1306 -- is added to list Stmts.
1308 -------------------
1309 -- Add_DIC_Check --
1310 -------------------
1312 procedure Add_DIC_Check
1313 (DIC_Prag : Node_Id;
1314 DIC_Expr : Node_Id;
1315 Stmts : in out List_Id)
1317 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1318 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1320 begin
1321 -- The DIC pragma is ignored, nothing left to do
1323 if Is_Ignored (DIC_Prag) then
1324 null;
1326 -- Otherwise the DIC expression must be checked at run time.
1327 -- Generate:
1329 -- pragma Check (<Nam>, <DIC_Expr>);
1331 else
1332 Append_New_To (Stmts,
1333 Make_Pragma (Loc,
1334 Pragma_Identifier =>
1335 Make_Identifier (Loc, Name_Check),
1337 Pragma_Argument_Associations => New_List (
1338 Make_Pragma_Argument_Association (Loc,
1339 Expression => Make_Identifier (Loc, Nam)),
1341 Make_Pragma_Argument_Association (Loc,
1342 Expression => DIC_Expr))));
1343 end if;
1344 end Add_DIC_Check;
1346 -----------------------
1347 -- Add_Inherited_DIC --
1348 -----------------------
1350 procedure Add_Inherited_DIC
1351 (DIC_Prag : Node_Id;
1352 Par_Typ : Entity_Id;
1353 Deriv_Typ : Entity_Id;
1354 Stmts : in out List_Id)
1356 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1357 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1358 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1359 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1360 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1362 begin
1363 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1365 -- Verify the inherited DIC assertion expression by calling the DIC
1366 -- procedure of the parent type.
1368 -- Generate:
1369 -- <Par_Typ>DIC (Par_Typ (_object));
1371 Append_New_To (Stmts,
1372 Make_Procedure_Call_Statement (Loc,
1373 Name => New_Occurrence_Of (Par_Proc, Loc),
1374 Parameter_Associations => New_List (
1375 Convert_To
1376 (Typ => Etype (Par_Obj),
1377 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1378 end Add_Inherited_DIC;
1380 ------------------------------
1381 -- Add_Inherited_Tagged_DIC --
1382 ------------------------------
1384 procedure Add_Inherited_Tagged_DIC
1385 (DIC_Prag : Node_Id;
1386 Par_Typ : Entity_Id;
1387 Deriv_Typ : Entity_Id;
1388 Stmts : in out List_Id)
1390 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1391 DIC_Args : constant List_Id :=
1392 Pragma_Argument_Associations (DIC_Prag);
1393 DIC_Arg : constant Node_Id := First (DIC_Args);
1394 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1395 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1397 Expr : Node_Id;
1399 begin
1400 -- The processing of an inherited DIC assertion expression starts off
1401 -- with a copy of the original parent expression where all references
1402 -- to the parent type have already been replaced with references to
1403 -- the _object formal parameter of the parent type's DIC procedure.
1405 pragma Assert (Present (DIC_Expr));
1406 Expr := New_Copy_Tree (DIC_Expr);
1408 -- Perform the following substitutions:
1410 -- * Replace a reference to the _object parameter of the parent
1411 -- type's DIC procedure with a reference to the _object parameter
1412 -- of the derived types' DIC procedure.
1414 -- * Replace a reference to a discriminant of the parent type with
1415 -- a suitable value from the point of view of the derived type.
1417 -- * Replace a call to an overridden parent primitive with a call
1418 -- to the overriding derived type primitive.
1420 -- * Replace a call to an inherited parent primitive with a call to
1421 -- the internally-generated inherited derived type primitive.
1423 -- Note that primitives defined in the private part are automatically
1424 -- handled by the overriding/inheritance mechanism and do not require
1425 -- an extra replacement pass.
1427 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1429 Replace_References
1430 (Expr => Expr,
1431 Par_Typ => Par_Typ,
1432 Deriv_Typ => Deriv_Typ,
1433 Par_Obj => First_Formal (Par_Proc),
1434 Deriv_Obj => First_Formal (Deriv_Proc));
1436 -- Once the DIC assertion expression is fully processed, add a check
1437 -- to the statements of the DIC procedure.
1439 Add_DIC_Check
1440 (DIC_Prag => DIC_Prag,
1441 DIC_Expr => Expr,
1442 Stmts => Stmts);
1443 end Add_Inherited_Tagged_DIC;
1445 -----------------
1446 -- Add_Own_DIC --
1447 -----------------
1449 procedure Add_Own_DIC
1450 (DIC_Prag : Node_Id;
1451 DIC_Typ : Entity_Id;
1452 Stmts : in out List_Id)
1454 DIC_Args : constant List_Id :=
1455 Pragma_Argument_Associations (DIC_Prag);
1456 DIC_Arg : constant Node_Id := First (DIC_Args);
1457 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1458 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1459 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1460 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1462 procedure Preanalyze_Own_DIC_For_ASIS;
1463 -- Preanalyze the original DIC expression of an aspect or a source
1464 -- pragma for ASIS.
1466 ---------------------------------
1467 -- Preanalyze_Own_DIC_For_ASIS --
1468 ---------------------------------
1470 procedure Preanalyze_Own_DIC_For_ASIS is
1471 Expr : Node_Id := Empty;
1473 begin
1474 -- The DIC pragma is a source construct, preanalyze the original
1475 -- expression of the pragma.
1477 if Comes_From_Source (DIC_Prag) then
1478 Expr := DIC_Expr;
1480 -- Otherwise preanalyze the expression of the corresponding aspect
1482 elsif Present (DIC_Asp) then
1483 Expr := Expression (DIC_Asp);
1484 end if;
1486 -- The expression must be subjected to the same substitutions as
1487 -- the copy used in the generation of the runtime check.
1489 if Present (Expr) then
1490 Replace_Type_References
1491 (Expr => Expr,
1492 Typ => DIC_Typ,
1493 Obj_Id => Obj_Id);
1495 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1496 end if;
1497 end Preanalyze_Own_DIC_For_ASIS;
1499 -- Local variables
1501 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1503 Expr : Node_Id;
1505 -- Start of processing for Add_Own_DIC
1507 begin
1508 Expr := New_Copy_Tree (DIC_Expr);
1510 -- Perform the following substitution:
1512 -- * Replace the current instance of DIC_Typ with a reference to
1513 -- the _object formal parameter of the DIC procedure.
1515 Replace_Type_References
1516 (Expr => Expr,
1517 Typ => DIC_Typ,
1518 Obj_Id => Obj_Id);
1520 -- Preanalyze the DIC expression to detect errors and at the same
1521 -- time capture the visibility of the proper package part.
1523 Set_Parent (Expr, Typ_Decl);
1524 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1526 -- Save a copy of the expression with all replacements and analysis
1527 -- already taken place in case a derived type inherits the pragma.
1528 -- The copy will be used as the foundation of the derived type's own
1529 -- version of the DIC assertion expression.
1531 if Is_Tagged_Type (DIC_Typ) then
1532 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1533 end if;
1535 -- If the pragma comes from an aspect specification, replace the
1536 -- saved expression because all type references must be substituted
1537 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1538 -- routines.
1540 if Present (DIC_Asp) then
1541 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1542 end if;
1544 -- Preanalyze the original DIC expression for ASIS
1546 if ASIS_Mode then
1547 Preanalyze_Own_DIC_For_ASIS;
1548 end if;
1550 -- Once the DIC assertion expression is fully processed, add a check
1551 -- to the statements of the DIC procedure.
1553 Add_DIC_Check
1554 (DIC_Prag => DIC_Prag,
1555 DIC_Expr => Expr,
1556 Stmts => Stmts);
1557 end Add_Own_DIC;
1559 -- Local variables
1561 Loc : constant Source_Ptr := Sloc (Typ);
1563 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1564 -- Save the Ghost mode to restore on exit
1566 DIC_Prag : Node_Id;
1567 DIC_Typ : Entity_Id;
1568 Dummy_1 : Entity_Id;
1569 Dummy_2 : Entity_Id;
1570 Proc_Body : Node_Id;
1571 Proc_Body_Id : Entity_Id;
1572 Proc_Decl : Node_Id;
1573 Proc_Id : Entity_Id;
1574 Stmts : List_Id := No_List;
1576 Build_Body : Boolean := False;
1577 -- Flag set when the type requires a DIC procedure body to be built
1579 Work_Typ : Entity_Id;
1580 -- The working type
1582 -- Start of processing for Build_DIC_Procedure_Body
1584 begin
1585 Work_Typ := Base_Type (Typ);
1587 -- Do not process class-wide types as these are Itypes, but lack a first
1588 -- subtype (see below).
1590 if Is_Class_Wide_Type (Work_Typ) then
1591 return;
1593 -- Do not process the underlying full view of a private type. There is
1594 -- no way to get back to the partial view, plus the body will be built
1595 -- by the full view or the base type.
1597 elsif Is_Underlying_Full_View (Work_Typ) then
1598 return;
1600 -- Use the first subtype when dealing with various base types
1602 elsif Is_Itype (Work_Typ) then
1603 Work_Typ := First_Subtype (Work_Typ);
1605 -- The input denotes the corresponding record type of a protected or a
1606 -- task type. Work with the concurrent type because the corresponding
1607 -- record type may not be visible to clients of the type.
1609 elsif Ekind (Work_Typ) = E_Record_Type
1610 and then Is_Concurrent_Record_Type (Work_Typ)
1611 then
1612 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1613 end if;
1615 -- The working type may be subject to pragma Ghost. Set the mode now to
1616 -- ensure that the DIC procedure is properly marked as Ghost.
1618 Set_Ghost_Mode (Work_Typ);
1620 -- The working type must be either define a DIC pragma of its own or
1621 -- inherit one from a parent type.
1623 pragma Assert (Has_DIC (Work_Typ));
1625 -- Recover the type which defines the DIC pragma. This is either the
1626 -- working type itself or a parent type when the pragma is inherited.
1628 DIC_Typ := Find_DIC_Type (Work_Typ);
1629 pragma Assert (Present (DIC_Typ));
1631 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1632 pragma Assert (Present (DIC_Prag));
1634 -- Nothing to do if pragma DIC appears without an argument or its sole
1635 -- argument is "null".
1637 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1638 goto Leave;
1639 end if;
1641 -- The working type may lack a DIC procedure declaration. This may be
1642 -- due to several reasons:
1644 -- * The working type's own DIC pragma does not contain a verifiable
1645 -- assertion expression. In this case there is no need to build a
1646 -- DIC procedure because there is nothing to check.
1648 -- * The working type derives from a parent type. In this case a DIC
1649 -- procedure should be built only when the inherited DIC pragma has
1650 -- a verifiable assertion expression.
1652 Proc_Id := DIC_Procedure (Work_Typ);
1654 -- Build a DIC procedure declaration when the working type derives from
1655 -- a parent type.
1657 if No (Proc_Id) then
1658 Build_DIC_Procedure_Declaration (Work_Typ);
1659 Proc_Id := DIC_Procedure (Work_Typ);
1660 end if;
1662 -- At this point there should be a DIC procedure declaration
1664 pragma Assert (Present (Proc_Id));
1665 Proc_Decl := Unit_Declaration_Node (Proc_Id);
1667 -- Nothing to do if the DIC procedure already has a body
1669 if Present (Corresponding_Body (Proc_Decl)) then
1670 goto Leave;
1671 end if;
1673 -- Emulate the environment of the DIC procedure by installing its scope
1674 -- and formal parameters.
1676 Push_Scope (Proc_Id);
1677 Install_Formals (Proc_Id);
1679 -- The working type defines its own DIC pragma. Replace the current
1680 -- instance of the working type with the formal of the DIC procedure.
1681 -- Note that there is no need to consider inherited DIC pragmas from
1682 -- parent types because the working type's DIC pragma "hides" all
1683 -- inherited DIC pragmas.
1685 if Has_Own_DIC (Work_Typ) then
1686 pragma Assert (DIC_Typ = Work_Typ);
1688 Add_Own_DIC
1689 (DIC_Prag => DIC_Prag,
1690 DIC_Typ => DIC_Typ,
1691 Stmts => Stmts);
1693 Build_Body := True;
1695 -- Otherwise the working type inherits a DIC pragma from a parent type.
1696 -- This processing is carried out when the type is frozen because the
1697 -- state of all parent discriminants is known at that point. Note that
1698 -- it is semantically sound to delay the creation of the DIC procedure
1699 -- body till the freeze point. If the type has a DIC pragma of its own,
1700 -- then the DIC procedure body would have already been constructed at
1701 -- the end of the visible declarations and all parent DIC pragmas are
1702 -- effectively "hidden" and irrelevant.
1704 elsif For_Freeze then
1705 pragma Assert (Has_Inherited_DIC (Work_Typ));
1706 pragma Assert (DIC_Typ /= Work_Typ);
1708 -- The working type is tagged. The verification of the assertion
1709 -- expression is subject to the same semantics as class-wide pre-
1710 -- and postconditions.
1712 if Is_Tagged_Type (Work_Typ) then
1713 Add_Inherited_Tagged_DIC
1714 (DIC_Prag => DIC_Prag,
1715 Par_Typ => DIC_Typ,
1716 Deriv_Typ => Work_Typ,
1717 Stmts => Stmts);
1719 -- Otherwise the working type is not tagged. Verify the assertion
1720 -- expression of the inherited DIC pragma by directly calling the
1721 -- DIC procedure of the parent type.
1723 else
1724 Add_Inherited_DIC
1725 (DIC_Prag => DIC_Prag,
1726 Par_Typ => DIC_Typ,
1727 Deriv_Typ => Work_Typ,
1728 Stmts => Stmts);
1729 end if;
1731 Build_Body := True;
1732 end if;
1734 End_Scope;
1736 if Build_Body then
1738 -- Produce an empty completing body in the following cases:
1739 -- * Assertions are disabled
1740 -- * The DIC Assertion_Policy is Ignore
1741 -- * Pragma DIC appears without an argument
1742 -- * Pragma DIC appears with argument "null"
1744 if No (Stmts) then
1745 Stmts := New_List (Make_Null_Statement (Loc));
1746 end if;
1748 -- Generate:
1749 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1750 -- begin
1751 -- <Stmts>
1752 -- end <Work_Typ>DIC;
1754 Proc_Body :=
1755 Make_Subprogram_Body (Loc,
1756 Specification =>
1757 Copy_Subprogram_Spec (Parent (Proc_Id)),
1758 Declarations => Empty_List,
1759 Handled_Statement_Sequence =>
1760 Make_Handled_Sequence_Of_Statements (Loc,
1761 Statements => Stmts));
1762 Proc_Body_Id := Defining_Entity (Proc_Body);
1764 -- Perform minor decoration in case the body is not analyzed
1766 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1767 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1768 Set_Scope (Proc_Body_Id, Current_Scope);
1770 -- Link both spec and body to avoid generating duplicates
1772 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1773 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1775 -- The body should not be inserted into the tree when the context
1776 -- is ASIS or a generic unit because it is not part of the template.
1777 -- Note that the body must still be generated in order to resolve the
1778 -- DIC assertion expression.
1780 if ASIS_Mode or Inside_A_Generic then
1781 null;
1783 -- Semi-insert the body into the tree for GNATprove by setting its
1784 -- Parent field. This allows for proper upstream tree traversals.
1786 elsif GNATprove_Mode then
1787 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1789 -- Otherwise the body is part of the freezing actions of the working
1790 -- type.
1792 else
1793 Append_Freeze_Action (Work_Typ, Proc_Body);
1794 end if;
1795 end if;
1797 <<Leave>>
1798 Restore_Ghost_Mode (Saved_GM);
1799 end Build_DIC_Procedure_Body;
1801 -------------------------------------
1802 -- Build_DIC_Procedure_Declaration --
1803 -------------------------------------
1805 -- WARNING: This routine manages Ghost regions. Return statements must be
1806 -- replaced by gotos which jump to the end of the routine and restore the
1807 -- Ghost mode.
1809 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1810 Loc : constant Source_Ptr := Sloc (Typ);
1812 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1813 -- Save the Ghost mode to restore on exit
1815 DIC_Prag : Node_Id;
1816 DIC_Typ : Entity_Id;
1817 Proc_Decl : Node_Id;
1818 Proc_Id : Entity_Id;
1819 Typ_Decl : Node_Id;
1821 CRec_Typ : Entity_Id;
1822 -- The corresponding record type of Full_Typ
1824 Full_Base : Entity_Id;
1825 -- The base type of Full_Typ
1827 Full_Typ : Entity_Id;
1828 -- The full view of working type
1830 Obj_Id : Entity_Id;
1831 -- The _object formal parameter of the DIC procedure
1833 Priv_Typ : Entity_Id;
1834 -- The partial view of working type
1836 Work_Typ : Entity_Id;
1837 -- The working type
1839 begin
1840 Work_Typ := Base_Type (Typ);
1842 -- Do not process class-wide types as these are Itypes, but lack a first
1843 -- subtype (see below).
1845 if Is_Class_Wide_Type (Work_Typ) then
1846 return;
1848 -- Do not process the underlying full view of a private type. There is
1849 -- no way to get back to the partial view, plus the body will be built
1850 -- by the full view or the base type.
1852 elsif Is_Underlying_Full_View (Work_Typ) then
1853 return;
1855 -- Use the first subtype when dealing with various base types
1857 elsif Is_Itype (Work_Typ) then
1858 Work_Typ := First_Subtype (Work_Typ);
1860 -- The input denotes the corresponding record type of a protected or a
1861 -- task type. Work with the concurrent type because the corresponding
1862 -- record type may not be visible to clients of the type.
1864 elsif Ekind (Work_Typ) = E_Record_Type
1865 and then Is_Concurrent_Record_Type (Work_Typ)
1866 then
1867 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1868 end if;
1870 -- The working type may be subject to pragma Ghost. Set the mode now to
1871 -- ensure that the DIC procedure is properly marked as Ghost.
1873 Set_Ghost_Mode (Work_Typ);
1875 -- The type must be either subject to a DIC pragma or inherit one from a
1876 -- parent type.
1878 pragma Assert (Has_DIC (Work_Typ));
1880 -- Recover the type which defines the DIC pragma. This is either the
1881 -- working type itself or a parent type when the pragma is inherited.
1883 DIC_Typ := Find_DIC_Type (Work_Typ);
1884 pragma Assert (Present (DIC_Typ));
1886 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1887 pragma Assert (Present (DIC_Prag));
1889 -- Nothing to do if pragma DIC appears without an argument or its sole
1890 -- argument is "null".
1892 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1893 goto Leave;
1895 -- Nothing to do if the type already has a DIC procedure
1897 elsif Present (DIC_Procedure (Work_Typ)) then
1898 goto Leave;
1899 end if;
1901 Proc_Id :=
1902 Make_Defining_Identifier (Loc,
1903 Chars =>
1904 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1906 -- Perform minor decoration in case the declaration is not analyzed
1908 Set_Ekind (Proc_Id, E_Procedure);
1909 Set_Etype (Proc_Id, Standard_Void_Type);
1910 Set_Scope (Proc_Id, Current_Scope);
1912 Set_Is_DIC_Procedure (Proc_Id);
1913 Set_DIC_Procedure (Work_Typ, Proc_Id);
1915 -- The DIC procedure requires debug info when the assertion expression
1916 -- is subject to Source Coverage Obligations.
1918 if Opt.Generate_SCO then
1919 Set_Needs_Debug_Info (Proc_Id);
1920 end if;
1922 -- Obtain all views of the input type
1924 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1926 -- Associate the DIC procedure and various relevant flags with all views
1928 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1929 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1930 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1931 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1933 -- The declaration of the DIC procedure must be inserted after the
1934 -- declaration of the partial view as this allows for proper external
1935 -- visibility.
1937 if Present (Priv_Typ) then
1938 Typ_Decl := Declaration_Node (Priv_Typ);
1940 -- Derived types with the full view as parent do not have a partial
1941 -- view. Insert the DIC procedure after the derived type.
1943 else
1944 Typ_Decl := Declaration_Node (Full_Typ);
1945 end if;
1947 -- The type should have a declarative node
1949 pragma Assert (Present (Typ_Decl));
1951 -- Create the formal parameter which emulates the variable-like behavior
1952 -- of the type's current instance.
1954 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1956 -- Perform minor decoration in case the declaration is not analyzed
1958 Set_Ekind (Obj_Id, E_In_Parameter);
1959 Set_Etype (Obj_Id, Work_Typ);
1960 Set_Scope (Obj_Id, Proc_Id);
1962 Set_First_Entity (Proc_Id, Obj_Id);
1964 -- Generate:
1965 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1967 Proc_Decl :=
1968 Make_Subprogram_Declaration (Loc,
1969 Specification =>
1970 Make_Procedure_Specification (Loc,
1971 Defining_Unit_Name => Proc_Id,
1972 Parameter_Specifications => New_List (
1973 Make_Parameter_Specification (Loc,
1974 Defining_Identifier => Obj_Id,
1975 Parameter_Type =>
1976 New_Occurrence_Of (Work_Typ, Loc)))));
1978 -- The declaration should not be inserted into the tree when the context
1979 -- is ASIS or a generic unit because it is not part of the template.
1981 if ASIS_Mode or Inside_A_Generic then
1982 null;
1984 -- Semi-insert the declaration into the tree for GNATprove by setting
1985 -- its Parent field. This allows for proper upstream tree traversals.
1987 elsif GNATprove_Mode then
1988 Set_Parent (Proc_Decl, Parent (Typ_Decl));
1990 -- Otherwise insert the declaration
1992 else
1993 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
1994 end if;
1996 <<Leave>>
1997 Restore_Ghost_Mode (Saved_GM);
1998 end Build_DIC_Procedure_Declaration;
2000 ------------------------------------
2001 -- Build_Invariant_Procedure_Body --
2002 ------------------------------------
2004 -- WARNING: This routine manages Ghost regions. Return statements must be
2005 -- replaced by gotos which jump to the end of the routine and restore the
2006 -- Ghost mode.
2008 procedure Build_Invariant_Procedure_Body
2009 (Typ : Entity_Id;
2010 Partial_Invariant : Boolean := False)
2012 Loc : constant Source_Ptr := Sloc (Typ);
2014 Pragmas_Seen : Elist_Id := No_Elist;
2015 -- This list contains all invariant pragmas processed so far. The list
2016 -- is used to avoid generating redundant invariant checks.
2018 Produced_Check : Boolean := False;
2019 -- This flag tracks whether the type has produced at least one invariant
2020 -- check. The flag is used as a sanity check at the end of the routine.
2022 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2023 -- intentionally unnested to avoid deep indentation of code.
2025 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2026 -- they emit checks, loops (for arrays) and case statements (for record
2027 -- variant parts) only when there are invariants to verify. This keeps
2028 -- the body of the invariant procedure free of useless code.
2030 procedure Add_Array_Component_Invariants
2031 (T : Entity_Id;
2032 Obj_Id : Entity_Id;
2033 Checks : in out List_Id);
2034 -- Generate an invariant check for each component of array type T.
2035 -- Obj_Id denotes the entity of the _object formal parameter of the
2036 -- invariant procedure. All created checks are added to list Checks.
2038 procedure Add_Inherited_Invariants
2039 (T : Entity_Id;
2040 Priv_Typ : Entity_Id;
2041 Full_Typ : Entity_Id;
2042 Obj_Id : Entity_Id;
2043 Checks : in out List_Id);
2044 -- Generate an invariant check for each inherited class-wide invariant
2045 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2046 -- the partial and full view of the parent type. Obj_Id denotes the
2047 -- entity of the _object formal parameter of the invariant procedure.
2048 -- All created checks are added to list Checks.
2050 procedure Add_Interface_Invariants
2051 (T : Entity_Id;
2052 Obj_Id : Entity_Id;
2053 Checks : in out List_Id);
2054 -- Generate an invariant check for each inherited class-wide invariant
2055 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2056 -- entity of the _object formal parameter of the invariant procedure.
2057 -- All created checks are added to list Checks.
2059 procedure Add_Invariant_Check
2060 (Prag : Node_Id;
2061 Expr : Node_Id;
2062 Checks : in out List_Id;
2063 Inherited : Boolean := False);
2064 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2065 -- verify assertion expression Expr of pragma Prag. All generated code
2066 -- is added to list Checks. Flag Inherited should be set when the pragma
2067 -- is inherited from a parent or interface type.
2069 procedure Add_Own_Invariants
2070 (T : Entity_Id;
2071 Obj_Id : Entity_Id;
2072 Checks : in out List_Id;
2073 Priv_Item : Node_Id := Empty);
2074 -- Generate an invariant check for each invariant found for type T.
2075 -- Obj_Id denotes the entity of the _object formal parameter of the
2076 -- invariant procedure. All created checks are added to list Checks.
2077 -- Priv_Item denotes the first rep item of the private type.
2079 procedure Add_Parent_Invariants
2080 (T : Entity_Id;
2081 Obj_Id : Entity_Id;
2082 Checks : in out List_Id);
2083 -- Generate an invariant check for each inherited class-wide invariant
2084 -- coming from all parent types of type T. Obj_Id denotes the entity of
2085 -- the _object formal parameter of the invariant procedure. All created
2086 -- checks are added to list Checks.
2088 procedure Add_Record_Component_Invariants
2089 (T : Entity_Id;
2090 Obj_Id : Entity_Id;
2091 Checks : in out List_Id);
2092 -- Generate an invariant check for each component of record type T.
2093 -- Obj_Id denotes the entity of the _object formal parameter of the
2094 -- invariant procedure. All created checks are added to list Checks.
2096 ------------------------------------
2097 -- Add_Array_Component_Invariants --
2098 ------------------------------------
2100 procedure Add_Array_Component_Invariants
2101 (T : Entity_Id;
2102 Obj_Id : Entity_Id;
2103 Checks : in out List_Id)
2105 Comp_Typ : constant Entity_Id := Component_Type (T);
2106 Dims : constant Pos := Number_Dimensions (T);
2108 procedure Process_Array_Component
2109 (Indices : List_Id;
2110 Comp_Checks : in out List_Id);
2111 -- Generate an invariant check for an array component identified by
2112 -- the indices in list Indices. All created checks are added to list
2113 -- Comp_Checks.
2115 procedure Process_One_Dimension
2116 (Dim : Pos;
2117 Indices : List_Id;
2118 Dim_Checks : in out List_Id);
2119 -- Generate a loop over the Nth dimension Dim of an array type. List
2120 -- Indices contains all array indices for the dimension. All created
2121 -- checks are added to list Dim_Checks.
2123 -----------------------------
2124 -- Process_Array_Component --
2125 -----------------------------
2127 procedure Process_Array_Component
2128 (Indices : List_Id;
2129 Comp_Checks : in out List_Id)
2131 Proc_Id : Entity_Id;
2133 begin
2134 if Has_Invariants (Comp_Typ) then
2136 -- In GNATprove mode, the component invariants are checked by
2137 -- other means. They should not be added to the array type
2138 -- invariant procedure, so that the procedure can be used to
2139 -- check the array type invariants if any.
2141 if GNATprove_Mode then
2142 null;
2144 else
2145 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2147 -- The component type should have an invariant procedure
2148 -- if it has invariants of its own or inherits class-wide
2149 -- invariants from parent or interface types.
2151 pragma Assert (Present (Proc_Id));
2153 -- Generate:
2154 -- <Comp_Typ>Invariant (_object (<Indices>));
2156 -- Note that the invariant procedure may have a null body if
2157 -- assertions are disabled or Assertion_Policy Ignore is in
2158 -- effect.
2160 if not Has_Null_Body (Proc_Id) then
2161 Append_New_To (Comp_Checks,
2162 Make_Procedure_Call_Statement (Loc,
2163 Name =>
2164 New_Occurrence_Of (Proc_Id, Loc),
2165 Parameter_Associations => New_List (
2166 Make_Indexed_Component (Loc,
2167 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2168 Expressions => New_Copy_List (Indices)))));
2169 end if;
2170 end if;
2172 Produced_Check := True;
2173 end if;
2174 end Process_Array_Component;
2176 ---------------------------
2177 -- Process_One_Dimension --
2178 ---------------------------
2180 procedure Process_One_Dimension
2181 (Dim : Pos;
2182 Indices : List_Id;
2183 Dim_Checks : in out List_Id)
2185 Comp_Checks : List_Id := No_List;
2186 Index : Entity_Id;
2188 begin
2189 -- Generate the invariant checks for the array component after all
2190 -- dimensions have produced their respective loops.
2192 if Dim > Dims then
2193 Process_Array_Component
2194 (Indices => Indices,
2195 Comp_Checks => Dim_Checks);
2197 -- Otherwise create a loop for the current dimension
2199 else
2200 -- Create a new loop variable for each dimension
2202 Index :=
2203 Make_Defining_Identifier (Loc,
2204 Chars => New_External_Name ('I', Dim));
2205 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2207 Process_One_Dimension
2208 (Dim => Dim + 1,
2209 Indices => Indices,
2210 Dim_Checks => Comp_Checks);
2212 -- Generate:
2213 -- for I<Dim> in _object'Range (<Dim>) loop
2214 -- <Comp_Checks>
2215 -- end loop;
2217 -- Note that the invariant procedure may have a null body if
2218 -- assertions are disabled or Assertion_Policy Ignore is in
2219 -- effect.
2221 if Present (Comp_Checks) then
2222 Append_New_To (Dim_Checks,
2223 Make_Implicit_Loop_Statement (T,
2224 Identifier => Empty,
2225 Iteration_Scheme =>
2226 Make_Iteration_Scheme (Loc,
2227 Loop_Parameter_Specification =>
2228 Make_Loop_Parameter_Specification (Loc,
2229 Defining_Identifier => Index,
2230 Discrete_Subtype_Definition =>
2231 Make_Attribute_Reference (Loc,
2232 Prefix =>
2233 New_Occurrence_Of (Obj_Id, Loc),
2234 Attribute_Name => Name_Range,
2235 Expressions => New_List (
2236 Make_Integer_Literal (Loc, Dim))))),
2237 Statements => Comp_Checks));
2238 end if;
2239 end if;
2240 end Process_One_Dimension;
2242 -- Start of processing for Add_Array_Component_Invariants
2244 begin
2245 Process_One_Dimension
2246 (Dim => 1,
2247 Indices => New_List,
2248 Dim_Checks => Checks);
2249 end Add_Array_Component_Invariants;
2251 ------------------------------
2252 -- Add_Inherited_Invariants --
2253 ------------------------------
2255 procedure Add_Inherited_Invariants
2256 (T : Entity_Id;
2257 Priv_Typ : Entity_Id;
2258 Full_Typ : Entity_Id;
2259 Obj_Id : Entity_Id;
2260 Checks : in out List_Id)
2262 Deriv_Typ : Entity_Id;
2263 Expr : Node_Id;
2264 Prag : Node_Id;
2265 Prag_Expr : Node_Id;
2266 Prag_Expr_Arg : Node_Id;
2267 Prag_Typ : Node_Id;
2268 Prag_Typ_Arg : Node_Id;
2270 Par_Proc : Entity_Id;
2271 -- The "partial" invariant procedure of Par_Typ
2273 Par_Typ : Entity_Id;
2274 -- The suitable view of the parent type used in the substitution of
2275 -- type attributes.
2277 begin
2278 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2279 return;
2280 end if;
2282 -- When the type inheriting the class-wide invariant is a concurrent
2283 -- type, use the corresponding record type because it contains all
2284 -- primitive operations of the concurrent type and allows for proper
2285 -- substitution.
2287 if Is_Concurrent_Type (T) then
2288 Deriv_Typ := Corresponding_Record_Type (T);
2289 else
2290 Deriv_Typ := T;
2291 end if;
2293 pragma Assert (Present (Deriv_Typ));
2295 -- Determine which rep item chain to use. Precedence is given to that
2296 -- of the parent type's partial view since it usually carries all the
2297 -- class-wide invariants.
2299 if Present (Priv_Typ) then
2300 Prag := First_Rep_Item (Priv_Typ);
2301 else
2302 Prag := First_Rep_Item (Full_Typ);
2303 end if;
2305 while Present (Prag) loop
2306 if Nkind (Prag) = N_Pragma
2307 and then Pragma_Name (Prag) = Name_Invariant
2308 then
2309 -- Nothing to do if the pragma was already processed
2311 if Contains (Pragmas_Seen, Prag) then
2312 return;
2314 -- Nothing to do when the caller requests the processing of all
2315 -- inherited class-wide invariants, but the pragma does not
2316 -- fall in this category.
2318 elsif not Class_Present (Prag) then
2319 return;
2320 end if;
2322 -- Extract the arguments of the invariant pragma
2324 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2325 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2326 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2327 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2329 -- The pragma applies to the partial view of the parent type
2331 if Present (Priv_Typ)
2332 and then Entity (Prag_Typ) = Priv_Typ
2333 then
2334 Par_Typ := Priv_Typ;
2336 -- The pragma applies to the full view of the parent type
2338 elsif Present (Full_Typ)
2339 and then Entity (Prag_Typ) = Full_Typ
2340 then
2341 Par_Typ := Full_Typ;
2343 -- Otherwise the pragma does not belong to the parent type and
2344 -- should not be considered.
2346 else
2347 return;
2348 end if;
2350 -- Perform the following substitutions:
2352 -- * Replace a reference to the _object parameter of the
2353 -- parent type's partial invariant procedure with a
2354 -- reference to the _object parameter of the derived
2355 -- type's full invariant procedure.
2357 -- * Replace a reference to a discriminant of the parent type
2358 -- with a suitable value from the point of view of the
2359 -- derived type.
2361 -- * Replace a call to an overridden parent primitive with a
2362 -- call to the overriding derived type primitive.
2364 -- * Replace a call to an inherited parent primitive with a
2365 -- call to the internally-generated inherited derived type
2366 -- primitive.
2368 Expr := New_Copy_Tree (Prag_Expr);
2370 -- The parent type must have a "partial" invariant procedure
2371 -- because class-wide invariants are captured exclusively by
2372 -- it.
2374 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2375 pragma Assert (Present (Par_Proc));
2377 Replace_References
2378 (Expr => Expr,
2379 Par_Typ => Par_Typ,
2380 Deriv_Typ => Deriv_Typ,
2381 Par_Obj => First_Formal (Par_Proc),
2382 Deriv_Obj => Obj_Id);
2384 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2385 end if;
2387 Next_Rep_Item (Prag);
2388 end loop;
2389 end Add_Inherited_Invariants;
2391 ------------------------------
2392 -- Add_Interface_Invariants --
2393 ------------------------------
2395 procedure Add_Interface_Invariants
2396 (T : Entity_Id;
2397 Obj_Id : Entity_Id;
2398 Checks : in out List_Id)
2400 Iface_Elmt : Elmt_Id;
2401 Ifaces : Elist_Id;
2403 begin
2404 -- Generate an invariant check for each class-wide invariant coming
2405 -- from all interfaces implemented by type T.
2407 if Is_Tagged_Type (T) then
2408 Collect_Interfaces (T, Ifaces);
2410 -- Process the class-wide invariants of all implemented interfaces
2412 Iface_Elmt := First_Elmt (Ifaces);
2413 while Present (Iface_Elmt) loop
2415 -- The Full_Typ parameter is intentionally left Empty because
2416 -- interfaces are treated as the partial view of a private type
2417 -- in order to achieve uniformity with the general case.
2419 Add_Inherited_Invariants
2420 (T => T,
2421 Priv_Typ => Node (Iface_Elmt),
2422 Full_Typ => Empty,
2423 Obj_Id => Obj_Id,
2424 Checks => Checks);
2426 Next_Elmt (Iface_Elmt);
2427 end loop;
2428 end if;
2429 end Add_Interface_Invariants;
2431 -------------------------
2432 -- Add_Invariant_Check --
2433 -------------------------
2435 procedure Add_Invariant_Check
2436 (Prag : Node_Id;
2437 Expr : Node_Id;
2438 Checks : in out List_Id;
2439 Inherited : Boolean := False)
2441 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2442 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2443 Ploc : constant Source_Ptr := Sloc (Prag);
2444 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2446 Assoc : List_Id;
2447 Str : String_Id;
2449 begin
2450 -- The invariant is ignored, nothing left to do
2452 if Is_Ignored (Prag) then
2453 null;
2455 -- Otherwise the invariant is checked. Build a pragma Check to verify
2456 -- the expression at run time.
2458 else
2459 Assoc := New_List (
2460 Make_Pragma_Argument_Association (Ploc,
2461 Expression => Make_Identifier (Ploc, Nam)),
2462 Make_Pragma_Argument_Association (Ploc,
2463 Expression => Expr));
2465 -- Handle the String argument (if any)
2467 if Present (Str_Arg) then
2468 Str := Strval (Get_Pragma_Arg (Str_Arg));
2470 -- When inheriting an invariant, modify the message from
2471 -- "failed invariant" to "failed inherited invariant".
2473 if Inherited then
2474 String_To_Name_Buffer (Str);
2476 if Name_Buffer (1 .. 16) = "failed invariant" then
2477 Insert_Str_In_Name_Buffer ("inherited ", 8);
2478 Str := String_From_Name_Buffer;
2479 end if;
2480 end if;
2482 Append_To (Assoc,
2483 Make_Pragma_Argument_Association (Ploc,
2484 Expression => Make_String_Literal (Ploc, Str)));
2485 end if;
2487 -- Generate:
2488 -- pragma Check (<Nam>, <Expr>, <Str>);
2490 Append_New_To (Checks,
2491 Make_Pragma (Ploc,
2492 Chars => Name_Check,
2493 Pragma_Argument_Associations => Assoc));
2494 end if;
2496 -- Output an info message when inheriting an invariant and the
2497 -- listing option is enabled.
2499 if Inherited and Opt.List_Inherited_Aspects then
2500 Error_Msg_Sloc := Sloc (Prag);
2501 Error_Msg_N
2502 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2503 end if;
2505 -- Add the pragma to the list of processed pragmas
2507 Append_New_Elmt (Prag, Pragmas_Seen);
2508 Produced_Check := True;
2509 end Add_Invariant_Check;
2511 ---------------------------
2512 -- Add_Parent_Invariants --
2513 ---------------------------
2515 procedure Add_Parent_Invariants
2516 (T : Entity_Id;
2517 Obj_Id : Entity_Id;
2518 Checks : in out List_Id)
2520 Dummy_1 : Entity_Id;
2521 Dummy_2 : Entity_Id;
2523 Curr_Typ : Entity_Id;
2524 -- The entity of the current type being examined
2526 Full_Typ : Entity_Id;
2527 -- The full view of Par_Typ
2529 Par_Typ : Entity_Id;
2530 -- The entity of the parent type
2532 Priv_Typ : Entity_Id;
2533 -- The partial view of Par_Typ
2535 begin
2536 -- Do not process array types because they cannot have true parent
2537 -- types. This also prevents the generation of a duplicate invariant
2538 -- check when the input type is an array base type because its Etype
2539 -- denotes the first subtype, both of which share the same component
2540 -- type.
2542 if Is_Array_Type (T) then
2543 return;
2544 end if;
2546 -- Climb the parent type chain
2548 Curr_Typ := T;
2549 loop
2550 -- Do not consider subtypes as they inherit the invariants
2551 -- from their base types.
2553 Par_Typ := Base_Type (Etype (Curr_Typ));
2555 -- Stop the climb once the root of the parent chain is
2556 -- reached.
2558 exit when Curr_Typ = Par_Typ;
2560 -- Process the class-wide invariants of the parent type
2562 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2564 -- Process the elements of an array type
2566 if Is_Array_Type (Full_Typ) then
2567 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2569 -- Process the components of a record type
2571 elsif Ekind (Full_Typ) = E_Record_Type then
2572 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2573 end if;
2575 Add_Inherited_Invariants
2576 (T => T,
2577 Priv_Typ => Priv_Typ,
2578 Full_Typ => Full_Typ,
2579 Obj_Id => Obj_Id,
2580 Checks => Checks);
2582 Curr_Typ := Par_Typ;
2583 end loop;
2584 end Add_Parent_Invariants;
2586 ------------------------
2587 -- Add_Own_Invariants --
2588 ------------------------
2590 procedure Add_Own_Invariants
2591 (T : Entity_Id;
2592 Obj_Id : Entity_Id;
2593 Checks : in out List_Id;
2594 Priv_Item : Node_Id := Empty)
2596 ASIS_Expr : Node_Id;
2597 Expr : Node_Id;
2598 Prag : Node_Id;
2599 Prag_Asp : Node_Id;
2600 Prag_Expr : Node_Id;
2601 Prag_Expr_Arg : Node_Id;
2602 Prag_Typ : Node_Id;
2603 Prag_Typ_Arg : Node_Id;
2605 begin
2606 if not Present (T) then
2607 return;
2608 end if;
2610 Prag := First_Rep_Item (T);
2611 while Present (Prag) loop
2612 if Nkind (Prag) = N_Pragma
2613 and then Pragma_Name (Prag) = Name_Invariant
2614 then
2615 -- Stop the traversal of the rep item chain once a specific
2616 -- item is encountered.
2618 if Present (Priv_Item) and then Prag = Priv_Item then
2619 exit;
2620 end if;
2622 -- Nothing to do if the pragma was already processed
2624 if Contains (Pragmas_Seen, Prag) then
2625 return;
2626 end if;
2628 -- Extract the arguments of the invariant pragma
2630 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2631 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2632 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2633 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2634 Prag_Asp := Corresponding_Aspect (Prag);
2636 -- Verify the pragma belongs to T, otherwise the pragma applies
2637 -- to a parent type in which case it will be processed later by
2638 -- Add_Parent_Invariants or Add_Interface_Invariants.
2640 if Entity (Prag_Typ) /= T then
2641 return;
2642 end if;
2644 Expr := New_Copy_Tree (Prag_Expr);
2646 -- Substitute all references to type T with references to the
2647 -- _object formal parameter.
2649 Replace_Type_References (Expr, T, Obj_Id);
2651 -- Preanalyze the invariant expression to detect errors and at
2652 -- the same time capture the visibility of the proper package
2653 -- part.
2655 Set_Parent (Expr, Parent (Prag_Expr));
2656 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2658 -- Save a copy of the expression when T is tagged to detect
2659 -- errors and capture the visibility of the proper package part
2660 -- for the generation of inherited type invariants.
2662 if Is_Tagged_Type (T) then
2663 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2664 end if;
2666 -- If the pragma comes from an aspect specification, replace
2667 -- the saved expression because all type references must be
2668 -- substituted for the call to Preanalyze_Spec_Expression in
2669 -- Check_Aspect_At_xxx routines.
2671 if Present (Prag_Asp) then
2672 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2673 end if;
2675 -- Analyze the original invariant expression for ASIS
2677 if ASIS_Mode then
2678 ASIS_Expr := Empty;
2680 if Comes_From_Source (Prag) then
2681 ASIS_Expr := Prag_Expr;
2682 elsif Present (Prag_Asp) then
2683 ASIS_Expr := Expression (Prag_Asp);
2684 end if;
2686 if Present (ASIS_Expr) then
2687 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2688 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2689 end if;
2690 end if;
2692 Add_Invariant_Check (Prag, Expr, Checks);
2693 end if;
2695 Next_Rep_Item (Prag);
2696 end loop;
2697 end Add_Own_Invariants;
2699 -------------------------------------
2700 -- Add_Record_Component_Invariants --
2701 -------------------------------------
2703 procedure Add_Record_Component_Invariants
2704 (T : Entity_Id;
2705 Obj_Id : Entity_Id;
2706 Checks : in out List_Id)
2708 procedure Process_Component_List
2709 (Comp_List : Node_Id;
2710 CL_Checks : in out List_Id);
2711 -- Generate invariant checks for all record components found in
2712 -- component list Comp_List, including variant parts. All created
2713 -- checks are added to list CL_Checks.
2715 procedure Process_Record_Component
2716 (Comp_Id : Entity_Id;
2717 Comp_Checks : in out List_Id);
2718 -- Generate an invariant check for a record component identified by
2719 -- Comp_Id. All created checks are added to list Comp_Checks.
2721 ----------------------------
2722 -- Process_Component_List --
2723 ----------------------------
2725 procedure Process_Component_List
2726 (Comp_List : Node_Id;
2727 CL_Checks : in out List_Id)
2729 Comp : Node_Id;
2730 Var : Node_Id;
2731 Var_Alts : List_Id := No_List;
2732 Var_Checks : List_Id := No_List;
2733 Var_Stmts : List_Id;
2735 Produced_Variant_Check : Boolean := False;
2736 -- This flag tracks whether the component has produced at least
2737 -- one invariant check.
2739 begin
2740 -- Traverse the component items
2742 Comp := First (Component_Items (Comp_List));
2743 while Present (Comp) loop
2744 if Nkind (Comp) = N_Component_Declaration then
2746 -- Generate the component invariant check
2748 Process_Record_Component
2749 (Comp_Id => Defining_Entity (Comp),
2750 Comp_Checks => CL_Checks);
2751 end if;
2753 Next (Comp);
2754 end loop;
2756 -- Traverse the variant part
2758 if Present (Variant_Part (Comp_List)) then
2759 Var := First (Variants (Variant_Part (Comp_List)));
2760 while Present (Var) loop
2761 Var_Checks := No_List;
2763 -- Generate invariant checks for all components and variant
2764 -- parts that qualify.
2766 Process_Component_List
2767 (Comp_List => Component_List (Var),
2768 CL_Checks => Var_Checks);
2770 -- The components of the current variant produced at least
2771 -- one invariant check.
2773 if Present (Var_Checks) then
2774 Var_Stmts := Var_Checks;
2775 Produced_Variant_Check := True;
2777 -- Otherwise there are either no components with invariants,
2778 -- assertions are disabled, or Assertion_Policy Ignore is in
2779 -- effect.
2781 else
2782 Var_Stmts := New_List (Make_Null_Statement (Loc));
2783 end if;
2785 Append_New_To (Var_Alts,
2786 Make_Case_Statement_Alternative (Loc,
2787 Discrete_Choices =>
2788 New_Copy_List (Discrete_Choices (Var)),
2789 Statements => Var_Stmts));
2791 Next (Var);
2792 end loop;
2794 -- Create a case statement which verifies the invariant checks
2795 -- of a particular component list depending on the discriminant
2796 -- values only when there is at least one real invariant check.
2798 if Produced_Variant_Check then
2799 Append_New_To (CL_Checks,
2800 Make_Case_Statement (Loc,
2801 Expression =>
2802 Make_Selected_Component (Loc,
2803 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2804 Selector_Name =>
2805 New_Occurrence_Of
2806 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2807 Alternatives => Var_Alts));
2808 end if;
2809 end if;
2810 end Process_Component_List;
2812 ------------------------------
2813 -- Process_Record_Component --
2814 ------------------------------
2816 procedure Process_Record_Component
2817 (Comp_Id : Entity_Id;
2818 Comp_Checks : in out List_Id)
2820 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2821 Proc_Id : Entity_Id;
2823 Produced_Component_Check : Boolean := False;
2824 -- This flag tracks whether the component has produced at least
2825 -- one invariant check.
2827 begin
2828 -- Nothing to do for internal component _parent. Note that it is
2829 -- not desirable to check whether the component comes from source
2830 -- because protected type components are relocated to an internal
2831 -- corresponding record, but still need processing.
2833 if Chars (Comp_Id) = Name_uParent then
2834 return;
2835 end if;
2837 -- Verify the invariant of the component. Note that an access
2838 -- type may have an invariant when it acts as the full view of a
2839 -- private type and the invariant appears on the partial view. In
2840 -- this case verify the access value itself.
2842 if Has_Invariants (Comp_Typ) then
2844 -- In GNATprove mode, the component invariants are checked by
2845 -- other means. They should not be added to the record type
2846 -- invariant procedure, so that the procedure can be used to
2847 -- check the record type invariants if any.
2849 if GNATprove_Mode then
2850 null;
2852 else
2853 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2855 -- The component type should have an invariant procedure
2856 -- if it has invariants of its own or inherits class-wide
2857 -- invariants from parent or interface types.
2859 pragma Assert (Present (Proc_Id));
2861 -- Generate:
2862 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2864 -- Note that the invariant procedure may have a null body if
2865 -- assertions are disabled or Assertion_Policy Ignore is in
2866 -- effect.
2868 if not Has_Null_Body (Proc_Id) then
2869 Append_New_To (Comp_Checks,
2870 Make_Procedure_Call_Statement (Loc,
2871 Name =>
2872 New_Occurrence_Of (Proc_Id, Loc),
2873 Parameter_Associations => New_List (
2874 Make_Selected_Component (Loc,
2875 Prefix =>
2876 Unchecked_Convert_To
2877 (T, New_Occurrence_Of (Obj_Id, Loc)),
2878 Selector_Name =>
2879 New_Occurrence_Of (Comp_Id, Loc)))));
2880 end if;
2881 end if;
2883 Produced_Check := True;
2884 Produced_Component_Check := True;
2885 end if;
2887 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2888 Error_Msg_NE
2889 ("invariants cannot be checked on components of "
2890 & "unchecked_union type &?", Comp_Id, T);
2891 end if;
2892 end Process_Record_Component;
2894 -- Local variables
2896 Comps : Node_Id;
2897 Def : Node_Id;
2899 -- Start of processing for Add_Record_Component_Invariants
2901 begin
2902 -- An untagged derived type inherits the components of its parent
2903 -- type. In order to avoid creating redundant invariant checks, do
2904 -- not process the components now. Instead wait until the ultimate
2905 -- parent of the untagged derivation chain is reached.
2907 if not Is_Untagged_Derivation (T) then
2908 Def := Type_Definition (Parent (T));
2910 if Nkind (Def) = N_Derived_Type_Definition then
2911 Def := Record_Extension_Part (Def);
2912 end if;
2914 pragma Assert (Nkind (Def) = N_Record_Definition);
2915 Comps := Component_List (Def);
2917 if Present (Comps) then
2918 Process_Component_List
2919 (Comp_List => Comps,
2920 CL_Checks => Checks);
2921 end if;
2922 end if;
2923 end Add_Record_Component_Invariants;
2925 -- Local variables
2927 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2928 -- Save the Ghost mode to restore on exit
2930 Dummy : Entity_Id;
2931 Priv_Item : Node_Id;
2932 Proc_Body : Node_Id;
2933 Proc_Body_Id : Entity_Id;
2934 Proc_Decl : Node_Id;
2935 Proc_Id : Entity_Id;
2936 Stmts : List_Id := No_List;
2938 CRec_Typ : Entity_Id := Empty;
2939 -- The corresponding record type of Full_Typ
2941 Full_Proc : Entity_Id := Empty;
2942 -- The entity of the "full" invariant procedure
2944 Full_Typ : Entity_Id := Empty;
2945 -- The full view of the working type
2947 Obj_Id : Entity_Id := Empty;
2948 -- The _object formal parameter of the invariant procedure
2950 Part_Proc : Entity_Id := Empty;
2951 -- The entity of the "partial" invariant procedure
2953 Priv_Typ : Entity_Id := Empty;
2954 -- The partial view of the working type
2956 Work_Typ : Entity_Id := Empty;
2957 -- The working type
2959 -- Start of processing for Build_Invariant_Procedure_Body
2961 begin
2962 Work_Typ := Typ;
2964 -- The input type denotes the implementation base type of a constrained
2965 -- array type. Work with the first subtype as all invariant pragmas are
2966 -- on its rep item chain.
2968 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2969 Work_Typ := First_Subtype (Work_Typ);
2971 -- The input type denotes the corresponding record type of a protected
2972 -- or task type. Work with the concurrent type because the corresponding
2973 -- record type may not be visible to clients of the type.
2975 elsif Ekind (Work_Typ) = E_Record_Type
2976 and then Is_Concurrent_Record_Type (Work_Typ)
2977 then
2978 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2979 end if;
2981 -- The working type may be subject to pragma Ghost. Set the mode now to
2982 -- ensure that the invariant procedure is properly marked as Ghost.
2984 Set_Ghost_Mode (Work_Typ);
2986 -- The type must either have invariants of its own, inherit class-wide
2987 -- invariants from parent types or interfaces, or be an array or record
2988 -- type whose components have invariants.
2990 pragma Assert (Has_Invariants (Work_Typ));
2992 -- Interfaces are treated as the partial view of a private type in order
2993 -- to achieve uniformity with the general case.
2995 if Is_Interface (Work_Typ) then
2996 Priv_Typ := Work_Typ;
2998 -- Otherwise obtain both views of the type
3000 else
3001 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3002 end if;
3004 -- The caller requests a body for the partial invariant procedure
3006 if Partial_Invariant then
3007 Full_Proc := Invariant_Procedure (Work_Typ);
3008 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3010 -- The "full" invariant procedure body was already created
3012 if Present (Full_Proc)
3013 and then Present
3014 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3015 then
3016 -- This scenario happens only when the type is an untagged
3017 -- derivation from a private parent and the underlying full
3018 -- view was processed before the partial view.
3020 pragma Assert
3021 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3023 -- Nothing to do because the processing of the underlying full
3024 -- view already checked the invariants of the partial view.
3026 goto Leave;
3027 end if;
3029 -- Create a declaration for the "partial" invariant procedure if it
3030 -- is not available.
3032 if No (Proc_Id) then
3033 Build_Invariant_Procedure_Declaration
3034 (Typ => Work_Typ,
3035 Partial_Invariant => True);
3037 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3038 end if;
3040 -- The caller requests a body for the "full" invariant procedure
3042 else
3043 Proc_Id := Invariant_Procedure (Work_Typ);
3044 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3046 -- Create a declaration for the "full" invariant procedure if it is
3047 -- not available.
3049 if No (Proc_Id) then
3050 Build_Invariant_Procedure_Declaration (Work_Typ);
3051 Proc_Id := Invariant_Procedure (Work_Typ);
3052 end if;
3053 end if;
3055 -- At this point there should be an invariant procedure declaration
3057 pragma Assert (Present (Proc_Id));
3058 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3060 -- Nothing to do if the invariant procedure already has a body
3062 if Present (Corresponding_Body (Proc_Decl)) then
3063 goto Leave;
3064 end if;
3066 -- Emulate the environment of the invariant procedure by installing its
3067 -- scope and formal parameters. Note that this is not needed, but having
3068 -- the scope installed helps with the detection of invariant-related
3069 -- errors.
3071 Push_Scope (Proc_Id);
3072 Install_Formals (Proc_Id);
3074 Obj_Id := First_Formal (Proc_Id);
3075 pragma Assert (Present (Obj_Id));
3077 -- The "partial" invariant procedure verifies the invariants of the
3078 -- partial view only.
3080 if Partial_Invariant then
3081 pragma Assert (Present (Priv_Typ));
3083 Add_Own_Invariants
3084 (T => Priv_Typ,
3085 Obj_Id => Obj_Id,
3086 Checks => Stmts);
3088 -- Otherwise the "full" invariant procedure verifies the invariants of
3089 -- the full view, all array or record components, as well as class-wide
3090 -- invariants inherited from parent types or interfaces. In addition, it
3091 -- indirectly verifies the invariants of the partial view by calling the
3092 -- "partial" invariant procedure.
3094 else
3095 pragma Assert (Present (Full_Typ));
3097 -- Check the invariants of the partial view by calling the "partial"
3098 -- invariant procedure. Generate:
3100 -- <Work_Typ>Partial_Invariant (_object);
3102 if Present (Part_Proc) then
3103 Append_New_To (Stmts,
3104 Make_Procedure_Call_Statement (Loc,
3105 Name => New_Occurrence_Of (Part_Proc, Loc),
3106 Parameter_Associations => New_List (
3107 New_Occurrence_Of (Obj_Id, Loc))));
3109 Produced_Check := True;
3110 end if;
3112 Priv_Item := Empty;
3114 -- Derived subtypes do not have a partial view
3116 if Present (Priv_Typ) then
3118 -- The processing of the "full" invariant procedure intentionally
3119 -- skips the partial view because a) this may result in changes of
3120 -- visibility and b) lead to duplicate checks. However, when the
3121 -- full view is the underlying full view of an untagged derived
3122 -- type whose parent type is private, partial invariants appear on
3123 -- the rep item chain of the partial view only.
3125 -- package Pack_1 is
3126 -- type Root ... is private;
3127 -- private
3128 -- <full view of Root>
3129 -- end Pack_1;
3131 -- with Pack_1;
3132 -- package Pack_2 is
3133 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3134 -- <underlying full view of Child>
3135 -- end Pack_2;
3137 -- As a result, the processing of the full view must also consider
3138 -- all invariants of the partial view.
3140 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3141 null;
3143 -- Otherwise the invariants of the partial view are ignored
3145 else
3146 -- Note that the rep item chain is shared between the partial
3147 -- and full views of a type. To avoid processing the invariants
3148 -- of the partial view, signal the logic to stop when the first
3149 -- rep item of the partial view has been reached.
3151 Priv_Item := First_Rep_Item (Priv_Typ);
3153 -- Ignore the invariants of the partial view by eliminating the
3154 -- view.
3156 Priv_Typ := Empty;
3157 end if;
3158 end if;
3160 -- Process the invariants of the full view and in certain cases those
3161 -- of the partial view. This also handles any invariants on array or
3162 -- record components.
3164 Add_Own_Invariants
3165 (T => Priv_Typ,
3166 Obj_Id => Obj_Id,
3167 Checks => Stmts,
3168 Priv_Item => Priv_Item);
3170 Add_Own_Invariants
3171 (T => Full_Typ,
3172 Obj_Id => Obj_Id,
3173 Checks => Stmts,
3174 Priv_Item => Priv_Item);
3176 -- Process the elements of an array type
3178 if Is_Array_Type (Full_Typ) then
3179 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3181 -- Process the components of a record type
3183 elsif Ekind (Full_Typ) = E_Record_Type then
3184 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3186 -- Process the components of a corresponding record
3188 elsif Present (CRec_Typ) then
3189 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3190 end if;
3192 -- Process the inherited class-wide invariants of all parent types.
3193 -- This also handles any invariants on record components.
3195 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3197 -- Process the inherited class-wide invariants of all implemented
3198 -- interface types.
3200 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3201 end if;
3203 End_Scope;
3205 -- At this point there should be at least one invariant check. If this
3206 -- is not the case, then the invariant-related flags were not properly
3207 -- set, or there is a missing invariant procedure on one of the array
3208 -- or record components.
3210 pragma Assert (Produced_Check);
3212 -- Account for the case where assertions are disabled or all invariant
3213 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3214 -- empty body.
3216 if No (Stmts) then
3217 Stmts := New_List (Make_Null_Statement (Loc));
3218 end if;
3220 -- Generate:
3221 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3222 -- begin
3223 -- <Stmts>
3224 -- end <Work_Typ>[Partial_]Invariant;
3226 Proc_Body :=
3227 Make_Subprogram_Body (Loc,
3228 Specification =>
3229 Copy_Subprogram_Spec (Parent (Proc_Id)),
3230 Declarations => Empty_List,
3231 Handled_Statement_Sequence =>
3232 Make_Handled_Sequence_Of_Statements (Loc,
3233 Statements => Stmts));
3234 Proc_Body_Id := Defining_Entity (Proc_Body);
3236 -- Perform minor decoration in case the body is not analyzed
3238 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3239 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3240 Set_Scope (Proc_Body_Id, Current_Scope);
3242 -- Link both spec and body to avoid generating duplicates
3244 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3245 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3247 -- The body should not be inserted into the tree when the context is
3248 -- ASIS or a generic unit because it is not part of the template. Note
3249 -- that the body must still be generated in order to resolve the
3250 -- invariants.
3252 if ASIS_Mode or Inside_A_Generic then
3253 null;
3255 -- Semi-insert the body into the tree for GNATprove by setting its
3256 -- Parent field. This allows for proper upstream tree traversals.
3258 elsif GNATprove_Mode then
3259 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3261 -- Otherwise the body is part of the freezing actions of the type
3263 else
3264 Append_Freeze_Action (Work_Typ, Proc_Body);
3265 end if;
3267 <<Leave>>
3268 Restore_Ghost_Mode (Saved_GM);
3269 end Build_Invariant_Procedure_Body;
3271 -------------------------------------------
3272 -- Build_Invariant_Procedure_Declaration --
3273 -------------------------------------------
3275 -- WARNING: This routine manages Ghost regions. Return statements must be
3276 -- replaced by gotos which jump to the end of the routine and restore the
3277 -- Ghost mode.
3279 procedure Build_Invariant_Procedure_Declaration
3280 (Typ : Entity_Id;
3281 Partial_Invariant : Boolean := False)
3283 Loc : constant Source_Ptr := Sloc (Typ);
3285 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3286 -- Save the Ghost mode to restore on exit
3288 Proc_Decl : Node_Id;
3289 Proc_Id : Entity_Id;
3290 Proc_Nam : Name_Id;
3291 Typ_Decl : Node_Id;
3293 CRec_Typ : Entity_Id;
3294 -- The corresponding record type of Full_Typ
3296 Full_Base : Entity_Id;
3297 -- The base type of Full_Typ
3299 Full_Typ : Entity_Id;
3300 -- The full view of working type
3302 Obj_Id : Entity_Id;
3303 -- The _object formal parameter of the invariant procedure
3305 Obj_Typ : Entity_Id;
3306 -- The type of the _object formal parameter
3308 Priv_Typ : Entity_Id;
3309 -- The partial view of working type
3311 Work_Typ : Entity_Id;
3312 -- The working type
3314 begin
3315 Work_Typ := Typ;
3317 -- The input type denotes the implementation base type of a constrained
3318 -- array type. Work with the first subtype as all invariant pragmas are
3319 -- on its rep item chain.
3321 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3322 Work_Typ := First_Subtype (Work_Typ);
3324 -- The input denotes the corresponding record type of a protected or a
3325 -- task type. Work with the concurrent type because the corresponding
3326 -- record type may not be visible to clients of the type.
3328 elsif Ekind (Work_Typ) = E_Record_Type
3329 and then Is_Concurrent_Record_Type (Work_Typ)
3330 then
3331 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3332 end if;
3334 -- The working type may be subject to pragma Ghost. Set the mode now to
3335 -- ensure that the invariant procedure is properly marked as Ghost.
3337 Set_Ghost_Mode (Work_Typ);
3339 -- The type must either have invariants of its own, inherit class-wide
3340 -- invariants from parent or interface types, or be an array or record
3341 -- type whose components have invariants.
3343 pragma Assert (Has_Invariants (Work_Typ));
3345 -- Nothing to do if the type already has a "partial" invariant procedure
3347 if Partial_Invariant then
3348 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3349 goto Leave;
3350 end if;
3352 -- Nothing to do if the type already has a "full" invariant procedure
3354 elsif Present (Invariant_Procedure (Work_Typ)) then
3355 goto Leave;
3356 end if;
3358 -- The caller requests the declaration of the "partial" invariant
3359 -- procedure.
3361 if Partial_Invariant then
3362 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3364 -- Otherwise the caller requests the declaration of the "full" invariant
3365 -- procedure.
3367 else
3368 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3369 end if;
3371 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3373 -- Perform minor decoration in case the declaration is not analyzed
3375 Set_Ekind (Proc_Id, E_Procedure);
3376 Set_Etype (Proc_Id, Standard_Void_Type);
3377 Set_Scope (Proc_Id, Current_Scope);
3379 if Partial_Invariant then
3380 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3381 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3382 else
3383 Set_Is_Invariant_Procedure (Proc_Id);
3384 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3385 end if;
3387 -- The invariant procedure requires debug info when the invariants are
3388 -- subject to Source Coverage Obligations.
3390 if Opt.Generate_SCO then
3391 Set_Needs_Debug_Info (Proc_Id);
3392 end if;
3394 -- Obtain all views of the input type
3396 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3398 -- Associate the invariant procedure with all views
3400 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3401 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3402 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3403 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3405 -- The declaration of the invariant procedure is inserted after the
3406 -- declaration of the partial view as this allows for proper external
3407 -- visibility.
3409 if Present (Priv_Typ) then
3410 Typ_Decl := Declaration_Node (Priv_Typ);
3412 -- Anonymous arrays in object declarations have no explicit declaration
3413 -- so use the related object declaration as the insertion point.
3415 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3416 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3418 -- Derived types with the full view as parent do not have a partial
3419 -- view. Insert the invariant procedure after the derived type.
3421 else
3422 Typ_Decl := Declaration_Node (Full_Typ);
3423 end if;
3425 -- The type should have a declarative node
3427 pragma Assert (Present (Typ_Decl));
3429 -- Create the formal parameter which emulates the variable-like behavior
3430 -- of the current type instance.
3432 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3434 -- When generating an invariant procedure declaration for an abstract
3435 -- type (including interfaces), use the class-wide type as the _object
3436 -- type. This has several desirable effects:
3438 -- * The invariant procedure does not become a primitive of the type.
3439 -- This eliminates the need to either special case the treatment of
3440 -- invariant procedures, or to make it a predefined primitive and
3441 -- force every derived type to potentially provide an empty body.
3443 -- * The invariant procedure does not need to be declared as abstract.
3444 -- This allows for a proper body, which in turn avoids redundant
3445 -- processing of the same invariants for types with multiple views.
3447 -- * The class-wide type allows for calls to abstract primitives
3448 -- within a nonabstract subprogram. The calls are treated as
3449 -- dispatching and require additional processing when they are
3450 -- remapped to call primitives of derived types. See routine
3451 -- Replace_References for details.
3453 if Is_Abstract_Type (Work_Typ) then
3454 Obj_Typ := Class_Wide_Type (Work_Typ);
3455 else
3456 Obj_Typ := Work_Typ;
3457 end if;
3459 -- Perform minor decoration in case the declaration is not analyzed
3461 Set_Ekind (Obj_Id, E_In_Parameter);
3462 Set_Etype (Obj_Id, Obj_Typ);
3463 Set_Scope (Obj_Id, Proc_Id);
3465 Set_First_Entity (Proc_Id, Obj_Id);
3467 -- Generate:
3468 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3470 Proc_Decl :=
3471 Make_Subprogram_Declaration (Loc,
3472 Specification =>
3473 Make_Procedure_Specification (Loc,
3474 Defining_Unit_Name => Proc_Id,
3475 Parameter_Specifications => New_List (
3476 Make_Parameter_Specification (Loc,
3477 Defining_Identifier => Obj_Id,
3478 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3480 -- The declaration should not be inserted into the tree when the context
3481 -- is ASIS or a generic unit because it is not part of the template.
3483 if ASIS_Mode or Inside_A_Generic then
3484 null;
3486 -- Semi-insert the declaration into the tree for GNATprove by setting
3487 -- its Parent field. This allows for proper upstream tree traversals.
3489 elsif GNATprove_Mode then
3490 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3492 -- Otherwise insert the declaration
3494 else
3495 pragma Assert (Present (Typ_Decl));
3496 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3497 end if;
3499 <<Leave>>
3500 Restore_Ghost_Mode (Saved_GM);
3501 end Build_Invariant_Procedure_Declaration;
3503 --------------------------
3504 -- Build_Procedure_Form --
3505 --------------------------
3507 procedure Build_Procedure_Form (N : Node_Id) is
3508 Loc : constant Source_Ptr := Sloc (N);
3509 Subp : constant Entity_Id := Defining_Entity (N);
3511 Func_Formal : Entity_Id;
3512 Proc_Formals : List_Id;
3513 Proc_Decl : Node_Id;
3515 begin
3516 -- No action needed if this transformation was already done, or in case
3517 -- of subprogram renaming declarations.
3519 if Nkind (Specification (N)) = N_Procedure_Specification
3520 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3521 then
3522 return;
3523 end if;
3525 -- Ditto when dealing with an expression function, where both the
3526 -- original expression and the generated declaration end up being
3527 -- expanded here.
3529 if Rewritten_For_C (Subp) then
3530 return;
3531 end if;
3533 Proc_Formals := New_List;
3535 -- Create a list of formal parameters with the same types as the
3536 -- function.
3538 Func_Formal := First_Formal (Subp);
3539 while Present (Func_Formal) loop
3540 Append_To (Proc_Formals,
3541 Make_Parameter_Specification (Loc,
3542 Defining_Identifier =>
3543 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3544 Parameter_Type =>
3545 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3547 Next_Formal (Func_Formal);
3548 end loop;
3550 -- Add an extra out parameter to carry the function result
3552 Name_Len := 6;
3553 Name_Buffer (1 .. Name_Len) := "RESULT";
3554 Append_To (Proc_Formals,
3555 Make_Parameter_Specification (Loc,
3556 Defining_Identifier =>
3557 Make_Defining_Identifier (Loc, Chars => Name_Find),
3558 Out_Present => True,
3559 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3561 -- The new procedure declaration is inserted immediately after the
3562 -- function declaration. The processing in Build_Procedure_Body_Form
3563 -- relies on this order.
3565 Proc_Decl :=
3566 Make_Subprogram_Declaration (Loc,
3567 Specification =>
3568 Make_Procedure_Specification (Loc,
3569 Defining_Unit_Name =>
3570 Make_Defining_Identifier (Loc, Chars (Subp)),
3571 Parameter_Specifications => Proc_Formals));
3573 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3575 -- Entity of procedure must remain invisible so that it does not
3576 -- overload subsequent references to the original function.
3578 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3580 -- Mark the function as having a procedure form and link the function
3581 -- and its internally built procedure.
3583 Set_Rewritten_For_C (Subp);
3584 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3585 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3586 end Build_Procedure_Form;
3588 ------------------------
3589 -- Build_Runtime_Call --
3590 ------------------------
3592 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3593 begin
3594 -- If entity is not available, we can skip making the call (this avoids
3595 -- junk duplicated error messages in a number of cases).
3597 if not RTE_Available (RE) then
3598 return Make_Null_Statement (Loc);
3599 else
3600 return
3601 Make_Procedure_Call_Statement (Loc,
3602 Name => New_Occurrence_Of (RTE (RE), Loc));
3603 end if;
3604 end Build_Runtime_Call;
3606 ------------------------
3607 -- Build_SS_Mark_Call --
3608 ------------------------
3610 function Build_SS_Mark_Call
3611 (Loc : Source_Ptr;
3612 Mark : Entity_Id) return Node_Id
3614 begin
3615 -- Generate:
3616 -- Mark : constant Mark_Id := SS_Mark;
3618 return
3619 Make_Object_Declaration (Loc,
3620 Defining_Identifier => Mark,
3621 Constant_Present => True,
3622 Object_Definition =>
3623 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3624 Expression =>
3625 Make_Function_Call (Loc,
3626 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3627 end Build_SS_Mark_Call;
3629 ---------------------------
3630 -- Build_SS_Release_Call --
3631 ---------------------------
3633 function Build_SS_Release_Call
3634 (Loc : Source_Ptr;
3635 Mark : Entity_Id) return Node_Id
3637 begin
3638 -- Generate:
3639 -- SS_Release (Mark);
3641 return
3642 Make_Procedure_Call_Statement (Loc,
3643 Name =>
3644 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3645 Parameter_Associations => New_List (
3646 New_Occurrence_Of (Mark, Loc)));
3647 end Build_SS_Release_Call;
3649 ----------------------------
3650 -- Build_Task_Array_Image --
3651 ----------------------------
3653 -- This function generates the body for a function that constructs the
3654 -- image string for a task that is an array component. The function is
3655 -- local to the init proc for the array type, and is called for each one
3656 -- of the components. The constructed image has the form of an indexed
3657 -- component, whose prefix is the outer variable of the array type.
3658 -- The n-dimensional array type has known indexes Index, Index2...
3660 -- Id_Ref is an indexed component form created by the enclosing init proc.
3661 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3662 -- in the loops that call the individual task init proc on each component.
3664 -- The generated function has the following structure:
3666 -- function F return String is
3667 -- Pref : string renames Task_Name;
3668 -- T1 : String := Index1'Image (Val1);
3669 -- ...
3670 -- Tn : String := indexn'image (Valn);
3671 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3672 -- -- Len includes commas and the end parentheses.
3673 -- Res : String (1..Len);
3674 -- Pos : Integer := Pref'Length;
3676 -- begin
3677 -- Res (1 .. Pos) := Pref;
3678 -- Pos := Pos + 1;
3679 -- Res (Pos) := '(';
3680 -- Pos := Pos + 1;
3681 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3682 -- Pos := Pos + T1'Length;
3683 -- Res (Pos) := '.';
3684 -- Pos := Pos + 1;
3685 -- ...
3686 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3687 -- Res (Len) := ')';
3689 -- return Res;
3690 -- end F;
3692 -- Needless to say, multidimensional arrays of tasks are rare enough that
3693 -- the bulkiness of this code is not really a concern.
3695 function Build_Task_Array_Image
3696 (Loc : Source_Ptr;
3697 Id_Ref : Node_Id;
3698 A_Type : Entity_Id;
3699 Dyn : Boolean := False) return Node_Id
3701 Dims : constant Nat := Number_Dimensions (A_Type);
3702 -- Number of dimensions for array of tasks
3704 Temps : array (1 .. Dims) of Entity_Id;
3705 -- Array of temporaries to hold string for each index
3707 Indx : Node_Id;
3708 -- Index expression
3710 Len : Entity_Id;
3711 -- Total length of generated name
3713 Pos : Entity_Id;
3714 -- Running index for substring assignments
3716 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3717 -- Name of enclosing variable, prefix of resulting name
3719 Res : Entity_Id;
3720 -- String to hold result
3722 Val : Node_Id;
3723 -- Value of successive indexes
3725 Sum : Node_Id;
3726 -- Expression to compute total size of string
3728 T : Entity_Id;
3729 -- Entity for name at one index position
3731 Decls : constant List_Id := New_List;
3732 Stats : constant List_Id := New_List;
3734 begin
3735 -- For a dynamic task, the name comes from the target variable. For a
3736 -- static one it is a formal of the enclosing init proc.
3738 if Dyn then
3739 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3740 Append_To (Decls,
3741 Make_Object_Declaration (Loc,
3742 Defining_Identifier => Pref,
3743 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3744 Expression =>
3745 Make_String_Literal (Loc,
3746 Strval => String_From_Name_Buffer)));
3748 else
3749 Append_To (Decls,
3750 Make_Object_Renaming_Declaration (Loc,
3751 Defining_Identifier => Pref,
3752 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3753 Name => Make_Identifier (Loc, Name_uTask_Name)));
3754 end if;
3756 Indx := First_Index (A_Type);
3757 Val := First (Expressions (Id_Ref));
3759 for J in 1 .. Dims loop
3760 T := Make_Temporary (Loc, 'T');
3761 Temps (J) := T;
3763 Append_To (Decls,
3764 Make_Object_Declaration (Loc,
3765 Defining_Identifier => T,
3766 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3767 Expression =>
3768 Make_Attribute_Reference (Loc,
3769 Attribute_Name => Name_Image,
3770 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3771 Expressions => New_List (New_Copy_Tree (Val)))));
3773 Next_Index (Indx);
3774 Next (Val);
3775 end loop;
3777 Sum := Make_Integer_Literal (Loc, Dims + 1);
3779 Sum :=
3780 Make_Op_Add (Loc,
3781 Left_Opnd => Sum,
3782 Right_Opnd =>
3783 Make_Attribute_Reference (Loc,
3784 Attribute_Name => Name_Length,
3785 Prefix => New_Occurrence_Of (Pref, Loc),
3786 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3788 for J in 1 .. Dims loop
3789 Sum :=
3790 Make_Op_Add (Loc,
3791 Left_Opnd => Sum,
3792 Right_Opnd =>
3793 Make_Attribute_Reference (Loc,
3794 Attribute_Name => Name_Length,
3795 Prefix =>
3796 New_Occurrence_Of (Temps (J), Loc),
3797 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3798 end loop;
3800 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3802 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3804 Append_To (Stats,
3805 Make_Assignment_Statement (Loc,
3806 Name =>
3807 Make_Indexed_Component (Loc,
3808 Prefix => New_Occurrence_Of (Res, Loc),
3809 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3810 Expression =>
3811 Make_Character_Literal (Loc,
3812 Chars => Name_Find,
3813 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3815 Append_To (Stats,
3816 Make_Assignment_Statement (Loc,
3817 Name => New_Occurrence_Of (Pos, Loc),
3818 Expression =>
3819 Make_Op_Add (Loc,
3820 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3821 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3823 for J in 1 .. Dims loop
3825 Append_To (Stats,
3826 Make_Assignment_Statement (Loc,
3827 Name =>
3828 Make_Slice (Loc,
3829 Prefix => New_Occurrence_Of (Res, Loc),
3830 Discrete_Range =>
3831 Make_Range (Loc,
3832 Low_Bound => New_Occurrence_Of (Pos, Loc),
3833 High_Bound =>
3834 Make_Op_Subtract (Loc,
3835 Left_Opnd =>
3836 Make_Op_Add (Loc,
3837 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3838 Right_Opnd =>
3839 Make_Attribute_Reference (Loc,
3840 Attribute_Name => Name_Length,
3841 Prefix =>
3842 New_Occurrence_Of (Temps (J), Loc),
3843 Expressions =>
3844 New_List (Make_Integer_Literal (Loc, 1)))),
3845 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3847 Expression => New_Occurrence_Of (Temps (J), Loc)));
3849 if J < Dims then
3850 Append_To (Stats,
3851 Make_Assignment_Statement (Loc,
3852 Name => New_Occurrence_Of (Pos, Loc),
3853 Expression =>
3854 Make_Op_Add (Loc,
3855 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3856 Right_Opnd =>
3857 Make_Attribute_Reference (Loc,
3858 Attribute_Name => Name_Length,
3859 Prefix => New_Occurrence_Of (Temps (J), Loc),
3860 Expressions =>
3861 New_List (Make_Integer_Literal (Loc, 1))))));
3863 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3865 Append_To (Stats,
3866 Make_Assignment_Statement (Loc,
3867 Name => Make_Indexed_Component (Loc,
3868 Prefix => New_Occurrence_Of (Res, Loc),
3869 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3870 Expression =>
3871 Make_Character_Literal (Loc,
3872 Chars => Name_Find,
3873 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3875 Append_To (Stats,
3876 Make_Assignment_Statement (Loc,
3877 Name => New_Occurrence_Of (Pos, Loc),
3878 Expression =>
3879 Make_Op_Add (Loc,
3880 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3881 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3882 end if;
3883 end loop;
3885 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3887 Append_To (Stats,
3888 Make_Assignment_Statement (Loc,
3889 Name =>
3890 Make_Indexed_Component (Loc,
3891 Prefix => New_Occurrence_Of (Res, Loc),
3892 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3893 Expression =>
3894 Make_Character_Literal (Loc,
3895 Chars => Name_Find,
3896 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3897 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3898 end Build_Task_Array_Image;
3900 ----------------------------
3901 -- Build_Task_Image_Decls --
3902 ----------------------------
3904 function Build_Task_Image_Decls
3905 (Loc : Source_Ptr;
3906 Id_Ref : Node_Id;
3907 A_Type : Entity_Id;
3908 In_Init_Proc : Boolean := False) return List_Id
3910 Decls : constant List_Id := New_List;
3911 T_Id : Entity_Id := Empty;
3912 Decl : Node_Id;
3913 Expr : Node_Id := Empty;
3914 Fun : Node_Id := Empty;
3915 Is_Dyn : constant Boolean :=
3916 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3917 and then
3918 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3920 begin
3921 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3922 -- generate a dummy declaration only.
3924 if Restriction_Active (No_Implicit_Heap_Allocations)
3925 or else Global_Discard_Names
3926 then
3927 T_Id := Make_Temporary (Loc, 'J');
3928 Name_Len := 0;
3930 return
3931 New_List (
3932 Make_Object_Declaration (Loc,
3933 Defining_Identifier => T_Id,
3934 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3935 Expression =>
3936 Make_String_Literal (Loc,
3937 Strval => String_From_Name_Buffer)));
3939 else
3940 if Nkind (Id_Ref) = N_Identifier
3941 or else Nkind (Id_Ref) = N_Defining_Identifier
3942 then
3943 -- For a simple variable, the image of the task is built from
3944 -- the name of the variable. To avoid possible conflict with the
3945 -- anonymous type created for a single protected object, add a
3946 -- numeric suffix.
3948 T_Id :=
3949 Make_Defining_Identifier (Loc,
3950 New_External_Name (Chars (Id_Ref), 'T', 1));
3952 Get_Name_String (Chars (Id_Ref));
3954 Expr :=
3955 Make_String_Literal (Loc,
3956 Strval => String_From_Name_Buffer);
3958 elsif Nkind (Id_Ref) = N_Selected_Component then
3959 T_Id :=
3960 Make_Defining_Identifier (Loc,
3961 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3962 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3964 elsif Nkind (Id_Ref) = N_Indexed_Component then
3965 T_Id :=
3966 Make_Defining_Identifier (Loc,
3967 New_External_Name (Chars (A_Type), 'N'));
3969 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3970 end if;
3971 end if;
3973 if Present (Fun) then
3974 Append (Fun, Decls);
3975 Expr := Make_Function_Call (Loc,
3976 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
3978 if not In_Init_Proc then
3979 Set_Uses_Sec_Stack (Defining_Entity (Fun));
3980 end if;
3981 end if;
3983 Decl := Make_Object_Declaration (Loc,
3984 Defining_Identifier => T_Id,
3985 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3986 Constant_Present => True,
3987 Expression => Expr);
3989 Append (Decl, Decls);
3990 return Decls;
3991 end Build_Task_Image_Decls;
3993 -------------------------------
3994 -- Build_Task_Image_Function --
3995 -------------------------------
3997 function Build_Task_Image_Function
3998 (Loc : Source_Ptr;
3999 Decls : List_Id;
4000 Stats : List_Id;
4001 Res : Entity_Id) return Node_Id
4003 Spec : Node_Id;
4005 begin
4006 Append_To (Stats,
4007 Make_Simple_Return_Statement (Loc,
4008 Expression => New_Occurrence_Of (Res, Loc)));
4010 Spec := Make_Function_Specification (Loc,
4011 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4012 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4014 -- Calls to 'Image use the secondary stack, which must be cleaned up
4015 -- after the task name is built.
4017 return Make_Subprogram_Body (Loc,
4018 Specification => Spec,
4019 Declarations => Decls,
4020 Handled_Statement_Sequence =>
4021 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4022 end Build_Task_Image_Function;
4024 -----------------------------
4025 -- Build_Task_Image_Prefix --
4026 -----------------------------
4028 procedure Build_Task_Image_Prefix
4029 (Loc : Source_Ptr;
4030 Len : out Entity_Id;
4031 Res : out Entity_Id;
4032 Pos : out Entity_Id;
4033 Prefix : Entity_Id;
4034 Sum : Node_Id;
4035 Decls : List_Id;
4036 Stats : List_Id)
4038 begin
4039 Len := Make_Temporary (Loc, 'L', Sum);
4041 Append_To (Decls,
4042 Make_Object_Declaration (Loc,
4043 Defining_Identifier => Len,
4044 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4045 Expression => Sum));
4047 Res := Make_Temporary (Loc, 'R');
4049 Append_To (Decls,
4050 Make_Object_Declaration (Loc,
4051 Defining_Identifier => Res,
4052 Object_Definition =>
4053 Make_Subtype_Indication (Loc,
4054 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4055 Constraint =>
4056 Make_Index_Or_Discriminant_Constraint (Loc,
4057 Constraints =>
4058 New_List (
4059 Make_Range (Loc,
4060 Low_Bound => Make_Integer_Literal (Loc, 1),
4061 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4063 -- Indicate that the result is an internal temporary, so it does not
4064 -- receive a bogus initialization when declaration is expanded. This
4065 -- is both efficient, and prevents anomalies in the handling of
4066 -- dynamic objects on the secondary stack.
4068 Set_Is_Internal (Res);
4069 Pos := Make_Temporary (Loc, 'P');
4071 Append_To (Decls,
4072 Make_Object_Declaration (Loc,
4073 Defining_Identifier => Pos,
4074 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4076 -- Pos := Prefix'Length;
4078 Append_To (Stats,
4079 Make_Assignment_Statement (Loc,
4080 Name => New_Occurrence_Of (Pos, Loc),
4081 Expression =>
4082 Make_Attribute_Reference (Loc,
4083 Attribute_Name => Name_Length,
4084 Prefix => New_Occurrence_Of (Prefix, Loc),
4085 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4087 -- Res (1 .. Pos) := Prefix;
4089 Append_To (Stats,
4090 Make_Assignment_Statement (Loc,
4091 Name =>
4092 Make_Slice (Loc,
4093 Prefix => New_Occurrence_Of (Res, Loc),
4094 Discrete_Range =>
4095 Make_Range (Loc,
4096 Low_Bound => Make_Integer_Literal (Loc, 1),
4097 High_Bound => New_Occurrence_Of (Pos, Loc))),
4099 Expression => New_Occurrence_Of (Prefix, Loc)));
4101 Append_To (Stats,
4102 Make_Assignment_Statement (Loc,
4103 Name => New_Occurrence_Of (Pos, Loc),
4104 Expression =>
4105 Make_Op_Add (Loc,
4106 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4107 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4108 end Build_Task_Image_Prefix;
4110 -----------------------------
4111 -- Build_Task_Record_Image --
4112 -----------------------------
4114 function Build_Task_Record_Image
4115 (Loc : Source_Ptr;
4116 Id_Ref : Node_Id;
4117 Dyn : Boolean := False) return Node_Id
4119 Len : Entity_Id;
4120 -- Total length of generated name
4122 Pos : Entity_Id;
4123 -- Index into result
4125 Res : Entity_Id;
4126 -- String to hold result
4128 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4129 -- Name of enclosing variable, prefix of resulting name
4131 Sum : Node_Id;
4132 -- Expression to compute total size of string
4134 Sel : Entity_Id;
4135 -- Entity for selector name
4137 Decls : constant List_Id := New_List;
4138 Stats : constant List_Id := New_List;
4140 begin
4141 -- For a dynamic task, the name comes from the target variable. For a
4142 -- static one it is a formal of the enclosing init proc.
4144 if Dyn then
4145 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4146 Append_To (Decls,
4147 Make_Object_Declaration (Loc,
4148 Defining_Identifier => Pref,
4149 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4150 Expression =>
4151 Make_String_Literal (Loc,
4152 Strval => String_From_Name_Buffer)));
4154 else
4155 Append_To (Decls,
4156 Make_Object_Renaming_Declaration (Loc,
4157 Defining_Identifier => Pref,
4158 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4159 Name => Make_Identifier (Loc, Name_uTask_Name)));
4160 end if;
4162 Sel := Make_Temporary (Loc, 'S');
4164 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4166 Append_To (Decls,
4167 Make_Object_Declaration (Loc,
4168 Defining_Identifier => Sel,
4169 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4170 Expression =>
4171 Make_String_Literal (Loc,
4172 Strval => String_From_Name_Buffer)));
4174 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4176 Sum :=
4177 Make_Op_Add (Loc,
4178 Left_Opnd => Sum,
4179 Right_Opnd =>
4180 Make_Attribute_Reference (Loc,
4181 Attribute_Name => Name_Length,
4182 Prefix =>
4183 New_Occurrence_Of (Pref, Loc),
4184 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4186 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4188 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4190 -- Res (Pos) := '.';
4192 Append_To (Stats,
4193 Make_Assignment_Statement (Loc,
4194 Name => Make_Indexed_Component (Loc,
4195 Prefix => New_Occurrence_Of (Res, Loc),
4196 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4197 Expression =>
4198 Make_Character_Literal (Loc,
4199 Chars => Name_Find,
4200 Char_Literal_Value =>
4201 UI_From_Int (Character'Pos ('.')))));
4203 Append_To (Stats,
4204 Make_Assignment_Statement (Loc,
4205 Name => New_Occurrence_Of (Pos, Loc),
4206 Expression =>
4207 Make_Op_Add (Loc,
4208 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4209 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4211 -- Res (Pos .. Len) := Selector;
4213 Append_To (Stats,
4214 Make_Assignment_Statement (Loc,
4215 Name => Make_Slice (Loc,
4216 Prefix => New_Occurrence_Of (Res, Loc),
4217 Discrete_Range =>
4218 Make_Range (Loc,
4219 Low_Bound => New_Occurrence_Of (Pos, Loc),
4220 High_Bound => New_Occurrence_Of (Len, Loc))),
4221 Expression => New_Occurrence_Of (Sel, Loc)));
4223 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4224 end Build_Task_Record_Image;
4226 ---------------------------------------
4227 -- Build_Transient_Object_Statements --
4228 ---------------------------------------
4230 procedure Build_Transient_Object_Statements
4231 (Obj_Decl : Node_Id;
4232 Fin_Call : out Node_Id;
4233 Hook_Assign : out Node_Id;
4234 Hook_Clear : out Node_Id;
4235 Hook_Decl : out Node_Id;
4236 Ptr_Decl : out Node_Id;
4237 Finalize_Obj : Boolean := True)
4239 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4240 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4241 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4243 Desig_Typ : Entity_Id;
4244 Hook_Expr : Node_Id;
4245 Hook_Id : Entity_Id;
4246 Obj_Ref : Node_Id;
4247 Ptr_Typ : Entity_Id;
4249 begin
4250 -- Recover the type of the object
4252 Desig_Typ := Obj_Typ;
4254 if Is_Access_Type (Desig_Typ) then
4255 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4256 end if;
4258 -- Create an access type which provides a reference to the transient
4259 -- object. Generate:
4261 -- type Ptr_Typ is access all Desig_Typ;
4263 Ptr_Typ := Make_Temporary (Loc, 'A');
4264 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4265 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4267 Ptr_Decl :=
4268 Make_Full_Type_Declaration (Loc,
4269 Defining_Identifier => Ptr_Typ,
4270 Type_Definition =>
4271 Make_Access_To_Object_Definition (Loc,
4272 All_Present => True,
4273 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4275 -- Create a temporary check which acts as a hook to the transient
4276 -- object. Generate:
4278 -- Hook : Ptr_Typ := null;
4280 Hook_Id := Make_Temporary (Loc, 'T');
4281 Set_Ekind (Hook_Id, E_Variable);
4282 Set_Etype (Hook_Id, Ptr_Typ);
4284 Hook_Decl :=
4285 Make_Object_Declaration (Loc,
4286 Defining_Identifier => Hook_Id,
4287 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4288 Expression => Make_Null (Loc));
4290 -- Mark the temporary as a hook. This signals the machinery in
4291 -- Build_Finalizer to recognize this special case.
4293 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4295 -- Hook the transient object to the temporary. Generate:
4297 -- Hook := Ptr_Typ (Obj_Id);
4298 -- <or>
4299 -- Hool := Obj_Id'Unrestricted_Access;
4301 if Is_Access_Type (Obj_Typ) then
4302 Hook_Expr :=
4303 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4304 else
4305 Hook_Expr :=
4306 Make_Attribute_Reference (Loc,
4307 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4308 Attribute_Name => Name_Unrestricted_Access);
4309 end if;
4311 Hook_Assign :=
4312 Make_Assignment_Statement (Loc,
4313 Name => New_Occurrence_Of (Hook_Id, Loc),
4314 Expression => Hook_Expr);
4316 -- Crear the hook prior to finalizing the object. Generate:
4318 -- Hook := null;
4320 Hook_Clear :=
4321 Make_Assignment_Statement (Loc,
4322 Name => New_Occurrence_Of (Hook_Id, Loc),
4323 Expression => Make_Null (Loc));
4325 -- Finalize the object. Generate:
4327 -- [Deep_]Finalize (Obj_Ref[.all]);
4329 if Finalize_Obj then
4330 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4332 if Is_Access_Type (Obj_Typ) then
4333 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4334 Set_Etype (Obj_Ref, Desig_Typ);
4335 end if;
4337 Fin_Call :=
4338 Make_Final_Call
4339 (Obj_Ref => Obj_Ref,
4340 Typ => Desig_Typ);
4342 -- Otherwise finalize the hook. Generate:
4344 -- [Deep_]Finalize (Hook.all);
4346 else
4347 Fin_Call :=
4348 Make_Final_Call (
4349 Obj_Ref =>
4350 Make_Explicit_Dereference (Loc,
4351 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4352 Typ => Desig_Typ);
4353 end if;
4354 end Build_Transient_Object_Statements;
4356 -----------------------------
4357 -- Check_Float_Op_Overflow --
4358 -----------------------------
4360 procedure Check_Float_Op_Overflow (N : Node_Id) is
4361 begin
4362 -- Return if no check needed
4364 if not Is_Floating_Point_Type (Etype (N))
4365 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4367 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4368 -- and do not expand the code for float overflow checking.
4370 or else CodePeer_Mode
4371 then
4372 return;
4373 end if;
4375 -- Otherwise we replace the expression by
4377 -- do Tnn : constant ftype := expression;
4378 -- constraint_error when not Tnn'Valid;
4379 -- in Tnn;
4381 declare
4382 Loc : constant Source_Ptr := Sloc (N);
4383 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4384 Typ : constant Entity_Id := Etype (N);
4386 begin
4387 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4388 -- right here. We also set the node as analyzed to prevent infinite
4389 -- recursion from repeating the operation in the expansion.
4391 Set_Do_Overflow_Check (N, False);
4392 Set_Analyzed (N, True);
4394 -- Do the rewrite to include the check
4396 Rewrite (N,
4397 Make_Expression_With_Actions (Loc,
4398 Actions => New_List (
4399 Make_Object_Declaration (Loc,
4400 Defining_Identifier => Tnn,
4401 Object_Definition => New_Occurrence_Of (Typ, Loc),
4402 Constant_Present => True,
4403 Expression => Relocate_Node (N)),
4404 Make_Raise_Constraint_Error (Loc,
4405 Condition =>
4406 Make_Op_Not (Loc,
4407 Right_Opnd =>
4408 Make_Attribute_Reference (Loc,
4409 Prefix => New_Occurrence_Of (Tnn, Loc),
4410 Attribute_Name => Name_Valid)),
4411 Reason => CE_Overflow_Check_Failed)),
4412 Expression => New_Occurrence_Of (Tnn, Loc)));
4414 Analyze_And_Resolve (N, Typ);
4415 end;
4416 end Check_Float_Op_Overflow;
4418 ----------------------------------
4419 -- Component_May_Be_Bit_Aligned --
4420 ----------------------------------
4422 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4423 UT : Entity_Id;
4425 begin
4426 -- If no component clause, then everything is fine, since the back end
4427 -- never bit-misaligns by default, even if there is a pragma Packed for
4428 -- the record.
4430 if No (Comp) or else No (Component_Clause (Comp)) then
4431 return False;
4432 end if;
4434 UT := Underlying_Type (Etype (Comp));
4436 -- It is only array and record types that cause trouble
4438 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4439 return False;
4441 -- If we know that we have a small (64 bits or less) record or small
4442 -- bit-packed array, then everything is fine, since the back end can
4443 -- handle these cases correctly.
4445 elsif Esize (Comp) <= 64
4446 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4447 then
4448 return False;
4450 -- Otherwise if the component is not byte aligned, we know we have the
4451 -- nasty unaligned case.
4453 elsif Normalized_First_Bit (Comp) /= Uint_0
4454 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4455 then
4456 return True;
4458 -- If we are large and byte aligned, then OK at this level
4460 else
4461 return False;
4462 end if;
4463 end Component_May_Be_Bit_Aligned;
4465 ----------------------------------------
4466 -- Containing_Package_With_Ext_Axioms --
4467 ----------------------------------------
4469 function Containing_Package_With_Ext_Axioms
4470 (E : Entity_Id) return Entity_Id
4472 begin
4473 -- E is the package or generic package which is externally axiomatized
4475 if Ekind_In (E, E_Generic_Package, E_Package)
4476 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4477 then
4478 return E;
4479 end if;
4481 -- If E's scope is axiomatized, E is axiomatized
4483 if Present (Scope (E)) then
4484 declare
4485 First_Ax_Parent_Scope : constant Entity_Id :=
4486 Containing_Package_With_Ext_Axioms (Scope (E));
4487 begin
4488 if Present (First_Ax_Parent_Scope) then
4489 return First_Ax_Parent_Scope;
4490 end if;
4491 end;
4492 end if;
4494 -- Otherwise, if E is a package instance, it is axiomatized if the
4495 -- corresponding generic package is axiomatized.
4497 if Ekind (E) = E_Package then
4498 declare
4499 Par : constant Node_Id := Parent (E);
4500 Decl : Node_Id;
4502 begin
4503 if Nkind (Par) = N_Defining_Program_Unit_Name then
4504 Decl := Parent (Par);
4505 else
4506 Decl := Par;
4507 end if;
4509 if Present (Generic_Parent (Decl)) then
4510 return
4511 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4512 end if;
4513 end;
4514 end if;
4516 return Empty;
4517 end Containing_Package_With_Ext_Axioms;
4519 -------------------------------
4520 -- Convert_To_Actual_Subtype --
4521 -------------------------------
4523 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4524 Act_ST : Entity_Id;
4526 begin
4527 Act_ST := Get_Actual_Subtype (Exp);
4529 if Act_ST = Etype (Exp) then
4530 return;
4531 else
4532 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4533 Analyze_And_Resolve (Exp, Act_ST);
4534 end if;
4535 end Convert_To_Actual_Subtype;
4537 -----------------------------------
4538 -- Corresponding_Runtime_Package --
4539 -----------------------------------
4541 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4542 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4543 -- Return True if protected type T has one entry and the maximum queue
4544 -- length is one.
4546 --------------------------------
4547 -- Has_One_Entry_And_No_Queue --
4548 --------------------------------
4550 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4551 Item : Entity_Id;
4552 Is_First : Boolean := True;
4554 begin
4555 Item := First_Entity (T);
4556 while Present (Item) loop
4557 if Is_Entry (Item) then
4559 -- The protected type has more than one entry
4561 if not Is_First then
4562 return False;
4563 end if;
4565 -- The queue length is not one
4567 if not Restriction_Active (No_Entry_Queue)
4568 and then Get_Max_Queue_Length (Item) /= Uint_1
4569 then
4570 return False;
4571 end if;
4573 Is_First := False;
4574 end if;
4576 Next_Entity (Item);
4577 end loop;
4579 return True;
4580 end Has_One_Entry_And_No_Queue;
4582 -- Local variables
4584 Pkg_Id : RTU_Id := RTU_Null;
4586 -- Start of processing for Corresponding_Runtime_Package
4588 begin
4589 pragma Assert (Is_Concurrent_Type (Typ));
4591 if Ekind (Typ) in Protected_Kind then
4592 if Has_Entries (Typ)
4594 -- A protected type without entries that covers an interface and
4595 -- overrides the abstract routines with protected procedures is
4596 -- considered equivalent to a protected type with entries in the
4597 -- context of dispatching select statements. It is sufficient to
4598 -- check for the presence of an interface list in the declaration
4599 -- node to recognize this case.
4601 or else Present (Interface_List (Parent (Typ)))
4603 -- Protected types with interrupt handlers (when not using a
4604 -- restricted profile) are also considered equivalent to
4605 -- protected types with entries. The types which are used
4606 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4607 -- are derived from Protection_Entries.
4609 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4610 or else Has_Interrupt_Handler (Typ)
4611 then
4612 if Abort_Allowed
4613 or else Restriction_Active (No_Select_Statements) = False
4614 or else not Has_One_Entry_And_No_Queue (Typ)
4615 or else (Has_Attach_Handler (Typ)
4616 and then not Restricted_Profile)
4617 then
4618 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4619 else
4620 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4621 end if;
4623 else
4624 Pkg_Id := System_Tasking_Protected_Objects;
4625 end if;
4626 end if;
4628 return Pkg_Id;
4629 end Corresponding_Runtime_Package;
4631 -----------------------------------
4632 -- Current_Sem_Unit_Declarations --
4633 -----------------------------------
4635 function Current_Sem_Unit_Declarations return List_Id is
4636 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4637 Decls : List_Id;
4639 begin
4640 -- If the current unit is a package body, locate the visible
4641 -- declarations of the package spec.
4643 if Nkind (U) = N_Package_Body then
4644 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4645 end if;
4647 if Nkind (U) = N_Package_Declaration then
4648 U := Specification (U);
4649 Decls := Visible_Declarations (U);
4651 if No (Decls) then
4652 Decls := New_List;
4653 Set_Visible_Declarations (U, Decls);
4654 end if;
4656 else
4657 Decls := Declarations (U);
4659 if No (Decls) then
4660 Decls := New_List;
4661 Set_Declarations (U, Decls);
4662 end if;
4663 end if;
4665 return Decls;
4666 end Current_Sem_Unit_Declarations;
4668 -----------------------
4669 -- Duplicate_Subexpr --
4670 -----------------------
4672 function Duplicate_Subexpr
4673 (Exp : Node_Id;
4674 Name_Req : Boolean := False;
4675 Renaming_Req : Boolean := False) return Node_Id
4677 begin
4678 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4679 return New_Copy_Tree (Exp);
4680 end Duplicate_Subexpr;
4682 ---------------------------------
4683 -- Duplicate_Subexpr_No_Checks --
4684 ---------------------------------
4686 function Duplicate_Subexpr_No_Checks
4687 (Exp : Node_Id;
4688 Name_Req : Boolean := False;
4689 Renaming_Req : Boolean := False;
4690 Related_Id : Entity_Id := Empty;
4691 Is_Low_Bound : Boolean := False;
4692 Is_High_Bound : Boolean := False) return Node_Id
4694 New_Exp : Node_Id;
4696 begin
4697 Remove_Side_Effects
4698 (Exp => Exp,
4699 Name_Req => Name_Req,
4700 Renaming_Req => Renaming_Req,
4701 Related_Id => Related_Id,
4702 Is_Low_Bound => Is_Low_Bound,
4703 Is_High_Bound => Is_High_Bound);
4705 New_Exp := New_Copy_Tree (Exp);
4706 Remove_Checks (New_Exp);
4707 return New_Exp;
4708 end Duplicate_Subexpr_No_Checks;
4710 -----------------------------------
4711 -- Duplicate_Subexpr_Move_Checks --
4712 -----------------------------------
4714 function Duplicate_Subexpr_Move_Checks
4715 (Exp : Node_Id;
4716 Name_Req : Boolean := False;
4717 Renaming_Req : Boolean := False) return Node_Id
4719 New_Exp : Node_Id;
4721 begin
4722 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4723 New_Exp := New_Copy_Tree (Exp);
4724 Remove_Checks (Exp);
4725 return New_Exp;
4726 end Duplicate_Subexpr_Move_Checks;
4728 --------------------
4729 -- Ensure_Defined --
4730 --------------------
4732 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4733 IR : Node_Id;
4735 begin
4736 -- An itype reference must only be created if this is a local itype, so
4737 -- that gigi can elaborate it on the proper objstack.
4739 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4740 IR := Make_Itype_Reference (Sloc (N));
4741 Set_Itype (IR, Typ);
4742 Insert_Action (N, IR);
4743 end if;
4744 end Ensure_Defined;
4746 --------------------
4747 -- Entry_Names_OK --
4748 --------------------
4750 function Entry_Names_OK return Boolean is
4751 begin
4752 return
4753 not Restricted_Profile
4754 and then not Global_Discard_Names
4755 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4756 and then not Restriction_Active (No_Local_Allocators);
4757 end Entry_Names_OK;
4759 -------------------
4760 -- Evaluate_Name --
4761 -------------------
4763 procedure Evaluate_Name (Nam : Node_Id) is
4764 begin
4765 -- For an attribute reference or an indexed component, evaluate the
4766 -- prefix, which is itself a name, recursively, and then force the
4767 -- evaluation of all the subscripts (or attribute expressions).
4769 case Nkind (Nam) is
4770 when N_Attribute_Reference
4771 | N_Indexed_Component
4773 Evaluate_Name (Prefix (Nam));
4775 declare
4776 E : Node_Id;
4778 begin
4779 E := First (Expressions (Nam));
4780 while Present (E) loop
4781 Force_Evaluation (E);
4783 if Original_Node (E) /= E then
4784 Set_Do_Range_Check
4785 (E, Do_Range_Check (Original_Node (E)));
4786 end if;
4788 Next (E);
4789 end loop;
4790 end;
4792 -- For an explicit dereference, we simply force the evaluation of
4793 -- the name expression. The dereference provides a value that is the
4794 -- address for the renamed object, and it is precisely this value
4795 -- that we want to preserve.
4797 when N_Explicit_Dereference =>
4798 Force_Evaluation (Prefix (Nam));
4800 -- For a function call, we evaluate the call
4802 when N_Function_Call =>
4803 Force_Evaluation (Nam);
4805 -- For a qualified expression, we evaluate the underlying object
4806 -- name if any, otherwise we force the evaluation of the underlying
4807 -- expression.
4809 when N_Qualified_Expression =>
4810 if Is_Object_Reference (Expression (Nam)) then
4811 Evaluate_Name (Expression (Nam));
4812 else
4813 Force_Evaluation (Expression (Nam));
4814 end if;
4816 -- For a selected component, we simply evaluate the prefix
4818 when N_Selected_Component =>
4819 Evaluate_Name (Prefix (Nam));
4821 -- For a slice, we evaluate the prefix, as for the indexed component
4822 -- case and then, if there is a range present, either directly or as
4823 -- the constraint of a discrete subtype indication, we evaluate the
4824 -- two bounds of this range.
4826 when N_Slice =>
4827 Evaluate_Name (Prefix (Nam));
4828 Evaluate_Slice_Bounds (Nam);
4830 -- For a type conversion, the expression of the conversion must be
4831 -- the name of an object, and we simply need to evaluate this name.
4833 when N_Type_Conversion =>
4834 Evaluate_Name (Expression (Nam));
4836 -- The remaining cases are direct name, operator symbol and character
4837 -- literal. In all these cases, we do nothing, since we want to
4838 -- reevaluate each time the renamed object is used.
4840 when others =>
4841 null;
4842 end case;
4843 end Evaluate_Name;
4845 ---------------------------
4846 -- Evaluate_Slice_Bounds --
4847 ---------------------------
4849 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4850 DR : constant Node_Id := Discrete_Range (Slice);
4851 Constr : Node_Id;
4852 Rexpr : Node_Id;
4854 begin
4855 if Nkind (DR) = N_Range then
4856 Force_Evaluation (Low_Bound (DR));
4857 Force_Evaluation (High_Bound (DR));
4859 elsif Nkind (DR) = N_Subtype_Indication then
4860 Constr := Constraint (DR);
4862 if Nkind (Constr) = N_Range_Constraint then
4863 Rexpr := Range_Expression (Constr);
4865 Force_Evaluation (Low_Bound (Rexpr));
4866 Force_Evaluation (High_Bound (Rexpr));
4867 end if;
4868 end if;
4869 end Evaluate_Slice_Bounds;
4871 ---------------------
4872 -- Evolve_And_Then --
4873 ---------------------
4875 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4876 begin
4877 if No (Cond) then
4878 Cond := Cond1;
4879 else
4880 Cond :=
4881 Make_And_Then (Sloc (Cond1),
4882 Left_Opnd => Cond,
4883 Right_Opnd => Cond1);
4884 end if;
4885 end Evolve_And_Then;
4887 --------------------
4888 -- Evolve_Or_Else --
4889 --------------------
4891 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4892 begin
4893 if No (Cond) then
4894 Cond := Cond1;
4895 else
4896 Cond :=
4897 Make_Or_Else (Sloc (Cond1),
4898 Left_Opnd => Cond,
4899 Right_Opnd => Cond1);
4900 end if;
4901 end Evolve_Or_Else;
4903 -----------------------------------
4904 -- Exceptions_In_Finalization_OK --
4905 -----------------------------------
4907 function Exceptions_In_Finalization_OK return Boolean is
4908 begin
4909 return
4910 not (Restriction_Active (No_Exception_Handlers) or else
4911 Restriction_Active (No_Exception_Propagation) or else
4912 Restriction_Active (No_Exceptions));
4913 end Exceptions_In_Finalization_OK;
4915 -----------------------------------------
4916 -- Expand_Static_Predicates_In_Choices --
4917 -----------------------------------------
4919 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4920 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4922 Choices : constant List_Id := Discrete_Choices (N);
4924 Choice : Node_Id;
4925 Next_C : Node_Id;
4926 P : Node_Id;
4927 C : Node_Id;
4929 begin
4930 Choice := First (Choices);
4931 while Present (Choice) loop
4932 Next_C := Next (Choice);
4934 -- Check for name of subtype with static predicate
4936 if Is_Entity_Name (Choice)
4937 and then Is_Type (Entity (Choice))
4938 and then Has_Predicates (Entity (Choice))
4939 then
4940 -- Loop through entries in predicate list, converting to choices
4941 -- and inserting in the list before the current choice. Note that
4942 -- if the list is empty, corresponding to a False predicate, then
4943 -- no choices are inserted.
4945 P := First (Static_Discrete_Predicate (Entity (Choice)));
4946 while Present (P) loop
4948 -- If low bound and high bounds are equal, copy simple choice
4950 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4951 C := New_Copy (Low_Bound (P));
4953 -- Otherwise copy a range
4955 else
4956 C := New_Copy (P);
4957 end if;
4959 -- Change Sloc to referencing choice (rather than the Sloc of
4960 -- the predicate declaration element itself).
4962 Set_Sloc (C, Sloc (Choice));
4963 Insert_Before (Choice, C);
4964 Next (P);
4965 end loop;
4967 -- Delete the predicated entry
4969 Remove (Choice);
4970 end if;
4972 -- Move to next choice to check
4974 Choice := Next_C;
4975 end loop;
4976 end Expand_Static_Predicates_In_Choices;
4978 ------------------------------
4979 -- Expand_Subtype_From_Expr --
4980 ------------------------------
4982 -- This function is applicable for both static and dynamic allocation of
4983 -- objects which are constrained by an initial expression. Basically it
4984 -- transforms an unconstrained subtype indication into a constrained one.
4986 -- The expression may also be transformed in certain cases in order to
4987 -- avoid multiple evaluation. In the static allocation case, the general
4988 -- scheme is:
4990 -- Val : T := Expr;
4992 -- is transformed into
4994 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
4996 -- Here are the main cases :
4998 -- <if Expr is a Slice>
4999 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5001 -- <elsif Expr is a String Literal>
5002 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5004 -- <elsif Expr is Constrained>
5005 -- subtype T is Type_Of_Expr
5006 -- Val : T := Expr;
5008 -- <elsif Expr is an entity_name>
5009 -- Val : T (constraints taken from Expr) := Expr;
5011 -- <else>
5012 -- type Axxx is access all T;
5013 -- Rval : Axxx := Expr'ref;
5014 -- Val : T (constraints taken from Rval) := Rval.all;
5016 -- ??? note: when the Expression is allocated in the secondary stack
5017 -- we could use it directly instead of copying it by declaring
5018 -- Val : T (...) renames Rval.all
5020 procedure Expand_Subtype_From_Expr
5021 (N : Node_Id;
5022 Unc_Type : Entity_Id;
5023 Subtype_Indic : Node_Id;
5024 Exp : Node_Id;
5025 Related_Id : Entity_Id := Empty)
5027 Loc : constant Source_Ptr := Sloc (N);
5028 Exp_Typ : constant Entity_Id := Etype (Exp);
5029 T : Entity_Id;
5031 begin
5032 -- In general we cannot build the subtype if expansion is disabled,
5033 -- because internal entities may not have been defined. However, to
5034 -- avoid some cascaded errors, we try to continue when the expression is
5035 -- an array (or string), because it is safe to compute the bounds. It is
5036 -- in fact required to do so even in a generic context, because there
5037 -- may be constants that depend on the bounds of a string literal, both
5038 -- standard string types and more generally arrays of characters.
5040 -- In GNATprove mode, these extra subtypes are not needed
5042 if GNATprove_Mode then
5043 return;
5044 end if;
5046 if not Expander_Active
5047 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5048 then
5049 return;
5050 end if;
5052 if Nkind (Exp) = N_Slice then
5053 declare
5054 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5056 begin
5057 Rewrite (Subtype_Indic,
5058 Make_Subtype_Indication (Loc,
5059 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5060 Constraint =>
5061 Make_Index_Or_Discriminant_Constraint (Loc,
5062 Constraints => New_List
5063 (New_Occurrence_Of (Slice_Type, Loc)))));
5065 -- This subtype indication may be used later for constraint checks
5066 -- we better make sure that if a variable was used as a bound of
5067 -- of the original slice, its value is frozen.
5069 Evaluate_Slice_Bounds (Exp);
5070 end;
5072 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5073 Rewrite (Subtype_Indic,
5074 Make_Subtype_Indication (Loc,
5075 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5076 Constraint =>
5077 Make_Index_Or_Discriminant_Constraint (Loc,
5078 Constraints => New_List (
5079 Make_Literal_Range (Loc,
5080 Literal_Typ => Exp_Typ)))));
5082 -- If the type of the expression is an internally generated type it
5083 -- may not be necessary to create a new subtype. However there are two
5084 -- exceptions: references to the current instances, and aliased array
5085 -- object declarations for which the back end has to create a template.
5087 elsif Is_Constrained (Exp_Typ)
5088 and then not Is_Class_Wide_Type (Unc_Type)
5089 and then
5090 (Nkind (N) /= N_Object_Declaration
5091 or else not Is_Entity_Name (Expression (N))
5092 or else not Comes_From_Source (Entity (Expression (N)))
5093 or else not Is_Array_Type (Exp_Typ)
5094 or else not Aliased_Present (N))
5095 then
5096 if Is_Itype (Exp_Typ) then
5098 -- Within an initialization procedure, a selected component
5099 -- denotes a component of the enclosing record, and it appears as
5100 -- an actual in a call to its own initialization procedure. If
5101 -- this component depends on the outer discriminant, we must
5102 -- generate the proper actual subtype for it.
5104 if Nkind (Exp) = N_Selected_Component
5105 and then Within_Init_Proc
5106 then
5107 declare
5108 Decl : constant Node_Id :=
5109 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5110 begin
5111 if Present (Decl) then
5112 Insert_Action (N, Decl);
5113 T := Defining_Identifier (Decl);
5114 else
5115 T := Exp_Typ;
5116 end if;
5117 end;
5119 -- No need to generate a new subtype
5121 else
5122 T := Exp_Typ;
5123 end if;
5125 else
5126 T := Make_Temporary (Loc, 'T');
5128 Insert_Action (N,
5129 Make_Subtype_Declaration (Loc,
5130 Defining_Identifier => T,
5131 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5133 -- This type is marked as an itype even though it has an explicit
5134 -- declaration since otherwise Is_Generic_Actual_Type can get
5135 -- set, resulting in the generation of spurious errors. (See
5136 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5138 Set_Is_Itype (T);
5139 Set_Associated_Node_For_Itype (T, Exp);
5140 end if;
5142 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5144 -- Nothing needs to be done for private types with unknown discriminants
5145 -- if the underlying type is not an unconstrained composite type or it
5146 -- is an unchecked union.
5148 elsif Is_Private_Type (Unc_Type)
5149 and then Has_Unknown_Discriminants (Unc_Type)
5150 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5151 or else Is_Constrained (Underlying_Type (Unc_Type))
5152 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5153 then
5154 null;
5156 -- Case of derived type with unknown discriminants where the parent type
5157 -- also has unknown discriminants.
5159 elsif Is_Record_Type (Unc_Type)
5160 and then not Is_Class_Wide_Type (Unc_Type)
5161 and then Has_Unknown_Discriminants (Unc_Type)
5162 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5163 then
5164 -- Nothing to be done if no underlying record view available
5166 -- If this is a limited type derived from a type with unknown
5167 -- discriminants, do not expand either, so that subsequent expansion
5168 -- of the call can add build-in-place parameters to call.
5170 if No (Underlying_Record_View (Unc_Type))
5171 or else Is_Limited_Type (Unc_Type)
5172 then
5173 null;
5175 -- Otherwise use the Underlying_Record_View to create the proper
5176 -- constrained subtype for an object of a derived type with unknown
5177 -- discriminants.
5179 else
5180 Remove_Side_Effects (Exp);
5181 Rewrite (Subtype_Indic,
5182 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5183 end if;
5185 -- Renamings of class-wide interface types require no equivalent
5186 -- constrained type declarations because we only need to reference
5187 -- the tag component associated with the interface. The same is
5188 -- presumably true for class-wide types in general, so this test
5189 -- is broadened to include all class-wide renamings, which also
5190 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5191 -- (Is this really correct, or are there some cases of class-wide
5192 -- renamings that require action in this procedure???)
5194 elsif Present (N)
5195 and then Nkind (N) = N_Object_Renaming_Declaration
5196 and then Is_Class_Wide_Type (Unc_Type)
5197 then
5198 null;
5200 -- In Ada 95 nothing to be done if the type of the expression is limited
5201 -- because in this case the expression cannot be copied, and its use can
5202 -- only be by reference.
5204 -- In Ada 2005 the context can be an object declaration whose expression
5205 -- is a function that returns in place. If the nominal subtype has
5206 -- unknown discriminants, the call still provides constraints on the
5207 -- object, and we have to create an actual subtype from it.
5209 -- If the type is class-wide, the expression is dynamically tagged and
5210 -- we do not create an actual subtype either. Ditto for an interface.
5211 -- For now this applies only if the type is immutably limited, and the
5212 -- function being called is build-in-place. This will have to be revised
5213 -- when build-in-place functions are generalized to other types.
5215 elsif Is_Limited_View (Exp_Typ)
5216 and then
5217 (Is_Class_Wide_Type (Exp_Typ)
5218 or else Is_Interface (Exp_Typ)
5219 or else not Has_Unknown_Discriminants (Exp_Typ)
5220 or else not Is_Composite_Type (Unc_Type))
5221 then
5222 null;
5224 -- For limited objects initialized with build in place function calls,
5225 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5226 -- node in the expression initializing the object, which breaks the
5227 -- circuitry that detects and adds the additional arguments to the
5228 -- called function.
5230 elsif Is_Build_In_Place_Function_Call (Exp) then
5231 null;
5233 else
5234 Remove_Side_Effects (Exp);
5235 Rewrite (Subtype_Indic,
5236 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5237 end if;
5238 end Expand_Subtype_From_Expr;
5240 ---------------------------------------------
5241 -- Expression_Contains_Primitives_Calls_Of --
5242 ---------------------------------------------
5244 function Expression_Contains_Primitives_Calls_Of
5245 (Expr : Node_Id;
5246 Typ : Entity_Id) return Boolean
5248 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5250 Calls_OK : Boolean := False;
5251 -- This flag is set to True when expression Expr contains at least one
5252 -- call to a nondispatching primitive function of Typ.
5254 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5255 -- Search for nondispatching calls to primitive functions of type Typ
5257 ----------------------------
5258 -- Search_Primitive_Calls --
5259 ----------------------------
5261 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5262 Disp_Typ : Entity_Id;
5263 Subp : Entity_Id;
5265 begin
5266 -- Detect a function call that could denote a nondispatching
5267 -- primitive of the input type.
5269 if Nkind (N) = N_Function_Call
5270 and then Is_Entity_Name (Name (N))
5271 then
5272 Subp := Entity (Name (N));
5274 -- Do not consider function calls with a controlling argument, as
5275 -- those are always dispatching calls.
5277 if Is_Dispatching_Operation (Subp)
5278 and then No (Controlling_Argument (N))
5279 then
5280 Disp_Typ := Find_Dispatching_Type (Subp);
5282 -- To qualify as a suitable primitive, the dispatching type of
5283 -- the function must be the input type.
5285 if Present (Disp_Typ)
5286 and then Unique_Entity (Disp_Typ) = U_Typ
5287 then
5288 Calls_OK := True;
5290 -- There is no need to continue the traversal, as one such
5291 -- call suffices.
5293 return Abandon;
5294 end if;
5295 end if;
5296 end if;
5298 return OK;
5299 end Search_Primitive_Calls;
5301 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5303 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5305 begin
5306 Search_Calls (Expr);
5307 return Calls_OK;
5308 end Expression_Contains_Primitives_Calls_Of;
5310 ----------------------
5311 -- Finalize_Address --
5312 ----------------------
5314 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5315 Utyp : Entity_Id := Typ;
5317 begin
5318 -- Handle protected class-wide or task class-wide types
5320 if Is_Class_Wide_Type (Utyp) then
5321 if Is_Concurrent_Type (Root_Type (Utyp)) then
5322 Utyp := Root_Type (Utyp);
5324 elsif Is_Private_Type (Root_Type (Utyp))
5325 and then Present (Full_View (Root_Type (Utyp)))
5326 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5327 then
5328 Utyp := Full_View (Root_Type (Utyp));
5329 end if;
5330 end if;
5332 -- Handle private types
5334 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5335 Utyp := Full_View (Utyp);
5336 end if;
5338 -- Handle protected and task types
5340 if Is_Concurrent_Type (Utyp)
5341 and then Present (Corresponding_Record_Type (Utyp))
5342 then
5343 Utyp := Corresponding_Record_Type (Utyp);
5344 end if;
5346 Utyp := Underlying_Type (Base_Type (Utyp));
5348 -- Deal with untagged derivation of private views. If the parent is
5349 -- now known to be protected, the finalization routine is the one
5350 -- defined on the corresponding record of the ancestor (corresponding
5351 -- records do not automatically inherit operations, but maybe they
5352 -- should???)
5354 if Is_Untagged_Derivation (Typ) then
5355 if Is_Protected_Type (Typ) then
5356 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5358 else
5359 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5361 if Is_Protected_Type (Utyp) then
5362 Utyp := Corresponding_Record_Type (Utyp);
5363 end if;
5364 end if;
5365 end if;
5367 -- If the underlying_type is a subtype, we are dealing with the
5368 -- completion of a private type. We need to access the base type and
5369 -- generate a conversion to it.
5371 if Utyp /= Base_Type (Utyp) then
5372 pragma Assert (Is_Private_Type (Typ));
5374 Utyp := Base_Type (Utyp);
5375 end if;
5377 -- When dealing with an internally built full view for a type with
5378 -- unknown discriminants, use the original record type.
5380 if Is_Underlying_Record_View (Utyp) then
5381 Utyp := Etype (Utyp);
5382 end if;
5384 return TSS (Utyp, TSS_Finalize_Address);
5385 end Finalize_Address;
5387 -------------------
5388 -- Find_DIC_Type --
5389 -------------------
5391 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id is
5392 Curr_Typ : Entity_Id;
5393 -- The current type being examined in the parent hierarchy traversal
5395 DIC_Typ : Entity_Id;
5396 -- The type which carries the DIC pragma. This variable denotes the
5397 -- partial view when private types are involved.
5399 Par_Typ : Entity_Id;
5400 -- The parent type of the current type. This variable denotes the full
5401 -- view when private types are involved.
5403 begin
5404 -- The input type defines its own DIC pragma, therefore it is the owner
5406 if Has_Own_DIC (Typ) then
5407 DIC_Typ := Typ;
5409 -- Otherwise the DIC pragma is inherited from a parent type
5411 else
5412 pragma Assert (Has_Inherited_DIC (Typ));
5414 -- Climb the parent chain
5416 Curr_Typ := Typ;
5417 loop
5418 -- Inspect the parent type. Do not consider subtypes as they
5419 -- inherit the DIC attributes from their base types.
5421 DIC_Typ := Base_Type (Etype (Curr_Typ));
5423 -- Look at the full view of a private type because the type may
5424 -- have a hidden parent introduced in the full view.
5426 Par_Typ := DIC_Typ;
5428 if Is_Private_Type (Par_Typ)
5429 and then Present (Full_View (Par_Typ))
5430 then
5431 Par_Typ := Full_View (Par_Typ);
5432 end if;
5434 -- Stop the climb once the nearest parent type which defines a DIC
5435 -- pragma of its own is encountered or when the root of the parent
5436 -- chain is reached.
5438 exit when Has_Own_DIC (DIC_Typ) or else Curr_Typ = Par_Typ;
5440 Curr_Typ := Par_Typ;
5441 end loop;
5442 end if;
5444 return DIC_Typ;
5445 end Find_DIC_Type;
5447 ------------------------
5448 -- Find_Interface_ADT --
5449 ------------------------
5451 function Find_Interface_ADT
5452 (T : Entity_Id;
5453 Iface : Entity_Id) return Elmt_Id
5455 ADT : Elmt_Id;
5456 Typ : Entity_Id := T;
5458 begin
5459 pragma Assert (Is_Interface (Iface));
5461 -- Handle private types
5463 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5464 Typ := Full_View (Typ);
5465 end if;
5467 -- Handle access types
5469 if Is_Access_Type (Typ) then
5470 Typ := Designated_Type (Typ);
5471 end if;
5473 -- Handle task and protected types implementing interfaces
5475 if Is_Concurrent_Type (Typ) then
5476 Typ := Corresponding_Record_Type (Typ);
5477 end if;
5479 pragma Assert
5480 (not Is_Class_Wide_Type (Typ)
5481 and then Ekind (Typ) /= E_Incomplete_Type);
5483 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5484 return First_Elmt (Access_Disp_Table (Typ));
5486 else
5487 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5488 while Present (ADT)
5489 and then Present (Related_Type (Node (ADT)))
5490 and then Related_Type (Node (ADT)) /= Iface
5491 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5492 Use_Full_View => True)
5493 loop
5494 Next_Elmt (ADT);
5495 end loop;
5497 pragma Assert (Present (Related_Type (Node (ADT))));
5498 return ADT;
5499 end if;
5500 end Find_Interface_ADT;
5502 ------------------------
5503 -- Find_Interface_Tag --
5504 ------------------------
5506 function Find_Interface_Tag
5507 (T : Entity_Id;
5508 Iface : Entity_Id) return Entity_Id
5510 AI_Tag : Entity_Id;
5511 Found : Boolean := False;
5512 Typ : Entity_Id := T;
5514 procedure Find_Tag (Typ : Entity_Id);
5515 -- Internal subprogram used to recursively climb to the ancestors
5517 --------------
5518 -- Find_Tag --
5519 --------------
5521 procedure Find_Tag (Typ : Entity_Id) is
5522 AI_Elmt : Elmt_Id;
5523 AI : Node_Id;
5525 begin
5526 -- This routine does not handle the case in which the interface is an
5527 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5529 pragma Assert (Typ /= Iface);
5531 -- Climb to the root type handling private types
5533 if Present (Full_View (Etype (Typ))) then
5534 if Full_View (Etype (Typ)) /= Typ then
5535 Find_Tag (Full_View (Etype (Typ)));
5536 end if;
5538 elsif Etype (Typ) /= Typ then
5539 Find_Tag (Etype (Typ));
5540 end if;
5542 -- Traverse the list of interfaces implemented by the type
5544 if not Found
5545 and then Present (Interfaces (Typ))
5546 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5547 then
5548 -- Skip the tag associated with the primary table
5550 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5551 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5552 pragma Assert (Present (AI_Tag));
5554 AI_Elmt := First_Elmt (Interfaces (Typ));
5555 while Present (AI_Elmt) loop
5556 AI := Node (AI_Elmt);
5558 if AI = Iface
5559 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5560 then
5561 Found := True;
5562 return;
5563 end if;
5565 AI_Tag := Next_Tag_Component (AI_Tag);
5566 Next_Elmt (AI_Elmt);
5567 end loop;
5568 end if;
5569 end Find_Tag;
5571 -- Start of processing for Find_Interface_Tag
5573 begin
5574 pragma Assert (Is_Interface (Iface));
5576 -- Handle access types
5578 if Is_Access_Type (Typ) then
5579 Typ := Designated_Type (Typ);
5580 end if;
5582 -- Handle class-wide types
5584 if Is_Class_Wide_Type (Typ) then
5585 Typ := Root_Type (Typ);
5586 end if;
5588 -- Handle private types
5590 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5591 Typ := Full_View (Typ);
5592 end if;
5594 -- Handle entities from the limited view
5596 if Ekind (Typ) = E_Incomplete_Type then
5597 pragma Assert (Present (Non_Limited_View (Typ)));
5598 Typ := Non_Limited_View (Typ);
5599 end if;
5601 -- Handle task and protected types implementing interfaces
5603 if Is_Concurrent_Type (Typ) then
5604 Typ := Corresponding_Record_Type (Typ);
5605 end if;
5607 -- If the interface is an ancestor of the type, then it shared the
5608 -- primary dispatch table.
5610 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5611 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5612 return First_Tag_Component (Typ);
5614 -- Otherwise we need to search for its associated tag component
5616 else
5617 Find_Tag (Typ);
5618 pragma Assert (Found);
5619 return AI_Tag;
5620 end if;
5621 end Find_Interface_Tag;
5623 ---------------------------
5624 -- Find_Optional_Prim_Op --
5625 ---------------------------
5627 function Find_Optional_Prim_Op
5628 (T : Entity_Id; Name : Name_Id) return Entity_Id
5630 Prim : Elmt_Id;
5631 Typ : Entity_Id := T;
5632 Op : Entity_Id;
5634 begin
5635 if Is_Class_Wide_Type (Typ) then
5636 Typ := Root_Type (Typ);
5637 end if;
5639 Typ := Underlying_Type (Typ);
5641 -- Loop through primitive operations
5643 Prim := First_Elmt (Primitive_Operations (Typ));
5644 while Present (Prim) loop
5645 Op := Node (Prim);
5647 -- We can retrieve primitive operations by name if it is an internal
5648 -- name. For equality we must check that both of its operands have
5649 -- the same type, to avoid confusion with user-defined equalities
5650 -- than may have a non-symmetric signature.
5652 exit when Chars (Op) = Name
5653 and then
5654 (Name /= Name_Op_Eq
5655 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5657 Next_Elmt (Prim);
5658 end loop;
5660 return Node (Prim); -- Empty if not found
5661 end Find_Optional_Prim_Op;
5663 ---------------------------
5664 -- Find_Optional_Prim_Op --
5665 ---------------------------
5667 function Find_Optional_Prim_Op
5668 (T : Entity_Id;
5669 Name : TSS_Name_Type) return Entity_Id
5671 Inher_Op : Entity_Id := Empty;
5672 Own_Op : Entity_Id := Empty;
5673 Prim_Elmt : Elmt_Id;
5674 Prim_Id : Entity_Id;
5675 Typ : Entity_Id := T;
5677 begin
5678 if Is_Class_Wide_Type (Typ) then
5679 Typ := Root_Type (Typ);
5680 end if;
5682 Typ := Underlying_Type (Typ);
5684 -- This search is based on the assertion that the dispatching version
5685 -- of the TSS routine always precedes the real primitive.
5687 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5688 while Present (Prim_Elmt) loop
5689 Prim_Id := Node (Prim_Elmt);
5691 if Is_TSS (Prim_Id, Name) then
5692 if Present (Alias (Prim_Id)) then
5693 Inher_Op := Prim_Id;
5694 else
5695 Own_Op := Prim_Id;
5696 end if;
5697 end if;
5699 Next_Elmt (Prim_Elmt);
5700 end loop;
5702 if Present (Own_Op) then
5703 return Own_Op;
5704 elsif Present (Inher_Op) then
5705 return Inher_Op;
5706 else
5707 return Empty;
5708 end if;
5709 end Find_Optional_Prim_Op;
5711 ------------------
5712 -- Find_Prim_Op --
5713 ------------------
5715 function Find_Prim_Op
5716 (T : Entity_Id; Name : Name_Id) return Entity_Id
5718 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5719 begin
5720 if No (Result) then
5721 raise Program_Error;
5722 end if;
5724 return Result;
5725 end Find_Prim_Op;
5727 ------------------
5728 -- Find_Prim_Op --
5729 ------------------
5731 function Find_Prim_Op
5732 (T : Entity_Id;
5733 Name : TSS_Name_Type) return Entity_Id
5735 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5736 begin
5737 if No (Result) then
5738 raise Program_Error;
5739 end if;
5741 return Result;
5742 end Find_Prim_Op;
5744 ----------------------------
5745 -- Find_Protection_Object --
5746 ----------------------------
5748 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5749 S : Entity_Id;
5751 begin
5752 S := Scop;
5753 while Present (S) loop
5754 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5755 and then Present (Protection_Object (S))
5756 then
5757 return Protection_Object (S);
5758 end if;
5760 S := Scope (S);
5761 end loop;
5763 -- If we do not find a Protection object in the scope chain, then
5764 -- something has gone wrong, most likely the object was never created.
5766 raise Program_Error;
5767 end Find_Protection_Object;
5769 --------------------------
5770 -- Find_Protection_Type --
5771 --------------------------
5773 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5774 Comp : Entity_Id;
5775 Typ : Entity_Id := Conc_Typ;
5777 begin
5778 if Is_Concurrent_Type (Typ) then
5779 Typ := Corresponding_Record_Type (Typ);
5780 end if;
5782 -- Since restriction violations are not considered serious errors, the
5783 -- expander remains active, but may leave the corresponding record type
5784 -- malformed. In such cases, component _object is not available so do
5785 -- not look for it.
5787 if not Analyzed (Typ) then
5788 return Empty;
5789 end if;
5791 Comp := First_Component (Typ);
5792 while Present (Comp) loop
5793 if Chars (Comp) = Name_uObject then
5794 return Base_Type (Etype (Comp));
5795 end if;
5797 Next_Component (Comp);
5798 end loop;
5800 -- The corresponding record of a protected type should always have an
5801 -- _object field.
5803 raise Program_Error;
5804 end Find_Protection_Type;
5806 -----------------------
5807 -- Find_Hook_Context --
5808 -----------------------
5810 function Find_Hook_Context (N : Node_Id) return Node_Id is
5811 Par : Node_Id;
5812 Top : Node_Id;
5814 Wrapped_Node : Node_Id;
5815 -- Note: if we are in a transient scope, we want to reuse it as
5816 -- the context for actions insertion, if possible. But if N is itself
5817 -- part of the stored actions for the current transient scope,
5818 -- then we need to insert at the appropriate (inner) location in
5819 -- the not as an action on Node_To_Be_Wrapped.
5821 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5823 begin
5824 -- When the node is inside a case/if expression, the lifetime of any
5825 -- temporary controlled object is extended. Find a suitable insertion
5826 -- node by locating the topmost case or if expressions.
5828 if In_Cond_Expr then
5829 Par := N;
5830 Top := N;
5831 while Present (Par) loop
5832 if Nkind_In (Original_Node (Par), N_Case_Expression,
5833 N_If_Expression)
5834 then
5835 Top := Par;
5837 -- Prevent the search from going too far
5839 elsif Is_Body_Or_Package_Declaration (Par) then
5840 exit;
5841 end if;
5843 Par := Parent (Par);
5844 end loop;
5846 -- The topmost case or if expression is now recovered, but it may
5847 -- still not be the correct place to add generated code. Climb to
5848 -- find a parent that is part of a declarative or statement list,
5849 -- and is not a list of actuals in a call.
5851 Par := Top;
5852 while Present (Par) loop
5853 if Is_List_Member (Par)
5854 and then not Nkind_In (Par, N_Component_Association,
5855 N_Discriminant_Association,
5856 N_Parameter_Association,
5857 N_Pragma_Argument_Association)
5858 and then not Nkind_In (Parent (Par), N_Function_Call,
5859 N_Procedure_Call_Statement,
5860 N_Entry_Call_Statement)
5862 then
5863 return Par;
5865 -- Prevent the search from going too far
5867 elsif Is_Body_Or_Package_Declaration (Par) then
5868 exit;
5869 end if;
5871 Par := Parent (Par);
5872 end loop;
5874 return Par;
5876 else
5877 Par := N;
5878 while Present (Par) loop
5880 -- Keep climbing past various operators
5882 if Nkind (Parent (Par)) in N_Op
5883 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5884 then
5885 Par := Parent (Par);
5886 else
5887 exit;
5888 end if;
5889 end loop;
5891 Top := Par;
5893 -- The node may be located in a pragma in which case return the
5894 -- pragma itself:
5896 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5898 -- Similar case occurs when the node is related to an object
5899 -- declaration or assignment:
5901 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5903 -- Another case to consider is when the node is part of a return
5904 -- statement:
5906 -- return ... and then Ctrl_Func_Call ...;
5908 -- Another case is when the node acts as a formal in a procedure
5909 -- call statement:
5911 -- Proc (... and then Ctrl_Func_Call ...);
5913 if Scope_Is_Transient then
5914 Wrapped_Node := Node_To_Be_Wrapped;
5915 else
5916 Wrapped_Node := Empty;
5917 end if;
5919 while Present (Par) loop
5920 if Par = Wrapped_Node
5921 or else Nkind_In (Par, N_Assignment_Statement,
5922 N_Object_Declaration,
5923 N_Pragma,
5924 N_Procedure_Call_Statement,
5925 N_Simple_Return_Statement)
5926 then
5927 return Par;
5929 -- Prevent the search from going too far
5931 elsif Is_Body_Or_Package_Declaration (Par) then
5932 exit;
5933 end if;
5935 Par := Parent (Par);
5936 end loop;
5938 -- Return the topmost short circuit operator
5940 return Top;
5941 end if;
5942 end Find_Hook_Context;
5944 ------------------------------
5945 -- Following_Address_Clause --
5946 ------------------------------
5948 function Following_Address_Clause (D : Node_Id) return Node_Id is
5949 Id : constant Entity_Id := Defining_Identifier (D);
5950 Result : Node_Id;
5951 Par : Node_Id;
5953 function Check_Decls (D : Node_Id) return Node_Id;
5954 -- This internal function differs from the main function in that it
5955 -- gets called to deal with a following package private part, and
5956 -- it checks declarations starting with D (the main function checks
5957 -- declarations following D). If D is Empty, then Empty is returned.
5959 -----------------
5960 -- Check_Decls --
5961 -----------------
5963 function Check_Decls (D : Node_Id) return Node_Id is
5964 Decl : Node_Id;
5966 begin
5967 Decl := D;
5968 while Present (Decl) loop
5969 if Nkind (Decl) = N_At_Clause
5970 and then Chars (Identifier (Decl)) = Chars (Id)
5971 then
5972 return Decl;
5974 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5975 and then Chars (Decl) = Name_Address
5976 and then Chars (Name (Decl)) = Chars (Id)
5977 then
5978 return Decl;
5979 end if;
5981 Next (Decl);
5982 end loop;
5984 -- Otherwise not found, return Empty
5986 return Empty;
5987 end Check_Decls;
5989 -- Start of processing for Following_Address_Clause
5991 begin
5992 -- If parser detected no address clause for the identifier in question,
5993 -- then the answer is a quick NO, without the need for a search.
5995 if not Get_Name_Table_Boolean1 (Chars (Id)) then
5996 return Empty;
5997 end if;
5999 -- Otherwise search current declarative unit
6001 Result := Check_Decls (Next (D));
6003 if Present (Result) then
6004 return Result;
6005 end if;
6007 -- Check for possible package private part following
6009 Par := Parent (D);
6011 if Nkind (Par) = N_Package_Specification
6012 and then Visible_Declarations (Par) = List_Containing (D)
6013 and then Present (Private_Declarations (Par))
6014 then
6015 -- Private part present, check declarations there
6017 return Check_Decls (First (Private_Declarations (Par)));
6019 else
6020 -- No private part, clause not found, return Empty
6022 return Empty;
6023 end if;
6024 end Following_Address_Clause;
6026 ----------------------
6027 -- Force_Evaluation --
6028 ----------------------
6030 procedure Force_Evaluation
6031 (Exp : Node_Id;
6032 Name_Req : Boolean := False;
6033 Related_Id : Entity_Id := Empty;
6034 Is_Low_Bound : Boolean := False;
6035 Is_High_Bound : Boolean := False;
6036 Mode : Force_Evaluation_Mode := Relaxed)
6038 begin
6039 Remove_Side_Effects
6040 (Exp => Exp,
6041 Name_Req => Name_Req,
6042 Variable_Ref => True,
6043 Renaming_Req => False,
6044 Related_Id => Related_Id,
6045 Is_Low_Bound => Is_Low_Bound,
6046 Is_High_Bound => Is_High_Bound,
6047 Check_Side_Effects =>
6048 Is_Static_Expression (Exp)
6049 or else Mode = Relaxed);
6050 end Force_Evaluation;
6052 ---------------------------------
6053 -- Fully_Qualified_Name_String --
6054 ---------------------------------
6056 function Fully_Qualified_Name_String
6057 (E : Entity_Id;
6058 Append_NUL : Boolean := True) return String_Id
6060 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6061 -- Compute recursively the qualified name without NUL at the end, adding
6062 -- it to the currently started string being generated
6064 ----------------------------------
6065 -- Internal_Full_Qualified_Name --
6066 ----------------------------------
6068 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6069 Ent : Entity_Id;
6071 begin
6072 -- Deal properly with child units
6074 if Nkind (E) = N_Defining_Program_Unit_Name then
6075 Ent := Defining_Identifier (E);
6076 else
6077 Ent := E;
6078 end if;
6080 -- Compute qualification recursively (only "Standard" has no scope)
6082 if Present (Scope (Scope (Ent))) then
6083 Internal_Full_Qualified_Name (Scope (Ent));
6084 Store_String_Char (Get_Char_Code ('.'));
6085 end if;
6087 -- Every entity should have a name except some expanded blocks
6088 -- don't bother about those.
6090 if Chars (Ent) = No_Name then
6091 return;
6092 end if;
6094 -- Generates the entity name in upper case
6096 Get_Decoded_Name_String (Chars (Ent));
6097 Set_All_Upper_Case;
6098 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6099 return;
6100 end Internal_Full_Qualified_Name;
6102 -- Start of processing for Full_Qualified_Name
6104 begin
6105 Start_String;
6106 Internal_Full_Qualified_Name (E);
6108 if Append_NUL then
6109 Store_String_Char (Get_Char_Code (ASCII.NUL));
6110 end if;
6112 return End_String;
6113 end Fully_Qualified_Name_String;
6115 ------------------------
6116 -- Generate_Poll_Call --
6117 ------------------------
6119 procedure Generate_Poll_Call (N : Node_Id) is
6120 begin
6121 -- No poll call if polling not active
6123 if not Polling_Required then
6124 return;
6126 -- Otherwise generate require poll call
6128 else
6129 Insert_Before_And_Analyze (N,
6130 Make_Procedure_Call_Statement (Sloc (N),
6131 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6132 end if;
6133 end Generate_Poll_Call;
6135 ---------------------------------
6136 -- Get_Current_Value_Condition --
6137 ---------------------------------
6139 -- Note: the implementation of this procedure is very closely tied to the
6140 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6141 -- interpret Current_Value fields set by the Set procedure, so the two
6142 -- procedures need to be closely coordinated.
6144 procedure Get_Current_Value_Condition
6145 (Var : Node_Id;
6146 Op : out Node_Kind;
6147 Val : out Node_Id)
6149 Loc : constant Source_Ptr := Sloc (Var);
6150 Ent : constant Entity_Id := Entity (Var);
6152 procedure Process_Current_Value_Condition
6153 (N : Node_Id;
6154 S : Boolean);
6155 -- N is an expression which holds either True (S = True) or False (S =
6156 -- False) in the condition. This procedure digs out the expression and
6157 -- if it refers to Ent, sets Op and Val appropriately.
6159 -------------------------------------
6160 -- Process_Current_Value_Condition --
6161 -------------------------------------
6163 procedure Process_Current_Value_Condition
6164 (N : Node_Id;
6165 S : Boolean)
6167 Cond : Node_Id;
6168 Prev_Cond : Node_Id;
6169 Sens : Boolean;
6171 begin
6172 Cond := N;
6173 Sens := S;
6175 loop
6176 Prev_Cond := Cond;
6178 -- Deal with NOT operators, inverting sense
6180 while Nkind (Cond) = N_Op_Not loop
6181 Cond := Right_Opnd (Cond);
6182 Sens := not Sens;
6183 end loop;
6185 -- Deal with conversions, qualifications, and expressions with
6186 -- actions.
6188 while Nkind_In (Cond,
6189 N_Type_Conversion,
6190 N_Qualified_Expression,
6191 N_Expression_With_Actions)
6192 loop
6193 Cond := Expression (Cond);
6194 end loop;
6196 exit when Cond = Prev_Cond;
6197 end loop;
6199 -- Deal with AND THEN and AND cases
6201 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6203 -- Don't ever try to invert a condition that is of the form of an
6204 -- AND or AND THEN (since we are not doing sufficiently general
6205 -- processing to allow this).
6207 if Sens = False then
6208 Op := N_Empty;
6209 Val := Empty;
6210 return;
6211 end if;
6213 -- Recursively process AND and AND THEN branches
6215 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6217 if Op /= N_Empty then
6218 return;
6219 end if;
6221 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6222 return;
6224 -- Case of relational operator
6226 elsif Nkind (Cond) in N_Op_Compare then
6227 Op := Nkind (Cond);
6229 -- Invert sense of test if inverted test
6231 if Sens = False then
6232 case Op is
6233 when N_Op_Eq => Op := N_Op_Ne;
6234 when N_Op_Ne => Op := N_Op_Eq;
6235 when N_Op_Lt => Op := N_Op_Ge;
6236 when N_Op_Gt => Op := N_Op_Le;
6237 when N_Op_Le => Op := N_Op_Gt;
6238 when N_Op_Ge => Op := N_Op_Lt;
6239 when others => raise Program_Error;
6240 end case;
6241 end if;
6243 -- Case of entity op value
6245 if Is_Entity_Name (Left_Opnd (Cond))
6246 and then Ent = Entity (Left_Opnd (Cond))
6247 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6248 then
6249 Val := Right_Opnd (Cond);
6251 -- Case of value op entity
6253 elsif Is_Entity_Name (Right_Opnd (Cond))
6254 and then Ent = Entity (Right_Opnd (Cond))
6255 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6256 then
6257 Val := Left_Opnd (Cond);
6259 -- We are effectively swapping operands
6261 case Op is
6262 when N_Op_Eq => null;
6263 when N_Op_Ne => null;
6264 when N_Op_Lt => Op := N_Op_Gt;
6265 when N_Op_Gt => Op := N_Op_Lt;
6266 when N_Op_Le => Op := N_Op_Ge;
6267 when N_Op_Ge => Op := N_Op_Le;
6268 when others => raise Program_Error;
6269 end case;
6271 else
6272 Op := N_Empty;
6273 end if;
6275 return;
6277 elsif Nkind_In (Cond,
6278 N_Type_Conversion,
6279 N_Qualified_Expression,
6280 N_Expression_With_Actions)
6281 then
6282 Cond := Expression (Cond);
6284 -- Case of Boolean variable reference, return as though the
6285 -- reference had said var = True.
6287 else
6288 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6289 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6291 if Sens = False then
6292 Op := N_Op_Ne;
6293 else
6294 Op := N_Op_Eq;
6295 end if;
6296 end if;
6297 end if;
6298 end Process_Current_Value_Condition;
6300 -- Start of processing for Get_Current_Value_Condition
6302 begin
6303 Op := N_Empty;
6304 Val := Empty;
6306 -- Immediate return, nothing doing, if this is not an object
6308 if Ekind (Ent) not in Object_Kind then
6309 return;
6310 end if;
6312 -- Otherwise examine current value
6314 declare
6315 CV : constant Node_Id := Current_Value (Ent);
6316 Sens : Boolean;
6317 Stm : Node_Id;
6319 begin
6320 -- If statement. Condition is known true in THEN section, known False
6321 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6323 if Nkind (CV) = N_If_Statement then
6325 -- Before start of IF statement
6327 if Loc < Sloc (CV) then
6328 return;
6330 -- After end of IF statement
6332 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6333 return;
6334 end if;
6336 -- At this stage we know that we are within the IF statement, but
6337 -- unfortunately, the tree does not record the SLOC of the ELSE so
6338 -- we cannot use a simple SLOC comparison to distinguish between
6339 -- the then/else statements, so we have to climb the tree.
6341 declare
6342 N : Node_Id;
6344 begin
6345 N := Parent (Var);
6346 while Parent (N) /= CV loop
6347 N := Parent (N);
6349 -- If we fall off the top of the tree, then that's odd, but
6350 -- perhaps it could occur in some error situation, and the
6351 -- safest response is simply to assume that the outcome of
6352 -- the condition is unknown. No point in bombing during an
6353 -- attempt to optimize things.
6355 if No (N) then
6356 return;
6357 end if;
6358 end loop;
6360 -- Now we have N pointing to a node whose parent is the IF
6361 -- statement in question, so now we can tell if we are within
6362 -- the THEN statements.
6364 if Is_List_Member (N)
6365 and then List_Containing (N) = Then_Statements (CV)
6366 then
6367 Sens := True;
6369 -- If the variable reference does not come from source, we
6370 -- cannot reliably tell whether it appears in the else part.
6371 -- In particular, if it appears in generated code for a node
6372 -- that requires finalization, it may be attached to a list
6373 -- that has not been yet inserted into the code. For now,
6374 -- treat it as unknown.
6376 elsif not Comes_From_Source (N) then
6377 return;
6379 -- Otherwise we must be in ELSIF or ELSE part
6381 else
6382 Sens := False;
6383 end if;
6384 end;
6386 -- ELSIF part. Condition is known true within the referenced
6387 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6388 -- and unknown before the ELSE part or after the IF statement.
6390 elsif Nkind (CV) = N_Elsif_Part then
6392 -- if the Elsif_Part had condition_actions, the elsif has been
6393 -- rewritten as a nested if, and the original elsif_part is
6394 -- detached from the tree, so there is no way to obtain useful
6395 -- information on the current value of the variable.
6396 -- Can this be improved ???
6398 if No (Parent (CV)) then
6399 return;
6400 end if;
6402 Stm := Parent (CV);
6404 -- If the tree has been otherwise rewritten there is nothing
6405 -- else to be done either.
6407 if Nkind (Stm) /= N_If_Statement then
6408 return;
6409 end if;
6411 -- Before start of ELSIF part
6413 if Loc < Sloc (CV) then
6414 return;
6416 -- After end of IF statement
6418 elsif Loc >= Sloc (Stm) +
6419 Text_Ptr (UI_To_Int (End_Span (Stm)))
6420 then
6421 return;
6422 end if;
6424 -- Again we lack the SLOC of the ELSE, so we need to climb the
6425 -- tree to see if we are within the ELSIF part in question.
6427 declare
6428 N : Node_Id;
6430 begin
6431 N := Parent (Var);
6432 while Parent (N) /= Stm loop
6433 N := Parent (N);
6435 -- If we fall off the top of the tree, then that's odd, but
6436 -- perhaps it could occur in some error situation, and the
6437 -- safest response is simply to assume that the outcome of
6438 -- the condition is unknown. No point in bombing during an
6439 -- attempt to optimize things.
6441 if No (N) then
6442 return;
6443 end if;
6444 end loop;
6446 -- Now we have N pointing to a node whose parent is the IF
6447 -- statement in question, so see if is the ELSIF part we want.
6448 -- the THEN statements.
6450 if N = CV then
6451 Sens := True;
6453 -- Otherwise we must be in subsequent ELSIF or ELSE part
6455 else
6456 Sens := False;
6457 end if;
6458 end;
6460 -- Iteration scheme of while loop. The condition is known to be
6461 -- true within the body of the loop.
6463 elsif Nkind (CV) = N_Iteration_Scheme then
6464 declare
6465 Loop_Stmt : constant Node_Id := Parent (CV);
6467 begin
6468 -- Before start of body of loop
6470 if Loc < Sloc (Loop_Stmt) then
6471 return;
6473 -- After end of LOOP statement
6475 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6476 return;
6478 -- We are within the body of the loop
6480 else
6481 Sens := True;
6482 end if;
6483 end;
6485 -- All other cases of Current_Value settings
6487 else
6488 return;
6489 end if;
6491 -- If we fall through here, then we have a reportable condition, Sens
6492 -- is True if the condition is true and False if it needs inverting.
6494 Process_Current_Value_Condition (Condition (CV), Sens);
6495 end;
6496 end Get_Current_Value_Condition;
6498 ---------------------
6499 -- Get_Stream_Size --
6500 ---------------------
6502 function Get_Stream_Size (E : Entity_Id) return Uint is
6503 begin
6504 -- If we have a Stream_Size clause for this type use it
6506 if Has_Stream_Size_Clause (E) then
6507 return Static_Integer (Expression (Stream_Size_Clause (E)));
6509 -- Otherwise the Stream_Size if the size of the type
6511 else
6512 return Esize (E);
6513 end if;
6514 end Get_Stream_Size;
6516 ---------------------------
6517 -- Has_Access_Constraint --
6518 ---------------------------
6520 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6521 Disc : Entity_Id;
6522 T : constant Entity_Id := Etype (E);
6524 begin
6525 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6526 Disc := First_Discriminant (T);
6527 while Present (Disc) loop
6528 if Is_Access_Type (Etype (Disc)) then
6529 return True;
6530 end if;
6532 Next_Discriminant (Disc);
6533 end loop;
6535 return False;
6536 else
6537 return False;
6538 end if;
6539 end Has_Access_Constraint;
6541 -----------------------------------------------------
6542 -- Has_Annotate_Pragma_For_External_Axiomatization --
6543 -----------------------------------------------------
6545 function Has_Annotate_Pragma_For_External_Axiomatization
6546 (E : Entity_Id) return Boolean
6548 function Is_Annotate_Pragma_For_External_Axiomatization
6549 (N : Node_Id) return Boolean;
6550 -- Returns whether N is
6551 -- pragma Annotate (GNATprove, External_Axiomatization);
6553 ----------------------------------------------------
6554 -- Is_Annotate_Pragma_For_External_Axiomatization --
6555 ----------------------------------------------------
6557 -- The general form of pragma Annotate is
6559 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6560 -- ARG ::= NAME | EXPRESSION
6562 -- The first two arguments are by convention intended to refer to an
6563 -- external tool and a tool-specific function. These arguments are
6564 -- not analyzed.
6566 -- The following is used to annotate a package specification which
6567 -- GNATprove should treat specially, because the axiomatization of
6568 -- this unit is given by the user instead of being automatically
6569 -- generated.
6571 -- pragma Annotate (GNATprove, External_Axiomatization);
6573 function Is_Annotate_Pragma_For_External_Axiomatization
6574 (N : Node_Id) return Boolean
6576 Name_GNATprove : constant String :=
6577 "gnatprove";
6578 Name_External_Axiomatization : constant String :=
6579 "external_axiomatization";
6580 -- Special names
6582 begin
6583 if Nkind (N) = N_Pragma
6584 and then Get_Pragma_Id (N) = Pragma_Annotate
6585 and then List_Length (Pragma_Argument_Associations (N)) = 2
6586 then
6587 declare
6588 Arg1 : constant Node_Id :=
6589 First (Pragma_Argument_Associations (N));
6590 Arg2 : constant Node_Id := Next (Arg1);
6591 Nam1 : Name_Id;
6592 Nam2 : Name_Id;
6594 begin
6595 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6596 -- Name_External_Axiomatization so that Name_Find returns the
6597 -- corresponding name. This takes care of all possible casings.
6599 Name_Len := 0;
6600 Add_Str_To_Name_Buffer (Name_GNATprove);
6601 Nam1 := Name_Find;
6603 Name_Len := 0;
6604 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6605 Nam2 := Name_Find;
6607 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6608 and then
6609 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6610 end;
6612 else
6613 return False;
6614 end if;
6615 end Is_Annotate_Pragma_For_External_Axiomatization;
6617 -- Local variables
6619 Decl : Node_Id;
6620 Vis_Decls : List_Id;
6621 N : Node_Id;
6623 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6625 begin
6626 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6627 Decl := Parent (Parent (E));
6628 else
6629 Decl := Parent (E);
6630 end if;
6632 Vis_Decls := Visible_Declarations (Decl);
6634 N := First (Vis_Decls);
6635 while Present (N) loop
6637 -- Skip declarations generated by the frontend. Skip all pragmas
6638 -- that are not the desired Annotate pragma. Stop the search on
6639 -- the first non-pragma source declaration.
6641 if Comes_From_Source (N) then
6642 if Nkind (N) = N_Pragma then
6643 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6644 return True;
6645 end if;
6646 else
6647 return False;
6648 end if;
6649 end if;
6651 Next (N);
6652 end loop;
6654 return False;
6655 end Has_Annotate_Pragma_For_External_Axiomatization;
6657 --------------------
6658 -- Homonym_Number --
6659 --------------------
6661 function Homonym_Number (Subp : Entity_Id) return Nat is
6662 Count : Nat;
6663 Hom : Entity_Id;
6665 begin
6666 Count := 1;
6667 Hom := Homonym (Subp);
6668 while Present (Hom) loop
6669 if Scope (Hom) = Scope (Subp) then
6670 Count := Count + 1;
6671 end if;
6673 Hom := Homonym (Hom);
6674 end loop;
6676 return Count;
6677 end Homonym_Number;
6679 -----------------------------------
6680 -- In_Library_Level_Package_Body --
6681 -----------------------------------
6683 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6684 begin
6685 -- First determine whether the entity appears at the library level, then
6686 -- look at the containing unit.
6688 if Is_Library_Level_Entity (Id) then
6689 declare
6690 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6692 begin
6693 return Nkind (Unit (Container)) = N_Package_Body;
6694 end;
6695 end if;
6697 return False;
6698 end In_Library_Level_Package_Body;
6700 ------------------------------
6701 -- In_Unconditional_Context --
6702 ------------------------------
6704 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6705 P : Node_Id;
6707 begin
6708 P := Node;
6709 while Present (P) loop
6710 case Nkind (P) is
6711 when N_Subprogram_Body => return True;
6712 when N_If_Statement => return False;
6713 when N_Loop_Statement => return False;
6714 when N_Case_Statement => return False;
6715 when others => P := Parent (P);
6716 end case;
6717 end loop;
6719 return False;
6720 end In_Unconditional_Context;
6722 -------------------
6723 -- Insert_Action --
6724 -------------------
6726 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6727 begin
6728 if Present (Ins_Action) then
6729 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6730 end if;
6731 end Insert_Action;
6733 -- Version with check(s) suppressed
6735 procedure Insert_Action
6736 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6738 begin
6739 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6740 end Insert_Action;
6742 -------------------------
6743 -- Insert_Action_After --
6744 -------------------------
6746 procedure Insert_Action_After
6747 (Assoc_Node : Node_Id;
6748 Ins_Action : Node_Id)
6750 begin
6751 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6752 end Insert_Action_After;
6754 --------------------
6755 -- Insert_Actions --
6756 --------------------
6758 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6759 N : Node_Id;
6760 P : Node_Id;
6762 Wrapped_Node : Node_Id := Empty;
6764 begin
6765 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6766 return;
6767 end if;
6769 -- Ignore insert of actions from inside default expression (or other
6770 -- similar "spec expression") in the special spec-expression analyze
6771 -- mode. Any insertions at this point have no relevance, since we are
6772 -- only doing the analyze to freeze the types of any static expressions.
6773 -- See section "Handling of Default Expressions" in the spec of package
6774 -- Sem for further details.
6776 if In_Spec_Expression then
6777 return;
6778 end if;
6780 -- If the action derives from stuff inside a record, then the actions
6781 -- are attached to the current scope, to be inserted and analyzed on
6782 -- exit from the scope. The reason for this is that we may also be
6783 -- generating freeze actions at the same time, and they must eventually
6784 -- be elaborated in the correct order.
6786 if Is_Record_Type (Current_Scope)
6787 and then not Is_Frozen (Current_Scope)
6788 then
6789 if No (Scope_Stack.Table
6790 (Scope_Stack.Last).Pending_Freeze_Actions)
6791 then
6792 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6793 Ins_Actions;
6794 else
6795 Append_List
6796 (Ins_Actions,
6797 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6798 end if;
6800 return;
6801 end if;
6803 -- We now intend to climb up the tree to find the right point to
6804 -- insert the actions. We start at Assoc_Node, unless this node is a
6805 -- subexpression in which case we start with its parent. We do this for
6806 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6807 -- itself one of the special nodes like N_And_Then, then we assume that
6808 -- an initial request to insert actions for such a node does not expect
6809 -- the actions to get deposited in the node for later handling when the
6810 -- node is expanded, since clearly the node is being dealt with by the
6811 -- caller. Note that in the subexpression case, N is always the child we
6812 -- came from.
6814 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6815 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6816 -- Procedure calls, and similarly procedure attribute references, are
6817 -- also statements.
6819 if Nkind (Assoc_Node) in N_Subexpr
6820 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6821 or else Etype (Assoc_Node) /= Standard_Void_Type)
6822 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6823 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6824 or else not Is_Procedure_Attribute_Name
6825 (Attribute_Name (Assoc_Node)))
6826 then
6827 N := Assoc_Node;
6828 P := Parent (Assoc_Node);
6830 -- Non-subexpression case. Note that N is initially Empty in this case
6831 -- (N is only guaranteed Non-Empty in the subexpr case).
6833 else
6834 N := Empty;
6835 P := Assoc_Node;
6836 end if;
6838 -- Capture root of the transient scope
6840 if Scope_Is_Transient then
6841 Wrapped_Node := Node_To_Be_Wrapped;
6842 end if;
6844 loop
6845 pragma Assert (Present (P));
6847 -- Make sure that inserted actions stay in the transient scope
6849 if Present (Wrapped_Node) and then N = Wrapped_Node then
6850 Store_Before_Actions_In_Scope (Ins_Actions);
6851 return;
6852 end if;
6854 case Nkind (P) is
6856 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6857 -- in the Actions field of the right operand. They will be moved
6858 -- out further when the AND THEN or OR ELSE operator is expanded.
6859 -- Nothing special needs to be done for the left operand since
6860 -- in that case the actions are executed unconditionally.
6862 when N_Short_Circuit =>
6863 if N = Right_Opnd (P) then
6865 -- We are now going to either append the actions to the
6866 -- actions field of the short-circuit operation. We will
6867 -- also analyze the actions now.
6869 -- This analysis is really too early, the proper thing would
6870 -- be to just park them there now, and only analyze them if
6871 -- we find we really need them, and to it at the proper
6872 -- final insertion point. However attempting to this proved
6873 -- tricky, so for now we just kill current values before and
6874 -- after the analyze call to make sure we avoid peculiar
6875 -- optimizations from this out of order insertion.
6877 Kill_Current_Values;
6879 -- If P has already been expanded, we can't park new actions
6880 -- on it, so we need to expand them immediately, introducing
6881 -- an Expression_With_Actions. N can't be an expression
6882 -- with actions, or else then the actions would have been
6883 -- inserted at an inner level.
6885 if Analyzed (P) then
6886 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6887 Rewrite (N,
6888 Make_Expression_With_Actions (Sloc (N),
6889 Actions => Ins_Actions,
6890 Expression => Relocate_Node (N)));
6891 Analyze_And_Resolve (N);
6893 elsif Present (Actions (P)) then
6894 Insert_List_After_And_Analyze
6895 (Last (Actions (P)), Ins_Actions);
6896 else
6897 Set_Actions (P, Ins_Actions);
6898 Analyze_List (Actions (P));
6899 end if;
6901 Kill_Current_Values;
6903 return;
6904 end if;
6906 -- Then or Else dependent expression of an if expression. Add
6907 -- actions to Then_Actions or Else_Actions field as appropriate.
6908 -- The actions will be moved further out when the if is expanded.
6910 when N_If_Expression =>
6911 declare
6912 ThenX : constant Node_Id := Next (First (Expressions (P)));
6913 ElseX : constant Node_Id := Next (ThenX);
6915 begin
6916 -- If the enclosing expression is already analyzed, as
6917 -- is the case for nested elaboration checks, insert the
6918 -- conditional further out.
6920 if Analyzed (P) then
6921 null;
6923 -- Actions belong to the then expression, temporarily place
6924 -- them as Then_Actions of the if expression. They will be
6925 -- moved to the proper place later when the if expression
6926 -- is expanded.
6928 elsif N = ThenX then
6929 if Present (Then_Actions (P)) then
6930 Insert_List_After_And_Analyze
6931 (Last (Then_Actions (P)), Ins_Actions);
6932 else
6933 Set_Then_Actions (P, Ins_Actions);
6934 Analyze_List (Then_Actions (P));
6935 end if;
6937 return;
6939 -- Actions belong to the else expression, temporarily place
6940 -- them as Else_Actions of the if expression. They will be
6941 -- moved to the proper place later when the if expression
6942 -- is expanded.
6944 elsif N = ElseX then
6945 if Present (Else_Actions (P)) then
6946 Insert_List_After_And_Analyze
6947 (Last (Else_Actions (P)), Ins_Actions);
6948 else
6949 Set_Else_Actions (P, Ins_Actions);
6950 Analyze_List (Else_Actions (P));
6951 end if;
6953 return;
6955 -- Actions belong to the condition. In this case they are
6956 -- unconditionally executed, and so we can continue the
6957 -- search for the proper insert point.
6959 else
6960 null;
6961 end if;
6962 end;
6964 -- Alternative of case expression, we place the action in the
6965 -- Actions field of the case expression alternative, this will
6966 -- be handled when the case expression is expanded.
6968 when N_Case_Expression_Alternative =>
6969 if Present (Actions (P)) then
6970 Insert_List_After_And_Analyze
6971 (Last (Actions (P)), Ins_Actions);
6972 else
6973 Set_Actions (P, Ins_Actions);
6974 Analyze_List (Actions (P));
6975 end if;
6977 return;
6979 -- Case of appearing within an Expressions_With_Actions node. When
6980 -- the new actions come from the expression of the expression with
6981 -- actions, they must be added to the existing actions. The other
6982 -- alternative is when the new actions are related to one of the
6983 -- existing actions of the expression with actions, and should
6984 -- never reach here: if actions are inserted on a statement
6985 -- within the Actions of an expression with actions, or on some
6986 -- subexpression of such a statement, then the outermost proper
6987 -- insertion point is right before the statement, and we should
6988 -- never climb up as far as the N_Expression_With_Actions itself.
6990 when N_Expression_With_Actions =>
6991 if N = Expression (P) then
6992 if Is_Empty_List (Actions (P)) then
6993 Append_List_To (Actions (P), Ins_Actions);
6994 Analyze_List (Actions (P));
6995 else
6996 Insert_List_After_And_Analyze
6997 (Last (Actions (P)), Ins_Actions);
6998 end if;
7000 return;
7002 else
7003 raise Program_Error;
7004 end if;
7006 -- Case of appearing in the condition of a while expression or
7007 -- elsif. We insert the actions into the Condition_Actions field.
7008 -- They will be moved further out when the while loop or elsif
7009 -- is analyzed.
7011 when N_Elsif_Part
7012 | N_Iteration_Scheme
7014 if N = Condition (P) then
7015 if Present (Condition_Actions (P)) then
7016 Insert_List_After_And_Analyze
7017 (Last (Condition_Actions (P)), Ins_Actions);
7018 else
7019 Set_Condition_Actions (P, Ins_Actions);
7021 -- Set the parent of the insert actions explicitly. This
7022 -- is not a syntactic field, but we need the parent field
7023 -- set, in particular so that freeze can understand that
7024 -- it is dealing with condition actions, and properly
7025 -- insert the freezing actions.
7027 Set_Parent (Ins_Actions, P);
7028 Analyze_List (Condition_Actions (P));
7029 end if;
7031 return;
7032 end if;
7034 -- Statements, declarations, pragmas, representation clauses
7036 when
7037 -- Statements
7039 N_Procedure_Call_Statement
7040 | N_Statement_Other_Than_Procedure_Call
7042 -- Pragmas
7044 | N_Pragma
7046 -- Representation_Clause
7048 | N_At_Clause
7049 | N_Attribute_Definition_Clause
7050 | N_Enumeration_Representation_Clause
7051 | N_Record_Representation_Clause
7053 -- Declarations
7055 | N_Abstract_Subprogram_Declaration
7056 | N_Entry_Body
7057 | N_Exception_Declaration
7058 | N_Exception_Renaming_Declaration
7059 | N_Expression_Function
7060 | N_Formal_Abstract_Subprogram_Declaration
7061 | N_Formal_Concrete_Subprogram_Declaration
7062 | N_Formal_Object_Declaration
7063 | N_Formal_Type_Declaration
7064 | N_Full_Type_Declaration
7065 | N_Function_Instantiation
7066 | N_Generic_Function_Renaming_Declaration
7067 | N_Generic_Package_Declaration
7068 | N_Generic_Package_Renaming_Declaration
7069 | N_Generic_Procedure_Renaming_Declaration
7070 | N_Generic_Subprogram_Declaration
7071 | N_Implicit_Label_Declaration
7072 | N_Incomplete_Type_Declaration
7073 | N_Number_Declaration
7074 | N_Object_Declaration
7075 | N_Object_Renaming_Declaration
7076 | N_Package_Body
7077 | N_Package_Body_Stub
7078 | N_Package_Declaration
7079 | N_Package_Instantiation
7080 | N_Package_Renaming_Declaration
7081 | N_Private_Extension_Declaration
7082 | N_Private_Type_Declaration
7083 | N_Procedure_Instantiation
7084 | N_Protected_Body
7085 | N_Protected_Body_Stub
7086 | N_Protected_Type_Declaration
7087 | N_Single_Task_Declaration
7088 | N_Subprogram_Body
7089 | N_Subprogram_Body_Stub
7090 | N_Subprogram_Declaration
7091 | N_Subprogram_Renaming_Declaration
7092 | N_Subtype_Declaration
7093 | N_Task_Body
7094 | N_Task_Body_Stub
7095 | N_Task_Type_Declaration
7097 -- Use clauses can appear in lists of declarations
7099 | N_Use_Package_Clause
7100 | N_Use_Type_Clause
7102 -- Freeze entity behaves like a declaration or statement
7104 | N_Freeze_Entity
7105 | N_Freeze_Generic_Entity
7107 -- Do not insert here if the item is not a list member (this
7108 -- happens for example with a triggering statement, and the
7109 -- proper approach is to insert before the entire select).
7111 if not Is_List_Member (P) then
7112 null;
7114 -- Do not insert if parent of P is an N_Component_Association
7115 -- node (i.e. we are in the context of an N_Aggregate or
7116 -- N_Extension_Aggregate node. In this case we want to insert
7117 -- before the entire aggregate.
7119 elsif Nkind (Parent (P)) = N_Component_Association then
7120 null;
7122 -- Do not insert if the parent of P is either an N_Variant node
7123 -- or an N_Record_Definition node, meaning in either case that
7124 -- P is a member of a component list, and that therefore the
7125 -- actions should be inserted outside the complete record
7126 -- declaration.
7128 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7129 null;
7131 -- Do not insert freeze nodes within the loop generated for
7132 -- an aggregate, because they may be elaborated too late for
7133 -- subsequent use in the back end: within a package spec the
7134 -- loop is part of the elaboration procedure and is only
7135 -- elaborated during the second pass.
7137 -- If the loop comes from source, or the entity is local to the
7138 -- loop itself it must remain within.
7140 elsif Nkind (Parent (P)) = N_Loop_Statement
7141 and then not Comes_From_Source (Parent (P))
7142 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7143 and then
7144 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7145 then
7146 null;
7148 -- Otherwise we can go ahead and do the insertion
7150 elsif P = Wrapped_Node then
7151 Store_Before_Actions_In_Scope (Ins_Actions);
7152 return;
7154 else
7155 Insert_List_Before_And_Analyze (P, Ins_Actions);
7156 return;
7157 end if;
7159 -- A special case, N_Raise_xxx_Error can act either as a statement
7160 -- or a subexpression. We tell the difference by looking at the
7161 -- Etype. It is set to Standard_Void_Type in the statement case.
7163 when N_Raise_xxx_Error =>
7164 if Etype (P) = Standard_Void_Type then
7165 if P = Wrapped_Node then
7166 Store_Before_Actions_In_Scope (Ins_Actions);
7167 else
7168 Insert_List_Before_And_Analyze (P, Ins_Actions);
7169 end if;
7171 return;
7173 -- In the subexpression case, keep climbing
7175 else
7176 null;
7177 end if;
7179 -- If a component association appears within a loop created for
7180 -- an array aggregate, attach the actions to the association so
7181 -- they can be subsequently inserted within the loop. For other
7182 -- component associations insert outside of the aggregate. For
7183 -- an association that will generate a loop, its Loop_Actions
7184 -- attribute is already initialized (see exp_aggr.adb).
7186 -- The list of Loop_Actions can in turn generate additional ones,
7187 -- that are inserted before the associated node. If the associated
7188 -- node is outside the aggregate, the new actions are collected
7189 -- at the end of the Loop_Actions, to respect the order in which
7190 -- they are to be elaborated.
7192 when N_Component_Association
7193 | N_Iterated_Component_Association
7195 if Nkind (Parent (P)) = N_Aggregate
7196 and then Present (Loop_Actions (P))
7197 then
7198 if Is_Empty_List (Loop_Actions (P)) then
7199 Set_Loop_Actions (P, Ins_Actions);
7200 Analyze_List (Ins_Actions);
7201 else
7202 declare
7203 Decl : Node_Id;
7205 begin
7206 -- Check whether these actions were generated by a
7207 -- declaration that is part of the Loop_Actions for
7208 -- the component_association.
7210 Decl := Assoc_Node;
7211 while Present (Decl) loop
7212 exit when Parent (Decl) = P
7213 and then Is_List_Member (Decl)
7214 and then
7215 List_Containing (Decl) = Loop_Actions (P);
7216 Decl := Parent (Decl);
7217 end loop;
7219 if Present (Decl) then
7220 Insert_List_Before_And_Analyze
7221 (Decl, Ins_Actions);
7222 else
7223 Insert_List_After_And_Analyze
7224 (Last (Loop_Actions (P)), Ins_Actions);
7225 end if;
7226 end;
7227 end if;
7229 return;
7231 else
7232 null;
7233 end if;
7235 -- Another special case, an attribute denoting a procedure call
7237 when N_Attribute_Reference =>
7238 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7239 if P = Wrapped_Node then
7240 Store_Before_Actions_In_Scope (Ins_Actions);
7241 else
7242 Insert_List_Before_And_Analyze (P, Ins_Actions);
7243 end if;
7245 return;
7247 -- In the subexpression case, keep climbing
7249 else
7250 null;
7251 end if;
7253 -- A contract node should not belong to the tree
7255 when N_Contract =>
7256 raise Program_Error;
7258 -- For all other node types, keep climbing tree
7260 when N_Abortable_Part
7261 | N_Accept_Alternative
7262 | N_Access_Definition
7263 | N_Access_Function_Definition
7264 | N_Access_Procedure_Definition
7265 | N_Access_To_Object_Definition
7266 | N_Aggregate
7267 | N_Allocator
7268 | N_Aspect_Specification
7269 | N_Case_Expression
7270 | N_Case_Statement_Alternative
7271 | N_Character_Literal
7272 | N_Compilation_Unit
7273 | N_Compilation_Unit_Aux
7274 | N_Component_Clause
7275 | N_Component_Declaration
7276 | N_Component_Definition
7277 | N_Component_List
7278 | N_Constrained_Array_Definition
7279 | N_Decimal_Fixed_Point_Definition
7280 | N_Defining_Character_Literal
7281 | N_Defining_Identifier
7282 | N_Defining_Operator_Symbol
7283 | N_Defining_Program_Unit_Name
7284 | N_Delay_Alternative
7285 | N_Delta_Aggregate
7286 | N_Delta_Constraint
7287 | N_Derived_Type_Definition
7288 | N_Designator
7289 | N_Digits_Constraint
7290 | N_Discriminant_Association
7291 | N_Discriminant_Specification
7292 | N_Empty
7293 | N_Entry_Body_Formal_Part
7294 | N_Entry_Call_Alternative
7295 | N_Entry_Declaration
7296 | N_Entry_Index_Specification
7297 | N_Enumeration_Type_Definition
7298 | N_Error
7299 | N_Exception_Handler
7300 | N_Expanded_Name
7301 | N_Explicit_Dereference
7302 | N_Extension_Aggregate
7303 | N_Floating_Point_Definition
7304 | N_Formal_Decimal_Fixed_Point_Definition
7305 | N_Formal_Derived_Type_Definition
7306 | N_Formal_Discrete_Type_Definition
7307 | N_Formal_Floating_Point_Definition
7308 | N_Formal_Modular_Type_Definition
7309 | N_Formal_Ordinary_Fixed_Point_Definition
7310 | N_Formal_Package_Declaration
7311 | N_Formal_Private_Type_Definition
7312 | N_Formal_Incomplete_Type_Definition
7313 | N_Formal_Signed_Integer_Type_Definition
7314 | N_Function_Call
7315 | N_Function_Specification
7316 | N_Generic_Association
7317 | N_Handled_Sequence_Of_Statements
7318 | N_Identifier
7319 | N_In
7320 | N_Index_Or_Discriminant_Constraint
7321 | N_Indexed_Component
7322 | N_Integer_Literal
7323 | N_Iterator_Specification
7324 | N_Itype_Reference
7325 | N_Label
7326 | N_Loop_Parameter_Specification
7327 | N_Mod_Clause
7328 | N_Modular_Type_Definition
7329 | N_Not_In
7330 | N_Null
7331 | N_Op_Abs
7332 | N_Op_Add
7333 | N_Op_And
7334 | N_Op_Concat
7335 | N_Op_Divide
7336 | N_Op_Eq
7337 | N_Op_Expon
7338 | N_Op_Ge
7339 | N_Op_Gt
7340 | N_Op_Le
7341 | N_Op_Lt
7342 | N_Op_Minus
7343 | N_Op_Mod
7344 | N_Op_Multiply
7345 | N_Op_Ne
7346 | N_Op_Not
7347 | N_Op_Or
7348 | N_Op_Plus
7349 | N_Op_Rem
7350 | N_Op_Rotate_Left
7351 | N_Op_Rotate_Right
7352 | N_Op_Shift_Left
7353 | N_Op_Shift_Right
7354 | N_Op_Shift_Right_Arithmetic
7355 | N_Op_Subtract
7356 | N_Op_Xor
7357 | N_Operator_Symbol
7358 | N_Ordinary_Fixed_Point_Definition
7359 | N_Others_Choice
7360 | N_Package_Specification
7361 | N_Parameter_Association
7362 | N_Parameter_Specification
7363 | N_Pop_Constraint_Error_Label
7364 | N_Pop_Program_Error_Label
7365 | N_Pop_Storage_Error_Label
7366 | N_Pragma_Argument_Association
7367 | N_Procedure_Specification
7368 | N_Protected_Definition
7369 | N_Push_Constraint_Error_Label
7370 | N_Push_Program_Error_Label
7371 | N_Push_Storage_Error_Label
7372 | N_Qualified_Expression
7373 | N_Quantified_Expression
7374 | N_Raise_Expression
7375 | N_Range
7376 | N_Range_Constraint
7377 | N_Real_Literal
7378 | N_Real_Range_Specification
7379 | N_Record_Definition
7380 | N_Reference
7381 | N_SCIL_Dispatch_Table_Tag_Init
7382 | N_SCIL_Dispatching_Call
7383 | N_SCIL_Membership_Test
7384 | N_Selected_Component
7385 | N_Signed_Integer_Type_Definition
7386 | N_Single_Protected_Declaration
7387 | N_Slice
7388 | N_String_Literal
7389 | N_Subtype_Indication
7390 | N_Subunit
7391 | N_Target_Name
7392 | N_Task_Definition
7393 | N_Terminate_Alternative
7394 | N_Triggering_Alternative
7395 | N_Type_Conversion
7396 | N_Unchecked_Expression
7397 | N_Unchecked_Type_Conversion
7398 | N_Unconstrained_Array_Definition
7399 | N_Unused_At_End
7400 | N_Unused_At_Start
7401 | N_Variant
7402 | N_Variant_Part
7403 | N_Validate_Unchecked_Conversion
7404 | N_With_Clause
7406 null;
7407 end case;
7409 -- If we fall through above tests, keep climbing tree
7411 N := P;
7413 if Nkind (Parent (N)) = N_Subunit then
7415 -- This is the proper body corresponding to a stub. Insertion must
7416 -- be done at the point of the stub, which is in the declarative
7417 -- part of the parent unit.
7419 P := Corresponding_Stub (Parent (N));
7421 else
7422 P := Parent (N);
7423 end if;
7424 end loop;
7425 end Insert_Actions;
7427 -- Version with check(s) suppressed
7429 procedure Insert_Actions
7430 (Assoc_Node : Node_Id;
7431 Ins_Actions : List_Id;
7432 Suppress : Check_Id)
7434 begin
7435 if Suppress = All_Checks then
7436 declare
7437 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7438 begin
7439 Scope_Suppress.Suppress := (others => True);
7440 Insert_Actions (Assoc_Node, Ins_Actions);
7441 Scope_Suppress.Suppress := Sva;
7442 end;
7444 else
7445 declare
7446 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7447 begin
7448 Scope_Suppress.Suppress (Suppress) := True;
7449 Insert_Actions (Assoc_Node, Ins_Actions);
7450 Scope_Suppress.Suppress (Suppress) := Svg;
7451 end;
7452 end if;
7453 end Insert_Actions;
7455 --------------------------
7456 -- Insert_Actions_After --
7457 --------------------------
7459 procedure Insert_Actions_After
7460 (Assoc_Node : Node_Id;
7461 Ins_Actions : List_Id)
7463 begin
7464 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7465 Store_After_Actions_In_Scope (Ins_Actions);
7466 else
7467 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7468 end if;
7469 end Insert_Actions_After;
7471 ------------------------
7472 -- Insert_Declaration --
7473 ------------------------
7475 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7476 P : Node_Id;
7478 begin
7479 pragma Assert (Nkind (N) in N_Subexpr);
7481 -- Climb until we find a procedure or a package
7483 P := N;
7484 loop
7485 pragma Assert (Present (Parent (P)));
7486 P := Parent (P);
7488 if Is_List_Member (P) then
7489 exit when Nkind_In (Parent (P), N_Package_Specification,
7490 N_Subprogram_Body);
7492 -- Special handling for handled sequence of statements, we must
7493 -- insert in the statements not the exception handlers!
7495 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7496 P := First (Statements (Parent (P)));
7497 exit;
7498 end if;
7499 end if;
7500 end loop;
7502 -- Now do the insertion
7504 Insert_Before (P, Decl);
7505 Analyze (Decl);
7506 end Insert_Declaration;
7508 ---------------------------------
7509 -- Insert_Library_Level_Action --
7510 ---------------------------------
7512 procedure Insert_Library_Level_Action (N : Node_Id) is
7513 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7515 begin
7516 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7517 -- And not Main_Unit as previously. If the main unit is a body,
7518 -- the scope needed to analyze the actions is the entity of the
7519 -- corresponding declaration.
7521 if No (Actions (Aux)) then
7522 Set_Actions (Aux, New_List (N));
7523 else
7524 Append (N, Actions (Aux));
7525 end if;
7527 Analyze (N);
7528 Pop_Scope;
7529 end Insert_Library_Level_Action;
7531 ----------------------------------
7532 -- Insert_Library_Level_Actions --
7533 ----------------------------------
7535 procedure Insert_Library_Level_Actions (L : List_Id) is
7536 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7538 begin
7539 if Is_Non_Empty_List (L) then
7540 Push_Scope (Cunit_Entity (Main_Unit));
7541 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7543 if No (Actions (Aux)) then
7544 Set_Actions (Aux, L);
7545 Analyze_List (L);
7546 else
7547 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7548 end if;
7550 Pop_Scope;
7551 end if;
7552 end Insert_Library_Level_Actions;
7554 ----------------------
7555 -- Inside_Init_Proc --
7556 ----------------------
7558 function Inside_Init_Proc return Boolean is
7559 S : Entity_Id;
7561 begin
7562 S := Current_Scope;
7563 while Present (S) and then S /= Standard_Standard loop
7564 if Is_Init_Proc (S) then
7565 return True;
7566 else
7567 S := Scope (S);
7568 end if;
7569 end loop;
7571 return False;
7572 end Inside_Init_Proc;
7574 ----------------------------
7575 -- Is_All_Null_Statements --
7576 ----------------------------
7578 function Is_All_Null_Statements (L : List_Id) return Boolean is
7579 Stm : Node_Id;
7581 begin
7582 Stm := First (L);
7583 while Present (Stm) loop
7584 if Nkind (Stm) /= N_Null_Statement then
7585 return False;
7586 end if;
7588 Next (Stm);
7589 end loop;
7591 return True;
7592 end Is_All_Null_Statements;
7594 --------------------------------------------------
7595 -- Is_Displacement_Of_Object_Or_Function_Result --
7596 --------------------------------------------------
7598 function Is_Displacement_Of_Object_Or_Function_Result
7599 (Obj_Id : Entity_Id) return Boolean
7601 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7602 -- Determine whether node N denotes a controlled function call
7604 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
7605 -- Determine whether node N denotes a generalized indexing form which
7606 -- involves a controlled result.
7608 function Is_Displace_Call (N : Node_Id) return Boolean;
7609 -- Determine whether node N denotes a call to Ada.Tags.Displace
7611 function Is_Source_Object (N : Node_Id) return Boolean;
7612 -- Determine whether a particular node denotes a source object
7614 function Strip (N : Node_Id) return Node_Id;
7615 -- Examine arbitrary node N by stripping various indirections and return
7616 -- the "real" node.
7618 ---------------------------------
7619 -- Is_Controlled_Function_Call --
7620 ---------------------------------
7622 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7623 Expr : Node_Id;
7625 begin
7626 -- When a function call appears in Object.Operation format, the
7627 -- original representation has several possible forms depending on
7628 -- the availability and form of actual parameters:
7630 -- Obj.Func N_Selected_Component
7631 -- Obj.Func (Actual) N_Indexed_Component
7632 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7633 -- N_Selected_Component
7635 Expr := Original_Node (N);
7636 loop
7637 if Nkind (Expr) = N_Function_Call then
7638 Expr := Name (Expr);
7640 -- "Obj.Func (Actual)" case
7642 elsif Nkind (Expr) = N_Indexed_Component then
7643 Expr := Prefix (Expr);
7645 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7647 elsif Nkind (Expr) = N_Selected_Component then
7648 Expr := Selector_Name (Expr);
7650 else
7651 exit;
7652 end if;
7653 end loop;
7655 return
7656 Nkind (Expr) in N_Has_Entity
7657 and then Present (Entity (Expr))
7658 and then Ekind (Entity (Expr)) = E_Function
7659 and then Needs_Finalization (Etype (Entity (Expr)));
7660 end Is_Controlled_Function_Call;
7662 ----------------------------
7663 -- Is_Controlled_Indexing --
7664 ----------------------------
7666 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
7667 Expr : constant Node_Id := Original_Node (N);
7669 begin
7670 return
7671 Nkind (Expr) = N_Indexed_Component
7672 and then Present (Generalized_Indexing (Expr))
7673 and then Needs_Finalization (Etype (Expr));
7674 end Is_Controlled_Indexing;
7676 ----------------------
7677 -- Is_Displace_Call --
7678 ----------------------
7680 function Is_Displace_Call (N : Node_Id) return Boolean is
7681 Call : constant Node_Id := Strip (N);
7683 begin
7684 return
7685 Present (Call)
7686 and then Nkind (Call) = N_Function_Call
7687 and then Nkind (Name (Call)) in N_Has_Entity
7688 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7689 end Is_Displace_Call;
7691 ----------------------
7692 -- Is_Source_Object --
7693 ----------------------
7695 function Is_Source_Object (N : Node_Id) return Boolean is
7696 Obj : constant Node_Id := Strip (N);
7698 begin
7699 return
7700 Present (Obj)
7701 and then Comes_From_Source (Obj)
7702 and then Nkind (Obj) in N_Has_Entity
7703 and then Is_Object (Entity (Obj));
7704 end Is_Source_Object;
7706 -----------
7707 -- Strip --
7708 -----------
7710 function Strip (N : Node_Id) return Node_Id is
7711 Result : Node_Id;
7713 begin
7714 Result := N;
7715 loop
7716 if Nkind (Result) = N_Explicit_Dereference then
7717 Result := Prefix (Result);
7719 elsif Nkind_In (Result, N_Type_Conversion,
7720 N_Unchecked_Type_Conversion)
7721 then
7722 Result := Expression (Result);
7724 else
7725 exit;
7726 end if;
7727 end loop;
7729 return Result;
7730 end Strip;
7732 -- Local variables
7734 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
7735 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7736 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
7737 Orig_Expr : Node_Id;
7739 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7741 begin
7742 -- Case 1:
7744 -- Obj : CW_Type := Function_Call (...);
7746 -- is rewritten into:
7748 -- Temp : ... := Function_Call (...)'reference;
7749 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7751 -- where the return type of the function and the class-wide type require
7752 -- dispatch table pointer displacement.
7754 -- Case 2:
7756 -- Obj : CW_Type := Container (...);
7758 -- is rewritten into:
7760 -- Temp : ... := Function_Call (Container, ...)'reference;
7761 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7763 -- where the container element type and the class-wide type require
7764 -- dispatch table pointer dispacement.
7766 -- Case 3:
7768 -- Obj : CW_Type := Src_Obj;
7770 -- is rewritten into:
7772 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7774 -- where the type of the source object and the class-wide type require
7775 -- dispatch table pointer displacement.
7777 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
7778 and then Is_Class_Wide_Type (Obj_Typ)
7779 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7780 and then Nkind (Orig_Decl) = N_Object_Declaration
7781 and then Comes_From_Source (Orig_Decl)
7782 then
7783 Orig_Expr := Expression (Orig_Decl);
7785 return
7786 Is_Controlled_Function_Call (Orig_Expr)
7787 or else Is_Controlled_Indexing (Orig_Expr)
7788 or else Is_Source_Object (Orig_Expr);
7789 end if;
7791 return False;
7792 end Is_Displacement_Of_Object_Or_Function_Result;
7794 ------------------------------
7795 -- Is_Finalizable_Transient --
7796 ------------------------------
7798 function Is_Finalizable_Transient
7799 (Decl : Node_Id;
7800 Rel_Node : Node_Id) return Boolean
7802 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7803 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7805 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7806 -- Determine whether transient object Trans_Id is initialized either
7807 -- by a function call which returns an access type or simply renames
7808 -- another pointer.
7810 function Initialized_By_Aliased_BIP_Func_Call
7811 (Trans_Id : Entity_Id) return Boolean;
7812 -- Determine whether transient object Trans_Id is initialized by a
7813 -- build-in-place function call where the BIPalloc parameter is of
7814 -- value 1 and BIPaccess is not null. This case creates an aliasing
7815 -- between the returned value and the value denoted by BIPaccess.
7817 function Is_Aliased
7818 (Trans_Id : Entity_Id;
7819 First_Stmt : Node_Id) return Boolean;
7820 -- Determine whether transient object Trans_Id has been renamed or
7821 -- aliased through 'reference in the statement list starting from
7822 -- First_Stmt.
7824 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7825 -- Determine whether transient object Trans_Id is allocated on the heap
7827 function Is_Iterated_Container
7828 (Trans_Id : Entity_Id;
7829 First_Stmt : Node_Id) return Boolean;
7830 -- Determine whether transient object Trans_Id denotes a container which
7831 -- is in the process of being iterated in the statement list starting
7832 -- from First_Stmt.
7834 ---------------------------
7835 -- Initialized_By_Access --
7836 ---------------------------
7838 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7839 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7841 begin
7842 return
7843 Present (Expr)
7844 and then Nkind (Expr) /= N_Reference
7845 and then Is_Access_Type (Etype (Expr));
7846 end Initialized_By_Access;
7848 ------------------------------------------
7849 -- Initialized_By_Aliased_BIP_Func_Call --
7850 ------------------------------------------
7852 function Initialized_By_Aliased_BIP_Func_Call
7853 (Trans_Id : Entity_Id) return Boolean
7855 Call : Node_Id := Expression (Parent (Trans_Id));
7857 begin
7858 -- Build-in-place calls usually appear in 'reference format
7860 if Nkind (Call) = N_Reference then
7861 Call := Prefix (Call);
7862 end if;
7864 Call := Unqual_Conv (Call);
7866 if Is_Build_In_Place_Function_Call (Call) then
7867 declare
7868 Access_Nam : Name_Id := No_Name;
7869 Access_OK : Boolean := False;
7870 Actual : Node_Id;
7871 Alloc_Nam : Name_Id := No_Name;
7872 Alloc_OK : Boolean := False;
7873 Formal : Node_Id;
7874 Func_Id : Entity_Id;
7875 Param : Node_Id;
7877 begin
7878 -- Examine all parameter associations of the function call
7880 Param := First (Parameter_Associations (Call));
7881 while Present (Param) loop
7882 if Nkind (Param) = N_Parameter_Association
7883 and then Nkind (Selector_Name (Param)) = N_Identifier
7884 then
7885 Actual := Explicit_Actual_Parameter (Param);
7886 Formal := Selector_Name (Param);
7888 -- Construct the names of formals BIPaccess and BIPalloc
7889 -- using the function name retrieved from an arbitrary
7890 -- formal.
7892 if Access_Nam = No_Name
7893 and then Alloc_Nam = No_Name
7894 and then Present (Entity (Formal))
7895 then
7896 Func_Id := Scope (Entity (Formal));
7898 Access_Nam :=
7899 New_External_Name (Chars (Func_Id),
7900 BIP_Formal_Suffix (BIP_Object_Access));
7902 Alloc_Nam :=
7903 New_External_Name (Chars (Func_Id),
7904 BIP_Formal_Suffix (BIP_Alloc_Form));
7905 end if;
7907 -- A match for BIPaccess => Temp has been found
7909 if Chars (Formal) = Access_Nam
7910 and then Nkind (Actual) /= N_Null
7911 then
7912 Access_OK := True;
7913 end if;
7915 -- A match for BIPalloc => 1 has been found
7917 if Chars (Formal) = Alloc_Nam
7918 and then Nkind (Actual) = N_Integer_Literal
7919 and then Intval (Actual) = Uint_1
7920 then
7921 Alloc_OK := True;
7922 end if;
7923 end if;
7925 Next (Param);
7926 end loop;
7928 return Access_OK and Alloc_OK;
7929 end;
7930 end if;
7932 return False;
7933 end Initialized_By_Aliased_BIP_Func_Call;
7935 ----------------
7936 -- Is_Aliased --
7937 ----------------
7939 function Is_Aliased
7940 (Trans_Id : Entity_Id;
7941 First_Stmt : Node_Id) return Boolean
7943 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7944 -- Given an object renaming declaration, retrieve the entity of the
7945 -- renamed name. Return Empty if the renamed name is anything other
7946 -- than a variable or a constant.
7948 -------------------------
7949 -- Find_Renamed_Object --
7950 -------------------------
7952 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7953 Ren_Obj : Node_Id := Empty;
7955 function Find_Object (N : Node_Id) return Traverse_Result;
7956 -- Try to detect an object which is either a constant or a
7957 -- variable.
7959 -----------------
7960 -- Find_Object --
7961 -----------------
7963 function Find_Object (N : Node_Id) return Traverse_Result is
7964 begin
7965 -- Stop the search once a constant or a variable has been
7966 -- detected.
7968 if Nkind (N) = N_Identifier
7969 and then Present (Entity (N))
7970 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7971 then
7972 Ren_Obj := Entity (N);
7973 return Abandon;
7974 end if;
7976 return OK;
7977 end Find_Object;
7979 procedure Search is new Traverse_Proc (Find_Object);
7981 -- Local variables
7983 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7985 -- Start of processing for Find_Renamed_Object
7987 begin
7988 -- Actions related to dispatching calls may appear as renamings of
7989 -- tags. Do not process this type of renaming because it does not
7990 -- use the actual value of the object.
7992 if not Is_RTE (Typ, RE_Tag_Ptr) then
7993 Search (Name (Ren_Decl));
7994 end if;
7996 return Ren_Obj;
7997 end Find_Renamed_Object;
7999 -- Local variables
8001 Expr : Node_Id;
8002 Ren_Obj : Entity_Id;
8003 Stmt : Node_Id;
8005 -- Start of processing for Is_Aliased
8007 begin
8008 -- A controlled transient object is not considered aliased when it
8009 -- appears inside an expression_with_actions node even when there are
8010 -- explicit aliases of it:
8012 -- do
8013 -- Trans_Id : Ctrl_Typ ...; -- transient object
8014 -- Alias : ... := Trans_Id; -- object is aliased
8015 -- Val : constant Boolean :=
8016 -- ... Alias ...; -- aliasing ends
8017 -- <finalize Trans_Id> -- object safe to finalize
8018 -- in Val end;
8020 -- Expansion ensures that all aliases are encapsulated in the actions
8021 -- list and do not leak to the expression by forcing the evaluation
8022 -- of the expression.
8024 if Nkind (Rel_Node) = N_Expression_With_Actions then
8025 return False;
8027 -- Otherwise examine the statements after the controlled transient
8028 -- object and look for various forms of aliasing.
8030 else
8031 Stmt := First_Stmt;
8032 while Present (Stmt) loop
8033 if Nkind (Stmt) = N_Object_Declaration then
8034 Expr := Expression (Stmt);
8036 -- Aliasing of the form:
8037 -- Obj : ... := Trans_Id'reference;
8039 if Present (Expr)
8040 and then Nkind (Expr) = N_Reference
8041 and then Nkind (Prefix (Expr)) = N_Identifier
8042 and then Entity (Prefix (Expr)) = Trans_Id
8043 then
8044 return True;
8045 end if;
8047 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8048 Ren_Obj := Find_Renamed_Object (Stmt);
8050 -- Aliasing of the form:
8051 -- Obj : ... renames ... Trans_Id ...;
8053 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8054 return True;
8055 end if;
8056 end if;
8058 Next (Stmt);
8059 end loop;
8061 return False;
8062 end if;
8063 end Is_Aliased;
8065 ------------------
8066 -- Is_Allocated --
8067 ------------------
8069 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8070 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8071 begin
8072 return
8073 Is_Access_Type (Etype (Trans_Id))
8074 and then Present (Expr)
8075 and then Nkind (Expr) = N_Allocator;
8076 end Is_Allocated;
8078 ---------------------------
8079 -- Is_Iterated_Container --
8080 ---------------------------
8082 function Is_Iterated_Container
8083 (Trans_Id : Entity_Id;
8084 First_Stmt : Node_Id) return Boolean
8086 Aspect : Node_Id;
8087 Call : Node_Id;
8088 Iter : Entity_Id;
8089 Param : Node_Id;
8090 Stmt : Node_Id;
8091 Typ : Entity_Id;
8093 begin
8094 -- It is not possible to iterate over containers in non-Ada 2012 code
8096 if Ada_Version < Ada_2012 then
8097 return False;
8098 end if;
8100 Typ := Etype (Trans_Id);
8102 -- Handle access type created for secondary stack use
8104 if Is_Access_Type (Typ) then
8105 Typ := Designated_Type (Typ);
8106 end if;
8108 -- Look for aspect Default_Iterator. It may be part of a type
8109 -- declaration for a container, or inherited from a base type
8110 -- or parent type.
8112 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8114 if Present (Aspect) then
8115 Iter := Entity (Aspect);
8117 -- Examine the statements following the container object and
8118 -- look for a call to the default iterate routine where the
8119 -- first parameter is the transient. Such a call appears as:
8121 -- It : Access_To_CW_Iterator :=
8122 -- Iterate (Tran_Id.all, ...)'reference;
8124 Stmt := First_Stmt;
8125 while Present (Stmt) loop
8127 -- Detect an object declaration which is initialized by a
8128 -- secondary stack function call.
8130 if Nkind (Stmt) = N_Object_Declaration
8131 and then Present (Expression (Stmt))
8132 and then Nkind (Expression (Stmt)) = N_Reference
8133 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8134 then
8135 Call := Prefix (Expression (Stmt));
8137 -- The call must invoke the default iterate routine of
8138 -- the container and the transient object must appear as
8139 -- the first actual parameter. Skip any calls whose names
8140 -- are not entities.
8142 if Is_Entity_Name (Name (Call))
8143 and then Entity (Name (Call)) = Iter
8144 and then Present (Parameter_Associations (Call))
8145 then
8146 Param := First (Parameter_Associations (Call));
8148 if Nkind (Param) = N_Explicit_Dereference
8149 and then Entity (Prefix (Param)) = Trans_Id
8150 then
8151 return True;
8152 end if;
8153 end if;
8154 end if;
8156 Next (Stmt);
8157 end loop;
8158 end if;
8160 return False;
8161 end Is_Iterated_Container;
8163 -- Local variables
8165 Desig : Entity_Id := Obj_Typ;
8167 -- Start of processing for Is_Finalizable_Transient
8169 begin
8170 -- Handle access types
8172 if Is_Access_Type (Desig) then
8173 Desig := Available_View (Designated_Type (Desig));
8174 end if;
8176 return
8177 Ekind_In (Obj_Id, E_Constant, E_Variable)
8178 and then Needs_Finalization (Desig)
8179 and then Requires_Transient_Scope (Desig)
8180 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8182 -- Do not consider a transient object that was already processed
8184 and then not Is_Finalized_Transient (Obj_Id)
8186 -- Do not consider renamed or 'reference-d transient objects because
8187 -- the act of renaming extends the object's lifetime.
8189 and then not Is_Aliased (Obj_Id, Decl)
8191 -- Do not consider transient objects allocated on the heap since
8192 -- they are attached to a finalization master.
8194 and then not Is_Allocated (Obj_Id)
8196 -- If the transient object is a pointer, check that it is not
8197 -- initialized by a function that returns a pointer or acts as a
8198 -- renaming of another pointer.
8200 and then
8201 (not Is_Access_Type (Obj_Typ)
8202 or else not Initialized_By_Access (Obj_Id))
8204 -- Do not consider transient objects which act as indirect aliases
8205 -- of build-in-place function results.
8207 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8209 -- Do not consider conversions of tags to class-wide types
8211 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8213 -- Do not consider iterators because those are treated as normal
8214 -- controlled objects and are processed by the usual finalization
8215 -- machinery. This avoids the double finalization of an iterator.
8217 and then not Is_Iterator (Desig)
8219 -- Do not consider containers in the context of iterator loops. Such
8220 -- transient objects must exist for as long as the loop is around,
8221 -- otherwise any operation carried out by the iterator will fail.
8223 and then not Is_Iterated_Container (Obj_Id, Decl);
8224 end Is_Finalizable_Transient;
8226 ---------------------------------
8227 -- Is_Fully_Repped_Tagged_Type --
8228 ---------------------------------
8230 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8231 U : constant Entity_Id := Underlying_Type (T);
8232 Comp : Entity_Id;
8234 begin
8235 if No (U) or else not Is_Tagged_Type (U) then
8236 return False;
8237 elsif Has_Discriminants (U) then
8238 return False;
8239 elsif not Has_Specified_Layout (U) then
8240 return False;
8241 end if;
8243 -- Here we have a tagged type, see if it has any unlayed out fields
8244 -- other than a possible tag and parent fields. If so, we return False.
8246 Comp := First_Component (U);
8247 while Present (Comp) loop
8248 if not Is_Tag (Comp)
8249 and then Chars (Comp) /= Name_uParent
8250 and then No (Component_Clause (Comp))
8251 then
8252 return False;
8253 else
8254 Next_Component (Comp);
8255 end if;
8256 end loop;
8258 -- All components are layed out
8260 return True;
8261 end Is_Fully_Repped_Tagged_Type;
8263 ----------------------------------
8264 -- Is_Library_Level_Tagged_Type --
8265 ----------------------------------
8267 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8268 begin
8269 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8270 end Is_Library_Level_Tagged_Type;
8272 --------------------------
8273 -- Is_Non_BIP_Func_Call --
8274 --------------------------
8276 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8277 begin
8278 -- The expected call is of the format
8280 -- Func_Call'reference
8282 return
8283 Nkind (Expr) = N_Reference
8284 and then Nkind (Prefix (Expr)) = N_Function_Call
8285 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8286 end Is_Non_BIP_Func_Call;
8288 ----------------------------------
8289 -- Is_Possibly_Unaligned_Object --
8290 ----------------------------------
8292 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8293 T : constant Entity_Id := Etype (N);
8295 begin
8296 -- If renamed object, apply test to underlying object
8298 if Is_Entity_Name (N)
8299 and then Is_Object (Entity (N))
8300 and then Present (Renamed_Object (Entity (N)))
8301 then
8302 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8303 end if;
8305 -- Tagged and controlled types and aliased types are always aligned, as
8306 -- are concurrent types.
8308 if Is_Aliased (T)
8309 or else Has_Controlled_Component (T)
8310 or else Is_Concurrent_Type (T)
8311 or else Is_Tagged_Type (T)
8312 or else Is_Controlled (T)
8313 then
8314 return False;
8315 end if;
8317 -- If this is an element of a packed array, may be unaligned
8319 if Is_Ref_To_Bit_Packed_Array (N) then
8320 return True;
8321 end if;
8323 -- Case of indexed component reference: test whether prefix is unaligned
8325 if Nkind (N) = N_Indexed_Component then
8326 return Is_Possibly_Unaligned_Object (Prefix (N));
8328 -- Case of selected component reference
8330 elsif Nkind (N) = N_Selected_Component then
8331 declare
8332 P : constant Node_Id := Prefix (N);
8333 C : constant Entity_Id := Entity (Selector_Name (N));
8334 M : Nat;
8335 S : Nat;
8337 begin
8338 -- If component reference is for an array with non-static bounds,
8339 -- then it is always aligned: we can only process unaligned arrays
8340 -- with static bounds (more precisely compile time known bounds).
8342 if Is_Array_Type (T)
8343 and then not Compile_Time_Known_Bounds (T)
8344 then
8345 return False;
8346 end if;
8348 -- If component is aliased, it is definitely properly aligned
8350 if Is_Aliased (C) then
8351 return False;
8352 end if;
8354 -- If component is for a type implemented as a scalar, and the
8355 -- record is packed, and the component is other than the first
8356 -- component of the record, then the component may be unaligned.
8358 if Is_Packed (Etype (P))
8359 and then Represented_As_Scalar (Etype (C))
8360 and then First_Entity (Scope (C)) /= C
8361 then
8362 return True;
8363 end if;
8365 -- Compute maximum possible alignment for T
8367 -- If alignment is known, then that settles things
8369 if Known_Alignment (T) then
8370 M := UI_To_Int (Alignment (T));
8372 -- If alignment is not known, tentatively set max alignment
8374 else
8375 M := Ttypes.Maximum_Alignment;
8377 -- We can reduce this if the Esize is known since the default
8378 -- alignment will never be more than the smallest power of 2
8379 -- that does not exceed this Esize value.
8381 if Known_Esize (T) then
8382 S := UI_To_Int (Esize (T));
8384 while (M / 2) >= S loop
8385 M := M / 2;
8386 end loop;
8387 end if;
8388 end if;
8390 -- The following code is historical, it used to be present but it
8391 -- is too cautious, because the front-end does not know the proper
8392 -- default alignments for the target. Also, if the alignment is
8393 -- not known, the front end can't know in any case. If a copy is
8394 -- needed, the back-end will take care of it. This whole section
8395 -- including this comment can be removed later ???
8397 -- If the component reference is for a record that has a specified
8398 -- alignment, and we either know it is too small, or cannot tell,
8399 -- then the component may be unaligned.
8401 -- What is the following commented out code ???
8403 -- if Known_Alignment (Etype (P))
8404 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8405 -- and then M > Alignment (Etype (P))
8406 -- then
8407 -- return True;
8408 -- end if;
8410 -- Case of component clause present which may specify an
8411 -- unaligned position.
8413 if Present (Component_Clause (C)) then
8415 -- Otherwise we can do a test to make sure that the actual
8416 -- start position in the record, and the length, are both
8417 -- consistent with the required alignment. If not, we know
8418 -- that we are unaligned.
8420 declare
8421 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8422 begin
8423 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
8424 or else Esize (C) mod Align_In_Bits /= 0
8425 then
8426 return True;
8427 end if;
8428 end;
8429 end if;
8431 -- Otherwise, for a component reference, test prefix
8433 return Is_Possibly_Unaligned_Object (P);
8434 end;
8436 -- If not a component reference, must be aligned
8438 else
8439 return False;
8440 end if;
8441 end Is_Possibly_Unaligned_Object;
8443 ---------------------------------
8444 -- Is_Possibly_Unaligned_Slice --
8445 ---------------------------------
8447 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8448 begin
8449 -- Go to renamed object
8451 if Is_Entity_Name (N)
8452 and then Is_Object (Entity (N))
8453 and then Present (Renamed_Object (Entity (N)))
8454 then
8455 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8456 end if;
8458 -- The reference must be a slice
8460 if Nkind (N) /= N_Slice then
8461 return False;
8462 end if;
8464 -- We only need to worry if the target has strict alignment
8466 if not Target_Strict_Alignment then
8467 return False;
8468 end if;
8470 -- If it is a slice, then look at the array type being sliced
8472 declare
8473 Sarr : constant Node_Id := Prefix (N);
8474 -- Prefix of the slice, i.e. the array being sliced
8476 Styp : constant Entity_Id := Etype (Prefix (N));
8477 -- Type of the array being sliced
8479 Pref : Node_Id;
8480 Ptyp : Entity_Id;
8482 begin
8483 -- The problems arise if the array object that is being sliced
8484 -- is a component of a record or array, and we cannot guarantee
8485 -- the alignment of the array within its containing object.
8487 -- To investigate this, we look at successive prefixes to see
8488 -- if we have a worrisome indexed or selected component.
8490 Pref := Sarr;
8491 loop
8492 -- Case of array is part of an indexed component reference
8494 if Nkind (Pref) = N_Indexed_Component then
8495 Ptyp := Etype (Prefix (Pref));
8497 -- The only problematic case is when the array is packed, in
8498 -- which case we really know nothing about the alignment of
8499 -- individual components.
8501 if Is_Bit_Packed_Array (Ptyp) then
8502 return True;
8503 end if;
8505 -- Case of array is part of a selected component reference
8507 elsif Nkind (Pref) = N_Selected_Component then
8508 Ptyp := Etype (Prefix (Pref));
8510 -- We are definitely in trouble if the record in question
8511 -- has an alignment, and either we know this alignment is
8512 -- inconsistent with the alignment of the slice, or we don't
8513 -- know what the alignment of the slice should be.
8515 if Known_Alignment (Ptyp)
8516 and then (Unknown_Alignment (Styp)
8517 or else Alignment (Styp) > Alignment (Ptyp))
8518 then
8519 return True;
8520 end if;
8522 -- We are in potential trouble if the record type is packed.
8523 -- We could special case when we know that the array is the
8524 -- first component, but that's not such a simple case ???
8526 if Is_Packed (Ptyp) then
8527 return True;
8528 end if;
8530 -- We are in trouble if there is a component clause, and
8531 -- either we do not know the alignment of the slice, or
8532 -- the alignment of the slice is inconsistent with the
8533 -- bit position specified by the component clause.
8535 declare
8536 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8537 begin
8538 if Present (Component_Clause (Field))
8539 and then
8540 (Unknown_Alignment (Styp)
8541 or else
8542 (Component_Bit_Offset (Field) mod
8543 (System_Storage_Unit * Alignment (Styp))) /= 0)
8544 then
8545 return True;
8546 end if;
8547 end;
8549 -- For cases other than selected or indexed components we know we
8550 -- are OK, since no issues arise over alignment.
8552 else
8553 return False;
8554 end if;
8556 -- We processed an indexed component or selected component
8557 -- reference that looked safe, so keep checking prefixes.
8559 Pref := Prefix (Pref);
8560 end loop;
8561 end;
8562 end Is_Possibly_Unaligned_Slice;
8564 -------------------------------
8565 -- Is_Related_To_Func_Return --
8566 -------------------------------
8568 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8569 Expr : constant Node_Id := Related_Expression (Id);
8570 begin
8571 return
8572 Present (Expr)
8573 and then Nkind (Expr) = N_Explicit_Dereference
8574 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8575 end Is_Related_To_Func_Return;
8577 --------------------------------
8578 -- Is_Ref_To_Bit_Packed_Array --
8579 --------------------------------
8581 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8582 Result : Boolean;
8583 Expr : Node_Id;
8585 begin
8586 if Is_Entity_Name (N)
8587 and then Is_Object (Entity (N))
8588 and then Present (Renamed_Object (Entity (N)))
8589 then
8590 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8591 end if;
8593 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8594 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8595 Result := True;
8596 else
8597 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8598 end if;
8600 if Result and then Nkind (N) = N_Indexed_Component then
8601 Expr := First (Expressions (N));
8602 while Present (Expr) loop
8603 Force_Evaluation (Expr);
8604 Next (Expr);
8605 end loop;
8606 end if;
8608 return Result;
8610 else
8611 return False;
8612 end if;
8613 end Is_Ref_To_Bit_Packed_Array;
8615 --------------------------------
8616 -- Is_Ref_To_Bit_Packed_Slice --
8617 --------------------------------
8619 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8620 begin
8621 if Nkind (N) = N_Type_Conversion then
8622 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8624 elsif Is_Entity_Name (N)
8625 and then Is_Object (Entity (N))
8626 and then Present (Renamed_Object (Entity (N)))
8627 then
8628 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8630 elsif Nkind (N) = N_Slice
8631 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8632 then
8633 return True;
8635 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8636 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8638 else
8639 return False;
8640 end if;
8641 end Is_Ref_To_Bit_Packed_Slice;
8643 -----------------------
8644 -- Is_Renamed_Object --
8645 -----------------------
8647 function Is_Renamed_Object (N : Node_Id) return Boolean is
8648 Pnod : constant Node_Id := Parent (N);
8649 Kind : constant Node_Kind := Nkind (Pnod);
8650 begin
8651 if Kind = N_Object_Renaming_Declaration then
8652 return True;
8653 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8654 return Is_Renamed_Object (Pnod);
8655 else
8656 return False;
8657 end if;
8658 end Is_Renamed_Object;
8660 --------------------------------------
8661 -- Is_Secondary_Stack_BIP_Func_Call --
8662 --------------------------------------
8664 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8665 Alloc_Nam : Name_Id := No_Name;
8666 Actual : Node_Id;
8667 Call : Node_Id := Expr;
8668 Formal : Node_Id;
8669 Param : Node_Id;
8671 begin
8672 -- Build-in-place calls usually appear in 'reference format. Note that
8673 -- the accessibility check machinery may add an extra 'reference due to
8674 -- side effect removal.
8676 while Nkind (Call) = N_Reference loop
8677 Call := Prefix (Call);
8678 end loop;
8680 Call := Unqual_Conv (Call);
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 then
8689 Formal := Selector_Name (Param);
8690 Actual := Explicit_Actual_Parameter (Param);
8692 -- Construct the name of formal BIPalloc. It is much easier to
8693 -- extract the name of the function using an arbitrary formal's
8694 -- scope rather than the Name field of Call.
8696 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8697 Alloc_Nam :=
8698 New_External_Name
8699 (Chars (Scope (Entity (Formal))),
8700 BIP_Formal_Suffix (BIP_Alloc_Form));
8701 end if;
8703 -- A match for BIPalloc => 2 has been found
8705 if Chars (Formal) = Alloc_Nam
8706 and then Nkind (Actual) = N_Integer_Literal
8707 and then Intval (Actual) = Uint_2
8708 then
8709 return True;
8710 end if;
8711 end if;
8713 Next (Param);
8714 end loop;
8715 end if;
8717 return False;
8718 end Is_Secondary_Stack_BIP_Func_Call;
8720 -------------------------------------
8721 -- Is_Tag_To_Class_Wide_Conversion --
8722 -------------------------------------
8724 function Is_Tag_To_Class_Wide_Conversion
8725 (Obj_Id : Entity_Id) return Boolean
8727 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8729 begin
8730 return
8731 Is_Class_Wide_Type (Etype (Obj_Id))
8732 and then Present (Expr)
8733 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8734 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8735 end Is_Tag_To_Class_Wide_Conversion;
8737 ----------------------------
8738 -- Is_Untagged_Derivation --
8739 ----------------------------
8741 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8742 begin
8743 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8744 or else
8745 (Is_Private_Type (T) and then Present (Full_View (T))
8746 and then not Is_Tagged_Type (Full_View (T))
8747 and then Is_Derived_Type (Full_View (T))
8748 and then Etype (Full_View (T)) /= T);
8749 end Is_Untagged_Derivation;
8751 ------------------------------------
8752 -- Is_Untagged_Private_Derivation --
8753 ------------------------------------
8755 function Is_Untagged_Private_Derivation
8756 (Priv_Typ : Entity_Id;
8757 Full_Typ : Entity_Id) return Boolean
8759 begin
8760 return
8761 Present (Priv_Typ)
8762 and then Is_Untagged_Derivation (Priv_Typ)
8763 and then Is_Private_Type (Etype (Priv_Typ))
8764 and then Present (Full_Typ)
8765 and then Is_Itype (Full_Typ);
8766 end Is_Untagged_Private_Derivation;
8768 ---------------------------
8769 -- Is_Volatile_Reference --
8770 ---------------------------
8772 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8773 begin
8774 -- Only source references are to be treated as volatile, internally
8775 -- generated stuff cannot have volatile external effects.
8777 if not Comes_From_Source (N) then
8778 return False;
8780 -- Never true for reference to a type
8782 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8783 return False;
8785 -- Never true for a compile time known constant
8787 elsif Compile_Time_Known_Value (N) then
8788 return False;
8790 -- True if object reference with volatile type
8792 elsif Is_Volatile_Object (N) then
8793 return True;
8795 -- True if reference to volatile entity
8797 elsif Is_Entity_Name (N) then
8798 return Treat_As_Volatile (Entity (N));
8800 -- True for slice of volatile array
8802 elsif Nkind (N) = N_Slice then
8803 return Is_Volatile_Reference (Prefix (N));
8805 -- True if volatile component
8807 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8808 if (Is_Entity_Name (Prefix (N))
8809 and then Has_Volatile_Components (Entity (Prefix (N))))
8810 or else (Present (Etype (Prefix (N)))
8811 and then Has_Volatile_Components (Etype (Prefix (N))))
8812 then
8813 return True;
8814 else
8815 return Is_Volatile_Reference (Prefix (N));
8816 end if;
8818 -- Otherwise false
8820 else
8821 return False;
8822 end if;
8823 end Is_Volatile_Reference;
8825 --------------------
8826 -- Kill_Dead_Code --
8827 --------------------
8829 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8830 W : Boolean := Warn;
8831 -- Set False if warnings suppressed
8833 begin
8834 if Present (N) then
8835 Remove_Warning_Messages (N);
8837 -- Generate warning if appropriate
8839 if W then
8841 -- We suppress the warning if this code is under control of an
8842 -- if statement, whose condition is a simple identifier, and
8843 -- either we are in an instance, or warnings off is set for this
8844 -- identifier. The reason for killing it in the instance case is
8845 -- that it is common and reasonable for code to be deleted in
8846 -- instances for various reasons.
8848 -- Could we use Is_Statically_Unevaluated here???
8850 if Nkind (Parent (N)) = N_If_Statement then
8851 declare
8852 C : constant Node_Id := Condition (Parent (N));
8853 begin
8854 if Nkind (C) = N_Identifier
8855 and then
8856 (In_Instance
8857 or else (Present (Entity (C))
8858 and then Has_Warnings_Off (Entity (C))))
8859 then
8860 W := False;
8861 end if;
8862 end;
8863 end if;
8865 -- Generate warning if not suppressed
8867 if W then
8868 Error_Msg_F
8869 ("?t?this code can never be executed and has been deleted!",
8871 end if;
8872 end if;
8874 -- Recurse into block statements and bodies to process declarations
8875 -- and statements.
8877 if Nkind (N) = N_Block_Statement
8878 or else Nkind (N) = N_Subprogram_Body
8879 or else Nkind (N) = N_Package_Body
8880 then
8881 Kill_Dead_Code (Declarations (N), False);
8882 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8884 if Nkind (N) = N_Subprogram_Body then
8885 Set_Is_Eliminated (Defining_Entity (N));
8886 end if;
8888 elsif Nkind (N) = N_Package_Declaration then
8889 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8890 Kill_Dead_Code (Private_Declarations (Specification (N)));
8892 -- ??? After this point, Delete_Tree has been called on all
8893 -- declarations in Specification (N), so references to entities
8894 -- therein look suspicious.
8896 declare
8897 E : Entity_Id := First_Entity (Defining_Entity (N));
8899 begin
8900 while Present (E) loop
8901 if Ekind (E) = E_Operator then
8902 Set_Is_Eliminated (E);
8903 end if;
8905 Next_Entity (E);
8906 end loop;
8907 end;
8909 -- Recurse into composite statement to kill individual statements in
8910 -- particular instantiations.
8912 elsif Nkind (N) = N_If_Statement then
8913 Kill_Dead_Code (Then_Statements (N));
8914 Kill_Dead_Code (Elsif_Parts (N));
8915 Kill_Dead_Code (Else_Statements (N));
8917 elsif Nkind (N) = N_Loop_Statement then
8918 Kill_Dead_Code (Statements (N));
8920 elsif Nkind (N) = N_Case_Statement then
8921 declare
8922 Alt : Node_Id;
8923 begin
8924 Alt := First (Alternatives (N));
8925 while Present (Alt) loop
8926 Kill_Dead_Code (Statements (Alt));
8927 Next (Alt);
8928 end loop;
8929 end;
8931 elsif Nkind (N) = N_Case_Statement_Alternative then
8932 Kill_Dead_Code (Statements (N));
8934 -- Deal with dead instances caused by deleting instantiations
8936 elsif Nkind (N) in N_Generic_Instantiation then
8937 Remove_Dead_Instance (N);
8938 end if;
8939 end if;
8940 end Kill_Dead_Code;
8942 -- Case where argument is a list of nodes to be killed
8944 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8945 N : Node_Id;
8946 W : Boolean;
8948 begin
8949 W := Warn;
8951 if Is_Non_Empty_List (L) then
8952 N := First (L);
8953 while Present (N) loop
8954 Kill_Dead_Code (N, W);
8955 W := False;
8956 Next (N);
8957 end loop;
8958 end if;
8959 end Kill_Dead_Code;
8961 ------------------------
8962 -- Known_Non_Negative --
8963 ------------------------
8965 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8966 begin
8967 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8968 return True;
8970 else
8971 declare
8972 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8973 begin
8974 return
8975 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8976 end;
8977 end if;
8978 end Known_Non_Negative;
8980 -----------------------------
8981 -- Make_CW_Equivalent_Type --
8982 -----------------------------
8984 -- Create a record type used as an equivalent of any member of the class
8985 -- which takes its size from exp.
8987 -- Generate the following code:
8989 -- type Equiv_T is record
8990 -- _parent : T (List of discriminant constraints taken from Exp);
8991 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8992 -- end Equiv_T;
8994 -- ??? Note that this type does not guarantee same alignment as all
8995 -- derived types
8997 function Make_CW_Equivalent_Type
8998 (T : Entity_Id;
8999 E : Node_Id) return Entity_Id
9001 Loc : constant Source_Ptr := Sloc (E);
9002 Root_Typ : constant Entity_Id := Root_Type (T);
9003 List_Def : constant List_Id := Empty_List;
9004 Comp_List : constant List_Id := New_List;
9005 Equiv_Type : Entity_Id;
9006 Range_Type : Entity_Id;
9007 Str_Type : Entity_Id;
9008 Constr_Root : Entity_Id;
9009 Sizexpr : Node_Id;
9011 begin
9012 -- If the root type is already constrained, there are no discriminants
9013 -- in the expression.
9015 if not Has_Discriminants (Root_Typ)
9016 or else Is_Constrained (Root_Typ)
9017 then
9018 Constr_Root := Root_Typ;
9020 -- At this point in the expansion, non-limited view of the type
9021 -- must be available, otherwise the error will be reported later.
9023 if From_Limited_With (Constr_Root)
9024 and then Present (Non_Limited_View (Constr_Root))
9025 then
9026 Constr_Root := Non_Limited_View (Constr_Root);
9027 end if;
9029 else
9030 Constr_Root := Make_Temporary (Loc, 'R');
9032 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9034 Append_To (List_Def,
9035 Make_Subtype_Declaration (Loc,
9036 Defining_Identifier => Constr_Root,
9037 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9038 end if;
9040 -- Generate the range subtype declaration
9042 Range_Type := Make_Temporary (Loc, 'G');
9044 if not Is_Interface (Root_Typ) then
9046 -- subtype rg__xx is
9047 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9049 Sizexpr :=
9050 Make_Op_Subtract (Loc,
9051 Left_Opnd =>
9052 Make_Attribute_Reference (Loc,
9053 Prefix =>
9054 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9055 Attribute_Name => Name_Size),
9056 Right_Opnd =>
9057 Make_Attribute_Reference (Loc,
9058 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9059 Attribute_Name => Name_Object_Size));
9060 else
9061 -- subtype rg__xx is
9062 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9064 Sizexpr :=
9065 Make_Attribute_Reference (Loc,
9066 Prefix =>
9067 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9068 Attribute_Name => Name_Size);
9069 end if;
9071 Set_Paren_Count (Sizexpr, 1);
9073 Append_To (List_Def,
9074 Make_Subtype_Declaration (Loc,
9075 Defining_Identifier => Range_Type,
9076 Subtype_Indication =>
9077 Make_Subtype_Indication (Loc,
9078 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9079 Constraint => Make_Range_Constraint (Loc,
9080 Range_Expression =>
9081 Make_Range (Loc,
9082 Low_Bound => Make_Integer_Literal (Loc, 1),
9083 High_Bound =>
9084 Make_Op_Divide (Loc,
9085 Left_Opnd => Sizexpr,
9086 Right_Opnd => Make_Integer_Literal (Loc,
9087 Intval => System_Storage_Unit)))))));
9089 -- subtype str__nn is Storage_Array (rg__x);
9091 Str_Type := Make_Temporary (Loc, 'S');
9092 Append_To (List_Def,
9093 Make_Subtype_Declaration (Loc,
9094 Defining_Identifier => Str_Type,
9095 Subtype_Indication =>
9096 Make_Subtype_Indication (Loc,
9097 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9098 Constraint =>
9099 Make_Index_Or_Discriminant_Constraint (Loc,
9100 Constraints =>
9101 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9103 -- type Equiv_T is record
9104 -- [ _parent : Tnn; ]
9105 -- E : Str_Type;
9106 -- end Equiv_T;
9108 Equiv_Type := Make_Temporary (Loc, 'T');
9109 Set_Ekind (Equiv_Type, E_Record_Type);
9110 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9112 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9113 -- treatment for this type. In particular, even though _parent's type
9114 -- is a controlled type or contains controlled components, we do not
9115 -- want to set Has_Controlled_Component on it to avoid making it gain
9116 -- an unwanted _controller component.
9118 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9120 -- A class-wide equivalent type does not require initialization
9122 Set_Suppress_Initialization (Equiv_Type);
9124 if not Is_Interface (Root_Typ) then
9125 Append_To (Comp_List,
9126 Make_Component_Declaration (Loc,
9127 Defining_Identifier =>
9128 Make_Defining_Identifier (Loc, Name_uParent),
9129 Component_Definition =>
9130 Make_Component_Definition (Loc,
9131 Aliased_Present => False,
9132 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9133 end if;
9135 Append_To (Comp_List,
9136 Make_Component_Declaration (Loc,
9137 Defining_Identifier => Make_Temporary (Loc, 'C'),
9138 Component_Definition =>
9139 Make_Component_Definition (Loc,
9140 Aliased_Present => False,
9141 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9143 Append_To (List_Def,
9144 Make_Full_Type_Declaration (Loc,
9145 Defining_Identifier => Equiv_Type,
9146 Type_Definition =>
9147 Make_Record_Definition (Loc,
9148 Component_List =>
9149 Make_Component_List (Loc,
9150 Component_Items => Comp_List,
9151 Variant_Part => Empty))));
9153 -- Suppress all checks during the analysis of the expanded code to avoid
9154 -- the generation of spurious warnings under ZFP run-time.
9156 Insert_Actions (E, List_Def, Suppress => All_Checks);
9157 return Equiv_Type;
9158 end Make_CW_Equivalent_Type;
9160 -------------------------
9161 -- Make_Invariant_Call --
9162 -------------------------
9164 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9165 Loc : constant Source_Ptr := Sloc (Expr);
9166 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9168 Proc_Id : Entity_Id;
9170 begin
9171 pragma Assert (Has_Invariants (Typ));
9173 Proc_Id := Invariant_Procedure (Typ);
9174 pragma Assert (Present (Proc_Id));
9176 return
9177 Make_Procedure_Call_Statement (Loc,
9178 Name => New_Occurrence_Of (Proc_Id, Loc),
9179 Parameter_Associations => New_List (Relocate_Node (Expr)));
9180 end Make_Invariant_Call;
9182 ------------------------
9183 -- Make_Literal_Range --
9184 ------------------------
9186 function Make_Literal_Range
9187 (Loc : Source_Ptr;
9188 Literal_Typ : Entity_Id) return Node_Id
9190 Lo : constant Node_Id :=
9191 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9192 Index : constant Entity_Id := Etype (Lo);
9194 Hi : Node_Id;
9195 Length_Expr : constant Node_Id :=
9196 Make_Op_Subtract (Loc,
9197 Left_Opnd =>
9198 Make_Integer_Literal (Loc,
9199 Intval => String_Literal_Length (Literal_Typ)),
9200 Right_Opnd =>
9201 Make_Integer_Literal (Loc, 1));
9203 begin
9204 Set_Analyzed (Lo, False);
9206 if Is_Integer_Type (Index) then
9207 Hi :=
9208 Make_Op_Add (Loc,
9209 Left_Opnd => New_Copy_Tree (Lo),
9210 Right_Opnd => Length_Expr);
9211 else
9212 Hi :=
9213 Make_Attribute_Reference (Loc,
9214 Attribute_Name => Name_Val,
9215 Prefix => New_Occurrence_Of (Index, Loc),
9216 Expressions => New_List (
9217 Make_Op_Add (Loc,
9218 Left_Opnd =>
9219 Make_Attribute_Reference (Loc,
9220 Attribute_Name => Name_Pos,
9221 Prefix => New_Occurrence_Of (Index, Loc),
9222 Expressions => New_List (New_Copy_Tree (Lo))),
9223 Right_Opnd => Length_Expr)));
9224 end if;
9226 return
9227 Make_Range (Loc,
9228 Low_Bound => Lo,
9229 High_Bound => Hi);
9230 end Make_Literal_Range;
9232 --------------------------
9233 -- Make_Non_Empty_Check --
9234 --------------------------
9236 function Make_Non_Empty_Check
9237 (Loc : Source_Ptr;
9238 N : Node_Id) return Node_Id
9240 begin
9241 return
9242 Make_Op_Ne (Loc,
9243 Left_Opnd =>
9244 Make_Attribute_Reference (Loc,
9245 Attribute_Name => Name_Length,
9246 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9247 Right_Opnd =>
9248 Make_Integer_Literal (Loc, 0));
9249 end Make_Non_Empty_Check;
9251 -------------------------
9252 -- Make_Predicate_Call --
9253 -------------------------
9255 -- WARNING: This routine manages Ghost regions. Return statements must be
9256 -- replaced by gotos which jump to the end of the routine and restore the
9257 -- Ghost mode.
9259 function Make_Predicate_Call
9260 (Typ : Entity_Id;
9261 Expr : Node_Id;
9262 Mem : Boolean := False) return Node_Id
9264 Loc : constant Source_Ptr := Sloc (Expr);
9266 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9267 -- Save the Ghost mode to restore on exit
9269 Call : Node_Id;
9270 Func_Id : Entity_Id;
9272 begin
9273 pragma Assert (Present (Predicate_Function (Typ)));
9275 -- The related type may be subject to pragma Ghost. Set the mode now to
9276 -- ensure that the call is properly marked as Ghost.
9278 Set_Ghost_Mode (Typ);
9280 -- Call special membership version if requested and available
9282 if Mem and then Present (Predicate_Function_M (Typ)) then
9283 Func_Id := Predicate_Function_M (Typ);
9284 else
9285 Func_Id := Predicate_Function (Typ);
9286 end if;
9288 -- Case of calling normal predicate function
9290 Call :=
9291 Make_Function_Call (Loc,
9292 Name => New_Occurrence_Of (Func_Id, Loc),
9293 Parameter_Associations => New_List (Relocate_Node (Expr)));
9295 Restore_Ghost_Mode (Saved_GM);
9297 return Call;
9298 end Make_Predicate_Call;
9300 --------------------------
9301 -- Make_Predicate_Check --
9302 --------------------------
9304 function Make_Predicate_Check
9305 (Typ : Entity_Id;
9306 Expr : Node_Id) return Node_Id
9308 procedure Replace_Subtype_Reference (N : Node_Id);
9309 -- Replace current occurrences of the subtype to which a dynamic
9310 -- predicate applies, by the expression that triggers a predicate
9311 -- check. This is needed for aspect Predicate_Failure, for which
9312 -- we do not generate a wrapper procedure, but simply modify the
9313 -- expression for the pragma of the predicate check.
9315 --------------------------------
9316 -- Replace_Subtype_Reference --
9317 --------------------------------
9319 procedure Replace_Subtype_Reference (N : Node_Id) is
9320 begin
9321 Rewrite (N, New_Copy_Tree (Expr));
9323 -- We want to treat the node as if it comes from source, so
9324 -- that ASIS will not ignore it.
9326 Set_Comes_From_Source (N, True);
9327 end Replace_Subtype_Reference;
9329 procedure Replace_Subtype_References is
9330 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9332 -- Local variables
9334 Loc : constant Source_Ptr := Sloc (Expr);
9335 Arg_List : List_Id;
9336 Fail_Expr : Node_Id;
9337 Nam : Name_Id;
9339 -- Start of processing for Make_Predicate_Check
9341 begin
9342 -- If predicate checks are suppressed, then return a null statement. For
9343 -- this call, we check only the scope setting. If the caller wants to
9344 -- check a specific entity's setting, they must do it manually.
9346 if Predicate_Checks_Suppressed (Empty) then
9347 return Make_Null_Statement (Loc);
9348 end if;
9350 -- Do not generate a check within an internal subprogram (stream
9351 -- functions and the like, including including predicate functions).
9353 if Within_Internal_Subprogram then
9354 return Make_Null_Statement (Loc);
9355 end if;
9357 -- Compute proper name to use, we need to get this right so that the
9358 -- right set of check policies apply to the Check pragma we are making.
9360 if Has_Dynamic_Predicate_Aspect (Typ) then
9361 Nam := Name_Dynamic_Predicate;
9362 elsif Has_Static_Predicate_Aspect (Typ) then
9363 Nam := Name_Static_Predicate;
9364 else
9365 Nam := Name_Predicate;
9366 end if;
9368 Arg_List := New_List (
9369 Make_Pragma_Argument_Association (Loc,
9370 Expression => Make_Identifier (Loc, Nam)),
9371 Make_Pragma_Argument_Association (Loc,
9372 Expression => Make_Predicate_Call (Typ, Expr)));
9374 -- If subtype has Predicate_Failure defined, add the correponding
9375 -- expression as an additional pragma parameter, after replacing
9376 -- current instances with the expression being checked.
9378 if Has_Aspect (Typ, Aspect_Predicate_Failure) then
9379 Fail_Expr :=
9380 New_Copy_Tree
9381 (Expression (Find_Aspect (Typ, Aspect_Predicate_Failure)));
9382 Replace_Subtype_References (Fail_Expr, Typ);
9384 Append_To (Arg_List,
9385 Make_Pragma_Argument_Association (Loc,
9386 Expression => Fail_Expr));
9387 end if;
9389 return
9390 Make_Pragma (Loc,
9391 Chars => Name_Check,
9392 Pragma_Argument_Associations => Arg_List);
9393 end Make_Predicate_Check;
9395 ----------------------------
9396 -- Make_Subtype_From_Expr --
9397 ----------------------------
9399 -- 1. If Expr is an unconstrained array expression, creates
9400 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9402 -- 2. If Expr is a unconstrained discriminated type expression, creates
9403 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9405 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9407 function Make_Subtype_From_Expr
9408 (E : Node_Id;
9409 Unc_Typ : Entity_Id;
9410 Related_Id : Entity_Id := Empty) return Node_Id
9412 List_Constr : constant List_Id := New_List;
9413 Loc : constant Source_Ptr := Sloc (E);
9414 D : Entity_Id;
9415 Full_Exp : Node_Id;
9416 Full_Subtyp : Entity_Id;
9417 High_Bound : Entity_Id;
9418 Index_Typ : Entity_Id;
9419 Low_Bound : Entity_Id;
9420 Priv_Subtyp : Entity_Id;
9421 Utyp : Entity_Id;
9423 begin
9424 if Is_Private_Type (Unc_Typ)
9425 and then Has_Unknown_Discriminants (Unc_Typ)
9426 then
9427 -- The caller requests a unique external name for both the private
9428 -- and the full subtype.
9430 if Present (Related_Id) then
9431 Full_Subtyp :=
9432 Make_Defining_Identifier (Loc,
9433 Chars => New_External_Name (Chars (Related_Id), 'C'));
9434 Priv_Subtyp :=
9435 Make_Defining_Identifier (Loc,
9436 Chars => New_External_Name (Chars (Related_Id), 'P'));
9438 else
9439 Full_Subtyp := Make_Temporary (Loc, 'C');
9440 Priv_Subtyp := Make_Temporary (Loc, 'P');
9441 end if;
9443 -- Prepare the subtype completion. Use the base type to find the
9444 -- underlying type because the type may be a generic actual or an
9445 -- explicit subtype.
9447 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9449 Full_Exp :=
9450 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9451 Set_Parent (Full_Exp, Parent (E));
9453 Insert_Action (E,
9454 Make_Subtype_Declaration (Loc,
9455 Defining_Identifier => Full_Subtyp,
9456 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9458 -- Define the dummy private subtype
9460 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9461 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9462 Set_Scope (Priv_Subtyp, Full_Subtyp);
9463 Set_Is_Constrained (Priv_Subtyp);
9464 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9465 Set_Is_Itype (Priv_Subtyp);
9466 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9468 if Is_Tagged_Type (Priv_Subtyp) then
9469 Set_Class_Wide_Type
9470 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9471 Set_Direct_Primitive_Operations (Priv_Subtyp,
9472 Direct_Primitive_Operations (Unc_Typ));
9473 end if;
9475 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9477 return New_Occurrence_Of (Priv_Subtyp, Loc);
9479 elsif Is_Array_Type (Unc_Typ) then
9480 Index_Typ := First_Index (Unc_Typ);
9481 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9483 -- Capture the bounds of each index constraint in case the context
9484 -- is an object declaration of an unconstrained type initialized
9485 -- by a function call:
9487 -- Obj : Unconstr_Typ := Func_Call;
9489 -- This scenario requires secondary scope management and the index
9490 -- constraint cannot depend on the temporary used to capture the
9491 -- result of the function call.
9493 -- SS_Mark;
9494 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9495 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9496 -- Obj : S := Temp.all;
9497 -- SS_Release; -- Temp is gone at this point, bounds of S are
9498 -- -- non existent.
9500 -- Generate:
9501 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9503 Low_Bound := Make_Temporary (Loc, 'B');
9504 Insert_Action (E,
9505 Make_Object_Declaration (Loc,
9506 Defining_Identifier => Low_Bound,
9507 Object_Definition =>
9508 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9509 Constant_Present => True,
9510 Expression =>
9511 Make_Attribute_Reference (Loc,
9512 Prefix => Duplicate_Subexpr_No_Checks (E),
9513 Attribute_Name => Name_First,
9514 Expressions => New_List (
9515 Make_Integer_Literal (Loc, J)))));
9517 -- Generate:
9518 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9520 High_Bound := Make_Temporary (Loc, 'B');
9521 Insert_Action (E,
9522 Make_Object_Declaration (Loc,
9523 Defining_Identifier => High_Bound,
9524 Object_Definition =>
9525 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9526 Constant_Present => True,
9527 Expression =>
9528 Make_Attribute_Reference (Loc,
9529 Prefix => Duplicate_Subexpr_No_Checks (E),
9530 Attribute_Name => Name_Last,
9531 Expressions => New_List (
9532 Make_Integer_Literal (Loc, J)))));
9534 Append_To (List_Constr,
9535 Make_Range (Loc,
9536 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9537 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9539 Index_Typ := Next_Index (Index_Typ);
9540 end loop;
9542 elsif Is_Class_Wide_Type (Unc_Typ) then
9543 declare
9544 CW_Subtype : Entity_Id;
9545 EQ_Typ : Entity_Id := Empty;
9547 begin
9548 -- A class-wide equivalent type is not needed on VM targets
9549 -- because the VM back-ends handle the class-wide object
9550 -- initialization itself (and doesn't need or want the
9551 -- additional intermediate type to handle the assignment).
9553 if Expander_Active and then Tagged_Type_Expansion then
9555 -- If this is the class-wide type of a completion that is a
9556 -- record subtype, set the type of the class-wide type to be
9557 -- the full base type, for use in the expanded code for the
9558 -- equivalent type. Should this be done earlier when the
9559 -- completion is analyzed ???
9561 if Is_Private_Type (Etype (Unc_Typ))
9562 and then
9563 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9564 then
9565 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9566 end if;
9568 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9569 end if;
9571 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9572 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9573 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9575 return New_Occurrence_Of (CW_Subtype, Loc);
9576 end;
9578 -- Indefinite record type with discriminants
9580 else
9581 D := First_Discriminant (Unc_Typ);
9582 while Present (D) loop
9583 Append_To (List_Constr,
9584 Make_Selected_Component (Loc,
9585 Prefix => Duplicate_Subexpr_No_Checks (E),
9586 Selector_Name => New_Occurrence_Of (D, Loc)));
9588 Next_Discriminant (D);
9589 end loop;
9590 end if;
9592 return
9593 Make_Subtype_Indication (Loc,
9594 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9595 Constraint =>
9596 Make_Index_Or_Discriminant_Constraint (Loc,
9597 Constraints => List_Constr));
9598 end Make_Subtype_From_Expr;
9600 ---------------
9601 -- Map_Types --
9602 ---------------
9604 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9606 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9607 -- avoid deep indentation of code.
9609 -- NOTE: Routines which deal with discriminant mapping operate on the
9610 -- [underlying/record] full view of various types because those views
9611 -- contain all discriminants and stored constraints.
9613 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9614 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9615 -- overriding chain starting from Prim whose dispatching type is parent
9616 -- type Par_Typ and add a mapping between the result and primitive Prim.
9618 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9619 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9620 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9621 -- if no such primitive is available.
9623 function Build_Chain
9624 (Par_Typ : Entity_Id;
9625 Deriv_Typ : Entity_Id) return Elist_Id;
9626 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9627 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9628 -- list has the form:
9630 -- head tail
9631 -- v v
9632 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9634 -- Note that Par_Typ is not part of the resulting derivation chain
9636 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9637 -- Return the view of type Typ which could potentially contains either
9638 -- the discriminants or stored constraints of the type.
9640 function Find_Discriminant_Value
9641 (Discr : Entity_Id;
9642 Par_Typ : Entity_Id;
9643 Deriv_Typ : Entity_Id;
9644 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9645 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9646 -- in the derivation chain starting from parent type Par_Typ leading to
9647 -- derived type Deriv_Typ. The returned value is one of the following:
9649 -- * An entity which is either a discriminant or a non-discriminant
9650 -- name, and renames/constraints Discr.
9652 -- * An expression which constraints Discr
9654 -- Typ_Elmt is an element of the derivation chain created by routine
9655 -- Build_Chain and denotes the current ancestor being examined.
9657 procedure Map_Discriminants
9658 (Par_Typ : Entity_Id;
9659 Deriv_Typ : Entity_Id);
9660 -- Map each discriminant of type Par_Typ to a meaningful constraint
9661 -- from the point of view of type Deriv_Typ.
9663 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9664 -- Map each primitive of type Par_Typ to a corresponding primitive of
9665 -- type Deriv_Typ.
9667 -------------------
9668 -- Add_Primitive --
9669 -------------------
9671 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9672 Par_Prim : Entity_Id;
9674 begin
9675 -- Inspect the inheritance chain through the Alias attribute and the
9676 -- overriding chain through the Overridden_Operation looking for an
9677 -- ancestor primitive with the appropriate dispatching type.
9679 Par_Prim := Prim;
9680 while Present (Par_Prim) loop
9681 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9682 Par_Prim := Ancestor_Primitive (Par_Prim);
9683 end loop;
9685 -- Create a mapping of the form:
9687 -- parent type primitive -> derived type primitive
9689 if Present (Par_Prim) then
9690 Type_Map.Set (Par_Prim, Prim);
9691 end if;
9692 end Add_Primitive;
9694 ------------------------
9695 -- Ancestor_Primitive --
9696 ------------------------
9698 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9699 Inher_Prim : constant Entity_Id := Alias (Subp);
9700 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9702 begin
9703 -- The current subprogram overrides an ancestor primitive
9705 if Present (Over_Prim) then
9706 return Over_Prim;
9708 -- The current subprogram is an internally generated alias of an
9709 -- inherited ancestor primitive.
9711 elsif Present (Inher_Prim) then
9712 return Inher_Prim;
9714 -- Otherwise the current subprogram is the root of the inheritance or
9715 -- overriding chain.
9717 else
9718 return Empty;
9719 end if;
9720 end Ancestor_Primitive;
9722 -----------------
9723 -- Build_Chain --
9724 -----------------
9726 function Build_Chain
9727 (Par_Typ : Entity_Id;
9728 Deriv_Typ : Entity_Id) return Elist_Id
9730 Anc_Typ : Entity_Id;
9731 Chain : Elist_Id;
9732 Curr_Typ : Entity_Id;
9734 begin
9735 Chain := New_Elmt_List;
9737 -- Add the derived type to the derivation chain
9739 Prepend_Elmt (Deriv_Typ, Chain);
9741 -- Examine all ancestors starting from the derived type climbing
9742 -- towards parent type Par_Typ.
9744 Curr_Typ := Deriv_Typ;
9745 loop
9746 -- Handle the case where the current type is a record which
9747 -- derives from a subtype.
9749 -- subtype Sub_Typ is Par_Typ ...
9750 -- type Deriv_Typ is Sub_Typ ...
9752 if Ekind (Curr_Typ) = E_Record_Type
9753 and then Present (Parent_Subtype (Curr_Typ))
9754 then
9755 Anc_Typ := Parent_Subtype (Curr_Typ);
9757 -- Handle the case where the current type is a record subtype of
9758 -- another subtype.
9760 -- subtype Sub_Typ1 is Par_Typ ...
9761 -- subtype Sub_Typ2 is Sub_Typ1 ...
9763 elsif Ekind (Curr_Typ) = E_Record_Subtype
9764 and then Present (Cloned_Subtype (Curr_Typ))
9765 then
9766 Anc_Typ := Cloned_Subtype (Curr_Typ);
9768 -- Otherwise use the direct parent type
9770 else
9771 Anc_Typ := Etype (Curr_Typ);
9772 end if;
9774 -- Use the first subtype when dealing with itypes
9776 if Is_Itype (Anc_Typ) then
9777 Anc_Typ := First_Subtype (Anc_Typ);
9778 end if;
9780 -- Work with the view which contains the discriminants and stored
9781 -- constraints.
9783 Anc_Typ := Discriminated_View (Anc_Typ);
9785 -- Stop the climb when either the parent type has been reached or
9786 -- there are no more ancestors left to examine.
9788 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9790 Prepend_Unique_Elmt (Anc_Typ, Chain);
9791 Curr_Typ := Anc_Typ;
9792 end loop;
9794 return Chain;
9795 end Build_Chain;
9797 ------------------------
9798 -- Discriminated_View --
9799 ------------------------
9801 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9802 T : Entity_Id;
9804 begin
9805 T := Typ;
9807 -- Use the [underlying] full view when dealing with private types
9808 -- because the view contains all inherited discriminants or stored
9809 -- constraints.
9811 if Is_Private_Type (T) then
9812 if Present (Underlying_Full_View (T)) then
9813 T := Underlying_Full_View (T);
9815 elsif Present (Full_View (T)) then
9816 T := Full_View (T);
9817 end if;
9818 end if;
9820 -- Use the underlying record view when the type is an extenstion of
9821 -- a parent type with unknown discriminants because the view contains
9822 -- all inherited discriminants or stored constraints.
9824 if Ekind (T) = E_Record_Type
9825 and then Present (Underlying_Record_View (T))
9826 then
9827 T := Underlying_Record_View (T);
9828 end if;
9830 return T;
9831 end Discriminated_View;
9833 -----------------------------
9834 -- Find_Discriminant_Value --
9835 -----------------------------
9837 function Find_Discriminant_Value
9838 (Discr : Entity_Id;
9839 Par_Typ : Entity_Id;
9840 Deriv_Typ : Entity_Id;
9841 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9843 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9844 Typ : constant Entity_Id := Node (Typ_Elmt);
9846 function Find_Constraint_Value
9847 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
9848 -- Given constraint Constr, find what it denotes. This is either:
9850 -- * An entity which is either a discriminant or a name
9852 -- * An expression
9854 ---------------------------
9855 -- Find_Constraint_Value --
9856 ---------------------------
9858 function Find_Constraint_Value
9859 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
9861 begin
9862 if Nkind (Constr) in N_Entity then
9864 -- The constraint denotes a discriminant of the curren type
9865 -- which renames the ancestor discriminant:
9867 -- vv
9868 -- type Typ (D1 : ...; DN : ...) is
9869 -- new Anc (Discr => D1) with ...
9870 -- ^^
9872 if Ekind (Constr) = E_Discriminant then
9874 -- The discriminant belongs to derived type Deriv_Typ. This
9875 -- is the final value for the ancestor discriminant as the
9876 -- derivations chain has been fully exhausted.
9878 if Typ = Deriv_Typ then
9879 return Constr;
9881 -- Otherwise the discriminant may be renamed or constrained
9882 -- at a lower level. Continue looking down the derivation
9883 -- chain.
9885 else
9886 return
9887 Find_Discriminant_Value
9888 (Discr => Constr,
9889 Par_Typ => Par_Typ,
9890 Deriv_Typ => Deriv_Typ,
9891 Typ_Elmt => Next_Elmt (Typ_Elmt));
9892 end if;
9894 -- Otherwise the constraint denotes a reference to some name
9895 -- which results in a Girder discriminant:
9897 -- vvvv
9898 -- Name : ...;
9899 -- type Typ (D1 : ...; DN : ...) is
9900 -- new Anc (Discr => Name) with ...
9901 -- ^^^^
9903 -- Return the name as this is the proper constraint of the
9904 -- discriminant.
9906 else
9907 return Constr;
9908 end if;
9910 -- The constraint denotes a reference to a name
9912 elsif Is_Entity_Name (Constr) then
9913 return Find_Constraint_Value (Entity (Constr));
9915 -- Otherwise the current constraint is an expression which yields
9916 -- a Girder discriminant:
9918 -- type Typ (D1 : ...; DN : ...) is
9919 -- new Anc (Discr => <expression>) with ...
9920 -- ^^^^^^^^^^
9922 -- Return the expression as this is the proper constraint of the
9923 -- discriminant.
9925 else
9926 return Constr;
9927 end if;
9928 end Find_Constraint_Value;
9930 -- Local variables
9932 Constrs : constant Elist_Id := Stored_Constraint (Typ);
9934 Constr_Elmt : Elmt_Id;
9935 Pos : Uint;
9936 Typ_Discr : Entity_Id;
9938 -- Start of processing for Find_Discriminant_Value
9940 begin
9941 -- The algorithm for finding the value of a discriminant works as
9942 -- follows. First, it recreates the derivation chain from Par_Typ
9943 -- to Deriv_Typ as a list:
9945 -- Par_Typ (shown for completeness)
9946 -- v
9947 -- Ancestor_N <-- head of chain
9948 -- v
9949 -- Ancestor_1
9950 -- v
9951 -- Deriv_Typ <-- tail of chain
9953 -- The algorithm then traces the fate of a parent discriminant down
9954 -- the derivation chain. At each derivation level, the discriminant
9955 -- may be either inherited or constrained.
9957 -- 1) Discriminant is inherited: there are two cases, depending on
9958 -- which type is inheriting.
9960 -- 1.1) Deriv_Typ is inheriting:
9962 -- type Ancestor (D_1 : ...) is tagged ...
9963 -- type Deriv_Typ is new Ancestor ...
9965 -- In this case the inherited discriminant is the final value of
9966 -- the parent discriminant because the end of the derivation chain
9967 -- has been reached.
9969 -- 1.2) Some other type is inheriting:
9971 -- type Ancestor_1 (D_1 : ...) is tagged ...
9972 -- type Ancestor_2 is new Ancestor_1 ...
9974 -- In this case the algorithm continues to trace the fate of the
9975 -- inherited discriminant down the derivation chain because it may
9976 -- be further inherited or constrained.
9978 -- 2) Discriminant is constrained: there are three cases, depending
9979 -- on what the constraint is.
9981 -- 2.1) The constraint is another discriminant (aka renaming):
9983 -- type Ancestor_1 (D_1 : ...) is tagged ...
9984 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
9986 -- In this case the constraining discriminant becomes the one to
9987 -- track down the derivation chain. The algorithm already knows
9988 -- that D_2 constrains D_1, therefore if the algorithm finds the
9989 -- value of D_2, then this would also be the value for D_1.
9991 -- 2.2) The constraint is a name (aka Girder):
9993 -- Name : ...
9994 -- type Ancestor_1 (D_1 : ...) is tagged ...
9995 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
9997 -- In this case the name is the final value of D_1 because the
9998 -- discriminant cannot be further constrained.
10000 -- 2.3) The constraint is an expression (aka Girder):
10002 -- type Ancestor_1 (D_1 : ...) is tagged ...
10003 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10005 -- Similar to 2.2, the expression is the final value of D_1
10007 Pos := Uint_1;
10009 -- When a derived type constrains its parent type, all constaints
10010 -- appear in the Stored_Constraint list. Examine the list looking
10011 -- for a positional match.
10013 if Present (Constrs) then
10014 Constr_Elmt := First_Elmt (Constrs);
10015 while Present (Constr_Elmt) loop
10017 -- The position of the current constraint matches that of the
10018 -- ancestor discriminant.
10020 if Pos = Discr_Pos then
10021 return Find_Constraint_Value (Node (Constr_Elmt));
10022 end if;
10024 Next_Elmt (Constr_Elmt);
10025 Pos := Pos + 1;
10026 end loop;
10028 -- Otherwise the derived type does not constraint its parent type in
10029 -- which case it inherits the parent discriminants.
10031 else
10032 Typ_Discr := First_Discriminant (Typ);
10033 while Present (Typ_Discr) loop
10035 -- The position of the current discriminant matches that of the
10036 -- ancestor discriminant.
10038 if Pos = Discr_Pos then
10039 return Find_Constraint_Value (Typ_Discr);
10040 end if;
10042 Next_Discriminant (Typ_Discr);
10043 Pos := Pos + 1;
10044 end loop;
10045 end if;
10047 -- A discriminant must always have a corresponding value. This is
10048 -- either another discriminant, a name, or an expression. If this
10049 -- point is reached, them most likely the derivation chain employs
10050 -- the wrong views of types.
10052 pragma Assert (False);
10054 return Empty;
10055 end Find_Discriminant_Value;
10057 -----------------------
10058 -- Map_Discriminants --
10059 -----------------------
10061 procedure Map_Discriminants
10062 (Par_Typ : Entity_Id;
10063 Deriv_Typ : Entity_Id)
10065 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10067 Discr : Entity_Id;
10068 Discr_Val : Node_Or_Entity_Id;
10070 begin
10071 -- Examine each discriminant of parent type Par_Typ and find a
10072 -- suitable value for it from the point of view of derived type
10073 -- Deriv_Typ.
10075 if Has_Discriminants (Par_Typ) then
10076 Discr := First_Discriminant (Par_Typ);
10077 while Present (Discr) loop
10078 Discr_Val :=
10079 Find_Discriminant_Value
10080 (Discr => Discr,
10081 Par_Typ => Par_Typ,
10082 Deriv_Typ => Deriv_Typ,
10083 Typ_Elmt => First_Elmt (Deriv_Chain));
10085 -- Create a mapping of the form:
10087 -- parent type discriminant -> value
10089 Type_Map.Set (Discr, Discr_Val);
10091 Next_Discriminant (Discr);
10092 end loop;
10093 end if;
10094 end Map_Discriminants;
10096 --------------------
10097 -- Map_Primitives --
10098 --------------------
10100 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10101 Deriv_Prim : Entity_Id;
10102 Par_Prim : Entity_Id;
10103 Par_Prims : Elist_Id;
10104 Prim_Elmt : Elmt_Id;
10106 begin
10107 -- Inspect the primitives of the derived type and determine whether
10108 -- they relate to the primitives of the parent type. If there is a
10109 -- meaningful relation, create a mapping of the form:
10111 -- parent type primitive -> perived type primitive
10113 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10114 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10115 while Present (Prim_Elmt) loop
10116 Deriv_Prim := Node (Prim_Elmt);
10118 if Is_Subprogram (Deriv_Prim)
10119 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10120 then
10121 Add_Primitive (Deriv_Prim, Par_Typ);
10122 end if;
10124 Next_Elmt (Prim_Elmt);
10125 end loop;
10126 end if;
10128 -- If the parent operation is an interface operation, the overriding
10129 -- indicator is not present. Instead, we get from the interface
10130 -- operation the primitive of the current type that implements it.
10132 if Is_Interface (Par_Typ) then
10133 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10135 if Present (Par_Prims) then
10136 Prim_Elmt := First_Elmt (Par_Prims);
10138 while Present (Prim_Elmt) loop
10139 Par_Prim := Node (Prim_Elmt);
10140 Deriv_Prim :=
10141 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10143 if Present (Deriv_Prim) then
10144 Type_Map.Set (Par_Prim, Deriv_Prim);
10145 end if;
10147 Next_Elmt (Prim_Elmt);
10148 end loop;
10149 end if;
10150 end if;
10151 end Map_Primitives;
10153 -- Start of processing for Map_Types
10155 begin
10156 -- Nothing to do if there are no types to work with
10158 if No (Parent_Type) or else No (Derived_Type) then
10159 return;
10161 -- Nothing to do if the mapping already exists
10163 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10164 return;
10166 -- Nothing to do if both types are not tagged. Note that untagged types
10167 -- do not have primitive operations and their discriminants are already
10168 -- handled by gigi.
10170 elsif not Is_Tagged_Type (Parent_Type)
10171 or else not Is_Tagged_Type (Derived_Type)
10172 then
10173 return;
10174 end if;
10176 -- Create a mapping of the form
10178 -- parent type -> derived type
10180 -- to prevent any subsequent attempts to produce the same relations
10182 Type_Map.Set (Parent_Type, Derived_Type);
10184 -- Create mappings of the form
10186 -- parent type discriminant -> derived type discriminant
10187 -- <or>
10188 -- parent type discriminant -> constraint
10190 -- Note that mapping of discriminants breaks privacy because it needs to
10191 -- work with those views which contains the discriminants and any stored
10192 -- constraints.
10194 Map_Discriminants
10195 (Par_Typ => Discriminated_View (Parent_Type),
10196 Deriv_Typ => Discriminated_View (Derived_Type));
10198 -- Create mappings of the form
10200 -- parent type primitive -> derived type primitive
10202 Map_Primitives
10203 (Par_Typ => Parent_Type,
10204 Deriv_Typ => Derived_Type);
10205 end Map_Types;
10207 ----------------------------
10208 -- Matching_Standard_Type --
10209 ----------------------------
10211 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10212 pragma Assert (Is_Scalar_Type (Typ));
10213 Siz : constant Uint := Esize (Typ);
10215 begin
10216 -- Floating-point cases
10218 if Is_Floating_Point_Type (Typ) then
10219 if Siz <= Esize (Standard_Short_Float) then
10220 return Standard_Short_Float;
10221 elsif Siz <= Esize (Standard_Float) then
10222 return Standard_Float;
10223 elsif Siz <= Esize (Standard_Long_Float) then
10224 return Standard_Long_Float;
10225 elsif Siz <= Esize (Standard_Long_Long_Float) then
10226 return Standard_Long_Long_Float;
10227 else
10228 raise Program_Error;
10229 end if;
10231 -- Integer cases (includes fixed-point types)
10233 -- Unsigned integer cases (includes normal enumeration types)
10235 elsif Is_Unsigned_Type (Typ) then
10236 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10237 return Standard_Short_Short_Unsigned;
10238 elsif Siz <= Esize (Standard_Short_Unsigned) then
10239 return Standard_Short_Unsigned;
10240 elsif Siz <= Esize (Standard_Unsigned) then
10241 return Standard_Unsigned;
10242 elsif Siz <= Esize (Standard_Long_Unsigned) then
10243 return Standard_Long_Unsigned;
10244 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10245 return Standard_Long_Long_Unsigned;
10246 else
10247 raise Program_Error;
10248 end if;
10250 -- Signed integer cases
10252 else
10253 if Siz <= Esize (Standard_Short_Short_Integer) then
10254 return Standard_Short_Short_Integer;
10255 elsif Siz <= Esize (Standard_Short_Integer) then
10256 return Standard_Short_Integer;
10257 elsif Siz <= Esize (Standard_Integer) then
10258 return Standard_Integer;
10259 elsif Siz <= Esize (Standard_Long_Integer) then
10260 return Standard_Long_Integer;
10261 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10262 return Standard_Long_Long_Integer;
10263 else
10264 raise Program_Error;
10265 end if;
10266 end if;
10267 end Matching_Standard_Type;
10269 -----------------------------
10270 -- May_Generate_Large_Temp --
10271 -----------------------------
10273 -- At the current time, the only types that we return False for (i.e. where
10274 -- we decide we know they cannot generate large temps) are ones where we
10275 -- know the size is 256 bits or less at compile time, and we are still not
10276 -- doing a thorough job on arrays and records ???
10278 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10279 begin
10280 if not Size_Known_At_Compile_Time (Typ) then
10281 return False;
10283 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10284 return False;
10286 elsif Is_Array_Type (Typ)
10287 and then Present (Packed_Array_Impl_Type (Typ))
10288 then
10289 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10291 -- We could do more here to find other small types ???
10293 else
10294 return True;
10295 end if;
10296 end May_Generate_Large_Temp;
10298 ------------------------
10299 -- Needs_Finalization --
10300 ------------------------
10302 function Needs_Finalization (Typ : Entity_Id) return Boolean is
10303 function Has_Some_Controlled_Component
10304 (Input_Typ : Entity_Id) return Boolean;
10305 -- Determine whether type Input_Typ has at least one controlled
10306 -- component.
10308 -----------------------------------
10309 -- Has_Some_Controlled_Component --
10310 -----------------------------------
10312 function Has_Some_Controlled_Component
10313 (Input_Typ : Entity_Id) return Boolean
10315 Comp : Entity_Id;
10317 begin
10318 -- When a type is already frozen and has at least one controlled
10319 -- component, or is manually decorated, it is sufficient to inspect
10320 -- flag Has_Controlled_Component.
10322 if Has_Controlled_Component (Input_Typ) then
10323 return True;
10325 -- Otherwise inspect the internals of the type
10327 elsif not Is_Frozen (Input_Typ) then
10328 if Is_Array_Type (Input_Typ) then
10329 return Needs_Finalization (Component_Type (Input_Typ));
10331 elsif Is_Record_Type (Input_Typ) then
10332 Comp := First_Component (Input_Typ);
10333 while Present (Comp) loop
10334 if Needs_Finalization (Etype (Comp)) then
10335 return True;
10336 end if;
10338 Next_Component (Comp);
10339 end loop;
10340 end if;
10341 end if;
10343 return False;
10344 end Has_Some_Controlled_Component;
10346 -- Start of processing for Needs_Finalization
10348 begin
10349 -- Certain run-time configurations and targets do not provide support
10350 -- for controlled types.
10352 if Restriction_Active (No_Finalization) then
10353 return False;
10355 -- C++ types are not considered controlled. It is assumed that the non-
10356 -- Ada side will handle their clean up.
10358 elsif Convention (Typ) = Convention_CPP then
10359 return False;
10361 -- Class-wide types are treated as controlled because derivations from
10362 -- the root type may introduce controlled components.
10364 elsif Is_Class_Wide_Type (Typ) then
10365 return True;
10367 -- Concurrent types are controlled as long as their corresponding record
10368 -- is controlled.
10370 elsif Is_Concurrent_Type (Typ)
10371 and then Present (Corresponding_Record_Type (Typ))
10372 and then Needs_Finalization (Corresponding_Record_Type (Typ))
10373 then
10374 return True;
10376 -- Otherwise the type is controlled when it is either derived from type
10377 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10378 -- contains at least one controlled component.
10380 else
10381 return
10382 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
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
10395 -- If we have no initialization of any kind, then we don't need to place
10396 -- any restrictions on the address clause, because the object will be
10397 -- elaborated after the address clause is evaluated. This happens if the
10398 -- declaration has no initial expression, or the type has no implicit
10399 -- initialization, or the object is imported.
10401 -- The same holds for all initialized scalar types and all access types.
10402 -- Packed bit arrays of size up to 64 are represented using a modular
10403 -- type with an initialization (to zero) and can be processed like other
10404 -- initialized scalar types.
10406 -- If the type is controlled, code to attach the object to a
10407 -- finalization chain is generated at the point of declaration, and
10408 -- therefore the elaboration of the object cannot be delayed: the
10409 -- address expression must be a constant.
10411 if No (Expression (Decl))
10412 and then not Needs_Finalization (Typ)
10413 and then
10414 (not Has_Non_Null_Base_Init_Proc (Typ)
10415 or else Is_Imported (Defining_Identifier (Decl)))
10416 then
10417 return False;
10419 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10420 or else Is_Access_Type (Typ)
10421 or else
10422 (Is_Bit_Packed_Array (Typ)
10423 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10424 then
10425 return False;
10427 else
10429 -- Otherwise, we require the address clause to be constant because
10430 -- the call to the initialization procedure (or the attach code) has
10431 -- to happen at the point of the declaration.
10433 -- Actually the IP call has been moved to the freeze actions anyway,
10434 -- so maybe we can relax this restriction???
10436 return True;
10437 end if;
10438 end Needs_Constant_Address;
10440 ----------------------------
10441 -- New_Class_Wide_Subtype --
10442 ----------------------------
10444 function New_Class_Wide_Subtype
10445 (CW_Typ : Entity_Id;
10446 N : Node_Id) return Entity_Id
10448 Res : constant Entity_Id := Create_Itype (E_Void, N);
10449 Res_Name : constant Name_Id := Chars (Res);
10450 Res_Scope : constant Entity_Id := Scope (Res);
10452 begin
10453 Copy_Node (CW_Typ, Res);
10454 Set_Comes_From_Source (Res, False);
10455 Set_Sloc (Res, Sloc (N));
10456 Set_Is_Itype (Res);
10457 Set_Associated_Node_For_Itype (Res, N);
10458 Set_Is_Public (Res, False); -- By default, may be changed below.
10459 Set_Public_Status (Res);
10460 Set_Chars (Res, Res_Name);
10461 Set_Scope (Res, Res_Scope);
10462 Set_Ekind (Res, E_Class_Wide_Subtype);
10463 Set_Next_Entity (Res, Empty);
10464 Set_Etype (Res, Base_Type (CW_Typ));
10465 Set_Is_Frozen (Res, False);
10466 Set_Freeze_Node (Res, Empty);
10467 return (Res);
10468 end New_Class_Wide_Subtype;
10470 --------------------------------
10471 -- Non_Limited_Designated_Type --
10472 ---------------------------------
10474 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10475 Desig : constant Entity_Id := Designated_Type (T);
10476 begin
10477 if Has_Non_Limited_View (Desig) then
10478 return Non_Limited_View (Desig);
10479 else
10480 return Desig;
10481 end if;
10482 end Non_Limited_Designated_Type;
10484 -----------------------------------
10485 -- OK_To_Do_Constant_Replacement --
10486 -----------------------------------
10488 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10489 ES : constant Entity_Id := Scope (E);
10490 CS : Entity_Id;
10492 begin
10493 -- Do not replace statically allocated objects, because they may be
10494 -- modified outside the current scope.
10496 if Is_Statically_Allocated (E) then
10497 return False;
10499 -- Do not replace aliased or volatile objects, since we don't know what
10500 -- else might change the value.
10502 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10503 return False;
10505 -- Debug flag -gnatdM disconnects this optimization
10507 elsif Debug_Flag_MM then
10508 return False;
10510 -- Otherwise check scopes
10512 else
10513 CS := Current_Scope;
10515 loop
10516 -- If we are in right scope, replacement is safe
10518 if CS = ES then
10519 return True;
10521 -- Packages do not affect the determination of safety
10523 elsif Ekind (CS) = E_Package then
10524 exit when CS = Standard_Standard;
10525 CS := Scope (CS);
10527 -- Blocks do not affect the determination of safety
10529 elsif Ekind (CS) = E_Block then
10530 CS := Scope (CS);
10532 -- Loops do not affect the determination of safety. Note that we
10533 -- kill all current values on entry to a loop, so we are just
10534 -- talking about processing within a loop here.
10536 elsif Ekind (CS) = E_Loop then
10537 CS := Scope (CS);
10539 -- Otherwise, the reference is dubious, and we cannot be sure that
10540 -- it is safe to do the replacement.
10542 else
10543 exit;
10544 end if;
10545 end loop;
10547 return False;
10548 end if;
10549 end OK_To_Do_Constant_Replacement;
10551 ------------------------------------
10552 -- Possible_Bit_Aligned_Component --
10553 ------------------------------------
10555 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10556 begin
10557 -- Do not process an unanalyzed node because it is not yet decorated and
10558 -- most checks performed below will fail.
10560 if not Analyzed (N) then
10561 return False;
10562 end if;
10564 case Nkind (N) is
10566 -- Case of indexed component
10568 when N_Indexed_Component =>
10569 declare
10570 P : constant Node_Id := Prefix (N);
10571 Ptyp : constant Entity_Id := Etype (P);
10573 begin
10574 -- If we know the component size and it is less than 64, then
10575 -- we are definitely OK. The back end always does assignment of
10576 -- misaligned small objects correctly.
10578 if Known_Static_Component_Size (Ptyp)
10579 and then Component_Size (Ptyp) <= 64
10580 then
10581 return False;
10583 -- Otherwise, we need to test the prefix, to see if we are
10584 -- indexing from a possibly unaligned component.
10586 else
10587 return Possible_Bit_Aligned_Component (P);
10588 end if;
10589 end;
10591 -- Case of selected component
10593 when N_Selected_Component =>
10594 declare
10595 P : constant Node_Id := Prefix (N);
10596 Comp : constant Entity_Id := Entity (Selector_Name (N));
10598 begin
10599 -- If there is no component clause, then we are in the clear
10600 -- since the back end will never misalign a large component
10601 -- unless it is forced to do so. In the clear means we need
10602 -- only the recursive test on the prefix.
10604 if Component_May_Be_Bit_Aligned (Comp) then
10605 return True;
10606 else
10607 return Possible_Bit_Aligned_Component (P);
10608 end if;
10609 end;
10611 -- For a slice, test the prefix, if that is possibly misaligned,
10612 -- then for sure the slice is.
10614 when N_Slice =>
10615 return Possible_Bit_Aligned_Component (Prefix (N));
10617 -- For an unchecked conversion, check whether the expression may
10618 -- be bit-aligned.
10620 when N_Unchecked_Type_Conversion =>
10621 return Possible_Bit_Aligned_Component (Expression (N));
10623 -- If we have none of the above, it means that we have fallen off the
10624 -- top testing prefixes recursively, and we now have a stand alone
10625 -- object, where we don't have a problem, unless this is a renaming,
10626 -- in which case we need to look into the renamed object.
10628 when others =>
10629 if Is_Entity_Name (N)
10630 and then Present (Renamed_Object (Entity (N)))
10631 then
10632 return
10633 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10634 else
10635 return False;
10636 end if;
10637 end case;
10638 end Possible_Bit_Aligned_Component;
10640 -----------------------------------------------
10641 -- Process_Statements_For_Controlled_Objects --
10642 -----------------------------------------------
10644 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10645 Loc : constant Source_Ptr := Sloc (N);
10647 function Are_Wrapped (L : List_Id) return Boolean;
10648 -- Determine whether list L contains only one statement which is a block
10650 function Wrap_Statements_In_Block
10651 (L : List_Id;
10652 Scop : Entity_Id := Current_Scope) return Node_Id;
10653 -- Given a list of statements L, wrap it in a block statement and return
10654 -- the generated node. Scop is either the current scope or the scope of
10655 -- the context (if applicable).
10657 -----------------
10658 -- Are_Wrapped --
10659 -----------------
10661 function Are_Wrapped (L : List_Id) return Boolean is
10662 Stmt : constant Node_Id := First (L);
10663 begin
10664 return
10665 Present (Stmt)
10666 and then No (Next (Stmt))
10667 and then Nkind (Stmt) = N_Block_Statement;
10668 end Are_Wrapped;
10670 ------------------------------
10671 -- Wrap_Statements_In_Block --
10672 ------------------------------
10674 function Wrap_Statements_In_Block
10675 (L : List_Id;
10676 Scop : Entity_Id := Current_Scope) return Node_Id
10678 Block_Id : Entity_Id;
10679 Block_Nod : Node_Id;
10680 Iter_Loop : Entity_Id;
10682 begin
10683 Block_Nod :=
10684 Make_Block_Statement (Loc,
10685 Declarations => No_List,
10686 Handled_Statement_Sequence =>
10687 Make_Handled_Sequence_Of_Statements (Loc,
10688 Statements => L));
10690 -- Create a label for the block in case the block needs to manage the
10691 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10693 Add_Block_Identifier (Block_Nod, Block_Id);
10695 -- When wrapping the statements of an iterator loop, check whether
10696 -- the loop requires secondary stack management and if so, propagate
10697 -- the appropriate flags to the block. This ensures that the cursor
10698 -- is properly cleaned up at each iteration of the loop.
10700 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10702 if Present (Iter_Loop) then
10703 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10705 -- Secondary stack reclamation is suppressed when the associated
10706 -- iterator loop contains a return statement which uses the stack.
10708 Set_Sec_Stack_Needed_For_Return
10709 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10710 end if;
10712 return Block_Nod;
10713 end Wrap_Statements_In_Block;
10715 -- Local variables
10717 Block : Node_Id;
10719 -- Start of processing for Process_Statements_For_Controlled_Objects
10721 begin
10722 -- Whenever a non-handled statement list is wrapped in a block, the
10723 -- block must be explicitly analyzed to redecorate all entities in the
10724 -- list and ensure that a finalizer is properly built.
10726 case Nkind (N) is
10727 when N_Conditional_Entry_Call
10728 | N_Elsif_Part
10729 | N_If_Statement
10730 | N_Selective_Accept
10732 -- Check the "then statements" for elsif parts and if statements
10734 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10735 and then not Is_Empty_List (Then_Statements (N))
10736 and then not Are_Wrapped (Then_Statements (N))
10737 and then Requires_Cleanup_Actions
10738 (Then_Statements (N), False, False)
10739 then
10740 Block := Wrap_Statements_In_Block (Then_Statements (N));
10741 Set_Then_Statements (N, New_List (Block));
10743 Analyze (Block);
10744 end if;
10746 -- Check the "else statements" for conditional entry calls, if
10747 -- statements and selective accepts.
10749 if Nkind_In (N, N_Conditional_Entry_Call,
10750 N_If_Statement,
10751 N_Selective_Accept)
10752 and then not Is_Empty_List (Else_Statements (N))
10753 and then not Are_Wrapped (Else_Statements (N))
10754 and then Requires_Cleanup_Actions
10755 (Else_Statements (N), False, False)
10756 then
10757 Block := Wrap_Statements_In_Block (Else_Statements (N));
10758 Set_Else_Statements (N, New_List (Block));
10760 Analyze (Block);
10761 end if;
10763 when N_Abortable_Part
10764 | N_Accept_Alternative
10765 | N_Case_Statement_Alternative
10766 | N_Delay_Alternative
10767 | N_Entry_Call_Alternative
10768 | N_Exception_Handler
10769 | N_Loop_Statement
10770 | N_Triggering_Alternative
10772 if not Is_Empty_List (Statements (N))
10773 and then not Are_Wrapped (Statements (N))
10774 and then Requires_Cleanup_Actions (Statements (N), False, False)
10775 then
10776 if Nkind (N) = N_Loop_Statement
10777 and then Present (Identifier (N))
10778 then
10779 Block :=
10780 Wrap_Statements_In_Block
10781 (L => Statements (N),
10782 Scop => Entity (Identifier (N)));
10783 else
10784 Block := Wrap_Statements_In_Block (Statements (N));
10785 end if;
10787 Set_Statements (N, New_List (Block));
10788 Analyze (Block);
10789 end if;
10791 when others =>
10792 null;
10793 end case;
10794 end Process_Statements_For_Controlled_Objects;
10796 ------------------
10797 -- Power_Of_Two --
10798 ------------------
10800 function Power_Of_Two (N : Node_Id) return Nat is
10801 Typ : constant Entity_Id := Etype (N);
10802 pragma Assert (Is_Integer_Type (Typ));
10804 Siz : constant Nat := UI_To_Int (Esize (Typ));
10805 Val : Uint;
10807 begin
10808 if not Compile_Time_Known_Value (N) then
10809 return 0;
10811 else
10812 Val := Expr_Value (N);
10813 for J in 1 .. Siz - 1 loop
10814 if Val = Uint_2 ** J then
10815 return J;
10816 end if;
10817 end loop;
10819 return 0;
10820 end if;
10821 end Power_Of_Two;
10823 ----------------------
10824 -- Remove_Init_Call --
10825 ----------------------
10827 function Remove_Init_Call
10828 (Var : Entity_Id;
10829 Rep_Clause : Node_Id) return Node_Id
10831 Par : constant Node_Id := Parent (Var);
10832 Typ : constant Entity_Id := Etype (Var);
10834 Init_Proc : Entity_Id;
10835 -- Initialization procedure for Typ
10837 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
10838 -- Look for init call for Var starting at From and scanning the
10839 -- enclosing list until Rep_Clause or the end of the list is reached.
10841 ----------------------------
10842 -- Find_Init_Call_In_List --
10843 ----------------------------
10845 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
10846 Init_Call : Node_Id;
10848 begin
10849 Init_Call := From;
10850 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
10851 if Nkind (Init_Call) = N_Procedure_Call_Statement
10852 and then Is_Entity_Name (Name (Init_Call))
10853 and then Entity (Name (Init_Call)) = Init_Proc
10854 then
10855 return Init_Call;
10856 end if;
10858 Next (Init_Call);
10859 end loop;
10861 return Empty;
10862 end Find_Init_Call_In_List;
10864 Init_Call : Node_Id;
10866 -- Start of processing for Find_Init_Call
10868 begin
10869 if Present (Initialization_Statements (Var)) then
10870 Init_Call := Initialization_Statements (Var);
10871 Set_Initialization_Statements (Var, Empty);
10873 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
10875 -- No init proc for the type, so obviously no call to be found
10877 return Empty;
10879 else
10880 -- We might be able to handle other cases below by just properly
10881 -- setting Initialization_Statements at the point where the init proc
10882 -- call is generated???
10884 Init_Proc := Base_Init_Proc (Typ);
10886 -- First scan the list containing the declaration of Var
10888 Init_Call := Find_Init_Call_In_List (From => Next (Par));
10890 -- If not found, also look on Var's freeze actions list, if any,
10891 -- since the init call may have been moved there (case of an address
10892 -- clause applying to Var).
10894 if No (Init_Call) and then Present (Freeze_Node (Var)) then
10895 Init_Call :=
10896 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
10897 end if;
10899 -- If the initialization call has actuals that use the secondary
10900 -- stack, the call may have been wrapped into a temporary block, in
10901 -- which case the block itself has to be removed.
10903 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
10904 declare
10905 Blk : constant Node_Id := Next (Par);
10906 begin
10907 if Present
10908 (Find_Init_Call_In_List
10909 (First (Statements (Handled_Statement_Sequence (Blk)))))
10910 then
10911 Init_Call := Blk;
10912 end if;
10913 end;
10914 end if;
10915 end if;
10917 if Present (Init_Call) then
10918 Remove (Init_Call);
10919 end if;
10920 return Init_Call;
10921 end Remove_Init_Call;
10923 -------------------------
10924 -- Remove_Side_Effects --
10925 -------------------------
10927 procedure Remove_Side_Effects
10928 (Exp : Node_Id;
10929 Name_Req : Boolean := False;
10930 Renaming_Req : Boolean := False;
10931 Variable_Ref : Boolean := False;
10932 Related_Id : Entity_Id := Empty;
10933 Is_Low_Bound : Boolean := False;
10934 Is_High_Bound : Boolean := False;
10935 Check_Side_Effects : Boolean := True)
10937 function Build_Temporary
10938 (Loc : Source_Ptr;
10939 Id : Character;
10940 Related_Nod : Node_Id := Empty) return Entity_Id;
10941 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
10942 -- is present (xxx is taken from the Chars field of Related_Nod),
10943 -- otherwise it generates an internal temporary.
10945 ---------------------
10946 -- Build_Temporary --
10947 ---------------------
10949 function Build_Temporary
10950 (Loc : Source_Ptr;
10951 Id : Character;
10952 Related_Nod : Node_Id := Empty) return Entity_Id
10954 Temp_Nam : Name_Id;
10956 begin
10957 -- The context requires an external symbol
10959 if Present (Related_Id) then
10960 if Is_Low_Bound then
10961 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
10962 else pragma Assert (Is_High_Bound);
10963 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
10964 end if;
10966 return Make_Defining_Identifier (Loc, Temp_Nam);
10968 -- Otherwise generate an internal temporary
10970 else
10971 return Make_Temporary (Loc, Id, Related_Nod);
10972 end if;
10973 end Build_Temporary;
10975 -- Local variables
10977 Loc : constant Source_Ptr := Sloc (Exp);
10978 Exp_Type : constant Entity_Id := Etype (Exp);
10979 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
10980 Def_Id : Entity_Id;
10981 E : Node_Id;
10982 New_Exp : Node_Id;
10983 Ptr_Typ_Decl : Node_Id;
10984 Ref_Type : Entity_Id;
10985 Res : Node_Id;
10987 -- Start of processing for Remove_Side_Effects
10989 begin
10990 -- Handle cases in which there is nothing to do. In GNATprove mode,
10991 -- removal of side effects is useful for the light expansion of
10992 -- renamings. This removal should only occur when not inside a
10993 -- generic and not doing a pre-analysis.
10995 if not Expander_Active
10996 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
10997 then
10998 return;
11000 -- Cannot generate temporaries if the invocation to remove side effects
11001 -- was issued too early and the type of the expression is not resolved
11002 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11003 -- Remove_Side_Effects).
11005 elsif No (Exp_Type)
11006 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11007 then
11008 return;
11010 -- Nothing to do if prior expansion determined that a function call does
11011 -- not require side effect removal.
11013 elsif Nkind (Exp) = N_Function_Call
11014 and then No_Side_Effect_Removal (Exp)
11015 then
11016 return;
11018 -- No action needed for side-effect free expressions
11020 elsif Check_Side_Effects
11021 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11022 then
11023 return;
11024 end if;
11026 -- The remaining processing is done with all checks suppressed
11028 -- Note: from now on, don't use return statements, instead do a goto
11029 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11031 Scope_Suppress.Suppress := (others => True);
11033 -- If this is an elementary or a small not by-reference record type, and
11034 -- we need to capture the value, just make a constant; this is cheap and
11035 -- objects of both kinds of types can be bit aligned, so it might not be
11036 -- possible to generate a reference to them. Likewise if this is not a
11037 -- name reference, except for a type conversion because we would enter
11038 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11039 -- type has predicates (and type conversions need a specific treatment
11040 -- anyway, see below). Also do it if we have a volatile reference and
11041 -- Name_Req is not set (see comments for Side_Effect_Free).
11043 if (Is_Elementary_Type (Exp_Type)
11044 or else (Is_Record_Type (Exp_Type)
11045 and then Known_Static_RM_Size (Exp_Type)
11046 and then RM_Size (Exp_Type) <= 64
11047 and then not Has_Discriminants (Exp_Type)
11048 and then not Is_By_Reference_Type (Exp_Type)))
11049 and then (Variable_Ref
11050 or else (not Is_Name_Reference (Exp)
11051 and then Nkind (Exp) /= N_Type_Conversion)
11052 or else (not Name_Req
11053 and then Is_Volatile_Reference (Exp)))
11054 then
11055 Def_Id := Build_Temporary (Loc, 'R', Exp);
11056 Set_Etype (Def_Id, Exp_Type);
11057 Res := New_Occurrence_Of (Def_Id, Loc);
11059 -- If the expression is a packed reference, it must be reanalyzed and
11060 -- expanded, depending on context. This is the case for actuals where
11061 -- a constraint check may capture the actual before expansion of the
11062 -- call is complete.
11064 if Nkind (Exp) = N_Indexed_Component
11065 and then Is_Packed (Etype (Prefix (Exp)))
11066 then
11067 Set_Analyzed (Exp, False);
11068 Set_Analyzed (Prefix (Exp), False);
11069 end if;
11071 -- Generate:
11072 -- Rnn : Exp_Type renames Expr;
11074 if Renaming_Req then
11075 E :=
11076 Make_Object_Renaming_Declaration (Loc,
11077 Defining_Identifier => Def_Id,
11078 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11079 Name => Relocate_Node (Exp));
11081 -- Generate:
11082 -- Rnn : constant Exp_Type := Expr;
11084 else
11085 E :=
11086 Make_Object_Declaration (Loc,
11087 Defining_Identifier => Def_Id,
11088 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11089 Constant_Present => True,
11090 Expression => Relocate_Node (Exp));
11092 Set_Assignment_OK (E);
11093 end if;
11095 Insert_Action (Exp, E);
11097 -- If the expression has the form v.all then we can just capture the
11098 -- pointer, and then do an explicit dereference on the result, but
11099 -- this is not right if this is a volatile reference.
11101 elsif Nkind (Exp) = N_Explicit_Dereference
11102 and then not Is_Volatile_Reference (Exp)
11103 then
11104 Def_Id := Build_Temporary (Loc, 'R', Exp);
11105 Res :=
11106 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11108 Insert_Action (Exp,
11109 Make_Object_Declaration (Loc,
11110 Defining_Identifier => Def_Id,
11111 Object_Definition =>
11112 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11113 Constant_Present => True,
11114 Expression => Relocate_Node (Prefix (Exp))));
11116 -- Similar processing for an unchecked conversion of an expression of
11117 -- the form v.all, where we want the same kind of treatment.
11119 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11120 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11121 then
11122 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11123 goto Leave;
11125 -- If this is a type conversion, leave the type conversion and remove
11126 -- the side effects in the expression. This is important in several
11127 -- circumstances: for change of representations, and also when this is a
11128 -- view conversion to a smaller object, where gigi can end up creating
11129 -- its own temporary of the wrong size.
11131 elsif Nkind (Exp) = N_Type_Conversion then
11132 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11134 -- Generating C code the type conversion of an access to constrained
11135 -- array type into an access to unconstrained array type involves
11136 -- initializing a fat pointer and the expression must be free of
11137 -- side effects to safely compute its bounds.
11139 if Modify_Tree_For_C
11140 and then Is_Access_Type (Etype (Exp))
11141 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11142 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11143 then
11144 Def_Id := Build_Temporary (Loc, 'R', Exp);
11145 Set_Etype (Def_Id, Exp_Type);
11146 Res := New_Occurrence_Of (Def_Id, Loc);
11148 Insert_Action (Exp,
11149 Make_Object_Declaration (Loc,
11150 Defining_Identifier => Def_Id,
11151 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11152 Constant_Present => True,
11153 Expression => Relocate_Node (Exp)));
11154 else
11155 goto Leave;
11156 end if;
11158 -- If this is an unchecked conversion that Gigi can't handle, make
11159 -- a copy or a use a renaming to capture the value.
11161 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11162 and then not Safe_Unchecked_Type_Conversion (Exp)
11163 then
11164 if CW_Or_Has_Controlled_Part (Exp_Type) then
11166 -- Use a renaming to capture the expression, rather than create
11167 -- a controlled temporary.
11169 Def_Id := Build_Temporary (Loc, 'R', Exp);
11170 Res := New_Occurrence_Of (Def_Id, Loc);
11172 Insert_Action (Exp,
11173 Make_Object_Renaming_Declaration (Loc,
11174 Defining_Identifier => Def_Id,
11175 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11176 Name => Relocate_Node (Exp)));
11178 else
11179 Def_Id := Build_Temporary (Loc, 'R', Exp);
11180 Set_Etype (Def_Id, Exp_Type);
11181 Res := New_Occurrence_Of (Def_Id, Loc);
11183 E :=
11184 Make_Object_Declaration (Loc,
11185 Defining_Identifier => Def_Id,
11186 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11187 Constant_Present => not Is_Variable (Exp),
11188 Expression => Relocate_Node (Exp));
11190 Set_Assignment_OK (E);
11191 Insert_Action (Exp, E);
11192 end if;
11194 -- For expressions that denote names, we can use a renaming scheme.
11195 -- This is needed for correctness in the case of a volatile object of
11196 -- a non-volatile type because the Make_Reference call of the "default"
11197 -- approach would generate an illegal access value (an access value
11198 -- cannot designate such an object - see Analyze_Reference).
11200 elsif Is_Name_Reference (Exp)
11202 -- We skip using this scheme if we have an object of a volatile
11203 -- type and we do not have Name_Req set true (see comments for
11204 -- Side_Effect_Free).
11206 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11207 then
11208 Def_Id := Build_Temporary (Loc, 'R', Exp);
11209 Res := New_Occurrence_Of (Def_Id, Loc);
11211 Insert_Action (Exp,
11212 Make_Object_Renaming_Declaration (Loc,
11213 Defining_Identifier => Def_Id,
11214 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11215 Name => Relocate_Node (Exp)));
11217 -- If this is a packed reference, or a selected component with
11218 -- a non-standard representation, a reference to the temporary
11219 -- will be replaced by a copy of the original expression (see
11220 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11221 -- elaborated by gigi, and is of course not to be replaced in-line
11222 -- by the expression it renames, which would defeat the purpose of
11223 -- removing the side-effect.
11225 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11226 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11227 then
11228 null;
11229 else
11230 Set_Is_Renaming_Of_Object (Def_Id, False);
11231 end if;
11233 -- Avoid generating a variable-sized temporary, by generating the
11234 -- reference just for the function call. The transformation could be
11235 -- refined to apply only when the array component is constrained by a
11236 -- discriminant???
11238 elsif Nkind (Exp) = N_Selected_Component
11239 and then Nkind (Prefix (Exp)) = N_Function_Call
11240 and then Is_Array_Type (Exp_Type)
11241 then
11242 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11243 goto Leave;
11245 -- Otherwise we generate a reference to the expression
11247 else
11248 -- An expression which is in SPARK mode is considered side effect
11249 -- free if the resulting value is captured by a variable or a
11250 -- constant.
11252 if GNATprove_Mode
11253 and then Nkind (Parent (Exp)) = N_Object_Declaration
11254 then
11255 goto Leave;
11257 -- When generating C code we cannot consider side effect free object
11258 -- declarations that have discriminants and are initialized by means
11259 -- of a function call since on this target there is no secondary
11260 -- stack to store the return value and the expander may generate an
11261 -- extra call to the function to compute the discriminant value. In
11262 -- addition, for targets that have secondary stack, the expansion of
11263 -- functions with side effects involves the generation of an access
11264 -- type to capture the return value stored in the secondary stack;
11265 -- by contrast when generating C code such expansion generates an
11266 -- internal object declaration (no access type involved) which must
11267 -- be identified here to avoid entering into a never-ending loop
11268 -- generating internal object declarations.
11270 elsif Modify_Tree_For_C
11271 and then Nkind (Parent (Exp)) = N_Object_Declaration
11272 and then
11273 (Nkind (Exp) /= N_Function_Call
11274 or else not Has_Discriminants (Exp_Type)
11275 or else Is_Internal_Name
11276 (Chars (Defining_Identifier (Parent (Exp)))))
11277 then
11278 goto Leave;
11279 end if;
11281 -- Special processing for function calls that return a limited type.
11282 -- We need to build a declaration that will enable build-in-place
11283 -- expansion of the call. This is not done if the context is already
11284 -- an object declaration, to prevent infinite recursion.
11286 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11287 -- to accommodate functions returning limited objects by reference.
11289 if Ada_Version >= Ada_2005
11290 and then Nkind (Exp) = N_Function_Call
11291 and then Is_Limited_View (Etype (Exp))
11292 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11293 then
11294 declare
11295 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11296 Decl : Node_Id;
11298 begin
11299 Decl :=
11300 Make_Object_Declaration (Loc,
11301 Defining_Identifier => Obj,
11302 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11303 Expression => Relocate_Node (Exp));
11305 Insert_Action (Exp, Decl);
11306 Set_Etype (Obj, Exp_Type);
11307 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11308 goto Leave;
11309 end;
11310 end if;
11312 Def_Id := Build_Temporary (Loc, 'R', Exp);
11314 -- The regular expansion of functions with side effects involves the
11315 -- generation of an access type to capture the return value found on
11316 -- the secondary stack. Since SPARK (and why) cannot process access
11317 -- types, use a different approach which ignores the secondary stack
11318 -- and "copies" the returned object.
11319 -- When generating C code, no need for a 'reference since the
11320 -- secondary stack is not supported.
11322 if GNATprove_Mode or Modify_Tree_For_C then
11323 Res := New_Occurrence_Of (Def_Id, Loc);
11324 Ref_Type := Exp_Type;
11326 -- Regular expansion utilizing an access type and 'reference
11328 else
11329 Res :=
11330 Make_Explicit_Dereference (Loc,
11331 Prefix => New_Occurrence_Of (Def_Id, Loc));
11333 -- Generate:
11334 -- type Ann is access all <Exp_Type>;
11336 Ref_Type := Make_Temporary (Loc, 'A');
11338 Ptr_Typ_Decl :=
11339 Make_Full_Type_Declaration (Loc,
11340 Defining_Identifier => Ref_Type,
11341 Type_Definition =>
11342 Make_Access_To_Object_Definition (Loc,
11343 All_Present => True,
11344 Subtype_Indication =>
11345 New_Occurrence_Of (Exp_Type, Loc)));
11347 Insert_Action (Exp, Ptr_Typ_Decl);
11348 end if;
11350 E := Exp;
11351 if Nkind (E) = N_Explicit_Dereference then
11352 New_Exp := Relocate_Node (Prefix (E));
11354 else
11355 E := Relocate_Node (E);
11357 -- Do not generate a 'reference in SPARK mode or C generation
11358 -- since the access type is not created in the first place.
11360 if GNATprove_Mode or Modify_Tree_For_C then
11361 New_Exp := E;
11363 -- Otherwise generate reference, marking the value as non-null
11364 -- since we know it cannot be null and we don't want a check.
11366 else
11367 New_Exp := Make_Reference (Loc, E);
11368 Set_Is_Known_Non_Null (Def_Id);
11369 end if;
11370 end if;
11372 if Is_Delayed_Aggregate (E) then
11374 -- The expansion of nested aggregates is delayed until the
11375 -- enclosing aggregate is expanded. As aggregates are often
11376 -- qualified, the predicate applies to qualified expressions as
11377 -- well, indicating that the enclosing aggregate has not been
11378 -- expanded yet. At this point the aggregate is part of a
11379 -- stand-alone declaration, and must be fully expanded.
11381 if Nkind (E) = N_Qualified_Expression then
11382 Set_Expansion_Delayed (Expression (E), False);
11383 Set_Analyzed (Expression (E), False);
11384 else
11385 Set_Expansion_Delayed (E, False);
11386 end if;
11388 Set_Analyzed (E, False);
11389 end if;
11391 -- Generating C code of object declarations that have discriminants
11392 -- and are initialized by means of a function call we propagate the
11393 -- discriminants of the parent type to the internally built object.
11394 -- This is needed to avoid generating an extra call to the called
11395 -- function.
11397 -- For example, if we generate here the following declaration, it
11398 -- will be expanded later adding an extra call to evaluate the value
11399 -- of the discriminant (needed to compute the size of the object).
11401 -- type Rec (D : Integer) is ...
11402 -- Obj : constant Rec := SomeFunc;
11404 if Modify_Tree_For_C
11405 and then Nkind (Parent (Exp)) = N_Object_Declaration
11406 and then Has_Discriminants (Exp_Type)
11407 and then Nkind (Exp) = N_Function_Call
11408 then
11409 Insert_Action (Exp,
11410 Make_Object_Declaration (Loc,
11411 Defining_Identifier => Def_Id,
11412 Object_Definition => New_Copy_Tree
11413 (Object_Definition (Parent (Exp))),
11414 Constant_Present => True,
11415 Expression => New_Exp));
11416 else
11417 Insert_Action (Exp,
11418 Make_Object_Declaration (Loc,
11419 Defining_Identifier => Def_Id,
11420 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11421 Constant_Present => True,
11422 Expression => New_Exp));
11423 end if;
11424 end if;
11426 -- Preserve the Assignment_OK flag in all copies, since at least one
11427 -- copy may be used in a context where this flag must be set (otherwise
11428 -- why would the flag be set in the first place).
11430 Set_Assignment_OK (Res, Assignment_OK (Exp));
11432 -- Finally rewrite the original expression and we are done
11434 Rewrite (Exp, Res);
11435 Analyze_And_Resolve (Exp, Exp_Type);
11437 <<Leave>>
11438 Scope_Suppress := Svg_Suppress;
11439 end Remove_Side_Effects;
11441 ------------------------
11442 -- Replace_References --
11443 ------------------------
11445 procedure Replace_References
11446 (Expr : Node_Id;
11447 Par_Typ : Entity_Id;
11448 Deriv_Typ : Entity_Id;
11449 Par_Obj : Entity_Id := Empty;
11450 Deriv_Obj : Entity_Id := Empty)
11452 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11453 -- Determine whether node Ref denotes some component of Deriv_Obj
11455 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11456 -- Substitute a reference to an entity with the corresponding value
11457 -- stored in table Type_Map.
11459 function Type_Of_Formal
11460 (Call : Node_Id;
11461 Actual : Node_Id) return Entity_Id;
11462 -- Find the type of the formal parameter which corresponds to actual
11463 -- parameter Actual in subprogram call Call.
11465 ----------------------
11466 -- Is_Deriv_Obj_Ref --
11467 ----------------------
11469 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11470 Par : constant Node_Id := Parent (Ref);
11472 begin
11473 -- Detect the folowing selected component form:
11475 -- Deriv_Obj.(something)
11477 return
11478 Nkind (Par) = N_Selected_Component
11479 and then Is_Entity_Name (Prefix (Par))
11480 and then Entity (Prefix (Par)) = Deriv_Obj;
11481 end Is_Deriv_Obj_Ref;
11483 -----------------
11484 -- Replace_Ref --
11485 -----------------
11487 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11488 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11489 -- Reset the Controlling_Argument of all function calls that
11490 -- encapsulate node From_Arg.
11492 ----------------------------------
11493 -- Remove_Controlling_Arguments --
11494 ----------------------------------
11496 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11497 Par : Node_Id;
11499 begin
11500 Par := From_Arg;
11501 while Present (Par) loop
11502 if Nkind (Par) = N_Function_Call
11503 and then Present (Controlling_Argument (Par))
11504 then
11505 Set_Controlling_Argument (Par, Empty);
11507 -- Prevent the search from going too far
11509 elsif Is_Body_Or_Package_Declaration (Par) then
11510 exit;
11511 end if;
11513 Par := Parent (Par);
11514 end loop;
11515 end Remove_Controlling_Arguments;
11517 -- Local variables
11519 Context : constant Node_Id := Parent (Ref);
11520 Loc : constant Source_Ptr := Sloc (Ref);
11521 Ref_Id : Entity_Id;
11522 Result : Traverse_Result;
11524 New_Ref : Node_Id;
11525 -- The new reference which is intended to substitute the old one
11527 Old_Ref : Node_Id;
11528 -- The reference designated for replacement. In certain cases this
11529 -- may be a node other than Ref.
11531 Val : Node_Or_Entity_Id;
11532 -- The corresponding value of Ref from the type map
11534 -- Start of processing for Replace_Ref
11536 begin
11537 -- Assume that the input reference is to be replaced and that the
11538 -- traversal should examine the children of the reference.
11540 Old_Ref := Ref;
11541 Result := OK;
11543 -- The input denotes a meaningful reference
11545 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11546 Ref_Id := Entity (Ref);
11547 Val := Type_Map.Get (Ref_Id);
11549 -- The reference has a corresponding value in the type map, a
11550 -- substitution is possible.
11552 if Present (Val) then
11554 -- The reference denotes a discriminant
11556 if Ekind (Ref_Id) = E_Discriminant then
11557 if Nkind (Val) in N_Entity then
11559 -- The value denotes another discriminant. Replace as
11560 -- follows:
11562 -- _object.Discr -> _object.Val
11564 if Ekind (Val) = E_Discriminant then
11565 New_Ref := New_Occurrence_Of (Val, Loc);
11567 -- Otherwise the value denotes the entity of a name which
11568 -- constraints the discriminant. Replace as follows:
11570 -- _object.Discr -> Val
11572 else
11573 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11575 New_Ref := New_Occurrence_Of (Val, Loc);
11576 Old_Ref := Parent (Old_Ref);
11577 end if;
11579 -- Otherwise the value denotes an arbitrary expression which
11580 -- constraints the discriminant. Replace as follows:
11582 -- _object.Discr -> Val
11584 else
11585 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11587 New_Ref := New_Copy_Tree (Val);
11588 Old_Ref := Parent (Old_Ref);
11589 end if;
11591 -- Otherwise the reference denotes a primitive. Replace as
11592 -- follows:
11594 -- Primitive -> Val
11596 else
11597 pragma Assert (Nkind (Val) in N_Entity);
11598 New_Ref := New_Occurrence_Of (Val, Loc);
11599 end if;
11601 -- The reference mentions the _object parameter of the parent
11602 -- type's DIC or type invariant procedure. Replace as follows:
11604 -- _object -> _object
11606 elsif Present (Par_Obj)
11607 and then Present (Deriv_Obj)
11608 and then Ref_Id = Par_Obj
11609 then
11610 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11612 -- The type of the _object parameter is class-wide when the
11613 -- expression comes from an assertion pragma that applies to
11614 -- an abstract parent type or an interface. The class-wide type
11615 -- facilitates the preanalysis of the expression by treating
11616 -- calls to abstract primitives that mention the current
11617 -- instance of the type as dispatching. Once the calls are
11618 -- remapped to invoke overriding or inherited primitives, the
11619 -- calls no longer need to be dispatching. Examine all function
11620 -- calls that encapsulate the _object parameter and reset their
11621 -- Controlling_Argument attribute.
11623 if Is_Class_Wide_Type (Etype (Par_Obj))
11624 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11625 then
11626 Remove_Controlling_Arguments (Old_Ref);
11627 end if;
11629 -- The reference to _object acts as an actual parameter in a
11630 -- subprogram call which may be invoking a primitive of the
11631 -- parent type:
11633 -- Primitive (... _object ...);
11635 -- The parent type primitive may not be overridden nor
11636 -- inherited when it is declared after the derived type
11637 -- definition:
11639 -- type Parent is tagged private;
11640 -- type Child is new Parent with private;
11641 -- procedure Primitive (Obj : Parent);
11643 -- In this scenario the _object parameter is converted to the
11644 -- parent type. Due to complications with partial/full views
11645 -- and view swaps, the parent type is taken from the formal
11646 -- parameter of the subprogram being called.
11648 if Nkind_In (Context, N_Function_Call,
11649 N_Procedure_Call_Statement)
11650 and then No (Type_Map.Get (Entity (Name (Context))))
11651 then
11652 New_Ref :=
11653 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11655 -- Do not process the generated type conversion because
11656 -- both the parent type and the derived type are in the
11657 -- Type_Map table. This will clobber the type conversion
11658 -- by resetting its subtype mark.
11660 Result := Skip;
11661 end if;
11663 -- Otherwise there is nothing to replace
11665 else
11666 New_Ref := Empty;
11667 end if;
11669 if Present (New_Ref) then
11670 Rewrite (Old_Ref, New_Ref);
11672 -- Update the return type when the context of the reference
11673 -- acts as the name of a function call. Note that the update
11674 -- should not be performed when the reference appears as an
11675 -- actual in the call.
11677 if Nkind (Context) = N_Function_Call
11678 and then Name (Context) = Old_Ref
11679 then
11680 Set_Etype (Context, Etype (Val));
11681 end if;
11682 end if;
11683 end if;
11685 -- Reanalyze the reference due to potential replacements
11687 if Nkind (Old_Ref) in N_Has_Etype then
11688 Set_Analyzed (Old_Ref, False);
11689 end if;
11691 return Result;
11692 end Replace_Ref;
11694 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11696 --------------------
11697 -- Type_Of_Formal --
11698 --------------------
11700 function Type_Of_Formal
11701 (Call : Node_Id;
11702 Actual : Node_Id) return Entity_Id
11704 A : Node_Id;
11705 F : Entity_Id;
11707 begin
11708 -- Examine the list of actual and formal parameters in parallel
11710 A := First (Parameter_Associations (Call));
11711 F := First_Formal (Entity (Name (Call)));
11712 while Present (A) and then Present (F) loop
11713 if A = Actual then
11714 return Etype (F);
11715 end if;
11717 Next (A);
11718 Next_Formal (F);
11719 end loop;
11721 -- The actual parameter must always have a corresponding formal
11723 pragma Assert (False);
11725 return Empty;
11726 end Type_Of_Formal;
11728 -- Start of processing for Replace_References
11730 begin
11731 -- Map the attributes of the parent type to the proper corresponding
11732 -- attributes of the derived type.
11734 Map_Types
11735 (Parent_Type => Par_Typ,
11736 Derived_Type => Deriv_Typ);
11738 -- Inspect the input expression and perform substitutions where
11739 -- necessary.
11741 Replace_Refs (Expr);
11742 end Replace_References;
11744 -----------------------------
11745 -- Replace_Type_References --
11746 -----------------------------
11748 procedure Replace_Type_References
11749 (Expr : Node_Id;
11750 Typ : Entity_Id;
11751 Obj_Id : Entity_Id)
11753 procedure Replace_Type_Ref (N : Node_Id);
11754 -- Substitute a single reference of the current instance of type Typ
11755 -- with a reference to Obj_Id.
11757 ----------------------
11758 -- Replace_Type_Ref --
11759 ----------------------
11761 procedure Replace_Type_Ref (N : Node_Id) is
11762 begin
11763 -- Decorate the reference to Typ even though it may be rewritten
11764 -- further down. This is done for two reasons:
11766 -- * ASIS has all necessary semantic information in the original
11767 -- tree.
11769 -- * Routines which examine properties of the Original_Node have
11770 -- some semantic information.
11772 if Nkind (N) = N_Identifier then
11773 Set_Entity (N, Typ);
11774 Set_Etype (N, Typ);
11776 elsif Nkind (N) = N_Selected_Component then
11777 Analyze (Prefix (N));
11778 Set_Entity (Selector_Name (N), Typ);
11779 Set_Etype (Selector_Name (N), Typ);
11780 end if;
11782 -- Perform the following substitution:
11784 -- Typ --> _object
11786 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
11787 Set_Comes_From_Source (N, True);
11788 end Replace_Type_Ref;
11790 procedure Replace_Type_Refs is
11791 new Replace_Type_References_Generic (Replace_Type_Ref);
11793 -- Start of processing for Replace_Type_References
11795 begin
11796 Replace_Type_Refs (Expr, Typ);
11797 end Replace_Type_References;
11799 ---------------------------
11800 -- Represented_As_Scalar --
11801 ---------------------------
11803 function Represented_As_Scalar (T : Entity_Id) return Boolean is
11804 UT : constant Entity_Id := Underlying_Type (T);
11805 begin
11806 return Is_Scalar_Type (UT)
11807 or else (Is_Bit_Packed_Array (UT)
11808 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
11809 end Represented_As_Scalar;
11811 ------------------------------
11812 -- Requires_Cleanup_Actions --
11813 ------------------------------
11815 function Requires_Cleanup_Actions
11816 (N : Node_Id;
11817 Lib_Level : Boolean) return Boolean
11819 At_Lib_Level : constant Boolean :=
11820 Lib_Level
11821 and then Nkind_In (N, N_Package_Body,
11822 N_Package_Specification);
11823 -- N is at the library level if the top-most context is a package and
11824 -- the path taken to reach N does not inlcude non-package constructs.
11826 begin
11827 case Nkind (N) is
11828 when N_Accept_Statement
11829 | N_Block_Statement
11830 | N_Entry_Body
11831 | N_Package_Body
11832 | N_Protected_Body
11833 | N_Subprogram_Body
11834 | N_Task_Body
11836 return
11837 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
11838 or else
11839 (Present (Handled_Statement_Sequence (N))
11840 and then
11841 Requires_Cleanup_Actions
11842 (Statements (Handled_Statement_Sequence (N)),
11843 At_Lib_Level, True));
11845 when N_Package_Specification =>
11846 return
11847 Requires_Cleanup_Actions
11848 (Visible_Declarations (N), At_Lib_Level, True)
11849 or else
11850 Requires_Cleanup_Actions
11851 (Private_Declarations (N), At_Lib_Level, True);
11853 when others =>
11854 return False;
11855 end case;
11856 end Requires_Cleanup_Actions;
11858 ------------------------------
11859 -- Requires_Cleanup_Actions --
11860 ------------------------------
11862 function Requires_Cleanup_Actions
11863 (L : List_Id;
11864 Lib_Level : Boolean;
11865 Nested_Constructs : Boolean) return Boolean
11867 Decl : Node_Id;
11868 Expr : Node_Id;
11869 Obj_Id : Entity_Id;
11870 Obj_Typ : Entity_Id;
11871 Pack_Id : Entity_Id;
11872 Typ : Entity_Id;
11874 begin
11875 if No (L)
11876 or else Is_Empty_List (L)
11877 then
11878 return False;
11879 end if;
11881 Decl := First (L);
11882 while Present (Decl) loop
11884 -- Library-level tagged types
11886 if Nkind (Decl) = N_Full_Type_Declaration then
11887 Typ := Defining_Identifier (Decl);
11889 -- Ignored Ghost types do not need any cleanup actions because
11890 -- they will not appear in the final tree.
11892 if Is_Ignored_Ghost_Entity (Typ) then
11893 null;
11895 elsif Is_Tagged_Type (Typ)
11896 and then Is_Library_Level_Entity (Typ)
11897 and then Convention (Typ) = Convention_Ada
11898 and then Present (Access_Disp_Table (Typ))
11899 and then RTE_Available (RE_Unregister_Tag)
11900 and then not Is_Abstract_Type (Typ)
11901 and then not No_Run_Time_Mode
11902 then
11903 return True;
11904 end if;
11906 -- Regular object declarations
11908 elsif Nkind (Decl) = N_Object_Declaration then
11909 Obj_Id := Defining_Identifier (Decl);
11910 Obj_Typ := Base_Type (Etype (Obj_Id));
11911 Expr := Expression (Decl);
11913 -- Bypass any form of processing for objects which have their
11914 -- finalization disabled. This applies only to objects at the
11915 -- library level.
11917 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
11918 null;
11920 -- Finalization of transient objects are treated separately in
11921 -- order to handle sensitive cases. These include:
11923 -- * Aggregate expansion
11924 -- * If, case, and expression with actions expansion
11925 -- * Transient scopes
11927 -- If one of those contexts has marked the transient object as
11928 -- ignored, do not generate finalization actions for it.
11930 elsif Is_Finalized_Transient (Obj_Id)
11931 or else Is_Ignored_Transient (Obj_Id)
11932 then
11933 null;
11935 -- Ignored Ghost objects do not need any cleanup actions because
11936 -- they will not appear in the final tree.
11938 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
11939 null;
11941 -- The object is of the form:
11942 -- Obj : [constant] Typ [:= Expr];
11944 -- Do not process tag-to-class-wide conversions because they do
11945 -- not yield an object. Do not process the incomplete view of a
11946 -- deferred constant. Note that an object initialized by means
11947 -- of a build-in-place function call may appear as a deferred
11948 -- constant after expansion activities. These kinds of objects
11949 -- must be finalized.
11951 elsif not Is_Imported (Obj_Id)
11952 and then Needs_Finalization (Obj_Typ)
11953 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
11954 and then not (Ekind (Obj_Id) = E_Constant
11955 and then not Has_Completion (Obj_Id)
11956 and then No (BIP_Initialization_Call (Obj_Id)))
11957 then
11958 return True;
11960 -- The object is of the form:
11961 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
11963 -- Obj : Access_Typ :=
11964 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
11966 elsif Is_Access_Type (Obj_Typ)
11967 and then Needs_Finalization
11968 (Available_View (Designated_Type (Obj_Typ)))
11969 and then Present (Expr)
11970 and then
11971 (Is_Secondary_Stack_BIP_Func_Call (Expr)
11972 or else
11973 (Is_Non_BIP_Func_Call (Expr)
11974 and then not Is_Related_To_Func_Return (Obj_Id)))
11975 then
11976 return True;
11978 -- Processing for "hook" objects generated for transient objects
11979 -- declared inside an Expression_With_Actions.
11981 elsif Is_Access_Type (Obj_Typ)
11982 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
11983 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
11984 N_Object_Declaration
11985 then
11986 return True;
11988 -- Processing for intermediate results of if expressions where
11989 -- one of the alternatives uses a controlled function call.
11991 elsif Is_Access_Type (Obj_Typ)
11992 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
11993 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
11994 N_Defining_Identifier
11995 and then Present (Expr)
11996 and then Nkind (Expr) = N_Null
11997 then
11998 return True;
12000 -- Simple protected objects which use type System.Tasking.
12001 -- Protected_Objects.Protection to manage their locks should be
12002 -- treated as controlled since they require manual cleanup.
12004 elsif Ekind (Obj_Id) = E_Variable
12005 and then (Is_Simple_Protected_Type (Obj_Typ)
12006 or else Has_Simple_Protected_Object (Obj_Typ))
12007 then
12008 return True;
12009 end if;
12011 -- Specific cases of object renamings
12013 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12014 Obj_Id := Defining_Identifier (Decl);
12015 Obj_Typ := Base_Type (Etype (Obj_Id));
12017 -- Bypass any form of processing for objects which have their
12018 -- finalization disabled. This applies only to objects at the
12019 -- library level.
12021 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12022 null;
12024 -- Ignored Ghost object renamings do not need any cleanup actions
12025 -- because they will not appear in the final tree.
12027 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12028 null;
12030 -- Return object of a build-in-place function. This case is
12031 -- recognized and marked by the expansion of an extended return
12032 -- statement (see Expand_N_Extended_Return_Statement).
12034 elsif Needs_Finalization (Obj_Typ)
12035 and then Is_Return_Object (Obj_Id)
12036 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12037 then
12038 return True;
12040 -- Detect a case where a source object has been initialized by
12041 -- a controlled function call or another object which was later
12042 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12044 -- Obj1 : CW_Type := Src_Obj;
12045 -- Obj2 : CW_Type := Function_Call (...);
12047 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12048 -- Tmp : ... := Function_Call (...)'reference;
12049 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12051 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12052 return True;
12053 end if;
12055 -- Inspect the freeze node of an access-to-controlled type and look
12056 -- for a delayed finalization master. This case arises when the
12057 -- freeze actions are inserted at a later time than the expansion of
12058 -- the context. Since Build_Finalizer is never called on a single
12059 -- construct twice, the master will be ultimately left out and never
12060 -- finalized. This is also needed for freeze actions of designated
12061 -- types themselves, since in some cases the finalization master is
12062 -- associated with a designated type's freeze node rather than that
12063 -- of the access type (see handling for freeze actions in
12064 -- Build_Finalization_Master).
12066 elsif Nkind (Decl) = N_Freeze_Entity
12067 and then Present (Actions (Decl))
12068 then
12069 Typ := Entity (Decl);
12071 -- Freeze nodes for ignored Ghost types do not need cleanup
12072 -- actions because they will never appear in the final tree.
12074 if Is_Ignored_Ghost_Entity (Typ) then
12075 null;
12077 elsif ((Is_Access_Type (Typ)
12078 and then not Is_Access_Subprogram_Type (Typ)
12079 and then Needs_Finalization
12080 (Available_View (Designated_Type (Typ))))
12081 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12082 and then Requires_Cleanup_Actions
12083 (Actions (Decl), Lib_Level, Nested_Constructs)
12084 then
12085 return True;
12086 end if;
12088 -- Nested package declarations
12090 elsif Nested_Constructs
12091 and then Nkind (Decl) = N_Package_Declaration
12092 then
12093 Pack_Id := Defining_Entity (Decl);
12095 -- Do not inspect an ignored Ghost package because all code found
12096 -- within will not appear in the final tree.
12098 if Is_Ignored_Ghost_Entity (Pack_Id) then
12099 null;
12101 elsif Ekind (Pack_Id) /= E_Generic_Package
12102 and then Requires_Cleanup_Actions
12103 (Specification (Decl), Lib_Level)
12104 then
12105 return True;
12106 end if;
12108 -- Nested package bodies
12110 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12112 -- Do not inspect an ignored Ghost package body because all code
12113 -- found within will not appear in the final tree.
12115 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12116 null;
12118 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12119 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12120 then
12121 return True;
12122 end if;
12124 elsif Nkind (Decl) = N_Block_Statement
12125 and then
12127 -- Handle a rare case caused by a controlled transient object
12128 -- created as part of a record init proc. The variable is wrapped
12129 -- in a block, but the block is not associated with a transient
12130 -- scope.
12132 (Inside_Init_Proc
12134 -- Handle the case where the original context has been wrapped in
12135 -- a block to avoid interference between exception handlers and
12136 -- At_End handlers. Treat the block as transparent and process its
12137 -- contents.
12139 or else Is_Finalization_Wrapper (Decl))
12140 then
12141 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12142 return True;
12143 end if;
12144 end if;
12146 Next (Decl);
12147 end loop;
12149 return False;
12150 end Requires_Cleanup_Actions;
12152 ------------------------------------
12153 -- Safe_Unchecked_Type_Conversion --
12154 ------------------------------------
12156 -- Note: this function knows quite a bit about the exact requirements of
12157 -- Gigi with respect to unchecked type conversions, and its code must be
12158 -- coordinated with any changes in Gigi in this area.
12160 -- The above requirements should be documented in Sinfo ???
12162 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12163 Otyp : Entity_Id;
12164 Ityp : Entity_Id;
12165 Oalign : Uint;
12166 Ialign : Uint;
12167 Pexp : constant Node_Id := Parent (Exp);
12169 begin
12170 -- If the expression is the RHS of an assignment or object declaration
12171 -- we are always OK because there will always be a target.
12173 -- Object renaming declarations, (generated for view conversions of
12174 -- actuals in inlined calls), like object declarations, provide an
12175 -- explicit type, and are safe as well.
12177 if (Nkind (Pexp) = N_Assignment_Statement
12178 and then Expression (Pexp) = Exp)
12179 or else Nkind_In (Pexp, N_Object_Declaration,
12180 N_Object_Renaming_Declaration)
12181 then
12182 return True;
12184 -- If the expression is the prefix of an N_Selected_Component we should
12185 -- also be OK because GCC knows to look inside the conversion except if
12186 -- the type is discriminated. We assume that we are OK anyway if the
12187 -- type is not set yet or if it is controlled since we can't afford to
12188 -- introduce a temporary in this case.
12190 elsif Nkind (Pexp) = N_Selected_Component
12191 and then Prefix (Pexp) = Exp
12192 then
12193 if No (Etype (Pexp)) then
12194 return True;
12195 else
12196 return
12197 not Has_Discriminants (Etype (Pexp))
12198 or else Is_Constrained (Etype (Pexp));
12199 end if;
12200 end if;
12202 -- Set the output type, this comes from Etype if it is set, otherwise we
12203 -- take it from the subtype mark, which we assume was already fully
12204 -- analyzed.
12206 if Present (Etype (Exp)) then
12207 Otyp := Etype (Exp);
12208 else
12209 Otyp := Entity (Subtype_Mark (Exp));
12210 end if;
12212 -- The input type always comes from the expression, and we assume this
12213 -- is indeed always analyzed, so we can simply get the Etype.
12215 Ityp := Etype (Expression (Exp));
12217 -- Initialize alignments to unknown so far
12219 Oalign := No_Uint;
12220 Ialign := No_Uint;
12222 -- Replace a concurrent type by its corresponding record type and each
12223 -- type by its underlying type and do the tests on those. The original
12224 -- type may be a private type whose completion is a concurrent type, so
12225 -- find the underlying type first.
12227 if Present (Underlying_Type (Otyp)) then
12228 Otyp := Underlying_Type (Otyp);
12229 end if;
12231 if Present (Underlying_Type (Ityp)) then
12232 Ityp := Underlying_Type (Ityp);
12233 end if;
12235 if Is_Concurrent_Type (Otyp) then
12236 Otyp := Corresponding_Record_Type (Otyp);
12237 end if;
12239 if Is_Concurrent_Type (Ityp) then
12240 Ityp := Corresponding_Record_Type (Ityp);
12241 end if;
12243 -- If the base types are the same, we know there is no problem since
12244 -- this conversion will be a noop.
12246 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12247 return True;
12249 -- Same if this is an upwards conversion of an untagged type, and there
12250 -- are no constraints involved (could be more general???)
12252 elsif Etype (Ityp) = Otyp
12253 and then not Is_Tagged_Type (Ityp)
12254 and then not Has_Discriminants (Ityp)
12255 and then No (First_Rep_Item (Base_Type (Ityp)))
12256 then
12257 return True;
12259 -- If the expression has an access type (object or subprogram) we assume
12260 -- that the conversion is safe, because the size of the target is safe,
12261 -- even if it is a record (which might be treated as having unknown size
12262 -- at this point).
12264 elsif Is_Access_Type (Ityp) then
12265 return True;
12267 -- If the size of output type is known at compile time, there is never
12268 -- a problem. Note that unconstrained records are considered to be of
12269 -- known size, but we can't consider them that way here, because we are
12270 -- talking about the actual size of the object.
12272 -- We also make sure that in addition to the size being known, we do not
12273 -- have a case which might generate an embarrassingly large temp in
12274 -- stack checking mode.
12276 elsif Size_Known_At_Compile_Time (Otyp)
12277 and then
12278 (not Stack_Checking_Enabled
12279 or else not May_Generate_Large_Temp (Otyp))
12280 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12281 then
12282 return True;
12284 -- If either type is tagged, then we know the alignment is OK so Gigi
12285 -- will be able to use pointer punning.
12287 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12288 return True;
12290 -- If either type is a limited record type, we cannot do a copy, so say
12291 -- safe since there's nothing else we can do.
12293 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12294 return True;
12296 -- Conversions to and from packed array types are always ignored and
12297 -- hence are safe.
12299 elsif Is_Packed_Array_Impl_Type (Otyp)
12300 or else Is_Packed_Array_Impl_Type (Ityp)
12301 then
12302 return True;
12303 end if;
12305 -- The only other cases known to be safe is if the input type's
12306 -- alignment is known to be at least the maximum alignment for the
12307 -- target or if both alignments are known and the output type's
12308 -- alignment is no stricter than the input's. We can use the component
12309 -- type alignment for an array if a type is an unpacked array type.
12311 if Present (Alignment_Clause (Otyp)) then
12312 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12314 elsif Is_Array_Type (Otyp)
12315 and then Present (Alignment_Clause (Component_Type (Otyp)))
12316 then
12317 Oalign := Expr_Value (Expression (Alignment_Clause
12318 (Component_Type (Otyp))));
12319 end if;
12321 if Present (Alignment_Clause (Ityp)) then
12322 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12324 elsif Is_Array_Type (Ityp)
12325 and then Present (Alignment_Clause (Component_Type (Ityp)))
12326 then
12327 Ialign := Expr_Value (Expression (Alignment_Clause
12328 (Component_Type (Ityp))));
12329 end if;
12331 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12332 return True;
12334 elsif Ialign /= No_Uint
12335 and then Oalign /= No_Uint
12336 and then Ialign <= Oalign
12337 then
12338 return True;
12340 -- Otherwise, Gigi cannot handle this and we must make a temporary
12342 else
12343 return False;
12344 end if;
12345 end Safe_Unchecked_Type_Conversion;
12347 ---------------------------------
12348 -- Set_Current_Value_Condition --
12349 ---------------------------------
12351 -- Note: the implementation of this procedure is very closely tied to the
12352 -- implementation of Get_Current_Value_Condition. Here we set required
12353 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12354 -- them, so they must have a consistent view.
12356 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12358 procedure Set_Entity_Current_Value (N : Node_Id);
12359 -- If N is an entity reference, where the entity is of an appropriate
12360 -- kind, then set the current value of this entity to Cnode, unless
12361 -- there is already a definite value set there.
12363 procedure Set_Expression_Current_Value (N : Node_Id);
12364 -- If N is of an appropriate form, sets an appropriate entry in current
12365 -- value fields of relevant entities. Multiple entities can be affected
12366 -- in the case of an AND or AND THEN.
12368 ------------------------------
12369 -- Set_Entity_Current_Value --
12370 ------------------------------
12372 procedure Set_Entity_Current_Value (N : Node_Id) is
12373 begin
12374 if Is_Entity_Name (N) then
12375 declare
12376 Ent : constant Entity_Id := Entity (N);
12378 begin
12379 -- Don't capture if not safe to do so
12381 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12382 return;
12383 end if;
12385 -- Here we have a case where the Current_Value field may need
12386 -- to be set. We set it if it is not already set to a compile
12387 -- time expression value.
12389 -- Note that this represents a decision that one condition
12390 -- blots out another previous one. That's certainly right if
12391 -- they occur at the same level. If the second one is nested,
12392 -- then the decision is neither right nor wrong (it would be
12393 -- equally OK to leave the outer one in place, or take the new
12394 -- inner one. Really we should record both, but our data
12395 -- structures are not that elaborate.
12397 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12398 Set_Current_Value (Ent, Cnode);
12399 end if;
12400 end;
12401 end if;
12402 end Set_Entity_Current_Value;
12404 ----------------------------------
12405 -- Set_Expression_Current_Value --
12406 ----------------------------------
12408 procedure Set_Expression_Current_Value (N : Node_Id) is
12409 Cond : Node_Id;
12411 begin
12412 Cond := N;
12414 -- Loop to deal with (ignore for now) any NOT operators present. The
12415 -- presence of NOT operators will be handled properly when we call
12416 -- Get_Current_Value_Condition.
12418 while Nkind (Cond) = N_Op_Not loop
12419 Cond := Right_Opnd (Cond);
12420 end loop;
12422 -- For an AND or AND THEN, recursively process operands
12424 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12425 Set_Expression_Current_Value (Left_Opnd (Cond));
12426 Set_Expression_Current_Value (Right_Opnd (Cond));
12427 return;
12428 end if;
12430 -- Check possible relational operator
12432 if Nkind (Cond) in N_Op_Compare then
12433 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12434 Set_Entity_Current_Value (Left_Opnd (Cond));
12435 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12436 Set_Entity_Current_Value (Right_Opnd (Cond));
12437 end if;
12439 elsif Nkind_In (Cond,
12440 N_Type_Conversion,
12441 N_Qualified_Expression,
12442 N_Expression_With_Actions)
12443 then
12444 Set_Expression_Current_Value (Expression (Cond));
12446 -- Check possible boolean variable reference
12448 else
12449 Set_Entity_Current_Value (Cond);
12450 end if;
12451 end Set_Expression_Current_Value;
12453 -- Start of processing for Set_Current_Value_Condition
12455 begin
12456 Set_Expression_Current_Value (Condition (Cnode));
12457 end Set_Current_Value_Condition;
12459 --------------------------
12460 -- Set_Elaboration_Flag --
12461 --------------------------
12463 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12464 Loc : constant Source_Ptr := Sloc (N);
12465 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12466 Asn : Node_Id;
12468 begin
12469 if Present (Ent) then
12471 -- Nothing to do if at the compilation unit level, because in this
12472 -- case the flag is set by the binder generated elaboration routine.
12474 if Nkind (Parent (N)) = N_Compilation_Unit then
12475 null;
12477 -- Here we do need to generate an assignment statement
12479 else
12480 Check_Restriction (No_Elaboration_Code, N);
12481 Asn :=
12482 Make_Assignment_Statement (Loc,
12483 Name => New_Occurrence_Of (Ent, Loc),
12484 Expression => Make_Integer_Literal (Loc, Uint_1));
12486 if Nkind (Parent (N)) = N_Subunit then
12487 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12488 else
12489 Insert_After (N, Asn);
12490 end if;
12492 Analyze (Asn);
12494 -- Kill current value indication. This is necessary because the
12495 -- tests of this flag are inserted out of sequence and must not
12496 -- pick up bogus indications of the wrong constant value.
12498 Set_Current_Value (Ent, Empty);
12500 -- If the subprogram is in the current declarative part and
12501 -- 'access has been applied to it, generate an elaboration
12502 -- check at the beginning of the declarations of the body.
12504 if Nkind (N) = N_Subprogram_Body
12505 and then Address_Taken (Spec_Id)
12506 and then
12507 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12508 then
12509 declare
12510 Loc : constant Source_Ptr := Sloc (N);
12511 Decls : constant List_Id := Declarations (N);
12512 Chk : Node_Id;
12514 begin
12515 -- No need to generate this check if first entry in the
12516 -- declaration list is a raise of Program_Error now.
12518 if Present (Decls)
12519 and then Nkind (First (Decls)) = N_Raise_Program_Error
12520 then
12521 return;
12522 end if;
12524 -- Otherwise generate the check
12526 Chk :=
12527 Make_Raise_Program_Error (Loc,
12528 Condition =>
12529 Make_Op_Eq (Loc,
12530 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12531 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12532 Reason => PE_Access_Before_Elaboration);
12534 if No (Decls) then
12535 Set_Declarations (N, New_List (Chk));
12536 else
12537 Prepend (Chk, Decls);
12538 end if;
12540 Analyze (Chk);
12541 end;
12542 end if;
12543 end if;
12544 end if;
12545 end Set_Elaboration_Flag;
12547 ----------------------------
12548 -- Set_Renamed_Subprogram --
12549 ----------------------------
12551 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12552 begin
12553 -- If input node is an identifier, we can just reset it
12555 if Nkind (N) = N_Identifier then
12556 Set_Chars (N, Chars (E));
12557 Set_Entity (N, E);
12559 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12561 else
12562 declare
12563 CS : constant Boolean := Comes_From_Source (N);
12564 begin
12565 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12566 Set_Entity (N, E);
12567 Set_Comes_From_Source (N, CS);
12568 Set_Analyzed (N, True);
12569 end;
12570 end if;
12571 end Set_Renamed_Subprogram;
12573 ----------------------
12574 -- Side_Effect_Free --
12575 ----------------------
12577 function Side_Effect_Free
12578 (N : Node_Id;
12579 Name_Req : Boolean := False;
12580 Variable_Ref : Boolean := False) return Boolean
12582 Typ : constant Entity_Id := Etype (N);
12583 -- Result type of the expression
12585 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12586 -- The argument N is a construct where the Prefix is dereferenced if it
12587 -- is an access type and the result is a variable. The call returns True
12588 -- if the construct is side effect free (not considering side effects in
12589 -- other than the prefix which are to be tested by the caller).
12591 function Within_In_Parameter (N : Node_Id) return Boolean;
12592 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12593 -- N is not side-effect free when the actual is global and modifiable
12594 -- indirectly from within a subprogram, because it may be passed by
12595 -- reference. The front-end must be conservative here and assume that
12596 -- this may happen with any array or record type. On the other hand, we
12597 -- cannot create temporaries for all expressions for which this
12598 -- condition is true, for various reasons that might require clearing up
12599 -- ??? For example, discriminant references that appear out of place, or
12600 -- spurious type errors with class-wide expressions. As a result, we
12601 -- limit the transformation to loop bounds, which is so far the only
12602 -- case that requires it.
12604 -----------------------------
12605 -- Safe_Prefixed_Reference --
12606 -----------------------------
12608 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12609 begin
12610 -- If prefix is not side effect free, definitely not safe
12612 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12613 return False;
12615 -- If the prefix is of an access type that is not access-to-constant,
12616 -- then this construct is a variable reference, which means it is to
12617 -- be considered to have side effects if Variable_Ref is set True.
12619 elsif Is_Access_Type (Etype (Prefix (N)))
12620 and then not Is_Access_Constant (Etype (Prefix (N)))
12621 and then Variable_Ref
12622 then
12623 -- Exception is a prefix that is the result of a previous removal
12624 -- of side-effects.
12626 return Is_Entity_Name (Prefix (N))
12627 and then not Comes_From_Source (Prefix (N))
12628 and then Ekind (Entity (Prefix (N))) = E_Constant
12629 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12631 -- If the prefix is an explicit dereference then this construct is a
12632 -- variable reference, which means it is to be considered to have
12633 -- side effects if Variable_Ref is True.
12635 -- We do NOT exclude dereferences of access-to-constant types because
12636 -- we handle them as constant view of variables.
12638 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12639 and then Variable_Ref
12640 then
12641 return False;
12643 -- Note: The following test is the simplest way of solving a complex
12644 -- problem uncovered by the following test (Side effect on loop bound
12645 -- that is a subcomponent of a global variable:
12647 -- with Text_Io; use Text_Io;
12648 -- procedure Tloop is
12649 -- type X is
12650 -- record
12651 -- V : Natural := 4;
12652 -- S : String (1..5) := (others => 'a');
12653 -- end record;
12654 -- X1 : X;
12656 -- procedure Modi;
12658 -- generic
12659 -- with procedure Action;
12660 -- procedure Loop_G (Arg : X; Msg : String)
12662 -- procedure Loop_G (Arg : X; Msg : String) is
12663 -- begin
12664 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12665 -- & Natural'Image (Arg.V));
12666 -- for Index in 1 .. Arg.V loop
12667 -- Text_Io.Put_Line
12668 -- (Natural'Image (Index) & " " & Arg.S (Index));
12669 -- if Index > 2 then
12670 -- Modi;
12671 -- end if;
12672 -- end loop;
12673 -- Put_Line ("end loop_g " & Msg);
12674 -- end;
12676 -- procedure Loop1 is new Loop_G (Modi);
12677 -- procedure Modi is
12678 -- begin
12679 -- X1.V := 1;
12680 -- Loop1 (X1, "from modi");
12681 -- end;
12683 -- begin
12684 -- Loop1 (X1, "initial");
12685 -- end;
12687 -- The output of the above program should be:
12689 -- begin loop_g initial will loop till: 4
12690 -- 1 a
12691 -- 2 a
12692 -- 3 a
12693 -- begin loop_g from modi will loop till: 1
12694 -- 1 a
12695 -- end loop_g from modi
12696 -- 4 a
12697 -- begin loop_g from modi will loop till: 1
12698 -- 1 a
12699 -- end loop_g from modi
12700 -- end loop_g initial
12702 -- If a loop bound is a subcomponent of a global variable, a
12703 -- modification of that variable within the loop may incorrectly
12704 -- affect the execution of the loop.
12706 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12707 and then Within_In_Parameter (Prefix (N))
12708 and then Variable_Ref
12709 then
12710 return False;
12712 -- All other cases are side effect free
12714 else
12715 return True;
12716 end if;
12717 end Safe_Prefixed_Reference;
12719 -------------------------
12720 -- Within_In_Parameter --
12721 -------------------------
12723 function Within_In_Parameter (N : Node_Id) return Boolean is
12724 begin
12725 if not Comes_From_Source (N) then
12726 return False;
12728 elsif Is_Entity_Name (N) then
12729 return Ekind (Entity (N)) = E_In_Parameter;
12731 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12732 return Within_In_Parameter (Prefix (N));
12734 else
12735 return False;
12736 end if;
12737 end Within_In_Parameter;
12739 -- Start of processing for Side_Effect_Free
12741 begin
12742 -- If volatile reference, always consider it to have side effects
12744 if Is_Volatile_Reference (N) then
12745 return False;
12746 end if;
12748 -- Note on checks that could raise Constraint_Error. Strictly, if we
12749 -- take advantage of 11.6, these checks do not count as side effects.
12750 -- However, we would prefer to consider that they are side effects,
12751 -- since the back end CSE does not work very well on expressions which
12752 -- can raise Constraint_Error. On the other hand if we don't consider
12753 -- them to be side effect free, then we get some awkward expansions
12754 -- in -gnato mode, resulting in code insertions at a point where we
12755 -- do not have a clear model for performing the insertions.
12757 -- Special handling for entity names
12759 if Is_Entity_Name (N) then
12761 -- A type reference is always side effect free
12763 if Is_Type (Entity (N)) then
12764 return True;
12766 -- Variables are considered to be a side effect if Variable_Ref
12767 -- is set or if we have a volatile reference and Name_Req is off.
12768 -- If Name_Req is True then we can't help returning a name which
12769 -- effectively allows multiple references in any case.
12771 elsif Is_Variable (N, Use_Original_Node => False) then
12772 return not Variable_Ref
12773 and then (not Is_Volatile_Reference (N) or else Name_Req);
12775 -- Any other entity (e.g. a subtype name) is definitely side
12776 -- effect free.
12778 else
12779 return True;
12780 end if;
12782 -- A value known at compile time is always side effect free
12784 elsif Compile_Time_Known_Value (N) then
12785 return True;
12787 -- A variable renaming is not side-effect free, because the renaming
12788 -- will function like a macro in the front-end in some cases, and an
12789 -- assignment can modify the component designated by N, so we need to
12790 -- create a temporary for it.
12792 -- The guard testing for Entity being present is needed at least in
12793 -- the case of rewritten predicate expressions, and may well also be
12794 -- appropriate elsewhere. Obviously we can't go testing the entity
12795 -- field if it does not exist, so it's reasonable to say that this is
12796 -- not the renaming case if it does not exist.
12798 elsif Is_Entity_Name (Original_Node (N))
12799 and then Present (Entity (Original_Node (N)))
12800 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
12801 and then Ekind (Entity (Original_Node (N))) /= E_Constant
12802 then
12803 declare
12804 RO : constant Node_Id :=
12805 Renamed_Object (Entity (Original_Node (N)));
12807 begin
12808 -- If the renamed object is an indexed component, or an
12809 -- explicit dereference, then the designated object could
12810 -- be modified by an assignment.
12812 if Nkind_In (RO, N_Indexed_Component,
12813 N_Explicit_Dereference)
12814 then
12815 return False;
12817 -- A selected component must have a safe prefix
12819 elsif Nkind (RO) = N_Selected_Component then
12820 return Safe_Prefixed_Reference (RO);
12822 -- In all other cases, designated object cannot be changed so
12823 -- we are side effect free.
12825 else
12826 return True;
12827 end if;
12828 end;
12830 -- Remove_Side_Effects generates an object renaming declaration to
12831 -- capture the expression of a class-wide expression. In VM targets
12832 -- the frontend performs no expansion for dispatching calls to
12833 -- class- wide types since they are handled by the VM. Hence, we must
12834 -- locate here if this node corresponds to a previous invocation of
12835 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
12837 elsif not Tagged_Type_Expansion
12838 and then not Comes_From_Source (N)
12839 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
12840 and then Is_Class_Wide_Type (Typ)
12841 then
12842 return True;
12844 -- Generating C the type conversion of an access to constrained array
12845 -- type into an access to unconstrained array type involves initializing
12846 -- a fat pointer and the expression cannot be assumed to be free of side
12847 -- effects since it must referenced several times to compute its bounds.
12849 elsif Modify_Tree_For_C
12850 and then Nkind (N) = N_Type_Conversion
12851 and then Is_Access_Type (Typ)
12852 and then Is_Array_Type (Designated_Type (Typ))
12853 and then not Is_Constrained (Designated_Type (Typ))
12854 then
12855 return False;
12856 end if;
12858 -- For other than entity names and compile time known values,
12859 -- check the node kind for special processing.
12861 case Nkind (N) is
12863 -- An attribute reference is side effect free if its expressions
12864 -- are side effect free and its prefix is side effect free or
12865 -- is an entity reference.
12867 -- Is this right? what about x'first where x is a variable???
12869 when N_Attribute_Reference =>
12870 Attribute_Reference : declare
12872 function Side_Effect_Free_Attribute
12873 (Attribute_Name : Name_Id) return Boolean;
12874 -- Returns True if evaluation of the given attribute is
12875 -- considered side-effect free (independent of prefix and
12876 -- arguments).
12878 --------------------------------
12879 -- Side_Effect_Free_Attribute --
12880 --------------------------------
12882 function Side_Effect_Free_Attribute
12883 (Attribute_Name : Name_Id) return Boolean
12885 begin
12886 case Attribute_Name is
12887 when Name_Input =>
12888 return False;
12890 when Name_Image
12891 | Name_Img
12892 | Name_Wide_Image
12893 | Name_Wide_Wide_Image
12895 -- CodePeer doesn't want to see replicated copies of
12896 -- 'Image calls.
12898 return not CodePeer_Mode;
12900 when others =>
12901 return True;
12902 end case;
12903 end Side_Effect_Free_Attribute;
12905 -- Start of processing for Attribute_Reference
12907 begin
12908 return
12909 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12910 and then Side_Effect_Free_Attribute (Attribute_Name (N))
12911 and then (Is_Entity_Name (Prefix (N))
12912 or else Side_Effect_Free
12913 (Prefix (N), Name_Req, Variable_Ref));
12914 end Attribute_Reference;
12916 -- A binary operator is side effect free if and both operands are
12917 -- side effect free. For this purpose binary operators include
12918 -- membership tests and short circuit forms.
12920 when N_Binary_Op
12921 | N_Membership_Test
12922 | N_Short_Circuit
12924 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
12925 and then
12926 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
12928 -- An explicit dereference is side effect free only if it is
12929 -- a side effect free prefixed reference.
12931 when N_Explicit_Dereference =>
12932 return Safe_Prefixed_Reference (N);
12934 -- An expression with action is side effect free if its expression
12935 -- is side effect free and it has no actions.
12937 when N_Expression_With_Actions =>
12938 return
12939 Is_Empty_List (Actions (N))
12940 and then Side_Effect_Free
12941 (Expression (N), Name_Req, Variable_Ref);
12943 -- A call to _rep_to_pos is side effect free, since we generate
12944 -- this pure function call ourselves. Moreover it is critically
12945 -- important to make this exception, since otherwise we can have
12946 -- discriminants in array components which don't look side effect
12947 -- free in the case of an array whose index type is an enumeration
12948 -- type with an enumeration rep clause.
12950 -- All other function calls are not side effect free
12952 when N_Function_Call =>
12953 return
12954 Nkind (Name (N)) = N_Identifier
12955 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
12956 and then Side_Effect_Free
12957 (First (Parameter_Associations (N)),
12958 Name_Req, Variable_Ref);
12960 -- An IF expression is side effect free if it's of a scalar type, and
12961 -- all its components are all side effect free (conditions and then
12962 -- actions and else actions). We restrict to scalar types, since it
12963 -- is annoying to deal with things like (if A then B else C)'First
12964 -- where the type involved is a string type.
12966 when N_If_Expression =>
12967 return
12968 Is_Scalar_Type (Typ)
12969 and then Side_Effect_Free
12970 (Expressions (N), Name_Req, Variable_Ref);
12972 -- An indexed component is side effect free if it is a side
12973 -- effect free prefixed reference and all the indexing
12974 -- expressions are side effect free.
12976 when N_Indexed_Component =>
12977 return
12978 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12979 and then Safe_Prefixed_Reference (N);
12981 -- A type qualification, type conversion, or unchecked expression is
12982 -- side effect free if the expression is side effect free.
12984 when N_Qualified_Expression
12985 | N_Type_Conversion
12986 | N_Unchecked_Expression
12988 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
12990 -- A selected component is side effect free only if it is a side
12991 -- effect free prefixed reference.
12993 when N_Selected_Component =>
12994 return Safe_Prefixed_Reference (N);
12996 -- A range is side effect free if the bounds are side effect free
12998 when N_Range =>
12999 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13000 and then
13001 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13003 -- A slice is side effect free if it is a side effect free
13004 -- prefixed reference and the bounds are side effect free.
13006 when N_Slice =>
13007 return
13008 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13009 and then Safe_Prefixed_Reference (N);
13011 -- A unary operator is side effect free if the operand
13012 -- is side effect free.
13014 when N_Unary_Op =>
13015 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13017 -- An unchecked type conversion is side effect free only if it
13018 -- is safe and its argument is side effect free.
13020 when N_Unchecked_Type_Conversion =>
13021 return
13022 Safe_Unchecked_Type_Conversion (N)
13023 and then Side_Effect_Free
13024 (Expression (N), Name_Req, Variable_Ref);
13026 -- A literal is side effect free
13028 when N_Character_Literal
13029 | N_Integer_Literal
13030 | N_Real_Literal
13031 | N_String_Literal
13033 return True;
13035 -- We consider that anything else has side effects. This is a bit
13036 -- crude, but we are pretty close for most common cases, and we
13037 -- are certainly correct (i.e. we never return True when the
13038 -- answer should be False).
13040 when others =>
13041 return False;
13042 end case;
13043 end Side_Effect_Free;
13045 -- A list is side effect free if all elements of the list are side
13046 -- effect free.
13048 function Side_Effect_Free
13049 (L : List_Id;
13050 Name_Req : Boolean := False;
13051 Variable_Ref : Boolean := False) return Boolean
13053 N : Node_Id;
13055 begin
13056 if L = No_List or else L = Error_List then
13057 return True;
13059 else
13060 N := First (L);
13061 while Present (N) loop
13062 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13063 return False;
13064 else
13065 Next (N);
13066 end if;
13067 end loop;
13069 return True;
13070 end if;
13071 end Side_Effect_Free;
13073 ----------------------------------
13074 -- Silly_Boolean_Array_Not_Test --
13075 ----------------------------------
13077 -- This procedure implements an odd and silly test. We explicitly check
13078 -- for the case where the 'First of the component type is equal to the
13079 -- 'Last of this component type, and if this is the case, we make sure
13080 -- that constraint error is raised. The reason is that the NOT is bound
13081 -- to cause CE in this case, and we will not otherwise catch it.
13083 -- No such check is required for AND and OR, since for both these cases
13084 -- False op False = False, and True op True = True. For the XOR case,
13085 -- see Silly_Boolean_Array_Xor_Test.
13087 -- Believe it or not, this was reported as a bug. Note that nearly always,
13088 -- the test will evaluate statically to False, so the code will be
13089 -- statically removed, and no extra overhead caused.
13091 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13092 Loc : constant Source_Ptr := Sloc (N);
13093 CT : constant Entity_Id := Component_Type (T);
13095 begin
13096 -- The check we install is
13098 -- constraint_error when
13099 -- component_type'first = component_type'last
13100 -- and then array_type'Length /= 0)
13102 -- We need the last guard because we don't want to raise CE for empty
13103 -- arrays since no out of range values result. (Empty arrays with a
13104 -- component type of True .. True -- very useful -- even the ACATS
13105 -- does not test that marginal case).
13107 Insert_Action (N,
13108 Make_Raise_Constraint_Error (Loc,
13109 Condition =>
13110 Make_And_Then (Loc,
13111 Left_Opnd =>
13112 Make_Op_Eq (Loc,
13113 Left_Opnd =>
13114 Make_Attribute_Reference (Loc,
13115 Prefix => New_Occurrence_Of (CT, Loc),
13116 Attribute_Name => Name_First),
13118 Right_Opnd =>
13119 Make_Attribute_Reference (Loc,
13120 Prefix => New_Occurrence_Of (CT, Loc),
13121 Attribute_Name => Name_Last)),
13123 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13124 Reason => CE_Range_Check_Failed));
13125 end Silly_Boolean_Array_Not_Test;
13127 ----------------------------------
13128 -- Silly_Boolean_Array_Xor_Test --
13129 ----------------------------------
13131 -- This procedure implements an odd and silly test. We explicitly check
13132 -- for the XOR case where the component type is True .. True, since this
13133 -- will raise constraint error. A special check is required since CE
13134 -- will not be generated otherwise (cf Expand_Packed_Not).
13136 -- No such check is required for AND and OR, since for both these cases
13137 -- False op False = False, and True op True = True, and no check is
13138 -- required for the case of False .. False, since False xor False = False.
13139 -- See also Silly_Boolean_Array_Not_Test
13141 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13142 Loc : constant Source_Ptr := Sloc (N);
13143 CT : constant Entity_Id := Component_Type (T);
13145 begin
13146 -- The check we install is
13148 -- constraint_error when
13149 -- Boolean (component_type'First)
13150 -- and then Boolean (component_type'Last)
13151 -- and then array_type'Length /= 0)
13153 -- We need the last guard because we don't want to raise CE for empty
13154 -- arrays since no out of range values result (Empty arrays with a
13155 -- component type of True .. True -- very useful -- even the ACATS
13156 -- does not test that marginal case).
13158 Insert_Action (N,
13159 Make_Raise_Constraint_Error (Loc,
13160 Condition =>
13161 Make_And_Then (Loc,
13162 Left_Opnd =>
13163 Make_And_Then (Loc,
13164 Left_Opnd =>
13165 Convert_To (Standard_Boolean,
13166 Make_Attribute_Reference (Loc,
13167 Prefix => New_Occurrence_Of (CT, Loc),
13168 Attribute_Name => Name_First)),
13170 Right_Opnd =>
13171 Convert_To (Standard_Boolean,
13172 Make_Attribute_Reference (Loc,
13173 Prefix => New_Occurrence_Of (CT, Loc),
13174 Attribute_Name => Name_Last))),
13176 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13177 Reason => CE_Range_Check_Failed));
13178 end Silly_Boolean_Array_Xor_Test;
13180 --------------------------
13181 -- Target_Has_Fixed_Ops --
13182 --------------------------
13184 Integer_Sized_Small : Ureal;
13185 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13186 -- called (we don't want to compute it more than once).
13188 Long_Integer_Sized_Small : Ureal;
13189 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13190 -- is called (we don't want to compute it more than once)
13192 First_Time_For_THFO : Boolean := True;
13193 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13195 function Target_Has_Fixed_Ops
13196 (Left_Typ : Entity_Id;
13197 Right_Typ : Entity_Id;
13198 Result_Typ : Entity_Id) return Boolean
13200 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13201 -- Return True if the given type is a fixed-point type with a small
13202 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13203 -- an absolute value less than 1.0. This is currently limited to
13204 -- fixed-point types that map to Integer or Long_Integer.
13206 ------------------------
13207 -- Is_Fractional_Type --
13208 ------------------------
13210 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13211 begin
13212 if Esize (Typ) = Standard_Integer_Size then
13213 return Small_Value (Typ) = Integer_Sized_Small;
13215 elsif Esize (Typ) = Standard_Long_Integer_Size then
13216 return Small_Value (Typ) = Long_Integer_Sized_Small;
13218 else
13219 return False;
13220 end if;
13221 end Is_Fractional_Type;
13223 -- Start of processing for Target_Has_Fixed_Ops
13225 begin
13226 -- Return False if Fractional_Fixed_Ops_On_Target is false
13228 if not Fractional_Fixed_Ops_On_Target then
13229 return False;
13230 end if;
13232 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13233 -- standard constants used by Is_Fractional_Type.
13235 if First_Time_For_THFO then
13236 First_Time_For_THFO := False;
13238 Integer_Sized_Small :=
13239 UR_From_Components
13240 (Num => Uint_1,
13241 Den => UI_From_Int (Standard_Integer_Size - 1),
13242 Rbase => 2);
13244 Long_Integer_Sized_Small :=
13245 UR_From_Components
13246 (Num => Uint_1,
13247 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13248 Rbase => 2);
13249 end if;
13251 -- Return True if target supports fixed-by-fixed multiply/divide for
13252 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13253 -- and result types are equivalent fractional types.
13255 return Is_Fractional_Type (Base_Type (Left_Typ))
13256 and then Is_Fractional_Type (Base_Type (Right_Typ))
13257 and then Is_Fractional_Type (Base_Type (Result_Typ))
13258 and then Esize (Left_Typ) = Esize (Right_Typ)
13259 and then Esize (Left_Typ) = Esize (Result_Typ);
13260 end Target_Has_Fixed_Ops;
13262 -------------------
13263 -- Type_Map_Hash --
13264 -------------------
13266 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13267 begin
13268 return Type_Map_Header (Id mod Type_Map_Size);
13269 end Type_Map_Hash;
13271 ------------------------------------------
13272 -- Type_May_Have_Bit_Aligned_Components --
13273 ------------------------------------------
13275 function Type_May_Have_Bit_Aligned_Components
13276 (Typ : Entity_Id) return Boolean
13278 begin
13279 -- Array type, check component type
13281 if Is_Array_Type (Typ) then
13282 return
13283 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13285 -- Record type, check components
13287 elsif Is_Record_Type (Typ) then
13288 declare
13289 E : Entity_Id;
13291 begin
13292 E := First_Component_Or_Discriminant (Typ);
13293 while Present (E) loop
13294 if Component_May_Be_Bit_Aligned (E)
13295 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13296 then
13297 return True;
13298 end if;
13300 Next_Component_Or_Discriminant (E);
13301 end loop;
13303 return False;
13304 end;
13306 -- Type other than array or record is always OK
13308 else
13309 return False;
13310 end if;
13311 end Type_May_Have_Bit_Aligned_Components;
13313 -------------------------------
13314 -- Update_Primitives_Mapping --
13315 -------------------------------
13317 procedure Update_Primitives_Mapping
13318 (Inher_Id : Entity_Id;
13319 Subp_Id : Entity_Id)
13321 begin
13322 Map_Types
13323 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13324 Derived_Type => Find_Dispatching_Type (Subp_Id));
13325 end Update_Primitives_Mapping;
13327 ----------------------------------
13328 -- Within_Case_Or_If_Expression --
13329 ----------------------------------
13331 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13332 Par : Node_Id;
13334 begin
13335 -- Locate an enclosing case or if expression. Note that these constructs
13336 -- can be expanded into Expression_With_Actions, hence the test of the
13337 -- original node.
13339 Par := Parent (N);
13340 while Present (Par) loop
13341 if Nkind_In (Original_Node (Par), N_Case_Expression,
13342 N_If_Expression)
13343 then
13344 return True;
13346 -- Prevent the search from going too far
13348 elsif Is_Body_Or_Package_Declaration (Par) then
13349 return False;
13350 end if;
13352 Par := Parent (Par);
13353 end loop;
13355 return False;
13356 end Within_Case_Or_If_Expression;
13358 --------------------------------
13359 -- Within_Internal_Subprogram --
13360 --------------------------------
13362 function Within_Internal_Subprogram return Boolean is
13363 S : Entity_Id;
13365 begin
13366 S := Current_Scope;
13367 while Present (S) and then not Is_Subprogram (S) loop
13368 S := Scope (S);
13369 end loop;
13371 return Present (S)
13372 and then Get_TSS_Name (S) /= TSS_Null
13373 and then not Is_Predicate_Function (S)
13374 and then not Is_Predicate_Function_M (S);
13375 end Within_Internal_Subprogram;
13377 end Exp_Util;