Fix date
[official-gcc.git] / gcc / ada / exp_util.adb
blob05e075917ab6519ebcda376323b75eeb2ede7d61
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_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Stringt; use Stringt;
62 with Targparm; use Targparm;
63 with Tbuild; use Tbuild;
64 with Ttypes; use Ttypes;
65 with Urealp; use Urealp;
66 with Validsw; use Validsw;
68 with GNAT.HTable; use 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) then
654 return;
656 -- Optimize the case where we are using the default Global_Pool_Object,
657 -- and we don't need the heavy finalization machinery.
659 elsif Pool_Id = RTE (RE_Global_Pool_Object)
660 and then not Needs_Finalization (Desig_Typ)
661 then
662 return;
664 -- Do not replicate the machinery if the allocator / free has already
665 -- been expanded and has a custom Allocate / Deallocate.
667 elsif Present (Proc_To_Call)
668 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
669 then
670 return;
671 end if;
673 -- Finalization actions are required when the object to be allocated or
674 -- deallocated needs these actions and the associated access type is not
675 -- subject to pragma No_Heap_Finalization.
677 Needs_Fin :=
678 Needs_Finalization (Desig_Typ)
679 and then not No_Heap_Finalization (Ptr_Typ);
681 if Needs_Fin then
683 -- Certain run-time configurations and targets do not provide support
684 -- for controlled types.
686 if Restriction_Active (No_Finalization) then
687 return;
689 -- Do nothing if the access type may never allocate / deallocate
690 -- objects.
692 elsif No_Pool_Assigned (Ptr_Typ) then
693 return;
694 end if;
696 -- The allocation / deallocation of a controlled object must be
697 -- chained on / detached from a finalization master.
699 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
701 -- The only other kind of allocation / deallocation supported by this
702 -- routine is on / from a subpool.
704 elsif Nkind (Expr) = N_Allocator
705 and then No (Subpool_Handle_Name (Expr))
706 then
707 return;
708 end if;
710 declare
711 Loc : constant Source_Ptr := Sloc (N);
712 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
713 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
714 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
715 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
717 Actuals : List_Id;
718 Fin_Addr_Id : Entity_Id;
719 Fin_Mas_Act : Node_Id;
720 Fin_Mas_Id : Entity_Id;
721 Proc_To_Call : Entity_Id;
722 Subpool : Node_Id := Empty;
724 begin
725 -- Step 1: Construct all the actuals for the call to library routine
726 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
728 -- a) Storage pool
730 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
732 if Is_Allocate then
734 -- b) Subpool
736 if Nkind (Expr) = N_Allocator then
737 Subpool := Subpool_Handle_Name (Expr);
738 end if;
740 -- If a subpool is present it can be an arbitrary name, so make
741 -- the actual by copying the tree.
743 if Present (Subpool) then
744 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
745 else
746 Append_To (Actuals, Make_Null (Loc));
747 end if;
749 -- c) Finalization master
751 if Needs_Fin then
752 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
753 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
755 -- Handle the case where the master is actually a pointer to a
756 -- master. This case arises in build-in-place functions.
758 if Is_Access_Type (Etype (Fin_Mas_Id)) then
759 Append_To (Actuals, Fin_Mas_Act);
760 else
761 Append_To (Actuals,
762 Make_Attribute_Reference (Loc,
763 Prefix => Fin_Mas_Act,
764 Attribute_Name => Name_Unrestricted_Access));
765 end if;
766 else
767 Append_To (Actuals, Make_Null (Loc));
768 end if;
770 -- d) Finalize_Address
772 -- Primitive Finalize_Address is never generated in CodePeer mode
773 -- since it contains an Unchecked_Conversion.
775 if Needs_Fin and then not CodePeer_Mode then
776 Fin_Addr_Id := Finalize_Address (Desig_Typ);
777 pragma Assert (Present (Fin_Addr_Id));
779 Append_To (Actuals,
780 Make_Attribute_Reference (Loc,
781 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
782 Attribute_Name => Name_Unrestricted_Access));
783 else
784 Append_To (Actuals, Make_Null (Loc));
785 end if;
786 end if;
788 -- e) Address
789 -- f) Storage_Size
790 -- g) Alignment
792 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
793 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
795 if Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ) then
796 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
798 -- For deallocation of class-wide types we obtain the value of
799 -- alignment from the Type Specific Record of the deallocated object.
800 -- This is needed because the frontend expansion of class-wide types
801 -- into equivalent types confuses the back end.
803 else
804 -- Generate:
805 -- Obj.all'Alignment
807 -- ... because 'Alignment applied to class-wide types is expanded
808 -- into the code that reads the value of alignment from the TSD
809 -- (see Expand_N_Attribute_Reference)
811 Append_To (Actuals,
812 Unchecked_Convert_To (RTE (RE_Storage_Offset),
813 Make_Attribute_Reference (Loc,
814 Prefix =>
815 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
816 Attribute_Name => Name_Alignment)));
817 end if;
819 -- h) Is_Controlled
821 if Needs_Fin then
822 Is_Controlled : declare
823 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
824 Flag_Expr : Node_Id;
825 Param : Node_Id;
826 Pref : Node_Id;
827 Temp : Node_Id;
829 begin
830 if Is_Allocate then
831 Temp := Find_Object (Expression (Expr));
832 else
833 Temp := Expr;
834 end if;
836 -- Processing for allocations where the expression is a subtype
837 -- indication.
839 if Is_Allocate
840 and then Is_Entity_Name (Temp)
841 and then Is_Type (Entity (Temp))
842 then
843 Flag_Expr :=
844 New_Occurrence_Of
845 (Boolean_Literals
846 (Needs_Finalization (Entity (Temp))), Loc);
848 -- The allocation / deallocation of a class-wide object relies
849 -- on a runtime check to determine whether the object is truly
850 -- controlled or not. Depending on this check, the finalization
851 -- machinery will request or reclaim extra storage reserved for
852 -- a list header.
854 elsif Is_Class_Wide_Type (Desig_Typ) then
856 -- Detect a special case where interface class-wide types
857 -- are involved as the object appears as:
859 -- Tag_Ptr (Base_Address (<object>'Address))
861 -- The expression already yields the proper tag, generate:
863 -- Temp.all
865 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
866 Param :=
867 Make_Explicit_Dereference (Loc,
868 Prefix => Relocate_Node (Temp));
870 -- In the default case, obtain the tag of the object about
871 -- to be allocated / deallocated. Generate:
873 -- Temp'Tag
875 -- If the object is an unchecked conversion (typically to
876 -- an access to class-wide type), we must preserve the
877 -- conversion to ensure that the object is seen as tagged
878 -- in the code that follows.
880 else
881 Pref := Temp;
883 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
884 then
885 Pref := Parent (Pref);
886 end if;
888 Param :=
889 Make_Attribute_Reference (Loc,
890 Prefix => Relocate_Node (Pref),
891 Attribute_Name => Name_Tag);
892 end if;
894 -- Generate:
895 -- Needs_Finalization (<Param>)
897 Flag_Expr :=
898 Make_Function_Call (Loc,
899 Name =>
900 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
901 Parameter_Associations => New_List (Param));
903 -- Processing for generic actuals
905 elsif Is_Generic_Actual_Type (Desig_Typ) then
906 Flag_Expr :=
907 New_Occurrence_Of (Boolean_Literals
908 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
910 -- The object does not require any specialized checks, it is
911 -- known to be controlled.
913 else
914 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
915 end if;
917 -- Create the temporary which represents the finalization state
918 -- of the expression. Generate:
920 -- F : constant Boolean := <Flag_Expr>;
922 Insert_Action (N,
923 Make_Object_Declaration (Loc,
924 Defining_Identifier => Flag_Id,
925 Constant_Present => True,
926 Object_Definition =>
927 New_Occurrence_Of (Standard_Boolean, Loc),
928 Expression => Flag_Expr));
930 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
931 end Is_Controlled;
933 -- The object is not controlled
935 else
936 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
937 end if;
939 -- i) On_Subpool
941 if Is_Allocate then
942 Append_To (Actuals,
943 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
944 end if;
946 -- Step 2: Build a wrapper Allocate / Deallocate which internally
947 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
949 -- Select the proper routine to call
951 if Is_Allocate then
952 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
953 else
954 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
955 end if;
957 -- Create a custom Allocate / Deallocate routine which has identical
958 -- profile to that of System.Storage_Pools.
960 Insert_Action (N,
961 Make_Subprogram_Body (Loc,
962 Specification =>
964 -- procedure Pnn
966 Make_Procedure_Specification (Loc,
967 Defining_Unit_Name => Proc_Id,
968 Parameter_Specifications => New_List (
970 -- P : Root_Storage_Pool
972 Make_Parameter_Specification (Loc,
973 Defining_Identifier => Make_Temporary (Loc, 'P'),
974 Parameter_Type =>
975 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)),
977 -- A : [out] Address
979 Make_Parameter_Specification (Loc,
980 Defining_Identifier => Addr_Id,
981 Out_Present => Is_Allocate,
982 Parameter_Type =>
983 New_Occurrence_Of (RTE (RE_Address), Loc)),
985 -- S : Storage_Count
987 Make_Parameter_Specification (Loc,
988 Defining_Identifier => Size_Id,
989 Parameter_Type =>
990 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)),
992 -- L : Storage_Count
994 Make_Parameter_Specification (Loc,
995 Defining_Identifier => Alig_Id,
996 Parameter_Type =>
997 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)))),
999 Declarations => No_List,
1001 Handled_Statement_Sequence =>
1002 Make_Handled_Sequence_Of_Statements (Loc,
1003 Statements => New_List (
1004 Make_Procedure_Call_Statement (Loc,
1005 Name =>
1006 New_Occurrence_Of (Proc_To_Call, Loc),
1007 Parameter_Associations => Actuals)))),
1008 Suppress => All_Checks);
1010 -- The newly generated Allocate / Deallocate becomes the default
1011 -- procedure to call when the back end processes the allocation /
1012 -- deallocation.
1014 if Is_Allocate then
1015 Set_Procedure_To_Call (Expr, Proc_Id);
1016 else
1017 Set_Procedure_To_Call (N, Proc_Id);
1018 end if;
1019 end;
1020 end Build_Allocate_Deallocate_Proc;
1022 -------------------------------
1023 -- Build_Abort_Undefer_Block --
1024 -------------------------------
1026 function Build_Abort_Undefer_Block
1027 (Loc : Source_Ptr;
1028 Stmts : List_Id;
1029 Context : Node_Id) return Node_Id
1031 Exceptions_OK : constant Boolean :=
1032 not Restriction_Active (No_Exception_Propagation);
1034 AUD : Entity_Id;
1035 Blk : Node_Id;
1036 Blk_Id : Entity_Id;
1037 HSS : Node_Id;
1039 begin
1040 -- The block should be generated only when undeferring abort in the
1041 -- context of a potential exception.
1043 pragma Assert (Abort_Allowed and Exceptions_OK);
1045 -- Generate:
1046 -- begin
1047 -- <Stmts>
1048 -- at end
1049 -- Abort_Undefer_Direct;
1050 -- end;
1052 AUD := RTE (RE_Abort_Undefer_Direct);
1054 HSS :=
1055 Make_Handled_Sequence_Of_Statements (Loc,
1056 Statements => Stmts,
1057 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1059 Blk :=
1060 Make_Block_Statement (Loc,
1061 Handled_Statement_Sequence => HSS);
1062 Set_Is_Abort_Block (Blk);
1064 Add_Block_Identifier (Blk, Blk_Id);
1065 Expand_At_End_Handler (HSS, Blk_Id);
1067 -- Present the Abort_Undefer_Direct function to the back end to inline
1068 -- the call to the routine.
1070 Add_Inlined_Body (AUD, Context);
1072 return Blk;
1073 end Build_Abort_Undefer_Block;
1075 ---------------------------------
1076 -- Build_Class_Wide_Expression --
1077 ---------------------------------
1079 procedure Build_Class_Wide_Expression
1080 (Prag : Node_Id;
1081 Subp : Entity_Id;
1082 Par_Subp : Entity_Id;
1083 Adjust_Sloc : Boolean;
1084 Needs_Wrapper : out Boolean)
1086 function Replace_Entity (N : Node_Id) return Traverse_Result;
1087 -- Replace reference to formal of inherited operation or to primitive
1088 -- operation of root type, with corresponding entity for derived type,
1089 -- when constructing the class-wide condition of an overriding
1090 -- subprogram.
1092 --------------------
1093 -- Replace_Entity --
1094 --------------------
1096 function Replace_Entity (N : Node_Id) return Traverse_Result is
1097 New_E : Entity_Id;
1099 begin
1100 if Adjust_Sloc then
1101 Adjust_Inherited_Pragma_Sloc (N);
1102 end if;
1104 if Nkind (N) = N_Identifier
1105 and then Present (Entity (N))
1106 and then
1107 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1108 and then
1109 (Nkind (Parent (N)) /= N_Attribute_Reference
1110 or else Attribute_Name (Parent (N)) /= Name_Class)
1111 then
1112 -- The replacement does not apply to dispatching calls within the
1113 -- condition, but only to calls whose static tag is that of the
1114 -- parent type.
1116 if Is_Subprogram (Entity (N))
1117 and then Nkind (Parent (N)) = N_Function_Call
1118 and then Present (Controlling_Argument (Parent (N)))
1119 then
1120 return OK;
1121 end if;
1123 -- Determine whether entity has a renaming
1125 New_E := Type_Map.Get (Entity (N));
1127 if Present (New_E) then
1128 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1130 -- If the entity is an overridden primitive and we are not
1131 -- in GNATprove mode, we must build a wrapper for the current
1132 -- inherited operation. If the reference is the prefix of an
1133 -- attribute such as 'Result (or others ???) there is no need
1134 -- for a wrapper: the condition is just rewritten in terms of
1135 -- the inherited subprogram.
1137 if Is_Subprogram (New_E)
1138 and then Nkind (Parent (N)) /= N_Attribute_Reference
1139 and then not GNATprove_Mode
1140 then
1141 Needs_Wrapper := True;
1142 end if;
1143 end if;
1145 -- Check that there are no calls left to abstract operations if
1146 -- the current subprogram is not abstract.
1148 if Nkind (Parent (N)) = N_Function_Call
1149 and then N = Name (Parent (N))
1150 then
1151 if not Is_Abstract_Subprogram (Subp)
1152 and then Is_Abstract_Subprogram (Entity (N))
1153 then
1154 Error_Msg_Sloc := Sloc (Current_Scope);
1155 Error_Msg_Node_2 := Subp;
1156 if Comes_From_Source (Subp) then
1157 Error_Msg_NE
1158 ("cannot call abstract subprogram & in inherited "
1159 & "condition for&#", Subp, Entity (N));
1160 else
1161 Error_Msg_NE
1162 ("cannot call abstract subprogram & in inherited "
1163 & "condition for inherited&#", Subp, Entity (N));
1164 end if;
1166 -- In SPARK mode, reject an inherited condition for an
1167 -- inherited operation if it contains a call to an overriding
1168 -- operation, because this implies that the pre/postconditions
1169 -- of the inherited operation have changed silently.
1171 elsif SPARK_Mode = On
1172 and then Warn_On_Suspicious_Contract
1173 and then Present (Alias (Subp))
1174 and then Present (New_E)
1175 and then Comes_From_Source (New_E)
1176 then
1177 Error_Msg_N
1178 ("cannot modify inherited condition (SPARK RM 6.1.1(1))",
1179 Parent (Subp));
1180 Error_Msg_Sloc := Sloc (New_E);
1181 Error_Msg_Node_2 := Subp;
1182 Error_Msg_NE
1183 ("\overriding of&# forces overriding of&",
1184 Parent (Subp), New_E);
1185 end if;
1186 end if;
1188 -- Update type of function call node, which should be the same as
1189 -- the function's return type.
1191 if Is_Subprogram (Entity (N))
1192 and then Nkind (Parent (N)) = N_Function_Call
1193 then
1194 Set_Etype (Parent (N), Etype (Entity (N)));
1195 end if;
1197 -- The whole expression will be reanalyzed
1199 elsif Nkind (N) in N_Has_Etype then
1200 Set_Analyzed (N, False);
1201 end if;
1203 return OK;
1204 end Replace_Entity;
1206 procedure Replace_Condition_Entities is
1207 new Traverse_Proc (Replace_Entity);
1209 -- Local variables
1211 Par_Formal : Entity_Id;
1212 Subp_Formal : Entity_Id;
1214 -- Start of processing for Build_Class_Wide_Expression
1216 begin
1217 Needs_Wrapper := False;
1219 -- Add mapping from old formals to new formals
1221 Par_Formal := First_Formal (Par_Subp);
1222 Subp_Formal := First_Formal (Subp);
1224 while Present (Par_Formal) and then Present (Subp_Formal) loop
1225 Type_Map.Set (Par_Formal, Subp_Formal);
1226 Next_Formal (Par_Formal);
1227 Next_Formal (Subp_Formal);
1228 end loop;
1230 Replace_Condition_Entities (Prag);
1231 end Build_Class_Wide_Expression;
1233 --------------------
1234 -- Build_DIC_Call --
1235 --------------------
1237 function Build_DIC_Call
1238 (Loc : Source_Ptr;
1239 Obj_Id : Entity_Id;
1240 Typ : Entity_Id) return Node_Id
1242 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1243 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1245 begin
1246 return
1247 Make_Procedure_Call_Statement (Loc,
1248 Name => New_Occurrence_Of (Proc_Id, Loc),
1249 Parameter_Associations => New_List (
1250 Make_Unchecked_Type_Conversion (Loc,
1251 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1252 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1253 end Build_DIC_Call;
1255 ------------------------------
1256 -- Build_DIC_Procedure_Body --
1257 ------------------------------
1259 -- WARNING: This routine manages Ghost regions. Return statements must be
1260 -- replaced by gotos which jump to the end of the routine and restore the
1261 -- Ghost mode.
1263 procedure Build_DIC_Procedure_Body
1264 (Typ : Entity_Id;
1265 For_Freeze : Boolean := False)
1267 procedure Add_DIC_Check
1268 (DIC_Prag : Node_Id;
1269 DIC_Expr : Node_Id;
1270 Stmts : in out List_Id);
1271 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1272 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1273 -- is added to list Stmts.
1275 procedure Add_Inherited_DIC
1276 (DIC_Prag : Node_Id;
1277 Par_Typ : Entity_Id;
1278 Deriv_Typ : Entity_Id;
1279 Stmts : in out List_Id);
1280 -- Add a runtime check to verify the assertion expression of inherited
1281 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1282 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1283 -- pragma. All generated code is added to list Stmts.
1285 procedure Add_Inherited_Tagged_DIC
1286 (DIC_Prag : Node_Id;
1287 Par_Typ : Entity_Id;
1288 Deriv_Typ : Entity_Id;
1289 Stmts : in out List_Id);
1290 -- Add a runtime check to verify assertion expression DIC_Expr of
1291 -- inherited pragma DIC_Prag. This routine applies class-wide pre- and
1292 -- postcondition-like runtime semantics to the check. Par_Typ is the
1293 -- parent type whose DIC pragma is being inherited. Deriv_Typ is the
1294 -- derived type inheriting the DIC pragma. All generated code is added
1295 -- to list Stmts.
1297 procedure Add_Own_DIC
1298 (DIC_Prag : Node_Id;
1299 DIC_Typ : Entity_Id;
1300 Stmts : in out List_Id);
1301 -- Add a runtime check to verify the assertion expression of pragma
1302 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. All generated code
1303 -- is added to list Stmts.
1305 -------------------
1306 -- Add_DIC_Check --
1307 -------------------
1309 procedure Add_DIC_Check
1310 (DIC_Prag : Node_Id;
1311 DIC_Expr : Node_Id;
1312 Stmts : in out List_Id)
1314 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1315 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1317 begin
1318 -- The DIC pragma is ignored, nothing left to do
1320 if Is_Ignored (DIC_Prag) then
1321 null;
1323 -- Otherwise the DIC expression must be checked at run time.
1324 -- Generate:
1326 -- pragma Check (<Nam>, <DIC_Expr>);
1328 else
1329 Append_New_To (Stmts,
1330 Make_Pragma (Loc,
1331 Pragma_Identifier =>
1332 Make_Identifier (Loc, Name_Check),
1334 Pragma_Argument_Associations => New_List (
1335 Make_Pragma_Argument_Association (Loc,
1336 Expression => Make_Identifier (Loc, Nam)),
1338 Make_Pragma_Argument_Association (Loc,
1339 Expression => DIC_Expr))));
1340 end if;
1341 end Add_DIC_Check;
1343 -----------------------
1344 -- Add_Inherited_DIC --
1345 -----------------------
1347 procedure Add_Inherited_DIC
1348 (DIC_Prag : Node_Id;
1349 Par_Typ : Entity_Id;
1350 Deriv_Typ : Entity_Id;
1351 Stmts : in out List_Id)
1353 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1354 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1355 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1356 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1357 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1359 begin
1360 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1362 -- Verify the inherited DIC assertion expression by calling the DIC
1363 -- procedure of the parent type.
1365 -- Generate:
1366 -- <Par_Typ>DIC (Par_Typ (_object));
1368 Append_New_To (Stmts,
1369 Make_Procedure_Call_Statement (Loc,
1370 Name => New_Occurrence_Of (Par_Proc, Loc),
1371 Parameter_Associations => New_List (
1372 Convert_To
1373 (Typ => Etype (Par_Obj),
1374 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1375 end Add_Inherited_DIC;
1377 ------------------------------
1378 -- Add_Inherited_Tagged_DIC --
1379 ------------------------------
1381 procedure Add_Inherited_Tagged_DIC
1382 (DIC_Prag : Node_Id;
1383 Par_Typ : Entity_Id;
1384 Deriv_Typ : Entity_Id;
1385 Stmts : in out List_Id)
1387 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1388 DIC_Args : constant List_Id :=
1389 Pragma_Argument_Associations (DIC_Prag);
1390 DIC_Arg : constant Node_Id := First (DIC_Args);
1391 DIC_Expr : constant Node_Id := Expression_Copy (DIC_Arg);
1392 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1394 Expr : Node_Id;
1396 begin
1397 -- The processing of an inherited DIC assertion expression starts off
1398 -- with a copy of the original parent expression where all references
1399 -- to the parent type have already been replaced with references to
1400 -- the _object formal parameter of the parent type's DIC procedure.
1402 pragma Assert (Present (DIC_Expr));
1403 Expr := New_Copy_Tree (DIC_Expr);
1405 -- Perform the following substitutions:
1407 -- * Replace a reference to the _object parameter of the parent
1408 -- type's DIC procedure with a reference to the _object parameter
1409 -- of the derived types' DIC procedure.
1411 -- * Replace a reference to a discriminant of the parent type with
1412 -- a suitable value from the point of view of the derived type.
1414 -- * Replace a call to an overridden parent primitive with a call
1415 -- to the overriding derived type primitive.
1417 -- * Replace a call to an inherited parent primitive with a call to
1418 -- the internally-generated inherited derived type primitive.
1420 -- Note that primitives defined in the private part are automatically
1421 -- handled by the overriding/inheritance mechanism and do not require
1422 -- an extra replacement pass.
1424 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1426 Replace_References
1427 (Expr => Expr,
1428 Par_Typ => Par_Typ,
1429 Deriv_Typ => Deriv_Typ,
1430 Par_Obj => First_Formal (Par_Proc),
1431 Deriv_Obj => First_Formal (Deriv_Proc));
1433 -- Once the DIC assertion expression is fully processed, add a check
1434 -- to the statements of the DIC procedure.
1436 Add_DIC_Check
1437 (DIC_Prag => DIC_Prag,
1438 DIC_Expr => Expr,
1439 Stmts => Stmts);
1440 end Add_Inherited_Tagged_DIC;
1442 -----------------
1443 -- Add_Own_DIC --
1444 -----------------
1446 procedure Add_Own_DIC
1447 (DIC_Prag : Node_Id;
1448 DIC_Typ : Entity_Id;
1449 Stmts : in out List_Id)
1451 DIC_Args : constant List_Id :=
1452 Pragma_Argument_Associations (DIC_Prag);
1453 DIC_Arg : constant Node_Id := First (DIC_Args);
1454 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1455 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1456 DIC_Proc : constant Entity_Id := DIC_Procedure (DIC_Typ);
1457 Obj_Id : constant Entity_Id := First_Formal (DIC_Proc);
1459 procedure Preanalyze_Own_DIC_For_ASIS;
1460 -- Preanalyze the original DIC expression of an aspect or a source
1461 -- pragma for ASIS.
1463 ---------------------------------
1464 -- Preanalyze_Own_DIC_For_ASIS --
1465 ---------------------------------
1467 procedure Preanalyze_Own_DIC_For_ASIS is
1468 Expr : Node_Id := Empty;
1470 begin
1471 -- The DIC pragma is a source construct, preanalyze the original
1472 -- expression of the pragma.
1474 if Comes_From_Source (DIC_Prag) then
1475 Expr := DIC_Expr;
1477 -- Otherwise preanalyze the expression of the corresponding aspect
1479 elsif Present (DIC_Asp) then
1480 Expr := Expression (DIC_Asp);
1481 end if;
1483 -- The expression must be subjected to the same substitutions as
1484 -- the copy used in the generation of the runtime check.
1486 if Present (Expr) then
1487 Replace_Type_References
1488 (Expr => Expr,
1489 Typ => DIC_Typ,
1490 Obj_Id => Obj_Id);
1492 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1493 end if;
1494 end Preanalyze_Own_DIC_For_ASIS;
1496 -- Local variables
1498 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1500 Expr : Node_Id;
1502 -- Start of processing for Add_Own_DIC
1504 begin
1505 Expr := New_Copy_Tree (DIC_Expr);
1507 -- Perform the following substitution:
1509 -- * Replace the current instance of DIC_Typ with a reference to
1510 -- the _object formal parameter of the DIC procedure.
1512 Replace_Type_References
1513 (Expr => Expr,
1514 Typ => DIC_Typ,
1515 Obj_Id => Obj_Id);
1517 -- Preanalyze the DIC expression to detect errors and at the same
1518 -- time capture the visibility of the proper package part.
1520 Set_Parent (Expr, Typ_Decl);
1521 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1523 -- Save a copy of the expression with all replacements and analysis
1524 -- already taken place in case a derived type inherits the pragma.
1525 -- The copy will be used as the foundation of the derived type's own
1526 -- version of the DIC assertion expression.
1528 if Is_Tagged_Type (DIC_Typ) then
1529 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1530 end if;
1532 -- If the pragma comes from an aspect specification, replace the
1533 -- saved expression because all type references must be substituted
1534 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1535 -- routines.
1537 if Present (DIC_Asp) then
1538 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1539 end if;
1541 -- Preanalyze the original DIC expression for ASIS
1543 if ASIS_Mode then
1544 Preanalyze_Own_DIC_For_ASIS;
1545 end if;
1547 -- Once the DIC assertion expression is fully processed, add a check
1548 -- to the statements of the DIC procedure.
1550 Add_DIC_Check
1551 (DIC_Prag => DIC_Prag,
1552 DIC_Expr => Expr,
1553 Stmts => Stmts);
1554 end Add_Own_DIC;
1556 -- Local variables
1558 Loc : constant Source_Ptr := Sloc (Typ);
1560 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1561 -- Save the Ghost mode to restore on exit
1563 DIC_Prag : Node_Id;
1564 DIC_Typ : Entity_Id;
1565 Dummy_1 : Entity_Id;
1566 Dummy_2 : Entity_Id;
1567 Proc_Body : Node_Id;
1568 Proc_Body_Id : Entity_Id;
1569 Proc_Decl : Node_Id;
1570 Proc_Id : Entity_Id;
1571 Stmts : List_Id := No_List;
1573 Build_Body : Boolean := False;
1574 -- Flag set when the type requires a DIC procedure body to be built
1576 Work_Typ : Entity_Id;
1577 -- The working type
1579 -- Start of processing for Build_DIC_Procedure_Body
1581 begin
1582 Work_Typ := Base_Type (Typ);
1584 -- Do not process class-wide types as these are Itypes, but lack a first
1585 -- subtype (see below).
1587 if Is_Class_Wide_Type (Work_Typ) then
1588 return;
1590 -- Do not process the underlying full view of a private type. There is
1591 -- no way to get back to the partial view, plus the body will be built
1592 -- by the full view or the base type.
1594 elsif Is_Underlying_Full_View (Work_Typ) then
1595 return;
1597 -- Use the first subtype when dealing with various base types
1599 elsif Is_Itype (Work_Typ) then
1600 Work_Typ := First_Subtype (Work_Typ);
1602 -- The input denotes the corresponding record type of a protected or a
1603 -- task type. Work with the concurrent type because the corresponding
1604 -- record type may not be visible to clients of the type.
1606 elsif Ekind (Work_Typ) = E_Record_Type
1607 and then Is_Concurrent_Record_Type (Work_Typ)
1608 then
1609 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1610 end if;
1612 -- The working type may be subject to pragma Ghost. Set the mode now to
1613 -- ensure that the DIC procedure is properly marked as Ghost.
1615 Set_Ghost_Mode (Work_Typ);
1617 -- The working type must be either define a DIC pragma of its own or
1618 -- inherit one from a parent type.
1620 pragma Assert (Has_DIC (Work_Typ));
1622 -- Recover the type which defines the DIC pragma. This is either the
1623 -- working type itself or a parent type when the pragma is inherited.
1625 DIC_Typ := Find_DIC_Type (Work_Typ);
1626 pragma Assert (Present (DIC_Typ));
1628 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1629 pragma Assert (Present (DIC_Prag));
1631 -- Nothing to do if pragma DIC appears without an argument or its sole
1632 -- argument is "null".
1634 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1635 goto Leave;
1636 end if;
1638 -- The working type may lack a DIC procedure declaration. This may be
1639 -- due to several reasons:
1641 -- * The working type's own DIC pragma does not contain a verifiable
1642 -- assertion expression. In this case there is no need to build a
1643 -- DIC procedure because there is nothing to check.
1645 -- * The working type derives from a parent type. In this case a DIC
1646 -- procedure should be built only when the inherited DIC pragma has
1647 -- a verifiable assertion expression.
1649 Proc_Id := DIC_Procedure (Work_Typ);
1651 -- Build a DIC procedure declaration when the working type derives from
1652 -- a parent type.
1654 if No (Proc_Id) then
1655 Build_DIC_Procedure_Declaration (Work_Typ);
1656 Proc_Id := DIC_Procedure (Work_Typ);
1657 end if;
1659 -- At this point there should be a DIC procedure declaration
1661 pragma Assert (Present (Proc_Id));
1662 Proc_Decl := Unit_Declaration_Node (Proc_Id);
1664 -- Nothing to do if the DIC procedure already has a body
1666 if Present (Corresponding_Body (Proc_Decl)) then
1667 goto Leave;
1668 end if;
1670 -- Emulate the environment of the DIC procedure by installing its scope
1671 -- and formal parameters.
1673 Push_Scope (Proc_Id);
1674 Install_Formals (Proc_Id);
1676 -- The working type defines its own DIC pragma. Replace the current
1677 -- instance of the working type with the formal of the DIC procedure.
1678 -- Note that there is no need to consider inherited DIC pragmas from
1679 -- parent types because the working type's DIC pragma "hides" all
1680 -- inherited DIC pragmas.
1682 if Has_Own_DIC (Work_Typ) then
1683 pragma Assert (DIC_Typ = Work_Typ);
1685 Add_Own_DIC
1686 (DIC_Prag => DIC_Prag,
1687 DIC_Typ => DIC_Typ,
1688 Stmts => Stmts);
1690 Build_Body := True;
1692 -- Otherwise the working type inherits a DIC pragma from a parent type.
1693 -- This processing is carried out when the type is frozen because the
1694 -- state of all parent discriminants is known at that point. Note that
1695 -- it is semantically sound to delay the creation of the DIC procedure
1696 -- body till the freeze point. If the type has a DIC pragma of its own,
1697 -- then the DIC procedure body would have already been constructed at
1698 -- the end of the visible declarations and all parent DIC pragmas are
1699 -- effectively "hidden" and irrelevant.
1701 elsif For_Freeze then
1702 pragma Assert (Has_Inherited_DIC (Work_Typ));
1703 pragma Assert (DIC_Typ /= Work_Typ);
1705 -- The working type is tagged. The verification of the assertion
1706 -- expression is subject to the same semantics as class-wide pre-
1707 -- and postconditions.
1709 if Is_Tagged_Type (Work_Typ) then
1710 Add_Inherited_Tagged_DIC
1711 (DIC_Prag => DIC_Prag,
1712 Par_Typ => DIC_Typ,
1713 Deriv_Typ => Work_Typ,
1714 Stmts => Stmts);
1716 -- Otherwise the working type is not tagged. Verify the assertion
1717 -- expression of the inherited DIC pragma by directly calling the
1718 -- DIC procedure of the parent type.
1720 else
1721 Add_Inherited_DIC
1722 (DIC_Prag => DIC_Prag,
1723 Par_Typ => DIC_Typ,
1724 Deriv_Typ => Work_Typ,
1725 Stmts => Stmts);
1726 end if;
1728 Build_Body := True;
1729 end if;
1731 End_Scope;
1733 if Build_Body then
1735 -- Produce an empty completing body in the following cases:
1736 -- * Assertions are disabled
1737 -- * The DIC Assertion_Policy is Ignore
1738 -- * Pragma DIC appears without an argument
1739 -- * Pragma DIC appears with argument "null"
1741 if No (Stmts) then
1742 Stmts := New_List (Make_Null_Statement (Loc));
1743 end if;
1745 -- Generate:
1746 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
1747 -- begin
1748 -- <Stmts>
1749 -- end <Work_Typ>DIC;
1751 Proc_Body :=
1752 Make_Subprogram_Body (Loc,
1753 Specification =>
1754 Copy_Subprogram_Spec (Parent (Proc_Id)),
1755 Declarations => Empty_List,
1756 Handled_Statement_Sequence =>
1757 Make_Handled_Sequence_Of_Statements (Loc,
1758 Statements => Stmts));
1759 Proc_Body_Id := Defining_Entity (Proc_Body);
1761 -- Perform minor decoration in case the body is not analyzed
1763 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
1764 Set_Etype (Proc_Body_Id, Standard_Void_Type);
1765 Set_Scope (Proc_Body_Id, Current_Scope);
1767 -- Link both spec and body to avoid generating duplicates
1769 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
1770 Set_Corresponding_Spec (Proc_Body, Proc_Id);
1772 -- The body should not be inserted into the tree when the context
1773 -- is ASIS or a generic unit because it is not part of the template.
1774 -- Note that the body must still be generated in order to resolve the
1775 -- DIC assertion expression.
1777 if ASIS_Mode or Inside_A_Generic then
1778 null;
1780 -- Semi-insert the body into the tree for GNATprove by setting its
1781 -- Parent field. This allows for proper upstream tree traversals.
1783 elsif GNATprove_Mode then
1784 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
1786 -- Otherwise the body is part of the freezing actions of the working
1787 -- type.
1789 else
1790 Append_Freeze_Action (Work_Typ, Proc_Body);
1791 end if;
1792 end if;
1794 <<Leave>>
1795 Restore_Ghost_Mode (Saved_GM);
1796 end Build_DIC_Procedure_Body;
1798 -------------------------------------
1799 -- Build_DIC_Procedure_Declaration --
1800 -------------------------------------
1802 -- WARNING: This routine manages Ghost regions. Return statements must be
1803 -- replaced by gotos which jump to the end of the routine and restore the
1804 -- Ghost mode.
1806 procedure Build_DIC_Procedure_Declaration (Typ : Entity_Id) is
1807 Loc : constant Source_Ptr := Sloc (Typ);
1809 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1810 -- Save the Ghost mode to restore on exit
1812 DIC_Prag : Node_Id;
1813 DIC_Typ : Entity_Id;
1814 Proc_Decl : Node_Id;
1815 Proc_Id : Entity_Id;
1816 Typ_Decl : Node_Id;
1818 CRec_Typ : Entity_Id;
1819 -- The corresponding record type of Full_Typ
1821 Full_Base : Entity_Id;
1822 -- The base type of Full_Typ
1824 Full_Typ : Entity_Id;
1825 -- The full view of working type
1827 Obj_Id : Entity_Id;
1828 -- The _object formal parameter of the DIC procedure
1830 Priv_Typ : Entity_Id;
1831 -- The partial view of working type
1833 Work_Typ : Entity_Id;
1834 -- The working type
1836 begin
1837 Work_Typ := Base_Type (Typ);
1839 -- Do not process class-wide types as these are Itypes, but lack a first
1840 -- subtype (see below).
1842 if Is_Class_Wide_Type (Work_Typ) then
1843 return;
1845 -- Do not process the underlying full view of a private type. There is
1846 -- no way to get back to the partial view, plus the body will be built
1847 -- by the full view or the base type.
1849 elsif Is_Underlying_Full_View (Work_Typ) then
1850 return;
1852 -- Use the first subtype when dealing with various base types
1854 elsif Is_Itype (Work_Typ) then
1855 Work_Typ := First_Subtype (Work_Typ);
1857 -- The input denotes the corresponding record type of a protected or a
1858 -- task type. Work with the concurrent type because the corresponding
1859 -- record type may not be visible to clients of the type.
1861 elsif Ekind (Work_Typ) = E_Record_Type
1862 and then Is_Concurrent_Record_Type (Work_Typ)
1863 then
1864 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1865 end if;
1867 -- The working type may be subject to pragma Ghost. Set the mode now to
1868 -- ensure that the DIC procedure is properly marked as Ghost.
1870 Set_Ghost_Mode (Work_Typ);
1872 -- The type must be either subject to a DIC pragma or inherit one from a
1873 -- parent type.
1875 pragma Assert (Has_DIC (Work_Typ));
1877 -- Recover the type which defines the DIC pragma. This is either the
1878 -- working type itself or a parent type when the pragma is inherited.
1880 DIC_Typ := Find_DIC_Type (Work_Typ);
1881 pragma Assert (Present (DIC_Typ));
1883 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1884 pragma Assert (Present (DIC_Prag));
1886 -- Nothing to do if pragma DIC appears without an argument or its sole
1887 -- argument is "null".
1889 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1890 goto Leave;
1892 -- Nothing to do if the type already has a DIC procedure
1894 elsif Present (DIC_Procedure (Work_Typ)) then
1895 goto Leave;
1896 end if;
1898 Proc_Id :=
1899 Make_Defining_Identifier (Loc,
1900 Chars =>
1901 New_External_Name (Chars (Work_Typ), "Default_Initial_Condition"));
1903 -- Perform minor decoration in case the declaration is not analyzed
1905 Set_Ekind (Proc_Id, E_Procedure);
1906 Set_Etype (Proc_Id, Standard_Void_Type);
1907 Set_Scope (Proc_Id, Current_Scope);
1909 Set_Is_DIC_Procedure (Proc_Id);
1910 Set_DIC_Procedure (Work_Typ, Proc_Id);
1912 -- The DIC procedure requires debug info when the assertion expression
1913 -- is subject to Source Coverage Obligations.
1915 if Opt.Generate_SCO then
1916 Set_Needs_Debug_Info (Proc_Id);
1917 end if;
1919 -- Obtain all views of the input type
1921 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
1923 -- Associate the DIC procedure and various relevant flags with all views
1925 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
1926 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
1927 Propagate_DIC_Attributes (Full_Base, From_Typ => Work_Typ);
1928 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
1930 -- The declaration of the DIC procedure must be inserted after the
1931 -- declaration of the partial view as this allows for proper external
1932 -- visibility.
1934 if Present (Priv_Typ) then
1935 Typ_Decl := Declaration_Node (Priv_Typ);
1937 -- Derived types with the full view as parent do not have a partial
1938 -- view. Insert the DIC procedure after the derived type.
1940 else
1941 Typ_Decl := Declaration_Node (Full_Typ);
1942 end if;
1944 -- The type should have a declarative node
1946 pragma Assert (Present (Typ_Decl));
1948 -- Create the formal parameter which emulates the variable-like behavior
1949 -- of the type's current instance.
1951 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
1953 -- Perform minor decoration in case the declaration is not analyzed
1955 Set_Ekind (Obj_Id, E_In_Parameter);
1956 Set_Etype (Obj_Id, Work_Typ);
1957 Set_Scope (Obj_Id, Proc_Id);
1959 Set_First_Entity (Proc_Id, Obj_Id);
1961 -- Generate:
1962 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
1964 Proc_Decl :=
1965 Make_Subprogram_Declaration (Loc,
1966 Specification =>
1967 Make_Procedure_Specification (Loc,
1968 Defining_Unit_Name => Proc_Id,
1969 Parameter_Specifications => New_List (
1970 Make_Parameter_Specification (Loc,
1971 Defining_Identifier => Obj_Id,
1972 Parameter_Type =>
1973 New_Occurrence_Of (Work_Typ, Loc)))));
1975 -- The declaration should not be inserted into the tree when the context
1976 -- is ASIS or a generic unit because it is not part of the template.
1978 if ASIS_Mode or Inside_A_Generic then
1979 null;
1981 -- Semi-insert the declaration into the tree for GNATprove by setting
1982 -- its Parent field. This allows for proper upstream tree traversals.
1984 elsif GNATprove_Mode then
1985 Set_Parent (Proc_Decl, Parent (Typ_Decl));
1987 -- Otherwise insert the declaration
1989 else
1990 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
1991 end if;
1993 <<Leave>>
1994 Restore_Ghost_Mode (Saved_GM);
1995 end Build_DIC_Procedure_Declaration;
1997 ------------------------------------
1998 -- Build_Invariant_Procedure_Body --
1999 ------------------------------------
2001 -- WARNING: This routine manages Ghost regions. Return statements must be
2002 -- replaced by gotos which jump to the end of the routine and restore the
2003 -- Ghost mode.
2005 procedure Build_Invariant_Procedure_Body
2006 (Typ : Entity_Id;
2007 Partial_Invariant : Boolean := False)
2009 Loc : constant Source_Ptr := Sloc (Typ);
2011 Pragmas_Seen : Elist_Id := No_Elist;
2012 -- This list contains all invariant pragmas processed so far. The list
2013 -- is used to avoid generating redundant invariant checks.
2015 Produced_Check : Boolean := False;
2016 -- This flag tracks whether the type has produced at least one invariant
2017 -- check. The flag is used as a sanity check at the end of the routine.
2019 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2020 -- intentionally unnested to avoid deep indentation of code.
2022 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2023 -- they emit checks, loops (for arrays) and case statements (for record
2024 -- variant parts) only when there are invariants to verify. This keeps
2025 -- the body of the invariant procedure free of useless code.
2027 procedure Add_Array_Component_Invariants
2028 (T : Entity_Id;
2029 Obj_Id : Entity_Id;
2030 Checks : in out List_Id);
2031 -- Generate an invariant check for each component of array type T.
2032 -- Obj_Id denotes the entity of the _object formal parameter of the
2033 -- invariant procedure. All created checks are added to list Checks.
2035 procedure Add_Inherited_Invariants
2036 (T : Entity_Id;
2037 Priv_Typ : Entity_Id;
2038 Full_Typ : Entity_Id;
2039 Obj_Id : Entity_Id;
2040 Checks : in out List_Id);
2041 -- Generate an invariant check for each inherited class-wide invariant
2042 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2043 -- the partial and full view of the parent type. Obj_Id denotes the
2044 -- entity of the _object formal parameter of the invariant procedure.
2045 -- All created checks are added to list Checks.
2047 procedure Add_Interface_Invariants
2048 (T : Entity_Id;
2049 Obj_Id : Entity_Id;
2050 Checks : in out List_Id);
2051 -- Generate an invariant check for each inherited class-wide invariant
2052 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2053 -- entity of the _object formal parameter of the invariant procedure.
2054 -- All created checks are added to list Checks.
2056 procedure Add_Invariant_Check
2057 (Prag : Node_Id;
2058 Expr : Node_Id;
2059 Checks : in out List_Id;
2060 Inherited : Boolean := False);
2061 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2062 -- verify assertion expression Expr of pragma Prag. All generated code
2063 -- is added to list Checks. Flag Inherited should be set when the pragma
2064 -- is inherited from a parent or interface type.
2066 procedure Add_Own_Invariants
2067 (T : Entity_Id;
2068 Obj_Id : Entity_Id;
2069 Checks : in out List_Id;
2070 Priv_Item : Node_Id := Empty);
2071 -- Generate an invariant check for each invariant found for type T.
2072 -- Obj_Id denotes the entity of the _object formal parameter of the
2073 -- invariant procedure. All created checks are added to list Checks.
2074 -- Priv_Item denotes the first rep item of the private type.
2076 procedure Add_Parent_Invariants
2077 (T : Entity_Id;
2078 Obj_Id : Entity_Id;
2079 Checks : in out List_Id);
2080 -- Generate an invariant check for each inherited class-wide invariant
2081 -- coming from all parent types of type T. Obj_Id denotes the entity of
2082 -- the _object formal parameter of the invariant procedure. All created
2083 -- checks are added to list Checks.
2085 procedure Add_Record_Component_Invariants
2086 (T : Entity_Id;
2087 Obj_Id : Entity_Id;
2088 Checks : in out List_Id);
2089 -- Generate an invariant check for each component of record type T.
2090 -- Obj_Id denotes the entity of the _object formal parameter of the
2091 -- invariant procedure. All created checks are added to list Checks.
2093 ------------------------------------
2094 -- Add_Array_Component_Invariants --
2095 ------------------------------------
2097 procedure Add_Array_Component_Invariants
2098 (T : Entity_Id;
2099 Obj_Id : Entity_Id;
2100 Checks : in out List_Id)
2102 Comp_Typ : constant Entity_Id := Component_Type (T);
2103 Dims : constant Pos := Number_Dimensions (T);
2105 procedure Process_Array_Component
2106 (Indices : List_Id;
2107 Comp_Checks : in out List_Id);
2108 -- Generate an invariant check for an array component identified by
2109 -- the indices in list Indices. All created checks are added to list
2110 -- Comp_Checks.
2112 procedure Process_One_Dimension
2113 (Dim : Pos;
2114 Indices : List_Id;
2115 Dim_Checks : in out List_Id);
2116 -- Generate a loop over the Nth dimension Dim of an array type. List
2117 -- Indices contains all array indices for the dimension. All created
2118 -- checks are added to list Dim_Checks.
2120 -----------------------------
2121 -- Process_Array_Component --
2122 -----------------------------
2124 procedure Process_Array_Component
2125 (Indices : List_Id;
2126 Comp_Checks : in out List_Id)
2128 Proc_Id : Entity_Id;
2130 begin
2131 if Has_Invariants (Comp_Typ) then
2133 -- In GNATprove mode, the component invariants are checked by
2134 -- other means. They should not be added to the array type
2135 -- invariant procedure, so that the procedure can be used to
2136 -- check the array type invariants if any.
2138 if GNATprove_Mode then
2139 null;
2141 else
2142 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2144 -- The component type should have an invariant procedure
2145 -- if it has invariants of its own or inherits class-wide
2146 -- invariants from parent or interface types.
2148 pragma Assert (Present (Proc_Id));
2150 -- Generate:
2151 -- <Comp_Typ>Invariant (_object (<Indices>));
2153 -- Note that the invariant procedure may have a null body if
2154 -- assertions are disabled or Assertion_Policy Ignore is in
2155 -- effect.
2157 if not Has_Null_Body (Proc_Id) then
2158 Append_New_To (Comp_Checks,
2159 Make_Procedure_Call_Statement (Loc,
2160 Name =>
2161 New_Occurrence_Of (Proc_Id, Loc),
2162 Parameter_Associations => New_List (
2163 Make_Indexed_Component (Loc,
2164 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2165 Expressions => New_Copy_List (Indices)))));
2166 end if;
2167 end if;
2169 Produced_Check := True;
2170 end if;
2171 end Process_Array_Component;
2173 ---------------------------
2174 -- Process_One_Dimension --
2175 ---------------------------
2177 procedure Process_One_Dimension
2178 (Dim : Pos;
2179 Indices : List_Id;
2180 Dim_Checks : in out List_Id)
2182 Comp_Checks : List_Id := No_List;
2183 Index : Entity_Id;
2185 begin
2186 -- Generate the invariant checks for the array component after all
2187 -- dimensions have produced their respective loops.
2189 if Dim > Dims then
2190 Process_Array_Component
2191 (Indices => Indices,
2192 Comp_Checks => Dim_Checks);
2194 -- Otherwise create a loop for the current dimension
2196 else
2197 -- Create a new loop variable for each dimension
2199 Index :=
2200 Make_Defining_Identifier (Loc,
2201 Chars => New_External_Name ('I', Dim));
2202 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2204 Process_One_Dimension
2205 (Dim => Dim + 1,
2206 Indices => Indices,
2207 Dim_Checks => Comp_Checks);
2209 -- Generate:
2210 -- for I<Dim> in _object'Range (<Dim>) loop
2211 -- <Comp_Checks>
2212 -- end loop;
2214 -- Note that the invariant procedure may have a null body if
2215 -- assertions are disabled or Assertion_Policy Ignore is in
2216 -- effect.
2218 if Present (Comp_Checks) then
2219 Append_New_To (Dim_Checks,
2220 Make_Implicit_Loop_Statement (T,
2221 Identifier => Empty,
2222 Iteration_Scheme =>
2223 Make_Iteration_Scheme (Loc,
2224 Loop_Parameter_Specification =>
2225 Make_Loop_Parameter_Specification (Loc,
2226 Defining_Identifier => Index,
2227 Discrete_Subtype_Definition =>
2228 Make_Attribute_Reference (Loc,
2229 Prefix =>
2230 New_Occurrence_Of (Obj_Id, Loc),
2231 Attribute_Name => Name_Range,
2232 Expressions => New_List (
2233 Make_Integer_Literal (Loc, Dim))))),
2234 Statements => Comp_Checks));
2235 end if;
2236 end if;
2237 end Process_One_Dimension;
2239 -- Start of processing for Add_Array_Component_Invariants
2241 begin
2242 Process_One_Dimension
2243 (Dim => 1,
2244 Indices => New_List,
2245 Dim_Checks => Checks);
2246 end Add_Array_Component_Invariants;
2248 ------------------------------
2249 -- Add_Inherited_Invariants --
2250 ------------------------------
2252 procedure Add_Inherited_Invariants
2253 (T : Entity_Id;
2254 Priv_Typ : Entity_Id;
2255 Full_Typ : Entity_Id;
2256 Obj_Id : Entity_Id;
2257 Checks : in out List_Id)
2259 Deriv_Typ : Entity_Id;
2260 Expr : Node_Id;
2261 Prag : Node_Id;
2262 Prag_Expr : Node_Id;
2263 Prag_Expr_Arg : Node_Id;
2264 Prag_Typ : Node_Id;
2265 Prag_Typ_Arg : Node_Id;
2267 Par_Proc : Entity_Id;
2268 -- The "partial" invariant procedure of Par_Typ
2270 Par_Typ : Entity_Id;
2271 -- The suitable view of the parent type used in the substitution of
2272 -- type attributes.
2274 begin
2275 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2276 return;
2277 end if;
2279 -- When the type inheriting the class-wide invariant is a concurrent
2280 -- type, use the corresponding record type because it contains all
2281 -- primitive operations of the concurrent type and allows for proper
2282 -- substitution.
2284 if Is_Concurrent_Type (T) then
2285 Deriv_Typ := Corresponding_Record_Type (T);
2286 else
2287 Deriv_Typ := T;
2288 end if;
2290 pragma Assert (Present (Deriv_Typ));
2292 -- Determine which rep item chain to use. Precedence is given to that
2293 -- of the parent type's partial view since it usually carries all the
2294 -- class-wide invariants.
2296 if Present (Priv_Typ) then
2297 Prag := First_Rep_Item (Priv_Typ);
2298 else
2299 Prag := First_Rep_Item (Full_Typ);
2300 end if;
2302 while Present (Prag) loop
2303 if Nkind (Prag) = N_Pragma
2304 and then Pragma_Name (Prag) = Name_Invariant
2305 then
2306 -- Nothing to do if the pragma was already processed
2308 if Contains (Pragmas_Seen, Prag) then
2309 return;
2311 -- Nothing to do when the caller requests the processing of all
2312 -- inherited class-wide invariants, but the pragma does not
2313 -- fall in this category.
2315 elsif not Class_Present (Prag) then
2316 return;
2317 end if;
2319 -- Extract the arguments of the invariant pragma
2321 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2322 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2323 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2324 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2326 -- The pragma applies to the partial view of the parent type
2328 if Present (Priv_Typ)
2329 and then Entity (Prag_Typ) = Priv_Typ
2330 then
2331 Par_Typ := Priv_Typ;
2333 -- The pragma applies to the full view of the parent type
2335 elsif Present (Full_Typ)
2336 and then Entity (Prag_Typ) = Full_Typ
2337 then
2338 Par_Typ := Full_Typ;
2340 -- Otherwise the pragma does not belong to the parent type and
2341 -- should not be considered.
2343 else
2344 return;
2345 end if;
2347 -- Perform the following substitutions:
2349 -- * Replace a reference to the _object parameter of the
2350 -- parent type's partial invariant procedure with a
2351 -- reference to the _object parameter of the derived
2352 -- type's full invariant procedure.
2354 -- * Replace a reference to a discriminant of the parent type
2355 -- with a suitable value from the point of view of the
2356 -- derived type.
2358 -- * Replace a call to an overridden parent primitive with a
2359 -- call to the overriding derived type primitive.
2361 -- * Replace a call to an inherited parent primitive with a
2362 -- call to the internally-generated inherited derived type
2363 -- primitive.
2365 Expr := New_Copy_Tree (Prag_Expr);
2367 -- The parent type must have a "partial" invariant procedure
2368 -- because class-wide invariants are captured exclusively by
2369 -- it.
2371 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2372 pragma Assert (Present (Par_Proc));
2374 Replace_References
2375 (Expr => Expr,
2376 Par_Typ => Par_Typ,
2377 Deriv_Typ => Deriv_Typ,
2378 Par_Obj => First_Formal (Par_Proc),
2379 Deriv_Obj => Obj_Id);
2381 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2382 end if;
2384 Next_Rep_Item (Prag);
2385 end loop;
2386 end Add_Inherited_Invariants;
2388 ------------------------------
2389 -- Add_Interface_Invariants --
2390 ------------------------------
2392 procedure Add_Interface_Invariants
2393 (T : Entity_Id;
2394 Obj_Id : Entity_Id;
2395 Checks : in out List_Id)
2397 Iface_Elmt : Elmt_Id;
2398 Ifaces : Elist_Id;
2400 begin
2401 -- Generate an invariant check for each class-wide invariant coming
2402 -- from all interfaces implemented by type T.
2404 if Is_Tagged_Type (T) then
2405 Collect_Interfaces (T, Ifaces);
2407 -- Process the class-wide invariants of all implemented interfaces
2409 Iface_Elmt := First_Elmt (Ifaces);
2410 while Present (Iface_Elmt) loop
2412 -- The Full_Typ parameter is intentionally left Empty because
2413 -- interfaces are treated as the partial view of a private type
2414 -- in order to achieve uniformity with the general case.
2416 Add_Inherited_Invariants
2417 (T => T,
2418 Priv_Typ => Node (Iface_Elmt),
2419 Full_Typ => Empty,
2420 Obj_Id => Obj_Id,
2421 Checks => Checks);
2423 Next_Elmt (Iface_Elmt);
2424 end loop;
2425 end if;
2426 end Add_Interface_Invariants;
2428 -------------------------
2429 -- Add_Invariant_Check --
2430 -------------------------
2432 procedure Add_Invariant_Check
2433 (Prag : Node_Id;
2434 Expr : Node_Id;
2435 Checks : in out List_Id;
2436 Inherited : Boolean := False)
2438 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2439 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2440 Ploc : constant Source_Ptr := Sloc (Prag);
2441 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2443 Assoc : List_Id;
2444 Str : String_Id;
2446 begin
2447 -- The invariant is ignored, nothing left to do
2449 if Is_Ignored (Prag) then
2450 null;
2452 -- Otherwise the invariant is checked. Build a pragma Check to verify
2453 -- the expression at run time.
2455 else
2456 Assoc := New_List (
2457 Make_Pragma_Argument_Association (Ploc,
2458 Expression => Make_Identifier (Ploc, Nam)),
2459 Make_Pragma_Argument_Association (Ploc,
2460 Expression => Expr));
2462 -- Handle the String argument (if any)
2464 if Present (Str_Arg) then
2465 Str := Strval (Get_Pragma_Arg (Str_Arg));
2467 -- When inheriting an invariant, modify the message from
2468 -- "failed invariant" to "failed inherited invariant".
2470 if Inherited then
2471 String_To_Name_Buffer (Str);
2473 if Name_Buffer (1 .. 16) = "failed invariant" then
2474 Insert_Str_In_Name_Buffer ("inherited ", 8);
2475 Str := String_From_Name_Buffer;
2476 end if;
2477 end if;
2479 Append_To (Assoc,
2480 Make_Pragma_Argument_Association (Ploc,
2481 Expression => Make_String_Literal (Ploc, Str)));
2482 end if;
2484 -- Generate:
2485 -- pragma Check (<Nam>, <Expr>, <Str>);
2487 Append_New_To (Checks,
2488 Make_Pragma (Ploc,
2489 Chars => Name_Check,
2490 Pragma_Argument_Associations => Assoc));
2491 end if;
2493 -- Output an info message when inheriting an invariant and the
2494 -- listing option is enabled.
2496 if Inherited and Opt.List_Inherited_Aspects then
2497 Error_Msg_Sloc := Sloc (Prag);
2498 Error_Msg_N
2499 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
2500 end if;
2502 -- Add the pragma to the list of processed pragmas
2504 Append_New_Elmt (Prag, Pragmas_Seen);
2505 Produced_Check := True;
2506 end Add_Invariant_Check;
2508 ---------------------------
2509 -- Add_Parent_Invariants --
2510 ---------------------------
2512 procedure Add_Parent_Invariants
2513 (T : Entity_Id;
2514 Obj_Id : Entity_Id;
2515 Checks : in out List_Id)
2517 Dummy_1 : Entity_Id;
2518 Dummy_2 : Entity_Id;
2520 Curr_Typ : Entity_Id;
2521 -- The entity of the current type being examined
2523 Full_Typ : Entity_Id;
2524 -- The full view of Par_Typ
2526 Par_Typ : Entity_Id;
2527 -- The entity of the parent type
2529 Priv_Typ : Entity_Id;
2530 -- The partial view of Par_Typ
2532 begin
2533 -- Do not process array types because they cannot have true parent
2534 -- types. This also prevents the generation of a duplicate invariant
2535 -- check when the input type is an array base type because its Etype
2536 -- denotes the first subtype, both of which share the same component
2537 -- type.
2539 if Is_Array_Type (T) then
2540 return;
2541 end if;
2543 -- Climb the parent type chain
2545 Curr_Typ := T;
2546 loop
2547 -- Do not consider subtypes as they inherit the invariants
2548 -- from their base types.
2550 Par_Typ := Base_Type (Etype (Curr_Typ));
2552 -- Stop the climb once the root of the parent chain is
2553 -- reached.
2555 exit when Curr_Typ = Par_Typ;
2557 -- Process the class-wide invariants of the parent type
2559 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2561 -- Process the elements of an array type
2563 if Is_Array_Type (Full_Typ) then
2564 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2566 -- Process the components of a record type
2568 elsif Ekind (Full_Typ) = E_Record_Type then
2569 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2570 end if;
2572 Add_Inherited_Invariants
2573 (T => T,
2574 Priv_Typ => Priv_Typ,
2575 Full_Typ => Full_Typ,
2576 Obj_Id => Obj_Id,
2577 Checks => Checks);
2579 Curr_Typ := Par_Typ;
2580 end loop;
2581 end Add_Parent_Invariants;
2583 ------------------------
2584 -- Add_Own_Invariants --
2585 ------------------------
2587 procedure Add_Own_Invariants
2588 (T : Entity_Id;
2589 Obj_Id : Entity_Id;
2590 Checks : in out List_Id;
2591 Priv_Item : Node_Id := Empty)
2593 ASIS_Expr : Node_Id;
2594 Expr : Node_Id;
2595 Prag : Node_Id;
2596 Prag_Asp : Node_Id;
2597 Prag_Expr : Node_Id;
2598 Prag_Expr_Arg : Node_Id;
2599 Prag_Typ : Node_Id;
2600 Prag_Typ_Arg : Node_Id;
2602 begin
2603 if not Present (T) then
2604 return;
2605 end if;
2607 Prag := First_Rep_Item (T);
2608 while Present (Prag) loop
2609 if Nkind (Prag) = N_Pragma
2610 and then Pragma_Name (Prag) = Name_Invariant
2611 then
2612 -- Stop the traversal of the rep item chain once a specific
2613 -- item is encountered.
2615 if Present (Priv_Item) and then Prag = Priv_Item then
2616 exit;
2617 end if;
2619 -- Nothing to do if the pragma was already processed
2621 if Contains (Pragmas_Seen, Prag) then
2622 return;
2623 end if;
2625 -- Extract the arguments of the invariant pragma
2627 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2628 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2629 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2630 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2631 Prag_Asp := Corresponding_Aspect (Prag);
2633 -- Verify the pragma belongs to T, otherwise the pragma applies
2634 -- to a parent type in which case it will be processed later by
2635 -- Add_Parent_Invariants or Add_Interface_Invariants.
2637 if Entity (Prag_Typ) /= T then
2638 return;
2639 end if;
2641 Expr := New_Copy_Tree (Prag_Expr);
2643 -- Substitute all references to type T with references to the
2644 -- _object formal parameter.
2646 Replace_Type_References (Expr, T, Obj_Id);
2648 -- Preanalyze the invariant expression to detect errors and at
2649 -- the same time capture the visibility of the proper package
2650 -- part.
2652 Set_Parent (Expr, Parent (Prag_Expr));
2653 Preanalyze_Assert_Expression (Expr, Any_Boolean);
2655 -- Save a copy of the expression when T is tagged to detect
2656 -- errors and capture the visibility of the proper package part
2657 -- for the generation of inherited type invariants.
2659 if Is_Tagged_Type (T) then
2660 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
2661 end if;
2663 -- If the pragma comes from an aspect specification, replace
2664 -- the saved expression because all type references must be
2665 -- substituted for the call to Preanalyze_Spec_Expression in
2666 -- Check_Aspect_At_xxx routines.
2668 if Present (Prag_Asp) then
2669 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
2670 end if;
2672 -- Analyze the original invariant expression for ASIS
2674 if ASIS_Mode then
2675 ASIS_Expr := Empty;
2677 if Comes_From_Source (Prag) then
2678 ASIS_Expr := Prag_Expr;
2679 elsif Present (Prag_Asp) then
2680 ASIS_Expr := Expression (Prag_Asp);
2681 end if;
2683 if Present (ASIS_Expr) then
2684 Replace_Type_References (ASIS_Expr, T, Obj_Id);
2685 Preanalyze_Assert_Expression (ASIS_Expr, Any_Boolean);
2686 end if;
2687 end if;
2689 Add_Invariant_Check (Prag, Expr, Checks);
2690 end if;
2692 Next_Rep_Item (Prag);
2693 end loop;
2694 end Add_Own_Invariants;
2696 -------------------------------------
2697 -- Add_Record_Component_Invariants --
2698 -------------------------------------
2700 procedure Add_Record_Component_Invariants
2701 (T : Entity_Id;
2702 Obj_Id : Entity_Id;
2703 Checks : in out List_Id)
2705 procedure Process_Component_List
2706 (Comp_List : Node_Id;
2707 CL_Checks : in out List_Id);
2708 -- Generate invariant checks for all record components found in
2709 -- component list Comp_List, including variant parts. All created
2710 -- checks are added to list CL_Checks.
2712 procedure Process_Record_Component
2713 (Comp_Id : Entity_Id;
2714 Comp_Checks : in out List_Id);
2715 -- Generate an invariant check for a record component identified by
2716 -- Comp_Id. All created checks are added to list Comp_Checks.
2718 ----------------------------
2719 -- Process_Component_List --
2720 ----------------------------
2722 procedure Process_Component_List
2723 (Comp_List : Node_Id;
2724 CL_Checks : in out List_Id)
2726 Comp : Node_Id;
2727 Var : Node_Id;
2728 Var_Alts : List_Id := No_List;
2729 Var_Checks : List_Id := No_List;
2730 Var_Stmts : List_Id;
2732 Produced_Variant_Check : Boolean := False;
2733 -- This flag tracks whether the component has produced at least
2734 -- one invariant check.
2736 begin
2737 -- Traverse the component items
2739 Comp := First (Component_Items (Comp_List));
2740 while Present (Comp) loop
2741 if Nkind (Comp) = N_Component_Declaration then
2743 -- Generate the component invariant check
2745 Process_Record_Component
2746 (Comp_Id => Defining_Entity (Comp),
2747 Comp_Checks => CL_Checks);
2748 end if;
2750 Next (Comp);
2751 end loop;
2753 -- Traverse the variant part
2755 if Present (Variant_Part (Comp_List)) then
2756 Var := First (Variants (Variant_Part (Comp_List)));
2757 while Present (Var) loop
2758 Var_Checks := No_List;
2760 -- Generate invariant checks for all components and variant
2761 -- parts that qualify.
2763 Process_Component_List
2764 (Comp_List => Component_List (Var),
2765 CL_Checks => Var_Checks);
2767 -- The components of the current variant produced at least
2768 -- one invariant check.
2770 if Present (Var_Checks) then
2771 Var_Stmts := Var_Checks;
2772 Produced_Variant_Check := True;
2774 -- Otherwise there are either no components with invariants,
2775 -- assertions are disabled, or Assertion_Policy Ignore is in
2776 -- effect.
2778 else
2779 Var_Stmts := New_List (Make_Null_Statement (Loc));
2780 end if;
2782 Append_New_To (Var_Alts,
2783 Make_Case_Statement_Alternative (Loc,
2784 Discrete_Choices =>
2785 New_Copy_List (Discrete_Choices (Var)),
2786 Statements => Var_Stmts));
2788 Next (Var);
2789 end loop;
2791 -- Create a case statement which verifies the invariant checks
2792 -- of a particular component list depending on the discriminant
2793 -- values only when there is at least one real invariant check.
2795 if Produced_Variant_Check then
2796 Append_New_To (CL_Checks,
2797 Make_Case_Statement (Loc,
2798 Expression =>
2799 Make_Selected_Component (Loc,
2800 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2801 Selector_Name =>
2802 New_Occurrence_Of
2803 (Entity (Name (Variant_Part (Comp_List))), Loc)),
2804 Alternatives => Var_Alts));
2805 end if;
2806 end if;
2807 end Process_Component_List;
2809 ------------------------------
2810 -- Process_Record_Component --
2811 ------------------------------
2813 procedure Process_Record_Component
2814 (Comp_Id : Entity_Id;
2815 Comp_Checks : in out List_Id)
2817 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
2818 Proc_Id : Entity_Id;
2820 Produced_Component_Check : Boolean := False;
2821 -- This flag tracks whether the component has produced at least
2822 -- one invariant check.
2824 begin
2825 -- Nothing to do for internal component _parent. Note that it is
2826 -- not desirable to check whether the component comes from source
2827 -- because protected type components are relocated to an internal
2828 -- corresponding record, but still need processing.
2830 if Chars (Comp_Id) = Name_uParent then
2831 return;
2832 end if;
2834 -- Verify the invariant of the component. Note that an access
2835 -- type may have an invariant when it acts as the full view of a
2836 -- private type and the invariant appears on the partial view. In
2837 -- this case verify the access value itself.
2839 if Has_Invariants (Comp_Typ) then
2841 -- In GNATprove mode, the component invariants are checked by
2842 -- other means. They should not be added to the record type
2843 -- invariant procedure, so that the procedure can be used to
2844 -- check the record type invariants if any.
2846 if GNATprove_Mode then
2847 null;
2849 else
2850 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2852 -- The component type should have an invariant procedure
2853 -- if it has invariants of its own or inherits class-wide
2854 -- invariants from parent or interface types.
2856 pragma Assert (Present (Proc_Id));
2858 -- Generate:
2859 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
2861 -- Note that the invariant procedure may have a null body if
2862 -- assertions are disabled or Assertion_Policy Ignore is in
2863 -- effect.
2865 if not Has_Null_Body (Proc_Id) then
2866 Append_New_To (Comp_Checks,
2867 Make_Procedure_Call_Statement (Loc,
2868 Name =>
2869 New_Occurrence_Of (Proc_Id, Loc),
2870 Parameter_Associations => New_List (
2871 Make_Selected_Component (Loc,
2872 Prefix =>
2873 Unchecked_Convert_To
2874 (T, New_Occurrence_Of (Obj_Id, Loc)),
2875 Selector_Name =>
2876 New_Occurrence_Of (Comp_Id, Loc)))));
2877 end if;
2878 end if;
2880 Produced_Check := True;
2881 Produced_Component_Check := True;
2882 end if;
2884 if Produced_Component_Check and then Has_Unchecked_Union (T) then
2885 Error_Msg_NE
2886 ("invariants cannot be checked on components of "
2887 & "unchecked_union type &?", Comp_Id, T);
2888 end if;
2889 end Process_Record_Component;
2891 -- Local variables
2893 Comps : Node_Id;
2894 Def : Node_Id;
2896 -- Start of processing for Add_Record_Component_Invariants
2898 begin
2899 -- An untagged derived type inherits the components of its parent
2900 -- type. In order to avoid creating redundant invariant checks, do
2901 -- not process the components now. Instead wait until the ultimate
2902 -- parent of the untagged derivation chain is reached.
2904 if not Is_Untagged_Derivation (T) then
2905 Def := Type_Definition (Parent (T));
2907 if Nkind (Def) = N_Derived_Type_Definition then
2908 Def := Record_Extension_Part (Def);
2909 end if;
2911 pragma Assert (Nkind (Def) = N_Record_Definition);
2912 Comps := Component_List (Def);
2914 if Present (Comps) then
2915 Process_Component_List
2916 (Comp_List => Comps,
2917 CL_Checks => Checks);
2918 end if;
2919 end if;
2920 end Add_Record_Component_Invariants;
2922 -- Local variables
2924 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2925 -- Save the Ghost mode to restore on exit
2927 Dummy : Entity_Id;
2928 Priv_Item : Node_Id;
2929 Proc_Body : Node_Id;
2930 Proc_Body_Id : Entity_Id;
2931 Proc_Decl : Node_Id;
2932 Proc_Id : Entity_Id;
2933 Stmts : List_Id := No_List;
2935 CRec_Typ : Entity_Id := Empty;
2936 -- The corresponding record type of Full_Typ
2938 Full_Proc : Entity_Id := Empty;
2939 -- The entity of the "full" invariant procedure
2941 Full_Typ : Entity_Id := Empty;
2942 -- The full view of the working type
2944 Obj_Id : Entity_Id := Empty;
2945 -- The _object formal parameter of the invariant procedure
2947 Part_Proc : Entity_Id := Empty;
2948 -- The entity of the "partial" invariant procedure
2950 Priv_Typ : Entity_Id := Empty;
2951 -- The partial view of the working type
2953 Work_Typ : Entity_Id := Empty;
2954 -- The working type
2956 -- Start of processing for Build_Invariant_Procedure_Body
2958 begin
2959 Work_Typ := Typ;
2961 -- The input type denotes the implementation base type of a constrained
2962 -- array type. Work with the first subtype as all invariant pragmas are
2963 -- on its rep item chain.
2965 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
2966 Work_Typ := First_Subtype (Work_Typ);
2968 -- The input type denotes the corresponding record type of a protected
2969 -- or task type. Work with the concurrent type because the corresponding
2970 -- record type may not be visible to clients of the type.
2972 elsif Ekind (Work_Typ) = E_Record_Type
2973 and then Is_Concurrent_Record_Type (Work_Typ)
2974 then
2975 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2976 end if;
2978 -- The working type may be subject to pragma Ghost. Set the mode now to
2979 -- ensure that the invariant procedure is properly marked as Ghost.
2981 Set_Ghost_Mode (Work_Typ);
2983 -- The type must either have invariants of its own, inherit class-wide
2984 -- invariants from parent types or interfaces, or be an array or record
2985 -- type whose components have invariants.
2987 pragma Assert (Has_Invariants (Work_Typ));
2989 -- Interfaces are treated as the partial view of a private type in order
2990 -- to achieve uniformity with the general case.
2992 if Is_Interface (Work_Typ) then
2993 Priv_Typ := Work_Typ;
2995 -- Otherwise obtain both views of the type
2997 else
2998 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
2999 end if;
3001 -- The caller requests a body for the partial invariant procedure
3003 if Partial_Invariant then
3004 Full_Proc := Invariant_Procedure (Work_Typ);
3005 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3007 -- The "full" invariant procedure body was already created
3009 if Present (Full_Proc)
3010 and then Present
3011 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3012 then
3013 -- This scenario happens only when the type is an untagged
3014 -- derivation from a private parent and the underlying full
3015 -- view was processed before the partial view.
3017 pragma Assert
3018 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3020 -- Nothing to do because the processing of the underlying full
3021 -- view already checked the invariants of the partial view.
3023 goto Leave;
3024 end if;
3026 -- Create a declaration for the "partial" invariant procedure if it
3027 -- is not available.
3029 if No (Proc_Id) then
3030 Build_Invariant_Procedure_Declaration
3031 (Typ => Work_Typ,
3032 Partial_Invariant => True);
3034 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3035 end if;
3037 -- The caller requests a body for the "full" invariant procedure
3039 else
3040 Proc_Id := Invariant_Procedure (Work_Typ);
3041 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3043 -- Create a declaration for the "full" invariant procedure if it is
3044 -- not available.
3046 if No (Proc_Id) then
3047 Build_Invariant_Procedure_Declaration (Work_Typ);
3048 Proc_Id := Invariant_Procedure (Work_Typ);
3049 end if;
3050 end if;
3052 -- At this point there should be an invariant procedure declaration
3054 pragma Assert (Present (Proc_Id));
3055 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3057 -- Nothing to do if the invariant procedure already has a body
3059 if Present (Corresponding_Body (Proc_Decl)) then
3060 goto Leave;
3061 end if;
3063 -- Emulate the environment of the invariant procedure by installing its
3064 -- scope and formal parameters. Note that this is not needed, but having
3065 -- the scope installed helps with the detection of invariant-related
3066 -- errors.
3068 Push_Scope (Proc_Id);
3069 Install_Formals (Proc_Id);
3071 Obj_Id := First_Formal (Proc_Id);
3072 pragma Assert (Present (Obj_Id));
3074 -- The "partial" invariant procedure verifies the invariants of the
3075 -- partial view only.
3077 if Partial_Invariant then
3078 pragma Assert (Present (Priv_Typ));
3080 Add_Own_Invariants
3081 (T => Priv_Typ,
3082 Obj_Id => Obj_Id,
3083 Checks => Stmts);
3085 -- Otherwise the "full" invariant procedure verifies the invariants of
3086 -- the full view, all array or record components, as well as class-wide
3087 -- invariants inherited from parent types or interfaces. In addition, it
3088 -- indirectly verifies the invariants of the partial view by calling the
3089 -- "partial" invariant procedure.
3091 else
3092 pragma Assert (Present (Full_Typ));
3094 -- Check the invariants of the partial view by calling the "partial"
3095 -- invariant procedure. Generate:
3097 -- <Work_Typ>Partial_Invariant (_object);
3099 if Present (Part_Proc) then
3100 Append_New_To (Stmts,
3101 Make_Procedure_Call_Statement (Loc,
3102 Name => New_Occurrence_Of (Part_Proc, Loc),
3103 Parameter_Associations => New_List (
3104 New_Occurrence_Of (Obj_Id, Loc))));
3106 Produced_Check := True;
3107 end if;
3109 Priv_Item := Empty;
3111 -- Derived subtypes do not have a partial view
3113 if Present (Priv_Typ) then
3115 -- The processing of the "full" invariant procedure intentionally
3116 -- skips the partial view because a) this may result in changes of
3117 -- visibility and b) lead to duplicate checks. However, when the
3118 -- full view is the underlying full view of an untagged derived
3119 -- type whose parent type is private, partial invariants appear on
3120 -- the rep item chain of the partial view only.
3122 -- package Pack_1 is
3123 -- type Root ... is private;
3124 -- private
3125 -- <full view of Root>
3126 -- end Pack_1;
3128 -- with Pack_1;
3129 -- package Pack_2 is
3130 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3131 -- <underlying full view of Child>
3132 -- end Pack_2;
3134 -- As a result, the processing of the full view must also consider
3135 -- all invariants of the partial view.
3137 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3138 null;
3140 -- Otherwise the invariants of the partial view are ignored
3142 else
3143 -- Note that the rep item chain is shared between the partial
3144 -- and full views of a type. To avoid processing the invariants
3145 -- of the partial view, signal the logic to stop when the first
3146 -- rep item of the partial view has been reached.
3148 Priv_Item := First_Rep_Item (Priv_Typ);
3150 -- Ignore the invariants of the partial view by eliminating the
3151 -- view.
3153 Priv_Typ := Empty;
3154 end if;
3155 end if;
3157 -- Process the invariants of the full view and in certain cases those
3158 -- of the partial view. This also handles any invariants on array or
3159 -- record components.
3161 Add_Own_Invariants
3162 (T => Priv_Typ,
3163 Obj_Id => Obj_Id,
3164 Checks => Stmts,
3165 Priv_Item => Priv_Item);
3167 Add_Own_Invariants
3168 (T => Full_Typ,
3169 Obj_Id => Obj_Id,
3170 Checks => Stmts,
3171 Priv_Item => Priv_Item);
3173 -- Process the elements of an array type
3175 if Is_Array_Type (Full_Typ) then
3176 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3178 -- Process the components of a record type
3180 elsif Ekind (Full_Typ) = E_Record_Type then
3181 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3183 -- Process the components of a corresponding record
3185 elsif Present (CRec_Typ) then
3186 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3187 end if;
3189 -- Process the inherited class-wide invariants of all parent types.
3190 -- This also handles any invariants on record components.
3192 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3194 -- Process the inherited class-wide invariants of all implemented
3195 -- interface types.
3197 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3198 end if;
3200 End_Scope;
3202 -- At this point there should be at least one invariant check. If this
3203 -- is not the case, then the invariant-related flags were not properly
3204 -- set, or there is a missing invariant procedure on one of the array
3205 -- or record components.
3207 pragma Assert (Produced_Check);
3209 -- Account for the case where assertions are disabled or all invariant
3210 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3211 -- empty body.
3213 if No (Stmts) then
3214 Stmts := New_List (Make_Null_Statement (Loc));
3215 end if;
3217 -- Generate:
3218 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3219 -- begin
3220 -- <Stmts>
3221 -- end <Work_Typ>[Partial_]Invariant;
3223 Proc_Body :=
3224 Make_Subprogram_Body (Loc,
3225 Specification =>
3226 Copy_Subprogram_Spec (Parent (Proc_Id)),
3227 Declarations => Empty_List,
3228 Handled_Statement_Sequence =>
3229 Make_Handled_Sequence_Of_Statements (Loc,
3230 Statements => Stmts));
3231 Proc_Body_Id := Defining_Entity (Proc_Body);
3233 -- Perform minor decoration in case the body is not analyzed
3235 Set_Ekind (Proc_Body_Id, E_Subprogram_Body);
3236 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3237 Set_Scope (Proc_Body_Id, Current_Scope);
3239 -- Link both spec and body to avoid generating duplicates
3241 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3242 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3244 -- The body should not be inserted into the tree when the context is
3245 -- ASIS or a generic unit because it is not part of the template. Note
3246 -- that the body must still be generated in order to resolve the
3247 -- invariants.
3249 if ASIS_Mode or Inside_A_Generic then
3250 null;
3252 -- Semi-insert the body into the tree for GNATprove by setting its
3253 -- Parent field. This allows for proper upstream tree traversals.
3255 elsif GNATprove_Mode then
3256 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3258 -- Otherwise the body is part of the freezing actions of the type
3260 else
3261 Append_Freeze_Action (Work_Typ, Proc_Body);
3262 end if;
3264 <<Leave>>
3265 Restore_Ghost_Mode (Saved_GM);
3266 end Build_Invariant_Procedure_Body;
3268 -------------------------------------------
3269 -- Build_Invariant_Procedure_Declaration --
3270 -------------------------------------------
3272 -- WARNING: This routine manages Ghost regions. Return statements must be
3273 -- replaced by gotos which jump to the end of the routine and restore the
3274 -- Ghost mode.
3276 procedure Build_Invariant_Procedure_Declaration
3277 (Typ : Entity_Id;
3278 Partial_Invariant : Boolean := False)
3280 Loc : constant Source_Ptr := Sloc (Typ);
3282 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3283 -- Save the Ghost mode to restore on exit
3285 Proc_Decl : Node_Id;
3286 Proc_Id : Entity_Id;
3287 Proc_Nam : Name_Id;
3288 Typ_Decl : Node_Id;
3290 CRec_Typ : Entity_Id;
3291 -- The corresponding record type of Full_Typ
3293 Full_Base : Entity_Id;
3294 -- The base type of Full_Typ
3296 Full_Typ : Entity_Id;
3297 -- The full view of working type
3299 Obj_Id : Entity_Id;
3300 -- The _object formal parameter of the invariant procedure
3302 Obj_Typ : Entity_Id;
3303 -- The type of the _object formal parameter
3305 Priv_Typ : Entity_Id;
3306 -- The partial view of working type
3308 Work_Typ : Entity_Id;
3309 -- The working type
3311 begin
3312 Work_Typ := Typ;
3314 -- The input type denotes the implementation base type of a constrained
3315 -- array type. Work with the first subtype as all invariant pragmas are
3316 -- on its rep item chain.
3318 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3319 Work_Typ := First_Subtype (Work_Typ);
3321 -- The input denotes the corresponding record type of a protected or a
3322 -- task type. Work with the concurrent type because the corresponding
3323 -- record type may not be visible to clients of the type.
3325 elsif Ekind (Work_Typ) = E_Record_Type
3326 and then Is_Concurrent_Record_Type (Work_Typ)
3327 then
3328 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3329 end if;
3331 -- The working type may be subject to pragma Ghost. Set the mode now to
3332 -- ensure that the invariant procedure is properly marked as Ghost.
3334 Set_Ghost_Mode (Work_Typ);
3336 -- The type must either have invariants of its own, inherit class-wide
3337 -- invariants from parent or interface types, or be an array or record
3338 -- type whose components have invariants.
3340 pragma Assert (Has_Invariants (Work_Typ));
3342 -- Nothing to do if the type already has a "partial" invariant procedure
3344 if Partial_Invariant then
3345 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3346 goto Leave;
3347 end if;
3349 -- Nothing to do if the type already has a "full" invariant procedure
3351 elsif Present (Invariant_Procedure (Work_Typ)) then
3352 goto Leave;
3353 end if;
3355 -- The caller requests the declaration of the "partial" invariant
3356 -- procedure.
3358 if Partial_Invariant then
3359 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3361 -- Otherwise the caller requests the declaration of the "full" invariant
3362 -- procedure.
3364 else
3365 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3366 end if;
3368 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3370 -- Perform minor decoration in case the declaration is not analyzed
3372 Set_Ekind (Proc_Id, E_Procedure);
3373 Set_Etype (Proc_Id, Standard_Void_Type);
3374 Set_Scope (Proc_Id, Current_Scope);
3376 if Partial_Invariant then
3377 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3378 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3379 else
3380 Set_Is_Invariant_Procedure (Proc_Id);
3381 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3382 end if;
3384 -- The invariant procedure requires debug info when the invariants are
3385 -- subject to Source Coverage Obligations.
3387 if Opt.Generate_SCO then
3388 Set_Needs_Debug_Info (Proc_Id);
3389 end if;
3391 -- Obtain all views of the input type
3393 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Full_Base, CRec_Typ);
3395 -- Associate the invariant procedure with all views
3397 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3398 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3399 Propagate_Invariant_Attributes (Full_Base, From_Typ => Work_Typ);
3400 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3402 -- The declaration of the invariant procedure is inserted after the
3403 -- declaration of the partial view as this allows for proper external
3404 -- visibility.
3406 if Present (Priv_Typ) then
3407 Typ_Decl := Declaration_Node (Priv_Typ);
3409 -- Anonymous arrays in object declarations have no explicit declaration
3410 -- so use the related object declaration as the insertion point.
3412 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3413 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3415 -- Derived types with the full view as parent do not have a partial
3416 -- view. Insert the invariant procedure after the derived type.
3418 else
3419 Typ_Decl := Declaration_Node (Full_Typ);
3420 end if;
3422 -- The type should have a declarative node
3424 pragma Assert (Present (Typ_Decl));
3426 -- Create the formal parameter which emulates the variable-like behavior
3427 -- of the current type instance.
3429 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3431 -- When generating an invariant procedure declaration for an abstract
3432 -- type (including interfaces), use the class-wide type as the _object
3433 -- type. This has several desirable effects:
3435 -- * The invariant procedure does not become a primitive of the type.
3436 -- This eliminates the need to either special case the treatment of
3437 -- invariant procedures, or to make it a predefined primitive and
3438 -- force every derived type to potentially provide an empty body.
3440 -- * The invariant procedure does not need to be declared as abstract.
3441 -- This allows for a proper body, which in turn avoids redundant
3442 -- processing of the same invariants for types with multiple views.
3444 -- * The class-wide type allows for calls to abstract primitives
3445 -- within a nonabstract subprogram. The calls are treated as
3446 -- dispatching and require additional processing when they are
3447 -- remapped to call primitives of derived types. See routine
3448 -- Replace_References for details.
3450 if Is_Abstract_Type (Work_Typ) then
3451 Obj_Typ := Class_Wide_Type (Work_Typ);
3452 else
3453 Obj_Typ := Work_Typ;
3454 end if;
3456 -- Perform minor decoration in case the declaration is not analyzed
3458 Set_Ekind (Obj_Id, E_In_Parameter);
3459 Set_Etype (Obj_Id, Obj_Typ);
3460 Set_Scope (Obj_Id, Proc_Id);
3462 Set_First_Entity (Proc_Id, Obj_Id);
3464 -- Generate:
3465 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3467 Proc_Decl :=
3468 Make_Subprogram_Declaration (Loc,
3469 Specification =>
3470 Make_Procedure_Specification (Loc,
3471 Defining_Unit_Name => Proc_Id,
3472 Parameter_Specifications => New_List (
3473 Make_Parameter_Specification (Loc,
3474 Defining_Identifier => Obj_Id,
3475 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3477 -- The declaration should not be inserted into the tree when the context
3478 -- is ASIS or a generic unit because it is not part of the template.
3480 if ASIS_Mode or Inside_A_Generic then
3481 null;
3483 -- Semi-insert the declaration into the tree for GNATprove by setting
3484 -- its Parent field. This allows for proper upstream tree traversals.
3486 elsif GNATprove_Mode then
3487 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3489 -- Otherwise insert the declaration
3491 else
3492 pragma Assert (Present (Typ_Decl));
3493 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3494 end if;
3496 <<Leave>>
3497 Restore_Ghost_Mode (Saved_GM);
3498 end Build_Invariant_Procedure_Declaration;
3500 --------------------------
3501 -- Build_Procedure_Form --
3502 --------------------------
3504 procedure Build_Procedure_Form (N : Node_Id) is
3505 Loc : constant Source_Ptr := Sloc (N);
3506 Subp : constant Entity_Id := Defining_Entity (N);
3508 Func_Formal : Entity_Id;
3509 Proc_Formals : List_Id;
3510 Proc_Decl : Node_Id;
3512 begin
3513 -- No action needed if this transformation was already done, or in case
3514 -- of subprogram renaming declarations.
3516 if Nkind (Specification (N)) = N_Procedure_Specification
3517 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3518 then
3519 return;
3520 end if;
3522 -- Ditto when dealing with an expression function, where both the
3523 -- original expression and the generated declaration end up being
3524 -- expanded here.
3526 if Rewritten_For_C (Subp) then
3527 return;
3528 end if;
3530 Proc_Formals := New_List;
3532 -- Create a list of formal parameters with the same types as the
3533 -- function.
3535 Func_Formal := First_Formal (Subp);
3536 while Present (Func_Formal) loop
3537 Append_To (Proc_Formals,
3538 Make_Parameter_Specification (Loc,
3539 Defining_Identifier =>
3540 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3541 Parameter_Type =>
3542 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3544 Next_Formal (Func_Formal);
3545 end loop;
3547 -- Add an extra out parameter to carry the function result
3549 Name_Len := 6;
3550 Name_Buffer (1 .. Name_Len) := "RESULT";
3551 Append_To (Proc_Formals,
3552 Make_Parameter_Specification (Loc,
3553 Defining_Identifier =>
3554 Make_Defining_Identifier (Loc, Chars => Name_Find),
3555 Out_Present => True,
3556 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3558 -- The new procedure declaration is inserted immediately after the
3559 -- function declaration. The processing in Build_Procedure_Body_Form
3560 -- relies on this order.
3562 Proc_Decl :=
3563 Make_Subprogram_Declaration (Loc,
3564 Specification =>
3565 Make_Procedure_Specification (Loc,
3566 Defining_Unit_Name =>
3567 Make_Defining_Identifier (Loc, Chars (Subp)),
3568 Parameter_Specifications => Proc_Formals));
3570 Insert_After_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3572 -- Entity of procedure must remain invisible so that it does not
3573 -- overload subsequent references to the original function.
3575 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3577 -- Mark the function as having a procedure form and link the function
3578 -- and its internally built procedure.
3580 Set_Rewritten_For_C (Subp);
3581 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3582 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3583 end Build_Procedure_Form;
3585 ------------------------
3586 -- Build_Runtime_Call --
3587 ------------------------
3589 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3590 begin
3591 -- If entity is not available, we can skip making the call (this avoids
3592 -- junk duplicated error messages in a number of cases).
3594 if not RTE_Available (RE) then
3595 return Make_Null_Statement (Loc);
3596 else
3597 return
3598 Make_Procedure_Call_Statement (Loc,
3599 Name => New_Occurrence_Of (RTE (RE), Loc));
3600 end if;
3601 end Build_Runtime_Call;
3603 ------------------------
3604 -- Build_SS_Mark_Call --
3605 ------------------------
3607 function Build_SS_Mark_Call
3608 (Loc : Source_Ptr;
3609 Mark : Entity_Id) return Node_Id
3611 begin
3612 -- Generate:
3613 -- Mark : constant Mark_Id := SS_Mark;
3615 return
3616 Make_Object_Declaration (Loc,
3617 Defining_Identifier => Mark,
3618 Constant_Present => True,
3619 Object_Definition =>
3620 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3621 Expression =>
3622 Make_Function_Call (Loc,
3623 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3624 end Build_SS_Mark_Call;
3626 ---------------------------
3627 -- Build_SS_Release_Call --
3628 ---------------------------
3630 function Build_SS_Release_Call
3631 (Loc : Source_Ptr;
3632 Mark : Entity_Id) return Node_Id
3634 begin
3635 -- Generate:
3636 -- SS_Release (Mark);
3638 return
3639 Make_Procedure_Call_Statement (Loc,
3640 Name =>
3641 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3642 Parameter_Associations => New_List (
3643 New_Occurrence_Of (Mark, Loc)));
3644 end Build_SS_Release_Call;
3646 ----------------------------
3647 -- Build_Task_Array_Image --
3648 ----------------------------
3650 -- This function generates the body for a function that constructs the
3651 -- image string for a task that is an array component. The function is
3652 -- local to the init proc for the array type, and is called for each one
3653 -- of the components. The constructed image has the form of an indexed
3654 -- component, whose prefix is the outer variable of the array type.
3655 -- The n-dimensional array type has known indexes Index, Index2...
3657 -- Id_Ref is an indexed component form created by the enclosing init proc.
3658 -- Its successive indexes are Val1, Val2, ... which are the loop variables
3659 -- in the loops that call the individual task init proc on each component.
3661 -- The generated function has the following structure:
3663 -- function F return String is
3664 -- Pref : string renames Task_Name;
3665 -- T1 : String := Index1'Image (Val1);
3666 -- ...
3667 -- Tn : String := indexn'image (Valn);
3668 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
3669 -- -- Len includes commas and the end parentheses.
3670 -- Res : String (1..Len);
3671 -- Pos : Integer := Pref'Length;
3673 -- begin
3674 -- Res (1 .. Pos) := Pref;
3675 -- Pos := Pos + 1;
3676 -- Res (Pos) := '(';
3677 -- Pos := Pos + 1;
3678 -- Res (Pos .. Pos + T1'Length - 1) := T1;
3679 -- Pos := Pos + T1'Length;
3680 -- Res (Pos) := '.';
3681 -- Pos := Pos + 1;
3682 -- ...
3683 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
3684 -- Res (Len) := ')';
3686 -- return Res;
3687 -- end F;
3689 -- Needless to say, multidimensional arrays of tasks are rare enough that
3690 -- the bulkiness of this code is not really a concern.
3692 function Build_Task_Array_Image
3693 (Loc : Source_Ptr;
3694 Id_Ref : Node_Id;
3695 A_Type : Entity_Id;
3696 Dyn : Boolean := False) return Node_Id
3698 Dims : constant Nat := Number_Dimensions (A_Type);
3699 -- Number of dimensions for array of tasks
3701 Temps : array (1 .. Dims) of Entity_Id;
3702 -- Array of temporaries to hold string for each index
3704 Indx : Node_Id;
3705 -- Index expression
3707 Len : Entity_Id;
3708 -- Total length of generated name
3710 Pos : Entity_Id;
3711 -- Running index for substring assignments
3713 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
3714 -- Name of enclosing variable, prefix of resulting name
3716 Res : Entity_Id;
3717 -- String to hold result
3719 Val : Node_Id;
3720 -- Value of successive indexes
3722 Sum : Node_Id;
3723 -- Expression to compute total size of string
3725 T : Entity_Id;
3726 -- Entity for name at one index position
3728 Decls : constant List_Id := New_List;
3729 Stats : constant List_Id := New_List;
3731 begin
3732 -- For a dynamic task, the name comes from the target variable. For a
3733 -- static one it is a formal of the enclosing init proc.
3735 if Dyn then
3736 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
3737 Append_To (Decls,
3738 Make_Object_Declaration (Loc,
3739 Defining_Identifier => Pref,
3740 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3741 Expression =>
3742 Make_String_Literal (Loc,
3743 Strval => String_From_Name_Buffer)));
3745 else
3746 Append_To (Decls,
3747 Make_Object_Renaming_Declaration (Loc,
3748 Defining_Identifier => Pref,
3749 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
3750 Name => Make_Identifier (Loc, Name_uTask_Name)));
3751 end if;
3753 Indx := First_Index (A_Type);
3754 Val := First (Expressions (Id_Ref));
3756 for J in 1 .. Dims loop
3757 T := Make_Temporary (Loc, 'T');
3758 Temps (J) := T;
3760 Append_To (Decls,
3761 Make_Object_Declaration (Loc,
3762 Defining_Identifier => T,
3763 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3764 Expression =>
3765 Make_Attribute_Reference (Loc,
3766 Attribute_Name => Name_Image,
3767 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
3768 Expressions => New_List (New_Copy_Tree (Val)))));
3770 Next_Index (Indx);
3771 Next (Val);
3772 end loop;
3774 Sum := Make_Integer_Literal (Loc, Dims + 1);
3776 Sum :=
3777 Make_Op_Add (Loc,
3778 Left_Opnd => Sum,
3779 Right_Opnd =>
3780 Make_Attribute_Reference (Loc,
3781 Attribute_Name => Name_Length,
3782 Prefix => New_Occurrence_Of (Pref, Loc),
3783 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3785 for J in 1 .. Dims loop
3786 Sum :=
3787 Make_Op_Add (Loc,
3788 Left_Opnd => Sum,
3789 Right_Opnd =>
3790 Make_Attribute_Reference (Loc,
3791 Attribute_Name => Name_Length,
3792 Prefix =>
3793 New_Occurrence_Of (Temps (J), Loc),
3794 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
3795 end loop;
3797 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
3799 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
3801 Append_To (Stats,
3802 Make_Assignment_Statement (Loc,
3803 Name =>
3804 Make_Indexed_Component (Loc,
3805 Prefix => New_Occurrence_Of (Res, Loc),
3806 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3807 Expression =>
3808 Make_Character_Literal (Loc,
3809 Chars => Name_Find,
3810 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
3812 Append_To (Stats,
3813 Make_Assignment_Statement (Loc,
3814 Name => New_Occurrence_Of (Pos, Loc),
3815 Expression =>
3816 Make_Op_Add (Loc,
3817 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3818 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3820 for J in 1 .. Dims loop
3822 Append_To (Stats,
3823 Make_Assignment_Statement (Loc,
3824 Name =>
3825 Make_Slice (Loc,
3826 Prefix => New_Occurrence_Of (Res, Loc),
3827 Discrete_Range =>
3828 Make_Range (Loc,
3829 Low_Bound => New_Occurrence_Of (Pos, Loc),
3830 High_Bound =>
3831 Make_Op_Subtract (Loc,
3832 Left_Opnd =>
3833 Make_Op_Add (Loc,
3834 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3835 Right_Opnd =>
3836 Make_Attribute_Reference (Loc,
3837 Attribute_Name => Name_Length,
3838 Prefix =>
3839 New_Occurrence_Of (Temps (J), Loc),
3840 Expressions =>
3841 New_List (Make_Integer_Literal (Loc, 1)))),
3842 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
3844 Expression => New_Occurrence_Of (Temps (J), Loc)));
3846 if J < Dims then
3847 Append_To (Stats,
3848 Make_Assignment_Statement (Loc,
3849 Name => New_Occurrence_Of (Pos, Loc),
3850 Expression =>
3851 Make_Op_Add (Loc,
3852 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3853 Right_Opnd =>
3854 Make_Attribute_Reference (Loc,
3855 Attribute_Name => Name_Length,
3856 Prefix => New_Occurrence_Of (Temps (J), Loc),
3857 Expressions =>
3858 New_List (Make_Integer_Literal (Loc, 1))))));
3860 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
3862 Append_To (Stats,
3863 Make_Assignment_Statement (Loc,
3864 Name => Make_Indexed_Component (Loc,
3865 Prefix => New_Occurrence_Of (Res, Loc),
3866 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
3867 Expression =>
3868 Make_Character_Literal (Loc,
3869 Chars => Name_Find,
3870 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
3872 Append_To (Stats,
3873 Make_Assignment_Statement (Loc,
3874 Name => New_Occurrence_Of (Pos, Loc),
3875 Expression =>
3876 Make_Op_Add (Loc,
3877 Left_Opnd => New_Occurrence_Of (Pos, Loc),
3878 Right_Opnd => Make_Integer_Literal (Loc, 1))));
3879 end if;
3880 end loop;
3882 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
3884 Append_To (Stats,
3885 Make_Assignment_Statement (Loc,
3886 Name =>
3887 Make_Indexed_Component (Loc,
3888 Prefix => New_Occurrence_Of (Res, Loc),
3889 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
3890 Expression =>
3891 Make_Character_Literal (Loc,
3892 Chars => Name_Find,
3893 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
3894 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
3895 end Build_Task_Array_Image;
3897 ----------------------------
3898 -- Build_Task_Image_Decls --
3899 ----------------------------
3901 function Build_Task_Image_Decls
3902 (Loc : Source_Ptr;
3903 Id_Ref : Node_Id;
3904 A_Type : Entity_Id;
3905 In_Init_Proc : Boolean := False) return List_Id
3907 Decls : constant List_Id := New_List;
3908 T_Id : Entity_Id := Empty;
3909 Decl : Node_Id;
3910 Expr : Node_Id := Empty;
3911 Fun : Node_Id := Empty;
3912 Is_Dyn : constant Boolean :=
3913 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
3914 and then
3915 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
3917 begin
3918 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
3919 -- generate a dummy declaration only.
3921 if Restriction_Active (No_Implicit_Heap_Allocations)
3922 or else Global_Discard_Names
3923 then
3924 T_Id := Make_Temporary (Loc, 'J');
3925 Name_Len := 0;
3927 return
3928 New_List (
3929 Make_Object_Declaration (Loc,
3930 Defining_Identifier => T_Id,
3931 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3932 Expression =>
3933 Make_String_Literal (Loc,
3934 Strval => String_From_Name_Buffer)));
3936 else
3937 if Nkind (Id_Ref) = N_Identifier
3938 or else Nkind (Id_Ref) = N_Defining_Identifier
3939 then
3940 -- For a simple variable, the image of the task is built from
3941 -- the name of the variable. To avoid possible conflict with the
3942 -- anonymous type created for a single protected object, add a
3943 -- numeric suffix.
3945 T_Id :=
3946 Make_Defining_Identifier (Loc,
3947 New_External_Name (Chars (Id_Ref), 'T', 1));
3949 Get_Name_String (Chars (Id_Ref));
3951 Expr :=
3952 Make_String_Literal (Loc,
3953 Strval => String_From_Name_Buffer);
3955 elsif Nkind (Id_Ref) = N_Selected_Component then
3956 T_Id :=
3957 Make_Defining_Identifier (Loc,
3958 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
3959 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
3961 elsif Nkind (Id_Ref) = N_Indexed_Component then
3962 T_Id :=
3963 Make_Defining_Identifier (Loc,
3964 New_External_Name (Chars (A_Type), 'N'));
3966 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
3967 end if;
3968 end if;
3970 if Present (Fun) then
3971 Append (Fun, Decls);
3972 Expr := Make_Function_Call (Loc,
3973 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
3975 if not In_Init_Proc then
3976 Set_Uses_Sec_Stack (Defining_Entity (Fun));
3977 end if;
3978 end if;
3980 Decl := Make_Object_Declaration (Loc,
3981 Defining_Identifier => T_Id,
3982 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
3983 Constant_Present => True,
3984 Expression => Expr);
3986 Append (Decl, Decls);
3987 return Decls;
3988 end Build_Task_Image_Decls;
3990 -------------------------------
3991 -- Build_Task_Image_Function --
3992 -------------------------------
3994 function Build_Task_Image_Function
3995 (Loc : Source_Ptr;
3996 Decls : List_Id;
3997 Stats : List_Id;
3998 Res : Entity_Id) return Node_Id
4000 Spec : Node_Id;
4002 begin
4003 Append_To (Stats,
4004 Make_Simple_Return_Statement (Loc,
4005 Expression => New_Occurrence_Of (Res, Loc)));
4007 Spec := Make_Function_Specification (Loc,
4008 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4009 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4011 -- Calls to 'Image use the secondary stack, which must be cleaned up
4012 -- after the task name is built.
4014 return Make_Subprogram_Body (Loc,
4015 Specification => Spec,
4016 Declarations => Decls,
4017 Handled_Statement_Sequence =>
4018 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4019 end Build_Task_Image_Function;
4021 -----------------------------
4022 -- Build_Task_Image_Prefix --
4023 -----------------------------
4025 procedure Build_Task_Image_Prefix
4026 (Loc : Source_Ptr;
4027 Len : out Entity_Id;
4028 Res : out Entity_Id;
4029 Pos : out Entity_Id;
4030 Prefix : Entity_Id;
4031 Sum : Node_Id;
4032 Decls : List_Id;
4033 Stats : List_Id)
4035 begin
4036 Len := Make_Temporary (Loc, 'L', Sum);
4038 Append_To (Decls,
4039 Make_Object_Declaration (Loc,
4040 Defining_Identifier => Len,
4041 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4042 Expression => Sum));
4044 Res := Make_Temporary (Loc, 'R');
4046 Append_To (Decls,
4047 Make_Object_Declaration (Loc,
4048 Defining_Identifier => Res,
4049 Object_Definition =>
4050 Make_Subtype_Indication (Loc,
4051 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4052 Constraint =>
4053 Make_Index_Or_Discriminant_Constraint (Loc,
4054 Constraints =>
4055 New_List (
4056 Make_Range (Loc,
4057 Low_Bound => Make_Integer_Literal (Loc, 1),
4058 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4060 -- Indicate that the result is an internal temporary, so it does not
4061 -- receive a bogus initialization when declaration is expanded. This
4062 -- is both efficient, and prevents anomalies in the handling of
4063 -- dynamic objects on the secondary stack.
4065 Set_Is_Internal (Res);
4066 Pos := Make_Temporary (Loc, 'P');
4068 Append_To (Decls,
4069 Make_Object_Declaration (Loc,
4070 Defining_Identifier => Pos,
4071 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4073 -- Pos := Prefix'Length;
4075 Append_To (Stats,
4076 Make_Assignment_Statement (Loc,
4077 Name => New_Occurrence_Of (Pos, Loc),
4078 Expression =>
4079 Make_Attribute_Reference (Loc,
4080 Attribute_Name => Name_Length,
4081 Prefix => New_Occurrence_Of (Prefix, Loc),
4082 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4084 -- Res (1 .. Pos) := Prefix;
4086 Append_To (Stats,
4087 Make_Assignment_Statement (Loc,
4088 Name =>
4089 Make_Slice (Loc,
4090 Prefix => New_Occurrence_Of (Res, Loc),
4091 Discrete_Range =>
4092 Make_Range (Loc,
4093 Low_Bound => Make_Integer_Literal (Loc, 1),
4094 High_Bound => New_Occurrence_Of (Pos, Loc))),
4096 Expression => New_Occurrence_Of (Prefix, Loc)));
4098 Append_To (Stats,
4099 Make_Assignment_Statement (Loc,
4100 Name => New_Occurrence_Of (Pos, Loc),
4101 Expression =>
4102 Make_Op_Add (Loc,
4103 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4104 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4105 end Build_Task_Image_Prefix;
4107 -----------------------------
4108 -- Build_Task_Record_Image --
4109 -----------------------------
4111 function Build_Task_Record_Image
4112 (Loc : Source_Ptr;
4113 Id_Ref : Node_Id;
4114 Dyn : Boolean := False) return Node_Id
4116 Len : Entity_Id;
4117 -- Total length of generated name
4119 Pos : Entity_Id;
4120 -- Index into result
4122 Res : Entity_Id;
4123 -- String to hold result
4125 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4126 -- Name of enclosing variable, prefix of resulting name
4128 Sum : Node_Id;
4129 -- Expression to compute total size of string
4131 Sel : Entity_Id;
4132 -- Entity for selector name
4134 Decls : constant List_Id := New_List;
4135 Stats : constant List_Id := New_List;
4137 begin
4138 -- For a dynamic task, the name comes from the target variable. For a
4139 -- static one it is a formal of the enclosing init proc.
4141 if Dyn then
4142 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4143 Append_To (Decls,
4144 Make_Object_Declaration (Loc,
4145 Defining_Identifier => Pref,
4146 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4147 Expression =>
4148 Make_String_Literal (Loc,
4149 Strval => String_From_Name_Buffer)));
4151 else
4152 Append_To (Decls,
4153 Make_Object_Renaming_Declaration (Loc,
4154 Defining_Identifier => Pref,
4155 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4156 Name => Make_Identifier (Loc, Name_uTask_Name)));
4157 end if;
4159 Sel := Make_Temporary (Loc, 'S');
4161 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4163 Append_To (Decls,
4164 Make_Object_Declaration (Loc,
4165 Defining_Identifier => Sel,
4166 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4167 Expression =>
4168 Make_String_Literal (Loc,
4169 Strval => String_From_Name_Buffer)));
4171 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4173 Sum :=
4174 Make_Op_Add (Loc,
4175 Left_Opnd => Sum,
4176 Right_Opnd =>
4177 Make_Attribute_Reference (Loc,
4178 Attribute_Name => Name_Length,
4179 Prefix =>
4180 New_Occurrence_Of (Pref, Loc),
4181 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4183 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4185 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4187 -- Res (Pos) := '.';
4189 Append_To (Stats,
4190 Make_Assignment_Statement (Loc,
4191 Name => Make_Indexed_Component (Loc,
4192 Prefix => New_Occurrence_Of (Res, Loc),
4193 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4194 Expression =>
4195 Make_Character_Literal (Loc,
4196 Chars => Name_Find,
4197 Char_Literal_Value =>
4198 UI_From_Int (Character'Pos ('.')))));
4200 Append_To (Stats,
4201 Make_Assignment_Statement (Loc,
4202 Name => New_Occurrence_Of (Pos, Loc),
4203 Expression =>
4204 Make_Op_Add (Loc,
4205 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4206 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4208 -- Res (Pos .. Len) := Selector;
4210 Append_To (Stats,
4211 Make_Assignment_Statement (Loc,
4212 Name => Make_Slice (Loc,
4213 Prefix => New_Occurrence_Of (Res, Loc),
4214 Discrete_Range =>
4215 Make_Range (Loc,
4216 Low_Bound => New_Occurrence_Of (Pos, Loc),
4217 High_Bound => New_Occurrence_Of (Len, Loc))),
4218 Expression => New_Occurrence_Of (Sel, Loc)));
4220 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4221 end Build_Task_Record_Image;
4223 ---------------------------------------
4224 -- Build_Transient_Object_Statements --
4225 ---------------------------------------
4227 procedure Build_Transient_Object_Statements
4228 (Obj_Decl : Node_Id;
4229 Fin_Call : out Node_Id;
4230 Hook_Assign : out Node_Id;
4231 Hook_Clear : out Node_Id;
4232 Hook_Decl : out Node_Id;
4233 Ptr_Decl : out Node_Id;
4234 Finalize_Obj : Boolean := True)
4236 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4237 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4238 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4240 Desig_Typ : Entity_Id;
4241 Hook_Expr : Node_Id;
4242 Hook_Id : Entity_Id;
4243 Obj_Ref : Node_Id;
4244 Ptr_Typ : Entity_Id;
4246 begin
4247 -- Recover the type of the object
4249 Desig_Typ := Obj_Typ;
4251 if Is_Access_Type (Desig_Typ) then
4252 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4253 end if;
4255 -- Create an access type which provides a reference to the transient
4256 -- object. Generate:
4258 -- type Ptr_Typ is access all Desig_Typ;
4260 Ptr_Typ := Make_Temporary (Loc, 'A');
4261 Set_Ekind (Ptr_Typ, E_General_Access_Type);
4262 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4264 Ptr_Decl :=
4265 Make_Full_Type_Declaration (Loc,
4266 Defining_Identifier => Ptr_Typ,
4267 Type_Definition =>
4268 Make_Access_To_Object_Definition (Loc,
4269 All_Present => True,
4270 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4272 -- Create a temporary check which acts as a hook to the transient
4273 -- object. Generate:
4275 -- Hook : Ptr_Typ := null;
4277 Hook_Id := Make_Temporary (Loc, 'T');
4278 Set_Ekind (Hook_Id, E_Variable);
4279 Set_Etype (Hook_Id, Ptr_Typ);
4281 Hook_Decl :=
4282 Make_Object_Declaration (Loc,
4283 Defining_Identifier => Hook_Id,
4284 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4285 Expression => Make_Null (Loc));
4287 -- Mark the temporary as a hook. This signals the machinery in
4288 -- Build_Finalizer to recognize this special case.
4290 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4292 -- Hook the transient object to the temporary. Generate:
4294 -- Hook := Ptr_Typ (Obj_Id);
4295 -- <or>
4296 -- Hool := Obj_Id'Unrestricted_Access;
4298 if Is_Access_Type (Obj_Typ) then
4299 Hook_Expr :=
4300 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4301 else
4302 Hook_Expr :=
4303 Make_Attribute_Reference (Loc,
4304 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4305 Attribute_Name => Name_Unrestricted_Access);
4306 end if;
4308 Hook_Assign :=
4309 Make_Assignment_Statement (Loc,
4310 Name => New_Occurrence_Of (Hook_Id, Loc),
4311 Expression => Hook_Expr);
4313 -- Crear the hook prior to finalizing the object. Generate:
4315 -- Hook := null;
4317 Hook_Clear :=
4318 Make_Assignment_Statement (Loc,
4319 Name => New_Occurrence_Of (Hook_Id, Loc),
4320 Expression => Make_Null (Loc));
4322 -- Finalize the object. Generate:
4324 -- [Deep_]Finalize (Obj_Ref[.all]);
4326 if Finalize_Obj then
4327 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4329 if Is_Access_Type (Obj_Typ) then
4330 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4331 Set_Etype (Obj_Ref, Desig_Typ);
4332 end if;
4334 Fin_Call :=
4335 Make_Final_Call
4336 (Obj_Ref => Obj_Ref,
4337 Typ => Desig_Typ);
4339 -- Otherwise finalize the hook. Generate:
4341 -- [Deep_]Finalize (Hook.all);
4343 else
4344 Fin_Call :=
4345 Make_Final_Call (
4346 Obj_Ref =>
4347 Make_Explicit_Dereference (Loc,
4348 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4349 Typ => Desig_Typ);
4350 end if;
4351 end Build_Transient_Object_Statements;
4353 -----------------------------
4354 -- Check_Float_Op_Overflow --
4355 -----------------------------
4357 procedure Check_Float_Op_Overflow (N : Node_Id) is
4358 begin
4359 -- Return if no check needed
4361 if not Is_Floating_Point_Type (Etype (N))
4362 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4364 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4365 -- and do not expand the code for float overflow checking.
4367 or else CodePeer_Mode
4368 then
4369 return;
4370 end if;
4372 -- Otherwise we replace the expression by
4374 -- do Tnn : constant ftype := expression;
4375 -- constraint_error when not Tnn'Valid;
4376 -- in Tnn;
4378 declare
4379 Loc : constant Source_Ptr := Sloc (N);
4380 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4381 Typ : constant Entity_Id := Etype (N);
4383 begin
4384 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4385 -- right here. We also set the node as analyzed to prevent infinite
4386 -- recursion from repeating the operation in the expansion.
4388 Set_Do_Overflow_Check (N, False);
4389 Set_Analyzed (N, True);
4391 -- Do the rewrite to include the check
4393 Rewrite (N,
4394 Make_Expression_With_Actions (Loc,
4395 Actions => New_List (
4396 Make_Object_Declaration (Loc,
4397 Defining_Identifier => Tnn,
4398 Object_Definition => New_Occurrence_Of (Typ, Loc),
4399 Constant_Present => True,
4400 Expression => Relocate_Node (N)),
4401 Make_Raise_Constraint_Error (Loc,
4402 Condition =>
4403 Make_Op_Not (Loc,
4404 Right_Opnd =>
4405 Make_Attribute_Reference (Loc,
4406 Prefix => New_Occurrence_Of (Tnn, Loc),
4407 Attribute_Name => Name_Valid)),
4408 Reason => CE_Overflow_Check_Failed)),
4409 Expression => New_Occurrence_Of (Tnn, Loc)));
4411 Analyze_And_Resolve (N, Typ);
4412 end;
4413 end Check_Float_Op_Overflow;
4415 ----------------------------------
4416 -- Component_May_Be_Bit_Aligned --
4417 ----------------------------------
4419 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4420 UT : Entity_Id;
4422 begin
4423 -- If no component clause, then everything is fine, since the back end
4424 -- never bit-misaligns by default, even if there is a pragma Packed for
4425 -- the record.
4427 if No (Comp) or else No (Component_Clause (Comp)) then
4428 return False;
4429 end if;
4431 UT := Underlying_Type (Etype (Comp));
4433 -- It is only array and record types that cause trouble
4435 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4436 return False;
4438 -- If we know that we have a small (64 bits or less) record or small
4439 -- bit-packed array, then everything is fine, since the back end can
4440 -- handle these cases correctly.
4442 elsif Esize (Comp) <= 64
4443 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4444 then
4445 return False;
4447 -- Otherwise if the component is not byte aligned, we know we have the
4448 -- nasty unaligned case.
4450 elsif Normalized_First_Bit (Comp) /= Uint_0
4451 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4452 then
4453 return True;
4455 -- If we are large and byte aligned, then OK at this level
4457 else
4458 return False;
4459 end if;
4460 end Component_May_Be_Bit_Aligned;
4462 ----------------------------------------
4463 -- Containing_Package_With_Ext_Axioms --
4464 ----------------------------------------
4466 function Containing_Package_With_Ext_Axioms
4467 (E : Entity_Id) return Entity_Id
4469 begin
4470 -- E is the package or generic package which is externally axiomatized
4472 if Ekind_In (E, E_Generic_Package, E_Package)
4473 and then Has_Annotate_Pragma_For_External_Axiomatization (E)
4474 then
4475 return E;
4476 end if;
4478 -- If E's scope is axiomatized, E is axiomatized
4480 if Present (Scope (E)) then
4481 declare
4482 First_Ax_Parent_Scope : constant Entity_Id :=
4483 Containing_Package_With_Ext_Axioms (Scope (E));
4484 begin
4485 if Present (First_Ax_Parent_Scope) then
4486 return First_Ax_Parent_Scope;
4487 end if;
4488 end;
4489 end if;
4491 -- Otherwise, if E is a package instance, it is axiomatized if the
4492 -- corresponding generic package is axiomatized.
4494 if Ekind (E) = E_Package then
4495 declare
4496 Par : constant Node_Id := Parent (E);
4497 Decl : Node_Id;
4499 begin
4500 if Nkind (Par) = N_Defining_Program_Unit_Name then
4501 Decl := Parent (Par);
4502 else
4503 Decl := Par;
4504 end if;
4506 if Present (Generic_Parent (Decl)) then
4507 return
4508 Containing_Package_With_Ext_Axioms (Generic_Parent (Decl));
4509 end if;
4510 end;
4511 end if;
4513 return Empty;
4514 end Containing_Package_With_Ext_Axioms;
4516 -------------------------------
4517 -- Convert_To_Actual_Subtype --
4518 -------------------------------
4520 procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
4521 Act_ST : Entity_Id;
4523 begin
4524 Act_ST := Get_Actual_Subtype (Exp);
4526 if Act_ST = Etype (Exp) then
4527 return;
4528 else
4529 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4530 Analyze_And_Resolve (Exp, Act_ST);
4531 end if;
4532 end Convert_To_Actual_Subtype;
4534 -----------------------------------
4535 -- Corresponding_Runtime_Package --
4536 -----------------------------------
4538 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4539 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4540 -- Return True if protected type T has one entry and the maximum queue
4541 -- length is one.
4543 --------------------------------
4544 -- Has_One_Entry_And_No_Queue --
4545 --------------------------------
4547 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4548 Item : Entity_Id;
4549 Is_First : Boolean := True;
4551 begin
4552 Item := First_Entity (T);
4553 while Present (Item) loop
4554 if Is_Entry (Item) then
4556 -- The protected type has more than one entry
4558 if not Is_First then
4559 return False;
4560 end if;
4562 -- The queue length is not one
4564 if not Restriction_Active (No_Entry_Queue)
4565 and then Get_Max_Queue_Length (Item) /= Uint_1
4566 then
4567 return False;
4568 end if;
4570 Is_First := False;
4571 end if;
4573 Next_Entity (Item);
4574 end loop;
4576 return True;
4577 end Has_One_Entry_And_No_Queue;
4579 -- Local variables
4581 Pkg_Id : RTU_Id := RTU_Null;
4583 -- Start of processing for Corresponding_Runtime_Package
4585 begin
4586 pragma Assert (Is_Concurrent_Type (Typ));
4588 if Ekind (Typ) in Protected_Kind then
4589 if Has_Entries (Typ)
4591 -- A protected type without entries that covers an interface and
4592 -- overrides the abstract routines with protected procedures is
4593 -- considered equivalent to a protected type with entries in the
4594 -- context of dispatching select statements. It is sufficient to
4595 -- check for the presence of an interface list in the declaration
4596 -- node to recognize this case.
4598 or else Present (Interface_List (Parent (Typ)))
4600 -- Protected types with interrupt handlers (when not using a
4601 -- restricted profile) are also considered equivalent to
4602 -- protected types with entries. The types which are used
4603 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4604 -- are derived from Protection_Entries.
4606 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4607 or else Has_Interrupt_Handler (Typ)
4608 then
4609 if Abort_Allowed
4610 or else Restriction_Active (No_Select_Statements) = False
4611 or else not Has_One_Entry_And_No_Queue (Typ)
4612 or else (Has_Attach_Handler (Typ)
4613 and then not Restricted_Profile)
4614 then
4615 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4616 else
4617 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4618 end if;
4620 else
4621 Pkg_Id := System_Tasking_Protected_Objects;
4622 end if;
4623 end if;
4625 return Pkg_Id;
4626 end Corresponding_Runtime_Package;
4628 -----------------------------------
4629 -- Current_Sem_Unit_Declarations --
4630 -----------------------------------
4632 function Current_Sem_Unit_Declarations return List_Id is
4633 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4634 Decls : List_Id;
4636 begin
4637 -- If the current unit is a package body, locate the visible
4638 -- declarations of the package spec.
4640 if Nkind (U) = N_Package_Body then
4641 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4642 end if;
4644 if Nkind (U) = N_Package_Declaration then
4645 U := Specification (U);
4646 Decls := Visible_Declarations (U);
4648 if No (Decls) then
4649 Decls := New_List;
4650 Set_Visible_Declarations (U, Decls);
4651 end if;
4653 else
4654 Decls := Declarations (U);
4656 if No (Decls) then
4657 Decls := New_List;
4658 Set_Declarations (U, Decls);
4659 end if;
4660 end if;
4662 return Decls;
4663 end Current_Sem_Unit_Declarations;
4665 -----------------------
4666 -- Duplicate_Subexpr --
4667 -----------------------
4669 function Duplicate_Subexpr
4670 (Exp : Node_Id;
4671 Name_Req : Boolean := False;
4672 Renaming_Req : Boolean := False) return Node_Id
4674 begin
4675 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4676 return New_Copy_Tree (Exp);
4677 end Duplicate_Subexpr;
4679 ---------------------------------
4680 -- Duplicate_Subexpr_No_Checks --
4681 ---------------------------------
4683 function Duplicate_Subexpr_No_Checks
4684 (Exp : Node_Id;
4685 Name_Req : Boolean := False;
4686 Renaming_Req : Boolean := False;
4687 Related_Id : Entity_Id := Empty;
4688 Is_Low_Bound : Boolean := False;
4689 Is_High_Bound : Boolean := False) return Node_Id
4691 New_Exp : Node_Id;
4693 begin
4694 Remove_Side_Effects
4695 (Exp => Exp,
4696 Name_Req => Name_Req,
4697 Renaming_Req => Renaming_Req,
4698 Related_Id => Related_Id,
4699 Is_Low_Bound => Is_Low_Bound,
4700 Is_High_Bound => Is_High_Bound);
4702 New_Exp := New_Copy_Tree (Exp);
4703 Remove_Checks (New_Exp);
4704 return New_Exp;
4705 end Duplicate_Subexpr_No_Checks;
4707 -----------------------------------
4708 -- Duplicate_Subexpr_Move_Checks --
4709 -----------------------------------
4711 function Duplicate_Subexpr_Move_Checks
4712 (Exp : Node_Id;
4713 Name_Req : Boolean := False;
4714 Renaming_Req : Boolean := False) return Node_Id
4716 New_Exp : Node_Id;
4718 begin
4719 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4720 New_Exp := New_Copy_Tree (Exp);
4721 Remove_Checks (Exp);
4722 return New_Exp;
4723 end Duplicate_Subexpr_Move_Checks;
4725 --------------------
4726 -- Ensure_Defined --
4727 --------------------
4729 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
4730 IR : Node_Id;
4732 begin
4733 -- An itype reference must only be created if this is a local itype, so
4734 -- that gigi can elaborate it on the proper objstack.
4736 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
4737 IR := Make_Itype_Reference (Sloc (N));
4738 Set_Itype (IR, Typ);
4739 Insert_Action (N, IR);
4740 end if;
4741 end Ensure_Defined;
4743 --------------------
4744 -- Entry_Names_OK --
4745 --------------------
4747 function Entry_Names_OK return Boolean is
4748 begin
4749 return
4750 not Restricted_Profile
4751 and then not Global_Discard_Names
4752 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4753 and then not Restriction_Active (No_Local_Allocators);
4754 end Entry_Names_OK;
4756 -------------------
4757 -- Evaluate_Name --
4758 -------------------
4760 procedure Evaluate_Name (Nam : Node_Id) is
4761 begin
4762 -- For an attribute reference or an indexed component, evaluate the
4763 -- prefix, which is itself a name, recursively, and then force the
4764 -- evaluation of all the subscripts (or attribute expressions).
4766 case Nkind (Nam) is
4767 when N_Attribute_Reference
4768 | N_Indexed_Component
4770 Evaluate_Name (Prefix (Nam));
4772 declare
4773 E : Node_Id;
4775 begin
4776 E := First (Expressions (Nam));
4777 while Present (E) loop
4778 Force_Evaluation (E);
4780 if Original_Node (E) /= E then
4781 Set_Do_Range_Check
4782 (E, Do_Range_Check (Original_Node (E)));
4783 end if;
4785 Next (E);
4786 end loop;
4787 end;
4789 -- For an explicit dereference, we simply force the evaluation of
4790 -- the name expression. The dereference provides a value that is the
4791 -- address for the renamed object, and it is precisely this value
4792 -- that we want to preserve.
4794 when N_Explicit_Dereference =>
4795 Force_Evaluation (Prefix (Nam));
4797 -- For a function call, we evaluate the call
4799 when N_Function_Call =>
4800 Force_Evaluation (Nam);
4802 -- For a qualified expression, we evaluate the underlying object
4803 -- name if any, otherwise we force the evaluation of the underlying
4804 -- expression.
4806 when N_Qualified_Expression =>
4807 if Is_Object_Reference (Expression (Nam)) then
4808 Evaluate_Name (Expression (Nam));
4809 else
4810 Force_Evaluation (Expression (Nam));
4811 end if;
4813 -- For a selected component, we simply evaluate the prefix
4815 when N_Selected_Component =>
4816 Evaluate_Name (Prefix (Nam));
4818 -- For a slice, we evaluate the prefix, as for the indexed component
4819 -- case and then, if there is a range present, either directly or as
4820 -- the constraint of a discrete subtype indication, we evaluate the
4821 -- two bounds of this range.
4823 when N_Slice =>
4824 Evaluate_Name (Prefix (Nam));
4825 Evaluate_Slice_Bounds (Nam);
4827 -- For a type conversion, the expression of the conversion must be
4828 -- the name of an object, and we simply need to evaluate this name.
4830 when N_Type_Conversion =>
4831 Evaluate_Name (Expression (Nam));
4833 -- The remaining cases are direct name, operator symbol and character
4834 -- literal. In all these cases, we do nothing, since we want to
4835 -- reevaluate each time the renamed object is used.
4837 when others =>
4838 null;
4839 end case;
4840 end Evaluate_Name;
4842 ---------------------------
4843 -- Evaluate_Slice_Bounds --
4844 ---------------------------
4846 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
4847 DR : constant Node_Id := Discrete_Range (Slice);
4848 Constr : Node_Id;
4849 Rexpr : Node_Id;
4851 begin
4852 if Nkind (DR) = N_Range then
4853 Force_Evaluation (Low_Bound (DR));
4854 Force_Evaluation (High_Bound (DR));
4856 elsif Nkind (DR) = N_Subtype_Indication then
4857 Constr := Constraint (DR);
4859 if Nkind (Constr) = N_Range_Constraint then
4860 Rexpr := Range_Expression (Constr);
4862 Force_Evaluation (Low_Bound (Rexpr));
4863 Force_Evaluation (High_Bound (Rexpr));
4864 end if;
4865 end if;
4866 end Evaluate_Slice_Bounds;
4868 ---------------------
4869 -- Evolve_And_Then --
4870 ---------------------
4872 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
4873 begin
4874 if No (Cond) then
4875 Cond := Cond1;
4876 else
4877 Cond :=
4878 Make_And_Then (Sloc (Cond1),
4879 Left_Opnd => Cond,
4880 Right_Opnd => Cond1);
4881 end if;
4882 end Evolve_And_Then;
4884 --------------------
4885 -- Evolve_Or_Else --
4886 --------------------
4888 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
4889 begin
4890 if No (Cond) then
4891 Cond := Cond1;
4892 else
4893 Cond :=
4894 Make_Or_Else (Sloc (Cond1),
4895 Left_Opnd => Cond,
4896 Right_Opnd => Cond1);
4897 end if;
4898 end Evolve_Or_Else;
4900 -----------------------------------
4901 -- Exceptions_In_Finalization_OK --
4902 -----------------------------------
4904 function Exceptions_In_Finalization_OK return Boolean is
4905 begin
4906 return
4907 not (Restriction_Active (No_Exception_Handlers) or else
4908 Restriction_Active (No_Exception_Propagation) or else
4909 Restriction_Active (No_Exceptions));
4910 end Exceptions_In_Finalization_OK;
4912 -----------------------------------------
4913 -- Expand_Static_Predicates_In_Choices --
4914 -----------------------------------------
4916 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
4917 pragma Assert (Nkind_In (N, N_Case_Statement_Alternative, N_Variant));
4919 Choices : constant List_Id := Discrete_Choices (N);
4921 Choice : Node_Id;
4922 Next_C : Node_Id;
4923 P : Node_Id;
4924 C : Node_Id;
4926 begin
4927 Choice := First (Choices);
4928 while Present (Choice) loop
4929 Next_C := Next (Choice);
4931 -- Check for name of subtype with static predicate
4933 if Is_Entity_Name (Choice)
4934 and then Is_Type (Entity (Choice))
4935 and then Has_Predicates (Entity (Choice))
4936 then
4937 -- Loop through entries in predicate list, converting to choices
4938 -- and inserting in the list before the current choice. Note that
4939 -- if the list is empty, corresponding to a False predicate, then
4940 -- no choices are inserted.
4942 P := First (Static_Discrete_Predicate (Entity (Choice)));
4943 while Present (P) loop
4945 -- If low bound and high bounds are equal, copy simple choice
4947 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
4948 C := New_Copy (Low_Bound (P));
4950 -- Otherwise copy a range
4952 else
4953 C := New_Copy (P);
4954 end if;
4956 -- Change Sloc to referencing choice (rather than the Sloc of
4957 -- the predicate declaration element itself).
4959 Set_Sloc (C, Sloc (Choice));
4960 Insert_Before (Choice, C);
4961 Next (P);
4962 end loop;
4964 -- Delete the predicated entry
4966 Remove (Choice);
4967 end if;
4969 -- Move to next choice to check
4971 Choice := Next_C;
4972 end loop;
4973 end Expand_Static_Predicates_In_Choices;
4975 ------------------------------
4976 -- Expand_Subtype_From_Expr --
4977 ------------------------------
4979 -- This function is applicable for both static and dynamic allocation of
4980 -- objects which are constrained by an initial expression. Basically it
4981 -- transforms an unconstrained subtype indication into a constrained one.
4983 -- The expression may also be transformed in certain cases in order to
4984 -- avoid multiple evaluation. In the static allocation case, the general
4985 -- scheme is:
4987 -- Val : T := Expr;
4989 -- is transformed into
4991 -- Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
4993 -- Here are the main cases :
4995 -- <if Expr is a Slice>
4996 -- Val : T ([Index_Subtype (Expr)]) := Expr;
4998 -- <elsif Expr is a String Literal>
4999 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5001 -- <elsif Expr is Constrained>
5002 -- subtype T is Type_Of_Expr
5003 -- Val : T := Expr;
5005 -- <elsif Expr is an entity_name>
5006 -- Val : T (constraints taken from Expr) := Expr;
5008 -- <else>
5009 -- type Axxx is access all T;
5010 -- Rval : Axxx := Expr'ref;
5011 -- Val : T (constraints taken from Rval) := Rval.all;
5013 -- ??? note: when the Expression is allocated in the secondary stack
5014 -- we could use it directly instead of copying it by declaring
5015 -- Val : T (...) renames Rval.all
5017 procedure Expand_Subtype_From_Expr
5018 (N : Node_Id;
5019 Unc_Type : Entity_Id;
5020 Subtype_Indic : Node_Id;
5021 Exp : Node_Id;
5022 Related_Id : Entity_Id := Empty)
5024 Loc : constant Source_Ptr := Sloc (N);
5025 Exp_Typ : constant Entity_Id := Etype (Exp);
5026 T : Entity_Id;
5028 begin
5029 -- In general we cannot build the subtype if expansion is disabled,
5030 -- because internal entities may not have been defined. However, to
5031 -- avoid some cascaded errors, we try to continue when the expression is
5032 -- an array (or string), because it is safe to compute the bounds. It is
5033 -- in fact required to do so even in a generic context, because there
5034 -- may be constants that depend on the bounds of a string literal, both
5035 -- standard string types and more generally arrays of characters.
5037 -- In GNATprove mode, these extra subtypes are not needed
5039 if GNATprove_Mode then
5040 return;
5041 end if;
5043 if not Expander_Active
5044 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5045 then
5046 return;
5047 end if;
5049 if Nkind (Exp) = N_Slice then
5050 declare
5051 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5053 begin
5054 Rewrite (Subtype_Indic,
5055 Make_Subtype_Indication (Loc,
5056 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5057 Constraint =>
5058 Make_Index_Or_Discriminant_Constraint (Loc,
5059 Constraints => New_List
5060 (New_Occurrence_Of (Slice_Type, Loc)))));
5062 -- This subtype indication may be used later for constraint checks
5063 -- we better make sure that if a variable was used as a bound of
5064 -- of the original slice, its value is frozen.
5066 Evaluate_Slice_Bounds (Exp);
5067 end;
5069 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5070 Rewrite (Subtype_Indic,
5071 Make_Subtype_Indication (Loc,
5072 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5073 Constraint =>
5074 Make_Index_Or_Discriminant_Constraint (Loc,
5075 Constraints => New_List (
5076 Make_Literal_Range (Loc,
5077 Literal_Typ => Exp_Typ)))));
5079 -- If the type of the expression is an internally generated type it
5080 -- may not be necessary to create a new subtype. However there are two
5081 -- exceptions: references to the current instances, and aliased array
5082 -- object declarations for which the back end has to create a template.
5084 elsif Is_Constrained (Exp_Typ)
5085 and then not Is_Class_Wide_Type (Unc_Type)
5086 and then
5087 (Nkind (N) /= N_Object_Declaration
5088 or else not Is_Entity_Name (Expression (N))
5089 or else not Comes_From_Source (Entity (Expression (N)))
5090 or else not Is_Array_Type (Exp_Typ)
5091 or else not Aliased_Present (N))
5092 then
5093 if Is_Itype (Exp_Typ) then
5095 -- Within an initialization procedure, a selected component
5096 -- denotes a component of the enclosing record, and it appears as
5097 -- an actual in a call to its own initialization procedure. If
5098 -- this component depends on the outer discriminant, we must
5099 -- generate the proper actual subtype for it.
5101 if Nkind (Exp) = N_Selected_Component
5102 and then Within_Init_Proc
5103 then
5104 declare
5105 Decl : constant Node_Id :=
5106 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5107 begin
5108 if Present (Decl) then
5109 Insert_Action (N, Decl);
5110 T := Defining_Identifier (Decl);
5111 else
5112 T := Exp_Typ;
5113 end if;
5114 end;
5116 -- No need to generate a new subtype
5118 else
5119 T := Exp_Typ;
5120 end if;
5122 else
5123 T := Make_Temporary (Loc, 'T');
5125 Insert_Action (N,
5126 Make_Subtype_Declaration (Loc,
5127 Defining_Identifier => T,
5128 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5130 -- This type is marked as an itype even though it has an explicit
5131 -- declaration since otherwise Is_Generic_Actual_Type can get
5132 -- set, resulting in the generation of spurious errors. (See
5133 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5135 Set_Is_Itype (T);
5136 Set_Associated_Node_For_Itype (T, Exp);
5137 end if;
5139 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5141 -- Nothing needs to be done for private types with unknown discriminants
5142 -- if the underlying type is not an unconstrained composite type or it
5143 -- is an unchecked union.
5145 elsif Is_Private_Type (Unc_Type)
5146 and then Has_Unknown_Discriminants (Unc_Type)
5147 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5148 or else Is_Constrained (Underlying_Type (Unc_Type))
5149 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5150 then
5151 null;
5153 -- Case of derived type with unknown discriminants where the parent type
5154 -- also has unknown discriminants.
5156 elsif Is_Record_Type (Unc_Type)
5157 and then not Is_Class_Wide_Type (Unc_Type)
5158 and then Has_Unknown_Discriminants (Unc_Type)
5159 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5160 then
5161 -- Nothing to be done if no underlying record view available
5163 -- If this is a limited type derived from a type with unknown
5164 -- discriminants, do not expand either, so that subsequent expansion
5165 -- of the call can add build-in-place parameters to call.
5167 if No (Underlying_Record_View (Unc_Type))
5168 or else Is_Limited_Type (Unc_Type)
5169 then
5170 null;
5172 -- Otherwise use the Underlying_Record_View to create the proper
5173 -- constrained subtype for an object of a derived type with unknown
5174 -- discriminants.
5176 else
5177 Remove_Side_Effects (Exp);
5178 Rewrite (Subtype_Indic,
5179 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5180 end if;
5182 -- Renamings of class-wide interface types require no equivalent
5183 -- constrained type declarations because we only need to reference
5184 -- the tag component associated with the interface. The same is
5185 -- presumably true for class-wide types in general, so this test
5186 -- is broadened to include all class-wide renamings, which also
5187 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5188 -- (Is this really correct, or are there some cases of class-wide
5189 -- renamings that require action in this procedure???)
5191 elsif Present (N)
5192 and then Nkind (N) = N_Object_Renaming_Declaration
5193 and then Is_Class_Wide_Type (Unc_Type)
5194 then
5195 null;
5197 -- In Ada 95 nothing to be done if the type of the expression is limited
5198 -- because in this case the expression cannot be copied, and its use can
5199 -- only be by reference.
5201 -- In Ada 2005 the context can be an object declaration whose expression
5202 -- is a function that returns in place. If the nominal subtype has
5203 -- unknown discriminants, the call still provides constraints on the
5204 -- object, and we have to create an actual subtype from it.
5206 -- If the type is class-wide, the expression is dynamically tagged and
5207 -- we do not create an actual subtype either. Ditto for an interface.
5208 -- For now this applies only if the type is immutably limited, and the
5209 -- function being called is build-in-place. This will have to be revised
5210 -- when build-in-place functions are generalized to other types.
5212 elsif Is_Limited_View (Exp_Typ)
5213 and then
5214 (Is_Class_Wide_Type (Exp_Typ)
5215 or else Is_Interface (Exp_Typ)
5216 or else not Has_Unknown_Discriminants (Exp_Typ)
5217 or else not Is_Composite_Type (Unc_Type))
5218 then
5219 null;
5221 -- For limited objects initialized with build in place function calls,
5222 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5223 -- node in the expression initializing the object, which breaks the
5224 -- circuitry that detects and adds the additional arguments to the
5225 -- called function.
5227 elsif Is_Build_In_Place_Function_Call (Exp) then
5228 null;
5230 else
5231 Remove_Side_Effects (Exp);
5232 Rewrite (Subtype_Indic,
5233 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5234 end if;
5235 end Expand_Subtype_From_Expr;
5237 ---------------------------------------------
5238 -- Expression_Contains_Primitives_Calls_Of --
5239 ---------------------------------------------
5241 function Expression_Contains_Primitives_Calls_Of
5242 (Expr : Node_Id;
5243 Typ : Entity_Id) return Boolean
5245 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5247 Calls_OK : Boolean := False;
5248 -- This flag is set to True when expression Expr contains at least one
5249 -- call to a nondispatching primitive function of Typ.
5251 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5252 -- Search for nondispatching calls to primitive functions of type Typ
5254 ----------------------------
5255 -- Search_Primitive_Calls --
5256 ----------------------------
5258 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5259 Disp_Typ : Entity_Id;
5260 Subp : Entity_Id;
5262 begin
5263 -- Detect a function call that could denote a nondispatching
5264 -- primitive of the input type.
5266 if Nkind (N) = N_Function_Call
5267 and then Is_Entity_Name (Name (N))
5268 then
5269 Subp := Entity (Name (N));
5271 -- Do not consider function calls with a controlling argument, as
5272 -- those are always dispatching calls.
5274 if Is_Dispatching_Operation (Subp)
5275 and then No (Controlling_Argument (N))
5276 then
5277 Disp_Typ := Find_Dispatching_Type (Subp);
5279 -- To qualify as a suitable primitive, the dispatching type of
5280 -- the function must be the input type.
5282 if Present (Disp_Typ)
5283 and then Unique_Entity (Disp_Typ) = U_Typ
5284 then
5285 Calls_OK := True;
5287 -- There is no need to continue the traversal, as one such
5288 -- call suffices.
5290 return Abandon;
5291 end if;
5292 end if;
5293 end if;
5295 return OK;
5296 end Search_Primitive_Calls;
5298 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5300 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5302 begin
5303 Search_Calls (Expr);
5304 return Calls_OK;
5305 end Expression_Contains_Primitives_Calls_Of;
5307 ----------------------
5308 -- Finalize_Address --
5309 ----------------------
5311 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5312 Utyp : Entity_Id := Typ;
5314 begin
5315 -- Handle protected class-wide or task class-wide types
5317 if Is_Class_Wide_Type (Utyp) then
5318 if Is_Concurrent_Type (Root_Type (Utyp)) then
5319 Utyp := Root_Type (Utyp);
5321 elsif Is_Private_Type (Root_Type (Utyp))
5322 and then Present (Full_View (Root_Type (Utyp)))
5323 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5324 then
5325 Utyp := Full_View (Root_Type (Utyp));
5326 end if;
5327 end if;
5329 -- Handle private types
5331 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5332 Utyp := Full_View (Utyp);
5333 end if;
5335 -- Handle protected and task types
5337 if Is_Concurrent_Type (Utyp)
5338 and then Present (Corresponding_Record_Type (Utyp))
5339 then
5340 Utyp := Corresponding_Record_Type (Utyp);
5341 end if;
5343 Utyp := Underlying_Type (Base_Type (Utyp));
5345 -- Deal with untagged derivation of private views. If the parent is
5346 -- now known to be protected, the finalization routine is the one
5347 -- defined on the corresponding record of the ancestor (corresponding
5348 -- records do not automatically inherit operations, but maybe they
5349 -- should???)
5351 if Is_Untagged_Derivation (Typ) then
5352 if Is_Protected_Type (Typ) then
5353 Utyp := Corresponding_Record_Type (Root_Type (Base_Type (Typ)));
5355 else
5356 Utyp := Underlying_Type (Root_Type (Base_Type (Typ)));
5358 if Is_Protected_Type (Utyp) then
5359 Utyp := Corresponding_Record_Type (Utyp);
5360 end if;
5361 end if;
5362 end if;
5364 -- If the underlying_type is a subtype, we are dealing with the
5365 -- completion of a private type. We need to access the base type and
5366 -- generate a conversion to it.
5368 if Utyp /= Base_Type (Utyp) then
5369 pragma Assert (Is_Private_Type (Typ));
5371 Utyp := Base_Type (Utyp);
5372 end if;
5374 -- When dealing with an internally built full view for a type with
5375 -- unknown discriminants, use the original record type.
5377 if Is_Underlying_Record_View (Utyp) then
5378 Utyp := Etype (Utyp);
5379 end if;
5381 return TSS (Utyp, TSS_Finalize_Address);
5382 end Finalize_Address;
5384 -------------------
5385 -- Find_DIC_Type --
5386 -------------------
5388 function Find_DIC_Type (Typ : Entity_Id) return Entity_Id is
5389 Curr_Typ : Entity_Id;
5390 -- The current type being examined in the parent hierarchy traversal
5392 DIC_Typ : Entity_Id;
5393 -- The type which carries the DIC pragma. This variable denotes the
5394 -- partial view when private types are involved.
5396 Par_Typ : Entity_Id;
5397 -- The parent type of the current type. This variable denotes the full
5398 -- view when private types are involved.
5400 begin
5401 -- The input type defines its own DIC pragma, therefore it is the owner
5403 if Has_Own_DIC (Typ) then
5404 DIC_Typ := Typ;
5406 -- Otherwise the DIC pragma is inherited from a parent type
5408 else
5409 pragma Assert (Has_Inherited_DIC (Typ));
5411 -- Climb the parent chain
5413 Curr_Typ := Typ;
5414 loop
5415 -- Inspect the parent type. Do not consider subtypes as they
5416 -- inherit the DIC attributes from their base types.
5418 DIC_Typ := Base_Type (Etype (Curr_Typ));
5420 -- Look at the full view of a private type because the type may
5421 -- have a hidden parent introduced in the full view.
5423 Par_Typ := DIC_Typ;
5425 if Is_Private_Type (Par_Typ)
5426 and then Present (Full_View (Par_Typ))
5427 then
5428 Par_Typ := Full_View (Par_Typ);
5429 end if;
5431 -- Stop the climb once the nearest parent type which defines a DIC
5432 -- pragma of its own is encountered or when the root of the parent
5433 -- chain is reached.
5435 exit when Has_Own_DIC (DIC_Typ) or else Curr_Typ = Par_Typ;
5437 Curr_Typ := Par_Typ;
5438 end loop;
5439 end if;
5441 return DIC_Typ;
5442 end Find_DIC_Type;
5444 ------------------------
5445 -- Find_Interface_ADT --
5446 ------------------------
5448 function Find_Interface_ADT
5449 (T : Entity_Id;
5450 Iface : Entity_Id) return Elmt_Id
5452 ADT : Elmt_Id;
5453 Typ : Entity_Id := T;
5455 begin
5456 pragma Assert (Is_Interface (Iface));
5458 -- Handle private types
5460 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5461 Typ := Full_View (Typ);
5462 end if;
5464 -- Handle access types
5466 if Is_Access_Type (Typ) then
5467 Typ := Designated_Type (Typ);
5468 end if;
5470 -- Handle task and protected types implementing interfaces
5472 if Is_Concurrent_Type (Typ) then
5473 Typ := Corresponding_Record_Type (Typ);
5474 end if;
5476 pragma Assert
5477 (not Is_Class_Wide_Type (Typ)
5478 and then Ekind (Typ) /= E_Incomplete_Type);
5480 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5481 return First_Elmt (Access_Disp_Table (Typ));
5483 else
5484 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5485 while Present (ADT)
5486 and then Present (Related_Type (Node (ADT)))
5487 and then Related_Type (Node (ADT)) /= Iface
5488 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5489 Use_Full_View => True)
5490 loop
5491 Next_Elmt (ADT);
5492 end loop;
5494 pragma Assert (Present (Related_Type (Node (ADT))));
5495 return ADT;
5496 end if;
5497 end Find_Interface_ADT;
5499 ------------------------
5500 -- Find_Interface_Tag --
5501 ------------------------
5503 function Find_Interface_Tag
5504 (T : Entity_Id;
5505 Iface : Entity_Id) return Entity_Id
5507 AI_Tag : Entity_Id;
5508 Found : Boolean := False;
5509 Typ : Entity_Id := T;
5511 procedure Find_Tag (Typ : Entity_Id);
5512 -- Internal subprogram used to recursively climb to the ancestors
5514 --------------
5515 -- Find_Tag --
5516 --------------
5518 procedure Find_Tag (Typ : Entity_Id) is
5519 AI_Elmt : Elmt_Id;
5520 AI : Node_Id;
5522 begin
5523 -- This routine does not handle the case in which the interface is an
5524 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5526 pragma Assert (Typ /= Iface);
5528 -- Climb to the root type handling private types
5530 if Present (Full_View (Etype (Typ))) then
5531 if Full_View (Etype (Typ)) /= Typ then
5532 Find_Tag (Full_View (Etype (Typ)));
5533 end if;
5535 elsif Etype (Typ) /= Typ then
5536 Find_Tag (Etype (Typ));
5537 end if;
5539 -- Traverse the list of interfaces implemented by the type
5541 if not Found
5542 and then Present (Interfaces (Typ))
5543 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
5544 then
5545 -- Skip the tag associated with the primary table
5547 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5548 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
5549 pragma Assert (Present (AI_Tag));
5551 AI_Elmt := First_Elmt (Interfaces (Typ));
5552 while Present (AI_Elmt) loop
5553 AI := Node (AI_Elmt);
5555 if AI = Iface
5556 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
5557 then
5558 Found := True;
5559 return;
5560 end if;
5562 AI_Tag := Next_Tag_Component (AI_Tag);
5563 Next_Elmt (AI_Elmt);
5564 end loop;
5565 end if;
5566 end Find_Tag;
5568 -- Start of processing for Find_Interface_Tag
5570 begin
5571 pragma Assert (Is_Interface (Iface));
5573 -- Handle access types
5575 if Is_Access_Type (Typ) then
5576 Typ := Designated_Type (Typ);
5577 end if;
5579 -- Handle class-wide types
5581 if Is_Class_Wide_Type (Typ) then
5582 Typ := Root_Type (Typ);
5583 end if;
5585 -- Handle private types
5587 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5588 Typ := Full_View (Typ);
5589 end if;
5591 -- Handle entities from the limited view
5593 if Ekind (Typ) = E_Incomplete_Type then
5594 pragma Assert (Present (Non_Limited_View (Typ)));
5595 Typ := Non_Limited_View (Typ);
5596 end if;
5598 -- Handle task and protected types implementing interfaces
5600 if Is_Concurrent_Type (Typ) then
5601 Typ := Corresponding_Record_Type (Typ);
5602 end if;
5604 -- If the interface is an ancestor of the type, then it shared the
5605 -- primary dispatch table.
5607 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5608 pragma Assert (Etype (First_Tag_Component (Typ)) = RTE (RE_Tag));
5609 return First_Tag_Component (Typ);
5611 -- Otherwise we need to search for its associated tag component
5613 else
5614 Find_Tag (Typ);
5615 pragma Assert (Found);
5616 return AI_Tag;
5617 end if;
5618 end Find_Interface_Tag;
5620 ---------------------------
5621 -- Find_Optional_Prim_Op --
5622 ---------------------------
5624 function Find_Optional_Prim_Op
5625 (T : Entity_Id; Name : Name_Id) return Entity_Id
5627 Prim : Elmt_Id;
5628 Typ : Entity_Id := T;
5629 Op : Entity_Id;
5631 begin
5632 if Is_Class_Wide_Type (Typ) then
5633 Typ := Root_Type (Typ);
5634 end if;
5636 Typ := Underlying_Type (Typ);
5638 -- Loop through primitive operations
5640 Prim := First_Elmt (Primitive_Operations (Typ));
5641 while Present (Prim) loop
5642 Op := Node (Prim);
5644 -- We can retrieve primitive operations by name if it is an internal
5645 -- name. For equality we must check that both of its operands have
5646 -- the same type, to avoid confusion with user-defined equalities
5647 -- than may have a non-symmetric signature.
5649 exit when Chars (Op) = Name
5650 and then
5651 (Name /= Name_Op_Eq
5652 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
5654 Next_Elmt (Prim);
5655 end loop;
5657 return Node (Prim); -- Empty if not found
5658 end Find_Optional_Prim_Op;
5660 ---------------------------
5661 -- Find_Optional_Prim_Op --
5662 ---------------------------
5664 function Find_Optional_Prim_Op
5665 (T : Entity_Id;
5666 Name : TSS_Name_Type) return Entity_Id
5668 Inher_Op : Entity_Id := Empty;
5669 Own_Op : Entity_Id := Empty;
5670 Prim_Elmt : Elmt_Id;
5671 Prim_Id : Entity_Id;
5672 Typ : Entity_Id := T;
5674 begin
5675 if Is_Class_Wide_Type (Typ) then
5676 Typ := Root_Type (Typ);
5677 end if;
5679 Typ := Underlying_Type (Typ);
5681 -- This search is based on the assertion that the dispatching version
5682 -- of the TSS routine always precedes the real primitive.
5684 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
5685 while Present (Prim_Elmt) loop
5686 Prim_Id := Node (Prim_Elmt);
5688 if Is_TSS (Prim_Id, Name) then
5689 if Present (Alias (Prim_Id)) then
5690 Inher_Op := Prim_Id;
5691 else
5692 Own_Op := Prim_Id;
5693 end if;
5694 end if;
5696 Next_Elmt (Prim_Elmt);
5697 end loop;
5699 if Present (Own_Op) then
5700 return Own_Op;
5701 elsif Present (Inher_Op) then
5702 return Inher_Op;
5703 else
5704 return Empty;
5705 end if;
5706 end Find_Optional_Prim_Op;
5708 ------------------
5709 -- Find_Prim_Op --
5710 ------------------
5712 function Find_Prim_Op
5713 (T : Entity_Id; Name : Name_Id) return Entity_Id
5715 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5716 begin
5717 if No (Result) then
5718 raise Program_Error;
5719 end if;
5721 return Result;
5722 end Find_Prim_Op;
5724 ------------------
5725 -- Find_Prim_Op --
5726 ------------------
5728 function Find_Prim_Op
5729 (T : Entity_Id;
5730 Name : TSS_Name_Type) return Entity_Id
5732 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
5733 begin
5734 if No (Result) then
5735 raise Program_Error;
5736 end if;
5738 return Result;
5739 end Find_Prim_Op;
5741 ----------------------------
5742 -- Find_Protection_Object --
5743 ----------------------------
5745 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
5746 S : Entity_Id;
5748 begin
5749 S := Scop;
5750 while Present (S) loop
5751 if Ekind_In (S, E_Entry, E_Entry_Family, E_Function, E_Procedure)
5752 and then Present (Protection_Object (S))
5753 then
5754 return Protection_Object (S);
5755 end if;
5757 S := Scope (S);
5758 end loop;
5760 -- If we do not find a Protection object in the scope chain, then
5761 -- something has gone wrong, most likely the object was never created.
5763 raise Program_Error;
5764 end Find_Protection_Object;
5766 --------------------------
5767 -- Find_Protection_Type --
5768 --------------------------
5770 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
5771 Comp : Entity_Id;
5772 Typ : Entity_Id := Conc_Typ;
5774 begin
5775 if Is_Concurrent_Type (Typ) then
5776 Typ := Corresponding_Record_Type (Typ);
5777 end if;
5779 -- Since restriction violations are not considered serious errors, the
5780 -- expander remains active, but may leave the corresponding record type
5781 -- malformed. In such cases, component _object is not available so do
5782 -- not look for it.
5784 if not Analyzed (Typ) then
5785 return Empty;
5786 end if;
5788 Comp := First_Component (Typ);
5789 while Present (Comp) loop
5790 if Chars (Comp) = Name_uObject then
5791 return Base_Type (Etype (Comp));
5792 end if;
5794 Next_Component (Comp);
5795 end loop;
5797 -- The corresponding record of a protected type should always have an
5798 -- _object field.
5800 raise Program_Error;
5801 end Find_Protection_Type;
5803 -----------------------
5804 -- Find_Hook_Context --
5805 -----------------------
5807 function Find_Hook_Context (N : Node_Id) return Node_Id is
5808 Par : Node_Id;
5809 Top : Node_Id;
5811 Wrapped_Node : Node_Id;
5812 -- Note: if we are in a transient scope, we want to reuse it as
5813 -- the context for actions insertion, if possible. But if N is itself
5814 -- part of the stored actions for the current transient scope,
5815 -- then we need to insert at the appropriate (inner) location in
5816 -- the not as an action on Node_To_Be_Wrapped.
5818 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
5820 begin
5821 -- When the node is inside a case/if expression, the lifetime of any
5822 -- temporary controlled object is extended. Find a suitable insertion
5823 -- node by locating the topmost case or if expressions.
5825 if In_Cond_Expr then
5826 Par := N;
5827 Top := N;
5828 while Present (Par) loop
5829 if Nkind_In (Original_Node (Par), N_Case_Expression,
5830 N_If_Expression)
5831 then
5832 Top := Par;
5834 -- Prevent the search from going too far
5836 elsif Is_Body_Or_Package_Declaration (Par) then
5837 exit;
5838 end if;
5840 Par := Parent (Par);
5841 end loop;
5843 -- The topmost case or if expression is now recovered, but it may
5844 -- still not be the correct place to add generated code. Climb to
5845 -- find a parent that is part of a declarative or statement list,
5846 -- and is not a list of actuals in a call.
5848 Par := Top;
5849 while Present (Par) loop
5850 if Is_List_Member (Par)
5851 and then not Nkind_In (Par, N_Component_Association,
5852 N_Discriminant_Association,
5853 N_Parameter_Association,
5854 N_Pragma_Argument_Association)
5855 and then not Nkind_In (Parent (Par), N_Function_Call,
5856 N_Procedure_Call_Statement,
5857 N_Entry_Call_Statement)
5859 then
5860 return Par;
5862 -- Prevent the search from going too far
5864 elsif Is_Body_Or_Package_Declaration (Par) then
5865 exit;
5866 end if;
5868 Par := Parent (Par);
5869 end loop;
5871 return Par;
5873 else
5874 Par := N;
5875 while Present (Par) loop
5877 -- Keep climbing past various operators
5879 if Nkind (Parent (Par)) in N_Op
5880 or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else)
5881 then
5882 Par := Parent (Par);
5883 else
5884 exit;
5885 end if;
5886 end loop;
5888 Top := Par;
5890 -- The node may be located in a pragma in which case return the
5891 -- pragma itself:
5893 -- pragma Precondition (... and then Ctrl_Func_Call ...);
5895 -- Similar case occurs when the node is related to an object
5896 -- declaration or assignment:
5898 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
5900 -- Another case to consider is when the node is part of a return
5901 -- statement:
5903 -- return ... and then Ctrl_Func_Call ...;
5905 -- Another case is when the node acts as a formal in a procedure
5906 -- call statement:
5908 -- Proc (... and then Ctrl_Func_Call ...);
5910 if Scope_Is_Transient then
5911 Wrapped_Node := Node_To_Be_Wrapped;
5912 else
5913 Wrapped_Node := Empty;
5914 end if;
5916 while Present (Par) loop
5917 if Par = Wrapped_Node
5918 or else Nkind_In (Par, N_Assignment_Statement,
5919 N_Object_Declaration,
5920 N_Pragma,
5921 N_Procedure_Call_Statement,
5922 N_Simple_Return_Statement)
5923 then
5924 return Par;
5926 -- Prevent the search from going too far
5928 elsif Is_Body_Or_Package_Declaration (Par) then
5929 exit;
5930 end if;
5932 Par := Parent (Par);
5933 end loop;
5935 -- Return the topmost short circuit operator
5937 return Top;
5938 end if;
5939 end Find_Hook_Context;
5941 ------------------------------
5942 -- Following_Address_Clause --
5943 ------------------------------
5945 function Following_Address_Clause (D : Node_Id) return Node_Id is
5946 Id : constant Entity_Id := Defining_Identifier (D);
5947 Result : Node_Id;
5948 Par : Node_Id;
5950 function Check_Decls (D : Node_Id) return Node_Id;
5951 -- This internal function differs from the main function in that it
5952 -- gets called to deal with a following package private part, and
5953 -- it checks declarations starting with D (the main function checks
5954 -- declarations following D). If D is Empty, then Empty is returned.
5956 -----------------
5957 -- Check_Decls --
5958 -----------------
5960 function Check_Decls (D : Node_Id) return Node_Id is
5961 Decl : Node_Id;
5963 begin
5964 Decl := D;
5965 while Present (Decl) loop
5966 if Nkind (Decl) = N_At_Clause
5967 and then Chars (Identifier (Decl)) = Chars (Id)
5968 then
5969 return Decl;
5971 elsif Nkind (Decl) = N_Attribute_Definition_Clause
5972 and then Chars (Decl) = Name_Address
5973 and then Chars (Name (Decl)) = Chars (Id)
5974 then
5975 return Decl;
5976 end if;
5978 Next (Decl);
5979 end loop;
5981 -- Otherwise not found, return Empty
5983 return Empty;
5984 end Check_Decls;
5986 -- Start of processing for Following_Address_Clause
5988 begin
5989 -- If parser detected no address clause for the identifier in question,
5990 -- then the answer is a quick NO, without the need for a search.
5992 if not Get_Name_Table_Boolean1 (Chars (Id)) then
5993 return Empty;
5994 end if;
5996 -- Otherwise search current declarative unit
5998 Result := Check_Decls (Next (D));
6000 if Present (Result) then
6001 return Result;
6002 end if;
6004 -- Check for possible package private part following
6006 Par := Parent (D);
6008 if Nkind (Par) = N_Package_Specification
6009 and then Visible_Declarations (Par) = List_Containing (D)
6010 and then Present (Private_Declarations (Par))
6011 then
6012 -- Private part present, check declarations there
6014 return Check_Decls (First (Private_Declarations (Par)));
6016 else
6017 -- No private part, clause not found, return Empty
6019 return Empty;
6020 end if;
6021 end Following_Address_Clause;
6023 ----------------------
6024 -- Force_Evaluation --
6025 ----------------------
6027 procedure Force_Evaluation
6028 (Exp : Node_Id;
6029 Name_Req : Boolean := False;
6030 Related_Id : Entity_Id := Empty;
6031 Is_Low_Bound : Boolean := False;
6032 Is_High_Bound : Boolean := False;
6033 Mode : Force_Evaluation_Mode := Relaxed)
6035 begin
6036 Remove_Side_Effects
6037 (Exp => Exp,
6038 Name_Req => Name_Req,
6039 Variable_Ref => True,
6040 Renaming_Req => False,
6041 Related_Id => Related_Id,
6042 Is_Low_Bound => Is_Low_Bound,
6043 Is_High_Bound => Is_High_Bound,
6044 Check_Side_Effects =>
6045 Is_Static_Expression (Exp)
6046 or else Mode = Relaxed);
6047 end Force_Evaluation;
6049 ---------------------------------
6050 -- Fully_Qualified_Name_String --
6051 ---------------------------------
6053 function Fully_Qualified_Name_String
6054 (E : Entity_Id;
6055 Append_NUL : Boolean := True) return String_Id
6057 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6058 -- Compute recursively the qualified name without NUL at the end, adding
6059 -- it to the currently started string being generated
6061 ----------------------------------
6062 -- Internal_Full_Qualified_Name --
6063 ----------------------------------
6065 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6066 Ent : Entity_Id;
6068 begin
6069 -- Deal properly with child units
6071 if Nkind (E) = N_Defining_Program_Unit_Name then
6072 Ent := Defining_Identifier (E);
6073 else
6074 Ent := E;
6075 end if;
6077 -- Compute qualification recursively (only "Standard" has no scope)
6079 if Present (Scope (Scope (Ent))) then
6080 Internal_Full_Qualified_Name (Scope (Ent));
6081 Store_String_Char (Get_Char_Code ('.'));
6082 end if;
6084 -- Every entity should have a name except some expanded blocks
6085 -- don't bother about those.
6087 if Chars (Ent) = No_Name then
6088 return;
6089 end if;
6091 -- Generates the entity name in upper case
6093 Get_Decoded_Name_String (Chars (Ent));
6094 Set_All_Upper_Case;
6095 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6096 return;
6097 end Internal_Full_Qualified_Name;
6099 -- Start of processing for Full_Qualified_Name
6101 begin
6102 Start_String;
6103 Internal_Full_Qualified_Name (E);
6105 if Append_NUL then
6106 Store_String_Char (Get_Char_Code (ASCII.NUL));
6107 end if;
6109 return End_String;
6110 end Fully_Qualified_Name_String;
6112 ------------------------
6113 -- Generate_Poll_Call --
6114 ------------------------
6116 procedure Generate_Poll_Call (N : Node_Id) is
6117 begin
6118 -- No poll call if polling not active
6120 if not Polling_Required then
6121 return;
6123 -- Otherwise generate require poll call
6125 else
6126 Insert_Before_And_Analyze (N,
6127 Make_Procedure_Call_Statement (Sloc (N),
6128 Name => New_Occurrence_Of (RTE (RE_Poll), Sloc (N))));
6129 end if;
6130 end Generate_Poll_Call;
6132 ---------------------------------
6133 -- Get_Current_Value_Condition --
6134 ---------------------------------
6136 -- Note: the implementation of this procedure is very closely tied to the
6137 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6138 -- interpret Current_Value fields set by the Set procedure, so the two
6139 -- procedures need to be closely coordinated.
6141 procedure Get_Current_Value_Condition
6142 (Var : Node_Id;
6143 Op : out Node_Kind;
6144 Val : out Node_Id)
6146 Loc : constant Source_Ptr := Sloc (Var);
6147 Ent : constant Entity_Id := Entity (Var);
6149 procedure Process_Current_Value_Condition
6150 (N : Node_Id;
6151 S : Boolean);
6152 -- N is an expression which holds either True (S = True) or False (S =
6153 -- False) in the condition. This procedure digs out the expression and
6154 -- if it refers to Ent, sets Op and Val appropriately.
6156 -------------------------------------
6157 -- Process_Current_Value_Condition --
6158 -------------------------------------
6160 procedure Process_Current_Value_Condition
6161 (N : Node_Id;
6162 S : Boolean)
6164 Cond : Node_Id;
6165 Prev_Cond : Node_Id;
6166 Sens : Boolean;
6168 begin
6169 Cond := N;
6170 Sens := S;
6172 loop
6173 Prev_Cond := Cond;
6175 -- Deal with NOT operators, inverting sense
6177 while Nkind (Cond) = N_Op_Not loop
6178 Cond := Right_Opnd (Cond);
6179 Sens := not Sens;
6180 end loop;
6182 -- Deal with conversions, qualifications, and expressions with
6183 -- actions.
6185 while Nkind_In (Cond,
6186 N_Type_Conversion,
6187 N_Qualified_Expression,
6188 N_Expression_With_Actions)
6189 loop
6190 Cond := Expression (Cond);
6191 end loop;
6193 exit when Cond = Prev_Cond;
6194 end loop;
6196 -- Deal with AND THEN and AND cases
6198 if Nkind_In (Cond, N_And_Then, N_Op_And) then
6200 -- Don't ever try to invert a condition that is of the form of an
6201 -- AND or AND THEN (since we are not doing sufficiently general
6202 -- processing to allow this).
6204 if Sens = False then
6205 Op := N_Empty;
6206 Val := Empty;
6207 return;
6208 end if;
6210 -- Recursively process AND and AND THEN branches
6212 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6214 if Op /= N_Empty then
6215 return;
6216 end if;
6218 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6219 return;
6221 -- Case of relational operator
6223 elsif Nkind (Cond) in N_Op_Compare then
6224 Op := Nkind (Cond);
6226 -- Invert sense of test if inverted test
6228 if Sens = False then
6229 case Op is
6230 when N_Op_Eq => Op := N_Op_Ne;
6231 when N_Op_Ne => Op := N_Op_Eq;
6232 when N_Op_Lt => Op := N_Op_Ge;
6233 when N_Op_Gt => Op := N_Op_Le;
6234 when N_Op_Le => Op := N_Op_Gt;
6235 when N_Op_Ge => Op := N_Op_Lt;
6236 when others => raise Program_Error;
6237 end case;
6238 end if;
6240 -- Case of entity op value
6242 if Is_Entity_Name (Left_Opnd (Cond))
6243 and then Ent = Entity (Left_Opnd (Cond))
6244 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6245 then
6246 Val := Right_Opnd (Cond);
6248 -- Case of value op entity
6250 elsif Is_Entity_Name (Right_Opnd (Cond))
6251 and then Ent = Entity (Right_Opnd (Cond))
6252 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6253 then
6254 Val := Left_Opnd (Cond);
6256 -- We are effectively swapping operands
6258 case Op is
6259 when N_Op_Eq => null;
6260 when N_Op_Ne => null;
6261 when N_Op_Lt => Op := N_Op_Gt;
6262 when N_Op_Gt => Op := N_Op_Lt;
6263 when N_Op_Le => Op := N_Op_Ge;
6264 when N_Op_Ge => Op := N_Op_Le;
6265 when others => raise Program_Error;
6266 end case;
6268 else
6269 Op := N_Empty;
6270 end if;
6272 return;
6274 elsif Nkind_In (Cond,
6275 N_Type_Conversion,
6276 N_Qualified_Expression,
6277 N_Expression_With_Actions)
6278 then
6279 Cond := Expression (Cond);
6281 -- Case of Boolean variable reference, return as though the
6282 -- reference had said var = True.
6284 else
6285 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6286 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6288 if Sens = False then
6289 Op := N_Op_Ne;
6290 else
6291 Op := N_Op_Eq;
6292 end if;
6293 end if;
6294 end if;
6295 end Process_Current_Value_Condition;
6297 -- Start of processing for Get_Current_Value_Condition
6299 begin
6300 Op := N_Empty;
6301 Val := Empty;
6303 -- Immediate return, nothing doing, if this is not an object
6305 if Ekind (Ent) not in Object_Kind then
6306 return;
6307 end if;
6309 -- Otherwise examine current value
6311 declare
6312 CV : constant Node_Id := Current_Value (Ent);
6313 Sens : Boolean;
6314 Stm : Node_Id;
6316 begin
6317 -- If statement. Condition is known true in THEN section, known False
6318 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6320 if Nkind (CV) = N_If_Statement then
6322 -- Before start of IF statement
6324 if Loc < Sloc (CV) then
6325 return;
6327 -- After end of IF statement
6329 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6330 return;
6331 end if;
6333 -- At this stage we know that we are within the IF statement, but
6334 -- unfortunately, the tree does not record the SLOC of the ELSE so
6335 -- we cannot use a simple SLOC comparison to distinguish between
6336 -- the then/else statements, so we have to climb the tree.
6338 declare
6339 N : Node_Id;
6341 begin
6342 N := Parent (Var);
6343 while Parent (N) /= CV loop
6344 N := Parent (N);
6346 -- If we fall off the top of the tree, then that's odd, but
6347 -- perhaps it could occur in some error situation, and the
6348 -- safest response is simply to assume that the outcome of
6349 -- the condition is unknown. No point in bombing during an
6350 -- attempt to optimize things.
6352 if No (N) then
6353 return;
6354 end if;
6355 end loop;
6357 -- Now we have N pointing to a node whose parent is the IF
6358 -- statement in question, so now we can tell if we are within
6359 -- the THEN statements.
6361 if Is_List_Member (N)
6362 and then List_Containing (N) = Then_Statements (CV)
6363 then
6364 Sens := True;
6366 -- If the variable reference does not come from source, we
6367 -- cannot reliably tell whether it appears in the else part.
6368 -- In particular, if it appears in generated code for a node
6369 -- that requires finalization, it may be attached to a list
6370 -- that has not been yet inserted into the code. For now,
6371 -- treat it as unknown.
6373 elsif not Comes_From_Source (N) then
6374 return;
6376 -- Otherwise we must be in ELSIF or ELSE part
6378 else
6379 Sens := False;
6380 end if;
6381 end;
6383 -- ELSIF part. Condition is known true within the referenced
6384 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6385 -- and unknown before the ELSE part or after the IF statement.
6387 elsif Nkind (CV) = N_Elsif_Part then
6389 -- if the Elsif_Part had condition_actions, the elsif has been
6390 -- rewritten as a nested if, and the original elsif_part is
6391 -- detached from the tree, so there is no way to obtain useful
6392 -- information on the current value of the variable.
6393 -- Can this be improved ???
6395 if No (Parent (CV)) then
6396 return;
6397 end if;
6399 Stm := Parent (CV);
6401 -- If the tree has been otherwise rewritten there is nothing
6402 -- else to be done either.
6404 if Nkind (Stm) /= N_If_Statement then
6405 return;
6406 end if;
6408 -- Before start of ELSIF part
6410 if Loc < Sloc (CV) then
6411 return;
6413 -- After end of IF statement
6415 elsif Loc >= Sloc (Stm) +
6416 Text_Ptr (UI_To_Int (End_Span (Stm)))
6417 then
6418 return;
6419 end if;
6421 -- Again we lack the SLOC of the ELSE, so we need to climb the
6422 -- tree to see if we are within the ELSIF part in question.
6424 declare
6425 N : Node_Id;
6427 begin
6428 N := Parent (Var);
6429 while Parent (N) /= Stm loop
6430 N := Parent (N);
6432 -- If we fall off the top of the tree, then that's odd, but
6433 -- perhaps it could occur in some error situation, and the
6434 -- safest response is simply to assume that the outcome of
6435 -- the condition is unknown. No point in bombing during an
6436 -- attempt to optimize things.
6438 if No (N) then
6439 return;
6440 end if;
6441 end loop;
6443 -- Now we have N pointing to a node whose parent is the IF
6444 -- statement in question, so see if is the ELSIF part we want.
6445 -- the THEN statements.
6447 if N = CV then
6448 Sens := True;
6450 -- Otherwise we must be in subsequent ELSIF or ELSE part
6452 else
6453 Sens := False;
6454 end if;
6455 end;
6457 -- Iteration scheme of while loop. The condition is known to be
6458 -- true within the body of the loop.
6460 elsif Nkind (CV) = N_Iteration_Scheme then
6461 declare
6462 Loop_Stmt : constant Node_Id := Parent (CV);
6464 begin
6465 -- Before start of body of loop
6467 if Loc < Sloc (Loop_Stmt) then
6468 return;
6470 -- After end of LOOP statement
6472 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6473 return;
6475 -- We are within the body of the loop
6477 else
6478 Sens := True;
6479 end if;
6480 end;
6482 -- All other cases of Current_Value settings
6484 else
6485 return;
6486 end if;
6488 -- If we fall through here, then we have a reportable condition, Sens
6489 -- is True if the condition is true and False if it needs inverting.
6491 Process_Current_Value_Condition (Condition (CV), Sens);
6492 end;
6493 end Get_Current_Value_Condition;
6495 ---------------------
6496 -- Get_Stream_Size --
6497 ---------------------
6499 function Get_Stream_Size (E : Entity_Id) return Uint is
6500 begin
6501 -- If we have a Stream_Size clause for this type use it
6503 if Has_Stream_Size_Clause (E) then
6504 return Static_Integer (Expression (Stream_Size_Clause (E)));
6506 -- Otherwise the Stream_Size if the size of the type
6508 else
6509 return Esize (E);
6510 end if;
6511 end Get_Stream_Size;
6513 ---------------------------
6514 -- Has_Access_Constraint --
6515 ---------------------------
6517 function Has_Access_Constraint (E : Entity_Id) return Boolean is
6518 Disc : Entity_Id;
6519 T : constant Entity_Id := Etype (E);
6521 begin
6522 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
6523 Disc := First_Discriminant (T);
6524 while Present (Disc) loop
6525 if Is_Access_Type (Etype (Disc)) then
6526 return True;
6527 end if;
6529 Next_Discriminant (Disc);
6530 end loop;
6532 return False;
6533 else
6534 return False;
6535 end if;
6536 end Has_Access_Constraint;
6538 -----------------------------------------------------
6539 -- Has_Annotate_Pragma_For_External_Axiomatization --
6540 -----------------------------------------------------
6542 function Has_Annotate_Pragma_For_External_Axiomatization
6543 (E : Entity_Id) return Boolean
6545 function Is_Annotate_Pragma_For_External_Axiomatization
6546 (N : Node_Id) return Boolean;
6547 -- Returns whether N is
6548 -- pragma Annotate (GNATprove, External_Axiomatization);
6550 ----------------------------------------------------
6551 -- Is_Annotate_Pragma_For_External_Axiomatization --
6552 ----------------------------------------------------
6554 -- The general form of pragma Annotate is
6556 -- pragma Annotate (IDENTIFIER [, IDENTIFIER {, ARG}]);
6557 -- ARG ::= NAME | EXPRESSION
6559 -- The first two arguments are by convention intended to refer to an
6560 -- external tool and a tool-specific function. These arguments are
6561 -- not analyzed.
6563 -- The following is used to annotate a package specification which
6564 -- GNATprove should treat specially, because the axiomatization of
6565 -- this unit is given by the user instead of being automatically
6566 -- generated.
6568 -- pragma Annotate (GNATprove, External_Axiomatization);
6570 function Is_Annotate_Pragma_For_External_Axiomatization
6571 (N : Node_Id) return Boolean
6573 Name_GNATprove : constant String :=
6574 "gnatprove";
6575 Name_External_Axiomatization : constant String :=
6576 "external_axiomatization";
6577 -- Special names
6579 begin
6580 if Nkind (N) = N_Pragma
6581 and then Get_Pragma_Id (N) = Pragma_Annotate
6582 and then List_Length (Pragma_Argument_Associations (N)) = 2
6583 then
6584 declare
6585 Arg1 : constant Node_Id :=
6586 First (Pragma_Argument_Associations (N));
6587 Arg2 : constant Node_Id := Next (Arg1);
6588 Nam1 : Name_Id;
6589 Nam2 : Name_Id;
6591 begin
6592 -- Fill in Name_Buffer with Name_GNATprove first, and then with
6593 -- Name_External_Axiomatization so that Name_Find returns the
6594 -- corresponding name. This takes care of all possible casings.
6596 Name_Len := 0;
6597 Add_Str_To_Name_Buffer (Name_GNATprove);
6598 Nam1 := Name_Find;
6600 Name_Len := 0;
6601 Add_Str_To_Name_Buffer (Name_External_Axiomatization);
6602 Nam2 := Name_Find;
6604 return Chars (Get_Pragma_Arg (Arg1)) = Nam1
6605 and then
6606 Chars (Get_Pragma_Arg (Arg2)) = Nam2;
6607 end;
6609 else
6610 return False;
6611 end if;
6612 end Is_Annotate_Pragma_For_External_Axiomatization;
6614 -- Local variables
6616 Decl : Node_Id;
6617 Vis_Decls : List_Id;
6618 N : Node_Id;
6620 -- Start of processing for Has_Annotate_Pragma_For_External_Axiomatization
6622 begin
6623 if Nkind (Parent (E)) = N_Defining_Program_Unit_Name then
6624 Decl := Parent (Parent (E));
6625 else
6626 Decl := Parent (E);
6627 end if;
6629 Vis_Decls := Visible_Declarations (Decl);
6631 N := First (Vis_Decls);
6632 while Present (N) loop
6634 -- Skip declarations generated by the frontend. Skip all pragmas
6635 -- that are not the desired Annotate pragma. Stop the search on
6636 -- the first non-pragma source declaration.
6638 if Comes_From_Source (N) then
6639 if Nkind (N) = N_Pragma then
6640 if Is_Annotate_Pragma_For_External_Axiomatization (N) then
6641 return True;
6642 end if;
6643 else
6644 return False;
6645 end if;
6646 end if;
6648 Next (N);
6649 end loop;
6651 return False;
6652 end Has_Annotate_Pragma_For_External_Axiomatization;
6654 --------------------
6655 -- Homonym_Number --
6656 --------------------
6658 function Homonym_Number (Subp : Entity_Id) return Nat is
6659 Count : Nat;
6660 Hom : Entity_Id;
6662 begin
6663 Count := 1;
6664 Hom := Homonym (Subp);
6665 while Present (Hom) loop
6666 if Scope (Hom) = Scope (Subp) then
6667 Count := Count + 1;
6668 end if;
6670 Hom := Homonym (Hom);
6671 end loop;
6673 return Count;
6674 end Homonym_Number;
6676 -----------------------------------
6677 -- In_Library_Level_Package_Body --
6678 -----------------------------------
6680 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
6681 begin
6682 -- First determine whether the entity appears at the library level, then
6683 -- look at the containing unit.
6685 if Is_Library_Level_Entity (Id) then
6686 declare
6687 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
6689 begin
6690 return Nkind (Unit (Container)) = N_Package_Body;
6691 end;
6692 end if;
6694 return False;
6695 end In_Library_Level_Package_Body;
6697 ------------------------------
6698 -- In_Unconditional_Context --
6699 ------------------------------
6701 function In_Unconditional_Context (Node : Node_Id) return Boolean is
6702 P : Node_Id;
6704 begin
6705 P := Node;
6706 while Present (P) loop
6707 case Nkind (P) is
6708 when N_Subprogram_Body => return True;
6709 when N_If_Statement => return False;
6710 when N_Loop_Statement => return False;
6711 when N_Case_Statement => return False;
6712 when others => P := Parent (P);
6713 end case;
6714 end loop;
6716 return False;
6717 end In_Unconditional_Context;
6719 -------------------
6720 -- Insert_Action --
6721 -------------------
6723 procedure Insert_Action (Assoc_Node : Node_Id; Ins_Action : Node_Id) is
6724 begin
6725 if Present (Ins_Action) then
6726 Insert_Actions (Assoc_Node, New_List (Ins_Action));
6727 end if;
6728 end Insert_Action;
6730 -- Version with check(s) suppressed
6732 procedure Insert_Action
6733 (Assoc_Node : Node_Id; Ins_Action : Node_Id; Suppress : Check_Id)
6735 begin
6736 Insert_Actions (Assoc_Node, New_List (Ins_Action), Suppress);
6737 end Insert_Action;
6739 -------------------------
6740 -- Insert_Action_After --
6741 -------------------------
6743 procedure Insert_Action_After
6744 (Assoc_Node : Node_Id;
6745 Ins_Action : Node_Id)
6747 begin
6748 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
6749 end Insert_Action_After;
6751 --------------------
6752 -- Insert_Actions --
6753 --------------------
6755 procedure Insert_Actions (Assoc_Node : Node_Id; Ins_Actions : List_Id) is
6756 N : Node_Id;
6757 P : Node_Id;
6759 Wrapped_Node : Node_Id := Empty;
6761 begin
6762 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
6763 return;
6764 end if;
6766 -- Ignore insert of actions from inside default expression (or other
6767 -- similar "spec expression") in the special spec-expression analyze
6768 -- mode. Any insertions at this point have no relevance, since we are
6769 -- only doing the analyze to freeze the types of any static expressions.
6770 -- See section "Handling of Default Expressions" in the spec of package
6771 -- Sem for further details.
6773 if In_Spec_Expression then
6774 return;
6775 end if;
6777 -- If the action derives from stuff inside a record, then the actions
6778 -- are attached to the current scope, to be inserted and analyzed on
6779 -- exit from the scope. The reason for this is that we may also be
6780 -- generating freeze actions at the same time, and they must eventually
6781 -- be elaborated in the correct order.
6783 if Is_Record_Type (Current_Scope)
6784 and then not Is_Frozen (Current_Scope)
6785 then
6786 if No (Scope_Stack.Table
6787 (Scope_Stack.Last).Pending_Freeze_Actions)
6788 then
6789 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
6790 Ins_Actions;
6791 else
6792 Append_List
6793 (Ins_Actions,
6794 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
6795 end if;
6797 return;
6798 end if;
6800 -- We now intend to climb up the tree to find the right point to
6801 -- insert the actions. We start at Assoc_Node, unless this node is a
6802 -- subexpression in which case we start with its parent. We do this for
6803 -- two reasons. First it speeds things up. Second, if Assoc_Node is
6804 -- itself one of the special nodes like N_And_Then, then we assume that
6805 -- an initial request to insert actions for such a node does not expect
6806 -- the actions to get deposited in the node for later handling when the
6807 -- node is expanded, since clearly the node is being dealt with by the
6808 -- caller. Note that in the subexpression case, N is always the child we
6809 -- came from.
6811 -- N_Raise_xxx_Error is an annoying special case, it is a statement
6812 -- if it has type Standard_Void_Type, and a subexpression otherwise.
6813 -- Procedure calls, and similarly procedure attribute references, are
6814 -- also statements.
6816 if Nkind (Assoc_Node) in N_Subexpr
6817 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
6818 or else Etype (Assoc_Node) /= Standard_Void_Type)
6819 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
6820 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
6821 or else not Is_Procedure_Attribute_Name
6822 (Attribute_Name (Assoc_Node)))
6823 then
6824 N := Assoc_Node;
6825 P := Parent (Assoc_Node);
6827 -- Non-subexpression case. Note that N is initially Empty in this case
6828 -- (N is only guaranteed Non-Empty in the subexpr case).
6830 else
6831 N := Empty;
6832 P := Assoc_Node;
6833 end if;
6835 -- Capture root of the transient scope
6837 if Scope_Is_Transient then
6838 Wrapped_Node := Node_To_Be_Wrapped;
6839 end if;
6841 loop
6842 pragma Assert (Present (P));
6844 -- Make sure that inserted actions stay in the transient scope
6846 if Present (Wrapped_Node) and then N = Wrapped_Node then
6847 Store_Before_Actions_In_Scope (Ins_Actions);
6848 return;
6849 end if;
6851 case Nkind (P) is
6853 -- Case of right operand of AND THEN or OR ELSE. Put the actions
6854 -- in the Actions field of the right operand. They will be moved
6855 -- out further when the AND THEN or OR ELSE operator is expanded.
6856 -- Nothing special needs to be done for the left operand since
6857 -- in that case the actions are executed unconditionally.
6859 when N_Short_Circuit =>
6860 if N = Right_Opnd (P) then
6862 -- We are now going to either append the actions to the
6863 -- actions field of the short-circuit operation. We will
6864 -- also analyze the actions now.
6866 -- This analysis is really too early, the proper thing would
6867 -- be to just park them there now, and only analyze them if
6868 -- we find we really need them, and to it at the proper
6869 -- final insertion point. However attempting to this proved
6870 -- tricky, so for now we just kill current values before and
6871 -- after the analyze call to make sure we avoid peculiar
6872 -- optimizations from this out of order insertion.
6874 Kill_Current_Values;
6876 -- If P has already been expanded, we can't park new actions
6877 -- on it, so we need to expand them immediately, introducing
6878 -- an Expression_With_Actions. N can't be an expression
6879 -- with actions, or else then the actions would have been
6880 -- inserted at an inner level.
6882 if Analyzed (P) then
6883 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
6884 Rewrite (N,
6885 Make_Expression_With_Actions (Sloc (N),
6886 Actions => Ins_Actions,
6887 Expression => Relocate_Node (N)));
6888 Analyze_And_Resolve (N);
6890 elsif Present (Actions (P)) then
6891 Insert_List_After_And_Analyze
6892 (Last (Actions (P)), Ins_Actions);
6893 else
6894 Set_Actions (P, Ins_Actions);
6895 Analyze_List (Actions (P));
6896 end if;
6898 Kill_Current_Values;
6900 return;
6901 end if;
6903 -- Then or Else dependent expression of an if expression. Add
6904 -- actions to Then_Actions or Else_Actions field as appropriate.
6905 -- The actions will be moved further out when the if is expanded.
6907 when N_If_Expression =>
6908 declare
6909 ThenX : constant Node_Id := Next (First (Expressions (P)));
6910 ElseX : constant Node_Id := Next (ThenX);
6912 begin
6913 -- If the enclosing expression is already analyzed, as
6914 -- is the case for nested elaboration checks, insert the
6915 -- conditional further out.
6917 if Analyzed (P) then
6918 null;
6920 -- Actions belong to the then expression, temporarily place
6921 -- them as Then_Actions of the if expression. They will be
6922 -- moved to the proper place later when the if expression
6923 -- is expanded.
6925 elsif N = ThenX then
6926 if Present (Then_Actions (P)) then
6927 Insert_List_After_And_Analyze
6928 (Last (Then_Actions (P)), Ins_Actions);
6929 else
6930 Set_Then_Actions (P, Ins_Actions);
6931 Analyze_List (Then_Actions (P));
6932 end if;
6934 return;
6936 -- Actions belong to the else expression, temporarily place
6937 -- them as Else_Actions of the if expression. They will be
6938 -- moved to the proper place later when the if expression
6939 -- is expanded.
6941 elsif N = ElseX then
6942 if Present (Else_Actions (P)) then
6943 Insert_List_After_And_Analyze
6944 (Last (Else_Actions (P)), Ins_Actions);
6945 else
6946 Set_Else_Actions (P, Ins_Actions);
6947 Analyze_List (Else_Actions (P));
6948 end if;
6950 return;
6952 -- Actions belong to the condition. In this case they are
6953 -- unconditionally executed, and so we can continue the
6954 -- search for the proper insert point.
6956 else
6957 null;
6958 end if;
6959 end;
6961 -- Alternative of case expression, we place the action in the
6962 -- Actions field of the case expression alternative, this will
6963 -- be handled when the case expression is expanded.
6965 when N_Case_Expression_Alternative =>
6966 if Present (Actions (P)) then
6967 Insert_List_After_And_Analyze
6968 (Last (Actions (P)), Ins_Actions);
6969 else
6970 Set_Actions (P, Ins_Actions);
6971 Analyze_List (Actions (P));
6972 end if;
6974 return;
6976 -- Case of appearing within an Expressions_With_Actions node. When
6977 -- the new actions come from the expression of the expression with
6978 -- actions, they must be added to the existing actions. The other
6979 -- alternative is when the new actions are related to one of the
6980 -- existing actions of the expression with actions, and should
6981 -- never reach here: if actions are inserted on a statement
6982 -- within the Actions of an expression with actions, or on some
6983 -- subexpression of such a statement, then the outermost proper
6984 -- insertion point is right before the statement, and we should
6985 -- never climb up as far as the N_Expression_With_Actions itself.
6987 when N_Expression_With_Actions =>
6988 if N = Expression (P) then
6989 if Is_Empty_List (Actions (P)) then
6990 Append_List_To (Actions (P), Ins_Actions);
6991 Analyze_List (Actions (P));
6992 else
6993 Insert_List_After_And_Analyze
6994 (Last (Actions (P)), Ins_Actions);
6995 end if;
6997 return;
6999 else
7000 raise Program_Error;
7001 end if;
7003 -- Case of appearing in the condition of a while expression or
7004 -- elsif. We insert the actions into the Condition_Actions field.
7005 -- They will be moved further out when the while loop or elsif
7006 -- is analyzed.
7008 when N_Elsif_Part
7009 | N_Iteration_Scheme
7011 if N = Condition (P) then
7012 if Present (Condition_Actions (P)) then
7013 Insert_List_After_And_Analyze
7014 (Last (Condition_Actions (P)), Ins_Actions);
7015 else
7016 Set_Condition_Actions (P, Ins_Actions);
7018 -- Set the parent of the insert actions explicitly. This
7019 -- is not a syntactic field, but we need the parent field
7020 -- set, in particular so that freeze can understand that
7021 -- it is dealing with condition actions, and properly
7022 -- insert the freezing actions.
7024 Set_Parent (Ins_Actions, P);
7025 Analyze_List (Condition_Actions (P));
7026 end if;
7028 return;
7029 end if;
7031 -- Statements, declarations, pragmas, representation clauses
7033 when
7034 -- Statements
7036 N_Procedure_Call_Statement
7037 | N_Statement_Other_Than_Procedure_Call
7039 -- Pragmas
7041 | N_Pragma
7043 -- Representation_Clause
7045 | N_At_Clause
7046 | N_Attribute_Definition_Clause
7047 | N_Enumeration_Representation_Clause
7048 | N_Record_Representation_Clause
7050 -- Declarations
7052 | N_Abstract_Subprogram_Declaration
7053 | N_Entry_Body
7054 | N_Exception_Declaration
7055 | N_Exception_Renaming_Declaration
7056 | N_Expression_Function
7057 | N_Formal_Abstract_Subprogram_Declaration
7058 | N_Formal_Concrete_Subprogram_Declaration
7059 | N_Formal_Object_Declaration
7060 | N_Formal_Type_Declaration
7061 | N_Full_Type_Declaration
7062 | N_Function_Instantiation
7063 | N_Generic_Function_Renaming_Declaration
7064 | N_Generic_Package_Declaration
7065 | N_Generic_Package_Renaming_Declaration
7066 | N_Generic_Procedure_Renaming_Declaration
7067 | N_Generic_Subprogram_Declaration
7068 | N_Implicit_Label_Declaration
7069 | N_Incomplete_Type_Declaration
7070 | N_Number_Declaration
7071 | N_Object_Declaration
7072 | N_Object_Renaming_Declaration
7073 | N_Package_Body
7074 | N_Package_Body_Stub
7075 | N_Package_Declaration
7076 | N_Package_Instantiation
7077 | N_Package_Renaming_Declaration
7078 | N_Private_Extension_Declaration
7079 | N_Private_Type_Declaration
7080 | N_Procedure_Instantiation
7081 | N_Protected_Body
7082 | N_Protected_Body_Stub
7083 | N_Protected_Type_Declaration
7084 | N_Single_Task_Declaration
7085 | N_Subprogram_Body
7086 | N_Subprogram_Body_Stub
7087 | N_Subprogram_Declaration
7088 | N_Subprogram_Renaming_Declaration
7089 | N_Subtype_Declaration
7090 | N_Task_Body
7091 | N_Task_Body_Stub
7092 | N_Task_Type_Declaration
7094 -- Use clauses can appear in lists of declarations
7096 | N_Use_Package_Clause
7097 | N_Use_Type_Clause
7099 -- Freeze entity behaves like a declaration or statement
7101 | N_Freeze_Entity
7102 | N_Freeze_Generic_Entity
7104 -- Do not insert here if the item is not a list member (this
7105 -- happens for example with a triggering statement, and the
7106 -- proper approach is to insert before the entire select).
7108 if not Is_List_Member (P) then
7109 null;
7111 -- Do not insert if parent of P is an N_Component_Association
7112 -- node (i.e. we are in the context of an N_Aggregate or
7113 -- N_Extension_Aggregate node. In this case we want to insert
7114 -- before the entire aggregate.
7116 elsif Nkind (Parent (P)) = N_Component_Association then
7117 null;
7119 -- Do not insert if the parent of P is either an N_Variant node
7120 -- or an N_Record_Definition node, meaning in either case that
7121 -- P is a member of a component list, and that therefore the
7122 -- actions should be inserted outside the complete record
7123 -- declaration.
7125 elsif Nkind_In (Parent (P), N_Variant, N_Record_Definition) then
7126 null;
7128 -- Do not insert freeze nodes within the loop generated for
7129 -- an aggregate, because they may be elaborated too late for
7130 -- subsequent use in the back end: within a package spec the
7131 -- loop is part of the elaboration procedure and is only
7132 -- elaborated during the second pass.
7134 -- If the loop comes from source, or the entity is local to the
7135 -- loop itself it must remain within.
7137 elsif Nkind (Parent (P)) = N_Loop_Statement
7138 and then not Comes_From_Source (Parent (P))
7139 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7140 and then
7141 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7142 then
7143 null;
7145 -- Otherwise we can go ahead and do the insertion
7147 elsif P = Wrapped_Node then
7148 Store_Before_Actions_In_Scope (Ins_Actions);
7149 return;
7151 else
7152 Insert_List_Before_And_Analyze (P, Ins_Actions);
7153 return;
7154 end if;
7156 -- A special case, N_Raise_xxx_Error can act either as a statement
7157 -- or a subexpression. We tell the difference by looking at the
7158 -- Etype. It is set to Standard_Void_Type in the statement case.
7160 when N_Raise_xxx_Error =>
7161 if Etype (P) = Standard_Void_Type then
7162 if P = Wrapped_Node then
7163 Store_Before_Actions_In_Scope (Ins_Actions);
7164 else
7165 Insert_List_Before_And_Analyze (P, Ins_Actions);
7166 end if;
7168 return;
7170 -- In the subexpression case, keep climbing
7172 else
7173 null;
7174 end if;
7176 -- If a component association appears within a loop created for
7177 -- an array aggregate, attach the actions to the association so
7178 -- they can be subsequently inserted within the loop. For other
7179 -- component associations insert outside of the aggregate. For
7180 -- an association that will generate a loop, its Loop_Actions
7181 -- attribute is already initialized (see exp_aggr.adb).
7183 -- The list of Loop_Actions can in turn generate additional ones,
7184 -- that are inserted before the associated node. If the associated
7185 -- node is outside the aggregate, the new actions are collected
7186 -- at the end of the Loop_Actions, to respect the order in which
7187 -- they are to be elaborated.
7189 when N_Component_Association
7190 | N_Iterated_Component_Association
7192 if Nkind (Parent (P)) = N_Aggregate
7193 and then Present (Loop_Actions (P))
7194 then
7195 if Is_Empty_List (Loop_Actions (P)) then
7196 Set_Loop_Actions (P, Ins_Actions);
7197 Analyze_List (Ins_Actions);
7198 else
7199 declare
7200 Decl : Node_Id;
7202 begin
7203 -- Check whether these actions were generated by a
7204 -- declaration that is part of the Loop_Actions for
7205 -- the component_association.
7207 Decl := Assoc_Node;
7208 while Present (Decl) loop
7209 exit when Parent (Decl) = P
7210 and then Is_List_Member (Decl)
7211 and then
7212 List_Containing (Decl) = Loop_Actions (P);
7213 Decl := Parent (Decl);
7214 end loop;
7216 if Present (Decl) then
7217 Insert_List_Before_And_Analyze
7218 (Decl, Ins_Actions);
7219 else
7220 Insert_List_After_And_Analyze
7221 (Last (Loop_Actions (P)), Ins_Actions);
7222 end if;
7223 end;
7224 end if;
7226 return;
7228 else
7229 null;
7230 end if;
7232 -- Another special case, an attribute denoting a procedure call
7234 when N_Attribute_Reference =>
7235 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7236 if P = Wrapped_Node then
7237 Store_Before_Actions_In_Scope (Ins_Actions);
7238 else
7239 Insert_List_Before_And_Analyze (P, Ins_Actions);
7240 end if;
7242 return;
7244 -- In the subexpression case, keep climbing
7246 else
7247 null;
7248 end if;
7250 -- A contract node should not belong to the tree
7252 when N_Contract =>
7253 raise Program_Error;
7255 -- For all other node types, keep climbing tree
7257 when N_Abortable_Part
7258 | N_Accept_Alternative
7259 | N_Access_Definition
7260 | N_Access_Function_Definition
7261 | N_Access_Procedure_Definition
7262 | N_Access_To_Object_Definition
7263 | N_Aggregate
7264 | N_Allocator
7265 | N_Aspect_Specification
7266 | N_Case_Expression
7267 | N_Case_Statement_Alternative
7268 | N_Character_Literal
7269 | N_Compilation_Unit
7270 | N_Compilation_Unit_Aux
7271 | N_Component_Clause
7272 | N_Component_Declaration
7273 | N_Component_Definition
7274 | N_Component_List
7275 | N_Constrained_Array_Definition
7276 | N_Decimal_Fixed_Point_Definition
7277 | N_Defining_Character_Literal
7278 | N_Defining_Identifier
7279 | N_Defining_Operator_Symbol
7280 | N_Defining_Program_Unit_Name
7281 | N_Delay_Alternative
7282 | N_Delta_Aggregate
7283 | N_Delta_Constraint
7284 | N_Derived_Type_Definition
7285 | N_Designator
7286 | N_Digits_Constraint
7287 | N_Discriminant_Association
7288 | N_Discriminant_Specification
7289 | N_Empty
7290 | N_Entry_Body_Formal_Part
7291 | N_Entry_Call_Alternative
7292 | N_Entry_Declaration
7293 | N_Entry_Index_Specification
7294 | N_Enumeration_Type_Definition
7295 | N_Error
7296 | N_Exception_Handler
7297 | N_Expanded_Name
7298 | N_Explicit_Dereference
7299 | N_Extension_Aggregate
7300 | N_Floating_Point_Definition
7301 | N_Formal_Decimal_Fixed_Point_Definition
7302 | N_Formal_Derived_Type_Definition
7303 | N_Formal_Discrete_Type_Definition
7304 | N_Formal_Floating_Point_Definition
7305 | N_Formal_Modular_Type_Definition
7306 | N_Formal_Ordinary_Fixed_Point_Definition
7307 | N_Formal_Package_Declaration
7308 | N_Formal_Private_Type_Definition
7309 | N_Formal_Incomplete_Type_Definition
7310 | N_Formal_Signed_Integer_Type_Definition
7311 | N_Function_Call
7312 | N_Function_Specification
7313 | N_Generic_Association
7314 | N_Handled_Sequence_Of_Statements
7315 | N_Identifier
7316 | N_In
7317 | N_Index_Or_Discriminant_Constraint
7318 | N_Indexed_Component
7319 | N_Integer_Literal
7320 | N_Iterator_Specification
7321 | N_Itype_Reference
7322 | N_Label
7323 | N_Loop_Parameter_Specification
7324 | N_Mod_Clause
7325 | N_Modular_Type_Definition
7326 | N_Not_In
7327 | N_Null
7328 | N_Op_Abs
7329 | N_Op_Add
7330 | N_Op_And
7331 | N_Op_Concat
7332 | N_Op_Divide
7333 | N_Op_Eq
7334 | N_Op_Expon
7335 | N_Op_Ge
7336 | N_Op_Gt
7337 | N_Op_Le
7338 | N_Op_Lt
7339 | N_Op_Minus
7340 | N_Op_Mod
7341 | N_Op_Multiply
7342 | N_Op_Ne
7343 | N_Op_Not
7344 | N_Op_Or
7345 | N_Op_Plus
7346 | N_Op_Rem
7347 | N_Op_Rotate_Left
7348 | N_Op_Rotate_Right
7349 | N_Op_Shift_Left
7350 | N_Op_Shift_Right
7351 | N_Op_Shift_Right_Arithmetic
7352 | N_Op_Subtract
7353 | N_Op_Xor
7354 | N_Operator_Symbol
7355 | N_Ordinary_Fixed_Point_Definition
7356 | N_Others_Choice
7357 | N_Package_Specification
7358 | N_Parameter_Association
7359 | N_Parameter_Specification
7360 | N_Pop_Constraint_Error_Label
7361 | N_Pop_Program_Error_Label
7362 | N_Pop_Storage_Error_Label
7363 | N_Pragma_Argument_Association
7364 | N_Procedure_Specification
7365 | N_Protected_Definition
7366 | N_Push_Constraint_Error_Label
7367 | N_Push_Program_Error_Label
7368 | N_Push_Storage_Error_Label
7369 | N_Qualified_Expression
7370 | N_Quantified_Expression
7371 | N_Raise_Expression
7372 | N_Range
7373 | N_Range_Constraint
7374 | N_Real_Literal
7375 | N_Real_Range_Specification
7376 | N_Record_Definition
7377 | N_Reference
7378 | N_SCIL_Dispatch_Table_Tag_Init
7379 | N_SCIL_Dispatching_Call
7380 | N_SCIL_Membership_Test
7381 | N_Selected_Component
7382 | N_Signed_Integer_Type_Definition
7383 | N_Single_Protected_Declaration
7384 | N_Slice
7385 | N_String_Literal
7386 | N_Subtype_Indication
7387 | N_Subunit
7388 | N_Target_Name
7389 | N_Task_Definition
7390 | N_Terminate_Alternative
7391 | N_Triggering_Alternative
7392 | N_Type_Conversion
7393 | N_Unchecked_Expression
7394 | N_Unchecked_Type_Conversion
7395 | N_Unconstrained_Array_Definition
7396 | N_Unused_At_End
7397 | N_Unused_At_Start
7398 | N_Variant
7399 | N_Variant_Part
7400 | N_Validate_Unchecked_Conversion
7401 | N_With_Clause
7403 null;
7404 end case;
7406 -- If we fall through above tests, keep climbing tree
7408 N := P;
7410 if Nkind (Parent (N)) = N_Subunit then
7412 -- This is the proper body corresponding to a stub. Insertion must
7413 -- be done at the point of the stub, which is in the declarative
7414 -- part of the parent unit.
7416 P := Corresponding_Stub (Parent (N));
7418 else
7419 P := Parent (N);
7420 end if;
7421 end loop;
7422 end Insert_Actions;
7424 -- Version with check(s) suppressed
7426 procedure Insert_Actions
7427 (Assoc_Node : Node_Id;
7428 Ins_Actions : List_Id;
7429 Suppress : Check_Id)
7431 begin
7432 if Suppress = All_Checks then
7433 declare
7434 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7435 begin
7436 Scope_Suppress.Suppress := (others => True);
7437 Insert_Actions (Assoc_Node, Ins_Actions);
7438 Scope_Suppress.Suppress := Sva;
7439 end;
7441 else
7442 declare
7443 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7444 begin
7445 Scope_Suppress.Suppress (Suppress) := True;
7446 Insert_Actions (Assoc_Node, Ins_Actions);
7447 Scope_Suppress.Suppress (Suppress) := Svg;
7448 end;
7449 end if;
7450 end Insert_Actions;
7452 --------------------------
7453 -- Insert_Actions_After --
7454 --------------------------
7456 procedure Insert_Actions_After
7457 (Assoc_Node : Node_Id;
7458 Ins_Actions : List_Id)
7460 begin
7461 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7462 Store_After_Actions_In_Scope (Ins_Actions);
7463 else
7464 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7465 end if;
7466 end Insert_Actions_After;
7468 ------------------------
7469 -- Insert_Declaration --
7470 ------------------------
7472 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7473 P : Node_Id;
7475 begin
7476 pragma Assert (Nkind (N) in N_Subexpr);
7478 -- Climb until we find a procedure or a package
7480 P := N;
7481 loop
7482 pragma Assert (Present (Parent (P)));
7483 P := Parent (P);
7485 if Is_List_Member (P) then
7486 exit when Nkind_In (Parent (P), N_Package_Specification,
7487 N_Subprogram_Body);
7489 -- Special handling for handled sequence of statements, we must
7490 -- insert in the statements not the exception handlers!
7492 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7493 P := First (Statements (Parent (P)));
7494 exit;
7495 end if;
7496 end if;
7497 end loop;
7499 -- Now do the insertion
7501 Insert_Before (P, Decl);
7502 Analyze (Decl);
7503 end Insert_Declaration;
7505 ---------------------------------
7506 -- Insert_Library_Level_Action --
7507 ---------------------------------
7509 procedure Insert_Library_Level_Action (N : Node_Id) is
7510 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7512 begin
7513 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7514 -- And not Main_Unit as previously. If the main unit is a body,
7515 -- the scope needed to analyze the actions is the entity of the
7516 -- corresponding declaration.
7518 if No (Actions (Aux)) then
7519 Set_Actions (Aux, New_List (N));
7520 else
7521 Append (N, Actions (Aux));
7522 end if;
7524 Analyze (N);
7525 Pop_Scope;
7526 end Insert_Library_Level_Action;
7528 ----------------------------------
7529 -- Insert_Library_Level_Actions --
7530 ----------------------------------
7532 procedure Insert_Library_Level_Actions (L : List_Id) is
7533 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7535 begin
7536 if Is_Non_Empty_List (L) then
7537 Push_Scope (Cunit_Entity (Main_Unit));
7538 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7540 if No (Actions (Aux)) then
7541 Set_Actions (Aux, L);
7542 Analyze_List (L);
7543 else
7544 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7545 end if;
7547 Pop_Scope;
7548 end if;
7549 end Insert_Library_Level_Actions;
7551 ----------------------
7552 -- Inside_Init_Proc --
7553 ----------------------
7555 function Inside_Init_Proc return Boolean is
7556 S : Entity_Id;
7558 begin
7559 S := Current_Scope;
7560 while Present (S) and then S /= Standard_Standard loop
7561 if Is_Init_Proc (S) then
7562 return True;
7563 else
7564 S := Scope (S);
7565 end if;
7566 end loop;
7568 return False;
7569 end Inside_Init_Proc;
7571 ----------------------------
7572 -- Is_All_Null_Statements --
7573 ----------------------------
7575 function Is_All_Null_Statements (L : List_Id) return Boolean is
7576 Stm : Node_Id;
7578 begin
7579 Stm := First (L);
7580 while Present (Stm) loop
7581 if Nkind (Stm) /= N_Null_Statement then
7582 return False;
7583 end if;
7585 Next (Stm);
7586 end loop;
7588 return True;
7589 end Is_All_Null_Statements;
7591 --------------------------------------------------
7592 -- Is_Displacement_Of_Object_Or_Function_Result --
7593 --------------------------------------------------
7595 function Is_Displacement_Of_Object_Or_Function_Result
7596 (Obj_Id : Entity_Id) return Boolean
7598 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
7599 -- Determine whether node N denotes a controlled function call
7601 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
7602 -- Determine whether node N denotes a generalized indexing form which
7603 -- involves a controlled result.
7605 function Is_Displace_Call (N : Node_Id) return Boolean;
7606 -- Determine whether node N denotes a call to Ada.Tags.Displace
7608 function Is_Source_Object (N : Node_Id) return Boolean;
7609 -- Determine whether a particular node denotes a source object
7611 function Strip (N : Node_Id) return Node_Id;
7612 -- Examine arbitrary node N by stripping various indirections and return
7613 -- the "real" node.
7615 ---------------------------------
7616 -- Is_Controlled_Function_Call --
7617 ---------------------------------
7619 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
7620 Expr : Node_Id;
7622 begin
7623 -- When a function call appears in Object.Operation format, the
7624 -- original representation has several possible forms depending on
7625 -- the availability and form of actual parameters:
7627 -- Obj.Func N_Selected_Component
7628 -- Obj.Func (Actual) N_Indexed_Component
7629 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
7630 -- N_Selected_Component
7632 Expr := Original_Node (N);
7633 loop
7634 if Nkind (Expr) = N_Function_Call then
7635 Expr := Name (Expr);
7637 -- "Obj.Func (Actual)" case
7639 elsif Nkind (Expr) = N_Indexed_Component then
7640 Expr := Prefix (Expr);
7642 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
7644 elsif Nkind (Expr) = N_Selected_Component then
7645 Expr := Selector_Name (Expr);
7647 else
7648 exit;
7649 end if;
7650 end loop;
7652 return
7653 Nkind (Expr) in N_Has_Entity
7654 and then Present (Entity (Expr))
7655 and then Ekind (Entity (Expr)) = E_Function
7656 and then Needs_Finalization (Etype (Entity (Expr)));
7657 end Is_Controlled_Function_Call;
7659 ----------------------------
7660 -- Is_Controlled_Indexing --
7661 ----------------------------
7663 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
7664 Expr : constant Node_Id := Original_Node (N);
7666 begin
7667 return
7668 Nkind (Expr) = N_Indexed_Component
7669 and then Present (Generalized_Indexing (Expr))
7670 and then Needs_Finalization (Etype (Expr));
7671 end Is_Controlled_Indexing;
7673 ----------------------
7674 -- Is_Displace_Call --
7675 ----------------------
7677 function Is_Displace_Call (N : Node_Id) return Boolean is
7678 Call : constant Node_Id := Strip (N);
7680 begin
7681 return
7682 Present (Call)
7683 and then Nkind (Call) = N_Function_Call
7684 and then Nkind (Name (Call)) in N_Has_Entity
7685 and then Is_RTE (Entity (Name (Call)), RE_Displace);
7686 end Is_Displace_Call;
7688 ----------------------
7689 -- Is_Source_Object --
7690 ----------------------
7692 function Is_Source_Object (N : Node_Id) return Boolean is
7693 Obj : constant Node_Id := Strip (N);
7695 begin
7696 return
7697 Present (Obj)
7698 and then Comes_From_Source (Obj)
7699 and then Nkind (Obj) in N_Has_Entity
7700 and then Is_Object (Entity (Obj));
7701 end Is_Source_Object;
7703 -----------
7704 -- Strip --
7705 -----------
7707 function Strip (N : Node_Id) return Node_Id is
7708 Result : Node_Id;
7710 begin
7711 Result := N;
7712 loop
7713 if Nkind (Result) = N_Explicit_Dereference then
7714 Result := Prefix (Result);
7716 elsif Nkind_In (Result, N_Type_Conversion,
7717 N_Unchecked_Type_Conversion)
7718 then
7719 Result := Expression (Result);
7721 else
7722 exit;
7723 end if;
7724 end loop;
7726 return Result;
7727 end Strip;
7729 -- Local variables
7731 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
7732 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7733 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
7734 Orig_Expr : Node_Id;
7736 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
7738 begin
7739 -- Case 1:
7741 -- Obj : CW_Type := Function_Call (...);
7743 -- is rewritten into:
7745 -- Temp : ... := Function_Call (...)'reference;
7746 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7748 -- where the return type of the function and the class-wide type require
7749 -- dispatch table pointer displacement.
7751 -- Case 2:
7753 -- Obj : CW_Type := Container (...);
7755 -- is rewritten into:
7757 -- Temp : ... := Function_Call (Container, ...)'reference;
7758 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
7760 -- where the container element type and the class-wide type require
7761 -- dispatch table pointer dispacement.
7763 -- Case 3:
7765 -- Obj : CW_Type := Src_Obj;
7767 -- is rewritten into:
7769 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
7771 -- where the type of the source object and the class-wide type require
7772 -- dispatch table pointer displacement.
7774 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
7775 and then Is_Class_Wide_Type (Obj_Typ)
7776 and then Is_Displace_Call (Renamed_Object (Obj_Id))
7777 and then Nkind (Orig_Decl) = N_Object_Declaration
7778 and then Comes_From_Source (Orig_Decl)
7779 then
7780 Orig_Expr := Expression (Orig_Decl);
7782 return
7783 Is_Controlled_Function_Call (Orig_Expr)
7784 or else Is_Controlled_Indexing (Orig_Expr)
7785 or else Is_Source_Object (Orig_Expr);
7786 end if;
7788 return False;
7789 end Is_Displacement_Of_Object_Or_Function_Result;
7791 ------------------------------
7792 -- Is_Finalizable_Transient --
7793 ------------------------------
7795 function Is_Finalizable_Transient
7796 (Decl : Node_Id;
7797 Rel_Node : Node_Id) return Boolean
7799 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
7800 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
7802 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
7803 -- Determine whether transient object Trans_Id is initialized either
7804 -- by a function call which returns an access type or simply renames
7805 -- another pointer.
7807 function Initialized_By_Aliased_BIP_Func_Call
7808 (Trans_Id : Entity_Id) return Boolean;
7809 -- Determine whether transient object Trans_Id is initialized by a
7810 -- build-in-place function call where the BIPalloc parameter is of
7811 -- value 1 and BIPaccess is not null. This case creates an aliasing
7812 -- between the returned value and the value denoted by BIPaccess.
7814 function Is_Aliased
7815 (Trans_Id : Entity_Id;
7816 First_Stmt : Node_Id) return Boolean;
7817 -- Determine whether transient object Trans_Id has been renamed or
7818 -- aliased through 'reference in the statement list starting from
7819 -- First_Stmt.
7821 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
7822 -- Determine whether transient object Trans_Id is allocated on the heap
7824 function Is_Iterated_Container
7825 (Trans_Id : Entity_Id;
7826 First_Stmt : Node_Id) return Boolean;
7827 -- Determine whether transient object Trans_Id denotes a container which
7828 -- is in the process of being iterated in the statement list starting
7829 -- from First_Stmt.
7831 ---------------------------
7832 -- Initialized_By_Access --
7833 ---------------------------
7835 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
7836 Expr : constant Node_Id := Expression (Parent (Trans_Id));
7838 begin
7839 return
7840 Present (Expr)
7841 and then Nkind (Expr) /= N_Reference
7842 and then Is_Access_Type (Etype (Expr));
7843 end Initialized_By_Access;
7845 ------------------------------------------
7846 -- Initialized_By_Aliased_BIP_Func_Call --
7847 ------------------------------------------
7849 function Initialized_By_Aliased_BIP_Func_Call
7850 (Trans_Id : Entity_Id) return Boolean
7852 Call : Node_Id := Expression (Parent (Trans_Id));
7854 begin
7855 -- Build-in-place calls usually appear in 'reference format
7857 if Nkind (Call) = N_Reference then
7858 Call := Prefix (Call);
7859 end if;
7861 if Is_Build_In_Place_Function_Call (Call) then
7862 declare
7863 Access_Nam : Name_Id := No_Name;
7864 Access_OK : Boolean := False;
7865 Actual : Node_Id;
7866 Alloc_Nam : Name_Id := No_Name;
7867 Alloc_OK : Boolean := False;
7868 Formal : Node_Id;
7869 Func_Id : Entity_Id;
7870 Param : Node_Id;
7872 begin
7873 -- Examine all parameter associations of the function call
7875 Param := First (Parameter_Associations (Call));
7876 while Present (Param) loop
7877 if Nkind (Param) = N_Parameter_Association
7878 and then Nkind (Selector_Name (Param)) = N_Identifier
7879 then
7880 Actual := Explicit_Actual_Parameter (Param);
7881 Formal := Selector_Name (Param);
7883 -- Construct the names of formals BIPaccess and BIPalloc
7884 -- using the function name retrieved from an arbitrary
7885 -- formal.
7887 if Access_Nam = No_Name
7888 and then Alloc_Nam = No_Name
7889 and then Present (Entity (Formal))
7890 then
7891 Func_Id := Scope (Entity (Formal));
7893 Access_Nam :=
7894 New_External_Name (Chars (Func_Id),
7895 BIP_Formal_Suffix (BIP_Object_Access));
7897 Alloc_Nam :=
7898 New_External_Name (Chars (Func_Id),
7899 BIP_Formal_Suffix (BIP_Alloc_Form));
7900 end if;
7902 -- A match for BIPaccess => Temp has been found
7904 if Chars (Formal) = Access_Nam
7905 and then Nkind (Actual) /= N_Null
7906 then
7907 Access_OK := True;
7908 end if;
7910 -- A match for BIPalloc => 1 has been found
7912 if Chars (Formal) = Alloc_Nam
7913 and then Nkind (Actual) = N_Integer_Literal
7914 and then Intval (Actual) = Uint_1
7915 then
7916 Alloc_OK := True;
7917 end if;
7918 end if;
7920 Next (Param);
7921 end loop;
7923 return Access_OK and Alloc_OK;
7924 end;
7925 end if;
7927 return False;
7928 end Initialized_By_Aliased_BIP_Func_Call;
7930 ----------------
7931 -- Is_Aliased --
7932 ----------------
7934 function Is_Aliased
7935 (Trans_Id : Entity_Id;
7936 First_Stmt : Node_Id) return Boolean
7938 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
7939 -- Given an object renaming declaration, retrieve the entity of the
7940 -- renamed name. Return Empty if the renamed name is anything other
7941 -- than a variable or a constant.
7943 -------------------------
7944 -- Find_Renamed_Object --
7945 -------------------------
7947 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
7948 Ren_Obj : Node_Id := Empty;
7950 function Find_Object (N : Node_Id) return Traverse_Result;
7951 -- Try to detect an object which is either a constant or a
7952 -- variable.
7954 -----------------
7955 -- Find_Object --
7956 -----------------
7958 function Find_Object (N : Node_Id) return Traverse_Result is
7959 begin
7960 -- Stop the search once a constant or a variable has been
7961 -- detected.
7963 if Nkind (N) = N_Identifier
7964 and then Present (Entity (N))
7965 and then Ekind_In (Entity (N), E_Constant, E_Variable)
7966 then
7967 Ren_Obj := Entity (N);
7968 return Abandon;
7969 end if;
7971 return OK;
7972 end Find_Object;
7974 procedure Search is new Traverse_Proc (Find_Object);
7976 -- Local variables
7978 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
7980 -- Start of processing for Find_Renamed_Object
7982 begin
7983 -- Actions related to dispatching calls may appear as renamings of
7984 -- tags. Do not process this type of renaming because it does not
7985 -- use the actual value of the object.
7987 if not Is_RTE (Typ, RE_Tag_Ptr) then
7988 Search (Name (Ren_Decl));
7989 end if;
7991 return Ren_Obj;
7992 end Find_Renamed_Object;
7994 -- Local variables
7996 Expr : Node_Id;
7997 Ren_Obj : Entity_Id;
7998 Stmt : Node_Id;
8000 -- Start of processing for Is_Aliased
8002 begin
8003 -- A controlled transient object is not considered aliased when it
8004 -- appears inside an expression_with_actions node even when there are
8005 -- explicit aliases of it:
8007 -- do
8008 -- Trans_Id : Ctrl_Typ ...; -- transient object
8009 -- Alias : ... := Trans_Id; -- object is aliased
8010 -- Val : constant Boolean :=
8011 -- ... Alias ...; -- aliasing ends
8012 -- <finalize Trans_Id> -- object safe to finalize
8013 -- in Val end;
8015 -- Expansion ensures that all aliases are encapsulated in the actions
8016 -- list and do not leak to the expression by forcing the evaluation
8017 -- of the expression.
8019 if Nkind (Rel_Node) = N_Expression_With_Actions then
8020 return False;
8022 -- Otherwise examine the statements after the controlled transient
8023 -- object and look for various forms of aliasing.
8025 else
8026 Stmt := First_Stmt;
8027 while Present (Stmt) loop
8028 if Nkind (Stmt) = N_Object_Declaration then
8029 Expr := Expression (Stmt);
8031 -- Aliasing of the form:
8032 -- Obj : ... := Trans_Id'reference;
8034 if Present (Expr)
8035 and then Nkind (Expr) = N_Reference
8036 and then Nkind (Prefix (Expr)) = N_Identifier
8037 and then Entity (Prefix (Expr)) = Trans_Id
8038 then
8039 return True;
8040 end if;
8042 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8043 Ren_Obj := Find_Renamed_Object (Stmt);
8045 -- Aliasing of the form:
8046 -- Obj : ... renames ... Trans_Id ...;
8048 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8049 return True;
8050 end if;
8051 end if;
8053 Next (Stmt);
8054 end loop;
8056 return False;
8057 end if;
8058 end Is_Aliased;
8060 ------------------
8061 -- Is_Allocated --
8062 ------------------
8064 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8065 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8066 begin
8067 return
8068 Is_Access_Type (Etype (Trans_Id))
8069 and then Present (Expr)
8070 and then Nkind (Expr) = N_Allocator;
8071 end Is_Allocated;
8073 ---------------------------
8074 -- Is_Iterated_Container --
8075 ---------------------------
8077 function Is_Iterated_Container
8078 (Trans_Id : Entity_Id;
8079 First_Stmt : Node_Id) return Boolean
8081 Aspect : Node_Id;
8082 Call : Node_Id;
8083 Iter : Entity_Id;
8084 Param : Node_Id;
8085 Stmt : Node_Id;
8086 Typ : Entity_Id;
8088 begin
8089 -- It is not possible to iterate over containers in non-Ada 2012 code
8091 if Ada_Version < Ada_2012 then
8092 return False;
8093 end if;
8095 Typ := Etype (Trans_Id);
8097 -- Handle access type created for secondary stack use
8099 if Is_Access_Type (Typ) then
8100 Typ := Designated_Type (Typ);
8101 end if;
8103 -- Look for aspect Default_Iterator. It may be part of a type
8104 -- declaration for a container, or inherited from a base type
8105 -- or parent type.
8107 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8109 if Present (Aspect) then
8110 Iter := Entity (Aspect);
8112 -- Examine the statements following the container object and
8113 -- look for a call to the default iterate routine where the
8114 -- first parameter is the transient. Such a call appears as:
8116 -- It : Access_To_CW_Iterator :=
8117 -- Iterate (Tran_Id.all, ...)'reference;
8119 Stmt := First_Stmt;
8120 while Present (Stmt) loop
8122 -- Detect an object declaration which is initialized by a
8123 -- secondary stack function call.
8125 if Nkind (Stmt) = N_Object_Declaration
8126 and then Present (Expression (Stmt))
8127 and then Nkind (Expression (Stmt)) = N_Reference
8128 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8129 then
8130 Call := Prefix (Expression (Stmt));
8132 -- The call must invoke the default iterate routine of
8133 -- the container and the transient object must appear as
8134 -- the first actual parameter. Skip any calls whose names
8135 -- are not entities.
8137 if Is_Entity_Name (Name (Call))
8138 and then Entity (Name (Call)) = Iter
8139 and then Present (Parameter_Associations (Call))
8140 then
8141 Param := First (Parameter_Associations (Call));
8143 if Nkind (Param) = N_Explicit_Dereference
8144 and then Entity (Prefix (Param)) = Trans_Id
8145 then
8146 return True;
8147 end if;
8148 end if;
8149 end if;
8151 Next (Stmt);
8152 end loop;
8153 end if;
8155 return False;
8156 end Is_Iterated_Container;
8158 -- Local variables
8160 Desig : Entity_Id := Obj_Typ;
8162 -- Start of processing for Is_Finalizable_Transient
8164 begin
8165 -- Handle access types
8167 if Is_Access_Type (Desig) then
8168 Desig := Available_View (Designated_Type (Desig));
8169 end if;
8171 return
8172 Ekind_In (Obj_Id, E_Constant, E_Variable)
8173 and then Needs_Finalization (Desig)
8174 and then Requires_Transient_Scope (Desig)
8175 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8177 -- Do not consider a transient object that was already processed
8179 and then not Is_Finalized_Transient (Obj_Id)
8181 -- Do not consider renamed or 'reference-d transient objects because
8182 -- the act of renaming extends the object's lifetime.
8184 and then not Is_Aliased (Obj_Id, Decl)
8186 -- Do not consider transient objects allocated on the heap since
8187 -- they are attached to a finalization master.
8189 and then not Is_Allocated (Obj_Id)
8191 -- If the transient object is a pointer, check that it is not
8192 -- initialized by a function that returns a pointer or acts as a
8193 -- renaming of another pointer.
8195 and then
8196 (not Is_Access_Type (Obj_Typ)
8197 or else not Initialized_By_Access (Obj_Id))
8199 -- Do not consider transient objects which act as indirect aliases
8200 -- of build-in-place function results.
8202 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8204 -- Do not consider conversions of tags to class-wide types
8206 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8208 -- Do not consider iterators because those are treated as normal
8209 -- controlled objects and are processed by the usual finalization
8210 -- machinery. This avoids the double finalization of an iterator.
8212 and then not Is_Iterator (Desig)
8214 -- Do not consider containers in the context of iterator loops. Such
8215 -- transient objects must exist for as long as the loop is around,
8216 -- otherwise any operation carried out by the iterator will fail.
8218 and then not Is_Iterated_Container (Obj_Id, Decl);
8219 end Is_Finalizable_Transient;
8221 ---------------------------------
8222 -- Is_Fully_Repped_Tagged_Type --
8223 ---------------------------------
8225 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8226 U : constant Entity_Id := Underlying_Type (T);
8227 Comp : Entity_Id;
8229 begin
8230 if No (U) or else not Is_Tagged_Type (U) then
8231 return False;
8232 elsif Has_Discriminants (U) then
8233 return False;
8234 elsif not Has_Specified_Layout (U) then
8235 return False;
8236 end if;
8238 -- Here we have a tagged type, see if it has any unlayed out fields
8239 -- other than a possible tag and parent fields. If so, we return False.
8241 Comp := First_Component (U);
8242 while Present (Comp) loop
8243 if not Is_Tag (Comp)
8244 and then Chars (Comp) /= Name_uParent
8245 and then No (Component_Clause (Comp))
8246 then
8247 return False;
8248 else
8249 Next_Component (Comp);
8250 end if;
8251 end loop;
8253 -- All components are layed out
8255 return True;
8256 end Is_Fully_Repped_Tagged_Type;
8258 ----------------------------------
8259 -- Is_Library_Level_Tagged_Type --
8260 ----------------------------------
8262 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8263 begin
8264 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8265 end Is_Library_Level_Tagged_Type;
8267 --------------------------
8268 -- Is_Non_BIP_Func_Call --
8269 --------------------------
8271 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8272 begin
8273 -- The expected call is of the format
8275 -- Func_Call'reference
8277 return
8278 Nkind (Expr) = N_Reference
8279 and then Nkind (Prefix (Expr)) = N_Function_Call
8280 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8281 end Is_Non_BIP_Func_Call;
8283 ----------------------------------
8284 -- Is_Possibly_Unaligned_Object --
8285 ----------------------------------
8287 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8288 T : constant Entity_Id := Etype (N);
8290 begin
8291 -- If renamed object, apply test to underlying object
8293 if Is_Entity_Name (N)
8294 and then Is_Object (Entity (N))
8295 and then Present (Renamed_Object (Entity (N)))
8296 then
8297 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8298 end if;
8300 -- Tagged and controlled types and aliased types are always aligned, as
8301 -- are concurrent types.
8303 if Is_Aliased (T)
8304 or else Has_Controlled_Component (T)
8305 or else Is_Concurrent_Type (T)
8306 or else Is_Tagged_Type (T)
8307 or else Is_Controlled (T)
8308 then
8309 return False;
8310 end if;
8312 -- If this is an element of a packed array, may be unaligned
8314 if Is_Ref_To_Bit_Packed_Array (N) then
8315 return True;
8316 end if;
8318 -- Case of indexed component reference: test whether prefix is unaligned
8320 if Nkind (N) = N_Indexed_Component then
8321 return Is_Possibly_Unaligned_Object (Prefix (N));
8323 -- Case of selected component reference
8325 elsif Nkind (N) = N_Selected_Component then
8326 declare
8327 P : constant Node_Id := Prefix (N);
8328 C : constant Entity_Id := Entity (Selector_Name (N));
8329 M : Nat;
8330 S : Nat;
8332 begin
8333 -- If component reference is for an array with non-static bounds,
8334 -- then it is always aligned: we can only process unaligned arrays
8335 -- with static bounds (more precisely compile time known bounds).
8337 if Is_Array_Type (T)
8338 and then not Compile_Time_Known_Bounds (T)
8339 then
8340 return False;
8341 end if;
8343 -- If component is aliased, it is definitely properly aligned
8345 if Is_Aliased (C) then
8346 return False;
8347 end if;
8349 -- If component is for a type implemented as a scalar, and the
8350 -- record is packed, and the component is other than the first
8351 -- component of the record, then the component may be unaligned.
8353 if Is_Packed (Etype (P))
8354 and then Represented_As_Scalar (Etype (C))
8355 and then First_Entity (Scope (C)) /= C
8356 then
8357 return True;
8358 end if;
8360 -- Compute maximum possible alignment for T
8362 -- If alignment is known, then that settles things
8364 if Known_Alignment (T) then
8365 M := UI_To_Int (Alignment (T));
8367 -- If alignment is not known, tentatively set max alignment
8369 else
8370 M := Ttypes.Maximum_Alignment;
8372 -- We can reduce this if the Esize is known since the default
8373 -- alignment will never be more than the smallest power of 2
8374 -- that does not exceed this Esize value.
8376 if Known_Esize (T) then
8377 S := UI_To_Int (Esize (T));
8379 while (M / 2) >= S loop
8380 M := M / 2;
8381 end loop;
8382 end if;
8383 end if;
8385 -- The following code is historical, it used to be present but it
8386 -- is too cautious, because the front-end does not know the proper
8387 -- default alignments for the target. Also, if the alignment is
8388 -- not known, the front end can't know in any case. If a copy is
8389 -- needed, the back-end will take care of it. This whole section
8390 -- including this comment can be removed later ???
8392 -- If the component reference is for a record that has a specified
8393 -- alignment, and we either know it is too small, or cannot tell,
8394 -- then the component may be unaligned.
8396 -- What is the following commented out code ???
8398 -- if Known_Alignment (Etype (P))
8399 -- and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
8400 -- and then M > Alignment (Etype (P))
8401 -- then
8402 -- return True;
8403 -- end if;
8405 -- Case of component clause present which may specify an
8406 -- unaligned position.
8408 if Present (Component_Clause (C)) then
8410 -- Otherwise we can do a test to make sure that the actual
8411 -- start position in the record, and the length, are both
8412 -- consistent with the required alignment. If not, we know
8413 -- that we are unaligned.
8415 declare
8416 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8417 begin
8418 if Component_Bit_Offset (C) mod Align_In_Bits /= 0
8419 or else Esize (C) mod Align_In_Bits /= 0
8420 then
8421 return True;
8422 end if;
8423 end;
8424 end if;
8426 -- Otherwise, for a component reference, test prefix
8428 return Is_Possibly_Unaligned_Object (P);
8429 end;
8431 -- If not a component reference, must be aligned
8433 else
8434 return False;
8435 end if;
8436 end Is_Possibly_Unaligned_Object;
8438 ---------------------------------
8439 -- Is_Possibly_Unaligned_Slice --
8440 ---------------------------------
8442 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8443 begin
8444 -- Go to renamed object
8446 if Is_Entity_Name (N)
8447 and then Is_Object (Entity (N))
8448 and then Present (Renamed_Object (Entity (N)))
8449 then
8450 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8451 end if;
8453 -- The reference must be a slice
8455 if Nkind (N) /= N_Slice then
8456 return False;
8457 end if;
8459 -- We only need to worry if the target has strict alignment
8461 if not Target_Strict_Alignment then
8462 return False;
8463 end if;
8465 -- If it is a slice, then look at the array type being sliced
8467 declare
8468 Sarr : constant Node_Id := Prefix (N);
8469 -- Prefix of the slice, i.e. the array being sliced
8471 Styp : constant Entity_Id := Etype (Prefix (N));
8472 -- Type of the array being sliced
8474 Pref : Node_Id;
8475 Ptyp : Entity_Id;
8477 begin
8478 -- The problems arise if the array object that is being sliced
8479 -- is a component of a record or array, and we cannot guarantee
8480 -- the alignment of the array within its containing object.
8482 -- To investigate this, we look at successive prefixes to see
8483 -- if we have a worrisome indexed or selected component.
8485 Pref := Sarr;
8486 loop
8487 -- Case of array is part of an indexed component reference
8489 if Nkind (Pref) = N_Indexed_Component then
8490 Ptyp := Etype (Prefix (Pref));
8492 -- The only problematic case is when the array is packed, in
8493 -- which case we really know nothing about the alignment of
8494 -- individual components.
8496 if Is_Bit_Packed_Array (Ptyp) then
8497 return True;
8498 end if;
8500 -- Case of array is part of a selected component reference
8502 elsif Nkind (Pref) = N_Selected_Component then
8503 Ptyp := Etype (Prefix (Pref));
8505 -- We are definitely in trouble if the record in question
8506 -- has an alignment, and either we know this alignment is
8507 -- inconsistent with the alignment of the slice, or we don't
8508 -- know what the alignment of the slice should be.
8510 if Known_Alignment (Ptyp)
8511 and then (Unknown_Alignment (Styp)
8512 or else Alignment (Styp) > Alignment (Ptyp))
8513 then
8514 return True;
8515 end if;
8517 -- We are in potential trouble if the record type is packed.
8518 -- We could special case when we know that the array is the
8519 -- first component, but that's not such a simple case ???
8521 if Is_Packed (Ptyp) then
8522 return True;
8523 end if;
8525 -- We are in trouble if there is a component clause, and
8526 -- either we do not know the alignment of the slice, or
8527 -- the alignment of the slice is inconsistent with the
8528 -- bit position specified by the component clause.
8530 declare
8531 Field : constant Entity_Id := Entity (Selector_Name (Pref));
8532 begin
8533 if Present (Component_Clause (Field))
8534 and then
8535 (Unknown_Alignment (Styp)
8536 or else
8537 (Component_Bit_Offset (Field) mod
8538 (System_Storage_Unit * Alignment (Styp))) /= 0)
8539 then
8540 return True;
8541 end if;
8542 end;
8544 -- For cases other than selected or indexed components we know we
8545 -- are OK, since no issues arise over alignment.
8547 else
8548 return False;
8549 end if;
8551 -- We processed an indexed component or selected component
8552 -- reference that looked safe, so keep checking prefixes.
8554 Pref := Prefix (Pref);
8555 end loop;
8556 end;
8557 end Is_Possibly_Unaligned_Slice;
8559 -------------------------------
8560 -- Is_Related_To_Func_Return --
8561 -------------------------------
8563 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
8564 Expr : constant Node_Id := Related_Expression (Id);
8565 begin
8566 return
8567 Present (Expr)
8568 and then Nkind (Expr) = N_Explicit_Dereference
8569 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
8570 end Is_Related_To_Func_Return;
8572 --------------------------------
8573 -- Is_Ref_To_Bit_Packed_Array --
8574 --------------------------------
8576 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
8577 Result : Boolean;
8578 Expr : Node_Id;
8580 begin
8581 if Is_Entity_Name (N)
8582 and then Is_Object (Entity (N))
8583 and then Present (Renamed_Object (Entity (N)))
8584 then
8585 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
8586 end if;
8588 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8589 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
8590 Result := True;
8591 else
8592 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
8593 end if;
8595 if Result and then Nkind (N) = N_Indexed_Component then
8596 Expr := First (Expressions (N));
8597 while Present (Expr) loop
8598 Force_Evaluation (Expr);
8599 Next (Expr);
8600 end loop;
8601 end if;
8603 return Result;
8605 else
8606 return False;
8607 end if;
8608 end Is_Ref_To_Bit_Packed_Array;
8610 --------------------------------
8611 -- Is_Ref_To_Bit_Packed_Slice --
8612 --------------------------------
8614 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
8615 begin
8616 if Nkind (N) = N_Type_Conversion then
8617 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
8619 elsif Is_Entity_Name (N)
8620 and then Is_Object (Entity (N))
8621 and then Present (Renamed_Object (Entity (N)))
8622 then
8623 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
8625 elsif Nkind (N) = N_Slice
8626 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
8627 then
8628 return True;
8630 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8631 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
8633 else
8634 return False;
8635 end if;
8636 end Is_Ref_To_Bit_Packed_Slice;
8638 -----------------------
8639 -- Is_Renamed_Object --
8640 -----------------------
8642 function Is_Renamed_Object (N : Node_Id) return Boolean is
8643 Pnod : constant Node_Id := Parent (N);
8644 Kind : constant Node_Kind := Nkind (Pnod);
8645 begin
8646 if Kind = N_Object_Renaming_Declaration then
8647 return True;
8648 elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
8649 return Is_Renamed_Object (Pnod);
8650 else
8651 return False;
8652 end if;
8653 end Is_Renamed_Object;
8655 --------------------------------------
8656 -- Is_Secondary_Stack_BIP_Func_Call --
8657 --------------------------------------
8659 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
8660 Alloc_Nam : Name_Id := No_Name;
8661 Actual : Node_Id;
8662 Call : Node_Id := Expr;
8663 Formal : Node_Id;
8664 Param : Node_Id;
8666 begin
8667 -- Build-in-place calls usually appear in 'reference format. Note that
8668 -- the accessibility check machinery may add an extra 'reference due to
8669 -- side effect removal.
8671 while Nkind (Call) = N_Reference loop
8672 Call := Prefix (Call);
8673 end loop;
8675 Call := Unqual_Conv (Call);
8677 if Is_Build_In_Place_Function_Call (Call) then
8679 -- Examine all parameter associations of the function call
8681 Param := First (Parameter_Associations (Call));
8682 while Present (Param) loop
8683 if Nkind (Param) = N_Parameter_Association
8684 and then Nkind (Selector_Name (Param)) = N_Identifier
8685 then
8686 Formal := Selector_Name (Param);
8687 Actual := Explicit_Actual_Parameter (Param);
8689 -- Construct the name of formal BIPalloc. It is much easier to
8690 -- extract the name of the function using an arbitrary formal's
8691 -- scope rather than the Name field of Call.
8693 if Alloc_Nam = No_Name and then Present (Entity (Formal)) then
8694 Alloc_Nam :=
8695 New_External_Name
8696 (Chars (Scope (Entity (Formal))),
8697 BIP_Formal_Suffix (BIP_Alloc_Form));
8698 end if;
8700 -- A match for BIPalloc => 2 has been found
8702 if Chars (Formal) = Alloc_Nam
8703 and then Nkind (Actual) = N_Integer_Literal
8704 and then Intval (Actual) = Uint_2
8705 then
8706 return True;
8707 end if;
8708 end if;
8710 Next (Param);
8711 end loop;
8712 end if;
8714 return False;
8715 end Is_Secondary_Stack_BIP_Func_Call;
8717 -------------------------------------
8718 -- Is_Tag_To_Class_Wide_Conversion --
8719 -------------------------------------
8721 function Is_Tag_To_Class_Wide_Conversion
8722 (Obj_Id : Entity_Id) return Boolean
8724 Expr : constant Node_Id := Expression (Parent (Obj_Id));
8726 begin
8727 return
8728 Is_Class_Wide_Type (Etype (Obj_Id))
8729 and then Present (Expr)
8730 and then Nkind (Expr) = N_Unchecked_Type_Conversion
8731 and then Etype (Expression (Expr)) = RTE (RE_Tag);
8732 end Is_Tag_To_Class_Wide_Conversion;
8734 ----------------------------
8735 -- Is_Untagged_Derivation --
8736 ----------------------------
8738 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
8739 begin
8740 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
8741 or else
8742 (Is_Private_Type (T) and then Present (Full_View (T))
8743 and then not Is_Tagged_Type (Full_View (T))
8744 and then Is_Derived_Type (Full_View (T))
8745 and then Etype (Full_View (T)) /= T);
8746 end Is_Untagged_Derivation;
8748 ------------------------------------
8749 -- Is_Untagged_Private_Derivation --
8750 ------------------------------------
8752 function Is_Untagged_Private_Derivation
8753 (Priv_Typ : Entity_Id;
8754 Full_Typ : Entity_Id) return Boolean
8756 begin
8757 return
8758 Present (Priv_Typ)
8759 and then Is_Untagged_Derivation (Priv_Typ)
8760 and then Is_Private_Type (Etype (Priv_Typ))
8761 and then Present (Full_Typ)
8762 and then Is_Itype (Full_Typ);
8763 end Is_Untagged_Private_Derivation;
8765 ---------------------------
8766 -- Is_Volatile_Reference --
8767 ---------------------------
8769 function Is_Volatile_Reference (N : Node_Id) return Boolean is
8770 begin
8771 -- Only source references are to be treated as volatile, internally
8772 -- generated stuff cannot have volatile external effects.
8774 if not Comes_From_Source (N) then
8775 return False;
8777 -- Never true for reference to a type
8779 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8780 return False;
8782 -- Never true for a compile time known constant
8784 elsif Compile_Time_Known_Value (N) then
8785 return False;
8787 -- True if object reference with volatile type
8789 elsif Is_Volatile_Object (N) then
8790 return True;
8792 -- True if reference to volatile entity
8794 elsif Is_Entity_Name (N) then
8795 return Treat_As_Volatile (Entity (N));
8797 -- True for slice of volatile array
8799 elsif Nkind (N) = N_Slice then
8800 return Is_Volatile_Reference (Prefix (N));
8802 -- True if volatile component
8804 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
8805 if (Is_Entity_Name (Prefix (N))
8806 and then Has_Volatile_Components (Entity (Prefix (N))))
8807 or else (Present (Etype (Prefix (N)))
8808 and then Has_Volatile_Components (Etype (Prefix (N))))
8809 then
8810 return True;
8811 else
8812 return Is_Volatile_Reference (Prefix (N));
8813 end if;
8815 -- Otherwise false
8817 else
8818 return False;
8819 end if;
8820 end Is_Volatile_Reference;
8822 --------------------
8823 -- Kill_Dead_Code --
8824 --------------------
8826 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
8827 W : Boolean := Warn;
8828 -- Set False if warnings suppressed
8830 begin
8831 if Present (N) then
8832 Remove_Warning_Messages (N);
8834 -- Generate warning if appropriate
8836 if W then
8838 -- We suppress the warning if this code is under control of an
8839 -- if statement, whose condition is a simple identifier, and
8840 -- either we are in an instance, or warnings off is set for this
8841 -- identifier. The reason for killing it in the instance case is
8842 -- that it is common and reasonable for code to be deleted in
8843 -- instances for various reasons.
8845 -- Could we use Is_Statically_Unevaluated here???
8847 if Nkind (Parent (N)) = N_If_Statement then
8848 declare
8849 C : constant Node_Id := Condition (Parent (N));
8850 begin
8851 if Nkind (C) = N_Identifier
8852 and then
8853 (In_Instance
8854 or else (Present (Entity (C))
8855 and then Has_Warnings_Off (Entity (C))))
8856 then
8857 W := False;
8858 end if;
8859 end;
8860 end if;
8862 -- Generate warning if not suppressed
8864 if W then
8865 Error_Msg_F
8866 ("?t?this code can never be executed and has been deleted!",
8868 end if;
8869 end if;
8871 -- Recurse into block statements and bodies to process declarations
8872 -- and statements.
8874 if Nkind (N) = N_Block_Statement
8875 or else Nkind (N) = N_Subprogram_Body
8876 or else Nkind (N) = N_Package_Body
8877 then
8878 Kill_Dead_Code (Declarations (N), False);
8879 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
8881 if Nkind (N) = N_Subprogram_Body then
8882 Set_Is_Eliminated (Defining_Entity (N));
8883 end if;
8885 elsif Nkind (N) = N_Package_Declaration then
8886 Kill_Dead_Code (Visible_Declarations (Specification (N)));
8887 Kill_Dead_Code (Private_Declarations (Specification (N)));
8889 -- ??? After this point, Delete_Tree has been called on all
8890 -- declarations in Specification (N), so references to entities
8891 -- therein look suspicious.
8893 declare
8894 E : Entity_Id := First_Entity (Defining_Entity (N));
8896 begin
8897 while Present (E) loop
8898 if Ekind (E) = E_Operator then
8899 Set_Is_Eliminated (E);
8900 end if;
8902 Next_Entity (E);
8903 end loop;
8904 end;
8906 -- Recurse into composite statement to kill individual statements in
8907 -- particular instantiations.
8909 elsif Nkind (N) = N_If_Statement then
8910 Kill_Dead_Code (Then_Statements (N));
8911 Kill_Dead_Code (Elsif_Parts (N));
8912 Kill_Dead_Code (Else_Statements (N));
8914 elsif Nkind (N) = N_Loop_Statement then
8915 Kill_Dead_Code (Statements (N));
8917 elsif Nkind (N) = N_Case_Statement then
8918 declare
8919 Alt : Node_Id;
8920 begin
8921 Alt := First (Alternatives (N));
8922 while Present (Alt) loop
8923 Kill_Dead_Code (Statements (Alt));
8924 Next (Alt);
8925 end loop;
8926 end;
8928 elsif Nkind (N) = N_Case_Statement_Alternative then
8929 Kill_Dead_Code (Statements (N));
8931 -- Deal with dead instances caused by deleting instantiations
8933 elsif Nkind (N) in N_Generic_Instantiation then
8934 Remove_Dead_Instance (N);
8935 end if;
8936 end if;
8937 end Kill_Dead_Code;
8939 -- Case where argument is a list of nodes to be killed
8941 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
8942 N : Node_Id;
8943 W : Boolean;
8945 begin
8946 W := Warn;
8948 if Is_Non_Empty_List (L) then
8949 N := First (L);
8950 while Present (N) loop
8951 Kill_Dead_Code (N, W);
8952 W := False;
8953 Next (N);
8954 end loop;
8955 end if;
8956 end Kill_Dead_Code;
8958 ------------------------
8959 -- Known_Non_Negative --
8960 ------------------------
8962 function Known_Non_Negative (Opnd : Node_Id) return Boolean is
8963 begin
8964 if Is_OK_Static_Expression (Opnd) and then Expr_Value (Opnd) >= 0 then
8965 return True;
8967 else
8968 declare
8969 Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
8970 begin
8971 return
8972 Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
8973 end;
8974 end if;
8975 end Known_Non_Negative;
8977 -----------------------------
8978 -- Make_CW_Equivalent_Type --
8979 -----------------------------
8981 -- Create a record type used as an equivalent of any member of the class
8982 -- which takes its size from exp.
8984 -- Generate the following code:
8986 -- type Equiv_T is record
8987 -- _parent : T (List of discriminant constraints taken from Exp);
8988 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
8989 -- end Equiv_T;
8991 -- ??? Note that this type does not guarantee same alignment as all
8992 -- derived types
8994 function Make_CW_Equivalent_Type
8995 (T : Entity_Id;
8996 E : Node_Id) return Entity_Id
8998 Loc : constant Source_Ptr := Sloc (E);
8999 Root_Typ : constant Entity_Id := Root_Type (T);
9000 List_Def : constant List_Id := Empty_List;
9001 Comp_List : constant List_Id := New_List;
9002 Equiv_Type : Entity_Id;
9003 Range_Type : Entity_Id;
9004 Str_Type : Entity_Id;
9005 Constr_Root : Entity_Id;
9006 Sizexpr : Node_Id;
9008 begin
9009 -- If the root type is already constrained, there are no discriminants
9010 -- in the expression.
9012 if not Has_Discriminants (Root_Typ)
9013 or else Is_Constrained (Root_Typ)
9014 then
9015 Constr_Root := Root_Typ;
9017 -- At this point in the expansion, non-limited view of the type
9018 -- must be available, otherwise the error will be reported later.
9020 if From_Limited_With (Constr_Root)
9021 and then Present (Non_Limited_View (Constr_Root))
9022 then
9023 Constr_Root := Non_Limited_View (Constr_Root);
9024 end if;
9026 else
9027 Constr_Root := Make_Temporary (Loc, 'R');
9029 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9031 Append_To (List_Def,
9032 Make_Subtype_Declaration (Loc,
9033 Defining_Identifier => Constr_Root,
9034 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9035 end if;
9037 -- Generate the range subtype declaration
9039 Range_Type := Make_Temporary (Loc, 'G');
9041 if not Is_Interface (Root_Typ) then
9043 -- subtype rg__xx is
9044 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9046 Sizexpr :=
9047 Make_Op_Subtract (Loc,
9048 Left_Opnd =>
9049 Make_Attribute_Reference (Loc,
9050 Prefix =>
9051 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9052 Attribute_Name => Name_Size),
9053 Right_Opnd =>
9054 Make_Attribute_Reference (Loc,
9055 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9056 Attribute_Name => Name_Object_Size));
9057 else
9058 -- subtype rg__xx is
9059 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9061 Sizexpr :=
9062 Make_Attribute_Reference (Loc,
9063 Prefix =>
9064 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9065 Attribute_Name => Name_Size);
9066 end if;
9068 Set_Paren_Count (Sizexpr, 1);
9070 Append_To (List_Def,
9071 Make_Subtype_Declaration (Loc,
9072 Defining_Identifier => Range_Type,
9073 Subtype_Indication =>
9074 Make_Subtype_Indication (Loc,
9075 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9076 Constraint => Make_Range_Constraint (Loc,
9077 Range_Expression =>
9078 Make_Range (Loc,
9079 Low_Bound => Make_Integer_Literal (Loc, 1),
9080 High_Bound =>
9081 Make_Op_Divide (Loc,
9082 Left_Opnd => Sizexpr,
9083 Right_Opnd => Make_Integer_Literal (Loc,
9084 Intval => System_Storage_Unit)))))));
9086 -- subtype str__nn is Storage_Array (rg__x);
9088 Str_Type := Make_Temporary (Loc, 'S');
9089 Append_To (List_Def,
9090 Make_Subtype_Declaration (Loc,
9091 Defining_Identifier => Str_Type,
9092 Subtype_Indication =>
9093 Make_Subtype_Indication (Loc,
9094 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9095 Constraint =>
9096 Make_Index_Or_Discriminant_Constraint (Loc,
9097 Constraints =>
9098 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9100 -- type Equiv_T is record
9101 -- [ _parent : Tnn; ]
9102 -- E : Str_Type;
9103 -- end Equiv_T;
9105 Equiv_Type := Make_Temporary (Loc, 'T');
9106 Set_Ekind (Equiv_Type, E_Record_Type);
9107 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9109 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9110 -- treatment for this type. In particular, even though _parent's type
9111 -- is a controlled type or contains controlled components, we do not
9112 -- want to set Has_Controlled_Component on it to avoid making it gain
9113 -- an unwanted _controller component.
9115 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9117 -- A class-wide equivalent type does not require initialization
9119 Set_Suppress_Initialization (Equiv_Type);
9121 if not Is_Interface (Root_Typ) then
9122 Append_To (Comp_List,
9123 Make_Component_Declaration (Loc,
9124 Defining_Identifier =>
9125 Make_Defining_Identifier (Loc, Name_uParent),
9126 Component_Definition =>
9127 Make_Component_Definition (Loc,
9128 Aliased_Present => False,
9129 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9130 end if;
9132 Append_To (Comp_List,
9133 Make_Component_Declaration (Loc,
9134 Defining_Identifier => Make_Temporary (Loc, 'C'),
9135 Component_Definition =>
9136 Make_Component_Definition (Loc,
9137 Aliased_Present => False,
9138 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9140 Append_To (List_Def,
9141 Make_Full_Type_Declaration (Loc,
9142 Defining_Identifier => Equiv_Type,
9143 Type_Definition =>
9144 Make_Record_Definition (Loc,
9145 Component_List =>
9146 Make_Component_List (Loc,
9147 Component_Items => Comp_List,
9148 Variant_Part => Empty))));
9150 -- Suppress all checks during the analysis of the expanded code to avoid
9151 -- the generation of spurious warnings under ZFP run-time.
9153 Insert_Actions (E, List_Def, Suppress => All_Checks);
9154 return Equiv_Type;
9155 end Make_CW_Equivalent_Type;
9157 -------------------------
9158 -- Make_Invariant_Call --
9159 -------------------------
9161 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9162 Loc : constant Source_Ptr := Sloc (Expr);
9163 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9165 Proc_Id : Entity_Id;
9167 begin
9168 pragma Assert (Has_Invariants (Typ));
9170 Proc_Id := Invariant_Procedure (Typ);
9171 pragma Assert (Present (Proc_Id));
9173 return
9174 Make_Procedure_Call_Statement (Loc,
9175 Name => New_Occurrence_Of (Proc_Id, Loc),
9176 Parameter_Associations => New_List (Relocate_Node (Expr)));
9177 end Make_Invariant_Call;
9179 ------------------------
9180 -- Make_Literal_Range --
9181 ------------------------
9183 function Make_Literal_Range
9184 (Loc : Source_Ptr;
9185 Literal_Typ : Entity_Id) return Node_Id
9187 Lo : constant Node_Id :=
9188 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9189 Index : constant Entity_Id := Etype (Lo);
9191 Hi : Node_Id;
9192 Length_Expr : constant Node_Id :=
9193 Make_Op_Subtract (Loc,
9194 Left_Opnd =>
9195 Make_Integer_Literal (Loc,
9196 Intval => String_Literal_Length (Literal_Typ)),
9197 Right_Opnd =>
9198 Make_Integer_Literal (Loc, 1));
9200 begin
9201 Set_Analyzed (Lo, False);
9203 if Is_Integer_Type (Index) then
9204 Hi :=
9205 Make_Op_Add (Loc,
9206 Left_Opnd => New_Copy_Tree (Lo),
9207 Right_Opnd => Length_Expr);
9208 else
9209 Hi :=
9210 Make_Attribute_Reference (Loc,
9211 Attribute_Name => Name_Val,
9212 Prefix => New_Occurrence_Of (Index, Loc),
9213 Expressions => New_List (
9214 Make_Op_Add (Loc,
9215 Left_Opnd =>
9216 Make_Attribute_Reference (Loc,
9217 Attribute_Name => Name_Pos,
9218 Prefix => New_Occurrence_Of (Index, Loc),
9219 Expressions => New_List (New_Copy_Tree (Lo))),
9220 Right_Opnd => Length_Expr)));
9221 end if;
9223 return
9224 Make_Range (Loc,
9225 Low_Bound => Lo,
9226 High_Bound => Hi);
9227 end Make_Literal_Range;
9229 --------------------------
9230 -- Make_Non_Empty_Check --
9231 --------------------------
9233 function Make_Non_Empty_Check
9234 (Loc : Source_Ptr;
9235 N : Node_Id) return Node_Id
9237 begin
9238 return
9239 Make_Op_Ne (Loc,
9240 Left_Opnd =>
9241 Make_Attribute_Reference (Loc,
9242 Attribute_Name => Name_Length,
9243 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9244 Right_Opnd =>
9245 Make_Integer_Literal (Loc, 0));
9246 end Make_Non_Empty_Check;
9248 -------------------------
9249 -- Make_Predicate_Call --
9250 -------------------------
9252 -- WARNING: This routine manages Ghost regions. Return statements must be
9253 -- replaced by gotos which jump to the end of the routine and restore the
9254 -- Ghost mode.
9256 function Make_Predicate_Call
9257 (Typ : Entity_Id;
9258 Expr : Node_Id;
9259 Mem : Boolean := False) return Node_Id
9261 Loc : constant Source_Ptr := Sloc (Expr);
9263 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9264 -- Save the Ghost mode to restore on exit
9266 Call : Node_Id;
9267 Func_Id : Entity_Id;
9269 begin
9270 pragma Assert (Present (Predicate_Function (Typ)));
9272 -- The related type may be subject to pragma Ghost. Set the mode now to
9273 -- ensure that the call is properly marked as Ghost.
9275 Set_Ghost_Mode (Typ);
9277 -- Call special membership version if requested and available
9279 if Mem and then Present (Predicate_Function_M (Typ)) then
9280 Func_Id := Predicate_Function_M (Typ);
9281 else
9282 Func_Id := Predicate_Function (Typ);
9283 end if;
9285 -- Case of calling normal predicate function
9287 Call :=
9288 Make_Function_Call (Loc,
9289 Name => New_Occurrence_Of (Func_Id, Loc),
9290 Parameter_Associations => New_List (Relocate_Node (Expr)));
9292 Restore_Ghost_Mode (Saved_GM);
9294 return Call;
9295 end Make_Predicate_Call;
9297 --------------------------
9298 -- Make_Predicate_Check --
9299 --------------------------
9301 function Make_Predicate_Check
9302 (Typ : Entity_Id;
9303 Expr : Node_Id) return Node_Id
9305 procedure Replace_Subtype_Reference (N : Node_Id);
9306 -- Replace current occurrences of the subtype to which a dynamic
9307 -- predicate applies, by the expression that triggers a predicate
9308 -- check. This is needed for aspect Predicate_Failure, for which
9309 -- we do not generate a wrapper procedure, but simply modify the
9310 -- expression for the pragma of the predicate check.
9312 --------------------------------
9313 -- Replace_Subtype_Reference --
9314 --------------------------------
9316 procedure Replace_Subtype_Reference (N : Node_Id) is
9317 begin
9318 Rewrite (N, New_Copy_Tree (Expr));
9320 -- We want to treat the node as if it comes from source, so
9321 -- that ASIS will not ignore it.
9323 Set_Comes_From_Source (N, True);
9324 end Replace_Subtype_Reference;
9326 procedure Replace_Subtype_References is
9327 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9329 -- Local variables
9331 Loc : constant Source_Ptr := Sloc (Expr);
9332 Arg_List : List_Id;
9333 Fail_Expr : Node_Id;
9334 Nam : Name_Id;
9336 -- Start of processing for Make_Predicate_Check
9338 begin
9339 -- If predicate checks are suppressed, then return a null statement. For
9340 -- this call, we check only the scope setting. If the caller wants to
9341 -- check a specific entity's setting, they must do it manually.
9343 if Predicate_Checks_Suppressed (Empty) then
9344 return Make_Null_Statement (Loc);
9345 end if;
9347 -- Do not generate a check within an internal subprogram (stream
9348 -- functions and the like, including including predicate functions).
9350 if Within_Internal_Subprogram then
9351 return Make_Null_Statement (Loc);
9352 end if;
9354 -- Compute proper name to use, we need to get this right so that the
9355 -- right set of check policies apply to the Check pragma we are making.
9357 if Has_Dynamic_Predicate_Aspect (Typ) then
9358 Nam := Name_Dynamic_Predicate;
9359 elsif Has_Static_Predicate_Aspect (Typ) then
9360 Nam := Name_Static_Predicate;
9361 else
9362 Nam := Name_Predicate;
9363 end if;
9365 Arg_List := New_List (
9366 Make_Pragma_Argument_Association (Loc,
9367 Expression => Make_Identifier (Loc, Nam)),
9368 Make_Pragma_Argument_Association (Loc,
9369 Expression => Make_Predicate_Call (Typ, Expr)));
9371 -- If subtype has Predicate_Failure defined, add the correponding
9372 -- expression as an additional pragma parameter, after replacing
9373 -- current instances with the expression being checked.
9375 if Has_Aspect (Typ, Aspect_Predicate_Failure) then
9376 Fail_Expr :=
9377 New_Copy_Tree
9378 (Expression (Find_Aspect (Typ, Aspect_Predicate_Failure)));
9379 Replace_Subtype_References (Fail_Expr, Typ);
9381 Append_To (Arg_List,
9382 Make_Pragma_Argument_Association (Loc,
9383 Expression => Fail_Expr));
9384 end if;
9386 return
9387 Make_Pragma (Loc,
9388 Chars => Name_Check,
9389 Pragma_Argument_Associations => Arg_List);
9390 end Make_Predicate_Check;
9392 ----------------------------
9393 -- Make_Subtype_From_Expr --
9394 ----------------------------
9396 -- 1. If Expr is an unconstrained array expression, creates
9397 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9399 -- 2. If Expr is a unconstrained discriminated type expression, creates
9400 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9402 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9404 function Make_Subtype_From_Expr
9405 (E : Node_Id;
9406 Unc_Typ : Entity_Id;
9407 Related_Id : Entity_Id := Empty) return Node_Id
9409 List_Constr : constant List_Id := New_List;
9410 Loc : constant Source_Ptr := Sloc (E);
9411 D : Entity_Id;
9412 Full_Exp : Node_Id;
9413 Full_Subtyp : Entity_Id;
9414 High_Bound : Entity_Id;
9415 Index_Typ : Entity_Id;
9416 Low_Bound : Entity_Id;
9417 Priv_Subtyp : Entity_Id;
9418 Utyp : Entity_Id;
9420 begin
9421 if Is_Private_Type (Unc_Typ)
9422 and then Has_Unknown_Discriminants (Unc_Typ)
9423 then
9424 -- The caller requests a unique external name for both the private
9425 -- and the full subtype.
9427 if Present (Related_Id) then
9428 Full_Subtyp :=
9429 Make_Defining_Identifier (Loc,
9430 Chars => New_External_Name (Chars (Related_Id), 'C'));
9431 Priv_Subtyp :=
9432 Make_Defining_Identifier (Loc,
9433 Chars => New_External_Name (Chars (Related_Id), 'P'));
9435 else
9436 Full_Subtyp := Make_Temporary (Loc, 'C');
9437 Priv_Subtyp := Make_Temporary (Loc, 'P');
9438 end if;
9440 -- Prepare the subtype completion. Use the base type to find the
9441 -- underlying type because the type may be a generic actual or an
9442 -- explicit subtype.
9444 Utyp := Underlying_Type (Base_Type (Unc_Typ));
9446 Full_Exp :=
9447 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
9448 Set_Parent (Full_Exp, Parent (E));
9450 Insert_Action (E,
9451 Make_Subtype_Declaration (Loc,
9452 Defining_Identifier => Full_Subtyp,
9453 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
9455 -- Define the dummy private subtype
9457 Set_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
9458 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
9459 Set_Scope (Priv_Subtyp, Full_Subtyp);
9460 Set_Is_Constrained (Priv_Subtyp);
9461 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
9462 Set_Is_Itype (Priv_Subtyp);
9463 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
9465 if Is_Tagged_Type (Priv_Subtyp) then
9466 Set_Class_Wide_Type
9467 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
9468 Set_Direct_Primitive_Operations (Priv_Subtyp,
9469 Direct_Primitive_Operations (Unc_Typ));
9470 end if;
9472 Set_Full_View (Priv_Subtyp, Full_Subtyp);
9474 return New_Occurrence_Of (Priv_Subtyp, Loc);
9476 elsif Is_Array_Type (Unc_Typ) then
9477 Index_Typ := First_Index (Unc_Typ);
9478 for J in 1 .. Number_Dimensions (Unc_Typ) loop
9480 -- Capture the bounds of each index constraint in case the context
9481 -- is an object declaration of an unconstrained type initialized
9482 -- by a function call:
9484 -- Obj : Unconstr_Typ := Func_Call;
9486 -- This scenario requires secondary scope management and the index
9487 -- constraint cannot depend on the temporary used to capture the
9488 -- result of the function call.
9490 -- SS_Mark;
9491 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
9492 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
9493 -- Obj : S := Temp.all;
9494 -- SS_Release; -- Temp is gone at this point, bounds of S are
9495 -- -- non existent.
9497 -- Generate:
9498 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
9500 Low_Bound := Make_Temporary (Loc, 'B');
9501 Insert_Action (E,
9502 Make_Object_Declaration (Loc,
9503 Defining_Identifier => Low_Bound,
9504 Object_Definition =>
9505 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9506 Constant_Present => True,
9507 Expression =>
9508 Make_Attribute_Reference (Loc,
9509 Prefix => Duplicate_Subexpr_No_Checks (E),
9510 Attribute_Name => Name_First,
9511 Expressions => New_List (
9512 Make_Integer_Literal (Loc, J)))));
9514 -- Generate:
9515 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
9517 High_Bound := Make_Temporary (Loc, 'B');
9518 Insert_Action (E,
9519 Make_Object_Declaration (Loc,
9520 Defining_Identifier => High_Bound,
9521 Object_Definition =>
9522 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
9523 Constant_Present => True,
9524 Expression =>
9525 Make_Attribute_Reference (Loc,
9526 Prefix => Duplicate_Subexpr_No_Checks (E),
9527 Attribute_Name => Name_Last,
9528 Expressions => New_List (
9529 Make_Integer_Literal (Loc, J)))));
9531 Append_To (List_Constr,
9532 Make_Range (Loc,
9533 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
9534 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
9536 Index_Typ := Next_Index (Index_Typ);
9537 end loop;
9539 elsif Is_Class_Wide_Type (Unc_Typ) then
9540 declare
9541 CW_Subtype : Entity_Id;
9542 EQ_Typ : Entity_Id := Empty;
9544 begin
9545 -- A class-wide equivalent type is not needed on VM targets
9546 -- because the VM back-ends handle the class-wide object
9547 -- initialization itself (and doesn't need or want the
9548 -- additional intermediate type to handle the assignment).
9550 if Expander_Active and then Tagged_Type_Expansion then
9552 -- If this is the class-wide type of a completion that is a
9553 -- record subtype, set the type of the class-wide type to be
9554 -- the full base type, for use in the expanded code for the
9555 -- equivalent type. Should this be done earlier when the
9556 -- completion is analyzed ???
9558 if Is_Private_Type (Etype (Unc_Typ))
9559 and then
9560 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
9561 then
9562 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
9563 end if;
9565 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
9566 end if;
9568 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
9569 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
9570 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
9572 return New_Occurrence_Of (CW_Subtype, Loc);
9573 end;
9575 -- Indefinite record type with discriminants
9577 else
9578 D := First_Discriminant (Unc_Typ);
9579 while Present (D) loop
9580 Append_To (List_Constr,
9581 Make_Selected_Component (Loc,
9582 Prefix => Duplicate_Subexpr_No_Checks (E),
9583 Selector_Name => New_Occurrence_Of (D, Loc)));
9585 Next_Discriminant (D);
9586 end loop;
9587 end if;
9589 return
9590 Make_Subtype_Indication (Loc,
9591 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
9592 Constraint =>
9593 Make_Index_Or_Discriminant_Constraint (Loc,
9594 Constraints => List_Constr));
9595 end Make_Subtype_From_Expr;
9597 ---------------
9598 -- Map_Types --
9599 ---------------
9601 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
9603 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
9604 -- avoid deep indentation of code.
9606 -- NOTE: Routines which deal with discriminant mapping operate on the
9607 -- [underlying/record] full view of various types because those views
9608 -- contain all discriminants and stored constraints.
9610 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
9611 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
9612 -- overriding chain starting from Prim whose dispatching type is parent
9613 -- type Par_Typ and add a mapping between the result and primitive Prim.
9615 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
9616 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
9617 -- the inheritance or overriding chain of subprogram Subp. Return Empty
9618 -- if no such primitive is available.
9620 function Build_Chain
9621 (Par_Typ : Entity_Id;
9622 Deriv_Typ : Entity_Id) return Elist_Id;
9623 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
9624 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
9625 -- list has the form:
9627 -- head tail
9628 -- v v
9629 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
9631 -- Note that Par_Typ is not part of the resulting derivation chain
9633 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
9634 -- Return the view of type Typ which could potentially contains either
9635 -- the discriminants or stored constraints of the type.
9637 function Find_Discriminant_Value
9638 (Discr : Entity_Id;
9639 Par_Typ : Entity_Id;
9640 Deriv_Typ : Entity_Id;
9641 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
9642 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
9643 -- in the derivation chain starting from parent type Par_Typ leading to
9644 -- derived type Deriv_Typ. The returned value is one of the following:
9646 -- * An entity which is either a discriminant or a non-discriminant
9647 -- name, and renames/constraints Discr.
9649 -- * An expression which constraints Discr
9651 -- Typ_Elmt is an element of the derivation chain created by routine
9652 -- Build_Chain and denotes the current ancestor being examined.
9654 procedure Map_Discriminants
9655 (Par_Typ : Entity_Id;
9656 Deriv_Typ : Entity_Id);
9657 -- Map each discriminant of type Par_Typ to a meaningful constraint
9658 -- from the point of view of type Deriv_Typ.
9660 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
9661 -- Map each primitive of type Par_Typ to a corresponding primitive of
9662 -- type Deriv_Typ.
9664 -------------------
9665 -- Add_Primitive --
9666 -------------------
9668 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
9669 Par_Prim : Entity_Id;
9671 begin
9672 -- Inspect the inheritance chain through the Alias attribute and the
9673 -- overriding chain through the Overridden_Operation looking for an
9674 -- ancestor primitive with the appropriate dispatching type.
9676 Par_Prim := Prim;
9677 while Present (Par_Prim) loop
9678 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
9679 Par_Prim := Ancestor_Primitive (Par_Prim);
9680 end loop;
9682 -- Create a mapping of the form:
9684 -- parent type primitive -> derived type primitive
9686 if Present (Par_Prim) then
9687 Type_Map.Set (Par_Prim, Prim);
9688 end if;
9689 end Add_Primitive;
9691 ------------------------
9692 -- Ancestor_Primitive --
9693 ------------------------
9695 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
9696 Inher_Prim : constant Entity_Id := Alias (Subp);
9697 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
9699 begin
9700 -- The current subprogram overrides an ancestor primitive
9702 if Present (Over_Prim) then
9703 return Over_Prim;
9705 -- The current subprogram is an internally generated alias of an
9706 -- inherited ancestor primitive.
9708 elsif Present (Inher_Prim) then
9709 return Inher_Prim;
9711 -- Otherwise the current subprogram is the root of the inheritance or
9712 -- overriding chain.
9714 else
9715 return Empty;
9716 end if;
9717 end Ancestor_Primitive;
9719 -----------------
9720 -- Build_Chain --
9721 -----------------
9723 function Build_Chain
9724 (Par_Typ : Entity_Id;
9725 Deriv_Typ : Entity_Id) return Elist_Id
9727 Anc_Typ : Entity_Id;
9728 Chain : Elist_Id;
9729 Curr_Typ : Entity_Id;
9731 begin
9732 Chain := New_Elmt_List;
9734 -- Add the derived type to the derivation chain
9736 Prepend_Elmt (Deriv_Typ, Chain);
9738 -- Examine all ancestors starting from the derived type climbing
9739 -- towards parent type Par_Typ.
9741 Curr_Typ := Deriv_Typ;
9742 loop
9743 -- Handle the case where the current type is a record which
9744 -- derives from a subtype.
9746 -- subtype Sub_Typ is Par_Typ ...
9747 -- type Deriv_Typ is Sub_Typ ...
9749 if Ekind (Curr_Typ) = E_Record_Type
9750 and then Present (Parent_Subtype (Curr_Typ))
9751 then
9752 Anc_Typ := Parent_Subtype (Curr_Typ);
9754 -- Handle the case where the current type is a record subtype of
9755 -- another subtype.
9757 -- subtype Sub_Typ1 is Par_Typ ...
9758 -- subtype Sub_Typ2 is Sub_Typ1 ...
9760 elsif Ekind (Curr_Typ) = E_Record_Subtype
9761 and then Present (Cloned_Subtype (Curr_Typ))
9762 then
9763 Anc_Typ := Cloned_Subtype (Curr_Typ);
9765 -- Otherwise use the direct parent type
9767 else
9768 Anc_Typ := Etype (Curr_Typ);
9769 end if;
9771 -- Use the first subtype when dealing with itypes
9773 if Is_Itype (Anc_Typ) then
9774 Anc_Typ := First_Subtype (Anc_Typ);
9775 end if;
9777 -- Work with the view which contains the discriminants and stored
9778 -- constraints.
9780 Anc_Typ := Discriminated_View (Anc_Typ);
9782 -- Stop the climb when either the parent type has been reached or
9783 -- there are no more ancestors left to examine.
9785 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
9787 Prepend_Unique_Elmt (Anc_Typ, Chain);
9788 Curr_Typ := Anc_Typ;
9789 end loop;
9791 return Chain;
9792 end Build_Chain;
9794 ------------------------
9795 -- Discriminated_View --
9796 ------------------------
9798 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
9799 T : Entity_Id;
9801 begin
9802 T := Typ;
9804 -- Use the [underlying] full view when dealing with private types
9805 -- because the view contains all inherited discriminants or stored
9806 -- constraints.
9808 if Is_Private_Type (T) then
9809 if Present (Underlying_Full_View (T)) then
9810 T := Underlying_Full_View (T);
9812 elsif Present (Full_View (T)) then
9813 T := Full_View (T);
9814 end if;
9815 end if;
9817 -- Use the underlying record view when the type is an extenstion of
9818 -- a parent type with unknown discriminants because the view contains
9819 -- all inherited discriminants or stored constraints.
9821 if Ekind (T) = E_Record_Type
9822 and then Present (Underlying_Record_View (T))
9823 then
9824 T := Underlying_Record_View (T);
9825 end if;
9827 return T;
9828 end Discriminated_View;
9830 -----------------------------
9831 -- Find_Discriminant_Value --
9832 -----------------------------
9834 function Find_Discriminant_Value
9835 (Discr : Entity_Id;
9836 Par_Typ : Entity_Id;
9837 Deriv_Typ : Entity_Id;
9838 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
9840 Discr_Pos : constant Uint := Discriminant_Number (Discr);
9841 Typ : constant Entity_Id := Node (Typ_Elmt);
9843 function Find_Constraint_Value
9844 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
9845 -- Given constraint Constr, find what it denotes. This is either:
9847 -- * An entity which is either a discriminant or a name
9849 -- * An expression
9851 ---------------------------
9852 -- Find_Constraint_Value --
9853 ---------------------------
9855 function Find_Constraint_Value
9856 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
9858 begin
9859 if Nkind (Constr) in N_Entity then
9861 -- The constraint denotes a discriminant of the curren type
9862 -- which renames the ancestor discriminant:
9864 -- vv
9865 -- type Typ (D1 : ...; DN : ...) is
9866 -- new Anc (Discr => D1) with ...
9867 -- ^^
9869 if Ekind (Constr) = E_Discriminant then
9871 -- The discriminant belongs to derived type Deriv_Typ. This
9872 -- is the final value for the ancestor discriminant as the
9873 -- derivations chain has been fully exhausted.
9875 if Typ = Deriv_Typ then
9876 return Constr;
9878 -- Otherwise the discriminant may be renamed or constrained
9879 -- at a lower level. Continue looking down the derivation
9880 -- chain.
9882 else
9883 return
9884 Find_Discriminant_Value
9885 (Discr => Constr,
9886 Par_Typ => Par_Typ,
9887 Deriv_Typ => Deriv_Typ,
9888 Typ_Elmt => Next_Elmt (Typ_Elmt));
9889 end if;
9891 -- Otherwise the constraint denotes a reference to some name
9892 -- which results in a Girder discriminant:
9894 -- vvvv
9895 -- Name : ...;
9896 -- type Typ (D1 : ...; DN : ...) is
9897 -- new Anc (Discr => Name) with ...
9898 -- ^^^^
9900 -- Return the name as this is the proper constraint of the
9901 -- discriminant.
9903 else
9904 return Constr;
9905 end if;
9907 -- The constraint denotes a reference to a name
9909 elsif Is_Entity_Name (Constr) then
9910 return Find_Constraint_Value (Entity (Constr));
9912 -- Otherwise the current constraint is an expression which yields
9913 -- a Girder discriminant:
9915 -- type Typ (D1 : ...; DN : ...) is
9916 -- new Anc (Discr => <expression>) with ...
9917 -- ^^^^^^^^^^
9919 -- Return the expression as this is the proper constraint of the
9920 -- discriminant.
9922 else
9923 return Constr;
9924 end if;
9925 end Find_Constraint_Value;
9927 -- Local variables
9929 Constrs : constant Elist_Id := Stored_Constraint (Typ);
9931 Constr_Elmt : Elmt_Id;
9932 Pos : Uint;
9933 Typ_Discr : Entity_Id;
9935 -- Start of processing for Find_Discriminant_Value
9937 begin
9938 -- The algorithm for finding the value of a discriminant works as
9939 -- follows. First, it recreates the derivation chain from Par_Typ
9940 -- to Deriv_Typ as a list:
9942 -- Par_Typ (shown for completeness)
9943 -- v
9944 -- Ancestor_N <-- head of chain
9945 -- v
9946 -- Ancestor_1
9947 -- v
9948 -- Deriv_Typ <-- tail of chain
9950 -- The algorithm then traces the fate of a parent discriminant down
9951 -- the derivation chain. At each derivation level, the discriminant
9952 -- may be either inherited or constrained.
9954 -- 1) Discriminant is inherited: there are two cases, depending on
9955 -- which type is inheriting.
9957 -- 1.1) Deriv_Typ is inheriting:
9959 -- type Ancestor (D_1 : ...) is tagged ...
9960 -- type Deriv_Typ is new Ancestor ...
9962 -- In this case the inherited discriminant is the final value of
9963 -- the parent discriminant because the end of the derivation chain
9964 -- has been reached.
9966 -- 1.2) Some other type is inheriting:
9968 -- type Ancestor_1 (D_1 : ...) is tagged ...
9969 -- type Ancestor_2 is new Ancestor_1 ...
9971 -- In this case the algorithm continues to trace the fate of the
9972 -- inherited discriminant down the derivation chain because it may
9973 -- be further inherited or constrained.
9975 -- 2) Discriminant is constrained: there are three cases, depending
9976 -- on what the constraint is.
9978 -- 2.1) The constraint is another discriminant (aka renaming):
9980 -- type Ancestor_1 (D_1 : ...) is tagged ...
9981 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
9983 -- In this case the constraining discriminant becomes the one to
9984 -- track down the derivation chain. The algorithm already knows
9985 -- that D_2 constrains D_1, therefore if the algorithm finds the
9986 -- value of D_2, then this would also be the value for D_1.
9988 -- 2.2) The constraint is a name (aka Girder):
9990 -- Name : ...
9991 -- type Ancestor_1 (D_1 : ...) is tagged ...
9992 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
9994 -- In this case the name is the final value of D_1 because the
9995 -- discriminant cannot be further constrained.
9997 -- 2.3) The constraint is an expression (aka Girder):
9999 -- type Ancestor_1 (D_1 : ...) is tagged ...
10000 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10002 -- Similar to 2.2, the expression is the final value of D_1
10004 Pos := Uint_1;
10006 -- When a derived type constrains its parent type, all constaints
10007 -- appear in the Stored_Constraint list. Examine the list looking
10008 -- for a positional match.
10010 if Present (Constrs) then
10011 Constr_Elmt := First_Elmt (Constrs);
10012 while Present (Constr_Elmt) loop
10014 -- The position of the current constraint matches that of the
10015 -- ancestor discriminant.
10017 if Pos = Discr_Pos then
10018 return Find_Constraint_Value (Node (Constr_Elmt));
10019 end if;
10021 Next_Elmt (Constr_Elmt);
10022 Pos := Pos + 1;
10023 end loop;
10025 -- Otherwise the derived type does not constraint its parent type in
10026 -- which case it inherits the parent discriminants.
10028 else
10029 Typ_Discr := First_Discriminant (Typ);
10030 while Present (Typ_Discr) loop
10032 -- The position of the current discriminant matches that of the
10033 -- ancestor discriminant.
10035 if Pos = Discr_Pos then
10036 return Find_Constraint_Value (Typ_Discr);
10037 end if;
10039 Next_Discriminant (Typ_Discr);
10040 Pos := Pos + 1;
10041 end loop;
10042 end if;
10044 -- A discriminant must always have a corresponding value. This is
10045 -- either another discriminant, a name, or an expression. If this
10046 -- point is reached, them most likely the derivation chain employs
10047 -- the wrong views of types.
10049 pragma Assert (False);
10051 return Empty;
10052 end Find_Discriminant_Value;
10054 -----------------------
10055 -- Map_Discriminants --
10056 -----------------------
10058 procedure Map_Discriminants
10059 (Par_Typ : Entity_Id;
10060 Deriv_Typ : Entity_Id)
10062 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10064 Discr : Entity_Id;
10065 Discr_Val : Node_Or_Entity_Id;
10067 begin
10068 -- Examine each discriminant of parent type Par_Typ and find a
10069 -- suitable value for it from the point of view of derived type
10070 -- Deriv_Typ.
10072 if Has_Discriminants (Par_Typ) then
10073 Discr := First_Discriminant (Par_Typ);
10074 while Present (Discr) loop
10075 Discr_Val :=
10076 Find_Discriminant_Value
10077 (Discr => Discr,
10078 Par_Typ => Par_Typ,
10079 Deriv_Typ => Deriv_Typ,
10080 Typ_Elmt => First_Elmt (Deriv_Chain));
10082 -- Create a mapping of the form:
10084 -- parent type discriminant -> value
10086 Type_Map.Set (Discr, Discr_Val);
10088 Next_Discriminant (Discr);
10089 end loop;
10090 end if;
10091 end Map_Discriminants;
10093 --------------------
10094 -- Map_Primitives --
10095 --------------------
10097 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10098 Deriv_Prim : Entity_Id;
10099 Par_Prim : Entity_Id;
10100 Par_Prims : Elist_Id;
10101 Prim_Elmt : Elmt_Id;
10103 begin
10104 -- Inspect the primitives of the derived type and determine whether
10105 -- they relate to the primitives of the parent type. If there is a
10106 -- meaningful relation, create a mapping of the form:
10108 -- parent type primitive -> perived type primitive
10110 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10111 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10112 while Present (Prim_Elmt) loop
10113 Deriv_Prim := Node (Prim_Elmt);
10115 if Is_Subprogram (Deriv_Prim)
10116 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10117 then
10118 Add_Primitive (Deriv_Prim, Par_Typ);
10119 end if;
10121 Next_Elmt (Prim_Elmt);
10122 end loop;
10123 end if;
10125 -- If the parent operation is an interface operation, the overriding
10126 -- indicator is not present. Instead, we get from the interface
10127 -- operation the primitive of the current type that implements it.
10129 if Is_Interface (Par_Typ) then
10130 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10132 if Present (Par_Prims) then
10133 Prim_Elmt := First_Elmt (Par_Prims);
10135 while Present (Prim_Elmt) loop
10136 Par_Prim := Node (Prim_Elmt);
10137 Deriv_Prim :=
10138 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10140 if Present (Deriv_Prim) then
10141 Type_Map.Set (Par_Prim, Deriv_Prim);
10142 end if;
10144 Next_Elmt (Prim_Elmt);
10145 end loop;
10146 end if;
10147 end if;
10148 end Map_Primitives;
10150 -- Start of processing for Map_Types
10152 begin
10153 -- Nothing to do if there are no types to work with
10155 if No (Parent_Type) or else No (Derived_Type) then
10156 return;
10158 -- Nothing to do if the mapping already exists
10160 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10161 return;
10163 -- Nothing to do if both types are not tagged. Note that untagged types
10164 -- do not have primitive operations and their discriminants are already
10165 -- handled by gigi.
10167 elsif not Is_Tagged_Type (Parent_Type)
10168 or else not Is_Tagged_Type (Derived_Type)
10169 then
10170 return;
10171 end if;
10173 -- Create a mapping of the form
10175 -- parent type -> derived type
10177 -- to prevent any subsequent attempts to produce the same relations
10179 Type_Map.Set (Parent_Type, Derived_Type);
10181 -- Create mappings of the form
10183 -- parent type discriminant -> derived type discriminant
10184 -- <or>
10185 -- parent type discriminant -> constraint
10187 -- Note that mapping of discriminants breaks privacy because it needs to
10188 -- work with those views which contains the discriminants and any stored
10189 -- constraints.
10191 Map_Discriminants
10192 (Par_Typ => Discriminated_View (Parent_Type),
10193 Deriv_Typ => Discriminated_View (Derived_Type));
10195 -- Create mappings of the form
10197 -- parent type primitive -> derived type primitive
10199 Map_Primitives
10200 (Par_Typ => Parent_Type,
10201 Deriv_Typ => Derived_Type);
10202 end Map_Types;
10204 ----------------------------
10205 -- Matching_Standard_Type --
10206 ----------------------------
10208 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10209 pragma Assert (Is_Scalar_Type (Typ));
10210 Siz : constant Uint := Esize (Typ);
10212 begin
10213 -- Floating-point cases
10215 if Is_Floating_Point_Type (Typ) then
10216 if Siz <= Esize (Standard_Short_Float) then
10217 return Standard_Short_Float;
10218 elsif Siz <= Esize (Standard_Float) then
10219 return Standard_Float;
10220 elsif Siz <= Esize (Standard_Long_Float) then
10221 return Standard_Long_Float;
10222 elsif Siz <= Esize (Standard_Long_Long_Float) then
10223 return Standard_Long_Long_Float;
10224 else
10225 raise Program_Error;
10226 end if;
10228 -- Integer cases (includes fixed-point types)
10230 -- Unsigned integer cases (includes normal enumeration types)
10232 elsif Is_Unsigned_Type (Typ) then
10233 if Siz <= Esize (Standard_Short_Short_Unsigned) then
10234 return Standard_Short_Short_Unsigned;
10235 elsif Siz <= Esize (Standard_Short_Unsigned) then
10236 return Standard_Short_Unsigned;
10237 elsif Siz <= Esize (Standard_Unsigned) then
10238 return Standard_Unsigned;
10239 elsif Siz <= Esize (Standard_Long_Unsigned) then
10240 return Standard_Long_Unsigned;
10241 elsif Siz <= Esize (Standard_Long_Long_Unsigned) then
10242 return Standard_Long_Long_Unsigned;
10243 else
10244 raise Program_Error;
10245 end if;
10247 -- Signed integer cases
10249 else
10250 if Siz <= Esize (Standard_Short_Short_Integer) then
10251 return Standard_Short_Short_Integer;
10252 elsif Siz <= Esize (Standard_Short_Integer) then
10253 return Standard_Short_Integer;
10254 elsif Siz <= Esize (Standard_Integer) then
10255 return Standard_Integer;
10256 elsif Siz <= Esize (Standard_Long_Integer) then
10257 return Standard_Long_Integer;
10258 elsif Siz <= Esize (Standard_Long_Long_Integer) then
10259 return Standard_Long_Long_Integer;
10260 else
10261 raise Program_Error;
10262 end if;
10263 end if;
10264 end Matching_Standard_Type;
10266 -----------------------------
10267 -- May_Generate_Large_Temp --
10268 -----------------------------
10270 -- At the current time, the only types that we return False for (i.e. where
10271 -- we decide we know they cannot generate large temps) are ones where we
10272 -- know the size is 256 bits or less at compile time, and we are still not
10273 -- doing a thorough job on arrays and records ???
10275 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10276 begin
10277 if not Size_Known_At_Compile_Time (Typ) then
10278 return False;
10280 elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
10281 return False;
10283 elsif Is_Array_Type (Typ)
10284 and then Present (Packed_Array_Impl_Type (Typ))
10285 then
10286 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10288 -- We could do more here to find other small types ???
10290 else
10291 return True;
10292 end if;
10293 end May_Generate_Large_Temp;
10295 ------------------------
10296 -- Needs_Finalization --
10297 ------------------------
10299 function Needs_Finalization (T : Entity_Id) return Boolean is
10300 function Has_Some_Controlled_Component (Rec : Entity_Id) return Boolean;
10301 -- If type is not frozen yet, check explicitly among its components,
10302 -- because the Has_Controlled_Component flag is not necessarily set.
10304 -----------------------------------
10305 -- Has_Some_Controlled_Component --
10306 -----------------------------------
10308 function Has_Some_Controlled_Component
10309 (Rec : Entity_Id) return Boolean
10311 Comp : Entity_Id;
10313 begin
10314 if Has_Controlled_Component (Rec) then
10315 return True;
10317 elsif not Is_Frozen (Rec) then
10318 if Is_Record_Type (Rec) then
10319 Comp := First_Entity (Rec);
10321 while Present (Comp) loop
10322 if not Is_Type (Comp)
10323 and then Needs_Finalization (Etype (Comp))
10324 then
10325 return True;
10326 end if;
10328 Next_Entity (Comp);
10329 end loop;
10331 return False;
10333 else
10334 return
10335 Is_Array_Type (Rec)
10336 and then Needs_Finalization (Component_Type (Rec));
10337 end if;
10338 else
10339 return False;
10340 end if;
10341 end Has_Some_Controlled_Component;
10343 -- Start of processing for Needs_Finalization
10345 begin
10346 -- Certain run-time configurations and targets do not provide support
10347 -- for controlled types.
10349 if Restriction_Active (No_Finalization) then
10350 return False;
10352 -- C++ types are not considered controlled. It is assumed that the
10353 -- non-Ada side will handle their clean up.
10355 elsif Convention (T) = Convention_CPP then
10356 return False;
10358 -- Never needs finalization if Disable_Controlled set
10360 elsif Disable_Controlled (T) then
10361 return False;
10363 elsif Is_Class_Wide_Type (T) and then Disable_Controlled (Etype (T)) then
10364 return False;
10366 else
10367 -- Class-wide types are treated as controlled because derivations
10368 -- from the root type can introduce controlled components.
10370 return
10371 Is_Class_Wide_Type (T)
10372 or else Is_Controlled (T)
10373 or else Has_Some_Controlled_Component (T)
10374 or else
10375 (Is_Concurrent_Type (T)
10376 and then Present (Corresponding_Record_Type (T))
10377 and then Needs_Finalization (Corresponding_Record_Type (T)));
10378 end if;
10379 end Needs_Finalization;
10381 ----------------------------
10382 -- Needs_Constant_Address --
10383 ----------------------------
10385 function Needs_Constant_Address
10386 (Decl : Node_Id;
10387 Typ : Entity_Id) return Boolean
10389 begin
10391 -- If we have no initialization of any kind, then we don't need to place
10392 -- any restrictions on the address clause, because the object will be
10393 -- elaborated after the address clause is evaluated. This happens if the
10394 -- declaration has no initial expression, or the type has no implicit
10395 -- initialization, or the object is imported.
10397 -- The same holds for all initialized scalar types and all access types.
10398 -- Packed bit arrays of size up to 64 are represented using a modular
10399 -- type with an initialization (to zero) and can be processed like other
10400 -- initialized scalar types.
10402 -- If the type is controlled, code to attach the object to a
10403 -- finalization chain is generated at the point of declaration, and
10404 -- therefore the elaboration of the object cannot be delayed: the
10405 -- address expression must be a constant.
10407 if No (Expression (Decl))
10408 and then not Needs_Finalization (Typ)
10409 and then
10410 (not Has_Non_Null_Base_Init_Proc (Typ)
10411 or else Is_Imported (Defining_Identifier (Decl)))
10412 then
10413 return False;
10415 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10416 or else Is_Access_Type (Typ)
10417 or else
10418 (Is_Bit_Packed_Array (Typ)
10419 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10420 then
10421 return False;
10423 else
10425 -- Otherwise, we require the address clause to be constant because
10426 -- the call to the initialization procedure (or the attach code) has
10427 -- to happen at the point of the declaration.
10429 -- Actually the IP call has been moved to the freeze actions anyway,
10430 -- so maybe we can relax this restriction???
10432 return True;
10433 end if;
10434 end Needs_Constant_Address;
10436 ----------------------------
10437 -- New_Class_Wide_Subtype --
10438 ----------------------------
10440 function New_Class_Wide_Subtype
10441 (CW_Typ : Entity_Id;
10442 N : Node_Id) return Entity_Id
10444 Res : constant Entity_Id := Create_Itype (E_Void, N);
10445 Res_Name : constant Name_Id := Chars (Res);
10446 Res_Scope : constant Entity_Id := Scope (Res);
10448 begin
10449 Copy_Node (CW_Typ, Res);
10450 Set_Comes_From_Source (Res, False);
10451 Set_Sloc (Res, Sloc (N));
10452 Set_Is_Itype (Res);
10453 Set_Associated_Node_For_Itype (Res, N);
10454 Set_Is_Public (Res, False); -- By default, may be changed below.
10455 Set_Public_Status (Res);
10456 Set_Chars (Res, Res_Name);
10457 Set_Scope (Res, Res_Scope);
10458 Set_Ekind (Res, E_Class_Wide_Subtype);
10459 Set_Next_Entity (Res, Empty);
10460 Set_Etype (Res, Base_Type (CW_Typ));
10461 Set_Is_Frozen (Res, False);
10462 Set_Freeze_Node (Res, Empty);
10463 return (Res);
10464 end New_Class_Wide_Subtype;
10466 --------------------------------
10467 -- Non_Limited_Designated_Type --
10468 ---------------------------------
10470 function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
10471 Desig : constant Entity_Id := Designated_Type (T);
10472 begin
10473 if Has_Non_Limited_View (Desig) then
10474 return Non_Limited_View (Desig);
10475 else
10476 return Desig;
10477 end if;
10478 end Non_Limited_Designated_Type;
10480 -----------------------------------
10481 -- OK_To_Do_Constant_Replacement --
10482 -----------------------------------
10484 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
10485 ES : constant Entity_Id := Scope (E);
10486 CS : Entity_Id;
10488 begin
10489 -- Do not replace statically allocated objects, because they may be
10490 -- modified outside the current scope.
10492 if Is_Statically_Allocated (E) then
10493 return False;
10495 -- Do not replace aliased or volatile objects, since we don't know what
10496 -- else might change the value.
10498 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
10499 return False;
10501 -- Debug flag -gnatdM disconnects this optimization
10503 elsif Debug_Flag_MM then
10504 return False;
10506 -- Otherwise check scopes
10508 else
10509 CS := Current_Scope;
10511 loop
10512 -- If we are in right scope, replacement is safe
10514 if CS = ES then
10515 return True;
10517 -- Packages do not affect the determination of safety
10519 elsif Ekind (CS) = E_Package then
10520 exit when CS = Standard_Standard;
10521 CS := Scope (CS);
10523 -- Blocks do not affect the determination of safety
10525 elsif Ekind (CS) = E_Block then
10526 CS := Scope (CS);
10528 -- Loops do not affect the determination of safety. Note that we
10529 -- kill all current values on entry to a loop, so we are just
10530 -- talking about processing within a loop here.
10532 elsif Ekind (CS) = E_Loop then
10533 CS := Scope (CS);
10535 -- Otherwise, the reference is dubious, and we cannot be sure that
10536 -- it is safe to do the replacement.
10538 else
10539 exit;
10540 end if;
10541 end loop;
10543 return False;
10544 end if;
10545 end OK_To_Do_Constant_Replacement;
10547 ------------------------------------
10548 -- Possible_Bit_Aligned_Component --
10549 ------------------------------------
10551 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
10552 begin
10553 -- Do not process an unanalyzed node because it is not yet decorated and
10554 -- most checks performed below will fail.
10556 if not Analyzed (N) then
10557 return False;
10558 end if;
10560 case Nkind (N) is
10562 -- Case of indexed component
10564 when N_Indexed_Component =>
10565 declare
10566 P : constant Node_Id := Prefix (N);
10567 Ptyp : constant Entity_Id := Etype (P);
10569 begin
10570 -- If we know the component size and it is less than 64, then
10571 -- we are definitely OK. The back end always does assignment of
10572 -- misaligned small objects correctly.
10574 if Known_Static_Component_Size (Ptyp)
10575 and then Component_Size (Ptyp) <= 64
10576 then
10577 return False;
10579 -- Otherwise, we need to test the prefix, to see if we are
10580 -- indexing from a possibly unaligned component.
10582 else
10583 return Possible_Bit_Aligned_Component (P);
10584 end if;
10585 end;
10587 -- Case of selected component
10589 when N_Selected_Component =>
10590 declare
10591 P : constant Node_Id := Prefix (N);
10592 Comp : constant Entity_Id := Entity (Selector_Name (N));
10594 begin
10595 -- If there is no component clause, then we are in the clear
10596 -- since the back end will never misalign a large component
10597 -- unless it is forced to do so. In the clear means we need
10598 -- only the recursive test on the prefix.
10600 if Component_May_Be_Bit_Aligned (Comp) then
10601 return True;
10602 else
10603 return Possible_Bit_Aligned_Component (P);
10604 end if;
10605 end;
10607 -- For a slice, test the prefix, if that is possibly misaligned,
10608 -- then for sure the slice is.
10610 when N_Slice =>
10611 return Possible_Bit_Aligned_Component (Prefix (N));
10613 -- For an unchecked conversion, check whether the expression may
10614 -- be bit-aligned.
10616 when N_Unchecked_Type_Conversion =>
10617 return Possible_Bit_Aligned_Component (Expression (N));
10619 -- If we have none of the above, it means that we have fallen off the
10620 -- top testing prefixes recursively, and we now have a stand alone
10621 -- object, where we don't have a problem, unless this is a renaming,
10622 -- in which case we need to look into the renamed object.
10624 when others =>
10625 if Is_Entity_Name (N)
10626 and then Present (Renamed_Object (Entity (N)))
10627 then
10628 return
10629 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
10630 else
10631 return False;
10632 end if;
10633 end case;
10634 end Possible_Bit_Aligned_Component;
10636 -----------------------------------------------
10637 -- Process_Statements_For_Controlled_Objects --
10638 -----------------------------------------------
10640 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
10641 Loc : constant Source_Ptr := Sloc (N);
10643 function Are_Wrapped (L : List_Id) return Boolean;
10644 -- Determine whether list L contains only one statement which is a block
10646 function Wrap_Statements_In_Block
10647 (L : List_Id;
10648 Scop : Entity_Id := Current_Scope) return Node_Id;
10649 -- Given a list of statements L, wrap it in a block statement and return
10650 -- the generated node. Scop is either the current scope or the scope of
10651 -- the context (if applicable).
10653 -----------------
10654 -- Are_Wrapped --
10655 -----------------
10657 function Are_Wrapped (L : List_Id) return Boolean is
10658 Stmt : constant Node_Id := First (L);
10659 begin
10660 return
10661 Present (Stmt)
10662 and then No (Next (Stmt))
10663 and then Nkind (Stmt) = N_Block_Statement;
10664 end Are_Wrapped;
10666 ------------------------------
10667 -- Wrap_Statements_In_Block --
10668 ------------------------------
10670 function Wrap_Statements_In_Block
10671 (L : List_Id;
10672 Scop : Entity_Id := Current_Scope) return Node_Id
10674 Block_Id : Entity_Id;
10675 Block_Nod : Node_Id;
10676 Iter_Loop : Entity_Id;
10678 begin
10679 Block_Nod :=
10680 Make_Block_Statement (Loc,
10681 Declarations => No_List,
10682 Handled_Statement_Sequence =>
10683 Make_Handled_Sequence_Of_Statements (Loc,
10684 Statements => L));
10686 -- Create a label for the block in case the block needs to manage the
10687 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
10689 Add_Block_Identifier (Block_Nod, Block_Id);
10691 -- When wrapping the statements of an iterator loop, check whether
10692 -- the loop requires secondary stack management and if so, propagate
10693 -- the appropriate flags to the block. This ensures that the cursor
10694 -- is properly cleaned up at each iteration of the loop.
10696 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
10698 if Present (Iter_Loop) then
10699 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
10701 -- Secondary stack reclamation is suppressed when the associated
10702 -- iterator loop contains a return statement which uses the stack.
10704 Set_Sec_Stack_Needed_For_Return
10705 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
10706 end if;
10708 return Block_Nod;
10709 end Wrap_Statements_In_Block;
10711 -- Local variables
10713 Block : Node_Id;
10715 -- Start of processing for Process_Statements_For_Controlled_Objects
10717 begin
10718 -- Whenever a non-handled statement list is wrapped in a block, the
10719 -- block must be explicitly analyzed to redecorate all entities in the
10720 -- list and ensure that a finalizer is properly built.
10722 case Nkind (N) is
10723 when N_Conditional_Entry_Call
10724 | N_Elsif_Part
10725 | N_If_Statement
10726 | N_Selective_Accept
10728 -- Check the "then statements" for elsif parts and if statements
10730 if Nkind_In (N, N_Elsif_Part, N_If_Statement)
10731 and then not Is_Empty_List (Then_Statements (N))
10732 and then not Are_Wrapped (Then_Statements (N))
10733 and then Requires_Cleanup_Actions
10734 (Then_Statements (N), False, False)
10735 then
10736 Block := Wrap_Statements_In_Block (Then_Statements (N));
10737 Set_Then_Statements (N, New_List (Block));
10739 Analyze (Block);
10740 end if;
10742 -- Check the "else statements" for conditional entry calls, if
10743 -- statements and selective accepts.
10745 if Nkind_In (N, N_Conditional_Entry_Call,
10746 N_If_Statement,
10747 N_Selective_Accept)
10748 and then not Is_Empty_List (Else_Statements (N))
10749 and then not Are_Wrapped (Else_Statements (N))
10750 and then Requires_Cleanup_Actions
10751 (Else_Statements (N), False, False)
10752 then
10753 Block := Wrap_Statements_In_Block (Else_Statements (N));
10754 Set_Else_Statements (N, New_List (Block));
10756 Analyze (Block);
10757 end if;
10759 when N_Abortable_Part
10760 | N_Accept_Alternative
10761 | N_Case_Statement_Alternative
10762 | N_Delay_Alternative
10763 | N_Entry_Call_Alternative
10764 | N_Exception_Handler
10765 | N_Loop_Statement
10766 | N_Triggering_Alternative
10768 if not Is_Empty_List (Statements (N))
10769 and then not Are_Wrapped (Statements (N))
10770 and then Requires_Cleanup_Actions (Statements (N), False, False)
10771 then
10772 if Nkind (N) = N_Loop_Statement
10773 and then Present (Identifier (N))
10774 then
10775 Block :=
10776 Wrap_Statements_In_Block
10777 (L => Statements (N),
10778 Scop => Entity (Identifier (N)));
10779 else
10780 Block := Wrap_Statements_In_Block (Statements (N));
10781 end if;
10783 Set_Statements (N, New_List (Block));
10784 Analyze (Block);
10785 end if;
10787 when others =>
10788 null;
10789 end case;
10790 end Process_Statements_For_Controlled_Objects;
10792 ------------------
10793 -- Power_Of_Two --
10794 ------------------
10796 function Power_Of_Two (N : Node_Id) return Nat is
10797 Typ : constant Entity_Id := Etype (N);
10798 pragma Assert (Is_Integer_Type (Typ));
10800 Siz : constant Nat := UI_To_Int (Esize (Typ));
10801 Val : Uint;
10803 begin
10804 if not Compile_Time_Known_Value (N) then
10805 return 0;
10807 else
10808 Val := Expr_Value (N);
10809 for J in 1 .. Siz - 1 loop
10810 if Val = Uint_2 ** J then
10811 return J;
10812 end if;
10813 end loop;
10815 return 0;
10816 end if;
10817 end Power_Of_Two;
10819 ----------------------
10820 -- Remove_Init_Call --
10821 ----------------------
10823 function Remove_Init_Call
10824 (Var : Entity_Id;
10825 Rep_Clause : Node_Id) return Node_Id
10827 Par : constant Node_Id := Parent (Var);
10828 Typ : constant Entity_Id := Etype (Var);
10830 Init_Proc : Entity_Id;
10831 -- Initialization procedure for Typ
10833 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
10834 -- Look for init call for Var starting at From and scanning the
10835 -- enclosing list until Rep_Clause or the end of the list is reached.
10837 ----------------------------
10838 -- Find_Init_Call_In_List --
10839 ----------------------------
10841 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
10842 Init_Call : Node_Id;
10844 begin
10845 Init_Call := From;
10846 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
10847 if Nkind (Init_Call) = N_Procedure_Call_Statement
10848 and then Is_Entity_Name (Name (Init_Call))
10849 and then Entity (Name (Init_Call)) = Init_Proc
10850 then
10851 return Init_Call;
10852 end if;
10854 Next (Init_Call);
10855 end loop;
10857 return Empty;
10858 end Find_Init_Call_In_List;
10860 Init_Call : Node_Id;
10862 -- Start of processing for Find_Init_Call
10864 begin
10865 if Present (Initialization_Statements (Var)) then
10866 Init_Call := Initialization_Statements (Var);
10867 Set_Initialization_Statements (Var, Empty);
10869 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
10871 -- No init proc for the type, so obviously no call to be found
10873 return Empty;
10875 else
10876 -- We might be able to handle other cases below by just properly
10877 -- setting Initialization_Statements at the point where the init proc
10878 -- call is generated???
10880 Init_Proc := Base_Init_Proc (Typ);
10882 -- First scan the list containing the declaration of Var
10884 Init_Call := Find_Init_Call_In_List (From => Next (Par));
10886 -- If not found, also look on Var's freeze actions list, if any,
10887 -- since the init call may have been moved there (case of an address
10888 -- clause applying to Var).
10890 if No (Init_Call) and then Present (Freeze_Node (Var)) then
10891 Init_Call :=
10892 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
10893 end if;
10895 -- If the initialization call has actuals that use the secondary
10896 -- stack, the call may have been wrapped into a temporary block, in
10897 -- which case the block itself has to be removed.
10899 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
10900 declare
10901 Blk : constant Node_Id := Next (Par);
10902 begin
10903 if Present
10904 (Find_Init_Call_In_List
10905 (First (Statements (Handled_Statement_Sequence (Blk)))))
10906 then
10907 Init_Call := Blk;
10908 end if;
10909 end;
10910 end if;
10911 end if;
10913 if Present (Init_Call) then
10914 Remove (Init_Call);
10915 end if;
10916 return Init_Call;
10917 end Remove_Init_Call;
10919 -------------------------
10920 -- Remove_Side_Effects --
10921 -------------------------
10923 procedure Remove_Side_Effects
10924 (Exp : Node_Id;
10925 Name_Req : Boolean := False;
10926 Renaming_Req : Boolean := False;
10927 Variable_Ref : Boolean := False;
10928 Related_Id : Entity_Id := Empty;
10929 Is_Low_Bound : Boolean := False;
10930 Is_High_Bound : Boolean := False;
10931 Check_Side_Effects : Boolean := True)
10933 function Build_Temporary
10934 (Loc : Source_Ptr;
10935 Id : Character;
10936 Related_Nod : Node_Id := Empty) return Entity_Id;
10937 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
10938 -- is present (xxx is taken from the Chars field of Related_Nod),
10939 -- otherwise it generates an internal temporary.
10941 ---------------------
10942 -- Build_Temporary --
10943 ---------------------
10945 function Build_Temporary
10946 (Loc : Source_Ptr;
10947 Id : Character;
10948 Related_Nod : Node_Id := Empty) return Entity_Id
10950 Temp_Nam : Name_Id;
10952 begin
10953 -- The context requires an external symbol
10955 if Present (Related_Id) then
10956 if Is_Low_Bound then
10957 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
10958 else pragma Assert (Is_High_Bound);
10959 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
10960 end if;
10962 return Make_Defining_Identifier (Loc, Temp_Nam);
10964 -- Otherwise generate an internal temporary
10966 else
10967 return Make_Temporary (Loc, Id, Related_Nod);
10968 end if;
10969 end Build_Temporary;
10971 -- Local variables
10973 Loc : constant Source_Ptr := Sloc (Exp);
10974 Exp_Type : constant Entity_Id := Etype (Exp);
10975 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
10976 Def_Id : Entity_Id;
10977 E : Node_Id;
10978 New_Exp : Node_Id;
10979 Ptr_Typ_Decl : Node_Id;
10980 Ref_Type : Entity_Id;
10981 Res : Node_Id;
10983 -- Start of processing for Remove_Side_Effects
10985 begin
10986 -- Handle cases in which there is nothing to do. In GNATprove mode,
10987 -- removal of side effects is useful for the light expansion of
10988 -- renamings. This removal should only occur when not inside a
10989 -- generic and not doing a pre-analysis.
10991 if not Expander_Active
10992 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
10993 then
10994 return;
10996 -- Cannot generate temporaries if the invocation to remove side effects
10997 -- was issued too early and the type of the expression is not resolved
10998 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
10999 -- Remove_Side_Effects).
11001 elsif No (Exp_Type)
11002 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11003 then
11004 return;
11006 -- Nothing to do if prior expansion determined that a function call does
11007 -- not require side effect removal.
11009 elsif Nkind (Exp) = N_Function_Call
11010 and then No_Side_Effect_Removal (Exp)
11011 then
11012 return;
11014 -- No action needed for side-effect free expressions
11016 elsif Check_Side_Effects
11017 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11018 then
11019 return;
11020 end if;
11022 -- The remaining processing is done with all checks suppressed
11024 -- Note: from now on, don't use return statements, instead do a goto
11025 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11027 Scope_Suppress.Suppress := (others => True);
11029 -- If this is an elementary or a small not by-reference record type, and
11030 -- we need to capture the value, just make a constant; this is cheap and
11031 -- objects of both kinds of types can be bit aligned, so it might not be
11032 -- possible to generate a reference to them. Likewise if this is not a
11033 -- name reference, except for a type conversion because we would enter
11034 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11035 -- type has predicates (and type conversions need a specific treatment
11036 -- anyway, see below). Also do it if we have a volatile reference and
11037 -- Name_Req is not set (see comments for Side_Effect_Free).
11039 if (Is_Elementary_Type (Exp_Type)
11040 or else (Is_Record_Type (Exp_Type)
11041 and then Known_Static_RM_Size (Exp_Type)
11042 and then RM_Size (Exp_Type) <= 64
11043 and then not Has_Discriminants (Exp_Type)
11044 and then not Is_By_Reference_Type (Exp_Type)))
11045 and then (Variable_Ref
11046 or else (not Is_Name_Reference (Exp)
11047 and then Nkind (Exp) /= N_Type_Conversion)
11048 or else (not Name_Req
11049 and then Is_Volatile_Reference (Exp)))
11050 then
11051 Def_Id := Build_Temporary (Loc, 'R', Exp);
11052 Set_Etype (Def_Id, Exp_Type);
11053 Res := New_Occurrence_Of (Def_Id, Loc);
11055 -- If the expression is a packed reference, it must be reanalyzed and
11056 -- expanded, depending on context. This is the case for actuals where
11057 -- a constraint check may capture the actual before expansion of the
11058 -- call is complete.
11060 if Nkind (Exp) = N_Indexed_Component
11061 and then Is_Packed (Etype (Prefix (Exp)))
11062 then
11063 Set_Analyzed (Exp, False);
11064 Set_Analyzed (Prefix (Exp), False);
11065 end if;
11067 -- Generate:
11068 -- Rnn : Exp_Type renames Expr;
11070 if Renaming_Req then
11071 E :=
11072 Make_Object_Renaming_Declaration (Loc,
11073 Defining_Identifier => Def_Id,
11074 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11075 Name => Relocate_Node (Exp));
11077 -- Generate:
11078 -- Rnn : constant Exp_Type := Expr;
11080 else
11081 E :=
11082 Make_Object_Declaration (Loc,
11083 Defining_Identifier => Def_Id,
11084 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11085 Constant_Present => True,
11086 Expression => Relocate_Node (Exp));
11088 Set_Assignment_OK (E);
11089 end if;
11091 Insert_Action (Exp, E);
11093 -- If the expression has the form v.all then we can just capture the
11094 -- pointer, and then do an explicit dereference on the result, but
11095 -- this is not right if this is a volatile reference.
11097 elsif Nkind (Exp) = N_Explicit_Dereference
11098 and then not Is_Volatile_Reference (Exp)
11099 then
11100 Def_Id := Build_Temporary (Loc, 'R', Exp);
11101 Res :=
11102 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11104 Insert_Action (Exp,
11105 Make_Object_Declaration (Loc,
11106 Defining_Identifier => Def_Id,
11107 Object_Definition =>
11108 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11109 Constant_Present => True,
11110 Expression => Relocate_Node (Prefix (Exp))));
11112 -- Similar processing for an unchecked conversion of an expression of
11113 -- the form v.all, where we want the same kind of treatment.
11115 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11116 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11117 then
11118 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11119 goto Leave;
11121 -- If this is a type conversion, leave the type conversion and remove
11122 -- the side effects in the expression. This is important in several
11123 -- circumstances: for change of representations, and also when this is a
11124 -- view conversion to a smaller object, where gigi can end up creating
11125 -- its own temporary of the wrong size.
11127 elsif Nkind (Exp) = N_Type_Conversion then
11128 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11130 -- Generating C code the type conversion of an access to constrained
11131 -- array type into an access to unconstrained array type involves
11132 -- initializing a fat pointer and the expression must be free of
11133 -- side effects to safely compute its bounds.
11135 if Modify_Tree_For_C
11136 and then Is_Access_Type (Etype (Exp))
11137 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11138 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11139 then
11140 Def_Id := Build_Temporary (Loc, 'R', Exp);
11141 Set_Etype (Def_Id, Exp_Type);
11142 Res := New_Occurrence_Of (Def_Id, Loc);
11144 Insert_Action (Exp,
11145 Make_Object_Declaration (Loc,
11146 Defining_Identifier => Def_Id,
11147 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11148 Constant_Present => True,
11149 Expression => Relocate_Node (Exp)));
11150 else
11151 goto Leave;
11152 end if;
11154 -- If this is an unchecked conversion that Gigi can't handle, make
11155 -- a copy or a use a renaming to capture the value.
11157 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11158 and then not Safe_Unchecked_Type_Conversion (Exp)
11159 then
11160 if CW_Or_Has_Controlled_Part (Exp_Type) then
11162 -- Use a renaming to capture the expression, rather than create
11163 -- a controlled temporary.
11165 Def_Id := Build_Temporary (Loc, 'R', Exp);
11166 Res := New_Occurrence_Of (Def_Id, Loc);
11168 Insert_Action (Exp,
11169 Make_Object_Renaming_Declaration (Loc,
11170 Defining_Identifier => Def_Id,
11171 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11172 Name => Relocate_Node (Exp)));
11174 else
11175 Def_Id := Build_Temporary (Loc, 'R', Exp);
11176 Set_Etype (Def_Id, Exp_Type);
11177 Res := New_Occurrence_Of (Def_Id, Loc);
11179 E :=
11180 Make_Object_Declaration (Loc,
11181 Defining_Identifier => Def_Id,
11182 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11183 Constant_Present => not Is_Variable (Exp),
11184 Expression => Relocate_Node (Exp));
11186 Set_Assignment_OK (E);
11187 Insert_Action (Exp, E);
11188 end if;
11190 -- For expressions that denote names, we can use a renaming scheme.
11191 -- This is needed for correctness in the case of a volatile object of
11192 -- a non-volatile type because the Make_Reference call of the "default"
11193 -- approach would generate an illegal access value (an access value
11194 -- cannot designate such an object - see Analyze_Reference).
11196 elsif Is_Name_Reference (Exp)
11198 -- We skip using this scheme if we have an object of a volatile
11199 -- type and we do not have Name_Req set true (see comments for
11200 -- Side_Effect_Free).
11202 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
11203 then
11204 Def_Id := Build_Temporary (Loc, 'R', Exp);
11205 Res := New_Occurrence_Of (Def_Id, Loc);
11207 Insert_Action (Exp,
11208 Make_Object_Renaming_Declaration (Loc,
11209 Defining_Identifier => Def_Id,
11210 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11211 Name => Relocate_Node (Exp)));
11213 -- If this is a packed reference, or a selected component with
11214 -- a non-standard representation, a reference to the temporary
11215 -- will be replaced by a copy of the original expression (see
11216 -- Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
11217 -- elaborated by gigi, and is of course not to be replaced in-line
11218 -- by the expression it renames, which would defeat the purpose of
11219 -- removing the side-effect.
11221 if Nkind_In (Exp, N_Selected_Component, N_Indexed_Component)
11222 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11223 then
11224 null;
11225 else
11226 Set_Is_Renaming_Of_Object (Def_Id, False);
11227 end if;
11229 -- Avoid generating a variable-sized temporary, by generating the
11230 -- reference just for the function call. The transformation could be
11231 -- refined to apply only when the array component is constrained by a
11232 -- discriminant???
11234 elsif Nkind (Exp) = N_Selected_Component
11235 and then Nkind (Prefix (Exp)) = N_Function_Call
11236 and then Is_Array_Type (Exp_Type)
11237 then
11238 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11239 goto Leave;
11241 -- Otherwise we generate a reference to the expression
11243 else
11244 -- An expression which is in SPARK mode is considered side effect
11245 -- free if the resulting value is captured by a variable or a
11246 -- constant.
11248 if GNATprove_Mode
11249 and then Nkind (Parent (Exp)) = N_Object_Declaration
11250 then
11251 goto Leave;
11253 -- When generating C code we cannot consider side effect free object
11254 -- declarations that have discriminants and are initialized by means
11255 -- of a function call since on this target there is no secondary
11256 -- stack to store the return value and the expander may generate an
11257 -- extra call to the function to compute the discriminant value. In
11258 -- addition, for targets that have secondary stack, the expansion of
11259 -- functions with side effects involves the generation of an access
11260 -- type to capture the return value stored in the secondary stack;
11261 -- by contrast when generating C code such expansion generates an
11262 -- internal object declaration (no access type involved) which must
11263 -- be identified here to avoid entering into a never-ending loop
11264 -- generating internal object declarations.
11266 elsif Modify_Tree_For_C
11267 and then Nkind (Parent (Exp)) = N_Object_Declaration
11268 and then
11269 (Nkind (Exp) /= N_Function_Call
11270 or else not Has_Discriminants (Exp_Type)
11271 or else Is_Internal_Name
11272 (Chars (Defining_Identifier (Parent (Exp)))))
11273 then
11274 goto Leave;
11275 end if;
11277 -- Special processing for function calls that return a limited type.
11278 -- We need to build a declaration that will enable build-in-place
11279 -- expansion of the call. This is not done if the context is already
11280 -- an object declaration, to prevent infinite recursion.
11282 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11283 -- to accommodate functions returning limited objects by reference.
11285 if Ada_Version >= Ada_2005
11286 and then Nkind (Exp) = N_Function_Call
11287 and then Is_Limited_View (Etype (Exp))
11288 and then Nkind (Parent (Exp)) /= N_Object_Declaration
11289 then
11290 declare
11291 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
11292 Decl : Node_Id;
11294 begin
11295 Decl :=
11296 Make_Object_Declaration (Loc,
11297 Defining_Identifier => Obj,
11298 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11299 Expression => Relocate_Node (Exp));
11301 Insert_Action (Exp, Decl);
11302 Set_Etype (Obj, Exp_Type);
11303 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
11304 goto Leave;
11305 end;
11306 end if;
11308 Def_Id := Build_Temporary (Loc, 'R', Exp);
11310 -- The regular expansion of functions with side effects involves the
11311 -- generation of an access type to capture the return value found on
11312 -- the secondary stack. Since SPARK (and why) cannot process access
11313 -- types, use a different approach which ignores the secondary stack
11314 -- and "copies" the returned object.
11315 -- When generating C code, no need for a 'reference since the
11316 -- secondary stack is not supported.
11318 if GNATprove_Mode or Modify_Tree_For_C then
11319 Res := New_Occurrence_Of (Def_Id, Loc);
11320 Ref_Type := Exp_Type;
11322 -- Regular expansion utilizing an access type and 'reference
11324 else
11325 Res :=
11326 Make_Explicit_Dereference (Loc,
11327 Prefix => New_Occurrence_Of (Def_Id, Loc));
11329 -- Generate:
11330 -- type Ann is access all <Exp_Type>;
11332 Ref_Type := Make_Temporary (Loc, 'A');
11334 Ptr_Typ_Decl :=
11335 Make_Full_Type_Declaration (Loc,
11336 Defining_Identifier => Ref_Type,
11337 Type_Definition =>
11338 Make_Access_To_Object_Definition (Loc,
11339 All_Present => True,
11340 Subtype_Indication =>
11341 New_Occurrence_Of (Exp_Type, Loc)));
11343 Insert_Action (Exp, Ptr_Typ_Decl);
11344 end if;
11346 E := Exp;
11347 if Nkind (E) = N_Explicit_Dereference then
11348 New_Exp := Relocate_Node (Prefix (E));
11350 else
11351 E := Relocate_Node (E);
11353 -- Do not generate a 'reference in SPARK mode or C generation
11354 -- since the access type is not created in the first place.
11356 if GNATprove_Mode or Modify_Tree_For_C then
11357 New_Exp := E;
11359 -- Otherwise generate reference, marking the value as non-null
11360 -- since we know it cannot be null and we don't want a check.
11362 else
11363 New_Exp := Make_Reference (Loc, E);
11364 Set_Is_Known_Non_Null (Def_Id);
11365 end if;
11366 end if;
11368 if Is_Delayed_Aggregate (E) then
11370 -- The expansion of nested aggregates is delayed until the
11371 -- enclosing aggregate is expanded. As aggregates are often
11372 -- qualified, the predicate applies to qualified expressions as
11373 -- well, indicating that the enclosing aggregate has not been
11374 -- expanded yet. At this point the aggregate is part of a
11375 -- stand-alone declaration, and must be fully expanded.
11377 if Nkind (E) = N_Qualified_Expression then
11378 Set_Expansion_Delayed (Expression (E), False);
11379 Set_Analyzed (Expression (E), False);
11380 else
11381 Set_Expansion_Delayed (E, False);
11382 end if;
11384 Set_Analyzed (E, False);
11385 end if;
11387 -- Generating C code of object declarations that have discriminants
11388 -- and are initialized by means of a function call we propagate the
11389 -- discriminants of the parent type to the internally built object.
11390 -- This is needed to avoid generating an extra call to the called
11391 -- function.
11393 -- For example, if we generate here the following declaration, it
11394 -- will be expanded later adding an extra call to evaluate the value
11395 -- of the discriminant (needed to compute the size of the object).
11397 -- type Rec (D : Integer) is ...
11398 -- Obj : constant Rec := SomeFunc;
11400 if Modify_Tree_For_C
11401 and then Nkind (Parent (Exp)) = N_Object_Declaration
11402 and then Has_Discriminants (Exp_Type)
11403 and then Nkind (Exp) = N_Function_Call
11404 then
11405 Insert_Action (Exp,
11406 Make_Object_Declaration (Loc,
11407 Defining_Identifier => Def_Id,
11408 Object_Definition => New_Copy_Tree
11409 (Object_Definition (Parent (Exp))),
11410 Constant_Present => True,
11411 Expression => New_Exp));
11412 else
11413 Insert_Action (Exp,
11414 Make_Object_Declaration (Loc,
11415 Defining_Identifier => Def_Id,
11416 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
11417 Constant_Present => True,
11418 Expression => New_Exp));
11419 end if;
11420 end if;
11422 -- Preserve the Assignment_OK flag in all copies, since at least one
11423 -- copy may be used in a context where this flag must be set (otherwise
11424 -- why would the flag be set in the first place).
11426 Set_Assignment_OK (Res, Assignment_OK (Exp));
11428 -- Finally rewrite the original expression and we are done
11430 Rewrite (Exp, Res);
11431 Analyze_And_Resolve (Exp, Exp_Type);
11433 <<Leave>>
11434 Scope_Suppress := Svg_Suppress;
11435 end Remove_Side_Effects;
11437 ------------------------
11438 -- Replace_References --
11439 ------------------------
11441 procedure Replace_References
11442 (Expr : Node_Id;
11443 Par_Typ : Entity_Id;
11444 Deriv_Typ : Entity_Id;
11445 Par_Obj : Entity_Id := Empty;
11446 Deriv_Obj : Entity_Id := Empty)
11448 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
11449 -- Determine whether node Ref denotes some component of Deriv_Obj
11451 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
11452 -- Substitute a reference to an entity with the corresponding value
11453 -- stored in table Type_Map.
11455 function Type_Of_Formal
11456 (Call : Node_Id;
11457 Actual : Node_Id) return Entity_Id;
11458 -- Find the type of the formal parameter which corresponds to actual
11459 -- parameter Actual in subprogram call Call.
11461 ----------------------
11462 -- Is_Deriv_Obj_Ref --
11463 ----------------------
11465 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
11466 Par : constant Node_Id := Parent (Ref);
11468 begin
11469 -- Detect the folowing selected component form:
11471 -- Deriv_Obj.(something)
11473 return
11474 Nkind (Par) = N_Selected_Component
11475 and then Is_Entity_Name (Prefix (Par))
11476 and then Entity (Prefix (Par)) = Deriv_Obj;
11477 end Is_Deriv_Obj_Ref;
11479 -----------------
11480 -- Replace_Ref --
11481 -----------------
11483 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
11484 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
11485 -- Reset the Controlling_Argument of all function calls that
11486 -- encapsulate node From_Arg.
11488 ----------------------------------
11489 -- Remove_Controlling_Arguments --
11490 ----------------------------------
11492 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
11493 Par : Node_Id;
11495 begin
11496 Par := From_Arg;
11497 while Present (Par) loop
11498 if Nkind (Par) = N_Function_Call
11499 and then Present (Controlling_Argument (Par))
11500 then
11501 Set_Controlling_Argument (Par, Empty);
11503 -- Prevent the search from going too far
11505 elsif Is_Body_Or_Package_Declaration (Par) then
11506 exit;
11507 end if;
11509 Par := Parent (Par);
11510 end loop;
11511 end Remove_Controlling_Arguments;
11513 -- Local variables
11515 Context : constant Node_Id := Parent (Ref);
11516 Loc : constant Source_Ptr := Sloc (Ref);
11517 Ref_Id : Entity_Id;
11518 Result : Traverse_Result;
11520 New_Ref : Node_Id;
11521 -- The new reference which is intended to substitute the old one
11523 Old_Ref : Node_Id;
11524 -- The reference designated for replacement. In certain cases this
11525 -- may be a node other than Ref.
11527 Val : Node_Or_Entity_Id;
11528 -- The corresponding value of Ref from the type map
11530 -- Start of processing for Replace_Ref
11532 begin
11533 -- Assume that the input reference is to be replaced and that the
11534 -- traversal should examine the children of the reference.
11536 Old_Ref := Ref;
11537 Result := OK;
11539 -- The input denotes a meaningful reference
11541 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
11542 Ref_Id := Entity (Ref);
11543 Val := Type_Map.Get (Ref_Id);
11545 -- The reference has a corresponding value in the type map, a
11546 -- substitution is possible.
11548 if Present (Val) then
11550 -- The reference denotes a discriminant
11552 if Ekind (Ref_Id) = E_Discriminant then
11553 if Nkind (Val) in N_Entity then
11555 -- The value denotes another discriminant. Replace as
11556 -- follows:
11558 -- _object.Discr -> _object.Val
11560 if Ekind (Val) = E_Discriminant then
11561 New_Ref := New_Occurrence_Of (Val, Loc);
11563 -- Otherwise the value denotes the entity of a name which
11564 -- constraints the discriminant. Replace as follows:
11566 -- _object.Discr -> Val
11568 else
11569 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11571 New_Ref := New_Occurrence_Of (Val, Loc);
11572 Old_Ref := Parent (Old_Ref);
11573 end if;
11575 -- Otherwise the value denotes an arbitrary expression which
11576 -- constraints the discriminant. Replace as follows:
11578 -- _object.Discr -> Val
11580 else
11581 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
11583 New_Ref := New_Copy_Tree (Val);
11584 Old_Ref := Parent (Old_Ref);
11585 end if;
11587 -- Otherwise the reference denotes a primitive. Replace as
11588 -- follows:
11590 -- Primitive -> Val
11592 else
11593 pragma Assert (Nkind (Val) in N_Entity);
11594 New_Ref := New_Occurrence_Of (Val, Loc);
11595 end if;
11597 -- The reference mentions the _object parameter of the parent
11598 -- type's DIC or type invariant procedure. Replace as follows:
11600 -- _object -> _object
11602 elsif Present (Par_Obj)
11603 and then Present (Deriv_Obj)
11604 and then Ref_Id = Par_Obj
11605 then
11606 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
11608 -- The type of the _object parameter is class-wide when the
11609 -- expression comes from an assertion pragma that applies to
11610 -- an abstract parent type or an interface. The class-wide type
11611 -- facilitates the preanalysis of the expression by treating
11612 -- calls to abstract primitives that mention the current
11613 -- instance of the type as dispatching. Once the calls are
11614 -- remapped to invoke overriding or inherited primitives, the
11615 -- calls no longer need to be dispatching. Examine all function
11616 -- calls that encapsulate the _object parameter and reset their
11617 -- Controlling_Argument attribute.
11619 if Is_Class_Wide_Type (Etype (Par_Obj))
11620 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
11621 then
11622 Remove_Controlling_Arguments (Old_Ref);
11623 end if;
11625 -- The reference to _object acts as an actual parameter in a
11626 -- subprogram call which may be invoking a primitive of the
11627 -- parent type:
11629 -- Primitive (... _object ...);
11631 -- The parent type primitive may not be overridden nor
11632 -- inherited when it is declared after the derived type
11633 -- definition:
11635 -- type Parent is tagged private;
11636 -- type Child is new Parent with private;
11637 -- procedure Primitive (Obj : Parent);
11639 -- In this scenario the _object parameter is converted to the
11640 -- parent type. Due to complications with partial/full views
11641 -- and view swaps, the parent type is taken from the formal
11642 -- parameter of the subprogram being called.
11644 if Nkind_In (Context, N_Function_Call,
11645 N_Procedure_Call_Statement)
11646 and then No (Type_Map.Get (Entity (Name (Context))))
11647 then
11648 New_Ref :=
11649 Convert_To (Type_Of_Formal (Context, Old_Ref), New_Ref);
11651 -- Do not process the generated type conversion because
11652 -- both the parent type and the derived type are in the
11653 -- Type_Map table. This will clobber the type conversion
11654 -- by resetting its subtype mark.
11656 Result := Skip;
11657 end if;
11659 -- Otherwise there is nothing to replace
11661 else
11662 New_Ref := Empty;
11663 end if;
11665 if Present (New_Ref) then
11666 Rewrite (Old_Ref, New_Ref);
11668 -- Update the return type when the context of the reference
11669 -- acts as the name of a function call. Note that the update
11670 -- should not be performed when the reference appears as an
11671 -- actual in the call.
11673 if Nkind (Context) = N_Function_Call
11674 and then Name (Context) = Old_Ref
11675 then
11676 Set_Etype (Context, Etype (Val));
11677 end if;
11678 end if;
11679 end if;
11681 -- Reanalyze the reference due to potential replacements
11683 if Nkind (Old_Ref) in N_Has_Etype then
11684 Set_Analyzed (Old_Ref, False);
11685 end if;
11687 return Result;
11688 end Replace_Ref;
11690 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
11692 --------------------
11693 -- Type_Of_Formal --
11694 --------------------
11696 function Type_Of_Formal
11697 (Call : Node_Id;
11698 Actual : Node_Id) return Entity_Id
11700 A : Node_Id;
11701 F : Entity_Id;
11703 begin
11704 -- Examine the list of actual and formal parameters in parallel
11706 A := First (Parameter_Associations (Call));
11707 F := First_Formal (Entity (Name (Call)));
11708 while Present (A) and then Present (F) loop
11709 if A = Actual then
11710 return Etype (F);
11711 end if;
11713 Next (A);
11714 Next_Formal (F);
11715 end loop;
11717 -- The actual parameter must always have a corresponding formal
11719 pragma Assert (False);
11721 return Empty;
11722 end Type_Of_Formal;
11724 -- Start of processing for Replace_References
11726 begin
11727 -- Map the attributes of the parent type to the proper corresponding
11728 -- attributes of the derived type.
11730 Map_Types
11731 (Parent_Type => Par_Typ,
11732 Derived_Type => Deriv_Typ);
11734 -- Inspect the input expression and perform substitutions where
11735 -- necessary.
11737 Replace_Refs (Expr);
11738 end Replace_References;
11740 -----------------------------
11741 -- Replace_Type_References --
11742 -----------------------------
11744 procedure Replace_Type_References
11745 (Expr : Node_Id;
11746 Typ : Entity_Id;
11747 Obj_Id : Entity_Id)
11749 procedure Replace_Type_Ref (N : Node_Id);
11750 -- Substitute a single reference of the current instance of type Typ
11751 -- with a reference to Obj_Id.
11753 ----------------------
11754 -- Replace_Type_Ref --
11755 ----------------------
11757 procedure Replace_Type_Ref (N : Node_Id) is
11758 begin
11759 -- Decorate the reference to Typ even though it may be rewritten
11760 -- further down. This is done for two reasons:
11762 -- * ASIS has all necessary semantic information in the original
11763 -- tree.
11765 -- * Routines which examine properties of the Original_Node have
11766 -- some semantic information.
11768 if Nkind (N) = N_Identifier then
11769 Set_Entity (N, Typ);
11770 Set_Etype (N, Typ);
11772 elsif Nkind (N) = N_Selected_Component then
11773 Analyze (Prefix (N));
11774 Set_Entity (Selector_Name (N), Typ);
11775 Set_Etype (Selector_Name (N), Typ);
11776 end if;
11778 -- Perform the following substitution:
11780 -- Typ --> _object
11782 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
11783 Set_Comes_From_Source (N, True);
11784 end Replace_Type_Ref;
11786 procedure Replace_Type_Refs is
11787 new Replace_Type_References_Generic (Replace_Type_Ref);
11789 -- Start of processing for Replace_Type_References
11791 begin
11792 Replace_Type_Refs (Expr, Typ);
11793 end Replace_Type_References;
11795 ---------------------------
11796 -- Represented_As_Scalar --
11797 ---------------------------
11799 function Represented_As_Scalar (T : Entity_Id) return Boolean is
11800 UT : constant Entity_Id := Underlying_Type (T);
11801 begin
11802 return Is_Scalar_Type (UT)
11803 or else (Is_Bit_Packed_Array (UT)
11804 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
11805 end Represented_As_Scalar;
11807 ------------------------------
11808 -- Requires_Cleanup_Actions --
11809 ------------------------------
11811 function Requires_Cleanup_Actions
11812 (N : Node_Id;
11813 Lib_Level : Boolean) return Boolean
11815 At_Lib_Level : constant Boolean :=
11816 Lib_Level
11817 and then Nkind_In (N, N_Package_Body,
11818 N_Package_Specification);
11819 -- N is at the library level if the top-most context is a package and
11820 -- the path taken to reach N does not inlcude non-package constructs.
11822 begin
11823 case Nkind (N) is
11824 when N_Accept_Statement
11825 | N_Block_Statement
11826 | N_Entry_Body
11827 | N_Package_Body
11828 | N_Protected_Body
11829 | N_Subprogram_Body
11830 | N_Task_Body
11832 return
11833 Requires_Cleanup_Actions (Declarations (N), At_Lib_Level, True)
11834 or else
11835 (Present (Handled_Statement_Sequence (N))
11836 and then
11837 Requires_Cleanup_Actions
11838 (Statements (Handled_Statement_Sequence (N)),
11839 At_Lib_Level, True));
11841 when N_Package_Specification =>
11842 return
11843 Requires_Cleanup_Actions
11844 (Visible_Declarations (N), At_Lib_Level, True)
11845 or else
11846 Requires_Cleanup_Actions
11847 (Private_Declarations (N), At_Lib_Level, True);
11849 when others =>
11850 return False;
11851 end case;
11852 end Requires_Cleanup_Actions;
11854 ------------------------------
11855 -- Requires_Cleanup_Actions --
11856 ------------------------------
11858 function Requires_Cleanup_Actions
11859 (L : List_Id;
11860 Lib_Level : Boolean;
11861 Nested_Constructs : Boolean) return Boolean
11863 Decl : Node_Id;
11864 Expr : Node_Id;
11865 Obj_Id : Entity_Id;
11866 Obj_Typ : Entity_Id;
11867 Pack_Id : Entity_Id;
11868 Typ : Entity_Id;
11870 begin
11871 if No (L)
11872 or else Is_Empty_List (L)
11873 then
11874 return False;
11875 end if;
11877 Decl := First (L);
11878 while Present (Decl) loop
11880 -- Library-level tagged types
11882 if Nkind (Decl) = N_Full_Type_Declaration then
11883 Typ := Defining_Identifier (Decl);
11885 -- Ignored Ghost types do not need any cleanup actions because
11886 -- they will not appear in the final tree.
11888 if Is_Ignored_Ghost_Entity (Typ) then
11889 null;
11891 elsif Is_Tagged_Type (Typ)
11892 and then Is_Library_Level_Entity (Typ)
11893 and then Convention (Typ) = Convention_Ada
11894 and then Present (Access_Disp_Table (Typ))
11895 and then RTE_Available (RE_Unregister_Tag)
11896 and then not Is_Abstract_Type (Typ)
11897 and then not No_Run_Time_Mode
11898 then
11899 return True;
11900 end if;
11902 -- Regular object declarations
11904 elsif Nkind (Decl) = N_Object_Declaration then
11905 Obj_Id := Defining_Identifier (Decl);
11906 Obj_Typ := Base_Type (Etype (Obj_Id));
11907 Expr := Expression (Decl);
11909 -- Bypass any form of processing for objects which have their
11910 -- finalization disabled. This applies only to objects at the
11911 -- library level.
11913 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
11914 null;
11916 -- Finalization of transient objects are treated separately in
11917 -- order to handle sensitive cases. These include:
11919 -- * Aggregate expansion
11920 -- * If, case, and expression with actions expansion
11921 -- * Transient scopes
11923 -- If one of those contexts has marked the transient object as
11924 -- ignored, do not generate finalization actions for it.
11926 elsif Is_Finalized_Transient (Obj_Id)
11927 or else Is_Ignored_Transient (Obj_Id)
11928 then
11929 null;
11931 -- Ignored Ghost objects do not need any cleanup actions because
11932 -- they will not appear in the final tree.
11934 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
11935 null;
11937 -- The object is of the form:
11938 -- Obj : [constant] Typ [:= Expr];
11940 -- Do not process tag-to-class-wide conversions because they do
11941 -- not yield an object. Do not process the incomplete view of a
11942 -- deferred constant. Note that an object initialized by means
11943 -- of a build-in-place function call may appear as a deferred
11944 -- constant after expansion activities. These kinds of objects
11945 -- must be finalized.
11947 elsif not Is_Imported (Obj_Id)
11948 and then Needs_Finalization (Obj_Typ)
11949 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
11950 and then not (Ekind (Obj_Id) = E_Constant
11951 and then not Has_Completion (Obj_Id)
11952 and then No (BIP_Initialization_Call (Obj_Id)))
11953 then
11954 return True;
11956 -- The object is of the form:
11957 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
11959 -- Obj : Access_Typ :=
11960 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
11962 elsif Is_Access_Type (Obj_Typ)
11963 and then Needs_Finalization
11964 (Available_View (Designated_Type (Obj_Typ)))
11965 and then Present (Expr)
11966 and then
11967 (Is_Secondary_Stack_BIP_Func_Call (Expr)
11968 or else
11969 (Is_Non_BIP_Func_Call (Expr)
11970 and then not Is_Related_To_Func_Return (Obj_Id)))
11971 then
11972 return True;
11974 -- Processing for "hook" objects generated for transient objects
11975 -- declared inside an Expression_With_Actions.
11977 elsif Is_Access_Type (Obj_Typ)
11978 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
11979 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
11980 N_Object_Declaration
11981 then
11982 return True;
11984 -- Processing for intermediate results of if expressions where
11985 -- one of the alternatives uses a controlled function call.
11987 elsif Is_Access_Type (Obj_Typ)
11988 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
11989 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
11990 N_Defining_Identifier
11991 and then Present (Expr)
11992 and then Nkind (Expr) = N_Null
11993 then
11994 return True;
11996 -- Simple protected objects which use type System.Tasking.
11997 -- Protected_Objects.Protection to manage their locks should be
11998 -- treated as controlled since they require manual cleanup.
12000 elsif Ekind (Obj_Id) = E_Variable
12001 and then (Is_Simple_Protected_Type (Obj_Typ)
12002 or else Has_Simple_Protected_Object (Obj_Typ))
12003 then
12004 return True;
12005 end if;
12007 -- Specific cases of object renamings
12009 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12010 Obj_Id := Defining_Identifier (Decl);
12011 Obj_Typ := Base_Type (Etype (Obj_Id));
12013 -- Bypass any form of processing for objects which have their
12014 -- finalization disabled. This applies only to objects at the
12015 -- library level.
12017 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12018 null;
12020 -- Ignored Ghost object renamings do not need any cleanup actions
12021 -- because they will not appear in the final tree.
12023 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12024 null;
12026 -- Return object of a build-in-place function. This case is
12027 -- recognized and marked by the expansion of an extended return
12028 -- statement (see Expand_N_Extended_Return_Statement).
12030 elsif Needs_Finalization (Obj_Typ)
12031 and then Is_Return_Object (Obj_Id)
12032 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12033 then
12034 return True;
12036 -- Detect a case where a source object has been initialized by
12037 -- a controlled function call or another object which was later
12038 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12040 -- Obj1 : CW_Type := Src_Obj;
12041 -- Obj2 : CW_Type := Function_Call (...);
12043 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12044 -- Tmp : ... := Function_Call (...)'reference;
12045 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12047 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12048 return True;
12049 end if;
12051 -- Inspect the freeze node of an access-to-controlled type and look
12052 -- for a delayed finalization master. This case arises when the
12053 -- freeze actions are inserted at a later time than the expansion of
12054 -- the context. Since Build_Finalizer is never called on a single
12055 -- construct twice, the master will be ultimately left out and never
12056 -- finalized. This is also needed for freeze actions of designated
12057 -- types themselves, since in some cases the finalization master is
12058 -- associated with a designated type's freeze node rather than that
12059 -- of the access type (see handling for freeze actions in
12060 -- Build_Finalization_Master).
12062 elsif Nkind (Decl) = N_Freeze_Entity
12063 and then Present (Actions (Decl))
12064 then
12065 Typ := Entity (Decl);
12067 -- Freeze nodes for ignored Ghost types do not need cleanup
12068 -- actions because they will never appear in the final tree.
12070 if Is_Ignored_Ghost_Entity (Typ) then
12071 null;
12073 elsif ((Is_Access_Type (Typ)
12074 and then not Is_Access_Subprogram_Type (Typ)
12075 and then Needs_Finalization
12076 (Available_View (Designated_Type (Typ))))
12077 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12078 and then Requires_Cleanup_Actions
12079 (Actions (Decl), Lib_Level, Nested_Constructs)
12080 then
12081 return True;
12082 end if;
12084 -- Nested package declarations
12086 elsif Nested_Constructs
12087 and then Nkind (Decl) = N_Package_Declaration
12088 then
12089 Pack_Id := Defining_Entity (Decl);
12091 -- Do not inspect an ignored Ghost package because all code found
12092 -- within will not appear in the final tree.
12094 if Is_Ignored_Ghost_Entity (Pack_Id) then
12095 null;
12097 elsif Ekind (Pack_Id) /= E_Generic_Package
12098 and then Requires_Cleanup_Actions
12099 (Specification (Decl), Lib_Level)
12100 then
12101 return True;
12102 end if;
12104 -- Nested package bodies
12106 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12108 -- Do not inspect an ignored Ghost package body because all code
12109 -- found within will not appear in the final tree.
12111 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12112 null;
12114 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12115 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12116 then
12117 return True;
12118 end if;
12120 elsif Nkind (Decl) = N_Block_Statement
12121 and then
12123 -- Handle a rare case caused by a controlled transient object
12124 -- created as part of a record init proc. The variable is wrapped
12125 -- in a block, but the block is not associated with a transient
12126 -- scope.
12128 (Inside_Init_Proc
12130 -- Handle the case where the original context has been wrapped in
12131 -- a block to avoid interference between exception handlers and
12132 -- At_End handlers. Treat the block as transparent and process its
12133 -- contents.
12135 or else Is_Finalization_Wrapper (Decl))
12136 then
12137 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12138 return True;
12139 end if;
12140 end if;
12142 Next (Decl);
12143 end loop;
12145 return False;
12146 end Requires_Cleanup_Actions;
12148 ------------------------------------
12149 -- Safe_Unchecked_Type_Conversion --
12150 ------------------------------------
12152 -- Note: this function knows quite a bit about the exact requirements of
12153 -- Gigi with respect to unchecked type conversions, and its code must be
12154 -- coordinated with any changes in Gigi in this area.
12156 -- The above requirements should be documented in Sinfo ???
12158 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12159 Otyp : Entity_Id;
12160 Ityp : Entity_Id;
12161 Oalign : Uint;
12162 Ialign : Uint;
12163 Pexp : constant Node_Id := Parent (Exp);
12165 begin
12166 -- If the expression is the RHS of an assignment or object declaration
12167 -- we are always OK because there will always be a target.
12169 -- Object renaming declarations, (generated for view conversions of
12170 -- actuals in inlined calls), like object declarations, provide an
12171 -- explicit type, and are safe as well.
12173 if (Nkind (Pexp) = N_Assignment_Statement
12174 and then Expression (Pexp) = Exp)
12175 or else Nkind_In (Pexp, N_Object_Declaration,
12176 N_Object_Renaming_Declaration)
12177 then
12178 return True;
12180 -- If the expression is the prefix of an N_Selected_Component we should
12181 -- also be OK because GCC knows to look inside the conversion except if
12182 -- the type is discriminated. We assume that we are OK anyway if the
12183 -- type is not set yet or if it is controlled since we can't afford to
12184 -- introduce a temporary in this case.
12186 elsif Nkind (Pexp) = N_Selected_Component
12187 and then Prefix (Pexp) = Exp
12188 then
12189 if No (Etype (Pexp)) then
12190 return True;
12191 else
12192 return
12193 not Has_Discriminants (Etype (Pexp))
12194 or else Is_Constrained (Etype (Pexp));
12195 end if;
12196 end if;
12198 -- Set the output type, this comes from Etype if it is set, otherwise we
12199 -- take it from the subtype mark, which we assume was already fully
12200 -- analyzed.
12202 if Present (Etype (Exp)) then
12203 Otyp := Etype (Exp);
12204 else
12205 Otyp := Entity (Subtype_Mark (Exp));
12206 end if;
12208 -- The input type always comes from the expression, and we assume this
12209 -- is indeed always analyzed, so we can simply get the Etype.
12211 Ityp := Etype (Expression (Exp));
12213 -- Initialize alignments to unknown so far
12215 Oalign := No_Uint;
12216 Ialign := No_Uint;
12218 -- Replace a concurrent type by its corresponding record type and each
12219 -- type by its underlying type and do the tests on those. The original
12220 -- type may be a private type whose completion is a concurrent type, so
12221 -- find the underlying type first.
12223 if Present (Underlying_Type (Otyp)) then
12224 Otyp := Underlying_Type (Otyp);
12225 end if;
12227 if Present (Underlying_Type (Ityp)) then
12228 Ityp := Underlying_Type (Ityp);
12229 end if;
12231 if Is_Concurrent_Type (Otyp) then
12232 Otyp := Corresponding_Record_Type (Otyp);
12233 end if;
12235 if Is_Concurrent_Type (Ityp) then
12236 Ityp := Corresponding_Record_Type (Ityp);
12237 end if;
12239 -- If the base types are the same, we know there is no problem since
12240 -- this conversion will be a noop.
12242 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12243 return True;
12245 -- Same if this is an upwards conversion of an untagged type, and there
12246 -- are no constraints involved (could be more general???)
12248 elsif Etype (Ityp) = Otyp
12249 and then not Is_Tagged_Type (Ityp)
12250 and then not Has_Discriminants (Ityp)
12251 and then No (First_Rep_Item (Base_Type (Ityp)))
12252 then
12253 return True;
12255 -- If the expression has an access type (object or subprogram) we assume
12256 -- that the conversion is safe, because the size of the target is safe,
12257 -- even if it is a record (which might be treated as having unknown size
12258 -- at this point).
12260 elsif Is_Access_Type (Ityp) then
12261 return True;
12263 -- If the size of output type is known at compile time, there is never
12264 -- a problem. Note that unconstrained records are considered to be of
12265 -- known size, but we can't consider them that way here, because we are
12266 -- talking about the actual size of the object.
12268 -- We also make sure that in addition to the size being known, we do not
12269 -- have a case which might generate an embarrassingly large temp in
12270 -- stack checking mode.
12272 elsif Size_Known_At_Compile_Time (Otyp)
12273 and then
12274 (not Stack_Checking_Enabled
12275 or else not May_Generate_Large_Temp (Otyp))
12276 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12277 then
12278 return True;
12280 -- If either type is tagged, then we know the alignment is OK so Gigi
12281 -- will be able to use pointer punning.
12283 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
12284 return True;
12286 -- If either type is a limited record type, we cannot do a copy, so say
12287 -- safe since there's nothing else we can do.
12289 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
12290 return True;
12292 -- Conversions to and from packed array types are always ignored and
12293 -- hence are safe.
12295 elsif Is_Packed_Array_Impl_Type (Otyp)
12296 or else Is_Packed_Array_Impl_Type (Ityp)
12297 then
12298 return True;
12299 end if;
12301 -- The only other cases known to be safe is if the input type's
12302 -- alignment is known to be at least the maximum alignment for the
12303 -- target or if both alignments are known and the output type's
12304 -- alignment is no stricter than the input's. We can use the component
12305 -- type alignment for an array if a type is an unpacked array type.
12307 if Present (Alignment_Clause (Otyp)) then
12308 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
12310 elsif Is_Array_Type (Otyp)
12311 and then Present (Alignment_Clause (Component_Type (Otyp)))
12312 then
12313 Oalign := Expr_Value (Expression (Alignment_Clause
12314 (Component_Type (Otyp))));
12315 end if;
12317 if Present (Alignment_Clause (Ityp)) then
12318 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
12320 elsif Is_Array_Type (Ityp)
12321 and then Present (Alignment_Clause (Component_Type (Ityp)))
12322 then
12323 Ialign := Expr_Value (Expression (Alignment_Clause
12324 (Component_Type (Ityp))));
12325 end if;
12327 if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
12328 return True;
12330 elsif Ialign /= No_Uint
12331 and then Oalign /= No_Uint
12332 and then Ialign <= Oalign
12333 then
12334 return True;
12336 -- Otherwise, Gigi cannot handle this and we must make a temporary
12338 else
12339 return False;
12340 end if;
12341 end Safe_Unchecked_Type_Conversion;
12343 ---------------------------------
12344 -- Set_Current_Value_Condition --
12345 ---------------------------------
12347 -- Note: the implementation of this procedure is very closely tied to the
12348 -- implementation of Get_Current_Value_Condition. Here we set required
12349 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
12350 -- them, so they must have a consistent view.
12352 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
12354 procedure Set_Entity_Current_Value (N : Node_Id);
12355 -- If N is an entity reference, where the entity is of an appropriate
12356 -- kind, then set the current value of this entity to Cnode, unless
12357 -- there is already a definite value set there.
12359 procedure Set_Expression_Current_Value (N : Node_Id);
12360 -- If N is of an appropriate form, sets an appropriate entry in current
12361 -- value fields of relevant entities. Multiple entities can be affected
12362 -- in the case of an AND or AND THEN.
12364 ------------------------------
12365 -- Set_Entity_Current_Value --
12366 ------------------------------
12368 procedure Set_Entity_Current_Value (N : Node_Id) is
12369 begin
12370 if Is_Entity_Name (N) then
12371 declare
12372 Ent : constant Entity_Id := Entity (N);
12374 begin
12375 -- Don't capture if not safe to do so
12377 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
12378 return;
12379 end if;
12381 -- Here we have a case where the Current_Value field may need
12382 -- to be set. We set it if it is not already set to a compile
12383 -- time expression value.
12385 -- Note that this represents a decision that one condition
12386 -- blots out another previous one. That's certainly right if
12387 -- they occur at the same level. If the second one is nested,
12388 -- then the decision is neither right nor wrong (it would be
12389 -- equally OK to leave the outer one in place, or take the new
12390 -- inner one. Really we should record both, but our data
12391 -- structures are not that elaborate.
12393 if Nkind (Current_Value (Ent)) not in N_Subexpr then
12394 Set_Current_Value (Ent, Cnode);
12395 end if;
12396 end;
12397 end if;
12398 end Set_Entity_Current_Value;
12400 ----------------------------------
12401 -- Set_Expression_Current_Value --
12402 ----------------------------------
12404 procedure Set_Expression_Current_Value (N : Node_Id) is
12405 Cond : Node_Id;
12407 begin
12408 Cond := N;
12410 -- Loop to deal with (ignore for now) any NOT operators present. The
12411 -- presence of NOT operators will be handled properly when we call
12412 -- Get_Current_Value_Condition.
12414 while Nkind (Cond) = N_Op_Not loop
12415 Cond := Right_Opnd (Cond);
12416 end loop;
12418 -- For an AND or AND THEN, recursively process operands
12420 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
12421 Set_Expression_Current_Value (Left_Opnd (Cond));
12422 Set_Expression_Current_Value (Right_Opnd (Cond));
12423 return;
12424 end if;
12426 -- Check possible relational operator
12428 if Nkind (Cond) in N_Op_Compare then
12429 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
12430 Set_Entity_Current_Value (Left_Opnd (Cond));
12431 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
12432 Set_Entity_Current_Value (Right_Opnd (Cond));
12433 end if;
12435 elsif Nkind_In (Cond,
12436 N_Type_Conversion,
12437 N_Qualified_Expression,
12438 N_Expression_With_Actions)
12439 then
12440 Set_Expression_Current_Value (Expression (Cond));
12442 -- Check possible boolean variable reference
12444 else
12445 Set_Entity_Current_Value (Cond);
12446 end if;
12447 end Set_Expression_Current_Value;
12449 -- Start of processing for Set_Current_Value_Condition
12451 begin
12452 Set_Expression_Current_Value (Condition (Cnode));
12453 end Set_Current_Value_Condition;
12455 --------------------------
12456 -- Set_Elaboration_Flag --
12457 --------------------------
12459 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
12460 Loc : constant Source_Ptr := Sloc (N);
12461 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
12462 Asn : Node_Id;
12464 begin
12465 if Present (Ent) then
12467 -- Nothing to do if at the compilation unit level, because in this
12468 -- case the flag is set by the binder generated elaboration routine.
12470 if Nkind (Parent (N)) = N_Compilation_Unit then
12471 null;
12473 -- Here we do need to generate an assignment statement
12475 else
12476 Check_Restriction (No_Elaboration_Code, N);
12477 Asn :=
12478 Make_Assignment_Statement (Loc,
12479 Name => New_Occurrence_Of (Ent, Loc),
12480 Expression => Make_Integer_Literal (Loc, Uint_1));
12482 if Nkind (Parent (N)) = N_Subunit then
12483 Insert_After (Corresponding_Stub (Parent (N)), Asn);
12484 else
12485 Insert_After (N, Asn);
12486 end if;
12488 Analyze (Asn);
12490 -- Kill current value indication. This is necessary because the
12491 -- tests of this flag are inserted out of sequence and must not
12492 -- pick up bogus indications of the wrong constant value.
12494 Set_Current_Value (Ent, Empty);
12496 -- If the subprogram is in the current declarative part and
12497 -- 'access has been applied to it, generate an elaboration
12498 -- check at the beginning of the declarations of the body.
12500 if Nkind (N) = N_Subprogram_Body
12501 and then Address_Taken (Spec_Id)
12502 and then
12503 Ekind_In (Scope (Spec_Id), E_Block, E_Procedure, E_Function)
12504 then
12505 declare
12506 Loc : constant Source_Ptr := Sloc (N);
12507 Decls : constant List_Id := Declarations (N);
12508 Chk : Node_Id;
12510 begin
12511 -- No need to generate this check if first entry in the
12512 -- declaration list is a raise of Program_Error now.
12514 if Present (Decls)
12515 and then Nkind (First (Decls)) = N_Raise_Program_Error
12516 then
12517 return;
12518 end if;
12520 -- Otherwise generate the check
12522 Chk :=
12523 Make_Raise_Program_Error (Loc,
12524 Condition =>
12525 Make_Op_Eq (Loc,
12526 Left_Opnd => New_Occurrence_Of (Ent, Loc),
12527 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
12528 Reason => PE_Access_Before_Elaboration);
12530 if No (Decls) then
12531 Set_Declarations (N, New_List (Chk));
12532 else
12533 Prepend (Chk, Decls);
12534 end if;
12536 Analyze (Chk);
12537 end;
12538 end if;
12539 end if;
12540 end if;
12541 end Set_Elaboration_Flag;
12543 ----------------------------
12544 -- Set_Renamed_Subprogram --
12545 ----------------------------
12547 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
12548 begin
12549 -- If input node is an identifier, we can just reset it
12551 if Nkind (N) = N_Identifier then
12552 Set_Chars (N, Chars (E));
12553 Set_Entity (N, E);
12555 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
12557 else
12558 declare
12559 CS : constant Boolean := Comes_From_Source (N);
12560 begin
12561 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
12562 Set_Entity (N, E);
12563 Set_Comes_From_Source (N, CS);
12564 Set_Analyzed (N, True);
12565 end;
12566 end if;
12567 end Set_Renamed_Subprogram;
12569 ----------------------
12570 -- Side_Effect_Free --
12571 ----------------------
12573 function Side_Effect_Free
12574 (N : Node_Id;
12575 Name_Req : Boolean := False;
12576 Variable_Ref : Boolean := False) return Boolean
12578 Typ : constant Entity_Id := Etype (N);
12579 -- Result type of the expression
12581 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
12582 -- The argument N is a construct where the Prefix is dereferenced if it
12583 -- is an access type and the result is a variable. The call returns True
12584 -- if the construct is side effect free (not considering side effects in
12585 -- other than the prefix which are to be tested by the caller).
12587 function Within_In_Parameter (N : Node_Id) return Boolean;
12588 -- Determines if N is a subcomponent of a composite in-parameter. If so,
12589 -- N is not side-effect free when the actual is global and modifiable
12590 -- indirectly from within a subprogram, because it may be passed by
12591 -- reference. The front-end must be conservative here and assume that
12592 -- this may happen with any array or record type. On the other hand, we
12593 -- cannot create temporaries for all expressions for which this
12594 -- condition is true, for various reasons that might require clearing up
12595 -- ??? For example, discriminant references that appear out of place, or
12596 -- spurious type errors with class-wide expressions. As a result, we
12597 -- limit the transformation to loop bounds, which is so far the only
12598 -- case that requires it.
12600 -----------------------------
12601 -- Safe_Prefixed_Reference --
12602 -----------------------------
12604 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
12605 begin
12606 -- If prefix is not side effect free, definitely not safe
12608 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
12609 return False;
12611 -- If the prefix is of an access type that is not access-to-constant,
12612 -- then this construct is a variable reference, which means it is to
12613 -- be considered to have side effects if Variable_Ref is set True.
12615 elsif Is_Access_Type (Etype (Prefix (N)))
12616 and then not Is_Access_Constant (Etype (Prefix (N)))
12617 and then Variable_Ref
12618 then
12619 -- Exception is a prefix that is the result of a previous removal
12620 -- of side-effects.
12622 return Is_Entity_Name (Prefix (N))
12623 and then not Comes_From_Source (Prefix (N))
12624 and then Ekind (Entity (Prefix (N))) = E_Constant
12625 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
12627 -- If the prefix is an explicit dereference then this construct is a
12628 -- variable reference, which means it is to be considered to have
12629 -- side effects if Variable_Ref is True.
12631 -- We do NOT exclude dereferences of access-to-constant types because
12632 -- we handle them as constant view of variables.
12634 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
12635 and then Variable_Ref
12636 then
12637 return False;
12639 -- Note: The following test is the simplest way of solving a complex
12640 -- problem uncovered by the following test (Side effect on loop bound
12641 -- that is a subcomponent of a global variable:
12643 -- with Text_Io; use Text_Io;
12644 -- procedure Tloop is
12645 -- type X is
12646 -- record
12647 -- V : Natural := 4;
12648 -- S : String (1..5) := (others => 'a');
12649 -- end record;
12650 -- X1 : X;
12652 -- procedure Modi;
12654 -- generic
12655 -- with procedure Action;
12656 -- procedure Loop_G (Arg : X; Msg : String)
12658 -- procedure Loop_G (Arg : X; Msg : String) is
12659 -- begin
12660 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
12661 -- & Natural'Image (Arg.V));
12662 -- for Index in 1 .. Arg.V loop
12663 -- Text_Io.Put_Line
12664 -- (Natural'Image (Index) & " " & Arg.S (Index));
12665 -- if Index > 2 then
12666 -- Modi;
12667 -- end if;
12668 -- end loop;
12669 -- Put_Line ("end loop_g " & Msg);
12670 -- end;
12672 -- procedure Loop1 is new Loop_G (Modi);
12673 -- procedure Modi is
12674 -- begin
12675 -- X1.V := 1;
12676 -- Loop1 (X1, "from modi");
12677 -- end;
12679 -- begin
12680 -- Loop1 (X1, "initial");
12681 -- end;
12683 -- The output of the above program should be:
12685 -- begin loop_g initial will loop till: 4
12686 -- 1 a
12687 -- 2 a
12688 -- 3 a
12689 -- begin loop_g from modi will loop till: 1
12690 -- 1 a
12691 -- end loop_g from modi
12692 -- 4 a
12693 -- begin loop_g from modi will loop till: 1
12694 -- 1 a
12695 -- end loop_g from modi
12696 -- end loop_g initial
12698 -- If a loop bound is a subcomponent of a global variable, a
12699 -- modification of that variable within the loop may incorrectly
12700 -- affect the execution of the loop.
12702 elsif Nkind (Parent (Parent (N))) = N_Loop_Parameter_Specification
12703 and then Within_In_Parameter (Prefix (N))
12704 and then Variable_Ref
12705 then
12706 return False;
12708 -- All other cases are side effect free
12710 else
12711 return True;
12712 end if;
12713 end Safe_Prefixed_Reference;
12715 -------------------------
12716 -- Within_In_Parameter --
12717 -------------------------
12719 function Within_In_Parameter (N : Node_Id) return Boolean is
12720 begin
12721 if not Comes_From_Source (N) then
12722 return False;
12724 elsif Is_Entity_Name (N) then
12725 return Ekind (Entity (N)) = E_In_Parameter;
12727 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
12728 return Within_In_Parameter (Prefix (N));
12730 else
12731 return False;
12732 end if;
12733 end Within_In_Parameter;
12735 -- Start of processing for Side_Effect_Free
12737 begin
12738 -- If volatile reference, always consider it to have side effects
12740 if Is_Volatile_Reference (N) then
12741 return False;
12742 end if;
12744 -- Note on checks that could raise Constraint_Error. Strictly, if we
12745 -- take advantage of 11.6, these checks do not count as side effects.
12746 -- However, we would prefer to consider that they are side effects,
12747 -- since the back end CSE does not work very well on expressions which
12748 -- can raise Constraint_Error. On the other hand if we don't consider
12749 -- them to be side effect free, then we get some awkward expansions
12750 -- in -gnato mode, resulting in code insertions at a point where we
12751 -- do not have a clear model for performing the insertions.
12753 -- Special handling for entity names
12755 if Is_Entity_Name (N) then
12757 -- A type reference is always side effect free
12759 if Is_Type (Entity (N)) then
12760 return True;
12762 -- Variables are considered to be a side effect if Variable_Ref
12763 -- is set or if we have a volatile reference and Name_Req is off.
12764 -- If Name_Req is True then we can't help returning a name which
12765 -- effectively allows multiple references in any case.
12767 elsif Is_Variable (N, Use_Original_Node => False) then
12768 return not Variable_Ref
12769 and then (not Is_Volatile_Reference (N) or else Name_Req);
12771 -- Any other entity (e.g. a subtype name) is definitely side
12772 -- effect free.
12774 else
12775 return True;
12776 end if;
12778 -- A value known at compile time is always side effect free
12780 elsif Compile_Time_Known_Value (N) then
12781 return True;
12783 -- A variable renaming is not side-effect free, because the renaming
12784 -- will function like a macro in the front-end in some cases, and an
12785 -- assignment can modify the component designated by N, so we need to
12786 -- create a temporary for it.
12788 -- The guard testing for Entity being present is needed at least in
12789 -- the case of rewritten predicate expressions, and may well also be
12790 -- appropriate elsewhere. Obviously we can't go testing the entity
12791 -- field if it does not exist, so it's reasonable to say that this is
12792 -- not the renaming case if it does not exist.
12794 elsif Is_Entity_Name (Original_Node (N))
12795 and then Present (Entity (Original_Node (N)))
12796 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
12797 and then Ekind (Entity (Original_Node (N))) /= E_Constant
12798 then
12799 declare
12800 RO : constant Node_Id :=
12801 Renamed_Object (Entity (Original_Node (N)));
12803 begin
12804 -- If the renamed object is an indexed component, or an
12805 -- explicit dereference, then the designated object could
12806 -- be modified by an assignment.
12808 if Nkind_In (RO, N_Indexed_Component,
12809 N_Explicit_Dereference)
12810 then
12811 return False;
12813 -- A selected component must have a safe prefix
12815 elsif Nkind (RO) = N_Selected_Component then
12816 return Safe_Prefixed_Reference (RO);
12818 -- In all other cases, designated object cannot be changed so
12819 -- we are side effect free.
12821 else
12822 return True;
12823 end if;
12824 end;
12826 -- Remove_Side_Effects generates an object renaming declaration to
12827 -- capture the expression of a class-wide expression. In VM targets
12828 -- the frontend performs no expansion for dispatching calls to
12829 -- class- wide types since they are handled by the VM. Hence, we must
12830 -- locate here if this node corresponds to a previous invocation of
12831 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
12833 elsif not Tagged_Type_Expansion
12834 and then not Comes_From_Source (N)
12835 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
12836 and then Is_Class_Wide_Type (Typ)
12837 then
12838 return True;
12840 -- Generating C the type conversion of an access to constrained array
12841 -- type into an access to unconstrained array type involves initializing
12842 -- a fat pointer and the expression cannot be assumed to be free of side
12843 -- effects since it must referenced several times to compute its bounds.
12845 elsif Modify_Tree_For_C
12846 and then Nkind (N) = N_Type_Conversion
12847 and then Is_Access_Type (Typ)
12848 and then Is_Array_Type (Designated_Type (Typ))
12849 and then not Is_Constrained (Designated_Type (Typ))
12850 then
12851 return False;
12852 end if;
12854 -- For other than entity names and compile time known values,
12855 -- check the node kind for special processing.
12857 case Nkind (N) is
12859 -- An attribute reference is side effect free if its expressions
12860 -- are side effect free and its prefix is side effect free or
12861 -- is an entity reference.
12863 -- Is this right? what about x'first where x is a variable???
12865 when N_Attribute_Reference =>
12866 Attribute_Reference : declare
12868 function Side_Effect_Free_Attribute
12869 (Attribute_Name : Name_Id) return Boolean;
12870 -- Returns True if evaluation of the given attribute is
12871 -- considered side-effect free (independent of prefix and
12872 -- arguments).
12874 --------------------------------
12875 -- Side_Effect_Free_Attribute --
12876 --------------------------------
12878 function Side_Effect_Free_Attribute
12879 (Attribute_Name : Name_Id) return Boolean
12881 begin
12882 case Attribute_Name is
12883 when Name_Input =>
12884 return False;
12886 when Name_Image
12887 | Name_Img
12888 | Name_Wide_Image
12889 | Name_Wide_Wide_Image
12891 -- CodePeer doesn't want to see replicated copies of
12892 -- 'Image calls.
12894 return not CodePeer_Mode;
12896 when others =>
12897 return True;
12898 end case;
12899 end Side_Effect_Free_Attribute;
12901 -- Start of processing for Attribute_Reference
12903 begin
12904 return
12905 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12906 and then Side_Effect_Free_Attribute (Attribute_Name (N))
12907 and then (Is_Entity_Name (Prefix (N))
12908 or else Side_Effect_Free
12909 (Prefix (N), Name_Req, Variable_Ref));
12910 end Attribute_Reference;
12912 -- A binary operator is side effect free if and both operands are
12913 -- side effect free. For this purpose binary operators include
12914 -- membership tests and short circuit forms.
12916 when N_Binary_Op
12917 | N_Membership_Test
12918 | N_Short_Circuit
12920 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
12921 and then
12922 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
12924 -- An explicit dereference is side effect free only if it is
12925 -- a side effect free prefixed reference.
12927 when N_Explicit_Dereference =>
12928 return Safe_Prefixed_Reference (N);
12930 -- An expression with action is side effect free if its expression
12931 -- is side effect free and it has no actions.
12933 when N_Expression_With_Actions =>
12934 return
12935 Is_Empty_List (Actions (N))
12936 and then Side_Effect_Free
12937 (Expression (N), Name_Req, Variable_Ref);
12939 -- A call to _rep_to_pos is side effect free, since we generate
12940 -- this pure function call ourselves. Moreover it is critically
12941 -- important to make this exception, since otherwise we can have
12942 -- discriminants in array components which don't look side effect
12943 -- free in the case of an array whose index type is an enumeration
12944 -- type with an enumeration rep clause.
12946 -- All other function calls are not side effect free
12948 when N_Function_Call =>
12949 return
12950 Nkind (Name (N)) = N_Identifier
12951 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
12952 and then Side_Effect_Free
12953 (First (Parameter_Associations (N)),
12954 Name_Req, Variable_Ref);
12956 -- An IF expression is side effect free if it's of a scalar type, and
12957 -- all its components are all side effect free (conditions and then
12958 -- actions and else actions). We restrict to scalar types, since it
12959 -- is annoying to deal with things like (if A then B else C)'First
12960 -- where the type involved is a string type.
12962 when N_If_Expression =>
12963 return
12964 Is_Scalar_Type (Typ)
12965 and then Side_Effect_Free
12966 (Expressions (N), Name_Req, Variable_Ref);
12968 -- An indexed component is side effect free if it is a side
12969 -- effect free prefixed reference and all the indexing
12970 -- expressions are side effect free.
12972 when N_Indexed_Component =>
12973 return
12974 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
12975 and then Safe_Prefixed_Reference (N);
12977 -- A type qualification, type conversion, or unchecked expression is
12978 -- side effect free if the expression is side effect free.
12980 when N_Qualified_Expression
12981 | N_Type_Conversion
12982 | N_Unchecked_Expression
12984 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
12986 -- A selected component is side effect free only if it is a side
12987 -- effect free prefixed reference.
12989 when N_Selected_Component =>
12990 return Safe_Prefixed_Reference (N);
12992 -- A range is side effect free if the bounds are side effect free
12994 when N_Range =>
12995 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
12996 and then
12997 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
12999 -- A slice is side effect free if it is a side effect free
13000 -- prefixed reference and the bounds are side effect free.
13002 when N_Slice =>
13003 return
13004 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13005 and then Safe_Prefixed_Reference (N);
13007 -- A unary operator is side effect free if the operand
13008 -- is side effect free.
13010 when N_Unary_Op =>
13011 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13013 -- An unchecked type conversion is side effect free only if it
13014 -- is safe and its argument is side effect free.
13016 when N_Unchecked_Type_Conversion =>
13017 return
13018 Safe_Unchecked_Type_Conversion (N)
13019 and then Side_Effect_Free
13020 (Expression (N), Name_Req, Variable_Ref);
13022 -- A literal is side effect free
13024 when N_Character_Literal
13025 | N_Integer_Literal
13026 | N_Real_Literal
13027 | N_String_Literal
13029 return True;
13031 -- We consider that anything else has side effects. This is a bit
13032 -- crude, but we are pretty close for most common cases, and we
13033 -- are certainly correct (i.e. we never return True when the
13034 -- answer should be False).
13036 when others =>
13037 return False;
13038 end case;
13039 end Side_Effect_Free;
13041 -- A list is side effect free if all elements of the list are side
13042 -- effect free.
13044 function Side_Effect_Free
13045 (L : List_Id;
13046 Name_Req : Boolean := False;
13047 Variable_Ref : Boolean := False) return Boolean
13049 N : Node_Id;
13051 begin
13052 if L = No_List or else L = Error_List then
13053 return True;
13055 else
13056 N := First (L);
13057 while Present (N) loop
13058 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13059 return False;
13060 else
13061 Next (N);
13062 end if;
13063 end loop;
13065 return True;
13066 end if;
13067 end Side_Effect_Free;
13069 ----------------------------------
13070 -- Silly_Boolean_Array_Not_Test --
13071 ----------------------------------
13073 -- This procedure implements an odd and silly test. We explicitly check
13074 -- for the case where the 'First of the component type is equal to the
13075 -- 'Last of this component type, and if this is the case, we make sure
13076 -- that constraint error is raised. The reason is that the NOT is bound
13077 -- to cause CE in this case, and we will not otherwise catch it.
13079 -- No such check is required for AND and OR, since for both these cases
13080 -- False op False = False, and True op True = True. For the XOR case,
13081 -- see Silly_Boolean_Array_Xor_Test.
13083 -- Believe it or not, this was reported as a bug. Note that nearly always,
13084 -- the test will evaluate statically to False, so the code will be
13085 -- statically removed, and no extra overhead caused.
13087 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13088 Loc : constant Source_Ptr := Sloc (N);
13089 CT : constant Entity_Id := Component_Type (T);
13091 begin
13092 -- The check we install is
13094 -- constraint_error when
13095 -- component_type'first = component_type'last
13096 -- and then array_type'Length /= 0)
13098 -- We need the last guard because we don't want to raise CE for empty
13099 -- arrays since no out of range values result. (Empty arrays with a
13100 -- component type of True .. True -- very useful -- even the ACATS
13101 -- does not test that marginal case).
13103 Insert_Action (N,
13104 Make_Raise_Constraint_Error (Loc,
13105 Condition =>
13106 Make_And_Then (Loc,
13107 Left_Opnd =>
13108 Make_Op_Eq (Loc,
13109 Left_Opnd =>
13110 Make_Attribute_Reference (Loc,
13111 Prefix => New_Occurrence_Of (CT, Loc),
13112 Attribute_Name => Name_First),
13114 Right_Opnd =>
13115 Make_Attribute_Reference (Loc,
13116 Prefix => New_Occurrence_Of (CT, Loc),
13117 Attribute_Name => Name_Last)),
13119 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13120 Reason => CE_Range_Check_Failed));
13121 end Silly_Boolean_Array_Not_Test;
13123 ----------------------------------
13124 -- Silly_Boolean_Array_Xor_Test --
13125 ----------------------------------
13127 -- This procedure implements an odd and silly test. We explicitly check
13128 -- for the XOR case where the component type is True .. True, since this
13129 -- will raise constraint error. A special check is required since CE
13130 -- will not be generated otherwise (cf Expand_Packed_Not).
13132 -- No such check is required for AND and OR, since for both these cases
13133 -- False op False = False, and True op True = True, and no check is
13134 -- required for the case of False .. False, since False xor False = False.
13135 -- See also Silly_Boolean_Array_Not_Test
13137 procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
13138 Loc : constant Source_Ptr := Sloc (N);
13139 CT : constant Entity_Id := Component_Type (T);
13141 begin
13142 -- The check we install is
13144 -- constraint_error when
13145 -- Boolean (component_type'First)
13146 -- and then Boolean (component_type'Last)
13147 -- and then array_type'Length /= 0)
13149 -- We need the last guard because we don't want to raise CE for empty
13150 -- arrays since no out of range values result (Empty arrays with a
13151 -- component type of True .. True -- very useful -- even the ACATS
13152 -- does not test that marginal case).
13154 Insert_Action (N,
13155 Make_Raise_Constraint_Error (Loc,
13156 Condition =>
13157 Make_And_Then (Loc,
13158 Left_Opnd =>
13159 Make_And_Then (Loc,
13160 Left_Opnd =>
13161 Convert_To (Standard_Boolean,
13162 Make_Attribute_Reference (Loc,
13163 Prefix => New_Occurrence_Of (CT, Loc),
13164 Attribute_Name => Name_First)),
13166 Right_Opnd =>
13167 Convert_To (Standard_Boolean,
13168 Make_Attribute_Reference (Loc,
13169 Prefix => New_Occurrence_Of (CT, Loc),
13170 Attribute_Name => Name_Last))),
13172 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13173 Reason => CE_Range_Check_Failed));
13174 end Silly_Boolean_Array_Xor_Test;
13176 --------------------------
13177 -- Target_Has_Fixed_Ops --
13178 --------------------------
13180 Integer_Sized_Small : Ureal;
13181 -- Set to 2.0 ** -(Integer'Size - 1) the first time that this function is
13182 -- called (we don't want to compute it more than once).
13184 Long_Integer_Sized_Small : Ureal;
13185 -- Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this function
13186 -- is called (we don't want to compute it more than once)
13188 First_Time_For_THFO : Boolean := True;
13189 -- Set to False after first call (if Fractional_Fixed_Ops_On_Target)
13191 function Target_Has_Fixed_Ops
13192 (Left_Typ : Entity_Id;
13193 Right_Typ : Entity_Id;
13194 Result_Typ : Entity_Id) return Boolean
13196 function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
13197 -- Return True if the given type is a fixed-point type with a small
13198 -- value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
13199 -- an absolute value less than 1.0. This is currently limited to
13200 -- fixed-point types that map to Integer or Long_Integer.
13202 ------------------------
13203 -- Is_Fractional_Type --
13204 ------------------------
13206 function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
13207 begin
13208 if Esize (Typ) = Standard_Integer_Size then
13209 return Small_Value (Typ) = Integer_Sized_Small;
13211 elsif Esize (Typ) = Standard_Long_Integer_Size then
13212 return Small_Value (Typ) = Long_Integer_Sized_Small;
13214 else
13215 return False;
13216 end if;
13217 end Is_Fractional_Type;
13219 -- Start of processing for Target_Has_Fixed_Ops
13221 begin
13222 -- Return False if Fractional_Fixed_Ops_On_Target is false
13224 if not Fractional_Fixed_Ops_On_Target then
13225 return False;
13226 end if;
13228 -- Here the target has Fractional_Fixed_Ops, if first time, compute
13229 -- standard constants used by Is_Fractional_Type.
13231 if First_Time_For_THFO then
13232 First_Time_For_THFO := False;
13234 Integer_Sized_Small :=
13235 UR_From_Components
13236 (Num => Uint_1,
13237 Den => UI_From_Int (Standard_Integer_Size - 1),
13238 Rbase => 2);
13240 Long_Integer_Sized_Small :=
13241 UR_From_Components
13242 (Num => Uint_1,
13243 Den => UI_From_Int (Standard_Long_Integer_Size - 1),
13244 Rbase => 2);
13245 end if;
13247 -- Return True if target supports fixed-by-fixed multiply/divide for
13248 -- fractional fixed-point types (see Is_Fractional_Type) and the operand
13249 -- and result types are equivalent fractional types.
13251 return Is_Fractional_Type (Base_Type (Left_Typ))
13252 and then Is_Fractional_Type (Base_Type (Right_Typ))
13253 and then Is_Fractional_Type (Base_Type (Result_Typ))
13254 and then Esize (Left_Typ) = Esize (Right_Typ)
13255 and then Esize (Left_Typ) = Esize (Result_Typ);
13256 end Target_Has_Fixed_Ops;
13258 -------------------
13259 -- Type_Map_Hash --
13260 -------------------
13262 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13263 begin
13264 return Type_Map_Header (Id mod Type_Map_Size);
13265 end Type_Map_Hash;
13267 ------------------------------------------
13268 -- Type_May_Have_Bit_Aligned_Components --
13269 ------------------------------------------
13271 function Type_May_Have_Bit_Aligned_Components
13272 (Typ : Entity_Id) return Boolean
13274 begin
13275 -- Array type, check component type
13277 if Is_Array_Type (Typ) then
13278 return
13279 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13281 -- Record type, check components
13283 elsif Is_Record_Type (Typ) then
13284 declare
13285 E : Entity_Id;
13287 begin
13288 E := First_Component_Or_Discriminant (Typ);
13289 while Present (E) loop
13290 if Component_May_Be_Bit_Aligned (E)
13291 or else Type_May_Have_Bit_Aligned_Components (Etype (E))
13292 then
13293 return True;
13294 end if;
13296 Next_Component_Or_Discriminant (E);
13297 end loop;
13299 return False;
13300 end;
13302 -- Type other than array or record is always OK
13304 else
13305 return False;
13306 end if;
13307 end Type_May_Have_Bit_Aligned_Components;
13309 -------------------------------
13310 -- Update_Primitives_Mapping --
13311 -------------------------------
13313 procedure Update_Primitives_Mapping
13314 (Inher_Id : Entity_Id;
13315 Subp_Id : Entity_Id)
13317 begin
13318 Map_Types
13319 (Parent_Type => Find_Dispatching_Type (Inher_Id),
13320 Derived_Type => Find_Dispatching_Type (Subp_Id));
13321 end Update_Primitives_Mapping;
13323 ----------------------------------
13324 -- Within_Case_Or_If_Expression --
13325 ----------------------------------
13327 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
13328 Par : Node_Id;
13330 begin
13331 -- Locate an enclosing case or if expression. Note that these constructs
13332 -- can be expanded into Expression_With_Actions, hence the test of the
13333 -- original node.
13335 Par := Parent (N);
13336 while Present (Par) loop
13337 if Nkind_In (Original_Node (Par), N_Case_Expression,
13338 N_If_Expression)
13339 then
13340 return True;
13342 -- Prevent the search from going too far
13344 elsif Is_Body_Or_Package_Declaration (Par) then
13345 return False;
13346 end if;
13348 Par := Parent (Par);
13349 end loop;
13351 return False;
13352 end Within_Case_Or_If_Expression;
13354 --------------------------------
13355 -- Within_Internal_Subprogram --
13356 --------------------------------
13358 function Within_Internal_Subprogram return Boolean is
13359 S : Entity_Id;
13361 begin
13362 S := Current_Scope;
13363 while Present (S) and then not Is_Subprogram (S) loop
13364 S := Scope (S);
13365 end loop;
13367 return Present (S)
13368 and then Get_TSS_Name (S) /= TSS_Null
13369 and then not Is_Predicate_Function (S)
13370 and then not Is_Predicate_Function_M (S);
13371 end Within_Internal_Subprogram;
13373 end Exp_Util;