2018-08-29 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / exp_util.adb
blob314e3ee97cd1e353c351b18ea0b6e9b0cce62985
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-2018, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Exp_Aggr; use Exp_Aggr;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch11; use Exp_Ch11;
38 with Ghost; use Ghost;
39 with Inline; use Inline;
40 with Itypes; use Itypes;
41 with Lib; use Lib;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Ttypes; use Ttypes;
66 with Urealp; use Urealp;
67 with Validsw; use Validsw;
69 with GNAT.HTable;
70 package body Exp_Util is
72 ---------------------------------------------------------
73 -- Handling of inherited class-wide pre/postconditions --
74 ---------------------------------------------------------
76 -- Following AI12-0113, the expression for a class-wide condition is
77 -- transformed for a subprogram that inherits it, by replacing calls
78 -- to primitive operations of the original controlling type into the
79 -- corresponding overriding operations of the derived type. The following
80 -- hash table manages this mapping, and is expanded on demand whenever
81 -- such inherited expression needs to be constructed.
83 -- The mapping is also used to check whether an inherited operation has
84 -- a condition that depends on overridden operations. For such an
85 -- operation we must create a wrapper that is then treated as a normal
86 -- overriding. In SPARK mode such operations are illegal.
88 -- For a given root type there may be several type extensions with their
89 -- own overriding operations, so at various times a given operation of
90 -- the root will be mapped into different overridings. The root type is
91 -- also mapped into the current type extension to indicate that its
92 -- operations are mapped into the overriding operations of that current
93 -- type extension.
95 -- The contents of the map are as follows:
97 -- Key Value
99 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
100 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
101 -- Discriminant (Entity_Id) Expression (Node_Id)
102 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
103 -- Type (Entity_Id) Type (Entity_Id)
105 Type_Map_Size : constant := 511;
107 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
108 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
110 package Type_Map is new GNAT.HTable.Simple_HTable
111 (Header_Num => Type_Map_Header,
112 Key => Entity_Id,
113 Element => Node_Or_Entity_Id,
114 No_element => Empty,
115 Hash => Type_Map_Hash,
116 Equal => "=");
118 -----------------------
119 -- Local Subprograms --
120 -----------------------
122 function Build_Task_Array_Image
123 (Loc : Source_Ptr;
124 Id_Ref : Node_Id;
125 A_Type : Entity_Id;
126 Dyn : Boolean := False) return Node_Id;
127 -- Build function to generate the image string for a task that is an array
128 -- component, concatenating the images of each index. To avoid storage
129 -- leaks, the string is built with successive slice assignments. The flag
130 -- Dyn indicates whether this is called for the initialization procedure of
131 -- an array of tasks, or for the name of a dynamically created task that is
132 -- assigned to an indexed component.
134 function Build_Task_Image_Function
135 (Loc : Source_Ptr;
136 Decls : List_Id;
137 Stats : List_Id;
138 Res : Entity_Id) return Node_Id;
139 -- Common processing for Task_Array_Image and Task_Record_Image. Build
140 -- function body that computes image.
142 procedure Build_Task_Image_Prefix
143 (Loc : Source_Ptr;
144 Len : out Entity_Id;
145 Res : out Entity_Id;
146 Pos : out Entity_Id;
147 Prefix : Entity_Id;
148 Sum : Node_Id;
149 Decls : List_Id;
150 Stats : List_Id);
151 -- Common processing for Task_Array_Image and Task_Record_Image. Create
152 -- local variables and assign prefix of name to result string.
154 function Build_Task_Record_Image
155 (Loc : Source_Ptr;
156 Id_Ref : Node_Id;
157 Dyn : Boolean := False) return Node_Id;
158 -- Build function to generate the image string for a task that is a record
159 -- component. Concatenate name of variable with that of selector. The flag
160 -- Dyn indicates whether this is called for the initialization procedure of
161 -- record with task components, or for a dynamically created task that is
162 -- assigned to a selected component.
164 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
165 -- Force evaluation of bounds of a slice, which may be given by a range
166 -- or by a subtype indication with or without a constraint.
168 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
169 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
170 -- an assertion expression that should be verified at run time.
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 (Nkind (Expr) = N_Allocator
654 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool))
655 then
656 return;
658 -- Optimize the case where we are using the default Global_Pool_Object,
659 -- and we don't need the heavy finalization machinery.
661 elsif Pool_Id = RTE (RE_Global_Pool_Object)
662 and then not Needs_Finalization (Desig_Typ)
663 then
664 return;
666 -- Do not replicate the machinery if the allocator / free has already
667 -- been expanded and has a custom Allocate / Deallocate.
669 elsif Present (Proc_To_Call)
670 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
671 then
672 return;
673 end if;
675 -- Finalization actions are required when the object to be allocated or
676 -- deallocated needs these actions and the associated access type is not
677 -- subject to pragma No_Heap_Finalization.
679 Needs_Fin :=
680 Needs_Finalization (Desig_Typ)
681 and then not No_Heap_Finalization (Ptr_Typ);
683 if Needs_Fin then
685 -- Do nothing if the access type may never allocate / deallocate
686 -- objects.
688 if No_Pool_Assigned (Ptr_Typ) then
689 return;
690 end if;
692 -- The allocation / deallocation of a controlled object must be
693 -- chained on / detached from a finalization master.
695 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
697 -- The only other kind of allocation / deallocation supported by this
698 -- routine is on / from a subpool.
700 elsif Nkind (Expr) = N_Allocator
701 and then No (Subpool_Handle_Name (Expr))
702 then
703 return;
704 end if;
706 declare
707 Loc : constant Source_Ptr := Sloc (N);
708 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
709 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
710 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
711 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
713 Actuals : List_Id;
714 Fin_Addr_Id : Entity_Id;
715 Fin_Mas_Act : Node_Id;
716 Fin_Mas_Id : Entity_Id;
717 Proc_To_Call : Entity_Id;
718 Subpool : Node_Id := Empty;
720 begin
721 -- Step 1: Construct all the actuals for the call to library routine
722 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
724 -- a) Storage pool
726 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
728 if Is_Allocate then
730 -- b) Subpool
732 if Nkind (Expr) = N_Allocator then
733 Subpool := Subpool_Handle_Name (Expr);
734 end if;
736 -- If a subpool is present it can be an arbitrary name, so make
737 -- the actual by copying the tree.
739 if Present (Subpool) then
740 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
741 else
742 Append_To (Actuals, Make_Null (Loc));
743 end if;
745 -- c) Finalization master
747 if Needs_Fin then
748 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
749 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
751 -- Handle the case where the master is actually a pointer to a
752 -- master. This case arises in build-in-place functions.
754 if Is_Access_Type (Etype (Fin_Mas_Id)) then
755 Append_To (Actuals, Fin_Mas_Act);
756 else
757 Append_To (Actuals,
758 Make_Attribute_Reference (Loc,
759 Prefix => Fin_Mas_Act,
760 Attribute_Name => Name_Unrestricted_Access));
761 end if;
762 else
763 Append_To (Actuals, Make_Null (Loc));
764 end if;
766 -- d) Finalize_Address
768 -- Primitive Finalize_Address is never generated in CodePeer mode
769 -- since it contains an Unchecked_Conversion.
771 if Needs_Fin and then not CodePeer_Mode then
772 Fin_Addr_Id := Finalize_Address (Desig_Typ);
773 pragma Assert (Present (Fin_Addr_Id));
775 Append_To (Actuals,
776 Make_Attribute_Reference (Loc,
777 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
778 Attribute_Name => Name_Unrestricted_Access));
779 else
780 Append_To (Actuals, Make_Null (Loc));
781 end if;
782 end if;
784 -- e) Address
785 -- f) Storage_Size
786 -- g) Alignment
788 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
789 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
791 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
792 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
794 -- For deallocation of class-wide types we obtain the value of
795 -- alignment from the Type Specific Record of the deallocated object.
796 -- This is needed because the frontend expansion of class-wide types
797 -- into equivalent types confuses the back end.
799 else
800 -- Generate:
801 -- Obj.all'Alignment
803 -- ... because 'Alignment applied to class-wide types is expanded
804 -- into the code that reads the value of alignment from the TSD
805 -- (see Expand_N_Attribute_Reference)
807 Append_To (Actuals,
808 Unchecked_Convert_To (RTE (RE_Storage_Offset),
809 Make_Attribute_Reference (Loc,
810 Prefix =>
811 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
812 Attribute_Name => Name_Alignment)));
813 end if;
815 -- h) Is_Controlled
817 if Needs_Fin then
818 Is_Controlled : declare
819 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
820 Flag_Expr : Node_Id;
821 Param : Node_Id;
822 Pref : Node_Id;
823 Temp : Node_Id;
825 begin
826 if Is_Allocate then
827 Temp := Find_Object (Expression (Expr));
828 else
829 Temp := Expr;
830 end if;
832 -- Processing for allocations where the expression is a subtype
833 -- indication.
835 if Is_Allocate
836 and then Is_Entity_Name (Temp)
837 and then Is_Type (Entity (Temp))
838 then
839 Flag_Expr :=
840 New_Occurrence_Of
841 (Boolean_Literals
842 (Needs_Finalization (Entity (Temp))), Loc);
844 -- The allocation / deallocation of a class-wide object relies
845 -- on a runtime check to determine whether the object is truly
846 -- controlled or not. Depending on this check, the finalization
847 -- machinery will request or reclaim extra storage reserved for
848 -- a list header.
850 elsif Is_Class_Wide_Type (Desig_Typ) then
852 -- Detect a special case where interface class-wide types
853 -- are involved as the object appears as:
855 -- Tag_Ptr (Base_Address (<object>'Address))
857 -- The expression already yields the proper tag, generate:
859 -- Temp.all
861 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
862 Param :=
863 Make_Explicit_Dereference (Loc,
864 Prefix => Relocate_Node (Temp));
866 -- In the default case, obtain the tag of the object about
867 -- to be allocated / deallocated. Generate:
869 -- Temp'Tag
871 -- If the object is an unchecked conversion (typically to
872 -- an access to class-wide type), we must preserve the
873 -- conversion to ensure that the object is seen as tagged
874 -- in the code that follows.
876 else
877 Pref := Temp;
879 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
880 then
881 Pref := Parent (Pref);
882 end if;
884 Param :=
885 Make_Attribute_Reference (Loc,
886 Prefix => Relocate_Node (Pref),
887 Attribute_Name => Name_Tag);
888 end if;
890 -- Generate:
891 -- Needs_Finalization (<Param>)
893 Flag_Expr :=
894 Make_Function_Call (Loc,
895 Name =>
896 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
897 Parameter_Associations => New_List (Param));
899 -- Processing for generic actuals
901 elsif Is_Generic_Actual_Type (Desig_Typ) then
902 Flag_Expr :=
903 New_Occurrence_Of (Boolean_Literals
904 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
906 -- The object does not require any specialized checks, it is
907 -- known to be controlled.
909 else
910 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
911 end if;
913 -- Create the temporary which represents the finalization state
914 -- of the expression. Generate:
916 -- F : constant Boolean := <Flag_Expr>;
918 Insert_Action (N,
919 Make_Object_Declaration (Loc,
920 Defining_Identifier => Flag_Id,
921 Constant_Present => True,
922 Object_Definition =>
923 New_Occurrence_Of (Standard_Boolean, Loc),
924 Expression => Flag_Expr));
926 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
927 end Is_Controlled;
929 -- The object is not controlled
931 else
932 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
933 end if;
935 -- i) On_Subpool
937 if Is_Allocate then
938 Append_To (Actuals,
939 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
940 end if;
942 -- Step 2: Build a wrapper Allocate / Deallocate which internally
943 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
945 -- Select the proper routine to call
947 if Is_Allocate then
948 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
949 else
950 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
951 end if;
953 -- Create a custom Allocate / Deallocate routine which has identical
954 -- profile to that of System.Storage_Pools.
956 Insert_Action (N,
957 Make_Subprogram_Body (Loc,
958 Specification =>
960 -- procedure Pnn
962 Make_Procedure_Specification (Loc,
963 Defining_Unit_Name => Proc_Id,
964 Parameter_Specifications => New_List (
966 -- P : Root_Storage_Pool
968 Make_Parameter_Specification (Loc,
969 Defining_Identifier => Make_Temporary (Loc, 'P'),
970 Parameter_Type =>
971 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
973 -- A : [out] Address
975 Make_Parameter_Specification (Loc,
976 Defining_Identifier => Addr_Id,
977 Out_Present => Is_Allocate,
978 Parameter_Type =>
979 New_Occurrence_Of (RTE (RE_Address), Loc)),
981 -- S : Storage_Count
983 Make_Parameter_Specification (Loc,
984 Defining_Identifier => Size_Id,
985 Parameter_Type =>
986 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
988 -- L : Storage_Count
990 Make_Parameter_Specification (Loc,
991 Defining_Identifier => Alig_Id,
992 Parameter_Type =>
993 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
995 Declarations => No_List,
997 Handled_Statement_Sequence =>
998 Make_Handled_Sequence_Of_Statements (Loc,
999 Statements => New_List (
1000 Make_Procedure_Call_Statement (Loc,
1001 Name =>
1002 New_Occurrence_Of (Proc_To_Call, Loc),
1003 Parameter_Associations => Actuals)))),
1004 Suppress => All_Checks);
1006 -- The newly generated Allocate / Deallocate becomes the default
1007 -- procedure to call when the back end processes the allocation /
1008 -- deallocation.
1010 if Is_Allocate then
1011 Set_Procedure_To_Call (Expr, Proc_Id);
1012 else
1013 Set_Procedure_To_Call (N, Proc_Id);
1014 end if;
1015 end;
1016 end Build_Allocate_Deallocate_Proc;
1018 -------------------------------
1019 -- Build_Abort_Undefer_Block --
1020 -------------------------------
1022 function Build_Abort_Undefer_Block
1023 (Loc : Source_Ptr;
1024 Stmts : List_Id;
1025 Context : Node_Id) return Node_Id
1027 Exceptions_OK : constant Boolean :=
1028 not Restriction_Active (No_Exception_Propagation);
1030 AUD : Entity_Id;
1031 Blk : Node_Id;
1032 Blk_Id : Entity_Id;
1033 HSS : Node_Id;
1035 begin
1036 -- The block should be generated only when undeferring abort in the
1037 -- context of a potential exception.
1039 pragma Assert (Abort_Allowed and Exceptions_OK);
1041 -- Generate:
1042 -- begin
1043 -- <Stmts>
1044 -- at end
1045 -- Abort_Undefer_Direct;
1046 -- end;
1048 AUD := RTE (RE_Abort_Undefer_Direct);
1050 HSS :=
1051 Make_Handled_Sequence_Of_Statements (Loc,
1052 Statements => Stmts,
1053 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1055 Blk :=
1056 Make_Block_Statement (Loc,
1057 Handled_Statement_Sequence => HSS);
1058 Set_Is_Abort_Block (Blk);
1060 Add_Block_Identifier (Blk, Blk_Id);
1061 Expand_At_End_Handler (HSS, Blk_Id);
1063 -- Present the Abort_Undefer_Direct function to the back end to inline
1064 -- the call to the routine.
1066 Add_Inlined_Body (AUD, Context);
1068 return Blk;
1069 end Build_Abort_Undefer_Block;
1071 ---------------------------------
1072 -- Build_Class_Wide_Expression --
1073 ---------------------------------
1075 procedure Build_Class_Wide_Expression
1076 (Prag : Node_Id;
1077 Subp : Entity_Id;
1078 Par_Subp : Entity_Id;
1079 Adjust_Sloc : Boolean;
1080 Needs_Wrapper : out Boolean)
1082 function Replace_Entity (N : Node_Id) return Traverse_Result;
1083 -- Replace reference to formal of inherited operation or to primitive
1084 -- operation of root type, with corresponding entity for derived type,
1085 -- when constructing the class-wide condition of an overriding
1086 -- subprogram.
1088 --------------------
1089 -- Replace_Entity --
1090 --------------------
1092 function Replace_Entity (N : Node_Id) return Traverse_Result is
1093 New_E : Entity_Id;
1095 begin
1096 if Adjust_Sloc then
1097 Adjust_Inherited_Pragma_Sloc (N);
1098 end if;
1100 if Nkind (N) = N_Identifier
1101 and then Present (Entity (N))
1102 and then
1103 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1104 and then
1105 (Nkind (Parent (N)) /= N_Attribute_Reference
1106 or else Attribute_Name (Parent (N)) /= Name_Class)
1107 then
1108 -- The replacement does not apply to dispatching calls within the
1109 -- condition, but only to calls whose static tag is that of the
1110 -- parent type.
1112 if Is_Subprogram (Entity (N))
1113 and then Nkind (Parent (N)) = N_Function_Call
1114 and then Present (Controlling_Argument (Parent (N)))
1115 then
1116 return OK;
1117 end if;
1119 -- Determine whether entity has a renaming
1121 New_E := Type_Map.Get (Entity (N));
1123 if Present (New_E) then
1124 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1126 -- AI12-0166: a precondition for a protected operation
1127 -- cannot include an internal call to a protected function
1128 -- of the type. In the case of an inherited condition for an
1129 -- overriding operation, both the operation and the function
1130 -- are given by primitive wrappers.
1132 if Ekind (New_E) = E_Function
1133 and then Is_Primitive_Wrapper (New_E)
1134 and then Is_Primitive_Wrapper (Subp)
1135 and then Scope (Subp) = Scope (New_E)
1136 then
1137 Error_Msg_Node_2 := Wrapped_Entity (Subp);
1138 Error_Msg_NE
1139 ("internal call to& cannot appear in inherited "
1140 & "precondition of protected operation&",
1141 N, Wrapped_Entity (New_E));
1142 end if;
1144 -- If the entity is an overridden primitive and we are not
1145 -- in GNATprove mode, we must build a wrapper for the current
1146 -- inherited operation. If the reference is the prefix of an
1147 -- attribute such as 'Result (or others ???) there is no need
1148 -- for a wrapper: the condition is just rewritten in terms of
1149 -- the inherited subprogram.
1151 if Is_Subprogram (New_E)
1152 and then Nkind (Parent (N)) /= N_Attribute_Reference
1153 and then not GNATprove_Mode
1154 then
1155 Needs_Wrapper := True;
1156 end if;
1157 end if;
1159 -- Check that there are no calls left to abstract operations if
1160 -- the current subprogram is not abstract.
1162 if Nkind (Parent (N)) = N_Function_Call
1163 and then N = Name (Parent (N))
1164 then
1165 if not Is_Abstract_Subprogram (Subp)
1166 and then Is_Abstract_Subprogram (Entity (N))
1167 then
1168 Error_Msg_Sloc := Sloc (Current_Scope);
1169 Error_Msg_Node_2 := Subp;
1170 if Comes_From_Source (Subp) then
1171 Error_Msg_NE
1172 ("cannot call abstract subprogram & in inherited "
1173 & "condition for&#", Subp, Entity (N));
1174 else
1175 Error_Msg_NE
1176 ("cannot call abstract subprogram & in inherited "
1177 & "condition for inherited&#", Subp, Entity (N));
1178 end if;
1180 -- In SPARK mode, reject an inherited condition for an
1181 -- inherited operation if it contains a call to an overriding
1182 -- operation, because this implies that the pre/postconditions
1183 -- of the inherited operation have changed silently.
1185 elsif SPARK_Mode = On
1186 and then Warn_On_Suspicious_Contract
1187 and then Present (Alias (Subp))
1188 and then Present (New_E)
1189 and then Comes_From_Source (New_E)
1190 then
1191 Error_Msg_N
1192 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1193 Parent (Subp));
1194 Error_Msg_Sloc := Sloc (New_E);
1195 Error_Msg_Node_2 := Subp;
1196 Error_Msg_NE
1197 ("\overriding of&# forces overriding of&",
1198 Parent (Subp), New_E);
1199 end if;
1200 end if;
1202 -- Update type of function call node, which should be the same as
1203 -- the function's return type.
1205 if Is_Subprogram (Entity (N))
1206 and then Nkind (Parent (N)) = N_Function_Call
1207 then
1208 Set_Etype (Parent (N), Etype (Entity (N)));
1209 end if;
1211 -- The whole expression will be reanalyzed
1213 elsif Nkind (N) in N_Has_Etype then
1214 Set_Analyzed (N, False);
1215 end if;
1217 return OK;
1218 end Replace_Entity;
1220 procedure Replace_Condition_Entities is
1221 new Traverse_Proc (Replace_Entity);
1223 -- Local variables
1225 Par_Formal : Entity_Id;
1226 Subp_Formal : Entity_Id;
1228 -- Start of processing for Build_Class_Wide_Expression
1230 begin
1231 Needs_Wrapper := False;
1233 -- Add mapping from old formals to new formals
1235 Par_Formal := First_Formal (Par_Subp);
1236 Subp_Formal := First_Formal (Subp);
1238 while Present (Par_Formal) and then Present (Subp_Formal) loop
1239 Type_Map.Set (Par_Formal, Subp_Formal);
1240 Next_Formal (Par_Formal);
1241 Next_Formal (Subp_Formal);
1242 end loop;
1244 Replace_Condition_Entities (Prag);
1245 end Build_Class_Wide_Expression;
1247 --------------------
1248 -- Build_DIC_Call --
1249 --------------------
1251 function Build_DIC_Call
1252 (Loc : Source_Ptr;
1253 Obj_Id : Entity_Id;
1254 Typ : Entity_Id) return Node_Id
1256 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1257 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1259 begin
1260 return
1261 Make_Procedure_Call_Statement (Loc,
1262 Name => New_Occurrence_Of (Proc_Id, Loc),
1263 Parameter_Associations => New_List (
1264 Make_Unchecked_Type_Conversion (Loc,
1265 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1266 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1267 end Build_DIC_Call;
1269 ------------------------------
1270 -- Build_DIC_Procedure_Body --
1271 ------------------------------
1273 -- WARNING: This routine manages Ghost regions. Return statements must be
1274 -- replaced by gotos which jump to the end of the routine and restore the
1275 -- Ghost mode.
1277 procedure Build_DIC_Procedure_Body
1278 (Typ : Entity_Id;
1279 For_Freeze : Boolean := False)
1281 procedure Add_DIC_Check
1282 (DIC_Prag : Node_Id;
1283 DIC_Expr : Node_Id;
1284 Stmts : in out List_Id);
1285 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1286 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1287 -- is added to list Stmts.
1289 procedure Add_Inherited_DIC
1290 (DIC_Prag : Node_Id;
1291 Par_Typ : Entity_Id;
1292 Deriv_Typ : Entity_Id;
1293 Stmts : in out List_Id);
1294 -- Add a runtime check to verify the assertion expression of inherited
1295 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1296 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1297 -- pragma. All generated code is added to list Stmts.
1299 procedure Add_Inherited_Tagged_DIC
1300 (DIC_Prag : Node_Id;
1301 Par_Typ : Entity_Id;
1302 Deriv_Typ : Entity_Id;
1303 Stmts : in out List_Id);
1304 -- Add a runtime check to verify assertion expression DIC_Expr of
1305 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1306 -- postcondition-like runtime semantics to the check. Par_Typ is the
1307 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1308 -- derived type inheriting the DIC pragma. All generated code is added
1309 -- to list Stmts.
1311 procedure Add_Own_DIC
1312 (DIC_Prag : Node_Id;
1313 DIC_Typ : Entity_Id;
1314 Stmts : in out List_Id);
1315 -- Add a runtime check to verify the assertion expression of pragma
1316 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1317 -- is added to list Stmts.
1319 -------------------
1320 -- Add_DIC_Check --
1321 -------------------
1323 procedure Add_DIC_Check
1324 (DIC_Prag : Node_Id;
1325 DIC_Expr : Node_Id;
1326 Stmts : in out List_Id)
1328 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1329 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1331 begin
1332 -- The DIC pragma is ignored, nothing left to do
1334 if Is_Ignored (DIC_Prag) then
1335 null;
1337 -- Otherwise the DIC expression must be checked at run time.
1338 -- Generate:
1340 -- pragma Check (<Nam>, <DIC_Expr>);
1342 else
1343 Append_New_To (Stmts,
1344 Make_Pragma (Loc,
1345 Pragma_Identifier =>
1346 Make_Identifier (Loc, Name_Check),
1348 Pragma_Argument_Associations => New_List (
1349 Make_Pragma_Argument_Association (Loc,
1350 Expression => Make_Identifier (Loc, Nam)),
1352 Make_Pragma_Argument_Association (Loc,
1353 Expression => DIC_Expr))));
1354 end if;
1355 end Add_DIC_Check;
1357 -----------------------
1358 -- Add_Inherited_DIC --
1359 -----------------------
1361 procedure Add_Inherited_DIC
1362 (DIC_Prag : Node_Id;
1363 Par_Typ : Entity_Id;
1364 Deriv_Typ : Entity_Id;
1365 Stmts : in out List_Id)
1367 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1368 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1369 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1370 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1371 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1373 begin
1374 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1376 -- Verify the inherited DIC assertion expression by calling the DIC
1377 -- procedure of the parent type.
1379 -- Generate:
1380 -- <Par_Typ>DIC (Par_Typ (_object));
1382 Append_New_To (Stmts,
1383 Make_Procedure_Call_Statement (Loc,
1384 Name => New_Occurrence_Of (Par_Proc, Loc),
1385 Parameter_Associations => New_List (
1386 Convert_To
1387 (Typ => Etype (Par_Obj),
1388 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1389 end Add_Inherited_DIC;
1391 ------------------------------
1392 -- Add_Inherited_Tagged_DIC --
1393 ------------------------------
1395 procedure Add_Inherited_Tagged_DIC
1396 (DIC_Prag : Node_Id;
1397 Par_Typ : Entity_Id;
1398 Deriv_Typ : Entity_Id;
1399 Stmts : in out List_Id)
1401 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1402 DIC_Args : constant List_Id :=
1403 Pragma_Argument_Associations (DIC_Prag);
1404 DIC_Arg : constant Node_Id := First (DIC_Args);
1405 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1406 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1408 Expr : Node_Id;
1410 begin
1411 -- The processing of an inherited DIC assertion expression starts off
1412 -- with a copy of the original parent expression where all references
1413 -- to the parent type have already been replaced with references to
1414 -- the _object formal parameter of the parent type's DIC procedure.
1416 pragma Assert (Present (DIC_Expr));
1417 Expr := New_Copy_Tree (DIC_Expr);
1419 -- Perform the following substitutions:
1421 -- * Replace a reference to the _object parameter of the parent
1422 -- type's DIC procedure with a reference to the _object parameter
1423 -- of the derived types' DIC procedure.
1425 -- * Replace a reference to a discriminant of the parent type with
1426 -- a suitable value from the point of view of the derived type.
1428 -- * Replace a call to an overridden parent primitive with a call
1429 -- to the overriding derived type primitive.
1431 -- * Replace a call to an inherited parent primitive with a call to
1432 -- the internally-generated inherited derived type primitive.
1434 -- Note that primitives defined in the private part are automatically
1435 -- handled by the overriding/inheritance mechanism and do not require
1436 -- an extra replacement pass.
1438 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1440 Replace_References
1441 (Expr => Expr,
1442 Par_Typ => Par_Typ,
1443 Deriv_Typ => Deriv_Typ,
1444 Par_Obj => First_Formal (Par_Proc),
1445 Deriv_Obj => First_Formal (Deriv_Proc));
1447 -- Once the DIC assertion expression is fully processed, add a check
1448 -- to the statements of the DIC procedure.
1450 Add_DIC_Check
1451 (DIC_Prag => DIC_Prag,
1452 DIC_Expr => Expr,
1453 Stmts => Stmts);
1454 end Add_Inherited_Tagged_DIC;
1456 -----------------
1457 -- Add_Own_DIC --
1458 -----------------
1460 procedure Add_Own_DIC
1461 (DIC_Prag : Node_Id;
1462 DIC_Typ : Entity_Id;
1463 Stmts : in out List_Id)
1465 DIC_Args : constant List_Id :=
1466 Pragma_Argument_Associations (DIC_Prag);
1467 DIC_Arg : constant Node_Id := First (DIC_Args);
1468 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1469 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1470 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1471 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1473 procedure Preanalyze_Own_DIC_For_ASIS;
1474 -- Preanalyze the original DIC expression of an aspect or a source
1475 -- pragma for ASIS.
1477 ---------------------------------
1478 -- Preanalyze_Own_DIC_For_ASIS --
1479 ---------------------------------
1481 procedure Preanalyze_Own_DIC_For_ASIS is
1482 Expr : Node_Id := Empty;
1484 begin
1485 -- The DIC pragma is a source construct, preanalyze the original
1486 -- expression of the pragma.
1488 if Comes_From_Source (DIC_Prag) then
1489 Expr := DIC_Expr;
1491 -- Otherwise preanalyze the expression of the corresponding aspect
1493 elsif Present (DIC_Asp) then
1494 Expr := Expression (DIC_Asp);
1495 end if;
1497 -- The expression must be subjected to the same substitutions as
1498 -- the copy used in the generation of the runtime check.
1500 if Present (Expr) then
1501 Replace_Type_References
1502 (Expr => Expr,
1503 Typ => DIC_Typ,
1504 Obj_Id => Obj_Id);
1506 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1507 end if;
1508 end Preanalyze_Own_DIC_For_ASIS;
1510 -- Local variables
1512 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1514 Expr : Node_Id;
1516 -- Start of processing for Add_Own_DIC
1518 begin
1519 pragma Assert (Present (DIC_Expr));
1520 Expr := New_Copy_Tree (DIC_Expr);
1522 -- Perform the following substitution:
1524 -- * Replace the current instance of DIC_Typ with a reference to
1525 -- the _object formal parameter of the DIC procedure.
1527 Replace_Type_References
1528 (Expr => Expr,
1529 Typ => DIC_Typ,
1530 Obj_Id => Obj_Id);
1532 -- Preanalyze the DIC expression to detect errors and at the same
1533 -- time capture the visibility of the proper package part.
1535 Set_Parent (Expr, Typ_Decl);
1536 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1538 -- Save a copy of the expression with all replacements and analysis
1539 -- already taken place in case a derived type inherits the pragma.
1540 -- The copy will be used as the foundation of the derived type's own
1541 -- version of the DIC assertion expression.
1543 if Is_Tagged_Type (DIC_Typ) then
1544 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1545 end if;
1547 -- If the pragma comes from an aspect specification, replace the
1548 -- saved expression because all type references must be substituted
1549 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1550 -- routines.
1552 if Present (DIC_Asp) then
1553 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1554 end if;
1556 -- Preanalyze the original DIC expression for ASIS
1558 if ASIS_Mode then
1559 Preanalyze_Own_DIC_For_ASIS;
1560 end if;
1562 -- Once the DIC assertion expression is fully processed, add a check
1563 -- to the statements of the DIC procedure.
1565 Add_DIC_Check
1566 (DIC_Prag => DIC_Prag,
1567 DIC_Expr => Expr,
1568 Stmts => Stmts);
1569 end Add_Own_DIC;
1571 -- Local variables
1573 Loc : constant Source_Ptr := Sloc (Typ);
1575 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1576 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1577 -- Save the Ghost-related attributes to restore on exit
1579 DIC_Prag : Node_Id;
1580 DIC_Typ : Entity_Id;
1581 Dummy_1 : Entity_Id;
1582 Dummy_2 : Entity_Id;
1583 Proc_Body : Node_Id;
1584 Proc_Body_Id : Entity_Id;
1585 Proc_Decl : Node_Id;
1586 Proc_Id : Entity_Id;
1587 Stmts : List_Id := No_List;
1589 Build_Body : Boolean := False;
1590 -- Flag set when the type requires a DIC procedure body to be built
1592 Work_Typ : Entity_Id;
1593 -- The working type
1595 -- Start of processing for Build_DIC_Procedure_Body
1597 begin
1598 Work_Typ := Base_Type (Typ);
1600 -- Do not process class-wide types as these are Itypes, but lack a first
1601 -- subtype (see below).
1603 if Is_Class_Wide_Type (Work_Typ) then
1604 return;
1606 -- Do not process the underlying full view of a private type. There is
1607 -- no way to get back to the partial view, plus the body will be built
1608 -- by the full view or the base type.
1610 elsif Is_Underlying_Full_View (Work_Typ) then
1611 return;
1613 -- Use the first subtype when dealing with various base types
1615 elsif Is_Itype (Work_Typ) then
1616 Work_Typ := First_Subtype (Work_Typ);
1618 -- The input denotes the corresponding record type of a protected or a
1619 -- task type. Work with the concurrent type because the corresponding
1620 -- record type may not be visible to clients of the type.
1622 elsif Ekind (Work_Typ) = E_Record_Type
1623 and then Is_Concurrent_Record_Type (Work_Typ)
1624 then
1625 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1626 end if;
1628 -- The working type may be subject to pragma Ghost. Set the mode now to
1629 -- ensure that the DIC procedure is properly marked as Ghost.
1631 Set_Ghost_Mode (Work_Typ);
1633 -- The working type must be either define a DIC pragma of its own or
1634 -- inherit one from a parent type.
1636 pragma Assert (Has_DIC (Work_Typ));
1638 -- Recover the type which defines the DIC pragma. This is either the
1639 -- working type itself or a parent type when the pragma is inherited.
1641 DIC_Typ := Find_DIC_Type (Work_Typ);
1642 pragma Assert (Present (DIC_Typ));
1644 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1645 pragma Assert (Present (DIC_Prag));
1647 -- Nothing to do if pragma DIC appears without an argument or its sole
1648 -- argument is "null".
1650 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1651 goto Leave;
1652 end if;
1654 -- The working type may lack a DIC procedure declaration. This may be
1655 -- due to several reasons:
1657 -- * The working type's own DIC pragma does not contain a verifiable
1658 -- assertion expression. In this case there is no need to build a
1659 -- DIC procedure because there is nothing to check.
1661 -- * The working type derives from a parent type. In this case a DIC
1662 -- procedure should be built only when the inherited DIC pragma has
1663 -- a verifiable assertion expression.
1665 Proc_Id := DIC_Procedure (Work_Typ);
1667 -- Build a DIC procedure declaration when the working type derives from
1668 -- a parent type.
1670 if No (Proc_Id) then
1671 Build_DIC_Procedure_Declaration (Work_Typ);
1672 Proc_Id := DIC_Procedure (Work_Typ);
1673 end if;
1675 -- At this point there should be a DIC procedure declaration
1677 pragma Assert (Present (Proc_Id));
1678 Proc_Decl := Unit_Declaration_Node (Proc_Id);
1680 -- Nothing to do if the DIC procedure already has a body
1682 if Present (Corresponding_Body (Proc_Decl)) then
1683 goto Leave;
1684 end if;
1686 -- Emulate the environment of the DIC procedure by installing its scope
1687 -- and formal parameters.
1689 Push_Scope (Proc_Id);
1690 Install_Formals (Proc_Id);
1692 -- The working type defines its own DIC pragma. Replace the current
1693 -- instance of the working type with the formal of the DIC procedure.
1694 -- Note that there is no need to consider inherited DIC pragmas from
1695 -- parent types because the working type's DIC pragma "hides" all
1696 -- inherited DIC pragmas.
1698 if Has_Own_DIC (Work_Typ) then
1699 pragma Assert (DIC_Typ = Work_Typ);
1701 Add_Own_DIC
1702 (DIC_Prag => DIC_Prag,
1703 DIC_Typ => DIC_Typ,
1704 Stmts => Stmts);
1706 Build_Body := True;
1708 -- Otherwise the working type inherits a DIC pragma from a parent type.
1709 -- This processing is carried out when the type is frozen because the
1710 -- state of all parent discriminants is known at that point. Note that
1711 -- it is semantically sound to delay the creation of the DIC procedure
1712 -- body till the freeze point. If the type has a DIC pragma of its own,
1713 -- then the DIC procedure body would have already been constructed at
1714 -- the end of the visible declarations and all parent DIC pragmas are
1715 -- effectively "hidden" and irrelevant.
1717 elsif For_Freeze then
1718 pragma Assert (Has_Inherited_DIC (Work_Typ));
1719 pragma Assert (DIC_Typ /= Work_Typ);
1721 -- The working type is tagged. The verification of the assertion
1722 -- expression is subject to the same semantics as class-wide pre-
1723 -- and postconditions.
1725 if Is_Tagged_Type (Work_Typ) then
1726 Add_Inherited_Tagged_DIC
1727 (DIC_Prag => DIC_Prag,
1728 Par_Typ => DIC_Typ,
1729 Deriv_Typ => Work_Typ,
1730 Stmts => Stmts);
1732 -- Otherwise the working type is not tagged. Verify the assertion
1733 -- expression of the inherited DIC pragma by directly calling the
1734 -- DIC procedure of the parent type.
1736 else
1737 Add_Inherited_DIC
1738 (DIC_Prag => DIC_Prag,
1739 Par_Typ => DIC_Typ,
1740 Deriv_Typ => Work_Typ,
1741 Stmts => Stmts);
1742 end if;
1744 Build_Body := True;
1745 end if;
1747 End_Scope;
1749 if Build_Body then
1751 -- Produce an empty completing body in the following cases:
1752 -- * Assertions are disabled
1753 -- * The DIC Assertion_Policy is Ignore
1755 if No (Stmts) then
1756 Stmts := New_List (Make_Null_Statement (Loc));
1757 end if;
1759 -- Generate:
1760 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1761 -- begin
1762 -- <Stmts>
1763 -- end <Work_Typ>DIC;
1765 Proc_Body :=
1766 Make_Subprogram_Body (Loc,
1767 Specification =>
1768 Copy_Subprogram_Spec (Parent (Proc_Id)),
1769 Declarations => Empty_List,
1770 Handled_Statement_Sequence =>
1771 Make_Handled_Sequence_Of_Statements (Loc,
1772 Statements => Stmts));
1773 Proc_Body_Id := Defining_Entity (Proc_Body);
1775 -- Perform minor decoration in case the body is not analyzed
1777 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1778 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1779 Set_Scope (Proc_Body_Id, Current_Scope);
1780 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
1781 Set_SPARK_Pragma_Inherited
1782 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
1784 -- Link both spec and body to avoid generating duplicates
1786 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1787 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1789 -- The body should not be inserted into the tree when the context
1790 -- is ASIS or a generic unit because it is not part of the template.
1791 -- Note that the body must still be generated in order to resolve the
1792 -- DIC assertion expression.
1794 if ASIS_Mode or Inside_A_Generic then
1795 null;
1797 -- Semi-insert the body into the tree for GNATprove by setting its
1798 -- Parent field. This allows for proper upstream tree traversals.
1800 elsif GNATprove_Mode then
1801 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1803 -- Otherwise the body is part of the freezing actions of the working
1804 -- type.
1806 else
1807 Append_Freeze_Action (Work_Typ, Proc_Body);
1808 end if;
1809 end if;
1811 <<Leave>>
1812 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1813 end Build_DIC_Procedure_Body;
1815 -------------------------------------
1816 -- Build_DIC_Procedure_Declaration --
1817 -------------------------------------
1819 -- WARNING: This routine manages Ghost regions. Return statements must be
1820 -- replaced by gotos which jump to the end of the routine and restore the
1821 -- Ghost mode.
1823 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1824 Loc : constant Source_Ptr := Sloc (Typ);
1826 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1827 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1828 -- Save the Ghost-related attributes to restore on exit
1830 DIC_Prag : Node_Id;
1831 DIC_Typ : Entity_Id;
1832 Proc_Decl : Node_Id;
1833 Proc_Id : Entity_Id;
1834 Typ_Decl : Node_Id;
1836 CRec_Typ : Entity_Id;
1837 -- The corresponding record type of Full_Typ
1839 Full_Base : Entity_Id;
1840 -- The base type of Full_Typ
1842 Full_Typ : Entity_Id;
1843 -- The full view of working type
1845 Obj_Id : Entity_Id;
1846 -- The _object formal parameter of the DIC procedure
1848 Priv_Typ : Entity_Id;
1849 -- The partial view of working type
1851 Work_Typ : Entity_Id;
1852 -- The working type
1854 begin
1855 Work_Typ := Base_Type (Typ);
1857 -- Do not process class-wide types as these are Itypes, but lack a first
1858 -- subtype (see below).
1860 if Is_Class_Wide_Type (Work_Typ) then
1861 return;
1863 -- Do not process the underlying full view of a private type. There is
1864 -- no way to get back to the partial view, plus the body will be built
1865 -- by the full view or the base type.
1867 elsif Is_Underlying_Full_View (Work_Typ) then
1868 return;
1870 -- Use the first subtype when dealing with various base types
1872 elsif Is_Itype (Work_Typ) then
1873 Work_Typ := First_Subtype (Work_Typ);
1875 -- The input denotes the corresponding record type of a protected or a
1876 -- task type. Work with the concurrent type because the corresponding
1877 -- record type may not be visible to clients of the type.
1879 elsif Ekind (Work_Typ) = E_Record_Type
1880 and then Is_Concurrent_Record_Type (Work_Typ)
1881 then
1882 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1883 end if;
1885 -- The working type may be subject to pragma Ghost. Set the mode now to
1886 -- ensure that the DIC procedure is properly marked as Ghost.
1888 Set_Ghost_Mode (Work_Typ);
1890 -- The type must be either subject to a DIC pragma or inherit one from a
1891 -- parent type.
1893 pragma Assert (Has_DIC (Work_Typ));
1895 -- Recover the type which defines the DIC pragma. This is either the
1896 -- working type itself or a parent type when the pragma is inherited.
1898 DIC_Typ := Find_DIC_Type (Work_Typ);
1899 pragma Assert (Present (DIC_Typ));
1901 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1902 pragma Assert (Present (DIC_Prag));
1904 -- Nothing to do if pragma DIC appears without an argument or its sole
1905 -- argument is "null".
1907 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1908 goto Leave;
1910 -- Nothing to do if the type already has a DIC procedure
1912 elsif Present (DIC_Procedure (Work_Typ)) then
1913 goto Leave;
1914 end if;
1916 Proc_Id :=
1917 Make_Defining_Identifier (Loc,
1918 Chars =>
1919 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1921 -- Perform minor decoration in case the declaration is not analyzed
1923 Set_Ekind (Proc_Id, E_Procedure);
1924 Set_Etype (Proc_Id, Standard_Void_Type);
1925 Set_Is_DIC_Procedure (Proc_Id);
1926 Set_Scope (Proc_Id, Current_Scope);
1927 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
1928 Set_SPARK_Pragma_Inherited (Proc_Id);
1930 Set_DIC_Procedure (Work_Typ, Proc_Id);
1932 -- The DIC procedure requires debug info when the assertion expression
1933 -- is subject to Source Coverage Obligations.
1935 if Generate_SCO then
1936 Set_Needs_Debug_Info (Proc_Id);
1937 end if;
1939 -- Obtain all views of the input type
1941 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1943 -- Associate the DIC procedure and various relevant flags with all views
1945 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1946 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1947 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1948 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1950 -- The declaration of the DIC procedure must be inserted after the
1951 -- declaration of the partial view as this allows for proper external
1952 -- visibility.
1954 if Present (Priv_Typ) then
1955 Typ_Decl := Declaration_Node (Priv_Typ);
1957 -- Derived types with the full view as parent do not have a partial
1958 -- view. Insert the DIC procedure after the derived type.
1960 else
1961 Typ_Decl := Declaration_Node (Full_Typ);
1962 end if;
1964 -- The type should have a declarative node
1966 pragma Assert (Present (Typ_Decl));
1968 -- Create the formal parameter which emulates the variable-like behavior
1969 -- of the type's current instance.
1971 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1973 -- Perform minor decoration in case the declaration is not analyzed
1975 Set_Ekind (Obj_Id, E_In_Parameter);
1976 Set_Etype (Obj_Id, Work_Typ);
1977 Set_Scope (Obj_Id, Proc_Id);
1979 Set_First_Entity (Proc_Id, Obj_Id);
1981 -- Generate:
1982 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1984 Proc_Decl :=
1985 Make_Subprogram_Declaration (Loc,
1986 Specification =>
1987 Make_Procedure_Specification (Loc,
1988 Defining_Unit_Name => Proc_Id,
1989 Parameter_Specifications => New_List (
1990 Make_Parameter_Specification (Loc,
1991 Defining_Identifier => Obj_Id,
1992 Parameter_Type =>
1993 New_Occurrence_Of (Work_Typ, Loc)))));
1995 -- The declaration should not be inserted into the tree when the context
1996 -- is ASIS or a generic unit because it is not part of the template.
1998 if ASIS_Mode or Inside_A_Generic then
1999 null;
2001 -- Semi-insert the declaration into the tree for GNATprove by setting
2002 -- its Parent field. This allows for proper upstream tree traversals.
2004 elsif GNATprove_Mode then
2005 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2007 -- Otherwise insert the declaration
2009 else
2010 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2011 end if;
2013 <<Leave>>
2014 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2015 end Build_DIC_Procedure_Declaration;
2017 ------------------------------------
2018 -- Build_Invariant_Procedure_Body --
2019 ------------------------------------
2021 -- WARNING: This routine manages Ghost regions. Return statements must be
2022 -- replaced by gotos which jump to the end of the routine and restore the
2023 -- Ghost mode.
2025 procedure Build_Invariant_Procedure_Body
2026 (Typ : Entity_Id;
2027 Partial_Invariant : Boolean := False)
2029 Loc : constant Source_Ptr := Sloc (Typ);
2031 Pragmas_Seen : Elist_Id := No_Elist;
2032 -- This list contains all invariant pragmas processed so far. The list
2033 -- is used to avoid generating redundant invariant checks.
2035 Produced_Check : Boolean := False;
2036 -- This flag tracks whether the type has produced at least one invariant
2037 -- check. The flag is used as a sanity check at the end of the routine.
2039 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2040 -- intentionally unnested to avoid deep indentation of code.
2042 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2043 -- they emit checks, loops (for arrays) and case statements (for record
2044 -- variant parts) only when there are invariants to verify. This keeps
2045 -- the body of the invariant procedure free of useless code.
2047 procedure Add_Array_Component_Invariants
2048 (T : Entity_Id;
2049 Obj_Id : Entity_Id;
2050 Checks : in out List_Id);
2051 -- Generate an invariant check for each component of array type T.
2052 -- Obj_Id denotes the entity of the _object formal parameter of the
2053 -- invariant procedure. All created checks are added to list Checks.
2055 procedure Add_Inherited_Invariants
2056 (T : Entity_Id;
2057 Priv_Typ : Entity_Id;
2058 Full_Typ : Entity_Id;
2059 Obj_Id : Entity_Id;
2060 Checks : in out List_Id);
2061 -- Generate an invariant check for each inherited class-wide invariant
2062 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2063 -- the partial and full view of the parent type. Obj_Id denotes the
2064 -- entity of the _object formal parameter of the invariant procedure.
2065 -- All created checks are added to list Checks.
2067 procedure Add_Interface_Invariants
2068 (T : Entity_Id;
2069 Obj_Id : Entity_Id;
2070 Checks : in out List_Id);
2071 -- Generate an invariant check for each inherited class-wide invariant
2072 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2073 -- entity of the _object formal parameter of the invariant procedure.
2074 -- All created checks are added to list Checks.
2076 procedure Add_Invariant_Check
2077 (Prag : Node_Id;
2078 Expr : Node_Id;
2079 Checks : in out List_Id;
2080 Inherited : Boolean := False);
2081 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2082 -- verify assertion expression Expr of pragma Prag. All generated code
2083 -- is added to list Checks. Flag Inherited should be set when the pragma
2084 -- is inherited from a parent or interface type.
2086 procedure Add_Own_Invariants
2087 (T : Entity_Id;
2088 Obj_Id : Entity_Id;
2089 Checks : in out List_Id;
2090 Priv_Item : Node_Id := Empty);
2091 -- Generate an invariant check for each invariant found for type T.
2092 -- Obj_Id denotes the entity of the _object formal parameter of the
2093 -- invariant procedure. All created checks are added to list Checks.
2094 -- Priv_Item denotes the first rep item of the private type.
2096 procedure Add_Parent_Invariants
2097 (T : Entity_Id;
2098 Obj_Id : Entity_Id;
2099 Checks : in out List_Id);
2100 -- Generate an invariant check for each inherited class-wide invariant
2101 -- coming from all parent types of type T. Obj_Id denotes the entity of
2102 -- the _object formal parameter of the invariant procedure. All created
2103 -- checks are added to list Checks.
2105 procedure Add_Record_Component_Invariants
2106 (T : Entity_Id;
2107 Obj_Id : Entity_Id;
2108 Checks : in out List_Id);
2109 -- Generate an invariant check for each component of record type T.
2110 -- Obj_Id denotes the entity of the _object formal parameter of the
2111 -- invariant procedure. All created checks are added to list Checks.
2113 ------------------------------------
2114 -- Add_Array_Component_Invariants --
2115 ------------------------------------
2117 procedure Add_Array_Component_Invariants
2118 (T : Entity_Id;
2119 Obj_Id : Entity_Id;
2120 Checks : in out List_Id)
2122 Comp_Typ : constant Entity_Id := Component_Type (T);
2123 Dims : constant Pos := Number_Dimensions (T);
2125 procedure Process_Array_Component
2126 (Indices : List_Id;
2127 Comp_Checks : in out List_Id);
2128 -- Generate an invariant check for an array component identified by
2129 -- the indices in list Indices. All created checks are added to list
2130 -- Comp_Checks.
2132 procedure Process_One_Dimension
2133 (Dim : Pos;
2134 Indices : List_Id;
2135 Dim_Checks : in out List_Id);
2136 -- Generate a loop over the Nth dimension Dim of an array type. List
2137 -- Indices contains all array indices for the dimension. All created
2138 -- checks are added to list Dim_Checks.
2140 -----------------------------
2141 -- Process_Array_Component --
2142 -----------------------------
2144 procedure Process_Array_Component
2145 (Indices : List_Id;
2146 Comp_Checks : in out List_Id)
2148 Proc_Id : Entity_Id;
2150 begin
2151 if Has_Invariants (Comp_Typ) then
2153 -- In GNATprove mode, the component invariants are checked by
2154 -- other means. They should not be added to the array type
2155 -- invariant procedure, so that the procedure can be used to
2156 -- check the array type invariants if any.
2158 if GNATprove_Mode then
2159 null;
2161 else
2162 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2164 -- The component type should have an invariant procedure
2165 -- if it has invariants of its own or inherits class-wide
2166 -- invariants from parent or interface types.
2168 pragma Assert (Present (Proc_Id));
2170 -- Generate:
2171 -- <Comp_Typ>Invariant (_object (<Indices>));
2173 -- Note that the invariant procedure may have a null body if
2174 -- assertions are disabled or Assertion_Policy Ignore is in
2175 -- effect.
2177 if not Has_Null_Body (Proc_Id) then
2178 Append_New_To (Comp_Checks,
2179 Make_Procedure_Call_Statement (Loc,
2180 Name =>
2181 New_Occurrence_Of (Proc_Id, Loc),
2182 Parameter_Associations => New_List (
2183 Make_Indexed_Component (Loc,
2184 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2185 Expressions => New_Copy_List (Indices)))));
2186 end if;
2187 end if;
2189 Produced_Check := True;
2190 end if;
2191 end Process_Array_Component;
2193 ---------------------------
2194 -- Process_One_Dimension --
2195 ---------------------------
2197 procedure Process_One_Dimension
2198 (Dim : Pos;
2199 Indices : List_Id;
2200 Dim_Checks : in out List_Id)
2202 Comp_Checks : List_Id := No_List;
2203 Index : Entity_Id;
2205 begin
2206 -- Generate the invariant checks for the array component after all
2207 -- dimensions have produced their respective loops.
2209 if Dim > Dims then
2210 Process_Array_Component
2211 (Indices => Indices,
2212 Comp_Checks => Dim_Checks);
2214 -- Otherwise create a loop for the current dimension
2216 else
2217 -- Create a new loop variable for each dimension
2219 Index :=
2220 Make_Defining_Identifier (Loc,
2221 Chars => New_External_Name ('I', Dim));
2222 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2224 Process_One_Dimension
2225 (Dim => Dim + 1,
2226 Indices => Indices,
2227 Dim_Checks => Comp_Checks);
2229 -- Generate:
2230 -- for I<Dim> in _object'Range (<Dim>) loop
2231 -- <Comp_Checks>
2232 -- end loop;
2234 -- Note that the invariant procedure may have a null body if
2235 -- assertions are disabled or Assertion_Policy Ignore is in
2236 -- effect.
2238 if Present (Comp_Checks) then
2239 Append_New_To (Dim_Checks,
2240 Make_Implicit_Loop_Statement (T,
2241 Identifier => Empty,
2242 Iteration_Scheme =>
2243 Make_Iteration_Scheme (Loc,
2244 Loop_Parameter_Specification =>
2245 Make_Loop_Parameter_Specification (Loc,
2246 Defining_Identifier => Index,
2247 Discrete_Subtype_Definition =>
2248 Make_Attribute_Reference (Loc,
2249 Prefix =>
2250 New_Occurrence_Of (Obj_Id, Loc),
2251 Attribute_Name => Name_Range,
2252 Expressions => New_List (
2253 Make_Integer_Literal (Loc, Dim))))),
2254 Statements => Comp_Checks));
2255 end if;
2256 end if;
2257 end Process_One_Dimension;
2259 -- Start of processing for Add_Array_Component_Invariants
2261 begin
2262 Process_One_Dimension
2263 (Dim => 1,
2264 Indices => New_List,
2265 Dim_Checks => Checks);
2266 end Add_Array_Component_Invariants;
2268 ------------------------------
2269 -- Add_Inherited_Invariants --
2270 ------------------------------
2272 procedure Add_Inherited_Invariants
2273 (T : Entity_Id;
2274 Priv_Typ : Entity_Id;
2275 Full_Typ : Entity_Id;
2276 Obj_Id : Entity_Id;
2277 Checks : in out List_Id)
2279 Deriv_Typ : Entity_Id;
2280 Expr : Node_Id;
2281 Prag : Node_Id;
2282 Prag_Expr : Node_Id;
2283 Prag_Expr_Arg : Node_Id;
2284 Prag_Typ : Node_Id;
2285 Prag_Typ_Arg : Node_Id;
2287 Par_Proc : Entity_Id;
2288 -- The "partial" invariant procedure of Par_Typ
2290 Par_Typ : Entity_Id;
2291 -- The suitable view of the parent type used in the substitution of
2292 -- type attributes.
2294 begin
2295 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2296 return;
2297 end if;
2299 -- When the type inheriting the class-wide invariant is a concurrent
2300 -- type, use the corresponding record type because it contains all
2301 -- primitive operations of the concurrent type and allows for proper
2302 -- substitution.
2304 if Is_Concurrent_Type (T) then
2305 Deriv_Typ := Corresponding_Record_Type (T);
2306 else
2307 Deriv_Typ := T;
2308 end if;
2310 pragma Assert (Present (Deriv_Typ));
2312 -- Determine which rep item chain to use. Precedence is given to that
2313 -- of the parent type's partial view since it usually carries all the
2314 -- class-wide invariants.
2316 if Present (Priv_Typ) then
2317 Prag := First_Rep_Item (Priv_Typ);
2318 else
2319 Prag := First_Rep_Item (Full_Typ);
2320 end if;
2322 while Present (Prag) loop
2323 if Nkind (Prag) = N_Pragma
2324 and then Pragma_Name (Prag) = Name_Invariant
2325 then
2326 -- Nothing to do if the pragma was already processed
2328 if Contains (Pragmas_Seen, Prag) then
2329 return;
2331 -- Nothing to do when the caller requests the processing of all
2332 -- inherited class-wide invariants, but the pragma does not
2333 -- fall in this category.
2335 elsif not Class_Present (Prag) then
2336 return;
2337 end if;
2339 -- Extract the arguments of the invariant pragma
2341 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2342 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2343 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2344 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2346 -- The pragma applies to the partial view of the parent type
2348 if Present (Priv_Typ)
2349 and then Entity (Prag_Typ) = Priv_Typ
2350 then
2351 Par_Typ := Priv_Typ;
2353 -- The pragma applies to the full view of the parent type
2355 elsif Present (Full_Typ)
2356 and then Entity (Prag_Typ) = Full_Typ
2357 then
2358 Par_Typ := Full_Typ;
2360 -- Otherwise the pragma does not belong to the parent type and
2361 -- should not be considered.
2363 else
2364 return;
2365 end if;
2367 -- Perform the following substitutions:
2369 -- * Replace a reference to the _object parameter of the
2370 -- parent type's partial invariant procedure with a
2371 -- reference to the _object parameter of the derived
2372 -- type's full invariant procedure.
2374 -- * Replace a reference to a discriminant of the parent type
2375 -- with a suitable value from the point of view of the
2376 -- derived type.
2378 -- * Replace a call to an overridden parent primitive with a
2379 -- call to the overriding derived type primitive.
2381 -- * Replace a call to an inherited parent primitive with a
2382 -- call to the internally-generated inherited derived type
2383 -- primitive.
2385 Expr := New_Copy_Tree (Prag_Expr);
2387 -- The parent type must have a "partial" invariant procedure
2388 -- because class-wide invariants are captured exclusively by
2389 -- it.
2391 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2392 pragma Assert (Present (Par_Proc));
2394 Replace_References
2395 (Expr => Expr,
2396 Par_Typ => Par_Typ,
2397 Deriv_Typ => Deriv_Typ,
2398 Par_Obj => First_Formal (Par_Proc),
2399 Deriv_Obj => Obj_Id);
2401 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2402 end if;
2404 Next_Rep_Item (Prag);
2405 end loop;
2406 end Add_Inherited_Invariants;
2408 ------------------------------
2409 -- Add_Interface_Invariants --
2410 ------------------------------
2412 procedure Add_Interface_Invariants
2413 (T : Entity_Id;
2414 Obj_Id : Entity_Id;
2415 Checks : in out List_Id)
2417 Iface_Elmt : Elmt_Id;
2418 Ifaces : Elist_Id;
2420 begin
2421 -- Generate an invariant check for each class-wide invariant coming
2422 -- from all interfaces implemented by type T.
2424 if Is_Tagged_Type (T) then
2425 Collect_Interfaces (T, Ifaces);
2427 -- Process the class-wide invariants of all implemented interfaces
2429 Iface_Elmt := First_Elmt (Ifaces);
2430 while Present (Iface_Elmt) loop
2432 -- The Full_Typ parameter is intentionally left Empty because
2433 -- interfaces are treated as the partial view of a private type
2434 -- in order to achieve uniformity with the general case.
2436 Add_Inherited_Invariants
2437 (T => T,
2438 Priv_Typ => Node (Iface_Elmt),
2439 Full_Typ => Empty,
2440 Obj_Id => Obj_Id,
2441 Checks => Checks);
2443 Next_Elmt (Iface_Elmt);
2444 end loop;
2445 end if;
2446 end Add_Interface_Invariants;
2448 -------------------------
2449 -- Add_Invariant_Check --
2450 -------------------------
2452 procedure Add_Invariant_Check
2453 (Prag : Node_Id;
2454 Expr : Node_Id;
2455 Checks : in out List_Id;
2456 Inherited : Boolean := False)
2458 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2459 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2460 Ploc : constant Source_Ptr := Sloc (Prag);
2461 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2463 Assoc : List_Id;
2464 Str : String_Id;
2466 begin
2467 -- The invariant is ignored, nothing left to do
2469 if Is_Ignored (Prag) then
2470 null;
2472 -- Otherwise the invariant is checked. Build a pragma Check to verify
2473 -- the expression at run time.
2475 else
2476 Assoc := New_List (
2477 Make_Pragma_Argument_Association (Ploc,
2478 Expression => Make_Identifier (Ploc, Nam)),
2479 Make_Pragma_Argument_Association (Ploc,
2480 Expression => Expr));
2482 -- Handle the String argument (if any)
2484 if Present (Str_Arg) then
2485 Str := Strval (Get_Pragma_Arg (Str_Arg));
2487 -- When inheriting an invariant, modify the message from
2488 -- "failed invariant" to "failed inherited invariant".
2490 if Inherited then
2491 String_To_Name_Buffer (Str);
2493 if Name_Buffer (1 .. 16) = "failed invariant" then
2494 Insert_Str_In_Name_Buffer ("inherited ", 8);
2495 Str := String_From_Name_Buffer;
2496 end if;
2497 end if;
2499 Append_To (Assoc,
2500 Make_Pragma_Argument_Association (Ploc,
2501 Expression => Make_String_Literal (Ploc, Str)));
2502 end if;
2504 -- Generate:
2505 -- pragma Check (<Nam>, <Expr>, <Str>);
2507 Append_New_To (Checks,
2508 Make_Pragma (Ploc,
2509 Chars => Name_Check,
2510 Pragma_Argument_Associations => Assoc));
2511 end if;
2513 -- Output an info message when inheriting an invariant and the
2514 -- listing option is enabled.
2516 if Inherited and Opt.List_Inherited_Aspects then
2517 Error_Msg_Sloc := Sloc (Prag);
2518 Error_Msg_N
2519 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2520 end if;
2522 -- Add the pragma to the list of processed pragmas
2524 Append_New_Elmt (Prag, Pragmas_Seen);
2525 Produced_Check := True;
2526 end Add_Invariant_Check;
2528 ---------------------------
2529 -- Add_Parent_Invariants --
2530 ---------------------------
2532 procedure Add_Parent_Invariants
2533 (T : Entity_Id;
2534 Obj_Id : Entity_Id;
2535 Checks : in out List_Id)
2537 Dummy_1 : Entity_Id;
2538 Dummy_2 : Entity_Id;
2540 Curr_Typ : Entity_Id;
2541 -- The entity of the current type being examined
2543 Full_Typ : Entity_Id;
2544 -- The full view of Par_Typ
2546 Par_Typ : Entity_Id;
2547 -- The entity of the parent type
2549 Priv_Typ : Entity_Id;
2550 -- The partial view of Par_Typ
2552 begin
2553 -- Do not process array types because they cannot have true parent
2554 -- types. This also prevents the generation of a duplicate invariant
2555 -- check when the input type is an array base type because its Etype
2556 -- denotes the first subtype, both of which share the same component
2557 -- type.
2559 if Is_Array_Type (T) then
2560 return;
2561 end if;
2563 -- Climb the parent type chain
2565 Curr_Typ := T;
2566 loop
2567 -- Do not consider subtypes as they inherit the invariants
2568 -- from their base types.
2570 Par_Typ := Base_Type (Etype (Curr_Typ));
2572 -- Stop the climb once the root of the parent chain is
2573 -- reached.
2575 exit when Curr_Typ = Par_Typ;
2577 -- Process the class-wide invariants of the parent type
2579 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2581 -- Process the elements of an array type
2583 if Is_Array_Type (Full_Typ) then
2584 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2586 -- Process the components of a record type
2588 elsif Ekind (Full_Typ) = E_Record_Type then
2589 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2590 end if;
2592 Add_Inherited_Invariants
2593 (T => T,
2594 Priv_Typ => Priv_Typ,
2595 Full_Typ => Full_Typ,
2596 Obj_Id => Obj_Id,
2597 Checks => Checks);
2599 Curr_Typ := Par_Typ;
2600 end loop;
2601 end Add_Parent_Invariants;
2603 ------------------------
2604 -- Add_Own_Invariants --
2605 ------------------------
2607 procedure Add_Own_Invariants
2608 (T : Entity_Id;
2609 Obj_Id : Entity_Id;
2610 Checks : in out List_Id;
2611 Priv_Item : Node_Id := Empty)
2613 ASIS_Expr : Node_Id;
2614 Expr : Node_Id;
2615 Prag : Node_Id;
2616 Prag_Asp : Node_Id;
2617 Prag_Expr : Node_Id;
2618 Prag_Expr_Arg : Node_Id;
2619 Prag_Typ : Node_Id;
2620 Prag_Typ_Arg : Node_Id;
2622 begin
2623 if not Present (T) then
2624 return;
2625 end if;
2627 Prag := First_Rep_Item (T);
2628 while Present (Prag) loop
2629 if Nkind (Prag) = N_Pragma
2630 and then Pragma_Name (Prag) = Name_Invariant
2631 then
2632 -- Stop the traversal of the rep item chain once a specific
2633 -- item is encountered.
2635 if Present (Priv_Item) and then Prag = Priv_Item then
2636 exit;
2637 end if;
2639 -- Nothing to do if the pragma was already processed
2641 if Contains (Pragmas_Seen, Prag) then
2642 return;
2643 end if;
2645 -- Extract the arguments of the invariant pragma
2647 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2648 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2649 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2650 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2651 Prag_Asp := Corresponding_Aspect (Prag);
2653 -- Verify the pragma belongs to T, otherwise the pragma applies
2654 -- to a parent type in which case it will be processed later by
2655 -- Add_Parent_Invariants or Add_Interface_Invariants.
2657 if Entity (Prag_Typ) /= T then
2658 return;
2659 end if;
2661 Expr := New_Copy_Tree (Prag_Expr);
2663 -- Substitute all references to type T with references to the
2664 -- _object formal parameter.
2666 Replace_Type_References (Expr, T, Obj_Id);
2668 -- Preanalyze the invariant expression to detect errors and at
2669 -- the same time capture the visibility of the proper package
2670 -- part.
2672 Set_Parent (Expr, Parent (Prag_Expr));
2673 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2675 -- Save a copy of the expression when T is tagged to detect
2676 -- errors and capture the visibility of the proper package part
2677 -- for the generation of inherited type invariants.
2679 if Is_Tagged_Type (T) then
2680 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2681 end if;
2683 -- If the pragma comes from an aspect specification, replace
2684 -- the saved expression because all type references must be
2685 -- substituted for the call to Preanalyze_Spec_Expression in
2686 -- Check_Aspect_At_xxx routines.
2688 if Present (Prag_Asp) then
2689 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2690 end if;
2692 -- Analyze the original invariant expression for ASIS
2694 if ASIS_Mode then
2695 ASIS_Expr := Empty;
2697 if Comes_From_Source (Prag) then
2698 ASIS_Expr := Prag_Expr;
2699 elsif Present (Prag_Asp) then
2700 ASIS_Expr := Expression (Prag_Asp);
2701 end if;
2703 if Present (ASIS_Expr) then
2704 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2705 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2706 end if;
2707 end if;
2709 Add_Invariant_Check (Prag, Expr, Checks);
2710 end if;
2712 Next_Rep_Item (Prag);
2713 end loop;
2714 end Add_Own_Invariants;
2716 -------------------------------------
2717 -- Add_Record_Component_Invariants --
2718 -------------------------------------
2720 procedure Add_Record_Component_Invariants
2721 (T : Entity_Id;
2722 Obj_Id : Entity_Id;
2723 Checks : in out List_Id)
2725 procedure Process_Component_List
2726 (Comp_List : Node_Id;
2727 CL_Checks : in out List_Id);
2728 -- Generate invariant checks for all record components found in
2729 -- component list Comp_List, including variant parts. All created
2730 -- checks are added to list CL_Checks.
2732 procedure Process_Record_Component
2733 (Comp_Id : Entity_Id;
2734 Comp_Checks : in out List_Id);
2735 -- Generate an invariant check for a record component identified by
2736 -- Comp_Id. All created checks are added to list Comp_Checks.
2738 ----------------------------
2739 -- Process_Component_List --
2740 ----------------------------
2742 procedure Process_Component_List
2743 (Comp_List : Node_Id;
2744 CL_Checks : in out List_Id)
2746 Comp : Node_Id;
2747 Var : Node_Id;
2748 Var_Alts : List_Id := No_List;
2749 Var_Checks : List_Id := No_List;
2750 Var_Stmts : List_Id;
2752 Produced_Variant_Check : Boolean := False;
2753 -- This flag tracks whether the component has produced at least
2754 -- one invariant check.
2756 begin
2757 -- Traverse the component items
2759 Comp := First (Component_Items (Comp_List));
2760 while Present (Comp) loop
2761 if Nkind (Comp) = N_Component_Declaration then
2763 -- Generate the component invariant check
2765 Process_Record_Component
2766 (Comp_Id => Defining_Entity (Comp),
2767 Comp_Checks => CL_Checks);
2768 end if;
2770 Next (Comp);
2771 end loop;
2773 -- Traverse the variant part
2775 if Present (Variant_Part (Comp_List)) then
2776 Var := First (Variants (Variant_Part (Comp_List)));
2777 while Present (Var) loop
2778 Var_Checks := No_List;
2780 -- Generate invariant checks for all components and variant
2781 -- parts that qualify.
2783 Process_Component_List
2784 (Comp_List => Component_List (Var),
2785 CL_Checks => Var_Checks);
2787 -- The components of the current variant produced at least
2788 -- one invariant check.
2790 if Present (Var_Checks) then
2791 Var_Stmts := Var_Checks;
2792 Produced_Variant_Check := True;
2794 -- Otherwise there are either no components with invariants,
2795 -- assertions are disabled, or Assertion_Policy Ignore is in
2796 -- effect.
2798 else
2799 Var_Stmts := New_List (Make_Null_Statement (Loc));
2800 end if;
2802 Append_New_To (Var_Alts,
2803 Make_Case_Statement_Alternative (Loc,
2804 Discrete_Choices =>
2805 New_Copy_List (Discrete_Choices (Var)),
2806 Statements => Var_Stmts));
2808 Next (Var);
2809 end loop;
2811 -- Create a case statement which verifies the invariant checks
2812 -- of a particular component list depending on the discriminant
2813 -- values only when there is at least one real invariant check.
2815 if Produced_Variant_Check then
2816 Append_New_To (CL_Checks,
2817 Make_Case_Statement (Loc,
2818 Expression =>
2819 Make_Selected_Component (Loc,
2820 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2821 Selector_Name =>
2822 New_Occurrence_Of
2823 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2824 Alternatives => Var_Alts));
2825 end if;
2826 end if;
2827 end Process_Component_List;
2829 ------------------------------
2830 -- Process_Record_Component --
2831 ------------------------------
2833 procedure Process_Record_Component
2834 (Comp_Id : Entity_Id;
2835 Comp_Checks : in out List_Id)
2837 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2838 Proc_Id : Entity_Id;
2840 Produced_Component_Check : Boolean := False;
2841 -- This flag tracks whether the component has produced at least
2842 -- one invariant check.
2844 begin
2845 -- Nothing to do for internal component _parent. Note that it is
2846 -- not desirable to check whether the component comes from source
2847 -- because protected type components are relocated to an internal
2848 -- corresponding record, but still need processing.
2850 if Chars (Comp_Id) = Name_uParent then
2851 return;
2852 end if;
2854 -- Verify the invariant of the component. Note that an access
2855 -- type may have an invariant when it acts as the full view of a
2856 -- private type and the invariant appears on the partial view. In
2857 -- this case verify the access value itself.
2859 if Has_Invariants (Comp_Typ) then
2861 -- In GNATprove mode, the component invariants are checked by
2862 -- other means. They should not be added to the record type
2863 -- invariant procedure, so that the procedure can be used to
2864 -- check the record type invariants if any.
2866 if GNATprove_Mode then
2867 null;
2869 else
2870 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2872 -- The component type should have an invariant procedure
2873 -- if it has invariants of its own or inherits class-wide
2874 -- invariants from parent or interface types.
2876 pragma Assert (Present (Proc_Id));
2878 -- Generate:
2879 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2881 -- Note that the invariant procedure may have a null body if
2882 -- assertions are disabled or Assertion_Policy Ignore is in
2883 -- effect.
2885 if not Has_Null_Body (Proc_Id) then
2886 Append_New_To (Comp_Checks,
2887 Make_Procedure_Call_Statement (Loc,
2888 Name =>
2889 New_Occurrence_Of (Proc_Id, Loc),
2890 Parameter_Associations => New_List (
2891 Make_Selected_Component (Loc,
2892 Prefix =>
2893 Unchecked_Convert_To
2894 (T, New_Occurrence_Of (Obj_Id, Loc)),
2895 Selector_Name =>
2896 New_Occurrence_Of (Comp_Id, Loc)))));
2897 end if;
2898 end if;
2900 Produced_Check := True;
2901 Produced_Component_Check := True;
2902 end if;
2904 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2905 Error_Msg_NE
2906 ("invariants cannot be checked on components of "
2907 & "unchecked_union type &?", Comp_Id, T);
2908 end if;
2909 end Process_Record_Component;
2911 -- Local variables
2913 Comps : Node_Id;
2914 Def : Node_Id;
2916 -- Start of processing for Add_Record_Component_Invariants
2918 begin
2919 -- An untagged derived type inherits the components of its parent
2920 -- type. In order to avoid creating redundant invariant checks, do
2921 -- not process the components now. Instead wait until the ultimate
2922 -- parent of the untagged derivation chain is reached.
2924 if not Is_Untagged_Derivation (T) then
2925 Def := Type_Definition (Parent (T));
2927 if Nkind (Def) = N_Derived_Type_Definition then
2928 Def := Record_Extension_Part (Def);
2929 end if;
2931 pragma Assert (Nkind (Def) = N_Record_Definition);
2932 Comps := Component_List (Def);
2934 if Present (Comps) then
2935 Process_Component_List
2936 (Comp_List => Comps,
2937 CL_Checks => Checks);
2938 end if;
2939 end if;
2940 end Add_Record_Component_Invariants;
2942 -- Local variables
2944 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2945 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2946 -- Save the Ghost-related attributes to restore on exit
2948 Dummy : Entity_Id;
2949 Priv_Item : Node_Id;
2950 Proc_Body : Node_Id;
2951 Proc_Body_Id : Entity_Id;
2952 Proc_Decl : Node_Id;
2953 Proc_Id : Entity_Id;
2954 Stmts : List_Id := No_List;
2956 CRec_Typ : Entity_Id := Empty;
2957 -- The corresponding record type of Full_Typ
2959 Full_Proc : Entity_Id := Empty;
2960 -- The entity of the "full" invariant procedure
2962 Full_Typ : Entity_Id := Empty;
2963 -- The full view of the working type
2965 Obj_Id : Entity_Id := Empty;
2966 -- The _object formal parameter of the invariant procedure
2968 Part_Proc : Entity_Id := Empty;
2969 -- The entity of the "partial" invariant procedure
2971 Priv_Typ : Entity_Id := Empty;
2972 -- The partial view of the working type
2974 Work_Typ : Entity_Id := Empty;
2975 -- The working type
2977 -- Start of processing for Build_Invariant_Procedure_Body
2979 begin
2980 Work_Typ := Typ;
2982 -- The input type denotes the implementation base type of a constrained
2983 -- array type. Work with the first subtype as all invariant pragmas are
2984 -- on its rep item chain.
2986 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2987 Work_Typ := First_Subtype (Work_Typ);
2989 -- The input type denotes the corresponding record type of a protected
2990 -- or task type. Work with the concurrent type because the corresponding
2991 -- record type may not be visible to clients of the type.
2993 elsif Ekind (Work_Typ) = E_Record_Type
2994 and then Is_Concurrent_Record_Type (Work_Typ)
2995 then
2996 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2997 end if;
2999 -- The working type may be subject to pragma Ghost. Set the mode now to
3000 -- ensure that the invariant procedure is properly marked as Ghost.
3002 Set_Ghost_Mode (Work_Typ);
3004 -- The type must either have invariants of its own, inherit class-wide
3005 -- invariants from parent types or interfaces, or be an array or record
3006 -- type whose components have invariants.
3008 pragma Assert (Has_Invariants (Work_Typ));
3010 -- Interfaces are treated as the partial view of a private type in order
3011 -- to achieve uniformity with the general case.
3013 if Is_Interface (Work_Typ) then
3014 Priv_Typ := Work_Typ;
3016 -- Otherwise obtain both views of the type
3018 else
3019 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3020 end if;
3022 -- The caller requests a body for the partial invariant procedure
3024 if Partial_Invariant then
3025 Full_Proc := Invariant_Procedure (Work_Typ);
3026 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3028 -- The "full" invariant procedure body was already created
3030 if Present (Full_Proc)
3031 and then Present
3032 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3033 then
3034 -- This scenario happens only when the type is an untagged
3035 -- derivation from a private parent and the underlying full
3036 -- view was processed before the partial view.
3038 pragma Assert
3039 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3041 -- Nothing to do because the processing of the underlying full
3042 -- view already checked the invariants of the partial view.
3044 goto Leave;
3045 end if;
3047 -- Create a declaration for the "partial" invariant procedure if it
3048 -- is not available.
3050 if No (Proc_Id) then
3051 Build_Invariant_Procedure_Declaration
3052 (Typ => Work_Typ,
3053 Partial_Invariant => True);
3055 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3056 end if;
3058 -- The caller requests a body for the "full" invariant procedure
3060 else
3061 Proc_Id := Invariant_Procedure (Work_Typ);
3062 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3064 -- Create a declaration for the "full" invariant procedure if it is
3065 -- not available.
3067 if No (Proc_Id) then
3068 Build_Invariant_Procedure_Declaration (Work_Typ);
3069 Proc_Id := Invariant_Procedure (Work_Typ);
3070 end if;
3071 end if;
3073 -- At this point there should be an invariant procedure declaration
3075 pragma Assert (Present (Proc_Id));
3076 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3078 -- Nothing to do if the invariant procedure already has a body
3080 if Present (Corresponding_Body (Proc_Decl)) then
3081 goto Leave;
3082 end if;
3084 -- Emulate the environment of the invariant procedure by installing its
3085 -- scope and formal parameters. Note that this is not needed, but having
3086 -- the scope installed helps with the detection of invariant-related
3087 -- errors.
3089 Push_Scope (Proc_Id);
3090 Install_Formals (Proc_Id);
3092 Obj_Id := First_Formal (Proc_Id);
3093 pragma Assert (Present (Obj_Id));
3095 -- The "partial" invariant procedure verifies the invariants of the
3096 -- partial view only.
3098 if Partial_Invariant then
3099 pragma Assert (Present (Priv_Typ));
3101 Add_Own_Invariants
3102 (T => Priv_Typ,
3103 Obj_Id => Obj_Id,
3104 Checks => Stmts);
3106 -- Otherwise the "full" invariant procedure verifies the invariants of
3107 -- the full view, all array or record components, as well as class-wide
3108 -- invariants inherited from parent types or interfaces. In addition, it
3109 -- indirectly verifies the invariants of the partial view by calling the
3110 -- "partial" invariant procedure.
3112 else
3113 pragma Assert (Present (Full_Typ));
3115 -- Check the invariants of the partial view by calling the "partial"
3116 -- invariant procedure. Generate:
3118 -- <Work_Typ>Partial_Invariant (_object);
3120 if Present (Part_Proc) then
3121 Append_New_To (Stmts,
3122 Make_Procedure_Call_Statement (Loc,
3123 Name => New_Occurrence_Of (Part_Proc, Loc),
3124 Parameter_Associations => New_List (
3125 New_Occurrence_Of (Obj_Id, Loc))));
3127 Produced_Check := True;
3128 end if;
3130 Priv_Item := Empty;
3132 -- Derived subtypes do not have a partial view
3134 if Present (Priv_Typ) then
3136 -- The processing of the "full" invariant procedure intentionally
3137 -- skips the partial view because a) this may result in changes of
3138 -- visibility and b) lead to duplicate checks. However, when the
3139 -- full view is the underlying full view of an untagged derived
3140 -- type whose parent type is private, partial invariants appear on
3141 -- the rep item chain of the partial view only.
3143 -- package Pack_1 is
3144 -- type Root ... is private;
3145 -- private
3146 -- <full view of Root>
3147 -- end Pack_1;
3149 -- with Pack_1;
3150 -- package Pack_2 is
3151 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3152 -- <underlying full view of Child>
3153 -- end Pack_2;
3155 -- As a result, the processing of the full view must also consider
3156 -- all invariants of the partial view.
3158 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3159 null;
3161 -- Otherwise the invariants of the partial view are ignored
3163 else
3164 -- Note that the rep item chain is shared between the partial
3165 -- and full views of a type. To avoid processing the invariants
3166 -- of the partial view, signal the logic to stop when the first
3167 -- rep item of the partial view has been reached.
3169 Priv_Item := First_Rep_Item (Priv_Typ);
3171 -- Ignore the invariants of the partial view by eliminating the
3172 -- view.
3174 Priv_Typ := Empty;
3175 end if;
3176 end if;
3178 -- Process the invariants of the full view and in certain cases those
3179 -- of the partial view. This also handles any invariants on array or
3180 -- record components.
3182 Add_Own_Invariants
3183 (T => Priv_Typ,
3184 Obj_Id => Obj_Id,
3185 Checks => Stmts,
3186 Priv_Item => Priv_Item);
3188 Add_Own_Invariants
3189 (T => Full_Typ,
3190 Obj_Id => Obj_Id,
3191 Checks => Stmts,
3192 Priv_Item => Priv_Item);
3194 -- Process the elements of an array type
3196 if Is_Array_Type (Full_Typ) then
3197 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3199 -- Process the components of a record type
3201 elsif Ekind (Full_Typ) = E_Record_Type then
3202 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3204 -- Process the components of a corresponding record
3206 elsif Present (CRec_Typ) then
3207 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3208 end if;
3210 -- Process the inherited class-wide invariants of all parent types.
3211 -- This also handles any invariants on record components.
3213 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3215 -- Process the inherited class-wide invariants of all implemented
3216 -- interface types.
3218 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3219 end if;
3221 End_Scope;
3223 -- At this point there should be at least one invariant check. If this
3224 -- is not the case, then the invariant-related flags were not properly
3225 -- set, or there is a missing invariant procedure on one of the array
3226 -- or record components.
3228 pragma Assert (Produced_Check);
3230 -- Account for the case where assertions are disabled or all invariant
3231 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3232 -- empty body.
3234 if No (Stmts) then
3235 Stmts := New_List (Make_Null_Statement (Loc));
3236 end if;
3238 -- Generate:
3239 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3240 -- begin
3241 -- <Stmts>
3242 -- end <Work_Typ>[Partial_]Invariant;
3244 Proc_Body :=
3245 Make_Subprogram_Body (Loc,
3246 Specification =>
3247 Copy_Subprogram_Spec (Parent (Proc_Id)),
3248 Declarations => Empty_List,
3249 Handled_Statement_Sequence =>
3250 Make_Handled_Sequence_Of_Statements (Loc,
3251 Statements => Stmts));
3252 Proc_Body_Id := Defining_Entity (Proc_Body);
3254 -- Perform minor decoration in case the body is not analyzed
3256 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3257 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3258 Set_Scope (Proc_Body_Id, Current_Scope);
3260 -- Link both spec and body to avoid generating duplicates
3262 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3263 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3265 -- The body should not be inserted into the tree when the context is
3266 -- ASIS or a generic unit because it is not part of the template. Note
3267 -- that the body must still be generated in order to resolve the
3268 -- invariants.
3270 if ASIS_Mode or Inside_A_Generic then
3271 null;
3273 -- Semi-insert the body into the tree for GNATprove by setting its
3274 -- Parent field. This allows for proper upstream tree traversals.
3276 elsif GNATprove_Mode then
3277 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3279 -- Otherwise the body is part of the freezing actions of the type
3281 else
3282 Append_Freeze_Action (Work_Typ, Proc_Body);
3283 end if;
3285 <<Leave>>
3286 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3287 end Build_Invariant_Procedure_Body;
3289 -------------------------------------------
3290 -- Build_Invariant_Procedure_Declaration --
3291 -------------------------------------------
3293 -- WARNING: This routine manages Ghost regions. Return statements must be
3294 -- replaced by gotos which jump to the end of the routine and restore the
3295 -- Ghost mode.
3297 procedure Build_Invariant_Procedure_Declaration
3298 (Typ : Entity_Id;
3299 Partial_Invariant : Boolean := False)
3301 Loc : constant Source_Ptr := Sloc (Typ);
3303 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3304 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3305 -- Save the Ghost-related attributes to restore on exit
3307 Proc_Decl : Node_Id;
3308 Proc_Id : Entity_Id;
3309 Proc_Nam : Name_Id;
3310 Typ_Decl : Node_Id;
3312 CRec_Typ : Entity_Id;
3313 -- The corresponding record type of Full_Typ
3315 Full_Base : Entity_Id;
3316 -- The base type of Full_Typ
3318 Full_Typ : Entity_Id;
3319 -- The full view of working type
3321 Obj_Id : Entity_Id;
3322 -- The _object formal parameter of the invariant procedure
3324 Obj_Typ : Entity_Id;
3325 -- The type of the _object formal parameter
3327 Priv_Typ : Entity_Id;
3328 -- The partial view of working type
3330 Work_Typ : Entity_Id;
3331 -- The working type
3333 begin
3334 Work_Typ := Typ;
3336 -- The input type denotes the implementation base type of a constrained
3337 -- array type. Work with the first subtype as all invariant pragmas are
3338 -- on its rep item chain.
3340 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3341 Work_Typ := First_Subtype (Work_Typ);
3343 -- The input denotes the corresponding record type of a protected or a
3344 -- task type. Work with the concurrent type because the corresponding
3345 -- record type may not be visible to clients of the type.
3347 elsif Ekind (Work_Typ) = E_Record_Type
3348 and then Is_Concurrent_Record_Type (Work_Typ)
3349 then
3350 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3351 end if;
3353 -- The working type may be subject to pragma Ghost. Set the mode now to
3354 -- ensure that the invariant procedure is properly marked as Ghost.
3356 Set_Ghost_Mode (Work_Typ);
3358 -- The type must either have invariants of its own, inherit class-wide
3359 -- invariants from parent or interface types, or be an array or record
3360 -- type whose components have invariants.
3362 pragma Assert (Has_Invariants (Work_Typ));
3364 -- Nothing to do if the type already has a "partial" invariant procedure
3366 if Partial_Invariant then
3367 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3368 goto Leave;
3369 end if;
3371 -- Nothing to do if the type already has a "full" invariant procedure
3373 elsif Present (Invariant_Procedure (Work_Typ)) then
3374 goto Leave;
3375 end if;
3377 -- The caller requests the declaration of the "partial" invariant
3378 -- procedure.
3380 if Partial_Invariant then
3381 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3383 -- Otherwise the caller requests the declaration of the "full" invariant
3384 -- procedure.
3386 else
3387 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3388 end if;
3390 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3392 -- Perform minor decoration in case the declaration is not analyzed
3394 Set_Ekind (Proc_Id, E_Procedure);
3395 Set_Etype (Proc_Id, Standard_Void_Type);
3396 Set_Scope (Proc_Id, Current_Scope);
3398 if Partial_Invariant then
3399 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3400 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3401 else
3402 Set_Is_Invariant_Procedure (Proc_Id);
3403 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3404 end if;
3406 -- The invariant procedure requires debug info when the invariants are
3407 -- subject to Source Coverage Obligations.
3409 if Generate_SCO then
3410 Set_Needs_Debug_Info (Proc_Id);
3411 end if;
3413 -- Obtain all views of the input type
3415 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3417 -- Associate the invariant procedure with all views
3419 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3420 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3421 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3422 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3424 -- The declaration of the invariant procedure is inserted after the
3425 -- declaration of the partial view as this allows for proper external
3426 -- visibility.
3428 if Present (Priv_Typ) then
3429 Typ_Decl := Declaration_Node (Priv_Typ);
3431 -- Anonymous arrays in object declarations have no explicit declaration
3432 -- so use the related object declaration as the insertion point.
3434 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3435 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3437 -- Derived types with the full view as parent do not have a partial
3438 -- view. Insert the invariant procedure after the derived type.
3440 else
3441 Typ_Decl := Declaration_Node (Full_Typ);
3442 end if;
3444 -- The type should have a declarative node
3446 pragma Assert (Present (Typ_Decl));
3448 -- Create the formal parameter which emulates the variable-like behavior
3449 -- of the current type instance.
3451 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3453 -- When generating an invariant procedure declaration for an abstract
3454 -- type (including interfaces), use the class-wide type as the _object
3455 -- type. This has several desirable effects:
3457 -- * The invariant procedure does not become a primitive of the type.
3458 -- This eliminates the need to either special case the treatment of
3459 -- invariant procedures, or to make it a predefined primitive and
3460 -- force every derived type to potentially provide an empty body.
3462 -- * The invariant procedure does not need to be declared as abstract.
3463 -- This allows for a proper body, which in turn avoids redundant
3464 -- processing of the same invariants for types with multiple views.
3466 -- * The class-wide type allows for calls to abstract primitives
3467 -- within a nonabstract subprogram. The calls are treated as
3468 -- dispatching and require additional processing when they are
3469 -- remapped to call primitives of derived types. See routine
3470 -- Replace_References for details.
3472 if Is_Abstract_Type (Work_Typ) then
3473 Obj_Typ := Class_Wide_Type (Work_Typ);
3474 else
3475 Obj_Typ := Work_Typ;
3476 end if;
3478 -- Perform minor decoration in case the declaration is not analyzed
3480 Set_Ekind (Obj_Id, E_In_Parameter);
3481 Set_Etype (Obj_Id, Obj_Typ);
3482 Set_Scope (Obj_Id, Proc_Id);
3484 Set_First_Entity (Proc_Id, Obj_Id);
3485 Set_Last_Entity (Proc_Id, Obj_Id);
3487 -- Generate:
3488 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3490 Proc_Decl :=
3491 Make_Subprogram_Declaration (Loc,
3492 Specification =>
3493 Make_Procedure_Specification (Loc,
3494 Defining_Unit_Name => Proc_Id,
3495 Parameter_Specifications => New_List (
3496 Make_Parameter_Specification (Loc,
3497 Defining_Identifier => Obj_Id,
3498 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3500 -- The declaration should not be inserted into the tree when the context
3501 -- is ASIS or a generic unit because it is not part of the template.
3503 if ASIS_Mode or Inside_A_Generic then
3504 null;
3506 -- Semi-insert the declaration into the tree for GNATprove by setting
3507 -- its Parent field. This allows for proper upstream tree traversals.
3509 elsif GNATprove_Mode then
3510 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3512 -- Otherwise insert the declaration
3514 else
3515 pragma Assert (Present (Typ_Decl));
3516 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3517 end if;
3519 <<Leave>>
3520 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3521 end Build_Invariant_Procedure_Declaration;
3523 --------------------------
3524 -- Build_Procedure_Form --
3525 --------------------------
3527 procedure Build_Procedure_Form (N : Node_Id) is
3528 Loc : constant Source_Ptr := Sloc (N);
3529 Subp : constant Entity_Id := Defining_Entity (N);
3531 Func_Formal : Entity_Id;
3532 Proc_Formals : List_Id;
3533 Proc_Decl : Node_Id;
3535 begin
3536 -- No action needed if this transformation was already done, or in case
3537 -- of subprogram renaming declarations.
3539 if Nkind (Specification (N)) = N_Procedure_Specification
3540 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3541 then
3542 return;
3543 end if;
3545 -- Ditto when dealing with an expression function, where both the
3546 -- original expression and the generated declaration end up being
3547 -- expanded here.
3549 if Rewritten_For_C (Subp) then
3550 return;
3551 end if;
3553 Proc_Formals := New_List;
3555 -- Create a list of formal parameters with the same types as the
3556 -- function.
3558 Func_Formal := First_Formal (Subp);
3559 while Present (Func_Formal) loop
3560 Append_To (Proc_Formals,
3561 Make_Parameter_Specification (Loc,
3562 Defining_Identifier =>
3563 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3564 Parameter_Type =>
3565 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3567 Next_Formal (Func_Formal);
3568 end loop;
3570 -- Add an extra out parameter to carry the function result
3572 Name_Len := 6;
3573 Name_Buffer (1 .. Name_Len) := "RESULT";
3574 Append_To (Proc_Formals,
3575 Make_Parameter_Specification (Loc,
3576 Defining_Identifier =>
3577 Make_Defining_Identifier (Loc, Chars => Name_Find),
3578 Out_Present => True,
3579 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3581 -- The new procedure declaration is inserted immediately after the
3582 -- function declaration. The processing in Build_Procedure_Body_Form
3583 -- relies on this order.
3585 Proc_Decl :=
3586 Make_Subprogram_Declaration (Loc,
3587 Specification =>
3588 Make_Procedure_Specification (Loc,
3589 Defining_Unit_Name =>
3590 Make_Defining_Identifier (Loc, Chars (Subp)),
3591 Parameter_Specifications => Proc_Formals));
3593 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3595 -- Entity of procedure must remain invisible so that it does not
3596 -- overload subsequent references to the original function.
3598 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3600 -- Mark the function as having a procedure form and link the function
3601 -- and its internally built procedure.
3603 Set_Rewritten_For_C (Subp);
3604 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3605 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3606 end Build_Procedure_Form;
3608 ------------------------
3609 -- Build_Runtime_Call --
3610 ------------------------
3612 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3613 begin
3614 -- If entity is not available, we can skip making the call (this avoids
3615 -- junk duplicated error messages in a number of cases).
3617 if not RTE_Available (RE) then
3618 return Make_Null_Statement (Loc);
3619 else
3620 return
3621 Make_Procedure_Call_Statement (Loc,
3622 Name => New_Occurrence_Of (RTE (RE), Loc));
3623 end if;
3624 end Build_Runtime_Call;
3626 ------------------------
3627 -- Build_SS_Mark_Call --
3628 ------------------------
3630 function Build_SS_Mark_Call
3631 (Loc : Source_Ptr;
3632 Mark : Entity_Id) return Node_Id
3634 begin
3635 -- Generate:
3636 -- Mark : constant Mark_Id := SS_Mark;
3638 return
3639 Make_Object_Declaration (Loc,
3640 Defining_Identifier => Mark,
3641 Constant_Present => True,
3642 Object_Definition =>
3643 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3644 Expression =>
3645 Make_Function_Call (Loc,
3646 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3647 end Build_SS_Mark_Call;
3649 ---------------------------
3650 -- Build_SS_Release_Call --
3651 ---------------------------
3653 function Build_SS_Release_Call
3654 (Loc : Source_Ptr;
3655 Mark : Entity_Id) return Node_Id
3657 begin
3658 -- Generate:
3659 -- SS_Release (Mark);
3661 return
3662 Make_Procedure_Call_Statement (Loc,
3663 Name =>
3664 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3665 Parameter_Associations => New_List (
3666 New_Occurrence_Of (Mark, Loc)));
3667 end Build_SS_Release_Call;
3669 ----------------------------
3670 -- Build_Task_Array_Image --
3671 ----------------------------
3673 -- This function generates the body for a function that constructs the
3674 -- image string for a task that is an array component. The function is
3675 -- local to the init proc for the array type, and is called for each one
3676 -- of the components. The constructed image has the form of an indexed
3677 -- component, whose prefix is the outer variable of the array type.
3678 -- The n-dimensional array type has known indexes Index, Index2...
3680 -- Id_Ref is an indexed component form created by the enclosing init proc.
3681 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3682 -- in the loops that call the individual task init proc on each component.
3684 -- The generated function has the following structure:
3686 -- function F return String is
3687 -- Pref : string renames Task_Name;
3688 -- T1 : String := Index1'Image (Val1);
3689 -- ...
3690 -- Tn : String := indexn'image (Valn);
3691 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3692 -- -- Len includes commas and the end parentheses.
3693 -- Res : String (1..Len);
3694 -- Pos : Integer := Pref'Length;
3696 -- begin
3697 -- Res (1 .. Pos) := Pref;
3698 -- Pos := Pos + 1;
3699 -- Res (Pos) := '(';
3700 -- Pos := Pos + 1;
3701 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3702 -- Pos := Pos + T1'Length;
3703 -- Res (Pos) := '.';
3704 -- Pos := Pos + 1;
3705 -- ...
3706 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3707 -- Res (Len) := ')';
3709 -- return Res;
3710 -- end F;
3712 -- Needless to say, multidimensional arrays of tasks are rare enough that
3713 -- the bulkiness of this code is not really a concern.
3715 function Build_Task_Array_Image
3716 (Loc : Source_Ptr;
3717 Id_Ref : Node_Id;
3718 A_Type : Entity_Id;
3719 Dyn : Boolean := False) return Node_Id
3721 Dims : constant Nat := Number_Dimensions (A_Type);
3722 -- Number of dimensions for array of tasks
3724 Temps : array (1 .. Dims) of Entity_Id;
3725 -- Array of temporaries to hold string for each index
3727 Indx : Node_Id;
3728 -- Index expression
3730 Len : Entity_Id;
3731 -- Total length of generated name
3733 Pos : Entity_Id;
3734 -- Running index for substring assignments
3736 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3737 -- Name of enclosing variable, prefix of resulting name
3739 Res : Entity_Id;
3740 -- String to hold result
3742 Val : Node_Id;
3743 -- Value of successive indexes
3745 Sum : Node_Id;
3746 -- Expression to compute total size of string
3748 T : Entity_Id;
3749 -- Entity for name at one index position
3751 Decls : constant List_Id := New_List;
3752 Stats : constant List_Id := New_List;
3754 begin
3755 -- For a dynamic task, the name comes from the target variable. For a
3756 -- static one it is a formal of the enclosing init proc.
3758 if Dyn then
3759 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3760 Append_To (Decls,
3761 Make_Object_Declaration (Loc,
3762 Defining_Identifier => Pref,
3763 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3764 Expression =>
3765 Make_String_Literal (Loc,
3766 Strval => String_From_Name_Buffer)));
3768 else
3769 Append_To (Decls,
3770 Make_Object_Renaming_Declaration (Loc,
3771 Defining_Identifier => Pref,
3772 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3773 Name => Make_Identifier (Loc, Name_uTask_Name)));
3774 end if;
3776 Indx := First_Index (A_Type);
3777 Val := First (Expressions (Id_Ref));
3779 for J in 1 .. Dims loop
3780 T := Make_Temporary (Loc, 'T');
3781 Temps (J) := T;
3783 Append_To (Decls,
3784 Make_Object_Declaration (Loc,
3785 Defining_Identifier => T,
3786 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3787 Expression =>
3788 Make_Attribute_Reference (Loc,
3789 Attribute_Name => Name_Image,
3790 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3791 Expressions => New_List (New_Copy_Tree (Val)))));
3793 Next_Index (Indx);
3794 Next (Val);
3795 end loop;
3797 Sum := Make_Integer_Literal (Loc, Dims + 1);
3799 Sum :=
3800 Make_Op_Add (Loc,
3801 Left_Opnd => Sum,
3802 Right_Opnd =>
3803 Make_Attribute_Reference (Loc,
3804 Attribute_Name => Name_Length,
3805 Prefix => New_Occurrence_Of (Pref, Loc),
3806 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3808 for J in 1 .. Dims loop
3809 Sum :=
3810 Make_Op_Add (Loc,
3811 Left_Opnd => Sum,
3812 Right_Opnd =>
3813 Make_Attribute_Reference (Loc,
3814 Attribute_Name => Name_Length,
3815 Prefix =>
3816 New_Occurrence_Of (Temps (J), Loc),
3817 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3818 end loop;
3820 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3822 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3824 Append_To (Stats,
3825 Make_Assignment_Statement (Loc,
3826 Name =>
3827 Make_Indexed_Component (Loc,
3828 Prefix => New_Occurrence_Of (Res, Loc),
3829 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3830 Expression =>
3831 Make_Character_Literal (Loc,
3832 Chars => Name_Find,
3833 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3835 Append_To (Stats,
3836 Make_Assignment_Statement (Loc,
3837 Name => New_Occurrence_Of (Pos, Loc),
3838 Expression =>
3839 Make_Op_Add (Loc,
3840 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3841 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3843 for J in 1 .. Dims loop
3845 Append_To (Stats,
3846 Make_Assignment_Statement (Loc,
3847 Name =>
3848 Make_Slice (Loc,
3849 Prefix => New_Occurrence_Of (Res, Loc),
3850 Discrete_Range =>
3851 Make_Range (Loc,
3852 Low_Bound => New_Occurrence_Of (Pos, Loc),
3853 High_Bound =>
3854 Make_Op_Subtract (Loc,
3855 Left_Opnd =>
3856 Make_Op_Add (Loc,
3857 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3858 Right_Opnd =>
3859 Make_Attribute_Reference (Loc,
3860 Attribute_Name => Name_Length,
3861 Prefix =>
3862 New_Occurrence_Of (Temps (J), Loc),
3863 Expressions =>
3864 New_List (Make_Integer_Literal (Loc, 1)))),
3865 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3867 Expression => New_Occurrence_Of (Temps (J), Loc)));
3869 if J < Dims then
3870 Append_To (Stats,
3871 Make_Assignment_Statement (Loc,
3872 Name => New_Occurrence_Of (Pos, Loc),
3873 Expression =>
3874 Make_Op_Add (Loc,
3875 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3876 Right_Opnd =>
3877 Make_Attribute_Reference (Loc,
3878 Attribute_Name => Name_Length,
3879 Prefix => New_Occurrence_Of (Temps (J), Loc),
3880 Expressions =>
3881 New_List (Make_Integer_Literal (Loc, 1))))));
3883 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3885 Append_To (Stats,
3886 Make_Assignment_Statement (Loc,
3887 Name => Make_Indexed_Component (Loc,
3888 Prefix => New_Occurrence_Of (Res, Loc),
3889 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3890 Expression =>
3891 Make_Character_Literal (Loc,
3892 Chars => Name_Find,
3893 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3895 Append_To (Stats,
3896 Make_Assignment_Statement (Loc,
3897 Name => New_Occurrence_Of (Pos, Loc),
3898 Expression =>
3899 Make_Op_Add (Loc,
3900 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3901 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3902 end if;
3903 end loop;
3905 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3907 Append_To (Stats,
3908 Make_Assignment_Statement (Loc,
3909 Name =>
3910 Make_Indexed_Component (Loc,
3911 Prefix => New_Occurrence_Of (Res, Loc),
3912 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3913 Expression =>
3914 Make_Character_Literal (Loc,
3915 Chars => Name_Find,
3916 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3917 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3918 end Build_Task_Array_Image;
3920 ----------------------------
3921 -- Build_Task_Image_Decls --
3922 ----------------------------
3924 function Build_Task_Image_Decls
3925 (Loc : Source_Ptr;
3926 Id_Ref : Node_Id;
3927 A_Type : Entity_Id;
3928 In_Init_Proc : Boolean := False) return List_Id
3930 Decls : constant List_Id := New_List;
3931 T_Id : Entity_Id := Empty;
3932 Decl : Node_Id;
3933 Expr : Node_Id := Empty;
3934 Fun : Node_Id := Empty;
3935 Is_Dyn : constant Boolean :=
3936 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3937 and then
3938 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3940 begin
3941 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3942 -- generate a dummy declaration only.
3944 if Restriction_Active (No_Implicit_Heap_Allocations)
3945 or else Global_Discard_Names
3946 then
3947 T_Id := Make_Temporary (Loc, 'J');
3948 Name_Len := 0;
3950 return
3951 New_List (
3952 Make_Object_Declaration (Loc,
3953 Defining_Identifier => T_Id,
3954 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3955 Expression =>
3956 Make_String_Literal (Loc,
3957 Strval => String_From_Name_Buffer)));
3959 else
3960 if Nkind (Id_Ref) = N_Identifier
3961 or else Nkind (Id_Ref) = N_Defining_Identifier
3962 then
3963 -- For a simple variable, the image of the task is built from
3964 -- the name of the variable. To avoid possible conflict with the
3965 -- anonymous type created for a single protected object, add a
3966 -- numeric suffix.
3968 T_Id :=
3969 Make_Defining_Identifier (Loc,
3970 New_External_Name (Chars (Id_Ref), 'T', 1));
3972 Get_Name_String (Chars (Id_Ref));
3974 Expr :=
3975 Make_String_Literal (Loc,
3976 Strval => String_From_Name_Buffer);
3978 elsif Nkind (Id_Ref) = N_Selected_Component then
3979 T_Id :=
3980 Make_Defining_Identifier (Loc,
3981 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3982 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3984 elsif Nkind (Id_Ref) = N_Indexed_Component then
3985 T_Id :=
3986 Make_Defining_Identifier (Loc,
3987 New_External_Name (Chars (A_Type), 'N'));
3989 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3990 end if;
3991 end if;
3993 if Present (Fun) then
3994 Append (Fun, Decls);
3995 Expr := Make_Function_Call (Loc,
3996 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
3998 if not In_Init_Proc then
3999 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4000 end if;
4001 end if;
4003 Decl := Make_Object_Declaration (Loc,
4004 Defining_Identifier => T_Id,
4005 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4006 Constant_Present => True,
4007 Expression => Expr);
4009 Append (Decl, Decls);
4010 return Decls;
4011 end Build_Task_Image_Decls;
4013 -------------------------------
4014 -- Build_Task_Image_Function --
4015 -------------------------------
4017 function Build_Task_Image_Function
4018 (Loc : Source_Ptr;
4019 Decls : List_Id;
4020 Stats : List_Id;
4021 Res : Entity_Id) return Node_Id
4023 Spec : Node_Id;
4025 begin
4026 Append_To (Stats,
4027 Make_Simple_Return_Statement (Loc,
4028 Expression => New_Occurrence_Of (Res, Loc)));
4030 Spec := Make_Function_Specification (Loc,
4031 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4032 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4034 -- Calls to 'Image use the secondary stack, which must be cleaned up
4035 -- after the task name is built.
4037 return Make_Subprogram_Body (Loc,
4038 Specification => Spec,
4039 Declarations => Decls,
4040 Handled_Statement_Sequence =>
4041 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4042 end Build_Task_Image_Function;
4044 -----------------------------
4045 -- Build_Task_Image_Prefix --
4046 -----------------------------
4048 procedure Build_Task_Image_Prefix
4049 (Loc : Source_Ptr;
4050 Len : out Entity_Id;
4051 Res : out Entity_Id;
4052 Pos : out Entity_Id;
4053 Prefix : Entity_Id;
4054 Sum : Node_Id;
4055 Decls : List_Id;
4056 Stats : List_Id)
4058 begin
4059 Len := Make_Temporary (Loc, 'L', Sum);
4061 Append_To (Decls,
4062 Make_Object_Declaration (Loc,
4063 Defining_Identifier => Len,
4064 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4065 Expression => Sum));
4067 Res := Make_Temporary (Loc, 'R');
4069 Append_To (Decls,
4070 Make_Object_Declaration (Loc,
4071 Defining_Identifier => Res,
4072 Object_Definition =>
4073 Make_Subtype_Indication (Loc,
4074 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4075 Constraint =>
4076 Make_Index_Or_Discriminant_Constraint (Loc,
4077 Constraints =>
4078 New_List (
4079 Make_Range (Loc,
4080 Low_Bound => Make_Integer_Literal (Loc, 1),
4081 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4083 -- Indicate that the result is an internal temporary, so it does not
4084 -- receive a bogus initialization when declaration is expanded. This
4085 -- is both efficient, and prevents anomalies in the handling of
4086 -- dynamic objects on the secondary stack.
4088 Set_Is_Internal (Res);
4089 Pos := Make_Temporary (Loc, 'P');
4091 Append_To (Decls,
4092 Make_Object_Declaration (Loc,
4093 Defining_Identifier => Pos,
4094 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4096 -- Pos := Prefix'Length;
4098 Append_To (Stats,
4099 Make_Assignment_Statement (Loc,
4100 Name => New_Occurrence_Of (Pos, Loc),
4101 Expression =>
4102 Make_Attribute_Reference (Loc,
4103 Attribute_Name => Name_Length,
4104 Prefix => New_Occurrence_Of (Prefix, Loc),
4105 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4107 -- Res (1 .. Pos) := Prefix;
4109 Append_To (Stats,
4110 Make_Assignment_Statement (Loc,
4111 Name =>
4112 Make_Slice (Loc,
4113 Prefix => New_Occurrence_Of (Res, Loc),
4114 Discrete_Range =>
4115 Make_Range (Loc,
4116 Low_Bound => Make_Integer_Literal (Loc, 1),
4117 High_Bound => New_Occurrence_Of (Pos, Loc))),
4119 Expression => New_Occurrence_Of (Prefix, Loc)));
4121 Append_To (Stats,
4122 Make_Assignment_Statement (Loc,
4123 Name => New_Occurrence_Of (Pos, Loc),
4124 Expression =>
4125 Make_Op_Add (Loc,
4126 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4127 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4128 end Build_Task_Image_Prefix;
4130 -----------------------------
4131 -- Build_Task_Record_Image --
4132 -----------------------------
4134 function Build_Task_Record_Image
4135 (Loc : Source_Ptr;
4136 Id_Ref : Node_Id;
4137 Dyn : Boolean := False) return Node_Id
4139 Len : Entity_Id;
4140 -- Total length of generated name
4142 Pos : Entity_Id;
4143 -- Index into result
4145 Res : Entity_Id;
4146 -- String to hold result
4148 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4149 -- Name of enclosing variable, prefix of resulting name
4151 Sum : Node_Id;
4152 -- Expression to compute total size of string
4154 Sel : Entity_Id;
4155 -- Entity for selector name
4157 Decls : constant List_Id := New_List;
4158 Stats : constant List_Id := New_List;
4160 begin
4161 -- For a dynamic task, the name comes from the target variable. For a
4162 -- static one it is a formal of the enclosing init proc.
4164 if Dyn then
4165 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4166 Append_To (Decls,
4167 Make_Object_Declaration (Loc,
4168 Defining_Identifier => Pref,
4169 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4170 Expression =>
4171 Make_String_Literal (Loc,
4172 Strval => String_From_Name_Buffer)));
4174 else
4175 Append_To (Decls,
4176 Make_Object_Renaming_Declaration (Loc,
4177 Defining_Identifier => Pref,
4178 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4179 Name => Make_Identifier (Loc, Name_uTask_Name)));
4180 end if;
4182 Sel := Make_Temporary (Loc, 'S');
4184 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4186 Append_To (Decls,
4187 Make_Object_Declaration (Loc,
4188 Defining_Identifier => Sel,
4189 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4190 Expression =>
4191 Make_String_Literal (Loc,
4192 Strval => String_From_Name_Buffer)));
4194 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4196 Sum :=
4197 Make_Op_Add (Loc,
4198 Left_Opnd => Sum,
4199 Right_Opnd =>
4200 Make_Attribute_Reference (Loc,
4201 Attribute_Name => Name_Length,
4202 Prefix =>
4203 New_Occurrence_Of (Pref, Loc),
4204 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4206 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4208 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4210 -- Res (Pos) := '.';
4212 Append_To (Stats,
4213 Make_Assignment_Statement (Loc,
4214 Name => Make_Indexed_Component (Loc,
4215 Prefix => New_Occurrence_Of (Res, Loc),
4216 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4217 Expression =>
4218 Make_Character_Literal (Loc,
4219 Chars => Name_Find,
4220 Char_Literal_Value =>
4221 UI_From_Int (Character'Pos ('.')))));
4223 Append_To (Stats,
4224 Make_Assignment_Statement (Loc,
4225 Name => New_Occurrence_Of (Pos, Loc),
4226 Expression =>
4227 Make_Op_Add (Loc,
4228 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4229 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4231 -- Res (Pos .. Len) := Selector;
4233 Append_To (Stats,
4234 Make_Assignment_Statement (Loc,
4235 Name => Make_Slice (Loc,
4236 Prefix => New_Occurrence_Of (Res, Loc),
4237 Discrete_Range =>
4238 Make_Range (Loc,
4239 Low_Bound => New_Occurrence_Of (Pos, Loc),
4240 High_Bound => New_Occurrence_Of (Len, Loc))),
4241 Expression => New_Occurrence_Of (Sel, Loc)));
4243 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4244 end Build_Task_Record_Image;
4246 ---------------------------------------
4247 -- Build_Transient_Object_Statements --
4248 ---------------------------------------
4250 procedure Build_Transient_Object_Statements
4251 (Obj_Decl : Node_Id;
4252 Fin_Call : out Node_Id;
4253 Hook_Assign : out Node_Id;
4254 Hook_Clear : out Node_Id;
4255 Hook_Decl : out Node_Id;
4256 Ptr_Decl : out Node_Id;
4257 Finalize_Obj : Boolean := True)
4259 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4260 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4261 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4263 Desig_Typ : Entity_Id;
4264 Hook_Expr : Node_Id;
4265 Hook_Id : Entity_Id;
4266 Obj_Ref : Node_Id;
4267 Ptr_Typ : Entity_Id;
4269 begin
4270 -- Recover the type of the object
4272 Desig_Typ := Obj_Typ;
4274 if Is_Access_Type (Desig_Typ) then
4275 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4276 end if;
4278 -- Create an access type which provides a reference to the transient
4279 -- object. Generate:
4281 -- type Ptr_Typ is access all Desig_Typ;
4283 Ptr_Typ := Make_Temporary (Loc, 'A');
4284 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4285 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4287 Ptr_Decl :=
4288 Make_Full_Type_Declaration (Loc,
4289 Defining_Identifier => Ptr_Typ,
4290 Type_Definition =>
4291 Make_Access_To_Object_Definition (Loc,
4292 All_Present => True,
4293 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4295 -- Create a temporary check which acts as a hook to the transient
4296 -- object. Generate:
4298 -- Hook : Ptr_Typ := null;
4300 Hook_Id := Make_Temporary (Loc, 'T');
4301 Set_Ekind (Hook_Id, E_Variable);
4302 Set_Etype (Hook_Id, Ptr_Typ);
4304 Hook_Decl :=
4305 Make_Object_Declaration (Loc,
4306 Defining_Identifier => Hook_Id,
4307 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4308 Expression => Make_Null (Loc));
4310 -- Mark the temporary as a hook. This signals the machinery in
4311 -- Build_Finalizer to recognize this special case.
4313 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4315 -- Hook the transient object to the temporary. Generate:
4317 -- Hook := Ptr_Typ (Obj_Id);
4318 -- <or>
4319 -- Hool := Obj_Id'Unrestricted_Access;
4321 if Is_Access_Type (Obj_Typ) then
4322 Hook_Expr :=
4323 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4324 else
4325 Hook_Expr :=
4326 Make_Attribute_Reference (Loc,
4327 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4328 Attribute_Name => Name_Unrestricted_Access);
4329 end if;
4331 Hook_Assign :=
4332 Make_Assignment_Statement (Loc,
4333 Name => New_Occurrence_Of (Hook_Id, Loc),
4334 Expression => Hook_Expr);
4336 -- Crear the hook prior to finalizing the object. Generate:
4338 -- Hook := null;
4340 Hook_Clear :=
4341 Make_Assignment_Statement (Loc,
4342 Name => New_Occurrence_Of (Hook_Id, Loc),
4343 Expression => Make_Null (Loc));
4345 -- Finalize the object. Generate:
4347 -- [Deep_]Finalize (Obj_Ref[.all]);
4349 if Finalize_Obj then
4350 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4352 if Is_Access_Type (Obj_Typ) then
4353 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4354 Set_Etype (Obj_Ref, Desig_Typ);
4355 end if;
4357 Fin_Call :=
4358 Make_Final_Call
4359 (Obj_Ref => Obj_Ref,
4360 Typ => Desig_Typ);
4362 -- Otherwise finalize the hook. Generate:
4364 -- [Deep_]Finalize (Hook.all);
4366 else
4367 Fin_Call :=
4368 Make_Final_Call (
4369 Obj_Ref =>
4370 Make_Explicit_Dereference (Loc,
4371 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4372 Typ => Desig_Typ);
4373 end if;
4374 end Build_Transient_Object_Statements;
4376 -----------------------------
4377 -- Check_Float_Op_Overflow --
4378 -----------------------------
4380 procedure Check_Float_Op_Overflow (N : Node_Id) is
4381 begin
4382 -- Return if no check needed
4384 if not Is_Floating_Point_Type (Etype (N))
4385 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4387 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4388 -- and do not expand the code for float overflow checking.
4390 or else CodePeer_Mode
4391 then
4392 return;
4393 end if;
4395 -- Otherwise we replace the expression by
4397 -- do Tnn : constant ftype := expression;
4398 -- constraint_error when not Tnn'Valid;
4399 -- in Tnn;
4401 declare
4402 Loc : constant Source_Ptr := Sloc (N);
4403 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4404 Typ : constant Entity_Id := Etype (N);
4406 begin
4407 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4408 -- right here. We also set the node as analyzed to prevent infinite
4409 -- recursion from repeating the operation in the expansion.
4411 Set_Do_Overflow_Check (N, False);
4412 Set_Analyzed (N, True);
4414 -- Do the rewrite to include the check
4416 Rewrite (N,
4417 Make_Expression_With_Actions (Loc,
4418 Actions => New_List (
4419 Make_Object_Declaration (Loc,
4420 Defining_Identifier => Tnn,
4421 Object_Definition => New_Occurrence_Of (Typ, Loc),
4422 Constant_Present => True,
4423 Expression => Relocate_Node (N)),
4424 Make_Raise_Constraint_Error (Loc,
4425 Condition =>
4426 Make_Op_Not (Loc,
4427 Right_Opnd =>
4428 Make_Attribute_Reference (Loc,
4429 Prefix => New_Occurrence_Of (Tnn, Loc),
4430 Attribute_Name => Name_Valid)),
4431 Reason => CE_Overflow_Check_Failed)),
4432 Expression => New_Occurrence_Of (Tnn, Loc)));
4434 Analyze_And_Resolve (N, Typ);
4435 end;
4436 end Check_Float_Op_Overflow;
4438 ----------------------------------
4439 -- Component_May_Be_Bit_Aligned --
4440 ----------------------------------
4442 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4443 UT : Entity_Id;
4445 begin
4446 -- If no component clause, then everything is fine, since the back end
4447 -- never bit-misaligns by default, even if there is a pragma Packed for
4448 -- the record.
4450 if No (Comp) or else No (Component_Clause (Comp)) then
4451 return False;
4452 end if;
4454 UT := Underlying_Type (Etype (Comp));
4456 -- It is only array and record types that cause trouble
4458 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4459 return False;
4461 -- If we know that we have a small (64 bits or less) record or small
4462 -- bit-packed array, then everything is fine, since the back end can
4463 -- handle these cases correctly.
4465 elsif Esize (Comp) <= 64
4466 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4467 then
4468 return False;
4470 -- Otherwise if the component is not byte aligned, we know we have the
4471 -- nasty unaligned case.
4473 elsif Normalized_First_Bit (Comp) /= Uint_0
4474 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4475 then
4476 return True;
4478 -- If we are large and byte aligned, then OK at this level
4480 else
4481 return False;
4482 end if;
4483 end Component_May_Be_Bit_Aligned;
4485 ----------------------------------------
4486 -- Containing_Package_With_Ext_Axioms --
4487 ----------------------------------------
4489 function Containing_Package_With_Ext_Axioms
4490 (E : Entity_Id) return Entity_Id
4492 begin
4493 -- E is the package or generic package which is externally axiomatized
4495 if Ekind_In (E, E_Generic_Package, E_Package)
4496 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4497 then
4498 return E;
4499 end if;
4501 -- If E's scope is axiomatized, E is axiomatized
4503 if Present (Scope (E)) then
4504 declare
4505 First_Ax_Parent_Scope : constant Entity_Id :=
4506 Containing_Package_With_Ext_Axioms (Scope (E));
4507 begin
4508 if Present (First_Ax_Parent_Scope) then
4509 return First_Ax_Parent_Scope;
4510 end if;
4511 end;
4512 end if;
4514 -- Otherwise, if E is a package instance, it is axiomatized if the
4515 -- corresponding generic package is axiomatized.
4517 if Ekind (E) = E_Package then
4518 declare
4519 Par : constant Node_Id := Parent (E);
4520 Decl : Node_Id;
4522 begin
4523 if Nkind (Par) = N_Defining_Program_Unit_Name then
4524 Decl := Parent (Par);
4525 else
4526 Decl := Par;
4527 end if;
4529 if Present (Generic_Parent (Decl)) then
4530 return
4531 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4532 end if;
4533 end;
4534 end if;
4536 return Empty;
4537 end Containing_Package_With_Ext_Axioms;
4539 -------------------------------
4540 -- Convert_To_Actual_Subtype --
4541 -------------------------------
4543 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4544 Act_ST : Entity_Id;
4546 begin
4547 Act_ST := Get_Actual_Subtype (Exp);
4549 if Act_ST = Etype (Exp) then
4550 return;
4551 else
4552 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4553 Analyze_And_Resolve (Exp, Act_ST);
4554 end if;
4555 end Convert_To_Actual_Subtype;
4557 -----------------------------------
4558 -- Corresponding_Runtime_Package --
4559 -----------------------------------
4561 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4562 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4563 -- Return True if protected type T has one entry and the maximum queue
4564 -- length is one.
4566 --------------------------------
4567 -- Has_One_Entry_And_No_Queue --
4568 --------------------------------
4570 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4571 Item : Entity_Id;
4572 Is_First : Boolean := True;
4574 begin
4575 Item := First_Entity (T);
4576 while Present (Item) loop
4577 if Is_Entry (Item) then
4579 -- The protected type has more than one entry
4581 if not Is_First then
4582 return False;
4583 end if;
4585 -- The queue length is not one
4587 if not Restriction_Active (No_Entry_Queue)
4588 and then Get_Max_Queue_Length (Item) /= Uint_1
4589 then
4590 return False;
4591 end if;
4593 Is_First := False;
4594 end if;
4596 Next_Entity (Item);
4597 end loop;
4599 return True;
4600 end Has_One_Entry_And_No_Queue;
4602 -- Local variables
4604 Pkg_Id : RTU_Id := RTU_Null;
4606 -- Start of processing for Corresponding_Runtime_Package
4608 begin
4609 pragma Assert (Is_Concurrent_Type (Typ));
4611 if Ekind (Typ) in Protected_Kind then
4612 if Has_Entries (Typ)
4614 -- A protected type without entries that covers an interface and
4615 -- overrides the abstract routines with protected procedures is
4616 -- considered equivalent to a protected type with entries in the
4617 -- context of dispatching select statements. It is sufficient to
4618 -- check for the presence of an interface list in the declaration
4619 -- node to recognize this case.
4621 or else Present (Interface_List (Parent (Typ)))
4623 -- Protected types with interrupt handlers (when not using a
4624 -- restricted profile) are also considered equivalent to
4625 -- protected types with entries. The types which are used
4626 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4627 -- are derived from Protection_Entries.
4629 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4630 or else Has_Interrupt_Handler (Typ)
4631 then
4632 if Abort_Allowed
4633 or else Restriction_Active (No_Select_Statements) = False
4634 or else not Has_One_Entry_And_No_Queue (Typ)
4635 or else (Has_Attach_Handler (Typ)
4636 and then not Restricted_Profile)
4637 then
4638 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4639 else
4640 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4641 end if;
4643 else
4644 Pkg_Id := System_Tasking_Protected_Objects;
4645 end if;
4646 end if;
4648 return Pkg_Id;
4649 end Corresponding_Runtime_Package;
4651 -----------------------------------
4652 -- Current_Sem_Unit_Declarations --
4653 -----------------------------------
4655 function Current_Sem_Unit_Declarations return List_Id is
4656 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4657 Decls : List_Id;
4659 begin
4660 -- If the current unit is a package body, locate the visible
4661 -- declarations of the package spec.
4663 if Nkind (U) = N_Package_Body then
4664 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4665 end if;
4667 if Nkind (U) = N_Package_Declaration then
4668 U := Specification (U);
4669 Decls := Visible_Declarations (U);
4671 if No (Decls) then
4672 Decls := New_List;
4673 Set_Visible_Declarations (U, Decls);
4674 end if;
4676 else
4677 Decls := Declarations (U);
4679 if No (Decls) then
4680 Decls := New_List;
4681 Set_Declarations (U, Decls);
4682 end if;
4683 end if;
4685 return Decls;
4686 end Current_Sem_Unit_Declarations;
4688 -----------------------
4689 -- Duplicate_Subexpr --
4690 -----------------------
4692 function Duplicate_Subexpr
4693 (Exp : Node_Id;
4694 Name_Req : Boolean := False;
4695 Renaming_Req : Boolean := False) return Node_Id
4697 begin
4698 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4699 return New_Copy_Tree (Exp);
4700 end Duplicate_Subexpr;
4702 ---------------------------------
4703 -- Duplicate_Subexpr_No_Checks --
4704 ---------------------------------
4706 function Duplicate_Subexpr_No_Checks
4707 (Exp : Node_Id;
4708 Name_Req : Boolean := False;
4709 Renaming_Req : Boolean := False;
4710 Related_Id : Entity_Id := Empty;
4711 Is_Low_Bound : Boolean := False;
4712 Is_High_Bound : Boolean := False) return Node_Id
4714 New_Exp : Node_Id;
4716 begin
4717 Remove_Side_Effects
4718 (Exp => Exp,
4719 Name_Req => Name_Req,
4720 Renaming_Req => Renaming_Req,
4721 Related_Id => Related_Id,
4722 Is_Low_Bound => Is_Low_Bound,
4723 Is_High_Bound => Is_High_Bound);
4725 New_Exp := New_Copy_Tree (Exp);
4726 Remove_Checks (New_Exp);
4727 return New_Exp;
4728 end Duplicate_Subexpr_No_Checks;
4730 -----------------------------------
4731 -- Duplicate_Subexpr_Move_Checks --
4732 -----------------------------------
4734 function Duplicate_Subexpr_Move_Checks
4735 (Exp : Node_Id;
4736 Name_Req : Boolean := False;
4737 Renaming_Req : Boolean := False) return Node_Id
4739 New_Exp : Node_Id;
4741 begin
4742 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4743 New_Exp := New_Copy_Tree (Exp);
4744 Remove_Checks (Exp);
4745 return New_Exp;
4746 end Duplicate_Subexpr_Move_Checks;
4748 -------------------------
4749 -- Enclosing_Init_Proc --
4750 -------------------------
4752 function Enclosing_Init_Proc return Entity_Id is
4753 S : Entity_Id;
4755 begin
4756 S := Current_Scope;
4757 while Present (S) and then S /= Standard_Standard loop
4758 if Is_Init_Proc (S) then
4759 return S;
4760 else
4761 S := Scope (S);
4762 end if;
4763 end loop;
4765 return Empty;
4766 end Enclosing_Init_Proc;
4768 --------------------
4769 -- Ensure_Defined --
4770 --------------------
4772 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4773 IR : Node_Id;
4775 begin
4776 -- An itype reference must only be created if this is a local itype, so
4777 -- that gigi can elaborate it on the proper objstack.
4779 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4780 IR := Make_Itype_Reference (Sloc (N));
4781 Set_Itype (IR, Typ);
4782 Insert_Action (N, IR);
4783 end if;
4784 end Ensure_Defined;
4786 --------------------
4787 -- Entry_Names_OK --
4788 --------------------
4790 function Entry_Names_OK return Boolean is
4791 begin
4792 return
4793 not Restricted_Profile
4794 and then not Global_Discard_Names
4795 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4796 and then not Restriction_Active (No_Local_Allocators);
4797 end Entry_Names_OK;
4799 -------------------
4800 -- Evaluate_Name --
4801 -------------------
4803 procedure Evaluate_Name (Nam : Node_Id) is
4804 begin
4805 -- For an attribute reference or an indexed component, evaluate the
4806 -- prefix, which is itself a name, recursively, and then force the
4807 -- evaluation of all the subscripts (or attribute expressions).
4809 case Nkind (Nam) is
4810 when N_Attribute_Reference
4811 | N_Indexed_Component
4813 Evaluate_Name (Prefix (Nam));
4815 declare
4816 E : Node_Id;
4818 begin
4819 E := First (Expressions (Nam));
4820 while Present (E) loop
4821 Force_Evaluation (E);
4823 if Is_Rewrite_Substitution (E) then
4824 Set_Do_Range_Check
4825 (E, Do_Range_Check (Original_Node (E)));
4826 end if;
4828 Next (E);
4829 end loop;
4830 end;
4832 -- For an explicit dereference, we simply force the evaluation of
4833 -- the name expression. The dereference provides a value that is the
4834 -- address for the renamed object, and it is precisely this value
4835 -- that we want to preserve.
4837 when N_Explicit_Dereference =>
4838 Force_Evaluation (Prefix (Nam));
4840 -- For a function call, we evaluate the call
4842 when N_Function_Call =>
4843 Force_Evaluation (Nam);
4845 -- For a qualified expression, we evaluate the underlying object
4846 -- name if any, otherwise we force the evaluation of the underlying
4847 -- expression.
4849 when N_Qualified_Expression =>
4850 if Is_Object_Reference (Expression (Nam)) then
4851 Evaluate_Name (Expression (Nam));
4852 else
4853 Force_Evaluation (Expression (Nam));
4854 end if;
4856 -- For a selected component, we simply evaluate the prefix
4858 when N_Selected_Component =>
4859 Evaluate_Name (Prefix (Nam));
4861 -- For a slice, we evaluate the prefix, as for the indexed component
4862 -- case and then, if there is a range present, either directly or as
4863 -- the constraint of a discrete subtype indication, we evaluate the
4864 -- two bounds of this range.
4866 when N_Slice =>
4867 Evaluate_Name (Prefix (Nam));
4868 Evaluate_Slice_Bounds (Nam);
4870 -- For a type conversion, the expression of the conversion must be
4871 -- the name of an object, and we simply need to evaluate this name.
4873 when N_Type_Conversion =>
4874 Evaluate_Name (Expression (Nam));
4876 -- The remaining cases are direct name, operator symbol and character
4877 -- literal. In all these cases, we do nothing, since we want to
4878 -- reevaluate each time the renamed object is used.
4880 when others =>
4881 null;
4882 end case;
4883 end Evaluate_Name;
4885 ---------------------------
4886 -- Evaluate_Slice_Bounds --
4887 ---------------------------
4889 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4890 DR : constant Node_Id := Discrete_Range (Slice);
4891 Constr : Node_Id;
4892 Rexpr : Node_Id;
4894 begin
4895 if Nkind (DR) = N_Range then
4896 Force_Evaluation (Low_Bound (DR));
4897 Force_Evaluation (High_Bound (DR));
4899 elsif Nkind (DR) = N_Subtype_Indication then
4900 Constr := Constraint (DR);
4902 if Nkind (Constr) = N_Range_Constraint then
4903 Rexpr := Range_Expression (Constr);
4905 Force_Evaluation (Low_Bound (Rexpr));
4906 Force_Evaluation (High_Bound (Rexpr));
4907 end if;
4908 end if;
4909 end Evaluate_Slice_Bounds;
4911 ---------------------
4912 -- Evolve_And_Then --
4913 ---------------------
4915 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4916 begin
4917 if No (Cond) then
4918 Cond := Cond1;
4919 else
4920 Cond :=
4921 Make_And_Then (Sloc (Cond1),
4922 Left_Opnd => Cond,
4923 Right_Opnd => Cond1);
4924 end if;
4925 end Evolve_And_Then;
4927 --------------------
4928 -- Evolve_Or_Else --
4929 --------------------
4931 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4932 begin
4933 if No (Cond) then
4934 Cond := Cond1;
4935 else
4936 Cond :=
4937 Make_Or_Else (Sloc (Cond1),
4938 Left_Opnd => Cond,
4939 Right_Opnd => Cond1);
4940 end if;
4941 end Evolve_Or_Else;
4943 -----------------------------------
4944 -- Exceptions_In_Finalization_OK --
4945 -----------------------------------
4947 function Exceptions_In_Finalization_OK return Boolean is
4948 begin
4949 return
4950 not (Restriction_Active (No_Exception_Handlers) or else
4951 Restriction_Active (No_Exception_Propagation) or else
4952 Restriction_Active (No_Exceptions));
4953 end Exceptions_In_Finalization_OK;
4955 -----------------------------------------
4956 -- Expand_Static_Predicates_In_Choices --
4957 -----------------------------------------
4959 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4960 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4962 Choices : constant List_Id := Discrete_Choices (N);
4964 Choice : Node_Id;
4965 Next_C : Node_Id;
4966 P : Node_Id;
4967 C : Node_Id;
4969 begin
4970 Choice := First (Choices);
4971 while Present (Choice) loop
4972 Next_C := Next (Choice);
4974 -- Check for name of subtype with static predicate
4976 if Is_Entity_Name (Choice)
4977 and then Is_Type (Entity (Choice))
4978 and then Has_Predicates (Entity (Choice))
4979 then
4980 -- Loop through entries in predicate list, converting to choices
4981 -- and inserting in the list before the current choice. Note that
4982 -- if the list is empty, corresponding to a False predicate, then
4983 -- no choices are inserted.
4985 P := First (Static_Discrete_Predicate (Entity (Choice)));
4986 while Present (P) loop
4988 -- If low bound and high bounds are equal, copy simple choice
4990 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4991 C := New_Copy (Low_Bound (P));
4993 -- Otherwise copy a range
4995 else
4996 C := New_Copy (P);
4997 end if;
4999 -- Change Sloc to referencing choice (rather than the Sloc of
5000 -- the predicate declaration element itself).
5002 Set_Sloc (C, Sloc (Choice));
5003 Insert_Before (Choice, C);
5004 Next (P);
5005 end loop;
5007 -- Delete the predicated entry
5009 Remove (Choice);
5010 end if;
5012 -- Move to next choice to check
5014 Choice := Next_C;
5015 end loop;
5017 Set_Has_SP_Choice (N, False);
5018 end Expand_Static_Predicates_In_Choices;
5020 ------------------------------
5021 -- Expand_Subtype_From_Expr --
5022 ------------------------------
5024 -- This function is applicable for both static and dynamic allocation of
5025 -- objects which are constrained by an initial expression. Basically it
5026 -- transforms an unconstrained subtype indication into a constrained one.
5028 -- The expression may also be transformed in certain cases in order to
5029 -- avoid multiple evaluation. In the static allocation case, the general
5030 -- scheme is:
5032 -- Val : T := Expr;
5034 -- is transformed into
5036 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5038 -- Here are the main cases :
5040 -- <if Expr is a Slice>
5041 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5043 -- <elsif Expr is a String Literal>
5044 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5046 -- <elsif Expr is Constrained>
5047 -- subtype T is Type_Of_Expr
5048 -- Val : T := Expr;
5050 -- <elsif Expr is an entity_name>
5051 -- Val : T (constraints taken from Expr) := Expr;
5053 -- <else>
5054 -- type Axxx is access all T;
5055 -- Rval : Axxx := Expr'ref;
5056 -- Val : T (constraints taken from Rval) := Rval.all;
5058 -- ??? note: when the Expression is allocated in the secondary stack
5059 -- we could use it directly instead of copying it by declaring
5060 -- Val : T (...) renames Rval.all
5062 procedure Expand_Subtype_From_Expr
5063 (N : Node_Id;
5064 Unc_Type : Entity_Id;
5065 Subtype_Indic : Node_Id;
5066 Exp : Node_Id;
5067 Related_Id : Entity_Id := Empty)
5069 Loc : constant Source_Ptr := Sloc (N);
5070 Exp_Typ : constant Entity_Id := Etype (Exp);
5071 T : Entity_Id;
5073 begin
5074 -- In general we cannot build the subtype if expansion is disabled,
5075 -- because internal entities may not have been defined. However, to
5076 -- avoid some cascaded errors, we try to continue when the expression is
5077 -- an array (or string), because it is safe to compute the bounds. It is
5078 -- in fact required to do so even in a generic context, because there
5079 -- may be constants that depend on the bounds of a string literal, both
5080 -- standard string types and more generally arrays of characters.
5082 -- In GNATprove mode, these extra subtypes are not needed
5084 if GNATprove_Mode then
5085 return;
5086 end if;
5088 if not Expander_Active
5089 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5090 then
5091 return;
5092 end if;
5094 if Nkind (Exp) = N_Slice then
5095 declare
5096 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5098 begin
5099 Rewrite (Subtype_Indic,
5100 Make_Subtype_Indication (Loc,
5101 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5102 Constraint =>
5103 Make_Index_Or_Discriminant_Constraint (Loc,
5104 Constraints => New_List
5105 (New_Occurrence_Of (Slice_Type, Loc)))));
5107 -- This subtype indication may be used later for constraint checks
5108 -- we better make sure that if a variable was used as a bound of
5109 -- of the original slice, its value is frozen.
5111 Evaluate_Slice_Bounds (Exp);
5112 end;
5114 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5115 Rewrite (Subtype_Indic,
5116 Make_Subtype_Indication (Loc,
5117 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5118 Constraint =>
5119 Make_Index_Or_Discriminant_Constraint (Loc,
5120 Constraints => New_List (
5121 Make_Literal_Range (Loc,
5122 Literal_Typ => Exp_Typ)))));
5124 -- If the type of the expression is an internally generated type it
5125 -- may not be necessary to create a new subtype. However there are two
5126 -- exceptions: references to the current instances, and aliased array
5127 -- object declarations for which the back end has to create a template.
5129 elsif Is_Constrained (Exp_Typ)
5130 and then not Is_Class_Wide_Type (Unc_Type)
5131 and then
5132 (Nkind (N) /= N_Object_Declaration
5133 or else not Is_Entity_Name (Expression (N))
5134 or else not Comes_From_Source (Entity (Expression (N)))
5135 or else not Is_Array_Type (Exp_Typ)
5136 or else not Aliased_Present (N))
5137 then
5138 if Is_Itype (Exp_Typ) then
5140 -- Within an initialization procedure, a selected component
5141 -- denotes a component of the enclosing record, and it appears as
5142 -- an actual in a call to its own initialization procedure. If
5143 -- this component depends on the outer discriminant, we must
5144 -- generate the proper actual subtype for it.
5146 if Nkind (Exp) = N_Selected_Component
5147 and then Within_Init_Proc
5148 then
5149 declare
5150 Decl : constant Node_Id :=
5151 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5152 begin
5153 if Present (Decl) then
5154 Insert_Action (N, Decl);
5155 T := Defining_Identifier (Decl);
5156 else
5157 T := Exp_Typ;
5158 end if;
5159 end;
5161 -- No need to generate a new subtype
5163 else
5164 T := Exp_Typ;
5165 end if;
5167 else
5168 T := Make_Temporary (Loc, 'T');
5170 Insert_Action (N,
5171 Make_Subtype_Declaration (Loc,
5172 Defining_Identifier => T,
5173 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5175 -- This type is marked as an itype even though it has an explicit
5176 -- declaration since otherwise Is_Generic_Actual_Type can get
5177 -- set, resulting in the generation of spurious errors. (See
5178 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5180 Set_Is_Itype (T);
5181 Set_Associated_Node_For_Itype (T, Exp);
5182 end if;
5184 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5186 -- Nothing needs to be done for private types with unknown discriminants
5187 -- if the underlying type is not an unconstrained composite type or it
5188 -- is an unchecked union.
5190 elsif Is_Private_Type (Unc_Type)
5191 and then Has_Unknown_Discriminants (Unc_Type)
5192 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5193 or else Is_Constrained (Underlying_Type (Unc_Type))
5194 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5195 then
5196 null;
5198 -- Case of derived type with unknown discriminants where the parent type
5199 -- also has unknown discriminants.
5201 elsif Is_Record_Type (Unc_Type)
5202 and then not Is_Class_Wide_Type (Unc_Type)
5203 and then Has_Unknown_Discriminants (Unc_Type)
5204 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5205 then
5206 -- Nothing to be done if no underlying record view available
5208 -- If this is a limited type derived from a type with unknown
5209 -- discriminants, do not expand either, so that subsequent expansion
5210 -- of the call can add build-in-place parameters to call.
5212 if No (Underlying_Record_View (Unc_Type))
5213 or else Is_Limited_Type (Unc_Type)
5214 then
5215 null;
5217 -- Otherwise use the Underlying_Record_View to create the proper
5218 -- constrained subtype for an object of a derived type with unknown
5219 -- discriminants.
5221 else
5222 Remove_Side_Effects (Exp);
5223 Rewrite (Subtype_Indic,
5224 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5225 end if;
5227 -- Renamings of class-wide interface types require no equivalent
5228 -- constrained type declarations because we only need to reference
5229 -- the tag component associated with the interface. The same is
5230 -- presumably true for class-wide types in general, so this test
5231 -- is broadened to include all class-wide renamings, which also
5232 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5233 -- (Is this really correct, or are there some cases of class-wide
5234 -- renamings that require action in this procedure???)
5236 elsif Present (N)
5237 and then Nkind (N) = N_Object_Renaming_Declaration
5238 and then Is_Class_Wide_Type (Unc_Type)
5239 then
5240 null;
5242 -- In Ada 95 nothing to be done if the type of the expression is limited
5243 -- because in this case the expression cannot be copied, and its use can
5244 -- only be by reference.
5246 -- In Ada 2005 the context can be an object declaration whose expression
5247 -- is a function that returns in place. If the nominal subtype has
5248 -- unknown discriminants, the call still provides constraints on the
5249 -- object, and we have to create an actual subtype from it.
5251 -- If the type is class-wide, the expression is dynamically tagged and
5252 -- we do not create an actual subtype either. Ditto for an interface.
5253 -- For now this applies only if the type is immutably limited, and the
5254 -- function being called is build-in-place. This will have to be revised
5255 -- when build-in-place functions are generalized to other types.
5257 elsif Is_Limited_View (Exp_Typ)
5258 and then
5259 (Is_Class_Wide_Type (Exp_Typ)
5260 or else Is_Interface (Exp_Typ)
5261 or else not Has_Unknown_Discriminants (Exp_Typ)
5262 or else not Is_Composite_Type (Unc_Type))
5263 then
5264 null;
5266 -- For limited objects initialized with build in place function calls,
5267 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5268 -- node in the expression initializing the object, which breaks the
5269 -- circuitry that detects and adds the additional arguments to the
5270 -- called function.
5272 elsif Is_Build_In_Place_Function_Call (Exp) then
5273 null;
5275 else
5276 Remove_Side_Effects (Exp);
5277 Rewrite (Subtype_Indic,
5278 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5279 end if;
5280 end Expand_Subtype_From_Expr;
5282 ---------------------------------------------
5283 -- Expression_Contains_Primitives_Calls_Of --
5284 ---------------------------------------------
5286 function Expression_Contains_Primitives_Calls_Of
5287 (Expr : Node_Id;
5288 Typ : Entity_Id) return Boolean
5290 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5292 Calls_OK : Boolean := False;
5293 -- This flag is set to True when expression Expr contains at least one
5294 -- call to a nondispatching primitive function of Typ.
5296 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5297 -- Search for nondispatching calls to primitive functions of type Typ
5299 ----------------------------
5300 -- Search_Primitive_Calls --
5301 ----------------------------
5303 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5304 Disp_Typ : Entity_Id;
5305 Subp : Entity_Id;
5307 begin
5308 -- Detect a function call that could denote a nondispatching
5309 -- primitive of the input type.
5311 if Nkind (N) = N_Function_Call
5312 and then Is_Entity_Name (Name (N))
5313 then
5314 Subp := Entity (Name (N));
5316 -- Do not consider function calls with a controlling argument, as
5317 -- those are always dispatching calls.
5319 if Is_Dispatching_Operation (Subp)
5320 and then No (Controlling_Argument (N))
5321 then
5322 Disp_Typ := Find_Dispatching_Type (Subp);
5324 -- To qualify as a suitable primitive, the dispatching type of
5325 -- the function must be the input type.
5327 if Present (Disp_Typ)
5328 and then Unique_Entity (Disp_Typ) = U_Typ
5329 then
5330 Calls_OK := True;
5332 -- There is no need to continue the traversal, as one such
5333 -- call suffices.
5335 return Abandon;
5336 end if;
5337 end if;
5338 end if;
5340 return OK;
5341 end Search_Primitive_Calls;
5343 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5345 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5347 begin
5348 Search_Calls (Expr);
5349 return Calls_OK;
5350 end Expression_Contains_Primitives_Calls_Of;
5352 ----------------------
5353 -- Finalize_Address --
5354 ----------------------
5356 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5357 Utyp : Entity_Id := Typ;
5359 begin
5360 -- Handle protected class-wide or task class-wide types
5362 if Is_Class_Wide_Type (Utyp) then
5363 if Is_Concurrent_Type (Root_Type (Utyp)) then
5364 Utyp := Root_Type (Utyp);
5366 elsif Is_Private_Type (Root_Type (Utyp))
5367 and then Present (Full_View (Root_Type (Utyp)))
5368 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5369 then
5370 Utyp := Full_View (Root_Type (Utyp));
5371 end if;
5372 end if;
5374 -- Handle private types
5376 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5377 Utyp := Full_View (Utyp);
5378 end if;
5380 -- Handle protected and task types
5382 if Is_Concurrent_Type (Utyp)
5383 and then Present (Corresponding_Record_Type (Utyp))
5384 then
5385 Utyp := Corresponding_Record_Type (Utyp);
5386 end if;
5388 Utyp := Underlying_Type (Base_Type (Utyp));
5390 -- Deal with untagged derivation of private views. If the parent is
5391 -- now known to be protected, the finalization routine is the one
5392 -- defined on the corresponding record of the ancestor (corresponding
5393 -- records do not automatically inherit operations, but maybe they
5394 -- should???)
5396 if Is_Untagged_Derivation (Typ) then
5397 if Is_Protected_Type (Typ) then
5398 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5400 else
5401 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5403 if Is_Protected_Type (Utyp) then
5404 Utyp := Corresponding_Record_Type (Utyp);
5405 end if;
5406 end if;
5407 end if;
5409 -- If the underlying_type is a subtype, we are dealing with the
5410 -- completion of a private type. We need to access the base type and
5411 -- generate a conversion to it.
5413 if Utyp /= Base_Type (Utyp) then
5414 pragma Assert (Is_Private_Type (Typ));
5416 Utyp := Base_Type (Utyp);
5417 end if;
5419 -- When dealing with an internally built full view for a type with
5420 -- unknown discriminants, use the original record type.
5422 if Is_Underlying_Record_View (Utyp) then
5423 Utyp := Etype (Utyp);
5424 end if;
5426 return TSS (Utyp, TSS_Finalize_Address);
5427 end Finalize_Address;
5429 ------------------------
5430 -- Find_Interface_ADT --
5431 ------------------------
5433 function Find_Interface_ADT
5434 (T : Entity_Id;
5435 Iface : Entity_Id) return Elmt_Id
5437 ADT : Elmt_Id;
5438 Typ : Entity_Id := T;
5440 begin
5441 pragma Assert (Is_Interface (Iface));
5443 -- Handle private types
5445 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5446 Typ := Full_View (Typ);
5447 end if;
5449 -- Handle access types
5451 if Is_Access_Type (Typ) then
5452 Typ := Designated_Type (Typ);
5453 end if;
5455 -- Handle task and protected types implementing interfaces
5457 if Is_Concurrent_Type (Typ) then
5458 Typ := Corresponding_Record_Type (Typ);
5459 end if;
5461 pragma Assert
5462 (not Is_Class_Wide_Type (Typ)
5463 and then Ekind (Typ) /= E_Incomplete_Type);
5465 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5466 return First_Elmt (Access_Disp_Table (Typ));
5468 else
5469 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5470 while Present (ADT)
5471 and then Present (Related_Type (Node (ADT)))
5472 and then Related_Type (Node (ADT)) /= Iface
5473 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5474 Use_Full_View => True)
5475 loop
5476 Next_Elmt (ADT);
5477 end loop;
5479 pragma Assert (Present (Related_Type (Node (ADT))));
5480 return ADT;
5481 end if;
5482 end Find_Interface_ADT;
5484 ------------------------
5485 -- Find_Interface_Tag --
5486 ------------------------
5488 function Find_Interface_Tag
5489 (T : Entity_Id;
5490 Iface : Entity_Id) return Entity_Id
5492 AI_Tag : Entity_Id := Empty;
5493 Found : Boolean := False;
5494 Typ : Entity_Id := T;
5496 procedure Find_Tag (Typ : Entity_Id);
5497 -- Internal subprogram used to recursively climb to the ancestors
5499 --------------
5500 -- Find_Tag --
5501 --------------
5503 procedure Find_Tag (Typ : Entity_Id) is
5504 AI_Elmt : Elmt_Id;
5505 AI : Node_Id;
5507 begin
5508 -- This routine does not handle the case in which the interface is an
5509 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5511 pragma Assert (Typ /= Iface);
5513 -- Climb to the root type handling private types
5515 if Present (Full_View (Etype (Typ))) then
5516 if Full_View (Etype (Typ)) /= Typ then
5517 Find_Tag (Full_View (Etype (Typ)));
5518 end if;
5520 elsif Etype (Typ) /= Typ then
5521 Find_Tag (Etype (Typ));
5522 end if;
5524 -- Traverse the list of interfaces implemented by the type
5526 if not Found
5527 and then Present (Interfaces (Typ))
5528 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5529 then
5530 -- Skip the tag associated with the primary table
5532 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5533 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5534 pragma Assert (Present (AI_Tag));
5536 AI_Elmt := First_Elmt (Interfaces (Typ));
5537 while Present (AI_Elmt) loop
5538 AI := Node (AI_Elmt);
5540 if AI = Iface
5541 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5542 then
5543 Found := True;
5544 return;
5545 end if;
5547 AI_Tag := Next_Tag_Component (AI_Tag);
5548 Next_Elmt (AI_Elmt);
5549 end loop;
5550 end if;
5551 end Find_Tag;
5553 -- Start of processing for Find_Interface_Tag
5555 begin
5556 pragma Assert (Is_Interface (Iface));
5558 -- Handle access types
5560 if Is_Access_Type (Typ) then
5561 Typ := Designated_Type (Typ);
5562 end if;
5564 -- Handle class-wide types
5566 if Is_Class_Wide_Type (Typ) then
5567 Typ := Root_Type (Typ);
5568 end if;
5570 -- Handle private types
5572 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5573 Typ := Full_View (Typ);
5574 end if;
5576 -- Handle entities from the limited view
5578 if Ekind (Typ) = E_Incomplete_Type then
5579 pragma Assert (Present (Non_Limited_View (Typ)));
5580 Typ := Non_Limited_View (Typ);
5581 end if;
5583 -- Handle task and protected types implementing interfaces
5585 if Is_Concurrent_Type (Typ) then
5586 Typ := Corresponding_Record_Type (Typ);
5587 end if;
5589 -- If the interface is an ancestor of the type, then it shared the
5590 -- primary dispatch table.
5592 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5593 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5594 return First_Tag_Component (Typ);
5596 -- Otherwise we need to search for its associated tag component
5598 else
5599 Find_Tag (Typ);
5600 pragma Assert (Found);
5601 return AI_Tag;
5602 end if;
5603 end Find_Interface_Tag;
5605 ---------------------------
5606 -- Find_Optional_Prim_Op --
5607 ---------------------------
5609 function Find_Optional_Prim_Op
5610 (T : Entity_Id; Name : Name_Id) return Entity_Id
5612 Prim : Elmt_Id;
5613 Typ : Entity_Id := T;
5614 Op : Entity_Id;
5616 begin
5617 if Is_Class_Wide_Type (Typ) then
5618 Typ := Root_Type (Typ);
5619 end if;
5621 Typ := Underlying_Type (Typ);
5623 -- Loop through primitive operations
5625 Prim := First_Elmt (Primitive_Operations (Typ));
5626 while Present (Prim) loop
5627 Op := Node (Prim);
5629 -- We can retrieve primitive operations by name if it is an internal
5630 -- name. For equality we must check that both of its operands have
5631 -- the same type, to avoid confusion with user-defined equalities
5632 -- than may have a non-symmetric signature.
5634 exit when Chars (Op) = Name
5635 and then
5636 (Name /= Name_Op_Eq
5637 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5639 Next_Elmt (Prim);
5640 end loop;
5642 return Node (Prim); -- Empty if not found
5643 end Find_Optional_Prim_Op;
5645 ---------------------------
5646 -- Find_Optional_Prim_Op --
5647 ---------------------------
5649 function Find_Optional_Prim_Op
5650 (T : Entity_Id;
5651 Name : TSS_Name_Type) return Entity_Id
5653 Inher_Op : Entity_Id := Empty;
5654 Own_Op : Entity_Id := Empty;
5655 Prim_Elmt : Elmt_Id;
5656 Prim_Id : Entity_Id;
5657 Typ : Entity_Id := T;
5659 begin
5660 if Is_Class_Wide_Type (Typ) then
5661 Typ := Root_Type (Typ);
5662 end if;
5664 Typ := Underlying_Type (Typ);
5666 -- This search is based on the assertion that the dispatching version
5667 -- of the TSS routine always precedes the real primitive.
5669 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5670 while Present (Prim_Elmt) loop
5671 Prim_Id := Node (Prim_Elmt);
5673 if Is_TSS (Prim_Id, Name) then
5674 if Present (Alias (Prim_Id)) then
5675 Inher_Op := Prim_Id;
5676 else
5677 Own_Op := Prim_Id;
5678 end if;
5679 end if;
5681 Next_Elmt (Prim_Elmt);
5682 end loop;
5684 if Present (Own_Op) then
5685 return Own_Op;
5686 elsif Present (Inher_Op) then
5687 return Inher_Op;
5688 else
5689 return Empty;
5690 end if;
5691 end Find_Optional_Prim_Op;
5693 ------------------
5694 -- Find_Prim_Op --
5695 ------------------
5697 function Find_Prim_Op
5698 (T : Entity_Id; Name : Name_Id) return Entity_Id
5700 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5701 begin
5702 if No (Result) then
5703 raise Program_Error;
5704 end if;
5706 return Result;
5707 end Find_Prim_Op;
5709 ------------------
5710 -- Find_Prim_Op --
5711 ------------------
5713 function Find_Prim_Op
5714 (T : Entity_Id;
5715 Name : TSS_Name_Type) return Entity_Id
5717 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5718 begin
5719 if No (Result) then
5720 raise Program_Error;
5721 end if;
5723 return Result;
5724 end Find_Prim_Op;
5726 ----------------------------
5727 -- Find_Protection_Object --
5728 ----------------------------
5730 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5731 S : Entity_Id;
5733 begin
5734 S := Scop;
5735 while Present (S) loop
5736 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5737 and then Present (Protection_Object (S))
5738 then
5739 return Protection_Object (S);
5740 end if;
5742 S := Scope (S);
5743 end loop;
5745 -- If we do not find a Protection object in the scope chain, then
5746 -- something has gone wrong, most likely the object was never created.
5748 raise Program_Error;
5749 end Find_Protection_Object;
5751 --------------------------
5752 -- Find_Protection_Type --
5753 --------------------------
5755 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5756 Comp : Entity_Id;
5757 Typ : Entity_Id := Conc_Typ;
5759 begin
5760 if Is_Concurrent_Type (Typ) then
5761 Typ := Corresponding_Record_Type (Typ);
5762 end if;
5764 -- Since restriction violations are not considered serious errors, the
5765 -- expander remains active, but may leave the corresponding record type
5766 -- malformed. In such cases, component _object is not available so do
5767 -- not look for it.
5769 if not Analyzed (Typ) then
5770 return Empty;
5771 end if;
5773 Comp := First_Component (Typ);
5774 while Present (Comp) loop
5775 if Chars (Comp) = Name_uObject then
5776 return Base_Type (Etype (Comp));
5777 end if;
5779 Next_Component (Comp);
5780 end loop;
5782 -- The corresponding record of a protected type should always have an
5783 -- _object field.
5785 raise Program_Error;
5786 end Find_Protection_Type;
5788 -----------------------
5789 -- Find_Hook_Context --
5790 -----------------------
5792 function Find_Hook_Context (N : Node_Id) return Node_Id is
5793 Par : Node_Id;
5794 Top : Node_Id;
5796 Wrapped_Node : Node_Id;
5797 -- Note: if we are in a transient scope, we want to reuse it as
5798 -- the context for actions insertion, if possible. But if N is itself
5799 -- part of the stored actions for the current transient scope,
5800 -- then we need to insert at the appropriate (inner) location in
5801 -- the not as an action on Node_To_Be_Wrapped.
5803 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5805 begin
5806 -- When the node is inside a case/if expression, the lifetime of any
5807 -- temporary controlled object is extended. Find a suitable insertion
5808 -- node by locating the topmost case or if expressions.
5810 if In_Cond_Expr then
5811 Par := N;
5812 Top := N;
5813 while Present (Par) loop
5814 if Nkind_In (Original_Node (Par), N_Case_Expression,
5815 N_If_Expression)
5816 then
5817 Top := Par;
5819 -- Prevent the search from going too far
5821 elsif Is_Body_Or_Package_Declaration (Par) then
5822 exit;
5823 end if;
5825 Par := Parent (Par);
5826 end loop;
5828 -- The topmost case or if expression is now recovered, but it may
5829 -- still not be the correct place to add generated code. Climb to
5830 -- find a parent that is part of a declarative or statement list,
5831 -- and is not a list of actuals in a call.
5833 Par := Top;
5834 while Present (Par) loop
5835 if Is_List_Member (Par)
5836 and then not Nkind_In (Par, N_Component_Association,
5837 N_Discriminant_Association,
5838 N_Parameter_Association,
5839 N_Pragma_Argument_Association)
5840 and then not Nkind_In (Parent (Par), N_Function_Call,
5841 N_Procedure_Call_Statement,
5842 N_Entry_Call_Statement)
5844 then
5845 return Par;
5847 -- Prevent the search from going too far
5849 elsif Is_Body_Or_Package_Declaration (Par) then
5850 exit;
5851 end if;
5853 Par := Parent (Par);
5854 end loop;
5856 return Par;
5858 else
5859 Par := N;
5860 while Present (Par) loop
5862 -- Keep climbing past various operators
5864 if Nkind (Parent (Par)) in N_Op
5865 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5866 then
5867 Par := Parent (Par);
5868 else
5869 exit;
5870 end if;
5871 end loop;
5873 Top := Par;
5875 -- The node may be located in a pragma in which case return the
5876 -- pragma itself:
5878 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5880 -- Similar case occurs when the node is related to an object
5881 -- declaration or assignment:
5883 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5885 -- Another case to consider is when the node is part of a return
5886 -- statement:
5888 -- return ... and then Ctrl_Func_Call ...;
5890 -- Another case is when the node acts as a formal in a procedure
5891 -- call statement:
5893 -- Proc (... and then Ctrl_Func_Call ...);
5895 if Scope_Is_Transient then
5896 Wrapped_Node := Node_To_Be_Wrapped;
5897 else
5898 Wrapped_Node := Empty;
5899 end if;
5901 while Present (Par) loop
5902 if Par = Wrapped_Node
5903 or else Nkind_In (Par, N_Assignment_Statement,
5904 N_Object_Declaration,
5905 N_Pragma,
5906 N_Procedure_Call_Statement,
5907 N_Simple_Return_Statement)
5908 then
5909 return Par;
5911 -- Prevent the search from going too far
5913 elsif Is_Body_Or_Package_Declaration (Par) then
5914 exit;
5915 end if;
5917 Par := Parent (Par);
5918 end loop;
5920 -- Return the topmost short circuit operator
5922 return Top;
5923 end if;
5924 end Find_Hook_Context;
5926 ------------------------------
5927 -- Following_Address_Clause --
5928 ------------------------------
5930 function Following_Address_Clause (D : Node_Id) return Node_Id is
5931 Id : constant Entity_Id := Defining_Identifier (D);
5932 Result : Node_Id;
5933 Par : Node_Id;
5935 function Check_Decls (D : Node_Id) return Node_Id;
5936 -- This internal function differs from the main function in that it
5937 -- gets called to deal with a following package private part, and
5938 -- it checks declarations starting with D (the main function checks
5939 -- declarations following D). If D is Empty, then Empty is returned.
5941 -----------------
5942 -- Check_Decls --
5943 -----------------
5945 function Check_Decls (D : Node_Id) return Node_Id is
5946 Decl : Node_Id;
5948 begin
5949 Decl := D;
5950 while Present (Decl) loop
5951 if Nkind (Decl) = N_At_Clause
5952 and then Chars (Identifier (Decl)) = Chars (Id)
5953 then
5954 return Decl;
5956 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5957 and then Chars (Decl) = Name_Address
5958 and then Chars (Name (Decl)) = Chars (Id)
5959 then
5960 return Decl;
5961 end if;
5963 Next (Decl);
5964 end loop;
5966 -- Otherwise not found, return Empty
5968 return Empty;
5969 end Check_Decls;
5971 -- Start of processing for Following_Address_Clause
5973 begin
5974 -- If parser detected no address clause for the identifier in question,
5975 -- then the answer is a quick NO, without the need for a search.
5977 if not Get_Name_Table_Boolean1 (Chars (Id)) then
5978 return Empty;
5979 end if;
5981 -- Otherwise search current declarative unit
5983 Result := Check_Decls (Next (D));
5985 if Present (Result) then
5986 return Result;
5987 end if;
5989 -- Check for possible package private part following
5991 Par := Parent (D);
5993 if Nkind (Par) = N_Package_Specification
5994 and then Visible_Declarations (Par) = List_Containing (D)
5995 and then Present (Private_Declarations (Par))
5996 then
5997 -- Private part present, check declarations there
5999 return Check_Decls (First (Private_Declarations (Par)));
6001 else
6002 -- No private part, clause not found, return Empty
6004 return Empty;
6005 end if;
6006 end Following_Address_Clause;
6008 ----------------------
6009 -- Force_Evaluation --
6010 ----------------------
6012 procedure Force_Evaluation
6013 (Exp : Node_Id;
6014 Name_Req : Boolean := False;
6015 Related_Id : Entity_Id := Empty;
6016 Is_Low_Bound : Boolean := False;
6017 Is_High_Bound : Boolean := False;
6018 Mode : Force_Evaluation_Mode := Relaxed)
6020 begin
6021 Remove_Side_Effects
6022 (Exp => Exp,
6023 Name_Req => Name_Req,
6024 Variable_Ref => True,
6025 Renaming_Req => False,
6026 Related_Id => Related_Id,
6027 Is_Low_Bound => Is_Low_Bound,
6028 Is_High_Bound => Is_High_Bound,
6029 Check_Side_Effects =>
6030 Is_Static_Expression (Exp)
6031 or else Mode = Relaxed);
6032 end Force_Evaluation;
6034 ---------------------------------
6035 -- Fully_Qualified_Name_String --
6036 ---------------------------------
6038 function Fully_Qualified_Name_String
6039 (E : Entity_Id;
6040 Append_NUL : Boolean := True) return String_Id
6042 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6043 -- Compute recursively the qualified name without NUL at the end, adding
6044 -- it to the currently started string being generated
6046 ----------------------------------
6047 -- Internal_Full_Qualified_Name --
6048 ----------------------------------
6050 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6051 Ent : Entity_Id;
6053 begin
6054 -- Deal properly with child units
6056 if Nkind (E) = N_Defining_Program_Unit_Name then
6057 Ent := Defining_Identifier (E);
6058 else
6059 Ent := E;
6060 end if;
6062 -- Compute qualification recursively (only "Standard" has no scope)
6064 if Present (Scope (Scope (Ent))) then
6065 Internal_Full_Qualified_Name (Scope (Ent));
6066 Store_String_Char (Get_Char_Code ('.'));
6067 end if;
6069 -- Every entity should have a name except some expanded blocks
6070 -- don't bother about those.
6072 if Chars (Ent) = No_Name then
6073 return;
6074 end if;
6076 -- Generates the entity name in upper case
6078 Get_Decoded_Name_String (Chars (Ent));
6079 Set_All_Upper_Case;
6080 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6081 return;
6082 end Internal_Full_Qualified_Name;
6084 -- Start of processing for Full_Qualified_Name
6086 begin
6087 Start_String;
6088 Internal_Full_Qualified_Name (E);
6090 if Append_NUL then
6091 Store_String_Char (Get_Char_Code (ASCII.NUL));
6092 end if;
6094 return End_String;
6095 end Fully_Qualified_Name_String;
6097 ------------------------
6098 -- Generate_Poll_Call --
6099 ------------------------
6101 procedure Generate_Poll_Call (N : Node_Id) is
6102 begin
6103 -- No poll call if polling not active
6105 if not Polling_Required then
6106 return;
6108 -- Otherwise generate require poll call
6110 else
6111 Insert_Before_And_Analyze (N,
6112 Make_Procedure_Call_Statement (Sloc (N),
6113 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6114 end if;
6115 end Generate_Poll_Call;
6117 ---------------------------------
6118 -- Get_Current_Value_Condition --
6119 ---------------------------------
6121 -- Note: the implementation of this procedure is very closely tied to the
6122 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6123 -- interpret Current_Value fields set by the Set procedure, so the two
6124 -- procedures need to be closely coordinated.
6126 procedure Get_Current_Value_Condition
6127 (Var : Node_Id;
6128 Op : out Node_Kind;
6129 Val : out Node_Id)
6131 Loc : constant Source_Ptr := Sloc (Var);
6132 Ent : constant Entity_Id := Entity (Var);
6134 procedure Process_Current_Value_Condition
6135 (N : Node_Id;
6136 S : Boolean);
6137 -- N is an expression which holds either True (S = True) or False (S =
6138 -- False) in the condition. This procedure digs out the expression and
6139 -- if it refers to Ent, sets Op and Val appropriately.
6141 -------------------------------------
6142 -- Process_Current_Value_Condition --
6143 -------------------------------------
6145 procedure Process_Current_Value_Condition
6146 (N : Node_Id;
6147 S : Boolean)
6149 Cond : Node_Id;
6150 Prev_Cond : Node_Id;
6151 Sens : Boolean;
6153 begin
6154 Cond := N;
6155 Sens := S;
6157 loop
6158 Prev_Cond := Cond;
6160 -- Deal with NOT operators, inverting sense
6162 while Nkind (Cond) = N_Op_Not loop
6163 Cond := Right_Opnd (Cond);
6164 Sens := not Sens;
6165 end loop;
6167 -- Deal with conversions, qualifications, and expressions with
6168 -- actions.
6170 while Nkind_In (Cond,
6171 N_Type_Conversion,
6172 N_Qualified_Expression,
6173 N_Expression_With_Actions)
6174 loop
6175 Cond := Expression (Cond);
6176 end loop;
6178 exit when Cond = Prev_Cond;
6179 end loop;
6181 -- Deal with AND THEN and AND cases
6183 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6185 -- Don't ever try to invert a condition that is of the form of an
6186 -- AND or AND THEN (since we are not doing sufficiently general
6187 -- processing to allow this).
6189 if Sens = False then
6190 Op := N_Empty;
6191 Val := Empty;
6192 return;
6193 end if;
6195 -- Recursively process AND and AND THEN branches
6197 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6199 if Op /= N_Empty then
6200 return;
6201 end if;
6203 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6204 return;
6206 -- Case of relational operator
6208 elsif Nkind (Cond) in N_Op_Compare then
6209 Op := Nkind (Cond);
6211 -- Invert sense of test if inverted test
6213 if Sens = False then
6214 case Op is
6215 when N_Op_Eq => Op := N_Op_Ne;
6216 when N_Op_Ne => Op := N_Op_Eq;
6217 when N_Op_Lt => Op := N_Op_Ge;
6218 when N_Op_Gt => Op := N_Op_Le;
6219 when N_Op_Le => Op := N_Op_Gt;
6220 when N_Op_Ge => Op := N_Op_Lt;
6221 when others => raise Program_Error;
6222 end case;
6223 end if;
6225 -- Case of entity op value
6227 if Is_Entity_Name (Left_Opnd (Cond))
6228 and then Ent = Entity (Left_Opnd (Cond))
6229 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6230 then
6231 Val := Right_Opnd (Cond);
6233 -- Case of value op entity
6235 elsif Is_Entity_Name (Right_Opnd (Cond))
6236 and then Ent = Entity (Right_Opnd (Cond))
6237 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6238 then
6239 Val := Left_Opnd (Cond);
6241 -- We are effectively swapping operands
6243 case Op is
6244 when N_Op_Eq => null;
6245 when N_Op_Ne => null;
6246 when N_Op_Lt => Op := N_Op_Gt;
6247 when N_Op_Gt => Op := N_Op_Lt;
6248 when N_Op_Le => Op := N_Op_Ge;
6249 when N_Op_Ge => Op := N_Op_Le;
6250 when others => raise Program_Error;
6251 end case;
6253 else
6254 Op := N_Empty;
6255 end if;
6257 return;
6259 elsif Nkind_In (Cond,
6260 N_Type_Conversion,
6261 N_Qualified_Expression,
6262 N_Expression_With_Actions)
6263 then
6264 Cond := Expression (Cond);
6266 -- Case of Boolean variable reference, return as though the
6267 -- reference had said var = True.
6269 else
6270 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6271 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6273 if Sens = False then
6274 Op := N_Op_Ne;
6275 else
6276 Op := N_Op_Eq;
6277 end if;
6278 end if;
6279 end if;
6280 end Process_Current_Value_Condition;
6282 -- Start of processing for Get_Current_Value_Condition
6284 begin
6285 Op := N_Empty;
6286 Val := Empty;
6288 -- Immediate return, nothing doing, if this is not an object
6290 if Ekind (Ent) not in Object_Kind then
6291 return;
6292 end if;
6294 -- Otherwise examine current value
6296 declare
6297 CV : constant Node_Id := Current_Value (Ent);
6298 Sens : Boolean;
6299 Stm : Node_Id;
6301 begin
6302 -- If statement. Condition is known true in THEN section, known False
6303 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6305 if Nkind (CV) = N_If_Statement then
6307 -- Before start of IF statement
6309 if Loc < Sloc (CV) then
6310 return;
6312 -- After end of IF statement
6314 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6315 return;
6316 end if;
6318 -- At this stage we know that we are within the IF statement, but
6319 -- unfortunately, the tree does not record the SLOC of the ELSE so
6320 -- we cannot use a simple SLOC comparison to distinguish between
6321 -- the then/else statements, so we have to climb the tree.
6323 declare
6324 N : Node_Id;
6326 begin
6327 N := Parent (Var);
6328 while Parent (N) /= CV loop
6329 N := Parent (N);
6331 -- If we fall off the top of the tree, then that's odd, but
6332 -- perhaps it could occur in some error situation, and the
6333 -- safest response is simply to assume that the outcome of
6334 -- the condition is unknown. No point in bombing during an
6335 -- attempt to optimize things.
6337 if No (N) then
6338 return;
6339 end if;
6340 end loop;
6342 -- Now we have N pointing to a node whose parent is the IF
6343 -- statement in question, so now we can tell if we are within
6344 -- the THEN statements.
6346 if Is_List_Member (N)
6347 and then List_Containing (N) = Then_Statements (CV)
6348 then
6349 Sens := True;
6351 -- If the variable reference does not come from source, we
6352 -- cannot reliably tell whether it appears in the else part.
6353 -- In particular, if it appears in generated code for a node
6354 -- that requires finalization, it may be attached to a list
6355 -- that has not been yet inserted into the code. For now,
6356 -- treat it as unknown.
6358 elsif not Comes_From_Source (N) then
6359 return;
6361 -- Otherwise we must be in ELSIF or ELSE part
6363 else
6364 Sens := False;
6365 end if;
6366 end;
6368 -- ELSIF part. Condition is known true within the referenced
6369 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6370 -- and unknown before the ELSE part or after the IF statement.
6372 elsif Nkind (CV) = N_Elsif_Part then
6374 -- if the Elsif_Part had condition_actions, the elsif has been
6375 -- rewritten as a nested if, and the original elsif_part is
6376 -- detached from the tree, so there is no way to obtain useful
6377 -- information on the current value of the variable.
6378 -- Can this be improved ???
6380 if No (Parent (CV)) then
6381 return;
6382 end if;
6384 Stm := Parent (CV);
6386 -- If the tree has been otherwise rewritten there is nothing
6387 -- else to be done either.
6389 if Nkind (Stm) /= N_If_Statement then
6390 return;
6391 end if;
6393 -- Before start of ELSIF part
6395 if Loc < Sloc (CV) then
6396 return;
6398 -- After end of IF statement
6400 elsif Loc >= Sloc (Stm) +
6401 Text_Ptr (UI_To_Int (End_Span (Stm)))
6402 then
6403 return;
6404 end if;
6406 -- Again we lack the SLOC of the ELSE, so we need to climb the
6407 -- tree to see if we are within the ELSIF part in question.
6409 declare
6410 N : Node_Id;
6412 begin
6413 N := Parent (Var);
6414 while Parent (N) /= Stm loop
6415 N := Parent (N);
6417 -- If we fall off the top of the tree, then that's odd, but
6418 -- perhaps it could occur in some error situation, and the
6419 -- safest response is simply to assume that the outcome of
6420 -- the condition is unknown. No point in bombing during an
6421 -- attempt to optimize things.
6423 if No (N) then
6424 return;
6425 end if;
6426 end loop;
6428 -- Now we have N pointing to a node whose parent is the IF
6429 -- statement in question, so see if is the ELSIF part we want.
6430 -- the THEN statements.
6432 if N = CV then
6433 Sens := True;
6435 -- Otherwise we must be in subsequent ELSIF or ELSE part
6437 else
6438 Sens := False;
6439 end if;
6440 end;
6442 -- Iteration scheme of while loop. The condition is known to be
6443 -- true within the body of the loop.
6445 elsif Nkind (CV) = N_Iteration_Scheme then
6446 declare
6447 Loop_Stmt : constant Node_Id := Parent (CV);
6449 begin
6450 -- Before start of body of loop
6452 if Loc < Sloc (Loop_Stmt) then
6453 return;
6455 -- After end of LOOP statement
6457 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6458 return;
6460 -- We are within the body of the loop
6462 else
6463 Sens := True;
6464 end if;
6465 end;
6467 -- All other cases of Current_Value settings
6469 else
6470 return;
6471 end if;
6473 -- If we fall through here, then we have a reportable condition, Sens
6474 -- is True if the condition is true and False if it needs inverting.
6476 Process_Current_Value_Condition (Condition (CV), Sens);
6477 end;
6478 end Get_Current_Value_Condition;
6480 ---------------------
6481 -- Get_Stream_Size --
6482 ---------------------
6484 function Get_Stream_Size (E : Entity_Id) return Uint is
6485 begin
6486 -- If we have a Stream_Size clause for this type use it
6488 if Has_Stream_Size_Clause (E) then
6489 return Static_Integer (Expression (Stream_Size_Clause (E)));
6491 -- Otherwise the Stream_Size if the size of the type
6493 else
6494 return Esize (E);
6495 end if;
6496 end Get_Stream_Size;
6498 ---------------------------
6499 -- Has_Access_Constraint --
6500 ---------------------------
6502 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6503 Disc : Entity_Id;
6504 T : constant Entity_Id := Etype (E);
6506 begin
6507 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6508 Disc := First_Discriminant (T);
6509 while Present (Disc) loop
6510 if Is_Access_Type (Etype (Disc)) then
6511 return True;
6512 end if;
6514 Next_Discriminant (Disc);
6515 end loop;
6517 return False;
6518 else
6519 return False;
6520 end if;
6521 end Has_Access_Constraint;
6523 -----------------------------------------------------
6524 -- Has_Annotate_Pragma_For_External_Axiomatization --
6525 -----------------------------------------------------
6527 function Has_Annotate_Pragma_For_External_Axiomatization
6528 (E : Entity_Id) return Boolean
6530 function Is_Annotate_Pragma_For_External_Axiomatization
6531 (N : Node_Id) return Boolean;
6532 -- Returns whether N is
6533 -- pragma Annotate (GNATprove, External_Axiomatization);
6535 ----------------------------------------------------
6536 -- Is_Annotate_Pragma_For_External_Axiomatization --
6537 ----------------------------------------------------
6539 -- The general form of pragma Annotate is
6541 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6542 -- ARG ::= NAME | EXPRESSION
6544 -- The first two arguments are by convention intended to refer to an
6545 -- external tool and a tool-specific function. These arguments are
6546 -- not analyzed.
6548 -- The following is used to annotate a package specification which
6549 -- GNATprove should treat specially, because the axiomatization of
6550 -- this unit is given by the user instead of being automatically
6551 -- generated.
6553 -- pragma Annotate (GNATprove, External_Axiomatization);
6555 function Is_Annotate_Pragma_For_External_Axiomatization
6556 (N : Node_Id) return Boolean
6558 Name_GNATprove : constant String :=
6559 "gnatprove";
6560 Name_External_Axiomatization : constant String :=
6561 "external_axiomatization";
6562 -- Special names
6564 begin
6565 if Nkind (N) = N_Pragma
6566 and then Get_Pragma_Id (N) = Pragma_Annotate
6567 and then List_Length (Pragma_Argument_Associations (N)) = 2
6568 then
6569 declare
6570 Arg1 : constant Node_Id :=
6571 First (Pragma_Argument_Associations (N));
6572 Arg2 : constant Node_Id := Next (Arg1);
6573 Nam1 : Name_Id;
6574 Nam2 : Name_Id;
6576 begin
6577 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6578 -- Name_External_Axiomatization so that Name_Find returns the
6579 -- corresponding name. This takes care of all possible casings.
6581 Name_Len := 0;
6582 Add_Str_To_Name_Buffer (Name_GNATprove);
6583 Nam1 := Name_Find;
6585 Name_Len := 0;
6586 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6587 Nam2 := Name_Find;
6589 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6590 and then
6591 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6592 end;
6594 else
6595 return False;
6596 end if;
6597 end Is_Annotate_Pragma_For_External_Axiomatization;
6599 -- Local variables
6601 Decl : Node_Id;
6602 Vis_Decls : List_Id;
6603 N : Node_Id;
6605 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6607 begin
6608 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6609 Decl := Parent (Parent (E));
6610 else
6611 Decl := Parent (E);
6612 end if;
6614 Vis_Decls := Visible_Declarations (Decl);
6616 N := First (Vis_Decls);
6617 while Present (N) loop
6619 -- Skip declarations generated by the frontend. Skip all pragmas
6620 -- that are not the desired Annotate pragma. Stop the search on
6621 -- the first non-pragma source declaration.
6623 if Comes_From_Source (N) then
6624 if Nkind (N) = N_Pragma then
6625 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6626 return True;
6627 end if;
6628 else
6629 return False;
6630 end if;
6631 end if;
6633 Next (N);
6634 end loop;
6636 return False;
6637 end Has_Annotate_Pragma_For_External_Axiomatization;
6639 --------------------
6640 -- Homonym_Number --
6641 --------------------
6643 function Homonym_Number (Subp : Entity_Id) return Nat is
6644 Count : Nat;
6645 Hom : Entity_Id;
6647 begin
6648 Count := 1;
6649 Hom := Homonym (Subp);
6650 while Present (Hom) loop
6651 if Scope (Hom) = Scope (Subp) then
6652 Count := Count + 1;
6653 end if;
6655 Hom := Homonym (Hom);
6656 end loop;
6658 return Count;
6659 end Homonym_Number;
6661 -----------------------------------
6662 -- In_Library_Level_Package_Body --
6663 -----------------------------------
6665 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6666 begin
6667 -- First determine whether the entity appears at the library level, then
6668 -- look at the containing unit.
6670 if Is_Library_Level_Entity (Id) then
6671 declare
6672 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6674 begin
6675 return Nkind (Unit (Container)) = N_Package_Body;
6676 end;
6677 end if;
6679 return False;
6680 end In_Library_Level_Package_Body;
6682 ------------------------------
6683 -- In_Unconditional_Context --
6684 ------------------------------
6686 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6687 P : Node_Id;
6689 begin
6690 P := Node;
6691 while Present (P) loop
6692 case Nkind (P) is
6693 when N_Subprogram_Body => return True;
6694 when N_If_Statement => return False;
6695 when N_Loop_Statement => return False;
6696 when N_Case_Statement => return False;
6697 when others => P := Parent (P);
6698 end case;
6699 end loop;
6701 return False;
6702 end In_Unconditional_Context;
6704 -------------------
6705 -- Insert_Action --
6706 -------------------
6708 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6709 begin
6710 if Present (Ins_Action) then
6711 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6712 end if;
6713 end Insert_Action;
6715 -- Version with check(s) suppressed
6717 procedure Insert_Action
6718 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6720 begin
6721 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6722 end Insert_Action;
6724 -------------------------
6725 -- Insert_Action_After --
6726 -------------------------
6728 procedure Insert_Action_After
6729 (Assoc_Node : Node_Id;
6730 Ins_Action : Node_Id)
6732 begin
6733 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6734 end Insert_Action_After;
6736 --------------------
6737 -- Insert_Actions --
6738 --------------------
6740 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6741 N : Node_Id;
6742 P : Node_Id;
6744 Wrapped_Node : Node_Id := Empty;
6746 begin
6747 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6748 return;
6749 end if;
6751 -- Ignore insert of actions from inside default expression (or other
6752 -- similar "spec expression") in the special spec-expression analyze
6753 -- mode. Any insertions at this point have no relevance, since we are
6754 -- only doing the analyze to freeze the types of any static expressions.
6755 -- See section "Handling of Default Expressions" in the spec of package
6756 -- Sem for further details.
6758 if In_Spec_Expression then
6759 return;
6760 end if;
6762 -- If the action derives from stuff inside a record, then the actions
6763 -- are attached to the current scope, to be inserted and analyzed on
6764 -- exit from the scope. The reason for this is that we may also be
6765 -- generating freeze actions at the same time, and they must eventually
6766 -- be elaborated in the correct order.
6768 if Is_Record_Type (Current_Scope)
6769 and then not Is_Frozen (Current_Scope)
6770 then
6771 if No (Scope_Stack.Table
6772 (Scope_Stack.Last).Pending_Freeze_Actions)
6773 then
6774 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6775 Ins_Actions;
6776 else
6777 Append_List
6778 (Ins_Actions,
6779 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6780 end if;
6782 return;
6783 end if;
6785 -- We now intend to climb up the tree to find the right point to
6786 -- insert the actions. We start at Assoc_Node, unless this node is a
6787 -- subexpression in which case we start with its parent. We do this for
6788 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6789 -- itself one of the special nodes like N_And_Then, then we assume that
6790 -- an initial request to insert actions for such a node does not expect
6791 -- the actions to get deposited in the node for later handling when the
6792 -- node is expanded, since clearly the node is being dealt with by the
6793 -- caller. Note that in the subexpression case, N is always the child we
6794 -- came from.
6796 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6797 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6798 -- Procedure calls, and similarly procedure attribute references, are
6799 -- also statements.
6801 if Nkind (Assoc_Node) in N_Subexpr
6802 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6803 or else Etype (Assoc_Node) /= Standard_Void_Type)
6804 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6805 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6806 or else not Is_Procedure_Attribute_Name
6807 (Attribute_Name (Assoc_Node)))
6808 then
6809 N := Assoc_Node;
6810 P := Parent (Assoc_Node);
6812 -- Non-subexpression case. Note that N is initially Empty in this case
6813 -- (N is only guaranteed Non-Empty in the subexpr case).
6815 else
6816 N := Empty;
6817 P := Assoc_Node;
6818 end if;
6820 -- Capture root of the transient scope
6822 if Scope_Is_Transient then
6823 Wrapped_Node := Node_To_Be_Wrapped;
6824 end if;
6826 loop
6827 pragma Assert (Present (P));
6829 -- Make sure that inserted actions stay in the transient scope
6831 if Present (Wrapped_Node) and then N = Wrapped_Node then
6832 Store_Before_Actions_In_Scope (Ins_Actions);
6833 return;
6834 end if;
6836 case Nkind (P) is
6838 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6839 -- in the Actions field of the right operand. They will be moved
6840 -- out further when the AND THEN or OR ELSE operator is expanded.
6841 -- Nothing special needs to be done for the left operand since
6842 -- in that case the actions are executed unconditionally.
6844 when N_Short_Circuit =>
6845 if N = Right_Opnd (P) then
6847 -- We are now going to either append the actions to the
6848 -- actions field of the short-circuit operation. We will
6849 -- also analyze the actions now.
6851 -- This analysis is really too early, the proper thing would
6852 -- be to just park them there now, and only analyze them if
6853 -- we find we really need them, and to it at the proper
6854 -- final insertion point. However attempting to this proved
6855 -- tricky, so for now we just kill current values before and
6856 -- after the analyze call to make sure we avoid peculiar
6857 -- optimizations from this out of order insertion.
6859 Kill_Current_Values;
6861 -- If P has already been expanded, we can't park new actions
6862 -- on it, so we need to expand them immediately, introducing
6863 -- an Expression_With_Actions. N can't be an expression
6864 -- with actions, or else then the actions would have been
6865 -- inserted at an inner level.
6867 if Analyzed (P) then
6868 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6869 Rewrite (N,
6870 Make_Expression_With_Actions (Sloc (N),
6871 Actions => Ins_Actions,
6872 Expression => Relocate_Node (N)));
6873 Analyze_And_Resolve (N);
6875 elsif Present (Actions (P)) then
6876 Insert_List_After_And_Analyze
6877 (Last (Actions (P)), Ins_Actions);
6878 else
6879 Set_Actions (P, Ins_Actions);
6880 Analyze_List (Actions (P));
6881 end if;
6883 Kill_Current_Values;
6885 return;
6886 end if;
6888 -- Then or Else dependent expression of an if expression. Add
6889 -- actions to Then_Actions or Else_Actions field as appropriate.
6890 -- The actions will be moved further out when the if is expanded.
6892 when N_If_Expression =>
6893 declare
6894 ThenX : constant Node_Id := Next (First (Expressions (P)));
6895 ElseX : constant Node_Id := Next (ThenX);
6897 begin
6898 -- If the enclosing expression is already analyzed, as
6899 -- is the case for nested elaboration checks, insert the
6900 -- conditional further out.
6902 if Analyzed (P) then
6903 null;
6905 -- Actions belong to the then expression, temporarily place
6906 -- them as Then_Actions of the if expression. They will be
6907 -- moved to the proper place later when the if expression
6908 -- is expanded.
6910 elsif N = ThenX then
6911 if Present (Then_Actions (P)) then
6912 Insert_List_After_And_Analyze
6913 (Last (Then_Actions (P)), Ins_Actions);
6914 else
6915 Set_Then_Actions (P, Ins_Actions);
6916 Analyze_List (Then_Actions (P));
6917 end if;
6919 return;
6921 -- Actions belong to the else expression, temporarily place
6922 -- them as Else_Actions of the if expression. They will be
6923 -- moved to the proper place later when the if expression
6924 -- is expanded.
6926 elsif N = ElseX then
6927 if Present (Else_Actions (P)) then
6928 Insert_List_After_And_Analyze
6929 (Last (Else_Actions (P)), Ins_Actions);
6930 else
6931 Set_Else_Actions (P, Ins_Actions);
6932 Analyze_List (Else_Actions (P));
6933 end if;
6935 return;
6937 -- Actions belong to the condition. In this case they are
6938 -- unconditionally executed, and so we can continue the
6939 -- search for the proper insert point.
6941 else
6942 null;
6943 end if;
6944 end;
6946 -- Alternative of case expression, we place the action in the
6947 -- Actions field of the case expression alternative, this will
6948 -- be handled when the case expression is expanded.
6950 when N_Case_Expression_Alternative =>
6951 if Present (Actions (P)) then
6952 Insert_List_After_And_Analyze
6953 (Last (Actions (P)), Ins_Actions);
6954 else
6955 Set_Actions (P, Ins_Actions);
6956 Analyze_List (Actions (P));
6957 end if;
6959 return;
6961 -- Case of appearing within an Expressions_With_Actions node. When
6962 -- the new actions come from the expression of the expression with
6963 -- actions, they must be added to the existing actions. The other
6964 -- alternative is when the new actions are related to one of the
6965 -- existing actions of the expression with actions, and should
6966 -- never reach here: if actions are inserted on a statement
6967 -- within the Actions of an expression with actions, or on some
6968 -- subexpression of such a statement, then the outermost proper
6969 -- insertion point is right before the statement, and we should
6970 -- never climb up as far as the N_Expression_With_Actions itself.
6972 when N_Expression_With_Actions =>
6973 if N = Expression (P) then
6974 if Is_Empty_List (Actions (P)) then
6975 Append_List_To (Actions (P), Ins_Actions);
6976 Analyze_List (Actions (P));
6977 else
6978 Insert_List_After_And_Analyze
6979 (Last (Actions (P)), Ins_Actions);
6980 end if;
6982 return;
6984 else
6985 raise Program_Error;
6986 end if;
6988 -- Case of appearing in the condition of a while expression or
6989 -- elsif. We insert the actions into the Condition_Actions field.
6990 -- They will be moved further out when the while loop or elsif
6991 -- is analyzed.
6993 when N_Elsif_Part
6994 | N_Iteration_Scheme
6996 if N = Condition (P) then
6997 if Present (Condition_Actions (P)) then
6998 Insert_List_After_And_Analyze
6999 (Last (Condition_Actions (P)), Ins_Actions);
7000 else
7001 Set_Condition_Actions (P, Ins_Actions);
7003 -- Set the parent of the insert actions explicitly. This
7004 -- is not a syntactic field, but we need the parent field
7005 -- set, in particular so that freeze can understand that
7006 -- it is dealing with condition actions, and properly
7007 -- insert the freezing actions.
7009 Set_Parent (Ins_Actions, P);
7010 Analyze_List (Condition_Actions (P));
7011 end if;
7013 return;
7014 end if;
7016 -- Statements, declarations, pragmas, representation clauses
7018 when
7019 -- Statements
7021 N_Procedure_Call_Statement
7022 | N_Statement_Other_Than_Procedure_Call
7024 -- Pragmas
7026 | N_Pragma
7028 -- Representation_Clause
7030 | N_At_Clause
7031 | N_Attribute_Definition_Clause
7032 | N_Enumeration_Representation_Clause
7033 | N_Record_Representation_Clause
7035 -- Declarations
7037 | N_Abstract_Subprogram_Declaration
7038 | N_Entry_Body
7039 | N_Exception_Declaration
7040 | N_Exception_Renaming_Declaration
7041 | N_Expression_Function
7042 | N_Formal_Abstract_Subprogram_Declaration
7043 | N_Formal_Concrete_Subprogram_Declaration
7044 | N_Formal_Object_Declaration
7045 | N_Formal_Type_Declaration
7046 | N_Full_Type_Declaration
7047 | N_Function_Instantiation
7048 | N_Generic_Function_Renaming_Declaration
7049 | N_Generic_Package_Declaration
7050 | N_Generic_Package_Renaming_Declaration
7051 | N_Generic_Procedure_Renaming_Declaration
7052 | N_Generic_Subprogram_Declaration
7053 | N_Implicit_Label_Declaration
7054 | N_Incomplete_Type_Declaration
7055 | N_Number_Declaration
7056 | N_Object_Declaration
7057 | N_Object_Renaming_Declaration
7058 | N_Package_Body
7059 | N_Package_Body_Stub
7060 | N_Package_Declaration
7061 | N_Package_Instantiation
7062 | N_Package_Renaming_Declaration
7063 | N_Private_Extension_Declaration
7064 | N_Private_Type_Declaration
7065 | N_Procedure_Instantiation
7066 | N_Protected_Body
7067 | N_Protected_Body_Stub
7068 | N_Protected_Type_Declaration
7069 | N_Single_Task_Declaration
7070 | N_Subprogram_Body
7071 | N_Subprogram_Body_Stub
7072 | N_Subprogram_Declaration
7073 | N_Subprogram_Renaming_Declaration
7074 | N_Subtype_Declaration
7075 | N_Task_Body
7076 | N_Task_Body_Stub
7077 | N_Task_Type_Declaration
7079 -- Use clauses can appear in lists of declarations
7081 | N_Use_Package_Clause
7082 | N_Use_Type_Clause
7084 -- Freeze entity behaves like a declaration or statement
7086 | N_Freeze_Entity
7087 | N_Freeze_Generic_Entity
7089 -- Do not insert here if the item is not a list member (this
7090 -- happens for example with a triggering statement, and the
7091 -- proper approach is to insert before the entire select).
7093 if not Is_List_Member (P) then
7094 null;
7096 -- Do not insert if parent of P is an N_Component_Association
7097 -- node (i.e. we are in the context of an N_Aggregate or
7098 -- N_Extension_Aggregate node. In this case we want to insert
7099 -- before the entire aggregate.
7101 elsif Nkind (Parent (P)) = N_Component_Association then
7102 null;
7104 -- Do not insert if the parent of P is either an N_Variant node
7105 -- or an N_Record_Definition node, meaning in either case that
7106 -- P is a member of a component list, and that therefore the
7107 -- actions should be inserted outside the complete record
7108 -- declaration.
7110 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7111 null;
7113 -- Do not insert freeze nodes within the loop generated for
7114 -- an aggregate, because they may be elaborated too late for
7115 -- subsequent use in the back end: within a package spec the
7116 -- loop is part of the elaboration procedure and is only
7117 -- elaborated during the second pass.
7119 -- If the loop comes from source, or the entity is local to the
7120 -- loop itself it must remain within.
7122 elsif Nkind (Parent (P)) = N_Loop_Statement
7123 and then not Comes_From_Source (Parent (P))
7124 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7125 and then
7126 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7127 then
7128 null;
7130 -- Otherwise we can go ahead and do the insertion
7132 elsif P = Wrapped_Node then
7133 Store_Before_Actions_In_Scope (Ins_Actions);
7134 return;
7136 else
7137 Insert_List_Before_And_Analyze (P, Ins_Actions);
7138 return;
7139 end if;
7141 -- A special case, N_Raise_xxx_Error can act either as a statement
7142 -- or a subexpression. We tell the difference by looking at the
7143 -- Etype. It is set to Standard_Void_Type in the statement case.
7145 when N_Raise_xxx_Error =>
7146 if Etype (P) = Standard_Void_Type then
7147 if P = Wrapped_Node then
7148 Store_Before_Actions_In_Scope (Ins_Actions);
7149 else
7150 Insert_List_Before_And_Analyze (P, Ins_Actions);
7151 end if;
7153 return;
7155 -- In the subexpression case, keep climbing
7157 else
7158 null;
7159 end if;
7161 -- If a component association appears within a loop created for
7162 -- an array aggregate, attach the actions to the association so
7163 -- they can be subsequently inserted within the loop. For other
7164 -- component associations insert outside of the aggregate. For
7165 -- an association that will generate a loop, its Loop_Actions
7166 -- attribute is already initialized (see exp_aggr.adb).
7168 -- The list of Loop_Actions can in turn generate additional ones,
7169 -- that are inserted before the associated node. If the associated
7170 -- node is outside the aggregate, the new actions are collected
7171 -- at the end of the Loop_Actions, to respect the order in which
7172 -- they are to be elaborated.
7174 when N_Component_Association
7175 | N_Iterated_Component_Association
7177 if Nkind (Parent (P)) = N_Aggregate
7178 and then Present (Loop_Actions (P))
7179 then
7180 if Is_Empty_List (Loop_Actions (P)) then
7181 Set_Loop_Actions (P, Ins_Actions);
7182 Analyze_List (Ins_Actions);
7183 else
7184 declare
7185 Decl : Node_Id;
7187 begin
7188 -- Check whether these actions were generated by a
7189 -- declaration that is part of the Loop_Actions for
7190 -- the component_association.
7192 Decl := Assoc_Node;
7193 while Present (Decl) loop
7194 exit when Parent (Decl) = P
7195 and then Is_List_Member (Decl)
7196 and then
7197 List_Containing (Decl) = Loop_Actions (P);
7198 Decl := Parent (Decl);
7199 end loop;
7201 if Present (Decl) then
7202 Insert_List_Before_And_Analyze
7203 (Decl, Ins_Actions);
7204 else
7205 Insert_List_After_And_Analyze
7206 (Last (Loop_Actions (P)), Ins_Actions);
7207 end if;
7208 end;
7209 end if;
7211 return;
7213 else
7214 null;
7215 end if;
7217 -- Special case: an attribute denoting a procedure call
7219 when N_Attribute_Reference =>
7220 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7221 if P = Wrapped_Node then
7222 Store_Before_Actions_In_Scope (Ins_Actions);
7223 else
7224 Insert_List_Before_And_Analyze (P, Ins_Actions);
7225 end if;
7227 return;
7229 -- In the subexpression case, keep climbing
7231 else
7232 null;
7233 end if;
7235 -- Special case: a marker
7237 when N_Call_Marker
7238 | N_Variable_Reference_Marker
7240 if Is_List_Member (P) then
7241 Insert_List_Before_And_Analyze (P, Ins_Actions);
7242 return;
7243 end if;
7245 -- A contract node should not belong to the tree
7247 when N_Contract =>
7248 raise Program_Error;
7250 -- For all other node types, keep climbing tree
7252 when N_Abortable_Part
7253 | N_Accept_Alternative
7254 | N_Access_Definition
7255 | N_Access_Function_Definition
7256 | N_Access_Procedure_Definition
7257 | N_Access_To_Object_Definition
7258 | N_Aggregate
7259 | N_Allocator
7260 | N_Aspect_Specification
7261 | N_Case_Expression
7262 | N_Case_Statement_Alternative
7263 | N_Character_Literal
7264 | N_Compilation_Unit
7265 | N_Compilation_Unit_Aux
7266 | N_Component_Clause
7267 | N_Component_Declaration
7268 | N_Component_Definition
7269 | N_Component_List
7270 | N_Constrained_Array_Definition
7271 | N_Decimal_Fixed_Point_Definition
7272 | N_Defining_Character_Literal
7273 | N_Defining_Identifier
7274 | N_Defining_Operator_Symbol
7275 | N_Defining_Program_Unit_Name
7276 | N_Delay_Alternative
7277 | N_Delta_Aggregate
7278 | N_Delta_Constraint
7279 | N_Derived_Type_Definition
7280 | N_Designator
7281 | N_Digits_Constraint
7282 | N_Discriminant_Association
7283 | N_Discriminant_Specification
7284 | N_Empty
7285 | N_Entry_Body_Formal_Part
7286 | N_Entry_Call_Alternative
7287 | N_Entry_Declaration
7288 | N_Entry_Index_Specification
7289 | N_Enumeration_Type_Definition
7290 | N_Error
7291 | N_Exception_Handler
7292 | N_Expanded_Name
7293 | N_Explicit_Dereference
7294 | N_Extension_Aggregate
7295 | N_Floating_Point_Definition
7296 | N_Formal_Decimal_Fixed_Point_Definition
7297 | N_Formal_Derived_Type_Definition
7298 | N_Formal_Discrete_Type_Definition
7299 | N_Formal_Floating_Point_Definition
7300 | N_Formal_Modular_Type_Definition
7301 | N_Formal_Ordinary_Fixed_Point_Definition
7302 | N_Formal_Package_Declaration
7303 | N_Formal_Private_Type_Definition
7304 | N_Formal_Incomplete_Type_Definition
7305 | N_Formal_Signed_Integer_Type_Definition
7306 | N_Function_Call
7307 | N_Function_Specification
7308 | N_Generic_Association
7309 | N_Handled_Sequence_Of_Statements
7310 | N_Identifier
7311 | N_In
7312 | N_Index_Or_Discriminant_Constraint
7313 | N_Indexed_Component
7314 | N_Integer_Literal
7315 | N_Iterator_Specification
7316 | N_Itype_Reference
7317 | N_Label
7318 | N_Loop_Parameter_Specification
7319 | N_Mod_Clause
7320 | N_Modular_Type_Definition
7321 | N_Not_In
7322 | N_Null
7323 | N_Op_Abs
7324 | N_Op_Add
7325 | N_Op_And
7326 | N_Op_Concat
7327 | N_Op_Divide
7328 | N_Op_Eq
7329 | N_Op_Expon
7330 | N_Op_Ge
7331 | N_Op_Gt
7332 | N_Op_Le
7333 | N_Op_Lt
7334 | N_Op_Minus
7335 | N_Op_Mod
7336 | N_Op_Multiply
7337 | N_Op_Ne
7338 | N_Op_Not
7339 | N_Op_Or
7340 | N_Op_Plus
7341 | N_Op_Rem
7342 | N_Op_Rotate_Left
7343 | N_Op_Rotate_Right
7344 | N_Op_Shift_Left
7345 | N_Op_Shift_Right
7346 | N_Op_Shift_Right_Arithmetic
7347 | N_Op_Subtract
7348 | N_Op_Xor
7349 | N_Operator_Symbol
7350 | N_Ordinary_Fixed_Point_Definition
7351 | N_Others_Choice
7352 | N_Package_Specification
7353 | N_Parameter_Association
7354 | N_Parameter_Specification
7355 | N_Pop_Constraint_Error_Label
7356 | N_Pop_Program_Error_Label
7357 | N_Pop_Storage_Error_Label
7358 | N_Pragma_Argument_Association
7359 | N_Procedure_Specification
7360 | N_Protected_Definition
7361 | N_Push_Constraint_Error_Label
7362 | N_Push_Program_Error_Label
7363 | N_Push_Storage_Error_Label
7364 | N_Qualified_Expression
7365 | N_Quantified_Expression
7366 | N_Raise_Expression
7367 | N_Range
7368 | N_Range_Constraint
7369 | N_Real_Literal
7370 | N_Real_Range_Specification
7371 | N_Record_Definition
7372 | N_Reference
7373 | N_SCIL_Dispatch_Table_Tag_Init
7374 | N_SCIL_Dispatching_Call
7375 | N_SCIL_Membership_Test
7376 | N_Selected_Component
7377 | N_Signed_Integer_Type_Definition
7378 | N_Single_Protected_Declaration
7379 | N_Slice
7380 | N_String_Literal
7381 | N_Subtype_Indication
7382 | N_Subunit
7383 | N_Target_Name
7384 | N_Task_Definition
7385 | N_Terminate_Alternative
7386 | N_Triggering_Alternative
7387 | N_Type_Conversion
7388 | N_Unchecked_Expression
7389 | N_Unchecked_Type_Conversion
7390 | N_Unconstrained_Array_Definition
7391 | N_Unused_At_End
7392 | N_Unused_At_Start
7393 | N_Variant
7394 | N_Variant_Part
7395 | N_Validate_Unchecked_Conversion
7396 | N_With_Clause
7398 null;
7399 end case;
7401 -- If we fall through above tests, keep climbing tree
7403 N := P;
7405 if Nkind (Parent (N)) = N_Subunit then
7407 -- This is the proper body corresponding to a stub. Insertion must
7408 -- be done at the point of the stub, which is in the declarative
7409 -- part of the parent unit.
7411 P := Corresponding_Stub (Parent (N));
7413 else
7414 P := Parent (N);
7415 end if;
7416 end loop;
7417 end Insert_Actions;
7419 -- Version with check(s) suppressed
7421 procedure Insert_Actions
7422 (Assoc_Node : Node_Id;
7423 Ins_Actions : List_Id;
7424 Suppress : Check_Id)
7426 begin
7427 if Suppress = All_Checks then
7428 declare
7429 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7430 begin
7431 Scope_Suppress.Suppress := (others => True);
7432 Insert_Actions (Assoc_Node, Ins_Actions);
7433 Scope_Suppress.Suppress := Sva;
7434 end;
7436 else
7437 declare
7438 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7439 begin
7440 Scope_Suppress.Suppress (Suppress) := True;
7441 Insert_Actions (Assoc_Node, Ins_Actions);
7442 Scope_Suppress.Suppress (Suppress) := Svg;
7443 end;
7444 end if;
7445 end Insert_Actions;
7447 --------------------------
7448 -- Insert_Actions_After --
7449 --------------------------
7451 procedure Insert_Actions_After
7452 (Assoc_Node : Node_Id;
7453 Ins_Actions : List_Id)
7455 begin
7456 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7457 Store_After_Actions_In_Scope (Ins_Actions);
7458 else
7459 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7460 end if;
7461 end Insert_Actions_After;
7463 ------------------------
7464 -- Insert_Declaration --
7465 ------------------------
7467 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7468 P : Node_Id;
7470 begin
7471 pragma Assert (Nkind (N) in N_Subexpr);
7473 -- Climb until we find a procedure or a package
7475 P := N;
7476 loop
7477 pragma Assert (Present (Parent (P)));
7478 P := Parent (P);
7480 if Is_List_Member (P) then
7481 exit when Nkind_In (Parent (P), N_Package_Specification,
7482 N_Subprogram_Body);
7484 -- Special handling for handled sequence of statements, we must
7485 -- insert in the statements not the exception handlers!
7487 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7488 P := First (Statements (Parent (P)));
7489 exit;
7490 end if;
7491 end if;
7492 end loop;
7494 -- Now do the insertion
7496 Insert_Before (P, Decl);
7497 Analyze (Decl);
7498 end Insert_Declaration;
7500 ---------------------------------
7501 -- Insert_Library_Level_Action --
7502 ---------------------------------
7504 procedure Insert_Library_Level_Action (N : Node_Id) is
7505 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7507 begin
7508 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7509 -- And not Main_Unit as previously. If the main unit is a body,
7510 -- the scope needed to analyze the actions is the entity of the
7511 -- corresponding declaration.
7513 if No (Actions (Aux)) then
7514 Set_Actions (Aux, New_List (N));
7515 else
7516 Append (N, Actions (Aux));
7517 end if;
7519 Analyze (N);
7520 Pop_Scope;
7521 end Insert_Library_Level_Action;
7523 ----------------------------------
7524 -- Insert_Library_Level_Actions --
7525 ----------------------------------
7527 procedure Insert_Library_Level_Actions (L : List_Id) is
7528 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7530 begin
7531 if Is_Non_Empty_List (L) then
7532 Push_Scope (Cunit_Entity (Main_Unit));
7533 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7535 if No (Actions (Aux)) then
7536 Set_Actions (Aux, L);
7537 Analyze_List (L);
7538 else
7539 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7540 end if;
7542 Pop_Scope;
7543 end if;
7544 end Insert_Library_Level_Actions;
7546 ----------------------
7547 -- Inside_Init_Proc --
7548 ----------------------
7550 function Inside_Init_Proc return Boolean is
7551 Proc : constant Entity_Id := Enclosing_Init_Proc;
7553 begin
7554 return Proc /= Empty;
7555 end Inside_Init_Proc;
7557 ----------------------------
7558 -- Is_All_Null_Statements --
7559 ----------------------------
7561 function Is_All_Null_Statements (L : List_Id) return Boolean is
7562 Stm : Node_Id;
7564 begin
7565 Stm := First (L);
7566 while Present (Stm) loop
7567 if Nkind (Stm) /= N_Null_Statement then
7568 return False;
7569 end if;
7571 Next (Stm);
7572 end loop;
7574 return True;
7575 end Is_All_Null_Statements;
7577 --------------------------------------------------
7578 -- Is_Displacement_Of_Object_Or_Function_Result --
7579 --------------------------------------------------
7581 function Is_Displacement_Of_Object_Or_Function_Result
7582 (Obj_Id : Entity_Id) return Boolean
7584 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7585 -- Determine whether node N denotes a controlled function call
7587 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
7588 -- Determine whether node N denotes a generalized indexing form which
7589 -- involves a controlled result.
7591 function Is_Displace_Call (N : Node_Id) return Boolean;
7592 -- Determine whether node N denotes a call to Ada.Tags.Displace
7594 function Is_Source_Object (N : Node_Id) return Boolean;
7595 -- Determine whether a particular node denotes a source object
7597 function Strip (N : Node_Id) return Node_Id;
7598 -- Examine arbitrary node N by stripping various indirections and return
7599 -- the "real" node.
7601 ---------------------------------
7602 -- Is_Controlled_Function_Call --
7603 ---------------------------------
7605 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7606 Expr : Node_Id;
7608 begin
7609 -- When a function call appears in Object.Operation format, the
7610 -- original representation has several possible forms depending on
7611 -- the availability and form of actual parameters:
7613 -- Obj.Func N_Selected_Component
7614 -- Obj.Func (Actual) N_Indexed_Component
7615 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7616 -- N_Selected_Component
7618 Expr := Original_Node (N);
7619 loop
7620 if Nkind (Expr) = N_Function_Call then
7621 Expr := Name (Expr);
7623 -- "Obj.Func (Actual)" case
7625 elsif Nkind (Expr) = N_Indexed_Component then
7626 Expr := Prefix (Expr);
7628 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7630 elsif Nkind (Expr) = N_Selected_Component then
7631 Expr := Selector_Name (Expr);
7633 else
7634 exit;
7635 end if;
7636 end loop;
7638 return
7639 Nkind (Expr) in N_Has_Entity
7640 and then Present (Entity (Expr))
7641 and then Ekind (Entity (Expr)) = E_Function
7642 and then Needs_Finalization (Etype (Entity (Expr)));
7643 end Is_Controlled_Function_Call;
7645 ----------------------------
7646 -- Is_Controlled_Indexing --
7647 ----------------------------
7649 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
7650 Expr : constant Node_Id := Original_Node (N);
7652 begin
7653 return
7654 Nkind (Expr) = N_Indexed_Component
7655 and then Present (Generalized_Indexing (Expr))
7656 and then Needs_Finalization (Etype (Expr));
7657 end Is_Controlled_Indexing;
7659 ----------------------
7660 -- Is_Displace_Call --
7661 ----------------------
7663 function Is_Displace_Call (N : Node_Id) return Boolean is
7664 Call : constant Node_Id := Strip (N);
7666 begin
7667 return
7668 Present (Call)
7669 and then Nkind (Call) = N_Function_Call
7670 and then Nkind (Name (Call)) in N_Has_Entity
7671 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7672 end Is_Displace_Call;
7674 ----------------------
7675 -- Is_Source_Object --
7676 ----------------------
7678 function Is_Source_Object (N : Node_Id) return Boolean is
7679 Obj : constant Node_Id := Strip (N);
7681 begin
7682 return
7683 Present (Obj)
7684 and then Comes_From_Source (Obj)
7685 and then Nkind (Obj) in N_Has_Entity
7686 and then Is_Object (Entity (Obj));
7687 end Is_Source_Object;
7689 -----------
7690 -- Strip --
7691 -----------
7693 function Strip (N : Node_Id) return Node_Id is
7694 Result : Node_Id;
7696 begin
7697 Result := N;
7698 loop
7699 if Nkind (Result) = N_Explicit_Dereference then
7700 Result := Prefix (Result);
7702 elsif Nkind_In (Result, N_Type_Conversion,
7703 N_Unchecked_Type_Conversion)
7704 then
7705 Result := Expression (Result);
7707 else
7708 exit;
7709 end if;
7710 end loop;
7712 return Result;
7713 end Strip;
7715 -- Local variables
7717 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
7718 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7719 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
7720 Orig_Expr : Node_Id;
7722 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7724 begin
7725 -- Case 1:
7727 -- Obj : CW_Type := Function_Call (...);
7729 -- is rewritten into:
7731 -- Temp : ... := Function_Call (...)'reference;
7732 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7734 -- where the return type of the function and the class-wide type require
7735 -- dispatch table pointer displacement.
7737 -- Case 2:
7739 -- Obj : CW_Type := Container (...);
7741 -- is rewritten into:
7743 -- Temp : ... := Function_Call (Container, ...)'reference;
7744 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7746 -- where the container element type and the class-wide type require
7747 -- dispatch table pointer dispacement.
7749 -- Case 3:
7751 -- Obj : CW_Type := Src_Obj;
7753 -- is rewritten into:
7755 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7757 -- where the type of the source object and the class-wide type require
7758 -- dispatch table pointer displacement.
7760 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
7761 and then Is_Class_Wide_Type (Obj_Typ)
7762 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7763 and then Nkind (Orig_Decl) = N_Object_Declaration
7764 and then Comes_From_Source (Orig_Decl)
7765 then
7766 Orig_Expr := Expression (Orig_Decl);
7768 return
7769 Is_Controlled_Function_Call (Orig_Expr)
7770 or else Is_Controlled_Indexing (Orig_Expr)
7771 or else Is_Source_Object (Orig_Expr);
7772 end if;
7774 return False;
7775 end Is_Displacement_Of_Object_Or_Function_Result;
7777 ------------------------------
7778 -- Is_Finalizable_Transient --
7779 ------------------------------
7781 function Is_Finalizable_Transient
7782 (Decl : Node_Id;
7783 Rel_Node : Node_Id) return Boolean
7785 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7786 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7788 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7789 -- Determine whether transient object Trans_Id is initialized either
7790 -- by a function call which returns an access type or simply renames
7791 -- another pointer.
7793 function Initialized_By_Aliased_BIP_Func_Call
7794 (Trans_Id : Entity_Id) return Boolean;
7795 -- Determine whether transient object Trans_Id is initialized by a
7796 -- build-in-place function call where the BIPalloc parameter is of
7797 -- value 1 and BIPaccess is not null. This case creates an aliasing
7798 -- between the returned value and the value denoted by BIPaccess.
7800 function Is_Aliased
7801 (Trans_Id : Entity_Id;
7802 First_Stmt : Node_Id) return Boolean;
7803 -- Determine whether transient object Trans_Id has been renamed or
7804 -- aliased through 'reference in the statement list starting from
7805 -- First_Stmt.
7807 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7808 -- Determine whether transient object Trans_Id is allocated on the heap
7810 function Is_Iterated_Container
7811 (Trans_Id : Entity_Id;
7812 First_Stmt : Node_Id) return Boolean;
7813 -- Determine whether transient object Trans_Id denotes a container which
7814 -- is in the process of being iterated in the statement list starting
7815 -- from First_Stmt.
7817 ---------------------------
7818 -- Initialized_By_Access --
7819 ---------------------------
7821 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7822 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7824 begin
7825 return
7826 Present (Expr)
7827 and then Nkind (Expr) /= N_Reference
7828 and then Is_Access_Type (Etype (Expr));
7829 end Initialized_By_Access;
7831 ------------------------------------------
7832 -- Initialized_By_Aliased_BIP_Func_Call --
7833 ------------------------------------------
7835 function Initialized_By_Aliased_BIP_Func_Call
7836 (Trans_Id : Entity_Id) return Boolean
7838 Call : Node_Id := Expression (Parent (Trans_Id));
7840 begin
7841 -- Build-in-place calls usually appear in 'reference format
7843 if Nkind (Call) = N_Reference then
7844 Call := Prefix (Call);
7845 end if;
7847 Call := Unqual_Conv (Call);
7849 if Is_Build_In_Place_Function_Call (Call) then
7850 declare
7851 Access_Nam : Name_Id := No_Name;
7852 Access_OK : Boolean := False;
7853 Actual : Node_Id;
7854 Alloc_Nam : Name_Id := No_Name;
7855 Alloc_OK : Boolean := False;
7856 Formal : Node_Id;
7857 Func_Id : Entity_Id;
7858 Param : Node_Id;
7860 begin
7861 -- Examine all parameter associations of the function call
7863 Param := First (Parameter_Associations (Call));
7864 while Present (Param) loop
7865 if Nkind (Param) = N_Parameter_Association
7866 and then Nkind (Selector_Name (Param)) = N_Identifier
7867 then
7868 Actual := Explicit_Actual_Parameter (Param);
7869 Formal := Selector_Name (Param);
7871 -- Construct the names of formals BIPaccess and BIPalloc
7872 -- using the function name retrieved from an arbitrary
7873 -- formal.
7875 if Access_Nam = No_Name
7876 and then Alloc_Nam = No_Name
7877 and then Present (Entity (Formal))
7878 then
7879 Func_Id := Scope (Entity (Formal));
7881 Access_Nam :=
7882 New_External_Name (Chars (Func_Id),
7883 BIP_Formal_Suffix (BIP_Object_Access));
7885 Alloc_Nam :=
7886 New_External_Name (Chars (Func_Id),
7887 BIP_Formal_Suffix (BIP_Alloc_Form));
7888 end if;
7890 -- A match for BIPaccess => Temp has been found
7892 if Chars (Formal) = Access_Nam
7893 and then Nkind (Actual) /= N_Null
7894 then
7895 Access_OK := True;
7896 end if;
7898 -- A match for BIPalloc => 1 has been found
7900 if Chars (Formal) = Alloc_Nam
7901 and then Nkind (Actual) = N_Integer_Literal
7902 and then Intval (Actual) = Uint_1
7903 then
7904 Alloc_OK := True;
7905 end if;
7906 end if;
7908 Next (Param);
7909 end loop;
7911 return Access_OK and Alloc_OK;
7912 end;
7913 end if;
7915 return False;
7916 end Initialized_By_Aliased_BIP_Func_Call;
7918 ----------------
7919 -- Is_Aliased --
7920 ----------------
7922 function Is_Aliased
7923 (Trans_Id : Entity_Id;
7924 First_Stmt : Node_Id) return Boolean
7926 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7927 -- Given an object renaming declaration, retrieve the entity of the
7928 -- renamed name. Return Empty if the renamed name is anything other
7929 -- than a variable or a constant.
7931 -------------------------
7932 -- Find_Renamed_Object --
7933 -------------------------
7935 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7936 Ren_Obj : Node_Id := Empty;
7938 function Find_Object (N : Node_Id) return Traverse_Result;
7939 -- Try to detect an object which is either a constant or a
7940 -- variable.
7942 -----------------
7943 -- Find_Object --
7944 -----------------
7946 function Find_Object (N : Node_Id) return Traverse_Result is
7947 begin
7948 -- Stop the search once a constant or a variable has been
7949 -- detected.
7951 if Nkind (N) = N_Identifier
7952 and then Present (Entity (N))
7953 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7954 then
7955 Ren_Obj := Entity (N);
7956 return Abandon;
7957 end if;
7959 return OK;
7960 end Find_Object;
7962 procedure Search is new Traverse_Proc (Find_Object);
7964 -- Local variables
7966 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7968 -- Start of processing for Find_Renamed_Object
7970 begin
7971 -- Actions related to dispatching calls may appear as renamings of
7972 -- tags. Do not process this type of renaming because it does not
7973 -- use the actual value of the object.
7975 if not Is_RTE (Typ, RE_Tag_Ptr) then
7976 Search (Name (Ren_Decl));
7977 end if;
7979 return Ren_Obj;
7980 end Find_Renamed_Object;
7982 -- Local variables
7984 Expr : Node_Id;
7985 Ren_Obj : Entity_Id;
7986 Stmt : Node_Id;
7988 -- Start of processing for Is_Aliased
7990 begin
7991 -- A controlled transient object is not considered aliased when it
7992 -- appears inside an expression_with_actions node even when there are
7993 -- explicit aliases of it:
7995 -- do
7996 -- Trans_Id : Ctrl_Typ ...; -- transient object
7997 -- Alias : ... := Trans_Id; -- object is aliased
7998 -- Val : constant Boolean :=
7999 -- ... Alias ...; -- aliasing ends
8000 -- <finalize Trans_Id> -- object safe to finalize
8001 -- in Val end;
8003 -- Expansion ensures that all aliases are encapsulated in the actions
8004 -- list and do not leak to the expression by forcing the evaluation
8005 -- of the expression.
8007 if Nkind (Rel_Node) = N_Expression_With_Actions then
8008 return False;
8010 -- Otherwise examine the statements after the controlled transient
8011 -- object and look for various forms of aliasing.
8013 else
8014 Stmt := First_Stmt;
8015 while Present (Stmt) loop
8016 if Nkind (Stmt) = N_Object_Declaration then
8017 Expr := Expression (Stmt);
8019 -- Aliasing of the form:
8020 -- Obj : ... := Trans_Id'reference;
8022 if Present (Expr)
8023 and then Nkind (Expr) = N_Reference
8024 and then Nkind (Prefix (Expr)) = N_Identifier
8025 and then Entity (Prefix (Expr)) = Trans_Id
8026 then
8027 return True;
8028 end if;
8030 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8031 Ren_Obj := Find_Renamed_Object (Stmt);
8033 -- Aliasing of the form:
8034 -- Obj : ... renames ... Trans_Id ...;
8036 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8037 return True;
8038 end if;
8039 end if;
8041 Next (Stmt);
8042 end loop;
8044 return False;
8045 end if;
8046 end Is_Aliased;
8048 ------------------
8049 -- Is_Allocated --
8050 ------------------
8052 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8053 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8054 begin
8055 return
8056 Is_Access_Type (Etype (Trans_Id))
8057 and then Present (Expr)
8058 and then Nkind (Expr) = N_Allocator;
8059 end Is_Allocated;
8061 ---------------------------
8062 -- Is_Iterated_Container --
8063 ---------------------------
8065 function Is_Iterated_Container
8066 (Trans_Id : Entity_Id;
8067 First_Stmt : Node_Id) return Boolean
8069 Aspect : Node_Id;
8070 Call : Node_Id;
8071 Iter : Entity_Id;
8072 Param : Node_Id;
8073 Stmt : Node_Id;
8074 Typ : Entity_Id;
8076 begin
8077 -- It is not possible to iterate over containers in non-Ada 2012 code
8079 if Ada_Version < Ada_2012 then
8080 return False;
8081 end if;
8083 Typ := Etype (Trans_Id);
8085 -- Handle access type created for secondary stack use
8087 if Is_Access_Type (Typ) then
8088 Typ := Designated_Type (Typ);
8089 end if;
8091 -- Look for aspect Default_Iterator. It may be part of a type
8092 -- declaration for a container, or inherited from a base type
8093 -- or parent type.
8095 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8097 if Present (Aspect) then
8098 Iter := Entity (Aspect);
8100 -- Examine the statements following the container object and
8101 -- look for a call to the default iterate routine where the
8102 -- first parameter is the transient. Such a call appears as:
8104 -- It : Access_To_CW_Iterator :=
8105 -- Iterate (Tran_Id.all, ...)'reference;
8107 Stmt := First_Stmt;
8108 while Present (Stmt) loop
8110 -- Detect an object declaration which is initialized by a
8111 -- secondary stack function call.
8113 if Nkind (Stmt) = N_Object_Declaration
8114 and then Present (Expression (Stmt))
8115 and then Nkind (Expression (Stmt)) = N_Reference
8116 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8117 then
8118 Call := Prefix (Expression (Stmt));
8120 -- The call must invoke the default iterate routine of
8121 -- the container and the transient object must appear as
8122 -- the first actual parameter. Skip any calls whose names
8123 -- are not entities.
8125 if Is_Entity_Name (Name (Call))
8126 and then Entity (Name (Call)) = Iter
8127 and then Present (Parameter_Associations (Call))
8128 then
8129 Param := First (Parameter_Associations (Call));
8131 if Nkind (Param) = N_Explicit_Dereference
8132 and then Entity (Prefix (Param)) = Trans_Id
8133 then
8134 return True;
8135 end if;
8136 end if;
8137 end if;
8139 Next (Stmt);
8140 end loop;
8141 end if;
8143 return False;
8144 end Is_Iterated_Container;
8146 -- Local variables
8148 Desig : Entity_Id := Obj_Typ;
8150 -- Start of processing for Is_Finalizable_Transient
8152 begin
8153 -- Handle access types
8155 if Is_Access_Type (Desig) then
8156 Desig := Available_View (Designated_Type (Desig));
8157 end if;
8159 return
8160 Ekind_In (Obj_Id, E_Constant, E_Variable)
8161 and then Needs_Finalization (Desig)
8162 and then Requires_Transient_Scope (Desig)
8163 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8165 -- Do not consider a transient object that was already processed
8167 and then not Is_Finalized_Transient (Obj_Id)
8169 -- Do not consider renamed or 'reference-d transient objects because
8170 -- the act of renaming extends the object's lifetime.
8172 and then not Is_Aliased (Obj_Id, Decl)
8174 -- Do not consider transient objects allocated on the heap since
8175 -- they are attached to a finalization master.
8177 and then not Is_Allocated (Obj_Id)
8179 -- If the transient object is a pointer, check that it is not
8180 -- initialized by a function that returns a pointer or acts as a
8181 -- renaming of another pointer.
8183 and then
8184 (not Is_Access_Type (Obj_Typ)
8185 or else not Initialized_By_Access (Obj_Id))
8187 -- Do not consider transient objects which act as indirect aliases
8188 -- of build-in-place function results.
8190 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8192 -- Do not consider conversions of tags to class-wide types
8194 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8196 -- Do not consider iterators because those are treated as normal
8197 -- controlled objects and are processed by the usual finalization
8198 -- machinery. This avoids the double finalization of an iterator.
8200 and then not Is_Iterator (Desig)
8202 -- Do not consider containers in the context of iterator loops. Such
8203 -- transient objects must exist for as long as the loop is around,
8204 -- otherwise any operation carried out by the iterator will fail.
8206 and then not Is_Iterated_Container (Obj_Id, Decl);
8207 end Is_Finalizable_Transient;
8209 ---------------------------------
8210 -- Is_Fully_Repped_Tagged_Type --
8211 ---------------------------------
8213 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8214 U : constant Entity_Id := Underlying_Type (T);
8215 Comp : Entity_Id;
8217 begin
8218 if No (U) or else not Is_Tagged_Type (U) then
8219 return False;
8220 elsif Has_Discriminants (U) then
8221 return False;
8222 elsif not Has_Specified_Layout (U) then
8223 return False;
8224 end if;
8226 -- Here we have a tagged type, see if it has any unlayed out fields
8227 -- other than a possible tag and parent fields. If so, we return False.
8229 Comp := First_Component (U);
8230 while Present (Comp) loop
8231 if not Is_Tag (Comp)
8232 and then Chars (Comp) /= Name_uParent
8233 and then No (Component_Clause (Comp))
8234 then
8235 return False;
8236 else
8237 Next_Component (Comp);
8238 end if;
8239 end loop;
8241 -- All components are layed out
8243 return True;
8244 end Is_Fully_Repped_Tagged_Type;
8246 ----------------------------------
8247 -- Is_Library_Level_Tagged_Type --
8248 ----------------------------------
8250 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8251 begin
8252 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8253 end Is_Library_Level_Tagged_Type;
8255 --------------------------
8256 -- Is_Non_BIP_Func_Call --
8257 --------------------------
8259 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8260 begin
8261 -- The expected call is of the format
8263 -- Func_Call'reference
8265 return
8266 Nkind (Expr) = N_Reference
8267 and then Nkind (Prefix (Expr)) = N_Function_Call
8268 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8269 end Is_Non_BIP_Func_Call;
8271 ----------------------------------
8272 -- Is_Possibly_Unaligned_Object --
8273 ----------------------------------
8275 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8276 T : constant Entity_Id := Etype (N);
8278 begin
8279 -- If renamed object, apply test to underlying object
8281 if Is_Entity_Name (N)
8282 and then Is_Object (Entity (N))
8283 and then Present (Renamed_Object (Entity (N)))
8284 then
8285 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8286 end if;
8288 -- Tagged and controlled types and aliased types are always aligned, as
8289 -- are concurrent types.
8291 if Is_Aliased (T)
8292 or else Has_Controlled_Component (T)
8293 or else Is_Concurrent_Type (T)
8294 or else Is_Tagged_Type (T)
8295 or else Is_Controlled (T)
8296 then
8297 return False;
8298 end if;
8300 -- If this is an element of a packed array, may be unaligned
8302 if Is_Ref_To_Bit_Packed_Array (N) then
8303 return True;
8304 end if;
8306 -- Case of indexed component reference: test whether prefix is unaligned
8308 if Nkind (N) = N_Indexed_Component then
8309 return Is_Possibly_Unaligned_Object (Prefix (N));
8311 -- Case of selected component reference
8313 elsif Nkind (N) = N_Selected_Component then
8314 declare
8315 P : constant Node_Id := Prefix (N);
8316 C : constant Entity_Id := Entity (Selector_Name (N));
8317 M : Nat;
8318 S : Nat;
8320 begin
8321 -- If component reference is for an array with non-static bounds,
8322 -- then it is always aligned: we can only process unaligned arrays
8323 -- with static bounds (more precisely compile time known bounds).
8325 if Is_Array_Type (T)
8326 and then not Compile_Time_Known_Bounds (T)
8327 then
8328 return False;
8329 end if;
8331 -- If component is aliased, it is definitely properly aligned
8333 if Is_Aliased (C) then
8334 return False;
8335 end if;
8337 -- If component is for a type implemented as a scalar, and the
8338 -- record is packed, and the component is other than the first
8339 -- component of the record, then the component may be unaligned.
8341 if Is_Packed (Etype (P))
8342 and then Represented_As_Scalar (Etype (C))
8343 and then First_Entity (Scope (C)) /= C
8344 then
8345 return True;
8346 end if;
8348 -- Compute maximum possible alignment for T
8350 -- If alignment is known, then that settles things
8352 if Known_Alignment (T) then
8353 M := UI_To_Int (Alignment (T));
8355 -- If alignment is not known, tentatively set max alignment
8357 else
8358 M := Ttypes.Maximum_Alignment;
8360 -- We can reduce this if the Esize is known since the default
8361 -- alignment will never be more than the smallest power of 2
8362 -- that does not exceed this Esize value.
8364 if Known_Esize (T) then
8365 S := UI_To_Int (Esize (T));
8367 while (M / 2) >= S loop
8368 M := M / 2;
8369 end loop;
8370 end if;
8371 end if;
8373 -- The following code is historical, it used to be present but it
8374 -- is too cautious, because the front-end does not know the proper
8375 -- default alignments for the target. Also, if the alignment is
8376 -- not known, the front end can't know in any case. If a copy is
8377 -- needed, the back-end will take care of it. This whole section
8378 -- including this comment can be removed later ???
8380 -- If the component reference is for a record that has a specified
8381 -- alignment, and we either know it is too small, or cannot tell,
8382 -- then the component may be unaligned.
8384 -- What is the following commented out code ???
8386 -- if Known_Alignment (Etype (P))
8387 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8388 -- and then M > Alignment (Etype (P))
8389 -- then
8390 -- return True;
8391 -- end if;
8393 -- Case of component clause present which may specify an
8394 -- unaligned position.
8396 if Present (Component_Clause (C)) then
8398 -- Otherwise we can do a test to make sure that the actual
8399 -- start position in the record, and the length, are both
8400 -- consistent with the required alignment. If not, we know
8401 -- that we are unaligned.
8403 declare
8404 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8405 Comp : Entity_Id;
8407 begin
8408 Comp := C;
8410 -- For a component inherited in a record extension, the
8411 -- clause is inherited but position and size are not set.
8413 if Is_Base_Type (Etype (P))
8414 and then Is_Tagged_Type (Etype (P))
8415 and then Present (Original_Record_Component (Comp))
8416 then
8417 Comp := Original_Record_Component (Comp);
8418 end if;
8420 if Component_Bit_Offset (Comp) mod Align_In_Bits /= 0
8421 or else Esize (Comp) mod Align_In_Bits /= 0
8422 then
8423 return True;
8424 end if;
8425 end;
8426 end if;
8428 -- Otherwise, for a component reference, test prefix
8430 return Is_Possibly_Unaligned_Object (P);
8431 end;
8433 -- If not a component reference, must be aligned
8435 else
8436 return False;
8437 end if;
8438 end Is_Possibly_Unaligned_Object;
8440 ---------------------------------
8441 -- Is_Possibly_Unaligned_Slice --
8442 ---------------------------------
8444 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8445 begin
8446 -- Go to renamed object
8448 if Is_Entity_Name (N)
8449 and then Is_Object (Entity (N))
8450 and then Present (Renamed_Object (Entity (N)))
8451 then
8452 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8453 end if;
8455 -- The reference must be a slice
8457 if Nkind (N) /= N_Slice then
8458 return False;
8459 end if;
8461 -- We only need to worry if the target has strict alignment
8463 if not Target_Strict_Alignment then
8464 return False;
8465 end if;
8467 -- If it is a slice, then look at the array type being sliced
8469 declare
8470 Sarr : constant Node_Id := Prefix (N);
8471 -- Prefix of the slice, i.e. the array being sliced
8473 Styp : constant Entity_Id := Etype (Prefix (N));
8474 -- Type of the array being sliced
8476 Pref : Node_Id;
8477 Ptyp : Entity_Id;
8479 begin
8480 -- The problems arise if the array object that is being sliced
8481 -- is a component of a record or array, and we cannot guarantee
8482 -- the alignment of the array within its containing object.
8484 -- To investigate this, we look at successive prefixes to see
8485 -- if we have a worrisome indexed or selected component.
8487 Pref := Sarr;
8488 loop
8489 -- Case of array is part of an indexed component reference
8491 if Nkind (Pref) = N_Indexed_Component then
8492 Ptyp := Etype (Prefix (Pref));
8494 -- The only problematic case is when the array is packed, in
8495 -- which case we really know nothing about the alignment of
8496 -- individual components.
8498 if Is_Bit_Packed_Array (Ptyp) then
8499 return True;
8500 end if;
8502 -- Case of array is part of a selected component reference
8504 elsif Nkind (Pref) = N_Selected_Component then
8505 Ptyp := Etype (Prefix (Pref));
8507 -- We are definitely in trouble if the record in question
8508 -- has an alignment, and either we know this alignment is
8509 -- inconsistent with the alignment of the slice, or we don't
8510 -- know what the alignment of the slice should be.
8512 if Known_Alignment (Ptyp)
8513 and then (Unknown_Alignment (Styp)
8514 or else Alignment (Styp) > Alignment (Ptyp))
8515 then
8516 return True;
8517 end if;
8519 -- We are in potential trouble if the record type is packed.
8520 -- We could special case when we know that the array is the
8521 -- first component, but that's not such a simple case ???
8523 if Is_Packed (Ptyp) then
8524 return True;
8525 end if;
8527 -- We are in trouble if there is a component clause, and
8528 -- either we do not know the alignment of the slice, or
8529 -- the alignment of the slice is inconsistent with the
8530 -- bit position specified by the component clause.
8532 declare
8533 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8534 begin
8535 if Present (Component_Clause (Field))
8536 and then
8537 (Unknown_Alignment (Styp)
8538 or else
8539 (Component_Bit_Offset (Field) mod
8540 (System_Storage_Unit * Alignment (Styp))) /= 0)
8541 then
8542 return True;
8543 end if;
8544 end;
8546 -- For cases other than selected or indexed components we know we
8547 -- are OK, since no issues arise over alignment.
8549 else
8550 return False;
8551 end if;
8553 -- We processed an indexed component or selected component
8554 -- reference that looked safe, so keep checking prefixes.
8556 Pref := Prefix (Pref);
8557 end loop;
8558 end;
8559 end Is_Possibly_Unaligned_Slice;
8561 -------------------------------
8562 -- Is_Related_To_Func_Return --
8563 -------------------------------
8565 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8566 Expr : constant Node_Id := Related_Expression (Id);
8567 begin
8568 return
8569 Present (Expr)
8570 and then Nkind (Expr) = N_Explicit_Dereference
8571 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8572 end Is_Related_To_Func_Return;
8574 --------------------------------
8575 -- Is_Ref_To_Bit_Packed_Array --
8576 --------------------------------
8578 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8579 Result : Boolean;
8580 Expr : Node_Id;
8582 begin
8583 if Is_Entity_Name (N)
8584 and then Is_Object (Entity (N))
8585 and then Present (Renamed_Object (Entity (N)))
8586 then
8587 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8588 end if;
8590 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8591 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8592 Result := True;
8593 else
8594 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8595 end if;
8597 if Result and then Nkind (N) = N_Indexed_Component then
8598 Expr := First (Expressions (N));
8599 while Present (Expr) loop
8600 Force_Evaluation (Expr);
8601 Next (Expr);
8602 end loop;
8603 end if;
8605 return Result;
8607 else
8608 return False;
8609 end if;
8610 end Is_Ref_To_Bit_Packed_Array;
8612 --------------------------------
8613 -- Is_Ref_To_Bit_Packed_Slice --
8614 --------------------------------
8616 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8617 begin
8618 if Nkind (N) = N_Type_Conversion then
8619 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8621 elsif Is_Entity_Name (N)
8622 and then Is_Object (Entity (N))
8623 and then Present (Renamed_Object (Entity (N)))
8624 then
8625 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8627 elsif Nkind (N) = N_Slice
8628 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8629 then
8630 return True;
8632 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8633 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8635 else
8636 return False;
8637 end if;
8638 end Is_Ref_To_Bit_Packed_Slice;
8640 -----------------------
8641 -- Is_Renamed_Object --
8642 -----------------------
8644 function Is_Renamed_Object (N : Node_Id) return Boolean is
8645 Pnod : constant Node_Id := Parent (N);
8646 Kind : constant Node_Kind := Nkind (Pnod);
8647 begin
8648 if Kind = N_Object_Renaming_Declaration then
8649 return True;
8650 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8651 return Is_Renamed_Object (Pnod);
8652 else
8653 return False;
8654 end if;
8655 end Is_Renamed_Object;
8657 --------------------------------------
8658 -- Is_Secondary_Stack_BIP_Func_Call --
8659 --------------------------------------
8661 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8662 Alloc_Nam : Name_Id := No_Name;
8663 Actual : Node_Id;
8664 Call : Node_Id := Expr;
8665 Formal : Node_Id;
8666 Param : Node_Id;
8668 begin
8669 -- Build-in-place calls usually appear in 'reference format. Note that
8670 -- the accessibility check machinery may add an extra 'reference due to
8671 -- side effect removal.
8673 while Nkind (Call) = N_Reference loop
8674 Call := Prefix (Call);
8675 end loop;
8677 Call := Unqual_Conv (Call);
8679 if Is_Build_In_Place_Function_Call (Call) then
8681 -- Examine all parameter associations of the function call
8683 Param := First (Parameter_Associations (Call));
8684 while Present (Param) loop
8685 if Nkind (Param) = N_Parameter_Association then
8686 Formal := Selector_Name (Param);
8687 Actual := Explicit_Actual_Parameter (Param);
8689 -- Construct the name of formal BIPalloc. It is much easier to
8690 -- extract the name of the function using an arbitrary formal's
8691 -- scope rather than the Name field of Call.
8693 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8694 Alloc_Nam :=
8695 New_External_Name
8696 (Chars (Scope (Entity (Formal))),
8697 BIP_Formal_Suffix (BIP_Alloc_Form));
8698 end if;
8700 -- A match for BIPalloc => 2 has been found
8702 if Chars (Formal) = Alloc_Nam
8703 and then Nkind (Actual) = N_Integer_Literal
8704 and then Intval (Actual) = Uint_2
8705 then
8706 return True;
8707 end if;
8708 end if;
8710 Next (Param);
8711 end loop;
8712 end if;
8714 return False;
8715 end Is_Secondary_Stack_BIP_Func_Call;
8717 -------------------------------------
8718 -- Is_Tag_To_Class_Wide_Conversion --
8719 -------------------------------------
8721 function Is_Tag_To_Class_Wide_Conversion
8722 (Obj_Id : Entity_Id) return Boolean
8724 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8726 begin
8727 return
8728 Is_Class_Wide_Type (Etype (Obj_Id))
8729 and then Present (Expr)
8730 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8731 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8732 end Is_Tag_To_Class_Wide_Conversion;
8734 ----------------------------
8735 -- Is_Untagged_Derivation --
8736 ----------------------------
8738 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8739 begin
8740 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8741 or else
8742 (Is_Private_Type (T) and then Present (Full_View (T))
8743 and then not Is_Tagged_Type (Full_View (T))
8744 and then Is_Derived_Type (Full_View (T))
8745 and then Etype (Full_View (T)) /= T);
8746 end Is_Untagged_Derivation;
8748 ------------------------------------
8749 -- Is_Untagged_Private_Derivation --
8750 ------------------------------------
8752 function Is_Untagged_Private_Derivation
8753 (Priv_Typ : Entity_Id;
8754 Full_Typ : Entity_Id) return Boolean
8756 begin
8757 return
8758 Present (Priv_Typ)
8759 and then Is_Untagged_Derivation (Priv_Typ)
8760 and then Is_Private_Type (Etype (Priv_Typ))
8761 and then Present (Full_Typ)
8762 and then Is_Itype (Full_Typ);
8763 end Is_Untagged_Private_Derivation;
8765 ------------------------------
8766 -- Is_Verifiable_DIC_Pragma --
8767 ------------------------------
8769 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
8770 Args : constant List_Id := Pragma_Argument_Associations (Prag);
8772 begin
8773 -- To qualify as verifiable, a DIC pragma must have a non-null argument
8775 return
8776 Present (Args)
8777 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
8778 end Is_Verifiable_DIC_Pragma;
8780 ---------------------------
8781 -- Is_Volatile_Reference --
8782 ---------------------------
8784 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8785 begin
8786 -- Only source references are to be treated as volatile, internally
8787 -- generated stuff cannot have volatile external effects.
8789 if not Comes_From_Source (N) then
8790 return False;
8792 -- Never true for reference to a type
8794 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8795 return False;
8797 -- Never true for a compile time known constant
8799 elsif Compile_Time_Known_Value (N) then
8800 return False;
8802 -- True if object reference with volatile type
8804 elsif Is_Volatile_Object (N) then
8805 return True;
8807 -- True if reference to volatile entity
8809 elsif Is_Entity_Name (N) then
8810 return Treat_As_Volatile (Entity (N));
8812 -- True for slice of volatile array
8814 elsif Nkind (N) = N_Slice then
8815 return Is_Volatile_Reference (Prefix (N));
8817 -- True if volatile component
8819 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8820 if (Is_Entity_Name (Prefix (N))
8821 and then Has_Volatile_Components (Entity (Prefix (N))))
8822 or else (Present (Etype (Prefix (N)))
8823 and then Has_Volatile_Components (Etype (Prefix (N))))
8824 then
8825 return True;
8826 else
8827 return Is_Volatile_Reference (Prefix (N));
8828 end if;
8830 -- Otherwise false
8832 else
8833 return False;
8834 end if;
8835 end Is_Volatile_Reference;
8837 --------------------
8838 -- Kill_Dead_Code --
8839 --------------------
8841 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8842 W : Boolean := Warn;
8843 -- Set False if warnings suppressed
8845 begin
8846 if Present (N) then
8847 Remove_Warning_Messages (N);
8849 -- Update the internal structures of the ABE mechanism in case the
8850 -- dead node is an elaboration scenario.
8852 Kill_Elaboration_Scenario (N);
8854 -- Generate warning if appropriate
8856 if W then
8858 -- We suppress the warning if this code is under control of an
8859 -- if statement, whose condition is a simple identifier, and
8860 -- either we are in an instance, or warnings off is set for this
8861 -- identifier. The reason for killing it in the instance case is
8862 -- that it is common and reasonable for code to be deleted in
8863 -- instances for various reasons.
8865 -- Could we use Is_Statically_Unevaluated here???
8867 if Nkind (Parent (N)) = N_If_Statement then
8868 declare
8869 C : constant Node_Id := Condition (Parent (N));
8870 begin
8871 if Nkind (C) = N_Identifier
8872 and then
8873 (In_Instance
8874 or else (Present (Entity (C))
8875 and then Has_Warnings_Off (Entity (C))))
8876 then
8877 W := False;
8878 end if;
8879 end;
8880 end if;
8882 -- Generate warning if not suppressed
8884 if W then
8885 Error_Msg_F
8886 ("?t?this code can never be executed and has been deleted!",
8888 end if;
8889 end if;
8891 -- Recurse into block statements and bodies to process declarations
8892 -- and statements.
8894 if Nkind (N) = N_Block_Statement
8895 or else Nkind (N) = N_Subprogram_Body
8896 or else Nkind (N) = N_Package_Body
8897 then
8898 Kill_Dead_Code (Declarations (N), False);
8899 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8901 if Nkind (N) = N_Subprogram_Body then
8902 Set_Is_Eliminated (Defining_Entity (N));
8903 end if;
8905 elsif Nkind (N) = N_Package_Declaration then
8906 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8907 Kill_Dead_Code (Private_Declarations (Specification (N)));
8909 -- ??? After this point, Delete_Tree has been called on all
8910 -- declarations in Specification (N), so references to entities
8911 -- therein look suspicious.
8913 declare
8914 E : Entity_Id := First_Entity (Defining_Entity (N));
8916 begin
8917 while Present (E) loop
8918 if Ekind (E) = E_Operator then
8919 Set_Is_Eliminated (E);
8920 end if;
8922 Next_Entity (E);
8923 end loop;
8924 end;
8926 -- Recurse into composite statement to kill individual statements in
8927 -- particular instantiations.
8929 elsif Nkind (N) = N_If_Statement then
8930 Kill_Dead_Code (Then_Statements (N));
8931 Kill_Dead_Code (Elsif_Parts (N));
8932 Kill_Dead_Code (Else_Statements (N));
8934 elsif Nkind (N) = N_Loop_Statement then
8935 Kill_Dead_Code (Statements (N));
8937 elsif Nkind (N) = N_Case_Statement then
8938 declare
8939 Alt : Node_Id;
8940 begin
8941 Alt := First (Alternatives (N));
8942 while Present (Alt) loop
8943 Kill_Dead_Code (Statements (Alt));
8944 Next (Alt);
8945 end loop;
8946 end;
8948 elsif Nkind (N) = N_Case_Statement_Alternative then
8949 Kill_Dead_Code (Statements (N));
8951 -- Deal with dead instances caused by deleting instantiations
8953 elsif Nkind (N) in N_Generic_Instantiation then
8954 Remove_Dead_Instance (N);
8955 end if;
8956 end if;
8957 end Kill_Dead_Code;
8959 -- Case where argument is a list of nodes to be killed
8961 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8962 N : Node_Id;
8963 W : Boolean;
8965 begin
8966 W := Warn;
8968 if Is_Non_Empty_List (L) then
8969 N := First (L);
8970 while Present (N) loop
8971 Kill_Dead_Code (N, W);
8972 W := False;
8973 Next (N);
8974 end loop;
8975 end if;
8976 end Kill_Dead_Code;
8978 ------------------------
8979 -- Known_Non_Negative --
8980 ------------------------
8982 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8983 begin
8984 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8985 return True;
8987 else
8988 declare
8989 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8990 begin
8991 return
8992 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8993 end;
8994 end if;
8995 end Known_Non_Negative;
8997 -----------------------------
8998 -- Make_CW_Equivalent_Type --
8999 -----------------------------
9001 -- Create a record type used as an equivalent of any member of the class
9002 -- which takes its size from exp.
9004 -- Generate the following code:
9006 -- type Equiv_T is record
9007 -- _parent : T (List of discriminant constraints taken from Exp);
9008 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
9009 -- end Equiv_T;
9011 -- ??? Note that this type does not guarantee same alignment as all
9012 -- derived types
9014 function Make_CW_Equivalent_Type
9015 (T : Entity_Id;
9016 E : Node_Id) return Entity_Id
9018 Loc : constant Source_Ptr := Sloc (E);
9019 Root_Typ : constant Entity_Id := Root_Type (T);
9020 List_Def : constant List_Id := Empty_List;
9021 Comp_List : constant List_Id := New_List;
9022 Equiv_Type : Entity_Id;
9023 Range_Type : Entity_Id;
9024 Str_Type : Entity_Id;
9025 Constr_Root : Entity_Id;
9026 Sizexpr : Node_Id;
9028 begin
9029 -- If the root type is already constrained, there are no discriminants
9030 -- in the expression.
9032 if not Has_Discriminants (Root_Typ)
9033 or else Is_Constrained (Root_Typ)
9034 then
9035 Constr_Root := Root_Typ;
9037 -- At this point in the expansion, non-limited view of the type
9038 -- must be available, otherwise the error will be reported later.
9040 if From_Limited_With (Constr_Root)
9041 and then Present (Non_Limited_View (Constr_Root))
9042 then
9043 Constr_Root := Non_Limited_View (Constr_Root);
9044 end if;
9046 else
9047 Constr_Root := Make_Temporary (Loc, 'R');
9049 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9051 Append_To (List_Def,
9052 Make_Subtype_Declaration (Loc,
9053 Defining_Identifier => Constr_Root,
9054 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9055 end if;
9057 -- Generate the range subtype declaration
9059 Range_Type := Make_Temporary (Loc, 'G');
9061 if not Is_Interface (Root_Typ) then
9063 -- subtype rg__xx is
9064 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9066 Sizexpr :=
9067 Make_Op_Subtract (Loc,
9068 Left_Opnd =>
9069 Make_Attribute_Reference (Loc,
9070 Prefix =>
9071 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9072 Attribute_Name => Name_Size),
9073 Right_Opnd =>
9074 Make_Attribute_Reference (Loc,
9075 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9076 Attribute_Name => Name_Object_Size));
9077 else
9078 -- subtype rg__xx is
9079 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9081 Sizexpr :=
9082 Make_Attribute_Reference (Loc,
9083 Prefix =>
9084 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9085 Attribute_Name => Name_Size);
9086 end if;
9088 Set_Paren_Count (Sizexpr, 1);
9090 Append_To (List_Def,
9091 Make_Subtype_Declaration (Loc,
9092 Defining_Identifier => Range_Type,
9093 Subtype_Indication =>
9094 Make_Subtype_Indication (Loc,
9095 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9096 Constraint => Make_Range_Constraint (Loc,
9097 Range_Expression =>
9098 Make_Range (Loc,
9099 Low_Bound => Make_Integer_Literal (Loc, 1),
9100 High_Bound =>
9101 Make_Op_Divide (Loc,
9102 Left_Opnd => Sizexpr,
9103 Right_Opnd => Make_Integer_Literal (Loc,
9104 Intval => System_Storage_Unit)))))));
9106 -- subtype str__nn is Storage_Array (rg__x);
9108 Str_Type := Make_Temporary (Loc, 'S');
9109 Append_To (List_Def,
9110 Make_Subtype_Declaration (Loc,
9111 Defining_Identifier => Str_Type,
9112 Subtype_Indication =>
9113 Make_Subtype_Indication (Loc,
9114 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9115 Constraint =>
9116 Make_Index_Or_Discriminant_Constraint (Loc,
9117 Constraints =>
9118 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9120 -- type Equiv_T is record
9121 -- [ _parent : Tnn; ]
9122 -- E : Str_Type;
9123 -- end Equiv_T;
9125 Equiv_Type := Make_Temporary (Loc, 'T');
9126 Set_Ekind (Equiv_Type, E_Record_Type);
9127 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9129 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9130 -- treatment for this type. In particular, even though _parent's type
9131 -- is a controlled type or contains controlled components, we do not
9132 -- want to set Has_Controlled_Component on it to avoid making it gain
9133 -- an unwanted _controller component.
9135 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9137 -- A class-wide equivalent type does not require initialization
9139 Set_Suppress_Initialization (Equiv_Type);
9141 if not Is_Interface (Root_Typ) then
9142 Append_To (Comp_List,
9143 Make_Component_Declaration (Loc,
9144 Defining_Identifier =>
9145 Make_Defining_Identifier (Loc, Name_uParent),
9146 Component_Definition =>
9147 Make_Component_Definition (Loc,
9148 Aliased_Present => False,
9149 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9150 end if;
9152 Append_To (Comp_List,
9153 Make_Component_Declaration (Loc,
9154 Defining_Identifier => Make_Temporary (Loc, 'C'),
9155 Component_Definition =>
9156 Make_Component_Definition (Loc,
9157 Aliased_Present => False,
9158 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9160 Append_To (List_Def,
9161 Make_Full_Type_Declaration (Loc,
9162 Defining_Identifier => Equiv_Type,
9163 Type_Definition =>
9164 Make_Record_Definition (Loc,
9165 Component_List =>
9166 Make_Component_List (Loc,
9167 Component_Items => Comp_List,
9168 Variant_Part => Empty))));
9170 -- Suppress all checks during the analysis of the expanded code to avoid
9171 -- the generation of spurious warnings under ZFP run-time.
9173 Insert_Actions (E, List_Def, Suppress => All_Checks);
9174 return Equiv_Type;
9175 end Make_CW_Equivalent_Type;
9177 -------------------------
9178 -- Make_Invariant_Call --
9179 -------------------------
9181 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9182 Loc : constant Source_Ptr := Sloc (Expr);
9183 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9185 Proc_Id : Entity_Id;
9187 begin
9188 pragma Assert (Has_Invariants (Typ));
9190 Proc_Id := Invariant_Procedure (Typ);
9191 pragma Assert (Present (Proc_Id));
9193 return
9194 Make_Procedure_Call_Statement (Loc,
9195 Name => New_Occurrence_Of (Proc_Id, Loc),
9196 Parameter_Associations => New_List (Relocate_Node (Expr)));
9197 end Make_Invariant_Call;
9199 ------------------------
9200 -- Make_Literal_Range --
9201 ------------------------
9203 function Make_Literal_Range
9204 (Loc : Source_Ptr;
9205 Literal_Typ : Entity_Id) return Node_Id
9207 Lo : constant Node_Id :=
9208 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9209 Index : constant Entity_Id := Etype (Lo);
9210 Length_Expr : constant Node_Id :=
9211 Make_Op_Subtract (Loc,
9212 Left_Opnd =>
9213 Make_Integer_Literal (Loc,
9214 Intval => String_Literal_Length (Literal_Typ)),
9215 Right_Opnd => Make_Integer_Literal (Loc, 1));
9217 Hi : Node_Id;
9219 begin
9220 Set_Analyzed (Lo, False);
9222 if Is_Integer_Type (Index) then
9223 Hi :=
9224 Make_Op_Add (Loc,
9225 Left_Opnd => New_Copy_Tree (Lo),
9226 Right_Opnd => Length_Expr);
9227 else
9228 Hi :=
9229 Make_Attribute_Reference (Loc,
9230 Attribute_Name => Name_Val,
9231 Prefix => New_Occurrence_Of (Index, Loc),
9232 Expressions => New_List (
9233 Make_Op_Add (Loc,
9234 Left_Opnd =>
9235 Make_Attribute_Reference (Loc,
9236 Attribute_Name => Name_Pos,
9237 Prefix => New_Occurrence_Of (Index, Loc),
9238 Expressions => New_List (New_Copy_Tree (Lo))),
9239 Right_Opnd => Length_Expr)));
9240 end if;
9242 return
9243 Make_Range (Loc,
9244 Low_Bound => Lo,
9245 High_Bound => Hi);
9246 end Make_Literal_Range;
9248 --------------------------
9249 -- Make_Non_Empty_Check --
9250 --------------------------
9252 function Make_Non_Empty_Check
9253 (Loc : Source_Ptr;
9254 N : Node_Id) return Node_Id
9256 begin
9257 return
9258 Make_Op_Ne (Loc,
9259 Left_Opnd =>
9260 Make_Attribute_Reference (Loc,
9261 Attribute_Name => Name_Length,
9262 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9263 Right_Opnd =>
9264 Make_Integer_Literal (Loc, 0));
9265 end Make_Non_Empty_Check;
9267 -------------------------
9268 -- Make_Predicate_Call --
9269 -------------------------
9271 -- WARNING: This routine manages Ghost regions. Return statements must be
9272 -- replaced by gotos which jump to the end of the routine and restore the
9273 -- Ghost mode.
9275 function Make_Predicate_Call
9276 (Typ : Entity_Id;
9277 Expr : Node_Id;
9278 Mem : Boolean := False) return Node_Id
9280 Loc : constant Source_Ptr := Sloc (Expr);
9282 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9283 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9284 -- Save the Ghost-related attributes to restore on exit
9286 Call : Node_Id;
9287 Func_Id : Entity_Id;
9289 begin
9290 Func_Id := Predicate_Function (Typ);
9291 pragma Assert (Present (Func_Id));
9293 -- The related type may be subject to pragma Ghost. Set the mode now to
9294 -- ensure that the call is properly marked as Ghost.
9296 Set_Ghost_Mode (Typ);
9298 -- Call special membership version if requested and available
9300 if Mem and then Present (Predicate_Function_M (Typ)) then
9301 Func_Id := Predicate_Function_M (Typ);
9302 end if;
9304 -- Case of calling normal predicate function
9306 -- If the type is tagged, the expression may be class-wide, in which
9307 -- case it has to be converted to its root type, given that the
9308 -- generated predicate function is not dispatching.
9310 if Is_Tagged_Type (Typ) then
9311 Call :=
9312 Make_Function_Call (Loc,
9313 Name => New_Occurrence_Of (Func_Id, Loc),
9314 Parameter_Associations =>
9315 New_List (Convert_To (Typ, Relocate_Node (Expr))));
9316 else
9317 Call :=
9318 Make_Function_Call (Loc,
9319 Name => New_Occurrence_Of (Func_Id, Loc),
9320 Parameter_Associations => New_List (Relocate_Node (Expr)));
9321 end if;
9323 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9325 return Call;
9326 end Make_Predicate_Call;
9328 --------------------------
9329 -- Make_Predicate_Check --
9330 --------------------------
9332 function Make_Predicate_Check
9333 (Typ : Entity_Id;
9334 Expr : Node_Id) return Node_Id
9336 Loc : constant Source_Ptr := Sloc (Expr);
9338 procedure Add_Failure_Expression (Args : List_Id);
9339 -- Add the failure expression of pragma Predicate_Failure (if any) to
9340 -- list Args.
9342 ----------------------------
9343 -- Add_Failure_Expression --
9344 ----------------------------
9346 procedure Add_Failure_Expression (Args : List_Id) is
9347 function Failure_Expression return Node_Id;
9348 pragma Inline (Failure_Expression);
9349 -- Find aspect or pragma Predicate_Failure that applies to type Typ
9350 -- and return its expression. Return Empty if no such annotation is
9351 -- available.
9353 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean;
9354 pragma Inline (Is_OK_PF_Aspect);
9355 -- Determine whether aspect Asp is a suitable Predicate_Failure
9356 -- aspect that applies to type Typ.
9358 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean;
9359 pragma Inline (Is_OK_PF_Pragma);
9360 -- Determine whether pragma Prag is a suitable Predicate_Failure
9361 -- pragma that applies to type Typ.
9363 procedure Replace_Subtype_Reference (N : Node_Id);
9364 -- Replace the current instance of type Typ denoted by N with
9365 -- expression Expr.
9367 ------------------------
9368 -- Failure_Expression --
9369 ------------------------
9371 function Failure_Expression return Node_Id is
9372 Item : Node_Id;
9374 begin
9375 -- The management of the rep item chain involves "inheritance" of
9376 -- parent type chains. If a parent [sub]type is already subject to
9377 -- pragma Predicate_Failure, then the pragma will also appear in
9378 -- the chain of the child [sub]type, which in turn may possess a
9379 -- pragma of its own. Avoid order-dependent issues by inspecting
9380 -- the rep item chain directly. Note that routine Get_Pragma may
9381 -- return a parent pragma.
9383 Item := First_Rep_Item (Typ);
9384 while Present (Item) loop
9386 -- Predicate_Failure appears as an aspect
9388 if Nkind (Item) = N_Aspect_Specification
9389 and then Is_OK_PF_Aspect (Item)
9390 then
9391 return Expression (Item);
9393 -- Predicate_Failure appears as a pragma
9395 elsif Nkind (Item) = N_Pragma
9396 and then Is_OK_PF_Pragma (Item)
9397 then
9398 return
9399 Get_Pragma_Arg
9400 (Next (First (Pragma_Argument_Associations (Item))));
9401 end if;
9403 Item := Next_Rep_Item (Item);
9404 end loop;
9406 return Empty;
9407 end Failure_Expression;
9409 ---------------------
9410 -- Is_OK_PF_Aspect --
9411 ---------------------
9413 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean is
9414 begin
9415 -- To qualify, the aspect must apply to the type subjected to the
9416 -- predicate check.
9418 return
9419 Chars (Identifier (Asp)) = Name_Predicate_Failure
9420 and then Present (Entity (Asp))
9421 and then Entity (Asp) = Typ;
9422 end Is_OK_PF_Aspect;
9424 ---------------------
9425 -- Is_OK_PF_Pragma --
9426 ---------------------
9428 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean is
9429 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9430 Typ_Arg : Node_Id;
9432 begin
9433 -- Nothing to do when the pragma does not denote Predicate_Failure
9435 if Pragma_Name (Prag) /= Name_Predicate_Failure then
9436 return False;
9438 -- Nothing to do when the pragma lacks arguments, in which case it
9439 -- is illegal.
9441 elsif No (Args) or else Is_Empty_List (Args) then
9442 return False;
9443 end if;
9445 Typ_Arg := Get_Pragma_Arg (First (Args));
9447 -- To qualify, the local name argument of the pragma must denote
9448 -- the type subjected to the predicate check.
9450 return
9451 Is_Entity_Name (Typ_Arg)
9452 and then Present (Entity (Typ_Arg))
9453 and then Entity (Typ_Arg) = Typ;
9454 end Is_OK_PF_Pragma;
9456 --------------------------------
9457 -- Replace_Subtype_Reference --
9458 --------------------------------
9460 procedure Replace_Subtype_Reference (N : Node_Id) is
9461 begin
9462 Rewrite (N, New_Copy_Tree (Expr));
9464 -- We want to treat the node as if it comes from source, so that
9465 -- ASIS will not ignore it.
9467 Set_Comes_From_Source (N, True);
9468 end Replace_Subtype_Reference;
9470 procedure Replace_Subtype_References is
9471 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9473 -- Local variables
9475 PF_Expr : constant Node_Id := Failure_Expression;
9476 Expr : Node_Id;
9478 -- Start of processing for Add_Failure_Expression
9480 begin
9481 if Present (PF_Expr) then
9483 -- Replace any occurrences of the current instance of the type
9484 -- with the object subjected to the predicate check.
9486 Expr := New_Copy_Tree (PF_Expr);
9487 Replace_Subtype_References (Expr, Typ);
9489 -- The failure expression appears as the third argument of the
9490 -- Check pragma.
9492 Append_To (Args,
9493 Make_Pragma_Argument_Association (Loc,
9494 Expression => Expr));
9495 end if;
9496 end Add_Failure_Expression;
9498 -- Local variables
9500 Args : List_Id;
9501 Nam : Name_Id;
9503 -- Start of processing for Make_Predicate_Check
9505 begin
9506 -- If predicate checks are suppressed, then return a null statement. For
9507 -- this call, we check only the scope setting. If the caller wants to
9508 -- check a specific entity's setting, they must do it manually.
9510 if Predicate_Checks_Suppressed (Empty) then
9511 return Make_Null_Statement (Loc);
9512 end if;
9514 -- Do not generate a check within an internal subprogram (stream
9515 -- functions and the like, including including predicate functions).
9517 if Within_Internal_Subprogram then
9518 return Make_Null_Statement (Loc);
9519 end if;
9521 -- Compute proper name to use, we need to get this right so that the
9522 -- right set of check policies apply to the Check pragma we are making.
9524 if Has_Dynamic_Predicate_Aspect (Typ) then
9525 Nam := Name_Dynamic_Predicate;
9526 elsif Has_Static_Predicate_Aspect (Typ) then
9527 Nam := Name_Static_Predicate;
9528 else
9529 Nam := Name_Predicate;
9530 end if;
9532 Args := New_List (
9533 Make_Pragma_Argument_Association (Loc,
9534 Expression => Make_Identifier (Loc, Nam)),
9535 Make_Pragma_Argument_Association (Loc,
9536 Expression => Make_Predicate_Call (Typ, Expr)));
9538 -- If the subtype is subject to pragma Predicate_Failure, add the
9539 -- failure expression as an additional parameter.
9541 Add_Failure_Expression (Args);
9543 return
9544 Make_Pragma (Loc,
9545 Chars => Name_Check,
9546 Pragma_Argument_Associations => Args);
9547 end Make_Predicate_Check;
9549 ----------------------------
9550 -- Make_Subtype_From_Expr --
9551 ----------------------------
9553 -- 1. If Expr is an unconstrained array expression, creates
9554 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9556 -- 2. If Expr is a unconstrained discriminated type expression, creates
9557 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9559 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9561 function Make_Subtype_From_Expr
9562 (E : Node_Id;
9563 Unc_Typ : Entity_Id;
9564 Related_Id : Entity_Id := Empty) return Node_Id
9566 List_Constr : constant List_Id := New_List;
9567 Loc : constant Source_Ptr := Sloc (E);
9568 D : Entity_Id;
9569 Full_Exp : Node_Id;
9570 Full_Subtyp : Entity_Id;
9571 High_Bound : Entity_Id;
9572 Index_Typ : Entity_Id;
9573 Low_Bound : Entity_Id;
9574 Priv_Subtyp : Entity_Id;
9575 Utyp : Entity_Id;
9577 begin
9578 if Is_Private_Type (Unc_Typ)
9579 and then Has_Unknown_Discriminants (Unc_Typ)
9580 then
9581 -- The caller requests a unique external name for both the private
9582 -- and the full subtype.
9584 if Present (Related_Id) then
9585 Full_Subtyp :=
9586 Make_Defining_Identifier (Loc,
9587 Chars => New_External_Name (Chars (Related_Id), 'C'));
9588 Priv_Subtyp :=
9589 Make_Defining_Identifier (Loc,
9590 Chars => New_External_Name (Chars (Related_Id), 'P'));
9592 else
9593 Full_Subtyp := Make_Temporary (Loc, 'C');
9594 Priv_Subtyp := Make_Temporary (Loc, 'P');
9595 end if;
9597 -- Prepare the subtype completion. Use the base type to find the
9598 -- underlying type because the type may be a generic actual or an
9599 -- explicit subtype.
9601 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9603 Full_Exp :=
9604 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9605 Set_Parent (Full_Exp, Parent (E));
9607 Insert_Action (E,
9608 Make_Subtype_Declaration (Loc,
9609 Defining_Identifier => Full_Subtyp,
9610 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9612 -- Define the dummy private subtype
9614 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9615 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9616 Set_Scope (Priv_Subtyp, Full_Subtyp);
9617 Set_Is_Constrained (Priv_Subtyp);
9618 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9619 Set_Is_Itype (Priv_Subtyp);
9620 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9622 if Is_Tagged_Type (Priv_Subtyp) then
9623 Set_Class_Wide_Type
9624 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9625 Set_Direct_Primitive_Operations (Priv_Subtyp,
9626 Direct_Primitive_Operations (Unc_Typ));
9627 end if;
9629 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9631 return New_Occurrence_Of (Priv_Subtyp, Loc);
9633 elsif Is_Array_Type (Unc_Typ) then
9634 Index_Typ := First_Index (Unc_Typ);
9635 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9637 -- Capture the bounds of each index constraint in case the context
9638 -- is an object declaration of an unconstrained type initialized
9639 -- by a function call:
9641 -- Obj : Unconstr_Typ := Func_Call;
9643 -- This scenario requires secondary scope management and the index
9644 -- constraint cannot depend on the temporary used to capture the
9645 -- result of the function call.
9647 -- SS_Mark;
9648 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9649 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9650 -- Obj : S := Temp.all;
9651 -- SS_Release; -- Temp is gone at this point, bounds of S are
9652 -- -- non existent.
9654 -- Generate:
9655 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9657 Low_Bound := Make_Temporary (Loc, 'B');
9658 Insert_Action (E,
9659 Make_Object_Declaration (Loc,
9660 Defining_Identifier => Low_Bound,
9661 Object_Definition =>
9662 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9663 Constant_Present => True,
9664 Expression =>
9665 Make_Attribute_Reference (Loc,
9666 Prefix => Duplicate_Subexpr_No_Checks (E),
9667 Attribute_Name => Name_First,
9668 Expressions => New_List (
9669 Make_Integer_Literal (Loc, J)))));
9671 -- Generate:
9672 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9674 High_Bound := Make_Temporary (Loc, 'B');
9675 Insert_Action (E,
9676 Make_Object_Declaration (Loc,
9677 Defining_Identifier => High_Bound,
9678 Object_Definition =>
9679 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9680 Constant_Present => True,
9681 Expression =>
9682 Make_Attribute_Reference (Loc,
9683 Prefix => Duplicate_Subexpr_No_Checks (E),
9684 Attribute_Name => Name_Last,
9685 Expressions => New_List (
9686 Make_Integer_Literal (Loc, J)))));
9688 Append_To (List_Constr,
9689 Make_Range (Loc,
9690 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9691 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9693 Index_Typ := Next_Index (Index_Typ);
9694 end loop;
9696 elsif Is_Class_Wide_Type (Unc_Typ) then
9697 declare
9698 CW_Subtype : Entity_Id;
9699 EQ_Typ : Entity_Id := Empty;
9701 begin
9702 -- A class-wide equivalent type is not needed on VM targets
9703 -- because the VM back-ends handle the class-wide object
9704 -- initialization itself (and doesn't need or want the
9705 -- additional intermediate type to handle the assignment).
9707 if Expander_Active and then Tagged_Type_Expansion then
9709 -- If this is the class-wide type of a completion that is a
9710 -- record subtype, set the type of the class-wide type to be
9711 -- the full base type, for use in the expanded code for the
9712 -- equivalent type. Should this be done earlier when the
9713 -- completion is analyzed ???
9715 if Is_Private_Type (Etype (Unc_Typ))
9716 and then
9717 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9718 then
9719 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9720 end if;
9722 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9723 end if;
9725 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9726 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9727 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9729 return New_Occurrence_Of (CW_Subtype, Loc);
9730 end;
9732 -- Indefinite record type with discriminants
9734 else
9735 D := First_Discriminant (Unc_Typ);
9736 while Present (D) loop
9737 Append_To (List_Constr,
9738 Make_Selected_Component (Loc,
9739 Prefix => Duplicate_Subexpr_No_Checks (E),
9740 Selector_Name => New_Occurrence_Of (D, Loc)));
9742 Next_Discriminant (D);
9743 end loop;
9744 end if;
9746 return
9747 Make_Subtype_Indication (Loc,
9748 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9749 Constraint =>
9750 Make_Index_Or_Discriminant_Constraint (Loc,
9751 Constraints => List_Constr));
9752 end Make_Subtype_From_Expr;
9754 ---------------
9755 -- Map_Types --
9756 ---------------
9758 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9760 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9761 -- avoid deep indentation of code.
9763 -- NOTE: Routines which deal with discriminant mapping operate on the
9764 -- [underlying/record] full view of various types because those views
9765 -- contain all discriminants and stored constraints.
9767 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9768 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9769 -- overriding chain starting from Prim whose dispatching type is parent
9770 -- type Par_Typ and add a mapping between the result and primitive Prim.
9772 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9773 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9774 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9775 -- if no such primitive is available.
9777 function Build_Chain
9778 (Par_Typ : Entity_Id;
9779 Deriv_Typ : Entity_Id) return Elist_Id;
9780 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9781 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9782 -- list has the form:
9784 -- head tail
9785 -- v v
9786 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9788 -- Note that Par_Typ is not part of the resulting derivation chain
9790 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9791 -- Return the view of type Typ which could potentially contains either
9792 -- the discriminants or stored constraints of the type.
9794 function Find_Discriminant_Value
9795 (Discr : Entity_Id;
9796 Par_Typ : Entity_Id;
9797 Deriv_Typ : Entity_Id;
9798 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9799 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9800 -- in the derivation chain starting from parent type Par_Typ leading to
9801 -- derived type Deriv_Typ. The returned value is one of the following:
9803 -- * An entity which is either a discriminant or a non-discriminant
9804 -- name, and renames/constraints Discr.
9806 -- * An expression which constraints Discr
9808 -- Typ_Elmt is an element of the derivation chain created by routine
9809 -- Build_Chain and denotes the current ancestor being examined.
9811 procedure Map_Discriminants
9812 (Par_Typ : Entity_Id;
9813 Deriv_Typ : Entity_Id);
9814 -- Map each discriminant of type Par_Typ to a meaningful constraint
9815 -- from the point of view of type Deriv_Typ.
9817 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9818 -- Map each primitive of type Par_Typ to a corresponding primitive of
9819 -- type Deriv_Typ.
9821 -------------------
9822 -- Add_Primitive --
9823 -------------------
9825 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9826 Par_Prim : Entity_Id;
9828 begin
9829 -- Inspect the inheritance chain through the Alias attribute and the
9830 -- overriding chain through the Overridden_Operation looking for an
9831 -- ancestor primitive with the appropriate dispatching type.
9833 Par_Prim := Prim;
9834 while Present (Par_Prim) loop
9835 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9836 Par_Prim := Ancestor_Primitive (Par_Prim);
9837 end loop;
9839 -- Create a mapping of the form:
9841 -- parent type primitive -> derived type primitive
9843 if Present (Par_Prim) then
9844 Type_Map.Set (Par_Prim, Prim);
9845 end if;
9846 end Add_Primitive;
9848 ------------------------
9849 -- Ancestor_Primitive --
9850 ------------------------
9852 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9853 Inher_Prim : constant Entity_Id := Alias (Subp);
9854 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9856 begin
9857 -- The current subprogram overrides an ancestor primitive
9859 if Present (Over_Prim) then
9860 return Over_Prim;
9862 -- The current subprogram is an internally generated alias of an
9863 -- inherited ancestor primitive.
9865 elsif Present (Inher_Prim) then
9866 return Inher_Prim;
9868 -- Otherwise the current subprogram is the root of the inheritance or
9869 -- overriding chain.
9871 else
9872 return Empty;
9873 end if;
9874 end Ancestor_Primitive;
9876 -----------------
9877 -- Build_Chain --
9878 -----------------
9880 function Build_Chain
9881 (Par_Typ : Entity_Id;
9882 Deriv_Typ : Entity_Id) return Elist_Id
9884 Anc_Typ : Entity_Id;
9885 Chain : Elist_Id;
9886 Curr_Typ : Entity_Id;
9888 begin
9889 Chain := New_Elmt_List;
9891 -- Add the derived type to the derivation chain
9893 Prepend_Elmt (Deriv_Typ, Chain);
9895 -- Examine all ancestors starting from the derived type climbing
9896 -- towards parent type Par_Typ.
9898 Curr_Typ := Deriv_Typ;
9899 loop
9900 -- Handle the case where the current type is a record which
9901 -- derives from a subtype.
9903 -- subtype Sub_Typ is Par_Typ ...
9904 -- type Deriv_Typ is Sub_Typ ...
9906 if Ekind (Curr_Typ) = E_Record_Type
9907 and then Present (Parent_Subtype (Curr_Typ))
9908 then
9909 Anc_Typ := Parent_Subtype (Curr_Typ);
9911 -- Handle the case where the current type is a record subtype of
9912 -- another subtype.
9914 -- subtype Sub_Typ1 is Par_Typ ...
9915 -- subtype Sub_Typ2 is Sub_Typ1 ...
9917 elsif Ekind (Curr_Typ) = E_Record_Subtype
9918 and then Present (Cloned_Subtype (Curr_Typ))
9919 then
9920 Anc_Typ := Cloned_Subtype (Curr_Typ);
9922 -- Otherwise use the direct parent type
9924 else
9925 Anc_Typ := Etype (Curr_Typ);
9926 end if;
9928 -- Use the first subtype when dealing with itypes
9930 if Is_Itype (Anc_Typ) then
9931 Anc_Typ := First_Subtype (Anc_Typ);
9932 end if;
9934 -- Work with the view which contains the discriminants and stored
9935 -- constraints.
9937 Anc_Typ := Discriminated_View (Anc_Typ);
9939 -- Stop the climb when either the parent type has been reached or
9940 -- there are no more ancestors left to examine.
9942 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9944 Prepend_Unique_Elmt (Anc_Typ, Chain);
9945 Curr_Typ := Anc_Typ;
9946 end loop;
9948 return Chain;
9949 end Build_Chain;
9951 ------------------------
9952 -- Discriminated_View --
9953 ------------------------
9955 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9956 T : Entity_Id;
9958 begin
9959 T := Typ;
9961 -- Use the [underlying] full view when dealing with private types
9962 -- because the view contains all inherited discriminants or stored
9963 -- constraints.
9965 if Is_Private_Type (T) then
9966 if Present (Underlying_Full_View (T)) then
9967 T := Underlying_Full_View (T);
9969 elsif Present (Full_View (T)) then
9970 T := Full_View (T);
9971 end if;
9972 end if;
9974 -- Use the underlying record view when the type is an extenstion of
9975 -- a parent type with unknown discriminants because the view contains
9976 -- all inherited discriminants or stored constraints.
9978 if Ekind (T) = E_Record_Type
9979 and then Present (Underlying_Record_View (T))
9980 then
9981 T := Underlying_Record_View (T);
9982 end if;
9984 return T;
9985 end Discriminated_View;
9987 -----------------------------
9988 -- Find_Discriminant_Value --
9989 -----------------------------
9991 function Find_Discriminant_Value
9992 (Discr : Entity_Id;
9993 Par_Typ : Entity_Id;
9994 Deriv_Typ : Entity_Id;
9995 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9997 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9998 Typ : constant Entity_Id := Node (Typ_Elmt);
10000 function Find_Constraint_Value
10001 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
10002 -- Given constraint Constr, find what it denotes. This is either:
10004 -- * An entity which is either a discriminant or a name
10006 -- * An expression
10008 ---------------------------
10009 -- Find_Constraint_Value --
10010 ---------------------------
10012 function Find_Constraint_Value
10013 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
10015 begin
10016 if Nkind (Constr) in N_Entity then
10018 -- The constraint denotes a discriminant of the curren type
10019 -- which renames the ancestor discriminant:
10021 -- vv
10022 -- type Typ (D1 : ...; DN : ...) is
10023 -- new Anc (Discr => D1) with ...
10024 -- ^^
10026 if Ekind (Constr) = E_Discriminant then
10028 -- The discriminant belongs to derived type Deriv_Typ. This
10029 -- is the final value for the ancestor discriminant as the
10030 -- derivations chain has been fully exhausted.
10032 if Typ = Deriv_Typ then
10033 return Constr;
10035 -- Otherwise the discriminant may be renamed or constrained
10036 -- at a lower level. Continue looking down the derivation
10037 -- chain.
10039 else
10040 return
10041 Find_Discriminant_Value
10042 (Discr => Constr,
10043 Par_Typ => Par_Typ,
10044 Deriv_Typ => Deriv_Typ,
10045 Typ_Elmt => Next_Elmt (Typ_Elmt));
10046 end if;
10048 -- Otherwise the constraint denotes a reference to some name
10049 -- which results in a Girder discriminant:
10051 -- vvvv
10052 -- Name : ...;
10053 -- type Typ (D1 : ...; DN : ...) is
10054 -- new Anc (Discr => Name) with ...
10055 -- ^^^^
10057 -- Return the name as this is the proper constraint of the
10058 -- discriminant.
10060 else
10061 return Constr;
10062 end if;
10064 -- The constraint denotes a reference to a name
10066 elsif Is_Entity_Name (Constr) then
10067 return Find_Constraint_Value (Entity (Constr));
10069 -- Otherwise the current constraint is an expression which yields
10070 -- a Girder discriminant:
10072 -- type Typ (D1 : ...; DN : ...) is
10073 -- new Anc (Discr => <expression>) with ...
10074 -- ^^^^^^^^^^
10076 -- Return the expression as this is the proper constraint of the
10077 -- discriminant.
10079 else
10080 return Constr;
10081 end if;
10082 end Find_Constraint_Value;
10084 -- Local variables
10086 Constrs : constant Elist_Id := Stored_Constraint (Typ);
10088 Constr_Elmt : Elmt_Id;
10089 Pos : Uint;
10090 Typ_Discr : Entity_Id;
10092 -- Start of processing for Find_Discriminant_Value
10094 begin
10095 -- The algorithm for finding the value of a discriminant works as
10096 -- follows. First, it recreates the derivation chain from Par_Typ
10097 -- to Deriv_Typ as a list:
10099 -- Par_Typ (shown for completeness)
10100 -- v
10101 -- Ancestor_N <-- head of chain
10102 -- v
10103 -- Ancestor_1
10104 -- v
10105 -- Deriv_Typ <-- tail of chain
10107 -- The algorithm then traces the fate of a parent discriminant down
10108 -- the derivation chain. At each derivation level, the discriminant
10109 -- may be either inherited or constrained.
10111 -- 1) Discriminant is inherited: there are two cases, depending on
10112 -- which type is inheriting.
10114 -- 1.1) Deriv_Typ is inheriting:
10116 -- type Ancestor (D_1 : ...) is tagged ...
10117 -- type Deriv_Typ is new Ancestor ...
10119 -- In this case the inherited discriminant is the final value of
10120 -- the parent discriminant because the end of the derivation chain
10121 -- has been reached.
10123 -- 1.2) Some other type is inheriting:
10125 -- type Ancestor_1 (D_1 : ...) is tagged ...
10126 -- type Ancestor_2 is new Ancestor_1 ...
10128 -- In this case the algorithm continues to trace the fate of the
10129 -- inherited discriminant down the derivation chain because it may
10130 -- be further inherited or constrained.
10132 -- 2) Discriminant is constrained: there are three cases, depending
10133 -- on what the constraint is.
10135 -- 2.1) The constraint is another discriminant (aka renaming):
10137 -- type Ancestor_1 (D_1 : ...) is tagged ...
10138 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10140 -- In this case the constraining discriminant becomes the one to
10141 -- track down the derivation chain. The algorithm already knows
10142 -- that D_2 constrains D_1, therefore if the algorithm finds the
10143 -- value of D_2, then this would also be the value for D_1.
10145 -- 2.2) The constraint is a name (aka Girder):
10147 -- Name : ...
10148 -- type Ancestor_1 (D_1 : ...) is tagged ...
10149 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10151 -- In this case the name is the final value of D_1 because the
10152 -- discriminant cannot be further constrained.
10154 -- 2.3) The constraint is an expression (aka Girder):
10156 -- type Ancestor_1 (D_1 : ...) is tagged ...
10157 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10159 -- Similar to 2.2, the expression is the final value of D_1
10161 Pos := Uint_1;
10163 -- When a derived type constrains its parent type, all constaints
10164 -- appear in the Stored_Constraint list. Examine the list looking
10165 -- for a positional match.
10167 if Present (Constrs) then
10168 Constr_Elmt := First_Elmt (Constrs);
10169 while Present (Constr_Elmt) loop
10171 -- The position of the current constraint matches that of the
10172 -- ancestor discriminant.
10174 if Pos = Discr_Pos then
10175 return Find_Constraint_Value (Node (Constr_Elmt));
10176 end if;
10178 Next_Elmt (Constr_Elmt);
10179 Pos := Pos + 1;
10180 end loop;
10182 -- Otherwise the derived type does not constraint its parent type in
10183 -- which case it inherits the parent discriminants.
10185 else
10186 Typ_Discr := First_Discriminant (Typ);
10187 while Present (Typ_Discr) loop
10189 -- The position of the current discriminant matches that of the
10190 -- ancestor discriminant.
10192 if Pos = Discr_Pos then
10193 return Find_Constraint_Value (Typ_Discr);
10194 end if;
10196 Next_Discriminant (Typ_Discr);
10197 Pos := Pos + 1;
10198 end loop;
10199 end if;
10201 -- A discriminant must always have a corresponding value. This is
10202 -- either another discriminant, a name, or an expression. If this
10203 -- point is reached, them most likely the derivation chain employs
10204 -- the wrong views of types.
10206 pragma Assert (False);
10208 return Empty;
10209 end Find_Discriminant_Value;
10211 -----------------------
10212 -- Map_Discriminants --
10213 -----------------------
10215 procedure Map_Discriminants
10216 (Par_Typ : Entity_Id;
10217 Deriv_Typ : Entity_Id)
10219 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10221 Discr : Entity_Id;
10222 Discr_Val : Node_Or_Entity_Id;
10224 begin
10225 -- Examine each discriminant of parent type Par_Typ and find a
10226 -- suitable value for it from the point of view of derived type
10227 -- Deriv_Typ.
10229 if Has_Discriminants (Par_Typ) then
10230 Discr := First_Discriminant (Par_Typ);
10231 while Present (Discr) loop
10232 Discr_Val :=
10233 Find_Discriminant_Value
10234 (Discr => Discr,
10235 Par_Typ => Par_Typ,
10236 Deriv_Typ => Deriv_Typ,
10237 Typ_Elmt => First_Elmt (Deriv_Chain));
10239 -- Create a mapping of the form:
10241 -- parent type discriminant -> value
10243 Type_Map.Set (Discr, Discr_Val);
10245 Next_Discriminant (Discr);
10246 end loop;
10247 end if;
10248 end Map_Discriminants;
10250 --------------------
10251 -- Map_Primitives --
10252 --------------------
10254 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10255 Deriv_Prim : Entity_Id;
10256 Par_Prim : Entity_Id;
10257 Par_Prims : Elist_Id;
10258 Prim_Elmt : Elmt_Id;
10260 begin
10261 -- Inspect the primitives of the derived type and determine whether
10262 -- they relate to the primitives of the parent type. If there is a
10263 -- meaningful relation, create a mapping of the form:
10265 -- parent type primitive -> perived type primitive
10267 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10268 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10269 while Present (Prim_Elmt) loop
10270 Deriv_Prim := Node (Prim_Elmt);
10272 if Is_Subprogram (Deriv_Prim)
10273 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10274 then
10275 Add_Primitive (Deriv_Prim, Par_Typ);
10276 end if;
10278 Next_Elmt (Prim_Elmt);
10279 end loop;
10280 end if;
10282 -- If the parent operation is an interface operation, the overriding
10283 -- indicator is not present. Instead, we get from the interface
10284 -- operation the primitive of the current type that implements it.
10286 if Is_Interface (Par_Typ) then
10287 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10289 if Present (Par_Prims) then
10290 Prim_Elmt := First_Elmt (Par_Prims);
10292 while Present (Prim_Elmt) loop
10293 Par_Prim := Node (Prim_Elmt);
10294 Deriv_Prim :=
10295 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10297 if Present (Deriv_Prim) then
10298 Type_Map.Set (Par_Prim, Deriv_Prim);
10299 end if;
10301 Next_Elmt (Prim_Elmt);
10302 end loop;
10303 end if;
10304 end if;
10305 end Map_Primitives;
10307 -- Start of processing for Map_Types
10309 begin
10310 -- Nothing to do if there are no types to work with
10312 if No (Parent_Type) or else No (Derived_Type) then
10313 return;
10315 -- Nothing to do if the mapping already exists
10317 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10318 return;
10320 -- Nothing to do if both types are not tagged. Note that untagged types
10321 -- do not have primitive operations and their discriminants are already
10322 -- handled by gigi.
10324 elsif not Is_Tagged_Type (Parent_Type)
10325 or else not Is_Tagged_Type (Derived_Type)
10326 then
10327 return;
10328 end if;
10330 -- Create a mapping of the form
10332 -- parent type -> derived type
10334 -- to prevent any subsequent attempts to produce the same relations
10336 Type_Map.Set (Parent_Type, Derived_Type);
10338 -- Create mappings of the form
10340 -- parent type discriminant -> derived type discriminant
10341 -- <or>
10342 -- parent type discriminant -> constraint
10344 -- Note that mapping of discriminants breaks privacy because it needs to
10345 -- work with those views which contains the discriminants and any stored
10346 -- constraints.
10348 Map_Discriminants
10349 (Par_Typ => Discriminated_View (Parent_Type),
10350 Deriv_Typ => Discriminated_View (Derived_Type));
10352 -- Create mappings of the form
10354 -- parent type primitive -> derived type primitive
10356 Map_Primitives
10357 (Par_Typ => Parent_Type,
10358 Deriv_Typ => Derived_Type);
10359 end Map_Types;
10361 ----------------------------
10362 -- Matching_Standard_Type --
10363 ----------------------------
10365 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10366 pragma Assert (Is_Scalar_Type (Typ));
10367 Siz : constant Uint := Esize (Typ);
10369 begin
10370 -- Floating-point cases
10372 if Is_Floating_Point_Type (Typ) then
10373 if Siz <= Esize (Standard_Short_Float) then
10374 return Standard_Short_Float;
10375 elsif Siz <= Esize (Standard_Float) then
10376 return Standard_Float;
10377 elsif Siz <= Esize (Standard_Long_Float) then
10378 return Standard_Long_Float;
10379 elsif Siz <= Esize (Standard_Long_Long_Float) then
10380 return Standard_Long_Long_Float;
10381 else
10382 raise Program_Error;
10383 end if;
10385 -- Integer cases (includes fixed-point types)
10387 -- Unsigned integer cases (includes normal enumeration types)
10389 elsif Is_Unsigned_Type (Typ) then
10390 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10391 return Standard_Short_Short_Unsigned;
10392 elsif Siz <= Esize (Standard_Short_Unsigned) then
10393 return Standard_Short_Unsigned;
10394 elsif Siz <= Esize (Standard_Unsigned) then
10395 return Standard_Unsigned;
10396 elsif Siz <= Esize (Standard_Long_Unsigned) then
10397 return Standard_Long_Unsigned;
10398 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10399 return Standard_Long_Long_Unsigned;
10400 else
10401 raise Program_Error;
10402 end if;
10404 -- Signed integer cases
10406 else
10407 if Siz <= Esize (Standard_Short_Short_Integer) then
10408 return Standard_Short_Short_Integer;
10409 elsif Siz <= Esize (Standard_Short_Integer) then
10410 return Standard_Short_Integer;
10411 elsif Siz <= Esize (Standard_Integer) then
10412 return Standard_Integer;
10413 elsif Siz <= Esize (Standard_Long_Integer) then
10414 return Standard_Long_Integer;
10415 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10416 return Standard_Long_Long_Integer;
10417 else
10418 raise Program_Error;
10419 end if;
10420 end if;
10421 end Matching_Standard_Type;
10423 -----------------------------
10424 -- May_Generate_Large_Temp --
10425 -----------------------------
10427 -- At the current time, the only types that we return False for (i.e. where
10428 -- we decide we know they cannot generate large temps) are ones where we
10429 -- know the size is 256 bits or less at compile time, and we are still not
10430 -- doing a thorough job on arrays and records ???
10432 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10433 begin
10434 if not Size_Known_At_Compile_Time (Typ) then
10435 return False;
10437 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10438 return False;
10440 elsif Is_Array_Type (Typ)
10441 and then Present (Packed_Array_Impl_Type (Typ))
10442 then
10443 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10445 -- We could do more here to find other small types ???
10447 else
10448 return True;
10449 end if;
10450 end May_Generate_Large_Temp;
10452 --------------------------------------------
10453 -- Needs_Conditional_Null_Excluding_Check --
10454 --------------------------------------------
10456 function Needs_Conditional_Null_Excluding_Check
10457 (Typ : Entity_Id) return Boolean
10459 begin
10460 return
10461 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
10462 end Needs_Conditional_Null_Excluding_Check;
10464 ----------------------------
10465 -- Needs_Constant_Address --
10466 ----------------------------
10468 function Needs_Constant_Address
10469 (Decl : Node_Id;
10470 Typ : Entity_Id) return Boolean
10472 begin
10473 -- If we have no initialization of any kind, then we don't need to place
10474 -- any restrictions on the address clause, because the object will be
10475 -- elaborated after the address clause is evaluated. This happens if the
10476 -- declaration has no initial expression, or the type has no implicit
10477 -- initialization, or the object is imported.
10479 -- The same holds for all initialized scalar types and all access types.
10480 -- Packed bit arrays of size up to 64 are represented using a modular
10481 -- type with an initialization (to zero) and can be processed like other
10482 -- initialized scalar types.
10484 -- If the type is controlled, code to attach the object to a
10485 -- finalization chain is generated at the point of declaration, and
10486 -- therefore the elaboration of the object cannot be delayed: the
10487 -- address expression must be a constant.
10489 if No (Expression (Decl))
10490 and then not Needs_Finalization (Typ)
10491 and then
10492 (not Has_Non_Null_Base_Init_Proc (Typ)
10493 or else Is_Imported (Defining_Identifier (Decl)))
10494 then
10495 return False;
10497 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10498 or else Is_Access_Type (Typ)
10499 or else
10500 (Is_Bit_Packed_Array (Typ)
10501 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10502 then
10503 return False;
10505 else
10506 -- Otherwise, we require the address clause to be constant because
10507 -- the call to the initialization procedure (or the attach code) has
10508 -- to happen at the point of the declaration.
10510 -- Actually the IP call has been moved to the freeze actions anyway,
10511 -- so maybe we can relax this restriction???
10513 return True;
10514 end if;
10515 end Needs_Constant_Address;
10517 ------------------------
10518 -- Needs_Finalization --
10519 ------------------------
10521 function Needs_Finalization (Typ : Entity_Id) return Boolean is
10522 function Has_Some_Controlled_Component
10523 (Input_Typ : Entity_Id) return Boolean;
10524 -- Determine whether type Input_Typ has at least one controlled
10525 -- component.
10527 -----------------------------------
10528 -- Has_Some_Controlled_Component --
10529 -----------------------------------
10531 function Has_Some_Controlled_Component
10532 (Input_Typ : Entity_Id) return Boolean
10534 Comp : Entity_Id;
10536 begin
10537 -- When a type is already frozen and has at least one controlled
10538 -- component, or is manually decorated, it is sufficient to inspect
10539 -- flag Has_Controlled_Component.
10541 if Has_Controlled_Component (Input_Typ) then
10542 return True;
10544 -- Otherwise inspect the internals of the type
10546 elsif not Is_Frozen (Input_Typ) then
10547 if Is_Array_Type (Input_Typ) then
10548 return Needs_Finalization (Component_Type (Input_Typ));
10550 elsif Is_Record_Type (Input_Typ) then
10551 Comp := First_Component (Input_Typ);
10552 while Present (Comp) loop
10553 if Needs_Finalization (Etype (Comp)) then
10554 return True;
10555 end if;
10557 Next_Component (Comp);
10558 end loop;
10559 end if;
10560 end if;
10562 return False;
10563 end Has_Some_Controlled_Component;
10565 -- Start of processing for Needs_Finalization
10567 begin
10568 -- Certain run-time configurations and targets do not provide support
10569 -- for controlled types.
10571 if Restriction_Active (No_Finalization) then
10572 return False;
10574 -- C++ types are not considered controlled. It is assumed that the non-
10575 -- Ada side will handle their clean up.
10577 elsif Convention (Typ) = Convention_CPP then
10578 return False;
10580 -- Class-wide types are treated as controlled because derivations from
10581 -- the root type may introduce controlled components.
10583 elsif Is_Class_Wide_Type (Typ) then
10584 return True;
10586 -- Concurrent types are controlled as long as their corresponding record
10587 -- is controlled.
10589 elsif Is_Concurrent_Type (Typ)
10590 and then Present (Corresponding_Record_Type (Typ))
10591 and then Needs_Finalization (Corresponding_Record_Type (Typ))
10592 then
10593 return True;
10595 -- Otherwise the type is controlled when it is either derived from type
10596 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10597 -- contains at least one controlled component.
10599 else
10600 return
10601 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
10602 end if;
10603 end Needs_Finalization;
10605 ----------------------------
10606 -- New_Class_Wide_Subtype --
10607 ----------------------------
10609 function New_Class_Wide_Subtype
10610 (CW_Typ : Entity_Id;
10611 N : Node_Id) return Entity_Id
10613 Res : constant Entity_Id := Create_Itype (E_Void, N);
10615 -- Capture relevant attributes of the class-wide subtype which must be
10616 -- restored after the copy.
10618 Res_Chars : constant Name_Id := Chars (Res);
10619 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
10620 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
10621 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
10622 Res_Scope : constant Entity_Id := Scope (Res);
10624 begin
10625 Copy_Node (CW_Typ, Res);
10627 -- Restore the relevant attributes of the class-wide subtype
10629 Set_Chars (Res, Res_Chars);
10630 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
10631 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
10632 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
10633 Set_Scope (Res, Res_Scope);
10635 -- Decorate the class-wide subtype
10637 Set_Associated_Node_For_Itype (Res, N);
10638 Set_Comes_From_Source (Res, False);
10639 Set_Ekind (Res, E_Class_Wide_Subtype);
10640 Set_Etype (Res, Base_Type (CW_Typ));
10641 Set_Freeze_Node (Res, Empty);
10642 Set_Is_Frozen (Res, False);
10643 Set_Is_Itype (Res);
10644 Set_Is_Public (Res, False);
10645 Set_Next_Entity (Res, Empty);
10646 Set_Prev_Entity (Res, Empty);
10647 Set_Sloc (Res, Sloc (N));
10649 Set_Public_Status (Res);
10651 return Res;
10652 end New_Class_Wide_Subtype;
10654 --------------------------------
10655 -- Non_Limited_Designated_Type --
10656 ---------------------------------
10658 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10659 Desig : constant Entity_Id := Designated_Type (T);
10660 begin
10661 if Has_Non_Limited_View (Desig) then
10662 return Non_Limited_View (Desig);
10663 else
10664 return Desig;
10665 end if;
10666 end Non_Limited_Designated_Type;
10668 -----------------------------------
10669 -- OK_To_Do_Constant_Replacement --
10670 -----------------------------------
10672 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10673 ES : constant Entity_Id := Scope (E);
10674 CS : Entity_Id;
10676 begin
10677 -- Do not replace statically allocated objects, because they may be
10678 -- modified outside the current scope.
10680 if Is_Statically_Allocated (E) then
10681 return False;
10683 -- Do not replace aliased or volatile objects, since we don't know what
10684 -- else might change the value.
10686 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10687 return False;
10689 -- Debug flag -gnatdM disconnects this optimization
10691 elsif Debug_Flag_MM then
10692 return False;
10694 -- Otherwise check scopes
10696 else
10697 CS := Current_Scope;
10699 loop
10700 -- If we are in right scope, replacement is safe
10702 if CS = ES then
10703 return True;
10705 -- Packages do not affect the determination of safety
10707 elsif Ekind (CS) = E_Package then
10708 exit when CS = Standard_Standard;
10709 CS := Scope (CS);
10711 -- Blocks do not affect the determination of safety
10713 elsif Ekind (CS) = E_Block then
10714 CS := Scope (CS);
10716 -- Loops do not affect the determination of safety. Note that we
10717 -- kill all current values on entry to a loop, so we are just
10718 -- talking about processing within a loop here.
10720 elsif Ekind (CS) = E_Loop then
10721 CS := Scope (CS);
10723 -- Otherwise, the reference is dubious, and we cannot be sure that
10724 -- it is safe to do the replacement.
10726 else
10727 exit;
10728 end if;
10729 end loop;
10731 return False;
10732 end if;
10733 end OK_To_Do_Constant_Replacement;
10735 ------------------------------------
10736 -- Possible_Bit_Aligned_Component --
10737 ------------------------------------
10739 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10740 begin
10741 -- Do not process an unanalyzed node because it is not yet decorated and
10742 -- most checks performed below will fail.
10744 if not Analyzed (N) then
10745 return False;
10746 end if;
10748 -- There are never alignment issues in CodePeer mode
10750 if CodePeer_Mode then
10751 return False;
10752 end if;
10754 case Nkind (N) is
10756 -- Case of indexed component
10758 when N_Indexed_Component =>
10759 declare
10760 P : constant Node_Id := Prefix (N);
10761 Ptyp : constant Entity_Id := Etype (P);
10763 begin
10764 -- If we know the component size and it is less than 64, then
10765 -- we are definitely OK. The back end always does assignment of
10766 -- misaligned small objects correctly.
10768 if Known_Static_Component_Size (Ptyp)
10769 and then Component_Size (Ptyp) <= 64
10770 then
10771 return False;
10773 -- Otherwise, we need to test the prefix, to see if we are
10774 -- indexing from a possibly unaligned component.
10776 else
10777 return Possible_Bit_Aligned_Component (P);
10778 end if;
10779 end;
10781 -- Case of selected component
10783 when N_Selected_Component =>
10784 declare
10785 P : constant Node_Id := Prefix (N);
10786 Comp : constant Entity_Id := Entity (Selector_Name (N));
10788 begin
10789 -- If there is no component clause, then we are in the clear
10790 -- since the back end will never misalign a large component
10791 -- unless it is forced to do so. In the clear means we need
10792 -- only the recursive test on the prefix.
10794 if Component_May_Be_Bit_Aligned (Comp) then
10795 return True;
10796 else
10797 return Possible_Bit_Aligned_Component (P);
10798 end if;
10799 end;
10801 -- For a slice, test the prefix, if that is possibly misaligned,
10802 -- then for sure the slice is.
10804 when N_Slice =>
10805 return Possible_Bit_Aligned_Component (Prefix (N));
10807 -- For an unchecked conversion, check whether the expression may
10808 -- be bit-aligned.
10810 when N_Unchecked_Type_Conversion =>
10811 return Possible_Bit_Aligned_Component (Expression (N));
10813 -- If we have none of the above, it means that we have fallen off the
10814 -- top testing prefixes recursively, and we now have a stand alone
10815 -- object, where we don't have a problem, unless this is a renaming,
10816 -- in which case we need to look into the renamed object.
10818 when others =>
10819 if Is_Entity_Name (N)
10820 and then Present (Renamed_Object (Entity (N)))
10821 then
10822 return
10823 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10824 else
10825 return False;
10826 end if;
10827 end case;
10828 end Possible_Bit_Aligned_Component;
10830 -----------------------------------------------
10831 -- Process_Statements_For_Controlled_Objects --
10832 -----------------------------------------------
10834 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10835 Loc : constant Source_Ptr := Sloc (N);
10837 function Are_Wrapped (L : List_Id) return Boolean;
10838 -- Determine whether list L contains only one statement which is a block
10840 function Wrap_Statements_In_Block
10841 (L : List_Id;
10842 Scop : Entity_Id := Current_Scope) return Node_Id;
10843 -- Given a list of statements L, wrap it in a block statement and return
10844 -- the generated node. Scop is either the current scope or the scope of
10845 -- the context (if applicable).
10847 -----------------
10848 -- Are_Wrapped --
10849 -----------------
10851 function Are_Wrapped (L : List_Id) return Boolean is
10852 Stmt : constant Node_Id := First (L);
10853 begin
10854 return
10855 Present (Stmt)
10856 and then No (Next (Stmt))
10857 and then Nkind (Stmt) = N_Block_Statement;
10858 end Are_Wrapped;
10860 ------------------------------
10861 -- Wrap_Statements_In_Block --
10862 ------------------------------
10864 function Wrap_Statements_In_Block
10865 (L : List_Id;
10866 Scop : Entity_Id := Current_Scope) return Node_Id
10868 Block_Id : Entity_Id;
10869 Block_Nod : Node_Id;
10870 Iter_Loop : Entity_Id;
10872 begin
10873 Block_Nod :=
10874 Make_Block_Statement (Loc,
10875 Declarations => No_List,
10876 Handled_Statement_Sequence =>
10877 Make_Handled_Sequence_Of_Statements (Loc,
10878 Statements => L));
10880 -- Create a label for the block in case the block needs to manage the
10881 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10883 Add_Block_Identifier (Block_Nod, Block_Id);
10885 -- When wrapping the statements of an iterator loop, check whether
10886 -- the loop requires secondary stack management and if so, propagate
10887 -- the appropriate flags to the block. This ensures that the cursor
10888 -- is properly cleaned up at each iteration of the loop.
10890 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10892 if Present (Iter_Loop) then
10893 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10895 -- Secondary stack reclamation is suppressed when the associated
10896 -- iterator loop contains a return statement which uses the stack.
10898 Set_Sec_Stack_Needed_For_Return
10899 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10900 end if;
10902 return Block_Nod;
10903 end Wrap_Statements_In_Block;
10905 -- Local variables
10907 Block : Node_Id;
10909 -- Start of processing for Process_Statements_For_Controlled_Objects
10911 begin
10912 -- Whenever a non-handled statement list is wrapped in a block, the
10913 -- block must be explicitly analyzed to redecorate all entities in the
10914 -- list and ensure that a finalizer is properly built.
10916 case Nkind (N) is
10917 when N_Conditional_Entry_Call
10918 | N_Elsif_Part
10919 | N_If_Statement
10920 | N_Selective_Accept
10922 -- Check the "then statements" for elsif parts and if statements
10924 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10925 and then not Is_Empty_List (Then_Statements (N))
10926 and then not Are_Wrapped (Then_Statements (N))
10927 and then Requires_Cleanup_Actions
10928 (L => Then_Statements (N),
10929 Lib_Level => False,
10930 Nested_Constructs => False)
10931 then
10932 Block := Wrap_Statements_In_Block (Then_Statements (N));
10933 Set_Then_Statements (N, New_List (Block));
10935 Analyze (Block);
10936 end if;
10938 -- Check the "else statements" for conditional entry calls, if
10939 -- statements and selective accepts.
10941 if Nkind_In (N, N_Conditional_Entry_Call,
10942 N_If_Statement,
10943 N_Selective_Accept)
10944 and then not Is_Empty_List (Else_Statements (N))
10945 and then not Are_Wrapped (Else_Statements (N))
10946 and then Requires_Cleanup_Actions
10947 (L => Else_Statements (N),
10948 Lib_Level => False,
10949 Nested_Constructs => False)
10950 then
10951 Block := Wrap_Statements_In_Block (Else_Statements (N));
10952 Set_Else_Statements (N, New_List (Block));
10954 Analyze (Block);
10955 end if;
10957 when N_Abortable_Part
10958 | N_Accept_Alternative
10959 | N_Case_Statement_Alternative
10960 | N_Delay_Alternative
10961 | N_Entry_Call_Alternative
10962 | N_Exception_Handler
10963 | N_Loop_Statement
10964 | N_Triggering_Alternative
10966 if not Is_Empty_List (Statements (N))
10967 and then not Are_Wrapped (Statements (N))
10968 and then Requires_Cleanup_Actions
10969 (L => Statements (N),
10970 Lib_Level => False,
10971 Nested_Constructs => False)
10972 then
10973 if Nkind (N) = N_Loop_Statement
10974 and then Present (Identifier (N))
10975 then
10976 Block :=
10977 Wrap_Statements_In_Block
10978 (L => Statements (N),
10979 Scop => Entity (Identifier (N)));
10980 else
10981 Block := Wrap_Statements_In_Block (Statements (N));
10982 end if;
10984 Set_Statements (N, New_List (Block));
10985 Analyze (Block);
10986 end if;
10988 -- Could be e.g. a loop that was transformed into a block or null
10989 -- statement. Do nothing for terminate alternatives.
10991 when N_Block_Statement
10992 | N_Null_Statement
10993 | N_Terminate_Alternative
10995 null;
10997 when others =>
10998 raise Program_Error;
10999 end case;
11000 end Process_Statements_For_Controlled_Objects;
11002 ------------------
11003 -- Power_Of_Two --
11004 ------------------
11006 function Power_Of_Two (N : Node_Id) return Nat is
11007 Typ : constant Entity_Id := Etype (N);
11008 pragma Assert (Is_Integer_Type (Typ));
11010 Siz : constant Nat := UI_To_Int (Esize (Typ));
11011 Val : Uint;
11013 begin
11014 if not Compile_Time_Known_Value (N) then
11015 return 0;
11017 else
11018 Val := Expr_Value (N);
11019 for J in 1 .. Siz - 1 loop
11020 if Val = Uint_2 ** J then
11021 return J;
11022 end if;
11023 end loop;
11025 return 0;
11026 end if;
11027 end Power_Of_Two;
11029 ----------------------
11030 -- Remove_Init_Call --
11031 ----------------------
11033 function Remove_Init_Call
11034 (Var : Entity_Id;
11035 Rep_Clause : Node_Id) return Node_Id
11037 Par : constant Node_Id := Parent (Var);
11038 Typ : constant Entity_Id := Etype (Var);
11040 Init_Proc : Entity_Id;
11041 -- Initialization procedure for Typ
11043 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
11044 -- Look for init call for Var starting at From and scanning the
11045 -- enclosing list until Rep_Clause or the end of the list is reached.
11047 ----------------------------
11048 -- Find_Init_Call_In_List --
11049 ----------------------------
11051 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
11052 Init_Call : Node_Id;
11054 begin
11055 Init_Call := From;
11056 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
11057 if Nkind (Init_Call) = N_Procedure_Call_Statement
11058 and then Is_Entity_Name (Name (Init_Call))
11059 and then Entity (Name (Init_Call)) = Init_Proc
11060 then
11061 return Init_Call;
11062 end if;
11064 Next (Init_Call);
11065 end loop;
11067 return Empty;
11068 end Find_Init_Call_In_List;
11070 Init_Call : Node_Id;
11072 -- Start of processing for Find_Init_Call
11074 begin
11075 if Present (Initialization_Statements (Var)) then
11076 Init_Call := Initialization_Statements (Var);
11077 Set_Initialization_Statements (Var, Empty);
11079 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
11081 -- No init proc for the type, so obviously no call to be found
11083 return Empty;
11085 else
11086 -- We might be able to handle other cases below by just properly
11087 -- setting Initialization_Statements at the point where the init proc
11088 -- call is generated???
11090 Init_Proc := Base_Init_Proc (Typ);
11092 -- First scan the list containing the declaration of Var
11094 Init_Call := Find_Init_Call_In_List (From => Next (Par));
11096 -- If not found, also look on Var's freeze actions list, if any,
11097 -- since the init call may have been moved there (case of an address
11098 -- clause applying to Var).
11100 if No (Init_Call) and then Present (Freeze_Node (Var)) then
11101 Init_Call :=
11102 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
11103 end if;
11105 -- If the initialization call has actuals that use the secondary
11106 -- stack, the call may have been wrapped into a temporary block, in
11107 -- which case the block itself has to be removed.
11109 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
11110 declare
11111 Blk : constant Node_Id := Next (Par);
11112 begin
11113 if Present
11114 (Find_Init_Call_In_List
11115 (First (Statements (Handled_Statement_Sequence (Blk)))))
11116 then
11117 Init_Call := Blk;
11118 end if;
11119 end;
11120 end if;
11121 end if;
11123 if Present (Init_Call) then
11124 Remove (Init_Call);
11125 end if;
11126 return Init_Call;
11127 end Remove_Init_Call;
11129 -------------------------
11130 -- Remove_Side_Effects --
11131 -------------------------
11133 procedure Remove_Side_Effects
11134 (Exp : Node_Id;
11135 Name_Req : Boolean := False;
11136 Renaming_Req : Boolean := False;
11137 Variable_Ref : Boolean := False;
11138 Related_Id : Entity_Id := Empty;
11139 Is_Low_Bound : Boolean := False;
11140 Is_High_Bound : Boolean := False;
11141 Check_Side_Effects : Boolean := True)
11143 function Build_Temporary
11144 (Loc : Source_Ptr;
11145 Id : Character;
11146 Related_Nod : Node_Id := Empty) return Entity_Id;
11147 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11148 -- is present (xxx is taken from the Chars field of Related_Nod),
11149 -- otherwise it generates an internal temporary. The created temporary
11150 -- entity is marked as internal.
11152 ---------------------
11153 -- Build_Temporary --
11154 ---------------------
11156 function Build_Temporary
11157 (Loc : Source_Ptr;
11158 Id : Character;
11159 Related_Nod : Node_Id := Empty) return Entity_Id
11161 Temp_Id : Entity_Id;
11162 Temp_Nam : Name_Id;
11164 begin
11165 -- The context requires an external symbol
11167 if Present (Related_Id) then
11168 if Is_Low_Bound then
11169 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
11170 else pragma Assert (Is_High_Bound);
11171 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
11172 end if;
11174 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
11176 -- Otherwise generate an internal temporary
11178 else
11179 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
11180 end if;
11182 Set_Is_Internal (Temp_Id);
11184 return Temp_Id;
11185 end Build_Temporary;
11187 -- Local variables
11189 Loc : constant Source_Ptr := Sloc (Exp);
11190 Exp_Type : constant Entity_Id := Etype (Exp);
11191 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
11192 Def_Id : Entity_Id;
11193 E : Node_Id;
11194 New_Exp : Node_Id;
11195 Ptr_Typ_Decl : Node_Id;
11196 Ref_Type : Entity_Id;
11197 Res : Node_Id;
11199 -- Start of processing for Remove_Side_Effects
11201 begin
11202 -- Handle cases in which there is nothing to do. In GNATprove mode,
11203 -- removal of side effects is useful for the light expansion of
11204 -- renamings. This removal should only occur when not inside a
11205 -- generic and not doing a preanalysis.
11207 if not Expander_Active
11208 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
11209 then
11210 return;
11212 -- Cannot generate temporaries if the invocation to remove side effects
11213 -- was issued too early and the type of the expression is not resolved
11214 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11215 -- Remove_Side_Effects).
11217 elsif No (Exp_Type)
11218 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11219 then
11220 return;
11222 -- Nothing to do if prior expansion determined that a function call does
11223 -- not require side effect removal.
11225 elsif Nkind (Exp) = N_Function_Call
11226 and then No_Side_Effect_Removal (Exp)
11227 then
11228 return;
11230 -- No action needed for side-effect free expressions
11232 elsif Check_Side_Effects
11233 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11234 then
11235 return;
11237 -- Generating C code we cannot remove side effect of function returning
11238 -- class-wide types since there is no secondary stack (required to use
11239 -- 'reference).
11241 elsif Modify_Tree_For_C
11242 and then Nkind (Exp) = N_Function_Call
11243 and then Is_Class_Wide_Type (Etype (Exp))
11244 then
11245 return;
11246 end if;
11248 -- The remaining processing is done with all checks suppressed
11250 -- Note: from now on, don't use return statements, instead do a goto
11251 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11253 Scope_Suppress.Suppress := (others => True);
11255 -- If this is an elementary or a small not-by-reference record type, and
11256 -- we need to capture the value, just make a constant; this is cheap and
11257 -- objects of both kinds of types can be bit aligned, so it might not be
11258 -- possible to generate a reference to them. Likewise if this is not a
11259 -- name reference, except for a type conversion, because we would enter
11260 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11261 -- type has predicates (and type conversions need a specific treatment
11262 -- anyway, see below). Also do it if we have a volatile reference and
11263 -- Name_Req is not set (see comments for Side_Effect_Free).
11265 if (Is_Elementary_Type (Exp_Type)
11266 or else (Is_Record_Type (Exp_Type)
11267 and then Known_Static_RM_Size (Exp_Type)
11268 and then RM_Size (Exp_Type) <= 64
11269 and then not Has_Discriminants (Exp_Type)
11270 and then not Is_By_Reference_Type (Exp_Type)))
11271 and then (Variable_Ref
11272 or else (not Is_Name_Reference (Exp)
11273 and then Nkind (Exp) /= N_Type_Conversion)
11274 or else (not Name_Req
11275 and then Is_Volatile_Reference (Exp)))
11276 then
11277 Def_Id := Build_Temporary (Loc, 'R', Exp);
11278 Set_Etype (Def_Id, Exp_Type);
11279 Res := New_Occurrence_Of (Def_Id, Loc);
11281 -- If the expression is a packed reference, it must be reanalyzed and
11282 -- expanded, depending on context. This is the case for actuals where
11283 -- a constraint check may capture the actual before expansion of the
11284 -- call is complete.
11286 if Nkind (Exp) = N_Indexed_Component
11287 and then Is_Packed (Etype (Prefix (Exp)))
11288 then
11289 Set_Analyzed (Exp, False);
11290 Set_Analyzed (Prefix (Exp), False);
11291 end if;
11293 -- Generate:
11294 -- Rnn : Exp_Type renames Expr;
11296 if Renaming_Req then
11297 E :=
11298 Make_Object_Renaming_Declaration (Loc,
11299 Defining_Identifier => Def_Id,
11300 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11301 Name => Relocate_Node (Exp));
11303 -- Generate:
11304 -- Rnn : constant Exp_Type := Expr;
11306 else
11307 E :=
11308 Make_Object_Declaration (Loc,
11309 Defining_Identifier => Def_Id,
11310 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11311 Constant_Present => True,
11312 Expression => Relocate_Node (Exp));
11314 Set_Assignment_OK (E);
11315 end if;
11317 Insert_Action (Exp, E);
11319 -- If the expression has the form v.all then we can just capture the
11320 -- pointer, and then do an explicit dereference on the result, but
11321 -- this is not right if this is a volatile reference.
11323 elsif Nkind (Exp) = N_Explicit_Dereference
11324 and then not Is_Volatile_Reference (Exp)
11325 then
11326 Def_Id := Build_Temporary (Loc, 'R', Exp);
11327 Res :=
11328 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11330 Insert_Action (Exp,
11331 Make_Object_Declaration (Loc,
11332 Defining_Identifier => Def_Id,
11333 Object_Definition =>
11334 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11335 Constant_Present => True,
11336 Expression => Relocate_Node (Prefix (Exp))));
11338 -- Similar processing for an unchecked conversion of an expression of
11339 -- the form v.all, where we want the same kind of treatment.
11341 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11342 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11343 then
11344 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11345 goto Leave;
11347 -- If this is a type conversion, leave the type conversion and remove
11348 -- the side effects in the expression. This is important in several
11349 -- circumstances: for change of representations, and also when this is a
11350 -- view conversion to a smaller object, where gigi can end up creating
11351 -- its own temporary of the wrong size.
11353 elsif Nkind (Exp) = N_Type_Conversion then
11354 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11356 -- Generating C code the type conversion of an access to constrained
11357 -- array type into an access to unconstrained array type involves
11358 -- initializing a fat pointer and the expression must be free of
11359 -- side effects to safely compute its bounds.
11361 if Modify_Tree_For_C
11362 and then Is_Access_Type (Etype (Exp))
11363 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11364 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11365 then
11366 Def_Id := Build_Temporary (Loc, 'R', Exp);
11367 Set_Etype (Def_Id, Exp_Type);
11368 Res := New_Occurrence_Of (Def_Id, Loc);
11370 Insert_Action (Exp,
11371 Make_Object_Declaration (Loc,
11372 Defining_Identifier => Def_Id,
11373 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11374 Constant_Present => True,
11375 Expression => Relocate_Node (Exp)));
11376 else
11377 goto Leave;
11378 end if;
11380 -- If this is an unchecked conversion that Gigi can't handle, make
11381 -- a copy or a use a renaming to capture the value.
11383 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11384 and then not Safe_Unchecked_Type_Conversion (Exp)
11385 then
11386 if CW_Or_Has_Controlled_Part (Exp_Type) then
11388 -- Use a renaming to capture the expression, rather than create
11389 -- a controlled temporary.
11391 Def_Id := Build_Temporary (Loc, 'R', Exp);
11392 Res := New_Occurrence_Of (Def_Id, Loc);
11394 Insert_Action (Exp,
11395 Make_Object_Renaming_Declaration (Loc,
11396 Defining_Identifier => Def_Id,
11397 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11398 Name => Relocate_Node (Exp)));
11400 else
11401 Def_Id := Build_Temporary (Loc, 'R', Exp);
11402 Set_Etype (Def_Id, Exp_Type);
11403 Res := New_Occurrence_Of (Def_Id, Loc);
11405 E :=
11406 Make_Object_Declaration (Loc,
11407 Defining_Identifier => Def_Id,
11408 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11409 Constant_Present => not Is_Variable (Exp),
11410 Expression => Relocate_Node (Exp));
11412 Set_Assignment_OK (E);
11413 Insert_Action (Exp, E);
11414 end if;
11416 -- For expressions that denote names, we can use a renaming scheme.
11417 -- This is needed for correctness in the case of a volatile object of
11418 -- a non-volatile type because the Make_Reference call of the "default"
11419 -- approach would generate an illegal access value (an access value
11420 -- cannot designate such an object - see Analyze_Reference).
11422 elsif Is_Name_Reference (Exp)
11424 -- We skip using this scheme if we have an object of a volatile
11425 -- type and we do not have Name_Req set true (see comments for
11426 -- Side_Effect_Free).
11428 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11429 then
11430 Def_Id := Build_Temporary (Loc, 'R', Exp);
11431 Res := New_Occurrence_Of (Def_Id, Loc);
11433 Insert_Action (Exp,
11434 Make_Object_Renaming_Declaration (Loc,
11435 Defining_Identifier => Def_Id,
11436 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11437 Name => Relocate_Node (Exp)));
11439 -- If this is a packed reference, or a selected component with
11440 -- a non-standard representation, a reference to the temporary
11441 -- will be replaced by a copy of the original expression (see
11442 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11443 -- elaborated by gigi, and is of course not to be replaced in-line
11444 -- by the expression it renames, which would defeat the purpose of
11445 -- removing the side effect.
11447 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11448 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11449 then
11450 null;
11451 else
11452 Set_Is_Renaming_Of_Object (Def_Id, False);
11453 end if;
11455 -- Avoid generating a variable-sized temporary, by generating the
11456 -- reference just for the function call. The transformation could be
11457 -- refined to apply only when the array component is constrained by a
11458 -- discriminant???
11460 elsif Nkind (Exp) = N_Selected_Component
11461 and then Nkind (Prefix (Exp)) = N_Function_Call
11462 and then Is_Array_Type (Exp_Type)
11463 then
11464 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11465 goto Leave;
11467 -- Otherwise we generate a reference to the expression
11469 else
11470 -- An expression which is in SPARK mode is considered side effect
11471 -- free if the resulting value is captured by a variable or a
11472 -- constant.
11474 if GNATprove_Mode
11475 and then Nkind (Parent (Exp)) = N_Object_Declaration
11476 then
11477 goto Leave;
11479 -- When generating C code we cannot consider side effect free object
11480 -- declarations that have discriminants and are initialized by means
11481 -- of a function call since on this target there is no secondary
11482 -- stack to store the return value and the expander may generate an
11483 -- extra call to the function to compute the discriminant value. In
11484 -- addition, for targets that have secondary stack, the expansion of
11485 -- functions with side effects involves the generation of an access
11486 -- type to capture the return value stored in the secondary stack;
11487 -- by contrast when generating C code such expansion generates an
11488 -- internal object declaration (no access type involved) which must
11489 -- be identified here to avoid entering into a never-ending loop
11490 -- generating internal object declarations.
11492 elsif Modify_Tree_For_C
11493 and then Nkind (Parent (Exp)) = N_Object_Declaration
11494 and then
11495 (Nkind (Exp) /= N_Function_Call
11496 or else not Has_Discriminants (Exp_Type)
11497 or else Is_Internal_Name
11498 (Chars (Defining_Identifier (Parent (Exp)))))
11499 then
11500 goto Leave;
11501 end if;
11503 -- Special processing for function calls that return a limited type.
11504 -- We need to build a declaration that will enable build-in-place
11505 -- expansion of the call. This is not done if the context is already
11506 -- an object declaration, to prevent infinite recursion.
11508 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11509 -- to accommodate functions returning limited objects by reference.
11511 if Ada_Version >= Ada_2005
11512 and then Nkind (Exp) = N_Function_Call
11513 and then Is_Limited_View (Etype (Exp))
11514 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11515 then
11516 declare
11517 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11518 Decl : Node_Id;
11520 begin
11521 Decl :=
11522 Make_Object_Declaration (Loc,
11523 Defining_Identifier => Obj,
11524 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11525 Expression => Relocate_Node (Exp));
11527 Insert_Action (Exp, Decl);
11528 Set_Etype (Obj, Exp_Type);
11529 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11530 goto Leave;
11531 end;
11532 end if;
11534 Def_Id := Build_Temporary (Loc, 'R', Exp);
11536 -- The regular expansion of functions with side effects involves the
11537 -- generation of an access type to capture the return value found on
11538 -- the secondary stack. Since SPARK (and why) cannot process access
11539 -- types, use a different approach which ignores the secondary stack
11540 -- and "copies" the returned object.
11541 -- When generating C code, no need for a 'reference since the
11542 -- secondary stack is not supported.
11544 if GNATprove_Mode or Modify_Tree_For_C then
11545 Res := New_Occurrence_Of (Def_Id, Loc);
11546 Ref_Type := Exp_Type;
11548 -- Regular expansion utilizing an access type and 'reference
11550 else
11551 Res :=
11552 Make_Explicit_Dereference (Loc,
11553 Prefix => New_Occurrence_Of (Def_Id, Loc));
11555 -- Generate:
11556 -- type Ann is access all <Exp_Type>;
11558 Ref_Type := Make_Temporary (Loc, 'A');
11560 Ptr_Typ_Decl :=
11561 Make_Full_Type_Declaration (Loc,
11562 Defining_Identifier => Ref_Type,
11563 Type_Definition =>
11564 Make_Access_To_Object_Definition (Loc,
11565 All_Present => True,
11566 Subtype_Indication =>
11567 New_Occurrence_Of (Exp_Type, Loc)));
11569 Insert_Action (Exp, Ptr_Typ_Decl);
11570 end if;
11572 E := Exp;
11573 if Nkind (E) = N_Explicit_Dereference then
11574 New_Exp := Relocate_Node (Prefix (E));
11576 else
11577 E := Relocate_Node (E);
11579 -- Do not generate a 'reference in SPARK mode or C generation
11580 -- since the access type is not created in the first place.
11582 if GNATprove_Mode or Modify_Tree_For_C then
11583 New_Exp := E;
11585 -- Otherwise generate reference, marking the value as non-null
11586 -- since we know it cannot be null and we don't want a check.
11588 else
11589 New_Exp := Make_Reference (Loc, E);
11590 Set_Is_Known_Non_Null (Def_Id);
11591 end if;
11592 end if;
11594 if Is_Delayed_Aggregate (E) then
11596 -- The expansion of nested aggregates is delayed until the
11597 -- enclosing aggregate is expanded. As aggregates are often
11598 -- qualified, the predicate applies to qualified expressions as
11599 -- well, indicating that the enclosing aggregate has not been
11600 -- expanded yet. At this point the aggregate is part of a
11601 -- stand-alone declaration, and must be fully expanded.
11603 if Nkind (E) = N_Qualified_Expression then
11604 Set_Expansion_Delayed (Expression (E), False);
11605 Set_Analyzed (Expression (E), False);
11606 else
11607 Set_Expansion_Delayed (E, False);
11608 end if;
11610 Set_Analyzed (E, False);
11611 end if;
11613 -- Generating C code of object declarations that have discriminants
11614 -- and are initialized by means of a function call we propagate the
11615 -- discriminants of the parent type to the internally built object.
11616 -- This is needed to avoid generating an extra call to the called
11617 -- function.
11619 -- For example, if we generate here the following declaration, it
11620 -- will be expanded later adding an extra call to evaluate the value
11621 -- of the discriminant (needed to compute the size of the object).
11623 -- type Rec (D : Integer) is ...
11624 -- Obj : constant Rec := SomeFunc;
11626 if Modify_Tree_For_C
11627 and then Nkind (Parent (Exp)) = N_Object_Declaration
11628 and then Has_Discriminants (Exp_Type)
11629 and then Nkind (Exp) = N_Function_Call
11630 then
11631 Insert_Action (Exp,
11632 Make_Object_Declaration (Loc,
11633 Defining_Identifier => Def_Id,
11634 Object_Definition => New_Copy_Tree
11635 (Object_Definition (Parent (Exp))),
11636 Constant_Present => True,
11637 Expression => New_Exp));
11638 else
11639 Insert_Action (Exp,
11640 Make_Object_Declaration (Loc,
11641 Defining_Identifier => Def_Id,
11642 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11643 Constant_Present => True,
11644 Expression => New_Exp));
11645 end if;
11646 end if;
11648 -- Preserve the Assignment_OK flag in all copies, since at least one
11649 -- copy may be used in a context where this flag must be set (otherwise
11650 -- why would the flag be set in the first place).
11652 Set_Assignment_OK (Res, Assignment_OK (Exp));
11654 -- Finally rewrite the original expression and we are done
11656 Rewrite (Exp, Res);
11657 Analyze_And_Resolve (Exp, Exp_Type);
11659 <<Leave>>
11660 Scope_Suppress := Svg_Suppress;
11661 end Remove_Side_Effects;
11663 ------------------------
11664 -- Replace_References --
11665 ------------------------
11667 procedure Replace_References
11668 (Expr : Node_Id;
11669 Par_Typ : Entity_Id;
11670 Deriv_Typ : Entity_Id;
11671 Par_Obj : Entity_Id := Empty;
11672 Deriv_Obj : Entity_Id := Empty)
11674 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11675 -- Determine whether node Ref denotes some component of Deriv_Obj
11677 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11678 -- Substitute a reference to an entity with the corresponding value
11679 -- stored in table Type_Map.
11681 function Type_Of_Formal
11682 (Call : Node_Id;
11683 Actual : Node_Id) return Entity_Id;
11684 -- Find the type of the formal parameter which corresponds to actual
11685 -- parameter Actual in subprogram call Call.
11687 ----------------------
11688 -- Is_Deriv_Obj_Ref --
11689 ----------------------
11691 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11692 Par : constant Node_Id := Parent (Ref);
11694 begin
11695 -- Detect the folowing selected component form:
11697 -- Deriv_Obj.(something)
11699 return
11700 Nkind (Par) = N_Selected_Component
11701 and then Is_Entity_Name (Prefix (Par))
11702 and then Entity (Prefix (Par)) = Deriv_Obj;
11703 end Is_Deriv_Obj_Ref;
11705 -----------------
11706 -- Replace_Ref --
11707 -----------------
11709 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11710 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11711 -- Reset the Controlling_Argument of all function calls that
11712 -- encapsulate node From_Arg.
11714 ----------------------------------
11715 -- Remove_Controlling_Arguments --
11716 ----------------------------------
11718 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11719 Par : Node_Id;
11721 begin
11722 Par := From_Arg;
11723 while Present (Par) loop
11724 if Nkind (Par) = N_Function_Call
11725 and then Present (Controlling_Argument (Par))
11726 then
11727 Set_Controlling_Argument (Par, Empty);
11729 -- Prevent the search from going too far
11731 elsif Is_Body_Or_Package_Declaration (Par) then
11732 exit;
11733 end if;
11735 Par := Parent (Par);
11736 end loop;
11737 end Remove_Controlling_Arguments;
11739 -- Local variables
11741 Context : constant Node_Id := Parent (Ref);
11742 Loc : constant Source_Ptr := Sloc (Ref);
11743 Ref_Id : Entity_Id;
11744 Result : Traverse_Result;
11746 New_Ref : Node_Id;
11747 -- The new reference which is intended to substitute the old one
11749 Old_Ref : Node_Id;
11750 -- The reference designated for replacement. In certain cases this
11751 -- may be a node other than Ref.
11753 Val : Node_Or_Entity_Id;
11754 -- The corresponding value of Ref from the type map
11756 -- Start of processing for Replace_Ref
11758 begin
11759 -- Assume that the input reference is to be replaced and that the
11760 -- traversal should examine the children of the reference.
11762 Old_Ref := Ref;
11763 Result := OK;
11765 -- The input denotes a meaningful reference
11767 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11768 Ref_Id := Entity (Ref);
11769 Val := Type_Map.Get (Ref_Id);
11771 -- The reference has a corresponding value in the type map, a
11772 -- substitution is possible.
11774 if Present (Val) then
11776 -- The reference denotes a discriminant
11778 if Ekind (Ref_Id) = E_Discriminant then
11779 if Nkind (Val) in N_Entity then
11781 -- The value denotes another discriminant. Replace as
11782 -- follows:
11784 -- _object.Discr -> _object.Val
11786 if Ekind (Val) = E_Discriminant then
11787 New_Ref := New_Occurrence_Of (Val, Loc);
11789 -- Otherwise the value denotes the entity of a name which
11790 -- constraints the discriminant. Replace as follows:
11792 -- _object.Discr -> Val
11794 else
11795 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11797 New_Ref := New_Occurrence_Of (Val, Loc);
11798 Old_Ref := Parent (Old_Ref);
11799 end if;
11801 -- Otherwise the value denotes an arbitrary expression which
11802 -- constraints the discriminant. Replace as follows:
11804 -- _object.Discr -> Val
11806 else
11807 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11809 New_Ref := New_Copy_Tree (Val);
11810 Old_Ref := Parent (Old_Ref);
11811 end if;
11813 -- Otherwise the reference denotes a primitive. Replace as
11814 -- follows:
11816 -- Primitive -> Val
11818 else
11819 pragma Assert (Nkind (Val) in N_Entity);
11820 New_Ref := New_Occurrence_Of (Val, Loc);
11821 end if;
11823 -- The reference mentions the _object parameter of the parent
11824 -- type's DIC or type invariant procedure. Replace as follows:
11826 -- _object -> _object
11828 elsif Present (Par_Obj)
11829 and then Present (Deriv_Obj)
11830 and then Ref_Id = Par_Obj
11831 then
11832 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11834 -- The type of the _object parameter is class-wide when the
11835 -- expression comes from an assertion pragma that applies to
11836 -- an abstract parent type or an interface. The class-wide type
11837 -- facilitates the preanalysis of the expression by treating
11838 -- calls to abstract primitives that mention the current
11839 -- instance of the type as dispatching. Once the calls are
11840 -- remapped to invoke overriding or inherited primitives, the
11841 -- calls no longer need to be dispatching. Examine all function
11842 -- calls that encapsulate the _object parameter and reset their
11843 -- Controlling_Argument attribute.
11845 if Is_Class_Wide_Type (Etype (Par_Obj))
11846 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11847 then
11848 Remove_Controlling_Arguments (Old_Ref);
11849 end if;
11851 -- The reference to _object acts as an actual parameter in a
11852 -- subprogram call which may be invoking a primitive of the
11853 -- parent type:
11855 -- Primitive (... _object ...);
11857 -- The parent type primitive may not be overridden nor
11858 -- inherited when it is declared after the derived type
11859 -- definition:
11861 -- type Parent is tagged private;
11862 -- type Child is new Parent with private;
11863 -- procedure Primitive (Obj : Parent);
11865 -- In this scenario the _object parameter is converted to the
11866 -- parent type. Due to complications with partial/full views
11867 -- and view swaps, the parent type is taken from the formal
11868 -- parameter of the subprogram being called.
11870 if Nkind_In (Context, N_Function_Call,
11871 N_Procedure_Call_Statement)
11872 and then No (Type_Map.Get (Entity (Name (Context))))
11873 then
11874 New_Ref :=
11875 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11877 -- Do not process the generated type conversion because
11878 -- both the parent type and the derived type are in the
11879 -- Type_Map table. This will clobber the type conversion
11880 -- by resetting its subtype mark.
11882 Result := Skip;
11883 end if;
11885 -- Otherwise there is nothing to replace
11887 else
11888 New_Ref := Empty;
11889 end if;
11891 if Present (New_Ref) then
11892 Rewrite (Old_Ref, New_Ref);
11894 -- Update the return type when the context of the reference
11895 -- acts as the name of a function call. Note that the update
11896 -- should not be performed when the reference appears as an
11897 -- actual in the call.
11899 if Nkind (Context) = N_Function_Call
11900 and then Name (Context) = Old_Ref
11901 then
11902 Set_Etype (Context, Etype (Val));
11903 end if;
11904 end if;
11905 end if;
11907 -- Reanalyze the reference due to potential replacements
11909 if Nkind (Old_Ref) in N_Has_Etype then
11910 Set_Analyzed (Old_Ref, False);
11911 end if;
11913 return Result;
11914 end Replace_Ref;
11916 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11918 --------------------
11919 -- Type_Of_Formal --
11920 --------------------
11922 function Type_Of_Formal
11923 (Call : Node_Id;
11924 Actual : Node_Id) return Entity_Id
11926 A : Node_Id;
11927 F : Entity_Id;
11929 begin
11930 -- Examine the list of actual and formal parameters in parallel
11932 A := First (Parameter_Associations (Call));
11933 F := First_Formal (Entity (Name (Call)));
11934 while Present (A) and then Present (F) loop
11935 if A = Actual then
11936 return Etype (F);
11937 end if;
11939 Next (A);
11940 Next_Formal (F);
11941 end loop;
11943 -- The actual parameter must always have a corresponding formal
11945 pragma Assert (False);
11947 return Empty;
11948 end Type_Of_Formal;
11950 -- Start of processing for Replace_References
11952 begin
11953 -- Map the attributes of the parent type to the proper corresponding
11954 -- attributes of the derived type.
11956 Map_Types
11957 (Parent_Type => Par_Typ,
11958 Derived_Type => Deriv_Typ);
11960 -- Inspect the input expression and perform substitutions where
11961 -- necessary.
11963 Replace_Refs (Expr);
11964 end Replace_References;
11966 -----------------------------
11967 -- Replace_Type_References --
11968 -----------------------------
11970 procedure Replace_Type_References
11971 (Expr : Node_Id;
11972 Typ : Entity_Id;
11973 Obj_Id : Entity_Id)
11975 procedure Replace_Type_Ref (N : Node_Id);
11976 -- Substitute a single reference of the current instance of type Typ
11977 -- with a reference to Obj_Id.
11979 ----------------------
11980 -- Replace_Type_Ref --
11981 ----------------------
11983 procedure Replace_Type_Ref (N : Node_Id) is
11984 begin
11985 -- Decorate the reference to Typ even though it may be rewritten
11986 -- further down. This is done for two reasons:
11988 -- * ASIS has all necessary semantic information in the original
11989 -- tree.
11991 -- * Routines which examine properties of the Original_Node have
11992 -- some semantic information.
11994 if Nkind (N) = N_Identifier then
11995 Set_Entity (N, Typ);
11996 Set_Etype (N, Typ);
11998 elsif Nkind (N) = N_Selected_Component then
11999 Analyze (Prefix (N));
12000 Set_Entity (Selector_Name (N), Typ);
12001 Set_Etype (Selector_Name (N), Typ);
12002 end if;
12004 -- Perform the following substitution:
12006 -- Typ --> _object
12008 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
12009 Set_Comes_From_Source (N, True);
12010 end Replace_Type_Ref;
12012 procedure Replace_Type_Refs is
12013 new Replace_Type_References_Generic (Replace_Type_Ref);
12015 -- Start of processing for Replace_Type_References
12017 begin
12018 Replace_Type_Refs (Expr, Typ);
12019 end Replace_Type_References;
12021 ---------------------------
12022 -- Represented_As_Scalar --
12023 ---------------------------
12025 function Represented_As_Scalar (T : Entity_Id) return Boolean is
12026 UT : constant Entity_Id := Underlying_Type (T);
12027 begin
12028 return Is_Scalar_Type (UT)
12029 or else (Is_Bit_Packed_Array (UT)
12030 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
12031 end Represented_As_Scalar;
12033 ------------------------------
12034 -- Requires_Cleanup_Actions --
12035 ------------------------------
12037 function Requires_Cleanup_Actions
12038 (N : Node_Id;
12039 Lib_Level : Boolean) return Boolean
12041 At_Lib_Level : constant Boolean :=
12042 Lib_Level
12043 and then Nkind_In (N, N_Package_Body,
12044 N_Package_Specification);
12045 -- N is at the library level if the top-most context is a package and
12046 -- the path taken to reach N does not inlcude non-package constructs.
12048 begin
12049 case Nkind (N) is
12050 when N_Accept_Statement
12051 | N_Block_Statement
12052 | N_Entry_Body
12053 | N_Package_Body
12054 | N_Protected_Body
12055 | N_Subprogram_Body
12056 | N_Task_Body
12058 return
12059 Requires_Cleanup_Actions
12060 (L => Declarations (N),
12061 Lib_Level => At_Lib_Level,
12062 Nested_Constructs => True)
12063 or else
12064 (Present (Handled_Statement_Sequence (N))
12065 and then
12066 Requires_Cleanup_Actions
12067 (L =>
12068 Statements (Handled_Statement_Sequence (N)),
12069 Lib_Level => At_Lib_Level,
12070 Nested_Constructs => True));
12072 -- Extended return statements are the same as the above, except that
12073 -- there is no Declarations field. We do not want to clean up the
12074 -- Return_Object_Declarations.
12076 when N_Extended_Return_Statement =>
12077 return
12078 Present (Handled_Statement_Sequence (N))
12079 and then Requires_Cleanup_Actions
12080 (L =>
12081 Statements (Handled_Statement_Sequence (N)),
12082 Lib_Level => At_Lib_Level,
12083 Nested_Constructs => True);
12085 when N_Package_Specification =>
12086 return
12087 Requires_Cleanup_Actions
12088 (L => Visible_Declarations (N),
12089 Lib_Level => At_Lib_Level,
12090 Nested_Constructs => True)
12091 or else
12092 Requires_Cleanup_Actions
12093 (L => Private_Declarations (N),
12094 Lib_Level => At_Lib_Level,
12095 Nested_Constructs => True);
12097 when others =>
12098 raise Program_Error;
12099 end case;
12100 end Requires_Cleanup_Actions;
12102 ------------------------------
12103 -- Requires_Cleanup_Actions --
12104 ------------------------------
12106 function Requires_Cleanup_Actions
12107 (L : List_Id;
12108 Lib_Level : Boolean;
12109 Nested_Constructs : Boolean) return Boolean
12111 Decl : Node_Id;
12112 Expr : Node_Id;
12113 Obj_Id : Entity_Id;
12114 Obj_Typ : Entity_Id;
12115 Pack_Id : Entity_Id;
12116 Typ : Entity_Id;
12118 begin
12119 if No (L)
12120 or else Is_Empty_List (L)
12121 then
12122 return False;
12123 end if;
12125 Decl := First (L);
12126 while Present (Decl) loop
12128 -- Library-level tagged types
12130 if Nkind (Decl) = N_Full_Type_Declaration then
12131 Typ := Defining_Identifier (Decl);
12133 -- Ignored Ghost types do not need any cleanup actions because
12134 -- they will not appear in the final tree.
12136 if Is_Ignored_Ghost_Entity (Typ) then
12137 null;
12139 elsif Is_Tagged_Type (Typ)
12140 and then Is_Library_Level_Entity (Typ)
12141 and then Convention (Typ) = Convention_Ada
12142 and then Present (Access_Disp_Table (Typ))
12143 and then RTE_Available (RE_Unregister_Tag)
12144 and then not Is_Abstract_Type (Typ)
12145 and then not No_Run_Time_Mode
12146 then
12147 return True;
12148 end if;
12150 -- Regular object declarations
12152 elsif Nkind (Decl) = N_Object_Declaration then
12153 Obj_Id := Defining_Identifier (Decl);
12154 Obj_Typ := Base_Type (Etype (Obj_Id));
12155 Expr := Expression (Decl);
12157 -- Bypass any form of processing for objects which have their
12158 -- finalization disabled. This applies only to objects at the
12159 -- library level.
12161 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12162 null;
12164 -- Finalization of transient objects are treated separately in
12165 -- order to handle sensitive cases. These include:
12167 -- * Aggregate expansion
12168 -- * If, case, and expression with actions expansion
12169 -- * Transient scopes
12171 -- If one of those contexts has marked the transient object as
12172 -- ignored, do not generate finalization actions for it.
12174 elsif Is_Finalized_Transient (Obj_Id)
12175 or else Is_Ignored_Transient (Obj_Id)
12176 then
12177 null;
12179 -- Ignored Ghost objects do not need any cleanup actions because
12180 -- they will not appear in the final tree.
12182 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12183 null;
12185 -- The object is of the form:
12186 -- Obj : [constant] Typ [:= Expr];
12188 -- Do not process tag-to-class-wide conversions because they do
12189 -- not yield an object. Do not process the incomplete view of a
12190 -- deferred constant. Note that an object initialized by means
12191 -- of a build-in-place function call may appear as a deferred
12192 -- constant after expansion activities. These kinds of objects
12193 -- must be finalized.
12195 elsif not Is_Imported (Obj_Id)
12196 and then Needs_Finalization (Obj_Typ)
12197 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
12198 and then not (Ekind (Obj_Id) = E_Constant
12199 and then not Has_Completion (Obj_Id)
12200 and then No (BIP_Initialization_Call (Obj_Id)))
12201 then
12202 return True;
12204 -- The object is of the form:
12205 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12207 -- Obj : Access_Typ :=
12208 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12210 elsif Is_Access_Type (Obj_Typ)
12211 and then Needs_Finalization
12212 (Available_View (Designated_Type (Obj_Typ)))
12213 and then Present (Expr)
12214 and then
12215 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12216 or else
12217 (Is_Non_BIP_Func_Call (Expr)
12218 and then not Is_Related_To_Func_Return (Obj_Id)))
12219 then
12220 return True;
12222 -- Processing for "hook" objects generated for transient objects
12223 -- declared inside an Expression_With_Actions.
12225 elsif Is_Access_Type (Obj_Typ)
12226 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12227 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12228 N_Object_Declaration
12229 then
12230 return True;
12232 -- Processing for intermediate results of if expressions where
12233 -- one of the alternatives uses a controlled function call.
12235 elsif Is_Access_Type (Obj_Typ)
12236 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12237 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12238 N_Defining_Identifier
12239 and then Present (Expr)
12240 and then Nkind (Expr) = N_Null
12241 then
12242 return True;
12244 -- Simple protected objects which use type System.Tasking.
12245 -- Protected_Objects.Protection to manage their locks should be
12246 -- treated as controlled since they require manual cleanup.
12248 elsif Ekind (Obj_Id) = E_Variable
12249 and then (Is_Simple_Protected_Type (Obj_Typ)
12250 or else Has_Simple_Protected_Object (Obj_Typ))
12251 then
12252 return True;
12253 end if;
12255 -- Specific cases of object renamings
12257 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12258 Obj_Id := Defining_Identifier (Decl);
12259 Obj_Typ := Base_Type (Etype (Obj_Id));
12261 -- Bypass any form of processing for objects which have their
12262 -- finalization disabled. This applies only to objects at the
12263 -- library level.
12265 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12266 null;
12268 -- Ignored Ghost object renamings do not need any cleanup actions
12269 -- because they will not appear in the final tree.
12271 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12272 null;
12274 -- Return object of a build-in-place function. This case is
12275 -- recognized and marked by the expansion of an extended return
12276 -- statement (see Expand_N_Extended_Return_Statement).
12278 elsif Needs_Finalization (Obj_Typ)
12279 and then Is_Return_Object (Obj_Id)
12280 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12281 then
12282 return True;
12284 -- Detect a case where a source object has been initialized by
12285 -- a controlled function call or another object which was later
12286 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12288 -- Obj1 : CW_Type := Src_Obj;
12289 -- Obj2 : CW_Type := Function_Call (...);
12291 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12292 -- Tmp : ... := Function_Call (...)'reference;
12293 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12295 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12296 return True;
12297 end if;
12299 -- Inspect the freeze node of an access-to-controlled type and look
12300 -- for a delayed finalization master. This case arises when the
12301 -- freeze actions are inserted at a later time than the expansion of
12302 -- the context. Since Build_Finalizer is never called on a single
12303 -- construct twice, the master will be ultimately left out and never
12304 -- finalized. This is also needed for freeze actions of designated
12305 -- types themselves, since in some cases the finalization master is
12306 -- associated with a designated type's freeze node rather than that
12307 -- of the access type (see handling for freeze actions in
12308 -- Build_Finalization_Master).
12310 elsif Nkind (Decl) = N_Freeze_Entity
12311 and then Present (Actions (Decl))
12312 then
12313 Typ := Entity (Decl);
12315 -- Freeze nodes for ignored Ghost types do not need cleanup
12316 -- actions because they will never appear in the final tree.
12318 if Is_Ignored_Ghost_Entity (Typ) then
12319 null;
12321 elsif ((Is_Access_Type (Typ)
12322 and then not Is_Access_Subprogram_Type (Typ)
12323 and then Needs_Finalization
12324 (Available_View (Designated_Type (Typ))))
12325 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12326 and then Requires_Cleanup_Actions
12327 (Actions (Decl), Lib_Level, Nested_Constructs)
12328 then
12329 return True;
12330 end if;
12332 -- Nested package declarations
12334 elsif Nested_Constructs
12335 and then Nkind (Decl) = N_Package_Declaration
12336 then
12337 Pack_Id := Defining_Entity (Decl);
12339 -- Do not inspect an ignored Ghost package because all code found
12340 -- within will not appear in the final tree.
12342 if Is_Ignored_Ghost_Entity (Pack_Id) then
12343 null;
12345 elsif Ekind (Pack_Id) /= E_Generic_Package
12346 and then Requires_Cleanup_Actions
12347 (Specification (Decl), Lib_Level)
12348 then
12349 return True;
12350 end if;
12352 -- Nested package bodies
12354 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12356 -- Do not inspect an ignored Ghost package body because all code
12357 -- found within will not appear in the final tree.
12359 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12360 null;
12362 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12363 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12364 then
12365 return True;
12366 end if;
12368 elsif Nkind (Decl) = N_Block_Statement
12369 and then
12371 -- Handle a rare case caused by a controlled transient object
12372 -- created as part of a record init proc. The variable is wrapped
12373 -- in a block, but the block is not associated with a transient
12374 -- scope.
12376 (Inside_Init_Proc
12378 -- Handle the case where the original context has been wrapped in
12379 -- a block to avoid interference between exception handlers and
12380 -- At_End handlers. Treat the block as transparent and process its
12381 -- contents.
12383 or else Is_Finalization_Wrapper (Decl))
12384 then
12385 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12386 return True;
12387 end if;
12388 end if;
12390 Next (Decl);
12391 end loop;
12393 return False;
12394 end Requires_Cleanup_Actions;
12396 ------------------------------------
12397 -- Safe_Unchecked_Type_Conversion --
12398 ------------------------------------
12400 -- Note: this function knows quite a bit about the exact requirements of
12401 -- Gigi with respect to unchecked type conversions, and its code must be
12402 -- coordinated with any changes in Gigi in this area.
12404 -- The above requirements should be documented in Sinfo ???
12406 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12407 Otyp : Entity_Id;
12408 Ityp : Entity_Id;
12409 Oalign : Uint;
12410 Ialign : Uint;
12411 Pexp : constant Node_Id := Parent (Exp);
12413 begin
12414 -- If the expression is the RHS of an assignment or object declaration
12415 -- we are always OK because there will always be a target.
12417 -- Object renaming declarations, (generated for view conversions of
12418 -- actuals in inlined calls), like object declarations, provide an
12419 -- explicit type, and are safe as well.
12421 if (Nkind (Pexp) = N_Assignment_Statement
12422 and then Expression (Pexp) = Exp)
12423 or else Nkind_In (Pexp, N_Object_Declaration,
12424 N_Object_Renaming_Declaration)
12425 then
12426 return True;
12428 -- If the expression is the prefix of an N_Selected_Component we should
12429 -- also be OK because GCC knows to look inside the conversion except if
12430 -- the type is discriminated. We assume that we are OK anyway if the
12431 -- type is not set yet or if it is controlled since we can't afford to
12432 -- introduce a temporary in this case.
12434 elsif Nkind (Pexp) = N_Selected_Component
12435 and then Prefix (Pexp) = Exp
12436 then
12437 if No (Etype (Pexp)) then
12438 return True;
12439 else
12440 return
12441 not Has_Discriminants (Etype (Pexp))
12442 or else Is_Constrained (Etype (Pexp));
12443 end if;
12444 end if;
12446 -- Set the output type, this comes from Etype if it is set, otherwise we
12447 -- take it from the subtype mark, which we assume was already fully
12448 -- analyzed.
12450 if Present (Etype (Exp)) then
12451 Otyp := Etype (Exp);
12452 else
12453 Otyp := Entity (Subtype_Mark (Exp));
12454 end if;
12456 -- The input type always comes from the expression, and we assume this
12457 -- is indeed always analyzed, so we can simply get the Etype.
12459 Ityp := Etype (Expression (Exp));
12461 -- Initialize alignments to unknown so far
12463 Oalign := No_Uint;
12464 Ialign := No_Uint;
12466 -- Replace a concurrent type by its corresponding record type and each
12467 -- type by its underlying type and do the tests on those. The original
12468 -- type may be a private type whose completion is a concurrent type, so
12469 -- find the underlying type first.
12471 if Present (Underlying_Type (Otyp)) then
12472 Otyp := Underlying_Type (Otyp);
12473 end if;
12475 if Present (Underlying_Type (Ityp)) then
12476 Ityp := Underlying_Type (Ityp);
12477 end if;
12479 if Is_Concurrent_Type (Otyp) then
12480 Otyp := Corresponding_Record_Type (Otyp);
12481 end if;
12483 if Is_Concurrent_Type (Ityp) then
12484 Ityp := Corresponding_Record_Type (Ityp);
12485 end if;
12487 -- If the base types are the same, we know there is no problem since
12488 -- this conversion will be a noop.
12490 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12491 return True;
12493 -- Same if this is an upwards conversion of an untagged type, and there
12494 -- are no constraints involved (could be more general???)
12496 elsif Etype (Ityp) = Otyp
12497 and then not Is_Tagged_Type (Ityp)
12498 and then not Has_Discriminants (Ityp)
12499 and then No (First_Rep_Item (Base_Type (Ityp)))
12500 then
12501 return True;
12503 -- If the expression has an access type (object or subprogram) we assume
12504 -- that the conversion is safe, because the size of the target is safe,
12505 -- even if it is a record (which might be treated as having unknown size
12506 -- at this point).
12508 elsif Is_Access_Type (Ityp) then
12509 return True;
12511 -- If the size of output type is known at compile time, there is never
12512 -- a problem. Note that unconstrained records are considered to be of
12513 -- known size, but we can't consider them that way here, because we are
12514 -- talking about the actual size of the object.
12516 -- We also make sure that in addition to the size being known, we do not
12517 -- have a case which might generate an embarrassingly large temp in
12518 -- stack checking mode.
12520 elsif Size_Known_At_Compile_Time (Otyp)
12521 and then
12522 (not Stack_Checking_Enabled
12523 or else not May_Generate_Large_Temp (Otyp))
12524 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12525 then
12526 return True;
12528 -- If either type is tagged, then we know the alignment is OK so Gigi
12529 -- will be able to use pointer punning.
12531 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12532 return True;
12534 -- If either type is a limited record type, we cannot do a copy, so say
12535 -- safe since there's nothing else we can do.
12537 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12538 return True;
12540 -- Conversions to and from packed array types are always ignored and
12541 -- hence are safe.
12543 elsif Is_Packed_Array_Impl_Type (Otyp)
12544 or else Is_Packed_Array_Impl_Type (Ityp)
12545 then
12546 return True;
12547 end if;
12549 -- The only other cases known to be safe is if the input type's
12550 -- alignment is known to be at least the maximum alignment for the
12551 -- target or if both alignments are known and the output type's
12552 -- alignment is no stricter than the input's. We can use the component
12553 -- type alignment for an array if a type is an unpacked array type.
12555 if Present (Alignment_Clause (Otyp)) then
12556 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12558 elsif Is_Array_Type (Otyp)
12559 and then Present (Alignment_Clause (Component_Type (Otyp)))
12560 then
12561 Oalign := Expr_Value (Expression (Alignment_Clause
12562 (Component_Type (Otyp))));
12563 end if;
12565 if Present (Alignment_Clause (Ityp)) then
12566 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12568 elsif Is_Array_Type (Ityp)
12569 and then Present (Alignment_Clause (Component_Type (Ityp)))
12570 then
12571 Ialign := Expr_Value (Expression (Alignment_Clause
12572 (Component_Type (Ityp))));
12573 end if;
12575 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12576 return True;
12578 elsif Ialign /= No_Uint
12579 and then Oalign /= No_Uint
12580 and then Ialign <= Oalign
12581 then
12582 return True;
12584 -- Otherwise, Gigi cannot handle this and we must make a temporary
12586 else
12587 return False;
12588 end if;
12589 end Safe_Unchecked_Type_Conversion;
12591 ---------------------------------
12592 -- Set_Current_Value_Condition --
12593 ---------------------------------
12595 -- Note: the implementation of this procedure is very closely tied to the
12596 -- implementation of Get_Current_Value_Condition. Here we set required
12597 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12598 -- them, so they must have a consistent view.
12600 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12602 procedure Set_Entity_Current_Value (N : Node_Id);
12603 -- If N is an entity reference, where the entity is of an appropriate
12604 -- kind, then set the current value of this entity to Cnode, unless
12605 -- there is already a definite value set there.
12607 procedure Set_Expression_Current_Value (N : Node_Id);
12608 -- If N is of an appropriate form, sets an appropriate entry in current
12609 -- value fields of relevant entities. Multiple entities can be affected
12610 -- in the case of an AND or AND THEN.
12612 ------------------------------
12613 -- Set_Entity_Current_Value --
12614 ------------------------------
12616 procedure Set_Entity_Current_Value (N : Node_Id) is
12617 begin
12618 if Is_Entity_Name (N) then
12619 declare
12620 Ent : constant Entity_Id := Entity (N);
12622 begin
12623 -- Don't capture if not safe to do so
12625 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12626 return;
12627 end if;
12629 -- Here we have a case where the Current_Value field may need
12630 -- to be set. We set it if it is not already set to a compile
12631 -- time expression value.
12633 -- Note that this represents a decision that one condition
12634 -- blots out another previous one. That's certainly right if
12635 -- they occur at the same level. If the second one is nested,
12636 -- then the decision is neither right nor wrong (it would be
12637 -- equally OK to leave the outer one in place, or take the new
12638 -- inner one. Really we should record both, but our data
12639 -- structures are not that elaborate.
12641 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12642 Set_Current_Value (Ent, Cnode);
12643 end if;
12644 end;
12645 end if;
12646 end Set_Entity_Current_Value;
12648 ----------------------------------
12649 -- Set_Expression_Current_Value --
12650 ----------------------------------
12652 procedure Set_Expression_Current_Value (N : Node_Id) is
12653 Cond : Node_Id;
12655 begin
12656 Cond := N;
12658 -- Loop to deal with (ignore for now) any NOT operators present. The
12659 -- presence of NOT operators will be handled properly when we call
12660 -- Get_Current_Value_Condition.
12662 while Nkind (Cond) = N_Op_Not loop
12663 Cond := Right_Opnd (Cond);
12664 end loop;
12666 -- For an AND or AND THEN, recursively process operands
12668 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12669 Set_Expression_Current_Value (Left_Opnd (Cond));
12670 Set_Expression_Current_Value (Right_Opnd (Cond));
12671 return;
12672 end if;
12674 -- Check possible relational operator
12676 if Nkind (Cond) in N_Op_Compare then
12677 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12678 Set_Entity_Current_Value (Left_Opnd (Cond));
12679 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12680 Set_Entity_Current_Value (Right_Opnd (Cond));
12681 end if;
12683 elsif Nkind_In (Cond,
12684 N_Type_Conversion,
12685 N_Qualified_Expression,
12686 N_Expression_With_Actions)
12687 then
12688 Set_Expression_Current_Value (Expression (Cond));
12690 -- Check possible boolean variable reference
12692 else
12693 Set_Entity_Current_Value (Cond);
12694 end if;
12695 end Set_Expression_Current_Value;
12697 -- Start of processing for Set_Current_Value_Condition
12699 begin
12700 Set_Expression_Current_Value (Condition (Cnode));
12701 end Set_Current_Value_Condition;
12703 --------------------------
12704 -- Set_Elaboration_Flag --
12705 --------------------------
12707 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12708 Loc : constant Source_Ptr := Sloc (N);
12709 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12710 Asn : Node_Id;
12712 begin
12713 if Present (Ent) then
12715 -- Nothing to do if at the compilation unit level, because in this
12716 -- case the flag is set by the binder generated elaboration routine.
12718 if Nkind (Parent (N)) = N_Compilation_Unit then
12719 null;
12721 -- Here we do need to generate an assignment statement
12723 else
12724 Check_Restriction (No_Elaboration_Code, N);
12726 Asn :=
12727 Make_Assignment_Statement (Loc,
12728 Name => New_Occurrence_Of (Ent, Loc),
12729 Expression => Make_Integer_Literal (Loc, Uint_1));
12731 -- Mark the assignment statement as elaboration code. This allows
12732 -- the early call region mechanism (see Sem_Elab) to properly
12733 -- ignore such assignments even though they are non-preelaborable
12734 -- code.
12736 Set_Is_Elaboration_Code (Asn);
12738 if Nkind (Parent (N)) = N_Subunit then
12739 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12740 else
12741 Insert_After (N, Asn);
12742 end if;
12744 Analyze (Asn);
12746 -- Kill current value indication. This is necessary because the
12747 -- tests of this flag are inserted out of sequence and must not
12748 -- pick up bogus indications of the wrong constant value.
12750 Set_Current_Value (Ent, Empty);
12752 -- If the subprogram is in the current declarative part and
12753 -- 'access has been applied to it, generate an elaboration
12754 -- check at the beginning of the declarations of the body.
12756 if Nkind (N) = N_Subprogram_Body
12757 and then Address_Taken (Spec_Id)
12758 and then
12759 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12760 then
12761 declare
12762 Loc : constant Source_Ptr := Sloc (N);
12763 Decls : constant List_Id := Declarations (N);
12764 Chk : Node_Id;
12766 begin
12767 -- No need to generate this check if first entry in the
12768 -- declaration list is a raise of Program_Error now.
12770 if Present (Decls)
12771 and then Nkind (First (Decls)) = N_Raise_Program_Error
12772 then
12773 return;
12774 end if;
12776 -- Otherwise generate the check
12778 Chk :=
12779 Make_Raise_Program_Error (Loc,
12780 Condition =>
12781 Make_Op_Eq (Loc,
12782 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12783 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12784 Reason => PE_Access_Before_Elaboration);
12786 if No (Decls) then
12787 Set_Declarations (N, New_List (Chk));
12788 else
12789 Prepend (Chk, Decls);
12790 end if;
12792 Analyze (Chk);
12793 end;
12794 end if;
12795 end if;
12796 end if;
12797 end Set_Elaboration_Flag;
12799 ----------------------------
12800 -- Set_Renamed_Subprogram --
12801 ----------------------------
12803 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12804 begin
12805 -- If input node is an identifier, we can just reset it
12807 if Nkind (N) = N_Identifier then
12808 Set_Chars (N, Chars (E));
12809 Set_Entity (N, E);
12811 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12813 else
12814 declare
12815 CS : constant Boolean := Comes_From_Source (N);
12816 begin
12817 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12818 Set_Entity (N, E);
12819 Set_Comes_From_Source (N, CS);
12820 Set_Analyzed (N, True);
12821 end;
12822 end if;
12823 end Set_Renamed_Subprogram;
12825 ----------------------
12826 -- Side_Effect_Free --
12827 ----------------------
12829 function Side_Effect_Free
12830 (N : Node_Id;
12831 Name_Req : Boolean := False;
12832 Variable_Ref : Boolean := False) return Boolean
12834 Typ : constant Entity_Id := Etype (N);
12835 -- Result type of the expression
12837 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12838 -- The argument N is a construct where the Prefix is dereferenced if it
12839 -- is an access type and the result is a variable. The call returns True
12840 -- if the construct is side effect free (not considering side effects in
12841 -- other than the prefix which are to be tested by the caller).
12843 function Within_In_Parameter (N : Node_Id) return Boolean;
12844 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12845 -- N is not side-effect free when the actual is global and modifiable
12846 -- indirectly from within a subprogram, because it may be passed by
12847 -- reference. The front-end must be conservative here and assume that
12848 -- this may happen with any array or record type. On the other hand, we
12849 -- cannot create temporaries for all expressions for which this
12850 -- condition is true, for various reasons that might require clearing up
12851 -- ??? For example, discriminant references that appear out of place, or
12852 -- spurious type errors with class-wide expressions. As a result, we
12853 -- limit the transformation to loop bounds, which is so far the only
12854 -- case that requires it.
12856 -----------------------------
12857 -- Safe_Prefixed_Reference --
12858 -----------------------------
12860 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12861 begin
12862 -- If prefix is not side effect free, definitely not safe
12864 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12865 return False;
12867 -- If the prefix is of an access type that is not access-to-constant,
12868 -- then this construct is a variable reference, which means it is to
12869 -- be considered to have side effects if Variable_Ref is set True.
12871 elsif Is_Access_Type (Etype (Prefix (N)))
12872 and then not Is_Access_Constant (Etype (Prefix (N)))
12873 and then Variable_Ref
12874 then
12875 -- Exception is a prefix that is the result of a previous removal
12876 -- of side effects.
12878 return Is_Entity_Name (Prefix (N))
12879 and then not Comes_From_Source (Prefix (N))
12880 and then Ekind (Entity (Prefix (N))) = E_Constant
12881 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12883 -- If the prefix is an explicit dereference then this construct is a
12884 -- variable reference, which means it is to be considered to have
12885 -- side effects if Variable_Ref is True.
12887 -- We do NOT exclude dereferences of access-to-constant types because
12888 -- we handle them as constant view of variables.
12890 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12891 and then Variable_Ref
12892 then
12893 return False;
12895 -- Note: The following test is the simplest way of solving a complex
12896 -- problem uncovered by the following test (Side effect on loop bound
12897 -- that is a subcomponent of a global variable:
12899 -- with Text_Io; use Text_Io;
12900 -- procedure Tloop is
12901 -- type X is
12902 -- record
12903 -- V : Natural := 4;
12904 -- S : String (1..5) := (others => 'a');
12905 -- end record;
12906 -- X1 : X;
12908 -- procedure Modi;
12910 -- generic
12911 -- with procedure Action;
12912 -- procedure Loop_G (Arg : X; Msg : String)
12914 -- procedure Loop_G (Arg : X; Msg : String) is
12915 -- begin
12916 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12917 -- & Natural'Image (Arg.V));
12918 -- for Index in 1 .. Arg.V loop
12919 -- Text_Io.Put_Line
12920 -- (Natural'Image (Index) & " " & Arg.S (Index));
12921 -- if Index > 2 then
12922 -- Modi;
12923 -- end if;
12924 -- end loop;
12925 -- Put_Line ("end loop_g " & Msg);
12926 -- end;
12928 -- procedure Loop1 is new Loop_G (Modi);
12929 -- procedure Modi is
12930 -- begin
12931 -- X1.V := 1;
12932 -- Loop1 (X1, "from modi");
12933 -- end;
12935 -- begin
12936 -- Loop1 (X1, "initial");
12937 -- end;
12939 -- The output of the above program should be:
12941 -- begin loop_g initial will loop till: 4
12942 -- 1 a
12943 -- 2 a
12944 -- 3 a
12945 -- begin loop_g from modi will loop till: 1
12946 -- 1 a
12947 -- end loop_g from modi
12948 -- 4 a
12949 -- begin loop_g from modi will loop till: 1
12950 -- 1 a
12951 -- end loop_g from modi
12952 -- end loop_g initial
12954 -- If a loop bound is a subcomponent of a global variable, a
12955 -- modification of that variable within the loop may incorrectly
12956 -- affect the execution of the loop.
12958 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12959 and then Within_In_Parameter (Prefix (N))
12960 and then Variable_Ref
12961 then
12962 return False;
12964 -- All other cases are side effect free
12966 else
12967 return True;
12968 end if;
12969 end Safe_Prefixed_Reference;
12971 -------------------------
12972 -- Within_In_Parameter --
12973 -------------------------
12975 function Within_In_Parameter (N : Node_Id) return Boolean is
12976 begin
12977 if not Comes_From_Source (N) then
12978 return False;
12980 elsif Is_Entity_Name (N) then
12981 return Ekind (Entity (N)) = E_In_Parameter;
12983 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12984 return Within_In_Parameter (Prefix (N));
12986 else
12987 return False;
12988 end if;
12989 end Within_In_Parameter;
12991 -- Start of processing for Side_Effect_Free
12993 begin
12994 -- If volatile reference, always consider it to have side effects
12996 if Is_Volatile_Reference (N) then
12997 return False;
12998 end if;
13000 -- Note on checks that could raise Constraint_Error. Strictly, if we
13001 -- take advantage of 11.6, these checks do not count as side effects.
13002 -- However, we would prefer to consider that they are side effects,
13003 -- since the back end CSE does not work very well on expressions which
13004 -- can raise Constraint_Error. On the other hand if we don't consider
13005 -- them to be side effect free, then we get some awkward expansions
13006 -- in -gnato mode, resulting in code insertions at a point where we
13007 -- do not have a clear model for performing the insertions.
13009 -- Special handling for entity names
13011 if Is_Entity_Name (N) then
13013 -- A type reference is always side effect free
13015 if Is_Type (Entity (N)) then
13016 return True;
13018 -- Variables are considered to be a side effect if Variable_Ref
13019 -- is set or if we have a volatile reference and Name_Req is off.
13020 -- If Name_Req is True then we can't help returning a name which
13021 -- effectively allows multiple references in any case.
13023 elsif Is_Variable (N, Use_Original_Node => False) then
13024 return not Variable_Ref
13025 and then (not Is_Volatile_Reference (N) or else Name_Req);
13027 -- Any other entity (e.g. a subtype name) is definitely side
13028 -- effect free.
13030 else
13031 return True;
13032 end if;
13034 -- A value known at compile time is always side effect free
13036 elsif Compile_Time_Known_Value (N) then
13037 return True;
13039 -- A variable renaming is not side-effect free, because the renaming
13040 -- will function like a macro in the front-end in some cases, and an
13041 -- assignment can modify the component designated by N, so we need to
13042 -- create a temporary for it.
13044 -- The guard testing for Entity being present is needed at least in
13045 -- the case of rewritten predicate expressions, and may well also be
13046 -- appropriate elsewhere. Obviously we can't go testing the entity
13047 -- field if it does not exist, so it's reasonable to say that this is
13048 -- not the renaming case if it does not exist.
13050 elsif Is_Entity_Name (Original_Node (N))
13051 and then Present (Entity (Original_Node (N)))
13052 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
13053 and then Ekind (Entity (Original_Node (N))) /= E_Constant
13054 then
13055 declare
13056 RO : constant Node_Id :=
13057 Renamed_Object (Entity (Original_Node (N)));
13059 begin
13060 -- If the renamed object is an indexed component, or an
13061 -- explicit dereference, then the designated object could
13062 -- be modified by an assignment.
13064 if Nkind_In (RO, N_Indexed_Component,
13065 N_Explicit_Dereference)
13066 then
13067 return False;
13069 -- A selected component must have a safe prefix
13071 elsif Nkind (RO) = N_Selected_Component then
13072 return Safe_Prefixed_Reference (RO);
13074 -- In all other cases, designated object cannot be changed so
13075 -- we are side effect free.
13077 else
13078 return True;
13079 end if;
13080 end;
13082 -- Remove_Side_Effects generates an object renaming declaration to
13083 -- capture the expression of a class-wide expression. In VM targets
13084 -- the frontend performs no expansion for dispatching calls to
13085 -- class- wide types since they are handled by the VM. Hence, we must
13086 -- locate here if this node corresponds to a previous invocation of
13087 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13089 elsif not Tagged_Type_Expansion
13090 and then not Comes_From_Source (N)
13091 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
13092 and then Is_Class_Wide_Type (Typ)
13093 then
13094 return True;
13096 -- Generating C the type conversion of an access to constrained array
13097 -- type into an access to unconstrained array type involves initializing
13098 -- a fat pointer and the expression cannot be assumed to be free of side
13099 -- effects since it must referenced several times to compute its bounds.
13101 elsif Modify_Tree_For_C
13102 and then Nkind (N) = N_Type_Conversion
13103 and then Is_Access_Type (Typ)
13104 and then Is_Array_Type (Designated_Type (Typ))
13105 and then not Is_Constrained (Designated_Type (Typ))
13106 then
13107 return False;
13108 end if;
13110 -- For other than entity names and compile time known values,
13111 -- check the node kind for special processing.
13113 case Nkind (N) is
13115 -- An attribute reference is side effect free if its expressions
13116 -- are side effect free and its prefix is side effect free or
13117 -- is an entity reference.
13119 -- Is this right? what about x'first where x is a variable???
13121 when N_Attribute_Reference =>
13122 Attribute_Reference : declare
13124 function Side_Effect_Free_Attribute
13125 (Attribute_Name : Name_Id) return Boolean;
13126 -- Returns True if evaluation of the given attribute is
13127 -- considered side-effect free (independent of prefix and
13128 -- arguments).
13130 --------------------------------
13131 -- Side_Effect_Free_Attribute --
13132 --------------------------------
13134 function Side_Effect_Free_Attribute
13135 (Attribute_Name : Name_Id) return Boolean
13137 begin
13138 case Attribute_Name is
13139 when Name_Input =>
13140 return False;
13142 when Name_Image
13143 | Name_Img
13144 | Name_Wide_Image
13145 | Name_Wide_Wide_Image
13147 -- CodePeer doesn't want to see replicated copies of
13148 -- 'Image calls.
13150 return not CodePeer_Mode;
13152 when others =>
13153 return True;
13154 end case;
13155 end Side_Effect_Free_Attribute;
13157 -- Start of processing for Attribute_Reference
13159 begin
13160 return
13161 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13162 and then Side_Effect_Free_Attribute (Attribute_Name (N))
13163 and then (Is_Entity_Name (Prefix (N))
13164 or else Side_Effect_Free
13165 (Prefix (N), Name_Req, Variable_Ref));
13166 end Attribute_Reference;
13168 -- A binary operator is side effect free if and both operands are
13169 -- side effect free. For this purpose binary operators include
13170 -- membership tests and short circuit forms.
13172 when N_Binary_Op
13173 | N_Membership_Test
13174 | N_Short_Circuit
13176 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13177 and then
13178 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13180 -- An explicit dereference is side effect free only if it is
13181 -- a side effect free prefixed reference.
13183 when N_Explicit_Dereference =>
13184 return Safe_Prefixed_Reference (N);
13186 -- An expression with action is side effect free if its expression
13187 -- is side effect free and it has no actions.
13189 when N_Expression_With_Actions =>
13190 return
13191 Is_Empty_List (Actions (N))
13192 and then Side_Effect_Free
13193 (Expression (N), Name_Req, Variable_Ref);
13195 -- A call to _rep_to_pos is side effect free, since we generate
13196 -- this pure function call ourselves. Moreover it is critically
13197 -- important to make this exception, since otherwise we can have
13198 -- discriminants in array components which don't look side effect
13199 -- free in the case of an array whose index type is an enumeration
13200 -- type with an enumeration rep clause.
13202 -- All other function calls are not side effect free
13204 when N_Function_Call =>
13205 return
13206 Nkind (Name (N)) = N_Identifier
13207 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
13208 and then Side_Effect_Free
13209 (First (Parameter_Associations (N)),
13210 Name_Req, Variable_Ref);
13212 -- An IF expression is side effect free if it's of a scalar type, and
13213 -- all its components are all side effect free (conditions and then
13214 -- actions and else actions). We restrict to scalar types, since it
13215 -- is annoying to deal with things like (if A then B else C)'First
13216 -- where the type involved is a string type.
13218 when N_If_Expression =>
13219 return
13220 Is_Scalar_Type (Typ)
13221 and then Side_Effect_Free
13222 (Expressions (N), Name_Req, Variable_Ref);
13224 -- An indexed component is side effect free if it is a side
13225 -- effect free prefixed reference and all the indexing
13226 -- expressions are side effect free.
13228 when N_Indexed_Component =>
13229 return
13230 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13231 and then Safe_Prefixed_Reference (N);
13233 -- A type qualification, type conversion, or unchecked expression is
13234 -- side effect free if the expression is side effect free.
13236 when N_Qualified_Expression
13237 | N_Type_Conversion
13238 | N_Unchecked_Expression
13240 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13242 -- A selected component is side effect free only if it is a side
13243 -- effect free prefixed reference.
13245 when N_Selected_Component =>
13246 return Safe_Prefixed_Reference (N);
13248 -- A range is side effect free if the bounds are side effect free
13250 when N_Range =>
13251 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13252 and then
13253 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13255 -- A slice is side effect free if it is a side effect free
13256 -- prefixed reference and the bounds are side effect free.
13258 when N_Slice =>
13259 return
13260 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13261 and then Safe_Prefixed_Reference (N);
13263 -- A unary operator is side effect free if the operand
13264 -- is side effect free.
13266 when N_Unary_Op =>
13267 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13269 -- An unchecked type conversion is side effect free only if it
13270 -- is safe and its argument is side effect free.
13272 when N_Unchecked_Type_Conversion =>
13273 return
13274 Safe_Unchecked_Type_Conversion (N)
13275 and then Side_Effect_Free
13276 (Expression (N), Name_Req, Variable_Ref);
13278 -- A literal is side effect free
13280 when N_Character_Literal
13281 | N_Integer_Literal
13282 | N_Real_Literal
13283 | N_String_Literal
13285 return True;
13287 -- We consider that anything else has side effects. This is a bit
13288 -- crude, but we are pretty close for most common cases, and we
13289 -- are certainly correct (i.e. we never return True when the
13290 -- answer should be False).
13292 when others =>
13293 return False;
13294 end case;
13295 end Side_Effect_Free;
13297 -- A list is side effect free if all elements of the list are side
13298 -- effect free.
13300 function Side_Effect_Free
13301 (L : List_Id;
13302 Name_Req : Boolean := False;
13303 Variable_Ref : Boolean := False) return Boolean
13305 N : Node_Id;
13307 begin
13308 if L = No_List or else L = Error_List then
13309 return True;
13311 else
13312 N := First (L);
13313 while Present (N) loop
13314 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13315 return False;
13316 else
13317 Next (N);
13318 end if;
13319 end loop;
13321 return True;
13322 end if;
13323 end Side_Effect_Free;
13325 ----------------------------------
13326 -- Silly_Boolean_Array_Not_Test --
13327 ----------------------------------
13329 -- This procedure implements an odd and silly test. We explicitly check
13330 -- for the case where the 'First of the component type is equal to the
13331 -- 'Last of this component type, and if this is the case, we make sure
13332 -- that constraint error is raised. The reason is that the NOT is bound
13333 -- to cause CE in this case, and we will not otherwise catch it.
13335 -- No such check is required for AND and OR, since for both these cases
13336 -- False op False = False, and True op True = True. For the XOR case,
13337 -- see Silly_Boolean_Array_Xor_Test.
13339 -- Believe it or not, this was reported as a bug. Note that nearly always,
13340 -- the test will evaluate statically to False, so the code will be
13341 -- statically removed, and no extra overhead caused.
13343 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13344 Loc : constant Source_Ptr := Sloc (N);
13345 CT : constant Entity_Id := Component_Type (T);
13347 begin
13348 -- The check we install is
13350 -- constraint_error when
13351 -- component_type'first = component_type'last
13352 -- and then array_type'Length /= 0)
13354 -- We need the last guard because we don't want to raise CE for empty
13355 -- arrays since no out of range values result. (Empty arrays with a
13356 -- component type of True .. True -- very useful -- even the ACATS
13357 -- does not test that marginal case).
13359 Insert_Action (N,
13360 Make_Raise_Constraint_Error (Loc,
13361 Condition =>
13362 Make_And_Then (Loc,
13363 Left_Opnd =>
13364 Make_Op_Eq (Loc,
13365 Left_Opnd =>
13366 Make_Attribute_Reference (Loc,
13367 Prefix => New_Occurrence_Of (CT, Loc),
13368 Attribute_Name => Name_First),
13370 Right_Opnd =>
13371 Make_Attribute_Reference (Loc,
13372 Prefix => New_Occurrence_Of (CT, Loc),
13373 Attribute_Name => Name_Last)),
13375 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13376 Reason => CE_Range_Check_Failed));
13377 end Silly_Boolean_Array_Not_Test;
13379 ----------------------------------
13380 -- Silly_Boolean_Array_Xor_Test --
13381 ----------------------------------
13383 -- This procedure implements an odd and silly test. We explicitly check
13384 -- for the XOR case where the component type is True .. True, since this
13385 -- will raise constraint error. A special check is required since CE
13386 -- will not be generated otherwise (cf Expand_Packed_Not).
13388 -- No such check is required for AND and OR, since for both these cases
13389 -- False op False = False, and True op True = True, and no check is
13390 -- required for the case of False .. False, since False xor False = False.
13391 -- See also Silly_Boolean_Array_Not_Test
13393 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13394 Loc : constant Source_Ptr := Sloc (N);
13395 CT : constant Entity_Id := Component_Type (T);
13397 begin
13398 -- The check we install is
13400 -- constraint_error when
13401 -- Boolean (component_type'First)
13402 -- and then Boolean (component_type'Last)
13403 -- and then array_type'Length /= 0)
13405 -- We need the last guard because we don't want to raise CE for empty
13406 -- arrays since no out of range values result (Empty arrays with a
13407 -- component type of True .. True -- very useful -- even the ACATS
13408 -- does not test that marginal case).
13410 Insert_Action (N,
13411 Make_Raise_Constraint_Error (Loc,
13412 Condition =>
13413 Make_And_Then (Loc,
13414 Left_Opnd =>
13415 Make_And_Then (Loc,
13416 Left_Opnd =>
13417 Convert_To (Standard_Boolean,
13418 Make_Attribute_Reference (Loc,
13419 Prefix => New_Occurrence_Of (CT, Loc),
13420 Attribute_Name => Name_First)),
13422 Right_Opnd =>
13423 Convert_To (Standard_Boolean,
13424 Make_Attribute_Reference (Loc,
13425 Prefix => New_Occurrence_Of (CT, Loc),
13426 Attribute_Name => Name_Last))),
13428 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13429 Reason => CE_Range_Check_Failed));
13430 end Silly_Boolean_Array_Xor_Test;
13432 --------------------------
13433 -- Target_Has_Fixed_Ops --
13434 --------------------------
13436 Integer_Sized_Small : Ureal;
13437 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13438 -- called (we don't want to compute it more than once).
13440 Long_Integer_Sized_Small : Ureal;
13441 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13442 -- is called (we don't want to compute it more than once)
13444 First_Time_For_THFO : Boolean := True;
13445 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13447 function Target_Has_Fixed_Ops
13448 (Left_Typ : Entity_Id;
13449 Right_Typ : Entity_Id;
13450 Result_Typ : Entity_Id) return Boolean
13452 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13453 -- Return True if the given type is a fixed-point type with a small
13454 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13455 -- an absolute value less than 1.0. This is currently limited to
13456 -- fixed-point types that map to Integer or Long_Integer.
13458 ------------------------
13459 -- Is_Fractional_Type --
13460 ------------------------
13462 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13463 begin
13464 if Esize (Typ) = Standard_Integer_Size then
13465 return Small_Value (Typ) = Integer_Sized_Small;
13467 elsif Esize (Typ) = Standard_Long_Integer_Size then
13468 return Small_Value (Typ) = Long_Integer_Sized_Small;
13470 else
13471 return False;
13472 end if;
13473 end Is_Fractional_Type;
13475 -- Start of processing for Target_Has_Fixed_Ops
13477 begin
13478 -- Return False if Fractional_Fixed_Ops_On_Target is false
13480 if not Fractional_Fixed_Ops_On_Target then
13481 return False;
13482 end if;
13484 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13485 -- standard constants used by Is_Fractional_Type.
13487 if First_Time_For_THFO then
13488 First_Time_For_THFO := False;
13490 Integer_Sized_Small :=
13491 UR_From_Components
13492 (Num => Uint_1,
13493 Den => UI_From_Int (Standard_Integer_Size - 1),
13494 Rbase => 2);
13496 Long_Integer_Sized_Small :=
13497 UR_From_Components
13498 (Num => Uint_1,
13499 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13500 Rbase => 2);
13501 end if;
13503 -- Return True if target supports fixed-by-fixed multiply/divide for
13504 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13505 -- and result types are equivalent fractional types.
13507 return Is_Fractional_Type (Base_Type (Left_Typ))
13508 and then Is_Fractional_Type (Base_Type (Right_Typ))
13509 and then Is_Fractional_Type (Base_Type (Result_Typ))
13510 and then Esize (Left_Typ) = Esize (Right_Typ)
13511 and then Esize (Left_Typ) = Esize (Result_Typ);
13512 end Target_Has_Fixed_Ops;
13514 -------------------
13515 -- Type_Map_Hash --
13516 -------------------
13518 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13519 begin
13520 return Type_Map_Header (Id mod Type_Map_Size);
13521 end Type_Map_Hash;
13523 ------------------------------------------
13524 -- Type_May_Have_Bit_Aligned_Components --
13525 ------------------------------------------
13527 function Type_May_Have_Bit_Aligned_Components
13528 (Typ : Entity_Id) return Boolean
13530 begin
13531 -- Array type, check component type
13533 if Is_Array_Type (Typ) then
13534 return
13535 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13537 -- Record type, check components
13539 elsif Is_Record_Type (Typ) then
13540 declare
13541 E : Entity_Id;
13543 begin
13544 E := First_Component_Or_Discriminant (Typ);
13545 while Present (E) loop
13546 if Component_May_Be_Bit_Aligned (E)
13547 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13548 then
13549 return True;
13550 end if;
13552 Next_Component_Or_Discriminant (E);
13553 end loop;
13555 return False;
13556 end;
13558 -- Type other than array or record is always OK
13560 else
13561 return False;
13562 end if;
13563 end Type_May_Have_Bit_Aligned_Components;
13565 -------------------------------
13566 -- Update_Primitives_Mapping --
13567 -------------------------------
13569 procedure Update_Primitives_Mapping
13570 (Inher_Id : Entity_Id;
13571 Subp_Id : Entity_Id)
13573 begin
13574 Map_Types
13575 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13576 Derived_Type => Find_Dispatching_Type (Subp_Id));
13577 end Update_Primitives_Mapping;
13579 ----------------------------------
13580 -- Within_Case_Or_If_Expression --
13581 ----------------------------------
13583 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13584 Par : Node_Id;
13586 begin
13587 -- Locate an enclosing case or if expression. Note that these constructs
13588 -- can be expanded into Expression_With_Actions, hence the test of the
13589 -- original node.
13591 Par := Parent (N);
13592 while Present (Par) loop
13593 if Nkind_In (Original_Node (Par), N_Case_Expression,
13594 N_If_Expression)
13595 then
13596 return True;
13598 -- Prevent the search from going too far
13600 elsif Is_Body_Or_Package_Declaration (Par) then
13601 return False;
13602 end if;
13604 Par := Parent (Par);
13605 end loop;
13607 return False;
13608 end Within_Case_Or_If_Expression;
13610 --------------------------------
13611 -- Within_Internal_Subprogram --
13612 --------------------------------
13614 function Within_Internal_Subprogram return Boolean is
13615 S : Entity_Id;
13617 begin
13618 S := Current_Scope;
13619 while Present (S) and then not Is_Subprogram (S) loop
13620 S := Scope (S);
13621 end loop;
13623 return Present (S)
13624 and then Get_TSS_Name (S) /= TSS_Null
13625 and then not Is_Predicate_Function (S)
13626 and then not Is_Predicate_Function_M (S);
13627 end Within_Internal_Subprogram;
13629 end Exp_Util;