Fix compilation failure with C++98 compilers
[official-gcc.git] / gcc / ada / exp_util.adb
blobcf277c1bb5d83be4c12340fd70838a44b047df09
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Exp_Aggr; use Exp_Aggr;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch11; use Exp_Ch11;
38 with Ghost; use Ghost;
39 with Inline; use Inline;
40 with Itypes; use Itypes;
41 with Lib; use Lib;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Ttypes; use Ttypes;
66 with Urealp; use Urealp;
67 with Validsw; use Validsw;
69 with GNAT.HTable;
70 package body Exp_Util is
72 ---------------------------------------------------------
73 -- Handling of inherited class-wide pre/postconditions --
74 ---------------------------------------------------------
76 -- Following AI12-0113, the expression for a class-wide condition is
77 -- transformed for a subprogram that inherits it, by replacing calls
78 -- to primitive operations of the original controlling type into the
79 -- corresponding overriding operations of the derived type. The following
80 -- hash table manages this mapping, and is expanded on demand whenever
81 -- such inherited expression needs to be constructed.
83 -- The mapping is also used to check whether an inherited operation has
84 -- a condition that depends on overridden operations. For such an
85 -- operation we must create a wrapper that is then treated as a normal
86 -- overriding. In SPARK mode such operations are illegal.
88 -- For a given root type there may be several type extensions with their
89 -- own overriding operations, so at various times a given operation of
90 -- the root will be mapped into different overridings. The root type is
91 -- also mapped into the current type extension to indicate that its
92 -- operations are mapped into the overriding operations of that current
93 -- type extension.
95 -- The contents of the map are as follows:
97 -- Key Value
99 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
100 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
101 -- Discriminant (Entity_Id) Expression (Node_Id)
102 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
103 -- Type (Entity_Id) Type (Entity_Id)
105 Type_Map_Size : constant := 511;
107 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
108 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
110 package Type_Map is new GNAT.HTable.Simple_HTable
111 (Header_Num => Type_Map_Header,
112 Key => Entity_Id,
113 Element => Node_Or_Entity_Id,
114 No_element => Empty,
115 Hash => Type_Map_Hash,
116 Equal => "=");
118 -----------------------
119 -- Local Subprograms --
120 -----------------------
122 function Build_Task_Array_Image
123 (Loc : Source_Ptr;
124 Id_Ref : Node_Id;
125 A_Type : Entity_Id;
126 Dyn : Boolean := False) return Node_Id;
127 -- Build function to generate the image string for a task that is an array
128 -- component, concatenating the images of each index. To avoid storage
129 -- leaks, the string is built with successive slice assignments. The flag
130 -- Dyn indicates whether this is called for the initialization procedure of
131 -- an array of tasks, or for the name of a dynamically created task that is
132 -- assigned to an indexed component.
134 function Build_Task_Image_Function
135 (Loc : Source_Ptr;
136 Decls : List_Id;
137 Stats : List_Id;
138 Res : Entity_Id) return Node_Id;
139 -- Common processing for Task_Array_Image and Task_Record_Image. Build
140 -- function body that computes image.
142 procedure Build_Task_Image_Prefix
143 (Loc : Source_Ptr;
144 Len : out Entity_Id;
145 Res : out Entity_Id;
146 Pos : out Entity_Id;
147 Prefix : Entity_Id;
148 Sum : Node_Id;
149 Decls : List_Id;
150 Stats : List_Id);
151 -- Common processing for Task_Array_Image and Task_Record_Image. Create
152 -- local variables and assign prefix of name to result string.
154 function Build_Task_Record_Image
155 (Loc : Source_Ptr;
156 Id_Ref : Node_Id;
157 Dyn : Boolean := False) return Node_Id;
158 -- Build function to generate the image string for a task that is a record
159 -- component. Concatenate name of variable with that of selector. The flag
160 -- Dyn indicates whether this is called for the initialization procedure of
161 -- record with task components, or for a dynamically created task that is
162 -- assigned to a selected component.
164 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
165 -- Force evaluation of bounds of a slice, which may be given by a range
166 -- or by a subtype indication with or without a constraint.
168 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
169 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
170 -- an assertion expression that should be verified at run time.
172 function Make_CW_Equivalent_Type
173 (T : Entity_Id;
174 E : Node_Id) return Entity_Id;
175 -- T is a class-wide type entity, E is the initial expression node that
176 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
177 -- returns the entity of the Equivalent type and inserts on the fly the
178 -- necessary declaration such as:
180 -- type anon is record
181 -- _parent : Root_Type (T); constrained with E discriminants (if any)
182 -- Extension : String (1 .. expr to match size of E);
183 -- end record;
185 -- This record is compatible with any object of the class of T thanks to
186 -- the first field and has the same size as E thanks to the second.
188 function Make_Literal_Range
189 (Loc : Source_Ptr;
190 Literal_Typ : Entity_Id) return Node_Id;
191 -- Produce a Range node whose bounds are:
192 -- Low_Bound (Literal_Type) ..
193 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
194 -- this is used for expanding declarations like X : String := "sdfgdfg";
196 -- If the index type of the target array is not integer, we generate:
197 -- Low_Bound (Literal_Type) ..
198 -- Literal_Type'Val
199 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
200 -- + (Length (Literal_Typ) -1))
202 function Make_Non_Empty_Check
203 (Loc : Source_Ptr;
204 N : Node_Id) return Node_Id;
205 -- Produce a boolean expression checking that the unidimensional array
206 -- node N is not empty.
208 function New_Class_Wide_Subtype
209 (CW_Typ : Entity_Id;
210 N : Node_Id) return Entity_Id;
211 -- Create an implicit subtype of CW_Typ attached to node N
213 function Requires_Cleanup_Actions
214 (L : List_Id;
215 Lib_Level : Boolean;
216 Nested_Constructs : Boolean) return Boolean;
217 -- Given a list L, determine whether it contains one of the following:
219 -- 1) controlled objects
220 -- 2) library-level tagged types
222 -- Lib_Level is True when the list comes from a construct at the library
223 -- level, and False otherwise. Nested_Constructs is True when any nested
224 -- packages declared in L must be processed, and False otherwise.
226 -------------------------------------
227 -- Activate_Atomic_Synchronization --
228 -------------------------------------
230 procedure Activate_Atomic_Synchronization (N : Node_Id) is
231 Msg_Node : Node_Id;
233 begin
234 case Nkind (Parent (N)) is
236 -- Check for cases of appearing in the prefix of a construct where we
237 -- don't need atomic synchronization for this kind of usage.
239 when
240 -- Nothing to do if we are the prefix of an attribute, since we
241 -- do not want an atomic sync operation for things like 'Size.
243 N_Attribute_Reference
245 -- The N_Reference node is like an attribute
247 | N_Reference
249 -- Nothing to do for a reference to a component (or components)
250 -- of a composite object. Only reads and updates of the object
251 -- as a whole require atomic synchronization (RM C.6 (15)).
253 | N_Indexed_Component
254 | N_Selected_Component
255 | N_Slice
257 -- For all the above cases, nothing to do if we are the prefix
259 if Prefix (Parent (N)) = N then
260 return;
261 end if;
263 when others =>
264 null;
265 end case;
267 -- Nothing to do for the identifier in an object renaming declaration,
268 -- the renaming itself does not need atomic synchronization.
270 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
271 return;
272 end if;
274 -- Go ahead and set the flag
276 Set_Atomic_Sync_Required (N);
278 -- Generate info message if requested
280 if Warn_On_Atomic_Synchronization then
281 case Nkind (N) is
282 when N_Identifier =>
283 Msg_Node := N;
285 when N_Expanded_Name
286 | N_Selected_Component
288 Msg_Node := Selector_Name (N);
290 when N_Explicit_Dereference
291 | N_Indexed_Component
293 Msg_Node := Empty;
295 when others =>
296 pragma Assert (False);
297 return;
298 end case;
300 if Present (Msg_Node) then
301 Error_Msg_N
302 ("info: atomic synchronization set for &?N?", Msg_Node);
303 else
304 Error_Msg_N
305 ("info: atomic synchronization set?N?", N);
306 end if;
307 end if;
308 end Activate_Atomic_Synchronization;
310 ----------------------
311 -- Adjust_Condition --
312 ----------------------
314 procedure Adjust_Condition (N : Node_Id) is
315 begin
316 if No (N) then
317 return;
318 end if;
320 declare
321 Loc : constant Source_Ptr := Sloc (N);
322 T : constant Entity_Id := Etype (N);
323 Ti : Entity_Id;
325 begin
326 -- Defend against a call where the argument has no type, or has a
327 -- type that is not Boolean. This can occur because of prior errors.
329 if No (T) or else not Is_Boolean_Type (T) then
330 return;
331 end if;
333 -- Apply validity checking if needed
335 if Validity_Checks_On and Validity_Check_Tests then
336 Ensure_Valid (N);
337 end if;
339 -- Immediate return if standard boolean, the most common case,
340 -- where nothing needs to be done.
342 if Base_Type (T) = Standard_Boolean then
343 return;
344 end if;
346 -- Case of zero/non-zero semantics or non-standard enumeration
347 -- representation. In each case, we rewrite the node as:
349 -- ityp!(N) /= False'Enum_Rep
351 -- where ityp is an integer type with large enough size to hold any
352 -- value of type T.
354 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
355 if Esize (T) <= Esize (Standard_Integer) then
356 Ti := Standard_Integer;
357 else
358 Ti := Standard_Long_Long_Integer;
359 end if;
361 Rewrite (N,
362 Make_Op_Ne (Loc,
363 Left_Opnd => Unchecked_Convert_To (Ti, N),
364 Right_Opnd =>
365 Make_Attribute_Reference (Loc,
366 Attribute_Name => Name_Enum_Rep,
367 Prefix =>
368 New_Occurrence_Of (First_Literal (T), Loc))));
369 Analyze_And_Resolve (N, Standard_Boolean);
371 else
372 Rewrite (N, Convert_To (Standard_Boolean, N));
373 Analyze_And_Resolve (N, Standard_Boolean);
374 end if;
375 end;
376 end Adjust_Condition;
378 ------------------------
379 -- Adjust_Result_Type --
380 ------------------------
382 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
383 begin
384 -- Ignore call if current type is not Standard.Boolean
386 if Etype (N) /= Standard_Boolean then
387 return;
388 end if;
390 -- If result is already of correct type, nothing to do. Note that
391 -- this will get the most common case where everything has a type
392 -- of Standard.Boolean.
394 if Base_Type (T) = Standard_Boolean then
395 return;
397 else
398 declare
399 KP : constant Node_Kind := Nkind (Parent (N));
401 begin
402 -- If result is to be used as a Condition in the syntax, no need
403 -- to convert it back, since if it was changed to Standard.Boolean
404 -- using Adjust_Condition, that is just fine for this usage.
406 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
407 return;
409 -- If result is an operand of another logical operation, no need
410 -- to reset its type, since Standard.Boolean is just fine, and
411 -- such operations always do Adjust_Condition on their operands.
413 elsif KP in N_Op_Boolean
414 or else KP in N_Short_Circuit
415 or else KP = N_Op_Not
416 then
417 return;
419 -- Otherwise we perform a conversion from the current type, which
420 -- must be Standard.Boolean, to the desired type. Use the base
421 -- type to prevent spurious constraint checks that are extraneous
422 -- to the transformation. The type and its base have the same
423 -- representation, standard or otherwise.
425 else
426 Set_Analyzed (N);
427 Rewrite (N, Convert_To (Base_Type (T), N));
428 Analyze_And_Resolve (N, Base_Type (T));
429 end if;
430 end;
431 end if;
432 end Adjust_Result_Type;
434 --------------------------
435 -- Append_Freeze_Action --
436 --------------------------
438 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
439 Fnode : Node_Id;
441 begin
442 Ensure_Freeze_Node (T);
443 Fnode := Freeze_Node (T);
445 if No (Actions (Fnode)) then
446 Set_Actions (Fnode, New_List (N));
447 else
448 Append (N, Actions (Fnode));
449 end if;
451 end Append_Freeze_Action;
453 ---------------------------
454 -- Append_Freeze_Actions --
455 ---------------------------
457 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
458 Fnode : Node_Id;
460 begin
461 if No (L) then
462 return;
463 end if;
465 Ensure_Freeze_Node (T);
466 Fnode := Freeze_Node (T);
468 if No (Actions (Fnode)) then
469 Set_Actions (Fnode, L);
470 else
471 Append_List (L, Actions (Fnode));
472 end if;
473 end Append_Freeze_Actions;
475 ------------------------------------
476 -- Build_Allocate_Deallocate_Proc --
477 ------------------------------------
479 procedure Build_Allocate_Deallocate_Proc
480 (N : Node_Id;
481 Is_Allocate : Boolean)
483 function Find_Object (E : Node_Id) return Node_Id;
484 -- Given an arbitrary expression of an allocator, try to find an object
485 -- reference in it, otherwise return the original expression.
487 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
488 -- Determine whether subprogram Subp denotes a custom allocate or
489 -- deallocate.
491 -----------------
492 -- Find_Object --
493 -----------------
495 function Find_Object (E : Node_Id) return Node_Id is
496 Expr : Node_Id;
498 begin
499 pragma Assert (Is_Allocate);
501 Expr := E;
502 loop
503 if Nkind (Expr) = N_Explicit_Dereference then
504 Expr := Prefix (Expr);
506 elsif Nkind (Expr) = N_Qualified_Expression then
507 Expr := Expression (Expr);
509 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
511 -- When interface class-wide types are involved in allocation,
512 -- the expander introduces several levels of address arithmetic
513 -- to perform dispatch table displacement. In this scenario the
514 -- object appears as:
516 -- Tag_Ptr (Base_Address (<object>'Address))
518 -- Detect this case and utilize the whole expression as the
519 -- "object" since it now points to the proper dispatch table.
521 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
522 exit;
524 -- Continue to strip the object
526 else
527 Expr := Expression (Expr);
528 end if;
530 else
531 exit;
532 end if;
533 end loop;
535 return Expr;
536 end Find_Object;
538 ---------------------------------
539 -- Is_Allocate_Deallocate_Proc --
540 ---------------------------------
542 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
543 begin
544 -- Look for a subprogram body with only one statement which is a
545 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
547 if Ekind (Subp) = E_Procedure
548 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
549 then
550 declare
551 HSS : constant Node_Id :=
552 Handled_Statement_Sequence (Parent (Parent (Subp)));
553 Proc : Entity_Id;
555 begin
556 if Present (Statements (HSS))
557 and then Nkind (First (Statements (HSS))) =
558 N_Procedure_Call_Statement
559 then
560 Proc := Entity (Name (First (Statements (HSS))));
562 return
563 Is_RTE (Proc, RE_Allocate_Any_Controlled)
564 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
565 end if;
566 end;
567 end if;
569 return False;
570 end Is_Allocate_Deallocate_Proc;
572 -- Local variables
574 Desig_Typ : Entity_Id;
575 Expr : Node_Id;
576 Needs_Fin : Boolean;
577 Pool_Id : Entity_Id;
578 Proc_To_Call : Node_Id := Empty;
579 Ptr_Typ : Entity_Id;
581 -- Start of processing for Build_Allocate_Deallocate_Proc
583 begin
584 -- Obtain the attributes of the allocation / deallocation
586 if Nkind (N) = N_Free_Statement then
587 Expr := Expression (N);
588 Ptr_Typ := Base_Type (Etype (Expr));
589 Proc_To_Call := Procedure_To_Call (N);
591 else
592 if Nkind (N) = N_Object_Declaration then
593 Expr := Expression (N);
594 else
595 Expr := N;
596 end if;
598 -- In certain cases an allocator with a qualified expression may
599 -- be relocated and used as the initialization expression of a
600 -- temporary:
602 -- before:
603 -- Obj : Ptr_Typ := new Desig_Typ'(...);
605 -- after:
606 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
607 -- Obj : Ptr_Typ := Tmp;
609 -- Since the allocator is always marked as analyzed to avoid infinite
610 -- expansion, it will never be processed by this routine given that
611 -- the designated type needs finalization actions. Detect this case
612 -- and complete the expansion of the allocator.
614 if Nkind (Expr) = N_Identifier
615 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
616 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
617 then
618 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
619 return;
620 end if;
622 -- The allocator may have been rewritten into something else in which
623 -- case the expansion performed by this routine does not apply.
625 if Nkind (Expr) /= N_Allocator then
626 return;
627 end if;
629 Ptr_Typ := Base_Type (Etype (Expr));
630 Proc_To_Call := Procedure_To_Call (Expr);
631 end if;
633 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
634 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
636 -- Handle concurrent types
638 if Is_Concurrent_Type (Desig_Typ)
639 and then Present (Corresponding_Record_Type (Desig_Typ))
640 then
641 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
642 end if;
644 -- Do not process allocations / deallocations without a pool
646 if No (Pool_Id) then
647 return;
649 -- Do not process allocations on / deallocations from the secondary
650 -- stack.
652 elsif Is_RTE (Pool_Id, RE_SS_Pool)
653 or else (Nkind (Expr) = N_Allocator
654 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool))
655 then
656 return;
658 -- Optimize the case where we are using the default Global_Pool_Object,
659 -- and we don't need the heavy finalization machinery.
661 elsif Pool_Id = RTE (RE_Global_Pool_Object)
662 and then not Needs_Finalization (Desig_Typ)
663 then
664 return;
666 -- Do not replicate the machinery if the allocator / free has already
667 -- been expanded and has a custom Allocate / Deallocate.
669 elsif Present (Proc_To_Call)
670 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
671 then
672 return;
673 end if;
675 -- Finalization actions are required when the object to be allocated or
676 -- deallocated needs these actions and the associated access type is not
677 -- subject to pragma No_Heap_Finalization.
679 Needs_Fin :=
680 Needs_Finalization (Desig_Typ)
681 and then not No_Heap_Finalization (Ptr_Typ);
683 if Needs_Fin then
685 -- Do nothing if the access type may never allocate / deallocate
686 -- objects.
688 if No_Pool_Assigned (Ptr_Typ) then
689 return;
690 end if;
692 -- The allocation / deallocation of a controlled object must be
693 -- chained on / detached from a finalization master.
695 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
697 -- The only other kind of allocation / deallocation supported by this
698 -- routine is on / from a subpool.
700 elsif Nkind (Expr) = N_Allocator
701 and then No (Subpool_Handle_Name (Expr))
702 then
703 return;
704 end if;
706 declare
707 Loc : constant Source_Ptr := Sloc (N);
708 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
709 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
710 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
711 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
713 Actuals : List_Id;
714 Fin_Addr_Id : Entity_Id;
715 Fin_Mas_Act : Node_Id;
716 Fin_Mas_Id : Entity_Id;
717 Proc_To_Call : Entity_Id;
718 Subpool : Node_Id := Empty;
720 begin
721 -- Step 1: Construct all the actuals for the call to library routine
722 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
724 -- a) Storage pool
726 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
728 if Is_Allocate then
730 -- b) Subpool
732 if Nkind (Expr) = N_Allocator then
733 Subpool := Subpool_Handle_Name (Expr);
734 end if;
736 -- If a subpool is present it can be an arbitrary name, so make
737 -- the actual by copying the tree.
739 if Present (Subpool) then
740 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
741 else
742 Append_To (Actuals, Make_Null (Loc));
743 end if;
745 -- c) Finalization master
747 if Needs_Fin then
748 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
749 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
751 -- Handle the case where the master is actually a pointer to a
752 -- master. This case arises in build-in-place functions.
754 if Is_Access_Type (Etype (Fin_Mas_Id)) then
755 Append_To (Actuals, Fin_Mas_Act);
756 else
757 Append_To (Actuals,
758 Make_Attribute_Reference (Loc,
759 Prefix => Fin_Mas_Act,
760 Attribute_Name => Name_Unrestricted_Access));
761 end if;
762 else
763 Append_To (Actuals, Make_Null (Loc));
764 end if;
766 -- d) Finalize_Address
768 -- Primitive Finalize_Address is never generated in CodePeer mode
769 -- since it contains an Unchecked_Conversion.
771 if Needs_Fin and then not CodePeer_Mode then
772 Fin_Addr_Id := Finalize_Address (Desig_Typ);
773 pragma Assert (Present (Fin_Addr_Id));
775 Append_To (Actuals,
776 Make_Attribute_Reference (Loc,
777 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
778 Attribute_Name => Name_Unrestricted_Access));
779 else
780 Append_To (Actuals, Make_Null (Loc));
781 end if;
782 end if;
784 -- e) Address
785 -- f) Storage_Size
786 -- g) Alignment
788 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
789 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
791 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
792 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
794 -- For deallocation of class-wide types we obtain the value of
795 -- alignment from the Type Specific Record of the deallocated object.
796 -- This is needed because the frontend expansion of class-wide types
797 -- into equivalent types confuses the back end.
799 else
800 -- Generate:
801 -- Obj.all'Alignment
803 -- ... because 'Alignment applied to class-wide types is expanded
804 -- into the code that reads the value of alignment from the TSD
805 -- (see Expand_N_Attribute_Reference)
807 Append_To (Actuals,
808 Unchecked_Convert_To (RTE (RE_Storage_Offset),
809 Make_Attribute_Reference (Loc,
810 Prefix =>
811 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
812 Attribute_Name => Name_Alignment)));
813 end if;
815 -- h) Is_Controlled
817 if Needs_Fin then
818 Is_Controlled : declare
819 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
820 Flag_Expr : Node_Id;
821 Param : Node_Id;
822 Pref : Node_Id;
823 Temp : Node_Id;
825 begin
826 if Is_Allocate then
827 Temp := Find_Object (Expression (Expr));
828 else
829 Temp := Expr;
830 end if;
832 -- Processing for allocations where the expression is a subtype
833 -- indication.
835 if Is_Allocate
836 and then Is_Entity_Name (Temp)
837 and then Is_Type (Entity (Temp))
838 then
839 Flag_Expr :=
840 New_Occurrence_Of
841 (Boolean_Literals
842 (Needs_Finalization (Entity (Temp))), Loc);
844 -- The allocation / deallocation of a class-wide object relies
845 -- on a runtime check to determine whether the object is truly
846 -- controlled or not. Depending on this check, the finalization
847 -- machinery will request or reclaim extra storage reserved for
848 -- a list header.
850 elsif Is_Class_Wide_Type (Desig_Typ) then
852 -- Detect a special case where interface class-wide types
853 -- are involved as the object appears as:
855 -- Tag_Ptr (Base_Address (<object>'Address))
857 -- The expression already yields the proper tag, generate:
859 -- Temp.all
861 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
862 Param :=
863 Make_Explicit_Dereference (Loc,
864 Prefix => Relocate_Node (Temp));
866 -- In the default case, obtain the tag of the object about
867 -- to be allocated / deallocated. Generate:
869 -- Temp'Tag
871 -- If the object is an unchecked conversion (typically to
872 -- an access to class-wide type), we must preserve the
873 -- conversion to ensure that the object is seen as tagged
874 -- in the code that follows.
876 else
877 Pref := Temp;
879 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
880 then
881 Pref := Parent (Pref);
882 end if;
884 Param :=
885 Make_Attribute_Reference (Loc,
886 Prefix => Relocate_Node (Pref),
887 Attribute_Name => Name_Tag);
888 end if;
890 -- Generate:
891 -- Needs_Finalization (<Param>)
893 Flag_Expr :=
894 Make_Function_Call (Loc,
895 Name =>
896 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
897 Parameter_Associations => New_List (Param));
899 -- Processing for generic actuals
901 elsif Is_Generic_Actual_Type (Desig_Typ) then
902 Flag_Expr :=
903 New_Occurrence_Of (Boolean_Literals
904 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
906 -- The object does not require any specialized checks, it is
907 -- known to be controlled.
909 else
910 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
911 end if;
913 -- Create the temporary which represents the finalization state
914 -- of the expression. Generate:
916 -- F : constant Boolean := <Flag_Expr>;
918 Insert_Action (N,
919 Make_Object_Declaration (Loc,
920 Defining_Identifier => Flag_Id,
921 Constant_Present => True,
922 Object_Definition =>
923 New_Occurrence_Of (Standard_Boolean, Loc),
924 Expression => Flag_Expr));
926 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
927 end Is_Controlled;
929 -- The object is not controlled
931 else
932 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
933 end if;
935 -- i) On_Subpool
937 if Is_Allocate then
938 Append_To (Actuals,
939 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
940 end if;
942 -- Step 2: Build a wrapper Allocate / Deallocate which internally
943 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
945 -- Select the proper routine to call
947 if Is_Allocate then
948 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
949 else
950 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
951 end if;
953 -- Create a custom Allocate / Deallocate routine which has identical
954 -- profile to that of System.Storage_Pools.
956 Insert_Action (N,
957 Make_Subprogram_Body (Loc,
958 Specification =>
960 -- procedure Pnn
962 Make_Procedure_Specification (Loc,
963 Defining_Unit_Name => Proc_Id,
964 Parameter_Specifications => New_List (
966 -- P : Root_Storage_Pool
968 Make_Parameter_Specification (Loc,
969 Defining_Identifier => Make_Temporary (Loc, 'P'),
970 Parameter_Type =>
971 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
973 -- A : [out] Address
975 Make_Parameter_Specification (Loc,
976 Defining_Identifier => Addr_Id,
977 Out_Present => Is_Allocate,
978 Parameter_Type =>
979 New_Occurrence_Of (RTE (RE_Address), Loc)),
981 -- S : Storage_Count
983 Make_Parameter_Specification (Loc,
984 Defining_Identifier => Size_Id,
985 Parameter_Type =>
986 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
988 -- L : Storage_Count
990 Make_Parameter_Specification (Loc,
991 Defining_Identifier => Alig_Id,
992 Parameter_Type =>
993 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
995 Declarations => No_List,
997 Handled_Statement_Sequence =>
998 Make_Handled_Sequence_Of_Statements (Loc,
999 Statements => New_List (
1000 Make_Procedure_Call_Statement (Loc,
1001 Name =>
1002 New_Occurrence_Of (Proc_To_Call, Loc),
1003 Parameter_Associations => Actuals)))),
1004 Suppress => All_Checks);
1006 -- The newly generated Allocate / Deallocate becomes the default
1007 -- procedure to call when the back end processes the allocation /
1008 -- deallocation.
1010 if Is_Allocate then
1011 Set_Procedure_To_Call (Expr, Proc_Id);
1012 else
1013 Set_Procedure_To_Call (N, Proc_Id);
1014 end if;
1015 end;
1016 end Build_Allocate_Deallocate_Proc;
1018 -------------------------------
1019 -- Build_Abort_Undefer_Block --
1020 -------------------------------
1022 function Build_Abort_Undefer_Block
1023 (Loc : Source_Ptr;
1024 Stmts : List_Id;
1025 Context : Node_Id) return Node_Id
1027 Exceptions_OK : constant Boolean :=
1028 not Restriction_Active (No_Exception_Propagation);
1030 AUD : Entity_Id;
1031 Blk : Node_Id;
1032 Blk_Id : Entity_Id;
1033 HSS : Node_Id;
1035 begin
1036 -- The block should be generated only when undeferring abort in the
1037 -- context of a potential exception.
1039 pragma Assert (Abort_Allowed and Exceptions_OK);
1041 -- Generate:
1042 -- begin
1043 -- <Stmts>
1044 -- at end
1045 -- Abort_Undefer_Direct;
1046 -- end;
1048 AUD := RTE (RE_Abort_Undefer_Direct);
1050 HSS :=
1051 Make_Handled_Sequence_Of_Statements (Loc,
1052 Statements => Stmts,
1053 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1055 Blk :=
1056 Make_Block_Statement (Loc,
1057 Handled_Statement_Sequence => HSS);
1058 Set_Is_Abort_Block (Blk);
1060 Add_Block_Identifier (Blk, Blk_Id);
1061 Expand_At_End_Handler (HSS, Blk_Id);
1063 -- Present the Abort_Undefer_Direct function to the back end to inline
1064 -- the call to the routine.
1066 Add_Inlined_Body (AUD, Context);
1068 return Blk;
1069 end Build_Abort_Undefer_Block;
1071 ---------------------------------
1072 -- Build_Class_Wide_Expression --
1073 ---------------------------------
1075 procedure Build_Class_Wide_Expression
1076 (Prag : Node_Id;
1077 Subp : Entity_Id;
1078 Par_Subp : Entity_Id;
1079 Adjust_Sloc : Boolean;
1080 Needs_Wrapper : out Boolean)
1082 function Replace_Entity (N : Node_Id) return Traverse_Result;
1083 -- Replace reference to formal of inherited operation or to primitive
1084 -- operation of root type, with corresponding entity for derived type,
1085 -- when constructing the class-wide condition of an overriding
1086 -- subprogram.
1088 --------------------
1089 -- Replace_Entity --
1090 --------------------
1092 function Replace_Entity (N : Node_Id) return Traverse_Result is
1093 New_E : Entity_Id;
1095 begin
1096 if Adjust_Sloc then
1097 Adjust_Inherited_Pragma_Sloc (N);
1098 end if;
1100 if Nkind (N) = N_Identifier
1101 and then Present (Entity (N))
1102 and then
1103 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1104 and then
1105 (Nkind (Parent (N)) /= N_Attribute_Reference
1106 or else Attribute_Name (Parent (N)) /= Name_Class)
1107 then
1108 -- The replacement does not apply to dispatching calls within the
1109 -- condition, but only to calls whose static tag is that of the
1110 -- parent type.
1112 if Is_Subprogram (Entity (N))
1113 and then Nkind (Parent (N)) = N_Function_Call
1114 and then Present (Controlling_Argument (Parent (N)))
1115 then
1116 return OK;
1117 end if;
1119 -- Determine whether entity has a renaming
1121 New_E := Type_Map.Get (Entity (N));
1123 if Present (New_E) then
1124 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1126 -- AI12-0166: a precondition for a protected operation
1127 -- cannot include an internal call to a protected function
1128 -- of the type. In the case of an inherited condition for an
1129 -- overriding operation, both the operation and the function
1130 -- are given by primitive wrappers.
1132 if Ekind (New_E) = E_Function
1133 and then Is_Primitive_Wrapper (New_E)
1134 and then Is_Primitive_Wrapper (Subp)
1135 and then Scope (Subp) = Scope (New_E)
1136 then
1137 Error_Msg_Node_2 := Wrapped_Entity (Subp);
1138 Error_Msg_NE
1139 ("internal call to& cannot appear in inherited "
1140 & "precondition of protected operation&",
1141 N, Wrapped_Entity (New_E));
1142 end if;
1144 -- If the entity is an overridden primitive and we are not
1145 -- in GNATprove mode, we must build a wrapper for the current
1146 -- inherited operation. If the reference is the prefix of an
1147 -- attribute such as 'Result (or others ???) there is no need
1148 -- for a wrapper: the condition is just rewritten in terms of
1149 -- the inherited subprogram.
1151 if Is_Subprogram (New_E)
1152 and then Nkind (Parent (N)) /= N_Attribute_Reference
1153 and then not GNATprove_Mode
1154 then
1155 Needs_Wrapper := True;
1156 end if;
1157 end if;
1159 -- Check that there are no calls left to abstract operations if
1160 -- the current subprogram is not abstract.
1162 if Nkind (Parent (N)) = N_Function_Call
1163 and then N = Name (Parent (N))
1164 then
1165 if not Is_Abstract_Subprogram (Subp)
1166 and then Is_Abstract_Subprogram (Entity (N))
1167 then
1168 Error_Msg_Sloc := Sloc (Current_Scope);
1169 Error_Msg_Node_2 := Subp;
1170 if Comes_From_Source (Subp) then
1171 Error_Msg_NE
1172 ("cannot call abstract subprogram & in inherited "
1173 & "condition for&#", Subp, Entity (N));
1174 else
1175 Error_Msg_NE
1176 ("cannot call abstract subprogram & in inherited "
1177 & "condition for inherited&#", Subp, Entity (N));
1178 end if;
1180 -- In SPARK mode, reject an inherited condition for an
1181 -- inherited operation if it contains a call to an overriding
1182 -- operation, because this implies that the pre/postconditions
1183 -- of the inherited operation have changed silently.
1185 elsif SPARK_Mode = On
1186 and then Warn_On_Suspicious_Contract
1187 and then Present (Alias (Subp))
1188 and then Present (New_E)
1189 and then Comes_From_Source (New_E)
1190 then
1191 Error_Msg_N
1192 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1193 Parent (Subp));
1194 Error_Msg_Sloc := Sloc (New_E);
1195 Error_Msg_Node_2 := Subp;
1196 Error_Msg_NE
1197 ("\overriding of&# forces overriding of&",
1198 Parent (Subp), New_E);
1199 end if;
1200 end if;
1202 -- Update type of function call node, which should be the same as
1203 -- the function's return type.
1205 if Is_Subprogram (Entity (N))
1206 and then Nkind (Parent (N)) = N_Function_Call
1207 then
1208 Set_Etype (Parent (N), Etype (Entity (N)));
1209 end if;
1211 -- The whole expression will be reanalyzed
1213 elsif Nkind (N) in N_Has_Etype then
1214 Set_Analyzed (N, False);
1215 end if;
1217 return OK;
1218 end Replace_Entity;
1220 procedure Replace_Condition_Entities is
1221 new Traverse_Proc (Replace_Entity);
1223 -- Local variables
1225 Par_Formal : Entity_Id;
1226 Subp_Formal : Entity_Id;
1228 -- Start of processing for Build_Class_Wide_Expression
1230 begin
1231 Needs_Wrapper := False;
1233 -- Add mapping from old formals to new formals
1235 Par_Formal := First_Formal (Par_Subp);
1236 Subp_Formal := First_Formal (Subp);
1238 while Present (Par_Formal) and then Present (Subp_Formal) loop
1239 Type_Map.Set (Par_Formal, Subp_Formal);
1240 Next_Formal (Par_Formal);
1241 Next_Formal (Subp_Formal);
1242 end loop;
1244 Replace_Condition_Entities (Prag);
1245 end Build_Class_Wide_Expression;
1247 --------------------
1248 -- Build_DIC_Call --
1249 --------------------
1251 function Build_DIC_Call
1252 (Loc : Source_Ptr;
1253 Obj_Id : Entity_Id;
1254 Typ : Entity_Id) return Node_Id
1256 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1257 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1259 begin
1260 return
1261 Make_Procedure_Call_Statement (Loc,
1262 Name => New_Occurrence_Of (Proc_Id, Loc),
1263 Parameter_Associations => New_List (
1264 Make_Unchecked_Type_Conversion (Loc,
1265 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1266 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1267 end Build_DIC_Call;
1269 ------------------------------
1270 -- Build_DIC_Procedure_Body --
1271 ------------------------------
1273 -- WARNING: This routine manages Ghost regions. Return statements must be
1274 -- replaced by gotos which jump to the end of the routine and restore the
1275 -- Ghost mode.
1277 procedure Build_DIC_Procedure_Body
1278 (Typ : Entity_Id;
1279 For_Freeze : Boolean := False)
1281 procedure Add_DIC_Check
1282 (DIC_Prag : Node_Id;
1283 DIC_Expr : Node_Id;
1284 Stmts : in out List_Id);
1285 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1286 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1287 -- is added to list Stmts.
1289 procedure Add_Inherited_DIC
1290 (DIC_Prag : Node_Id;
1291 Par_Typ : Entity_Id;
1292 Deriv_Typ : Entity_Id;
1293 Stmts : in out List_Id);
1294 -- Add a runtime check to verify the assertion expression of inherited
1295 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1296 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1297 -- pragma. All generated code is added to list Stmts.
1299 procedure Add_Inherited_Tagged_DIC
1300 (DIC_Prag : Node_Id;
1301 Par_Typ : Entity_Id;
1302 Deriv_Typ : Entity_Id;
1303 Stmts : in out List_Id);
1304 -- Add a runtime check to verify assertion expression DIC_Expr of
1305 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1306 -- postcondition-like runtime semantics to the check. Par_Typ is the
1307 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1308 -- derived type inheriting the DIC pragma. All generated code is added
1309 -- to list Stmts.
1311 procedure Add_Own_DIC
1312 (DIC_Prag : Node_Id;
1313 DIC_Typ : Entity_Id;
1314 Stmts : in out List_Id);
1315 -- Add a runtime check to verify the assertion expression of pragma
1316 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1317 -- is added to list Stmts.
1319 -------------------
1320 -- Add_DIC_Check --
1321 -------------------
1323 procedure Add_DIC_Check
1324 (DIC_Prag : Node_Id;
1325 DIC_Expr : Node_Id;
1326 Stmts : in out List_Id)
1328 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1329 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1331 begin
1332 -- The DIC pragma is ignored, nothing left to do
1334 if Is_Ignored (DIC_Prag) then
1335 null;
1337 -- Otherwise the DIC expression must be checked at run time.
1338 -- Generate:
1340 -- pragma Check (<Nam>, <DIC_Expr>);
1342 else
1343 Append_New_To (Stmts,
1344 Make_Pragma (Loc,
1345 Pragma_Identifier =>
1346 Make_Identifier (Loc, Name_Check),
1348 Pragma_Argument_Associations => New_List (
1349 Make_Pragma_Argument_Association (Loc,
1350 Expression => Make_Identifier (Loc, Nam)),
1352 Make_Pragma_Argument_Association (Loc,
1353 Expression => DIC_Expr))));
1354 end if;
1355 end Add_DIC_Check;
1357 -----------------------
1358 -- Add_Inherited_DIC --
1359 -----------------------
1361 procedure Add_Inherited_DIC
1362 (DIC_Prag : Node_Id;
1363 Par_Typ : Entity_Id;
1364 Deriv_Typ : Entity_Id;
1365 Stmts : in out List_Id)
1367 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1368 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1369 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1370 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1371 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1373 begin
1374 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1376 -- Verify the inherited DIC assertion expression by calling the DIC
1377 -- procedure of the parent type.
1379 -- Generate:
1380 -- <Par_Typ>DIC (Par_Typ (_object));
1382 Append_New_To (Stmts,
1383 Make_Procedure_Call_Statement (Loc,
1384 Name => New_Occurrence_Of (Par_Proc, Loc),
1385 Parameter_Associations => New_List (
1386 Convert_To
1387 (Typ => Etype (Par_Obj),
1388 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1389 end Add_Inherited_DIC;
1391 ------------------------------
1392 -- Add_Inherited_Tagged_DIC --
1393 ------------------------------
1395 procedure Add_Inherited_Tagged_DIC
1396 (DIC_Prag : Node_Id;
1397 Par_Typ : Entity_Id;
1398 Deriv_Typ : Entity_Id;
1399 Stmts : in out List_Id)
1401 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1402 DIC_Args : constant List_Id :=
1403 Pragma_Argument_Associations (DIC_Prag);
1404 DIC_Arg : constant Node_Id := First (DIC_Args);
1405 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1406 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1408 Expr : Node_Id;
1410 begin
1411 -- The processing of an inherited DIC assertion expression starts off
1412 -- with a copy of the original parent expression where all references
1413 -- to the parent type have already been replaced with references to
1414 -- the _object formal parameter of the parent type's DIC procedure.
1416 pragma Assert (Present (DIC_Expr));
1417 Expr := New_Copy_Tree (DIC_Expr);
1419 -- Perform the following substitutions:
1421 -- * Replace a reference to the _object parameter of the parent
1422 -- type's DIC procedure with a reference to the _object parameter
1423 -- of the derived types' DIC procedure.
1425 -- * Replace a reference to a discriminant of the parent type with
1426 -- a suitable value from the point of view of the derived type.
1428 -- * Replace a call to an overridden parent primitive with a call
1429 -- to the overriding derived type primitive.
1431 -- * Replace a call to an inherited parent primitive with a call to
1432 -- the internally-generated inherited derived type primitive.
1434 -- Note that primitives defined in the private part are automatically
1435 -- handled by the overriding/inheritance mechanism and do not require
1436 -- an extra replacement pass.
1438 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1440 Replace_References
1441 (Expr => Expr,
1442 Par_Typ => Par_Typ,
1443 Deriv_Typ => Deriv_Typ,
1444 Par_Obj => First_Formal (Par_Proc),
1445 Deriv_Obj => First_Formal (Deriv_Proc));
1447 -- Once the DIC assertion expression is fully processed, add a check
1448 -- to the statements of the DIC procedure.
1450 Add_DIC_Check
1451 (DIC_Prag => DIC_Prag,
1452 DIC_Expr => Expr,
1453 Stmts => Stmts);
1454 end Add_Inherited_Tagged_DIC;
1456 -----------------
1457 -- Add_Own_DIC --
1458 -----------------
1460 procedure Add_Own_DIC
1461 (DIC_Prag : Node_Id;
1462 DIC_Typ : Entity_Id;
1463 Stmts : in out List_Id)
1465 DIC_Args : constant List_Id :=
1466 Pragma_Argument_Associations (DIC_Prag);
1467 DIC_Arg : constant Node_Id := First (DIC_Args);
1468 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1469 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1470 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1471 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1473 procedure Preanalyze_Own_DIC_For_ASIS;
1474 -- Preanalyze the original DIC expression of an aspect or a source
1475 -- pragma for ASIS.
1477 ---------------------------------
1478 -- Preanalyze_Own_DIC_For_ASIS --
1479 ---------------------------------
1481 procedure Preanalyze_Own_DIC_For_ASIS is
1482 Expr : Node_Id := Empty;
1484 begin
1485 -- The DIC pragma is a source construct, preanalyze the original
1486 -- expression of the pragma.
1488 if Comes_From_Source (DIC_Prag) then
1489 Expr := DIC_Expr;
1491 -- Otherwise preanalyze the expression of the corresponding aspect
1493 elsif Present (DIC_Asp) then
1494 Expr := Expression (DIC_Asp);
1495 end if;
1497 -- The expression must be subjected to the same substitutions as
1498 -- the copy used in the generation of the runtime check.
1500 if Present (Expr) then
1501 Replace_Type_References
1502 (Expr => Expr,
1503 Typ => DIC_Typ,
1504 Obj_Id => Obj_Id);
1506 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1507 end if;
1508 end Preanalyze_Own_DIC_For_ASIS;
1510 -- Local variables
1512 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1514 Expr : Node_Id;
1516 -- Start of processing for Add_Own_DIC
1518 begin
1519 pragma Assert (Present (DIC_Expr));
1520 Expr := New_Copy_Tree (DIC_Expr);
1522 -- Perform the following substitution:
1524 -- * Replace the current instance of DIC_Typ with a reference to
1525 -- the _object formal parameter of the DIC procedure.
1527 Replace_Type_References
1528 (Expr => Expr,
1529 Typ => DIC_Typ,
1530 Obj_Id => Obj_Id);
1532 -- Preanalyze the DIC expression to detect errors and at the same
1533 -- time capture the visibility of the proper package part.
1535 Set_Parent (Expr, Typ_Decl);
1536 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1538 -- Save a copy of the expression with all replacements and analysis
1539 -- already taken place in case a derived type inherits the pragma.
1540 -- The copy will be used as the foundation of the derived type's own
1541 -- version of the DIC assertion expression.
1543 if Is_Tagged_Type (DIC_Typ) then
1544 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1545 end if;
1547 -- If the pragma comes from an aspect specification, replace the
1548 -- saved expression because all type references must be substituted
1549 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1550 -- routines.
1552 if Present (DIC_Asp) then
1553 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1554 end if;
1556 -- Preanalyze the original DIC expression for ASIS
1558 if ASIS_Mode then
1559 Preanalyze_Own_DIC_For_ASIS;
1560 end if;
1562 -- Once the DIC assertion expression is fully processed, add a check
1563 -- to the statements of the DIC procedure.
1565 Add_DIC_Check
1566 (DIC_Prag => DIC_Prag,
1567 DIC_Expr => Expr,
1568 Stmts => Stmts);
1569 end Add_Own_DIC;
1571 -- Local variables
1573 Loc : constant Source_Ptr := Sloc (Typ);
1575 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1576 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1577 -- Save the Ghost-related attributes to restore on exit
1579 DIC_Prag : Node_Id;
1580 DIC_Typ : Entity_Id;
1581 Dummy_1 : Entity_Id;
1582 Dummy_2 : Entity_Id;
1583 Proc_Body : Node_Id;
1584 Proc_Body_Id : Entity_Id;
1585 Proc_Decl : Node_Id;
1586 Proc_Id : Entity_Id;
1587 Stmts : List_Id := No_List;
1589 Build_Body : Boolean := False;
1590 -- Flag set when the type requires a DIC procedure body to be built
1592 Work_Typ : Entity_Id;
1593 -- The working type
1595 -- Start of processing for Build_DIC_Procedure_Body
1597 begin
1598 Work_Typ := Base_Type (Typ);
1600 -- Do not process class-wide types as these are Itypes, but lack a first
1601 -- subtype (see below).
1603 if Is_Class_Wide_Type (Work_Typ) then
1604 return;
1606 -- Do not process the underlying full view of a private type. There is
1607 -- no way to get back to the partial view, plus the body will be built
1608 -- by the full view or the base type.
1610 elsif Is_Underlying_Full_View (Work_Typ) then
1611 return;
1613 -- Use the first subtype when dealing with various base types
1615 elsif Is_Itype (Work_Typ) then
1616 Work_Typ := First_Subtype (Work_Typ);
1618 -- The input denotes the corresponding record type of a protected or a
1619 -- task type. Work with the concurrent type because the corresponding
1620 -- record type may not be visible to clients of the type.
1622 elsif Ekind (Work_Typ) = E_Record_Type
1623 and then Is_Concurrent_Record_Type (Work_Typ)
1624 then
1625 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1626 end if;
1628 -- The working type may be subject to pragma Ghost. Set the mode now to
1629 -- ensure that the DIC procedure is properly marked as Ghost.
1631 Set_Ghost_Mode (Work_Typ);
1633 -- The working type must be either define a DIC pragma of its own or
1634 -- inherit one from a parent type.
1636 pragma Assert (Has_DIC (Work_Typ));
1638 -- Recover the type which defines the DIC pragma. This is either the
1639 -- working type itself or a parent type when the pragma is inherited.
1641 DIC_Typ := Find_DIC_Type (Work_Typ);
1642 pragma Assert (Present (DIC_Typ));
1644 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1645 pragma Assert (Present (DIC_Prag));
1647 -- Nothing to do if pragma DIC appears without an argument or its sole
1648 -- argument is "null".
1650 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1651 goto Leave;
1652 end if;
1654 -- The working type may lack a DIC procedure declaration. This may be
1655 -- due to several reasons:
1657 -- * The working type's own DIC pragma does not contain a verifiable
1658 -- assertion expression. In this case there is no need to build a
1659 -- DIC procedure because there is nothing to check.
1661 -- * The working type derives from a parent type. In this case a DIC
1662 -- procedure should be built only when the inherited DIC pragma has
1663 -- a verifiable assertion expression.
1665 Proc_Id := DIC_Procedure (Work_Typ);
1667 -- Build a DIC procedure declaration when the working type derives from
1668 -- a parent type.
1670 if No (Proc_Id) then
1671 Build_DIC_Procedure_Declaration (Work_Typ);
1672 Proc_Id := DIC_Procedure (Work_Typ);
1673 end if;
1675 -- At this point there should be a DIC procedure declaration
1677 pragma Assert (Present (Proc_Id));
1678 Proc_Decl := Unit_Declaration_Node (Proc_Id);
1680 -- Nothing to do if the DIC procedure already has a body
1682 if Present (Corresponding_Body (Proc_Decl)) then
1683 goto Leave;
1684 end if;
1686 -- Emulate the environment of the DIC procedure by installing its scope
1687 -- and formal parameters.
1689 Push_Scope (Proc_Id);
1690 Install_Formals (Proc_Id);
1692 -- The working type defines its own DIC pragma. Replace the current
1693 -- instance of the working type with the formal of the DIC procedure.
1694 -- Note that there is no need to consider inherited DIC pragmas from
1695 -- parent types because the working type's DIC pragma "hides" all
1696 -- inherited DIC pragmas.
1698 if Has_Own_DIC (Work_Typ) then
1699 pragma Assert (DIC_Typ = Work_Typ);
1701 Add_Own_DIC
1702 (DIC_Prag => DIC_Prag,
1703 DIC_Typ => DIC_Typ,
1704 Stmts => Stmts);
1706 Build_Body := True;
1708 -- Otherwise the working type inherits a DIC pragma from a parent type.
1709 -- This processing is carried out when the type is frozen because the
1710 -- state of all parent discriminants is known at that point. Note that
1711 -- it is semantically sound to delay the creation of the DIC procedure
1712 -- body till the freeze point. If the type has a DIC pragma of its own,
1713 -- then the DIC procedure body would have already been constructed at
1714 -- the end of the visible declarations and all parent DIC pragmas are
1715 -- effectively "hidden" and irrelevant.
1717 elsif For_Freeze then
1718 pragma Assert (Has_Inherited_DIC (Work_Typ));
1719 pragma Assert (DIC_Typ /= Work_Typ);
1721 -- The working type is tagged. The verification of the assertion
1722 -- expression is subject to the same semantics as class-wide pre-
1723 -- and postconditions.
1725 if Is_Tagged_Type (Work_Typ) then
1726 Add_Inherited_Tagged_DIC
1727 (DIC_Prag => DIC_Prag,
1728 Par_Typ => DIC_Typ,
1729 Deriv_Typ => Work_Typ,
1730 Stmts => Stmts);
1732 -- Otherwise the working type is not tagged. Verify the assertion
1733 -- expression of the inherited DIC pragma by directly calling the
1734 -- DIC procedure of the parent type.
1736 else
1737 Add_Inherited_DIC
1738 (DIC_Prag => DIC_Prag,
1739 Par_Typ => DIC_Typ,
1740 Deriv_Typ => Work_Typ,
1741 Stmts => Stmts);
1742 end if;
1744 Build_Body := True;
1745 end if;
1747 End_Scope;
1749 if Build_Body then
1751 -- Produce an empty completing body in the following cases:
1752 -- * Assertions are disabled
1753 -- * The DIC Assertion_Policy is Ignore
1755 if No (Stmts) then
1756 Stmts := New_List (Make_Null_Statement (Loc));
1757 end if;
1759 -- Generate:
1760 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1761 -- begin
1762 -- <Stmts>
1763 -- end <Work_Typ>DIC;
1765 Proc_Body :=
1766 Make_Subprogram_Body (Loc,
1767 Specification =>
1768 Copy_Subprogram_Spec (Parent (Proc_Id)),
1769 Declarations => Empty_List,
1770 Handled_Statement_Sequence =>
1771 Make_Handled_Sequence_Of_Statements (Loc,
1772 Statements => Stmts));
1773 Proc_Body_Id := Defining_Entity (Proc_Body);
1775 -- Perform minor decoration in case the body is not analyzed
1777 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1778 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1779 Set_Scope (Proc_Body_Id, Current_Scope);
1780 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
1781 Set_SPARK_Pragma_Inherited
1782 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
1784 -- Link both spec and body to avoid generating duplicates
1786 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1787 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1789 -- The body should not be inserted into the tree when the context
1790 -- is ASIS or a generic unit because it is not part of the template.
1791 -- Note that the body must still be generated in order to resolve the
1792 -- DIC assertion expression.
1794 if ASIS_Mode or Inside_A_Generic then
1795 null;
1797 -- Semi-insert the body into the tree for GNATprove by setting its
1798 -- Parent field. This allows for proper upstream tree traversals.
1800 elsif GNATprove_Mode then
1801 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1803 -- Otherwise the body is part of the freezing actions of the working
1804 -- type.
1806 else
1807 Append_Freeze_Action (Work_Typ, Proc_Body);
1808 end if;
1809 end if;
1811 <<Leave>>
1812 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1813 end Build_DIC_Procedure_Body;
1815 -------------------------------------
1816 -- Build_DIC_Procedure_Declaration --
1817 -------------------------------------
1819 -- WARNING: This routine manages Ghost regions. Return statements must be
1820 -- replaced by gotos which jump to the end of the routine and restore the
1821 -- Ghost mode.
1823 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1824 Loc : constant Source_Ptr := Sloc (Typ);
1826 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1827 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1828 -- Save the Ghost-related attributes to restore on exit
1830 DIC_Prag : Node_Id;
1831 DIC_Typ : Entity_Id;
1832 Proc_Decl : Node_Id;
1833 Proc_Id : Entity_Id;
1834 Typ_Decl : Node_Id;
1836 CRec_Typ : Entity_Id;
1837 -- The corresponding record type of Full_Typ
1839 Full_Base : Entity_Id;
1840 -- The base type of Full_Typ
1842 Full_Typ : Entity_Id;
1843 -- The full view of working type
1845 Obj_Id : Entity_Id;
1846 -- The _object formal parameter of the DIC procedure
1848 Priv_Typ : Entity_Id;
1849 -- The partial view of working type
1851 Work_Typ : Entity_Id;
1852 -- The working type
1854 begin
1855 Work_Typ := Base_Type (Typ);
1857 -- Do not process class-wide types as these are Itypes, but lack a first
1858 -- subtype (see below).
1860 if Is_Class_Wide_Type (Work_Typ) then
1861 return;
1863 -- Do not process the underlying full view of a private type. There is
1864 -- no way to get back to the partial view, plus the body will be built
1865 -- by the full view or the base type.
1867 elsif Is_Underlying_Full_View (Work_Typ) then
1868 return;
1870 -- Use the first subtype when dealing with various base types
1872 elsif Is_Itype (Work_Typ) then
1873 Work_Typ := First_Subtype (Work_Typ);
1875 -- The input denotes the corresponding record type of a protected or a
1876 -- task type. Work with the concurrent type because the corresponding
1877 -- record type may not be visible to clients of the type.
1879 elsif Ekind (Work_Typ) = E_Record_Type
1880 and then Is_Concurrent_Record_Type (Work_Typ)
1881 then
1882 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1883 end if;
1885 -- The working type may be subject to pragma Ghost. Set the mode now to
1886 -- ensure that the DIC procedure is properly marked as Ghost.
1888 Set_Ghost_Mode (Work_Typ);
1890 -- The type must be either subject to a DIC pragma or inherit one from a
1891 -- parent type.
1893 pragma Assert (Has_DIC (Work_Typ));
1895 -- Recover the type which defines the DIC pragma. This is either the
1896 -- working type itself or a parent type when the pragma is inherited.
1898 DIC_Typ := Find_DIC_Type (Work_Typ);
1899 pragma Assert (Present (DIC_Typ));
1901 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1902 pragma Assert (Present (DIC_Prag));
1904 -- Nothing to do if pragma DIC appears without an argument or its sole
1905 -- argument is "null".
1907 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1908 goto Leave;
1910 -- Nothing to do if the type already has a DIC procedure
1912 elsif Present (DIC_Procedure (Work_Typ)) then
1913 goto Leave;
1914 end if;
1916 Proc_Id :=
1917 Make_Defining_Identifier (Loc,
1918 Chars =>
1919 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1921 -- Perform minor decoration in case the declaration is not analyzed
1923 Set_Ekind (Proc_Id, E_Procedure);
1924 Set_Etype (Proc_Id, Standard_Void_Type);
1925 Set_Is_DIC_Procedure (Proc_Id);
1926 Set_Scope (Proc_Id, Current_Scope);
1927 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
1928 Set_SPARK_Pragma_Inherited (Proc_Id);
1930 Set_DIC_Procedure (Work_Typ, Proc_Id);
1932 -- The DIC procedure requires debug info when the assertion expression
1933 -- is subject to Source Coverage Obligations.
1935 if Generate_SCO then
1936 Set_Needs_Debug_Info (Proc_Id);
1937 end if;
1939 -- Obtain all views of the input type
1941 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1943 -- Associate the DIC procedure and various relevant flags with all views
1945 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1946 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1947 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1948 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1950 -- The declaration of the DIC procedure must be inserted after the
1951 -- declaration of the partial view as this allows for proper external
1952 -- visibility.
1954 if Present (Priv_Typ) then
1955 Typ_Decl := Declaration_Node (Priv_Typ);
1957 -- Derived types with the full view as parent do not have a partial
1958 -- view. Insert the DIC procedure after the derived type.
1960 else
1961 Typ_Decl := Declaration_Node (Full_Typ);
1962 end if;
1964 -- The type should have a declarative node
1966 pragma Assert (Present (Typ_Decl));
1968 -- Create the formal parameter which emulates the variable-like behavior
1969 -- of the type's current instance.
1971 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1973 -- Perform minor decoration in case the declaration is not analyzed
1975 Set_Ekind (Obj_Id, E_In_Parameter);
1976 Set_Etype (Obj_Id, Work_Typ);
1977 Set_Scope (Obj_Id, Proc_Id);
1979 Set_First_Entity (Proc_Id, Obj_Id);
1981 -- Generate:
1982 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1984 Proc_Decl :=
1985 Make_Subprogram_Declaration (Loc,
1986 Specification =>
1987 Make_Procedure_Specification (Loc,
1988 Defining_Unit_Name => Proc_Id,
1989 Parameter_Specifications => New_List (
1990 Make_Parameter_Specification (Loc,
1991 Defining_Identifier => Obj_Id,
1992 Parameter_Type =>
1993 New_Occurrence_Of (Work_Typ, Loc)))));
1995 -- The declaration should not be inserted into the tree when the context
1996 -- is ASIS or a generic unit because it is not part of the template.
1998 if ASIS_Mode or Inside_A_Generic then
1999 null;
2001 -- Semi-insert the declaration into the tree for GNATprove by setting
2002 -- its Parent field. This allows for proper upstream tree traversals.
2004 elsif GNATprove_Mode then
2005 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2007 -- Otherwise insert the declaration
2009 else
2010 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2011 end if;
2013 <<Leave>>
2014 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2015 end Build_DIC_Procedure_Declaration;
2017 ------------------------------------
2018 -- Build_Invariant_Procedure_Body --
2019 ------------------------------------
2021 -- WARNING: This routine manages Ghost regions. Return statements must be
2022 -- replaced by gotos which jump to the end of the routine and restore the
2023 -- Ghost mode.
2025 procedure Build_Invariant_Procedure_Body
2026 (Typ : Entity_Id;
2027 Partial_Invariant : Boolean := False)
2029 Loc : constant Source_Ptr := Sloc (Typ);
2031 Pragmas_Seen : Elist_Id := No_Elist;
2032 -- This list contains all invariant pragmas processed so far. The list
2033 -- is used to avoid generating redundant invariant checks.
2035 Produced_Check : Boolean := False;
2036 -- This flag tracks whether the type has produced at least one invariant
2037 -- check. The flag is used as a sanity check at the end of the routine.
2039 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2040 -- intentionally unnested to avoid deep indentation of code.
2042 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2043 -- they emit checks, loops (for arrays) and case statements (for record
2044 -- variant parts) only when there are invariants to verify. This keeps
2045 -- the body of the invariant procedure free of useless code.
2047 procedure Add_Array_Component_Invariants
2048 (T : Entity_Id;
2049 Obj_Id : Entity_Id;
2050 Checks : in out List_Id);
2051 -- Generate an invariant check for each component of array type T.
2052 -- Obj_Id denotes the entity of the _object formal parameter of the
2053 -- invariant procedure. All created checks are added to list Checks.
2055 procedure Add_Inherited_Invariants
2056 (T : Entity_Id;
2057 Priv_Typ : Entity_Id;
2058 Full_Typ : Entity_Id;
2059 Obj_Id : Entity_Id;
2060 Checks : in out List_Id);
2061 -- Generate an invariant check for each inherited class-wide invariant
2062 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2063 -- the partial and full view of the parent type. Obj_Id denotes the
2064 -- entity of the _object formal parameter of the invariant procedure.
2065 -- All created checks are added to list Checks.
2067 procedure Add_Interface_Invariants
2068 (T : Entity_Id;
2069 Obj_Id : Entity_Id;
2070 Checks : in out List_Id);
2071 -- Generate an invariant check for each inherited class-wide invariant
2072 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2073 -- entity of the _object formal parameter of the invariant procedure.
2074 -- All created checks are added to list Checks.
2076 procedure Add_Invariant_Check
2077 (Prag : Node_Id;
2078 Expr : Node_Id;
2079 Checks : in out List_Id;
2080 Inherited : Boolean := False);
2081 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2082 -- verify assertion expression Expr of pragma Prag. All generated code
2083 -- is added to list Checks. Flag Inherited should be set when the pragma
2084 -- is inherited from a parent or interface type.
2086 procedure Add_Own_Invariants
2087 (T : Entity_Id;
2088 Obj_Id : Entity_Id;
2089 Checks : in out List_Id;
2090 Priv_Item : Node_Id := Empty);
2091 -- Generate an invariant check for each invariant found for type T.
2092 -- Obj_Id denotes the entity of the _object formal parameter of the
2093 -- invariant procedure. All created checks are added to list Checks.
2094 -- Priv_Item denotes the first rep item of the private type.
2096 procedure Add_Parent_Invariants
2097 (T : Entity_Id;
2098 Obj_Id : Entity_Id;
2099 Checks : in out List_Id);
2100 -- Generate an invariant check for each inherited class-wide invariant
2101 -- coming from all parent types of type T. Obj_Id denotes the entity of
2102 -- the _object formal parameter of the invariant procedure. All created
2103 -- checks are added to list Checks.
2105 procedure Add_Record_Component_Invariants
2106 (T : Entity_Id;
2107 Obj_Id : Entity_Id;
2108 Checks : in out List_Id);
2109 -- Generate an invariant check for each component of record type T.
2110 -- Obj_Id denotes the entity of the _object formal parameter of the
2111 -- invariant procedure. All created checks are added to list Checks.
2113 ------------------------------------
2114 -- Add_Array_Component_Invariants --
2115 ------------------------------------
2117 procedure Add_Array_Component_Invariants
2118 (T : Entity_Id;
2119 Obj_Id : Entity_Id;
2120 Checks : in out List_Id)
2122 Comp_Typ : constant Entity_Id := Component_Type (T);
2123 Dims : constant Pos := Number_Dimensions (T);
2125 procedure Process_Array_Component
2126 (Indices : List_Id;
2127 Comp_Checks : in out List_Id);
2128 -- Generate an invariant check for an array component identified by
2129 -- the indices in list Indices. All created checks are added to list
2130 -- Comp_Checks.
2132 procedure Process_One_Dimension
2133 (Dim : Pos;
2134 Indices : List_Id;
2135 Dim_Checks : in out List_Id);
2136 -- Generate a loop over the Nth dimension Dim of an array type. List
2137 -- Indices contains all array indices for the dimension. All created
2138 -- checks are added to list Dim_Checks.
2140 -----------------------------
2141 -- Process_Array_Component --
2142 -----------------------------
2144 procedure Process_Array_Component
2145 (Indices : List_Id;
2146 Comp_Checks : in out List_Id)
2148 Proc_Id : Entity_Id;
2150 begin
2151 if Has_Invariants (Comp_Typ) then
2153 -- In GNATprove mode, the component invariants are checked by
2154 -- other means. They should not be added to the array type
2155 -- invariant procedure, so that the procedure can be used to
2156 -- check the array type invariants if any.
2158 if GNATprove_Mode then
2159 null;
2161 else
2162 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2164 -- The component type should have an invariant procedure
2165 -- if it has invariants of its own or inherits class-wide
2166 -- invariants from parent or interface types.
2168 pragma Assert (Present (Proc_Id));
2170 -- Generate:
2171 -- <Comp_Typ>Invariant (_object (<Indices>));
2173 -- Note that the invariant procedure may have a null body if
2174 -- assertions are disabled or Assertion_Policy Ignore is in
2175 -- effect.
2177 if not Has_Null_Body (Proc_Id) then
2178 Append_New_To (Comp_Checks,
2179 Make_Procedure_Call_Statement (Loc,
2180 Name =>
2181 New_Occurrence_Of (Proc_Id, Loc),
2182 Parameter_Associations => New_List (
2183 Make_Indexed_Component (Loc,
2184 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2185 Expressions => New_Copy_List (Indices)))));
2186 end if;
2187 end if;
2189 Produced_Check := True;
2190 end if;
2191 end Process_Array_Component;
2193 ---------------------------
2194 -- Process_One_Dimension --
2195 ---------------------------
2197 procedure Process_One_Dimension
2198 (Dim : Pos;
2199 Indices : List_Id;
2200 Dim_Checks : in out List_Id)
2202 Comp_Checks : List_Id := No_List;
2203 Index : Entity_Id;
2205 begin
2206 -- Generate the invariant checks for the array component after all
2207 -- dimensions have produced their respective loops.
2209 if Dim > Dims then
2210 Process_Array_Component
2211 (Indices => Indices,
2212 Comp_Checks => Dim_Checks);
2214 -- Otherwise create a loop for the current dimension
2216 else
2217 -- Create a new loop variable for each dimension
2219 Index :=
2220 Make_Defining_Identifier (Loc,
2221 Chars => New_External_Name ('I', Dim));
2222 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2224 Process_One_Dimension
2225 (Dim => Dim + 1,
2226 Indices => Indices,
2227 Dim_Checks => Comp_Checks);
2229 -- Generate:
2230 -- for I<Dim> in _object'Range (<Dim>) loop
2231 -- <Comp_Checks>
2232 -- end loop;
2234 -- Note that the invariant procedure may have a null body if
2235 -- assertions are disabled or Assertion_Policy Ignore is in
2236 -- effect.
2238 if Present (Comp_Checks) then
2239 Append_New_To (Dim_Checks,
2240 Make_Implicit_Loop_Statement (T,
2241 Identifier => Empty,
2242 Iteration_Scheme =>
2243 Make_Iteration_Scheme (Loc,
2244 Loop_Parameter_Specification =>
2245 Make_Loop_Parameter_Specification (Loc,
2246 Defining_Identifier => Index,
2247 Discrete_Subtype_Definition =>
2248 Make_Attribute_Reference (Loc,
2249 Prefix =>
2250 New_Occurrence_Of (Obj_Id, Loc),
2251 Attribute_Name => Name_Range,
2252 Expressions => New_List (
2253 Make_Integer_Literal (Loc, Dim))))),
2254 Statements => Comp_Checks));
2255 end if;
2256 end if;
2257 end Process_One_Dimension;
2259 -- Start of processing for Add_Array_Component_Invariants
2261 begin
2262 Process_One_Dimension
2263 (Dim => 1,
2264 Indices => New_List,
2265 Dim_Checks => Checks);
2266 end Add_Array_Component_Invariants;
2268 ------------------------------
2269 -- Add_Inherited_Invariants --
2270 ------------------------------
2272 procedure Add_Inherited_Invariants
2273 (T : Entity_Id;
2274 Priv_Typ : Entity_Id;
2275 Full_Typ : Entity_Id;
2276 Obj_Id : Entity_Id;
2277 Checks : in out List_Id)
2279 Deriv_Typ : Entity_Id;
2280 Expr : Node_Id;
2281 Prag : Node_Id;
2282 Prag_Expr : Node_Id;
2283 Prag_Expr_Arg : Node_Id;
2284 Prag_Typ : Node_Id;
2285 Prag_Typ_Arg : Node_Id;
2287 Par_Proc : Entity_Id;
2288 -- The "partial" invariant procedure of Par_Typ
2290 Par_Typ : Entity_Id;
2291 -- The suitable view of the parent type used in the substitution of
2292 -- type attributes.
2294 begin
2295 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2296 return;
2297 end if;
2299 -- When the type inheriting the class-wide invariant is a concurrent
2300 -- type, use the corresponding record type because it contains all
2301 -- primitive operations of the concurrent type and allows for proper
2302 -- substitution.
2304 if Is_Concurrent_Type (T) then
2305 Deriv_Typ := Corresponding_Record_Type (T);
2306 else
2307 Deriv_Typ := T;
2308 end if;
2310 pragma Assert (Present (Deriv_Typ));
2312 -- Determine which rep item chain to use. Precedence is given to that
2313 -- of the parent type's partial view since it usually carries all the
2314 -- class-wide invariants.
2316 if Present (Priv_Typ) then
2317 Prag := First_Rep_Item (Priv_Typ);
2318 else
2319 Prag := First_Rep_Item (Full_Typ);
2320 end if;
2322 while Present (Prag) loop
2323 if Nkind (Prag) = N_Pragma
2324 and then Pragma_Name (Prag) = Name_Invariant
2325 then
2326 -- Nothing to do if the pragma was already processed
2328 if Contains (Pragmas_Seen, Prag) then
2329 return;
2331 -- Nothing to do when the caller requests the processing of all
2332 -- inherited class-wide invariants, but the pragma does not
2333 -- fall in this category.
2335 elsif not Class_Present (Prag) then
2336 return;
2337 end if;
2339 -- Extract the arguments of the invariant pragma
2341 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2342 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2343 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2344 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2346 -- The pragma applies to the partial view of the parent type
2348 if Present (Priv_Typ)
2349 and then Entity (Prag_Typ) = Priv_Typ
2350 then
2351 Par_Typ := Priv_Typ;
2353 -- The pragma applies to the full view of the parent type
2355 elsif Present (Full_Typ)
2356 and then Entity (Prag_Typ) = Full_Typ
2357 then
2358 Par_Typ := Full_Typ;
2360 -- Otherwise the pragma does not belong to the parent type and
2361 -- should not be considered.
2363 else
2364 return;
2365 end if;
2367 -- Perform the following substitutions:
2369 -- * Replace a reference to the _object parameter of the
2370 -- parent type's partial invariant procedure with a
2371 -- reference to the _object parameter of the derived
2372 -- type's full invariant procedure.
2374 -- * Replace a reference to a discriminant of the parent type
2375 -- with a suitable value from the point of view of the
2376 -- derived type.
2378 -- * Replace a call to an overridden parent primitive with a
2379 -- call to the overriding derived type primitive.
2381 -- * Replace a call to an inherited parent primitive with a
2382 -- call to the internally-generated inherited derived type
2383 -- primitive.
2385 Expr := New_Copy_Tree (Prag_Expr);
2387 -- The parent type must have a "partial" invariant procedure
2388 -- because class-wide invariants are captured exclusively by
2389 -- it.
2391 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2392 pragma Assert (Present (Par_Proc));
2394 Replace_References
2395 (Expr => Expr,
2396 Par_Typ => Par_Typ,
2397 Deriv_Typ => Deriv_Typ,
2398 Par_Obj => First_Formal (Par_Proc),
2399 Deriv_Obj => Obj_Id);
2401 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2402 end if;
2404 Next_Rep_Item (Prag);
2405 end loop;
2406 end Add_Inherited_Invariants;
2408 ------------------------------
2409 -- Add_Interface_Invariants --
2410 ------------------------------
2412 procedure Add_Interface_Invariants
2413 (T : Entity_Id;
2414 Obj_Id : Entity_Id;
2415 Checks : in out List_Id)
2417 Iface_Elmt : Elmt_Id;
2418 Ifaces : Elist_Id;
2420 begin
2421 -- Generate an invariant check for each class-wide invariant coming
2422 -- from all interfaces implemented by type T.
2424 if Is_Tagged_Type (T) then
2425 Collect_Interfaces (T, Ifaces);
2427 -- Process the class-wide invariants of all implemented interfaces
2429 Iface_Elmt := First_Elmt (Ifaces);
2430 while Present (Iface_Elmt) loop
2432 -- The Full_Typ parameter is intentionally left Empty because
2433 -- interfaces are treated as the partial view of a private type
2434 -- in order to achieve uniformity with the general case.
2436 Add_Inherited_Invariants
2437 (T => T,
2438 Priv_Typ => Node (Iface_Elmt),
2439 Full_Typ => Empty,
2440 Obj_Id => Obj_Id,
2441 Checks => Checks);
2443 Next_Elmt (Iface_Elmt);
2444 end loop;
2445 end if;
2446 end Add_Interface_Invariants;
2448 -------------------------
2449 -- Add_Invariant_Check --
2450 -------------------------
2452 procedure Add_Invariant_Check
2453 (Prag : Node_Id;
2454 Expr : Node_Id;
2455 Checks : in out List_Id;
2456 Inherited : Boolean := False)
2458 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2459 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2460 Ploc : constant Source_Ptr := Sloc (Prag);
2461 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2463 Assoc : List_Id;
2464 Str : String_Id;
2466 begin
2467 -- The invariant is ignored, nothing left to do
2469 if Is_Ignored (Prag) then
2470 null;
2472 -- Otherwise the invariant is checked. Build a pragma Check to verify
2473 -- the expression at run time.
2475 else
2476 Assoc := New_List (
2477 Make_Pragma_Argument_Association (Ploc,
2478 Expression => Make_Identifier (Ploc, Nam)),
2479 Make_Pragma_Argument_Association (Ploc,
2480 Expression => Expr));
2482 -- Handle the String argument (if any)
2484 if Present (Str_Arg) then
2485 Str := Strval (Get_Pragma_Arg (Str_Arg));
2487 -- When inheriting an invariant, modify the message from
2488 -- "failed invariant" to "failed inherited invariant".
2490 if Inherited then
2491 String_To_Name_Buffer (Str);
2493 if Name_Buffer (1 .. 16) = "failed invariant" then
2494 Insert_Str_In_Name_Buffer ("inherited ", 8);
2495 Str := String_From_Name_Buffer;
2496 end if;
2497 end if;
2499 Append_To (Assoc,
2500 Make_Pragma_Argument_Association (Ploc,
2501 Expression => Make_String_Literal (Ploc, Str)));
2502 end if;
2504 -- Generate:
2505 -- pragma Check (<Nam>, <Expr>, <Str>);
2507 Append_New_To (Checks,
2508 Make_Pragma (Ploc,
2509 Chars => Name_Check,
2510 Pragma_Argument_Associations => Assoc));
2511 end if;
2513 -- Output an info message when inheriting an invariant and the
2514 -- listing option is enabled.
2516 if Inherited and Opt.List_Inherited_Aspects then
2517 Error_Msg_Sloc := Sloc (Prag);
2518 Error_Msg_N
2519 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2520 end if;
2522 -- Add the pragma to the list of processed pragmas
2524 Append_New_Elmt (Prag, Pragmas_Seen);
2525 Produced_Check := True;
2526 end Add_Invariant_Check;
2528 ---------------------------
2529 -- Add_Parent_Invariants --
2530 ---------------------------
2532 procedure Add_Parent_Invariants
2533 (T : Entity_Id;
2534 Obj_Id : Entity_Id;
2535 Checks : in out List_Id)
2537 Dummy_1 : Entity_Id;
2538 Dummy_2 : Entity_Id;
2540 Curr_Typ : Entity_Id;
2541 -- The entity of the current type being examined
2543 Full_Typ : Entity_Id;
2544 -- The full view of Par_Typ
2546 Par_Typ : Entity_Id;
2547 -- The entity of the parent type
2549 Priv_Typ : Entity_Id;
2550 -- The partial view of Par_Typ
2552 begin
2553 -- Do not process array types because they cannot have true parent
2554 -- types. This also prevents the generation of a duplicate invariant
2555 -- check when the input type is an array base type because its Etype
2556 -- denotes the first subtype, both of which share the same component
2557 -- type.
2559 if Is_Array_Type (T) then
2560 return;
2561 end if;
2563 -- Climb the parent type chain
2565 Curr_Typ := T;
2566 loop
2567 -- Do not consider subtypes as they inherit the invariants
2568 -- from their base types.
2570 Par_Typ := Base_Type (Etype (Curr_Typ));
2572 -- Stop the climb once the root of the parent chain is
2573 -- reached.
2575 exit when Curr_Typ = Par_Typ;
2577 -- Process the class-wide invariants of the parent type
2579 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2581 -- Process the elements of an array type
2583 if Is_Array_Type (Full_Typ) then
2584 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2586 -- Process the components of a record type
2588 elsif Ekind (Full_Typ) = E_Record_Type then
2589 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2590 end if;
2592 Add_Inherited_Invariants
2593 (T => T,
2594 Priv_Typ => Priv_Typ,
2595 Full_Typ => Full_Typ,
2596 Obj_Id => Obj_Id,
2597 Checks => Checks);
2599 Curr_Typ := Par_Typ;
2600 end loop;
2601 end Add_Parent_Invariants;
2603 ------------------------
2604 -- Add_Own_Invariants --
2605 ------------------------
2607 procedure Add_Own_Invariants
2608 (T : Entity_Id;
2609 Obj_Id : Entity_Id;
2610 Checks : in out List_Id;
2611 Priv_Item : Node_Id := Empty)
2613 ASIS_Expr : Node_Id;
2614 Expr : Node_Id;
2615 Prag : Node_Id;
2616 Prag_Asp : Node_Id;
2617 Prag_Expr : Node_Id;
2618 Prag_Expr_Arg : Node_Id;
2619 Prag_Typ : Node_Id;
2620 Prag_Typ_Arg : Node_Id;
2622 begin
2623 if not Present (T) then
2624 return;
2625 end if;
2627 Prag := First_Rep_Item (T);
2628 while Present (Prag) loop
2629 if Nkind (Prag) = N_Pragma
2630 and then Pragma_Name (Prag) = Name_Invariant
2631 then
2632 -- Stop the traversal of the rep item chain once a specific
2633 -- item is encountered.
2635 if Present (Priv_Item) and then Prag = Priv_Item then
2636 exit;
2637 end if;
2639 -- Nothing to do if the pragma was already processed
2641 if Contains (Pragmas_Seen, Prag) then
2642 return;
2643 end if;
2645 -- Extract the arguments of the invariant pragma
2647 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2648 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2649 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2650 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2651 Prag_Asp := Corresponding_Aspect (Prag);
2653 -- Verify the pragma belongs to T, otherwise the pragma applies
2654 -- to a parent type in which case it will be processed later by
2655 -- Add_Parent_Invariants or Add_Interface_Invariants.
2657 if Entity (Prag_Typ) /= T then
2658 return;
2659 end if;
2661 Expr := New_Copy_Tree (Prag_Expr);
2663 -- Substitute all references to type T with references to the
2664 -- _object formal parameter.
2666 Replace_Type_References (Expr, T, Obj_Id);
2668 -- Preanalyze the invariant expression to detect errors and at
2669 -- the same time capture the visibility of the proper package
2670 -- part.
2672 Set_Parent (Expr, Parent (Prag_Expr));
2673 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2675 -- Save a copy of the expression when T is tagged to detect
2676 -- errors and capture the visibility of the proper package part
2677 -- for the generation of inherited type invariants.
2679 if Is_Tagged_Type (T) then
2680 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2681 end if;
2683 -- If the pragma comes from an aspect specification, replace
2684 -- the saved expression because all type references must be
2685 -- substituted for the call to Preanalyze_Spec_Expression in
2686 -- Check_Aspect_At_xxx routines.
2688 if Present (Prag_Asp) then
2689 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2690 end if;
2692 -- Analyze the original invariant expression for ASIS
2694 if ASIS_Mode then
2695 ASIS_Expr := Empty;
2697 if Comes_From_Source (Prag) then
2698 ASIS_Expr := Prag_Expr;
2699 elsif Present (Prag_Asp) then
2700 ASIS_Expr := Expression (Prag_Asp);
2701 end if;
2703 if Present (ASIS_Expr) then
2704 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2705 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2706 end if;
2707 end if;
2709 Add_Invariant_Check (Prag, Expr, Checks);
2710 end if;
2712 Next_Rep_Item (Prag);
2713 end loop;
2714 end Add_Own_Invariants;
2716 -------------------------------------
2717 -- Add_Record_Component_Invariants --
2718 -------------------------------------
2720 procedure Add_Record_Component_Invariants
2721 (T : Entity_Id;
2722 Obj_Id : Entity_Id;
2723 Checks : in out List_Id)
2725 procedure Process_Component_List
2726 (Comp_List : Node_Id;
2727 CL_Checks : in out List_Id);
2728 -- Generate invariant checks for all record components found in
2729 -- component list Comp_List, including variant parts. All created
2730 -- checks are added to list CL_Checks.
2732 procedure Process_Record_Component
2733 (Comp_Id : Entity_Id;
2734 Comp_Checks : in out List_Id);
2735 -- Generate an invariant check for a record component identified by
2736 -- Comp_Id. All created checks are added to list Comp_Checks.
2738 ----------------------------
2739 -- Process_Component_List --
2740 ----------------------------
2742 procedure Process_Component_List
2743 (Comp_List : Node_Id;
2744 CL_Checks : in out List_Id)
2746 Comp : Node_Id;
2747 Var : Node_Id;
2748 Var_Alts : List_Id := No_List;
2749 Var_Checks : List_Id := No_List;
2750 Var_Stmts : List_Id;
2752 Produced_Variant_Check : Boolean := False;
2753 -- This flag tracks whether the component has produced at least
2754 -- one invariant check.
2756 begin
2757 -- Traverse the component items
2759 Comp := First (Component_Items (Comp_List));
2760 while Present (Comp) loop
2761 if Nkind (Comp) = N_Component_Declaration then
2763 -- Generate the component invariant check
2765 Process_Record_Component
2766 (Comp_Id => Defining_Entity (Comp),
2767 Comp_Checks => CL_Checks);
2768 end if;
2770 Next (Comp);
2771 end loop;
2773 -- Traverse the variant part
2775 if Present (Variant_Part (Comp_List)) then
2776 Var := First (Variants (Variant_Part (Comp_List)));
2777 while Present (Var) loop
2778 Var_Checks := No_List;
2780 -- Generate invariant checks for all components and variant
2781 -- parts that qualify.
2783 Process_Component_List
2784 (Comp_List => Component_List (Var),
2785 CL_Checks => Var_Checks);
2787 -- The components of the current variant produced at least
2788 -- one invariant check.
2790 if Present (Var_Checks) then
2791 Var_Stmts := Var_Checks;
2792 Produced_Variant_Check := True;
2794 -- Otherwise there are either no components with invariants,
2795 -- assertions are disabled, or Assertion_Policy Ignore is in
2796 -- effect.
2798 else
2799 Var_Stmts := New_List (Make_Null_Statement (Loc));
2800 end if;
2802 Append_New_To (Var_Alts,
2803 Make_Case_Statement_Alternative (Loc,
2804 Discrete_Choices =>
2805 New_Copy_List (Discrete_Choices (Var)),
2806 Statements => Var_Stmts));
2808 Next (Var);
2809 end loop;
2811 -- Create a case statement which verifies the invariant checks
2812 -- of a particular component list depending on the discriminant
2813 -- values only when there is at least one real invariant check.
2815 if Produced_Variant_Check then
2816 Append_New_To (CL_Checks,
2817 Make_Case_Statement (Loc,
2818 Expression =>
2819 Make_Selected_Component (Loc,
2820 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2821 Selector_Name =>
2822 New_Occurrence_Of
2823 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2824 Alternatives => Var_Alts));
2825 end if;
2826 end if;
2827 end Process_Component_List;
2829 ------------------------------
2830 -- Process_Record_Component --
2831 ------------------------------
2833 procedure Process_Record_Component
2834 (Comp_Id : Entity_Id;
2835 Comp_Checks : in out List_Id)
2837 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2838 Proc_Id : Entity_Id;
2840 Produced_Component_Check : Boolean := False;
2841 -- This flag tracks whether the component has produced at least
2842 -- one invariant check.
2844 begin
2845 -- Nothing to do for internal component _parent. Note that it is
2846 -- not desirable to check whether the component comes from source
2847 -- because protected type components are relocated to an internal
2848 -- corresponding record, but still need processing.
2850 if Chars (Comp_Id) = Name_uParent then
2851 return;
2852 end if;
2854 -- Verify the invariant of the component. Note that an access
2855 -- type may have an invariant when it acts as the full view of a
2856 -- private type and the invariant appears on the partial view. In
2857 -- this case verify the access value itself.
2859 if Has_Invariants (Comp_Typ) then
2861 -- In GNATprove mode, the component invariants are checked by
2862 -- other means. They should not be added to the record type
2863 -- invariant procedure, so that the procedure can be used to
2864 -- check the record type invariants if any.
2866 if GNATprove_Mode then
2867 null;
2869 else
2870 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2872 -- The component type should have an invariant procedure
2873 -- if it has invariants of its own or inherits class-wide
2874 -- invariants from parent or interface types.
2876 pragma Assert (Present (Proc_Id));
2878 -- Generate:
2879 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2881 -- Note that the invariant procedure may have a null body if
2882 -- assertions are disabled or Assertion_Policy Ignore is in
2883 -- effect.
2885 if not Has_Null_Body (Proc_Id) then
2886 Append_New_To (Comp_Checks,
2887 Make_Procedure_Call_Statement (Loc,
2888 Name =>
2889 New_Occurrence_Of (Proc_Id, Loc),
2890 Parameter_Associations => New_List (
2891 Make_Selected_Component (Loc,
2892 Prefix =>
2893 Unchecked_Convert_To
2894 (T, New_Occurrence_Of (Obj_Id, Loc)),
2895 Selector_Name =>
2896 New_Occurrence_Of (Comp_Id, Loc)))));
2897 end if;
2898 end if;
2900 Produced_Check := True;
2901 Produced_Component_Check := True;
2902 end if;
2904 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2905 Error_Msg_NE
2906 ("invariants cannot be checked on components of "
2907 & "unchecked_union type &?", Comp_Id, T);
2908 end if;
2909 end Process_Record_Component;
2911 -- Local variables
2913 Comps : Node_Id;
2914 Def : Node_Id;
2916 -- Start of processing for Add_Record_Component_Invariants
2918 begin
2919 -- An untagged derived type inherits the components of its parent
2920 -- type. In order to avoid creating redundant invariant checks, do
2921 -- not process the components now. Instead wait until the ultimate
2922 -- parent of the untagged derivation chain is reached.
2924 if not Is_Untagged_Derivation (T) then
2925 Def := Type_Definition (Parent (T));
2927 if Nkind (Def) = N_Derived_Type_Definition then
2928 Def := Record_Extension_Part (Def);
2929 end if;
2931 pragma Assert (Nkind (Def) = N_Record_Definition);
2932 Comps := Component_List (Def);
2934 if Present (Comps) then
2935 Process_Component_List
2936 (Comp_List => Comps,
2937 CL_Checks => Checks);
2938 end if;
2939 end if;
2940 end Add_Record_Component_Invariants;
2942 -- Local variables
2944 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2945 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2946 -- Save the Ghost-related attributes to restore on exit
2948 Dummy : Entity_Id;
2949 Priv_Item : Node_Id;
2950 Proc_Body : Node_Id;
2951 Proc_Body_Id : Entity_Id;
2952 Proc_Decl : Node_Id;
2953 Proc_Id : Entity_Id;
2954 Stmts : List_Id := No_List;
2956 CRec_Typ : Entity_Id := Empty;
2957 -- The corresponding record type of Full_Typ
2959 Full_Proc : Entity_Id := Empty;
2960 -- The entity of the "full" invariant procedure
2962 Full_Typ : Entity_Id := Empty;
2963 -- The full view of the working type
2965 Obj_Id : Entity_Id := Empty;
2966 -- The _object formal parameter of the invariant procedure
2968 Part_Proc : Entity_Id := Empty;
2969 -- The entity of the "partial" invariant procedure
2971 Priv_Typ : Entity_Id := Empty;
2972 -- The partial view of the working type
2974 Work_Typ : Entity_Id := Empty;
2975 -- The working type
2977 -- Start of processing for Build_Invariant_Procedure_Body
2979 begin
2980 Work_Typ := Typ;
2982 -- The input type denotes the implementation base type of a constrained
2983 -- array type. Work with the first subtype as all invariant pragmas are
2984 -- on its rep item chain.
2986 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2987 Work_Typ := First_Subtype (Work_Typ);
2989 -- The input type denotes the corresponding record type of a protected
2990 -- or task type. Work with the concurrent type because the corresponding
2991 -- record type may not be visible to clients of the type.
2993 elsif Ekind (Work_Typ) = E_Record_Type
2994 and then Is_Concurrent_Record_Type (Work_Typ)
2995 then
2996 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2997 end if;
2999 -- The working type may be subject to pragma Ghost. Set the mode now to
3000 -- ensure that the invariant procedure is properly marked as Ghost.
3002 Set_Ghost_Mode (Work_Typ);
3004 -- The type must either have invariants of its own, inherit class-wide
3005 -- invariants from parent types or interfaces, or be an array or record
3006 -- type whose components have invariants.
3008 pragma Assert (Has_Invariants (Work_Typ));
3010 -- Interfaces are treated as the partial view of a private type in order
3011 -- to achieve uniformity with the general case.
3013 if Is_Interface (Work_Typ) then
3014 Priv_Typ := Work_Typ;
3016 -- Otherwise obtain both views of the type
3018 else
3019 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3020 end if;
3022 -- The caller requests a body for the partial invariant procedure
3024 if Partial_Invariant then
3025 Full_Proc := Invariant_Procedure (Work_Typ);
3026 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3028 -- The "full" invariant procedure body was already created
3030 if Present (Full_Proc)
3031 and then Present
3032 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3033 then
3034 -- This scenario happens only when the type is an untagged
3035 -- derivation from a private parent and the underlying full
3036 -- view was processed before the partial view.
3038 pragma Assert
3039 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3041 -- Nothing to do because the processing of the underlying full
3042 -- view already checked the invariants of the partial view.
3044 goto Leave;
3045 end if;
3047 -- Create a declaration for the "partial" invariant procedure if it
3048 -- is not available.
3050 if No (Proc_Id) then
3051 Build_Invariant_Procedure_Declaration
3052 (Typ => Work_Typ,
3053 Partial_Invariant => True);
3055 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3056 end if;
3058 -- The caller requests a body for the "full" invariant procedure
3060 else
3061 Proc_Id := Invariant_Procedure (Work_Typ);
3062 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3064 -- Create a declaration for the "full" invariant procedure if it is
3065 -- not available.
3067 if No (Proc_Id) then
3068 Build_Invariant_Procedure_Declaration (Work_Typ);
3069 Proc_Id := Invariant_Procedure (Work_Typ);
3070 end if;
3071 end if;
3073 -- At this point there should be an invariant procedure declaration
3075 pragma Assert (Present (Proc_Id));
3076 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3078 -- Nothing to do if the invariant procedure already has a body
3080 if Present (Corresponding_Body (Proc_Decl)) then
3081 goto Leave;
3082 end if;
3084 -- Emulate the environment of the invariant procedure by installing its
3085 -- scope and formal parameters. Note that this is not needed, but having
3086 -- the scope installed helps with the detection of invariant-related
3087 -- errors.
3089 Push_Scope (Proc_Id);
3090 Install_Formals (Proc_Id);
3092 Obj_Id := First_Formal (Proc_Id);
3093 pragma Assert (Present (Obj_Id));
3095 -- The "partial" invariant procedure verifies the invariants of the
3096 -- partial view only.
3098 if Partial_Invariant then
3099 pragma Assert (Present (Priv_Typ));
3101 Add_Own_Invariants
3102 (T => Priv_Typ,
3103 Obj_Id => Obj_Id,
3104 Checks => Stmts);
3106 -- Otherwise the "full" invariant procedure verifies the invariants of
3107 -- the full view, all array or record components, as well as class-wide
3108 -- invariants inherited from parent types or interfaces. In addition, it
3109 -- indirectly verifies the invariants of the partial view by calling the
3110 -- "partial" invariant procedure.
3112 else
3113 pragma Assert (Present (Full_Typ));
3115 -- Check the invariants of the partial view by calling the "partial"
3116 -- invariant procedure. Generate:
3118 -- <Work_Typ>Partial_Invariant (_object);
3120 if Present (Part_Proc) then
3121 Append_New_To (Stmts,
3122 Make_Procedure_Call_Statement (Loc,
3123 Name => New_Occurrence_Of (Part_Proc, Loc),
3124 Parameter_Associations => New_List (
3125 New_Occurrence_Of (Obj_Id, Loc))));
3127 Produced_Check := True;
3128 end if;
3130 Priv_Item := Empty;
3132 -- Derived subtypes do not have a partial view
3134 if Present (Priv_Typ) then
3136 -- The processing of the "full" invariant procedure intentionally
3137 -- skips the partial view because a) this may result in changes of
3138 -- visibility and b) lead to duplicate checks. However, when the
3139 -- full view is the underlying full view of an untagged derived
3140 -- type whose parent type is private, partial invariants appear on
3141 -- the rep item chain of the partial view only.
3143 -- package Pack_1 is
3144 -- type Root ... is private;
3145 -- private
3146 -- <full view of Root>
3147 -- end Pack_1;
3149 -- with Pack_1;
3150 -- package Pack_2 is
3151 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3152 -- <underlying full view of Child>
3153 -- end Pack_2;
3155 -- As a result, the processing of the full view must also consider
3156 -- all invariants of the partial view.
3158 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3159 null;
3161 -- Otherwise the invariants of the partial view are ignored
3163 else
3164 -- Note that the rep item chain is shared between the partial
3165 -- and full views of a type. To avoid processing the invariants
3166 -- of the partial view, signal the logic to stop when the first
3167 -- rep item of the partial view has been reached.
3169 Priv_Item := First_Rep_Item (Priv_Typ);
3171 -- Ignore the invariants of the partial view by eliminating the
3172 -- view.
3174 Priv_Typ := Empty;
3175 end if;
3176 end if;
3178 -- Process the invariants of the full view and in certain cases those
3179 -- of the partial view. This also handles any invariants on array or
3180 -- record components.
3182 Add_Own_Invariants
3183 (T => Priv_Typ,
3184 Obj_Id => Obj_Id,
3185 Checks => Stmts,
3186 Priv_Item => Priv_Item);
3188 Add_Own_Invariants
3189 (T => Full_Typ,
3190 Obj_Id => Obj_Id,
3191 Checks => Stmts,
3192 Priv_Item => Priv_Item);
3194 -- Process the elements of an array type
3196 if Is_Array_Type (Full_Typ) then
3197 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3199 -- Process the components of a record type
3201 elsif Ekind (Full_Typ) = E_Record_Type then
3202 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3204 -- Process the components of a corresponding record
3206 elsif Present (CRec_Typ) then
3207 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3208 end if;
3210 -- Process the inherited class-wide invariants of all parent types.
3211 -- This also handles any invariants on record components.
3213 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3215 -- Process the inherited class-wide invariants of all implemented
3216 -- interface types.
3218 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3219 end if;
3221 End_Scope;
3223 -- At this point there should be at least one invariant check. If this
3224 -- is not the case, then the invariant-related flags were not properly
3225 -- set, or there is a missing invariant procedure on one of the array
3226 -- or record components.
3228 pragma Assert (Produced_Check);
3230 -- Account for the case where assertions are disabled or all invariant
3231 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3232 -- empty body.
3234 if No (Stmts) then
3235 Stmts := New_List (Make_Null_Statement (Loc));
3236 end if;
3238 -- Generate:
3239 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3240 -- begin
3241 -- <Stmts>
3242 -- end <Work_Typ>[Partial_]Invariant;
3244 Proc_Body :=
3245 Make_Subprogram_Body (Loc,
3246 Specification =>
3247 Copy_Subprogram_Spec (Parent (Proc_Id)),
3248 Declarations => Empty_List,
3249 Handled_Statement_Sequence =>
3250 Make_Handled_Sequence_Of_Statements (Loc,
3251 Statements => Stmts));
3252 Proc_Body_Id := Defining_Entity (Proc_Body);
3254 -- Perform minor decoration in case the body is not analyzed
3256 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3257 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3258 Set_Scope (Proc_Body_Id, Current_Scope);
3260 -- Link both spec and body to avoid generating duplicates
3262 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3263 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3265 -- The body should not be inserted into the tree when the context is
3266 -- ASIS or a generic unit because it is not part of the template. Note
3267 -- that the body must still be generated in order to resolve the
3268 -- invariants.
3270 if ASIS_Mode or Inside_A_Generic then
3271 null;
3273 -- Semi-insert the body into the tree for GNATprove by setting its
3274 -- Parent field. This allows for proper upstream tree traversals.
3276 elsif GNATprove_Mode then
3277 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3279 -- Otherwise the body is part of the freezing actions of the type
3281 else
3282 Append_Freeze_Action (Work_Typ, Proc_Body);
3283 end if;
3285 <<Leave>>
3286 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3287 end Build_Invariant_Procedure_Body;
3289 -------------------------------------------
3290 -- Build_Invariant_Procedure_Declaration --
3291 -------------------------------------------
3293 -- WARNING: This routine manages Ghost regions. Return statements must be
3294 -- replaced by gotos which jump to the end of the routine and restore the
3295 -- Ghost mode.
3297 procedure Build_Invariant_Procedure_Declaration
3298 (Typ : Entity_Id;
3299 Partial_Invariant : Boolean := False)
3301 Loc : constant Source_Ptr := Sloc (Typ);
3303 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3304 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3305 -- Save the Ghost-related attributes to restore on exit
3307 Proc_Decl : Node_Id;
3308 Proc_Id : Entity_Id;
3309 Proc_Nam : Name_Id;
3310 Typ_Decl : Node_Id;
3312 CRec_Typ : Entity_Id;
3313 -- The corresponding record type of Full_Typ
3315 Full_Base : Entity_Id;
3316 -- The base type of Full_Typ
3318 Full_Typ : Entity_Id;
3319 -- The full view of working type
3321 Obj_Id : Entity_Id;
3322 -- The _object formal parameter of the invariant procedure
3324 Obj_Typ : Entity_Id;
3325 -- The type of the _object formal parameter
3327 Priv_Typ : Entity_Id;
3328 -- The partial view of working type
3330 Work_Typ : Entity_Id;
3331 -- The working type
3333 begin
3334 Work_Typ := Typ;
3336 -- The input type denotes the implementation base type of a constrained
3337 -- array type. Work with the first subtype as all invariant pragmas are
3338 -- on its rep item chain.
3340 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3341 Work_Typ := First_Subtype (Work_Typ);
3343 -- The input denotes the corresponding record type of a protected or a
3344 -- task type. Work with the concurrent type because the corresponding
3345 -- record type may not be visible to clients of the type.
3347 elsif Ekind (Work_Typ) = E_Record_Type
3348 and then Is_Concurrent_Record_Type (Work_Typ)
3349 then
3350 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3351 end if;
3353 -- The working type may be subject to pragma Ghost. Set the mode now to
3354 -- ensure that the invariant procedure is properly marked as Ghost.
3356 Set_Ghost_Mode (Work_Typ);
3358 -- The type must either have invariants of its own, inherit class-wide
3359 -- invariants from parent or interface types, or be an array or record
3360 -- type whose components have invariants.
3362 pragma Assert (Has_Invariants (Work_Typ));
3364 -- Nothing to do if the type already has a "partial" invariant procedure
3366 if Partial_Invariant then
3367 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3368 goto Leave;
3369 end if;
3371 -- Nothing to do if the type already has a "full" invariant procedure
3373 elsif Present (Invariant_Procedure (Work_Typ)) then
3374 goto Leave;
3375 end if;
3377 -- The caller requests the declaration of the "partial" invariant
3378 -- procedure.
3380 if Partial_Invariant then
3381 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3383 -- Otherwise the caller requests the declaration of the "full" invariant
3384 -- procedure.
3386 else
3387 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3388 end if;
3390 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3392 -- Perform minor decoration in case the declaration is not analyzed
3394 Set_Ekind (Proc_Id, E_Procedure);
3395 Set_Etype (Proc_Id, Standard_Void_Type);
3396 Set_Scope (Proc_Id, Current_Scope);
3398 if Partial_Invariant then
3399 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3400 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3401 else
3402 Set_Is_Invariant_Procedure (Proc_Id);
3403 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3404 end if;
3406 -- The invariant procedure requires debug info when the invariants are
3407 -- subject to Source Coverage Obligations.
3409 if Generate_SCO then
3410 Set_Needs_Debug_Info (Proc_Id);
3411 end if;
3413 -- Obtain all views of the input type
3415 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3417 -- Associate the invariant procedure with all views
3419 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3420 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3421 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3422 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3424 -- The declaration of the invariant procedure is inserted after the
3425 -- declaration of the partial view as this allows for proper external
3426 -- visibility.
3428 if Present (Priv_Typ) then
3429 Typ_Decl := Declaration_Node (Priv_Typ);
3431 -- Anonymous arrays in object declarations have no explicit declaration
3432 -- so use the related object declaration as the insertion point.
3434 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3435 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3437 -- Derived types with the full view as parent do not have a partial
3438 -- view. Insert the invariant procedure after the derived type.
3440 else
3441 Typ_Decl := Declaration_Node (Full_Typ);
3442 end if;
3444 -- The type should have a declarative node
3446 pragma Assert (Present (Typ_Decl));
3448 -- Create the formal parameter which emulates the variable-like behavior
3449 -- of the current type instance.
3451 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3453 -- When generating an invariant procedure declaration for an abstract
3454 -- type (including interfaces), use the class-wide type as the _object
3455 -- type. This has several desirable effects:
3457 -- * The invariant procedure does not become a primitive of the type.
3458 -- This eliminates the need to either special case the treatment of
3459 -- invariant procedures, or to make it a predefined primitive and
3460 -- force every derived type to potentially provide an empty body.
3462 -- * The invariant procedure does not need to be declared as abstract.
3463 -- This allows for a proper body, which in turn avoids redundant
3464 -- processing of the same invariants for types with multiple views.
3466 -- * The class-wide type allows for calls to abstract primitives
3467 -- within a nonabstract subprogram. The calls are treated as
3468 -- dispatching and require additional processing when they are
3469 -- remapped to call primitives of derived types. See routine
3470 -- Replace_References for details.
3472 if Is_Abstract_Type (Work_Typ) then
3473 Obj_Typ := Class_Wide_Type (Work_Typ);
3474 else
3475 Obj_Typ := Work_Typ;
3476 end if;
3478 -- Perform minor decoration in case the declaration is not analyzed
3480 Set_Ekind (Obj_Id, E_In_Parameter);
3481 Set_Etype (Obj_Id, Obj_Typ);
3482 Set_Scope (Obj_Id, Proc_Id);
3484 Set_First_Entity (Proc_Id, Obj_Id);
3485 Set_Last_Entity (Proc_Id, Obj_Id);
3487 -- Generate:
3488 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3490 Proc_Decl :=
3491 Make_Subprogram_Declaration (Loc,
3492 Specification =>
3493 Make_Procedure_Specification (Loc,
3494 Defining_Unit_Name => Proc_Id,
3495 Parameter_Specifications => New_List (
3496 Make_Parameter_Specification (Loc,
3497 Defining_Identifier => Obj_Id,
3498 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3500 -- The declaration should not be inserted into the tree when the context
3501 -- is ASIS or a generic unit because it is not part of the template.
3503 if ASIS_Mode or Inside_A_Generic then
3504 null;
3506 -- Semi-insert the declaration into the tree for GNATprove by setting
3507 -- its Parent field. This allows for proper upstream tree traversals.
3509 elsif GNATprove_Mode then
3510 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3512 -- Otherwise insert the declaration
3514 else
3515 pragma Assert (Present (Typ_Decl));
3516 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3517 end if;
3519 <<Leave>>
3520 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3521 end Build_Invariant_Procedure_Declaration;
3523 --------------------------
3524 -- Build_Procedure_Form --
3525 --------------------------
3527 procedure Build_Procedure_Form (N : Node_Id) is
3528 Loc : constant Source_Ptr := Sloc (N);
3529 Subp : constant Entity_Id := Defining_Entity (N);
3531 Func_Formal : Entity_Id;
3532 Proc_Formals : List_Id;
3533 Proc_Decl : Node_Id;
3535 begin
3536 -- No action needed if this transformation was already done, or in case
3537 -- of subprogram renaming declarations.
3539 if Nkind (Specification (N)) = N_Procedure_Specification
3540 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3541 then
3542 return;
3543 end if;
3545 -- Ditto when dealing with an expression function, where both the
3546 -- original expression and the generated declaration end up being
3547 -- expanded here.
3549 if Rewritten_For_C (Subp) then
3550 return;
3551 end if;
3553 Proc_Formals := New_List;
3555 -- Create a list of formal parameters with the same types as the
3556 -- function.
3558 Func_Formal := First_Formal (Subp);
3559 while Present (Func_Formal) loop
3560 Append_To (Proc_Formals,
3561 Make_Parameter_Specification (Loc,
3562 Defining_Identifier =>
3563 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3564 Parameter_Type =>
3565 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3567 Next_Formal (Func_Formal);
3568 end loop;
3570 -- Add an extra out parameter to carry the function result
3572 Name_Len := 6;
3573 Name_Buffer (1 .. Name_Len) := "RESULT";
3574 Append_To (Proc_Formals,
3575 Make_Parameter_Specification (Loc,
3576 Defining_Identifier =>
3577 Make_Defining_Identifier (Loc, Chars => Name_Find),
3578 Out_Present => True,
3579 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3581 -- The new procedure declaration is inserted immediately after the
3582 -- function declaration. The processing in Build_Procedure_Body_Form
3583 -- relies on this order.
3585 Proc_Decl :=
3586 Make_Subprogram_Declaration (Loc,
3587 Specification =>
3588 Make_Procedure_Specification (Loc,
3589 Defining_Unit_Name =>
3590 Make_Defining_Identifier (Loc, Chars (Subp)),
3591 Parameter_Specifications => Proc_Formals));
3593 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3595 -- Entity of procedure must remain invisible so that it does not
3596 -- overload subsequent references to the original function.
3598 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3600 -- Mark the function as having a procedure form and link the function
3601 -- and its internally built procedure.
3603 Set_Rewritten_For_C (Subp);
3604 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3605 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3606 end Build_Procedure_Form;
3608 ------------------------
3609 -- Build_Runtime_Call --
3610 ------------------------
3612 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3613 begin
3614 -- If entity is not available, we can skip making the call (this avoids
3615 -- junk duplicated error messages in a number of cases).
3617 if not RTE_Available (RE) then
3618 return Make_Null_Statement (Loc);
3619 else
3620 return
3621 Make_Procedure_Call_Statement (Loc,
3622 Name => New_Occurrence_Of (RTE (RE), Loc));
3623 end if;
3624 end Build_Runtime_Call;
3626 ------------------------
3627 -- Build_SS_Mark_Call --
3628 ------------------------
3630 function Build_SS_Mark_Call
3631 (Loc : Source_Ptr;
3632 Mark : Entity_Id) return Node_Id
3634 begin
3635 -- Generate:
3636 -- Mark : constant Mark_Id := SS_Mark;
3638 return
3639 Make_Object_Declaration (Loc,
3640 Defining_Identifier => Mark,
3641 Constant_Present => True,
3642 Object_Definition =>
3643 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3644 Expression =>
3645 Make_Function_Call (Loc,
3646 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3647 end Build_SS_Mark_Call;
3649 ---------------------------
3650 -- Build_SS_Release_Call --
3651 ---------------------------
3653 function Build_SS_Release_Call
3654 (Loc : Source_Ptr;
3655 Mark : Entity_Id) return Node_Id
3657 begin
3658 -- Generate:
3659 -- SS_Release (Mark);
3661 return
3662 Make_Procedure_Call_Statement (Loc,
3663 Name =>
3664 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3665 Parameter_Associations => New_List (
3666 New_Occurrence_Of (Mark, Loc)));
3667 end Build_SS_Release_Call;
3669 ----------------------------
3670 -- Build_Task_Array_Image --
3671 ----------------------------
3673 -- This function generates the body for a function that constructs the
3674 -- image string for a task that is an array component. The function is
3675 -- local to the init proc for the array type, and is called for each one
3676 -- of the components. The constructed image has the form of an indexed
3677 -- component, whose prefix is the outer variable of the array type.
3678 -- The n-dimensional array type has known indexes Index, Index2...
3680 -- Id_Ref is an indexed component form created by the enclosing init proc.
3681 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3682 -- in the loops that call the individual task init proc on each component.
3684 -- The generated function has the following structure:
3686 -- function F return String is
3687 -- Pref : string renames Task_Name;
3688 -- T1 : String := Index1'Image (Val1);
3689 -- ...
3690 -- Tn : String := indexn'image (Valn);
3691 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3692 -- -- Len includes commas and the end parentheses.
3693 -- Res : String (1..Len);
3694 -- Pos : Integer := Pref'Length;
3696 -- begin
3697 -- Res (1 .. Pos) := Pref;
3698 -- Pos := Pos + 1;
3699 -- Res (Pos) := '(';
3700 -- Pos := Pos + 1;
3701 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3702 -- Pos := Pos + T1'Length;
3703 -- Res (Pos) := '.';
3704 -- Pos := Pos + 1;
3705 -- ...
3706 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3707 -- Res (Len) := ')';
3709 -- return Res;
3710 -- end F;
3712 -- Needless to say, multidimensional arrays of tasks are rare enough that
3713 -- the bulkiness of this code is not really a concern.
3715 function Build_Task_Array_Image
3716 (Loc : Source_Ptr;
3717 Id_Ref : Node_Id;
3718 A_Type : Entity_Id;
3719 Dyn : Boolean := False) return Node_Id
3721 Dims : constant Nat := Number_Dimensions (A_Type);
3722 -- Number of dimensions for array of tasks
3724 Temps : array (1 .. Dims) of Entity_Id;
3725 -- Array of temporaries to hold string for each index
3727 Indx : Node_Id;
3728 -- Index expression
3730 Len : Entity_Id;
3731 -- Total length of generated name
3733 Pos : Entity_Id;
3734 -- Running index for substring assignments
3736 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3737 -- Name of enclosing variable, prefix of resulting name
3739 Res : Entity_Id;
3740 -- String to hold result
3742 Val : Node_Id;
3743 -- Value of successive indexes
3745 Sum : Node_Id;
3746 -- Expression to compute total size of string
3748 T : Entity_Id;
3749 -- Entity for name at one index position
3751 Decls : constant List_Id := New_List;
3752 Stats : constant List_Id := New_List;
3754 begin
3755 -- For a dynamic task, the name comes from the target variable. For a
3756 -- static one it is a formal of the enclosing init proc.
3758 if Dyn then
3759 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3760 Append_To (Decls,
3761 Make_Object_Declaration (Loc,
3762 Defining_Identifier => Pref,
3763 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3764 Expression =>
3765 Make_String_Literal (Loc,
3766 Strval => String_From_Name_Buffer)));
3768 else
3769 Append_To (Decls,
3770 Make_Object_Renaming_Declaration (Loc,
3771 Defining_Identifier => Pref,
3772 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3773 Name => Make_Identifier (Loc, Name_uTask_Name)));
3774 end if;
3776 Indx := First_Index (A_Type);
3777 Val := First (Expressions (Id_Ref));
3779 for J in 1 .. Dims loop
3780 T := Make_Temporary (Loc, 'T');
3781 Temps (J) := T;
3783 Append_To (Decls,
3784 Make_Object_Declaration (Loc,
3785 Defining_Identifier => T,
3786 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3787 Expression =>
3788 Make_Attribute_Reference (Loc,
3789 Attribute_Name => Name_Image,
3790 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3791 Expressions => New_List (New_Copy_Tree (Val)))));
3793 Next_Index (Indx);
3794 Next (Val);
3795 end loop;
3797 Sum := Make_Integer_Literal (Loc, Dims + 1);
3799 Sum :=
3800 Make_Op_Add (Loc,
3801 Left_Opnd => Sum,
3802 Right_Opnd =>
3803 Make_Attribute_Reference (Loc,
3804 Attribute_Name => Name_Length,
3805 Prefix => New_Occurrence_Of (Pref, Loc),
3806 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3808 for J in 1 .. Dims loop
3809 Sum :=
3810 Make_Op_Add (Loc,
3811 Left_Opnd => Sum,
3812 Right_Opnd =>
3813 Make_Attribute_Reference (Loc,
3814 Attribute_Name => Name_Length,
3815 Prefix =>
3816 New_Occurrence_Of (Temps (J), Loc),
3817 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3818 end loop;
3820 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3822 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3824 Append_To (Stats,
3825 Make_Assignment_Statement (Loc,
3826 Name =>
3827 Make_Indexed_Component (Loc,
3828 Prefix => New_Occurrence_Of (Res, Loc),
3829 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3830 Expression =>
3831 Make_Character_Literal (Loc,
3832 Chars => Name_Find,
3833 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3835 Append_To (Stats,
3836 Make_Assignment_Statement (Loc,
3837 Name => New_Occurrence_Of (Pos, Loc),
3838 Expression =>
3839 Make_Op_Add (Loc,
3840 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3841 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3843 for J in 1 .. Dims loop
3845 Append_To (Stats,
3846 Make_Assignment_Statement (Loc,
3847 Name =>
3848 Make_Slice (Loc,
3849 Prefix => New_Occurrence_Of (Res, Loc),
3850 Discrete_Range =>
3851 Make_Range (Loc,
3852 Low_Bound => New_Occurrence_Of (Pos, Loc),
3853 High_Bound =>
3854 Make_Op_Subtract (Loc,
3855 Left_Opnd =>
3856 Make_Op_Add (Loc,
3857 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3858 Right_Opnd =>
3859 Make_Attribute_Reference (Loc,
3860 Attribute_Name => Name_Length,
3861 Prefix =>
3862 New_Occurrence_Of (Temps (J), Loc),
3863 Expressions =>
3864 New_List (Make_Integer_Literal (Loc, 1)))),
3865 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3867 Expression => New_Occurrence_Of (Temps (J), Loc)));
3869 if J < Dims then
3870 Append_To (Stats,
3871 Make_Assignment_Statement (Loc,
3872 Name => New_Occurrence_Of (Pos, Loc),
3873 Expression =>
3874 Make_Op_Add (Loc,
3875 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3876 Right_Opnd =>
3877 Make_Attribute_Reference (Loc,
3878 Attribute_Name => Name_Length,
3879 Prefix => New_Occurrence_Of (Temps (J), Loc),
3880 Expressions =>
3881 New_List (Make_Integer_Literal (Loc, 1))))));
3883 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3885 Append_To (Stats,
3886 Make_Assignment_Statement (Loc,
3887 Name => Make_Indexed_Component (Loc,
3888 Prefix => New_Occurrence_Of (Res, Loc),
3889 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3890 Expression =>
3891 Make_Character_Literal (Loc,
3892 Chars => Name_Find,
3893 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3895 Append_To (Stats,
3896 Make_Assignment_Statement (Loc,
3897 Name => New_Occurrence_Of (Pos, Loc),
3898 Expression =>
3899 Make_Op_Add (Loc,
3900 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3901 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3902 end if;
3903 end loop;
3905 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3907 Append_To (Stats,
3908 Make_Assignment_Statement (Loc,
3909 Name =>
3910 Make_Indexed_Component (Loc,
3911 Prefix => New_Occurrence_Of (Res, Loc),
3912 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3913 Expression =>
3914 Make_Character_Literal (Loc,
3915 Chars => Name_Find,
3916 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3917 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3918 end Build_Task_Array_Image;
3920 ----------------------------
3921 -- Build_Task_Image_Decls --
3922 ----------------------------
3924 function Build_Task_Image_Decls
3925 (Loc : Source_Ptr;
3926 Id_Ref : Node_Id;
3927 A_Type : Entity_Id;
3928 In_Init_Proc : Boolean := False) return List_Id
3930 Decls : constant List_Id := New_List;
3931 T_Id : Entity_Id := Empty;
3932 Decl : Node_Id;
3933 Expr : Node_Id := Empty;
3934 Fun : Node_Id := Empty;
3935 Is_Dyn : constant Boolean :=
3936 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3937 and then
3938 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3940 begin
3941 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3942 -- generate a dummy declaration only.
3944 if Restriction_Active (No_Implicit_Heap_Allocations)
3945 or else Global_Discard_Names
3946 then
3947 T_Id := Make_Temporary (Loc, 'J');
3948 Name_Len := 0;
3950 return
3951 New_List (
3952 Make_Object_Declaration (Loc,
3953 Defining_Identifier => T_Id,
3954 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3955 Expression =>
3956 Make_String_Literal (Loc,
3957 Strval => String_From_Name_Buffer)));
3959 else
3960 if Nkind (Id_Ref) = N_Identifier
3961 or else Nkind (Id_Ref) = N_Defining_Identifier
3962 then
3963 -- For a simple variable, the image of the task is built from
3964 -- the name of the variable. To avoid possible conflict with the
3965 -- anonymous type created for a single protected object, add a
3966 -- numeric suffix.
3968 T_Id :=
3969 Make_Defining_Identifier (Loc,
3970 New_External_Name (Chars (Id_Ref), 'T', 1));
3972 Get_Name_String (Chars (Id_Ref));
3974 Expr :=
3975 Make_String_Literal (Loc,
3976 Strval => String_From_Name_Buffer);
3978 elsif Nkind (Id_Ref) = N_Selected_Component then
3979 T_Id :=
3980 Make_Defining_Identifier (Loc,
3981 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3982 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3984 elsif Nkind (Id_Ref) = N_Indexed_Component then
3985 T_Id :=
3986 Make_Defining_Identifier (Loc,
3987 New_External_Name (Chars (A_Type), 'N'));
3989 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3990 end if;
3991 end if;
3993 if Present (Fun) then
3994 Append (Fun, Decls);
3995 Expr := Make_Function_Call (Loc,
3996 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
3998 if not In_Init_Proc then
3999 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4000 end if;
4001 end if;
4003 Decl := Make_Object_Declaration (Loc,
4004 Defining_Identifier => T_Id,
4005 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4006 Constant_Present => True,
4007 Expression => Expr);
4009 Append (Decl, Decls);
4010 return Decls;
4011 end Build_Task_Image_Decls;
4013 -------------------------------
4014 -- Build_Task_Image_Function --
4015 -------------------------------
4017 function Build_Task_Image_Function
4018 (Loc : Source_Ptr;
4019 Decls : List_Id;
4020 Stats : List_Id;
4021 Res : Entity_Id) return Node_Id
4023 Spec : Node_Id;
4025 begin
4026 Append_To (Stats,
4027 Make_Simple_Return_Statement (Loc,
4028 Expression => New_Occurrence_Of (Res, Loc)));
4030 Spec := Make_Function_Specification (Loc,
4031 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4032 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4034 -- Calls to 'Image use the secondary stack, which must be cleaned up
4035 -- after the task name is built.
4037 return Make_Subprogram_Body (Loc,
4038 Specification => Spec,
4039 Declarations => Decls,
4040 Handled_Statement_Sequence =>
4041 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4042 end Build_Task_Image_Function;
4044 -----------------------------
4045 -- Build_Task_Image_Prefix --
4046 -----------------------------
4048 procedure Build_Task_Image_Prefix
4049 (Loc : Source_Ptr;
4050 Len : out Entity_Id;
4051 Res : out Entity_Id;
4052 Pos : out Entity_Id;
4053 Prefix : Entity_Id;
4054 Sum : Node_Id;
4055 Decls : List_Id;
4056 Stats : List_Id)
4058 begin
4059 Len := Make_Temporary (Loc, 'L', Sum);
4061 Append_To (Decls,
4062 Make_Object_Declaration (Loc,
4063 Defining_Identifier => Len,
4064 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4065 Expression => Sum));
4067 Res := Make_Temporary (Loc, 'R');
4069 Append_To (Decls,
4070 Make_Object_Declaration (Loc,
4071 Defining_Identifier => Res,
4072 Object_Definition =>
4073 Make_Subtype_Indication (Loc,
4074 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4075 Constraint =>
4076 Make_Index_Or_Discriminant_Constraint (Loc,
4077 Constraints =>
4078 New_List (
4079 Make_Range (Loc,
4080 Low_Bound => Make_Integer_Literal (Loc, 1),
4081 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4083 -- Indicate that the result is an internal temporary, so it does not
4084 -- receive a bogus initialization when declaration is expanded. This
4085 -- is both efficient, and prevents anomalies in the handling of
4086 -- dynamic objects on the secondary stack.
4088 Set_Is_Internal (Res);
4089 Pos := Make_Temporary (Loc, 'P');
4091 Append_To (Decls,
4092 Make_Object_Declaration (Loc,
4093 Defining_Identifier => Pos,
4094 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4096 -- Pos := Prefix'Length;
4098 Append_To (Stats,
4099 Make_Assignment_Statement (Loc,
4100 Name => New_Occurrence_Of (Pos, Loc),
4101 Expression =>
4102 Make_Attribute_Reference (Loc,
4103 Attribute_Name => Name_Length,
4104 Prefix => New_Occurrence_Of (Prefix, Loc),
4105 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4107 -- Res (1 .. Pos) := Prefix;
4109 Append_To (Stats,
4110 Make_Assignment_Statement (Loc,
4111 Name =>
4112 Make_Slice (Loc,
4113 Prefix => New_Occurrence_Of (Res, Loc),
4114 Discrete_Range =>
4115 Make_Range (Loc,
4116 Low_Bound => Make_Integer_Literal (Loc, 1),
4117 High_Bound => New_Occurrence_Of (Pos, Loc))),
4119 Expression => New_Occurrence_Of (Prefix, Loc)));
4121 Append_To (Stats,
4122 Make_Assignment_Statement (Loc,
4123 Name => New_Occurrence_Of (Pos, Loc),
4124 Expression =>
4125 Make_Op_Add (Loc,
4126 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4127 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4128 end Build_Task_Image_Prefix;
4130 -----------------------------
4131 -- Build_Task_Record_Image --
4132 -----------------------------
4134 function Build_Task_Record_Image
4135 (Loc : Source_Ptr;
4136 Id_Ref : Node_Id;
4137 Dyn : Boolean := False) return Node_Id
4139 Len : Entity_Id;
4140 -- Total length of generated name
4142 Pos : Entity_Id;
4143 -- Index into result
4145 Res : Entity_Id;
4146 -- String to hold result
4148 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4149 -- Name of enclosing variable, prefix of resulting name
4151 Sum : Node_Id;
4152 -- Expression to compute total size of string
4154 Sel : Entity_Id;
4155 -- Entity for selector name
4157 Decls : constant List_Id := New_List;
4158 Stats : constant List_Id := New_List;
4160 begin
4161 -- For a dynamic task, the name comes from the target variable. For a
4162 -- static one it is a formal of the enclosing init proc.
4164 if Dyn then
4165 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4166 Append_To (Decls,
4167 Make_Object_Declaration (Loc,
4168 Defining_Identifier => Pref,
4169 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4170 Expression =>
4171 Make_String_Literal (Loc,
4172 Strval => String_From_Name_Buffer)));
4174 else
4175 Append_To (Decls,
4176 Make_Object_Renaming_Declaration (Loc,
4177 Defining_Identifier => Pref,
4178 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4179 Name => Make_Identifier (Loc, Name_uTask_Name)));
4180 end if;
4182 Sel := Make_Temporary (Loc, 'S');
4184 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4186 Append_To (Decls,
4187 Make_Object_Declaration (Loc,
4188 Defining_Identifier => Sel,
4189 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4190 Expression =>
4191 Make_String_Literal (Loc,
4192 Strval => String_From_Name_Buffer)));
4194 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4196 Sum :=
4197 Make_Op_Add (Loc,
4198 Left_Opnd => Sum,
4199 Right_Opnd =>
4200 Make_Attribute_Reference (Loc,
4201 Attribute_Name => Name_Length,
4202 Prefix =>
4203 New_Occurrence_Of (Pref, Loc),
4204 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4206 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4208 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4210 -- Res (Pos) := '.';
4212 Append_To (Stats,
4213 Make_Assignment_Statement (Loc,
4214 Name => Make_Indexed_Component (Loc,
4215 Prefix => New_Occurrence_Of (Res, Loc),
4216 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4217 Expression =>
4218 Make_Character_Literal (Loc,
4219 Chars => Name_Find,
4220 Char_Literal_Value =>
4221 UI_From_Int (Character'Pos ('.')))));
4223 Append_To (Stats,
4224 Make_Assignment_Statement (Loc,
4225 Name => New_Occurrence_Of (Pos, Loc),
4226 Expression =>
4227 Make_Op_Add (Loc,
4228 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4229 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4231 -- Res (Pos .. Len) := Selector;
4233 Append_To (Stats,
4234 Make_Assignment_Statement (Loc,
4235 Name => Make_Slice (Loc,
4236 Prefix => New_Occurrence_Of (Res, Loc),
4237 Discrete_Range =>
4238 Make_Range (Loc,
4239 Low_Bound => New_Occurrence_Of (Pos, Loc),
4240 High_Bound => New_Occurrence_Of (Len, Loc))),
4241 Expression => New_Occurrence_Of (Sel, Loc)));
4243 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4244 end Build_Task_Record_Image;
4246 ---------------------------------------
4247 -- Build_Transient_Object_Statements --
4248 ---------------------------------------
4250 procedure Build_Transient_Object_Statements
4251 (Obj_Decl : Node_Id;
4252 Fin_Call : out Node_Id;
4253 Hook_Assign : out Node_Id;
4254 Hook_Clear : out Node_Id;
4255 Hook_Decl : out Node_Id;
4256 Ptr_Decl : out Node_Id;
4257 Finalize_Obj : Boolean := True)
4259 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4260 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4261 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4263 Desig_Typ : Entity_Id;
4264 Hook_Expr : Node_Id;
4265 Hook_Id : Entity_Id;
4266 Obj_Ref : Node_Id;
4267 Ptr_Typ : Entity_Id;
4269 begin
4270 -- Recover the type of the object
4272 Desig_Typ := Obj_Typ;
4274 if Is_Access_Type (Desig_Typ) then
4275 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4276 end if;
4278 -- Create an access type which provides a reference to the transient
4279 -- object. Generate:
4281 -- type Ptr_Typ is access all Desig_Typ;
4283 Ptr_Typ := Make_Temporary (Loc, 'A');
4284 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4285 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4287 Ptr_Decl :=
4288 Make_Full_Type_Declaration (Loc,
4289 Defining_Identifier => Ptr_Typ,
4290 Type_Definition =>
4291 Make_Access_To_Object_Definition (Loc,
4292 All_Present => True,
4293 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4295 -- Create a temporary check which acts as a hook to the transient
4296 -- object. Generate:
4298 -- Hook : Ptr_Typ := null;
4300 Hook_Id := Make_Temporary (Loc, 'T');
4301 Set_Ekind (Hook_Id, E_Variable);
4302 Set_Etype (Hook_Id, Ptr_Typ);
4304 Hook_Decl :=
4305 Make_Object_Declaration (Loc,
4306 Defining_Identifier => Hook_Id,
4307 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4308 Expression => Make_Null (Loc));
4310 -- Mark the temporary as a hook. This signals the machinery in
4311 -- Build_Finalizer to recognize this special case.
4313 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4315 -- Hook the transient object to the temporary. Generate:
4317 -- Hook := Ptr_Typ (Obj_Id);
4318 -- <or>
4319 -- Hool := Obj_Id'Unrestricted_Access;
4321 if Is_Access_Type (Obj_Typ) then
4322 Hook_Expr :=
4323 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4324 else
4325 Hook_Expr :=
4326 Make_Attribute_Reference (Loc,
4327 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4328 Attribute_Name => Name_Unrestricted_Access);
4329 end if;
4331 Hook_Assign :=
4332 Make_Assignment_Statement (Loc,
4333 Name => New_Occurrence_Of (Hook_Id, Loc),
4334 Expression => Hook_Expr);
4336 -- Crear the hook prior to finalizing the object. Generate:
4338 -- Hook := null;
4340 Hook_Clear :=
4341 Make_Assignment_Statement (Loc,
4342 Name => New_Occurrence_Of (Hook_Id, Loc),
4343 Expression => Make_Null (Loc));
4345 -- Finalize the object. Generate:
4347 -- [Deep_]Finalize (Obj_Ref[.all]);
4349 if Finalize_Obj then
4350 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4352 if Is_Access_Type (Obj_Typ) then
4353 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4354 Set_Etype (Obj_Ref, Desig_Typ);
4355 end if;
4357 Fin_Call :=
4358 Make_Final_Call
4359 (Obj_Ref => Obj_Ref,
4360 Typ => Desig_Typ);
4362 -- Otherwise finalize the hook. Generate:
4364 -- [Deep_]Finalize (Hook.all);
4366 else
4367 Fin_Call :=
4368 Make_Final_Call (
4369 Obj_Ref =>
4370 Make_Explicit_Dereference (Loc,
4371 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4372 Typ => Desig_Typ);
4373 end if;
4374 end Build_Transient_Object_Statements;
4376 -----------------------------
4377 -- Check_Float_Op_Overflow --
4378 -----------------------------
4380 procedure Check_Float_Op_Overflow (N : Node_Id) is
4381 begin
4382 -- Return if no check needed
4384 if not Is_Floating_Point_Type (Etype (N))
4385 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4387 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4388 -- and do not expand the code for float overflow checking.
4390 or else CodePeer_Mode
4391 then
4392 return;
4393 end if;
4395 -- Otherwise we replace the expression by
4397 -- do Tnn : constant ftype := expression;
4398 -- constraint_error when not Tnn'Valid;
4399 -- in Tnn;
4401 declare
4402 Loc : constant Source_Ptr := Sloc (N);
4403 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4404 Typ : constant Entity_Id := Etype (N);
4406 begin
4407 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4408 -- right here. We also set the node as analyzed to prevent infinite
4409 -- recursion from repeating the operation in the expansion.
4411 Set_Do_Overflow_Check (N, False);
4412 Set_Analyzed (N, True);
4414 -- Do the rewrite to include the check
4416 Rewrite (N,
4417 Make_Expression_With_Actions (Loc,
4418 Actions => New_List (
4419 Make_Object_Declaration (Loc,
4420 Defining_Identifier => Tnn,
4421 Object_Definition => New_Occurrence_Of (Typ, Loc),
4422 Constant_Present => True,
4423 Expression => Relocate_Node (N)),
4424 Make_Raise_Constraint_Error (Loc,
4425 Condition =>
4426 Make_Op_Not (Loc,
4427 Right_Opnd =>
4428 Make_Attribute_Reference (Loc,
4429 Prefix => New_Occurrence_Of (Tnn, Loc),
4430 Attribute_Name => Name_Valid)),
4431 Reason => CE_Overflow_Check_Failed)),
4432 Expression => New_Occurrence_Of (Tnn, Loc)));
4434 Analyze_And_Resolve (N, Typ);
4435 end;
4436 end Check_Float_Op_Overflow;
4438 ----------------------------------
4439 -- Component_May_Be_Bit_Aligned --
4440 ----------------------------------
4442 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4443 UT : Entity_Id;
4445 begin
4446 -- If no component clause, then everything is fine, since the back end
4447 -- never bit-misaligns by default, even if there is a pragma Packed for
4448 -- the record.
4450 if No (Comp) or else No (Component_Clause (Comp)) then
4451 return False;
4452 end if;
4454 UT := Underlying_Type (Etype (Comp));
4456 -- It is only array and record types that cause trouble
4458 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4459 return False;
4461 -- If we know that we have a small (64 bits or less) record or small
4462 -- bit-packed array, then everything is fine, since the back end can
4463 -- handle these cases correctly.
4465 elsif Esize (Comp) <= 64
4466 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4467 then
4468 return False;
4470 -- Otherwise if the component is not byte aligned, we know we have the
4471 -- nasty unaligned case.
4473 elsif Normalized_First_Bit (Comp) /= Uint_0
4474 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4475 then
4476 return True;
4478 -- If we are large and byte aligned, then OK at this level
4480 else
4481 return False;
4482 end if;
4483 end Component_May_Be_Bit_Aligned;
4485 ----------------------------------------
4486 -- Containing_Package_With_Ext_Axioms --
4487 ----------------------------------------
4489 function Containing_Package_With_Ext_Axioms
4490 (E : Entity_Id) return Entity_Id
4492 begin
4493 -- E is the package or generic package which is externally axiomatized
4495 if Ekind_In (E, E_Generic_Package, E_Package)
4496 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4497 then
4498 return E;
4499 end if;
4501 -- If E's scope is axiomatized, E is axiomatized
4503 if Present (Scope (E)) then
4504 declare
4505 First_Ax_Parent_Scope : constant Entity_Id :=
4506 Containing_Package_With_Ext_Axioms (Scope (E));
4507 begin
4508 if Present (First_Ax_Parent_Scope) then
4509 return First_Ax_Parent_Scope;
4510 end if;
4511 end;
4512 end if;
4514 -- Otherwise, if E is a package instance, it is axiomatized if the
4515 -- corresponding generic package is axiomatized.
4517 if Ekind (E) = E_Package then
4518 declare
4519 Par : constant Node_Id := Parent (E);
4520 Decl : Node_Id;
4522 begin
4523 if Nkind (Par) = N_Defining_Program_Unit_Name then
4524 Decl := Parent (Par);
4525 else
4526 Decl := Par;
4527 end if;
4529 if Present (Generic_Parent (Decl)) then
4530 return
4531 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4532 end if;
4533 end;
4534 end if;
4536 return Empty;
4537 end Containing_Package_With_Ext_Axioms;
4539 -------------------------------
4540 -- Convert_To_Actual_Subtype --
4541 -------------------------------
4543 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4544 Act_ST : Entity_Id;
4546 begin
4547 Act_ST := Get_Actual_Subtype (Exp);
4549 if Act_ST = Etype (Exp) then
4550 return;
4551 else
4552 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4553 Analyze_And_Resolve (Exp, Act_ST);
4554 end if;
4555 end Convert_To_Actual_Subtype;
4557 -----------------------------------
4558 -- Corresponding_Runtime_Package --
4559 -----------------------------------
4561 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4562 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4563 -- Return True if protected type T has one entry and the maximum queue
4564 -- length is one.
4566 --------------------------------
4567 -- Has_One_Entry_And_No_Queue --
4568 --------------------------------
4570 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4571 Item : Entity_Id;
4572 Is_First : Boolean := True;
4574 begin
4575 Item := First_Entity (T);
4576 while Present (Item) loop
4577 if Is_Entry (Item) then
4579 -- The protected type has more than one entry
4581 if not Is_First then
4582 return False;
4583 end if;
4585 -- The queue length is not one
4587 if not Restriction_Active (No_Entry_Queue)
4588 and then Get_Max_Queue_Length (Item) /= Uint_1
4589 then
4590 return False;
4591 end if;
4593 Is_First := False;
4594 end if;
4596 Next_Entity (Item);
4597 end loop;
4599 return True;
4600 end Has_One_Entry_And_No_Queue;
4602 -- Local variables
4604 Pkg_Id : RTU_Id := RTU_Null;
4606 -- Start of processing for Corresponding_Runtime_Package
4608 begin
4609 pragma Assert (Is_Concurrent_Type (Typ));
4611 if Ekind (Typ) in Protected_Kind then
4612 if Has_Entries (Typ)
4614 -- A protected type without entries that covers an interface and
4615 -- overrides the abstract routines with protected procedures is
4616 -- considered equivalent to a protected type with entries in the
4617 -- context of dispatching select statements. It is sufficient to
4618 -- check for the presence of an interface list in the declaration
4619 -- node to recognize this case.
4621 or else Present (Interface_List (Parent (Typ)))
4623 -- Protected types with interrupt handlers (when not using a
4624 -- restricted profile) are also considered equivalent to
4625 -- protected types with entries. The types which are used
4626 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4627 -- are derived from Protection_Entries.
4629 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4630 or else Has_Interrupt_Handler (Typ)
4631 then
4632 if Abort_Allowed
4633 or else Restriction_Active (No_Select_Statements) = False
4634 or else not Has_One_Entry_And_No_Queue (Typ)
4635 or else (Has_Attach_Handler (Typ)
4636 and then not Restricted_Profile)
4637 then
4638 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4639 else
4640 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4641 end if;
4643 else
4644 Pkg_Id := System_Tasking_Protected_Objects;
4645 end if;
4646 end if;
4648 return Pkg_Id;
4649 end Corresponding_Runtime_Package;
4651 -----------------------------------
4652 -- Current_Sem_Unit_Declarations --
4653 -----------------------------------
4655 function Current_Sem_Unit_Declarations return List_Id is
4656 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4657 Decls : List_Id;
4659 begin
4660 -- If the current unit is a package body, locate the visible
4661 -- declarations of the package spec.
4663 if Nkind (U) = N_Package_Body then
4664 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4665 end if;
4667 if Nkind (U) = N_Package_Declaration then
4668 U := Specification (U);
4669 Decls := Visible_Declarations (U);
4671 if No (Decls) then
4672 Decls := New_List;
4673 Set_Visible_Declarations (U, Decls);
4674 end if;
4676 else
4677 Decls := Declarations (U);
4679 if No (Decls) then
4680 Decls := New_List;
4681 Set_Declarations (U, Decls);
4682 end if;
4683 end if;
4685 return Decls;
4686 end Current_Sem_Unit_Declarations;
4688 -----------------------
4689 -- Duplicate_Subexpr --
4690 -----------------------
4692 function Duplicate_Subexpr
4693 (Exp : Node_Id;
4694 Name_Req : Boolean := False;
4695 Renaming_Req : Boolean := False) return Node_Id
4697 begin
4698 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4699 return New_Copy_Tree (Exp);
4700 end Duplicate_Subexpr;
4702 ---------------------------------
4703 -- Duplicate_Subexpr_No_Checks --
4704 ---------------------------------
4706 function Duplicate_Subexpr_No_Checks
4707 (Exp : Node_Id;
4708 Name_Req : Boolean := False;
4709 Renaming_Req : Boolean := False;
4710 Related_Id : Entity_Id := Empty;
4711 Is_Low_Bound : Boolean := False;
4712 Is_High_Bound : Boolean := False) return Node_Id
4714 New_Exp : Node_Id;
4716 begin
4717 Remove_Side_Effects
4718 (Exp => Exp,
4719 Name_Req => Name_Req,
4720 Renaming_Req => Renaming_Req,
4721 Related_Id => Related_Id,
4722 Is_Low_Bound => Is_Low_Bound,
4723 Is_High_Bound => Is_High_Bound);
4725 New_Exp := New_Copy_Tree (Exp);
4726 Remove_Checks (New_Exp);
4727 return New_Exp;
4728 end Duplicate_Subexpr_No_Checks;
4730 -----------------------------------
4731 -- Duplicate_Subexpr_Move_Checks --
4732 -----------------------------------
4734 function Duplicate_Subexpr_Move_Checks
4735 (Exp : Node_Id;
4736 Name_Req : Boolean := False;
4737 Renaming_Req : Boolean := False) return Node_Id
4739 New_Exp : Node_Id;
4741 begin
4742 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4743 New_Exp := New_Copy_Tree (Exp);
4744 Remove_Checks (Exp);
4745 return New_Exp;
4746 end Duplicate_Subexpr_Move_Checks;
4748 -------------------------
4749 -- Enclosing_Init_Proc --
4750 -------------------------
4752 function Enclosing_Init_Proc return Entity_Id is
4753 S : Entity_Id;
4755 begin
4756 S := Current_Scope;
4757 while Present (S) and then S /= Standard_Standard loop
4758 if Is_Init_Proc (S) then
4759 return S;
4760 else
4761 S := Scope (S);
4762 end if;
4763 end loop;
4765 return Empty;
4766 end Enclosing_Init_Proc;
4768 --------------------
4769 -- Ensure_Defined --
4770 --------------------
4772 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4773 IR : Node_Id;
4775 begin
4776 -- An itype reference must only be created if this is a local itype, so
4777 -- that gigi can elaborate it on the proper objstack.
4779 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4780 IR := Make_Itype_Reference (Sloc (N));
4781 Set_Itype (IR, Typ);
4782 Insert_Action (N, IR);
4783 end if;
4784 end Ensure_Defined;
4786 --------------------
4787 -- Entry_Names_OK --
4788 --------------------
4790 function Entry_Names_OK return Boolean is
4791 begin
4792 return
4793 not Restricted_Profile
4794 and then not Global_Discard_Names
4795 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4796 and then not Restriction_Active (No_Local_Allocators);
4797 end Entry_Names_OK;
4799 -------------------
4800 -- Evaluate_Name --
4801 -------------------
4803 procedure Evaluate_Name (Nam : Node_Id) is
4804 begin
4805 -- For an attribute reference or an indexed component, evaluate the
4806 -- prefix, which is itself a name, recursively, and then force the
4807 -- evaluation of all the subscripts (or attribute expressions).
4809 case Nkind (Nam) is
4810 when N_Attribute_Reference
4811 | N_Indexed_Component
4813 Evaluate_Name (Prefix (Nam));
4815 declare
4816 E : Node_Id;
4818 begin
4819 E := First (Expressions (Nam));
4820 while Present (E) loop
4821 Force_Evaluation (E);
4823 if Is_Rewrite_Substitution (E) then
4824 Set_Do_Range_Check
4825 (E, Do_Range_Check (Original_Node (E)));
4826 end if;
4828 Next (E);
4829 end loop;
4830 end;
4832 -- For an explicit dereference, we simply force the evaluation of
4833 -- the name expression. The dereference provides a value that is the
4834 -- address for the renamed object, and it is precisely this value
4835 -- that we want to preserve.
4837 when N_Explicit_Dereference =>
4838 Force_Evaluation (Prefix (Nam));
4840 -- For a function call, we evaluate the call
4842 when N_Function_Call =>
4843 Force_Evaluation (Nam);
4845 -- For a qualified expression, we evaluate the underlying object
4846 -- name if any, otherwise we force the evaluation of the underlying
4847 -- expression.
4849 when N_Qualified_Expression =>
4850 if Is_Object_Reference (Expression (Nam)) then
4851 Evaluate_Name (Expression (Nam));
4852 else
4853 Force_Evaluation (Expression (Nam));
4854 end if;
4856 -- For a selected component, we simply evaluate the prefix
4858 when N_Selected_Component =>
4859 Evaluate_Name (Prefix (Nam));
4861 -- For a slice, we evaluate the prefix, as for the indexed component
4862 -- case and then, if there is a range present, either directly or as
4863 -- the constraint of a discrete subtype indication, we evaluate the
4864 -- two bounds of this range.
4866 when N_Slice =>
4867 Evaluate_Name (Prefix (Nam));
4868 Evaluate_Slice_Bounds (Nam);
4870 -- For a type conversion, the expression of the conversion must be
4871 -- the name of an object, and we simply need to evaluate this name.
4873 when N_Type_Conversion =>
4874 Evaluate_Name (Expression (Nam));
4876 -- The remaining cases are direct name, operator symbol and character
4877 -- literal. In all these cases, we do nothing, since we want to
4878 -- reevaluate each time the renamed object is used.
4880 when others =>
4881 null;
4882 end case;
4883 end Evaluate_Name;
4885 ---------------------------
4886 -- Evaluate_Slice_Bounds --
4887 ---------------------------
4889 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4890 DR : constant Node_Id := Discrete_Range (Slice);
4891 Constr : Node_Id;
4892 Rexpr : Node_Id;
4894 begin
4895 if Nkind (DR) = N_Range then
4896 Force_Evaluation (Low_Bound (DR));
4897 Force_Evaluation (High_Bound (DR));
4899 elsif Nkind (DR) = N_Subtype_Indication then
4900 Constr := Constraint (DR);
4902 if Nkind (Constr) = N_Range_Constraint then
4903 Rexpr := Range_Expression (Constr);
4905 Force_Evaluation (Low_Bound (Rexpr));
4906 Force_Evaluation (High_Bound (Rexpr));
4907 end if;
4908 end if;
4909 end Evaluate_Slice_Bounds;
4911 ---------------------
4912 -- Evolve_And_Then --
4913 ---------------------
4915 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4916 begin
4917 if No (Cond) then
4918 Cond := Cond1;
4919 else
4920 Cond :=
4921 Make_And_Then (Sloc (Cond1),
4922 Left_Opnd => Cond,
4923 Right_Opnd => Cond1);
4924 end if;
4925 end Evolve_And_Then;
4927 --------------------
4928 -- Evolve_Or_Else --
4929 --------------------
4931 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4932 begin
4933 if No (Cond) then
4934 Cond := Cond1;
4935 else
4936 Cond :=
4937 Make_Or_Else (Sloc (Cond1),
4938 Left_Opnd => Cond,
4939 Right_Opnd => Cond1);
4940 end if;
4941 end Evolve_Or_Else;
4943 -----------------------------------
4944 -- Exceptions_In_Finalization_OK --
4945 -----------------------------------
4947 function Exceptions_In_Finalization_OK return Boolean is
4948 begin
4949 return
4950 not (Restriction_Active (No_Exception_Handlers) or else
4951 Restriction_Active (No_Exception_Propagation) or else
4952 Restriction_Active (No_Exceptions));
4953 end Exceptions_In_Finalization_OK;
4955 -----------------------------------------
4956 -- Expand_Static_Predicates_In_Choices --
4957 -----------------------------------------
4959 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4960 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4962 Choices : constant List_Id := Discrete_Choices (N);
4964 Choice : Node_Id;
4965 Next_C : Node_Id;
4966 P : Node_Id;
4967 C : Node_Id;
4969 begin
4970 Choice := First (Choices);
4971 while Present (Choice) loop
4972 Next_C := Next (Choice);
4974 -- Check for name of subtype with static predicate
4976 if Is_Entity_Name (Choice)
4977 and then Is_Type (Entity (Choice))
4978 and then Has_Predicates (Entity (Choice))
4979 then
4980 -- Loop through entries in predicate list, converting to choices
4981 -- and inserting in the list before the current choice. Note that
4982 -- if the list is empty, corresponding to a False predicate, then
4983 -- no choices are inserted.
4985 P := First (Static_Discrete_Predicate (Entity (Choice)));
4986 while Present (P) loop
4988 -- If low bound and high bounds are equal, copy simple choice
4990 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4991 C := New_Copy (Low_Bound (P));
4993 -- Otherwise copy a range
4995 else
4996 C := New_Copy (P);
4997 end if;
4999 -- Change Sloc to referencing choice (rather than the Sloc of
5000 -- the predicate declaration element itself).
5002 Set_Sloc (C, Sloc (Choice));
5003 Insert_Before (Choice, C);
5004 Next (P);
5005 end loop;
5007 -- Delete the predicated entry
5009 Remove (Choice);
5010 end if;
5012 -- Move to next choice to check
5014 Choice := Next_C;
5015 end loop;
5017 Set_Has_SP_Choice (N, False);
5018 end Expand_Static_Predicates_In_Choices;
5020 ------------------------------
5021 -- Expand_Subtype_From_Expr --
5022 ------------------------------
5024 -- This function is applicable for both static and dynamic allocation of
5025 -- objects which are constrained by an initial expression. Basically it
5026 -- transforms an unconstrained subtype indication into a constrained one.
5028 -- The expression may also be transformed in certain cases in order to
5029 -- avoid multiple evaluation. In the static allocation case, the general
5030 -- scheme is:
5032 -- Val : T := Expr;
5034 -- is transformed into
5036 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5038 -- Here are the main cases :
5040 -- <if Expr is a Slice>
5041 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5043 -- <elsif Expr is a String Literal>
5044 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5046 -- <elsif Expr is Constrained>
5047 -- subtype T is Type_Of_Expr
5048 -- Val : T := Expr;
5050 -- <elsif Expr is an entity_name>
5051 -- Val : T (constraints taken from Expr) := Expr;
5053 -- <else>
5054 -- type Axxx is access all T;
5055 -- Rval : Axxx := Expr'ref;
5056 -- Val : T (constraints taken from Rval) := Rval.all;
5058 -- ??? note: when the Expression is allocated in the secondary stack
5059 -- we could use it directly instead of copying it by declaring
5060 -- Val : T (...) renames Rval.all
5062 procedure Expand_Subtype_From_Expr
5063 (N : Node_Id;
5064 Unc_Type : Entity_Id;
5065 Subtype_Indic : Node_Id;
5066 Exp : Node_Id;
5067 Related_Id : Entity_Id := Empty)
5069 Loc : constant Source_Ptr := Sloc (N);
5070 Exp_Typ : constant Entity_Id := Etype (Exp);
5071 T : Entity_Id;
5073 begin
5074 -- In general we cannot build the subtype if expansion is disabled,
5075 -- because internal entities may not have been defined. However, to
5076 -- avoid some cascaded errors, we try to continue when the expression is
5077 -- an array (or string), because it is safe to compute the bounds. It is
5078 -- in fact required to do so even in a generic context, because there
5079 -- may be constants that depend on the bounds of a string literal, both
5080 -- standard string types and more generally arrays of characters.
5082 -- In GNATprove mode, these extra subtypes are not needed
5084 if GNATprove_Mode then
5085 return;
5086 end if;
5088 if not Expander_Active
5089 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5090 then
5091 return;
5092 end if;
5094 if Nkind (Exp) = N_Slice then
5095 declare
5096 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5098 begin
5099 Rewrite (Subtype_Indic,
5100 Make_Subtype_Indication (Loc,
5101 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5102 Constraint =>
5103 Make_Index_Or_Discriminant_Constraint (Loc,
5104 Constraints => New_List
5105 (New_Occurrence_Of (Slice_Type, Loc)))));
5107 -- This subtype indication may be used later for constraint checks
5108 -- we better make sure that if a variable was used as a bound of
5109 -- of the original slice, its value is frozen.
5111 Evaluate_Slice_Bounds (Exp);
5112 end;
5114 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5115 Rewrite (Subtype_Indic,
5116 Make_Subtype_Indication (Loc,
5117 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5118 Constraint =>
5119 Make_Index_Or_Discriminant_Constraint (Loc,
5120 Constraints => New_List (
5121 Make_Literal_Range (Loc,
5122 Literal_Typ => Exp_Typ)))));
5124 -- If the type of the expression is an internally generated type it
5125 -- may not be necessary to create a new subtype. However there are two
5126 -- exceptions: references to the current instances, and aliased array
5127 -- object declarations for which the back end has to create a template.
5129 elsif Is_Constrained (Exp_Typ)
5130 and then not Is_Class_Wide_Type (Unc_Type)
5131 and then
5132 (Nkind (N) /= N_Object_Declaration
5133 or else not Is_Entity_Name (Expression (N))
5134 or else not Comes_From_Source (Entity (Expression (N)))
5135 or else not Is_Array_Type (Exp_Typ)
5136 or else not Aliased_Present (N))
5137 then
5138 if Is_Itype (Exp_Typ) then
5140 -- Within an initialization procedure, a selected component
5141 -- denotes a component of the enclosing record, and it appears as
5142 -- an actual in a call to its own initialization procedure. If
5143 -- this component depends on the outer discriminant, we must
5144 -- generate the proper actual subtype for it.
5146 if Nkind (Exp) = N_Selected_Component
5147 and then Within_Init_Proc
5148 then
5149 declare
5150 Decl : constant Node_Id :=
5151 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5152 begin
5153 if Present (Decl) then
5154 Insert_Action (N, Decl);
5155 T := Defining_Identifier (Decl);
5156 else
5157 T := Exp_Typ;
5158 end if;
5159 end;
5161 -- No need to generate a new subtype
5163 else
5164 T := Exp_Typ;
5165 end if;
5167 else
5168 T := Make_Temporary (Loc, 'T');
5170 Insert_Action (N,
5171 Make_Subtype_Declaration (Loc,
5172 Defining_Identifier => T,
5173 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5175 -- This type is marked as an itype even though it has an explicit
5176 -- declaration since otherwise Is_Generic_Actual_Type can get
5177 -- set, resulting in the generation of spurious errors. (See
5178 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5180 Set_Is_Itype (T);
5181 Set_Associated_Node_For_Itype (T, Exp);
5182 end if;
5184 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5186 -- Nothing needs to be done for private types with unknown discriminants
5187 -- if the underlying type is not an unconstrained composite type or it
5188 -- is an unchecked union.
5190 elsif Is_Private_Type (Unc_Type)
5191 and then Has_Unknown_Discriminants (Unc_Type)
5192 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5193 or else Is_Constrained (Underlying_Type (Unc_Type))
5194 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5195 then
5196 null;
5198 -- Case of derived type with unknown discriminants where the parent type
5199 -- also has unknown discriminants.
5201 elsif Is_Record_Type (Unc_Type)
5202 and then not Is_Class_Wide_Type (Unc_Type)
5203 and then Has_Unknown_Discriminants (Unc_Type)
5204 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5205 then
5206 -- Nothing to be done if no underlying record view available
5208 -- If this is a limited type derived from a type with unknown
5209 -- discriminants, do not expand either, so that subsequent expansion
5210 -- of the call can add build-in-place parameters to call.
5212 if No (Underlying_Record_View (Unc_Type))
5213 or else Is_Limited_Type (Unc_Type)
5214 then
5215 null;
5217 -- Otherwise use the Underlying_Record_View to create the proper
5218 -- constrained subtype for an object of a derived type with unknown
5219 -- discriminants.
5221 else
5222 Remove_Side_Effects (Exp);
5223 Rewrite (Subtype_Indic,
5224 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5225 end if;
5227 -- Renamings of class-wide interface types require no equivalent
5228 -- constrained type declarations because we only need to reference
5229 -- the tag component associated with the interface. The same is
5230 -- presumably true for class-wide types in general, so this test
5231 -- is broadened to include all class-wide renamings, which also
5232 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5233 -- (Is this really correct, or are there some cases of class-wide
5234 -- renamings that require action in this procedure???)
5236 elsif Present (N)
5237 and then Nkind (N) = N_Object_Renaming_Declaration
5238 and then Is_Class_Wide_Type (Unc_Type)
5239 then
5240 null;
5242 -- In Ada 95 nothing to be done if the type of the expression is limited
5243 -- because in this case the expression cannot be copied, and its use can
5244 -- only be by reference.
5246 -- In Ada 2005 the context can be an object declaration whose expression
5247 -- is a function that returns in place. If the nominal subtype has
5248 -- unknown discriminants, the call still provides constraints on the
5249 -- object, and we have to create an actual subtype from it.
5251 -- If the type is class-wide, the expression is dynamically tagged and
5252 -- we do not create an actual subtype either. Ditto for an interface.
5253 -- For now this applies only if the type is immutably limited, and the
5254 -- function being called is build-in-place. This will have to be revised
5255 -- when build-in-place functions are generalized to other types.
5257 elsif Is_Limited_View (Exp_Typ)
5258 and then
5259 (Is_Class_Wide_Type (Exp_Typ)
5260 or else Is_Interface (Exp_Typ)
5261 or else not Has_Unknown_Discriminants (Exp_Typ)
5262 or else not Is_Composite_Type (Unc_Type))
5263 then
5264 null;
5266 -- For limited objects initialized with build in place function calls,
5267 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5268 -- node in the expression initializing the object, which breaks the
5269 -- circuitry that detects and adds the additional arguments to the
5270 -- called function.
5272 elsif Is_Build_In_Place_Function_Call (Exp) then
5273 null;
5275 else
5276 Remove_Side_Effects (Exp);
5277 Rewrite (Subtype_Indic,
5278 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5279 end if;
5280 end Expand_Subtype_From_Expr;
5282 ---------------------------------------------
5283 -- Expression_Contains_Primitives_Calls_Of --
5284 ---------------------------------------------
5286 function Expression_Contains_Primitives_Calls_Of
5287 (Expr : Node_Id;
5288 Typ : Entity_Id) return Boolean
5290 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5292 Calls_OK : Boolean := False;
5293 -- This flag is set to True when expression Expr contains at least one
5294 -- call to a nondispatching primitive function of Typ.
5296 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5297 -- Search for nondispatching calls to primitive functions of type Typ
5299 ----------------------------
5300 -- Search_Primitive_Calls --
5301 ----------------------------
5303 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5304 Disp_Typ : Entity_Id;
5305 Subp : Entity_Id;
5307 begin
5308 -- Detect a function call that could denote a nondispatching
5309 -- primitive of the input type.
5311 if Nkind (N) = N_Function_Call
5312 and then Is_Entity_Name (Name (N))
5313 then
5314 Subp := Entity (Name (N));
5316 -- Do not consider function calls with a controlling argument, as
5317 -- those are always dispatching calls.
5319 if Is_Dispatching_Operation (Subp)
5320 and then No (Controlling_Argument (N))
5321 then
5322 Disp_Typ := Find_Dispatching_Type (Subp);
5324 -- To qualify as a suitable primitive, the dispatching type of
5325 -- the function must be the input type.
5327 if Present (Disp_Typ)
5328 and then Unique_Entity (Disp_Typ) = U_Typ
5329 then
5330 Calls_OK := True;
5332 -- There is no need to continue the traversal, as one such
5333 -- call suffices.
5335 return Abandon;
5336 end if;
5337 end if;
5338 end if;
5340 return OK;
5341 end Search_Primitive_Calls;
5343 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5345 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5347 begin
5348 Search_Calls (Expr);
5349 return Calls_OK;
5350 end Expression_Contains_Primitives_Calls_Of;
5352 ----------------------
5353 -- Finalize_Address --
5354 ----------------------
5356 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5357 Utyp : Entity_Id := Typ;
5359 begin
5360 -- Handle protected class-wide or task class-wide types
5362 if Is_Class_Wide_Type (Utyp) then
5363 if Is_Concurrent_Type (Root_Type (Utyp)) then
5364 Utyp := Root_Type (Utyp);
5366 elsif Is_Private_Type (Root_Type (Utyp))
5367 and then Present (Full_View (Root_Type (Utyp)))
5368 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5369 then
5370 Utyp := Full_View (Root_Type (Utyp));
5371 end if;
5372 end if;
5374 -- Handle private types
5376 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5377 Utyp := Full_View (Utyp);
5378 end if;
5380 -- Handle protected and task types
5382 if Is_Concurrent_Type (Utyp)
5383 and then Present (Corresponding_Record_Type (Utyp))
5384 then
5385 Utyp := Corresponding_Record_Type (Utyp);
5386 end if;
5388 Utyp := Underlying_Type (Base_Type (Utyp));
5390 -- Deal with untagged derivation of private views. If the parent is
5391 -- now known to be protected, the finalization routine is the one
5392 -- defined on the corresponding record of the ancestor (corresponding
5393 -- records do not automatically inherit operations, but maybe they
5394 -- should???)
5396 if Is_Untagged_Derivation (Typ) then
5397 if Is_Protected_Type (Typ) then
5398 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5400 else
5401 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5403 if Is_Protected_Type (Utyp) then
5404 Utyp := Corresponding_Record_Type (Utyp);
5405 end if;
5406 end if;
5407 end if;
5409 -- If the underlying_type is a subtype, we are dealing with the
5410 -- completion of a private type. We need to access the base type and
5411 -- generate a conversion to it.
5413 if Utyp /= Base_Type (Utyp) then
5414 pragma Assert (Is_Private_Type (Typ));
5416 Utyp := Base_Type (Utyp);
5417 end if;
5419 -- When dealing with an internally built full view for a type with
5420 -- unknown discriminants, use the original record type.
5422 if Is_Underlying_Record_View (Utyp) then
5423 Utyp := Etype (Utyp);
5424 end if;
5426 return TSS (Utyp, TSS_Finalize_Address);
5427 end Finalize_Address;
5429 ------------------------
5430 -- Find_Interface_ADT --
5431 ------------------------
5433 function Find_Interface_ADT
5434 (T : Entity_Id;
5435 Iface : Entity_Id) return Elmt_Id
5437 ADT : Elmt_Id;
5438 Typ : Entity_Id := T;
5440 begin
5441 pragma Assert (Is_Interface (Iface));
5443 -- Handle private types
5445 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5446 Typ := Full_View (Typ);
5447 end if;
5449 -- Handle access types
5451 if Is_Access_Type (Typ) then
5452 Typ := Designated_Type (Typ);
5453 end if;
5455 -- Handle task and protected types implementing interfaces
5457 if Is_Concurrent_Type (Typ) then
5458 Typ := Corresponding_Record_Type (Typ);
5459 end if;
5461 pragma Assert
5462 (not Is_Class_Wide_Type (Typ)
5463 and then Ekind (Typ) /= E_Incomplete_Type);
5465 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5466 return First_Elmt (Access_Disp_Table (Typ));
5468 else
5469 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5470 while Present (ADT)
5471 and then Present (Related_Type (Node (ADT)))
5472 and then Related_Type (Node (ADT)) /= Iface
5473 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5474 Use_Full_View => True)
5475 loop
5476 Next_Elmt (ADT);
5477 end loop;
5479 pragma Assert (Present (Related_Type (Node (ADT))));
5480 return ADT;
5481 end if;
5482 end Find_Interface_ADT;
5484 ------------------------
5485 -- Find_Interface_Tag --
5486 ------------------------
5488 function Find_Interface_Tag
5489 (T : Entity_Id;
5490 Iface : Entity_Id) return Entity_Id
5492 AI_Tag : Entity_Id := Empty;
5493 Found : Boolean := False;
5494 Typ : Entity_Id := T;
5496 procedure Find_Tag (Typ : Entity_Id);
5497 -- Internal subprogram used to recursively climb to the ancestors
5499 --------------
5500 -- Find_Tag --
5501 --------------
5503 procedure Find_Tag (Typ : Entity_Id) is
5504 AI_Elmt : Elmt_Id;
5505 AI : Node_Id;
5507 begin
5508 -- This routine does not handle the case in which the interface is an
5509 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5511 pragma Assert (Typ /= Iface);
5513 -- Climb to the root type handling private types
5515 if Present (Full_View (Etype (Typ))) then
5516 if Full_View (Etype (Typ)) /= Typ then
5517 Find_Tag (Full_View (Etype (Typ)));
5518 end if;
5520 elsif Etype (Typ) /= Typ then
5521 Find_Tag (Etype (Typ));
5522 end if;
5524 -- Traverse the list of interfaces implemented by the type
5526 if not Found
5527 and then Present (Interfaces (Typ))
5528 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5529 then
5530 -- Skip the tag associated with the primary table
5532 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5533 pragma Assert (Present (AI_Tag));
5535 AI_Elmt := First_Elmt (Interfaces (Typ));
5536 while Present (AI_Elmt) loop
5537 AI := Node (AI_Elmt);
5539 if AI = Iface
5540 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5541 then
5542 Found := True;
5543 return;
5544 end if;
5546 AI_Tag := Next_Tag_Component (AI_Tag);
5547 Next_Elmt (AI_Elmt);
5548 end loop;
5549 end if;
5550 end Find_Tag;
5552 -- Start of processing for Find_Interface_Tag
5554 begin
5555 pragma Assert (Is_Interface (Iface));
5557 -- Handle access types
5559 if Is_Access_Type (Typ) then
5560 Typ := Designated_Type (Typ);
5561 end if;
5563 -- Handle class-wide types
5565 if Is_Class_Wide_Type (Typ) then
5566 Typ := Root_Type (Typ);
5567 end if;
5569 -- Handle private types
5571 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5572 Typ := Full_View (Typ);
5573 end if;
5575 -- Handle entities from the limited view
5577 if Ekind (Typ) = E_Incomplete_Type then
5578 pragma Assert (Present (Non_Limited_View (Typ)));
5579 Typ := Non_Limited_View (Typ);
5580 end if;
5582 -- Handle task and protected types implementing interfaces
5584 if Is_Concurrent_Type (Typ) then
5585 Typ := Corresponding_Record_Type (Typ);
5586 end if;
5588 -- If the interface is an ancestor of the type, then it shared the
5589 -- primary dispatch table.
5591 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5592 return First_Tag_Component (Typ);
5594 -- Otherwise we need to search for its associated tag component
5596 else
5597 Find_Tag (Typ);
5598 return AI_Tag;
5599 end if;
5600 end Find_Interface_Tag;
5602 ---------------------------
5603 -- Find_Optional_Prim_Op --
5604 ---------------------------
5606 function Find_Optional_Prim_Op
5607 (T : Entity_Id; Name : Name_Id) return Entity_Id
5609 Prim : Elmt_Id;
5610 Typ : Entity_Id := T;
5611 Op : Entity_Id;
5613 begin
5614 if Is_Class_Wide_Type (Typ) then
5615 Typ := Root_Type (Typ);
5616 end if;
5618 Typ := Underlying_Type (Typ);
5620 -- Loop through primitive operations
5622 Prim := First_Elmt (Primitive_Operations (Typ));
5623 while Present (Prim) loop
5624 Op := Node (Prim);
5626 -- We can retrieve primitive operations by name if it is an internal
5627 -- name. For equality we must check that both of its operands have
5628 -- the same type, to avoid confusion with user-defined equalities
5629 -- than may have a non-symmetric signature.
5631 exit when Chars (Op) = Name
5632 and then
5633 (Name /= Name_Op_Eq
5634 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5636 Next_Elmt (Prim);
5637 end loop;
5639 return Node (Prim); -- Empty if not found
5640 end Find_Optional_Prim_Op;
5642 ---------------------------
5643 -- Find_Optional_Prim_Op --
5644 ---------------------------
5646 function Find_Optional_Prim_Op
5647 (T : Entity_Id;
5648 Name : TSS_Name_Type) return Entity_Id
5650 Inher_Op : Entity_Id := Empty;
5651 Own_Op : Entity_Id := Empty;
5652 Prim_Elmt : Elmt_Id;
5653 Prim_Id : Entity_Id;
5654 Typ : Entity_Id := T;
5656 begin
5657 if Is_Class_Wide_Type (Typ) then
5658 Typ := Root_Type (Typ);
5659 end if;
5661 Typ := Underlying_Type (Typ);
5663 -- This search is based on the assertion that the dispatching version
5664 -- of the TSS routine always precedes the real primitive.
5666 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5667 while Present (Prim_Elmt) loop
5668 Prim_Id := Node (Prim_Elmt);
5670 if Is_TSS (Prim_Id, Name) then
5671 if Present (Alias (Prim_Id)) then
5672 Inher_Op := Prim_Id;
5673 else
5674 Own_Op := Prim_Id;
5675 end if;
5676 end if;
5678 Next_Elmt (Prim_Elmt);
5679 end loop;
5681 if Present (Own_Op) then
5682 return Own_Op;
5683 elsif Present (Inher_Op) then
5684 return Inher_Op;
5685 else
5686 return Empty;
5687 end if;
5688 end Find_Optional_Prim_Op;
5690 ------------------
5691 -- Find_Prim_Op --
5692 ------------------
5694 function Find_Prim_Op
5695 (T : Entity_Id; Name : Name_Id) return Entity_Id
5697 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5698 begin
5699 if No (Result) then
5700 raise Program_Error;
5701 end if;
5703 return Result;
5704 end Find_Prim_Op;
5706 ------------------
5707 -- Find_Prim_Op --
5708 ------------------
5710 function Find_Prim_Op
5711 (T : Entity_Id;
5712 Name : TSS_Name_Type) return Entity_Id
5714 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5715 begin
5716 if No (Result) then
5717 raise Program_Error;
5718 end if;
5720 return Result;
5721 end Find_Prim_Op;
5723 ----------------------------
5724 -- Find_Protection_Object --
5725 ----------------------------
5727 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5728 S : Entity_Id;
5730 begin
5731 S := Scop;
5732 while Present (S) loop
5733 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5734 and then Present (Protection_Object (S))
5735 then
5736 return Protection_Object (S);
5737 end if;
5739 S := Scope (S);
5740 end loop;
5742 -- If we do not find a Protection object in the scope chain, then
5743 -- something has gone wrong, most likely the object was never created.
5745 raise Program_Error;
5746 end Find_Protection_Object;
5748 --------------------------
5749 -- Find_Protection_Type --
5750 --------------------------
5752 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5753 Comp : Entity_Id;
5754 Typ : Entity_Id := Conc_Typ;
5756 begin
5757 if Is_Concurrent_Type (Typ) then
5758 Typ := Corresponding_Record_Type (Typ);
5759 end if;
5761 -- Since restriction violations are not considered serious errors, the
5762 -- expander remains active, but may leave the corresponding record type
5763 -- malformed. In such cases, component _object is not available so do
5764 -- not look for it.
5766 if not Analyzed (Typ) then
5767 return Empty;
5768 end if;
5770 Comp := First_Component (Typ);
5771 while Present (Comp) loop
5772 if Chars (Comp) = Name_uObject then
5773 return Base_Type (Etype (Comp));
5774 end if;
5776 Next_Component (Comp);
5777 end loop;
5779 -- The corresponding record of a protected type should always have an
5780 -- _object field.
5782 raise Program_Error;
5783 end Find_Protection_Type;
5785 -----------------------
5786 -- Find_Hook_Context --
5787 -----------------------
5789 function Find_Hook_Context (N : Node_Id) return Node_Id is
5790 Par : Node_Id;
5791 Top : Node_Id;
5793 Wrapped_Node : Node_Id;
5794 -- Note: if we are in a transient scope, we want to reuse it as
5795 -- the context for actions insertion, if possible. But if N is itself
5796 -- part of the stored actions for the current transient scope,
5797 -- then we need to insert at the appropriate (inner) location in
5798 -- the not as an action on Node_To_Be_Wrapped.
5800 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5802 begin
5803 -- When the node is inside a case/if expression, the lifetime of any
5804 -- temporary controlled object is extended. Find a suitable insertion
5805 -- node by locating the topmost case or if expressions.
5807 if In_Cond_Expr then
5808 Par := N;
5809 Top := N;
5810 while Present (Par) loop
5811 if Nkind_In (Original_Node (Par), N_Case_Expression,
5812 N_If_Expression)
5813 then
5814 Top := Par;
5816 -- Prevent the search from going too far
5818 elsif Is_Body_Or_Package_Declaration (Par) then
5819 exit;
5820 end if;
5822 Par := Parent (Par);
5823 end loop;
5825 -- The topmost case or if expression is now recovered, but it may
5826 -- still not be the correct place to add generated code. Climb to
5827 -- find a parent that is part of a declarative or statement list,
5828 -- and is not a list of actuals in a call.
5830 Par := Top;
5831 while Present (Par) loop
5832 if Is_List_Member (Par)
5833 and then not Nkind_In (Par, N_Component_Association,
5834 N_Discriminant_Association,
5835 N_Parameter_Association,
5836 N_Pragma_Argument_Association)
5837 and then not Nkind_In (Parent (Par), N_Function_Call,
5838 N_Procedure_Call_Statement,
5839 N_Entry_Call_Statement)
5841 then
5842 return Par;
5844 -- Prevent the search from going too far
5846 elsif Is_Body_Or_Package_Declaration (Par) then
5847 exit;
5848 end if;
5850 Par := Parent (Par);
5851 end loop;
5853 return Par;
5855 else
5856 Par := N;
5857 while Present (Par) loop
5859 -- Keep climbing past various operators
5861 if Nkind (Parent (Par)) in N_Op
5862 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5863 then
5864 Par := Parent (Par);
5865 else
5866 exit;
5867 end if;
5868 end loop;
5870 Top := Par;
5872 -- The node may be located in a pragma in which case return the
5873 -- pragma itself:
5875 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5877 -- Similar case occurs when the node is related to an object
5878 -- declaration or assignment:
5880 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5882 -- Another case to consider is when the node is part of a return
5883 -- statement:
5885 -- return ... and then Ctrl_Func_Call ...;
5887 -- Another case is when the node acts as a formal in a procedure
5888 -- call statement:
5890 -- Proc (... and then Ctrl_Func_Call ...);
5892 if Scope_Is_Transient then
5893 Wrapped_Node := Node_To_Be_Wrapped;
5894 else
5895 Wrapped_Node := Empty;
5896 end if;
5898 while Present (Par) loop
5899 if Par = Wrapped_Node
5900 or else Nkind_In (Par, N_Assignment_Statement,
5901 N_Object_Declaration,
5902 N_Pragma,
5903 N_Procedure_Call_Statement,
5904 N_Simple_Return_Statement)
5905 then
5906 return Par;
5908 -- Prevent the search from going too far
5910 elsif Is_Body_Or_Package_Declaration (Par) then
5911 exit;
5912 end if;
5914 Par := Parent (Par);
5915 end loop;
5917 -- Return the topmost short circuit operator
5919 return Top;
5920 end if;
5921 end Find_Hook_Context;
5923 ------------------------------
5924 -- Following_Address_Clause --
5925 ------------------------------
5927 function Following_Address_Clause (D : Node_Id) return Node_Id is
5928 Id : constant Entity_Id := Defining_Identifier (D);
5929 Result : Node_Id;
5930 Par : Node_Id;
5932 function Check_Decls (D : Node_Id) return Node_Id;
5933 -- This internal function differs from the main function in that it
5934 -- gets called to deal with a following package private part, and
5935 -- it checks declarations starting with D (the main function checks
5936 -- declarations following D). If D is Empty, then Empty is returned.
5938 -----------------
5939 -- Check_Decls --
5940 -----------------
5942 function Check_Decls (D : Node_Id) return Node_Id is
5943 Decl : Node_Id;
5945 begin
5946 Decl := D;
5947 while Present (Decl) loop
5948 if Nkind (Decl) = N_At_Clause
5949 and then Chars (Identifier (Decl)) = Chars (Id)
5950 then
5951 return Decl;
5953 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5954 and then Chars (Decl) = Name_Address
5955 and then Chars (Name (Decl)) = Chars (Id)
5956 then
5957 return Decl;
5958 end if;
5960 Next (Decl);
5961 end loop;
5963 -- Otherwise not found, return Empty
5965 return Empty;
5966 end Check_Decls;
5968 -- Start of processing for Following_Address_Clause
5970 begin
5971 -- If parser detected no address clause for the identifier in question,
5972 -- then the answer is a quick NO, without the need for a search.
5974 if not Get_Name_Table_Boolean1 (Chars (Id)) then
5975 return Empty;
5976 end if;
5978 -- Otherwise search current declarative unit
5980 Result := Check_Decls (Next (D));
5982 if Present (Result) then
5983 return Result;
5984 end if;
5986 -- Check for possible package private part following
5988 Par := Parent (D);
5990 if Nkind (Par) = N_Package_Specification
5991 and then Visible_Declarations (Par) = List_Containing (D)
5992 and then Present (Private_Declarations (Par))
5993 then
5994 -- Private part present, check declarations there
5996 return Check_Decls (First (Private_Declarations (Par)));
5998 else
5999 -- No private part, clause not found, return Empty
6001 return Empty;
6002 end if;
6003 end Following_Address_Clause;
6005 ----------------------
6006 -- Force_Evaluation --
6007 ----------------------
6009 procedure Force_Evaluation
6010 (Exp : Node_Id;
6011 Name_Req : Boolean := False;
6012 Related_Id : Entity_Id := Empty;
6013 Is_Low_Bound : Boolean := False;
6014 Is_High_Bound : Boolean := False;
6015 Mode : Force_Evaluation_Mode := Relaxed)
6017 begin
6018 Remove_Side_Effects
6019 (Exp => Exp,
6020 Name_Req => Name_Req,
6021 Variable_Ref => True,
6022 Renaming_Req => False,
6023 Related_Id => Related_Id,
6024 Is_Low_Bound => Is_Low_Bound,
6025 Is_High_Bound => Is_High_Bound,
6026 Check_Side_Effects =>
6027 Is_Static_Expression (Exp)
6028 or else Mode = Relaxed);
6029 end Force_Evaluation;
6031 ---------------------------------
6032 -- Fully_Qualified_Name_String --
6033 ---------------------------------
6035 function Fully_Qualified_Name_String
6036 (E : Entity_Id;
6037 Append_NUL : Boolean := True) return String_Id
6039 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6040 -- Compute recursively the qualified name without NUL at the end, adding
6041 -- it to the currently started string being generated
6043 ----------------------------------
6044 -- Internal_Full_Qualified_Name --
6045 ----------------------------------
6047 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6048 Ent : Entity_Id;
6050 begin
6051 -- Deal properly with child units
6053 if Nkind (E) = N_Defining_Program_Unit_Name then
6054 Ent := Defining_Identifier (E);
6055 else
6056 Ent := E;
6057 end if;
6059 -- Compute qualification recursively (only "Standard" has no scope)
6061 if Present (Scope (Scope (Ent))) then
6062 Internal_Full_Qualified_Name (Scope (Ent));
6063 Store_String_Char (Get_Char_Code ('.'));
6064 end if;
6066 -- Every entity should have a name except some expanded blocks
6067 -- don't bother about those.
6069 if Chars (Ent) = No_Name then
6070 return;
6071 end if;
6073 -- Generates the entity name in upper case
6075 Get_Decoded_Name_String (Chars (Ent));
6076 Set_All_Upper_Case;
6077 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6078 return;
6079 end Internal_Full_Qualified_Name;
6081 -- Start of processing for Full_Qualified_Name
6083 begin
6084 Start_String;
6085 Internal_Full_Qualified_Name (E);
6087 if Append_NUL then
6088 Store_String_Char (Get_Char_Code (ASCII.NUL));
6089 end if;
6091 return End_String;
6092 end Fully_Qualified_Name_String;
6094 ------------------------
6095 -- Generate_Poll_Call --
6096 ------------------------
6098 procedure Generate_Poll_Call (N : Node_Id) is
6099 begin
6100 -- No poll call if polling not active
6102 if not Polling_Required then
6103 return;
6105 -- Otherwise generate require poll call
6107 else
6108 Insert_Before_And_Analyze (N,
6109 Make_Procedure_Call_Statement (Sloc (N),
6110 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6111 end if;
6112 end Generate_Poll_Call;
6114 ---------------------------------
6115 -- Get_Current_Value_Condition --
6116 ---------------------------------
6118 -- Note: the implementation of this procedure is very closely tied to the
6119 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6120 -- interpret Current_Value fields set by the Set procedure, so the two
6121 -- procedures need to be closely coordinated.
6123 procedure Get_Current_Value_Condition
6124 (Var : Node_Id;
6125 Op : out Node_Kind;
6126 Val : out Node_Id)
6128 Loc : constant Source_Ptr := Sloc (Var);
6129 Ent : constant Entity_Id := Entity (Var);
6131 procedure Process_Current_Value_Condition
6132 (N : Node_Id;
6133 S : Boolean);
6134 -- N is an expression which holds either True (S = True) or False (S =
6135 -- False) in the condition. This procedure digs out the expression and
6136 -- if it refers to Ent, sets Op and Val appropriately.
6138 -------------------------------------
6139 -- Process_Current_Value_Condition --
6140 -------------------------------------
6142 procedure Process_Current_Value_Condition
6143 (N : Node_Id;
6144 S : Boolean)
6146 Cond : Node_Id;
6147 Prev_Cond : Node_Id;
6148 Sens : Boolean;
6150 begin
6151 Cond := N;
6152 Sens := S;
6154 loop
6155 Prev_Cond := Cond;
6157 -- Deal with NOT operators, inverting sense
6159 while Nkind (Cond) = N_Op_Not loop
6160 Cond := Right_Opnd (Cond);
6161 Sens := not Sens;
6162 end loop;
6164 -- Deal with conversions, qualifications, and expressions with
6165 -- actions.
6167 while Nkind_In (Cond,
6168 N_Type_Conversion,
6169 N_Qualified_Expression,
6170 N_Expression_With_Actions)
6171 loop
6172 Cond := Expression (Cond);
6173 end loop;
6175 exit when Cond = Prev_Cond;
6176 end loop;
6178 -- Deal with AND THEN and AND cases
6180 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6182 -- Don't ever try to invert a condition that is of the form of an
6183 -- AND or AND THEN (since we are not doing sufficiently general
6184 -- processing to allow this).
6186 if Sens = False then
6187 Op := N_Empty;
6188 Val := Empty;
6189 return;
6190 end if;
6192 -- Recursively process AND and AND THEN branches
6194 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6196 if Op /= N_Empty then
6197 return;
6198 end if;
6200 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6201 return;
6203 -- Case of relational operator
6205 elsif Nkind (Cond) in N_Op_Compare then
6206 Op := Nkind (Cond);
6208 -- Invert sense of test if inverted test
6210 if Sens = False then
6211 case Op is
6212 when N_Op_Eq => Op := N_Op_Ne;
6213 when N_Op_Ne => Op := N_Op_Eq;
6214 when N_Op_Lt => Op := N_Op_Ge;
6215 when N_Op_Gt => Op := N_Op_Le;
6216 when N_Op_Le => Op := N_Op_Gt;
6217 when N_Op_Ge => Op := N_Op_Lt;
6218 when others => raise Program_Error;
6219 end case;
6220 end if;
6222 -- Case of entity op value
6224 if Is_Entity_Name (Left_Opnd (Cond))
6225 and then Ent = Entity (Left_Opnd (Cond))
6226 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6227 then
6228 Val := Right_Opnd (Cond);
6230 -- Case of value op entity
6232 elsif Is_Entity_Name (Right_Opnd (Cond))
6233 and then Ent = Entity (Right_Opnd (Cond))
6234 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6235 then
6236 Val := Left_Opnd (Cond);
6238 -- We are effectively swapping operands
6240 case Op is
6241 when N_Op_Eq => null;
6242 when N_Op_Ne => null;
6243 when N_Op_Lt => Op := N_Op_Gt;
6244 when N_Op_Gt => Op := N_Op_Lt;
6245 when N_Op_Le => Op := N_Op_Ge;
6246 when N_Op_Ge => Op := N_Op_Le;
6247 when others => raise Program_Error;
6248 end case;
6250 else
6251 Op := N_Empty;
6252 end if;
6254 return;
6256 elsif Nkind_In (Cond,
6257 N_Type_Conversion,
6258 N_Qualified_Expression,
6259 N_Expression_With_Actions)
6260 then
6261 Cond := Expression (Cond);
6263 -- Case of Boolean variable reference, return as though the
6264 -- reference had said var = True.
6266 else
6267 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6268 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6270 if Sens = False then
6271 Op := N_Op_Ne;
6272 else
6273 Op := N_Op_Eq;
6274 end if;
6275 end if;
6276 end if;
6277 end Process_Current_Value_Condition;
6279 -- Start of processing for Get_Current_Value_Condition
6281 begin
6282 Op := N_Empty;
6283 Val := Empty;
6285 -- Immediate return, nothing doing, if this is not an object
6287 if Ekind (Ent) not in Object_Kind then
6288 return;
6289 end if;
6291 -- Otherwise examine current value
6293 declare
6294 CV : constant Node_Id := Current_Value (Ent);
6295 Sens : Boolean;
6296 Stm : Node_Id;
6298 begin
6299 -- If statement. Condition is known true in THEN section, known False
6300 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6302 if Nkind (CV) = N_If_Statement then
6304 -- Before start of IF statement
6306 if Loc < Sloc (CV) then
6307 return;
6309 -- After end of IF statement
6311 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6312 return;
6313 end if;
6315 -- At this stage we know that we are within the IF statement, but
6316 -- unfortunately, the tree does not record the SLOC of the ELSE so
6317 -- we cannot use a simple SLOC comparison to distinguish between
6318 -- the then/else statements, so we have to climb the tree.
6320 declare
6321 N : Node_Id;
6323 begin
6324 N := Parent (Var);
6325 while Parent (N) /= CV loop
6326 N := Parent (N);
6328 -- If we fall off the top of the tree, then that's odd, but
6329 -- perhaps it could occur in some error situation, and the
6330 -- safest response is simply to assume that the outcome of
6331 -- the condition is unknown. No point in bombing during an
6332 -- attempt to optimize things.
6334 if No (N) then
6335 return;
6336 end if;
6337 end loop;
6339 -- Now we have N pointing to a node whose parent is the IF
6340 -- statement in question, so now we can tell if we are within
6341 -- the THEN statements.
6343 if Is_List_Member (N)
6344 and then List_Containing (N) = Then_Statements (CV)
6345 then
6346 Sens := True;
6348 -- If the variable reference does not come from source, we
6349 -- cannot reliably tell whether it appears in the else part.
6350 -- In particular, if it appears in generated code for a node
6351 -- that requires finalization, it may be attached to a list
6352 -- that has not been yet inserted into the code. For now,
6353 -- treat it as unknown.
6355 elsif not Comes_From_Source (N) then
6356 return;
6358 -- Otherwise we must be in ELSIF or ELSE part
6360 else
6361 Sens := False;
6362 end if;
6363 end;
6365 -- ELSIF part. Condition is known true within the referenced
6366 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6367 -- and unknown before the ELSE part or after the IF statement.
6369 elsif Nkind (CV) = N_Elsif_Part then
6371 -- if the Elsif_Part had condition_actions, the elsif has been
6372 -- rewritten as a nested if, and the original elsif_part is
6373 -- detached from the tree, so there is no way to obtain useful
6374 -- information on the current value of the variable.
6375 -- Can this be improved ???
6377 if No (Parent (CV)) then
6378 return;
6379 end if;
6381 Stm := Parent (CV);
6383 -- If the tree has been otherwise rewritten there is nothing
6384 -- else to be done either.
6386 if Nkind (Stm) /= N_If_Statement then
6387 return;
6388 end if;
6390 -- Before start of ELSIF part
6392 if Loc < Sloc (CV) then
6393 return;
6395 -- After end of IF statement
6397 elsif Loc >= Sloc (Stm) +
6398 Text_Ptr (UI_To_Int (End_Span (Stm)))
6399 then
6400 return;
6401 end if;
6403 -- Again we lack the SLOC of the ELSE, so we need to climb the
6404 -- tree to see if we are within the ELSIF part in question.
6406 declare
6407 N : Node_Id;
6409 begin
6410 N := Parent (Var);
6411 while Parent (N) /= Stm loop
6412 N := Parent (N);
6414 -- If we fall off the top of the tree, then that's odd, but
6415 -- perhaps it could occur in some error situation, and the
6416 -- safest response is simply to assume that the outcome of
6417 -- the condition is unknown. No point in bombing during an
6418 -- attempt to optimize things.
6420 if No (N) then
6421 return;
6422 end if;
6423 end loop;
6425 -- Now we have N pointing to a node whose parent is the IF
6426 -- statement in question, so see if is the ELSIF part we want.
6427 -- the THEN statements.
6429 if N = CV then
6430 Sens := True;
6432 -- Otherwise we must be in subsequent ELSIF or ELSE part
6434 else
6435 Sens := False;
6436 end if;
6437 end;
6439 -- Iteration scheme of while loop. The condition is known to be
6440 -- true within the body of the loop.
6442 elsif Nkind (CV) = N_Iteration_Scheme then
6443 declare
6444 Loop_Stmt : constant Node_Id := Parent (CV);
6446 begin
6447 -- Before start of body of loop
6449 if Loc < Sloc (Loop_Stmt) then
6450 return;
6452 -- After end of LOOP statement
6454 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6455 return;
6457 -- We are within the body of the loop
6459 else
6460 Sens := True;
6461 end if;
6462 end;
6464 -- All other cases of Current_Value settings
6466 else
6467 return;
6468 end if;
6470 -- If we fall through here, then we have a reportable condition, Sens
6471 -- is True if the condition is true and False if it needs inverting.
6473 Process_Current_Value_Condition (Condition (CV), Sens);
6474 end;
6475 end Get_Current_Value_Condition;
6477 ---------------------
6478 -- Get_Stream_Size --
6479 ---------------------
6481 function Get_Stream_Size (E : Entity_Id) return Uint is
6482 begin
6483 -- If we have a Stream_Size clause for this type use it
6485 if Has_Stream_Size_Clause (E) then
6486 return Static_Integer (Expression (Stream_Size_Clause (E)));
6488 -- Otherwise the Stream_Size if the size of the type
6490 else
6491 return Esize (E);
6492 end if;
6493 end Get_Stream_Size;
6495 ---------------------------
6496 -- Has_Access_Constraint --
6497 ---------------------------
6499 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6500 Disc : Entity_Id;
6501 T : constant Entity_Id := Etype (E);
6503 begin
6504 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6505 Disc := First_Discriminant (T);
6506 while Present (Disc) loop
6507 if Is_Access_Type (Etype (Disc)) then
6508 return True;
6509 end if;
6511 Next_Discriminant (Disc);
6512 end loop;
6514 return False;
6515 else
6516 return False;
6517 end if;
6518 end Has_Access_Constraint;
6520 -----------------------------------------------------
6521 -- Has_Annotate_Pragma_For_External_Axiomatization --
6522 -----------------------------------------------------
6524 function Has_Annotate_Pragma_For_External_Axiomatization
6525 (E : Entity_Id) return Boolean
6527 function Is_Annotate_Pragma_For_External_Axiomatization
6528 (N : Node_Id) return Boolean;
6529 -- Returns whether N is
6530 -- pragma Annotate (GNATprove, External_Axiomatization);
6532 ----------------------------------------------------
6533 -- Is_Annotate_Pragma_For_External_Axiomatization --
6534 ----------------------------------------------------
6536 -- The general form of pragma Annotate is
6538 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6539 -- ARG ::= NAME | EXPRESSION
6541 -- The first two arguments are by convention intended to refer to an
6542 -- external tool and a tool-specific function. These arguments are
6543 -- not analyzed.
6545 -- The following is used to annotate a package specification which
6546 -- GNATprove should treat specially, because the axiomatization of
6547 -- this unit is given by the user instead of being automatically
6548 -- generated.
6550 -- pragma Annotate (GNATprove, External_Axiomatization);
6552 function Is_Annotate_Pragma_For_External_Axiomatization
6553 (N : Node_Id) return Boolean
6555 Name_GNATprove : constant String :=
6556 "gnatprove";
6557 Name_External_Axiomatization : constant String :=
6558 "external_axiomatization";
6559 -- Special names
6561 begin
6562 if Nkind (N) = N_Pragma
6563 and then Get_Pragma_Id (N) = Pragma_Annotate
6564 and then List_Length (Pragma_Argument_Associations (N)) = 2
6565 then
6566 declare
6567 Arg1 : constant Node_Id :=
6568 First (Pragma_Argument_Associations (N));
6569 Arg2 : constant Node_Id := Next (Arg1);
6570 Nam1 : Name_Id;
6571 Nam2 : Name_Id;
6573 begin
6574 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6575 -- Name_External_Axiomatization so that Name_Find returns the
6576 -- corresponding name. This takes care of all possible casings.
6578 Name_Len := 0;
6579 Add_Str_To_Name_Buffer (Name_GNATprove);
6580 Nam1 := Name_Find;
6582 Name_Len := 0;
6583 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6584 Nam2 := Name_Find;
6586 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6587 and then
6588 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6589 end;
6591 else
6592 return False;
6593 end if;
6594 end Is_Annotate_Pragma_For_External_Axiomatization;
6596 -- Local variables
6598 Decl : Node_Id;
6599 Vis_Decls : List_Id;
6600 N : Node_Id;
6602 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6604 begin
6605 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6606 Decl := Parent (Parent (E));
6607 else
6608 Decl := Parent (E);
6609 end if;
6611 Vis_Decls := Visible_Declarations (Decl);
6613 N := First (Vis_Decls);
6614 while Present (N) loop
6616 -- Skip declarations generated by the frontend. Skip all pragmas
6617 -- that are not the desired Annotate pragma. Stop the search on
6618 -- the first non-pragma source declaration.
6620 if Comes_From_Source (N) then
6621 if Nkind (N) = N_Pragma then
6622 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6623 return True;
6624 end if;
6625 else
6626 return False;
6627 end if;
6628 end if;
6630 Next (N);
6631 end loop;
6633 return False;
6634 end Has_Annotate_Pragma_For_External_Axiomatization;
6636 --------------------
6637 -- Homonym_Number --
6638 --------------------
6640 function Homonym_Number (Subp : Entity_Id) return Nat is
6641 Count : Nat;
6642 Hom : Entity_Id;
6644 begin
6645 Count := 1;
6646 Hom := Homonym (Subp);
6647 while Present (Hom) loop
6648 if Scope (Hom) = Scope (Subp) then
6649 Count := Count + 1;
6650 end if;
6652 Hom := Homonym (Hom);
6653 end loop;
6655 return Count;
6656 end Homonym_Number;
6658 -----------------------------------
6659 -- In_Library_Level_Package_Body --
6660 -----------------------------------
6662 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6663 begin
6664 -- First determine whether the entity appears at the library level, then
6665 -- look at the containing unit.
6667 if Is_Library_Level_Entity (Id) then
6668 declare
6669 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6671 begin
6672 return Nkind (Unit (Container)) = N_Package_Body;
6673 end;
6674 end if;
6676 return False;
6677 end In_Library_Level_Package_Body;
6679 ------------------------------
6680 -- In_Unconditional_Context --
6681 ------------------------------
6683 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6684 P : Node_Id;
6686 begin
6687 P := Node;
6688 while Present (P) loop
6689 case Nkind (P) is
6690 when N_Subprogram_Body => return True;
6691 when N_If_Statement => return False;
6692 when N_Loop_Statement => return False;
6693 when N_Case_Statement => return False;
6694 when others => P := Parent (P);
6695 end case;
6696 end loop;
6698 return False;
6699 end In_Unconditional_Context;
6701 -------------------
6702 -- Insert_Action --
6703 -------------------
6705 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6706 begin
6707 if Present (Ins_Action) then
6708 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6709 end if;
6710 end Insert_Action;
6712 -- Version with check(s) suppressed
6714 procedure Insert_Action
6715 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6717 begin
6718 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6719 end Insert_Action;
6721 -------------------------
6722 -- Insert_Action_After --
6723 -------------------------
6725 procedure Insert_Action_After
6726 (Assoc_Node : Node_Id;
6727 Ins_Action : Node_Id)
6729 begin
6730 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6731 end Insert_Action_After;
6733 --------------------
6734 -- Insert_Actions --
6735 --------------------
6737 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6738 N : Node_Id;
6739 P : Node_Id;
6741 Wrapped_Node : Node_Id := Empty;
6743 begin
6744 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6745 return;
6746 end if;
6748 -- Ignore insert of actions from inside default expression (or other
6749 -- similar "spec expression") in the special spec-expression analyze
6750 -- mode. Any insertions at this point have no relevance, since we are
6751 -- only doing the analyze to freeze the types of any static expressions.
6752 -- See section "Handling of Default Expressions" in the spec of package
6753 -- Sem for further details.
6755 if In_Spec_Expression then
6756 return;
6757 end if;
6759 -- If the action derives from stuff inside a record, then the actions
6760 -- are attached to the current scope, to be inserted and analyzed on
6761 -- exit from the scope. The reason for this is that we may also be
6762 -- generating freeze actions at the same time, and they must eventually
6763 -- be elaborated in the correct order.
6765 if Is_Record_Type (Current_Scope)
6766 and then not Is_Frozen (Current_Scope)
6767 then
6768 if No (Scope_Stack.Table
6769 (Scope_Stack.Last).Pending_Freeze_Actions)
6770 then
6771 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6772 Ins_Actions;
6773 else
6774 Append_List
6775 (Ins_Actions,
6776 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6777 end if;
6779 return;
6780 end if;
6782 -- We now intend to climb up the tree to find the right point to
6783 -- insert the actions. We start at Assoc_Node, unless this node is a
6784 -- subexpression in which case we start with its parent. We do this for
6785 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6786 -- itself one of the special nodes like N_And_Then, then we assume that
6787 -- an initial request to insert actions for such a node does not expect
6788 -- the actions to get deposited in the node for later handling when the
6789 -- node is expanded, since clearly the node is being dealt with by the
6790 -- caller. Note that in the subexpression case, N is always the child we
6791 -- came from.
6793 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6794 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6795 -- Procedure calls, and similarly procedure attribute references, are
6796 -- also statements.
6798 if Nkind (Assoc_Node) in N_Subexpr
6799 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6800 or else Etype (Assoc_Node) /= Standard_Void_Type)
6801 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6802 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6803 or else not Is_Procedure_Attribute_Name
6804 (Attribute_Name (Assoc_Node)))
6805 then
6806 N := Assoc_Node;
6807 P := Parent (Assoc_Node);
6809 -- Non-subexpression case. Note that N is initially Empty in this case
6810 -- (N is only guaranteed Non-Empty in the subexpr case).
6812 else
6813 N := Empty;
6814 P := Assoc_Node;
6815 end if;
6817 -- Capture root of the transient scope
6819 if Scope_Is_Transient then
6820 Wrapped_Node := Node_To_Be_Wrapped;
6821 end if;
6823 loop
6824 pragma Assert (Present (P));
6826 -- Make sure that inserted actions stay in the transient scope
6828 if Present (Wrapped_Node) and then N = Wrapped_Node then
6829 Store_Before_Actions_In_Scope (Ins_Actions);
6830 return;
6831 end if;
6833 case Nkind (P) is
6835 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6836 -- in the Actions field of the right operand. They will be moved
6837 -- out further when the AND THEN or OR ELSE operator is expanded.
6838 -- Nothing special needs to be done for the left operand since
6839 -- in that case the actions are executed unconditionally.
6841 when N_Short_Circuit =>
6842 if N = Right_Opnd (P) then
6844 -- We are now going to either append the actions to the
6845 -- actions field of the short-circuit operation. We will
6846 -- also analyze the actions now.
6848 -- This analysis is really too early, the proper thing would
6849 -- be to just park them there now, and only analyze them if
6850 -- we find we really need them, and to it at the proper
6851 -- final insertion point. However attempting to this proved
6852 -- tricky, so for now we just kill current values before and
6853 -- after the analyze call to make sure we avoid peculiar
6854 -- optimizations from this out of order insertion.
6856 Kill_Current_Values;
6858 -- If P has already been expanded, we can't park new actions
6859 -- on it, so we need to expand them immediately, introducing
6860 -- an Expression_With_Actions. N can't be an expression
6861 -- with actions, or else then the actions would have been
6862 -- inserted at an inner level.
6864 if Analyzed (P) then
6865 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6866 Rewrite (N,
6867 Make_Expression_With_Actions (Sloc (N),
6868 Actions => Ins_Actions,
6869 Expression => Relocate_Node (N)));
6870 Analyze_And_Resolve (N);
6872 elsif Present (Actions (P)) then
6873 Insert_List_After_And_Analyze
6874 (Last (Actions (P)), Ins_Actions);
6875 else
6876 Set_Actions (P, Ins_Actions);
6877 Analyze_List (Actions (P));
6878 end if;
6880 Kill_Current_Values;
6882 return;
6883 end if;
6885 -- Then or Else dependent expression of an if expression. Add
6886 -- actions to Then_Actions or Else_Actions field as appropriate.
6887 -- The actions will be moved further out when the if is expanded.
6889 when N_If_Expression =>
6890 declare
6891 ThenX : constant Node_Id := Next (First (Expressions (P)));
6892 ElseX : constant Node_Id := Next (ThenX);
6894 begin
6895 -- If the enclosing expression is already analyzed, as
6896 -- is the case for nested elaboration checks, insert the
6897 -- conditional further out.
6899 if Analyzed (P) then
6900 null;
6902 -- Actions belong to the then expression, temporarily place
6903 -- them as Then_Actions of the if expression. They will be
6904 -- moved to the proper place later when the if expression
6905 -- is expanded.
6907 elsif N = ThenX then
6908 if Present (Then_Actions (P)) then
6909 Insert_List_After_And_Analyze
6910 (Last (Then_Actions (P)), Ins_Actions);
6911 else
6912 Set_Then_Actions (P, Ins_Actions);
6913 Analyze_List (Then_Actions (P));
6914 end if;
6916 return;
6918 -- Actions belong to the else expression, temporarily place
6919 -- them as Else_Actions of the if expression. They will be
6920 -- moved to the proper place later when the if expression
6921 -- is expanded.
6923 elsif N = ElseX then
6924 if Present (Else_Actions (P)) then
6925 Insert_List_After_And_Analyze
6926 (Last (Else_Actions (P)), Ins_Actions);
6927 else
6928 Set_Else_Actions (P, Ins_Actions);
6929 Analyze_List (Else_Actions (P));
6930 end if;
6932 return;
6934 -- Actions belong to the condition. In this case they are
6935 -- unconditionally executed, and so we can continue the
6936 -- search for the proper insert point.
6938 else
6939 null;
6940 end if;
6941 end;
6943 -- Alternative of case expression, we place the action in the
6944 -- Actions field of the case expression alternative, this will
6945 -- be handled when the case expression is expanded.
6947 when N_Case_Expression_Alternative =>
6948 if Present (Actions (P)) then
6949 Insert_List_After_And_Analyze
6950 (Last (Actions (P)), Ins_Actions);
6951 else
6952 Set_Actions (P, Ins_Actions);
6953 Analyze_List (Actions (P));
6954 end if;
6956 return;
6958 -- Case of appearing within an Expressions_With_Actions node. When
6959 -- the new actions come from the expression of the expression with
6960 -- actions, they must be added to the existing actions. The other
6961 -- alternative is when the new actions are related to one of the
6962 -- existing actions of the expression with actions, and should
6963 -- never reach here: if actions are inserted on a statement
6964 -- within the Actions of an expression with actions, or on some
6965 -- subexpression of such a statement, then the outermost proper
6966 -- insertion point is right before the statement, and we should
6967 -- never climb up as far as the N_Expression_With_Actions itself.
6969 when N_Expression_With_Actions =>
6970 if N = Expression (P) then
6971 if Is_Empty_List (Actions (P)) then
6972 Append_List_To (Actions (P), Ins_Actions);
6973 Analyze_List (Actions (P));
6974 else
6975 Insert_List_After_And_Analyze
6976 (Last (Actions (P)), Ins_Actions);
6977 end if;
6979 return;
6981 else
6982 raise Program_Error;
6983 end if;
6985 -- Case of appearing in the condition of a while expression or
6986 -- elsif. We insert the actions into the Condition_Actions field.
6987 -- They will be moved further out when the while loop or elsif
6988 -- is analyzed.
6990 when N_Elsif_Part
6991 | N_Iteration_Scheme
6993 if N = Condition (P) then
6994 if Present (Condition_Actions (P)) then
6995 Insert_List_After_And_Analyze
6996 (Last (Condition_Actions (P)), Ins_Actions);
6997 else
6998 Set_Condition_Actions (P, Ins_Actions);
7000 -- Set the parent of the insert actions explicitly. This
7001 -- is not a syntactic field, but we need the parent field
7002 -- set, in particular so that freeze can understand that
7003 -- it is dealing with condition actions, and properly
7004 -- insert the freezing actions.
7006 Set_Parent (Ins_Actions, P);
7007 Analyze_List (Condition_Actions (P));
7008 end if;
7010 return;
7011 end if;
7013 -- Statements, declarations, pragmas, representation clauses
7015 when
7016 -- Statements
7018 N_Procedure_Call_Statement
7019 | N_Statement_Other_Than_Procedure_Call
7021 -- Pragmas
7023 | N_Pragma
7025 -- Representation_Clause
7027 | N_At_Clause
7028 | N_Attribute_Definition_Clause
7029 | N_Enumeration_Representation_Clause
7030 | N_Record_Representation_Clause
7032 -- Declarations
7034 | N_Abstract_Subprogram_Declaration
7035 | N_Entry_Body
7036 | N_Exception_Declaration
7037 | N_Exception_Renaming_Declaration
7038 | N_Expression_Function
7039 | N_Formal_Abstract_Subprogram_Declaration
7040 | N_Formal_Concrete_Subprogram_Declaration
7041 | N_Formal_Object_Declaration
7042 | N_Formal_Type_Declaration
7043 | N_Full_Type_Declaration
7044 | N_Function_Instantiation
7045 | N_Generic_Function_Renaming_Declaration
7046 | N_Generic_Package_Declaration
7047 | N_Generic_Package_Renaming_Declaration
7048 | N_Generic_Procedure_Renaming_Declaration
7049 | N_Generic_Subprogram_Declaration
7050 | N_Implicit_Label_Declaration
7051 | N_Incomplete_Type_Declaration
7052 | N_Number_Declaration
7053 | N_Object_Declaration
7054 | N_Object_Renaming_Declaration
7055 | N_Package_Body
7056 | N_Package_Body_Stub
7057 | N_Package_Declaration
7058 | N_Package_Instantiation
7059 | N_Package_Renaming_Declaration
7060 | N_Private_Extension_Declaration
7061 | N_Private_Type_Declaration
7062 | N_Procedure_Instantiation
7063 | N_Protected_Body
7064 | N_Protected_Body_Stub
7065 | N_Protected_Type_Declaration
7066 | N_Single_Task_Declaration
7067 | N_Subprogram_Body
7068 | N_Subprogram_Body_Stub
7069 | N_Subprogram_Declaration
7070 | N_Subprogram_Renaming_Declaration
7071 | N_Subtype_Declaration
7072 | N_Task_Body
7073 | N_Task_Body_Stub
7074 | N_Task_Type_Declaration
7076 -- Use clauses can appear in lists of declarations
7078 | N_Use_Package_Clause
7079 | N_Use_Type_Clause
7081 -- Freeze entity behaves like a declaration or statement
7083 | N_Freeze_Entity
7084 | N_Freeze_Generic_Entity
7086 -- Do not insert here if the item is not a list member (this
7087 -- happens for example with a triggering statement, and the
7088 -- proper approach is to insert before the entire select).
7090 if not Is_List_Member (P) then
7091 null;
7093 -- Do not insert if parent of P is an N_Component_Association
7094 -- node (i.e. we are in the context of an N_Aggregate or
7095 -- N_Extension_Aggregate node. In this case we want to insert
7096 -- before the entire aggregate.
7098 elsif Nkind (Parent (P)) = N_Component_Association then
7099 null;
7101 -- Do not insert if the parent of P is either an N_Variant node
7102 -- or an N_Record_Definition node, meaning in either case that
7103 -- P is a member of a component list, and that therefore the
7104 -- actions should be inserted outside the complete record
7105 -- declaration.
7107 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7108 null;
7110 -- Do not insert freeze nodes within the loop generated for
7111 -- an aggregate, because they may be elaborated too late for
7112 -- subsequent use in the back end: within a package spec the
7113 -- loop is part of the elaboration procedure and is only
7114 -- elaborated during the second pass.
7116 -- If the loop comes from source, or the entity is local to the
7117 -- loop itself it must remain within.
7119 elsif Nkind (Parent (P)) = N_Loop_Statement
7120 and then not Comes_From_Source (Parent (P))
7121 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7122 and then
7123 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7124 then
7125 null;
7127 -- Otherwise we can go ahead and do the insertion
7129 elsif P = Wrapped_Node then
7130 Store_Before_Actions_In_Scope (Ins_Actions);
7131 return;
7133 else
7134 Insert_List_Before_And_Analyze (P, Ins_Actions);
7135 return;
7136 end if;
7138 -- A special case, N_Raise_xxx_Error can act either as a statement
7139 -- or a subexpression. We tell the difference by looking at the
7140 -- Etype. It is set to Standard_Void_Type in the statement case.
7142 when N_Raise_xxx_Error =>
7143 if Etype (P) = Standard_Void_Type then
7144 if P = Wrapped_Node then
7145 Store_Before_Actions_In_Scope (Ins_Actions);
7146 else
7147 Insert_List_Before_And_Analyze (P, Ins_Actions);
7148 end if;
7150 return;
7152 -- In the subexpression case, keep climbing
7154 else
7155 null;
7156 end if;
7158 -- If a component association appears within a loop created for
7159 -- an array aggregate, attach the actions to the association so
7160 -- they can be subsequently inserted within the loop. For other
7161 -- component associations insert outside of the aggregate. For
7162 -- an association that will generate a loop, its Loop_Actions
7163 -- attribute is already initialized (see exp_aggr.adb).
7165 -- The list of Loop_Actions can in turn generate additional ones,
7166 -- that are inserted before the associated node. If the associated
7167 -- node is outside the aggregate, the new actions are collected
7168 -- at the end of the Loop_Actions, to respect the order in which
7169 -- they are to be elaborated.
7171 when N_Component_Association
7172 | N_Iterated_Component_Association
7174 if Nkind (Parent (P)) = N_Aggregate
7175 and then Present (Loop_Actions (P))
7176 then
7177 if Is_Empty_List (Loop_Actions (P)) then
7178 Set_Loop_Actions (P, Ins_Actions);
7179 Analyze_List (Ins_Actions);
7180 else
7181 declare
7182 Decl : Node_Id;
7184 begin
7185 -- Check whether these actions were generated by a
7186 -- declaration that is part of the Loop_Actions for
7187 -- the component_association.
7189 Decl := Assoc_Node;
7190 while Present (Decl) loop
7191 exit when Parent (Decl) = P
7192 and then Is_List_Member (Decl)
7193 and then
7194 List_Containing (Decl) = Loop_Actions (P);
7195 Decl := Parent (Decl);
7196 end loop;
7198 if Present (Decl) then
7199 Insert_List_Before_And_Analyze
7200 (Decl, Ins_Actions);
7201 else
7202 Insert_List_After_And_Analyze
7203 (Last (Loop_Actions (P)), Ins_Actions);
7204 end if;
7205 end;
7206 end if;
7208 return;
7210 else
7211 null;
7212 end if;
7214 -- Special case: an attribute denoting a procedure call
7216 when N_Attribute_Reference =>
7217 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7218 if P = Wrapped_Node then
7219 Store_Before_Actions_In_Scope (Ins_Actions);
7220 else
7221 Insert_List_Before_And_Analyze (P, Ins_Actions);
7222 end if;
7224 return;
7226 -- In the subexpression case, keep climbing
7228 else
7229 null;
7230 end if;
7232 -- Special case: a marker
7234 when N_Call_Marker
7235 | N_Variable_Reference_Marker
7237 if Is_List_Member (P) then
7238 Insert_List_Before_And_Analyze (P, Ins_Actions);
7239 return;
7240 end if;
7242 -- A contract node should not belong to the tree
7244 when N_Contract =>
7245 raise Program_Error;
7247 -- For all other node types, keep climbing tree
7249 when N_Abortable_Part
7250 | N_Accept_Alternative
7251 | N_Access_Definition
7252 | N_Access_Function_Definition
7253 | N_Access_Procedure_Definition
7254 | N_Access_To_Object_Definition
7255 | N_Aggregate
7256 | N_Allocator
7257 | N_Aspect_Specification
7258 | N_Case_Expression
7259 | N_Case_Statement_Alternative
7260 | N_Character_Literal
7261 | N_Compilation_Unit
7262 | N_Compilation_Unit_Aux
7263 | N_Component_Clause
7264 | N_Component_Declaration
7265 | N_Component_Definition
7266 | N_Component_List
7267 | N_Constrained_Array_Definition
7268 | N_Decimal_Fixed_Point_Definition
7269 | N_Defining_Character_Literal
7270 | N_Defining_Identifier
7271 | N_Defining_Operator_Symbol
7272 | N_Defining_Program_Unit_Name
7273 | N_Delay_Alternative
7274 | N_Delta_Aggregate
7275 | N_Delta_Constraint
7276 | N_Derived_Type_Definition
7277 | N_Designator
7278 | N_Digits_Constraint
7279 | N_Discriminant_Association
7280 | N_Discriminant_Specification
7281 | N_Empty
7282 | N_Entry_Body_Formal_Part
7283 | N_Entry_Call_Alternative
7284 | N_Entry_Declaration
7285 | N_Entry_Index_Specification
7286 | N_Enumeration_Type_Definition
7287 | N_Error
7288 | N_Exception_Handler
7289 | N_Expanded_Name
7290 | N_Explicit_Dereference
7291 | N_Extension_Aggregate
7292 | N_Floating_Point_Definition
7293 | N_Formal_Decimal_Fixed_Point_Definition
7294 | N_Formal_Derived_Type_Definition
7295 | N_Formal_Discrete_Type_Definition
7296 | N_Formal_Floating_Point_Definition
7297 | N_Formal_Modular_Type_Definition
7298 | N_Formal_Ordinary_Fixed_Point_Definition
7299 | N_Formal_Package_Declaration
7300 | N_Formal_Private_Type_Definition
7301 | N_Formal_Incomplete_Type_Definition
7302 | N_Formal_Signed_Integer_Type_Definition
7303 | N_Function_Call
7304 | N_Function_Specification
7305 | N_Generic_Association
7306 | N_Handled_Sequence_Of_Statements
7307 | N_Identifier
7308 | N_In
7309 | N_Index_Or_Discriminant_Constraint
7310 | N_Indexed_Component
7311 | N_Integer_Literal
7312 | N_Iterator_Specification
7313 | N_Itype_Reference
7314 | N_Label
7315 | N_Loop_Parameter_Specification
7316 | N_Mod_Clause
7317 | N_Modular_Type_Definition
7318 | N_Not_In
7319 | N_Null
7320 | N_Op_Abs
7321 | N_Op_Add
7322 | N_Op_And
7323 | N_Op_Concat
7324 | N_Op_Divide
7325 | N_Op_Eq
7326 | N_Op_Expon
7327 | N_Op_Ge
7328 | N_Op_Gt
7329 | N_Op_Le
7330 | N_Op_Lt
7331 | N_Op_Minus
7332 | N_Op_Mod
7333 | N_Op_Multiply
7334 | N_Op_Ne
7335 | N_Op_Not
7336 | N_Op_Or
7337 | N_Op_Plus
7338 | N_Op_Rem
7339 | N_Op_Rotate_Left
7340 | N_Op_Rotate_Right
7341 | N_Op_Shift_Left
7342 | N_Op_Shift_Right
7343 | N_Op_Shift_Right_Arithmetic
7344 | N_Op_Subtract
7345 | N_Op_Xor
7346 | N_Operator_Symbol
7347 | N_Ordinary_Fixed_Point_Definition
7348 | N_Others_Choice
7349 | N_Package_Specification
7350 | N_Parameter_Association
7351 | N_Parameter_Specification
7352 | N_Pop_Constraint_Error_Label
7353 | N_Pop_Program_Error_Label
7354 | N_Pop_Storage_Error_Label
7355 | N_Pragma_Argument_Association
7356 | N_Procedure_Specification
7357 | N_Protected_Definition
7358 | N_Push_Constraint_Error_Label
7359 | N_Push_Program_Error_Label
7360 | N_Push_Storage_Error_Label
7361 | N_Qualified_Expression
7362 | N_Quantified_Expression
7363 | N_Raise_Expression
7364 | N_Range
7365 | N_Range_Constraint
7366 | N_Real_Literal
7367 | N_Real_Range_Specification
7368 | N_Record_Definition
7369 | N_Reference
7370 | N_SCIL_Dispatch_Table_Tag_Init
7371 | N_SCIL_Dispatching_Call
7372 | N_SCIL_Membership_Test
7373 | N_Selected_Component
7374 | N_Signed_Integer_Type_Definition
7375 | N_Single_Protected_Declaration
7376 | N_Slice
7377 | N_String_Literal
7378 | N_Subtype_Indication
7379 | N_Subunit
7380 | N_Target_Name
7381 | N_Task_Definition
7382 | N_Terminate_Alternative
7383 | N_Triggering_Alternative
7384 | N_Type_Conversion
7385 | N_Unchecked_Expression
7386 | N_Unchecked_Type_Conversion
7387 | N_Unconstrained_Array_Definition
7388 | N_Unused_At_End
7389 | N_Unused_At_Start
7390 | N_Variant
7391 | N_Variant_Part
7392 | N_Validate_Unchecked_Conversion
7393 | N_With_Clause
7395 null;
7396 end case;
7398 -- If we fall through above tests, keep climbing tree
7400 N := P;
7402 if Nkind (Parent (N)) = N_Subunit then
7404 -- This is the proper body corresponding to a stub. Insertion must
7405 -- be done at the point of the stub, which is in the declarative
7406 -- part of the parent unit.
7408 P := Corresponding_Stub (Parent (N));
7410 else
7411 P := Parent (N);
7412 end if;
7413 end loop;
7414 end Insert_Actions;
7416 -- Version with check(s) suppressed
7418 procedure Insert_Actions
7419 (Assoc_Node : Node_Id;
7420 Ins_Actions : List_Id;
7421 Suppress : Check_Id)
7423 begin
7424 if Suppress = All_Checks then
7425 declare
7426 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7427 begin
7428 Scope_Suppress.Suppress := (others => True);
7429 Insert_Actions (Assoc_Node, Ins_Actions);
7430 Scope_Suppress.Suppress := Sva;
7431 end;
7433 else
7434 declare
7435 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7436 begin
7437 Scope_Suppress.Suppress (Suppress) := True;
7438 Insert_Actions (Assoc_Node, Ins_Actions);
7439 Scope_Suppress.Suppress (Suppress) := Svg;
7440 end;
7441 end if;
7442 end Insert_Actions;
7444 --------------------------
7445 -- Insert_Actions_After --
7446 --------------------------
7448 procedure Insert_Actions_After
7449 (Assoc_Node : Node_Id;
7450 Ins_Actions : List_Id)
7452 begin
7453 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7454 Store_After_Actions_In_Scope (Ins_Actions);
7455 else
7456 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7457 end if;
7458 end Insert_Actions_After;
7460 ------------------------
7461 -- Insert_Declaration --
7462 ------------------------
7464 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7465 P : Node_Id;
7467 begin
7468 pragma Assert (Nkind (N) in N_Subexpr);
7470 -- Climb until we find a procedure or a package
7472 P := N;
7473 loop
7474 pragma Assert (Present (Parent (P)));
7475 P := Parent (P);
7477 if Is_List_Member (P) then
7478 exit when Nkind_In (Parent (P), N_Package_Specification,
7479 N_Subprogram_Body);
7481 -- Special handling for handled sequence of statements, we must
7482 -- insert in the statements not the exception handlers!
7484 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7485 P := First (Statements (Parent (P)));
7486 exit;
7487 end if;
7488 end if;
7489 end loop;
7491 -- Now do the insertion
7493 Insert_Before (P, Decl);
7494 Analyze (Decl);
7495 end Insert_Declaration;
7497 ---------------------------------
7498 -- Insert_Library_Level_Action --
7499 ---------------------------------
7501 procedure Insert_Library_Level_Action (N : Node_Id) is
7502 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7504 begin
7505 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7506 -- And not Main_Unit as previously. If the main unit is a body,
7507 -- the scope needed to analyze the actions is the entity of the
7508 -- corresponding declaration.
7510 if No (Actions (Aux)) then
7511 Set_Actions (Aux, New_List (N));
7512 else
7513 Append (N, Actions (Aux));
7514 end if;
7516 Analyze (N);
7517 Pop_Scope;
7518 end Insert_Library_Level_Action;
7520 ----------------------------------
7521 -- Insert_Library_Level_Actions --
7522 ----------------------------------
7524 procedure Insert_Library_Level_Actions (L : List_Id) is
7525 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7527 begin
7528 if Is_Non_Empty_List (L) then
7529 Push_Scope (Cunit_Entity (Main_Unit));
7530 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7532 if No (Actions (Aux)) then
7533 Set_Actions (Aux, L);
7534 Analyze_List (L);
7535 else
7536 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7537 end if;
7539 Pop_Scope;
7540 end if;
7541 end Insert_Library_Level_Actions;
7543 ----------------------
7544 -- Inside_Init_Proc --
7545 ----------------------
7547 function Inside_Init_Proc return Boolean is
7548 Proc : constant Entity_Id := Enclosing_Init_Proc;
7550 begin
7551 return Proc /= Empty;
7552 end Inside_Init_Proc;
7554 ----------------------------
7555 -- Is_All_Null_Statements --
7556 ----------------------------
7558 function Is_All_Null_Statements (L : List_Id) return Boolean is
7559 Stm : Node_Id;
7561 begin
7562 Stm := First (L);
7563 while Present (Stm) loop
7564 if Nkind (Stm) /= N_Null_Statement then
7565 return False;
7566 end if;
7568 Next (Stm);
7569 end loop;
7571 return True;
7572 end Is_All_Null_Statements;
7574 --------------------------------------------------
7575 -- Is_Displacement_Of_Object_Or_Function_Result --
7576 --------------------------------------------------
7578 function Is_Displacement_Of_Object_Or_Function_Result
7579 (Obj_Id : Entity_Id) return Boolean
7581 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7582 -- Determine whether node N denotes a controlled function call
7584 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
7585 -- Determine whether node N denotes a generalized indexing form which
7586 -- involves a controlled result.
7588 function Is_Displace_Call (N : Node_Id) return Boolean;
7589 -- Determine whether node N denotes a call to Ada.Tags.Displace
7591 function Is_Source_Object (N : Node_Id) return Boolean;
7592 -- Determine whether a particular node denotes a source object
7594 function Strip (N : Node_Id) return Node_Id;
7595 -- Examine arbitrary node N by stripping various indirections and return
7596 -- the "real" node.
7598 ---------------------------------
7599 -- Is_Controlled_Function_Call --
7600 ---------------------------------
7602 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7603 Expr : Node_Id;
7605 begin
7606 -- When a function call appears in Object.Operation format, the
7607 -- original representation has several possible forms depending on
7608 -- the availability and form of actual parameters:
7610 -- Obj.Func N_Selected_Component
7611 -- Obj.Func (Actual) N_Indexed_Component
7612 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7613 -- N_Selected_Component
7615 Expr := Original_Node (N);
7616 loop
7617 if Nkind (Expr) = N_Function_Call then
7618 Expr := Name (Expr);
7620 -- "Obj.Func (Actual)" case
7622 elsif Nkind (Expr) = N_Indexed_Component then
7623 Expr := Prefix (Expr);
7625 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7627 elsif Nkind (Expr) = N_Selected_Component then
7628 Expr := Selector_Name (Expr);
7630 else
7631 exit;
7632 end if;
7633 end loop;
7635 return
7636 Nkind (Expr) in N_Has_Entity
7637 and then Present (Entity (Expr))
7638 and then Ekind (Entity (Expr)) = E_Function
7639 and then Needs_Finalization (Etype (Entity (Expr)));
7640 end Is_Controlled_Function_Call;
7642 ----------------------------
7643 -- Is_Controlled_Indexing --
7644 ----------------------------
7646 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
7647 Expr : constant Node_Id := Original_Node (N);
7649 begin
7650 return
7651 Nkind (Expr) = N_Indexed_Component
7652 and then Present (Generalized_Indexing (Expr))
7653 and then Needs_Finalization (Etype (Expr));
7654 end Is_Controlled_Indexing;
7656 ----------------------
7657 -- Is_Displace_Call --
7658 ----------------------
7660 function Is_Displace_Call (N : Node_Id) return Boolean is
7661 Call : constant Node_Id := Strip (N);
7663 begin
7664 return
7665 Present (Call)
7666 and then Nkind (Call) = N_Function_Call
7667 and then Nkind (Name (Call)) in N_Has_Entity
7668 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7669 end Is_Displace_Call;
7671 ----------------------
7672 -- Is_Source_Object --
7673 ----------------------
7675 function Is_Source_Object (N : Node_Id) return Boolean is
7676 Obj : constant Node_Id := Strip (N);
7678 begin
7679 return
7680 Present (Obj)
7681 and then Comes_From_Source (Obj)
7682 and then Nkind (Obj) in N_Has_Entity
7683 and then Is_Object (Entity (Obj));
7684 end Is_Source_Object;
7686 -----------
7687 -- Strip --
7688 -----------
7690 function Strip (N : Node_Id) return Node_Id is
7691 Result : Node_Id;
7693 begin
7694 Result := N;
7695 loop
7696 if Nkind (Result) = N_Explicit_Dereference then
7697 Result := Prefix (Result);
7699 elsif Nkind_In (Result, N_Type_Conversion,
7700 N_Unchecked_Type_Conversion)
7701 then
7702 Result := Expression (Result);
7704 else
7705 exit;
7706 end if;
7707 end loop;
7709 return Result;
7710 end Strip;
7712 -- Local variables
7714 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
7715 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7716 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
7717 Orig_Expr : Node_Id;
7719 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7721 begin
7722 -- Case 1:
7724 -- Obj : CW_Type := Function_Call (...);
7726 -- is rewritten into:
7728 -- Temp : ... := Function_Call (...)'reference;
7729 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7731 -- where the return type of the function and the class-wide type require
7732 -- dispatch table pointer displacement.
7734 -- Case 2:
7736 -- Obj : CW_Type := Container (...);
7738 -- is rewritten into:
7740 -- Temp : ... := Function_Call (Container, ...)'reference;
7741 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7743 -- where the container element type and the class-wide type require
7744 -- dispatch table pointer dispacement.
7746 -- Case 3:
7748 -- Obj : CW_Type := Src_Obj;
7750 -- is rewritten into:
7752 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7754 -- where the type of the source object and the class-wide type require
7755 -- dispatch table pointer displacement.
7757 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
7758 and then Is_Class_Wide_Type (Obj_Typ)
7759 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7760 and then Nkind (Orig_Decl) = N_Object_Declaration
7761 and then Comes_From_Source (Orig_Decl)
7762 then
7763 Orig_Expr := Expression (Orig_Decl);
7765 return
7766 Is_Controlled_Function_Call (Orig_Expr)
7767 or else Is_Controlled_Indexing (Orig_Expr)
7768 or else Is_Source_Object (Orig_Expr);
7769 end if;
7771 return False;
7772 end Is_Displacement_Of_Object_Or_Function_Result;
7774 ------------------------------
7775 -- Is_Finalizable_Transient --
7776 ------------------------------
7778 function Is_Finalizable_Transient
7779 (Decl : Node_Id;
7780 Rel_Node : Node_Id) return Boolean
7782 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7783 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7785 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7786 -- Determine whether transient object Trans_Id is initialized either
7787 -- by a function call which returns an access type or simply renames
7788 -- another pointer.
7790 function Initialized_By_Aliased_BIP_Func_Call
7791 (Trans_Id : Entity_Id) return Boolean;
7792 -- Determine whether transient object Trans_Id is initialized by a
7793 -- build-in-place function call where the BIPalloc parameter is of
7794 -- value 1 and BIPaccess is not null. This case creates an aliasing
7795 -- between the returned value and the value denoted by BIPaccess.
7797 function Is_Aliased
7798 (Trans_Id : Entity_Id;
7799 First_Stmt : Node_Id) return Boolean;
7800 -- Determine whether transient object Trans_Id has been renamed or
7801 -- aliased through 'reference in the statement list starting from
7802 -- First_Stmt.
7804 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7805 -- Determine whether transient object Trans_Id is allocated on the heap
7807 function Is_Iterated_Container
7808 (Trans_Id : Entity_Id;
7809 First_Stmt : Node_Id) return Boolean;
7810 -- Determine whether transient object Trans_Id denotes a container which
7811 -- is in the process of being iterated in the statement list starting
7812 -- from First_Stmt.
7814 ---------------------------
7815 -- Initialized_By_Access --
7816 ---------------------------
7818 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7819 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7821 begin
7822 return
7823 Present (Expr)
7824 and then Nkind (Expr) /= N_Reference
7825 and then Is_Access_Type (Etype (Expr));
7826 end Initialized_By_Access;
7828 ------------------------------------------
7829 -- Initialized_By_Aliased_BIP_Func_Call --
7830 ------------------------------------------
7832 function Initialized_By_Aliased_BIP_Func_Call
7833 (Trans_Id : Entity_Id) return Boolean
7835 Call : Node_Id := Expression (Parent (Trans_Id));
7837 begin
7838 -- Build-in-place calls usually appear in 'reference format
7840 if Nkind (Call) = N_Reference then
7841 Call := Prefix (Call);
7842 end if;
7844 Call := Unqual_Conv (Call);
7846 if Is_Build_In_Place_Function_Call (Call) then
7847 declare
7848 Access_Nam : Name_Id := No_Name;
7849 Access_OK : Boolean := False;
7850 Actual : Node_Id;
7851 Alloc_Nam : Name_Id := No_Name;
7852 Alloc_OK : Boolean := False;
7853 Formal : Node_Id;
7854 Func_Id : Entity_Id;
7855 Param : Node_Id;
7857 begin
7858 -- Examine all parameter associations of the function call
7860 Param := First (Parameter_Associations (Call));
7861 while Present (Param) loop
7862 if Nkind (Param) = N_Parameter_Association
7863 and then Nkind (Selector_Name (Param)) = N_Identifier
7864 then
7865 Actual := Explicit_Actual_Parameter (Param);
7866 Formal := Selector_Name (Param);
7868 -- Construct the names of formals BIPaccess and BIPalloc
7869 -- using the function name retrieved from an arbitrary
7870 -- formal.
7872 if Access_Nam = No_Name
7873 and then Alloc_Nam = No_Name
7874 and then Present (Entity (Formal))
7875 then
7876 Func_Id := Scope (Entity (Formal));
7878 Access_Nam :=
7879 New_External_Name (Chars (Func_Id),
7880 BIP_Formal_Suffix (BIP_Object_Access));
7882 Alloc_Nam :=
7883 New_External_Name (Chars (Func_Id),
7884 BIP_Formal_Suffix (BIP_Alloc_Form));
7885 end if;
7887 -- A match for BIPaccess => Temp has been found
7889 if Chars (Formal) = Access_Nam
7890 and then Nkind (Actual) /= N_Null
7891 then
7892 Access_OK := True;
7893 end if;
7895 -- A match for BIPalloc => 1 has been found
7897 if Chars (Formal) = Alloc_Nam
7898 and then Nkind (Actual) = N_Integer_Literal
7899 and then Intval (Actual) = Uint_1
7900 then
7901 Alloc_OK := True;
7902 end if;
7903 end if;
7905 Next (Param);
7906 end loop;
7908 return Access_OK and Alloc_OK;
7909 end;
7910 end if;
7912 return False;
7913 end Initialized_By_Aliased_BIP_Func_Call;
7915 ----------------
7916 -- Is_Aliased --
7917 ----------------
7919 function Is_Aliased
7920 (Trans_Id : Entity_Id;
7921 First_Stmt : Node_Id) return Boolean
7923 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7924 -- Given an object renaming declaration, retrieve the entity of the
7925 -- renamed name. Return Empty if the renamed name is anything other
7926 -- than a variable or a constant.
7928 -------------------------
7929 -- Find_Renamed_Object --
7930 -------------------------
7932 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7933 Ren_Obj : Node_Id := Empty;
7935 function Find_Object (N : Node_Id) return Traverse_Result;
7936 -- Try to detect an object which is either a constant or a
7937 -- variable.
7939 -----------------
7940 -- Find_Object --
7941 -----------------
7943 function Find_Object (N : Node_Id) return Traverse_Result is
7944 begin
7945 -- Stop the search once a constant or a variable has been
7946 -- detected.
7948 if Nkind (N) = N_Identifier
7949 and then Present (Entity (N))
7950 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7951 then
7952 Ren_Obj := Entity (N);
7953 return Abandon;
7954 end if;
7956 return OK;
7957 end Find_Object;
7959 procedure Search is new Traverse_Proc (Find_Object);
7961 -- Local variables
7963 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7965 -- Start of processing for Find_Renamed_Object
7967 begin
7968 -- Actions related to dispatching calls may appear as renamings of
7969 -- tags. Do not process this type of renaming because it does not
7970 -- use the actual value of the object.
7972 if not Is_RTE (Typ, RE_Tag_Ptr) then
7973 Search (Name (Ren_Decl));
7974 end if;
7976 return Ren_Obj;
7977 end Find_Renamed_Object;
7979 -- Local variables
7981 Expr : Node_Id;
7982 Ren_Obj : Entity_Id;
7983 Stmt : Node_Id;
7985 -- Start of processing for Is_Aliased
7987 begin
7988 -- A controlled transient object is not considered aliased when it
7989 -- appears inside an expression_with_actions node even when there are
7990 -- explicit aliases of it:
7992 -- do
7993 -- Trans_Id : Ctrl_Typ ...; -- transient object
7994 -- Alias : ... := Trans_Id; -- object is aliased
7995 -- Val : constant Boolean :=
7996 -- ... Alias ...; -- aliasing ends
7997 -- <finalize Trans_Id> -- object safe to finalize
7998 -- in Val end;
8000 -- Expansion ensures that all aliases are encapsulated in the actions
8001 -- list and do not leak to the expression by forcing the evaluation
8002 -- of the expression.
8004 if Nkind (Rel_Node) = N_Expression_With_Actions then
8005 return False;
8007 -- Otherwise examine the statements after the controlled transient
8008 -- object and look for various forms of aliasing.
8010 else
8011 Stmt := First_Stmt;
8012 while Present (Stmt) loop
8013 if Nkind (Stmt) = N_Object_Declaration then
8014 Expr := Expression (Stmt);
8016 -- Aliasing of the form:
8017 -- Obj : ... := Trans_Id'reference;
8019 if Present (Expr)
8020 and then Nkind (Expr) = N_Reference
8021 and then Nkind (Prefix (Expr)) = N_Identifier
8022 and then Entity (Prefix (Expr)) = Trans_Id
8023 then
8024 return True;
8025 end if;
8027 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8028 Ren_Obj := Find_Renamed_Object (Stmt);
8030 -- Aliasing of the form:
8031 -- Obj : ... renames ... Trans_Id ...;
8033 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8034 return True;
8035 end if;
8036 end if;
8038 Next (Stmt);
8039 end loop;
8041 return False;
8042 end if;
8043 end Is_Aliased;
8045 ------------------
8046 -- Is_Allocated --
8047 ------------------
8049 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8050 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8051 begin
8052 return
8053 Is_Access_Type (Etype (Trans_Id))
8054 and then Present (Expr)
8055 and then Nkind (Expr) = N_Allocator;
8056 end Is_Allocated;
8058 ---------------------------
8059 -- Is_Iterated_Container --
8060 ---------------------------
8062 function Is_Iterated_Container
8063 (Trans_Id : Entity_Id;
8064 First_Stmt : Node_Id) return Boolean
8066 Aspect : Node_Id;
8067 Call : Node_Id;
8068 Iter : Entity_Id;
8069 Param : Node_Id;
8070 Stmt : Node_Id;
8071 Typ : Entity_Id;
8073 begin
8074 -- It is not possible to iterate over containers in non-Ada 2012 code
8076 if Ada_Version < Ada_2012 then
8077 return False;
8078 end if;
8080 Typ := Etype (Trans_Id);
8082 -- Handle access type created for secondary stack use
8084 if Is_Access_Type (Typ) then
8085 Typ := Designated_Type (Typ);
8086 end if;
8088 -- Look for aspect Default_Iterator. It may be part of a type
8089 -- declaration for a container, or inherited from a base type
8090 -- or parent type.
8092 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8094 if Present (Aspect) then
8095 Iter := Entity (Aspect);
8097 -- Examine the statements following the container object and
8098 -- look for a call to the default iterate routine where the
8099 -- first parameter is the transient. Such a call appears as:
8101 -- It : Access_To_CW_Iterator :=
8102 -- Iterate (Tran_Id.all, ...)'reference;
8104 Stmt := First_Stmt;
8105 while Present (Stmt) loop
8107 -- Detect an object declaration which is initialized by a
8108 -- secondary stack function call.
8110 if Nkind (Stmt) = N_Object_Declaration
8111 and then Present (Expression (Stmt))
8112 and then Nkind (Expression (Stmt)) = N_Reference
8113 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8114 then
8115 Call := Prefix (Expression (Stmt));
8117 -- The call must invoke the default iterate routine of
8118 -- the container and the transient object must appear as
8119 -- the first actual parameter. Skip any calls whose names
8120 -- are not entities.
8122 if Is_Entity_Name (Name (Call))
8123 and then Entity (Name (Call)) = Iter
8124 and then Present (Parameter_Associations (Call))
8125 then
8126 Param := First (Parameter_Associations (Call));
8128 if Nkind (Param) = N_Explicit_Dereference
8129 and then Entity (Prefix (Param)) = Trans_Id
8130 then
8131 return True;
8132 end if;
8133 end if;
8134 end if;
8136 Next (Stmt);
8137 end loop;
8138 end if;
8140 return False;
8141 end Is_Iterated_Container;
8143 -- Local variables
8145 Desig : Entity_Id := Obj_Typ;
8147 -- Start of processing for Is_Finalizable_Transient
8149 begin
8150 -- Handle access types
8152 if Is_Access_Type (Desig) then
8153 Desig := Available_View (Designated_Type (Desig));
8154 end if;
8156 return
8157 Ekind_In (Obj_Id, E_Constant, E_Variable)
8158 and then Needs_Finalization (Desig)
8159 and then Requires_Transient_Scope (Desig)
8160 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8162 -- Do not consider a transient object that was already processed
8164 and then not Is_Finalized_Transient (Obj_Id)
8166 -- Do not consider renamed or 'reference-d transient objects because
8167 -- the act of renaming extends the object's lifetime.
8169 and then not Is_Aliased (Obj_Id, Decl)
8171 -- Do not consider transient objects allocated on the heap since
8172 -- they are attached to a finalization master.
8174 and then not Is_Allocated (Obj_Id)
8176 -- If the transient object is a pointer, check that it is not
8177 -- initialized by a function that returns a pointer or acts as a
8178 -- renaming of another pointer.
8180 and then
8181 (not Is_Access_Type (Obj_Typ)
8182 or else not Initialized_By_Access (Obj_Id))
8184 -- Do not consider transient objects which act as indirect aliases
8185 -- of build-in-place function results.
8187 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8189 -- Do not consider conversions of tags to class-wide types
8191 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8193 -- Do not consider iterators because those are treated as normal
8194 -- controlled objects and are processed by the usual finalization
8195 -- machinery. This avoids the double finalization of an iterator.
8197 and then not Is_Iterator (Desig)
8199 -- Do not consider containers in the context of iterator loops. Such
8200 -- transient objects must exist for as long as the loop is around,
8201 -- otherwise any operation carried out by the iterator will fail.
8203 and then not Is_Iterated_Container (Obj_Id, Decl);
8204 end Is_Finalizable_Transient;
8206 ---------------------------------
8207 -- Is_Fully_Repped_Tagged_Type --
8208 ---------------------------------
8210 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8211 U : constant Entity_Id := Underlying_Type (T);
8212 Comp : Entity_Id;
8214 begin
8215 if No (U) or else not Is_Tagged_Type (U) then
8216 return False;
8217 elsif Has_Discriminants (U) then
8218 return False;
8219 elsif not Has_Specified_Layout (U) then
8220 return False;
8221 end if;
8223 -- Here we have a tagged type, see if it has any unlayed out fields
8224 -- other than a possible tag and parent fields. If so, we return False.
8226 Comp := First_Component (U);
8227 while Present (Comp) loop
8228 if not Is_Tag (Comp)
8229 and then Chars (Comp) /= Name_uParent
8230 and then No (Component_Clause (Comp))
8231 then
8232 return False;
8233 else
8234 Next_Component (Comp);
8235 end if;
8236 end loop;
8238 -- All components are layed out
8240 return True;
8241 end Is_Fully_Repped_Tagged_Type;
8243 ----------------------------------
8244 -- Is_Library_Level_Tagged_Type --
8245 ----------------------------------
8247 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8248 begin
8249 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8250 end Is_Library_Level_Tagged_Type;
8252 --------------------------
8253 -- Is_Non_BIP_Func_Call --
8254 --------------------------
8256 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8257 begin
8258 -- The expected call is of the format
8260 -- Func_Call'reference
8262 return
8263 Nkind (Expr) = N_Reference
8264 and then Nkind (Prefix (Expr)) = N_Function_Call
8265 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8266 end Is_Non_BIP_Func_Call;
8268 ----------------------------------
8269 -- Is_Possibly_Unaligned_Object --
8270 ----------------------------------
8272 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8273 T : constant Entity_Id := Etype (N);
8275 begin
8276 -- If renamed object, apply test to underlying object
8278 if Is_Entity_Name (N)
8279 and then Is_Object (Entity (N))
8280 and then Present (Renamed_Object (Entity (N)))
8281 then
8282 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8283 end if;
8285 -- Tagged and controlled types and aliased types are always aligned, as
8286 -- are concurrent types.
8288 if Is_Aliased (T)
8289 or else Has_Controlled_Component (T)
8290 or else Is_Concurrent_Type (T)
8291 or else Is_Tagged_Type (T)
8292 or else Is_Controlled (T)
8293 then
8294 return False;
8295 end if;
8297 -- If this is an element of a packed array, may be unaligned
8299 if Is_Ref_To_Bit_Packed_Array (N) then
8300 return True;
8301 end if;
8303 -- Case of indexed component reference: test whether prefix is unaligned
8305 if Nkind (N) = N_Indexed_Component then
8306 return Is_Possibly_Unaligned_Object (Prefix (N));
8308 -- Case of selected component reference
8310 elsif Nkind (N) = N_Selected_Component then
8311 declare
8312 P : constant Node_Id := Prefix (N);
8313 C : constant Entity_Id := Entity (Selector_Name (N));
8314 M : Nat;
8315 S : Nat;
8317 begin
8318 -- If component reference is for an array with non-static bounds,
8319 -- then it is always aligned: we can only process unaligned arrays
8320 -- with static bounds (more precisely compile time known bounds).
8322 if Is_Array_Type (T)
8323 and then not Compile_Time_Known_Bounds (T)
8324 then
8325 return False;
8326 end if;
8328 -- If component is aliased, it is definitely properly aligned
8330 if Is_Aliased (C) then
8331 return False;
8332 end if;
8334 -- If component is for a type implemented as a scalar, and the
8335 -- record is packed, and the component is other than the first
8336 -- component of the record, then the component may be unaligned.
8338 if Is_Packed (Etype (P))
8339 and then Represented_As_Scalar (Etype (C))
8340 and then First_Entity (Scope (C)) /= C
8341 then
8342 return True;
8343 end if;
8345 -- Compute maximum possible alignment for T
8347 -- If alignment is known, then that settles things
8349 if Known_Alignment (T) then
8350 M := UI_To_Int (Alignment (T));
8352 -- If alignment is not known, tentatively set max alignment
8354 else
8355 M := Ttypes.Maximum_Alignment;
8357 -- We can reduce this if the Esize is known since the default
8358 -- alignment will never be more than the smallest power of 2
8359 -- that does not exceed this Esize value.
8361 if Known_Esize (T) then
8362 S := UI_To_Int (Esize (T));
8364 while (M / 2) >= S loop
8365 M := M / 2;
8366 end loop;
8367 end if;
8368 end if;
8370 -- The following code is historical, it used to be present but it
8371 -- is too cautious, because the front-end does not know the proper
8372 -- default alignments for the target. Also, if the alignment is
8373 -- not known, the front end can't know in any case. If a copy is
8374 -- needed, the back-end will take care of it. This whole section
8375 -- including this comment can be removed later ???
8377 -- If the component reference is for a record that has a specified
8378 -- alignment, and we either know it is too small, or cannot tell,
8379 -- then the component may be unaligned.
8381 -- What is the following commented out code ???
8383 -- if Known_Alignment (Etype (P))
8384 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8385 -- and then M > Alignment (Etype (P))
8386 -- then
8387 -- return True;
8388 -- end if;
8390 -- Case of component clause present which may specify an
8391 -- unaligned position.
8393 if Present (Component_Clause (C)) then
8395 -- Otherwise we can do a test to make sure that the actual
8396 -- start position in the record, and the length, are both
8397 -- consistent with the required alignment. If not, we know
8398 -- that we are unaligned.
8400 declare
8401 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8402 Comp : Entity_Id;
8404 begin
8405 Comp := C;
8407 -- For a component inherited in a record extension, the
8408 -- clause is inherited but position and size are not set.
8410 if Is_Base_Type (Etype (P))
8411 and then Is_Tagged_Type (Etype (P))
8412 and then Present (Original_Record_Component (Comp))
8413 then
8414 Comp := Original_Record_Component (Comp);
8415 end if;
8417 if Component_Bit_Offset (Comp) mod Align_In_Bits /= 0
8418 or else Esize (Comp) mod Align_In_Bits /= 0
8419 then
8420 return True;
8421 end if;
8422 end;
8423 end if;
8425 -- Otherwise, for a component reference, test prefix
8427 return Is_Possibly_Unaligned_Object (P);
8428 end;
8430 -- If not a component reference, must be aligned
8432 else
8433 return False;
8434 end if;
8435 end Is_Possibly_Unaligned_Object;
8437 ---------------------------------
8438 -- Is_Possibly_Unaligned_Slice --
8439 ---------------------------------
8441 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8442 begin
8443 -- Go to renamed object
8445 if Is_Entity_Name (N)
8446 and then Is_Object (Entity (N))
8447 and then Present (Renamed_Object (Entity (N)))
8448 then
8449 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8450 end if;
8452 -- The reference must be a slice
8454 if Nkind (N) /= N_Slice then
8455 return False;
8456 end if;
8458 -- We only need to worry if the target has strict alignment
8460 if not Target_Strict_Alignment then
8461 return False;
8462 end if;
8464 -- If it is a slice, then look at the array type being sliced
8466 declare
8467 Sarr : constant Node_Id := Prefix (N);
8468 -- Prefix of the slice, i.e. the array being sliced
8470 Styp : constant Entity_Id := Etype (Prefix (N));
8471 -- Type of the array being sliced
8473 Pref : Node_Id;
8474 Ptyp : Entity_Id;
8476 begin
8477 -- The problems arise if the array object that is being sliced
8478 -- is a component of a record or array, and we cannot guarantee
8479 -- the alignment of the array within its containing object.
8481 -- To investigate this, we look at successive prefixes to see
8482 -- if we have a worrisome indexed or selected component.
8484 Pref := Sarr;
8485 loop
8486 -- Case of array is part of an indexed component reference
8488 if Nkind (Pref) = N_Indexed_Component then
8489 Ptyp := Etype (Prefix (Pref));
8491 -- The only problematic case is when the array is packed, in
8492 -- which case we really know nothing about the alignment of
8493 -- individual components.
8495 if Is_Bit_Packed_Array (Ptyp) then
8496 return True;
8497 end if;
8499 -- Case of array is part of a selected component reference
8501 elsif Nkind (Pref) = N_Selected_Component then
8502 Ptyp := Etype (Prefix (Pref));
8504 -- We are definitely in trouble if the record in question
8505 -- has an alignment, and either we know this alignment is
8506 -- inconsistent with the alignment of the slice, or we don't
8507 -- know what the alignment of the slice should be.
8509 if Known_Alignment (Ptyp)
8510 and then (Unknown_Alignment (Styp)
8511 or else Alignment (Styp) > Alignment (Ptyp))
8512 then
8513 return True;
8514 end if;
8516 -- We are in potential trouble if the record type is packed.
8517 -- We could special case when we know that the array is the
8518 -- first component, but that's not such a simple case ???
8520 if Is_Packed (Ptyp) then
8521 return True;
8522 end if;
8524 -- We are in trouble if there is a component clause, and
8525 -- either we do not know the alignment of the slice, or
8526 -- the alignment of the slice is inconsistent with the
8527 -- bit position specified by the component clause.
8529 declare
8530 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8531 begin
8532 if Present (Component_Clause (Field))
8533 and then
8534 (Unknown_Alignment (Styp)
8535 or else
8536 (Component_Bit_Offset (Field) mod
8537 (System_Storage_Unit * Alignment (Styp))) /= 0)
8538 then
8539 return True;
8540 end if;
8541 end;
8543 -- For cases other than selected or indexed components we know we
8544 -- are OK, since no issues arise over alignment.
8546 else
8547 return False;
8548 end if;
8550 -- We processed an indexed component or selected component
8551 -- reference that looked safe, so keep checking prefixes.
8553 Pref := Prefix (Pref);
8554 end loop;
8555 end;
8556 end Is_Possibly_Unaligned_Slice;
8558 -------------------------------
8559 -- Is_Related_To_Func_Return --
8560 -------------------------------
8562 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8563 Expr : constant Node_Id := Related_Expression (Id);
8564 begin
8565 return
8566 Present (Expr)
8567 and then Nkind (Expr) = N_Explicit_Dereference
8568 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8569 end Is_Related_To_Func_Return;
8571 --------------------------------
8572 -- Is_Ref_To_Bit_Packed_Array --
8573 --------------------------------
8575 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8576 Result : Boolean;
8577 Expr : Node_Id;
8579 begin
8580 if Is_Entity_Name (N)
8581 and then Is_Object (Entity (N))
8582 and then Present (Renamed_Object (Entity (N)))
8583 then
8584 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8585 end if;
8587 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8588 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8589 Result := True;
8590 else
8591 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8592 end if;
8594 if Result and then Nkind (N) = N_Indexed_Component then
8595 Expr := First (Expressions (N));
8596 while Present (Expr) loop
8597 Force_Evaluation (Expr);
8598 Next (Expr);
8599 end loop;
8600 end if;
8602 return Result;
8604 else
8605 return False;
8606 end if;
8607 end Is_Ref_To_Bit_Packed_Array;
8609 --------------------------------
8610 -- Is_Ref_To_Bit_Packed_Slice --
8611 --------------------------------
8613 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8614 begin
8615 if Nkind (N) = N_Type_Conversion then
8616 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8618 elsif Is_Entity_Name (N)
8619 and then Is_Object (Entity (N))
8620 and then Present (Renamed_Object (Entity (N)))
8621 then
8622 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8624 elsif Nkind (N) = N_Slice
8625 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8626 then
8627 return True;
8629 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8630 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8632 else
8633 return False;
8634 end if;
8635 end Is_Ref_To_Bit_Packed_Slice;
8637 -----------------------
8638 -- Is_Renamed_Object --
8639 -----------------------
8641 function Is_Renamed_Object (N : Node_Id) return Boolean is
8642 Pnod : constant Node_Id := Parent (N);
8643 Kind : constant Node_Kind := Nkind (Pnod);
8644 begin
8645 if Kind = N_Object_Renaming_Declaration then
8646 return True;
8647 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8648 return Is_Renamed_Object (Pnod);
8649 else
8650 return False;
8651 end if;
8652 end Is_Renamed_Object;
8654 --------------------------------------
8655 -- Is_Secondary_Stack_BIP_Func_Call --
8656 --------------------------------------
8658 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8659 Alloc_Nam : Name_Id := No_Name;
8660 Actual : Node_Id;
8661 Call : Node_Id := Expr;
8662 Formal : Node_Id;
8663 Param : Node_Id;
8665 begin
8666 -- Build-in-place calls usually appear in 'reference format. Note that
8667 -- the accessibility check machinery may add an extra 'reference due to
8668 -- side effect removal.
8670 while Nkind (Call) = N_Reference loop
8671 Call := Prefix (Call);
8672 end loop;
8674 Call := Unqual_Conv (Call);
8676 if Is_Build_In_Place_Function_Call (Call) then
8678 -- Examine all parameter associations of the function call
8680 Param := First (Parameter_Associations (Call));
8681 while Present (Param) loop
8682 if Nkind (Param) = N_Parameter_Association then
8683 Formal := Selector_Name (Param);
8684 Actual := Explicit_Actual_Parameter (Param);
8686 -- Construct the name of formal BIPalloc. It is much easier to
8687 -- extract the name of the function using an arbitrary formal's
8688 -- scope rather than the Name field of Call.
8690 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8691 Alloc_Nam :=
8692 New_External_Name
8693 (Chars (Scope (Entity (Formal))),
8694 BIP_Formal_Suffix (BIP_Alloc_Form));
8695 end if;
8697 -- A match for BIPalloc => 2 has been found
8699 if Chars (Formal) = Alloc_Nam
8700 and then Nkind (Actual) = N_Integer_Literal
8701 and then Intval (Actual) = Uint_2
8702 then
8703 return True;
8704 end if;
8705 end if;
8707 Next (Param);
8708 end loop;
8709 end if;
8711 return False;
8712 end Is_Secondary_Stack_BIP_Func_Call;
8714 -------------------------------------
8715 -- Is_Tag_To_Class_Wide_Conversion --
8716 -------------------------------------
8718 function Is_Tag_To_Class_Wide_Conversion
8719 (Obj_Id : Entity_Id) return Boolean
8721 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8723 begin
8724 return
8725 Is_Class_Wide_Type (Etype (Obj_Id))
8726 and then Present (Expr)
8727 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8728 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8729 end Is_Tag_To_Class_Wide_Conversion;
8731 ----------------------------
8732 -- Is_Untagged_Derivation --
8733 ----------------------------
8735 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8736 begin
8737 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8738 or else
8739 (Is_Private_Type (T) and then Present (Full_View (T))
8740 and then not Is_Tagged_Type (Full_View (T))
8741 and then Is_Derived_Type (Full_View (T))
8742 and then Etype (Full_View (T)) /= T);
8743 end Is_Untagged_Derivation;
8745 ------------------------------------
8746 -- Is_Untagged_Private_Derivation --
8747 ------------------------------------
8749 function Is_Untagged_Private_Derivation
8750 (Priv_Typ : Entity_Id;
8751 Full_Typ : Entity_Id) return Boolean
8753 begin
8754 return
8755 Present (Priv_Typ)
8756 and then Is_Untagged_Derivation (Priv_Typ)
8757 and then Is_Private_Type (Etype (Priv_Typ))
8758 and then Present (Full_Typ)
8759 and then Is_Itype (Full_Typ);
8760 end Is_Untagged_Private_Derivation;
8762 ------------------------------
8763 -- Is_Verifiable_DIC_Pragma --
8764 ------------------------------
8766 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
8767 Args : constant List_Id := Pragma_Argument_Associations (Prag);
8769 begin
8770 -- To qualify as verifiable, a DIC pragma must have a non-null argument
8772 return
8773 Present (Args)
8774 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
8775 end Is_Verifiable_DIC_Pragma;
8777 ---------------------------
8778 -- Is_Volatile_Reference --
8779 ---------------------------
8781 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8782 begin
8783 -- Only source references are to be treated as volatile, internally
8784 -- generated stuff cannot have volatile external effects.
8786 if not Comes_From_Source (N) then
8787 return False;
8789 -- Never true for reference to a type
8791 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8792 return False;
8794 -- Never true for a compile time known constant
8796 elsif Compile_Time_Known_Value (N) then
8797 return False;
8799 -- True if object reference with volatile type
8801 elsif Is_Volatile_Object (N) then
8802 return True;
8804 -- True if reference to volatile entity
8806 elsif Is_Entity_Name (N) then
8807 return Treat_As_Volatile (Entity (N));
8809 -- True for slice of volatile array
8811 elsif Nkind (N) = N_Slice then
8812 return Is_Volatile_Reference (Prefix (N));
8814 -- True if volatile component
8816 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8817 if (Is_Entity_Name (Prefix (N))
8818 and then Has_Volatile_Components (Entity (Prefix (N))))
8819 or else (Present (Etype (Prefix (N)))
8820 and then Has_Volatile_Components (Etype (Prefix (N))))
8821 then
8822 return True;
8823 else
8824 return Is_Volatile_Reference (Prefix (N));
8825 end if;
8827 -- Otherwise false
8829 else
8830 return False;
8831 end if;
8832 end Is_Volatile_Reference;
8834 --------------------
8835 -- Kill_Dead_Code --
8836 --------------------
8838 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8839 W : Boolean := Warn;
8840 -- Set False if warnings suppressed
8842 begin
8843 if Present (N) then
8844 Remove_Warning_Messages (N);
8846 -- Update the internal structures of the ABE mechanism in case the
8847 -- dead node is an elaboration scenario.
8849 Kill_Elaboration_Scenario (N);
8851 -- Generate warning if appropriate
8853 if W then
8855 -- We suppress the warning if this code is under control of an
8856 -- if statement, whose condition is a simple identifier, and
8857 -- either we are in an instance, or warnings off is set for this
8858 -- identifier. The reason for killing it in the instance case is
8859 -- that it is common and reasonable for code to be deleted in
8860 -- instances for various reasons.
8862 -- Could we use Is_Statically_Unevaluated here???
8864 if Nkind (Parent (N)) = N_If_Statement then
8865 declare
8866 C : constant Node_Id := Condition (Parent (N));
8867 begin
8868 if Nkind (C) = N_Identifier
8869 and then
8870 (In_Instance
8871 or else (Present (Entity (C))
8872 and then Has_Warnings_Off (Entity (C))))
8873 then
8874 W := False;
8875 end if;
8876 end;
8877 end if;
8879 -- Generate warning if not suppressed
8881 if W then
8882 Error_Msg_F
8883 ("?t?this code can never be executed and has been deleted!",
8885 end if;
8886 end if;
8888 -- Recurse into block statements and bodies to process declarations
8889 -- and statements.
8891 if Nkind (N) = N_Block_Statement
8892 or else Nkind (N) = N_Subprogram_Body
8893 or else Nkind (N) = N_Package_Body
8894 then
8895 Kill_Dead_Code (Declarations (N), False);
8896 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8898 if Nkind (N) = N_Subprogram_Body then
8899 Set_Is_Eliminated (Defining_Entity (N));
8900 end if;
8902 elsif Nkind (N) = N_Package_Declaration then
8903 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8904 Kill_Dead_Code (Private_Declarations (Specification (N)));
8906 -- ??? After this point, Delete_Tree has been called on all
8907 -- declarations in Specification (N), so references to entities
8908 -- therein look suspicious.
8910 declare
8911 E : Entity_Id := First_Entity (Defining_Entity (N));
8913 begin
8914 while Present (E) loop
8915 if Ekind (E) = E_Operator then
8916 Set_Is_Eliminated (E);
8917 end if;
8919 Next_Entity (E);
8920 end loop;
8921 end;
8923 -- Recurse into composite statement to kill individual statements in
8924 -- particular instantiations.
8926 elsif Nkind (N) = N_If_Statement then
8927 Kill_Dead_Code (Then_Statements (N));
8928 Kill_Dead_Code (Elsif_Parts (N));
8929 Kill_Dead_Code (Else_Statements (N));
8931 elsif Nkind (N) = N_Loop_Statement then
8932 Kill_Dead_Code (Statements (N));
8934 elsif Nkind (N) = N_Case_Statement then
8935 declare
8936 Alt : Node_Id;
8937 begin
8938 Alt := First (Alternatives (N));
8939 while Present (Alt) loop
8940 Kill_Dead_Code (Statements (Alt));
8941 Next (Alt);
8942 end loop;
8943 end;
8945 elsif Nkind (N) = N_Case_Statement_Alternative then
8946 Kill_Dead_Code (Statements (N));
8948 -- Deal with dead instances caused by deleting instantiations
8950 elsif Nkind (N) in N_Generic_Instantiation then
8951 Remove_Dead_Instance (N);
8952 end if;
8953 end if;
8954 end Kill_Dead_Code;
8956 -- Case where argument is a list of nodes to be killed
8958 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8959 N : Node_Id;
8960 W : Boolean;
8962 begin
8963 W := Warn;
8965 if Is_Non_Empty_List (L) then
8966 N := First (L);
8967 while Present (N) loop
8968 Kill_Dead_Code (N, W);
8969 W := False;
8970 Next (N);
8971 end loop;
8972 end if;
8973 end Kill_Dead_Code;
8975 ------------------------
8976 -- Known_Non_Negative --
8977 ------------------------
8979 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8980 begin
8981 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8982 return True;
8984 else
8985 declare
8986 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8987 begin
8988 return
8989 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8990 end;
8991 end if;
8992 end Known_Non_Negative;
8994 -----------------------------
8995 -- Make_CW_Equivalent_Type --
8996 -----------------------------
8998 -- Create a record type used as an equivalent of any member of the class
8999 -- which takes its size from exp.
9001 -- Generate the following code:
9003 -- type Equiv_T is record
9004 -- _parent : T (List of discriminant constraints taken from Exp);
9005 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
9006 -- end Equiv_T;
9008 -- ??? Note that this type does not guarantee same alignment as all
9009 -- derived types
9011 -- Note: for the freezing circuitry, this looks like a record extension,
9012 -- and so we need to make sure that the scalar storage order is the same
9013 -- as that of the parent type. (This does not change anything for the
9014 -- representation of the extension part.)
9016 function Make_CW_Equivalent_Type
9017 (T : Entity_Id;
9018 E : Node_Id) return Entity_Id
9020 Loc : constant Source_Ptr := Sloc (E);
9021 Root_Typ : constant Entity_Id := Root_Type (T);
9022 Root_Utyp : constant Entity_Id := Underlying_Type (Root_Typ);
9023 List_Def : constant List_Id := Empty_List;
9024 Comp_List : constant List_Id := New_List;
9025 Equiv_Type : Entity_Id;
9026 Range_Type : Entity_Id;
9027 Str_Type : Entity_Id;
9028 Constr_Root : Entity_Id;
9029 Sizexpr : Node_Id;
9031 begin
9032 -- If the root type is already constrained, there are no discriminants
9033 -- in the expression.
9035 if not Has_Discriminants (Root_Typ)
9036 or else Is_Constrained (Root_Typ)
9037 then
9038 Constr_Root := Root_Typ;
9040 -- At this point in the expansion, non-limited view of the type
9041 -- must be available, otherwise the error will be reported later.
9043 if From_Limited_With (Constr_Root)
9044 and then Present (Non_Limited_View (Constr_Root))
9045 then
9046 Constr_Root := Non_Limited_View (Constr_Root);
9047 end if;
9049 else
9050 Constr_Root := Make_Temporary (Loc, 'R');
9052 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9054 Append_To (List_Def,
9055 Make_Subtype_Declaration (Loc,
9056 Defining_Identifier => Constr_Root,
9057 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9058 end if;
9060 -- Generate the range subtype declaration
9062 Range_Type := Make_Temporary (Loc, 'G');
9064 if not Is_Interface (Root_Typ) then
9066 -- subtype rg__xx is
9067 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9069 Sizexpr :=
9070 Make_Op_Subtract (Loc,
9071 Left_Opnd =>
9072 Make_Attribute_Reference (Loc,
9073 Prefix =>
9074 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9075 Attribute_Name => Name_Size),
9076 Right_Opnd =>
9077 Make_Attribute_Reference (Loc,
9078 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9079 Attribute_Name => Name_Object_Size));
9080 else
9081 -- subtype rg__xx is
9082 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9084 Sizexpr :=
9085 Make_Attribute_Reference (Loc,
9086 Prefix =>
9087 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9088 Attribute_Name => Name_Size);
9089 end if;
9091 Set_Paren_Count (Sizexpr, 1);
9093 Append_To (List_Def,
9094 Make_Subtype_Declaration (Loc,
9095 Defining_Identifier => Range_Type,
9096 Subtype_Indication =>
9097 Make_Subtype_Indication (Loc,
9098 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9099 Constraint => Make_Range_Constraint (Loc,
9100 Range_Expression =>
9101 Make_Range (Loc,
9102 Low_Bound => Make_Integer_Literal (Loc, 1),
9103 High_Bound =>
9104 Make_Op_Divide (Loc,
9105 Left_Opnd => Sizexpr,
9106 Right_Opnd => Make_Integer_Literal (Loc,
9107 Intval => System_Storage_Unit)))))));
9109 -- subtype str__nn is Storage_Array (rg__x);
9111 Str_Type := Make_Temporary (Loc, 'S');
9112 Append_To (List_Def,
9113 Make_Subtype_Declaration (Loc,
9114 Defining_Identifier => Str_Type,
9115 Subtype_Indication =>
9116 Make_Subtype_Indication (Loc,
9117 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9118 Constraint =>
9119 Make_Index_Or_Discriminant_Constraint (Loc,
9120 Constraints =>
9121 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9123 -- type Equiv_T is record
9124 -- [ _parent : Tnn; ]
9125 -- E : Str_Type;
9126 -- end Equiv_T;
9128 Equiv_Type := Make_Temporary (Loc, 'T');
9129 Set_Ekind (Equiv_Type, E_Record_Type);
9130 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9132 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9133 -- treatment for this type. In particular, even though _parent's type
9134 -- is a controlled type or contains controlled components, we do not
9135 -- want to set Has_Controlled_Component on it to avoid making it gain
9136 -- an unwanted _controller component.
9138 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9140 -- A class-wide equivalent type does not require initialization
9142 Set_Suppress_Initialization (Equiv_Type);
9144 if not Is_Interface (Root_Typ) then
9145 Append_To (Comp_List,
9146 Make_Component_Declaration (Loc,
9147 Defining_Identifier =>
9148 Make_Defining_Identifier (Loc, Name_uParent),
9149 Component_Definition =>
9150 Make_Component_Definition (Loc,
9151 Aliased_Present => False,
9152 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9154 Set_Reverse_Storage_Order
9155 (Equiv_Type, Reverse_Storage_Order (Base_Type (Root_Utyp)));
9156 Set_Reverse_Bit_Order
9157 (Equiv_Type, Reverse_Bit_Order (Base_Type (Root_Utyp)));
9158 end if;
9160 Append_To (Comp_List,
9161 Make_Component_Declaration (Loc,
9162 Defining_Identifier => Make_Temporary (Loc, 'C'),
9163 Component_Definition =>
9164 Make_Component_Definition (Loc,
9165 Aliased_Present => False,
9166 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9168 Append_To (List_Def,
9169 Make_Full_Type_Declaration (Loc,
9170 Defining_Identifier => Equiv_Type,
9171 Type_Definition =>
9172 Make_Record_Definition (Loc,
9173 Component_List =>
9174 Make_Component_List (Loc,
9175 Component_Items => Comp_List,
9176 Variant_Part => Empty))));
9178 -- Suppress all checks during the analysis of the expanded code to avoid
9179 -- the generation of spurious warnings under ZFP run-time.
9181 Insert_Actions (E, List_Def, Suppress => All_Checks);
9182 return Equiv_Type;
9183 end Make_CW_Equivalent_Type;
9185 -------------------------
9186 -- Make_Invariant_Call --
9187 -------------------------
9189 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9190 Loc : constant Source_Ptr := Sloc (Expr);
9191 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9193 Proc_Id : Entity_Id;
9195 begin
9196 pragma Assert (Has_Invariants (Typ));
9198 Proc_Id := Invariant_Procedure (Typ);
9199 pragma Assert (Present (Proc_Id));
9201 return
9202 Make_Procedure_Call_Statement (Loc,
9203 Name => New_Occurrence_Of (Proc_Id, Loc),
9204 Parameter_Associations => New_List (Relocate_Node (Expr)));
9205 end Make_Invariant_Call;
9207 ------------------------
9208 -- Make_Literal_Range --
9209 ------------------------
9211 function Make_Literal_Range
9212 (Loc : Source_Ptr;
9213 Literal_Typ : Entity_Id) return Node_Id
9215 Lo : constant Node_Id :=
9216 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9217 Index : constant Entity_Id := Etype (Lo);
9218 Length_Expr : constant Node_Id :=
9219 Make_Op_Subtract (Loc,
9220 Left_Opnd =>
9221 Make_Integer_Literal (Loc,
9222 Intval => String_Literal_Length (Literal_Typ)),
9223 Right_Opnd => Make_Integer_Literal (Loc, 1));
9225 Hi : Node_Id;
9227 begin
9228 Set_Analyzed (Lo, False);
9230 if Is_Integer_Type (Index) then
9231 Hi :=
9232 Make_Op_Add (Loc,
9233 Left_Opnd => New_Copy_Tree (Lo),
9234 Right_Opnd => Length_Expr);
9235 else
9236 Hi :=
9237 Make_Attribute_Reference (Loc,
9238 Attribute_Name => Name_Val,
9239 Prefix => New_Occurrence_Of (Index, Loc),
9240 Expressions => New_List (
9241 Make_Op_Add (Loc,
9242 Left_Opnd =>
9243 Make_Attribute_Reference (Loc,
9244 Attribute_Name => Name_Pos,
9245 Prefix => New_Occurrence_Of (Index, Loc),
9246 Expressions => New_List (New_Copy_Tree (Lo))),
9247 Right_Opnd => Length_Expr)));
9248 end if;
9250 return
9251 Make_Range (Loc,
9252 Low_Bound => Lo,
9253 High_Bound => Hi);
9254 end Make_Literal_Range;
9256 --------------------------
9257 -- Make_Non_Empty_Check --
9258 --------------------------
9260 function Make_Non_Empty_Check
9261 (Loc : Source_Ptr;
9262 N : Node_Id) return Node_Id
9264 begin
9265 return
9266 Make_Op_Ne (Loc,
9267 Left_Opnd =>
9268 Make_Attribute_Reference (Loc,
9269 Attribute_Name => Name_Length,
9270 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9271 Right_Opnd =>
9272 Make_Integer_Literal (Loc, 0));
9273 end Make_Non_Empty_Check;
9275 -------------------------
9276 -- Make_Predicate_Call --
9277 -------------------------
9279 -- WARNING: This routine manages Ghost regions. Return statements must be
9280 -- replaced by gotos which jump to the end of the routine and restore the
9281 -- Ghost mode.
9283 function Make_Predicate_Call
9284 (Typ : Entity_Id;
9285 Expr : Node_Id;
9286 Mem : Boolean := False) return Node_Id
9288 Loc : constant Source_Ptr := Sloc (Expr);
9290 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9291 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9292 -- Save the Ghost-related attributes to restore on exit
9294 Call : Node_Id;
9295 Func_Id : Entity_Id;
9297 begin
9298 Func_Id := Predicate_Function (Typ);
9299 pragma Assert (Present (Func_Id));
9301 -- The related type may be subject to pragma Ghost. Set the mode now to
9302 -- ensure that the call is properly marked as Ghost.
9304 Set_Ghost_Mode (Typ);
9306 -- Call special membership version if requested and available
9308 if Mem and then Present (Predicate_Function_M (Typ)) then
9309 Func_Id := Predicate_Function_M (Typ);
9310 end if;
9312 -- Case of calling normal predicate function
9314 -- If the type is tagged, the expression may be class-wide, in which
9315 -- case it has to be converted to its root type, given that the
9316 -- generated predicate function is not dispatching. The conversion
9317 -- is type-safe and does not need validation, which matters when
9318 -- private extensions are involved.
9320 if Is_Tagged_Type (Typ) then
9321 Call :=
9322 Make_Function_Call (Loc,
9323 Name => New_Occurrence_Of (Func_Id, Loc),
9324 Parameter_Associations =>
9325 New_List (OK_Convert_To (Typ, Relocate_Node (Expr))));
9326 else
9327 Call :=
9328 Make_Function_Call (Loc,
9329 Name => New_Occurrence_Of (Func_Id, Loc),
9330 Parameter_Associations => New_List (Relocate_Node (Expr)));
9331 end if;
9333 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9335 return Call;
9336 end Make_Predicate_Call;
9338 --------------------------
9339 -- Make_Predicate_Check --
9340 --------------------------
9342 function Make_Predicate_Check
9343 (Typ : Entity_Id;
9344 Expr : Node_Id) return Node_Id
9346 Loc : constant Source_Ptr := Sloc (Expr);
9348 procedure Add_Failure_Expression (Args : List_Id);
9349 -- Add the failure expression of pragma Predicate_Failure (if any) to
9350 -- list Args.
9352 ----------------------------
9353 -- Add_Failure_Expression --
9354 ----------------------------
9356 procedure Add_Failure_Expression (Args : List_Id) is
9357 function Failure_Expression return Node_Id;
9358 pragma Inline (Failure_Expression);
9359 -- Find aspect or pragma Predicate_Failure that applies to type Typ
9360 -- and return its expression. Return Empty if no such annotation is
9361 -- available.
9363 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean;
9364 pragma Inline (Is_OK_PF_Aspect);
9365 -- Determine whether aspect Asp is a suitable Predicate_Failure
9366 -- aspect that applies to type Typ.
9368 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean;
9369 pragma Inline (Is_OK_PF_Pragma);
9370 -- Determine whether pragma Prag is a suitable Predicate_Failure
9371 -- pragma that applies to type Typ.
9373 procedure Replace_Subtype_Reference (N : Node_Id);
9374 -- Replace the current instance of type Typ denoted by N with
9375 -- expression Expr.
9377 ------------------------
9378 -- Failure_Expression --
9379 ------------------------
9381 function Failure_Expression return Node_Id is
9382 Item : Node_Id;
9384 begin
9385 -- The management of the rep item chain involves "inheritance" of
9386 -- parent type chains. If a parent [sub]type is already subject to
9387 -- pragma Predicate_Failure, then the pragma will also appear in
9388 -- the chain of the child [sub]type, which in turn may possess a
9389 -- pragma of its own. Avoid order-dependent issues by inspecting
9390 -- the rep item chain directly. Note that routine Get_Pragma may
9391 -- return a parent pragma.
9393 Item := First_Rep_Item (Typ);
9394 while Present (Item) loop
9396 -- Predicate_Failure appears as an aspect
9398 if Nkind (Item) = N_Aspect_Specification
9399 and then Is_OK_PF_Aspect (Item)
9400 then
9401 return Expression (Item);
9403 -- Predicate_Failure appears as a pragma
9405 elsif Nkind (Item) = N_Pragma
9406 and then Is_OK_PF_Pragma (Item)
9407 then
9408 return
9409 Get_Pragma_Arg
9410 (Next (First (Pragma_Argument_Associations (Item))));
9411 end if;
9413 Item := Next_Rep_Item (Item);
9414 end loop;
9416 return Empty;
9417 end Failure_Expression;
9419 ---------------------
9420 -- Is_OK_PF_Aspect --
9421 ---------------------
9423 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean is
9424 begin
9425 -- To qualify, the aspect must apply to the type subjected to the
9426 -- predicate check.
9428 return
9429 Chars (Identifier (Asp)) = Name_Predicate_Failure
9430 and then Present (Entity (Asp))
9431 and then Entity (Asp) = Typ;
9432 end Is_OK_PF_Aspect;
9434 ---------------------
9435 -- Is_OK_PF_Pragma --
9436 ---------------------
9438 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean is
9439 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9440 Typ_Arg : Node_Id;
9442 begin
9443 -- Nothing to do when the pragma does not denote Predicate_Failure
9445 if Pragma_Name (Prag) /= Name_Predicate_Failure then
9446 return False;
9448 -- Nothing to do when the pragma lacks arguments, in which case it
9449 -- is illegal.
9451 elsif No (Args) or else Is_Empty_List (Args) then
9452 return False;
9453 end if;
9455 Typ_Arg := Get_Pragma_Arg (First (Args));
9457 -- To qualify, the local name argument of the pragma must denote
9458 -- the type subjected to the predicate check.
9460 return
9461 Is_Entity_Name (Typ_Arg)
9462 and then Present (Entity (Typ_Arg))
9463 and then Entity (Typ_Arg) = Typ;
9464 end Is_OK_PF_Pragma;
9466 --------------------------------
9467 -- Replace_Subtype_Reference --
9468 --------------------------------
9470 procedure Replace_Subtype_Reference (N : Node_Id) is
9471 begin
9472 Rewrite (N, New_Copy_Tree (Expr));
9474 -- We want to treat the node as if it comes from source, so that
9475 -- ASIS will not ignore it.
9477 Set_Comes_From_Source (N, True);
9478 end Replace_Subtype_Reference;
9480 procedure Replace_Subtype_References is
9481 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9483 -- Local variables
9485 PF_Expr : constant Node_Id := Failure_Expression;
9486 Expr : Node_Id;
9488 -- Start of processing for Add_Failure_Expression
9490 begin
9491 if Present (PF_Expr) then
9493 -- Replace any occurrences of the current instance of the type
9494 -- with the object subjected to the predicate check.
9496 Expr := New_Copy_Tree (PF_Expr);
9497 Replace_Subtype_References (Expr, Typ);
9499 -- The failure expression appears as the third argument of the
9500 -- Check pragma.
9502 Append_To (Args,
9503 Make_Pragma_Argument_Association (Loc,
9504 Expression => Expr));
9505 end if;
9506 end Add_Failure_Expression;
9508 -- Local variables
9510 Args : List_Id;
9511 Nam : Name_Id;
9513 -- Start of processing for Make_Predicate_Check
9515 begin
9516 -- If predicate checks are suppressed, then return a null statement. For
9517 -- this call, we check only the scope setting. If the caller wants to
9518 -- check a specific entity's setting, they must do it manually.
9520 if Predicate_Checks_Suppressed (Empty) then
9521 return Make_Null_Statement (Loc);
9522 end if;
9524 -- Do not generate a check within an internal subprogram (stream
9525 -- functions and the like, including including predicate functions).
9527 if Within_Internal_Subprogram then
9528 return Make_Null_Statement (Loc);
9529 end if;
9531 -- Compute proper name to use, we need to get this right so that the
9532 -- right set of check policies apply to the Check pragma we are making.
9534 if Has_Dynamic_Predicate_Aspect (Typ) then
9535 Nam := Name_Dynamic_Predicate;
9536 elsif Has_Static_Predicate_Aspect (Typ) then
9537 Nam := Name_Static_Predicate;
9538 else
9539 Nam := Name_Predicate;
9540 end if;
9542 Args := New_List (
9543 Make_Pragma_Argument_Association (Loc,
9544 Expression => Make_Identifier (Loc, Nam)),
9545 Make_Pragma_Argument_Association (Loc,
9546 Expression => Make_Predicate_Call (Typ, Expr)));
9548 -- If the subtype is subject to pragma Predicate_Failure, add the
9549 -- failure expression as an additional parameter.
9551 Add_Failure_Expression (Args);
9553 return
9554 Make_Pragma (Loc,
9555 Chars => Name_Check,
9556 Pragma_Argument_Associations => Args);
9557 end Make_Predicate_Check;
9559 ----------------------------
9560 -- Make_Subtype_From_Expr --
9561 ----------------------------
9563 -- 1. If Expr is an unconstrained array expression, creates
9564 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9566 -- 2. If Expr is a unconstrained discriminated type expression, creates
9567 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9569 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9571 function Make_Subtype_From_Expr
9572 (E : Node_Id;
9573 Unc_Typ : Entity_Id;
9574 Related_Id : Entity_Id := Empty) return Node_Id
9576 List_Constr : constant List_Id := New_List;
9577 Loc : constant Source_Ptr := Sloc (E);
9578 D : Entity_Id;
9579 Full_Exp : Node_Id;
9580 Full_Subtyp : Entity_Id;
9581 High_Bound : Entity_Id;
9582 Index_Typ : Entity_Id;
9583 Low_Bound : Entity_Id;
9584 Priv_Subtyp : Entity_Id;
9585 Utyp : Entity_Id;
9587 begin
9588 if Is_Private_Type (Unc_Typ)
9589 and then Has_Unknown_Discriminants (Unc_Typ)
9590 then
9591 -- The caller requests a unique external name for both the private
9592 -- and the full subtype.
9594 if Present (Related_Id) then
9595 Full_Subtyp :=
9596 Make_Defining_Identifier (Loc,
9597 Chars => New_External_Name (Chars (Related_Id), 'C'));
9598 Priv_Subtyp :=
9599 Make_Defining_Identifier (Loc,
9600 Chars => New_External_Name (Chars (Related_Id), 'P'));
9602 else
9603 Full_Subtyp := Make_Temporary (Loc, 'C');
9604 Priv_Subtyp := Make_Temporary (Loc, 'P');
9605 end if;
9607 -- Prepare the subtype completion. Use the base type to find the
9608 -- underlying type because the type may be a generic actual or an
9609 -- explicit subtype.
9611 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9613 Full_Exp :=
9614 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9615 Set_Parent (Full_Exp, Parent (E));
9617 Insert_Action (E,
9618 Make_Subtype_Declaration (Loc,
9619 Defining_Identifier => Full_Subtyp,
9620 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9622 -- Define the dummy private subtype
9624 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9625 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9626 Set_Scope (Priv_Subtyp, Full_Subtyp);
9627 Set_Is_Constrained (Priv_Subtyp);
9628 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9629 Set_Is_Itype (Priv_Subtyp);
9630 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9632 if Is_Tagged_Type (Priv_Subtyp) then
9633 Set_Class_Wide_Type
9634 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9635 Set_Direct_Primitive_Operations (Priv_Subtyp,
9636 Direct_Primitive_Operations (Unc_Typ));
9637 end if;
9639 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9641 return New_Occurrence_Of (Priv_Subtyp, Loc);
9643 elsif Is_Array_Type (Unc_Typ) then
9644 Index_Typ := First_Index (Unc_Typ);
9645 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9647 -- Capture the bounds of each index constraint in case the context
9648 -- is an object declaration of an unconstrained type initialized
9649 -- by a function call:
9651 -- Obj : Unconstr_Typ := Func_Call;
9653 -- This scenario requires secondary scope management and the index
9654 -- constraint cannot depend on the temporary used to capture the
9655 -- result of the function call.
9657 -- SS_Mark;
9658 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9659 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9660 -- Obj : S := Temp.all;
9661 -- SS_Release; -- Temp is gone at this point, bounds of S are
9662 -- -- non existent.
9664 -- Generate:
9665 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9667 Low_Bound := Make_Temporary (Loc, 'B');
9668 Insert_Action (E,
9669 Make_Object_Declaration (Loc,
9670 Defining_Identifier => Low_Bound,
9671 Object_Definition =>
9672 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9673 Constant_Present => True,
9674 Expression =>
9675 Make_Attribute_Reference (Loc,
9676 Prefix => Duplicate_Subexpr_No_Checks (E),
9677 Attribute_Name => Name_First,
9678 Expressions => New_List (
9679 Make_Integer_Literal (Loc, J)))));
9681 -- Generate:
9682 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9684 High_Bound := Make_Temporary (Loc, 'B');
9685 Insert_Action (E,
9686 Make_Object_Declaration (Loc,
9687 Defining_Identifier => High_Bound,
9688 Object_Definition =>
9689 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9690 Constant_Present => True,
9691 Expression =>
9692 Make_Attribute_Reference (Loc,
9693 Prefix => Duplicate_Subexpr_No_Checks (E),
9694 Attribute_Name => Name_Last,
9695 Expressions => New_List (
9696 Make_Integer_Literal (Loc, J)))));
9698 Append_To (List_Constr,
9699 Make_Range (Loc,
9700 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9701 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9703 Index_Typ := Next_Index (Index_Typ);
9704 end loop;
9706 elsif Is_Class_Wide_Type (Unc_Typ) then
9707 declare
9708 CW_Subtype : Entity_Id;
9709 EQ_Typ : Entity_Id := Empty;
9711 begin
9712 -- A class-wide equivalent type is not needed on VM targets
9713 -- because the VM back-ends handle the class-wide object
9714 -- initialization itself (and doesn't need or want the
9715 -- additional intermediate type to handle the assignment).
9717 if Expander_Active and then Tagged_Type_Expansion then
9719 -- If this is the class-wide type of a completion that is a
9720 -- record subtype, set the type of the class-wide type to be
9721 -- the full base type, for use in the expanded code for the
9722 -- equivalent type. Should this be done earlier when the
9723 -- completion is analyzed ???
9725 if Is_Private_Type (Etype (Unc_Typ))
9726 and then
9727 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9728 then
9729 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9730 end if;
9732 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9733 end if;
9735 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9736 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9737 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9739 return New_Occurrence_Of (CW_Subtype, Loc);
9740 end;
9742 -- Indefinite record type with discriminants
9744 else
9745 D := First_Discriminant (Unc_Typ);
9746 while Present (D) loop
9747 Append_To (List_Constr,
9748 Make_Selected_Component (Loc,
9749 Prefix => Duplicate_Subexpr_No_Checks (E),
9750 Selector_Name => New_Occurrence_Of (D, Loc)));
9752 Next_Discriminant (D);
9753 end loop;
9754 end if;
9756 return
9757 Make_Subtype_Indication (Loc,
9758 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9759 Constraint =>
9760 Make_Index_Or_Discriminant_Constraint (Loc,
9761 Constraints => List_Constr));
9762 end Make_Subtype_From_Expr;
9764 ---------------
9765 -- Map_Types --
9766 ---------------
9768 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9770 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9771 -- avoid deep indentation of code.
9773 -- NOTE: Routines which deal with discriminant mapping operate on the
9774 -- [underlying/record] full view of various types because those views
9775 -- contain all discriminants and stored constraints.
9777 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9778 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9779 -- overriding chain starting from Prim whose dispatching type is parent
9780 -- type Par_Typ and add a mapping between the result and primitive Prim.
9782 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9783 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9784 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9785 -- if no such primitive is available.
9787 function Build_Chain
9788 (Par_Typ : Entity_Id;
9789 Deriv_Typ : Entity_Id) return Elist_Id;
9790 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9791 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9792 -- list has the form:
9794 -- head tail
9795 -- v v
9796 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9798 -- Note that Par_Typ is not part of the resulting derivation chain
9800 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9801 -- Return the view of type Typ which could potentially contains either
9802 -- the discriminants or stored constraints of the type.
9804 function Find_Discriminant_Value
9805 (Discr : Entity_Id;
9806 Par_Typ : Entity_Id;
9807 Deriv_Typ : Entity_Id;
9808 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9809 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9810 -- in the derivation chain starting from parent type Par_Typ leading to
9811 -- derived type Deriv_Typ. The returned value is one of the following:
9813 -- * An entity which is either a discriminant or a non-discriminant
9814 -- name, and renames/constraints Discr.
9816 -- * An expression which constraints Discr
9818 -- Typ_Elmt is an element of the derivation chain created by routine
9819 -- Build_Chain and denotes the current ancestor being examined.
9821 procedure Map_Discriminants
9822 (Par_Typ : Entity_Id;
9823 Deriv_Typ : Entity_Id);
9824 -- Map each discriminant of type Par_Typ to a meaningful constraint
9825 -- from the point of view of type Deriv_Typ.
9827 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9828 -- Map each primitive of type Par_Typ to a corresponding primitive of
9829 -- type Deriv_Typ.
9831 -------------------
9832 -- Add_Primitive --
9833 -------------------
9835 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9836 Par_Prim : Entity_Id;
9838 begin
9839 -- Inspect the inheritance chain through the Alias attribute and the
9840 -- overriding chain through the Overridden_Operation looking for an
9841 -- ancestor primitive with the appropriate dispatching type.
9843 Par_Prim := Prim;
9844 while Present (Par_Prim) loop
9845 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9846 Par_Prim := Ancestor_Primitive (Par_Prim);
9847 end loop;
9849 -- Create a mapping of the form:
9851 -- parent type primitive -> derived type primitive
9853 if Present (Par_Prim) then
9854 Type_Map.Set (Par_Prim, Prim);
9855 end if;
9856 end Add_Primitive;
9858 ------------------------
9859 -- Ancestor_Primitive --
9860 ------------------------
9862 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9863 Inher_Prim : constant Entity_Id := Alias (Subp);
9864 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9866 begin
9867 -- The current subprogram overrides an ancestor primitive
9869 if Present (Over_Prim) then
9870 return Over_Prim;
9872 -- The current subprogram is an internally generated alias of an
9873 -- inherited ancestor primitive.
9875 elsif Present (Inher_Prim) then
9876 return Inher_Prim;
9878 -- Otherwise the current subprogram is the root of the inheritance or
9879 -- overriding chain.
9881 else
9882 return Empty;
9883 end if;
9884 end Ancestor_Primitive;
9886 -----------------
9887 -- Build_Chain --
9888 -----------------
9890 function Build_Chain
9891 (Par_Typ : Entity_Id;
9892 Deriv_Typ : Entity_Id) return Elist_Id
9894 Anc_Typ : Entity_Id;
9895 Chain : Elist_Id;
9896 Curr_Typ : Entity_Id;
9898 begin
9899 Chain := New_Elmt_List;
9901 -- Add the derived type to the derivation chain
9903 Prepend_Elmt (Deriv_Typ, Chain);
9905 -- Examine all ancestors starting from the derived type climbing
9906 -- towards parent type Par_Typ.
9908 Curr_Typ := Deriv_Typ;
9909 loop
9910 -- Handle the case where the current type is a record which
9911 -- derives from a subtype.
9913 -- subtype Sub_Typ is Par_Typ ...
9914 -- type Deriv_Typ is Sub_Typ ...
9916 if Ekind (Curr_Typ) = E_Record_Type
9917 and then Present (Parent_Subtype (Curr_Typ))
9918 then
9919 Anc_Typ := Parent_Subtype (Curr_Typ);
9921 -- Handle the case where the current type is a record subtype of
9922 -- another subtype.
9924 -- subtype Sub_Typ1 is Par_Typ ...
9925 -- subtype Sub_Typ2 is Sub_Typ1 ...
9927 elsif Ekind (Curr_Typ) = E_Record_Subtype
9928 and then Present (Cloned_Subtype (Curr_Typ))
9929 then
9930 Anc_Typ := Cloned_Subtype (Curr_Typ);
9932 -- Otherwise use the direct parent type
9934 else
9935 Anc_Typ := Etype (Curr_Typ);
9936 end if;
9938 -- Use the first subtype when dealing with itypes
9940 if Is_Itype (Anc_Typ) then
9941 Anc_Typ := First_Subtype (Anc_Typ);
9942 end if;
9944 -- Work with the view which contains the discriminants and stored
9945 -- constraints.
9947 Anc_Typ := Discriminated_View (Anc_Typ);
9949 -- Stop the climb when either the parent type has been reached or
9950 -- there are no more ancestors left to examine.
9952 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9954 Prepend_Unique_Elmt (Anc_Typ, Chain);
9955 Curr_Typ := Anc_Typ;
9956 end loop;
9958 return Chain;
9959 end Build_Chain;
9961 ------------------------
9962 -- Discriminated_View --
9963 ------------------------
9965 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9966 T : Entity_Id;
9968 begin
9969 T := Typ;
9971 -- Use the [underlying] full view when dealing with private types
9972 -- because the view contains all inherited discriminants or stored
9973 -- constraints.
9975 if Is_Private_Type (T) then
9976 if Present (Underlying_Full_View (T)) then
9977 T := Underlying_Full_View (T);
9979 elsif Present (Full_View (T)) then
9980 T := Full_View (T);
9981 end if;
9982 end if;
9984 -- Use the underlying record view when the type is an extenstion of
9985 -- a parent type with unknown discriminants because the view contains
9986 -- all inherited discriminants or stored constraints.
9988 if Ekind (T) = E_Record_Type
9989 and then Present (Underlying_Record_View (T))
9990 then
9991 T := Underlying_Record_View (T);
9992 end if;
9994 return T;
9995 end Discriminated_View;
9997 -----------------------------
9998 -- Find_Discriminant_Value --
9999 -----------------------------
10001 function Find_Discriminant_Value
10002 (Discr : Entity_Id;
10003 Par_Typ : Entity_Id;
10004 Deriv_Typ : Entity_Id;
10005 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
10007 Discr_Pos : constant Uint := Discriminant_Number (Discr);
10008 Typ : constant Entity_Id := Node (Typ_Elmt);
10010 function Find_Constraint_Value
10011 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
10012 -- Given constraint Constr, find what it denotes. This is either:
10014 -- * An entity which is either a discriminant or a name
10016 -- * An expression
10018 ---------------------------
10019 -- Find_Constraint_Value --
10020 ---------------------------
10022 function Find_Constraint_Value
10023 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
10025 begin
10026 if Nkind (Constr) in N_Entity then
10028 -- The constraint denotes a discriminant of the curren type
10029 -- which renames the ancestor discriminant:
10031 -- vv
10032 -- type Typ (D1 : ...; DN : ...) is
10033 -- new Anc (Discr => D1) with ...
10034 -- ^^
10036 if Ekind (Constr) = E_Discriminant then
10038 -- The discriminant belongs to derived type Deriv_Typ. This
10039 -- is the final value for the ancestor discriminant as the
10040 -- derivations chain has been fully exhausted.
10042 if Typ = Deriv_Typ then
10043 return Constr;
10045 -- Otherwise the discriminant may be renamed or constrained
10046 -- at a lower level. Continue looking down the derivation
10047 -- chain.
10049 else
10050 return
10051 Find_Discriminant_Value
10052 (Discr => Constr,
10053 Par_Typ => Par_Typ,
10054 Deriv_Typ => Deriv_Typ,
10055 Typ_Elmt => Next_Elmt (Typ_Elmt));
10056 end if;
10058 -- Otherwise the constraint denotes a reference to some name
10059 -- which results in a Girder discriminant:
10061 -- vvvv
10062 -- Name : ...;
10063 -- type Typ (D1 : ...; DN : ...) is
10064 -- new Anc (Discr => Name) with ...
10065 -- ^^^^
10067 -- Return the name as this is the proper constraint of the
10068 -- discriminant.
10070 else
10071 return Constr;
10072 end if;
10074 -- The constraint denotes a reference to a name
10076 elsif Is_Entity_Name (Constr) then
10077 return Find_Constraint_Value (Entity (Constr));
10079 -- Otherwise the current constraint is an expression which yields
10080 -- a Girder discriminant:
10082 -- type Typ (D1 : ...; DN : ...) is
10083 -- new Anc (Discr => <expression>) with ...
10084 -- ^^^^^^^^^^
10086 -- Return the expression as this is the proper constraint of the
10087 -- discriminant.
10089 else
10090 return Constr;
10091 end if;
10092 end Find_Constraint_Value;
10094 -- Local variables
10096 Constrs : constant Elist_Id := Stored_Constraint (Typ);
10098 Constr_Elmt : Elmt_Id;
10099 Pos : Uint;
10100 Typ_Discr : Entity_Id;
10102 -- Start of processing for Find_Discriminant_Value
10104 begin
10105 -- The algorithm for finding the value of a discriminant works as
10106 -- follows. First, it recreates the derivation chain from Par_Typ
10107 -- to Deriv_Typ as a list:
10109 -- Par_Typ (shown for completeness)
10110 -- v
10111 -- Ancestor_N <-- head of chain
10112 -- v
10113 -- Ancestor_1
10114 -- v
10115 -- Deriv_Typ <-- tail of chain
10117 -- The algorithm then traces the fate of a parent discriminant down
10118 -- the derivation chain. At each derivation level, the discriminant
10119 -- may be either inherited or constrained.
10121 -- 1) Discriminant is inherited: there are two cases, depending on
10122 -- which type is inheriting.
10124 -- 1.1) Deriv_Typ is inheriting:
10126 -- type Ancestor (D_1 : ...) is tagged ...
10127 -- type Deriv_Typ is new Ancestor ...
10129 -- In this case the inherited discriminant is the final value of
10130 -- the parent discriminant because the end of the derivation chain
10131 -- has been reached.
10133 -- 1.2) Some other type is inheriting:
10135 -- type Ancestor_1 (D_1 : ...) is tagged ...
10136 -- type Ancestor_2 is new Ancestor_1 ...
10138 -- In this case the algorithm continues to trace the fate of the
10139 -- inherited discriminant down the derivation chain because it may
10140 -- be further inherited or constrained.
10142 -- 2) Discriminant is constrained: there are three cases, depending
10143 -- on what the constraint is.
10145 -- 2.1) The constraint is another discriminant (aka renaming):
10147 -- type Ancestor_1 (D_1 : ...) is tagged ...
10148 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10150 -- In this case the constraining discriminant becomes the one to
10151 -- track down the derivation chain. The algorithm already knows
10152 -- that D_2 constrains D_1, therefore if the algorithm finds the
10153 -- value of D_2, then this would also be the value for D_1.
10155 -- 2.2) The constraint is a name (aka Girder):
10157 -- Name : ...
10158 -- type Ancestor_1 (D_1 : ...) is tagged ...
10159 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10161 -- In this case the name is the final value of D_1 because the
10162 -- discriminant cannot be further constrained.
10164 -- 2.3) The constraint is an expression (aka Girder):
10166 -- type Ancestor_1 (D_1 : ...) is tagged ...
10167 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10169 -- Similar to 2.2, the expression is the final value of D_1
10171 Pos := Uint_1;
10173 -- When a derived type constrains its parent type, all constaints
10174 -- appear in the Stored_Constraint list. Examine the list looking
10175 -- for a positional match.
10177 if Present (Constrs) then
10178 Constr_Elmt := First_Elmt (Constrs);
10179 while Present (Constr_Elmt) loop
10181 -- The position of the current constraint matches that of the
10182 -- ancestor discriminant.
10184 if Pos = Discr_Pos then
10185 return Find_Constraint_Value (Node (Constr_Elmt));
10186 end if;
10188 Next_Elmt (Constr_Elmt);
10189 Pos := Pos + 1;
10190 end loop;
10192 -- Otherwise the derived type does not constraint its parent type in
10193 -- which case it inherits the parent discriminants.
10195 else
10196 Typ_Discr := First_Discriminant (Typ);
10197 while Present (Typ_Discr) loop
10199 -- The position of the current discriminant matches that of the
10200 -- ancestor discriminant.
10202 if Pos = Discr_Pos then
10203 return Find_Constraint_Value (Typ_Discr);
10204 end if;
10206 Next_Discriminant (Typ_Discr);
10207 Pos := Pos + 1;
10208 end loop;
10209 end if;
10211 -- A discriminant must always have a corresponding value. This is
10212 -- either another discriminant, a name, or an expression. If this
10213 -- point is reached, them most likely the derivation chain employs
10214 -- the wrong views of types.
10216 pragma Assert (False);
10218 return Empty;
10219 end Find_Discriminant_Value;
10221 -----------------------
10222 -- Map_Discriminants --
10223 -----------------------
10225 procedure Map_Discriminants
10226 (Par_Typ : Entity_Id;
10227 Deriv_Typ : Entity_Id)
10229 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10231 Discr : Entity_Id;
10232 Discr_Val : Node_Or_Entity_Id;
10234 begin
10235 -- Examine each discriminant of parent type Par_Typ and find a
10236 -- suitable value for it from the point of view of derived type
10237 -- Deriv_Typ.
10239 if Has_Discriminants (Par_Typ) then
10240 Discr := First_Discriminant (Par_Typ);
10241 while Present (Discr) loop
10242 Discr_Val :=
10243 Find_Discriminant_Value
10244 (Discr => Discr,
10245 Par_Typ => Par_Typ,
10246 Deriv_Typ => Deriv_Typ,
10247 Typ_Elmt => First_Elmt (Deriv_Chain));
10249 -- Create a mapping of the form:
10251 -- parent type discriminant -> value
10253 Type_Map.Set (Discr, Discr_Val);
10255 Next_Discriminant (Discr);
10256 end loop;
10257 end if;
10258 end Map_Discriminants;
10260 --------------------
10261 -- Map_Primitives --
10262 --------------------
10264 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10265 Deriv_Prim : Entity_Id;
10266 Par_Prim : Entity_Id;
10267 Par_Prims : Elist_Id;
10268 Prim_Elmt : Elmt_Id;
10270 begin
10271 -- Inspect the primitives of the derived type and determine whether
10272 -- they relate to the primitives of the parent type. If there is a
10273 -- meaningful relation, create a mapping of the form:
10275 -- parent type primitive -> perived type primitive
10277 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10278 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10279 while Present (Prim_Elmt) loop
10280 Deriv_Prim := Node (Prim_Elmt);
10282 if Is_Subprogram (Deriv_Prim)
10283 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10284 then
10285 Add_Primitive (Deriv_Prim, Par_Typ);
10286 end if;
10288 Next_Elmt (Prim_Elmt);
10289 end loop;
10290 end if;
10292 -- If the parent operation is an interface operation, the overriding
10293 -- indicator is not present. Instead, we get from the interface
10294 -- operation the primitive of the current type that implements it.
10296 if Is_Interface (Par_Typ) then
10297 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10299 if Present (Par_Prims) then
10300 Prim_Elmt := First_Elmt (Par_Prims);
10302 while Present (Prim_Elmt) loop
10303 Par_Prim := Node (Prim_Elmt);
10304 Deriv_Prim :=
10305 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10307 if Present (Deriv_Prim) then
10308 Type_Map.Set (Par_Prim, Deriv_Prim);
10309 end if;
10311 Next_Elmt (Prim_Elmt);
10312 end loop;
10313 end if;
10314 end if;
10315 end Map_Primitives;
10317 -- Start of processing for Map_Types
10319 begin
10320 -- Nothing to do if there are no types to work with
10322 if No (Parent_Type) or else No (Derived_Type) then
10323 return;
10325 -- Nothing to do if the mapping already exists
10327 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10328 return;
10330 -- Nothing to do if both types are not tagged. Note that untagged types
10331 -- do not have primitive operations and their discriminants are already
10332 -- handled by gigi.
10334 elsif not Is_Tagged_Type (Parent_Type)
10335 or else not Is_Tagged_Type (Derived_Type)
10336 then
10337 return;
10338 end if;
10340 -- Create a mapping of the form
10342 -- parent type -> derived type
10344 -- to prevent any subsequent attempts to produce the same relations
10346 Type_Map.Set (Parent_Type, Derived_Type);
10348 -- Create mappings of the form
10350 -- parent type discriminant -> derived type discriminant
10351 -- <or>
10352 -- parent type discriminant -> constraint
10354 -- Note that mapping of discriminants breaks privacy because it needs to
10355 -- work with those views which contains the discriminants and any stored
10356 -- constraints.
10358 Map_Discriminants
10359 (Par_Typ => Discriminated_View (Parent_Type),
10360 Deriv_Typ => Discriminated_View (Derived_Type));
10362 -- Create mappings of the form
10364 -- parent type primitive -> derived type primitive
10366 Map_Primitives
10367 (Par_Typ => Parent_Type,
10368 Deriv_Typ => Derived_Type);
10369 end Map_Types;
10371 ----------------------------
10372 -- Matching_Standard_Type --
10373 ----------------------------
10375 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10376 pragma Assert (Is_Scalar_Type (Typ));
10377 Siz : constant Uint := Esize (Typ);
10379 begin
10380 -- Floating-point cases
10382 if Is_Floating_Point_Type (Typ) then
10383 if Siz <= Esize (Standard_Short_Float) then
10384 return Standard_Short_Float;
10385 elsif Siz <= Esize (Standard_Float) then
10386 return Standard_Float;
10387 elsif Siz <= Esize (Standard_Long_Float) then
10388 return Standard_Long_Float;
10389 elsif Siz <= Esize (Standard_Long_Long_Float) then
10390 return Standard_Long_Long_Float;
10391 else
10392 raise Program_Error;
10393 end if;
10395 -- Integer cases (includes fixed-point types)
10397 -- Unsigned integer cases (includes normal enumeration types)
10399 elsif Is_Unsigned_Type (Typ) then
10400 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10401 return Standard_Short_Short_Unsigned;
10402 elsif Siz <= Esize (Standard_Short_Unsigned) then
10403 return Standard_Short_Unsigned;
10404 elsif Siz <= Esize (Standard_Unsigned) then
10405 return Standard_Unsigned;
10406 elsif Siz <= Esize (Standard_Long_Unsigned) then
10407 return Standard_Long_Unsigned;
10408 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10409 return Standard_Long_Long_Unsigned;
10410 else
10411 raise Program_Error;
10412 end if;
10414 -- Signed integer cases
10416 else
10417 if Siz <= Esize (Standard_Short_Short_Integer) then
10418 return Standard_Short_Short_Integer;
10419 elsif Siz <= Esize (Standard_Short_Integer) then
10420 return Standard_Short_Integer;
10421 elsif Siz <= Esize (Standard_Integer) then
10422 return Standard_Integer;
10423 elsif Siz <= Esize (Standard_Long_Integer) then
10424 return Standard_Long_Integer;
10425 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10426 return Standard_Long_Long_Integer;
10427 else
10428 raise Program_Error;
10429 end if;
10430 end if;
10431 end Matching_Standard_Type;
10433 -----------------------------
10434 -- May_Generate_Large_Temp --
10435 -----------------------------
10437 -- At the current time, the only types that we return False for (i.e. where
10438 -- we decide we know they cannot generate large temps) are ones where we
10439 -- know the size is 256 bits or less at compile time, and we are still not
10440 -- doing a thorough job on arrays and records ???
10442 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10443 begin
10444 if not Size_Known_At_Compile_Time (Typ) then
10445 return False;
10447 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10448 return False;
10450 elsif Is_Array_Type (Typ)
10451 and then Present (Packed_Array_Impl_Type (Typ))
10452 then
10453 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10455 -- We could do more here to find other small types ???
10457 else
10458 return True;
10459 end if;
10460 end May_Generate_Large_Temp;
10462 --------------------------------------------
10463 -- Needs_Conditional_Null_Excluding_Check --
10464 --------------------------------------------
10466 function Needs_Conditional_Null_Excluding_Check
10467 (Typ : Entity_Id) return Boolean
10469 begin
10470 return
10471 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
10472 end Needs_Conditional_Null_Excluding_Check;
10474 ----------------------------
10475 -- Needs_Constant_Address --
10476 ----------------------------
10478 function Needs_Constant_Address
10479 (Decl : Node_Id;
10480 Typ : Entity_Id) return Boolean
10482 begin
10483 -- If we have no initialization of any kind, then we don't need to place
10484 -- any restrictions on the address clause, because the object will be
10485 -- elaborated after the address clause is evaluated. This happens if the
10486 -- declaration has no initial expression, or the type has no implicit
10487 -- initialization, or the object is imported.
10489 -- The same holds for all initialized scalar types and all access types.
10490 -- Packed bit arrays of size up to 64 are represented using a modular
10491 -- type with an initialization (to zero) and can be processed like other
10492 -- initialized scalar types.
10494 -- If the type is controlled, code to attach the object to a
10495 -- finalization chain is generated at the point of declaration, and
10496 -- therefore the elaboration of the object cannot be delayed: the
10497 -- address expression must be a constant.
10499 if No (Expression (Decl))
10500 and then not Needs_Finalization (Typ)
10501 and then
10502 (not Has_Non_Null_Base_Init_Proc (Typ)
10503 or else Is_Imported (Defining_Identifier (Decl)))
10504 then
10505 return False;
10507 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10508 or else Is_Access_Type (Typ)
10509 or else
10510 (Is_Bit_Packed_Array (Typ)
10511 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10512 then
10513 return False;
10515 else
10516 -- Otherwise, we require the address clause to be constant because
10517 -- the call to the initialization procedure (or the attach code) has
10518 -- to happen at the point of the declaration.
10520 -- Actually the IP call has been moved to the freeze actions anyway,
10521 -- so maybe we can relax this restriction???
10523 return True;
10524 end if;
10525 end Needs_Constant_Address;
10527 ------------------------
10528 -- Needs_Finalization --
10529 ------------------------
10531 function Needs_Finalization (Typ : Entity_Id) return Boolean is
10532 function Has_Some_Controlled_Component
10533 (Input_Typ : Entity_Id) return Boolean;
10534 -- Determine whether type Input_Typ has at least one controlled
10535 -- component.
10537 -----------------------------------
10538 -- Has_Some_Controlled_Component --
10539 -----------------------------------
10541 function Has_Some_Controlled_Component
10542 (Input_Typ : Entity_Id) return Boolean
10544 Comp : Entity_Id;
10546 begin
10547 -- When a type is already frozen and has at least one controlled
10548 -- component, or is manually decorated, it is sufficient to inspect
10549 -- flag Has_Controlled_Component.
10551 if Has_Controlled_Component (Input_Typ) then
10552 return True;
10554 -- Otherwise inspect the internals of the type
10556 elsif not Is_Frozen (Input_Typ) then
10557 if Is_Array_Type (Input_Typ) then
10558 return Needs_Finalization (Component_Type (Input_Typ));
10560 elsif Is_Record_Type (Input_Typ) then
10561 Comp := First_Component (Input_Typ);
10562 while Present (Comp) loop
10563 if Needs_Finalization (Etype (Comp)) then
10564 return True;
10565 end if;
10567 Next_Component (Comp);
10568 end loop;
10569 end if;
10570 end if;
10572 return False;
10573 end Has_Some_Controlled_Component;
10575 -- Start of processing for Needs_Finalization
10577 begin
10578 -- Certain run-time configurations and targets do not provide support
10579 -- for controlled types.
10581 if Restriction_Active (No_Finalization) then
10582 return False;
10584 -- C++ types are not considered controlled. It is assumed that the non-
10585 -- Ada side will handle their clean up.
10587 elsif Convention (Typ) = Convention_CPP then
10588 return False;
10590 -- Class-wide types are treated as controlled because derivations from
10591 -- the root type may introduce controlled components.
10593 elsif Is_Class_Wide_Type (Typ) then
10594 return True;
10596 -- Concurrent types are controlled as long as their corresponding record
10597 -- is controlled.
10599 elsif Is_Concurrent_Type (Typ)
10600 and then Present (Corresponding_Record_Type (Typ))
10601 and then Needs_Finalization (Corresponding_Record_Type (Typ))
10602 then
10603 return True;
10605 -- Otherwise the type is controlled when it is either derived from type
10606 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10607 -- contains at least one controlled component.
10609 else
10610 return
10611 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
10612 end if;
10613 end Needs_Finalization;
10615 ----------------------------
10616 -- New_Class_Wide_Subtype --
10617 ----------------------------
10619 function New_Class_Wide_Subtype
10620 (CW_Typ : Entity_Id;
10621 N : Node_Id) return Entity_Id
10623 Res : constant Entity_Id := Create_Itype (E_Void, N);
10625 -- Capture relevant attributes of the class-wide subtype which must be
10626 -- restored after the copy.
10628 Res_Chars : constant Name_Id := Chars (Res);
10629 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
10630 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
10631 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
10632 Res_Scope : constant Entity_Id := Scope (Res);
10634 begin
10635 Copy_Node (CW_Typ, Res);
10637 -- Restore the relevant attributes of the class-wide subtype
10639 Set_Chars (Res, Res_Chars);
10640 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
10641 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
10642 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
10643 Set_Scope (Res, Res_Scope);
10645 -- Decorate the class-wide subtype
10647 Set_Associated_Node_For_Itype (Res, N);
10648 Set_Comes_From_Source (Res, False);
10649 Set_Ekind (Res, E_Class_Wide_Subtype);
10650 Set_Etype (Res, Base_Type (CW_Typ));
10651 Set_Freeze_Node (Res, Empty);
10652 Set_Is_Frozen (Res, False);
10653 Set_Is_Itype (Res);
10654 Set_Is_Public (Res, False);
10655 Set_Next_Entity (Res, Empty);
10656 Set_Prev_Entity (Res, Empty);
10657 Set_Sloc (Res, Sloc (N));
10659 Set_Public_Status (Res);
10661 return Res;
10662 end New_Class_Wide_Subtype;
10664 --------------------------------
10665 -- Non_Limited_Designated_Type --
10666 ---------------------------------
10668 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10669 Desig : constant Entity_Id := Designated_Type (T);
10670 begin
10671 if Has_Non_Limited_View (Desig) then
10672 return Non_Limited_View (Desig);
10673 else
10674 return Desig;
10675 end if;
10676 end Non_Limited_Designated_Type;
10678 -----------------------------------
10679 -- OK_To_Do_Constant_Replacement --
10680 -----------------------------------
10682 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10683 ES : constant Entity_Id := Scope (E);
10684 CS : Entity_Id;
10686 begin
10687 -- Do not replace statically allocated objects, because they may be
10688 -- modified outside the current scope.
10690 if Is_Statically_Allocated (E) then
10691 return False;
10693 -- Do not replace aliased or volatile objects, since we don't know what
10694 -- else might change the value.
10696 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10697 return False;
10699 -- Debug flag -gnatdM disconnects this optimization
10701 elsif Debug_Flag_MM then
10702 return False;
10704 -- Otherwise check scopes
10706 else
10707 CS := Current_Scope;
10709 loop
10710 -- If we are in right scope, replacement is safe
10712 if CS = ES then
10713 return True;
10715 -- Packages do not affect the determination of safety
10717 elsif Ekind (CS) = E_Package then
10718 exit when CS = Standard_Standard;
10719 CS := Scope (CS);
10721 -- Blocks do not affect the determination of safety
10723 elsif Ekind (CS) = E_Block then
10724 CS := Scope (CS);
10726 -- Loops do not affect the determination of safety. Note that we
10727 -- kill all current values on entry to a loop, so we are just
10728 -- talking about processing within a loop here.
10730 elsif Ekind (CS) = E_Loop then
10731 CS := Scope (CS);
10733 -- Otherwise, the reference is dubious, and we cannot be sure that
10734 -- it is safe to do the replacement.
10736 else
10737 exit;
10738 end if;
10739 end loop;
10741 return False;
10742 end if;
10743 end OK_To_Do_Constant_Replacement;
10745 ------------------------------------
10746 -- Possible_Bit_Aligned_Component --
10747 ------------------------------------
10749 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10750 begin
10751 -- Do not process an unanalyzed node because it is not yet decorated and
10752 -- most checks performed below will fail.
10754 if not Analyzed (N) then
10755 return False;
10756 end if;
10758 -- There are never alignment issues in CodePeer mode
10760 if CodePeer_Mode then
10761 return False;
10762 end if;
10764 case Nkind (N) is
10766 -- Case of indexed component
10768 when N_Indexed_Component =>
10769 declare
10770 P : constant Node_Id := Prefix (N);
10771 Ptyp : constant Entity_Id := Etype (P);
10773 begin
10774 -- If we know the component size and it is less than 64, then
10775 -- we are definitely OK. The back end always does assignment of
10776 -- misaligned small objects correctly.
10778 if Known_Static_Component_Size (Ptyp)
10779 and then Component_Size (Ptyp) <= 64
10780 then
10781 return False;
10783 -- Otherwise, we need to test the prefix, to see if we are
10784 -- indexing from a possibly unaligned component.
10786 else
10787 return Possible_Bit_Aligned_Component (P);
10788 end if;
10789 end;
10791 -- Case of selected component
10793 when N_Selected_Component =>
10794 declare
10795 P : constant Node_Id := Prefix (N);
10796 Comp : constant Entity_Id := Entity (Selector_Name (N));
10798 begin
10799 -- If there is no component clause, then we are in the clear
10800 -- since the back end will never misalign a large component
10801 -- unless it is forced to do so. In the clear means we need
10802 -- only the recursive test on the prefix.
10804 if Component_May_Be_Bit_Aligned (Comp) then
10805 return True;
10806 else
10807 return Possible_Bit_Aligned_Component (P);
10808 end if;
10809 end;
10811 -- For a slice, test the prefix, if that is possibly misaligned,
10812 -- then for sure the slice is.
10814 when N_Slice =>
10815 return Possible_Bit_Aligned_Component (Prefix (N));
10817 -- For an unchecked conversion, check whether the expression may
10818 -- be bit-aligned.
10820 when N_Unchecked_Type_Conversion =>
10821 return Possible_Bit_Aligned_Component (Expression (N));
10823 -- If we have none of the above, it means that we have fallen off the
10824 -- top testing prefixes recursively, and we now have a stand alone
10825 -- object, where we don't have a problem, unless this is a renaming,
10826 -- in which case we need to look into the renamed object.
10828 when others =>
10829 if Is_Entity_Name (N)
10830 and then Present (Renamed_Object (Entity (N)))
10831 then
10832 return
10833 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10834 else
10835 return False;
10836 end if;
10837 end case;
10838 end Possible_Bit_Aligned_Component;
10840 -----------------------------------------------
10841 -- Process_Statements_For_Controlled_Objects --
10842 -----------------------------------------------
10844 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10845 Loc : constant Source_Ptr := Sloc (N);
10847 function Are_Wrapped (L : List_Id) return Boolean;
10848 -- Determine whether list L contains only one statement which is a block
10850 function Wrap_Statements_In_Block
10851 (L : List_Id;
10852 Scop : Entity_Id := Current_Scope) return Node_Id;
10853 -- Given a list of statements L, wrap it in a block statement and return
10854 -- the generated node. Scop is either the current scope or the scope of
10855 -- the context (if applicable).
10857 -----------------
10858 -- Are_Wrapped --
10859 -----------------
10861 function Are_Wrapped (L : List_Id) return Boolean is
10862 Stmt : constant Node_Id := First (L);
10863 begin
10864 return
10865 Present (Stmt)
10866 and then No (Next (Stmt))
10867 and then Nkind (Stmt) = N_Block_Statement;
10868 end Are_Wrapped;
10870 ------------------------------
10871 -- Wrap_Statements_In_Block --
10872 ------------------------------
10874 function Wrap_Statements_In_Block
10875 (L : List_Id;
10876 Scop : Entity_Id := Current_Scope) return Node_Id
10878 Block_Id : Entity_Id;
10879 Block_Nod : Node_Id;
10880 Iter_Loop : Entity_Id;
10882 begin
10883 Block_Nod :=
10884 Make_Block_Statement (Loc,
10885 Declarations => No_List,
10886 Handled_Statement_Sequence =>
10887 Make_Handled_Sequence_Of_Statements (Loc,
10888 Statements => L));
10890 -- Create a label for the block in case the block needs to manage the
10891 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10893 Add_Block_Identifier (Block_Nod, Block_Id);
10895 -- When wrapping the statements of an iterator loop, check whether
10896 -- the loop requires secondary stack management and if so, propagate
10897 -- the appropriate flags to the block. This ensures that the cursor
10898 -- is properly cleaned up at each iteration of the loop.
10900 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10902 if Present (Iter_Loop) then
10903 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10905 -- Secondary stack reclamation is suppressed when the associated
10906 -- iterator loop contains a return statement which uses the stack.
10908 Set_Sec_Stack_Needed_For_Return
10909 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10910 end if;
10912 return Block_Nod;
10913 end Wrap_Statements_In_Block;
10915 -- Local variables
10917 Block : Node_Id;
10919 -- Start of processing for Process_Statements_For_Controlled_Objects
10921 begin
10922 -- Whenever a non-handled statement list is wrapped in a block, the
10923 -- block must be explicitly analyzed to redecorate all entities in the
10924 -- list and ensure that a finalizer is properly built.
10926 case Nkind (N) is
10927 when N_Conditional_Entry_Call
10928 | N_Elsif_Part
10929 | N_If_Statement
10930 | N_Selective_Accept
10932 -- Check the "then statements" for elsif parts and if statements
10934 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10935 and then not Is_Empty_List (Then_Statements (N))
10936 and then not Are_Wrapped (Then_Statements (N))
10937 and then Requires_Cleanup_Actions
10938 (L => Then_Statements (N),
10939 Lib_Level => False,
10940 Nested_Constructs => False)
10941 then
10942 Block := Wrap_Statements_In_Block (Then_Statements (N));
10943 Set_Then_Statements (N, New_List (Block));
10945 Analyze (Block);
10946 end if;
10948 -- Check the "else statements" for conditional entry calls, if
10949 -- statements and selective accepts.
10951 if Nkind_In (N, N_Conditional_Entry_Call,
10952 N_If_Statement,
10953 N_Selective_Accept)
10954 and then not Is_Empty_List (Else_Statements (N))
10955 and then not Are_Wrapped (Else_Statements (N))
10956 and then Requires_Cleanup_Actions
10957 (L => Else_Statements (N),
10958 Lib_Level => False,
10959 Nested_Constructs => False)
10960 then
10961 Block := Wrap_Statements_In_Block (Else_Statements (N));
10962 Set_Else_Statements (N, New_List (Block));
10964 Analyze (Block);
10965 end if;
10967 when N_Abortable_Part
10968 | N_Accept_Alternative
10969 | N_Case_Statement_Alternative
10970 | N_Delay_Alternative
10971 | N_Entry_Call_Alternative
10972 | N_Exception_Handler
10973 | N_Loop_Statement
10974 | N_Triggering_Alternative
10976 if not Is_Empty_List (Statements (N))
10977 and then not Are_Wrapped (Statements (N))
10978 and then Requires_Cleanup_Actions
10979 (L => Statements (N),
10980 Lib_Level => False,
10981 Nested_Constructs => False)
10982 then
10983 if Nkind (N) = N_Loop_Statement
10984 and then Present (Identifier (N))
10985 then
10986 Block :=
10987 Wrap_Statements_In_Block
10988 (L => Statements (N),
10989 Scop => Entity (Identifier (N)));
10990 else
10991 Block := Wrap_Statements_In_Block (Statements (N));
10992 end if;
10994 Set_Statements (N, New_List (Block));
10995 Analyze (Block);
10996 end if;
10998 -- Could be e.g. a loop that was transformed into a block or null
10999 -- statement. Do nothing for terminate alternatives.
11001 when N_Block_Statement
11002 | N_Null_Statement
11003 | N_Terminate_Alternative
11005 null;
11007 when others =>
11008 raise Program_Error;
11009 end case;
11010 end Process_Statements_For_Controlled_Objects;
11012 ------------------
11013 -- Power_Of_Two --
11014 ------------------
11016 function Power_Of_Two (N : Node_Id) return Nat is
11017 Typ : constant Entity_Id := Etype (N);
11018 pragma Assert (Is_Integer_Type (Typ));
11020 Siz : constant Nat := UI_To_Int (Esize (Typ));
11021 Val : Uint;
11023 begin
11024 if not Compile_Time_Known_Value (N) then
11025 return 0;
11027 else
11028 Val := Expr_Value (N);
11029 for J in 1 .. Siz - 1 loop
11030 if Val = Uint_2 ** J then
11031 return J;
11032 end if;
11033 end loop;
11035 return 0;
11036 end if;
11037 end Power_Of_Two;
11039 ----------------------
11040 -- Remove_Init_Call --
11041 ----------------------
11043 function Remove_Init_Call
11044 (Var : Entity_Id;
11045 Rep_Clause : Node_Id) return Node_Id
11047 Par : constant Node_Id := Parent (Var);
11048 Typ : constant Entity_Id := Etype (Var);
11050 Init_Proc : Entity_Id;
11051 -- Initialization procedure for Typ
11053 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
11054 -- Look for init call for Var starting at From and scanning the
11055 -- enclosing list until Rep_Clause or the end of the list is reached.
11057 ----------------------------
11058 -- Find_Init_Call_In_List --
11059 ----------------------------
11061 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
11062 Init_Call : Node_Id;
11064 begin
11065 Init_Call := From;
11066 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
11067 if Nkind (Init_Call) = N_Procedure_Call_Statement
11068 and then Is_Entity_Name (Name (Init_Call))
11069 and then Entity (Name (Init_Call)) = Init_Proc
11070 then
11071 return Init_Call;
11072 end if;
11074 Next (Init_Call);
11075 end loop;
11077 return Empty;
11078 end Find_Init_Call_In_List;
11080 Init_Call : Node_Id;
11082 -- Start of processing for Find_Init_Call
11084 begin
11085 if Present (Initialization_Statements (Var)) then
11086 Init_Call := Initialization_Statements (Var);
11087 Set_Initialization_Statements (Var, Empty);
11089 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
11091 -- No init proc for the type, so obviously no call to be found
11093 return Empty;
11095 else
11096 -- We might be able to handle other cases below by just properly
11097 -- setting Initialization_Statements at the point where the init proc
11098 -- call is generated???
11100 Init_Proc := Base_Init_Proc (Typ);
11102 -- First scan the list containing the declaration of Var
11104 Init_Call := Find_Init_Call_In_List (From => Next (Par));
11106 -- If not found, also look on Var's freeze actions list, if any,
11107 -- since the init call may have been moved there (case of an address
11108 -- clause applying to Var).
11110 if No (Init_Call) and then Present (Freeze_Node (Var)) then
11111 Init_Call :=
11112 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
11113 end if;
11115 -- If the initialization call has actuals that use the secondary
11116 -- stack, the call may have been wrapped into a temporary block, in
11117 -- which case the block itself has to be removed.
11119 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
11120 declare
11121 Blk : constant Node_Id := Next (Par);
11122 begin
11123 if Present
11124 (Find_Init_Call_In_List
11125 (First (Statements (Handled_Statement_Sequence (Blk)))))
11126 then
11127 Init_Call := Blk;
11128 end if;
11129 end;
11130 end if;
11131 end if;
11133 if Present (Init_Call) then
11134 Remove (Init_Call);
11135 end if;
11136 return Init_Call;
11137 end Remove_Init_Call;
11139 -------------------------
11140 -- Remove_Side_Effects --
11141 -------------------------
11143 procedure Remove_Side_Effects
11144 (Exp : Node_Id;
11145 Name_Req : Boolean := False;
11146 Renaming_Req : Boolean := False;
11147 Variable_Ref : Boolean := False;
11148 Related_Id : Entity_Id := Empty;
11149 Is_Low_Bound : Boolean := False;
11150 Is_High_Bound : Boolean := False;
11151 Check_Side_Effects : Boolean := True)
11153 function Build_Temporary
11154 (Loc : Source_Ptr;
11155 Id : Character;
11156 Related_Nod : Node_Id := Empty) return Entity_Id;
11157 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11158 -- is present (xxx is taken from the Chars field of Related_Nod),
11159 -- otherwise it generates an internal temporary. The created temporary
11160 -- entity is marked as internal.
11162 ---------------------
11163 -- Build_Temporary --
11164 ---------------------
11166 function Build_Temporary
11167 (Loc : Source_Ptr;
11168 Id : Character;
11169 Related_Nod : Node_Id := Empty) return Entity_Id
11171 Temp_Id : Entity_Id;
11172 Temp_Nam : Name_Id;
11174 begin
11175 -- The context requires an external symbol
11177 if Present (Related_Id) then
11178 if Is_Low_Bound then
11179 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
11180 else pragma Assert (Is_High_Bound);
11181 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
11182 end if;
11184 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
11186 -- Otherwise generate an internal temporary
11188 else
11189 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
11190 end if;
11192 Set_Is_Internal (Temp_Id);
11194 return Temp_Id;
11195 end Build_Temporary;
11197 -- Local variables
11199 Loc : constant Source_Ptr := Sloc (Exp);
11200 Exp_Type : constant Entity_Id := Etype (Exp);
11201 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
11202 Def_Id : Entity_Id;
11203 E : Node_Id;
11204 New_Exp : Node_Id;
11205 Ptr_Typ_Decl : Node_Id;
11206 Ref_Type : Entity_Id;
11207 Res : Node_Id;
11209 -- Start of processing for Remove_Side_Effects
11211 begin
11212 -- Handle cases in which there is nothing to do. In GNATprove mode,
11213 -- removal of side effects is useful for the light expansion of
11214 -- renamings. This removal should only occur when not inside a
11215 -- generic and not doing a preanalysis.
11217 if not Expander_Active
11218 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
11219 then
11220 return;
11222 -- Cannot generate temporaries if the invocation to remove side effects
11223 -- was issued too early and the type of the expression is not resolved
11224 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11225 -- Remove_Side_Effects).
11227 elsif No (Exp_Type)
11228 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11229 then
11230 return;
11232 -- Nothing to do if prior expansion determined that a function call does
11233 -- not require side effect removal.
11235 elsif Nkind (Exp) = N_Function_Call
11236 and then No_Side_Effect_Removal (Exp)
11237 then
11238 return;
11240 -- No action needed for side-effect free expressions
11242 elsif Check_Side_Effects
11243 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11244 then
11245 return;
11247 -- Generating C code we cannot remove side effect of function returning
11248 -- class-wide types since there is no secondary stack (required to use
11249 -- 'reference).
11251 elsif Modify_Tree_For_C
11252 and then Nkind (Exp) = N_Function_Call
11253 and then Is_Class_Wide_Type (Etype (Exp))
11254 then
11255 return;
11256 end if;
11258 -- The remaining processing is done with all checks suppressed
11260 -- Note: from now on, don't use return statements, instead do a goto
11261 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11263 Scope_Suppress.Suppress := (others => True);
11265 -- If this is an elementary or a small not-by-reference record type, and
11266 -- we need to capture the value, just make a constant; this is cheap and
11267 -- objects of both kinds of types can be bit aligned, so it might not be
11268 -- possible to generate a reference to them. Likewise if this is not a
11269 -- name reference, except for a type conversion, because we would enter
11270 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11271 -- type has predicates (and type conversions need a specific treatment
11272 -- anyway, see below). Also do it if we have a volatile reference and
11273 -- Name_Req is not set (see comments for Side_Effect_Free).
11275 if (Is_Elementary_Type (Exp_Type)
11276 or else (Is_Record_Type (Exp_Type)
11277 and then Known_Static_RM_Size (Exp_Type)
11278 and then RM_Size (Exp_Type) <= 64
11279 and then not Has_Discriminants (Exp_Type)
11280 and then not Is_By_Reference_Type (Exp_Type)))
11281 and then (Variable_Ref
11282 or else (not Is_Name_Reference (Exp)
11283 and then Nkind (Exp) /= N_Type_Conversion)
11284 or else (not Name_Req
11285 and then Is_Volatile_Reference (Exp)))
11286 then
11287 Def_Id := Build_Temporary (Loc, 'R', Exp);
11288 Set_Etype (Def_Id, Exp_Type);
11289 Res := New_Occurrence_Of (Def_Id, Loc);
11291 -- If the expression is a packed reference, it must be reanalyzed and
11292 -- expanded, depending on context. This is the case for actuals where
11293 -- a constraint check may capture the actual before expansion of the
11294 -- call is complete.
11296 if Nkind (Exp) = N_Indexed_Component
11297 and then Is_Packed (Etype (Prefix (Exp)))
11298 then
11299 Set_Analyzed (Exp, False);
11300 Set_Analyzed (Prefix (Exp), False);
11301 end if;
11303 -- Generate:
11304 -- Rnn : Exp_Type renames Expr;
11306 if Renaming_Req then
11307 E :=
11308 Make_Object_Renaming_Declaration (Loc,
11309 Defining_Identifier => Def_Id,
11310 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11311 Name => Relocate_Node (Exp));
11313 -- Generate:
11314 -- Rnn : constant Exp_Type := Expr;
11316 else
11317 E :=
11318 Make_Object_Declaration (Loc,
11319 Defining_Identifier => Def_Id,
11320 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11321 Constant_Present => True,
11322 Expression => Relocate_Node (Exp));
11324 Set_Assignment_OK (E);
11325 end if;
11327 Insert_Action (Exp, E);
11329 -- If the expression has the form v.all then we can just capture the
11330 -- pointer, and then do an explicit dereference on the result, but
11331 -- this is not right if this is a volatile reference.
11333 elsif Nkind (Exp) = N_Explicit_Dereference
11334 and then not Is_Volatile_Reference (Exp)
11335 then
11336 Def_Id := Build_Temporary (Loc, 'R', Exp);
11337 Res :=
11338 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11340 Insert_Action (Exp,
11341 Make_Object_Declaration (Loc,
11342 Defining_Identifier => Def_Id,
11343 Object_Definition =>
11344 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11345 Constant_Present => True,
11346 Expression => Relocate_Node (Prefix (Exp))));
11348 -- Similar processing for an unchecked conversion of an expression of
11349 -- the form v.all, where we want the same kind of treatment.
11351 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11352 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11353 then
11354 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11355 goto Leave;
11357 -- If this is a type conversion, leave the type conversion and remove
11358 -- the side effects in the expression. This is important in several
11359 -- circumstances: for change of representations, and also when this is a
11360 -- view conversion to a smaller object, where gigi can end up creating
11361 -- its own temporary of the wrong size.
11363 elsif Nkind (Exp) = N_Type_Conversion then
11364 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11366 -- Generating C code the type conversion of an access to constrained
11367 -- array type into an access to unconstrained array type involves
11368 -- initializing a fat pointer and the expression must be free of
11369 -- side effects to safely compute its bounds.
11371 if Modify_Tree_For_C
11372 and then Is_Access_Type (Etype (Exp))
11373 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11374 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11375 then
11376 Def_Id := Build_Temporary (Loc, 'R', Exp);
11377 Set_Etype (Def_Id, Exp_Type);
11378 Res := New_Occurrence_Of (Def_Id, Loc);
11380 Insert_Action (Exp,
11381 Make_Object_Declaration (Loc,
11382 Defining_Identifier => Def_Id,
11383 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11384 Constant_Present => True,
11385 Expression => Relocate_Node (Exp)));
11386 else
11387 goto Leave;
11388 end if;
11390 -- If this is an unchecked conversion that Gigi can't handle, make
11391 -- a copy or a use a renaming to capture the value.
11393 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11394 and then not Safe_Unchecked_Type_Conversion (Exp)
11395 then
11396 if CW_Or_Has_Controlled_Part (Exp_Type) then
11398 -- Use a renaming to capture the expression, rather than create
11399 -- a controlled temporary.
11401 Def_Id := Build_Temporary (Loc, 'R', Exp);
11402 Res := New_Occurrence_Of (Def_Id, Loc);
11404 Insert_Action (Exp,
11405 Make_Object_Renaming_Declaration (Loc,
11406 Defining_Identifier => Def_Id,
11407 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11408 Name => Relocate_Node (Exp)));
11410 else
11411 Def_Id := Build_Temporary (Loc, 'R', Exp);
11412 Set_Etype (Def_Id, Exp_Type);
11413 Res := New_Occurrence_Of (Def_Id, Loc);
11415 E :=
11416 Make_Object_Declaration (Loc,
11417 Defining_Identifier => Def_Id,
11418 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11419 Constant_Present => not Is_Variable (Exp),
11420 Expression => Relocate_Node (Exp));
11422 Set_Assignment_OK (E);
11423 Insert_Action (Exp, E);
11424 end if;
11426 -- For expressions that denote names, we can use a renaming scheme.
11427 -- This is needed for correctness in the case of a volatile object of
11428 -- a non-volatile type because the Make_Reference call of the "default"
11429 -- approach would generate an illegal access value (an access value
11430 -- cannot designate such an object - see Analyze_Reference).
11432 elsif Is_Name_Reference (Exp)
11434 -- We skip using this scheme if we have an object of a volatile
11435 -- type and we do not have Name_Req set true (see comments for
11436 -- Side_Effect_Free).
11438 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11439 then
11440 Def_Id := Build_Temporary (Loc, 'R', Exp);
11441 Res := New_Occurrence_Of (Def_Id, Loc);
11443 Insert_Action (Exp,
11444 Make_Object_Renaming_Declaration (Loc,
11445 Defining_Identifier => Def_Id,
11446 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11447 Name => Relocate_Node (Exp)));
11449 -- If this is a packed reference, or a selected component with
11450 -- a non-standard representation, a reference to the temporary
11451 -- will be replaced by a copy of the original expression (see
11452 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11453 -- elaborated by gigi, and is of course not to be replaced in-line
11454 -- by the expression it renames, which would defeat the purpose of
11455 -- removing the side effect.
11457 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11458 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11459 then
11460 null;
11461 else
11462 Set_Is_Renaming_Of_Object (Def_Id, False);
11463 end if;
11465 -- Avoid generating a variable-sized temporary, by generating the
11466 -- reference just for the function call. The transformation could be
11467 -- refined to apply only when the array component is constrained by a
11468 -- discriminant???
11470 elsif Nkind (Exp) = N_Selected_Component
11471 and then Nkind (Prefix (Exp)) = N_Function_Call
11472 and then Is_Array_Type (Exp_Type)
11473 then
11474 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11475 goto Leave;
11477 -- Otherwise we generate a reference to the expression
11479 else
11480 -- An expression which is in SPARK mode is considered side effect
11481 -- free if the resulting value is captured by a variable or a
11482 -- constant.
11484 if GNATprove_Mode
11485 and then Nkind (Parent (Exp)) = N_Object_Declaration
11486 then
11487 goto Leave;
11489 -- When generating C code we cannot consider side effect free object
11490 -- declarations that have discriminants and are initialized by means
11491 -- of a function call since on this target there is no secondary
11492 -- stack to store the return value and the expander may generate an
11493 -- extra call to the function to compute the discriminant value. In
11494 -- addition, for targets that have secondary stack, the expansion of
11495 -- functions with side effects involves the generation of an access
11496 -- type to capture the return value stored in the secondary stack;
11497 -- by contrast when generating C code such expansion generates an
11498 -- internal object declaration (no access type involved) which must
11499 -- be identified here to avoid entering into a never-ending loop
11500 -- generating internal object declarations.
11502 elsif Modify_Tree_For_C
11503 and then Nkind (Parent (Exp)) = N_Object_Declaration
11504 and then
11505 (Nkind (Exp) /= N_Function_Call
11506 or else not Has_Discriminants (Exp_Type)
11507 or else Is_Internal_Name
11508 (Chars (Defining_Identifier (Parent (Exp)))))
11509 then
11510 goto Leave;
11511 end if;
11513 -- Special processing for function calls that return a limited type.
11514 -- We need to build a declaration that will enable build-in-place
11515 -- expansion of the call. This is not done if the context is already
11516 -- an object declaration, to prevent infinite recursion.
11518 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11519 -- to accommodate functions returning limited objects by reference.
11521 if Ada_Version >= Ada_2005
11522 and then Nkind (Exp) = N_Function_Call
11523 and then Is_Limited_View (Etype (Exp))
11524 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11525 then
11526 declare
11527 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11528 Decl : Node_Id;
11530 begin
11531 Decl :=
11532 Make_Object_Declaration (Loc,
11533 Defining_Identifier => Obj,
11534 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11535 Expression => Relocate_Node (Exp));
11537 Insert_Action (Exp, Decl);
11538 Set_Etype (Obj, Exp_Type);
11539 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11540 goto Leave;
11541 end;
11542 end if;
11544 Def_Id := Build_Temporary (Loc, 'R', Exp);
11546 -- The regular expansion of functions with side effects involves the
11547 -- generation of an access type to capture the return value found on
11548 -- the secondary stack. Since SPARK (and why) cannot process access
11549 -- types, use a different approach which ignores the secondary stack
11550 -- and "copies" the returned object.
11551 -- When generating C code, no need for a 'reference since the
11552 -- secondary stack is not supported.
11554 if GNATprove_Mode or Modify_Tree_For_C then
11555 Res := New_Occurrence_Of (Def_Id, Loc);
11556 Ref_Type := Exp_Type;
11558 -- Regular expansion utilizing an access type and 'reference
11560 else
11561 Res :=
11562 Make_Explicit_Dereference (Loc,
11563 Prefix => New_Occurrence_Of (Def_Id, Loc));
11565 -- Generate:
11566 -- type Ann is access all <Exp_Type>;
11568 Ref_Type := Make_Temporary (Loc, 'A');
11570 Ptr_Typ_Decl :=
11571 Make_Full_Type_Declaration (Loc,
11572 Defining_Identifier => Ref_Type,
11573 Type_Definition =>
11574 Make_Access_To_Object_Definition (Loc,
11575 All_Present => True,
11576 Subtype_Indication =>
11577 New_Occurrence_Of (Exp_Type, Loc)));
11579 Insert_Action (Exp, Ptr_Typ_Decl);
11580 end if;
11582 E := Exp;
11583 if Nkind (E) = N_Explicit_Dereference then
11584 New_Exp := Relocate_Node (Prefix (E));
11586 else
11587 E := Relocate_Node (E);
11589 -- Do not generate a 'reference in SPARK mode or C generation
11590 -- since the access type is not created in the first place.
11592 if GNATprove_Mode or Modify_Tree_For_C then
11593 New_Exp := E;
11595 -- Otherwise generate reference, marking the value as non-null
11596 -- since we know it cannot be null and we don't want a check.
11598 else
11599 New_Exp := Make_Reference (Loc, E);
11600 Set_Is_Known_Non_Null (Def_Id);
11601 end if;
11602 end if;
11604 if Is_Delayed_Aggregate (E) then
11606 -- The expansion of nested aggregates is delayed until the
11607 -- enclosing aggregate is expanded. As aggregates are often
11608 -- qualified, the predicate applies to qualified expressions as
11609 -- well, indicating that the enclosing aggregate has not been
11610 -- expanded yet. At this point the aggregate is part of a
11611 -- stand-alone declaration, and must be fully expanded.
11613 if Nkind (E) = N_Qualified_Expression then
11614 Set_Expansion_Delayed (Expression (E), False);
11615 Set_Analyzed (Expression (E), False);
11616 else
11617 Set_Expansion_Delayed (E, False);
11618 end if;
11620 Set_Analyzed (E, False);
11621 end if;
11623 -- Generating C code of object declarations that have discriminants
11624 -- and are initialized by means of a function call we propagate the
11625 -- discriminants of the parent type to the internally built object.
11626 -- This is needed to avoid generating an extra call to the called
11627 -- function.
11629 -- For example, if we generate here the following declaration, it
11630 -- will be expanded later adding an extra call to evaluate the value
11631 -- of the discriminant (needed to compute the size of the object).
11633 -- type Rec (D : Integer) is ...
11634 -- Obj : constant Rec := SomeFunc;
11636 if Modify_Tree_For_C
11637 and then Nkind (Parent (Exp)) = N_Object_Declaration
11638 and then Has_Discriminants (Exp_Type)
11639 and then Nkind (Exp) = N_Function_Call
11640 then
11641 Insert_Action (Exp,
11642 Make_Object_Declaration (Loc,
11643 Defining_Identifier => Def_Id,
11644 Object_Definition => New_Copy_Tree
11645 (Object_Definition (Parent (Exp))),
11646 Constant_Present => True,
11647 Expression => New_Exp));
11648 else
11649 Insert_Action (Exp,
11650 Make_Object_Declaration (Loc,
11651 Defining_Identifier => Def_Id,
11652 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11653 Constant_Present => True,
11654 Expression => New_Exp));
11655 end if;
11656 end if;
11658 -- Preserve the Assignment_OK flag in all copies, since at least one
11659 -- copy may be used in a context where this flag must be set (otherwise
11660 -- why would the flag be set in the first place).
11662 Set_Assignment_OK (Res, Assignment_OK (Exp));
11664 -- Finally rewrite the original expression and we are done
11666 Rewrite (Exp, Res);
11667 Analyze_And_Resolve (Exp, Exp_Type);
11669 <<Leave>>
11670 Scope_Suppress := Svg_Suppress;
11671 end Remove_Side_Effects;
11673 ------------------------
11674 -- Replace_References --
11675 ------------------------
11677 procedure Replace_References
11678 (Expr : Node_Id;
11679 Par_Typ : Entity_Id;
11680 Deriv_Typ : Entity_Id;
11681 Par_Obj : Entity_Id := Empty;
11682 Deriv_Obj : Entity_Id := Empty)
11684 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11685 -- Determine whether node Ref denotes some component of Deriv_Obj
11687 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11688 -- Substitute a reference to an entity with the corresponding value
11689 -- stored in table Type_Map.
11691 function Type_Of_Formal
11692 (Call : Node_Id;
11693 Actual : Node_Id) return Entity_Id;
11694 -- Find the type of the formal parameter which corresponds to actual
11695 -- parameter Actual in subprogram call Call.
11697 ----------------------
11698 -- Is_Deriv_Obj_Ref --
11699 ----------------------
11701 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11702 Par : constant Node_Id := Parent (Ref);
11704 begin
11705 -- Detect the folowing selected component form:
11707 -- Deriv_Obj.(something)
11709 return
11710 Nkind (Par) = N_Selected_Component
11711 and then Is_Entity_Name (Prefix (Par))
11712 and then Entity (Prefix (Par)) = Deriv_Obj;
11713 end Is_Deriv_Obj_Ref;
11715 -----------------
11716 -- Replace_Ref --
11717 -----------------
11719 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11720 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11721 -- Reset the Controlling_Argument of all function calls that
11722 -- encapsulate node From_Arg.
11724 ----------------------------------
11725 -- Remove_Controlling_Arguments --
11726 ----------------------------------
11728 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11729 Par : Node_Id;
11731 begin
11732 Par := From_Arg;
11733 while Present (Par) loop
11734 if Nkind (Par) = N_Function_Call
11735 and then Present (Controlling_Argument (Par))
11736 then
11737 Set_Controlling_Argument (Par, Empty);
11739 -- Prevent the search from going too far
11741 elsif Is_Body_Or_Package_Declaration (Par) then
11742 exit;
11743 end if;
11745 Par := Parent (Par);
11746 end loop;
11747 end Remove_Controlling_Arguments;
11749 -- Local variables
11751 Context : constant Node_Id := Parent (Ref);
11752 Loc : constant Source_Ptr := Sloc (Ref);
11753 Ref_Id : Entity_Id;
11754 Result : Traverse_Result;
11756 New_Ref : Node_Id;
11757 -- The new reference which is intended to substitute the old one
11759 Old_Ref : Node_Id;
11760 -- The reference designated for replacement. In certain cases this
11761 -- may be a node other than Ref.
11763 Val : Node_Or_Entity_Id;
11764 -- The corresponding value of Ref from the type map
11766 -- Start of processing for Replace_Ref
11768 begin
11769 -- Assume that the input reference is to be replaced and that the
11770 -- traversal should examine the children of the reference.
11772 Old_Ref := Ref;
11773 Result := OK;
11775 -- The input denotes a meaningful reference
11777 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11778 Ref_Id := Entity (Ref);
11779 Val := Type_Map.Get (Ref_Id);
11781 -- The reference has a corresponding value in the type map, a
11782 -- substitution is possible.
11784 if Present (Val) then
11786 -- The reference denotes a discriminant
11788 if Ekind (Ref_Id) = E_Discriminant then
11789 if Nkind (Val) in N_Entity then
11791 -- The value denotes another discriminant. Replace as
11792 -- follows:
11794 -- _object.Discr -> _object.Val
11796 if Ekind (Val) = E_Discriminant then
11797 New_Ref := New_Occurrence_Of (Val, Loc);
11799 -- Otherwise the value denotes the entity of a name which
11800 -- constraints the discriminant. Replace as follows:
11802 -- _object.Discr -> Val
11804 else
11805 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11807 New_Ref := New_Occurrence_Of (Val, Loc);
11808 Old_Ref := Parent (Old_Ref);
11809 end if;
11811 -- Otherwise the value denotes an arbitrary expression which
11812 -- constraints the discriminant. Replace as follows:
11814 -- _object.Discr -> Val
11816 else
11817 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11819 New_Ref := New_Copy_Tree (Val);
11820 Old_Ref := Parent (Old_Ref);
11821 end if;
11823 -- Otherwise the reference denotes a primitive. Replace as
11824 -- follows:
11826 -- Primitive -> Val
11828 else
11829 pragma Assert (Nkind (Val) in N_Entity);
11830 New_Ref := New_Occurrence_Of (Val, Loc);
11831 end if;
11833 -- The reference mentions the _object parameter of the parent
11834 -- type's DIC or type invariant procedure. Replace as follows:
11836 -- _object -> _object
11838 elsif Present (Par_Obj)
11839 and then Present (Deriv_Obj)
11840 and then Ref_Id = Par_Obj
11841 then
11842 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11844 -- The type of the _object parameter is class-wide when the
11845 -- expression comes from an assertion pragma that applies to
11846 -- an abstract parent type or an interface. The class-wide type
11847 -- facilitates the preanalysis of the expression by treating
11848 -- calls to abstract primitives that mention the current
11849 -- instance of the type as dispatching. Once the calls are
11850 -- remapped to invoke overriding or inherited primitives, the
11851 -- calls no longer need to be dispatching. Examine all function
11852 -- calls that encapsulate the _object parameter and reset their
11853 -- Controlling_Argument attribute.
11855 if Is_Class_Wide_Type (Etype (Par_Obj))
11856 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11857 then
11858 Remove_Controlling_Arguments (Old_Ref);
11859 end if;
11861 -- The reference to _object acts as an actual parameter in a
11862 -- subprogram call which may be invoking a primitive of the
11863 -- parent type:
11865 -- Primitive (... _object ...);
11867 -- The parent type primitive may not be overridden nor
11868 -- inherited when it is declared after the derived type
11869 -- definition:
11871 -- type Parent is tagged private;
11872 -- type Child is new Parent with private;
11873 -- procedure Primitive (Obj : Parent);
11875 -- In this scenario the _object parameter is converted to the
11876 -- parent type. Due to complications with partial/full views
11877 -- and view swaps, the parent type is taken from the formal
11878 -- parameter of the subprogram being called.
11880 if Nkind_In (Context, N_Function_Call,
11881 N_Procedure_Call_Statement)
11882 and then No (Type_Map.Get (Entity (Name (Context))))
11883 then
11884 New_Ref :=
11885 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11887 -- Do not process the generated type conversion because
11888 -- both the parent type and the derived type are in the
11889 -- Type_Map table. This will clobber the type conversion
11890 -- by resetting its subtype mark.
11892 Result := Skip;
11893 end if;
11895 -- Otherwise there is nothing to replace
11897 else
11898 New_Ref := Empty;
11899 end if;
11901 if Present (New_Ref) then
11902 Rewrite (Old_Ref, New_Ref);
11904 -- Update the return type when the context of the reference
11905 -- acts as the name of a function call. Note that the update
11906 -- should not be performed when the reference appears as an
11907 -- actual in the call.
11909 if Nkind (Context) = N_Function_Call
11910 and then Name (Context) = Old_Ref
11911 then
11912 Set_Etype (Context, Etype (Val));
11913 end if;
11914 end if;
11915 end if;
11917 -- Reanalyze the reference due to potential replacements
11919 if Nkind (Old_Ref) in N_Has_Etype then
11920 Set_Analyzed (Old_Ref, False);
11921 end if;
11923 return Result;
11924 end Replace_Ref;
11926 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11928 --------------------
11929 -- Type_Of_Formal --
11930 --------------------
11932 function Type_Of_Formal
11933 (Call : Node_Id;
11934 Actual : Node_Id) return Entity_Id
11936 A : Node_Id;
11937 F : Entity_Id;
11939 begin
11940 -- Examine the list of actual and formal parameters in parallel
11942 A := First (Parameter_Associations (Call));
11943 F := First_Formal (Entity (Name (Call)));
11944 while Present (A) and then Present (F) loop
11945 if A = Actual then
11946 return Etype (F);
11947 end if;
11949 Next (A);
11950 Next_Formal (F);
11951 end loop;
11953 -- The actual parameter must always have a corresponding formal
11955 pragma Assert (False);
11957 return Empty;
11958 end Type_Of_Formal;
11960 -- Start of processing for Replace_References
11962 begin
11963 -- Map the attributes of the parent type to the proper corresponding
11964 -- attributes of the derived type.
11966 Map_Types
11967 (Parent_Type => Par_Typ,
11968 Derived_Type => Deriv_Typ);
11970 -- Inspect the input expression and perform substitutions where
11971 -- necessary.
11973 Replace_Refs (Expr);
11974 end Replace_References;
11976 -----------------------------
11977 -- Replace_Type_References --
11978 -----------------------------
11980 procedure Replace_Type_References
11981 (Expr : Node_Id;
11982 Typ : Entity_Id;
11983 Obj_Id : Entity_Id)
11985 procedure Replace_Type_Ref (N : Node_Id);
11986 -- Substitute a single reference of the current instance of type Typ
11987 -- with a reference to Obj_Id.
11989 ----------------------
11990 -- Replace_Type_Ref --
11991 ----------------------
11993 procedure Replace_Type_Ref (N : Node_Id) is
11994 begin
11995 -- Decorate the reference to Typ even though it may be rewritten
11996 -- further down. This is done for two reasons:
11998 -- * ASIS has all necessary semantic information in the original
11999 -- tree.
12001 -- * Routines which examine properties of the Original_Node have
12002 -- some semantic information.
12004 if Nkind (N) = N_Identifier then
12005 Set_Entity (N, Typ);
12006 Set_Etype (N, Typ);
12008 elsif Nkind (N) = N_Selected_Component then
12009 Analyze (Prefix (N));
12010 Set_Entity (Selector_Name (N), Typ);
12011 Set_Etype (Selector_Name (N), Typ);
12012 end if;
12014 -- Perform the following substitution:
12016 -- Typ --> _object
12018 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
12019 Set_Comes_From_Source (N, True);
12020 end Replace_Type_Ref;
12022 procedure Replace_Type_Refs is
12023 new Replace_Type_References_Generic (Replace_Type_Ref);
12025 -- Start of processing for Replace_Type_References
12027 begin
12028 Replace_Type_Refs (Expr, Typ);
12029 end Replace_Type_References;
12031 ---------------------------
12032 -- Represented_As_Scalar --
12033 ---------------------------
12035 function Represented_As_Scalar (T : Entity_Id) return Boolean is
12036 UT : constant Entity_Id := Underlying_Type (T);
12037 begin
12038 return Is_Scalar_Type (UT)
12039 or else (Is_Bit_Packed_Array (UT)
12040 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
12041 end Represented_As_Scalar;
12043 ------------------------------
12044 -- Requires_Cleanup_Actions --
12045 ------------------------------
12047 function Requires_Cleanup_Actions
12048 (N : Node_Id;
12049 Lib_Level : Boolean) return Boolean
12051 At_Lib_Level : constant Boolean :=
12052 Lib_Level
12053 and then Nkind_In (N, N_Package_Body,
12054 N_Package_Specification);
12055 -- N is at the library level if the top-most context is a package and
12056 -- the path taken to reach N does not inlcude non-package constructs.
12058 begin
12059 case Nkind (N) is
12060 when N_Accept_Statement
12061 | N_Block_Statement
12062 | N_Entry_Body
12063 | N_Package_Body
12064 | N_Protected_Body
12065 | N_Subprogram_Body
12066 | N_Task_Body
12068 return
12069 Requires_Cleanup_Actions
12070 (L => Declarations (N),
12071 Lib_Level => At_Lib_Level,
12072 Nested_Constructs => True)
12073 or else
12074 (Present (Handled_Statement_Sequence (N))
12075 and then
12076 Requires_Cleanup_Actions
12077 (L =>
12078 Statements (Handled_Statement_Sequence (N)),
12079 Lib_Level => At_Lib_Level,
12080 Nested_Constructs => True));
12082 -- Extended return statements are the same as the above, except that
12083 -- there is no Declarations field. We do not want to clean up the
12084 -- Return_Object_Declarations.
12086 when N_Extended_Return_Statement =>
12087 return
12088 Present (Handled_Statement_Sequence (N))
12089 and then Requires_Cleanup_Actions
12090 (L =>
12091 Statements (Handled_Statement_Sequence (N)),
12092 Lib_Level => At_Lib_Level,
12093 Nested_Constructs => True);
12095 when N_Package_Specification =>
12096 return
12097 Requires_Cleanup_Actions
12098 (L => Visible_Declarations (N),
12099 Lib_Level => At_Lib_Level,
12100 Nested_Constructs => True)
12101 or else
12102 Requires_Cleanup_Actions
12103 (L => Private_Declarations (N),
12104 Lib_Level => At_Lib_Level,
12105 Nested_Constructs => True);
12107 when others =>
12108 raise Program_Error;
12109 end case;
12110 end Requires_Cleanup_Actions;
12112 ------------------------------
12113 -- Requires_Cleanup_Actions --
12114 ------------------------------
12116 function Requires_Cleanup_Actions
12117 (L : List_Id;
12118 Lib_Level : Boolean;
12119 Nested_Constructs : Boolean) return Boolean
12121 Decl : Node_Id;
12122 Expr : Node_Id;
12123 Obj_Id : Entity_Id;
12124 Obj_Typ : Entity_Id;
12125 Pack_Id : Entity_Id;
12126 Typ : Entity_Id;
12128 begin
12129 if No (L)
12130 or else Is_Empty_List (L)
12131 then
12132 return False;
12133 end if;
12135 Decl := First (L);
12136 while Present (Decl) loop
12138 -- Library-level tagged types
12140 if Nkind (Decl) = N_Full_Type_Declaration then
12141 Typ := Defining_Identifier (Decl);
12143 -- Ignored Ghost types do not need any cleanup actions because
12144 -- they will not appear in the final tree.
12146 if Is_Ignored_Ghost_Entity (Typ) then
12147 null;
12149 elsif Is_Tagged_Type (Typ)
12150 and then Is_Library_Level_Entity (Typ)
12151 and then Convention (Typ) = Convention_Ada
12152 and then Present (Access_Disp_Table (Typ))
12153 and then RTE_Available (RE_Unregister_Tag)
12154 and then not Is_Abstract_Type (Typ)
12155 and then not No_Run_Time_Mode
12156 then
12157 return True;
12158 end if;
12160 -- Regular object declarations
12162 elsif Nkind (Decl) = N_Object_Declaration then
12163 Obj_Id := Defining_Identifier (Decl);
12164 Obj_Typ := Base_Type (Etype (Obj_Id));
12165 Expr := Expression (Decl);
12167 -- Bypass any form of processing for objects which have their
12168 -- finalization disabled. This applies only to objects at the
12169 -- library level.
12171 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12172 null;
12174 -- Finalization of transient objects are treated separately in
12175 -- order to handle sensitive cases. These include:
12177 -- * Aggregate expansion
12178 -- * If, case, and expression with actions expansion
12179 -- * Transient scopes
12181 -- If one of those contexts has marked the transient object as
12182 -- ignored, do not generate finalization actions for it.
12184 elsif Is_Finalized_Transient (Obj_Id)
12185 or else Is_Ignored_Transient (Obj_Id)
12186 then
12187 null;
12189 -- Ignored Ghost objects do not need any cleanup actions because
12190 -- they will not appear in the final tree.
12192 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12193 null;
12195 -- The object is of the form:
12196 -- Obj : [constant] Typ [:= Expr];
12198 -- Do not process tag-to-class-wide conversions because they do
12199 -- not yield an object. Do not process the incomplete view of a
12200 -- deferred constant. Note that an object initialized by means
12201 -- of a build-in-place function call may appear as a deferred
12202 -- constant after expansion activities. These kinds of objects
12203 -- must be finalized.
12205 elsif not Is_Imported (Obj_Id)
12206 and then Needs_Finalization (Obj_Typ)
12207 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
12208 and then not (Ekind (Obj_Id) = E_Constant
12209 and then not Has_Completion (Obj_Id)
12210 and then No (BIP_Initialization_Call (Obj_Id)))
12211 then
12212 return True;
12214 -- The object is of the form:
12215 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12217 -- Obj : Access_Typ :=
12218 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12220 elsif Is_Access_Type (Obj_Typ)
12221 and then Needs_Finalization
12222 (Available_View (Designated_Type (Obj_Typ)))
12223 and then Present (Expr)
12224 and then
12225 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12226 or else
12227 (Is_Non_BIP_Func_Call (Expr)
12228 and then not Is_Related_To_Func_Return (Obj_Id)))
12229 then
12230 return True;
12232 -- Processing for "hook" objects generated for transient objects
12233 -- declared inside an Expression_With_Actions.
12235 elsif Is_Access_Type (Obj_Typ)
12236 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12237 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12238 N_Object_Declaration
12239 then
12240 return True;
12242 -- Processing for intermediate results of if expressions where
12243 -- one of the alternatives uses a controlled function call.
12245 elsif Is_Access_Type (Obj_Typ)
12246 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12247 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12248 N_Defining_Identifier
12249 and then Present (Expr)
12250 and then Nkind (Expr) = N_Null
12251 then
12252 return True;
12254 -- Simple protected objects which use type System.Tasking.
12255 -- Protected_Objects.Protection to manage their locks should be
12256 -- treated as controlled since they require manual cleanup.
12258 elsif Ekind (Obj_Id) = E_Variable
12259 and then (Is_Simple_Protected_Type (Obj_Typ)
12260 or else Has_Simple_Protected_Object (Obj_Typ))
12261 then
12262 return True;
12263 end if;
12265 -- Specific cases of object renamings
12267 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12268 Obj_Id := Defining_Identifier (Decl);
12269 Obj_Typ := Base_Type (Etype (Obj_Id));
12271 -- Bypass any form of processing for objects which have their
12272 -- finalization disabled. This applies only to objects at the
12273 -- library level.
12275 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12276 null;
12278 -- Ignored Ghost object renamings do not need any cleanup actions
12279 -- because they will not appear in the final tree.
12281 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12282 null;
12284 -- Return object of a build-in-place function. This case is
12285 -- recognized and marked by the expansion of an extended return
12286 -- statement (see Expand_N_Extended_Return_Statement).
12288 elsif Needs_Finalization (Obj_Typ)
12289 and then Is_Return_Object (Obj_Id)
12290 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12291 then
12292 return True;
12294 -- Detect a case where a source object has been initialized by
12295 -- a controlled function call or another object which was later
12296 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12298 -- Obj1 : CW_Type := Src_Obj;
12299 -- Obj2 : CW_Type := Function_Call (...);
12301 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12302 -- Tmp : ... := Function_Call (...)'reference;
12303 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12305 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12306 return True;
12307 end if;
12309 -- Inspect the freeze node of an access-to-controlled type and look
12310 -- for a delayed finalization master. This case arises when the
12311 -- freeze actions are inserted at a later time than the expansion of
12312 -- the context. Since Build_Finalizer is never called on a single
12313 -- construct twice, the master will be ultimately left out and never
12314 -- finalized. This is also needed for freeze actions of designated
12315 -- types themselves, since in some cases the finalization master is
12316 -- associated with a designated type's freeze node rather than that
12317 -- of the access type (see handling for freeze actions in
12318 -- Build_Finalization_Master).
12320 elsif Nkind (Decl) = N_Freeze_Entity
12321 and then Present (Actions (Decl))
12322 then
12323 Typ := Entity (Decl);
12325 -- Freeze nodes for ignored Ghost types do not need cleanup
12326 -- actions because they will never appear in the final tree.
12328 if Is_Ignored_Ghost_Entity (Typ) then
12329 null;
12331 elsif ((Is_Access_Type (Typ)
12332 and then not Is_Access_Subprogram_Type (Typ)
12333 and then Needs_Finalization
12334 (Available_View (Designated_Type (Typ))))
12335 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12336 and then Requires_Cleanup_Actions
12337 (Actions (Decl), Lib_Level, Nested_Constructs)
12338 then
12339 return True;
12340 end if;
12342 -- Nested package declarations
12344 elsif Nested_Constructs
12345 and then Nkind (Decl) = N_Package_Declaration
12346 then
12347 Pack_Id := Defining_Entity (Decl);
12349 -- Do not inspect an ignored Ghost package because all code found
12350 -- within will not appear in the final tree.
12352 if Is_Ignored_Ghost_Entity (Pack_Id) then
12353 null;
12355 elsif Ekind (Pack_Id) /= E_Generic_Package
12356 and then Requires_Cleanup_Actions
12357 (Specification (Decl), Lib_Level)
12358 then
12359 return True;
12360 end if;
12362 -- Nested package bodies
12364 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12366 -- Do not inspect an ignored Ghost package body because all code
12367 -- found within will not appear in the final tree.
12369 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12370 null;
12372 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12373 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12374 then
12375 return True;
12376 end if;
12378 elsif Nkind (Decl) = N_Block_Statement
12379 and then
12381 -- Handle a rare case caused by a controlled transient object
12382 -- created as part of a record init proc. The variable is wrapped
12383 -- in a block, but the block is not associated with a transient
12384 -- scope.
12386 (Inside_Init_Proc
12388 -- Handle the case where the original context has been wrapped in
12389 -- a block to avoid interference between exception handlers and
12390 -- At_End handlers. Treat the block as transparent and process its
12391 -- contents.
12393 or else Is_Finalization_Wrapper (Decl))
12394 then
12395 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12396 return True;
12397 end if;
12398 end if;
12400 Next (Decl);
12401 end loop;
12403 return False;
12404 end Requires_Cleanup_Actions;
12406 ------------------------------------
12407 -- Safe_Unchecked_Type_Conversion --
12408 ------------------------------------
12410 -- Note: this function knows quite a bit about the exact requirements of
12411 -- Gigi with respect to unchecked type conversions, and its code must be
12412 -- coordinated with any changes in Gigi in this area.
12414 -- The above requirements should be documented in Sinfo ???
12416 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12417 Otyp : Entity_Id;
12418 Ityp : Entity_Id;
12419 Oalign : Uint;
12420 Ialign : Uint;
12421 Pexp : constant Node_Id := Parent (Exp);
12423 begin
12424 -- If the expression is the RHS of an assignment or object declaration
12425 -- we are always OK because there will always be a target.
12427 -- Object renaming declarations, (generated for view conversions of
12428 -- actuals in inlined calls), like object declarations, provide an
12429 -- explicit type, and are safe as well.
12431 if (Nkind (Pexp) = N_Assignment_Statement
12432 and then Expression (Pexp) = Exp)
12433 or else Nkind_In (Pexp, N_Object_Declaration,
12434 N_Object_Renaming_Declaration)
12435 then
12436 return True;
12438 -- If the expression is the prefix of an N_Selected_Component we should
12439 -- also be OK because GCC knows to look inside the conversion except if
12440 -- the type is discriminated. We assume that we are OK anyway if the
12441 -- type is not set yet or if it is controlled since we can't afford to
12442 -- introduce a temporary in this case.
12444 elsif Nkind (Pexp) = N_Selected_Component
12445 and then Prefix (Pexp) = Exp
12446 then
12447 if No (Etype (Pexp)) then
12448 return True;
12449 else
12450 return
12451 not Has_Discriminants (Etype (Pexp))
12452 or else Is_Constrained (Etype (Pexp));
12453 end if;
12454 end if;
12456 -- Set the output type, this comes from Etype if it is set, otherwise we
12457 -- take it from the subtype mark, which we assume was already fully
12458 -- analyzed.
12460 if Present (Etype (Exp)) then
12461 Otyp := Etype (Exp);
12462 else
12463 Otyp := Entity (Subtype_Mark (Exp));
12464 end if;
12466 -- The input type always comes from the expression, and we assume this
12467 -- is indeed always analyzed, so we can simply get the Etype.
12469 Ityp := Etype (Expression (Exp));
12471 -- Initialize alignments to unknown so far
12473 Oalign := No_Uint;
12474 Ialign := No_Uint;
12476 -- Replace a concurrent type by its corresponding record type and each
12477 -- type by its underlying type and do the tests on those. The original
12478 -- type may be a private type whose completion is a concurrent type, so
12479 -- find the underlying type first.
12481 if Present (Underlying_Type (Otyp)) then
12482 Otyp := Underlying_Type (Otyp);
12483 end if;
12485 if Present (Underlying_Type (Ityp)) then
12486 Ityp := Underlying_Type (Ityp);
12487 end if;
12489 if Is_Concurrent_Type (Otyp) then
12490 Otyp := Corresponding_Record_Type (Otyp);
12491 end if;
12493 if Is_Concurrent_Type (Ityp) then
12494 Ityp := Corresponding_Record_Type (Ityp);
12495 end if;
12497 -- If the base types are the same, we know there is no problem since
12498 -- this conversion will be a noop.
12500 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12501 return True;
12503 -- Same if this is an upwards conversion of an untagged type, and there
12504 -- are no constraints involved (could be more general???)
12506 elsif Etype (Ityp) = Otyp
12507 and then not Is_Tagged_Type (Ityp)
12508 and then not Has_Discriminants (Ityp)
12509 and then No (First_Rep_Item (Base_Type (Ityp)))
12510 then
12511 return True;
12513 -- If the expression has an access type (object or subprogram) we assume
12514 -- that the conversion is safe, because the size of the target is safe,
12515 -- even if it is a record (which might be treated as having unknown size
12516 -- at this point).
12518 elsif Is_Access_Type (Ityp) then
12519 return True;
12521 -- If the size of output type is known at compile time, there is never
12522 -- a problem. Note that unconstrained records are considered to be of
12523 -- known size, but we can't consider them that way here, because we are
12524 -- talking about the actual size of the object.
12526 -- We also make sure that in addition to the size being known, we do not
12527 -- have a case which might generate an embarrassingly large temp in
12528 -- stack checking mode.
12530 elsif Size_Known_At_Compile_Time (Otyp)
12531 and then
12532 (not Stack_Checking_Enabled
12533 or else not May_Generate_Large_Temp (Otyp))
12534 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12535 then
12536 return True;
12538 -- If either type is tagged, then we know the alignment is OK so Gigi
12539 -- will be able to use pointer punning.
12541 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12542 return True;
12544 -- If either type is a limited record type, we cannot do a copy, so say
12545 -- safe since there's nothing else we can do.
12547 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12548 return True;
12550 -- Conversions to and from packed array types are always ignored and
12551 -- hence are safe.
12553 elsif Is_Packed_Array_Impl_Type (Otyp)
12554 or else Is_Packed_Array_Impl_Type (Ityp)
12555 then
12556 return True;
12557 end if;
12559 -- The only other cases known to be safe is if the input type's
12560 -- alignment is known to be at least the maximum alignment for the
12561 -- target or if both alignments are known and the output type's
12562 -- alignment is no stricter than the input's. We can use the component
12563 -- type alignment for an array if a type is an unpacked array type.
12565 if Present (Alignment_Clause (Otyp)) then
12566 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12568 elsif Is_Array_Type (Otyp)
12569 and then Present (Alignment_Clause (Component_Type (Otyp)))
12570 then
12571 Oalign := Expr_Value (Expression (Alignment_Clause
12572 (Component_Type (Otyp))));
12573 end if;
12575 if Present (Alignment_Clause (Ityp)) then
12576 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12578 elsif Is_Array_Type (Ityp)
12579 and then Present (Alignment_Clause (Component_Type (Ityp)))
12580 then
12581 Ialign := Expr_Value (Expression (Alignment_Clause
12582 (Component_Type (Ityp))));
12583 end if;
12585 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12586 return True;
12588 elsif Ialign /= No_Uint
12589 and then Oalign /= No_Uint
12590 and then Ialign <= Oalign
12591 then
12592 return True;
12594 -- Otherwise, Gigi cannot handle this and we must make a temporary
12596 else
12597 return False;
12598 end if;
12599 end Safe_Unchecked_Type_Conversion;
12601 ---------------------------------
12602 -- Set_Current_Value_Condition --
12603 ---------------------------------
12605 -- Note: the implementation of this procedure is very closely tied to the
12606 -- implementation of Get_Current_Value_Condition. Here we set required
12607 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12608 -- them, so they must have a consistent view.
12610 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12612 procedure Set_Entity_Current_Value (N : Node_Id);
12613 -- If N is an entity reference, where the entity is of an appropriate
12614 -- kind, then set the current value of this entity to Cnode, unless
12615 -- there is already a definite value set there.
12617 procedure Set_Expression_Current_Value (N : Node_Id);
12618 -- If N is of an appropriate form, sets an appropriate entry in current
12619 -- value fields of relevant entities. Multiple entities can be affected
12620 -- in the case of an AND or AND THEN.
12622 ------------------------------
12623 -- Set_Entity_Current_Value --
12624 ------------------------------
12626 procedure Set_Entity_Current_Value (N : Node_Id) is
12627 begin
12628 if Is_Entity_Name (N) then
12629 declare
12630 Ent : constant Entity_Id := Entity (N);
12632 begin
12633 -- Don't capture if not safe to do so
12635 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12636 return;
12637 end if;
12639 -- Here we have a case where the Current_Value field may need
12640 -- to be set. We set it if it is not already set to a compile
12641 -- time expression value.
12643 -- Note that this represents a decision that one condition
12644 -- blots out another previous one. That's certainly right if
12645 -- they occur at the same level. If the second one is nested,
12646 -- then the decision is neither right nor wrong (it would be
12647 -- equally OK to leave the outer one in place, or take the new
12648 -- inner one. Really we should record both, but our data
12649 -- structures are not that elaborate.
12651 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12652 Set_Current_Value (Ent, Cnode);
12653 end if;
12654 end;
12655 end if;
12656 end Set_Entity_Current_Value;
12658 ----------------------------------
12659 -- Set_Expression_Current_Value --
12660 ----------------------------------
12662 procedure Set_Expression_Current_Value (N : Node_Id) is
12663 Cond : Node_Id;
12665 begin
12666 Cond := N;
12668 -- Loop to deal with (ignore for now) any NOT operators present. The
12669 -- presence of NOT operators will be handled properly when we call
12670 -- Get_Current_Value_Condition.
12672 while Nkind (Cond) = N_Op_Not loop
12673 Cond := Right_Opnd (Cond);
12674 end loop;
12676 -- For an AND or AND THEN, recursively process operands
12678 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12679 Set_Expression_Current_Value (Left_Opnd (Cond));
12680 Set_Expression_Current_Value (Right_Opnd (Cond));
12681 return;
12682 end if;
12684 -- Check possible relational operator
12686 if Nkind (Cond) in N_Op_Compare then
12687 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12688 Set_Entity_Current_Value (Left_Opnd (Cond));
12689 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12690 Set_Entity_Current_Value (Right_Opnd (Cond));
12691 end if;
12693 elsif Nkind_In (Cond,
12694 N_Type_Conversion,
12695 N_Qualified_Expression,
12696 N_Expression_With_Actions)
12697 then
12698 Set_Expression_Current_Value (Expression (Cond));
12700 -- Check possible boolean variable reference
12702 else
12703 Set_Entity_Current_Value (Cond);
12704 end if;
12705 end Set_Expression_Current_Value;
12707 -- Start of processing for Set_Current_Value_Condition
12709 begin
12710 Set_Expression_Current_Value (Condition (Cnode));
12711 end Set_Current_Value_Condition;
12713 --------------------------
12714 -- Set_Elaboration_Flag --
12715 --------------------------
12717 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12718 Loc : constant Source_Ptr := Sloc (N);
12719 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12720 Asn : Node_Id;
12722 begin
12723 if Present (Ent) then
12725 -- Nothing to do if at the compilation unit level, because in this
12726 -- case the flag is set by the binder generated elaboration routine.
12728 if Nkind (Parent (N)) = N_Compilation_Unit then
12729 null;
12731 -- Here we do need to generate an assignment statement
12733 else
12734 Check_Restriction (No_Elaboration_Code, N);
12736 Asn :=
12737 Make_Assignment_Statement (Loc,
12738 Name => New_Occurrence_Of (Ent, Loc),
12739 Expression => Make_Integer_Literal (Loc, Uint_1));
12741 -- Mark the assignment statement as elaboration code. This allows
12742 -- the early call region mechanism (see Sem_Elab) to properly
12743 -- ignore such assignments even though they are non-preelaborable
12744 -- code.
12746 Set_Is_Elaboration_Code (Asn);
12748 if Nkind (Parent (N)) = N_Subunit then
12749 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12750 else
12751 Insert_After (N, Asn);
12752 end if;
12754 Analyze (Asn);
12756 -- Kill current value indication. This is necessary because the
12757 -- tests of this flag are inserted out of sequence and must not
12758 -- pick up bogus indications of the wrong constant value.
12760 Set_Current_Value (Ent, Empty);
12762 -- If the subprogram is in the current declarative part and
12763 -- 'access has been applied to it, generate an elaboration
12764 -- check at the beginning of the declarations of the body.
12766 if Nkind (N) = N_Subprogram_Body
12767 and then Address_Taken (Spec_Id)
12768 and then
12769 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12770 then
12771 declare
12772 Loc : constant Source_Ptr := Sloc (N);
12773 Decls : constant List_Id := Declarations (N);
12774 Chk : Node_Id;
12776 begin
12777 -- No need to generate this check if first entry in the
12778 -- declaration list is a raise of Program_Error now.
12780 if Present (Decls)
12781 and then Nkind (First (Decls)) = N_Raise_Program_Error
12782 then
12783 return;
12784 end if;
12786 -- Otherwise generate the check
12788 Chk :=
12789 Make_Raise_Program_Error (Loc,
12790 Condition =>
12791 Make_Op_Eq (Loc,
12792 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12793 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12794 Reason => PE_Access_Before_Elaboration);
12796 if No (Decls) then
12797 Set_Declarations (N, New_List (Chk));
12798 else
12799 Prepend (Chk, Decls);
12800 end if;
12802 Analyze (Chk);
12803 end;
12804 end if;
12805 end if;
12806 end if;
12807 end Set_Elaboration_Flag;
12809 ----------------------------
12810 -- Set_Renamed_Subprogram --
12811 ----------------------------
12813 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12814 begin
12815 -- If input node is an identifier, we can just reset it
12817 if Nkind (N) = N_Identifier then
12818 Set_Chars (N, Chars (E));
12819 Set_Entity (N, E);
12821 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12823 else
12824 declare
12825 CS : constant Boolean := Comes_From_Source (N);
12826 begin
12827 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12828 Set_Entity (N, E);
12829 Set_Comes_From_Source (N, CS);
12830 Set_Analyzed (N, True);
12831 end;
12832 end if;
12833 end Set_Renamed_Subprogram;
12835 ----------------------
12836 -- Side_Effect_Free --
12837 ----------------------
12839 function Side_Effect_Free
12840 (N : Node_Id;
12841 Name_Req : Boolean := False;
12842 Variable_Ref : Boolean := False) return Boolean
12844 Typ : constant Entity_Id := Etype (N);
12845 -- Result type of the expression
12847 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12848 -- The argument N is a construct where the Prefix is dereferenced if it
12849 -- is an access type and the result is a variable. The call returns True
12850 -- if the construct is side effect free (not considering side effects in
12851 -- other than the prefix which are to be tested by the caller).
12853 function Within_In_Parameter (N : Node_Id) return Boolean;
12854 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12855 -- N is not side-effect free when the actual is global and modifiable
12856 -- indirectly from within a subprogram, because it may be passed by
12857 -- reference. The front-end must be conservative here and assume that
12858 -- this may happen with any array or record type. On the other hand, we
12859 -- cannot create temporaries for all expressions for which this
12860 -- condition is true, for various reasons that might require clearing up
12861 -- ??? For example, discriminant references that appear out of place, or
12862 -- spurious type errors with class-wide expressions. As a result, we
12863 -- limit the transformation to loop bounds, which is so far the only
12864 -- case that requires it.
12866 -----------------------------
12867 -- Safe_Prefixed_Reference --
12868 -----------------------------
12870 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12871 begin
12872 -- If prefix is not side effect free, definitely not safe
12874 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12875 return False;
12877 -- If the prefix is of an access type that is not access-to-constant,
12878 -- then this construct is a variable reference, which means it is to
12879 -- be considered to have side effects if Variable_Ref is set True.
12881 elsif Is_Access_Type (Etype (Prefix (N)))
12882 and then not Is_Access_Constant (Etype (Prefix (N)))
12883 and then Variable_Ref
12884 then
12885 -- Exception is a prefix that is the result of a previous removal
12886 -- of side effects.
12888 return Is_Entity_Name (Prefix (N))
12889 and then not Comes_From_Source (Prefix (N))
12890 and then Ekind (Entity (Prefix (N))) = E_Constant
12891 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12893 -- If the prefix is an explicit dereference then this construct is a
12894 -- variable reference, which means it is to be considered to have
12895 -- side effects if Variable_Ref is True.
12897 -- We do NOT exclude dereferences of access-to-constant types because
12898 -- we handle them as constant view of variables.
12900 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12901 and then Variable_Ref
12902 then
12903 return False;
12905 -- Note: The following test is the simplest way of solving a complex
12906 -- problem uncovered by the following test (Side effect on loop bound
12907 -- that is a subcomponent of a global variable:
12909 -- with Text_Io; use Text_Io;
12910 -- procedure Tloop is
12911 -- type X is
12912 -- record
12913 -- V : Natural := 4;
12914 -- S : String (1..5) := (others => 'a');
12915 -- end record;
12916 -- X1 : X;
12918 -- procedure Modi;
12920 -- generic
12921 -- with procedure Action;
12922 -- procedure Loop_G (Arg : X; Msg : String)
12924 -- procedure Loop_G (Arg : X; Msg : String) is
12925 -- begin
12926 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12927 -- & Natural'Image (Arg.V));
12928 -- for Index in 1 .. Arg.V loop
12929 -- Text_Io.Put_Line
12930 -- (Natural'Image (Index) & " " & Arg.S (Index));
12931 -- if Index > 2 then
12932 -- Modi;
12933 -- end if;
12934 -- end loop;
12935 -- Put_Line ("end loop_g " & Msg);
12936 -- end;
12938 -- procedure Loop1 is new Loop_G (Modi);
12939 -- procedure Modi is
12940 -- begin
12941 -- X1.V := 1;
12942 -- Loop1 (X1, "from modi");
12943 -- end;
12945 -- begin
12946 -- Loop1 (X1, "initial");
12947 -- end;
12949 -- The output of the above program should be:
12951 -- begin loop_g initial will loop till: 4
12952 -- 1 a
12953 -- 2 a
12954 -- 3 a
12955 -- begin loop_g from modi will loop till: 1
12956 -- 1 a
12957 -- end loop_g from modi
12958 -- 4 a
12959 -- begin loop_g from modi will loop till: 1
12960 -- 1 a
12961 -- end loop_g from modi
12962 -- end loop_g initial
12964 -- If a loop bound is a subcomponent of a global variable, a
12965 -- modification of that variable within the loop may incorrectly
12966 -- affect the execution of the loop.
12968 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12969 and then Within_In_Parameter (Prefix (N))
12970 and then Variable_Ref
12971 then
12972 return False;
12974 -- All other cases are side effect free
12976 else
12977 return True;
12978 end if;
12979 end Safe_Prefixed_Reference;
12981 -------------------------
12982 -- Within_In_Parameter --
12983 -------------------------
12985 function Within_In_Parameter (N : Node_Id) return Boolean is
12986 begin
12987 if not Comes_From_Source (N) then
12988 return False;
12990 elsif Is_Entity_Name (N) then
12991 return Ekind (Entity (N)) = E_In_Parameter;
12993 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12994 return Within_In_Parameter (Prefix (N));
12996 else
12997 return False;
12998 end if;
12999 end Within_In_Parameter;
13001 -- Start of processing for Side_Effect_Free
13003 begin
13004 -- If volatile reference, always consider it to have side effects
13006 if Is_Volatile_Reference (N) then
13007 return False;
13008 end if;
13010 -- Note on checks that could raise Constraint_Error. Strictly, if we
13011 -- take advantage of 11.6, these checks do not count as side effects.
13012 -- However, we would prefer to consider that they are side effects,
13013 -- since the back end CSE does not work very well on expressions which
13014 -- can raise Constraint_Error. On the other hand if we don't consider
13015 -- them to be side effect free, then we get some awkward expansions
13016 -- in -gnato mode, resulting in code insertions at a point where we
13017 -- do not have a clear model for performing the insertions.
13019 -- Special handling for entity names
13021 if Is_Entity_Name (N) then
13023 -- A type reference is always side effect free
13025 if Is_Type (Entity (N)) then
13026 return True;
13028 -- Variables are considered to be a side effect if Variable_Ref
13029 -- is set or if we have a volatile reference and Name_Req is off.
13030 -- If Name_Req is True then we can't help returning a name which
13031 -- effectively allows multiple references in any case.
13033 elsif Is_Variable (N, Use_Original_Node => False) then
13034 return not Variable_Ref
13035 and then (not Is_Volatile_Reference (N) or else Name_Req);
13037 -- Any other entity (e.g. a subtype name) is definitely side
13038 -- effect free.
13040 else
13041 return True;
13042 end if;
13044 -- A value known at compile time is always side effect free
13046 elsif Compile_Time_Known_Value (N) then
13047 return True;
13049 -- A variable renaming is not side-effect free, because the renaming
13050 -- will function like a macro in the front-end in some cases, and an
13051 -- assignment can modify the component designated by N, so we need to
13052 -- create a temporary for it.
13054 -- The guard testing for Entity being present is needed at least in
13055 -- the case of rewritten predicate expressions, and may well also be
13056 -- appropriate elsewhere. Obviously we can't go testing the entity
13057 -- field if it does not exist, so it's reasonable to say that this is
13058 -- not the renaming case if it does not exist.
13060 elsif Is_Entity_Name (Original_Node (N))
13061 and then Present (Entity (Original_Node (N)))
13062 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
13063 and then Ekind (Entity (Original_Node (N))) /= E_Constant
13064 then
13065 declare
13066 RO : constant Node_Id :=
13067 Renamed_Object (Entity (Original_Node (N)));
13069 begin
13070 -- If the renamed object is an indexed component, or an
13071 -- explicit dereference, then the designated object could
13072 -- be modified by an assignment.
13074 if Nkind_In (RO, N_Indexed_Component,
13075 N_Explicit_Dereference)
13076 then
13077 return False;
13079 -- A selected component must have a safe prefix
13081 elsif Nkind (RO) = N_Selected_Component then
13082 return Safe_Prefixed_Reference (RO);
13084 -- In all other cases, designated object cannot be changed so
13085 -- we are side effect free.
13087 else
13088 return True;
13089 end if;
13090 end;
13092 -- Remove_Side_Effects generates an object renaming declaration to
13093 -- capture the expression of a class-wide expression. In VM targets
13094 -- the frontend performs no expansion for dispatching calls to
13095 -- class- wide types since they are handled by the VM. Hence, we must
13096 -- locate here if this node corresponds to a previous invocation of
13097 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13099 elsif not Tagged_Type_Expansion
13100 and then not Comes_From_Source (N)
13101 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
13102 and then Is_Class_Wide_Type (Typ)
13103 then
13104 return True;
13106 -- Generating C the type conversion of an access to constrained array
13107 -- type into an access to unconstrained array type involves initializing
13108 -- a fat pointer and the expression cannot be assumed to be free of side
13109 -- effects since it must referenced several times to compute its bounds.
13111 elsif Modify_Tree_For_C
13112 and then Nkind (N) = N_Type_Conversion
13113 and then Is_Access_Type (Typ)
13114 and then Is_Array_Type (Designated_Type (Typ))
13115 and then not Is_Constrained (Designated_Type (Typ))
13116 then
13117 return False;
13118 end if;
13120 -- For other than entity names and compile time known values,
13121 -- check the node kind for special processing.
13123 case Nkind (N) is
13125 -- An attribute reference is side effect free if its expressions
13126 -- are side effect free and its prefix is side effect free or
13127 -- is an entity reference.
13129 -- Is this right? what about x'first where x is a variable???
13131 when N_Attribute_Reference =>
13132 Attribute_Reference : declare
13134 function Side_Effect_Free_Attribute
13135 (Attribute_Name : Name_Id) return Boolean;
13136 -- Returns True if evaluation of the given attribute is
13137 -- considered side-effect free (independent of prefix and
13138 -- arguments).
13140 --------------------------------
13141 -- Side_Effect_Free_Attribute --
13142 --------------------------------
13144 function Side_Effect_Free_Attribute
13145 (Attribute_Name : Name_Id) return Boolean
13147 begin
13148 case Attribute_Name is
13149 when Name_Input =>
13150 return False;
13152 when Name_Image
13153 | Name_Img
13154 | Name_Wide_Image
13155 | Name_Wide_Wide_Image
13157 -- CodePeer doesn't want to see replicated copies of
13158 -- 'Image calls.
13160 return not CodePeer_Mode;
13162 when others =>
13163 return True;
13164 end case;
13165 end Side_Effect_Free_Attribute;
13167 -- Start of processing for Attribute_Reference
13169 begin
13170 return
13171 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13172 and then Side_Effect_Free_Attribute (Attribute_Name (N))
13173 and then (Is_Entity_Name (Prefix (N))
13174 or else Side_Effect_Free
13175 (Prefix (N), Name_Req, Variable_Ref));
13176 end Attribute_Reference;
13178 -- A binary operator is side effect free if and both operands are
13179 -- side effect free. For this purpose binary operators include
13180 -- membership tests and short circuit forms.
13182 when N_Binary_Op
13183 | N_Membership_Test
13184 | N_Short_Circuit
13186 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13187 and then
13188 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13190 -- An explicit dereference is side effect free only if it is
13191 -- a side effect free prefixed reference.
13193 when N_Explicit_Dereference =>
13194 return Safe_Prefixed_Reference (N);
13196 -- An expression with action is side effect free if its expression
13197 -- is side effect free and it has no actions.
13199 when N_Expression_With_Actions =>
13200 return
13201 Is_Empty_List (Actions (N))
13202 and then Side_Effect_Free
13203 (Expression (N), Name_Req, Variable_Ref);
13205 -- A call to _rep_to_pos is side effect free, since we generate
13206 -- this pure function call ourselves. Moreover it is critically
13207 -- important to make this exception, since otherwise we can have
13208 -- discriminants in array components which don't look side effect
13209 -- free in the case of an array whose index type is an enumeration
13210 -- type with an enumeration rep clause.
13212 -- All other function calls are not side effect free
13214 when N_Function_Call =>
13215 return
13216 Nkind (Name (N)) = N_Identifier
13217 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
13218 and then Side_Effect_Free
13219 (First (Parameter_Associations (N)),
13220 Name_Req, Variable_Ref);
13222 -- An IF expression is side effect free if it's of a scalar type, and
13223 -- all its components are all side effect free (conditions and then
13224 -- actions and else actions). We restrict to scalar types, since it
13225 -- is annoying to deal with things like (if A then B else C)'First
13226 -- where the type involved is a string type.
13228 when N_If_Expression =>
13229 return
13230 Is_Scalar_Type (Typ)
13231 and then Side_Effect_Free
13232 (Expressions (N), Name_Req, Variable_Ref);
13234 -- An indexed component is side effect free if it is a side
13235 -- effect free prefixed reference and all the indexing
13236 -- expressions are side effect free.
13238 when N_Indexed_Component =>
13239 return
13240 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13241 and then Safe_Prefixed_Reference (N);
13243 -- A type qualification, type conversion, or unchecked expression is
13244 -- side effect free if the expression is side effect free.
13246 when N_Qualified_Expression
13247 | N_Type_Conversion
13248 | N_Unchecked_Expression
13250 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13252 -- A selected component is side effect free only if it is a side
13253 -- effect free prefixed reference.
13255 when N_Selected_Component =>
13256 return Safe_Prefixed_Reference (N);
13258 -- A range is side effect free if the bounds are side effect free
13260 when N_Range =>
13261 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13262 and then
13263 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13265 -- A slice is side effect free if it is a side effect free
13266 -- prefixed reference and the bounds are side effect free.
13268 when N_Slice =>
13269 return
13270 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13271 and then Safe_Prefixed_Reference (N);
13273 -- A unary operator is side effect free if the operand
13274 -- is side effect free.
13276 when N_Unary_Op =>
13277 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13279 -- An unchecked type conversion is side effect free only if it
13280 -- is safe and its argument is side effect free.
13282 when N_Unchecked_Type_Conversion =>
13283 return
13284 Safe_Unchecked_Type_Conversion (N)
13285 and then Side_Effect_Free
13286 (Expression (N), Name_Req, Variable_Ref);
13288 -- A literal is side effect free
13290 when N_Character_Literal
13291 | N_Integer_Literal
13292 | N_Real_Literal
13293 | N_String_Literal
13295 return True;
13297 -- We consider that anything else has side effects. This is a bit
13298 -- crude, but we are pretty close for most common cases, and we
13299 -- are certainly correct (i.e. we never return True when the
13300 -- answer should be False).
13302 when others =>
13303 return False;
13304 end case;
13305 end Side_Effect_Free;
13307 -- A list is side effect free if all elements of the list are side
13308 -- effect free.
13310 function Side_Effect_Free
13311 (L : List_Id;
13312 Name_Req : Boolean := False;
13313 Variable_Ref : Boolean := False) return Boolean
13315 N : Node_Id;
13317 begin
13318 if L = No_List or else L = Error_List then
13319 return True;
13321 else
13322 N := First (L);
13323 while Present (N) loop
13324 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13325 return False;
13326 else
13327 Next (N);
13328 end if;
13329 end loop;
13331 return True;
13332 end if;
13333 end Side_Effect_Free;
13335 ----------------------------------
13336 -- Silly_Boolean_Array_Not_Test --
13337 ----------------------------------
13339 -- This procedure implements an odd and silly test. We explicitly check
13340 -- for the case where the 'First of the component type is equal to the
13341 -- 'Last of this component type, and if this is the case, we make sure
13342 -- that constraint error is raised. The reason is that the NOT is bound
13343 -- to cause CE in this case, and we will not otherwise catch it.
13345 -- No such check is required for AND and OR, since for both these cases
13346 -- False op False = False, and True op True = True. For the XOR case,
13347 -- see Silly_Boolean_Array_Xor_Test.
13349 -- Believe it or not, this was reported as a bug. Note that nearly always,
13350 -- the test will evaluate statically to False, so the code will be
13351 -- statically removed, and no extra overhead caused.
13353 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13354 Loc : constant Source_Ptr := Sloc (N);
13355 CT : constant Entity_Id := Component_Type (T);
13357 begin
13358 -- The check we install is
13360 -- constraint_error when
13361 -- component_type'first = component_type'last
13362 -- and then array_type'Length /= 0)
13364 -- We need the last guard because we don't want to raise CE for empty
13365 -- arrays since no out of range values result. (Empty arrays with a
13366 -- component type of True .. True -- very useful -- even the ACATS
13367 -- does not test that marginal case).
13369 Insert_Action (N,
13370 Make_Raise_Constraint_Error (Loc,
13371 Condition =>
13372 Make_And_Then (Loc,
13373 Left_Opnd =>
13374 Make_Op_Eq (Loc,
13375 Left_Opnd =>
13376 Make_Attribute_Reference (Loc,
13377 Prefix => New_Occurrence_Of (CT, Loc),
13378 Attribute_Name => Name_First),
13380 Right_Opnd =>
13381 Make_Attribute_Reference (Loc,
13382 Prefix => New_Occurrence_Of (CT, Loc),
13383 Attribute_Name => Name_Last)),
13385 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13386 Reason => CE_Range_Check_Failed));
13387 end Silly_Boolean_Array_Not_Test;
13389 ----------------------------------
13390 -- Silly_Boolean_Array_Xor_Test --
13391 ----------------------------------
13393 -- This procedure implements an odd and silly test. We explicitly check
13394 -- for the XOR case where the component type is True .. True, since this
13395 -- will raise constraint error. A special check is required since CE
13396 -- will not be generated otherwise (cf Expand_Packed_Not).
13398 -- No such check is required for AND and OR, since for both these cases
13399 -- False op False = False, and True op True = True, and no check is
13400 -- required for the case of False .. False, since False xor False = False.
13401 -- See also Silly_Boolean_Array_Not_Test
13403 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13404 Loc : constant Source_Ptr := Sloc (N);
13405 CT : constant Entity_Id := Component_Type (T);
13407 begin
13408 -- The check we install is
13410 -- constraint_error when
13411 -- Boolean (component_type'First)
13412 -- and then Boolean (component_type'Last)
13413 -- and then array_type'Length /= 0)
13415 -- We need the last guard because we don't want to raise CE for empty
13416 -- arrays since no out of range values result (Empty arrays with a
13417 -- component type of True .. True -- very useful -- even the ACATS
13418 -- does not test that marginal case).
13420 Insert_Action (N,
13421 Make_Raise_Constraint_Error (Loc,
13422 Condition =>
13423 Make_And_Then (Loc,
13424 Left_Opnd =>
13425 Make_And_Then (Loc,
13426 Left_Opnd =>
13427 Convert_To (Standard_Boolean,
13428 Make_Attribute_Reference (Loc,
13429 Prefix => New_Occurrence_Of (CT, Loc),
13430 Attribute_Name => Name_First)),
13432 Right_Opnd =>
13433 Convert_To (Standard_Boolean,
13434 Make_Attribute_Reference (Loc,
13435 Prefix => New_Occurrence_Of (CT, Loc),
13436 Attribute_Name => Name_Last))),
13438 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13439 Reason => CE_Range_Check_Failed));
13440 end Silly_Boolean_Array_Xor_Test;
13442 --------------------------
13443 -- Target_Has_Fixed_Ops --
13444 --------------------------
13446 Integer_Sized_Small : Ureal;
13447 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13448 -- called (we don't want to compute it more than once).
13450 Long_Integer_Sized_Small : Ureal;
13451 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13452 -- is called (we don't want to compute it more than once)
13454 First_Time_For_THFO : Boolean := True;
13455 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13457 function Target_Has_Fixed_Ops
13458 (Left_Typ : Entity_Id;
13459 Right_Typ : Entity_Id;
13460 Result_Typ : Entity_Id) return Boolean
13462 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13463 -- Return True if the given type is a fixed-point type with a small
13464 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13465 -- an absolute value less than 1.0. This is currently limited to
13466 -- fixed-point types that map to Integer or Long_Integer.
13468 ------------------------
13469 -- Is_Fractional_Type --
13470 ------------------------
13472 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13473 begin
13474 if Esize (Typ) = Standard_Integer_Size then
13475 return Small_Value (Typ) = Integer_Sized_Small;
13477 elsif Esize (Typ) = Standard_Long_Integer_Size then
13478 return Small_Value (Typ) = Long_Integer_Sized_Small;
13480 else
13481 return False;
13482 end if;
13483 end Is_Fractional_Type;
13485 -- Start of processing for Target_Has_Fixed_Ops
13487 begin
13488 -- Return False if Fractional_Fixed_Ops_On_Target is false
13490 if not Fractional_Fixed_Ops_On_Target then
13491 return False;
13492 end if;
13494 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13495 -- standard constants used by Is_Fractional_Type.
13497 if First_Time_For_THFO then
13498 First_Time_For_THFO := False;
13500 Integer_Sized_Small :=
13501 UR_From_Components
13502 (Num => Uint_1,
13503 Den => UI_From_Int (Standard_Integer_Size - 1),
13504 Rbase => 2);
13506 Long_Integer_Sized_Small :=
13507 UR_From_Components
13508 (Num => Uint_1,
13509 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13510 Rbase => 2);
13511 end if;
13513 -- Return True if target supports fixed-by-fixed multiply/divide for
13514 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13515 -- and result types are equivalent fractional types.
13517 return Is_Fractional_Type (Base_Type (Left_Typ))
13518 and then Is_Fractional_Type (Base_Type (Right_Typ))
13519 and then Is_Fractional_Type (Base_Type (Result_Typ))
13520 and then Esize (Left_Typ) = Esize (Right_Typ)
13521 and then Esize (Left_Typ) = Esize (Result_Typ);
13522 end Target_Has_Fixed_Ops;
13524 -------------------
13525 -- Type_Map_Hash --
13526 -------------------
13528 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13529 begin
13530 return Type_Map_Header (Id mod Type_Map_Size);
13531 end Type_Map_Hash;
13533 ------------------------------------------
13534 -- Type_May_Have_Bit_Aligned_Components --
13535 ------------------------------------------
13537 function Type_May_Have_Bit_Aligned_Components
13538 (Typ : Entity_Id) return Boolean
13540 begin
13541 -- Array type, check component type
13543 if Is_Array_Type (Typ) then
13544 return
13545 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13547 -- Record type, check components
13549 elsif Is_Record_Type (Typ) then
13550 declare
13551 E : Entity_Id;
13553 begin
13554 E := First_Component_Or_Discriminant (Typ);
13555 while Present (E) loop
13556 if Component_May_Be_Bit_Aligned (E)
13557 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13558 then
13559 return True;
13560 end if;
13562 Next_Component_Or_Discriminant (E);
13563 end loop;
13565 return False;
13566 end;
13568 -- Type other than array or record is always OK
13570 else
13571 return False;
13572 end if;
13573 end Type_May_Have_Bit_Aligned_Components;
13575 -------------------------------
13576 -- Update_Primitives_Mapping --
13577 -------------------------------
13579 procedure Update_Primitives_Mapping
13580 (Inher_Id : Entity_Id;
13581 Subp_Id : Entity_Id)
13583 begin
13584 Map_Types
13585 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13586 Derived_Type => Find_Dispatching_Type (Subp_Id));
13587 end Update_Primitives_Mapping;
13589 ----------------------------------
13590 -- Within_Case_Or_If_Expression --
13591 ----------------------------------
13593 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13594 Par : Node_Id;
13596 begin
13597 -- Locate an enclosing case or if expression. Note that these constructs
13598 -- can be expanded into Expression_With_Actions, hence the test of the
13599 -- original node.
13601 Par := Parent (N);
13602 while Present (Par) loop
13603 if Nkind_In (Original_Node (Par), N_Case_Expression,
13604 N_If_Expression)
13605 then
13606 return True;
13608 -- Prevent the search from going too far
13610 elsif Is_Body_Or_Package_Declaration (Par) then
13611 return False;
13612 end if;
13614 Par := Parent (Par);
13615 end loop;
13617 return False;
13618 end Within_Case_Or_If_Expression;
13620 --------------------------------
13621 -- Within_Internal_Subprogram --
13622 --------------------------------
13624 function Within_Internal_Subprogram return Boolean is
13625 S : Entity_Id;
13627 begin
13628 S := Current_Scope;
13629 while Present (S) and then not Is_Subprogram (S) loop
13630 S := Scope (S);
13631 end loop;
13633 return Present (S)
13634 and then Get_TSS_Name (S) /= TSS_Null
13635 and then not Is_Predicate_Function (S)
13636 and then not Is_Predicate_Function_M (S);
13637 end Within_Internal_Subprogram;
13639 end Exp_Util;