PR target/82524
[official-gcc.git] / gcc / ada / exp_util.adb
blobb1ab606f055169b80e305a0ed81ca14efbc17389
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 Find_DIC_Type (Typ : Entity_Id) return Entity_Id;
169 -- Subsidiary to all Build_DIC_Procedure_xxx routines. Find the type which
170 -- defines the Default_Initial_Condition pragma of type Typ. This is either
171 -- Typ itself or a parent type when the pragma is inherited.
173 function Make_CW_Equivalent_Type
174 (T : Entity_Id;
175 E : Node_Id) return Entity_Id;
176 -- T is a class-wide type entity, E is the initial expression node that
177 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
178 -- returns the entity of the Equivalent type and inserts on the fly the
179 -- necessary declaration such as:
181 -- type anon is record
182 -- _parent : Root_Type (T); constrained with E discriminants (if any)
183 -- Extension : String (1 .. expr to match size of E);
184 -- end record;
186 -- This record is compatible with any object of the class of T thanks to
187 -- the first field and has the same size as E thanks to the second.
189 function Make_Literal_Range
190 (Loc : Source_Ptr;
191 Literal_Typ : Entity_Id) return Node_Id;
192 -- Produce a Range node whose bounds are:
193 -- Low_Bound (Literal_Type) ..
194 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
195 -- this is used for expanding declarations like X : String := "sdfgdfg";
197 -- If the index type of the target array is not integer, we generate:
198 -- Low_Bound (Literal_Type) ..
199 -- Literal_Type'Val
200 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
201 -- + (Length (Literal_Typ) -1))
203 function Make_Non_Empty_Check
204 (Loc : Source_Ptr;
205 N : Node_Id) return Node_Id;
206 -- Produce a boolean expression checking that the unidimensional array
207 -- node N is not empty.
209 function New_Class_Wide_Subtype
210 (CW_Typ : Entity_Id;
211 N : Node_Id) return Entity_Id;
212 -- Create an implicit subtype of CW_Typ attached to node N
214 function Requires_Cleanup_Actions
215 (L : List_Id;
216 Lib_Level : Boolean;
217 Nested_Constructs : Boolean) return Boolean;
218 -- Given a list L, determine whether it contains one of the following:
220 -- 1) controlled objects
221 -- 2) library-level tagged types
223 -- Lib_Level is True when the list comes from a construct at the library
224 -- level, and False otherwise. Nested_Constructs is True when any nested
225 -- packages declared in L must be processed, and False otherwise.
227 -------------------------------------
228 -- Activate_Atomic_Synchronization --
229 -------------------------------------
231 procedure Activate_Atomic_Synchronization (N : Node_Id) is
232 Msg_Node : Node_Id;
234 begin
235 case Nkind (Parent (N)) is
237 -- Check for cases of appearing in the prefix of a construct where we
238 -- don't need atomic synchronization for this kind of usage.
240 when
241 -- Nothing to do if we are the prefix of an attribute, since we
242 -- do not want an atomic sync operation for things like 'Size.
244 N_Attribute_Reference
246 -- The N_Reference node is like an attribute
248 | N_Reference
250 -- Nothing to do for a reference to a component (or components)
251 -- of a composite object. Only reads and updates of the object
252 -- as a whole require atomic synchronization (RM C.6 (15)).
254 | N_Indexed_Component
255 | N_Selected_Component
256 | N_Slice
258 -- For all the above cases, nothing to do if we are the prefix
260 if Prefix (Parent (N)) = N then
261 return;
262 end if;
264 when others =>
265 null;
266 end case;
268 -- Nothing to do for the identifier in an object renaming declaration,
269 -- the renaming itself does not need atomic synchronization.
271 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
272 return;
273 end if;
275 -- Go ahead and set the flag
277 Set_Atomic_Sync_Required (N);
279 -- Generate info message if requested
281 if Warn_On_Atomic_Synchronization then
282 case Nkind (N) is
283 when N_Identifier =>
284 Msg_Node := N;
286 when N_Expanded_Name
287 | N_Selected_Component
289 Msg_Node := Selector_Name (N);
291 when N_Explicit_Dereference
292 | N_Indexed_Component
294 Msg_Node := Empty;
296 when others =>
297 pragma Assert (False);
298 return;
299 end case;
301 if Present (Msg_Node) then
302 Error_Msg_N
303 ("info: atomic synchronization set for &?N?", Msg_Node);
304 else
305 Error_Msg_N
306 ("info: atomic synchronization set?N?", N);
307 end if;
308 end if;
309 end Activate_Atomic_Synchronization;
311 ----------------------
312 -- Adjust_Condition --
313 ----------------------
315 procedure Adjust_Condition (N : Node_Id) is
316 begin
317 if No (N) then
318 return;
319 end if;
321 declare
322 Loc : constant Source_Ptr := Sloc (N);
323 T : constant Entity_Id := Etype (N);
324 Ti : Entity_Id;
326 begin
327 -- Defend against a call where the argument has no type, or has a
328 -- type that is not Boolean. This can occur because of prior errors.
330 if No (T) or else not Is_Boolean_Type (T) then
331 return;
332 end if;
334 -- Apply validity checking if needed
336 if Validity_Checks_On and Validity_Check_Tests then
337 Ensure_Valid (N);
338 end if;
340 -- Immediate return if standard boolean, the most common case,
341 -- where nothing needs to be done.
343 if Base_Type (T) = Standard_Boolean then
344 return;
345 end if;
347 -- Case of zero/non-zero semantics or non-standard enumeration
348 -- representation. In each case, we rewrite the node as:
350 -- ityp!(N) /= False'Enum_Rep
352 -- where ityp is an integer type with large enough size to hold any
353 -- value of type T.
355 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
356 if Esize (T) <= Esize (Standard_Integer) then
357 Ti := Standard_Integer;
358 else
359 Ti := Standard_Long_Long_Integer;
360 end if;
362 Rewrite (N,
363 Make_Op_Ne (Loc,
364 Left_Opnd => Unchecked_Convert_To (Ti, N),
365 Right_Opnd =>
366 Make_Attribute_Reference (Loc,
367 Attribute_Name => Name_Enum_Rep,
368 Prefix =>
369 New_Occurrence_Of (First_Literal (T), Loc))));
370 Analyze_And_Resolve (N, Standard_Boolean);
372 else
373 Rewrite (N, Convert_To (Standard_Boolean, N));
374 Analyze_And_Resolve (N, Standard_Boolean);
375 end if;
376 end;
377 end Adjust_Condition;
379 ------------------------
380 -- Adjust_Result_Type --
381 ------------------------
383 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
384 begin
385 -- Ignore call if current type is not Standard.Boolean
387 if Etype (N) /= Standard_Boolean then
388 return;
389 end if;
391 -- If result is already of correct type, nothing to do. Note that
392 -- this will get the most common case where everything has a type
393 -- of Standard.Boolean.
395 if Base_Type (T) = Standard_Boolean then
396 return;
398 else
399 declare
400 KP : constant Node_Kind := Nkind (Parent (N));
402 begin
403 -- If result is to be used as a Condition in the syntax, no need
404 -- to convert it back, since if it was changed to Standard.Boolean
405 -- using Adjust_Condition, that is just fine for this usage.
407 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
408 return;
410 -- If result is an operand of another logical operation, no need
411 -- to reset its type, since Standard.Boolean is just fine, and
412 -- such operations always do Adjust_Condition on their operands.
414 elsif KP in N_Op_Boolean
415 or else KP in N_Short_Circuit
416 or else KP = N_Op_Not
417 then
418 return;
420 -- Otherwise we perform a conversion from the current type, which
421 -- must be Standard.Boolean, to the desired type. Use the base
422 -- type to prevent spurious constraint checks that are extraneous
423 -- to the transformation. The type and its base have the same
424 -- representation, standard or otherwise.
426 else
427 Set_Analyzed (N);
428 Rewrite (N, Convert_To (Base_Type (T), N));
429 Analyze_And_Resolve (N, Base_Type (T));
430 end if;
431 end;
432 end if;
433 end Adjust_Result_Type;
435 --------------------------
436 -- Append_Freeze_Action --
437 --------------------------
439 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
440 Fnode : Node_Id;
442 begin
443 Ensure_Freeze_Node (T);
444 Fnode := Freeze_Node (T);
446 if No (Actions (Fnode)) then
447 Set_Actions (Fnode, New_List (N));
448 else
449 Append (N, Actions (Fnode));
450 end if;
452 end Append_Freeze_Action;
454 ---------------------------
455 -- Append_Freeze_Actions --
456 ---------------------------
458 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
459 Fnode : Node_Id;
461 begin
462 if No (L) then
463 return;
464 end if;
466 Ensure_Freeze_Node (T);
467 Fnode := Freeze_Node (T);
469 if No (Actions (Fnode)) then
470 Set_Actions (Fnode, L);
471 else
472 Append_List (L, Actions (Fnode));
473 end if;
474 end Append_Freeze_Actions;
476 ------------------------------------
477 -- Build_Allocate_Deallocate_Proc --
478 ------------------------------------
480 procedure Build_Allocate_Deallocate_Proc
481 (N : Node_Id;
482 Is_Allocate : Boolean)
484 function Find_Object (E : Node_Id) return Node_Id;
485 -- Given an arbitrary expression of an allocator, try to find an object
486 -- reference in it, otherwise return the original expression.
488 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
489 -- Determine whether subprogram Subp denotes a custom allocate or
490 -- deallocate.
492 -----------------
493 -- Find_Object --
494 -----------------
496 function Find_Object (E : Node_Id) return Node_Id is
497 Expr : Node_Id;
499 begin
500 pragma Assert (Is_Allocate);
502 Expr := E;
503 loop
504 if Nkind (Expr) = N_Explicit_Dereference then
505 Expr := Prefix (Expr);
507 elsif Nkind (Expr) = N_Qualified_Expression then
508 Expr := Expression (Expr);
510 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
512 -- When interface class-wide types are involved in allocation,
513 -- the expander introduces several levels of address arithmetic
514 -- to perform dispatch table displacement. In this scenario the
515 -- object appears as:
517 -- Tag_Ptr (Base_Address (<object>'Address))
519 -- Detect this case and utilize the whole expression as the
520 -- "object" since it now points to the proper dispatch table.
522 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
523 exit;
525 -- Continue to strip the object
527 else
528 Expr := Expression (Expr);
529 end if;
531 else
532 exit;
533 end if;
534 end loop;
536 return Expr;
537 end Find_Object;
539 ---------------------------------
540 -- Is_Allocate_Deallocate_Proc --
541 ---------------------------------
543 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
544 begin
545 -- Look for a subprogram body with only one statement which is a
546 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
548 if Ekind (Subp) = E_Procedure
549 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
550 then
551 declare
552 HSS : constant Node_Id :=
553 Handled_Statement_Sequence (Parent (Parent (Subp)));
554 Proc : Entity_Id;
556 begin
557 if Present (Statements (HSS))
558 and then Nkind (First (Statements (HSS))) =
559 N_Procedure_Call_Statement
560 then
561 Proc := Entity (Name (First (Statements (HSS))));
563 return
564 Is_RTE (Proc, RE_Allocate_Any_Controlled)
565 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
566 end if;
567 end;
568 end if;
570 return False;
571 end Is_Allocate_Deallocate_Proc;
573 -- Local variables
575 Desig_Typ : Entity_Id;
576 Expr : Node_Id;
577 Needs_Fin : Boolean;
578 Pool_Id : Entity_Id;
579 Proc_To_Call : Node_Id := Empty;
580 Ptr_Typ : Entity_Id;
582 -- Start of processing for Build_Allocate_Deallocate_Proc
584 begin
585 -- Obtain the attributes of the allocation / deallocation
587 if Nkind (N) = N_Free_Statement then
588 Expr := Expression (N);
589 Ptr_Typ := Base_Type (Etype (Expr));
590 Proc_To_Call := Procedure_To_Call (N);
592 else
593 if Nkind (N) = N_Object_Declaration then
594 Expr := Expression (N);
595 else
596 Expr := N;
597 end if;
599 -- In certain cases an allocator with a qualified expression may
600 -- be relocated and used as the initialization expression of a
601 -- temporary:
603 -- before:
604 -- Obj : Ptr_Typ := new Desig_Typ'(...);
606 -- after:
607 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
608 -- Obj : Ptr_Typ := Tmp;
610 -- Since the allocator is always marked as analyzed to avoid infinite
611 -- expansion, it will never be processed by this routine given that
612 -- the designated type needs finalization actions. Detect this case
613 -- and complete the expansion of the allocator.
615 if Nkind (Expr) = N_Identifier
616 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
617 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
618 then
619 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
620 return;
621 end if;
623 -- The allocator may have been rewritten into something else in which
624 -- case the expansion performed by this routine does not apply.
626 if Nkind (Expr) /= N_Allocator then
627 return;
628 end if;
630 Ptr_Typ := Base_Type (Etype (Expr));
631 Proc_To_Call := Procedure_To_Call (Expr);
632 end if;
634 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
635 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
637 -- Handle concurrent types
639 if Is_Concurrent_Type (Desig_Typ)
640 and then Present (Corresponding_Record_Type (Desig_Typ))
641 then
642 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
643 end if;
645 -- Do not process allocations / deallocations without a pool
647 if No (Pool_Id) then
648 return;
650 -- Do not process allocations on / deallocations from the secondary
651 -- stack.
653 elsif Is_RTE (Pool_Id, RE_SS_Pool)
654 or else (Nkind (Expr) = N_Allocator
655 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool))
656 then
657 return;
659 -- Optimize the case where we are using the default Global_Pool_Object,
660 -- and we don't need the heavy finalization machinery.
662 elsif Pool_Id = RTE (RE_Global_Pool_Object)
663 and then not Needs_Finalization (Desig_Typ)
664 then
665 return;
667 -- Do not replicate the machinery if the allocator / free has already
668 -- been expanded and has a custom Allocate / Deallocate.
670 elsif Present (Proc_To_Call)
671 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
672 then
673 return;
674 end if;
676 -- Finalization actions are required when the object to be allocated or
677 -- deallocated needs these actions and the associated access type is not
678 -- subject to pragma No_Heap_Finalization.
680 Needs_Fin :=
681 Needs_Finalization (Desig_Typ)
682 and then not No_Heap_Finalization (Ptr_Typ);
684 if Needs_Fin then
686 -- Certain run-time configurations and targets do not provide support
687 -- for controlled types.
689 if Restriction_Active (No_Finalization) then
690 return;
692 -- Do nothing if the access type may never allocate / deallocate
693 -- objects.
695 elsif No_Pool_Assigned (Ptr_Typ) then
696 return;
697 end if;
699 -- The allocation / deallocation of a controlled object must be
700 -- chained on / detached from a finalization master.
702 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
704 -- The only other kind of allocation / deallocation supported by this
705 -- routine is on / from a subpool.
707 elsif Nkind (Expr) = N_Allocator
708 and then No (Subpool_Handle_Name (Expr))
709 then
710 return;
711 end if;
713 declare
714 Loc : constant Source_Ptr := Sloc (N);
715 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
716 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
717 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
718 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
720 Actuals : List_Id;
721 Fin_Addr_Id : Entity_Id;
722 Fin_Mas_Act : Node_Id;
723 Fin_Mas_Id : Entity_Id;
724 Proc_To_Call : Entity_Id;
725 Subpool : Node_Id := Empty;
727 begin
728 -- Step 1: Construct all the actuals for the call to library routine
729 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
731 -- a) Storage pool
733 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
735 if Is_Allocate then
737 -- b) Subpool
739 if Nkind (Expr) = N_Allocator then
740 Subpool := Subpool_Handle_Name (Expr);
741 end if;
743 -- If a subpool is present it can be an arbitrary name, so make
744 -- the actual by copying the tree.
746 if Present (Subpool) then
747 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
748 else
749 Append_To (Actuals, Make_Null (Loc));
750 end if;
752 -- c) Finalization master
754 if Needs_Fin then
755 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
756 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
758 -- Handle the case where the master is actually a pointer to a
759 -- master. This case arises in build-in-place functions.
761 if Is_Access_Type (Etype (Fin_Mas_Id)) then
762 Append_To (Actuals, Fin_Mas_Act);
763 else
764 Append_To (Actuals,
765 Make_Attribute_Reference (Loc,
766 Prefix => Fin_Mas_Act,
767 Attribute_Name => Name_Unrestricted_Access));
768 end if;
769 else
770 Append_To (Actuals, Make_Null (Loc));
771 end if;
773 -- d) Finalize_Address
775 -- Primitive Finalize_Address is never generated in CodePeer mode
776 -- since it contains an Unchecked_Conversion.
778 if Needs_Fin and then not CodePeer_Mode then
779 Fin_Addr_Id := Finalize_Address (Desig_Typ);
780 pragma Assert (Present (Fin_Addr_Id));
782 Append_To (Actuals,
783 Make_Attribute_Reference (Loc,
784 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
785 Attribute_Name => Name_Unrestricted_Access));
786 else
787 Append_To (Actuals, Make_Null (Loc));
788 end if;
789 end if;
791 -- e) Address
792 -- f) Storage_Size
793 -- g) Alignment
795 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
796 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
798 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
799 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
801 -- For deallocation of class-wide types we obtain the value of
802 -- alignment from the Type Specific Record of the deallocated object.
803 -- This is needed because the frontend expansion of class-wide types
804 -- into equivalent types confuses the back end.
806 else
807 -- Generate:
808 -- Obj.all'Alignment
810 -- ... because 'Alignment applied to class-wide types is expanded
811 -- into the code that reads the value of alignment from the TSD
812 -- (see Expand_N_Attribute_Reference)
814 Append_To (Actuals,
815 Unchecked_Convert_To (RTE (RE_Storage_Offset),
816 Make_Attribute_Reference (Loc,
817 Prefix =>
818 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
819 Attribute_Name => Name_Alignment)));
820 end if;
822 -- h) Is_Controlled
824 if Needs_Fin then
825 Is_Controlled : declare
826 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
827 Flag_Expr : Node_Id;
828 Param : Node_Id;
829 Pref : Node_Id;
830 Temp : Node_Id;
832 begin
833 if Is_Allocate then
834 Temp := Find_Object (Expression (Expr));
835 else
836 Temp := Expr;
837 end if;
839 -- Processing for allocations where the expression is a subtype
840 -- indication.
842 if Is_Allocate
843 and then Is_Entity_Name (Temp)
844 and then Is_Type (Entity (Temp))
845 then
846 Flag_Expr :=
847 New_Occurrence_Of
848 (Boolean_Literals
849 (Needs_Finalization (Entity (Temp))), Loc);
851 -- The allocation / deallocation of a class-wide object relies
852 -- on a runtime check to determine whether the object is truly
853 -- controlled or not. Depending on this check, the finalization
854 -- machinery will request or reclaim extra storage reserved for
855 -- a list header.
857 elsif Is_Class_Wide_Type (Desig_Typ) then
859 -- Detect a special case where interface class-wide types
860 -- are involved as the object appears as:
862 -- Tag_Ptr (Base_Address (<object>'Address))
864 -- The expression already yields the proper tag, generate:
866 -- Temp.all
868 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
869 Param :=
870 Make_Explicit_Dereference (Loc,
871 Prefix => Relocate_Node (Temp));
873 -- In the default case, obtain the tag of the object about
874 -- to be allocated / deallocated. Generate:
876 -- Temp'Tag
878 -- If the object is an unchecked conversion (typically to
879 -- an access to class-wide type), we must preserve the
880 -- conversion to ensure that the object is seen as tagged
881 -- in the code that follows.
883 else
884 Pref := Temp;
886 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
887 then
888 Pref := Parent (Pref);
889 end if;
891 Param :=
892 Make_Attribute_Reference (Loc,
893 Prefix => Relocate_Node (Pref),
894 Attribute_Name => Name_Tag);
895 end if;
897 -- Generate:
898 -- Needs_Finalization (<Param>)
900 Flag_Expr :=
901 Make_Function_Call (Loc,
902 Name =>
903 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
904 Parameter_Associations => New_List (Param));
906 -- Processing for generic actuals
908 elsif Is_Generic_Actual_Type (Desig_Typ) then
909 Flag_Expr :=
910 New_Occurrence_Of (Boolean_Literals
911 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
913 -- The object does not require any specialized checks, it is
914 -- known to be controlled.
916 else
917 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
918 end if;
920 -- Create the temporary which represents the finalization state
921 -- of the expression. Generate:
923 -- F : constant Boolean := <Flag_Expr>;
925 Insert_Action (N,
926 Make_Object_Declaration (Loc,
927 Defining_Identifier => Flag_Id,
928 Constant_Present => True,
929 Object_Definition =>
930 New_Occurrence_Of (Standard_Boolean, Loc),
931 Expression => Flag_Expr));
933 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
934 end Is_Controlled;
936 -- The object is not controlled
938 else
939 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
940 end if;
942 -- i) On_Subpool
944 if Is_Allocate then
945 Append_To (Actuals,
946 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
947 end if;
949 -- Step 2: Build a wrapper Allocate / Deallocate which internally
950 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
952 -- Select the proper routine to call
954 if Is_Allocate then
955 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
956 else
957 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
958 end if;
960 -- Create a custom Allocate / Deallocate routine which has identical
961 -- profile to that of System.Storage_Pools.
963 Insert_Action (N,
964 Make_Subprogram_Body (Loc,
965 Specification =>
967 -- procedure Pnn
969 Make_Procedure_Specification (Loc,
970 Defining_Unit_Name => Proc_Id,
971 Parameter_Specifications => New_List (
973 -- P : Root_Storage_Pool
975 Make_Parameter_Specification (Loc,
976 Defining_Identifier => Make_Temporary (Loc, 'P'),
977 Parameter_Type =>
978 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
980 -- A : [out] Address
982 Make_Parameter_Specification (Loc,
983 Defining_Identifier => Addr_Id,
984 Out_Present => Is_Allocate,
985 Parameter_Type =>
986 New_Occurrence_Of (RTE (RE_Address), Loc)),
988 -- S : Storage_Count
990 Make_Parameter_Specification (Loc,
991 Defining_Identifier => Size_Id,
992 Parameter_Type =>
993 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
995 -- L : Storage_Count
997 Make_Parameter_Specification (Loc,
998 Defining_Identifier => Alig_Id,
999 Parameter_Type =>
1000 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
1002 Declarations => No_List,
1004 Handled_Statement_Sequence =>
1005 Make_Handled_Sequence_Of_Statements (Loc,
1006 Statements => New_List (
1007 Make_Procedure_Call_Statement (Loc,
1008 Name =>
1009 New_Occurrence_Of (Proc_To_Call, Loc),
1010 Parameter_Associations => Actuals)))),
1011 Suppress => All_Checks);
1013 -- The newly generated Allocate / Deallocate becomes the default
1014 -- procedure to call when the back end processes the allocation /
1015 -- deallocation.
1017 if Is_Allocate then
1018 Set_Procedure_To_Call (Expr, Proc_Id);
1019 else
1020 Set_Procedure_To_Call (N, Proc_Id);
1021 end if;
1022 end;
1023 end Build_Allocate_Deallocate_Proc;
1025 -------------------------------
1026 -- Build_Abort_Undefer_Block --
1027 -------------------------------
1029 function Build_Abort_Undefer_Block
1030 (Loc : Source_Ptr;
1031 Stmts : List_Id;
1032 Context : Node_Id) return Node_Id
1034 Exceptions_OK : constant Boolean :=
1035 not Restriction_Active (No_Exception_Propagation);
1037 AUD : Entity_Id;
1038 Blk : Node_Id;
1039 Blk_Id : Entity_Id;
1040 HSS : Node_Id;
1042 begin
1043 -- The block should be generated only when undeferring abort in the
1044 -- context of a potential exception.
1046 pragma Assert (Abort_Allowed and Exceptions_OK);
1048 -- Generate:
1049 -- begin
1050 -- <Stmts>
1051 -- at end
1052 -- Abort_Undefer_Direct;
1053 -- end;
1055 AUD := RTE (RE_Abort_Undefer_Direct);
1057 HSS :=
1058 Make_Handled_Sequence_Of_Statements (Loc,
1059 Statements => Stmts,
1060 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1062 Blk :=
1063 Make_Block_Statement (Loc,
1064 Handled_Statement_Sequence => HSS);
1065 Set_Is_Abort_Block (Blk);
1067 Add_Block_Identifier (Blk, Blk_Id);
1068 Expand_At_End_Handler (HSS, Blk_Id);
1070 -- Present the Abort_Undefer_Direct function to the back end to inline
1071 -- the call to the routine.
1073 Add_Inlined_Body (AUD, Context);
1075 return Blk;
1076 end Build_Abort_Undefer_Block;
1078 ---------------------------------
1079 -- Build_Class_Wide_Expression --
1080 ---------------------------------
1082 procedure Build_Class_Wide_Expression
1083 (Prag : Node_Id;
1084 Subp : Entity_Id;
1085 Par_Subp : Entity_Id;
1086 Adjust_Sloc : Boolean;
1087 Needs_Wrapper : out Boolean)
1089 function Replace_Entity (N : Node_Id) return Traverse_Result;
1090 -- Replace reference to formal of inherited operation or to primitive
1091 -- operation of root type, with corresponding entity for derived type,
1092 -- when constructing the class-wide condition of an overriding
1093 -- subprogram.
1095 --------------------
1096 -- Replace_Entity --
1097 --------------------
1099 function Replace_Entity (N : Node_Id) return Traverse_Result is
1100 New_E : Entity_Id;
1102 begin
1103 if Adjust_Sloc then
1104 Adjust_Inherited_Pragma_Sloc (N);
1105 end if;
1107 if Nkind (N) = N_Identifier
1108 and then Present (Entity (N))
1109 and then
1110 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1111 and then
1112 (Nkind (Parent (N)) /= N_Attribute_Reference
1113 or else Attribute_Name (Parent (N)) /= Name_Class)
1114 then
1115 -- The replacement does not apply to dispatching calls within the
1116 -- condition, but only to calls whose static tag is that of the
1117 -- parent type.
1119 if Is_Subprogram (Entity (N))
1120 and then Nkind (Parent (N)) = N_Function_Call
1121 and then Present (Controlling_Argument (Parent (N)))
1122 then
1123 return OK;
1124 end if;
1126 -- Determine whether entity has a renaming
1128 New_E := Type_Map.Get (Entity (N));
1130 if Present (New_E) then
1131 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1133 -- If the entity is an overridden primitive and we are not
1134 -- in GNATprove mode, we must build a wrapper for the current
1135 -- inherited operation. If the reference is the prefix of an
1136 -- attribute such as 'Result (or others ???) there is no need
1137 -- for a wrapper: the condition is just rewritten in terms of
1138 -- the inherited subprogram.
1140 if Is_Subprogram (New_E)
1141 and then Nkind (Parent (N)) /= N_Attribute_Reference
1142 and then not GNATprove_Mode
1143 then
1144 Needs_Wrapper := True;
1145 end if;
1146 end if;
1148 -- Check that there are no calls left to abstract operations if
1149 -- the current subprogram is not abstract.
1151 if Nkind (Parent (N)) = N_Function_Call
1152 and then N = Name (Parent (N))
1153 then
1154 if not Is_Abstract_Subprogram (Subp)
1155 and then Is_Abstract_Subprogram (Entity (N))
1156 then
1157 Error_Msg_Sloc := Sloc (Current_Scope);
1158 Error_Msg_Node_2 := Subp;
1159 if Comes_From_Source (Subp) then
1160 Error_Msg_NE
1161 ("cannot call abstract subprogram & in inherited "
1162 & "condition for&#", Subp, Entity (N));
1163 else
1164 Error_Msg_NE
1165 ("cannot call abstract subprogram & in inherited "
1166 & "condition for inherited&#", Subp, Entity (N));
1167 end if;
1169 -- In SPARK mode, reject an inherited condition for an
1170 -- inherited operation if it contains a call to an overriding
1171 -- operation, because this implies that the pre/postconditions
1172 -- of the inherited operation have changed silently.
1174 elsif SPARK_Mode = On
1175 and then Warn_On_Suspicious_Contract
1176 and then Present (Alias (Subp))
1177 and then Present (New_E)
1178 and then Comes_From_Source (New_E)
1179 then
1180 Error_Msg_N
1181 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1182 Parent (Subp));
1183 Error_Msg_Sloc := Sloc (New_E);
1184 Error_Msg_Node_2 := Subp;
1185 Error_Msg_NE
1186 ("\overriding of&# forces overriding of&",
1187 Parent (Subp), New_E);
1188 end if;
1189 end if;
1191 -- Update type of function call node, which should be the same as
1192 -- the function's return type.
1194 if Is_Subprogram (Entity (N))
1195 and then Nkind (Parent (N)) = N_Function_Call
1196 then
1197 Set_Etype (Parent (N), Etype (Entity (N)));
1198 end if;
1200 -- The whole expression will be reanalyzed
1202 elsif Nkind (N) in N_Has_Etype then
1203 Set_Analyzed (N, False);
1204 end if;
1206 return OK;
1207 end Replace_Entity;
1209 procedure Replace_Condition_Entities is
1210 new Traverse_Proc (Replace_Entity);
1212 -- Local variables
1214 Par_Formal : Entity_Id;
1215 Subp_Formal : Entity_Id;
1217 -- Start of processing for Build_Class_Wide_Expression
1219 begin
1220 Needs_Wrapper := False;
1222 -- Add mapping from old formals to new formals
1224 Par_Formal := First_Formal (Par_Subp);
1225 Subp_Formal := First_Formal (Subp);
1227 while Present (Par_Formal) and then Present (Subp_Formal) loop
1228 Type_Map.Set (Par_Formal, Subp_Formal);
1229 Next_Formal (Par_Formal);
1230 Next_Formal (Subp_Formal);
1231 end loop;
1233 Replace_Condition_Entities (Prag);
1234 end Build_Class_Wide_Expression;
1236 --------------------
1237 -- Build_DIC_Call --
1238 --------------------
1240 function Build_DIC_Call
1241 (Loc : Source_Ptr;
1242 Obj_Id : Entity_Id;
1243 Typ : Entity_Id) return Node_Id
1245 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1246 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1248 begin
1249 return
1250 Make_Procedure_Call_Statement (Loc,
1251 Name => New_Occurrence_Of (Proc_Id, Loc),
1252 Parameter_Associations => New_List (
1253 Make_Unchecked_Type_Conversion (Loc,
1254 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1255 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1256 end Build_DIC_Call;
1258 ------------------------------
1259 -- Build_DIC_Procedure_Body --
1260 ------------------------------
1262 -- WARNING: This routine manages Ghost regions. Return statements must be
1263 -- replaced by gotos which jump to the end of the routine and restore the
1264 -- Ghost mode.
1266 procedure Build_DIC_Procedure_Body
1267 (Typ : Entity_Id;
1268 For_Freeze : Boolean := False)
1270 procedure Add_DIC_Check
1271 (DIC_Prag : Node_Id;
1272 DIC_Expr : Node_Id;
1273 Stmts : in out List_Id);
1274 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1275 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1276 -- is added to list Stmts.
1278 procedure Add_Inherited_DIC
1279 (DIC_Prag : Node_Id;
1280 Par_Typ : Entity_Id;
1281 Deriv_Typ : Entity_Id;
1282 Stmts : in out List_Id);
1283 -- Add a runtime check to verify the assertion expression of inherited
1284 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1285 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1286 -- pragma. All generated code is added to list Stmts.
1288 procedure Add_Inherited_Tagged_DIC
1289 (DIC_Prag : Node_Id;
1290 Par_Typ : Entity_Id;
1291 Deriv_Typ : Entity_Id;
1292 Stmts : in out List_Id);
1293 -- Add a runtime check to verify assertion expression DIC_Expr of
1294 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1295 -- postcondition-like runtime semantics to the check. Par_Typ is the
1296 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1297 -- derived type inheriting the DIC pragma. All generated code is added
1298 -- to list Stmts.
1300 procedure Add_Own_DIC
1301 (DIC_Prag : Node_Id;
1302 DIC_Typ : Entity_Id;
1303 Stmts : in out List_Id);
1304 -- Add a runtime check to verify the assertion expression of pragma
1305 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1306 -- is added to list Stmts.
1308 -------------------
1309 -- Add_DIC_Check --
1310 -------------------
1312 procedure Add_DIC_Check
1313 (DIC_Prag : Node_Id;
1314 DIC_Expr : Node_Id;
1315 Stmts : in out List_Id)
1317 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1318 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1320 begin
1321 -- The DIC pragma is ignored, nothing left to do
1323 if Is_Ignored (DIC_Prag) then
1324 null;
1326 -- Otherwise the DIC expression must be checked at run time.
1327 -- Generate:
1329 -- pragma Check (<Nam>, <DIC_Expr>);
1331 else
1332 Append_New_To (Stmts,
1333 Make_Pragma (Loc,
1334 Pragma_Identifier =>
1335 Make_Identifier (Loc, Name_Check),
1337 Pragma_Argument_Associations => New_List (
1338 Make_Pragma_Argument_Association (Loc,
1339 Expression => Make_Identifier (Loc, Nam)),
1341 Make_Pragma_Argument_Association (Loc,
1342 Expression => DIC_Expr))));
1343 end if;
1344 end Add_DIC_Check;
1346 -----------------------
1347 -- Add_Inherited_DIC --
1348 -----------------------
1350 procedure Add_Inherited_DIC
1351 (DIC_Prag : Node_Id;
1352 Par_Typ : Entity_Id;
1353 Deriv_Typ : Entity_Id;
1354 Stmts : in out List_Id)
1356 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1357 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1358 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1359 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1360 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1362 begin
1363 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1365 -- Verify the inherited DIC assertion expression by calling the DIC
1366 -- procedure of the parent type.
1368 -- Generate:
1369 -- <Par_Typ>DIC (Par_Typ (_object));
1371 Append_New_To (Stmts,
1372 Make_Procedure_Call_Statement (Loc,
1373 Name => New_Occurrence_Of (Par_Proc, Loc),
1374 Parameter_Associations => New_List (
1375 Convert_To
1376 (Typ => Etype (Par_Obj),
1377 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1378 end Add_Inherited_DIC;
1380 ------------------------------
1381 -- Add_Inherited_Tagged_DIC --
1382 ------------------------------
1384 procedure Add_Inherited_Tagged_DIC
1385 (DIC_Prag : Node_Id;
1386 Par_Typ : Entity_Id;
1387 Deriv_Typ : Entity_Id;
1388 Stmts : in out List_Id)
1390 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1391 DIC_Args : constant List_Id :=
1392 Pragma_Argument_Associations (DIC_Prag);
1393 DIC_Arg : constant Node_Id := First (DIC_Args);
1394 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1395 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1397 Expr : Node_Id;
1399 begin
1400 -- The processing of an inherited DIC assertion expression starts off
1401 -- with a copy of the original parent expression where all references
1402 -- to the parent type have already been replaced with references to
1403 -- the _object formal parameter of the parent type's DIC procedure.
1405 pragma Assert (Present (DIC_Expr));
1406 Expr := New_Copy_Tree (DIC_Expr);
1408 -- Perform the following substitutions:
1410 -- * Replace a reference to the _object parameter of the parent
1411 -- type's DIC procedure with a reference to the _object parameter
1412 -- of the derived types' DIC procedure.
1414 -- * Replace a reference to a discriminant of the parent type with
1415 -- a suitable value from the point of view of the derived type.
1417 -- * Replace a call to an overridden parent primitive with a call
1418 -- to the overriding derived type primitive.
1420 -- * Replace a call to an inherited parent primitive with a call to
1421 -- the internally-generated inherited derived type primitive.
1423 -- Note that primitives defined in the private part are automatically
1424 -- handled by the overriding/inheritance mechanism and do not require
1425 -- an extra replacement pass.
1427 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1429 Replace_References
1430 (Expr => Expr,
1431 Par_Typ => Par_Typ,
1432 Deriv_Typ => Deriv_Typ,
1433 Par_Obj => First_Formal (Par_Proc),
1434 Deriv_Obj => First_Formal (Deriv_Proc));
1436 -- Once the DIC assertion expression is fully processed, add a check
1437 -- to the statements of the DIC procedure.
1439 Add_DIC_Check
1440 (DIC_Prag => DIC_Prag,
1441 DIC_Expr => Expr,
1442 Stmts => Stmts);
1443 end Add_Inherited_Tagged_DIC;
1445 -----------------
1446 -- Add_Own_DIC --
1447 -----------------
1449 procedure Add_Own_DIC
1450 (DIC_Prag : Node_Id;
1451 DIC_Typ : Entity_Id;
1452 Stmts : in out List_Id)
1454 DIC_Args : constant List_Id :=
1455 Pragma_Argument_Associations (DIC_Prag);
1456 DIC_Arg : constant Node_Id := First (DIC_Args);
1457 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1458 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1459 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1460 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1462 procedure Preanalyze_Own_DIC_For_ASIS;
1463 -- Preanalyze the original DIC expression of an aspect or a source
1464 -- pragma for ASIS.
1466 ---------------------------------
1467 -- Preanalyze_Own_DIC_For_ASIS --
1468 ---------------------------------
1470 procedure Preanalyze_Own_DIC_For_ASIS is
1471 Expr : Node_Id := Empty;
1473 begin
1474 -- The DIC pragma is a source construct, preanalyze the original
1475 -- expression of the pragma.
1477 if Comes_From_Source (DIC_Prag) then
1478 Expr := DIC_Expr;
1480 -- Otherwise preanalyze the expression of the corresponding aspect
1482 elsif Present (DIC_Asp) then
1483 Expr := Expression (DIC_Asp);
1484 end if;
1486 -- The expression must be subjected to the same substitutions as
1487 -- the copy used in the generation of the runtime check.
1489 if Present (Expr) then
1490 Replace_Type_References
1491 (Expr => Expr,
1492 Typ => DIC_Typ,
1493 Obj_Id => Obj_Id);
1495 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1496 end if;
1497 end Preanalyze_Own_DIC_For_ASIS;
1499 -- Local variables
1501 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1503 Expr : Node_Id;
1505 -- Start of processing for Add_Own_DIC
1507 begin
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
1741 -- * Pragma DIC appears without an argument
1742 -- * Pragma DIC appears with argument "null"
1744 if No (Stmts) then
1745 Stmts := New_List (Make_Null_Statement (Loc));
1746 end if;
1748 -- Generate:
1749 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1750 -- begin
1751 -- <Stmts>
1752 -- end <Work_Typ>DIC;
1754 Proc_Body :=
1755 Make_Subprogram_Body (Loc,
1756 Specification =>
1757 Copy_Subprogram_Spec (Parent (Proc_Id)),
1758 Declarations => Empty_List,
1759 Handled_Statement_Sequence =>
1760 Make_Handled_Sequence_Of_Statements (Loc,
1761 Statements => Stmts));
1762 Proc_Body_Id := Defining_Entity (Proc_Body);
1764 -- Perform minor decoration in case the body is not analyzed
1766 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1767 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1768 Set_Scope (Proc_Body_Id, Current_Scope);
1769 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
1770 Set_SPARK_Pragma_Inherited
1771 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
1773 -- Link both spec and body to avoid generating duplicates
1775 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1776 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1778 -- The body should not be inserted into the tree when the context
1779 -- is ASIS or a generic unit because it is not part of the template.
1780 -- Note that the body must still be generated in order to resolve the
1781 -- DIC assertion expression.
1783 if ASIS_Mode or Inside_A_Generic then
1784 null;
1786 -- Semi-insert the body into the tree for GNATprove by setting its
1787 -- Parent field. This allows for proper upstream tree traversals.
1789 elsif GNATprove_Mode then
1790 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1792 -- Otherwise the body is part of the freezing actions of the working
1793 -- type.
1795 else
1796 Append_Freeze_Action (Work_Typ, Proc_Body);
1797 end if;
1798 end if;
1800 <<Leave>>
1801 Restore_Ghost_Mode (Saved_GM);
1802 end Build_DIC_Procedure_Body;
1804 -------------------------------------
1805 -- Build_DIC_Procedure_Declaration --
1806 -------------------------------------
1808 -- WARNING: This routine manages Ghost regions. Return statements must be
1809 -- replaced by gotos which jump to the end of the routine and restore the
1810 -- Ghost mode.
1812 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1813 Loc : constant Source_Ptr := Sloc (Typ);
1815 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1816 -- Save the Ghost mode to restore on exit
1818 DIC_Prag : Node_Id;
1819 DIC_Typ : Entity_Id;
1820 Proc_Decl : Node_Id;
1821 Proc_Id : Entity_Id;
1822 Typ_Decl : Node_Id;
1824 CRec_Typ : Entity_Id;
1825 -- The corresponding record type of Full_Typ
1827 Full_Base : Entity_Id;
1828 -- The base type of Full_Typ
1830 Full_Typ : Entity_Id;
1831 -- The full view of working type
1833 Obj_Id : Entity_Id;
1834 -- The _object formal parameter of the DIC procedure
1836 Priv_Typ : Entity_Id;
1837 -- The partial view of working type
1839 Work_Typ : Entity_Id;
1840 -- The working type
1842 begin
1843 Work_Typ := Base_Type (Typ);
1845 -- Do not process class-wide types as these are Itypes, but lack a first
1846 -- subtype (see below).
1848 if Is_Class_Wide_Type (Work_Typ) then
1849 return;
1851 -- Do not process the underlying full view of a private type. There is
1852 -- no way to get back to the partial view, plus the body will be built
1853 -- by the full view or the base type.
1855 elsif Is_Underlying_Full_View (Work_Typ) then
1856 return;
1858 -- Use the first subtype when dealing with various base types
1860 elsif Is_Itype (Work_Typ) then
1861 Work_Typ := First_Subtype (Work_Typ);
1863 -- The input denotes the corresponding record type of a protected or a
1864 -- task type. Work with the concurrent type because the corresponding
1865 -- record type may not be visible to clients of the type.
1867 elsif Ekind (Work_Typ) = E_Record_Type
1868 and then Is_Concurrent_Record_Type (Work_Typ)
1869 then
1870 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1871 end if;
1873 -- The working type may be subject to pragma Ghost. Set the mode now to
1874 -- ensure that the DIC procedure is properly marked as Ghost.
1876 Set_Ghost_Mode (Work_Typ);
1878 -- The type must be either subject to a DIC pragma or inherit one from a
1879 -- parent type.
1881 pragma Assert (Has_DIC (Work_Typ));
1883 -- Recover the type which defines the DIC pragma. This is either the
1884 -- working type itself or a parent type when the pragma is inherited.
1886 DIC_Typ := Find_DIC_Type (Work_Typ);
1887 pragma Assert (Present (DIC_Typ));
1889 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1890 pragma Assert (Present (DIC_Prag));
1892 -- Nothing to do if pragma DIC appears without an argument or its sole
1893 -- argument is "null".
1895 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1896 goto Leave;
1898 -- Nothing to do if the type already has a DIC procedure
1900 elsif Present (DIC_Procedure (Work_Typ)) then
1901 goto Leave;
1902 end if;
1904 Proc_Id :=
1905 Make_Defining_Identifier (Loc,
1906 Chars =>
1907 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1909 -- Perform minor decoration in case the declaration is not analyzed
1911 Set_Ekind (Proc_Id, E_Procedure);
1912 Set_Etype (Proc_Id, Standard_Void_Type);
1913 Set_Is_DIC_Procedure (Proc_Id);
1914 Set_Scope (Proc_Id, Current_Scope);
1915 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
1916 Set_SPARK_Pragma_Inherited (Proc_Id);
1918 Set_DIC_Procedure (Work_Typ, Proc_Id);
1920 -- The DIC procedure requires debug info when the assertion expression
1921 -- is subject to Source Coverage Obligations.
1923 if Generate_SCO then
1924 Set_Needs_Debug_Info (Proc_Id);
1925 end if;
1927 -- Obtain all views of the input type
1929 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1931 -- Associate the DIC procedure and various relevant flags with all views
1933 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1934 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1935 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1936 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1938 -- The declaration of the DIC procedure must be inserted after the
1939 -- declaration of the partial view as this allows for proper external
1940 -- visibility.
1942 if Present (Priv_Typ) then
1943 Typ_Decl := Declaration_Node (Priv_Typ);
1945 -- Derived types with the full view as parent do not have a partial
1946 -- view. Insert the DIC procedure after the derived type.
1948 else
1949 Typ_Decl := Declaration_Node (Full_Typ);
1950 end if;
1952 -- The type should have a declarative node
1954 pragma Assert (Present (Typ_Decl));
1956 -- Create the formal parameter which emulates the variable-like behavior
1957 -- of the type's current instance.
1959 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1961 -- Perform minor decoration in case the declaration is not analyzed
1963 Set_Ekind (Obj_Id, E_In_Parameter);
1964 Set_Etype (Obj_Id, Work_Typ);
1965 Set_Scope (Obj_Id, Proc_Id);
1967 Set_First_Entity (Proc_Id, Obj_Id);
1969 -- Generate:
1970 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1972 Proc_Decl :=
1973 Make_Subprogram_Declaration (Loc,
1974 Specification =>
1975 Make_Procedure_Specification (Loc,
1976 Defining_Unit_Name => Proc_Id,
1977 Parameter_Specifications => New_List (
1978 Make_Parameter_Specification (Loc,
1979 Defining_Identifier => Obj_Id,
1980 Parameter_Type =>
1981 New_Occurrence_Of (Work_Typ, Loc)))));
1983 -- The declaration should not be inserted into the tree when the context
1984 -- is ASIS or a generic unit because it is not part of the template.
1986 if ASIS_Mode or Inside_A_Generic then
1987 null;
1989 -- Semi-insert the declaration into the tree for GNATprove by setting
1990 -- its Parent field. This allows for proper upstream tree traversals.
1992 elsif GNATprove_Mode then
1993 Set_Parent (Proc_Decl, Parent (Typ_Decl));
1995 -- Otherwise insert the declaration
1997 else
1998 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
1999 end if;
2001 <<Leave>>
2002 Restore_Ghost_Mode (Saved_GM);
2003 end Build_DIC_Procedure_Declaration;
2005 ------------------------------------
2006 -- Build_Invariant_Procedure_Body --
2007 ------------------------------------
2009 -- WARNING: This routine manages Ghost regions. Return statements must be
2010 -- replaced by gotos which jump to the end of the routine and restore the
2011 -- Ghost mode.
2013 procedure Build_Invariant_Procedure_Body
2014 (Typ : Entity_Id;
2015 Partial_Invariant : Boolean := False)
2017 Loc : constant Source_Ptr := Sloc (Typ);
2019 Pragmas_Seen : Elist_Id := No_Elist;
2020 -- This list contains all invariant pragmas processed so far. The list
2021 -- is used to avoid generating redundant invariant checks.
2023 Produced_Check : Boolean := False;
2024 -- This flag tracks whether the type has produced at least one invariant
2025 -- check. The flag is used as a sanity check at the end of the routine.
2027 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2028 -- intentionally unnested to avoid deep indentation of code.
2030 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2031 -- they emit checks, loops (for arrays) and case statements (for record
2032 -- variant parts) only when there are invariants to verify. This keeps
2033 -- the body of the invariant procedure free of useless code.
2035 procedure Add_Array_Component_Invariants
2036 (T : Entity_Id;
2037 Obj_Id : Entity_Id;
2038 Checks : in out List_Id);
2039 -- Generate an invariant check for each component of array type T.
2040 -- Obj_Id denotes the entity of the _object formal parameter of the
2041 -- invariant procedure. All created checks are added to list Checks.
2043 procedure Add_Inherited_Invariants
2044 (T : Entity_Id;
2045 Priv_Typ : Entity_Id;
2046 Full_Typ : Entity_Id;
2047 Obj_Id : Entity_Id;
2048 Checks : in out List_Id);
2049 -- Generate an invariant check for each inherited class-wide invariant
2050 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2051 -- the partial and full view of the parent type. Obj_Id denotes the
2052 -- entity of the _object formal parameter of the invariant procedure.
2053 -- All created checks are added to list Checks.
2055 procedure Add_Interface_Invariants
2056 (T : Entity_Id;
2057 Obj_Id : Entity_Id;
2058 Checks : in out List_Id);
2059 -- Generate an invariant check for each inherited class-wide invariant
2060 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2061 -- entity of the _object formal parameter of the invariant procedure.
2062 -- All created checks are added to list Checks.
2064 procedure Add_Invariant_Check
2065 (Prag : Node_Id;
2066 Expr : Node_Id;
2067 Checks : in out List_Id;
2068 Inherited : Boolean := False);
2069 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2070 -- verify assertion expression Expr of pragma Prag. All generated code
2071 -- is added to list Checks. Flag Inherited should be set when the pragma
2072 -- is inherited from a parent or interface type.
2074 procedure Add_Own_Invariants
2075 (T : Entity_Id;
2076 Obj_Id : Entity_Id;
2077 Checks : in out List_Id;
2078 Priv_Item : Node_Id := Empty);
2079 -- Generate an invariant check for each invariant found for type T.
2080 -- Obj_Id denotes the entity of the _object formal parameter of the
2081 -- invariant procedure. All created checks are added to list Checks.
2082 -- Priv_Item denotes the first rep item of the private type.
2084 procedure Add_Parent_Invariants
2085 (T : Entity_Id;
2086 Obj_Id : Entity_Id;
2087 Checks : in out List_Id);
2088 -- Generate an invariant check for each inherited class-wide invariant
2089 -- coming from all parent types of type T. Obj_Id denotes the entity of
2090 -- the _object formal parameter of the invariant procedure. All created
2091 -- checks are added to list Checks.
2093 procedure Add_Record_Component_Invariants
2094 (T : Entity_Id;
2095 Obj_Id : Entity_Id;
2096 Checks : in out List_Id);
2097 -- Generate an invariant check for each component of record type T.
2098 -- Obj_Id denotes the entity of the _object formal parameter of the
2099 -- invariant procedure. All created checks are added to list Checks.
2101 ------------------------------------
2102 -- Add_Array_Component_Invariants --
2103 ------------------------------------
2105 procedure Add_Array_Component_Invariants
2106 (T : Entity_Id;
2107 Obj_Id : Entity_Id;
2108 Checks : in out List_Id)
2110 Comp_Typ : constant Entity_Id := Component_Type (T);
2111 Dims : constant Pos := Number_Dimensions (T);
2113 procedure Process_Array_Component
2114 (Indices : List_Id;
2115 Comp_Checks : in out List_Id);
2116 -- Generate an invariant check for an array component identified by
2117 -- the indices in list Indices. All created checks are added to list
2118 -- Comp_Checks.
2120 procedure Process_One_Dimension
2121 (Dim : Pos;
2122 Indices : List_Id;
2123 Dim_Checks : in out List_Id);
2124 -- Generate a loop over the Nth dimension Dim of an array type. List
2125 -- Indices contains all array indices for the dimension. All created
2126 -- checks are added to list Dim_Checks.
2128 -----------------------------
2129 -- Process_Array_Component --
2130 -----------------------------
2132 procedure Process_Array_Component
2133 (Indices : List_Id;
2134 Comp_Checks : in out List_Id)
2136 Proc_Id : Entity_Id;
2138 begin
2139 if Has_Invariants (Comp_Typ) then
2141 -- In GNATprove mode, the component invariants are checked by
2142 -- other means. They should not be added to the array type
2143 -- invariant procedure, so that the procedure can be used to
2144 -- check the array type invariants if any.
2146 if GNATprove_Mode then
2147 null;
2149 else
2150 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2152 -- The component type should have an invariant procedure
2153 -- if it has invariants of its own or inherits class-wide
2154 -- invariants from parent or interface types.
2156 pragma Assert (Present (Proc_Id));
2158 -- Generate:
2159 -- <Comp_Typ>Invariant (_object (<Indices>));
2161 -- Note that the invariant procedure may have a null body if
2162 -- assertions are disabled or Assertion_Policy Ignore is in
2163 -- effect.
2165 if not Has_Null_Body (Proc_Id) then
2166 Append_New_To (Comp_Checks,
2167 Make_Procedure_Call_Statement (Loc,
2168 Name =>
2169 New_Occurrence_Of (Proc_Id, Loc),
2170 Parameter_Associations => New_List (
2171 Make_Indexed_Component (Loc,
2172 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2173 Expressions => New_Copy_List (Indices)))));
2174 end if;
2175 end if;
2177 Produced_Check := True;
2178 end if;
2179 end Process_Array_Component;
2181 ---------------------------
2182 -- Process_One_Dimension --
2183 ---------------------------
2185 procedure Process_One_Dimension
2186 (Dim : Pos;
2187 Indices : List_Id;
2188 Dim_Checks : in out List_Id)
2190 Comp_Checks : List_Id := No_List;
2191 Index : Entity_Id;
2193 begin
2194 -- Generate the invariant checks for the array component after all
2195 -- dimensions have produced their respective loops.
2197 if Dim > Dims then
2198 Process_Array_Component
2199 (Indices => Indices,
2200 Comp_Checks => Dim_Checks);
2202 -- Otherwise create a loop for the current dimension
2204 else
2205 -- Create a new loop variable for each dimension
2207 Index :=
2208 Make_Defining_Identifier (Loc,
2209 Chars => New_External_Name ('I', Dim));
2210 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2212 Process_One_Dimension
2213 (Dim => Dim + 1,
2214 Indices => Indices,
2215 Dim_Checks => Comp_Checks);
2217 -- Generate:
2218 -- for I<Dim> in _object'Range (<Dim>) loop
2219 -- <Comp_Checks>
2220 -- end loop;
2222 -- Note that the invariant procedure may have a null body if
2223 -- assertions are disabled or Assertion_Policy Ignore is in
2224 -- effect.
2226 if Present (Comp_Checks) then
2227 Append_New_To (Dim_Checks,
2228 Make_Implicit_Loop_Statement (T,
2229 Identifier => Empty,
2230 Iteration_Scheme =>
2231 Make_Iteration_Scheme (Loc,
2232 Loop_Parameter_Specification =>
2233 Make_Loop_Parameter_Specification (Loc,
2234 Defining_Identifier => Index,
2235 Discrete_Subtype_Definition =>
2236 Make_Attribute_Reference (Loc,
2237 Prefix =>
2238 New_Occurrence_Of (Obj_Id, Loc),
2239 Attribute_Name => Name_Range,
2240 Expressions => New_List (
2241 Make_Integer_Literal (Loc, Dim))))),
2242 Statements => Comp_Checks));
2243 end if;
2244 end if;
2245 end Process_One_Dimension;
2247 -- Start of processing for Add_Array_Component_Invariants
2249 begin
2250 Process_One_Dimension
2251 (Dim => 1,
2252 Indices => New_List,
2253 Dim_Checks => Checks);
2254 end Add_Array_Component_Invariants;
2256 ------------------------------
2257 -- Add_Inherited_Invariants --
2258 ------------------------------
2260 procedure Add_Inherited_Invariants
2261 (T : Entity_Id;
2262 Priv_Typ : Entity_Id;
2263 Full_Typ : Entity_Id;
2264 Obj_Id : Entity_Id;
2265 Checks : in out List_Id)
2267 Deriv_Typ : Entity_Id;
2268 Expr : Node_Id;
2269 Prag : Node_Id;
2270 Prag_Expr : Node_Id;
2271 Prag_Expr_Arg : Node_Id;
2272 Prag_Typ : Node_Id;
2273 Prag_Typ_Arg : Node_Id;
2275 Par_Proc : Entity_Id;
2276 -- The "partial" invariant procedure of Par_Typ
2278 Par_Typ : Entity_Id;
2279 -- The suitable view of the parent type used in the substitution of
2280 -- type attributes.
2282 begin
2283 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2284 return;
2285 end if;
2287 -- When the type inheriting the class-wide invariant is a concurrent
2288 -- type, use the corresponding record type because it contains all
2289 -- primitive operations of the concurrent type and allows for proper
2290 -- substitution.
2292 if Is_Concurrent_Type (T) then
2293 Deriv_Typ := Corresponding_Record_Type (T);
2294 else
2295 Deriv_Typ := T;
2296 end if;
2298 pragma Assert (Present (Deriv_Typ));
2300 -- Determine which rep item chain to use. Precedence is given to that
2301 -- of the parent type's partial view since it usually carries all the
2302 -- class-wide invariants.
2304 if Present (Priv_Typ) then
2305 Prag := First_Rep_Item (Priv_Typ);
2306 else
2307 Prag := First_Rep_Item (Full_Typ);
2308 end if;
2310 while Present (Prag) loop
2311 if Nkind (Prag) = N_Pragma
2312 and then Pragma_Name (Prag) = Name_Invariant
2313 then
2314 -- Nothing to do if the pragma was already processed
2316 if Contains (Pragmas_Seen, Prag) then
2317 return;
2319 -- Nothing to do when the caller requests the processing of all
2320 -- inherited class-wide invariants, but the pragma does not
2321 -- fall in this category.
2323 elsif not Class_Present (Prag) then
2324 return;
2325 end if;
2327 -- Extract the arguments of the invariant pragma
2329 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2330 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2331 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2332 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2334 -- The pragma applies to the partial view of the parent type
2336 if Present (Priv_Typ)
2337 and then Entity (Prag_Typ) = Priv_Typ
2338 then
2339 Par_Typ := Priv_Typ;
2341 -- The pragma applies to the full view of the parent type
2343 elsif Present (Full_Typ)
2344 and then Entity (Prag_Typ) = Full_Typ
2345 then
2346 Par_Typ := Full_Typ;
2348 -- Otherwise the pragma does not belong to the parent type and
2349 -- should not be considered.
2351 else
2352 return;
2353 end if;
2355 -- Perform the following substitutions:
2357 -- * Replace a reference to the _object parameter of the
2358 -- parent type's partial invariant procedure with a
2359 -- reference to the _object parameter of the derived
2360 -- type's full invariant procedure.
2362 -- * Replace a reference to a discriminant of the parent type
2363 -- with a suitable value from the point of view of the
2364 -- derived type.
2366 -- * Replace a call to an overridden parent primitive with a
2367 -- call to the overriding derived type primitive.
2369 -- * Replace a call to an inherited parent primitive with a
2370 -- call to the internally-generated inherited derived type
2371 -- primitive.
2373 Expr := New_Copy_Tree (Prag_Expr);
2375 -- The parent type must have a "partial" invariant procedure
2376 -- because class-wide invariants are captured exclusively by
2377 -- it.
2379 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2380 pragma Assert (Present (Par_Proc));
2382 Replace_References
2383 (Expr => Expr,
2384 Par_Typ => Par_Typ,
2385 Deriv_Typ => Deriv_Typ,
2386 Par_Obj => First_Formal (Par_Proc),
2387 Deriv_Obj => Obj_Id);
2389 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2390 end if;
2392 Next_Rep_Item (Prag);
2393 end loop;
2394 end Add_Inherited_Invariants;
2396 ------------------------------
2397 -- Add_Interface_Invariants --
2398 ------------------------------
2400 procedure Add_Interface_Invariants
2401 (T : Entity_Id;
2402 Obj_Id : Entity_Id;
2403 Checks : in out List_Id)
2405 Iface_Elmt : Elmt_Id;
2406 Ifaces : Elist_Id;
2408 begin
2409 -- Generate an invariant check for each class-wide invariant coming
2410 -- from all interfaces implemented by type T.
2412 if Is_Tagged_Type (T) then
2413 Collect_Interfaces (T, Ifaces);
2415 -- Process the class-wide invariants of all implemented interfaces
2417 Iface_Elmt := First_Elmt (Ifaces);
2418 while Present (Iface_Elmt) loop
2420 -- The Full_Typ parameter is intentionally left Empty because
2421 -- interfaces are treated as the partial view of a private type
2422 -- in order to achieve uniformity with the general case.
2424 Add_Inherited_Invariants
2425 (T => T,
2426 Priv_Typ => Node (Iface_Elmt),
2427 Full_Typ => Empty,
2428 Obj_Id => Obj_Id,
2429 Checks => Checks);
2431 Next_Elmt (Iface_Elmt);
2432 end loop;
2433 end if;
2434 end Add_Interface_Invariants;
2436 -------------------------
2437 -- Add_Invariant_Check --
2438 -------------------------
2440 procedure Add_Invariant_Check
2441 (Prag : Node_Id;
2442 Expr : Node_Id;
2443 Checks : in out List_Id;
2444 Inherited : Boolean := False)
2446 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2447 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2448 Ploc : constant Source_Ptr := Sloc (Prag);
2449 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2451 Assoc : List_Id;
2452 Str : String_Id;
2454 begin
2455 -- The invariant is ignored, nothing left to do
2457 if Is_Ignored (Prag) then
2458 null;
2460 -- Otherwise the invariant is checked. Build a pragma Check to verify
2461 -- the expression at run time.
2463 else
2464 Assoc := New_List (
2465 Make_Pragma_Argument_Association (Ploc,
2466 Expression => Make_Identifier (Ploc, Nam)),
2467 Make_Pragma_Argument_Association (Ploc,
2468 Expression => Expr));
2470 -- Handle the String argument (if any)
2472 if Present (Str_Arg) then
2473 Str := Strval (Get_Pragma_Arg (Str_Arg));
2475 -- When inheriting an invariant, modify the message from
2476 -- "failed invariant" to "failed inherited invariant".
2478 if Inherited then
2479 String_To_Name_Buffer (Str);
2481 if Name_Buffer (1 .. 16) = "failed invariant" then
2482 Insert_Str_In_Name_Buffer ("inherited ", 8);
2483 Str := String_From_Name_Buffer;
2484 end if;
2485 end if;
2487 Append_To (Assoc,
2488 Make_Pragma_Argument_Association (Ploc,
2489 Expression => Make_String_Literal (Ploc, Str)));
2490 end if;
2492 -- Generate:
2493 -- pragma Check (<Nam>, <Expr>, <Str>);
2495 Append_New_To (Checks,
2496 Make_Pragma (Ploc,
2497 Chars => Name_Check,
2498 Pragma_Argument_Associations => Assoc));
2499 end if;
2501 -- Output an info message when inheriting an invariant and the
2502 -- listing option is enabled.
2504 if Inherited and Opt.List_Inherited_Aspects then
2505 Error_Msg_Sloc := Sloc (Prag);
2506 Error_Msg_N
2507 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2508 end if;
2510 -- Add the pragma to the list of processed pragmas
2512 Append_New_Elmt (Prag, Pragmas_Seen);
2513 Produced_Check := True;
2514 end Add_Invariant_Check;
2516 ---------------------------
2517 -- Add_Parent_Invariants --
2518 ---------------------------
2520 procedure Add_Parent_Invariants
2521 (T : Entity_Id;
2522 Obj_Id : Entity_Id;
2523 Checks : in out List_Id)
2525 Dummy_1 : Entity_Id;
2526 Dummy_2 : Entity_Id;
2528 Curr_Typ : Entity_Id;
2529 -- The entity of the current type being examined
2531 Full_Typ : Entity_Id;
2532 -- The full view of Par_Typ
2534 Par_Typ : Entity_Id;
2535 -- The entity of the parent type
2537 Priv_Typ : Entity_Id;
2538 -- The partial view of Par_Typ
2540 begin
2541 -- Do not process array types because they cannot have true parent
2542 -- types. This also prevents the generation of a duplicate invariant
2543 -- check when the input type is an array base type because its Etype
2544 -- denotes the first subtype, both of which share the same component
2545 -- type.
2547 if Is_Array_Type (T) then
2548 return;
2549 end if;
2551 -- Climb the parent type chain
2553 Curr_Typ := T;
2554 loop
2555 -- Do not consider subtypes as they inherit the invariants
2556 -- from their base types.
2558 Par_Typ := Base_Type (Etype (Curr_Typ));
2560 -- Stop the climb once the root of the parent chain is
2561 -- reached.
2563 exit when Curr_Typ = Par_Typ;
2565 -- Process the class-wide invariants of the parent type
2567 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2569 -- Process the elements of an array type
2571 if Is_Array_Type (Full_Typ) then
2572 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2574 -- Process the components of a record type
2576 elsif Ekind (Full_Typ) = E_Record_Type then
2577 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2578 end if;
2580 Add_Inherited_Invariants
2581 (T => T,
2582 Priv_Typ => Priv_Typ,
2583 Full_Typ => Full_Typ,
2584 Obj_Id => Obj_Id,
2585 Checks => Checks);
2587 Curr_Typ := Par_Typ;
2588 end loop;
2589 end Add_Parent_Invariants;
2591 ------------------------
2592 -- Add_Own_Invariants --
2593 ------------------------
2595 procedure Add_Own_Invariants
2596 (T : Entity_Id;
2597 Obj_Id : Entity_Id;
2598 Checks : in out List_Id;
2599 Priv_Item : Node_Id := Empty)
2601 ASIS_Expr : Node_Id;
2602 Expr : Node_Id;
2603 Prag : Node_Id;
2604 Prag_Asp : Node_Id;
2605 Prag_Expr : Node_Id;
2606 Prag_Expr_Arg : Node_Id;
2607 Prag_Typ : Node_Id;
2608 Prag_Typ_Arg : Node_Id;
2610 begin
2611 if not Present (T) then
2612 return;
2613 end if;
2615 Prag := First_Rep_Item (T);
2616 while Present (Prag) loop
2617 if Nkind (Prag) = N_Pragma
2618 and then Pragma_Name (Prag) = Name_Invariant
2619 then
2620 -- Stop the traversal of the rep item chain once a specific
2621 -- item is encountered.
2623 if Present (Priv_Item) and then Prag = Priv_Item then
2624 exit;
2625 end if;
2627 -- Nothing to do if the pragma was already processed
2629 if Contains (Pragmas_Seen, Prag) then
2630 return;
2631 end if;
2633 -- Extract the arguments of the invariant pragma
2635 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2636 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2637 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2638 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2639 Prag_Asp := Corresponding_Aspect (Prag);
2641 -- Verify the pragma belongs to T, otherwise the pragma applies
2642 -- to a parent type in which case it will be processed later by
2643 -- Add_Parent_Invariants or Add_Interface_Invariants.
2645 if Entity (Prag_Typ) /= T then
2646 return;
2647 end if;
2649 Expr := New_Copy_Tree (Prag_Expr);
2651 -- Substitute all references to type T with references to the
2652 -- _object formal parameter.
2654 Replace_Type_References (Expr, T, Obj_Id);
2656 -- Preanalyze the invariant expression to detect errors and at
2657 -- the same time capture the visibility of the proper package
2658 -- part.
2660 Set_Parent (Expr, Parent (Prag_Expr));
2661 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2663 -- Save a copy of the expression when T is tagged to detect
2664 -- errors and capture the visibility of the proper package part
2665 -- for the generation of inherited type invariants.
2667 if Is_Tagged_Type (T) then
2668 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2669 end if;
2671 -- If the pragma comes from an aspect specification, replace
2672 -- the saved expression because all type references must be
2673 -- substituted for the call to Preanalyze_Spec_Expression in
2674 -- Check_Aspect_At_xxx routines.
2676 if Present (Prag_Asp) then
2677 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2678 end if;
2680 -- Analyze the original invariant expression for ASIS
2682 if ASIS_Mode then
2683 ASIS_Expr := Empty;
2685 if Comes_From_Source (Prag) then
2686 ASIS_Expr := Prag_Expr;
2687 elsif Present (Prag_Asp) then
2688 ASIS_Expr := Expression (Prag_Asp);
2689 end if;
2691 if Present (ASIS_Expr) then
2692 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2693 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2694 end if;
2695 end if;
2697 Add_Invariant_Check (Prag, Expr, Checks);
2698 end if;
2700 Next_Rep_Item (Prag);
2701 end loop;
2702 end Add_Own_Invariants;
2704 -------------------------------------
2705 -- Add_Record_Component_Invariants --
2706 -------------------------------------
2708 procedure Add_Record_Component_Invariants
2709 (T : Entity_Id;
2710 Obj_Id : Entity_Id;
2711 Checks : in out List_Id)
2713 procedure Process_Component_List
2714 (Comp_List : Node_Id;
2715 CL_Checks : in out List_Id);
2716 -- Generate invariant checks for all record components found in
2717 -- component list Comp_List, including variant parts. All created
2718 -- checks are added to list CL_Checks.
2720 procedure Process_Record_Component
2721 (Comp_Id : Entity_Id;
2722 Comp_Checks : in out List_Id);
2723 -- Generate an invariant check for a record component identified by
2724 -- Comp_Id. All created checks are added to list Comp_Checks.
2726 ----------------------------
2727 -- Process_Component_List --
2728 ----------------------------
2730 procedure Process_Component_List
2731 (Comp_List : Node_Id;
2732 CL_Checks : in out List_Id)
2734 Comp : Node_Id;
2735 Var : Node_Id;
2736 Var_Alts : List_Id := No_List;
2737 Var_Checks : List_Id := No_List;
2738 Var_Stmts : List_Id;
2740 Produced_Variant_Check : Boolean := False;
2741 -- This flag tracks whether the component has produced at least
2742 -- one invariant check.
2744 begin
2745 -- Traverse the component items
2747 Comp := First (Component_Items (Comp_List));
2748 while Present (Comp) loop
2749 if Nkind (Comp) = N_Component_Declaration then
2751 -- Generate the component invariant check
2753 Process_Record_Component
2754 (Comp_Id => Defining_Entity (Comp),
2755 Comp_Checks => CL_Checks);
2756 end if;
2758 Next (Comp);
2759 end loop;
2761 -- Traverse the variant part
2763 if Present (Variant_Part (Comp_List)) then
2764 Var := First (Variants (Variant_Part (Comp_List)));
2765 while Present (Var) loop
2766 Var_Checks := No_List;
2768 -- Generate invariant checks for all components and variant
2769 -- parts that qualify.
2771 Process_Component_List
2772 (Comp_List => Component_List (Var),
2773 CL_Checks => Var_Checks);
2775 -- The components of the current variant produced at least
2776 -- one invariant check.
2778 if Present (Var_Checks) then
2779 Var_Stmts := Var_Checks;
2780 Produced_Variant_Check := True;
2782 -- Otherwise there are either no components with invariants,
2783 -- assertions are disabled, or Assertion_Policy Ignore is in
2784 -- effect.
2786 else
2787 Var_Stmts := New_List (Make_Null_Statement (Loc));
2788 end if;
2790 Append_New_To (Var_Alts,
2791 Make_Case_Statement_Alternative (Loc,
2792 Discrete_Choices =>
2793 New_Copy_List (Discrete_Choices (Var)),
2794 Statements => Var_Stmts));
2796 Next (Var);
2797 end loop;
2799 -- Create a case statement which verifies the invariant checks
2800 -- of a particular component list depending on the discriminant
2801 -- values only when there is at least one real invariant check.
2803 if Produced_Variant_Check then
2804 Append_New_To (CL_Checks,
2805 Make_Case_Statement (Loc,
2806 Expression =>
2807 Make_Selected_Component (Loc,
2808 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2809 Selector_Name =>
2810 New_Occurrence_Of
2811 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2812 Alternatives => Var_Alts));
2813 end if;
2814 end if;
2815 end Process_Component_List;
2817 ------------------------------
2818 -- Process_Record_Component --
2819 ------------------------------
2821 procedure Process_Record_Component
2822 (Comp_Id : Entity_Id;
2823 Comp_Checks : in out List_Id)
2825 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2826 Proc_Id : Entity_Id;
2828 Produced_Component_Check : Boolean := False;
2829 -- This flag tracks whether the component has produced at least
2830 -- one invariant check.
2832 begin
2833 -- Nothing to do for internal component _parent. Note that it is
2834 -- not desirable to check whether the component comes from source
2835 -- because protected type components are relocated to an internal
2836 -- corresponding record, but still need processing.
2838 if Chars (Comp_Id) = Name_uParent then
2839 return;
2840 end if;
2842 -- Verify the invariant of the component. Note that an access
2843 -- type may have an invariant when it acts as the full view of a
2844 -- private type and the invariant appears on the partial view. In
2845 -- this case verify the access value itself.
2847 if Has_Invariants (Comp_Typ) then
2849 -- In GNATprove mode, the component invariants are checked by
2850 -- other means. They should not be added to the record type
2851 -- invariant procedure, so that the procedure can be used to
2852 -- check the record type invariants if any.
2854 if GNATprove_Mode then
2855 null;
2857 else
2858 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2860 -- The component type should have an invariant procedure
2861 -- if it has invariants of its own or inherits class-wide
2862 -- invariants from parent or interface types.
2864 pragma Assert (Present (Proc_Id));
2866 -- Generate:
2867 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2869 -- Note that the invariant procedure may have a null body if
2870 -- assertions are disabled or Assertion_Policy Ignore is in
2871 -- effect.
2873 if not Has_Null_Body (Proc_Id) then
2874 Append_New_To (Comp_Checks,
2875 Make_Procedure_Call_Statement (Loc,
2876 Name =>
2877 New_Occurrence_Of (Proc_Id, Loc),
2878 Parameter_Associations => New_List (
2879 Make_Selected_Component (Loc,
2880 Prefix =>
2881 Unchecked_Convert_To
2882 (T, New_Occurrence_Of (Obj_Id, Loc)),
2883 Selector_Name =>
2884 New_Occurrence_Of (Comp_Id, Loc)))));
2885 end if;
2886 end if;
2888 Produced_Check := True;
2889 Produced_Component_Check := True;
2890 end if;
2892 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2893 Error_Msg_NE
2894 ("invariants cannot be checked on components of "
2895 & "unchecked_union type &?", Comp_Id, T);
2896 end if;
2897 end Process_Record_Component;
2899 -- Local variables
2901 Comps : Node_Id;
2902 Def : Node_Id;
2904 -- Start of processing for Add_Record_Component_Invariants
2906 begin
2907 -- An untagged derived type inherits the components of its parent
2908 -- type. In order to avoid creating redundant invariant checks, do
2909 -- not process the components now. Instead wait until the ultimate
2910 -- parent of the untagged derivation chain is reached.
2912 if not Is_Untagged_Derivation (T) then
2913 Def := Type_Definition (Parent (T));
2915 if Nkind (Def) = N_Derived_Type_Definition then
2916 Def := Record_Extension_Part (Def);
2917 end if;
2919 pragma Assert (Nkind (Def) = N_Record_Definition);
2920 Comps := Component_List (Def);
2922 if Present (Comps) then
2923 Process_Component_List
2924 (Comp_List => Comps,
2925 CL_Checks => Checks);
2926 end if;
2927 end if;
2928 end Add_Record_Component_Invariants;
2930 -- Local variables
2932 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2933 -- Save the Ghost mode to restore on exit
2935 Dummy : Entity_Id;
2936 Priv_Item : Node_Id;
2937 Proc_Body : Node_Id;
2938 Proc_Body_Id : Entity_Id;
2939 Proc_Decl : Node_Id;
2940 Proc_Id : Entity_Id;
2941 Stmts : List_Id := No_List;
2943 CRec_Typ : Entity_Id := Empty;
2944 -- The corresponding record type of Full_Typ
2946 Full_Proc : Entity_Id := Empty;
2947 -- The entity of the "full" invariant procedure
2949 Full_Typ : Entity_Id := Empty;
2950 -- The full view of the working type
2952 Obj_Id : Entity_Id := Empty;
2953 -- The _object formal parameter of the invariant procedure
2955 Part_Proc : Entity_Id := Empty;
2956 -- The entity of the "partial" invariant procedure
2958 Priv_Typ : Entity_Id := Empty;
2959 -- The partial view of the working type
2961 Work_Typ : Entity_Id := Empty;
2962 -- The working type
2964 -- Start of processing for Build_Invariant_Procedure_Body
2966 begin
2967 Work_Typ := Typ;
2969 -- The input type denotes the implementation base type of a constrained
2970 -- array type. Work with the first subtype as all invariant pragmas are
2971 -- on its rep item chain.
2973 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2974 Work_Typ := First_Subtype (Work_Typ);
2976 -- The input type denotes the corresponding record type of a protected
2977 -- or task type. Work with the concurrent type because the corresponding
2978 -- record type may not be visible to clients of the type.
2980 elsif Ekind (Work_Typ) = E_Record_Type
2981 and then Is_Concurrent_Record_Type (Work_Typ)
2982 then
2983 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2984 end if;
2986 -- The working type may be subject to pragma Ghost. Set the mode now to
2987 -- ensure that the invariant procedure is properly marked as Ghost.
2989 Set_Ghost_Mode (Work_Typ);
2991 -- The type must either have invariants of its own, inherit class-wide
2992 -- invariants from parent types or interfaces, or be an array or record
2993 -- type whose components have invariants.
2995 pragma Assert (Has_Invariants (Work_Typ));
2997 -- Interfaces are treated as the partial view of a private type in order
2998 -- to achieve uniformity with the general case.
3000 if Is_Interface (Work_Typ) then
3001 Priv_Typ := Work_Typ;
3003 -- Otherwise obtain both views of the type
3005 else
3006 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3007 end if;
3009 -- The caller requests a body for the partial invariant procedure
3011 if Partial_Invariant then
3012 Full_Proc := Invariant_Procedure (Work_Typ);
3013 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3015 -- The "full" invariant procedure body was already created
3017 if Present (Full_Proc)
3018 and then Present
3019 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3020 then
3021 -- This scenario happens only when the type is an untagged
3022 -- derivation from a private parent and the underlying full
3023 -- view was processed before the partial view.
3025 pragma Assert
3026 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3028 -- Nothing to do because the processing of the underlying full
3029 -- view already checked the invariants of the partial view.
3031 goto Leave;
3032 end if;
3034 -- Create a declaration for the "partial" invariant procedure if it
3035 -- is not available.
3037 if No (Proc_Id) then
3038 Build_Invariant_Procedure_Declaration
3039 (Typ => Work_Typ,
3040 Partial_Invariant => True);
3042 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3043 end if;
3045 -- The caller requests a body for the "full" invariant procedure
3047 else
3048 Proc_Id := Invariant_Procedure (Work_Typ);
3049 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3051 -- Create a declaration for the "full" invariant procedure if it is
3052 -- not available.
3054 if No (Proc_Id) then
3055 Build_Invariant_Procedure_Declaration (Work_Typ);
3056 Proc_Id := Invariant_Procedure (Work_Typ);
3057 end if;
3058 end if;
3060 -- At this point there should be an invariant procedure declaration
3062 pragma Assert (Present (Proc_Id));
3063 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3065 -- Nothing to do if the invariant procedure already has a body
3067 if Present (Corresponding_Body (Proc_Decl)) then
3068 goto Leave;
3069 end if;
3071 -- Emulate the environment of the invariant procedure by installing its
3072 -- scope and formal parameters. Note that this is not needed, but having
3073 -- the scope installed helps with the detection of invariant-related
3074 -- errors.
3076 Push_Scope (Proc_Id);
3077 Install_Formals (Proc_Id);
3079 Obj_Id := First_Formal (Proc_Id);
3080 pragma Assert (Present (Obj_Id));
3082 -- The "partial" invariant procedure verifies the invariants of the
3083 -- partial view only.
3085 if Partial_Invariant then
3086 pragma Assert (Present (Priv_Typ));
3088 Add_Own_Invariants
3089 (T => Priv_Typ,
3090 Obj_Id => Obj_Id,
3091 Checks => Stmts);
3093 -- Otherwise the "full" invariant procedure verifies the invariants of
3094 -- the full view, all array or record components, as well as class-wide
3095 -- invariants inherited from parent types or interfaces. In addition, it
3096 -- indirectly verifies the invariants of the partial view by calling the
3097 -- "partial" invariant procedure.
3099 else
3100 pragma Assert (Present (Full_Typ));
3102 -- Check the invariants of the partial view by calling the "partial"
3103 -- invariant procedure. Generate:
3105 -- <Work_Typ>Partial_Invariant (_object);
3107 if Present (Part_Proc) then
3108 Append_New_To (Stmts,
3109 Make_Procedure_Call_Statement (Loc,
3110 Name => New_Occurrence_Of (Part_Proc, Loc),
3111 Parameter_Associations => New_List (
3112 New_Occurrence_Of (Obj_Id, Loc))));
3114 Produced_Check := True;
3115 end if;
3117 Priv_Item := Empty;
3119 -- Derived subtypes do not have a partial view
3121 if Present (Priv_Typ) then
3123 -- The processing of the "full" invariant procedure intentionally
3124 -- skips the partial view because a) this may result in changes of
3125 -- visibility and b) lead to duplicate checks. However, when the
3126 -- full view is the underlying full view of an untagged derived
3127 -- type whose parent type is private, partial invariants appear on
3128 -- the rep item chain of the partial view only.
3130 -- package Pack_1 is
3131 -- type Root ... is private;
3132 -- private
3133 -- <full view of Root>
3134 -- end Pack_1;
3136 -- with Pack_1;
3137 -- package Pack_2 is
3138 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3139 -- <underlying full view of Child>
3140 -- end Pack_2;
3142 -- As a result, the processing of the full view must also consider
3143 -- all invariants of the partial view.
3145 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3146 null;
3148 -- Otherwise the invariants of the partial view are ignored
3150 else
3151 -- Note that the rep item chain is shared between the partial
3152 -- and full views of a type. To avoid processing the invariants
3153 -- of the partial view, signal the logic to stop when the first
3154 -- rep item of the partial view has been reached.
3156 Priv_Item := First_Rep_Item (Priv_Typ);
3158 -- Ignore the invariants of the partial view by eliminating the
3159 -- view.
3161 Priv_Typ := Empty;
3162 end if;
3163 end if;
3165 -- Process the invariants of the full view and in certain cases those
3166 -- of the partial view. This also handles any invariants on array or
3167 -- record components.
3169 Add_Own_Invariants
3170 (T => Priv_Typ,
3171 Obj_Id => Obj_Id,
3172 Checks => Stmts,
3173 Priv_Item => Priv_Item);
3175 Add_Own_Invariants
3176 (T => Full_Typ,
3177 Obj_Id => Obj_Id,
3178 Checks => Stmts,
3179 Priv_Item => Priv_Item);
3181 -- Process the elements of an array type
3183 if Is_Array_Type (Full_Typ) then
3184 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3186 -- Process the components of a record type
3188 elsif Ekind (Full_Typ) = E_Record_Type then
3189 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3191 -- Process the components of a corresponding record
3193 elsif Present (CRec_Typ) then
3194 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3195 end if;
3197 -- Process the inherited class-wide invariants of all parent types.
3198 -- This also handles any invariants on record components.
3200 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3202 -- Process the inherited class-wide invariants of all implemented
3203 -- interface types.
3205 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3206 end if;
3208 End_Scope;
3210 -- At this point there should be at least one invariant check. If this
3211 -- is not the case, then the invariant-related flags were not properly
3212 -- set, or there is a missing invariant procedure on one of the array
3213 -- or record components.
3215 pragma Assert (Produced_Check);
3217 -- Account for the case where assertions are disabled or all invariant
3218 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3219 -- empty body.
3221 if No (Stmts) then
3222 Stmts := New_List (Make_Null_Statement (Loc));
3223 end if;
3225 -- Generate:
3226 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3227 -- begin
3228 -- <Stmts>
3229 -- end <Work_Typ>[Partial_]Invariant;
3231 Proc_Body :=
3232 Make_Subprogram_Body (Loc,
3233 Specification =>
3234 Copy_Subprogram_Spec (Parent (Proc_Id)),
3235 Declarations => Empty_List,
3236 Handled_Statement_Sequence =>
3237 Make_Handled_Sequence_Of_Statements (Loc,
3238 Statements => Stmts));
3239 Proc_Body_Id := Defining_Entity (Proc_Body);
3241 -- Perform minor decoration in case the body is not analyzed
3243 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3244 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3245 Set_Scope (Proc_Body_Id, Current_Scope);
3247 -- Link both spec and body to avoid generating duplicates
3249 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3250 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3252 -- The body should not be inserted into the tree when the context is
3253 -- ASIS or a generic unit because it is not part of the template. Note
3254 -- that the body must still be generated in order to resolve the
3255 -- invariants.
3257 if ASIS_Mode or Inside_A_Generic then
3258 null;
3260 -- Semi-insert the body into the tree for GNATprove by setting its
3261 -- Parent field. This allows for proper upstream tree traversals.
3263 elsif GNATprove_Mode then
3264 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3266 -- Otherwise the body is part of the freezing actions of the type
3268 else
3269 Append_Freeze_Action (Work_Typ, Proc_Body);
3270 end if;
3272 <<Leave>>
3273 Restore_Ghost_Mode (Saved_GM);
3274 end Build_Invariant_Procedure_Body;
3276 -------------------------------------------
3277 -- Build_Invariant_Procedure_Declaration --
3278 -------------------------------------------
3280 -- WARNING: This routine manages Ghost regions. Return statements must be
3281 -- replaced by gotos which jump to the end of the routine and restore the
3282 -- Ghost mode.
3284 procedure Build_Invariant_Procedure_Declaration
3285 (Typ : Entity_Id;
3286 Partial_Invariant : Boolean := False)
3288 Loc : constant Source_Ptr := Sloc (Typ);
3290 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3291 -- Save the Ghost mode to restore on exit
3293 Proc_Decl : Node_Id;
3294 Proc_Id : Entity_Id;
3295 Proc_Nam : Name_Id;
3296 Typ_Decl : Node_Id;
3298 CRec_Typ : Entity_Id;
3299 -- The corresponding record type of Full_Typ
3301 Full_Base : Entity_Id;
3302 -- The base type of Full_Typ
3304 Full_Typ : Entity_Id;
3305 -- The full view of working type
3307 Obj_Id : Entity_Id;
3308 -- The _object formal parameter of the invariant procedure
3310 Obj_Typ : Entity_Id;
3311 -- The type of the _object formal parameter
3313 Priv_Typ : Entity_Id;
3314 -- The partial view of working type
3316 Work_Typ : Entity_Id;
3317 -- The working type
3319 begin
3320 Work_Typ := Typ;
3322 -- The input type denotes the implementation base type of a constrained
3323 -- array type. Work with the first subtype as all invariant pragmas are
3324 -- on its rep item chain.
3326 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3327 Work_Typ := First_Subtype (Work_Typ);
3329 -- The input denotes the corresponding record type of a protected or a
3330 -- task type. Work with the concurrent type because the corresponding
3331 -- record type may not be visible to clients of the type.
3333 elsif Ekind (Work_Typ) = E_Record_Type
3334 and then Is_Concurrent_Record_Type (Work_Typ)
3335 then
3336 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3337 end if;
3339 -- The working type may be subject to pragma Ghost. Set the mode now to
3340 -- ensure that the invariant procedure is properly marked as Ghost.
3342 Set_Ghost_Mode (Work_Typ);
3344 -- The type must either have invariants of its own, inherit class-wide
3345 -- invariants from parent or interface types, or be an array or record
3346 -- type whose components have invariants.
3348 pragma Assert (Has_Invariants (Work_Typ));
3350 -- Nothing to do if the type already has a "partial" invariant procedure
3352 if Partial_Invariant then
3353 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3354 goto Leave;
3355 end if;
3357 -- Nothing to do if the type already has a "full" invariant procedure
3359 elsif Present (Invariant_Procedure (Work_Typ)) then
3360 goto Leave;
3361 end if;
3363 -- The caller requests the declaration of the "partial" invariant
3364 -- procedure.
3366 if Partial_Invariant then
3367 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3369 -- Otherwise the caller requests the declaration of the "full" invariant
3370 -- procedure.
3372 else
3373 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3374 end if;
3376 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3378 -- Perform minor decoration in case the declaration is not analyzed
3380 Set_Ekind (Proc_Id, E_Procedure);
3381 Set_Etype (Proc_Id, Standard_Void_Type);
3382 Set_Scope (Proc_Id, Current_Scope);
3384 if Partial_Invariant then
3385 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3386 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3387 else
3388 Set_Is_Invariant_Procedure (Proc_Id);
3389 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3390 end if;
3392 -- The invariant procedure requires debug info when the invariants are
3393 -- subject to Source Coverage Obligations.
3395 if Generate_SCO then
3396 Set_Needs_Debug_Info (Proc_Id);
3397 end if;
3399 -- Obtain all views of the input type
3401 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3403 -- Associate the invariant procedure with all views
3405 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3406 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3407 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3408 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3410 -- The declaration of the invariant procedure is inserted after the
3411 -- declaration of the partial view as this allows for proper external
3412 -- visibility.
3414 if Present (Priv_Typ) then
3415 Typ_Decl := Declaration_Node (Priv_Typ);
3417 -- Anonymous arrays in object declarations have no explicit declaration
3418 -- so use the related object declaration as the insertion point.
3420 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3421 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3423 -- Derived types with the full view as parent do not have a partial
3424 -- view. Insert the invariant procedure after the derived type.
3426 else
3427 Typ_Decl := Declaration_Node (Full_Typ);
3428 end if;
3430 -- The type should have a declarative node
3432 pragma Assert (Present (Typ_Decl));
3434 -- Create the formal parameter which emulates the variable-like behavior
3435 -- of the current type instance.
3437 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3439 -- When generating an invariant procedure declaration for an abstract
3440 -- type (including interfaces), use the class-wide type as the _object
3441 -- type. This has several desirable effects:
3443 -- * The invariant procedure does not become a primitive of the type.
3444 -- This eliminates the need to either special case the treatment of
3445 -- invariant procedures, or to make it a predefined primitive and
3446 -- force every derived type to potentially provide an empty body.
3448 -- * The invariant procedure does not need to be declared as abstract.
3449 -- This allows for a proper body, which in turn avoids redundant
3450 -- processing of the same invariants for types with multiple views.
3452 -- * The class-wide type allows for calls to abstract primitives
3453 -- within a nonabstract subprogram. The calls are treated as
3454 -- dispatching and require additional processing when they are
3455 -- remapped to call primitives of derived types. See routine
3456 -- Replace_References for details.
3458 if Is_Abstract_Type (Work_Typ) then
3459 Obj_Typ := Class_Wide_Type (Work_Typ);
3460 else
3461 Obj_Typ := Work_Typ;
3462 end if;
3464 -- Perform minor decoration in case the declaration is not analyzed
3466 Set_Ekind (Obj_Id, E_In_Parameter);
3467 Set_Etype (Obj_Id, Obj_Typ);
3468 Set_Scope (Obj_Id, Proc_Id);
3470 Set_First_Entity (Proc_Id, Obj_Id);
3472 -- Generate:
3473 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3475 Proc_Decl :=
3476 Make_Subprogram_Declaration (Loc,
3477 Specification =>
3478 Make_Procedure_Specification (Loc,
3479 Defining_Unit_Name => Proc_Id,
3480 Parameter_Specifications => New_List (
3481 Make_Parameter_Specification (Loc,
3482 Defining_Identifier => Obj_Id,
3483 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3485 -- The declaration should not be inserted into the tree when the context
3486 -- is ASIS or a generic unit because it is not part of the template.
3488 if ASIS_Mode or Inside_A_Generic then
3489 null;
3491 -- Semi-insert the declaration into the tree for GNATprove by setting
3492 -- its Parent field. This allows for proper upstream tree traversals.
3494 elsif GNATprove_Mode then
3495 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3497 -- Otherwise insert the declaration
3499 else
3500 pragma Assert (Present (Typ_Decl));
3501 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3502 end if;
3504 <<Leave>>
3505 Restore_Ghost_Mode (Saved_GM);
3506 end Build_Invariant_Procedure_Declaration;
3508 --------------------------
3509 -- Build_Procedure_Form --
3510 --------------------------
3512 procedure Build_Procedure_Form (N : Node_Id) is
3513 Loc : constant Source_Ptr := Sloc (N);
3514 Subp : constant Entity_Id := Defining_Entity (N);
3516 Func_Formal : Entity_Id;
3517 Proc_Formals : List_Id;
3518 Proc_Decl : Node_Id;
3520 begin
3521 -- No action needed if this transformation was already done, or in case
3522 -- of subprogram renaming declarations.
3524 if Nkind (Specification (N)) = N_Procedure_Specification
3525 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3526 then
3527 return;
3528 end if;
3530 -- Ditto when dealing with an expression function, where both the
3531 -- original expression and the generated declaration end up being
3532 -- expanded here.
3534 if Rewritten_For_C (Subp) then
3535 return;
3536 end if;
3538 Proc_Formals := New_List;
3540 -- Create a list of formal parameters with the same types as the
3541 -- function.
3543 Func_Formal := First_Formal (Subp);
3544 while Present (Func_Formal) loop
3545 Append_To (Proc_Formals,
3546 Make_Parameter_Specification (Loc,
3547 Defining_Identifier =>
3548 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3549 Parameter_Type =>
3550 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3552 Next_Formal (Func_Formal);
3553 end loop;
3555 -- Add an extra out parameter to carry the function result
3557 Name_Len := 6;
3558 Name_Buffer (1 .. Name_Len) := "RESULT";
3559 Append_To (Proc_Formals,
3560 Make_Parameter_Specification (Loc,
3561 Defining_Identifier =>
3562 Make_Defining_Identifier (Loc, Chars => Name_Find),
3563 Out_Present => True,
3564 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3566 -- The new procedure declaration is inserted immediately after the
3567 -- function declaration. The processing in Build_Procedure_Body_Form
3568 -- relies on this order.
3570 Proc_Decl :=
3571 Make_Subprogram_Declaration (Loc,
3572 Specification =>
3573 Make_Procedure_Specification (Loc,
3574 Defining_Unit_Name =>
3575 Make_Defining_Identifier (Loc, Chars (Subp)),
3576 Parameter_Specifications => Proc_Formals));
3578 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3580 -- Entity of procedure must remain invisible so that it does not
3581 -- overload subsequent references to the original function.
3583 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3585 -- Mark the function as having a procedure form and link the function
3586 -- and its internally built procedure.
3588 Set_Rewritten_For_C (Subp);
3589 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3590 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3591 end Build_Procedure_Form;
3593 ------------------------
3594 -- Build_Runtime_Call --
3595 ------------------------
3597 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3598 begin
3599 -- If entity is not available, we can skip making the call (this avoids
3600 -- junk duplicated error messages in a number of cases).
3602 if not RTE_Available (RE) then
3603 return Make_Null_Statement (Loc);
3604 else
3605 return
3606 Make_Procedure_Call_Statement (Loc,
3607 Name => New_Occurrence_Of (RTE (RE), Loc));
3608 end if;
3609 end Build_Runtime_Call;
3611 ------------------------
3612 -- Build_SS_Mark_Call --
3613 ------------------------
3615 function Build_SS_Mark_Call
3616 (Loc : Source_Ptr;
3617 Mark : Entity_Id) return Node_Id
3619 begin
3620 -- Generate:
3621 -- Mark : constant Mark_Id := SS_Mark;
3623 return
3624 Make_Object_Declaration (Loc,
3625 Defining_Identifier => Mark,
3626 Constant_Present => True,
3627 Object_Definition =>
3628 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3629 Expression =>
3630 Make_Function_Call (Loc,
3631 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3632 end Build_SS_Mark_Call;
3634 ---------------------------
3635 -- Build_SS_Release_Call --
3636 ---------------------------
3638 function Build_SS_Release_Call
3639 (Loc : Source_Ptr;
3640 Mark : Entity_Id) return Node_Id
3642 begin
3643 -- Generate:
3644 -- SS_Release (Mark);
3646 return
3647 Make_Procedure_Call_Statement (Loc,
3648 Name =>
3649 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3650 Parameter_Associations => New_List (
3651 New_Occurrence_Of (Mark, Loc)));
3652 end Build_SS_Release_Call;
3654 ----------------------------
3655 -- Build_Task_Array_Image --
3656 ----------------------------
3658 -- This function generates the body for a function that constructs the
3659 -- image string for a task that is an array component. The function is
3660 -- local to the init proc for the array type, and is called for each one
3661 -- of the components. The constructed image has the form of an indexed
3662 -- component, whose prefix is the outer variable of the array type.
3663 -- The n-dimensional array type has known indexes Index, Index2...
3665 -- Id_Ref is an indexed component form created by the enclosing init proc.
3666 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3667 -- in the loops that call the individual task init proc on each component.
3669 -- The generated function has the following structure:
3671 -- function F return String is
3672 -- Pref : string renames Task_Name;
3673 -- T1 : String := Index1'Image (Val1);
3674 -- ...
3675 -- Tn : String := indexn'image (Valn);
3676 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3677 -- -- Len includes commas and the end parentheses.
3678 -- Res : String (1..Len);
3679 -- Pos : Integer := Pref'Length;
3681 -- begin
3682 -- Res (1 .. Pos) := Pref;
3683 -- Pos := Pos + 1;
3684 -- Res (Pos) := '(';
3685 -- Pos := Pos + 1;
3686 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3687 -- Pos := Pos + T1'Length;
3688 -- Res (Pos) := '.';
3689 -- Pos := Pos + 1;
3690 -- ...
3691 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3692 -- Res (Len) := ')';
3694 -- return Res;
3695 -- end F;
3697 -- Needless to say, multidimensional arrays of tasks are rare enough that
3698 -- the bulkiness of this code is not really a concern.
3700 function Build_Task_Array_Image
3701 (Loc : Source_Ptr;
3702 Id_Ref : Node_Id;
3703 A_Type : Entity_Id;
3704 Dyn : Boolean := False) return Node_Id
3706 Dims : constant Nat := Number_Dimensions (A_Type);
3707 -- Number of dimensions for array of tasks
3709 Temps : array (1 .. Dims) of Entity_Id;
3710 -- Array of temporaries to hold string for each index
3712 Indx : Node_Id;
3713 -- Index expression
3715 Len : Entity_Id;
3716 -- Total length of generated name
3718 Pos : Entity_Id;
3719 -- Running index for substring assignments
3721 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3722 -- Name of enclosing variable, prefix of resulting name
3724 Res : Entity_Id;
3725 -- String to hold result
3727 Val : Node_Id;
3728 -- Value of successive indexes
3730 Sum : Node_Id;
3731 -- Expression to compute total size of string
3733 T : Entity_Id;
3734 -- Entity for name at one index position
3736 Decls : constant List_Id := New_List;
3737 Stats : constant List_Id := New_List;
3739 begin
3740 -- For a dynamic task, the name comes from the target variable. For a
3741 -- static one it is a formal of the enclosing init proc.
3743 if Dyn then
3744 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3745 Append_To (Decls,
3746 Make_Object_Declaration (Loc,
3747 Defining_Identifier => Pref,
3748 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3749 Expression =>
3750 Make_String_Literal (Loc,
3751 Strval => String_From_Name_Buffer)));
3753 else
3754 Append_To (Decls,
3755 Make_Object_Renaming_Declaration (Loc,
3756 Defining_Identifier => Pref,
3757 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3758 Name => Make_Identifier (Loc, Name_uTask_Name)));
3759 end if;
3761 Indx := First_Index (A_Type);
3762 Val := First (Expressions (Id_Ref));
3764 for J in 1 .. Dims loop
3765 T := Make_Temporary (Loc, 'T');
3766 Temps (J) := T;
3768 Append_To (Decls,
3769 Make_Object_Declaration (Loc,
3770 Defining_Identifier => T,
3771 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3772 Expression =>
3773 Make_Attribute_Reference (Loc,
3774 Attribute_Name => Name_Image,
3775 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3776 Expressions => New_List (New_Copy_Tree (Val)))));
3778 Next_Index (Indx);
3779 Next (Val);
3780 end loop;
3782 Sum := Make_Integer_Literal (Loc, Dims + 1);
3784 Sum :=
3785 Make_Op_Add (Loc,
3786 Left_Opnd => Sum,
3787 Right_Opnd =>
3788 Make_Attribute_Reference (Loc,
3789 Attribute_Name => Name_Length,
3790 Prefix => New_Occurrence_Of (Pref, Loc),
3791 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3793 for J in 1 .. Dims loop
3794 Sum :=
3795 Make_Op_Add (Loc,
3796 Left_Opnd => Sum,
3797 Right_Opnd =>
3798 Make_Attribute_Reference (Loc,
3799 Attribute_Name => Name_Length,
3800 Prefix =>
3801 New_Occurrence_Of (Temps (J), Loc),
3802 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3803 end loop;
3805 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3807 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3809 Append_To (Stats,
3810 Make_Assignment_Statement (Loc,
3811 Name =>
3812 Make_Indexed_Component (Loc,
3813 Prefix => New_Occurrence_Of (Res, Loc),
3814 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3815 Expression =>
3816 Make_Character_Literal (Loc,
3817 Chars => Name_Find,
3818 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3820 Append_To (Stats,
3821 Make_Assignment_Statement (Loc,
3822 Name => New_Occurrence_Of (Pos, Loc),
3823 Expression =>
3824 Make_Op_Add (Loc,
3825 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3826 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3828 for J in 1 .. Dims loop
3830 Append_To (Stats,
3831 Make_Assignment_Statement (Loc,
3832 Name =>
3833 Make_Slice (Loc,
3834 Prefix => New_Occurrence_Of (Res, Loc),
3835 Discrete_Range =>
3836 Make_Range (Loc,
3837 Low_Bound => New_Occurrence_Of (Pos, Loc),
3838 High_Bound =>
3839 Make_Op_Subtract (Loc,
3840 Left_Opnd =>
3841 Make_Op_Add (Loc,
3842 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3843 Right_Opnd =>
3844 Make_Attribute_Reference (Loc,
3845 Attribute_Name => Name_Length,
3846 Prefix =>
3847 New_Occurrence_Of (Temps (J), Loc),
3848 Expressions =>
3849 New_List (Make_Integer_Literal (Loc, 1)))),
3850 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3852 Expression => New_Occurrence_Of (Temps (J), Loc)));
3854 if J < Dims then
3855 Append_To (Stats,
3856 Make_Assignment_Statement (Loc,
3857 Name => New_Occurrence_Of (Pos, Loc),
3858 Expression =>
3859 Make_Op_Add (Loc,
3860 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3861 Right_Opnd =>
3862 Make_Attribute_Reference (Loc,
3863 Attribute_Name => Name_Length,
3864 Prefix => New_Occurrence_Of (Temps (J), Loc),
3865 Expressions =>
3866 New_List (Make_Integer_Literal (Loc, 1))))));
3868 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3870 Append_To (Stats,
3871 Make_Assignment_Statement (Loc,
3872 Name => Make_Indexed_Component (Loc,
3873 Prefix => New_Occurrence_Of (Res, Loc),
3874 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3875 Expression =>
3876 Make_Character_Literal (Loc,
3877 Chars => Name_Find,
3878 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3880 Append_To (Stats,
3881 Make_Assignment_Statement (Loc,
3882 Name => New_Occurrence_Of (Pos, Loc),
3883 Expression =>
3884 Make_Op_Add (Loc,
3885 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3886 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3887 end if;
3888 end loop;
3890 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3892 Append_To (Stats,
3893 Make_Assignment_Statement (Loc,
3894 Name =>
3895 Make_Indexed_Component (Loc,
3896 Prefix => New_Occurrence_Of (Res, Loc),
3897 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3898 Expression =>
3899 Make_Character_Literal (Loc,
3900 Chars => Name_Find,
3901 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3902 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3903 end Build_Task_Array_Image;
3905 ----------------------------
3906 -- Build_Task_Image_Decls --
3907 ----------------------------
3909 function Build_Task_Image_Decls
3910 (Loc : Source_Ptr;
3911 Id_Ref : Node_Id;
3912 A_Type : Entity_Id;
3913 In_Init_Proc : Boolean := False) return List_Id
3915 Decls : constant List_Id := New_List;
3916 T_Id : Entity_Id := Empty;
3917 Decl : Node_Id;
3918 Expr : Node_Id := Empty;
3919 Fun : Node_Id := Empty;
3920 Is_Dyn : constant Boolean :=
3921 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3922 and then
3923 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3925 begin
3926 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3927 -- generate a dummy declaration only.
3929 if Restriction_Active (No_Implicit_Heap_Allocations)
3930 or else Global_Discard_Names
3931 then
3932 T_Id := Make_Temporary (Loc, 'J');
3933 Name_Len := 0;
3935 return
3936 New_List (
3937 Make_Object_Declaration (Loc,
3938 Defining_Identifier => T_Id,
3939 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3940 Expression =>
3941 Make_String_Literal (Loc,
3942 Strval => String_From_Name_Buffer)));
3944 else
3945 if Nkind (Id_Ref) = N_Identifier
3946 or else Nkind (Id_Ref) = N_Defining_Identifier
3947 then
3948 -- For a simple variable, the image of the task is built from
3949 -- the name of the variable. To avoid possible conflict with the
3950 -- anonymous type created for a single protected object, add a
3951 -- numeric suffix.
3953 T_Id :=
3954 Make_Defining_Identifier (Loc,
3955 New_External_Name (Chars (Id_Ref), 'T', 1));
3957 Get_Name_String (Chars (Id_Ref));
3959 Expr :=
3960 Make_String_Literal (Loc,
3961 Strval => String_From_Name_Buffer);
3963 elsif Nkind (Id_Ref) = N_Selected_Component then
3964 T_Id :=
3965 Make_Defining_Identifier (Loc,
3966 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3967 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3969 elsif Nkind (Id_Ref) = N_Indexed_Component then
3970 T_Id :=
3971 Make_Defining_Identifier (Loc,
3972 New_External_Name (Chars (A_Type), 'N'));
3974 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3975 end if;
3976 end if;
3978 if Present (Fun) then
3979 Append (Fun, Decls);
3980 Expr := Make_Function_Call (Loc,
3981 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
3983 if not In_Init_Proc then
3984 Set_Uses_Sec_Stack (Defining_Entity (Fun));
3985 end if;
3986 end if;
3988 Decl := Make_Object_Declaration (Loc,
3989 Defining_Identifier => T_Id,
3990 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3991 Constant_Present => True,
3992 Expression => Expr);
3994 Append (Decl, Decls);
3995 return Decls;
3996 end Build_Task_Image_Decls;
3998 -------------------------------
3999 -- Build_Task_Image_Function --
4000 -------------------------------
4002 function Build_Task_Image_Function
4003 (Loc : Source_Ptr;
4004 Decls : List_Id;
4005 Stats : List_Id;
4006 Res : Entity_Id) return Node_Id
4008 Spec : Node_Id;
4010 begin
4011 Append_To (Stats,
4012 Make_Simple_Return_Statement (Loc,
4013 Expression => New_Occurrence_Of (Res, Loc)));
4015 Spec := Make_Function_Specification (Loc,
4016 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4017 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4019 -- Calls to 'Image use the secondary stack, which must be cleaned up
4020 -- after the task name is built.
4022 return Make_Subprogram_Body (Loc,
4023 Specification => Spec,
4024 Declarations => Decls,
4025 Handled_Statement_Sequence =>
4026 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4027 end Build_Task_Image_Function;
4029 -----------------------------
4030 -- Build_Task_Image_Prefix --
4031 -----------------------------
4033 procedure Build_Task_Image_Prefix
4034 (Loc : Source_Ptr;
4035 Len : out Entity_Id;
4036 Res : out Entity_Id;
4037 Pos : out Entity_Id;
4038 Prefix : Entity_Id;
4039 Sum : Node_Id;
4040 Decls : List_Id;
4041 Stats : List_Id)
4043 begin
4044 Len := Make_Temporary (Loc, 'L', Sum);
4046 Append_To (Decls,
4047 Make_Object_Declaration (Loc,
4048 Defining_Identifier => Len,
4049 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4050 Expression => Sum));
4052 Res := Make_Temporary (Loc, 'R');
4054 Append_To (Decls,
4055 Make_Object_Declaration (Loc,
4056 Defining_Identifier => Res,
4057 Object_Definition =>
4058 Make_Subtype_Indication (Loc,
4059 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4060 Constraint =>
4061 Make_Index_Or_Discriminant_Constraint (Loc,
4062 Constraints =>
4063 New_List (
4064 Make_Range (Loc,
4065 Low_Bound => Make_Integer_Literal (Loc, 1),
4066 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4068 -- Indicate that the result is an internal temporary, so it does not
4069 -- receive a bogus initialization when declaration is expanded. This
4070 -- is both efficient, and prevents anomalies in the handling of
4071 -- dynamic objects on the secondary stack.
4073 Set_Is_Internal (Res);
4074 Pos := Make_Temporary (Loc, 'P');
4076 Append_To (Decls,
4077 Make_Object_Declaration (Loc,
4078 Defining_Identifier => Pos,
4079 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4081 -- Pos := Prefix'Length;
4083 Append_To (Stats,
4084 Make_Assignment_Statement (Loc,
4085 Name => New_Occurrence_Of (Pos, Loc),
4086 Expression =>
4087 Make_Attribute_Reference (Loc,
4088 Attribute_Name => Name_Length,
4089 Prefix => New_Occurrence_Of (Prefix, Loc),
4090 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4092 -- Res (1 .. Pos) := Prefix;
4094 Append_To (Stats,
4095 Make_Assignment_Statement (Loc,
4096 Name =>
4097 Make_Slice (Loc,
4098 Prefix => New_Occurrence_Of (Res, Loc),
4099 Discrete_Range =>
4100 Make_Range (Loc,
4101 Low_Bound => Make_Integer_Literal (Loc, 1),
4102 High_Bound => New_Occurrence_Of (Pos, Loc))),
4104 Expression => New_Occurrence_Of (Prefix, Loc)));
4106 Append_To (Stats,
4107 Make_Assignment_Statement (Loc,
4108 Name => New_Occurrence_Of (Pos, Loc),
4109 Expression =>
4110 Make_Op_Add (Loc,
4111 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4112 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4113 end Build_Task_Image_Prefix;
4115 -----------------------------
4116 -- Build_Task_Record_Image --
4117 -----------------------------
4119 function Build_Task_Record_Image
4120 (Loc : Source_Ptr;
4121 Id_Ref : Node_Id;
4122 Dyn : Boolean := False) return Node_Id
4124 Len : Entity_Id;
4125 -- Total length of generated name
4127 Pos : Entity_Id;
4128 -- Index into result
4130 Res : Entity_Id;
4131 -- String to hold result
4133 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4134 -- Name of enclosing variable, prefix of resulting name
4136 Sum : Node_Id;
4137 -- Expression to compute total size of string
4139 Sel : Entity_Id;
4140 -- Entity for selector name
4142 Decls : constant List_Id := New_List;
4143 Stats : constant List_Id := New_List;
4145 begin
4146 -- For a dynamic task, the name comes from the target variable. For a
4147 -- static one it is a formal of the enclosing init proc.
4149 if Dyn then
4150 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4151 Append_To (Decls,
4152 Make_Object_Declaration (Loc,
4153 Defining_Identifier => Pref,
4154 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4155 Expression =>
4156 Make_String_Literal (Loc,
4157 Strval => String_From_Name_Buffer)));
4159 else
4160 Append_To (Decls,
4161 Make_Object_Renaming_Declaration (Loc,
4162 Defining_Identifier => Pref,
4163 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4164 Name => Make_Identifier (Loc, Name_uTask_Name)));
4165 end if;
4167 Sel := Make_Temporary (Loc, 'S');
4169 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4171 Append_To (Decls,
4172 Make_Object_Declaration (Loc,
4173 Defining_Identifier => Sel,
4174 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4175 Expression =>
4176 Make_String_Literal (Loc,
4177 Strval => String_From_Name_Buffer)));
4179 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4181 Sum :=
4182 Make_Op_Add (Loc,
4183 Left_Opnd => Sum,
4184 Right_Opnd =>
4185 Make_Attribute_Reference (Loc,
4186 Attribute_Name => Name_Length,
4187 Prefix =>
4188 New_Occurrence_Of (Pref, Loc),
4189 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4191 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4193 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4195 -- Res (Pos) := '.';
4197 Append_To (Stats,
4198 Make_Assignment_Statement (Loc,
4199 Name => Make_Indexed_Component (Loc,
4200 Prefix => New_Occurrence_Of (Res, Loc),
4201 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4202 Expression =>
4203 Make_Character_Literal (Loc,
4204 Chars => Name_Find,
4205 Char_Literal_Value =>
4206 UI_From_Int (Character'Pos ('.')))));
4208 Append_To (Stats,
4209 Make_Assignment_Statement (Loc,
4210 Name => New_Occurrence_Of (Pos, Loc),
4211 Expression =>
4212 Make_Op_Add (Loc,
4213 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4214 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4216 -- Res (Pos .. Len) := Selector;
4218 Append_To (Stats,
4219 Make_Assignment_Statement (Loc,
4220 Name => Make_Slice (Loc,
4221 Prefix => New_Occurrence_Of (Res, Loc),
4222 Discrete_Range =>
4223 Make_Range (Loc,
4224 Low_Bound => New_Occurrence_Of (Pos, Loc),
4225 High_Bound => New_Occurrence_Of (Len, Loc))),
4226 Expression => New_Occurrence_Of (Sel, Loc)));
4228 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4229 end Build_Task_Record_Image;
4231 ---------------------------------------
4232 -- Build_Transient_Object_Statements --
4233 ---------------------------------------
4235 procedure Build_Transient_Object_Statements
4236 (Obj_Decl : Node_Id;
4237 Fin_Call : out Node_Id;
4238 Hook_Assign : out Node_Id;
4239 Hook_Clear : out Node_Id;
4240 Hook_Decl : out Node_Id;
4241 Ptr_Decl : out Node_Id;
4242 Finalize_Obj : Boolean := True)
4244 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4245 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4246 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4248 Desig_Typ : Entity_Id;
4249 Hook_Expr : Node_Id;
4250 Hook_Id : Entity_Id;
4251 Obj_Ref : Node_Id;
4252 Ptr_Typ : Entity_Id;
4254 begin
4255 -- Recover the type of the object
4257 Desig_Typ := Obj_Typ;
4259 if Is_Access_Type (Desig_Typ) then
4260 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4261 end if;
4263 -- Create an access type which provides a reference to the transient
4264 -- object. Generate:
4266 -- type Ptr_Typ is access all Desig_Typ;
4268 Ptr_Typ := Make_Temporary (Loc, 'A');
4269 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4270 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4272 Ptr_Decl :=
4273 Make_Full_Type_Declaration (Loc,
4274 Defining_Identifier => Ptr_Typ,
4275 Type_Definition =>
4276 Make_Access_To_Object_Definition (Loc,
4277 All_Present => True,
4278 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4280 -- Create a temporary check which acts as a hook to the transient
4281 -- object. Generate:
4283 -- Hook : Ptr_Typ := null;
4285 Hook_Id := Make_Temporary (Loc, 'T');
4286 Set_Ekind (Hook_Id, E_Variable);
4287 Set_Etype (Hook_Id, Ptr_Typ);
4289 Hook_Decl :=
4290 Make_Object_Declaration (Loc,
4291 Defining_Identifier => Hook_Id,
4292 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4293 Expression => Make_Null (Loc));
4295 -- Mark the temporary as a hook. This signals the machinery in
4296 -- Build_Finalizer to recognize this special case.
4298 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4300 -- Hook the transient object to the temporary. Generate:
4302 -- Hook := Ptr_Typ (Obj_Id);
4303 -- <or>
4304 -- Hool := Obj_Id'Unrestricted_Access;
4306 if Is_Access_Type (Obj_Typ) then
4307 Hook_Expr :=
4308 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4309 else
4310 Hook_Expr :=
4311 Make_Attribute_Reference (Loc,
4312 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4313 Attribute_Name => Name_Unrestricted_Access);
4314 end if;
4316 Hook_Assign :=
4317 Make_Assignment_Statement (Loc,
4318 Name => New_Occurrence_Of (Hook_Id, Loc),
4319 Expression => Hook_Expr);
4321 -- Crear the hook prior to finalizing the object. Generate:
4323 -- Hook := null;
4325 Hook_Clear :=
4326 Make_Assignment_Statement (Loc,
4327 Name => New_Occurrence_Of (Hook_Id, Loc),
4328 Expression => Make_Null (Loc));
4330 -- Finalize the object. Generate:
4332 -- [Deep_]Finalize (Obj_Ref[.all]);
4334 if Finalize_Obj then
4335 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4337 if Is_Access_Type (Obj_Typ) then
4338 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4339 Set_Etype (Obj_Ref, Desig_Typ);
4340 end if;
4342 Fin_Call :=
4343 Make_Final_Call
4344 (Obj_Ref => Obj_Ref,
4345 Typ => Desig_Typ);
4347 -- Otherwise finalize the hook. Generate:
4349 -- [Deep_]Finalize (Hook.all);
4351 else
4352 Fin_Call :=
4353 Make_Final_Call (
4354 Obj_Ref =>
4355 Make_Explicit_Dereference (Loc,
4356 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4357 Typ => Desig_Typ);
4358 end if;
4359 end Build_Transient_Object_Statements;
4361 -----------------------------
4362 -- Check_Float_Op_Overflow --
4363 -----------------------------
4365 procedure Check_Float_Op_Overflow (N : Node_Id) is
4366 begin
4367 -- Return if no check needed
4369 if not Is_Floating_Point_Type (Etype (N))
4370 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4372 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4373 -- and do not expand the code for float overflow checking.
4375 or else CodePeer_Mode
4376 then
4377 return;
4378 end if;
4380 -- Otherwise we replace the expression by
4382 -- do Tnn : constant ftype := expression;
4383 -- constraint_error when not Tnn'Valid;
4384 -- in Tnn;
4386 declare
4387 Loc : constant Source_Ptr := Sloc (N);
4388 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4389 Typ : constant Entity_Id := Etype (N);
4391 begin
4392 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4393 -- right here. We also set the node as analyzed to prevent infinite
4394 -- recursion from repeating the operation in the expansion.
4396 Set_Do_Overflow_Check (N, False);
4397 Set_Analyzed (N, True);
4399 -- Do the rewrite to include the check
4401 Rewrite (N,
4402 Make_Expression_With_Actions (Loc,
4403 Actions => New_List (
4404 Make_Object_Declaration (Loc,
4405 Defining_Identifier => Tnn,
4406 Object_Definition => New_Occurrence_Of (Typ, Loc),
4407 Constant_Present => True,
4408 Expression => Relocate_Node (N)),
4409 Make_Raise_Constraint_Error (Loc,
4410 Condition =>
4411 Make_Op_Not (Loc,
4412 Right_Opnd =>
4413 Make_Attribute_Reference (Loc,
4414 Prefix => New_Occurrence_Of (Tnn, Loc),
4415 Attribute_Name => Name_Valid)),
4416 Reason => CE_Overflow_Check_Failed)),
4417 Expression => New_Occurrence_Of (Tnn, Loc)));
4419 Analyze_And_Resolve (N, Typ);
4420 end;
4421 end Check_Float_Op_Overflow;
4423 ----------------------------------
4424 -- Component_May_Be_Bit_Aligned --
4425 ----------------------------------
4427 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4428 UT : Entity_Id;
4430 begin
4431 -- If no component clause, then everything is fine, since the back end
4432 -- never bit-misaligns by default, even if there is a pragma Packed for
4433 -- the record.
4435 if No (Comp) or else No (Component_Clause (Comp)) then
4436 return False;
4437 end if;
4439 UT := Underlying_Type (Etype (Comp));
4441 -- It is only array and record types that cause trouble
4443 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4444 return False;
4446 -- If we know that we have a small (64 bits or less) record or small
4447 -- bit-packed array, then everything is fine, since the back end can
4448 -- handle these cases correctly.
4450 elsif Esize (Comp) <= 64
4451 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4452 then
4453 return False;
4455 -- Otherwise if the component is not byte aligned, we know we have the
4456 -- nasty unaligned case.
4458 elsif Normalized_First_Bit (Comp) /= Uint_0
4459 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4460 then
4461 return True;
4463 -- If we are large and byte aligned, then OK at this level
4465 else
4466 return False;
4467 end if;
4468 end Component_May_Be_Bit_Aligned;
4470 ----------------------------------------
4471 -- Containing_Package_With_Ext_Axioms --
4472 ----------------------------------------
4474 function Containing_Package_With_Ext_Axioms
4475 (E : Entity_Id) return Entity_Id
4477 begin
4478 -- E is the package or generic package which is externally axiomatized
4480 if Ekind_In (E, E_Generic_Package, E_Package)
4481 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4482 then
4483 return E;
4484 end if;
4486 -- If E's scope is axiomatized, E is axiomatized
4488 if Present (Scope (E)) then
4489 declare
4490 First_Ax_Parent_Scope : constant Entity_Id :=
4491 Containing_Package_With_Ext_Axioms (Scope (E));
4492 begin
4493 if Present (First_Ax_Parent_Scope) then
4494 return First_Ax_Parent_Scope;
4495 end if;
4496 end;
4497 end if;
4499 -- Otherwise, if E is a package instance, it is axiomatized if the
4500 -- corresponding generic package is axiomatized.
4502 if Ekind (E) = E_Package then
4503 declare
4504 Par : constant Node_Id := Parent (E);
4505 Decl : Node_Id;
4507 begin
4508 if Nkind (Par) = N_Defining_Program_Unit_Name then
4509 Decl := Parent (Par);
4510 else
4511 Decl := Par;
4512 end if;
4514 if Present (Generic_Parent (Decl)) then
4515 return
4516 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4517 end if;
4518 end;
4519 end if;
4521 return Empty;
4522 end Containing_Package_With_Ext_Axioms;
4524 -------------------------------
4525 -- Convert_To_Actual_Subtype --
4526 -------------------------------
4528 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4529 Act_ST : Entity_Id;
4531 begin
4532 Act_ST := Get_Actual_Subtype (Exp);
4534 if Act_ST = Etype (Exp) then
4535 return;
4536 else
4537 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4538 Analyze_And_Resolve (Exp, Act_ST);
4539 end if;
4540 end Convert_To_Actual_Subtype;
4542 -----------------------------------
4543 -- Corresponding_Runtime_Package --
4544 -----------------------------------
4546 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4547 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4548 -- Return True if protected type T has one entry and the maximum queue
4549 -- length is one.
4551 --------------------------------
4552 -- Has_One_Entry_And_No_Queue --
4553 --------------------------------
4555 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4556 Item : Entity_Id;
4557 Is_First : Boolean := True;
4559 begin
4560 Item := First_Entity (T);
4561 while Present (Item) loop
4562 if Is_Entry (Item) then
4564 -- The protected type has more than one entry
4566 if not Is_First then
4567 return False;
4568 end if;
4570 -- The queue length is not one
4572 if not Restriction_Active (No_Entry_Queue)
4573 and then Get_Max_Queue_Length (Item) /= Uint_1
4574 then
4575 return False;
4576 end if;
4578 Is_First := False;
4579 end if;
4581 Next_Entity (Item);
4582 end loop;
4584 return True;
4585 end Has_One_Entry_And_No_Queue;
4587 -- Local variables
4589 Pkg_Id : RTU_Id := RTU_Null;
4591 -- Start of processing for Corresponding_Runtime_Package
4593 begin
4594 pragma Assert (Is_Concurrent_Type (Typ));
4596 if Ekind (Typ) in Protected_Kind then
4597 if Has_Entries (Typ)
4599 -- A protected type without entries that covers an interface and
4600 -- overrides the abstract routines with protected procedures is
4601 -- considered equivalent to a protected type with entries in the
4602 -- context of dispatching select statements. It is sufficient to
4603 -- check for the presence of an interface list in the declaration
4604 -- node to recognize this case.
4606 or else Present (Interface_List (Parent (Typ)))
4608 -- Protected types with interrupt handlers (when not using a
4609 -- restricted profile) are also considered equivalent to
4610 -- protected types with entries. The types which are used
4611 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4612 -- are derived from Protection_Entries.
4614 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4615 or else Has_Interrupt_Handler (Typ)
4616 then
4617 if Abort_Allowed
4618 or else Restriction_Active (No_Select_Statements) = False
4619 or else not Has_One_Entry_And_No_Queue (Typ)
4620 or else (Has_Attach_Handler (Typ)
4621 and then not Restricted_Profile)
4622 then
4623 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4624 else
4625 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4626 end if;
4628 else
4629 Pkg_Id := System_Tasking_Protected_Objects;
4630 end if;
4631 end if;
4633 return Pkg_Id;
4634 end Corresponding_Runtime_Package;
4636 -----------------------------------
4637 -- Current_Sem_Unit_Declarations --
4638 -----------------------------------
4640 function Current_Sem_Unit_Declarations return List_Id is
4641 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4642 Decls : List_Id;
4644 begin
4645 -- If the current unit is a package body, locate the visible
4646 -- declarations of the package spec.
4648 if Nkind (U) = N_Package_Body then
4649 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4650 end if;
4652 if Nkind (U) = N_Package_Declaration then
4653 U := Specification (U);
4654 Decls := Visible_Declarations (U);
4656 if No (Decls) then
4657 Decls := New_List;
4658 Set_Visible_Declarations (U, Decls);
4659 end if;
4661 else
4662 Decls := Declarations (U);
4664 if No (Decls) then
4665 Decls := New_List;
4666 Set_Declarations (U, Decls);
4667 end if;
4668 end if;
4670 return Decls;
4671 end Current_Sem_Unit_Declarations;
4673 -----------------------
4674 -- Duplicate_Subexpr --
4675 -----------------------
4677 function Duplicate_Subexpr
4678 (Exp : Node_Id;
4679 Name_Req : Boolean := False;
4680 Renaming_Req : Boolean := False) return Node_Id
4682 begin
4683 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4684 return New_Copy_Tree (Exp);
4685 end Duplicate_Subexpr;
4687 ---------------------------------
4688 -- Duplicate_Subexpr_No_Checks --
4689 ---------------------------------
4691 function Duplicate_Subexpr_No_Checks
4692 (Exp : Node_Id;
4693 Name_Req : Boolean := False;
4694 Renaming_Req : Boolean := False;
4695 Related_Id : Entity_Id := Empty;
4696 Is_Low_Bound : Boolean := False;
4697 Is_High_Bound : Boolean := False) return Node_Id
4699 New_Exp : Node_Id;
4701 begin
4702 Remove_Side_Effects
4703 (Exp => Exp,
4704 Name_Req => Name_Req,
4705 Renaming_Req => Renaming_Req,
4706 Related_Id => Related_Id,
4707 Is_Low_Bound => Is_Low_Bound,
4708 Is_High_Bound => Is_High_Bound);
4710 New_Exp := New_Copy_Tree (Exp);
4711 Remove_Checks (New_Exp);
4712 return New_Exp;
4713 end Duplicate_Subexpr_No_Checks;
4715 -----------------------------------
4716 -- Duplicate_Subexpr_Move_Checks --
4717 -----------------------------------
4719 function Duplicate_Subexpr_Move_Checks
4720 (Exp : Node_Id;
4721 Name_Req : Boolean := False;
4722 Renaming_Req : Boolean := False) return Node_Id
4724 New_Exp : Node_Id;
4726 begin
4727 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4728 New_Exp := New_Copy_Tree (Exp);
4729 Remove_Checks (Exp);
4730 return New_Exp;
4731 end Duplicate_Subexpr_Move_Checks;
4733 --------------------
4734 -- Ensure_Defined --
4735 --------------------
4737 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4738 IR : Node_Id;
4740 begin
4741 -- An itype reference must only be created if this is a local itype, so
4742 -- that gigi can elaborate it on the proper objstack.
4744 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4745 IR := Make_Itype_Reference (Sloc (N));
4746 Set_Itype (IR, Typ);
4747 Insert_Action (N, IR);
4748 end if;
4749 end Ensure_Defined;
4751 --------------------
4752 -- Entry_Names_OK --
4753 --------------------
4755 function Entry_Names_OK return Boolean is
4756 begin
4757 return
4758 not Restricted_Profile
4759 and then not Global_Discard_Names
4760 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4761 and then not Restriction_Active (No_Local_Allocators);
4762 end Entry_Names_OK;
4764 -------------------
4765 -- Evaluate_Name --
4766 -------------------
4768 procedure Evaluate_Name (Nam : Node_Id) is
4769 begin
4770 -- For an attribute reference or an indexed component, evaluate the
4771 -- prefix, which is itself a name, recursively, and then force the
4772 -- evaluation of all the subscripts (or attribute expressions).
4774 case Nkind (Nam) is
4775 when N_Attribute_Reference
4776 | N_Indexed_Component
4778 Evaluate_Name (Prefix (Nam));
4780 declare
4781 E : Node_Id;
4783 begin
4784 E := First (Expressions (Nam));
4785 while Present (E) loop
4786 Force_Evaluation (E);
4788 if Original_Node (E) /= E then
4789 Set_Do_Range_Check
4790 (E, Do_Range_Check (Original_Node (E)));
4791 end if;
4793 Next (E);
4794 end loop;
4795 end;
4797 -- For an explicit dereference, we simply force the evaluation of
4798 -- the name expression. The dereference provides a value that is the
4799 -- address for the renamed object, and it is precisely this value
4800 -- that we want to preserve.
4802 when N_Explicit_Dereference =>
4803 Force_Evaluation (Prefix (Nam));
4805 -- For a function call, we evaluate the call
4807 when N_Function_Call =>
4808 Force_Evaluation (Nam);
4810 -- For a qualified expression, we evaluate the underlying object
4811 -- name if any, otherwise we force the evaluation of the underlying
4812 -- expression.
4814 when N_Qualified_Expression =>
4815 if Is_Object_Reference (Expression (Nam)) then
4816 Evaluate_Name (Expression (Nam));
4817 else
4818 Force_Evaluation (Expression (Nam));
4819 end if;
4821 -- For a selected component, we simply evaluate the prefix
4823 when N_Selected_Component =>
4824 Evaluate_Name (Prefix (Nam));
4826 -- For a slice, we evaluate the prefix, as for the indexed component
4827 -- case and then, if there is a range present, either directly or as
4828 -- the constraint of a discrete subtype indication, we evaluate the
4829 -- two bounds of this range.
4831 when N_Slice =>
4832 Evaluate_Name (Prefix (Nam));
4833 Evaluate_Slice_Bounds (Nam);
4835 -- For a type conversion, the expression of the conversion must be
4836 -- the name of an object, and we simply need to evaluate this name.
4838 when N_Type_Conversion =>
4839 Evaluate_Name (Expression (Nam));
4841 -- The remaining cases are direct name, operator symbol and character
4842 -- literal. In all these cases, we do nothing, since we want to
4843 -- reevaluate each time the renamed object is used.
4845 when others =>
4846 null;
4847 end case;
4848 end Evaluate_Name;
4850 ---------------------------
4851 -- Evaluate_Slice_Bounds --
4852 ---------------------------
4854 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4855 DR : constant Node_Id := Discrete_Range (Slice);
4856 Constr : Node_Id;
4857 Rexpr : Node_Id;
4859 begin
4860 if Nkind (DR) = N_Range then
4861 Force_Evaluation (Low_Bound (DR));
4862 Force_Evaluation (High_Bound (DR));
4864 elsif Nkind (DR) = N_Subtype_Indication then
4865 Constr := Constraint (DR);
4867 if Nkind (Constr) = N_Range_Constraint then
4868 Rexpr := Range_Expression (Constr);
4870 Force_Evaluation (Low_Bound (Rexpr));
4871 Force_Evaluation (High_Bound (Rexpr));
4872 end if;
4873 end if;
4874 end Evaluate_Slice_Bounds;
4876 ---------------------
4877 -- Evolve_And_Then --
4878 ---------------------
4880 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4881 begin
4882 if No (Cond) then
4883 Cond := Cond1;
4884 else
4885 Cond :=
4886 Make_And_Then (Sloc (Cond1),
4887 Left_Opnd => Cond,
4888 Right_Opnd => Cond1);
4889 end if;
4890 end Evolve_And_Then;
4892 --------------------
4893 -- Evolve_Or_Else --
4894 --------------------
4896 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4897 begin
4898 if No (Cond) then
4899 Cond := Cond1;
4900 else
4901 Cond :=
4902 Make_Or_Else (Sloc (Cond1),
4903 Left_Opnd => Cond,
4904 Right_Opnd => Cond1);
4905 end if;
4906 end Evolve_Or_Else;
4908 -----------------------------------
4909 -- Exceptions_In_Finalization_OK --
4910 -----------------------------------
4912 function Exceptions_In_Finalization_OK return Boolean is
4913 begin
4914 return
4915 not (Restriction_Active (No_Exception_Handlers) or else
4916 Restriction_Active (No_Exception_Propagation) or else
4917 Restriction_Active (No_Exceptions));
4918 end Exceptions_In_Finalization_OK;
4920 -----------------------------------------
4921 -- Expand_Static_Predicates_In_Choices --
4922 -----------------------------------------
4924 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4925 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4927 Choices : constant List_Id := Discrete_Choices (N);
4929 Choice : Node_Id;
4930 Next_C : Node_Id;
4931 P : Node_Id;
4932 C : Node_Id;
4934 begin
4935 Choice := First (Choices);
4936 while Present (Choice) loop
4937 Next_C := Next (Choice);
4939 -- Check for name of subtype with static predicate
4941 if Is_Entity_Name (Choice)
4942 and then Is_Type (Entity (Choice))
4943 and then Has_Predicates (Entity (Choice))
4944 then
4945 -- Loop through entries in predicate list, converting to choices
4946 -- and inserting in the list before the current choice. Note that
4947 -- if the list is empty, corresponding to a False predicate, then
4948 -- no choices are inserted.
4950 P := First (Static_Discrete_Predicate (Entity (Choice)));
4951 while Present (P) loop
4953 -- If low bound and high bounds are equal, copy simple choice
4955 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4956 C := New_Copy (Low_Bound (P));
4958 -- Otherwise copy a range
4960 else
4961 C := New_Copy (P);
4962 end if;
4964 -- Change Sloc to referencing choice (rather than the Sloc of
4965 -- the predicate declaration element itself).
4967 Set_Sloc (C, Sloc (Choice));
4968 Insert_Before (Choice, C);
4969 Next (P);
4970 end loop;
4972 -- Delete the predicated entry
4974 Remove (Choice);
4975 end if;
4977 -- Move to next choice to check
4979 Choice := Next_C;
4980 end loop;
4981 end Expand_Static_Predicates_In_Choices;
4983 ------------------------------
4984 -- Expand_Subtype_From_Expr --
4985 ------------------------------
4987 -- This function is applicable for both static and dynamic allocation of
4988 -- objects which are constrained by an initial expression. Basically it
4989 -- transforms an unconstrained subtype indication into a constrained one.
4991 -- The expression may also be transformed in certain cases in order to
4992 -- avoid multiple evaluation. In the static allocation case, the general
4993 -- scheme is:
4995 -- Val : T := Expr;
4997 -- is transformed into
4999 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
5001 -- Here are the main cases :
5003 -- <if Expr is a Slice>
5004 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5006 -- <elsif Expr is a String Literal>
5007 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5009 -- <elsif Expr is Constrained>
5010 -- subtype T is Type_Of_Expr
5011 -- Val : T := Expr;
5013 -- <elsif Expr is an entity_name>
5014 -- Val : T (constraints taken from Expr) := Expr;
5016 -- <else>
5017 -- type Axxx is access all T;
5018 -- Rval : Axxx := Expr'ref;
5019 -- Val : T (constraints taken from Rval) := Rval.all;
5021 -- ??? note: when the Expression is allocated in the secondary stack
5022 -- we could use it directly instead of copying it by declaring
5023 -- Val : T (...) renames Rval.all
5025 procedure Expand_Subtype_From_Expr
5026 (N : Node_Id;
5027 Unc_Type : Entity_Id;
5028 Subtype_Indic : Node_Id;
5029 Exp : Node_Id;
5030 Related_Id : Entity_Id := Empty)
5032 Loc : constant Source_Ptr := Sloc (N);
5033 Exp_Typ : constant Entity_Id := Etype (Exp);
5034 T : Entity_Id;
5036 begin
5037 -- In general we cannot build the subtype if expansion is disabled,
5038 -- because internal entities may not have been defined. However, to
5039 -- avoid some cascaded errors, we try to continue when the expression is
5040 -- an array (or string), because it is safe to compute the bounds. It is
5041 -- in fact required to do so even in a generic context, because there
5042 -- may be constants that depend on the bounds of a string literal, both
5043 -- standard string types and more generally arrays of characters.
5045 -- In GNATprove mode, these extra subtypes are not needed
5047 if GNATprove_Mode then
5048 return;
5049 end if;
5051 if not Expander_Active
5052 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5053 then
5054 return;
5055 end if;
5057 if Nkind (Exp) = N_Slice then
5058 declare
5059 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5061 begin
5062 Rewrite (Subtype_Indic,
5063 Make_Subtype_Indication (Loc,
5064 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5065 Constraint =>
5066 Make_Index_Or_Discriminant_Constraint (Loc,
5067 Constraints => New_List
5068 (New_Occurrence_Of (Slice_Type, Loc)))));
5070 -- This subtype indication may be used later for constraint checks
5071 -- we better make sure that if a variable was used as a bound of
5072 -- of the original slice, its value is frozen.
5074 Evaluate_Slice_Bounds (Exp);
5075 end;
5077 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5078 Rewrite (Subtype_Indic,
5079 Make_Subtype_Indication (Loc,
5080 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5081 Constraint =>
5082 Make_Index_Or_Discriminant_Constraint (Loc,
5083 Constraints => New_List (
5084 Make_Literal_Range (Loc,
5085 Literal_Typ => Exp_Typ)))));
5087 -- If the type of the expression is an internally generated type it
5088 -- may not be necessary to create a new subtype. However there are two
5089 -- exceptions: references to the current instances, and aliased array
5090 -- object declarations for which the back end has to create a template.
5092 elsif Is_Constrained (Exp_Typ)
5093 and then not Is_Class_Wide_Type (Unc_Type)
5094 and then
5095 (Nkind (N) /= N_Object_Declaration
5096 or else not Is_Entity_Name (Expression (N))
5097 or else not Comes_From_Source (Entity (Expression (N)))
5098 or else not Is_Array_Type (Exp_Typ)
5099 or else not Aliased_Present (N))
5100 then
5101 if Is_Itype (Exp_Typ) then
5103 -- Within an initialization procedure, a selected component
5104 -- denotes a component of the enclosing record, and it appears as
5105 -- an actual in a call to its own initialization procedure. If
5106 -- this component depends on the outer discriminant, we must
5107 -- generate the proper actual subtype for it.
5109 if Nkind (Exp) = N_Selected_Component
5110 and then Within_Init_Proc
5111 then
5112 declare
5113 Decl : constant Node_Id :=
5114 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5115 begin
5116 if Present (Decl) then
5117 Insert_Action (N, Decl);
5118 T := Defining_Identifier (Decl);
5119 else
5120 T := Exp_Typ;
5121 end if;
5122 end;
5124 -- No need to generate a new subtype
5126 else
5127 T := Exp_Typ;
5128 end if;
5130 else
5131 T := Make_Temporary (Loc, 'T');
5133 Insert_Action (N,
5134 Make_Subtype_Declaration (Loc,
5135 Defining_Identifier => T,
5136 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5138 -- This type is marked as an itype even though it has an explicit
5139 -- declaration since otherwise Is_Generic_Actual_Type can get
5140 -- set, resulting in the generation of spurious errors. (See
5141 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5143 Set_Is_Itype (T);
5144 Set_Associated_Node_For_Itype (T, Exp);
5145 end if;
5147 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5149 -- Nothing needs to be done for private types with unknown discriminants
5150 -- if the underlying type is not an unconstrained composite type or it
5151 -- is an unchecked union.
5153 elsif Is_Private_Type (Unc_Type)
5154 and then Has_Unknown_Discriminants (Unc_Type)
5155 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5156 or else Is_Constrained (Underlying_Type (Unc_Type))
5157 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5158 then
5159 null;
5161 -- Case of derived type with unknown discriminants where the parent type
5162 -- also has unknown discriminants.
5164 elsif Is_Record_Type (Unc_Type)
5165 and then not Is_Class_Wide_Type (Unc_Type)
5166 and then Has_Unknown_Discriminants (Unc_Type)
5167 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5168 then
5169 -- Nothing to be done if no underlying record view available
5171 -- If this is a limited type derived from a type with unknown
5172 -- discriminants, do not expand either, so that subsequent expansion
5173 -- of the call can add build-in-place parameters to call.
5175 if No (Underlying_Record_View (Unc_Type))
5176 or else Is_Limited_Type (Unc_Type)
5177 then
5178 null;
5180 -- Otherwise use the Underlying_Record_View to create the proper
5181 -- constrained subtype for an object of a derived type with unknown
5182 -- discriminants.
5184 else
5185 Remove_Side_Effects (Exp);
5186 Rewrite (Subtype_Indic,
5187 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5188 end if;
5190 -- Renamings of class-wide interface types require no equivalent
5191 -- constrained type declarations because we only need to reference
5192 -- the tag component associated with the interface. The same is
5193 -- presumably true for class-wide types in general, so this test
5194 -- is broadened to include all class-wide renamings, which also
5195 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5196 -- (Is this really correct, or are there some cases of class-wide
5197 -- renamings that require action in this procedure???)
5199 elsif Present (N)
5200 and then Nkind (N) = N_Object_Renaming_Declaration
5201 and then Is_Class_Wide_Type (Unc_Type)
5202 then
5203 null;
5205 -- In Ada 95 nothing to be done if the type of the expression is limited
5206 -- because in this case the expression cannot be copied, and its use can
5207 -- only be by reference.
5209 -- In Ada 2005 the context can be an object declaration whose expression
5210 -- is a function that returns in place. If the nominal subtype has
5211 -- unknown discriminants, the call still provides constraints on the
5212 -- object, and we have to create an actual subtype from it.
5214 -- If the type is class-wide, the expression is dynamically tagged and
5215 -- we do not create an actual subtype either. Ditto for an interface.
5216 -- For now this applies only if the type is immutably limited, and the
5217 -- function being called is build-in-place. This will have to be revised
5218 -- when build-in-place functions are generalized to other types.
5220 elsif Is_Limited_View (Exp_Typ)
5221 and then
5222 (Is_Class_Wide_Type (Exp_Typ)
5223 or else Is_Interface (Exp_Typ)
5224 or else not Has_Unknown_Discriminants (Exp_Typ)
5225 or else not Is_Composite_Type (Unc_Type))
5226 then
5227 null;
5229 -- For limited objects initialized with build in place function calls,
5230 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5231 -- node in the expression initializing the object, which breaks the
5232 -- circuitry that detects and adds the additional arguments to the
5233 -- called function.
5235 elsif Is_Build_In_Place_Function_Call (Exp) then
5236 null;
5238 else
5239 Remove_Side_Effects (Exp);
5240 Rewrite (Subtype_Indic,
5241 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5242 end if;
5243 end Expand_Subtype_From_Expr;
5245 ---------------------------------------------
5246 -- Expression_Contains_Primitives_Calls_Of --
5247 ---------------------------------------------
5249 function Expression_Contains_Primitives_Calls_Of
5250 (Expr : Node_Id;
5251 Typ : Entity_Id) return Boolean
5253 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5255 Calls_OK : Boolean := False;
5256 -- This flag is set to True when expression Expr contains at least one
5257 -- call to a nondispatching primitive function of Typ.
5259 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5260 -- Search for nondispatching calls to primitive functions of type Typ
5262 ----------------------------
5263 -- Search_Primitive_Calls --
5264 ----------------------------
5266 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5267 Disp_Typ : Entity_Id;
5268 Subp : Entity_Id;
5270 begin
5271 -- Detect a function call that could denote a nondispatching
5272 -- primitive of the input type.
5274 if Nkind (N) = N_Function_Call
5275 and then Is_Entity_Name (Name (N))
5276 then
5277 Subp := Entity (Name (N));
5279 -- Do not consider function calls with a controlling argument, as
5280 -- those are always dispatching calls.
5282 if Is_Dispatching_Operation (Subp)
5283 and then No (Controlling_Argument (N))
5284 then
5285 Disp_Typ := Find_Dispatching_Type (Subp);
5287 -- To qualify as a suitable primitive, the dispatching type of
5288 -- the function must be the input type.
5290 if Present (Disp_Typ)
5291 and then Unique_Entity (Disp_Typ) = U_Typ
5292 then
5293 Calls_OK := True;
5295 -- There is no need to continue the traversal, as one such
5296 -- call suffices.
5298 return Abandon;
5299 end if;
5300 end if;
5301 end if;
5303 return OK;
5304 end Search_Primitive_Calls;
5306 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5308 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5310 begin
5311 Search_Calls (Expr);
5312 return Calls_OK;
5313 end Expression_Contains_Primitives_Calls_Of;
5315 ----------------------
5316 -- Finalize_Address --
5317 ----------------------
5319 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5320 Utyp : Entity_Id := Typ;
5322 begin
5323 -- Handle protected class-wide or task class-wide types
5325 if Is_Class_Wide_Type (Utyp) then
5326 if Is_Concurrent_Type (Root_Type (Utyp)) then
5327 Utyp := Root_Type (Utyp);
5329 elsif Is_Private_Type (Root_Type (Utyp))
5330 and then Present (Full_View (Root_Type (Utyp)))
5331 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5332 then
5333 Utyp := Full_View (Root_Type (Utyp));
5334 end if;
5335 end if;
5337 -- Handle private types
5339 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5340 Utyp := Full_View (Utyp);
5341 end if;
5343 -- Handle protected and task types
5345 if Is_Concurrent_Type (Utyp)
5346 and then Present (Corresponding_Record_Type (Utyp))
5347 then
5348 Utyp := Corresponding_Record_Type (Utyp);
5349 end if;
5351 Utyp := Underlying_Type (Base_Type (Utyp));
5353 -- Deal with untagged derivation of private views. If the parent is
5354 -- now known to be protected, the finalization routine is the one
5355 -- defined on the corresponding record of the ancestor (corresponding
5356 -- records do not automatically inherit operations, but maybe they
5357 -- should???)
5359 if Is_Untagged_Derivation (Typ) then
5360 if Is_Protected_Type (Typ) then
5361 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5363 else
5364 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5366 if Is_Protected_Type (Utyp) then
5367 Utyp := Corresponding_Record_Type (Utyp);
5368 end if;
5369 end if;
5370 end if;
5372 -- If the underlying_type is a subtype, we are dealing with the
5373 -- completion of a private type. We need to access the base type and
5374 -- generate a conversion to it.
5376 if Utyp /= Base_Type (Utyp) then
5377 pragma Assert (Is_Private_Type (Typ));
5379 Utyp := Base_Type (Utyp);
5380 end if;
5382 -- When dealing with an internally built full view for a type with
5383 -- unknown discriminants, use the original record type.
5385 if Is_Underlying_Record_View (Utyp) then
5386 Utyp := Etype (Utyp);
5387 end if;
5389 return TSS (Utyp, TSS_Finalize_Address);
5390 end Finalize_Address;
5392 -------------------
5393 -- Find_DIC_Type --
5394 -------------------
5396 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id is
5397 Curr_Typ : Entity_Id;
5398 -- The current type being examined in the parent hierarchy traversal
5400 DIC_Typ : Entity_Id;
5401 -- The type which carries the DIC pragma. This variable denotes the
5402 -- partial view when private types are involved.
5404 Par_Typ : Entity_Id;
5405 -- The parent type of the current type. This variable denotes the full
5406 -- view when private types are involved.
5408 begin
5409 -- The input type defines its own DIC pragma, therefore it is the owner
5411 if Has_Own_DIC (Typ) then
5412 DIC_Typ := Typ;
5414 -- Otherwise the DIC pragma is inherited from a parent type
5416 else
5417 pragma Assert (Has_Inherited_DIC (Typ));
5419 -- Climb the parent chain
5421 Curr_Typ := Typ;
5422 loop
5423 -- Inspect the parent type. Do not consider subtypes as they
5424 -- inherit the DIC attributes from their base types.
5426 DIC_Typ := Base_Type (Etype (Curr_Typ));
5428 -- Look at the full view of a private type because the type may
5429 -- have a hidden parent introduced in the full view.
5431 Par_Typ := DIC_Typ;
5433 if Is_Private_Type (Par_Typ)
5434 and then Present (Full_View (Par_Typ))
5435 then
5436 Par_Typ := Full_View (Par_Typ);
5437 end if;
5439 -- Stop the climb once the nearest parent type which defines a DIC
5440 -- pragma of its own is encountered or when the root of the parent
5441 -- chain is reached.
5443 exit when Has_Own_DIC (DIC_Typ) or else Curr_Typ = Par_Typ;
5445 Curr_Typ := Par_Typ;
5446 end loop;
5447 end if;
5449 return DIC_Typ;
5450 end Find_DIC_Type;
5452 ------------------------
5453 -- Find_Interface_ADT --
5454 ------------------------
5456 function Find_Interface_ADT
5457 (T : Entity_Id;
5458 Iface : Entity_Id) return Elmt_Id
5460 ADT : Elmt_Id;
5461 Typ : Entity_Id := T;
5463 begin
5464 pragma Assert (Is_Interface (Iface));
5466 -- Handle private types
5468 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5469 Typ := Full_View (Typ);
5470 end if;
5472 -- Handle access types
5474 if Is_Access_Type (Typ) then
5475 Typ := Designated_Type (Typ);
5476 end if;
5478 -- Handle task and protected types implementing interfaces
5480 if Is_Concurrent_Type (Typ) then
5481 Typ := Corresponding_Record_Type (Typ);
5482 end if;
5484 pragma Assert
5485 (not Is_Class_Wide_Type (Typ)
5486 and then Ekind (Typ) /= E_Incomplete_Type);
5488 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5489 return First_Elmt (Access_Disp_Table (Typ));
5491 else
5492 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5493 while Present (ADT)
5494 and then Present (Related_Type (Node (ADT)))
5495 and then Related_Type (Node (ADT)) /= Iface
5496 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5497 Use_Full_View => True)
5498 loop
5499 Next_Elmt (ADT);
5500 end loop;
5502 pragma Assert (Present (Related_Type (Node (ADT))));
5503 return ADT;
5504 end if;
5505 end Find_Interface_ADT;
5507 ------------------------
5508 -- Find_Interface_Tag --
5509 ------------------------
5511 function Find_Interface_Tag
5512 (T : Entity_Id;
5513 Iface : Entity_Id) return Entity_Id
5515 AI_Tag : Entity_Id;
5516 Found : Boolean := False;
5517 Typ : Entity_Id := T;
5519 procedure Find_Tag (Typ : Entity_Id);
5520 -- Internal subprogram used to recursively climb to the ancestors
5522 --------------
5523 -- Find_Tag --
5524 --------------
5526 procedure Find_Tag (Typ : Entity_Id) is
5527 AI_Elmt : Elmt_Id;
5528 AI : Node_Id;
5530 begin
5531 -- This routine does not handle the case in which the interface is an
5532 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5534 pragma Assert (Typ /= Iface);
5536 -- Climb to the root type handling private types
5538 if Present (Full_View (Etype (Typ))) then
5539 if Full_View (Etype (Typ)) /= Typ then
5540 Find_Tag (Full_View (Etype (Typ)));
5541 end if;
5543 elsif Etype (Typ) /= Typ then
5544 Find_Tag (Etype (Typ));
5545 end if;
5547 -- Traverse the list of interfaces implemented by the type
5549 if not Found
5550 and then Present (Interfaces (Typ))
5551 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5552 then
5553 -- Skip the tag associated with the primary table
5555 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5556 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5557 pragma Assert (Present (AI_Tag));
5559 AI_Elmt := First_Elmt (Interfaces (Typ));
5560 while Present (AI_Elmt) loop
5561 AI := Node (AI_Elmt);
5563 if AI = Iface
5564 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5565 then
5566 Found := True;
5567 return;
5568 end if;
5570 AI_Tag := Next_Tag_Component (AI_Tag);
5571 Next_Elmt (AI_Elmt);
5572 end loop;
5573 end if;
5574 end Find_Tag;
5576 -- Start of processing for Find_Interface_Tag
5578 begin
5579 pragma Assert (Is_Interface (Iface));
5581 -- Handle access types
5583 if Is_Access_Type (Typ) then
5584 Typ := Designated_Type (Typ);
5585 end if;
5587 -- Handle class-wide types
5589 if Is_Class_Wide_Type (Typ) then
5590 Typ := Root_Type (Typ);
5591 end if;
5593 -- Handle private types
5595 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5596 Typ := Full_View (Typ);
5597 end if;
5599 -- Handle entities from the limited view
5601 if Ekind (Typ) = E_Incomplete_Type then
5602 pragma Assert (Present (Non_Limited_View (Typ)));
5603 Typ := Non_Limited_View (Typ);
5604 end if;
5606 -- Handle task and protected types implementing interfaces
5608 if Is_Concurrent_Type (Typ) then
5609 Typ := Corresponding_Record_Type (Typ);
5610 end if;
5612 -- If the interface is an ancestor of the type, then it shared the
5613 -- primary dispatch table.
5615 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5616 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5617 return First_Tag_Component (Typ);
5619 -- Otherwise we need to search for its associated tag component
5621 else
5622 Find_Tag (Typ);
5623 pragma Assert (Found);
5624 return AI_Tag;
5625 end if;
5626 end Find_Interface_Tag;
5628 ---------------------------
5629 -- Find_Optional_Prim_Op --
5630 ---------------------------
5632 function Find_Optional_Prim_Op
5633 (T : Entity_Id; Name : Name_Id) return Entity_Id
5635 Prim : Elmt_Id;
5636 Typ : Entity_Id := T;
5637 Op : Entity_Id;
5639 begin
5640 if Is_Class_Wide_Type (Typ) then
5641 Typ := Root_Type (Typ);
5642 end if;
5644 Typ := Underlying_Type (Typ);
5646 -- Loop through primitive operations
5648 Prim := First_Elmt (Primitive_Operations (Typ));
5649 while Present (Prim) loop
5650 Op := Node (Prim);
5652 -- We can retrieve primitive operations by name if it is an internal
5653 -- name. For equality we must check that both of its operands have
5654 -- the same type, to avoid confusion with user-defined equalities
5655 -- than may have a non-symmetric signature.
5657 exit when Chars (Op) = Name
5658 and then
5659 (Name /= Name_Op_Eq
5660 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5662 Next_Elmt (Prim);
5663 end loop;
5665 return Node (Prim); -- Empty if not found
5666 end Find_Optional_Prim_Op;
5668 ---------------------------
5669 -- Find_Optional_Prim_Op --
5670 ---------------------------
5672 function Find_Optional_Prim_Op
5673 (T : Entity_Id;
5674 Name : TSS_Name_Type) return Entity_Id
5676 Inher_Op : Entity_Id := Empty;
5677 Own_Op : Entity_Id := Empty;
5678 Prim_Elmt : Elmt_Id;
5679 Prim_Id : Entity_Id;
5680 Typ : Entity_Id := T;
5682 begin
5683 if Is_Class_Wide_Type (Typ) then
5684 Typ := Root_Type (Typ);
5685 end if;
5687 Typ := Underlying_Type (Typ);
5689 -- This search is based on the assertion that the dispatching version
5690 -- of the TSS routine always precedes the real primitive.
5692 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5693 while Present (Prim_Elmt) loop
5694 Prim_Id := Node (Prim_Elmt);
5696 if Is_TSS (Prim_Id, Name) then
5697 if Present (Alias (Prim_Id)) then
5698 Inher_Op := Prim_Id;
5699 else
5700 Own_Op := Prim_Id;
5701 end if;
5702 end if;
5704 Next_Elmt (Prim_Elmt);
5705 end loop;
5707 if Present (Own_Op) then
5708 return Own_Op;
5709 elsif Present (Inher_Op) then
5710 return Inher_Op;
5711 else
5712 return Empty;
5713 end if;
5714 end Find_Optional_Prim_Op;
5716 ------------------
5717 -- Find_Prim_Op --
5718 ------------------
5720 function Find_Prim_Op
5721 (T : Entity_Id; Name : Name_Id) return Entity_Id
5723 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5724 begin
5725 if No (Result) then
5726 raise Program_Error;
5727 end if;
5729 return Result;
5730 end Find_Prim_Op;
5732 ------------------
5733 -- Find_Prim_Op --
5734 ------------------
5736 function Find_Prim_Op
5737 (T : Entity_Id;
5738 Name : TSS_Name_Type) return Entity_Id
5740 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5741 begin
5742 if No (Result) then
5743 raise Program_Error;
5744 end if;
5746 return Result;
5747 end Find_Prim_Op;
5749 ----------------------------
5750 -- Find_Protection_Object --
5751 ----------------------------
5753 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5754 S : Entity_Id;
5756 begin
5757 S := Scop;
5758 while Present (S) loop
5759 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5760 and then Present (Protection_Object (S))
5761 then
5762 return Protection_Object (S);
5763 end if;
5765 S := Scope (S);
5766 end loop;
5768 -- If we do not find a Protection object in the scope chain, then
5769 -- something has gone wrong, most likely the object was never created.
5771 raise Program_Error;
5772 end Find_Protection_Object;
5774 --------------------------
5775 -- Find_Protection_Type --
5776 --------------------------
5778 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5779 Comp : Entity_Id;
5780 Typ : Entity_Id := Conc_Typ;
5782 begin
5783 if Is_Concurrent_Type (Typ) then
5784 Typ := Corresponding_Record_Type (Typ);
5785 end if;
5787 -- Since restriction violations are not considered serious errors, the
5788 -- expander remains active, but may leave the corresponding record type
5789 -- malformed. In such cases, component _object is not available so do
5790 -- not look for it.
5792 if not Analyzed (Typ) then
5793 return Empty;
5794 end if;
5796 Comp := First_Component (Typ);
5797 while Present (Comp) loop
5798 if Chars (Comp) = Name_uObject then
5799 return Base_Type (Etype (Comp));
5800 end if;
5802 Next_Component (Comp);
5803 end loop;
5805 -- The corresponding record of a protected type should always have an
5806 -- _object field.
5808 raise Program_Error;
5809 end Find_Protection_Type;
5811 -----------------------
5812 -- Find_Hook_Context --
5813 -----------------------
5815 function Find_Hook_Context (N : Node_Id) return Node_Id is
5816 Par : Node_Id;
5817 Top : Node_Id;
5819 Wrapped_Node : Node_Id;
5820 -- Note: if we are in a transient scope, we want to reuse it as
5821 -- the context for actions insertion, if possible. But if N is itself
5822 -- part of the stored actions for the current transient scope,
5823 -- then we need to insert at the appropriate (inner) location in
5824 -- the not as an action on Node_To_Be_Wrapped.
5826 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5828 begin
5829 -- When the node is inside a case/if expression, the lifetime of any
5830 -- temporary controlled object is extended. Find a suitable insertion
5831 -- node by locating the topmost case or if expressions.
5833 if In_Cond_Expr then
5834 Par := N;
5835 Top := N;
5836 while Present (Par) loop
5837 if Nkind_In (Original_Node (Par), N_Case_Expression,
5838 N_If_Expression)
5839 then
5840 Top := Par;
5842 -- Prevent the search from going too far
5844 elsif Is_Body_Or_Package_Declaration (Par) then
5845 exit;
5846 end if;
5848 Par := Parent (Par);
5849 end loop;
5851 -- The topmost case or if expression is now recovered, but it may
5852 -- still not be the correct place to add generated code. Climb to
5853 -- find a parent that is part of a declarative or statement list,
5854 -- and is not a list of actuals in a call.
5856 Par := Top;
5857 while Present (Par) loop
5858 if Is_List_Member (Par)
5859 and then not Nkind_In (Par, N_Component_Association,
5860 N_Discriminant_Association,
5861 N_Parameter_Association,
5862 N_Pragma_Argument_Association)
5863 and then not Nkind_In (Parent (Par), N_Function_Call,
5864 N_Procedure_Call_Statement,
5865 N_Entry_Call_Statement)
5867 then
5868 return Par;
5870 -- Prevent the search from going too far
5872 elsif Is_Body_Or_Package_Declaration (Par) then
5873 exit;
5874 end if;
5876 Par := Parent (Par);
5877 end loop;
5879 return Par;
5881 else
5882 Par := N;
5883 while Present (Par) loop
5885 -- Keep climbing past various operators
5887 if Nkind (Parent (Par)) in N_Op
5888 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5889 then
5890 Par := Parent (Par);
5891 else
5892 exit;
5893 end if;
5894 end loop;
5896 Top := Par;
5898 -- The node may be located in a pragma in which case return the
5899 -- pragma itself:
5901 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5903 -- Similar case occurs when the node is related to an object
5904 -- declaration or assignment:
5906 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5908 -- Another case to consider is when the node is part of a return
5909 -- statement:
5911 -- return ... and then Ctrl_Func_Call ...;
5913 -- Another case is when the node acts as a formal in a procedure
5914 -- call statement:
5916 -- Proc (... and then Ctrl_Func_Call ...);
5918 if Scope_Is_Transient then
5919 Wrapped_Node := Node_To_Be_Wrapped;
5920 else
5921 Wrapped_Node := Empty;
5922 end if;
5924 while Present (Par) loop
5925 if Par = Wrapped_Node
5926 or else Nkind_In (Par, N_Assignment_Statement,
5927 N_Object_Declaration,
5928 N_Pragma,
5929 N_Procedure_Call_Statement,
5930 N_Simple_Return_Statement)
5931 then
5932 return Par;
5934 -- Prevent the search from going too far
5936 elsif Is_Body_Or_Package_Declaration (Par) then
5937 exit;
5938 end if;
5940 Par := Parent (Par);
5941 end loop;
5943 -- Return the topmost short circuit operator
5945 return Top;
5946 end if;
5947 end Find_Hook_Context;
5949 ------------------------------
5950 -- Following_Address_Clause --
5951 ------------------------------
5953 function Following_Address_Clause (D : Node_Id) return Node_Id is
5954 Id : constant Entity_Id := Defining_Identifier (D);
5955 Result : Node_Id;
5956 Par : Node_Id;
5958 function Check_Decls (D : Node_Id) return Node_Id;
5959 -- This internal function differs from the main function in that it
5960 -- gets called to deal with a following package private part, and
5961 -- it checks declarations starting with D (the main function checks
5962 -- declarations following D). If D is Empty, then Empty is returned.
5964 -----------------
5965 -- Check_Decls --
5966 -----------------
5968 function Check_Decls (D : Node_Id) return Node_Id is
5969 Decl : Node_Id;
5971 begin
5972 Decl := D;
5973 while Present (Decl) loop
5974 if Nkind (Decl) = N_At_Clause
5975 and then Chars (Identifier (Decl)) = Chars (Id)
5976 then
5977 return Decl;
5979 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5980 and then Chars (Decl) = Name_Address
5981 and then Chars (Name (Decl)) = Chars (Id)
5982 then
5983 return Decl;
5984 end if;
5986 Next (Decl);
5987 end loop;
5989 -- Otherwise not found, return Empty
5991 return Empty;
5992 end Check_Decls;
5994 -- Start of processing for Following_Address_Clause
5996 begin
5997 -- If parser detected no address clause for the identifier in question,
5998 -- then the answer is a quick NO, without the need for a search.
6000 if not Get_Name_Table_Boolean1 (Chars (Id)) then
6001 return Empty;
6002 end if;
6004 -- Otherwise search current declarative unit
6006 Result := Check_Decls (Next (D));
6008 if Present (Result) then
6009 return Result;
6010 end if;
6012 -- Check for possible package private part following
6014 Par := Parent (D);
6016 if Nkind (Par) = N_Package_Specification
6017 and then Visible_Declarations (Par) = List_Containing (D)
6018 and then Present (Private_Declarations (Par))
6019 then
6020 -- Private part present, check declarations there
6022 return Check_Decls (First (Private_Declarations (Par)));
6024 else
6025 -- No private part, clause not found, return Empty
6027 return Empty;
6028 end if;
6029 end Following_Address_Clause;
6031 ----------------------
6032 -- Force_Evaluation --
6033 ----------------------
6035 procedure Force_Evaluation
6036 (Exp : Node_Id;
6037 Name_Req : Boolean := False;
6038 Related_Id : Entity_Id := Empty;
6039 Is_Low_Bound : Boolean := False;
6040 Is_High_Bound : Boolean := False;
6041 Mode : Force_Evaluation_Mode := Relaxed)
6043 begin
6044 Remove_Side_Effects
6045 (Exp => Exp,
6046 Name_Req => Name_Req,
6047 Variable_Ref => True,
6048 Renaming_Req => False,
6049 Related_Id => Related_Id,
6050 Is_Low_Bound => Is_Low_Bound,
6051 Is_High_Bound => Is_High_Bound,
6052 Check_Side_Effects =>
6053 Is_Static_Expression (Exp)
6054 or else Mode = Relaxed);
6055 end Force_Evaluation;
6057 ---------------------------------
6058 -- Fully_Qualified_Name_String --
6059 ---------------------------------
6061 function Fully_Qualified_Name_String
6062 (E : Entity_Id;
6063 Append_NUL : Boolean := True) return String_Id
6065 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6066 -- Compute recursively the qualified name without NUL at the end, adding
6067 -- it to the currently started string being generated
6069 ----------------------------------
6070 -- Internal_Full_Qualified_Name --
6071 ----------------------------------
6073 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6074 Ent : Entity_Id;
6076 begin
6077 -- Deal properly with child units
6079 if Nkind (E) = N_Defining_Program_Unit_Name then
6080 Ent := Defining_Identifier (E);
6081 else
6082 Ent := E;
6083 end if;
6085 -- Compute qualification recursively (only "Standard" has no scope)
6087 if Present (Scope (Scope (Ent))) then
6088 Internal_Full_Qualified_Name (Scope (Ent));
6089 Store_String_Char (Get_Char_Code ('.'));
6090 end if;
6092 -- Every entity should have a name except some expanded blocks
6093 -- don't bother about those.
6095 if Chars (Ent) = No_Name then
6096 return;
6097 end if;
6099 -- Generates the entity name in upper case
6101 Get_Decoded_Name_String (Chars (Ent));
6102 Set_All_Upper_Case;
6103 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6104 return;
6105 end Internal_Full_Qualified_Name;
6107 -- Start of processing for Full_Qualified_Name
6109 begin
6110 Start_String;
6111 Internal_Full_Qualified_Name (E);
6113 if Append_NUL then
6114 Store_String_Char (Get_Char_Code (ASCII.NUL));
6115 end if;
6117 return End_String;
6118 end Fully_Qualified_Name_String;
6120 ------------------------
6121 -- Generate_Poll_Call --
6122 ------------------------
6124 procedure Generate_Poll_Call (N : Node_Id) is
6125 begin
6126 -- No poll call if polling not active
6128 if not Polling_Required then
6129 return;
6131 -- Otherwise generate require poll call
6133 else
6134 Insert_Before_And_Analyze (N,
6135 Make_Procedure_Call_Statement (Sloc (N),
6136 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6137 end if;
6138 end Generate_Poll_Call;
6140 ---------------------------------
6141 -- Get_Current_Value_Condition --
6142 ---------------------------------
6144 -- Note: the implementation of this procedure is very closely tied to the
6145 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6146 -- interpret Current_Value fields set by the Set procedure, so the two
6147 -- procedures need to be closely coordinated.
6149 procedure Get_Current_Value_Condition
6150 (Var : Node_Id;
6151 Op : out Node_Kind;
6152 Val : out Node_Id)
6154 Loc : constant Source_Ptr := Sloc (Var);
6155 Ent : constant Entity_Id := Entity (Var);
6157 procedure Process_Current_Value_Condition
6158 (N : Node_Id;
6159 S : Boolean);
6160 -- N is an expression which holds either True (S = True) or False (S =
6161 -- False) in the condition. This procedure digs out the expression and
6162 -- if it refers to Ent, sets Op and Val appropriately.
6164 -------------------------------------
6165 -- Process_Current_Value_Condition --
6166 -------------------------------------
6168 procedure Process_Current_Value_Condition
6169 (N : Node_Id;
6170 S : Boolean)
6172 Cond : Node_Id;
6173 Prev_Cond : Node_Id;
6174 Sens : Boolean;
6176 begin
6177 Cond := N;
6178 Sens := S;
6180 loop
6181 Prev_Cond := Cond;
6183 -- Deal with NOT operators, inverting sense
6185 while Nkind (Cond) = N_Op_Not loop
6186 Cond := Right_Opnd (Cond);
6187 Sens := not Sens;
6188 end loop;
6190 -- Deal with conversions, qualifications, and expressions with
6191 -- actions.
6193 while Nkind_In (Cond,
6194 N_Type_Conversion,
6195 N_Qualified_Expression,
6196 N_Expression_With_Actions)
6197 loop
6198 Cond := Expression (Cond);
6199 end loop;
6201 exit when Cond = Prev_Cond;
6202 end loop;
6204 -- Deal with AND THEN and AND cases
6206 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6208 -- Don't ever try to invert a condition that is of the form of an
6209 -- AND or AND THEN (since we are not doing sufficiently general
6210 -- processing to allow this).
6212 if Sens = False then
6213 Op := N_Empty;
6214 Val := Empty;
6215 return;
6216 end if;
6218 -- Recursively process AND and AND THEN branches
6220 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6222 if Op /= N_Empty then
6223 return;
6224 end if;
6226 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6227 return;
6229 -- Case of relational operator
6231 elsif Nkind (Cond) in N_Op_Compare then
6232 Op := Nkind (Cond);
6234 -- Invert sense of test if inverted test
6236 if Sens = False then
6237 case Op is
6238 when N_Op_Eq => Op := N_Op_Ne;
6239 when N_Op_Ne => Op := N_Op_Eq;
6240 when N_Op_Lt => Op := N_Op_Ge;
6241 when N_Op_Gt => Op := N_Op_Le;
6242 when N_Op_Le => Op := N_Op_Gt;
6243 when N_Op_Ge => Op := N_Op_Lt;
6244 when others => raise Program_Error;
6245 end case;
6246 end if;
6248 -- Case of entity op value
6250 if Is_Entity_Name (Left_Opnd (Cond))
6251 and then Ent = Entity (Left_Opnd (Cond))
6252 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6253 then
6254 Val := Right_Opnd (Cond);
6256 -- Case of value op entity
6258 elsif Is_Entity_Name (Right_Opnd (Cond))
6259 and then Ent = Entity (Right_Opnd (Cond))
6260 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6261 then
6262 Val := Left_Opnd (Cond);
6264 -- We are effectively swapping operands
6266 case Op is
6267 when N_Op_Eq => null;
6268 when N_Op_Ne => null;
6269 when N_Op_Lt => Op := N_Op_Gt;
6270 when N_Op_Gt => Op := N_Op_Lt;
6271 when N_Op_Le => Op := N_Op_Ge;
6272 when N_Op_Ge => Op := N_Op_Le;
6273 when others => raise Program_Error;
6274 end case;
6276 else
6277 Op := N_Empty;
6278 end if;
6280 return;
6282 elsif Nkind_In (Cond,
6283 N_Type_Conversion,
6284 N_Qualified_Expression,
6285 N_Expression_With_Actions)
6286 then
6287 Cond := Expression (Cond);
6289 -- Case of Boolean variable reference, return as though the
6290 -- reference had said var = True.
6292 else
6293 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6294 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6296 if Sens = False then
6297 Op := N_Op_Ne;
6298 else
6299 Op := N_Op_Eq;
6300 end if;
6301 end if;
6302 end if;
6303 end Process_Current_Value_Condition;
6305 -- Start of processing for Get_Current_Value_Condition
6307 begin
6308 Op := N_Empty;
6309 Val := Empty;
6311 -- Immediate return, nothing doing, if this is not an object
6313 if Ekind (Ent) not in Object_Kind then
6314 return;
6315 end if;
6317 -- Otherwise examine current value
6319 declare
6320 CV : constant Node_Id := Current_Value (Ent);
6321 Sens : Boolean;
6322 Stm : Node_Id;
6324 begin
6325 -- If statement. Condition is known true in THEN section, known False
6326 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6328 if Nkind (CV) = N_If_Statement then
6330 -- Before start of IF statement
6332 if Loc < Sloc (CV) then
6333 return;
6335 -- After end of IF statement
6337 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6338 return;
6339 end if;
6341 -- At this stage we know that we are within the IF statement, but
6342 -- unfortunately, the tree does not record the SLOC of the ELSE so
6343 -- we cannot use a simple SLOC comparison to distinguish between
6344 -- the then/else statements, so we have to climb the tree.
6346 declare
6347 N : Node_Id;
6349 begin
6350 N := Parent (Var);
6351 while Parent (N) /= CV loop
6352 N := Parent (N);
6354 -- If we fall off the top of the tree, then that's odd, but
6355 -- perhaps it could occur in some error situation, and the
6356 -- safest response is simply to assume that the outcome of
6357 -- the condition is unknown. No point in bombing during an
6358 -- attempt to optimize things.
6360 if No (N) then
6361 return;
6362 end if;
6363 end loop;
6365 -- Now we have N pointing to a node whose parent is the IF
6366 -- statement in question, so now we can tell if we are within
6367 -- the THEN statements.
6369 if Is_List_Member (N)
6370 and then List_Containing (N) = Then_Statements (CV)
6371 then
6372 Sens := True;
6374 -- If the variable reference does not come from source, we
6375 -- cannot reliably tell whether it appears in the else part.
6376 -- In particular, if it appears in generated code for a node
6377 -- that requires finalization, it may be attached to a list
6378 -- that has not been yet inserted into the code. For now,
6379 -- treat it as unknown.
6381 elsif not Comes_From_Source (N) then
6382 return;
6384 -- Otherwise we must be in ELSIF or ELSE part
6386 else
6387 Sens := False;
6388 end if;
6389 end;
6391 -- ELSIF part. Condition is known true within the referenced
6392 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6393 -- and unknown before the ELSE part or after the IF statement.
6395 elsif Nkind (CV) = N_Elsif_Part then
6397 -- if the Elsif_Part had condition_actions, the elsif has been
6398 -- rewritten as a nested if, and the original elsif_part is
6399 -- detached from the tree, so there is no way to obtain useful
6400 -- information on the current value of the variable.
6401 -- Can this be improved ???
6403 if No (Parent (CV)) then
6404 return;
6405 end if;
6407 Stm := Parent (CV);
6409 -- If the tree has been otherwise rewritten there is nothing
6410 -- else to be done either.
6412 if Nkind (Stm) /= N_If_Statement then
6413 return;
6414 end if;
6416 -- Before start of ELSIF part
6418 if Loc < Sloc (CV) then
6419 return;
6421 -- After end of IF statement
6423 elsif Loc >= Sloc (Stm) +
6424 Text_Ptr (UI_To_Int (End_Span (Stm)))
6425 then
6426 return;
6427 end if;
6429 -- Again we lack the SLOC of the ELSE, so we need to climb the
6430 -- tree to see if we are within the ELSIF part in question.
6432 declare
6433 N : Node_Id;
6435 begin
6436 N := Parent (Var);
6437 while Parent (N) /= Stm loop
6438 N := Parent (N);
6440 -- If we fall off the top of the tree, then that's odd, but
6441 -- perhaps it could occur in some error situation, and the
6442 -- safest response is simply to assume that the outcome of
6443 -- the condition is unknown. No point in bombing during an
6444 -- attempt to optimize things.
6446 if No (N) then
6447 return;
6448 end if;
6449 end loop;
6451 -- Now we have N pointing to a node whose parent is the IF
6452 -- statement in question, so see if is the ELSIF part we want.
6453 -- the THEN statements.
6455 if N = CV then
6456 Sens := True;
6458 -- Otherwise we must be in subsequent ELSIF or ELSE part
6460 else
6461 Sens := False;
6462 end if;
6463 end;
6465 -- Iteration scheme of while loop. The condition is known to be
6466 -- true within the body of the loop.
6468 elsif Nkind (CV) = N_Iteration_Scheme then
6469 declare
6470 Loop_Stmt : constant Node_Id := Parent (CV);
6472 begin
6473 -- Before start of body of loop
6475 if Loc < Sloc (Loop_Stmt) then
6476 return;
6478 -- After end of LOOP statement
6480 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6481 return;
6483 -- We are within the body of the loop
6485 else
6486 Sens := True;
6487 end if;
6488 end;
6490 -- All other cases of Current_Value settings
6492 else
6493 return;
6494 end if;
6496 -- If we fall through here, then we have a reportable condition, Sens
6497 -- is True if the condition is true and False if it needs inverting.
6499 Process_Current_Value_Condition (Condition (CV), Sens);
6500 end;
6501 end Get_Current_Value_Condition;
6503 ---------------------
6504 -- Get_Stream_Size --
6505 ---------------------
6507 function Get_Stream_Size (E : Entity_Id) return Uint is
6508 begin
6509 -- If we have a Stream_Size clause for this type use it
6511 if Has_Stream_Size_Clause (E) then
6512 return Static_Integer (Expression (Stream_Size_Clause (E)));
6514 -- Otherwise the Stream_Size if the size of the type
6516 else
6517 return Esize (E);
6518 end if;
6519 end Get_Stream_Size;
6521 ---------------------------
6522 -- Has_Access_Constraint --
6523 ---------------------------
6525 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6526 Disc : Entity_Id;
6527 T : constant Entity_Id := Etype (E);
6529 begin
6530 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6531 Disc := First_Discriminant (T);
6532 while Present (Disc) loop
6533 if Is_Access_Type (Etype (Disc)) then
6534 return True;
6535 end if;
6537 Next_Discriminant (Disc);
6538 end loop;
6540 return False;
6541 else
6542 return False;
6543 end if;
6544 end Has_Access_Constraint;
6546 -----------------------------------------------------
6547 -- Has_Annotate_Pragma_For_External_Axiomatization --
6548 -----------------------------------------------------
6550 function Has_Annotate_Pragma_For_External_Axiomatization
6551 (E : Entity_Id) return Boolean
6553 function Is_Annotate_Pragma_For_External_Axiomatization
6554 (N : Node_Id) return Boolean;
6555 -- Returns whether N is
6556 -- pragma Annotate (GNATprove, External_Axiomatization);
6558 ----------------------------------------------------
6559 -- Is_Annotate_Pragma_For_External_Axiomatization --
6560 ----------------------------------------------------
6562 -- The general form of pragma Annotate is
6564 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6565 -- ARG ::= NAME | EXPRESSION
6567 -- The first two arguments are by convention intended to refer to an
6568 -- external tool and a tool-specific function. These arguments are
6569 -- not analyzed.
6571 -- The following is used to annotate a package specification which
6572 -- GNATprove should treat specially, because the axiomatization of
6573 -- this unit is given by the user instead of being automatically
6574 -- generated.
6576 -- pragma Annotate (GNATprove, External_Axiomatization);
6578 function Is_Annotate_Pragma_For_External_Axiomatization
6579 (N : Node_Id) return Boolean
6581 Name_GNATprove : constant String :=
6582 "gnatprove";
6583 Name_External_Axiomatization : constant String :=
6584 "external_axiomatization";
6585 -- Special names
6587 begin
6588 if Nkind (N) = N_Pragma
6589 and then Get_Pragma_Id (N) = Pragma_Annotate
6590 and then List_Length (Pragma_Argument_Associations (N)) = 2
6591 then
6592 declare
6593 Arg1 : constant Node_Id :=
6594 First (Pragma_Argument_Associations (N));
6595 Arg2 : constant Node_Id := Next (Arg1);
6596 Nam1 : Name_Id;
6597 Nam2 : Name_Id;
6599 begin
6600 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6601 -- Name_External_Axiomatization so that Name_Find returns the
6602 -- corresponding name. This takes care of all possible casings.
6604 Name_Len := 0;
6605 Add_Str_To_Name_Buffer (Name_GNATprove);
6606 Nam1 := Name_Find;
6608 Name_Len := 0;
6609 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6610 Nam2 := Name_Find;
6612 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6613 and then
6614 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6615 end;
6617 else
6618 return False;
6619 end if;
6620 end Is_Annotate_Pragma_For_External_Axiomatization;
6622 -- Local variables
6624 Decl : Node_Id;
6625 Vis_Decls : List_Id;
6626 N : Node_Id;
6628 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6630 begin
6631 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6632 Decl := Parent (Parent (E));
6633 else
6634 Decl := Parent (E);
6635 end if;
6637 Vis_Decls := Visible_Declarations (Decl);
6639 N := First (Vis_Decls);
6640 while Present (N) loop
6642 -- Skip declarations generated by the frontend. Skip all pragmas
6643 -- that are not the desired Annotate pragma. Stop the search on
6644 -- the first non-pragma source declaration.
6646 if Comes_From_Source (N) then
6647 if Nkind (N) = N_Pragma then
6648 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6649 return True;
6650 end if;
6651 else
6652 return False;
6653 end if;
6654 end if;
6656 Next (N);
6657 end loop;
6659 return False;
6660 end Has_Annotate_Pragma_For_External_Axiomatization;
6662 --------------------
6663 -- Homonym_Number --
6664 --------------------
6666 function Homonym_Number (Subp : Entity_Id) return Nat is
6667 Count : Nat;
6668 Hom : Entity_Id;
6670 begin
6671 Count := 1;
6672 Hom := Homonym (Subp);
6673 while Present (Hom) loop
6674 if Scope (Hom) = Scope (Subp) then
6675 Count := Count + 1;
6676 end if;
6678 Hom := Homonym (Hom);
6679 end loop;
6681 return Count;
6682 end Homonym_Number;
6684 -----------------------------------
6685 -- In_Library_Level_Package_Body --
6686 -----------------------------------
6688 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6689 begin
6690 -- First determine whether the entity appears at the library level, then
6691 -- look at the containing unit.
6693 if Is_Library_Level_Entity (Id) then
6694 declare
6695 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6697 begin
6698 return Nkind (Unit (Container)) = N_Package_Body;
6699 end;
6700 end if;
6702 return False;
6703 end In_Library_Level_Package_Body;
6705 ------------------------------
6706 -- In_Unconditional_Context --
6707 ------------------------------
6709 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6710 P : Node_Id;
6712 begin
6713 P := Node;
6714 while Present (P) loop
6715 case Nkind (P) is
6716 when N_Subprogram_Body => return True;
6717 when N_If_Statement => return False;
6718 when N_Loop_Statement => return False;
6719 when N_Case_Statement => return False;
6720 when others => P := Parent (P);
6721 end case;
6722 end loop;
6724 return False;
6725 end In_Unconditional_Context;
6727 -------------------
6728 -- Insert_Action --
6729 -------------------
6731 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6732 begin
6733 if Present (Ins_Action) then
6734 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6735 end if;
6736 end Insert_Action;
6738 -- Version with check(s) suppressed
6740 procedure Insert_Action
6741 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6743 begin
6744 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6745 end Insert_Action;
6747 -------------------------
6748 -- Insert_Action_After --
6749 -------------------------
6751 procedure Insert_Action_After
6752 (Assoc_Node : Node_Id;
6753 Ins_Action : Node_Id)
6755 begin
6756 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6757 end Insert_Action_After;
6759 --------------------
6760 -- Insert_Actions --
6761 --------------------
6763 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6764 N : Node_Id;
6765 P : Node_Id;
6767 Wrapped_Node : Node_Id := Empty;
6769 begin
6770 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6771 return;
6772 end if;
6774 -- Ignore insert of actions from inside default expression (or other
6775 -- similar "spec expression") in the special spec-expression analyze
6776 -- mode. Any insertions at this point have no relevance, since we are
6777 -- only doing the analyze to freeze the types of any static expressions.
6778 -- See section "Handling of Default Expressions" in the spec of package
6779 -- Sem for further details.
6781 if In_Spec_Expression then
6782 return;
6783 end if;
6785 -- If the action derives from stuff inside a record, then the actions
6786 -- are attached to the current scope, to be inserted and analyzed on
6787 -- exit from the scope. The reason for this is that we may also be
6788 -- generating freeze actions at the same time, and they must eventually
6789 -- be elaborated in the correct order.
6791 if Is_Record_Type (Current_Scope)
6792 and then not Is_Frozen (Current_Scope)
6793 then
6794 if No (Scope_Stack.Table
6795 (Scope_Stack.Last).Pending_Freeze_Actions)
6796 then
6797 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6798 Ins_Actions;
6799 else
6800 Append_List
6801 (Ins_Actions,
6802 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6803 end if;
6805 return;
6806 end if;
6808 -- We now intend to climb up the tree to find the right point to
6809 -- insert the actions. We start at Assoc_Node, unless this node is a
6810 -- subexpression in which case we start with its parent. We do this for
6811 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6812 -- itself one of the special nodes like N_And_Then, then we assume that
6813 -- an initial request to insert actions for such a node does not expect
6814 -- the actions to get deposited in the node for later handling when the
6815 -- node is expanded, since clearly the node is being dealt with by the
6816 -- caller. Note that in the subexpression case, N is always the child we
6817 -- came from.
6819 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6820 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6821 -- Procedure calls, and similarly procedure attribute references, are
6822 -- also statements.
6824 if Nkind (Assoc_Node) in N_Subexpr
6825 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6826 or else Etype (Assoc_Node) /= Standard_Void_Type)
6827 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6828 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6829 or else not Is_Procedure_Attribute_Name
6830 (Attribute_Name (Assoc_Node)))
6831 then
6832 N := Assoc_Node;
6833 P := Parent (Assoc_Node);
6835 -- Non-subexpression case. Note that N is initially Empty in this case
6836 -- (N is only guaranteed Non-Empty in the subexpr case).
6838 else
6839 N := Empty;
6840 P := Assoc_Node;
6841 end if;
6843 -- Capture root of the transient scope
6845 if Scope_Is_Transient then
6846 Wrapped_Node := Node_To_Be_Wrapped;
6847 end if;
6849 loop
6850 pragma Assert (Present (P));
6852 -- Make sure that inserted actions stay in the transient scope
6854 if Present (Wrapped_Node) and then N = Wrapped_Node then
6855 Store_Before_Actions_In_Scope (Ins_Actions);
6856 return;
6857 end if;
6859 case Nkind (P) is
6861 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6862 -- in the Actions field of the right operand. They will be moved
6863 -- out further when the AND THEN or OR ELSE operator is expanded.
6864 -- Nothing special needs to be done for the left operand since
6865 -- in that case the actions are executed unconditionally.
6867 when N_Short_Circuit =>
6868 if N = Right_Opnd (P) then
6870 -- We are now going to either append the actions to the
6871 -- actions field of the short-circuit operation. We will
6872 -- also analyze the actions now.
6874 -- This analysis is really too early, the proper thing would
6875 -- be to just park them there now, and only analyze them if
6876 -- we find we really need them, and to it at the proper
6877 -- final insertion point. However attempting to this proved
6878 -- tricky, so for now we just kill current values before and
6879 -- after the analyze call to make sure we avoid peculiar
6880 -- optimizations from this out of order insertion.
6882 Kill_Current_Values;
6884 -- If P has already been expanded, we can't park new actions
6885 -- on it, so we need to expand them immediately, introducing
6886 -- an Expression_With_Actions. N can't be an expression
6887 -- with actions, or else then the actions would have been
6888 -- inserted at an inner level.
6890 if Analyzed (P) then
6891 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6892 Rewrite (N,
6893 Make_Expression_With_Actions (Sloc (N),
6894 Actions => Ins_Actions,
6895 Expression => Relocate_Node (N)));
6896 Analyze_And_Resolve (N);
6898 elsif Present (Actions (P)) then
6899 Insert_List_After_And_Analyze
6900 (Last (Actions (P)), Ins_Actions);
6901 else
6902 Set_Actions (P, Ins_Actions);
6903 Analyze_List (Actions (P));
6904 end if;
6906 Kill_Current_Values;
6908 return;
6909 end if;
6911 -- Then or Else dependent expression of an if expression. Add
6912 -- actions to Then_Actions or Else_Actions field as appropriate.
6913 -- The actions will be moved further out when the if is expanded.
6915 when N_If_Expression =>
6916 declare
6917 ThenX : constant Node_Id := Next (First (Expressions (P)));
6918 ElseX : constant Node_Id := Next (ThenX);
6920 begin
6921 -- If the enclosing expression is already analyzed, as
6922 -- is the case for nested elaboration checks, insert the
6923 -- conditional further out.
6925 if Analyzed (P) then
6926 null;
6928 -- Actions belong to the then expression, temporarily place
6929 -- them as Then_Actions of the if expression. They will be
6930 -- moved to the proper place later when the if expression
6931 -- is expanded.
6933 elsif N = ThenX then
6934 if Present (Then_Actions (P)) then
6935 Insert_List_After_And_Analyze
6936 (Last (Then_Actions (P)), Ins_Actions);
6937 else
6938 Set_Then_Actions (P, Ins_Actions);
6939 Analyze_List (Then_Actions (P));
6940 end if;
6942 return;
6944 -- Actions belong to the else expression, temporarily place
6945 -- them as Else_Actions of the if expression. They will be
6946 -- moved to the proper place later when the if expression
6947 -- is expanded.
6949 elsif N = ElseX then
6950 if Present (Else_Actions (P)) then
6951 Insert_List_After_And_Analyze
6952 (Last (Else_Actions (P)), Ins_Actions);
6953 else
6954 Set_Else_Actions (P, Ins_Actions);
6955 Analyze_List (Else_Actions (P));
6956 end if;
6958 return;
6960 -- Actions belong to the condition. In this case they are
6961 -- unconditionally executed, and so we can continue the
6962 -- search for the proper insert point.
6964 else
6965 null;
6966 end if;
6967 end;
6969 -- Alternative of case expression, we place the action in the
6970 -- Actions field of the case expression alternative, this will
6971 -- be handled when the case expression is expanded.
6973 when N_Case_Expression_Alternative =>
6974 if Present (Actions (P)) then
6975 Insert_List_After_And_Analyze
6976 (Last (Actions (P)), Ins_Actions);
6977 else
6978 Set_Actions (P, Ins_Actions);
6979 Analyze_List (Actions (P));
6980 end if;
6982 return;
6984 -- Case of appearing within an Expressions_With_Actions node. When
6985 -- the new actions come from the expression of the expression with
6986 -- actions, they must be added to the existing actions. The other
6987 -- alternative is when the new actions are related to one of the
6988 -- existing actions of the expression with actions, and should
6989 -- never reach here: if actions are inserted on a statement
6990 -- within the Actions of an expression with actions, or on some
6991 -- subexpression of such a statement, then the outermost proper
6992 -- insertion point is right before the statement, and we should
6993 -- never climb up as far as the N_Expression_With_Actions itself.
6995 when N_Expression_With_Actions =>
6996 if N = Expression (P) then
6997 if Is_Empty_List (Actions (P)) then
6998 Append_List_To (Actions (P), Ins_Actions);
6999 Analyze_List (Actions (P));
7000 else
7001 Insert_List_After_And_Analyze
7002 (Last (Actions (P)), Ins_Actions);
7003 end if;
7005 return;
7007 else
7008 raise Program_Error;
7009 end if;
7011 -- Case of appearing in the condition of a while expression or
7012 -- elsif. We insert the actions into the Condition_Actions field.
7013 -- They will be moved further out when the while loop or elsif
7014 -- is analyzed.
7016 when N_Elsif_Part
7017 | N_Iteration_Scheme
7019 if N = Condition (P) then
7020 if Present (Condition_Actions (P)) then
7021 Insert_List_After_And_Analyze
7022 (Last (Condition_Actions (P)), Ins_Actions);
7023 else
7024 Set_Condition_Actions (P, Ins_Actions);
7026 -- Set the parent of the insert actions explicitly. This
7027 -- is not a syntactic field, but we need the parent field
7028 -- set, in particular so that freeze can understand that
7029 -- it is dealing with condition actions, and properly
7030 -- insert the freezing actions.
7032 Set_Parent (Ins_Actions, P);
7033 Analyze_List (Condition_Actions (P));
7034 end if;
7036 return;
7037 end if;
7039 -- Statements, declarations, pragmas, representation clauses
7041 when
7042 -- Statements
7044 N_Procedure_Call_Statement
7045 | N_Statement_Other_Than_Procedure_Call
7047 -- Pragmas
7049 | N_Pragma
7051 -- Representation_Clause
7053 | N_At_Clause
7054 | N_Attribute_Definition_Clause
7055 | N_Enumeration_Representation_Clause
7056 | N_Record_Representation_Clause
7058 -- Declarations
7060 | N_Abstract_Subprogram_Declaration
7061 | N_Entry_Body
7062 | N_Exception_Declaration
7063 | N_Exception_Renaming_Declaration
7064 | N_Expression_Function
7065 | N_Formal_Abstract_Subprogram_Declaration
7066 | N_Formal_Concrete_Subprogram_Declaration
7067 | N_Formal_Object_Declaration
7068 | N_Formal_Type_Declaration
7069 | N_Full_Type_Declaration
7070 | N_Function_Instantiation
7071 | N_Generic_Function_Renaming_Declaration
7072 | N_Generic_Package_Declaration
7073 | N_Generic_Package_Renaming_Declaration
7074 | N_Generic_Procedure_Renaming_Declaration
7075 | N_Generic_Subprogram_Declaration
7076 | N_Implicit_Label_Declaration
7077 | N_Incomplete_Type_Declaration
7078 | N_Number_Declaration
7079 | N_Object_Declaration
7080 | N_Object_Renaming_Declaration
7081 | N_Package_Body
7082 | N_Package_Body_Stub
7083 | N_Package_Declaration
7084 | N_Package_Instantiation
7085 | N_Package_Renaming_Declaration
7086 | N_Private_Extension_Declaration
7087 | N_Private_Type_Declaration
7088 | N_Procedure_Instantiation
7089 | N_Protected_Body
7090 | N_Protected_Body_Stub
7091 | N_Protected_Type_Declaration
7092 | N_Single_Task_Declaration
7093 | N_Subprogram_Body
7094 | N_Subprogram_Body_Stub
7095 | N_Subprogram_Declaration
7096 | N_Subprogram_Renaming_Declaration
7097 | N_Subtype_Declaration
7098 | N_Task_Body
7099 | N_Task_Body_Stub
7100 | N_Task_Type_Declaration
7102 -- Use clauses can appear in lists of declarations
7104 | N_Use_Package_Clause
7105 | N_Use_Type_Clause
7107 -- Freeze entity behaves like a declaration or statement
7109 | N_Freeze_Entity
7110 | N_Freeze_Generic_Entity
7112 -- Do not insert here if the item is not a list member (this
7113 -- happens for example with a triggering statement, and the
7114 -- proper approach is to insert before the entire select).
7116 if not Is_List_Member (P) then
7117 null;
7119 -- Do not insert if parent of P is an N_Component_Association
7120 -- node (i.e. we are in the context of an N_Aggregate or
7121 -- N_Extension_Aggregate node. In this case we want to insert
7122 -- before the entire aggregate.
7124 elsif Nkind (Parent (P)) = N_Component_Association then
7125 null;
7127 -- Do not insert if the parent of P is either an N_Variant node
7128 -- or an N_Record_Definition node, meaning in either case that
7129 -- P is a member of a component list, and that therefore the
7130 -- actions should be inserted outside the complete record
7131 -- declaration.
7133 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7134 null;
7136 -- Do not insert freeze nodes within the loop generated for
7137 -- an aggregate, because they may be elaborated too late for
7138 -- subsequent use in the back end: within a package spec the
7139 -- loop is part of the elaboration procedure and is only
7140 -- elaborated during the second pass.
7142 -- If the loop comes from source, or the entity is local to the
7143 -- loop itself it must remain within.
7145 elsif Nkind (Parent (P)) = N_Loop_Statement
7146 and then not Comes_From_Source (Parent (P))
7147 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7148 and then
7149 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7150 then
7151 null;
7153 -- Otherwise we can go ahead and do the insertion
7155 elsif P = Wrapped_Node then
7156 Store_Before_Actions_In_Scope (Ins_Actions);
7157 return;
7159 else
7160 Insert_List_Before_And_Analyze (P, Ins_Actions);
7161 return;
7162 end if;
7164 -- A special case, N_Raise_xxx_Error can act either as a statement
7165 -- or a subexpression. We tell the difference by looking at the
7166 -- Etype. It is set to Standard_Void_Type in the statement case.
7168 when N_Raise_xxx_Error =>
7169 if Etype (P) = Standard_Void_Type then
7170 if P = Wrapped_Node then
7171 Store_Before_Actions_In_Scope (Ins_Actions);
7172 else
7173 Insert_List_Before_And_Analyze (P, Ins_Actions);
7174 end if;
7176 return;
7178 -- In the subexpression case, keep climbing
7180 else
7181 null;
7182 end if;
7184 -- If a component association appears within a loop created for
7185 -- an array aggregate, attach the actions to the association so
7186 -- they can be subsequently inserted within the loop. For other
7187 -- component associations insert outside of the aggregate. For
7188 -- an association that will generate a loop, its Loop_Actions
7189 -- attribute is already initialized (see exp_aggr.adb).
7191 -- The list of Loop_Actions can in turn generate additional ones,
7192 -- that are inserted before the associated node. If the associated
7193 -- node is outside the aggregate, the new actions are collected
7194 -- at the end of the Loop_Actions, to respect the order in which
7195 -- they are to be elaborated.
7197 when N_Component_Association
7198 | N_Iterated_Component_Association
7200 if Nkind (Parent (P)) = N_Aggregate
7201 and then Present (Loop_Actions (P))
7202 then
7203 if Is_Empty_List (Loop_Actions (P)) then
7204 Set_Loop_Actions (P, Ins_Actions);
7205 Analyze_List (Ins_Actions);
7206 else
7207 declare
7208 Decl : Node_Id;
7210 begin
7211 -- Check whether these actions were generated by a
7212 -- declaration that is part of the Loop_Actions for
7213 -- the component_association.
7215 Decl := Assoc_Node;
7216 while Present (Decl) loop
7217 exit when Parent (Decl) = P
7218 and then Is_List_Member (Decl)
7219 and then
7220 List_Containing (Decl) = Loop_Actions (P);
7221 Decl := Parent (Decl);
7222 end loop;
7224 if Present (Decl) then
7225 Insert_List_Before_And_Analyze
7226 (Decl, Ins_Actions);
7227 else
7228 Insert_List_After_And_Analyze
7229 (Last (Loop_Actions (P)), Ins_Actions);
7230 end if;
7231 end;
7232 end if;
7234 return;
7236 else
7237 null;
7238 end if;
7240 -- Special case: an attribute denoting a procedure call
7242 when N_Attribute_Reference =>
7243 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7244 if P = Wrapped_Node then
7245 Store_Before_Actions_In_Scope (Ins_Actions);
7246 else
7247 Insert_List_Before_And_Analyze (P, Ins_Actions);
7248 end if;
7250 return;
7252 -- In the subexpression case, keep climbing
7254 else
7255 null;
7256 end if;
7258 -- Special case: a call marker
7260 when N_Call_Marker =>
7261 if Is_List_Member (P) then
7262 Insert_List_Before_And_Analyze (P, Ins_Actions);
7263 return;
7264 end if;
7266 -- A contract node should not belong to the tree
7268 when N_Contract =>
7269 raise Program_Error;
7271 -- For all other node types, keep climbing tree
7273 when N_Abortable_Part
7274 | N_Accept_Alternative
7275 | N_Access_Definition
7276 | N_Access_Function_Definition
7277 | N_Access_Procedure_Definition
7278 | N_Access_To_Object_Definition
7279 | N_Aggregate
7280 | N_Allocator
7281 | N_Aspect_Specification
7282 | N_Case_Expression
7283 | N_Case_Statement_Alternative
7284 | N_Character_Literal
7285 | N_Compilation_Unit
7286 | N_Compilation_Unit_Aux
7287 | N_Component_Clause
7288 | N_Component_Declaration
7289 | N_Component_Definition
7290 | N_Component_List
7291 | N_Constrained_Array_Definition
7292 | N_Decimal_Fixed_Point_Definition
7293 | N_Defining_Character_Literal
7294 | N_Defining_Identifier
7295 | N_Defining_Operator_Symbol
7296 | N_Defining_Program_Unit_Name
7297 | N_Delay_Alternative
7298 | N_Delta_Aggregate
7299 | N_Delta_Constraint
7300 | N_Derived_Type_Definition
7301 | N_Designator
7302 | N_Digits_Constraint
7303 | N_Discriminant_Association
7304 | N_Discriminant_Specification
7305 | N_Empty
7306 | N_Entry_Body_Formal_Part
7307 | N_Entry_Call_Alternative
7308 | N_Entry_Declaration
7309 | N_Entry_Index_Specification
7310 | N_Enumeration_Type_Definition
7311 | N_Error
7312 | N_Exception_Handler
7313 | N_Expanded_Name
7314 | N_Explicit_Dereference
7315 | N_Extension_Aggregate
7316 | N_Floating_Point_Definition
7317 | N_Formal_Decimal_Fixed_Point_Definition
7318 | N_Formal_Derived_Type_Definition
7319 | N_Formal_Discrete_Type_Definition
7320 | N_Formal_Floating_Point_Definition
7321 | N_Formal_Modular_Type_Definition
7322 | N_Formal_Ordinary_Fixed_Point_Definition
7323 | N_Formal_Package_Declaration
7324 | N_Formal_Private_Type_Definition
7325 | N_Formal_Incomplete_Type_Definition
7326 | N_Formal_Signed_Integer_Type_Definition
7327 | N_Function_Call
7328 | N_Function_Specification
7329 | N_Generic_Association
7330 | N_Handled_Sequence_Of_Statements
7331 | N_Identifier
7332 | N_In
7333 | N_Index_Or_Discriminant_Constraint
7334 | N_Indexed_Component
7335 | N_Integer_Literal
7336 | N_Iterator_Specification
7337 | N_Itype_Reference
7338 | N_Label
7339 | N_Loop_Parameter_Specification
7340 | N_Mod_Clause
7341 | N_Modular_Type_Definition
7342 | N_Not_In
7343 | N_Null
7344 | N_Op_Abs
7345 | N_Op_Add
7346 | N_Op_And
7347 | N_Op_Concat
7348 | N_Op_Divide
7349 | N_Op_Eq
7350 | N_Op_Expon
7351 | N_Op_Ge
7352 | N_Op_Gt
7353 | N_Op_Le
7354 | N_Op_Lt
7355 | N_Op_Minus
7356 | N_Op_Mod
7357 | N_Op_Multiply
7358 | N_Op_Ne
7359 | N_Op_Not
7360 | N_Op_Or
7361 | N_Op_Plus
7362 | N_Op_Rem
7363 | N_Op_Rotate_Left
7364 | N_Op_Rotate_Right
7365 | N_Op_Shift_Left
7366 | N_Op_Shift_Right
7367 | N_Op_Shift_Right_Arithmetic
7368 | N_Op_Subtract
7369 | N_Op_Xor
7370 | N_Operator_Symbol
7371 | N_Ordinary_Fixed_Point_Definition
7372 | N_Others_Choice
7373 | N_Package_Specification
7374 | N_Parameter_Association
7375 | N_Parameter_Specification
7376 | N_Pop_Constraint_Error_Label
7377 | N_Pop_Program_Error_Label
7378 | N_Pop_Storage_Error_Label
7379 | N_Pragma_Argument_Association
7380 | N_Procedure_Specification
7381 | N_Protected_Definition
7382 | N_Push_Constraint_Error_Label
7383 | N_Push_Program_Error_Label
7384 | N_Push_Storage_Error_Label
7385 | N_Qualified_Expression
7386 | N_Quantified_Expression
7387 | N_Raise_Expression
7388 | N_Range
7389 | N_Range_Constraint
7390 | N_Real_Literal
7391 | N_Real_Range_Specification
7392 | N_Record_Definition
7393 | N_Reference
7394 | N_SCIL_Dispatch_Table_Tag_Init
7395 | N_SCIL_Dispatching_Call
7396 | N_SCIL_Membership_Test
7397 | N_Selected_Component
7398 | N_Signed_Integer_Type_Definition
7399 | N_Single_Protected_Declaration
7400 | N_Slice
7401 | N_String_Literal
7402 | N_Subtype_Indication
7403 | N_Subunit
7404 | N_Target_Name
7405 | N_Task_Definition
7406 | N_Terminate_Alternative
7407 | N_Triggering_Alternative
7408 | N_Type_Conversion
7409 | N_Unchecked_Expression
7410 | N_Unchecked_Type_Conversion
7411 | N_Unconstrained_Array_Definition
7412 | N_Unused_At_End
7413 | N_Unused_At_Start
7414 | N_Variant
7415 | N_Variant_Part
7416 | N_Validate_Unchecked_Conversion
7417 | N_With_Clause
7419 null;
7420 end case;
7422 -- If we fall through above tests, keep climbing tree
7424 N := P;
7426 if Nkind (Parent (N)) = N_Subunit then
7428 -- This is the proper body corresponding to a stub. Insertion must
7429 -- be done at the point of the stub, which is in the declarative
7430 -- part of the parent unit.
7432 P := Corresponding_Stub (Parent (N));
7434 else
7435 P := Parent (N);
7436 end if;
7437 end loop;
7438 end Insert_Actions;
7440 -- Version with check(s) suppressed
7442 procedure Insert_Actions
7443 (Assoc_Node : Node_Id;
7444 Ins_Actions : List_Id;
7445 Suppress : Check_Id)
7447 begin
7448 if Suppress = All_Checks then
7449 declare
7450 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7451 begin
7452 Scope_Suppress.Suppress := (others => True);
7453 Insert_Actions (Assoc_Node, Ins_Actions);
7454 Scope_Suppress.Suppress := Sva;
7455 end;
7457 else
7458 declare
7459 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7460 begin
7461 Scope_Suppress.Suppress (Suppress) := True;
7462 Insert_Actions (Assoc_Node, Ins_Actions);
7463 Scope_Suppress.Suppress (Suppress) := Svg;
7464 end;
7465 end if;
7466 end Insert_Actions;
7468 --------------------------
7469 -- Insert_Actions_After --
7470 --------------------------
7472 procedure Insert_Actions_After
7473 (Assoc_Node : Node_Id;
7474 Ins_Actions : List_Id)
7476 begin
7477 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7478 Store_After_Actions_In_Scope (Ins_Actions);
7479 else
7480 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7481 end if;
7482 end Insert_Actions_After;
7484 ------------------------
7485 -- Insert_Declaration --
7486 ------------------------
7488 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7489 P : Node_Id;
7491 begin
7492 pragma Assert (Nkind (N) in N_Subexpr);
7494 -- Climb until we find a procedure or a package
7496 P := N;
7497 loop
7498 pragma Assert (Present (Parent (P)));
7499 P := Parent (P);
7501 if Is_List_Member (P) then
7502 exit when Nkind_In (Parent (P), N_Package_Specification,
7503 N_Subprogram_Body);
7505 -- Special handling for handled sequence of statements, we must
7506 -- insert in the statements not the exception handlers!
7508 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7509 P := First (Statements (Parent (P)));
7510 exit;
7511 end if;
7512 end if;
7513 end loop;
7515 -- Now do the insertion
7517 Insert_Before (P, Decl);
7518 Analyze (Decl);
7519 end Insert_Declaration;
7521 ---------------------------------
7522 -- Insert_Library_Level_Action --
7523 ---------------------------------
7525 procedure Insert_Library_Level_Action (N : Node_Id) is
7526 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7528 begin
7529 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7530 -- And not Main_Unit as previously. If the main unit is a body,
7531 -- the scope needed to analyze the actions is the entity of the
7532 -- corresponding declaration.
7534 if No (Actions (Aux)) then
7535 Set_Actions (Aux, New_List (N));
7536 else
7537 Append (N, Actions (Aux));
7538 end if;
7540 Analyze (N);
7541 Pop_Scope;
7542 end Insert_Library_Level_Action;
7544 ----------------------------------
7545 -- Insert_Library_Level_Actions --
7546 ----------------------------------
7548 procedure Insert_Library_Level_Actions (L : List_Id) is
7549 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7551 begin
7552 if Is_Non_Empty_List (L) then
7553 Push_Scope (Cunit_Entity (Main_Unit));
7554 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7556 if No (Actions (Aux)) then
7557 Set_Actions (Aux, L);
7558 Analyze_List (L);
7559 else
7560 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7561 end if;
7563 Pop_Scope;
7564 end if;
7565 end Insert_Library_Level_Actions;
7567 ----------------------
7568 -- Inside_Init_Proc --
7569 ----------------------
7571 function Inside_Init_Proc return Boolean is
7572 S : Entity_Id;
7574 begin
7575 S := Current_Scope;
7576 while Present (S) and then S /= Standard_Standard loop
7577 if Is_Init_Proc (S) then
7578 return True;
7579 else
7580 S := Scope (S);
7581 end if;
7582 end loop;
7584 return False;
7585 end Inside_Init_Proc;
7587 ----------------------------
7588 -- Is_All_Null_Statements --
7589 ----------------------------
7591 function Is_All_Null_Statements (L : List_Id) return Boolean is
7592 Stm : Node_Id;
7594 begin
7595 Stm := First (L);
7596 while Present (Stm) loop
7597 if Nkind (Stm) /= N_Null_Statement then
7598 return False;
7599 end if;
7601 Next (Stm);
7602 end loop;
7604 return True;
7605 end Is_All_Null_Statements;
7607 --------------------------------------------------
7608 -- Is_Displacement_Of_Object_Or_Function_Result --
7609 --------------------------------------------------
7611 function Is_Displacement_Of_Object_Or_Function_Result
7612 (Obj_Id : Entity_Id) return Boolean
7614 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7615 -- Determine whether node N denotes a controlled function call
7617 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
7618 -- Determine whether node N denotes a generalized indexing form which
7619 -- involves a controlled result.
7621 function Is_Displace_Call (N : Node_Id) return Boolean;
7622 -- Determine whether node N denotes a call to Ada.Tags.Displace
7624 function Is_Source_Object (N : Node_Id) return Boolean;
7625 -- Determine whether a particular node denotes a source object
7627 function Strip (N : Node_Id) return Node_Id;
7628 -- Examine arbitrary node N by stripping various indirections and return
7629 -- the "real" node.
7631 ---------------------------------
7632 -- Is_Controlled_Function_Call --
7633 ---------------------------------
7635 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7636 Expr : Node_Id;
7638 begin
7639 -- When a function call appears in Object.Operation format, the
7640 -- original representation has several possible forms depending on
7641 -- the availability and form of actual parameters:
7643 -- Obj.Func N_Selected_Component
7644 -- Obj.Func (Actual) N_Indexed_Component
7645 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7646 -- N_Selected_Component
7648 Expr := Original_Node (N);
7649 loop
7650 if Nkind (Expr) = N_Function_Call then
7651 Expr := Name (Expr);
7653 -- "Obj.Func (Actual)" case
7655 elsif Nkind (Expr) = N_Indexed_Component then
7656 Expr := Prefix (Expr);
7658 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7660 elsif Nkind (Expr) = N_Selected_Component then
7661 Expr := Selector_Name (Expr);
7663 else
7664 exit;
7665 end if;
7666 end loop;
7668 return
7669 Nkind (Expr) in N_Has_Entity
7670 and then Present (Entity (Expr))
7671 and then Ekind (Entity (Expr)) = E_Function
7672 and then Needs_Finalization (Etype (Entity (Expr)));
7673 end Is_Controlled_Function_Call;
7675 ----------------------------
7676 -- Is_Controlled_Indexing --
7677 ----------------------------
7679 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
7680 Expr : constant Node_Id := Original_Node (N);
7682 begin
7683 return
7684 Nkind (Expr) = N_Indexed_Component
7685 and then Present (Generalized_Indexing (Expr))
7686 and then Needs_Finalization (Etype (Expr));
7687 end Is_Controlled_Indexing;
7689 ----------------------
7690 -- Is_Displace_Call --
7691 ----------------------
7693 function Is_Displace_Call (N : Node_Id) return Boolean is
7694 Call : constant Node_Id := Strip (N);
7696 begin
7697 return
7698 Present (Call)
7699 and then Nkind (Call) = N_Function_Call
7700 and then Nkind (Name (Call)) in N_Has_Entity
7701 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7702 end Is_Displace_Call;
7704 ----------------------
7705 -- Is_Source_Object --
7706 ----------------------
7708 function Is_Source_Object (N : Node_Id) return Boolean is
7709 Obj : constant Node_Id := Strip (N);
7711 begin
7712 return
7713 Present (Obj)
7714 and then Comes_From_Source (Obj)
7715 and then Nkind (Obj) in N_Has_Entity
7716 and then Is_Object (Entity (Obj));
7717 end Is_Source_Object;
7719 -----------
7720 -- Strip --
7721 -----------
7723 function Strip (N : Node_Id) return Node_Id is
7724 Result : Node_Id;
7726 begin
7727 Result := N;
7728 loop
7729 if Nkind (Result) = N_Explicit_Dereference then
7730 Result := Prefix (Result);
7732 elsif Nkind_In (Result, N_Type_Conversion,
7733 N_Unchecked_Type_Conversion)
7734 then
7735 Result := Expression (Result);
7737 else
7738 exit;
7739 end if;
7740 end loop;
7742 return Result;
7743 end Strip;
7745 -- Local variables
7747 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
7748 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7749 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
7750 Orig_Expr : Node_Id;
7752 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7754 begin
7755 -- Case 1:
7757 -- Obj : CW_Type := Function_Call (...);
7759 -- is rewritten into:
7761 -- Temp : ... := Function_Call (...)'reference;
7762 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7764 -- where the return type of the function and the class-wide type require
7765 -- dispatch table pointer displacement.
7767 -- Case 2:
7769 -- Obj : CW_Type := Container (...);
7771 -- is rewritten into:
7773 -- Temp : ... := Function_Call (Container, ...)'reference;
7774 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7776 -- where the container element type and the class-wide type require
7777 -- dispatch table pointer dispacement.
7779 -- Case 3:
7781 -- Obj : CW_Type := Src_Obj;
7783 -- is rewritten into:
7785 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7787 -- where the type of the source object and the class-wide type require
7788 -- dispatch table pointer displacement.
7790 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
7791 and then Is_Class_Wide_Type (Obj_Typ)
7792 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7793 and then Nkind (Orig_Decl) = N_Object_Declaration
7794 and then Comes_From_Source (Orig_Decl)
7795 then
7796 Orig_Expr := Expression (Orig_Decl);
7798 return
7799 Is_Controlled_Function_Call (Orig_Expr)
7800 or else Is_Controlled_Indexing (Orig_Expr)
7801 or else Is_Source_Object (Orig_Expr);
7802 end if;
7804 return False;
7805 end Is_Displacement_Of_Object_Or_Function_Result;
7807 ------------------------------
7808 -- Is_Finalizable_Transient --
7809 ------------------------------
7811 function Is_Finalizable_Transient
7812 (Decl : Node_Id;
7813 Rel_Node : Node_Id) return Boolean
7815 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7816 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7818 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7819 -- Determine whether transient object Trans_Id is initialized either
7820 -- by a function call which returns an access type or simply renames
7821 -- another pointer.
7823 function Initialized_By_Aliased_BIP_Func_Call
7824 (Trans_Id : Entity_Id) return Boolean;
7825 -- Determine whether transient object Trans_Id is initialized by a
7826 -- build-in-place function call where the BIPalloc parameter is of
7827 -- value 1 and BIPaccess is not null. This case creates an aliasing
7828 -- between the returned value and the value denoted by BIPaccess.
7830 function Is_Aliased
7831 (Trans_Id : Entity_Id;
7832 First_Stmt : Node_Id) return Boolean;
7833 -- Determine whether transient object Trans_Id has been renamed or
7834 -- aliased through 'reference in the statement list starting from
7835 -- First_Stmt.
7837 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7838 -- Determine whether transient object Trans_Id is allocated on the heap
7840 function Is_Iterated_Container
7841 (Trans_Id : Entity_Id;
7842 First_Stmt : Node_Id) return Boolean;
7843 -- Determine whether transient object Trans_Id denotes a container which
7844 -- is in the process of being iterated in the statement list starting
7845 -- from First_Stmt.
7847 ---------------------------
7848 -- Initialized_By_Access --
7849 ---------------------------
7851 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7852 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7854 begin
7855 return
7856 Present (Expr)
7857 and then Nkind (Expr) /= N_Reference
7858 and then Is_Access_Type (Etype (Expr));
7859 end Initialized_By_Access;
7861 ------------------------------------------
7862 -- Initialized_By_Aliased_BIP_Func_Call --
7863 ------------------------------------------
7865 function Initialized_By_Aliased_BIP_Func_Call
7866 (Trans_Id : Entity_Id) return Boolean
7868 Call : Node_Id := Expression (Parent (Trans_Id));
7870 begin
7871 -- Build-in-place calls usually appear in 'reference format
7873 if Nkind (Call) = N_Reference then
7874 Call := Prefix (Call);
7875 end if;
7877 Call := Unqual_Conv (Call);
7879 if Is_Build_In_Place_Function_Call (Call) then
7880 declare
7881 Access_Nam : Name_Id := No_Name;
7882 Access_OK : Boolean := False;
7883 Actual : Node_Id;
7884 Alloc_Nam : Name_Id := No_Name;
7885 Alloc_OK : Boolean := False;
7886 Formal : Node_Id;
7887 Func_Id : Entity_Id;
7888 Param : Node_Id;
7890 begin
7891 -- Examine all parameter associations of the function call
7893 Param := First (Parameter_Associations (Call));
7894 while Present (Param) loop
7895 if Nkind (Param) = N_Parameter_Association
7896 and then Nkind (Selector_Name (Param)) = N_Identifier
7897 then
7898 Actual := Explicit_Actual_Parameter (Param);
7899 Formal := Selector_Name (Param);
7901 -- Construct the names of formals BIPaccess and BIPalloc
7902 -- using the function name retrieved from an arbitrary
7903 -- formal.
7905 if Access_Nam = No_Name
7906 and then Alloc_Nam = No_Name
7907 and then Present (Entity (Formal))
7908 then
7909 Func_Id := Scope (Entity (Formal));
7911 Access_Nam :=
7912 New_External_Name (Chars (Func_Id),
7913 BIP_Formal_Suffix (BIP_Object_Access));
7915 Alloc_Nam :=
7916 New_External_Name (Chars (Func_Id),
7917 BIP_Formal_Suffix (BIP_Alloc_Form));
7918 end if;
7920 -- A match for BIPaccess => Temp has been found
7922 if Chars (Formal) = Access_Nam
7923 and then Nkind (Actual) /= N_Null
7924 then
7925 Access_OK := True;
7926 end if;
7928 -- A match for BIPalloc => 1 has been found
7930 if Chars (Formal) = Alloc_Nam
7931 and then Nkind (Actual) = N_Integer_Literal
7932 and then Intval (Actual) = Uint_1
7933 then
7934 Alloc_OK := True;
7935 end if;
7936 end if;
7938 Next (Param);
7939 end loop;
7941 return Access_OK and Alloc_OK;
7942 end;
7943 end if;
7945 return False;
7946 end Initialized_By_Aliased_BIP_Func_Call;
7948 ----------------
7949 -- Is_Aliased --
7950 ----------------
7952 function Is_Aliased
7953 (Trans_Id : Entity_Id;
7954 First_Stmt : Node_Id) return Boolean
7956 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7957 -- Given an object renaming declaration, retrieve the entity of the
7958 -- renamed name. Return Empty if the renamed name is anything other
7959 -- than a variable or a constant.
7961 -------------------------
7962 -- Find_Renamed_Object --
7963 -------------------------
7965 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7966 Ren_Obj : Node_Id := Empty;
7968 function Find_Object (N : Node_Id) return Traverse_Result;
7969 -- Try to detect an object which is either a constant or a
7970 -- variable.
7972 -----------------
7973 -- Find_Object --
7974 -----------------
7976 function Find_Object (N : Node_Id) return Traverse_Result is
7977 begin
7978 -- Stop the search once a constant or a variable has been
7979 -- detected.
7981 if Nkind (N) = N_Identifier
7982 and then Present (Entity (N))
7983 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7984 then
7985 Ren_Obj := Entity (N);
7986 return Abandon;
7987 end if;
7989 return OK;
7990 end Find_Object;
7992 procedure Search is new Traverse_Proc (Find_Object);
7994 -- Local variables
7996 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7998 -- Start of processing for Find_Renamed_Object
8000 begin
8001 -- Actions related to dispatching calls may appear as renamings of
8002 -- tags. Do not process this type of renaming because it does not
8003 -- use the actual value of the object.
8005 if not Is_RTE (Typ, RE_Tag_Ptr) then
8006 Search (Name (Ren_Decl));
8007 end if;
8009 return Ren_Obj;
8010 end Find_Renamed_Object;
8012 -- Local variables
8014 Expr : Node_Id;
8015 Ren_Obj : Entity_Id;
8016 Stmt : Node_Id;
8018 -- Start of processing for Is_Aliased
8020 begin
8021 -- A controlled transient object is not considered aliased when it
8022 -- appears inside an expression_with_actions node even when there are
8023 -- explicit aliases of it:
8025 -- do
8026 -- Trans_Id : Ctrl_Typ ...; -- transient object
8027 -- Alias : ... := Trans_Id; -- object is aliased
8028 -- Val : constant Boolean :=
8029 -- ... Alias ...; -- aliasing ends
8030 -- <finalize Trans_Id> -- object safe to finalize
8031 -- in Val end;
8033 -- Expansion ensures that all aliases are encapsulated in the actions
8034 -- list and do not leak to the expression by forcing the evaluation
8035 -- of the expression.
8037 if Nkind (Rel_Node) = N_Expression_With_Actions then
8038 return False;
8040 -- Otherwise examine the statements after the controlled transient
8041 -- object and look for various forms of aliasing.
8043 else
8044 Stmt := First_Stmt;
8045 while Present (Stmt) loop
8046 if Nkind (Stmt) = N_Object_Declaration then
8047 Expr := Expression (Stmt);
8049 -- Aliasing of the form:
8050 -- Obj : ... := Trans_Id'reference;
8052 if Present (Expr)
8053 and then Nkind (Expr) = N_Reference
8054 and then Nkind (Prefix (Expr)) = N_Identifier
8055 and then Entity (Prefix (Expr)) = Trans_Id
8056 then
8057 return True;
8058 end if;
8060 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8061 Ren_Obj := Find_Renamed_Object (Stmt);
8063 -- Aliasing of the form:
8064 -- Obj : ... renames ... Trans_Id ...;
8066 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8067 return True;
8068 end if;
8069 end if;
8071 Next (Stmt);
8072 end loop;
8074 return False;
8075 end if;
8076 end Is_Aliased;
8078 ------------------
8079 -- Is_Allocated --
8080 ------------------
8082 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8083 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8084 begin
8085 return
8086 Is_Access_Type (Etype (Trans_Id))
8087 and then Present (Expr)
8088 and then Nkind (Expr) = N_Allocator;
8089 end Is_Allocated;
8091 ---------------------------
8092 -- Is_Iterated_Container --
8093 ---------------------------
8095 function Is_Iterated_Container
8096 (Trans_Id : Entity_Id;
8097 First_Stmt : Node_Id) return Boolean
8099 Aspect : Node_Id;
8100 Call : Node_Id;
8101 Iter : Entity_Id;
8102 Param : Node_Id;
8103 Stmt : Node_Id;
8104 Typ : Entity_Id;
8106 begin
8107 -- It is not possible to iterate over containers in non-Ada 2012 code
8109 if Ada_Version < Ada_2012 then
8110 return False;
8111 end if;
8113 Typ := Etype (Trans_Id);
8115 -- Handle access type created for secondary stack use
8117 if Is_Access_Type (Typ) then
8118 Typ := Designated_Type (Typ);
8119 end if;
8121 -- Look for aspect Default_Iterator. It may be part of a type
8122 -- declaration for a container, or inherited from a base type
8123 -- or parent type.
8125 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8127 if Present (Aspect) then
8128 Iter := Entity (Aspect);
8130 -- Examine the statements following the container object and
8131 -- look for a call to the default iterate routine where the
8132 -- first parameter is the transient. Such a call appears as:
8134 -- It : Access_To_CW_Iterator :=
8135 -- Iterate (Tran_Id.all, ...)'reference;
8137 Stmt := First_Stmt;
8138 while Present (Stmt) loop
8140 -- Detect an object declaration which is initialized by a
8141 -- secondary stack function call.
8143 if Nkind (Stmt) = N_Object_Declaration
8144 and then Present (Expression (Stmt))
8145 and then Nkind (Expression (Stmt)) = N_Reference
8146 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8147 then
8148 Call := Prefix (Expression (Stmt));
8150 -- The call must invoke the default iterate routine of
8151 -- the container and the transient object must appear as
8152 -- the first actual parameter. Skip any calls whose names
8153 -- are not entities.
8155 if Is_Entity_Name (Name (Call))
8156 and then Entity (Name (Call)) = Iter
8157 and then Present (Parameter_Associations (Call))
8158 then
8159 Param := First (Parameter_Associations (Call));
8161 if Nkind (Param) = N_Explicit_Dereference
8162 and then Entity (Prefix (Param)) = Trans_Id
8163 then
8164 return True;
8165 end if;
8166 end if;
8167 end if;
8169 Next (Stmt);
8170 end loop;
8171 end if;
8173 return False;
8174 end Is_Iterated_Container;
8176 -- Local variables
8178 Desig : Entity_Id := Obj_Typ;
8180 -- Start of processing for Is_Finalizable_Transient
8182 begin
8183 -- Handle access types
8185 if Is_Access_Type (Desig) then
8186 Desig := Available_View (Designated_Type (Desig));
8187 end if;
8189 return
8190 Ekind_In (Obj_Id, E_Constant, E_Variable)
8191 and then Needs_Finalization (Desig)
8192 and then Requires_Transient_Scope (Desig)
8193 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8195 -- Do not consider a transient object that was already processed
8197 and then not Is_Finalized_Transient (Obj_Id)
8199 -- Do not consider renamed or 'reference-d transient objects because
8200 -- the act of renaming extends the object's lifetime.
8202 and then not Is_Aliased (Obj_Id, Decl)
8204 -- Do not consider transient objects allocated on the heap since
8205 -- they are attached to a finalization master.
8207 and then not Is_Allocated (Obj_Id)
8209 -- If the transient object is a pointer, check that it is not
8210 -- initialized by a function that returns a pointer or acts as a
8211 -- renaming of another pointer.
8213 and then
8214 (not Is_Access_Type (Obj_Typ)
8215 or else not Initialized_By_Access (Obj_Id))
8217 -- Do not consider transient objects which act as indirect aliases
8218 -- of build-in-place function results.
8220 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8222 -- Do not consider conversions of tags to class-wide types
8224 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8226 -- Do not consider iterators because those are treated as normal
8227 -- controlled objects and are processed by the usual finalization
8228 -- machinery. This avoids the double finalization of an iterator.
8230 and then not Is_Iterator (Desig)
8232 -- Do not consider containers in the context of iterator loops. Such
8233 -- transient objects must exist for as long as the loop is around,
8234 -- otherwise any operation carried out by the iterator will fail.
8236 and then not Is_Iterated_Container (Obj_Id, Decl);
8237 end Is_Finalizable_Transient;
8239 ---------------------------------
8240 -- Is_Fully_Repped_Tagged_Type --
8241 ---------------------------------
8243 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8244 U : constant Entity_Id := Underlying_Type (T);
8245 Comp : Entity_Id;
8247 begin
8248 if No (U) or else not Is_Tagged_Type (U) then
8249 return False;
8250 elsif Has_Discriminants (U) then
8251 return False;
8252 elsif not Has_Specified_Layout (U) then
8253 return False;
8254 end if;
8256 -- Here we have a tagged type, see if it has any unlayed out fields
8257 -- other than a possible tag and parent fields. If so, we return False.
8259 Comp := First_Component (U);
8260 while Present (Comp) loop
8261 if not Is_Tag (Comp)
8262 and then Chars (Comp) /= Name_uParent
8263 and then No (Component_Clause (Comp))
8264 then
8265 return False;
8266 else
8267 Next_Component (Comp);
8268 end if;
8269 end loop;
8271 -- All components are layed out
8273 return True;
8274 end Is_Fully_Repped_Tagged_Type;
8276 ----------------------------------
8277 -- Is_Library_Level_Tagged_Type --
8278 ----------------------------------
8280 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8281 begin
8282 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8283 end Is_Library_Level_Tagged_Type;
8285 --------------------------
8286 -- Is_Non_BIP_Func_Call --
8287 --------------------------
8289 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8290 begin
8291 -- The expected call is of the format
8293 -- Func_Call'reference
8295 return
8296 Nkind (Expr) = N_Reference
8297 and then Nkind (Prefix (Expr)) = N_Function_Call
8298 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8299 end Is_Non_BIP_Func_Call;
8301 ----------------------------------
8302 -- Is_Possibly_Unaligned_Object --
8303 ----------------------------------
8305 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8306 T : constant Entity_Id := Etype (N);
8308 begin
8309 -- If renamed object, apply test to underlying object
8311 if Is_Entity_Name (N)
8312 and then Is_Object (Entity (N))
8313 and then Present (Renamed_Object (Entity (N)))
8314 then
8315 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8316 end if;
8318 -- Tagged and controlled types and aliased types are always aligned, as
8319 -- are concurrent types.
8321 if Is_Aliased (T)
8322 or else Has_Controlled_Component (T)
8323 or else Is_Concurrent_Type (T)
8324 or else Is_Tagged_Type (T)
8325 or else Is_Controlled (T)
8326 then
8327 return False;
8328 end if;
8330 -- If this is an element of a packed array, may be unaligned
8332 if Is_Ref_To_Bit_Packed_Array (N) then
8333 return True;
8334 end if;
8336 -- Case of indexed component reference: test whether prefix is unaligned
8338 if Nkind (N) = N_Indexed_Component then
8339 return Is_Possibly_Unaligned_Object (Prefix (N));
8341 -- Case of selected component reference
8343 elsif Nkind (N) = N_Selected_Component then
8344 declare
8345 P : constant Node_Id := Prefix (N);
8346 C : constant Entity_Id := Entity (Selector_Name (N));
8347 M : Nat;
8348 S : Nat;
8350 begin
8351 -- If component reference is for an array with non-static bounds,
8352 -- then it is always aligned: we can only process unaligned arrays
8353 -- with static bounds (more precisely compile time known bounds).
8355 if Is_Array_Type (T)
8356 and then not Compile_Time_Known_Bounds (T)
8357 then
8358 return False;
8359 end if;
8361 -- If component is aliased, it is definitely properly aligned
8363 if Is_Aliased (C) then
8364 return False;
8365 end if;
8367 -- If component is for a type implemented as a scalar, and the
8368 -- record is packed, and the component is other than the first
8369 -- component of the record, then the component may be unaligned.
8371 if Is_Packed (Etype (P))
8372 and then Represented_As_Scalar (Etype (C))
8373 and then First_Entity (Scope (C)) /= C
8374 then
8375 return True;
8376 end if;
8378 -- Compute maximum possible alignment for T
8380 -- If alignment is known, then that settles things
8382 if Known_Alignment (T) then
8383 M := UI_To_Int (Alignment (T));
8385 -- If alignment is not known, tentatively set max alignment
8387 else
8388 M := Ttypes.Maximum_Alignment;
8390 -- We can reduce this if the Esize is known since the default
8391 -- alignment will never be more than the smallest power of 2
8392 -- that does not exceed this Esize value.
8394 if Known_Esize (T) then
8395 S := UI_To_Int (Esize (T));
8397 while (M / 2) >= S loop
8398 M := M / 2;
8399 end loop;
8400 end if;
8401 end if;
8403 -- The following code is historical, it used to be present but it
8404 -- is too cautious, because the front-end does not know the proper
8405 -- default alignments for the target. Also, if the alignment is
8406 -- not known, the front end can't know in any case. If a copy is
8407 -- needed, the back-end will take care of it. This whole section
8408 -- including this comment can be removed later ???
8410 -- If the component reference is for a record that has a specified
8411 -- alignment, and we either know it is too small, or cannot tell,
8412 -- then the component may be unaligned.
8414 -- What is the following commented out code ???
8416 -- if Known_Alignment (Etype (P))
8417 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8418 -- and then M > Alignment (Etype (P))
8419 -- then
8420 -- return True;
8421 -- end if;
8423 -- Case of component clause present which may specify an
8424 -- unaligned position.
8426 if Present (Component_Clause (C)) then
8428 -- Otherwise we can do a test to make sure that the actual
8429 -- start position in the record, and the length, are both
8430 -- consistent with the required alignment. If not, we know
8431 -- that we are unaligned.
8433 declare
8434 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8435 begin
8436 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
8437 or else Esize (C) mod Align_In_Bits /= 0
8438 then
8439 return True;
8440 end if;
8441 end;
8442 end if;
8444 -- Otherwise, for a component reference, test prefix
8446 return Is_Possibly_Unaligned_Object (P);
8447 end;
8449 -- If not a component reference, must be aligned
8451 else
8452 return False;
8453 end if;
8454 end Is_Possibly_Unaligned_Object;
8456 ---------------------------------
8457 -- Is_Possibly_Unaligned_Slice --
8458 ---------------------------------
8460 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8461 begin
8462 -- Go to renamed object
8464 if Is_Entity_Name (N)
8465 and then Is_Object (Entity (N))
8466 and then Present (Renamed_Object (Entity (N)))
8467 then
8468 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8469 end if;
8471 -- The reference must be a slice
8473 if Nkind (N) /= N_Slice then
8474 return False;
8475 end if;
8477 -- We only need to worry if the target has strict alignment
8479 if not Target_Strict_Alignment then
8480 return False;
8481 end if;
8483 -- If it is a slice, then look at the array type being sliced
8485 declare
8486 Sarr : constant Node_Id := Prefix (N);
8487 -- Prefix of the slice, i.e. the array being sliced
8489 Styp : constant Entity_Id := Etype (Prefix (N));
8490 -- Type of the array being sliced
8492 Pref : Node_Id;
8493 Ptyp : Entity_Id;
8495 begin
8496 -- The problems arise if the array object that is being sliced
8497 -- is a component of a record or array, and we cannot guarantee
8498 -- the alignment of the array within its containing object.
8500 -- To investigate this, we look at successive prefixes to see
8501 -- if we have a worrisome indexed or selected component.
8503 Pref := Sarr;
8504 loop
8505 -- Case of array is part of an indexed component reference
8507 if Nkind (Pref) = N_Indexed_Component then
8508 Ptyp := Etype (Prefix (Pref));
8510 -- The only problematic case is when the array is packed, in
8511 -- which case we really know nothing about the alignment of
8512 -- individual components.
8514 if Is_Bit_Packed_Array (Ptyp) then
8515 return True;
8516 end if;
8518 -- Case of array is part of a selected component reference
8520 elsif Nkind (Pref) = N_Selected_Component then
8521 Ptyp := Etype (Prefix (Pref));
8523 -- We are definitely in trouble if the record in question
8524 -- has an alignment, and either we know this alignment is
8525 -- inconsistent with the alignment of the slice, or we don't
8526 -- know what the alignment of the slice should be.
8528 if Known_Alignment (Ptyp)
8529 and then (Unknown_Alignment (Styp)
8530 or else Alignment (Styp) > Alignment (Ptyp))
8531 then
8532 return True;
8533 end if;
8535 -- We are in potential trouble if the record type is packed.
8536 -- We could special case when we know that the array is the
8537 -- first component, but that's not such a simple case ???
8539 if Is_Packed (Ptyp) then
8540 return True;
8541 end if;
8543 -- We are in trouble if there is a component clause, and
8544 -- either we do not know the alignment of the slice, or
8545 -- the alignment of the slice is inconsistent with the
8546 -- bit position specified by the component clause.
8548 declare
8549 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8550 begin
8551 if Present (Component_Clause (Field))
8552 and then
8553 (Unknown_Alignment (Styp)
8554 or else
8555 (Component_Bit_Offset (Field) mod
8556 (System_Storage_Unit * Alignment (Styp))) /= 0)
8557 then
8558 return True;
8559 end if;
8560 end;
8562 -- For cases other than selected or indexed components we know we
8563 -- are OK, since no issues arise over alignment.
8565 else
8566 return False;
8567 end if;
8569 -- We processed an indexed component or selected component
8570 -- reference that looked safe, so keep checking prefixes.
8572 Pref := Prefix (Pref);
8573 end loop;
8574 end;
8575 end Is_Possibly_Unaligned_Slice;
8577 -------------------------------
8578 -- Is_Related_To_Func_Return --
8579 -------------------------------
8581 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8582 Expr : constant Node_Id := Related_Expression (Id);
8583 begin
8584 return
8585 Present (Expr)
8586 and then Nkind (Expr) = N_Explicit_Dereference
8587 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8588 end Is_Related_To_Func_Return;
8590 --------------------------------
8591 -- Is_Ref_To_Bit_Packed_Array --
8592 --------------------------------
8594 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8595 Result : Boolean;
8596 Expr : Node_Id;
8598 begin
8599 if Is_Entity_Name (N)
8600 and then Is_Object (Entity (N))
8601 and then Present (Renamed_Object (Entity (N)))
8602 then
8603 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8604 end if;
8606 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8607 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8608 Result := True;
8609 else
8610 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8611 end if;
8613 if Result and then Nkind (N) = N_Indexed_Component then
8614 Expr := First (Expressions (N));
8615 while Present (Expr) loop
8616 Force_Evaluation (Expr);
8617 Next (Expr);
8618 end loop;
8619 end if;
8621 return Result;
8623 else
8624 return False;
8625 end if;
8626 end Is_Ref_To_Bit_Packed_Array;
8628 --------------------------------
8629 -- Is_Ref_To_Bit_Packed_Slice --
8630 --------------------------------
8632 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8633 begin
8634 if Nkind (N) = N_Type_Conversion then
8635 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8637 elsif Is_Entity_Name (N)
8638 and then Is_Object (Entity (N))
8639 and then Present (Renamed_Object (Entity (N)))
8640 then
8641 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8643 elsif Nkind (N) = N_Slice
8644 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8645 then
8646 return True;
8648 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8649 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8651 else
8652 return False;
8653 end if;
8654 end Is_Ref_To_Bit_Packed_Slice;
8656 -----------------------
8657 -- Is_Renamed_Object --
8658 -----------------------
8660 function Is_Renamed_Object (N : Node_Id) return Boolean is
8661 Pnod : constant Node_Id := Parent (N);
8662 Kind : constant Node_Kind := Nkind (Pnod);
8663 begin
8664 if Kind = N_Object_Renaming_Declaration then
8665 return True;
8666 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8667 return Is_Renamed_Object (Pnod);
8668 else
8669 return False;
8670 end if;
8671 end Is_Renamed_Object;
8673 --------------------------------------
8674 -- Is_Secondary_Stack_BIP_Func_Call --
8675 --------------------------------------
8677 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8678 Alloc_Nam : Name_Id := No_Name;
8679 Actual : Node_Id;
8680 Call : Node_Id := Expr;
8681 Formal : Node_Id;
8682 Param : Node_Id;
8684 begin
8685 -- Build-in-place calls usually appear in 'reference format. Note that
8686 -- the accessibility check machinery may add an extra 'reference due to
8687 -- side effect removal.
8689 while Nkind (Call) = N_Reference loop
8690 Call := Prefix (Call);
8691 end loop;
8693 Call := Unqual_Conv (Call);
8695 if Is_Build_In_Place_Function_Call (Call) then
8697 -- Examine all parameter associations of the function call
8699 Param := First (Parameter_Associations (Call));
8700 while Present (Param) loop
8701 if Nkind (Param) = N_Parameter_Association then
8702 Formal := Selector_Name (Param);
8703 Actual := Explicit_Actual_Parameter (Param);
8705 -- Construct the name of formal BIPalloc. It is much easier to
8706 -- extract the name of the function using an arbitrary formal's
8707 -- scope rather than the Name field of Call.
8709 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8710 Alloc_Nam :=
8711 New_External_Name
8712 (Chars (Scope (Entity (Formal))),
8713 BIP_Formal_Suffix (BIP_Alloc_Form));
8714 end if;
8716 -- A match for BIPalloc => 2 has been found
8718 if Chars (Formal) = Alloc_Nam
8719 and then Nkind (Actual) = N_Integer_Literal
8720 and then Intval (Actual) = Uint_2
8721 then
8722 return True;
8723 end if;
8724 end if;
8726 Next (Param);
8727 end loop;
8728 end if;
8730 return False;
8731 end Is_Secondary_Stack_BIP_Func_Call;
8733 -------------------------------------
8734 -- Is_Tag_To_Class_Wide_Conversion --
8735 -------------------------------------
8737 function Is_Tag_To_Class_Wide_Conversion
8738 (Obj_Id : Entity_Id) return Boolean
8740 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8742 begin
8743 return
8744 Is_Class_Wide_Type (Etype (Obj_Id))
8745 and then Present (Expr)
8746 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8747 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8748 end Is_Tag_To_Class_Wide_Conversion;
8750 ----------------------------
8751 -- Is_Untagged_Derivation --
8752 ----------------------------
8754 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8755 begin
8756 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8757 or else
8758 (Is_Private_Type (T) and then Present (Full_View (T))
8759 and then not Is_Tagged_Type (Full_View (T))
8760 and then Is_Derived_Type (Full_View (T))
8761 and then Etype (Full_View (T)) /= T);
8762 end Is_Untagged_Derivation;
8764 ------------------------------------
8765 -- Is_Untagged_Private_Derivation --
8766 ------------------------------------
8768 function Is_Untagged_Private_Derivation
8769 (Priv_Typ : Entity_Id;
8770 Full_Typ : Entity_Id) return Boolean
8772 begin
8773 return
8774 Present (Priv_Typ)
8775 and then Is_Untagged_Derivation (Priv_Typ)
8776 and then Is_Private_Type (Etype (Priv_Typ))
8777 and then Present (Full_Typ)
8778 and then Is_Itype (Full_Typ);
8779 end Is_Untagged_Private_Derivation;
8781 ---------------------------
8782 -- Is_Volatile_Reference --
8783 ---------------------------
8785 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8786 begin
8787 -- Only source references are to be treated as volatile, internally
8788 -- generated stuff cannot have volatile external effects.
8790 if not Comes_From_Source (N) then
8791 return False;
8793 -- Never true for reference to a type
8795 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8796 return False;
8798 -- Never true for a compile time known constant
8800 elsif Compile_Time_Known_Value (N) then
8801 return False;
8803 -- True if object reference with volatile type
8805 elsif Is_Volatile_Object (N) then
8806 return True;
8808 -- True if reference to volatile entity
8810 elsif Is_Entity_Name (N) then
8811 return Treat_As_Volatile (Entity (N));
8813 -- True for slice of volatile array
8815 elsif Nkind (N) = N_Slice then
8816 return Is_Volatile_Reference (Prefix (N));
8818 -- True if volatile component
8820 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8821 if (Is_Entity_Name (Prefix (N))
8822 and then Has_Volatile_Components (Entity (Prefix (N))))
8823 or else (Present (Etype (Prefix (N)))
8824 and then Has_Volatile_Components (Etype (Prefix (N))))
8825 then
8826 return True;
8827 else
8828 return Is_Volatile_Reference (Prefix (N));
8829 end if;
8831 -- Otherwise false
8833 else
8834 return False;
8835 end if;
8836 end Is_Volatile_Reference;
8838 --------------------
8839 -- Kill_Dead_Code --
8840 --------------------
8842 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8843 W : Boolean := Warn;
8844 -- Set False if warnings suppressed
8846 begin
8847 if Present (N) then
8848 Remove_Warning_Messages (N);
8850 -- Update the internal structures of the ABE mechanism in case the
8851 -- dead node is an elaboration scenario.
8853 Kill_Elaboration_Scenario (N);
8855 -- Generate warning if appropriate
8857 if W then
8859 -- We suppress the warning if this code is under control of an
8860 -- if statement, whose condition is a simple identifier, and
8861 -- either we are in an instance, or warnings off is set for this
8862 -- identifier. The reason for killing it in the instance case is
8863 -- that it is common and reasonable for code to be deleted in
8864 -- instances for various reasons.
8866 -- Could we use Is_Statically_Unevaluated here???
8868 if Nkind (Parent (N)) = N_If_Statement then
8869 declare
8870 C : constant Node_Id := Condition (Parent (N));
8871 begin
8872 if Nkind (C) = N_Identifier
8873 and then
8874 (In_Instance
8875 or else (Present (Entity (C))
8876 and then Has_Warnings_Off (Entity (C))))
8877 then
8878 W := False;
8879 end if;
8880 end;
8881 end if;
8883 -- Generate warning if not suppressed
8885 if W then
8886 Error_Msg_F
8887 ("?t?this code can never be executed and has been deleted!",
8889 end if;
8890 end if;
8892 -- Recurse into block statements and bodies to process declarations
8893 -- and statements.
8895 if Nkind (N) = N_Block_Statement
8896 or else Nkind (N) = N_Subprogram_Body
8897 or else Nkind (N) = N_Package_Body
8898 then
8899 Kill_Dead_Code (Declarations (N), False);
8900 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8902 if Nkind (N) = N_Subprogram_Body then
8903 Set_Is_Eliminated (Defining_Entity (N));
8904 end if;
8906 elsif Nkind (N) = N_Package_Declaration then
8907 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8908 Kill_Dead_Code (Private_Declarations (Specification (N)));
8910 -- ??? After this point, Delete_Tree has been called on all
8911 -- declarations in Specification (N), so references to entities
8912 -- therein look suspicious.
8914 declare
8915 E : Entity_Id := First_Entity (Defining_Entity (N));
8917 begin
8918 while Present (E) loop
8919 if Ekind (E) = E_Operator then
8920 Set_Is_Eliminated (E);
8921 end if;
8923 Next_Entity (E);
8924 end loop;
8925 end;
8927 -- Recurse into composite statement to kill individual statements in
8928 -- particular instantiations.
8930 elsif Nkind (N) = N_If_Statement then
8931 Kill_Dead_Code (Then_Statements (N));
8932 Kill_Dead_Code (Elsif_Parts (N));
8933 Kill_Dead_Code (Else_Statements (N));
8935 elsif Nkind (N) = N_Loop_Statement then
8936 Kill_Dead_Code (Statements (N));
8938 elsif Nkind (N) = N_Case_Statement then
8939 declare
8940 Alt : Node_Id;
8941 begin
8942 Alt := First (Alternatives (N));
8943 while Present (Alt) loop
8944 Kill_Dead_Code (Statements (Alt));
8945 Next (Alt);
8946 end loop;
8947 end;
8949 elsif Nkind (N) = N_Case_Statement_Alternative then
8950 Kill_Dead_Code (Statements (N));
8952 -- Deal with dead instances caused by deleting instantiations
8954 elsif Nkind (N) in N_Generic_Instantiation then
8955 Remove_Dead_Instance (N);
8956 end if;
8957 end if;
8958 end Kill_Dead_Code;
8960 -- Case where argument is a list of nodes to be killed
8962 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8963 N : Node_Id;
8964 W : Boolean;
8966 begin
8967 W := Warn;
8969 if Is_Non_Empty_List (L) then
8970 N := First (L);
8971 while Present (N) loop
8972 Kill_Dead_Code (N, W);
8973 W := False;
8974 Next (N);
8975 end loop;
8976 end if;
8977 end Kill_Dead_Code;
8979 ------------------------
8980 -- Known_Non_Negative --
8981 ------------------------
8983 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8984 begin
8985 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8986 return True;
8988 else
8989 declare
8990 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8991 begin
8992 return
8993 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8994 end;
8995 end if;
8996 end Known_Non_Negative;
8998 -----------------------------
8999 -- Make_CW_Equivalent_Type --
9000 -----------------------------
9002 -- Create a record type used as an equivalent of any member of the class
9003 -- which takes its size from exp.
9005 -- Generate the following code:
9007 -- type Equiv_T is record
9008 -- _parent : T (List of discriminant constraints taken from Exp);
9009 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
9010 -- end Equiv_T;
9012 -- ??? Note that this type does not guarantee same alignment as all
9013 -- derived types
9015 function Make_CW_Equivalent_Type
9016 (T : Entity_Id;
9017 E : Node_Id) return Entity_Id
9019 Loc : constant Source_Ptr := Sloc (E);
9020 Root_Typ : constant Entity_Id := Root_Type (T);
9021 List_Def : constant List_Id := Empty_List;
9022 Comp_List : constant List_Id := New_List;
9023 Equiv_Type : Entity_Id;
9024 Range_Type : Entity_Id;
9025 Str_Type : Entity_Id;
9026 Constr_Root : Entity_Id;
9027 Sizexpr : Node_Id;
9029 begin
9030 -- If the root type is already constrained, there are no discriminants
9031 -- in the expression.
9033 if not Has_Discriminants (Root_Typ)
9034 or else Is_Constrained (Root_Typ)
9035 then
9036 Constr_Root := Root_Typ;
9038 -- At this point in the expansion, non-limited view of the type
9039 -- must be available, otherwise the error will be reported later.
9041 if From_Limited_With (Constr_Root)
9042 and then Present (Non_Limited_View (Constr_Root))
9043 then
9044 Constr_Root := Non_Limited_View (Constr_Root);
9045 end if;
9047 else
9048 Constr_Root := Make_Temporary (Loc, 'R');
9050 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9052 Append_To (List_Def,
9053 Make_Subtype_Declaration (Loc,
9054 Defining_Identifier => Constr_Root,
9055 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9056 end if;
9058 -- Generate the range subtype declaration
9060 Range_Type := Make_Temporary (Loc, 'G');
9062 if not Is_Interface (Root_Typ) then
9064 -- subtype rg__xx is
9065 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9067 Sizexpr :=
9068 Make_Op_Subtract (Loc,
9069 Left_Opnd =>
9070 Make_Attribute_Reference (Loc,
9071 Prefix =>
9072 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9073 Attribute_Name => Name_Size),
9074 Right_Opnd =>
9075 Make_Attribute_Reference (Loc,
9076 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9077 Attribute_Name => Name_Object_Size));
9078 else
9079 -- subtype rg__xx is
9080 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9082 Sizexpr :=
9083 Make_Attribute_Reference (Loc,
9084 Prefix =>
9085 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9086 Attribute_Name => Name_Size);
9087 end if;
9089 Set_Paren_Count (Sizexpr, 1);
9091 Append_To (List_Def,
9092 Make_Subtype_Declaration (Loc,
9093 Defining_Identifier => Range_Type,
9094 Subtype_Indication =>
9095 Make_Subtype_Indication (Loc,
9096 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9097 Constraint => Make_Range_Constraint (Loc,
9098 Range_Expression =>
9099 Make_Range (Loc,
9100 Low_Bound => Make_Integer_Literal (Loc, 1),
9101 High_Bound =>
9102 Make_Op_Divide (Loc,
9103 Left_Opnd => Sizexpr,
9104 Right_Opnd => Make_Integer_Literal (Loc,
9105 Intval => System_Storage_Unit)))))));
9107 -- subtype str__nn is Storage_Array (rg__x);
9109 Str_Type := Make_Temporary (Loc, 'S');
9110 Append_To (List_Def,
9111 Make_Subtype_Declaration (Loc,
9112 Defining_Identifier => Str_Type,
9113 Subtype_Indication =>
9114 Make_Subtype_Indication (Loc,
9115 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9116 Constraint =>
9117 Make_Index_Or_Discriminant_Constraint (Loc,
9118 Constraints =>
9119 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9121 -- type Equiv_T is record
9122 -- [ _parent : Tnn; ]
9123 -- E : Str_Type;
9124 -- end Equiv_T;
9126 Equiv_Type := Make_Temporary (Loc, 'T');
9127 Set_Ekind (Equiv_Type, E_Record_Type);
9128 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9130 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9131 -- treatment for this type. In particular, even though _parent's type
9132 -- is a controlled type or contains controlled components, we do not
9133 -- want to set Has_Controlled_Component on it to avoid making it gain
9134 -- an unwanted _controller component.
9136 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9138 -- A class-wide equivalent type does not require initialization
9140 Set_Suppress_Initialization (Equiv_Type);
9142 if not Is_Interface (Root_Typ) then
9143 Append_To (Comp_List,
9144 Make_Component_Declaration (Loc,
9145 Defining_Identifier =>
9146 Make_Defining_Identifier (Loc, Name_uParent),
9147 Component_Definition =>
9148 Make_Component_Definition (Loc,
9149 Aliased_Present => False,
9150 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9151 end if;
9153 Append_To (Comp_List,
9154 Make_Component_Declaration (Loc,
9155 Defining_Identifier => Make_Temporary (Loc, 'C'),
9156 Component_Definition =>
9157 Make_Component_Definition (Loc,
9158 Aliased_Present => False,
9159 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9161 Append_To (List_Def,
9162 Make_Full_Type_Declaration (Loc,
9163 Defining_Identifier => Equiv_Type,
9164 Type_Definition =>
9165 Make_Record_Definition (Loc,
9166 Component_List =>
9167 Make_Component_List (Loc,
9168 Component_Items => Comp_List,
9169 Variant_Part => Empty))));
9171 -- Suppress all checks during the analysis of the expanded code to avoid
9172 -- the generation of spurious warnings under ZFP run-time.
9174 Insert_Actions (E, List_Def, Suppress => All_Checks);
9175 return Equiv_Type;
9176 end Make_CW_Equivalent_Type;
9178 -------------------------
9179 -- Make_Invariant_Call --
9180 -------------------------
9182 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9183 Loc : constant Source_Ptr := Sloc (Expr);
9184 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9186 Proc_Id : Entity_Id;
9188 begin
9189 pragma Assert (Has_Invariants (Typ));
9191 Proc_Id := Invariant_Procedure (Typ);
9192 pragma Assert (Present (Proc_Id));
9194 return
9195 Make_Procedure_Call_Statement (Loc,
9196 Name => New_Occurrence_Of (Proc_Id, Loc),
9197 Parameter_Associations => New_List (Relocate_Node (Expr)));
9198 end Make_Invariant_Call;
9200 ------------------------
9201 -- Make_Literal_Range --
9202 ------------------------
9204 function Make_Literal_Range
9205 (Loc : Source_Ptr;
9206 Literal_Typ : Entity_Id) return Node_Id
9208 Lo : constant Node_Id :=
9209 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9210 Index : constant Entity_Id := Etype (Lo);
9211 Length_Expr : constant Node_Id :=
9212 Make_Op_Subtract (Loc,
9213 Left_Opnd =>
9214 Make_Integer_Literal (Loc,
9215 Intval => String_Literal_Length (Literal_Typ)),
9216 Right_Opnd => Make_Integer_Literal (Loc, 1));
9218 Hi : Node_Id;
9220 begin
9221 Set_Analyzed (Lo, False);
9223 if Is_Integer_Type (Index) then
9224 Hi :=
9225 Make_Op_Add (Loc,
9226 Left_Opnd => New_Copy_Tree (Lo),
9227 Right_Opnd => Length_Expr);
9228 else
9229 Hi :=
9230 Make_Attribute_Reference (Loc,
9231 Attribute_Name => Name_Val,
9232 Prefix => New_Occurrence_Of (Index, Loc),
9233 Expressions => New_List (
9234 Make_Op_Add (Loc,
9235 Left_Opnd =>
9236 Make_Attribute_Reference (Loc,
9237 Attribute_Name => Name_Pos,
9238 Prefix => New_Occurrence_Of (Index, Loc),
9239 Expressions => New_List (New_Copy_Tree (Lo))),
9240 Right_Opnd => Length_Expr)));
9241 end if;
9243 return
9244 Make_Range (Loc,
9245 Low_Bound => Lo,
9246 High_Bound => Hi);
9247 end Make_Literal_Range;
9249 --------------------------
9250 -- Make_Non_Empty_Check --
9251 --------------------------
9253 function Make_Non_Empty_Check
9254 (Loc : Source_Ptr;
9255 N : Node_Id) return Node_Id
9257 begin
9258 return
9259 Make_Op_Ne (Loc,
9260 Left_Opnd =>
9261 Make_Attribute_Reference (Loc,
9262 Attribute_Name => Name_Length,
9263 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9264 Right_Opnd =>
9265 Make_Integer_Literal (Loc, 0));
9266 end Make_Non_Empty_Check;
9268 -------------------------
9269 -- Make_Predicate_Call --
9270 -------------------------
9272 -- WARNING: This routine manages Ghost regions. Return statements must be
9273 -- replaced by gotos which jump to the end of the routine and restore the
9274 -- Ghost mode.
9276 function Make_Predicate_Call
9277 (Typ : Entity_Id;
9278 Expr : Node_Id;
9279 Mem : Boolean := False) return Node_Id
9281 Loc : constant Source_Ptr := Sloc (Expr);
9283 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9284 -- Save the Ghost mode to restore on exit
9286 Call : Node_Id;
9287 Func_Id : Entity_Id;
9289 begin
9290 pragma Assert (Present (Predicate_Function (Typ)));
9292 -- The related type may be subject to pragma Ghost. Set the mode now to
9293 -- ensure that the call is properly marked as Ghost.
9295 Set_Ghost_Mode (Typ);
9297 -- Call special membership version if requested and available
9299 if Mem and then Present (Predicate_Function_M (Typ)) then
9300 Func_Id := Predicate_Function_M (Typ);
9301 else
9302 Func_Id := Predicate_Function (Typ);
9303 end if;
9305 -- Case of calling normal predicate function
9307 -- If the type is tagged, the expression may be class-wide, in which
9308 -- case it has to be converted to its root type, given that the
9309 -- generated predicate function is not dispatching.
9311 if Is_Tagged_Type (Typ) then
9312 Call :=
9313 Make_Function_Call (Loc,
9314 Name => New_Occurrence_Of (Func_Id, Loc),
9315 Parameter_Associations =>
9316 New_List (Convert_To (Typ, Relocate_Node (Expr))));
9317 else
9318 Call :=
9319 Make_Function_Call (Loc,
9320 Name => New_Occurrence_Of (Func_Id, Loc),
9321 Parameter_Associations => New_List (Relocate_Node (Expr)));
9322 end if;
9324 Restore_Ghost_Mode (Saved_GM);
9326 return Call;
9327 end Make_Predicate_Call;
9329 --------------------------
9330 -- Make_Predicate_Check --
9331 --------------------------
9333 function Make_Predicate_Check
9334 (Typ : Entity_Id;
9335 Expr : Node_Id) return Node_Id
9337 procedure Replace_Subtype_Reference (N : Node_Id);
9338 -- Replace current occurrences of the subtype to which a dynamic
9339 -- predicate applies, by the expression that triggers a predicate
9340 -- check. This is needed for aspect Predicate_Failure, for which
9341 -- we do not generate a wrapper procedure, but simply modify the
9342 -- expression for the pragma of the predicate check.
9344 --------------------------------
9345 -- Replace_Subtype_Reference --
9346 --------------------------------
9348 procedure Replace_Subtype_Reference (N : Node_Id) is
9349 begin
9350 Rewrite (N, New_Copy_Tree (Expr));
9352 -- We want to treat the node as if it comes from source, so
9353 -- that ASIS will not ignore it.
9355 Set_Comes_From_Source (N, True);
9356 end Replace_Subtype_Reference;
9358 procedure Replace_Subtype_References is
9359 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9361 -- Local variables
9363 Loc : constant Source_Ptr := Sloc (Expr);
9364 Arg_List : List_Id;
9365 Fail_Expr : Node_Id;
9366 Nam : Name_Id;
9368 -- Start of processing for Make_Predicate_Check
9370 begin
9371 -- If predicate checks are suppressed, then return a null statement. For
9372 -- this call, we check only the scope setting. If the caller wants to
9373 -- check a specific entity's setting, they must do it manually.
9375 if Predicate_Checks_Suppressed (Empty) then
9376 return Make_Null_Statement (Loc);
9377 end if;
9379 -- Do not generate a check within an internal subprogram (stream
9380 -- functions and the like, including including predicate functions).
9382 if Within_Internal_Subprogram then
9383 return Make_Null_Statement (Loc);
9384 end if;
9386 -- Compute proper name to use, we need to get this right so that the
9387 -- right set of check policies apply to the Check pragma we are making.
9389 if Has_Dynamic_Predicate_Aspect (Typ) then
9390 Nam := Name_Dynamic_Predicate;
9391 elsif Has_Static_Predicate_Aspect (Typ) then
9392 Nam := Name_Static_Predicate;
9393 else
9394 Nam := Name_Predicate;
9395 end if;
9397 Arg_List := New_List (
9398 Make_Pragma_Argument_Association (Loc,
9399 Expression => Make_Identifier (Loc, Nam)),
9400 Make_Pragma_Argument_Association (Loc,
9401 Expression => Make_Predicate_Call (Typ, Expr)));
9403 -- If subtype has Predicate_Failure defined, add the correponding
9404 -- expression as an additional pragma parameter, after replacing
9405 -- current instances with the expression being checked.
9407 if Has_Aspect (Typ, Aspect_Predicate_Failure) then
9408 Fail_Expr :=
9409 New_Copy_Tree
9410 (Expression (Find_Aspect (Typ, Aspect_Predicate_Failure)));
9411 Replace_Subtype_References (Fail_Expr, Typ);
9413 Append_To (Arg_List,
9414 Make_Pragma_Argument_Association (Loc,
9415 Expression => Fail_Expr));
9416 end if;
9418 return
9419 Make_Pragma (Loc,
9420 Chars => Name_Check,
9421 Pragma_Argument_Associations => Arg_List);
9422 end Make_Predicate_Check;
9424 ----------------------------
9425 -- Make_Subtype_From_Expr --
9426 ----------------------------
9428 -- 1. If Expr is an unconstrained array expression, creates
9429 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9431 -- 2. If Expr is a unconstrained discriminated type expression, creates
9432 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9434 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9436 function Make_Subtype_From_Expr
9437 (E : Node_Id;
9438 Unc_Typ : Entity_Id;
9439 Related_Id : Entity_Id := Empty) return Node_Id
9441 List_Constr : constant List_Id := New_List;
9442 Loc : constant Source_Ptr := Sloc (E);
9443 D : Entity_Id;
9444 Full_Exp : Node_Id;
9445 Full_Subtyp : Entity_Id;
9446 High_Bound : Entity_Id;
9447 Index_Typ : Entity_Id;
9448 Low_Bound : Entity_Id;
9449 Priv_Subtyp : Entity_Id;
9450 Utyp : Entity_Id;
9452 begin
9453 if Is_Private_Type (Unc_Typ)
9454 and then Has_Unknown_Discriminants (Unc_Typ)
9455 then
9456 -- The caller requests a unique external name for both the private
9457 -- and the full subtype.
9459 if Present (Related_Id) then
9460 Full_Subtyp :=
9461 Make_Defining_Identifier (Loc,
9462 Chars => New_External_Name (Chars (Related_Id), 'C'));
9463 Priv_Subtyp :=
9464 Make_Defining_Identifier (Loc,
9465 Chars => New_External_Name (Chars (Related_Id), 'P'));
9467 else
9468 Full_Subtyp := Make_Temporary (Loc, 'C');
9469 Priv_Subtyp := Make_Temporary (Loc, 'P');
9470 end if;
9472 -- Prepare the subtype completion. Use the base type to find the
9473 -- underlying type because the type may be a generic actual or an
9474 -- explicit subtype.
9476 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9478 Full_Exp :=
9479 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9480 Set_Parent (Full_Exp, Parent (E));
9482 Insert_Action (E,
9483 Make_Subtype_Declaration (Loc,
9484 Defining_Identifier => Full_Subtyp,
9485 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9487 -- Define the dummy private subtype
9489 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9490 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9491 Set_Scope (Priv_Subtyp, Full_Subtyp);
9492 Set_Is_Constrained (Priv_Subtyp);
9493 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9494 Set_Is_Itype (Priv_Subtyp);
9495 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9497 if Is_Tagged_Type (Priv_Subtyp) then
9498 Set_Class_Wide_Type
9499 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9500 Set_Direct_Primitive_Operations (Priv_Subtyp,
9501 Direct_Primitive_Operations (Unc_Typ));
9502 end if;
9504 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9506 return New_Occurrence_Of (Priv_Subtyp, Loc);
9508 elsif Is_Array_Type (Unc_Typ) then
9509 Index_Typ := First_Index (Unc_Typ);
9510 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9512 -- Capture the bounds of each index constraint in case the context
9513 -- is an object declaration of an unconstrained type initialized
9514 -- by a function call:
9516 -- Obj : Unconstr_Typ := Func_Call;
9518 -- This scenario requires secondary scope management and the index
9519 -- constraint cannot depend on the temporary used to capture the
9520 -- result of the function call.
9522 -- SS_Mark;
9523 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9524 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9525 -- Obj : S := Temp.all;
9526 -- SS_Release; -- Temp is gone at this point, bounds of S are
9527 -- -- non existent.
9529 -- Generate:
9530 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9532 Low_Bound := Make_Temporary (Loc, 'B');
9533 Insert_Action (E,
9534 Make_Object_Declaration (Loc,
9535 Defining_Identifier => Low_Bound,
9536 Object_Definition =>
9537 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9538 Constant_Present => True,
9539 Expression =>
9540 Make_Attribute_Reference (Loc,
9541 Prefix => Duplicate_Subexpr_No_Checks (E),
9542 Attribute_Name => Name_First,
9543 Expressions => New_List (
9544 Make_Integer_Literal (Loc, J)))));
9546 -- Generate:
9547 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9549 High_Bound := Make_Temporary (Loc, 'B');
9550 Insert_Action (E,
9551 Make_Object_Declaration (Loc,
9552 Defining_Identifier => High_Bound,
9553 Object_Definition =>
9554 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9555 Constant_Present => True,
9556 Expression =>
9557 Make_Attribute_Reference (Loc,
9558 Prefix => Duplicate_Subexpr_No_Checks (E),
9559 Attribute_Name => Name_Last,
9560 Expressions => New_List (
9561 Make_Integer_Literal (Loc, J)))));
9563 Append_To (List_Constr,
9564 Make_Range (Loc,
9565 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9566 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9568 Index_Typ := Next_Index (Index_Typ);
9569 end loop;
9571 elsif Is_Class_Wide_Type (Unc_Typ) then
9572 declare
9573 CW_Subtype : Entity_Id;
9574 EQ_Typ : Entity_Id := Empty;
9576 begin
9577 -- A class-wide equivalent type is not needed on VM targets
9578 -- because the VM back-ends handle the class-wide object
9579 -- initialization itself (and doesn't need or want the
9580 -- additional intermediate type to handle the assignment).
9582 if Expander_Active and then Tagged_Type_Expansion then
9584 -- If this is the class-wide type of a completion that is a
9585 -- record subtype, set the type of the class-wide type to be
9586 -- the full base type, for use in the expanded code for the
9587 -- equivalent type. Should this be done earlier when the
9588 -- completion is analyzed ???
9590 if Is_Private_Type (Etype (Unc_Typ))
9591 and then
9592 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9593 then
9594 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9595 end if;
9597 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9598 end if;
9600 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9601 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9602 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9604 return New_Occurrence_Of (CW_Subtype, Loc);
9605 end;
9607 -- Indefinite record type with discriminants
9609 else
9610 D := First_Discriminant (Unc_Typ);
9611 while Present (D) loop
9612 Append_To (List_Constr,
9613 Make_Selected_Component (Loc,
9614 Prefix => Duplicate_Subexpr_No_Checks (E),
9615 Selector_Name => New_Occurrence_Of (D, Loc)));
9617 Next_Discriminant (D);
9618 end loop;
9619 end if;
9621 return
9622 Make_Subtype_Indication (Loc,
9623 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9624 Constraint =>
9625 Make_Index_Or_Discriminant_Constraint (Loc,
9626 Constraints => List_Constr));
9627 end Make_Subtype_From_Expr;
9629 ---------------
9630 -- Map_Types --
9631 ---------------
9633 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9635 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9636 -- avoid deep indentation of code.
9638 -- NOTE: Routines which deal with discriminant mapping operate on the
9639 -- [underlying/record] full view of various types because those views
9640 -- contain all discriminants and stored constraints.
9642 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9643 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9644 -- overriding chain starting from Prim whose dispatching type is parent
9645 -- type Par_Typ and add a mapping between the result and primitive Prim.
9647 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9648 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9649 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9650 -- if no such primitive is available.
9652 function Build_Chain
9653 (Par_Typ : Entity_Id;
9654 Deriv_Typ : Entity_Id) return Elist_Id;
9655 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9656 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9657 -- list has the form:
9659 -- head tail
9660 -- v v
9661 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9663 -- Note that Par_Typ is not part of the resulting derivation chain
9665 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9666 -- Return the view of type Typ which could potentially contains either
9667 -- the discriminants or stored constraints of the type.
9669 function Find_Discriminant_Value
9670 (Discr : Entity_Id;
9671 Par_Typ : Entity_Id;
9672 Deriv_Typ : Entity_Id;
9673 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9674 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9675 -- in the derivation chain starting from parent type Par_Typ leading to
9676 -- derived type Deriv_Typ. The returned value is one of the following:
9678 -- * An entity which is either a discriminant or a non-discriminant
9679 -- name, and renames/constraints Discr.
9681 -- * An expression which constraints Discr
9683 -- Typ_Elmt is an element of the derivation chain created by routine
9684 -- Build_Chain and denotes the current ancestor being examined.
9686 procedure Map_Discriminants
9687 (Par_Typ : Entity_Id;
9688 Deriv_Typ : Entity_Id);
9689 -- Map each discriminant of type Par_Typ to a meaningful constraint
9690 -- from the point of view of type Deriv_Typ.
9692 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9693 -- Map each primitive of type Par_Typ to a corresponding primitive of
9694 -- type Deriv_Typ.
9696 -------------------
9697 -- Add_Primitive --
9698 -------------------
9700 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9701 Par_Prim : Entity_Id;
9703 begin
9704 -- Inspect the inheritance chain through the Alias attribute and the
9705 -- overriding chain through the Overridden_Operation looking for an
9706 -- ancestor primitive with the appropriate dispatching type.
9708 Par_Prim := Prim;
9709 while Present (Par_Prim) loop
9710 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9711 Par_Prim := Ancestor_Primitive (Par_Prim);
9712 end loop;
9714 -- Create a mapping of the form:
9716 -- parent type primitive -> derived type primitive
9718 if Present (Par_Prim) then
9719 Type_Map.Set (Par_Prim, Prim);
9720 end if;
9721 end Add_Primitive;
9723 ------------------------
9724 -- Ancestor_Primitive --
9725 ------------------------
9727 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9728 Inher_Prim : constant Entity_Id := Alias (Subp);
9729 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9731 begin
9732 -- The current subprogram overrides an ancestor primitive
9734 if Present (Over_Prim) then
9735 return Over_Prim;
9737 -- The current subprogram is an internally generated alias of an
9738 -- inherited ancestor primitive.
9740 elsif Present (Inher_Prim) then
9741 return Inher_Prim;
9743 -- Otherwise the current subprogram is the root of the inheritance or
9744 -- overriding chain.
9746 else
9747 return Empty;
9748 end if;
9749 end Ancestor_Primitive;
9751 -----------------
9752 -- Build_Chain --
9753 -----------------
9755 function Build_Chain
9756 (Par_Typ : Entity_Id;
9757 Deriv_Typ : Entity_Id) return Elist_Id
9759 Anc_Typ : Entity_Id;
9760 Chain : Elist_Id;
9761 Curr_Typ : Entity_Id;
9763 begin
9764 Chain := New_Elmt_List;
9766 -- Add the derived type to the derivation chain
9768 Prepend_Elmt (Deriv_Typ, Chain);
9770 -- Examine all ancestors starting from the derived type climbing
9771 -- towards parent type Par_Typ.
9773 Curr_Typ := Deriv_Typ;
9774 loop
9775 -- Handle the case where the current type is a record which
9776 -- derives from a subtype.
9778 -- subtype Sub_Typ is Par_Typ ...
9779 -- type Deriv_Typ is Sub_Typ ...
9781 if Ekind (Curr_Typ) = E_Record_Type
9782 and then Present (Parent_Subtype (Curr_Typ))
9783 then
9784 Anc_Typ := Parent_Subtype (Curr_Typ);
9786 -- Handle the case where the current type is a record subtype of
9787 -- another subtype.
9789 -- subtype Sub_Typ1 is Par_Typ ...
9790 -- subtype Sub_Typ2 is Sub_Typ1 ...
9792 elsif Ekind (Curr_Typ) = E_Record_Subtype
9793 and then Present (Cloned_Subtype (Curr_Typ))
9794 then
9795 Anc_Typ := Cloned_Subtype (Curr_Typ);
9797 -- Otherwise use the direct parent type
9799 else
9800 Anc_Typ := Etype (Curr_Typ);
9801 end if;
9803 -- Use the first subtype when dealing with itypes
9805 if Is_Itype (Anc_Typ) then
9806 Anc_Typ := First_Subtype (Anc_Typ);
9807 end if;
9809 -- Work with the view which contains the discriminants and stored
9810 -- constraints.
9812 Anc_Typ := Discriminated_View (Anc_Typ);
9814 -- Stop the climb when either the parent type has been reached or
9815 -- there are no more ancestors left to examine.
9817 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9819 Prepend_Unique_Elmt (Anc_Typ, Chain);
9820 Curr_Typ := Anc_Typ;
9821 end loop;
9823 return Chain;
9824 end Build_Chain;
9826 ------------------------
9827 -- Discriminated_View --
9828 ------------------------
9830 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9831 T : Entity_Id;
9833 begin
9834 T := Typ;
9836 -- Use the [underlying] full view when dealing with private types
9837 -- because the view contains all inherited discriminants or stored
9838 -- constraints.
9840 if Is_Private_Type (T) then
9841 if Present (Underlying_Full_View (T)) then
9842 T := Underlying_Full_View (T);
9844 elsif Present (Full_View (T)) then
9845 T := Full_View (T);
9846 end if;
9847 end if;
9849 -- Use the underlying record view when the type is an extenstion of
9850 -- a parent type with unknown discriminants because the view contains
9851 -- all inherited discriminants or stored constraints.
9853 if Ekind (T) = E_Record_Type
9854 and then Present (Underlying_Record_View (T))
9855 then
9856 T := Underlying_Record_View (T);
9857 end if;
9859 return T;
9860 end Discriminated_View;
9862 -----------------------------
9863 -- Find_Discriminant_Value --
9864 -----------------------------
9866 function Find_Discriminant_Value
9867 (Discr : Entity_Id;
9868 Par_Typ : Entity_Id;
9869 Deriv_Typ : Entity_Id;
9870 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9872 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9873 Typ : constant Entity_Id := Node (Typ_Elmt);
9875 function Find_Constraint_Value
9876 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
9877 -- Given constraint Constr, find what it denotes. This is either:
9879 -- * An entity which is either a discriminant or a name
9881 -- * An expression
9883 ---------------------------
9884 -- Find_Constraint_Value --
9885 ---------------------------
9887 function Find_Constraint_Value
9888 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
9890 begin
9891 if Nkind (Constr) in N_Entity then
9893 -- The constraint denotes a discriminant of the curren type
9894 -- which renames the ancestor discriminant:
9896 -- vv
9897 -- type Typ (D1 : ...; DN : ...) is
9898 -- new Anc (Discr => D1) with ...
9899 -- ^^
9901 if Ekind (Constr) = E_Discriminant then
9903 -- The discriminant belongs to derived type Deriv_Typ. This
9904 -- is the final value for the ancestor discriminant as the
9905 -- derivations chain has been fully exhausted.
9907 if Typ = Deriv_Typ then
9908 return Constr;
9910 -- Otherwise the discriminant may be renamed or constrained
9911 -- at a lower level. Continue looking down the derivation
9912 -- chain.
9914 else
9915 return
9916 Find_Discriminant_Value
9917 (Discr => Constr,
9918 Par_Typ => Par_Typ,
9919 Deriv_Typ => Deriv_Typ,
9920 Typ_Elmt => Next_Elmt (Typ_Elmt));
9921 end if;
9923 -- Otherwise the constraint denotes a reference to some name
9924 -- which results in a Girder discriminant:
9926 -- vvvv
9927 -- Name : ...;
9928 -- type Typ (D1 : ...; DN : ...) is
9929 -- new Anc (Discr => Name) with ...
9930 -- ^^^^
9932 -- Return the name as this is the proper constraint of the
9933 -- discriminant.
9935 else
9936 return Constr;
9937 end if;
9939 -- The constraint denotes a reference to a name
9941 elsif Is_Entity_Name (Constr) then
9942 return Find_Constraint_Value (Entity (Constr));
9944 -- Otherwise the current constraint is an expression which yields
9945 -- a Girder discriminant:
9947 -- type Typ (D1 : ...; DN : ...) is
9948 -- new Anc (Discr => <expression>) with ...
9949 -- ^^^^^^^^^^
9951 -- Return the expression as this is the proper constraint of the
9952 -- discriminant.
9954 else
9955 return Constr;
9956 end if;
9957 end Find_Constraint_Value;
9959 -- Local variables
9961 Constrs : constant Elist_Id := Stored_Constraint (Typ);
9963 Constr_Elmt : Elmt_Id;
9964 Pos : Uint;
9965 Typ_Discr : Entity_Id;
9967 -- Start of processing for Find_Discriminant_Value
9969 begin
9970 -- The algorithm for finding the value of a discriminant works as
9971 -- follows. First, it recreates the derivation chain from Par_Typ
9972 -- to Deriv_Typ as a list:
9974 -- Par_Typ (shown for completeness)
9975 -- v
9976 -- Ancestor_N <-- head of chain
9977 -- v
9978 -- Ancestor_1
9979 -- v
9980 -- Deriv_Typ <-- tail of chain
9982 -- The algorithm then traces the fate of a parent discriminant down
9983 -- the derivation chain. At each derivation level, the discriminant
9984 -- may be either inherited or constrained.
9986 -- 1) Discriminant is inherited: there are two cases, depending on
9987 -- which type is inheriting.
9989 -- 1.1) Deriv_Typ is inheriting:
9991 -- type Ancestor (D_1 : ...) is tagged ...
9992 -- type Deriv_Typ is new Ancestor ...
9994 -- In this case the inherited discriminant is the final value of
9995 -- the parent discriminant because the end of the derivation chain
9996 -- has been reached.
9998 -- 1.2) Some other type is inheriting:
10000 -- type Ancestor_1 (D_1 : ...) is tagged ...
10001 -- type Ancestor_2 is new Ancestor_1 ...
10003 -- In this case the algorithm continues to trace the fate of the
10004 -- inherited discriminant down the derivation chain because it may
10005 -- be further inherited or constrained.
10007 -- 2) Discriminant is constrained: there are three cases, depending
10008 -- on what the constraint is.
10010 -- 2.1) The constraint is another discriminant (aka renaming):
10012 -- type Ancestor_1 (D_1 : ...) is tagged ...
10013 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10015 -- In this case the constraining discriminant becomes the one to
10016 -- track down the derivation chain. The algorithm already knows
10017 -- that D_2 constrains D_1, therefore if the algorithm finds the
10018 -- value of D_2, then this would also be the value for D_1.
10020 -- 2.2) The constraint is a name (aka Girder):
10022 -- Name : ...
10023 -- type Ancestor_1 (D_1 : ...) is tagged ...
10024 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10026 -- In this case the name is the final value of D_1 because the
10027 -- discriminant cannot be further constrained.
10029 -- 2.3) The constraint is an expression (aka Girder):
10031 -- type Ancestor_1 (D_1 : ...) is tagged ...
10032 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10034 -- Similar to 2.2, the expression is the final value of D_1
10036 Pos := Uint_1;
10038 -- When a derived type constrains its parent type, all constaints
10039 -- appear in the Stored_Constraint list. Examine the list looking
10040 -- for a positional match.
10042 if Present (Constrs) then
10043 Constr_Elmt := First_Elmt (Constrs);
10044 while Present (Constr_Elmt) loop
10046 -- The position of the current constraint matches that of the
10047 -- ancestor discriminant.
10049 if Pos = Discr_Pos then
10050 return Find_Constraint_Value (Node (Constr_Elmt));
10051 end if;
10053 Next_Elmt (Constr_Elmt);
10054 Pos := Pos + 1;
10055 end loop;
10057 -- Otherwise the derived type does not constraint its parent type in
10058 -- which case it inherits the parent discriminants.
10060 else
10061 Typ_Discr := First_Discriminant (Typ);
10062 while Present (Typ_Discr) loop
10064 -- The position of the current discriminant matches that of the
10065 -- ancestor discriminant.
10067 if Pos = Discr_Pos then
10068 return Find_Constraint_Value (Typ_Discr);
10069 end if;
10071 Next_Discriminant (Typ_Discr);
10072 Pos := Pos + 1;
10073 end loop;
10074 end if;
10076 -- A discriminant must always have a corresponding value. This is
10077 -- either another discriminant, a name, or an expression. If this
10078 -- point is reached, them most likely the derivation chain employs
10079 -- the wrong views of types.
10081 pragma Assert (False);
10083 return Empty;
10084 end Find_Discriminant_Value;
10086 -----------------------
10087 -- Map_Discriminants --
10088 -----------------------
10090 procedure Map_Discriminants
10091 (Par_Typ : Entity_Id;
10092 Deriv_Typ : Entity_Id)
10094 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10096 Discr : Entity_Id;
10097 Discr_Val : Node_Or_Entity_Id;
10099 begin
10100 -- Examine each discriminant of parent type Par_Typ and find a
10101 -- suitable value for it from the point of view of derived type
10102 -- Deriv_Typ.
10104 if Has_Discriminants (Par_Typ) then
10105 Discr := First_Discriminant (Par_Typ);
10106 while Present (Discr) loop
10107 Discr_Val :=
10108 Find_Discriminant_Value
10109 (Discr => Discr,
10110 Par_Typ => Par_Typ,
10111 Deriv_Typ => Deriv_Typ,
10112 Typ_Elmt => First_Elmt (Deriv_Chain));
10114 -- Create a mapping of the form:
10116 -- parent type discriminant -> value
10118 Type_Map.Set (Discr, Discr_Val);
10120 Next_Discriminant (Discr);
10121 end loop;
10122 end if;
10123 end Map_Discriminants;
10125 --------------------
10126 -- Map_Primitives --
10127 --------------------
10129 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10130 Deriv_Prim : Entity_Id;
10131 Par_Prim : Entity_Id;
10132 Par_Prims : Elist_Id;
10133 Prim_Elmt : Elmt_Id;
10135 begin
10136 -- Inspect the primitives of the derived type and determine whether
10137 -- they relate to the primitives of the parent type. If there is a
10138 -- meaningful relation, create a mapping of the form:
10140 -- parent type primitive -> perived type primitive
10142 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10143 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10144 while Present (Prim_Elmt) loop
10145 Deriv_Prim := Node (Prim_Elmt);
10147 if Is_Subprogram (Deriv_Prim)
10148 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10149 then
10150 Add_Primitive (Deriv_Prim, Par_Typ);
10151 end if;
10153 Next_Elmt (Prim_Elmt);
10154 end loop;
10155 end if;
10157 -- If the parent operation is an interface operation, the overriding
10158 -- indicator is not present. Instead, we get from the interface
10159 -- operation the primitive of the current type that implements it.
10161 if Is_Interface (Par_Typ) then
10162 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10164 if Present (Par_Prims) then
10165 Prim_Elmt := First_Elmt (Par_Prims);
10167 while Present (Prim_Elmt) loop
10168 Par_Prim := Node (Prim_Elmt);
10169 Deriv_Prim :=
10170 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10172 if Present (Deriv_Prim) then
10173 Type_Map.Set (Par_Prim, Deriv_Prim);
10174 end if;
10176 Next_Elmt (Prim_Elmt);
10177 end loop;
10178 end if;
10179 end if;
10180 end Map_Primitives;
10182 -- Start of processing for Map_Types
10184 begin
10185 -- Nothing to do if there are no types to work with
10187 if No (Parent_Type) or else No (Derived_Type) then
10188 return;
10190 -- Nothing to do if the mapping already exists
10192 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10193 return;
10195 -- Nothing to do if both types are not tagged. Note that untagged types
10196 -- do not have primitive operations and their discriminants are already
10197 -- handled by gigi.
10199 elsif not Is_Tagged_Type (Parent_Type)
10200 or else not Is_Tagged_Type (Derived_Type)
10201 then
10202 return;
10203 end if;
10205 -- Create a mapping of the form
10207 -- parent type -> derived type
10209 -- to prevent any subsequent attempts to produce the same relations
10211 Type_Map.Set (Parent_Type, Derived_Type);
10213 -- Create mappings of the form
10215 -- parent type discriminant -> derived type discriminant
10216 -- <or>
10217 -- parent type discriminant -> constraint
10219 -- Note that mapping of discriminants breaks privacy because it needs to
10220 -- work with those views which contains the discriminants and any stored
10221 -- constraints.
10223 Map_Discriminants
10224 (Par_Typ => Discriminated_View (Parent_Type),
10225 Deriv_Typ => Discriminated_View (Derived_Type));
10227 -- Create mappings of the form
10229 -- parent type primitive -> derived type primitive
10231 Map_Primitives
10232 (Par_Typ => Parent_Type,
10233 Deriv_Typ => Derived_Type);
10234 end Map_Types;
10236 ----------------------------
10237 -- Matching_Standard_Type --
10238 ----------------------------
10240 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10241 pragma Assert (Is_Scalar_Type (Typ));
10242 Siz : constant Uint := Esize (Typ);
10244 begin
10245 -- Floating-point cases
10247 if Is_Floating_Point_Type (Typ) then
10248 if Siz <= Esize (Standard_Short_Float) then
10249 return Standard_Short_Float;
10250 elsif Siz <= Esize (Standard_Float) then
10251 return Standard_Float;
10252 elsif Siz <= Esize (Standard_Long_Float) then
10253 return Standard_Long_Float;
10254 elsif Siz <= Esize (Standard_Long_Long_Float) then
10255 return Standard_Long_Long_Float;
10256 else
10257 raise Program_Error;
10258 end if;
10260 -- Integer cases (includes fixed-point types)
10262 -- Unsigned integer cases (includes normal enumeration types)
10264 elsif Is_Unsigned_Type (Typ) then
10265 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10266 return Standard_Short_Short_Unsigned;
10267 elsif Siz <= Esize (Standard_Short_Unsigned) then
10268 return Standard_Short_Unsigned;
10269 elsif Siz <= Esize (Standard_Unsigned) then
10270 return Standard_Unsigned;
10271 elsif Siz <= Esize (Standard_Long_Unsigned) then
10272 return Standard_Long_Unsigned;
10273 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10274 return Standard_Long_Long_Unsigned;
10275 else
10276 raise Program_Error;
10277 end if;
10279 -- Signed integer cases
10281 else
10282 if Siz <= Esize (Standard_Short_Short_Integer) then
10283 return Standard_Short_Short_Integer;
10284 elsif Siz <= Esize (Standard_Short_Integer) then
10285 return Standard_Short_Integer;
10286 elsif Siz <= Esize (Standard_Integer) then
10287 return Standard_Integer;
10288 elsif Siz <= Esize (Standard_Long_Integer) then
10289 return Standard_Long_Integer;
10290 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10291 return Standard_Long_Long_Integer;
10292 else
10293 raise Program_Error;
10294 end if;
10295 end if;
10296 end Matching_Standard_Type;
10298 -----------------------------
10299 -- May_Generate_Large_Temp --
10300 -----------------------------
10302 -- At the current time, the only types that we return False for (i.e. where
10303 -- we decide we know they cannot generate large temps) are ones where we
10304 -- know the size is 256 bits or less at compile time, and we are still not
10305 -- doing a thorough job on arrays and records ???
10307 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10308 begin
10309 if not Size_Known_At_Compile_Time (Typ) then
10310 return False;
10312 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10313 return False;
10315 elsif Is_Array_Type (Typ)
10316 and then Present (Packed_Array_Impl_Type (Typ))
10317 then
10318 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10320 -- We could do more here to find other small types ???
10322 else
10323 return True;
10324 end if;
10325 end May_Generate_Large_Temp;
10327 ------------------------
10328 -- Needs_Finalization --
10329 ------------------------
10331 function Needs_Finalization (Typ : Entity_Id) return Boolean is
10332 function Has_Some_Controlled_Component
10333 (Input_Typ : Entity_Id) return Boolean;
10334 -- Determine whether type Input_Typ has at least one controlled
10335 -- component.
10337 -----------------------------------
10338 -- Has_Some_Controlled_Component --
10339 -----------------------------------
10341 function Has_Some_Controlled_Component
10342 (Input_Typ : Entity_Id) return Boolean
10344 Comp : Entity_Id;
10346 begin
10347 -- When a type is already frozen and has at least one controlled
10348 -- component, or is manually decorated, it is sufficient to inspect
10349 -- flag Has_Controlled_Component.
10351 if Has_Controlled_Component (Input_Typ) then
10352 return True;
10354 -- Otherwise inspect the internals of the type
10356 elsif not Is_Frozen (Input_Typ) then
10357 if Is_Array_Type (Input_Typ) then
10358 return Needs_Finalization (Component_Type (Input_Typ));
10360 elsif Is_Record_Type (Input_Typ) then
10361 Comp := First_Component (Input_Typ);
10362 while Present (Comp) loop
10363 if Needs_Finalization (Etype (Comp)) then
10364 return True;
10365 end if;
10367 Next_Component (Comp);
10368 end loop;
10369 end if;
10370 end if;
10372 return False;
10373 end Has_Some_Controlled_Component;
10375 -- Start of processing for Needs_Finalization
10377 begin
10378 -- Certain run-time configurations and targets do not provide support
10379 -- for controlled types.
10381 if Restriction_Active (No_Finalization) then
10382 return False;
10384 -- C++ types are not considered controlled. It is assumed that the non-
10385 -- Ada side will handle their clean up.
10387 elsif Convention (Typ) = Convention_CPP then
10388 return False;
10390 -- Class-wide types are treated as controlled because derivations from
10391 -- the root type may introduce controlled components.
10393 elsif Is_Class_Wide_Type (Typ) then
10394 return True;
10396 -- Concurrent types are controlled as long as their corresponding record
10397 -- is controlled.
10399 elsif Is_Concurrent_Type (Typ)
10400 and then Present (Corresponding_Record_Type (Typ))
10401 and then Needs_Finalization (Corresponding_Record_Type (Typ))
10402 then
10403 return True;
10405 -- Otherwise the type is controlled when it is either derived from type
10406 -- [Limited_]Controlled and not subject to aspect Disable_Controlled, or
10407 -- contains at least one controlled component.
10409 else
10410 return
10411 Is_Controlled (Typ) or else Has_Some_Controlled_Component (Typ);
10412 end if;
10413 end Needs_Finalization;
10415 ----------------------------
10416 -- Needs_Constant_Address --
10417 ----------------------------
10419 function Needs_Constant_Address
10420 (Decl : Node_Id;
10421 Typ : Entity_Id) return Boolean
10423 begin
10424 -- If we have no initialization of any kind, then we don't need to place
10425 -- any restrictions on the address clause, because the object will be
10426 -- elaborated after the address clause is evaluated. This happens if the
10427 -- declaration has no initial expression, or the type has no implicit
10428 -- initialization, or the object is imported.
10430 -- The same holds for all initialized scalar types and all access types.
10431 -- Packed bit arrays of size up to 64 are represented using a modular
10432 -- type with an initialization (to zero) and can be processed like other
10433 -- initialized scalar types.
10435 -- If the type is controlled, code to attach the object to a
10436 -- finalization chain is generated at the point of declaration, and
10437 -- therefore the elaboration of the object cannot be delayed: the
10438 -- address expression must be a constant.
10440 if No (Expression (Decl))
10441 and then not Needs_Finalization (Typ)
10442 and then
10443 (not Has_Non_Null_Base_Init_Proc (Typ)
10444 or else Is_Imported (Defining_Identifier (Decl)))
10445 then
10446 return False;
10448 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10449 or else Is_Access_Type (Typ)
10450 or else
10451 (Is_Bit_Packed_Array (Typ)
10452 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10453 then
10454 return False;
10456 else
10458 -- Otherwise, we require the address clause to be constant because
10459 -- the call to the initialization procedure (or the attach code) has
10460 -- to happen at the point of the declaration.
10462 -- Actually the IP call has been moved to the freeze actions anyway,
10463 -- so maybe we can relax this restriction???
10465 return True;
10466 end if;
10467 end Needs_Constant_Address;
10469 ----------------------------
10470 -- New_Class_Wide_Subtype --
10471 ----------------------------
10473 function New_Class_Wide_Subtype
10474 (CW_Typ : Entity_Id;
10475 N : Node_Id) return Entity_Id
10477 Res : constant Entity_Id := Create_Itype (E_Void, N);
10478 Res_Name : constant Name_Id := Chars (Res);
10479 Res_Scope : constant Entity_Id := Scope (Res);
10481 begin
10482 Copy_Node (CW_Typ, Res);
10483 Set_Comes_From_Source (Res, False);
10484 Set_Sloc (Res, Sloc (N));
10485 Set_Is_Itype (Res);
10486 Set_Associated_Node_For_Itype (Res, N);
10487 Set_Is_Public (Res, False); -- By default, may be changed below.
10488 Set_Public_Status (Res);
10489 Set_Chars (Res, Res_Name);
10490 Set_Scope (Res, Res_Scope);
10491 Set_Ekind (Res, E_Class_Wide_Subtype);
10492 Set_Next_Entity (Res, Empty);
10493 Set_Etype (Res, Base_Type (CW_Typ));
10494 Set_Is_Frozen (Res, False);
10495 Set_Freeze_Node (Res, Empty);
10496 return (Res);
10497 end New_Class_Wide_Subtype;
10499 --------------------------------
10500 -- Non_Limited_Designated_Type --
10501 ---------------------------------
10503 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10504 Desig : constant Entity_Id := Designated_Type (T);
10505 begin
10506 if Has_Non_Limited_View (Desig) then
10507 return Non_Limited_View (Desig);
10508 else
10509 return Desig;
10510 end if;
10511 end Non_Limited_Designated_Type;
10513 -----------------------------------
10514 -- OK_To_Do_Constant_Replacement --
10515 -----------------------------------
10517 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10518 ES : constant Entity_Id := Scope (E);
10519 CS : Entity_Id;
10521 begin
10522 -- Do not replace statically allocated objects, because they may be
10523 -- modified outside the current scope.
10525 if Is_Statically_Allocated (E) then
10526 return False;
10528 -- Do not replace aliased or volatile objects, since we don't know what
10529 -- else might change the value.
10531 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10532 return False;
10534 -- Debug flag -gnatdM disconnects this optimization
10536 elsif Debug_Flag_MM then
10537 return False;
10539 -- Otherwise check scopes
10541 else
10542 CS := Current_Scope;
10544 loop
10545 -- If we are in right scope, replacement is safe
10547 if CS = ES then
10548 return True;
10550 -- Packages do not affect the determination of safety
10552 elsif Ekind (CS) = E_Package then
10553 exit when CS = Standard_Standard;
10554 CS := Scope (CS);
10556 -- Blocks do not affect the determination of safety
10558 elsif Ekind (CS) = E_Block then
10559 CS := Scope (CS);
10561 -- Loops do not affect the determination of safety. Note that we
10562 -- kill all current values on entry to a loop, so we are just
10563 -- talking about processing within a loop here.
10565 elsif Ekind (CS) = E_Loop then
10566 CS := Scope (CS);
10568 -- Otherwise, the reference is dubious, and we cannot be sure that
10569 -- it is safe to do the replacement.
10571 else
10572 exit;
10573 end if;
10574 end loop;
10576 return False;
10577 end if;
10578 end OK_To_Do_Constant_Replacement;
10580 ------------------------------------
10581 -- Possible_Bit_Aligned_Component --
10582 ------------------------------------
10584 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10585 begin
10586 -- Do not process an unanalyzed node because it is not yet decorated and
10587 -- most checks performed below will fail.
10589 if not Analyzed (N) then
10590 return False;
10591 end if;
10593 case Nkind (N) is
10595 -- Case of indexed component
10597 when N_Indexed_Component =>
10598 declare
10599 P : constant Node_Id := Prefix (N);
10600 Ptyp : constant Entity_Id := Etype (P);
10602 begin
10603 -- If we know the component size and it is less than 64, then
10604 -- we are definitely OK. The back end always does assignment of
10605 -- misaligned small objects correctly.
10607 if Known_Static_Component_Size (Ptyp)
10608 and then Component_Size (Ptyp) <= 64
10609 then
10610 return False;
10612 -- Otherwise, we need to test the prefix, to see if we are
10613 -- indexing from a possibly unaligned component.
10615 else
10616 return Possible_Bit_Aligned_Component (P);
10617 end if;
10618 end;
10620 -- Case of selected component
10622 when N_Selected_Component =>
10623 declare
10624 P : constant Node_Id := Prefix (N);
10625 Comp : constant Entity_Id := Entity (Selector_Name (N));
10627 begin
10628 -- If there is no component clause, then we are in the clear
10629 -- since the back end will never misalign a large component
10630 -- unless it is forced to do so. In the clear means we need
10631 -- only the recursive test on the prefix.
10633 if Component_May_Be_Bit_Aligned (Comp) then
10634 return True;
10635 else
10636 return Possible_Bit_Aligned_Component (P);
10637 end if;
10638 end;
10640 -- For a slice, test the prefix, if that is possibly misaligned,
10641 -- then for sure the slice is.
10643 when N_Slice =>
10644 return Possible_Bit_Aligned_Component (Prefix (N));
10646 -- For an unchecked conversion, check whether the expression may
10647 -- be bit-aligned.
10649 when N_Unchecked_Type_Conversion =>
10650 return Possible_Bit_Aligned_Component (Expression (N));
10652 -- If we have none of the above, it means that we have fallen off the
10653 -- top testing prefixes recursively, and we now have a stand alone
10654 -- object, where we don't have a problem, unless this is a renaming,
10655 -- in which case we need to look into the renamed object.
10657 when others =>
10658 if Is_Entity_Name (N)
10659 and then Present (Renamed_Object (Entity (N)))
10660 then
10661 return
10662 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10663 else
10664 return False;
10665 end if;
10666 end case;
10667 end Possible_Bit_Aligned_Component;
10669 -----------------------------------------------
10670 -- Process_Statements_For_Controlled_Objects --
10671 -----------------------------------------------
10673 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10674 Loc : constant Source_Ptr := Sloc (N);
10676 function Are_Wrapped (L : List_Id) return Boolean;
10677 -- Determine whether list L contains only one statement which is a block
10679 function Wrap_Statements_In_Block
10680 (L : List_Id;
10681 Scop : Entity_Id := Current_Scope) return Node_Id;
10682 -- Given a list of statements L, wrap it in a block statement and return
10683 -- the generated node. Scop is either the current scope or the scope of
10684 -- the context (if applicable).
10686 -----------------
10687 -- Are_Wrapped --
10688 -----------------
10690 function Are_Wrapped (L : List_Id) return Boolean is
10691 Stmt : constant Node_Id := First (L);
10692 begin
10693 return
10694 Present (Stmt)
10695 and then No (Next (Stmt))
10696 and then Nkind (Stmt) = N_Block_Statement;
10697 end Are_Wrapped;
10699 ------------------------------
10700 -- Wrap_Statements_In_Block --
10701 ------------------------------
10703 function Wrap_Statements_In_Block
10704 (L : List_Id;
10705 Scop : Entity_Id := Current_Scope) return Node_Id
10707 Block_Id : Entity_Id;
10708 Block_Nod : Node_Id;
10709 Iter_Loop : Entity_Id;
10711 begin
10712 Block_Nod :=
10713 Make_Block_Statement (Loc,
10714 Declarations => No_List,
10715 Handled_Statement_Sequence =>
10716 Make_Handled_Sequence_Of_Statements (Loc,
10717 Statements => L));
10719 -- Create a label for the block in case the block needs to manage the
10720 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10722 Add_Block_Identifier (Block_Nod, Block_Id);
10724 -- When wrapping the statements of an iterator loop, check whether
10725 -- the loop requires secondary stack management and if so, propagate
10726 -- the appropriate flags to the block. This ensures that the cursor
10727 -- is properly cleaned up at each iteration of the loop.
10729 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10731 if Present (Iter_Loop) then
10732 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10734 -- Secondary stack reclamation is suppressed when the associated
10735 -- iterator loop contains a return statement which uses the stack.
10737 Set_Sec_Stack_Needed_For_Return
10738 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10739 end if;
10741 return Block_Nod;
10742 end Wrap_Statements_In_Block;
10744 -- Local variables
10746 Block : Node_Id;
10748 -- Start of processing for Process_Statements_For_Controlled_Objects
10750 begin
10751 -- Whenever a non-handled statement list is wrapped in a block, the
10752 -- block must be explicitly analyzed to redecorate all entities in the
10753 -- list and ensure that a finalizer is properly built.
10755 case Nkind (N) is
10756 when N_Conditional_Entry_Call
10757 | N_Elsif_Part
10758 | N_If_Statement
10759 | N_Selective_Accept
10761 -- Check the "then statements" for elsif parts and if statements
10763 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10764 and then not Is_Empty_List (Then_Statements (N))
10765 and then not Are_Wrapped (Then_Statements (N))
10766 and then Requires_Cleanup_Actions
10767 (Then_Statements (N), False, False)
10768 then
10769 Block := Wrap_Statements_In_Block (Then_Statements (N));
10770 Set_Then_Statements (N, New_List (Block));
10772 Analyze (Block);
10773 end if;
10775 -- Check the "else statements" for conditional entry calls, if
10776 -- statements and selective accepts.
10778 if Nkind_In (N, N_Conditional_Entry_Call,
10779 N_If_Statement,
10780 N_Selective_Accept)
10781 and then not Is_Empty_List (Else_Statements (N))
10782 and then not Are_Wrapped (Else_Statements (N))
10783 and then Requires_Cleanup_Actions
10784 (Else_Statements (N), False, False)
10785 then
10786 Block := Wrap_Statements_In_Block (Else_Statements (N));
10787 Set_Else_Statements (N, New_List (Block));
10789 Analyze (Block);
10790 end if;
10792 when N_Abortable_Part
10793 | N_Accept_Alternative
10794 | N_Case_Statement_Alternative
10795 | N_Delay_Alternative
10796 | N_Entry_Call_Alternative
10797 | N_Exception_Handler
10798 | N_Loop_Statement
10799 | N_Triggering_Alternative
10801 if not Is_Empty_List (Statements (N))
10802 and then not Are_Wrapped (Statements (N))
10803 and then Requires_Cleanup_Actions (Statements (N), False, False)
10804 then
10805 if Nkind (N) = N_Loop_Statement
10806 and then Present (Identifier (N))
10807 then
10808 Block :=
10809 Wrap_Statements_In_Block
10810 (L => Statements (N),
10811 Scop => Entity (Identifier (N)));
10812 else
10813 Block := Wrap_Statements_In_Block (Statements (N));
10814 end if;
10816 Set_Statements (N, New_List (Block));
10817 Analyze (Block);
10818 end if;
10820 when others =>
10821 null;
10822 end case;
10823 end Process_Statements_For_Controlled_Objects;
10825 ------------------
10826 -- Power_Of_Two --
10827 ------------------
10829 function Power_Of_Two (N : Node_Id) return Nat is
10830 Typ : constant Entity_Id := Etype (N);
10831 pragma Assert (Is_Integer_Type (Typ));
10833 Siz : constant Nat := UI_To_Int (Esize (Typ));
10834 Val : Uint;
10836 begin
10837 if not Compile_Time_Known_Value (N) then
10838 return 0;
10840 else
10841 Val := Expr_Value (N);
10842 for J in 1 .. Siz - 1 loop
10843 if Val = Uint_2 ** J then
10844 return J;
10845 end if;
10846 end loop;
10848 return 0;
10849 end if;
10850 end Power_Of_Two;
10852 ----------------------
10853 -- Remove_Init_Call --
10854 ----------------------
10856 function Remove_Init_Call
10857 (Var : Entity_Id;
10858 Rep_Clause : Node_Id) return Node_Id
10860 Par : constant Node_Id := Parent (Var);
10861 Typ : constant Entity_Id := Etype (Var);
10863 Init_Proc : Entity_Id;
10864 -- Initialization procedure for Typ
10866 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
10867 -- Look for init call for Var starting at From and scanning the
10868 -- enclosing list until Rep_Clause or the end of the list is reached.
10870 ----------------------------
10871 -- Find_Init_Call_In_List --
10872 ----------------------------
10874 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
10875 Init_Call : Node_Id;
10877 begin
10878 Init_Call := From;
10879 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
10880 if Nkind (Init_Call) = N_Procedure_Call_Statement
10881 and then Is_Entity_Name (Name (Init_Call))
10882 and then Entity (Name (Init_Call)) = Init_Proc
10883 then
10884 return Init_Call;
10885 end if;
10887 Next (Init_Call);
10888 end loop;
10890 return Empty;
10891 end Find_Init_Call_In_List;
10893 Init_Call : Node_Id;
10895 -- Start of processing for Find_Init_Call
10897 begin
10898 if Present (Initialization_Statements (Var)) then
10899 Init_Call := Initialization_Statements (Var);
10900 Set_Initialization_Statements (Var, Empty);
10902 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
10904 -- No init proc for the type, so obviously no call to be found
10906 return Empty;
10908 else
10909 -- We might be able to handle other cases below by just properly
10910 -- setting Initialization_Statements at the point where the init proc
10911 -- call is generated???
10913 Init_Proc := Base_Init_Proc (Typ);
10915 -- First scan the list containing the declaration of Var
10917 Init_Call := Find_Init_Call_In_List (From => Next (Par));
10919 -- If not found, also look on Var's freeze actions list, if any,
10920 -- since the init call may have been moved there (case of an address
10921 -- clause applying to Var).
10923 if No (Init_Call) and then Present (Freeze_Node (Var)) then
10924 Init_Call :=
10925 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
10926 end if;
10928 -- If the initialization call has actuals that use the secondary
10929 -- stack, the call may have been wrapped into a temporary block, in
10930 -- which case the block itself has to be removed.
10932 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
10933 declare
10934 Blk : constant Node_Id := Next (Par);
10935 begin
10936 if Present
10937 (Find_Init_Call_In_List
10938 (First (Statements (Handled_Statement_Sequence (Blk)))))
10939 then
10940 Init_Call := Blk;
10941 end if;
10942 end;
10943 end if;
10944 end if;
10946 if Present (Init_Call) then
10947 Remove (Init_Call);
10948 end if;
10949 return Init_Call;
10950 end Remove_Init_Call;
10952 -------------------------
10953 -- Remove_Side_Effects --
10954 -------------------------
10956 procedure Remove_Side_Effects
10957 (Exp : Node_Id;
10958 Name_Req : Boolean := False;
10959 Renaming_Req : Boolean := False;
10960 Variable_Ref : Boolean := False;
10961 Related_Id : Entity_Id := Empty;
10962 Is_Low_Bound : Boolean := False;
10963 Is_High_Bound : Boolean := False;
10964 Check_Side_Effects : Boolean := True)
10966 function Build_Temporary
10967 (Loc : Source_Ptr;
10968 Id : Character;
10969 Related_Nod : Node_Id := Empty) return Entity_Id;
10970 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
10971 -- is present (xxx is taken from the Chars field of Related_Nod),
10972 -- otherwise it generates an internal temporary.
10974 ---------------------
10975 -- Build_Temporary --
10976 ---------------------
10978 function Build_Temporary
10979 (Loc : Source_Ptr;
10980 Id : Character;
10981 Related_Nod : Node_Id := Empty) return Entity_Id
10983 Temp_Nam : Name_Id;
10985 begin
10986 -- The context requires an external symbol
10988 if Present (Related_Id) then
10989 if Is_Low_Bound then
10990 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
10991 else pragma Assert (Is_High_Bound);
10992 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
10993 end if;
10995 return Make_Defining_Identifier (Loc, Temp_Nam);
10997 -- Otherwise generate an internal temporary
10999 else
11000 return Make_Temporary (Loc, Id, Related_Nod);
11001 end if;
11002 end Build_Temporary;
11004 -- Local variables
11006 Loc : constant Source_Ptr := Sloc (Exp);
11007 Exp_Type : constant Entity_Id := Etype (Exp);
11008 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
11009 Def_Id : Entity_Id;
11010 E : Node_Id;
11011 New_Exp : Node_Id;
11012 Ptr_Typ_Decl : Node_Id;
11013 Ref_Type : Entity_Id;
11014 Res : Node_Id;
11016 -- Start of processing for Remove_Side_Effects
11018 begin
11019 -- Handle cases in which there is nothing to do. In GNATprove mode,
11020 -- removal of side effects is useful for the light expansion of
11021 -- renamings. This removal should only occur when not inside a
11022 -- generic and not doing a pre-analysis.
11024 if not Expander_Active
11025 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
11026 then
11027 return;
11029 -- Cannot generate temporaries if the invocation to remove side effects
11030 -- was issued too early and the type of the expression is not resolved
11031 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11032 -- Remove_Side_Effects).
11034 elsif No (Exp_Type)
11035 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11036 then
11037 return;
11039 -- Nothing to do if prior expansion determined that a function call does
11040 -- not require side effect removal.
11042 elsif Nkind (Exp) = N_Function_Call
11043 and then No_Side_Effect_Removal (Exp)
11044 then
11045 return;
11047 -- No action needed for side-effect free expressions
11049 elsif Check_Side_Effects
11050 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11051 then
11052 return;
11053 end if;
11055 -- The remaining processing is done with all checks suppressed
11057 -- Note: from now on, don't use return statements, instead do a goto
11058 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11060 Scope_Suppress.Suppress := (others => True);
11062 -- If this is an elementary or a small not by-reference record type, and
11063 -- we need to capture the value, just make a constant; this is cheap and
11064 -- objects of both kinds of types can be bit aligned, so it might not be
11065 -- possible to generate a reference to them. Likewise if this is not a
11066 -- name reference, except for a type conversion because we would enter
11067 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11068 -- type has predicates (and type conversions need a specific treatment
11069 -- anyway, see below). Also do it if we have a volatile reference and
11070 -- Name_Req is not set (see comments for Side_Effect_Free).
11072 if (Is_Elementary_Type (Exp_Type)
11073 or else (Is_Record_Type (Exp_Type)
11074 and then Known_Static_RM_Size (Exp_Type)
11075 and then RM_Size (Exp_Type) <= 64
11076 and then not Has_Discriminants (Exp_Type)
11077 and then not Is_By_Reference_Type (Exp_Type)))
11078 and then (Variable_Ref
11079 or else (not Is_Name_Reference (Exp)
11080 and then Nkind (Exp) /= N_Type_Conversion)
11081 or else (not Name_Req
11082 and then Is_Volatile_Reference (Exp)))
11083 then
11084 Def_Id := Build_Temporary (Loc, 'R', Exp);
11085 Set_Etype (Def_Id, Exp_Type);
11086 Res := New_Occurrence_Of (Def_Id, Loc);
11088 -- If the expression is a packed reference, it must be reanalyzed and
11089 -- expanded, depending on context. This is the case for actuals where
11090 -- a constraint check may capture the actual before expansion of the
11091 -- call is complete.
11093 if Nkind (Exp) = N_Indexed_Component
11094 and then Is_Packed (Etype (Prefix (Exp)))
11095 then
11096 Set_Analyzed (Exp, False);
11097 Set_Analyzed (Prefix (Exp), False);
11098 end if;
11100 -- Generate:
11101 -- Rnn : Exp_Type renames Expr;
11103 if Renaming_Req then
11104 E :=
11105 Make_Object_Renaming_Declaration (Loc,
11106 Defining_Identifier => Def_Id,
11107 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11108 Name => Relocate_Node (Exp));
11110 -- Generate:
11111 -- Rnn : constant Exp_Type := Expr;
11113 else
11114 E :=
11115 Make_Object_Declaration (Loc,
11116 Defining_Identifier => Def_Id,
11117 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11118 Constant_Present => True,
11119 Expression => Relocate_Node (Exp));
11121 Set_Assignment_OK (E);
11122 end if;
11124 Insert_Action (Exp, E);
11126 -- If the expression has the form v.all then we can just capture the
11127 -- pointer, and then do an explicit dereference on the result, but
11128 -- this is not right if this is a volatile reference.
11130 elsif Nkind (Exp) = N_Explicit_Dereference
11131 and then not Is_Volatile_Reference (Exp)
11132 then
11133 Def_Id := Build_Temporary (Loc, 'R', Exp);
11134 Res :=
11135 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11137 Insert_Action (Exp,
11138 Make_Object_Declaration (Loc,
11139 Defining_Identifier => Def_Id,
11140 Object_Definition =>
11141 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11142 Constant_Present => True,
11143 Expression => Relocate_Node (Prefix (Exp))));
11145 -- Similar processing for an unchecked conversion of an expression of
11146 -- the form v.all, where we want the same kind of treatment.
11148 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11149 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11150 then
11151 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11152 goto Leave;
11154 -- If this is a type conversion, leave the type conversion and remove
11155 -- the side effects in the expression. This is important in several
11156 -- circumstances: for change of representations, and also when this is a
11157 -- view conversion to a smaller object, where gigi can end up creating
11158 -- its own temporary of the wrong size.
11160 elsif Nkind (Exp) = N_Type_Conversion then
11161 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11163 -- Generating C code the type conversion of an access to constrained
11164 -- array type into an access to unconstrained array type involves
11165 -- initializing a fat pointer and the expression must be free of
11166 -- side effects to safely compute its bounds.
11168 if Modify_Tree_For_C
11169 and then Is_Access_Type (Etype (Exp))
11170 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11171 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11172 then
11173 Def_Id := Build_Temporary (Loc, 'R', Exp);
11174 Set_Etype (Def_Id, Exp_Type);
11175 Res := New_Occurrence_Of (Def_Id, Loc);
11177 Insert_Action (Exp,
11178 Make_Object_Declaration (Loc,
11179 Defining_Identifier => Def_Id,
11180 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11181 Constant_Present => True,
11182 Expression => Relocate_Node (Exp)));
11183 else
11184 goto Leave;
11185 end if;
11187 -- If this is an unchecked conversion that Gigi can't handle, make
11188 -- a copy or a use a renaming to capture the value.
11190 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11191 and then not Safe_Unchecked_Type_Conversion (Exp)
11192 then
11193 if CW_Or_Has_Controlled_Part (Exp_Type) then
11195 -- Use a renaming to capture the expression, rather than create
11196 -- a controlled temporary.
11198 Def_Id := Build_Temporary (Loc, 'R', Exp);
11199 Res := New_Occurrence_Of (Def_Id, Loc);
11201 Insert_Action (Exp,
11202 Make_Object_Renaming_Declaration (Loc,
11203 Defining_Identifier => Def_Id,
11204 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11205 Name => Relocate_Node (Exp)));
11207 else
11208 Def_Id := Build_Temporary (Loc, 'R', Exp);
11209 Set_Etype (Def_Id, Exp_Type);
11210 Res := New_Occurrence_Of (Def_Id, Loc);
11212 E :=
11213 Make_Object_Declaration (Loc,
11214 Defining_Identifier => Def_Id,
11215 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11216 Constant_Present => not Is_Variable (Exp),
11217 Expression => Relocate_Node (Exp));
11219 Set_Assignment_OK (E);
11220 Insert_Action (Exp, E);
11221 end if;
11223 -- For expressions that denote names, we can use a renaming scheme.
11224 -- This is needed for correctness in the case of a volatile object of
11225 -- a non-volatile type because the Make_Reference call of the "default"
11226 -- approach would generate an illegal access value (an access value
11227 -- cannot designate such an object - see Analyze_Reference).
11229 elsif Is_Name_Reference (Exp)
11231 -- We skip using this scheme if we have an object of a volatile
11232 -- type and we do not have Name_Req set true (see comments for
11233 -- Side_Effect_Free).
11235 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11236 then
11237 Def_Id := Build_Temporary (Loc, 'R', Exp);
11238 Res := New_Occurrence_Of (Def_Id, Loc);
11240 Insert_Action (Exp,
11241 Make_Object_Renaming_Declaration (Loc,
11242 Defining_Identifier => Def_Id,
11243 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11244 Name => Relocate_Node (Exp)));
11246 -- If this is a packed reference, or a selected component with
11247 -- a non-standard representation, a reference to the temporary
11248 -- will be replaced by a copy of the original expression (see
11249 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11250 -- elaborated by gigi, and is of course not to be replaced in-line
11251 -- by the expression it renames, which would defeat the purpose of
11252 -- removing the side-effect.
11254 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11255 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11256 then
11257 null;
11258 else
11259 Set_Is_Renaming_Of_Object (Def_Id, False);
11260 end if;
11262 -- Avoid generating a variable-sized temporary, by generating the
11263 -- reference just for the function call. The transformation could be
11264 -- refined to apply only when the array component is constrained by a
11265 -- discriminant???
11267 elsif Nkind (Exp) = N_Selected_Component
11268 and then Nkind (Prefix (Exp)) = N_Function_Call
11269 and then Is_Array_Type (Exp_Type)
11270 then
11271 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11272 goto Leave;
11274 -- Otherwise we generate a reference to the expression
11276 else
11277 -- An expression which is in SPARK mode is considered side effect
11278 -- free if the resulting value is captured by a variable or a
11279 -- constant.
11281 if GNATprove_Mode
11282 and then Nkind (Parent (Exp)) = N_Object_Declaration
11283 then
11284 goto Leave;
11286 -- When generating C code we cannot consider side effect free object
11287 -- declarations that have discriminants and are initialized by means
11288 -- of a function call since on this target there is no secondary
11289 -- stack to store the return value and the expander may generate an
11290 -- extra call to the function to compute the discriminant value. In
11291 -- addition, for targets that have secondary stack, the expansion of
11292 -- functions with side effects involves the generation of an access
11293 -- type to capture the return value stored in the secondary stack;
11294 -- by contrast when generating C code such expansion generates an
11295 -- internal object declaration (no access type involved) which must
11296 -- be identified here to avoid entering into a never-ending loop
11297 -- generating internal object declarations.
11299 elsif Modify_Tree_For_C
11300 and then Nkind (Parent (Exp)) = N_Object_Declaration
11301 and then
11302 (Nkind (Exp) /= N_Function_Call
11303 or else not Has_Discriminants (Exp_Type)
11304 or else Is_Internal_Name
11305 (Chars (Defining_Identifier (Parent (Exp)))))
11306 then
11307 goto Leave;
11308 end if;
11310 -- Special processing for function calls that return a limited type.
11311 -- We need to build a declaration that will enable build-in-place
11312 -- expansion of the call. This is not done if the context is already
11313 -- an object declaration, to prevent infinite recursion.
11315 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11316 -- to accommodate functions returning limited objects by reference.
11318 if Ada_Version >= Ada_2005
11319 and then Nkind (Exp) = N_Function_Call
11320 and then Is_Limited_View (Etype (Exp))
11321 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11322 then
11323 declare
11324 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11325 Decl : Node_Id;
11327 begin
11328 Decl :=
11329 Make_Object_Declaration (Loc,
11330 Defining_Identifier => Obj,
11331 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11332 Expression => Relocate_Node (Exp));
11334 Insert_Action (Exp, Decl);
11335 Set_Etype (Obj, Exp_Type);
11336 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11337 goto Leave;
11338 end;
11339 end if;
11341 Def_Id := Build_Temporary (Loc, 'R', Exp);
11343 -- The regular expansion of functions with side effects involves the
11344 -- generation of an access type to capture the return value found on
11345 -- the secondary stack. Since SPARK (and why) cannot process access
11346 -- types, use a different approach which ignores the secondary stack
11347 -- and "copies" the returned object.
11348 -- When generating C code, no need for a 'reference since the
11349 -- secondary stack is not supported.
11351 if GNATprove_Mode or Modify_Tree_For_C then
11352 Res := New_Occurrence_Of (Def_Id, Loc);
11353 Ref_Type := Exp_Type;
11355 -- Regular expansion utilizing an access type and 'reference
11357 else
11358 Res :=
11359 Make_Explicit_Dereference (Loc,
11360 Prefix => New_Occurrence_Of (Def_Id, Loc));
11362 -- Generate:
11363 -- type Ann is access all <Exp_Type>;
11365 Ref_Type := Make_Temporary (Loc, 'A');
11367 Ptr_Typ_Decl :=
11368 Make_Full_Type_Declaration (Loc,
11369 Defining_Identifier => Ref_Type,
11370 Type_Definition =>
11371 Make_Access_To_Object_Definition (Loc,
11372 All_Present => True,
11373 Subtype_Indication =>
11374 New_Occurrence_Of (Exp_Type, Loc)));
11376 Insert_Action (Exp, Ptr_Typ_Decl);
11377 end if;
11379 E := Exp;
11380 if Nkind (E) = N_Explicit_Dereference then
11381 New_Exp := Relocate_Node (Prefix (E));
11383 else
11384 E := Relocate_Node (E);
11386 -- Do not generate a 'reference in SPARK mode or C generation
11387 -- since the access type is not created in the first place.
11389 if GNATprove_Mode or Modify_Tree_For_C then
11390 New_Exp := E;
11392 -- Otherwise generate reference, marking the value as non-null
11393 -- since we know it cannot be null and we don't want a check.
11395 else
11396 New_Exp := Make_Reference (Loc, E);
11397 Set_Is_Known_Non_Null (Def_Id);
11398 end if;
11399 end if;
11401 if Is_Delayed_Aggregate (E) then
11403 -- The expansion of nested aggregates is delayed until the
11404 -- enclosing aggregate is expanded. As aggregates are often
11405 -- qualified, the predicate applies to qualified expressions as
11406 -- well, indicating that the enclosing aggregate has not been
11407 -- expanded yet. At this point the aggregate is part of a
11408 -- stand-alone declaration, and must be fully expanded.
11410 if Nkind (E) = N_Qualified_Expression then
11411 Set_Expansion_Delayed (Expression (E), False);
11412 Set_Analyzed (Expression (E), False);
11413 else
11414 Set_Expansion_Delayed (E, False);
11415 end if;
11417 Set_Analyzed (E, False);
11418 end if;
11420 -- Generating C code of object declarations that have discriminants
11421 -- and are initialized by means of a function call we propagate the
11422 -- discriminants of the parent type to the internally built object.
11423 -- This is needed to avoid generating an extra call to the called
11424 -- function.
11426 -- For example, if we generate here the following declaration, it
11427 -- will be expanded later adding an extra call to evaluate the value
11428 -- of the discriminant (needed to compute the size of the object).
11430 -- type Rec (D : Integer) is ...
11431 -- Obj : constant Rec := SomeFunc;
11433 if Modify_Tree_For_C
11434 and then Nkind (Parent (Exp)) = N_Object_Declaration
11435 and then Has_Discriminants (Exp_Type)
11436 and then Nkind (Exp) = N_Function_Call
11437 then
11438 Insert_Action (Exp,
11439 Make_Object_Declaration (Loc,
11440 Defining_Identifier => Def_Id,
11441 Object_Definition => New_Copy_Tree
11442 (Object_Definition (Parent (Exp))),
11443 Constant_Present => True,
11444 Expression => New_Exp));
11445 else
11446 Insert_Action (Exp,
11447 Make_Object_Declaration (Loc,
11448 Defining_Identifier => Def_Id,
11449 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11450 Constant_Present => True,
11451 Expression => New_Exp));
11452 end if;
11453 end if;
11455 -- Preserve the Assignment_OK flag in all copies, since at least one
11456 -- copy may be used in a context where this flag must be set (otherwise
11457 -- why would the flag be set in the first place).
11459 Set_Assignment_OK (Res, Assignment_OK (Exp));
11461 -- Finally rewrite the original expression and we are done
11463 Rewrite (Exp, Res);
11464 Analyze_And_Resolve (Exp, Exp_Type);
11466 <<Leave>>
11467 Scope_Suppress := Svg_Suppress;
11468 end Remove_Side_Effects;
11470 ------------------------
11471 -- Replace_References --
11472 ------------------------
11474 procedure Replace_References
11475 (Expr : Node_Id;
11476 Par_Typ : Entity_Id;
11477 Deriv_Typ : Entity_Id;
11478 Par_Obj : Entity_Id := Empty;
11479 Deriv_Obj : Entity_Id := Empty)
11481 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11482 -- Determine whether node Ref denotes some component of Deriv_Obj
11484 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11485 -- Substitute a reference to an entity with the corresponding value
11486 -- stored in table Type_Map.
11488 function Type_Of_Formal
11489 (Call : Node_Id;
11490 Actual : Node_Id) return Entity_Id;
11491 -- Find the type of the formal parameter which corresponds to actual
11492 -- parameter Actual in subprogram call Call.
11494 ----------------------
11495 -- Is_Deriv_Obj_Ref --
11496 ----------------------
11498 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11499 Par : constant Node_Id := Parent (Ref);
11501 begin
11502 -- Detect the folowing selected component form:
11504 -- Deriv_Obj.(something)
11506 return
11507 Nkind (Par) = N_Selected_Component
11508 and then Is_Entity_Name (Prefix (Par))
11509 and then Entity (Prefix (Par)) = Deriv_Obj;
11510 end Is_Deriv_Obj_Ref;
11512 -----------------
11513 -- Replace_Ref --
11514 -----------------
11516 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11517 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11518 -- Reset the Controlling_Argument of all function calls that
11519 -- encapsulate node From_Arg.
11521 ----------------------------------
11522 -- Remove_Controlling_Arguments --
11523 ----------------------------------
11525 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11526 Par : Node_Id;
11528 begin
11529 Par := From_Arg;
11530 while Present (Par) loop
11531 if Nkind (Par) = N_Function_Call
11532 and then Present (Controlling_Argument (Par))
11533 then
11534 Set_Controlling_Argument (Par, Empty);
11536 -- Prevent the search from going too far
11538 elsif Is_Body_Or_Package_Declaration (Par) then
11539 exit;
11540 end if;
11542 Par := Parent (Par);
11543 end loop;
11544 end Remove_Controlling_Arguments;
11546 -- Local variables
11548 Context : constant Node_Id := Parent (Ref);
11549 Loc : constant Source_Ptr := Sloc (Ref);
11550 Ref_Id : Entity_Id;
11551 Result : Traverse_Result;
11553 New_Ref : Node_Id;
11554 -- The new reference which is intended to substitute the old one
11556 Old_Ref : Node_Id;
11557 -- The reference designated for replacement. In certain cases this
11558 -- may be a node other than Ref.
11560 Val : Node_Or_Entity_Id;
11561 -- The corresponding value of Ref from the type map
11563 -- Start of processing for Replace_Ref
11565 begin
11566 -- Assume that the input reference is to be replaced and that the
11567 -- traversal should examine the children of the reference.
11569 Old_Ref := Ref;
11570 Result := OK;
11572 -- The input denotes a meaningful reference
11574 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11575 Ref_Id := Entity (Ref);
11576 Val := Type_Map.Get (Ref_Id);
11578 -- The reference has a corresponding value in the type map, a
11579 -- substitution is possible.
11581 if Present (Val) then
11583 -- The reference denotes a discriminant
11585 if Ekind (Ref_Id) = E_Discriminant then
11586 if Nkind (Val) in N_Entity then
11588 -- The value denotes another discriminant. Replace as
11589 -- follows:
11591 -- _object.Discr -> _object.Val
11593 if Ekind (Val) = E_Discriminant then
11594 New_Ref := New_Occurrence_Of (Val, Loc);
11596 -- Otherwise the value denotes the entity of a name which
11597 -- constraints the discriminant. Replace as follows:
11599 -- _object.Discr -> Val
11601 else
11602 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11604 New_Ref := New_Occurrence_Of (Val, Loc);
11605 Old_Ref := Parent (Old_Ref);
11606 end if;
11608 -- Otherwise the value denotes an arbitrary expression which
11609 -- constraints the discriminant. Replace as follows:
11611 -- _object.Discr -> Val
11613 else
11614 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11616 New_Ref := New_Copy_Tree (Val);
11617 Old_Ref := Parent (Old_Ref);
11618 end if;
11620 -- Otherwise the reference denotes a primitive. Replace as
11621 -- follows:
11623 -- Primitive -> Val
11625 else
11626 pragma Assert (Nkind (Val) in N_Entity);
11627 New_Ref := New_Occurrence_Of (Val, Loc);
11628 end if;
11630 -- The reference mentions the _object parameter of the parent
11631 -- type's DIC or type invariant procedure. Replace as follows:
11633 -- _object -> _object
11635 elsif Present (Par_Obj)
11636 and then Present (Deriv_Obj)
11637 and then Ref_Id = Par_Obj
11638 then
11639 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11641 -- The type of the _object parameter is class-wide when the
11642 -- expression comes from an assertion pragma that applies to
11643 -- an abstract parent type or an interface. The class-wide type
11644 -- facilitates the preanalysis of the expression by treating
11645 -- calls to abstract primitives that mention the current
11646 -- instance of the type as dispatching. Once the calls are
11647 -- remapped to invoke overriding or inherited primitives, the
11648 -- calls no longer need to be dispatching. Examine all function
11649 -- calls that encapsulate the _object parameter and reset their
11650 -- Controlling_Argument attribute.
11652 if Is_Class_Wide_Type (Etype (Par_Obj))
11653 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11654 then
11655 Remove_Controlling_Arguments (Old_Ref);
11656 end if;
11658 -- The reference to _object acts as an actual parameter in a
11659 -- subprogram call which may be invoking a primitive of the
11660 -- parent type:
11662 -- Primitive (... _object ...);
11664 -- The parent type primitive may not be overridden nor
11665 -- inherited when it is declared after the derived type
11666 -- definition:
11668 -- type Parent is tagged private;
11669 -- type Child is new Parent with private;
11670 -- procedure Primitive (Obj : Parent);
11672 -- In this scenario the _object parameter is converted to the
11673 -- parent type. Due to complications with partial/full views
11674 -- and view swaps, the parent type is taken from the formal
11675 -- parameter of the subprogram being called.
11677 if Nkind_In (Context, N_Function_Call,
11678 N_Procedure_Call_Statement)
11679 and then No (Type_Map.Get (Entity (Name (Context))))
11680 then
11681 New_Ref :=
11682 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11684 -- Do not process the generated type conversion because
11685 -- both the parent type and the derived type are in the
11686 -- Type_Map table. This will clobber the type conversion
11687 -- by resetting its subtype mark.
11689 Result := Skip;
11690 end if;
11692 -- Otherwise there is nothing to replace
11694 else
11695 New_Ref := Empty;
11696 end if;
11698 if Present (New_Ref) then
11699 Rewrite (Old_Ref, New_Ref);
11701 -- Update the return type when the context of the reference
11702 -- acts as the name of a function call. Note that the update
11703 -- should not be performed when the reference appears as an
11704 -- actual in the call.
11706 if Nkind (Context) = N_Function_Call
11707 and then Name (Context) = Old_Ref
11708 then
11709 Set_Etype (Context, Etype (Val));
11710 end if;
11711 end if;
11712 end if;
11714 -- Reanalyze the reference due to potential replacements
11716 if Nkind (Old_Ref) in N_Has_Etype then
11717 Set_Analyzed (Old_Ref, False);
11718 end if;
11720 return Result;
11721 end Replace_Ref;
11723 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11725 --------------------
11726 -- Type_Of_Formal --
11727 --------------------
11729 function Type_Of_Formal
11730 (Call : Node_Id;
11731 Actual : Node_Id) return Entity_Id
11733 A : Node_Id;
11734 F : Entity_Id;
11736 begin
11737 -- Examine the list of actual and formal parameters in parallel
11739 A := First (Parameter_Associations (Call));
11740 F := First_Formal (Entity (Name (Call)));
11741 while Present (A) and then Present (F) loop
11742 if A = Actual then
11743 return Etype (F);
11744 end if;
11746 Next (A);
11747 Next_Formal (F);
11748 end loop;
11750 -- The actual parameter must always have a corresponding formal
11752 pragma Assert (False);
11754 return Empty;
11755 end Type_Of_Formal;
11757 -- Start of processing for Replace_References
11759 begin
11760 -- Map the attributes of the parent type to the proper corresponding
11761 -- attributes of the derived type.
11763 Map_Types
11764 (Parent_Type => Par_Typ,
11765 Derived_Type => Deriv_Typ);
11767 -- Inspect the input expression and perform substitutions where
11768 -- necessary.
11770 Replace_Refs (Expr);
11771 end Replace_References;
11773 -----------------------------
11774 -- Replace_Type_References --
11775 -----------------------------
11777 procedure Replace_Type_References
11778 (Expr : Node_Id;
11779 Typ : Entity_Id;
11780 Obj_Id : Entity_Id)
11782 procedure Replace_Type_Ref (N : Node_Id);
11783 -- Substitute a single reference of the current instance of type Typ
11784 -- with a reference to Obj_Id.
11786 ----------------------
11787 -- Replace_Type_Ref --
11788 ----------------------
11790 procedure Replace_Type_Ref (N : Node_Id) is
11791 begin
11792 -- Decorate the reference to Typ even though it may be rewritten
11793 -- further down. This is done for two reasons:
11795 -- * ASIS has all necessary semantic information in the original
11796 -- tree.
11798 -- * Routines which examine properties of the Original_Node have
11799 -- some semantic information.
11801 if Nkind (N) = N_Identifier then
11802 Set_Entity (N, Typ);
11803 Set_Etype (N, Typ);
11805 elsif Nkind (N) = N_Selected_Component then
11806 Analyze (Prefix (N));
11807 Set_Entity (Selector_Name (N), Typ);
11808 Set_Etype (Selector_Name (N), Typ);
11809 end if;
11811 -- Perform the following substitution:
11813 -- Typ --> _object
11815 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
11816 Set_Comes_From_Source (N, True);
11817 end Replace_Type_Ref;
11819 procedure Replace_Type_Refs is
11820 new Replace_Type_References_Generic (Replace_Type_Ref);
11822 -- Start of processing for Replace_Type_References
11824 begin
11825 Replace_Type_Refs (Expr, Typ);
11826 end Replace_Type_References;
11828 ---------------------------
11829 -- Represented_As_Scalar --
11830 ---------------------------
11832 function Represented_As_Scalar (T : Entity_Id) return Boolean is
11833 UT : constant Entity_Id := Underlying_Type (T);
11834 begin
11835 return Is_Scalar_Type (UT)
11836 or else (Is_Bit_Packed_Array (UT)
11837 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
11838 end Represented_As_Scalar;
11840 ------------------------------
11841 -- Requires_Cleanup_Actions --
11842 ------------------------------
11844 function Requires_Cleanup_Actions
11845 (N : Node_Id;
11846 Lib_Level : Boolean) return Boolean
11848 At_Lib_Level : constant Boolean :=
11849 Lib_Level
11850 and then Nkind_In (N, N_Package_Body,
11851 N_Package_Specification);
11852 -- N is at the library level if the top-most context is a package and
11853 -- the path taken to reach N does not inlcude non-package constructs.
11855 begin
11856 case Nkind (N) is
11857 when N_Accept_Statement
11858 | N_Block_Statement
11859 | N_Entry_Body
11860 | N_Package_Body
11861 | N_Protected_Body
11862 | N_Subprogram_Body
11863 | N_Task_Body
11865 return
11866 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
11867 or else
11868 (Present (Handled_Statement_Sequence (N))
11869 and then
11870 Requires_Cleanup_Actions
11871 (Statements (Handled_Statement_Sequence (N)),
11872 At_Lib_Level, True));
11874 when N_Package_Specification =>
11875 return
11876 Requires_Cleanup_Actions
11877 (Visible_Declarations (N), At_Lib_Level, True)
11878 or else
11879 Requires_Cleanup_Actions
11880 (Private_Declarations (N), At_Lib_Level, True);
11882 when others =>
11883 return False;
11884 end case;
11885 end Requires_Cleanup_Actions;
11887 ------------------------------
11888 -- Requires_Cleanup_Actions --
11889 ------------------------------
11891 function Requires_Cleanup_Actions
11892 (L : List_Id;
11893 Lib_Level : Boolean;
11894 Nested_Constructs : Boolean) return Boolean
11896 Decl : Node_Id;
11897 Expr : Node_Id;
11898 Obj_Id : Entity_Id;
11899 Obj_Typ : Entity_Id;
11900 Pack_Id : Entity_Id;
11901 Typ : Entity_Id;
11903 begin
11904 if No (L)
11905 or else Is_Empty_List (L)
11906 then
11907 return False;
11908 end if;
11910 Decl := First (L);
11911 while Present (Decl) loop
11913 -- Library-level tagged types
11915 if Nkind (Decl) = N_Full_Type_Declaration then
11916 Typ := Defining_Identifier (Decl);
11918 -- Ignored Ghost types do not need any cleanup actions because
11919 -- they will not appear in the final tree.
11921 if Is_Ignored_Ghost_Entity (Typ) then
11922 null;
11924 elsif Is_Tagged_Type (Typ)
11925 and then Is_Library_Level_Entity (Typ)
11926 and then Convention (Typ) = Convention_Ada
11927 and then Present (Access_Disp_Table (Typ))
11928 and then RTE_Available (RE_Unregister_Tag)
11929 and then not Is_Abstract_Type (Typ)
11930 and then not No_Run_Time_Mode
11931 then
11932 return True;
11933 end if;
11935 -- Regular object declarations
11937 elsif Nkind (Decl) = N_Object_Declaration then
11938 Obj_Id := Defining_Identifier (Decl);
11939 Obj_Typ := Base_Type (Etype (Obj_Id));
11940 Expr := Expression (Decl);
11942 -- Bypass any form of processing for objects which have their
11943 -- finalization disabled. This applies only to objects at the
11944 -- library level.
11946 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
11947 null;
11949 -- Finalization of transient objects are treated separately in
11950 -- order to handle sensitive cases. These include:
11952 -- * Aggregate expansion
11953 -- * If, case, and expression with actions expansion
11954 -- * Transient scopes
11956 -- If one of those contexts has marked the transient object as
11957 -- ignored, do not generate finalization actions for it.
11959 elsif Is_Finalized_Transient (Obj_Id)
11960 or else Is_Ignored_Transient (Obj_Id)
11961 then
11962 null;
11964 -- Ignored Ghost objects do not need any cleanup actions because
11965 -- they will not appear in the final tree.
11967 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
11968 null;
11970 -- The object is of the form:
11971 -- Obj : [constant] Typ [:= Expr];
11973 -- Do not process tag-to-class-wide conversions because they do
11974 -- not yield an object. Do not process the incomplete view of a
11975 -- deferred constant. Note that an object initialized by means
11976 -- of a build-in-place function call may appear as a deferred
11977 -- constant after expansion activities. These kinds of objects
11978 -- must be finalized.
11980 elsif not Is_Imported (Obj_Id)
11981 and then Needs_Finalization (Obj_Typ)
11982 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
11983 and then not (Ekind (Obj_Id) = E_Constant
11984 and then not Has_Completion (Obj_Id)
11985 and then No (BIP_Initialization_Call (Obj_Id)))
11986 then
11987 return True;
11989 -- The object is of the form:
11990 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
11992 -- Obj : Access_Typ :=
11993 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
11995 elsif Is_Access_Type (Obj_Typ)
11996 and then Needs_Finalization
11997 (Available_View (Designated_Type (Obj_Typ)))
11998 and then Present (Expr)
11999 and then
12000 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12001 or else
12002 (Is_Non_BIP_Func_Call (Expr)
12003 and then not Is_Related_To_Func_Return (Obj_Id)))
12004 then
12005 return True;
12007 -- Processing for "hook" objects generated for transient objects
12008 -- declared inside an Expression_With_Actions.
12010 elsif Is_Access_Type (Obj_Typ)
12011 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12012 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12013 N_Object_Declaration
12014 then
12015 return True;
12017 -- Processing for intermediate results of if expressions where
12018 -- one of the alternatives uses a controlled function call.
12020 elsif Is_Access_Type (Obj_Typ)
12021 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12022 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12023 N_Defining_Identifier
12024 and then Present (Expr)
12025 and then Nkind (Expr) = N_Null
12026 then
12027 return True;
12029 -- Simple protected objects which use type System.Tasking.
12030 -- Protected_Objects.Protection to manage their locks should be
12031 -- treated as controlled since they require manual cleanup.
12033 elsif Ekind (Obj_Id) = E_Variable
12034 and then (Is_Simple_Protected_Type (Obj_Typ)
12035 or else Has_Simple_Protected_Object (Obj_Typ))
12036 then
12037 return True;
12038 end if;
12040 -- Specific cases of object renamings
12042 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12043 Obj_Id := Defining_Identifier (Decl);
12044 Obj_Typ := Base_Type (Etype (Obj_Id));
12046 -- Bypass any form of processing for objects which have their
12047 -- finalization disabled. This applies only to objects at the
12048 -- library level.
12050 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12051 null;
12053 -- Ignored Ghost object renamings do not need any cleanup actions
12054 -- because they will not appear in the final tree.
12056 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12057 null;
12059 -- Return object of a build-in-place function. This case is
12060 -- recognized and marked by the expansion of an extended return
12061 -- statement (see Expand_N_Extended_Return_Statement).
12063 elsif Needs_Finalization (Obj_Typ)
12064 and then Is_Return_Object (Obj_Id)
12065 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12066 then
12067 return True;
12069 -- Detect a case where a source object has been initialized by
12070 -- a controlled function call or another object which was later
12071 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12073 -- Obj1 : CW_Type := Src_Obj;
12074 -- Obj2 : CW_Type := Function_Call (...);
12076 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12077 -- Tmp : ... := Function_Call (...)'reference;
12078 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12080 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12081 return True;
12082 end if;
12084 -- Inspect the freeze node of an access-to-controlled type and look
12085 -- for a delayed finalization master. This case arises when the
12086 -- freeze actions are inserted at a later time than the expansion of
12087 -- the context. Since Build_Finalizer is never called on a single
12088 -- construct twice, the master will be ultimately left out and never
12089 -- finalized. This is also needed for freeze actions of designated
12090 -- types themselves, since in some cases the finalization master is
12091 -- associated with a designated type's freeze node rather than that
12092 -- of the access type (see handling for freeze actions in
12093 -- Build_Finalization_Master).
12095 elsif Nkind (Decl) = N_Freeze_Entity
12096 and then Present (Actions (Decl))
12097 then
12098 Typ := Entity (Decl);
12100 -- Freeze nodes for ignored Ghost types do not need cleanup
12101 -- actions because they will never appear in the final tree.
12103 if Is_Ignored_Ghost_Entity (Typ) then
12104 null;
12106 elsif ((Is_Access_Type (Typ)
12107 and then not Is_Access_Subprogram_Type (Typ)
12108 and then Needs_Finalization
12109 (Available_View (Designated_Type (Typ))))
12110 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12111 and then Requires_Cleanup_Actions
12112 (Actions (Decl), Lib_Level, Nested_Constructs)
12113 then
12114 return True;
12115 end if;
12117 -- Nested package declarations
12119 elsif Nested_Constructs
12120 and then Nkind (Decl) = N_Package_Declaration
12121 then
12122 Pack_Id := Defining_Entity (Decl);
12124 -- Do not inspect an ignored Ghost package because all code found
12125 -- within will not appear in the final tree.
12127 if Is_Ignored_Ghost_Entity (Pack_Id) then
12128 null;
12130 elsif Ekind (Pack_Id) /= E_Generic_Package
12131 and then Requires_Cleanup_Actions
12132 (Specification (Decl), Lib_Level)
12133 then
12134 return True;
12135 end if;
12137 -- Nested package bodies
12139 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12141 -- Do not inspect an ignored Ghost package body because all code
12142 -- found within will not appear in the final tree.
12144 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12145 null;
12147 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12148 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12149 then
12150 return True;
12151 end if;
12153 elsif Nkind (Decl) = N_Block_Statement
12154 and then
12156 -- Handle a rare case caused by a controlled transient object
12157 -- created as part of a record init proc. The variable is wrapped
12158 -- in a block, but the block is not associated with a transient
12159 -- scope.
12161 (Inside_Init_Proc
12163 -- Handle the case where the original context has been wrapped in
12164 -- a block to avoid interference between exception handlers and
12165 -- At_End handlers. Treat the block as transparent and process its
12166 -- contents.
12168 or else Is_Finalization_Wrapper (Decl))
12169 then
12170 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12171 return True;
12172 end if;
12173 end if;
12175 Next (Decl);
12176 end loop;
12178 return False;
12179 end Requires_Cleanup_Actions;
12181 ------------------------------------
12182 -- Safe_Unchecked_Type_Conversion --
12183 ------------------------------------
12185 -- Note: this function knows quite a bit about the exact requirements of
12186 -- Gigi with respect to unchecked type conversions, and its code must be
12187 -- coordinated with any changes in Gigi in this area.
12189 -- The above requirements should be documented in Sinfo ???
12191 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12192 Otyp : Entity_Id;
12193 Ityp : Entity_Id;
12194 Oalign : Uint;
12195 Ialign : Uint;
12196 Pexp : constant Node_Id := Parent (Exp);
12198 begin
12199 -- If the expression is the RHS of an assignment or object declaration
12200 -- we are always OK because there will always be a target.
12202 -- Object renaming declarations, (generated for view conversions of
12203 -- actuals in inlined calls), like object declarations, provide an
12204 -- explicit type, and are safe as well.
12206 if (Nkind (Pexp) = N_Assignment_Statement
12207 and then Expression (Pexp) = Exp)
12208 or else Nkind_In (Pexp, N_Object_Declaration,
12209 N_Object_Renaming_Declaration)
12210 then
12211 return True;
12213 -- If the expression is the prefix of an N_Selected_Component we should
12214 -- also be OK because GCC knows to look inside the conversion except if
12215 -- the type is discriminated. We assume that we are OK anyway if the
12216 -- type is not set yet or if it is controlled since we can't afford to
12217 -- introduce a temporary in this case.
12219 elsif Nkind (Pexp) = N_Selected_Component
12220 and then Prefix (Pexp) = Exp
12221 then
12222 if No (Etype (Pexp)) then
12223 return True;
12224 else
12225 return
12226 not Has_Discriminants (Etype (Pexp))
12227 or else Is_Constrained (Etype (Pexp));
12228 end if;
12229 end if;
12231 -- Set the output type, this comes from Etype if it is set, otherwise we
12232 -- take it from the subtype mark, which we assume was already fully
12233 -- analyzed.
12235 if Present (Etype (Exp)) then
12236 Otyp := Etype (Exp);
12237 else
12238 Otyp := Entity (Subtype_Mark (Exp));
12239 end if;
12241 -- The input type always comes from the expression, and we assume this
12242 -- is indeed always analyzed, so we can simply get the Etype.
12244 Ityp := Etype (Expression (Exp));
12246 -- Initialize alignments to unknown so far
12248 Oalign := No_Uint;
12249 Ialign := No_Uint;
12251 -- Replace a concurrent type by its corresponding record type and each
12252 -- type by its underlying type and do the tests on those. The original
12253 -- type may be a private type whose completion is a concurrent type, so
12254 -- find the underlying type first.
12256 if Present (Underlying_Type (Otyp)) then
12257 Otyp := Underlying_Type (Otyp);
12258 end if;
12260 if Present (Underlying_Type (Ityp)) then
12261 Ityp := Underlying_Type (Ityp);
12262 end if;
12264 if Is_Concurrent_Type (Otyp) then
12265 Otyp := Corresponding_Record_Type (Otyp);
12266 end if;
12268 if Is_Concurrent_Type (Ityp) then
12269 Ityp := Corresponding_Record_Type (Ityp);
12270 end if;
12272 -- If the base types are the same, we know there is no problem since
12273 -- this conversion will be a noop.
12275 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12276 return True;
12278 -- Same if this is an upwards conversion of an untagged type, and there
12279 -- are no constraints involved (could be more general???)
12281 elsif Etype (Ityp) = Otyp
12282 and then not Is_Tagged_Type (Ityp)
12283 and then not Has_Discriminants (Ityp)
12284 and then No (First_Rep_Item (Base_Type (Ityp)))
12285 then
12286 return True;
12288 -- If the expression has an access type (object or subprogram) we assume
12289 -- that the conversion is safe, because the size of the target is safe,
12290 -- even if it is a record (which might be treated as having unknown size
12291 -- at this point).
12293 elsif Is_Access_Type (Ityp) then
12294 return True;
12296 -- If the size of output type is known at compile time, there is never
12297 -- a problem. Note that unconstrained records are considered to be of
12298 -- known size, but we can't consider them that way here, because we are
12299 -- talking about the actual size of the object.
12301 -- We also make sure that in addition to the size being known, we do not
12302 -- have a case which might generate an embarrassingly large temp in
12303 -- stack checking mode.
12305 elsif Size_Known_At_Compile_Time (Otyp)
12306 and then
12307 (not Stack_Checking_Enabled
12308 or else not May_Generate_Large_Temp (Otyp))
12309 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12310 then
12311 return True;
12313 -- If either type is tagged, then we know the alignment is OK so Gigi
12314 -- will be able to use pointer punning.
12316 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12317 return True;
12319 -- If either type is a limited record type, we cannot do a copy, so say
12320 -- safe since there's nothing else we can do.
12322 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12323 return True;
12325 -- Conversions to and from packed array types are always ignored and
12326 -- hence are safe.
12328 elsif Is_Packed_Array_Impl_Type (Otyp)
12329 or else Is_Packed_Array_Impl_Type (Ityp)
12330 then
12331 return True;
12332 end if;
12334 -- The only other cases known to be safe is if the input type's
12335 -- alignment is known to be at least the maximum alignment for the
12336 -- target or if both alignments are known and the output type's
12337 -- alignment is no stricter than the input's. We can use the component
12338 -- type alignment for an array if a type is an unpacked array type.
12340 if Present (Alignment_Clause (Otyp)) then
12341 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12343 elsif Is_Array_Type (Otyp)
12344 and then Present (Alignment_Clause (Component_Type (Otyp)))
12345 then
12346 Oalign := Expr_Value (Expression (Alignment_Clause
12347 (Component_Type (Otyp))));
12348 end if;
12350 if Present (Alignment_Clause (Ityp)) then
12351 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12353 elsif Is_Array_Type (Ityp)
12354 and then Present (Alignment_Clause (Component_Type (Ityp)))
12355 then
12356 Ialign := Expr_Value (Expression (Alignment_Clause
12357 (Component_Type (Ityp))));
12358 end if;
12360 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12361 return True;
12363 elsif Ialign /= No_Uint
12364 and then Oalign /= No_Uint
12365 and then Ialign <= Oalign
12366 then
12367 return True;
12369 -- Otherwise, Gigi cannot handle this and we must make a temporary
12371 else
12372 return False;
12373 end if;
12374 end Safe_Unchecked_Type_Conversion;
12376 ---------------------------------
12377 -- Set_Current_Value_Condition --
12378 ---------------------------------
12380 -- Note: the implementation of this procedure is very closely tied to the
12381 -- implementation of Get_Current_Value_Condition. Here we set required
12382 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12383 -- them, so they must have a consistent view.
12385 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12387 procedure Set_Entity_Current_Value (N : Node_Id);
12388 -- If N is an entity reference, where the entity is of an appropriate
12389 -- kind, then set the current value of this entity to Cnode, unless
12390 -- there is already a definite value set there.
12392 procedure Set_Expression_Current_Value (N : Node_Id);
12393 -- If N is of an appropriate form, sets an appropriate entry in current
12394 -- value fields of relevant entities. Multiple entities can be affected
12395 -- in the case of an AND or AND THEN.
12397 ------------------------------
12398 -- Set_Entity_Current_Value --
12399 ------------------------------
12401 procedure Set_Entity_Current_Value (N : Node_Id) is
12402 begin
12403 if Is_Entity_Name (N) then
12404 declare
12405 Ent : constant Entity_Id := Entity (N);
12407 begin
12408 -- Don't capture if not safe to do so
12410 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12411 return;
12412 end if;
12414 -- Here we have a case where the Current_Value field may need
12415 -- to be set. We set it if it is not already set to a compile
12416 -- time expression value.
12418 -- Note that this represents a decision that one condition
12419 -- blots out another previous one. That's certainly right if
12420 -- they occur at the same level. If the second one is nested,
12421 -- then the decision is neither right nor wrong (it would be
12422 -- equally OK to leave the outer one in place, or take the new
12423 -- inner one. Really we should record both, but our data
12424 -- structures are not that elaborate.
12426 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12427 Set_Current_Value (Ent, Cnode);
12428 end if;
12429 end;
12430 end if;
12431 end Set_Entity_Current_Value;
12433 ----------------------------------
12434 -- Set_Expression_Current_Value --
12435 ----------------------------------
12437 procedure Set_Expression_Current_Value (N : Node_Id) is
12438 Cond : Node_Id;
12440 begin
12441 Cond := N;
12443 -- Loop to deal with (ignore for now) any NOT operators present. The
12444 -- presence of NOT operators will be handled properly when we call
12445 -- Get_Current_Value_Condition.
12447 while Nkind (Cond) = N_Op_Not loop
12448 Cond := Right_Opnd (Cond);
12449 end loop;
12451 -- For an AND or AND THEN, recursively process operands
12453 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12454 Set_Expression_Current_Value (Left_Opnd (Cond));
12455 Set_Expression_Current_Value (Right_Opnd (Cond));
12456 return;
12457 end if;
12459 -- Check possible relational operator
12461 if Nkind (Cond) in N_Op_Compare then
12462 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12463 Set_Entity_Current_Value (Left_Opnd (Cond));
12464 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12465 Set_Entity_Current_Value (Right_Opnd (Cond));
12466 end if;
12468 elsif Nkind_In (Cond,
12469 N_Type_Conversion,
12470 N_Qualified_Expression,
12471 N_Expression_With_Actions)
12472 then
12473 Set_Expression_Current_Value (Expression (Cond));
12475 -- Check possible boolean variable reference
12477 else
12478 Set_Entity_Current_Value (Cond);
12479 end if;
12480 end Set_Expression_Current_Value;
12482 -- Start of processing for Set_Current_Value_Condition
12484 begin
12485 Set_Expression_Current_Value (Condition (Cnode));
12486 end Set_Current_Value_Condition;
12488 --------------------------
12489 -- Set_Elaboration_Flag --
12490 --------------------------
12492 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12493 Loc : constant Source_Ptr := Sloc (N);
12494 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12495 Asn : Node_Id;
12497 begin
12498 if Present (Ent) then
12500 -- Nothing to do if at the compilation unit level, because in this
12501 -- case the flag is set by the binder generated elaboration routine.
12503 if Nkind (Parent (N)) = N_Compilation_Unit then
12504 null;
12506 -- Here we do need to generate an assignment statement
12508 else
12509 Check_Restriction (No_Elaboration_Code, N);
12510 Asn :=
12511 Make_Assignment_Statement (Loc,
12512 Name => New_Occurrence_Of (Ent, Loc),
12513 Expression => Make_Integer_Literal (Loc, Uint_1));
12515 if Nkind (Parent (N)) = N_Subunit then
12516 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12517 else
12518 Insert_After (N, Asn);
12519 end if;
12521 Analyze (Asn);
12523 -- Kill current value indication. This is necessary because the
12524 -- tests of this flag are inserted out of sequence and must not
12525 -- pick up bogus indications of the wrong constant value.
12527 Set_Current_Value (Ent, Empty);
12529 -- If the subprogram is in the current declarative part and
12530 -- 'access has been applied to it, generate an elaboration
12531 -- check at the beginning of the declarations of the body.
12533 if Nkind (N) = N_Subprogram_Body
12534 and then Address_Taken (Spec_Id)
12535 and then
12536 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12537 then
12538 declare
12539 Loc : constant Source_Ptr := Sloc (N);
12540 Decls : constant List_Id := Declarations (N);
12541 Chk : Node_Id;
12543 begin
12544 -- No need to generate this check if first entry in the
12545 -- declaration list is a raise of Program_Error now.
12547 if Present (Decls)
12548 and then Nkind (First (Decls)) = N_Raise_Program_Error
12549 then
12550 return;
12551 end if;
12553 -- Otherwise generate the check
12555 Chk :=
12556 Make_Raise_Program_Error (Loc,
12557 Condition =>
12558 Make_Op_Eq (Loc,
12559 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12560 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12561 Reason => PE_Access_Before_Elaboration);
12563 if No (Decls) then
12564 Set_Declarations (N, New_List (Chk));
12565 else
12566 Prepend (Chk, Decls);
12567 end if;
12569 Analyze (Chk);
12570 end;
12571 end if;
12572 end if;
12573 end if;
12574 end Set_Elaboration_Flag;
12576 ----------------------------
12577 -- Set_Renamed_Subprogram --
12578 ----------------------------
12580 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12581 begin
12582 -- If input node is an identifier, we can just reset it
12584 if Nkind (N) = N_Identifier then
12585 Set_Chars (N, Chars (E));
12586 Set_Entity (N, E);
12588 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12590 else
12591 declare
12592 CS : constant Boolean := Comes_From_Source (N);
12593 begin
12594 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12595 Set_Entity (N, E);
12596 Set_Comes_From_Source (N, CS);
12597 Set_Analyzed (N, True);
12598 end;
12599 end if;
12600 end Set_Renamed_Subprogram;
12602 ----------------------
12603 -- Side_Effect_Free --
12604 ----------------------
12606 function Side_Effect_Free
12607 (N : Node_Id;
12608 Name_Req : Boolean := False;
12609 Variable_Ref : Boolean := False) return Boolean
12611 Typ : constant Entity_Id := Etype (N);
12612 -- Result type of the expression
12614 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12615 -- The argument N is a construct where the Prefix is dereferenced if it
12616 -- is an access type and the result is a variable. The call returns True
12617 -- if the construct is side effect free (not considering side effects in
12618 -- other than the prefix which are to be tested by the caller).
12620 function Within_In_Parameter (N : Node_Id) return Boolean;
12621 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12622 -- N is not side-effect free when the actual is global and modifiable
12623 -- indirectly from within a subprogram, because it may be passed by
12624 -- reference. The front-end must be conservative here and assume that
12625 -- this may happen with any array or record type. On the other hand, we
12626 -- cannot create temporaries for all expressions for which this
12627 -- condition is true, for various reasons that might require clearing up
12628 -- ??? For example, discriminant references that appear out of place, or
12629 -- spurious type errors with class-wide expressions. As a result, we
12630 -- limit the transformation to loop bounds, which is so far the only
12631 -- case that requires it.
12633 -----------------------------
12634 -- Safe_Prefixed_Reference --
12635 -----------------------------
12637 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12638 begin
12639 -- If prefix is not side effect free, definitely not safe
12641 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12642 return False;
12644 -- If the prefix is of an access type that is not access-to-constant,
12645 -- then this construct is a variable reference, which means it is to
12646 -- be considered to have side effects if Variable_Ref is set True.
12648 elsif Is_Access_Type (Etype (Prefix (N)))
12649 and then not Is_Access_Constant (Etype (Prefix (N)))
12650 and then Variable_Ref
12651 then
12652 -- Exception is a prefix that is the result of a previous removal
12653 -- of side-effects.
12655 return Is_Entity_Name (Prefix (N))
12656 and then not Comes_From_Source (Prefix (N))
12657 and then Ekind (Entity (Prefix (N))) = E_Constant
12658 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12660 -- If the prefix is an explicit dereference then this construct is a
12661 -- variable reference, which means it is to be considered to have
12662 -- side effects if Variable_Ref is True.
12664 -- We do NOT exclude dereferences of access-to-constant types because
12665 -- we handle them as constant view of variables.
12667 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12668 and then Variable_Ref
12669 then
12670 return False;
12672 -- Note: The following test is the simplest way of solving a complex
12673 -- problem uncovered by the following test (Side effect on loop bound
12674 -- that is a subcomponent of a global variable:
12676 -- with Text_Io; use Text_Io;
12677 -- procedure Tloop is
12678 -- type X is
12679 -- record
12680 -- V : Natural := 4;
12681 -- S : String (1..5) := (others => 'a');
12682 -- end record;
12683 -- X1 : X;
12685 -- procedure Modi;
12687 -- generic
12688 -- with procedure Action;
12689 -- procedure Loop_G (Arg : X; Msg : String)
12691 -- procedure Loop_G (Arg : X; Msg : String) is
12692 -- begin
12693 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12694 -- & Natural'Image (Arg.V));
12695 -- for Index in 1 .. Arg.V loop
12696 -- Text_Io.Put_Line
12697 -- (Natural'Image (Index) & " " & Arg.S (Index));
12698 -- if Index > 2 then
12699 -- Modi;
12700 -- end if;
12701 -- end loop;
12702 -- Put_Line ("end loop_g " & Msg);
12703 -- end;
12705 -- procedure Loop1 is new Loop_G (Modi);
12706 -- procedure Modi is
12707 -- begin
12708 -- X1.V := 1;
12709 -- Loop1 (X1, "from modi");
12710 -- end;
12712 -- begin
12713 -- Loop1 (X1, "initial");
12714 -- end;
12716 -- The output of the above program should be:
12718 -- begin loop_g initial will loop till: 4
12719 -- 1 a
12720 -- 2 a
12721 -- 3 a
12722 -- begin loop_g from modi will loop till: 1
12723 -- 1 a
12724 -- end loop_g from modi
12725 -- 4 a
12726 -- begin loop_g from modi will loop till: 1
12727 -- 1 a
12728 -- end loop_g from modi
12729 -- end loop_g initial
12731 -- If a loop bound is a subcomponent of a global variable, a
12732 -- modification of that variable within the loop may incorrectly
12733 -- affect the execution of the loop.
12735 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12736 and then Within_In_Parameter (Prefix (N))
12737 and then Variable_Ref
12738 then
12739 return False;
12741 -- All other cases are side effect free
12743 else
12744 return True;
12745 end if;
12746 end Safe_Prefixed_Reference;
12748 -------------------------
12749 -- Within_In_Parameter --
12750 -------------------------
12752 function Within_In_Parameter (N : Node_Id) return Boolean is
12753 begin
12754 if not Comes_From_Source (N) then
12755 return False;
12757 elsif Is_Entity_Name (N) then
12758 return Ekind (Entity (N)) = E_In_Parameter;
12760 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12761 return Within_In_Parameter (Prefix (N));
12763 else
12764 return False;
12765 end if;
12766 end Within_In_Parameter;
12768 -- Start of processing for Side_Effect_Free
12770 begin
12771 -- If volatile reference, always consider it to have side effects
12773 if Is_Volatile_Reference (N) then
12774 return False;
12775 end if;
12777 -- Note on checks that could raise Constraint_Error. Strictly, if we
12778 -- take advantage of 11.6, these checks do not count as side effects.
12779 -- However, we would prefer to consider that they are side effects,
12780 -- since the back end CSE does not work very well on expressions which
12781 -- can raise Constraint_Error. On the other hand if we don't consider
12782 -- them to be side effect free, then we get some awkward expansions
12783 -- in -gnato mode, resulting in code insertions at a point where we
12784 -- do not have a clear model for performing the insertions.
12786 -- Special handling for entity names
12788 if Is_Entity_Name (N) then
12790 -- A type reference is always side effect free
12792 if Is_Type (Entity (N)) then
12793 return True;
12795 -- Variables are considered to be a side effect if Variable_Ref
12796 -- is set or if we have a volatile reference and Name_Req is off.
12797 -- If Name_Req is True then we can't help returning a name which
12798 -- effectively allows multiple references in any case.
12800 elsif Is_Variable (N, Use_Original_Node => False) then
12801 return not Variable_Ref
12802 and then (not Is_Volatile_Reference (N) or else Name_Req);
12804 -- Any other entity (e.g. a subtype name) is definitely side
12805 -- effect free.
12807 else
12808 return True;
12809 end if;
12811 -- A value known at compile time is always side effect free
12813 elsif Compile_Time_Known_Value (N) then
12814 return True;
12816 -- A variable renaming is not side-effect free, because the renaming
12817 -- will function like a macro in the front-end in some cases, and an
12818 -- assignment can modify the component designated by N, so we need to
12819 -- create a temporary for it.
12821 -- The guard testing for Entity being present is needed at least in
12822 -- the case of rewritten predicate expressions, and may well also be
12823 -- appropriate elsewhere. Obviously we can't go testing the entity
12824 -- field if it does not exist, so it's reasonable to say that this is
12825 -- not the renaming case if it does not exist.
12827 elsif Is_Entity_Name (Original_Node (N))
12828 and then Present (Entity (Original_Node (N)))
12829 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
12830 and then Ekind (Entity (Original_Node (N))) /= E_Constant
12831 then
12832 declare
12833 RO : constant Node_Id :=
12834 Renamed_Object (Entity (Original_Node (N)));
12836 begin
12837 -- If the renamed object is an indexed component, or an
12838 -- explicit dereference, then the designated object could
12839 -- be modified by an assignment.
12841 if Nkind_In (RO, N_Indexed_Component,
12842 N_Explicit_Dereference)
12843 then
12844 return False;
12846 -- A selected component must have a safe prefix
12848 elsif Nkind (RO) = N_Selected_Component then
12849 return Safe_Prefixed_Reference (RO);
12851 -- In all other cases, designated object cannot be changed so
12852 -- we are side effect free.
12854 else
12855 return True;
12856 end if;
12857 end;
12859 -- Remove_Side_Effects generates an object renaming declaration to
12860 -- capture the expression of a class-wide expression. In VM targets
12861 -- the frontend performs no expansion for dispatching calls to
12862 -- class- wide types since they are handled by the VM. Hence, we must
12863 -- locate here if this node corresponds to a previous invocation of
12864 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
12866 elsif not Tagged_Type_Expansion
12867 and then not Comes_From_Source (N)
12868 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
12869 and then Is_Class_Wide_Type (Typ)
12870 then
12871 return True;
12873 -- Generating C the type conversion of an access to constrained array
12874 -- type into an access to unconstrained array type involves initializing
12875 -- a fat pointer and the expression cannot be assumed to be free of side
12876 -- effects since it must referenced several times to compute its bounds.
12878 elsif Modify_Tree_For_C
12879 and then Nkind (N) = N_Type_Conversion
12880 and then Is_Access_Type (Typ)
12881 and then Is_Array_Type (Designated_Type (Typ))
12882 and then not Is_Constrained (Designated_Type (Typ))
12883 then
12884 return False;
12885 end if;
12887 -- For other than entity names and compile time known values,
12888 -- check the node kind for special processing.
12890 case Nkind (N) is
12892 -- An attribute reference is side effect free if its expressions
12893 -- are side effect free and its prefix is side effect free or
12894 -- is an entity reference.
12896 -- Is this right? what about x'first where x is a variable???
12898 when N_Attribute_Reference =>
12899 Attribute_Reference : declare
12901 function Side_Effect_Free_Attribute
12902 (Attribute_Name : Name_Id) return Boolean;
12903 -- Returns True if evaluation of the given attribute is
12904 -- considered side-effect free (independent of prefix and
12905 -- arguments).
12907 --------------------------------
12908 -- Side_Effect_Free_Attribute --
12909 --------------------------------
12911 function Side_Effect_Free_Attribute
12912 (Attribute_Name : Name_Id) return Boolean
12914 begin
12915 case Attribute_Name is
12916 when Name_Input =>
12917 return False;
12919 when Name_Image
12920 | Name_Img
12921 | Name_Wide_Image
12922 | Name_Wide_Wide_Image
12924 -- CodePeer doesn't want to see replicated copies of
12925 -- 'Image calls.
12927 return not CodePeer_Mode;
12929 when others =>
12930 return True;
12931 end case;
12932 end Side_Effect_Free_Attribute;
12934 -- Start of processing for Attribute_Reference
12936 begin
12937 return
12938 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12939 and then Side_Effect_Free_Attribute (Attribute_Name (N))
12940 and then (Is_Entity_Name (Prefix (N))
12941 or else Side_Effect_Free
12942 (Prefix (N), Name_Req, Variable_Ref));
12943 end Attribute_Reference;
12945 -- A binary operator is side effect free if and both operands are
12946 -- side effect free. For this purpose binary operators include
12947 -- membership tests and short circuit forms.
12949 when N_Binary_Op
12950 | N_Membership_Test
12951 | N_Short_Circuit
12953 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
12954 and then
12955 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
12957 -- An explicit dereference is side effect free only if it is
12958 -- a side effect free prefixed reference.
12960 when N_Explicit_Dereference =>
12961 return Safe_Prefixed_Reference (N);
12963 -- An expression with action is side effect free if its expression
12964 -- is side effect free and it has no actions.
12966 when N_Expression_With_Actions =>
12967 return
12968 Is_Empty_List (Actions (N))
12969 and then Side_Effect_Free
12970 (Expression (N), Name_Req, Variable_Ref);
12972 -- A call to _rep_to_pos is side effect free, since we generate
12973 -- this pure function call ourselves. Moreover it is critically
12974 -- important to make this exception, since otherwise we can have
12975 -- discriminants in array components which don't look side effect
12976 -- free in the case of an array whose index type is an enumeration
12977 -- type with an enumeration rep clause.
12979 -- All other function calls are not side effect free
12981 when N_Function_Call =>
12982 return
12983 Nkind (Name (N)) = N_Identifier
12984 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
12985 and then Side_Effect_Free
12986 (First (Parameter_Associations (N)),
12987 Name_Req, Variable_Ref);
12989 -- An IF expression is side effect free if it's of a scalar type, and
12990 -- all its components are all side effect free (conditions and then
12991 -- actions and else actions). We restrict to scalar types, since it
12992 -- is annoying to deal with things like (if A then B else C)'First
12993 -- where the type involved is a string type.
12995 when N_If_Expression =>
12996 return
12997 Is_Scalar_Type (Typ)
12998 and then Side_Effect_Free
12999 (Expressions (N), Name_Req, Variable_Ref);
13001 -- An indexed component is side effect free if it is a side
13002 -- effect free prefixed reference and all the indexing
13003 -- expressions are side effect free.
13005 when N_Indexed_Component =>
13006 return
13007 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13008 and then Safe_Prefixed_Reference (N);
13010 -- A type qualification, type conversion, or unchecked expression is
13011 -- side effect free if the expression is side effect free.
13013 when N_Qualified_Expression
13014 | N_Type_Conversion
13015 | N_Unchecked_Expression
13017 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13019 -- A selected component is side effect free only if it is a side
13020 -- effect free prefixed reference.
13022 when N_Selected_Component =>
13023 return Safe_Prefixed_Reference (N);
13025 -- A range is side effect free if the bounds are side effect free
13027 when N_Range =>
13028 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13029 and then
13030 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13032 -- A slice is side effect free if it is a side effect free
13033 -- prefixed reference and the bounds are side effect free.
13035 when N_Slice =>
13036 return
13037 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13038 and then Safe_Prefixed_Reference (N);
13040 -- A unary operator is side effect free if the operand
13041 -- is side effect free.
13043 when N_Unary_Op =>
13044 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13046 -- An unchecked type conversion is side effect free only if it
13047 -- is safe and its argument is side effect free.
13049 when N_Unchecked_Type_Conversion =>
13050 return
13051 Safe_Unchecked_Type_Conversion (N)
13052 and then Side_Effect_Free
13053 (Expression (N), Name_Req, Variable_Ref);
13055 -- A literal is side effect free
13057 when N_Character_Literal
13058 | N_Integer_Literal
13059 | N_Real_Literal
13060 | N_String_Literal
13062 return True;
13064 -- We consider that anything else has side effects. This is a bit
13065 -- crude, but we are pretty close for most common cases, and we
13066 -- are certainly correct (i.e. we never return True when the
13067 -- answer should be False).
13069 when others =>
13070 return False;
13071 end case;
13072 end Side_Effect_Free;
13074 -- A list is side effect free if all elements of the list are side
13075 -- effect free.
13077 function Side_Effect_Free
13078 (L : List_Id;
13079 Name_Req : Boolean := False;
13080 Variable_Ref : Boolean := False) return Boolean
13082 N : Node_Id;
13084 begin
13085 if L = No_List or else L = Error_List then
13086 return True;
13088 else
13089 N := First (L);
13090 while Present (N) loop
13091 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13092 return False;
13093 else
13094 Next (N);
13095 end if;
13096 end loop;
13098 return True;
13099 end if;
13100 end Side_Effect_Free;
13102 ----------------------------------
13103 -- Silly_Boolean_Array_Not_Test --
13104 ----------------------------------
13106 -- This procedure implements an odd and silly test. We explicitly check
13107 -- for the case where the 'First of the component type is equal to the
13108 -- 'Last of this component type, and if this is the case, we make sure
13109 -- that constraint error is raised. The reason is that the NOT is bound
13110 -- to cause CE in this case, and we will not otherwise catch it.
13112 -- No such check is required for AND and OR, since for both these cases
13113 -- False op False = False, and True op True = True. For the XOR case,
13114 -- see Silly_Boolean_Array_Xor_Test.
13116 -- Believe it or not, this was reported as a bug. Note that nearly always,
13117 -- the test will evaluate statically to False, so the code will be
13118 -- statically removed, and no extra overhead caused.
13120 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13121 Loc : constant Source_Ptr := Sloc (N);
13122 CT : constant Entity_Id := Component_Type (T);
13124 begin
13125 -- The check we install is
13127 -- constraint_error when
13128 -- component_type'first = component_type'last
13129 -- and then array_type'Length /= 0)
13131 -- We need the last guard because we don't want to raise CE for empty
13132 -- arrays since no out of range values result. (Empty arrays with a
13133 -- component type of True .. True -- very useful -- even the ACATS
13134 -- does not test that marginal case).
13136 Insert_Action (N,
13137 Make_Raise_Constraint_Error (Loc,
13138 Condition =>
13139 Make_And_Then (Loc,
13140 Left_Opnd =>
13141 Make_Op_Eq (Loc,
13142 Left_Opnd =>
13143 Make_Attribute_Reference (Loc,
13144 Prefix => New_Occurrence_Of (CT, Loc),
13145 Attribute_Name => Name_First),
13147 Right_Opnd =>
13148 Make_Attribute_Reference (Loc,
13149 Prefix => New_Occurrence_Of (CT, Loc),
13150 Attribute_Name => Name_Last)),
13152 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13153 Reason => CE_Range_Check_Failed));
13154 end Silly_Boolean_Array_Not_Test;
13156 ----------------------------------
13157 -- Silly_Boolean_Array_Xor_Test --
13158 ----------------------------------
13160 -- This procedure implements an odd and silly test. We explicitly check
13161 -- for the XOR case where the component type is True .. True, since this
13162 -- will raise constraint error. A special check is required since CE
13163 -- will not be generated otherwise (cf Expand_Packed_Not).
13165 -- No such check is required for AND and OR, since for both these cases
13166 -- False op False = False, and True op True = True, and no check is
13167 -- required for the case of False .. False, since False xor False = False.
13168 -- See also Silly_Boolean_Array_Not_Test
13170 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13171 Loc : constant Source_Ptr := Sloc (N);
13172 CT : constant Entity_Id := Component_Type (T);
13174 begin
13175 -- The check we install is
13177 -- constraint_error when
13178 -- Boolean (component_type'First)
13179 -- and then Boolean (component_type'Last)
13180 -- and then array_type'Length /= 0)
13182 -- We need the last guard because we don't want to raise CE for empty
13183 -- arrays since no out of range values result (Empty arrays with a
13184 -- component type of True .. True -- very useful -- even the ACATS
13185 -- does not test that marginal case).
13187 Insert_Action (N,
13188 Make_Raise_Constraint_Error (Loc,
13189 Condition =>
13190 Make_And_Then (Loc,
13191 Left_Opnd =>
13192 Make_And_Then (Loc,
13193 Left_Opnd =>
13194 Convert_To (Standard_Boolean,
13195 Make_Attribute_Reference (Loc,
13196 Prefix => New_Occurrence_Of (CT, Loc),
13197 Attribute_Name => Name_First)),
13199 Right_Opnd =>
13200 Convert_To (Standard_Boolean,
13201 Make_Attribute_Reference (Loc,
13202 Prefix => New_Occurrence_Of (CT, Loc),
13203 Attribute_Name => Name_Last))),
13205 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13206 Reason => CE_Range_Check_Failed));
13207 end Silly_Boolean_Array_Xor_Test;
13209 --------------------------
13210 -- Target_Has_Fixed_Ops --
13211 --------------------------
13213 Integer_Sized_Small : Ureal;
13214 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13215 -- called (we don't want to compute it more than once).
13217 Long_Integer_Sized_Small : Ureal;
13218 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13219 -- is called (we don't want to compute it more than once)
13221 First_Time_For_THFO : Boolean := True;
13222 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13224 function Target_Has_Fixed_Ops
13225 (Left_Typ : Entity_Id;
13226 Right_Typ : Entity_Id;
13227 Result_Typ : Entity_Id) return Boolean
13229 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13230 -- Return True if the given type is a fixed-point type with a small
13231 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13232 -- an absolute value less than 1.0. This is currently limited to
13233 -- fixed-point types that map to Integer or Long_Integer.
13235 ------------------------
13236 -- Is_Fractional_Type --
13237 ------------------------
13239 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13240 begin
13241 if Esize (Typ) = Standard_Integer_Size then
13242 return Small_Value (Typ) = Integer_Sized_Small;
13244 elsif Esize (Typ) = Standard_Long_Integer_Size then
13245 return Small_Value (Typ) = Long_Integer_Sized_Small;
13247 else
13248 return False;
13249 end if;
13250 end Is_Fractional_Type;
13252 -- Start of processing for Target_Has_Fixed_Ops
13254 begin
13255 -- Return False if Fractional_Fixed_Ops_On_Target is false
13257 if not Fractional_Fixed_Ops_On_Target then
13258 return False;
13259 end if;
13261 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13262 -- standard constants used by Is_Fractional_Type.
13264 if First_Time_For_THFO then
13265 First_Time_For_THFO := False;
13267 Integer_Sized_Small :=
13268 UR_From_Components
13269 (Num => Uint_1,
13270 Den => UI_From_Int (Standard_Integer_Size - 1),
13271 Rbase => 2);
13273 Long_Integer_Sized_Small :=
13274 UR_From_Components
13275 (Num => Uint_1,
13276 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13277 Rbase => 2);
13278 end if;
13280 -- Return True if target supports fixed-by-fixed multiply/divide for
13281 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13282 -- and result types are equivalent fractional types.
13284 return Is_Fractional_Type (Base_Type (Left_Typ))
13285 and then Is_Fractional_Type (Base_Type (Right_Typ))
13286 and then Is_Fractional_Type (Base_Type (Result_Typ))
13287 and then Esize (Left_Typ) = Esize (Right_Typ)
13288 and then Esize (Left_Typ) = Esize (Result_Typ);
13289 end Target_Has_Fixed_Ops;
13291 -------------------
13292 -- Type_Map_Hash --
13293 -------------------
13295 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13296 begin
13297 return Type_Map_Header (Id mod Type_Map_Size);
13298 end Type_Map_Hash;
13300 ------------------------------------------
13301 -- Type_May_Have_Bit_Aligned_Components --
13302 ------------------------------------------
13304 function Type_May_Have_Bit_Aligned_Components
13305 (Typ : Entity_Id) return Boolean
13307 begin
13308 -- Array type, check component type
13310 if Is_Array_Type (Typ) then
13311 return
13312 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13314 -- Record type, check components
13316 elsif Is_Record_Type (Typ) then
13317 declare
13318 E : Entity_Id;
13320 begin
13321 E := First_Component_Or_Discriminant (Typ);
13322 while Present (E) loop
13323 if Component_May_Be_Bit_Aligned (E)
13324 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13325 then
13326 return True;
13327 end if;
13329 Next_Component_Or_Discriminant (E);
13330 end loop;
13332 return False;
13333 end;
13335 -- Type other than array or record is always OK
13337 else
13338 return False;
13339 end if;
13340 end Type_May_Have_Bit_Aligned_Components;
13342 -------------------------------
13343 -- Update_Primitives_Mapping --
13344 -------------------------------
13346 procedure Update_Primitives_Mapping
13347 (Inher_Id : Entity_Id;
13348 Subp_Id : Entity_Id)
13350 begin
13351 Map_Types
13352 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13353 Derived_Type => Find_Dispatching_Type (Subp_Id));
13354 end Update_Primitives_Mapping;
13356 ----------------------------------
13357 -- Within_Case_Or_If_Expression --
13358 ----------------------------------
13360 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13361 Par : Node_Id;
13363 begin
13364 -- Locate an enclosing case or if expression. Note that these constructs
13365 -- can be expanded into Expression_With_Actions, hence the test of the
13366 -- original node.
13368 Par := Parent (N);
13369 while Present (Par) loop
13370 if Nkind_In (Original_Node (Par), N_Case_Expression,
13371 N_If_Expression)
13372 then
13373 return True;
13375 -- Prevent the search from going too far
13377 elsif Is_Body_Or_Package_Declaration (Par) then
13378 return False;
13379 end if;
13381 Par := Parent (Par);
13382 end loop;
13384 return False;
13385 end Within_Case_Or_If_Expression;
13387 --------------------------------
13388 -- Within_Internal_Subprogram --
13389 --------------------------------
13391 function Within_Internal_Subprogram return Boolean is
13392 S : Entity_Id;
13394 begin
13395 S := Current_Scope;
13396 while Present (S) and then not Is_Subprogram (S) loop
13397 S := Scope (S);
13398 end loop;
13400 return Present (S)
13401 and then Get_TSS_Name (S) /= TSS_Null
13402 and then not Is_Predicate_Function (S)
13403 and then not Is_Predicate_Function_M (S);
13404 end Within_Internal_Subprogram;
13406 end Exp_Util;