Preserving locations for variable-uses and constants (PR c++/43486)
[official-gcc.git] / gcc / ada / exp_util.adb
blobf768e201e93c1ee2a6fdf077725aa94c4b0a2bf4
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Exp_Aggr; use Exp_Aggr;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch11; use Exp_Ch11;
38 with Ghost; use Ghost;
39 with Inline; use Inline;
40 with Itypes; use Itypes;
41 with Lib; use Lib;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Ttypes; use Ttypes;
66 with Urealp; use Urealp;
67 with Validsw; use Validsw;
69 with GNAT.HTable;
70 package body Exp_Util is
72 ---------------------------------------------------------
73 -- Handling of inherited class-wide pre/postconditions --
74 ---------------------------------------------------------
76 -- Following AI12-0113, the expression for a class-wide condition is
77 -- transformed for a subprogram that inherits it, by replacing calls
78 -- to primitive operations of the original controlling type into the
79 -- corresponding overriding operations of the derived type. The following
80 -- hash table manages this mapping, and is expanded on demand whenever
81 -- such inherited expression needs to be constructed.
83 -- The mapping is also used to check whether an inherited operation has
84 -- a condition that depends on overridden operations. For such an
85 -- operation we must create a wrapper that is then treated as a normal
86 -- overriding. In SPARK mode such operations are illegal.
88 -- For a given root type there may be several type extensions with their
89 -- own overriding operations, so at various times a given operation of
90 -- the root will be mapped into different overridings. The root type is
91 -- also mapped into the current type extension to indicate that its
92 -- operations are mapped into the overriding operations of that current
93 -- type extension.
95 -- The contents of the map are as follows:
97 -- Key Value
99 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
100 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
101 -- Discriminant (Entity_Id) Expression (Node_Id)
102 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
103 -- Type (Entity_Id) Type (Entity_Id)
105 Type_Map_Size : constant := 511;
107 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
108 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
110 package Type_Map is new GNAT.HTable.Simple_HTable
111 (Header_Num => Type_Map_Header,
112 Key => Entity_Id,
113 Element => Node_Or_Entity_Id,
114 No_element => Empty,
115 Hash => Type_Map_Hash,
116 Equal => "=");
118 -----------------------
119 -- Local Subprograms --
120 -----------------------
122 function Build_Task_Array_Image
123 (Loc : Source_Ptr;
124 Id_Ref : Node_Id;
125 A_Type : Entity_Id;
126 Dyn : Boolean := False) return Node_Id;
127 -- Build function to generate the image string for a task that is an array
128 -- component, concatenating the images of each index. To avoid storage
129 -- leaks, the string is built with successive slice assignments. The flag
130 -- Dyn indicates whether this is called for the initialization procedure of
131 -- an array of tasks, or for the name of a dynamically created task that is
132 -- assigned to an indexed component.
134 function Build_Task_Image_Function
135 (Loc : Source_Ptr;
136 Decls : List_Id;
137 Stats : List_Id;
138 Res : Entity_Id) return Node_Id;
139 -- Common processing for Task_Array_Image and Task_Record_Image. Build
140 -- function body that computes image.
142 procedure Build_Task_Image_Prefix
143 (Loc : Source_Ptr;
144 Len : out Entity_Id;
145 Res : out Entity_Id;
146 Pos : out Entity_Id;
147 Prefix : Entity_Id;
148 Sum : Node_Id;
149 Decls : List_Id;
150 Stats : List_Id);
151 -- Common processing for Task_Array_Image and Task_Record_Image. Create
152 -- local variables and assign prefix of name to result string.
154 function Build_Task_Record_Image
155 (Loc : Source_Ptr;
156 Id_Ref : Node_Id;
157 Dyn : Boolean := False) return Node_Id;
158 -- Build function to generate the image string for a task that is a record
159 -- component. Concatenate name of variable with that of selector. The flag
160 -- Dyn indicates whether this is called for the initialization procedure of
161 -- record with task components, or for a dynamically created task that is
162 -- assigned to a selected component.
164 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
165 -- Force evaluation of bounds of a slice, which may be given by a range
166 -- or by a subtype indication with or without a constraint.
168 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
169 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
170 -- an assertion expression that should be verified at run time.
172 function Make_CW_Equivalent_Type
173 (T : Entity_Id;
174 E : Node_Id) return Entity_Id;
175 -- T is a class-wide type entity, E is the initial expression node that
176 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
177 -- returns the entity of the Equivalent type and inserts on the fly the
178 -- necessary declaration such as:
180 -- type anon is record
181 -- _parent : Root_Type (T); constrained with E discriminants (if any)
182 -- Extension : String (1 .. expr to match size of E);
183 -- end record;
185 -- This record is compatible with any object of the class of T thanks to
186 -- the first field and has the same size as E thanks to the second.
188 function Make_Literal_Range
189 (Loc : Source_Ptr;
190 Literal_Typ : Entity_Id) return Node_Id;
191 -- Produce a Range node whose bounds are:
192 -- Low_Bound (Literal_Type) ..
193 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
194 -- this is used for expanding declarations like X : String := "sdfgdfg";
196 -- If the index type of the target array is not integer, we generate:
197 -- Low_Bound (Literal_Type) ..
198 -- Literal_Type'Val
199 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
200 -- + (Length (Literal_Typ) -1))
202 function Make_Non_Empty_Check
203 (Loc : Source_Ptr;
204 N : Node_Id) return Node_Id;
205 -- Produce a boolean expression checking that the unidimensional array
206 -- node N is not empty.
208 function New_Class_Wide_Subtype
209 (CW_Typ : Entity_Id;
210 N : Node_Id) return Entity_Id;
211 -- Create an implicit subtype of CW_Typ attached to node N
213 function Requires_Cleanup_Actions
214 (L : List_Id;
215 Lib_Level : Boolean;
216 Nested_Constructs : Boolean) return Boolean;
217 -- Given a list L, determine whether it contains one of the following:
219 -- 1) controlled objects
220 -- 2) library-level tagged types
222 -- Lib_Level is True when the list comes from a construct at the library
223 -- level, and False otherwise. Nested_Constructs is True when any nested
224 -- packages declared in L must be processed, and False otherwise.
226 -------------------------------------
227 -- Activate_Atomic_Synchronization --
228 -------------------------------------
230 procedure Activate_Atomic_Synchronization (N : Node_Id) is
231 Msg_Node : Node_Id;
233 begin
234 case Nkind (Parent (N)) is
236 -- Check for cases of appearing in the prefix of a construct where we
237 -- don't need atomic synchronization for this kind of usage.
239 when
240 -- Nothing to do if we are the prefix of an attribute, since we
241 -- do not want an atomic sync operation for things like 'Size.
243 N_Attribute_Reference
245 -- The N_Reference node is like an attribute
247 | N_Reference
249 -- Nothing to do for a reference to a component (or components)
250 -- of a composite object. Only reads and updates of the object
251 -- as a whole require atomic synchronization (RM C.6 (15)).
253 | N_Indexed_Component
254 | N_Selected_Component
255 | N_Slice
257 -- For all the above cases, nothing to do if we are the prefix
259 if Prefix (Parent (N)) = N then
260 return;
261 end if;
263 when others =>
264 null;
265 end case;
267 -- Nothing to do for the identifier in an object renaming declaration,
268 -- the renaming itself does not need atomic synchronization.
270 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
271 return;
272 end if;
274 -- Go ahead and set the flag
276 Set_Atomic_Sync_Required (N);
278 -- Generate info message if requested
280 if Warn_On_Atomic_Synchronization then
281 case Nkind (N) is
282 when N_Identifier =>
283 Msg_Node := N;
285 when N_Expanded_Name
286 | N_Selected_Component
288 Msg_Node := Selector_Name (N);
290 when N_Explicit_Dereference
291 | N_Indexed_Component
293 Msg_Node := Empty;
295 when others =>
296 pragma Assert (False);
297 return;
298 end case;
300 if Present (Msg_Node) then
301 Error_Msg_N
302 ("info: atomic synchronization set for &?N?", Msg_Node);
303 else
304 Error_Msg_N
305 ("info: atomic synchronization set?N?", N);
306 end if;
307 end if;
308 end Activate_Atomic_Synchronization;
310 ----------------------
311 -- Adjust_Condition --
312 ----------------------
314 procedure Adjust_Condition (N : Node_Id) is
315 begin
316 if No (N) then
317 return;
318 end if;
320 declare
321 Loc : constant Source_Ptr := Sloc (N);
322 T : constant Entity_Id := Etype (N);
323 Ti : Entity_Id;
325 begin
326 -- Defend against a call where the argument has no type, or has a
327 -- type that is not Boolean. This can occur because of prior errors.
329 if No (T) or else not Is_Boolean_Type (T) then
330 return;
331 end if;
333 -- Apply validity checking if needed
335 if Validity_Checks_On and Validity_Check_Tests then
336 Ensure_Valid (N);
337 end if;
339 -- Immediate return if standard boolean, the most common case,
340 -- where nothing needs to be done.
342 if Base_Type (T) = Standard_Boolean then
343 return;
344 end if;
346 -- Case of zero/non-zero semantics or non-standard enumeration
347 -- representation. In each case, we rewrite the node as:
349 -- ityp!(N) /= False'Enum_Rep
351 -- where ityp is an integer type with large enough size to hold any
352 -- value of type T.
354 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
355 if Esize (T) <= Esize (Standard_Integer) then
356 Ti := Standard_Integer;
357 else
358 Ti := Standard_Long_Long_Integer;
359 end if;
361 Rewrite (N,
362 Make_Op_Ne (Loc,
363 Left_Opnd => Unchecked_Convert_To (Ti, N),
364 Right_Opnd =>
365 Make_Attribute_Reference (Loc,
366 Attribute_Name => Name_Enum_Rep,
367 Prefix =>
368 New_Occurrence_Of (First_Literal (T), Loc))));
369 Analyze_And_Resolve (N, Standard_Boolean);
371 else
372 Rewrite (N, Convert_To (Standard_Boolean, N));
373 Analyze_And_Resolve (N, Standard_Boolean);
374 end if;
375 end;
376 end Adjust_Condition;
378 ------------------------
379 -- Adjust_Result_Type --
380 ------------------------
382 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
383 begin
384 -- Ignore call if current type is not Standard.Boolean
386 if Etype (N) /= Standard_Boolean then
387 return;
388 end if;
390 -- If result is already of correct type, nothing to do. Note that
391 -- this will get the most common case where everything has a type
392 -- of Standard.Boolean.
394 if Base_Type (T) = Standard_Boolean then
395 return;
397 else
398 declare
399 KP : constant Node_Kind := Nkind (Parent (N));
401 begin
402 -- If result is to be used as a Condition in the syntax, no need
403 -- to convert it back, since if it was changed to Standard.Boolean
404 -- using Adjust_Condition, that is just fine for this usage.
406 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
407 return;
409 -- If result is an operand of another logical operation, no need
410 -- to reset its type, since Standard.Boolean is just fine, and
411 -- such operations always do Adjust_Condition on their operands.
413 elsif KP in N_Op_Boolean
414 or else KP in N_Short_Circuit
415 or else KP = N_Op_Not
416 then
417 return;
419 -- Otherwise we perform a conversion from the current type, which
420 -- must be Standard.Boolean, to the desired type. Use the base
421 -- type to prevent spurious constraint checks that are extraneous
422 -- to the transformation. The type and its base have the same
423 -- representation, standard or otherwise.
425 else
426 Set_Analyzed (N);
427 Rewrite (N, Convert_To (Base_Type (T), N));
428 Analyze_And_Resolve (N, Base_Type (T));
429 end if;
430 end;
431 end if;
432 end Adjust_Result_Type;
434 --------------------------
435 -- Append_Freeze_Action --
436 --------------------------
438 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
439 Fnode : Node_Id;
441 begin
442 Ensure_Freeze_Node (T);
443 Fnode := Freeze_Node (T);
445 if No (Actions (Fnode)) then
446 Set_Actions (Fnode, New_List (N));
447 else
448 Append (N, Actions (Fnode));
449 end if;
451 end Append_Freeze_Action;
453 ---------------------------
454 -- Append_Freeze_Actions --
455 ---------------------------
457 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
458 Fnode : Node_Id;
460 begin
461 if No (L) then
462 return;
463 end if;
465 Ensure_Freeze_Node (T);
466 Fnode := Freeze_Node (T);
468 if No (Actions (Fnode)) then
469 Set_Actions (Fnode, L);
470 else
471 Append_List (L, Actions (Fnode));
472 end if;
473 end Append_Freeze_Actions;
475 ------------------------------------
476 -- Build_Allocate_Deallocate_Proc --
477 ------------------------------------
479 procedure Build_Allocate_Deallocate_Proc
480 (N : Node_Id;
481 Is_Allocate : Boolean)
483 function Find_Object (E : Node_Id) return Node_Id;
484 -- Given an arbitrary expression of an allocator, try to find an object
485 -- reference in it, otherwise return the original expression.
487 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
488 -- Determine whether subprogram Subp denotes a custom allocate or
489 -- deallocate.
491 -----------------
492 -- Find_Object --
493 -----------------
495 function Find_Object (E : Node_Id) return Node_Id is
496 Expr : Node_Id;
498 begin
499 pragma Assert (Is_Allocate);
501 Expr := E;
502 loop
503 if Nkind (Expr) = N_Explicit_Dereference then
504 Expr := Prefix (Expr);
506 elsif Nkind (Expr) = N_Qualified_Expression then
507 Expr := Expression (Expr);
509 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
511 -- When interface class-wide types are involved in allocation,
512 -- the expander introduces several levels of address arithmetic
513 -- to perform dispatch table displacement. In this scenario the
514 -- object appears as:
516 -- Tag_Ptr (Base_Address (<object>'Address))
518 -- Detect this case and utilize the whole expression as the
519 -- "object" since it now points to the proper dispatch table.
521 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
522 exit;
524 -- Continue to strip the object
526 else
527 Expr := Expression (Expr);
528 end if;
530 else
531 exit;
532 end if;
533 end loop;
535 return Expr;
536 end Find_Object;
538 ---------------------------------
539 -- Is_Allocate_Deallocate_Proc --
540 ---------------------------------
542 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
543 begin
544 -- Look for a subprogram body with only one statement which is a
545 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
547 if Ekind (Subp) = E_Procedure
548 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
549 then
550 declare
551 HSS : constant Node_Id :=
552 Handled_Statement_Sequence (Parent (Parent (Subp)));
553 Proc : Entity_Id;
555 begin
556 if Present (Statements (HSS))
557 and then Nkind (First (Statements (HSS))) =
558 N_Procedure_Call_Statement
559 then
560 Proc := Entity (Name (First (Statements (HSS))));
562 return
563 Is_RTE (Proc, RE_Allocate_Any_Controlled)
564 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
565 end if;
566 end;
567 end if;
569 return False;
570 end Is_Allocate_Deallocate_Proc;
572 -- Local variables
574 Desig_Typ : Entity_Id;
575 Expr : Node_Id;
576 Needs_Fin : Boolean;
577 Pool_Id : Entity_Id;
578 Proc_To_Call : Node_Id := Empty;
579 Ptr_Typ : Entity_Id;
581 -- Start of processing for Build_Allocate_Deallocate_Proc
583 begin
584 -- Obtain the attributes of the allocation / deallocation
586 if Nkind (N) = N_Free_Statement then
587 Expr := Expression (N);
588 Ptr_Typ := Base_Type (Etype (Expr));
589 Proc_To_Call := Procedure_To_Call (N);
591 else
592 if Nkind (N) = N_Object_Declaration then
593 Expr := Expression (N);
594 else
595 Expr := N;
596 end if;
598 -- In certain cases an allocator with a qualified expression may
599 -- be relocated and used as the initialization expression of a
600 -- temporary:
602 -- before:
603 -- Obj : Ptr_Typ := new Desig_Typ'(...);
605 -- after:
606 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
607 -- Obj : Ptr_Typ := Tmp;
609 -- Since the allocator is always marked as analyzed to avoid infinite
610 -- expansion, it will never be processed by this routine given that
611 -- the designated type needs finalization actions. Detect this case
612 -- and complete the expansion of the allocator.
614 if Nkind (Expr) = N_Identifier
615 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
616 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
617 then
618 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
619 return;
620 end if;
622 -- The allocator may have been rewritten into something else in which
623 -- case the expansion performed by this routine does not apply.
625 if Nkind (Expr) /= N_Allocator then
626 return;
627 end if;
629 Ptr_Typ := Base_Type (Etype (Expr));
630 Proc_To_Call := Procedure_To_Call (Expr);
631 end if;
633 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
634 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
636 -- Handle concurrent types
638 if Is_Concurrent_Type (Desig_Typ)
639 and then Present (Corresponding_Record_Type (Desig_Typ))
640 then
641 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
642 end if;
644 -- Do not process allocations / deallocations without a pool
646 if No (Pool_Id) then
647 return;
649 -- Do not process allocations on / deallocations from the secondary
650 -- stack.
652 elsif Is_RTE (Pool_Id, RE_SS_Pool)
653 or else (Nkind (Expr) = N_Allocator
654 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool))
655 then
656 return;
658 -- Optimize the case where we are using the default Global_Pool_Object,
659 -- and we don't need the heavy finalization machinery.
661 elsif Pool_Id = RTE (RE_Global_Pool_Object)
662 and then not Needs_Finalization (Desig_Typ)
663 then
664 return;
666 -- Do not replicate the machinery if the allocator / free has already
667 -- been expanded and has a custom Allocate / Deallocate.
669 elsif Present (Proc_To_Call)
670 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
671 then
672 return;
673 end if;
675 -- Finalization actions are required when the object to be allocated or
676 -- deallocated needs these actions and the associated access type is not
677 -- subject to pragma No_Heap_Finalization.
679 Needs_Fin :=
680 Needs_Finalization (Desig_Typ)
681 and then not No_Heap_Finalization (Ptr_Typ);
683 if Needs_Fin then
685 -- Certain run-time configurations and targets do not provide support
686 -- for controlled types.
688 if Restriction_Active (No_Finalization) then
689 return;
691 -- Do nothing if the access type may never allocate / deallocate
692 -- objects.
694 elsif No_Pool_Assigned (Ptr_Typ) then
695 return;
696 end if;
698 -- The allocation / deallocation of a controlled object must be
699 -- chained on / detached from a finalization master.
701 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
703 -- The only other kind of allocation / deallocation supported by this
704 -- routine is on / from a subpool.
706 elsif Nkind (Expr) = N_Allocator
707 and then No (Subpool_Handle_Name (Expr))
708 then
709 return;
710 end if;
712 declare
713 Loc : constant Source_Ptr := Sloc (N);
714 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
715 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
716 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
717 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
719 Actuals : List_Id;
720 Fin_Addr_Id : Entity_Id;
721 Fin_Mas_Act : Node_Id;
722 Fin_Mas_Id : Entity_Id;
723 Proc_To_Call : Entity_Id;
724 Subpool : Node_Id := Empty;
726 begin
727 -- Step 1: Construct all the actuals for the call to library routine
728 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
730 -- a) Storage pool
732 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
734 if Is_Allocate then
736 -- b) Subpool
738 if Nkind (Expr) = N_Allocator then
739 Subpool := Subpool_Handle_Name (Expr);
740 end if;
742 -- If a subpool is present it can be an arbitrary name, so make
743 -- the actual by copying the tree.
745 if Present (Subpool) then
746 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
747 else
748 Append_To (Actuals, Make_Null (Loc));
749 end if;
751 -- c) Finalization master
753 if Needs_Fin then
754 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
755 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
757 -- Handle the case where the master is actually a pointer to a
758 -- master. This case arises in build-in-place functions.
760 if Is_Access_Type (Etype (Fin_Mas_Id)) then
761 Append_To (Actuals, Fin_Mas_Act);
762 else
763 Append_To (Actuals,
764 Make_Attribute_Reference (Loc,
765 Prefix => Fin_Mas_Act,
766 Attribute_Name => Name_Unrestricted_Access));
767 end if;
768 else
769 Append_To (Actuals, Make_Null (Loc));
770 end if;
772 -- d) Finalize_Address
774 -- Primitive Finalize_Address is never generated in CodePeer mode
775 -- since it contains an Unchecked_Conversion.
777 if Needs_Fin and then not CodePeer_Mode then
778 Fin_Addr_Id := Finalize_Address (Desig_Typ);
779 pragma Assert (Present (Fin_Addr_Id));
781 Append_To (Actuals,
782 Make_Attribute_Reference (Loc,
783 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
784 Attribute_Name => Name_Unrestricted_Access));
785 else
786 Append_To (Actuals, Make_Null (Loc));
787 end if;
788 end if;
790 -- e) Address
791 -- f) Storage_Size
792 -- g) Alignment
794 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
795 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
797 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
798 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
800 -- For deallocation of class-wide types we obtain the value of
801 -- alignment from the Type Specific Record of the deallocated object.
802 -- This is needed because the frontend expansion of class-wide types
803 -- into equivalent types confuses the back end.
805 else
806 -- Generate:
807 -- Obj.all'Alignment
809 -- ... because 'Alignment applied to class-wide types is expanded
810 -- into the code that reads the value of alignment from the TSD
811 -- (see Expand_N_Attribute_Reference)
813 Append_To (Actuals,
814 Unchecked_Convert_To (RTE (RE_Storage_Offset),
815 Make_Attribute_Reference (Loc,
816 Prefix =>
817 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
818 Attribute_Name => Name_Alignment)));
819 end if;
821 -- h) Is_Controlled
823 if Needs_Fin then
824 Is_Controlled : declare
825 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
826 Flag_Expr : Node_Id;
827 Param : Node_Id;
828 Pref : Node_Id;
829 Temp : Node_Id;
831 begin
832 if Is_Allocate then
833 Temp := Find_Object (Expression (Expr));
834 else
835 Temp := Expr;
836 end if;
838 -- Processing for allocations where the expression is a subtype
839 -- indication.
841 if Is_Allocate
842 and then Is_Entity_Name (Temp)
843 and then Is_Type (Entity (Temp))
844 then
845 Flag_Expr :=
846 New_Occurrence_Of
847 (Boolean_Literals
848 (Needs_Finalization (Entity (Temp))), Loc);
850 -- The allocation / deallocation of a class-wide object relies
851 -- on a runtime check to determine whether the object is truly
852 -- controlled or not. Depending on this check, the finalization
853 -- machinery will request or reclaim extra storage reserved for
854 -- a list header.
856 elsif Is_Class_Wide_Type (Desig_Typ) then
858 -- Detect a special case where interface class-wide types
859 -- are involved as the object appears as:
861 -- Tag_Ptr (Base_Address (<object>'Address))
863 -- The expression already yields the proper tag, generate:
865 -- Temp.all
867 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
868 Param :=
869 Make_Explicit_Dereference (Loc,
870 Prefix => Relocate_Node (Temp));
872 -- In the default case, obtain the tag of the object about
873 -- to be allocated / deallocated. Generate:
875 -- Temp'Tag
877 -- If the object is an unchecked conversion (typically to
878 -- an access to class-wide type), we must preserve the
879 -- conversion to ensure that the object is seen as tagged
880 -- in the code that follows.
882 else
883 Pref := Temp;
885 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
886 then
887 Pref := Parent (Pref);
888 end if;
890 Param :=
891 Make_Attribute_Reference (Loc,
892 Prefix => Relocate_Node (Pref),
893 Attribute_Name => Name_Tag);
894 end if;
896 -- Generate:
897 -- Needs_Finalization (<Param>)
899 Flag_Expr :=
900 Make_Function_Call (Loc,
901 Name =>
902 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
903 Parameter_Associations => New_List (Param));
905 -- Processing for generic actuals
907 elsif Is_Generic_Actual_Type (Desig_Typ) then
908 Flag_Expr :=
909 New_Occurrence_Of (Boolean_Literals
910 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
912 -- The object does not require any specialized checks, it is
913 -- known to be controlled.
915 else
916 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
917 end if;
919 -- Create the temporary which represents the finalization state
920 -- of the expression. Generate:
922 -- F : constant Boolean := <Flag_Expr>;
924 Insert_Action (N,
925 Make_Object_Declaration (Loc,
926 Defining_Identifier => Flag_Id,
927 Constant_Present => True,
928 Object_Definition =>
929 New_Occurrence_Of (Standard_Boolean, Loc),
930 Expression => Flag_Expr));
932 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
933 end Is_Controlled;
935 -- The object is not controlled
937 else
938 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
939 end if;
941 -- i) On_Subpool
943 if Is_Allocate then
944 Append_To (Actuals,
945 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
946 end if;
948 -- Step 2: Build a wrapper Allocate / Deallocate which internally
949 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
951 -- Select the proper routine to call
953 if Is_Allocate then
954 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
955 else
956 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
957 end if;
959 -- Create a custom Allocate / Deallocate routine which has identical
960 -- profile to that of System.Storage_Pools.
962 Insert_Action (N,
963 Make_Subprogram_Body (Loc,
964 Specification =>
966 -- procedure Pnn
968 Make_Procedure_Specification (Loc,
969 Defining_Unit_Name => Proc_Id,
970 Parameter_Specifications => New_List (
972 -- P : Root_Storage_Pool
974 Make_Parameter_Specification (Loc,
975 Defining_Identifier => Make_Temporary (Loc, 'P'),
976 Parameter_Type =>
977 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
979 -- A : [out] Address
981 Make_Parameter_Specification (Loc,
982 Defining_Identifier => Addr_Id,
983 Out_Present => Is_Allocate,
984 Parameter_Type =>
985 New_Occurrence_Of (RTE (RE_Address), Loc)),
987 -- S : Storage_Count
989 Make_Parameter_Specification (Loc,
990 Defining_Identifier => Size_Id,
991 Parameter_Type =>
992 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
994 -- L : Storage_Count
996 Make_Parameter_Specification (Loc,
997 Defining_Identifier => Alig_Id,
998 Parameter_Type =>
999 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
1001 Declarations => No_List,
1003 Handled_Statement_Sequence =>
1004 Make_Handled_Sequence_Of_Statements (Loc,
1005 Statements => New_List (
1006 Make_Procedure_Call_Statement (Loc,
1007 Name =>
1008 New_Occurrence_Of (Proc_To_Call, Loc),
1009 Parameter_Associations => Actuals)))),
1010 Suppress => All_Checks);
1012 -- The newly generated Allocate / Deallocate becomes the default
1013 -- procedure to call when the back end processes the allocation /
1014 -- deallocation.
1016 if Is_Allocate then
1017 Set_Procedure_To_Call (Expr, Proc_Id);
1018 else
1019 Set_Procedure_To_Call (N, Proc_Id);
1020 end if;
1021 end;
1022 end Build_Allocate_Deallocate_Proc;
1024 -------------------------------
1025 -- Build_Abort_Undefer_Block --
1026 -------------------------------
1028 function Build_Abort_Undefer_Block
1029 (Loc : Source_Ptr;
1030 Stmts : List_Id;
1031 Context : Node_Id) return Node_Id
1033 Exceptions_OK : constant Boolean :=
1034 not Restriction_Active (No_Exception_Propagation);
1036 AUD : Entity_Id;
1037 Blk : Node_Id;
1038 Blk_Id : Entity_Id;
1039 HSS : Node_Id;
1041 begin
1042 -- The block should be generated only when undeferring abort in the
1043 -- context of a potential exception.
1045 pragma Assert (Abort_Allowed and Exceptions_OK);
1047 -- Generate:
1048 -- begin
1049 -- <Stmts>
1050 -- at end
1051 -- Abort_Undefer_Direct;
1052 -- end;
1054 AUD := RTE (RE_Abort_Undefer_Direct);
1056 HSS :=
1057 Make_Handled_Sequence_Of_Statements (Loc,
1058 Statements => Stmts,
1059 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1061 Blk :=
1062 Make_Block_Statement (Loc,
1063 Handled_Statement_Sequence => HSS);
1064 Set_Is_Abort_Block (Blk);
1066 Add_Block_Identifier (Blk, Blk_Id);
1067 Expand_At_End_Handler (HSS, Blk_Id);
1069 -- Present the Abort_Undefer_Direct function to the back end to inline
1070 -- the call to the routine.
1072 Add_Inlined_Body (AUD, Context);
1074 return Blk;
1075 end Build_Abort_Undefer_Block;
1077 ---------------------------------
1078 -- Build_Class_Wide_Expression --
1079 ---------------------------------
1081 procedure Build_Class_Wide_Expression
1082 (Prag : Node_Id;
1083 Subp : Entity_Id;
1084 Par_Subp : Entity_Id;
1085 Adjust_Sloc : Boolean;
1086 Needs_Wrapper : out Boolean)
1088 function Replace_Entity (N : Node_Id) return Traverse_Result;
1089 -- Replace reference to formal of inherited operation or to primitive
1090 -- operation of root type, with corresponding entity for derived type,
1091 -- when constructing the class-wide condition of an overriding
1092 -- subprogram.
1094 --------------------
1095 -- Replace_Entity --
1096 --------------------
1098 function Replace_Entity (N : Node_Id) return Traverse_Result is
1099 New_E : Entity_Id;
1101 begin
1102 if Adjust_Sloc then
1103 Adjust_Inherited_Pragma_Sloc (N);
1104 end if;
1106 if Nkind (N) = N_Identifier
1107 and then Present (Entity (N))
1108 and then
1109 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1110 and then
1111 (Nkind (Parent (N)) /= N_Attribute_Reference
1112 or else Attribute_Name (Parent (N)) /= Name_Class)
1113 then
1114 -- The replacement does not apply to dispatching calls within the
1115 -- condition, but only to calls whose static tag is that of the
1116 -- parent type.
1118 if Is_Subprogram (Entity (N))
1119 and then Nkind (Parent (N)) = N_Function_Call
1120 and then Present (Controlling_Argument (Parent (N)))
1121 then
1122 return OK;
1123 end if;
1125 -- Determine whether entity has a renaming
1127 New_E := Type_Map.Get (Entity (N));
1129 if Present (New_E) then
1130 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1132 -- If the entity is an overridden primitive and we are not
1133 -- in GNATprove mode, we must build a wrapper for the current
1134 -- inherited operation. If the reference is the prefix of an
1135 -- attribute such as 'Result (or others ???) there is no need
1136 -- for a wrapper: the condition is just rewritten in terms of
1137 -- the inherited subprogram.
1139 if Is_Subprogram (New_E)
1140 and then Nkind (Parent (N)) /= N_Attribute_Reference
1141 and then not GNATprove_Mode
1142 then
1143 Needs_Wrapper := True;
1144 end if;
1145 end if;
1147 -- Check that there are no calls left to abstract operations if
1148 -- the current subprogram is not abstract.
1150 if Nkind (Parent (N)) = N_Function_Call
1151 and then N = Name (Parent (N))
1152 then
1153 if not Is_Abstract_Subprogram (Subp)
1154 and then Is_Abstract_Subprogram (Entity (N))
1155 then
1156 Error_Msg_Sloc := Sloc (Current_Scope);
1157 Error_Msg_Node_2 := Subp;
1158 if Comes_From_Source (Subp) then
1159 Error_Msg_NE
1160 ("cannot call abstract subprogram & in inherited "
1161 & "condition for&#", Subp, Entity (N));
1162 else
1163 Error_Msg_NE
1164 ("cannot call abstract subprogram & in inherited "
1165 & "condition for inherited&#", Subp, Entity (N));
1166 end if;
1168 -- In SPARK mode, reject an inherited condition for an
1169 -- inherited operation if it contains a call to an overriding
1170 -- operation, because this implies that the pre/postconditions
1171 -- of the inherited operation have changed silently.
1173 elsif SPARK_Mode = On
1174 and then Warn_On_Suspicious_Contract
1175 and then Present (Alias (Subp))
1176 and then Present (New_E)
1177 and then Comes_From_Source (New_E)
1178 then
1179 Error_Msg_N
1180 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1181 Parent (Subp));
1182 Error_Msg_Sloc := Sloc (New_E);
1183 Error_Msg_Node_2 := Subp;
1184 Error_Msg_NE
1185 ("\overriding of&# forces overriding of&",
1186 Parent (Subp), New_E);
1187 end if;
1188 end if;
1190 -- Update type of function call node, which should be the same as
1191 -- the function's return type.
1193 if Is_Subprogram (Entity (N))
1194 and then Nkind (Parent (N)) = N_Function_Call
1195 then
1196 Set_Etype (Parent (N), Etype (Entity (N)));
1197 end if;
1199 -- The whole expression will be reanalyzed
1201 elsif Nkind (N) in N_Has_Etype then
1202 Set_Analyzed (N, False);
1203 end if;
1205 return OK;
1206 end Replace_Entity;
1208 procedure Replace_Condition_Entities is
1209 new Traverse_Proc (Replace_Entity);
1211 -- Local variables
1213 Par_Formal : Entity_Id;
1214 Subp_Formal : Entity_Id;
1216 -- Start of processing for Build_Class_Wide_Expression
1218 begin
1219 Needs_Wrapper := False;
1221 -- Add mapping from old formals to new formals
1223 Par_Formal := First_Formal (Par_Subp);
1224 Subp_Formal := First_Formal (Subp);
1226 while Present (Par_Formal) and then Present (Subp_Formal) loop
1227 Type_Map.Set (Par_Formal, Subp_Formal);
1228 Next_Formal (Par_Formal);
1229 Next_Formal (Subp_Formal);
1230 end loop;
1232 Replace_Condition_Entities (Prag);
1233 end Build_Class_Wide_Expression;
1235 --------------------
1236 -- Build_DIC_Call --
1237 --------------------
1239 function Build_DIC_Call
1240 (Loc : Source_Ptr;
1241 Obj_Id : Entity_Id;
1242 Typ : Entity_Id) return Node_Id
1244 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1245 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1247 begin
1248 return
1249 Make_Procedure_Call_Statement (Loc,
1250 Name => New_Occurrence_Of (Proc_Id, Loc),
1251 Parameter_Associations => New_List (
1252 Make_Unchecked_Type_Conversion (Loc,
1253 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1254 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1255 end Build_DIC_Call;
1257 ------------------------------
1258 -- Build_DIC_Procedure_Body --
1259 ------------------------------
1261 -- WARNING: This routine manages Ghost regions. Return statements must be
1262 -- replaced by gotos which jump to the end of the routine and restore the
1263 -- Ghost mode.
1265 procedure Build_DIC_Procedure_Body
1266 (Typ : Entity_Id;
1267 For_Freeze : Boolean := False)
1269 procedure Add_DIC_Check
1270 (DIC_Prag : Node_Id;
1271 DIC_Expr : Node_Id;
1272 Stmts : in out List_Id);
1273 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1274 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1275 -- is added to list Stmts.
1277 procedure Add_Inherited_DIC
1278 (DIC_Prag : Node_Id;
1279 Par_Typ : Entity_Id;
1280 Deriv_Typ : Entity_Id;
1281 Stmts : in out List_Id);
1282 -- Add a runtime check to verify the assertion expression of inherited
1283 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1284 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1285 -- pragma. All generated code is added to list Stmts.
1287 procedure Add_Inherited_Tagged_DIC
1288 (DIC_Prag : Node_Id;
1289 Par_Typ : Entity_Id;
1290 Deriv_Typ : Entity_Id;
1291 Stmts : in out List_Id);
1292 -- Add a runtime check to verify assertion expression DIC_Expr of
1293 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1294 -- postcondition-like runtime semantics to the check. Par_Typ is the
1295 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1296 -- derived type inheriting the DIC pragma. All generated code is added
1297 -- to list Stmts.
1299 procedure Add_Own_DIC
1300 (DIC_Prag : Node_Id;
1301 DIC_Typ : Entity_Id;
1302 Stmts : in out List_Id);
1303 -- Add a runtime check to verify the assertion expression of pragma
1304 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1305 -- is added to list Stmts.
1307 -------------------
1308 -- Add_DIC_Check --
1309 -------------------
1311 procedure Add_DIC_Check
1312 (DIC_Prag : Node_Id;
1313 DIC_Expr : Node_Id;
1314 Stmts : in out List_Id)
1316 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1317 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1319 begin
1320 -- The DIC pragma is ignored, nothing left to do
1322 if Is_Ignored (DIC_Prag) then
1323 null;
1325 -- Otherwise the DIC expression must be checked at run time.
1326 -- Generate:
1328 -- pragma Check (<Nam>, <DIC_Expr>);
1330 else
1331 Append_New_To (Stmts,
1332 Make_Pragma (Loc,
1333 Pragma_Identifier =>
1334 Make_Identifier (Loc, Name_Check),
1336 Pragma_Argument_Associations => New_List (
1337 Make_Pragma_Argument_Association (Loc,
1338 Expression => Make_Identifier (Loc, Nam)),
1340 Make_Pragma_Argument_Association (Loc,
1341 Expression => DIC_Expr))));
1342 end if;
1343 end Add_DIC_Check;
1345 -----------------------
1346 -- Add_Inherited_DIC --
1347 -----------------------
1349 procedure Add_Inherited_DIC
1350 (DIC_Prag : Node_Id;
1351 Par_Typ : Entity_Id;
1352 Deriv_Typ : Entity_Id;
1353 Stmts : in out List_Id)
1355 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1356 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1357 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1358 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1359 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1361 begin
1362 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1364 -- Verify the inherited DIC assertion expression by calling the DIC
1365 -- procedure of the parent type.
1367 -- Generate:
1368 -- <Par_Typ>DIC (Par_Typ (_object));
1370 Append_New_To (Stmts,
1371 Make_Procedure_Call_Statement (Loc,
1372 Name => New_Occurrence_Of (Par_Proc, Loc),
1373 Parameter_Associations => New_List (
1374 Convert_To
1375 (Typ => Etype (Par_Obj),
1376 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1377 end Add_Inherited_DIC;
1379 ------------------------------
1380 -- Add_Inherited_Tagged_DIC --
1381 ------------------------------
1383 procedure Add_Inherited_Tagged_DIC
1384 (DIC_Prag : Node_Id;
1385 Par_Typ : Entity_Id;
1386 Deriv_Typ : Entity_Id;
1387 Stmts : in out List_Id)
1389 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1390 DIC_Args : constant List_Id :=
1391 Pragma_Argument_Associations (DIC_Prag);
1392 DIC_Arg : constant Node_Id := First (DIC_Args);
1393 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1394 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1396 Expr : Node_Id;
1398 begin
1399 -- The processing of an inherited DIC assertion expression starts off
1400 -- with a copy of the original parent expression where all references
1401 -- to the parent type have already been replaced with references to
1402 -- the _object formal parameter of the parent type's DIC procedure.
1404 pragma Assert (Present (DIC_Expr));
1405 Expr := New_Copy_Tree (DIC_Expr);
1407 -- Perform the following substitutions:
1409 -- * Replace a reference to the _object parameter of the parent
1410 -- type's DIC procedure with a reference to the _object parameter
1411 -- of the derived types' DIC procedure.
1413 -- * Replace a reference to a discriminant of the parent type with
1414 -- a suitable value from the point of view of the derived type.
1416 -- * Replace a call to an overridden parent primitive with a call
1417 -- to the overriding derived type primitive.
1419 -- * Replace a call to an inherited parent primitive with a call to
1420 -- the internally-generated inherited derived type primitive.
1422 -- Note that primitives defined in the private part are automatically
1423 -- handled by the overriding/inheritance mechanism and do not require
1424 -- an extra replacement pass.
1426 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1428 Replace_References
1429 (Expr => Expr,
1430 Par_Typ => Par_Typ,
1431 Deriv_Typ => Deriv_Typ,
1432 Par_Obj => First_Formal (Par_Proc),
1433 Deriv_Obj => First_Formal (Deriv_Proc));
1435 -- Once the DIC assertion expression is fully processed, add a check
1436 -- to the statements of the DIC procedure.
1438 Add_DIC_Check
1439 (DIC_Prag => DIC_Prag,
1440 DIC_Expr => Expr,
1441 Stmts => Stmts);
1442 end Add_Inherited_Tagged_DIC;
1444 -----------------
1445 -- Add_Own_DIC --
1446 -----------------
1448 procedure Add_Own_DIC
1449 (DIC_Prag : Node_Id;
1450 DIC_Typ : Entity_Id;
1451 Stmts : in out List_Id)
1453 DIC_Args : constant List_Id :=
1454 Pragma_Argument_Associations (DIC_Prag);
1455 DIC_Arg : constant Node_Id := First (DIC_Args);
1456 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1457 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1458 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1459 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1461 procedure Preanalyze_Own_DIC_For_ASIS;
1462 -- Preanalyze the original DIC expression of an aspect or a source
1463 -- pragma for ASIS.
1465 ---------------------------------
1466 -- Preanalyze_Own_DIC_For_ASIS --
1467 ---------------------------------
1469 procedure Preanalyze_Own_DIC_For_ASIS is
1470 Expr : Node_Id := Empty;
1472 begin
1473 -- The DIC pragma is a source construct, preanalyze the original
1474 -- expression of the pragma.
1476 if Comes_From_Source (DIC_Prag) then
1477 Expr := DIC_Expr;
1479 -- Otherwise preanalyze the expression of the corresponding aspect
1481 elsif Present (DIC_Asp) then
1482 Expr := Expression (DIC_Asp);
1483 end if;
1485 -- The expression must be subjected to the same substitutions as
1486 -- the copy used in the generation of the runtime check.
1488 if Present (Expr) then
1489 Replace_Type_References
1490 (Expr => Expr,
1491 Typ => DIC_Typ,
1492 Obj_Id => Obj_Id);
1494 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1495 end if;
1496 end Preanalyze_Own_DIC_For_ASIS;
1498 -- Local variables
1500 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1502 Expr : Node_Id;
1504 -- Start of processing for Add_Own_DIC
1506 begin
1507 pragma Assert (Present (DIC_Expr));
1508 Expr := New_Copy_Tree (DIC_Expr);
1510 -- Perform the following substitution:
1512 -- * Replace the current instance of DIC_Typ with a reference to
1513 -- the _object formal parameter of the DIC procedure.
1515 Replace_Type_References
1516 (Expr => Expr,
1517 Typ => DIC_Typ,
1518 Obj_Id => Obj_Id);
1520 -- Preanalyze the DIC expression to detect errors and at the same
1521 -- time capture the visibility of the proper package part.
1523 Set_Parent (Expr, Typ_Decl);
1524 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1526 -- Save a copy of the expression with all replacements and analysis
1527 -- already taken place in case a derived type inherits the pragma.
1528 -- The copy will be used as the foundation of the derived type's own
1529 -- version of the DIC assertion expression.
1531 if Is_Tagged_Type (DIC_Typ) then
1532 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1533 end if;
1535 -- If the pragma comes from an aspect specification, replace the
1536 -- saved expression because all type references must be substituted
1537 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1538 -- routines.
1540 if Present (DIC_Asp) then
1541 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1542 end if;
1544 -- Preanalyze the original DIC expression for ASIS
1546 if ASIS_Mode then
1547 Preanalyze_Own_DIC_For_ASIS;
1548 end if;
1550 -- Once the DIC assertion expression is fully processed, add a check
1551 -- to the statements of the DIC procedure.
1553 Add_DIC_Check
1554 (DIC_Prag => DIC_Prag,
1555 DIC_Expr => Expr,
1556 Stmts => Stmts);
1557 end Add_Own_DIC;
1559 -- Local variables
1561 Loc : constant Source_Ptr := Sloc (Typ);
1563 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1564 -- Save the Ghost mode to restore on exit
1566 DIC_Prag : Node_Id;
1567 DIC_Typ : Entity_Id;
1568 Dummy_1 : Entity_Id;
1569 Dummy_2 : Entity_Id;
1570 Proc_Body : Node_Id;
1571 Proc_Body_Id : Entity_Id;
1572 Proc_Decl : Node_Id;
1573 Proc_Id : Entity_Id;
1574 Stmts : List_Id := No_List;
1576 Build_Body : Boolean := False;
1577 -- Flag set when the type requires a DIC procedure body to be built
1579 Work_Typ : Entity_Id;
1580 -- The working type
1582 -- Start of processing for Build_DIC_Procedure_Body
1584 begin
1585 Work_Typ := Base_Type (Typ);
1587 -- Do not process class-wide types as these are Itypes, but lack a first
1588 -- subtype (see below).
1590 if Is_Class_Wide_Type (Work_Typ) then
1591 return;
1593 -- Do not process the underlying full view of a private type. There is
1594 -- no way to get back to the partial view, plus the body will be built
1595 -- by the full view or the base type.
1597 elsif Is_Underlying_Full_View (Work_Typ) then
1598 return;
1600 -- Use the first subtype when dealing with various base types
1602 elsif Is_Itype (Work_Typ) then
1603 Work_Typ := First_Subtype (Work_Typ);
1605 -- The input denotes the corresponding record type of a protected or a
1606 -- task type. Work with the concurrent type because the corresponding
1607 -- record type may not be visible to clients of the type.
1609 elsif Ekind (Work_Typ) = E_Record_Type
1610 and then Is_Concurrent_Record_Type (Work_Typ)
1611 then
1612 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1613 end if;
1615 -- The working type may be subject to pragma Ghost. Set the mode now to
1616 -- ensure that the DIC procedure is properly marked as Ghost.
1618 Set_Ghost_Mode (Work_Typ);
1620 -- The working type must be either define a DIC pragma of its own or
1621 -- inherit one from a parent type.
1623 pragma Assert (Has_DIC (Work_Typ));
1625 -- Recover the type which defines the DIC pragma. This is either the
1626 -- working type itself or a parent type when the pragma is inherited.
1628 DIC_Typ := Find_DIC_Type (Work_Typ);
1629 pragma Assert (Present (DIC_Typ));
1631 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1632 pragma Assert (Present (DIC_Prag));
1634 -- Nothing to do if pragma DIC appears without an argument or its sole
1635 -- argument is "null".
1637 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1638 goto Leave;
1639 end if;
1641 -- The working type may lack a DIC procedure declaration. This may be
1642 -- due to several reasons:
1644 -- * The working type's own DIC pragma does not contain a verifiable
1645 -- assertion expression. In this case there is no need to build a
1646 -- DIC procedure because there is nothing to check.
1648 -- * The working type derives from a parent type. In this case a DIC
1649 -- procedure should be built only when the inherited DIC pragma has
1650 -- a verifiable assertion expression.
1652 Proc_Id := DIC_Procedure (Work_Typ);
1654 -- Build a DIC procedure declaration when the working type derives from
1655 -- a parent type.
1657 if No (Proc_Id) then
1658 Build_DIC_Procedure_Declaration (Work_Typ);
1659 Proc_Id := DIC_Procedure (Work_Typ);
1660 end if;
1662 -- At this point there should be a DIC procedure declaration
1664 pragma Assert (Present (Proc_Id));
1665 Proc_Decl := Unit_Declaration_Node (Proc_Id);
1667 -- Nothing to do if the DIC procedure already has a body
1669 if Present (Corresponding_Body (Proc_Decl)) then
1670 goto Leave;
1671 end if;
1673 -- Emulate the environment of the DIC procedure by installing its scope
1674 -- and formal parameters.
1676 Push_Scope (Proc_Id);
1677 Install_Formals (Proc_Id);
1679 -- The working type defines its own DIC pragma. Replace the current
1680 -- instance of the working type with the formal of the DIC procedure.
1681 -- Note that there is no need to consider inherited DIC pragmas from
1682 -- parent types because the working type's DIC pragma "hides" all
1683 -- inherited DIC pragmas.
1685 if Has_Own_DIC (Work_Typ) then
1686 pragma Assert (DIC_Typ = Work_Typ);
1688 Add_Own_DIC
1689 (DIC_Prag => DIC_Prag,
1690 DIC_Typ => DIC_Typ,
1691 Stmts => Stmts);
1693 Build_Body := True;
1695 -- Otherwise the working type inherits a DIC pragma from a parent type.
1696 -- This processing is carried out when the type is frozen because the
1697 -- state of all parent discriminants is known at that point. Note that
1698 -- it is semantically sound to delay the creation of the DIC procedure
1699 -- body till the freeze point. If the type has a DIC pragma of its own,
1700 -- then the DIC procedure body would have already been constructed at
1701 -- the end of the visible declarations and all parent DIC pragmas are
1702 -- effectively "hidden" and irrelevant.
1704 elsif For_Freeze then
1705 pragma Assert (Has_Inherited_DIC (Work_Typ));
1706 pragma Assert (DIC_Typ /= Work_Typ);
1708 -- The working type is tagged. The verification of the assertion
1709 -- expression is subject to the same semantics as class-wide pre-
1710 -- and postconditions.
1712 if Is_Tagged_Type (Work_Typ) then
1713 Add_Inherited_Tagged_DIC
1714 (DIC_Prag => DIC_Prag,
1715 Par_Typ => DIC_Typ,
1716 Deriv_Typ => Work_Typ,
1717 Stmts => Stmts);
1719 -- Otherwise the working type is not tagged. Verify the assertion
1720 -- expression of the inherited DIC pragma by directly calling the
1721 -- DIC procedure of the parent type.
1723 else
1724 Add_Inherited_DIC
1725 (DIC_Prag => DIC_Prag,
1726 Par_Typ => DIC_Typ,
1727 Deriv_Typ => Work_Typ,
1728 Stmts => Stmts);
1729 end if;
1731 Build_Body := True;
1732 end if;
1734 End_Scope;
1736 if Build_Body then
1738 -- Produce an empty completing body in the following cases:
1739 -- * Assertions are disabled
1740 -- * The DIC Assertion_Policy is Ignore
1742 if No (Stmts) then
1743 Stmts := New_List (Make_Null_Statement (Loc));
1744 end if;
1746 -- Generate:
1747 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1748 -- begin
1749 -- <Stmts>
1750 -- end <Work_Typ>DIC;
1752 Proc_Body :=
1753 Make_Subprogram_Body (Loc,
1754 Specification =>
1755 Copy_Subprogram_Spec (Parent (Proc_Id)),
1756 Declarations => Empty_List,
1757 Handled_Statement_Sequence =>
1758 Make_Handled_Sequence_Of_Statements (Loc,
1759 Statements => Stmts));
1760 Proc_Body_Id := Defining_Entity (Proc_Body);
1762 -- Perform minor decoration in case the body is not analyzed
1764 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1765 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1766 Set_Scope (Proc_Body_Id, Current_Scope);
1767 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
1768 Set_SPARK_Pragma_Inherited
1769 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
1771 -- Link both spec and body to avoid generating duplicates
1773 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1774 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1776 -- The body should not be inserted into the tree when the context
1777 -- is ASIS or a generic unit because it is not part of the template.
1778 -- Note that the body must still be generated in order to resolve the
1779 -- DIC assertion expression.
1781 if ASIS_Mode or Inside_A_Generic then
1782 null;
1784 -- Semi-insert the body into the tree for GNATprove by setting its
1785 -- Parent field. This allows for proper upstream tree traversals.
1787 elsif GNATprove_Mode then
1788 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1790 -- Otherwise the body is part of the freezing actions of the working
1791 -- type.
1793 else
1794 Append_Freeze_Action (Work_Typ, Proc_Body);
1795 end if;
1796 end if;
1798 <<Leave>>
1799 Restore_Ghost_Mode (Saved_GM);
1800 end Build_DIC_Procedure_Body;
1802 -------------------------------------
1803 -- Build_DIC_Procedure_Declaration --
1804 -------------------------------------
1806 -- WARNING: This routine manages Ghost regions. Return statements must be
1807 -- replaced by gotos which jump to the end of the routine and restore the
1808 -- Ghost mode.
1810 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1811 Loc : constant Source_Ptr := Sloc (Typ);
1813 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1814 -- Save the Ghost mode to restore on exit
1816 DIC_Prag : Node_Id;
1817 DIC_Typ : Entity_Id;
1818 Proc_Decl : Node_Id;
1819 Proc_Id : Entity_Id;
1820 Typ_Decl : Node_Id;
1822 CRec_Typ : Entity_Id;
1823 -- The corresponding record type of Full_Typ
1825 Full_Base : Entity_Id;
1826 -- The base type of Full_Typ
1828 Full_Typ : Entity_Id;
1829 -- The full view of working type
1831 Obj_Id : Entity_Id;
1832 -- The _object formal parameter of the DIC procedure
1834 Priv_Typ : Entity_Id;
1835 -- The partial view of working type
1837 Work_Typ : Entity_Id;
1838 -- The working type
1840 begin
1841 Work_Typ := Base_Type (Typ);
1843 -- Do not process class-wide types as these are Itypes, but lack a first
1844 -- subtype (see below).
1846 if Is_Class_Wide_Type (Work_Typ) then
1847 return;
1849 -- Do not process the underlying full view of a private type. There is
1850 -- no way to get back to the partial view, plus the body will be built
1851 -- by the full view or the base type.
1853 elsif Is_Underlying_Full_View (Work_Typ) then
1854 return;
1856 -- Use the first subtype when dealing with various base types
1858 elsif Is_Itype (Work_Typ) then
1859 Work_Typ := First_Subtype (Work_Typ);
1861 -- The input denotes the corresponding record type of a protected or a
1862 -- task type. Work with the concurrent type because the corresponding
1863 -- record type may not be visible to clients of the type.
1865 elsif Ekind (Work_Typ) = E_Record_Type
1866 and then Is_Concurrent_Record_Type (Work_Typ)
1867 then
1868 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1869 end if;
1871 -- The working type may be subject to pragma Ghost. Set the mode now to
1872 -- ensure that the DIC procedure is properly marked as Ghost.
1874 Set_Ghost_Mode (Work_Typ);
1876 -- The type must be either subject to a DIC pragma or inherit one from a
1877 -- parent type.
1879 pragma Assert (Has_DIC (Work_Typ));
1881 -- Recover the type which defines the DIC pragma. This is either the
1882 -- working type itself or a parent type when the pragma is inherited.
1884 DIC_Typ := Find_DIC_Type (Work_Typ);
1885 pragma Assert (Present (DIC_Typ));
1887 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1888 pragma Assert (Present (DIC_Prag));
1890 -- Nothing to do if pragma DIC appears without an argument or its sole
1891 -- argument is "null".
1893 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1894 goto Leave;
1896 -- Nothing to do if the type already has a DIC procedure
1898 elsif Present (DIC_Procedure (Work_Typ)) then
1899 goto Leave;
1900 end if;
1902 Proc_Id :=
1903 Make_Defining_Identifier (Loc,
1904 Chars =>
1905 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1907 -- Perform minor decoration in case the declaration is not analyzed
1909 Set_Ekind (Proc_Id, E_Procedure);
1910 Set_Etype (Proc_Id, Standard_Void_Type);
1911 Set_Is_DIC_Procedure (Proc_Id);
1912 Set_Scope (Proc_Id, Current_Scope);
1913 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
1914 Set_SPARK_Pragma_Inherited (Proc_Id);
1916 Set_DIC_Procedure (Work_Typ, Proc_Id);
1918 -- The DIC procedure requires debug info when the assertion expression
1919 -- is subject to Source Coverage Obligations.
1921 if Generate_SCO then
1922 Set_Needs_Debug_Info (Proc_Id);
1923 end if;
1925 -- Obtain all views of the input type
1927 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1929 -- Associate the DIC procedure and various relevant flags with all views
1931 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1932 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1933 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1934 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1936 -- The declaration of the DIC procedure must be inserted after the
1937 -- declaration of the partial view as this allows for proper external
1938 -- visibility.
1940 if Present (Priv_Typ) then
1941 Typ_Decl := Declaration_Node (Priv_Typ);
1943 -- Derived types with the full view as parent do not have a partial
1944 -- view. Insert the DIC procedure after the derived type.
1946 else
1947 Typ_Decl := Declaration_Node (Full_Typ);
1948 end if;
1950 -- The type should have a declarative node
1952 pragma Assert (Present (Typ_Decl));
1954 -- Create the formal parameter which emulates the variable-like behavior
1955 -- of the type's current instance.
1957 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1959 -- Perform minor decoration in case the declaration is not analyzed
1961 Set_Ekind (Obj_Id, E_In_Parameter);
1962 Set_Etype (Obj_Id, Work_Typ);
1963 Set_Scope (Obj_Id, Proc_Id);
1965 Set_First_Entity (Proc_Id, Obj_Id);
1967 -- Generate:
1968 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1970 Proc_Decl :=
1971 Make_Subprogram_Declaration (Loc,
1972 Specification =>
1973 Make_Procedure_Specification (Loc,
1974 Defining_Unit_Name => Proc_Id,
1975 Parameter_Specifications => New_List (
1976 Make_Parameter_Specification (Loc,
1977 Defining_Identifier => Obj_Id,
1978 Parameter_Type =>
1979 New_Occurrence_Of (Work_Typ, Loc)))));
1981 -- The declaration should not be inserted into the tree when the context
1982 -- is ASIS or a generic unit because it is not part of the template.
1984 if ASIS_Mode or Inside_A_Generic then
1985 null;
1987 -- Semi-insert the declaration into the tree for GNATprove by setting
1988 -- its Parent field. This allows for proper upstream tree traversals.
1990 elsif GNATprove_Mode then
1991 Set_Parent (Proc_Decl, Parent (Typ_Decl));
1993 -- Otherwise insert the declaration
1995 else
1996 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
1997 end if;
1999 <<Leave>>
2000 Restore_Ghost_Mode (Saved_GM);
2001 end Build_DIC_Procedure_Declaration;
2003 ------------------------------------
2004 -- Build_Invariant_Procedure_Body --
2005 ------------------------------------
2007 -- WARNING: This routine manages Ghost regions. Return statements must be
2008 -- replaced by gotos which jump to the end of the routine and restore the
2009 -- Ghost mode.
2011 procedure Build_Invariant_Procedure_Body
2012 (Typ : Entity_Id;
2013 Partial_Invariant : Boolean := False)
2015 Loc : constant Source_Ptr := Sloc (Typ);
2017 Pragmas_Seen : Elist_Id := No_Elist;
2018 -- This list contains all invariant pragmas processed so far. The list
2019 -- is used to avoid generating redundant invariant checks.
2021 Produced_Check : Boolean := False;
2022 -- This flag tracks whether the type has produced at least one invariant
2023 -- check. The flag is used as a sanity check at the end of the routine.
2025 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2026 -- intentionally unnested to avoid deep indentation of code.
2028 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2029 -- they emit checks, loops (for arrays) and case statements (for record
2030 -- variant parts) only when there are invariants to verify. This keeps
2031 -- the body of the invariant procedure free of useless code.
2033 procedure Add_Array_Component_Invariants
2034 (T : Entity_Id;
2035 Obj_Id : Entity_Id;
2036 Checks : in out List_Id);
2037 -- Generate an invariant check for each component of array type T.
2038 -- Obj_Id denotes the entity of the _object formal parameter of the
2039 -- invariant procedure. All created checks are added to list Checks.
2041 procedure Add_Inherited_Invariants
2042 (T : Entity_Id;
2043 Priv_Typ : Entity_Id;
2044 Full_Typ : Entity_Id;
2045 Obj_Id : Entity_Id;
2046 Checks : in out List_Id);
2047 -- Generate an invariant check for each inherited class-wide invariant
2048 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2049 -- the partial and full view of the parent type. Obj_Id denotes the
2050 -- entity of the _object formal parameter of the invariant procedure.
2051 -- All created checks are added to list Checks.
2053 procedure Add_Interface_Invariants
2054 (T : Entity_Id;
2055 Obj_Id : Entity_Id;
2056 Checks : in out List_Id);
2057 -- Generate an invariant check for each inherited class-wide invariant
2058 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2059 -- entity of the _object formal parameter of the invariant procedure.
2060 -- All created checks are added to list Checks.
2062 procedure Add_Invariant_Check
2063 (Prag : Node_Id;
2064 Expr : Node_Id;
2065 Checks : in out List_Id;
2066 Inherited : Boolean := False);
2067 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2068 -- verify assertion expression Expr of pragma Prag. All generated code
2069 -- is added to list Checks. Flag Inherited should be set when the pragma
2070 -- is inherited from a parent or interface type.
2072 procedure Add_Own_Invariants
2073 (T : Entity_Id;
2074 Obj_Id : Entity_Id;
2075 Checks : in out List_Id;
2076 Priv_Item : Node_Id := Empty);
2077 -- Generate an invariant check for each invariant found for type T.
2078 -- Obj_Id denotes the entity of the _object formal parameter of the
2079 -- invariant procedure. All created checks are added to list Checks.
2080 -- Priv_Item denotes the first rep item of the private type.
2082 procedure Add_Parent_Invariants
2083 (T : Entity_Id;
2084 Obj_Id : Entity_Id;
2085 Checks : in out List_Id);
2086 -- Generate an invariant check for each inherited class-wide invariant
2087 -- coming from all parent types of type T. Obj_Id denotes the entity of
2088 -- the _object formal parameter of the invariant procedure. All created
2089 -- checks are added to list Checks.
2091 procedure Add_Record_Component_Invariants
2092 (T : Entity_Id;
2093 Obj_Id : Entity_Id;
2094 Checks : in out List_Id);
2095 -- Generate an invariant check for each component of record type T.
2096 -- Obj_Id denotes the entity of the _object formal parameter of the
2097 -- invariant procedure. All created checks are added to list Checks.
2099 ------------------------------------
2100 -- Add_Array_Component_Invariants --
2101 ------------------------------------
2103 procedure Add_Array_Component_Invariants
2104 (T : Entity_Id;
2105 Obj_Id : Entity_Id;
2106 Checks : in out List_Id)
2108 Comp_Typ : constant Entity_Id := Component_Type (T);
2109 Dims : constant Pos := Number_Dimensions (T);
2111 procedure Process_Array_Component
2112 (Indices : List_Id;
2113 Comp_Checks : in out List_Id);
2114 -- Generate an invariant check for an array component identified by
2115 -- the indices in list Indices. All created checks are added to list
2116 -- Comp_Checks.
2118 procedure Process_One_Dimension
2119 (Dim : Pos;
2120 Indices : List_Id;
2121 Dim_Checks : in out List_Id);
2122 -- Generate a loop over the Nth dimension Dim of an array type. List
2123 -- Indices contains all array indices for the dimension. All created
2124 -- checks are added to list Dim_Checks.
2126 -----------------------------
2127 -- Process_Array_Component --
2128 -----------------------------
2130 procedure Process_Array_Component
2131 (Indices : List_Id;
2132 Comp_Checks : in out List_Id)
2134 Proc_Id : Entity_Id;
2136 begin
2137 if Has_Invariants (Comp_Typ) then
2139 -- In GNATprove mode, the component invariants are checked by
2140 -- other means. They should not be added to the array type
2141 -- invariant procedure, so that the procedure can be used to
2142 -- check the array type invariants if any.
2144 if GNATprove_Mode then
2145 null;
2147 else
2148 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2150 -- The component type should have an invariant procedure
2151 -- if it has invariants of its own or inherits class-wide
2152 -- invariants from parent or interface types.
2154 pragma Assert (Present (Proc_Id));
2156 -- Generate:
2157 -- <Comp_Typ>Invariant (_object (<Indices>));
2159 -- Note that the invariant procedure may have a null body if
2160 -- assertions are disabled or Assertion_Policy Ignore is in
2161 -- effect.
2163 if not Has_Null_Body (Proc_Id) then
2164 Append_New_To (Comp_Checks,
2165 Make_Procedure_Call_Statement (Loc,
2166 Name =>
2167 New_Occurrence_Of (Proc_Id, Loc),
2168 Parameter_Associations => New_List (
2169 Make_Indexed_Component (Loc,
2170 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2171 Expressions => New_Copy_List (Indices)))));
2172 end if;
2173 end if;
2175 Produced_Check := True;
2176 end if;
2177 end Process_Array_Component;
2179 ---------------------------
2180 -- Process_One_Dimension --
2181 ---------------------------
2183 procedure Process_One_Dimension
2184 (Dim : Pos;
2185 Indices : List_Id;
2186 Dim_Checks : in out List_Id)
2188 Comp_Checks : List_Id := No_List;
2189 Index : Entity_Id;
2191 begin
2192 -- Generate the invariant checks for the array component after all
2193 -- dimensions have produced their respective loops.
2195 if Dim > Dims then
2196 Process_Array_Component
2197 (Indices => Indices,
2198 Comp_Checks => Dim_Checks);
2200 -- Otherwise create a loop for the current dimension
2202 else
2203 -- Create a new loop variable for each dimension
2205 Index :=
2206 Make_Defining_Identifier (Loc,
2207 Chars => New_External_Name ('I', Dim));
2208 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2210 Process_One_Dimension
2211 (Dim => Dim + 1,
2212 Indices => Indices,
2213 Dim_Checks => Comp_Checks);
2215 -- Generate:
2216 -- for I<Dim> in _object'Range (<Dim>) loop
2217 -- <Comp_Checks>
2218 -- end loop;
2220 -- Note that the invariant procedure may have a null body if
2221 -- assertions are disabled or Assertion_Policy Ignore is in
2222 -- effect.
2224 if Present (Comp_Checks) then
2225 Append_New_To (Dim_Checks,
2226 Make_Implicit_Loop_Statement (T,
2227 Identifier => Empty,
2228 Iteration_Scheme =>
2229 Make_Iteration_Scheme (Loc,
2230 Loop_Parameter_Specification =>
2231 Make_Loop_Parameter_Specification (Loc,
2232 Defining_Identifier => Index,
2233 Discrete_Subtype_Definition =>
2234 Make_Attribute_Reference (Loc,
2235 Prefix =>
2236 New_Occurrence_Of (Obj_Id, Loc),
2237 Attribute_Name => Name_Range,
2238 Expressions => New_List (
2239 Make_Integer_Literal (Loc, Dim))))),
2240 Statements => Comp_Checks));
2241 end if;
2242 end if;
2243 end Process_One_Dimension;
2245 -- Start of processing for Add_Array_Component_Invariants
2247 begin
2248 Process_One_Dimension
2249 (Dim => 1,
2250 Indices => New_List,
2251 Dim_Checks => Checks);
2252 end Add_Array_Component_Invariants;
2254 ------------------------------
2255 -- Add_Inherited_Invariants --
2256 ------------------------------
2258 procedure Add_Inherited_Invariants
2259 (T : Entity_Id;
2260 Priv_Typ : Entity_Id;
2261 Full_Typ : Entity_Id;
2262 Obj_Id : Entity_Id;
2263 Checks : in out List_Id)
2265 Deriv_Typ : Entity_Id;
2266 Expr : Node_Id;
2267 Prag : Node_Id;
2268 Prag_Expr : Node_Id;
2269 Prag_Expr_Arg : Node_Id;
2270 Prag_Typ : Node_Id;
2271 Prag_Typ_Arg : Node_Id;
2273 Par_Proc : Entity_Id;
2274 -- The "partial" invariant procedure of Par_Typ
2276 Par_Typ : Entity_Id;
2277 -- The suitable view of the parent type used in the substitution of
2278 -- type attributes.
2280 begin
2281 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2282 return;
2283 end if;
2285 -- When the type inheriting the class-wide invariant is a concurrent
2286 -- type, use the corresponding record type because it contains all
2287 -- primitive operations of the concurrent type and allows for proper
2288 -- substitution.
2290 if Is_Concurrent_Type (T) then
2291 Deriv_Typ := Corresponding_Record_Type (T);
2292 else
2293 Deriv_Typ := T;
2294 end if;
2296 pragma Assert (Present (Deriv_Typ));
2298 -- Determine which rep item chain to use. Precedence is given to that
2299 -- of the parent type's partial view since it usually carries all the
2300 -- class-wide invariants.
2302 if Present (Priv_Typ) then
2303 Prag := First_Rep_Item (Priv_Typ);
2304 else
2305 Prag := First_Rep_Item (Full_Typ);
2306 end if;
2308 while Present (Prag) loop
2309 if Nkind (Prag) = N_Pragma
2310 and then Pragma_Name (Prag) = Name_Invariant
2311 then
2312 -- Nothing to do if the pragma was already processed
2314 if Contains (Pragmas_Seen, Prag) then
2315 return;
2317 -- Nothing to do when the caller requests the processing of all
2318 -- inherited class-wide invariants, but the pragma does not
2319 -- fall in this category.
2321 elsif not Class_Present (Prag) then
2322 return;
2323 end if;
2325 -- Extract the arguments of the invariant pragma
2327 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2328 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2329 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2330 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2332 -- The pragma applies to the partial view of the parent type
2334 if Present (Priv_Typ)
2335 and then Entity (Prag_Typ) = Priv_Typ
2336 then
2337 Par_Typ := Priv_Typ;
2339 -- The pragma applies to the full view of the parent type
2341 elsif Present (Full_Typ)
2342 and then Entity (Prag_Typ) = Full_Typ
2343 then
2344 Par_Typ := Full_Typ;
2346 -- Otherwise the pragma does not belong to the parent type and
2347 -- should not be considered.
2349 else
2350 return;
2351 end if;
2353 -- Perform the following substitutions:
2355 -- * Replace a reference to the _object parameter of the
2356 -- parent type's partial invariant procedure with a
2357 -- reference to the _object parameter of the derived
2358 -- type's full invariant procedure.
2360 -- * Replace a reference to a discriminant of the parent type
2361 -- with a suitable value from the point of view of the
2362 -- derived type.
2364 -- * Replace a call to an overridden parent primitive with a
2365 -- call to the overriding derived type primitive.
2367 -- * Replace a call to an inherited parent primitive with a
2368 -- call to the internally-generated inherited derived type
2369 -- primitive.
2371 Expr := New_Copy_Tree (Prag_Expr);
2373 -- The parent type must have a "partial" invariant procedure
2374 -- because class-wide invariants are captured exclusively by
2375 -- it.
2377 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2378 pragma Assert (Present (Par_Proc));
2380 Replace_References
2381 (Expr => Expr,
2382 Par_Typ => Par_Typ,
2383 Deriv_Typ => Deriv_Typ,
2384 Par_Obj => First_Formal (Par_Proc),
2385 Deriv_Obj => Obj_Id);
2387 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2388 end if;
2390 Next_Rep_Item (Prag);
2391 end loop;
2392 end Add_Inherited_Invariants;
2394 ------------------------------
2395 -- Add_Interface_Invariants --
2396 ------------------------------
2398 procedure Add_Interface_Invariants
2399 (T : Entity_Id;
2400 Obj_Id : Entity_Id;
2401 Checks : in out List_Id)
2403 Iface_Elmt : Elmt_Id;
2404 Ifaces : Elist_Id;
2406 begin
2407 -- Generate an invariant check for each class-wide invariant coming
2408 -- from all interfaces implemented by type T.
2410 if Is_Tagged_Type (T) then
2411 Collect_Interfaces (T, Ifaces);
2413 -- Process the class-wide invariants of all implemented interfaces
2415 Iface_Elmt := First_Elmt (Ifaces);
2416 while Present (Iface_Elmt) loop
2418 -- The Full_Typ parameter is intentionally left Empty because
2419 -- interfaces are treated as the partial view of a private type
2420 -- in order to achieve uniformity with the general case.
2422 Add_Inherited_Invariants
2423 (T => T,
2424 Priv_Typ => Node (Iface_Elmt),
2425 Full_Typ => Empty,
2426 Obj_Id => Obj_Id,
2427 Checks => Checks);
2429 Next_Elmt (Iface_Elmt);
2430 end loop;
2431 end if;
2432 end Add_Interface_Invariants;
2434 -------------------------
2435 -- Add_Invariant_Check --
2436 -------------------------
2438 procedure Add_Invariant_Check
2439 (Prag : Node_Id;
2440 Expr : Node_Id;
2441 Checks : in out List_Id;
2442 Inherited : Boolean := False)
2444 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2445 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2446 Ploc : constant Source_Ptr := Sloc (Prag);
2447 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2449 Assoc : List_Id;
2450 Str : String_Id;
2452 begin
2453 -- The invariant is ignored, nothing left to do
2455 if Is_Ignored (Prag) then
2456 null;
2458 -- Otherwise the invariant is checked. Build a pragma Check to verify
2459 -- the expression at run time.
2461 else
2462 Assoc := New_List (
2463 Make_Pragma_Argument_Association (Ploc,
2464 Expression => Make_Identifier (Ploc, Nam)),
2465 Make_Pragma_Argument_Association (Ploc,
2466 Expression => Expr));
2468 -- Handle the String argument (if any)
2470 if Present (Str_Arg) then
2471 Str := Strval (Get_Pragma_Arg (Str_Arg));
2473 -- When inheriting an invariant, modify the message from
2474 -- "failed invariant" to "failed inherited invariant".
2476 if Inherited then
2477 String_To_Name_Buffer (Str);
2479 if Name_Buffer (1 .. 16) = "failed invariant" then
2480 Insert_Str_In_Name_Buffer ("inherited ", 8);
2481 Str := String_From_Name_Buffer;
2482 end if;
2483 end if;
2485 Append_To (Assoc,
2486 Make_Pragma_Argument_Association (Ploc,
2487 Expression => Make_String_Literal (Ploc, Str)));
2488 end if;
2490 -- Generate:
2491 -- pragma Check (<Nam>, <Expr>, <Str>);
2493 Append_New_To (Checks,
2494 Make_Pragma (Ploc,
2495 Chars => Name_Check,
2496 Pragma_Argument_Associations => Assoc));
2497 end if;
2499 -- Output an info message when inheriting an invariant and the
2500 -- listing option is enabled.
2502 if Inherited and Opt.List_Inherited_Aspects then
2503 Error_Msg_Sloc := Sloc (Prag);
2504 Error_Msg_N
2505 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2506 end if;
2508 -- Add the pragma to the list of processed pragmas
2510 Append_New_Elmt (Prag, Pragmas_Seen);
2511 Produced_Check := True;
2512 end Add_Invariant_Check;
2514 ---------------------------
2515 -- Add_Parent_Invariants --
2516 ---------------------------
2518 procedure Add_Parent_Invariants
2519 (T : Entity_Id;
2520 Obj_Id : Entity_Id;
2521 Checks : in out List_Id)
2523 Dummy_1 : Entity_Id;
2524 Dummy_2 : Entity_Id;
2526 Curr_Typ : Entity_Id;
2527 -- The entity of the current type being examined
2529 Full_Typ : Entity_Id;
2530 -- The full view of Par_Typ
2532 Par_Typ : Entity_Id;
2533 -- The entity of the parent type
2535 Priv_Typ : Entity_Id;
2536 -- The partial view of Par_Typ
2538 begin
2539 -- Do not process array types because they cannot have true parent
2540 -- types. This also prevents the generation of a duplicate invariant
2541 -- check when the input type is an array base type because its Etype
2542 -- denotes the first subtype, both of which share the same component
2543 -- type.
2545 if Is_Array_Type (T) then
2546 return;
2547 end if;
2549 -- Climb the parent type chain
2551 Curr_Typ := T;
2552 loop
2553 -- Do not consider subtypes as they inherit the invariants
2554 -- from their base types.
2556 Par_Typ := Base_Type (Etype (Curr_Typ));
2558 -- Stop the climb once the root of the parent chain is
2559 -- reached.
2561 exit when Curr_Typ = Par_Typ;
2563 -- Process the class-wide invariants of the parent type
2565 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2567 -- Process the elements of an array type
2569 if Is_Array_Type (Full_Typ) then
2570 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2572 -- Process the components of a record type
2574 elsif Ekind (Full_Typ) = E_Record_Type then
2575 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2576 end if;
2578 Add_Inherited_Invariants
2579 (T => T,
2580 Priv_Typ => Priv_Typ,
2581 Full_Typ => Full_Typ,
2582 Obj_Id => Obj_Id,
2583 Checks => Checks);
2585 Curr_Typ := Par_Typ;
2586 end loop;
2587 end Add_Parent_Invariants;
2589 ------------------------
2590 -- Add_Own_Invariants --
2591 ------------------------
2593 procedure Add_Own_Invariants
2594 (T : Entity_Id;
2595 Obj_Id : Entity_Id;
2596 Checks : in out List_Id;
2597 Priv_Item : Node_Id := Empty)
2599 ASIS_Expr : Node_Id;
2600 Expr : Node_Id;
2601 Prag : Node_Id;
2602 Prag_Asp : Node_Id;
2603 Prag_Expr : Node_Id;
2604 Prag_Expr_Arg : Node_Id;
2605 Prag_Typ : Node_Id;
2606 Prag_Typ_Arg : Node_Id;
2608 begin
2609 if not Present (T) then
2610 return;
2611 end if;
2613 Prag := First_Rep_Item (T);
2614 while Present (Prag) loop
2615 if Nkind (Prag) = N_Pragma
2616 and then Pragma_Name (Prag) = Name_Invariant
2617 then
2618 -- Stop the traversal of the rep item chain once a specific
2619 -- item is encountered.
2621 if Present (Priv_Item) and then Prag = Priv_Item then
2622 exit;
2623 end if;
2625 -- Nothing to do if the pragma was already processed
2627 if Contains (Pragmas_Seen, Prag) then
2628 return;
2629 end if;
2631 -- Extract the arguments of the invariant pragma
2633 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2634 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2635 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2636 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2637 Prag_Asp := Corresponding_Aspect (Prag);
2639 -- Verify the pragma belongs to T, otherwise the pragma applies
2640 -- to a parent type in which case it will be processed later by
2641 -- Add_Parent_Invariants or Add_Interface_Invariants.
2643 if Entity (Prag_Typ) /= T then
2644 return;
2645 end if;
2647 Expr := New_Copy_Tree (Prag_Expr);
2649 -- Substitute all references to type T with references to the
2650 -- _object formal parameter.
2652 Replace_Type_References (Expr, T, Obj_Id);
2654 -- Preanalyze the invariant expression to detect errors and at
2655 -- the same time capture the visibility of the proper package
2656 -- part.
2658 Set_Parent (Expr, Parent (Prag_Expr));
2659 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2661 -- Save a copy of the expression when T is tagged to detect
2662 -- errors and capture the visibility of the proper package part
2663 -- for the generation of inherited type invariants.
2665 if Is_Tagged_Type (T) then
2666 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2667 end if;
2669 -- If the pragma comes from an aspect specification, replace
2670 -- the saved expression because all type references must be
2671 -- substituted for the call to Preanalyze_Spec_Expression in
2672 -- Check_Aspect_At_xxx routines.
2674 if Present (Prag_Asp) then
2675 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2676 end if;
2678 -- Analyze the original invariant expression for ASIS
2680 if ASIS_Mode then
2681 ASIS_Expr := Empty;
2683 if Comes_From_Source (Prag) then
2684 ASIS_Expr := Prag_Expr;
2685 elsif Present (Prag_Asp) then
2686 ASIS_Expr := Expression (Prag_Asp);
2687 end if;
2689 if Present (ASIS_Expr) then
2690 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2691 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2692 end if;
2693 end if;
2695 Add_Invariant_Check (Prag, Expr, Checks);
2696 end if;
2698 Next_Rep_Item (Prag);
2699 end loop;
2700 end Add_Own_Invariants;
2702 -------------------------------------
2703 -- Add_Record_Component_Invariants --
2704 -------------------------------------
2706 procedure Add_Record_Component_Invariants
2707 (T : Entity_Id;
2708 Obj_Id : Entity_Id;
2709 Checks : in out List_Id)
2711 procedure Process_Component_List
2712 (Comp_List : Node_Id;
2713 CL_Checks : in out List_Id);
2714 -- Generate invariant checks for all record components found in
2715 -- component list Comp_List, including variant parts. All created
2716 -- checks are added to list CL_Checks.
2718 procedure Process_Record_Component
2719 (Comp_Id : Entity_Id;
2720 Comp_Checks : in out List_Id);
2721 -- Generate an invariant check for a record component identified by
2722 -- Comp_Id. All created checks are added to list Comp_Checks.
2724 ----------------------------
2725 -- Process_Component_List --
2726 ----------------------------
2728 procedure Process_Component_List
2729 (Comp_List : Node_Id;
2730 CL_Checks : in out List_Id)
2732 Comp : Node_Id;
2733 Var : Node_Id;
2734 Var_Alts : List_Id := No_List;
2735 Var_Checks : List_Id := No_List;
2736 Var_Stmts : List_Id;
2738 Produced_Variant_Check : Boolean := False;
2739 -- This flag tracks whether the component has produced at least
2740 -- one invariant check.
2742 begin
2743 -- Traverse the component items
2745 Comp := First (Component_Items (Comp_List));
2746 while Present (Comp) loop
2747 if Nkind (Comp) = N_Component_Declaration then
2749 -- Generate the component invariant check
2751 Process_Record_Component
2752 (Comp_Id => Defining_Entity (Comp),
2753 Comp_Checks => CL_Checks);
2754 end if;
2756 Next (Comp);
2757 end loop;
2759 -- Traverse the variant part
2761 if Present (Variant_Part (Comp_List)) then
2762 Var := First (Variants (Variant_Part (Comp_List)));
2763 while Present (Var) loop
2764 Var_Checks := No_List;
2766 -- Generate invariant checks for all components and variant
2767 -- parts that qualify.
2769 Process_Component_List
2770 (Comp_List => Component_List (Var),
2771 CL_Checks => Var_Checks);
2773 -- The components of the current variant produced at least
2774 -- one invariant check.
2776 if Present (Var_Checks) then
2777 Var_Stmts := Var_Checks;
2778 Produced_Variant_Check := True;
2780 -- Otherwise there are either no components with invariants,
2781 -- assertions are disabled, or Assertion_Policy Ignore is in
2782 -- effect.
2784 else
2785 Var_Stmts := New_List (Make_Null_Statement (Loc));
2786 end if;
2788 Append_New_To (Var_Alts,
2789 Make_Case_Statement_Alternative (Loc,
2790 Discrete_Choices =>
2791 New_Copy_List (Discrete_Choices (Var)),
2792 Statements => Var_Stmts));
2794 Next (Var);
2795 end loop;
2797 -- Create a case statement which verifies the invariant checks
2798 -- of a particular component list depending on the discriminant
2799 -- values only when there is at least one real invariant check.
2801 if Produced_Variant_Check then
2802 Append_New_To (CL_Checks,
2803 Make_Case_Statement (Loc,
2804 Expression =>
2805 Make_Selected_Component (Loc,
2806 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2807 Selector_Name =>
2808 New_Occurrence_Of
2809 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2810 Alternatives => Var_Alts));
2811 end if;
2812 end if;
2813 end Process_Component_List;
2815 ------------------------------
2816 -- Process_Record_Component --
2817 ------------------------------
2819 procedure Process_Record_Component
2820 (Comp_Id : Entity_Id;
2821 Comp_Checks : in out List_Id)
2823 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2824 Proc_Id : Entity_Id;
2826 Produced_Component_Check : Boolean := False;
2827 -- This flag tracks whether the component has produced at least
2828 -- one invariant check.
2830 begin
2831 -- Nothing to do for internal component _parent. Note that it is
2832 -- not desirable to check whether the component comes from source
2833 -- because protected type components are relocated to an internal
2834 -- corresponding record, but still need processing.
2836 if Chars (Comp_Id) = Name_uParent then
2837 return;
2838 end if;
2840 -- Verify the invariant of the component. Note that an access
2841 -- type may have an invariant when it acts as the full view of a
2842 -- private type and the invariant appears on the partial view. In
2843 -- this case verify the access value itself.
2845 if Has_Invariants (Comp_Typ) then
2847 -- In GNATprove mode, the component invariants are checked by
2848 -- other means. They should not be added to the record type
2849 -- invariant procedure, so that the procedure can be used to
2850 -- check the record type invariants if any.
2852 if GNATprove_Mode then
2853 null;
2855 else
2856 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2858 -- The component type should have an invariant procedure
2859 -- if it has invariants of its own or inherits class-wide
2860 -- invariants from parent or interface types.
2862 pragma Assert (Present (Proc_Id));
2864 -- Generate:
2865 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2867 -- Note that the invariant procedure may have a null body if
2868 -- assertions are disabled or Assertion_Policy Ignore is in
2869 -- effect.
2871 if not Has_Null_Body (Proc_Id) then
2872 Append_New_To (Comp_Checks,
2873 Make_Procedure_Call_Statement (Loc,
2874 Name =>
2875 New_Occurrence_Of (Proc_Id, Loc),
2876 Parameter_Associations => New_List (
2877 Make_Selected_Component (Loc,
2878 Prefix =>
2879 Unchecked_Convert_To
2880 (T, New_Occurrence_Of (Obj_Id, Loc)),
2881 Selector_Name =>
2882 New_Occurrence_Of (Comp_Id, Loc)))));
2883 end if;
2884 end if;
2886 Produced_Check := True;
2887 Produced_Component_Check := True;
2888 end if;
2890 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2891 Error_Msg_NE
2892 ("invariants cannot be checked on components of "
2893 & "unchecked_union type &?", Comp_Id, T);
2894 end if;
2895 end Process_Record_Component;
2897 -- Local variables
2899 Comps : Node_Id;
2900 Def : Node_Id;
2902 -- Start of processing for Add_Record_Component_Invariants
2904 begin
2905 -- An untagged derived type inherits the components of its parent
2906 -- type. In order to avoid creating redundant invariant checks, do
2907 -- not process the components now. Instead wait until the ultimate
2908 -- parent of the untagged derivation chain is reached.
2910 if not Is_Untagged_Derivation (T) then
2911 Def := Type_Definition (Parent (T));
2913 if Nkind (Def) = N_Derived_Type_Definition then
2914 Def := Record_Extension_Part (Def);
2915 end if;
2917 pragma Assert (Nkind (Def) = N_Record_Definition);
2918 Comps := Component_List (Def);
2920 if Present (Comps) then
2921 Process_Component_List
2922 (Comp_List => Comps,
2923 CL_Checks => Checks);
2924 end if;
2925 end if;
2926 end Add_Record_Component_Invariants;
2928 -- Local variables
2930 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2931 -- Save the Ghost mode to restore on exit
2933 Dummy : Entity_Id;
2934 Priv_Item : Node_Id;
2935 Proc_Body : Node_Id;
2936 Proc_Body_Id : Entity_Id;
2937 Proc_Decl : Node_Id;
2938 Proc_Id : Entity_Id;
2939 Stmts : List_Id := No_List;
2941 CRec_Typ : Entity_Id := Empty;
2942 -- The corresponding record type of Full_Typ
2944 Full_Proc : Entity_Id := Empty;
2945 -- The entity of the "full" invariant procedure
2947 Full_Typ : Entity_Id := Empty;
2948 -- The full view of the working type
2950 Obj_Id : Entity_Id := Empty;
2951 -- The _object formal parameter of the invariant procedure
2953 Part_Proc : Entity_Id := Empty;
2954 -- The entity of the "partial" invariant procedure
2956 Priv_Typ : Entity_Id := Empty;
2957 -- The partial view of the working type
2959 Work_Typ : Entity_Id := Empty;
2960 -- The working type
2962 -- Start of processing for Build_Invariant_Procedure_Body
2964 begin
2965 Work_Typ := Typ;
2967 -- The input type denotes the implementation base type of a constrained
2968 -- array type. Work with the first subtype as all invariant pragmas are
2969 -- on its rep item chain.
2971 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2972 Work_Typ := First_Subtype (Work_Typ);
2974 -- The input type denotes the corresponding record type of a protected
2975 -- or task type. Work with the concurrent type because the corresponding
2976 -- record type may not be visible to clients of the type.
2978 elsif Ekind (Work_Typ) = E_Record_Type
2979 and then Is_Concurrent_Record_Type (Work_Typ)
2980 then
2981 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2982 end if;
2984 -- The working type may be subject to pragma Ghost. Set the mode now to
2985 -- ensure that the invariant procedure is properly marked as Ghost.
2987 Set_Ghost_Mode (Work_Typ);
2989 -- The type must either have invariants of its own, inherit class-wide
2990 -- invariants from parent types or interfaces, or be an array or record
2991 -- type whose components have invariants.
2993 pragma Assert (Has_Invariants (Work_Typ));
2995 -- Interfaces are treated as the partial view of a private type in order
2996 -- to achieve uniformity with the general case.
2998 if Is_Interface (Work_Typ) then
2999 Priv_Typ := Work_Typ;
3001 -- Otherwise obtain both views of the type
3003 else
3004 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3005 end if;
3007 -- The caller requests a body for the partial invariant procedure
3009 if Partial_Invariant then
3010 Full_Proc := Invariant_Procedure (Work_Typ);
3011 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3013 -- The "full" invariant procedure body was already created
3015 if Present (Full_Proc)
3016 and then Present
3017 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3018 then
3019 -- This scenario happens only when the type is an untagged
3020 -- derivation from a private parent and the underlying full
3021 -- view was processed before the partial view.
3023 pragma Assert
3024 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3026 -- Nothing to do because the processing of the underlying full
3027 -- view already checked the invariants of the partial view.
3029 goto Leave;
3030 end if;
3032 -- Create a declaration for the "partial" invariant procedure if it
3033 -- is not available.
3035 if No (Proc_Id) then
3036 Build_Invariant_Procedure_Declaration
3037 (Typ => Work_Typ,
3038 Partial_Invariant => True);
3040 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3041 end if;
3043 -- The caller requests a body for the "full" invariant procedure
3045 else
3046 Proc_Id := Invariant_Procedure (Work_Typ);
3047 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3049 -- Create a declaration for the "full" invariant procedure if it is
3050 -- not available.
3052 if No (Proc_Id) then
3053 Build_Invariant_Procedure_Declaration (Work_Typ);
3054 Proc_Id := Invariant_Procedure (Work_Typ);
3055 end if;
3056 end if;
3058 -- At this point there should be an invariant procedure declaration
3060 pragma Assert (Present (Proc_Id));
3061 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3063 -- Nothing to do if the invariant procedure already has a body
3065 if Present (Corresponding_Body (Proc_Decl)) then
3066 goto Leave;
3067 end if;
3069 -- Emulate the environment of the invariant procedure by installing its
3070 -- scope and formal parameters. Note that this is not needed, but having
3071 -- the scope installed helps with the detection of invariant-related
3072 -- errors.
3074 Push_Scope (Proc_Id);
3075 Install_Formals (Proc_Id);
3077 Obj_Id := First_Formal (Proc_Id);
3078 pragma Assert (Present (Obj_Id));
3080 -- The "partial" invariant procedure verifies the invariants of the
3081 -- partial view only.
3083 if Partial_Invariant then
3084 pragma Assert (Present (Priv_Typ));
3086 Add_Own_Invariants
3087 (T => Priv_Typ,
3088 Obj_Id => Obj_Id,
3089 Checks => Stmts);
3091 -- Otherwise the "full" invariant procedure verifies the invariants of
3092 -- the full view, all array or record components, as well as class-wide
3093 -- invariants inherited from parent types or interfaces. In addition, it
3094 -- indirectly verifies the invariants of the partial view by calling the
3095 -- "partial" invariant procedure.
3097 else
3098 pragma Assert (Present (Full_Typ));
3100 -- Check the invariants of the partial view by calling the "partial"
3101 -- invariant procedure. Generate:
3103 -- <Work_Typ>Partial_Invariant (_object);
3105 if Present (Part_Proc) then
3106 Append_New_To (Stmts,
3107 Make_Procedure_Call_Statement (Loc,
3108 Name => New_Occurrence_Of (Part_Proc, Loc),
3109 Parameter_Associations => New_List (
3110 New_Occurrence_Of (Obj_Id, Loc))));
3112 Produced_Check := True;
3113 end if;
3115 Priv_Item := Empty;
3117 -- Derived subtypes do not have a partial view
3119 if Present (Priv_Typ) then
3121 -- The processing of the "full" invariant procedure intentionally
3122 -- skips the partial view because a) this may result in changes of
3123 -- visibility and b) lead to duplicate checks. However, when the
3124 -- full view is the underlying full view of an untagged derived
3125 -- type whose parent type is private, partial invariants appear on
3126 -- the rep item chain of the partial view only.
3128 -- package Pack_1 is
3129 -- type Root ... is private;
3130 -- private
3131 -- <full view of Root>
3132 -- end Pack_1;
3134 -- with Pack_1;
3135 -- package Pack_2 is
3136 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3137 -- <underlying full view of Child>
3138 -- end Pack_2;
3140 -- As a result, the processing of the full view must also consider
3141 -- all invariants of the partial view.
3143 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3144 null;
3146 -- Otherwise the invariants of the partial view are ignored
3148 else
3149 -- Note that the rep item chain is shared between the partial
3150 -- and full views of a type. To avoid processing the invariants
3151 -- of the partial view, signal the logic to stop when the first
3152 -- rep item of the partial view has been reached.
3154 Priv_Item := First_Rep_Item (Priv_Typ);
3156 -- Ignore the invariants of the partial view by eliminating the
3157 -- view.
3159 Priv_Typ := Empty;
3160 end if;
3161 end if;
3163 -- Process the invariants of the full view and in certain cases those
3164 -- of the partial view. This also handles any invariants on array or
3165 -- record components.
3167 Add_Own_Invariants
3168 (T => Priv_Typ,
3169 Obj_Id => Obj_Id,
3170 Checks => Stmts,
3171 Priv_Item => Priv_Item);
3173 Add_Own_Invariants
3174 (T => Full_Typ,
3175 Obj_Id => Obj_Id,
3176 Checks => Stmts,
3177 Priv_Item => Priv_Item);
3179 -- Process the elements of an array type
3181 if Is_Array_Type (Full_Typ) then
3182 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3184 -- Process the components of a record type
3186 elsif Ekind (Full_Typ) = E_Record_Type then
3187 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3189 -- Process the components of a corresponding record
3191 elsif Present (CRec_Typ) then
3192 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3193 end if;
3195 -- Process the inherited class-wide invariants of all parent types.
3196 -- This also handles any invariants on record components.
3198 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3200 -- Process the inherited class-wide invariants of all implemented
3201 -- interface types.
3203 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3204 end if;
3206 End_Scope;
3208 -- At this point there should be at least one invariant check. If this
3209 -- is not the case, then the invariant-related flags were not properly
3210 -- set, or there is a missing invariant procedure on one of the array
3211 -- or record components.
3213 pragma Assert (Produced_Check);
3215 -- Account for the case where assertions are disabled or all invariant
3216 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3217 -- empty body.
3219 if No (Stmts) then
3220 Stmts := New_List (Make_Null_Statement (Loc));
3221 end if;
3223 -- Generate:
3224 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3225 -- begin
3226 -- <Stmts>
3227 -- end <Work_Typ>[Partial_]Invariant;
3229 Proc_Body :=
3230 Make_Subprogram_Body (Loc,
3231 Specification =>
3232 Copy_Subprogram_Spec (Parent (Proc_Id)),
3233 Declarations => Empty_List,
3234 Handled_Statement_Sequence =>
3235 Make_Handled_Sequence_Of_Statements (Loc,
3236 Statements => Stmts));
3237 Proc_Body_Id := Defining_Entity (Proc_Body);
3239 -- Perform minor decoration in case the body is not analyzed
3241 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3242 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3243 Set_Scope (Proc_Body_Id, Current_Scope);
3245 -- Link both spec and body to avoid generating duplicates
3247 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3248 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3250 -- The body should not be inserted into the tree when the context is
3251 -- ASIS or a generic unit because it is not part of the template. Note
3252 -- that the body must still be generated in order to resolve the
3253 -- invariants.
3255 if ASIS_Mode or Inside_A_Generic then
3256 null;
3258 -- Semi-insert the body into the tree for GNATprove by setting its
3259 -- Parent field. This allows for proper upstream tree traversals.
3261 elsif GNATprove_Mode then
3262 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3264 -- Otherwise the body is part of the freezing actions of the type
3266 else
3267 Append_Freeze_Action (Work_Typ, Proc_Body);
3268 end if;
3270 <<Leave>>
3271 Restore_Ghost_Mode (Saved_GM);
3272 end Build_Invariant_Procedure_Body;
3274 -------------------------------------------
3275 -- Build_Invariant_Procedure_Declaration --
3276 -------------------------------------------
3278 -- WARNING: This routine manages Ghost regions. Return statements must be
3279 -- replaced by gotos which jump to the end of the routine and restore the
3280 -- Ghost mode.
3282 procedure Build_Invariant_Procedure_Declaration
3283 (Typ : Entity_Id;
3284 Partial_Invariant : Boolean := False)
3286 Loc : constant Source_Ptr := Sloc (Typ);
3288 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3289 -- Save the Ghost mode to restore on exit
3291 Proc_Decl : Node_Id;
3292 Proc_Id : Entity_Id;
3293 Proc_Nam : Name_Id;
3294 Typ_Decl : Node_Id;
3296 CRec_Typ : Entity_Id;
3297 -- The corresponding record type of Full_Typ
3299 Full_Base : Entity_Id;
3300 -- The base type of Full_Typ
3302 Full_Typ : Entity_Id;
3303 -- The full view of working type
3305 Obj_Id : Entity_Id;
3306 -- The _object formal parameter of the invariant procedure
3308 Obj_Typ : Entity_Id;
3309 -- The type of the _object formal parameter
3311 Priv_Typ : Entity_Id;
3312 -- The partial view of working type
3314 Work_Typ : Entity_Id;
3315 -- The working type
3317 begin
3318 Work_Typ := Typ;
3320 -- The input type denotes the implementation base type of a constrained
3321 -- array type. Work with the first subtype as all invariant pragmas are
3322 -- on its rep item chain.
3324 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3325 Work_Typ := First_Subtype (Work_Typ);
3327 -- The input denotes the corresponding record type of a protected or a
3328 -- task type. Work with the concurrent type because the corresponding
3329 -- record type may not be visible to clients of the type.
3331 elsif Ekind (Work_Typ) = E_Record_Type
3332 and then Is_Concurrent_Record_Type (Work_Typ)
3333 then
3334 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3335 end if;
3337 -- The working type may be subject to pragma Ghost. Set the mode now to
3338 -- ensure that the invariant procedure is properly marked as Ghost.
3340 Set_Ghost_Mode (Work_Typ);
3342 -- The type must either have invariants of its own, inherit class-wide
3343 -- invariants from parent or interface types, or be an array or record
3344 -- type whose components have invariants.
3346 pragma Assert (Has_Invariants (Work_Typ));
3348 -- Nothing to do if the type already has a "partial" invariant procedure
3350 if Partial_Invariant then
3351 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3352 goto Leave;
3353 end if;
3355 -- Nothing to do if the type already has a "full" invariant procedure
3357 elsif Present (Invariant_Procedure (Work_Typ)) then
3358 goto Leave;
3359 end if;
3361 -- The caller requests the declaration of the "partial" invariant
3362 -- procedure.
3364 if Partial_Invariant then
3365 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3367 -- Otherwise the caller requests the declaration of the "full" invariant
3368 -- procedure.
3370 else
3371 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3372 end if;
3374 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3376 -- Perform minor decoration in case the declaration is not analyzed
3378 Set_Ekind (Proc_Id, E_Procedure);
3379 Set_Etype (Proc_Id, Standard_Void_Type);
3380 Set_Scope (Proc_Id, Current_Scope);
3382 if Partial_Invariant then
3383 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3384 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3385 else
3386 Set_Is_Invariant_Procedure (Proc_Id);
3387 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3388 end if;
3390 -- The invariant procedure requires debug info when the invariants are
3391 -- subject to Source Coverage Obligations.
3393 if Generate_SCO then
3394 Set_Needs_Debug_Info (Proc_Id);
3395 end if;
3397 -- Obtain all views of the input type
3399 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3401 -- Associate the invariant procedure with all views
3403 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3404 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3405 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3406 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3408 -- The declaration of the invariant procedure is inserted after the
3409 -- declaration of the partial view as this allows for proper external
3410 -- visibility.
3412 if Present (Priv_Typ) then
3413 Typ_Decl := Declaration_Node (Priv_Typ);
3415 -- Anonymous arrays in object declarations have no explicit declaration
3416 -- so use the related object declaration as the insertion point.
3418 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3419 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3421 -- Derived types with the full view as parent do not have a partial
3422 -- view. Insert the invariant procedure after the derived type.
3424 else
3425 Typ_Decl := Declaration_Node (Full_Typ);
3426 end if;
3428 -- The type should have a declarative node
3430 pragma Assert (Present (Typ_Decl));
3432 -- Create the formal parameter which emulates the variable-like behavior
3433 -- of the current type instance.
3435 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3437 -- When generating an invariant procedure declaration for an abstract
3438 -- type (including interfaces), use the class-wide type as the _object
3439 -- type. This has several desirable effects:
3441 -- * The invariant procedure does not become a primitive of the type.
3442 -- This eliminates the need to either special case the treatment of
3443 -- invariant procedures, or to make it a predefined primitive and
3444 -- force every derived type to potentially provide an empty body.
3446 -- * The invariant procedure does not need to be declared as abstract.
3447 -- This allows for a proper body, which in turn avoids redundant
3448 -- processing of the same invariants for types with multiple views.
3450 -- * The class-wide type allows for calls to abstract primitives
3451 -- within a nonabstract subprogram. The calls are treated as
3452 -- dispatching and require additional processing when they are
3453 -- remapped to call primitives of derived types. See routine
3454 -- Replace_References for details.
3456 if Is_Abstract_Type (Work_Typ) then
3457 Obj_Typ := Class_Wide_Type (Work_Typ);
3458 else
3459 Obj_Typ := Work_Typ;
3460 end if;
3462 -- Perform minor decoration in case the declaration is not analyzed
3464 Set_Ekind (Obj_Id, E_In_Parameter);
3465 Set_Etype (Obj_Id, Obj_Typ);
3466 Set_Scope (Obj_Id, Proc_Id);
3468 Set_First_Entity (Proc_Id, Obj_Id);
3470 -- Generate:
3471 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3473 Proc_Decl :=
3474 Make_Subprogram_Declaration (Loc,
3475 Specification =>
3476 Make_Procedure_Specification (Loc,
3477 Defining_Unit_Name => Proc_Id,
3478 Parameter_Specifications => New_List (
3479 Make_Parameter_Specification (Loc,
3480 Defining_Identifier => Obj_Id,
3481 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3483 -- The declaration should not be inserted into the tree when the context
3484 -- is ASIS or a generic unit because it is not part of the template.
3486 if ASIS_Mode or Inside_A_Generic then
3487 null;
3489 -- Semi-insert the declaration into the tree for GNATprove by setting
3490 -- its Parent field. This allows for proper upstream tree traversals.
3492 elsif GNATprove_Mode then
3493 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3495 -- Otherwise insert the declaration
3497 else
3498 pragma Assert (Present (Typ_Decl));
3499 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3500 end if;
3502 <<Leave>>
3503 Restore_Ghost_Mode (Saved_GM);
3504 end Build_Invariant_Procedure_Declaration;
3506 --------------------------
3507 -- Build_Procedure_Form --
3508 --------------------------
3510 procedure Build_Procedure_Form (N : Node_Id) is
3511 Loc : constant Source_Ptr := Sloc (N);
3512 Subp : constant Entity_Id := Defining_Entity (N);
3514 Func_Formal : Entity_Id;
3515 Proc_Formals : List_Id;
3516 Proc_Decl : Node_Id;
3518 begin
3519 -- No action needed if this transformation was already done, or in case
3520 -- of subprogram renaming declarations.
3522 if Nkind (Specification (N)) = N_Procedure_Specification
3523 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3524 then
3525 return;
3526 end if;
3528 -- Ditto when dealing with an expression function, where both the
3529 -- original expression and the generated declaration end up being
3530 -- expanded here.
3532 if Rewritten_For_C (Subp) then
3533 return;
3534 end if;
3536 Proc_Formals := New_List;
3538 -- Create a list of formal parameters with the same types as the
3539 -- function.
3541 Func_Formal := First_Formal (Subp);
3542 while Present (Func_Formal) loop
3543 Append_To (Proc_Formals,
3544 Make_Parameter_Specification (Loc,
3545 Defining_Identifier =>
3546 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3547 Parameter_Type =>
3548 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3550 Next_Formal (Func_Formal);
3551 end loop;
3553 -- Add an extra out parameter to carry the function result
3555 Name_Len := 6;
3556 Name_Buffer (1 .. Name_Len) := "RESULT";
3557 Append_To (Proc_Formals,
3558 Make_Parameter_Specification (Loc,
3559 Defining_Identifier =>
3560 Make_Defining_Identifier (Loc, Chars => Name_Find),
3561 Out_Present => True,
3562 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3564 -- The new procedure declaration is inserted immediately after the
3565 -- function declaration. The processing in Build_Procedure_Body_Form
3566 -- relies on this order.
3568 Proc_Decl :=
3569 Make_Subprogram_Declaration (Loc,
3570 Specification =>
3571 Make_Procedure_Specification (Loc,
3572 Defining_Unit_Name =>
3573 Make_Defining_Identifier (Loc, Chars (Subp)),
3574 Parameter_Specifications => Proc_Formals));
3576 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3578 -- Entity of procedure must remain invisible so that it does not
3579 -- overload subsequent references to the original function.
3581 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3583 -- Mark the function as having a procedure form and link the function
3584 -- and its internally built procedure.
3586 Set_Rewritten_For_C (Subp);
3587 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3588 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3589 end Build_Procedure_Form;
3591 ------------------------
3592 -- Build_Runtime_Call --
3593 ------------------------
3595 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3596 begin
3597 -- If entity is not available, we can skip making the call (this avoids
3598 -- junk duplicated error messages in a number of cases).
3600 if not RTE_Available (RE) then
3601 return Make_Null_Statement (Loc);
3602 else
3603 return
3604 Make_Procedure_Call_Statement (Loc,
3605 Name => New_Occurrence_Of (RTE (RE), Loc));
3606 end if;
3607 end Build_Runtime_Call;
3609 ------------------------
3610 -- Build_SS_Mark_Call --
3611 ------------------------
3613 function Build_SS_Mark_Call
3614 (Loc : Source_Ptr;
3615 Mark : Entity_Id) return Node_Id
3617 begin
3618 -- Generate:
3619 -- Mark : constant Mark_Id := SS_Mark;
3621 return
3622 Make_Object_Declaration (Loc,
3623 Defining_Identifier => Mark,
3624 Constant_Present => True,
3625 Object_Definition =>
3626 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3627 Expression =>
3628 Make_Function_Call (Loc,
3629 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3630 end Build_SS_Mark_Call;
3632 ---------------------------
3633 -- Build_SS_Release_Call --
3634 ---------------------------
3636 function Build_SS_Release_Call
3637 (Loc : Source_Ptr;
3638 Mark : Entity_Id) return Node_Id
3640 begin
3641 -- Generate:
3642 -- SS_Release (Mark);
3644 return
3645 Make_Procedure_Call_Statement (Loc,
3646 Name =>
3647 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3648 Parameter_Associations => New_List (
3649 New_Occurrence_Of (Mark, Loc)));
3650 end Build_SS_Release_Call;
3652 ----------------------------
3653 -- Build_Task_Array_Image --
3654 ----------------------------
3656 -- This function generates the body for a function that constructs the
3657 -- image string for a task that is an array component. The function is
3658 -- local to the init proc for the array type, and is called for each one
3659 -- of the components. The constructed image has the form of an indexed
3660 -- component, whose prefix is the outer variable of the array type.
3661 -- The n-dimensional array type has known indexes Index, Index2...
3663 -- Id_Ref is an indexed component form created by the enclosing init proc.
3664 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3665 -- in the loops that call the individual task init proc on each component.
3667 -- The generated function has the following structure:
3669 -- function F return String is
3670 -- Pref : string renames Task_Name;
3671 -- T1 : String := Index1'Image (Val1);
3672 -- ...
3673 -- Tn : String := indexn'image (Valn);
3674 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3675 -- -- Len includes commas and the end parentheses.
3676 -- Res : String (1..Len);
3677 -- Pos : Integer := Pref'Length;
3679 -- begin
3680 -- Res (1 .. Pos) := Pref;
3681 -- Pos := Pos + 1;
3682 -- Res (Pos) := '(';
3683 -- Pos := Pos + 1;
3684 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3685 -- Pos := Pos + T1'Length;
3686 -- Res (Pos) := '.';
3687 -- Pos := Pos + 1;
3688 -- ...
3689 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3690 -- Res (Len) := ')';
3692 -- return Res;
3693 -- end F;
3695 -- Needless to say, multidimensional arrays of tasks are rare enough that
3696 -- the bulkiness of this code is not really a concern.
3698 function Build_Task_Array_Image
3699 (Loc : Source_Ptr;
3700 Id_Ref : Node_Id;
3701 A_Type : Entity_Id;
3702 Dyn : Boolean := False) return Node_Id
3704 Dims : constant Nat := Number_Dimensions (A_Type);
3705 -- Number of dimensions for array of tasks
3707 Temps : array (1 .. Dims) of Entity_Id;
3708 -- Array of temporaries to hold string for each index
3710 Indx : Node_Id;
3711 -- Index expression
3713 Len : Entity_Id;
3714 -- Total length of generated name
3716 Pos : Entity_Id;
3717 -- Running index for substring assignments
3719 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3720 -- Name of enclosing variable, prefix of resulting name
3722 Res : Entity_Id;
3723 -- String to hold result
3725 Val : Node_Id;
3726 -- Value of successive indexes
3728 Sum : Node_Id;
3729 -- Expression to compute total size of string
3731 T : Entity_Id;
3732 -- Entity for name at one index position
3734 Decls : constant List_Id := New_List;
3735 Stats : constant List_Id := New_List;
3737 begin
3738 -- For a dynamic task, the name comes from the target variable. For a
3739 -- static one it is a formal of the enclosing init proc.
3741 if Dyn then
3742 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3743 Append_To (Decls,
3744 Make_Object_Declaration (Loc,
3745 Defining_Identifier => Pref,
3746 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3747 Expression =>
3748 Make_String_Literal (Loc,
3749 Strval => String_From_Name_Buffer)));
3751 else
3752 Append_To (Decls,
3753 Make_Object_Renaming_Declaration (Loc,
3754 Defining_Identifier => Pref,
3755 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3756 Name => Make_Identifier (Loc, Name_uTask_Name)));
3757 end if;
3759 Indx := First_Index (A_Type);
3760 Val := First (Expressions (Id_Ref));
3762 for J in 1 .. Dims loop
3763 T := Make_Temporary (Loc, 'T');
3764 Temps (J) := T;
3766 Append_To (Decls,
3767 Make_Object_Declaration (Loc,
3768 Defining_Identifier => T,
3769 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3770 Expression =>
3771 Make_Attribute_Reference (Loc,
3772 Attribute_Name => Name_Image,
3773 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3774 Expressions => New_List (New_Copy_Tree (Val)))));
3776 Next_Index (Indx);
3777 Next (Val);
3778 end loop;
3780 Sum := Make_Integer_Literal (Loc, Dims + 1);
3782 Sum :=
3783 Make_Op_Add (Loc,
3784 Left_Opnd => Sum,
3785 Right_Opnd =>
3786 Make_Attribute_Reference (Loc,
3787 Attribute_Name => Name_Length,
3788 Prefix => New_Occurrence_Of (Pref, Loc),
3789 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3791 for J in 1 .. Dims loop
3792 Sum :=
3793 Make_Op_Add (Loc,
3794 Left_Opnd => Sum,
3795 Right_Opnd =>
3796 Make_Attribute_Reference (Loc,
3797 Attribute_Name => Name_Length,
3798 Prefix =>
3799 New_Occurrence_Of (Temps (J), Loc),
3800 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3801 end loop;
3803 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3805 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3807 Append_To (Stats,
3808 Make_Assignment_Statement (Loc,
3809 Name =>
3810 Make_Indexed_Component (Loc,
3811 Prefix => New_Occurrence_Of (Res, Loc),
3812 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3813 Expression =>
3814 Make_Character_Literal (Loc,
3815 Chars => Name_Find,
3816 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3818 Append_To (Stats,
3819 Make_Assignment_Statement (Loc,
3820 Name => New_Occurrence_Of (Pos, Loc),
3821 Expression =>
3822 Make_Op_Add (Loc,
3823 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3824 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3826 for J in 1 .. Dims loop
3828 Append_To (Stats,
3829 Make_Assignment_Statement (Loc,
3830 Name =>
3831 Make_Slice (Loc,
3832 Prefix => New_Occurrence_Of (Res, Loc),
3833 Discrete_Range =>
3834 Make_Range (Loc,
3835 Low_Bound => New_Occurrence_Of (Pos, Loc),
3836 High_Bound =>
3837 Make_Op_Subtract (Loc,
3838 Left_Opnd =>
3839 Make_Op_Add (Loc,
3840 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3841 Right_Opnd =>
3842 Make_Attribute_Reference (Loc,
3843 Attribute_Name => Name_Length,
3844 Prefix =>
3845 New_Occurrence_Of (Temps (J), Loc),
3846 Expressions =>
3847 New_List (Make_Integer_Literal (Loc, 1)))),
3848 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3850 Expression => New_Occurrence_Of (Temps (J), Loc)));
3852 if J < Dims then
3853 Append_To (Stats,
3854 Make_Assignment_Statement (Loc,
3855 Name => New_Occurrence_Of (Pos, Loc),
3856 Expression =>
3857 Make_Op_Add (Loc,
3858 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3859 Right_Opnd =>
3860 Make_Attribute_Reference (Loc,
3861 Attribute_Name => Name_Length,
3862 Prefix => New_Occurrence_Of (Temps (J), Loc),
3863 Expressions =>
3864 New_List (Make_Integer_Literal (Loc, 1))))));
3866 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3868 Append_To (Stats,
3869 Make_Assignment_Statement (Loc,
3870 Name => Make_Indexed_Component (Loc,
3871 Prefix => New_Occurrence_Of (Res, Loc),
3872 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3873 Expression =>
3874 Make_Character_Literal (Loc,
3875 Chars => Name_Find,
3876 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3878 Append_To (Stats,
3879 Make_Assignment_Statement (Loc,
3880 Name => New_Occurrence_Of (Pos, Loc),
3881 Expression =>
3882 Make_Op_Add (Loc,
3883 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3884 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3885 end if;
3886 end loop;
3888 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3890 Append_To (Stats,
3891 Make_Assignment_Statement (Loc,
3892 Name =>
3893 Make_Indexed_Component (Loc,
3894 Prefix => New_Occurrence_Of (Res, Loc),
3895 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3896 Expression =>
3897 Make_Character_Literal (Loc,
3898 Chars => Name_Find,
3899 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3900 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3901 end Build_Task_Array_Image;
3903 ----------------------------
3904 -- Build_Task_Image_Decls --
3905 ----------------------------
3907 function Build_Task_Image_Decls
3908 (Loc : Source_Ptr;
3909 Id_Ref : Node_Id;
3910 A_Type : Entity_Id;
3911 In_Init_Proc : Boolean := False) return List_Id
3913 Decls : constant List_Id := New_List;
3914 T_Id : Entity_Id := Empty;
3915 Decl : Node_Id;
3916 Expr : Node_Id := Empty;
3917 Fun : Node_Id := Empty;
3918 Is_Dyn : constant Boolean :=
3919 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3920 and then
3921 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3923 begin
3924 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3925 -- generate a dummy declaration only.
3927 if Restriction_Active (No_Implicit_Heap_Allocations)
3928 or else Global_Discard_Names
3929 then
3930 T_Id := Make_Temporary (Loc, 'J');
3931 Name_Len := 0;
3933 return
3934 New_List (
3935 Make_Object_Declaration (Loc,
3936 Defining_Identifier => T_Id,
3937 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3938 Expression =>
3939 Make_String_Literal (Loc,
3940 Strval => String_From_Name_Buffer)));
3942 else
3943 if Nkind (Id_Ref) = N_Identifier
3944 or else Nkind (Id_Ref) = N_Defining_Identifier
3945 then
3946 -- For a simple variable, the image of the task is built from
3947 -- the name of the variable. To avoid possible conflict with the
3948 -- anonymous type created for a single protected object, add a
3949 -- numeric suffix.
3951 T_Id :=
3952 Make_Defining_Identifier (Loc,
3953 New_External_Name (Chars (Id_Ref), 'T', 1));
3955 Get_Name_String (Chars (Id_Ref));
3957 Expr :=
3958 Make_String_Literal (Loc,
3959 Strval => String_From_Name_Buffer);
3961 elsif Nkind (Id_Ref) = N_Selected_Component then
3962 T_Id :=
3963 Make_Defining_Identifier (Loc,
3964 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3965 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3967 elsif Nkind (Id_Ref) = N_Indexed_Component then
3968 T_Id :=
3969 Make_Defining_Identifier (Loc,
3970 New_External_Name (Chars (A_Type), 'N'));
3972 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3973 end if;
3974 end if;
3976 if Present (Fun) then
3977 Append (Fun, Decls);
3978 Expr := Make_Function_Call (Loc,
3979 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
3981 if not In_Init_Proc then
3982 Set_Uses_Sec_Stack (Defining_Entity (Fun));
3983 end if;
3984 end if;
3986 Decl := Make_Object_Declaration (Loc,
3987 Defining_Identifier => T_Id,
3988 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3989 Constant_Present => True,
3990 Expression => Expr);
3992 Append (Decl, Decls);
3993 return Decls;
3994 end Build_Task_Image_Decls;
3996 -------------------------------
3997 -- Build_Task_Image_Function --
3998 -------------------------------
4000 function Build_Task_Image_Function
4001 (Loc : Source_Ptr;
4002 Decls : List_Id;
4003 Stats : List_Id;
4004 Res : Entity_Id) return Node_Id
4006 Spec : Node_Id;
4008 begin
4009 Append_To (Stats,
4010 Make_Simple_Return_Statement (Loc,
4011 Expression => New_Occurrence_Of (Res, Loc)));
4013 Spec := Make_Function_Specification (Loc,
4014 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4015 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4017 -- Calls to 'Image use the secondary stack, which must be cleaned up
4018 -- after the task name is built.
4020 return Make_Subprogram_Body (Loc,
4021 Specification => Spec,
4022 Declarations => Decls,
4023 Handled_Statement_Sequence =>
4024 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4025 end Build_Task_Image_Function;
4027 -----------------------------
4028 -- Build_Task_Image_Prefix --
4029 -----------------------------
4031 procedure Build_Task_Image_Prefix
4032 (Loc : Source_Ptr;
4033 Len : out Entity_Id;
4034 Res : out Entity_Id;
4035 Pos : out Entity_Id;
4036 Prefix : Entity_Id;
4037 Sum : Node_Id;
4038 Decls : List_Id;
4039 Stats : List_Id)
4041 begin
4042 Len := Make_Temporary (Loc, 'L', Sum);
4044 Append_To (Decls,
4045 Make_Object_Declaration (Loc,
4046 Defining_Identifier => Len,
4047 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4048 Expression => Sum));
4050 Res := Make_Temporary (Loc, 'R');
4052 Append_To (Decls,
4053 Make_Object_Declaration (Loc,
4054 Defining_Identifier => Res,
4055 Object_Definition =>
4056 Make_Subtype_Indication (Loc,
4057 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4058 Constraint =>
4059 Make_Index_Or_Discriminant_Constraint (Loc,
4060 Constraints =>
4061 New_List (
4062 Make_Range (Loc,
4063 Low_Bound => Make_Integer_Literal (Loc, 1),
4064 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4066 -- Indicate that the result is an internal temporary, so it does not
4067 -- receive a bogus initialization when declaration is expanded. This
4068 -- is both efficient, and prevents anomalies in the handling of
4069 -- dynamic objects on the secondary stack.
4071 Set_Is_Internal (Res);
4072 Pos := Make_Temporary (Loc, 'P');
4074 Append_To (Decls,
4075 Make_Object_Declaration (Loc,
4076 Defining_Identifier => Pos,
4077 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4079 -- Pos := Prefix'Length;
4081 Append_To (Stats,
4082 Make_Assignment_Statement (Loc,
4083 Name => New_Occurrence_Of (Pos, Loc),
4084 Expression =>
4085 Make_Attribute_Reference (Loc,
4086 Attribute_Name => Name_Length,
4087 Prefix => New_Occurrence_Of (Prefix, Loc),
4088 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4090 -- Res (1 .. Pos) := Prefix;
4092 Append_To (Stats,
4093 Make_Assignment_Statement (Loc,
4094 Name =>
4095 Make_Slice (Loc,
4096 Prefix => New_Occurrence_Of (Res, Loc),
4097 Discrete_Range =>
4098 Make_Range (Loc,
4099 Low_Bound => Make_Integer_Literal (Loc, 1),
4100 High_Bound => New_Occurrence_Of (Pos, Loc))),
4102 Expression => New_Occurrence_Of (Prefix, Loc)));
4104 Append_To (Stats,
4105 Make_Assignment_Statement (Loc,
4106 Name => New_Occurrence_Of (Pos, Loc),
4107 Expression =>
4108 Make_Op_Add (Loc,
4109 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4110 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4111 end Build_Task_Image_Prefix;
4113 -----------------------------
4114 -- Build_Task_Record_Image --
4115 -----------------------------
4117 function Build_Task_Record_Image
4118 (Loc : Source_Ptr;
4119 Id_Ref : Node_Id;
4120 Dyn : Boolean := False) return Node_Id
4122 Len : Entity_Id;
4123 -- Total length of generated name
4125 Pos : Entity_Id;
4126 -- Index into result
4128 Res : Entity_Id;
4129 -- String to hold result
4131 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4132 -- Name of enclosing variable, prefix of resulting name
4134 Sum : Node_Id;
4135 -- Expression to compute total size of string
4137 Sel : Entity_Id;
4138 -- Entity for selector name
4140 Decls : constant List_Id := New_List;
4141 Stats : constant List_Id := New_List;
4143 begin
4144 -- For a dynamic task, the name comes from the target variable. For a
4145 -- static one it is a formal of the enclosing init proc.
4147 if Dyn then
4148 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4149 Append_To (Decls,
4150 Make_Object_Declaration (Loc,
4151 Defining_Identifier => Pref,
4152 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4153 Expression =>
4154 Make_String_Literal (Loc,
4155 Strval => String_From_Name_Buffer)));
4157 else
4158 Append_To (Decls,
4159 Make_Object_Renaming_Declaration (Loc,
4160 Defining_Identifier => Pref,
4161 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4162 Name => Make_Identifier (Loc, Name_uTask_Name)));
4163 end if;
4165 Sel := Make_Temporary (Loc, 'S');
4167 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4169 Append_To (Decls,
4170 Make_Object_Declaration (Loc,
4171 Defining_Identifier => Sel,
4172 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4173 Expression =>
4174 Make_String_Literal (Loc,
4175 Strval => String_From_Name_Buffer)));
4177 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4179 Sum :=
4180 Make_Op_Add (Loc,
4181 Left_Opnd => Sum,
4182 Right_Opnd =>
4183 Make_Attribute_Reference (Loc,
4184 Attribute_Name => Name_Length,
4185 Prefix =>
4186 New_Occurrence_Of (Pref, Loc),
4187 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4189 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4191 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4193 -- Res (Pos) := '.';
4195 Append_To (Stats,
4196 Make_Assignment_Statement (Loc,
4197 Name => Make_Indexed_Component (Loc,
4198 Prefix => New_Occurrence_Of (Res, Loc),
4199 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4200 Expression =>
4201 Make_Character_Literal (Loc,
4202 Chars => Name_Find,
4203 Char_Literal_Value =>
4204 UI_From_Int (Character'Pos ('.')))));
4206 Append_To (Stats,
4207 Make_Assignment_Statement (Loc,
4208 Name => New_Occurrence_Of (Pos, Loc),
4209 Expression =>
4210 Make_Op_Add (Loc,
4211 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4212 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4214 -- Res (Pos .. Len) := Selector;
4216 Append_To (Stats,
4217 Make_Assignment_Statement (Loc,
4218 Name => Make_Slice (Loc,
4219 Prefix => New_Occurrence_Of (Res, Loc),
4220 Discrete_Range =>
4221 Make_Range (Loc,
4222 Low_Bound => New_Occurrence_Of (Pos, Loc),
4223 High_Bound => New_Occurrence_Of (Len, Loc))),
4224 Expression => New_Occurrence_Of (Sel, Loc)));
4226 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4227 end Build_Task_Record_Image;
4229 ---------------------------------------
4230 -- Build_Transient_Object_Statements --
4231 ---------------------------------------
4233 procedure Build_Transient_Object_Statements
4234 (Obj_Decl : Node_Id;
4235 Fin_Call : out Node_Id;
4236 Hook_Assign : out Node_Id;
4237 Hook_Clear : out Node_Id;
4238 Hook_Decl : out Node_Id;
4239 Ptr_Decl : out Node_Id;
4240 Finalize_Obj : Boolean := True)
4242 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4243 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4244 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4246 Desig_Typ : Entity_Id;
4247 Hook_Expr : Node_Id;
4248 Hook_Id : Entity_Id;
4249 Obj_Ref : Node_Id;
4250 Ptr_Typ : Entity_Id;
4252 begin
4253 -- Recover the type of the object
4255 Desig_Typ := Obj_Typ;
4257 if Is_Access_Type (Desig_Typ) then
4258 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4259 end if;
4261 -- Create an access type which provides a reference to the transient
4262 -- object. Generate:
4264 -- type Ptr_Typ is access all Desig_Typ;
4266 Ptr_Typ := Make_Temporary (Loc, 'A');
4267 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4268 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4270 Ptr_Decl :=
4271 Make_Full_Type_Declaration (Loc,
4272 Defining_Identifier => Ptr_Typ,
4273 Type_Definition =>
4274 Make_Access_To_Object_Definition (Loc,
4275 All_Present => True,
4276 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4278 -- Create a temporary check which acts as a hook to the transient
4279 -- object. Generate:
4281 -- Hook : Ptr_Typ := null;
4283 Hook_Id := Make_Temporary (Loc, 'T');
4284 Set_Ekind (Hook_Id, E_Variable);
4285 Set_Etype (Hook_Id, Ptr_Typ);
4287 Hook_Decl :=
4288 Make_Object_Declaration (Loc,
4289 Defining_Identifier => Hook_Id,
4290 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4291 Expression => Make_Null (Loc));
4293 -- Mark the temporary as a hook. This signals the machinery in
4294 -- Build_Finalizer to recognize this special case.
4296 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4298 -- Hook the transient object to the temporary. Generate:
4300 -- Hook := Ptr_Typ (Obj_Id);
4301 -- <or>
4302 -- Hool := Obj_Id'Unrestricted_Access;
4304 if Is_Access_Type (Obj_Typ) then
4305 Hook_Expr :=
4306 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4307 else
4308 Hook_Expr :=
4309 Make_Attribute_Reference (Loc,
4310 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4311 Attribute_Name => Name_Unrestricted_Access);
4312 end if;
4314 Hook_Assign :=
4315 Make_Assignment_Statement (Loc,
4316 Name => New_Occurrence_Of (Hook_Id, Loc),
4317 Expression => Hook_Expr);
4319 -- Crear the hook prior to finalizing the object. Generate:
4321 -- Hook := null;
4323 Hook_Clear :=
4324 Make_Assignment_Statement (Loc,
4325 Name => New_Occurrence_Of (Hook_Id, Loc),
4326 Expression => Make_Null (Loc));
4328 -- Finalize the object. Generate:
4330 -- [Deep_]Finalize (Obj_Ref[.all]);
4332 if Finalize_Obj then
4333 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4335 if Is_Access_Type (Obj_Typ) then
4336 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4337 Set_Etype (Obj_Ref, Desig_Typ);
4338 end if;
4340 Fin_Call :=
4341 Make_Final_Call
4342 (Obj_Ref => Obj_Ref,
4343 Typ => Desig_Typ);
4345 -- Otherwise finalize the hook. Generate:
4347 -- [Deep_]Finalize (Hook.all);
4349 else
4350 Fin_Call :=
4351 Make_Final_Call (
4352 Obj_Ref =>
4353 Make_Explicit_Dereference (Loc,
4354 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4355 Typ => Desig_Typ);
4356 end if;
4357 end Build_Transient_Object_Statements;
4359 -----------------------------
4360 -- Check_Float_Op_Overflow --
4361 -----------------------------
4363 procedure Check_Float_Op_Overflow (N : Node_Id) is
4364 begin
4365 -- Return if no check needed
4367 if not Is_Floating_Point_Type (Etype (N))
4368 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4370 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4371 -- and do not expand the code for float overflow checking.
4373 or else CodePeer_Mode
4374 then
4375 return;
4376 end if;
4378 -- Otherwise we replace the expression by
4380 -- do Tnn : constant ftype := expression;
4381 -- constraint_error when not Tnn'Valid;
4382 -- in Tnn;
4384 declare
4385 Loc : constant Source_Ptr := Sloc (N);
4386 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4387 Typ : constant Entity_Id := Etype (N);
4389 begin
4390 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4391 -- right here. We also set the node as analyzed to prevent infinite
4392 -- recursion from repeating the operation in the expansion.
4394 Set_Do_Overflow_Check (N, False);
4395 Set_Analyzed (N, True);
4397 -- Do the rewrite to include the check
4399 Rewrite (N,
4400 Make_Expression_With_Actions (Loc,
4401 Actions => New_List (
4402 Make_Object_Declaration (Loc,
4403 Defining_Identifier => Tnn,
4404 Object_Definition => New_Occurrence_Of (Typ, Loc),
4405 Constant_Present => True,
4406 Expression => Relocate_Node (N)),
4407 Make_Raise_Constraint_Error (Loc,
4408 Condition =>
4409 Make_Op_Not (Loc,
4410 Right_Opnd =>
4411 Make_Attribute_Reference (Loc,
4412 Prefix => New_Occurrence_Of (Tnn, Loc),
4413 Attribute_Name => Name_Valid)),
4414 Reason => CE_Overflow_Check_Failed)),
4415 Expression => New_Occurrence_Of (Tnn, Loc)));
4417 Analyze_And_Resolve (N, Typ);
4418 end;
4419 end Check_Float_Op_Overflow;
4421 ----------------------------------
4422 -- Component_May_Be_Bit_Aligned --
4423 ----------------------------------
4425 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4426 UT : Entity_Id;
4428 begin
4429 -- If no component clause, then everything is fine, since the back end
4430 -- never bit-misaligns by default, even if there is a pragma Packed for
4431 -- the record.
4433 if No (Comp) or else No (Component_Clause (Comp)) then
4434 return False;
4435 end if;
4437 UT := Underlying_Type (Etype (Comp));
4439 -- It is only array and record types that cause trouble
4441 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4442 return False;
4444 -- If we know that we have a small (64 bits or less) record or small
4445 -- bit-packed array, then everything is fine, since the back end can
4446 -- handle these cases correctly.
4448 elsif Esize (Comp) <= 64
4449 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4450 then
4451 return False;
4453 -- Otherwise if the component is not byte aligned, we know we have the
4454 -- nasty unaligned case.
4456 elsif Normalized_First_Bit (Comp) /= Uint_0
4457 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4458 then
4459 return True;
4461 -- If we are large and byte aligned, then OK at this level
4463 else
4464 return False;
4465 end if;
4466 end Component_May_Be_Bit_Aligned;
4468 ----------------------------------------
4469 -- Containing_Package_With_Ext_Axioms --
4470 ----------------------------------------
4472 function Containing_Package_With_Ext_Axioms
4473 (E : Entity_Id) return Entity_Id
4475 begin
4476 -- E is the package or generic package which is externally axiomatized
4478 if Ekind_In (E, E_Generic_Package, E_Package)
4479 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4480 then
4481 return E;
4482 end if;
4484 -- If E's scope is axiomatized, E is axiomatized
4486 if Present (Scope (E)) then
4487 declare
4488 First_Ax_Parent_Scope : constant Entity_Id :=
4489 Containing_Package_With_Ext_Axioms (Scope (E));
4490 begin
4491 if Present (First_Ax_Parent_Scope) then
4492 return First_Ax_Parent_Scope;
4493 end if;
4494 end;
4495 end if;
4497 -- Otherwise, if E is a package instance, it is axiomatized if the
4498 -- corresponding generic package is axiomatized.
4500 if Ekind (E) = E_Package then
4501 declare
4502 Par : constant Node_Id := Parent (E);
4503 Decl : Node_Id;
4505 begin
4506 if Nkind (Par) = N_Defining_Program_Unit_Name then
4507 Decl := Parent (Par);
4508 else
4509 Decl := Par;
4510 end if;
4512 if Present (Generic_Parent (Decl)) then
4513 return
4514 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4515 end if;
4516 end;
4517 end if;
4519 return Empty;
4520 end Containing_Package_With_Ext_Axioms;
4522 -------------------------------
4523 -- Convert_To_Actual_Subtype --
4524 -------------------------------
4526 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4527 Act_ST : Entity_Id;
4529 begin
4530 Act_ST := Get_Actual_Subtype (Exp);
4532 if Act_ST = Etype (Exp) then
4533 return;
4534 else
4535 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4536 Analyze_And_Resolve (Exp, Act_ST);
4537 end if;
4538 end Convert_To_Actual_Subtype;
4540 -----------------------------------
4541 -- Corresponding_Runtime_Package --
4542 -----------------------------------
4544 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4545 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4546 -- Return True if protected type T has one entry and the maximum queue
4547 -- length is one.
4549 --------------------------------
4550 -- Has_One_Entry_And_No_Queue --
4551 --------------------------------
4553 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4554 Item : Entity_Id;
4555 Is_First : Boolean := True;
4557 begin
4558 Item := First_Entity (T);
4559 while Present (Item) loop
4560 if Is_Entry (Item) then
4562 -- The protected type has more than one entry
4564 if not Is_First then
4565 return False;
4566 end if;
4568 -- The queue length is not one
4570 if not Restriction_Active (No_Entry_Queue)
4571 and then Get_Max_Queue_Length (Item) /= Uint_1
4572 then
4573 return False;
4574 end if;
4576 Is_First := False;
4577 end if;
4579 Next_Entity (Item);
4580 end loop;
4582 return True;
4583 end Has_One_Entry_And_No_Queue;
4585 -- Local variables
4587 Pkg_Id : RTU_Id := RTU_Null;
4589 -- Start of processing for Corresponding_Runtime_Package
4591 begin
4592 pragma Assert (Is_Concurrent_Type (Typ));
4594 if Ekind (Typ) in Protected_Kind then
4595 if Has_Entries (Typ)
4597 -- A protected type without entries that covers an interface and
4598 -- overrides the abstract routines with protected procedures is
4599 -- considered equivalent to a protected type with entries in the
4600 -- context of dispatching select statements. It is sufficient to
4601 -- check for the presence of an interface list in the declaration
4602 -- node to recognize this case.
4604 or else Present (Interface_List (Parent (Typ)))
4606 -- Protected types with interrupt handlers (when not using a
4607 -- restricted profile) are also considered equivalent to
4608 -- protected types with entries. The types which are used
4609 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4610 -- are derived from Protection_Entries.
4612 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4613 or else Has_Interrupt_Handler (Typ)
4614 then
4615 if Abort_Allowed
4616 or else Restriction_Active (No_Select_Statements) = False
4617 or else not Has_One_Entry_And_No_Queue (Typ)
4618 or else (Has_Attach_Handler (Typ)
4619 and then not Restricted_Profile)
4620 then
4621 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4622 else
4623 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4624 end if;
4626 else
4627 Pkg_Id := System_Tasking_Protected_Objects;
4628 end if;
4629 end if;
4631 return Pkg_Id;
4632 end Corresponding_Runtime_Package;
4634 -----------------------------------
4635 -- Current_Sem_Unit_Declarations --
4636 -----------------------------------
4638 function Current_Sem_Unit_Declarations return List_Id is
4639 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4640 Decls : List_Id;
4642 begin
4643 -- If the current unit is a package body, locate the visible
4644 -- declarations of the package spec.
4646 if Nkind (U) = N_Package_Body then
4647 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4648 end if;
4650 if Nkind (U) = N_Package_Declaration then
4651 U := Specification (U);
4652 Decls := Visible_Declarations (U);
4654 if No (Decls) then
4655 Decls := New_List;
4656 Set_Visible_Declarations (U, Decls);
4657 end if;
4659 else
4660 Decls := Declarations (U);
4662 if No (Decls) then
4663 Decls := New_List;
4664 Set_Declarations (U, Decls);
4665 end if;
4666 end if;
4668 return Decls;
4669 end Current_Sem_Unit_Declarations;
4671 -----------------------
4672 -- Duplicate_Subexpr --
4673 -----------------------
4675 function Duplicate_Subexpr
4676 (Exp : Node_Id;
4677 Name_Req : Boolean := False;
4678 Renaming_Req : Boolean := False) return Node_Id
4680 begin
4681 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4682 return New_Copy_Tree (Exp);
4683 end Duplicate_Subexpr;
4685 ---------------------------------
4686 -- Duplicate_Subexpr_No_Checks --
4687 ---------------------------------
4689 function Duplicate_Subexpr_No_Checks
4690 (Exp : Node_Id;
4691 Name_Req : Boolean := False;
4692 Renaming_Req : Boolean := False;
4693 Related_Id : Entity_Id := Empty;
4694 Is_Low_Bound : Boolean := False;
4695 Is_High_Bound : Boolean := False) return Node_Id
4697 New_Exp : Node_Id;
4699 begin
4700 Remove_Side_Effects
4701 (Exp => Exp,
4702 Name_Req => Name_Req,
4703 Renaming_Req => Renaming_Req,
4704 Related_Id => Related_Id,
4705 Is_Low_Bound => Is_Low_Bound,
4706 Is_High_Bound => Is_High_Bound);
4708 New_Exp := New_Copy_Tree (Exp);
4709 Remove_Checks (New_Exp);
4710 return New_Exp;
4711 end Duplicate_Subexpr_No_Checks;
4713 -----------------------------------
4714 -- Duplicate_Subexpr_Move_Checks --
4715 -----------------------------------
4717 function Duplicate_Subexpr_Move_Checks
4718 (Exp : Node_Id;
4719 Name_Req : Boolean := False;
4720 Renaming_Req : Boolean := False) return Node_Id
4722 New_Exp : Node_Id;
4724 begin
4725 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4726 New_Exp := New_Copy_Tree (Exp);
4727 Remove_Checks (Exp);
4728 return New_Exp;
4729 end Duplicate_Subexpr_Move_Checks;
4731 --------------------
4732 -- Ensure_Defined --
4733 --------------------
4735 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4736 IR : Node_Id;
4738 begin
4739 -- An itype reference must only be created if this is a local itype, so
4740 -- that gigi can elaborate it on the proper objstack.
4742 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4743 IR := Make_Itype_Reference (Sloc (N));
4744 Set_Itype (IR, Typ);
4745 Insert_Action (N, IR);
4746 end if;
4747 end Ensure_Defined;
4749 --------------------
4750 -- Entry_Names_OK --
4751 --------------------
4753 function Entry_Names_OK return Boolean is
4754 begin
4755 return
4756 not Restricted_Profile
4757 and then not Global_Discard_Names
4758 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4759 and then not Restriction_Active (No_Local_Allocators);
4760 end Entry_Names_OK;
4762 -------------------
4763 -- Evaluate_Name --
4764 -------------------
4766 procedure Evaluate_Name (Nam : Node_Id) is
4767 begin
4768 -- For an attribute reference or an indexed component, evaluate the
4769 -- prefix, which is itself a name, recursively, and then force the
4770 -- evaluation of all the subscripts (or attribute expressions).
4772 case Nkind (Nam) is
4773 when N_Attribute_Reference
4774 | N_Indexed_Component
4776 Evaluate_Name (Prefix (Nam));
4778 declare
4779 E : Node_Id;
4781 begin
4782 E := First (Expressions (Nam));
4783 while Present (E) loop
4784 Force_Evaluation (E);
4786 if Original_Node (E) /= E then
4787 Set_Do_Range_Check
4788 (E, Do_Range_Check (Original_Node (E)));
4789 end if;
4791 Next (E);
4792 end loop;
4793 end;
4795 -- For an explicit dereference, we simply force the evaluation of
4796 -- the name expression. The dereference provides a value that is the
4797 -- address for the renamed object, and it is precisely this value
4798 -- that we want to preserve.
4800 when N_Explicit_Dereference =>
4801 Force_Evaluation (Prefix (Nam));
4803 -- For a function call, we evaluate the call
4805 when N_Function_Call =>
4806 Force_Evaluation (Nam);
4808 -- For a qualified expression, we evaluate the underlying object
4809 -- name if any, otherwise we force the evaluation of the underlying
4810 -- expression.
4812 when N_Qualified_Expression =>
4813 if Is_Object_Reference (Expression (Nam)) then
4814 Evaluate_Name (Expression (Nam));
4815 else
4816 Force_Evaluation (Expression (Nam));
4817 end if;
4819 -- For a selected component, we simply evaluate the prefix
4821 when N_Selected_Component =>
4822 Evaluate_Name (Prefix (Nam));
4824 -- For a slice, we evaluate the prefix, as for the indexed component
4825 -- case and then, if there is a range present, either directly or as
4826 -- the constraint of a discrete subtype indication, we evaluate the
4827 -- two bounds of this range.
4829 when N_Slice =>
4830 Evaluate_Name (Prefix (Nam));
4831 Evaluate_Slice_Bounds (Nam);
4833 -- For a type conversion, the expression of the conversion must be
4834 -- the name of an object, and we simply need to evaluate this name.
4836 when N_Type_Conversion =>
4837 Evaluate_Name (Expression (Nam));
4839 -- The remaining cases are direct name, operator symbol and character
4840 -- literal. In all these cases, we do nothing, since we want to
4841 -- reevaluate each time the renamed object is used.
4843 when others =>
4844 null;
4845 end case;
4846 end Evaluate_Name;
4848 ---------------------------
4849 -- Evaluate_Slice_Bounds --
4850 ---------------------------
4852 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4853 DR : constant Node_Id := Discrete_Range (Slice);
4854 Constr : Node_Id;
4855 Rexpr : Node_Id;
4857 begin
4858 if Nkind (DR) = N_Range then
4859 Force_Evaluation (Low_Bound (DR));
4860 Force_Evaluation (High_Bound (DR));
4862 elsif Nkind (DR) = N_Subtype_Indication then
4863 Constr := Constraint (DR);
4865 if Nkind (Constr) = N_Range_Constraint then
4866 Rexpr := Range_Expression (Constr);
4868 Force_Evaluation (Low_Bound (Rexpr));
4869 Force_Evaluation (High_Bound (Rexpr));
4870 end if;
4871 end if;
4872 end Evaluate_Slice_Bounds;
4874 ---------------------
4875 -- Evolve_And_Then --
4876 ---------------------
4878 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4879 begin
4880 if No (Cond) then
4881 Cond := Cond1;
4882 else
4883 Cond :=
4884 Make_And_Then (Sloc (Cond1),
4885 Left_Opnd => Cond,
4886 Right_Opnd => Cond1);
4887 end if;
4888 end Evolve_And_Then;
4890 --------------------
4891 -- Evolve_Or_Else --
4892 --------------------
4894 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4895 begin
4896 if No (Cond) then
4897 Cond := Cond1;
4898 else
4899 Cond :=
4900 Make_Or_Else (Sloc (Cond1),
4901 Left_Opnd => Cond,
4902 Right_Opnd => Cond1);
4903 end if;
4904 end Evolve_Or_Else;
4906 -----------------------------------
4907 -- Exceptions_In_Finalization_OK --
4908 -----------------------------------
4910 function Exceptions_In_Finalization_OK return Boolean is
4911 begin
4912 return
4913 not (Restriction_Active (No_Exception_Handlers) or else
4914 Restriction_Active (No_Exception_Propagation) or else
4915 Restriction_Active (No_Exceptions));
4916 end Exceptions_In_Finalization_OK;
4918 -----------------------------------------
4919 -- Expand_Static_Predicates_In_Choices --
4920 -----------------------------------------
4922 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4923 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4925 Choices : constant List_Id := Discrete_Choices (N);
4927 Choice : Node_Id;
4928 Next_C : Node_Id;
4929 P : Node_Id;
4930 C : Node_Id;
4932 begin
4933 Choice := First (Choices);
4934 while Present (Choice) loop
4935 Next_C := Next (Choice);
4937 -- Check for name of subtype with static predicate
4939 if Is_Entity_Name (Choice)
4940 and then Is_Type (Entity (Choice))
4941 and then Has_Predicates (Entity (Choice))
4942 then
4943 -- Loop through entries in predicate list, converting to choices
4944 -- and inserting in the list before the current choice. Note that
4945 -- if the list is empty, corresponding to a False predicate, then
4946 -- no choices are inserted.
4948 P := First (Static_Discrete_Predicate (Entity (Choice)));
4949 while Present (P) loop
4951 -- If low bound and high bounds are equal, copy simple choice
4953 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4954 C := New_Copy (Low_Bound (P));
4956 -- Otherwise copy a range
4958 else
4959 C := New_Copy (P);
4960 end if;
4962 -- Change Sloc to referencing choice (rather than the Sloc of
4963 -- the predicate declaration element itself).
4965 Set_Sloc (C, Sloc (Choice));
4966 Insert_Before (Choice, C);
4967 Next (P);
4968 end loop;
4970 -- Delete the predicated entry
4972 Remove (Choice);
4973 end if;
4975 -- Move to next choice to check
4977 Choice := Next_C;
4978 end loop;
4979 end Expand_Static_Predicates_In_Choices;
4981 ------------------------------
4982 -- Expand_Subtype_From_Expr --
4983 ------------------------------
4985 -- This function is applicable for both static and dynamic allocation of
4986 -- objects which are constrained by an initial expression. Basically it
4987 -- transforms an unconstrained subtype indication into a constrained one.
4989 -- The expression may also be transformed in certain cases in order to
4990 -- avoid multiple evaluation. In the static allocation case, the general
4991 -- scheme is:
4993 -- Val : T := Expr;
4995 -- is transformed into
4997 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
4999 -- Here are the main cases :
5001 -- <if Expr is a Slice>
5002 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5004 -- <elsif Expr is a String Literal>
5005 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5007 -- <elsif Expr is Constrained>
5008 -- subtype T is Type_Of_Expr
5009 -- Val : T := Expr;
5011 -- <elsif Expr is an entity_name>
5012 -- Val : T (constraints taken from Expr) := Expr;
5014 -- <else>
5015 -- type Axxx is access all T;
5016 -- Rval : Axxx := Expr'ref;
5017 -- Val : T (constraints taken from Rval) := Rval.all;
5019 -- ??? note: when the Expression is allocated in the secondary stack
5020 -- we could use it directly instead of copying it by declaring
5021 -- Val : T (...) renames Rval.all
5023 procedure Expand_Subtype_From_Expr
5024 (N : Node_Id;
5025 Unc_Type : Entity_Id;
5026 Subtype_Indic : Node_Id;
5027 Exp : Node_Id;
5028 Related_Id : Entity_Id := Empty)
5030 Loc : constant Source_Ptr := Sloc (N);
5031 Exp_Typ : constant Entity_Id := Etype (Exp);
5032 T : Entity_Id;
5034 begin
5035 -- In general we cannot build the subtype if expansion is disabled,
5036 -- because internal entities may not have been defined. However, to
5037 -- avoid some cascaded errors, we try to continue when the expression is
5038 -- an array (or string), because it is safe to compute the bounds. It is
5039 -- in fact required to do so even in a generic context, because there
5040 -- may be constants that depend on the bounds of a string literal, both
5041 -- standard string types and more generally arrays of characters.
5043 -- In GNATprove mode, these extra subtypes are not needed
5045 if GNATprove_Mode then
5046 return;
5047 end if;
5049 if not Expander_Active
5050 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5051 then
5052 return;
5053 end if;
5055 if Nkind (Exp) = N_Slice then
5056 declare
5057 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5059 begin
5060 Rewrite (Subtype_Indic,
5061 Make_Subtype_Indication (Loc,
5062 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5063 Constraint =>
5064 Make_Index_Or_Discriminant_Constraint (Loc,
5065 Constraints => New_List
5066 (New_Occurrence_Of (Slice_Type, Loc)))));
5068 -- This subtype indication may be used later for constraint checks
5069 -- we better make sure that if a variable was used as a bound of
5070 -- of the original slice, its value is frozen.
5072 Evaluate_Slice_Bounds (Exp);
5073 end;
5075 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5076 Rewrite (Subtype_Indic,
5077 Make_Subtype_Indication (Loc,
5078 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5079 Constraint =>
5080 Make_Index_Or_Discriminant_Constraint (Loc,
5081 Constraints => New_List (
5082 Make_Literal_Range (Loc,
5083 Literal_Typ => Exp_Typ)))));
5085 -- If the type of the expression is an internally generated type it
5086 -- may not be necessary to create a new subtype. However there are two
5087 -- exceptions: references to the current instances, and aliased array
5088 -- object declarations for which the back end has to create a template.
5090 elsif Is_Constrained (Exp_Typ)
5091 and then not Is_Class_Wide_Type (Unc_Type)
5092 and then
5093 (Nkind (N) /= N_Object_Declaration
5094 or else not Is_Entity_Name (Expression (N))
5095 or else not Comes_From_Source (Entity (Expression (N)))
5096 or else not Is_Array_Type (Exp_Typ)
5097 or else not Aliased_Present (N))
5098 then
5099 if Is_Itype (Exp_Typ) then
5101 -- Within an initialization procedure, a selected component
5102 -- denotes a component of the enclosing record, and it appears as
5103 -- an actual in a call to its own initialization procedure. If
5104 -- this component depends on the outer discriminant, we must
5105 -- generate the proper actual subtype for it.
5107 if Nkind (Exp) = N_Selected_Component
5108 and then Within_Init_Proc
5109 then
5110 declare
5111 Decl : constant Node_Id :=
5112 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5113 begin
5114 if Present (Decl) then
5115 Insert_Action (N, Decl);
5116 T := Defining_Identifier (Decl);
5117 else
5118 T := Exp_Typ;
5119 end if;
5120 end;
5122 -- No need to generate a new subtype
5124 else
5125 T := Exp_Typ;
5126 end if;
5128 else
5129 T := Make_Temporary (Loc, 'T');
5131 Insert_Action (N,
5132 Make_Subtype_Declaration (Loc,
5133 Defining_Identifier => T,
5134 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5136 -- This type is marked as an itype even though it has an explicit
5137 -- declaration since otherwise Is_Generic_Actual_Type can get
5138 -- set, resulting in the generation of spurious errors. (See
5139 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5141 Set_Is_Itype (T);
5142 Set_Associated_Node_For_Itype (T, Exp);
5143 end if;
5145 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5147 -- Nothing needs to be done for private types with unknown discriminants
5148 -- if the underlying type is not an unconstrained composite type or it
5149 -- is an unchecked union.
5151 elsif Is_Private_Type (Unc_Type)
5152 and then Has_Unknown_Discriminants (Unc_Type)
5153 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5154 or else Is_Constrained (Underlying_Type (Unc_Type))
5155 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5156 then
5157 null;
5159 -- Case of derived type with unknown discriminants where the parent type
5160 -- also has unknown discriminants.
5162 elsif Is_Record_Type (Unc_Type)
5163 and then not Is_Class_Wide_Type (Unc_Type)
5164 and then Has_Unknown_Discriminants (Unc_Type)
5165 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5166 then
5167 -- Nothing to be done if no underlying record view available
5169 -- If this is a limited type derived from a type with unknown
5170 -- discriminants, do not expand either, so that subsequent expansion
5171 -- of the call can add build-in-place parameters to call.
5173 if No (Underlying_Record_View (Unc_Type))
5174 or else Is_Limited_Type (Unc_Type)
5175 then
5176 null;
5178 -- Otherwise use the Underlying_Record_View to create the proper
5179 -- constrained subtype for an object of a derived type with unknown
5180 -- discriminants.
5182 else
5183 Remove_Side_Effects (Exp);
5184 Rewrite (Subtype_Indic,
5185 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5186 end if;
5188 -- Renamings of class-wide interface types require no equivalent
5189 -- constrained type declarations because we only need to reference
5190 -- the tag component associated with the interface. The same is
5191 -- presumably true for class-wide types in general, so this test
5192 -- is broadened to include all class-wide renamings, which also
5193 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5194 -- (Is this really correct, or are there some cases of class-wide
5195 -- renamings that require action in this procedure???)
5197 elsif Present (N)
5198 and then Nkind (N) = N_Object_Renaming_Declaration
5199 and then Is_Class_Wide_Type (Unc_Type)
5200 then
5201 null;
5203 -- In Ada 95 nothing to be done if the type of the expression is limited
5204 -- because in this case the expression cannot be copied, and its use can
5205 -- only be by reference.
5207 -- In Ada 2005 the context can be an object declaration whose expression
5208 -- is a function that returns in place. If the nominal subtype has
5209 -- unknown discriminants, the call still provides constraints on the
5210 -- object, and we have to create an actual subtype from it.
5212 -- If the type is class-wide, the expression is dynamically tagged and
5213 -- we do not create an actual subtype either. Ditto for an interface.
5214 -- For now this applies only if the type is immutably limited, and the
5215 -- function being called is build-in-place. This will have to be revised
5216 -- when build-in-place functions are generalized to other types.
5218 elsif Is_Limited_View (Exp_Typ)
5219 and then
5220 (Is_Class_Wide_Type (Exp_Typ)
5221 or else Is_Interface (Exp_Typ)
5222 or else not Has_Unknown_Discriminants (Exp_Typ)
5223 or else not Is_Composite_Type (Unc_Type))
5224 then
5225 null;
5227 -- For limited objects initialized with build in place function calls,
5228 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5229 -- node in the expression initializing the object, which breaks the
5230 -- circuitry that detects and adds the additional arguments to the
5231 -- called function.
5233 elsif Is_Build_In_Place_Function_Call (Exp) then
5234 null;
5236 else
5237 Remove_Side_Effects (Exp);
5238 Rewrite (Subtype_Indic,
5239 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5240 end if;
5241 end Expand_Subtype_From_Expr;
5243 ---------------------------------------------
5244 -- Expression_Contains_Primitives_Calls_Of --
5245 ---------------------------------------------
5247 function Expression_Contains_Primitives_Calls_Of
5248 (Expr : Node_Id;
5249 Typ : Entity_Id) return Boolean
5251 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5253 Calls_OK : Boolean := False;
5254 -- This flag is set to True when expression Expr contains at least one
5255 -- call to a nondispatching primitive function of Typ.
5257 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5258 -- Search for nondispatching calls to primitive functions of type Typ
5260 ----------------------------
5261 -- Search_Primitive_Calls --
5262 ----------------------------
5264 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5265 Disp_Typ : Entity_Id;
5266 Subp : Entity_Id;
5268 begin
5269 -- Detect a function call that could denote a nondispatching
5270 -- primitive of the input type.
5272 if Nkind (N) = N_Function_Call
5273 and then Is_Entity_Name (Name (N))
5274 then
5275 Subp := Entity (Name (N));
5277 -- Do not consider function calls with a controlling argument, as
5278 -- those are always dispatching calls.
5280 if Is_Dispatching_Operation (Subp)
5281 and then No (Controlling_Argument (N))
5282 then
5283 Disp_Typ := Find_Dispatching_Type (Subp);
5285 -- To qualify as a suitable primitive, the dispatching type of
5286 -- the function must be the input type.
5288 if Present (Disp_Typ)
5289 and then Unique_Entity (Disp_Typ) = U_Typ
5290 then
5291 Calls_OK := True;
5293 -- There is no need to continue the traversal, as one such
5294 -- call suffices.
5296 return Abandon;
5297 end if;
5298 end if;
5299 end if;
5301 return OK;
5302 end Search_Primitive_Calls;
5304 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5306 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5308 begin
5309 Search_Calls (Expr);
5310 return Calls_OK;
5311 end Expression_Contains_Primitives_Calls_Of;
5313 ----------------------
5314 -- Finalize_Address --
5315 ----------------------
5317 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5318 Utyp : Entity_Id := Typ;
5320 begin
5321 -- Handle protected class-wide or task class-wide types
5323 if Is_Class_Wide_Type (Utyp) then
5324 if Is_Concurrent_Type (Root_Type (Utyp)) then
5325 Utyp := Root_Type (Utyp);
5327 elsif Is_Private_Type (Root_Type (Utyp))
5328 and then Present (Full_View (Root_Type (Utyp)))
5329 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5330 then
5331 Utyp := Full_View (Root_Type (Utyp));
5332 end if;
5333 end if;
5335 -- Handle private types
5337 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5338 Utyp := Full_View (Utyp);
5339 end if;
5341 -- Handle protected and task types
5343 if Is_Concurrent_Type (Utyp)
5344 and then Present (Corresponding_Record_Type (Utyp))
5345 then
5346 Utyp := Corresponding_Record_Type (Utyp);
5347 end if;
5349 Utyp := Underlying_Type (Base_Type (Utyp));
5351 -- Deal with untagged derivation of private views. If the parent is
5352 -- now known to be protected, the finalization routine is the one
5353 -- defined on the corresponding record of the ancestor (corresponding
5354 -- records do not automatically inherit operations, but maybe they
5355 -- should???)
5357 if Is_Untagged_Derivation (Typ) then
5358 if Is_Protected_Type (Typ) then
5359 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5361 else
5362 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5364 if Is_Protected_Type (Utyp) then
5365 Utyp := Corresponding_Record_Type (Utyp);
5366 end if;
5367 end if;
5368 end if;
5370 -- If the underlying_type is a subtype, we are dealing with the
5371 -- completion of a private type. We need to access the base type and
5372 -- generate a conversion to it.
5374 if Utyp /= Base_Type (Utyp) then
5375 pragma Assert (Is_Private_Type (Typ));
5377 Utyp := Base_Type (Utyp);
5378 end if;
5380 -- When dealing with an internally built full view for a type with
5381 -- unknown discriminants, use the original record type.
5383 if Is_Underlying_Record_View (Utyp) then
5384 Utyp := Etype (Utyp);
5385 end if;
5387 return TSS (Utyp, TSS_Finalize_Address);
5388 end Finalize_Address;
5390 ------------------------
5391 -- Find_Interface_ADT --
5392 ------------------------
5394 function Find_Interface_ADT
5395 (T : Entity_Id;
5396 Iface : Entity_Id) return Elmt_Id
5398 ADT : Elmt_Id;
5399 Typ : Entity_Id := T;
5401 begin
5402 pragma Assert (Is_Interface (Iface));
5404 -- Handle private types
5406 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5407 Typ := Full_View (Typ);
5408 end if;
5410 -- Handle access types
5412 if Is_Access_Type (Typ) then
5413 Typ := Designated_Type (Typ);
5414 end if;
5416 -- Handle task and protected types implementing interfaces
5418 if Is_Concurrent_Type (Typ) then
5419 Typ := Corresponding_Record_Type (Typ);
5420 end if;
5422 pragma Assert
5423 (not Is_Class_Wide_Type (Typ)
5424 and then Ekind (Typ) /= E_Incomplete_Type);
5426 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5427 return First_Elmt (Access_Disp_Table (Typ));
5429 else
5430 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5431 while Present (ADT)
5432 and then Present (Related_Type (Node (ADT)))
5433 and then Related_Type (Node (ADT)) /= Iface
5434 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5435 Use_Full_View => True)
5436 loop
5437 Next_Elmt (ADT);
5438 end loop;
5440 pragma Assert (Present (Related_Type (Node (ADT))));
5441 return ADT;
5442 end if;
5443 end Find_Interface_ADT;
5445 ------------------------
5446 -- Find_Interface_Tag --
5447 ------------------------
5449 function Find_Interface_Tag
5450 (T : Entity_Id;
5451 Iface : Entity_Id) return Entity_Id
5453 AI_Tag : Entity_Id := Empty;
5454 Found : Boolean := False;
5455 Typ : Entity_Id := T;
5457 procedure Find_Tag (Typ : Entity_Id);
5458 -- Internal subprogram used to recursively climb to the ancestors
5460 --------------
5461 -- Find_Tag --
5462 --------------
5464 procedure Find_Tag (Typ : Entity_Id) is
5465 AI_Elmt : Elmt_Id;
5466 AI : Node_Id;
5468 begin
5469 -- This routine does not handle the case in which the interface is an
5470 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5472 pragma Assert (Typ /= Iface);
5474 -- Climb to the root type handling private types
5476 if Present (Full_View (Etype (Typ))) then
5477 if Full_View (Etype (Typ)) /= Typ then
5478 Find_Tag (Full_View (Etype (Typ)));
5479 end if;
5481 elsif Etype (Typ) /= Typ then
5482 Find_Tag (Etype (Typ));
5483 end if;
5485 -- Traverse the list of interfaces implemented by the type
5487 if not Found
5488 and then Present (Interfaces (Typ))
5489 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5490 then
5491 -- Skip the tag associated with the primary table
5493 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5494 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5495 pragma Assert (Present (AI_Tag));
5497 AI_Elmt := First_Elmt (Interfaces (Typ));
5498 while Present (AI_Elmt) loop
5499 AI := Node (AI_Elmt);
5501 if AI = Iface
5502 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5503 then
5504 Found := True;
5505 return;
5506 end if;
5508 AI_Tag := Next_Tag_Component (AI_Tag);
5509 Next_Elmt (AI_Elmt);
5510 end loop;
5511 end if;
5512 end Find_Tag;
5514 -- Start of processing for Find_Interface_Tag
5516 begin
5517 pragma Assert (Is_Interface (Iface));
5519 -- Handle access types
5521 if Is_Access_Type (Typ) then
5522 Typ := Designated_Type (Typ);
5523 end if;
5525 -- Handle class-wide types
5527 if Is_Class_Wide_Type (Typ) then
5528 Typ := Root_Type (Typ);
5529 end if;
5531 -- Handle private types
5533 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5534 Typ := Full_View (Typ);
5535 end if;
5537 -- Handle entities from the limited view
5539 if Ekind (Typ) = E_Incomplete_Type then
5540 pragma Assert (Present (Non_Limited_View (Typ)));
5541 Typ := Non_Limited_View (Typ);
5542 end if;
5544 -- Handle task and protected types implementing interfaces
5546 if Is_Concurrent_Type (Typ) then
5547 Typ := Corresponding_Record_Type (Typ);
5548 end if;
5550 -- If the interface is an ancestor of the type, then it shared the
5551 -- primary dispatch table.
5553 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5554 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5555 return First_Tag_Component (Typ);
5557 -- Otherwise we need to search for its associated tag component
5559 else
5560 Find_Tag (Typ);
5561 pragma Assert (Found);
5562 return AI_Tag;
5563 end if;
5564 end Find_Interface_Tag;
5566 ---------------------------
5567 -- Find_Optional_Prim_Op --
5568 ---------------------------
5570 function Find_Optional_Prim_Op
5571 (T : Entity_Id; Name : Name_Id) return Entity_Id
5573 Prim : Elmt_Id;
5574 Typ : Entity_Id := T;
5575 Op : Entity_Id;
5577 begin
5578 if Is_Class_Wide_Type (Typ) then
5579 Typ := Root_Type (Typ);
5580 end if;
5582 Typ := Underlying_Type (Typ);
5584 -- Loop through primitive operations
5586 Prim := First_Elmt (Primitive_Operations (Typ));
5587 while Present (Prim) loop
5588 Op := Node (Prim);
5590 -- We can retrieve primitive operations by name if it is an internal
5591 -- name. For equality we must check that both of its operands have
5592 -- the same type, to avoid confusion with user-defined equalities
5593 -- than may have a non-symmetric signature.
5595 exit when Chars (Op) = Name
5596 and then
5597 (Name /= Name_Op_Eq
5598 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5600 Next_Elmt (Prim);
5601 end loop;
5603 return Node (Prim); -- Empty if not found
5604 end Find_Optional_Prim_Op;
5606 ---------------------------
5607 -- Find_Optional_Prim_Op --
5608 ---------------------------
5610 function Find_Optional_Prim_Op
5611 (T : Entity_Id;
5612 Name : TSS_Name_Type) return Entity_Id
5614 Inher_Op : Entity_Id := Empty;
5615 Own_Op : Entity_Id := Empty;
5616 Prim_Elmt : Elmt_Id;
5617 Prim_Id : Entity_Id;
5618 Typ : Entity_Id := T;
5620 begin
5621 if Is_Class_Wide_Type (Typ) then
5622 Typ := Root_Type (Typ);
5623 end if;
5625 Typ := Underlying_Type (Typ);
5627 -- This search is based on the assertion that the dispatching version
5628 -- of the TSS routine always precedes the real primitive.
5630 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5631 while Present (Prim_Elmt) loop
5632 Prim_Id := Node (Prim_Elmt);
5634 if Is_TSS (Prim_Id, Name) then
5635 if Present (Alias (Prim_Id)) then
5636 Inher_Op := Prim_Id;
5637 else
5638 Own_Op := Prim_Id;
5639 end if;
5640 end if;
5642 Next_Elmt (Prim_Elmt);
5643 end loop;
5645 if Present (Own_Op) then
5646 return Own_Op;
5647 elsif Present (Inher_Op) then
5648 return Inher_Op;
5649 else
5650 return Empty;
5651 end if;
5652 end Find_Optional_Prim_Op;
5654 ------------------
5655 -- Find_Prim_Op --
5656 ------------------
5658 function Find_Prim_Op
5659 (T : Entity_Id; Name : Name_Id) return Entity_Id
5661 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5662 begin
5663 if No (Result) then
5664 raise Program_Error;
5665 end if;
5667 return Result;
5668 end Find_Prim_Op;
5670 ------------------
5671 -- Find_Prim_Op --
5672 ------------------
5674 function Find_Prim_Op
5675 (T : Entity_Id;
5676 Name : TSS_Name_Type) return Entity_Id
5678 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5679 begin
5680 if No (Result) then
5681 raise Program_Error;
5682 end if;
5684 return Result;
5685 end Find_Prim_Op;
5687 ----------------------------
5688 -- Find_Protection_Object --
5689 ----------------------------
5691 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5692 S : Entity_Id;
5694 begin
5695 S := Scop;
5696 while Present (S) loop
5697 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5698 and then Present (Protection_Object (S))
5699 then
5700 return Protection_Object (S);
5701 end if;
5703 S := Scope (S);
5704 end loop;
5706 -- If we do not find a Protection object in the scope chain, then
5707 -- something has gone wrong, most likely the object was never created.
5709 raise Program_Error;
5710 end Find_Protection_Object;
5712 --------------------------
5713 -- Find_Protection_Type --
5714 --------------------------
5716 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5717 Comp : Entity_Id;
5718 Typ : Entity_Id := Conc_Typ;
5720 begin
5721 if Is_Concurrent_Type (Typ) then
5722 Typ := Corresponding_Record_Type (Typ);
5723 end if;
5725 -- Since restriction violations are not considered serious errors, the
5726 -- expander remains active, but may leave the corresponding record type
5727 -- malformed. In such cases, component _object is not available so do
5728 -- not look for it.
5730 if not Analyzed (Typ) then
5731 return Empty;
5732 end if;
5734 Comp := First_Component (Typ);
5735 while Present (Comp) loop
5736 if Chars (Comp) = Name_uObject then
5737 return Base_Type (Etype (Comp));
5738 end if;
5740 Next_Component (Comp);
5741 end loop;
5743 -- The corresponding record of a protected type should always have an
5744 -- _object field.
5746 raise Program_Error;
5747 end Find_Protection_Type;
5749 -----------------------
5750 -- Find_Hook_Context --
5751 -----------------------
5753 function Find_Hook_Context (N : Node_Id) return Node_Id is
5754 Par : Node_Id;
5755 Top : Node_Id;
5757 Wrapped_Node : Node_Id;
5758 -- Note: if we are in a transient scope, we want to reuse it as
5759 -- the context for actions insertion, if possible. But if N is itself
5760 -- part of the stored actions for the current transient scope,
5761 -- then we need to insert at the appropriate (inner) location in
5762 -- the not as an action on Node_To_Be_Wrapped.
5764 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5766 begin
5767 -- When the node is inside a case/if expression, the lifetime of any
5768 -- temporary controlled object is extended. Find a suitable insertion
5769 -- node by locating the topmost case or if expressions.
5771 if In_Cond_Expr then
5772 Par := N;
5773 Top := N;
5774 while Present (Par) loop
5775 if Nkind_In (Original_Node (Par), N_Case_Expression,
5776 N_If_Expression)
5777 then
5778 Top := Par;
5780 -- Prevent the search from going too far
5782 elsif Is_Body_Or_Package_Declaration (Par) then
5783 exit;
5784 end if;
5786 Par := Parent (Par);
5787 end loop;
5789 -- The topmost case or if expression is now recovered, but it may
5790 -- still not be the correct place to add generated code. Climb to
5791 -- find a parent that is part of a declarative or statement list,
5792 -- and is not a list of actuals in a call.
5794 Par := Top;
5795 while Present (Par) loop
5796 if Is_List_Member (Par)
5797 and then not Nkind_In (Par, N_Component_Association,
5798 N_Discriminant_Association,
5799 N_Parameter_Association,
5800 N_Pragma_Argument_Association)
5801 and then not Nkind_In (Parent (Par), N_Function_Call,
5802 N_Procedure_Call_Statement,
5803 N_Entry_Call_Statement)
5805 then
5806 return Par;
5808 -- Prevent the search from going too far
5810 elsif Is_Body_Or_Package_Declaration (Par) then
5811 exit;
5812 end if;
5814 Par := Parent (Par);
5815 end loop;
5817 return Par;
5819 else
5820 Par := N;
5821 while Present (Par) loop
5823 -- Keep climbing past various operators
5825 if Nkind (Parent (Par)) in N_Op
5826 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5827 then
5828 Par := Parent (Par);
5829 else
5830 exit;
5831 end if;
5832 end loop;
5834 Top := Par;
5836 -- The node may be located in a pragma in which case return the
5837 -- pragma itself:
5839 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5841 -- Similar case occurs when the node is related to an object
5842 -- declaration or assignment:
5844 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5846 -- Another case to consider is when the node is part of a return
5847 -- statement:
5849 -- return ... and then Ctrl_Func_Call ...;
5851 -- Another case is when the node acts as a formal in a procedure
5852 -- call statement:
5854 -- Proc (... and then Ctrl_Func_Call ...);
5856 if Scope_Is_Transient then
5857 Wrapped_Node := Node_To_Be_Wrapped;
5858 else
5859 Wrapped_Node := Empty;
5860 end if;
5862 while Present (Par) loop
5863 if Par = Wrapped_Node
5864 or else Nkind_In (Par, N_Assignment_Statement,
5865 N_Object_Declaration,
5866 N_Pragma,
5867 N_Procedure_Call_Statement,
5868 N_Simple_Return_Statement)
5869 then
5870 return Par;
5872 -- Prevent the search from going too far
5874 elsif Is_Body_Or_Package_Declaration (Par) then
5875 exit;
5876 end if;
5878 Par := Parent (Par);
5879 end loop;
5881 -- Return the topmost short circuit operator
5883 return Top;
5884 end if;
5885 end Find_Hook_Context;
5887 ------------------------------
5888 -- Following_Address_Clause --
5889 ------------------------------
5891 function Following_Address_Clause (D : Node_Id) return Node_Id is
5892 Id : constant Entity_Id := Defining_Identifier (D);
5893 Result : Node_Id;
5894 Par : Node_Id;
5896 function Check_Decls (D : Node_Id) return Node_Id;
5897 -- This internal function differs from the main function in that it
5898 -- gets called to deal with a following package private part, and
5899 -- it checks declarations starting with D (the main function checks
5900 -- declarations following D). If D is Empty, then Empty is returned.
5902 -----------------
5903 -- Check_Decls --
5904 -----------------
5906 function Check_Decls (D : Node_Id) return Node_Id is
5907 Decl : Node_Id;
5909 begin
5910 Decl := D;
5911 while Present (Decl) loop
5912 if Nkind (Decl) = N_At_Clause
5913 and then Chars (Identifier (Decl)) = Chars (Id)
5914 then
5915 return Decl;
5917 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5918 and then Chars (Decl) = Name_Address
5919 and then Chars (Name (Decl)) = Chars (Id)
5920 then
5921 return Decl;
5922 end if;
5924 Next (Decl);
5925 end loop;
5927 -- Otherwise not found, return Empty
5929 return Empty;
5930 end Check_Decls;
5932 -- Start of processing for Following_Address_Clause
5934 begin
5935 -- If parser detected no address clause for the identifier in question,
5936 -- then the answer is a quick NO, without the need for a search.
5938 if not Get_Name_Table_Boolean1 (Chars (Id)) then
5939 return Empty;
5940 end if;
5942 -- Otherwise search current declarative unit
5944 Result := Check_Decls (Next (D));
5946 if Present (Result) then
5947 return Result;
5948 end if;
5950 -- Check for possible package private part following
5952 Par := Parent (D);
5954 if Nkind (Par) = N_Package_Specification
5955 and then Visible_Declarations (Par) = List_Containing (D)
5956 and then Present (Private_Declarations (Par))
5957 then
5958 -- Private part present, check declarations there
5960 return Check_Decls (First (Private_Declarations (Par)));
5962 else
5963 -- No private part, clause not found, return Empty
5965 return Empty;
5966 end if;
5967 end Following_Address_Clause;
5969 ----------------------
5970 -- Force_Evaluation --
5971 ----------------------
5973 procedure Force_Evaluation
5974 (Exp : Node_Id;
5975 Name_Req : Boolean := False;
5976 Related_Id : Entity_Id := Empty;
5977 Is_Low_Bound : Boolean := False;
5978 Is_High_Bound : Boolean := False;
5979 Mode : Force_Evaluation_Mode := Relaxed)
5981 begin
5982 Remove_Side_Effects
5983 (Exp => Exp,
5984 Name_Req => Name_Req,
5985 Variable_Ref => True,
5986 Renaming_Req => False,
5987 Related_Id => Related_Id,
5988 Is_Low_Bound => Is_Low_Bound,
5989 Is_High_Bound => Is_High_Bound,
5990 Check_Side_Effects =>
5991 Is_Static_Expression (Exp)
5992 or else Mode = Relaxed);
5993 end Force_Evaluation;
5995 ---------------------------------
5996 -- Fully_Qualified_Name_String --
5997 ---------------------------------
5999 function Fully_Qualified_Name_String
6000 (E : Entity_Id;
6001 Append_NUL : Boolean := True) return String_Id
6003 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6004 -- Compute recursively the qualified name without NUL at the end, adding
6005 -- it to the currently started string being generated
6007 ----------------------------------
6008 -- Internal_Full_Qualified_Name --
6009 ----------------------------------
6011 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6012 Ent : Entity_Id;
6014 begin
6015 -- Deal properly with child units
6017 if Nkind (E) = N_Defining_Program_Unit_Name then
6018 Ent := Defining_Identifier (E);
6019 else
6020 Ent := E;
6021 end if;
6023 -- Compute qualification recursively (only "Standard" has no scope)
6025 if Present (Scope (Scope (Ent))) then
6026 Internal_Full_Qualified_Name (Scope (Ent));
6027 Store_String_Char (Get_Char_Code ('.'));
6028 end if;
6030 -- Every entity should have a name except some expanded blocks
6031 -- don't bother about those.
6033 if Chars (Ent) = No_Name then
6034 return;
6035 end if;
6037 -- Generates the entity name in upper case
6039 Get_Decoded_Name_String (Chars (Ent));
6040 Set_All_Upper_Case;
6041 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6042 return;
6043 end Internal_Full_Qualified_Name;
6045 -- Start of processing for Full_Qualified_Name
6047 begin
6048 Start_String;
6049 Internal_Full_Qualified_Name (E);
6051 if Append_NUL then
6052 Store_String_Char (Get_Char_Code (ASCII.NUL));
6053 end if;
6055 return End_String;
6056 end Fully_Qualified_Name_String;
6058 ------------------------
6059 -- Generate_Poll_Call --
6060 ------------------------
6062 procedure Generate_Poll_Call (N : Node_Id) is
6063 begin
6064 -- No poll call if polling not active
6066 if not Polling_Required then
6067 return;
6069 -- Otherwise generate require poll call
6071 else
6072 Insert_Before_And_Analyze (N,
6073 Make_Procedure_Call_Statement (Sloc (N),
6074 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6075 end if;
6076 end Generate_Poll_Call;
6078 ---------------------------------
6079 -- Get_Current_Value_Condition --
6080 ---------------------------------
6082 -- Note: the implementation of this procedure is very closely tied to the
6083 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6084 -- interpret Current_Value fields set by the Set procedure, so the two
6085 -- procedures need to be closely coordinated.
6087 procedure Get_Current_Value_Condition
6088 (Var : Node_Id;
6089 Op : out Node_Kind;
6090 Val : out Node_Id)
6092 Loc : constant Source_Ptr := Sloc (Var);
6093 Ent : constant Entity_Id := Entity (Var);
6095 procedure Process_Current_Value_Condition
6096 (N : Node_Id;
6097 S : Boolean);
6098 -- N is an expression which holds either True (S = True) or False (S =
6099 -- False) in the condition. This procedure digs out the expression and
6100 -- if it refers to Ent, sets Op and Val appropriately.
6102 -------------------------------------
6103 -- Process_Current_Value_Condition --
6104 -------------------------------------
6106 procedure Process_Current_Value_Condition
6107 (N : Node_Id;
6108 S : Boolean)
6110 Cond : Node_Id;
6111 Prev_Cond : Node_Id;
6112 Sens : Boolean;
6114 begin
6115 Cond := N;
6116 Sens := S;
6118 loop
6119 Prev_Cond := Cond;
6121 -- Deal with NOT operators, inverting sense
6123 while Nkind (Cond) = N_Op_Not loop
6124 Cond := Right_Opnd (Cond);
6125 Sens := not Sens;
6126 end loop;
6128 -- Deal with conversions, qualifications, and expressions with
6129 -- actions.
6131 while Nkind_In (Cond,
6132 N_Type_Conversion,
6133 N_Qualified_Expression,
6134 N_Expression_With_Actions)
6135 loop
6136 Cond := Expression (Cond);
6137 end loop;
6139 exit when Cond = Prev_Cond;
6140 end loop;
6142 -- Deal with AND THEN and AND cases
6144 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6146 -- Don't ever try to invert a condition that is of the form of an
6147 -- AND or AND THEN (since we are not doing sufficiently general
6148 -- processing to allow this).
6150 if Sens = False then
6151 Op := N_Empty;
6152 Val := Empty;
6153 return;
6154 end if;
6156 -- Recursively process AND and AND THEN branches
6158 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6160 if Op /= N_Empty then
6161 return;
6162 end if;
6164 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6165 return;
6167 -- Case of relational operator
6169 elsif Nkind (Cond) in N_Op_Compare then
6170 Op := Nkind (Cond);
6172 -- Invert sense of test if inverted test
6174 if Sens = False then
6175 case Op is
6176 when N_Op_Eq => Op := N_Op_Ne;
6177 when N_Op_Ne => Op := N_Op_Eq;
6178 when N_Op_Lt => Op := N_Op_Ge;
6179 when N_Op_Gt => Op := N_Op_Le;
6180 when N_Op_Le => Op := N_Op_Gt;
6181 when N_Op_Ge => Op := N_Op_Lt;
6182 when others => raise Program_Error;
6183 end case;
6184 end if;
6186 -- Case of entity op value
6188 if Is_Entity_Name (Left_Opnd (Cond))
6189 and then Ent = Entity (Left_Opnd (Cond))
6190 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6191 then
6192 Val := Right_Opnd (Cond);
6194 -- Case of value op entity
6196 elsif Is_Entity_Name (Right_Opnd (Cond))
6197 and then Ent = Entity (Right_Opnd (Cond))
6198 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6199 then
6200 Val := Left_Opnd (Cond);
6202 -- We are effectively swapping operands
6204 case Op is
6205 when N_Op_Eq => null;
6206 when N_Op_Ne => null;
6207 when N_Op_Lt => Op := N_Op_Gt;
6208 when N_Op_Gt => Op := N_Op_Lt;
6209 when N_Op_Le => Op := N_Op_Ge;
6210 when N_Op_Ge => Op := N_Op_Le;
6211 when others => raise Program_Error;
6212 end case;
6214 else
6215 Op := N_Empty;
6216 end if;
6218 return;
6220 elsif Nkind_In (Cond,
6221 N_Type_Conversion,
6222 N_Qualified_Expression,
6223 N_Expression_With_Actions)
6224 then
6225 Cond := Expression (Cond);
6227 -- Case of Boolean variable reference, return as though the
6228 -- reference had said var = True.
6230 else
6231 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6232 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6234 if Sens = False then
6235 Op := N_Op_Ne;
6236 else
6237 Op := N_Op_Eq;
6238 end if;
6239 end if;
6240 end if;
6241 end Process_Current_Value_Condition;
6243 -- Start of processing for Get_Current_Value_Condition
6245 begin
6246 Op := N_Empty;
6247 Val := Empty;
6249 -- Immediate return, nothing doing, if this is not an object
6251 if Ekind (Ent) not in Object_Kind then
6252 return;
6253 end if;
6255 -- Otherwise examine current value
6257 declare
6258 CV : constant Node_Id := Current_Value (Ent);
6259 Sens : Boolean;
6260 Stm : Node_Id;
6262 begin
6263 -- If statement. Condition is known true in THEN section, known False
6264 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6266 if Nkind (CV) = N_If_Statement then
6268 -- Before start of IF statement
6270 if Loc < Sloc (CV) then
6271 return;
6273 -- After end of IF statement
6275 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6276 return;
6277 end if;
6279 -- At this stage we know that we are within the IF statement, but
6280 -- unfortunately, the tree does not record the SLOC of the ELSE so
6281 -- we cannot use a simple SLOC comparison to distinguish between
6282 -- the then/else statements, so we have to climb the tree.
6284 declare
6285 N : Node_Id;
6287 begin
6288 N := Parent (Var);
6289 while Parent (N) /= CV loop
6290 N := Parent (N);
6292 -- If we fall off the top of the tree, then that's odd, but
6293 -- perhaps it could occur in some error situation, and the
6294 -- safest response is simply to assume that the outcome of
6295 -- the condition is unknown. No point in bombing during an
6296 -- attempt to optimize things.
6298 if No (N) then
6299 return;
6300 end if;
6301 end loop;
6303 -- Now we have N pointing to a node whose parent is the IF
6304 -- statement in question, so now we can tell if we are within
6305 -- the THEN statements.
6307 if Is_List_Member (N)
6308 and then List_Containing (N) = Then_Statements (CV)
6309 then
6310 Sens := True;
6312 -- If the variable reference does not come from source, we
6313 -- cannot reliably tell whether it appears in the else part.
6314 -- In particular, if it appears in generated code for a node
6315 -- that requires finalization, it may be attached to a list
6316 -- that has not been yet inserted into the code. For now,
6317 -- treat it as unknown.
6319 elsif not Comes_From_Source (N) then
6320 return;
6322 -- Otherwise we must be in ELSIF or ELSE part
6324 else
6325 Sens := False;
6326 end if;
6327 end;
6329 -- ELSIF part. Condition is known true within the referenced
6330 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6331 -- and unknown before the ELSE part or after the IF statement.
6333 elsif Nkind (CV) = N_Elsif_Part then
6335 -- if the Elsif_Part had condition_actions, the elsif has been
6336 -- rewritten as a nested if, and the original elsif_part is
6337 -- detached from the tree, so there is no way to obtain useful
6338 -- information on the current value of the variable.
6339 -- Can this be improved ???
6341 if No (Parent (CV)) then
6342 return;
6343 end if;
6345 Stm := Parent (CV);
6347 -- If the tree has been otherwise rewritten there is nothing
6348 -- else to be done either.
6350 if Nkind (Stm) /= N_If_Statement then
6351 return;
6352 end if;
6354 -- Before start of ELSIF part
6356 if Loc < Sloc (CV) then
6357 return;
6359 -- After end of IF statement
6361 elsif Loc >= Sloc (Stm) +
6362 Text_Ptr (UI_To_Int (End_Span (Stm)))
6363 then
6364 return;
6365 end if;
6367 -- Again we lack the SLOC of the ELSE, so we need to climb the
6368 -- tree to see if we are within the ELSIF part in question.
6370 declare
6371 N : Node_Id;
6373 begin
6374 N := Parent (Var);
6375 while Parent (N) /= Stm loop
6376 N := Parent (N);
6378 -- If we fall off the top of the tree, then that's odd, but
6379 -- perhaps it could occur in some error situation, and the
6380 -- safest response is simply to assume that the outcome of
6381 -- the condition is unknown. No point in bombing during an
6382 -- attempt to optimize things.
6384 if No (N) then
6385 return;
6386 end if;
6387 end loop;
6389 -- Now we have N pointing to a node whose parent is the IF
6390 -- statement in question, so see if is the ELSIF part we want.
6391 -- the THEN statements.
6393 if N = CV then
6394 Sens := True;
6396 -- Otherwise we must be in subsequent ELSIF or ELSE part
6398 else
6399 Sens := False;
6400 end if;
6401 end;
6403 -- Iteration scheme of while loop. The condition is known to be
6404 -- true within the body of the loop.
6406 elsif Nkind (CV) = N_Iteration_Scheme then
6407 declare
6408 Loop_Stmt : constant Node_Id := Parent (CV);
6410 begin
6411 -- Before start of body of loop
6413 if Loc < Sloc (Loop_Stmt) then
6414 return;
6416 -- After end of LOOP statement
6418 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6419 return;
6421 -- We are within the body of the loop
6423 else
6424 Sens := True;
6425 end if;
6426 end;
6428 -- All other cases of Current_Value settings
6430 else
6431 return;
6432 end if;
6434 -- If we fall through here, then we have a reportable condition, Sens
6435 -- is True if the condition is true and False if it needs inverting.
6437 Process_Current_Value_Condition (Condition (CV), Sens);
6438 end;
6439 end Get_Current_Value_Condition;
6441 ---------------------
6442 -- Get_Stream_Size --
6443 ---------------------
6445 function Get_Stream_Size (E : Entity_Id) return Uint is
6446 begin
6447 -- If we have a Stream_Size clause for this type use it
6449 if Has_Stream_Size_Clause (E) then
6450 return Static_Integer (Expression (Stream_Size_Clause (E)));
6452 -- Otherwise the Stream_Size if the size of the type
6454 else
6455 return Esize (E);
6456 end if;
6457 end Get_Stream_Size;
6459 ---------------------------
6460 -- Has_Access_Constraint --
6461 ---------------------------
6463 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6464 Disc : Entity_Id;
6465 T : constant Entity_Id := Etype (E);
6467 begin
6468 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6469 Disc := First_Discriminant (T);
6470 while Present (Disc) loop
6471 if Is_Access_Type (Etype (Disc)) then
6472 return True;
6473 end if;
6475 Next_Discriminant (Disc);
6476 end loop;
6478 return False;
6479 else
6480 return False;
6481 end if;
6482 end Has_Access_Constraint;
6484 -----------------------------------------------------
6485 -- Has_Annotate_Pragma_For_External_Axiomatization --
6486 -----------------------------------------------------
6488 function Has_Annotate_Pragma_For_External_Axiomatization
6489 (E : Entity_Id) return Boolean
6491 function Is_Annotate_Pragma_For_External_Axiomatization
6492 (N : Node_Id) return Boolean;
6493 -- Returns whether N is
6494 -- pragma Annotate (GNATprove, External_Axiomatization);
6496 ----------------------------------------------------
6497 -- Is_Annotate_Pragma_For_External_Axiomatization --
6498 ----------------------------------------------------
6500 -- The general form of pragma Annotate is
6502 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6503 -- ARG ::= NAME | EXPRESSION
6505 -- The first two arguments are by convention intended to refer to an
6506 -- external tool and a tool-specific function. These arguments are
6507 -- not analyzed.
6509 -- The following is used to annotate a package specification which
6510 -- GNATprove should treat specially, because the axiomatization of
6511 -- this unit is given by the user instead of being automatically
6512 -- generated.
6514 -- pragma Annotate (GNATprove, External_Axiomatization);
6516 function Is_Annotate_Pragma_For_External_Axiomatization
6517 (N : Node_Id) return Boolean
6519 Name_GNATprove : constant String :=
6520 "gnatprove";
6521 Name_External_Axiomatization : constant String :=
6522 "external_axiomatization";
6523 -- Special names
6525 begin
6526 if Nkind (N) = N_Pragma
6527 and then Get_Pragma_Id (N) = Pragma_Annotate
6528 and then List_Length (Pragma_Argument_Associations (N)) = 2
6529 then
6530 declare
6531 Arg1 : constant Node_Id :=
6532 First (Pragma_Argument_Associations (N));
6533 Arg2 : constant Node_Id := Next (Arg1);
6534 Nam1 : Name_Id;
6535 Nam2 : Name_Id;
6537 begin
6538 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6539 -- Name_External_Axiomatization so that Name_Find returns the
6540 -- corresponding name. This takes care of all possible casings.
6542 Name_Len := 0;
6543 Add_Str_To_Name_Buffer (Name_GNATprove);
6544 Nam1 := Name_Find;
6546 Name_Len := 0;
6547 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6548 Nam2 := Name_Find;
6550 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6551 and then
6552 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6553 end;
6555 else
6556 return False;
6557 end if;
6558 end Is_Annotate_Pragma_For_External_Axiomatization;
6560 -- Local variables
6562 Decl : Node_Id;
6563 Vis_Decls : List_Id;
6564 N : Node_Id;
6566 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6568 begin
6569 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6570 Decl := Parent (Parent (E));
6571 else
6572 Decl := Parent (E);
6573 end if;
6575 Vis_Decls := Visible_Declarations (Decl);
6577 N := First (Vis_Decls);
6578 while Present (N) loop
6580 -- Skip declarations generated by the frontend. Skip all pragmas
6581 -- that are not the desired Annotate pragma. Stop the search on
6582 -- the first non-pragma source declaration.
6584 if Comes_From_Source (N) then
6585 if Nkind (N) = N_Pragma then
6586 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6587 return True;
6588 end if;
6589 else
6590 return False;
6591 end if;
6592 end if;
6594 Next (N);
6595 end loop;
6597 return False;
6598 end Has_Annotate_Pragma_For_External_Axiomatization;
6600 --------------------
6601 -- Homonym_Number --
6602 --------------------
6604 function Homonym_Number (Subp : Entity_Id) return Nat is
6605 Count : Nat;
6606 Hom : Entity_Id;
6608 begin
6609 Count := 1;
6610 Hom := Homonym (Subp);
6611 while Present (Hom) loop
6612 if Scope (Hom) = Scope (Subp) then
6613 Count := Count + 1;
6614 end if;
6616 Hom := Homonym (Hom);
6617 end loop;
6619 return Count;
6620 end Homonym_Number;
6622 -----------------------------------
6623 -- In_Library_Level_Package_Body --
6624 -----------------------------------
6626 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6627 begin
6628 -- First determine whether the entity appears at the library level, then
6629 -- look at the containing unit.
6631 if Is_Library_Level_Entity (Id) then
6632 declare
6633 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6635 begin
6636 return Nkind (Unit (Container)) = N_Package_Body;
6637 end;
6638 end if;
6640 return False;
6641 end In_Library_Level_Package_Body;
6643 ------------------------------
6644 -- In_Unconditional_Context --
6645 ------------------------------
6647 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6648 P : Node_Id;
6650 begin
6651 P := Node;
6652 while Present (P) loop
6653 case Nkind (P) is
6654 when N_Subprogram_Body => return True;
6655 when N_If_Statement => return False;
6656 when N_Loop_Statement => return False;
6657 when N_Case_Statement => return False;
6658 when others => P := Parent (P);
6659 end case;
6660 end loop;
6662 return False;
6663 end In_Unconditional_Context;
6665 -------------------
6666 -- Insert_Action --
6667 -------------------
6669 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6670 begin
6671 if Present (Ins_Action) then
6672 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6673 end if;
6674 end Insert_Action;
6676 -- Version with check(s) suppressed
6678 procedure Insert_Action
6679 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6681 begin
6682 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6683 end Insert_Action;
6685 -------------------------
6686 -- Insert_Action_After --
6687 -------------------------
6689 procedure Insert_Action_After
6690 (Assoc_Node : Node_Id;
6691 Ins_Action : Node_Id)
6693 begin
6694 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6695 end Insert_Action_After;
6697 --------------------
6698 -- Insert_Actions --
6699 --------------------
6701 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6702 N : Node_Id;
6703 P : Node_Id;
6705 Wrapped_Node : Node_Id := Empty;
6707 begin
6708 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6709 return;
6710 end if;
6712 -- Ignore insert of actions from inside default expression (or other
6713 -- similar "spec expression") in the special spec-expression analyze
6714 -- mode. Any insertions at this point have no relevance, since we are
6715 -- only doing the analyze to freeze the types of any static expressions.
6716 -- See section "Handling of Default Expressions" in the spec of package
6717 -- Sem for further details.
6719 if In_Spec_Expression then
6720 return;
6721 end if;
6723 -- If the action derives from stuff inside a record, then the actions
6724 -- are attached to the current scope, to be inserted and analyzed on
6725 -- exit from the scope. The reason for this is that we may also be
6726 -- generating freeze actions at the same time, and they must eventually
6727 -- be elaborated in the correct order.
6729 if Is_Record_Type (Current_Scope)
6730 and then not Is_Frozen (Current_Scope)
6731 then
6732 if No (Scope_Stack.Table
6733 (Scope_Stack.Last).Pending_Freeze_Actions)
6734 then
6735 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6736 Ins_Actions;
6737 else
6738 Append_List
6739 (Ins_Actions,
6740 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6741 end if;
6743 return;
6744 end if;
6746 -- We now intend to climb up the tree to find the right point to
6747 -- insert the actions. We start at Assoc_Node, unless this node is a
6748 -- subexpression in which case we start with its parent. We do this for
6749 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6750 -- itself one of the special nodes like N_And_Then, then we assume that
6751 -- an initial request to insert actions for such a node does not expect
6752 -- the actions to get deposited in the node for later handling when the
6753 -- node is expanded, since clearly the node is being dealt with by the
6754 -- caller. Note that in the subexpression case, N is always the child we
6755 -- came from.
6757 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6758 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6759 -- Procedure calls, and similarly procedure attribute references, are
6760 -- also statements.
6762 if Nkind (Assoc_Node) in N_Subexpr
6763 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6764 or else Etype (Assoc_Node) /= Standard_Void_Type)
6765 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6766 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6767 or else not Is_Procedure_Attribute_Name
6768 (Attribute_Name (Assoc_Node)))
6769 then
6770 N := Assoc_Node;
6771 P := Parent (Assoc_Node);
6773 -- Non-subexpression case. Note that N is initially Empty in this case
6774 -- (N is only guaranteed Non-Empty in the subexpr case).
6776 else
6777 N := Empty;
6778 P := Assoc_Node;
6779 end if;
6781 -- Capture root of the transient scope
6783 if Scope_Is_Transient then
6784 Wrapped_Node := Node_To_Be_Wrapped;
6785 end if;
6787 loop
6788 pragma Assert (Present (P));
6790 -- Make sure that inserted actions stay in the transient scope
6792 if Present (Wrapped_Node) and then N = Wrapped_Node then
6793 Store_Before_Actions_In_Scope (Ins_Actions);
6794 return;
6795 end if;
6797 case Nkind (P) is
6799 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6800 -- in the Actions field of the right operand. They will be moved
6801 -- out further when the AND THEN or OR ELSE operator is expanded.
6802 -- Nothing special needs to be done for the left operand since
6803 -- in that case the actions are executed unconditionally.
6805 when N_Short_Circuit =>
6806 if N = Right_Opnd (P) then
6808 -- We are now going to either append the actions to the
6809 -- actions field of the short-circuit operation. We will
6810 -- also analyze the actions now.
6812 -- This analysis is really too early, the proper thing would
6813 -- be to just park them there now, and only analyze them if
6814 -- we find we really need them, and to it at the proper
6815 -- final insertion point. However attempting to this proved
6816 -- tricky, so for now we just kill current values before and
6817 -- after the analyze call to make sure we avoid peculiar
6818 -- optimizations from this out of order insertion.
6820 Kill_Current_Values;
6822 -- If P has already been expanded, we can't park new actions
6823 -- on it, so we need to expand them immediately, introducing
6824 -- an Expression_With_Actions. N can't be an expression
6825 -- with actions, or else then the actions would have been
6826 -- inserted at an inner level.
6828 if Analyzed (P) then
6829 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6830 Rewrite (N,
6831 Make_Expression_With_Actions (Sloc (N),
6832 Actions => Ins_Actions,
6833 Expression => Relocate_Node (N)));
6834 Analyze_And_Resolve (N);
6836 elsif Present (Actions (P)) then
6837 Insert_List_After_And_Analyze
6838 (Last (Actions (P)), Ins_Actions);
6839 else
6840 Set_Actions (P, Ins_Actions);
6841 Analyze_List (Actions (P));
6842 end if;
6844 Kill_Current_Values;
6846 return;
6847 end if;
6849 -- Then or Else dependent expression of an if expression. Add
6850 -- actions to Then_Actions or Else_Actions field as appropriate.
6851 -- The actions will be moved further out when the if is expanded.
6853 when N_If_Expression =>
6854 declare
6855 ThenX : constant Node_Id := Next (First (Expressions (P)));
6856 ElseX : constant Node_Id := Next (ThenX);
6858 begin
6859 -- If the enclosing expression is already analyzed, as
6860 -- is the case for nested elaboration checks, insert the
6861 -- conditional further out.
6863 if Analyzed (P) then
6864 null;
6866 -- Actions belong to the then expression, temporarily place
6867 -- them as Then_Actions of the if expression. They will be
6868 -- moved to the proper place later when the if expression
6869 -- is expanded.
6871 elsif N = ThenX then
6872 if Present (Then_Actions (P)) then
6873 Insert_List_After_And_Analyze
6874 (Last (Then_Actions (P)), Ins_Actions);
6875 else
6876 Set_Then_Actions (P, Ins_Actions);
6877 Analyze_List (Then_Actions (P));
6878 end if;
6880 return;
6882 -- Actions belong to the else expression, temporarily place
6883 -- them as Else_Actions of the if expression. They will be
6884 -- moved to the proper place later when the if expression
6885 -- is expanded.
6887 elsif N = ElseX then
6888 if Present (Else_Actions (P)) then
6889 Insert_List_After_And_Analyze
6890 (Last (Else_Actions (P)), Ins_Actions);
6891 else
6892 Set_Else_Actions (P, Ins_Actions);
6893 Analyze_List (Else_Actions (P));
6894 end if;
6896 return;
6898 -- Actions belong to the condition. In this case they are
6899 -- unconditionally executed, and so we can continue the
6900 -- search for the proper insert point.
6902 else
6903 null;
6904 end if;
6905 end;
6907 -- Alternative of case expression, we place the action in the
6908 -- Actions field of the case expression alternative, this will
6909 -- be handled when the case expression is expanded.
6911 when N_Case_Expression_Alternative =>
6912 if Present (Actions (P)) then
6913 Insert_List_After_And_Analyze
6914 (Last (Actions (P)), Ins_Actions);
6915 else
6916 Set_Actions (P, Ins_Actions);
6917 Analyze_List (Actions (P));
6918 end if;
6920 return;
6922 -- Case of appearing within an Expressions_With_Actions node. When
6923 -- the new actions come from the expression of the expression with
6924 -- actions, they must be added to the existing actions. The other
6925 -- alternative is when the new actions are related to one of the
6926 -- existing actions of the expression with actions, and should
6927 -- never reach here: if actions are inserted on a statement
6928 -- within the Actions of an expression with actions, or on some
6929 -- subexpression of such a statement, then the outermost proper
6930 -- insertion point is right before the statement, and we should
6931 -- never climb up as far as the N_Expression_With_Actions itself.
6933 when N_Expression_With_Actions =>
6934 if N = Expression (P) then
6935 if Is_Empty_List (Actions (P)) then
6936 Append_List_To (Actions (P), Ins_Actions);
6937 Analyze_List (Actions (P));
6938 else
6939 Insert_List_After_And_Analyze
6940 (Last (Actions (P)), Ins_Actions);
6941 end if;
6943 return;
6945 else
6946 raise Program_Error;
6947 end if;
6949 -- Case of appearing in the condition of a while expression or
6950 -- elsif. We insert the actions into the Condition_Actions field.
6951 -- They will be moved further out when the while loop or elsif
6952 -- is analyzed.
6954 when N_Elsif_Part
6955 | N_Iteration_Scheme
6957 if N = Condition (P) then
6958 if Present (Condition_Actions (P)) then
6959 Insert_List_After_And_Analyze
6960 (Last (Condition_Actions (P)), Ins_Actions);
6961 else
6962 Set_Condition_Actions (P, Ins_Actions);
6964 -- Set the parent of the insert actions explicitly. This
6965 -- is not a syntactic field, but we need the parent field
6966 -- set, in particular so that freeze can understand that
6967 -- it is dealing with condition actions, and properly
6968 -- insert the freezing actions.
6970 Set_Parent (Ins_Actions, P);
6971 Analyze_List (Condition_Actions (P));
6972 end if;
6974 return;
6975 end if;
6977 -- Statements, declarations, pragmas, representation clauses
6979 when
6980 -- Statements
6982 N_Procedure_Call_Statement
6983 | N_Statement_Other_Than_Procedure_Call
6985 -- Pragmas
6987 | N_Pragma
6989 -- Representation_Clause
6991 | N_At_Clause
6992 | N_Attribute_Definition_Clause
6993 | N_Enumeration_Representation_Clause
6994 | N_Record_Representation_Clause
6996 -- Declarations
6998 | N_Abstract_Subprogram_Declaration
6999 | N_Entry_Body
7000 | N_Exception_Declaration
7001 | N_Exception_Renaming_Declaration
7002 | N_Expression_Function
7003 | N_Formal_Abstract_Subprogram_Declaration
7004 | N_Formal_Concrete_Subprogram_Declaration
7005 | N_Formal_Object_Declaration
7006 | N_Formal_Type_Declaration
7007 | N_Full_Type_Declaration
7008 | N_Function_Instantiation
7009 | N_Generic_Function_Renaming_Declaration
7010 | N_Generic_Package_Declaration
7011 | N_Generic_Package_Renaming_Declaration
7012 | N_Generic_Procedure_Renaming_Declaration
7013 | N_Generic_Subprogram_Declaration
7014 | N_Implicit_Label_Declaration
7015 | N_Incomplete_Type_Declaration
7016 | N_Number_Declaration
7017 | N_Object_Declaration
7018 | N_Object_Renaming_Declaration
7019 | N_Package_Body
7020 | N_Package_Body_Stub
7021 | N_Package_Declaration
7022 | N_Package_Instantiation
7023 | N_Package_Renaming_Declaration
7024 | N_Private_Extension_Declaration
7025 | N_Private_Type_Declaration
7026 | N_Procedure_Instantiation
7027 | N_Protected_Body
7028 | N_Protected_Body_Stub
7029 | N_Protected_Type_Declaration
7030 | N_Single_Task_Declaration
7031 | N_Subprogram_Body
7032 | N_Subprogram_Body_Stub
7033 | N_Subprogram_Declaration
7034 | N_Subprogram_Renaming_Declaration
7035 | N_Subtype_Declaration
7036 | N_Task_Body
7037 | N_Task_Body_Stub
7038 | N_Task_Type_Declaration
7040 -- Use clauses can appear in lists of declarations
7042 | N_Use_Package_Clause
7043 | N_Use_Type_Clause
7045 -- Freeze entity behaves like a declaration or statement
7047 | N_Freeze_Entity
7048 | N_Freeze_Generic_Entity
7050 -- Do not insert here if the item is not a list member (this
7051 -- happens for example with a triggering statement, and the
7052 -- proper approach is to insert before the entire select).
7054 if not Is_List_Member (P) then
7055 null;
7057 -- Do not insert if parent of P is an N_Component_Association
7058 -- node (i.e. we are in the context of an N_Aggregate or
7059 -- N_Extension_Aggregate node. In this case we want to insert
7060 -- before the entire aggregate.
7062 elsif Nkind (Parent (P)) = N_Component_Association then
7063 null;
7065 -- Do not insert if the parent of P is either an N_Variant node
7066 -- or an N_Record_Definition node, meaning in either case that
7067 -- P is a member of a component list, and that therefore the
7068 -- actions should be inserted outside the complete record
7069 -- declaration.
7071 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7072 null;
7074 -- Do not insert freeze nodes within the loop generated for
7075 -- an aggregate, because they may be elaborated too late for
7076 -- subsequent use in the back end: within a package spec the
7077 -- loop is part of the elaboration procedure and is only
7078 -- elaborated during the second pass.
7080 -- If the loop comes from source, or the entity is local to the
7081 -- loop itself it must remain within.
7083 elsif Nkind (Parent (P)) = N_Loop_Statement
7084 and then not Comes_From_Source (Parent (P))
7085 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7086 and then
7087 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7088 then
7089 null;
7091 -- Otherwise we can go ahead and do the insertion
7093 elsif P = Wrapped_Node then
7094 Store_Before_Actions_In_Scope (Ins_Actions);
7095 return;
7097 else
7098 Insert_List_Before_And_Analyze (P, Ins_Actions);
7099 return;
7100 end if;
7102 -- A special case, N_Raise_xxx_Error can act either as a statement
7103 -- or a subexpression. We tell the difference by looking at the
7104 -- Etype. It is set to Standard_Void_Type in the statement case.
7106 when N_Raise_xxx_Error =>
7107 if Etype (P) = Standard_Void_Type then
7108 if P = Wrapped_Node then
7109 Store_Before_Actions_In_Scope (Ins_Actions);
7110 else
7111 Insert_List_Before_And_Analyze (P, Ins_Actions);
7112 end if;
7114 return;
7116 -- In the subexpression case, keep climbing
7118 else
7119 null;
7120 end if;
7122 -- If a component association appears within a loop created for
7123 -- an array aggregate, attach the actions to the association so
7124 -- they can be subsequently inserted within the loop. For other
7125 -- component associations insert outside of the aggregate. For
7126 -- an association that will generate a loop, its Loop_Actions
7127 -- attribute is already initialized (see exp_aggr.adb).
7129 -- The list of Loop_Actions can in turn generate additional ones,
7130 -- that are inserted before the associated node. If the associated
7131 -- node is outside the aggregate, the new actions are collected
7132 -- at the end of the Loop_Actions, to respect the order in which
7133 -- they are to be elaborated.
7135 when N_Component_Association
7136 | N_Iterated_Component_Association
7138 if Nkind (Parent (P)) = N_Aggregate
7139 and then Present (Loop_Actions (P))
7140 then
7141 if Is_Empty_List (Loop_Actions (P)) then
7142 Set_Loop_Actions (P, Ins_Actions);
7143 Analyze_List (Ins_Actions);
7144 else
7145 declare
7146 Decl : Node_Id;
7148 begin
7149 -- Check whether these actions were generated by a
7150 -- declaration that is part of the Loop_Actions for
7151 -- the component_association.
7153 Decl := Assoc_Node;
7154 while Present (Decl) loop
7155 exit when Parent (Decl) = P
7156 and then Is_List_Member (Decl)
7157 and then
7158 List_Containing (Decl) = Loop_Actions (P);
7159 Decl := Parent (Decl);
7160 end loop;
7162 if Present (Decl) then
7163 Insert_List_Before_And_Analyze
7164 (Decl, Ins_Actions);
7165 else
7166 Insert_List_After_And_Analyze
7167 (Last (Loop_Actions (P)), Ins_Actions);
7168 end if;
7169 end;
7170 end if;
7172 return;
7174 else
7175 null;
7176 end if;
7178 -- Special case: an attribute denoting a procedure call
7180 when N_Attribute_Reference =>
7181 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7182 if P = Wrapped_Node then
7183 Store_Before_Actions_In_Scope (Ins_Actions);
7184 else
7185 Insert_List_Before_And_Analyze (P, Ins_Actions);
7186 end if;
7188 return;
7190 -- In the subexpression case, keep climbing
7192 else
7193 null;
7194 end if;
7196 -- Special case: a marker
7198 when N_Call_Marker
7199 | N_Variable_Reference_Marker
7201 if Is_List_Member (P) then
7202 Insert_List_Before_And_Analyze (P, Ins_Actions);
7203 return;
7204 end if;
7206 -- A contract node should not belong to the tree
7208 when N_Contract =>
7209 raise Program_Error;
7211 -- For all other node types, keep climbing tree
7213 when N_Abortable_Part
7214 | N_Accept_Alternative
7215 | N_Access_Definition
7216 | N_Access_Function_Definition
7217 | N_Access_Procedure_Definition
7218 | N_Access_To_Object_Definition
7219 | N_Aggregate
7220 | N_Allocator
7221 | N_Aspect_Specification
7222 | N_Case_Expression
7223 | N_Case_Statement_Alternative
7224 | N_Character_Literal
7225 | N_Compilation_Unit
7226 | N_Compilation_Unit_Aux
7227 | N_Component_Clause
7228 | N_Component_Declaration
7229 | N_Component_Definition
7230 | N_Component_List
7231 | N_Constrained_Array_Definition
7232 | N_Decimal_Fixed_Point_Definition
7233 | N_Defining_Character_Literal
7234 | N_Defining_Identifier
7235 | N_Defining_Operator_Symbol
7236 | N_Defining_Program_Unit_Name
7237 | N_Delay_Alternative
7238 | N_Delta_Aggregate
7239 | N_Delta_Constraint
7240 | N_Derived_Type_Definition
7241 | N_Designator
7242 | N_Digits_Constraint
7243 | N_Discriminant_Association
7244 | N_Discriminant_Specification
7245 | N_Empty
7246 | N_Entry_Body_Formal_Part
7247 | N_Entry_Call_Alternative
7248 | N_Entry_Declaration
7249 | N_Entry_Index_Specification
7250 | N_Enumeration_Type_Definition
7251 | N_Error
7252 | N_Exception_Handler
7253 | N_Expanded_Name
7254 | N_Explicit_Dereference
7255 | N_Extension_Aggregate
7256 | N_Floating_Point_Definition
7257 | N_Formal_Decimal_Fixed_Point_Definition
7258 | N_Formal_Derived_Type_Definition
7259 | N_Formal_Discrete_Type_Definition
7260 | N_Formal_Floating_Point_Definition
7261 | N_Formal_Modular_Type_Definition
7262 | N_Formal_Ordinary_Fixed_Point_Definition
7263 | N_Formal_Package_Declaration
7264 | N_Formal_Private_Type_Definition
7265 | N_Formal_Incomplete_Type_Definition
7266 | N_Formal_Signed_Integer_Type_Definition
7267 | N_Function_Call
7268 | N_Function_Specification
7269 | N_Generic_Association
7270 | N_Handled_Sequence_Of_Statements
7271 | N_Identifier
7272 | N_In
7273 | N_Index_Or_Discriminant_Constraint
7274 | N_Indexed_Component
7275 | N_Integer_Literal
7276 | N_Iterator_Specification
7277 | N_Itype_Reference
7278 | N_Label
7279 | N_Loop_Parameter_Specification
7280 | N_Mod_Clause
7281 | N_Modular_Type_Definition
7282 | N_Not_In
7283 | N_Null
7284 | N_Op_Abs
7285 | N_Op_Add
7286 | N_Op_And
7287 | N_Op_Concat
7288 | N_Op_Divide
7289 | N_Op_Eq
7290 | N_Op_Expon
7291 | N_Op_Ge
7292 | N_Op_Gt
7293 | N_Op_Le
7294 | N_Op_Lt
7295 | N_Op_Minus
7296 | N_Op_Mod
7297 | N_Op_Multiply
7298 | N_Op_Ne
7299 | N_Op_Not
7300 | N_Op_Or
7301 | N_Op_Plus
7302 | N_Op_Rem
7303 | N_Op_Rotate_Left
7304 | N_Op_Rotate_Right
7305 | N_Op_Shift_Left
7306 | N_Op_Shift_Right
7307 | N_Op_Shift_Right_Arithmetic
7308 | N_Op_Subtract
7309 | N_Op_Xor
7310 | N_Operator_Symbol
7311 | N_Ordinary_Fixed_Point_Definition
7312 | N_Others_Choice
7313 | N_Package_Specification
7314 | N_Parameter_Association
7315 | N_Parameter_Specification
7316 | N_Pop_Constraint_Error_Label
7317 | N_Pop_Program_Error_Label
7318 | N_Pop_Storage_Error_Label
7319 | N_Pragma_Argument_Association
7320 | N_Procedure_Specification
7321 | N_Protected_Definition
7322 | N_Push_Constraint_Error_Label
7323 | N_Push_Program_Error_Label
7324 | N_Push_Storage_Error_Label
7325 | N_Qualified_Expression
7326 | N_Quantified_Expression
7327 | N_Raise_Expression
7328 | N_Range
7329 | N_Range_Constraint
7330 | N_Real_Literal
7331 | N_Real_Range_Specification
7332 | N_Record_Definition
7333 | N_Reduction_Expression
7334 | N_Reduction_Expression_Parameter
7335 | N_Reference
7336 | N_SCIL_Dispatch_Table_Tag_Init
7337 | N_SCIL_Dispatching_Call
7338 | N_SCIL_Membership_Test
7339 | N_Selected_Component
7340 | N_Signed_Integer_Type_Definition
7341 | N_Single_Protected_Declaration
7342 | N_Slice
7343 | N_String_Literal
7344 | N_Subtype_Indication
7345 | N_Subunit
7346 | N_Target_Name
7347 | N_Task_Definition
7348 | N_Terminate_Alternative
7349 | N_Triggering_Alternative
7350 | N_Type_Conversion
7351 | N_Unchecked_Expression
7352 | N_Unchecked_Type_Conversion
7353 | N_Unconstrained_Array_Definition
7354 | N_Unused_At_End
7355 | N_Unused_At_Start
7356 | N_Variant
7357 | N_Variant_Part
7358 | N_Validate_Unchecked_Conversion
7359 | N_With_Clause
7361 null;
7362 end case;
7364 -- If we fall through above tests, keep climbing tree
7366 N := P;
7368 if Nkind (Parent (N)) = N_Subunit then
7370 -- This is the proper body corresponding to a stub. Insertion must
7371 -- be done at the point of the stub, which is in the declarative
7372 -- part of the parent unit.
7374 P := Corresponding_Stub (Parent (N));
7376 else
7377 P := Parent (N);
7378 end if;
7379 end loop;
7380 end Insert_Actions;
7382 -- Version with check(s) suppressed
7384 procedure Insert_Actions
7385 (Assoc_Node : Node_Id;
7386 Ins_Actions : List_Id;
7387 Suppress : Check_Id)
7389 begin
7390 if Suppress = All_Checks then
7391 declare
7392 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7393 begin
7394 Scope_Suppress.Suppress := (others => True);
7395 Insert_Actions (Assoc_Node, Ins_Actions);
7396 Scope_Suppress.Suppress := Sva;
7397 end;
7399 else
7400 declare
7401 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7402 begin
7403 Scope_Suppress.Suppress (Suppress) := True;
7404 Insert_Actions (Assoc_Node, Ins_Actions);
7405 Scope_Suppress.Suppress (Suppress) := Svg;
7406 end;
7407 end if;
7408 end Insert_Actions;
7410 --------------------------
7411 -- Insert_Actions_After --
7412 --------------------------
7414 procedure Insert_Actions_After
7415 (Assoc_Node : Node_Id;
7416 Ins_Actions : List_Id)
7418 begin
7419 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7420 Store_After_Actions_In_Scope (Ins_Actions);
7421 else
7422 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7423 end if;
7424 end Insert_Actions_After;
7426 ------------------------
7427 -- Insert_Declaration --
7428 ------------------------
7430 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7431 P : Node_Id;
7433 begin
7434 pragma Assert (Nkind (N) in N_Subexpr);
7436 -- Climb until we find a procedure or a package
7438 P := N;
7439 loop
7440 pragma Assert (Present (Parent (P)));
7441 P := Parent (P);
7443 if Is_List_Member (P) then
7444 exit when Nkind_In (Parent (P), N_Package_Specification,
7445 N_Subprogram_Body);
7447 -- Special handling for handled sequence of statements, we must
7448 -- insert in the statements not the exception handlers!
7450 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7451 P := First (Statements (Parent (P)));
7452 exit;
7453 end if;
7454 end if;
7455 end loop;
7457 -- Now do the insertion
7459 Insert_Before (P, Decl);
7460 Analyze (Decl);
7461 end Insert_Declaration;
7463 ---------------------------------
7464 -- Insert_Library_Level_Action --
7465 ---------------------------------
7467 procedure Insert_Library_Level_Action (N : Node_Id) is
7468 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7470 begin
7471 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7472 -- And not Main_Unit as previously. If the main unit is a body,
7473 -- the scope needed to analyze the actions is the entity of the
7474 -- corresponding declaration.
7476 if No (Actions (Aux)) then
7477 Set_Actions (Aux, New_List (N));
7478 else
7479 Append (N, Actions (Aux));
7480 end if;
7482 Analyze (N);
7483 Pop_Scope;
7484 end Insert_Library_Level_Action;
7486 ----------------------------------
7487 -- Insert_Library_Level_Actions --
7488 ----------------------------------
7490 procedure Insert_Library_Level_Actions (L : List_Id) is
7491 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7493 begin
7494 if Is_Non_Empty_List (L) then
7495 Push_Scope (Cunit_Entity (Main_Unit));
7496 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7498 if No (Actions (Aux)) then
7499 Set_Actions (Aux, L);
7500 Analyze_List (L);
7501 else
7502 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7503 end if;
7505 Pop_Scope;
7506 end if;
7507 end Insert_Library_Level_Actions;
7509 ----------------------
7510 -- Inside_Init_Proc --
7511 ----------------------
7513 function Inside_Init_Proc return Boolean is
7514 S : Entity_Id;
7516 begin
7517 S := Current_Scope;
7518 while Present (S) and then S /= Standard_Standard loop
7519 if Is_Init_Proc (S) then
7520 return True;
7521 else
7522 S := Scope (S);
7523 end if;
7524 end loop;
7526 return False;
7527 end Inside_Init_Proc;
7529 ----------------------------
7530 -- Is_All_Null_Statements --
7531 ----------------------------
7533 function Is_All_Null_Statements (L : List_Id) return Boolean is
7534 Stm : Node_Id;
7536 begin
7537 Stm := First (L);
7538 while Present (Stm) loop
7539 if Nkind (Stm) /= N_Null_Statement then
7540 return False;
7541 end if;
7543 Next (Stm);
7544 end loop;
7546 return True;
7547 end Is_All_Null_Statements;
7549 --------------------------------------------------
7550 -- Is_Displacement_Of_Object_Or_Function_Result --
7551 --------------------------------------------------
7553 function Is_Displacement_Of_Object_Or_Function_Result
7554 (Obj_Id : Entity_Id) return Boolean
7556 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7557 -- Determine whether node N denotes a controlled function call
7559 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
7560 -- Determine whether node N denotes a generalized indexing form which
7561 -- involves a controlled result.
7563 function Is_Displace_Call (N : Node_Id) return Boolean;
7564 -- Determine whether node N denotes a call to Ada.Tags.Displace
7566 function Is_Source_Object (N : Node_Id) return Boolean;
7567 -- Determine whether a particular node denotes a source object
7569 function Strip (N : Node_Id) return Node_Id;
7570 -- Examine arbitrary node N by stripping various indirections and return
7571 -- the "real" node.
7573 ---------------------------------
7574 -- Is_Controlled_Function_Call --
7575 ---------------------------------
7577 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7578 Expr : Node_Id;
7580 begin
7581 -- When a function call appears in Object.Operation format, the
7582 -- original representation has several possible forms depending on
7583 -- the availability and form of actual parameters:
7585 -- Obj.Func N_Selected_Component
7586 -- Obj.Func (Actual) N_Indexed_Component
7587 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7588 -- N_Selected_Component
7590 Expr := Original_Node (N);
7591 loop
7592 if Nkind (Expr) = N_Function_Call then
7593 Expr := Name (Expr);
7595 -- "Obj.Func (Actual)" case
7597 elsif Nkind (Expr) = N_Indexed_Component then
7598 Expr := Prefix (Expr);
7600 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7602 elsif Nkind (Expr) = N_Selected_Component then
7603 Expr := Selector_Name (Expr);
7605 else
7606 exit;
7607 end if;
7608 end loop;
7610 return
7611 Nkind (Expr) in N_Has_Entity
7612 and then Present (Entity (Expr))
7613 and then Ekind (Entity (Expr)) = E_Function
7614 and then Needs_Finalization (Etype (Entity (Expr)));
7615 end Is_Controlled_Function_Call;
7617 ----------------------------
7618 -- Is_Controlled_Indexing --
7619 ----------------------------
7621 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
7622 Expr : constant Node_Id := Original_Node (N);
7624 begin
7625 return
7626 Nkind (Expr) = N_Indexed_Component
7627 and then Present (Generalized_Indexing (Expr))
7628 and then Needs_Finalization (Etype (Expr));
7629 end Is_Controlled_Indexing;
7631 ----------------------
7632 -- Is_Displace_Call --
7633 ----------------------
7635 function Is_Displace_Call (N : Node_Id) return Boolean is
7636 Call : constant Node_Id := Strip (N);
7638 begin
7639 return
7640 Present (Call)
7641 and then Nkind (Call) = N_Function_Call
7642 and then Nkind (Name (Call)) in N_Has_Entity
7643 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7644 end Is_Displace_Call;
7646 ----------------------
7647 -- Is_Source_Object --
7648 ----------------------
7650 function Is_Source_Object (N : Node_Id) return Boolean is
7651 Obj : constant Node_Id := Strip (N);
7653 begin
7654 return
7655 Present (Obj)
7656 and then Comes_From_Source (Obj)
7657 and then Nkind (Obj) in N_Has_Entity
7658 and then Is_Object (Entity (Obj));
7659 end Is_Source_Object;
7661 -----------
7662 -- Strip --
7663 -----------
7665 function Strip (N : Node_Id) return Node_Id is
7666 Result : Node_Id;
7668 begin
7669 Result := N;
7670 loop
7671 if Nkind (Result) = N_Explicit_Dereference then
7672 Result := Prefix (Result);
7674 elsif Nkind_In (Result, N_Type_Conversion,
7675 N_Unchecked_Type_Conversion)
7676 then
7677 Result := Expression (Result);
7679 else
7680 exit;
7681 end if;
7682 end loop;
7684 return Result;
7685 end Strip;
7687 -- Local variables
7689 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
7690 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7691 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
7692 Orig_Expr : Node_Id;
7694 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7696 begin
7697 -- Case 1:
7699 -- Obj : CW_Type := Function_Call (...);
7701 -- is rewritten into:
7703 -- Temp : ... := Function_Call (...)'reference;
7704 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7706 -- where the return type of the function and the class-wide type require
7707 -- dispatch table pointer displacement.
7709 -- Case 2:
7711 -- Obj : CW_Type := Container (...);
7713 -- is rewritten into:
7715 -- Temp : ... := Function_Call (Container, ...)'reference;
7716 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7718 -- where the container element type and the class-wide type require
7719 -- dispatch table pointer dispacement.
7721 -- Case 3:
7723 -- Obj : CW_Type := Src_Obj;
7725 -- is rewritten into:
7727 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7729 -- where the type of the source object and the class-wide type require
7730 -- dispatch table pointer displacement.
7732 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
7733 and then Is_Class_Wide_Type (Obj_Typ)
7734 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7735 and then Nkind (Orig_Decl) = N_Object_Declaration
7736 and then Comes_From_Source (Orig_Decl)
7737 then
7738 Orig_Expr := Expression (Orig_Decl);
7740 return
7741 Is_Controlled_Function_Call (Orig_Expr)
7742 or else Is_Controlled_Indexing (Orig_Expr)
7743 or else Is_Source_Object (Orig_Expr);
7744 end if;
7746 return False;
7747 end Is_Displacement_Of_Object_Or_Function_Result;
7749 ------------------------------
7750 -- Is_Finalizable_Transient --
7751 ------------------------------
7753 function Is_Finalizable_Transient
7754 (Decl : Node_Id;
7755 Rel_Node : Node_Id) return Boolean
7757 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7758 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7760 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7761 -- Determine whether transient object Trans_Id is initialized either
7762 -- by a function call which returns an access type or simply renames
7763 -- another pointer.
7765 function Initialized_By_Aliased_BIP_Func_Call
7766 (Trans_Id : Entity_Id) return Boolean;
7767 -- Determine whether transient object Trans_Id is initialized by a
7768 -- build-in-place function call where the BIPalloc parameter is of
7769 -- value 1 and BIPaccess is not null. This case creates an aliasing
7770 -- between the returned value and the value denoted by BIPaccess.
7772 function Is_Aliased
7773 (Trans_Id : Entity_Id;
7774 First_Stmt : Node_Id) return Boolean;
7775 -- Determine whether transient object Trans_Id has been renamed or
7776 -- aliased through 'reference in the statement list starting from
7777 -- First_Stmt.
7779 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7780 -- Determine whether transient object Trans_Id is allocated on the heap
7782 function Is_Iterated_Container
7783 (Trans_Id : Entity_Id;
7784 First_Stmt : Node_Id) return Boolean;
7785 -- Determine whether transient object Trans_Id denotes a container which
7786 -- is in the process of being iterated in the statement list starting
7787 -- from First_Stmt.
7789 ---------------------------
7790 -- Initialized_By_Access --
7791 ---------------------------
7793 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7794 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7796 begin
7797 return
7798 Present (Expr)
7799 and then Nkind (Expr) /= N_Reference
7800 and then Is_Access_Type (Etype (Expr));
7801 end Initialized_By_Access;
7803 ------------------------------------------
7804 -- Initialized_By_Aliased_BIP_Func_Call --
7805 ------------------------------------------
7807 function Initialized_By_Aliased_BIP_Func_Call
7808 (Trans_Id : Entity_Id) return Boolean
7810 Call : Node_Id := Expression (Parent (Trans_Id));
7812 begin
7813 -- Build-in-place calls usually appear in 'reference format
7815 if Nkind (Call) = N_Reference then
7816 Call := Prefix (Call);
7817 end if;
7819 Call := Unqual_Conv (Call);
7821 if Is_Build_In_Place_Function_Call (Call) then
7822 declare
7823 Access_Nam : Name_Id := No_Name;
7824 Access_OK : Boolean := False;
7825 Actual : Node_Id;
7826 Alloc_Nam : Name_Id := No_Name;
7827 Alloc_OK : Boolean := False;
7828 Formal : Node_Id;
7829 Func_Id : Entity_Id;
7830 Param : Node_Id;
7832 begin
7833 -- Examine all parameter associations of the function call
7835 Param := First (Parameter_Associations (Call));
7836 while Present (Param) loop
7837 if Nkind (Param) = N_Parameter_Association
7838 and then Nkind (Selector_Name (Param)) = N_Identifier
7839 then
7840 Actual := Explicit_Actual_Parameter (Param);
7841 Formal := Selector_Name (Param);
7843 -- Construct the names of formals BIPaccess and BIPalloc
7844 -- using the function name retrieved from an arbitrary
7845 -- formal.
7847 if Access_Nam = No_Name
7848 and then Alloc_Nam = No_Name
7849 and then Present (Entity (Formal))
7850 then
7851 Func_Id := Scope (Entity (Formal));
7853 Access_Nam :=
7854 New_External_Name (Chars (Func_Id),
7855 BIP_Formal_Suffix (BIP_Object_Access));
7857 Alloc_Nam :=
7858 New_External_Name (Chars (Func_Id),
7859 BIP_Formal_Suffix (BIP_Alloc_Form));
7860 end if;
7862 -- A match for BIPaccess => Temp has been found
7864 if Chars (Formal) = Access_Nam
7865 and then Nkind (Actual) /= N_Null
7866 then
7867 Access_OK := True;
7868 end if;
7870 -- A match for BIPalloc => 1 has been found
7872 if Chars (Formal) = Alloc_Nam
7873 and then Nkind (Actual) = N_Integer_Literal
7874 and then Intval (Actual) = Uint_1
7875 then
7876 Alloc_OK := True;
7877 end if;
7878 end if;
7880 Next (Param);
7881 end loop;
7883 return Access_OK and Alloc_OK;
7884 end;
7885 end if;
7887 return False;
7888 end Initialized_By_Aliased_BIP_Func_Call;
7890 ----------------
7891 -- Is_Aliased --
7892 ----------------
7894 function Is_Aliased
7895 (Trans_Id : Entity_Id;
7896 First_Stmt : Node_Id) return Boolean
7898 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7899 -- Given an object renaming declaration, retrieve the entity of the
7900 -- renamed name. Return Empty if the renamed name is anything other
7901 -- than a variable or a constant.
7903 -------------------------
7904 -- Find_Renamed_Object --
7905 -------------------------
7907 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7908 Ren_Obj : Node_Id := Empty;
7910 function Find_Object (N : Node_Id) return Traverse_Result;
7911 -- Try to detect an object which is either a constant or a
7912 -- variable.
7914 -----------------
7915 -- Find_Object --
7916 -----------------
7918 function Find_Object (N : Node_Id) return Traverse_Result is
7919 begin
7920 -- Stop the search once a constant or a variable has been
7921 -- detected.
7923 if Nkind (N) = N_Identifier
7924 and then Present (Entity (N))
7925 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7926 then
7927 Ren_Obj := Entity (N);
7928 return Abandon;
7929 end if;
7931 return OK;
7932 end Find_Object;
7934 procedure Search is new Traverse_Proc (Find_Object);
7936 -- Local variables
7938 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7940 -- Start of processing for Find_Renamed_Object
7942 begin
7943 -- Actions related to dispatching calls may appear as renamings of
7944 -- tags. Do not process this type of renaming because it does not
7945 -- use the actual value of the object.
7947 if not Is_RTE (Typ, RE_Tag_Ptr) then
7948 Search (Name (Ren_Decl));
7949 end if;
7951 return Ren_Obj;
7952 end Find_Renamed_Object;
7954 -- Local variables
7956 Expr : Node_Id;
7957 Ren_Obj : Entity_Id;
7958 Stmt : Node_Id;
7960 -- Start of processing for Is_Aliased
7962 begin
7963 -- A controlled transient object is not considered aliased when it
7964 -- appears inside an expression_with_actions node even when there are
7965 -- explicit aliases of it:
7967 -- do
7968 -- Trans_Id : Ctrl_Typ ...; -- transient object
7969 -- Alias : ... := Trans_Id; -- object is aliased
7970 -- Val : constant Boolean :=
7971 -- ... Alias ...; -- aliasing ends
7972 -- <finalize Trans_Id> -- object safe to finalize
7973 -- in Val end;
7975 -- Expansion ensures that all aliases are encapsulated in the actions
7976 -- list and do not leak to the expression by forcing the evaluation
7977 -- of the expression.
7979 if Nkind (Rel_Node) = N_Expression_With_Actions then
7980 return False;
7982 -- Otherwise examine the statements after the controlled transient
7983 -- object and look for various forms of aliasing.
7985 else
7986 Stmt := First_Stmt;
7987 while Present (Stmt) loop
7988 if Nkind (Stmt) = N_Object_Declaration then
7989 Expr := Expression (Stmt);
7991 -- Aliasing of the form:
7992 -- Obj : ... := Trans_Id'reference;
7994 if Present (Expr)
7995 and then Nkind (Expr) = N_Reference
7996 and then Nkind (Prefix (Expr)) = N_Identifier
7997 and then Entity (Prefix (Expr)) = Trans_Id
7998 then
7999 return True;
8000 end if;
8002 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8003 Ren_Obj := Find_Renamed_Object (Stmt);
8005 -- Aliasing of the form:
8006 -- Obj : ... renames ... Trans_Id ...;
8008 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8009 return True;
8010 end if;
8011 end if;
8013 Next (Stmt);
8014 end loop;
8016 return False;
8017 end if;
8018 end Is_Aliased;
8020 ------------------
8021 -- Is_Allocated --
8022 ------------------
8024 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8025 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8026 begin
8027 return
8028 Is_Access_Type (Etype (Trans_Id))
8029 and then Present (Expr)
8030 and then Nkind (Expr) = N_Allocator;
8031 end Is_Allocated;
8033 ---------------------------
8034 -- Is_Iterated_Container --
8035 ---------------------------
8037 function Is_Iterated_Container
8038 (Trans_Id : Entity_Id;
8039 First_Stmt : Node_Id) return Boolean
8041 Aspect : Node_Id;
8042 Call : Node_Id;
8043 Iter : Entity_Id;
8044 Param : Node_Id;
8045 Stmt : Node_Id;
8046 Typ : Entity_Id;
8048 begin
8049 -- It is not possible to iterate over containers in non-Ada 2012 code
8051 if Ada_Version < Ada_2012 then
8052 return False;
8053 end if;
8055 Typ := Etype (Trans_Id);
8057 -- Handle access type created for secondary stack use
8059 if Is_Access_Type (Typ) then
8060 Typ := Designated_Type (Typ);
8061 end if;
8063 -- Look for aspect Default_Iterator. It may be part of a type
8064 -- declaration for a container, or inherited from a base type
8065 -- or parent type.
8067 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8069 if Present (Aspect) then
8070 Iter := Entity (Aspect);
8072 -- Examine the statements following the container object and
8073 -- look for a call to the default iterate routine where the
8074 -- first parameter is the transient. Such a call appears as:
8076 -- It : Access_To_CW_Iterator :=
8077 -- Iterate (Tran_Id.all, ...)'reference;
8079 Stmt := First_Stmt;
8080 while Present (Stmt) loop
8082 -- Detect an object declaration which is initialized by a
8083 -- secondary stack function call.
8085 if Nkind (Stmt) = N_Object_Declaration
8086 and then Present (Expression (Stmt))
8087 and then Nkind (Expression (Stmt)) = N_Reference
8088 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8089 then
8090 Call := Prefix (Expression (Stmt));
8092 -- The call must invoke the default iterate routine of
8093 -- the container and the transient object must appear as
8094 -- the first actual parameter. Skip any calls whose names
8095 -- are not entities.
8097 if Is_Entity_Name (Name (Call))
8098 and then Entity (Name (Call)) = Iter
8099 and then Present (Parameter_Associations (Call))
8100 then
8101 Param := First (Parameter_Associations (Call));
8103 if Nkind (Param) = N_Explicit_Dereference
8104 and then Entity (Prefix (Param)) = Trans_Id
8105 then
8106 return True;
8107 end if;
8108 end if;
8109 end if;
8111 Next (Stmt);
8112 end loop;
8113 end if;
8115 return False;
8116 end Is_Iterated_Container;
8118 -- Local variables
8120 Desig : Entity_Id := Obj_Typ;
8122 -- Start of processing for Is_Finalizable_Transient
8124 begin
8125 -- Handle access types
8127 if Is_Access_Type (Desig) then
8128 Desig := Available_View (Designated_Type (Desig));
8129 end if;
8131 return
8132 Ekind_In (Obj_Id, E_Constant, E_Variable)
8133 and then Needs_Finalization (Desig)
8134 and then Requires_Transient_Scope (Desig)
8135 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8137 -- Do not consider a transient object that was already processed
8139 and then not Is_Finalized_Transient (Obj_Id)
8141 -- Do not consider renamed or 'reference-d transient objects because
8142 -- the act of renaming extends the object's lifetime.
8144 and then not Is_Aliased (Obj_Id, Decl)
8146 -- Do not consider transient objects allocated on the heap since
8147 -- they are attached to a finalization master.
8149 and then not Is_Allocated (Obj_Id)
8151 -- If the transient object is a pointer, check that it is not
8152 -- initialized by a function that returns a pointer or acts as a
8153 -- renaming of another pointer.
8155 and then
8156 (not Is_Access_Type (Obj_Typ)
8157 or else not Initialized_By_Access (Obj_Id))
8159 -- Do not consider transient objects which act as indirect aliases
8160 -- of build-in-place function results.
8162 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8164 -- Do not consider conversions of tags to class-wide types
8166 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8168 -- Do not consider iterators because those are treated as normal
8169 -- controlled objects and are processed by the usual finalization
8170 -- machinery. This avoids the double finalization of an iterator.
8172 and then not Is_Iterator (Desig)
8174 -- Do not consider containers in the context of iterator loops. Such
8175 -- transient objects must exist for as long as the loop is around,
8176 -- otherwise any operation carried out by the iterator will fail.
8178 and then not Is_Iterated_Container (Obj_Id, Decl);
8179 end Is_Finalizable_Transient;
8181 ---------------------------------
8182 -- Is_Fully_Repped_Tagged_Type --
8183 ---------------------------------
8185 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8186 U : constant Entity_Id := Underlying_Type (T);
8187 Comp : Entity_Id;
8189 begin
8190 if No (U) or else not Is_Tagged_Type (U) then
8191 return False;
8192 elsif Has_Discriminants (U) then
8193 return False;
8194 elsif not Has_Specified_Layout (U) then
8195 return False;
8196 end if;
8198 -- Here we have a tagged type, see if it has any unlayed out fields
8199 -- other than a possible tag and parent fields. If so, we return False.
8201 Comp := First_Component (U);
8202 while Present (Comp) loop
8203 if not Is_Tag (Comp)
8204 and then Chars (Comp) /= Name_uParent
8205 and then No (Component_Clause (Comp))
8206 then
8207 return False;
8208 else
8209 Next_Component (Comp);
8210 end if;
8211 end loop;
8213 -- All components are layed out
8215 return True;
8216 end Is_Fully_Repped_Tagged_Type;
8218 ----------------------------------
8219 -- Is_Library_Level_Tagged_Type --
8220 ----------------------------------
8222 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8223 begin
8224 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8225 end Is_Library_Level_Tagged_Type;
8227 --------------------------
8228 -- Is_Non_BIP_Func_Call --
8229 --------------------------
8231 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8232 begin
8233 -- The expected call is of the format
8235 -- Func_Call'reference
8237 return
8238 Nkind (Expr) = N_Reference
8239 and then Nkind (Prefix (Expr)) = N_Function_Call
8240 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8241 end Is_Non_BIP_Func_Call;
8243 ----------------------------------
8244 -- Is_Possibly_Unaligned_Object --
8245 ----------------------------------
8247 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8248 T : constant Entity_Id := Etype (N);
8250 begin
8251 -- If renamed object, apply test to underlying object
8253 if Is_Entity_Name (N)
8254 and then Is_Object (Entity (N))
8255 and then Present (Renamed_Object (Entity (N)))
8256 then
8257 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8258 end if;
8260 -- Tagged and controlled types and aliased types are always aligned, as
8261 -- are concurrent types.
8263 if Is_Aliased (T)
8264 or else Has_Controlled_Component (T)
8265 or else Is_Concurrent_Type (T)
8266 or else Is_Tagged_Type (T)
8267 or else Is_Controlled (T)
8268 then
8269 return False;
8270 end if;
8272 -- If this is an element of a packed array, may be unaligned
8274 if Is_Ref_To_Bit_Packed_Array (N) then
8275 return True;
8276 end if;
8278 -- Case of indexed component reference: test whether prefix is unaligned
8280 if Nkind (N) = N_Indexed_Component then
8281 return Is_Possibly_Unaligned_Object (Prefix (N));
8283 -- Case of selected component reference
8285 elsif Nkind (N) = N_Selected_Component then
8286 declare
8287 P : constant Node_Id := Prefix (N);
8288 C : constant Entity_Id := Entity (Selector_Name (N));
8289 M : Nat;
8290 S : Nat;
8292 begin
8293 -- If component reference is for an array with non-static bounds,
8294 -- then it is always aligned: we can only process unaligned arrays
8295 -- with static bounds (more precisely compile time known bounds).
8297 if Is_Array_Type (T)
8298 and then not Compile_Time_Known_Bounds (T)
8299 then
8300 return False;
8301 end if;
8303 -- If component is aliased, it is definitely properly aligned
8305 if Is_Aliased (C) then
8306 return False;
8307 end if;
8309 -- If component is for a type implemented as a scalar, and the
8310 -- record is packed, and the component is other than the first
8311 -- component of the record, then the component may be unaligned.
8313 if Is_Packed (Etype (P))
8314 and then Represented_As_Scalar (Etype (C))
8315 and then First_Entity (Scope (C)) /= C
8316 then
8317 return True;
8318 end if;
8320 -- Compute maximum possible alignment for T
8322 -- If alignment is known, then that settles things
8324 if Known_Alignment (T) then
8325 M := UI_To_Int (Alignment (T));
8327 -- If alignment is not known, tentatively set max alignment
8329 else
8330 M := Ttypes.Maximum_Alignment;
8332 -- We can reduce this if the Esize is known since the default
8333 -- alignment will never be more than the smallest power of 2
8334 -- that does not exceed this Esize value.
8336 if Known_Esize (T) then
8337 S := UI_To_Int (Esize (T));
8339 while (M / 2) >= S loop
8340 M := M / 2;
8341 end loop;
8342 end if;
8343 end if;
8345 -- The following code is historical, it used to be present but it
8346 -- is too cautious, because the front-end does not know the proper
8347 -- default alignments for the target. Also, if the alignment is
8348 -- not known, the front end can't know in any case. If a copy is
8349 -- needed, the back-end will take care of it. This whole section
8350 -- including this comment can be removed later ???
8352 -- If the component reference is for a record that has a specified
8353 -- alignment, and we either know it is too small, or cannot tell,
8354 -- then the component may be unaligned.
8356 -- What is the following commented out code ???
8358 -- if Known_Alignment (Etype (P))
8359 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8360 -- and then M > Alignment (Etype (P))
8361 -- then
8362 -- return True;
8363 -- end if;
8365 -- Case of component clause present which may specify an
8366 -- unaligned position.
8368 if Present (Component_Clause (C)) then
8370 -- Otherwise we can do a test to make sure that the actual
8371 -- start position in the record, and the length, are both
8372 -- consistent with the required alignment. If not, we know
8373 -- that we are unaligned.
8375 declare
8376 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8377 begin
8378 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
8379 or else Esize (C) mod Align_In_Bits /= 0
8380 then
8381 return True;
8382 end if;
8383 end;
8384 end if;
8386 -- Otherwise, for a component reference, test prefix
8388 return Is_Possibly_Unaligned_Object (P);
8389 end;
8391 -- If not a component reference, must be aligned
8393 else
8394 return False;
8395 end if;
8396 end Is_Possibly_Unaligned_Object;
8398 ---------------------------------
8399 -- Is_Possibly_Unaligned_Slice --
8400 ---------------------------------
8402 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8403 begin
8404 -- Go to renamed object
8406 if Is_Entity_Name (N)
8407 and then Is_Object (Entity (N))
8408 and then Present (Renamed_Object (Entity (N)))
8409 then
8410 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8411 end if;
8413 -- The reference must be a slice
8415 if Nkind (N) /= N_Slice then
8416 return False;
8417 end if;
8419 -- We only need to worry if the target has strict alignment
8421 if not Target_Strict_Alignment then
8422 return False;
8423 end if;
8425 -- If it is a slice, then look at the array type being sliced
8427 declare
8428 Sarr : constant Node_Id := Prefix (N);
8429 -- Prefix of the slice, i.e. the array being sliced
8431 Styp : constant Entity_Id := Etype (Prefix (N));
8432 -- Type of the array being sliced
8434 Pref : Node_Id;
8435 Ptyp : Entity_Id;
8437 begin
8438 -- The problems arise if the array object that is being sliced
8439 -- is a component of a record or array, and we cannot guarantee
8440 -- the alignment of the array within its containing object.
8442 -- To investigate this, we look at successive prefixes to see
8443 -- if we have a worrisome indexed or selected component.
8445 Pref := Sarr;
8446 loop
8447 -- Case of array is part of an indexed component reference
8449 if Nkind (Pref) = N_Indexed_Component then
8450 Ptyp := Etype (Prefix (Pref));
8452 -- The only problematic case is when the array is packed, in
8453 -- which case we really know nothing about the alignment of
8454 -- individual components.
8456 if Is_Bit_Packed_Array (Ptyp) then
8457 return True;
8458 end if;
8460 -- Case of array is part of a selected component reference
8462 elsif Nkind (Pref) = N_Selected_Component then
8463 Ptyp := Etype (Prefix (Pref));
8465 -- We are definitely in trouble if the record in question
8466 -- has an alignment, and either we know this alignment is
8467 -- inconsistent with the alignment of the slice, or we don't
8468 -- know what the alignment of the slice should be.
8470 if Known_Alignment (Ptyp)
8471 and then (Unknown_Alignment (Styp)
8472 or else Alignment (Styp) > Alignment (Ptyp))
8473 then
8474 return True;
8475 end if;
8477 -- We are in potential trouble if the record type is packed.
8478 -- We could special case when we know that the array is the
8479 -- first component, but that's not such a simple case ???
8481 if Is_Packed (Ptyp) then
8482 return True;
8483 end if;
8485 -- We are in trouble if there is a component clause, and
8486 -- either we do not know the alignment of the slice, or
8487 -- the alignment of the slice is inconsistent with the
8488 -- bit position specified by the component clause.
8490 declare
8491 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8492 begin
8493 if Present (Component_Clause (Field))
8494 and then
8495 (Unknown_Alignment (Styp)
8496 or else
8497 (Component_Bit_Offset (Field) mod
8498 (System_Storage_Unit * Alignment (Styp))) /= 0)
8499 then
8500 return True;
8501 end if;
8502 end;
8504 -- For cases other than selected or indexed components we know we
8505 -- are OK, since no issues arise over alignment.
8507 else
8508 return False;
8509 end if;
8511 -- We processed an indexed component or selected component
8512 -- reference that looked safe, so keep checking prefixes.
8514 Pref := Prefix (Pref);
8515 end loop;
8516 end;
8517 end Is_Possibly_Unaligned_Slice;
8519 -------------------------------
8520 -- Is_Related_To_Func_Return --
8521 -------------------------------
8523 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8524 Expr : constant Node_Id := Related_Expression (Id);
8525 begin
8526 return
8527 Present (Expr)
8528 and then Nkind (Expr) = N_Explicit_Dereference
8529 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8530 end Is_Related_To_Func_Return;
8532 --------------------------------
8533 -- Is_Ref_To_Bit_Packed_Array --
8534 --------------------------------
8536 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8537 Result : Boolean;
8538 Expr : Node_Id;
8540 begin
8541 if Is_Entity_Name (N)
8542 and then Is_Object (Entity (N))
8543 and then Present (Renamed_Object (Entity (N)))
8544 then
8545 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8546 end if;
8548 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8549 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8550 Result := True;
8551 else
8552 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8553 end if;
8555 if Result and then Nkind (N) = N_Indexed_Component then
8556 Expr := First (Expressions (N));
8557 while Present (Expr) loop
8558 Force_Evaluation (Expr);
8559 Next (Expr);
8560 end loop;
8561 end if;
8563 return Result;
8565 else
8566 return False;
8567 end if;
8568 end Is_Ref_To_Bit_Packed_Array;
8570 --------------------------------
8571 -- Is_Ref_To_Bit_Packed_Slice --
8572 --------------------------------
8574 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8575 begin
8576 if Nkind (N) = N_Type_Conversion then
8577 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8579 elsif Is_Entity_Name (N)
8580 and then Is_Object (Entity (N))
8581 and then Present (Renamed_Object (Entity (N)))
8582 then
8583 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8585 elsif Nkind (N) = N_Slice
8586 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8587 then
8588 return True;
8590 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8591 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8593 else
8594 return False;
8595 end if;
8596 end Is_Ref_To_Bit_Packed_Slice;
8598 -----------------------
8599 -- Is_Renamed_Object --
8600 -----------------------
8602 function Is_Renamed_Object (N : Node_Id) return Boolean is
8603 Pnod : constant Node_Id := Parent (N);
8604 Kind : constant Node_Kind := Nkind (Pnod);
8605 begin
8606 if Kind = N_Object_Renaming_Declaration then
8607 return True;
8608 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8609 return Is_Renamed_Object (Pnod);
8610 else
8611 return False;
8612 end if;
8613 end Is_Renamed_Object;
8615 --------------------------------------
8616 -- Is_Secondary_Stack_BIP_Func_Call --
8617 --------------------------------------
8619 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8620 Alloc_Nam : Name_Id := No_Name;
8621 Actual : Node_Id;
8622 Call : Node_Id := Expr;
8623 Formal : Node_Id;
8624 Param : Node_Id;
8626 begin
8627 -- Build-in-place calls usually appear in 'reference format. Note that
8628 -- the accessibility check machinery may add an extra 'reference due to
8629 -- side effect removal.
8631 while Nkind (Call) = N_Reference loop
8632 Call := Prefix (Call);
8633 end loop;
8635 Call := Unqual_Conv (Call);
8637 if Is_Build_In_Place_Function_Call (Call) then
8639 -- Examine all parameter associations of the function call
8641 Param := First (Parameter_Associations (Call));
8642 while Present (Param) loop
8643 if Nkind (Param) = N_Parameter_Association then
8644 Formal := Selector_Name (Param);
8645 Actual := Explicit_Actual_Parameter (Param);
8647 -- Construct the name of formal BIPalloc. It is much easier to
8648 -- extract the name of the function using an arbitrary formal's
8649 -- scope rather than the Name field of Call.
8651 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8652 Alloc_Nam :=
8653 New_External_Name
8654 (Chars (Scope (Entity (Formal))),
8655 BIP_Formal_Suffix (BIP_Alloc_Form));
8656 end if;
8658 -- A match for BIPalloc => 2 has been found
8660 if Chars (Formal) = Alloc_Nam
8661 and then Nkind (Actual) = N_Integer_Literal
8662 and then Intval (Actual) = Uint_2
8663 then
8664 return True;
8665 end if;
8666 end if;
8668 Next (Param);
8669 end loop;
8670 end if;
8672 return False;
8673 end Is_Secondary_Stack_BIP_Func_Call;
8675 -------------------------------------
8676 -- Is_Tag_To_Class_Wide_Conversion --
8677 -------------------------------------
8679 function Is_Tag_To_Class_Wide_Conversion
8680 (Obj_Id : Entity_Id) return Boolean
8682 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8684 begin
8685 return
8686 Is_Class_Wide_Type (Etype (Obj_Id))
8687 and then Present (Expr)
8688 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8689 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8690 end Is_Tag_To_Class_Wide_Conversion;
8692 ----------------------------
8693 -- Is_Untagged_Derivation --
8694 ----------------------------
8696 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8697 begin
8698 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8699 or else
8700 (Is_Private_Type (T) and then Present (Full_View (T))
8701 and then not Is_Tagged_Type (Full_View (T))
8702 and then Is_Derived_Type (Full_View (T))
8703 and then Etype (Full_View (T)) /= T);
8704 end Is_Untagged_Derivation;
8706 ------------------------------------
8707 -- Is_Untagged_Private_Derivation --
8708 ------------------------------------
8710 function Is_Untagged_Private_Derivation
8711 (Priv_Typ : Entity_Id;
8712 Full_Typ : Entity_Id) return Boolean
8714 begin
8715 return
8716 Present (Priv_Typ)
8717 and then Is_Untagged_Derivation (Priv_Typ)
8718 and then Is_Private_Type (Etype (Priv_Typ))
8719 and then Present (Full_Typ)
8720 and then Is_Itype (Full_Typ);
8721 end Is_Untagged_Private_Derivation;
8723 ------------------------------
8724 -- Is_Verifiable_DIC_Pragma --
8725 ------------------------------
8727 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
8728 Args : constant List_Id := Pragma_Argument_Associations (Prag);
8730 begin
8731 -- To qualify as verifiable, a DIC pragma must have a non-null argument
8733 return
8734 Present (Args)
8735 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
8736 end Is_Verifiable_DIC_Pragma;
8738 ---------------------------
8739 -- Is_Volatile_Reference --
8740 ---------------------------
8742 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8743 begin
8744 -- Only source references are to be treated as volatile, internally
8745 -- generated stuff cannot have volatile external effects.
8747 if not Comes_From_Source (N) then
8748 return False;
8750 -- Never true for reference to a type
8752 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8753 return False;
8755 -- Never true for a compile time known constant
8757 elsif Compile_Time_Known_Value (N) then
8758 return False;
8760 -- True if object reference with volatile type
8762 elsif Is_Volatile_Object (N) then
8763 return True;
8765 -- True if reference to volatile entity
8767 elsif Is_Entity_Name (N) then
8768 return Treat_As_Volatile (Entity (N));
8770 -- True for slice of volatile array
8772 elsif Nkind (N) = N_Slice then
8773 return Is_Volatile_Reference (Prefix (N));
8775 -- True if volatile component
8777 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8778 if (Is_Entity_Name (Prefix (N))
8779 and then Has_Volatile_Components (Entity (Prefix (N))))
8780 or else (Present (Etype (Prefix (N)))
8781 and then Has_Volatile_Components (Etype (Prefix (N))))
8782 then
8783 return True;
8784 else
8785 return Is_Volatile_Reference (Prefix (N));
8786 end if;
8788 -- Otherwise false
8790 else
8791 return False;
8792 end if;
8793 end Is_Volatile_Reference;
8795 --------------------
8796 -- Kill_Dead_Code --
8797 --------------------
8799 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8800 W : Boolean := Warn;
8801 -- Set False if warnings suppressed
8803 begin
8804 if Present (N) then
8805 Remove_Warning_Messages (N);
8807 -- Update the internal structures of the ABE mechanism in case the
8808 -- dead node is an elaboration scenario.
8810 Kill_Elaboration_Scenario (N);
8812 -- Generate warning if appropriate
8814 if W then
8816 -- We suppress the warning if this code is under control of an
8817 -- if statement, whose condition is a simple identifier, and
8818 -- either we are in an instance, or warnings off is set for this
8819 -- identifier. The reason for killing it in the instance case is
8820 -- that it is common and reasonable for code to be deleted in
8821 -- instances for various reasons.
8823 -- Could we use Is_Statically_Unevaluated here???
8825 if Nkind (Parent (N)) = N_If_Statement then
8826 declare
8827 C : constant Node_Id := Condition (Parent (N));
8828 begin
8829 if Nkind (C) = N_Identifier
8830 and then
8831 (In_Instance
8832 or else (Present (Entity (C))
8833 and then Has_Warnings_Off (Entity (C))))
8834 then
8835 W := False;
8836 end if;
8837 end;
8838 end if;
8840 -- Generate warning if not suppressed
8842 if W then
8843 Error_Msg_F
8844 ("?t?this code can never be executed and has been deleted!",
8846 end if;
8847 end if;
8849 -- Recurse into block statements and bodies to process declarations
8850 -- and statements.
8852 if Nkind (N) = N_Block_Statement
8853 or else Nkind (N) = N_Subprogram_Body
8854 or else Nkind (N) = N_Package_Body
8855 then
8856 Kill_Dead_Code (Declarations (N), False);
8857 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8859 if Nkind (N) = N_Subprogram_Body then
8860 Set_Is_Eliminated (Defining_Entity (N));
8861 end if;
8863 elsif Nkind (N) = N_Package_Declaration then
8864 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8865 Kill_Dead_Code (Private_Declarations (Specification (N)));
8867 -- ??? After this point, Delete_Tree has been called on all
8868 -- declarations in Specification (N), so references to entities
8869 -- therein look suspicious.
8871 declare
8872 E : Entity_Id := First_Entity (Defining_Entity (N));
8874 begin
8875 while Present (E) loop
8876 if Ekind (E) = E_Operator then
8877 Set_Is_Eliminated (E);
8878 end if;
8880 Next_Entity (E);
8881 end loop;
8882 end;
8884 -- Recurse into composite statement to kill individual statements in
8885 -- particular instantiations.
8887 elsif Nkind (N) = N_If_Statement then
8888 Kill_Dead_Code (Then_Statements (N));
8889 Kill_Dead_Code (Elsif_Parts (N));
8890 Kill_Dead_Code (Else_Statements (N));
8892 elsif Nkind (N) = N_Loop_Statement then
8893 Kill_Dead_Code (Statements (N));
8895 elsif Nkind (N) = N_Case_Statement then
8896 declare
8897 Alt : Node_Id;
8898 begin
8899 Alt := First (Alternatives (N));
8900 while Present (Alt) loop
8901 Kill_Dead_Code (Statements (Alt));
8902 Next (Alt);
8903 end loop;
8904 end;
8906 elsif Nkind (N) = N_Case_Statement_Alternative then
8907 Kill_Dead_Code (Statements (N));
8909 -- Deal with dead instances caused by deleting instantiations
8911 elsif Nkind (N) in N_Generic_Instantiation then
8912 Remove_Dead_Instance (N);
8913 end if;
8914 end if;
8915 end Kill_Dead_Code;
8917 -- Case where argument is a list of nodes to be killed
8919 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8920 N : Node_Id;
8921 W : Boolean;
8923 begin
8924 W := Warn;
8926 if Is_Non_Empty_List (L) then
8927 N := First (L);
8928 while Present (N) loop
8929 Kill_Dead_Code (N, W);
8930 W := False;
8931 Next (N);
8932 end loop;
8933 end if;
8934 end Kill_Dead_Code;
8936 ------------------------
8937 -- Known_Non_Negative --
8938 ------------------------
8940 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8941 begin
8942 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8943 return True;
8945 else
8946 declare
8947 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8948 begin
8949 return
8950 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8951 end;
8952 end if;
8953 end Known_Non_Negative;
8955 -----------------------------
8956 -- Make_CW_Equivalent_Type --
8957 -----------------------------
8959 -- Create a record type used as an equivalent of any member of the class
8960 -- which takes its size from exp.
8962 -- Generate the following code:
8964 -- type Equiv_T is record
8965 -- _parent : T (List of discriminant constraints taken from Exp);
8966 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8967 -- end Equiv_T;
8969 -- ??? Note that this type does not guarantee same alignment as all
8970 -- derived types
8972 function Make_CW_Equivalent_Type
8973 (T : Entity_Id;
8974 E : Node_Id) return Entity_Id
8976 Loc : constant Source_Ptr := Sloc (E);
8977 Root_Typ : constant Entity_Id := Root_Type (T);
8978 List_Def : constant List_Id := Empty_List;
8979 Comp_List : constant List_Id := New_List;
8980 Equiv_Type : Entity_Id;
8981 Range_Type : Entity_Id;
8982 Str_Type : Entity_Id;
8983 Constr_Root : Entity_Id;
8984 Sizexpr : Node_Id;
8986 begin
8987 -- If the root type is already constrained, there are no discriminants
8988 -- in the expression.
8990 if not Has_Discriminants (Root_Typ)
8991 or else Is_Constrained (Root_Typ)
8992 then
8993 Constr_Root := Root_Typ;
8995 -- At this point in the expansion, non-limited view of the type
8996 -- must be available, otherwise the error will be reported later.
8998 if From_Limited_With (Constr_Root)
8999 and then Present (Non_Limited_View (Constr_Root))
9000 then
9001 Constr_Root := Non_Limited_View (Constr_Root);
9002 end if;
9004 else
9005 Constr_Root := Make_Temporary (Loc, 'R');
9007 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9009 Append_To (List_Def,
9010 Make_Subtype_Declaration (Loc,
9011 Defining_Identifier => Constr_Root,
9012 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9013 end if;
9015 -- Generate the range subtype declaration
9017 Range_Type := Make_Temporary (Loc, 'G');
9019 if not Is_Interface (Root_Typ) then
9021 -- subtype rg__xx is
9022 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9024 Sizexpr :=
9025 Make_Op_Subtract (Loc,
9026 Left_Opnd =>
9027 Make_Attribute_Reference (Loc,
9028 Prefix =>
9029 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9030 Attribute_Name => Name_Size),
9031 Right_Opnd =>
9032 Make_Attribute_Reference (Loc,
9033 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9034 Attribute_Name => Name_Object_Size));
9035 else
9036 -- subtype rg__xx is
9037 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9039 Sizexpr :=
9040 Make_Attribute_Reference (Loc,
9041 Prefix =>
9042 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9043 Attribute_Name => Name_Size);
9044 end if;
9046 Set_Paren_Count (Sizexpr, 1);
9048 Append_To (List_Def,
9049 Make_Subtype_Declaration (Loc,
9050 Defining_Identifier => Range_Type,
9051 Subtype_Indication =>
9052 Make_Subtype_Indication (Loc,
9053 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9054 Constraint => Make_Range_Constraint (Loc,
9055 Range_Expression =>
9056 Make_Range (Loc,
9057 Low_Bound => Make_Integer_Literal (Loc, 1),
9058 High_Bound =>
9059 Make_Op_Divide (Loc,
9060 Left_Opnd => Sizexpr,
9061 Right_Opnd => Make_Integer_Literal (Loc,
9062 Intval => System_Storage_Unit)))))));
9064 -- subtype str__nn is Storage_Array (rg__x);
9066 Str_Type := Make_Temporary (Loc, 'S');
9067 Append_To (List_Def,
9068 Make_Subtype_Declaration (Loc,
9069 Defining_Identifier => Str_Type,
9070 Subtype_Indication =>
9071 Make_Subtype_Indication (Loc,
9072 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9073 Constraint =>
9074 Make_Index_Or_Discriminant_Constraint (Loc,
9075 Constraints =>
9076 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9078 -- type Equiv_T is record
9079 -- [ _parent : Tnn; ]
9080 -- E : Str_Type;
9081 -- end Equiv_T;
9083 Equiv_Type := Make_Temporary (Loc, 'T');
9084 Set_Ekind (Equiv_Type, E_Record_Type);
9085 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9087 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9088 -- treatment for this type. In particular, even though _parent's type
9089 -- is a controlled type or contains controlled components, we do not
9090 -- want to set Has_Controlled_Component on it to avoid making it gain
9091 -- an unwanted _controller component.
9093 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9095 -- A class-wide equivalent type does not require initialization
9097 Set_Suppress_Initialization (Equiv_Type);
9099 if not Is_Interface (Root_Typ) then
9100 Append_To (Comp_List,
9101 Make_Component_Declaration (Loc,
9102 Defining_Identifier =>
9103 Make_Defining_Identifier (Loc, Name_uParent),
9104 Component_Definition =>
9105 Make_Component_Definition (Loc,
9106 Aliased_Present => False,
9107 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9108 end if;
9110 Append_To (Comp_List,
9111 Make_Component_Declaration (Loc,
9112 Defining_Identifier => Make_Temporary (Loc, 'C'),
9113 Component_Definition =>
9114 Make_Component_Definition (Loc,
9115 Aliased_Present => False,
9116 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9118 Append_To (List_Def,
9119 Make_Full_Type_Declaration (Loc,
9120 Defining_Identifier => Equiv_Type,
9121 Type_Definition =>
9122 Make_Record_Definition (Loc,
9123 Component_List =>
9124 Make_Component_List (Loc,
9125 Component_Items => Comp_List,
9126 Variant_Part => Empty))));
9128 -- Suppress all checks during the analysis of the expanded code to avoid
9129 -- the generation of spurious warnings under ZFP run-time.
9131 Insert_Actions (E, List_Def, Suppress => All_Checks);
9132 return Equiv_Type;
9133 end Make_CW_Equivalent_Type;
9135 -------------------------
9136 -- Make_Invariant_Call --
9137 -------------------------
9139 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9140 Loc : constant Source_Ptr := Sloc (Expr);
9141 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9143 Proc_Id : Entity_Id;
9145 begin
9146 pragma Assert (Has_Invariants (Typ));
9148 Proc_Id := Invariant_Procedure (Typ);
9149 pragma Assert (Present (Proc_Id));
9151 return
9152 Make_Procedure_Call_Statement (Loc,
9153 Name => New_Occurrence_Of (Proc_Id, Loc),
9154 Parameter_Associations => New_List (Relocate_Node (Expr)));
9155 end Make_Invariant_Call;
9157 ------------------------
9158 -- Make_Literal_Range --
9159 ------------------------
9161 function Make_Literal_Range
9162 (Loc : Source_Ptr;
9163 Literal_Typ : Entity_Id) return Node_Id
9165 Lo : constant Node_Id :=
9166 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9167 Index : constant Entity_Id := Etype (Lo);
9168 Length_Expr : constant Node_Id :=
9169 Make_Op_Subtract (Loc,
9170 Left_Opnd =>
9171 Make_Integer_Literal (Loc,
9172 Intval => String_Literal_Length (Literal_Typ)),
9173 Right_Opnd => Make_Integer_Literal (Loc, 1));
9175 Hi : Node_Id;
9177 begin
9178 Set_Analyzed (Lo, False);
9180 if Is_Integer_Type (Index) then
9181 Hi :=
9182 Make_Op_Add (Loc,
9183 Left_Opnd => New_Copy_Tree (Lo),
9184 Right_Opnd => Length_Expr);
9185 else
9186 Hi :=
9187 Make_Attribute_Reference (Loc,
9188 Attribute_Name => Name_Val,
9189 Prefix => New_Occurrence_Of (Index, Loc),
9190 Expressions => New_List (
9191 Make_Op_Add (Loc,
9192 Left_Opnd =>
9193 Make_Attribute_Reference (Loc,
9194 Attribute_Name => Name_Pos,
9195 Prefix => New_Occurrence_Of (Index, Loc),
9196 Expressions => New_List (New_Copy_Tree (Lo))),
9197 Right_Opnd => Length_Expr)));
9198 end if;
9200 return
9201 Make_Range (Loc,
9202 Low_Bound => Lo,
9203 High_Bound => Hi);
9204 end Make_Literal_Range;
9206 --------------------------
9207 -- Make_Non_Empty_Check --
9208 --------------------------
9210 function Make_Non_Empty_Check
9211 (Loc : Source_Ptr;
9212 N : Node_Id) return Node_Id
9214 begin
9215 return
9216 Make_Op_Ne (Loc,
9217 Left_Opnd =>
9218 Make_Attribute_Reference (Loc,
9219 Attribute_Name => Name_Length,
9220 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9221 Right_Opnd =>
9222 Make_Integer_Literal (Loc, 0));
9223 end Make_Non_Empty_Check;
9225 -------------------------
9226 -- Make_Predicate_Call --
9227 -------------------------
9229 -- WARNING: This routine manages Ghost regions. Return statements must be
9230 -- replaced by gotos which jump to the end of the routine and restore the
9231 -- Ghost mode.
9233 function Make_Predicate_Call
9234 (Typ : Entity_Id;
9235 Expr : Node_Id;
9236 Mem : Boolean := False) return Node_Id
9238 Loc : constant Source_Ptr := Sloc (Expr);
9240 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9241 -- Save the Ghost mode to restore on exit
9243 Call : Node_Id;
9244 Func_Id : Entity_Id;
9246 begin
9247 pragma Assert (Present (Predicate_Function (Typ)));
9249 -- The related type may be subject to pragma Ghost. Set the mode now to
9250 -- ensure that the call is properly marked as Ghost.
9252 Set_Ghost_Mode (Typ);
9254 -- Call special membership version if requested and available
9256 if Mem and then Present (Predicate_Function_M (Typ)) then
9257 Func_Id := Predicate_Function_M (Typ);
9258 else
9259 Func_Id := Predicate_Function (Typ);
9260 end if;
9262 -- Case of calling normal predicate function
9264 -- If the type is tagged, the expression may be class-wide, in which
9265 -- case it has to be converted to its root type, given that the
9266 -- generated predicate function is not dispatching.
9268 if Is_Tagged_Type (Typ) then
9269 Call :=
9270 Make_Function_Call (Loc,
9271 Name => New_Occurrence_Of (Func_Id, Loc),
9272 Parameter_Associations =>
9273 New_List (Convert_To (Typ, Relocate_Node (Expr))));
9274 else
9275 Call :=
9276 Make_Function_Call (Loc,
9277 Name => New_Occurrence_Of (Func_Id, Loc),
9278 Parameter_Associations => New_List (Relocate_Node (Expr)));
9279 end if;
9281 Restore_Ghost_Mode (Saved_GM);
9283 return Call;
9284 end Make_Predicate_Call;
9286 --------------------------
9287 -- Make_Predicate_Check --
9288 --------------------------
9290 function Make_Predicate_Check
9291 (Typ : Entity_Id;
9292 Expr : Node_Id) return Node_Id
9294 procedure Replace_Subtype_Reference (N : Node_Id);
9295 -- Replace current occurrences of the subtype to which a dynamic
9296 -- predicate applies, by the expression that triggers a predicate
9297 -- check. This is needed for aspect Predicate_Failure, for which
9298 -- we do not generate a wrapper procedure, but simply modify the
9299 -- expression for the pragma of the predicate check.
9301 --------------------------------
9302 -- Replace_Subtype_Reference --
9303 --------------------------------
9305 procedure Replace_Subtype_Reference (N : Node_Id) is
9306 begin
9307 Rewrite (N, New_Copy_Tree (Expr));
9309 -- We want to treat the node as if it comes from source, so
9310 -- that ASIS will not ignore it.
9312 Set_Comes_From_Source (N, True);
9313 end Replace_Subtype_Reference;
9315 procedure Replace_Subtype_References is
9316 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9318 -- Local variables
9320 Loc : constant Source_Ptr := Sloc (Expr);
9321 Arg_List : List_Id;
9322 Fail_Expr : Node_Id;
9323 Nam : Name_Id;
9325 -- Start of processing for Make_Predicate_Check
9327 begin
9328 -- If predicate checks are suppressed, then return a null statement. For
9329 -- this call, we check only the scope setting. If the caller wants to
9330 -- check a specific entity's setting, they must do it manually.
9332 if Predicate_Checks_Suppressed (Empty) then
9333 return Make_Null_Statement (Loc);
9334 end if;
9336 -- Do not generate a check within an internal subprogram (stream
9337 -- functions and the like, including including predicate functions).
9339 if Within_Internal_Subprogram then
9340 return Make_Null_Statement (Loc);
9341 end if;
9343 -- Compute proper name to use, we need to get this right so that the
9344 -- right set of check policies apply to the Check pragma we are making.
9346 if Has_Dynamic_Predicate_Aspect (Typ) then
9347 Nam := Name_Dynamic_Predicate;
9348 elsif Has_Static_Predicate_Aspect (Typ) then
9349 Nam := Name_Static_Predicate;
9350 else
9351 Nam := Name_Predicate;
9352 end if;
9354 Arg_List := New_List (
9355 Make_Pragma_Argument_Association (Loc,
9356 Expression => Make_Identifier (Loc, Nam)),
9357 Make_Pragma_Argument_Association (Loc,
9358 Expression => Make_Predicate_Call (Typ, Expr)));
9360 -- If subtype has Predicate_Failure defined, add the correponding
9361 -- expression as an additional pragma parameter, after replacing
9362 -- current instances with the expression being checked.
9364 if Has_Aspect (Typ, Aspect_Predicate_Failure) then
9365 Fail_Expr :=
9366 New_Copy_Tree
9367 (Expression (Find_Aspect (Typ, Aspect_Predicate_Failure)));
9368 Replace_Subtype_References (Fail_Expr, Typ);
9370 Append_To (Arg_List,
9371 Make_Pragma_Argument_Association (Loc,
9372 Expression => Fail_Expr));
9373 end if;
9375 return
9376 Make_Pragma (Loc,
9377 Chars => Name_Check,
9378 Pragma_Argument_Associations => Arg_List);
9379 end Make_Predicate_Check;
9381 ----------------------------
9382 -- Make_Subtype_From_Expr --
9383 ----------------------------
9385 -- 1. If Expr is an unconstrained array expression, creates
9386 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9388 -- 2. If Expr is a unconstrained discriminated type expression, creates
9389 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9391 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9393 function Make_Subtype_From_Expr
9394 (E : Node_Id;
9395 Unc_Typ : Entity_Id;
9396 Related_Id : Entity_Id := Empty) return Node_Id
9398 List_Constr : constant List_Id := New_List;
9399 Loc : constant Source_Ptr := Sloc (E);
9400 D : Entity_Id;
9401 Full_Exp : Node_Id;
9402 Full_Subtyp : Entity_Id;
9403 High_Bound : Entity_Id;
9404 Index_Typ : Entity_Id;
9405 Low_Bound : Entity_Id;
9406 Priv_Subtyp : Entity_Id;
9407 Utyp : Entity_Id;
9409 begin
9410 if Is_Private_Type (Unc_Typ)
9411 and then Has_Unknown_Discriminants (Unc_Typ)
9412 then
9413 -- The caller requests a unique external name for both the private
9414 -- and the full subtype.
9416 if Present (Related_Id) then
9417 Full_Subtyp :=
9418 Make_Defining_Identifier (Loc,
9419 Chars => New_External_Name (Chars (Related_Id), 'C'));
9420 Priv_Subtyp :=
9421 Make_Defining_Identifier (Loc,
9422 Chars => New_External_Name (Chars (Related_Id), 'P'));
9424 else
9425 Full_Subtyp := Make_Temporary (Loc, 'C');
9426 Priv_Subtyp := Make_Temporary (Loc, 'P');
9427 end if;
9429 -- Prepare the subtype completion. Use the base type to find the
9430 -- underlying type because the type may be a generic actual or an
9431 -- explicit subtype.
9433 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9435 Full_Exp :=
9436 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9437 Set_Parent (Full_Exp, Parent (E));
9439 Insert_Action (E,
9440 Make_Subtype_Declaration (Loc,
9441 Defining_Identifier => Full_Subtyp,
9442 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9444 -- Define the dummy private subtype
9446 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9447 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9448 Set_Scope (Priv_Subtyp, Full_Subtyp);
9449 Set_Is_Constrained (Priv_Subtyp);
9450 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9451 Set_Is_Itype (Priv_Subtyp);
9452 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9454 if Is_Tagged_Type (Priv_Subtyp) then
9455 Set_Class_Wide_Type
9456 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9457 Set_Direct_Primitive_Operations (Priv_Subtyp,
9458 Direct_Primitive_Operations (Unc_Typ));
9459 end if;
9461 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9463 return New_Occurrence_Of (Priv_Subtyp, Loc);
9465 elsif Is_Array_Type (Unc_Typ) then
9466 Index_Typ := First_Index (Unc_Typ);
9467 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9469 -- Capture the bounds of each index constraint in case the context
9470 -- is an object declaration of an unconstrained type initialized
9471 -- by a function call:
9473 -- Obj : Unconstr_Typ := Func_Call;
9475 -- This scenario requires secondary scope management and the index
9476 -- constraint cannot depend on the temporary used to capture the
9477 -- result of the function call.
9479 -- SS_Mark;
9480 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9481 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9482 -- Obj : S := Temp.all;
9483 -- SS_Release; -- Temp is gone at this point, bounds of S are
9484 -- -- non existent.
9486 -- Generate:
9487 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9489 Low_Bound := Make_Temporary (Loc, 'B');
9490 Insert_Action (E,
9491 Make_Object_Declaration (Loc,
9492 Defining_Identifier => Low_Bound,
9493 Object_Definition =>
9494 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9495 Constant_Present => True,
9496 Expression =>
9497 Make_Attribute_Reference (Loc,
9498 Prefix => Duplicate_Subexpr_No_Checks (E),
9499 Attribute_Name => Name_First,
9500 Expressions => New_List (
9501 Make_Integer_Literal (Loc, J)))));
9503 -- Generate:
9504 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9506 High_Bound := Make_Temporary (Loc, 'B');
9507 Insert_Action (E,
9508 Make_Object_Declaration (Loc,
9509 Defining_Identifier => High_Bound,
9510 Object_Definition =>
9511 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9512 Constant_Present => True,
9513 Expression =>
9514 Make_Attribute_Reference (Loc,
9515 Prefix => Duplicate_Subexpr_No_Checks (E),
9516 Attribute_Name => Name_Last,
9517 Expressions => New_List (
9518 Make_Integer_Literal (Loc, J)))));
9520 Append_To (List_Constr,
9521 Make_Range (Loc,
9522 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9523 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9525 Index_Typ := Next_Index (Index_Typ);
9526 end loop;
9528 elsif Is_Class_Wide_Type (Unc_Typ) then
9529 declare
9530 CW_Subtype : Entity_Id;
9531 EQ_Typ : Entity_Id := Empty;
9533 begin
9534 -- A class-wide equivalent type is not needed on VM targets
9535 -- because the VM back-ends handle the class-wide object
9536 -- initialization itself (and doesn't need or want the
9537 -- additional intermediate type to handle the assignment).
9539 if Expander_Active and then Tagged_Type_Expansion then
9541 -- If this is the class-wide type of a completion that is a
9542 -- record subtype, set the type of the class-wide type to be
9543 -- the full base type, for use in the expanded code for the
9544 -- equivalent type. Should this be done earlier when the
9545 -- completion is analyzed ???
9547 if Is_Private_Type (Etype (Unc_Typ))
9548 and then
9549 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9550 then
9551 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9552 end if;
9554 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9555 end if;
9557 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9558 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9559 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9561 return New_Occurrence_Of (CW_Subtype, Loc);
9562 end;
9564 -- Indefinite record type with discriminants
9566 else
9567 D := First_Discriminant (Unc_Typ);
9568 while Present (D) loop
9569 Append_To (List_Constr,
9570 Make_Selected_Component (Loc,
9571 Prefix => Duplicate_Subexpr_No_Checks (E),
9572 Selector_Name => New_Occurrence_Of (D, Loc)));
9574 Next_Discriminant (D);
9575 end loop;
9576 end if;
9578 return
9579 Make_Subtype_Indication (Loc,
9580 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9581 Constraint =>
9582 Make_Index_Or_Discriminant_Constraint (Loc,
9583 Constraints => List_Constr));
9584 end Make_Subtype_From_Expr;
9586 ---------------
9587 -- Map_Types --
9588 ---------------
9590 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9592 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9593 -- avoid deep indentation of code.
9595 -- NOTE: Routines which deal with discriminant mapping operate on the
9596 -- [underlying/record] full view of various types because those views
9597 -- contain all discriminants and stored constraints.
9599 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9600 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9601 -- overriding chain starting from Prim whose dispatching type is parent
9602 -- type Par_Typ and add a mapping between the result and primitive Prim.
9604 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9605 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9606 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9607 -- if no such primitive is available.
9609 function Build_Chain
9610 (Par_Typ : Entity_Id;
9611 Deriv_Typ : Entity_Id) return Elist_Id;
9612 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9613 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9614 -- list has the form:
9616 -- head tail
9617 -- v v
9618 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9620 -- Note that Par_Typ is not part of the resulting derivation chain
9622 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9623 -- Return the view of type Typ which could potentially contains either
9624 -- the discriminants or stored constraints of the type.
9626 function Find_Discriminant_Value
9627 (Discr : Entity_Id;
9628 Par_Typ : Entity_Id;
9629 Deriv_Typ : Entity_Id;
9630 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9631 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9632 -- in the derivation chain starting from parent type Par_Typ leading to
9633 -- derived type Deriv_Typ. The returned value is one of the following:
9635 -- * An entity which is either a discriminant or a non-discriminant
9636 -- name, and renames/constraints Discr.
9638 -- * An expression which constraints Discr
9640 -- Typ_Elmt is an element of the derivation chain created by routine
9641 -- Build_Chain and denotes the current ancestor being examined.
9643 procedure Map_Discriminants
9644 (Par_Typ : Entity_Id;
9645 Deriv_Typ : Entity_Id);
9646 -- Map each discriminant of type Par_Typ to a meaningful constraint
9647 -- from the point of view of type Deriv_Typ.
9649 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9650 -- Map each primitive of type Par_Typ to a corresponding primitive of
9651 -- type Deriv_Typ.
9653 -------------------
9654 -- Add_Primitive --
9655 -------------------
9657 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9658 Par_Prim : Entity_Id;
9660 begin
9661 -- Inspect the inheritance chain through the Alias attribute and the
9662 -- overriding chain through the Overridden_Operation looking for an
9663 -- ancestor primitive with the appropriate dispatching type.
9665 Par_Prim := Prim;
9666 while Present (Par_Prim) loop
9667 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9668 Par_Prim := Ancestor_Primitive (Par_Prim);
9669 end loop;
9671 -- Create a mapping of the form:
9673 -- parent type primitive -> derived type primitive
9675 if Present (Par_Prim) then
9676 Type_Map.Set (Par_Prim, Prim);
9677 end if;
9678 end Add_Primitive;
9680 ------------------------
9681 -- Ancestor_Primitive --
9682 ------------------------
9684 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9685 Inher_Prim : constant Entity_Id := Alias (Subp);
9686 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9688 begin
9689 -- The current subprogram overrides an ancestor primitive
9691 if Present (Over_Prim) then
9692 return Over_Prim;
9694 -- The current subprogram is an internally generated alias of an
9695 -- inherited ancestor primitive.
9697 elsif Present (Inher_Prim) then
9698 return Inher_Prim;
9700 -- Otherwise the current subprogram is the root of the inheritance or
9701 -- overriding chain.
9703 else
9704 return Empty;
9705 end if;
9706 end Ancestor_Primitive;
9708 -----------------
9709 -- Build_Chain --
9710 -----------------
9712 function Build_Chain
9713 (Par_Typ : Entity_Id;
9714 Deriv_Typ : Entity_Id) return Elist_Id
9716 Anc_Typ : Entity_Id;
9717 Chain : Elist_Id;
9718 Curr_Typ : Entity_Id;
9720 begin
9721 Chain := New_Elmt_List;
9723 -- Add the derived type to the derivation chain
9725 Prepend_Elmt (Deriv_Typ, Chain);
9727 -- Examine all ancestors starting from the derived type climbing
9728 -- towards parent type Par_Typ.
9730 Curr_Typ := Deriv_Typ;
9731 loop
9732 -- Handle the case where the current type is a record which
9733 -- derives from a subtype.
9735 -- subtype Sub_Typ is Par_Typ ...
9736 -- type Deriv_Typ is Sub_Typ ...
9738 if Ekind (Curr_Typ) = E_Record_Type
9739 and then Present (Parent_Subtype (Curr_Typ))
9740 then
9741 Anc_Typ := Parent_Subtype (Curr_Typ);
9743 -- Handle the case where the current type is a record subtype of
9744 -- another subtype.
9746 -- subtype Sub_Typ1 is Par_Typ ...
9747 -- subtype Sub_Typ2 is Sub_Typ1 ...
9749 elsif Ekind (Curr_Typ) = E_Record_Subtype
9750 and then Present (Cloned_Subtype (Curr_Typ))
9751 then
9752 Anc_Typ := Cloned_Subtype (Curr_Typ);
9754 -- Otherwise use the direct parent type
9756 else
9757 Anc_Typ := Etype (Curr_Typ);
9758 end if;
9760 -- Use the first subtype when dealing with itypes
9762 if Is_Itype (Anc_Typ) then
9763 Anc_Typ := First_Subtype (Anc_Typ);
9764 end if;
9766 -- Work with the view which contains the discriminants and stored
9767 -- constraints.
9769 Anc_Typ := Discriminated_View (Anc_Typ);
9771 -- Stop the climb when either the parent type has been reached or
9772 -- there are no more ancestors left to examine.
9774 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9776 Prepend_Unique_Elmt (Anc_Typ, Chain);
9777 Curr_Typ := Anc_Typ;
9778 end loop;
9780 return Chain;
9781 end Build_Chain;
9783 ------------------------
9784 -- Discriminated_View --
9785 ------------------------
9787 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9788 T : Entity_Id;
9790 begin
9791 T := Typ;
9793 -- Use the [underlying] full view when dealing with private types
9794 -- because the view contains all inherited discriminants or stored
9795 -- constraints.
9797 if Is_Private_Type (T) then
9798 if Present (Underlying_Full_View (T)) then
9799 T := Underlying_Full_View (T);
9801 elsif Present (Full_View (T)) then
9802 T := Full_View (T);
9803 end if;
9804 end if;
9806 -- Use the underlying record view when the type is an extenstion of
9807 -- a parent type with unknown discriminants because the view contains
9808 -- all inherited discriminants or stored constraints.
9810 if Ekind (T) = E_Record_Type
9811 and then Present (Underlying_Record_View (T))
9812 then
9813 T := Underlying_Record_View (T);
9814 end if;
9816 return T;
9817 end Discriminated_View;
9819 -----------------------------
9820 -- Find_Discriminant_Value --
9821 -----------------------------
9823 function Find_Discriminant_Value
9824 (Discr : Entity_Id;
9825 Par_Typ : Entity_Id;
9826 Deriv_Typ : Entity_Id;
9827 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9829 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9830 Typ : constant Entity_Id := Node (Typ_Elmt);
9832 function Find_Constraint_Value
9833 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
9834 -- Given constraint Constr, find what it denotes. This is either:
9836 -- * An entity which is either a discriminant or a name
9838 -- * An expression
9840 ---------------------------
9841 -- Find_Constraint_Value --
9842 ---------------------------
9844 function Find_Constraint_Value
9845 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
9847 begin
9848 if Nkind (Constr) in N_Entity then
9850 -- The constraint denotes a discriminant of the curren type
9851 -- which renames the ancestor discriminant:
9853 -- vv
9854 -- type Typ (D1 : ...; DN : ...) is
9855 -- new Anc (Discr => D1) with ...
9856 -- ^^
9858 if Ekind (Constr) = E_Discriminant then
9860 -- The discriminant belongs to derived type Deriv_Typ. This
9861 -- is the final value for the ancestor discriminant as the
9862 -- derivations chain has been fully exhausted.
9864 if Typ = Deriv_Typ then
9865 return Constr;
9867 -- Otherwise the discriminant may be renamed or constrained
9868 -- at a lower level. Continue looking down the derivation
9869 -- chain.
9871 else
9872 return
9873 Find_Discriminant_Value
9874 (Discr => Constr,
9875 Par_Typ => Par_Typ,
9876 Deriv_Typ => Deriv_Typ,
9877 Typ_Elmt => Next_Elmt (Typ_Elmt));
9878 end if;
9880 -- Otherwise the constraint denotes a reference to some name
9881 -- which results in a Girder discriminant:
9883 -- vvvv
9884 -- Name : ...;
9885 -- type Typ (D1 : ...; DN : ...) is
9886 -- new Anc (Discr => Name) with ...
9887 -- ^^^^
9889 -- Return the name as this is the proper constraint of the
9890 -- discriminant.
9892 else
9893 return Constr;
9894 end if;
9896 -- The constraint denotes a reference to a name
9898 elsif Is_Entity_Name (Constr) then
9899 return Find_Constraint_Value (Entity (Constr));
9901 -- Otherwise the current constraint is an expression which yields
9902 -- a Girder discriminant:
9904 -- type Typ (D1 : ...; DN : ...) is
9905 -- new Anc (Discr => <expression>) with ...
9906 -- ^^^^^^^^^^
9908 -- Return the expression as this is the proper constraint of the
9909 -- discriminant.
9911 else
9912 return Constr;
9913 end if;
9914 end Find_Constraint_Value;
9916 -- Local variables
9918 Constrs : constant Elist_Id := Stored_Constraint (Typ);
9920 Constr_Elmt : Elmt_Id;
9921 Pos : Uint;
9922 Typ_Discr : Entity_Id;
9924 -- Start of processing for Find_Discriminant_Value
9926 begin
9927 -- The algorithm for finding the value of a discriminant works as
9928 -- follows. First, it recreates the derivation chain from Par_Typ
9929 -- to Deriv_Typ as a list:
9931 -- Par_Typ (shown for completeness)
9932 -- v
9933 -- Ancestor_N <-- head of chain
9934 -- v
9935 -- Ancestor_1
9936 -- v
9937 -- Deriv_Typ <-- tail of chain
9939 -- The algorithm then traces the fate of a parent discriminant down
9940 -- the derivation chain. At each derivation level, the discriminant
9941 -- may be either inherited or constrained.
9943 -- 1) Discriminant is inherited: there are two cases, depending on
9944 -- which type is inheriting.
9946 -- 1.1) Deriv_Typ is inheriting:
9948 -- type Ancestor (D_1 : ...) is tagged ...
9949 -- type Deriv_Typ is new Ancestor ...
9951 -- In this case the inherited discriminant is the final value of
9952 -- the parent discriminant because the end of the derivation chain
9953 -- has been reached.
9955 -- 1.2) Some other type is inheriting:
9957 -- type Ancestor_1 (D_1 : ...) is tagged ...
9958 -- type Ancestor_2 is new Ancestor_1 ...
9960 -- In this case the algorithm continues to trace the fate of the
9961 -- inherited discriminant down the derivation chain because it may
9962 -- be further inherited or constrained.
9964 -- 2) Discriminant is constrained: there are three cases, depending
9965 -- on what the constraint is.
9967 -- 2.1) The constraint is another discriminant (aka renaming):
9969 -- type Ancestor_1 (D_1 : ...) is tagged ...
9970 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
9972 -- In this case the constraining discriminant becomes the one to
9973 -- track down the derivation chain. The algorithm already knows
9974 -- that D_2 constrains D_1, therefore if the algorithm finds the
9975 -- value of D_2, then this would also be the value for D_1.
9977 -- 2.2) The constraint is a name (aka Girder):
9979 -- Name : ...
9980 -- type Ancestor_1 (D_1 : ...) is tagged ...
9981 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
9983 -- In this case the name is the final value of D_1 because the
9984 -- discriminant cannot be further constrained.
9986 -- 2.3) The constraint is an expression (aka Girder):
9988 -- type Ancestor_1 (D_1 : ...) is tagged ...
9989 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
9991 -- Similar to 2.2, the expression is the final value of D_1
9993 Pos := Uint_1;
9995 -- When a derived type constrains its parent type, all constaints
9996 -- appear in the Stored_Constraint list. Examine the list looking
9997 -- for a positional match.
9999 if Present (Constrs) then
10000 Constr_Elmt := First_Elmt (Constrs);
10001 while Present (Constr_Elmt) loop
10003 -- The position of the current constraint matches that of the
10004 -- ancestor discriminant.
10006 if Pos = Discr_Pos then
10007 return Find_Constraint_Value (Node (Constr_Elmt));
10008 end if;
10010 Next_Elmt (Constr_Elmt);
10011 Pos := Pos + 1;
10012 end loop;
10014 -- Otherwise the derived type does not constraint its parent type in
10015 -- which case it inherits the parent discriminants.
10017 else
10018 Typ_Discr := First_Discriminant (Typ);
10019 while Present (Typ_Discr) loop
10021 -- The position of the current discriminant matches that of the
10022 -- ancestor discriminant.
10024 if Pos = Discr_Pos then
10025 return Find_Constraint_Value (Typ_Discr);
10026 end if;
10028 Next_Discriminant (Typ_Discr);
10029 Pos := Pos + 1;
10030 end loop;
10031 end if;
10033 -- A discriminant must always have a corresponding value. This is
10034 -- either another discriminant, a name, or an expression. If this
10035 -- point is reached, them most likely the derivation chain employs
10036 -- the wrong views of types.
10038 pragma Assert (False);
10040 return Empty;
10041 end Find_Discriminant_Value;
10043 -----------------------
10044 -- Map_Discriminants --
10045 -----------------------
10047 procedure Map_Discriminants
10048 (Par_Typ : Entity_Id;
10049 Deriv_Typ : Entity_Id)
10051 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10053 Discr : Entity_Id;
10054 Discr_Val : Node_Or_Entity_Id;
10056 begin
10057 -- Examine each discriminant of parent type Par_Typ and find a
10058 -- suitable value for it from the point of view of derived type
10059 -- Deriv_Typ.
10061 if Has_Discriminants (Par_Typ) then
10062 Discr := First_Discriminant (Par_Typ);
10063 while Present (Discr) loop
10064 Discr_Val :=
10065 Find_Discriminant_Value
10066 (Discr => Discr,
10067 Par_Typ => Par_Typ,
10068 Deriv_Typ => Deriv_Typ,
10069 Typ_Elmt => First_Elmt (Deriv_Chain));
10071 -- Create a mapping of the form:
10073 -- parent type discriminant -> value
10075 Type_Map.Set (Discr, Discr_Val);
10077 Next_Discriminant (Discr);
10078 end loop;
10079 end if;
10080 end Map_Discriminants;
10082 --------------------
10083 -- Map_Primitives --
10084 --------------------
10086 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10087 Deriv_Prim : Entity_Id;
10088 Par_Prim : Entity_Id;
10089 Par_Prims : Elist_Id;
10090 Prim_Elmt : Elmt_Id;
10092 begin
10093 -- Inspect the primitives of the derived type and determine whether
10094 -- they relate to the primitives of the parent type. If there is a
10095 -- meaningful relation, create a mapping of the form:
10097 -- parent type primitive -> perived type primitive
10099 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10100 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10101 while Present (Prim_Elmt) loop
10102 Deriv_Prim := Node (Prim_Elmt);
10104 if Is_Subprogram (Deriv_Prim)
10105 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10106 then
10107 Add_Primitive (Deriv_Prim, Par_Typ);
10108 end if;
10110 Next_Elmt (Prim_Elmt);
10111 end loop;
10112 end if;
10114 -- If the parent operation is an interface operation, the overriding
10115 -- indicator is not present. Instead, we get from the interface
10116 -- operation the primitive of the current type that implements it.
10118 if Is_Interface (Par_Typ) then
10119 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10121 if Present (Par_Prims) then
10122 Prim_Elmt := First_Elmt (Par_Prims);
10124 while Present (Prim_Elmt) loop
10125 Par_Prim := Node (Prim_Elmt);
10126 Deriv_Prim :=
10127 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10129 if Present (Deriv_Prim) then
10130 Type_Map.Set (Par_Prim, Deriv_Prim);
10131 end if;
10133 Next_Elmt (Prim_Elmt);
10134 end loop;
10135 end if;
10136 end if;
10137 end Map_Primitives;
10139 -- Start of processing for Map_Types
10141 begin
10142 -- Nothing to do if there are no types to work with
10144 if No (Parent_Type) or else No (Derived_Type) then
10145 return;
10147 -- Nothing to do if the mapping already exists
10149 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10150 return;
10152 -- Nothing to do if both types are not tagged. Note that untagged types
10153 -- do not have primitive operations and their discriminants are already
10154 -- handled by gigi.
10156 elsif not Is_Tagged_Type (Parent_Type)
10157 or else not Is_Tagged_Type (Derived_Type)
10158 then
10159 return;
10160 end if;
10162 -- Create a mapping of the form
10164 -- parent type -> derived type
10166 -- to prevent any subsequent attempts to produce the same relations
10168 Type_Map.Set (Parent_Type, Derived_Type);
10170 -- Create mappings of the form
10172 -- parent type discriminant -> derived type discriminant
10173 -- <or>
10174 -- parent type discriminant -> constraint
10176 -- Note that mapping of discriminants breaks privacy because it needs to
10177 -- work with those views which contains the discriminants and any stored
10178 -- constraints.
10180 Map_Discriminants
10181 (Par_Typ => Discriminated_View (Parent_Type),
10182 Deriv_Typ => Discriminated_View (Derived_Type));
10184 -- Create mappings of the form
10186 -- parent type primitive -> derived type primitive
10188 Map_Primitives
10189 (Par_Typ => Parent_Type,
10190 Deriv_Typ => Derived_Type);
10191 end Map_Types;
10193 ----------------------------
10194 -- Matching_Standard_Type --
10195 ----------------------------
10197 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10198 pragma Assert (Is_Scalar_Type (Typ));
10199 Siz : constant Uint := Esize (Typ);
10201 begin
10202 -- Floating-point cases
10204 if Is_Floating_Point_Type (Typ) then
10205 if Siz <= Esize (Standard_Short_Float) then
10206 return Standard_Short_Float;
10207 elsif Siz <= Esize (Standard_Float) then
10208 return Standard_Float;
10209 elsif Siz <= Esize (Standard_Long_Float) then
10210 return Standard_Long_Float;
10211 elsif Siz <= Esize (Standard_Long_Long_Float) then
10212 return Standard_Long_Long_Float;
10213 else
10214 raise Program_Error;
10215 end if;
10217 -- Integer cases (includes fixed-point types)
10219 -- Unsigned integer cases (includes normal enumeration types)
10221 elsif Is_Unsigned_Type (Typ) then
10222 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10223 return Standard_Short_Short_Unsigned;
10224 elsif Siz <= Esize (Standard_Short_Unsigned) then
10225 return Standard_Short_Unsigned;
10226 elsif Siz <= Esize (Standard_Unsigned) then
10227 return Standard_Unsigned;
10228 elsif Siz <= Esize (Standard_Long_Unsigned) then
10229 return Standard_Long_Unsigned;
10230 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10231 return Standard_Long_Long_Unsigned;
10232 else
10233 raise Program_Error;
10234 end if;
10236 -- Signed integer cases
10238 else
10239 if Siz <= Esize (Standard_Short_Short_Integer) then
10240 return Standard_Short_Short_Integer;
10241 elsif Siz <= Esize (Standard_Short_Integer) then
10242 return Standard_Short_Integer;
10243 elsif Siz <= Esize (Standard_Integer) then
10244 return Standard_Integer;
10245 elsif Siz <= Esize (Standard_Long_Integer) then
10246 return Standard_Long_Integer;
10247 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10248 return Standard_Long_Long_Integer;
10249 else
10250 raise Program_Error;
10251 end if;
10252 end if;
10253 end Matching_Standard_Type;
10255 -----------------------------
10256 -- May_Generate_Large_Temp --
10257 -----------------------------
10259 -- At the current time, the only types that we return False for (i.e. where
10260 -- we decide we know they cannot generate large temps) are ones where we
10261 -- know the size is 256 bits or less at compile time, and we are still not
10262 -- doing a thorough job on arrays and records ???
10264 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10265 begin
10266 if not Size_Known_At_Compile_Time (Typ) then
10267 return False;
10269 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10270 return False;
10272 elsif Is_Array_Type (Typ)
10273 and then Present (Packed_Array_Impl_Type (Typ))
10274 then
10275 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10277 -- We could do more here to find other small types ???
10279 else
10280 return True;
10281 end if;
10282 end May_Generate_Large_Temp;
10284 ------------------------
10285 -- Needs_Finalization --
10286 ------------------------
10288 function Needs_Finalization (Typ : Entity_Id) return Boolean is
10289 function Has_Some_Controlled_Component
10290 (Input_Typ : Entity_Id) return Boolean;
10291 -- Determine whether type Input_Typ has at least one controlled
10292 -- component.
10294 -----------------------------------
10295 -- Has_Some_Controlled_Component --
10296 -----------------------------------
10298 function Has_Some_Controlled_Component
10299 (Input_Typ : Entity_Id) return Boolean
10301 Comp : Entity_Id;
10303 begin
10304 -- When a type is already frozen and has at least one controlled
10305 -- component, or is manually decorated, it is sufficient to inspect
10306 -- flag Has_Controlled_Component.
10308 if Has_Controlled_Component (Input_Typ) then
10309 return True;
10311 -- Otherwise inspect the internals of the type
10313 elsif not Is_Frozen (Input_Typ) then
10314 if Is_Array_Type (Input_Typ) then
10315 return Needs_Finalization (Component_Type (Input_Typ));
10317 elsif Is_Record_Type (Input_Typ) then
10318 Comp := First_Component (Input_Typ);
10319 while Present (Comp) loop
10320 if Needs_Finalization (Etype (Comp)) then
10321 return True;
10322 end if;
10324 Next_Component (Comp);
10325 end loop;
10326 end if;
10327 end if;
10329 return False;
10330 end Has_Some_Controlled_Component;
10332 -- Start of processing for Needs_Finalization
10334 begin
10335 -- Certain run-time configurations and targets do not provide support
10336 -- for controlled types.
10338 if Restriction_Active (No_Finalization) then
10339 return False;
10341 -- C++ types are not considered controlled. It is assumed that the non-
10342 -- Ada side will handle their clean up.
10344 elsif Convention (Typ) = Convention_CPP then
10345 return False;
10347 -- Class-wide types are treated as controlled because derivations from
10348 -- the root type may introduce controlled components.
10350 elsif Is_Class_Wide_Type (Typ) then
10351 return True;
10353 -- Concurrent types are controlled as long as their corresponding record
10354 -- is controlled.
10356 elsif Is_Concurrent_Type (Typ)
10357 and then Present (Corresponding_Record_Type (Typ))
10358 and then Needs_Finalization (Corresponding_Record_Type (Typ))
10359 then
10360 return True;
10362 -- Otherwise the type is controlled when it is either derived from type
10363 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10364 -- contains at least one controlled component.
10366 else
10367 return
10368 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
10369 end if;
10370 end Needs_Finalization;
10372 ----------------------------
10373 -- Needs_Constant_Address --
10374 ----------------------------
10376 function Needs_Constant_Address
10377 (Decl : Node_Id;
10378 Typ : Entity_Id) return Boolean
10380 begin
10381 -- If we have no initialization of any kind, then we don't need to place
10382 -- any restrictions on the address clause, because the object will be
10383 -- elaborated after the address clause is evaluated. This happens if the
10384 -- declaration has no initial expression, or the type has no implicit
10385 -- initialization, or the object is imported.
10387 -- The same holds for all initialized scalar types and all access types.
10388 -- Packed bit arrays of size up to 64 are represented using a modular
10389 -- type with an initialization (to zero) and can be processed like other
10390 -- initialized scalar types.
10392 -- If the type is controlled, code to attach the object to a
10393 -- finalization chain is generated at the point of declaration, and
10394 -- therefore the elaboration of the object cannot be delayed: the
10395 -- address expression must be a constant.
10397 if No (Expression (Decl))
10398 and then not Needs_Finalization (Typ)
10399 and then
10400 (not Has_Non_Null_Base_Init_Proc (Typ)
10401 or else Is_Imported (Defining_Identifier (Decl)))
10402 then
10403 return False;
10405 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10406 or else Is_Access_Type (Typ)
10407 or else
10408 (Is_Bit_Packed_Array (Typ)
10409 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10410 then
10411 return False;
10413 else
10415 -- Otherwise, we require the address clause to be constant because
10416 -- the call to the initialization procedure (or the attach code) has
10417 -- to happen at the point of the declaration.
10419 -- Actually the IP call has been moved to the freeze actions anyway,
10420 -- so maybe we can relax this restriction???
10422 return True;
10423 end if;
10424 end Needs_Constant_Address;
10426 ----------------------------
10427 -- New_Class_Wide_Subtype --
10428 ----------------------------
10430 function New_Class_Wide_Subtype
10431 (CW_Typ : Entity_Id;
10432 N : Node_Id) return Entity_Id
10434 Res : constant Entity_Id := Create_Itype (E_Void, N);
10435 Res_Name : constant Name_Id := Chars (Res);
10436 Res_Scope : constant Entity_Id := Scope (Res);
10438 begin
10439 Copy_Node (CW_Typ, Res);
10440 Set_Comes_From_Source (Res, False);
10441 Set_Sloc (Res, Sloc (N));
10442 Set_Is_Itype (Res);
10443 Set_Associated_Node_For_Itype (Res, N);
10444 Set_Is_Public (Res, False); -- By default, may be changed below.
10445 Set_Public_Status (Res);
10446 Set_Chars (Res, Res_Name);
10447 Set_Scope (Res, Res_Scope);
10448 Set_Ekind (Res, E_Class_Wide_Subtype);
10449 Set_Next_Entity (Res, Empty);
10450 Set_Etype (Res, Base_Type (CW_Typ));
10451 Set_Is_Frozen (Res, False);
10452 Set_Freeze_Node (Res, Empty);
10453 return (Res);
10454 end New_Class_Wide_Subtype;
10456 --------------------------------
10457 -- Non_Limited_Designated_Type --
10458 ---------------------------------
10460 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10461 Desig : constant Entity_Id := Designated_Type (T);
10462 begin
10463 if Has_Non_Limited_View (Desig) then
10464 return Non_Limited_View (Desig);
10465 else
10466 return Desig;
10467 end if;
10468 end Non_Limited_Designated_Type;
10470 -----------------------------------
10471 -- OK_To_Do_Constant_Replacement --
10472 -----------------------------------
10474 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10475 ES : constant Entity_Id := Scope (E);
10476 CS : Entity_Id;
10478 begin
10479 -- Do not replace statically allocated objects, because they may be
10480 -- modified outside the current scope.
10482 if Is_Statically_Allocated (E) then
10483 return False;
10485 -- Do not replace aliased or volatile objects, since we don't know what
10486 -- else might change the value.
10488 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10489 return False;
10491 -- Debug flag -gnatdM disconnects this optimization
10493 elsif Debug_Flag_MM then
10494 return False;
10496 -- Otherwise check scopes
10498 else
10499 CS := Current_Scope;
10501 loop
10502 -- If we are in right scope, replacement is safe
10504 if CS = ES then
10505 return True;
10507 -- Packages do not affect the determination of safety
10509 elsif Ekind (CS) = E_Package then
10510 exit when CS = Standard_Standard;
10511 CS := Scope (CS);
10513 -- Blocks do not affect the determination of safety
10515 elsif Ekind (CS) = E_Block then
10516 CS := Scope (CS);
10518 -- Loops do not affect the determination of safety. Note that we
10519 -- kill all current values on entry to a loop, so we are just
10520 -- talking about processing within a loop here.
10522 elsif Ekind (CS) = E_Loop then
10523 CS := Scope (CS);
10525 -- Otherwise, the reference is dubious, and we cannot be sure that
10526 -- it is safe to do the replacement.
10528 else
10529 exit;
10530 end if;
10531 end loop;
10533 return False;
10534 end if;
10535 end OK_To_Do_Constant_Replacement;
10537 ------------------------------------
10538 -- Possible_Bit_Aligned_Component --
10539 ------------------------------------
10541 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10542 begin
10543 -- Do not process an unanalyzed node because it is not yet decorated and
10544 -- most checks performed below will fail.
10546 if not Analyzed (N) then
10547 return False;
10548 end if;
10550 case Nkind (N) is
10552 -- Case of indexed component
10554 when N_Indexed_Component =>
10555 declare
10556 P : constant Node_Id := Prefix (N);
10557 Ptyp : constant Entity_Id := Etype (P);
10559 begin
10560 -- If we know the component size and it is less than 64, then
10561 -- we are definitely OK. The back end always does assignment of
10562 -- misaligned small objects correctly.
10564 if Known_Static_Component_Size (Ptyp)
10565 and then Component_Size (Ptyp) <= 64
10566 then
10567 return False;
10569 -- Otherwise, we need to test the prefix, to see if we are
10570 -- indexing from a possibly unaligned component.
10572 else
10573 return Possible_Bit_Aligned_Component (P);
10574 end if;
10575 end;
10577 -- Case of selected component
10579 when N_Selected_Component =>
10580 declare
10581 P : constant Node_Id := Prefix (N);
10582 Comp : constant Entity_Id := Entity (Selector_Name (N));
10584 begin
10585 -- If there is no component clause, then we are in the clear
10586 -- since the back end will never misalign a large component
10587 -- unless it is forced to do so. In the clear means we need
10588 -- only the recursive test on the prefix.
10590 if Component_May_Be_Bit_Aligned (Comp) then
10591 return True;
10592 else
10593 return Possible_Bit_Aligned_Component (P);
10594 end if;
10595 end;
10597 -- For a slice, test the prefix, if that is possibly misaligned,
10598 -- then for sure the slice is.
10600 when N_Slice =>
10601 return Possible_Bit_Aligned_Component (Prefix (N));
10603 -- For an unchecked conversion, check whether the expression may
10604 -- be bit-aligned.
10606 when N_Unchecked_Type_Conversion =>
10607 return Possible_Bit_Aligned_Component (Expression (N));
10609 -- If we have none of the above, it means that we have fallen off the
10610 -- top testing prefixes recursively, and we now have a stand alone
10611 -- object, where we don't have a problem, unless this is a renaming,
10612 -- in which case we need to look into the renamed object.
10614 when others =>
10615 if Is_Entity_Name (N)
10616 and then Present (Renamed_Object (Entity (N)))
10617 then
10618 return
10619 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10620 else
10621 return False;
10622 end if;
10623 end case;
10624 end Possible_Bit_Aligned_Component;
10626 -----------------------------------------------
10627 -- Process_Statements_For_Controlled_Objects --
10628 -----------------------------------------------
10630 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10631 Loc : constant Source_Ptr := Sloc (N);
10633 function Are_Wrapped (L : List_Id) return Boolean;
10634 -- Determine whether list L contains only one statement which is a block
10636 function Wrap_Statements_In_Block
10637 (L : List_Id;
10638 Scop : Entity_Id := Current_Scope) return Node_Id;
10639 -- Given a list of statements L, wrap it in a block statement and return
10640 -- the generated node. Scop is either the current scope or the scope of
10641 -- the context (if applicable).
10643 -----------------
10644 -- Are_Wrapped --
10645 -----------------
10647 function Are_Wrapped (L : List_Id) return Boolean is
10648 Stmt : constant Node_Id := First (L);
10649 begin
10650 return
10651 Present (Stmt)
10652 and then No (Next (Stmt))
10653 and then Nkind (Stmt) = N_Block_Statement;
10654 end Are_Wrapped;
10656 ------------------------------
10657 -- Wrap_Statements_In_Block --
10658 ------------------------------
10660 function Wrap_Statements_In_Block
10661 (L : List_Id;
10662 Scop : Entity_Id := Current_Scope) return Node_Id
10664 Block_Id : Entity_Id;
10665 Block_Nod : Node_Id;
10666 Iter_Loop : Entity_Id;
10668 begin
10669 Block_Nod :=
10670 Make_Block_Statement (Loc,
10671 Declarations => No_List,
10672 Handled_Statement_Sequence =>
10673 Make_Handled_Sequence_Of_Statements (Loc,
10674 Statements => L));
10676 -- Create a label for the block in case the block needs to manage the
10677 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10679 Add_Block_Identifier (Block_Nod, Block_Id);
10681 -- When wrapping the statements of an iterator loop, check whether
10682 -- the loop requires secondary stack management and if so, propagate
10683 -- the appropriate flags to the block. This ensures that the cursor
10684 -- is properly cleaned up at each iteration of the loop.
10686 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10688 if Present (Iter_Loop) then
10689 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10691 -- Secondary stack reclamation is suppressed when the associated
10692 -- iterator loop contains a return statement which uses the stack.
10694 Set_Sec_Stack_Needed_For_Return
10695 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10696 end if;
10698 return Block_Nod;
10699 end Wrap_Statements_In_Block;
10701 -- Local variables
10703 Block : Node_Id;
10705 -- Start of processing for Process_Statements_For_Controlled_Objects
10707 begin
10708 -- Whenever a non-handled statement list is wrapped in a block, the
10709 -- block must be explicitly analyzed to redecorate all entities in the
10710 -- list and ensure that a finalizer is properly built.
10712 case Nkind (N) is
10713 when N_Conditional_Entry_Call
10714 | N_Elsif_Part
10715 | N_If_Statement
10716 | N_Selective_Accept
10718 -- Check the "then statements" for elsif parts and if statements
10720 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10721 and then not Is_Empty_List (Then_Statements (N))
10722 and then not Are_Wrapped (Then_Statements (N))
10723 and then Requires_Cleanup_Actions
10724 (L => Then_Statements (N),
10725 Lib_Level => False,
10726 Nested_Constructs => False)
10727 then
10728 Block := Wrap_Statements_In_Block (Then_Statements (N));
10729 Set_Then_Statements (N, New_List (Block));
10731 Analyze (Block);
10732 end if;
10734 -- Check the "else statements" for conditional entry calls, if
10735 -- statements and selective accepts.
10737 if Nkind_In (N, N_Conditional_Entry_Call,
10738 N_If_Statement,
10739 N_Selective_Accept)
10740 and then not Is_Empty_List (Else_Statements (N))
10741 and then not Are_Wrapped (Else_Statements (N))
10742 and then Requires_Cleanup_Actions
10743 (L => Else_Statements (N),
10744 Lib_Level => False,
10745 Nested_Constructs => False)
10746 then
10747 Block := Wrap_Statements_In_Block (Else_Statements (N));
10748 Set_Else_Statements (N, New_List (Block));
10750 Analyze (Block);
10751 end if;
10753 when N_Abortable_Part
10754 | N_Accept_Alternative
10755 | N_Case_Statement_Alternative
10756 | N_Delay_Alternative
10757 | N_Entry_Call_Alternative
10758 | N_Exception_Handler
10759 | N_Loop_Statement
10760 | N_Triggering_Alternative
10762 if not Is_Empty_List (Statements (N))
10763 and then not Are_Wrapped (Statements (N))
10764 and then Requires_Cleanup_Actions
10765 (L => Statements (N),
10766 Lib_Level => False,
10767 Nested_Constructs => False)
10768 then
10769 if Nkind (N) = N_Loop_Statement
10770 and then Present (Identifier (N))
10771 then
10772 Block :=
10773 Wrap_Statements_In_Block
10774 (L => Statements (N),
10775 Scop => Entity (Identifier (N)));
10776 else
10777 Block := Wrap_Statements_In_Block (Statements (N));
10778 end if;
10780 Set_Statements (N, New_List (Block));
10781 Analyze (Block);
10782 end if;
10784 -- Could be e.g. a loop that was transformed into a block or null
10785 -- statement. Do nothing for terminate alternatives.
10787 when N_Block_Statement
10788 | N_Null_Statement
10789 | N_Terminate_Alternative
10791 null;
10793 when others =>
10794 raise Program_Error;
10795 end case;
10796 end Process_Statements_For_Controlled_Objects;
10798 ------------------
10799 -- Power_Of_Two --
10800 ------------------
10802 function Power_Of_Two (N : Node_Id) return Nat is
10803 Typ : constant Entity_Id := Etype (N);
10804 pragma Assert (Is_Integer_Type (Typ));
10806 Siz : constant Nat := UI_To_Int (Esize (Typ));
10807 Val : Uint;
10809 begin
10810 if not Compile_Time_Known_Value (N) then
10811 return 0;
10813 else
10814 Val := Expr_Value (N);
10815 for J in 1 .. Siz - 1 loop
10816 if Val = Uint_2 ** J then
10817 return J;
10818 end if;
10819 end loop;
10821 return 0;
10822 end if;
10823 end Power_Of_Two;
10825 ----------------------
10826 -- Remove_Init_Call --
10827 ----------------------
10829 function Remove_Init_Call
10830 (Var : Entity_Id;
10831 Rep_Clause : Node_Id) return Node_Id
10833 Par : constant Node_Id := Parent (Var);
10834 Typ : constant Entity_Id := Etype (Var);
10836 Init_Proc : Entity_Id;
10837 -- Initialization procedure for Typ
10839 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
10840 -- Look for init call for Var starting at From and scanning the
10841 -- enclosing list until Rep_Clause or the end of the list is reached.
10843 ----------------------------
10844 -- Find_Init_Call_In_List --
10845 ----------------------------
10847 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
10848 Init_Call : Node_Id;
10850 begin
10851 Init_Call := From;
10852 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
10853 if Nkind (Init_Call) = N_Procedure_Call_Statement
10854 and then Is_Entity_Name (Name (Init_Call))
10855 and then Entity (Name (Init_Call)) = Init_Proc
10856 then
10857 return Init_Call;
10858 end if;
10860 Next (Init_Call);
10861 end loop;
10863 return Empty;
10864 end Find_Init_Call_In_List;
10866 Init_Call : Node_Id;
10868 -- Start of processing for Find_Init_Call
10870 begin
10871 if Present (Initialization_Statements (Var)) then
10872 Init_Call := Initialization_Statements (Var);
10873 Set_Initialization_Statements (Var, Empty);
10875 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
10877 -- No init proc for the type, so obviously no call to be found
10879 return Empty;
10881 else
10882 -- We might be able to handle other cases below by just properly
10883 -- setting Initialization_Statements at the point where the init proc
10884 -- call is generated???
10886 Init_Proc := Base_Init_Proc (Typ);
10888 -- First scan the list containing the declaration of Var
10890 Init_Call := Find_Init_Call_In_List (From => Next (Par));
10892 -- If not found, also look on Var's freeze actions list, if any,
10893 -- since the init call may have been moved there (case of an address
10894 -- clause applying to Var).
10896 if No (Init_Call) and then Present (Freeze_Node (Var)) then
10897 Init_Call :=
10898 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
10899 end if;
10901 -- If the initialization call has actuals that use the secondary
10902 -- stack, the call may have been wrapped into a temporary block, in
10903 -- which case the block itself has to be removed.
10905 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
10906 declare
10907 Blk : constant Node_Id := Next (Par);
10908 begin
10909 if Present
10910 (Find_Init_Call_In_List
10911 (First (Statements (Handled_Statement_Sequence (Blk)))))
10912 then
10913 Init_Call := Blk;
10914 end if;
10915 end;
10916 end if;
10917 end if;
10919 if Present (Init_Call) then
10920 Remove (Init_Call);
10921 end if;
10922 return Init_Call;
10923 end Remove_Init_Call;
10925 -------------------------
10926 -- Remove_Side_Effects --
10927 -------------------------
10929 procedure Remove_Side_Effects
10930 (Exp : Node_Id;
10931 Name_Req : Boolean := False;
10932 Renaming_Req : Boolean := False;
10933 Variable_Ref : Boolean := False;
10934 Related_Id : Entity_Id := Empty;
10935 Is_Low_Bound : Boolean := False;
10936 Is_High_Bound : Boolean := False;
10937 Check_Side_Effects : Boolean := True)
10939 function Build_Temporary
10940 (Loc : Source_Ptr;
10941 Id : Character;
10942 Related_Nod : Node_Id := Empty) return Entity_Id;
10943 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
10944 -- is present (xxx is taken from the Chars field of Related_Nod),
10945 -- otherwise it generates an internal temporary. The created temporary
10946 -- entity is marked as internal.
10948 ---------------------
10949 -- Build_Temporary --
10950 ---------------------
10952 function Build_Temporary
10953 (Loc : Source_Ptr;
10954 Id : Character;
10955 Related_Nod : Node_Id := Empty) return Entity_Id
10957 Temp_Id : Entity_Id;
10958 Temp_Nam : Name_Id;
10960 begin
10961 -- The context requires an external symbol
10963 if Present (Related_Id) then
10964 if Is_Low_Bound then
10965 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
10966 else pragma Assert (Is_High_Bound);
10967 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
10968 end if;
10970 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
10972 -- Otherwise generate an internal temporary
10974 else
10975 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
10976 end if;
10978 Set_Is_Internal (Temp_Id);
10980 return Temp_Id;
10981 end Build_Temporary;
10983 -- Local variables
10985 Loc : constant Source_Ptr := Sloc (Exp);
10986 Exp_Type : constant Entity_Id := Etype (Exp);
10987 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
10988 Def_Id : Entity_Id;
10989 E : Node_Id;
10990 New_Exp : Node_Id;
10991 Ptr_Typ_Decl : Node_Id;
10992 Ref_Type : Entity_Id;
10993 Res : Node_Id;
10995 -- Start of processing for Remove_Side_Effects
10997 begin
10998 -- Handle cases in which there is nothing to do. In GNATprove mode,
10999 -- removal of side effects is useful for the light expansion of
11000 -- renamings. This removal should only occur when not inside a
11001 -- generic and not doing a pre-analysis.
11003 if not Expander_Active
11004 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
11005 then
11006 return;
11008 -- Cannot generate temporaries if the invocation to remove side effects
11009 -- was issued too early and the type of the expression is not resolved
11010 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11011 -- Remove_Side_Effects).
11013 elsif No (Exp_Type)
11014 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11015 then
11016 return;
11018 -- Nothing to do if prior expansion determined that a function call does
11019 -- not require side effect removal.
11021 elsif Nkind (Exp) = N_Function_Call
11022 and then No_Side_Effect_Removal (Exp)
11023 then
11024 return;
11026 -- No action needed for side-effect free expressions
11028 elsif Check_Side_Effects
11029 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11030 then
11031 return;
11032 end if;
11034 -- The remaining processing is done with all checks suppressed
11036 -- Note: from now on, don't use return statements, instead do a goto
11037 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11039 Scope_Suppress.Suppress := (others => True);
11041 -- If this is an elementary or a small not-by-reference record type, and
11042 -- we need to capture the value, just make a constant; this is cheap and
11043 -- objects of both kinds of types can be bit aligned, so it might not be
11044 -- possible to generate a reference to them. Likewise if this is not a
11045 -- name reference, except for a type conversion, because we would enter
11046 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11047 -- type has predicates (and type conversions need a specific treatment
11048 -- anyway, see below). Also do it if we have a volatile reference and
11049 -- Name_Req is not set (see comments for Side_Effect_Free).
11051 if (Is_Elementary_Type (Exp_Type)
11052 or else (Is_Record_Type (Exp_Type)
11053 and then Known_Static_RM_Size (Exp_Type)
11054 and then RM_Size (Exp_Type) <= 64
11055 and then not Has_Discriminants (Exp_Type)
11056 and then not Is_By_Reference_Type (Exp_Type)))
11057 and then (Variable_Ref
11058 or else (not Is_Name_Reference (Exp)
11059 and then Nkind (Exp) /= N_Type_Conversion)
11060 or else (not Name_Req
11061 and then Is_Volatile_Reference (Exp)))
11062 then
11063 Def_Id := Build_Temporary (Loc, 'R', Exp);
11064 Set_Etype (Def_Id, Exp_Type);
11065 Res := New_Occurrence_Of (Def_Id, Loc);
11067 -- If the expression is a packed reference, it must be reanalyzed and
11068 -- expanded, depending on context. This is the case for actuals where
11069 -- a constraint check may capture the actual before expansion of the
11070 -- call is complete.
11072 if Nkind (Exp) = N_Indexed_Component
11073 and then Is_Packed (Etype (Prefix (Exp)))
11074 then
11075 Set_Analyzed (Exp, False);
11076 Set_Analyzed (Prefix (Exp), False);
11077 end if;
11079 -- Generate:
11080 -- Rnn : Exp_Type renames Expr;
11082 if Renaming_Req then
11083 E :=
11084 Make_Object_Renaming_Declaration (Loc,
11085 Defining_Identifier => Def_Id,
11086 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11087 Name => Relocate_Node (Exp));
11089 -- Generate:
11090 -- Rnn : constant Exp_Type := Expr;
11092 else
11093 E :=
11094 Make_Object_Declaration (Loc,
11095 Defining_Identifier => Def_Id,
11096 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11097 Constant_Present => True,
11098 Expression => Relocate_Node (Exp));
11100 Set_Assignment_OK (E);
11101 end if;
11103 Insert_Action (Exp, E);
11105 -- If the expression has the form v.all then we can just capture the
11106 -- pointer, and then do an explicit dereference on the result, but
11107 -- this is not right if this is a volatile reference.
11109 elsif Nkind (Exp) = N_Explicit_Dereference
11110 and then not Is_Volatile_Reference (Exp)
11111 then
11112 Def_Id := Build_Temporary (Loc, 'R', Exp);
11113 Res :=
11114 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11116 Insert_Action (Exp,
11117 Make_Object_Declaration (Loc,
11118 Defining_Identifier => Def_Id,
11119 Object_Definition =>
11120 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11121 Constant_Present => True,
11122 Expression => Relocate_Node (Prefix (Exp))));
11124 -- Similar processing for an unchecked conversion of an expression of
11125 -- the form v.all, where we want the same kind of treatment.
11127 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11128 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11129 then
11130 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11131 goto Leave;
11133 -- If this is a type conversion, leave the type conversion and remove
11134 -- the side effects in the expression. This is important in several
11135 -- circumstances: for change of representations, and also when this is a
11136 -- view conversion to a smaller object, where gigi can end up creating
11137 -- its own temporary of the wrong size.
11139 elsif Nkind (Exp) = N_Type_Conversion then
11140 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11142 -- Generating C code the type conversion of an access to constrained
11143 -- array type into an access to unconstrained array type involves
11144 -- initializing a fat pointer and the expression must be free of
11145 -- side effects to safely compute its bounds.
11147 if Modify_Tree_For_C
11148 and then Is_Access_Type (Etype (Exp))
11149 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11150 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11151 then
11152 Def_Id := Build_Temporary (Loc, 'R', Exp);
11153 Set_Etype (Def_Id, Exp_Type);
11154 Res := New_Occurrence_Of (Def_Id, Loc);
11156 Insert_Action (Exp,
11157 Make_Object_Declaration (Loc,
11158 Defining_Identifier => Def_Id,
11159 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11160 Constant_Present => True,
11161 Expression => Relocate_Node (Exp)));
11162 else
11163 goto Leave;
11164 end if;
11166 -- If this is an unchecked conversion that Gigi can't handle, make
11167 -- a copy or a use a renaming to capture the value.
11169 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11170 and then not Safe_Unchecked_Type_Conversion (Exp)
11171 then
11172 if CW_Or_Has_Controlled_Part (Exp_Type) then
11174 -- Use a renaming to capture the expression, rather than create
11175 -- a controlled temporary.
11177 Def_Id := Build_Temporary (Loc, 'R', Exp);
11178 Res := New_Occurrence_Of (Def_Id, Loc);
11180 Insert_Action (Exp,
11181 Make_Object_Renaming_Declaration (Loc,
11182 Defining_Identifier => Def_Id,
11183 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11184 Name => Relocate_Node (Exp)));
11186 else
11187 Def_Id := Build_Temporary (Loc, 'R', Exp);
11188 Set_Etype (Def_Id, Exp_Type);
11189 Res := New_Occurrence_Of (Def_Id, Loc);
11191 E :=
11192 Make_Object_Declaration (Loc,
11193 Defining_Identifier => Def_Id,
11194 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11195 Constant_Present => not Is_Variable (Exp),
11196 Expression => Relocate_Node (Exp));
11198 Set_Assignment_OK (E);
11199 Insert_Action (Exp, E);
11200 end if;
11202 -- For expressions that denote names, we can use a renaming scheme.
11203 -- This is needed for correctness in the case of a volatile object of
11204 -- a non-volatile type because the Make_Reference call of the "default"
11205 -- approach would generate an illegal access value (an access value
11206 -- cannot designate such an object - see Analyze_Reference).
11208 elsif Is_Name_Reference (Exp)
11210 -- We skip using this scheme if we have an object of a volatile
11211 -- type and we do not have Name_Req set true (see comments for
11212 -- Side_Effect_Free).
11214 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11215 then
11216 Def_Id := Build_Temporary (Loc, 'R', Exp);
11217 Res := New_Occurrence_Of (Def_Id, Loc);
11219 Insert_Action (Exp,
11220 Make_Object_Renaming_Declaration (Loc,
11221 Defining_Identifier => Def_Id,
11222 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11223 Name => Relocate_Node (Exp)));
11225 -- If this is a packed reference, or a selected component with
11226 -- a non-standard representation, a reference to the temporary
11227 -- will be replaced by a copy of the original expression (see
11228 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11229 -- elaborated by gigi, and is of course not to be replaced in-line
11230 -- by the expression it renames, which would defeat the purpose of
11231 -- removing the side effect.
11233 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11234 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11235 then
11236 null;
11237 else
11238 Set_Is_Renaming_Of_Object (Def_Id, False);
11239 end if;
11241 -- Avoid generating a variable-sized temporary, by generating the
11242 -- reference just for the function call. The transformation could be
11243 -- refined to apply only when the array component is constrained by a
11244 -- discriminant???
11246 elsif Nkind (Exp) = N_Selected_Component
11247 and then Nkind (Prefix (Exp)) = N_Function_Call
11248 and then Is_Array_Type (Exp_Type)
11249 then
11250 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11251 goto Leave;
11253 -- Otherwise we generate a reference to the expression
11255 else
11256 -- An expression which is in SPARK mode is considered side effect
11257 -- free if the resulting value is captured by a variable or a
11258 -- constant.
11260 if GNATprove_Mode
11261 and then Nkind (Parent (Exp)) = N_Object_Declaration
11262 then
11263 goto Leave;
11265 -- When generating C code we cannot consider side effect free object
11266 -- declarations that have discriminants and are initialized by means
11267 -- of a function call since on this target there is no secondary
11268 -- stack to store the return value and the expander may generate an
11269 -- extra call to the function to compute the discriminant value. In
11270 -- addition, for targets that have secondary stack, the expansion of
11271 -- functions with side effects involves the generation of an access
11272 -- type to capture the return value stored in the secondary stack;
11273 -- by contrast when generating C code such expansion generates an
11274 -- internal object declaration (no access type involved) which must
11275 -- be identified here to avoid entering into a never-ending loop
11276 -- generating internal object declarations.
11278 elsif Modify_Tree_For_C
11279 and then Nkind (Parent (Exp)) = N_Object_Declaration
11280 and then
11281 (Nkind (Exp) /= N_Function_Call
11282 or else not Has_Discriminants (Exp_Type)
11283 or else Is_Internal_Name
11284 (Chars (Defining_Identifier (Parent (Exp)))))
11285 then
11286 goto Leave;
11287 end if;
11289 -- Special processing for function calls that return a limited type.
11290 -- We need to build a declaration that will enable build-in-place
11291 -- expansion of the call. This is not done if the context is already
11292 -- an object declaration, to prevent infinite recursion.
11294 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11295 -- to accommodate functions returning limited objects by reference.
11297 if Ada_Version >= Ada_2005
11298 and then Nkind (Exp) = N_Function_Call
11299 and then Is_Limited_View (Etype (Exp))
11300 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11301 then
11302 declare
11303 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11304 Decl : Node_Id;
11306 begin
11307 Decl :=
11308 Make_Object_Declaration (Loc,
11309 Defining_Identifier => Obj,
11310 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11311 Expression => Relocate_Node (Exp));
11313 Insert_Action (Exp, Decl);
11314 Set_Etype (Obj, Exp_Type);
11315 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11316 goto Leave;
11317 end;
11318 end if;
11320 Def_Id := Build_Temporary (Loc, 'R', Exp);
11322 -- The regular expansion of functions with side effects involves the
11323 -- generation of an access type to capture the return value found on
11324 -- the secondary stack. Since SPARK (and why) cannot process access
11325 -- types, use a different approach which ignores the secondary stack
11326 -- and "copies" the returned object.
11327 -- When generating C code, no need for a 'reference since the
11328 -- secondary stack is not supported.
11330 if GNATprove_Mode or Modify_Tree_For_C then
11331 Res := New_Occurrence_Of (Def_Id, Loc);
11332 Ref_Type := Exp_Type;
11334 -- Regular expansion utilizing an access type and 'reference
11336 else
11337 Res :=
11338 Make_Explicit_Dereference (Loc,
11339 Prefix => New_Occurrence_Of (Def_Id, Loc));
11341 -- Generate:
11342 -- type Ann is access all <Exp_Type>;
11344 Ref_Type := Make_Temporary (Loc, 'A');
11346 Ptr_Typ_Decl :=
11347 Make_Full_Type_Declaration (Loc,
11348 Defining_Identifier => Ref_Type,
11349 Type_Definition =>
11350 Make_Access_To_Object_Definition (Loc,
11351 All_Present => True,
11352 Subtype_Indication =>
11353 New_Occurrence_Of (Exp_Type, Loc)));
11355 Insert_Action (Exp, Ptr_Typ_Decl);
11356 end if;
11358 E := Exp;
11359 if Nkind (E) = N_Explicit_Dereference then
11360 New_Exp := Relocate_Node (Prefix (E));
11362 else
11363 E := Relocate_Node (E);
11365 -- Do not generate a 'reference in SPARK mode or C generation
11366 -- since the access type is not created in the first place.
11368 if GNATprove_Mode or Modify_Tree_For_C then
11369 New_Exp := E;
11371 -- Otherwise generate reference, marking the value as non-null
11372 -- since we know it cannot be null and we don't want a check.
11374 else
11375 New_Exp := Make_Reference (Loc, E);
11376 Set_Is_Known_Non_Null (Def_Id);
11377 end if;
11378 end if;
11380 if Is_Delayed_Aggregate (E) then
11382 -- The expansion of nested aggregates is delayed until the
11383 -- enclosing aggregate is expanded. As aggregates are often
11384 -- qualified, the predicate applies to qualified expressions as
11385 -- well, indicating that the enclosing aggregate has not been
11386 -- expanded yet. At this point the aggregate is part of a
11387 -- stand-alone declaration, and must be fully expanded.
11389 if Nkind (E) = N_Qualified_Expression then
11390 Set_Expansion_Delayed (Expression (E), False);
11391 Set_Analyzed (Expression (E), False);
11392 else
11393 Set_Expansion_Delayed (E, False);
11394 end if;
11396 Set_Analyzed (E, False);
11397 end if;
11399 -- Generating C code of object declarations that have discriminants
11400 -- and are initialized by means of a function call we propagate the
11401 -- discriminants of the parent type to the internally built object.
11402 -- This is needed to avoid generating an extra call to the called
11403 -- function.
11405 -- For example, if we generate here the following declaration, it
11406 -- will be expanded later adding an extra call to evaluate the value
11407 -- of the discriminant (needed to compute the size of the object).
11409 -- type Rec (D : Integer) is ...
11410 -- Obj : constant Rec := SomeFunc;
11412 if Modify_Tree_For_C
11413 and then Nkind (Parent (Exp)) = N_Object_Declaration
11414 and then Has_Discriminants (Exp_Type)
11415 and then Nkind (Exp) = N_Function_Call
11416 then
11417 Insert_Action (Exp,
11418 Make_Object_Declaration (Loc,
11419 Defining_Identifier => Def_Id,
11420 Object_Definition => New_Copy_Tree
11421 (Object_Definition (Parent (Exp))),
11422 Constant_Present => True,
11423 Expression => New_Exp));
11424 else
11425 Insert_Action (Exp,
11426 Make_Object_Declaration (Loc,
11427 Defining_Identifier => Def_Id,
11428 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11429 Constant_Present => True,
11430 Expression => New_Exp));
11431 end if;
11432 end if;
11434 -- Preserve the Assignment_OK flag in all copies, since at least one
11435 -- copy may be used in a context where this flag must be set (otherwise
11436 -- why would the flag be set in the first place).
11438 Set_Assignment_OK (Res, Assignment_OK (Exp));
11440 -- Finally rewrite the original expression and we are done
11442 Rewrite (Exp, Res);
11443 Analyze_And_Resolve (Exp, Exp_Type);
11445 <<Leave>>
11446 Scope_Suppress := Svg_Suppress;
11447 end Remove_Side_Effects;
11449 ------------------------
11450 -- Replace_References --
11451 ------------------------
11453 procedure Replace_References
11454 (Expr : Node_Id;
11455 Par_Typ : Entity_Id;
11456 Deriv_Typ : Entity_Id;
11457 Par_Obj : Entity_Id := Empty;
11458 Deriv_Obj : Entity_Id := Empty)
11460 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11461 -- Determine whether node Ref denotes some component of Deriv_Obj
11463 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11464 -- Substitute a reference to an entity with the corresponding value
11465 -- stored in table Type_Map.
11467 function Type_Of_Formal
11468 (Call : Node_Id;
11469 Actual : Node_Id) return Entity_Id;
11470 -- Find the type of the formal parameter which corresponds to actual
11471 -- parameter Actual in subprogram call Call.
11473 ----------------------
11474 -- Is_Deriv_Obj_Ref --
11475 ----------------------
11477 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11478 Par : constant Node_Id := Parent (Ref);
11480 begin
11481 -- Detect the folowing selected component form:
11483 -- Deriv_Obj.(something)
11485 return
11486 Nkind (Par) = N_Selected_Component
11487 and then Is_Entity_Name (Prefix (Par))
11488 and then Entity (Prefix (Par)) = Deriv_Obj;
11489 end Is_Deriv_Obj_Ref;
11491 -----------------
11492 -- Replace_Ref --
11493 -----------------
11495 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11496 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11497 -- Reset the Controlling_Argument of all function calls that
11498 -- encapsulate node From_Arg.
11500 ----------------------------------
11501 -- Remove_Controlling_Arguments --
11502 ----------------------------------
11504 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11505 Par : Node_Id;
11507 begin
11508 Par := From_Arg;
11509 while Present (Par) loop
11510 if Nkind (Par) = N_Function_Call
11511 and then Present (Controlling_Argument (Par))
11512 then
11513 Set_Controlling_Argument (Par, Empty);
11515 -- Prevent the search from going too far
11517 elsif Is_Body_Or_Package_Declaration (Par) then
11518 exit;
11519 end if;
11521 Par := Parent (Par);
11522 end loop;
11523 end Remove_Controlling_Arguments;
11525 -- Local variables
11527 Context : constant Node_Id := Parent (Ref);
11528 Loc : constant Source_Ptr := Sloc (Ref);
11529 Ref_Id : Entity_Id;
11530 Result : Traverse_Result;
11532 New_Ref : Node_Id;
11533 -- The new reference which is intended to substitute the old one
11535 Old_Ref : Node_Id;
11536 -- The reference designated for replacement. In certain cases this
11537 -- may be a node other than Ref.
11539 Val : Node_Or_Entity_Id;
11540 -- The corresponding value of Ref from the type map
11542 -- Start of processing for Replace_Ref
11544 begin
11545 -- Assume that the input reference is to be replaced and that the
11546 -- traversal should examine the children of the reference.
11548 Old_Ref := Ref;
11549 Result := OK;
11551 -- The input denotes a meaningful reference
11553 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11554 Ref_Id := Entity (Ref);
11555 Val := Type_Map.Get (Ref_Id);
11557 -- The reference has a corresponding value in the type map, a
11558 -- substitution is possible.
11560 if Present (Val) then
11562 -- The reference denotes a discriminant
11564 if Ekind (Ref_Id) = E_Discriminant then
11565 if Nkind (Val) in N_Entity then
11567 -- The value denotes another discriminant. Replace as
11568 -- follows:
11570 -- _object.Discr -> _object.Val
11572 if Ekind (Val) = E_Discriminant then
11573 New_Ref := New_Occurrence_Of (Val, Loc);
11575 -- Otherwise the value denotes the entity of a name which
11576 -- constraints the discriminant. Replace as follows:
11578 -- _object.Discr -> Val
11580 else
11581 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11583 New_Ref := New_Occurrence_Of (Val, Loc);
11584 Old_Ref := Parent (Old_Ref);
11585 end if;
11587 -- Otherwise the value denotes an arbitrary expression which
11588 -- constraints the discriminant. Replace as follows:
11590 -- _object.Discr -> Val
11592 else
11593 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11595 New_Ref := New_Copy_Tree (Val);
11596 Old_Ref := Parent (Old_Ref);
11597 end if;
11599 -- Otherwise the reference denotes a primitive. Replace as
11600 -- follows:
11602 -- Primitive -> Val
11604 else
11605 pragma Assert (Nkind (Val) in N_Entity);
11606 New_Ref := New_Occurrence_Of (Val, Loc);
11607 end if;
11609 -- The reference mentions the _object parameter of the parent
11610 -- type's DIC or type invariant procedure. Replace as follows:
11612 -- _object -> _object
11614 elsif Present (Par_Obj)
11615 and then Present (Deriv_Obj)
11616 and then Ref_Id = Par_Obj
11617 then
11618 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11620 -- The type of the _object parameter is class-wide when the
11621 -- expression comes from an assertion pragma that applies to
11622 -- an abstract parent type or an interface. The class-wide type
11623 -- facilitates the preanalysis of the expression by treating
11624 -- calls to abstract primitives that mention the current
11625 -- instance of the type as dispatching. Once the calls are
11626 -- remapped to invoke overriding or inherited primitives, the
11627 -- calls no longer need to be dispatching. Examine all function
11628 -- calls that encapsulate the _object parameter and reset their
11629 -- Controlling_Argument attribute.
11631 if Is_Class_Wide_Type (Etype (Par_Obj))
11632 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11633 then
11634 Remove_Controlling_Arguments (Old_Ref);
11635 end if;
11637 -- The reference to _object acts as an actual parameter in a
11638 -- subprogram call which may be invoking a primitive of the
11639 -- parent type:
11641 -- Primitive (... _object ...);
11643 -- The parent type primitive may not be overridden nor
11644 -- inherited when it is declared after the derived type
11645 -- definition:
11647 -- type Parent is tagged private;
11648 -- type Child is new Parent with private;
11649 -- procedure Primitive (Obj : Parent);
11651 -- In this scenario the _object parameter is converted to the
11652 -- parent type. Due to complications with partial/full views
11653 -- and view swaps, the parent type is taken from the formal
11654 -- parameter of the subprogram being called.
11656 if Nkind_In (Context, N_Function_Call,
11657 N_Procedure_Call_Statement)
11658 and then No (Type_Map.Get (Entity (Name (Context))))
11659 then
11660 New_Ref :=
11661 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11663 -- Do not process the generated type conversion because
11664 -- both the parent type and the derived type are in the
11665 -- Type_Map table. This will clobber the type conversion
11666 -- by resetting its subtype mark.
11668 Result := Skip;
11669 end if;
11671 -- Otherwise there is nothing to replace
11673 else
11674 New_Ref := Empty;
11675 end if;
11677 if Present (New_Ref) then
11678 Rewrite (Old_Ref, New_Ref);
11680 -- Update the return type when the context of the reference
11681 -- acts as the name of a function call. Note that the update
11682 -- should not be performed when the reference appears as an
11683 -- actual in the call.
11685 if Nkind (Context) = N_Function_Call
11686 and then Name (Context) = Old_Ref
11687 then
11688 Set_Etype (Context, Etype (Val));
11689 end if;
11690 end if;
11691 end if;
11693 -- Reanalyze the reference due to potential replacements
11695 if Nkind (Old_Ref) in N_Has_Etype then
11696 Set_Analyzed (Old_Ref, False);
11697 end if;
11699 return Result;
11700 end Replace_Ref;
11702 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11704 --------------------
11705 -- Type_Of_Formal --
11706 --------------------
11708 function Type_Of_Formal
11709 (Call : Node_Id;
11710 Actual : Node_Id) return Entity_Id
11712 A : Node_Id;
11713 F : Entity_Id;
11715 begin
11716 -- Examine the list of actual and formal parameters in parallel
11718 A := First (Parameter_Associations (Call));
11719 F := First_Formal (Entity (Name (Call)));
11720 while Present (A) and then Present (F) loop
11721 if A = Actual then
11722 return Etype (F);
11723 end if;
11725 Next (A);
11726 Next_Formal (F);
11727 end loop;
11729 -- The actual parameter must always have a corresponding formal
11731 pragma Assert (False);
11733 return Empty;
11734 end Type_Of_Formal;
11736 -- Start of processing for Replace_References
11738 begin
11739 -- Map the attributes of the parent type to the proper corresponding
11740 -- attributes of the derived type.
11742 Map_Types
11743 (Parent_Type => Par_Typ,
11744 Derived_Type => Deriv_Typ);
11746 -- Inspect the input expression and perform substitutions where
11747 -- necessary.
11749 Replace_Refs (Expr);
11750 end Replace_References;
11752 -----------------------------
11753 -- Replace_Type_References --
11754 -----------------------------
11756 procedure Replace_Type_References
11757 (Expr : Node_Id;
11758 Typ : Entity_Id;
11759 Obj_Id : Entity_Id)
11761 procedure Replace_Type_Ref (N : Node_Id);
11762 -- Substitute a single reference of the current instance of type Typ
11763 -- with a reference to Obj_Id.
11765 ----------------------
11766 -- Replace_Type_Ref --
11767 ----------------------
11769 procedure Replace_Type_Ref (N : Node_Id) is
11770 begin
11771 -- Decorate the reference to Typ even though it may be rewritten
11772 -- further down. This is done for two reasons:
11774 -- * ASIS has all necessary semantic information in the original
11775 -- tree.
11777 -- * Routines which examine properties of the Original_Node have
11778 -- some semantic information.
11780 if Nkind (N) = N_Identifier then
11781 Set_Entity (N, Typ);
11782 Set_Etype (N, Typ);
11784 elsif Nkind (N) = N_Selected_Component then
11785 Analyze (Prefix (N));
11786 Set_Entity (Selector_Name (N), Typ);
11787 Set_Etype (Selector_Name (N), Typ);
11788 end if;
11790 -- Perform the following substitution:
11792 -- Typ --> _object
11794 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
11795 Set_Comes_From_Source (N, True);
11796 end Replace_Type_Ref;
11798 procedure Replace_Type_Refs is
11799 new Replace_Type_References_Generic (Replace_Type_Ref);
11801 -- Start of processing for Replace_Type_References
11803 begin
11804 Replace_Type_Refs (Expr, Typ);
11805 end Replace_Type_References;
11807 ---------------------------
11808 -- Represented_As_Scalar --
11809 ---------------------------
11811 function Represented_As_Scalar (T : Entity_Id) return Boolean is
11812 UT : constant Entity_Id := Underlying_Type (T);
11813 begin
11814 return Is_Scalar_Type (UT)
11815 or else (Is_Bit_Packed_Array (UT)
11816 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
11817 end Represented_As_Scalar;
11819 ------------------------------
11820 -- Requires_Cleanup_Actions --
11821 ------------------------------
11823 function Requires_Cleanup_Actions
11824 (N : Node_Id;
11825 Lib_Level : Boolean) return Boolean
11827 At_Lib_Level : constant Boolean :=
11828 Lib_Level
11829 and then Nkind_In (N, N_Package_Body,
11830 N_Package_Specification);
11831 -- N is at the library level if the top-most context is a package and
11832 -- the path taken to reach N does not inlcude non-package constructs.
11834 begin
11835 case Nkind (N) is
11836 when N_Accept_Statement
11837 | N_Block_Statement
11838 | N_Entry_Body
11839 | N_Package_Body
11840 | N_Protected_Body
11841 | N_Subprogram_Body
11842 | N_Task_Body
11844 return
11845 Requires_Cleanup_Actions
11846 (L => Declarations (N),
11847 Lib_Level => At_Lib_Level,
11848 Nested_Constructs => True)
11849 or else
11850 (Present (Handled_Statement_Sequence (N))
11851 and then
11852 Requires_Cleanup_Actions
11853 (L =>
11854 Statements (Handled_Statement_Sequence (N)),
11855 Lib_Level => At_Lib_Level,
11856 Nested_Constructs => True));
11858 -- Extended return statements are the same as the above, except that
11859 -- there is no Declarations field. We do not want to clean up the
11860 -- Return_Object_Declarations.
11862 when N_Extended_Return_Statement =>
11863 return
11864 Present (Handled_Statement_Sequence (N))
11865 and then Requires_Cleanup_Actions
11866 (L =>
11867 Statements (Handled_Statement_Sequence (N)),
11868 Lib_Level => At_Lib_Level,
11869 Nested_Constructs => True);
11871 when N_Package_Specification =>
11872 return
11873 Requires_Cleanup_Actions
11874 (L => Visible_Declarations (N),
11875 Lib_Level => At_Lib_Level,
11876 Nested_Constructs => True)
11877 or else
11878 Requires_Cleanup_Actions
11879 (L => Private_Declarations (N),
11880 Lib_Level => At_Lib_Level,
11881 Nested_Constructs => True);
11883 when others =>
11884 raise Program_Error;
11885 end case;
11886 end Requires_Cleanup_Actions;
11888 ------------------------------
11889 -- Requires_Cleanup_Actions --
11890 ------------------------------
11892 function Requires_Cleanup_Actions
11893 (L : List_Id;
11894 Lib_Level : Boolean;
11895 Nested_Constructs : Boolean) return Boolean
11897 Decl : Node_Id;
11898 Expr : Node_Id;
11899 Obj_Id : Entity_Id;
11900 Obj_Typ : Entity_Id;
11901 Pack_Id : Entity_Id;
11902 Typ : Entity_Id;
11904 begin
11905 if No (L)
11906 or else Is_Empty_List (L)
11907 then
11908 return False;
11909 end if;
11911 Decl := First (L);
11912 while Present (Decl) loop
11914 -- Library-level tagged types
11916 if Nkind (Decl) = N_Full_Type_Declaration then
11917 Typ := Defining_Identifier (Decl);
11919 -- Ignored Ghost types do not need any cleanup actions because
11920 -- they will not appear in the final tree.
11922 if Is_Ignored_Ghost_Entity (Typ) then
11923 null;
11925 elsif Is_Tagged_Type (Typ)
11926 and then Is_Library_Level_Entity (Typ)
11927 and then Convention (Typ) = Convention_Ada
11928 and then Present (Access_Disp_Table (Typ))
11929 and then RTE_Available (RE_Unregister_Tag)
11930 and then not Is_Abstract_Type (Typ)
11931 and then not No_Run_Time_Mode
11932 then
11933 return True;
11934 end if;
11936 -- Regular object declarations
11938 elsif Nkind (Decl) = N_Object_Declaration then
11939 Obj_Id := Defining_Identifier (Decl);
11940 Obj_Typ := Base_Type (Etype (Obj_Id));
11941 Expr := Expression (Decl);
11943 -- Bypass any form of processing for objects which have their
11944 -- finalization disabled. This applies only to objects at the
11945 -- library level.
11947 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
11948 null;
11950 -- Finalization of transient objects are treated separately in
11951 -- order to handle sensitive cases. These include:
11953 -- * Aggregate expansion
11954 -- * If, case, and expression with actions expansion
11955 -- * Transient scopes
11957 -- If one of those contexts has marked the transient object as
11958 -- ignored, do not generate finalization actions for it.
11960 elsif Is_Finalized_Transient (Obj_Id)
11961 or else Is_Ignored_Transient (Obj_Id)
11962 then
11963 null;
11965 -- Ignored Ghost objects do not need any cleanup actions because
11966 -- they will not appear in the final tree.
11968 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
11969 null;
11971 -- The object is of the form:
11972 -- Obj : [constant] Typ [:= Expr];
11974 -- Do not process tag-to-class-wide conversions because they do
11975 -- not yield an object. Do not process the incomplete view of a
11976 -- deferred constant. Note that an object initialized by means
11977 -- of a build-in-place function call may appear as a deferred
11978 -- constant after expansion activities. These kinds of objects
11979 -- must be finalized.
11981 elsif not Is_Imported (Obj_Id)
11982 and then Needs_Finalization (Obj_Typ)
11983 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
11984 and then not (Ekind (Obj_Id) = E_Constant
11985 and then not Has_Completion (Obj_Id)
11986 and then No (BIP_Initialization_Call (Obj_Id)))
11987 then
11988 return True;
11990 -- The object is of the form:
11991 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
11993 -- Obj : Access_Typ :=
11994 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
11996 elsif Is_Access_Type (Obj_Typ)
11997 and then Needs_Finalization
11998 (Available_View (Designated_Type (Obj_Typ)))
11999 and then Present (Expr)
12000 and then
12001 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12002 or else
12003 (Is_Non_BIP_Func_Call (Expr)
12004 and then not Is_Related_To_Func_Return (Obj_Id)))
12005 then
12006 return True;
12008 -- Processing for "hook" objects generated for transient objects
12009 -- declared inside an Expression_With_Actions.
12011 elsif Is_Access_Type (Obj_Typ)
12012 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12013 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12014 N_Object_Declaration
12015 then
12016 return True;
12018 -- Processing for intermediate results of if expressions where
12019 -- one of the alternatives uses a controlled function call.
12021 elsif Is_Access_Type (Obj_Typ)
12022 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12023 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12024 N_Defining_Identifier
12025 and then Present (Expr)
12026 and then Nkind (Expr) = N_Null
12027 then
12028 return True;
12030 -- Simple protected objects which use type System.Tasking.
12031 -- Protected_Objects.Protection to manage their locks should be
12032 -- treated as controlled since they require manual cleanup.
12034 elsif Ekind (Obj_Id) = E_Variable
12035 and then (Is_Simple_Protected_Type (Obj_Typ)
12036 or else Has_Simple_Protected_Object (Obj_Typ))
12037 then
12038 return True;
12039 end if;
12041 -- Specific cases of object renamings
12043 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12044 Obj_Id := Defining_Identifier (Decl);
12045 Obj_Typ := Base_Type (Etype (Obj_Id));
12047 -- Bypass any form of processing for objects which have their
12048 -- finalization disabled. This applies only to objects at the
12049 -- library level.
12051 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12052 null;
12054 -- Ignored Ghost object renamings do not need any cleanup actions
12055 -- because they will not appear in the final tree.
12057 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12058 null;
12060 -- Return object of a build-in-place function. This case is
12061 -- recognized and marked by the expansion of an extended return
12062 -- statement (see Expand_N_Extended_Return_Statement).
12064 elsif Needs_Finalization (Obj_Typ)
12065 and then Is_Return_Object (Obj_Id)
12066 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12067 then
12068 return True;
12070 -- Detect a case where a source object has been initialized by
12071 -- a controlled function call or another object which was later
12072 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12074 -- Obj1 : CW_Type := Src_Obj;
12075 -- Obj2 : CW_Type := Function_Call (...);
12077 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12078 -- Tmp : ... := Function_Call (...)'reference;
12079 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12081 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12082 return True;
12083 end if;
12085 -- Inspect the freeze node of an access-to-controlled type and look
12086 -- for a delayed finalization master. This case arises when the
12087 -- freeze actions are inserted at a later time than the expansion of
12088 -- the context. Since Build_Finalizer is never called on a single
12089 -- construct twice, the master will be ultimately left out and never
12090 -- finalized. This is also needed for freeze actions of designated
12091 -- types themselves, since in some cases the finalization master is
12092 -- associated with a designated type's freeze node rather than that
12093 -- of the access type (see handling for freeze actions in
12094 -- Build_Finalization_Master).
12096 elsif Nkind (Decl) = N_Freeze_Entity
12097 and then Present (Actions (Decl))
12098 then
12099 Typ := Entity (Decl);
12101 -- Freeze nodes for ignored Ghost types do not need cleanup
12102 -- actions because they will never appear in the final tree.
12104 if Is_Ignored_Ghost_Entity (Typ) then
12105 null;
12107 elsif ((Is_Access_Type (Typ)
12108 and then not Is_Access_Subprogram_Type (Typ)
12109 and then Needs_Finalization
12110 (Available_View (Designated_Type (Typ))))
12111 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12112 and then Requires_Cleanup_Actions
12113 (Actions (Decl), Lib_Level, Nested_Constructs)
12114 then
12115 return True;
12116 end if;
12118 -- Nested package declarations
12120 elsif Nested_Constructs
12121 and then Nkind (Decl) = N_Package_Declaration
12122 then
12123 Pack_Id := Defining_Entity (Decl);
12125 -- Do not inspect an ignored Ghost package because all code found
12126 -- within will not appear in the final tree.
12128 if Is_Ignored_Ghost_Entity (Pack_Id) then
12129 null;
12131 elsif Ekind (Pack_Id) /= E_Generic_Package
12132 and then Requires_Cleanup_Actions
12133 (Specification (Decl), Lib_Level)
12134 then
12135 return True;
12136 end if;
12138 -- Nested package bodies
12140 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12142 -- Do not inspect an ignored Ghost package body because all code
12143 -- found within will not appear in the final tree.
12145 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12146 null;
12148 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12149 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12150 then
12151 return True;
12152 end if;
12154 elsif Nkind (Decl) = N_Block_Statement
12155 and then
12157 -- Handle a rare case caused by a controlled transient object
12158 -- created as part of a record init proc. The variable is wrapped
12159 -- in a block, but the block is not associated with a transient
12160 -- scope.
12162 (Inside_Init_Proc
12164 -- Handle the case where the original context has been wrapped in
12165 -- a block to avoid interference between exception handlers and
12166 -- At_End handlers. Treat the block as transparent and process its
12167 -- contents.
12169 or else Is_Finalization_Wrapper (Decl))
12170 then
12171 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12172 return True;
12173 end if;
12174 end if;
12176 Next (Decl);
12177 end loop;
12179 return False;
12180 end Requires_Cleanup_Actions;
12182 ------------------------------------
12183 -- Safe_Unchecked_Type_Conversion --
12184 ------------------------------------
12186 -- Note: this function knows quite a bit about the exact requirements of
12187 -- Gigi with respect to unchecked type conversions, and its code must be
12188 -- coordinated with any changes in Gigi in this area.
12190 -- The above requirements should be documented in Sinfo ???
12192 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12193 Otyp : Entity_Id;
12194 Ityp : Entity_Id;
12195 Oalign : Uint;
12196 Ialign : Uint;
12197 Pexp : constant Node_Id := Parent (Exp);
12199 begin
12200 -- If the expression is the RHS of an assignment or object declaration
12201 -- we are always OK because there will always be a target.
12203 -- Object renaming declarations, (generated for view conversions of
12204 -- actuals in inlined calls), like object declarations, provide an
12205 -- explicit type, and are safe as well.
12207 if (Nkind (Pexp) = N_Assignment_Statement
12208 and then Expression (Pexp) = Exp)
12209 or else Nkind_In (Pexp, N_Object_Declaration,
12210 N_Object_Renaming_Declaration)
12211 then
12212 return True;
12214 -- If the expression is the prefix of an N_Selected_Component we should
12215 -- also be OK because GCC knows to look inside the conversion except if
12216 -- the type is discriminated. We assume that we are OK anyway if the
12217 -- type is not set yet or if it is controlled since we can't afford to
12218 -- introduce a temporary in this case.
12220 elsif Nkind (Pexp) = N_Selected_Component
12221 and then Prefix (Pexp) = Exp
12222 then
12223 if No (Etype (Pexp)) then
12224 return True;
12225 else
12226 return
12227 not Has_Discriminants (Etype (Pexp))
12228 or else Is_Constrained (Etype (Pexp));
12229 end if;
12230 end if;
12232 -- Set the output type, this comes from Etype if it is set, otherwise we
12233 -- take it from the subtype mark, which we assume was already fully
12234 -- analyzed.
12236 if Present (Etype (Exp)) then
12237 Otyp := Etype (Exp);
12238 else
12239 Otyp := Entity (Subtype_Mark (Exp));
12240 end if;
12242 -- The input type always comes from the expression, and we assume this
12243 -- is indeed always analyzed, so we can simply get the Etype.
12245 Ityp := Etype (Expression (Exp));
12247 -- Initialize alignments to unknown so far
12249 Oalign := No_Uint;
12250 Ialign := No_Uint;
12252 -- Replace a concurrent type by its corresponding record type and each
12253 -- type by its underlying type and do the tests on those. The original
12254 -- type may be a private type whose completion is a concurrent type, so
12255 -- find the underlying type first.
12257 if Present (Underlying_Type (Otyp)) then
12258 Otyp := Underlying_Type (Otyp);
12259 end if;
12261 if Present (Underlying_Type (Ityp)) then
12262 Ityp := Underlying_Type (Ityp);
12263 end if;
12265 if Is_Concurrent_Type (Otyp) then
12266 Otyp := Corresponding_Record_Type (Otyp);
12267 end if;
12269 if Is_Concurrent_Type (Ityp) then
12270 Ityp := Corresponding_Record_Type (Ityp);
12271 end if;
12273 -- If the base types are the same, we know there is no problem since
12274 -- this conversion will be a noop.
12276 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12277 return True;
12279 -- Same if this is an upwards conversion of an untagged type, and there
12280 -- are no constraints involved (could be more general???)
12282 elsif Etype (Ityp) = Otyp
12283 and then not Is_Tagged_Type (Ityp)
12284 and then not Has_Discriminants (Ityp)
12285 and then No (First_Rep_Item (Base_Type (Ityp)))
12286 then
12287 return True;
12289 -- If the expression has an access type (object or subprogram) we assume
12290 -- that the conversion is safe, because the size of the target is safe,
12291 -- even if it is a record (which might be treated as having unknown size
12292 -- at this point).
12294 elsif Is_Access_Type (Ityp) then
12295 return True;
12297 -- If the size of output type is known at compile time, there is never
12298 -- a problem. Note that unconstrained records are considered to be of
12299 -- known size, but we can't consider them that way here, because we are
12300 -- talking about the actual size of the object.
12302 -- We also make sure that in addition to the size being known, we do not
12303 -- have a case which might generate an embarrassingly large temp in
12304 -- stack checking mode.
12306 elsif Size_Known_At_Compile_Time (Otyp)
12307 and then
12308 (not Stack_Checking_Enabled
12309 or else not May_Generate_Large_Temp (Otyp))
12310 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12311 then
12312 return True;
12314 -- If either type is tagged, then we know the alignment is OK so Gigi
12315 -- will be able to use pointer punning.
12317 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12318 return True;
12320 -- If either type is a limited record type, we cannot do a copy, so say
12321 -- safe since there's nothing else we can do.
12323 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12324 return True;
12326 -- Conversions to and from packed array types are always ignored and
12327 -- hence are safe.
12329 elsif Is_Packed_Array_Impl_Type (Otyp)
12330 or else Is_Packed_Array_Impl_Type (Ityp)
12331 then
12332 return True;
12333 end if;
12335 -- The only other cases known to be safe is if the input type's
12336 -- alignment is known to be at least the maximum alignment for the
12337 -- target or if both alignments are known and the output type's
12338 -- alignment is no stricter than the input's. We can use the component
12339 -- type alignment for an array if a type is an unpacked array type.
12341 if Present (Alignment_Clause (Otyp)) then
12342 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12344 elsif Is_Array_Type (Otyp)
12345 and then Present (Alignment_Clause (Component_Type (Otyp)))
12346 then
12347 Oalign := Expr_Value (Expression (Alignment_Clause
12348 (Component_Type (Otyp))));
12349 end if;
12351 if Present (Alignment_Clause (Ityp)) then
12352 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12354 elsif Is_Array_Type (Ityp)
12355 and then Present (Alignment_Clause (Component_Type (Ityp)))
12356 then
12357 Ialign := Expr_Value (Expression (Alignment_Clause
12358 (Component_Type (Ityp))));
12359 end if;
12361 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12362 return True;
12364 elsif Ialign /= No_Uint
12365 and then Oalign /= No_Uint
12366 and then Ialign <= Oalign
12367 then
12368 return True;
12370 -- Otherwise, Gigi cannot handle this and we must make a temporary
12372 else
12373 return False;
12374 end if;
12375 end Safe_Unchecked_Type_Conversion;
12377 ---------------------------------
12378 -- Set_Current_Value_Condition --
12379 ---------------------------------
12381 -- Note: the implementation of this procedure is very closely tied to the
12382 -- implementation of Get_Current_Value_Condition. Here we set required
12383 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12384 -- them, so they must have a consistent view.
12386 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12388 procedure Set_Entity_Current_Value (N : Node_Id);
12389 -- If N is an entity reference, where the entity is of an appropriate
12390 -- kind, then set the current value of this entity to Cnode, unless
12391 -- there is already a definite value set there.
12393 procedure Set_Expression_Current_Value (N : Node_Id);
12394 -- If N is of an appropriate form, sets an appropriate entry in current
12395 -- value fields of relevant entities. Multiple entities can be affected
12396 -- in the case of an AND or AND THEN.
12398 ------------------------------
12399 -- Set_Entity_Current_Value --
12400 ------------------------------
12402 procedure Set_Entity_Current_Value (N : Node_Id) is
12403 begin
12404 if Is_Entity_Name (N) then
12405 declare
12406 Ent : constant Entity_Id := Entity (N);
12408 begin
12409 -- Don't capture if not safe to do so
12411 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12412 return;
12413 end if;
12415 -- Here we have a case where the Current_Value field may need
12416 -- to be set. We set it if it is not already set to a compile
12417 -- time expression value.
12419 -- Note that this represents a decision that one condition
12420 -- blots out another previous one. That's certainly right if
12421 -- they occur at the same level. If the second one is nested,
12422 -- then the decision is neither right nor wrong (it would be
12423 -- equally OK to leave the outer one in place, or take the new
12424 -- inner one. Really we should record both, but our data
12425 -- structures are not that elaborate.
12427 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12428 Set_Current_Value (Ent, Cnode);
12429 end if;
12430 end;
12431 end if;
12432 end Set_Entity_Current_Value;
12434 ----------------------------------
12435 -- Set_Expression_Current_Value --
12436 ----------------------------------
12438 procedure Set_Expression_Current_Value (N : Node_Id) is
12439 Cond : Node_Id;
12441 begin
12442 Cond := N;
12444 -- Loop to deal with (ignore for now) any NOT operators present. The
12445 -- presence of NOT operators will be handled properly when we call
12446 -- Get_Current_Value_Condition.
12448 while Nkind (Cond) = N_Op_Not loop
12449 Cond := Right_Opnd (Cond);
12450 end loop;
12452 -- For an AND or AND THEN, recursively process operands
12454 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12455 Set_Expression_Current_Value (Left_Opnd (Cond));
12456 Set_Expression_Current_Value (Right_Opnd (Cond));
12457 return;
12458 end if;
12460 -- Check possible relational operator
12462 if Nkind (Cond) in N_Op_Compare then
12463 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12464 Set_Entity_Current_Value (Left_Opnd (Cond));
12465 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12466 Set_Entity_Current_Value (Right_Opnd (Cond));
12467 end if;
12469 elsif Nkind_In (Cond,
12470 N_Type_Conversion,
12471 N_Qualified_Expression,
12472 N_Expression_With_Actions)
12473 then
12474 Set_Expression_Current_Value (Expression (Cond));
12476 -- Check possible boolean variable reference
12478 else
12479 Set_Entity_Current_Value (Cond);
12480 end if;
12481 end Set_Expression_Current_Value;
12483 -- Start of processing for Set_Current_Value_Condition
12485 begin
12486 Set_Expression_Current_Value (Condition (Cnode));
12487 end Set_Current_Value_Condition;
12489 --------------------------
12490 -- Set_Elaboration_Flag --
12491 --------------------------
12493 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12494 Loc : constant Source_Ptr := Sloc (N);
12495 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12496 Asn : Node_Id;
12498 begin
12499 if Present (Ent) then
12501 -- Nothing to do if at the compilation unit level, because in this
12502 -- case the flag is set by the binder generated elaboration routine.
12504 if Nkind (Parent (N)) = N_Compilation_Unit then
12505 null;
12507 -- Here we do need to generate an assignment statement
12509 else
12510 Check_Restriction (No_Elaboration_Code, N);
12512 Asn :=
12513 Make_Assignment_Statement (Loc,
12514 Name => New_Occurrence_Of (Ent, Loc),
12515 Expression => Make_Integer_Literal (Loc, Uint_1));
12517 -- Mark the assignment statement as elaboration code. This allows
12518 -- the early call region mechanism (see Sem_Elab) to properly
12519 -- ignore such assignments even though they are non-preelaborable
12520 -- code.
12522 Set_Is_Elaboration_Code (Asn);
12524 if Nkind (Parent (N)) = N_Subunit then
12525 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12526 else
12527 Insert_After (N, Asn);
12528 end if;
12530 Analyze (Asn);
12532 -- Kill current value indication. This is necessary because the
12533 -- tests of this flag are inserted out of sequence and must not
12534 -- pick up bogus indications of the wrong constant value.
12536 Set_Current_Value (Ent, Empty);
12538 -- If the subprogram is in the current declarative part and
12539 -- 'access has been applied to it, generate an elaboration
12540 -- check at the beginning of the declarations of the body.
12542 if Nkind (N) = N_Subprogram_Body
12543 and then Address_Taken (Spec_Id)
12544 and then
12545 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12546 then
12547 declare
12548 Loc : constant Source_Ptr := Sloc (N);
12549 Decls : constant List_Id := Declarations (N);
12550 Chk : Node_Id;
12552 begin
12553 -- No need to generate this check if first entry in the
12554 -- declaration list is a raise of Program_Error now.
12556 if Present (Decls)
12557 and then Nkind (First (Decls)) = N_Raise_Program_Error
12558 then
12559 return;
12560 end if;
12562 -- Otherwise generate the check
12564 Chk :=
12565 Make_Raise_Program_Error (Loc,
12566 Condition =>
12567 Make_Op_Eq (Loc,
12568 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12569 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12570 Reason => PE_Access_Before_Elaboration);
12572 if No (Decls) then
12573 Set_Declarations (N, New_List (Chk));
12574 else
12575 Prepend (Chk, Decls);
12576 end if;
12578 Analyze (Chk);
12579 end;
12580 end if;
12581 end if;
12582 end if;
12583 end Set_Elaboration_Flag;
12585 ----------------------------
12586 -- Set_Renamed_Subprogram --
12587 ----------------------------
12589 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12590 begin
12591 -- If input node is an identifier, we can just reset it
12593 if Nkind (N) = N_Identifier then
12594 Set_Chars (N, Chars (E));
12595 Set_Entity (N, E);
12597 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12599 else
12600 declare
12601 CS : constant Boolean := Comes_From_Source (N);
12602 begin
12603 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12604 Set_Entity (N, E);
12605 Set_Comes_From_Source (N, CS);
12606 Set_Analyzed (N, True);
12607 end;
12608 end if;
12609 end Set_Renamed_Subprogram;
12611 ----------------------
12612 -- Side_Effect_Free --
12613 ----------------------
12615 function Side_Effect_Free
12616 (N : Node_Id;
12617 Name_Req : Boolean := False;
12618 Variable_Ref : Boolean := False) return Boolean
12620 Typ : constant Entity_Id := Etype (N);
12621 -- Result type of the expression
12623 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12624 -- The argument N is a construct where the Prefix is dereferenced if it
12625 -- is an access type and the result is a variable. The call returns True
12626 -- if the construct is side effect free (not considering side effects in
12627 -- other than the prefix which are to be tested by the caller).
12629 function Within_In_Parameter (N : Node_Id) return Boolean;
12630 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12631 -- N is not side-effect free when the actual is global and modifiable
12632 -- indirectly from within a subprogram, because it may be passed by
12633 -- reference. The front-end must be conservative here and assume that
12634 -- this may happen with any array or record type. On the other hand, we
12635 -- cannot create temporaries for all expressions for which this
12636 -- condition is true, for various reasons that might require clearing up
12637 -- ??? For example, discriminant references that appear out of place, or
12638 -- spurious type errors with class-wide expressions. As a result, we
12639 -- limit the transformation to loop bounds, which is so far the only
12640 -- case that requires it.
12642 -----------------------------
12643 -- Safe_Prefixed_Reference --
12644 -----------------------------
12646 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12647 begin
12648 -- If prefix is not side effect free, definitely not safe
12650 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12651 return False;
12653 -- If the prefix is of an access type that is not access-to-constant,
12654 -- then this construct is a variable reference, which means it is to
12655 -- be considered to have side effects if Variable_Ref is set True.
12657 elsif Is_Access_Type (Etype (Prefix (N)))
12658 and then not Is_Access_Constant (Etype (Prefix (N)))
12659 and then Variable_Ref
12660 then
12661 -- Exception is a prefix that is the result of a previous removal
12662 -- of side effects.
12664 return Is_Entity_Name (Prefix (N))
12665 and then not Comes_From_Source (Prefix (N))
12666 and then Ekind (Entity (Prefix (N))) = E_Constant
12667 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12669 -- If the prefix is an explicit dereference then this construct is a
12670 -- variable reference, which means it is to be considered to have
12671 -- side effects if Variable_Ref is True.
12673 -- We do NOT exclude dereferences of access-to-constant types because
12674 -- we handle them as constant view of variables.
12676 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12677 and then Variable_Ref
12678 then
12679 return False;
12681 -- Note: The following test is the simplest way of solving a complex
12682 -- problem uncovered by the following test (Side effect on loop bound
12683 -- that is a subcomponent of a global variable:
12685 -- with Text_Io; use Text_Io;
12686 -- procedure Tloop is
12687 -- type X is
12688 -- record
12689 -- V : Natural := 4;
12690 -- S : String (1..5) := (others => 'a');
12691 -- end record;
12692 -- X1 : X;
12694 -- procedure Modi;
12696 -- generic
12697 -- with procedure Action;
12698 -- procedure Loop_G (Arg : X; Msg : String)
12700 -- procedure Loop_G (Arg : X; Msg : String) is
12701 -- begin
12702 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12703 -- & Natural'Image (Arg.V));
12704 -- for Index in 1 .. Arg.V loop
12705 -- Text_Io.Put_Line
12706 -- (Natural'Image (Index) & " " & Arg.S (Index));
12707 -- if Index > 2 then
12708 -- Modi;
12709 -- end if;
12710 -- end loop;
12711 -- Put_Line ("end loop_g " & Msg);
12712 -- end;
12714 -- procedure Loop1 is new Loop_G (Modi);
12715 -- procedure Modi is
12716 -- begin
12717 -- X1.V := 1;
12718 -- Loop1 (X1, "from modi");
12719 -- end;
12721 -- begin
12722 -- Loop1 (X1, "initial");
12723 -- end;
12725 -- The output of the above program should be:
12727 -- begin loop_g initial will loop till: 4
12728 -- 1 a
12729 -- 2 a
12730 -- 3 a
12731 -- begin loop_g from modi will loop till: 1
12732 -- 1 a
12733 -- end loop_g from modi
12734 -- 4 a
12735 -- begin loop_g from modi will loop till: 1
12736 -- 1 a
12737 -- end loop_g from modi
12738 -- end loop_g initial
12740 -- If a loop bound is a subcomponent of a global variable, a
12741 -- modification of that variable within the loop may incorrectly
12742 -- affect the execution of the loop.
12744 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12745 and then Within_In_Parameter (Prefix (N))
12746 and then Variable_Ref
12747 then
12748 return False;
12750 -- All other cases are side effect free
12752 else
12753 return True;
12754 end if;
12755 end Safe_Prefixed_Reference;
12757 -------------------------
12758 -- Within_In_Parameter --
12759 -------------------------
12761 function Within_In_Parameter (N : Node_Id) return Boolean is
12762 begin
12763 if not Comes_From_Source (N) then
12764 return False;
12766 elsif Is_Entity_Name (N) then
12767 return Ekind (Entity (N)) = E_In_Parameter;
12769 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12770 return Within_In_Parameter (Prefix (N));
12772 else
12773 return False;
12774 end if;
12775 end Within_In_Parameter;
12777 -- Start of processing for Side_Effect_Free
12779 begin
12780 -- If volatile reference, always consider it to have side effects
12782 if Is_Volatile_Reference (N) then
12783 return False;
12784 end if;
12786 -- Note on checks that could raise Constraint_Error. Strictly, if we
12787 -- take advantage of 11.6, these checks do not count as side effects.
12788 -- However, we would prefer to consider that they are side effects,
12789 -- since the back end CSE does not work very well on expressions which
12790 -- can raise Constraint_Error. On the other hand if we don't consider
12791 -- them to be side effect free, then we get some awkward expansions
12792 -- in -gnato mode, resulting in code insertions at a point where we
12793 -- do not have a clear model for performing the insertions.
12795 -- Special handling for entity names
12797 if Is_Entity_Name (N) then
12799 -- A type reference is always side effect free
12801 if Is_Type (Entity (N)) then
12802 return True;
12804 -- Variables are considered to be a side effect if Variable_Ref
12805 -- is set or if we have a volatile reference and Name_Req is off.
12806 -- If Name_Req is True then we can't help returning a name which
12807 -- effectively allows multiple references in any case.
12809 elsif Is_Variable (N, Use_Original_Node => False) then
12810 return not Variable_Ref
12811 and then (not Is_Volatile_Reference (N) or else Name_Req);
12813 -- Any other entity (e.g. a subtype name) is definitely side
12814 -- effect free.
12816 else
12817 return True;
12818 end if;
12820 -- A value known at compile time is always side effect free
12822 elsif Compile_Time_Known_Value (N) then
12823 return True;
12825 -- A variable renaming is not side-effect free, because the renaming
12826 -- will function like a macro in the front-end in some cases, and an
12827 -- assignment can modify the component designated by N, so we need to
12828 -- create a temporary for it.
12830 -- The guard testing for Entity being present is needed at least in
12831 -- the case of rewritten predicate expressions, and may well also be
12832 -- appropriate elsewhere. Obviously we can't go testing the entity
12833 -- field if it does not exist, so it's reasonable to say that this is
12834 -- not the renaming case if it does not exist.
12836 elsif Is_Entity_Name (Original_Node (N))
12837 and then Present (Entity (Original_Node (N)))
12838 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
12839 and then Ekind (Entity (Original_Node (N))) /= E_Constant
12840 then
12841 declare
12842 RO : constant Node_Id :=
12843 Renamed_Object (Entity (Original_Node (N)));
12845 begin
12846 -- If the renamed object is an indexed component, or an
12847 -- explicit dereference, then the designated object could
12848 -- be modified by an assignment.
12850 if Nkind_In (RO, N_Indexed_Component,
12851 N_Explicit_Dereference)
12852 then
12853 return False;
12855 -- A selected component must have a safe prefix
12857 elsif Nkind (RO) = N_Selected_Component then
12858 return Safe_Prefixed_Reference (RO);
12860 -- In all other cases, designated object cannot be changed so
12861 -- we are side effect free.
12863 else
12864 return True;
12865 end if;
12866 end;
12868 -- Remove_Side_Effects generates an object renaming declaration to
12869 -- capture the expression of a class-wide expression. In VM targets
12870 -- the frontend performs no expansion for dispatching calls to
12871 -- class- wide types since they are handled by the VM. Hence, we must
12872 -- locate here if this node corresponds to a previous invocation of
12873 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
12875 elsif not Tagged_Type_Expansion
12876 and then not Comes_From_Source (N)
12877 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
12878 and then Is_Class_Wide_Type (Typ)
12879 then
12880 return True;
12882 -- Generating C the type conversion of an access to constrained array
12883 -- type into an access to unconstrained array type involves initializing
12884 -- a fat pointer and the expression cannot be assumed to be free of side
12885 -- effects since it must referenced several times to compute its bounds.
12887 elsif Modify_Tree_For_C
12888 and then Nkind (N) = N_Type_Conversion
12889 and then Is_Access_Type (Typ)
12890 and then Is_Array_Type (Designated_Type (Typ))
12891 and then not Is_Constrained (Designated_Type (Typ))
12892 then
12893 return False;
12894 end if;
12896 -- For other than entity names and compile time known values,
12897 -- check the node kind for special processing.
12899 case Nkind (N) is
12901 -- An attribute reference is side effect free if its expressions
12902 -- are side effect free and its prefix is side effect free or
12903 -- is an entity reference.
12905 -- Is this right? what about x'first where x is a variable???
12907 when N_Attribute_Reference =>
12908 Attribute_Reference : declare
12910 function Side_Effect_Free_Attribute
12911 (Attribute_Name : Name_Id) return Boolean;
12912 -- Returns True if evaluation of the given attribute is
12913 -- considered side-effect free (independent of prefix and
12914 -- arguments).
12916 --------------------------------
12917 -- Side_Effect_Free_Attribute --
12918 --------------------------------
12920 function Side_Effect_Free_Attribute
12921 (Attribute_Name : Name_Id) return Boolean
12923 begin
12924 case Attribute_Name is
12925 when Name_Input =>
12926 return False;
12928 when Name_Image
12929 | Name_Img
12930 | Name_Wide_Image
12931 | Name_Wide_Wide_Image
12933 -- CodePeer doesn't want to see replicated copies of
12934 -- 'Image calls.
12936 return not CodePeer_Mode;
12938 when others =>
12939 return True;
12940 end case;
12941 end Side_Effect_Free_Attribute;
12943 -- Start of processing for Attribute_Reference
12945 begin
12946 return
12947 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12948 and then Side_Effect_Free_Attribute (Attribute_Name (N))
12949 and then (Is_Entity_Name (Prefix (N))
12950 or else Side_Effect_Free
12951 (Prefix (N), Name_Req, Variable_Ref));
12952 end Attribute_Reference;
12954 -- A binary operator is side effect free if and both operands are
12955 -- side effect free. For this purpose binary operators include
12956 -- membership tests and short circuit forms.
12958 when N_Binary_Op
12959 | N_Membership_Test
12960 | N_Short_Circuit
12962 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
12963 and then
12964 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
12966 -- An explicit dereference is side effect free only if it is
12967 -- a side effect free prefixed reference.
12969 when N_Explicit_Dereference =>
12970 return Safe_Prefixed_Reference (N);
12972 -- An expression with action is side effect free if its expression
12973 -- is side effect free and it has no actions.
12975 when N_Expression_With_Actions =>
12976 return
12977 Is_Empty_List (Actions (N))
12978 and then Side_Effect_Free
12979 (Expression (N), Name_Req, Variable_Ref);
12981 -- A call to _rep_to_pos is side effect free, since we generate
12982 -- this pure function call ourselves. Moreover it is critically
12983 -- important to make this exception, since otherwise we can have
12984 -- discriminants in array components which don't look side effect
12985 -- free in the case of an array whose index type is an enumeration
12986 -- type with an enumeration rep clause.
12988 -- All other function calls are not side effect free
12990 when N_Function_Call =>
12991 return
12992 Nkind (Name (N)) = N_Identifier
12993 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
12994 and then Side_Effect_Free
12995 (First (Parameter_Associations (N)),
12996 Name_Req, Variable_Ref);
12998 -- An IF expression is side effect free if it's of a scalar type, and
12999 -- all its components are all side effect free (conditions and then
13000 -- actions and else actions). We restrict to scalar types, since it
13001 -- is annoying to deal with things like (if A then B else C)'First
13002 -- where the type involved is a string type.
13004 when N_If_Expression =>
13005 return
13006 Is_Scalar_Type (Typ)
13007 and then Side_Effect_Free
13008 (Expressions (N), Name_Req, Variable_Ref);
13010 -- An indexed component is side effect free if it is a side
13011 -- effect free prefixed reference and all the indexing
13012 -- expressions are side effect free.
13014 when N_Indexed_Component =>
13015 return
13016 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13017 and then Safe_Prefixed_Reference (N);
13019 -- A type qualification, type conversion, or unchecked expression is
13020 -- side effect free if the expression is side effect free.
13022 when N_Qualified_Expression
13023 | N_Type_Conversion
13024 | N_Unchecked_Expression
13026 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13028 -- A selected component is side effect free only if it is a side
13029 -- effect free prefixed reference.
13031 when N_Selected_Component =>
13032 return Safe_Prefixed_Reference (N);
13034 -- A range is side effect free if the bounds are side effect free
13036 when N_Range =>
13037 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13038 and then
13039 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13041 -- A slice is side effect free if it is a side effect free
13042 -- prefixed reference and the bounds are side effect free.
13044 when N_Slice =>
13045 return
13046 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13047 and then Safe_Prefixed_Reference (N);
13049 -- A unary operator is side effect free if the operand
13050 -- is side effect free.
13052 when N_Unary_Op =>
13053 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13055 -- An unchecked type conversion is side effect free only if it
13056 -- is safe and its argument is side effect free.
13058 when N_Unchecked_Type_Conversion =>
13059 return
13060 Safe_Unchecked_Type_Conversion (N)
13061 and then Side_Effect_Free
13062 (Expression (N), Name_Req, Variable_Ref);
13064 -- A literal is side effect free
13066 when N_Character_Literal
13067 | N_Integer_Literal
13068 | N_Real_Literal
13069 | N_String_Literal
13071 return True;
13073 -- We consider that anything else has side effects. This is a bit
13074 -- crude, but we are pretty close for most common cases, and we
13075 -- are certainly correct (i.e. we never return True when the
13076 -- answer should be False).
13078 when others =>
13079 return False;
13080 end case;
13081 end Side_Effect_Free;
13083 -- A list is side effect free if all elements of the list are side
13084 -- effect free.
13086 function Side_Effect_Free
13087 (L : List_Id;
13088 Name_Req : Boolean := False;
13089 Variable_Ref : Boolean := False) return Boolean
13091 N : Node_Id;
13093 begin
13094 if L = No_List or else L = Error_List then
13095 return True;
13097 else
13098 N := First (L);
13099 while Present (N) loop
13100 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13101 return False;
13102 else
13103 Next (N);
13104 end if;
13105 end loop;
13107 return True;
13108 end if;
13109 end Side_Effect_Free;
13111 ----------------------------------
13112 -- Silly_Boolean_Array_Not_Test --
13113 ----------------------------------
13115 -- This procedure implements an odd and silly test. We explicitly check
13116 -- for the case where the 'First of the component type is equal to the
13117 -- 'Last of this component type, and if this is the case, we make sure
13118 -- that constraint error is raised. The reason is that the NOT is bound
13119 -- to cause CE in this case, and we will not otherwise catch it.
13121 -- No such check is required for AND and OR, since for both these cases
13122 -- False op False = False, and True op True = True. For the XOR case,
13123 -- see Silly_Boolean_Array_Xor_Test.
13125 -- Believe it or not, this was reported as a bug. Note that nearly always,
13126 -- the test will evaluate statically to False, so the code will be
13127 -- statically removed, and no extra overhead caused.
13129 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13130 Loc : constant Source_Ptr := Sloc (N);
13131 CT : constant Entity_Id := Component_Type (T);
13133 begin
13134 -- The check we install is
13136 -- constraint_error when
13137 -- component_type'first = component_type'last
13138 -- and then array_type'Length /= 0)
13140 -- We need the last guard because we don't want to raise CE for empty
13141 -- arrays since no out of range values result. (Empty arrays with a
13142 -- component type of True .. True -- very useful -- even the ACATS
13143 -- does not test that marginal case).
13145 Insert_Action (N,
13146 Make_Raise_Constraint_Error (Loc,
13147 Condition =>
13148 Make_And_Then (Loc,
13149 Left_Opnd =>
13150 Make_Op_Eq (Loc,
13151 Left_Opnd =>
13152 Make_Attribute_Reference (Loc,
13153 Prefix => New_Occurrence_Of (CT, Loc),
13154 Attribute_Name => Name_First),
13156 Right_Opnd =>
13157 Make_Attribute_Reference (Loc,
13158 Prefix => New_Occurrence_Of (CT, Loc),
13159 Attribute_Name => Name_Last)),
13161 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13162 Reason => CE_Range_Check_Failed));
13163 end Silly_Boolean_Array_Not_Test;
13165 ----------------------------------
13166 -- Silly_Boolean_Array_Xor_Test --
13167 ----------------------------------
13169 -- This procedure implements an odd and silly test. We explicitly check
13170 -- for the XOR case where the component type is True .. True, since this
13171 -- will raise constraint error. A special check is required since CE
13172 -- will not be generated otherwise (cf Expand_Packed_Not).
13174 -- No such check is required for AND and OR, since for both these cases
13175 -- False op False = False, and True op True = True, and no check is
13176 -- required for the case of False .. False, since False xor False = False.
13177 -- See also Silly_Boolean_Array_Not_Test
13179 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13180 Loc : constant Source_Ptr := Sloc (N);
13181 CT : constant Entity_Id := Component_Type (T);
13183 begin
13184 -- The check we install is
13186 -- constraint_error when
13187 -- Boolean (component_type'First)
13188 -- and then Boolean (component_type'Last)
13189 -- and then array_type'Length /= 0)
13191 -- We need the last guard because we don't want to raise CE for empty
13192 -- arrays since no out of range values result (Empty arrays with a
13193 -- component type of True .. True -- very useful -- even the ACATS
13194 -- does not test that marginal case).
13196 Insert_Action (N,
13197 Make_Raise_Constraint_Error (Loc,
13198 Condition =>
13199 Make_And_Then (Loc,
13200 Left_Opnd =>
13201 Make_And_Then (Loc,
13202 Left_Opnd =>
13203 Convert_To (Standard_Boolean,
13204 Make_Attribute_Reference (Loc,
13205 Prefix => New_Occurrence_Of (CT, Loc),
13206 Attribute_Name => Name_First)),
13208 Right_Opnd =>
13209 Convert_To (Standard_Boolean,
13210 Make_Attribute_Reference (Loc,
13211 Prefix => New_Occurrence_Of (CT, Loc),
13212 Attribute_Name => Name_Last))),
13214 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13215 Reason => CE_Range_Check_Failed));
13216 end Silly_Boolean_Array_Xor_Test;
13218 --------------------------
13219 -- Target_Has_Fixed_Ops --
13220 --------------------------
13222 Integer_Sized_Small : Ureal;
13223 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13224 -- called (we don't want to compute it more than once).
13226 Long_Integer_Sized_Small : Ureal;
13227 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13228 -- is called (we don't want to compute it more than once)
13230 First_Time_For_THFO : Boolean := True;
13231 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13233 function Target_Has_Fixed_Ops
13234 (Left_Typ : Entity_Id;
13235 Right_Typ : Entity_Id;
13236 Result_Typ : Entity_Id) return Boolean
13238 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13239 -- Return True if the given type is a fixed-point type with a small
13240 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13241 -- an absolute value less than 1.0. This is currently limited to
13242 -- fixed-point types that map to Integer or Long_Integer.
13244 ------------------------
13245 -- Is_Fractional_Type --
13246 ------------------------
13248 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13249 begin
13250 if Esize (Typ) = Standard_Integer_Size then
13251 return Small_Value (Typ) = Integer_Sized_Small;
13253 elsif Esize (Typ) = Standard_Long_Integer_Size then
13254 return Small_Value (Typ) = Long_Integer_Sized_Small;
13256 else
13257 return False;
13258 end if;
13259 end Is_Fractional_Type;
13261 -- Start of processing for Target_Has_Fixed_Ops
13263 begin
13264 -- Return False if Fractional_Fixed_Ops_On_Target is false
13266 if not Fractional_Fixed_Ops_On_Target then
13267 return False;
13268 end if;
13270 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13271 -- standard constants used by Is_Fractional_Type.
13273 if First_Time_For_THFO then
13274 First_Time_For_THFO := False;
13276 Integer_Sized_Small :=
13277 UR_From_Components
13278 (Num => Uint_1,
13279 Den => UI_From_Int (Standard_Integer_Size - 1),
13280 Rbase => 2);
13282 Long_Integer_Sized_Small :=
13283 UR_From_Components
13284 (Num => Uint_1,
13285 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13286 Rbase => 2);
13287 end if;
13289 -- Return True if target supports fixed-by-fixed multiply/divide for
13290 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13291 -- and result types are equivalent fractional types.
13293 return Is_Fractional_Type (Base_Type (Left_Typ))
13294 and then Is_Fractional_Type (Base_Type (Right_Typ))
13295 and then Is_Fractional_Type (Base_Type (Result_Typ))
13296 and then Esize (Left_Typ) = Esize (Right_Typ)
13297 and then Esize (Left_Typ) = Esize (Result_Typ);
13298 end Target_Has_Fixed_Ops;
13300 -------------------
13301 -- Type_Map_Hash --
13302 -------------------
13304 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13305 begin
13306 return Type_Map_Header (Id mod Type_Map_Size);
13307 end Type_Map_Hash;
13309 ------------------------------------------
13310 -- Type_May_Have_Bit_Aligned_Components --
13311 ------------------------------------------
13313 function Type_May_Have_Bit_Aligned_Components
13314 (Typ : Entity_Id) return Boolean
13316 begin
13317 -- Array type, check component type
13319 if Is_Array_Type (Typ) then
13320 return
13321 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13323 -- Record type, check components
13325 elsif Is_Record_Type (Typ) then
13326 declare
13327 E : Entity_Id;
13329 begin
13330 E := First_Component_Or_Discriminant (Typ);
13331 while Present (E) loop
13332 if Component_May_Be_Bit_Aligned (E)
13333 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13334 then
13335 return True;
13336 end if;
13338 Next_Component_Or_Discriminant (E);
13339 end loop;
13341 return False;
13342 end;
13344 -- Type other than array or record is always OK
13346 else
13347 return False;
13348 end if;
13349 end Type_May_Have_Bit_Aligned_Components;
13351 -------------------------------
13352 -- Update_Primitives_Mapping --
13353 -------------------------------
13355 procedure Update_Primitives_Mapping
13356 (Inher_Id : Entity_Id;
13357 Subp_Id : Entity_Id)
13359 begin
13360 Map_Types
13361 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13362 Derived_Type => Find_Dispatching_Type (Subp_Id));
13363 end Update_Primitives_Mapping;
13365 ----------------------------------
13366 -- Within_Case_Or_If_Expression --
13367 ----------------------------------
13369 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13370 Par : Node_Id;
13372 begin
13373 -- Locate an enclosing case or if expression. Note that these constructs
13374 -- can be expanded into Expression_With_Actions, hence the test of the
13375 -- original node.
13377 Par := Parent (N);
13378 while Present (Par) loop
13379 if Nkind_In (Original_Node (Par), N_Case_Expression,
13380 N_If_Expression)
13381 then
13382 return True;
13384 -- Prevent the search from going too far
13386 elsif Is_Body_Or_Package_Declaration (Par) then
13387 return False;
13388 end if;
13390 Par := Parent (Par);
13391 end loop;
13393 return False;
13394 end Within_Case_Or_If_Expression;
13396 --------------------------------
13397 -- Within_Internal_Subprogram --
13398 --------------------------------
13400 function Within_Internal_Subprogram return Boolean is
13401 S : Entity_Id;
13403 begin
13404 S := Current_Scope;
13405 while Present (S) and then not Is_Subprogram (S) loop
13406 S := Scope (S);
13407 end loop;
13409 return Present (S)
13410 and then Get_TSS_Name (S) /= TSS_Null
13411 and then not Is_Predicate_Function (S)
13412 and then not Is_Predicate_Function_M (S);
13413 end Within_Internal_Subprogram;
13415 end Exp_Util;