* lto.c (do_stream_out): Add PART parameter; open dump file.
[official-gcc.git] / gcc / ada / exp_util.adb
blob3bed5080ffc0002352eddc5fd44879733b7ecc78
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 begin
8406 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
8407 or else Esize (C) mod Align_In_Bits /= 0
8408 then
8409 return True;
8410 end if;
8411 end;
8412 end if;
8414 -- Otherwise, for a component reference, test prefix
8416 return Is_Possibly_Unaligned_Object (P);
8417 end;
8419 -- If not a component reference, must be aligned
8421 else
8422 return False;
8423 end if;
8424 end Is_Possibly_Unaligned_Object;
8426 ---------------------------------
8427 -- Is_Possibly_Unaligned_Slice --
8428 ---------------------------------
8430 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8431 begin
8432 -- Go to renamed object
8434 if Is_Entity_Name (N)
8435 and then Is_Object (Entity (N))
8436 and then Present (Renamed_Object (Entity (N)))
8437 then
8438 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8439 end if;
8441 -- The reference must be a slice
8443 if Nkind (N) /= N_Slice then
8444 return False;
8445 end if;
8447 -- We only need to worry if the target has strict alignment
8449 if not Target_Strict_Alignment then
8450 return False;
8451 end if;
8453 -- If it is a slice, then look at the array type being sliced
8455 declare
8456 Sarr : constant Node_Id := Prefix (N);
8457 -- Prefix of the slice, i.e. the array being sliced
8459 Styp : constant Entity_Id := Etype (Prefix (N));
8460 -- Type of the array being sliced
8462 Pref : Node_Id;
8463 Ptyp : Entity_Id;
8465 begin
8466 -- The problems arise if the array object that is being sliced
8467 -- is a component of a record or array, and we cannot guarantee
8468 -- the alignment of the array within its containing object.
8470 -- To investigate this, we look at successive prefixes to see
8471 -- if we have a worrisome indexed or selected component.
8473 Pref := Sarr;
8474 loop
8475 -- Case of array is part of an indexed component reference
8477 if Nkind (Pref) = N_Indexed_Component then
8478 Ptyp := Etype (Prefix (Pref));
8480 -- The only problematic case is when the array is packed, in
8481 -- which case we really know nothing about the alignment of
8482 -- individual components.
8484 if Is_Bit_Packed_Array (Ptyp) then
8485 return True;
8486 end if;
8488 -- Case of array is part of a selected component reference
8490 elsif Nkind (Pref) = N_Selected_Component then
8491 Ptyp := Etype (Prefix (Pref));
8493 -- We are definitely in trouble if the record in question
8494 -- has an alignment, and either we know this alignment is
8495 -- inconsistent with the alignment of the slice, or we don't
8496 -- know what the alignment of the slice should be.
8498 if Known_Alignment (Ptyp)
8499 and then (Unknown_Alignment (Styp)
8500 or else Alignment (Styp) > Alignment (Ptyp))
8501 then
8502 return True;
8503 end if;
8505 -- We are in potential trouble if the record type is packed.
8506 -- We could special case when we know that the array is the
8507 -- first component, but that's not such a simple case ???
8509 if Is_Packed (Ptyp) then
8510 return True;
8511 end if;
8513 -- We are in trouble if there is a component clause, and
8514 -- either we do not know the alignment of the slice, or
8515 -- the alignment of the slice is inconsistent with the
8516 -- bit position specified by the component clause.
8518 declare
8519 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8520 begin
8521 if Present (Component_Clause (Field))
8522 and then
8523 (Unknown_Alignment (Styp)
8524 or else
8525 (Component_Bit_Offset (Field) mod
8526 (System_Storage_Unit * Alignment (Styp))) /= 0)
8527 then
8528 return True;
8529 end if;
8530 end;
8532 -- For cases other than selected or indexed components we know we
8533 -- are OK, since no issues arise over alignment.
8535 else
8536 return False;
8537 end if;
8539 -- We processed an indexed component or selected component
8540 -- reference that looked safe, so keep checking prefixes.
8542 Pref := Prefix (Pref);
8543 end loop;
8544 end;
8545 end Is_Possibly_Unaligned_Slice;
8547 -------------------------------
8548 -- Is_Related_To_Func_Return --
8549 -------------------------------
8551 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8552 Expr : constant Node_Id := Related_Expression (Id);
8553 begin
8554 return
8555 Present (Expr)
8556 and then Nkind (Expr) = N_Explicit_Dereference
8557 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8558 end Is_Related_To_Func_Return;
8560 --------------------------------
8561 -- Is_Ref_To_Bit_Packed_Array --
8562 --------------------------------
8564 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8565 Result : Boolean;
8566 Expr : Node_Id;
8568 begin
8569 if Is_Entity_Name (N)
8570 and then Is_Object (Entity (N))
8571 and then Present (Renamed_Object (Entity (N)))
8572 then
8573 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8574 end if;
8576 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8577 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8578 Result := True;
8579 else
8580 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8581 end if;
8583 if Result and then Nkind (N) = N_Indexed_Component then
8584 Expr := First (Expressions (N));
8585 while Present (Expr) loop
8586 Force_Evaluation (Expr);
8587 Next (Expr);
8588 end loop;
8589 end if;
8591 return Result;
8593 else
8594 return False;
8595 end if;
8596 end Is_Ref_To_Bit_Packed_Array;
8598 --------------------------------
8599 -- Is_Ref_To_Bit_Packed_Slice --
8600 --------------------------------
8602 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8603 begin
8604 if Nkind (N) = N_Type_Conversion then
8605 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8607 elsif Is_Entity_Name (N)
8608 and then Is_Object (Entity (N))
8609 and then Present (Renamed_Object (Entity (N)))
8610 then
8611 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8613 elsif Nkind (N) = N_Slice
8614 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8615 then
8616 return True;
8618 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8619 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8621 else
8622 return False;
8623 end if;
8624 end Is_Ref_To_Bit_Packed_Slice;
8626 -----------------------
8627 -- Is_Renamed_Object --
8628 -----------------------
8630 function Is_Renamed_Object (N : Node_Id) return Boolean is
8631 Pnod : constant Node_Id := Parent (N);
8632 Kind : constant Node_Kind := Nkind (Pnod);
8633 begin
8634 if Kind = N_Object_Renaming_Declaration then
8635 return True;
8636 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8637 return Is_Renamed_Object (Pnod);
8638 else
8639 return False;
8640 end if;
8641 end Is_Renamed_Object;
8643 --------------------------------------
8644 -- Is_Secondary_Stack_BIP_Func_Call --
8645 --------------------------------------
8647 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8648 Alloc_Nam : Name_Id := No_Name;
8649 Actual : Node_Id;
8650 Call : Node_Id := Expr;
8651 Formal : Node_Id;
8652 Param : Node_Id;
8654 begin
8655 -- Build-in-place calls usually appear in 'reference format. Note that
8656 -- the accessibility check machinery may add an extra 'reference due to
8657 -- side effect removal.
8659 while Nkind (Call) = N_Reference loop
8660 Call := Prefix (Call);
8661 end loop;
8663 Call := Unqual_Conv (Call);
8665 if Is_Build_In_Place_Function_Call (Call) then
8667 -- Examine all parameter associations of the function call
8669 Param := First (Parameter_Associations (Call));
8670 while Present (Param) loop
8671 if Nkind (Param) = N_Parameter_Association then
8672 Formal := Selector_Name (Param);
8673 Actual := Explicit_Actual_Parameter (Param);
8675 -- Construct the name of formal BIPalloc. It is much easier to
8676 -- extract the name of the function using an arbitrary formal's
8677 -- scope rather than the Name field of Call.
8679 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8680 Alloc_Nam :=
8681 New_External_Name
8682 (Chars (Scope (Entity (Formal))),
8683 BIP_Formal_Suffix (BIP_Alloc_Form));
8684 end if;
8686 -- A match for BIPalloc => 2 has been found
8688 if Chars (Formal) = Alloc_Nam
8689 and then Nkind (Actual) = N_Integer_Literal
8690 and then Intval (Actual) = Uint_2
8691 then
8692 return True;
8693 end if;
8694 end if;
8696 Next (Param);
8697 end loop;
8698 end if;
8700 return False;
8701 end Is_Secondary_Stack_BIP_Func_Call;
8703 -------------------------------------
8704 -- Is_Tag_To_Class_Wide_Conversion --
8705 -------------------------------------
8707 function Is_Tag_To_Class_Wide_Conversion
8708 (Obj_Id : Entity_Id) return Boolean
8710 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8712 begin
8713 return
8714 Is_Class_Wide_Type (Etype (Obj_Id))
8715 and then Present (Expr)
8716 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8717 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8718 end Is_Tag_To_Class_Wide_Conversion;
8720 ----------------------------
8721 -- Is_Untagged_Derivation --
8722 ----------------------------
8724 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8725 begin
8726 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8727 or else
8728 (Is_Private_Type (T) and then Present (Full_View (T))
8729 and then not Is_Tagged_Type (Full_View (T))
8730 and then Is_Derived_Type (Full_View (T))
8731 and then Etype (Full_View (T)) /= T);
8732 end Is_Untagged_Derivation;
8734 ------------------------------------
8735 -- Is_Untagged_Private_Derivation --
8736 ------------------------------------
8738 function Is_Untagged_Private_Derivation
8739 (Priv_Typ : Entity_Id;
8740 Full_Typ : Entity_Id) return Boolean
8742 begin
8743 return
8744 Present (Priv_Typ)
8745 and then Is_Untagged_Derivation (Priv_Typ)
8746 and then Is_Private_Type (Etype (Priv_Typ))
8747 and then Present (Full_Typ)
8748 and then Is_Itype (Full_Typ);
8749 end Is_Untagged_Private_Derivation;
8751 ------------------------------
8752 -- Is_Verifiable_DIC_Pragma --
8753 ------------------------------
8755 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
8756 Args : constant List_Id := Pragma_Argument_Associations (Prag);
8758 begin
8759 -- To qualify as verifiable, a DIC pragma must have a non-null argument
8761 return
8762 Present (Args)
8763 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
8764 end Is_Verifiable_DIC_Pragma;
8766 ---------------------------
8767 -- Is_Volatile_Reference --
8768 ---------------------------
8770 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8771 begin
8772 -- Only source references are to be treated as volatile, internally
8773 -- generated stuff cannot have volatile external effects.
8775 if not Comes_From_Source (N) then
8776 return False;
8778 -- Never true for reference to a type
8780 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8781 return False;
8783 -- Never true for a compile time known constant
8785 elsif Compile_Time_Known_Value (N) then
8786 return False;
8788 -- True if object reference with volatile type
8790 elsif Is_Volatile_Object (N) then
8791 return True;
8793 -- True if reference to volatile entity
8795 elsif Is_Entity_Name (N) then
8796 return Treat_As_Volatile (Entity (N));
8798 -- True for slice of volatile array
8800 elsif Nkind (N) = N_Slice then
8801 return Is_Volatile_Reference (Prefix (N));
8803 -- True if volatile component
8805 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8806 if (Is_Entity_Name (Prefix (N))
8807 and then Has_Volatile_Components (Entity (Prefix (N))))
8808 or else (Present (Etype (Prefix (N)))
8809 and then Has_Volatile_Components (Etype (Prefix (N))))
8810 then
8811 return True;
8812 else
8813 return Is_Volatile_Reference (Prefix (N));
8814 end if;
8816 -- Otherwise false
8818 else
8819 return False;
8820 end if;
8821 end Is_Volatile_Reference;
8823 --------------------
8824 -- Kill_Dead_Code --
8825 --------------------
8827 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8828 W : Boolean := Warn;
8829 -- Set False if warnings suppressed
8831 begin
8832 if Present (N) then
8833 Remove_Warning_Messages (N);
8835 -- Update the internal structures of the ABE mechanism in case the
8836 -- dead node is an elaboration scenario.
8838 Kill_Elaboration_Scenario (N);
8840 -- Generate warning if appropriate
8842 if W then
8844 -- We suppress the warning if this code is under control of an
8845 -- if statement, whose condition is a simple identifier, and
8846 -- either we are in an instance, or warnings off is set for this
8847 -- identifier. The reason for killing it in the instance case is
8848 -- that it is common and reasonable for code to be deleted in
8849 -- instances for various reasons.
8851 -- Could we use Is_Statically_Unevaluated here???
8853 if Nkind (Parent (N)) = N_If_Statement then
8854 declare
8855 C : constant Node_Id := Condition (Parent (N));
8856 begin
8857 if Nkind (C) = N_Identifier
8858 and then
8859 (In_Instance
8860 or else (Present (Entity (C))
8861 and then Has_Warnings_Off (Entity (C))))
8862 then
8863 W := False;
8864 end if;
8865 end;
8866 end if;
8868 -- Generate warning if not suppressed
8870 if W then
8871 Error_Msg_F
8872 ("?t?this code can never be executed and has been deleted!",
8874 end if;
8875 end if;
8877 -- Recurse into block statements and bodies to process declarations
8878 -- and statements.
8880 if Nkind (N) = N_Block_Statement
8881 or else Nkind (N) = N_Subprogram_Body
8882 or else Nkind (N) = N_Package_Body
8883 then
8884 Kill_Dead_Code (Declarations (N), False);
8885 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8887 if Nkind (N) = N_Subprogram_Body then
8888 Set_Is_Eliminated (Defining_Entity (N));
8889 end if;
8891 elsif Nkind (N) = N_Package_Declaration then
8892 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8893 Kill_Dead_Code (Private_Declarations (Specification (N)));
8895 -- ??? After this point, Delete_Tree has been called on all
8896 -- declarations in Specification (N), so references to entities
8897 -- therein look suspicious.
8899 declare
8900 E : Entity_Id := First_Entity (Defining_Entity (N));
8902 begin
8903 while Present (E) loop
8904 if Ekind (E) = E_Operator then
8905 Set_Is_Eliminated (E);
8906 end if;
8908 Next_Entity (E);
8909 end loop;
8910 end;
8912 -- Recurse into composite statement to kill individual statements in
8913 -- particular instantiations.
8915 elsif Nkind (N) = N_If_Statement then
8916 Kill_Dead_Code (Then_Statements (N));
8917 Kill_Dead_Code (Elsif_Parts (N));
8918 Kill_Dead_Code (Else_Statements (N));
8920 elsif Nkind (N) = N_Loop_Statement then
8921 Kill_Dead_Code (Statements (N));
8923 elsif Nkind (N) = N_Case_Statement then
8924 declare
8925 Alt : Node_Id;
8926 begin
8927 Alt := First (Alternatives (N));
8928 while Present (Alt) loop
8929 Kill_Dead_Code (Statements (Alt));
8930 Next (Alt);
8931 end loop;
8932 end;
8934 elsif Nkind (N) = N_Case_Statement_Alternative then
8935 Kill_Dead_Code (Statements (N));
8937 -- Deal with dead instances caused by deleting instantiations
8939 elsif Nkind (N) in N_Generic_Instantiation then
8940 Remove_Dead_Instance (N);
8941 end if;
8942 end if;
8943 end Kill_Dead_Code;
8945 -- Case where argument is a list of nodes to be killed
8947 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8948 N : Node_Id;
8949 W : Boolean;
8951 begin
8952 W := Warn;
8954 if Is_Non_Empty_List (L) then
8955 N := First (L);
8956 while Present (N) loop
8957 Kill_Dead_Code (N, W);
8958 W := False;
8959 Next (N);
8960 end loop;
8961 end if;
8962 end Kill_Dead_Code;
8964 ------------------------
8965 -- Known_Non_Negative --
8966 ------------------------
8968 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8969 begin
8970 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8971 return True;
8973 else
8974 declare
8975 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8976 begin
8977 return
8978 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8979 end;
8980 end if;
8981 end Known_Non_Negative;
8983 -----------------------------
8984 -- Make_CW_Equivalent_Type --
8985 -----------------------------
8987 -- Create a record type used as an equivalent of any member of the class
8988 -- which takes its size from exp.
8990 -- Generate the following code:
8992 -- type Equiv_T is record
8993 -- _parent : T (List of discriminant constraints taken from Exp);
8994 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8995 -- end Equiv_T;
8997 -- ??? Note that this type does not guarantee same alignment as all
8998 -- derived types
9000 function Make_CW_Equivalent_Type
9001 (T : Entity_Id;
9002 E : Node_Id) return Entity_Id
9004 Loc : constant Source_Ptr := Sloc (E);
9005 Root_Typ : constant Entity_Id := Root_Type (T);
9006 List_Def : constant List_Id := Empty_List;
9007 Comp_List : constant List_Id := New_List;
9008 Equiv_Type : Entity_Id;
9009 Range_Type : Entity_Id;
9010 Str_Type : Entity_Id;
9011 Constr_Root : Entity_Id;
9012 Sizexpr : Node_Id;
9014 begin
9015 -- If the root type is already constrained, there are no discriminants
9016 -- in the expression.
9018 if not Has_Discriminants (Root_Typ)
9019 or else Is_Constrained (Root_Typ)
9020 then
9021 Constr_Root := Root_Typ;
9023 -- At this point in the expansion, non-limited view of the type
9024 -- must be available, otherwise the error will be reported later.
9026 if From_Limited_With (Constr_Root)
9027 and then Present (Non_Limited_View (Constr_Root))
9028 then
9029 Constr_Root := Non_Limited_View (Constr_Root);
9030 end if;
9032 else
9033 Constr_Root := Make_Temporary (Loc, 'R');
9035 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9037 Append_To (List_Def,
9038 Make_Subtype_Declaration (Loc,
9039 Defining_Identifier => Constr_Root,
9040 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9041 end if;
9043 -- Generate the range subtype declaration
9045 Range_Type := Make_Temporary (Loc, 'G');
9047 if not Is_Interface (Root_Typ) then
9049 -- subtype rg__xx is
9050 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9052 Sizexpr :=
9053 Make_Op_Subtract (Loc,
9054 Left_Opnd =>
9055 Make_Attribute_Reference (Loc,
9056 Prefix =>
9057 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9058 Attribute_Name => Name_Size),
9059 Right_Opnd =>
9060 Make_Attribute_Reference (Loc,
9061 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9062 Attribute_Name => Name_Object_Size));
9063 else
9064 -- subtype rg__xx is
9065 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9067 Sizexpr :=
9068 Make_Attribute_Reference (Loc,
9069 Prefix =>
9070 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9071 Attribute_Name => Name_Size);
9072 end if;
9074 Set_Paren_Count (Sizexpr, 1);
9076 Append_To (List_Def,
9077 Make_Subtype_Declaration (Loc,
9078 Defining_Identifier => Range_Type,
9079 Subtype_Indication =>
9080 Make_Subtype_Indication (Loc,
9081 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9082 Constraint => Make_Range_Constraint (Loc,
9083 Range_Expression =>
9084 Make_Range (Loc,
9085 Low_Bound => Make_Integer_Literal (Loc, 1),
9086 High_Bound =>
9087 Make_Op_Divide (Loc,
9088 Left_Opnd => Sizexpr,
9089 Right_Opnd => Make_Integer_Literal (Loc,
9090 Intval => System_Storage_Unit)))))));
9092 -- subtype str__nn is Storage_Array (rg__x);
9094 Str_Type := Make_Temporary (Loc, 'S');
9095 Append_To (List_Def,
9096 Make_Subtype_Declaration (Loc,
9097 Defining_Identifier => Str_Type,
9098 Subtype_Indication =>
9099 Make_Subtype_Indication (Loc,
9100 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9101 Constraint =>
9102 Make_Index_Or_Discriminant_Constraint (Loc,
9103 Constraints =>
9104 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9106 -- type Equiv_T is record
9107 -- [ _parent : Tnn; ]
9108 -- E : Str_Type;
9109 -- end Equiv_T;
9111 Equiv_Type := Make_Temporary (Loc, 'T');
9112 Set_Ekind (Equiv_Type, E_Record_Type);
9113 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9115 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9116 -- treatment for this type. In particular, even though _parent's type
9117 -- is a controlled type or contains controlled components, we do not
9118 -- want to set Has_Controlled_Component on it to avoid making it gain
9119 -- an unwanted _controller component.
9121 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9123 -- A class-wide equivalent type does not require initialization
9125 Set_Suppress_Initialization (Equiv_Type);
9127 if not Is_Interface (Root_Typ) then
9128 Append_To (Comp_List,
9129 Make_Component_Declaration (Loc,
9130 Defining_Identifier =>
9131 Make_Defining_Identifier (Loc, Name_uParent),
9132 Component_Definition =>
9133 Make_Component_Definition (Loc,
9134 Aliased_Present => False,
9135 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9136 end if;
9138 Append_To (Comp_List,
9139 Make_Component_Declaration (Loc,
9140 Defining_Identifier => Make_Temporary (Loc, 'C'),
9141 Component_Definition =>
9142 Make_Component_Definition (Loc,
9143 Aliased_Present => False,
9144 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9146 Append_To (List_Def,
9147 Make_Full_Type_Declaration (Loc,
9148 Defining_Identifier => Equiv_Type,
9149 Type_Definition =>
9150 Make_Record_Definition (Loc,
9151 Component_List =>
9152 Make_Component_List (Loc,
9153 Component_Items => Comp_List,
9154 Variant_Part => Empty))));
9156 -- Suppress all checks during the analysis of the expanded code to avoid
9157 -- the generation of spurious warnings under ZFP run-time.
9159 Insert_Actions (E, List_Def, Suppress => All_Checks);
9160 return Equiv_Type;
9161 end Make_CW_Equivalent_Type;
9163 -------------------------
9164 -- Make_Invariant_Call --
9165 -------------------------
9167 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9168 Loc : constant Source_Ptr := Sloc (Expr);
9169 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9171 Proc_Id : Entity_Id;
9173 begin
9174 pragma Assert (Has_Invariants (Typ));
9176 Proc_Id := Invariant_Procedure (Typ);
9177 pragma Assert (Present (Proc_Id));
9179 return
9180 Make_Procedure_Call_Statement (Loc,
9181 Name => New_Occurrence_Of (Proc_Id, Loc),
9182 Parameter_Associations => New_List (Relocate_Node (Expr)));
9183 end Make_Invariant_Call;
9185 ------------------------
9186 -- Make_Literal_Range --
9187 ------------------------
9189 function Make_Literal_Range
9190 (Loc : Source_Ptr;
9191 Literal_Typ : Entity_Id) return Node_Id
9193 Lo : constant Node_Id :=
9194 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9195 Index : constant Entity_Id := Etype (Lo);
9196 Length_Expr : constant Node_Id :=
9197 Make_Op_Subtract (Loc,
9198 Left_Opnd =>
9199 Make_Integer_Literal (Loc,
9200 Intval => String_Literal_Length (Literal_Typ)),
9201 Right_Opnd => Make_Integer_Literal (Loc, 1));
9203 Hi : Node_Id;
9205 begin
9206 Set_Analyzed (Lo, False);
9208 if Is_Integer_Type (Index) then
9209 Hi :=
9210 Make_Op_Add (Loc,
9211 Left_Opnd => New_Copy_Tree (Lo),
9212 Right_Opnd => Length_Expr);
9213 else
9214 Hi :=
9215 Make_Attribute_Reference (Loc,
9216 Attribute_Name => Name_Val,
9217 Prefix => New_Occurrence_Of (Index, Loc),
9218 Expressions => New_List (
9219 Make_Op_Add (Loc,
9220 Left_Opnd =>
9221 Make_Attribute_Reference (Loc,
9222 Attribute_Name => Name_Pos,
9223 Prefix => New_Occurrence_Of (Index, Loc),
9224 Expressions => New_List (New_Copy_Tree (Lo))),
9225 Right_Opnd => Length_Expr)));
9226 end if;
9228 return
9229 Make_Range (Loc,
9230 Low_Bound => Lo,
9231 High_Bound => Hi);
9232 end Make_Literal_Range;
9234 --------------------------
9235 -- Make_Non_Empty_Check --
9236 --------------------------
9238 function Make_Non_Empty_Check
9239 (Loc : Source_Ptr;
9240 N : Node_Id) return Node_Id
9242 begin
9243 return
9244 Make_Op_Ne (Loc,
9245 Left_Opnd =>
9246 Make_Attribute_Reference (Loc,
9247 Attribute_Name => Name_Length,
9248 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9249 Right_Opnd =>
9250 Make_Integer_Literal (Loc, 0));
9251 end Make_Non_Empty_Check;
9253 -------------------------
9254 -- Make_Predicate_Call --
9255 -------------------------
9257 -- WARNING: This routine manages Ghost regions. Return statements must be
9258 -- replaced by gotos which jump to the end of the routine and restore the
9259 -- Ghost mode.
9261 function Make_Predicate_Call
9262 (Typ : Entity_Id;
9263 Expr : Node_Id;
9264 Mem : Boolean := False) return Node_Id
9266 Loc : constant Source_Ptr := Sloc (Expr);
9268 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9269 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9270 -- Save the Ghost-related attributes to restore on exit
9272 Call : Node_Id;
9273 Func_Id : Entity_Id;
9275 begin
9276 Func_Id := Predicate_Function (Typ);
9277 pragma Assert (Present (Func_Id));
9279 -- The related type may be subject to pragma Ghost. Set the mode now to
9280 -- ensure that the call is properly marked as Ghost.
9282 Set_Ghost_Mode (Typ);
9284 -- Call special membership version if requested and available
9286 if Mem and then Present (Predicate_Function_M (Typ)) then
9287 Func_Id := Predicate_Function_M (Typ);
9288 end if;
9290 -- Case of calling normal predicate function
9292 -- If the type is tagged, the expression may be class-wide, in which
9293 -- case it has to be converted to its root type, given that the
9294 -- generated predicate function is not dispatching.
9296 if Is_Tagged_Type (Typ) then
9297 Call :=
9298 Make_Function_Call (Loc,
9299 Name => New_Occurrence_Of (Func_Id, Loc),
9300 Parameter_Associations =>
9301 New_List (Convert_To (Typ, Relocate_Node (Expr))));
9302 else
9303 Call :=
9304 Make_Function_Call (Loc,
9305 Name => New_Occurrence_Of (Func_Id, Loc),
9306 Parameter_Associations => New_List (Relocate_Node (Expr)));
9307 end if;
9309 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9311 return Call;
9312 end Make_Predicate_Call;
9314 --------------------------
9315 -- Make_Predicate_Check --
9316 --------------------------
9318 function Make_Predicate_Check
9319 (Typ : Entity_Id;
9320 Expr : Node_Id) return Node_Id
9322 Loc : constant Source_Ptr := Sloc (Expr);
9324 procedure Add_Failure_Expression (Args : List_Id);
9325 -- Add the failure expression of pragma Predicate_Failure (if any) to
9326 -- list Args.
9328 ----------------------------
9329 -- Add_Failure_Expression --
9330 ----------------------------
9332 procedure Add_Failure_Expression (Args : List_Id) is
9333 function Failure_Expression return Node_Id;
9334 pragma Inline (Failure_Expression);
9335 -- Find aspect or pragma Predicate_Failure that applies to type Typ
9336 -- and return its expression. Return Empty if no such annotation is
9337 -- available.
9339 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean;
9340 pragma Inline (Is_OK_PF_Aspect);
9341 -- Determine whether aspect Asp is a suitable Predicate_Failure
9342 -- aspect that applies to type Typ.
9344 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean;
9345 pragma Inline (Is_OK_PF_Pragma);
9346 -- Determine whether pragma Prag is a suitable Predicate_Failure
9347 -- pragma that applies to type Typ.
9349 procedure Replace_Subtype_Reference (N : Node_Id);
9350 -- Replace the current instance of type Typ denoted by N with
9351 -- expression Expr.
9353 ------------------------
9354 -- Failure_Expression --
9355 ------------------------
9357 function Failure_Expression return Node_Id is
9358 Item : Node_Id;
9360 begin
9361 -- The management of the rep item chain involves "inheritance" of
9362 -- parent type chains. If a parent [sub]type is already subject to
9363 -- pragma Predicate_Failure, then the pragma will also appear in
9364 -- the chain of the child [sub]type, which in turn may possess a
9365 -- pragma of its own. Avoid order-dependent issues by inspecting
9366 -- the rep item chain directly. Note that routine Get_Pragma may
9367 -- return a parent pragma.
9369 Item := First_Rep_Item (Typ);
9370 while Present (Item) loop
9372 -- Predicate_Failure appears as an aspect
9374 if Nkind (Item) = N_Aspect_Specification
9375 and then Is_OK_PF_Aspect (Item)
9376 then
9377 return Expression (Item);
9379 -- Predicate_Failure appears as a pragma
9381 elsif Nkind (Item) = N_Pragma
9382 and then Is_OK_PF_Pragma (Item)
9383 then
9384 return
9385 Get_Pragma_Arg
9386 (Next (First (Pragma_Argument_Associations (Item))));
9387 end if;
9389 Item := Next_Rep_Item (Item);
9390 end loop;
9392 return Empty;
9393 end Failure_Expression;
9395 ---------------------
9396 -- Is_OK_PF_Aspect --
9397 ---------------------
9399 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean is
9400 begin
9401 -- To qualify, the aspect must apply to the type subjected to the
9402 -- predicate check.
9404 return
9405 Chars (Identifier (Asp)) = Name_Predicate_Failure
9406 and then Present (Entity (Asp))
9407 and then Entity (Asp) = Typ;
9408 end Is_OK_PF_Aspect;
9410 ---------------------
9411 -- Is_OK_PF_Pragma --
9412 ---------------------
9414 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean is
9415 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9416 Typ_Arg : Node_Id;
9418 begin
9419 -- Nothing to do when the pragma does not denote Predicate_Failure
9421 if Pragma_Name (Prag) /= Name_Predicate_Failure then
9422 return False;
9424 -- Nothing to do when the pragma lacks arguments, in which case it
9425 -- is illegal.
9427 elsif No (Args) or else Is_Empty_List (Args) then
9428 return False;
9429 end if;
9431 Typ_Arg := Get_Pragma_Arg (First (Args));
9433 -- To qualify, the local name argument of the pragma must denote
9434 -- the type subjected to the predicate check.
9436 return
9437 Is_Entity_Name (Typ_Arg)
9438 and then Present (Entity (Typ_Arg))
9439 and then Entity (Typ_Arg) = Typ;
9440 end Is_OK_PF_Pragma;
9442 --------------------------------
9443 -- Replace_Subtype_Reference --
9444 --------------------------------
9446 procedure Replace_Subtype_Reference (N : Node_Id) is
9447 begin
9448 Rewrite (N, New_Copy_Tree (Expr));
9450 -- We want to treat the node as if it comes from source, so that
9451 -- ASIS will not ignore it.
9453 Set_Comes_From_Source (N, True);
9454 end Replace_Subtype_Reference;
9456 procedure Replace_Subtype_References is
9457 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9459 -- Local variables
9461 PF_Expr : constant Node_Id := Failure_Expression;
9462 Expr : Node_Id;
9464 -- Start of processing for Add_Failure_Expression
9466 begin
9467 if Present (PF_Expr) then
9469 -- Replace any occurrences of the current instance of the type
9470 -- with the object subjected to the predicate check.
9472 Expr := New_Copy_Tree (PF_Expr);
9473 Replace_Subtype_References (Expr, Typ);
9475 -- The failure expression appears as the third argument of the
9476 -- Check pragma.
9478 Append_To (Args,
9479 Make_Pragma_Argument_Association (Loc,
9480 Expression => Expr));
9481 end if;
9482 end Add_Failure_Expression;
9484 -- Local variables
9486 Args : List_Id;
9487 Nam : Name_Id;
9489 -- Start of processing for Make_Predicate_Check
9491 begin
9492 -- If predicate checks are suppressed, then return a null statement. For
9493 -- this call, we check only the scope setting. If the caller wants to
9494 -- check a specific entity's setting, they must do it manually.
9496 if Predicate_Checks_Suppressed (Empty) then
9497 return Make_Null_Statement (Loc);
9498 end if;
9500 -- Do not generate a check within an internal subprogram (stream
9501 -- functions and the like, including including predicate functions).
9503 if Within_Internal_Subprogram then
9504 return Make_Null_Statement (Loc);
9505 end if;
9507 -- Compute proper name to use, we need to get this right so that the
9508 -- right set of check policies apply to the Check pragma we are making.
9510 if Has_Dynamic_Predicate_Aspect (Typ) then
9511 Nam := Name_Dynamic_Predicate;
9512 elsif Has_Static_Predicate_Aspect (Typ) then
9513 Nam := Name_Static_Predicate;
9514 else
9515 Nam := Name_Predicate;
9516 end if;
9518 Args := New_List (
9519 Make_Pragma_Argument_Association (Loc,
9520 Expression => Make_Identifier (Loc, Nam)),
9521 Make_Pragma_Argument_Association (Loc,
9522 Expression => Make_Predicate_Call (Typ, Expr)));
9524 -- If the subtype is subject to pragma Predicate_Failure, add the
9525 -- failure expression as an additional parameter.
9527 Add_Failure_Expression (Args);
9529 return
9530 Make_Pragma (Loc,
9531 Chars => Name_Check,
9532 Pragma_Argument_Associations => Args);
9533 end Make_Predicate_Check;
9535 ----------------------------
9536 -- Make_Subtype_From_Expr --
9537 ----------------------------
9539 -- 1. If Expr is an unconstrained array expression, creates
9540 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9542 -- 2. If Expr is a unconstrained discriminated type expression, creates
9543 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9545 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9547 function Make_Subtype_From_Expr
9548 (E : Node_Id;
9549 Unc_Typ : Entity_Id;
9550 Related_Id : Entity_Id := Empty) return Node_Id
9552 List_Constr : constant List_Id := New_List;
9553 Loc : constant Source_Ptr := Sloc (E);
9554 D : Entity_Id;
9555 Full_Exp : Node_Id;
9556 Full_Subtyp : Entity_Id;
9557 High_Bound : Entity_Id;
9558 Index_Typ : Entity_Id;
9559 Low_Bound : Entity_Id;
9560 Priv_Subtyp : Entity_Id;
9561 Utyp : Entity_Id;
9563 begin
9564 if Is_Private_Type (Unc_Typ)
9565 and then Has_Unknown_Discriminants (Unc_Typ)
9566 then
9567 -- The caller requests a unique external name for both the private
9568 -- and the full subtype.
9570 if Present (Related_Id) then
9571 Full_Subtyp :=
9572 Make_Defining_Identifier (Loc,
9573 Chars => New_External_Name (Chars (Related_Id), 'C'));
9574 Priv_Subtyp :=
9575 Make_Defining_Identifier (Loc,
9576 Chars => New_External_Name (Chars (Related_Id), 'P'));
9578 else
9579 Full_Subtyp := Make_Temporary (Loc, 'C');
9580 Priv_Subtyp := Make_Temporary (Loc, 'P');
9581 end if;
9583 -- Prepare the subtype completion. Use the base type to find the
9584 -- underlying type because the type may be a generic actual or an
9585 -- explicit subtype.
9587 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9589 Full_Exp :=
9590 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9591 Set_Parent (Full_Exp, Parent (E));
9593 Insert_Action (E,
9594 Make_Subtype_Declaration (Loc,
9595 Defining_Identifier => Full_Subtyp,
9596 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9598 -- Define the dummy private subtype
9600 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9601 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9602 Set_Scope (Priv_Subtyp, Full_Subtyp);
9603 Set_Is_Constrained (Priv_Subtyp);
9604 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9605 Set_Is_Itype (Priv_Subtyp);
9606 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9608 if Is_Tagged_Type (Priv_Subtyp) then
9609 Set_Class_Wide_Type
9610 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9611 Set_Direct_Primitive_Operations (Priv_Subtyp,
9612 Direct_Primitive_Operations (Unc_Typ));
9613 end if;
9615 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9617 return New_Occurrence_Of (Priv_Subtyp, Loc);
9619 elsif Is_Array_Type (Unc_Typ) then
9620 Index_Typ := First_Index (Unc_Typ);
9621 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9623 -- Capture the bounds of each index constraint in case the context
9624 -- is an object declaration of an unconstrained type initialized
9625 -- by a function call:
9627 -- Obj : Unconstr_Typ := Func_Call;
9629 -- This scenario requires secondary scope management and the index
9630 -- constraint cannot depend on the temporary used to capture the
9631 -- result of the function call.
9633 -- SS_Mark;
9634 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9635 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9636 -- Obj : S := Temp.all;
9637 -- SS_Release; -- Temp is gone at this point, bounds of S are
9638 -- -- non existent.
9640 -- Generate:
9641 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9643 Low_Bound := Make_Temporary (Loc, 'B');
9644 Insert_Action (E,
9645 Make_Object_Declaration (Loc,
9646 Defining_Identifier => Low_Bound,
9647 Object_Definition =>
9648 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9649 Constant_Present => True,
9650 Expression =>
9651 Make_Attribute_Reference (Loc,
9652 Prefix => Duplicate_Subexpr_No_Checks (E),
9653 Attribute_Name => Name_First,
9654 Expressions => New_List (
9655 Make_Integer_Literal (Loc, J)))));
9657 -- Generate:
9658 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9660 High_Bound := Make_Temporary (Loc, 'B');
9661 Insert_Action (E,
9662 Make_Object_Declaration (Loc,
9663 Defining_Identifier => High_Bound,
9664 Object_Definition =>
9665 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9666 Constant_Present => True,
9667 Expression =>
9668 Make_Attribute_Reference (Loc,
9669 Prefix => Duplicate_Subexpr_No_Checks (E),
9670 Attribute_Name => Name_Last,
9671 Expressions => New_List (
9672 Make_Integer_Literal (Loc, J)))));
9674 Append_To (List_Constr,
9675 Make_Range (Loc,
9676 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9677 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9679 Index_Typ := Next_Index (Index_Typ);
9680 end loop;
9682 elsif Is_Class_Wide_Type (Unc_Typ) then
9683 declare
9684 CW_Subtype : Entity_Id;
9685 EQ_Typ : Entity_Id := Empty;
9687 begin
9688 -- A class-wide equivalent type is not needed on VM targets
9689 -- because the VM back-ends handle the class-wide object
9690 -- initialization itself (and doesn't need or want the
9691 -- additional intermediate type to handle the assignment).
9693 if Expander_Active and then Tagged_Type_Expansion then
9695 -- If this is the class-wide type of a completion that is a
9696 -- record subtype, set the type of the class-wide type to be
9697 -- the full base type, for use in the expanded code for the
9698 -- equivalent type. Should this be done earlier when the
9699 -- completion is analyzed ???
9701 if Is_Private_Type (Etype (Unc_Typ))
9702 and then
9703 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9704 then
9705 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9706 end if;
9708 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9709 end if;
9711 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9712 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9713 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9715 return New_Occurrence_Of (CW_Subtype, Loc);
9716 end;
9718 -- Indefinite record type with discriminants
9720 else
9721 D := First_Discriminant (Unc_Typ);
9722 while Present (D) loop
9723 Append_To (List_Constr,
9724 Make_Selected_Component (Loc,
9725 Prefix => Duplicate_Subexpr_No_Checks (E),
9726 Selector_Name => New_Occurrence_Of (D, Loc)));
9728 Next_Discriminant (D);
9729 end loop;
9730 end if;
9732 return
9733 Make_Subtype_Indication (Loc,
9734 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9735 Constraint =>
9736 Make_Index_Or_Discriminant_Constraint (Loc,
9737 Constraints => List_Constr));
9738 end Make_Subtype_From_Expr;
9740 ---------------
9741 -- Map_Types --
9742 ---------------
9744 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9746 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9747 -- avoid deep indentation of code.
9749 -- NOTE: Routines which deal with discriminant mapping operate on the
9750 -- [underlying/record] full view of various types because those views
9751 -- contain all discriminants and stored constraints.
9753 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9754 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9755 -- overriding chain starting from Prim whose dispatching type is parent
9756 -- type Par_Typ and add a mapping between the result and primitive Prim.
9758 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9759 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9760 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9761 -- if no such primitive is available.
9763 function Build_Chain
9764 (Par_Typ : Entity_Id;
9765 Deriv_Typ : Entity_Id) return Elist_Id;
9766 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9767 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9768 -- list has the form:
9770 -- head tail
9771 -- v v
9772 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9774 -- Note that Par_Typ is not part of the resulting derivation chain
9776 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9777 -- Return the view of type Typ which could potentially contains either
9778 -- the discriminants or stored constraints of the type.
9780 function Find_Discriminant_Value
9781 (Discr : Entity_Id;
9782 Par_Typ : Entity_Id;
9783 Deriv_Typ : Entity_Id;
9784 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9785 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9786 -- in the derivation chain starting from parent type Par_Typ leading to
9787 -- derived type Deriv_Typ. The returned value is one of the following:
9789 -- * An entity which is either a discriminant or a non-discriminant
9790 -- name, and renames/constraints Discr.
9792 -- * An expression which constraints Discr
9794 -- Typ_Elmt is an element of the derivation chain created by routine
9795 -- Build_Chain and denotes the current ancestor being examined.
9797 procedure Map_Discriminants
9798 (Par_Typ : Entity_Id;
9799 Deriv_Typ : Entity_Id);
9800 -- Map each discriminant of type Par_Typ to a meaningful constraint
9801 -- from the point of view of type Deriv_Typ.
9803 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9804 -- Map each primitive of type Par_Typ to a corresponding primitive of
9805 -- type Deriv_Typ.
9807 -------------------
9808 -- Add_Primitive --
9809 -------------------
9811 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9812 Par_Prim : Entity_Id;
9814 begin
9815 -- Inspect the inheritance chain through the Alias attribute and the
9816 -- overriding chain through the Overridden_Operation looking for an
9817 -- ancestor primitive with the appropriate dispatching type.
9819 Par_Prim := Prim;
9820 while Present (Par_Prim) loop
9821 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9822 Par_Prim := Ancestor_Primitive (Par_Prim);
9823 end loop;
9825 -- Create a mapping of the form:
9827 -- parent type primitive -> derived type primitive
9829 if Present (Par_Prim) then
9830 Type_Map.Set (Par_Prim, Prim);
9831 end if;
9832 end Add_Primitive;
9834 ------------------------
9835 -- Ancestor_Primitive --
9836 ------------------------
9838 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9839 Inher_Prim : constant Entity_Id := Alias (Subp);
9840 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9842 begin
9843 -- The current subprogram overrides an ancestor primitive
9845 if Present (Over_Prim) then
9846 return Over_Prim;
9848 -- The current subprogram is an internally generated alias of an
9849 -- inherited ancestor primitive.
9851 elsif Present (Inher_Prim) then
9852 return Inher_Prim;
9854 -- Otherwise the current subprogram is the root of the inheritance or
9855 -- overriding chain.
9857 else
9858 return Empty;
9859 end if;
9860 end Ancestor_Primitive;
9862 -----------------
9863 -- Build_Chain --
9864 -----------------
9866 function Build_Chain
9867 (Par_Typ : Entity_Id;
9868 Deriv_Typ : Entity_Id) return Elist_Id
9870 Anc_Typ : Entity_Id;
9871 Chain : Elist_Id;
9872 Curr_Typ : Entity_Id;
9874 begin
9875 Chain := New_Elmt_List;
9877 -- Add the derived type to the derivation chain
9879 Prepend_Elmt (Deriv_Typ, Chain);
9881 -- Examine all ancestors starting from the derived type climbing
9882 -- towards parent type Par_Typ.
9884 Curr_Typ := Deriv_Typ;
9885 loop
9886 -- Handle the case where the current type is a record which
9887 -- derives from a subtype.
9889 -- subtype Sub_Typ is Par_Typ ...
9890 -- type Deriv_Typ is Sub_Typ ...
9892 if Ekind (Curr_Typ) = E_Record_Type
9893 and then Present (Parent_Subtype (Curr_Typ))
9894 then
9895 Anc_Typ := Parent_Subtype (Curr_Typ);
9897 -- Handle the case where the current type is a record subtype of
9898 -- another subtype.
9900 -- subtype Sub_Typ1 is Par_Typ ...
9901 -- subtype Sub_Typ2 is Sub_Typ1 ...
9903 elsif Ekind (Curr_Typ) = E_Record_Subtype
9904 and then Present (Cloned_Subtype (Curr_Typ))
9905 then
9906 Anc_Typ := Cloned_Subtype (Curr_Typ);
9908 -- Otherwise use the direct parent type
9910 else
9911 Anc_Typ := Etype (Curr_Typ);
9912 end if;
9914 -- Use the first subtype when dealing with itypes
9916 if Is_Itype (Anc_Typ) then
9917 Anc_Typ := First_Subtype (Anc_Typ);
9918 end if;
9920 -- Work with the view which contains the discriminants and stored
9921 -- constraints.
9923 Anc_Typ := Discriminated_View (Anc_Typ);
9925 -- Stop the climb when either the parent type has been reached or
9926 -- there are no more ancestors left to examine.
9928 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9930 Prepend_Unique_Elmt (Anc_Typ, Chain);
9931 Curr_Typ := Anc_Typ;
9932 end loop;
9934 return Chain;
9935 end Build_Chain;
9937 ------------------------
9938 -- Discriminated_View --
9939 ------------------------
9941 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9942 T : Entity_Id;
9944 begin
9945 T := Typ;
9947 -- Use the [underlying] full view when dealing with private types
9948 -- because the view contains all inherited discriminants or stored
9949 -- constraints.
9951 if Is_Private_Type (T) then
9952 if Present (Underlying_Full_View (T)) then
9953 T := Underlying_Full_View (T);
9955 elsif Present (Full_View (T)) then
9956 T := Full_View (T);
9957 end if;
9958 end if;
9960 -- Use the underlying record view when the type is an extenstion of
9961 -- a parent type with unknown discriminants because the view contains
9962 -- all inherited discriminants or stored constraints.
9964 if Ekind (T) = E_Record_Type
9965 and then Present (Underlying_Record_View (T))
9966 then
9967 T := Underlying_Record_View (T);
9968 end if;
9970 return T;
9971 end Discriminated_View;
9973 -----------------------------
9974 -- Find_Discriminant_Value --
9975 -----------------------------
9977 function Find_Discriminant_Value
9978 (Discr : Entity_Id;
9979 Par_Typ : Entity_Id;
9980 Deriv_Typ : Entity_Id;
9981 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9983 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9984 Typ : constant Entity_Id := Node (Typ_Elmt);
9986 function Find_Constraint_Value
9987 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
9988 -- Given constraint Constr, find what it denotes. This is either:
9990 -- * An entity which is either a discriminant or a name
9992 -- * An expression
9994 ---------------------------
9995 -- Find_Constraint_Value --
9996 ---------------------------
9998 function Find_Constraint_Value
9999 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
10001 begin
10002 if Nkind (Constr) in N_Entity then
10004 -- The constraint denotes a discriminant of the curren type
10005 -- which renames the ancestor discriminant:
10007 -- vv
10008 -- type Typ (D1 : ...; DN : ...) is
10009 -- new Anc (Discr => D1) with ...
10010 -- ^^
10012 if Ekind (Constr) = E_Discriminant then
10014 -- The discriminant belongs to derived type Deriv_Typ. This
10015 -- is the final value for the ancestor discriminant as the
10016 -- derivations chain has been fully exhausted.
10018 if Typ = Deriv_Typ then
10019 return Constr;
10021 -- Otherwise the discriminant may be renamed or constrained
10022 -- at a lower level. Continue looking down the derivation
10023 -- chain.
10025 else
10026 return
10027 Find_Discriminant_Value
10028 (Discr => Constr,
10029 Par_Typ => Par_Typ,
10030 Deriv_Typ => Deriv_Typ,
10031 Typ_Elmt => Next_Elmt (Typ_Elmt));
10032 end if;
10034 -- Otherwise the constraint denotes a reference to some name
10035 -- which results in a Girder discriminant:
10037 -- vvvv
10038 -- Name : ...;
10039 -- type Typ (D1 : ...; DN : ...) is
10040 -- new Anc (Discr => Name) with ...
10041 -- ^^^^
10043 -- Return the name as this is the proper constraint of the
10044 -- discriminant.
10046 else
10047 return Constr;
10048 end if;
10050 -- The constraint denotes a reference to a name
10052 elsif Is_Entity_Name (Constr) then
10053 return Find_Constraint_Value (Entity (Constr));
10055 -- Otherwise the current constraint is an expression which yields
10056 -- a Girder discriminant:
10058 -- type Typ (D1 : ...; DN : ...) is
10059 -- new Anc (Discr => <expression>) with ...
10060 -- ^^^^^^^^^^
10062 -- Return the expression as this is the proper constraint of the
10063 -- discriminant.
10065 else
10066 return Constr;
10067 end if;
10068 end Find_Constraint_Value;
10070 -- Local variables
10072 Constrs : constant Elist_Id := Stored_Constraint (Typ);
10074 Constr_Elmt : Elmt_Id;
10075 Pos : Uint;
10076 Typ_Discr : Entity_Id;
10078 -- Start of processing for Find_Discriminant_Value
10080 begin
10081 -- The algorithm for finding the value of a discriminant works as
10082 -- follows. First, it recreates the derivation chain from Par_Typ
10083 -- to Deriv_Typ as a list:
10085 -- Par_Typ (shown for completeness)
10086 -- v
10087 -- Ancestor_N <-- head of chain
10088 -- v
10089 -- Ancestor_1
10090 -- v
10091 -- Deriv_Typ <-- tail of chain
10093 -- The algorithm then traces the fate of a parent discriminant down
10094 -- the derivation chain. At each derivation level, the discriminant
10095 -- may be either inherited or constrained.
10097 -- 1) Discriminant is inherited: there are two cases, depending on
10098 -- which type is inheriting.
10100 -- 1.1) Deriv_Typ is inheriting:
10102 -- type Ancestor (D_1 : ...) is tagged ...
10103 -- type Deriv_Typ is new Ancestor ...
10105 -- In this case the inherited discriminant is the final value of
10106 -- the parent discriminant because the end of the derivation chain
10107 -- has been reached.
10109 -- 1.2) Some other type is inheriting:
10111 -- type Ancestor_1 (D_1 : ...) is tagged ...
10112 -- type Ancestor_2 is new Ancestor_1 ...
10114 -- In this case the algorithm continues to trace the fate of the
10115 -- inherited discriminant down the derivation chain because it may
10116 -- be further inherited or constrained.
10118 -- 2) Discriminant is constrained: there are three cases, depending
10119 -- on what the constraint is.
10121 -- 2.1) The constraint is another discriminant (aka renaming):
10123 -- type Ancestor_1 (D_1 : ...) is tagged ...
10124 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10126 -- In this case the constraining discriminant becomes the one to
10127 -- track down the derivation chain. The algorithm already knows
10128 -- that D_2 constrains D_1, therefore if the algorithm finds the
10129 -- value of D_2, then this would also be the value for D_1.
10131 -- 2.2) The constraint is a name (aka Girder):
10133 -- Name : ...
10134 -- type Ancestor_1 (D_1 : ...) is tagged ...
10135 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10137 -- In this case the name is the final value of D_1 because the
10138 -- discriminant cannot be further constrained.
10140 -- 2.3) The constraint is an expression (aka Girder):
10142 -- type Ancestor_1 (D_1 : ...) is tagged ...
10143 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10145 -- Similar to 2.2, the expression is the final value of D_1
10147 Pos := Uint_1;
10149 -- When a derived type constrains its parent type, all constaints
10150 -- appear in the Stored_Constraint list. Examine the list looking
10151 -- for a positional match.
10153 if Present (Constrs) then
10154 Constr_Elmt := First_Elmt (Constrs);
10155 while Present (Constr_Elmt) loop
10157 -- The position of the current constraint matches that of the
10158 -- ancestor discriminant.
10160 if Pos = Discr_Pos then
10161 return Find_Constraint_Value (Node (Constr_Elmt));
10162 end if;
10164 Next_Elmt (Constr_Elmt);
10165 Pos := Pos + 1;
10166 end loop;
10168 -- Otherwise the derived type does not constraint its parent type in
10169 -- which case it inherits the parent discriminants.
10171 else
10172 Typ_Discr := First_Discriminant (Typ);
10173 while Present (Typ_Discr) loop
10175 -- The position of the current discriminant matches that of the
10176 -- ancestor discriminant.
10178 if Pos = Discr_Pos then
10179 return Find_Constraint_Value (Typ_Discr);
10180 end if;
10182 Next_Discriminant (Typ_Discr);
10183 Pos := Pos + 1;
10184 end loop;
10185 end if;
10187 -- A discriminant must always have a corresponding value. This is
10188 -- either another discriminant, a name, or an expression. If this
10189 -- point is reached, them most likely the derivation chain employs
10190 -- the wrong views of types.
10192 pragma Assert (False);
10194 return Empty;
10195 end Find_Discriminant_Value;
10197 -----------------------
10198 -- Map_Discriminants --
10199 -----------------------
10201 procedure Map_Discriminants
10202 (Par_Typ : Entity_Id;
10203 Deriv_Typ : Entity_Id)
10205 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10207 Discr : Entity_Id;
10208 Discr_Val : Node_Or_Entity_Id;
10210 begin
10211 -- Examine each discriminant of parent type Par_Typ and find a
10212 -- suitable value for it from the point of view of derived type
10213 -- Deriv_Typ.
10215 if Has_Discriminants (Par_Typ) then
10216 Discr := First_Discriminant (Par_Typ);
10217 while Present (Discr) loop
10218 Discr_Val :=
10219 Find_Discriminant_Value
10220 (Discr => Discr,
10221 Par_Typ => Par_Typ,
10222 Deriv_Typ => Deriv_Typ,
10223 Typ_Elmt => First_Elmt (Deriv_Chain));
10225 -- Create a mapping of the form:
10227 -- parent type discriminant -> value
10229 Type_Map.Set (Discr, Discr_Val);
10231 Next_Discriminant (Discr);
10232 end loop;
10233 end if;
10234 end Map_Discriminants;
10236 --------------------
10237 -- Map_Primitives --
10238 --------------------
10240 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10241 Deriv_Prim : Entity_Id;
10242 Par_Prim : Entity_Id;
10243 Par_Prims : Elist_Id;
10244 Prim_Elmt : Elmt_Id;
10246 begin
10247 -- Inspect the primitives of the derived type and determine whether
10248 -- they relate to the primitives of the parent type. If there is a
10249 -- meaningful relation, create a mapping of the form:
10251 -- parent type primitive -> perived type primitive
10253 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10254 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10255 while Present (Prim_Elmt) loop
10256 Deriv_Prim := Node (Prim_Elmt);
10258 if Is_Subprogram (Deriv_Prim)
10259 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10260 then
10261 Add_Primitive (Deriv_Prim, Par_Typ);
10262 end if;
10264 Next_Elmt (Prim_Elmt);
10265 end loop;
10266 end if;
10268 -- If the parent operation is an interface operation, the overriding
10269 -- indicator is not present. Instead, we get from the interface
10270 -- operation the primitive of the current type that implements it.
10272 if Is_Interface (Par_Typ) then
10273 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10275 if Present (Par_Prims) then
10276 Prim_Elmt := First_Elmt (Par_Prims);
10278 while Present (Prim_Elmt) loop
10279 Par_Prim := Node (Prim_Elmt);
10280 Deriv_Prim :=
10281 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10283 if Present (Deriv_Prim) then
10284 Type_Map.Set (Par_Prim, Deriv_Prim);
10285 end if;
10287 Next_Elmt (Prim_Elmt);
10288 end loop;
10289 end if;
10290 end if;
10291 end Map_Primitives;
10293 -- Start of processing for Map_Types
10295 begin
10296 -- Nothing to do if there are no types to work with
10298 if No (Parent_Type) or else No (Derived_Type) then
10299 return;
10301 -- Nothing to do if the mapping already exists
10303 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10304 return;
10306 -- Nothing to do if both types are not tagged. Note that untagged types
10307 -- do not have primitive operations and their discriminants are already
10308 -- handled by gigi.
10310 elsif not Is_Tagged_Type (Parent_Type)
10311 or else not Is_Tagged_Type (Derived_Type)
10312 then
10313 return;
10314 end if;
10316 -- Create a mapping of the form
10318 -- parent type -> derived type
10320 -- to prevent any subsequent attempts to produce the same relations
10322 Type_Map.Set (Parent_Type, Derived_Type);
10324 -- Create mappings of the form
10326 -- parent type discriminant -> derived type discriminant
10327 -- <or>
10328 -- parent type discriminant -> constraint
10330 -- Note that mapping of discriminants breaks privacy because it needs to
10331 -- work with those views which contains the discriminants and any stored
10332 -- constraints.
10334 Map_Discriminants
10335 (Par_Typ => Discriminated_View (Parent_Type),
10336 Deriv_Typ => Discriminated_View (Derived_Type));
10338 -- Create mappings of the form
10340 -- parent type primitive -> derived type primitive
10342 Map_Primitives
10343 (Par_Typ => Parent_Type,
10344 Deriv_Typ => Derived_Type);
10345 end Map_Types;
10347 ----------------------------
10348 -- Matching_Standard_Type --
10349 ----------------------------
10351 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10352 pragma Assert (Is_Scalar_Type (Typ));
10353 Siz : constant Uint := Esize (Typ);
10355 begin
10356 -- Floating-point cases
10358 if Is_Floating_Point_Type (Typ) then
10359 if Siz <= Esize (Standard_Short_Float) then
10360 return Standard_Short_Float;
10361 elsif Siz <= Esize (Standard_Float) then
10362 return Standard_Float;
10363 elsif Siz <= Esize (Standard_Long_Float) then
10364 return Standard_Long_Float;
10365 elsif Siz <= Esize (Standard_Long_Long_Float) then
10366 return Standard_Long_Long_Float;
10367 else
10368 raise Program_Error;
10369 end if;
10371 -- Integer cases (includes fixed-point types)
10373 -- Unsigned integer cases (includes normal enumeration types)
10375 elsif Is_Unsigned_Type (Typ) then
10376 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10377 return Standard_Short_Short_Unsigned;
10378 elsif Siz <= Esize (Standard_Short_Unsigned) then
10379 return Standard_Short_Unsigned;
10380 elsif Siz <= Esize (Standard_Unsigned) then
10381 return Standard_Unsigned;
10382 elsif Siz <= Esize (Standard_Long_Unsigned) then
10383 return Standard_Long_Unsigned;
10384 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10385 return Standard_Long_Long_Unsigned;
10386 else
10387 raise Program_Error;
10388 end if;
10390 -- Signed integer cases
10392 else
10393 if Siz <= Esize (Standard_Short_Short_Integer) then
10394 return Standard_Short_Short_Integer;
10395 elsif Siz <= Esize (Standard_Short_Integer) then
10396 return Standard_Short_Integer;
10397 elsif Siz <= Esize (Standard_Integer) then
10398 return Standard_Integer;
10399 elsif Siz <= Esize (Standard_Long_Integer) then
10400 return Standard_Long_Integer;
10401 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10402 return Standard_Long_Long_Integer;
10403 else
10404 raise Program_Error;
10405 end if;
10406 end if;
10407 end Matching_Standard_Type;
10409 -----------------------------
10410 -- May_Generate_Large_Temp --
10411 -----------------------------
10413 -- At the current time, the only types that we return False for (i.e. where
10414 -- we decide we know they cannot generate large temps) are ones where we
10415 -- know the size is 256 bits or less at compile time, and we are still not
10416 -- doing a thorough job on arrays and records ???
10418 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10419 begin
10420 if not Size_Known_At_Compile_Time (Typ) then
10421 return False;
10423 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10424 return False;
10426 elsif Is_Array_Type (Typ)
10427 and then Present (Packed_Array_Impl_Type (Typ))
10428 then
10429 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10431 -- We could do more here to find other small types ???
10433 else
10434 return True;
10435 end if;
10436 end May_Generate_Large_Temp;
10438 --------------------------------------------
10439 -- Needs_Conditional_Null_Excluding_Check --
10440 --------------------------------------------
10442 function Needs_Conditional_Null_Excluding_Check
10443 (Typ : Entity_Id) return Boolean
10445 begin
10446 return
10447 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
10448 end Needs_Conditional_Null_Excluding_Check;
10450 ----------------------------
10451 -- Needs_Constant_Address --
10452 ----------------------------
10454 function Needs_Constant_Address
10455 (Decl : Node_Id;
10456 Typ : Entity_Id) return Boolean
10458 begin
10459 -- If we have no initialization of any kind, then we don't need to place
10460 -- any restrictions on the address clause, because the object will be
10461 -- elaborated after the address clause is evaluated. This happens if the
10462 -- declaration has no initial expression, or the type has no implicit
10463 -- initialization, or the object is imported.
10465 -- The same holds for all initialized scalar types and all access types.
10466 -- Packed bit arrays of size up to 64 are represented using a modular
10467 -- type with an initialization (to zero) and can be processed like other
10468 -- initialized scalar types.
10470 -- If the type is controlled, code to attach the object to a
10471 -- finalization chain is generated at the point of declaration, and
10472 -- therefore the elaboration of the object cannot be delayed: the
10473 -- address expression must be a constant.
10475 if No (Expression (Decl))
10476 and then not Needs_Finalization (Typ)
10477 and then
10478 (not Has_Non_Null_Base_Init_Proc (Typ)
10479 or else Is_Imported (Defining_Identifier (Decl)))
10480 then
10481 return False;
10483 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10484 or else Is_Access_Type (Typ)
10485 or else
10486 (Is_Bit_Packed_Array (Typ)
10487 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10488 then
10489 return False;
10491 else
10492 -- Otherwise, we require the address clause to be constant because
10493 -- the call to the initialization procedure (or the attach code) has
10494 -- to happen at the point of the declaration.
10496 -- Actually the IP call has been moved to the freeze actions anyway,
10497 -- so maybe we can relax this restriction???
10499 return True;
10500 end if;
10501 end Needs_Constant_Address;
10503 ------------------------
10504 -- Needs_Finalization --
10505 ------------------------
10507 function Needs_Finalization (Typ : Entity_Id) return Boolean is
10508 function Has_Some_Controlled_Component
10509 (Input_Typ : Entity_Id) return Boolean;
10510 -- Determine whether type Input_Typ has at least one controlled
10511 -- component.
10513 -----------------------------------
10514 -- Has_Some_Controlled_Component --
10515 -----------------------------------
10517 function Has_Some_Controlled_Component
10518 (Input_Typ : Entity_Id) return Boolean
10520 Comp : Entity_Id;
10522 begin
10523 -- When a type is already frozen and has at least one controlled
10524 -- component, or is manually decorated, it is sufficient to inspect
10525 -- flag Has_Controlled_Component.
10527 if Has_Controlled_Component (Input_Typ) then
10528 return True;
10530 -- Otherwise inspect the internals of the type
10532 elsif not Is_Frozen (Input_Typ) then
10533 if Is_Array_Type (Input_Typ) then
10534 return Needs_Finalization (Component_Type (Input_Typ));
10536 elsif Is_Record_Type (Input_Typ) then
10537 Comp := First_Component (Input_Typ);
10538 while Present (Comp) loop
10539 if Needs_Finalization (Etype (Comp)) then
10540 return True;
10541 end if;
10543 Next_Component (Comp);
10544 end loop;
10545 end if;
10546 end if;
10548 return False;
10549 end Has_Some_Controlled_Component;
10551 -- Start of processing for Needs_Finalization
10553 begin
10554 -- Certain run-time configurations and targets do not provide support
10555 -- for controlled types.
10557 if Restriction_Active (No_Finalization) then
10558 return False;
10560 -- C++ types are not considered controlled. It is assumed that the non-
10561 -- Ada side will handle their clean up.
10563 elsif Convention (Typ) = Convention_CPP then
10564 return False;
10566 -- Class-wide types are treated as controlled because derivations from
10567 -- the root type may introduce controlled components.
10569 elsif Is_Class_Wide_Type (Typ) then
10570 return True;
10572 -- Concurrent types are controlled as long as their corresponding record
10573 -- is controlled.
10575 elsif Is_Concurrent_Type (Typ)
10576 and then Present (Corresponding_Record_Type (Typ))
10577 and then Needs_Finalization (Corresponding_Record_Type (Typ))
10578 then
10579 return True;
10581 -- Otherwise the type is controlled when it is either derived from type
10582 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10583 -- contains at least one controlled component.
10585 else
10586 return
10587 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
10588 end if;
10589 end Needs_Finalization;
10591 ----------------------------
10592 -- New_Class_Wide_Subtype --
10593 ----------------------------
10595 function New_Class_Wide_Subtype
10596 (CW_Typ : Entity_Id;
10597 N : Node_Id) return Entity_Id
10599 Res : constant Entity_Id := Create_Itype (E_Void, N);
10601 -- Capture relevant attributes of the class-wide subtype which must be
10602 -- restored after the copy.
10604 Res_Chars : constant Name_Id := Chars (Res);
10605 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
10606 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
10607 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
10608 Res_Scope : constant Entity_Id := Scope (Res);
10610 begin
10611 Copy_Node (CW_Typ, Res);
10613 -- Restore the relevant attributes of the class-wide subtype
10615 Set_Chars (Res, Res_Chars);
10616 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
10617 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
10618 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
10619 Set_Scope (Res, Res_Scope);
10621 -- Decorate the class-wide subtype
10623 Set_Associated_Node_For_Itype (Res, N);
10624 Set_Comes_From_Source (Res, False);
10625 Set_Ekind (Res, E_Class_Wide_Subtype);
10626 Set_Etype (Res, Base_Type (CW_Typ));
10627 Set_Freeze_Node (Res, Empty);
10628 Set_Is_Frozen (Res, False);
10629 Set_Is_Itype (Res);
10630 Set_Is_Public (Res, False);
10631 Set_Next_Entity (Res, Empty);
10632 Set_Prev_Entity (Res, Empty);
10633 Set_Sloc (Res, Sloc (N));
10635 Set_Public_Status (Res);
10637 return Res;
10638 end New_Class_Wide_Subtype;
10640 --------------------------------
10641 -- Non_Limited_Designated_Type --
10642 ---------------------------------
10644 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10645 Desig : constant Entity_Id := Designated_Type (T);
10646 begin
10647 if Has_Non_Limited_View (Desig) then
10648 return Non_Limited_View (Desig);
10649 else
10650 return Desig;
10651 end if;
10652 end Non_Limited_Designated_Type;
10654 -----------------------------------
10655 -- OK_To_Do_Constant_Replacement --
10656 -----------------------------------
10658 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10659 ES : constant Entity_Id := Scope (E);
10660 CS : Entity_Id;
10662 begin
10663 -- Do not replace statically allocated objects, because they may be
10664 -- modified outside the current scope.
10666 if Is_Statically_Allocated (E) then
10667 return False;
10669 -- Do not replace aliased or volatile objects, since we don't know what
10670 -- else might change the value.
10672 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10673 return False;
10675 -- Debug flag -gnatdM disconnects this optimization
10677 elsif Debug_Flag_MM then
10678 return False;
10680 -- Otherwise check scopes
10682 else
10683 CS := Current_Scope;
10685 loop
10686 -- If we are in right scope, replacement is safe
10688 if CS = ES then
10689 return True;
10691 -- Packages do not affect the determination of safety
10693 elsif Ekind (CS) = E_Package then
10694 exit when CS = Standard_Standard;
10695 CS := Scope (CS);
10697 -- Blocks do not affect the determination of safety
10699 elsif Ekind (CS) = E_Block then
10700 CS := Scope (CS);
10702 -- Loops do not affect the determination of safety. Note that we
10703 -- kill all current values on entry to a loop, so we are just
10704 -- talking about processing within a loop here.
10706 elsif Ekind (CS) = E_Loop then
10707 CS := Scope (CS);
10709 -- Otherwise, the reference is dubious, and we cannot be sure that
10710 -- it is safe to do the replacement.
10712 else
10713 exit;
10714 end if;
10715 end loop;
10717 return False;
10718 end if;
10719 end OK_To_Do_Constant_Replacement;
10721 ------------------------------------
10722 -- Possible_Bit_Aligned_Component --
10723 ------------------------------------
10725 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10726 begin
10727 -- Do not process an unanalyzed node because it is not yet decorated and
10728 -- most checks performed below will fail.
10730 if not Analyzed (N) then
10731 return False;
10732 end if;
10734 -- There are never alignment issues in CodePeer mode
10736 if CodePeer_Mode then
10737 return False;
10738 end if;
10740 case Nkind (N) is
10742 -- Case of indexed component
10744 when N_Indexed_Component =>
10745 declare
10746 P : constant Node_Id := Prefix (N);
10747 Ptyp : constant Entity_Id := Etype (P);
10749 begin
10750 -- If we know the component size and it is less than 64, then
10751 -- we are definitely OK. The back end always does assignment of
10752 -- misaligned small objects correctly.
10754 if Known_Static_Component_Size (Ptyp)
10755 and then Component_Size (Ptyp) <= 64
10756 then
10757 return False;
10759 -- Otherwise, we need to test the prefix, to see if we are
10760 -- indexing from a possibly unaligned component.
10762 else
10763 return Possible_Bit_Aligned_Component (P);
10764 end if;
10765 end;
10767 -- Case of selected component
10769 when N_Selected_Component =>
10770 declare
10771 P : constant Node_Id := Prefix (N);
10772 Comp : constant Entity_Id := Entity (Selector_Name (N));
10774 begin
10775 -- If there is no component clause, then we are in the clear
10776 -- since the back end will never misalign a large component
10777 -- unless it is forced to do so. In the clear means we need
10778 -- only the recursive test on the prefix.
10780 if Component_May_Be_Bit_Aligned (Comp) then
10781 return True;
10782 else
10783 return Possible_Bit_Aligned_Component (P);
10784 end if;
10785 end;
10787 -- For a slice, test the prefix, if that is possibly misaligned,
10788 -- then for sure the slice is.
10790 when N_Slice =>
10791 return Possible_Bit_Aligned_Component (Prefix (N));
10793 -- For an unchecked conversion, check whether the expression may
10794 -- be bit-aligned.
10796 when N_Unchecked_Type_Conversion =>
10797 return Possible_Bit_Aligned_Component (Expression (N));
10799 -- If we have none of the above, it means that we have fallen off the
10800 -- top testing prefixes recursively, and we now have a stand alone
10801 -- object, where we don't have a problem, unless this is a renaming,
10802 -- in which case we need to look into the renamed object.
10804 when others =>
10805 if Is_Entity_Name (N)
10806 and then Present (Renamed_Object (Entity (N)))
10807 then
10808 return
10809 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10810 else
10811 return False;
10812 end if;
10813 end case;
10814 end Possible_Bit_Aligned_Component;
10816 -----------------------------------------------
10817 -- Process_Statements_For_Controlled_Objects --
10818 -----------------------------------------------
10820 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10821 Loc : constant Source_Ptr := Sloc (N);
10823 function Are_Wrapped (L : List_Id) return Boolean;
10824 -- Determine whether list L contains only one statement which is a block
10826 function Wrap_Statements_In_Block
10827 (L : List_Id;
10828 Scop : Entity_Id := Current_Scope) return Node_Id;
10829 -- Given a list of statements L, wrap it in a block statement and return
10830 -- the generated node. Scop is either the current scope or the scope of
10831 -- the context (if applicable).
10833 -----------------
10834 -- Are_Wrapped --
10835 -----------------
10837 function Are_Wrapped (L : List_Id) return Boolean is
10838 Stmt : constant Node_Id := First (L);
10839 begin
10840 return
10841 Present (Stmt)
10842 and then No (Next (Stmt))
10843 and then Nkind (Stmt) = N_Block_Statement;
10844 end Are_Wrapped;
10846 ------------------------------
10847 -- Wrap_Statements_In_Block --
10848 ------------------------------
10850 function Wrap_Statements_In_Block
10851 (L : List_Id;
10852 Scop : Entity_Id := Current_Scope) return Node_Id
10854 Block_Id : Entity_Id;
10855 Block_Nod : Node_Id;
10856 Iter_Loop : Entity_Id;
10858 begin
10859 Block_Nod :=
10860 Make_Block_Statement (Loc,
10861 Declarations => No_List,
10862 Handled_Statement_Sequence =>
10863 Make_Handled_Sequence_Of_Statements (Loc,
10864 Statements => L));
10866 -- Create a label for the block in case the block needs to manage the
10867 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10869 Add_Block_Identifier (Block_Nod, Block_Id);
10871 -- When wrapping the statements of an iterator loop, check whether
10872 -- the loop requires secondary stack management and if so, propagate
10873 -- the appropriate flags to the block. This ensures that the cursor
10874 -- is properly cleaned up at each iteration of the loop.
10876 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10878 if Present (Iter_Loop) then
10879 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10881 -- Secondary stack reclamation is suppressed when the associated
10882 -- iterator loop contains a return statement which uses the stack.
10884 Set_Sec_Stack_Needed_For_Return
10885 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10886 end if;
10888 return Block_Nod;
10889 end Wrap_Statements_In_Block;
10891 -- Local variables
10893 Block : Node_Id;
10895 -- Start of processing for Process_Statements_For_Controlled_Objects
10897 begin
10898 -- Whenever a non-handled statement list is wrapped in a block, the
10899 -- block must be explicitly analyzed to redecorate all entities in the
10900 -- list and ensure that a finalizer is properly built.
10902 case Nkind (N) is
10903 when N_Conditional_Entry_Call
10904 | N_Elsif_Part
10905 | N_If_Statement
10906 | N_Selective_Accept
10908 -- Check the "then statements" for elsif parts and if statements
10910 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10911 and then not Is_Empty_List (Then_Statements (N))
10912 and then not Are_Wrapped (Then_Statements (N))
10913 and then Requires_Cleanup_Actions
10914 (L => Then_Statements (N),
10915 Lib_Level => False,
10916 Nested_Constructs => False)
10917 then
10918 Block := Wrap_Statements_In_Block (Then_Statements (N));
10919 Set_Then_Statements (N, New_List (Block));
10921 Analyze (Block);
10922 end if;
10924 -- Check the "else statements" for conditional entry calls, if
10925 -- statements and selective accepts.
10927 if Nkind_In (N, N_Conditional_Entry_Call,
10928 N_If_Statement,
10929 N_Selective_Accept)
10930 and then not Is_Empty_List (Else_Statements (N))
10931 and then not Are_Wrapped (Else_Statements (N))
10932 and then Requires_Cleanup_Actions
10933 (L => Else_Statements (N),
10934 Lib_Level => False,
10935 Nested_Constructs => False)
10936 then
10937 Block := Wrap_Statements_In_Block (Else_Statements (N));
10938 Set_Else_Statements (N, New_List (Block));
10940 Analyze (Block);
10941 end if;
10943 when N_Abortable_Part
10944 | N_Accept_Alternative
10945 | N_Case_Statement_Alternative
10946 | N_Delay_Alternative
10947 | N_Entry_Call_Alternative
10948 | N_Exception_Handler
10949 | N_Loop_Statement
10950 | N_Triggering_Alternative
10952 if not Is_Empty_List (Statements (N))
10953 and then not Are_Wrapped (Statements (N))
10954 and then Requires_Cleanup_Actions
10955 (L => Statements (N),
10956 Lib_Level => False,
10957 Nested_Constructs => False)
10958 then
10959 if Nkind (N) = N_Loop_Statement
10960 and then Present (Identifier (N))
10961 then
10962 Block :=
10963 Wrap_Statements_In_Block
10964 (L => Statements (N),
10965 Scop => Entity (Identifier (N)));
10966 else
10967 Block := Wrap_Statements_In_Block (Statements (N));
10968 end if;
10970 Set_Statements (N, New_List (Block));
10971 Analyze (Block);
10972 end if;
10974 -- Could be e.g. a loop that was transformed into a block or null
10975 -- statement. Do nothing for terminate alternatives.
10977 when N_Block_Statement
10978 | N_Null_Statement
10979 | N_Terminate_Alternative
10981 null;
10983 when others =>
10984 raise Program_Error;
10985 end case;
10986 end Process_Statements_For_Controlled_Objects;
10988 ------------------
10989 -- Power_Of_Two --
10990 ------------------
10992 function Power_Of_Two (N : Node_Id) return Nat is
10993 Typ : constant Entity_Id := Etype (N);
10994 pragma Assert (Is_Integer_Type (Typ));
10996 Siz : constant Nat := UI_To_Int (Esize (Typ));
10997 Val : Uint;
10999 begin
11000 if not Compile_Time_Known_Value (N) then
11001 return 0;
11003 else
11004 Val := Expr_Value (N);
11005 for J in 1 .. Siz - 1 loop
11006 if Val = Uint_2 ** J then
11007 return J;
11008 end if;
11009 end loop;
11011 return 0;
11012 end if;
11013 end Power_Of_Two;
11015 ----------------------
11016 -- Remove_Init_Call --
11017 ----------------------
11019 function Remove_Init_Call
11020 (Var : Entity_Id;
11021 Rep_Clause : Node_Id) return Node_Id
11023 Par : constant Node_Id := Parent (Var);
11024 Typ : constant Entity_Id := Etype (Var);
11026 Init_Proc : Entity_Id;
11027 -- Initialization procedure for Typ
11029 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
11030 -- Look for init call for Var starting at From and scanning the
11031 -- enclosing list until Rep_Clause or the end of the list is reached.
11033 ----------------------------
11034 -- Find_Init_Call_In_List --
11035 ----------------------------
11037 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
11038 Init_Call : Node_Id;
11040 begin
11041 Init_Call := From;
11042 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
11043 if Nkind (Init_Call) = N_Procedure_Call_Statement
11044 and then Is_Entity_Name (Name (Init_Call))
11045 and then Entity (Name (Init_Call)) = Init_Proc
11046 then
11047 return Init_Call;
11048 end if;
11050 Next (Init_Call);
11051 end loop;
11053 return Empty;
11054 end Find_Init_Call_In_List;
11056 Init_Call : Node_Id;
11058 -- Start of processing for Find_Init_Call
11060 begin
11061 if Present (Initialization_Statements (Var)) then
11062 Init_Call := Initialization_Statements (Var);
11063 Set_Initialization_Statements (Var, Empty);
11065 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
11067 -- No init proc for the type, so obviously no call to be found
11069 return Empty;
11071 else
11072 -- We might be able to handle other cases below by just properly
11073 -- setting Initialization_Statements at the point where the init proc
11074 -- call is generated???
11076 Init_Proc := Base_Init_Proc (Typ);
11078 -- First scan the list containing the declaration of Var
11080 Init_Call := Find_Init_Call_In_List (From => Next (Par));
11082 -- If not found, also look on Var's freeze actions list, if any,
11083 -- since the init call may have been moved there (case of an address
11084 -- clause applying to Var).
11086 if No (Init_Call) and then Present (Freeze_Node (Var)) then
11087 Init_Call :=
11088 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
11089 end if;
11091 -- If the initialization call has actuals that use the secondary
11092 -- stack, the call may have been wrapped into a temporary block, in
11093 -- which case the block itself has to be removed.
11095 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
11096 declare
11097 Blk : constant Node_Id := Next (Par);
11098 begin
11099 if Present
11100 (Find_Init_Call_In_List
11101 (First (Statements (Handled_Statement_Sequence (Blk)))))
11102 then
11103 Init_Call := Blk;
11104 end if;
11105 end;
11106 end if;
11107 end if;
11109 if Present (Init_Call) then
11110 Remove (Init_Call);
11111 end if;
11112 return Init_Call;
11113 end Remove_Init_Call;
11115 -------------------------
11116 -- Remove_Side_Effects --
11117 -------------------------
11119 procedure Remove_Side_Effects
11120 (Exp : Node_Id;
11121 Name_Req : Boolean := False;
11122 Renaming_Req : Boolean := False;
11123 Variable_Ref : Boolean := False;
11124 Related_Id : Entity_Id := Empty;
11125 Is_Low_Bound : Boolean := False;
11126 Is_High_Bound : Boolean := False;
11127 Check_Side_Effects : Boolean := True)
11129 function Build_Temporary
11130 (Loc : Source_Ptr;
11131 Id : Character;
11132 Related_Nod : Node_Id := Empty) return Entity_Id;
11133 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11134 -- is present (xxx is taken from the Chars field of Related_Nod),
11135 -- otherwise it generates an internal temporary. The created temporary
11136 -- entity is marked as internal.
11138 ---------------------
11139 -- Build_Temporary --
11140 ---------------------
11142 function Build_Temporary
11143 (Loc : Source_Ptr;
11144 Id : Character;
11145 Related_Nod : Node_Id := Empty) return Entity_Id
11147 Temp_Id : Entity_Id;
11148 Temp_Nam : Name_Id;
11150 begin
11151 -- The context requires an external symbol
11153 if Present (Related_Id) then
11154 if Is_Low_Bound then
11155 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
11156 else pragma Assert (Is_High_Bound);
11157 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
11158 end if;
11160 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
11162 -- Otherwise generate an internal temporary
11164 else
11165 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
11166 end if;
11168 Set_Is_Internal (Temp_Id);
11170 return Temp_Id;
11171 end Build_Temporary;
11173 -- Local variables
11175 Loc : constant Source_Ptr := Sloc (Exp);
11176 Exp_Type : constant Entity_Id := Etype (Exp);
11177 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
11178 Def_Id : Entity_Id;
11179 E : Node_Id;
11180 New_Exp : Node_Id;
11181 Ptr_Typ_Decl : Node_Id;
11182 Ref_Type : Entity_Id;
11183 Res : Node_Id;
11185 -- Start of processing for Remove_Side_Effects
11187 begin
11188 -- Handle cases in which there is nothing to do. In GNATprove mode,
11189 -- removal of side effects is useful for the light expansion of
11190 -- renamings. This removal should only occur when not inside a
11191 -- generic and not doing a preanalysis.
11193 if not Expander_Active
11194 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
11195 then
11196 return;
11198 -- Cannot generate temporaries if the invocation to remove side effects
11199 -- was issued too early and the type of the expression is not resolved
11200 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11201 -- Remove_Side_Effects).
11203 elsif No (Exp_Type)
11204 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11205 then
11206 return;
11208 -- Nothing to do if prior expansion determined that a function call does
11209 -- not require side effect removal.
11211 elsif Nkind (Exp) = N_Function_Call
11212 and then No_Side_Effect_Removal (Exp)
11213 then
11214 return;
11216 -- No action needed for side-effect free expressions
11218 elsif Check_Side_Effects
11219 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11220 then
11221 return;
11223 -- Generating C code we cannot remove side effect of function returning
11224 -- class-wide types since there is no secondary stack (required to use
11225 -- 'reference).
11227 elsif Modify_Tree_For_C
11228 and then Nkind (Exp) = N_Function_Call
11229 and then Is_Class_Wide_Type (Etype (Exp))
11230 then
11231 return;
11232 end if;
11234 -- The remaining processing is done with all checks suppressed
11236 -- Note: from now on, don't use return statements, instead do a goto
11237 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11239 Scope_Suppress.Suppress := (others => True);
11241 -- If this is an elementary or a small not-by-reference record type, and
11242 -- we need to capture the value, just make a constant; this is cheap and
11243 -- objects of both kinds of types can be bit aligned, so it might not be
11244 -- possible to generate a reference to them. Likewise if this is not a
11245 -- name reference, except for a type conversion, because we would enter
11246 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11247 -- type has predicates (and type conversions need a specific treatment
11248 -- anyway, see below). Also do it if we have a volatile reference and
11249 -- Name_Req is not set (see comments for Side_Effect_Free).
11251 if (Is_Elementary_Type (Exp_Type)
11252 or else (Is_Record_Type (Exp_Type)
11253 and then Known_Static_RM_Size (Exp_Type)
11254 and then RM_Size (Exp_Type) <= 64
11255 and then not Has_Discriminants (Exp_Type)
11256 and then not Is_By_Reference_Type (Exp_Type)))
11257 and then (Variable_Ref
11258 or else (not Is_Name_Reference (Exp)
11259 and then Nkind (Exp) /= N_Type_Conversion)
11260 or else (not Name_Req
11261 and then Is_Volatile_Reference (Exp)))
11262 then
11263 Def_Id := Build_Temporary (Loc, 'R', Exp);
11264 Set_Etype (Def_Id, Exp_Type);
11265 Res := New_Occurrence_Of (Def_Id, Loc);
11267 -- If the expression is a packed reference, it must be reanalyzed and
11268 -- expanded, depending on context. This is the case for actuals where
11269 -- a constraint check may capture the actual before expansion of the
11270 -- call is complete.
11272 if Nkind (Exp) = N_Indexed_Component
11273 and then Is_Packed (Etype (Prefix (Exp)))
11274 then
11275 Set_Analyzed (Exp, False);
11276 Set_Analyzed (Prefix (Exp), False);
11277 end if;
11279 -- Generate:
11280 -- Rnn : Exp_Type renames Expr;
11282 if Renaming_Req then
11283 E :=
11284 Make_Object_Renaming_Declaration (Loc,
11285 Defining_Identifier => Def_Id,
11286 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11287 Name => Relocate_Node (Exp));
11289 -- Generate:
11290 -- Rnn : constant Exp_Type := Expr;
11292 else
11293 E :=
11294 Make_Object_Declaration (Loc,
11295 Defining_Identifier => Def_Id,
11296 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11297 Constant_Present => True,
11298 Expression => Relocate_Node (Exp));
11300 Set_Assignment_OK (E);
11301 end if;
11303 Insert_Action (Exp, E);
11305 -- If the expression has the form v.all then we can just capture the
11306 -- pointer, and then do an explicit dereference on the result, but
11307 -- this is not right if this is a volatile reference.
11309 elsif Nkind (Exp) = N_Explicit_Dereference
11310 and then not Is_Volatile_Reference (Exp)
11311 then
11312 Def_Id := Build_Temporary (Loc, 'R', Exp);
11313 Res :=
11314 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11316 Insert_Action (Exp,
11317 Make_Object_Declaration (Loc,
11318 Defining_Identifier => Def_Id,
11319 Object_Definition =>
11320 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11321 Constant_Present => True,
11322 Expression => Relocate_Node (Prefix (Exp))));
11324 -- Similar processing for an unchecked conversion of an expression of
11325 -- the form v.all, where we want the same kind of treatment.
11327 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11328 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11329 then
11330 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11331 goto Leave;
11333 -- If this is a type conversion, leave the type conversion and remove
11334 -- the side effects in the expression. This is important in several
11335 -- circumstances: for change of representations, and also when this is a
11336 -- view conversion to a smaller object, where gigi can end up creating
11337 -- its own temporary of the wrong size.
11339 elsif Nkind (Exp) = N_Type_Conversion then
11340 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11342 -- Generating C code the type conversion of an access to constrained
11343 -- array type into an access to unconstrained array type involves
11344 -- initializing a fat pointer and the expression must be free of
11345 -- side effects to safely compute its bounds.
11347 if Modify_Tree_For_C
11348 and then Is_Access_Type (Etype (Exp))
11349 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11350 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11351 then
11352 Def_Id := Build_Temporary (Loc, 'R', Exp);
11353 Set_Etype (Def_Id, Exp_Type);
11354 Res := New_Occurrence_Of (Def_Id, Loc);
11356 Insert_Action (Exp,
11357 Make_Object_Declaration (Loc,
11358 Defining_Identifier => Def_Id,
11359 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11360 Constant_Present => True,
11361 Expression => Relocate_Node (Exp)));
11362 else
11363 goto Leave;
11364 end if;
11366 -- If this is an unchecked conversion that Gigi can't handle, make
11367 -- a copy or a use a renaming to capture the value.
11369 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11370 and then not Safe_Unchecked_Type_Conversion (Exp)
11371 then
11372 if CW_Or_Has_Controlled_Part (Exp_Type) then
11374 -- Use a renaming to capture the expression, rather than create
11375 -- a controlled temporary.
11377 Def_Id := Build_Temporary (Loc, 'R', Exp);
11378 Res := New_Occurrence_Of (Def_Id, Loc);
11380 Insert_Action (Exp,
11381 Make_Object_Renaming_Declaration (Loc,
11382 Defining_Identifier => Def_Id,
11383 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11384 Name => Relocate_Node (Exp)));
11386 else
11387 Def_Id := Build_Temporary (Loc, 'R', Exp);
11388 Set_Etype (Def_Id, Exp_Type);
11389 Res := New_Occurrence_Of (Def_Id, Loc);
11391 E :=
11392 Make_Object_Declaration (Loc,
11393 Defining_Identifier => Def_Id,
11394 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11395 Constant_Present => not Is_Variable (Exp),
11396 Expression => Relocate_Node (Exp));
11398 Set_Assignment_OK (E);
11399 Insert_Action (Exp, E);
11400 end if;
11402 -- For expressions that denote names, we can use a renaming scheme.
11403 -- This is needed for correctness in the case of a volatile object of
11404 -- a non-volatile type because the Make_Reference call of the "default"
11405 -- approach would generate an illegal access value (an access value
11406 -- cannot designate such an object - see Analyze_Reference).
11408 elsif Is_Name_Reference (Exp)
11410 -- We skip using this scheme if we have an object of a volatile
11411 -- type and we do not have Name_Req set true (see comments for
11412 -- Side_Effect_Free).
11414 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11415 then
11416 Def_Id := Build_Temporary (Loc, 'R', Exp);
11417 Res := New_Occurrence_Of (Def_Id, Loc);
11419 Insert_Action (Exp,
11420 Make_Object_Renaming_Declaration (Loc,
11421 Defining_Identifier => Def_Id,
11422 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11423 Name => Relocate_Node (Exp)));
11425 -- If this is a packed reference, or a selected component with
11426 -- a non-standard representation, a reference to the temporary
11427 -- will be replaced by a copy of the original expression (see
11428 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11429 -- elaborated by gigi, and is of course not to be replaced in-line
11430 -- by the expression it renames, which would defeat the purpose of
11431 -- removing the side effect.
11433 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11434 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11435 then
11436 null;
11437 else
11438 Set_Is_Renaming_Of_Object (Def_Id, False);
11439 end if;
11441 -- Avoid generating a variable-sized temporary, by generating the
11442 -- reference just for the function call. The transformation could be
11443 -- refined to apply only when the array component is constrained by a
11444 -- discriminant???
11446 elsif Nkind (Exp) = N_Selected_Component
11447 and then Nkind (Prefix (Exp)) = N_Function_Call
11448 and then Is_Array_Type (Exp_Type)
11449 then
11450 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11451 goto Leave;
11453 -- Otherwise we generate a reference to the expression
11455 else
11456 -- An expression which is in SPARK mode is considered side effect
11457 -- free if the resulting value is captured by a variable or a
11458 -- constant.
11460 if GNATprove_Mode
11461 and then Nkind (Parent (Exp)) = N_Object_Declaration
11462 then
11463 goto Leave;
11465 -- When generating C code we cannot consider side effect free object
11466 -- declarations that have discriminants and are initialized by means
11467 -- of a function call since on this target there is no secondary
11468 -- stack to store the return value and the expander may generate an
11469 -- extra call to the function to compute the discriminant value. In
11470 -- addition, for targets that have secondary stack, the expansion of
11471 -- functions with side effects involves the generation of an access
11472 -- type to capture the return value stored in the secondary stack;
11473 -- by contrast when generating C code such expansion generates an
11474 -- internal object declaration (no access type involved) which must
11475 -- be identified here to avoid entering into a never-ending loop
11476 -- generating internal object declarations.
11478 elsif Modify_Tree_For_C
11479 and then Nkind (Parent (Exp)) = N_Object_Declaration
11480 and then
11481 (Nkind (Exp) /= N_Function_Call
11482 or else not Has_Discriminants (Exp_Type)
11483 or else Is_Internal_Name
11484 (Chars (Defining_Identifier (Parent (Exp)))))
11485 then
11486 goto Leave;
11487 end if;
11489 -- Special processing for function calls that return a limited type.
11490 -- We need to build a declaration that will enable build-in-place
11491 -- expansion of the call. This is not done if the context is already
11492 -- an object declaration, to prevent infinite recursion.
11494 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11495 -- to accommodate functions returning limited objects by reference.
11497 if Ada_Version >= Ada_2005
11498 and then Nkind (Exp) = N_Function_Call
11499 and then Is_Limited_View (Etype (Exp))
11500 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11501 then
11502 declare
11503 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11504 Decl : Node_Id;
11506 begin
11507 Decl :=
11508 Make_Object_Declaration (Loc,
11509 Defining_Identifier => Obj,
11510 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11511 Expression => Relocate_Node (Exp));
11513 Insert_Action (Exp, Decl);
11514 Set_Etype (Obj, Exp_Type);
11515 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11516 goto Leave;
11517 end;
11518 end if;
11520 Def_Id := Build_Temporary (Loc, 'R', Exp);
11522 -- The regular expansion of functions with side effects involves the
11523 -- generation of an access type to capture the return value found on
11524 -- the secondary stack. Since SPARK (and why) cannot process access
11525 -- types, use a different approach which ignores the secondary stack
11526 -- and "copies" the returned object.
11527 -- When generating C code, no need for a 'reference since the
11528 -- secondary stack is not supported.
11530 if GNATprove_Mode or Modify_Tree_For_C then
11531 Res := New_Occurrence_Of (Def_Id, Loc);
11532 Ref_Type := Exp_Type;
11534 -- Regular expansion utilizing an access type and 'reference
11536 else
11537 Res :=
11538 Make_Explicit_Dereference (Loc,
11539 Prefix => New_Occurrence_Of (Def_Id, Loc));
11541 -- Generate:
11542 -- type Ann is access all <Exp_Type>;
11544 Ref_Type := Make_Temporary (Loc, 'A');
11546 Ptr_Typ_Decl :=
11547 Make_Full_Type_Declaration (Loc,
11548 Defining_Identifier => Ref_Type,
11549 Type_Definition =>
11550 Make_Access_To_Object_Definition (Loc,
11551 All_Present => True,
11552 Subtype_Indication =>
11553 New_Occurrence_Of (Exp_Type, Loc)));
11555 Insert_Action (Exp, Ptr_Typ_Decl);
11556 end if;
11558 E := Exp;
11559 if Nkind (E) = N_Explicit_Dereference then
11560 New_Exp := Relocate_Node (Prefix (E));
11562 else
11563 E := Relocate_Node (E);
11565 -- Do not generate a 'reference in SPARK mode or C generation
11566 -- since the access type is not created in the first place.
11568 if GNATprove_Mode or Modify_Tree_For_C then
11569 New_Exp := E;
11571 -- Otherwise generate reference, marking the value as non-null
11572 -- since we know it cannot be null and we don't want a check.
11574 else
11575 New_Exp := Make_Reference (Loc, E);
11576 Set_Is_Known_Non_Null (Def_Id);
11577 end if;
11578 end if;
11580 if Is_Delayed_Aggregate (E) then
11582 -- The expansion of nested aggregates is delayed until the
11583 -- enclosing aggregate is expanded. As aggregates are often
11584 -- qualified, the predicate applies to qualified expressions as
11585 -- well, indicating that the enclosing aggregate has not been
11586 -- expanded yet. At this point the aggregate is part of a
11587 -- stand-alone declaration, and must be fully expanded.
11589 if Nkind (E) = N_Qualified_Expression then
11590 Set_Expansion_Delayed (Expression (E), False);
11591 Set_Analyzed (Expression (E), False);
11592 else
11593 Set_Expansion_Delayed (E, False);
11594 end if;
11596 Set_Analyzed (E, False);
11597 end if;
11599 -- Generating C code of object declarations that have discriminants
11600 -- and are initialized by means of a function call we propagate the
11601 -- discriminants of the parent type to the internally built object.
11602 -- This is needed to avoid generating an extra call to the called
11603 -- function.
11605 -- For example, if we generate here the following declaration, it
11606 -- will be expanded later adding an extra call to evaluate the value
11607 -- of the discriminant (needed to compute the size of the object).
11609 -- type Rec (D : Integer) is ...
11610 -- Obj : constant Rec := SomeFunc;
11612 if Modify_Tree_For_C
11613 and then Nkind (Parent (Exp)) = N_Object_Declaration
11614 and then Has_Discriminants (Exp_Type)
11615 and then Nkind (Exp) = N_Function_Call
11616 then
11617 Insert_Action (Exp,
11618 Make_Object_Declaration (Loc,
11619 Defining_Identifier => Def_Id,
11620 Object_Definition => New_Copy_Tree
11621 (Object_Definition (Parent (Exp))),
11622 Constant_Present => True,
11623 Expression => New_Exp));
11624 else
11625 Insert_Action (Exp,
11626 Make_Object_Declaration (Loc,
11627 Defining_Identifier => Def_Id,
11628 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11629 Constant_Present => True,
11630 Expression => New_Exp));
11631 end if;
11632 end if;
11634 -- Preserve the Assignment_OK flag in all copies, since at least one
11635 -- copy may be used in a context where this flag must be set (otherwise
11636 -- why would the flag be set in the first place).
11638 Set_Assignment_OK (Res, Assignment_OK (Exp));
11640 -- Finally rewrite the original expression and we are done
11642 Rewrite (Exp, Res);
11643 Analyze_And_Resolve (Exp, Exp_Type);
11645 <<Leave>>
11646 Scope_Suppress := Svg_Suppress;
11647 end Remove_Side_Effects;
11649 ------------------------
11650 -- Replace_References --
11651 ------------------------
11653 procedure Replace_References
11654 (Expr : Node_Id;
11655 Par_Typ : Entity_Id;
11656 Deriv_Typ : Entity_Id;
11657 Par_Obj : Entity_Id := Empty;
11658 Deriv_Obj : Entity_Id := Empty)
11660 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11661 -- Determine whether node Ref denotes some component of Deriv_Obj
11663 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11664 -- Substitute a reference to an entity with the corresponding value
11665 -- stored in table Type_Map.
11667 function Type_Of_Formal
11668 (Call : Node_Id;
11669 Actual : Node_Id) return Entity_Id;
11670 -- Find the type of the formal parameter which corresponds to actual
11671 -- parameter Actual in subprogram call Call.
11673 ----------------------
11674 -- Is_Deriv_Obj_Ref --
11675 ----------------------
11677 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11678 Par : constant Node_Id := Parent (Ref);
11680 begin
11681 -- Detect the folowing selected component form:
11683 -- Deriv_Obj.(something)
11685 return
11686 Nkind (Par) = N_Selected_Component
11687 and then Is_Entity_Name (Prefix (Par))
11688 and then Entity (Prefix (Par)) = Deriv_Obj;
11689 end Is_Deriv_Obj_Ref;
11691 -----------------
11692 -- Replace_Ref --
11693 -----------------
11695 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11696 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11697 -- Reset the Controlling_Argument of all function calls that
11698 -- encapsulate node From_Arg.
11700 ----------------------------------
11701 -- Remove_Controlling_Arguments --
11702 ----------------------------------
11704 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11705 Par : Node_Id;
11707 begin
11708 Par := From_Arg;
11709 while Present (Par) loop
11710 if Nkind (Par) = N_Function_Call
11711 and then Present (Controlling_Argument (Par))
11712 then
11713 Set_Controlling_Argument (Par, Empty);
11715 -- Prevent the search from going too far
11717 elsif Is_Body_Or_Package_Declaration (Par) then
11718 exit;
11719 end if;
11721 Par := Parent (Par);
11722 end loop;
11723 end Remove_Controlling_Arguments;
11725 -- Local variables
11727 Context : constant Node_Id := Parent (Ref);
11728 Loc : constant Source_Ptr := Sloc (Ref);
11729 Ref_Id : Entity_Id;
11730 Result : Traverse_Result;
11732 New_Ref : Node_Id;
11733 -- The new reference which is intended to substitute the old one
11735 Old_Ref : Node_Id;
11736 -- The reference designated for replacement. In certain cases this
11737 -- may be a node other than Ref.
11739 Val : Node_Or_Entity_Id;
11740 -- The corresponding value of Ref from the type map
11742 -- Start of processing for Replace_Ref
11744 begin
11745 -- Assume that the input reference is to be replaced and that the
11746 -- traversal should examine the children of the reference.
11748 Old_Ref := Ref;
11749 Result := OK;
11751 -- The input denotes a meaningful reference
11753 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11754 Ref_Id := Entity (Ref);
11755 Val := Type_Map.Get (Ref_Id);
11757 -- The reference has a corresponding value in the type map, a
11758 -- substitution is possible.
11760 if Present (Val) then
11762 -- The reference denotes a discriminant
11764 if Ekind (Ref_Id) = E_Discriminant then
11765 if Nkind (Val) in N_Entity then
11767 -- The value denotes another discriminant. Replace as
11768 -- follows:
11770 -- _object.Discr -> _object.Val
11772 if Ekind (Val) = E_Discriminant then
11773 New_Ref := New_Occurrence_Of (Val, Loc);
11775 -- Otherwise the value denotes the entity of a name which
11776 -- constraints the discriminant. Replace as follows:
11778 -- _object.Discr -> Val
11780 else
11781 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11783 New_Ref := New_Occurrence_Of (Val, Loc);
11784 Old_Ref := Parent (Old_Ref);
11785 end if;
11787 -- Otherwise the value denotes an arbitrary expression which
11788 -- constraints the discriminant. Replace as follows:
11790 -- _object.Discr -> Val
11792 else
11793 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11795 New_Ref := New_Copy_Tree (Val);
11796 Old_Ref := Parent (Old_Ref);
11797 end if;
11799 -- Otherwise the reference denotes a primitive. Replace as
11800 -- follows:
11802 -- Primitive -> Val
11804 else
11805 pragma Assert (Nkind (Val) in N_Entity);
11806 New_Ref := New_Occurrence_Of (Val, Loc);
11807 end if;
11809 -- The reference mentions the _object parameter of the parent
11810 -- type's DIC or type invariant procedure. Replace as follows:
11812 -- _object -> _object
11814 elsif Present (Par_Obj)
11815 and then Present (Deriv_Obj)
11816 and then Ref_Id = Par_Obj
11817 then
11818 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11820 -- The type of the _object parameter is class-wide when the
11821 -- expression comes from an assertion pragma that applies to
11822 -- an abstract parent type or an interface. The class-wide type
11823 -- facilitates the preanalysis of the expression by treating
11824 -- calls to abstract primitives that mention the current
11825 -- instance of the type as dispatching. Once the calls are
11826 -- remapped to invoke overriding or inherited primitives, the
11827 -- calls no longer need to be dispatching. Examine all function
11828 -- calls that encapsulate the _object parameter and reset their
11829 -- Controlling_Argument attribute.
11831 if Is_Class_Wide_Type (Etype (Par_Obj))
11832 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11833 then
11834 Remove_Controlling_Arguments (Old_Ref);
11835 end if;
11837 -- The reference to _object acts as an actual parameter in a
11838 -- subprogram call which may be invoking a primitive of the
11839 -- parent type:
11841 -- Primitive (... _object ...);
11843 -- The parent type primitive may not be overridden nor
11844 -- inherited when it is declared after the derived type
11845 -- definition:
11847 -- type Parent is tagged private;
11848 -- type Child is new Parent with private;
11849 -- procedure Primitive (Obj : Parent);
11851 -- In this scenario the _object parameter is converted to the
11852 -- parent type. Due to complications with partial/full views
11853 -- and view swaps, the parent type is taken from the formal
11854 -- parameter of the subprogram being called.
11856 if Nkind_In (Context, N_Function_Call,
11857 N_Procedure_Call_Statement)
11858 and then No (Type_Map.Get (Entity (Name (Context))))
11859 then
11860 New_Ref :=
11861 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11863 -- Do not process the generated type conversion because
11864 -- both the parent type and the derived type are in the
11865 -- Type_Map table. This will clobber the type conversion
11866 -- by resetting its subtype mark.
11868 Result := Skip;
11869 end if;
11871 -- Otherwise there is nothing to replace
11873 else
11874 New_Ref := Empty;
11875 end if;
11877 if Present (New_Ref) then
11878 Rewrite (Old_Ref, New_Ref);
11880 -- Update the return type when the context of the reference
11881 -- acts as the name of a function call. Note that the update
11882 -- should not be performed when the reference appears as an
11883 -- actual in the call.
11885 if Nkind (Context) = N_Function_Call
11886 and then Name (Context) = Old_Ref
11887 then
11888 Set_Etype (Context, Etype (Val));
11889 end if;
11890 end if;
11891 end if;
11893 -- Reanalyze the reference due to potential replacements
11895 if Nkind (Old_Ref) in N_Has_Etype then
11896 Set_Analyzed (Old_Ref, False);
11897 end if;
11899 return Result;
11900 end Replace_Ref;
11902 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11904 --------------------
11905 -- Type_Of_Formal --
11906 --------------------
11908 function Type_Of_Formal
11909 (Call : Node_Id;
11910 Actual : Node_Id) return Entity_Id
11912 A : Node_Id;
11913 F : Entity_Id;
11915 begin
11916 -- Examine the list of actual and formal parameters in parallel
11918 A := First (Parameter_Associations (Call));
11919 F := First_Formal (Entity (Name (Call)));
11920 while Present (A) and then Present (F) loop
11921 if A = Actual then
11922 return Etype (F);
11923 end if;
11925 Next (A);
11926 Next_Formal (F);
11927 end loop;
11929 -- The actual parameter must always have a corresponding formal
11931 pragma Assert (False);
11933 return Empty;
11934 end Type_Of_Formal;
11936 -- Start of processing for Replace_References
11938 begin
11939 -- Map the attributes of the parent type to the proper corresponding
11940 -- attributes of the derived type.
11942 Map_Types
11943 (Parent_Type => Par_Typ,
11944 Derived_Type => Deriv_Typ);
11946 -- Inspect the input expression and perform substitutions where
11947 -- necessary.
11949 Replace_Refs (Expr);
11950 end Replace_References;
11952 -----------------------------
11953 -- Replace_Type_References --
11954 -----------------------------
11956 procedure Replace_Type_References
11957 (Expr : Node_Id;
11958 Typ : Entity_Id;
11959 Obj_Id : Entity_Id)
11961 procedure Replace_Type_Ref (N : Node_Id);
11962 -- Substitute a single reference of the current instance of type Typ
11963 -- with a reference to Obj_Id.
11965 ----------------------
11966 -- Replace_Type_Ref --
11967 ----------------------
11969 procedure Replace_Type_Ref (N : Node_Id) is
11970 begin
11971 -- Decorate the reference to Typ even though it may be rewritten
11972 -- further down. This is done for two reasons:
11974 -- * ASIS has all necessary semantic information in the original
11975 -- tree.
11977 -- * Routines which examine properties of the Original_Node have
11978 -- some semantic information.
11980 if Nkind (N) = N_Identifier then
11981 Set_Entity (N, Typ);
11982 Set_Etype (N, Typ);
11984 elsif Nkind (N) = N_Selected_Component then
11985 Analyze (Prefix (N));
11986 Set_Entity (Selector_Name (N), Typ);
11987 Set_Etype (Selector_Name (N), Typ);
11988 end if;
11990 -- Perform the following substitution:
11992 -- Typ --> _object
11994 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
11995 Set_Comes_From_Source (N, True);
11996 end Replace_Type_Ref;
11998 procedure Replace_Type_Refs is
11999 new Replace_Type_References_Generic (Replace_Type_Ref);
12001 -- Start of processing for Replace_Type_References
12003 begin
12004 Replace_Type_Refs (Expr, Typ);
12005 end Replace_Type_References;
12007 ---------------------------
12008 -- Represented_As_Scalar --
12009 ---------------------------
12011 function Represented_As_Scalar (T : Entity_Id) return Boolean is
12012 UT : constant Entity_Id := Underlying_Type (T);
12013 begin
12014 return Is_Scalar_Type (UT)
12015 or else (Is_Bit_Packed_Array (UT)
12016 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
12017 end Represented_As_Scalar;
12019 ------------------------------
12020 -- Requires_Cleanup_Actions --
12021 ------------------------------
12023 function Requires_Cleanup_Actions
12024 (N : Node_Id;
12025 Lib_Level : Boolean) return Boolean
12027 At_Lib_Level : constant Boolean :=
12028 Lib_Level
12029 and then Nkind_In (N, N_Package_Body,
12030 N_Package_Specification);
12031 -- N is at the library level if the top-most context is a package and
12032 -- the path taken to reach N does not inlcude non-package constructs.
12034 begin
12035 case Nkind (N) is
12036 when N_Accept_Statement
12037 | N_Block_Statement
12038 | N_Entry_Body
12039 | N_Package_Body
12040 | N_Protected_Body
12041 | N_Subprogram_Body
12042 | N_Task_Body
12044 return
12045 Requires_Cleanup_Actions
12046 (L => Declarations (N),
12047 Lib_Level => At_Lib_Level,
12048 Nested_Constructs => True)
12049 or else
12050 (Present (Handled_Statement_Sequence (N))
12051 and then
12052 Requires_Cleanup_Actions
12053 (L =>
12054 Statements (Handled_Statement_Sequence (N)),
12055 Lib_Level => At_Lib_Level,
12056 Nested_Constructs => True));
12058 -- Extended return statements are the same as the above, except that
12059 -- there is no Declarations field. We do not want to clean up the
12060 -- Return_Object_Declarations.
12062 when N_Extended_Return_Statement =>
12063 return
12064 Present (Handled_Statement_Sequence (N))
12065 and then Requires_Cleanup_Actions
12066 (L =>
12067 Statements (Handled_Statement_Sequence (N)),
12068 Lib_Level => At_Lib_Level,
12069 Nested_Constructs => True);
12071 when N_Package_Specification =>
12072 return
12073 Requires_Cleanup_Actions
12074 (L => Visible_Declarations (N),
12075 Lib_Level => At_Lib_Level,
12076 Nested_Constructs => True)
12077 or else
12078 Requires_Cleanup_Actions
12079 (L => Private_Declarations (N),
12080 Lib_Level => At_Lib_Level,
12081 Nested_Constructs => True);
12083 when others =>
12084 raise Program_Error;
12085 end case;
12086 end Requires_Cleanup_Actions;
12088 ------------------------------
12089 -- Requires_Cleanup_Actions --
12090 ------------------------------
12092 function Requires_Cleanup_Actions
12093 (L : List_Id;
12094 Lib_Level : Boolean;
12095 Nested_Constructs : Boolean) return Boolean
12097 Decl : Node_Id;
12098 Expr : Node_Id;
12099 Obj_Id : Entity_Id;
12100 Obj_Typ : Entity_Id;
12101 Pack_Id : Entity_Id;
12102 Typ : Entity_Id;
12104 begin
12105 if No (L)
12106 or else Is_Empty_List (L)
12107 then
12108 return False;
12109 end if;
12111 Decl := First (L);
12112 while Present (Decl) loop
12114 -- Library-level tagged types
12116 if Nkind (Decl) = N_Full_Type_Declaration then
12117 Typ := Defining_Identifier (Decl);
12119 -- Ignored Ghost types do not need any cleanup actions because
12120 -- they will not appear in the final tree.
12122 if Is_Ignored_Ghost_Entity (Typ) then
12123 null;
12125 elsif Is_Tagged_Type (Typ)
12126 and then Is_Library_Level_Entity (Typ)
12127 and then Convention (Typ) = Convention_Ada
12128 and then Present (Access_Disp_Table (Typ))
12129 and then RTE_Available (RE_Unregister_Tag)
12130 and then not Is_Abstract_Type (Typ)
12131 and then not No_Run_Time_Mode
12132 then
12133 return True;
12134 end if;
12136 -- Regular object declarations
12138 elsif Nkind (Decl) = N_Object_Declaration then
12139 Obj_Id := Defining_Identifier (Decl);
12140 Obj_Typ := Base_Type (Etype (Obj_Id));
12141 Expr := Expression (Decl);
12143 -- Bypass any form of processing for objects which have their
12144 -- finalization disabled. This applies only to objects at the
12145 -- library level.
12147 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12148 null;
12150 -- Finalization of transient objects are treated separately in
12151 -- order to handle sensitive cases. These include:
12153 -- * Aggregate expansion
12154 -- * If, case, and expression with actions expansion
12155 -- * Transient scopes
12157 -- If one of those contexts has marked the transient object as
12158 -- ignored, do not generate finalization actions for it.
12160 elsif Is_Finalized_Transient (Obj_Id)
12161 or else Is_Ignored_Transient (Obj_Id)
12162 then
12163 null;
12165 -- Ignored Ghost objects do not need any cleanup actions because
12166 -- they will not appear in the final tree.
12168 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12169 null;
12171 -- The object is of the form:
12172 -- Obj : [constant] Typ [:= Expr];
12174 -- Do not process tag-to-class-wide conversions because they do
12175 -- not yield an object. Do not process the incomplete view of a
12176 -- deferred constant. Note that an object initialized by means
12177 -- of a build-in-place function call may appear as a deferred
12178 -- constant after expansion activities. These kinds of objects
12179 -- must be finalized.
12181 elsif not Is_Imported (Obj_Id)
12182 and then Needs_Finalization (Obj_Typ)
12183 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
12184 and then not (Ekind (Obj_Id) = E_Constant
12185 and then not Has_Completion (Obj_Id)
12186 and then No (BIP_Initialization_Call (Obj_Id)))
12187 then
12188 return True;
12190 -- The object is of the form:
12191 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12193 -- Obj : Access_Typ :=
12194 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12196 elsif Is_Access_Type (Obj_Typ)
12197 and then Needs_Finalization
12198 (Available_View (Designated_Type (Obj_Typ)))
12199 and then Present (Expr)
12200 and then
12201 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12202 or else
12203 (Is_Non_BIP_Func_Call (Expr)
12204 and then not Is_Related_To_Func_Return (Obj_Id)))
12205 then
12206 return True;
12208 -- Processing for "hook" objects generated for transient objects
12209 -- declared inside an Expression_With_Actions.
12211 elsif Is_Access_Type (Obj_Typ)
12212 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12213 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12214 N_Object_Declaration
12215 then
12216 return True;
12218 -- Processing for intermediate results of if expressions where
12219 -- one of the alternatives uses a controlled function call.
12221 elsif Is_Access_Type (Obj_Typ)
12222 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12223 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12224 N_Defining_Identifier
12225 and then Present (Expr)
12226 and then Nkind (Expr) = N_Null
12227 then
12228 return True;
12230 -- Simple protected objects which use type System.Tasking.
12231 -- Protected_Objects.Protection to manage their locks should be
12232 -- treated as controlled since they require manual cleanup.
12234 elsif Ekind (Obj_Id) = E_Variable
12235 and then (Is_Simple_Protected_Type (Obj_Typ)
12236 or else Has_Simple_Protected_Object (Obj_Typ))
12237 then
12238 return True;
12239 end if;
12241 -- Specific cases of object renamings
12243 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12244 Obj_Id := Defining_Identifier (Decl);
12245 Obj_Typ := Base_Type (Etype (Obj_Id));
12247 -- Bypass any form of processing for objects which have their
12248 -- finalization disabled. This applies only to objects at the
12249 -- library level.
12251 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12252 null;
12254 -- Ignored Ghost object renamings do not need any cleanup actions
12255 -- because they will not appear in the final tree.
12257 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12258 null;
12260 -- Return object of a build-in-place function. This case is
12261 -- recognized and marked by the expansion of an extended return
12262 -- statement (see Expand_N_Extended_Return_Statement).
12264 elsif Needs_Finalization (Obj_Typ)
12265 and then Is_Return_Object (Obj_Id)
12266 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12267 then
12268 return True;
12270 -- Detect a case where a source object has been initialized by
12271 -- a controlled function call or another object which was later
12272 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12274 -- Obj1 : CW_Type := Src_Obj;
12275 -- Obj2 : CW_Type := Function_Call (...);
12277 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12278 -- Tmp : ... := Function_Call (...)'reference;
12279 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12281 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12282 return True;
12283 end if;
12285 -- Inspect the freeze node of an access-to-controlled type and look
12286 -- for a delayed finalization master. This case arises when the
12287 -- freeze actions are inserted at a later time than the expansion of
12288 -- the context. Since Build_Finalizer is never called on a single
12289 -- construct twice, the master will be ultimately left out and never
12290 -- finalized. This is also needed for freeze actions of designated
12291 -- types themselves, since in some cases the finalization master is
12292 -- associated with a designated type's freeze node rather than that
12293 -- of the access type (see handling for freeze actions in
12294 -- Build_Finalization_Master).
12296 elsif Nkind (Decl) = N_Freeze_Entity
12297 and then Present (Actions (Decl))
12298 then
12299 Typ := Entity (Decl);
12301 -- Freeze nodes for ignored Ghost types do not need cleanup
12302 -- actions because they will never appear in the final tree.
12304 if Is_Ignored_Ghost_Entity (Typ) then
12305 null;
12307 elsif ((Is_Access_Type (Typ)
12308 and then not Is_Access_Subprogram_Type (Typ)
12309 and then Needs_Finalization
12310 (Available_View (Designated_Type (Typ))))
12311 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12312 and then Requires_Cleanup_Actions
12313 (Actions (Decl), Lib_Level, Nested_Constructs)
12314 then
12315 return True;
12316 end if;
12318 -- Nested package declarations
12320 elsif Nested_Constructs
12321 and then Nkind (Decl) = N_Package_Declaration
12322 then
12323 Pack_Id := Defining_Entity (Decl);
12325 -- Do not inspect an ignored Ghost package because all code found
12326 -- within will not appear in the final tree.
12328 if Is_Ignored_Ghost_Entity (Pack_Id) then
12329 null;
12331 elsif Ekind (Pack_Id) /= E_Generic_Package
12332 and then Requires_Cleanup_Actions
12333 (Specification (Decl), Lib_Level)
12334 then
12335 return True;
12336 end if;
12338 -- Nested package bodies
12340 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12342 -- Do not inspect an ignored Ghost package body because all code
12343 -- found within will not appear in the final tree.
12345 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12346 null;
12348 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12349 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12350 then
12351 return True;
12352 end if;
12354 elsif Nkind (Decl) = N_Block_Statement
12355 and then
12357 -- Handle a rare case caused by a controlled transient object
12358 -- created as part of a record init proc. The variable is wrapped
12359 -- in a block, but the block is not associated with a transient
12360 -- scope.
12362 (Inside_Init_Proc
12364 -- Handle the case where the original context has been wrapped in
12365 -- a block to avoid interference between exception handlers and
12366 -- At_End handlers. Treat the block as transparent and process its
12367 -- contents.
12369 or else Is_Finalization_Wrapper (Decl))
12370 then
12371 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12372 return True;
12373 end if;
12374 end if;
12376 Next (Decl);
12377 end loop;
12379 return False;
12380 end Requires_Cleanup_Actions;
12382 ------------------------------------
12383 -- Safe_Unchecked_Type_Conversion --
12384 ------------------------------------
12386 -- Note: this function knows quite a bit about the exact requirements of
12387 -- Gigi with respect to unchecked type conversions, and its code must be
12388 -- coordinated with any changes in Gigi in this area.
12390 -- The above requirements should be documented in Sinfo ???
12392 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12393 Otyp : Entity_Id;
12394 Ityp : Entity_Id;
12395 Oalign : Uint;
12396 Ialign : Uint;
12397 Pexp : constant Node_Id := Parent (Exp);
12399 begin
12400 -- If the expression is the RHS of an assignment or object declaration
12401 -- we are always OK because there will always be a target.
12403 -- Object renaming declarations, (generated for view conversions of
12404 -- actuals in inlined calls), like object declarations, provide an
12405 -- explicit type, and are safe as well.
12407 if (Nkind (Pexp) = N_Assignment_Statement
12408 and then Expression (Pexp) = Exp)
12409 or else Nkind_In (Pexp, N_Object_Declaration,
12410 N_Object_Renaming_Declaration)
12411 then
12412 return True;
12414 -- If the expression is the prefix of an N_Selected_Component we should
12415 -- also be OK because GCC knows to look inside the conversion except if
12416 -- the type is discriminated. We assume that we are OK anyway if the
12417 -- type is not set yet or if it is controlled since we can't afford to
12418 -- introduce a temporary in this case.
12420 elsif Nkind (Pexp) = N_Selected_Component
12421 and then Prefix (Pexp) = Exp
12422 then
12423 if No (Etype (Pexp)) then
12424 return True;
12425 else
12426 return
12427 not Has_Discriminants (Etype (Pexp))
12428 or else Is_Constrained (Etype (Pexp));
12429 end if;
12430 end if;
12432 -- Set the output type, this comes from Etype if it is set, otherwise we
12433 -- take it from the subtype mark, which we assume was already fully
12434 -- analyzed.
12436 if Present (Etype (Exp)) then
12437 Otyp := Etype (Exp);
12438 else
12439 Otyp := Entity (Subtype_Mark (Exp));
12440 end if;
12442 -- The input type always comes from the expression, and we assume this
12443 -- is indeed always analyzed, so we can simply get the Etype.
12445 Ityp := Etype (Expression (Exp));
12447 -- Initialize alignments to unknown so far
12449 Oalign := No_Uint;
12450 Ialign := No_Uint;
12452 -- Replace a concurrent type by its corresponding record type and each
12453 -- type by its underlying type and do the tests on those. The original
12454 -- type may be a private type whose completion is a concurrent type, so
12455 -- find the underlying type first.
12457 if Present (Underlying_Type (Otyp)) then
12458 Otyp := Underlying_Type (Otyp);
12459 end if;
12461 if Present (Underlying_Type (Ityp)) then
12462 Ityp := Underlying_Type (Ityp);
12463 end if;
12465 if Is_Concurrent_Type (Otyp) then
12466 Otyp := Corresponding_Record_Type (Otyp);
12467 end if;
12469 if Is_Concurrent_Type (Ityp) then
12470 Ityp := Corresponding_Record_Type (Ityp);
12471 end if;
12473 -- If the base types are the same, we know there is no problem since
12474 -- this conversion will be a noop.
12476 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12477 return True;
12479 -- Same if this is an upwards conversion of an untagged type, and there
12480 -- are no constraints involved (could be more general???)
12482 elsif Etype (Ityp) = Otyp
12483 and then not Is_Tagged_Type (Ityp)
12484 and then not Has_Discriminants (Ityp)
12485 and then No (First_Rep_Item (Base_Type (Ityp)))
12486 then
12487 return True;
12489 -- If the expression has an access type (object or subprogram) we assume
12490 -- that the conversion is safe, because the size of the target is safe,
12491 -- even if it is a record (which might be treated as having unknown size
12492 -- at this point).
12494 elsif Is_Access_Type (Ityp) then
12495 return True;
12497 -- If the size of output type is known at compile time, there is never
12498 -- a problem. Note that unconstrained records are considered to be of
12499 -- known size, but we can't consider them that way here, because we are
12500 -- talking about the actual size of the object.
12502 -- We also make sure that in addition to the size being known, we do not
12503 -- have a case which might generate an embarrassingly large temp in
12504 -- stack checking mode.
12506 elsif Size_Known_At_Compile_Time (Otyp)
12507 and then
12508 (not Stack_Checking_Enabled
12509 or else not May_Generate_Large_Temp (Otyp))
12510 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12511 then
12512 return True;
12514 -- If either type is tagged, then we know the alignment is OK so Gigi
12515 -- will be able to use pointer punning.
12517 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12518 return True;
12520 -- If either type is a limited record type, we cannot do a copy, so say
12521 -- safe since there's nothing else we can do.
12523 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12524 return True;
12526 -- Conversions to and from packed array types are always ignored and
12527 -- hence are safe.
12529 elsif Is_Packed_Array_Impl_Type (Otyp)
12530 or else Is_Packed_Array_Impl_Type (Ityp)
12531 then
12532 return True;
12533 end if;
12535 -- The only other cases known to be safe is if the input type's
12536 -- alignment is known to be at least the maximum alignment for the
12537 -- target or if both alignments are known and the output type's
12538 -- alignment is no stricter than the input's. We can use the component
12539 -- type alignment for an array if a type is an unpacked array type.
12541 if Present (Alignment_Clause (Otyp)) then
12542 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12544 elsif Is_Array_Type (Otyp)
12545 and then Present (Alignment_Clause (Component_Type (Otyp)))
12546 then
12547 Oalign := Expr_Value (Expression (Alignment_Clause
12548 (Component_Type (Otyp))));
12549 end if;
12551 if Present (Alignment_Clause (Ityp)) then
12552 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12554 elsif Is_Array_Type (Ityp)
12555 and then Present (Alignment_Clause (Component_Type (Ityp)))
12556 then
12557 Ialign := Expr_Value (Expression (Alignment_Clause
12558 (Component_Type (Ityp))));
12559 end if;
12561 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12562 return True;
12564 elsif Ialign /= No_Uint
12565 and then Oalign /= No_Uint
12566 and then Ialign <= Oalign
12567 then
12568 return True;
12570 -- Otherwise, Gigi cannot handle this and we must make a temporary
12572 else
12573 return False;
12574 end if;
12575 end Safe_Unchecked_Type_Conversion;
12577 ---------------------------------
12578 -- Set_Current_Value_Condition --
12579 ---------------------------------
12581 -- Note: the implementation of this procedure is very closely tied to the
12582 -- implementation of Get_Current_Value_Condition. Here we set required
12583 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12584 -- them, so they must have a consistent view.
12586 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12588 procedure Set_Entity_Current_Value (N : Node_Id);
12589 -- If N is an entity reference, where the entity is of an appropriate
12590 -- kind, then set the current value of this entity to Cnode, unless
12591 -- there is already a definite value set there.
12593 procedure Set_Expression_Current_Value (N : Node_Id);
12594 -- If N is of an appropriate form, sets an appropriate entry in current
12595 -- value fields of relevant entities. Multiple entities can be affected
12596 -- in the case of an AND or AND THEN.
12598 ------------------------------
12599 -- Set_Entity_Current_Value --
12600 ------------------------------
12602 procedure Set_Entity_Current_Value (N : Node_Id) is
12603 begin
12604 if Is_Entity_Name (N) then
12605 declare
12606 Ent : constant Entity_Id := Entity (N);
12608 begin
12609 -- Don't capture if not safe to do so
12611 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12612 return;
12613 end if;
12615 -- Here we have a case where the Current_Value field may need
12616 -- to be set. We set it if it is not already set to a compile
12617 -- time expression value.
12619 -- Note that this represents a decision that one condition
12620 -- blots out another previous one. That's certainly right if
12621 -- they occur at the same level. If the second one is nested,
12622 -- then the decision is neither right nor wrong (it would be
12623 -- equally OK to leave the outer one in place, or take the new
12624 -- inner one. Really we should record both, but our data
12625 -- structures are not that elaborate.
12627 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12628 Set_Current_Value (Ent, Cnode);
12629 end if;
12630 end;
12631 end if;
12632 end Set_Entity_Current_Value;
12634 ----------------------------------
12635 -- Set_Expression_Current_Value --
12636 ----------------------------------
12638 procedure Set_Expression_Current_Value (N : Node_Id) is
12639 Cond : Node_Id;
12641 begin
12642 Cond := N;
12644 -- Loop to deal with (ignore for now) any NOT operators present. The
12645 -- presence of NOT operators will be handled properly when we call
12646 -- Get_Current_Value_Condition.
12648 while Nkind (Cond) = N_Op_Not loop
12649 Cond := Right_Opnd (Cond);
12650 end loop;
12652 -- For an AND or AND THEN, recursively process operands
12654 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12655 Set_Expression_Current_Value (Left_Opnd (Cond));
12656 Set_Expression_Current_Value (Right_Opnd (Cond));
12657 return;
12658 end if;
12660 -- Check possible relational operator
12662 if Nkind (Cond) in N_Op_Compare then
12663 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12664 Set_Entity_Current_Value (Left_Opnd (Cond));
12665 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12666 Set_Entity_Current_Value (Right_Opnd (Cond));
12667 end if;
12669 elsif Nkind_In (Cond,
12670 N_Type_Conversion,
12671 N_Qualified_Expression,
12672 N_Expression_With_Actions)
12673 then
12674 Set_Expression_Current_Value (Expression (Cond));
12676 -- Check possible boolean variable reference
12678 else
12679 Set_Entity_Current_Value (Cond);
12680 end if;
12681 end Set_Expression_Current_Value;
12683 -- Start of processing for Set_Current_Value_Condition
12685 begin
12686 Set_Expression_Current_Value (Condition (Cnode));
12687 end Set_Current_Value_Condition;
12689 --------------------------
12690 -- Set_Elaboration_Flag --
12691 --------------------------
12693 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12694 Loc : constant Source_Ptr := Sloc (N);
12695 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12696 Asn : Node_Id;
12698 begin
12699 if Present (Ent) then
12701 -- Nothing to do if at the compilation unit level, because in this
12702 -- case the flag is set by the binder generated elaboration routine.
12704 if Nkind (Parent (N)) = N_Compilation_Unit then
12705 null;
12707 -- Here we do need to generate an assignment statement
12709 else
12710 Check_Restriction (No_Elaboration_Code, N);
12712 Asn :=
12713 Make_Assignment_Statement (Loc,
12714 Name => New_Occurrence_Of (Ent, Loc),
12715 Expression => Make_Integer_Literal (Loc, Uint_1));
12717 -- Mark the assignment statement as elaboration code. This allows
12718 -- the early call region mechanism (see Sem_Elab) to properly
12719 -- ignore such assignments even though they are non-preelaborable
12720 -- code.
12722 Set_Is_Elaboration_Code (Asn);
12724 if Nkind (Parent (N)) = N_Subunit then
12725 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12726 else
12727 Insert_After (N, Asn);
12728 end if;
12730 Analyze (Asn);
12732 -- Kill current value indication. This is necessary because the
12733 -- tests of this flag are inserted out of sequence and must not
12734 -- pick up bogus indications of the wrong constant value.
12736 Set_Current_Value (Ent, Empty);
12738 -- If the subprogram is in the current declarative part and
12739 -- 'access has been applied to it, generate an elaboration
12740 -- check at the beginning of the declarations of the body.
12742 if Nkind (N) = N_Subprogram_Body
12743 and then Address_Taken (Spec_Id)
12744 and then
12745 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12746 then
12747 declare
12748 Loc : constant Source_Ptr := Sloc (N);
12749 Decls : constant List_Id := Declarations (N);
12750 Chk : Node_Id;
12752 begin
12753 -- No need to generate this check if first entry in the
12754 -- declaration list is a raise of Program_Error now.
12756 if Present (Decls)
12757 and then Nkind (First (Decls)) = N_Raise_Program_Error
12758 then
12759 return;
12760 end if;
12762 -- Otherwise generate the check
12764 Chk :=
12765 Make_Raise_Program_Error (Loc,
12766 Condition =>
12767 Make_Op_Eq (Loc,
12768 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12769 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12770 Reason => PE_Access_Before_Elaboration);
12772 if No (Decls) then
12773 Set_Declarations (N, New_List (Chk));
12774 else
12775 Prepend (Chk, Decls);
12776 end if;
12778 Analyze (Chk);
12779 end;
12780 end if;
12781 end if;
12782 end if;
12783 end Set_Elaboration_Flag;
12785 ----------------------------
12786 -- Set_Renamed_Subprogram --
12787 ----------------------------
12789 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12790 begin
12791 -- If input node is an identifier, we can just reset it
12793 if Nkind (N) = N_Identifier then
12794 Set_Chars (N, Chars (E));
12795 Set_Entity (N, E);
12797 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12799 else
12800 declare
12801 CS : constant Boolean := Comes_From_Source (N);
12802 begin
12803 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12804 Set_Entity (N, E);
12805 Set_Comes_From_Source (N, CS);
12806 Set_Analyzed (N, True);
12807 end;
12808 end if;
12809 end Set_Renamed_Subprogram;
12811 ----------------------
12812 -- Side_Effect_Free --
12813 ----------------------
12815 function Side_Effect_Free
12816 (N : Node_Id;
12817 Name_Req : Boolean := False;
12818 Variable_Ref : Boolean := False) return Boolean
12820 Typ : constant Entity_Id := Etype (N);
12821 -- Result type of the expression
12823 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12824 -- The argument N is a construct where the Prefix is dereferenced if it
12825 -- is an access type and the result is a variable. The call returns True
12826 -- if the construct is side effect free (not considering side effects in
12827 -- other than the prefix which are to be tested by the caller).
12829 function Within_In_Parameter (N : Node_Id) return Boolean;
12830 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12831 -- N is not side-effect free when the actual is global and modifiable
12832 -- indirectly from within a subprogram, because it may be passed by
12833 -- reference. The front-end must be conservative here and assume that
12834 -- this may happen with any array or record type. On the other hand, we
12835 -- cannot create temporaries for all expressions for which this
12836 -- condition is true, for various reasons that might require clearing up
12837 -- ??? For example, discriminant references that appear out of place, or
12838 -- spurious type errors with class-wide expressions. As a result, we
12839 -- limit the transformation to loop bounds, which is so far the only
12840 -- case that requires it.
12842 -----------------------------
12843 -- Safe_Prefixed_Reference --
12844 -----------------------------
12846 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12847 begin
12848 -- If prefix is not side effect free, definitely not safe
12850 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12851 return False;
12853 -- If the prefix is of an access type that is not access-to-constant,
12854 -- then this construct is a variable reference, which means it is to
12855 -- be considered to have side effects if Variable_Ref is set True.
12857 elsif Is_Access_Type (Etype (Prefix (N)))
12858 and then not Is_Access_Constant (Etype (Prefix (N)))
12859 and then Variable_Ref
12860 then
12861 -- Exception is a prefix that is the result of a previous removal
12862 -- of side effects.
12864 return Is_Entity_Name (Prefix (N))
12865 and then not Comes_From_Source (Prefix (N))
12866 and then Ekind (Entity (Prefix (N))) = E_Constant
12867 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12869 -- If the prefix is an explicit dereference then this construct is a
12870 -- variable reference, which means it is to be considered to have
12871 -- side effects if Variable_Ref is True.
12873 -- We do NOT exclude dereferences of access-to-constant types because
12874 -- we handle them as constant view of variables.
12876 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12877 and then Variable_Ref
12878 then
12879 return False;
12881 -- Note: The following test is the simplest way of solving a complex
12882 -- problem uncovered by the following test (Side effect on loop bound
12883 -- that is a subcomponent of a global variable:
12885 -- with Text_Io; use Text_Io;
12886 -- procedure Tloop is
12887 -- type X is
12888 -- record
12889 -- V : Natural := 4;
12890 -- S : String (1..5) := (others => 'a');
12891 -- end record;
12892 -- X1 : X;
12894 -- procedure Modi;
12896 -- generic
12897 -- with procedure Action;
12898 -- procedure Loop_G (Arg : X; Msg : String)
12900 -- procedure Loop_G (Arg : X; Msg : String) is
12901 -- begin
12902 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12903 -- & Natural'Image (Arg.V));
12904 -- for Index in 1 .. Arg.V loop
12905 -- Text_Io.Put_Line
12906 -- (Natural'Image (Index) & " " & Arg.S (Index));
12907 -- if Index > 2 then
12908 -- Modi;
12909 -- end if;
12910 -- end loop;
12911 -- Put_Line ("end loop_g " & Msg);
12912 -- end;
12914 -- procedure Loop1 is new Loop_G (Modi);
12915 -- procedure Modi is
12916 -- begin
12917 -- X1.V := 1;
12918 -- Loop1 (X1, "from modi");
12919 -- end;
12921 -- begin
12922 -- Loop1 (X1, "initial");
12923 -- end;
12925 -- The output of the above program should be:
12927 -- begin loop_g initial will loop till: 4
12928 -- 1 a
12929 -- 2 a
12930 -- 3 a
12931 -- begin loop_g from modi will loop till: 1
12932 -- 1 a
12933 -- end loop_g from modi
12934 -- 4 a
12935 -- begin loop_g from modi will loop till: 1
12936 -- 1 a
12937 -- end loop_g from modi
12938 -- end loop_g initial
12940 -- If a loop bound is a subcomponent of a global variable, a
12941 -- modification of that variable within the loop may incorrectly
12942 -- affect the execution of the loop.
12944 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12945 and then Within_In_Parameter (Prefix (N))
12946 and then Variable_Ref
12947 then
12948 return False;
12950 -- All other cases are side effect free
12952 else
12953 return True;
12954 end if;
12955 end Safe_Prefixed_Reference;
12957 -------------------------
12958 -- Within_In_Parameter --
12959 -------------------------
12961 function Within_In_Parameter (N : Node_Id) return Boolean is
12962 begin
12963 if not Comes_From_Source (N) then
12964 return False;
12966 elsif Is_Entity_Name (N) then
12967 return Ekind (Entity (N)) = E_In_Parameter;
12969 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12970 return Within_In_Parameter (Prefix (N));
12972 else
12973 return False;
12974 end if;
12975 end Within_In_Parameter;
12977 -- Start of processing for Side_Effect_Free
12979 begin
12980 -- If volatile reference, always consider it to have side effects
12982 if Is_Volatile_Reference (N) then
12983 return False;
12984 end if;
12986 -- Note on checks that could raise Constraint_Error. Strictly, if we
12987 -- take advantage of 11.6, these checks do not count as side effects.
12988 -- However, we would prefer to consider that they are side effects,
12989 -- since the back end CSE does not work very well on expressions which
12990 -- can raise Constraint_Error. On the other hand if we don't consider
12991 -- them to be side effect free, then we get some awkward expansions
12992 -- in -gnato mode, resulting in code insertions at a point where we
12993 -- do not have a clear model for performing the insertions.
12995 -- Special handling for entity names
12997 if Is_Entity_Name (N) then
12999 -- A type reference is always side effect free
13001 if Is_Type (Entity (N)) then
13002 return True;
13004 -- Variables are considered to be a side effect if Variable_Ref
13005 -- is set or if we have a volatile reference and Name_Req is off.
13006 -- If Name_Req is True then we can't help returning a name which
13007 -- effectively allows multiple references in any case.
13009 elsif Is_Variable (N, Use_Original_Node => False) then
13010 return not Variable_Ref
13011 and then (not Is_Volatile_Reference (N) or else Name_Req);
13013 -- Any other entity (e.g. a subtype name) is definitely side
13014 -- effect free.
13016 else
13017 return True;
13018 end if;
13020 -- A value known at compile time is always side effect free
13022 elsif Compile_Time_Known_Value (N) then
13023 return True;
13025 -- A variable renaming is not side-effect free, because the renaming
13026 -- will function like a macro in the front-end in some cases, and an
13027 -- assignment can modify the component designated by N, so we need to
13028 -- create a temporary for it.
13030 -- The guard testing for Entity being present is needed at least in
13031 -- the case of rewritten predicate expressions, and may well also be
13032 -- appropriate elsewhere. Obviously we can't go testing the entity
13033 -- field if it does not exist, so it's reasonable to say that this is
13034 -- not the renaming case if it does not exist.
13036 elsif Is_Entity_Name (Original_Node (N))
13037 and then Present (Entity (Original_Node (N)))
13038 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
13039 and then Ekind (Entity (Original_Node (N))) /= E_Constant
13040 then
13041 declare
13042 RO : constant Node_Id :=
13043 Renamed_Object (Entity (Original_Node (N)));
13045 begin
13046 -- If the renamed object is an indexed component, or an
13047 -- explicit dereference, then the designated object could
13048 -- be modified by an assignment.
13050 if Nkind_In (RO, N_Indexed_Component,
13051 N_Explicit_Dereference)
13052 then
13053 return False;
13055 -- A selected component must have a safe prefix
13057 elsif Nkind (RO) = N_Selected_Component then
13058 return Safe_Prefixed_Reference (RO);
13060 -- In all other cases, designated object cannot be changed so
13061 -- we are side effect free.
13063 else
13064 return True;
13065 end if;
13066 end;
13068 -- Remove_Side_Effects generates an object renaming declaration to
13069 -- capture the expression of a class-wide expression. In VM targets
13070 -- the frontend performs no expansion for dispatching calls to
13071 -- class- wide types since they are handled by the VM. Hence, we must
13072 -- locate here if this node corresponds to a previous invocation of
13073 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13075 elsif not Tagged_Type_Expansion
13076 and then not Comes_From_Source (N)
13077 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
13078 and then Is_Class_Wide_Type (Typ)
13079 then
13080 return True;
13082 -- Generating C the type conversion of an access to constrained array
13083 -- type into an access to unconstrained array type involves initializing
13084 -- a fat pointer and the expression cannot be assumed to be free of side
13085 -- effects since it must referenced several times to compute its bounds.
13087 elsif Modify_Tree_For_C
13088 and then Nkind (N) = N_Type_Conversion
13089 and then Is_Access_Type (Typ)
13090 and then Is_Array_Type (Designated_Type (Typ))
13091 and then not Is_Constrained (Designated_Type (Typ))
13092 then
13093 return False;
13094 end if;
13096 -- For other than entity names and compile time known values,
13097 -- check the node kind for special processing.
13099 case Nkind (N) is
13101 -- An attribute reference is side effect free if its expressions
13102 -- are side effect free and its prefix is side effect free or
13103 -- is an entity reference.
13105 -- Is this right? what about x'first where x is a variable???
13107 when N_Attribute_Reference =>
13108 Attribute_Reference : declare
13110 function Side_Effect_Free_Attribute
13111 (Attribute_Name : Name_Id) return Boolean;
13112 -- Returns True if evaluation of the given attribute is
13113 -- considered side-effect free (independent of prefix and
13114 -- arguments).
13116 --------------------------------
13117 -- Side_Effect_Free_Attribute --
13118 --------------------------------
13120 function Side_Effect_Free_Attribute
13121 (Attribute_Name : Name_Id) return Boolean
13123 begin
13124 case Attribute_Name is
13125 when Name_Input =>
13126 return False;
13128 when Name_Image
13129 | Name_Img
13130 | Name_Wide_Image
13131 | Name_Wide_Wide_Image
13133 -- CodePeer doesn't want to see replicated copies of
13134 -- 'Image calls.
13136 return not CodePeer_Mode;
13138 when others =>
13139 return True;
13140 end case;
13141 end Side_Effect_Free_Attribute;
13143 -- Start of processing for Attribute_Reference
13145 begin
13146 return
13147 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13148 and then Side_Effect_Free_Attribute (Attribute_Name (N))
13149 and then (Is_Entity_Name (Prefix (N))
13150 or else Side_Effect_Free
13151 (Prefix (N), Name_Req, Variable_Ref));
13152 end Attribute_Reference;
13154 -- A binary operator is side effect free if and both operands are
13155 -- side effect free. For this purpose binary operators include
13156 -- membership tests and short circuit forms.
13158 when N_Binary_Op
13159 | N_Membership_Test
13160 | N_Short_Circuit
13162 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13163 and then
13164 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13166 -- An explicit dereference is side effect free only if it is
13167 -- a side effect free prefixed reference.
13169 when N_Explicit_Dereference =>
13170 return Safe_Prefixed_Reference (N);
13172 -- An expression with action is side effect free if its expression
13173 -- is side effect free and it has no actions.
13175 when N_Expression_With_Actions =>
13176 return
13177 Is_Empty_List (Actions (N))
13178 and then Side_Effect_Free
13179 (Expression (N), Name_Req, Variable_Ref);
13181 -- A call to _rep_to_pos is side effect free, since we generate
13182 -- this pure function call ourselves. Moreover it is critically
13183 -- important to make this exception, since otherwise we can have
13184 -- discriminants in array components which don't look side effect
13185 -- free in the case of an array whose index type is an enumeration
13186 -- type with an enumeration rep clause.
13188 -- All other function calls are not side effect free
13190 when N_Function_Call =>
13191 return
13192 Nkind (Name (N)) = N_Identifier
13193 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
13194 and then Side_Effect_Free
13195 (First (Parameter_Associations (N)),
13196 Name_Req, Variable_Ref);
13198 -- An IF expression is side effect free if it's of a scalar type, and
13199 -- all its components are all side effect free (conditions and then
13200 -- actions and else actions). We restrict to scalar types, since it
13201 -- is annoying to deal with things like (if A then B else C)'First
13202 -- where the type involved is a string type.
13204 when N_If_Expression =>
13205 return
13206 Is_Scalar_Type (Typ)
13207 and then Side_Effect_Free
13208 (Expressions (N), Name_Req, Variable_Ref);
13210 -- An indexed component is side effect free if it is a side
13211 -- effect free prefixed reference and all the indexing
13212 -- expressions are side effect free.
13214 when N_Indexed_Component =>
13215 return
13216 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13217 and then Safe_Prefixed_Reference (N);
13219 -- A type qualification, type conversion, or unchecked expression is
13220 -- side effect free if the expression is side effect free.
13222 when N_Qualified_Expression
13223 | N_Type_Conversion
13224 | N_Unchecked_Expression
13226 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13228 -- A selected component is side effect free only if it is a side
13229 -- effect free prefixed reference.
13231 when N_Selected_Component =>
13232 return Safe_Prefixed_Reference (N);
13234 -- A range is side effect free if the bounds are side effect free
13236 when N_Range =>
13237 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13238 and then
13239 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13241 -- A slice is side effect free if it is a side effect free
13242 -- prefixed reference and the bounds are side effect free.
13244 when N_Slice =>
13245 return
13246 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13247 and then Safe_Prefixed_Reference (N);
13249 -- A unary operator is side effect free if the operand
13250 -- is side effect free.
13252 when N_Unary_Op =>
13253 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13255 -- An unchecked type conversion is side effect free only if it
13256 -- is safe and its argument is side effect free.
13258 when N_Unchecked_Type_Conversion =>
13259 return
13260 Safe_Unchecked_Type_Conversion (N)
13261 and then Side_Effect_Free
13262 (Expression (N), Name_Req, Variable_Ref);
13264 -- A literal is side effect free
13266 when N_Character_Literal
13267 | N_Integer_Literal
13268 | N_Real_Literal
13269 | N_String_Literal
13271 return True;
13273 -- We consider that anything else has side effects. This is a bit
13274 -- crude, but we are pretty close for most common cases, and we
13275 -- are certainly correct (i.e. we never return True when the
13276 -- answer should be False).
13278 when others =>
13279 return False;
13280 end case;
13281 end Side_Effect_Free;
13283 -- A list is side effect free if all elements of the list are side
13284 -- effect free.
13286 function Side_Effect_Free
13287 (L : List_Id;
13288 Name_Req : Boolean := False;
13289 Variable_Ref : Boolean := False) return Boolean
13291 N : Node_Id;
13293 begin
13294 if L = No_List or else L = Error_List then
13295 return True;
13297 else
13298 N := First (L);
13299 while Present (N) loop
13300 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13301 return False;
13302 else
13303 Next (N);
13304 end if;
13305 end loop;
13307 return True;
13308 end if;
13309 end Side_Effect_Free;
13311 ----------------------------------
13312 -- Silly_Boolean_Array_Not_Test --
13313 ----------------------------------
13315 -- This procedure implements an odd and silly test. We explicitly check
13316 -- for the case where the 'First of the component type is equal to the
13317 -- 'Last of this component type, and if this is the case, we make sure
13318 -- that constraint error is raised. The reason is that the NOT is bound
13319 -- to cause CE in this case, and we will not otherwise catch it.
13321 -- No such check is required for AND and OR, since for both these cases
13322 -- False op False = False, and True op True = True. For the XOR case,
13323 -- see Silly_Boolean_Array_Xor_Test.
13325 -- Believe it or not, this was reported as a bug. Note that nearly always,
13326 -- the test will evaluate statically to False, so the code will be
13327 -- statically removed, and no extra overhead caused.
13329 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13330 Loc : constant Source_Ptr := Sloc (N);
13331 CT : constant Entity_Id := Component_Type (T);
13333 begin
13334 -- The check we install is
13336 -- constraint_error when
13337 -- component_type'first = component_type'last
13338 -- and then array_type'Length /= 0)
13340 -- We need the last guard because we don't want to raise CE for empty
13341 -- arrays since no out of range values result. (Empty arrays with a
13342 -- component type of True .. True -- very useful -- even the ACATS
13343 -- does not test that marginal case).
13345 Insert_Action (N,
13346 Make_Raise_Constraint_Error (Loc,
13347 Condition =>
13348 Make_And_Then (Loc,
13349 Left_Opnd =>
13350 Make_Op_Eq (Loc,
13351 Left_Opnd =>
13352 Make_Attribute_Reference (Loc,
13353 Prefix => New_Occurrence_Of (CT, Loc),
13354 Attribute_Name => Name_First),
13356 Right_Opnd =>
13357 Make_Attribute_Reference (Loc,
13358 Prefix => New_Occurrence_Of (CT, Loc),
13359 Attribute_Name => Name_Last)),
13361 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13362 Reason => CE_Range_Check_Failed));
13363 end Silly_Boolean_Array_Not_Test;
13365 ----------------------------------
13366 -- Silly_Boolean_Array_Xor_Test --
13367 ----------------------------------
13369 -- This procedure implements an odd and silly test. We explicitly check
13370 -- for the XOR case where the component type is True .. True, since this
13371 -- will raise constraint error. A special check is required since CE
13372 -- will not be generated otherwise (cf Expand_Packed_Not).
13374 -- No such check is required for AND and OR, since for both these cases
13375 -- False op False = False, and True op True = True, and no check is
13376 -- required for the case of False .. False, since False xor False = False.
13377 -- See also Silly_Boolean_Array_Not_Test
13379 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13380 Loc : constant Source_Ptr := Sloc (N);
13381 CT : constant Entity_Id := Component_Type (T);
13383 begin
13384 -- The check we install is
13386 -- constraint_error when
13387 -- Boolean (component_type'First)
13388 -- and then Boolean (component_type'Last)
13389 -- and then array_type'Length /= 0)
13391 -- We need the last guard because we don't want to raise CE for empty
13392 -- arrays since no out of range values result (Empty arrays with a
13393 -- component type of True .. True -- very useful -- even the ACATS
13394 -- does not test that marginal case).
13396 Insert_Action (N,
13397 Make_Raise_Constraint_Error (Loc,
13398 Condition =>
13399 Make_And_Then (Loc,
13400 Left_Opnd =>
13401 Make_And_Then (Loc,
13402 Left_Opnd =>
13403 Convert_To (Standard_Boolean,
13404 Make_Attribute_Reference (Loc,
13405 Prefix => New_Occurrence_Of (CT, Loc),
13406 Attribute_Name => Name_First)),
13408 Right_Opnd =>
13409 Convert_To (Standard_Boolean,
13410 Make_Attribute_Reference (Loc,
13411 Prefix => New_Occurrence_Of (CT, Loc),
13412 Attribute_Name => Name_Last))),
13414 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13415 Reason => CE_Range_Check_Failed));
13416 end Silly_Boolean_Array_Xor_Test;
13418 --------------------------
13419 -- Target_Has_Fixed_Ops --
13420 --------------------------
13422 Integer_Sized_Small : Ureal;
13423 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13424 -- called (we don't want to compute it more than once).
13426 Long_Integer_Sized_Small : Ureal;
13427 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13428 -- is called (we don't want to compute it more than once)
13430 First_Time_For_THFO : Boolean := True;
13431 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13433 function Target_Has_Fixed_Ops
13434 (Left_Typ : Entity_Id;
13435 Right_Typ : Entity_Id;
13436 Result_Typ : Entity_Id) return Boolean
13438 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13439 -- Return True if the given type is a fixed-point type with a small
13440 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13441 -- an absolute value less than 1.0. This is currently limited to
13442 -- fixed-point types that map to Integer or Long_Integer.
13444 ------------------------
13445 -- Is_Fractional_Type --
13446 ------------------------
13448 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13449 begin
13450 if Esize (Typ) = Standard_Integer_Size then
13451 return Small_Value (Typ) = Integer_Sized_Small;
13453 elsif Esize (Typ) = Standard_Long_Integer_Size then
13454 return Small_Value (Typ) = Long_Integer_Sized_Small;
13456 else
13457 return False;
13458 end if;
13459 end Is_Fractional_Type;
13461 -- Start of processing for Target_Has_Fixed_Ops
13463 begin
13464 -- Return False if Fractional_Fixed_Ops_On_Target is false
13466 if not Fractional_Fixed_Ops_On_Target then
13467 return False;
13468 end if;
13470 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13471 -- standard constants used by Is_Fractional_Type.
13473 if First_Time_For_THFO then
13474 First_Time_For_THFO := False;
13476 Integer_Sized_Small :=
13477 UR_From_Components
13478 (Num => Uint_1,
13479 Den => UI_From_Int (Standard_Integer_Size - 1),
13480 Rbase => 2);
13482 Long_Integer_Sized_Small :=
13483 UR_From_Components
13484 (Num => Uint_1,
13485 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13486 Rbase => 2);
13487 end if;
13489 -- Return True if target supports fixed-by-fixed multiply/divide for
13490 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13491 -- and result types are equivalent fractional types.
13493 return Is_Fractional_Type (Base_Type (Left_Typ))
13494 and then Is_Fractional_Type (Base_Type (Right_Typ))
13495 and then Is_Fractional_Type (Base_Type (Result_Typ))
13496 and then Esize (Left_Typ) = Esize (Right_Typ)
13497 and then Esize (Left_Typ) = Esize (Result_Typ);
13498 end Target_Has_Fixed_Ops;
13500 -------------------
13501 -- Type_Map_Hash --
13502 -------------------
13504 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13505 begin
13506 return Type_Map_Header (Id mod Type_Map_Size);
13507 end Type_Map_Hash;
13509 ------------------------------------------
13510 -- Type_May_Have_Bit_Aligned_Components --
13511 ------------------------------------------
13513 function Type_May_Have_Bit_Aligned_Components
13514 (Typ : Entity_Id) return Boolean
13516 begin
13517 -- Array type, check component type
13519 if Is_Array_Type (Typ) then
13520 return
13521 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13523 -- Record type, check components
13525 elsif Is_Record_Type (Typ) then
13526 declare
13527 E : Entity_Id;
13529 begin
13530 E := First_Component_Or_Discriminant (Typ);
13531 while Present (E) loop
13532 if Component_May_Be_Bit_Aligned (E)
13533 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13534 then
13535 return True;
13536 end if;
13538 Next_Component_Or_Discriminant (E);
13539 end loop;
13541 return False;
13542 end;
13544 -- Type other than array or record is always OK
13546 else
13547 return False;
13548 end if;
13549 end Type_May_Have_Bit_Aligned_Components;
13551 -------------------------------
13552 -- Update_Primitives_Mapping --
13553 -------------------------------
13555 procedure Update_Primitives_Mapping
13556 (Inher_Id : Entity_Id;
13557 Subp_Id : Entity_Id)
13559 begin
13560 Map_Types
13561 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13562 Derived_Type => Find_Dispatching_Type (Subp_Id));
13563 end Update_Primitives_Mapping;
13565 ----------------------------------
13566 -- Within_Case_Or_If_Expression --
13567 ----------------------------------
13569 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13570 Par : Node_Id;
13572 begin
13573 -- Locate an enclosing case or if expression. Note that these constructs
13574 -- can be expanded into Expression_With_Actions, hence the test of the
13575 -- original node.
13577 Par := Parent (N);
13578 while Present (Par) loop
13579 if Nkind_In (Original_Node (Par), N_Case_Expression,
13580 N_If_Expression)
13581 then
13582 return True;
13584 -- Prevent the search from going too far
13586 elsif Is_Body_Or_Package_Declaration (Par) then
13587 return False;
13588 end if;
13590 Par := Parent (Par);
13591 end loop;
13593 return False;
13594 end Within_Case_Or_If_Expression;
13596 --------------------------------
13597 -- Within_Internal_Subprogram --
13598 --------------------------------
13600 function Within_Internal_Subprogram return Boolean is
13601 S : Entity_Id;
13603 begin
13604 S := Current_Scope;
13605 while Present (S) and then not Is_Subprogram (S) loop
13606 S := Scope (S);
13607 end loop;
13609 return Present (S)
13610 and then Get_TSS_Name (S) /= TSS_Null
13611 and then not Is_Predicate_Function (S)
13612 and then not Is_Predicate_Function_M (S);
13613 end Within_Internal_Subprogram;
13615 end Exp_Util;