[NDS32] Implement bswapsi2 and bswaphi2 patterns.
[official-gcc.git] / gcc / ada / exp_util.adb
blob256f6bb9fff30b68908dad2b810fc4461a895a02
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 -- Certain run-time configurations and targets do not provide support
686 -- for controlled types.
688 if Restriction_Active (No_Finalization) then
689 return;
691 -- Do nothing if the access type may never allocate / deallocate
692 -- objects.
694 elsif No_Pool_Assigned (Ptr_Typ) then
695 return;
696 end if;
698 -- The allocation / deallocation of a controlled object must be
699 -- chained on / detached from a finalization master.
701 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
703 -- The only other kind of allocation / deallocation supported by this
704 -- routine is on / from a subpool.
706 elsif Nkind (Expr) = N_Allocator
707 and then No (Subpool_Handle_Name (Expr))
708 then
709 return;
710 end if;
712 declare
713 Loc : constant Source_Ptr := Sloc (N);
714 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
715 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
716 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
717 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
719 Actuals : List_Id;
720 Fin_Addr_Id : Entity_Id;
721 Fin_Mas_Act : Node_Id;
722 Fin_Mas_Id : Entity_Id;
723 Proc_To_Call : Entity_Id;
724 Subpool : Node_Id := Empty;
726 begin
727 -- Step 1: Construct all the actuals for the call to library routine
728 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
730 -- a) Storage pool
732 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
734 if Is_Allocate then
736 -- b) Subpool
738 if Nkind (Expr) = N_Allocator then
739 Subpool := Subpool_Handle_Name (Expr);
740 end if;
742 -- If a subpool is present it can be an arbitrary name, so make
743 -- the actual by copying the tree.
745 if Present (Subpool) then
746 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
747 else
748 Append_To (Actuals, Make_Null (Loc));
749 end if;
751 -- c) Finalization master
753 if Needs_Fin then
754 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
755 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
757 -- Handle the case where the master is actually a pointer to a
758 -- master. This case arises in build-in-place functions.
760 if Is_Access_Type (Etype (Fin_Mas_Id)) then
761 Append_To (Actuals, Fin_Mas_Act);
762 else
763 Append_To (Actuals,
764 Make_Attribute_Reference (Loc,
765 Prefix => Fin_Mas_Act,
766 Attribute_Name => Name_Unrestricted_Access));
767 end if;
768 else
769 Append_To (Actuals, Make_Null (Loc));
770 end if;
772 -- d) Finalize_Address
774 -- Primitive Finalize_Address is never generated in CodePeer mode
775 -- since it contains an Unchecked_Conversion.
777 if Needs_Fin and then not CodePeer_Mode then
778 Fin_Addr_Id := Finalize_Address (Desig_Typ);
779 pragma Assert (Present (Fin_Addr_Id));
781 Append_To (Actuals,
782 Make_Attribute_Reference (Loc,
783 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
784 Attribute_Name => Name_Unrestricted_Access));
785 else
786 Append_To (Actuals, Make_Null (Loc));
787 end if;
788 end if;
790 -- e) Address
791 -- f) Storage_Size
792 -- g) Alignment
794 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
795 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
797 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
798 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
800 -- For deallocation of class-wide types we obtain the value of
801 -- alignment from the Type Specific Record of the deallocated object.
802 -- This is needed because the frontend expansion of class-wide types
803 -- into equivalent types confuses the back end.
805 else
806 -- Generate:
807 -- Obj.all'Alignment
809 -- ... because 'Alignment applied to class-wide types is expanded
810 -- into the code that reads the value of alignment from the TSD
811 -- (see Expand_N_Attribute_Reference)
813 Append_To (Actuals,
814 Unchecked_Convert_To (RTE (RE_Storage_Offset),
815 Make_Attribute_Reference (Loc,
816 Prefix =>
817 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
818 Attribute_Name => Name_Alignment)));
819 end if;
821 -- h) Is_Controlled
823 if Needs_Fin then
824 Is_Controlled : declare
825 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
826 Flag_Expr : Node_Id;
827 Param : Node_Id;
828 Pref : Node_Id;
829 Temp : Node_Id;
831 begin
832 if Is_Allocate then
833 Temp := Find_Object (Expression (Expr));
834 else
835 Temp := Expr;
836 end if;
838 -- Processing for allocations where the expression is a subtype
839 -- indication.
841 if Is_Allocate
842 and then Is_Entity_Name (Temp)
843 and then Is_Type (Entity (Temp))
844 then
845 Flag_Expr :=
846 New_Occurrence_Of
847 (Boolean_Literals
848 (Needs_Finalization (Entity (Temp))), Loc);
850 -- The allocation / deallocation of a class-wide object relies
851 -- on a runtime check to determine whether the object is truly
852 -- controlled or not. Depending on this check, the finalization
853 -- machinery will request or reclaim extra storage reserved for
854 -- a list header.
856 elsif Is_Class_Wide_Type (Desig_Typ) then
858 -- Detect a special case where interface class-wide types
859 -- are involved as the object appears as:
861 -- Tag_Ptr (Base_Address (<object>'Address))
863 -- The expression already yields the proper tag, generate:
865 -- Temp.all
867 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
868 Param :=
869 Make_Explicit_Dereference (Loc,
870 Prefix => Relocate_Node (Temp));
872 -- In the default case, obtain the tag of the object about
873 -- to be allocated / deallocated. Generate:
875 -- Temp'Tag
877 -- If the object is an unchecked conversion (typically to
878 -- an access to class-wide type), we must preserve the
879 -- conversion to ensure that the object is seen as tagged
880 -- in the code that follows.
882 else
883 Pref := Temp;
885 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
886 then
887 Pref := Parent (Pref);
888 end if;
890 Param :=
891 Make_Attribute_Reference (Loc,
892 Prefix => Relocate_Node (Pref),
893 Attribute_Name => Name_Tag);
894 end if;
896 -- Generate:
897 -- Needs_Finalization (<Param>)
899 Flag_Expr :=
900 Make_Function_Call (Loc,
901 Name =>
902 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
903 Parameter_Associations => New_List (Param));
905 -- Processing for generic actuals
907 elsif Is_Generic_Actual_Type (Desig_Typ) then
908 Flag_Expr :=
909 New_Occurrence_Of (Boolean_Literals
910 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
912 -- The object does not require any specialized checks, it is
913 -- known to be controlled.
915 else
916 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
917 end if;
919 -- Create the temporary which represents the finalization state
920 -- of the expression. Generate:
922 -- F : constant Boolean := <Flag_Expr>;
924 Insert_Action (N,
925 Make_Object_Declaration (Loc,
926 Defining_Identifier => Flag_Id,
927 Constant_Present => True,
928 Object_Definition =>
929 New_Occurrence_Of (Standard_Boolean, Loc),
930 Expression => Flag_Expr));
932 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
933 end Is_Controlled;
935 -- The object is not controlled
937 else
938 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
939 end if;
941 -- i) On_Subpool
943 if Is_Allocate then
944 Append_To (Actuals,
945 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
946 end if;
948 -- Step 2: Build a wrapper Allocate / Deallocate which internally
949 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
951 -- Select the proper routine to call
953 if Is_Allocate then
954 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
955 else
956 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
957 end if;
959 -- Create a custom Allocate / Deallocate routine which has identical
960 -- profile to that of System.Storage_Pools.
962 Insert_Action (N,
963 Make_Subprogram_Body (Loc,
964 Specification =>
966 -- procedure Pnn
968 Make_Procedure_Specification (Loc,
969 Defining_Unit_Name => Proc_Id,
970 Parameter_Specifications => New_List (
972 -- P : Root_Storage_Pool
974 Make_Parameter_Specification (Loc,
975 Defining_Identifier => Make_Temporary (Loc, 'P'),
976 Parameter_Type =>
977 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
979 -- A : [out] Address
981 Make_Parameter_Specification (Loc,
982 Defining_Identifier => Addr_Id,
983 Out_Present => Is_Allocate,
984 Parameter_Type =>
985 New_Occurrence_Of (RTE (RE_Address), Loc)),
987 -- S : Storage_Count
989 Make_Parameter_Specification (Loc,
990 Defining_Identifier => Size_Id,
991 Parameter_Type =>
992 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
994 -- L : Storage_Count
996 Make_Parameter_Specification (Loc,
997 Defining_Identifier => Alig_Id,
998 Parameter_Type =>
999 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
1001 Declarations => No_List,
1003 Handled_Statement_Sequence =>
1004 Make_Handled_Sequence_Of_Statements (Loc,
1005 Statements => New_List (
1006 Make_Procedure_Call_Statement (Loc,
1007 Name =>
1008 New_Occurrence_Of (Proc_To_Call, Loc),
1009 Parameter_Associations => Actuals)))),
1010 Suppress => All_Checks);
1012 -- The newly generated Allocate / Deallocate becomes the default
1013 -- procedure to call when the back end processes the allocation /
1014 -- deallocation.
1016 if Is_Allocate then
1017 Set_Procedure_To_Call (Expr, Proc_Id);
1018 else
1019 Set_Procedure_To_Call (N, Proc_Id);
1020 end if;
1021 end;
1022 end Build_Allocate_Deallocate_Proc;
1024 -------------------------------
1025 -- Build_Abort_Undefer_Block --
1026 -------------------------------
1028 function Build_Abort_Undefer_Block
1029 (Loc : Source_Ptr;
1030 Stmts : List_Id;
1031 Context : Node_Id) return Node_Id
1033 Exceptions_OK : constant Boolean :=
1034 not Restriction_Active (No_Exception_Propagation);
1036 AUD : Entity_Id;
1037 Blk : Node_Id;
1038 Blk_Id : Entity_Id;
1039 HSS : Node_Id;
1041 begin
1042 -- The block should be generated only when undeferring abort in the
1043 -- context of a potential exception.
1045 pragma Assert (Abort_Allowed and Exceptions_OK);
1047 -- Generate:
1048 -- begin
1049 -- <Stmts>
1050 -- at end
1051 -- Abort_Undefer_Direct;
1052 -- end;
1054 AUD := RTE (RE_Abort_Undefer_Direct);
1056 HSS :=
1057 Make_Handled_Sequence_Of_Statements (Loc,
1058 Statements => Stmts,
1059 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1061 Blk :=
1062 Make_Block_Statement (Loc,
1063 Handled_Statement_Sequence => HSS);
1064 Set_Is_Abort_Block (Blk);
1066 Add_Block_Identifier (Blk, Blk_Id);
1067 Expand_At_End_Handler (HSS, Blk_Id);
1069 -- Present the Abort_Undefer_Direct function to the back end to inline
1070 -- the call to the routine.
1072 Add_Inlined_Body (AUD, Context);
1074 return Blk;
1075 end Build_Abort_Undefer_Block;
1077 ---------------------------------
1078 -- Build_Class_Wide_Expression --
1079 ---------------------------------
1081 procedure Build_Class_Wide_Expression
1082 (Prag : Node_Id;
1083 Subp : Entity_Id;
1084 Par_Subp : Entity_Id;
1085 Adjust_Sloc : Boolean;
1086 Needs_Wrapper : out Boolean)
1088 function Replace_Entity (N : Node_Id) return Traverse_Result;
1089 -- Replace reference to formal of inherited operation or to primitive
1090 -- operation of root type, with corresponding entity for derived type,
1091 -- when constructing the class-wide condition of an overriding
1092 -- subprogram.
1094 --------------------
1095 -- Replace_Entity --
1096 --------------------
1098 function Replace_Entity (N : Node_Id) return Traverse_Result is
1099 New_E : Entity_Id;
1101 begin
1102 if Adjust_Sloc then
1103 Adjust_Inherited_Pragma_Sloc (N);
1104 end if;
1106 if Nkind (N) = N_Identifier
1107 and then Present (Entity (N))
1108 and then
1109 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1110 and then
1111 (Nkind (Parent (N)) /= N_Attribute_Reference
1112 or else Attribute_Name (Parent (N)) /= Name_Class)
1113 then
1114 -- The replacement does not apply to dispatching calls within the
1115 -- condition, but only to calls whose static tag is that of the
1116 -- parent type.
1118 if Is_Subprogram (Entity (N))
1119 and then Nkind (Parent (N)) = N_Function_Call
1120 and then Present (Controlling_Argument (Parent (N)))
1121 then
1122 return OK;
1123 end if;
1125 -- Determine whether entity has a renaming
1127 New_E := Type_Map.Get (Entity (N));
1129 if Present (New_E) then
1130 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1132 -- AI12-0166: a precondition for a protected operation
1133 -- cannot include an internal call to a protected function
1134 -- of the type. In the case of an inherited condition for an
1135 -- overriding operation, both the operation and the function
1136 -- are given by primitive wrappers.
1138 if Ekind (New_E) = E_Function
1139 and then Is_Primitive_Wrapper (New_E)
1140 and then Is_Primitive_Wrapper (Subp)
1141 and then Scope (Subp) = Scope (New_E)
1142 then
1143 Error_Msg_Node_2 := Wrapped_Entity (Subp);
1144 Error_Msg_NE
1145 ("internal call to& cannot appear in inherited "
1146 & "precondition of protected operation&",
1147 N, Wrapped_Entity (New_E));
1148 end if;
1150 -- If the entity is an overridden primitive and we are not
1151 -- in GNATprove mode, we must build a wrapper for the current
1152 -- inherited operation. If the reference is the prefix of an
1153 -- attribute such as 'Result (or others ???) there is no need
1154 -- for a wrapper: the condition is just rewritten in terms of
1155 -- the inherited subprogram.
1157 if Is_Subprogram (New_E)
1158 and then Nkind (Parent (N)) /= N_Attribute_Reference
1159 and then not GNATprove_Mode
1160 then
1161 Needs_Wrapper := True;
1162 end if;
1163 end if;
1165 -- Check that there are no calls left to abstract operations if
1166 -- the current subprogram is not abstract.
1168 if Nkind (Parent (N)) = N_Function_Call
1169 and then N = Name (Parent (N))
1170 then
1171 if not Is_Abstract_Subprogram (Subp)
1172 and then Is_Abstract_Subprogram (Entity (N))
1173 then
1174 Error_Msg_Sloc := Sloc (Current_Scope);
1175 Error_Msg_Node_2 := Subp;
1176 if Comes_From_Source (Subp) then
1177 Error_Msg_NE
1178 ("cannot call abstract subprogram & in inherited "
1179 & "condition for&#", Subp, Entity (N));
1180 else
1181 Error_Msg_NE
1182 ("cannot call abstract subprogram & in inherited "
1183 & "condition for inherited&#", Subp, Entity (N));
1184 end if;
1186 -- In SPARK mode, reject an inherited condition for an
1187 -- inherited operation if it contains a call to an overriding
1188 -- operation, because this implies that the pre/postconditions
1189 -- of the inherited operation have changed silently.
1191 elsif SPARK_Mode = On
1192 and then Warn_On_Suspicious_Contract
1193 and then Present (Alias (Subp))
1194 and then Present (New_E)
1195 and then Comes_From_Source (New_E)
1196 then
1197 Error_Msg_N
1198 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1199 Parent (Subp));
1200 Error_Msg_Sloc := Sloc (New_E);
1201 Error_Msg_Node_2 := Subp;
1202 Error_Msg_NE
1203 ("\overriding of&# forces overriding of&",
1204 Parent (Subp), New_E);
1205 end if;
1206 end if;
1208 -- Update type of function call node, which should be the same as
1209 -- the function's return type.
1211 if Is_Subprogram (Entity (N))
1212 and then Nkind (Parent (N)) = N_Function_Call
1213 then
1214 Set_Etype (Parent (N), Etype (Entity (N)));
1215 end if;
1217 -- The whole expression will be reanalyzed
1219 elsif Nkind (N) in N_Has_Etype then
1220 Set_Analyzed (N, False);
1221 end if;
1223 return OK;
1224 end Replace_Entity;
1226 procedure Replace_Condition_Entities is
1227 new Traverse_Proc (Replace_Entity);
1229 -- Local variables
1231 Par_Formal : Entity_Id;
1232 Subp_Formal : Entity_Id;
1234 -- Start of processing for Build_Class_Wide_Expression
1236 begin
1237 Needs_Wrapper := False;
1239 -- Add mapping from old formals to new formals
1241 Par_Formal := First_Formal (Par_Subp);
1242 Subp_Formal := First_Formal (Subp);
1244 while Present (Par_Formal) and then Present (Subp_Formal) loop
1245 Type_Map.Set (Par_Formal, Subp_Formal);
1246 Next_Formal (Par_Formal);
1247 Next_Formal (Subp_Formal);
1248 end loop;
1250 Replace_Condition_Entities (Prag);
1251 end Build_Class_Wide_Expression;
1253 --------------------
1254 -- Build_DIC_Call --
1255 --------------------
1257 function Build_DIC_Call
1258 (Loc : Source_Ptr;
1259 Obj_Id : Entity_Id;
1260 Typ : Entity_Id) return Node_Id
1262 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1263 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1265 begin
1266 return
1267 Make_Procedure_Call_Statement (Loc,
1268 Name => New_Occurrence_Of (Proc_Id, Loc),
1269 Parameter_Associations => New_List (
1270 Make_Unchecked_Type_Conversion (Loc,
1271 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1272 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1273 end Build_DIC_Call;
1275 ------------------------------
1276 -- Build_DIC_Procedure_Body --
1277 ------------------------------
1279 -- WARNING: This routine manages Ghost regions. Return statements must be
1280 -- replaced by gotos which jump to the end of the routine and restore the
1281 -- Ghost mode.
1283 procedure Build_DIC_Procedure_Body
1284 (Typ : Entity_Id;
1285 For_Freeze : Boolean := False)
1287 procedure Add_DIC_Check
1288 (DIC_Prag : Node_Id;
1289 DIC_Expr : Node_Id;
1290 Stmts : in out List_Id);
1291 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1292 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1293 -- is added to list Stmts.
1295 procedure Add_Inherited_DIC
1296 (DIC_Prag : Node_Id;
1297 Par_Typ : Entity_Id;
1298 Deriv_Typ : Entity_Id;
1299 Stmts : in out List_Id);
1300 -- Add a runtime check to verify the assertion expression of inherited
1301 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1302 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1303 -- pragma. All generated code is added to list Stmts.
1305 procedure Add_Inherited_Tagged_DIC
1306 (DIC_Prag : Node_Id;
1307 Par_Typ : Entity_Id;
1308 Deriv_Typ : Entity_Id;
1309 Stmts : in out List_Id);
1310 -- Add a runtime check to verify assertion expression DIC_Expr of
1311 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1312 -- postcondition-like runtime semantics to the check. Par_Typ is the
1313 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1314 -- derived type inheriting the DIC pragma. All generated code is added
1315 -- to list Stmts.
1317 procedure Add_Own_DIC
1318 (DIC_Prag : Node_Id;
1319 DIC_Typ : Entity_Id;
1320 Stmts : in out List_Id);
1321 -- Add a runtime check to verify the assertion expression of pragma
1322 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1323 -- is added to list Stmts.
1325 -------------------
1326 -- Add_DIC_Check --
1327 -------------------
1329 procedure Add_DIC_Check
1330 (DIC_Prag : Node_Id;
1331 DIC_Expr : Node_Id;
1332 Stmts : in out List_Id)
1334 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1335 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1337 begin
1338 -- The DIC pragma is ignored, nothing left to do
1340 if Is_Ignored (DIC_Prag) then
1341 null;
1343 -- Otherwise the DIC expression must be checked at run time.
1344 -- Generate:
1346 -- pragma Check (<Nam>, <DIC_Expr>);
1348 else
1349 Append_New_To (Stmts,
1350 Make_Pragma (Loc,
1351 Pragma_Identifier =>
1352 Make_Identifier (Loc, Name_Check),
1354 Pragma_Argument_Associations => New_List (
1355 Make_Pragma_Argument_Association (Loc,
1356 Expression => Make_Identifier (Loc, Nam)),
1358 Make_Pragma_Argument_Association (Loc,
1359 Expression => DIC_Expr))));
1360 end if;
1361 end Add_DIC_Check;
1363 -----------------------
1364 -- Add_Inherited_DIC --
1365 -----------------------
1367 procedure Add_Inherited_DIC
1368 (DIC_Prag : Node_Id;
1369 Par_Typ : Entity_Id;
1370 Deriv_Typ : Entity_Id;
1371 Stmts : in out List_Id)
1373 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1374 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1375 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1376 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1377 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1379 begin
1380 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1382 -- Verify the inherited DIC assertion expression by calling the DIC
1383 -- procedure of the parent type.
1385 -- Generate:
1386 -- <Par_Typ>DIC (Par_Typ (_object));
1388 Append_New_To (Stmts,
1389 Make_Procedure_Call_Statement (Loc,
1390 Name => New_Occurrence_Of (Par_Proc, Loc),
1391 Parameter_Associations => New_List (
1392 Convert_To
1393 (Typ => Etype (Par_Obj),
1394 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1395 end Add_Inherited_DIC;
1397 ------------------------------
1398 -- Add_Inherited_Tagged_DIC --
1399 ------------------------------
1401 procedure Add_Inherited_Tagged_DIC
1402 (DIC_Prag : Node_Id;
1403 Par_Typ : Entity_Id;
1404 Deriv_Typ : Entity_Id;
1405 Stmts : in out List_Id)
1407 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1408 DIC_Args : constant List_Id :=
1409 Pragma_Argument_Associations (DIC_Prag);
1410 DIC_Arg : constant Node_Id := First (DIC_Args);
1411 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1412 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1414 Expr : Node_Id;
1416 begin
1417 -- The processing of an inherited DIC assertion expression starts off
1418 -- with a copy of the original parent expression where all references
1419 -- to the parent type have already been replaced with references to
1420 -- the _object formal parameter of the parent type's DIC procedure.
1422 pragma Assert (Present (DIC_Expr));
1423 Expr := New_Copy_Tree (DIC_Expr);
1425 -- Perform the following substitutions:
1427 -- * Replace a reference to the _object parameter of the parent
1428 -- type's DIC procedure with a reference to the _object parameter
1429 -- of the derived types' DIC procedure.
1431 -- * Replace a reference to a discriminant of the parent type with
1432 -- a suitable value from the point of view of the derived type.
1434 -- * Replace a call to an overridden parent primitive with a call
1435 -- to the overriding derived type primitive.
1437 -- * Replace a call to an inherited parent primitive with a call to
1438 -- the internally-generated inherited derived type primitive.
1440 -- Note that primitives defined in the private part are automatically
1441 -- handled by the overriding/inheritance mechanism and do not require
1442 -- an extra replacement pass.
1444 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1446 Replace_References
1447 (Expr => Expr,
1448 Par_Typ => Par_Typ,
1449 Deriv_Typ => Deriv_Typ,
1450 Par_Obj => First_Formal (Par_Proc),
1451 Deriv_Obj => First_Formal (Deriv_Proc));
1453 -- Once the DIC assertion expression is fully processed, add a check
1454 -- to the statements of the DIC procedure.
1456 Add_DIC_Check
1457 (DIC_Prag => DIC_Prag,
1458 DIC_Expr => Expr,
1459 Stmts => Stmts);
1460 end Add_Inherited_Tagged_DIC;
1462 -----------------
1463 -- Add_Own_DIC --
1464 -----------------
1466 procedure Add_Own_DIC
1467 (DIC_Prag : Node_Id;
1468 DIC_Typ : Entity_Id;
1469 Stmts : in out List_Id)
1471 DIC_Args : constant List_Id :=
1472 Pragma_Argument_Associations (DIC_Prag);
1473 DIC_Arg : constant Node_Id := First (DIC_Args);
1474 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1475 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1476 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1477 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1479 procedure Preanalyze_Own_DIC_For_ASIS;
1480 -- Preanalyze the original DIC expression of an aspect or a source
1481 -- pragma for ASIS.
1483 ---------------------------------
1484 -- Preanalyze_Own_DIC_For_ASIS --
1485 ---------------------------------
1487 procedure Preanalyze_Own_DIC_For_ASIS is
1488 Expr : Node_Id := Empty;
1490 begin
1491 -- The DIC pragma is a source construct, preanalyze the original
1492 -- expression of the pragma.
1494 if Comes_From_Source (DIC_Prag) then
1495 Expr := DIC_Expr;
1497 -- Otherwise preanalyze the expression of the corresponding aspect
1499 elsif Present (DIC_Asp) then
1500 Expr := Expression (DIC_Asp);
1501 end if;
1503 -- The expression must be subjected to the same substitutions as
1504 -- the copy used in the generation of the runtime check.
1506 if Present (Expr) then
1507 Replace_Type_References
1508 (Expr => Expr,
1509 Typ => DIC_Typ,
1510 Obj_Id => Obj_Id);
1512 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1513 end if;
1514 end Preanalyze_Own_DIC_For_ASIS;
1516 -- Local variables
1518 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1520 Expr : Node_Id;
1522 -- Start of processing for Add_Own_DIC
1524 begin
1525 pragma Assert (Present (DIC_Expr));
1526 Expr := New_Copy_Tree (DIC_Expr);
1528 -- Perform the following substitution:
1530 -- * Replace the current instance of DIC_Typ with a reference to
1531 -- the _object formal parameter of the DIC procedure.
1533 Replace_Type_References
1534 (Expr => Expr,
1535 Typ => DIC_Typ,
1536 Obj_Id => Obj_Id);
1538 -- Preanalyze the DIC expression to detect errors and at the same
1539 -- time capture the visibility of the proper package part.
1541 Set_Parent (Expr, Typ_Decl);
1542 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1544 -- Save a copy of the expression with all replacements and analysis
1545 -- already taken place in case a derived type inherits the pragma.
1546 -- The copy will be used as the foundation of the derived type's own
1547 -- version of the DIC assertion expression.
1549 if Is_Tagged_Type (DIC_Typ) then
1550 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1551 end if;
1553 -- If the pragma comes from an aspect specification, replace the
1554 -- saved expression because all type references must be substituted
1555 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1556 -- routines.
1558 if Present (DIC_Asp) then
1559 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1560 end if;
1562 -- Preanalyze the original DIC expression for ASIS
1564 if ASIS_Mode then
1565 Preanalyze_Own_DIC_For_ASIS;
1566 end if;
1568 -- Once the DIC assertion expression is fully processed, add a check
1569 -- to the statements of the DIC procedure.
1571 Add_DIC_Check
1572 (DIC_Prag => DIC_Prag,
1573 DIC_Expr => Expr,
1574 Stmts => Stmts);
1575 end Add_Own_DIC;
1577 -- Local variables
1579 Loc : constant Source_Ptr := Sloc (Typ);
1581 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1582 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1583 -- Save the Ghost-related attributes to restore on exit
1585 DIC_Prag : Node_Id;
1586 DIC_Typ : Entity_Id;
1587 Dummy_1 : Entity_Id;
1588 Dummy_2 : Entity_Id;
1589 Proc_Body : Node_Id;
1590 Proc_Body_Id : Entity_Id;
1591 Proc_Decl : Node_Id;
1592 Proc_Id : Entity_Id;
1593 Stmts : List_Id := No_List;
1595 Build_Body : Boolean := False;
1596 -- Flag set when the type requires a DIC procedure body to be built
1598 Work_Typ : Entity_Id;
1599 -- The working type
1601 -- Start of processing for Build_DIC_Procedure_Body
1603 begin
1604 Work_Typ := Base_Type (Typ);
1606 -- Do not process class-wide types as these are Itypes, but lack a first
1607 -- subtype (see below).
1609 if Is_Class_Wide_Type (Work_Typ) then
1610 return;
1612 -- Do not process the underlying full view of a private type. There is
1613 -- no way to get back to the partial view, plus the body will be built
1614 -- by the full view or the base type.
1616 elsif Is_Underlying_Full_View (Work_Typ) then
1617 return;
1619 -- Use the first subtype when dealing with various base types
1621 elsif Is_Itype (Work_Typ) then
1622 Work_Typ := First_Subtype (Work_Typ);
1624 -- The input denotes the corresponding record type of a protected or a
1625 -- task type. Work with the concurrent type because the corresponding
1626 -- record type may not be visible to clients of the type.
1628 elsif Ekind (Work_Typ) = E_Record_Type
1629 and then Is_Concurrent_Record_Type (Work_Typ)
1630 then
1631 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1632 end if;
1634 -- The working type may be subject to pragma Ghost. Set the mode now to
1635 -- ensure that the DIC procedure is properly marked as Ghost.
1637 Set_Ghost_Mode (Work_Typ);
1639 -- The working type must be either define a DIC pragma of its own or
1640 -- inherit one from a parent type.
1642 pragma Assert (Has_DIC (Work_Typ));
1644 -- Recover the type which defines the DIC pragma. This is either the
1645 -- working type itself or a parent type when the pragma is inherited.
1647 DIC_Typ := Find_DIC_Type (Work_Typ);
1648 pragma Assert (Present (DIC_Typ));
1650 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1651 pragma Assert (Present (DIC_Prag));
1653 -- Nothing to do if pragma DIC appears without an argument or its sole
1654 -- argument is "null".
1656 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1657 goto Leave;
1658 end if;
1660 -- The working type may lack a DIC procedure declaration. This may be
1661 -- due to several reasons:
1663 -- * The working type's own DIC pragma does not contain a verifiable
1664 -- assertion expression. In this case there is no need to build a
1665 -- DIC procedure because there is nothing to check.
1667 -- * The working type derives from a parent type. In this case a DIC
1668 -- procedure should be built only when the inherited DIC pragma has
1669 -- a verifiable assertion expression.
1671 Proc_Id := DIC_Procedure (Work_Typ);
1673 -- Build a DIC procedure declaration when the working type derives from
1674 -- a parent type.
1676 if No (Proc_Id) then
1677 Build_DIC_Procedure_Declaration (Work_Typ);
1678 Proc_Id := DIC_Procedure (Work_Typ);
1679 end if;
1681 -- At this point there should be a DIC procedure declaration
1683 pragma Assert (Present (Proc_Id));
1684 Proc_Decl := Unit_Declaration_Node (Proc_Id);
1686 -- Nothing to do if the DIC procedure already has a body
1688 if Present (Corresponding_Body (Proc_Decl)) then
1689 goto Leave;
1690 end if;
1692 -- Emulate the environment of the DIC procedure by installing its scope
1693 -- and formal parameters.
1695 Push_Scope (Proc_Id);
1696 Install_Formals (Proc_Id);
1698 -- The working type defines its own DIC pragma. Replace the current
1699 -- instance of the working type with the formal of the DIC procedure.
1700 -- Note that there is no need to consider inherited DIC pragmas from
1701 -- parent types because the working type's DIC pragma "hides" all
1702 -- inherited DIC pragmas.
1704 if Has_Own_DIC (Work_Typ) then
1705 pragma Assert (DIC_Typ = Work_Typ);
1707 Add_Own_DIC
1708 (DIC_Prag => DIC_Prag,
1709 DIC_Typ => DIC_Typ,
1710 Stmts => Stmts);
1712 Build_Body := True;
1714 -- Otherwise the working type inherits a DIC pragma from a parent type.
1715 -- This processing is carried out when the type is frozen because the
1716 -- state of all parent discriminants is known at that point. Note that
1717 -- it is semantically sound to delay the creation of the DIC procedure
1718 -- body till the freeze point. If the type has a DIC pragma of its own,
1719 -- then the DIC procedure body would have already been constructed at
1720 -- the end of the visible declarations and all parent DIC pragmas are
1721 -- effectively "hidden" and irrelevant.
1723 elsif For_Freeze then
1724 pragma Assert (Has_Inherited_DIC (Work_Typ));
1725 pragma Assert (DIC_Typ /= Work_Typ);
1727 -- The working type is tagged. The verification of the assertion
1728 -- expression is subject to the same semantics as class-wide pre-
1729 -- and postconditions.
1731 if Is_Tagged_Type (Work_Typ) then
1732 Add_Inherited_Tagged_DIC
1733 (DIC_Prag => DIC_Prag,
1734 Par_Typ => DIC_Typ,
1735 Deriv_Typ => Work_Typ,
1736 Stmts => Stmts);
1738 -- Otherwise the working type is not tagged. Verify the assertion
1739 -- expression of the inherited DIC pragma by directly calling the
1740 -- DIC procedure of the parent type.
1742 else
1743 Add_Inherited_DIC
1744 (DIC_Prag => DIC_Prag,
1745 Par_Typ => DIC_Typ,
1746 Deriv_Typ => Work_Typ,
1747 Stmts => Stmts);
1748 end if;
1750 Build_Body := True;
1751 end if;
1753 End_Scope;
1755 if Build_Body then
1757 -- Produce an empty completing body in the following cases:
1758 -- * Assertions are disabled
1759 -- * The DIC Assertion_Policy is Ignore
1761 if No (Stmts) then
1762 Stmts := New_List (Make_Null_Statement (Loc));
1763 end if;
1765 -- Generate:
1766 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1767 -- begin
1768 -- <Stmts>
1769 -- end <Work_Typ>DIC;
1771 Proc_Body :=
1772 Make_Subprogram_Body (Loc,
1773 Specification =>
1774 Copy_Subprogram_Spec (Parent (Proc_Id)),
1775 Declarations => Empty_List,
1776 Handled_Statement_Sequence =>
1777 Make_Handled_Sequence_Of_Statements (Loc,
1778 Statements => Stmts));
1779 Proc_Body_Id := Defining_Entity (Proc_Body);
1781 -- Perform minor decoration in case the body is not analyzed
1783 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1784 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1785 Set_Scope (Proc_Body_Id, Current_Scope);
1786 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
1787 Set_SPARK_Pragma_Inherited
1788 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
1790 -- Link both spec and body to avoid generating duplicates
1792 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1793 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1795 -- The body should not be inserted into the tree when the context
1796 -- is ASIS or a generic unit because it is not part of the template.
1797 -- Note that the body must still be generated in order to resolve the
1798 -- DIC assertion expression.
1800 if ASIS_Mode or Inside_A_Generic then
1801 null;
1803 -- Semi-insert the body into the tree for GNATprove by setting its
1804 -- Parent field. This allows for proper upstream tree traversals.
1806 elsif GNATprove_Mode then
1807 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1809 -- Otherwise the body is part of the freezing actions of the working
1810 -- type.
1812 else
1813 Append_Freeze_Action (Work_Typ, Proc_Body);
1814 end if;
1815 end if;
1817 <<Leave>>
1818 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1819 end Build_DIC_Procedure_Body;
1821 -------------------------------------
1822 -- Build_DIC_Procedure_Declaration --
1823 -------------------------------------
1825 -- WARNING: This routine manages Ghost regions. Return statements must be
1826 -- replaced by gotos which jump to the end of the routine and restore the
1827 -- Ghost mode.
1829 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1830 Loc : constant Source_Ptr := Sloc (Typ);
1832 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1833 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1834 -- Save the Ghost-related attributes to restore on exit
1836 DIC_Prag : Node_Id;
1837 DIC_Typ : Entity_Id;
1838 Proc_Decl : Node_Id;
1839 Proc_Id : Entity_Id;
1840 Typ_Decl : Node_Id;
1842 CRec_Typ : Entity_Id;
1843 -- The corresponding record type of Full_Typ
1845 Full_Base : Entity_Id;
1846 -- The base type of Full_Typ
1848 Full_Typ : Entity_Id;
1849 -- The full view of working type
1851 Obj_Id : Entity_Id;
1852 -- The _object formal parameter of the DIC procedure
1854 Priv_Typ : Entity_Id;
1855 -- The partial view of working type
1857 Work_Typ : Entity_Id;
1858 -- The working type
1860 begin
1861 Work_Typ := Base_Type (Typ);
1863 -- Do not process class-wide types as these are Itypes, but lack a first
1864 -- subtype (see below).
1866 if Is_Class_Wide_Type (Work_Typ) then
1867 return;
1869 -- Do not process the underlying full view of a private type. There is
1870 -- no way to get back to the partial view, plus the body will be built
1871 -- by the full view or the base type.
1873 elsif Is_Underlying_Full_View (Work_Typ) then
1874 return;
1876 -- Use the first subtype when dealing with various base types
1878 elsif Is_Itype (Work_Typ) then
1879 Work_Typ := First_Subtype (Work_Typ);
1881 -- The input denotes the corresponding record type of a protected or a
1882 -- task type. Work with the concurrent type because the corresponding
1883 -- record type may not be visible to clients of the type.
1885 elsif Ekind (Work_Typ) = E_Record_Type
1886 and then Is_Concurrent_Record_Type (Work_Typ)
1887 then
1888 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1889 end if;
1891 -- The working type may be subject to pragma Ghost. Set the mode now to
1892 -- ensure that the DIC procedure is properly marked as Ghost.
1894 Set_Ghost_Mode (Work_Typ);
1896 -- The type must be either subject to a DIC pragma or inherit one from a
1897 -- parent type.
1899 pragma Assert (Has_DIC (Work_Typ));
1901 -- Recover the type which defines the DIC pragma. This is either the
1902 -- working type itself or a parent type when the pragma is inherited.
1904 DIC_Typ := Find_DIC_Type (Work_Typ);
1905 pragma Assert (Present (DIC_Typ));
1907 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1908 pragma Assert (Present (DIC_Prag));
1910 -- Nothing to do if pragma DIC appears without an argument or its sole
1911 -- argument is "null".
1913 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1914 goto Leave;
1916 -- Nothing to do if the type already has a DIC procedure
1918 elsif Present (DIC_Procedure (Work_Typ)) then
1919 goto Leave;
1920 end if;
1922 Proc_Id :=
1923 Make_Defining_Identifier (Loc,
1924 Chars =>
1925 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1927 -- Perform minor decoration in case the declaration is not analyzed
1929 Set_Ekind (Proc_Id, E_Procedure);
1930 Set_Etype (Proc_Id, Standard_Void_Type);
1931 Set_Is_DIC_Procedure (Proc_Id);
1932 Set_Scope (Proc_Id, Current_Scope);
1933 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
1934 Set_SPARK_Pragma_Inherited (Proc_Id);
1936 Set_DIC_Procedure (Work_Typ, Proc_Id);
1938 -- The DIC procedure requires debug info when the assertion expression
1939 -- is subject to Source Coverage Obligations.
1941 if Generate_SCO then
1942 Set_Needs_Debug_Info (Proc_Id);
1943 end if;
1945 -- Obtain all views of the input type
1947 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1949 -- Associate the DIC procedure and various relevant flags with all views
1951 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1952 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1953 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1954 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1956 -- The declaration of the DIC procedure must be inserted after the
1957 -- declaration of the partial view as this allows for proper external
1958 -- visibility.
1960 if Present (Priv_Typ) then
1961 Typ_Decl := Declaration_Node (Priv_Typ);
1963 -- Derived types with the full view as parent do not have a partial
1964 -- view. Insert the DIC procedure after the derived type.
1966 else
1967 Typ_Decl := Declaration_Node (Full_Typ);
1968 end if;
1970 -- The type should have a declarative node
1972 pragma Assert (Present (Typ_Decl));
1974 -- Create the formal parameter which emulates the variable-like behavior
1975 -- of the type's current instance.
1977 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1979 -- Perform minor decoration in case the declaration is not analyzed
1981 Set_Ekind (Obj_Id, E_In_Parameter);
1982 Set_Etype (Obj_Id, Work_Typ);
1983 Set_Scope (Obj_Id, Proc_Id);
1985 Set_First_Entity (Proc_Id, Obj_Id);
1987 -- Generate:
1988 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1990 Proc_Decl :=
1991 Make_Subprogram_Declaration (Loc,
1992 Specification =>
1993 Make_Procedure_Specification (Loc,
1994 Defining_Unit_Name => Proc_Id,
1995 Parameter_Specifications => New_List (
1996 Make_Parameter_Specification (Loc,
1997 Defining_Identifier => Obj_Id,
1998 Parameter_Type =>
1999 New_Occurrence_Of (Work_Typ, Loc)))));
2001 -- The declaration should not be inserted into the tree when the context
2002 -- is ASIS or a generic unit because it is not part of the template.
2004 if ASIS_Mode or Inside_A_Generic then
2005 null;
2007 -- Semi-insert the declaration into the tree for GNATprove by setting
2008 -- its Parent field. This allows for proper upstream tree traversals.
2010 elsif GNATprove_Mode then
2011 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2013 -- Otherwise insert the declaration
2015 else
2016 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2017 end if;
2019 <<Leave>>
2020 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2021 end Build_DIC_Procedure_Declaration;
2023 ------------------------------------
2024 -- Build_Invariant_Procedure_Body --
2025 ------------------------------------
2027 -- WARNING: This routine manages Ghost regions. Return statements must be
2028 -- replaced by gotos which jump to the end of the routine and restore the
2029 -- Ghost mode.
2031 procedure Build_Invariant_Procedure_Body
2032 (Typ : Entity_Id;
2033 Partial_Invariant : Boolean := False)
2035 Loc : constant Source_Ptr := Sloc (Typ);
2037 Pragmas_Seen : Elist_Id := No_Elist;
2038 -- This list contains all invariant pragmas processed so far. The list
2039 -- is used to avoid generating redundant invariant checks.
2041 Produced_Check : Boolean := False;
2042 -- This flag tracks whether the type has produced at least one invariant
2043 -- check. The flag is used as a sanity check at the end of the routine.
2045 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2046 -- intentionally unnested to avoid deep indentation of code.
2048 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2049 -- they emit checks, loops (for arrays) and case statements (for record
2050 -- variant parts) only when there are invariants to verify. This keeps
2051 -- the body of the invariant procedure free of useless code.
2053 procedure Add_Array_Component_Invariants
2054 (T : Entity_Id;
2055 Obj_Id : Entity_Id;
2056 Checks : in out List_Id);
2057 -- Generate an invariant check for each component of array type T.
2058 -- Obj_Id denotes the entity of the _object formal parameter of the
2059 -- invariant procedure. All created checks are added to list Checks.
2061 procedure Add_Inherited_Invariants
2062 (T : Entity_Id;
2063 Priv_Typ : Entity_Id;
2064 Full_Typ : Entity_Id;
2065 Obj_Id : Entity_Id;
2066 Checks : in out List_Id);
2067 -- Generate an invariant check for each inherited class-wide invariant
2068 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2069 -- the partial and full view of the parent type. Obj_Id denotes the
2070 -- entity of the _object formal parameter of the invariant procedure.
2071 -- All created checks are added to list Checks.
2073 procedure Add_Interface_Invariants
2074 (T : Entity_Id;
2075 Obj_Id : Entity_Id;
2076 Checks : in out List_Id);
2077 -- Generate an invariant check for each inherited class-wide invariant
2078 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2079 -- entity of the _object formal parameter of the invariant procedure.
2080 -- All created checks are added to list Checks.
2082 procedure Add_Invariant_Check
2083 (Prag : Node_Id;
2084 Expr : Node_Id;
2085 Checks : in out List_Id;
2086 Inherited : Boolean := False);
2087 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2088 -- verify assertion expression Expr of pragma Prag. All generated code
2089 -- is added to list Checks. Flag Inherited should be set when the pragma
2090 -- is inherited from a parent or interface type.
2092 procedure Add_Own_Invariants
2093 (T : Entity_Id;
2094 Obj_Id : Entity_Id;
2095 Checks : in out List_Id;
2096 Priv_Item : Node_Id := Empty);
2097 -- Generate an invariant check for each invariant found for type T.
2098 -- Obj_Id denotes the entity of the _object formal parameter of the
2099 -- invariant procedure. All created checks are added to list Checks.
2100 -- Priv_Item denotes the first rep item of the private type.
2102 procedure Add_Parent_Invariants
2103 (T : Entity_Id;
2104 Obj_Id : Entity_Id;
2105 Checks : in out List_Id);
2106 -- Generate an invariant check for each inherited class-wide invariant
2107 -- coming from all parent types of type T. Obj_Id denotes the entity of
2108 -- the _object formal parameter of the invariant procedure. All created
2109 -- checks are added to list Checks.
2111 procedure Add_Record_Component_Invariants
2112 (T : Entity_Id;
2113 Obj_Id : Entity_Id;
2114 Checks : in out List_Id);
2115 -- Generate an invariant check for each component of record type T.
2116 -- Obj_Id denotes the entity of the _object formal parameter of the
2117 -- invariant procedure. All created checks are added to list Checks.
2119 ------------------------------------
2120 -- Add_Array_Component_Invariants --
2121 ------------------------------------
2123 procedure Add_Array_Component_Invariants
2124 (T : Entity_Id;
2125 Obj_Id : Entity_Id;
2126 Checks : in out List_Id)
2128 Comp_Typ : constant Entity_Id := Component_Type (T);
2129 Dims : constant Pos := Number_Dimensions (T);
2131 procedure Process_Array_Component
2132 (Indices : List_Id;
2133 Comp_Checks : in out List_Id);
2134 -- Generate an invariant check for an array component identified by
2135 -- the indices in list Indices. All created checks are added to list
2136 -- Comp_Checks.
2138 procedure Process_One_Dimension
2139 (Dim : Pos;
2140 Indices : List_Id;
2141 Dim_Checks : in out List_Id);
2142 -- Generate a loop over the Nth dimension Dim of an array type. List
2143 -- Indices contains all array indices for the dimension. All created
2144 -- checks are added to list Dim_Checks.
2146 -----------------------------
2147 -- Process_Array_Component --
2148 -----------------------------
2150 procedure Process_Array_Component
2151 (Indices : List_Id;
2152 Comp_Checks : in out List_Id)
2154 Proc_Id : Entity_Id;
2156 begin
2157 if Has_Invariants (Comp_Typ) then
2159 -- In GNATprove mode, the component invariants are checked by
2160 -- other means. They should not be added to the array type
2161 -- invariant procedure, so that the procedure can be used to
2162 -- check the array type invariants if any.
2164 if GNATprove_Mode then
2165 null;
2167 else
2168 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2170 -- The component type should have an invariant procedure
2171 -- if it has invariants of its own or inherits class-wide
2172 -- invariants from parent or interface types.
2174 pragma Assert (Present (Proc_Id));
2176 -- Generate:
2177 -- <Comp_Typ>Invariant (_object (<Indices>));
2179 -- Note that the invariant procedure may have a null body if
2180 -- assertions are disabled or Assertion_Policy Ignore is in
2181 -- effect.
2183 if not Has_Null_Body (Proc_Id) then
2184 Append_New_To (Comp_Checks,
2185 Make_Procedure_Call_Statement (Loc,
2186 Name =>
2187 New_Occurrence_Of (Proc_Id, Loc),
2188 Parameter_Associations => New_List (
2189 Make_Indexed_Component (Loc,
2190 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2191 Expressions => New_Copy_List (Indices)))));
2192 end if;
2193 end if;
2195 Produced_Check := True;
2196 end if;
2197 end Process_Array_Component;
2199 ---------------------------
2200 -- Process_One_Dimension --
2201 ---------------------------
2203 procedure Process_One_Dimension
2204 (Dim : Pos;
2205 Indices : List_Id;
2206 Dim_Checks : in out List_Id)
2208 Comp_Checks : List_Id := No_List;
2209 Index : Entity_Id;
2211 begin
2212 -- Generate the invariant checks for the array component after all
2213 -- dimensions have produced their respective loops.
2215 if Dim > Dims then
2216 Process_Array_Component
2217 (Indices => Indices,
2218 Comp_Checks => Dim_Checks);
2220 -- Otherwise create a loop for the current dimension
2222 else
2223 -- Create a new loop variable for each dimension
2225 Index :=
2226 Make_Defining_Identifier (Loc,
2227 Chars => New_External_Name ('I', Dim));
2228 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2230 Process_One_Dimension
2231 (Dim => Dim + 1,
2232 Indices => Indices,
2233 Dim_Checks => Comp_Checks);
2235 -- Generate:
2236 -- for I<Dim> in _object'Range (<Dim>) loop
2237 -- <Comp_Checks>
2238 -- end loop;
2240 -- Note that the invariant procedure may have a null body if
2241 -- assertions are disabled or Assertion_Policy Ignore is in
2242 -- effect.
2244 if Present (Comp_Checks) then
2245 Append_New_To (Dim_Checks,
2246 Make_Implicit_Loop_Statement (T,
2247 Identifier => Empty,
2248 Iteration_Scheme =>
2249 Make_Iteration_Scheme (Loc,
2250 Loop_Parameter_Specification =>
2251 Make_Loop_Parameter_Specification (Loc,
2252 Defining_Identifier => Index,
2253 Discrete_Subtype_Definition =>
2254 Make_Attribute_Reference (Loc,
2255 Prefix =>
2256 New_Occurrence_Of (Obj_Id, Loc),
2257 Attribute_Name => Name_Range,
2258 Expressions => New_List (
2259 Make_Integer_Literal (Loc, Dim))))),
2260 Statements => Comp_Checks));
2261 end if;
2262 end if;
2263 end Process_One_Dimension;
2265 -- Start of processing for Add_Array_Component_Invariants
2267 begin
2268 Process_One_Dimension
2269 (Dim => 1,
2270 Indices => New_List,
2271 Dim_Checks => Checks);
2272 end Add_Array_Component_Invariants;
2274 ------------------------------
2275 -- Add_Inherited_Invariants --
2276 ------------------------------
2278 procedure Add_Inherited_Invariants
2279 (T : Entity_Id;
2280 Priv_Typ : Entity_Id;
2281 Full_Typ : Entity_Id;
2282 Obj_Id : Entity_Id;
2283 Checks : in out List_Id)
2285 Deriv_Typ : Entity_Id;
2286 Expr : Node_Id;
2287 Prag : Node_Id;
2288 Prag_Expr : Node_Id;
2289 Prag_Expr_Arg : Node_Id;
2290 Prag_Typ : Node_Id;
2291 Prag_Typ_Arg : Node_Id;
2293 Par_Proc : Entity_Id;
2294 -- The "partial" invariant procedure of Par_Typ
2296 Par_Typ : Entity_Id;
2297 -- The suitable view of the parent type used in the substitution of
2298 -- type attributes.
2300 begin
2301 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2302 return;
2303 end if;
2305 -- When the type inheriting the class-wide invariant is a concurrent
2306 -- type, use the corresponding record type because it contains all
2307 -- primitive operations of the concurrent type and allows for proper
2308 -- substitution.
2310 if Is_Concurrent_Type (T) then
2311 Deriv_Typ := Corresponding_Record_Type (T);
2312 else
2313 Deriv_Typ := T;
2314 end if;
2316 pragma Assert (Present (Deriv_Typ));
2318 -- Determine which rep item chain to use. Precedence is given to that
2319 -- of the parent type's partial view since it usually carries all the
2320 -- class-wide invariants.
2322 if Present (Priv_Typ) then
2323 Prag := First_Rep_Item (Priv_Typ);
2324 else
2325 Prag := First_Rep_Item (Full_Typ);
2326 end if;
2328 while Present (Prag) loop
2329 if Nkind (Prag) = N_Pragma
2330 and then Pragma_Name (Prag) = Name_Invariant
2331 then
2332 -- Nothing to do if the pragma was already processed
2334 if Contains (Pragmas_Seen, Prag) then
2335 return;
2337 -- Nothing to do when the caller requests the processing of all
2338 -- inherited class-wide invariants, but the pragma does not
2339 -- fall in this category.
2341 elsif not Class_Present (Prag) then
2342 return;
2343 end if;
2345 -- Extract the arguments of the invariant pragma
2347 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2348 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2349 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2350 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2352 -- The pragma applies to the partial view of the parent type
2354 if Present (Priv_Typ)
2355 and then Entity (Prag_Typ) = Priv_Typ
2356 then
2357 Par_Typ := Priv_Typ;
2359 -- The pragma applies to the full view of the parent type
2361 elsif Present (Full_Typ)
2362 and then Entity (Prag_Typ) = Full_Typ
2363 then
2364 Par_Typ := Full_Typ;
2366 -- Otherwise the pragma does not belong to the parent type and
2367 -- should not be considered.
2369 else
2370 return;
2371 end if;
2373 -- Perform the following substitutions:
2375 -- * Replace a reference to the _object parameter of the
2376 -- parent type's partial invariant procedure with a
2377 -- reference to the _object parameter of the derived
2378 -- type's full invariant procedure.
2380 -- * Replace a reference to a discriminant of the parent type
2381 -- with a suitable value from the point of view of the
2382 -- derived type.
2384 -- * Replace a call to an overridden parent primitive with a
2385 -- call to the overriding derived type primitive.
2387 -- * Replace a call to an inherited parent primitive with a
2388 -- call to the internally-generated inherited derived type
2389 -- primitive.
2391 Expr := New_Copy_Tree (Prag_Expr);
2393 -- The parent type must have a "partial" invariant procedure
2394 -- because class-wide invariants are captured exclusively by
2395 -- it.
2397 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2398 pragma Assert (Present (Par_Proc));
2400 Replace_References
2401 (Expr => Expr,
2402 Par_Typ => Par_Typ,
2403 Deriv_Typ => Deriv_Typ,
2404 Par_Obj => First_Formal (Par_Proc),
2405 Deriv_Obj => Obj_Id);
2407 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2408 end if;
2410 Next_Rep_Item (Prag);
2411 end loop;
2412 end Add_Inherited_Invariants;
2414 ------------------------------
2415 -- Add_Interface_Invariants --
2416 ------------------------------
2418 procedure Add_Interface_Invariants
2419 (T : Entity_Id;
2420 Obj_Id : Entity_Id;
2421 Checks : in out List_Id)
2423 Iface_Elmt : Elmt_Id;
2424 Ifaces : Elist_Id;
2426 begin
2427 -- Generate an invariant check for each class-wide invariant coming
2428 -- from all interfaces implemented by type T.
2430 if Is_Tagged_Type (T) then
2431 Collect_Interfaces (T, Ifaces);
2433 -- Process the class-wide invariants of all implemented interfaces
2435 Iface_Elmt := First_Elmt (Ifaces);
2436 while Present (Iface_Elmt) loop
2438 -- The Full_Typ parameter is intentionally left Empty because
2439 -- interfaces are treated as the partial view of a private type
2440 -- in order to achieve uniformity with the general case.
2442 Add_Inherited_Invariants
2443 (T => T,
2444 Priv_Typ => Node (Iface_Elmt),
2445 Full_Typ => Empty,
2446 Obj_Id => Obj_Id,
2447 Checks => Checks);
2449 Next_Elmt (Iface_Elmt);
2450 end loop;
2451 end if;
2452 end Add_Interface_Invariants;
2454 -------------------------
2455 -- Add_Invariant_Check --
2456 -------------------------
2458 procedure Add_Invariant_Check
2459 (Prag : Node_Id;
2460 Expr : Node_Id;
2461 Checks : in out List_Id;
2462 Inherited : Boolean := False)
2464 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2465 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2466 Ploc : constant Source_Ptr := Sloc (Prag);
2467 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2469 Assoc : List_Id;
2470 Str : String_Id;
2472 begin
2473 -- The invariant is ignored, nothing left to do
2475 if Is_Ignored (Prag) then
2476 null;
2478 -- Otherwise the invariant is checked. Build a pragma Check to verify
2479 -- the expression at run time.
2481 else
2482 Assoc := New_List (
2483 Make_Pragma_Argument_Association (Ploc,
2484 Expression => Make_Identifier (Ploc, Nam)),
2485 Make_Pragma_Argument_Association (Ploc,
2486 Expression => Expr));
2488 -- Handle the String argument (if any)
2490 if Present (Str_Arg) then
2491 Str := Strval (Get_Pragma_Arg (Str_Arg));
2493 -- When inheriting an invariant, modify the message from
2494 -- "failed invariant" to "failed inherited invariant".
2496 if Inherited then
2497 String_To_Name_Buffer (Str);
2499 if Name_Buffer (1 .. 16) = "failed invariant" then
2500 Insert_Str_In_Name_Buffer ("inherited ", 8);
2501 Str := String_From_Name_Buffer;
2502 end if;
2503 end if;
2505 Append_To (Assoc,
2506 Make_Pragma_Argument_Association (Ploc,
2507 Expression => Make_String_Literal (Ploc, Str)));
2508 end if;
2510 -- Generate:
2511 -- pragma Check (<Nam>, <Expr>, <Str>);
2513 Append_New_To (Checks,
2514 Make_Pragma (Ploc,
2515 Chars => Name_Check,
2516 Pragma_Argument_Associations => Assoc));
2517 end if;
2519 -- Output an info message when inheriting an invariant and the
2520 -- listing option is enabled.
2522 if Inherited and Opt.List_Inherited_Aspects then
2523 Error_Msg_Sloc := Sloc (Prag);
2524 Error_Msg_N
2525 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2526 end if;
2528 -- Add the pragma to the list of processed pragmas
2530 Append_New_Elmt (Prag, Pragmas_Seen);
2531 Produced_Check := True;
2532 end Add_Invariant_Check;
2534 ---------------------------
2535 -- Add_Parent_Invariants --
2536 ---------------------------
2538 procedure Add_Parent_Invariants
2539 (T : Entity_Id;
2540 Obj_Id : Entity_Id;
2541 Checks : in out List_Id)
2543 Dummy_1 : Entity_Id;
2544 Dummy_2 : Entity_Id;
2546 Curr_Typ : Entity_Id;
2547 -- The entity of the current type being examined
2549 Full_Typ : Entity_Id;
2550 -- The full view of Par_Typ
2552 Par_Typ : Entity_Id;
2553 -- The entity of the parent type
2555 Priv_Typ : Entity_Id;
2556 -- The partial view of Par_Typ
2558 begin
2559 -- Do not process array types because they cannot have true parent
2560 -- types. This also prevents the generation of a duplicate invariant
2561 -- check when the input type is an array base type because its Etype
2562 -- denotes the first subtype, both of which share the same component
2563 -- type.
2565 if Is_Array_Type (T) then
2566 return;
2567 end if;
2569 -- Climb the parent type chain
2571 Curr_Typ := T;
2572 loop
2573 -- Do not consider subtypes as they inherit the invariants
2574 -- from their base types.
2576 Par_Typ := Base_Type (Etype (Curr_Typ));
2578 -- Stop the climb once the root of the parent chain is
2579 -- reached.
2581 exit when Curr_Typ = Par_Typ;
2583 -- Process the class-wide invariants of the parent type
2585 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2587 -- Process the elements of an array type
2589 if Is_Array_Type (Full_Typ) then
2590 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2592 -- Process the components of a record type
2594 elsif Ekind (Full_Typ) = E_Record_Type then
2595 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2596 end if;
2598 Add_Inherited_Invariants
2599 (T => T,
2600 Priv_Typ => Priv_Typ,
2601 Full_Typ => Full_Typ,
2602 Obj_Id => Obj_Id,
2603 Checks => Checks);
2605 Curr_Typ := Par_Typ;
2606 end loop;
2607 end Add_Parent_Invariants;
2609 ------------------------
2610 -- Add_Own_Invariants --
2611 ------------------------
2613 procedure Add_Own_Invariants
2614 (T : Entity_Id;
2615 Obj_Id : Entity_Id;
2616 Checks : in out List_Id;
2617 Priv_Item : Node_Id := Empty)
2619 ASIS_Expr : Node_Id;
2620 Expr : Node_Id;
2621 Prag : Node_Id;
2622 Prag_Asp : Node_Id;
2623 Prag_Expr : Node_Id;
2624 Prag_Expr_Arg : Node_Id;
2625 Prag_Typ : Node_Id;
2626 Prag_Typ_Arg : Node_Id;
2628 begin
2629 if not Present (T) then
2630 return;
2631 end if;
2633 Prag := First_Rep_Item (T);
2634 while Present (Prag) loop
2635 if Nkind (Prag) = N_Pragma
2636 and then Pragma_Name (Prag) = Name_Invariant
2637 then
2638 -- Stop the traversal of the rep item chain once a specific
2639 -- item is encountered.
2641 if Present (Priv_Item) and then Prag = Priv_Item then
2642 exit;
2643 end if;
2645 -- Nothing to do if the pragma was already processed
2647 if Contains (Pragmas_Seen, Prag) then
2648 return;
2649 end if;
2651 -- Extract the arguments of the invariant pragma
2653 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2654 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2655 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2656 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2657 Prag_Asp := Corresponding_Aspect (Prag);
2659 -- Verify the pragma belongs to T, otherwise the pragma applies
2660 -- to a parent type in which case it will be processed later by
2661 -- Add_Parent_Invariants or Add_Interface_Invariants.
2663 if Entity (Prag_Typ) /= T then
2664 return;
2665 end if;
2667 Expr := New_Copy_Tree (Prag_Expr);
2669 -- Substitute all references to type T with references to the
2670 -- _object formal parameter.
2672 Replace_Type_References (Expr, T, Obj_Id);
2674 -- Preanalyze the invariant expression to detect errors and at
2675 -- the same time capture the visibility of the proper package
2676 -- part.
2678 Set_Parent (Expr, Parent (Prag_Expr));
2679 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2681 -- Save a copy of the expression when T is tagged to detect
2682 -- errors and capture the visibility of the proper package part
2683 -- for the generation of inherited type invariants.
2685 if Is_Tagged_Type (T) then
2686 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2687 end if;
2689 -- If the pragma comes from an aspect specification, replace
2690 -- the saved expression because all type references must be
2691 -- substituted for the call to Preanalyze_Spec_Expression in
2692 -- Check_Aspect_At_xxx routines.
2694 if Present (Prag_Asp) then
2695 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2696 end if;
2698 -- Analyze the original invariant expression for ASIS
2700 if ASIS_Mode then
2701 ASIS_Expr := Empty;
2703 if Comes_From_Source (Prag) then
2704 ASIS_Expr := Prag_Expr;
2705 elsif Present (Prag_Asp) then
2706 ASIS_Expr := Expression (Prag_Asp);
2707 end if;
2709 if Present (ASIS_Expr) then
2710 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2711 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2712 end if;
2713 end if;
2715 Add_Invariant_Check (Prag, Expr, Checks);
2716 end if;
2718 Next_Rep_Item (Prag);
2719 end loop;
2720 end Add_Own_Invariants;
2722 -------------------------------------
2723 -- Add_Record_Component_Invariants --
2724 -------------------------------------
2726 procedure Add_Record_Component_Invariants
2727 (T : Entity_Id;
2728 Obj_Id : Entity_Id;
2729 Checks : in out List_Id)
2731 procedure Process_Component_List
2732 (Comp_List : Node_Id;
2733 CL_Checks : in out List_Id);
2734 -- Generate invariant checks for all record components found in
2735 -- component list Comp_List, including variant parts. All created
2736 -- checks are added to list CL_Checks.
2738 procedure Process_Record_Component
2739 (Comp_Id : Entity_Id;
2740 Comp_Checks : in out List_Id);
2741 -- Generate an invariant check for a record component identified by
2742 -- Comp_Id. All created checks are added to list Comp_Checks.
2744 ----------------------------
2745 -- Process_Component_List --
2746 ----------------------------
2748 procedure Process_Component_List
2749 (Comp_List : Node_Id;
2750 CL_Checks : in out List_Id)
2752 Comp : Node_Id;
2753 Var : Node_Id;
2754 Var_Alts : List_Id := No_List;
2755 Var_Checks : List_Id := No_List;
2756 Var_Stmts : List_Id;
2758 Produced_Variant_Check : Boolean := False;
2759 -- This flag tracks whether the component has produced at least
2760 -- one invariant check.
2762 begin
2763 -- Traverse the component items
2765 Comp := First (Component_Items (Comp_List));
2766 while Present (Comp) loop
2767 if Nkind (Comp) = N_Component_Declaration then
2769 -- Generate the component invariant check
2771 Process_Record_Component
2772 (Comp_Id => Defining_Entity (Comp),
2773 Comp_Checks => CL_Checks);
2774 end if;
2776 Next (Comp);
2777 end loop;
2779 -- Traverse the variant part
2781 if Present (Variant_Part (Comp_List)) then
2782 Var := First (Variants (Variant_Part (Comp_List)));
2783 while Present (Var) loop
2784 Var_Checks := No_List;
2786 -- Generate invariant checks for all components and variant
2787 -- parts that qualify.
2789 Process_Component_List
2790 (Comp_List => Component_List (Var),
2791 CL_Checks => Var_Checks);
2793 -- The components of the current variant produced at least
2794 -- one invariant check.
2796 if Present (Var_Checks) then
2797 Var_Stmts := Var_Checks;
2798 Produced_Variant_Check := True;
2800 -- Otherwise there are either no components with invariants,
2801 -- assertions are disabled, or Assertion_Policy Ignore is in
2802 -- effect.
2804 else
2805 Var_Stmts := New_List (Make_Null_Statement (Loc));
2806 end if;
2808 Append_New_To (Var_Alts,
2809 Make_Case_Statement_Alternative (Loc,
2810 Discrete_Choices =>
2811 New_Copy_List (Discrete_Choices (Var)),
2812 Statements => Var_Stmts));
2814 Next (Var);
2815 end loop;
2817 -- Create a case statement which verifies the invariant checks
2818 -- of a particular component list depending on the discriminant
2819 -- values only when there is at least one real invariant check.
2821 if Produced_Variant_Check then
2822 Append_New_To (CL_Checks,
2823 Make_Case_Statement (Loc,
2824 Expression =>
2825 Make_Selected_Component (Loc,
2826 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2827 Selector_Name =>
2828 New_Occurrence_Of
2829 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2830 Alternatives => Var_Alts));
2831 end if;
2832 end if;
2833 end Process_Component_List;
2835 ------------------------------
2836 -- Process_Record_Component --
2837 ------------------------------
2839 procedure Process_Record_Component
2840 (Comp_Id : Entity_Id;
2841 Comp_Checks : in out List_Id)
2843 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2844 Proc_Id : Entity_Id;
2846 Produced_Component_Check : Boolean := False;
2847 -- This flag tracks whether the component has produced at least
2848 -- one invariant check.
2850 begin
2851 -- Nothing to do for internal component _parent. Note that it is
2852 -- not desirable to check whether the component comes from source
2853 -- because protected type components are relocated to an internal
2854 -- corresponding record, but still need processing.
2856 if Chars (Comp_Id) = Name_uParent then
2857 return;
2858 end if;
2860 -- Verify the invariant of the component. Note that an access
2861 -- type may have an invariant when it acts as the full view of a
2862 -- private type and the invariant appears on the partial view. In
2863 -- this case verify the access value itself.
2865 if Has_Invariants (Comp_Typ) then
2867 -- In GNATprove mode, the component invariants are checked by
2868 -- other means. They should not be added to the record type
2869 -- invariant procedure, so that the procedure can be used to
2870 -- check the record type invariants if any.
2872 if GNATprove_Mode then
2873 null;
2875 else
2876 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2878 -- The component type should have an invariant procedure
2879 -- if it has invariants of its own or inherits class-wide
2880 -- invariants from parent or interface types.
2882 pragma Assert (Present (Proc_Id));
2884 -- Generate:
2885 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2887 -- Note that the invariant procedure may have a null body if
2888 -- assertions are disabled or Assertion_Policy Ignore is in
2889 -- effect.
2891 if not Has_Null_Body (Proc_Id) then
2892 Append_New_To (Comp_Checks,
2893 Make_Procedure_Call_Statement (Loc,
2894 Name =>
2895 New_Occurrence_Of (Proc_Id, Loc),
2896 Parameter_Associations => New_List (
2897 Make_Selected_Component (Loc,
2898 Prefix =>
2899 Unchecked_Convert_To
2900 (T, New_Occurrence_Of (Obj_Id, Loc)),
2901 Selector_Name =>
2902 New_Occurrence_Of (Comp_Id, Loc)))));
2903 end if;
2904 end if;
2906 Produced_Check := True;
2907 Produced_Component_Check := True;
2908 end if;
2910 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2911 Error_Msg_NE
2912 ("invariants cannot be checked on components of "
2913 & "unchecked_union type &?", Comp_Id, T);
2914 end if;
2915 end Process_Record_Component;
2917 -- Local variables
2919 Comps : Node_Id;
2920 Def : Node_Id;
2922 -- Start of processing for Add_Record_Component_Invariants
2924 begin
2925 -- An untagged derived type inherits the components of its parent
2926 -- type. In order to avoid creating redundant invariant checks, do
2927 -- not process the components now. Instead wait until the ultimate
2928 -- parent of the untagged derivation chain is reached.
2930 if not Is_Untagged_Derivation (T) then
2931 Def := Type_Definition (Parent (T));
2933 if Nkind (Def) = N_Derived_Type_Definition then
2934 Def := Record_Extension_Part (Def);
2935 end if;
2937 pragma Assert (Nkind (Def) = N_Record_Definition);
2938 Comps := Component_List (Def);
2940 if Present (Comps) then
2941 Process_Component_List
2942 (Comp_List => Comps,
2943 CL_Checks => Checks);
2944 end if;
2945 end if;
2946 end Add_Record_Component_Invariants;
2948 -- Local variables
2950 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2951 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2952 -- Save the Ghost-related attributes to restore on exit
2954 Dummy : Entity_Id;
2955 Priv_Item : Node_Id;
2956 Proc_Body : Node_Id;
2957 Proc_Body_Id : Entity_Id;
2958 Proc_Decl : Node_Id;
2959 Proc_Id : Entity_Id;
2960 Stmts : List_Id := No_List;
2962 CRec_Typ : Entity_Id := Empty;
2963 -- The corresponding record type of Full_Typ
2965 Full_Proc : Entity_Id := Empty;
2966 -- The entity of the "full" invariant procedure
2968 Full_Typ : Entity_Id := Empty;
2969 -- The full view of the working type
2971 Obj_Id : Entity_Id := Empty;
2972 -- The _object formal parameter of the invariant procedure
2974 Part_Proc : Entity_Id := Empty;
2975 -- The entity of the "partial" invariant procedure
2977 Priv_Typ : Entity_Id := Empty;
2978 -- The partial view of the working type
2980 Work_Typ : Entity_Id := Empty;
2981 -- The working type
2983 -- Start of processing for Build_Invariant_Procedure_Body
2985 begin
2986 Work_Typ := Typ;
2988 -- The input type denotes the implementation base type of a constrained
2989 -- array type. Work with the first subtype as all invariant pragmas are
2990 -- on its rep item chain.
2992 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2993 Work_Typ := First_Subtype (Work_Typ);
2995 -- The input type denotes the corresponding record type of a protected
2996 -- or task type. Work with the concurrent type because the corresponding
2997 -- record type may not be visible to clients of the type.
2999 elsif Ekind (Work_Typ) = E_Record_Type
3000 and then Is_Concurrent_Record_Type (Work_Typ)
3001 then
3002 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3003 end if;
3005 -- The working type may be subject to pragma Ghost. Set the mode now to
3006 -- ensure that the invariant procedure is properly marked as Ghost.
3008 Set_Ghost_Mode (Work_Typ);
3010 -- The type must either have invariants of its own, inherit class-wide
3011 -- invariants from parent types or interfaces, or be an array or record
3012 -- type whose components have invariants.
3014 pragma Assert (Has_Invariants (Work_Typ));
3016 -- Interfaces are treated as the partial view of a private type in order
3017 -- to achieve uniformity with the general case.
3019 if Is_Interface (Work_Typ) then
3020 Priv_Typ := Work_Typ;
3022 -- Otherwise obtain both views of the type
3024 else
3025 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3026 end if;
3028 -- The caller requests a body for the partial invariant procedure
3030 if Partial_Invariant then
3031 Full_Proc := Invariant_Procedure (Work_Typ);
3032 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3034 -- The "full" invariant procedure body was already created
3036 if Present (Full_Proc)
3037 and then Present
3038 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3039 then
3040 -- This scenario happens only when the type is an untagged
3041 -- derivation from a private parent and the underlying full
3042 -- view was processed before the partial view.
3044 pragma Assert
3045 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3047 -- Nothing to do because the processing of the underlying full
3048 -- view already checked the invariants of the partial view.
3050 goto Leave;
3051 end if;
3053 -- Create a declaration for the "partial" invariant procedure if it
3054 -- is not available.
3056 if No (Proc_Id) then
3057 Build_Invariant_Procedure_Declaration
3058 (Typ => Work_Typ,
3059 Partial_Invariant => True);
3061 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3062 end if;
3064 -- The caller requests a body for the "full" invariant procedure
3066 else
3067 Proc_Id := Invariant_Procedure (Work_Typ);
3068 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3070 -- Create a declaration for the "full" invariant procedure if it is
3071 -- not available.
3073 if No (Proc_Id) then
3074 Build_Invariant_Procedure_Declaration (Work_Typ);
3075 Proc_Id := Invariant_Procedure (Work_Typ);
3076 end if;
3077 end if;
3079 -- At this point there should be an invariant procedure declaration
3081 pragma Assert (Present (Proc_Id));
3082 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3084 -- Nothing to do if the invariant procedure already has a body
3086 if Present (Corresponding_Body (Proc_Decl)) then
3087 goto Leave;
3088 end if;
3090 -- Emulate the environment of the invariant procedure by installing its
3091 -- scope and formal parameters. Note that this is not needed, but having
3092 -- the scope installed helps with the detection of invariant-related
3093 -- errors.
3095 Push_Scope (Proc_Id);
3096 Install_Formals (Proc_Id);
3098 Obj_Id := First_Formal (Proc_Id);
3099 pragma Assert (Present (Obj_Id));
3101 -- The "partial" invariant procedure verifies the invariants of the
3102 -- partial view only.
3104 if Partial_Invariant then
3105 pragma Assert (Present (Priv_Typ));
3107 Add_Own_Invariants
3108 (T => Priv_Typ,
3109 Obj_Id => Obj_Id,
3110 Checks => Stmts);
3112 -- Otherwise the "full" invariant procedure verifies the invariants of
3113 -- the full view, all array or record components, as well as class-wide
3114 -- invariants inherited from parent types or interfaces. In addition, it
3115 -- indirectly verifies the invariants of the partial view by calling the
3116 -- "partial" invariant procedure.
3118 else
3119 pragma Assert (Present (Full_Typ));
3121 -- Check the invariants of the partial view by calling the "partial"
3122 -- invariant procedure. Generate:
3124 -- <Work_Typ>Partial_Invariant (_object);
3126 if Present (Part_Proc) then
3127 Append_New_To (Stmts,
3128 Make_Procedure_Call_Statement (Loc,
3129 Name => New_Occurrence_Of (Part_Proc, Loc),
3130 Parameter_Associations => New_List (
3131 New_Occurrence_Of (Obj_Id, Loc))));
3133 Produced_Check := True;
3134 end if;
3136 Priv_Item := Empty;
3138 -- Derived subtypes do not have a partial view
3140 if Present (Priv_Typ) then
3142 -- The processing of the "full" invariant procedure intentionally
3143 -- skips the partial view because a) this may result in changes of
3144 -- visibility and b) lead to duplicate checks. However, when the
3145 -- full view is the underlying full view of an untagged derived
3146 -- type whose parent type is private, partial invariants appear on
3147 -- the rep item chain of the partial view only.
3149 -- package Pack_1 is
3150 -- type Root ... is private;
3151 -- private
3152 -- <full view of Root>
3153 -- end Pack_1;
3155 -- with Pack_1;
3156 -- package Pack_2 is
3157 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3158 -- <underlying full view of Child>
3159 -- end Pack_2;
3161 -- As a result, the processing of the full view must also consider
3162 -- all invariants of the partial view.
3164 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3165 null;
3167 -- Otherwise the invariants of the partial view are ignored
3169 else
3170 -- Note that the rep item chain is shared between the partial
3171 -- and full views of a type. To avoid processing the invariants
3172 -- of the partial view, signal the logic to stop when the first
3173 -- rep item of the partial view has been reached.
3175 Priv_Item := First_Rep_Item (Priv_Typ);
3177 -- Ignore the invariants of the partial view by eliminating the
3178 -- view.
3180 Priv_Typ := Empty;
3181 end if;
3182 end if;
3184 -- Process the invariants of the full view and in certain cases those
3185 -- of the partial view. This also handles any invariants on array or
3186 -- record components.
3188 Add_Own_Invariants
3189 (T => Priv_Typ,
3190 Obj_Id => Obj_Id,
3191 Checks => Stmts,
3192 Priv_Item => Priv_Item);
3194 Add_Own_Invariants
3195 (T => Full_Typ,
3196 Obj_Id => Obj_Id,
3197 Checks => Stmts,
3198 Priv_Item => Priv_Item);
3200 -- Process the elements of an array type
3202 if Is_Array_Type (Full_Typ) then
3203 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3205 -- Process the components of a record type
3207 elsif Ekind (Full_Typ) = E_Record_Type then
3208 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3210 -- Process the components of a corresponding record
3212 elsif Present (CRec_Typ) then
3213 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3214 end if;
3216 -- Process the inherited class-wide invariants of all parent types.
3217 -- This also handles any invariants on record components.
3219 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3221 -- Process the inherited class-wide invariants of all implemented
3222 -- interface types.
3224 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3225 end if;
3227 End_Scope;
3229 -- At this point there should be at least one invariant check. If this
3230 -- is not the case, then the invariant-related flags were not properly
3231 -- set, or there is a missing invariant procedure on one of the array
3232 -- or record components.
3234 pragma Assert (Produced_Check);
3236 -- Account for the case where assertions are disabled or all invariant
3237 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3238 -- empty body.
3240 if No (Stmts) then
3241 Stmts := New_List (Make_Null_Statement (Loc));
3242 end if;
3244 -- Generate:
3245 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3246 -- begin
3247 -- <Stmts>
3248 -- end <Work_Typ>[Partial_]Invariant;
3250 Proc_Body :=
3251 Make_Subprogram_Body (Loc,
3252 Specification =>
3253 Copy_Subprogram_Spec (Parent (Proc_Id)),
3254 Declarations => Empty_List,
3255 Handled_Statement_Sequence =>
3256 Make_Handled_Sequence_Of_Statements (Loc,
3257 Statements => Stmts));
3258 Proc_Body_Id := Defining_Entity (Proc_Body);
3260 -- Perform minor decoration in case the body is not analyzed
3262 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3263 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3264 Set_Scope (Proc_Body_Id, Current_Scope);
3266 -- Link both spec and body to avoid generating duplicates
3268 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3269 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3271 -- The body should not be inserted into the tree when the context is
3272 -- ASIS or a generic unit because it is not part of the template. Note
3273 -- that the body must still be generated in order to resolve the
3274 -- invariants.
3276 if ASIS_Mode or Inside_A_Generic then
3277 null;
3279 -- Semi-insert the body into the tree for GNATprove by setting its
3280 -- Parent field. This allows for proper upstream tree traversals.
3282 elsif GNATprove_Mode then
3283 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3285 -- Otherwise the body is part of the freezing actions of the type
3287 else
3288 Append_Freeze_Action (Work_Typ, Proc_Body);
3289 end if;
3291 <<Leave>>
3292 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3293 end Build_Invariant_Procedure_Body;
3295 -------------------------------------------
3296 -- Build_Invariant_Procedure_Declaration --
3297 -------------------------------------------
3299 -- WARNING: This routine manages Ghost regions. Return statements must be
3300 -- replaced by gotos which jump to the end of the routine and restore the
3301 -- Ghost mode.
3303 procedure Build_Invariant_Procedure_Declaration
3304 (Typ : Entity_Id;
3305 Partial_Invariant : Boolean := False)
3307 Loc : constant Source_Ptr := Sloc (Typ);
3309 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3310 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3311 -- Save the Ghost-related attributes to restore on exit
3313 Proc_Decl : Node_Id;
3314 Proc_Id : Entity_Id;
3315 Proc_Nam : Name_Id;
3316 Typ_Decl : Node_Id;
3318 CRec_Typ : Entity_Id;
3319 -- The corresponding record type of Full_Typ
3321 Full_Base : Entity_Id;
3322 -- The base type of Full_Typ
3324 Full_Typ : Entity_Id;
3325 -- The full view of working type
3327 Obj_Id : Entity_Id;
3328 -- The _object formal parameter of the invariant procedure
3330 Obj_Typ : Entity_Id;
3331 -- The type of the _object formal parameter
3333 Priv_Typ : Entity_Id;
3334 -- The partial view of working type
3336 Work_Typ : Entity_Id;
3337 -- The working type
3339 begin
3340 Work_Typ := Typ;
3342 -- The input type denotes the implementation base type of a constrained
3343 -- array type. Work with the first subtype as all invariant pragmas are
3344 -- on its rep item chain.
3346 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3347 Work_Typ := First_Subtype (Work_Typ);
3349 -- The input denotes the corresponding record type of a protected or a
3350 -- task type. Work with the concurrent type because the corresponding
3351 -- record type may not be visible to clients of the type.
3353 elsif Ekind (Work_Typ) = E_Record_Type
3354 and then Is_Concurrent_Record_Type (Work_Typ)
3355 then
3356 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3357 end if;
3359 -- The working type may be subject to pragma Ghost. Set the mode now to
3360 -- ensure that the invariant procedure is properly marked as Ghost.
3362 Set_Ghost_Mode (Work_Typ);
3364 -- The type must either have invariants of its own, inherit class-wide
3365 -- invariants from parent or interface types, or be an array or record
3366 -- type whose components have invariants.
3368 pragma Assert (Has_Invariants (Work_Typ));
3370 -- Nothing to do if the type already has a "partial" invariant procedure
3372 if Partial_Invariant then
3373 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3374 goto Leave;
3375 end if;
3377 -- Nothing to do if the type already has a "full" invariant procedure
3379 elsif Present (Invariant_Procedure (Work_Typ)) then
3380 goto Leave;
3381 end if;
3383 -- The caller requests the declaration of the "partial" invariant
3384 -- procedure.
3386 if Partial_Invariant then
3387 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3389 -- Otherwise the caller requests the declaration of the "full" invariant
3390 -- procedure.
3392 else
3393 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3394 end if;
3396 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3398 -- Perform minor decoration in case the declaration is not analyzed
3400 Set_Ekind (Proc_Id, E_Procedure);
3401 Set_Etype (Proc_Id, Standard_Void_Type);
3402 Set_Scope (Proc_Id, Current_Scope);
3404 if Partial_Invariant then
3405 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3406 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3407 else
3408 Set_Is_Invariant_Procedure (Proc_Id);
3409 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3410 end if;
3412 -- The invariant procedure requires debug info when the invariants are
3413 -- subject to Source Coverage Obligations.
3415 if Generate_SCO then
3416 Set_Needs_Debug_Info (Proc_Id);
3417 end if;
3419 -- Obtain all views of the input type
3421 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3423 -- Associate the invariant procedure with all views
3425 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3426 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3427 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3428 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3430 -- The declaration of the invariant procedure is inserted after the
3431 -- declaration of the partial view as this allows for proper external
3432 -- visibility.
3434 if Present (Priv_Typ) then
3435 Typ_Decl := Declaration_Node (Priv_Typ);
3437 -- Anonymous arrays in object declarations have no explicit declaration
3438 -- so use the related object declaration as the insertion point.
3440 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3441 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3443 -- Derived types with the full view as parent do not have a partial
3444 -- view. Insert the invariant procedure after the derived type.
3446 else
3447 Typ_Decl := Declaration_Node (Full_Typ);
3448 end if;
3450 -- The type should have a declarative node
3452 pragma Assert (Present (Typ_Decl));
3454 -- Create the formal parameter which emulates the variable-like behavior
3455 -- of the current type instance.
3457 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3459 -- When generating an invariant procedure declaration for an abstract
3460 -- type (including interfaces), use the class-wide type as the _object
3461 -- type. This has several desirable effects:
3463 -- * The invariant procedure does not become a primitive of the type.
3464 -- This eliminates the need to either special case the treatment of
3465 -- invariant procedures, or to make it a predefined primitive and
3466 -- force every derived type to potentially provide an empty body.
3468 -- * The invariant procedure does not need to be declared as abstract.
3469 -- This allows for a proper body, which in turn avoids redundant
3470 -- processing of the same invariants for types with multiple views.
3472 -- * The class-wide type allows for calls to abstract primitives
3473 -- within a nonabstract subprogram. The calls are treated as
3474 -- dispatching and require additional processing when they are
3475 -- remapped to call primitives of derived types. See routine
3476 -- Replace_References for details.
3478 if Is_Abstract_Type (Work_Typ) then
3479 Obj_Typ := Class_Wide_Type (Work_Typ);
3480 else
3481 Obj_Typ := Work_Typ;
3482 end if;
3484 -- Perform minor decoration in case the declaration is not analyzed
3486 Set_Ekind (Obj_Id, E_In_Parameter);
3487 Set_Etype (Obj_Id, Obj_Typ);
3488 Set_Scope (Obj_Id, Proc_Id);
3490 Set_First_Entity (Proc_Id, Obj_Id);
3491 Set_Last_Entity (Proc_Id, Obj_Id);
3493 -- Generate:
3494 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3496 Proc_Decl :=
3497 Make_Subprogram_Declaration (Loc,
3498 Specification =>
3499 Make_Procedure_Specification (Loc,
3500 Defining_Unit_Name => Proc_Id,
3501 Parameter_Specifications => New_List (
3502 Make_Parameter_Specification (Loc,
3503 Defining_Identifier => Obj_Id,
3504 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3506 -- The declaration should not be inserted into the tree when the context
3507 -- is ASIS or a generic unit because it is not part of the template.
3509 if ASIS_Mode or Inside_A_Generic then
3510 null;
3512 -- Semi-insert the declaration into the tree for GNATprove by setting
3513 -- its Parent field. This allows for proper upstream tree traversals.
3515 elsif GNATprove_Mode then
3516 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3518 -- Otherwise insert the declaration
3520 else
3521 pragma Assert (Present (Typ_Decl));
3522 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3523 end if;
3525 <<Leave>>
3526 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3527 end Build_Invariant_Procedure_Declaration;
3529 --------------------------
3530 -- Build_Procedure_Form --
3531 --------------------------
3533 procedure Build_Procedure_Form (N : Node_Id) is
3534 Loc : constant Source_Ptr := Sloc (N);
3535 Subp : constant Entity_Id := Defining_Entity (N);
3537 Func_Formal : Entity_Id;
3538 Proc_Formals : List_Id;
3539 Proc_Decl : Node_Id;
3541 begin
3542 -- No action needed if this transformation was already done, or in case
3543 -- of subprogram renaming declarations.
3545 if Nkind (Specification (N)) = N_Procedure_Specification
3546 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3547 then
3548 return;
3549 end if;
3551 -- Ditto when dealing with an expression function, where both the
3552 -- original expression and the generated declaration end up being
3553 -- expanded here.
3555 if Rewritten_For_C (Subp) then
3556 return;
3557 end if;
3559 Proc_Formals := New_List;
3561 -- Create a list of formal parameters with the same types as the
3562 -- function.
3564 Func_Formal := First_Formal (Subp);
3565 while Present (Func_Formal) loop
3566 Append_To (Proc_Formals,
3567 Make_Parameter_Specification (Loc,
3568 Defining_Identifier =>
3569 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3570 Parameter_Type =>
3571 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3573 Next_Formal (Func_Formal);
3574 end loop;
3576 -- Add an extra out parameter to carry the function result
3578 Name_Len := 6;
3579 Name_Buffer (1 .. Name_Len) := "RESULT";
3580 Append_To (Proc_Formals,
3581 Make_Parameter_Specification (Loc,
3582 Defining_Identifier =>
3583 Make_Defining_Identifier (Loc, Chars => Name_Find),
3584 Out_Present => True,
3585 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3587 -- The new procedure declaration is inserted immediately after the
3588 -- function declaration. The processing in Build_Procedure_Body_Form
3589 -- relies on this order.
3591 Proc_Decl :=
3592 Make_Subprogram_Declaration (Loc,
3593 Specification =>
3594 Make_Procedure_Specification (Loc,
3595 Defining_Unit_Name =>
3596 Make_Defining_Identifier (Loc, Chars (Subp)),
3597 Parameter_Specifications => Proc_Formals));
3599 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3601 -- Entity of procedure must remain invisible so that it does not
3602 -- overload subsequent references to the original function.
3604 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3606 -- Mark the function as having a procedure form and link the function
3607 -- and its internally built procedure.
3609 Set_Rewritten_For_C (Subp);
3610 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3611 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3612 end Build_Procedure_Form;
3614 ------------------------
3615 -- Build_Runtime_Call --
3616 ------------------------
3618 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3619 begin
3620 -- If entity is not available, we can skip making the call (this avoids
3621 -- junk duplicated error messages in a number of cases).
3623 if not RTE_Available (RE) then
3624 return Make_Null_Statement (Loc);
3625 else
3626 return
3627 Make_Procedure_Call_Statement (Loc,
3628 Name => New_Occurrence_Of (RTE (RE), Loc));
3629 end if;
3630 end Build_Runtime_Call;
3632 ------------------------
3633 -- Build_SS_Mark_Call --
3634 ------------------------
3636 function Build_SS_Mark_Call
3637 (Loc : Source_Ptr;
3638 Mark : Entity_Id) return Node_Id
3640 begin
3641 -- Generate:
3642 -- Mark : constant Mark_Id := SS_Mark;
3644 return
3645 Make_Object_Declaration (Loc,
3646 Defining_Identifier => Mark,
3647 Constant_Present => True,
3648 Object_Definition =>
3649 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3650 Expression =>
3651 Make_Function_Call (Loc,
3652 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3653 end Build_SS_Mark_Call;
3655 ---------------------------
3656 -- Build_SS_Release_Call --
3657 ---------------------------
3659 function Build_SS_Release_Call
3660 (Loc : Source_Ptr;
3661 Mark : Entity_Id) return Node_Id
3663 begin
3664 -- Generate:
3665 -- SS_Release (Mark);
3667 return
3668 Make_Procedure_Call_Statement (Loc,
3669 Name =>
3670 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3671 Parameter_Associations => New_List (
3672 New_Occurrence_Of (Mark, Loc)));
3673 end Build_SS_Release_Call;
3675 ----------------------------
3676 -- Build_Task_Array_Image --
3677 ----------------------------
3679 -- This function generates the body for a function that constructs the
3680 -- image string for a task that is an array component. The function is
3681 -- local to the init proc for the array type, and is called for each one
3682 -- of the components. The constructed image has the form of an indexed
3683 -- component, whose prefix is the outer variable of the array type.
3684 -- The n-dimensional array type has known indexes Index, Index2...
3686 -- Id_Ref is an indexed component form created by the enclosing init proc.
3687 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3688 -- in the loops that call the individual task init proc on each component.
3690 -- The generated function has the following structure:
3692 -- function F return String is
3693 -- Pref : string renames Task_Name;
3694 -- T1 : String := Index1'Image (Val1);
3695 -- ...
3696 -- Tn : String := indexn'image (Valn);
3697 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3698 -- -- Len includes commas and the end parentheses.
3699 -- Res : String (1..Len);
3700 -- Pos : Integer := Pref'Length;
3702 -- begin
3703 -- Res (1 .. Pos) := Pref;
3704 -- Pos := Pos + 1;
3705 -- Res (Pos) := '(';
3706 -- Pos := Pos + 1;
3707 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3708 -- Pos := Pos + T1'Length;
3709 -- Res (Pos) := '.';
3710 -- Pos := Pos + 1;
3711 -- ...
3712 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3713 -- Res (Len) := ')';
3715 -- return Res;
3716 -- end F;
3718 -- Needless to say, multidimensional arrays of tasks are rare enough that
3719 -- the bulkiness of this code is not really a concern.
3721 function Build_Task_Array_Image
3722 (Loc : Source_Ptr;
3723 Id_Ref : Node_Id;
3724 A_Type : Entity_Id;
3725 Dyn : Boolean := False) return Node_Id
3727 Dims : constant Nat := Number_Dimensions (A_Type);
3728 -- Number of dimensions for array of tasks
3730 Temps : array (1 .. Dims) of Entity_Id;
3731 -- Array of temporaries to hold string for each index
3733 Indx : Node_Id;
3734 -- Index expression
3736 Len : Entity_Id;
3737 -- Total length of generated name
3739 Pos : Entity_Id;
3740 -- Running index for substring assignments
3742 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3743 -- Name of enclosing variable, prefix of resulting name
3745 Res : Entity_Id;
3746 -- String to hold result
3748 Val : Node_Id;
3749 -- Value of successive indexes
3751 Sum : Node_Id;
3752 -- Expression to compute total size of string
3754 T : Entity_Id;
3755 -- Entity for name at one index position
3757 Decls : constant List_Id := New_List;
3758 Stats : constant List_Id := New_List;
3760 begin
3761 -- For a dynamic task, the name comes from the target variable. For a
3762 -- static one it is a formal of the enclosing init proc.
3764 if Dyn then
3765 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3766 Append_To (Decls,
3767 Make_Object_Declaration (Loc,
3768 Defining_Identifier => Pref,
3769 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3770 Expression =>
3771 Make_String_Literal (Loc,
3772 Strval => String_From_Name_Buffer)));
3774 else
3775 Append_To (Decls,
3776 Make_Object_Renaming_Declaration (Loc,
3777 Defining_Identifier => Pref,
3778 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3779 Name => Make_Identifier (Loc, Name_uTask_Name)));
3780 end if;
3782 Indx := First_Index (A_Type);
3783 Val := First (Expressions (Id_Ref));
3785 for J in 1 .. Dims loop
3786 T := Make_Temporary (Loc, 'T');
3787 Temps (J) := T;
3789 Append_To (Decls,
3790 Make_Object_Declaration (Loc,
3791 Defining_Identifier => T,
3792 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3793 Expression =>
3794 Make_Attribute_Reference (Loc,
3795 Attribute_Name => Name_Image,
3796 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3797 Expressions => New_List (New_Copy_Tree (Val)))));
3799 Next_Index (Indx);
3800 Next (Val);
3801 end loop;
3803 Sum := Make_Integer_Literal (Loc, Dims + 1);
3805 Sum :=
3806 Make_Op_Add (Loc,
3807 Left_Opnd => Sum,
3808 Right_Opnd =>
3809 Make_Attribute_Reference (Loc,
3810 Attribute_Name => Name_Length,
3811 Prefix => New_Occurrence_Of (Pref, Loc),
3812 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3814 for J in 1 .. Dims loop
3815 Sum :=
3816 Make_Op_Add (Loc,
3817 Left_Opnd => Sum,
3818 Right_Opnd =>
3819 Make_Attribute_Reference (Loc,
3820 Attribute_Name => Name_Length,
3821 Prefix =>
3822 New_Occurrence_Of (Temps (J), Loc),
3823 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3824 end loop;
3826 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3828 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3830 Append_To (Stats,
3831 Make_Assignment_Statement (Loc,
3832 Name =>
3833 Make_Indexed_Component (Loc,
3834 Prefix => New_Occurrence_Of (Res, Loc),
3835 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3836 Expression =>
3837 Make_Character_Literal (Loc,
3838 Chars => Name_Find,
3839 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3841 Append_To (Stats,
3842 Make_Assignment_Statement (Loc,
3843 Name => New_Occurrence_Of (Pos, Loc),
3844 Expression =>
3845 Make_Op_Add (Loc,
3846 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3847 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3849 for J in 1 .. Dims loop
3851 Append_To (Stats,
3852 Make_Assignment_Statement (Loc,
3853 Name =>
3854 Make_Slice (Loc,
3855 Prefix => New_Occurrence_Of (Res, Loc),
3856 Discrete_Range =>
3857 Make_Range (Loc,
3858 Low_Bound => New_Occurrence_Of (Pos, Loc),
3859 High_Bound =>
3860 Make_Op_Subtract (Loc,
3861 Left_Opnd =>
3862 Make_Op_Add (Loc,
3863 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3864 Right_Opnd =>
3865 Make_Attribute_Reference (Loc,
3866 Attribute_Name => Name_Length,
3867 Prefix =>
3868 New_Occurrence_Of (Temps (J), Loc),
3869 Expressions =>
3870 New_List (Make_Integer_Literal (Loc, 1)))),
3871 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3873 Expression => New_Occurrence_Of (Temps (J), Loc)));
3875 if J < Dims then
3876 Append_To (Stats,
3877 Make_Assignment_Statement (Loc,
3878 Name => New_Occurrence_Of (Pos, Loc),
3879 Expression =>
3880 Make_Op_Add (Loc,
3881 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3882 Right_Opnd =>
3883 Make_Attribute_Reference (Loc,
3884 Attribute_Name => Name_Length,
3885 Prefix => New_Occurrence_Of (Temps (J), Loc),
3886 Expressions =>
3887 New_List (Make_Integer_Literal (Loc, 1))))));
3889 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3891 Append_To (Stats,
3892 Make_Assignment_Statement (Loc,
3893 Name => Make_Indexed_Component (Loc,
3894 Prefix => New_Occurrence_Of (Res, Loc),
3895 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3896 Expression =>
3897 Make_Character_Literal (Loc,
3898 Chars => Name_Find,
3899 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3901 Append_To (Stats,
3902 Make_Assignment_Statement (Loc,
3903 Name => New_Occurrence_Of (Pos, Loc),
3904 Expression =>
3905 Make_Op_Add (Loc,
3906 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3907 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3908 end if;
3909 end loop;
3911 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3913 Append_To (Stats,
3914 Make_Assignment_Statement (Loc,
3915 Name =>
3916 Make_Indexed_Component (Loc,
3917 Prefix => New_Occurrence_Of (Res, Loc),
3918 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3919 Expression =>
3920 Make_Character_Literal (Loc,
3921 Chars => Name_Find,
3922 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3923 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3924 end Build_Task_Array_Image;
3926 ----------------------------
3927 -- Build_Task_Image_Decls --
3928 ----------------------------
3930 function Build_Task_Image_Decls
3931 (Loc : Source_Ptr;
3932 Id_Ref : Node_Id;
3933 A_Type : Entity_Id;
3934 In_Init_Proc : Boolean := False) return List_Id
3936 Decls : constant List_Id := New_List;
3937 T_Id : Entity_Id := Empty;
3938 Decl : Node_Id;
3939 Expr : Node_Id := Empty;
3940 Fun : Node_Id := Empty;
3941 Is_Dyn : constant Boolean :=
3942 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3943 and then
3944 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3946 begin
3947 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3948 -- generate a dummy declaration only.
3950 if Restriction_Active (No_Implicit_Heap_Allocations)
3951 or else Global_Discard_Names
3952 then
3953 T_Id := Make_Temporary (Loc, 'J');
3954 Name_Len := 0;
3956 return
3957 New_List (
3958 Make_Object_Declaration (Loc,
3959 Defining_Identifier => T_Id,
3960 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3961 Expression =>
3962 Make_String_Literal (Loc,
3963 Strval => String_From_Name_Buffer)));
3965 else
3966 if Nkind (Id_Ref) = N_Identifier
3967 or else Nkind (Id_Ref) = N_Defining_Identifier
3968 then
3969 -- For a simple variable, the image of the task is built from
3970 -- the name of the variable. To avoid possible conflict with the
3971 -- anonymous type created for a single protected object, add a
3972 -- numeric suffix.
3974 T_Id :=
3975 Make_Defining_Identifier (Loc,
3976 New_External_Name (Chars (Id_Ref), 'T', 1));
3978 Get_Name_String (Chars (Id_Ref));
3980 Expr :=
3981 Make_String_Literal (Loc,
3982 Strval => String_From_Name_Buffer);
3984 elsif Nkind (Id_Ref) = N_Selected_Component then
3985 T_Id :=
3986 Make_Defining_Identifier (Loc,
3987 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3988 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3990 elsif Nkind (Id_Ref) = N_Indexed_Component then
3991 T_Id :=
3992 Make_Defining_Identifier (Loc,
3993 New_External_Name (Chars (A_Type), 'N'));
3995 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3996 end if;
3997 end if;
3999 if Present (Fun) then
4000 Append (Fun, Decls);
4001 Expr := Make_Function_Call (Loc,
4002 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
4004 if not In_Init_Proc then
4005 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4006 end if;
4007 end if;
4009 Decl := Make_Object_Declaration (Loc,
4010 Defining_Identifier => T_Id,
4011 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4012 Constant_Present => True,
4013 Expression => Expr);
4015 Append (Decl, Decls);
4016 return Decls;
4017 end Build_Task_Image_Decls;
4019 -------------------------------
4020 -- Build_Task_Image_Function --
4021 -------------------------------
4023 function Build_Task_Image_Function
4024 (Loc : Source_Ptr;
4025 Decls : List_Id;
4026 Stats : List_Id;
4027 Res : Entity_Id) return Node_Id
4029 Spec : Node_Id;
4031 begin
4032 Append_To (Stats,
4033 Make_Simple_Return_Statement (Loc,
4034 Expression => New_Occurrence_Of (Res, Loc)));
4036 Spec := Make_Function_Specification (Loc,
4037 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4038 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4040 -- Calls to 'Image use the secondary stack, which must be cleaned up
4041 -- after the task name is built.
4043 return Make_Subprogram_Body (Loc,
4044 Specification => Spec,
4045 Declarations => Decls,
4046 Handled_Statement_Sequence =>
4047 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4048 end Build_Task_Image_Function;
4050 -----------------------------
4051 -- Build_Task_Image_Prefix --
4052 -----------------------------
4054 procedure Build_Task_Image_Prefix
4055 (Loc : Source_Ptr;
4056 Len : out Entity_Id;
4057 Res : out Entity_Id;
4058 Pos : out Entity_Id;
4059 Prefix : Entity_Id;
4060 Sum : Node_Id;
4061 Decls : List_Id;
4062 Stats : List_Id)
4064 begin
4065 Len := Make_Temporary (Loc, 'L', Sum);
4067 Append_To (Decls,
4068 Make_Object_Declaration (Loc,
4069 Defining_Identifier => Len,
4070 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4071 Expression => Sum));
4073 Res := Make_Temporary (Loc, 'R');
4075 Append_To (Decls,
4076 Make_Object_Declaration (Loc,
4077 Defining_Identifier => Res,
4078 Object_Definition =>
4079 Make_Subtype_Indication (Loc,
4080 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4081 Constraint =>
4082 Make_Index_Or_Discriminant_Constraint (Loc,
4083 Constraints =>
4084 New_List (
4085 Make_Range (Loc,
4086 Low_Bound => Make_Integer_Literal (Loc, 1),
4087 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4089 -- Indicate that the result is an internal temporary, so it does not
4090 -- receive a bogus initialization when declaration is expanded. This
4091 -- is both efficient, and prevents anomalies in the handling of
4092 -- dynamic objects on the secondary stack.
4094 Set_Is_Internal (Res);
4095 Pos := Make_Temporary (Loc, 'P');
4097 Append_To (Decls,
4098 Make_Object_Declaration (Loc,
4099 Defining_Identifier => Pos,
4100 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4102 -- Pos := Prefix'Length;
4104 Append_To (Stats,
4105 Make_Assignment_Statement (Loc,
4106 Name => New_Occurrence_Of (Pos, Loc),
4107 Expression =>
4108 Make_Attribute_Reference (Loc,
4109 Attribute_Name => Name_Length,
4110 Prefix => New_Occurrence_Of (Prefix, Loc),
4111 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4113 -- Res (1 .. Pos) := Prefix;
4115 Append_To (Stats,
4116 Make_Assignment_Statement (Loc,
4117 Name =>
4118 Make_Slice (Loc,
4119 Prefix => New_Occurrence_Of (Res, Loc),
4120 Discrete_Range =>
4121 Make_Range (Loc,
4122 Low_Bound => Make_Integer_Literal (Loc, 1),
4123 High_Bound => New_Occurrence_Of (Pos, Loc))),
4125 Expression => New_Occurrence_Of (Prefix, Loc)));
4127 Append_To (Stats,
4128 Make_Assignment_Statement (Loc,
4129 Name => New_Occurrence_Of (Pos, Loc),
4130 Expression =>
4131 Make_Op_Add (Loc,
4132 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4133 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4134 end Build_Task_Image_Prefix;
4136 -----------------------------
4137 -- Build_Task_Record_Image --
4138 -----------------------------
4140 function Build_Task_Record_Image
4141 (Loc : Source_Ptr;
4142 Id_Ref : Node_Id;
4143 Dyn : Boolean := False) return Node_Id
4145 Len : Entity_Id;
4146 -- Total length of generated name
4148 Pos : Entity_Id;
4149 -- Index into result
4151 Res : Entity_Id;
4152 -- String to hold result
4154 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4155 -- Name of enclosing variable, prefix of resulting name
4157 Sum : Node_Id;
4158 -- Expression to compute total size of string
4160 Sel : Entity_Id;
4161 -- Entity for selector name
4163 Decls : constant List_Id := New_List;
4164 Stats : constant List_Id := New_List;
4166 begin
4167 -- For a dynamic task, the name comes from the target variable. For a
4168 -- static one it is a formal of the enclosing init proc.
4170 if Dyn then
4171 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4172 Append_To (Decls,
4173 Make_Object_Declaration (Loc,
4174 Defining_Identifier => Pref,
4175 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4176 Expression =>
4177 Make_String_Literal (Loc,
4178 Strval => String_From_Name_Buffer)));
4180 else
4181 Append_To (Decls,
4182 Make_Object_Renaming_Declaration (Loc,
4183 Defining_Identifier => Pref,
4184 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4185 Name => Make_Identifier (Loc, Name_uTask_Name)));
4186 end if;
4188 Sel := Make_Temporary (Loc, 'S');
4190 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4192 Append_To (Decls,
4193 Make_Object_Declaration (Loc,
4194 Defining_Identifier => Sel,
4195 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4196 Expression =>
4197 Make_String_Literal (Loc,
4198 Strval => String_From_Name_Buffer)));
4200 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4202 Sum :=
4203 Make_Op_Add (Loc,
4204 Left_Opnd => Sum,
4205 Right_Opnd =>
4206 Make_Attribute_Reference (Loc,
4207 Attribute_Name => Name_Length,
4208 Prefix =>
4209 New_Occurrence_Of (Pref, Loc),
4210 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4212 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4214 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4216 -- Res (Pos) := '.';
4218 Append_To (Stats,
4219 Make_Assignment_Statement (Loc,
4220 Name => Make_Indexed_Component (Loc,
4221 Prefix => New_Occurrence_Of (Res, Loc),
4222 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4223 Expression =>
4224 Make_Character_Literal (Loc,
4225 Chars => Name_Find,
4226 Char_Literal_Value =>
4227 UI_From_Int (Character'Pos ('.')))));
4229 Append_To (Stats,
4230 Make_Assignment_Statement (Loc,
4231 Name => New_Occurrence_Of (Pos, Loc),
4232 Expression =>
4233 Make_Op_Add (Loc,
4234 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4235 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4237 -- Res (Pos .. Len) := Selector;
4239 Append_To (Stats,
4240 Make_Assignment_Statement (Loc,
4241 Name => Make_Slice (Loc,
4242 Prefix => New_Occurrence_Of (Res, Loc),
4243 Discrete_Range =>
4244 Make_Range (Loc,
4245 Low_Bound => New_Occurrence_Of (Pos, Loc),
4246 High_Bound => New_Occurrence_Of (Len, Loc))),
4247 Expression => New_Occurrence_Of (Sel, Loc)));
4249 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4250 end Build_Task_Record_Image;
4252 ---------------------------------------
4253 -- Build_Transient_Object_Statements --
4254 ---------------------------------------
4256 procedure Build_Transient_Object_Statements
4257 (Obj_Decl : Node_Id;
4258 Fin_Call : out Node_Id;
4259 Hook_Assign : out Node_Id;
4260 Hook_Clear : out Node_Id;
4261 Hook_Decl : out Node_Id;
4262 Ptr_Decl : out Node_Id;
4263 Finalize_Obj : Boolean := True)
4265 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4266 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4267 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4269 Desig_Typ : Entity_Id;
4270 Hook_Expr : Node_Id;
4271 Hook_Id : Entity_Id;
4272 Obj_Ref : Node_Id;
4273 Ptr_Typ : Entity_Id;
4275 begin
4276 -- Recover the type of the object
4278 Desig_Typ := Obj_Typ;
4280 if Is_Access_Type (Desig_Typ) then
4281 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4282 end if;
4284 -- Create an access type which provides a reference to the transient
4285 -- object. Generate:
4287 -- type Ptr_Typ is access all Desig_Typ;
4289 Ptr_Typ := Make_Temporary (Loc, 'A');
4290 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4291 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4293 Ptr_Decl :=
4294 Make_Full_Type_Declaration (Loc,
4295 Defining_Identifier => Ptr_Typ,
4296 Type_Definition =>
4297 Make_Access_To_Object_Definition (Loc,
4298 All_Present => True,
4299 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4301 -- Create a temporary check which acts as a hook to the transient
4302 -- object. Generate:
4304 -- Hook : Ptr_Typ := null;
4306 Hook_Id := Make_Temporary (Loc, 'T');
4307 Set_Ekind (Hook_Id, E_Variable);
4308 Set_Etype (Hook_Id, Ptr_Typ);
4310 Hook_Decl :=
4311 Make_Object_Declaration (Loc,
4312 Defining_Identifier => Hook_Id,
4313 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4314 Expression => Make_Null (Loc));
4316 -- Mark the temporary as a hook. This signals the machinery in
4317 -- Build_Finalizer to recognize this special case.
4319 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4321 -- Hook the transient object to the temporary. Generate:
4323 -- Hook := Ptr_Typ (Obj_Id);
4324 -- <or>
4325 -- Hool := Obj_Id'Unrestricted_Access;
4327 if Is_Access_Type (Obj_Typ) then
4328 Hook_Expr :=
4329 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4330 else
4331 Hook_Expr :=
4332 Make_Attribute_Reference (Loc,
4333 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4334 Attribute_Name => Name_Unrestricted_Access);
4335 end if;
4337 Hook_Assign :=
4338 Make_Assignment_Statement (Loc,
4339 Name => New_Occurrence_Of (Hook_Id, Loc),
4340 Expression => Hook_Expr);
4342 -- Crear the hook prior to finalizing the object. Generate:
4344 -- Hook := null;
4346 Hook_Clear :=
4347 Make_Assignment_Statement (Loc,
4348 Name => New_Occurrence_Of (Hook_Id, Loc),
4349 Expression => Make_Null (Loc));
4351 -- Finalize the object. Generate:
4353 -- [Deep_]Finalize (Obj_Ref[.all]);
4355 if Finalize_Obj then
4356 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4358 if Is_Access_Type (Obj_Typ) then
4359 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4360 Set_Etype (Obj_Ref, Desig_Typ);
4361 end if;
4363 Fin_Call :=
4364 Make_Final_Call
4365 (Obj_Ref => Obj_Ref,
4366 Typ => Desig_Typ);
4368 -- Otherwise finalize the hook. Generate:
4370 -- [Deep_]Finalize (Hook.all);
4372 else
4373 Fin_Call :=
4374 Make_Final_Call (
4375 Obj_Ref =>
4376 Make_Explicit_Dereference (Loc,
4377 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4378 Typ => Desig_Typ);
4379 end if;
4380 end Build_Transient_Object_Statements;
4382 -----------------------------
4383 -- Check_Float_Op_Overflow --
4384 -----------------------------
4386 procedure Check_Float_Op_Overflow (N : Node_Id) is
4387 begin
4388 -- Return if no check needed
4390 if not Is_Floating_Point_Type (Etype (N))
4391 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4393 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4394 -- and do not expand the code for float overflow checking.
4396 or else CodePeer_Mode
4397 then
4398 return;
4399 end if;
4401 -- Otherwise we replace the expression by
4403 -- do Tnn : constant ftype := expression;
4404 -- constraint_error when not Tnn'Valid;
4405 -- in Tnn;
4407 declare
4408 Loc : constant Source_Ptr := Sloc (N);
4409 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4410 Typ : constant Entity_Id := Etype (N);
4412 begin
4413 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4414 -- right here. We also set the node as analyzed to prevent infinite
4415 -- recursion from repeating the operation in the expansion.
4417 Set_Do_Overflow_Check (N, False);
4418 Set_Analyzed (N, True);
4420 -- Do the rewrite to include the check
4422 Rewrite (N,
4423 Make_Expression_With_Actions (Loc,
4424 Actions => New_List (
4425 Make_Object_Declaration (Loc,
4426 Defining_Identifier => Tnn,
4427 Object_Definition => New_Occurrence_Of (Typ, Loc),
4428 Constant_Present => True,
4429 Expression => Relocate_Node (N)),
4430 Make_Raise_Constraint_Error (Loc,
4431 Condition =>
4432 Make_Op_Not (Loc,
4433 Right_Opnd =>
4434 Make_Attribute_Reference (Loc,
4435 Prefix => New_Occurrence_Of (Tnn, Loc),
4436 Attribute_Name => Name_Valid)),
4437 Reason => CE_Overflow_Check_Failed)),
4438 Expression => New_Occurrence_Of (Tnn, Loc)));
4440 Analyze_And_Resolve (N, Typ);
4441 end;
4442 end Check_Float_Op_Overflow;
4444 ----------------------------------
4445 -- Component_May_Be_Bit_Aligned --
4446 ----------------------------------
4448 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4449 UT : Entity_Id;
4451 begin
4452 -- If no component clause, then everything is fine, since the back end
4453 -- never bit-misaligns by default, even if there is a pragma Packed for
4454 -- the record.
4456 if No (Comp) or else No (Component_Clause (Comp)) then
4457 return False;
4458 end if;
4460 UT := Underlying_Type (Etype (Comp));
4462 -- It is only array and record types that cause trouble
4464 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4465 return False;
4467 -- If we know that we have a small (64 bits or less) record or small
4468 -- bit-packed array, then everything is fine, since the back end can
4469 -- handle these cases correctly.
4471 elsif Esize (Comp) <= 64
4472 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4473 then
4474 return False;
4476 -- Otherwise if the component is not byte aligned, we know we have the
4477 -- nasty unaligned case.
4479 elsif Normalized_First_Bit (Comp) /= Uint_0
4480 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4481 then
4482 return True;
4484 -- If we are large and byte aligned, then OK at this level
4486 else
4487 return False;
4488 end if;
4489 end Component_May_Be_Bit_Aligned;
4491 ----------------------------------------
4492 -- Containing_Package_With_Ext_Axioms --
4493 ----------------------------------------
4495 function Containing_Package_With_Ext_Axioms
4496 (E : Entity_Id) return Entity_Id
4498 begin
4499 -- E is the package or generic package which is externally axiomatized
4501 if Ekind_In (E, E_Generic_Package, E_Package)
4502 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4503 then
4504 return E;
4505 end if;
4507 -- If E's scope is axiomatized, E is axiomatized
4509 if Present (Scope (E)) then
4510 declare
4511 First_Ax_Parent_Scope : constant Entity_Id :=
4512 Containing_Package_With_Ext_Axioms (Scope (E));
4513 begin
4514 if Present (First_Ax_Parent_Scope) then
4515 return First_Ax_Parent_Scope;
4516 end if;
4517 end;
4518 end if;
4520 -- Otherwise, if E is a package instance, it is axiomatized if the
4521 -- corresponding generic package is axiomatized.
4523 if Ekind (E) = E_Package then
4524 declare
4525 Par : constant Node_Id := Parent (E);
4526 Decl : Node_Id;
4528 begin
4529 if Nkind (Par) = N_Defining_Program_Unit_Name then
4530 Decl := Parent (Par);
4531 else
4532 Decl := Par;
4533 end if;
4535 if Present (Generic_Parent (Decl)) then
4536 return
4537 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4538 end if;
4539 end;
4540 end if;
4542 return Empty;
4543 end Containing_Package_With_Ext_Axioms;
4545 -------------------------------
4546 -- Convert_To_Actual_Subtype --
4547 -------------------------------
4549 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4550 Act_ST : Entity_Id;
4552 begin
4553 Act_ST := Get_Actual_Subtype (Exp);
4555 if Act_ST = Etype (Exp) then
4556 return;
4557 else
4558 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4559 Analyze_And_Resolve (Exp, Act_ST);
4560 end if;
4561 end Convert_To_Actual_Subtype;
4563 -----------------------------------
4564 -- Corresponding_Runtime_Package --
4565 -----------------------------------
4567 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4568 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4569 -- Return True if protected type T has one entry and the maximum queue
4570 -- length is one.
4572 --------------------------------
4573 -- Has_One_Entry_And_No_Queue --
4574 --------------------------------
4576 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4577 Item : Entity_Id;
4578 Is_First : Boolean := True;
4580 begin
4581 Item := First_Entity (T);
4582 while Present (Item) loop
4583 if Is_Entry (Item) then
4585 -- The protected type has more than one entry
4587 if not Is_First then
4588 return False;
4589 end if;
4591 -- The queue length is not one
4593 if not Restriction_Active (No_Entry_Queue)
4594 and then Get_Max_Queue_Length (Item) /= Uint_1
4595 then
4596 return False;
4597 end if;
4599 Is_First := False;
4600 end if;
4602 Next_Entity (Item);
4603 end loop;
4605 return True;
4606 end Has_One_Entry_And_No_Queue;
4608 -- Local variables
4610 Pkg_Id : RTU_Id := RTU_Null;
4612 -- Start of processing for Corresponding_Runtime_Package
4614 begin
4615 pragma Assert (Is_Concurrent_Type (Typ));
4617 if Ekind (Typ) in Protected_Kind then
4618 if Has_Entries (Typ)
4620 -- A protected type without entries that covers an interface and
4621 -- overrides the abstract routines with protected procedures is
4622 -- considered equivalent to a protected type with entries in the
4623 -- context of dispatching select statements. It is sufficient to
4624 -- check for the presence of an interface list in the declaration
4625 -- node to recognize this case.
4627 or else Present (Interface_List (Parent (Typ)))
4629 -- Protected types with interrupt handlers (when not using a
4630 -- restricted profile) are also considered equivalent to
4631 -- protected types with entries. The types which are used
4632 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4633 -- are derived from Protection_Entries.
4635 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4636 or else Has_Interrupt_Handler (Typ)
4637 then
4638 if Abort_Allowed
4639 or else Restriction_Active (No_Select_Statements) = False
4640 or else not Has_One_Entry_And_No_Queue (Typ)
4641 or else (Has_Attach_Handler (Typ)
4642 and then not Restricted_Profile)
4643 then
4644 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4645 else
4646 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4647 end if;
4649 else
4650 Pkg_Id := System_Tasking_Protected_Objects;
4651 end if;
4652 end if;
4654 return Pkg_Id;
4655 end Corresponding_Runtime_Package;
4657 -----------------------------------
4658 -- Current_Sem_Unit_Declarations --
4659 -----------------------------------
4661 function Current_Sem_Unit_Declarations return List_Id is
4662 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4663 Decls : List_Id;
4665 begin
4666 -- If the current unit is a package body, locate the visible
4667 -- declarations of the package spec.
4669 if Nkind (U) = N_Package_Body then
4670 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4671 end if;
4673 if Nkind (U) = N_Package_Declaration then
4674 U := Specification (U);
4675 Decls := Visible_Declarations (U);
4677 if No (Decls) then
4678 Decls := New_List;
4679 Set_Visible_Declarations (U, Decls);
4680 end if;
4682 else
4683 Decls := Declarations (U);
4685 if No (Decls) then
4686 Decls := New_List;
4687 Set_Declarations (U, Decls);
4688 end if;
4689 end if;
4691 return Decls;
4692 end Current_Sem_Unit_Declarations;
4694 -----------------------
4695 -- Duplicate_Subexpr --
4696 -----------------------
4698 function Duplicate_Subexpr
4699 (Exp : Node_Id;
4700 Name_Req : Boolean := False;
4701 Renaming_Req : Boolean := False) return Node_Id
4703 begin
4704 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4705 return New_Copy_Tree (Exp);
4706 end Duplicate_Subexpr;
4708 ---------------------------------
4709 -- Duplicate_Subexpr_No_Checks --
4710 ---------------------------------
4712 function Duplicate_Subexpr_No_Checks
4713 (Exp : Node_Id;
4714 Name_Req : Boolean := False;
4715 Renaming_Req : Boolean := False;
4716 Related_Id : Entity_Id := Empty;
4717 Is_Low_Bound : Boolean := False;
4718 Is_High_Bound : Boolean := False) return Node_Id
4720 New_Exp : Node_Id;
4722 begin
4723 Remove_Side_Effects
4724 (Exp => Exp,
4725 Name_Req => Name_Req,
4726 Renaming_Req => Renaming_Req,
4727 Related_Id => Related_Id,
4728 Is_Low_Bound => Is_Low_Bound,
4729 Is_High_Bound => Is_High_Bound);
4731 New_Exp := New_Copy_Tree (Exp);
4732 Remove_Checks (New_Exp);
4733 return New_Exp;
4734 end Duplicate_Subexpr_No_Checks;
4736 -----------------------------------
4737 -- Duplicate_Subexpr_Move_Checks --
4738 -----------------------------------
4740 function Duplicate_Subexpr_Move_Checks
4741 (Exp : Node_Id;
4742 Name_Req : Boolean := False;
4743 Renaming_Req : Boolean := False) return Node_Id
4745 New_Exp : Node_Id;
4747 begin
4748 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4749 New_Exp := New_Copy_Tree (Exp);
4750 Remove_Checks (Exp);
4751 return New_Exp;
4752 end Duplicate_Subexpr_Move_Checks;
4754 --------------------
4755 -- Ensure_Defined --
4756 --------------------
4758 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4759 IR : Node_Id;
4761 begin
4762 -- An itype reference must only be created if this is a local itype, so
4763 -- that gigi can elaborate it on the proper objstack.
4765 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4766 IR := Make_Itype_Reference (Sloc (N));
4767 Set_Itype (IR, Typ);
4768 Insert_Action (N, IR);
4769 end if;
4770 end Ensure_Defined;
4772 --------------------
4773 -- Entry_Names_OK --
4774 --------------------
4776 function Entry_Names_OK return Boolean is
4777 begin
4778 return
4779 not Restricted_Profile
4780 and then not Global_Discard_Names
4781 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4782 and then not Restriction_Active (No_Local_Allocators);
4783 end Entry_Names_OK;
4785 -------------------
4786 -- Evaluate_Name --
4787 -------------------
4789 procedure Evaluate_Name (Nam : Node_Id) is
4790 begin
4791 -- For an attribute reference or an indexed component, evaluate the
4792 -- prefix, which is itself a name, recursively, and then force the
4793 -- evaluation of all the subscripts (or attribute expressions).
4795 case Nkind (Nam) is
4796 when N_Attribute_Reference
4797 | N_Indexed_Component
4799 Evaluate_Name (Prefix (Nam));
4801 declare
4802 E : Node_Id;
4804 begin
4805 E := First (Expressions (Nam));
4806 while Present (E) loop
4807 Force_Evaluation (E);
4809 if Original_Node (E) /= E then
4810 Set_Do_Range_Check
4811 (E, Do_Range_Check (Original_Node (E)));
4812 end if;
4814 Next (E);
4815 end loop;
4816 end;
4818 -- For an explicit dereference, we simply force the evaluation of
4819 -- the name expression. The dereference provides a value that is the
4820 -- address for the renamed object, and it is precisely this value
4821 -- that we want to preserve.
4823 when N_Explicit_Dereference =>
4824 Force_Evaluation (Prefix (Nam));
4826 -- For a function call, we evaluate the call
4828 when N_Function_Call =>
4829 Force_Evaluation (Nam);
4831 -- For a qualified expression, we evaluate the underlying object
4832 -- name if any, otherwise we force the evaluation of the underlying
4833 -- expression.
4835 when N_Qualified_Expression =>
4836 if Is_Object_Reference (Expression (Nam)) then
4837 Evaluate_Name (Expression (Nam));
4838 else
4839 Force_Evaluation (Expression (Nam));
4840 end if;
4842 -- For a selected component, we simply evaluate the prefix
4844 when N_Selected_Component =>
4845 Evaluate_Name (Prefix (Nam));
4847 -- For a slice, we evaluate the prefix, as for the indexed component
4848 -- case and then, if there is a range present, either directly or as
4849 -- the constraint of a discrete subtype indication, we evaluate the
4850 -- two bounds of this range.
4852 when N_Slice =>
4853 Evaluate_Name (Prefix (Nam));
4854 Evaluate_Slice_Bounds (Nam);
4856 -- For a type conversion, the expression of the conversion must be
4857 -- the name of an object, and we simply need to evaluate this name.
4859 when N_Type_Conversion =>
4860 Evaluate_Name (Expression (Nam));
4862 -- The remaining cases are direct name, operator symbol and character
4863 -- literal. In all these cases, we do nothing, since we want to
4864 -- reevaluate each time the renamed object is used.
4866 when others =>
4867 null;
4868 end case;
4869 end Evaluate_Name;
4871 ---------------------------
4872 -- Evaluate_Slice_Bounds --
4873 ---------------------------
4875 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4876 DR : constant Node_Id := Discrete_Range (Slice);
4877 Constr : Node_Id;
4878 Rexpr : Node_Id;
4880 begin
4881 if Nkind (DR) = N_Range then
4882 Force_Evaluation (Low_Bound (DR));
4883 Force_Evaluation (High_Bound (DR));
4885 elsif Nkind (DR) = N_Subtype_Indication then
4886 Constr := Constraint (DR);
4888 if Nkind (Constr) = N_Range_Constraint then
4889 Rexpr := Range_Expression (Constr);
4891 Force_Evaluation (Low_Bound (Rexpr));
4892 Force_Evaluation (High_Bound (Rexpr));
4893 end if;
4894 end if;
4895 end Evaluate_Slice_Bounds;
4897 ---------------------
4898 -- Evolve_And_Then --
4899 ---------------------
4901 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4902 begin
4903 if No (Cond) then
4904 Cond := Cond1;
4905 else
4906 Cond :=
4907 Make_And_Then (Sloc (Cond1),
4908 Left_Opnd => Cond,
4909 Right_Opnd => Cond1);
4910 end if;
4911 end Evolve_And_Then;
4913 --------------------
4914 -- Evolve_Or_Else --
4915 --------------------
4917 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4918 begin
4919 if No (Cond) then
4920 Cond := Cond1;
4921 else
4922 Cond :=
4923 Make_Or_Else (Sloc (Cond1),
4924 Left_Opnd => Cond,
4925 Right_Opnd => Cond1);
4926 end if;
4927 end Evolve_Or_Else;
4929 -----------------------------------
4930 -- Exceptions_In_Finalization_OK --
4931 -----------------------------------
4933 function Exceptions_In_Finalization_OK return Boolean is
4934 begin
4935 return
4936 not (Restriction_Active (No_Exception_Handlers) or else
4937 Restriction_Active (No_Exception_Propagation) or else
4938 Restriction_Active (No_Exceptions));
4939 end Exceptions_In_Finalization_OK;
4941 -----------------------------------------
4942 -- Expand_Static_Predicates_In_Choices --
4943 -----------------------------------------
4945 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4946 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4948 Choices : constant List_Id := Discrete_Choices (N);
4950 Choice : Node_Id;
4951 Next_C : Node_Id;
4952 P : Node_Id;
4953 C : Node_Id;
4955 begin
4956 Choice := First (Choices);
4957 while Present (Choice) loop
4958 Next_C := Next (Choice);
4960 -- Check for name of subtype with static predicate
4962 if Is_Entity_Name (Choice)
4963 and then Is_Type (Entity (Choice))
4964 and then Has_Predicates (Entity (Choice))
4965 then
4966 -- Loop through entries in predicate list, converting to choices
4967 -- and inserting in the list before the current choice. Note that
4968 -- if the list is empty, corresponding to a False predicate, then
4969 -- no choices are inserted.
4971 P := First (Static_Discrete_Predicate (Entity (Choice)));
4972 while Present (P) loop
4974 -- If low bound and high bounds are equal, copy simple choice
4976 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4977 C := New_Copy (Low_Bound (P));
4979 -- Otherwise copy a range
4981 else
4982 C := New_Copy (P);
4983 end if;
4985 -- Change Sloc to referencing choice (rather than the Sloc of
4986 -- the predicate declaration element itself).
4988 Set_Sloc (C, Sloc (Choice));
4989 Insert_Before (Choice, C);
4990 Next (P);
4991 end loop;
4993 -- Delete the predicated entry
4995 Remove (Choice);
4996 end if;
4998 -- Move to next choice to check
5000 Choice := Next_C;
5001 end loop;
5003 Set_Has_SP_Choice (N, False);
5004 end Expand_Static_Predicates_In_Choices;
5006 ------------------------------
5007 -- Expand_Subtype_From_Expr --
5008 ------------------------------
5010 -- This function is applicable for both static and dynamic allocation of
5011 -- objects which are constrained by an initial expression. Basically it
5012 -- transforms an unconstrained subtype indication into a constrained one.
5014 -- The expression may also be transformed in certain cases in order to
5015 -- avoid multiple evaluation. In the static allocation case, the general
5016 -- scheme is:
5018 -- Val : T := Expr;
5020 -- is transformed into
5022 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5024 -- Here are the main cases :
5026 -- <if Expr is a Slice>
5027 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5029 -- <elsif Expr is a String Literal>
5030 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5032 -- <elsif Expr is Constrained>
5033 -- subtype T is Type_Of_Expr
5034 -- Val : T := Expr;
5036 -- <elsif Expr is an entity_name>
5037 -- Val : T (constraints taken from Expr) := Expr;
5039 -- <else>
5040 -- type Axxx is access all T;
5041 -- Rval : Axxx := Expr'ref;
5042 -- Val : T (constraints taken from Rval) := Rval.all;
5044 -- ??? note: when the Expression is allocated in the secondary stack
5045 -- we could use it directly instead of copying it by declaring
5046 -- Val : T (...) renames Rval.all
5048 procedure Expand_Subtype_From_Expr
5049 (N : Node_Id;
5050 Unc_Type : Entity_Id;
5051 Subtype_Indic : Node_Id;
5052 Exp : Node_Id;
5053 Related_Id : Entity_Id := Empty)
5055 Loc : constant Source_Ptr := Sloc (N);
5056 Exp_Typ : constant Entity_Id := Etype (Exp);
5057 T : Entity_Id;
5059 begin
5060 -- In general we cannot build the subtype if expansion is disabled,
5061 -- because internal entities may not have been defined. However, to
5062 -- avoid some cascaded errors, we try to continue when the expression is
5063 -- an array (or string), because it is safe to compute the bounds. It is
5064 -- in fact required to do so even in a generic context, because there
5065 -- may be constants that depend on the bounds of a string literal, both
5066 -- standard string types and more generally arrays of characters.
5068 -- In GNATprove mode, these extra subtypes are not needed
5070 if GNATprove_Mode then
5071 return;
5072 end if;
5074 if not Expander_Active
5075 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5076 then
5077 return;
5078 end if;
5080 if Nkind (Exp) = N_Slice then
5081 declare
5082 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5084 begin
5085 Rewrite (Subtype_Indic,
5086 Make_Subtype_Indication (Loc,
5087 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5088 Constraint =>
5089 Make_Index_Or_Discriminant_Constraint (Loc,
5090 Constraints => New_List
5091 (New_Occurrence_Of (Slice_Type, Loc)))));
5093 -- This subtype indication may be used later for constraint checks
5094 -- we better make sure that if a variable was used as a bound of
5095 -- of the original slice, its value is frozen.
5097 Evaluate_Slice_Bounds (Exp);
5098 end;
5100 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5101 Rewrite (Subtype_Indic,
5102 Make_Subtype_Indication (Loc,
5103 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5104 Constraint =>
5105 Make_Index_Or_Discriminant_Constraint (Loc,
5106 Constraints => New_List (
5107 Make_Literal_Range (Loc,
5108 Literal_Typ => Exp_Typ)))));
5110 -- If the type of the expression is an internally generated type it
5111 -- may not be necessary to create a new subtype. However there are two
5112 -- exceptions: references to the current instances, and aliased array
5113 -- object declarations for which the back end has to create a template.
5115 elsif Is_Constrained (Exp_Typ)
5116 and then not Is_Class_Wide_Type (Unc_Type)
5117 and then
5118 (Nkind (N) /= N_Object_Declaration
5119 or else not Is_Entity_Name (Expression (N))
5120 or else not Comes_From_Source (Entity (Expression (N)))
5121 or else not Is_Array_Type (Exp_Typ)
5122 or else not Aliased_Present (N))
5123 then
5124 if Is_Itype (Exp_Typ) then
5126 -- Within an initialization procedure, a selected component
5127 -- denotes a component of the enclosing record, and it appears as
5128 -- an actual in a call to its own initialization procedure. If
5129 -- this component depends on the outer discriminant, we must
5130 -- generate the proper actual subtype for it.
5132 if Nkind (Exp) = N_Selected_Component
5133 and then Within_Init_Proc
5134 then
5135 declare
5136 Decl : constant Node_Id :=
5137 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5138 begin
5139 if Present (Decl) then
5140 Insert_Action (N, Decl);
5141 T := Defining_Identifier (Decl);
5142 else
5143 T := Exp_Typ;
5144 end if;
5145 end;
5147 -- No need to generate a new subtype
5149 else
5150 T := Exp_Typ;
5151 end if;
5153 else
5154 T := Make_Temporary (Loc, 'T');
5156 Insert_Action (N,
5157 Make_Subtype_Declaration (Loc,
5158 Defining_Identifier => T,
5159 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5161 -- This type is marked as an itype even though it has an explicit
5162 -- declaration since otherwise Is_Generic_Actual_Type can get
5163 -- set, resulting in the generation of spurious errors. (See
5164 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5166 Set_Is_Itype (T);
5167 Set_Associated_Node_For_Itype (T, Exp);
5168 end if;
5170 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5172 -- Nothing needs to be done for private types with unknown discriminants
5173 -- if the underlying type is not an unconstrained composite type or it
5174 -- is an unchecked union.
5176 elsif Is_Private_Type (Unc_Type)
5177 and then Has_Unknown_Discriminants (Unc_Type)
5178 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5179 or else Is_Constrained (Underlying_Type (Unc_Type))
5180 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5181 then
5182 null;
5184 -- Case of derived type with unknown discriminants where the parent type
5185 -- also has unknown discriminants.
5187 elsif Is_Record_Type (Unc_Type)
5188 and then not Is_Class_Wide_Type (Unc_Type)
5189 and then Has_Unknown_Discriminants (Unc_Type)
5190 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5191 then
5192 -- Nothing to be done if no underlying record view available
5194 -- If this is a limited type derived from a type with unknown
5195 -- discriminants, do not expand either, so that subsequent expansion
5196 -- of the call can add build-in-place parameters to call.
5198 if No (Underlying_Record_View (Unc_Type))
5199 or else Is_Limited_Type (Unc_Type)
5200 then
5201 null;
5203 -- Otherwise use the Underlying_Record_View to create the proper
5204 -- constrained subtype for an object of a derived type with unknown
5205 -- discriminants.
5207 else
5208 Remove_Side_Effects (Exp);
5209 Rewrite (Subtype_Indic,
5210 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5211 end if;
5213 -- Renamings of class-wide interface types require no equivalent
5214 -- constrained type declarations because we only need to reference
5215 -- the tag component associated with the interface. The same is
5216 -- presumably true for class-wide types in general, so this test
5217 -- is broadened to include all class-wide renamings, which also
5218 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5219 -- (Is this really correct, or are there some cases of class-wide
5220 -- renamings that require action in this procedure???)
5222 elsif Present (N)
5223 and then Nkind (N) = N_Object_Renaming_Declaration
5224 and then Is_Class_Wide_Type (Unc_Type)
5225 then
5226 null;
5228 -- In Ada 95 nothing to be done if the type of the expression is limited
5229 -- because in this case the expression cannot be copied, and its use can
5230 -- only be by reference.
5232 -- In Ada 2005 the context can be an object declaration whose expression
5233 -- is a function that returns in place. If the nominal subtype has
5234 -- unknown discriminants, the call still provides constraints on the
5235 -- object, and we have to create an actual subtype from it.
5237 -- If the type is class-wide, the expression is dynamically tagged and
5238 -- we do not create an actual subtype either. Ditto for an interface.
5239 -- For now this applies only if the type is immutably limited, and the
5240 -- function being called is build-in-place. This will have to be revised
5241 -- when build-in-place functions are generalized to other types.
5243 elsif Is_Limited_View (Exp_Typ)
5244 and then
5245 (Is_Class_Wide_Type (Exp_Typ)
5246 or else Is_Interface (Exp_Typ)
5247 or else not Has_Unknown_Discriminants (Exp_Typ)
5248 or else not Is_Composite_Type (Unc_Type))
5249 then
5250 null;
5252 -- For limited objects initialized with build in place function calls,
5253 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5254 -- node in the expression initializing the object, which breaks the
5255 -- circuitry that detects and adds the additional arguments to the
5256 -- called function.
5258 elsif Is_Build_In_Place_Function_Call (Exp) then
5259 null;
5261 else
5262 Remove_Side_Effects (Exp);
5263 Rewrite (Subtype_Indic,
5264 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5265 end if;
5266 end Expand_Subtype_From_Expr;
5268 ---------------------------------------------
5269 -- Expression_Contains_Primitives_Calls_Of --
5270 ---------------------------------------------
5272 function Expression_Contains_Primitives_Calls_Of
5273 (Expr : Node_Id;
5274 Typ : Entity_Id) return Boolean
5276 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5278 Calls_OK : Boolean := False;
5279 -- This flag is set to True when expression Expr contains at least one
5280 -- call to a nondispatching primitive function of Typ.
5282 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5283 -- Search for nondispatching calls to primitive functions of type Typ
5285 ----------------------------
5286 -- Search_Primitive_Calls --
5287 ----------------------------
5289 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5290 Disp_Typ : Entity_Id;
5291 Subp : Entity_Id;
5293 begin
5294 -- Detect a function call that could denote a nondispatching
5295 -- primitive of the input type.
5297 if Nkind (N) = N_Function_Call
5298 and then Is_Entity_Name (Name (N))
5299 then
5300 Subp := Entity (Name (N));
5302 -- Do not consider function calls with a controlling argument, as
5303 -- those are always dispatching calls.
5305 if Is_Dispatching_Operation (Subp)
5306 and then No (Controlling_Argument (N))
5307 then
5308 Disp_Typ := Find_Dispatching_Type (Subp);
5310 -- To qualify as a suitable primitive, the dispatching type of
5311 -- the function must be the input type.
5313 if Present (Disp_Typ)
5314 and then Unique_Entity (Disp_Typ) = U_Typ
5315 then
5316 Calls_OK := True;
5318 -- There is no need to continue the traversal, as one such
5319 -- call suffices.
5321 return Abandon;
5322 end if;
5323 end if;
5324 end if;
5326 return OK;
5327 end Search_Primitive_Calls;
5329 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5331 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5333 begin
5334 Search_Calls (Expr);
5335 return Calls_OK;
5336 end Expression_Contains_Primitives_Calls_Of;
5338 ----------------------
5339 -- Finalize_Address --
5340 ----------------------
5342 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5343 Utyp : Entity_Id := Typ;
5345 begin
5346 -- Handle protected class-wide or task class-wide types
5348 if Is_Class_Wide_Type (Utyp) then
5349 if Is_Concurrent_Type (Root_Type (Utyp)) then
5350 Utyp := Root_Type (Utyp);
5352 elsif Is_Private_Type (Root_Type (Utyp))
5353 and then Present (Full_View (Root_Type (Utyp)))
5354 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5355 then
5356 Utyp := Full_View (Root_Type (Utyp));
5357 end if;
5358 end if;
5360 -- Handle private types
5362 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5363 Utyp := Full_View (Utyp);
5364 end if;
5366 -- Handle protected and task types
5368 if Is_Concurrent_Type (Utyp)
5369 and then Present (Corresponding_Record_Type (Utyp))
5370 then
5371 Utyp := Corresponding_Record_Type (Utyp);
5372 end if;
5374 Utyp := Underlying_Type (Base_Type (Utyp));
5376 -- Deal with untagged derivation of private views. If the parent is
5377 -- now known to be protected, the finalization routine is the one
5378 -- defined on the corresponding record of the ancestor (corresponding
5379 -- records do not automatically inherit operations, but maybe they
5380 -- should???)
5382 if Is_Untagged_Derivation (Typ) then
5383 if Is_Protected_Type (Typ) then
5384 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5386 else
5387 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5389 if Is_Protected_Type (Utyp) then
5390 Utyp := Corresponding_Record_Type (Utyp);
5391 end if;
5392 end if;
5393 end if;
5395 -- If the underlying_type is a subtype, we are dealing with the
5396 -- completion of a private type. We need to access the base type and
5397 -- generate a conversion to it.
5399 if Utyp /= Base_Type (Utyp) then
5400 pragma Assert (Is_Private_Type (Typ));
5402 Utyp := Base_Type (Utyp);
5403 end if;
5405 -- When dealing with an internally built full view for a type with
5406 -- unknown discriminants, use the original record type.
5408 if Is_Underlying_Record_View (Utyp) then
5409 Utyp := Etype (Utyp);
5410 end if;
5412 return TSS (Utyp, TSS_Finalize_Address);
5413 end Finalize_Address;
5415 ------------------------
5416 -- Find_Interface_ADT --
5417 ------------------------
5419 function Find_Interface_ADT
5420 (T : Entity_Id;
5421 Iface : Entity_Id) return Elmt_Id
5423 ADT : Elmt_Id;
5424 Typ : Entity_Id := T;
5426 begin
5427 pragma Assert (Is_Interface (Iface));
5429 -- Handle private types
5431 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5432 Typ := Full_View (Typ);
5433 end if;
5435 -- Handle access types
5437 if Is_Access_Type (Typ) then
5438 Typ := Designated_Type (Typ);
5439 end if;
5441 -- Handle task and protected types implementing interfaces
5443 if Is_Concurrent_Type (Typ) then
5444 Typ := Corresponding_Record_Type (Typ);
5445 end if;
5447 pragma Assert
5448 (not Is_Class_Wide_Type (Typ)
5449 and then Ekind (Typ) /= E_Incomplete_Type);
5451 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5452 return First_Elmt (Access_Disp_Table (Typ));
5454 else
5455 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5456 while Present (ADT)
5457 and then Present (Related_Type (Node (ADT)))
5458 and then Related_Type (Node (ADT)) /= Iface
5459 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5460 Use_Full_View => True)
5461 loop
5462 Next_Elmt (ADT);
5463 end loop;
5465 pragma Assert (Present (Related_Type (Node (ADT))));
5466 return ADT;
5467 end if;
5468 end Find_Interface_ADT;
5470 ------------------------
5471 -- Find_Interface_Tag --
5472 ------------------------
5474 function Find_Interface_Tag
5475 (T : Entity_Id;
5476 Iface : Entity_Id) return Entity_Id
5478 AI_Tag : Entity_Id := Empty;
5479 Found : Boolean := False;
5480 Typ : Entity_Id := T;
5482 procedure Find_Tag (Typ : Entity_Id);
5483 -- Internal subprogram used to recursively climb to the ancestors
5485 --------------
5486 -- Find_Tag --
5487 --------------
5489 procedure Find_Tag (Typ : Entity_Id) is
5490 AI_Elmt : Elmt_Id;
5491 AI : Node_Id;
5493 begin
5494 -- This routine does not handle the case in which the interface is an
5495 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5497 pragma Assert (Typ /= Iface);
5499 -- Climb to the root type handling private types
5501 if Present (Full_View (Etype (Typ))) then
5502 if Full_View (Etype (Typ)) /= Typ then
5503 Find_Tag (Full_View (Etype (Typ)));
5504 end if;
5506 elsif Etype (Typ) /= Typ then
5507 Find_Tag (Etype (Typ));
5508 end if;
5510 -- Traverse the list of interfaces implemented by the type
5512 if not Found
5513 and then Present (Interfaces (Typ))
5514 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5515 then
5516 -- Skip the tag associated with the primary table
5518 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5519 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5520 pragma Assert (Present (AI_Tag));
5522 AI_Elmt := First_Elmt (Interfaces (Typ));
5523 while Present (AI_Elmt) loop
5524 AI := Node (AI_Elmt);
5526 if AI = Iface
5527 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5528 then
5529 Found := True;
5530 return;
5531 end if;
5533 AI_Tag := Next_Tag_Component (AI_Tag);
5534 Next_Elmt (AI_Elmt);
5535 end loop;
5536 end if;
5537 end Find_Tag;
5539 -- Start of processing for Find_Interface_Tag
5541 begin
5542 pragma Assert (Is_Interface (Iface));
5544 -- Handle access types
5546 if Is_Access_Type (Typ) then
5547 Typ := Designated_Type (Typ);
5548 end if;
5550 -- Handle class-wide types
5552 if Is_Class_Wide_Type (Typ) then
5553 Typ := Root_Type (Typ);
5554 end if;
5556 -- Handle private types
5558 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5559 Typ := Full_View (Typ);
5560 end if;
5562 -- Handle entities from the limited view
5564 if Ekind (Typ) = E_Incomplete_Type then
5565 pragma Assert (Present (Non_Limited_View (Typ)));
5566 Typ := Non_Limited_View (Typ);
5567 end if;
5569 -- Handle task and protected types implementing interfaces
5571 if Is_Concurrent_Type (Typ) then
5572 Typ := Corresponding_Record_Type (Typ);
5573 end if;
5575 -- If the interface is an ancestor of the type, then it shared the
5576 -- primary dispatch table.
5578 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5579 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5580 return First_Tag_Component (Typ);
5582 -- Otherwise we need to search for its associated tag component
5584 else
5585 Find_Tag (Typ);
5586 pragma Assert (Found);
5587 return AI_Tag;
5588 end if;
5589 end Find_Interface_Tag;
5591 ---------------------------
5592 -- Find_Optional_Prim_Op --
5593 ---------------------------
5595 function Find_Optional_Prim_Op
5596 (T : Entity_Id; Name : Name_Id) return Entity_Id
5598 Prim : Elmt_Id;
5599 Typ : Entity_Id := T;
5600 Op : Entity_Id;
5602 begin
5603 if Is_Class_Wide_Type (Typ) then
5604 Typ := Root_Type (Typ);
5605 end if;
5607 Typ := Underlying_Type (Typ);
5609 -- Loop through primitive operations
5611 Prim := First_Elmt (Primitive_Operations (Typ));
5612 while Present (Prim) loop
5613 Op := Node (Prim);
5615 -- We can retrieve primitive operations by name if it is an internal
5616 -- name. For equality we must check that both of its operands have
5617 -- the same type, to avoid confusion with user-defined equalities
5618 -- than may have a non-symmetric signature.
5620 exit when Chars (Op) = Name
5621 and then
5622 (Name /= Name_Op_Eq
5623 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5625 Next_Elmt (Prim);
5626 end loop;
5628 return Node (Prim); -- Empty if not found
5629 end Find_Optional_Prim_Op;
5631 ---------------------------
5632 -- Find_Optional_Prim_Op --
5633 ---------------------------
5635 function Find_Optional_Prim_Op
5636 (T : Entity_Id;
5637 Name : TSS_Name_Type) return Entity_Id
5639 Inher_Op : Entity_Id := Empty;
5640 Own_Op : Entity_Id := Empty;
5641 Prim_Elmt : Elmt_Id;
5642 Prim_Id : Entity_Id;
5643 Typ : Entity_Id := T;
5645 begin
5646 if Is_Class_Wide_Type (Typ) then
5647 Typ := Root_Type (Typ);
5648 end if;
5650 Typ := Underlying_Type (Typ);
5652 -- This search is based on the assertion that the dispatching version
5653 -- of the TSS routine always precedes the real primitive.
5655 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5656 while Present (Prim_Elmt) loop
5657 Prim_Id := Node (Prim_Elmt);
5659 if Is_TSS (Prim_Id, Name) then
5660 if Present (Alias (Prim_Id)) then
5661 Inher_Op := Prim_Id;
5662 else
5663 Own_Op := Prim_Id;
5664 end if;
5665 end if;
5667 Next_Elmt (Prim_Elmt);
5668 end loop;
5670 if Present (Own_Op) then
5671 return Own_Op;
5672 elsif Present (Inher_Op) then
5673 return Inher_Op;
5674 else
5675 return Empty;
5676 end if;
5677 end Find_Optional_Prim_Op;
5679 ------------------
5680 -- Find_Prim_Op --
5681 ------------------
5683 function Find_Prim_Op
5684 (T : Entity_Id; Name : Name_Id) return Entity_Id
5686 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5687 begin
5688 if No (Result) then
5689 raise Program_Error;
5690 end if;
5692 return Result;
5693 end Find_Prim_Op;
5695 ------------------
5696 -- Find_Prim_Op --
5697 ------------------
5699 function Find_Prim_Op
5700 (T : Entity_Id;
5701 Name : TSS_Name_Type) return Entity_Id
5703 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5704 begin
5705 if No (Result) then
5706 raise Program_Error;
5707 end if;
5709 return Result;
5710 end Find_Prim_Op;
5712 ----------------------------
5713 -- Find_Protection_Object --
5714 ----------------------------
5716 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5717 S : Entity_Id;
5719 begin
5720 S := Scop;
5721 while Present (S) loop
5722 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5723 and then Present (Protection_Object (S))
5724 then
5725 return Protection_Object (S);
5726 end if;
5728 S := Scope (S);
5729 end loop;
5731 -- If we do not find a Protection object in the scope chain, then
5732 -- something has gone wrong, most likely the object was never created.
5734 raise Program_Error;
5735 end Find_Protection_Object;
5737 --------------------------
5738 -- Find_Protection_Type --
5739 --------------------------
5741 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5742 Comp : Entity_Id;
5743 Typ : Entity_Id := Conc_Typ;
5745 begin
5746 if Is_Concurrent_Type (Typ) then
5747 Typ := Corresponding_Record_Type (Typ);
5748 end if;
5750 -- Since restriction violations are not considered serious errors, the
5751 -- expander remains active, but may leave the corresponding record type
5752 -- malformed. In such cases, component _object is not available so do
5753 -- not look for it.
5755 if not Analyzed (Typ) then
5756 return Empty;
5757 end if;
5759 Comp := First_Component (Typ);
5760 while Present (Comp) loop
5761 if Chars (Comp) = Name_uObject then
5762 return Base_Type (Etype (Comp));
5763 end if;
5765 Next_Component (Comp);
5766 end loop;
5768 -- The corresponding record of a protected type should always have an
5769 -- _object field.
5771 raise Program_Error;
5772 end Find_Protection_Type;
5774 -----------------------
5775 -- Find_Hook_Context --
5776 -----------------------
5778 function Find_Hook_Context (N : Node_Id) return Node_Id is
5779 Par : Node_Id;
5780 Top : Node_Id;
5782 Wrapped_Node : Node_Id;
5783 -- Note: if we are in a transient scope, we want to reuse it as
5784 -- the context for actions insertion, if possible. But if N is itself
5785 -- part of the stored actions for the current transient scope,
5786 -- then we need to insert at the appropriate (inner) location in
5787 -- the not as an action on Node_To_Be_Wrapped.
5789 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5791 begin
5792 -- When the node is inside a case/if expression, the lifetime of any
5793 -- temporary controlled object is extended. Find a suitable insertion
5794 -- node by locating the topmost case or if expressions.
5796 if In_Cond_Expr then
5797 Par := N;
5798 Top := N;
5799 while Present (Par) loop
5800 if Nkind_In (Original_Node (Par), N_Case_Expression,
5801 N_If_Expression)
5802 then
5803 Top := Par;
5805 -- Prevent the search from going too far
5807 elsif Is_Body_Or_Package_Declaration (Par) then
5808 exit;
5809 end if;
5811 Par := Parent (Par);
5812 end loop;
5814 -- The topmost case or if expression is now recovered, but it may
5815 -- still not be the correct place to add generated code. Climb to
5816 -- find a parent that is part of a declarative or statement list,
5817 -- and is not a list of actuals in a call.
5819 Par := Top;
5820 while Present (Par) loop
5821 if Is_List_Member (Par)
5822 and then not Nkind_In (Par, N_Component_Association,
5823 N_Discriminant_Association,
5824 N_Parameter_Association,
5825 N_Pragma_Argument_Association)
5826 and then not Nkind_In (Parent (Par), N_Function_Call,
5827 N_Procedure_Call_Statement,
5828 N_Entry_Call_Statement)
5830 then
5831 return Par;
5833 -- Prevent the search from going too far
5835 elsif Is_Body_Or_Package_Declaration (Par) then
5836 exit;
5837 end if;
5839 Par := Parent (Par);
5840 end loop;
5842 return Par;
5844 else
5845 Par := N;
5846 while Present (Par) loop
5848 -- Keep climbing past various operators
5850 if Nkind (Parent (Par)) in N_Op
5851 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5852 then
5853 Par := Parent (Par);
5854 else
5855 exit;
5856 end if;
5857 end loop;
5859 Top := Par;
5861 -- The node may be located in a pragma in which case return the
5862 -- pragma itself:
5864 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5866 -- Similar case occurs when the node is related to an object
5867 -- declaration or assignment:
5869 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5871 -- Another case to consider is when the node is part of a return
5872 -- statement:
5874 -- return ... and then Ctrl_Func_Call ...;
5876 -- Another case is when the node acts as a formal in a procedure
5877 -- call statement:
5879 -- Proc (... and then Ctrl_Func_Call ...);
5881 if Scope_Is_Transient then
5882 Wrapped_Node := Node_To_Be_Wrapped;
5883 else
5884 Wrapped_Node := Empty;
5885 end if;
5887 while Present (Par) loop
5888 if Par = Wrapped_Node
5889 or else Nkind_In (Par, N_Assignment_Statement,
5890 N_Object_Declaration,
5891 N_Pragma,
5892 N_Procedure_Call_Statement,
5893 N_Simple_Return_Statement)
5894 then
5895 return Par;
5897 -- Prevent the search from going too far
5899 elsif Is_Body_Or_Package_Declaration (Par) then
5900 exit;
5901 end if;
5903 Par := Parent (Par);
5904 end loop;
5906 -- Return the topmost short circuit operator
5908 return Top;
5909 end if;
5910 end Find_Hook_Context;
5912 ------------------------------
5913 -- Following_Address_Clause --
5914 ------------------------------
5916 function Following_Address_Clause (D : Node_Id) return Node_Id is
5917 Id : constant Entity_Id := Defining_Identifier (D);
5918 Result : Node_Id;
5919 Par : Node_Id;
5921 function Check_Decls (D : Node_Id) return Node_Id;
5922 -- This internal function differs from the main function in that it
5923 -- gets called to deal with a following package private part, and
5924 -- it checks declarations starting with D (the main function checks
5925 -- declarations following D). If D is Empty, then Empty is returned.
5927 -----------------
5928 -- Check_Decls --
5929 -----------------
5931 function Check_Decls (D : Node_Id) return Node_Id is
5932 Decl : Node_Id;
5934 begin
5935 Decl := D;
5936 while Present (Decl) loop
5937 if Nkind (Decl) = N_At_Clause
5938 and then Chars (Identifier (Decl)) = Chars (Id)
5939 then
5940 return Decl;
5942 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5943 and then Chars (Decl) = Name_Address
5944 and then Chars (Name (Decl)) = Chars (Id)
5945 then
5946 return Decl;
5947 end if;
5949 Next (Decl);
5950 end loop;
5952 -- Otherwise not found, return Empty
5954 return Empty;
5955 end Check_Decls;
5957 -- Start of processing for Following_Address_Clause
5959 begin
5960 -- If parser detected no address clause for the identifier in question,
5961 -- then the answer is a quick NO, without the need for a search.
5963 if not Get_Name_Table_Boolean1 (Chars (Id)) then
5964 return Empty;
5965 end if;
5967 -- Otherwise search current declarative unit
5969 Result := Check_Decls (Next (D));
5971 if Present (Result) then
5972 return Result;
5973 end if;
5975 -- Check for possible package private part following
5977 Par := Parent (D);
5979 if Nkind (Par) = N_Package_Specification
5980 and then Visible_Declarations (Par) = List_Containing (D)
5981 and then Present (Private_Declarations (Par))
5982 then
5983 -- Private part present, check declarations there
5985 return Check_Decls (First (Private_Declarations (Par)));
5987 else
5988 -- No private part, clause not found, return Empty
5990 return Empty;
5991 end if;
5992 end Following_Address_Clause;
5994 ----------------------
5995 -- Force_Evaluation --
5996 ----------------------
5998 procedure Force_Evaluation
5999 (Exp : Node_Id;
6000 Name_Req : Boolean := False;
6001 Related_Id : Entity_Id := Empty;
6002 Is_Low_Bound : Boolean := False;
6003 Is_High_Bound : Boolean := False;
6004 Mode : Force_Evaluation_Mode := Relaxed)
6006 begin
6007 Remove_Side_Effects
6008 (Exp => Exp,
6009 Name_Req => Name_Req,
6010 Variable_Ref => True,
6011 Renaming_Req => False,
6012 Related_Id => Related_Id,
6013 Is_Low_Bound => Is_Low_Bound,
6014 Is_High_Bound => Is_High_Bound,
6015 Check_Side_Effects =>
6016 Is_Static_Expression (Exp)
6017 or else Mode = Relaxed);
6018 end Force_Evaluation;
6020 ---------------------------------
6021 -- Fully_Qualified_Name_String --
6022 ---------------------------------
6024 function Fully_Qualified_Name_String
6025 (E : Entity_Id;
6026 Append_NUL : Boolean := True) return String_Id
6028 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6029 -- Compute recursively the qualified name without NUL at the end, adding
6030 -- it to the currently started string being generated
6032 ----------------------------------
6033 -- Internal_Full_Qualified_Name --
6034 ----------------------------------
6036 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6037 Ent : Entity_Id;
6039 begin
6040 -- Deal properly with child units
6042 if Nkind (E) = N_Defining_Program_Unit_Name then
6043 Ent := Defining_Identifier (E);
6044 else
6045 Ent := E;
6046 end if;
6048 -- Compute qualification recursively (only "Standard" has no scope)
6050 if Present (Scope (Scope (Ent))) then
6051 Internal_Full_Qualified_Name (Scope (Ent));
6052 Store_String_Char (Get_Char_Code ('.'));
6053 end if;
6055 -- Every entity should have a name except some expanded blocks
6056 -- don't bother about those.
6058 if Chars (Ent) = No_Name then
6059 return;
6060 end if;
6062 -- Generates the entity name in upper case
6064 Get_Decoded_Name_String (Chars (Ent));
6065 Set_All_Upper_Case;
6066 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6067 return;
6068 end Internal_Full_Qualified_Name;
6070 -- Start of processing for Full_Qualified_Name
6072 begin
6073 Start_String;
6074 Internal_Full_Qualified_Name (E);
6076 if Append_NUL then
6077 Store_String_Char (Get_Char_Code (ASCII.NUL));
6078 end if;
6080 return End_String;
6081 end Fully_Qualified_Name_String;
6083 ------------------------
6084 -- Generate_Poll_Call --
6085 ------------------------
6087 procedure Generate_Poll_Call (N : Node_Id) is
6088 begin
6089 -- No poll call if polling not active
6091 if not Polling_Required then
6092 return;
6094 -- Otherwise generate require poll call
6096 else
6097 Insert_Before_And_Analyze (N,
6098 Make_Procedure_Call_Statement (Sloc (N),
6099 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6100 end if;
6101 end Generate_Poll_Call;
6103 ---------------------------------
6104 -- Get_Current_Value_Condition --
6105 ---------------------------------
6107 -- Note: the implementation of this procedure is very closely tied to the
6108 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6109 -- interpret Current_Value fields set by the Set procedure, so the two
6110 -- procedures need to be closely coordinated.
6112 procedure Get_Current_Value_Condition
6113 (Var : Node_Id;
6114 Op : out Node_Kind;
6115 Val : out Node_Id)
6117 Loc : constant Source_Ptr := Sloc (Var);
6118 Ent : constant Entity_Id := Entity (Var);
6120 procedure Process_Current_Value_Condition
6121 (N : Node_Id;
6122 S : Boolean);
6123 -- N is an expression which holds either True (S = True) or False (S =
6124 -- False) in the condition. This procedure digs out the expression and
6125 -- if it refers to Ent, sets Op and Val appropriately.
6127 -------------------------------------
6128 -- Process_Current_Value_Condition --
6129 -------------------------------------
6131 procedure Process_Current_Value_Condition
6132 (N : Node_Id;
6133 S : Boolean)
6135 Cond : Node_Id;
6136 Prev_Cond : Node_Id;
6137 Sens : Boolean;
6139 begin
6140 Cond := N;
6141 Sens := S;
6143 loop
6144 Prev_Cond := Cond;
6146 -- Deal with NOT operators, inverting sense
6148 while Nkind (Cond) = N_Op_Not loop
6149 Cond := Right_Opnd (Cond);
6150 Sens := not Sens;
6151 end loop;
6153 -- Deal with conversions, qualifications, and expressions with
6154 -- actions.
6156 while Nkind_In (Cond,
6157 N_Type_Conversion,
6158 N_Qualified_Expression,
6159 N_Expression_With_Actions)
6160 loop
6161 Cond := Expression (Cond);
6162 end loop;
6164 exit when Cond = Prev_Cond;
6165 end loop;
6167 -- Deal with AND THEN and AND cases
6169 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6171 -- Don't ever try to invert a condition that is of the form of an
6172 -- AND or AND THEN (since we are not doing sufficiently general
6173 -- processing to allow this).
6175 if Sens = False then
6176 Op := N_Empty;
6177 Val := Empty;
6178 return;
6179 end if;
6181 -- Recursively process AND and AND THEN branches
6183 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6185 if Op /= N_Empty then
6186 return;
6187 end if;
6189 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6190 return;
6192 -- Case of relational operator
6194 elsif Nkind (Cond) in N_Op_Compare then
6195 Op := Nkind (Cond);
6197 -- Invert sense of test if inverted test
6199 if Sens = False then
6200 case Op is
6201 when N_Op_Eq => Op := N_Op_Ne;
6202 when N_Op_Ne => Op := N_Op_Eq;
6203 when N_Op_Lt => Op := N_Op_Ge;
6204 when N_Op_Gt => Op := N_Op_Le;
6205 when N_Op_Le => Op := N_Op_Gt;
6206 when N_Op_Ge => Op := N_Op_Lt;
6207 when others => raise Program_Error;
6208 end case;
6209 end if;
6211 -- Case of entity op value
6213 if Is_Entity_Name (Left_Opnd (Cond))
6214 and then Ent = Entity (Left_Opnd (Cond))
6215 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6216 then
6217 Val := Right_Opnd (Cond);
6219 -- Case of value op entity
6221 elsif Is_Entity_Name (Right_Opnd (Cond))
6222 and then Ent = Entity (Right_Opnd (Cond))
6223 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6224 then
6225 Val := Left_Opnd (Cond);
6227 -- We are effectively swapping operands
6229 case Op is
6230 when N_Op_Eq => null;
6231 when N_Op_Ne => null;
6232 when N_Op_Lt => Op := N_Op_Gt;
6233 when N_Op_Gt => Op := N_Op_Lt;
6234 when N_Op_Le => Op := N_Op_Ge;
6235 when N_Op_Ge => Op := N_Op_Le;
6236 when others => raise Program_Error;
6237 end case;
6239 else
6240 Op := N_Empty;
6241 end if;
6243 return;
6245 elsif Nkind_In (Cond,
6246 N_Type_Conversion,
6247 N_Qualified_Expression,
6248 N_Expression_With_Actions)
6249 then
6250 Cond := Expression (Cond);
6252 -- Case of Boolean variable reference, return as though the
6253 -- reference had said var = True.
6255 else
6256 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6257 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6259 if Sens = False then
6260 Op := N_Op_Ne;
6261 else
6262 Op := N_Op_Eq;
6263 end if;
6264 end if;
6265 end if;
6266 end Process_Current_Value_Condition;
6268 -- Start of processing for Get_Current_Value_Condition
6270 begin
6271 Op := N_Empty;
6272 Val := Empty;
6274 -- Immediate return, nothing doing, if this is not an object
6276 if Ekind (Ent) not in Object_Kind then
6277 return;
6278 end if;
6280 -- Otherwise examine current value
6282 declare
6283 CV : constant Node_Id := Current_Value (Ent);
6284 Sens : Boolean;
6285 Stm : Node_Id;
6287 begin
6288 -- If statement. Condition is known true in THEN section, known False
6289 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6291 if Nkind (CV) = N_If_Statement then
6293 -- Before start of IF statement
6295 if Loc < Sloc (CV) then
6296 return;
6298 -- After end of IF statement
6300 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6301 return;
6302 end if;
6304 -- At this stage we know that we are within the IF statement, but
6305 -- unfortunately, the tree does not record the SLOC of the ELSE so
6306 -- we cannot use a simple SLOC comparison to distinguish between
6307 -- the then/else statements, so we have to climb the tree.
6309 declare
6310 N : Node_Id;
6312 begin
6313 N := Parent (Var);
6314 while Parent (N) /= CV loop
6315 N := Parent (N);
6317 -- If we fall off the top of the tree, then that's odd, but
6318 -- perhaps it could occur in some error situation, and the
6319 -- safest response is simply to assume that the outcome of
6320 -- the condition is unknown. No point in bombing during an
6321 -- attempt to optimize things.
6323 if No (N) then
6324 return;
6325 end if;
6326 end loop;
6328 -- Now we have N pointing to a node whose parent is the IF
6329 -- statement in question, so now we can tell if we are within
6330 -- the THEN statements.
6332 if Is_List_Member (N)
6333 and then List_Containing (N) = Then_Statements (CV)
6334 then
6335 Sens := True;
6337 -- If the variable reference does not come from source, we
6338 -- cannot reliably tell whether it appears in the else part.
6339 -- In particular, if it appears in generated code for a node
6340 -- that requires finalization, it may be attached to a list
6341 -- that has not been yet inserted into the code. For now,
6342 -- treat it as unknown.
6344 elsif not Comes_From_Source (N) then
6345 return;
6347 -- Otherwise we must be in ELSIF or ELSE part
6349 else
6350 Sens := False;
6351 end if;
6352 end;
6354 -- ELSIF part. Condition is known true within the referenced
6355 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6356 -- and unknown before the ELSE part or after the IF statement.
6358 elsif Nkind (CV) = N_Elsif_Part then
6360 -- if the Elsif_Part had condition_actions, the elsif has been
6361 -- rewritten as a nested if, and the original elsif_part is
6362 -- detached from the tree, so there is no way to obtain useful
6363 -- information on the current value of the variable.
6364 -- Can this be improved ???
6366 if No (Parent (CV)) then
6367 return;
6368 end if;
6370 Stm := Parent (CV);
6372 -- If the tree has been otherwise rewritten there is nothing
6373 -- else to be done either.
6375 if Nkind (Stm) /= N_If_Statement then
6376 return;
6377 end if;
6379 -- Before start of ELSIF part
6381 if Loc < Sloc (CV) then
6382 return;
6384 -- After end of IF statement
6386 elsif Loc >= Sloc (Stm) +
6387 Text_Ptr (UI_To_Int (End_Span (Stm)))
6388 then
6389 return;
6390 end if;
6392 -- Again we lack the SLOC of the ELSE, so we need to climb the
6393 -- tree to see if we are within the ELSIF part in question.
6395 declare
6396 N : Node_Id;
6398 begin
6399 N := Parent (Var);
6400 while Parent (N) /= Stm loop
6401 N := Parent (N);
6403 -- If we fall off the top of the tree, then that's odd, but
6404 -- perhaps it could occur in some error situation, and the
6405 -- safest response is simply to assume that the outcome of
6406 -- the condition is unknown. No point in bombing during an
6407 -- attempt to optimize things.
6409 if No (N) then
6410 return;
6411 end if;
6412 end loop;
6414 -- Now we have N pointing to a node whose parent is the IF
6415 -- statement in question, so see if is the ELSIF part we want.
6416 -- the THEN statements.
6418 if N = CV then
6419 Sens := True;
6421 -- Otherwise we must be in subsequent ELSIF or ELSE part
6423 else
6424 Sens := False;
6425 end if;
6426 end;
6428 -- Iteration scheme of while loop. The condition is known to be
6429 -- true within the body of the loop.
6431 elsif Nkind (CV) = N_Iteration_Scheme then
6432 declare
6433 Loop_Stmt : constant Node_Id := Parent (CV);
6435 begin
6436 -- Before start of body of loop
6438 if Loc < Sloc (Loop_Stmt) then
6439 return;
6441 -- After end of LOOP statement
6443 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6444 return;
6446 -- We are within the body of the loop
6448 else
6449 Sens := True;
6450 end if;
6451 end;
6453 -- All other cases of Current_Value settings
6455 else
6456 return;
6457 end if;
6459 -- If we fall through here, then we have a reportable condition, Sens
6460 -- is True if the condition is true and False if it needs inverting.
6462 Process_Current_Value_Condition (Condition (CV), Sens);
6463 end;
6464 end Get_Current_Value_Condition;
6466 ---------------------
6467 -- Get_Stream_Size --
6468 ---------------------
6470 function Get_Stream_Size (E : Entity_Id) return Uint is
6471 begin
6472 -- If we have a Stream_Size clause for this type use it
6474 if Has_Stream_Size_Clause (E) then
6475 return Static_Integer (Expression (Stream_Size_Clause (E)));
6477 -- Otherwise the Stream_Size if the size of the type
6479 else
6480 return Esize (E);
6481 end if;
6482 end Get_Stream_Size;
6484 ---------------------------
6485 -- Has_Access_Constraint --
6486 ---------------------------
6488 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6489 Disc : Entity_Id;
6490 T : constant Entity_Id := Etype (E);
6492 begin
6493 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6494 Disc := First_Discriminant (T);
6495 while Present (Disc) loop
6496 if Is_Access_Type (Etype (Disc)) then
6497 return True;
6498 end if;
6500 Next_Discriminant (Disc);
6501 end loop;
6503 return False;
6504 else
6505 return False;
6506 end if;
6507 end Has_Access_Constraint;
6509 -----------------------------------------------------
6510 -- Has_Annotate_Pragma_For_External_Axiomatization --
6511 -----------------------------------------------------
6513 function Has_Annotate_Pragma_For_External_Axiomatization
6514 (E : Entity_Id) return Boolean
6516 function Is_Annotate_Pragma_For_External_Axiomatization
6517 (N : Node_Id) return Boolean;
6518 -- Returns whether N is
6519 -- pragma Annotate (GNATprove, External_Axiomatization);
6521 ----------------------------------------------------
6522 -- Is_Annotate_Pragma_For_External_Axiomatization --
6523 ----------------------------------------------------
6525 -- The general form of pragma Annotate is
6527 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6528 -- ARG ::= NAME | EXPRESSION
6530 -- The first two arguments are by convention intended to refer to an
6531 -- external tool and a tool-specific function. These arguments are
6532 -- not analyzed.
6534 -- The following is used to annotate a package specification which
6535 -- GNATprove should treat specially, because the axiomatization of
6536 -- this unit is given by the user instead of being automatically
6537 -- generated.
6539 -- pragma Annotate (GNATprove, External_Axiomatization);
6541 function Is_Annotate_Pragma_For_External_Axiomatization
6542 (N : Node_Id) return Boolean
6544 Name_GNATprove : constant String :=
6545 "gnatprove";
6546 Name_External_Axiomatization : constant String :=
6547 "external_axiomatization";
6548 -- Special names
6550 begin
6551 if Nkind (N) = N_Pragma
6552 and then Get_Pragma_Id (N) = Pragma_Annotate
6553 and then List_Length (Pragma_Argument_Associations (N)) = 2
6554 then
6555 declare
6556 Arg1 : constant Node_Id :=
6557 First (Pragma_Argument_Associations (N));
6558 Arg2 : constant Node_Id := Next (Arg1);
6559 Nam1 : Name_Id;
6560 Nam2 : Name_Id;
6562 begin
6563 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6564 -- Name_External_Axiomatization so that Name_Find returns the
6565 -- corresponding name. This takes care of all possible casings.
6567 Name_Len := 0;
6568 Add_Str_To_Name_Buffer (Name_GNATprove);
6569 Nam1 := Name_Find;
6571 Name_Len := 0;
6572 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6573 Nam2 := Name_Find;
6575 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6576 and then
6577 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6578 end;
6580 else
6581 return False;
6582 end if;
6583 end Is_Annotate_Pragma_For_External_Axiomatization;
6585 -- Local variables
6587 Decl : Node_Id;
6588 Vis_Decls : List_Id;
6589 N : Node_Id;
6591 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6593 begin
6594 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6595 Decl := Parent (Parent (E));
6596 else
6597 Decl := Parent (E);
6598 end if;
6600 Vis_Decls := Visible_Declarations (Decl);
6602 N := First (Vis_Decls);
6603 while Present (N) loop
6605 -- Skip declarations generated by the frontend. Skip all pragmas
6606 -- that are not the desired Annotate pragma. Stop the search on
6607 -- the first non-pragma source declaration.
6609 if Comes_From_Source (N) then
6610 if Nkind (N) = N_Pragma then
6611 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6612 return True;
6613 end if;
6614 else
6615 return False;
6616 end if;
6617 end if;
6619 Next (N);
6620 end loop;
6622 return False;
6623 end Has_Annotate_Pragma_For_External_Axiomatization;
6625 --------------------
6626 -- Homonym_Number --
6627 --------------------
6629 function Homonym_Number (Subp : Entity_Id) return Nat is
6630 Count : Nat;
6631 Hom : Entity_Id;
6633 begin
6634 Count := 1;
6635 Hom := Homonym (Subp);
6636 while Present (Hom) loop
6637 if Scope (Hom) = Scope (Subp) then
6638 Count := Count + 1;
6639 end if;
6641 Hom := Homonym (Hom);
6642 end loop;
6644 return Count;
6645 end Homonym_Number;
6647 -----------------------------------
6648 -- In_Library_Level_Package_Body --
6649 -----------------------------------
6651 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6652 begin
6653 -- First determine whether the entity appears at the library level, then
6654 -- look at the containing unit.
6656 if Is_Library_Level_Entity (Id) then
6657 declare
6658 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6660 begin
6661 return Nkind (Unit (Container)) = N_Package_Body;
6662 end;
6663 end if;
6665 return False;
6666 end In_Library_Level_Package_Body;
6668 ------------------------------
6669 -- In_Unconditional_Context --
6670 ------------------------------
6672 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6673 P : Node_Id;
6675 begin
6676 P := Node;
6677 while Present (P) loop
6678 case Nkind (P) is
6679 when N_Subprogram_Body => return True;
6680 when N_If_Statement => return False;
6681 when N_Loop_Statement => return False;
6682 when N_Case_Statement => return False;
6683 when others => P := Parent (P);
6684 end case;
6685 end loop;
6687 return False;
6688 end In_Unconditional_Context;
6690 -------------------
6691 -- Insert_Action --
6692 -------------------
6694 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6695 begin
6696 if Present (Ins_Action) then
6697 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6698 end if;
6699 end Insert_Action;
6701 -- Version with check(s) suppressed
6703 procedure Insert_Action
6704 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6706 begin
6707 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6708 end Insert_Action;
6710 -------------------------
6711 -- Insert_Action_After --
6712 -------------------------
6714 procedure Insert_Action_After
6715 (Assoc_Node : Node_Id;
6716 Ins_Action : Node_Id)
6718 begin
6719 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6720 end Insert_Action_After;
6722 --------------------
6723 -- Insert_Actions --
6724 --------------------
6726 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6727 N : Node_Id;
6728 P : Node_Id;
6730 Wrapped_Node : Node_Id := Empty;
6732 begin
6733 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6734 return;
6735 end if;
6737 -- Ignore insert of actions from inside default expression (or other
6738 -- similar "spec expression") in the special spec-expression analyze
6739 -- mode. Any insertions at this point have no relevance, since we are
6740 -- only doing the analyze to freeze the types of any static expressions.
6741 -- See section "Handling of Default Expressions" in the spec of package
6742 -- Sem for further details.
6744 if In_Spec_Expression then
6745 return;
6746 end if;
6748 -- If the action derives from stuff inside a record, then the actions
6749 -- are attached to the current scope, to be inserted and analyzed on
6750 -- exit from the scope. The reason for this is that we may also be
6751 -- generating freeze actions at the same time, and they must eventually
6752 -- be elaborated in the correct order.
6754 if Is_Record_Type (Current_Scope)
6755 and then not Is_Frozen (Current_Scope)
6756 then
6757 if No (Scope_Stack.Table
6758 (Scope_Stack.Last).Pending_Freeze_Actions)
6759 then
6760 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6761 Ins_Actions;
6762 else
6763 Append_List
6764 (Ins_Actions,
6765 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6766 end if;
6768 return;
6769 end if;
6771 -- We now intend to climb up the tree to find the right point to
6772 -- insert the actions. We start at Assoc_Node, unless this node is a
6773 -- subexpression in which case we start with its parent. We do this for
6774 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6775 -- itself one of the special nodes like N_And_Then, then we assume that
6776 -- an initial request to insert actions for such a node does not expect
6777 -- the actions to get deposited in the node for later handling when the
6778 -- node is expanded, since clearly the node is being dealt with by the
6779 -- caller. Note that in the subexpression case, N is always the child we
6780 -- came from.
6782 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6783 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6784 -- Procedure calls, and similarly procedure attribute references, are
6785 -- also statements.
6787 if Nkind (Assoc_Node) in N_Subexpr
6788 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6789 or else Etype (Assoc_Node) /= Standard_Void_Type)
6790 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6791 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6792 or else not Is_Procedure_Attribute_Name
6793 (Attribute_Name (Assoc_Node)))
6794 then
6795 N := Assoc_Node;
6796 P := Parent (Assoc_Node);
6798 -- Non-subexpression case. Note that N is initially Empty in this case
6799 -- (N is only guaranteed Non-Empty in the subexpr case).
6801 else
6802 N := Empty;
6803 P := Assoc_Node;
6804 end if;
6806 -- Capture root of the transient scope
6808 if Scope_Is_Transient then
6809 Wrapped_Node := Node_To_Be_Wrapped;
6810 end if;
6812 loop
6813 pragma Assert (Present (P));
6815 -- Make sure that inserted actions stay in the transient scope
6817 if Present (Wrapped_Node) and then N = Wrapped_Node then
6818 Store_Before_Actions_In_Scope (Ins_Actions);
6819 return;
6820 end if;
6822 case Nkind (P) is
6824 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6825 -- in the Actions field of the right operand. They will be moved
6826 -- out further when the AND THEN or OR ELSE operator is expanded.
6827 -- Nothing special needs to be done for the left operand since
6828 -- in that case the actions are executed unconditionally.
6830 when N_Short_Circuit =>
6831 if N = Right_Opnd (P) then
6833 -- We are now going to either append the actions to the
6834 -- actions field of the short-circuit operation. We will
6835 -- also analyze the actions now.
6837 -- This analysis is really too early, the proper thing would
6838 -- be to just park them there now, and only analyze them if
6839 -- we find we really need them, and to it at the proper
6840 -- final insertion point. However attempting to this proved
6841 -- tricky, so for now we just kill current values before and
6842 -- after the analyze call to make sure we avoid peculiar
6843 -- optimizations from this out of order insertion.
6845 Kill_Current_Values;
6847 -- If P has already been expanded, we can't park new actions
6848 -- on it, so we need to expand them immediately, introducing
6849 -- an Expression_With_Actions. N can't be an expression
6850 -- with actions, or else then the actions would have been
6851 -- inserted at an inner level.
6853 if Analyzed (P) then
6854 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6855 Rewrite (N,
6856 Make_Expression_With_Actions (Sloc (N),
6857 Actions => Ins_Actions,
6858 Expression => Relocate_Node (N)));
6859 Analyze_And_Resolve (N);
6861 elsif Present (Actions (P)) then
6862 Insert_List_After_And_Analyze
6863 (Last (Actions (P)), Ins_Actions);
6864 else
6865 Set_Actions (P, Ins_Actions);
6866 Analyze_List (Actions (P));
6867 end if;
6869 Kill_Current_Values;
6871 return;
6872 end if;
6874 -- Then or Else dependent expression of an if expression. Add
6875 -- actions to Then_Actions or Else_Actions field as appropriate.
6876 -- The actions will be moved further out when the if is expanded.
6878 when N_If_Expression =>
6879 declare
6880 ThenX : constant Node_Id := Next (First (Expressions (P)));
6881 ElseX : constant Node_Id := Next (ThenX);
6883 begin
6884 -- If the enclosing expression is already analyzed, as
6885 -- is the case for nested elaboration checks, insert the
6886 -- conditional further out.
6888 if Analyzed (P) then
6889 null;
6891 -- Actions belong to the then expression, temporarily place
6892 -- them as Then_Actions of the if expression. They will be
6893 -- moved to the proper place later when the if expression
6894 -- is expanded.
6896 elsif N = ThenX then
6897 if Present (Then_Actions (P)) then
6898 Insert_List_After_And_Analyze
6899 (Last (Then_Actions (P)), Ins_Actions);
6900 else
6901 Set_Then_Actions (P, Ins_Actions);
6902 Analyze_List (Then_Actions (P));
6903 end if;
6905 return;
6907 -- Actions belong to the else expression, temporarily place
6908 -- them as Else_Actions of the if expression. They will be
6909 -- moved to the proper place later when the if expression
6910 -- is expanded.
6912 elsif N = ElseX then
6913 if Present (Else_Actions (P)) then
6914 Insert_List_After_And_Analyze
6915 (Last (Else_Actions (P)), Ins_Actions);
6916 else
6917 Set_Else_Actions (P, Ins_Actions);
6918 Analyze_List (Else_Actions (P));
6919 end if;
6921 return;
6923 -- Actions belong to the condition. In this case they are
6924 -- unconditionally executed, and so we can continue the
6925 -- search for the proper insert point.
6927 else
6928 null;
6929 end if;
6930 end;
6932 -- Alternative of case expression, we place the action in the
6933 -- Actions field of the case expression alternative, this will
6934 -- be handled when the case expression is expanded.
6936 when N_Case_Expression_Alternative =>
6937 if Present (Actions (P)) then
6938 Insert_List_After_And_Analyze
6939 (Last (Actions (P)), Ins_Actions);
6940 else
6941 Set_Actions (P, Ins_Actions);
6942 Analyze_List (Actions (P));
6943 end if;
6945 return;
6947 -- Case of appearing within an Expressions_With_Actions node. When
6948 -- the new actions come from the expression of the expression with
6949 -- actions, they must be added to the existing actions. The other
6950 -- alternative is when the new actions are related to one of the
6951 -- existing actions of the expression with actions, and should
6952 -- never reach here: if actions are inserted on a statement
6953 -- within the Actions of an expression with actions, or on some
6954 -- subexpression of such a statement, then the outermost proper
6955 -- insertion point is right before the statement, and we should
6956 -- never climb up as far as the N_Expression_With_Actions itself.
6958 when N_Expression_With_Actions =>
6959 if N = Expression (P) then
6960 if Is_Empty_List (Actions (P)) then
6961 Append_List_To (Actions (P), Ins_Actions);
6962 Analyze_List (Actions (P));
6963 else
6964 Insert_List_After_And_Analyze
6965 (Last (Actions (P)), Ins_Actions);
6966 end if;
6968 return;
6970 else
6971 raise Program_Error;
6972 end if;
6974 -- Case of appearing in the condition of a while expression or
6975 -- elsif. We insert the actions into the Condition_Actions field.
6976 -- They will be moved further out when the while loop or elsif
6977 -- is analyzed.
6979 when N_Elsif_Part
6980 | N_Iteration_Scheme
6982 if N = Condition (P) then
6983 if Present (Condition_Actions (P)) then
6984 Insert_List_After_And_Analyze
6985 (Last (Condition_Actions (P)), Ins_Actions);
6986 else
6987 Set_Condition_Actions (P, Ins_Actions);
6989 -- Set the parent of the insert actions explicitly. This
6990 -- is not a syntactic field, but we need the parent field
6991 -- set, in particular so that freeze can understand that
6992 -- it is dealing with condition actions, and properly
6993 -- insert the freezing actions.
6995 Set_Parent (Ins_Actions, P);
6996 Analyze_List (Condition_Actions (P));
6997 end if;
6999 return;
7000 end if;
7002 -- Statements, declarations, pragmas, representation clauses
7004 when
7005 -- Statements
7007 N_Procedure_Call_Statement
7008 | N_Statement_Other_Than_Procedure_Call
7010 -- Pragmas
7012 | N_Pragma
7014 -- Representation_Clause
7016 | N_At_Clause
7017 | N_Attribute_Definition_Clause
7018 | N_Enumeration_Representation_Clause
7019 | N_Record_Representation_Clause
7021 -- Declarations
7023 | N_Abstract_Subprogram_Declaration
7024 | N_Entry_Body
7025 | N_Exception_Declaration
7026 | N_Exception_Renaming_Declaration
7027 | N_Expression_Function
7028 | N_Formal_Abstract_Subprogram_Declaration
7029 | N_Formal_Concrete_Subprogram_Declaration
7030 | N_Formal_Object_Declaration
7031 | N_Formal_Type_Declaration
7032 | N_Full_Type_Declaration
7033 | N_Function_Instantiation
7034 | N_Generic_Function_Renaming_Declaration
7035 | N_Generic_Package_Declaration
7036 | N_Generic_Package_Renaming_Declaration
7037 | N_Generic_Procedure_Renaming_Declaration
7038 | N_Generic_Subprogram_Declaration
7039 | N_Implicit_Label_Declaration
7040 | N_Incomplete_Type_Declaration
7041 | N_Number_Declaration
7042 | N_Object_Declaration
7043 | N_Object_Renaming_Declaration
7044 | N_Package_Body
7045 | N_Package_Body_Stub
7046 | N_Package_Declaration
7047 | N_Package_Instantiation
7048 | N_Package_Renaming_Declaration
7049 | N_Private_Extension_Declaration
7050 | N_Private_Type_Declaration
7051 | N_Procedure_Instantiation
7052 | N_Protected_Body
7053 | N_Protected_Body_Stub
7054 | N_Protected_Type_Declaration
7055 | N_Single_Task_Declaration
7056 | N_Subprogram_Body
7057 | N_Subprogram_Body_Stub
7058 | N_Subprogram_Declaration
7059 | N_Subprogram_Renaming_Declaration
7060 | N_Subtype_Declaration
7061 | N_Task_Body
7062 | N_Task_Body_Stub
7063 | N_Task_Type_Declaration
7065 -- Use clauses can appear in lists of declarations
7067 | N_Use_Package_Clause
7068 | N_Use_Type_Clause
7070 -- Freeze entity behaves like a declaration or statement
7072 | N_Freeze_Entity
7073 | N_Freeze_Generic_Entity
7075 -- Do not insert here if the item is not a list member (this
7076 -- happens for example with a triggering statement, and the
7077 -- proper approach is to insert before the entire select).
7079 if not Is_List_Member (P) then
7080 null;
7082 -- Do not insert if parent of P is an N_Component_Association
7083 -- node (i.e. we are in the context of an N_Aggregate or
7084 -- N_Extension_Aggregate node. In this case we want to insert
7085 -- before the entire aggregate.
7087 elsif Nkind (Parent (P)) = N_Component_Association then
7088 null;
7090 -- Do not insert if the parent of P is either an N_Variant node
7091 -- or an N_Record_Definition node, meaning in either case that
7092 -- P is a member of a component list, and that therefore the
7093 -- actions should be inserted outside the complete record
7094 -- declaration.
7096 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7097 null;
7099 -- Do not insert freeze nodes within the loop generated for
7100 -- an aggregate, because they may be elaborated too late for
7101 -- subsequent use in the back end: within a package spec the
7102 -- loop is part of the elaboration procedure and is only
7103 -- elaborated during the second pass.
7105 -- If the loop comes from source, or the entity is local to the
7106 -- loop itself it must remain within.
7108 elsif Nkind (Parent (P)) = N_Loop_Statement
7109 and then not Comes_From_Source (Parent (P))
7110 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7111 and then
7112 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7113 then
7114 null;
7116 -- Otherwise we can go ahead and do the insertion
7118 elsif P = Wrapped_Node then
7119 Store_Before_Actions_In_Scope (Ins_Actions);
7120 return;
7122 else
7123 Insert_List_Before_And_Analyze (P, Ins_Actions);
7124 return;
7125 end if;
7127 -- A special case, N_Raise_xxx_Error can act either as a statement
7128 -- or a subexpression. We tell the difference by looking at the
7129 -- Etype. It is set to Standard_Void_Type in the statement case.
7131 when N_Raise_xxx_Error =>
7132 if Etype (P) = Standard_Void_Type then
7133 if P = Wrapped_Node then
7134 Store_Before_Actions_In_Scope (Ins_Actions);
7135 else
7136 Insert_List_Before_And_Analyze (P, Ins_Actions);
7137 end if;
7139 return;
7141 -- In the subexpression case, keep climbing
7143 else
7144 null;
7145 end if;
7147 -- If a component association appears within a loop created for
7148 -- an array aggregate, attach the actions to the association so
7149 -- they can be subsequently inserted within the loop. For other
7150 -- component associations insert outside of the aggregate. For
7151 -- an association that will generate a loop, its Loop_Actions
7152 -- attribute is already initialized (see exp_aggr.adb).
7154 -- The list of Loop_Actions can in turn generate additional ones,
7155 -- that are inserted before the associated node. If the associated
7156 -- node is outside the aggregate, the new actions are collected
7157 -- at the end of the Loop_Actions, to respect the order in which
7158 -- they are to be elaborated.
7160 when N_Component_Association
7161 | N_Iterated_Component_Association
7163 if Nkind (Parent (P)) = N_Aggregate
7164 and then Present (Loop_Actions (P))
7165 then
7166 if Is_Empty_List (Loop_Actions (P)) then
7167 Set_Loop_Actions (P, Ins_Actions);
7168 Analyze_List (Ins_Actions);
7169 else
7170 declare
7171 Decl : Node_Id;
7173 begin
7174 -- Check whether these actions were generated by a
7175 -- declaration that is part of the Loop_Actions for
7176 -- the component_association.
7178 Decl := Assoc_Node;
7179 while Present (Decl) loop
7180 exit when Parent (Decl) = P
7181 and then Is_List_Member (Decl)
7182 and then
7183 List_Containing (Decl) = Loop_Actions (P);
7184 Decl := Parent (Decl);
7185 end loop;
7187 if Present (Decl) then
7188 Insert_List_Before_And_Analyze
7189 (Decl, Ins_Actions);
7190 else
7191 Insert_List_After_And_Analyze
7192 (Last (Loop_Actions (P)), Ins_Actions);
7193 end if;
7194 end;
7195 end if;
7197 return;
7199 else
7200 null;
7201 end if;
7203 -- Special case: an attribute denoting a procedure call
7205 when N_Attribute_Reference =>
7206 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7207 if P = Wrapped_Node then
7208 Store_Before_Actions_In_Scope (Ins_Actions);
7209 else
7210 Insert_List_Before_And_Analyze (P, Ins_Actions);
7211 end if;
7213 return;
7215 -- In the subexpression case, keep climbing
7217 else
7218 null;
7219 end if;
7221 -- Special case: a marker
7223 when N_Call_Marker
7224 | N_Variable_Reference_Marker
7226 if Is_List_Member (P) then
7227 Insert_List_Before_And_Analyze (P, Ins_Actions);
7228 return;
7229 end if;
7231 -- A contract node should not belong to the tree
7233 when N_Contract =>
7234 raise Program_Error;
7236 -- For all other node types, keep climbing tree
7238 when N_Abortable_Part
7239 | N_Accept_Alternative
7240 | N_Access_Definition
7241 | N_Access_Function_Definition
7242 | N_Access_Procedure_Definition
7243 | N_Access_To_Object_Definition
7244 | N_Aggregate
7245 | N_Allocator
7246 | N_Aspect_Specification
7247 | N_Case_Expression
7248 | N_Case_Statement_Alternative
7249 | N_Character_Literal
7250 | N_Compilation_Unit
7251 | N_Compilation_Unit_Aux
7252 | N_Component_Clause
7253 | N_Component_Declaration
7254 | N_Component_Definition
7255 | N_Component_List
7256 | N_Constrained_Array_Definition
7257 | N_Decimal_Fixed_Point_Definition
7258 | N_Defining_Character_Literal
7259 | N_Defining_Identifier
7260 | N_Defining_Operator_Symbol
7261 | N_Defining_Program_Unit_Name
7262 | N_Delay_Alternative
7263 | N_Delta_Aggregate
7264 | N_Delta_Constraint
7265 | N_Derived_Type_Definition
7266 | N_Designator
7267 | N_Digits_Constraint
7268 | N_Discriminant_Association
7269 | N_Discriminant_Specification
7270 | N_Empty
7271 | N_Entry_Body_Formal_Part
7272 | N_Entry_Call_Alternative
7273 | N_Entry_Declaration
7274 | N_Entry_Index_Specification
7275 | N_Enumeration_Type_Definition
7276 | N_Error
7277 | N_Exception_Handler
7278 | N_Expanded_Name
7279 | N_Explicit_Dereference
7280 | N_Extension_Aggregate
7281 | N_Floating_Point_Definition
7282 | N_Formal_Decimal_Fixed_Point_Definition
7283 | N_Formal_Derived_Type_Definition
7284 | N_Formal_Discrete_Type_Definition
7285 | N_Formal_Floating_Point_Definition
7286 | N_Formal_Modular_Type_Definition
7287 | N_Formal_Ordinary_Fixed_Point_Definition
7288 | N_Formal_Package_Declaration
7289 | N_Formal_Private_Type_Definition
7290 | N_Formal_Incomplete_Type_Definition
7291 | N_Formal_Signed_Integer_Type_Definition
7292 | N_Function_Call
7293 | N_Function_Specification
7294 | N_Generic_Association
7295 | N_Handled_Sequence_Of_Statements
7296 | N_Identifier
7297 | N_In
7298 | N_Index_Or_Discriminant_Constraint
7299 | N_Indexed_Component
7300 | N_Integer_Literal
7301 | N_Iterator_Specification
7302 | N_Itype_Reference
7303 | N_Label
7304 | N_Loop_Parameter_Specification
7305 | N_Mod_Clause
7306 | N_Modular_Type_Definition
7307 | N_Not_In
7308 | N_Null
7309 | N_Op_Abs
7310 | N_Op_Add
7311 | N_Op_And
7312 | N_Op_Concat
7313 | N_Op_Divide
7314 | N_Op_Eq
7315 | N_Op_Expon
7316 | N_Op_Ge
7317 | N_Op_Gt
7318 | N_Op_Le
7319 | N_Op_Lt
7320 | N_Op_Minus
7321 | N_Op_Mod
7322 | N_Op_Multiply
7323 | N_Op_Ne
7324 | N_Op_Not
7325 | N_Op_Or
7326 | N_Op_Plus
7327 | N_Op_Rem
7328 | N_Op_Rotate_Left
7329 | N_Op_Rotate_Right
7330 | N_Op_Shift_Left
7331 | N_Op_Shift_Right
7332 | N_Op_Shift_Right_Arithmetic
7333 | N_Op_Subtract
7334 | N_Op_Xor
7335 | N_Operator_Symbol
7336 | N_Ordinary_Fixed_Point_Definition
7337 | N_Others_Choice
7338 | N_Package_Specification
7339 | N_Parameter_Association
7340 | N_Parameter_Specification
7341 | N_Pop_Constraint_Error_Label
7342 | N_Pop_Program_Error_Label
7343 | N_Pop_Storage_Error_Label
7344 | N_Pragma_Argument_Association
7345 | N_Procedure_Specification
7346 | N_Protected_Definition
7347 | N_Push_Constraint_Error_Label
7348 | N_Push_Program_Error_Label
7349 | N_Push_Storage_Error_Label
7350 | N_Qualified_Expression
7351 | N_Quantified_Expression
7352 | N_Raise_Expression
7353 | N_Range
7354 | N_Range_Constraint
7355 | N_Real_Literal
7356 | N_Real_Range_Specification
7357 | N_Record_Definition
7358 | N_Reference
7359 | N_SCIL_Dispatch_Table_Tag_Init
7360 | N_SCIL_Dispatching_Call
7361 | N_SCIL_Membership_Test
7362 | N_Selected_Component
7363 | N_Signed_Integer_Type_Definition
7364 | N_Single_Protected_Declaration
7365 | N_Slice
7366 | N_String_Literal
7367 | N_Subtype_Indication
7368 | N_Subunit
7369 | N_Target_Name
7370 | N_Task_Definition
7371 | N_Terminate_Alternative
7372 | N_Triggering_Alternative
7373 | N_Type_Conversion
7374 | N_Unchecked_Expression
7375 | N_Unchecked_Type_Conversion
7376 | N_Unconstrained_Array_Definition
7377 | N_Unused_At_End
7378 | N_Unused_At_Start
7379 | N_Variant
7380 | N_Variant_Part
7381 | N_Validate_Unchecked_Conversion
7382 | N_With_Clause
7384 null;
7385 end case;
7387 -- If we fall through above tests, keep climbing tree
7389 N := P;
7391 if Nkind (Parent (N)) = N_Subunit then
7393 -- This is the proper body corresponding to a stub. Insertion must
7394 -- be done at the point of the stub, which is in the declarative
7395 -- part of the parent unit.
7397 P := Corresponding_Stub (Parent (N));
7399 else
7400 P := Parent (N);
7401 end if;
7402 end loop;
7403 end Insert_Actions;
7405 -- Version with check(s) suppressed
7407 procedure Insert_Actions
7408 (Assoc_Node : Node_Id;
7409 Ins_Actions : List_Id;
7410 Suppress : Check_Id)
7412 begin
7413 if Suppress = All_Checks then
7414 declare
7415 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7416 begin
7417 Scope_Suppress.Suppress := (others => True);
7418 Insert_Actions (Assoc_Node, Ins_Actions);
7419 Scope_Suppress.Suppress := Sva;
7420 end;
7422 else
7423 declare
7424 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7425 begin
7426 Scope_Suppress.Suppress (Suppress) := True;
7427 Insert_Actions (Assoc_Node, Ins_Actions);
7428 Scope_Suppress.Suppress (Suppress) := Svg;
7429 end;
7430 end if;
7431 end Insert_Actions;
7433 --------------------------
7434 -- Insert_Actions_After --
7435 --------------------------
7437 procedure Insert_Actions_After
7438 (Assoc_Node : Node_Id;
7439 Ins_Actions : List_Id)
7441 begin
7442 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7443 Store_After_Actions_In_Scope (Ins_Actions);
7444 else
7445 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7446 end if;
7447 end Insert_Actions_After;
7449 ------------------------
7450 -- Insert_Declaration --
7451 ------------------------
7453 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7454 P : Node_Id;
7456 begin
7457 pragma Assert (Nkind (N) in N_Subexpr);
7459 -- Climb until we find a procedure or a package
7461 P := N;
7462 loop
7463 pragma Assert (Present (Parent (P)));
7464 P := Parent (P);
7466 if Is_List_Member (P) then
7467 exit when Nkind_In (Parent (P), N_Package_Specification,
7468 N_Subprogram_Body);
7470 -- Special handling for handled sequence of statements, we must
7471 -- insert in the statements not the exception handlers!
7473 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7474 P := First (Statements (Parent (P)));
7475 exit;
7476 end if;
7477 end if;
7478 end loop;
7480 -- Now do the insertion
7482 Insert_Before (P, Decl);
7483 Analyze (Decl);
7484 end Insert_Declaration;
7486 ---------------------------------
7487 -- Insert_Library_Level_Action --
7488 ---------------------------------
7490 procedure Insert_Library_Level_Action (N : Node_Id) is
7491 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7493 begin
7494 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7495 -- And not Main_Unit as previously. If the main unit is a body,
7496 -- the scope needed to analyze the actions is the entity of the
7497 -- corresponding declaration.
7499 if No (Actions (Aux)) then
7500 Set_Actions (Aux, New_List (N));
7501 else
7502 Append (N, Actions (Aux));
7503 end if;
7505 Analyze (N);
7506 Pop_Scope;
7507 end Insert_Library_Level_Action;
7509 ----------------------------------
7510 -- Insert_Library_Level_Actions --
7511 ----------------------------------
7513 procedure Insert_Library_Level_Actions (L : List_Id) is
7514 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7516 begin
7517 if Is_Non_Empty_List (L) then
7518 Push_Scope (Cunit_Entity (Main_Unit));
7519 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7521 if No (Actions (Aux)) then
7522 Set_Actions (Aux, L);
7523 Analyze_List (L);
7524 else
7525 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7526 end if;
7528 Pop_Scope;
7529 end if;
7530 end Insert_Library_Level_Actions;
7532 ----------------------
7533 -- Inside_Init_Proc --
7534 ----------------------
7536 function Inside_Init_Proc return Boolean is
7537 S : Entity_Id;
7539 begin
7540 S := Current_Scope;
7541 while Present (S) and then S /= Standard_Standard loop
7542 if Is_Init_Proc (S) then
7543 return True;
7544 else
7545 S := Scope (S);
7546 end if;
7547 end loop;
7549 return False;
7550 end Inside_Init_Proc;
7552 ----------------------------
7553 -- Is_All_Null_Statements --
7554 ----------------------------
7556 function Is_All_Null_Statements (L : List_Id) return Boolean is
7557 Stm : Node_Id;
7559 begin
7560 Stm := First (L);
7561 while Present (Stm) loop
7562 if Nkind (Stm) /= N_Null_Statement then
7563 return False;
7564 end if;
7566 Next (Stm);
7567 end loop;
7569 return True;
7570 end Is_All_Null_Statements;
7572 --------------------------------------------------
7573 -- Is_Displacement_Of_Object_Or_Function_Result --
7574 --------------------------------------------------
7576 function Is_Displacement_Of_Object_Or_Function_Result
7577 (Obj_Id : Entity_Id) return Boolean
7579 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7580 -- Determine whether node N denotes a controlled function call
7582 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
7583 -- Determine whether node N denotes a generalized indexing form which
7584 -- involves a controlled result.
7586 function Is_Displace_Call (N : Node_Id) return Boolean;
7587 -- Determine whether node N denotes a call to Ada.Tags.Displace
7589 function Is_Source_Object (N : Node_Id) return Boolean;
7590 -- Determine whether a particular node denotes a source object
7592 function Strip (N : Node_Id) return Node_Id;
7593 -- Examine arbitrary node N by stripping various indirections and return
7594 -- the "real" node.
7596 ---------------------------------
7597 -- Is_Controlled_Function_Call --
7598 ---------------------------------
7600 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7601 Expr : Node_Id;
7603 begin
7604 -- When a function call appears in Object.Operation format, the
7605 -- original representation has several possible forms depending on
7606 -- the availability and form of actual parameters:
7608 -- Obj.Func N_Selected_Component
7609 -- Obj.Func (Actual) N_Indexed_Component
7610 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7611 -- N_Selected_Component
7613 Expr := Original_Node (N);
7614 loop
7615 if Nkind (Expr) = N_Function_Call then
7616 Expr := Name (Expr);
7618 -- "Obj.Func (Actual)" case
7620 elsif Nkind (Expr) = N_Indexed_Component then
7621 Expr := Prefix (Expr);
7623 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7625 elsif Nkind (Expr) = N_Selected_Component then
7626 Expr := Selector_Name (Expr);
7628 else
7629 exit;
7630 end if;
7631 end loop;
7633 return
7634 Nkind (Expr) in N_Has_Entity
7635 and then Present (Entity (Expr))
7636 and then Ekind (Entity (Expr)) = E_Function
7637 and then Needs_Finalization (Etype (Entity (Expr)));
7638 end Is_Controlled_Function_Call;
7640 ----------------------------
7641 -- Is_Controlled_Indexing --
7642 ----------------------------
7644 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
7645 Expr : constant Node_Id := Original_Node (N);
7647 begin
7648 return
7649 Nkind (Expr) = N_Indexed_Component
7650 and then Present (Generalized_Indexing (Expr))
7651 and then Needs_Finalization (Etype (Expr));
7652 end Is_Controlled_Indexing;
7654 ----------------------
7655 -- Is_Displace_Call --
7656 ----------------------
7658 function Is_Displace_Call (N : Node_Id) return Boolean is
7659 Call : constant Node_Id := Strip (N);
7661 begin
7662 return
7663 Present (Call)
7664 and then Nkind (Call) = N_Function_Call
7665 and then Nkind (Name (Call)) in N_Has_Entity
7666 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7667 end Is_Displace_Call;
7669 ----------------------
7670 -- Is_Source_Object --
7671 ----------------------
7673 function Is_Source_Object (N : Node_Id) return Boolean is
7674 Obj : constant Node_Id := Strip (N);
7676 begin
7677 return
7678 Present (Obj)
7679 and then Comes_From_Source (Obj)
7680 and then Nkind (Obj) in N_Has_Entity
7681 and then Is_Object (Entity (Obj));
7682 end Is_Source_Object;
7684 -----------
7685 -- Strip --
7686 -----------
7688 function Strip (N : Node_Id) return Node_Id is
7689 Result : Node_Id;
7691 begin
7692 Result := N;
7693 loop
7694 if Nkind (Result) = N_Explicit_Dereference then
7695 Result := Prefix (Result);
7697 elsif Nkind_In (Result, N_Type_Conversion,
7698 N_Unchecked_Type_Conversion)
7699 then
7700 Result := Expression (Result);
7702 else
7703 exit;
7704 end if;
7705 end loop;
7707 return Result;
7708 end Strip;
7710 -- Local variables
7712 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
7713 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7714 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
7715 Orig_Expr : Node_Id;
7717 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7719 begin
7720 -- Case 1:
7722 -- Obj : CW_Type := Function_Call (...);
7724 -- is rewritten into:
7726 -- Temp : ... := Function_Call (...)'reference;
7727 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7729 -- where the return type of the function and the class-wide type require
7730 -- dispatch table pointer displacement.
7732 -- Case 2:
7734 -- Obj : CW_Type := Container (...);
7736 -- is rewritten into:
7738 -- Temp : ... := Function_Call (Container, ...)'reference;
7739 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7741 -- where the container element type and the class-wide type require
7742 -- dispatch table pointer dispacement.
7744 -- Case 3:
7746 -- Obj : CW_Type := Src_Obj;
7748 -- is rewritten into:
7750 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7752 -- where the type of the source object and the class-wide type require
7753 -- dispatch table pointer displacement.
7755 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
7756 and then Is_Class_Wide_Type (Obj_Typ)
7757 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7758 and then Nkind (Orig_Decl) = N_Object_Declaration
7759 and then Comes_From_Source (Orig_Decl)
7760 then
7761 Orig_Expr := Expression (Orig_Decl);
7763 return
7764 Is_Controlled_Function_Call (Orig_Expr)
7765 or else Is_Controlled_Indexing (Orig_Expr)
7766 or else Is_Source_Object (Orig_Expr);
7767 end if;
7769 return False;
7770 end Is_Displacement_Of_Object_Or_Function_Result;
7772 ------------------------------
7773 -- Is_Finalizable_Transient --
7774 ------------------------------
7776 function Is_Finalizable_Transient
7777 (Decl : Node_Id;
7778 Rel_Node : Node_Id) return Boolean
7780 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7781 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7783 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7784 -- Determine whether transient object Trans_Id is initialized either
7785 -- by a function call which returns an access type or simply renames
7786 -- another pointer.
7788 function Initialized_By_Aliased_BIP_Func_Call
7789 (Trans_Id : Entity_Id) return Boolean;
7790 -- Determine whether transient object Trans_Id is initialized by a
7791 -- build-in-place function call where the BIPalloc parameter is of
7792 -- value 1 and BIPaccess is not null. This case creates an aliasing
7793 -- between the returned value and the value denoted by BIPaccess.
7795 function Is_Aliased
7796 (Trans_Id : Entity_Id;
7797 First_Stmt : Node_Id) return Boolean;
7798 -- Determine whether transient object Trans_Id has been renamed or
7799 -- aliased through 'reference in the statement list starting from
7800 -- First_Stmt.
7802 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7803 -- Determine whether transient object Trans_Id is allocated on the heap
7805 function Is_Iterated_Container
7806 (Trans_Id : Entity_Id;
7807 First_Stmt : Node_Id) return Boolean;
7808 -- Determine whether transient object Trans_Id denotes a container which
7809 -- is in the process of being iterated in the statement list starting
7810 -- from First_Stmt.
7812 ---------------------------
7813 -- Initialized_By_Access --
7814 ---------------------------
7816 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7817 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7819 begin
7820 return
7821 Present (Expr)
7822 and then Nkind (Expr) /= N_Reference
7823 and then Is_Access_Type (Etype (Expr));
7824 end Initialized_By_Access;
7826 ------------------------------------------
7827 -- Initialized_By_Aliased_BIP_Func_Call --
7828 ------------------------------------------
7830 function Initialized_By_Aliased_BIP_Func_Call
7831 (Trans_Id : Entity_Id) return Boolean
7833 Call : Node_Id := Expression (Parent (Trans_Id));
7835 begin
7836 -- Build-in-place calls usually appear in 'reference format
7838 if Nkind (Call) = N_Reference then
7839 Call := Prefix (Call);
7840 end if;
7842 Call := Unqual_Conv (Call);
7844 if Is_Build_In_Place_Function_Call (Call) then
7845 declare
7846 Access_Nam : Name_Id := No_Name;
7847 Access_OK : Boolean := False;
7848 Actual : Node_Id;
7849 Alloc_Nam : Name_Id := No_Name;
7850 Alloc_OK : Boolean := False;
7851 Formal : Node_Id;
7852 Func_Id : Entity_Id;
7853 Param : Node_Id;
7855 begin
7856 -- Examine all parameter associations of the function call
7858 Param := First (Parameter_Associations (Call));
7859 while Present (Param) loop
7860 if Nkind (Param) = N_Parameter_Association
7861 and then Nkind (Selector_Name (Param)) = N_Identifier
7862 then
7863 Actual := Explicit_Actual_Parameter (Param);
7864 Formal := Selector_Name (Param);
7866 -- Construct the names of formals BIPaccess and BIPalloc
7867 -- using the function name retrieved from an arbitrary
7868 -- formal.
7870 if Access_Nam = No_Name
7871 and then Alloc_Nam = No_Name
7872 and then Present (Entity (Formal))
7873 then
7874 Func_Id := Scope (Entity (Formal));
7876 Access_Nam :=
7877 New_External_Name (Chars (Func_Id),
7878 BIP_Formal_Suffix (BIP_Object_Access));
7880 Alloc_Nam :=
7881 New_External_Name (Chars (Func_Id),
7882 BIP_Formal_Suffix (BIP_Alloc_Form));
7883 end if;
7885 -- A match for BIPaccess => Temp has been found
7887 if Chars (Formal) = Access_Nam
7888 and then Nkind (Actual) /= N_Null
7889 then
7890 Access_OK := True;
7891 end if;
7893 -- A match for BIPalloc => 1 has been found
7895 if Chars (Formal) = Alloc_Nam
7896 and then Nkind (Actual) = N_Integer_Literal
7897 and then Intval (Actual) = Uint_1
7898 then
7899 Alloc_OK := True;
7900 end if;
7901 end if;
7903 Next (Param);
7904 end loop;
7906 return Access_OK and Alloc_OK;
7907 end;
7908 end if;
7910 return False;
7911 end Initialized_By_Aliased_BIP_Func_Call;
7913 ----------------
7914 -- Is_Aliased --
7915 ----------------
7917 function Is_Aliased
7918 (Trans_Id : Entity_Id;
7919 First_Stmt : Node_Id) return Boolean
7921 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7922 -- Given an object renaming declaration, retrieve the entity of the
7923 -- renamed name. Return Empty if the renamed name is anything other
7924 -- than a variable or a constant.
7926 -------------------------
7927 -- Find_Renamed_Object --
7928 -------------------------
7930 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7931 Ren_Obj : Node_Id := Empty;
7933 function Find_Object (N : Node_Id) return Traverse_Result;
7934 -- Try to detect an object which is either a constant or a
7935 -- variable.
7937 -----------------
7938 -- Find_Object --
7939 -----------------
7941 function Find_Object (N : Node_Id) return Traverse_Result is
7942 begin
7943 -- Stop the search once a constant or a variable has been
7944 -- detected.
7946 if Nkind (N) = N_Identifier
7947 and then Present (Entity (N))
7948 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7949 then
7950 Ren_Obj := Entity (N);
7951 return Abandon;
7952 end if;
7954 return OK;
7955 end Find_Object;
7957 procedure Search is new Traverse_Proc (Find_Object);
7959 -- Local variables
7961 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7963 -- Start of processing for Find_Renamed_Object
7965 begin
7966 -- Actions related to dispatching calls may appear as renamings of
7967 -- tags. Do not process this type of renaming because it does not
7968 -- use the actual value of the object.
7970 if not Is_RTE (Typ, RE_Tag_Ptr) then
7971 Search (Name (Ren_Decl));
7972 end if;
7974 return Ren_Obj;
7975 end Find_Renamed_Object;
7977 -- Local variables
7979 Expr : Node_Id;
7980 Ren_Obj : Entity_Id;
7981 Stmt : Node_Id;
7983 -- Start of processing for Is_Aliased
7985 begin
7986 -- A controlled transient object is not considered aliased when it
7987 -- appears inside an expression_with_actions node even when there are
7988 -- explicit aliases of it:
7990 -- do
7991 -- Trans_Id : Ctrl_Typ ...; -- transient object
7992 -- Alias : ... := Trans_Id; -- object is aliased
7993 -- Val : constant Boolean :=
7994 -- ... Alias ...; -- aliasing ends
7995 -- <finalize Trans_Id> -- object safe to finalize
7996 -- in Val end;
7998 -- Expansion ensures that all aliases are encapsulated in the actions
7999 -- list and do not leak to the expression by forcing the evaluation
8000 -- of the expression.
8002 if Nkind (Rel_Node) = N_Expression_With_Actions then
8003 return False;
8005 -- Otherwise examine the statements after the controlled transient
8006 -- object and look for various forms of aliasing.
8008 else
8009 Stmt := First_Stmt;
8010 while Present (Stmt) loop
8011 if Nkind (Stmt) = N_Object_Declaration then
8012 Expr := Expression (Stmt);
8014 -- Aliasing of the form:
8015 -- Obj : ... := Trans_Id'reference;
8017 if Present (Expr)
8018 and then Nkind (Expr) = N_Reference
8019 and then Nkind (Prefix (Expr)) = N_Identifier
8020 and then Entity (Prefix (Expr)) = Trans_Id
8021 then
8022 return True;
8023 end if;
8025 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8026 Ren_Obj := Find_Renamed_Object (Stmt);
8028 -- Aliasing of the form:
8029 -- Obj : ... renames ... Trans_Id ...;
8031 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8032 return True;
8033 end if;
8034 end if;
8036 Next (Stmt);
8037 end loop;
8039 return False;
8040 end if;
8041 end Is_Aliased;
8043 ------------------
8044 -- Is_Allocated --
8045 ------------------
8047 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8048 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8049 begin
8050 return
8051 Is_Access_Type (Etype (Trans_Id))
8052 and then Present (Expr)
8053 and then Nkind (Expr) = N_Allocator;
8054 end Is_Allocated;
8056 ---------------------------
8057 -- Is_Iterated_Container --
8058 ---------------------------
8060 function Is_Iterated_Container
8061 (Trans_Id : Entity_Id;
8062 First_Stmt : Node_Id) return Boolean
8064 Aspect : Node_Id;
8065 Call : Node_Id;
8066 Iter : Entity_Id;
8067 Param : Node_Id;
8068 Stmt : Node_Id;
8069 Typ : Entity_Id;
8071 begin
8072 -- It is not possible to iterate over containers in non-Ada 2012 code
8074 if Ada_Version < Ada_2012 then
8075 return False;
8076 end if;
8078 Typ := Etype (Trans_Id);
8080 -- Handle access type created for secondary stack use
8082 if Is_Access_Type (Typ) then
8083 Typ := Designated_Type (Typ);
8084 end if;
8086 -- Look for aspect Default_Iterator. It may be part of a type
8087 -- declaration for a container, or inherited from a base type
8088 -- or parent type.
8090 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8092 if Present (Aspect) then
8093 Iter := Entity (Aspect);
8095 -- Examine the statements following the container object and
8096 -- look for a call to the default iterate routine where the
8097 -- first parameter is the transient. Such a call appears as:
8099 -- It : Access_To_CW_Iterator :=
8100 -- Iterate (Tran_Id.all, ...)'reference;
8102 Stmt := First_Stmt;
8103 while Present (Stmt) loop
8105 -- Detect an object declaration which is initialized by a
8106 -- secondary stack function call.
8108 if Nkind (Stmt) = N_Object_Declaration
8109 and then Present (Expression (Stmt))
8110 and then Nkind (Expression (Stmt)) = N_Reference
8111 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8112 then
8113 Call := Prefix (Expression (Stmt));
8115 -- The call must invoke the default iterate routine of
8116 -- the container and the transient object must appear as
8117 -- the first actual parameter. Skip any calls whose names
8118 -- are not entities.
8120 if Is_Entity_Name (Name (Call))
8121 and then Entity (Name (Call)) = Iter
8122 and then Present (Parameter_Associations (Call))
8123 then
8124 Param := First (Parameter_Associations (Call));
8126 if Nkind (Param) = N_Explicit_Dereference
8127 and then Entity (Prefix (Param)) = Trans_Id
8128 then
8129 return True;
8130 end if;
8131 end if;
8132 end if;
8134 Next (Stmt);
8135 end loop;
8136 end if;
8138 return False;
8139 end Is_Iterated_Container;
8141 -- Local variables
8143 Desig : Entity_Id := Obj_Typ;
8145 -- Start of processing for Is_Finalizable_Transient
8147 begin
8148 -- Handle access types
8150 if Is_Access_Type (Desig) then
8151 Desig := Available_View (Designated_Type (Desig));
8152 end if;
8154 return
8155 Ekind_In (Obj_Id, E_Constant, E_Variable)
8156 and then Needs_Finalization (Desig)
8157 and then Requires_Transient_Scope (Desig)
8158 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8160 -- Do not consider a transient object that was already processed
8162 and then not Is_Finalized_Transient (Obj_Id)
8164 -- Do not consider renamed or 'reference-d transient objects because
8165 -- the act of renaming extends the object's lifetime.
8167 and then not Is_Aliased (Obj_Id, Decl)
8169 -- Do not consider transient objects allocated on the heap since
8170 -- they are attached to a finalization master.
8172 and then not Is_Allocated (Obj_Id)
8174 -- If the transient object is a pointer, check that it is not
8175 -- initialized by a function that returns a pointer or acts as a
8176 -- renaming of another pointer.
8178 and then
8179 (not Is_Access_Type (Obj_Typ)
8180 or else not Initialized_By_Access (Obj_Id))
8182 -- Do not consider transient objects which act as indirect aliases
8183 -- of build-in-place function results.
8185 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8187 -- Do not consider conversions of tags to class-wide types
8189 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8191 -- Do not consider iterators because those are treated as normal
8192 -- controlled objects and are processed by the usual finalization
8193 -- machinery. This avoids the double finalization of an iterator.
8195 and then not Is_Iterator (Desig)
8197 -- Do not consider containers in the context of iterator loops. Such
8198 -- transient objects must exist for as long as the loop is around,
8199 -- otherwise any operation carried out by the iterator will fail.
8201 and then not Is_Iterated_Container (Obj_Id, Decl);
8202 end Is_Finalizable_Transient;
8204 ---------------------------------
8205 -- Is_Fully_Repped_Tagged_Type --
8206 ---------------------------------
8208 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8209 U : constant Entity_Id := Underlying_Type (T);
8210 Comp : Entity_Id;
8212 begin
8213 if No (U) or else not Is_Tagged_Type (U) then
8214 return False;
8215 elsif Has_Discriminants (U) then
8216 return False;
8217 elsif not Has_Specified_Layout (U) then
8218 return False;
8219 end if;
8221 -- Here we have a tagged type, see if it has any unlayed out fields
8222 -- other than a possible tag and parent fields. If so, we return False.
8224 Comp := First_Component (U);
8225 while Present (Comp) loop
8226 if not Is_Tag (Comp)
8227 and then Chars (Comp) /= Name_uParent
8228 and then No (Component_Clause (Comp))
8229 then
8230 return False;
8231 else
8232 Next_Component (Comp);
8233 end if;
8234 end loop;
8236 -- All components are layed out
8238 return True;
8239 end Is_Fully_Repped_Tagged_Type;
8241 ----------------------------------
8242 -- Is_Library_Level_Tagged_Type --
8243 ----------------------------------
8245 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8246 begin
8247 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8248 end Is_Library_Level_Tagged_Type;
8250 --------------------------
8251 -- Is_Non_BIP_Func_Call --
8252 --------------------------
8254 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8255 begin
8256 -- The expected call is of the format
8258 -- Func_Call'reference
8260 return
8261 Nkind (Expr) = N_Reference
8262 and then Nkind (Prefix (Expr)) = N_Function_Call
8263 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8264 end Is_Non_BIP_Func_Call;
8266 ----------------------------------
8267 -- Is_Possibly_Unaligned_Object --
8268 ----------------------------------
8270 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8271 T : constant Entity_Id := Etype (N);
8273 begin
8274 -- If renamed object, apply test to underlying object
8276 if Is_Entity_Name (N)
8277 and then Is_Object (Entity (N))
8278 and then Present (Renamed_Object (Entity (N)))
8279 then
8280 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8281 end if;
8283 -- Tagged and controlled types and aliased types are always aligned, as
8284 -- are concurrent types.
8286 if Is_Aliased (T)
8287 or else Has_Controlled_Component (T)
8288 or else Is_Concurrent_Type (T)
8289 or else Is_Tagged_Type (T)
8290 or else Is_Controlled (T)
8291 then
8292 return False;
8293 end if;
8295 -- If this is an element of a packed array, may be unaligned
8297 if Is_Ref_To_Bit_Packed_Array (N) then
8298 return True;
8299 end if;
8301 -- Case of indexed component reference: test whether prefix is unaligned
8303 if Nkind (N) = N_Indexed_Component then
8304 return Is_Possibly_Unaligned_Object (Prefix (N));
8306 -- Case of selected component reference
8308 elsif Nkind (N) = N_Selected_Component then
8309 declare
8310 P : constant Node_Id := Prefix (N);
8311 C : constant Entity_Id := Entity (Selector_Name (N));
8312 M : Nat;
8313 S : Nat;
8315 begin
8316 -- If component reference is for an array with non-static bounds,
8317 -- then it is always aligned: we can only process unaligned arrays
8318 -- with static bounds (more precisely compile time known bounds).
8320 if Is_Array_Type (T)
8321 and then not Compile_Time_Known_Bounds (T)
8322 then
8323 return False;
8324 end if;
8326 -- If component is aliased, it is definitely properly aligned
8328 if Is_Aliased (C) then
8329 return False;
8330 end if;
8332 -- If component is for a type implemented as a scalar, and the
8333 -- record is packed, and the component is other than the first
8334 -- component of the record, then the component may be unaligned.
8336 if Is_Packed (Etype (P))
8337 and then Represented_As_Scalar (Etype (C))
8338 and then First_Entity (Scope (C)) /= C
8339 then
8340 return True;
8341 end if;
8343 -- Compute maximum possible alignment for T
8345 -- If alignment is known, then that settles things
8347 if Known_Alignment (T) then
8348 M := UI_To_Int (Alignment (T));
8350 -- If alignment is not known, tentatively set max alignment
8352 else
8353 M := Ttypes.Maximum_Alignment;
8355 -- We can reduce this if the Esize is known since the default
8356 -- alignment will never be more than the smallest power of 2
8357 -- that does not exceed this Esize value.
8359 if Known_Esize (T) then
8360 S := UI_To_Int (Esize (T));
8362 while (M / 2) >= S loop
8363 M := M / 2;
8364 end loop;
8365 end if;
8366 end if;
8368 -- The following code is historical, it used to be present but it
8369 -- is too cautious, because the front-end does not know the proper
8370 -- default alignments for the target. Also, if the alignment is
8371 -- not known, the front end can't know in any case. If a copy is
8372 -- needed, the back-end will take care of it. This whole section
8373 -- including this comment can be removed later ???
8375 -- If the component reference is for a record that has a specified
8376 -- alignment, and we either know it is too small, or cannot tell,
8377 -- then the component may be unaligned.
8379 -- What is the following commented out code ???
8381 -- if Known_Alignment (Etype (P))
8382 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8383 -- and then M > Alignment (Etype (P))
8384 -- then
8385 -- return True;
8386 -- end if;
8388 -- Case of component clause present which may specify an
8389 -- unaligned position.
8391 if Present (Component_Clause (C)) then
8393 -- Otherwise we can do a test to make sure that the actual
8394 -- start position in the record, and the length, are both
8395 -- consistent with the required alignment. If not, we know
8396 -- that we are unaligned.
8398 declare
8399 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8400 begin
8401 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
8402 or else Esize (C) mod Align_In_Bits /= 0
8403 then
8404 return True;
8405 end if;
8406 end;
8407 end if;
8409 -- Otherwise, for a component reference, test prefix
8411 return Is_Possibly_Unaligned_Object (P);
8412 end;
8414 -- If not a component reference, must be aligned
8416 else
8417 return False;
8418 end if;
8419 end Is_Possibly_Unaligned_Object;
8421 ---------------------------------
8422 -- Is_Possibly_Unaligned_Slice --
8423 ---------------------------------
8425 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8426 begin
8427 -- Go to renamed object
8429 if Is_Entity_Name (N)
8430 and then Is_Object (Entity (N))
8431 and then Present (Renamed_Object (Entity (N)))
8432 then
8433 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8434 end if;
8436 -- The reference must be a slice
8438 if Nkind (N) /= N_Slice then
8439 return False;
8440 end if;
8442 -- We only need to worry if the target has strict alignment
8444 if not Target_Strict_Alignment then
8445 return False;
8446 end if;
8448 -- If it is a slice, then look at the array type being sliced
8450 declare
8451 Sarr : constant Node_Id := Prefix (N);
8452 -- Prefix of the slice, i.e. the array being sliced
8454 Styp : constant Entity_Id := Etype (Prefix (N));
8455 -- Type of the array being sliced
8457 Pref : Node_Id;
8458 Ptyp : Entity_Id;
8460 begin
8461 -- The problems arise if the array object that is being sliced
8462 -- is a component of a record or array, and we cannot guarantee
8463 -- the alignment of the array within its containing object.
8465 -- To investigate this, we look at successive prefixes to see
8466 -- if we have a worrisome indexed or selected component.
8468 Pref := Sarr;
8469 loop
8470 -- Case of array is part of an indexed component reference
8472 if Nkind (Pref) = N_Indexed_Component then
8473 Ptyp := Etype (Prefix (Pref));
8475 -- The only problematic case is when the array is packed, in
8476 -- which case we really know nothing about the alignment of
8477 -- individual components.
8479 if Is_Bit_Packed_Array (Ptyp) then
8480 return True;
8481 end if;
8483 -- Case of array is part of a selected component reference
8485 elsif Nkind (Pref) = N_Selected_Component then
8486 Ptyp := Etype (Prefix (Pref));
8488 -- We are definitely in trouble if the record in question
8489 -- has an alignment, and either we know this alignment is
8490 -- inconsistent with the alignment of the slice, or we don't
8491 -- know what the alignment of the slice should be.
8493 if Known_Alignment (Ptyp)
8494 and then (Unknown_Alignment (Styp)
8495 or else Alignment (Styp) > Alignment (Ptyp))
8496 then
8497 return True;
8498 end if;
8500 -- We are in potential trouble if the record type is packed.
8501 -- We could special case when we know that the array is the
8502 -- first component, but that's not such a simple case ???
8504 if Is_Packed (Ptyp) then
8505 return True;
8506 end if;
8508 -- We are in trouble if there is a component clause, and
8509 -- either we do not know the alignment of the slice, or
8510 -- the alignment of the slice is inconsistent with the
8511 -- bit position specified by the component clause.
8513 declare
8514 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8515 begin
8516 if Present (Component_Clause (Field))
8517 and then
8518 (Unknown_Alignment (Styp)
8519 or else
8520 (Component_Bit_Offset (Field) mod
8521 (System_Storage_Unit * Alignment (Styp))) /= 0)
8522 then
8523 return True;
8524 end if;
8525 end;
8527 -- For cases other than selected or indexed components we know we
8528 -- are OK, since no issues arise over alignment.
8530 else
8531 return False;
8532 end if;
8534 -- We processed an indexed component or selected component
8535 -- reference that looked safe, so keep checking prefixes.
8537 Pref := Prefix (Pref);
8538 end loop;
8539 end;
8540 end Is_Possibly_Unaligned_Slice;
8542 -------------------------------
8543 -- Is_Related_To_Func_Return --
8544 -------------------------------
8546 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8547 Expr : constant Node_Id := Related_Expression (Id);
8548 begin
8549 return
8550 Present (Expr)
8551 and then Nkind (Expr) = N_Explicit_Dereference
8552 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8553 end Is_Related_To_Func_Return;
8555 --------------------------------
8556 -- Is_Ref_To_Bit_Packed_Array --
8557 --------------------------------
8559 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8560 Result : Boolean;
8561 Expr : Node_Id;
8563 begin
8564 if Is_Entity_Name (N)
8565 and then Is_Object (Entity (N))
8566 and then Present (Renamed_Object (Entity (N)))
8567 then
8568 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8569 end if;
8571 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8572 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8573 Result := True;
8574 else
8575 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8576 end if;
8578 if Result and then Nkind (N) = N_Indexed_Component then
8579 Expr := First (Expressions (N));
8580 while Present (Expr) loop
8581 Force_Evaluation (Expr);
8582 Next (Expr);
8583 end loop;
8584 end if;
8586 return Result;
8588 else
8589 return False;
8590 end if;
8591 end Is_Ref_To_Bit_Packed_Array;
8593 --------------------------------
8594 -- Is_Ref_To_Bit_Packed_Slice --
8595 --------------------------------
8597 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8598 begin
8599 if Nkind (N) = N_Type_Conversion then
8600 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8602 elsif Is_Entity_Name (N)
8603 and then Is_Object (Entity (N))
8604 and then Present (Renamed_Object (Entity (N)))
8605 then
8606 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8608 elsif Nkind (N) = N_Slice
8609 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8610 then
8611 return True;
8613 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8614 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8616 else
8617 return False;
8618 end if;
8619 end Is_Ref_To_Bit_Packed_Slice;
8621 -----------------------
8622 -- Is_Renamed_Object --
8623 -----------------------
8625 function Is_Renamed_Object (N : Node_Id) return Boolean is
8626 Pnod : constant Node_Id := Parent (N);
8627 Kind : constant Node_Kind := Nkind (Pnod);
8628 begin
8629 if Kind = N_Object_Renaming_Declaration then
8630 return True;
8631 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8632 return Is_Renamed_Object (Pnod);
8633 else
8634 return False;
8635 end if;
8636 end Is_Renamed_Object;
8638 --------------------------------------
8639 -- Is_Secondary_Stack_BIP_Func_Call --
8640 --------------------------------------
8642 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8643 Alloc_Nam : Name_Id := No_Name;
8644 Actual : Node_Id;
8645 Call : Node_Id := Expr;
8646 Formal : Node_Id;
8647 Param : Node_Id;
8649 begin
8650 -- Build-in-place calls usually appear in 'reference format. Note that
8651 -- the accessibility check machinery may add an extra 'reference due to
8652 -- side effect removal.
8654 while Nkind (Call) = N_Reference loop
8655 Call := Prefix (Call);
8656 end loop;
8658 Call := Unqual_Conv (Call);
8660 if Is_Build_In_Place_Function_Call (Call) then
8662 -- Examine all parameter associations of the function call
8664 Param := First (Parameter_Associations (Call));
8665 while Present (Param) loop
8666 if Nkind (Param) = N_Parameter_Association then
8667 Formal := Selector_Name (Param);
8668 Actual := Explicit_Actual_Parameter (Param);
8670 -- Construct the name of formal BIPalloc. It is much easier to
8671 -- extract the name of the function using an arbitrary formal's
8672 -- scope rather than the Name field of Call.
8674 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8675 Alloc_Nam :=
8676 New_External_Name
8677 (Chars (Scope (Entity (Formal))),
8678 BIP_Formal_Suffix (BIP_Alloc_Form));
8679 end if;
8681 -- A match for BIPalloc => 2 has been found
8683 if Chars (Formal) = Alloc_Nam
8684 and then Nkind (Actual) = N_Integer_Literal
8685 and then Intval (Actual) = Uint_2
8686 then
8687 return True;
8688 end if;
8689 end if;
8691 Next (Param);
8692 end loop;
8693 end if;
8695 return False;
8696 end Is_Secondary_Stack_BIP_Func_Call;
8698 -------------------------------------
8699 -- Is_Tag_To_Class_Wide_Conversion --
8700 -------------------------------------
8702 function Is_Tag_To_Class_Wide_Conversion
8703 (Obj_Id : Entity_Id) return Boolean
8705 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8707 begin
8708 return
8709 Is_Class_Wide_Type (Etype (Obj_Id))
8710 and then Present (Expr)
8711 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8712 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8713 end Is_Tag_To_Class_Wide_Conversion;
8715 ----------------------------
8716 -- Is_Untagged_Derivation --
8717 ----------------------------
8719 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8720 begin
8721 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8722 or else
8723 (Is_Private_Type (T) and then Present (Full_View (T))
8724 and then not Is_Tagged_Type (Full_View (T))
8725 and then Is_Derived_Type (Full_View (T))
8726 and then Etype (Full_View (T)) /= T);
8727 end Is_Untagged_Derivation;
8729 ------------------------------------
8730 -- Is_Untagged_Private_Derivation --
8731 ------------------------------------
8733 function Is_Untagged_Private_Derivation
8734 (Priv_Typ : Entity_Id;
8735 Full_Typ : Entity_Id) return Boolean
8737 begin
8738 return
8739 Present (Priv_Typ)
8740 and then Is_Untagged_Derivation (Priv_Typ)
8741 and then Is_Private_Type (Etype (Priv_Typ))
8742 and then Present (Full_Typ)
8743 and then Is_Itype (Full_Typ);
8744 end Is_Untagged_Private_Derivation;
8746 ------------------------------
8747 -- Is_Verifiable_DIC_Pragma --
8748 ------------------------------
8750 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
8751 Args : constant List_Id := Pragma_Argument_Associations (Prag);
8753 begin
8754 -- To qualify as verifiable, a DIC pragma must have a non-null argument
8756 return
8757 Present (Args)
8758 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
8759 end Is_Verifiable_DIC_Pragma;
8761 ---------------------------
8762 -- Is_Volatile_Reference --
8763 ---------------------------
8765 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8766 begin
8767 -- Only source references are to be treated as volatile, internally
8768 -- generated stuff cannot have volatile external effects.
8770 if not Comes_From_Source (N) then
8771 return False;
8773 -- Never true for reference to a type
8775 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8776 return False;
8778 -- Never true for a compile time known constant
8780 elsif Compile_Time_Known_Value (N) then
8781 return False;
8783 -- True if object reference with volatile type
8785 elsif Is_Volatile_Object (N) then
8786 return True;
8788 -- True if reference to volatile entity
8790 elsif Is_Entity_Name (N) then
8791 return Treat_As_Volatile (Entity (N));
8793 -- True for slice of volatile array
8795 elsif Nkind (N) = N_Slice then
8796 return Is_Volatile_Reference (Prefix (N));
8798 -- True if volatile component
8800 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8801 if (Is_Entity_Name (Prefix (N))
8802 and then Has_Volatile_Components (Entity (Prefix (N))))
8803 or else (Present (Etype (Prefix (N)))
8804 and then Has_Volatile_Components (Etype (Prefix (N))))
8805 then
8806 return True;
8807 else
8808 return Is_Volatile_Reference (Prefix (N));
8809 end if;
8811 -- Otherwise false
8813 else
8814 return False;
8815 end if;
8816 end Is_Volatile_Reference;
8818 --------------------
8819 -- Kill_Dead_Code --
8820 --------------------
8822 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8823 W : Boolean := Warn;
8824 -- Set False if warnings suppressed
8826 begin
8827 if Present (N) then
8828 Remove_Warning_Messages (N);
8830 -- Update the internal structures of the ABE mechanism in case the
8831 -- dead node is an elaboration scenario.
8833 Kill_Elaboration_Scenario (N);
8835 -- Generate warning if appropriate
8837 if W then
8839 -- We suppress the warning if this code is under control of an
8840 -- if statement, whose condition is a simple identifier, and
8841 -- either we are in an instance, or warnings off is set for this
8842 -- identifier. The reason for killing it in the instance case is
8843 -- that it is common and reasonable for code to be deleted in
8844 -- instances for various reasons.
8846 -- Could we use Is_Statically_Unevaluated here???
8848 if Nkind (Parent (N)) = N_If_Statement then
8849 declare
8850 C : constant Node_Id := Condition (Parent (N));
8851 begin
8852 if Nkind (C) = N_Identifier
8853 and then
8854 (In_Instance
8855 or else (Present (Entity (C))
8856 and then Has_Warnings_Off (Entity (C))))
8857 then
8858 W := False;
8859 end if;
8860 end;
8861 end if;
8863 -- Generate warning if not suppressed
8865 if W then
8866 Error_Msg_F
8867 ("?t?this code can never be executed and has been deleted!",
8869 end if;
8870 end if;
8872 -- Recurse into block statements and bodies to process declarations
8873 -- and statements.
8875 if Nkind (N) = N_Block_Statement
8876 or else Nkind (N) = N_Subprogram_Body
8877 or else Nkind (N) = N_Package_Body
8878 then
8879 Kill_Dead_Code (Declarations (N), False);
8880 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8882 if Nkind (N) = N_Subprogram_Body then
8883 Set_Is_Eliminated (Defining_Entity (N));
8884 end if;
8886 elsif Nkind (N) = N_Package_Declaration then
8887 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8888 Kill_Dead_Code (Private_Declarations (Specification (N)));
8890 -- ??? After this point, Delete_Tree has been called on all
8891 -- declarations in Specification (N), so references to entities
8892 -- therein look suspicious.
8894 declare
8895 E : Entity_Id := First_Entity (Defining_Entity (N));
8897 begin
8898 while Present (E) loop
8899 if Ekind (E) = E_Operator then
8900 Set_Is_Eliminated (E);
8901 end if;
8903 Next_Entity (E);
8904 end loop;
8905 end;
8907 -- Recurse into composite statement to kill individual statements in
8908 -- particular instantiations.
8910 elsif Nkind (N) = N_If_Statement then
8911 Kill_Dead_Code (Then_Statements (N));
8912 Kill_Dead_Code (Elsif_Parts (N));
8913 Kill_Dead_Code (Else_Statements (N));
8915 elsif Nkind (N) = N_Loop_Statement then
8916 Kill_Dead_Code (Statements (N));
8918 elsif Nkind (N) = N_Case_Statement then
8919 declare
8920 Alt : Node_Id;
8921 begin
8922 Alt := First (Alternatives (N));
8923 while Present (Alt) loop
8924 Kill_Dead_Code (Statements (Alt));
8925 Next (Alt);
8926 end loop;
8927 end;
8929 elsif Nkind (N) = N_Case_Statement_Alternative then
8930 Kill_Dead_Code (Statements (N));
8932 -- Deal with dead instances caused by deleting instantiations
8934 elsif Nkind (N) in N_Generic_Instantiation then
8935 Remove_Dead_Instance (N);
8936 end if;
8937 end if;
8938 end Kill_Dead_Code;
8940 -- Case where argument is a list of nodes to be killed
8942 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8943 N : Node_Id;
8944 W : Boolean;
8946 begin
8947 W := Warn;
8949 if Is_Non_Empty_List (L) then
8950 N := First (L);
8951 while Present (N) loop
8952 Kill_Dead_Code (N, W);
8953 W := False;
8954 Next (N);
8955 end loop;
8956 end if;
8957 end Kill_Dead_Code;
8959 ------------------------
8960 -- Known_Non_Negative --
8961 ------------------------
8963 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8964 begin
8965 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8966 return True;
8968 else
8969 declare
8970 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8971 begin
8972 return
8973 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8974 end;
8975 end if;
8976 end Known_Non_Negative;
8978 -----------------------------
8979 -- Make_CW_Equivalent_Type --
8980 -----------------------------
8982 -- Create a record type used as an equivalent of any member of the class
8983 -- which takes its size from exp.
8985 -- Generate the following code:
8987 -- type Equiv_T is record
8988 -- _parent : T (List of discriminant constraints taken from Exp);
8989 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8990 -- end Equiv_T;
8992 -- ??? Note that this type does not guarantee same alignment as all
8993 -- derived types
8995 function Make_CW_Equivalent_Type
8996 (T : Entity_Id;
8997 E : Node_Id) return Entity_Id
8999 Loc : constant Source_Ptr := Sloc (E);
9000 Root_Typ : constant Entity_Id := Root_Type (T);
9001 List_Def : constant List_Id := Empty_List;
9002 Comp_List : constant List_Id := New_List;
9003 Equiv_Type : Entity_Id;
9004 Range_Type : Entity_Id;
9005 Str_Type : Entity_Id;
9006 Constr_Root : Entity_Id;
9007 Sizexpr : Node_Id;
9009 begin
9010 -- If the root type is already constrained, there are no discriminants
9011 -- in the expression.
9013 if not Has_Discriminants (Root_Typ)
9014 or else Is_Constrained (Root_Typ)
9015 then
9016 Constr_Root := Root_Typ;
9018 -- At this point in the expansion, non-limited view of the type
9019 -- must be available, otherwise the error will be reported later.
9021 if From_Limited_With (Constr_Root)
9022 and then Present (Non_Limited_View (Constr_Root))
9023 then
9024 Constr_Root := Non_Limited_View (Constr_Root);
9025 end if;
9027 else
9028 Constr_Root := Make_Temporary (Loc, 'R');
9030 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9032 Append_To (List_Def,
9033 Make_Subtype_Declaration (Loc,
9034 Defining_Identifier => Constr_Root,
9035 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9036 end if;
9038 -- Generate the range subtype declaration
9040 Range_Type := Make_Temporary (Loc, 'G');
9042 if not Is_Interface (Root_Typ) then
9044 -- subtype rg__xx is
9045 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9047 Sizexpr :=
9048 Make_Op_Subtract (Loc,
9049 Left_Opnd =>
9050 Make_Attribute_Reference (Loc,
9051 Prefix =>
9052 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9053 Attribute_Name => Name_Size),
9054 Right_Opnd =>
9055 Make_Attribute_Reference (Loc,
9056 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9057 Attribute_Name => Name_Object_Size));
9058 else
9059 -- subtype rg__xx is
9060 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9062 Sizexpr :=
9063 Make_Attribute_Reference (Loc,
9064 Prefix =>
9065 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9066 Attribute_Name => Name_Size);
9067 end if;
9069 Set_Paren_Count (Sizexpr, 1);
9071 Append_To (List_Def,
9072 Make_Subtype_Declaration (Loc,
9073 Defining_Identifier => Range_Type,
9074 Subtype_Indication =>
9075 Make_Subtype_Indication (Loc,
9076 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9077 Constraint => Make_Range_Constraint (Loc,
9078 Range_Expression =>
9079 Make_Range (Loc,
9080 Low_Bound => Make_Integer_Literal (Loc, 1),
9081 High_Bound =>
9082 Make_Op_Divide (Loc,
9083 Left_Opnd => Sizexpr,
9084 Right_Opnd => Make_Integer_Literal (Loc,
9085 Intval => System_Storage_Unit)))))));
9087 -- subtype str__nn is Storage_Array (rg__x);
9089 Str_Type := Make_Temporary (Loc, 'S');
9090 Append_To (List_Def,
9091 Make_Subtype_Declaration (Loc,
9092 Defining_Identifier => Str_Type,
9093 Subtype_Indication =>
9094 Make_Subtype_Indication (Loc,
9095 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9096 Constraint =>
9097 Make_Index_Or_Discriminant_Constraint (Loc,
9098 Constraints =>
9099 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9101 -- type Equiv_T is record
9102 -- [ _parent : Tnn; ]
9103 -- E : Str_Type;
9104 -- end Equiv_T;
9106 Equiv_Type := Make_Temporary (Loc, 'T');
9107 Set_Ekind (Equiv_Type, E_Record_Type);
9108 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9110 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9111 -- treatment for this type. In particular, even though _parent's type
9112 -- is a controlled type or contains controlled components, we do not
9113 -- want to set Has_Controlled_Component on it to avoid making it gain
9114 -- an unwanted _controller component.
9116 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9118 -- A class-wide equivalent type does not require initialization
9120 Set_Suppress_Initialization (Equiv_Type);
9122 if not Is_Interface (Root_Typ) then
9123 Append_To (Comp_List,
9124 Make_Component_Declaration (Loc,
9125 Defining_Identifier =>
9126 Make_Defining_Identifier (Loc, Name_uParent),
9127 Component_Definition =>
9128 Make_Component_Definition (Loc,
9129 Aliased_Present => False,
9130 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9131 end if;
9133 Append_To (Comp_List,
9134 Make_Component_Declaration (Loc,
9135 Defining_Identifier => Make_Temporary (Loc, 'C'),
9136 Component_Definition =>
9137 Make_Component_Definition (Loc,
9138 Aliased_Present => False,
9139 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9141 Append_To (List_Def,
9142 Make_Full_Type_Declaration (Loc,
9143 Defining_Identifier => Equiv_Type,
9144 Type_Definition =>
9145 Make_Record_Definition (Loc,
9146 Component_List =>
9147 Make_Component_List (Loc,
9148 Component_Items => Comp_List,
9149 Variant_Part => Empty))));
9151 -- Suppress all checks during the analysis of the expanded code to avoid
9152 -- the generation of spurious warnings under ZFP run-time.
9154 Insert_Actions (E, List_Def, Suppress => All_Checks);
9155 return Equiv_Type;
9156 end Make_CW_Equivalent_Type;
9158 -------------------------
9159 -- Make_Invariant_Call --
9160 -------------------------
9162 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9163 Loc : constant Source_Ptr := Sloc (Expr);
9164 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9166 Proc_Id : Entity_Id;
9168 begin
9169 pragma Assert (Has_Invariants (Typ));
9171 Proc_Id := Invariant_Procedure (Typ);
9172 pragma Assert (Present (Proc_Id));
9174 return
9175 Make_Procedure_Call_Statement (Loc,
9176 Name => New_Occurrence_Of (Proc_Id, Loc),
9177 Parameter_Associations => New_List (Relocate_Node (Expr)));
9178 end Make_Invariant_Call;
9180 ------------------------
9181 -- Make_Literal_Range --
9182 ------------------------
9184 function Make_Literal_Range
9185 (Loc : Source_Ptr;
9186 Literal_Typ : Entity_Id) return Node_Id
9188 Lo : constant Node_Id :=
9189 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9190 Index : constant Entity_Id := Etype (Lo);
9191 Length_Expr : constant Node_Id :=
9192 Make_Op_Subtract (Loc,
9193 Left_Opnd =>
9194 Make_Integer_Literal (Loc,
9195 Intval => String_Literal_Length (Literal_Typ)),
9196 Right_Opnd => Make_Integer_Literal (Loc, 1));
9198 Hi : Node_Id;
9200 begin
9201 Set_Analyzed (Lo, False);
9203 if Is_Integer_Type (Index) then
9204 Hi :=
9205 Make_Op_Add (Loc,
9206 Left_Opnd => New_Copy_Tree (Lo),
9207 Right_Opnd => Length_Expr);
9208 else
9209 Hi :=
9210 Make_Attribute_Reference (Loc,
9211 Attribute_Name => Name_Val,
9212 Prefix => New_Occurrence_Of (Index, Loc),
9213 Expressions => New_List (
9214 Make_Op_Add (Loc,
9215 Left_Opnd =>
9216 Make_Attribute_Reference (Loc,
9217 Attribute_Name => Name_Pos,
9218 Prefix => New_Occurrence_Of (Index, Loc),
9219 Expressions => New_List (New_Copy_Tree (Lo))),
9220 Right_Opnd => Length_Expr)));
9221 end if;
9223 return
9224 Make_Range (Loc,
9225 Low_Bound => Lo,
9226 High_Bound => Hi);
9227 end Make_Literal_Range;
9229 --------------------------
9230 -- Make_Non_Empty_Check --
9231 --------------------------
9233 function Make_Non_Empty_Check
9234 (Loc : Source_Ptr;
9235 N : Node_Id) return Node_Id
9237 begin
9238 return
9239 Make_Op_Ne (Loc,
9240 Left_Opnd =>
9241 Make_Attribute_Reference (Loc,
9242 Attribute_Name => Name_Length,
9243 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9244 Right_Opnd =>
9245 Make_Integer_Literal (Loc, 0));
9246 end Make_Non_Empty_Check;
9248 -------------------------
9249 -- Make_Predicate_Call --
9250 -------------------------
9252 -- WARNING: This routine manages Ghost regions. Return statements must be
9253 -- replaced by gotos which jump to the end of the routine and restore the
9254 -- Ghost mode.
9256 function Make_Predicate_Call
9257 (Typ : Entity_Id;
9258 Expr : Node_Id;
9259 Mem : Boolean := False) return Node_Id
9261 Loc : constant Source_Ptr := Sloc (Expr);
9263 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9264 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9265 -- Save the Ghost-related attributes to restore on exit
9267 Call : Node_Id;
9268 Func_Id : Entity_Id;
9270 begin
9271 Func_Id := Predicate_Function (Typ);
9272 pragma Assert (Present (Func_Id));
9274 -- The related type may be subject to pragma Ghost. Set the mode now to
9275 -- ensure that the call is properly marked as Ghost.
9277 Set_Ghost_Mode (Typ);
9279 -- Call special membership version if requested and available
9281 if Mem and then Present (Predicate_Function_M (Typ)) then
9282 Func_Id := Predicate_Function_M (Typ);
9283 end if;
9285 -- Case of calling normal predicate function
9287 -- If the type is tagged, the expression may be class-wide, in which
9288 -- case it has to be converted to its root type, given that the
9289 -- generated predicate function is not dispatching.
9291 if Is_Tagged_Type (Typ) then
9292 Call :=
9293 Make_Function_Call (Loc,
9294 Name => New_Occurrence_Of (Func_Id, Loc),
9295 Parameter_Associations =>
9296 New_List (Convert_To (Typ, Relocate_Node (Expr))));
9297 else
9298 Call :=
9299 Make_Function_Call (Loc,
9300 Name => New_Occurrence_Of (Func_Id, Loc),
9301 Parameter_Associations => New_List (Relocate_Node (Expr)));
9302 end if;
9304 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9306 return Call;
9307 end Make_Predicate_Call;
9309 --------------------------
9310 -- Make_Predicate_Check --
9311 --------------------------
9313 function Make_Predicate_Check
9314 (Typ : Entity_Id;
9315 Expr : Node_Id) return Node_Id
9317 Loc : constant Source_Ptr := Sloc (Expr);
9319 procedure Add_Failure_Expression (Args : List_Id);
9320 -- Add the failure expression of pragma Predicate_Failure (if any) to
9321 -- list Args.
9323 ----------------------------
9324 -- Add_Failure_Expression --
9325 ----------------------------
9327 procedure Add_Failure_Expression (Args : List_Id) is
9328 function Failure_Expression return Node_Id;
9329 pragma Inline (Failure_Expression);
9330 -- Find aspect or pragma Predicate_Failure that applies to type Typ
9331 -- and return its expression. Return Empty if no such annotation is
9332 -- available.
9334 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean;
9335 pragma Inline (Is_OK_PF_Aspect);
9336 -- Determine whether aspect Asp is a suitable Predicate_Failure
9337 -- aspect that applies to type Typ.
9339 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean;
9340 pragma Inline (Is_OK_PF_Pragma);
9341 -- Determine whether pragma Prag is a suitable Predicate_Failure
9342 -- pragma that applies to type Typ.
9344 procedure Replace_Subtype_Reference (N : Node_Id);
9345 -- Replace the current instance of type Typ denoted by N with
9346 -- expression Expr.
9348 ------------------------
9349 -- Failure_Expression --
9350 ------------------------
9352 function Failure_Expression return Node_Id is
9353 Item : Node_Id;
9355 begin
9356 -- The management of the rep item chain involves "inheritance" of
9357 -- parent type chains. If a parent [sub]type is already subject to
9358 -- pragma Predicate_Failure, then the pragma will also appear in
9359 -- the chain of the child [sub]type, which in turn may possess a
9360 -- pragma of its own. Avoid order-dependent issues by inspecting
9361 -- the rep item chain directly. Note that routine Get_Pragma may
9362 -- return a parent pragma.
9364 Item := First_Rep_Item (Typ);
9365 while Present (Item) loop
9367 -- Predicate_Failure appears as an aspect
9369 if Nkind (Item) = N_Aspect_Specification
9370 and then Is_OK_PF_Aspect (Item)
9371 then
9372 return Expression (Item);
9374 -- Predicate_Failure appears as a pragma
9376 elsif Nkind (Item) = N_Pragma
9377 and then Is_OK_PF_Pragma (Item)
9378 then
9379 return
9380 Get_Pragma_Arg
9381 (Next (First (Pragma_Argument_Associations (Item))));
9382 end if;
9384 Item := Next_Rep_Item (Item);
9385 end loop;
9387 return Empty;
9388 end Failure_Expression;
9390 ---------------------
9391 -- Is_OK_PF_Aspect --
9392 ---------------------
9394 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean is
9395 begin
9396 -- To qualify, the aspect must apply to the type subjected to the
9397 -- predicate check.
9399 return
9400 Chars (Identifier (Asp)) = Name_Predicate_Failure
9401 and then Present (Entity (Asp))
9402 and then Entity (Asp) = Typ;
9403 end Is_OK_PF_Aspect;
9405 ---------------------
9406 -- Is_OK_PF_Pragma --
9407 ---------------------
9409 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean is
9410 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9411 Typ_Arg : Node_Id;
9413 begin
9414 -- Nothing to do when the pragma does not denote Predicate_Failure
9416 if Pragma_Name (Prag) /= Name_Predicate_Failure then
9417 return False;
9419 -- Nothing to do when the pragma lacks arguments, in which case it
9420 -- is illegal.
9422 elsif No (Args) or else Is_Empty_List (Args) then
9423 return False;
9424 end if;
9426 Typ_Arg := Get_Pragma_Arg (First (Args));
9428 -- To qualify, the local name argument of the pragma must denote
9429 -- the type subjected to the predicate check.
9431 return
9432 Is_Entity_Name (Typ_Arg)
9433 and then Present (Entity (Typ_Arg))
9434 and then Entity (Typ_Arg) = Typ;
9435 end Is_OK_PF_Pragma;
9437 --------------------------------
9438 -- Replace_Subtype_Reference --
9439 --------------------------------
9441 procedure Replace_Subtype_Reference (N : Node_Id) is
9442 begin
9443 Rewrite (N, New_Copy_Tree (Expr));
9445 -- We want to treat the node as if it comes from source, so that
9446 -- ASIS will not ignore it.
9448 Set_Comes_From_Source (N, True);
9449 end Replace_Subtype_Reference;
9451 procedure Replace_Subtype_References is
9452 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9454 -- Local variables
9456 PF_Expr : constant Node_Id := Failure_Expression;
9457 Expr : Node_Id;
9459 -- Start of processing for Add_Failure_Expression
9461 begin
9462 if Present (PF_Expr) then
9464 -- Replace any occurrences of the current instance of the type
9465 -- with the object subjected to the predicate check.
9467 Expr := New_Copy_Tree (PF_Expr);
9468 Replace_Subtype_References (Expr, Typ);
9470 -- The failure expression appears as the third argument of the
9471 -- Check pragma.
9473 Append_To (Args,
9474 Make_Pragma_Argument_Association (Loc,
9475 Expression => Expr));
9476 end if;
9477 end Add_Failure_Expression;
9479 -- Local variables
9481 Args : List_Id;
9482 Nam : Name_Id;
9484 -- Start of processing for Make_Predicate_Check
9486 begin
9487 -- If predicate checks are suppressed, then return a null statement. For
9488 -- this call, we check only the scope setting. If the caller wants to
9489 -- check a specific entity's setting, they must do it manually.
9491 if Predicate_Checks_Suppressed (Empty) then
9492 return Make_Null_Statement (Loc);
9493 end if;
9495 -- Do not generate a check within an internal subprogram (stream
9496 -- functions and the like, including including predicate functions).
9498 if Within_Internal_Subprogram then
9499 return Make_Null_Statement (Loc);
9500 end if;
9502 -- Compute proper name to use, we need to get this right so that the
9503 -- right set of check policies apply to the Check pragma we are making.
9505 if Has_Dynamic_Predicate_Aspect (Typ) then
9506 Nam := Name_Dynamic_Predicate;
9507 elsif Has_Static_Predicate_Aspect (Typ) then
9508 Nam := Name_Static_Predicate;
9509 else
9510 Nam := Name_Predicate;
9511 end if;
9513 Args := New_List (
9514 Make_Pragma_Argument_Association (Loc,
9515 Expression => Make_Identifier (Loc, Nam)),
9516 Make_Pragma_Argument_Association (Loc,
9517 Expression => Make_Predicate_Call (Typ, Expr)));
9519 -- If the subtype is subject to pragma Predicate_Failure, add the
9520 -- failure expression as an additional parameter.
9522 Add_Failure_Expression (Args);
9524 return
9525 Make_Pragma (Loc,
9526 Chars => Name_Check,
9527 Pragma_Argument_Associations => Args);
9528 end Make_Predicate_Check;
9530 ----------------------------
9531 -- Make_Subtype_From_Expr --
9532 ----------------------------
9534 -- 1. If Expr is an unconstrained array expression, creates
9535 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9537 -- 2. If Expr is a unconstrained discriminated type expression, creates
9538 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9540 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9542 function Make_Subtype_From_Expr
9543 (E : Node_Id;
9544 Unc_Typ : Entity_Id;
9545 Related_Id : Entity_Id := Empty) return Node_Id
9547 List_Constr : constant List_Id := New_List;
9548 Loc : constant Source_Ptr := Sloc (E);
9549 D : Entity_Id;
9550 Full_Exp : Node_Id;
9551 Full_Subtyp : Entity_Id;
9552 High_Bound : Entity_Id;
9553 Index_Typ : Entity_Id;
9554 Low_Bound : Entity_Id;
9555 Priv_Subtyp : Entity_Id;
9556 Utyp : Entity_Id;
9558 begin
9559 if Is_Private_Type (Unc_Typ)
9560 and then Has_Unknown_Discriminants (Unc_Typ)
9561 then
9562 -- The caller requests a unique external name for both the private
9563 -- and the full subtype.
9565 if Present (Related_Id) then
9566 Full_Subtyp :=
9567 Make_Defining_Identifier (Loc,
9568 Chars => New_External_Name (Chars (Related_Id), 'C'));
9569 Priv_Subtyp :=
9570 Make_Defining_Identifier (Loc,
9571 Chars => New_External_Name (Chars (Related_Id), 'P'));
9573 else
9574 Full_Subtyp := Make_Temporary (Loc, 'C');
9575 Priv_Subtyp := Make_Temporary (Loc, 'P');
9576 end if;
9578 -- Prepare the subtype completion. Use the base type to find the
9579 -- underlying type because the type may be a generic actual or an
9580 -- explicit subtype.
9582 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9584 Full_Exp :=
9585 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9586 Set_Parent (Full_Exp, Parent (E));
9588 Insert_Action (E,
9589 Make_Subtype_Declaration (Loc,
9590 Defining_Identifier => Full_Subtyp,
9591 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9593 -- Define the dummy private subtype
9595 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9596 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9597 Set_Scope (Priv_Subtyp, Full_Subtyp);
9598 Set_Is_Constrained (Priv_Subtyp);
9599 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9600 Set_Is_Itype (Priv_Subtyp);
9601 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9603 if Is_Tagged_Type (Priv_Subtyp) then
9604 Set_Class_Wide_Type
9605 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9606 Set_Direct_Primitive_Operations (Priv_Subtyp,
9607 Direct_Primitive_Operations (Unc_Typ));
9608 end if;
9610 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9612 return New_Occurrence_Of (Priv_Subtyp, Loc);
9614 elsif Is_Array_Type (Unc_Typ) then
9615 Index_Typ := First_Index (Unc_Typ);
9616 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9618 -- Capture the bounds of each index constraint in case the context
9619 -- is an object declaration of an unconstrained type initialized
9620 -- by a function call:
9622 -- Obj : Unconstr_Typ := Func_Call;
9624 -- This scenario requires secondary scope management and the index
9625 -- constraint cannot depend on the temporary used to capture the
9626 -- result of the function call.
9628 -- SS_Mark;
9629 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9630 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9631 -- Obj : S := Temp.all;
9632 -- SS_Release; -- Temp is gone at this point, bounds of S are
9633 -- -- non existent.
9635 -- Generate:
9636 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9638 Low_Bound := Make_Temporary (Loc, 'B');
9639 Insert_Action (E,
9640 Make_Object_Declaration (Loc,
9641 Defining_Identifier => Low_Bound,
9642 Object_Definition =>
9643 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9644 Constant_Present => True,
9645 Expression =>
9646 Make_Attribute_Reference (Loc,
9647 Prefix => Duplicate_Subexpr_No_Checks (E),
9648 Attribute_Name => Name_First,
9649 Expressions => New_List (
9650 Make_Integer_Literal (Loc, J)))));
9652 -- Generate:
9653 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9655 High_Bound := Make_Temporary (Loc, 'B');
9656 Insert_Action (E,
9657 Make_Object_Declaration (Loc,
9658 Defining_Identifier => High_Bound,
9659 Object_Definition =>
9660 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9661 Constant_Present => True,
9662 Expression =>
9663 Make_Attribute_Reference (Loc,
9664 Prefix => Duplicate_Subexpr_No_Checks (E),
9665 Attribute_Name => Name_Last,
9666 Expressions => New_List (
9667 Make_Integer_Literal (Loc, J)))));
9669 Append_To (List_Constr,
9670 Make_Range (Loc,
9671 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9672 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9674 Index_Typ := Next_Index (Index_Typ);
9675 end loop;
9677 elsif Is_Class_Wide_Type (Unc_Typ) then
9678 declare
9679 CW_Subtype : Entity_Id;
9680 EQ_Typ : Entity_Id := Empty;
9682 begin
9683 -- A class-wide equivalent type is not needed on VM targets
9684 -- because the VM back-ends handle the class-wide object
9685 -- initialization itself (and doesn't need or want the
9686 -- additional intermediate type to handle the assignment).
9688 if Expander_Active and then Tagged_Type_Expansion then
9690 -- If this is the class-wide type of a completion that is a
9691 -- record subtype, set the type of the class-wide type to be
9692 -- the full base type, for use in the expanded code for the
9693 -- equivalent type. Should this be done earlier when the
9694 -- completion is analyzed ???
9696 if Is_Private_Type (Etype (Unc_Typ))
9697 and then
9698 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9699 then
9700 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9701 end if;
9703 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9704 end if;
9706 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9707 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9708 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9710 return New_Occurrence_Of (CW_Subtype, Loc);
9711 end;
9713 -- Indefinite record type with discriminants
9715 else
9716 D := First_Discriminant (Unc_Typ);
9717 while Present (D) loop
9718 Append_To (List_Constr,
9719 Make_Selected_Component (Loc,
9720 Prefix => Duplicate_Subexpr_No_Checks (E),
9721 Selector_Name => New_Occurrence_Of (D, Loc)));
9723 Next_Discriminant (D);
9724 end loop;
9725 end if;
9727 return
9728 Make_Subtype_Indication (Loc,
9729 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9730 Constraint =>
9731 Make_Index_Or_Discriminant_Constraint (Loc,
9732 Constraints => List_Constr));
9733 end Make_Subtype_From_Expr;
9735 ---------------
9736 -- Map_Types --
9737 ---------------
9739 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9741 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9742 -- avoid deep indentation of code.
9744 -- NOTE: Routines which deal with discriminant mapping operate on the
9745 -- [underlying/record] full view of various types because those views
9746 -- contain all discriminants and stored constraints.
9748 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9749 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9750 -- overriding chain starting from Prim whose dispatching type is parent
9751 -- type Par_Typ and add a mapping between the result and primitive Prim.
9753 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9754 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9755 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9756 -- if no such primitive is available.
9758 function Build_Chain
9759 (Par_Typ : Entity_Id;
9760 Deriv_Typ : Entity_Id) return Elist_Id;
9761 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9762 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9763 -- list has the form:
9765 -- head tail
9766 -- v v
9767 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9769 -- Note that Par_Typ is not part of the resulting derivation chain
9771 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9772 -- Return the view of type Typ which could potentially contains either
9773 -- the discriminants or stored constraints of the type.
9775 function Find_Discriminant_Value
9776 (Discr : Entity_Id;
9777 Par_Typ : Entity_Id;
9778 Deriv_Typ : Entity_Id;
9779 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9780 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9781 -- in the derivation chain starting from parent type Par_Typ leading to
9782 -- derived type Deriv_Typ. The returned value is one of the following:
9784 -- * An entity which is either a discriminant or a non-discriminant
9785 -- name, and renames/constraints Discr.
9787 -- * An expression which constraints Discr
9789 -- Typ_Elmt is an element of the derivation chain created by routine
9790 -- Build_Chain and denotes the current ancestor being examined.
9792 procedure Map_Discriminants
9793 (Par_Typ : Entity_Id;
9794 Deriv_Typ : Entity_Id);
9795 -- Map each discriminant of type Par_Typ to a meaningful constraint
9796 -- from the point of view of type Deriv_Typ.
9798 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9799 -- Map each primitive of type Par_Typ to a corresponding primitive of
9800 -- type Deriv_Typ.
9802 -------------------
9803 -- Add_Primitive --
9804 -------------------
9806 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9807 Par_Prim : Entity_Id;
9809 begin
9810 -- Inspect the inheritance chain through the Alias attribute and the
9811 -- overriding chain through the Overridden_Operation looking for an
9812 -- ancestor primitive with the appropriate dispatching type.
9814 Par_Prim := Prim;
9815 while Present (Par_Prim) loop
9816 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9817 Par_Prim := Ancestor_Primitive (Par_Prim);
9818 end loop;
9820 -- Create a mapping of the form:
9822 -- parent type primitive -> derived type primitive
9824 if Present (Par_Prim) then
9825 Type_Map.Set (Par_Prim, Prim);
9826 end if;
9827 end Add_Primitive;
9829 ------------------------
9830 -- Ancestor_Primitive --
9831 ------------------------
9833 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9834 Inher_Prim : constant Entity_Id := Alias (Subp);
9835 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9837 begin
9838 -- The current subprogram overrides an ancestor primitive
9840 if Present (Over_Prim) then
9841 return Over_Prim;
9843 -- The current subprogram is an internally generated alias of an
9844 -- inherited ancestor primitive.
9846 elsif Present (Inher_Prim) then
9847 return Inher_Prim;
9849 -- Otherwise the current subprogram is the root of the inheritance or
9850 -- overriding chain.
9852 else
9853 return Empty;
9854 end if;
9855 end Ancestor_Primitive;
9857 -----------------
9858 -- Build_Chain --
9859 -----------------
9861 function Build_Chain
9862 (Par_Typ : Entity_Id;
9863 Deriv_Typ : Entity_Id) return Elist_Id
9865 Anc_Typ : Entity_Id;
9866 Chain : Elist_Id;
9867 Curr_Typ : Entity_Id;
9869 begin
9870 Chain := New_Elmt_List;
9872 -- Add the derived type to the derivation chain
9874 Prepend_Elmt (Deriv_Typ, Chain);
9876 -- Examine all ancestors starting from the derived type climbing
9877 -- towards parent type Par_Typ.
9879 Curr_Typ := Deriv_Typ;
9880 loop
9881 -- Handle the case where the current type is a record which
9882 -- derives from a subtype.
9884 -- subtype Sub_Typ is Par_Typ ...
9885 -- type Deriv_Typ is Sub_Typ ...
9887 if Ekind (Curr_Typ) = E_Record_Type
9888 and then Present (Parent_Subtype (Curr_Typ))
9889 then
9890 Anc_Typ := Parent_Subtype (Curr_Typ);
9892 -- Handle the case where the current type is a record subtype of
9893 -- another subtype.
9895 -- subtype Sub_Typ1 is Par_Typ ...
9896 -- subtype Sub_Typ2 is Sub_Typ1 ...
9898 elsif Ekind (Curr_Typ) = E_Record_Subtype
9899 and then Present (Cloned_Subtype (Curr_Typ))
9900 then
9901 Anc_Typ := Cloned_Subtype (Curr_Typ);
9903 -- Otherwise use the direct parent type
9905 else
9906 Anc_Typ := Etype (Curr_Typ);
9907 end if;
9909 -- Use the first subtype when dealing with itypes
9911 if Is_Itype (Anc_Typ) then
9912 Anc_Typ := First_Subtype (Anc_Typ);
9913 end if;
9915 -- Work with the view which contains the discriminants and stored
9916 -- constraints.
9918 Anc_Typ := Discriminated_View (Anc_Typ);
9920 -- Stop the climb when either the parent type has been reached or
9921 -- there are no more ancestors left to examine.
9923 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9925 Prepend_Unique_Elmt (Anc_Typ, Chain);
9926 Curr_Typ := Anc_Typ;
9927 end loop;
9929 return Chain;
9930 end Build_Chain;
9932 ------------------------
9933 -- Discriminated_View --
9934 ------------------------
9936 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9937 T : Entity_Id;
9939 begin
9940 T := Typ;
9942 -- Use the [underlying] full view when dealing with private types
9943 -- because the view contains all inherited discriminants or stored
9944 -- constraints.
9946 if Is_Private_Type (T) then
9947 if Present (Underlying_Full_View (T)) then
9948 T := Underlying_Full_View (T);
9950 elsif Present (Full_View (T)) then
9951 T := Full_View (T);
9952 end if;
9953 end if;
9955 -- Use the underlying record view when the type is an extenstion of
9956 -- a parent type with unknown discriminants because the view contains
9957 -- all inherited discriminants or stored constraints.
9959 if Ekind (T) = E_Record_Type
9960 and then Present (Underlying_Record_View (T))
9961 then
9962 T := Underlying_Record_View (T);
9963 end if;
9965 return T;
9966 end Discriminated_View;
9968 -----------------------------
9969 -- Find_Discriminant_Value --
9970 -----------------------------
9972 function Find_Discriminant_Value
9973 (Discr : Entity_Id;
9974 Par_Typ : Entity_Id;
9975 Deriv_Typ : Entity_Id;
9976 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9978 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9979 Typ : constant Entity_Id := Node (Typ_Elmt);
9981 function Find_Constraint_Value
9982 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
9983 -- Given constraint Constr, find what it denotes. This is either:
9985 -- * An entity which is either a discriminant or a name
9987 -- * An expression
9989 ---------------------------
9990 -- Find_Constraint_Value --
9991 ---------------------------
9993 function Find_Constraint_Value
9994 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
9996 begin
9997 if Nkind (Constr) in N_Entity then
9999 -- The constraint denotes a discriminant of the curren type
10000 -- which renames the ancestor discriminant:
10002 -- vv
10003 -- type Typ (D1 : ...; DN : ...) is
10004 -- new Anc (Discr => D1) with ...
10005 -- ^^
10007 if Ekind (Constr) = E_Discriminant then
10009 -- The discriminant belongs to derived type Deriv_Typ. This
10010 -- is the final value for the ancestor discriminant as the
10011 -- derivations chain has been fully exhausted.
10013 if Typ = Deriv_Typ then
10014 return Constr;
10016 -- Otherwise the discriminant may be renamed or constrained
10017 -- at a lower level. Continue looking down the derivation
10018 -- chain.
10020 else
10021 return
10022 Find_Discriminant_Value
10023 (Discr => Constr,
10024 Par_Typ => Par_Typ,
10025 Deriv_Typ => Deriv_Typ,
10026 Typ_Elmt => Next_Elmt (Typ_Elmt));
10027 end if;
10029 -- Otherwise the constraint denotes a reference to some name
10030 -- which results in a Girder discriminant:
10032 -- vvvv
10033 -- Name : ...;
10034 -- type Typ (D1 : ...; DN : ...) is
10035 -- new Anc (Discr => Name) with ...
10036 -- ^^^^
10038 -- Return the name as this is the proper constraint of the
10039 -- discriminant.
10041 else
10042 return Constr;
10043 end if;
10045 -- The constraint denotes a reference to a name
10047 elsif Is_Entity_Name (Constr) then
10048 return Find_Constraint_Value (Entity (Constr));
10050 -- Otherwise the current constraint is an expression which yields
10051 -- a Girder discriminant:
10053 -- type Typ (D1 : ...; DN : ...) is
10054 -- new Anc (Discr => <expression>) with ...
10055 -- ^^^^^^^^^^
10057 -- Return the expression as this is the proper constraint of the
10058 -- discriminant.
10060 else
10061 return Constr;
10062 end if;
10063 end Find_Constraint_Value;
10065 -- Local variables
10067 Constrs : constant Elist_Id := Stored_Constraint (Typ);
10069 Constr_Elmt : Elmt_Id;
10070 Pos : Uint;
10071 Typ_Discr : Entity_Id;
10073 -- Start of processing for Find_Discriminant_Value
10075 begin
10076 -- The algorithm for finding the value of a discriminant works as
10077 -- follows. First, it recreates the derivation chain from Par_Typ
10078 -- to Deriv_Typ as a list:
10080 -- Par_Typ (shown for completeness)
10081 -- v
10082 -- Ancestor_N <-- head of chain
10083 -- v
10084 -- Ancestor_1
10085 -- v
10086 -- Deriv_Typ <-- tail of chain
10088 -- The algorithm then traces the fate of a parent discriminant down
10089 -- the derivation chain. At each derivation level, the discriminant
10090 -- may be either inherited or constrained.
10092 -- 1) Discriminant is inherited: there are two cases, depending on
10093 -- which type is inheriting.
10095 -- 1.1) Deriv_Typ is inheriting:
10097 -- type Ancestor (D_1 : ...) is tagged ...
10098 -- type Deriv_Typ is new Ancestor ...
10100 -- In this case the inherited discriminant is the final value of
10101 -- the parent discriminant because the end of the derivation chain
10102 -- has been reached.
10104 -- 1.2) Some other type is inheriting:
10106 -- type Ancestor_1 (D_1 : ...) is tagged ...
10107 -- type Ancestor_2 is new Ancestor_1 ...
10109 -- In this case the algorithm continues to trace the fate of the
10110 -- inherited discriminant down the derivation chain because it may
10111 -- be further inherited or constrained.
10113 -- 2) Discriminant is constrained: there are three cases, depending
10114 -- on what the constraint is.
10116 -- 2.1) The constraint is another discriminant (aka renaming):
10118 -- type Ancestor_1 (D_1 : ...) is tagged ...
10119 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10121 -- In this case the constraining discriminant becomes the one to
10122 -- track down the derivation chain. The algorithm already knows
10123 -- that D_2 constrains D_1, therefore if the algorithm finds the
10124 -- value of D_2, then this would also be the value for D_1.
10126 -- 2.2) The constraint is a name (aka Girder):
10128 -- Name : ...
10129 -- type Ancestor_1 (D_1 : ...) is tagged ...
10130 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10132 -- In this case the name is the final value of D_1 because the
10133 -- discriminant cannot be further constrained.
10135 -- 2.3) The constraint is an expression (aka Girder):
10137 -- type Ancestor_1 (D_1 : ...) is tagged ...
10138 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10140 -- Similar to 2.2, the expression is the final value of D_1
10142 Pos := Uint_1;
10144 -- When a derived type constrains its parent type, all constaints
10145 -- appear in the Stored_Constraint list. Examine the list looking
10146 -- for a positional match.
10148 if Present (Constrs) then
10149 Constr_Elmt := First_Elmt (Constrs);
10150 while Present (Constr_Elmt) loop
10152 -- The position of the current constraint matches that of the
10153 -- ancestor discriminant.
10155 if Pos = Discr_Pos then
10156 return Find_Constraint_Value (Node (Constr_Elmt));
10157 end if;
10159 Next_Elmt (Constr_Elmt);
10160 Pos := Pos + 1;
10161 end loop;
10163 -- Otherwise the derived type does not constraint its parent type in
10164 -- which case it inherits the parent discriminants.
10166 else
10167 Typ_Discr := First_Discriminant (Typ);
10168 while Present (Typ_Discr) loop
10170 -- The position of the current discriminant matches that of the
10171 -- ancestor discriminant.
10173 if Pos = Discr_Pos then
10174 return Find_Constraint_Value (Typ_Discr);
10175 end if;
10177 Next_Discriminant (Typ_Discr);
10178 Pos := Pos + 1;
10179 end loop;
10180 end if;
10182 -- A discriminant must always have a corresponding value. This is
10183 -- either another discriminant, a name, or an expression. If this
10184 -- point is reached, them most likely the derivation chain employs
10185 -- the wrong views of types.
10187 pragma Assert (False);
10189 return Empty;
10190 end Find_Discriminant_Value;
10192 -----------------------
10193 -- Map_Discriminants --
10194 -----------------------
10196 procedure Map_Discriminants
10197 (Par_Typ : Entity_Id;
10198 Deriv_Typ : Entity_Id)
10200 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10202 Discr : Entity_Id;
10203 Discr_Val : Node_Or_Entity_Id;
10205 begin
10206 -- Examine each discriminant of parent type Par_Typ and find a
10207 -- suitable value for it from the point of view of derived type
10208 -- Deriv_Typ.
10210 if Has_Discriminants (Par_Typ) then
10211 Discr := First_Discriminant (Par_Typ);
10212 while Present (Discr) loop
10213 Discr_Val :=
10214 Find_Discriminant_Value
10215 (Discr => Discr,
10216 Par_Typ => Par_Typ,
10217 Deriv_Typ => Deriv_Typ,
10218 Typ_Elmt => First_Elmt (Deriv_Chain));
10220 -- Create a mapping of the form:
10222 -- parent type discriminant -> value
10224 Type_Map.Set (Discr, Discr_Val);
10226 Next_Discriminant (Discr);
10227 end loop;
10228 end if;
10229 end Map_Discriminants;
10231 --------------------
10232 -- Map_Primitives --
10233 --------------------
10235 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10236 Deriv_Prim : Entity_Id;
10237 Par_Prim : Entity_Id;
10238 Par_Prims : Elist_Id;
10239 Prim_Elmt : Elmt_Id;
10241 begin
10242 -- Inspect the primitives of the derived type and determine whether
10243 -- they relate to the primitives of the parent type. If there is a
10244 -- meaningful relation, create a mapping of the form:
10246 -- parent type primitive -> perived type primitive
10248 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10249 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10250 while Present (Prim_Elmt) loop
10251 Deriv_Prim := Node (Prim_Elmt);
10253 if Is_Subprogram (Deriv_Prim)
10254 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10255 then
10256 Add_Primitive (Deriv_Prim, Par_Typ);
10257 end if;
10259 Next_Elmt (Prim_Elmt);
10260 end loop;
10261 end if;
10263 -- If the parent operation is an interface operation, the overriding
10264 -- indicator is not present. Instead, we get from the interface
10265 -- operation the primitive of the current type that implements it.
10267 if Is_Interface (Par_Typ) then
10268 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10270 if Present (Par_Prims) then
10271 Prim_Elmt := First_Elmt (Par_Prims);
10273 while Present (Prim_Elmt) loop
10274 Par_Prim := Node (Prim_Elmt);
10275 Deriv_Prim :=
10276 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10278 if Present (Deriv_Prim) then
10279 Type_Map.Set (Par_Prim, Deriv_Prim);
10280 end if;
10282 Next_Elmt (Prim_Elmt);
10283 end loop;
10284 end if;
10285 end if;
10286 end Map_Primitives;
10288 -- Start of processing for Map_Types
10290 begin
10291 -- Nothing to do if there are no types to work with
10293 if No (Parent_Type) or else No (Derived_Type) then
10294 return;
10296 -- Nothing to do if the mapping already exists
10298 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10299 return;
10301 -- Nothing to do if both types are not tagged. Note that untagged types
10302 -- do not have primitive operations and their discriminants are already
10303 -- handled by gigi.
10305 elsif not Is_Tagged_Type (Parent_Type)
10306 or else not Is_Tagged_Type (Derived_Type)
10307 then
10308 return;
10309 end if;
10311 -- Create a mapping of the form
10313 -- parent type -> derived type
10315 -- to prevent any subsequent attempts to produce the same relations
10317 Type_Map.Set (Parent_Type, Derived_Type);
10319 -- Create mappings of the form
10321 -- parent type discriminant -> derived type discriminant
10322 -- <or>
10323 -- parent type discriminant -> constraint
10325 -- Note that mapping of discriminants breaks privacy because it needs to
10326 -- work with those views which contains the discriminants and any stored
10327 -- constraints.
10329 Map_Discriminants
10330 (Par_Typ => Discriminated_View (Parent_Type),
10331 Deriv_Typ => Discriminated_View (Derived_Type));
10333 -- Create mappings of the form
10335 -- parent type primitive -> derived type primitive
10337 Map_Primitives
10338 (Par_Typ => Parent_Type,
10339 Deriv_Typ => Derived_Type);
10340 end Map_Types;
10342 ----------------------------
10343 -- Matching_Standard_Type --
10344 ----------------------------
10346 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10347 pragma Assert (Is_Scalar_Type (Typ));
10348 Siz : constant Uint := Esize (Typ);
10350 begin
10351 -- Floating-point cases
10353 if Is_Floating_Point_Type (Typ) then
10354 if Siz <= Esize (Standard_Short_Float) then
10355 return Standard_Short_Float;
10356 elsif Siz <= Esize (Standard_Float) then
10357 return Standard_Float;
10358 elsif Siz <= Esize (Standard_Long_Float) then
10359 return Standard_Long_Float;
10360 elsif Siz <= Esize (Standard_Long_Long_Float) then
10361 return Standard_Long_Long_Float;
10362 else
10363 raise Program_Error;
10364 end if;
10366 -- Integer cases (includes fixed-point types)
10368 -- Unsigned integer cases (includes normal enumeration types)
10370 elsif Is_Unsigned_Type (Typ) then
10371 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10372 return Standard_Short_Short_Unsigned;
10373 elsif Siz <= Esize (Standard_Short_Unsigned) then
10374 return Standard_Short_Unsigned;
10375 elsif Siz <= Esize (Standard_Unsigned) then
10376 return Standard_Unsigned;
10377 elsif Siz <= Esize (Standard_Long_Unsigned) then
10378 return Standard_Long_Unsigned;
10379 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10380 return Standard_Long_Long_Unsigned;
10381 else
10382 raise Program_Error;
10383 end if;
10385 -- Signed integer cases
10387 else
10388 if Siz <= Esize (Standard_Short_Short_Integer) then
10389 return Standard_Short_Short_Integer;
10390 elsif Siz <= Esize (Standard_Short_Integer) then
10391 return Standard_Short_Integer;
10392 elsif Siz <= Esize (Standard_Integer) then
10393 return Standard_Integer;
10394 elsif Siz <= Esize (Standard_Long_Integer) then
10395 return Standard_Long_Integer;
10396 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10397 return Standard_Long_Long_Integer;
10398 else
10399 raise Program_Error;
10400 end if;
10401 end if;
10402 end Matching_Standard_Type;
10404 -----------------------------
10405 -- May_Generate_Large_Temp --
10406 -----------------------------
10408 -- At the current time, the only types that we return False for (i.e. where
10409 -- we decide we know they cannot generate large temps) are ones where we
10410 -- know the size is 256 bits or less at compile time, and we are still not
10411 -- doing a thorough job on arrays and records ???
10413 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10414 begin
10415 if not Size_Known_At_Compile_Time (Typ) then
10416 return False;
10418 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10419 return False;
10421 elsif Is_Array_Type (Typ)
10422 and then Present (Packed_Array_Impl_Type (Typ))
10423 then
10424 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10426 -- We could do more here to find other small types ???
10428 else
10429 return True;
10430 end if;
10431 end May_Generate_Large_Temp;
10433 ------------------------
10434 -- Needs_Finalization --
10435 ------------------------
10437 function Needs_Finalization (Typ : Entity_Id) return Boolean is
10438 function Has_Some_Controlled_Component
10439 (Input_Typ : Entity_Id) return Boolean;
10440 -- Determine whether type Input_Typ has at least one controlled
10441 -- component.
10443 -----------------------------------
10444 -- Has_Some_Controlled_Component --
10445 -----------------------------------
10447 function Has_Some_Controlled_Component
10448 (Input_Typ : Entity_Id) return Boolean
10450 Comp : Entity_Id;
10452 begin
10453 -- When a type is already frozen and has at least one controlled
10454 -- component, or is manually decorated, it is sufficient to inspect
10455 -- flag Has_Controlled_Component.
10457 if Has_Controlled_Component (Input_Typ) then
10458 return True;
10460 -- Otherwise inspect the internals of the type
10462 elsif not Is_Frozen (Input_Typ) then
10463 if Is_Array_Type (Input_Typ) then
10464 return Needs_Finalization (Component_Type (Input_Typ));
10466 elsif Is_Record_Type (Input_Typ) then
10467 Comp := First_Component (Input_Typ);
10468 while Present (Comp) loop
10469 if Needs_Finalization (Etype (Comp)) then
10470 return True;
10471 end if;
10473 Next_Component (Comp);
10474 end loop;
10475 end if;
10476 end if;
10478 return False;
10479 end Has_Some_Controlled_Component;
10481 -- Start of processing for Needs_Finalization
10483 begin
10484 -- Certain run-time configurations and targets do not provide support
10485 -- for controlled types.
10487 if Restriction_Active (No_Finalization) then
10488 return False;
10490 -- C++ types are not considered controlled. It is assumed that the non-
10491 -- Ada side will handle their clean up.
10493 elsif Convention (Typ) = Convention_CPP then
10494 return False;
10496 -- Class-wide types are treated as controlled because derivations from
10497 -- the root type may introduce controlled components.
10499 elsif Is_Class_Wide_Type (Typ) then
10500 return True;
10502 -- Concurrent types are controlled as long as their corresponding record
10503 -- is controlled.
10505 elsif Is_Concurrent_Type (Typ)
10506 and then Present (Corresponding_Record_Type (Typ))
10507 and then Needs_Finalization (Corresponding_Record_Type (Typ))
10508 then
10509 return True;
10511 -- Otherwise the type is controlled when it is either derived from type
10512 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10513 -- contains at least one controlled component.
10515 else
10516 return
10517 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
10518 end if;
10519 end Needs_Finalization;
10521 ----------------------------
10522 -- Needs_Constant_Address --
10523 ----------------------------
10525 function Needs_Constant_Address
10526 (Decl : Node_Id;
10527 Typ : Entity_Id) return Boolean
10529 begin
10530 -- If we have no initialization of any kind, then we don't need to place
10531 -- any restrictions on the address clause, because the object will be
10532 -- elaborated after the address clause is evaluated. This happens if the
10533 -- declaration has no initial expression, or the type has no implicit
10534 -- initialization, or the object is imported.
10536 -- The same holds for all initialized scalar types and all access types.
10537 -- Packed bit arrays of size up to 64 are represented using a modular
10538 -- type with an initialization (to zero) and can be processed like other
10539 -- initialized scalar types.
10541 -- If the type is controlled, code to attach the object to a
10542 -- finalization chain is generated at the point of declaration, and
10543 -- therefore the elaboration of the object cannot be delayed: the
10544 -- address expression must be a constant.
10546 if No (Expression (Decl))
10547 and then not Needs_Finalization (Typ)
10548 and then
10549 (not Has_Non_Null_Base_Init_Proc (Typ)
10550 or else Is_Imported (Defining_Identifier (Decl)))
10551 then
10552 return False;
10554 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10555 or else Is_Access_Type (Typ)
10556 or else
10557 (Is_Bit_Packed_Array (Typ)
10558 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10559 then
10560 return False;
10562 else
10564 -- Otherwise, we require the address clause to be constant because
10565 -- the call to the initialization procedure (or the attach code) has
10566 -- to happen at the point of the declaration.
10568 -- Actually the IP call has been moved to the freeze actions anyway,
10569 -- so maybe we can relax this restriction???
10571 return True;
10572 end if;
10573 end Needs_Constant_Address;
10575 ----------------------------
10576 -- New_Class_Wide_Subtype --
10577 ----------------------------
10579 function New_Class_Wide_Subtype
10580 (CW_Typ : Entity_Id;
10581 N : Node_Id) return Entity_Id
10583 Res : constant Entity_Id := Create_Itype (E_Void, N);
10585 -- Capture relevant attributes of the class-wide subtype which must be
10586 -- restored after the copy.
10588 Res_Chars : constant Name_Id := Chars (Res);
10589 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
10590 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
10591 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
10592 Res_Scope : constant Entity_Id := Scope (Res);
10594 begin
10595 Copy_Node (CW_Typ, Res);
10597 -- Restore the relevant attributes of the class-wide subtype
10599 Set_Chars (Res, Res_Chars);
10600 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
10601 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
10602 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
10603 Set_Scope (Res, Res_Scope);
10605 -- Decorate the class-wide subtype
10607 Set_Associated_Node_For_Itype (Res, N);
10608 Set_Comes_From_Source (Res, False);
10609 Set_Ekind (Res, E_Class_Wide_Subtype);
10610 Set_Etype (Res, Base_Type (CW_Typ));
10611 Set_Freeze_Node (Res, Empty);
10612 Set_Is_Frozen (Res, False);
10613 Set_Is_Itype (Res);
10614 Set_Is_Public (Res, False);
10615 Set_Next_Entity (Res, Empty);
10616 Set_Prev_Entity (Res, Empty);
10617 Set_Sloc (Res, Sloc (N));
10619 Set_Public_Status (Res);
10621 return Res;
10622 end New_Class_Wide_Subtype;
10624 --------------------------------
10625 -- Non_Limited_Designated_Type --
10626 ---------------------------------
10628 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10629 Desig : constant Entity_Id := Designated_Type (T);
10630 begin
10631 if Has_Non_Limited_View (Desig) then
10632 return Non_Limited_View (Desig);
10633 else
10634 return Desig;
10635 end if;
10636 end Non_Limited_Designated_Type;
10638 -----------------------------------
10639 -- OK_To_Do_Constant_Replacement --
10640 -----------------------------------
10642 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10643 ES : constant Entity_Id := Scope (E);
10644 CS : Entity_Id;
10646 begin
10647 -- Do not replace statically allocated objects, because they may be
10648 -- modified outside the current scope.
10650 if Is_Statically_Allocated (E) then
10651 return False;
10653 -- Do not replace aliased or volatile objects, since we don't know what
10654 -- else might change the value.
10656 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10657 return False;
10659 -- Debug flag -gnatdM disconnects this optimization
10661 elsif Debug_Flag_MM then
10662 return False;
10664 -- Otherwise check scopes
10666 else
10667 CS := Current_Scope;
10669 loop
10670 -- If we are in right scope, replacement is safe
10672 if CS = ES then
10673 return True;
10675 -- Packages do not affect the determination of safety
10677 elsif Ekind (CS) = E_Package then
10678 exit when CS = Standard_Standard;
10679 CS := Scope (CS);
10681 -- Blocks do not affect the determination of safety
10683 elsif Ekind (CS) = E_Block then
10684 CS := Scope (CS);
10686 -- Loops do not affect the determination of safety. Note that we
10687 -- kill all current values on entry to a loop, so we are just
10688 -- talking about processing within a loop here.
10690 elsif Ekind (CS) = E_Loop then
10691 CS := Scope (CS);
10693 -- Otherwise, the reference is dubious, and we cannot be sure that
10694 -- it is safe to do the replacement.
10696 else
10697 exit;
10698 end if;
10699 end loop;
10701 return False;
10702 end if;
10703 end OK_To_Do_Constant_Replacement;
10705 ------------------------------------
10706 -- Possible_Bit_Aligned_Component --
10707 ------------------------------------
10709 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10710 begin
10711 -- Do not process an unanalyzed node because it is not yet decorated and
10712 -- most checks performed below will fail.
10714 if not Analyzed (N) then
10715 return False;
10716 end if;
10718 case Nkind (N) is
10720 -- Case of indexed component
10722 when N_Indexed_Component =>
10723 declare
10724 P : constant Node_Id := Prefix (N);
10725 Ptyp : constant Entity_Id := Etype (P);
10727 begin
10728 -- If we know the component size and it is less than 64, then
10729 -- we are definitely OK. The back end always does assignment of
10730 -- misaligned small objects correctly.
10732 if Known_Static_Component_Size (Ptyp)
10733 and then Component_Size (Ptyp) <= 64
10734 then
10735 return False;
10737 -- Otherwise, we need to test the prefix, to see if we are
10738 -- indexing from a possibly unaligned component.
10740 else
10741 return Possible_Bit_Aligned_Component (P);
10742 end if;
10743 end;
10745 -- Case of selected component
10747 when N_Selected_Component =>
10748 declare
10749 P : constant Node_Id := Prefix (N);
10750 Comp : constant Entity_Id := Entity (Selector_Name (N));
10752 begin
10753 -- If there is no component clause, then we are in the clear
10754 -- since the back end will never misalign a large component
10755 -- unless it is forced to do so. In the clear means we need
10756 -- only the recursive test on the prefix.
10758 if Component_May_Be_Bit_Aligned (Comp) then
10759 return True;
10760 else
10761 return Possible_Bit_Aligned_Component (P);
10762 end if;
10763 end;
10765 -- For a slice, test the prefix, if that is possibly misaligned,
10766 -- then for sure the slice is.
10768 when N_Slice =>
10769 return Possible_Bit_Aligned_Component (Prefix (N));
10771 -- For an unchecked conversion, check whether the expression may
10772 -- be bit-aligned.
10774 when N_Unchecked_Type_Conversion =>
10775 return Possible_Bit_Aligned_Component (Expression (N));
10777 -- If we have none of the above, it means that we have fallen off the
10778 -- top testing prefixes recursively, and we now have a stand alone
10779 -- object, where we don't have a problem, unless this is a renaming,
10780 -- in which case we need to look into the renamed object.
10782 when others =>
10783 if Is_Entity_Name (N)
10784 and then Present (Renamed_Object (Entity (N)))
10785 then
10786 return
10787 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10788 else
10789 return False;
10790 end if;
10791 end case;
10792 end Possible_Bit_Aligned_Component;
10794 -----------------------------------------------
10795 -- Process_Statements_For_Controlled_Objects --
10796 -----------------------------------------------
10798 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10799 Loc : constant Source_Ptr := Sloc (N);
10801 function Are_Wrapped (L : List_Id) return Boolean;
10802 -- Determine whether list L contains only one statement which is a block
10804 function Wrap_Statements_In_Block
10805 (L : List_Id;
10806 Scop : Entity_Id := Current_Scope) return Node_Id;
10807 -- Given a list of statements L, wrap it in a block statement and return
10808 -- the generated node. Scop is either the current scope or the scope of
10809 -- the context (if applicable).
10811 -----------------
10812 -- Are_Wrapped --
10813 -----------------
10815 function Are_Wrapped (L : List_Id) return Boolean is
10816 Stmt : constant Node_Id := First (L);
10817 begin
10818 return
10819 Present (Stmt)
10820 and then No (Next (Stmt))
10821 and then Nkind (Stmt) = N_Block_Statement;
10822 end Are_Wrapped;
10824 ------------------------------
10825 -- Wrap_Statements_In_Block --
10826 ------------------------------
10828 function Wrap_Statements_In_Block
10829 (L : List_Id;
10830 Scop : Entity_Id := Current_Scope) return Node_Id
10832 Block_Id : Entity_Id;
10833 Block_Nod : Node_Id;
10834 Iter_Loop : Entity_Id;
10836 begin
10837 Block_Nod :=
10838 Make_Block_Statement (Loc,
10839 Declarations => No_List,
10840 Handled_Statement_Sequence =>
10841 Make_Handled_Sequence_Of_Statements (Loc,
10842 Statements => L));
10844 -- Create a label for the block in case the block needs to manage the
10845 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10847 Add_Block_Identifier (Block_Nod, Block_Id);
10849 -- When wrapping the statements of an iterator loop, check whether
10850 -- the loop requires secondary stack management and if so, propagate
10851 -- the appropriate flags to the block. This ensures that the cursor
10852 -- is properly cleaned up at each iteration of the loop.
10854 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10856 if Present (Iter_Loop) then
10857 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10859 -- Secondary stack reclamation is suppressed when the associated
10860 -- iterator loop contains a return statement which uses the stack.
10862 Set_Sec_Stack_Needed_For_Return
10863 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10864 end if;
10866 return Block_Nod;
10867 end Wrap_Statements_In_Block;
10869 -- Local variables
10871 Block : Node_Id;
10873 -- Start of processing for Process_Statements_For_Controlled_Objects
10875 begin
10876 -- Whenever a non-handled statement list is wrapped in a block, the
10877 -- block must be explicitly analyzed to redecorate all entities in the
10878 -- list and ensure that a finalizer is properly built.
10880 case Nkind (N) is
10881 when N_Conditional_Entry_Call
10882 | N_Elsif_Part
10883 | N_If_Statement
10884 | N_Selective_Accept
10886 -- Check the "then statements" for elsif parts and if statements
10888 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10889 and then not Is_Empty_List (Then_Statements (N))
10890 and then not Are_Wrapped (Then_Statements (N))
10891 and then Requires_Cleanup_Actions
10892 (L => Then_Statements (N),
10893 Lib_Level => False,
10894 Nested_Constructs => False)
10895 then
10896 Block := Wrap_Statements_In_Block (Then_Statements (N));
10897 Set_Then_Statements (N, New_List (Block));
10899 Analyze (Block);
10900 end if;
10902 -- Check the "else statements" for conditional entry calls, if
10903 -- statements and selective accepts.
10905 if Nkind_In (N, N_Conditional_Entry_Call,
10906 N_If_Statement,
10907 N_Selective_Accept)
10908 and then not Is_Empty_List (Else_Statements (N))
10909 and then not Are_Wrapped (Else_Statements (N))
10910 and then Requires_Cleanup_Actions
10911 (L => Else_Statements (N),
10912 Lib_Level => False,
10913 Nested_Constructs => False)
10914 then
10915 Block := Wrap_Statements_In_Block (Else_Statements (N));
10916 Set_Else_Statements (N, New_List (Block));
10918 Analyze (Block);
10919 end if;
10921 when N_Abortable_Part
10922 | N_Accept_Alternative
10923 | N_Case_Statement_Alternative
10924 | N_Delay_Alternative
10925 | N_Entry_Call_Alternative
10926 | N_Exception_Handler
10927 | N_Loop_Statement
10928 | N_Triggering_Alternative
10930 if not Is_Empty_List (Statements (N))
10931 and then not Are_Wrapped (Statements (N))
10932 and then Requires_Cleanup_Actions
10933 (L => Statements (N),
10934 Lib_Level => False,
10935 Nested_Constructs => False)
10936 then
10937 if Nkind (N) = N_Loop_Statement
10938 and then Present (Identifier (N))
10939 then
10940 Block :=
10941 Wrap_Statements_In_Block
10942 (L => Statements (N),
10943 Scop => Entity (Identifier (N)));
10944 else
10945 Block := Wrap_Statements_In_Block (Statements (N));
10946 end if;
10948 Set_Statements (N, New_List (Block));
10949 Analyze (Block);
10950 end if;
10952 -- Could be e.g. a loop that was transformed into a block or null
10953 -- statement. Do nothing for terminate alternatives.
10955 when N_Block_Statement
10956 | N_Null_Statement
10957 | N_Terminate_Alternative
10959 null;
10961 when others =>
10962 raise Program_Error;
10963 end case;
10964 end Process_Statements_For_Controlled_Objects;
10966 ------------------
10967 -- Power_Of_Two --
10968 ------------------
10970 function Power_Of_Two (N : Node_Id) return Nat is
10971 Typ : constant Entity_Id := Etype (N);
10972 pragma Assert (Is_Integer_Type (Typ));
10974 Siz : constant Nat := UI_To_Int (Esize (Typ));
10975 Val : Uint;
10977 begin
10978 if not Compile_Time_Known_Value (N) then
10979 return 0;
10981 else
10982 Val := Expr_Value (N);
10983 for J in 1 .. Siz - 1 loop
10984 if Val = Uint_2 ** J then
10985 return J;
10986 end if;
10987 end loop;
10989 return 0;
10990 end if;
10991 end Power_Of_Two;
10993 ----------------------
10994 -- Remove_Init_Call --
10995 ----------------------
10997 function Remove_Init_Call
10998 (Var : Entity_Id;
10999 Rep_Clause : Node_Id) return Node_Id
11001 Par : constant Node_Id := Parent (Var);
11002 Typ : constant Entity_Id := Etype (Var);
11004 Init_Proc : Entity_Id;
11005 -- Initialization procedure for Typ
11007 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
11008 -- Look for init call for Var starting at From and scanning the
11009 -- enclosing list until Rep_Clause or the end of the list is reached.
11011 ----------------------------
11012 -- Find_Init_Call_In_List --
11013 ----------------------------
11015 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
11016 Init_Call : Node_Id;
11018 begin
11019 Init_Call := From;
11020 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
11021 if Nkind (Init_Call) = N_Procedure_Call_Statement
11022 and then Is_Entity_Name (Name (Init_Call))
11023 and then Entity (Name (Init_Call)) = Init_Proc
11024 then
11025 return Init_Call;
11026 end if;
11028 Next (Init_Call);
11029 end loop;
11031 return Empty;
11032 end Find_Init_Call_In_List;
11034 Init_Call : Node_Id;
11036 -- Start of processing for Find_Init_Call
11038 begin
11039 if Present (Initialization_Statements (Var)) then
11040 Init_Call := Initialization_Statements (Var);
11041 Set_Initialization_Statements (Var, Empty);
11043 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
11045 -- No init proc for the type, so obviously no call to be found
11047 return Empty;
11049 else
11050 -- We might be able to handle other cases below by just properly
11051 -- setting Initialization_Statements at the point where the init proc
11052 -- call is generated???
11054 Init_Proc := Base_Init_Proc (Typ);
11056 -- First scan the list containing the declaration of Var
11058 Init_Call := Find_Init_Call_In_List (From => Next (Par));
11060 -- If not found, also look on Var's freeze actions list, if any,
11061 -- since the init call may have been moved there (case of an address
11062 -- clause applying to Var).
11064 if No (Init_Call) and then Present (Freeze_Node (Var)) then
11065 Init_Call :=
11066 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
11067 end if;
11069 -- If the initialization call has actuals that use the secondary
11070 -- stack, the call may have been wrapped into a temporary block, in
11071 -- which case the block itself has to be removed.
11073 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
11074 declare
11075 Blk : constant Node_Id := Next (Par);
11076 begin
11077 if Present
11078 (Find_Init_Call_In_List
11079 (First (Statements (Handled_Statement_Sequence (Blk)))))
11080 then
11081 Init_Call := Blk;
11082 end if;
11083 end;
11084 end if;
11085 end if;
11087 if Present (Init_Call) then
11088 Remove (Init_Call);
11089 end if;
11090 return Init_Call;
11091 end Remove_Init_Call;
11093 -------------------------
11094 -- Remove_Side_Effects --
11095 -------------------------
11097 procedure Remove_Side_Effects
11098 (Exp : Node_Id;
11099 Name_Req : Boolean := False;
11100 Renaming_Req : Boolean := False;
11101 Variable_Ref : Boolean := False;
11102 Related_Id : Entity_Id := Empty;
11103 Is_Low_Bound : Boolean := False;
11104 Is_High_Bound : Boolean := False;
11105 Check_Side_Effects : Boolean := True)
11107 function Build_Temporary
11108 (Loc : Source_Ptr;
11109 Id : Character;
11110 Related_Nod : Node_Id := Empty) return Entity_Id;
11111 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11112 -- is present (xxx is taken from the Chars field of Related_Nod),
11113 -- otherwise it generates an internal temporary. The created temporary
11114 -- entity is marked as internal.
11116 ---------------------
11117 -- Build_Temporary --
11118 ---------------------
11120 function Build_Temporary
11121 (Loc : Source_Ptr;
11122 Id : Character;
11123 Related_Nod : Node_Id := Empty) return Entity_Id
11125 Temp_Id : Entity_Id;
11126 Temp_Nam : Name_Id;
11128 begin
11129 -- The context requires an external symbol
11131 if Present (Related_Id) then
11132 if Is_Low_Bound then
11133 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
11134 else pragma Assert (Is_High_Bound);
11135 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
11136 end if;
11138 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
11140 -- Otherwise generate an internal temporary
11142 else
11143 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
11144 end if;
11146 Set_Is_Internal (Temp_Id);
11148 return Temp_Id;
11149 end Build_Temporary;
11151 -- Local variables
11153 Loc : constant Source_Ptr := Sloc (Exp);
11154 Exp_Type : constant Entity_Id := Etype (Exp);
11155 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
11156 Def_Id : Entity_Id;
11157 E : Node_Id;
11158 New_Exp : Node_Id;
11159 Ptr_Typ_Decl : Node_Id;
11160 Ref_Type : Entity_Id;
11161 Res : Node_Id;
11163 -- Start of processing for Remove_Side_Effects
11165 begin
11166 -- Handle cases in which there is nothing to do. In GNATprove mode,
11167 -- removal of side effects is useful for the light expansion of
11168 -- renamings. This removal should only occur when not inside a
11169 -- generic and not doing a pre-analysis.
11171 if not Expander_Active
11172 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
11173 then
11174 return;
11176 -- Cannot generate temporaries if the invocation to remove side effects
11177 -- was issued too early and the type of the expression is not resolved
11178 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11179 -- Remove_Side_Effects).
11181 elsif No (Exp_Type)
11182 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11183 then
11184 return;
11186 -- Nothing to do if prior expansion determined that a function call does
11187 -- not require side effect removal.
11189 elsif Nkind (Exp) = N_Function_Call
11190 and then No_Side_Effect_Removal (Exp)
11191 then
11192 return;
11194 -- No action needed for side-effect free expressions
11196 elsif Check_Side_Effects
11197 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11198 then
11199 return;
11201 -- Generating C code we cannot remove side effect of function returning
11202 -- class-wide types since there is no secondary stack (required to use
11203 -- 'reference).
11205 elsif Modify_Tree_For_C
11206 and then Nkind (Exp) = N_Function_Call
11207 and then Is_Class_Wide_Type (Etype (Exp))
11208 then
11209 return;
11210 end if;
11212 -- The remaining processing is done with all checks suppressed
11214 -- Note: from now on, don't use return statements, instead do a goto
11215 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11217 Scope_Suppress.Suppress := (others => True);
11219 -- If this is an elementary or a small not-by-reference record type, and
11220 -- we need to capture the value, just make a constant; this is cheap and
11221 -- objects of both kinds of types can be bit aligned, so it might not be
11222 -- possible to generate a reference to them. Likewise if this is not a
11223 -- name reference, except for a type conversion, because we would enter
11224 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11225 -- type has predicates (and type conversions need a specific treatment
11226 -- anyway, see below). Also do it if we have a volatile reference and
11227 -- Name_Req is not set (see comments for Side_Effect_Free).
11229 if (Is_Elementary_Type (Exp_Type)
11230 or else (Is_Record_Type (Exp_Type)
11231 and then Known_Static_RM_Size (Exp_Type)
11232 and then RM_Size (Exp_Type) <= 64
11233 and then not Has_Discriminants (Exp_Type)
11234 and then not Is_By_Reference_Type (Exp_Type)))
11235 and then (Variable_Ref
11236 or else (not Is_Name_Reference (Exp)
11237 and then Nkind (Exp) /= N_Type_Conversion)
11238 or else (not Name_Req
11239 and then Is_Volatile_Reference (Exp)))
11240 then
11241 Def_Id := Build_Temporary (Loc, 'R', Exp);
11242 Set_Etype (Def_Id, Exp_Type);
11243 Res := New_Occurrence_Of (Def_Id, Loc);
11245 -- If the expression is a packed reference, it must be reanalyzed and
11246 -- expanded, depending on context. This is the case for actuals where
11247 -- a constraint check may capture the actual before expansion of the
11248 -- call is complete.
11250 if Nkind (Exp) = N_Indexed_Component
11251 and then Is_Packed (Etype (Prefix (Exp)))
11252 then
11253 Set_Analyzed (Exp, False);
11254 Set_Analyzed (Prefix (Exp), False);
11255 end if;
11257 -- Generate:
11258 -- Rnn : Exp_Type renames Expr;
11260 if Renaming_Req then
11261 E :=
11262 Make_Object_Renaming_Declaration (Loc,
11263 Defining_Identifier => Def_Id,
11264 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11265 Name => Relocate_Node (Exp));
11267 -- Generate:
11268 -- Rnn : constant Exp_Type := Expr;
11270 else
11271 E :=
11272 Make_Object_Declaration (Loc,
11273 Defining_Identifier => Def_Id,
11274 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11275 Constant_Present => True,
11276 Expression => Relocate_Node (Exp));
11278 Set_Assignment_OK (E);
11279 end if;
11281 Insert_Action (Exp, E);
11283 -- If the expression has the form v.all then we can just capture the
11284 -- pointer, and then do an explicit dereference on the result, but
11285 -- this is not right if this is a volatile reference.
11287 elsif Nkind (Exp) = N_Explicit_Dereference
11288 and then not Is_Volatile_Reference (Exp)
11289 then
11290 Def_Id := Build_Temporary (Loc, 'R', Exp);
11291 Res :=
11292 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11294 Insert_Action (Exp,
11295 Make_Object_Declaration (Loc,
11296 Defining_Identifier => Def_Id,
11297 Object_Definition =>
11298 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11299 Constant_Present => True,
11300 Expression => Relocate_Node (Prefix (Exp))));
11302 -- Similar processing for an unchecked conversion of an expression of
11303 -- the form v.all, where we want the same kind of treatment.
11305 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11306 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11307 then
11308 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11309 goto Leave;
11311 -- If this is a type conversion, leave the type conversion and remove
11312 -- the side effects in the expression. This is important in several
11313 -- circumstances: for change of representations, and also when this is a
11314 -- view conversion to a smaller object, where gigi can end up creating
11315 -- its own temporary of the wrong size.
11317 elsif Nkind (Exp) = N_Type_Conversion then
11318 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11320 -- Generating C code the type conversion of an access to constrained
11321 -- array type into an access to unconstrained array type involves
11322 -- initializing a fat pointer and the expression must be free of
11323 -- side effects to safely compute its bounds.
11325 if Modify_Tree_For_C
11326 and then Is_Access_Type (Etype (Exp))
11327 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11328 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11329 then
11330 Def_Id := Build_Temporary (Loc, 'R', Exp);
11331 Set_Etype (Def_Id, Exp_Type);
11332 Res := New_Occurrence_Of (Def_Id, Loc);
11334 Insert_Action (Exp,
11335 Make_Object_Declaration (Loc,
11336 Defining_Identifier => Def_Id,
11337 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11338 Constant_Present => True,
11339 Expression => Relocate_Node (Exp)));
11340 else
11341 goto Leave;
11342 end if;
11344 -- If this is an unchecked conversion that Gigi can't handle, make
11345 -- a copy or a use a renaming to capture the value.
11347 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11348 and then not Safe_Unchecked_Type_Conversion (Exp)
11349 then
11350 if CW_Or_Has_Controlled_Part (Exp_Type) then
11352 -- Use a renaming to capture the expression, rather than create
11353 -- a controlled temporary.
11355 Def_Id := Build_Temporary (Loc, 'R', Exp);
11356 Res := New_Occurrence_Of (Def_Id, Loc);
11358 Insert_Action (Exp,
11359 Make_Object_Renaming_Declaration (Loc,
11360 Defining_Identifier => Def_Id,
11361 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11362 Name => Relocate_Node (Exp)));
11364 else
11365 Def_Id := Build_Temporary (Loc, 'R', Exp);
11366 Set_Etype (Def_Id, Exp_Type);
11367 Res := New_Occurrence_Of (Def_Id, Loc);
11369 E :=
11370 Make_Object_Declaration (Loc,
11371 Defining_Identifier => Def_Id,
11372 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11373 Constant_Present => not Is_Variable (Exp),
11374 Expression => Relocate_Node (Exp));
11376 Set_Assignment_OK (E);
11377 Insert_Action (Exp, E);
11378 end if;
11380 -- For expressions that denote names, we can use a renaming scheme.
11381 -- This is needed for correctness in the case of a volatile object of
11382 -- a non-volatile type because the Make_Reference call of the "default"
11383 -- approach would generate an illegal access value (an access value
11384 -- cannot designate such an object - see Analyze_Reference).
11386 elsif Is_Name_Reference (Exp)
11388 -- We skip using this scheme if we have an object of a volatile
11389 -- type and we do not have Name_Req set true (see comments for
11390 -- Side_Effect_Free).
11392 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11393 then
11394 Def_Id := Build_Temporary (Loc, 'R', Exp);
11395 Res := New_Occurrence_Of (Def_Id, Loc);
11397 Insert_Action (Exp,
11398 Make_Object_Renaming_Declaration (Loc,
11399 Defining_Identifier => Def_Id,
11400 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11401 Name => Relocate_Node (Exp)));
11403 -- If this is a packed reference, or a selected component with
11404 -- a non-standard representation, a reference to the temporary
11405 -- will be replaced by a copy of the original expression (see
11406 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11407 -- elaborated by gigi, and is of course not to be replaced in-line
11408 -- by the expression it renames, which would defeat the purpose of
11409 -- removing the side effect.
11411 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11412 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11413 then
11414 null;
11415 else
11416 Set_Is_Renaming_Of_Object (Def_Id, False);
11417 end if;
11419 -- Avoid generating a variable-sized temporary, by generating the
11420 -- reference just for the function call. The transformation could be
11421 -- refined to apply only when the array component is constrained by a
11422 -- discriminant???
11424 elsif Nkind (Exp) = N_Selected_Component
11425 and then Nkind (Prefix (Exp)) = N_Function_Call
11426 and then Is_Array_Type (Exp_Type)
11427 then
11428 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11429 goto Leave;
11431 -- Otherwise we generate a reference to the expression
11433 else
11434 -- An expression which is in SPARK mode is considered side effect
11435 -- free if the resulting value is captured by a variable or a
11436 -- constant.
11438 if GNATprove_Mode
11439 and then Nkind (Parent (Exp)) = N_Object_Declaration
11440 then
11441 goto Leave;
11443 -- When generating C code we cannot consider side effect free object
11444 -- declarations that have discriminants and are initialized by means
11445 -- of a function call since on this target there is no secondary
11446 -- stack to store the return value and the expander may generate an
11447 -- extra call to the function to compute the discriminant value. In
11448 -- addition, for targets that have secondary stack, the expansion of
11449 -- functions with side effects involves the generation of an access
11450 -- type to capture the return value stored in the secondary stack;
11451 -- by contrast when generating C code such expansion generates an
11452 -- internal object declaration (no access type involved) which must
11453 -- be identified here to avoid entering into a never-ending loop
11454 -- generating internal object declarations.
11456 elsif Modify_Tree_For_C
11457 and then Nkind (Parent (Exp)) = N_Object_Declaration
11458 and then
11459 (Nkind (Exp) /= N_Function_Call
11460 or else not Has_Discriminants (Exp_Type)
11461 or else Is_Internal_Name
11462 (Chars (Defining_Identifier (Parent (Exp)))))
11463 then
11464 goto Leave;
11465 end if;
11467 -- Special processing for function calls that return a limited type.
11468 -- We need to build a declaration that will enable build-in-place
11469 -- expansion of the call. This is not done if the context is already
11470 -- an object declaration, to prevent infinite recursion.
11472 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11473 -- to accommodate functions returning limited objects by reference.
11475 if Ada_Version >= Ada_2005
11476 and then Nkind (Exp) = N_Function_Call
11477 and then Is_Limited_View (Etype (Exp))
11478 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11479 then
11480 declare
11481 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11482 Decl : Node_Id;
11484 begin
11485 Decl :=
11486 Make_Object_Declaration (Loc,
11487 Defining_Identifier => Obj,
11488 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11489 Expression => Relocate_Node (Exp));
11491 Insert_Action (Exp, Decl);
11492 Set_Etype (Obj, Exp_Type);
11493 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11494 goto Leave;
11495 end;
11496 end if;
11498 Def_Id := Build_Temporary (Loc, 'R', Exp);
11500 -- The regular expansion of functions with side effects involves the
11501 -- generation of an access type to capture the return value found on
11502 -- the secondary stack. Since SPARK (and why) cannot process access
11503 -- types, use a different approach which ignores the secondary stack
11504 -- and "copies" the returned object.
11505 -- When generating C code, no need for a 'reference since the
11506 -- secondary stack is not supported.
11508 if GNATprove_Mode or Modify_Tree_For_C then
11509 Res := New_Occurrence_Of (Def_Id, Loc);
11510 Ref_Type := Exp_Type;
11512 -- Regular expansion utilizing an access type and 'reference
11514 else
11515 Res :=
11516 Make_Explicit_Dereference (Loc,
11517 Prefix => New_Occurrence_Of (Def_Id, Loc));
11519 -- Generate:
11520 -- type Ann is access all <Exp_Type>;
11522 Ref_Type := Make_Temporary (Loc, 'A');
11524 Ptr_Typ_Decl :=
11525 Make_Full_Type_Declaration (Loc,
11526 Defining_Identifier => Ref_Type,
11527 Type_Definition =>
11528 Make_Access_To_Object_Definition (Loc,
11529 All_Present => True,
11530 Subtype_Indication =>
11531 New_Occurrence_Of (Exp_Type, Loc)));
11533 Insert_Action (Exp, Ptr_Typ_Decl);
11534 end if;
11536 E := Exp;
11537 if Nkind (E) = N_Explicit_Dereference then
11538 New_Exp := Relocate_Node (Prefix (E));
11540 else
11541 E := Relocate_Node (E);
11543 -- Do not generate a 'reference in SPARK mode or C generation
11544 -- since the access type is not created in the first place.
11546 if GNATprove_Mode or Modify_Tree_For_C then
11547 New_Exp := E;
11549 -- Otherwise generate reference, marking the value as non-null
11550 -- since we know it cannot be null and we don't want a check.
11552 else
11553 New_Exp := Make_Reference (Loc, E);
11554 Set_Is_Known_Non_Null (Def_Id);
11555 end if;
11556 end if;
11558 if Is_Delayed_Aggregate (E) then
11560 -- The expansion of nested aggregates is delayed until the
11561 -- enclosing aggregate is expanded. As aggregates are often
11562 -- qualified, the predicate applies to qualified expressions as
11563 -- well, indicating that the enclosing aggregate has not been
11564 -- expanded yet. At this point the aggregate is part of a
11565 -- stand-alone declaration, and must be fully expanded.
11567 if Nkind (E) = N_Qualified_Expression then
11568 Set_Expansion_Delayed (Expression (E), False);
11569 Set_Analyzed (Expression (E), False);
11570 else
11571 Set_Expansion_Delayed (E, False);
11572 end if;
11574 Set_Analyzed (E, False);
11575 end if;
11577 -- Generating C code of object declarations that have discriminants
11578 -- and are initialized by means of a function call we propagate the
11579 -- discriminants of the parent type to the internally built object.
11580 -- This is needed to avoid generating an extra call to the called
11581 -- function.
11583 -- For example, if we generate here the following declaration, it
11584 -- will be expanded later adding an extra call to evaluate the value
11585 -- of the discriminant (needed to compute the size of the object).
11587 -- type Rec (D : Integer) is ...
11588 -- Obj : constant Rec := SomeFunc;
11590 if Modify_Tree_For_C
11591 and then Nkind (Parent (Exp)) = N_Object_Declaration
11592 and then Has_Discriminants (Exp_Type)
11593 and then Nkind (Exp) = N_Function_Call
11594 then
11595 Insert_Action (Exp,
11596 Make_Object_Declaration (Loc,
11597 Defining_Identifier => Def_Id,
11598 Object_Definition => New_Copy_Tree
11599 (Object_Definition (Parent (Exp))),
11600 Constant_Present => True,
11601 Expression => New_Exp));
11602 else
11603 Insert_Action (Exp,
11604 Make_Object_Declaration (Loc,
11605 Defining_Identifier => Def_Id,
11606 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11607 Constant_Present => True,
11608 Expression => New_Exp));
11609 end if;
11610 end if;
11612 -- Preserve the Assignment_OK flag in all copies, since at least one
11613 -- copy may be used in a context where this flag must be set (otherwise
11614 -- why would the flag be set in the first place).
11616 Set_Assignment_OK (Res, Assignment_OK (Exp));
11618 -- Finally rewrite the original expression and we are done
11620 Rewrite (Exp, Res);
11621 Analyze_And_Resolve (Exp, Exp_Type);
11623 <<Leave>>
11624 Scope_Suppress := Svg_Suppress;
11625 end Remove_Side_Effects;
11627 ------------------------
11628 -- Replace_References --
11629 ------------------------
11631 procedure Replace_References
11632 (Expr : Node_Id;
11633 Par_Typ : Entity_Id;
11634 Deriv_Typ : Entity_Id;
11635 Par_Obj : Entity_Id := Empty;
11636 Deriv_Obj : Entity_Id := Empty)
11638 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11639 -- Determine whether node Ref denotes some component of Deriv_Obj
11641 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11642 -- Substitute a reference to an entity with the corresponding value
11643 -- stored in table Type_Map.
11645 function Type_Of_Formal
11646 (Call : Node_Id;
11647 Actual : Node_Id) return Entity_Id;
11648 -- Find the type of the formal parameter which corresponds to actual
11649 -- parameter Actual in subprogram call Call.
11651 ----------------------
11652 -- Is_Deriv_Obj_Ref --
11653 ----------------------
11655 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11656 Par : constant Node_Id := Parent (Ref);
11658 begin
11659 -- Detect the folowing selected component form:
11661 -- Deriv_Obj.(something)
11663 return
11664 Nkind (Par) = N_Selected_Component
11665 and then Is_Entity_Name (Prefix (Par))
11666 and then Entity (Prefix (Par)) = Deriv_Obj;
11667 end Is_Deriv_Obj_Ref;
11669 -----------------
11670 -- Replace_Ref --
11671 -----------------
11673 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11674 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11675 -- Reset the Controlling_Argument of all function calls that
11676 -- encapsulate node From_Arg.
11678 ----------------------------------
11679 -- Remove_Controlling_Arguments --
11680 ----------------------------------
11682 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11683 Par : Node_Id;
11685 begin
11686 Par := From_Arg;
11687 while Present (Par) loop
11688 if Nkind (Par) = N_Function_Call
11689 and then Present (Controlling_Argument (Par))
11690 then
11691 Set_Controlling_Argument (Par, Empty);
11693 -- Prevent the search from going too far
11695 elsif Is_Body_Or_Package_Declaration (Par) then
11696 exit;
11697 end if;
11699 Par := Parent (Par);
11700 end loop;
11701 end Remove_Controlling_Arguments;
11703 -- Local variables
11705 Context : constant Node_Id := Parent (Ref);
11706 Loc : constant Source_Ptr := Sloc (Ref);
11707 Ref_Id : Entity_Id;
11708 Result : Traverse_Result;
11710 New_Ref : Node_Id;
11711 -- The new reference which is intended to substitute the old one
11713 Old_Ref : Node_Id;
11714 -- The reference designated for replacement. In certain cases this
11715 -- may be a node other than Ref.
11717 Val : Node_Or_Entity_Id;
11718 -- The corresponding value of Ref from the type map
11720 -- Start of processing for Replace_Ref
11722 begin
11723 -- Assume that the input reference is to be replaced and that the
11724 -- traversal should examine the children of the reference.
11726 Old_Ref := Ref;
11727 Result := OK;
11729 -- The input denotes a meaningful reference
11731 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11732 Ref_Id := Entity (Ref);
11733 Val := Type_Map.Get (Ref_Id);
11735 -- The reference has a corresponding value in the type map, a
11736 -- substitution is possible.
11738 if Present (Val) then
11740 -- The reference denotes a discriminant
11742 if Ekind (Ref_Id) = E_Discriminant then
11743 if Nkind (Val) in N_Entity then
11745 -- The value denotes another discriminant. Replace as
11746 -- follows:
11748 -- _object.Discr -> _object.Val
11750 if Ekind (Val) = E_Discriminant then
11751 New_Ref := New_Occurrence_Of (Val, Loc);
11753 -- Otherwise the value denotes the entity of a name which
11754 -- constraints the discriminant. Replace as follows:
11756 -- _object.Discr -> Val
11758 else
11759 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11761 New_Ref := New_Occurrence_Of (Val, Loc);
11762 Old_Ref := Parent (Old_Ref);
11763 end if;
11765 -- Otherwise the value denotes an arbitrary expression which
11766 -- constraints the discriminant. Replace as follows:
11768 -- _object.Discr -> Val
11770 else
11771 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11773 New_Ref := New_Copy_Tree (Val);
11774 Old_Ref := Parent (Old_Ref);
11775 end if;
11777 -- Otherwise the reference denotes a primitive. Replace as
11778 -- follows:
11780 -- Primitive -> Val
11782 else
11783 pragma Assert (Nkind (Val) in N_Entity);
11784 New_Ref := New_Occurrence_Of (Val, Loc);
11785 end if;
11787 -- The reference mentions the _object parameter of the parent
11788 -- type's DIC or type invariant procedure. Replace as follows:
11790 -- _object -> _object
11792 elsif Present (Par_Obj)
11793 and then Present (Deriv_Obj)
11794 and then Ref_Id = Par_Obj
11795 then
11796 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11798 -- The type of the _object parameter is class-wide when the
11799 -- expression comes from an assertion pragma that applies to
11800 -- an abstract parent type or an interface. The class-wide type
11801 -- facilitates the preanalysis of the expression by treating
11802 -- calls to abstract primitives that mention the current
11803 -- instance of the type as dispatching. Once the calls are
11804 -- remapped to invoke overriding or inherited primitives, the
11805 -- calls no longer need to be dispatching. Examine all function
11806 -- calls that encapsulate the _object parameter and reset their
11807 -- Controlling_Argument attribute.
11809 if Is_Class_Wide_Type (Etype (Par_Obj))
11810 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11811 then
11812 Remove_Controlling_Arguments (Old_Ref);
11813 end if;
11815 -- The reference to _object acts as an actual parameter in a
11816 -- subprogram call which may be invoking a primitive of the
11817 -- parent type:
11819 -- Primitive (... _object ...);
11821 -- The parent type primitive may not be overridden nor
11822 -- inherited when it is declared after the derived type
11823 -- definition:
11825 -- type Parent is tagged private;
11826 -- type Child is new Parent with private;
11827 -- procedure Primitive (Obj : Parent);
11829 -- In this scenario the _object parameter is converted to the
11830 -- parent type. Due to complications with partial/full views
11831 -- and view swaps, the parent type is taken from the formal
11832 -- parameter of the subprogram being called.
11834 if Nkind_In (Context, N_Function_Call,
11835 N_Procedure_Call_Statement)
11836 and then No (Type_Map.Get (Entity (Name (Context))))
11837 then
11838 New_Ref :=
11839 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11841 -- Do not process the generated type conversion because
11842 -- both the parent type and the derived type are in the
11843 -- Type_Map table. This will clobber the type conversion
11844 -- by resetting its subtype mark.
11846 Result := Skip;
11847 end if;
11849 -- Otherwise there is nothing to replace
11851 else
11852 New_Ref := Empty;
11853 end if;
11855 if Present (New_Ref) then
11856 Rewrite (Old_Ref, New_Ref);
11858 -- Update the return type when the context of the reference
11859 -- acts as the name of a function call. Note that the update
11860 -- should not be performed when the reference appears as an
11861 -- actual in the call.
11863 if Nkind (Context) = N_Function_Call
11864 and then Name (Context) = Old_Ref
11865 then
11866 Set_Etype (Context, Etype (Val));
11867 end if;
11868 end if;
11869 end if;
11871 -- Reanalyze the reference due to potential replacements
11873 if Nkind (Old_Ref) in N_Has_Etype then
11874 Set_Analyzed (Old_Ref, False);
11875 end if;
11877 return Result;
11878 end Replace_Ref;
11880 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11882 --------------------
11883 -- Type_Of_Formal --
11884 --------------------
11886 function Type_Of_Formal
11887 (Call : Node_Id;
11888 Actual : Node_Id) return Entity_Id
11890 A : Node_Id;
11891 F : Entity_Id;
11893 begin
11894 -- Examine the list of actual and formal parameters in parallel
11896 A := First (Parameter_Associations (Call));
11897 F := First_Formal (Entity (Name (Call)));
11898 while Present (A) and then Present (F) loop
11899 if A = Actual then
11900 return Etype (F);
11901 end if;
11903 Next (A);
11904 Next_Formal (F);
11905 end loop;
11907 -- The actual parameter must always have a corresponding formal
11909 pragma Assert (False);
11911 return Empty;
11912 end Type_Of_Formal;
11914 -- Start of processing for Replace_References
11916 begin
11917 -- Map the attributes of the parent type to the proper corresponding
11918 -- attributes of the derived type.
11920 Map_Types
11921 (Parent_Type => Par_Typ,
11922 Derived_Type => Deriv_Typ);
11924 -- Inspect the input expression and perform substitutions where
11925 -- necessary.
11927 Replace_Refs (Expr);
11928 end Replace_References;
11930 -----------------------------
11931 -- Replace_Type_References --
11932 -----------------------------
11934 procedure Replace_Type_References
11935 (Expr : Node_Id;
11936 Typ : Entity_Id;
11937 Obj_Id : Entity_Id)
11939 procedure Replace_Type_Ref (N : Node_Id);
11940 -- Substitute a single reference of the current instance of type Typ
11941 -- with a reference to Obj_Id.
11943 ----------------------
11944 -- Replace_Type_Ref --
11945 ----------------------
11947 procedure Replace_Type_Ref (N : Node_Id) is
11948 begin
11949 -- Decorate the reference to Typ even though it may be rewritten
11950 -- further down. This is done for two reasons:
11952 -- * ASIS has all necessary semantic information in the original
11953 -- tree.
11955 -- * Routines which examine properties of the Original_Node have
11956 -- some semantic information.
11958 if Nkind (N) = N_Identifier then
11959 Set_Entity (N, Typ);
11960 Set_Etype (N, Typ);
11962 elsif Nkind (N) = N_Selected_Component then
11963 Analyze (Prefix (N));
11964 Set_Entity (Selector_Name (N), Typ);
11965 Set_Etype (Selector_Name (N), Typ);
11966 end if;
11968 -- Perform the following substitution:
11970 -- Typ --> _object
11972 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
11973 Set_Comes_From_Source (N, True);
11974 end Replace_Type_Ref;
11976 procedure Replace_Type_Refs is
11977 new Replace_Type_References_Generic (Replace_Type_Ref);
11979 -- Start of processing for Replace_Type_References
11981 begin
11982 Replace_Type_Refs (Expr, Typ);
11983 end Replace_Type_References;
11985 ---------------------------
11986 -- Represented_As_Scalar --
11987 ---------------------------
11989 function Represented_As_Scalar (T : Entity_Id) return Boolean is
11990 UT : constant Entity_Id := Underlying_Type (T);
11991 begin
11992 return Is_Scalar_Type (UT)
11993 or else (Is_Bit_Packed_Array (UT)
11994 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
11995 end Represented_As_Scalar;
11997 ------------------------------
11998 -- Requires_Cleanup_Actions --
11999 ------------------------------
12001 function Requires_Cleanup_Actions
12002 (N : Node_Id;
12003 Lib_Level : Boolean) return Boolean
12005 At_Lib_Level : constant Boolean :=
12006 Lib_Level
12007 and then Nkind_In (N, N_Package_Body,
12008 N_Package_Specification);
12009 -- N is at the library level if the top-most context is a package and
12010 -- the path taken to reach N does not inlcude non-package constructs.
12012 begin
12013 case Nkind (N) is
12014 when N_Accept_Statement
12015 | N_Block_Statement
12016 | N_Entry_Body
12017 | N_Package_Body
12018 | N_Protected_Body
12019 | N_Subprogram_Body
12020 | N_Task_Body
12022 return
12023 Requires_Cleanup_Actions
12024 (L => Declarations (N),
12025 Lib_Level => At_Lib_Level,
12026 Nested_Constructs => True)
12027 or else
12028 (Present (Handled_Statement_Sequence (N))
12029 and then
12030 Requires_Cleanup_Actions
12031 (L =>
12032 Statements (Handled_Statement_Sequence (N)),
12033 Lib_Level => At_Lib_Level,
12034 Nested_Constructs => True));
12036 -- Extended return statements are the same as the above, except that
12037 -- there is no Declarations field. We do not want to clean up the
12038 -- Return_Object_Declarations.
12040 when N_Extended_Return_Statement =>
12041 return
12042 Present (Handled_Statement_Sequence (N))
12043 and then Requires_Cleanup_Actions
12044 (L =>
12045 Statements (Handled_Statement_Sequence (N)),
12046 Lib_Level => At_Lib_Level,
12047 Nested_Constructs => True);
12049 when N_Package_Specification =>
12050 return
12051 Requires_Cleanup_Actions
12052 (L => Visible_Declarations (N),
12053 Lib_Level => At_Lib_Level,
12054 Nested_Constructs => True)
12055 or else
12056 Requires_Cleanup_Actions
12057 (L => Private_Declarations (N),
12058 Lib_Level => At_Lib_Level,
12059 Nested_Constructs => True);
12061 when others =>
12062 raise Program_Error;
12063 end case;
12064 end Requires_Cleanup_Actions;
12066 ------------------------------
12067 -- Requires_Cleanup_Actions --
12068 ------------------------------
12070 function Requires_Cleanup_Actions
12071 (L : List_Id;
12072 Lib_Level : Boolean;
12073 Nested_Constructs : Boolean) return Boolean
12075 Decl : Node_Id;
12076 Expr : Node_Id;
12077 Obj_Id : Entity_Id;
12078 Obj_Typ : Entity_Id;
12079 Pack_Id : Entity_Id;
12080 Typ : Entity_Id;
12082 begin
12083 if No (L)
12084 or else Is_Empty_List (L)
12085 then
12086 return False;
12087 end if;
12089 Decl := First (L);
12090 while Present (Decl) loop
12092 -- Library-level tagged types
12094 if Nkind (Decl) = N_Full_Type_Declaration then
12095 Typ := Defining_Identifier (Decl);
12097 -- Ignored Ghost types do not need any cleanup actions because
12098 -- they will not appear in the final tree.
12100 if Is_Ignored_Ghost_Entity (Typ) then
12101 null;
12103 elsif Is_Tagged_Type (Typ)
12104 and then Is_Library_Level_Entity (Typ)
12105 and then Convention (Typ) = Convention_Ada
12106 and then Present (Access_Disp_Table (Typ))
12107 and then RTE_Available (RE_Unregister_Tag)
12108 and then not Is_Abstract_Type (Typ)
12109 and then not No_Run_Time_Mode
12110 then
12111 return True;
12112 end if;
12114 -- Regular object declarations
12116 elsif Nkind (Decl) = N_Object_Declaration then
12117 Obj_Id := Defining_Identifier (Decl);
12118 Obj_Typ := Base_Type (Etype (Obj_Id));
12119 Expr := Expression (Decl);
12121 -- Bypass any form of processing for objects which have their
12122 -- finalization disabled. This applies only to objects at the
12123 -- library level.
12125 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12126 null;
12128 -- Finalization of transient objects are treated separately in
12129 -- order to handle sensitive cases. These include:
12131 -- * Aggregate expansion
12132 -- * If, case, and expression with actions expansion
12133 -- * Transient scopes
12135 -- If one of those contexts has marked the transient object as
12136 -- ignored, do not generate finalization actions for it.
12138 elsif Is_Finalized_Transient (Obj_Id)
12139 or else Is_Ignored_Transient (Obj_Id)
12140 then
12141 null;
12143 -- Ignored Ghost objects do not need any cleanup actions because
12144 -- they will not appear in the final tree.
12146 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12147 null;
12149 -- The object is of the form:
12150 -- Obj : [constant] Typ [:= Expr];
12152 -- Do not process tag-to-class-wide conversions because they do
12153 -- not yield an object. Do not process the incomplete view of a
12154 -- deferred constant. Note that an object initialized by means
12155 -- of a build-in-place function call may appear as a deferred
12156 -- constant after expansion activities. These kinds of objects
12157 -- must be finalized.
12159 elsif not Is_Imported (Obj_Id)
12160 and then Needs_Finalization (Obj_Typ)
12161 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
12162 and then not (Ekind (Obj_Id) = E_Constant
12163 and then not Has_Completion (Obj_Id)
12164 and then No (BIP_Initialization_Call (Obj_Id)))
12165 then
12166 return True;
12168 -- The object is of the form:
12169 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12171 -- Obj : Access_Typ :=
12172 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12174 elsif Is_Access_Type (Obj_Typ)
12175 and then Needs_Finalization
12176 (Available_View (Designated_Type (Obj_Typ)))
12177 and then Present (Expr)
12178 and then
12179 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12180 or else
12181 (Is_Non_BIP_Func_Call (Expr)
12182 and then not Is_Related_To_Func_Return (Obj_Id)))
12183 then
12184 return True;
12186 -- Processing for "hook" objects generated for transient objects
12187 -- declared inside an Expression_With_Actions.
12189 elsif Is_Access_Type (Obj_Typ)
12190 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12191 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12192 N_Object_Declaration
12193 then
12194 return True;
12196 -- Processing for intermediate results of if expressions where
12197 -- one of the alternatives uses a controlled function call.
12199 elsif Is_Access_Type (Obj_Typ)
12200 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12201 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12202 N_Defining_Identifier
12203 and then Present (Expr)
12204 and then Nkind (Expr) = N_Null
12205 then
12206 return True;
12208 -- Simple protected objects which use type System.Tasking.
12209 -- Protected_Objects.Protection to manage their locks should be
12210 -- treated as controlled since they require manual cleanup.
12212 elsif Ekind (Obj_Id) = E_Variable
12213 and then (Is_Simple_Protected_Type (Obj_Typ)
12214 or else Has_Simple_Protected_Object (Obj_Typ))
12215 then
12216 return True;
12217 end if;
12219 -- Specific cases of object renamings
12221 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12222 Obj_Id := Defining_Identifier (Decl);
12223 Obj_Typ := Base_Type (Etype (Obj_Id));
12225 -- Bypass any form of processing for objects which have their
12226 -- finalization disabled. This applies only to objects at the
12227 -- library level.
12229 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12230 null;
12232 -- Ignored Ghost object renamings do not need any cleanup actions
12233 -- because they will not appear in the final tree.
12235 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12236 null;
12238 -- Return object of a build-in-place function. This case is
12239 -- recognized and marked by the expansion of an extended return
12240 -- statement (see Expand_N_Extended_Return_Statement).
12242 elsif Needs_Finalization (Obj_Typ)
12243 and then Is_Return_Object (Obj_Id)
12244 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12245 then
12246 return True;
12248 -- Detect a case where a source object has been initialized by
12249 -- a controlled function call or another object which was later
12250 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12252 -- Obj1 : CW_Type := Src_Obj;
12253 -- Obj2 : CW_Type := Function_Call (...);
12255 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12256 -- Tmp : ... := Function_Call (...)'reference;
12257 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12259 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12260 return True;
12261 end if;
12263 -- Inspect the freeze node of an access-to-controlled type and look
12264 -- for a delayed finalization master. This case arises when the
12265 -- freeze actions are inserted at a later time than the expansion of
12266 -- the context. Since Build_Finalizer is never called on a single
12267 -- construct twice, the master will be ultimately left out and never
12268 -- finalized. This is also needed for freeze actions of designated
12269 -- types themselves, since in some cases the finalization master is
12270 -- associated with a designated type's freeze node rather than that
12271 -- of the access type (see handling for freeze actions in
12272 -- Build_Finalization_Master).
12274 elsif Nkind (Decl) = N_Freeze_Entity
12275 and then Present (Actions (Decl))
12276 then
12277 Typ := Entity (Decl);
12279 -- Freeze nodes for ignored Ghost types do not need cleanup
12280 -- actions because they will never appear in the final tree.
12282 if Is_Ignored_Ghost_Entity (Typ) then
12283 null;
12285 elsif ((Is_Access_Type (Typ)
12286 and then not Is_Access_Subprogram_Type (Typ)
12287 and then Needs_Finalization
12288 (Available_View (Designated_Type (Typ))))
12289 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12290 and then Requires_Cleanup_Actions
12291 (Actions (Decl), Lib_Level, Nested_Constructs)
12292 then
12293 return True;
12294 end if;
12296 -- Nested package declarations
12298 elsif Nested_Constructs
12299 and then Nkind (Decl) = N_Package_Declaration
12300 then
12301 Pack_Id := Defining_Entity (Decl);
12303 -- Do not inspect an ignored Ghost package because all code found
12304 -- within will not appear in the final tree.
12306 if Is_Ignored_Ghost_Entity (Pack_Id) then
12307 null;
12309 elsif Ekind (Pack_Id) /= E_Generic_Package
12310 and then Requires_Cleanup_Actions
12311 (Specification (Decl), Lib_Level)
12312 then
12313 return True;
12314 end if;
12316 -- Nested package bodies
12318 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12320 -- Do not inspect an ignored Ghost package body because all code
12321 -- found within will not appear in the final tree.
12323 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12324 null;
12326 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12327 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12328 then
12329 return True;
12330 end if;
12332 elsif Nkind (Decl) = N_Block_Statement
12333 and then
12335 -- Handle a rare case caused by a controlled transient object
12336 -- created as part of a record init proc. The variable is wrapped
12337 -- in a block, but the block is not associated with a transient
12338 -- scope.
12340 (Inside_Init_Proc
12342 -- Handle the case where the original context has been wrapped in
12343 -- a block to avoid interference between exception handlers and
12344 -- At_End handlers. Treat the block as transparent and process its
12345 -- contents.
12347 or else Is_Finalization_Wrapper (Decl))
12348 then
12349 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12350 return True;
12351 end if;
12352 end if;
12354 Next (Decl);
12355 end loop;
12357 return False;
12358 end Requires_Cleanup_Actions;
12360 ------------------------------------
12361 -- Safe_Unchecked_Type_Conversion --
12362 ------------------------------------
12364 -- Note: this function knows quite a bit about the exact requirements of
12365 -- Gigi with respect to unchecked type conversions, and its code must be
12366 -- coordinated with any changes in Gigi in this area.
12368 -- The above requirements should be documented in Sinfo ???
12370 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12371 Otyp : Entity_Id;
12372 Ityp : Entity_Id;
12373 Oalign : Uint;
12374 Ialign : Uint;
12375 Pexp : constant Node_Id := Parent (Exp);
12377 begin
12378 -- If the expression is the RHS of an assignment or object declaration
12379 -- we are always OK because there will always be a target.
12381 -- Object renaming declarations, (generated for view conversions of
12382 -- actuals in inlined calls), like object declarations, provide an
12383 -- explicit type, and are safe as well.
12385 if (Nkind (Pexp) = N_Assignment_Statement
12386 and then Expression (Pexp) = Exp)
12387 or else Nkind_In (Pexp, N_Object_Declaration,
12388 N_Object_Renaming_Declaration)
12389 then
12390 return True;
12392 -- If the expression is the prefix of an N_Selected_Component we should
12393 -- also be OK because GCC knows to look inside the conversion except if
12394 -- the type is discriminated. We assume that we are OK anyway if the
12395 -- type is not set yet or if it is controlled since we can't afford to
12396 -- introduce a temporary in this case.
12398 elsif Nkind (Pexp) = N_Selected_Component
12399 and then Prefix (Pexp) = Exp
12400 then
12401 if No (Etype (Pexp)) then
12402 return True;
12403 else
12404 return
12405 not Has_Discriminants (Etype (Pexp))
12406 or else Is_Constrained (Etype (Pexp));
12407 end if;
12408 end if;
12410 -- Set the output type, this comes from Etype if it is set, otherwise we
12411 -- take it from the subtype mark, which we assume was already fully
12412 -- analyzed.
12414 if Present (Etype (Exp)) then
12415 Otyp := Etype (Exp);
12416 else
12417 Otyp := Entity (Subtype_Mark (Exp));
12418 end if;
12420 -- The input type always comes from the expression, and we assume this
12421 -- is indeed always analyzed, so we can simply get the Etype.
12423 Ityp := Etype (Expression (Exp));
12425 -- Initialize alignments to unknown so far
12427 Oalign := No_Uint;
12428 Ialign := No_Uint;
12430 -- Replace a concurrent type by its corresponding record type and each
12431 -- type by its underlying type and do the tests on those. The original
12432 -- type may be a private type whose completion is a concurrent type, so
12433 -- find the underlying type first.
12435 if Present (Underlying_Type (Otyp)) then
12436 Otyp := Underlying_Type (Otyp);
12437 end if;
12439 if Present (Underlying_Type (Ityp)) then
12440 Ityp := Underlying_Type (Ityp);
12441 end if;
12443 if Is_Concurrent_Type (Otyp) then
12444 Otyp := Corresponding_Record_Type (Otyp);
12445 end if;
12447 if Is_Concurrent_Type (Ityp) then
12448 Ityp := Corresponding_Record_Type (Ityp);
12449 end if;
12451 -- If the base types are the same, we know there is no problem since
12452 -- this conversion will be a noop.
12454 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12455 return True;
12457 -- Same if this is an upwards conversion of an untagged type, and there
12458 -- are no constraints involved (could be more general???)
12460 elsif Etype (Ityp) = Otyp
12461 and then not Is_Tagged_Type (Ityp)
12462 and then not Has_Discriminants (Ityp)
12463 and then No (First_Rep_Item (Base_Type (Ityp)))
12464 then
12465 return True;
12467 -- If the expression has an access type (object or subprogram) we assume
12468 -- that the conversion is safe, because the size of the target is safe,
12469 -- even if it is a record (which might be treated as having unknown size
12470 -- at this point).
12472 elsif Is_Access_Type (Ityp) then
12473 return True;
12475 -- If the size of output type is known at compile time, there is never
12476 -- a problem. Note that unconstrained records are considered to be of
12477 -- known size, but we can't consider them that way here, because we are
12478 -- talking about the actual size of the object.
12480 -- We also make sure that in addition to the size being known, we do not
12481 -- have a case which might generate an embarrassingly large temp in
12482 -- stack checking mode.
12484 elsif Size_Known_At_Compile_Time (Otyp)
12485 and then
12486 (not Stack_Checking_Enabled
12487 or else not May_Generate_Large_Temp (Otyp))
12488 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12489 then
12490 return True;
12492 -- If either type is tagged, then we know the alignment is OK so Gigi
12493 -- will be able to use pointer punning.
12495 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12496 return True;
12498 -- If either type is a limited record type, we cannot do a copy, so say
12499 -- safe since there's nothing else we can do.
12501 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12502 return True;
12504 -- Conversions to and from packed array types are always ignored and
12505 -- hence are safe.
12507 elsif Is_Packed_Array_Impl_Type (Otyp)
12508 or else Is_Packed_Array_Impl_Type (Ityp)
12509 then
12510 return True;
12511 end if;
12513 -- The only other cases known to be safe is if the input type's
12514 -- alignment is known to be at least the maximum alignment for the
12515 -- target or if both alignments are known and the output type's
12516 -- alignment is no stricter than the input's. We can use the component
12517 -- type alignment for an array if a type is an unpacked array type.
12519 if Present (Alignment_Clause (Otyp)) then
12520 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12522 elsif Is_Array_Type (Otyp)
12523 and then Present (Alignment_Clause (Component_Type (Otyp)))
12524 then
12525 Oalign := Expr_Value (Expression (Alignment_Clause
12526 (Component_Type (Otyp))));
12527 end if;
12529 if Present (Alignment_Clause (Ityp)) then
12530 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12532 elsif Is_Array_Type (Ityp)
12533 and then Present (Alignment_Clause (Component_Type (Ityp)))
12534 then
12535 Ialign := Expr_Value (Expression (Alignment_Clause
12536 (Component_Type (Ityp))));
12537 end if;
12539 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12540 return True;
12542 elsif Ialign /= No_Uint
12543 and then Oalign /= No_Uint
12544 and then Ialign <= Oalign
12545 then
12546 return True;
12548 -- Otherwise, Gigi cannot handle this and we must make a temporary
12550 else
12551 return False;
12552 end if;
12553 end Safe_Unchecked_Type_Conversion;
12555 ---------------------------------
12556 -- Set_Current_Value_Condition --
12557 ---------------------------------
12559 -- Note: the implementation of this procedure is very closely tied to the
12560 -- implementation of Get_Current_Value_Condition. Here we set required
12561 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12562 -- them, so they must have a consistent view.
12564 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12566 procedure Set_Entity_Current_Value (N : Node_Id);
12567 -- If N is an entity reference, where the entity is of an appropriate
12568 -- kind, then set the current value of this entity to Cnode, unless
12569 -- there is already a definite value set there.
12571 procedure Set_Expression_Current_Value (N : Node_Id);
12572 -- If N is of an appropriate form, sets an appropriate entry in current
12573 -- value fields of relevant entities. Multiple entities can be affected
12574 -- in the case of an AND or AND THEN.
12576 ------------------------------
12577 -- Set_Entity_Current_Value --
12578 ------------------------------
12580 procedure Set_Entity_Current_Value (N : Node_Id) is
12581 begin
12582 if Is_Entity_Name (N) then
12583 declare
12584 Ent : constant Entity_Id := Entity (N);
12586 begin
12587 -- Don't capture if not safe to do so
12589 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12590 return;
12591 end if;
12593 -- Here we have a case where the Current_Value field may need
12594 -- to be set. We set it if it is not already set to a compile
12595 -- time expression value.
12597 -- Note that this represents a decision that one condition
12598 -- blots out another previous one. That's certainly right if
12599 -- they occur at the same level. If the second one is nested,
12600 -- then the decision is neither right nor wrong (it would be
12601 -- equally OK to leave the outer one in place, or take the new
12602 -- inner one. Really we should record both, but our data
12603 -- structures are not that elaborate.
12605 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12606 Set_Current_Value (Ent, Cnode);
12607 end if;
12608 end;
12609 end if;
12610 end Set_Entity_Current_Value;
12612 ----------------------------------
12613 -- Set_Expression_Current_Value --
12614 ----------------------------------
12616 procedure Set_Expression_Current_Value (N : Node_Id) is
12617 Cond : Node_Id;
12619 begin
12620 Cond := N;
12622 -- Loop to deal with (ignore for now) any NOT operators present. The
12623 -- presence of NOT operators will be handled properly when we call
12624 -- Get_Current_Value_Condition.
12626 while Nkind (Cond) = N_Op_Not loop
12627 Cond := Right_Opnd (Cond);
12628 end loop;
12630 -- For an AND or AND THEN, recursively process operands
12632 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12633 Set_Expression_Current_Value (Left_Opnd (Cond));
12634 Set_Expression_Current_Value (Right_Opnd (Cond));
12635 return;
12636 end if;
12638 -- Check possible relational operator
12640 if Nkind (Cond) in N_Op_Compare then
12641 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12642 Set_Entity_Current_Value (Left_Opnd (Cond));
12643 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12644 Set_Entity_Current_Value (Right_Opnd (Cond));
12645 end if;
12647 elsif Nkind_In (Cond,
12648 N_Type_Conversion,
12649 N_Qualified_Expression,
12650 N_Expression_With_Actions)
12651 then
12652 Set_Expression_Current_Value (Expression (Cond));
12654 -- Check possible boolean variable reference
12656 else
12657 Set_Entity_Current_Value (Cond);
12658 end if;
12659 end Set_Expression_Current_Value;
12661 -- Start of processing for Set_Current_Value_Condition
12663 begin
12664 Set_Expression_Current_Value (Condition (Cnode));
12665 end Set_Current_Value_Condition;
12667 --------------------------
12668 -- Set_Elaboration_Flag --
12669 --------------------------
12671 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12672 Loc : constant Source_Ptr := Sloc (N);
12673 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12674 Asn : Node_Id;
12676 begin
12677 if Present (Ent) then
12679 -- Nothing to do if at the compilation unit level, because in this
12680 -- case the flag is set by the binder generated elaboration routine.
12682 if Nkind (Parent (N)) = N_Compilation_Unit then
12683 null;
12685 -- Here we do need to generate an assignment statement
12687 else
12688 Check_Restriction (No_Elaboration_Code, N);
12690 Asn :=
12691 Make_Assignment_Statement (Loc,
12692 Name => New_Occurrence_Of (Ent, Loc),
12693 Expression => Make_Integer_Literal (Loc, Uint_1));
12695 -- Mark the assignment statement as elaboration code. This allows
12696 -- the early call region mechanism (see Sem_Elab) to properly
12697 -- ignore such assignments even though they are non-preelaborable
12698 -- code.
12700 Set_Is_Elaboration_Code (Asn);
12702 if Nkind (Parent (N)) = N_Subunit then
12703 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12704 else
12705 Insert_After (N, Asn);
12706 end if;
12708 Analyze (Asn);
12710 -- Kill current value indication. This is necessary because the
12711 -- tests of this flag are inserted out of sequence and must not
12712 -- pick up bogus indications of the wrong constant value.
12714 Set_Current_Value (Ent, Empty);
12716 -- If the subprogram is in the current declarative part and
12717 -- 'access has been applied to it, generate an elaboration
12718 -- check at the beginning of the declarations of the body.
12720 if Nkind (N) = N_Subprogram_Body
12721 and then Address_Taken (Spec_Id)
12722 and then
12723 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12724 then
12725 declare
12726 Loc : constant Source_Ptr := Sloc (N);
12727 Decls : constant List_Id := Declarations (N);
12728 Chk : Node_Id;
12730 begin
12731 -- No need to generate this check if first entry in the
12732 -- declaration list is a raise of Program_Error now.
12734 if Present (Decls)
12735 and then Nkind (First (Decls)) = N_Raise_Program_Error
12736 then
12737 return;
12738 end if;
12740 -- Otherwise generate the check
12742 Chk :=
12743 Make_Raise_Program_Error (Loc,
12744 Condition =>
12745 Make_Op_Eq (Loc,
12746 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12747 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12748 Reason => PE_Access_Before_Elaboration);
12750 if No (Decls) then
12751 Set_Declarations (N, New_List (Chk));
12752 else
12753 Prepend (Chk, Decls);
12754 end if;
12756 Analyze (Chk);
12757 end;
12758 end if;
12759 end if;
12760 end if;
12761 end Set_Elaboration_Flag;
12763 ----------------------------
12764 -- Set_Renamed_Subprogram --
12765 ----------------------------
12767 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12768 begin
12769 -- If input node is an identifier, we can just reset it
12771 if Nkind (N) = N_Identifier then
12772 Set_Chars (N, Chars (E));
12773 Set_Entity (N, E);
12775 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12777 else
12778 declare
12779 CS : constant Boolean := Comes_From_Source (N);
12780 begin
12781 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12782 Set_Entity (N, E);
12783 Set_Comes_From_Source (N, CS);
12784 Set_Analyzed (N, True);
12785 end;
12786 end if;
12787 end Set_Renamed_Subprogram;
12789 ----------------------
12790 -- Side_Effect_Free --
12791 ----------------------
12793 function Side_Effect_Free
12794 (N : Node_Id;
12795 Name_Req : Boolean := False;
12796 Variable_Ref : Boolean := False) return Boolean
12798 Typ : constant Entity_Id := Etype (N);
12799 -- Result type of the expression
12801 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12802 -- The argument N is a construct where the Prefix is dereferenced if it
12803 -- is an access type and the result is a variable. The call returns True
12804 -- if the construct is side effect free (not considering side effects in
12805 -- other than the prefix which are to be tested by the caller).
12807 function Within_In_Parameter (N : Node_Id) return Boolean;
12808 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12809 -- N is not side-effect free when the actual is global and modifiable
12810 -- indirectly from within a subprogram, because it may be passed by
12811 -- reference. The front-end must be conservative here and assume that
12812 -- this may happen with any array or record type. On the other hand, we
12813 -- cannot create temporaries for all expressions for which this
12814 -- condition is true, for various reasons that might require clearing up
12815 -- ??? For example, discriminant references that appear out of place, or
12816 -- spurious type errors with class-wide expressions. As a result, we
12817 -- limit the transformation to loop bounds, which is so far the only
12818 -- case that requires it.
12820 -----------------------------
12821 -- Safe_Prefixed_Reference --
12822 -----------------------------
12824 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12825 begin
12826 -- If prefix is not side effect free, definitely not safe
12828 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12829 return False;
12831 -- If the prefix is of an access type that is not access-to-constant,
12832 -- then this construct is a variable reference, which means it is to
12833 -- be considered to have side effects if Variable_Ref is set True.
12835 elsif Is_Access_Type (Etype (Prefix (N)))
12836 and then not Is_Access_Constant (Etype (Prefix (N)))
12837 and then Variable_Ref
12838 then
12839 -- Exception is a prefix that is the result of a previous removal
12840 -- of side effects.
12842 return Is_Entity_Name (Prefix (N))
12843 and then not Comes_From_Source (Prefix (N))
12844 and then Ekind (Entity (Prefix (N))) = E_Constant
12845 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12847 -- If the prefix is an explicit dereference then this construct is a
12848 -- variable reference, which means it is to be considered to have
12849 -- side effects if Variable_Ref is True.
12851 -- We do NOT exclude dereferences of access-to-constant types because
12852 -- we handle them as constant view of variables.
12854 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12855 and then Variable_Ref
12856 then
12857 return False;
12859 -- Note: The following test is the simplest way of solving a complex
12860 -- problem uncovered by the following test (Side effect on loop bound
12861 -- that is a subcomponent of a global variable:
12863 -- with Text_Io; use Text_Io;
12864 -- procedure Tloop is
12865 -- type X is
12866 -- record
12867 -- V : Natural := 4;
12868 -- S : String (1..5) := (others => 'a');
12869 -- end record;
12870 -- X1 : X;
12872 -- procedure Modi;
12874 -- generic
12875 -- with procedure Action;
12876 -- procedure Loop_G (Arg : X; Msg : String)
12878 -- procedure Loop_G (Arg : X; Msg : String) is
12879 -- begin
12880 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12881 -- & Natural'Image (Arg.V));
12882 -- for Index in 1 .. Arg.V loop
12883 -- Text_Io.Put_Line
12884 -- (Natural'Image (Index) & " " & Arg.S (Index));
12885 -- if Index > 2 then
12886 -- Modi;
12887 -- end if;
12888 -- end loop;
12889 -- Put_Line ("end loop_g " & Msg);
12890 -- end;
12892 -- procedure Loop1 is new Loop_G (Modi);
12893 -- procedure Modi is
12894 -- begin
12895 -- X1.V := 1;
12896 -- Loop1 (X1, "from modi");
12897 -- end;
12899 -- begin
12900 -- Loop1 (X1, "initial");
12901 -- end;
12903 -- The output of the above program should be:
12905 -- begin loop_g initial will loop till: 4
12906 -- 1 a
12907 -- 2 a
12908 -- 3 a
12909 -- begin loop_g from modi will loop till: 1
12910 -- 1 a
12911 -- end loop_g from modi
12912 -- 4 a
12913 -- begin loop_g from modi will loop till: 1
12914 -- 1 a
12915 -- end loop_g from modi
12916 -- end loop_g initial
12918 -- If a loop bound is a subcomponent of a global variable, a
12919 -- modification of that variable within the loop may incorrectly
12920 -- affect the execution of the loop.
12922 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12923 and then Within_In_Parameter (Prefix (N))
12924 and then Variable_Ref
12925 then
12926 return False;
12928 -- All other cases are side effect free
12930 else
12931 return True;
12932 end if;
12933 end Safe_Prefixed_Reference;
12935 -------------------------
12936 -- Within_In_Parameter --
12937 -------------------------
12939 function Within_In_Parameter (N : Node_Id) return Boolean is
12940 begin
12941 if not Comes_From_Source (N) then
12942 return False;
12944 elsif Is_Entity_Name (N) then
12945 return Ekind (Entity (N)) = E_In_Parameter;
12947 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12948 return Within_In_Parameter (Prefix (N));
12950 else
12951 return False;
12952 end if;
12953 end Within_In_Parameter;
12955 -- Start of processing for Side_Effect_Free
12957 begin
12958 -- If volatile reference, always consider it to have side effects
12960 if Is_Volatile_Reference (N) then
12961 return False;
12962 end if;
12964 -- Note on checks that could raise Constraint_Error. Strictly, if we
12965 -- take advantage of 11.6, these checks do not count as side effects.
12966 -- However, we would prefer to consider that they are side effects,
12967 -- since the back end CSE does not work very well on expressions which
12968 -- can raise Constraint_Error. On the other hand if we don't consider
12969 -- them to be side effect free, then we get some awkward expansions
12970 -- in -gnato mode, resulting in code insertions at a point where we
12971 -- do not have a clear model for performing the insertions.
12973 -- Special handling for entity names
12975 if Is_Entity_Name (N) then
12977 -- A type reference is always side effect free
12979 if Is_Type (Entity (N)) then
12980 return True;
12982 -- Variables are considered to be a side effect if Variable_Ref
12983 -- is set or if we have a volatile reference and Name_Req is off.
12984 -- If Name_Req is True then we can't help returning a name which
12985 -- effectively allows multiple references in any case.
12987 elsif Is_Variable (N, Use_Original_Node => False) then
12988 return not Variable_Ref
12989 and then (not Is_Volatile_Reference (N) or else Name_Req);
12991 -- Any other entity (e.g. a subtype name) is definitely side
12992 -- effect free.
12994 else
12995 return True;
12996 end if;
12998 -- A value known at compile time is always side effect free
13000 elsif Compile_Time_Known_Value (N) then
13001 return True;
13003 -- A variable renaming is not side-effect free, because the renaming
13004 -- will function like a macro in the front-end in some cases, and an
13005 -- assignment can modify the component designated by N, so we need to
13006 -- create a temporary for it.
13008 -- The guard testing for Entity being present is needed at least in
13009 -- the case of rewritten predicate expressions, and may well also be
13010 -- appropriate elsewhere. Obviously we can't go testing the entity
13011 -- field if it does not exist, so it's reasonable to say that this is
13012 -- not the renaming case if it does not exist.
13014 elsif Is_Entity_Name (Original_Node (N))
13015 and then Present (Entity (Original_Node (N)))
13016 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
13017 and then Ekind (Entity (Original_Node (N))) /= E_Constant
13018 then
13019 declare
13020 RO : constant Node_Id :=
13021 Renamed_Object (Entity (Original_Node (N)));
13023 begin
13024 -- If the renamed object is an indexed component, or an
13025 -- explicit dereference, then the designated object could
13026 -- be modified by an assignment.
13028 if Nkind_In (RO, N_Indexed_Component,
13029 N_Explicit_Dereference)
13030 then
13031 return False;
13033 -- A selected component must have a safe prefix
13035 elsif Nkind (RO) = N_Selected_Component then
13036 return Safe_Prefixed_Reference (RO);
13038 -- In all other cases, designated object cannot be changed so
13039 -- we are side effect free.
13041 else
13042 return True;
13043 end if;
13044 end;
13046 -- Remove_Side_Effects generates an object renaming declaration to
13047 -- capture the expression of a class-wide expression. In VM targets
13048 -- the frontend performs no expansion for dispatching calls to
13049 -- class- wide types since they are handled by the VM. Hence, we must
13050 -- locate here if this node corresponds to a previous invocation of
13051 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13053 elsif not Tagged_Type_Expansion
13054 and then not Comes_From_Source (N)
13055 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
13056 and then Is_Class_Wide_Type (Typ)
13057 then
13058 return True;
13060 -- Generating C the type conversion of an access to constrained array
13061 -- type into an access to unconstrained array type involves initializing
13062 -- a fat pointer and the expression cannot be assumed to be free of side
13063 -- effects since it must referenced several times to compute its bounds.
13065 elsif Modify_Tree_For_C
13066 and then Nkind (N) = N_Type_Conversion
13067 and then Is_Access_Type (Typ)
13068 and then Is_Array_Type (Designated_Type (Typ))
13069 and then not Is_Constrained (Designated_Type (Typ))
13070 then
13071 return False;
13072 end if;
13074 -- For other than entity names and compile time known values,
13075 -- check the node kind for special processing.
13077 case Nkind (N) is
13079 -- An attribute reference is side effect free if its expressions
13080 -- are side effect free and its prefix is side effect free or
13081 -- is an entity reference.
13083 -- Is this right? what about x'first where x is a variable???
13085 when N_Attribute_Reference =>
13086 Attribute_Reference : declare
13088 function Side_Effect_Free_Attribute
13089 (Attribute_Name : Name_Id) return Boolean;
13090 -- Returns True if evaluation of the given attribute is
13091 -- considered side-effect free (independent of prefix and
13092 -- arguments).
13094 --------------------------------
13095 -- Side_Effect_Free_Attribute --
13096 --------------------------------
13098 function Side_Effect_Free_Attribute
13099 (Attribute_Name : Name_Id) return Boolean
13101 begin
13102 case Attribute_Name is
13103 when Name_Input =>
13104 return False;
13106 when Name_Image
13107 | Name_Img
13108 | Name_Wide_Image
13109 | Name_Wide_Wide_Image
13111 -- CodePeer doesn't want to see replicated copies of
13112 -- 'Image calls.
13114 return not CodePeer_Mode;
13116 when others =>
13117 return True;
13118 end case;
13119 end Side_Effect_Free_Attribute;
13121 -- Start of processing for Attribute_Reference
13123 begin
13124 return
13125 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13126 and then Side_Effect_Free_Attribute (Attribute_Name (N))
13127 and then (Is_Entity_Name (Prefix (N))
13128 or else Side_Effect_Free
13129 (Prefix (N), Name_Req, Variable_Ref));
13130 end Attribute_Reference;
13132 -- A binary operator is side effect free if and both operands are
13133 -- side effect free. For this purpose binary operators include
13134 -- membership tests and short circuit forms.
13136 when N_Binary_Op
13137 | N_Membership_Test
13138 | N_Short_Circuit
13140 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13141 and then
13142 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13144 -- An explicit dereference is side effect free only if it is
13145 -- a side effect free prefixed reference.
13147 when N_Explicit_Dereference =>
13148 return Safe_Prefixed_Reference (N);
13150 -- An expression with action is side effect free if its expression
13151 -- is side effect free and it has no actions.
13153 when N_Expression_With_Actions =>
13154 return
13155 Is_Empty_List (Actions (N))
13156 and then Side_Effect_Free
13157 (Expression (N), Name_Req, Variable_Ref);
13159 -- A call to _rep_to_pos is side effect free, since we generate
13160 -- this pure function call ourselves. Moreover it is critically
13161 -- important to make this exception, since otherwise we can have
13162 -- discriminants in array components which don't look side effect
13163 -- free in the case of an array whose index type is an enumeration
13164 -- type with an enumeration rep clause.
13166 -- All other function calls are not side effect free
13168 when N_Function_Call =>
13169 return
13170 Nkind (Name (N)) = N_Identifier
13171 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
13172 and then Side_Effect_Free
13173 (First (Parameter_Associations (N)),
13174 Name_Req, Variable_Ref);
13176 -- An IF expression is side effect free if it's of a scalar type, and
13177 -- all its components are all side effect free (conditions and then
13178 -- actions and else actions). We restrict to scalar types, since it
13179 -- is annoying to deal with things like (if A then B else C)'First
13180 -- where the type involved is a string type.
13182 when N_If_Expression =>
13183 return
13184 Is_Scalar_Type (Typ)
13185 and then Side_Effect_Free
13186 (Expressions (N), Name_Req, Variable_Ref);
13188 -- An indexed component is side effect free if it is a side
13189 -- effect free prefixed reference and all the indexing
13190 -- expressions are side effect free.
13192 when N_Indexed_Component =>
13193 return
13194 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13195 and then Safe_Prefixed_Reference (N);
13197 -- A type qualification, type conversion, or unchecked expression is
13198 -- side effect free if the expression is side effect free.
13200 when N_Qualified_Expression
13201 | N_Type_Conversion
13202 | N_Unchecked_Expression
13204 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13206 -- A selected component is side effect free only if it is a side
13207 -- effect free prefixed reference.
13209 when N_Selected_Component =>
13210 return Safe_Prefixed_Reference (N);
13212 -- A range is side effect free if the bounds are side effect free
13214 when N_Range =>
13215 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13216 and then
13217 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13219 -- A slice is side effect free if it is a side effect free
13220 -- prefixed reference and the bounds are side effect free.
13222 when N_Slice =>
13223 return
13224 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13225 and then Safe_Prefixed_Reference (N);
13227 -- A unary operator is side effect free if the operand
13228 -- is side effect free.
13230 when N_Unary_Op =>
13231 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13233 -- An unchecked type conversion is side effect free only if it
13234 -- is safe and its argument is side effect free.
13236 when N_Unchecked_Type_Conversion =>
13237 return
13238 Safe_Unchecked_Type_Conversion (N)
13239 and then Side_Effect_Free
13240 (Expression (N), Name_Req, Variable_Ref);
13242 -- A literal is side effect free
13244 when N_Character_Literal
13245 | N_Integer_Literal
13246 | N_Real_Literal
13247 | N_String_Literal
13249 return True;
13251 -- We consider that anything else has side effects. This is a bit
13252 -- crude, but we are pretty close for most common cases, and we
13253 -- are certainly correct (i.e. we never return True when the
13254 -- answer should be False).
13256 when others =>
13257 return False;
13258 end case;
13259 end Side_Effect_Free;
13261 -- A list is side effect free if all elements of the list are side
13262 -- effect free.
13264 function Side_Effect_Free
13265 (L : List_Id;
13266 Name_Req : Boolean := False;
13267 Variable_Ref : Boolean := False) return Boolean
13269 N : Node_Id;
13271 begin
13272 if L = No_List or else L = Error_List then
13273 return True;
13275 else
13276 N := First (L);
13277 while Present (N) loop
13278 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13279 return False;
13280 else
13281 Next (N);
13282 end if;
13283 end loop;
13285 return True;
13286 end if;
13287 end Side_Effect_Free;
13289 ----------------------------------
13290 -- Silly_Boolean_Array_Not_Test --
13291 ----------------------------------
13293 -- This procedure implements an odd and silly test. We explicitly check
13294 -- for the case where the 'First of the component type is equal to the
13295 -- 'Last of this component type, and if this is the case, we make sure
13296 -- that constraint error is raised. The reason is that the NOT is bound
13297 -- to cause CE in this case, and we will not otherwise catch it.
13299 -- No such check is required for AND and OR, since for both these cases
13300 -- False op False = False, and True op True = True. For the XOR case,
13301 -- see Silly_Boolean_Array_Xor_Test.
13303 -- Believe it or not, this was reported as a bug. Note that nearly always,
13304 -- the test will evaluate statically to False, so the code will be
13305 -- statically removed, and no extra overhead caused.
13307 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13308 Loc : constant Source_Ptr := Sloc (N);
13309 CT : constant Entity_Id := Component_Type (T);
13311 begin
13312 -- The check we install is
13314 -- constraint_error when
13315 -- component_type'first = component_type'last
13316 -- and then array_type'Length /= 0)
13318 -- We need the last guard because we don't want to raise CE for empty
13319 -- arrays since no out of range values result. (Empty arrays with a
13320 -- component type of True .. True -- very useful -- even the ACATS
13321 -- does not test that marginal case).
13323 Insert_Action (N,
13324 Make_Raise_Constraint_Error (Loc,
13325 Condition =>
13326 Make_And_Then (Loc,
13327 Left_Opnd =>
13328 Make_Op_Eq (Loc,
13329 Left_Opnd =>
13330 Make_Attribute_Reference (Loc,
13331 Prefix => New_Occurrence_Of (CT, Loc),
13332 Attribute_Name => Name_First),
13334 Right_Opnd =>
13335 Make_Attribute_Reference (Loc,
13336 Prefix => New_Occurrence_Of (CT, Loc),
13337 Attribute_Name => Name_Last)),
13339 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13340 Reason => CE_Range_Check_Failed));
13341 end Silly_Boolean_Array_Not_Test;
13343 ----------------------------------
13344 -- Silly_Boolean_Array_Xor_Test --
13345 ----------------------------------
13347 -- This procedure implements an odd and silly test. We explicitly check
13348 -- for the XOR case where the component type is True .. True, since this
13349 -- will raise constraint error. A special check is required since CE
13350 -- will not be generated otherwise (cf Expand_Packed_Not).
13352 -- No such check is required for AND and OR, since for both these cases
13353 -- False op False = False, and True op True = True, and no check is
13354 -- required for the case of False .. False, since False xor False = False.
13355 -- See also Silly_Boolean_Array_Not_Test
13357 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13358 Loc : constant Source_Ptr := Sloc (N);
13359 CT : constant Entity_Id := Component_Type (T);
13361 begin
13362 -- The check we install is
13364 -- constraint_error when
13365 -- Boolean (component_type'First)
13366 -- and then Boolean (component_type'Last)
13367 -- and then array_type'Length /= 0)
13369 -- We need the last guard because we don't want to raise CE for empty
13370 -- arrays since no out of range values result (Empty arrays with a
13371 -- component type of True .. True -- very useful -- even the ACATS
13372 -- does not test that marginal case).
13374 Insert_Action (N,
13375 Make_Raise_Constraint_Error (Loc,
13376 Condition =>
13377 Make_And_Then (Loc,
13378 Left_Opnd =>
13379 Make_And_Then (Loc,
13380 Left_Opnd =>
13381 Convert_To (Standard_Boolean,
13382 Make_Attribute_Reference (Loc,
13383 Prefix => New_Occurrence_Of (CT, Loc),
13384 Attribute_Name => Name_First)),
13386 Right_Opnd =>
13387 Convert_To (Standard_Boolean,
13388 Make_Attribute_Reference (Loc,
13389 Prefix => New_Occurrence_Of (CT, Loc),
13390 Attribute_Name => Name_Last))),
13392 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13393 Reason => CE_Range_Check_Failed));
13394 end Silly_Boolean_Array_Xor_Test;
13396 --------------------------
13397 -- Target_Has_Fixed_Ops --
13398 --------------------------
13400 Integer_Sized_Small : Ureal;
13401 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13402 -- called (we don't want to compute it more than once).
13404 Long_Integer_Sized_Small : Ureal;
13405 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13406 -- is called (we don't want to compute it more than once)
13408 First_Time_For_THFO : Boolean := True;
13409 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13411 function Target_Has_Fixed_Ops
13412 (Left_Typ : Entity_Id;
13413 Right_Typ : Entity_Id;
13414 Result_Typ : Entity_Id) return Boolean
13416 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13417 -- Return True if the given type is a fixed-point type with a small
13418 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13419 -- an absolute value less than 1.0. This is currently limited to
13420 -- fixed-point types that map to Integer or Long_Integer.
13422 ------------------------
13423 -- Is_Fractional_Type --
13424 ------------------------
13426 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13427 begin
13428 if Esize (Typ) = Standard_Integer_Size then
13429 return Small_Value (Typ) = Integer_Sized_Small;
13431 elsif Esize (Typ) = Standard_Long_Integer_Size then
13432 return Small_Value (Typ) = Long_Integer_Sized_Small;
13434 else
13435 return False;
13436 end if;
13437 end Is_Fractional_Type;
13439 -- Start of processing for Target_Has_Fixed_Ops
13441 begin
13442 -- Return False if Fractional_Fixed_Ops_On_Target is false
13444 if not Fractional_Fixed_Ops_On_Target then
13445 return False;
13446 end if;
13448 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13449 -- standard constants used by Is_Fractional_Type.
13451 if First_Time_For_THFO then
13452 First_Time_For_THFO := False;
13454 Integer_Sized_Small :=
13455 UR_From_Components
13456 (Num => Uint_1,
13457 Den => UI_From_Int (Standard_Integer_Size - 1),
13458 Rbase => 2);
13460 Long_Integer_Sized_Small :=
13461 UR_From_Components
13462 (Num => Uint_1,
13463 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13464 Rbase => 2);
13465 end if;
13467 -- Return True if target supports fixed-by-fixed multiply/divide for
13468 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13469 -- and result types are equivalent fractional types.
13471 return Is_Fractional_Type (Base_Type (Left_Typ))
13472 and then Is_Fractional_Type (Base_Type (Right_Typ))
13473 and then Is_Fractional_Type (Base_Type (Result_Typ))
13474 and then Esize (Left_Typ) = Esize (Right_Typ)
13475 and then Esize (Left_Typ) = Esize (Result_Typ);
13476 end Target_Has_Fixed_Ops;
13478 -------------------
13479 -- Type_Map_Hash --
13480 -------------------
13482 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13483 begin
13484 return Type_Map_Header (Id mod Type_Map_Size);
13485 end Type_Map_Hash;
13487 ------------------------------------------
13488 -- Type_May_Have_Bit_Aligned_Components --
13489 ------------------------------------------
13491 function Type_May_Have_Bit_Aligned_Components
13492 (Typ : Entity_Id) return Boolean
13494 begin
13495 -- Array type, check component type
13497 if Is_Array_Type (Typ) then
13498 return
13499 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13501 -- Record type, check components
13503 elsif Is_Record_Type (Typ) then
13504 declare
13505 E : Entity_Id;
13507 begin
13508 E := First_Component_Or_Discriminant (Typ);
13509 while Present (E) loop
13510 if Component_May_Be_Bit_Aligned (E)
13511 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13512 then
13513 return True;
13514 end if;
13516 Next_Component_Or_Discriminant (E);
13517 end loop;
13519 return False;
13520 end;
13522 -- Type other than array or record is always OK
13524 else
13525 return False;
13526 end if;
13527 end Type_May_Have_Bit_Aligned_Components;
13529 -------------------------------
13530 -- Update_Primitives_Mapping --
13531 -------------------------------
13533 procedure Update_Primitives_Mapping
13534 (Inher_Id : Entity_Id;
13535 Subp_Id : Entity_Id)
13537 begin
13538 Map_Types
13539 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13540 Derived_Type => Find_Dispatching_Type (Subp_Id));
13541 end Update_Primitives_Mapping;
13543 ----------------------------------
13544 -- Within_Case_Or_If_Expression --
13545 ----------------------------------
13547 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13548 Par : Node_Id;
13550 begin
13551 -- Locate an enclosing case or if expression. Note that these constructs
13552 -- can be expanded into Expression_With_Actions, hence the test of the
13553 -- original node.
13555 Par := Parent (N);
13556 while Present (Par) loop
13557 if Nkind_In (Original_Node (Par), N_Case_Expression,
13558 N_If_Expression)
13559 then
13560 return True;
13562 -- Prevent the search from going too far
13564 elsif Is_Body_Or_Package_Declaration (Par) then
13565 return False;
13566 end if;
13568 Par := Parent (Par);
13569 end loop;
13571 return False;
13572 end Within_Case_Or_If_Expression;
13574 --------------------------------
13575 -- Within_Internal_Subprogram --
13576 --------------------------------
13578 function Within_Internal_Subprogram return Boolean is
13579 S : Entity_Id;
13581 begin
13582 S := Current_Scope;
13583 while Present (S) and then not Is_Subprogram (S) loop
13584 S := Scope (S);
13585 end loop;
13587 return Present (S)
13588 and then Get_TSS_Name (S) /= TSS_Null
13589 and then not Is_Predicate_Function (S)
13590 and then not Is_Predicate_Function_M (S);
13591 end Within_Internal_Subprogram;
13593 end Exp_Util;