Daily bump.
[official-gcc.git] / gcc / ada / exp_util.adb
blob0aa7937b211f8c6332485c20d9c71f0f0149fe4b
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-2021, 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 Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Aggr; use Exp_Aggr;
37 with Exp_Ch6; use Exp_Ch6;
38 with Exp_Ch7; use Exp_Ch7;
39 with Exp_Ch11; use Exp_Ch11;
40 with Freeze; use Freeze;
41 with Ghost; use Ghost;
42 with Inline; use Inline;
43 with Itypes; use Itypes;
44 with Lib; use Lib;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch12; use Sem_Ch12;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Type; use Sem_Type;
62 with Sem_Util; use Sem_Util;
63 with Sinfo.Utils; use Sinfo.Utils;
64 with Snames; use Snames;
65 with Stand; use Stand;
66 with Stringt; use Stringt;
67 with Tbuild; use Tbuild;
68 with Ttypes; use Ttypes;
69 with Validsw; use Validsw;
71 with GNAT.HTable;
72 package body Exp_Util is
74 ---------------------------------------------------------
75 -- Handling of inherited class-wide pre/postconditions --
76 ---------------------------------------------------------
78 -- Following AI12-0113, the expression for a class-wide condition is
79 -- transformed for a subprogram that inherits it, by replacing calls
80 -- to primitive operations of the original controlling type into the
81 -- corresponding overriding operations of the derived type. The following
82 -- hash table manages this mapping, and is expanded on demand whenever
83 -- such inherited expression needs to be constructed.
85 -- The mapping is also used to check whether an inherited operation has
86 -- a condition that depends on overridden operations. For such an
87 -- operation we must create a wrapper that is then treated as a normal
88 -- overriding. In SPARK mode such operations are illegal.
90 -- For a given root type there may be several type extensions with their
91 -- own overriding operations, so at various times a given operation of
92 -- the root will be mapped into different overridings. The root type is
93 -- also mapped into the current type extension to indicate that its
94 -- operations are mapped into the overriding operations of that current
95 -- type extension.
97 -- The contents of the map are as follows:
99 -- Key Value
101 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
102 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
103 -- Discriminant (Entity_Id) Expression (Node_Id)
104 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
105 -- Type (Entity_Id) Type (Entity_Id)
107 Type_Map_Size : constant := 511;
109 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
110 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
112 package Type_Map is new GNAT.HTable.Simple_HTable
113 (Header_Num => Type_Map_Header,
114 Key => Entity_Id,
115 Element => Node_Or_Entity_Id,
116 No_element => Empty,
117 Hash => Type_Map_Hash,
118 Equal => "=");
120 -----------------------
121 -- Local Subprograms --
122 -----------------------
124 function Build_Task_Array_Image
125 (Loc : Source_Ptr;
126 Id_Ref : Node_Id;
127 A_Type : Entity_Id;
128 Dyn : Boolean := False) return Node_Id;
129 -- Build function to generate the image string for a task that is an array
130 -- component, concatenating the images of each index. To avoid storage
131 -- leaks, the string is built with successive slice assignments. The flag
132 -- Dyn indicates whether this is called for the initialization procedure of
133 -- an array of tasks, or for the name of a dynamically created task that is
134 -- assigned to an indexed component.
136 function Build_Task_Image_Function
137 (Loc : Source_Ptr;
138 Decls : List_Id;
139 Stats : List_Id;
140 Res : Entity_Id) return Node_Id;
141 -- Common processing for Task_Array_Image and Task_Record_Image. Build
142 -- function body that computes image.
144 procedure Build_Task_Image_Prefix
145 (Loc : Source_Ptr;
146 Len : out Entity_Id;
147 Res : out Entity_Id;
148 Pos : out Entity_Id;
149 Prefix : Entity_Id;
150 Sum : Node_Id;
151 Decls : List_Id;
152 Stats : List_Id);
153 -- Common processing for Task_Array_Image and Task_Record_Image. Create
154 -- local variables and assign prefix of name to result string.
156 function Build_Task_Record_Image
157 (Loc : Source_Ptr;
158 Id_Ref : Node_Id;
159 Dyn : Boolean := False) return Node_Id;
160 -- Build function to generate the image string for a task that is a record
161 -- component. Concatenate name of variable with that of selector. The flag
162 -- Dyn indicates whether this is called for the initialization procedure of
163 -- record with task components, or for a dynamically created task that is
164 -- assigned to a selected component.
166 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
167 -- Force evaluation of bounds of a slice, which may be given by a range
168 -- or by a subtype indication with or without a constraint.
170 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
171 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
172 -- an assertion expression that should be verified at run time.
174 function Is_Uninitialized_Aggregate
175 (Exp : Node_Id;
176 T : Entity_Id) return Boolean;
177 -- Determine whether an array aggregate used in an object declaration
178 -- is uninitialized, when the aggregate is declared with a box and
179 -- the component type has no default value. Such an aggregate can be
180 -- optimized away to prevent the copying of uninitialized data, and
181 -- the bounds of the aggregate can be propagated directly to the
182 -- object declaration.
184 function Make_CW_Equivalent_Type
185 (T : Entity_Id;
186 E : Node_Id) return Entity_Id;
187 -- T is a class-wide type entity, E is the initial expression node that
188 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
189 -- returns the entity of the Equivalent type and inserts on the fly the
190 -- necessary declaration such as:
192 -- type anon is record
193 -- _parent : Root_Type (T); constrained with E discriminants (if any)
194 -- Extension : String (1 .. expr to match size of E);
195 -- end record;
197 -- This record is compatible with any object of the class of T thanks to
198 -- the first field and has the same size as E thanks to the second.
200 function Make_Literal_Range
201 (Loc : Source_Ptr;
202 Literal_Typ : Entity_Id) return Node_Id;
203 -- Produce a Range node whose bounds are:
204 -- Low_Bound (Literal_Type) ..
205 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
206 -- this is used for expanding declarations like X : String := "sdfgdfg";
208 -- If the index type of the target array is not integer, we generate:
209 -- Low_Bound (Literal_Type) ..
210 -- Literal_Type'Val
211 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
212 -- + (Length (Literal_Typ) -1))
214 function Make_Non_Empty_Check
215 (Loc : Source_Ptr;
216 N : Node_Id) return Node_Id;
217 -- Produce a boolean expression checking that the unidimensional array
218 -- node N is not empty.
220 function New_Class_Wide_Subtype
221 (CW_Typ : Entity_Id;
222 N : Node_Id) return Entity_Id;
223 -- Create an implicit subtype of CW_Typ attached to node N
225 function Requires_Cleanup_Actions
226 (L : List_Id;
227 Lib_Level : Boolean;
228 Nested_Constructs : Boolean) return Boolean;
229 -- Given a list L, determine whether it contains one of the following:
231 -- 1) controlled objects
232 -- 2) library-level tagged types
234 -- Lib_Level is True when the list comes from a construct at the library
235 -- level, and False otherwise. Nested_Constructs is True when any nested
236 -- packages declared in L must be processed, and False otherwise.
238 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean;
239 -- Return True if the evaluation of the given attribute is considered
240 -- side-effect free, independently of its prefix and expressions.
242 -------------------------------------
243 -- Activate_Atomic_Synchronization --
244 -------------------------------------
246 procedure Activate_Atomic_Synchronization (N : Node_Id) is
247 Msg_Node : Node_Id;
249 begin
250 case Nkind (Parent (N)) is
252 -- Check for cases of appearing in the prefix of a construct where we
253 -- don't need atomic synchronization for this kind of usage.
255 when
256 -- Nothing to do if we are the prefix of an attribute, since we
257 -- do not want an atomic sync operation for things like 'Size.
259 N_Attribute_Reference
261 -- The N_Reference node is like an attribute
263 | N_Reference
265 -- Nothing to do for a reference to a component (or components)
266 -- of a composite object. Only reads and updates of the object
267 -- as a whole require atomic synchronization (RM C.6 (15)).
269 | N_Indexed_Component
270 | N_Selected_Component
271 | N_Slice
273 -- For all the above cases, nothing to do if we are the prefix
275 if Prefix (Parent (N)) = N then
276 return;
277 end if;
279 when others =>
280 null;
281 end case;
283 -- Nothing to do for the identifier in an object renaming declaration,
284 -- the renaming itself does not need atomic synchronization.
286 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
287 return;
288 end if;
290 -- Go ahead and set the flag
292 Set_Atomic_Sync_Required (N);
294 -- Generate info message if requested
296 if Warn_On_Atomic_Synchronization then
297 case Nkind (N) is
298 when N_Identifier =>
299 Msg_Node := N;
301 when N_Expanded_Name
302 | N_Selected_Component
304 Msg_Node := Selector_Name (N);
306 when N_Explicit_Dereference
307 | N_Indexed_Component
309 Msg_Node := Empty;
311 when others =>
312 pragma Assert (False);
313 return;
314 end case;
316 if Present (Msg_Node) then
317 Error_Msg_N
318 ("info: atomic synchronization set for &?.n?", Msg_Node);
319 else
320 Error_Msg_N
321 ("info: atomic synchronization set?.n?", N);
322 end if;
323 end if;
324 end Activate_Atomic_Synchronization;
326 ----------------------
327 -- Adjust_Condition --
328 ----------------------
330 procedure Adjust_Condition (N : Node_Id) is
331 begin
332 if No (N) then
333 return;
334 end if;
336 declare
337 Loc : constant Source_Ptr := Sloc (N);
338 T : constant Entity_Id := Etype (N);
340 begin
341 -- Defend against a call where the argument has no type, or has a
342 -- type that is not Boolean. This can occur because of prior errors.
344 if No (T) or else not Is_Boolean_Type (T) then
345 return;
346 end if;
348 -- Apply validity checking if needed
350 if Validity_Checks_On and Validity_Check_Tests then
351 Ensure_Valid (N);
352 end if;
354 -- Immediate return if standard boolean, the most common case,
355 -- where nothing needs to be done.
357 if Base_Type (T) = Standard_Boolean then
358 return;
359 end if;
361 -- Case of zero/nonzero semantics or nonstandard enumeration
362 -- representation. In each case, we rewrite the node as:
364 -- ityp!(N) /= False'Enum_Rep
366 -- where ityp is an integer type with large enough size to hold any
367 -- value of type T.
369 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
370 Rewrite (N,
371 Make_Op_Ne (Loc,
372 Left_Opnd =>
373 Unchecked_Convert_To
374 (Integer_Type_For (Esize (T), Uns => False), N),
375 Right_Opnd =>
376 Make_Attribute_Reference (Loc,
377 Attribute_Name => Name_Enum_Rep,
378 Prefix =>
379 New_Occurrence_Of (First_Literal (T), Loc))));
380 Analyze_And_Resolve (N, Standard_Boolean);
382 else
383 Rewrite (N, Convert_To (Standard_Boolean, N));
384 Analyze_And_Resolve (N, Standard_Boolean);
385 end if;
386 end;
387 end Adjust_Condition;
389 ------------------------
390 -- Adjust_Result_Type --
391 ------------------------
393 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
394 begin
395 -- Ignore call if current type is not Standard.Boolean
397 if Etype (N) /= Standard_Boolean then
398 return;
399 end if;
401 -- If result is already of correct type, nothing to do. Note that
402 -- this will get the most common case where everything has a type
403 -- of Standard.Boolean.
405 if Base_Type (T) = Standard_Boolean then
406 return;
408 else
409 declare
410 KP : constant Node_Kind := Nkind (Parent (N));
412 begin
413 -- If result is to be used as a Condition in the syntax, no need
414 -- to convert it back, since if it was changed to Standard.Boolean
415 -- using Adjust_Condition, that is just fine for this usage.
417 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
418 return;
420 -- If result is an operand of another logical operation, no need
421 -- to reset its type, since Standard.Boolean is just fine, and
422 -- such operations always do Adjust_Condition on their operands.
424 elsif KP in N_Op_Boolean
425 or else KP in N_Short_Circuit
426 or else KP = N_Op_Not
427 then
428 return;
430 -- Otherwise we perform a conversion from the current type, which
431 -- must be Standard.Boolean, to the desired type. Use the base
432 -- type to prevent spurious constraint checks that are extraneous
433 -- to the transformation. The type and its base have the same
434 -- representation, standard or otherwise.
436 else
437 Set_Analyzed (N);
438 Rewrite (N, Convert_To (Base_Type (T), N));
439 Analyze_And_Resolve (N, Base_Type (T));
440 end if;
441 end;
442 end if;
443 end Adjust_Result_Type;
445 --------------------------
446 -- Append_Freeze_Action --
447 --------------------------
449 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
450 Fnode : Node_Id;
452 begin
453 Ensure_Freeze_Node (T);
454 Fnode := Freeze_Node (T);
456 if No (Actions (Fnode)) then
457 Set_Actions (Fnode, New_List (N));
458 else
459 Append (N, Actions (Fnode));
460 end if;
461 end Append_Freeze_Action;
463 ---------------------------
464 -- Append_Freeze_Actions --
465 ---------------------------
467 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
468 Fnode : Node_Id;
470 begin
471 if No (L) then
472 return;
473 end if;
475 Ensure_Freeze_Node (T);
476 Fnode := Freeze_Node (T);
478 if No (Actions (Fnode)) then
479 Set_Actions (Fnode, L);
480 else
481 Append_List (L, Actions (Fnode));
482 end if;
483 end Append_Freeze_Actions;
485 ----------------------------------------
486 -- Attribute_Constrained_Static_Value --
487 ----------------------------------------
489 function Attribute_Constrained_Static_Value (Pref : Node_Id) return Boolean
491 Ptyp : constant Entity_Id := Etype (Pref);
492 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
494 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
495 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
496 -- view of an aliased object whose subtype is constrained.
498 ---------------------------------
499 -- Is_Constrained_Aliased_View --
500 ---------------------------------
502 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
503 E : Entity_Id;
505 begin
506 if Is_Entity_Name (Obj) then
507 E := Entity (Obj);
509 if Present (Renamed_Object (E)) then
510 return Is_Constrained_Aliased_View (Renamed_Object (E));
511 else
512 return Is_Aliased (E) and then Is_Constrained (Etype (E));
513 end if;
515 else
516 return Is_Aliased_View (Obj)
517 and then
518 (Is_Constrained (Etype (Obj))
519 or else
520 (Nkind (Obj) = N_Explicit_Dereference
521 and then
522 not Object_Type_Has_Constrained_Partial_View
523 (Typ => Base_Type (Etype (Obj)),
524 Scop => Current_Scope)));
525 end if;
526 end Is_Constrained_Aliased_View;
528 -- Start of processing for Attribute_Constrained_Static_Value
530 begin
531 -- We are in a case where the attribute is known statically, and
532 -- implicit dereferences have been rewritten.
534 pragma Assert
535 (not (Present (Formal_Ent)
536 and then Ekind (Formal_Ent) /= E_Constant
537 and then Present (Extra_Constrained (Formal_Ent)))
538 and then
539 not (Is_Access_Type (Etype (Pref))
540 and then (not Is_Entity_Name (Pref)
541 or else Is_Object (Entity (Pref))))
542 and then
543 not (Nkind (Pref) = N_Identifier
544 and then Ekind (Entity (Pref)) = E_Variable
545 and then Present (Extra_Constrained (Entity (Pref)))));
547 if Is_Entity_Name (Pref) then
548 declare
549 Ent : constant Entity_Id := Entity (Pref);
550 Res : Boolean;
552 begin
553 -- (RM J.4) obsolescent cases
555 if Is_Type (Ent) then
557 -- Private type
559 if Is_Private_Type (Ent) then
560 Res := not Has_Discriminants (Ent)
561 or else Is_Constrained (Ent);
563 -- It not a private type, must be a generic actual type
564 -- that corresponded to a private type. We know that this
565 -- correspondence holds, since otherwise the reference
566 -- within the generic template would have been illegal.
568 else
569 if Is_Composite_Type (Underlying_Type (Ent)) then
570 Res := Is_Constrained (Ent);
571 else
572 Res := True;
573 end if;
574 end if;
576 else
578 -- If the prefix is not a variable or is aliased, then
579 -- definitely true; if it's a formal parameter without an
580 -- associated extra formal, then treat it as constrained.
582 -- Ada 2005 (AI-363): An aliased prefix must be known to be
583 -- constrained in order to set the attribute to True.
585 if not Is_Variable (Pref)
586 or else Present (Formal_Ent)
587 or else (Ada_Version < Ada_2005
588 and then Is_Aliased_View (Pref))
589 or else (Ada_Version >= Ada_2005
590 and then Is_Constrained_Aliased_View (Pref))
591 then
592 Res := True;
594 -- Variable case, look at type to see if it is constrained.
595 -- Note that the one case where this is not accurate (the
596 -- procedure formal case), has been handled above.
598 -- We use the Underlying_Type here (and below) in case the
599 -- type is private without discriminants, but the full type
600 -- has discriminants. This case is illegal, but we generate
601 -- it internally for passing to the Extra_Constrained
602 -- parameter.
604 else
605 -- In Ada 2012, test for case of a limited tagged type,
606 -- in which case the attribute is always required to
607 -- return True. The underlying type is tested, to make
608 -- sure we also return True for cases where there is an
609 -- unconstrained object with an untagged limited partial
610 -- view which has defaulted discriminants (such objects
611 -- always produce a False in earlier versions of
612 -- Ada). (Ada 2012: AI05-0214)
614 Res :=
615 Is_Constrained (Underlying_Type (Etype (Ent)))
616 or else
617 (Ada_Version >= Ada_2012
618 and then Is_Tagged_Type (Underlying_Type (Ptyp))
619 and then Is_Limited_Type (Ptyp));
620 end if;
621 end if;
623 return Res;
624 end;
626 -- Prefix is not an entity name. These are also cases where we can
627 -- always tell at compile time by looking at the form and type of the
628 -- prefix. If an explicit dereference of an object with constrained
629 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
630 -- underlying type is a limited tagged type, then Constrained is
631 -- required to always return True (Ada 2012: AI05-0214).
633 else
634 return not Is_Variable (Pref)
635 or else
636 (Nkind (Pref) = N_Explicit_Dereference
637 and then
638 not Object_Type_Has_Constrained_Partial_View
639 (Typ => Base_Type (Ptyp),
640 Scop => Current_Scope))
641 or else Is_Constrained (Underlying_Type (Ptyp))
642 or else (Ada_Version >= Ada_2012
643 and then Is_Tagged_Type (Underlying_Type (Ptyp))
644 and then Is_Limited_Type (Ptyp));
645 end if;
646 end Attribute_Constrained_Static_Value;
648 ------------------------------------
649 -- Build_Allocate_Deallocate_Proc --
650 ------------------------------------
652 procedure Build_Allocate_Deallocate_Proc
653 (N : Node_Id;
654 Is_Allocate : Boolean)
656 function Find_Object (E : Node_Id) return Node_Id;
657 -- Given an arbitrary expression of an allocator, try to find an object
658 -- reference in it, otherwise return the original expression.
660 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
661 -- Determine whether subprogram Subp denotes a custom allocate or
662 -- deallocate.
664 -----------------
665 -- Find_Object --
666 -----------------
668 function Find_Object (E : Node_Id) return Node_Id is
669 Expr : Node_Id;
671 begin
672 pragma Assert (Is_Allocate);
674 Expr := E;
675 loop
676 if Nkind (Expr) = N_Explicit_Dereference then
677 Expr := Prefix (Expr);
679 elsif Nkind (Expr) = N_Qualified_Expression then
680 Expr := Expression (Expr);
682 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
684 -- When interface class-wide types are involved in allocation,
685 -- the expander introduces several levels of address arithmetic
686 -- to perform dispatch table displacement. In this scenario the
687 -- object appears as:
689 -- Tag_Ptr (Base_Address (<object>'Address))
691 -- Detect this case and utilize the whole expression as the
692 -- "object" since it now points to the proper dispatch table.
694 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
695 exit;
697 -- Continue to strip the object
699 else
700 Expr := Expression (Expr);
701 end if;
703 else
704 exit;
705 end if;
706 end loop;
708 return Expr;
709 end Find_Object;
711 ---------------------------------
712 -- Is_Allocate_Deallocate_Proc --
713 ---------------------------------
715 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
716 begin
717 -- Look for a subprogram body with only one statement which is a
718 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
720 if Ekind (Subp) = E_Procedure
721 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
722 then
723 declare
724 HSS : constant Node_Id :=
725 Handled_Statement_Sequence (Parent (Parent (Subp)));
726 Proc : Entity_Id;
728 begin
729 if Present (Statements (HSS))
730 and then Nkind (First (Statements (HSS))) =
731 N_Procedure_Call_Statement
732 then
733 Proc := Entity (Name (First (Statements (HSS))));
735 return
736 Is_RTE (Proc, RE_Allocate_Any_Controlled)
737 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
738 end if;
739 end;
740 end if;
742 return False;
743 end Is_Allocate_Deallocate_Proc;
745 -- Local variables
747 Desig_Typ : Entity_Id;
748 Expr : Node_Id;
749 Needs_Fin : Boolean;
750 Pool_Id : Entity_Id;
751 Proc_To_Call : Node_Id := Empty;
752 Ptr_Typ : Entity_Id;
753 Use_Secondary_Stack_Pool : Boolean;
755 -- Start of processing for Build_Allocate_Deallocate_Proc
757 begin
758 -- Obtain the attributes of the allocation / deallocation
760 if Nkind (N) = N_Free_Statement then
761 Expr := Expression (N);
762 Ptr_Typ := Base_Type (Etype (Expr));
763 Proc_To_Call := Procedure_To_Call (N);
765 else
766 if Nkind (N) = N_Object_Declaration then
767 Expr := Expression (N);
768 else
769 Expr := N;
770 end if;
772 -- In certain cases an allocator with a qualified expression may
773 -- be relocated and used as the initialization expression of a
774 -- temporary:
776 -- before:
777 -- Obj : Ptr_Typ := new Desig_Typ'(...);
779 -- after:
780 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
781 -- Obj : Ptr_Typ := Tmp;
783 -- Since the allocator is always marked as analyzed to avoid infinite
784 -- expansion, it will never be processed by this routine given that
785 -- the designated type needs finalization actions. Detect this case
786 -- and complete the expansion of the allocator.
788 if Nkind (Expr) = N_Identifier
789 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
790 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
791 then
792 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
793 return;
794 end if;
796 -- The allocator may have been rewritten into something else in which
797 -- case the expansion performed by this routine does not apply.
799 if Nkind (Expr) /= N_Allocator then
800 return;
801 end if;
803 Ptr_Typ := Base_Type (Etype (Expr));
804 Proc_To_Call := Procedure_To_Call (Expr);
805 end if;
807 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
808 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
810 -- Handle concurrent types
812 if Is_Concurrent_Type (Desig_Typ)
813 and then Present (Corresponding_Record_Type (Desig_Typ))
814 then
815 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
816 end if;
818 Use_Secondary_Stack_Pool :=
819 Is_RTE (Pool_Id, RE_SS_Pool)
820 or else (Nkind (Expr) = N_Allocator
821 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool));
823 -- Do not process allocations / deallocations without a pool
825 if No (Pool_Id) then
826 return;
828 -- Do not process allocations on / deallocations from the secondary
829 -- stack, except for access types used to implement indirect temps.
831 elsif Use_Secondary_Stack_Pool
832 and then not Old_Attr_Util.Indirect_Temps
833 .Is_Access_Type_For_Indirect_Temp (Ptr_Typ)
834 then
835 return;
837 -- Optimize the case where we are using the default Global_Pool_Object,
838 -- and we don't need the heavy finalization machinery.
840 elsif Is_RTE (Pool_Id, RE_Global_Pool_Object)
841 and then not Needs_Finalization (Desig_Typ)
842 then
843 return;
845 -- Do not replicate the machinery if the allocator / free has already
846 -- been expanded and has a custom Allocate / Deallocate.
848 elsif Present (Proc_To_Call)
849 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
850 then
851 return;
852 end if;
854 -- Finalization actions are required when the object to be allocated or
855 -- deallocated needs these actions and the associated access type is not
856 -- subject to pragma No_Heap_Finalization.
858 Needs_Fin :=
859 Needs_Finalization (Desig_Typ)
860 and then not No_Heap_Finalization (Ptr_Typ);
862 if Needs_Fin then
864 -- Do nothing if the access type may never allocate / deallocate
865 -- objects.
867 if No_Pool_Assigned (Ptr_Typ) then
868 return;
869 end if;
871 -- The allocation / deallocation of a controlled object must be
872 -- chained on / detached from a finalization master.
874 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
876 -- The only other kind of allocation / deallocation supported by this
877 -- routine is on / from a subpool.
879 elsif Nkind (Expr) = N_Allocator
880 and then No (Subpool_Handle_Name (Expr))
881 then
882 return;
883 end if;
885 declare
886 Loc : constant Source_Ptr := Sloc (N);
887 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
888 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
889 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
890 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
892 Actuals : List_Id;
893 Fin_Addr_Id : Entity_Id;
894 Fin_Mas_Act : Node_Id;
895 Fin_Mas_Id : Entity_Id;
896 Proc_To_Call : Entity_Id;
897 Subpool : Node_Id := Empty;
899 begin
900 -- Step 1: Construct all the actuals for the call to library routine
901 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
903 -- a) Storage pool
905 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
907 if Is_Allocate then
909 -- b) Subpool
911 if Nkind (Expr) = N_Allocator then
912 Subpool := Subpool_Handle_Name (Expr);
913 end if;
915 -- If a subpool is present it can be an arbitrary name, so make
916 -- the actual by copying the tree.
918 if Present (Subpool) then
919 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
920 else
921 Append_To (Actuals, Make_Null (Loc));
922 end if;
924 -- c) Finalization master
926 if Needs_Fin then
927 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
928 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
930 -- Handle the case where the master is actually a pointer to a
931 -- master. This case arises in build-in-place functions.
933 if Is_Access_Type (Etype (Fin_Mas_Id)) then
934 Append_To (Actuals, Fin_Mas_Act);
935 else
936 Append_To (Actuals,
937 Make_Attribute_Reference (Loc,
938 Prefix => Fin_Mas_Act,
939 Attribute_Name => Name_Unrestricted_Access));
940 end if;
941 else
942 Append_To (Actuals, Make_Null (Loc));
943 end if;
945 -- d) Finalize_Address
947 -- Primitive Finalize_Address is never generated in CodePeer mode
948 -- since it contains an Unchecked_Conversion.
950 if Needs_Fin and then not CodePeer_Mode then
951 Fin_Addr_Id := Finalize_Address (Desig_Typ);
952 pragma Assert (Present (Fin_Addr_Id));
954 Append_To (Actuals,
955 Make_Attribute_Reference (Loc,
956 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
957 Attribute_Name => Name_Unrestricted_Access));
958 else
959 Append_To (Actuals, Make_Null (Loc));
960 end if;
961 end if;
963 -- e) Address
964 -- f) Storage_Size
965 -- g) Alignment
967 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
968 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
970 if (Is_Allocate or else not Is_Class_Wide_Type (Desig_Typ))
971 and then not Use_Secondary_Stack_Pool
972 then
973 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
975 -- For deallocation of class-wide types we obtain the value of
976 -- alignment from the Type Specific Record of the deallocated object.
977 -- This is needed because the frontend expansion of class-wide types
978 -- into equivalent types confuses the back end.
980 else
981 -- Generate:
982 -- Obj.all'Alignment
984 -- ... because 'Alignment applied to class-wide types is expanded
985 -- into the code that reads the value of alignment from the TSD
986 -- (see Expand_N_Attribute_Reference)
988 -- In the Use_Secondary_Stack_Pool case, Alig_Id is not
989 -- passed in and therefore must not be referenced.
991 Append_To (Actuals,
992 Unchecked_Convert_To (RTE (RE_Storage_Offset),
993 Make_Attribute_Reference (Loc,
994 Prefix =>
995 Make_Explicit_Dereference (Loc, Relocate_Node (Expr)),
996 Attribute_Name => Name_Alignment)));
997 end if;
999 -- h) Is_Controlled
1001 if Needs_Fin then
1002 Is_Controlled : declare
1003 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
1004 Flag_Expr : Node_Id;
1005 Param : Node_Id;
1006 Pref : Node_Id;
1007 Temp : Node_Id;
1009 begin
1010 if Is_Allocate then
1011 Temp := Find_Object (Expression (Expr));
1012 else
1013 Temp := Expr;
1014 end if;
1016 -- Processing for allocations where the expression is a subtype
1017 -- indication.
1019 if Is_Allocate
1020 and then Is_Entity_Name (Temp)
1021 and then Is_Type (Entity (Temp))
1022 then
1023 Flag_Expr :=
1024 New_Occurrence_Of
1025 (Boolean_Literals
1026 (Needs_Finalization (Entity (Temp))), Loc);
1028 -- The allocation / deallocation of a class-wide object relies
1029 -- on a runtime check to determine whether the object is truly
1030 -- controlled or not. Depending on this check, the finalization
1031 -- machinery will request or reclaim extra storage reserved for
1032 -- a list header.
1034 elsif Is_Class_Wide_Type (Desig_Typ) then
1036 -- Detect a special case where interface class-wide types
1037 -- are involved as the object appears as:
1039 -- Tag_Ptr (Base_Address (<object>'Address))
1041 -- The expression already yields the proper tag, generate:
1043 -- Temp.all
1045 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
1046 Param :=
1047 Make_Explicit_Dereference (Loc,
1048 Prefix => Relocate_Node (Temp));
1050 -- In the default case, obtain the tag of the object about
1051 -- to be allocated / deallocated. Generate:
1053 -- Temp'Tag
1055 -- If the object is an unchecked conversion (typically to
1056 -- an access to class-wide type), we must preserve the
1057 -- conversion to ensure that the object is seen as tagged
1058 -- in the code that follows.
1060 else
1061 Pref := Temp;
1063 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
1064 then
1065 Pref := Parent (Pref);
1066 end if;
1068 Param :=
1069 Make_Attribute_Reference (Loc,
1070 Prefix => Relocate_Node (Pref),
1071 Attribute_Name => Name_Tag);
1072 end if;
1074 -- Generate:
1075 -- Needs_Finalization (<Param>)
1077 Flag_Expr :=
1078 Make_Function_Call (Loc,
1079 Name =>
1080 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
1081 Parameter_Associations => New_List (Param));
1083 -- Processing for generic actuals
1085 elsif Is_Generic_Actual_Type (Desig_Typ) then
1086 Flag_Expr :=
1087 New_Occurrence_Of (Boolean_Literals
1088 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
1090 -- The object does not require any specialized checks, it is
1091 -- known to be controlled.
1093 else
1094 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
1095 end if;
1097 -- Create the temporary which represents the finalization state
1098 -- of the expression. Generate:
1100 -- F : constant Boolean := <Flag_Expr>;
1102 Insert_Action (N,
1103 Make_Object_Declaration (Loc,
1104 Defining_Identifier => Flag_Id,
1105 Constant_Present => True,
1106 Object_Definition =>
1107 New_Occurrence_Of (Standard_Boolean, Loc),
1108 Expression => Flag_Expr));
1110 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
1111 end Is_Controlled;
1113 -- The object is not controlled
1115 else
1116 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
1117 end if;
1119 -- i) On_Subpool
1121 if Is_Allocate then
1122 Append_To (Actuals,
1123 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
1124 end if;
1126 -- Step 2: Build a wrapper Allocate / Deallocate which internally
1127 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
1129 -- Select the proper routine to call
1131 if Is_Allocate then
1132 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
1133 else
1134 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
1135 end if;
1137 -- Create a custom Allocate / Deallocate routine which has identical
1138 -- profile to that of System.Storage_Pools.
1140 declare
1141 -- P : Root_Storage_Pool
1142 function Pool_Param return Node_Id is (
1143 Make_Parameter_Specification (Loc,
1144 Defining_Identifier => Make_Temporary (Loc, 'P'),
1145 Parameter_Type =>
1146 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)));
1148 -- A : [out] Address
1149 function Address_Param return Node_Id is (
1150 Make_Parameter_Specification (Loc,
1151 Defining_Identifier => Addr_Id,
1152 Out_Present => Is_Allocate,
1153 Parameter_Type =>
1154 New_Occurrence_Of (RTE (RE_Address), Loc)));
1156 -- S : Storage_Count
1157 function Size_Param return Node_Id is (
1158 Make_Parameter_Specification (Loc,
1159 Defining_Identifier => Size_Id,
1160 Parameter_Type =>
1161 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1163 -- L : Storage_Count
1164 function Alignment_Param return Node_Id is (
1165 Make_Parameter_Specification (Loc,
1166 Defining_Identifier => Alig_Id,
1167 Parameter_Type =>
1168 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1170 Formal_Params : List_Id;
1171 begin
1172 if Use_Secondary_Stack_Pool then
1173 -- Gigi expects a different profile in the Secondary_Stack_Pool
1174 -- case. There must be no uses of the two missing formals
1175 -- (i.e., Pool_Param and Alignment_Param) in this case.
1176 Formal_Params := New_List (Address_Param, Size_Param);
1177 else
1178 Formal_Params := New_List (
1179 Pool_Param, Address_Param, Size_Param, Alignment_Param);
1180 end if;
1182 Insert_Action (N,
1183 Make_Subprogram_Body (Loc,
1184 Specification =>
1185 -- procedure Pnn
1186 Make_Procedure_Specification (Loc,
1187 Defining_Unit_Name => Proc_Id,
1188 Parameter_Specifications => Formal_Params),
1190 Declarations => No_List,
1192 Handled_Statement_Sequence =>
1193 Make_Handled_Sequence_Of_Statements (Loc,
1194 Statements => New_List (
1195 Make_Procedure_Call_Statement (Loc,
1196 Name =>
1197 New_Occurrence_Of (Proc_To_Call, Loc),
1198 Parameter_Associations => Actuals)))),
1199 Suppress => All_Checks);
1200 end;
1202 -- The newly generated Allocate / Deallocate becomes the default
1203 -- procedure to call when the back end processes the allocation /
1204 -- deallocation.
1206 if Is_Allocate then
1207 Set_Procedure_To_Call (Expr, Proc_Id);
1208 else
1209 Set_Procedure_To_Call (N, Proc_Id);
1210 end if;
1211 end;
1212 end Build_Allocate_Deallocate_Proc;
1214 -------------------------------
1215 -- Build_Abort_Undefer_Block --
1216 -------------------------------
1218 function Build_Abort_Undefer_Block
1219 (Loc : Source_Ptr;
1220 Stmts : List_Id;
1221 Context : Node_Id) return Node_Id
1223 Exceptions_OK : constant Boolean :=
1224 not Restriction_Active (No_Exception_Propagation);
1226 AUD : Entity_Id;
1227 Blk : Node_Id;
1228 Blk_Id : Entity_Id;
1229 HSS : Node_Id;
1231 begin
1232 -- The block should be generated only when undeferring abort in the
1233 -- context of a potential exception.
1235 pragma Assert (Abort_Allowed and Exceptions_OK);
1237 -- Generate:
1238 -- begin
1239 -- <Stmts>
1240 -- at end
1241 -- Abort_Undefer_Direct;
1242 -- end;
1244 AUD := RTE (RE_Abort_Undefer_Direct);
1246 HSS :=
1247 Make_Handled_Sequence_Of_Statements (Loc,
1248 Statements => Stmts,
1249 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1251 Blk :=
1252 Make_Block_Statement (Loc,
1253 Handled_Statement_Sequence => HSS);
1254 Set_Is_Abort_Block (Blk);
1256 Add_Block_Identifier (Blk, Blk_Id);
1257 Expand_At_End_Handler (HSS, Blk_Id);
1259 -- Present the Abort_Undefer_Direct function to the back end to inline
1260 -- the call to the routine.
1262 Add_Inlined_Body (AUD, Context);
1264 return Blk;
1265 end Build_Abort_Undefer_Block;
1267 ---------------------------------
1268 -- Build_Class_Wide_Expression --
1269 ---------------------------------
1271 procedure Build_Class_Wide_Expression
1272 (Pragma_Or_Expr : Node_Id;
1273 Subp : Entity_Id;
1274 Par_Subp : Entity_Id;
1275 Adjust_Sloc : Boolean)
1277 function Replace_Entity (N : Node_Id) return Traverse_Result;
1278 -- Replace reference to formal of inherited operation or to primitive
1279 -- operation of root type, with corresponding entity for derived type,
1280 -- when constructing the class-wide condition of an overriding
1281 -- subprogram.
1283 --------------------
1284 -- Replace_Entity --
1285 --------------------
1287 function Replace_Entity (N : Node_Id) return Traverse_Result is
1288 New_E : Entity_Id;
1290 begin
1291 if Adjust_Sloc then
1292 Adjust_Inherited_Pragma_Sloc (N);
1293 end if;
1295 if Nkind (N) in N_Identifier | N_Expanded_Name | N_Operator_Symbol
1296 and then Present (Entity (N))
1297 and then
1298 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1299 and then
1300 (Nkind (Parent (N)) /= N_Attribute_Reference
1301 or else Attribute_Name (Parent (N)) /= Name_Class)
1302 then
1303 -- The replacement does not apply to dispatching calls within the
1304 -- condition, but only to calls whose static tag is that of the
1305 -- parent type.
1307 if Is_Subprogram (Entity (N))
1308 and then Nkind (Parent (N)) = N_Function_Call
1309 and then Present (Controlling_Argument (Parent (N)))
1310 then
1311 return OK;
1312 end if;
1314 -- Determine whether entity has a renaming
1316 New_E := Type_Map.Get (Entity (N));
1318 if Present (New_E) then
1319 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1320 end if;
1322 -- Update type of function call node, which should be the same as
1323 -- the function's return type.
1325 if Is_Subprogram (Entity (N))
1326 and then Nkind (Parent (N)) = N_Function_Call
1327 then
1328 Set_Etype (Parent (N), Etype (Entity (N)));
1329 end if;
1331 -- The whole expression will be reanalyzed
1333 elsif Nkind (N) in N_Has_Etype then
1334 Set_Analyzed (N, False);
1335 end if;
1337 return OK;
1338 end Replace_Entity;
1340 procedure Replace_Condition_Entities is
1341 new Traverse_Proc (Replace_Entity);
1343 -- Local variables
1345 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Par_Subp);
1346 Subp_Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1348 -- Start of processing for Build_Class_Wide_Expression
1350 begin
1351 pragma Assert (Par_Typ /= Subp_Typ);
1353 Update_Primitives_Mapping (Par_Subp, Subp);
1354 Map_Formals (Par_Subp, Subp);
1355 Replace_Condition_Entities (Pragma_Or_Expr);
1356 end Build_Class_Wide_Expression;
1358 --------------------
1359 -- Build_DIC_Call --
1360 --------------------
1362 function Build_DIC_Call
1363 (Loc : Source_Ptr;
1364 Obj_Name : Node_Id;
1365 Typ : Entity_Id) return Node_Id
1367 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1368 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1370 begin
1371 -- The DIC procedure has a null body if assertions are disabled or
1372 -- Assertion_Policy Ignore is in effect. In that case, it would be
1373 -- nice to generate a null statement instead of a call to the DIC
1374 -- procedure, but doing that seems to interfere with the determination
1375 -- of ECRs (early call regions) in SPARK. ???
1377 return
1378 Make_Procedure_Call_Statement (Loc,
1379 Name => New_Occurrence_Of (Proc_Id, Loc),
1380 Parameter_Associations => New_List (
1381 Unchecked_Convert_To (Formal_Typ, Obj_Name)));
1382 end Build_DIC_Call;
1384 ------------------------------
1385 -- Build_DIC_Procedure_Body --
1386 ------------------------------
1388 -- WARNING: This routine manages Ghost regions. Return statements must be
1389 -- replaced by gotos which jump to the end of the routine and restore the
1390 -- Ghost mode.
1392 procedure Build_DIC_Procedure_Body
1393 (Typ : Entity_Id;
1394 Partial_DIC : Boolean := False)
1396 Pragmas_Seen : Elist_Id := No_Elist;
1397 -- This list contains all DIC pragmas processed so far. The list is used
1398 -- to avoid redundant Default_Initial_Condition checks.
1400 procedure Add_DIC_Check
1401 (DIC_Prag : Node_Id;
1402 DIC_Expr : Node_Id;
1403 Stmts : in out List_Id);
1404 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1405 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1406 -- is added to list Stmts.
1408 procedure Add_Inherited_DIC
1409 (DIC_Prag : Node_Id;
1410 Par_Typ : Entity_Id;
1411 Deriv_Typ : Entity_Id;
1412 Stmts : in out List_Id);
1413 -- Add a runtime check to verify the assertion expression of inherited
1414 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1415 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1416 -- pragma. All generated code is added to list Stmts.
1418 procedure Add_Inherited_Tagged_DIC
1419 (DIC_Prag : Node_Id;
1420 Expr : Node_Id;
1421 Stmts : in out List_Id);
1422 -- Add a runtime check to verify assertion expression DIC_Expr of
1423 -- inherited pragma DIC_Prag. This routine applies class-wide pre-
1424 -- and postcondition-like runtime semantics to the check. Expr is
1425 -- the assertion expression after substitition has been performed
1426 -- (via Replace_References). All generated code is added to list Stmts.
1428 procedure Add_Inherited_DICs
1429 (T : Entity_Id;
1430 Priv_Typ : Entity_Id;
1431 Full_Typ : Entity_Id;
1432 Obj_Id : Entity_Id;
1433 Checks : in out List_Id);
1434 -- Generate a DIC check for each inherited Default_Initial_Condition
1435 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
1436 -- the partial and full view of the parent type. Obj_Id denotes the
1437 -- entity of the _object formal parameter of the DIC procedure. All
1438 -- created checks are added to list Checks.
1440 procedure Add_Own_DIC
1441 (DIC_Prag : Node_Id;
1442 DIC_Typ : Entity_Id;
1443 Obj_Id : Entity_Id;
1444 Stmts : in out List_Id);
1445 -- Add a runtime check to verify the assertion expression of pragma
1446 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. Obj_Id is the
1447 -- object to substitute in the assertion expression for any references
1448 -- to the current instance of the type All generated code is added to
1449 -- list Stmts.
1451 procedure Add_Parent_DICs
1452 (T : Entity_Id;
1453 Obj_Id : Entity_Id;
1454 Checks : in out List_Id);
1455 -- Generate a Default_Initial_Condition check for each inherited DIC
1456 -- aspect coming from all parent types of type T. Obj_Id denotes the
1457 -- entity of the _object formal parameter of the DIC procedure. All
1458 -- created checks are added to list Checks.
1460 -------------------
1461 -- Add_DIC_Check --
1462 -------------------
1464 procedure Add_DIC_Check
1465 (DIC_Prag : Node_Id;
1466 DIC_Expr : Node_Id;
1467 Stmts : in out List_Id)
1469 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1470 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1472 begin
1473 -- The DIC pragma is ignored, nothing left to do
1475 if Is_Ignored (DIC_Prag) then
1476 null;
1478 -- Otherwise the DIC expression must be checked at run time.
1479 -- Generate:
1481 -- pragma Check (<Nam>, <DIC_Expr>);
1483 else
1484 Append_New_To (Stmts,
1485 Make_Pragma (Loc,
1486 Pragma_Identifier =>
1487 Make_Identifier (Loc, Name_Check),
1489 Pragma_Argument_Associations => New_List (
1490 Make_Pragma_Argument_Association (Loc,
1491 Expression => Make_Identifier (Loc, Nam)),
1493 Make_Pragma_Argument_Association (Loc,
1494 Expression => DIC_Expr))));
1495 end if;
1497 -- Add the pragma to the list of processed pragmas
1499 Append_New_Elmt (DIC_Prag, Pragmas_Seen);
1500 end Add_DIC_Check;
1502 -----------------------
1503 -- Add_Inherited_DIC --
1504 -----------------------
1506 procedure Add_Inherited_DIC
1507 (DIC_Prag : Node_Id;
1508 Par_Typ : Entity_Id;
1509 Deriv_Typ : Entity_Id;
1510 Stmts : in out List_Id)
1512 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1513 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1514 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1515 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1516 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1518 begin
1519 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1521 -- Verify the inherited DIC assertion expression by calling the DIC
1522 -- procedure of the parent type.
1524 -- Generate:
1525 -- <Par_Typ>DIC (Par_Typ (_object));
1527 Append_New_To (Stmts,
1528 Make_Procedure_Call_Statement (Loc,
1529 Name => New_Occurrence_Of (Par_Proc, Loc),
1530 Parameter_Associations => New_List (
1531 Convert_To
1532 (Typ => Etype (Par_Obj),
1533 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1534 end Add_Inherited_DIC;
1536 ------------------------------
1537 -- Add_Inherited_Tagged_DIC --
1538 ------------------------------
1540 procedure Add_Inherited_Tagged_DIC
1541 (DIC_Prag : Node_Id;
1542 Expr : Node_Id;
1543 Stmts : in out List_Id)
1545 begin
1546 -- Once the DIC assertion expression is fully processed, add a check
1547 -- to the statements of the DIC procedure.
1549 Add_DIC_Check
1550 (DIC_Prag => DIC_Prag,
1551 DIC_Expr => Expr,
1552 Stmts => Stmts);
1553 end Add_Inherited_Tagged_DIC;
1555 ------------------------
1556 -- Add_Inherited_DICs --
1557 ------------------------
1559 procedure Add_Inherited_DICs
1560 (T : Entity_Id;
1561 Priv_Typ : Entity_Id;
1562 Full_Typ : Entity_Id;
1563 Obj_Id : Entity_Id;
1564 Checks : in out List_Id)
1566 Deriv_Typ : Entity_Id;
1567 Expr : Node_Id;
1568 Prag : Node_Id;
1569 Prag_Expr : Node_Id;
1570 Prag_Expr_Arg : Node_Id;
1571 Prag_Typ : Node_Id;
1572 Prag_Typ_Arg : Node_Id;
1574 Par_Proc : Entity_Id;
1575 -- The "partial" invariant procedure of Par_Typ
1577 Par_Typ : Entity_Id;
1578 -- The suitable view of the parent type used in the substitution of
1579 -- type attributes.
1581 begin
1582 if not Present (Priv_Typ) and then not Present (Full_Typ) then
1583 return;
1584 end if;
1586 -- When the type inheriting the class-wide invariant is a concurrent
1587 -- type, use the corresponding record type because it contains all
1588 -- primitive operations of the concurrent type and allows for proper
1589 -- substitution.
1591 if Is_Concurrent_Type (T) then
1592 Deriv_Typ := Corresponding_Record_Type (T);
1593 else
1594 Deriv_Typ := T;
1595 end if;
1597 pragma Assert (Present (Deriv_Typ));
1599 -- Determine which rep item chain to use. Precedence is given to that
1600 -- of the parent type's partial view since it usually carries all the
1601 -- class-wide invariants.
1603 if Present (Priv_Typ) then
1604 Prag := First_Rep_Item (Priv_Typ);
1605 else
1606 Prag := First_Rep_Item (Full_Typ);
1607 end if;
1609 while Present (Prag) loop
1610 if Nkind (Prag) = N_Pragma
1611 and then Pragma_Name (Prag) = Name_Default_Initial_Condition
1612 then
1613 -- Nothing to do if the pragma was already processed
1615 if Contains (Pragmas_Seen, Prag) then
1616 return;
1617 end if;
1619 -- Extract arguments of the Default_Initial_Condition pragma
1621 Prag_Expr_Arg := First (Pragma_Argument_Associations (Prag));
1622 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
1624 -- Pick up the implicit second argument of the pragma, which
1625 -- indicates the type that the pragma applies to.
1627 Prag_Typ_Arg := Next (Prag_Expr_Arg);
1628 if Present (Prag_Typ_Arg) then
1629 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
1630 else
1631 Prag_Typ := Empty;
1632 end if;
1634 -- The pragma applies to the partial view of the parent type
1636 if Present (Priv_Typ)
1637 and then Present (Prag_Typ)
1638 and then Entity (Prag_Typ) = Priv_Typ
1639 then
1640 Par_Typ := Priv_Typ;
1642 -- The pragma applies to the full view of the parent type
1644 elsif Present (Full_Typ)
1645 and then Present (Prag_Typ)
1646 and then Entity (Prag_Typ) = Full_Typ
1647 then
1648 Par_Typ := Full_Typ;
1650 -- Otherwise the pragma does not belong to the parent type and
1651 -- should not be considered.
1653 else
1654 return;
1655 end if;
1657 -- Substitute references in the DIC expression that are related
1658 -- to the partial type with corresponding references related to
1659 -- the derived type (call to Replace_References below).
1661 Expr := New_Copy_Tree (Prag_Expr);
1663 Par_Proc := Partial_DIC_Procedure (Par_Typ);
1665 -- If there's not a partial DIC procedure (such as when a
1666 -- full type doesn't have its own DIC, but is inherited from
1667 -- a type with DIC), get the full DIC procedure.
1669 if not Present (Par_Proc) then
1670 Par_Proc := DIC_Procedure (Par_Typ);
1671 end if;
1673 Replace_References
1674 (Expr => Expr,
1675 Par_Typ => Par_Typ,
1676 Deriv_Typ => Deriv_Typ,
1677 Par_Obj => First_Formal (Par_Proc),
1678 Deriv_Obj => Obj_Id);
1680 -- Why are there different actions depending on whether T is
1681 -- tagged? Can these be unified? ???
1683 if Is_Tagged_Type (T) then
1684 Add_Inherited_Tagged_DIC
1685 (DIC_Prag => Prag,
1686 Expr => Expr,
1687 Stmts => Checks);
1689 else
1690 Add_Inherited_DIC
1691 (DIC_Prag => Prag,
1692 Par_Typ => Par_Typ,
1693 Deriv_Typ => Deriv_Typ,
1694 Stmts => Checks);
1695 end if;
1697 -- Leave as soon as we get a DIC pragma, since we'll visit
1698 -- the pragmas of the parents, so will get to any "inherited"
1699 -- pragmas that way.
1701 return;
1702 end if;
1704 Next_Rep_Item (Prag);
1705 end loop;
1706 end Add_Inherited_DICs;
1708 -----------------
1709 -- Add_Own_DIC --
1710 -----------------
1712 procedure Add_Own_DIC
1713 (DIC_Prag : Node_Id;
1714 DIC_Typ : Entity_Id;
1715 Obj_Id : Entity_Id;
1716 Stmts : in out List_Id)
1718 DIC_Args : constant List_Id :=
1719 Pragma_Argument_Associations (DIC_Prag);
1720 DIC_Arg : constant Node_Id := First (DIC_Args);
1721 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1722 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1724 -- Local variables
1726 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1728 Expr : Node_Id;
1730 -- Start of processing for Add_Own_DIC
1732 begin
1733 pragma Assert (Present (DIC_Expr));
1734 Expr := New_Copy_Tree (DIC_Expr);
1736 -- Perform the following substitution:
1738 -- * Replace the current instance of DIC_Typ with a reference to
1739 -- the _object formal parameter of the DIC procedure.
1741 Replace_Type_References
1742 (Expr => Expr,
1743 Typ => DIC_Typ,
1744 Obj_Id => Obj_Id);
1746 -- Preanalyze the DIC expression to detect errors and at the same
1747 -- time capture the visibility of the proper package part.
1749 Set_Parent (Expr, Typ_Decl);
1750 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1752 -- Save a copy of the expression with all replacements and analysis
1753 -- already taken place in case a derived type inherits the pragma.
1754 -- The copy will be used as the foundation of the derived type's own
1755 -- version of the DIC assertion expression.
1757 if Is_Tagged_Type (DIC_Typ) then
1758 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1759 end if;
1761 -- If the pragma comes from an aspect specification, replace the
1762 -- saved expression because all type references must be substituted
1763 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1764 -- routines.
1766 if Present (DIC_Asp) then
1767 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1768 end if;
1770 -- Once the DIC assertion expression is fully processed, add a check
1771 -- to the statements of the DIC procedure (unless the type is an
1772 -- abstract type, in which case we don't want the possibility of
1773 -- generating a call to an abstract function of the type; such DIC
1774 -- procedures can never be called in any case, so not generating the
1775 -- check at all is OK).
1777 if not Is_Abstract_Type (DIC_Typ) or else GNATprove_Mode then
1778 Add_DIC_Check
1779 (DIC_Prag => DIC_Prag,
1780 DIC_Expr => Expr,
1781 Stmts => Stmts);
1782 end if;
1783 end Add_Own_DIC;
1785 ---------------------
1786 -- Add_Parent_DICs --
1787 ---------------------
1789 procedure Add_Parent_DICs
1790 (T : Entity_Id;
1791 Obj_Id : Entity_Id;
1792 Checks : in out List_Id)
1794 Dummy_1 : Entity_Id;
1795 Dummy_2 : Entity_Id;
1797 Curr_Typ : Entity_Id;
1798 -- The entity of the current type being examined
1800 Full_Typ : Entity_Id;
1801 -- The full view of Par_Typ
1803 Par_Typ : Entity_Id;
1804 -- The entity of the parent type
1806 Priv_Typ : Entity_Id;
1807 -- The partial view of Par_Typ
1809 Op_Node : Elmt_Id;
1810 Par_Prim : Entity_Id;
1811 Prim : Entity_Id;
1813 begin
1814 -- Map the overridden primitive to the overriding one; required by
1815 -- Replace_References (called by Add_Inherited_DICs) to handle calls
1816 -- to parent primitives.
1818 Op_Node := First_Elmt (Primitive_Operations (T));
1819 while Present (Op_Node) loop
1820 Prim := Node (Op_Node);
1822 if Present (Overridden_Operation (Prim))
1823 and then Comes_From_Source (Prim)
1824 then
1825 Par_Prim := Overridden_Operation (Prim);
1827 -- Create a mapping of the form:
1828 -- parent type primitive -> derived type primitive
1830 Type_Map.Set (Par_Prim, Prim);
1831 end if;
1833 Next_Elmt (Op_Node);
1834 end loop;
1836 -- Climb the parent type chain
1838 Curr_Typ := T;
1839 loop
1840 -- Do not consider subtypes, as they inherit the DICs from their
1841 -- base types.
1843 Par_Typ := Base_Type (Etype (Base_Type (Curr_Typ)));
1845 -- Stop the climb once the root of the parent chain is
1846 -- reached.
1848 exit when Curr_Typ = Par_Typ;
1850 -- Process the DICs of the parent type
1852 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
1854 -- Only try to inherit a DIC pragma from the parent type Par_Typ
1855 -- if it Has_Own_DIC pragma. The loop will proceed up the parent
1856 -- chain to find all types that have their own DIC.
1858 if Has_Own_DIC (Par_Typ) then
1859 Add_Inherited_DICs
1860 (T => T,
1861 Priv_Typ => Priv_Typ,
1862 Full_Typ => Full_Typ,
1863 Obj_Id => Obj_Id,
1864 Checks => Checks);
1865 end if;
1867 Curr_Typ := Par_Typ;
1868 end loop;
1869 end Add_Parent_DICs;
1871 -- Local variables
1873 Loc : constant Source_Ptr := Sloc (Typ);
1875 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1876 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1877 -- Save the Ghost-related attributes to restore on exit
1879 DIC_Prag : Node_Id;
1880 DIC_Typ : Entity_Id;
1881 Dummy_1 : Entity_Id;
1882 Dummy_2 : Entity_Id;
1883 Proc_Body : Node_Id;
1884 Proc_Body_Id : Entity_Id;
1885 Proc_Decl : Node_Id;
1886 Proc_Id : Entity_Id;
1887 Stmts : List_Id := No_List;
1889 CRec_Typ : Entity_Id := Empty;
1890 -- The corresponding record type of Full_Typ
1892 Full_Typ : Entity_Id := Empty;
1893 -- The full view of the working type
1895 Obj_Id : Entity_Id := Empty;
1896 -- The _object formal parameter of the invariant procedure
1898 Part_Proc : Entity_Id := Empty;
1899 -- The entity of the "partial" invariant procedure
1901 Priv_Typ : Entity_Id := Empty;
1902 -- The partial view of the working type
1904 Work_Typ : Entity_Id;
1905 -- The working type
1907 -- Start of processing for Build_DIC_Procedure_Body
1909 begin
1910 Work_Typ := Base_Type (Typ);
1912 -- Do not process class-wide types as these are Itypes, but lack a first
1913 -- subtype (see below).
1915 if Is_Class_Wide_Type (Work_Typ) then
1916 return;
1918 -- Do not process the underlying full view of a private type. There is
1919 -- no way to get back to the partial view, plus the body will be built
1920 -- by the full view or the base type.
1922 elsif Is_Underlying_Full_View (Work_Typ) then
1923 return;
1925 -- Use the first subtype when dealing with various base types
1927 elsif Is_Itype (Work_Typ) then
1928 Work_Typ := First_Subtype (Work_Typ);
1930 -- The input denotes the corresponding record type of a protected or a
1931 -- task type. Work with the concurrent type because the corresponding
1932 -- record type may not be visible to clients of the type.
1934 elsif Ekind (Work_Typ) = E_Record_Type
1935 and then Is_Concurrent_Record_Type (Work_Typ)
1936 then
1937 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
1938 end if;
1940 -- The working type may be subject to pragma Ghost. Set the mode now to
1941 -- ensure that the DIC procedure is properly marked as Ghost.
1943 Set_Ghost_Mode (Work_Typ);
1945 -- The working type must be either define a DIC pragma of its own or
1946 -- inherit one from a parent type.
1948 pragma Assert (Has_DIC (Work_Typ));
1950 -- Recover the type which defines the DIC pragma. This is either the
1951 -- working type itself or a parent type when the pragma is inherited.
1953 DIC_Typ := Find_DIC_Type (Work_Typ);
1954 pragma Assert (Present (DIC_Typ));
1956 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
1957 pragma Assert (Present (DIC_Prag));
1959 -- Nothing to do if pragma DIC appears without an argument or its sole
1960 -- argument is "null".
1962 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
1963 goto Leave;
1964 end if;
1966 -- Obtain both views of the type
1968 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy_1, CRec_Typ);
1970 -- The caller requests a body for the partial DIC procedure
1972 if Partial_DIC then
1973 Proc_Id := Partial_DIC_Procedure (Work_Typ);
1975 -- The "full" DIC procedure body was already created
1977 -- Create a declaration for the "partial" DIC procedure if it
1978 -- is not available.
1980 if No (Proc_Id) then
1981 Build_DIC_Procedure_Declaration
1982 (Typ => Work_Typ,
1983 Partial_DIC => True);
1985 Proc_Id := Partial_DIC_Procedure (Work_Typ);
1986 end if;
1988 -- The caller requests a body for the "full" DIC procedure
1990 else
1991 Proc_Id := DIC_Procedure (Work_Typ);
1992 Part_Proc := Partial_DIC_Procedure (Work_Typ);
1994 -- Create a declaration for the "full" DIC procedure if it is
1995 -- not available.
1997 if No (Proc_Id) then
1998 Build_DIC_Procedure_Declaration (Work_Typ);
1999 Proc_Id := DIC_Procedure (Work_Typ);
2000 end if;
2001 end if;
2003 -- At this point there should be a DIC procedure declaration
2005 pragma Assert (Present (Proc_Id));
2006 Proc_Decl := Unit_Declaration_Node (Proc_Id);
2008 -- Nothing to do if the DIC procedure already has a body
2010 if Present (Corresponding_Body (Proc_Decl)) then
2011 goto Leave;
2012 end if;
2014 -- Emulate the environment of the DIC procedure by installing its scope
2015 -- and formal parameters.
2017 Push_Scope (Proc_Id);
2018 Install_Formals (Proc_Id);
2020 Obj_Id := First_Formal (Proc_Id);
2021 pragma Assert (Present (Obj_Id));
2023 -- The "partial" DIC procedure verifies the DICs of the partial view
2024 -- only.
2026 if Partial_DIC then
2027 pragma Assert (Present (Priv_Typ));
2029 if Has_Own_DIC (Work_Typ) then -- If we're testing this then maybe
2030 Add_Own_DIC -- we shouldn't be calling Find_DIC_Typ above???
2031 (DIC_Prag => DIC_Prag,
2032 DIC_Typ => DIC_Typ, -- Should this just be Work_Typ???
2033 Obj_Id => Obj_Id,
2034 Stmts => Stmts);
2035 end if;
2037 -- Otherwise, the "full" DIC procedure verifies the DICs inherited from
2038 -- parent types, as well as indirectly verifying the DICs of the partial
2039 -- view by calling the "partial" DIC procedure.
2041 else
2042 -- Check the DIC of the partial view by calling the "partial" DIC
2043 -- procedure, unless the partial DIC body is empty. Generate:
2045 -- <Work_Typ>Partial_DIC (_object);
2047 if Present (Part_Proc) and then not Has_Null_Body (Part_Proc) then
2048 Append_New_To (Stmts,
2049 Make_Procedure_Call_Statement (Loc,
2050 Name => New_Occurrence_Of (Part_Proc, Loc),
2051 Parameter_Associations => New_List (
2052 New_Occurrence_Of (Obj_Id, Loc))));
2053 end if;
2055 -- Process inherited Default_Initial_Conditions for all parent types
2057 Add_Parent_DICs (Work_Typ, Obj_Id, Stmts);
2058 end if;
2060 End_Scope;
2062 -- Produce an empty completing body in the following cases:
2063 -- * Assertions are disabled
2064 -- * The DIC Assertion_Policy is Ignore
2066 if No (Stmts) then
2067 Stmts := New_List (Make_Null_Statement (Loc));
2068 end if;
2070 -- Generate:
2071 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
2072 -- begin
2073 -- <Stmts>
2074 -- end <Work_Typ>DIC;
2076 Proc_Body :=
2077 Make_Subprogram_Body (Loc,
2078 Specification =>
2079 Copy_Subprogram_Spec (Parent (Proc_Id)),
2080 Declarations => Empty_List,
2081 Handled_Statement_Sequence =>
2082 Make_Handled_Sequence_Of_Statements (Loc,
2083 Statements => Stmts));
2084 Proc_Body_Id := Defining_Entity (Proc_Body);
2086 -- Perform minor decoration in case the body is not analyzed
2088 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
2089 Set_Etype (Proc_Body_Id, Standard_Void_Type);
2090 Set_Scope (Proc_Body_Id, Current_Scope);
2091 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
2092 Set_SPARK_Pragma_Inherited
2093 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
2095 -- Link both spec and body to avoid generating duplicates
2097 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
2098 Set_Corresponding_Spec (Proc_Body, Proc_Id);
2100 -- The body should not be inserted into the tree when the context
2101 -- is a generic unit because it is not part of the template.
2102 -- Note that the body must still be generated in order to resolve the
2103 -- DIC assertion expression.
2105 if Inside_A_Generic then
2106 null;
2108 -- Semi-insert the body into the tree for GNATprove by setting its
2109 -- Parent field. This allows for proper upstream tree traversals.
2111 elsif GNATprove_Mode then
2112 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
2114 -- Otherwise the body is part of the freezing actions of the working
2115 -- type.
2117 else
2118 Append_Freeze_Action (Work_Typ, Proc_Body);
2119 end if;
2121 <<Leave>>
2122 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2123 end Build_DIC_Procedure_Body;
2125 -------------------------------------
2126 -- Build_DIC_Procedure_Declaration --
2127 -------------------------------------
2129 -- WARNING: This routine manages Ghost regions. Return statements must be
2130 -- replaced by gotos which jump to the end of the routine and restore the
2131 -- Ghost mode.
2133 procedure Build_DIC_Procedure_Declaration
2134 (Typ : Entity_Id;
2135 Partial_DIC : Boolean := False)
2137 Loc : constant Source_Ptr := Sloc (Typ);
2139 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2140 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2141 -- Save the Ghost-related attributes to restore on exit
2143 DIC_Prag : Node_Id;
2144 DIC_Typ : Entity_Id;
2145 Proc_Decl : Node_Id;
2146 Proc_Id : Entity_Id;
2147 Proc_Nam : Name_Id;
2148 Typ_Decl : Node_Id;
2150 CRec_Typ : Entity_Id;
2151 -- The corresponding record type of Full_Typ
2153 Full_Typ : Entity_Id;
2154 -- The full view of working type
2156 Obj_Id : Entity_Id;
2157 -- The _object formal parameter of the DIC procedure
2159 Priv_Typ : Entity_Id;
2160 -- The partial view of working type
2162 UFull_Typ : Entity_Id;
2163 -- The underlying full view of Full_Typ
2165 Work_Typ : Entity_Id;
2166 -- The working type
2168 begin
2169 Work_Typ := Base_Type (Typ);
2171 -- Do not process class-wide types as these are Itypes, but lack a first
2172 -- subtype (see below).
2174 if Is_Class_Wide_Type (Work_Typ) then
2175 return;
2177 -- Do not process the underlying full view of a private type. There is
2178 -- no way to get back to the partial view, plus the body will be built
2179 -- by the full view or the base type.
2181 elsif Is_Underlying_Full_View (Work_Typ) then
2182 return;
2184 -- Use the first subtype when dealing with various base types
2186 elsif Is_Itype (Work_Typ) then
2187 Work_Typ := First_Subtype (Work_Typ);
2189 -- The input denotes the corresponding record type of a protected or a
2190 -- task type. Work with the concurrent type because the corresponding
2191 -- record type may not be visible to clients of the type.
2193 elsif Ekind (Work_Typ) = E_Record_Type
2194 and then Is_Concurrent_Record_Type (Work_Typ)
2195 then
2196 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2197 end if;
2199 -- The working type may be subject to pragma Ghost. Set the mode now to
2200 -- ensure that the DIC procedure is properly marked as Ghost.
2202 Set_Ghost_Mode (Work_Typ);
2204 -- The type must be either subject to a DIC pragma or inherit one from a
2205 -- parent type.
2207 pragma Assert (Has_DIC (Work_Typ));
2209 -- Recover the type which defines the DIC pragma. This is either the
2210 -- working type itself or a parent type when the pragma is inherited.
2212 DIC_Typ := Find_DIC_Type (Work_Typ);
2213 pragma Assert (Present (DIC_Typ));
2215 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2216 pragma Assert (Present (DIC_Prag));
2218 -- Nothing to do if pragma DIC appears without an argument or its sole
2219 -- argument is "null".
2221 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2222 goto Leave;
2223 end if;
2225 -- Nothing to do if the type already has a "partial" DIC procedure
2227 if Partial_DIC then
2228 if Present (Partial_DIC_Procedure (Work_Typ)) then
2229 goto Leave;
2230 end if;
2232 -- Nothing to do if the type already has a "full" DIC procedure
2234 elsif Present (DIC_Procedure (Work_Typ)) then
2235 goto Leave;
2236 end if;
2238 -- The caller requests the declaration of the "partial" DIC procedure
2240 if Partial_DIC then
2241 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_DIC");
2243 -- Otherwise the caller requests the declaration of the "full" DIC
2244 -- procedure.
2246 else
2247 Proc_Nam := New_External_Name (Chars (Work_Typ), "DIC");
2248 end if;
2250 Proc_Id :=
2251 Make_Defining_Identifier (Loc, Chars => Proc_Nam);
2253 -- Perform minor decoration in case the declaration is not analyzed
2255 Mutate_Ekind (Proc_Id, E_Procedure);
2256 Set_Etype (Proc_Id, Standard_Void_Type);
2257 Set_Is_DIC_Procedure (Proc_Id);
2258 Set_Scope (Proc_Id, Current_Scope);
2259 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
2260 Set_SPARK_Pragma_Inherited (Proc_Id);
2262 Set_DIC_Procedure (Work_Typ, Proc_Id);
2264 -- The DIC procedure requires debug info when the assertion expression
2265 -- is subject to Source Coverage Obligations.
2267 if Generate_SCO then
2268 Set_Debug_Info_Needed (Proc_Id);
2269 end if;
2271 -- Obtain all views of the input type
2273 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
2275 -- Associate the DIC procedure and various flags with all views
2277 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
2278 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
2279 Propagate_DIC_Attributes (UFull_Typ, From_Typ => Work_Typ);
2280 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
2282 -- The declaration of the DIC procedure must be inserted after the
2283 -- declaration of the partial view as this allows for proper external
2284 -- visibility.
2286 if Present (Priv_Typ) then
2287 Typ_Decl := Declaration_Node (Priv_Typ);
2289 -- Derived types with the full view as parent do not have a partial
2290 -- view. Insert the DIC procedure after the derived type.
2292 else
2293 Typ_Decl := Declaration_Node (Full_Typ);
2294 end if;
2296 -- The type should have a declarative node
2298 pragma Assert (Present (Typ_Decl));
2300 -- Create the formal parameter which emulates the variable-like behavior
2301 -- of the type's current instance.
2303 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
2305 -- Perform minor decoration in case the declaration is not analyzed
2307 Mutate_Ekind (Obj_Id, E_In_Parameter);
2308 Set_Etype (Obj_Id, Work_Typ);
2309 Set_Scope (Obj_Id, Proc_Id);
2311 Set_First_Entity (Proc_Id, Obj_Id);
2312 Set_Last_Entity (Proc_Id, Obj_Id);
2314 -- Generate:
2315 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
2317 Proc_Decl :=
2318 Make_Subprogram_Declaration (Loc,
2319 Specification =>
2320 Make_Procedure_Specification (Loc,
2321 Defining_Unit_Name => Proc_Id,
2322 Parameter_Specifications => New_List (
2323 Make_Parameter_Specification (Loc,
2324 Defining_Identifier => Obj_Id,
2325 Parameter_Type =>
2326 New_Occurrence_Of (Work_Typ, Loc)))));
2328 -- The declaration should not be inserted into the tree when the context
2329 -- is a generic unit because it is not part of the template.
2331 if Inside_A_Generic then
2332 null;
2334 -- Semi-insert the declaration into the tree for GNATprove by setting
2335 -- its Parent field. This allows for proper upstream tree traversals.
2337 elsif GNATprove_Mode then
2338 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2340 -- Otherwise insert the declaration
2342 else
2343 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2344 end if;
2346 <<Leave>>
2347 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2348 end Build_DIC_Procedure_Declaration;
2350 ------------------------------------
2351 -- Build_Invariant_Procedure_Body --
2352 ------------------------------------
2354 -- WARNING: This routine manages Ghost regions. Return statements must be
2355 -- replaced by gotos which jump to the end of the routine and restore the
2356 -- Ghost mode.
2358 procedure Build_Invariant_Procedure_Body
2359 (Typ : Entity_Id;
2360 Partial_Invariant : Boolean := False)
2362 Loc : constant Source_Ptr := Sloc (Typ);
2364 Pragmas_Seen : Elist_Id := No_Elist;
2365 -- This list contains all invariant pragmas processed so far. The list
2366 -- is used to avoid generating redundant invariant checks.
2368 Produced_Check : Boolean := False;
2369 -- This flag tracks whether the type has produced at least one invariant
2370 -- check. The flag is used as a sanity check at the end of the routine.
2372 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2373 -- intentionally unnested to avoid deep indentation of code.
2375 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2376 -- they emit checks, loops (for arrays) and case statements (for record
2377 -- variant parts) only when there are invariants to verify. This keeps
2378 -- the body of the invariant procedure free of useless code.
2380 procedure Add_Array_Component_Invariants
2381 (T : Entity_Id;
2382 Obj_Id : Entity_Id;
2383 Checks : in out List_Id);
2384 -- Generate an invariant check for each component of array type T.
2385 -- Obj_Id denotes the entity of the _object formal parameter of the
2386 -- invariant procedure. All created checks are added to list Checks.
2388 procedure Add_Inherited_Invariants
2389 (T : Entity_Id;
2390 Priv_Typ : Entity_Id;
2391 Full_Typ : Entity_Id;
2392 Obj_Id : Entity_Id;
2393 Checks : in out List_Id);
2394 -- Generate an invariant check for each inherited class-wide invariant
2395 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2396 -- the partial and full view of the parent type. Obj_Id denotes the
2397 -- entity of the _object formal parameter of the invariant procedure.
2398 -- All created checks are added to list Checks.
2400 procedure Add_Interface_Invariants
2401 (T : Entity_Id;
2402 Obj_Id : Entity_Id;
2403 Checks : in out List_Id);
2404 -- Generate an invariant check for each inherited class-wide invariant
2405 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2406 -- entity of the _object formal parameter of the invariant procedure.
2407 -- All created checks are added to list Checks.
2409 procedure Add_Invariant_Check
2410 (Prag : Node_Id;
2411 Expr : Node_Id;
2412 Checks : in out List_Id;
2413 Inherited : Boolean := False);
2414 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2415 -- verify assertion expression Expr of pragma Prag. All generated code
2416 -- is added to list Checks. Flag Inherited should be set when the pragma
2417 -- is inherited from a parent or interface type.
2419 procedure Add_Own_Invariants
2420 (T : Entity_Id;
2421 Obj_Id : Entity_Id;
2422 Checks : in out List_Id;
2423 Priv_Item : Node_Id := Empty);
2424 -- Generate an invariant check for each invariant found for type T.
2425 -- Obj_Id denotes the entity of the _object formal parameter of the
2426 -- invariant procedure. All created checks are added to list Checks.
2427 -- Priv_Item denotes the first rep item of the private type.
2429 procedure Add_Parent_Invariants
2430 (T : Entity_Id;
2431 Obj_Id : Entity_Id;
2432 Checks : in out List_Id);
2433 -- Generate an invariant check for each inherited class-wide invariant
2434 -- coming from all parent types of type T. Obj_Id denotes the entity of
2435 -- the _object formal parameter of the invariant procedure. All created
2436 -- checks are added to list Checks.
2438 procedure Add_Record_Component_Invariants
2439 (T : Entity_Id;
2440 Obj_Id : Entity_Id;
2441 Checks : in out List_Id);
2442 -- Generate an invariant check for each component of record type T.
2443 -- Obj_Id denotes the entity of the _object formal parameter of the
2444 -- invariant procedure. All created checks are added to list Checks.
2446 ------------------------------------
2447 -- Add_Array_Component_Invariants --
2448 ------------------------------------
2450 procedure Add_Array_Component_Invariants
2451 (T : Entity_Id;
2452 Obj_Id : Entity_Id;
2453 Checks : in out List_Id)
2455 Comp_Typ : constant Entity_Id := Component_Type (T);
2456 Dims : constant Pos := Number_Dimensions (T);
2458 procedure Process_Array_Component
2459 (Indices : List_Id;
2460 Comp_Checks : in out List_Id);
2461 -- Generate an invariant check for an array component identified by
2462 -- the indices in list Indices. All created checks are added to list
2463 -- Comp_Checks.
2465 procedure Process_One_Dimension
2466 (Dim : Pos;
2467 Indices : List_Id;
2468 Dim_Checks : in out List_Id);
2469 -- Generate a loop over the Nth dimension Dim of an array type. List
2470 -- Indices contains all array indices for the dimension. All created
2471 -- checks are added to list Dim_Checks.
2473 -----------------------------
2474 -- Process_Array_Component --
2475 -----------------------------
2477 procedure Process_Array_Component
2478 (Indices : List_Id;
2479 Comp_Checks : in out List_Id)
2481 Proc_Id : Entity_Id;
2483 begin
2484 if Has_Invariants (Comp_Typ) then
2486 -- In GNATprove mode, the component invariants are checked by
2487 -- other means. They should not be added to the array type
2488 -- invariant procedure, so that the procedure can be used to
2489 -- check the array type invariants if any.
2491 if GNATprove_Mode then
2492 null;
2494 else
2495 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2497 -- The component type should have an invariant procedure
2498 -- if it has invariants of its own or inherits class-wide
2499 -- invariants from parent or interface types.
2501 pragma Assert (Present (Proc_Id));
2503 -- Generate:
2504 -- <Comp_Typ>Invariant (_object (<Indices>));
2506 -- The invariant procedure has a null body if assertions are
2507 -- disabled or Assertion_Policy Ignore is in effect.
2509 if not Has_Null_Body (Proc_Id) then
2510 Append_New_To (Comp_Checks,
2511 Make_Procedure_Call_Statement (Loc,
2512 Name =>
2513 New_Occurrence_Of (Proc_Id, Loc),
2514 Parameter_Associations => New_List (
2515 Make_Indexed_Component (Loc,
2516 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2517 Expressions => New_Copy_List (Indices)))));
2518 end if;
2519 end if;
2521 Produced_Check := True;
2522 end if;
2523 end Process_Array_Component;
2525 ---------------------------
2526 -- Process_One_Dimension --
2527 ---------------------------
2529 procedure Process_One_Dimension
2530 (Dim : Pos;
2531 Indices : List_Id;
2532 Dim_Checks : in out List_Id)
2534 Comp_Checks : List_Id := No_List;
2535 Index : Entity_Id;
2537 begin
2538 -- Generate the invariant checks for the array component after all
2539 -- dimensions have produced their respective loops.
2541 if Dim > Dims then
2542 Process_Array_Component
2543 (Indices => Indices,
2544 Comp_Checks => Dim_Checks);
2546 -- Otherwise create a loop for the current dimension
2548 else
2549 -- Create a new loop variable for each dimension
2551 Index :=
2552 Make_Defining_Identifier (Loc,
2553 Chars => New_External_Name ('I', Dim));
2554 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2556 Process_One_Dimension
2557 (Dim => Dim + 1,
2558 Indices => Indices,
2559 Dim_Checks => Comp_Checks);
2561 -- Generate:
2562 -- for I<Dim> in _object'Range (<Dim>) loop
2563 -- <Comp_Checks>
2564 -- end loop;
2566 -- Note that the invariant procedure may have a null body if
2567 -- assertions are disabled or Assertion_Policy Ignore is in
2568 -- effect.
2570 if Present (Comp_Checks) then
2571 Append_New_To (Dim_Checks,
2572 Make_Implicit_Loop_Statement (T,
2573 Identifier => Empty,
2574 Iteration_Scheme =>
2575 Make_Iteration_Scheme (Loc,
2576 Loop_Parameter_Specification =>
2577 Make_Loop_Parameter_Specification (Loc,
2578 Defining_Identifier => Index,
2579 Discrete_Subtype_Definition =>
2580 Make_Attribute_Reference (Loc,
2581 Prefix =>
2582 New_Occurrence_Of (Obj_Id, Loc),
2583 Attribute_Name => Name_Range,
2584 Expressions => New_List (
2585 Make_Integer_Literal (Loc, Dim))))),
2586 Statements => Comp_Checks));
2587 end if;
2588 end if;
2589 end Process_One_Dimension;
2591 -- Start of processing for Add_Array_Component_Invariants
2593 begin
2594 Process_One_Dimension
2595 (Dim => 1,
2596 Indices => New_List,
2597 Dim_Checks => Checks);
2598 end Add_Array_Component_Invariants;
2600 ------------------------------
2601 -- Add_Inherited_Invariants --
2602 ------------------------------
2604 procedure Add_Inherited_Invariants
2605 (T : Entity_Id;
2606 Priv_Typ : Entity_Id;
2607 Full_Typ : Entity_Id;
2608 Obj_Id : Entity_Id;
2609 Checks : in out List_Id)
2611 Deriv_Typ : Entity_Id;
2612 Expr : Node_Id;
2613 Prag : Node_Id;
2614 Prag_Expr : Node_Id;
2615 Prag_Expr_Arg : Node_Id;
2616 Prag_Typ : Node_Id;
2617 Prag_Typ_Arg : Node_Id;
2619 Par_Proc : Entity_Id;
2620 -- The "partial" invariant procedure of Par_Typ
2622 Par_Typ : Entity_Id;
2623 -- The suitable view of the parent type used in the substitution of
2624 -- type attributes.
2626 begin
2627 if not Present (Priv_Typ) and then not Present (Full_Typ) then
2628 return;
2629 end if;
2631 -- When the type inheriting the class-wide invariant is a concurrent
2632 -- type, use the corresponding record type because it contains all
2633 -- primitive operations of the concurrent type and allows for proper
2634 -- substitution.
2636 if Is_Concurrent_Type (T) then
2637 Deriv_Typ := Corresponding_Record_Type (T);
2638 else
2639 Deriv_Typ := T;
2640 end if;
2642 pragma Assert (Present (Deriv_Typ));
2644 -- Determine which rep item chain to use. Precedence is given to that
2645 -- of the parent type's partial view since it usually carries all the
2646 -- class-wide invariants.
2648 if Present (Priv_Typ) then
2649 Prag := First_Rep_Item (Priv_Typ);
2650 else
2651 Prag := First_Rep_Item (Full_Typ);
2652 end if;
2654 while Present (Prag) loop
2655 if Nkind (Prag) = N_Pragma
2656 and then Pragma_Name (Prag) = Name_Invariant
2657 then
2658 -- Nothing to do if the pragma was already processed
2660 if Contains (Pragmas_Seen, Prag) then
2661 return;
2663 -- Nothing to do when the caller requests the processing of all
2664 -- inherited class-wide invariants, but the pragma does not
2665 -- fall in this category.
2667 elsif not Class_Present (Prag) then
2668 return;
2669 end if;
2671 -- Extract the arguments of the invariant pragma
2673 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2674 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2675 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2676 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2678 -- The pragma applies to the partial view of the parent type
2680 if Present (Priv_Typ)
2681 and then Entity (Prag_Typ) = Priv_Typ
2682 then
2683 Par_Typ := Priv_Typ;
2685 -- The pragma applies to the full view of the parent type
2687 elsif Present (Full_Typ)
2688 and then Entity (Prag_Typ) = Full_Typ
2689 then
2690 Par_Typ := Full_Typ;
2692 -- Otherwise the pragma does not belong to the parent type and
2693 -- should not be considered.
2695 else
2696 return;
2697 end if;
2699 -- Perform the following substitutions:
2701 -- * Replace a reference to the _object parameter of the
2702 -- parent type's partial invariant procedure with a
2703 -- reference to the _object parameter of the derived
2704 -- type's full invariant procedure.
2706 -- * Replace a reference to a discriminant of the parent type
2707 -- with a suitable value from the point of view of the
2708 -- derived type.
2710 -- * Replace a call to an overridden parent primitive with a
2711 -- call to the overriding derived type primitive.
2713 -- * Replace a call to an inherited parent primitive with a
2714 -- call to the internally-generated inherited derived type
2715 -- primitive.
2717 Expr := New_Copy_Tree (Prag_Expr);
2719 -- The parent type must have a "partial" invariant procedure
2720 -- because class-wide invariants are captured exclusively by
2721 -- it.
2723 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2724 pragma Assert (Present (Par_Proc));
2726 Replace_References
2727 (Expr => Expr,
2728 Par_Typ => Par_Typ,
2729 Deriv_Typ => Deriv_Typ,
2730 Par_Obj => First_Formal (Par_Proc),
2731 Deriv_Obj => Obj_Id);
2733 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2734 end if;
2736 Next_Rep_Item (Prag);
2737 end loop;
2738 end Add_Inherited_Invariants;
2740 ------------------------------
2741 -- Add_Interface_Invariants --
2742 ------------------------------
2744 procedure Add_Interface_Invariants
2745 (T : Entity_Id;
2746 Obj_Id : Entity_Id;
2747 Checks : in out List_Id)
2749 Iface_Elmt : Elmt_Id;
2750 Ifaces : Elist_Id;
2752 begin
2753 -- Generate an invariant check for each class-wide invariant coming
2754 -- from all interfaces implemented by type T.
2756 if Is_Tagged_Type (T) then
2757 Collect_Interfaces (T, Ifaces);
2759 -- Process the class-wide invariants of all implemented interfaces
2761 Iface_Elmt := First_Elmt (Ifaces);
2762 while Present (Iface_Elmt) loop
2764 -- The Full_Typ parameter is intentionally left Empty because
2765 -- interfaces are treated as the partial view of a private type
2766 -- in order to achieve uniformity with the general case.
2768 Add_Inherited_Invariants
2769 (T => T,
2770 Priv_Typ => Node (Iface_Elmt),
2771 Full_Typ => Empty,
2772 Obj_Id => Obj_Id,
2773 Checks => Checks);
2775 Next_Elmt (Iface_Elmt);
2776 end loop;
2777 end if;
2778 end Add_Interface_Invariants;
2780 -------------------------
2781 -- Add_Invariant_Check --
2782 -------------------------
2784 procedure Add_Invariant_Check
2785 (Prag : Node_Id;
2786 Expr : Node_Id;
2787 Checks : in out List_Id;
2788 Inherited : Boolean := False)
2790 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2791 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2792 Ploc : constant Source_Ptr := Sloc (Prag);
2793 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2795 Assoc : List_Id;
2796 Str : String_Id;
2798 begin
2799 -- The invariant is ignored, nothing left to do
2801 if Is_Ignored (Prag) then
2802 null;
2804 -- Otherwise the invariant is checked. Build a pragma Check to verify
2805 -- the expression at run time.
2807 else
2808 Assoc := New_List (
2809 Make_Pragma_Argument_Association (Ploc,
2810 Expression => Make_Identifier (Ploc, Nam)),
2811 Make_Pragma_Argument_Association (Ploc,
2812 Expression => Expr));
2814 -- Handle the String argument (if any)
2816 if Present (Str_Arg) then
2817 Str := Strval (Get_Pragma_Arg (Str_Arg));
2819 -- When inheriting an invariant, modify the message from
2820 -- "failed invariant" to "failed inherited invariant".
2822 if Inherited then
2823 String_To_Name_Buffer (Str);
2825 if Name_Buffer (1 .. 16) = "failed invariant" then
2826 Insert_Str_In_Name_Buffer ("inherited ", 8);
2827 Str := String_From_Name_Buffer;
2828 end if;
2829 end if;
2831 Append_To (Assoc,
2832 Make_Pragma_Argument_Association (Ploc,
2833 Expression => Make_String_Literal (Ploc, Str)));
2834 end if;
2836 -- Generate:
2837 -- pragma Check (<Nam>, <Expr>, <Str>);
2839 Append_New_To (Checks,
2840 Make_Pragma (Ploc,
2841 Chars => Name_Check,
2842 Pragma_Argument_Associations => Assoc));
2843 end if;
2845 -- Output an info message when inheriting an invariant and the
2846 -- listing option is enabled.
2848 if Inherited and Opt.List_Inherited_Aspects then
2849 Error_Msg_Sloc := Sloc (Prag);
2850 Error_Msg_N
2851 ("info: & inherits `Invariant''Class` aspect from #?.l?", Typ);
2852 end if;
2854 -- Add the pragma to the list of processed pragmas
2856 Append_New_Elmt (Prag, Pragmas_Seen);
2857 Produced_Check := True;
2858 end Add_Invariant_Check;
2860 ---------------------------
2861 -- Add_Parent_Invariants --
2862 ---------------------------
2864 procedure Add_Parent_Invariants
2865 (T : Entity_Id;
2866 Obj_Id : Entity_Id;
2867 Checks : in out List_Id)
2869 Dummy_1 : Entity_Id;
2870 Dummy_2 : Entity_Id;
2872 Curr_Typ : Entity_Id;
2873 -- The entity of the current type being examined
2875 Full_Typ : Entity_Id;
2876 -- The full view of Par_Typ
2878 Par_Typ : Entity_Id;
2879 -- The entity of the parent type
2881 Priv_Typ : Entity_Id;
2882 -- The partial view of Par_Typ
2884 begin
2885 -- Do not process array types because they cannot have true parent
2886 -- types. This also prevents the generation of a duplicate invariant
2887 -- check when the input type is an array base type because its Etype
2888 -- denotes the first subtype, both of which share the same component
2889 -- type.
2891 if Is_Array_Type (T) then
2892 return;
2893 end if;
2895 -- Climb the parent type chain
2897 Curr_Typ := T;
2898 loop
2899 -- Do not consider subtypes as they inherit the invariants
2900 -- from their base types.
2902 Par_Typ := Base_Type (Etype (Curr_Typ));
2904 -- Stop the climb once the root of the parent chain is
2905 -- reached.
2907 exit when Curr_Typ = Par_Typ;
2909 -- Process the class-wide invariants of the parent type
2911 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2913 -- Process the elements of an array type
2915 if Is_Array_Type (Full_Typ) then
2916 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
2918 -- Process the components of a record type
2920 elsif Ekind (Full_Typ) = E_Record_Type then
2921 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
2922 end if;
2924 Add_Inherited_Invariants
2925 (T => T,
2926 Priv_Typ => Priv_Typ,
2927 Full_Typ => Full_Typ,
2928 Obj_Id => Obj_Id,
2929 Checks => Checks);
2931 Curr_Typ := Par_Typ;
2932 end loop;
2933 end Add_Parent_Invariants;
2935 ------------------------
2936 -- Add_Own_Invariants --
2937 ------------------------
2939 procedure Add_Own_Invariants
2940 (T : Entity_Id;
2941 Obj_Id : Entity_Id;
2942 Checks : in out List_Id;
2943 Priv_Item : Node_Id := Empty)
2945 Expr : Node_Id;
2946 Prag : Node_Id;
2947 Prag_Asp : Node_Id;
2948 Prag_Expr : Node_Id;
2949 Prag_Expr_Arg : Node_Id;
2950 Prag_Typ : Node_Id;
2951 Prag_Typ_Arg : Node_Id;
2953 begin
2954 if not Present (T) then
2955 return;
2956 end if;
2958 Prag := First_Rep_Item (T);
2959 while Present (Prag) loop
2960 if Nkind (Prag) = N_Pragma
2961 and then Pragma_Name (Prag) = Name_Invariant
2962 then
2963 -- Stop the traversal of the rep item chain once a specific
2964 -- item is encountered.
2966 if Present (Priv_Item) and then Prag = Priv_Item then
2967 exit;
2968 end if;
2970 -- Nothing to do if the pragma was already processed
2972 if Contains (Pragmas_Seen, Prag) then
2973 return;
2974 end if;
2976 -- Extract the arguments of the invariant pragma
2978 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2979 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2980 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
2981 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2982 Prag_Asp := Corresponding_Aspect (Prag);
2984 -- Verify the pragma belongs to T, otherwise the pragma applies
2985 -- to a parent type in which case it will be processed later by
2986 -- Add_Parent_Invariants or Add_Interface_Invariants.
2988 if Entity (Prag_Typ) /= T then
2989 return;
2990 end if;
2992 Expr := New_Copy_Tree (Prag_Expr);
2994 -- Substitute all references to type T with references to the
2995 -- _object formal parameter.
2997 Replace_Type_References (Expr, T, Obj_Id);
2999 -- Preanalyze the invariant expression to detect errors and at
3000 -- the same time capture the visibility of the proper package
3001 -- part.
3003 Set_Parent (Expr, Parent (Prag_Expr));
3004 Preanalyze_Assert_Expression (Expr, Any_Boolean);
3006 -- Save a copy of the expression when T is tagged to detect
3007 -- errors and capture the visibility of the proper package part
3008 -- for the generation of inherited type invariants.
3010 if Is_Tagged_Type (T) then
3011 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
3012 end if;
3014 -- If the pragma comes from an aspect specification, replace
3015 -- the saved expression because all type references must be
3016 -- substituted for the call to Preanalyze_Spec_Expression in
3017 -- Check_Aspect_At_xxx routines.
3019 if Present (Prag_Asp) then
3020 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
3021 end if;
3023 Add_Invariant_Check (Prag, Expr, Checks);
3024 end if;
3026 Next_Rep_Item (Prag);
3027 end loop;
3028 end Add_Own_Invariants;
3030 -------------------------------------
3031 -- Add_Record_Component_Invariants --
3032 -------------------------------------
3034 procedure Add_Record_Component_Invariants
3035 (T : Entity_Id;
3036 Obj_Id : Entity_Id;
3037 Checks : in out List_Id)
3039 procedure Process_Component_List
3040 (Comp_List : Node_Id;
3041 CL_Checks : in out List_Id);
3042 -- Generate invariant checks for all record components found in
3043 -- component list Comp_List, including variant parts. All created
3044 -- checks are added to list CL_Checks.
3046 procedure Process_Record_Component
3047 (Comp_Id : Entity_Id;
3048 Comp_Checks : in out List_Id);
3049 -- Generate an invariant check for a record component identified by
3050 -- Comp_Id. All created checks are added to list Comp_Checks.
3052 ----------------------------
3053 -- Process_Component_List --
3054 ----------------------------
3056 procedure Process_Component_List
3057 (Comp_List : Node_Id;
3058 CL_Checks : in out List_Id)
3060 Comp : Node_Id;
3061 Var : Node_Id;
3062 Var_Alts : List_Id := No_List;
3063 Var_Checks : List_Id := No_List;
3064 Var_Stmts : List_Id;
3066 Produced_Variant_Check : Boolean := False;
3067 -- This flag tracks whether the component has produced at least
3068 -- one invariant check.
3070 begin
3071 -- Traverse the component items
3073 Comp := First (Component_Items (Comp_List));
3074 while Present (Comp) loop
3075 if Nkind (Comp) = N_Component_Declaration then
3077 -- Generate the component invariant check
3079 Process_Record_Component
3080 (Comp_Id => Defining_Entity (Comp),
3081 Comp_Checks => CL_Checks);
3082 end if;
3084 Next (Comp);
3085 end loop;
3087 -- Traverse the variant part
3089 if Present (Variant_Part (Comp_List)) then
3090 Var := First (Variants (Variant_Part (Comp_List)));
3091 while Present (Var) loop
3092 Var_Checks := No_List;
3094 -- Generate invariant checks for all components and variant
3095 -- parts that qualify.
3097 Process_Component_List
3098 (Comp_List => Component_List (Var),
3099 CL_Checks => Var_Checks);
3101 -- The components of the current variant produced at least
3102 -- one invariant check.
3104 if Present (Var_Checks) then
3105 Var_Stmts := Var_Checks;
3106 Produced_Variant_Check := True;
3108 -- Otherwise there are either no components with invariants,
3109 -- assertions are disabled, or Assertion_Policy Ignore is in
3110 -- effect.
3112 else
3113 Var_Stmts := New_List (Make_Null_Statement (Loc));
3114 end if;
3116 Append_New_To (Var_Alts,
3117 Make_Case_Statement_Alternative (Loc,
3118 Discrete_Choices =>
3119 New_Copy_List (Discrete_Choices (Var)),
3120 Statements => Var_Stmts));
3122 Next (Var);
3123 end loop;
3125 -- Create a case statement which verifies the invariant checks
3126 -- of a particular component list depending on the discriminant
3127 -- values only when there is at least one real invariant check.
3129 if Produced_Variant_Check then
3130 Append_New_To (CL_Checks,
3131 Make_Case_Statement (Loc,
3132 Expression =>
3133 Make_Selected_Component (Loc,
3134 Prefix => New_Occurrence_Of (Obj_Id, Loc),
3135 Selector_Name =>
3136 New_Occurrence_Of
3137 (Entity (Name (Variant_Part (Comp_List))), Loc)),
3138 Alternatives => Var_Alts));
3139 end if;
3140 end if;
3141 end Process_Component_List;
3143 ------------------------------
3144 -- Process_Record_Component --
3145 ------------------------------
3147 procedure Process_Record_Component
3148 (Comp_Id : Entity_Id;
3149 Comp_Checks : in out List_Id)
3151 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
3152 Proc_Id : Entity_Id;
3154 Produced_Component_Check : Boolean := False;
3155 -- This flag tracks whether the component has produced at least
3156 -- one invariant check.
3158 begin
3159 -- Nothing to do for internal component _parent. Note that it is
3160 -- not desirable to check whether the component comes from source
3161 -- because protected type components are relocated to an internal
3162 -- corresponding record, but still need processing.
3164 if Chars (Comp_Id) = Name_uParent then
3165 return;
3166 end if;
3168 -- Verify the invariant of the component. Note that an access
3169 -- type may have an invariant when it acts as the full view of a
3170 -- private type and the invariant appears on the partial view. In
3171 -- this case verify the access value itself.
3173 if Has_Invariants (Comp_Typ) then
3175 -- In GNATprove mode, the component invariants are checked by
3176 -- other means. They should not be added to the record type
3177 -- invariant procedure, so that the procedure can be used to
3178 -- check the record type invariants if any.
3180 if GNATprove_Mode then
3181 null;
3183 else
3184 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
3186 -- The component type should have an invariant procedure
3187 -- if it has invariants of its own or inherits class-wide
3188 -- invariants from parent or interface types.
3190 pragma Assert (Present (Proc_Id));
3192 -- Generate:
3193 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
3195 -- Note that the invariant procedure may have a null body if
3196 -- assertions are disabled or Assertion_Policy Ignore is in
3197 -- effect.
3199 if not Has_Null_Body (Proc_Id) then
3200 Append_New_To (Comp_Checks,
3201 Make_Procedure_Call_Statement (Loc,
3202 Name =>
3203 New_Occurrence_Of (Proc_Id, Loc),
3204 Parameter_Associations => New_List (
3205 Make_Selected_Component (Loc,
3206 Prefix =>
3207 Unchecked_Convert_To
3208 (T, New_Occurrence_Of (Obj_Id, Loc)),
3209 Selector_Name =>
3210 New_Occurrence_Of (Comp_Id, Loc)))));
3211 end if;
3212 end if;
3214 Produced_Check := True;
3215 Produced_Component_Check := True;
3216 end if;
3218 if Produced_Component_Check and then Has_Unchecked_Union (T) then
3219 Error_Msg_NE
3220 ("invariants cannot be checked on components of "
3221 & "unchecked_union type &??", Comp_Id, T);
3222 end if;
3223 end Process_Record_Component;
3225 -- Local variables
3227 Comps : Node_Id;
3228 Def : Node_Id;
3230 -- Start of processing for Add_Record_Component_Invariants
3232 begin
3233 -- An untagged derived type inherits the components of its parent
3234 -- type. In order to avoid creating redundant invariant checks, do
3235 -- not process the components now. Instead wait until the ultimate
3236 -- parent of the untagged derivation chain is reached.
3238 if not Is_Untagged_Derivation (T) then
3239 Def := Type_Definition (Parent (T));
3241 if Nkind (Def) = N_Derived_Type_Definition then
3242 Def := Record_Extension_Part (Def);
3243 end if;
3245 pragma Assert (Nkind (Def) = N_Record_Definition);
3246 Comps := Component_List (Def);
3248 if Present (Comps) then
3249 Process_Component_List
3250 (Comp_List => Comps,
3251 CL_Checks => Checks);
3252 end if;
3253 end if;
3254 end Add_Record_Component_Invariants;
3256 -- Local variables
3258 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3259 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3260 -- Save the Ghost-related attributes to restore on exit
3262 Dummy : Entity_Id;
3263 Priv_Item : Node_Id;
3264 Proc_Body : Node_Id;
3265 Proc_Body_Id : Entity_Id;
3266 Proc_Decl : Node_Id;
3267 Proc_Id : Entity_Id;
3268 Stmts : List_Id := No_List;
3270 CRec_Typ : Entity_Id := Empty;
3271 -- The corresponding record type of Full_Typ
3273 Full_Proc : Entity_Id := Empty;
3274 -- The entity of the "full" invariant procedure
3276 Full_Typ : Entity_Id := Empty;
3277 -- The full view of the working type
3279 Obj_Id : Entity_Id := Empty;
3280 -- The _object formal parameter of the invariant procedure
3282 Part_Proc : Entity_Id := Empty;
3283 -- The entity of the "partial" invariant procedure
3285 Priv_Typ : Entity_Id := Empty;
3286 -- The partial view of the working type
3288 Work_Typ : Entity_Id := Empty;
3289 -- The working type
3291 -- Start of processing for Build_Invariant_Procedure_Body
3293 begin
3294 Work_Typ := Typ;
3296 -- Do not process the underlying full view of a private type. There is
3297 -- no way to get back to the partial view, plus the body will be built
3298 -- by the full view or the base type.
3300 if Is_Underlying_Full_View (Work_Typ) then
3301 return;
3303 -- The input type denotes the implementation base type of a constrained
3304 -- array type. Work with the first subtype as all invariant pragmas are
3305 -- on its rep item chain.
3307 elsif Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3308 Work_Typ := First_Subtype (Work_Typ);
3310 -- The input type denotes the corresponding record type of a protected
3311 -- or task type. Work with the concurrent type because the corresponding
3312 -- record type may not be visible to clients of the type.
3314 elsif Ekind (Work_Typ) = E_Record_Type
3315 and then Is_Concurrent_Record_Type (Work_Typ)
3316 then
3317 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3318 end if;
3320 -- The working type may be subject to pragma Ghost. Set the mode now to
3321 -- ensure that the invariant procedure is properly marked as Ghost.
3323 Set_Ghost_Mode (Work_Typ);
3325 -- The type must either have invariants of its own, inherit class-wide
3326 -- invariants from parent types or interfaces, or be an array or record
3327 -- type whose components have invariants.
3329 pragma Assert (Has_Invariants (Work_Typ));
3331 -- Interfaces are treated as the partial view of a private type in order
3332 -- to achieve uniformity with the general case.
3334 if Is_Interface (Work_Typ) then
3335 Priv_Typ := Work_Typ;
3337 -- Otherwise obtain both views of the type
3339 else
3340 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3341 end if;
3343 -- The caller requests a body for the partial invariant procedure
3345 if Partial_Invariant then
3346 Full_Proc := Invariant_Procedure (Work_Typ);
3347 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3349 -- The "full" invariant procedure body was already created
3351 if Present (Full_Proc)
3352 and then Present
3353 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3354 then
3355 -- This scenario happens only when the type is an untagged
3356 -- derivation from a private parent and the underlying full
3357 -- view was processed before the partial view.
3359 pragma Assert
3360 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3362 -- Nothing to do because the processing of the underlying full
3363 -- view already checked the invariants of the partial view.
3365 goto Leave;
3366 end if;
3368 -- Create a declaration for the "partial" invariant procedure if it
3369 -- is not available.
3371 if No (Proc_Id) then
3372 Build_Invariant_Procedure_Declaration
3373 (Typ => Work_Typ,
3374 Partial_Invariant => True);
3376 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3377 end if;
3379 -- The caller requests a body for the "full" invariant procedure
3381 else
3382 Proc_Id := Invariant_Procedure (Work_Typ);
3383 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3385 -- Create a declaration for the "full" invariant procedure if it is
3386 -- not available.
3388 if No (Proc_Id) then
3389 Build_Invariant_Procedure_Declaration (Work_Typ);
3390 Proc_Id := Invariant_Procedure (Work_Typ);
3391 end if;
3392 end if;
3394 -- At this point there should be an invariant procedure declaration
3396 pragma Assert (Present (Proc_Id));
3397 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3399 -- Nothing to do if the invariant procedure already has a body
3401 if Present (Corresponding_Body (Proc_Decl)) then
3402 goto Leave;
3403 end if;
3405 -- Emulate the environment of the invariant procedure by installing its
3406 -- scope and formal parameters. Note that this is not needed, but having
3407 -- the scope installed helps with the detection of invariant-related
3408 -- errors.
3410 Push_Scope (Proc_Id);
3411 Install_Formals (Proc_Id);
3413 Obj_Id := First_Formal (Proc_Id);
3414 pragma Assert (Present (Obj_Id));
3416 -- The "partial" invariant procedure verifies the invariants of the
3417 -- partial view only.
3419 if Partial_Invariant then
3420 pragma Assert (Present (Priv_Typ));
3422 Add_Own_Invariants
3423 (T => Priv_Typ,
3424 Obj_Id => Obj_Id,
3425 Checks => Stmts);
3427 -- Otherwise the "full" invariant procedure verifies the invariants of
3428 -- the full view, all array or record components, as well as class-wide
3429 -- invariants inherited from parent types or interfaces. In addition, it
3430 -- indirectly verifies the invariants of the partial view by calling the
3431 -- "partial" invariant procedure.
3433 else
3434 pragma Assert (Present (Full_Typ));
3436 -- Check the invariants of the partial view by calling the "partial"
3437 -- invariant procedure. Generate:
3439 -- <Work_Typ>Partial_Invariant (_object);
3441 if Present (Part_Proc) then
3442 Append_New_To (Stmts,
3443 Make_Procedure_Call_Statement (Loc,
3444 Name => New_Occurrence_Of (Part_Proc, Loc),
3445 Parameter_Associations => New_List (
3446 New_Occurrence_Of (Obj_Id, Loc))));
3448 Produced_Check := True;
3449 end if;
3451 Priv_Item := Empty;
3453 -- Derived subtypes do not have a partial view
3455 if Present (Priv_Typ) then
3457 -- The processing of the "full" invariant procedure intentionally
3458 -- skips the partial view because a) this may result in changes of
3459 -- visibility and b) lead to duplicate checks. However, when the
3460 -- full view is the underlying full view of an untagged derived
3461 -- type whose parent type is private, partial invariants appear on
3462 -- the rep item chain of the partial view only.
3464 -- package Pack_1 is
3465 -- type Root ... is private;
3466 -- private
3467 -- <full view of Root>
3468 -- end Pack_1;
3470 -- with Pack_1;
3471 -- package Pack_2 is
3472 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3473 -- <underlying full view of Child>
3474 -- end Pack_2;
3476 -- As a result, the processing of the full view must also consider
3477 -- all invariants of the partial view.
3479 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3480 null;
3482 -- Otherwise the invariants of the partial view are ignored
3484 else
3485 -- Note that the rep item chain is shared between the partial
3486 -- and full views of a type. To avoid processing the invariants
3487 -- of the partial view, signal the logic to stop when the first
3488 -- rep item of the partial view has been reached.
3490 Priv_Item := First_Rep_Item (Priv_Typ);
3492 -- Ignore the invariants of the partial view by eliminating the
3493 -- view.
3495 Priv_Typ := Empty;
3496 end if;
3497 end if;
3499 -- Process the invariants of the full view and in certain cases those
3500 -- of the partial view. This also handles any invariants on array or
3501 -- record components.
3503 Add_Own_Invariants
3504 (T => Priv_Typ,
3505 Obj_Id => Obj_Id,
3506 Checks => Stmts,
3507 Priv_Item => Priv_Item);
3509 Add_Own_Invariants
3510 (T => Full_Typ,
3511 Obj_Id => Obj_Id,
3512 Checks => Stmts,
3513 Priv_Item => Priv_Item);
3515 -- Process the elements of an array type
3517 if Is_Array_Type (Full_Typ) then
3518 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3520 -- Process the components of a record type
3522 elsif Ekind (Full_Typ) = E_Record_Type then
3523 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3525 -- Process the components of a corresponding record
3527 elsif Present (CRec_Typ) then
3528 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3529 end if;
3531 -- Process the inherited class-wide invariants of all parent types.
3532 -- This also handles any invariants on record components.
3534 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3536 -- Process the inherited class-wide invariants of all implemented
3537 -- interface types.
3539 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3540 end if;
3542 End_Scope;
3544 -- At this point there should be at least one invariant check. If this
3545 -- is not the case, then the invariant-related flags were not properly
3546 -- set, or there is a missing invariant procedure on one of the array
3547 -- or record components.
3549 pragma Assert (Produced_Check);
3551 -- Account for the case where assertions are disabled or all invariant
3552 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3553 -- empty body.
3555 if No (Stmts) then
3556 Stmts := New_List (Make_Null_Statement (Loc));
3557 end if;
3559 -- Generate:
3560 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3561 -- begin
3562 -- <Stmts>
3563 -- end <Work_Typ>[Partial_]Invariant;
3565 Proc_Body :=
3566 Make_Subprogram_Body (Loc,
3567 Specification =>
3568 Copy_Subprogram_Spec (Parent (Proc_Id)),
3569 Declarations => Empty_List,
3570 Handled_Statement_Sequence =>
3571 Make_Handled_Sequence_Of_Statements (Loc,
3572 Statements => Stmts));
3573 Proc_Body_Id := Defining_Entity (Proc_Body);
3575 -- Perform minor decoration in case the body is not analyzed
3577 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
3578 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3579 Set_Scope (Proc_Body_Id, Current_Scope);
3581 -- Link both spec and body to avoid generating duplicates
3583 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3584 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3586 -- The body should not be inserted into the tree when the context is
3587 -- a generic unit because it is not part of the template. Note
3588 -- that the body must still be generated in order to resolve the
3589 -- invariants.
3591 if Inside_A_Generic then
3592 null;
3594 -- Semi-insert the body into the tree for GNATprove by setting its
3595 -- Parent field. This allows for proper upstream tree traversals.
3597 elsif GNATprove_Mode then
3598 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3600 -- Otherwise the body is part of the freezing actions of the type
3602 else
3603 Append_Freeze_Action (Work_Typ, Proc_Body);
3604 end if;
3606 <<Leave>>
3607 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3608 end Build_Invariant_Procedure_Body;
3610 -------------------------------------------
3611 -- Build_Invariant_Procedure_Declaration --
3612 -------------------------------------------
3614 -- WARNING: This routine manages Ghost regions. Return statements must be
3615 -- replaced by gotos which jump to the end of the routine and restore the
3616 -- Ghost mode.
3618 procedure Build_Invariant_Procedure_Declaration
3619 (Typ : Entity_Id;
3620 Partial_Invariant : Boolean := False)
3622 Loc : constant Source_Ptr := Sloc (Typ);
3624 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3625 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3626 -- Save the Ghost-related attributes to restore on exit
3628 Proc_Decl : Node_Id;
3629 Proc_Id : Entity_Id;
3630 Proc_Nam : Name_Id;
3631 Typ_Decl : Node_Id;
3633 CRec_Typ : Entity_Id;
3634 -- The corresponding record type of Full_Typ
3636 Full_Typ : Entity_Id;
3637 -- The full view of working type
3639 Obj_Id : Entity_Id;
3640 -- The _object formal parameter of the invariant procedure
3642 Obj_Typ : Entity_Id;
3643 -- The type of the _object formal parameter
3645 Priv_Typ : Entity_Id;
3646 -- The partial view of working type
3648 UFull_Typ : Entity_Id;
3649 -- The underlying full view of Full_Typ
3651 Work_Typ : Entity_Id;
3652 -- The working type
3654 begin
3655 Work_Typ := Typ;
3657 -- The input type denotes the implementation base type of a constrained
3658 -- array type. Work with the first subtype as all invariant pragmas are
3659 -- on its rep item chain.
3661 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3662 Work_Typ := First_Subtype (Work_Typ);
3664 -- The input denotes the corresponding record type of a protected or a
3665 -- task type. Work with the concurrent type because the corresponding
3666 -- record type may not be visible to clients of the type.
3668 elsif Ekind (Work_Typ) = E_Record_Type
3669 and then Is_Concurrent_Record_Type (Work_Typ)
3670 then
3671 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3672 end if;
3674 -- The working type may be subject to pragma Ghost. Set the mode now to
3675 -- ensure that the invariant procedure is properly marked as Ghost.
3677 Set_Ghost_Mode (Work_Typ);
3679 -- The type must either have invariants of its own, inherit class-wide
3680 -- invariants from parent or interface types, or be an array or record
3681 -- type whose components have invariants.
3683 pragma Assert (Has_Invariants (Work_Typ));
3685 -- Nothing to do if the type already has a "partial" invariant procedure
3687 if Partial_Invariant then
3688 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3689 goto Leave;
3690 end if;
3692 -- Nothing to do if the type already has a "full" invariant procedure
3694 elsif Present (Invariant_Procedure (Work_Typ)) then
3695 goto Leave;
3696 end if;
3698 -- The caller requests the declaration of the "partial" invariant
3699 -- procedure.
3701 if Partial_Invariant then
3702 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3704 -- Otherwise the caller requests the declaration of the "full" invariant
3705 -- procedure.
3707 else
3708 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3709 end if;
3711 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3713 -- Perform minor decoration in case the declaration is not analyzed
3715 Mutate_Ekind (Proc_Id, E_Procedure);
3716 Set_Etype (Proc_Id, Standard_Void_Type);
3717 Set_Scope (Proc_Id, Current_Scope);
3719 if Partial_Invariant then
3720 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3721 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3722 else
3723 Set_Is_Invariant_Procedure (Proc_Id);
3724 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3725 end if;
3727 -- The invariant procedure requires debug info when the invariants are
3728 -- subject to Source Coverage Obligations.
3730 if Generate_SCO then
3731 Set_Debug_Info_Needed (Proc_Id);
3732 end if;
3734 -- Obtain all views of the input type
3736 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
3738 -- Associate the invariant procedure and various flags with all views
3740 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3741 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3742 Propagate_Invariant_Attributes (UFull_Typ, From_Typ => Work_Typ);
3743 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3745 -- The declaration of the invariant procedure is inserted after the
3746 -- declaration of the partial view as this allows for proper external
3747 -- visibility.
3749 if Present (Priv_Typ) then
3750 Typ_Decl := Declaration_Node (Priv_Typ);
3752 -- Anonymous arrays in object declarations have no explicit declaration
3753 -- so use the related object declaration as the insertion point.
3755 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3756 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3758 -- Derived types with the full view as parent do not have a partial
3759 -- view. Insert the invariant procedure after the derived type.
3761 else
3762 Typ_Decl := Declaration_Node (Full_Typ);
3763 end if;
3765 -- The type should have a declarative node
3767 pragma Assert (Present (Typ_Decl));
3769 -- Create the formal parameter which emulates the variable-like behavior
3770 -- of the current type instance.
3772 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3774 -- When generating an invariant procedure declaration for an abstract
3775 -- type (including interfaces), use the class-wide type as the _object
3776 -- type. This has several desirable effects:
3778 -- * The invariant procedure does not become a primitive of the type.
3779 -- This eliminates the need to either special case the treatment of
3780 -- invariant procedures, or to make it a predefined primitive and
3781 -- force every derived type to potentially provide an empty body.
3783 -- * The invariant procedure does not need to be declared as abstract.
3784 -- This allows for a proper body, which in turn avoids redundant
3785 -- processing of the same invariants for types with multiple views.
3787 -- * The class-wide type allows for calls to abstract primitives
3788 -- within a nonabstract subprogram. The calls are treated as
3789 -- dispatching and require additional processing when they are
3790 -- remapped to call primitives of derived types. See routine
3791 -- Replace_References for details.
3793 if Is_Abstract_Type (Work_Typ) then
3794 Obj_Typ := Class_Wide_Type (Work_Typ);
3795 else
3796 Obj_Typ := Work_Typ;
3797 end if;
3799 -- Perform minor decoration in case the declaration is not analyzed
3801 Mutate_Ekind (Obj_Id, E_In_Parameter);
3802 Set_Etype (Obj_Id, Obj_Typ);
3803 Set_Scope (Obj_Id, Proc_Id);
3805 Set_First_Entity (Proc_Id, Obj_Id);
3806 Set_Last_Entity (Proc_Id, Obj_Id);
3808 -- Generate:
3809 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3811 Proc_Decl :=
3812 Make_Subprogram_Declaration (Loc,
3813 Specification =>
3814 Make_Procedure_Specification (Loc,
3815 Defining_Unit_Name => Proc_Id,
3816 Parameter_Specifications => New_List (
3817 Make_Parameter_Specification (Loc,
3818 Defining_Identifier => Obj_Id,
3819 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3821 -- The declaration should not be inserted into the tree when the context
3822 -- is a generic unit because it is not part of the template.
3824 if Inside_A_Generic then
3825 null;
3827 -- Semi-insert the declaration into the tree for GNATprove by setting
3828 -- its Parent field. This allows for proper upstream tree traversals.
3830 elsif GNATprove_Mode then
3831 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3833 -- Otherwise insert the declaration
3835 else
3836 pragma Assert (Present (Typ_Decl));
3837 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3838 end if;
3840 <<Leave>>
3841 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3842 end Build_Invariant_Procedure_Declaration;
3844 --------------------------
3845 -- Build_Procedure_Form --
3846 --------------------------
3848 procedure Build_Procedure_Form (N : Node_Id) is
3849 Loc : constant Source_Ptr := Sloc (N);
3850 Subp : constant Entity_Id := Defining_Entity (N);
3852 Func_Formal : Entity_Id;
3853 Proc_Formals : List_Id;
3854 Proc_Decl : Node_Id;
3856 begin
3857 -- No action needed if this transformation was already done, or in case
3858 -- of subprogram renaming declarations.
3860 if Nkind (Specification (N)) = N_Procedure_Specification
3861 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3862 then
3863 return;
3864 end if;
3866 -- Ditto when dealing with an expression function, where both the
3867 -- original expression and the generated declaration end up being
3868 -- expanded here.
3870 if Rewritten_For_C (Subp) then
3871 return;
3872 end if;
3874 Proc_Formals := New_List;
3876 -- Create a list of formal parameters with the same types as the
3877 -- function.
3879 Func_Formal := First_Formal (Subp);
3880 while Present (Func_Formal) loop
3881 Append_To (Proc_Formals,
3882 Make_Parameter_Specification (Loc,
3883 Defining_Identifier =>
3884 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
3885 Parameter_Type =>
3886 New_Occurrence_Of (Etype (Func_Formal), Loc)));
3888 Next_Formal (Func_Formal);
3889 end loop;
3891 -- Add an extra out parameter to carry the function result
3893 Append_To (Proc_Formals,
3894 Make_Parameter_Specification (Loc,
3895 Defining_Identifier =>
3896 Make_Defining_Identifier (Loc, Name_UP_RESULT),
3897 Out_Present => True,
3898 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
3900 -- The new procedure declaration is inserted before the function
3901 -- declaration. The processing in Build_Procedure_Body_Form relies on
3902 -- this order. Note that we insert before because in the case of a
3903 -- function body with no separate spec, we do not want to insert the
3904 -- new spec after the body which will later get rewritten.
3906 Proc_Decl :=
3907 Make_Subprogram_Declaration (Loc,
3908 Specification =>
3909 Make_Procedure_Specification (Loc,
3910 Defining_Unit_Name =>
3911 Make_Defining_Identifier (Loc, Chars (Subp)),
3912 Parameter_Specifications => Proc_Formals));
3914 Insert_Before_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
3916 -- Entity of procedure must remain invisible so that it does not
3917 -- overload subsequent references to the original function.
3919 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
3921 -- Mark the function as having a procedure form and link the function
3922 -- and its internally built procedure.
3924 Set_Rewritten_For_C (Subp);
3925 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
3926 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
3927 end Build_Procedure_Form;
3929 ------------------------
3930 -- Build_Runtime_Call --
3931 ------------------------
3933 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
3934 begin
3935 -- If entity is not available, we can skip making the call (this avoids
3936 -- junk duplicated error messages in a number of cases).
3938 if not RTE_Available (RE) then
3939 return Make_Null_Statement (Loc);
3940 else
3941 return
3942 Make_Procedure_Call_Statement (Loc,
3943 Name => New_Occurrence_Of (RTE (RE), Loc));
3944 end if;
3945 end Build_Runtime_Call;
3947 ------------------------
3948 -- Build_SS_Mark_Call --
3949 ------------------------
3951 function Build_SS_Mark_Call
3952 (Loc : Source_Ptr;
3953 Mark : Entity_Id) return Node_Id
3955 begin
3956 -- Generate:
3957 -- Mark : constant Mark_Id := SS_Mark;
3959 return
3960 Make_Object_Declaration (Loc,
3961 Defining_Identifier => Mark,
3962 Constant_Present => True,
3963 Object_Definition =>
3964 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
3965 Expression =>
3966 Make_Function_Call (Loc,
3967 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
3968 end Build_SS_Mark_Call;
3970 ---------------------------
3971 -- Build_SS_Release_Call --
3972 ---------------------------
3974 function Build_SS_Release_Call
3975 (Loc : Source_Ptr;
3976 Mark : Entity_Id) return Node_Id
3978 begin
3979 -- Generate:
3980 -- SS_Release (Mark);
3982 return
3983 Make_Procedure_Call_Statement (Loc,
3984 Name =>
3985 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
3986 Parameter_Associations => New_List (
3987 New_Occurrence_Of (Mark, Loc)));
3988 end Build_SS_Release_Call;
3990 ----------------------------
3991 -- Build_Task_Array_Image --
3992 ----------------------------
3994 -- This function generates the body for a function that constructs the
3995 -- image string for a task that is an array component. The function is
3996 -- local to the init proc for the array type, and is called for each one
3997 -- of the components. The constructed image has the form of an indexed
3998 -- component, whose prefix is the outer variable of the array type.
3999 -- The n-dimensional array type has known indexes Index, Index2...
4001 -- Id_Ref is an indexed component form created by the enclosing init proc.
4002 -- Its successive indexes are Val1, Val2, ... which are the loop variables
4003 -- in the loops that call the individual task init proc on each component.
4005 -- The generated function has the following structure:
4007 -- function F return String is
4008 -- Pref : string renames Task_Name;
4009 -- T1 : String := Index1'Image (Val1);
4010 -- ...
4011 -- Tn : String := indexn'image (Valn);
4012 -- Len : Integer := T1'Length + ... + Tn'Length + n + 1;
4013 -- -- Len includes commas and the end parentheses.
4014 -- Res : String (1..Len);
4015 -- Pos : Integer := Pref'Length;
4017 -- begin
4018 -- Res (1 .. Pos) := Pref;
4019 -- Pos := Pos + 1;
4020 -- Res (Pos) := '(';
4021 -- Pos := Pos + 1;
4022 -- Res (Pos .. Pos + T1'Length - 1) := T1;
4023 -- Pos := Pos + T1'Length;
4024 -- Res (Pos) := '.';
4025 -- Pos := Pos + 1;
4026 -- ...
4027 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
4028 -- Res (Len) := ')';
4030 -- return Res;
4031 -- end F;
4033 -- Needless to say, multidimensional arrays of tasks are rare enough that
4034 -- the bulkiness of this code is not really a concern.
4036 function Build_Task_Array_Image
4037 (Loc : Source_Ptr;
4038 Id_Ref : Node_Id;
4039 A_Type : Entity_Id;
4040 Dyn : Boolean := False) return Node_Id
4042 Dims : constant Nat := Number_Dimensions (A_Type);
4043 -- Number of dimensions for array of tasks
4045 Temps : array (1 .. Dims) of Entity_Id;
4046 -- Array of temporaries to hold string for each index
4048 Indx : Node_Id;
4049 -- Index expression
4051 Len : Entity_Id;
4052 -- Total length of generated name
4054 Pos : Entity_Id;
4055 -- Running index for substring assignments
4057 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4058 -- Name of enclosing variable, prefix of resulting name
4060 Res : Entity_Id;
4061 -- String to hold result
4063 Val : Node_Id;
4064 -- Value of successive indexes
4066 Sum : Node_Id;
4067 -- Expression to compute total size of string
4069 T : Entity_Id;
4070 -- Entity for name at one index position
4072 Decls : constant List_Id := New_List;
4073 Stats : constant List_Id := New_List;
4075 begin
4076 -- For a dynamic task, the name comes from the target variable. For a
4077 -- static one it is a formal of the enclosing init proc.
4079 if Dyn then
4080 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4081 Append_To (Decls,
4082 Make_Object_Declaration (Loc,
4083 Defining_Identifier => Pref,
4084 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4085 Expression =>
4086 Make_String_Literal (Loc,
4087 Strval => String_From_Name_Buffer)));
4089 else
4090 Append_To (Decls,
4091 Make_Object_Renaming_Declaration (Loc,
4092 Defining_Identifier => Pref,
4093 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4094 Name => Make_Identifier (Loc, Name_uTask_Name)));
4095 end if;
4097 Indx := First_Index (A_Type);
4098 Val := First (Expressions (Id_Ref));
4100 for J in 1 .. Dims loop
4101 T := Make_Temporary (Loc, 'T');
4102 Temps (J) := T;
4104 Append_To (Decls,
4105 Make_Object_Declaration (Loc,
4106 Defining_Identifier => T,
4107 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4108 Expression =>
4109 Make_Attribute_Reference (Loc,
4110 Attribute_Name => Name_Image,
4111 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
4112 Expressions => New_List (New_Copy_Tree (Val)))));
4114 Next_Index (Indx);
4115 Next (Val);
4116 end loop;
4118 Sum := Make_Integer_Literal (Loc, Dims + 1);
4120 Sum :=
4121 Make_Op_Add (Loc,
4122 Left_Opnd => Sum,
4123 Right_Opnd =>
4124 Make_Attribute_Reference (Loc,
4125 Attribute_Name => Name_Length,
4126 Prefix => New_Occurrence_Of (Pref, Loc),
4127 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4129 for J in 1 .. Dims loop
4130 Sum :=
4131 Make_Op_Add (Loc,
4132 Left_Opnd => Sum,
4133 Right_Opnd =>
4134 Make_Attribute_Reference (Loc,
4135 Attribute_Name => Name_Length,
4136 Prefix =>
4137 New_Occurrence_Of (Temps (J), Loc),
4138 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4139 end loop;
4141 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4143 Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
4145 Append_To (Stats,
4146 Make_Assignment_Statement (Loc,
4147 Name =>
4148 Make_Indexed_Component (Loc,
4149 Prefix => New_Occurrence_Of (Res, Loc),
4150 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4151 Expression =>
4152 Make_Character_Literal (Loc,
4153 Chars => Name_Find,
4154 Char_Literal_Value => UI_From_Int (Character'Pos ('(')))));
4156 Append_To (Stats,
4157 Make_Assignment_Statement (Loc,
4158 Name => New_Occurrence_Of (Pos, Loc),
4159 Expression =>
4160 Make_Op_Add (Loc,
4161 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4162 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4164 for J in 1 .. Dims loop
4166 Append_To (Stats,
4167 Make_Assignment_Statement (Loc,
4168 Name =>
4169 Make_Slice (Loc,
4170 Prefix => New_Occurrence_Of (Res, Loc),
4171 Discrete_Range =>
4172 Make_Range (Loc,
4173 Low_Bound => New_Occurrence_Of (Pos, Loc),
4174 High_Bound =>
4175 Make_Op_Subtract (Loc,
4176 Left_Opnd =>
4177 Make_Op_Add (Loc,
4178 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4179 Right_Opnd =>
4180 Make_Attribute_Reference (Loc,
4181 Attribute_Name => Name_Length,
4182 Prefix =>
4183 New_Occurrence_Of (Temps (J), Loc),
4184 Expressions =>
4185 New_List (Make_Integer_Literal (Loc, 1)))),
4186 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
4188 Expression => New_Occurrence_Of (Temps (J), Loc)));
4190 if J < Dims then
4191 Append_To (Stats,
4192 Make_Assignment_Statement (Loc,
4193 Name => New_Occurrence_Of (Pos, Loc),
4194 Expression =>
4195 Make_Op_Add (Loc,
4196 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4197 Right_Opnd =>
4198 Make_Attribute_Reference (Loc,
4199 Attribute_Name => Name_Length,
4200 Prefix => New_Occurrence_Of (Temps (J), Loc),
4201 Expressions =>
4202 New_List (Make_Integer_Literal (Loc, 1))))));
4204 Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
4206 Append_To (Stats,
4207 Make_Assignment_Statement (Loc,
4208 Name => Make_Indexed_Component (Loc,
4209 Prefix => New_Occurrence_Of (Res, Loc),
4210 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4211 Expression =>
4212 Make_Character_Literal (Loc,
4213 Chars => Name_Find,
4214 Char_Literal_Value => UI_From_Int (Character'Pos (',')))));
4216 Append_To (Stats,
4217 Make_Assignment_Statement (Loc,
4218 Name => New_Occurrence_Of (Pos, Loc),
4219 Expression =>
4220 Make_Op_Add (Loc,
4221 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4222 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4223 end if;
4224 end loop;
4226 Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
4228 Append_To (Stats,
4229 Make_Assignment_Statement (Loc,
4230 Name =>
4231 Make_Indexed_Component (Loc,
4232 Prefix => New_Occurrence_Of (Res, Loc),
4233 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
4234 Expression =>
4235 Make_Character_Literal (Loc,
4236 Chars => Name_Find,
4237 Char_Literal_Value => UI_From_Int (Character'Pos (')')))));
4238 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4239 end Build_Task_Array_Image;
4241 ----------------------------
4242 -- Build_Task_Image_Decls --
4243 ----------------------------
4245 function Build_Task_Image_Decls
4246 (Loc : Source_Ptr;
4247 Id_Ref : Node_Id;
4248 A_Type : Entity_Id;
4249 In_Init_Proc : Boolean := False) return List_Id
4251 Decls : constant List_Id := New_List;
4252 T_Id : Entity_Id := Empty;
4253 Decl : Node_Id;
4254 Expr : Node_Id := Empty;
4255 Fun : Node_Id := Empty;
4256 Is_Dyn : constant Boolean :=
4257 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
4258 and then
4259 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
4261 begin
4262 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
4263 -- generate a dummy declaration only.
4265 if Restriction_Active (No_Implicit_Heap_Allocations)
4266 or else Global_Discard_Names
4267 then
4268 T_Id := Make_Temporary (Loc, 'J');
4269 Name_Len := 0;
4271 return
4272 New_List (
4273 Make_Object_Declaration (Loc,
4274 Defining_Identifier => T_Id,
4275 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4276 Expression =>
4277 Make_String_Literal (Loc,
4278 Strval => String_From_Name_Buffer)));
4280 else
4281 if Nkind (Id_Ref) = N_Identifier
4282 or else Nkind (Id_Ref) = N_Defining_Identifier
4283 then
4284 -- For a simple variable, the image of the task is built from
4285 -- the name of the variable. To avoid possible conflict with the
4286 -- anonymous type created for a single protected object, add a
4287 -- numeric suffix.
4289 T_Id :=
4290 Make_Defining_Identifier (Loc,
4291 New_External_Name (Chars (Id_Ref), 'T', 1));
4293 Get_Name_String (Chars (Id_Ref));
4295 Expr :=
4296 Make_String_Literal (Loc,
4297 Strval => String_From_Name_Buffer);
4299 elsif Nkind (Id_Ref) = N_Selected_Component then
4300 T_Id :=
4301 Make_Defining_Identifier (Loc,
4302 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
4303 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
4305 elsif Nkind (Id_Ref) = N_Indexed_Component then
4306 T_Id :=
4307 Make_Defining_Identifier (Loc,
4308 New_External_Name (Chars (A_Type), 'N'));
4310 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
4311 end if;
4312 end if;
4314 if Present (Fun) then
4315 Append (Fun, Decls);
4316 Expr := Make_Function_Call (Loc,
4317 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
4319 if not In_Init_Proc then
4320 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4321 end if;
4322 end if;
4324 Decl := Make_Object_Declaration (Loc,
4325 Defining_Identifier => T_Id,
4326 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4327 Constant_Present => True,
4328 Expression => Expr);
4330 Append (Decl, Decls);
4331 return Decls;
4332 end Build_Task_Image_Decls;
4334 -------------------------------
4335 -- Build_Task_Image_Function --
4336 -------------------------------
4338 function Build_Task_Image_Function
4339 (Loc : Source_Ptr;
4340 Decls : List_Id;
4341 Stats : List_Id;
4342 Res : Entity_Id) return Node_Id
4344 Spec : Node_Id;
4346 begin
4347 Append_To (Stats,
4348 Make_Simple_Return_Statement (Loc,
4349 Expression => New_Occurrence_Of (Res, Loc)));
4351 Spec := Make_Function_Specification (Loc,
4352 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4353 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4355 -- Calls to 'Image use the secondary stack, which must be cleaned up
4356 -- after the task name is built.
4358 return Make_Subprogram_Body (Loc,
4359 Specification => Spec,
4360 Declarations => Decls,
4361 Handled_Statement_Sequence =>
4362 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4363 end Build_Task_Image_Function;
4365 -----------------------------
4366 -- Build_Task_Image_Prefix --
4367 -----------------------------
4369 procedure Build_Task_Image_Prefix
4370 (Loc : Source_Ptr;
4371 Len : out Entity_Id;
4372 Res : out Entity_Id;
4373 Pos : out Entity_Id;
4374 Prefix : Entity_Id;
4375 Sum : Node_Id;
4376 Decls : List_Id;
4377 Stats : List_Id)
4379 begin
4380 Len := Make_Temporary (Loc, 'L', Sum);
4382 Append_To (Decls,
4383 Make_Object_Declaration (Loc,
4384 Defining_Identifier => Len,
4385 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4386 Expression => Sum));
4388 Res := Make_Temporary (Loc, 'R');
4390 Append_To (Decls,
4391 Make_Object_Declaration (Loc,
4392 Defining_Identifier => Res,
4393 Object_Definition =>
4394 Make_Subtype_Indication (Loc,
4395 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4396 Constraint =>
4397 Make_Index_Or_Discriminant_Constraint (Loc,
4398 Constraints =>
4399 New_List (
4400 Make_Range (Loc,
4401 Low_Bound => Make_Integer_Literal (Loc, 1),
4402 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4404 -- Indicate that the result is an internal temporary, so it does not
4405 -- receive a bogus initialization when declaration is expanded. This
4406 -- is both efficient, and prevents anomalies in the handling of
4407 -- dynamic objects on the secondary stack.
4409 Set_Is_Internal (Res);
4410 Pos := Make_Temporary (Loc, 'P');
4412 Append_To (Decls,
4413 Make_Object_Declaration (Loc,
4414 Defining_Identifier => Pos,
4415 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4417 -- Pos := Prefix'Length;
4419 Append_To (Stats,
4420 Make_Assignment_Statement (Loc,
4421 Name => New_Occurrence_Of (Pos, Loc),
4422 Expression =>
4423 Make_Attribute_Reference (Loc,
4424 Attribute_Name => Name_Length,
4425 Prefix => New_Occurrence_Of (Prefix, Loc),
4426 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4428 -- Res (1 .. Pos) := Prefix;
4430 Append_To (Stats,
4431 Make_Assignment_Statement (Loc,
4432 Name =>
4433 Make_Slice (Loc,
4434 Prefix => New_Occurrence_Of (Res, Loc),
4435 Discrete_Range =>
4436 Make_Range (Loc,
4437 Low_Bound => Make_Integer_Literal (Loc, 1),
4438 High_Bound => New_Occurrence_Of (Pos, Loc))),
4440 Expression => New_Occurrence_Of (Prefix, Loc)));
4442 Append_To (Stats,
4443 Make_Assignment_Statement (Loc,
4444 Name => New_Occurrence_Of (Pos, Loc),
4445 Expression =>
4446 Make_Op_Add (Loc,
4447 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4448 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4449 end Build_Task_Image_Prefix;
4451 -----------------------------
4452 -- Build_Task_Record_Image --
4453 -----------------------------
4455 function Build_Task_Record_Image
4456 (Loc : Source_Ptr;
4457 Id_Ref : Node_Id;
4458 Dyn : Boolean := False) return Node_Id
4460 Len : Entity_Id;
4461 -- Total length of generated name
4463 Pos : Entity_Id;
4464 -- Index into result
4466 Res : Entity_Id;
4467 -- String to hold result
4469 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4470 -- Name of enclosing variable, prefix of resulting name
4472 Sum : Node_Id;
4473 -- Expression to compute total size of string
4475 Sel : Entity_Id;
4476 -- Entity for selector name
4478 Decls : constant List_Id := New_List;
4479 Stats : constant List_Id := New_List;
4481 begin
4482 -- For a dynamic task, the name comes from the target variable. For a
4483 -- static one it is a formal of the enclosing init proc.
4485 if Dyn then
4486 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4487 Append_To (Decls,
4488 Make_Object_Declaration (Loc,
4489 Defining_Identifier => Pref,
4490 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4491 Expression =>
4492 Make_String_Literal (Loc,
4493 Strval => String_From_Name_Buffer)));
4495 else
4496 Append_To (Decls,
4497 Make_Object_Renaming_Declaration (Loc,
4498 Defining_Identifier => Pref,
4499 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4500 Name => Make_Identifier (Loc, Name_uTask_Name)));
4501 end if;
4503 Sel := Make_Temporary (Loc, 'S');
4505 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4507 Append_To (Decls,
4508 Make_Object_Declaration (Loc,
4509 Defining_Identifier => Sel,
4510 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4511 Expression =>
4512 Make_String_Literal (Loc,
4513 Strval => String_From_Name_Buffer)));
4515 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4517 Sum :=
4518 Make_Op_Add (Loc,
4519 Left_Opnd => Sum,
4520 Right_Opnd =>
4521 Make_Attribute_Reference (Loc,
4522 Attribute_Name => Name_Length,
4523 Prefix =>
4524 New_Occurrence_Of (Pref, Loc),
4525 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4527 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4529 Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
4531 -- Res (Pos) := '.';
4533 Append_To (Stats,
4534 Make_Assignment_Statement (Loc,
4535 Name => Make_Indexed_Component (Loc,
4536 Prefix => New_Occurrence_Of (Res, Loc),
4537 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4538 Expression =>
4539 Make_Character_Literal (Loc,
4540 Chars => Name_Find,
4541 Char_Literal_Value =>
4542 UI_From_Int (Character'Pos ('.')))));
4544 Append_To (Stats,
4545 Make_Assignment_Statement (Loc,
4546 Name => New_Occurrence_Of (Pos, Loc),
4547 Expression =>
4548 Make_Op_Add (Loc,
4549 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4550 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4552 -- Res (Pos .. Len) := Selector;
4554 Append_To (Stats,
4555 Make_Assignment_Statement (Loc,
4556 Name => Make_Slice (Loc,
4557 Prefix => New_Occurrence_Of (Res, Loc),
4558 Discrete_Range =>
4559 Make_Range (Loc,
4560 Low_Bound => New_Occurrence_Of (Pos, Loc),
4561 High_Bound => New_Occurrence_Of (Len, Loc))),
4562 Expression => New_Occurrence_Of (Sel, Loc)));
4564 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4565 end Build_Task_Record_Image;
4567 ---------------------------------------
4568 -- Build_Transient_Object_Statements --
4569 ---------------------------------------
4571 procedure Build_Transient_Object_Statements
4572 (Obj_Decl : Node_Id;
4573 Fin_Call : out Node_Id;
4574 Hook_Assign : out Node_Id;
4575 Hook_Clear : out Node_Id;
4576 Hook_Decl : out Node_Id;
4577 Ptr_Decl : out Node_Id;
4578 Finalize_Obj : Boolean := True)
4580 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4581 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4582 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4584 Desig_Typ : Entity_Id;
4585 Hook_Expr : Node_Id;
4586 Hook_Id : Entity_Id;
4587 Obj_Ref : Node_Id;
4588 Ptr_Typ : Entity_Id;
4590 begin
4591 -- Recover the type of the object
4593 Desig_Typ := Obj_Typ;
4595 if Is_Access_Type (Desig_Typ) then
4596 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4597 end if;
4599 -- Create an access type which provides a reference to the transient
4600 -- object. Generate:
4602 -- type Ptr_Typ is access all Desig_Typ;
4604 Ptr_Typ := Make_Temporary (Loc, 'A');
4605 Mutate_Ekind (Ptr_Typ, E_General_Access_Type);
4606 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4608 Ptr_Decl :=
4609 Make_Full_Type_Declaration (Loc,
4610 Defining_Identifier => Ptr_Typ,
4611 Type_Definition =>
4612 Make_Access_To_Object_Definition (Loc,
4613 All_Present => True,
4614 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4616 -- Create a temporary check which acts as a hook to the transient
4617 -- object. Generate:
4619 -- Hook : Ptr_Typ := null;
4621 Hook_Id := Make_Temporary (Loc, 'T');
4622 Mutate_Ekind (Hook_Id, E_Variable);
4623 Set_Etype (Hook_Id, Ptr_Typ);
4625 Hook_Decl :=
4626 Make_Object_Declaration (Loc,
4627 Defining_Identifier => Hook_Id,
4628 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4629 Expression => Make_Null (Loc));
4631 -- Mark the temporary as a hook. This signals the machinery in
4632 -- Build_Finalizer to recognize this special case.
4634 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4636 -- Hook the transient object to the temporary. Generate:
4638 -- Hook := Ptr_Typ (Obj_Id);
4639 -- <or>
4640 -- Hool := Obj_Id'Unrestricted_Access;
4642 if Is_Access_Type (Obj_Typ) then
4643 Hook_Expr :=
4644 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4645 else
4646 Hook_Expr :=
4647 Make_Attribute_Reference (Loc,
4648 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4649 Attribute_Name => Name_Unrestricted_Access);
4650 end if;
4652 Hook_Assign :=
4653 Make_Assignment_Statement (Loc,
4654 Name => New_Occurrence_Of (Hook_Id, Loc),
4655 Expression => Hook_Expr);
4657 -- Crear the hook prior to finalizing the object. Generate:
4659 -- Hook := null;
4661 Hook_Clear :=
4662 Make_Assignment_Statement (Loc,
4663 Name => New_Occurrence_Of (Hook_Id, Loc),
4664 Expression => Make_Null (Loc));
4666 -- Finalize the object. Generate:
4668 -- [Deep_]Finalize (Obj_Ref[.all]);
4670 if Finalize_Obj then
4671 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4673 if Is_Access_Type (Obj_Typ) then
4674 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4675 Set_Etype (Obj_Ref, Desig_Typ);
4676 end if;
4678 Fin_Call :=
4679 Make_Final_Call
4680 (Obj_Ref => Obj_Ref,
4681 Typ => Desig_Typ);
4683 -- Otherwise finalize the hook. Generate:
4685 -- [Deep_]Finalize (Hook.all);
4687 else
4688 Fin_Call :=
4689 Make_Final_Call (
4690 Obj_Ref =>
4691 Make_Explicit_Dereference (Loc,
4692 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4693 Typ => Desig_Typ);
4694 end if;
4695 end Build_Transient_Object_Statements;
4697 -----------------------------
4698 -- Check_Float_Op_Overflow --
4699 -----------------------------
4701 procedure Check_Float_Op_Overflow (N : Node_Id) is
4702 begin
4703 -- Return if no check needed
4705 if not Is_Floating_Point_Type (Etype (N))
4706 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4708 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4709 -- and do not expand the code for float overflow checking.
4711 or else CodePeer_Mode
4712 then
4713 return;
4714 end if;
4716 -- Otherwise we replace the expression by
4718 -- do Tnn : constant ftype := expression;
4719 -- constraint_error when not Tnn'Valid;
4720 -- in Tnn;
4722 declare
4723 Loc : constant Source_Ptr := Sloc (N);
4724 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4725 Typ : constant Entity_Id := Etype (N);
4727 begin
4728 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4729 -- right here. We also set the node as analyzed to prevent infinite
4730 -- recursion from repeating the operation in the expansion.
4732 Set_Do_Overflow_Check (N, False);
4733 Set_Analyzed (N, True);
4735 -- Do the rewrite to include the check
4737 Rewrite (N,
4738 Make_Expression_With_Actions (Loc,
4739 Actions => New_List (
4740 Make_Object_Declaration (Loc,
4741 Defining_Identifier => Tnn,
4742 Object_Definition => New_Occurrence_Of (Typ, Loc),
4743 Constant_Present => True,
4744 Expression => Relocate_Node (N)),
4745 Make_Raise_Constraint_Error (Loc,
4746 Condition =>
4747 Make_Op_Not (Loc,
4748 Right_Opnd =>
4749 Make_Attribute_Reference (Loc,
4750 Prefix => New_Occurrence_Of (Tnn, Loc),
4751 Attribute_Name => Name_Valid)),
4752 Reason => CE_Overflow_Check_Failed)),
4753 Expression => New_Occurrence_Of (Tnn, Loc)));
4755 Analyze_And_Resolve (N, Typ);
4756 end;
4757 end Check_Float_Op_Overflow;
4759 ----------------------------------
4760 -- Component_May_Be_Bit_Aligned --
4761 ----------------------------------
4763 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4764 UT : Entity_Id;
4766 begin
4767 -- If no component clause, then everything is fine, since the back end
4768 -- never misaligns from byte boundaries by default, even if there is a
4769 -- pragma Pack for the record.
4771 if No (Comp) or else No (Component_Clause (Comp)) then
4772 return False;
4773 end if;
4775 UT := Underlying_Type (Etype (Comp));
4777 -- It is only array and record types that cause trouble
4779 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4780 return False;
4782 -- If we know that we have a small (at most the maximum integer size)
4783 -- record or bit-packed array, then everything is fine, since the back
4784 -- end can handle these cases correctly.
4786 elsif Known_Esize (Comp)
4787 and then Esize (Comp) <= System_Max_Integer_Size
4788 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4789 then
4790 return False;
4792 elsif not Known_Normalized_First_Bit (Comp) then
4793 return True;
4795 -- Otherwise if the component is not byte aligned, we know we have the
4796 -- nasty unaligned case.
4798 elsif Normalized_First_Bit (Comp) /= Uint_0
4799 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4800 then
4801 return True;
4803 -- If we are large and byte aligned, then OK at this level
4805 else
4806 return False;
4807 end if;
4808 end Component_May_Be_Bit_Aligned;
4810 -------------------------------
4811 -- Convert_To_Actual_Subtype --
4812 -------------------------------
4814 procedure Convert_To_Actual_Subtype (Exp : Node_Id) is
4815 Act_ST : Entity_Id;
4817 begin
4818 Act_ST := Get_Actual_Subtype (Exp);
4820 if Act_ST = Etype (Exp) then
4821 return;
4822 else
4823 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4824 Analyze_And_Resolve (Exp, Act_ST);
4825 end if;
4826 end Convert_To_Actual_Subtype;
4828 -----------------------------------
4829 -- Corresponding_Runtime_Package --
4830 -----------------------------------
4832 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4833 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4834 -- Return True if protected type T has one entry and the maximum queue
4835 -- length is one.
4837 --------------------------------
4838 -- Has_One_Entry_And_No_Queue --
4839 --------------------------------
4841 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4842 Item : Entity_Id;
4843 Is_First : Boolean := True;
4845 begin
4846 Item := First_Entity (T);
4847 while Present (Item) loop
4848 if Is_Entry (Item) then
4850 -- The protected type has more than one entry
4852 if not Is_First then
4853 return False;
4854 end if;
4856 -- The queue length is not one
4858 if not Restriction_Active (No_Entry_Queue)
4859 and then Get_Max_Queue_Length (Item) /= Uint_1
4860 then
4861 return False;
4862 end if;
4864 Is_First := False;
4865 end if;
4867 Next_Entity (Item);
4868 end loop;
4870 return True;
4871 end Has_One_Entry_And_No_Queue;
4873 -- Local variables
4875 Pkg_Id : RTU_Id := RTU_Null;
4877 -- Start of processing for Corresponding_Runtime_Package
4879 begin
4880 pragma Assert (Is_Concurrent_Type (Typ));
4882 if Is_Protected_Type (Typ) then
4883 if Has_Entries (Typ)
4885 -- A protected type without entries that covers an interface and
4886 -- overrides the abstract routines with protected procedures is
4887 -- considered equivalent to a protected type with entries in the
4888 -- context of dispatching select statements. It is sufficient to
4889 -- check for the presence of an interface list in the declaration
4890 -- node to recognize this case.
4892 or else Present (Interface_List (Parent (Typ)))
4894 -- Protected types with interrupt handlers (when not using a
4895 -- restricted profile) are also considered equivalent to
4896 -- protected types with entries. The types which are used
4897 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
4898 -- are derived from Protection_Entries.
4900 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
4901 or else Has_Interrupt_Handler (Typ)
4902 then
4903 if Abort_Allowed
4904 or else Restriction_Active (No_Select_Statements) = False
4905 or else not Has_One_Entry_And_No_Queue (Typ)
4906 or else (Has_Attach_Handler (Typ)
4907 and then not Restricted_Profile)
4908 then
4909 Pkg_Id := System_Tasking_Protected_Objects_Entries;
4910 else
4911 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
4912 end if;
4914 else
4915 Pkg_Id := System_Tasking_Protected_Objects;
4916 end if;
4917 end if;
4919 return Pkg_Id;
4920 end Corresponding_Runtime_Package;
4922 -----------------------------------
4923 -- Current_Sem_Unit_Declarations --
4924 -----------------------------------
4926 function Current_Sem_Unit_Declarations return List_Id is
4927 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
4928 Decls : List_Id;
4930 begin
4931 -- If the current unit is a package body, locate the visible
4932 -- declarations of the package spec.
4934 if Nkind (U) = N_Package_Body then
4935 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
4936 end if;
4938 if Nkind (U) = N_Package_Declaration then
4939 U := Specification (U);
4940 Decls := Visible_Declarations (U);
4942 if No (Decls) then
4943 Decls := New_List;
4944 Set_Visible_Declarations (U, Decls);
4945 end if;
4947 else
4948 Decls := Declarations (U);
4950 if No (Decls) then
4951 Decls := New_List;
4952 Set_Declarations (U, Decls);
4953 end if;
4954 end if;
4956 return Decls;
4957 end Current_Sem_Unit_Declarations;
4959 -----------------------
4960 -- Duplicate_Subexpr --
4961 -----------------------
4963 function Duplicate_Subexpr
4964 (Exp : Node_Id;
4965 Name_Req : Boolean := False;
4966 Renaming_Req : Boolean := False) return Node_Id
4968 begin
4969 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
4970 return New_Copy_Tree (Exp);
4971 end Duplicate_Subexpr;
4973 ---------------------------------
4974 -- Duplicate_Subexpr_No_Checks --
4975 ---------------------------------
4977 function Duplicate_Subexpr_No_Checks
4978 (Exp : Node_Id;
4979 Name_Req : Boolean := False;
4980 Renaming_Req : Boolean := False;
4981 Related_Id : Entity_Id := Empty;
4982 Is_Low_Bound : Boolean := False;
4983 Is_High_Bound : Boolean := False) return Node_Id
4985 New_Exp : Node_Id;
4987 begin
4988 Remove_Side_Effects
4989 (Exp => Exp,
4990 Name_Req => Name_Req,
4991 Renaming_Req => Renaming_Req,
4992 Related_Id => Related_Id,
4993 Is_Low_Bound => Is_Low_Bound,
4994 Is_High_Bound => Is_High_Bound);
4996 New_Exp := New_Copy_Tree (Exp);
4997 Remove_Checks (New_Exp);
4998 return New_Exp;
4999 end Duplicate_Subexpr_No_Checks;
5001 -----------------------------------
5002 -- Duplicate_Subexpr_Move_Checks --
5003 -----------------------------------
5005 function Duplicate_Subexpr_Move_Checks
5006 (Exp : Node_Id;
5007 Name_Req : Boolean := False;
5008 Renaming_Req : Boolean := False) return Node_Id
5010 New_Exp : Node_Id;
5012 begin
5013 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5014 New_Exp := New_Copy_Tree (Exp);
5015 Remove_Checks (Exp);
5016 return New_Exp;
5017 end Duplicate_Subexpr_Move_Checks;
5019 -------------------------
5020 -- Enclosing_Init_Proc --
5021 -------------------------
5023 function Enclosing_Init_Proc return Entity_Id is
5024 S : Entity_Id;
5026 begin
5027 S := Current_Scope;
5028 while Present (S) and then S /= Standard_Standard loop
5029 if Is_Init_Proc (S) then
5030 return S;
5031 else
5032 S := Scope (S);
5033 end if;
5034 end loop;
5036 return Empty;
5037 end Enclosing_Init_Proc;
5039 --------------------
5040 -- Ensure_Defined --
5041 --------------------
5043 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
5044 IR : Node_Id;
5046 begin
5047 -- An itype reference must only be created if this is a local itype, so
5048 -- that gigi can elaborate it on the proper objstack.
5050 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
5051 IR := Make_Itype_Reference (Sloc (N));
5052 Set_Itype (IR, Typ);
5053 Insert_Action (N, IR);
5054 end if;
5055 end Ensure_Defined;
5057 --------------------
5058 -- Entry_Names_OK --
5059 --------------------
5061 function Entry_Names_OK return Boolean is
5062 begin
5063 return
5064 not Restricted_Profile
5065 and then not Global_Discard_Names
5066 and then not Restriction_Active (No_Implicit_Heap_Allocations)
5067 and then not Restriction_Active (No_Local_Allocators);
5068 end Entry_Names_OK;
5070 -------------------
5071 -- Evaluate_Name --
5072 -------------------
5074 procedure Evaluate_Name (Nam : Node_Id) is
5075 begin
5076 case Nkind (Nam) is
5077 -- For an aggregate, force its evaluation
5079 when N_Aggregate =>
5080 Force_Evaluation (Nam);
5082 -- For an attribute reference or an indexed component, evaluate the
5083 -- prefix, which is itself a name, recursively, and then force the
5084 -- evaluation of all the subscripts (or attribute expressions).
5086 when N_Attribute_Reference
5087 | N_Indexed_Component
5089 Evaluate_Name (Prefix (Nam));
5091 declare
5092 E : Node_Id;
5094 begin
5095 E := First (Expressions (Nam));
5096 while Present (E) loop
5097 Force_Evaluation (E);
5099 if Is_Rewrite_Substitution (E) then
5100 Set_Do_Range_Check
5101 (E, Do_Range_Check (Original_Node (E)));
5102 end if;
5104 Next (E);
5105 end loop;
5106 end;
5108 -- For an explicit dereference, we simply force the evaluation of
5109 -- the name expression. The dereference provides a value that is the
5110 -- address for the renamed object, and it is precisely this value
5111 -- that we want to preserve.
5113 when N_Explicit_Dereference =>
5114 Force_Evaluation (Prefix (Nam));
5116 -- For a function call, we evaluate the call; same for an operator
5118 when N_Function_Call
5119 | N_Op
5121 Force_Evaluation (Nam);
5123 -- For a qualified expression, we evaluate the expression
5125 when N_Qualified_Expression =>
5126 Evaluate_Name (Expression (Nam));
5128 -- For a selected component, we simply evaluate the prefix
5130 when N_Selected_Component =>
5131 Evaluate_Name (Prefix (Nam));
5133 -- For a slice, we evaluate the prefix, as for the indexed component
5134 -- case and then, if there is a range present, either directly or as
5135 -- the constraint of a discrete subtype indication, we evaluate the
5136 -- two bounds of this range.
5138 when N_Slice =>
5139 Evaluate_Name (Prefix (Nam));
5140 Evaluate_Slice_Bounds (Nam);
5142 -- For a type conversion, the expression of the conversion must be
5143 -- the name of an object, and we simply need to evaluate this name.
5145 when N_Type_Conversion =>
5146 Evaluate_Name (Expression (Nam));
5148 -- The remaining cases are direct name and character literal. In all
5149 -- these cases, we do nothing, since we want to reevaluate each time
5150 -- the renamed object is used. ??? There are more remaining cases, at
5151 -- least in the GNATprove_Mode, where this routine is called in more
5152 -- contexts than in GNAT.
5154 when others =>
5155 null;
5156 end case;
5157 end Evaluate_Name;
5159 ---------------------------
5160 -- Evaluate_Slice_Bounds --
5161 ---------------------------
5163 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
5164 DR : constant Node_Id := Discrete_Range (Slice);
5165 Constr : Node_Id;
5166 Rexpr : Node_Id;
5168 begin
5169 if Nkind (DR) = N_Range then
5170 Force_Evaluation (Low_Bound (DR));
5171 Force_Evaluation (High_Bound (DR));
5173 elsif Nkind (DR) = N_Subtype_Indication then
5174 Constr := Constraint (DR);
5176 if Nkind (Constr) = N_Range_Constraint then
5177 Rexpr := Range_Expression (Constr);
5179 Force_Evaluation (Low_Bound (Rexpr));
5180 Force_Evaluation (High_Bound (Rexpr));
5181 end if;
5182 end if;
5183 end Evaluate_Slice_Bounds;
5185 ---------------------
5186 -- Evolve_And_Then --
5187 ---------------------
5189 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
5190 begin
5191 if No (Cond) then
5192 Cond := Cond1;
5193 else
5194 Cond :=
5195 Make_And_Then (Sloc (Cond1),
5196 Left_Opnd => Cond,
5197 Right_Opnd => Cond1);
5198 end if;
5199 end Evolve_And_Then;
5201 --------------------
5202 -- Evolve_Or_Else --
5203 --------------------
5205 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
5206 begin
5207 if No (Cond) then
5208 Cond := Cond1;
5209 else
5210 Cond :=
5211 Make_Or_Else (Sloc (Cond1),
5212 Left_Opnd => Cond,
5213 Right_Opnd => Cond1);
5214 end if;
5215 end Evolve_Or_Else;
5217 -------------------------------
5218 -- Expand_Sliding_Conversion --
5219 -------------------------------
5221 procedure Expand_Sliding_Conversion (N : Node_Id; Arr_Typ : Entity_Id) is
5223 pragma Assert (Is_Array_Type (Arr_Typ)
5224 and then not Is_Constrained (Arr_Typ)
5225 and then Is_Fixed_Lower_Bound_Array_Subtype (Arr_Typ));
5227 Constraints : List_Id;
5228 Index : Node_Id := First_Index (Arr_Typ);
5229 Loc : constant Source_Ptr := Sloc (N);
5230 Subt_Decl : Node_Id;
5231 Subt : Entity_Id;
5232 Subt_Low : Node_Id;
5233 Subt_High : Node_Id;
5235 Act_Subt : Entity_Id;
5236 Act_Index : Node_Id;
5237 Act_Low : Node_Id;
5238 Act_High : Node_Id;
5239 Adjust_Incr : Node_Id;
5240 Dimension : Int := 0;
5241 All_FLBs_Match : Boolean := True;
5243 begin
5244 -- This procedure is called during semantic analysis, and we only expand
5245 -- a sliding conversion when Expander_Active, to avoid doing it during
5246 -- preanalysis (which can lead to problems with the target subtype not
5247 -- getting properly expanded during later full analysis). Also, sliding
5248 -- should never be needed for string literals, because their bounds are
5249 -- determined directly based on the fixed lower bound of Arr_Typ and
5250 -- their length.
5252 if Expander_Active and then Nkind (N) /= N_String_Literal then
5253 Constraints := New_List;
5255 Act_Subt := Get_Actual_Subtype (N);
5256 Act_Index := First_Index (Act_Subt);
5258 -- Loop over the indexes of the fixed-lower-bound array type or
5259 -- subtype to build up an index constraint for constructing the
5260 -- subtype that will be the target of a conversion of the array
5261 -- object that may need a sliding conversion.
5263 while Present (Index) loop
5264 pragma Assert (Present (Act_Index));
5266 Dimension := Dimension + 1;
5268 Get_Index_Bounds (Act_Index, Act_Low, Act_High);
5270 -- If Index defines a normal unconstrained range (range <>),
5271 -- then we will simply use the bounds of the actual subtype's
5272 -- corresponding index range.
5274 if not Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index)) then
5275 Subt_Low := Act_Low;
5276 Subt_High := Act_High;
5278 -- Otherwise, a range will be created with a low bound given by
5279 -- the fixed lower bound of the array subtype's index, and with
5280 -- high bound given by (Actual'Length + fixed lower bound - 1).
5282 else
5283 if Nkind (Index) = N_Subtype_Indication then
5284 Subt_Low :=
5285 New_Copy_Tree
5286 (Low_Bound (Range_Expression (Constraint (Index))));
5287 else
5288 pragma Assert (Nkind (Index) = N_Range);
5290 Subt_Low := New_Copy_Tree (Low_Bound (Index));
5291 end if;
5293 -- If either we have a nonstatic lower bound, or the target and
5294 -- source subtypes are statically known to have unequal lower
5295 -- bounds, then we will need to make a subtype conversion to
5296 -- slide the bounds. However, if all of the indexes' lower
5297 -- bounds are static and known to be equal (the common case),
5298 -- then no conversion will be needed, and we'll end up not
5299 -- creating the subtype or the conversion (though we still
5300 -- build up the index constraint, which will simply be unused).
5302 if not (Compile_Time_Known_Value (Subt_Low)
5303 and then Compile_Time_Known_Value (Act_Low))
5304 or else Expr_Value (Subt_Low) /= Expr_Value (Act_Low)
5305 then
5306 All_FLBs_Match := False;
5307 end if;
5309 -- Apply 'Pos to lower bound, which may be of an enumeration
5310 -- type, before subtracting.
5312 Adjust_Incr :=
5313 Make_Op_Subtract (Loc,
5314 Make_Attribute_Reference (Loc,
5315 Prefix =>
5316 New_Occurrence_Of (Etype (Act_Index), Loc),
5317 Attribute_Name =>
5318 Name_Pos,
5319 Expressions =>
5320 New_List (New_Copy_Tree (Subt_Low))),
5321 Make_Integer_Literal (Loc, 1));
5323 -- Apply 'Val to the result of adding the increment to the
5324 -- length, to handle indexes of enumeration types.
5326 Subt_High :=
5327 Make_Attribute_Reference (Loc,
5328 Prefix =>
5329 New_Occurrence_Of (Etype (Act_Index), Loc),
5330 Attribute_Name =>
5331 Name_Val,
5332 Expressions =>
5333 New_List (Make_Op_Add (Loc,
5334 Make_Attribute_Reference (Loc,
5335 Prefix =>
5336 New_Occurrence_Of (Act_Subt, Loc),
5337 Attribute_Name =>
5338 Name_Length,
5339 Expressions =>
5340 New_List
5341 (Make_Integer_Literal
5342 (Loc, Dimension))),
5343 Adjust_Incr)));
5344 end if;
5346 Append (Make_Range (Loc, Subt_Low, Subt_High), Constraints);
5348 Next (Index);
5349 Next (Act_Index);
5350 end loop;
5352 -- If for each index with a fixed lower bound (FLB), the lower bound
5353 -- of the corresponding index of the actual subtype is statically
5354 -- known be equal to the FLB, then a sliding conversion isn't needed
5355 -- at all, so just return without building a subtype or conversion.
5357 if All_FLBs_Match then
5358 return;
5359 end if;
5361 -- A sliding conversion is needed, so create the target subtype using
5362 -- the index constraint created above, and rewrite the expression
5363 -- as a conversion to that subtype.
5365 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
5366 Set_Is_Internal (Subt);
5368 Subt_Decl :=
5369 Make_Subtype_Declaration (Loc,
5370 Defining_Identifier => Subt,
5371 Subtype_Indication =>
5372 Make_Subtype_Indication (Loc,
5373 Subtype_Mark =>
5374 New_Occurrence_Of (Arr_Typ, Loc),
5375 Constraint =>
5376 Make_Index_Or_Discriminant_Constraint (Loc,
5377 Constraints => Constraints)));
5379 Mark_Rewrite_Insertion (Subt_Decl);
5381 -- The actual subtype is an Itype, so we analyze the declaration,
5382 -- but do not attach it to the tree.
5384 Set_Parent (Subt_Decl, N);
5385 Set_Is_Itype (Subt);
5386 Analyze (Subt_Decl, Suppress => All_Checks);
5387 Set_Associated_Node_For_Itype (Subt, N);
5388 Set_Has_Delayed_Freeze (Subt, False);
5390 -- We need to freeze the actual subtype immediately. This is needed
5391 -- because otherwise this Itype will not get frozen at all, and it is
5392 -- always safe to freeze on creation because any associated types
5393 -- must be frozen at this point.
5395 Freeze_Itype (Subt, N);
5397 Rewrite (N,
5398 Make_Type_Conversion (Loc,
5399 Subtype_Mark =>
5400 New_Occurrence_Of (Subt, Loc),
5401 Expression => Relocate_Node (N)));
5402 Analyze (N);
5403 end if;
5404 end Expand_Sliding_Conversion;
5406 -----------------------------------------
5407 -- Expand_Static_Predicates_In_Choices --
5408 -----------------------------------------
5410 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
5411 pragma Assert (Nkind (N) in N_Case_Statement_Alternative | N_Variant);
5413 Choices : List_Id := Discrete_Choices (N);
5415 Choice : Node_Id;
5416 Next_C : Node_Id;
5417 P : Node_Id;
5418 C : Node_Id;
5420 begin
5421 -- If this is an "others" alternative, we need to process any static
5422 -- predicates in its Others_Discrete_Choices.
5424 if Nkind (First (Choices)) = N_Others_Choice then
5425 Choices := Others_Discrete_Choices (First (Choices));
5426 end if;
5428 Choice := First (Choices);
5429 while Present (Choice) loop
5430 Next_C := Next (Choice);
5432 -- Check for name of subtype with static predicate
5434 if Is_Entity_Name (Choice)
5435 and then Is_Type (Entity (Choice))
5436 and then Has_Predicates (Entity (Choice))
5437 then
5438 -- Loop through entries in predicate list, converting to choices
5439 -- and inserting in the list before the current choice. Note that
5440 -- if the list is empty, corresponding to a False predicate, then
5441 -- no choices are inserted.
5443 P := First (Static_Discrete_Predicate (Entity (Choice)));
5444 while Present (P) loop
5446 -- If low bound and high bounds are equal, copy simple choice
5448 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
5449 C := New_Copy (Low_Bound (P));
5451 -- Otherwise copy a range
5453 else
5454 C := New_Copy (P);
5455 end if;
5457 -- Change Sloc to referencing choice (rather than the Sloc of
5458 -- the predicate declaration element itself).
5460 Set_Sloc (C, Sloc (Choice));
5461 Insert_Before (Choice, C);
5462 Next (P);
5463 end loop;
5465 -- Delete the predicated entry
5467 Remove (Choice);
5468 end if;
5470 -- Move to next choice to check
5472 Choice := Next_C;
5473 end loop;
5475 Set_Has_SP_Choice (N, False);
5476 end Expand_Static_Predicates_In_Choices;
5478 ------------------------------
5479 -- Expand_Subtype_From_Expr --
5480 ------------------------------
5482 -- This function is applicable for both static and dynamic allocation of
5483 -- objects which are constrained by an initial expression. Basically it
5484 -- transforms an unconstrained subtype indication into a constrained one.
5486 -- The expression may also be transformed in certain cases in order to
5487 -- avoid multiple evaluation. In the static allocation case, the general
5488 -- scheme is:
5490 -- Val : T := Expr;
5492 -- is transformed into
5494 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5496 -- Here are the main cases :
5498 -- <if Expr is a Slice>
5499 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5501 -- <elsif Expr is a String Literal>
5502 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5504 -- <elsif Expr is Constrained>
5505 -- subtype T is Type_Of_Expr
5506 -- Val : T := Expr;
5508 -- <elsif Expr is an entity_name>
5509 -- Val : T (constraints taken from Expr) := Expr;
5511 -- <else>
5512 -- type Axxx is access all T;
5513 -- Rval : Axxx := Expr'ref;
5514 -- Val : T (constraints taken from Rval) := Rval.all;
5516 -- ??? note: when the Expression is allocated in the secondary stack
5517 -- we could use it directly instead of copying it by declaring
5518 -- Val : T (...) renames Rval.all
5520 procedure Expand_Subtype_From_Expr
5521 (N : Node_Id;
5522 Unc_Type : Entity_Id;
5523 Subtype_Indic : Node_Id;
5524 Exp : Node_Id;
5525 Related_Id : Entity_Id := Empty)
5527 Loc : constant Source_Ptr := Sloc (N);
5528 Exp_Typ : constant Entity_Id := Etype (Exp);
5529 T : Entity_Id;
5531 begin
5532 -- In general we cannot build the subtype if expansion is disabled,
5533 -- because internal entities may not have been defined. However, to
5534 -- avoid some cascaded errors, we try to continue when the expression is
5535 -- an array (or string), because it is safe to compute the bounds. It is
5536 -- in fact required to do so even in a generic context, because there
5537 -- may be constants that depend on the bounds of a string literal, both
5538 -- standard string types and more generally arrays of characters.
5540 -- In GNATprove mode, these extra subtypes are not needed, unless Exp is
5541 -- a static expression. In that case, the subtype will be constrained
5542 -- while the original type might be unconstrained, so expanding the type
5543 -- is necessary both for passing legality checks in GNAT and for precise
5544 -- analysis in GNATprove.
5546 if GNATprove_Mode and then not Is_Static_Expression (Exp) then
5547 return;
5548 end if;
5550 if not Expander_Active
5551 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5552 then
5553 return;
5554 end if;
5556 if Nkind (Exp) = N_Slice then
5557 declare
5558 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5560 begin
5561 Rewrite (Subtype_Indic,
5562 Make_Subtype_Indication (Loc,
5563 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5564 Constraint =>
5565 Make_Index_Or_Discriminant_Constraint (Loc,
5566 Constraints => New_List
5567 (New_Occurrence_Of (Slice_Type, Loc)))));
5569 -- This subtype indication may be used later for constraint checks
5570 -- we better make sure that if a variable was used as a bound of
5571 -- the original slice, its value is frozen.
5573 Evaluate_Slice_Bounds (Exp);
5574 end;
5576 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5577 Rewrite (Subtype_Indic,
5578 Make_Subtype_Indication (Loc,
5579 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5580 Constraint =>
5581 Make_Index_Or_Discriminant_Constraint (Loc,
5582 Constraints => New_List (
5583 Make_Literal_Range (Loc,
5584 Literal_Typ => Exp_Typ)))));
5586 -- If the type of the expression is an internally generated type it
5587 -- may not be necessary to create a new subtype. However there are two
5588 -- exceptions: references to the current instances, and aliased array
5589 -- object declarations for which the back end has to create a template.
5591 elsif Is_Constrained (Exp_Typ)
5592 and then not Is_Class_Wide_Type (Unc_Type)
5593 and then
5594 (Nkind (N) /= N_Object_Declaration
5595 or else not Is_Entity_Name (Expression (N))
5596 or else not Comes_From_Source (Entity (Expression (N)))
5597 or else not Is_Array_Type (Exp_Typ)
5598 or else not Aliased_Present (N))
5599 then
5600 if Is_Itype (Exp_Typ) then
5602 -- Within an initialization procedure, a selected component
5603 -- denotes a component of the enclosing record, and it appears as
5604 -- an actual in a call to its own initialization procedure. If
5605 -- this component depends on the outer discriminant, we must
5606 -- generate the proper actual subtype for it.
5608 if Nkind (Exp) = N_Selected_Component
5609 and then Within_Init_Proc
5610 then
5611 declare
5612 Decl : constant Node_Id :=
5613 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5614 begin
5615 if Present (Decl) then
5616 Insert_Action (N, Decl);
5617 T := Defining_Identifier (Decl);
5618 else
5619 T := Exp_Typ;
5620 end if;
5621 end;
5623 -- No need to generate a new subtype
5625 else
5626 T := Exp_Typ;
5627 end if;
5629 else
5630 T := Make_Temporary (Loc, 'T');
5632 Insert_Action (N,
5633 Make_Subtype_Declaration (Loc,
5634 Defining_Identifier => T,
5635 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5637 -- This type is marked as an itype even though it has an explicit
5638 -- declaration since otherwise Is_Generic_Actual_Type can get
5639 -- set, resulting in the generation of spurious errors. (See
5640 -- sem_ch8.Analyze_Package_Renaming and sem_type.covers)
5642 Set_Is_Itype (T);
5643 Set_Associated_Node_For_Itype (T, Exp);
5644 end if;
5646 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5648 -- Nothing needs to be done for private types with unknown discriminants
5649 -- if the underlying type is not an unconstrained composite type or it
5650 -- is an unchecked union.
5652 elsif Is_Private_Type (Unc_Type)
5653 and then Has_Unknown_Discriminants (Unc_Type)
5654 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5655 or else Is_Constrained (Underlying_Type (Unc_Type))
5656 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5657 then
5658 null;
5660 -- Case of derived type with unknown discriminants where the parent type
5661 -- also has unknown discriminants.
5663 elsif Is_Record_Type (Unc_Type)
5664 and then not Is_Class_Wide_Type (Unc_Type)
5665 and then Has_Unknown_Discriminants (Unc_Type)
5666 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5667 then
5668 -- Nothing to be done if no underlying record view available
5670 -- If this is a limited type derived from a type with unknown
5671 -- discriminants, do not expand either, so that subsequent expansion
5672 -- of the call can add build-in-place parameters to call.
5674 if No (Underlying_Record_View (Unc_Type))
5675 or else Is_Limited_Type (Unc_Type)
5676 then
5677 null;
5679 -- Otherwise use the Underlying_Record_View to create the proper
5680 -- constrained subtype for an object of a derived type with unknown
5681 -- discriminants.
5683 else
5684 Remove_Side_Effects (Exp);
5685 Rewrite (Subtype_Indic,
5686 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5687 end if;
5689 -- Renamings of class-wide interface types require no equivalent
5690 -- constrained type declarations because we only need to reference
5691 -- the tag component associated with the interface. The same is
5692 -- presumably true for class-wide types in general, so this test
5693 -- is broadened to include all class-wide renamings, which also
5694 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5695 -- (Is this really correct, or are there some cases of class-wide
5696 -- renamings that require action in this procedure???)
5698 elsif Present (N)
5699 and then Nkind (N) = N_Object_Renaming_Declaration
5700 and then Is_Class_Wide_Type (Unc_Type)
5701 then
5702 null;
5704 -- In Ada 95 nothing to be done if the type of the expression is limited
5705 -- because in this case the expression cannot be copied, and its use can
5706 -- only be by reference.
5708 -- In Ada 2005 the context can be an object declaration whose expression
5709 -- is a function that returns in place. If the nominal subtype has
5710 -- unknown discriminants, the call still provides constraints on the
5711 -- object, and we have to create an actual subtype from it.
5713 -- If the type is class-wide, the expression is dynamically tagged and
5714 -- we do not create an actual subtype either. Ditto for an interface.
5715 -- For now this applies only if the type is immutably limited, and the
5716 -- function being called is build-in-place. This will have to be revised
5717 -- when build-in-place functions are generalized to other types.
5719 elsif Is_Limited_View (Exp_Typ)
5720 and then
5721 (Is_Class_Wide_Type (Exp_Typ)
5722 or else Is_Interface (Exp_Typ)
5723 or else not Has_Unknown_Discriminants (Exp_Typ)
5724 or else not Is_Composite_Type (Unc_Type))
5725 then
5726 null;
5728 -- For limited objects initialized with build-in-place function calls,
5729 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5730 -- node in the expression initializing the object, which breaks the
5731 -- circuitry that detects and adds the additional arguments to the
5732 -- called function.
5734 elsif Is_Build_In_Place_Function_Call (Exp) then
5735 null;
5737 -- If the expression is an uninitialized aggregate, no need to build
5738 -- a subtype from the expression, because this may require the use of
5739 -- dynamic memory to create the object.
5741 elsif Is_Uninitialized_Aggregate (Exp, Exp_Typ) then
5742 Rewrite (Subtype_Indic, New_Occurrence_Of (Etype (Exp), Sloc (N)));
5743 if Nkind (N) = N_Object_Declaration then
5744 Set_Expression (N, Empty);
5745 Set_No_Initialization (N);
5746 end if;
5748 else
5749 Remove_Side_Effects (Exp);
5750 Rewrite (Subtype_Indic,
5751 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5752 end if;
5753 end Expand_Subtype_From_Expr;
5755 ---------------------------------------------
5756 -- Expression_Contains_Primitives_Calls_Of --
5757 ---------------------------------------------
5759 function Expression_Contains_Primitives_Calls_Of
5760 (Expr : Node_Id;
5761 Typ : Entity_Id) return Boolean
5763 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5765 Calls_OK : Boolean := False;
5766 -- This flag is set to True when expression Expr contains at least one
5767 -- call to a nondispatching primitive function of Typ.
5769 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5770 -- Search for nondispatching calls to primitive functions of type Typ
5772 ----------------------------
5773 -- Search_Primitive_Calls --
5774 ----------------------------
5776 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5777 Disp_Typ : Entity_Id;
5778 Subp : Entity_Id;
5780 begin
5781 -- Detect a function call that could denote a nondispatching
5782 -- primitive of the input type.
5784 if Nkind (N) = N_Function_Call
5785 and then Is_Entity_Name (Name (N))
5786 then
5787 Subp := Entity (Name (N));
5789 -- Do not consider function calls with a controlling argument, as
5790 -- those are always dispatching calls.
5792 if Is_Dispatching_Operation (Subp)
5793 and then No (Controlling_Argument (N))
5794 then
5795 Disp_Typ := Find_Dispatching_Type (Subp);
5797 -- To qualify as a suitable primitive, the dispatching type of
5798 -- the function must be the input type.
5800 if Present (Disp_Typ)
5801 and then Unique_Entity (Disp_Typ) = U_Typ
5802 then
5803 Calls_OK := True;
5805 -- There is no need to continue the traversal, as one such
5806 -- call suffices.
5808 return Abandon;
5809 end if;
5810 end if;
5811 end if;
5813 return OK;
5814 end Search_Primitive_Calls;
5816 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5818 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5820 begin
5821 Search_Calls (Expr);
5822 return Calls_OK;
5823 end Expression_Contains_Primitives_Calls_Of;
5825 ----------------------
5826 -- Finalize_Address --
5827 ----------------------
5829 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5830 Btyp : constant Entity_Id := Base_Type (Typ);
5831 Utyp : Entity_Id := Typ;
5833 begin
5834 -- Handle protected class-wide or task class-wide types
5836 if Is_Class_Wide_Type (Utyp) then
5837 if Is_Concurrent_Type (Root_Type (Utyp)) then
5838 Utyp := Root_Type (Utyp);
5840 elsif Is_Private_Type (Root_Type (Utyp))
5841 and then Present (Full_View (Root_Type (Utyp)))
5842 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5843 then
5844 Utyp := Full_View (Root_Type (Utyp));
5845 end if;
5846 end if;
5848 -- Handle private types
5850 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5851 Utyp := Full_View (Utyp);
5852 end if;
5854 -- Handle protected and task types
5856 if Is_Concurrent_Type (Utyp)
5857 and then Present (Corresponding_Record_Type (Utyp))
5858 then
5859 Utyp := Corresponding_Record_Type (Utyp);
5860 end if;
5862 Utyp := Underlying_Type (Base_Type (Utyp));
5864 -- Deal with untagged derivation of private views. If the parent is
5865 -- now known to be protected, the finalization routine is the one
5866 -- defined on the corresponding record of the ancestor (corresponding
5867 -- records do not automatically inherit operations, but maybe they
5868 -- should???)
5870 if Is_Untagged_Derivation (Btyp) then
5871 if Is_Protected_Type (Btyp) then
5872 Utyp := Corresponding_Record_Type (Root_Type (Btyp));
5874 else
5875 Utyp := Underlying_Type (Root_Type (Btyp));
5877 if Is_Protected_Type (Utyp) then
5878 Utyp := Corresponding_Record_Type (Utyp);
5879 end if;
5880 end if;
5881 end if;
5883 -- If the underlying_type is a subtype, we are dealing with the
5884 -- completion of a private type. We need to access the base type and
5885 -- generate a conversion to it.
5887 if Utyp /= Base_Type (Utyp) then
5888 pragma Assert (Is_Private_Type (Typ));
5890 Utyp := Base_Type (Utyp);
5891 end if;
5893 -- When dealing with an internally built full view for a type with
5894 -- unknown discriminants, use the original record type.
5896 if Is_Underlying_Record_View (Utyp) then
5897 Utyp := Etype (Utyp);
5898 end if;
5900 return TSS (Utyp, TSS_Finalize_Address);
5901 end Finalize_Address;
5903 ------------------------
5904 -- Find_Interface_ADT --
5905 ------------------------
5907 function Find_Interface_ADT
5908 (T : Entity_Id;
5909 Iface : Entity_Id) return Elmt_Id
5911 ADT : Elmt_Id;
5912 Typ : Entity_Id := T;
5914 begin
5915 pragma Assert (Is_Interface (Iface));
5917 -- Handle private types
5919 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
5920 Typ := Full_View (Typ);
5921 end if;
5923 -- Handle access types
5925 if Is_Access_Type (Typ) then
5926 Typ := Designated_Type (Typ);
5927 end if;
5929 -- Handle task and protected types implementing interfaces
5931 if Is_Concurrent_Type (Typ) then
5932 Typ := Corresponding_Record_Type (Typ);
5933 end if;
5935 pragma Assert
5936 (not Is_Class_Wide_Type (Typ)
5937 and then Ekind (Typ) /= E_Incomplete_Type);
5939 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
5940 return First_Elmt (Access_Disp_Table (Typ));
5942 else
5943 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
5944 while Present (ADT)
5945 and then Present (Related_Type (Node (ADT)))
5946 and then Related_Type (Node (ADT)) /= Iface
5947 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
5948 Use_Full_View => True)
5949 loop
5950 Next_Elmt (ADT);
5951 end loop;
5953 pragma Assert (Present (Related_Type (Node (ADT))));
5954 return ADT;
5955 end if;
5956 end Find_Interface_ADT;
5958 ------------------------
5959 -- Find_Interface_Tag --
5960 ------------------------
5962 function Find_Interface_Tag
5963 (T : Entity_Id;
5964 Iface : Entity_Id) return Entity_Id
5966 AI_Tag : Entity_Id := Empty;
5967 Found : Boolean := False;
5968 Typ : Entity_Id := T;
5970 procedure Find_Tag (Typ : Entity_Id);
5971 -- Internal subprogram used to recursively climb to the ancestors
5973 --------------
5974 -- Find_Tag --
5975 --------------
5977 procedure Find_Tag (Typ : Entity_Id) is
5978 AI_Elmt : Elmt_Id;
5979 AI : Node_Id;
5981 begin
5982 -- This routine does not handle the case in which the interface is an
5983 -- ancestor of Typ. That case is handled by the enclosing subprogram.
5985 pragma Assert (Typ /= Iface);
5987 -- Climb to the root type handling private types
5989 if Present (Full_View (Etype (Typ))) then
5990 if Full_View (Etype (Typ)) /= Typ then
5991 Find_Tag (Full_View (Etype (Typ)));
5992 end if;
5994 elsif Etype (Typ) /= Typ then
5995 Find_Tag (Etype (Typ));
5996 end if;
5998 -- Traverse the list of interfaces implemented by the type
6000 if not Found
6001 and then Present (Interfaces (Typ))
6002 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
6003 then
6004 -- Skip the tag associated with the primary table
6006 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
6007 pragma Assert (Present (AI_Tag));
6009 AI_Elmt := First_Elmt (Interfaces (Typ));
6010 while Present (AI_Elmt) loop
6011 AI := Node (AI_Elmt);
6013 if AI = Iface
6014 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
6015 then
6016 Found := True;
6017 return;
6018 end if;
6020 AI_Tag := Next_Tag_Component (AI_Tag);
6021 Next_Elmt (AI_Elmt);
6022 end loop;
6023 end if;
6024 end Find_Tag;
6026 -- Start of processing for Find_Interface_Tag
6028 begin
6029 pragma Assert (Is_Interface (Iface));
6031 -- Handle access types
6033 if Is_Access_Type (Typ) then
6034 Typ := Designated_Type (Typ);
6035 end if;
6037 -- Handle class-wide types
6039 if Is_Class_Wide_Type (Typ) then
6040 Typ := Root_Type (Typ);
6041 end if;
6043 -- Handle private types
6045 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6046 Typ := Full_View (Typ);
6047 end if;
6049 -- Handle entities from the limited view
6051 if Ekind (Typ) = E_Incomplete_Type then
6052 pragma Assert (Present (Non_Limited_View (Typ)));
6053 Typ := Non_Limited_View (Typ);
6054 end if;
6056 -- Handle task and protected types implementing interfaces
6058 if Is_Concurrent_Type (Typ) then
6059 Typ := Corresponding_Record_Type (Typ);
6060 end if;
6062 -- If the interface is an ancestor of the type, then it shared the
6063 -- primary dispatch table.
6065 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6066 return First_Tag_Component (Typ);
6068 -- Otherwise we need to search for its associated tag component
6070 else
6071 Find_Tag (Typ);
6072 return AI_Tag;
6073 end if;
6074 end Find_Interface_Tag;
6076 ---------------------------
6077 -- Find_Optional_Prim_Op --
6078 ---------------------------
6080 function Find_Optional_Prim_Op
6081 (T : Entity_Id; Name : Name_Id) return Entity_Id
6083 Prim : Elmt_Id;
6084 Typ : Entity_Id := T;
6085 Op : Entity_Id;
6087 begin
6088 if Is_Class_Wide_Type (Typ) then
6089 Typ := Root_Type (Typ);
6090 end if;
6092 Typ := Underlying_Type (Typ);
6094 -- Loop through primitive operations
6096 Prim := First_Elmt (Primitive_Operations (Typ));
6097 while Present (Prim) loop
6098 Op := Node (Prim);
6100 -- We can retrieve primitive operations by name if it is an internal
6101 -- name. For equality we must check that both of its operands have
6102 -- the same type, to avoid confusion with user-defined equalities
6103 -- than may have a asymmetric signature.
6105 exit when Chars (Op) = Name
6106 and then
6107 (Name /= Name_Op_Eq
6108 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
6110 Next_Elmt (Prim);
6111 end loop;
6113 return Node (Prim); -- Empty if not found
6114 end Find_Optional_Prim_Op;
6116 ---------------------------
6117 -- Find_Optional_Prim_Op --
6118 ---------------------------
6120 function Find_Optional_Prim_Op
6121 (T : Entity_Id;
6122 Name : TSS_Name_Type) return Entity_Id
6124 Inher_Op : Entity_Id := Empty;
6125 Own_Op : Entity_Id := Empty;
6126 Prim_Elmt : Elmt_Id;
6127 Prim_Id : Entity_Id;
6128 Typ : Entity_Id := T;
6130 begin
6131 if Is_Class_Wide_Type (Typ) then
6132 Typ := Root_Type (Typ);
6133 end if;
6135 Typ := Underlying_Type (Typ);
6137 -- This search is based on the assertion that the dispatching version
6138 -- of the TSS routine always precedes the real primitive.
6140 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6141 while Present (Prim_Elmt) loop
6142 Prim_Id := Node (Prim_Elmt);
6144 if Is_TSS (Prim_Id, Name) then
6145 if Present (Alias (Prim_Id)) then
6146 Inher_Op := Prim_Id;
6147 else
6148 Own_Op := Prim_Id;
6149 end if;
6150 end if;
6152 Next_Elmt (Prim_Elmt);
6153 end loop;
6155 if Present (Own_Op) then
6156 return Own_Op;
6157 elsif Present (Inher_Op) then
6158 return Inher_Op;
6159 else
6160 return Empty;
6161 end if;
6162 end Find_Optional_Prim_Op;
6164 ------------------
6165 -- Find_Prim_Op --
6166 ------------------
6168 function Find_Prim_Op
6169 (T : Entity_Id; Name : Name_Id) return Entity_Id
6171 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6172 begin
6173 if No (Result) then
6174 raise Program_Error;
6175 end if;
6177 return Result;
6178 end Find_Prim_Op;
6180 ------------------
6181 -- Find_Prim_Op --
6182 ------------------
6184 function Find_Prim_Op
6185 (T : Entity_Id;
6186 Name : TSS_Name_Type) return Entity_Id
6188 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6189 begin
6190 if No (Result) then
6191 raise Program_Error;
6192 end if;
6194 return Result;
6195 end Find_Prim_Op;
6197 ----------------------------
6198 -- Find_Protection_Object --
6199 ----------------------------
6201 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
6202 S : Entity_Id;
6204 begin
6205 S := Scop;
6206 while Present (S) loop
6207 if Ekind (S) in E_Entry | E_Entry_Family | E_Function | E_Procedure
6208 and then Present (Protection_Object (S))
6209 then
6210 return Protection_Object (S);
6211 end if;
6213 S := Scope (S);
6214 end loop;
6216 -- If we do not find a Protection object in the scope chain, then
6217 -- something has gone wrong, most likely the object was never created.
6219 raise Program_Error;
6220 end Find_Protection_Object;
6222 --------------------------
6223 -- Find_Protection_Type --
6224 --------------------------
6226 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
6227 Comp : Entity_Id;
6228 Typ : Entity_Id := Conc_Typ;
6230 begin
6231 if Is_Concurrent_Type (Typ) then
6232 Typ := Corresponding_Record_Type (Typ);
6233 end if;
6235 -- Since restriction violations are not considered serious errors, the
6236 -- expander remains active, but may leave the corresponding record type
6237 -- malformed. In such cases, component _object is not available so do
6238 -- not look for it.
6240 if not Analyzed (Typ) then
6241 return Empty;
6242 end if;
6244 Comp := First_Component (Typ);
6245 while Present (Comp) loop
6246 if Chars (Comp) = Name_uObject then
6247 return Base_Type (Etype (Comp));
6248 end if;
6250 Next_Component (Comp);
6251 end loop;
6253 -- The corresponding record of a protected type should always have an
6254 -- _object field.
6256 raise Program_Error;
6257 end Find_Protection_Type;
6259 function Find_Storage_Op
6260 (Typ : Entity_Id;
6261 Nam : Name_Id) return Entity_Id
6263 use Sem_Util.Storage_Model_Support;
6265 begin
6266 if Has_Storage_Model_Type_Aspect (Typ) then
6267 declare
6268 SMT_Op : constant Entity_Id :=
6269 Get_Storage_Model_Type_Entity (Typ, Nam);
6270 begin
6271 if not Present (SMT_Op) then
6272 raise Program_Error;
6273 else
6274 return SMT_Op;
6275 end if;
6276 end;
6278 -- Otherwise we assume that Typ is a descendant of Root_Storage_Pool
6280 else
6281 return Find_Prim_Op (Typ, Nam);
6282 end if;
6283 end Find_Storage_Op;
6285 -----------------------
6286 -- Find_Hook_Context --
6287 -----------------------
6289 function Find_Hook_Context (N : Node_Id) return Node_Id is
6290 Par : Node_Id;
6291 Top : Node_Id;
6293 Wrapped_Node : Node_Id;
6294 -- Note: if we are in a transient scope, we want to reuse it as
6295 -- the context for actions insertion, if possible. But if N is itself
6296 -- part of the stored actions for the current transient scope,
6297 -- then we need to insert at the appropriate (inner) location in
6298 -- the not as an action on Node_To_Be_Wrapped.
6300 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
6302 begin
6303 -- When the node is inside a case/if expression, the lifetime of any
6304 -- temporary controlled object is extended. Find a suitable insertion
6305 -- node by locating the topmost case or if expressions.
6307 if In_Cond_Expr then
6308 Par := N;
6309 Top := N;
6310 while Present (Par) loop
6311 if Nkind (Original_Node (Par)) in
6312 N_Case_Expression | N_If_Expression
6313 then
6314 Top := Par;
6316 -- Prevent the search from going too far
6318 elsif Is_Body_Or_Package_Declaration (Par) then
6319 exit;
6320 end if;
6322 Par := Parent (Par);
6323 end loop;
6325 -- The topmost case or if expression is now recovered, but it may
6326 -- still not be the correct place to add generated code. Climb to
6327 -- find a parent that is part of a declarative or statement list,
6328 -- and is not a list of actuals in a call.
6330 Par := Top;
6331 while Present (Par) loop
6332 if Is_List_Member (Par)
6333 and then Nkind (Par) not in N_Component_Association
6334 | N_Discriminant_Association
6335 | N_Parameter_Association
6336 | N_Pragma_Argument_Association
6337 | N_Aggregate
6338 | N_Delta_Aggregate
6339 | N_Extension_Aggregate
6340 and then Nkind (Parent (Par)) not in N_Function_Call
6341 | N_Procedure_Call_Statement
6342 | N_Entry_Call_Statement
6344 then
6345 return Par;
6347 -- Prevent the search from going too far
6349 elsif Is_Body_Or_Package_Declaration (Par) then
6350 exit;
6351 end if;
6353 Par := Parent (Par);
6354 end loop;
6356 return Par;
6358 else
6359 Par := N;
6360 while Present (Par) loop
6362 -- Keep climbing past various operators
6364 if Nkind (Parent (Par)) in N_Op
6365 or else Nkind (Parent (Par)) in N_And_Then | N_Or_Else
6366 then
6367 Par := Parent (Par);
6368 else
6369 exit;
6370 end if;
6371 end loop;
6373 Top := Par;
6375 -- The node may be located in a pragma in which case return the
6376 -- pragma itself:
6378 -- pragma Precondition (... and then Ctrl_Func_Call ...);
6380 -- Similar case occurs when the node is related to an object
6381 -- declaration or assignment:
6383 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
6385 -- Another case to consider is when the node is part of a return
6386 -- statement:
6388 -- return ... and then Ctrl_Func_Call ...;
6390 -- Another case is when the node acts as a formal in a procedure
6391 -- call statement:
6393 -- Proc (... and then Ctrl_Func_Call ...);
6395 if Scope_Is_Transient then
6396 Wrapped_Node := Node_To_Be_Wrapped;
6397 else
6398 Wrapped_Node := Empty;
6399 end if;
6401 while Present (Par) loop
6402 if Par = Wrapped_Node
6403 or else Nkind (Par) in N_Assignment_Statement
6404 | N_Object_Declaration
6405 | N_Pragma
6406 | N_Procedure_Call_Statement
6407 | N_Simple_Return_Statement
6408 then
6409 return Par;
6411 -- Prevent the search from going too far
6413 elsif Is_Body_Or_Package_Declaration (Par) then
6414 exit;
6415 end if;
6417 Par := Parent (Par);
6418 end loop;
6420 -- Return the topmost short circuit operator
6422 return Top;
6423 end if;
6424 end Find_Hook_Context;
6426 ------------------------------
6427 -- Following_Address_Clause --
6428 ------------------------------
6430 function Following_Address_Clause (D : Node_Id) return Node_Id is
6431 Id : constant Entity_Id := Defining_Identifier (D);
6432 Result : Node_Id;
6433 Par : Node_Id;
6435 function Check_Decls (D : Node_Id) return Node_Id;
6436 -- This internal function differs from the main function in that it
6437 -- gets called to deal with a following package private part, and
6438 -- it checks declarations starting with D (the main function checks
6439 -- declarations following D). If D is Empty, then Empty is returned.
6441 -----------------
6442 -- Check_Decls --
6443 -----------------
6445 function Check_Decls (D : Node_Id) return Node_Id is
6446 Decl : Node_Id;
6448 begin
6449 Decl := D;
6450 while Present (Decl) loop
6451 if Nkind (Decl) = N_At_Clause
6452 and then Chars (Identifier (Decl)) = Chars (Id)
6453 then
6454 return Decl;
6456 elsif Nkind (Decl) = N_Attribute_Definition_Clause
6457 and then Chars (Decl) = Name_Address
6458 and then Chars (Name (Decl)) = Chars (Id)
6459 then
6460 return Decl;
6461 end if;
6463 Next (Decl);
6464 end loop;
6466 -- Otherwise not found, return Empty
6468 return Empty;
6469 end Check_Decls;
6471 -- Start of processing for Following_Address_Clause
6473 begin
6474 -- If parser detected no address clause for the identifier in question,
6475 -- then the answer is a quick NO, without the need for a search.
6477 if not Get_Name_Table_Boolean1 (Chars (Id)) then
6478 return Empty;
6479 end if;
6481 -- Otherwise search current declarative unit
6483 Result := Check_Decls (Next (D));
6485 if Present (Result) then
6486 return Result;
6487 end if;
6489 -- Check for possible package private part following
6491 Par := Parent (D);
6493 if Nkind (Par) = N_Package_Specification
6494 and then Visible_Declarations (Par) = List_Containing (D)
6495 and then Present (Private_Declarations (Par))
6496 then
6497 -- Private part present, check declarations there
6499 return Check_Decls (First (Private_Declarations (Par)));
6501 else
6502 -- No private part, clause not found, return Empty
6504 return Empty;
6505 end if;
6506 end Following_Address_Clause;
6508 ----------------------
6509 -- Force_Evaluation --
6510 ----------------------
6512 procedure Force_Evaluation
6513 (Exp : Node_Id;
6514 Name_Req : Boolean := False;
6515 Related_Id : Entity_Id := Empty;
6516 Is_Low_Bound : Boolean := False;
6517 Is_High_Bound : Boolean := False;
6518 Discr_Number : Int := 0;
6519 Mode : Force_Evaluation_Mode := Relaxed)
6521 begin
6522 Remove_Side_Effects
6523 (Exp => Exp,
6524 Name_Req => Name_Req,
6525 Variable_Ref => True,
6526 Renaming_Req => False,
6527 Related_Id => Related_Id,
6528 Is_Low_Bound => Is_Low_Bound,
6529 Is_High_Bound => Is_High_Bound,
6530 Discr_Number => Discr_Number,
6531 Check_Side_Effects =>
6532 Is_Static_Expression (Exp)
6533 or else Mode = Relaxed);
6534 end Force_Evaluation;
6536 ---------------------------------
6537 -- Fully_Qualified_Name_String --
6538 ---------------------------------
6540 function Fully_Qualified_Name_String
6541 (E : Entity_Id;
6542 Append_NUL : Boolean := True) return String_Id
6544 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6545 -- Compute recursively the qualified name without NUL at the end, adding
6546 -- it to the currently started string being generated
6548 ----------------------------------
6549 -- Internal_Full_Qualified_Name --
6550 ----------------------------------
6552 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6553 Ent : Entity_Id;
6555 begin
6556 -- Deal properly with child units
6558 if Nkind (E) = N_Defining_Program_Unit_Name then
6559 Ent := Defining_Identifier (E);
6560 else
6561 Ent := E;
6562 end if;
6564 -- Compute qualification recursively (only "Standard" has no scope)
6566 if Present (Scope (Scope (Ent))) then
6567 Internal_Full_Qualified_Name (Scope (Ent));
6568 Store_String_Char (Get_Char_Code ('.'));
6569 end if;
6571 -- Every entity should have a name except some expanded blocks
6572 -- don't bother about those.
6574 if Chars (Ent) = No_Name then
6575 return;
6576 end if;
6578 -- Generates the entity name in upper case
6580 Get_Decoded_Name_String (Chars (Ent));
6581 Set_All_Upper_Case;
6582 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6583 return;
6584 end Internal_Full_Qualified_Name;
6586 -- Start of processing for Full_Qualified_Name
6588 begin
6589 Start_String;
6590 Internal_Full_Qualified_Name (E);
6592 if Append_NUL then
6593 Store_String_Char (Get_Char_Code (ASCII.NUL));
6594 end if;
6596 return End_String;
6597 end Fully_Qualified_Name_String;
6599 ---------------------------------
6600 -- Get_Current_Value_Condition --
6601 ---------------------------------
6603 -- Note: the implementation of this procedure is very closely tied to the
6604 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6605 -- interpret Current_Value fields set by the Set procedure, so the two
6606 -- procedures need to be closely coordinated.
6608 procedure Get_Current_Value_Condition
6609 (Var : Node_Id;
6610 Op : out Node_Kind;
6611 Val : out Node_Id)
6613 Loc : constant Source_Ptr := Sloc (Var);
6614 Ent : constant Entity_Id := Entity (Var);
6616 procedure Process_Current_Value_Condition (N : Node_Id; S : Boolean);
6617 -- N is an expression which holds either True (S = True) or False (S =
6618 -- False) in the condition. This procedure digs out the expression and
6619 -- if it refers to Ent, sets Op and Val appropriately.
6621 -------------------------------------
6622 -- Process_Current_Value_Condition --
6623 -------------------------------------
6625 procedure Process_Current_Value_Condition
6626 (N : Node_Id;
6627 S : Boolean)
6629 Cond : Node_Id;
6630 Prev_Cond : Node_Id;
6631 Sens : Boolean;
6633 begin
6634 Cond := N;
6635 Sens := S;
6637 loop
6638 Prev_Cond := Cond;
6640 -- Deal with NOT operators, inverting sense
6642 while Nkind (Cond) = N_Op_Not loop
6643 Cond := Right_Opnd (Cond);
6644 Sens := not Sens;
6645 end loop;
6647 -- Deal with conversions, qualifications, and expressions with
6648 -- actions.
6650 while Nkind (Cond) in N_Type_Conversion
6651 | N_Qualified_Expression
6652 | N_Expression_With_Actions
6653 loop
6654 Cond := Expression (Cond);
6655 end loop;
6657 exit when Cond = Prev_Cond;
6658 end loop;
6660 -- Deal with AND THEN and AND cases
6662 if Nkind (Cond) in N_And_Then | N_Op_And then
6664 -- Don't ever try to invert a condition that is of the form of an
6665 -- AND or AND THEN (since we are not doing sufficiently general
6666 -- processing to allow this).
6668 if Sens = False then
6669 Op := N_Empty;
6670 Val := Empty;
6671 return;
6672 end if;
6674 -- Recursively process AND and AND THEN branches
6676 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6677 pragma Assert (Op'Valid);
6679 if Op /= N_Empty then
6680 return;
6681 end if;
6683 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6684 return;
6686 -- Case of relational operator
6688 elsif Nkind (Cond) in N_Op_Compare then
6689 Op := Nkind (Cond);
6691 -- Invert sense of test if inverted test
6693 if Sens = False then
6694 case Op is
6695 when N_Op_Eq => Op := N_Op_Ne;
6696 when N_Op_Ne => Op := N_Op_Eq;
6697 when N_Op_Lt => Op := N_Op_Ge;
6698 when N_Op_Gt => Op := N_Op_Le;
6699 when N_Op_Le => Op := N_Op_Gt;
6700 when N_Op_Ge => Op := N_Op_Lt;
6701 when others => raise Program_Error;
6702 end case;
6703 end if;
6705 -- Case of entity op value
6707 if Is_Entity_Name (Left_Opnd (Cond))
6708 and then Ent = Entity (Left_Opnd (Cond))
6709 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6710 then
6711 Val := Right_Opnd (Cond);
6713 -- Case of value op entity
6715 elsif Is_Entity_Name (Right_Opnd (Cond))
6716 and then Ent = Entity (Right_Opnd (Cond))
6717 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6718 then
6719 Val := Left_Opnd (Cond);
6721 -- We are effectively swapping operands
6723 case Op is
6724 when N_Op_Eq => null;
6725 when N_Op_Ne => null;
6726 when N_Op_Lt => Op := N_Op_Gt;
6727 when N_Op_Gt => Op := N_Op_Lt;
6728 when N_Op_Le => Op := N_Op_Ge;
6729 when N_Op_Ge => Op := N_Op_Le;
6730 when others => raise Program_Error;
6731 end case;
6733 else
6734 Op := N_Empty;
6735 end if;
6737 return;
6739 elsif Nkind (Cond) in N_Type_Conversion
6740 | N_Qualified_Expression
6741 | N_Expression_With_Actions
6742 then
6743 Cond := Expression (Cond);
6745 -- Case of Boolean variable reference, return as though the
6746 -- reference had said var = True.
6748 else
6749 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6750 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6752 if Sens = False then
6753 Op := N_Op_Ne;
6754 else
6755 Op := N_Op_Eq;
6756 end if;
6757 end if;
6758 end if;
6759 end Process_Current_Value_Condition;
6761 -- Start of processing for Get_Current_Value_Condition
6763 begin
6764 Op := N_Empty;
6765 Val := Empty;
6767 -- Immediate return, nothing doing, if this is not an object
6769 if not Is_Object (Ent) then
6770 return;
6771 end if;
6773 -- In GNATprove mode we don't want to use current value optimizer, in
6774 -- particular for loop invariant expressions and other assertions that
6775 -- act as cut points for proof. The optimizer often folds expressions
6776 -- into True/False where they trivially follow from the previous
6777 -- assignments, but this deprives proof from the information needed to
6778 -- discharge checks that are beyond the scope of the value optimizer.
6780 if GNATprove_Mode then
6781 return;
6782 end if;
6784 -- Otherwise examine current value
6786 declare
6787 CV : constant Node_Id := Current_Value (Ent);
6788 Sens : Boolean;
6789 Stm : Node_Id;
6791 begin
6792 -- If statement. Condition is known true in THEN section, known False
6793 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6795 if Nkind (CV) = N_If_Statement then
6797 -- Before start of IF statement
6799 if Loc < Sloc (CV) then
6800 return;
6802 -- After end of IF statement
6804 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6805 return;
6806 end if;
6808 -- At this stage we know that we are within the IF statement, but
6809 -- unfortunately, the tree does not record the SLOC of the ELSE so
6810 -- we cannot use a simple SLOC comparison to distinguish between
6811 -- the then/else statements, so we have to climb the tree.
6813 declare
6814 N : Node_Id;
6816 begin
6817 N := Parent (Var);
6818 while Parent (N) /= CV loop
6819 N := Parent (N);
6821 -- If we fall off the top of the tree, then that's odd, but
6822 -- perhaps it could occur in some error situation, and the
6823 -- safest response is simply to assume that the outcome of
6824 -- the condition is unknown. No point in bombing during an
6825 -- attempt to optimize things.
6827 if No (N) then
6828 return;
6829 end if;
6830 end loop;
6832 -- Now we have N pointing to a node whose parent is the IF
6833 -- statement in question, so now we can tell if we are within
6834 -- the THEN statements.
6836 if Is_List_Member (N)
6837 and then List_Containing (N) = Then_Statements (CV)
6838 then
6839 Sens := True;
6841 -- If the variable reference does not come from source, we
6842 -- cannot reliably tell whether it appears in the else part.
6843 -- In particular, if it appears in generated code for a node
6844 -- that requires finalization, it may be attached to a list
6845 -- that has not been yet inserted into the code. For now,
6846 -- treat it as unknown.
6848 elsif not Comes_From_Source (N) then
6849 return;
6851 -- Otherwise we must be in ELSIF or ELSE part
6853 else
6854 Sens := False;
6855 end if;
6856 end;
6858 -- ELSIF part. Condition is known true within the referenced
6859 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6860 -- and unknown before the ELSE part or after the IF statement.
6862 elsif Nkind (CV) = N_Elsif_Part then
6864 -- if the Elsif_Part had condition_actions, the elsif has been
6865 -- rewritten as a nested if, and the original elsif_part is
6866 -- detached from the tree, so there is no way to obtain useful
6867 -- information on the current value of the variable.
6868 -- Can this be improved ???
6870 if No (Parent (CV)) then
6871 return;
6872 end if;
6874 Stm := Parent (CV);
6876 -- If the tree has been otherwise rewritten there is nothing
6877 -- else to be done either.
6879 if Nkind (Stm) /= N_If_Statement then
6880 return;
6881 end if;
6883 -- Before start of ELSIF part
6885 if Loc < Sloc (CV) then
6886 return;
6888 -- After end of IF statement
6890 elsif Loc >= Sloc (Stm) +
6891 Text_Ptr (UI_To_Int (End_Span (Stm)))
6892 then
6893 return;
6894 end if;
6896 -- Again we lack the SLOC of the ELSE, so we need to climb the
6897 -- tree to see if we are within the ELSIF part in question.
6899 declare
6900 N : Node_Id;
6902 begin
6903 N := Parent (Var);
6904 while Parent (N) /= Stm loop
6905 N := Parent (N);
6907 -- If we fall off the top of the tree, then that's odd, but
6908 -- perhaps it could occur in some error situation, and the
6909 -- safest response is simply to assume that the outcome of
6910 -- the condition is unknown. No point in bombing during an
6911 -- attempt to optimize things.
6913 if No (N) then
6914 return;
6915 end if;
6916 end loop;
6918 -- Now we have N pointing to a node whose parent is the IF
6919 -- statement in question, so see if is the ELSIF part we want.
6920 -- the THEN statements.
6922 if N = CV then
6923 Sens := True;
6925 -- Otherwise we must be in subsequent ELSIF or ELSE part
6927 else
6928 Sens := False;
6929 end if;
6930 end;
6932 -- Iteration scheme of while loop. The condition is known to be
6933 -- true within the body of the loop.
6935 elsif Nkind (CV) = N_Iteration_Scheme then
6936 declare
6937 Loop_Stmt : constant Node_Id := Parent (CV);
6939 begin
6940 -- Before start of body of loop
6942 if Loc < Sloc (Loop_Stmt) then
6943 return;
6945 -- After end of LOOP statement
6947 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
6948 return;
6950 -- We are within the body of the loop
6952 else
6953 Sens := True;
6954 end if;
6955 end;
6957 -- All other cases of Current_Value settings
6959 else
6960 return;
6961 end if;
6963 -- If we fall through here, then we have a reportable condition, Sens
6964 -- is True if the condition is true and False if it needs inverting.
6966 Process_Current_Value_Condition (Condition (CV), Sens);
6967 end;
6968 end Get_Current_Value_Condition;
6970 -----------------------
6971 -- Get_Index_Subtype --
6972 -----------------------
6974 function Get_Index_Subtype (N : Node_Id) return Entity_Id is
6975 P_Type : Entity_Id := Etype (Prefix (N));
6976 Indx : Node_Id;
6977 J : Int;
6979 begin
6980 if Is_Access_Type (P_Type) then
6981 P_Type := Designated_Type (P_Type);
6982 end if;
6984 if No (Expressions (N)) then
6985 J := 1;
6986 else
6987 J := UI_To_Int (Expr_Value (First (Expressions (N))));
6988 end if;
6990 Indx := First_Index (P_Type);
6991 while J > 1 loop
6992 Next_Index (Indx);
6993 J := J - 1;
6994 end loop;
6996 return Etype (Indx);
6997 end Get_Index_Subtype;
6999 -----------------------
7000 -- Get_Mapped_Entity --
7001 -----------------------
7003 function Get_Mapped_Entity (E : Entity_Id) return Entity_Id is
7004 begin
7005 return Type_Map.Get (E);
7006 end Get_Mapped_Entity;
7008 ---------------------
7009 -- Get_Stream_Size --
7010 ---------------------
7012 function Get_Stream_Size (E : Entity_Id) return Uint is
7013 begin
7014 -- If we have a Stream_Size clause for this type use it
7016 if Has_Stream_Size_Clause (E) then
7017 return Static_Integer (Expression (Stream_Size_Clause (E)));
7019 -- Otherwise the Stream_Size is the size of the type
7021 else
7022 return Esize (E);
7023 end if;
7024 end Get_Stream_Size;
7026 ---------------------------
7027 -- Has_Access_Constraint --
7028 ---------------------------
7030 function Has_Access_Constraint (E : Entity_Id) return Boolean is
7031 Disc : Entity_Id;
7032 T : constant Entity_Id := Etype (E);
7034 begin
7035 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
7036 Disc := First_Discriminant (T);
7037 while Present (Disc) loop
7038 if Is_Access_Type (Etype (Disc)) then
7039 return True;
7040 end if;
7042 Next_Discriminant (Disc);
7043 end loop;
7045 return False;
7046 else
7047 return False;
7048 end if;
7049 end Has_Access_Constraint;
7051 --------------------
7052 -- Homonym_Number --
7053 --------------------
7055 function Homonym_Number (Subp : Entity_Id) return Pos is
7056 Hom : Entity_Id := Homonym (Subp);
7057 Count : Pos := 1;
7059 begin
7060 while Present (Hom) loop
7061 if Scope (Hom) = Scope (Subp) then
7062 Count := Count + 1;
7063 end if;
7065 Hom := Homonym (Hom);
7066 end loop;
7068 return Count;
7069 end Homonym_Number;
7071 -----------------------------------
7072 -- In_Library_Level_Package_Body --
7073 -----------------------------------
7075 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
7076 begin
7077 -- First determine whether the entity appears at the library level, then
7078 -- look at the containing unit.
7080 if Is_Library_Level_Entity (Id) then
7081 declare
7082 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
7084 begin
7085 return Nkind (Unit (Container)) = N_Package_Body;
7086 end;
7087 end if;
7089 return False;
7090 end In_Library_Level_Package_Body;
7092 ------------------------------
7093 -- In_Unconditional_Context --
7094 ------------------------------
7096 function In_Unconditional_Context (Node : Node_Id) return Boolean is
7097 P : Node_Id;
7099 begin
7100 P := Node;
7101 while Present (P) loop
7102 case Nkind (P) is
7103 when N_Subprogram_Body => return True;
7104 when N_If_Statement => return False;
7105 when N_Loop_Statement => return False;
7106 when N_Case_Statement => return False;
7107 when others => P := Parent (P);
7108 end case;
7109 end loop;
7111 return False;
7112 end In_Unconditional_Context;
7114 -------------------
7115 -- Insert_Action --
7116 -------------------
7118 procedure Insert_Action
7119 (Assoc_Node : Node_Id;
7120 Ins_Action : Node_Id;
7121 Spec_Expr_OK : Boolean := False)
7123 begin
7124 if Present (Ins_Action) then
7125 Insert_Actions
7126 (Assoc_Node => Assoc_Node,
7127 Ins_Actions => New_List (Ins_Action),
7128 Spec_Expr_OK => Spec_Expr_OK);
7129 end if;
7130 end Insert_Action;
7132 -- Version with check(s) suppressed
7134 procedure Insert_Action
7135 (Assoc_Node : Node_Id;
7136 Ins_Action : Node_Id;
7137 Suppress : Check_Id;
7138 Spec_Expr_OK : Boolean := False)
7140 begin
7141 Insert_Actions
7142 (Assoc_Node => Assoc_Node,
7143 Ins_Actions => New_List (Ins_Action),
7144 Suppress => Suppress,
7145 Spec_Expr_OK => Spec_Expr_OK);
7146 end Insert_Action;
7148 -------------------------
7149 -- Insert_Action_After --
7150 -------------------------
7152 procedure Insert_Action_After
7153 (Assoc_Node : Node_Id;
7154 Ins_Action : Node_Id)
7156 begin
7157 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
7158 end Insert_Action_After;
7160 --------------------
7161 -- Insert_Actions --
7162 --------------------
7164 procedure Insert_Actions
7165 (Assoc_Node : Node_Id;
7166 Ins_Actions : List_Id;
7167 Spec_Expr_OK : Boolean := False)
7169 N : Node_Id;
7170 P : Node_Id;
7172 Wrapped_Node : Node_Id := Empty;
7174 begin
7175 if No (Ins_Actions) or else Is_Empty_List (Ins_Actions) then
7176 return;
7177 end if;
7179 -- Insert the action when the context is "Handling of Default and Per-
7180 -- Object Expressions" only when requested by the caller.
7182 if Spec_Expr_OK then
7183 null;
7185 -- Ignore insert of actions from inside default expression (or other
7186 -- similar "spec expression") in the special spec-expression analyze
7187 -- mode. Any insertions at this point have no relevance, since we are
7188 -- only doing the analyze to freeze the types of any static expressions.
7189 -- See section "Handling of Default and Per-Object Expressions" in the
7190 -- spec of package Sem for further details.
7192 elsif In_Spec_Expression then
7193 return;
7194 end if;
7196 -- If the action derives from stuff inside a record, then the actions
7197 -- are attached to the current scope, to be inserted and analyzed on
7198 -- exit from the scope. The reason for this is that we may also be
7199 -- generating freeze actions at the same time, and they must eventually
7200 -- be elaborated in the correct order.
7202 if Is_Record_Type (Current_Scope)
7203 and then not Is_Frozen (Current_Scope)
7204 then
7205 if No (Scope_Stack.Table
7206 (Scope_Stack.Last).Pending_Freeze_Actions)
7207 then
7208 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
7209 Ins_Actions;
7210 else
7211 Append_List
7212 (Ins_Actions,
7213 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
7214 end if;
7216 return;
7217 end if;
7219 -- We now intend to climb up the tree to find the right point to
7220 -- insert the actions. We start at Assoc_Node, unless this node is a
7221 -- subexpression in which case we start with its parent. We do this for
7222 -- two reasons. First it speeds things up. Second, if Assoc_Node is
7223 -- itself one of the special nodes like N_And_Then, then we assume that
7224 -- an initial request to insert actions for such a node does not expect
7225 -- the actions to get deposited in the node for later handling when the
7226 -- node is expanded, since clearly the node is being dealt with by the
7227 -- caller. Note that in the subexpression case, N is always the child we
7228 -- came from.
7230 -- N_Raise_xxx_Error is an annoying special case, it is a statement
7231 -- if it has type Standard_Void_Type, and a subexpression otherwise.
7232 -- Procedure calls, and similarly procedure attribute references, are
7233 -- also statements.
7235 if Nkind (Assoc_Node) in N_Subexpr
7236 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
7237 or else Etype (Assoc_Node) /= Standard_Void_Type)
7238 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
7239 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
7240 or else not Is_Procedure_Attribute_Name
7241 (Attribute_Name (Assoc_Node)))
7242 then
7243 N := Assoc_Node;
7244 P := Parent (Assoc_Node);
7246 -- Nonsubexpression case. Note that N is initially Empty in this case
7247 -- (N is only guaranteed non-Empty in the subexpr case).
7249 else
7250 N := Empty;
7251 P := Assoc_Node;
7252 end if;
7254 -- Capture root of the transient scope
7256 if Scope_Is_Transient then
7257 Wrapped_Node := Node_To_Be_Wrapped;
7258 end if;
7260 loop
7261 pragma Assert (Present (P));
7263 -- Make sure that inserted actions stay in the transient scope
7265 if Present (Wrapped_Node) and then N = Wrapped_Node then
7266 Store_Before_Actions_In_Scope (Ins_Actions);
7267 return;
7268 end if;
7270 case Nkind (P) is
7272 -- Case of right operand of AND THEN or OR ELSE. Put the actions
7273 -- in the Actions field of the right operand. They will be moved
7274 -- out further when the AND THEN or OR ELSE operator is expanded.
7275 -- Nothing special needs to be done for the left operand since
7276 -- in that case the actions are executed unconditionally.
7278 when N_Short_Circuit =>
7279 if N = Right_Opnd (P) then
7281 -- We are now going to either append the actions to the
7282 -- actions field of the short-circuit operation. We will
7283 -- also analyze the actions now.
7285 -- This analysis is really too early, the proper thing would
7286 -- be to just park them there now, and only analyze them if
7287 -- we find we really need them, and to it at the proper
7288 -- final insertion point. However attempting to this proved
7289 -- tricky, so for now we just kill current values before and
7290 -- after the analyze call to make sure we avoid peculiar
7291 -- optimizations from this out of order insertion.
7293 Kill_Current_Values;
7295 -- If P has already been expanded, we can't park new actions
7296 -- on it, so we need to expand them immediately, introducing
7297 -- an Expression_With_Actions. N can't be an expression
7298 -- with actions, or else then the actions would have been
7299 -- inserted at an inner level.
7301 if Analyzed (P) then
7302 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
7303 Rewrite (N,
7304 Make_Expression_With_Actions (Sloc (N),
7305 Actions => Ins_Actions,
7306 Expression => Relocate_Node (N)));
7307 Analyze_And_Resolve (N);
7309 elsif Present (Actions (P)) then
7310 Insert_List_After_And_Analyze
7311 (Last (Actions (P)), Ins_Actions);
7312 else
7313 Set_Actions (P, Ins_Actions);
7314 Analyze_List (Actions (P));
7315 end if;
7317 Kill_Current_Values;
7319 return;
7320 end if;
7322 -- Then or Else dependent expression of an if expression. Add
7323 -- actions to Then_Actions or Else_Actions field as appropriate.
7324 -- The actions will be moved further out when the if is expanded.
7326 when N_If_Expression =>
7327 declare
7328 ThenX : constant Node_Id := Next (First (Expressions (P)));
7329 ElseX : constant Node_Id := Next (ThenX);
7331 begin
7332 -- If the enclosing expression is already analyzed, as
7333 -- is the case for nested elaboration checks, insert the
7334 -- conditional further out.
7336 if Analyzed (P) then
7337 null;
7339 -- Actions belong to the then expression, temporarily place
7340 -- them as Then_Actions of the if expression. They will be
7341 -- moved to the proper place later when the if expression is
7342 -- expanded.
7344 elsif N = ThenX then
7345 if Present (Then_Actions (P)) then
7346 Insert_List_After_And_Analyze
7347 (Last (Then_Actions (P)), Ins_Actions);
7348 else
7349 Set_Then_Actions (P, Ins_Actions);
7350 Analyze_List (Then_Actions (P));
7351 end if;
7353 return;
7355 -- Else_Actions is treated the same as Then_Actions above
7357 elsif N = ElseX then
7358 if Present (Else_Actions (P)) then
7359 Insert_List_After_And_Analyze
7360 (Last (Else_Actions (P)), Ins_Actions);
7361 else
7362 Set_Else_Actions (P, Ins_Actions);
7363 Analyze_List (Else_Actions (P));
7364 end if;
7366 return;
7368 -- Actions belong to the condition. In this case they are
7369 -- unconditionally executed, and so we can continue the
7370 -- search for the proper insert point.
7372 else
7373 null;
7374 end if;
7375 end;
7377 -- Alternative of case expression, we place the action in the
7378 -- Actions field of the case expression alternative, this will
7379 -- be handled when the case expression is expanded.
7381 when N_Case_Expression_Alternative =>
7382 if Present (Actions (P)) then
7383 Insert_List_After_And_Analyze
7384 (Last (Actions (P)), Ins_Actions);
7385 else
7386 Set_Actions (P, Ins_Actions);
7387 Analyze_List (Actions (P));
7388 end if;
7390 return;
7392 -- Case of appearing within an Expressions_With_Actions node. When
7393 -- the new actions come from the expression of the expression with
7394 -- actions, they must be added to the existing actions. The other
7395 -- alternative is when the new actions are related to one of the
7396 -- existing actions of the expression with actions, and should
7397 -- never reach here: if actions are inserted on a statement
7398 -- within the Actions of an expression with actions, or on some
7399 -- subexpression of such a statement, then the outermost proper
7400 -- insertion point is right before the statement, and we should
7401 -- never climb up as far as the N_Expression_With_Actions itself.
7403 when N_Expression_With_Actions =>
7404 if N = Expression (P) then
7405 if Is_Empty_List (Actions (P)) then
7406 Append_List_To (Actions (P), Ins_Actions);
7407 Analyze_List (Actions (P));
7408 else
7409 Insert_List_After_And_Analyze
7410 (Last (Actions (P)), Ins_Actions);
7411 end if;
7413 return;
7415 else
7416 raise Program_Error;
7417 end if;
7419 -- Case of appearing in the condition of a while expression or
7420 -- elsif. We insert the actions into the Condition_Actions field.
7421 -- They will be moved further out when the while loop or elsif
7422 -- is analyzed.
7424 when N_Elsif_Part
7425 | N_Iteration_Scheme
7427 if N = Condition (P) then
7428 if Present (Condition_Actions (P)) then
7429 Insert_List_After_And_Analyze
7430 (Last (Condition_Actions (P)), Ins_Actions);
7431 else
7432 Set_Condition_Actions (P, Ins_Actions);
7434 -- Set the parent of the insert actions explicitly. This
7435 -- is not a syntactic field, but we need the parent field
7436 -- set, in particular so that freeze can understand that
7437 -- it is dealing with condition actions, and properly
7438 -- insert the freezing actions.
7440 Set_Parent (Ins_Actions, P);
7441 Analyze_List (Condition_Actions (P));
7442 end if;
7444 return;
7445 end if;
7447 -- Statements, declarations, pragmas, representation clauses
7449 when
7450 -- Statements
7452 N_Procedure_Call_Statement
7453 | N_Statement_Other_Than_Procedure_Call
7455 -- Pragmas
7457 | N_Pragma
7459 -- Representation_Clause
7461 | N_At_Clause
7462 | N_Attribute_Definition_Clause
7463 | N_Enumeration_Representation_Clause
7464 | N_Record_Representation_Clause
7466 -- Declarations
7468 | N_Abstract_Subprogram_Declaration
7469 | N_Entry_Body
7470 | N_Exception_Declaration
7471 | N_Exception_Renaming_Declaration
7472 | N_Expression_Function
7473 | N_Formal_Abstract_Subprogram_Declaration
7474 | N_Formal_Concrete_Subprogram_Declaration
7475 | N_Formal_Object_Declaration
7476 | N_Formal_Type_Declaration
7477 | N_Full_Type_Declaration
7478 | N_Function_Instantiation
7479 | N_Generic_Function_Renaming_Declaration
7480 | N_Generic_Package_Declaration
7481 | N_Generic_Package_Renaming_Declaration
7482 | N_Generic_Procedure_Renaming_Declaration
7483 | N_Generic_Subprogram_Declaration
7484 | N_Implicit_Label_Declaration
7485 | N_Incomplete_Type_Declaration
7486 | N_Number_Declaration
7487 | N_Object_Declaration
7488 | N_Object_Renaming_Declaration
7489 | N_Package_Body
7490 | N_Package_Body_Stub
7491 | N_Package_Declaration
7492 | N_Package_Instantiation
7493 | N_Package_Renaming_Declaration
7494 | N_Private_Extension_Declaration
7495 | N_Private_Type_Declaration
7496 | N_Procedure_Instantiation
7497 | N_Protected_Body
7498 | N_Protected_Body_Stub
7499 | N_Single_Task_Declaration
7500 | N_Subprogram_Body
7501 | N_Subprogram_Body_Stub
7502 | N_Subprogram_Declaration
7503 | N_Subprogram_Renaming_Declaration
7504 | N_Subtype_Declaration
7505 | N_Task_Body
7506 | N_Task_Body_Stub
7508 -- Use clauses can appear in lists of declarations
7510 | N_Use_Package_Clause
7511 | N_Use_Type_Clause
7513 -- Freeze entity behaves like a declaration or statement
7515 | N_Freeze_Entity
7516 | N_Freeze_Generic_Entity
7518 -- Do not insert here if the item is not a list member (this
7519 -- happens for example with a triggering statement, and the
7520 -- proper approach is to insert before the entire select).
7522 if not Is_List_Member (P) then
7523 null;
7525 -- Do not insert if parent of P is an N_Component_Association
7526 -- node (i.e. we are in the context of an N_Aggregate or
7527 -- N_Extension_Aggregate node. In this case we want to insert
7528 -- before the entire aggregate.
7530 elsif Nkind (Parent (P)) = N_Component_Association then
7531 null;
7533 -- Do not insert if the parent of P is either an N_Variant node
7534 -- or an N_Record_Definition node, meaning in either case that
7535 -- P is a member of a component list, and that therefore the
7536 -- actions should be inserted outside the complete record
7537 -- declaration.
7539 elsif Nkind (Parent (P)) in N_Variant | N_Record_Definition then
7540 null;
7542 -- Do not insert freeze nodes within the loop generated for
7543 -- an aggregate, because they may be elaborated too late for
7544 -- subsequent use in the back end: within a package spec the
7545 -- loop is part of the elaboration procedure and is only
7546 -- elaborated during the second pass.
7548 -- If the loop comes from source, or the entity is local to the
7549 -- loop itself it must remain within.
7551 elsif Nkind (Parent (P)) = N_Loop_Statement
7552 and then not Comes_From_Source (Parent (P))
7553 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7554 and then
7555 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7556 then
7557 null;
7559 -- Otherwise we can go ahead and do the insertion
7561 elsif P = Wrapped_Node then
7562 Store_Before_Actions_In_Scope (Ins_Actions);
7563 return;
7565 else
7566 Insert_List_Before_And_Analyze (P, Ins_Actions);
7567 return;
7568 end if;
7570 -- the expansion of Task and protected type declarations can
7571 -- create declarations for temporaries which, like other actions
7572 -- are inserted and analyzed before the current declaraation.
7573 -- However, the current scope is the synchronized type, and
7574 -- for unnesting it is critical that the proper scope for these
7575 -- generated entities be the enclosing one.
7577 when N_Task_Type_Declaration
7578 | N_Protected_Type_Declaration =>
7580 Push_Scope (Scope (Current_Scope));
7581 Insert_List_Before_And_Analyze (P, Ins_Actions);
7582 Pop_Scope;
7583 return;
7585 -- A special case, N_Raise_xxx_Error can act either as a statement
7586 -- or a subexpression. We tell the difference by looking at the
7587 -- Etype. It is set to Standard_Void_Type in the statement case.
7589 when N_Raise_xxx_Error =>
7590 if Etype (P) = Standard_Void_Type then
7591 if P = Wrapped_Node then
7592 Store_Before_Actions_In_Scope (Ins_Actions);
7593 else
7594 Insert_List_Before_And_Analyze (P, Ins_Actions);
7595 end if;
7597 return;
7599 -- In the subexpression case, keep climbing
7601 else
7602 null;
7603 end if;
7605 -- If a component association appears within a loop created for
7606 -- an array aggregate, attach the actions to the association so
7607 -- they can be subsequently inserted within the loop. For other
7608 -- component associations insert outside of the aggregate. For
7609 -- an association that will generate a loop, its Loop_Actions
7610 -- attribute is already initialized (see exp_aggr.adb).
7612 -- The list of Loop_Actions can in turn generate additional ones,
7613 -- that are inserted before the associated node. If the associated
7614 -- node is outside the aggregate, the new actions are collected
7615 -- at the end of the Loop_Actions, to respect the order in which
7616 -- they are to be elaborated.
7618 when N_Component_Association
7619 | N_Iterated_Component_Association
7620 | N_Iterated_Element_Association
7622 if Nkind (Parent (P)) in N_Aggregate | N_Delta_Aggregate
7624 -- We must not climb up out of an N_Iterated_xxx_Association
7625 -- because the actions might contain references to the loop
7626 -- parameter. But it turns out that setting the Loop_Actions
7627 -- attribute in the case of an N_Component_Association
7628 -- when the attribute was not already set can lead to
7629 -- (as yet not understood) bugboxes (gcc failures that are
7630 -- presumably due to malformed trees). So we don't do that.
7632 and then (Nkind (P) /= N_Component_Association
7633 or else Present (Loop_Actions (P)))
7634 then
7635 if Is_Empty_List (Loop_Actions (P)) then
7636 Set_Loop_Actions (P, Ins_Actions);
7637 Analyze_List (Ins_Actions);
7638 else
7639 declare
7640 Decl : Node_Id;
7642 begin
7643 -- Check whether these actions were generated by a
7644 -- declaration that is part of the Loop_Actions for
7645 -- the component_association.
7647 Decl := Assoc_Node;
7648 while Present (Decl) loop
7649 exit when Parent (Decl) = P
7650 and then Is_List_Member (Decl)
7651 and then
7652 List_Containing (Decl) = Loop_Actions (P);
7653 Decl := Parent (Decl);
7654 end loop;
7656 if Present (Decl) then
7657 Insert_List_Before_And_Analyze
7658 (Decl, Ins_Actions);
7659 else
7660 Insert_List_After_And_Analyze
7661 (Last (Loop_Actions (P)), Ins_Actions);
7662 end if;
7663 end;
7664 end if;
7666 return;
7668 else
7669 null;
7670 end if;
7672 -- Special case: an attribute denoting a procedure call
7674 when N_Attribute_Reference =>
7675 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7676 if P = Wrapped_Node then
7677 Store_Before_Actions_In_Scope (Ins_Actions);
7678 else
7679 Insert_List_Before_And_Analyze (P, Ins_Actions);
7680 end if;
7682 return;
7684 -- In the subexpression case, keep climbing
7686 else
7687 null;
7688 end if;
7690 -- Special case: a marker
7692 when N_Call_Marker
7693 | N_Variable_Reference_Marker
7695 if Is_List_Member (P) then
7696 Insert_List_Before_And_Analyze (P, Ins_Actions);
7697 return;
7698 end if;
7700 -- A contract node should not belong to the tree
7702 when N_Contract =>
7703 raise Program_Error;
7705 -- For all other node types, keep climbing tree
7707 when N_Abortable_Part
7708 | N_Accept_Alternative
7709 | N_Access_Definition
7710 | N_Access_Function_Definition
7711 | N_Access_Procedure_Definition
7712 | N_Access_To_Object_Definition
7713 | N_Aggregate
7714 | N_Allocator
7715 | N_Aspect_Specification
7716 | N_Case_Expression
7717 | N_Case_Statement_Alternative
7718 | N_Character_Literal
7719 | N_Compilation_Unit
7720 | N_Compilation_Unit_Aux
7721 | N_Component_Clause
7722 | N_Component_Declaration
7723 | N_Component_Definition
7724 | N_Component_List
7725 | N_Constrained_Array_Definition
7726 | N_Decimal_Fixed_Point_Definition
7727 | N_Defining_Character_Literal
7728 | N_Defining_Identifier
7729 | N_Defining_Operator_Symbol
7730 | N_Defining_Program_Unit_Name
7731 | N_Delay_Alternative
7732 | N_Delta_Aggregate
7733 | N_Delta_Constraint
7734 | N_Derived_Type_Definition
7735 | N_Designator
7736 | N_Digits_Constraint
7737 | N_Discriminant_Association
7738 | N_Discriminant_Specification
7739 | N_Empty
7740 | N_Entry_Body_Formal_Part
7741 | N_Entry_Call_Alternative
7742 | N_Entry_Declaration
7743 | N_Entry_Index_Specification
7744 | N_Enumeration_Type_Definition
7745 | N_Error
7746 | N_Exception_Handler
7747 | N_Expanded_Name
7748 | N_Explicit_Dereference
7749 | N_Extension_Aggregate
7750 | N_Floating_Point_Definition
7751 | N_Formal_Decimal_Fixed_Point_Definition
7752 | N_Formal_Derived_Type_Definition
7753 | N_Formal_Discrete_Type_Definition
7754 | N_Formal_Floating_Point_Definition
7755 | N_Formal_Modular_Type_Definition
7756 | N_Formal_Ordinary_Fixed_Point_Definition
7757 | N_Formal_Package_Declaration
7758 | N_Formal_Private_Type_Definition
7759 | N_Formal_Incomplete_Type_Definition
7760 | N_Formal_Signed_Integer_Type_Definition
7761 | N_Function_Call
7762 | N_Function_Specification
7763 | N_Generic_Association
7764 | N_Handled_Sequence_Of_Statements
7765 | N_Identifier
7766 | N_In
7767 | N_Index_Or_Discriminant_Constraint
7768 | N_Indexed_Component
7769 | N_Integer_Literal
7770 | N_Iterator_Specification
7771 | N_Itype_Reference
7772 | N_Label
7773 | N_Loop_Parameter_Specification
7774 | N_Mod_Clause
7775 | N_Modular_Type_Definition
7776 | N_Not_In
7777 | N_Null
7778 | N_Op_Abs
7779 | N_Op_Add
7780 | N_Op_And
7781 | N_Op_Concat
7782 | N_Op_Divide
7783 | N_Op_Eq
7784 | N_Op_Expon
7785 | N_Op_Ge
7786 | N_Op_Gt
7787 | N_Op_Le
7788 | N_Op_Lt
7789 | N_Op_Minus
7790 | N_Op_Mod
7791 | N_Op_Multiply
7792 | N_Op_Ne
7793 | N_Op_Not
7794 | N_Op_Or
7795 | N_Op_Plus
7796 | N_Op_Rem
7797 | N_Op_Rotate_Left
7798 | N_Op_Rotate_Right
7799 | N_Op_Shift_Left
7800 | N_Op_Shift_Right
7801 | N_Op_Shift_Right_Arithmetic
7802 | N_Op_Subtract
7803 | N_Op_Xor
7804 | N_Operator_Symbol
7805 | N_Ordinary_Fixed_Point_Definition
7806 | N_Others_Choice
7807 | N_Package_Specification
7808 | N_Parameter_Association
7809 | N_Parameter_Specification
7810 | N_Pop_Constraint_Error_Label
7811 | N_Pop_Program_Error_Label
7812 | N_Pop_Storage_Error_Label
7813 | N_Pragma_Argument_Association
7814 | N_Procedure_Specification
7815 | N_Protected_Definition
7816 | N_Push_Constraint_Error_Label
7817 | N_Push_Program_Error_Label
7818 | N_Push_Storage_Error_Label
7819 | N_Qualified_Expression
7820 | N_Quantified_Expression
7821 | N_Raise_Expression
7822 | N_Range
7823 | N_Range_Constraint
7824 | N_Real_Literal
7825 | N_Real_Range_Specification
7826 | N_Record_Definition
7827 | N_Reference
7828 | N_SCIL_Dispatch_Table_Tag_Init
7829 | N_SCIL_Dispatching_Call
7830 | N_SCIL_Membership_Test
7831 | N_Selected_Component
7832 | N_Signed_Integer_Type_Definition
7833 | N_Single_Protected_Declaration
7834 | N_Slice
7835 | N_String_Literal
7836 | N_Subtype_Indication
7837 | N_Subunit
7838 | N_Target_Name
7839 | N_Task_Definition
7840 | N_Terminate_Alternative
7841 | N_Triggering_Alternative
7842 | N_Type_Conversion
7843 | N_Unchecked_Expression
7844 | N_Unchecked_Type_Conversion
7845 | N_Unconstrained_Array_Definition
7846 | N_Unused_At_End
7847 | N_Unused_At_Start
7848 | N_Variant
7849 | N_Variant_Part
7850 | N_Validate_Unchecked_Conversion
7851 | N_With_Clause
7853 null;
7854 end case;
7856 -- If we fall through above tests, keep climbing tree
7858 N := P;
7860 if Nkind (Parent (N)) = N_Subunit then
7862 -- This is the proper body corresponding to a stub. Insertion must
7863 -- be done at the point of the stub, which is in the declarative
7864 -- part of the parent unit.
7866 P := Corresponding_Stub (Parent (N));
7868 else
7869 P := Parent (N);
7870 end if;
7871 end loop;
7872 end Insert_Actions;
7874 -- Version with check(s) suppressed
7876 procedure Insert_Actions
7877 (Assoc_Node : Node_Id;
7878 Ins_Actions : List_Id;
7879 Suppress : Check_Id;
7880 Spec_Expr_OK : Boolean := False)
7882 begin
7883 if Suppress = All_Checks then
7884 declare
7885 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
7886 begin
7887 Scope_Suppress.Suppress := (others => True);
7888 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
7889 Scope_Suppress.Suppress := Sva;
7890 end;
7892 else
7893 declare
7894 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
7895 begin
7896 Scope_Suppress.Suppress (Suppress) := True;
7897 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
7898 Scope_Suppress.Suppress (Suppress) := Svg;
7899 end;
7900 end if;
7901 end Insert_Actions;
7903 --------------------------
7904 -- Insert_Actions_After --
7905 --------------------------
7907 procedure Insert_Actions_After
7908 (Assoc_Node : Node_Id;
7909 Ins_Actions : List_Id)
7911 begin
7912 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
7913 Store_After_Actions_In_Scope (Ins_Actions);
7914 else
7915 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
7916 end if;
7917 end Insert_Actions_After;
7919 ------------------------
7920 -- Insert_Declaration --
7921 ------------------------
7923 procedure Insert_Declaration (N : Node_Id; Decl : Node_Id) is
7924 P : Node_Id;
7926 begin
7927 pragma Assert (Nkind (N) in N_Subexpr);
7929 -- Climb until we find a procedure or a package
7931 P := N;
7932 loop
7933 pragma Assert (Present (Parent (P)));
7934 P := Parent (P);
7936 if Is_List_Member (P) then
7937 exit when Nkind (Parent (P)) in
7938 N_Package_Specification | N_Subprogram_Body;
7940 -- Special handling for handled sequence of statements, we must
7941 -- insert in the statements not the exception handlers!
7943 if Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements then
7944 P := First (Statements (Parent (P)));
7945 exit;
7946 end if;
7947 end if;
7948 end loop;
7950 -- Now do the insertion
7952 Insert_Before (P, Decl);
7953 Analyze (Decl);
7954 end Insert_Declaration;
7956 ---------------------------------
7957 -- Insert_Library_Level_Action --
7958 ---------------------------------
7960 procedure Insert_Library_Level_Action (N : Node_Id) is
7961 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7963 begin
7964 Push_Scope (Cunit_Entity (Current_Sem_Unit));
7965 -- And not Main_Unit as previously. If the main unit is a body,
7966 -- the scope needed to analyze the actions is the entity of the
7967 -- corresponding declaration.
7969 if No (Actions (Aux)) then
7970 Set_Actions (Aux, New_List (N));
7971 else
7972 Append (N, Actions (Aux));
7973 end if;
7975 Analyze (N);
7976 Pop_Scope;
7977 end Insert_Library_Level_Action;
7979 ----------------------------------
7980 -- Insert_Library_Level_Actions --
7981 ----------------------------------
7983 procedure Insert_Library_Level_Actions (L : List_Id) is
7984 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
7986 begin
7987 if Is_Non_Empty_List (L) then
7988 Push_Scope (Cunit_Entity (Main_Unit));
7989 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
7991 if No (Actions (Aux)) then
7992 Set_Actions (Aux, L);
7993 Analyze_List (L);
7994 else
7995 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
7996 end if;
7998 Pop_Scope;
7999 end if;
8000 end Insert_Library_Level_Actions;
8002 ----------------------
8003 -- Inside_Init_Proc --
8004 ----------------------
8006 function Inside_Init_Proc return Boolean is
8007 begin
8008 return Present (Enclosing_Init_Proc);
8009 end Inside_Init_Proc;
8011 ----------------------
8012 -- Integer_Type_For --
8013 ----------------------
8015 function Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id is
8016 begin
8017 pragma Assert (S <= System_Max_Integer_Size);
8019 -- This is the canonical 32-bit type
8021 if S <= Standard_Integer_Size then
8022 if Uns then
8023 return Standard_Unsigned;
8024 else
8025 return Standard_Integer;
8026 end if;
8028 -- This is the canonical 64-bit type
8030 elsif S <= Standard_Long_Long_Integer_Size then
8031 if Uns then
8032 return Standard_Long_Long_Unsigned;
8033 else
8034 return Standard_Long_Long_Integer;
8035 end if;
8037 -- This is the canonical 128-bit type
8039 elsif S <= Standard_Long_Long_Long_Integer_Size then
8040 if Uns then
8041 return Standard_Long_Long_Long_Unsigned;
8042 else
8043 return Standard_Long_Long_Long_Integer;
8044 end if;
8046 else
8047 raise Program_Error;
8048 end if;
8049 end Integer_Type_For;
8051 --------------------------------------------------
8052 -- Is_Displacement_Of_Object_Or_Function_Result --
8053 --------------------------------------------------
8055 function Is_Displacement_Of_Object_Or_Function_Result
8056 (Obj_Id : Entity_Id) return Boolean
8058 function Is_Controlled_Function_Call (N : Node_Id) return Boolean;
8059 -- Determine whether node N denotes a controlled function call
8061 function Is_Controlled_Indexing (N : Node_Id) return Boolean;
8062 -- Determine whether node N denotes a generalized indexing form which
8063 -- involves a controlled result.
8065 function Is_Displace_Call (N : Node_Id) return Boolean;
8066 -- Determine whether node N denotes a call to Ada.Tags.Displace
8068 function Is_Source_Object (N : Node_Id) return Boolean;
8069 -- Determine whether a particular node denotes a source object
8071 function Strip (N : Node_Id) return Node_Id;
8072 -- Examine arbitrary node N by stripping various indirections and return
8073 -- the "real" node.
8075 ---------------------------------
8076 -- Is_Controlled_Function_Call --
8077 ---------------------------------
8079 function Is_Controlled_Function_Call (N : Node_Id) return Boolean is
8080 Expr : Node_Id;
8082 begin
8083 -- When a function call appears in Object.Operation format, the
8084 -- original representation has several possible forms depending on
8085 -- the availability and form of actual parameters:
8087 -- Obj.Func N_Selected_Component
8088 -- Obj.Func (Actual) N_Indexed_Component
8089 -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an
8090 -- N_Selected_Component
8092 Expr := Original_Node (N);
8093 loop
8094 if Nkind (Expr) = N_Function_Call then
8095 Expr := Name (Expr);
8097 -- "Obj.Func (Actual)" case
8099 elsif Nkind (Expr) = N_Indexed_Component then
8100 Expr := Prefix (Expr);
8102 -- "Obj.Func" or "Obj.Func (Formal => Actual) case
8104 elsif Nkind (Expr) = N_Selected_Component then
8105 Expr := Selector_Name (Expr);
8107 else
8108 exit;
8109 end if;
8110 end loop;
8112 return
8113 Nkind (Expr) in N_Has_Entity
8114 and then Present (Entity (Expr))
8115 and then Ekind (Entity (Expr)) = E_Function
8116 and then Needs_Finalization (Etype (Entity (Expr)));
8117 end Is_Controlled_Function_Call;
8119 ----------------------------
8120 -- Is_Controlled_Indexing --
8121 ----------------------------
8123 function Is_Controlled_Indexing (N : Node_Id) return Boolean is
8124 Expr : constant Node_Id := Original_Node (N);
8126 begin
8127 return
8128 Nkind (Expr) = N_Indexed_Component
8129 and then Present (Generalized_Indexing (Expr))
8130 and then Needs_Finalization (Etype (Expr));
8131 end Is_Controlled_Indexing;
8133 ----------------------
8134 -- Is_Displace_Call --
8135 ----------------------
8137 function Is_Displace_Call (N : Node_Id) return Boolean is
8138 Call : constant Node_Id := Strip (N);
8140 begin
8141 return
8142 Present (Call)
8143 and then Nkind (Call) = N_Function_Call
8144 and then Nkind (Name (Call)) in N_Has_Entity
8145 and then Is_RTE (Entity (Name (Call)), RE_Displace);
8146 end Is_Displace_Call;
8148 ----------------------
8149 -- Is_Source_Object --
8150 ----------------------
8152 function Is_Source_Object (N : Node_Id) return Boolean is
8153 Obj : constant Node_Id := Strip (N);
8155 begin
8156 return
8157 Present (Obj)
8158 and then Comes_From_Source (Obj)
8159 and then Nkind (Obj) in N_Has_Entity
8160 and then Is_Object (Entity (Obj));
8161 end Is_Source_Object;
8163 -----------
8164 -- Strip --
8165 -----------
8167 function Strip (N : Node_Id) return Node_Id is
8168 Result : Node_Id;
8170 begin
8171 Result := N;
8172 loop
8173 if Nkind (Result) = N_Explicit_Dereference then
8174 Result := Prefix (Result);
8176 elsif Nkind (Result) in
8177 N_Type_Conversion | N_Unchecked_Type_Conversion
8178 then
8179 Result := Expression (Result);
8181 else
8182 exit;
8183 end if;
8184 end loop;
8186 return Result;
8187 end Strip;
8189 -- Local variables
8191 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
8192 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
8193 Orig_Decl : constant Node_Id := Original_Node (Obj_Decl);
8194 Orig_Expr : Node_Id;
8196 -- Start of processing for Is_Displacement_Of_Object_Or_Function_Result
8198 begin
8199 -- Case 1:
8201 -- Obj : CW_Type := Function_Call (...);
8203 -- is rewritten into:
8205 -- Temp : ... := Function_Call (...)'reference;
8206 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
8208 -- where the return type of the function and the class-wide type require
8209 -- dispatch table pointer displacement.
8211 -- Case 2:
8213 -- Obj : CW_Type := Container (...);
8215 -- is rewritten into:
8217 -- Temp : ... := Function_Call (Container, ...)'reference;
8218 -- Obj : CW_Type renames (... Ada.Tags.Displace (Temp));
8220 -- where the container element type and the class-wide type require
8221 -- dispatch table pointer dispacement.
8223 -- Case 3:
8225 -- Obj : CW_Type := Src_Obj;
8227 -- is rewritten into:
8229 -- Obj : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
8231 -- where the type of the source object and the class-wide type require
8232 -- dispatch table pointer displacement.
8234 if Nkind (Obj_Decl) = N_Object_Renaming_Declaration
8235 and then Is_Class_Wide_Type (Obj_Typ)
8236 and then Is_Displace_Call (Renamed_Object (Obj_Id))
8237 and then Nkind (Orig_Decl) = N_Object_Declaration
8238 and then Comes_From_Source (Orig_Decl)
8239 then
8240 Orig_Expr := Expression (Orig_Decl);
8242 return
8243 Is_Controlled_Function_Call (Orig_Expr)
8244 or else Is_Controlled_Indexing (Orig_Expr)
8245 or else Is_Source_Object (Orig_Expr);
8246 end if;
8248 return False;
8249 end Is_Displacement_Of_Object_Or_Function_Result;
8251 ------------------------------
8252 -- Is_Finalizable_Transient --
8253 ------------------------------
8255 function Is_Finalizable_Transient
8256 (Decl : Node_Id;
8257 Rel_Node : Node_Id) return Boolean
8259 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
8260 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
8262 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
8263 -- Determine whether transient object Trans_Id is initialized either
8264 -- by a function call which returns an access type or simply renames
8265 -- another pointer.
8267 function Initialized_By_Aliased_BIP_Func_Call
8268 (Trans_Id : Entity_Id) return Boolean;
8269 -- Determine whether transient object Trans_Id is initialized by a
8270 -- build-in-place function call where the BIPalloc parameter is of
8271 -- value 1 and BIPaccess is not null. This case creates an aliasing
8272 -- between the returned value and the value denoted by BIPaccess.
8274 function Is_Aliased
8275 (Trans_Id : Entity_Id;
8276 First_Stmt : Node_Id) return Boolean;
8277 -- Determine whether transient object Trans_Id has been renamed or
8278 -- aliased through 'reference in the statement list starting from
8279 -- First_Stmt.
8281 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
8282 -- Determine whether transient object Trans_Id is allocated on the heap
8284 function Is_Iterated_Container
8285 (Trans_Id : Entity_Id;
8286 First_Stmt : Node_Id) return Boolean;
8287 -- Determine whether transient object Trans_Id denotes a container which
8288 -- is in the process of being iterated in the statement list starting
8289 -- from First_Stmt.
8291 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean;
8292 -- Return True if N is directly part of a build-in-place return
8293 -- statement.
8295 ---------------------------
8296 -- Initialized_By_Access --
8297 ---------------------------
8299 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
8300 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8302 begin
8303 return
8304 Present (Expr)
8305 and then Nkind (Expr) /= N_Reference
8306 and then Is_Access_Type (Etype (Expr));
8307 end Initialized_By_Access;
8309 ------------------------------------------
8310 -- Initialized_By_Aliased_BIP_Func_Call --
8311 ------------------------------------------
8313 function Initialized_By_Aliased_BIP_Func_Call
8314 (Trans_Id : Entity_Id) return Boolean
8316 Call : Node_Id := Expression (Parent (Trans_Id));
8318 begin
8319 -- Build-in-place calls usually appear in 'reference format
8321 if Nkind (Call) = N_Reference then
8322 Call := Prefix (Call);
8323 end if;
8325 Call := Unqual_Conv (Call);
8327 if Is_Build_In_Place_Function_Call (Call) then
8328 declare
8329 Access_Nam : Name_Id := No_Name;
8330 Access_OK : Boolean := False;
8331 Actual : Node_Id;
8332 Alloc_Nam : Name_Id := No_Name;
8333 Alloc_OK : Boolean := False;
8334 Formal : Node_Id;
8335 Func_Id : Entity_Id;
8336 Param : Node_Id;
8338 begin
8339 -- Examine all parameter associations of the function call
8341 Param := First (Parameter_Associations (Call));
8342 while Present (Param) loop
8343 if Nkind (Param) = N_Parameter_Association
8344 and then Nkind (Selector_Name (Param)) = N_Identifier
8345 then
8346 Actual := Explicit_Actual_Parameter (Param);
8347 Formal := Selector_Name (Param);
8349 -- Construct the names of formals BIPaccess and BIPalloc
8350 -- using the function name retrieved from an arbitrary
8351 -- formal.
8353 if Access_Nam = No_Name
8354 and then Alloc_Nam = No_Name
8355 and then Present (Entity (Formal))
8356 then
8357 Func_Id := Scope (Entity (Formal));
8359 Access_Nam :=
8360 New_External_Name (Chars (Func_Id),
8361 BIP_Formal_Suffix (BIP_Object_Access));
8363 Alloc_Nam :=
8364 New_External_Name (Chars (Func_Id),
8365 BIP_Formal_Suffix (BIP_Alloc_Form));
8366 end if;
8368 -- A match for BIPaccess => Temp has been found
8370 if Chars (Formal) = Access_Nam
8371 and then Nkind (Actual) /= N_Null
8372 then
8373 Access_OK := True;
8374 end if;
8376 -- A match for BIPalloc => 1 has been found
8378 if Chars (Formal) = Alloc_Nam
8379 and then Nkind (Actual) = N_Integer_Literal
8380 and then Intval (Actual) = Uint_1
8381 then
8382 Alloc_OK := True;
8383 end if;
8384 end if;
8386 Next (Param);
8387 end loop;
8389 return Access_OK and Alloc_OK;
8390 end;
8391 end if;
8393 return False;
8394 end Initialized_By_Aliased_BIP_Func_Call;
8396 ----------------
8397 -- Is_Aliased --
8398 ----------------
8400 function Is_Aliased
8401 (Trans_Id : Entity_Id;
8402 First_Stmt : Node_Id) return Boolean
8404 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
8405 -- Given an object renaming declaration, retrieve the entity of the
8406 -- renamed name. Return Empty if the renamed name is anything other
8407 -- than a variable or a constant.
8409 -------------------------
8410 -- Find_Renamed_Object --
8411 -------------------------
8413 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
8414 Ren_Obj : Node_Id := Empty;
8416 function Find_Object (N : Node_Id) return Traverse_Result;
8417 -- Try to detect an object which is either a constant or a
8418 -- variable.
8420 -----------------
8421 -- Find_Object --
8422 -----------------
8424 function Find_Object (N : Node_Id) return Traverse_Result is
8425 begin
8426 -- Stop the search once a constant or a variable has been
8427 -- detected.
8429 if Nkind (N) = N_Identifier
8430 and then Present (Entity (N))
8431 and then Ekind (Entity (N)) in E_Constant | E_Variable
8432 then
8433 Ren_Obj := Entity (N);
8434 return Abandon;
8435 end if;
8437 return OK;
8438 end Find_Object;
8440 procedure Search is new Traverse_Proc (Find_Object);
8442 -- Local variables
8444 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
8446 -- Start of processing for Find_Renamed_Object
8448 begin
8449 -- Actions related to dispatching calls may appear as renamings of
8450 -- tags. Do not process this type of renaming because it does not
8451 -- use the actual value of the object.
8453 if not Is_RTE (Typ, RE_Tag_Ptr) then
8454 Search (Name (Ren_Decl));
8455 end if;
8457 return Ren_Obj;
8458 end Find_Renamed_Object;
8460 -- Local variables
8462 Expr : Node_Id;
8463 Ren_Obj : Entity_Id;
8464 Stmt : Node_Id;
8466 -- Start of processing for Is_Aliased
8468 begin
8469 -- A controlled transient object is not considered aliased when it
8470 -- appears inside an expression_with_actions node even when there are
8471 -- explicit aliases of it:
8473 -- do
8474 -- Trans_Id : Ctrl_Typ ...; -- transient object
8475 -- Alias : ... := Trans_Id; -- object is aliased
8476 -- Val : constant Boolean :=
8477 -- ... Alias ...; -- aliasing ends
8478 -- <finalize Trans_Id> -- object safe to finalize
8479 -- in Val end;
8481 -- Expansion ensures that all aliases are encapsulated in the actions
8482 -- list and do not leak to the expression by forcing the evaluation
8483 -- of the expression.
8485 if Nkind (Rel_Node) = N_Expression_With_Actions then
8486 return False;
8488 -- Otherwise examine the statements after the controlled transient
8489 -- object and look for various forms of aliasing.
8491 else
8492 Stmt := First_Stmt;
8493 while Present (Stmt) loop
8494 if Nkind (Stmt) = N_Object_Declaration then
8495 Expr := Expression (Stmt);
8497 -- Aliasing of the form:
8498 -- Obj : ... := Trans_Id'reference;
8500 if Present (Expr)
8501 and then Nkind (Expr) = N_Reference
8502 and then Nkind (Prefix (Expr)) = N_Identifier
8503 and then Entity (Prefix (Expr)) = Trans_Id
8504 then
8505 return True;
8506 end if;
8508 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8509 Ren_Obj := Find_Renamed_Object (Stmt);
8511 -- Aliasing of the form:
8512 -- Obj : ... renames ... Trans_Id ...;
8514 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8515 return True;
8516 end if;
8517 end if;
8519 Next (Stmt);
8520 end loop;
8522 return False;
8523 end if;
8524 end Is_Aliased;
8526 ------------------
8527 -- Is_Allocated --
8528 ------------------
8530 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8531 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8532 begin
8533 return
8534 Is_Access_Type (Etype (Trans_Id))
8535 and then Present (Expr)
8536 and then Nkind (Expr) = N_Allocator;
8537 end Is_Allocated;
8539 ---------------------------
8540 -- Is_Iterated_Container --
8541 ---------------------------
8543 function Is_Iterated_Container
8544 (Trans_Id : Entity_Id;
8545 First_Stmt : Node_Id) return Boolean
8547 Aspect : Node_Id;
8548 Call : Node_Id;
8549 Iter : Entity_Id;
8550 Param : Node_Id;
8551 Stmt : Node_Id;
8552 Typ : Entity_Id;
8554 begin
8555 -- It is not possible to iterate over containers in non-Ada 2012 code
8557 if Ada_Version < Ada_2012 then
8558 return False;
8559 end if;
8561 Typ := Etype (Trans_Id);
8563 -- Handle access type created for secondary stack use
8565 if Is_Access_Type (Typ) then
8566 Typ := Designated_Type (Typ);
8567 end if;
8569 -- Look for aspect Default_Iterator. It may be part of a type
8570 -- declaration for a container, or inherited from a base type
8571 -- or parent type.
8573 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8575 if Present (Aspect) then
8576 Iter := Entity (Aspect);
8578 -- Examine the statements following the container object and
8579 -- look for a call to the default iterate routine where the
8580 -- first parameter is the transient. Such a call appears as:
8582 -- It : Access_To_CW_Iterator :=
8583 -- Iterate (Tran_Id.all, ...)'reference;
8585 Stmt := First_Stmt;
8586 while Present (Stmt) loop
8588 -- Detect an object declaration which is initialized by a
8589 -- secondary stack function call.
8591 if Nkind (Stmt) = N_Object_Declaration
8592 and then Present (Expression (Stmt))
8593 and then Nkind (Expression (Stmt)) = N_Reference
8594 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8595 then
8596 Call := Prefix (Expression (Stmt));
8598 -- The call must invoke the default iterate routine of
8599 -- the container and the transient object must appear as
8600 -- the first actual parameter. Skip any calls whose names
8601 -- are not entities.
8603 if Is_Entity_Name (Name (Call))
8604 and then Entity (Name (Call)) = Iter
8605 and then Present (Parameter_Associations (Call))
8606 then
8607 Param := First (Parameter_Associations (Call));
8609 if Nkind (Param) = N_Explicit_Dereference
8610 and then Entity (Prefix (Param)) = Trans_Id
8611 then
8612 return True;
8613 end if;
8614 end if;
8615 end if;
8617 Next (Stmt);
8618 end loop;
8619 end if;
8621 return False;
8622 end Is_Iterated_Container;
8624 -------------------------------------
8625 -- Is_Part_Of_BIP_Return_Statement --
8626 -------------------------------------
8628 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean is
8629 Subp : constant Entity_Id := Current_Subprogram;
8630 Context : Node_Id;
8631 begin
8632 -- First check if N is part of a BIP function
8634 if No (Subp)
8635 or else not Is_Build_In_Place_Function (Subp)
8636 then
8637 return False;
8638 end if;
8640 -- Then check whether N is a complete part of a return statement
8641 -- Should we consider other node kinds to go up the tree???
8643 Context := N;
8644 loop
8645 case Nkind (Context) is
8646 when N_Expression_With_Actions => Context := Parent (Context);
8647 when N_Simple_Return_Statement => return True;
8648 when others => return False;
8649 end case;
8650 end loop;
8651 end Is_Part_Of_BIP_Return_Statement;
8653 -- Local variables
8655 Desig : Entity_Id := Obj_Typ;
8657 -- Start of processing for Is_Finalizable_Transient
8659 begin
8660 -- Handle access types
8662 if Is_Access_Type (Desig) then
8663 Desig := Available_View (Designated_Type (Desig));
8664 end if;
8666 return
8667 Ekind (Obj_Id) in E_Constant | E_Variable
8668 and then Needs_Finalization (Desig)
8669 and then Requires_Transient_Scope (Desig)
8670 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8671 and then not Is_Part_Of_BIP_Return_Statement (Rel_Node)
8673 -- Do not consider a transient object that was already processed
8675 and then not Is_Finalized_Transient (Obj_Id)
8677 -- Do not consider renamed or 'reference-d transient objects because
8678 -- the act of renaming extends the object's lifetime.
8680 and then not Is_Aliased (Obj_Id, Decl)
8682 -- Do not consider transient objects allocated on the heap since
8683 -- they are attached to a finalization master.
8685 and then not Is_Allocated (Obj_Id)
8687 -- If the transient object is a pointer, check that it is not
8688 -- initialized by a function that returns a pointer or acts as a
8689 -- renaming of another pointer.
8691 and then not
8692 (Is_Access_Type (Obj_Typ) and then Initialized_By_Access (Obj_Id))
8694 -- Do not consider transient objects which act as indirect aliases
8695 -- of build-in-place function results.
8697 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8699 -- Do not consider conversions of tags to class-wide types
8701 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
8703 -- Do not consider iterators because those are treated as normal
8704 -- controlled objects and are processed by the usual finalization
8705 -- machinery. This avoids the double finalization of an iterator.
8707 and then not Is_Iterator (Desig)
8709 -- Do not consider containers in the context of iterator loops. Such
8710 -- transient objects must exist for as long as the loop is around,
8711 -- otherwise any operation carried out by the iterator will fail.
8713 and then not Is_Iterated_Container (Obj_Id, Decl);
8714 end Is_Finalizable_Transient;
8716 ---------------------------------
8717 -- Is_Fully_Repped_Tagged_Type --
8718 ---------------------------------
8720 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8721 U : constant Entity_Id := Underlying_Type (T);
8722 Comp : Entity_Id;
8724 begin
8725 if No (U) or else not Is_Tagged_Type (U) then
8726 return False;
8727 elsif Has_Discriminants (U) then
8728 return False;
8729 elsif not Has_Specified_Layout (U) then
8730 return False;
8731 end if;
8733 -- Here we have a tagged type, see if it has any component (other than
8734 -- tag and parent) with no component_clause. If so, we return False.
8736 Comp := First_Component (U);
8737 while Present (Comp) loop
8738 if not Is_Tag (Comp)
8739 and then Chars (Comp) /= Name_uParent
8740 and then No (Component_Clause (Comp))
8741 then
8742 return False;
8743 else
8744 Next_Component (Comp);
8745 end if;
8746 end loop;
8748 -- All components have clauses
8750 return True;
8751 end Is_Fully_Repped_Tagged_Type;
8753 ----------------------------------
8754 -- Is_Library_Level_Tagged_Type --
8755 ----------------------------------
8757 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8758 begin
8759 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8760 end Is_Library_Level_Tagged_Type;
8762 --------------------------
8763 -- Is_Non_BIP_Func_Call --
8764 --------------------------
8766 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8767 begin
8768 -- The expected call is of the format
8770 -- Func_Call'reference
8772 return
8773 Nkind (Expr) = N_Reference
8774 and then Nkind (Prefix (Expr)) = N_Function_Call
8775 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8776 end Is_Non_BIP_Func_Call;
8778 ----------------------------------
8779 -- Is_Possibly_Unaligned_Object --
8780 ----------------------------------
8782 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8783 T : constant Entity_Id := Etype (N);
8785 begin
8786 -- If renamed object, apply test to underlying object
8788 if Is_Entity_Name (N)
8789 and then Is_Object (Entity (N))
8790 and then Present (Renamed_Object (Entity (N)))
8791 then
8792 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8793 end if;
8795 -- Tagged and controlled types and aliased types are always aligned, as
8796 -- are concurrent types.
8798 if Is_Aliased (T)
8799 or else Has_Controlled_Component (T)
8800 or else Is_Concurrent_Type (T)
8801 or else Is_Tagged_Type (T)
8802 or else Is_Controlled (T)
8803 then
8804 return False;
8805 end if;
8807 -- If this is an element of a packed array, may be unaligned
8809 if Is_Ref_To_Bit_Packed_Array (N) then
8810 return True;
8811 end if;
8813 -- Case of indexed component reference: test whether prefix is unaligned
8815 if Nkind (N) = N_Indexed_Component then
8816 return Is_Possibly_Unaligned_Object (Prefix (N));
8818 -- Case of selected component reference
8820 elsif Nkind (N) = N_Selected_Component then
8821 declare
8822 P : constant Node_Id := Prefix (N);
8823 C : constant Entity_Id := Entity (Selector_Name (N));
8824 M : Nat;
8825 S : Nat;
8827 begin
8828 -- If component reference is for an array with nonstatic bounds,
8829 -- then it is always aligned: we can only process unaligned arrays
8830 -- with static bounds (more precisely compile time known bounds).
8832 if Is_Array_Type (T)
8833 and then not Compile_Time_Known_Bounds (T)
8834 then
8835 return False;
8836 end if;
8838 -- If component is aliased, it is definitely properly aligned
8840 if Is_Aliased (C) then
8841 return False;
8842 end if;
8844 -- If component is for a type implemented as a scalar, and the
8845 -- record is packed, and the component is other than the first
8846 -- component of the record, then the component may be unaligned.
8848 if Is_Packed (Etype (P))
8849 and then Represented_As_Scalar (Etype (C))
8850 and then First_Entity (Scope (C)) /= C
8851 then
8852 return True;
8853 end if;
8855 -- Compute maximum possible alignment for T
8857 -- If alignment is known, then that settles things
8859 if Known_Alignment (T) then
8860 M := UI_To_Int (Alignment (T));
8862 -- If alignment is not known, tentatively set max alignment
8864 else
8865 M := Ttypes.Maximum_Alignment;
8867 -- We can reduce this if the Esize is known since the default
8868 -- alignment will never be more than the smallest power of 2
8869 -- that does not exceed this Esize value.
8871 if Known_Esize (T) then
8872 S := UI_To_Int (Esize (T));
8874 while (M / 2) >= S loop
8875 M := M / 2;
8876 end loop;
8877 end if;
8878 end if;
8880 -- Case of component clause present which may specify an
8881 -- unaligned position.
8883 if Present (Component_Clause (C)) then
8885 -- Otherwise we can do a test to make sure that the actual
8886 -- start position in the record, and the length, are both
8887 -- consistent with the required alignment. If not, we know
8888 -- that we are unaligned.
8890 declare
8891 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8892 Comp : Entity_Id;
8894 begin
8895 Comp := C;
8897 -- For a component inherited in a record extension, the
8898 -- clause is inherited but position and size are not set.
8900 if Is_Base_Type (Etype (P))
8901 and then Is_Tagged_Type (Etype (P))
8902 and then Present (Original_Record_Component (Comp))
8903 then
8904 Comp := Original_Record_Component (Comp);
8905 end if;
8907 if Component_Bit_Offset (Comp) mod Align_In_Bits /= 0
8908 or else Esize (Comp) mod Align_In_Bits /= 0
8909 then
8910 return True;
8911 end if;
8912 end;
8913 end if;
8915 -- Otherwise, for a component reference, test prefix
8917 return Is_Possibly_Unaligned_Object (P);
8918 end;
8920 -- If not a component reference, must be aligned
8922 else
8923 return False;
8924 end if;
8925 end Is_Possibly_Unaligned_Object;
8927 ---------------------------------
8928 -- Is_Possibly_Unaligned_Slice --
8929 ---------------------------------
8931 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8932 begin
8933 -- Go to renamed object
8935 if Is_Entity_Name (N)
8936 and then Is_Object (Entity (N))
8937 and then Present (Renamed_Object (Entity (N)))
8938 then
8939 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8940 end if;
8942 -- The reference must be a slice
8944 if Nkind (N) /= N_Slice then
8945 return False;
8946 end if;
8948 -- If it is a slice, then look at the array type being sliced
8950 declare
8951 Sarr : constant Node_Id := Prefix (N);
8952 -- Prefix of the slice, i.e. the array being sliced
8954 Styp : constant Entity_Id := Etype (Prefix (N));
8955 -- Type of the array being sliced
8957 Pref : Node_Id;
8958 Ptyp : Entity_Id;
8960 begin
8961 -- The problems arise if the array object that is being sliced
8962 -- is a component of a record or array, and we cannot guarantee
8963 -- the alignment of the array within its containing object.
8965 -- To investigate this, we look at successive prefixes to see
8966 -- if we have a worrisome indexed or selected component.
8968 Pref := Sarr;
8969 loop
8970 -- Case of array is part of an indexed component reference
8972 if Nkind (Pref) = N_Indexed_Component then
8973 Ptyp := Etype (Prefix (Pref));
8975 -- The only problematic case is when the array is packed, in
8976 -- which case we really know nothing about the alignment of
8977 -- individual components.
8979 if Is_Bit_Packed_Array (Ptyp) then
8980 return True;
8981 end if;
8983 -- Case of array is part of a selected component reference
8985 elsif Nkind (Pref) = N_Selected_Component then
8986 Ptyp := Etype (Prefix (Pref));
8988 -- We are definitely in trouble if the record in question
8989 -- has an alignment, and either we know this alignment is
8990 -- inconsistent with the alignment of the slice, or we don't
8991 -- know what the alignment of the slice should be. But this
8992 -- really matters only if the target has strict alignment.
8994 if Target_Strict_Alignment
8995 and then Known_Alignment (Ptyp)
8996 and then (not Known_Alignment (Styp)
8997 or else Alignment (Styp) > Alignment (Ptyp))
8998 then
8999 return True;
9000 end if;
9002 -- We are in potential trouble if the record type is packed.
9003 -- We could special case when we know that the array is the
9004 -- first component, but that's not such a simple case ???
9006 if Is_Packed (Ptyp) then
9007 return True;
9008 end if;
9010 -- We are in trouble if there is a component clause, and
9011 -- either we do not know the alignment of the slice, or
9012 -- the alignment of the slice is inconsistent with the
9013 -- bit position specified by the component clause.
9015 declare
9016 Field : constant Entity_Id := Entity (Selector_Name (Pref));
9017 begin
9018 if Present (Component_Clause (Field))
9019 and then
9020 (not Known_Alignment (Styp)
9021 or else
9022 (Component_Bit_Offset (Field) mod
9023 (System_Storage_Unit * Alignment (Styp))) /= 0)
9024 then
9025 return True;
9026 end if;
9027 end;
9029 -- For cases other than selected or indexed components we know we
9030 -- are OK, since no issues arise over alignment.
9032 else
9033 return False;
9034 end if;
9036 -- We processed an indexed component or selected component
9037 -- reference that looked safe, so keep checking prefixes.
9039 Pref := Prefix (Pref);
9040 end loop;
9041 end;
9042 end Is_Possibly_Unaligned_Slice;
9044 -------------------------------
9045 -- Is_Related_To_Func_Return --
9046 -------------------------------
9048 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
9049 Expr : constant Node_Id := Related_Expression (Id);
9050 begin
9051 -- In the case of a function with a class-wide result that returns
9052 -- a call to a function with a specific result, we introduce a
9053 -- type conversion for the return expression. We do not want that
9054 -- type conversion to influence the result of this function.
9056 return
9057 Present (Expr)
9058 and then Nkind (Unqual_Conv (Expr)) = N_Explicit_Dereference
9059 and then Nkind (Parent (Expr)) = N_Simple_Return_Statement;
9060 end Is_Related_To_Func_Return;
9062 --------------------------------
9063 -- Is_Ref_To_Bit_Packed_Array --
9064 --------------------------------
9066 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
9067 Result : Boolean;
9068 Expr : Node_Id;
9070 begin
9071 if Is_Entity_Name (N)
9072 and then Is_Object (Entity (N))
9073 and then Present (Renamed_Object (Entity (N)))
9074 then
9075 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
9076 end if;
9078 if Nkind (N) in N_Indexed_Component | N_Selected_Component then
9079 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
9080 Result := True;
9081 else
9082 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
9083 end if;
9085 if Result and then Nkind (N) = N_Indexed_Component then
9086 Expr := First (Expressions (N));
9087 while Present (Expr) loop
9088 Force_Evaluation (Expr);
9089 Next (Expr);
9090 end loop;
9091 end if;
9093 return Result;
9095 else
9096 return False;
9097 end if;
9098 end Is_Ref_To_Bit_Packed_Array;
9100 --------------------------------
9101 -- Is_Ref_To_Bit_Packed_Slice --
9102 --------------------------------
9104 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
9105 begin
9106 if Nkind (N) = N_Type_Conversion then
9107 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
9109 elsif Is_Entity_Name (N)
9110 and then Is_Object (Entity (N))
9111 and then Present (Renamed_Object (Entity (N)))
9112 then
9113 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
9115 elsif Nkind (N) = N_Slice
9116 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
9117 then
9118 return True;
9120 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9121 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
9123 else
9124 return False;
9125 end if;
9126 end Is_Ref_To_Bit_Packed_Slice;
9128 -----------------------
9129 -- Is_Renamed_Object --
9130 -----------------------
9132 function Is_Renamed_Object (N : Node_Id) return Boolean is
9133 Pnod : constant Node_Id := Parent (N);
9134 Kind : constant Node_Kind := Nkind (Pnod);
9135 begin
9136 if Kind = N_Object_Renaming_Declaration then
9137 return True;
9138 elsif Kind in N_Indexed_Component | N_Selected_Component then
9139 return Is_Renamed_Object (Pnod);
9140 else
9141 return False;
9142 end if;
9143 end Is_Renamed_Object;
9145 --------------------------------------
9146 -- Is_Secondary_Stack_BIP_Func_Call --
9147 --------------------------------------
9149 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
9150 Actual : Node_Id;
9151 Call : Node_Id := Expr;
9152 Formal : Node_Id;
9153 Param : Node_Id;
9155 begin
9156 -- Build-in-place calls usually appear in 'reference format. Note that
9157 -- the accessibility check machinery may add an extra 'reference due to
9158 -- side effect removal.
9160 while Nkind (Call) = N_Reference loop
9161 Call := Prefix (Call);
9162 end loop;
9164 Call := Unqual_Conv (Call);
9166 if Is_Build_In_Place_Function_Call (Call) then
9168 -- Examine all parameter associations of the function call
9170 Param := First (Parameter_Associations (Call));
9171 while Present (Param) loop
9172 if Nkind (Param) = N_Parameter_Association then
9173 Formal := Selector_Name (Param);
9174 Actual := Explicit_Actual_Parameter (Param);
9176 -- A match for BIPalloc => 2 has been found
9178 if Is_Build_In_Place_Entity (Formal)
9179 and then BIP_Suffix_Kind (Formal) = BIP_Alloc_Form
9180 and then Nkind (Actual) = N_Integer_Literal
9181 and then Intval (Actual) = Uint_2
9182 then
9183 return True;
9184 end if;
9185 end if;
9187 Next (Param);
9188 end loop;
9189 end if;
9191 return False;
9192 end Is_Secondary_Stack_BIP_Func_Call;
9194 -------------------------------------
9195 -- Is_Tag_To_Class_Wide_Conversion --
9196 -------------------------------------
9198 function Is_Tag_To_Class_Wide_Conversion
9199 (Obj_Id : Entity_Id) return Boolean
9201 Expr : constant Node_Id := Expression (Parent (Obj_Id));
9203 begin
9204 return
9205 Is_Class_Wide_Type (Etype (Obj_Id))
9206 and then Present (Expr)
9207 and then Nkind (Expr) = N_Unchecked_Type_Conversion
9208 and then Is_RTE (Etype (Expression (Expr)), RE_Tag);
9209 end Is_Tag_To_Class_Wide_Conversion;
9211 --------------------------------
9212 -- Is_Uninitialized_Aggregate --
9213 --------------------------------
9215 function Is_Uninitialized_Aggregate
9216 (Exp : Node_Id;
9217 T : Entity_Id) return Boolean
9219 Comp : Node_Id;
9220 Comp_Type : Entity_Id;
9221 Typ : Entity_Id;
9223 begin
9224 if Nkind (Exp) /= N_Aggregate then
9225 return False;
9226 end if;
9228 Preanalyze_And_Resolve (Exp, T);
9229 Typ := Etype (Exp);
9231 if No (Typ)
9232 or else Ekind (Typ) /= E_Array_Subtype
9233 or else Present (Expressions (Exp))
9234 or else No (Component_Associations (Exp))
9235 then
9236 return False;
9237 else
9238 Comp_Type := Component_Type (Typ);
9239 Comp := First (Component_Associations (Exp));
9241 if not Box_Present (Comp)
9242 or else Present (Next (Comp))
9243 then
9244 return False;
9245 end if;
9247 return Is_Scalar_Type (Comp_Type)
9248 and then No (Default_Aspect_Component_Value (Typ));
9249 end if;
9250 end Is_Uninitialized_Aggregate;
9252 ----------------------------
9253 -- Is_Untagged_Derivation --
9254 ----------------------------
9256 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
9257 begin
9258 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
9259 or else
9260 (Is_Private_Type (T) and then Present (Full_View (T))
9261 and then not Is_Tagged_Type (Full_View (T))
9262 and then Is_Derived_Type (Full_View (T))
9263 and then Etype (Full_View (T)) /= T);
9264 end Is_Untagged_Derivation;
9266 ------------------------------------
9267 -- Is_Untagged_Private_Derivation --
9268 ------------------------------------
9270 function Is_Untagged_Private_Derivation
9271 (Priv_Typ : Entity_Id;
9272 Full_Typ : Entity_Id) return Boolean
9274 begin
9275 return
9276 Present (Priv_Typ)
9277 and then Is_Untagged_Derivation (Priv_Typ)
9278 and then Is_Private_Type (Etype (Priv_Typ))
9279 and then Present (Full_Typ)
9280 and then Is_Itype (Full_Typ);
9281 end Is_Untagged_Private_Derivation;
9283 ------------------------------
9284 -- Is_Verifiable_DIC_Pragma --
9285 ------------------------------
9287 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
9288 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9290 begin
9291 -- To qualify as verifiable, a DIC pragma must have a non-null argument
9293 return
9294 Present (Args)
9296 -- If there are args, but the first arg is Empty, then treat the
9297 -- pragma the same as having no args (there may be a second arg that
9298 -- is an implicitly added type arg, and Empty is a placeholder).
9300 and then Present (Get_Pragma_Arg (First (Args)))
9302 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
9303 end Is_Verifiable_DIC_Pragma;
9305 ---------------------------
9306 -- Is_Volatile_Reference --
9307 ---------------------------
9309 function Is_Volatile_Reference (N : Node_Id) return Boolean is
9310 begin
9311 -- Only source references are to be treated as volatile, internally
9312 -- generated stuff cannot have volatile external effects.
9314 if not Comes_From_Source (N) then
9315 return False;
9317 -- Never true for reference to a type
9319 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9320 return False;
9322 -- Never true for a compile time known constant
9324 elsif Compile_Time_Known_Value (N) then
9325 return False;
9327 -- True if object reference with volatile type
9329 elsif Is_Volatile_Object_Ref (N) then
9330 return True;
9332 -- True if reference to volatile entity
9334 elsif Is_Entity_Name (N) then
9335 return Treat_As_Volatile (Entity (N));
9337 -- True for slice of volatile array
9339 elsif Nkind (N) = N_Slice then
9340 return Is_Volatile_Reference (Prefix (N));
9342 -- True if volatile component
9344 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9345 if (Is_Entity_Name (Prefix (N))
9346 and then Has_Volatile_Components (Entity (Prefix (N))))
9347 or else (Present (Etype (Prefix (N)))
9348 and then Has_Volatile_Components (Etype (Prefix (N))))
9349 then
9350 return True;
9351 else
9352 return Is_Volatile_Reference (Prefix (N));
9353 end if;
9355 -- Otherwise false
9357 else
9358 return False;
9359 end if;
9360 end Is_Volatile_Reference;
9362 --------------------
9363 -- Kill_Dead_Code --
9364 --------------------
9366 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
9367 W : Boolean := Warn;
9368 -- Set False if warnings suppressed
9370 begin
9371 if Present (N) then
9372 Remove_Warning_Messages (N);
9374 -- Update the internal structures of the ABE mechanism in case the
9375 -- dead node is an elaboration scenario.
9377 Kill_Elaboration_Scenario (N);
9379 -- Generate warning if appropriate
9381 if W then
9383 -- We suppress the warning if this code is under control of an
9384 -- if/case statement and either
9385 -- a) we are in an instance and the condition/selector
9386 -- has a statically known value; or
9387 -- b) the condition/selector is a simple identifier and
9388 -- warnings off is set for this identifier.
9389 -- Dead code is common and reasonable in instances, so we don't
9390 -- want a warning in that case.
9392 declare
9393 C : Node_Id := Empty;
9394 begin
9395 if Nkind (Parent (N)) = N_If_Statement then
9396 C := Condition (Parent (N));
9397 elsif Nkind (Parent (N)) = N_Case_Statement_Alternative then
9398 C := Expression (Parent (Parent (N)));
9399 end if;
9401 if Present (C) then
9402 if (In_Instance and Compile_Time_Known_Value (C))
9403 or else (Nkind (C) = N_Identifier
9404 and then Present (Entity (C))
9405 and then Has_Warnings_Off (Entity (C)))
9406 then
9407 W := False;
9408 end if;
9409 end if;
9410 end;
9412 -- Generate warning if not suppressed
9414 if W then
9415 Error_Msg_F
9416 ("?t?this code can never be executed and has been deleted!",
9418 end if;
9419 end if;
9421 -- Recurse into block statements and bodies to process declarations
9422 -- and statements.
9424 if Nkind (N) = N_Block_Statement
9425 or else Nkind (N) = N_Subprogram_Body
9426 or else Nkind (N) = N_Package_Body
9427 then
9428 Kill_Dead_Code (Declarations (N), False);
9429 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
9431 if Nkind (N) = N_Subprogram_Body then
9432 Set_Is_Eliminated (Defining_Entity (N));
9433 end if;
9435 elsif Nkind (N) = N_Package_Declaration then
9436 Kill_Dead_Code (Visible_Declarations (Specification (N)));
9437 Kill_Dead_Code (Private_Declarations (Specification (N)));
9439 -- ??? After this point, Delete_Tree has been called on all
9440 -- declarations in Specification (N), so references to entities
9441 -- therein look suspicious.
9443 declare
9444 E : Entity_Id := First_Entity (Defining_Entity (N));
9446 begin
9447 while Present (E) loop
9448 if Ekind (E) = E_Operator then
9449 Set_Is_Eliminated (E);
9450 end if;
9452 Next_Entity (E);
9453 end loop;
9454 end;
9456 -- Recurse into composite statement to kill individual statements in
9457 -- particular instantiations.
9459 elsif Nkind (N) = N_If_Statement then
9460 Kill_Dead_Code (Then_Statements (N));
9461 Kill_Dead_Code (Elsif_Parts (N));
9462 Kill_Dead_Code (Else_Statements (N));
9464 elsif Nkind (N) = N_Loop_Statement then
9465 Kill_Dead_Code (Statements (N));
9467 elsif Nkind (N) = N_Case_Statement then
9468 declare
9469 Alt : Node_Id;
9470 begin
9471 Alt := First (Alternatives (N));
9472 while Present (Alt) loop
9473 Kill_Dead_Code (Statements (Alt));
9474 Next (Alt);
9475 end loop;
9476 end;
9478 elsif Nkind (N) = N_Case_Statement_Alternative then
9479 Kill_Dead_Code (Statements (N));
9481 -- Deal with dead instances caused by deleting instantiations
9483 elsif Nkind (N) in N_Generic_Instantiation then
9484 Remove_Dead_Instance (N);
9485 end if;
9486 end if;
9487 end Kill_Dead_Code;
9489 -- Case where argument is a list of nodes to be killed
9491 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
9492 N : Node_Id;
9493 W : Boolean;
9495 begin
9496 W := Warn;
9498 if Is_Non_Empty_List (L) then
9499 N := First (L);
9500 while Present (N) loop
9501 Kill_Dead_Code (N, W);
9502 W := False;
9503 Next (N);
9504 end loop;
9505 end if;
9506 end Kill_Dead_Code;
9508 -----------------------------
9509 -- Make_CW_Equivalent_Type --
9510 -----------------------------
9512 -- Create a record type used as an equivalent of any member of the class
9513 -- which takes its size from exp.
9515 -- Generate the following code:
9517 -- type Equiv_T is record
9518 -- _parent : T (List of discriminant constraints taken from Exp);
9519 -- Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
9520 -- end Equiv_T;
9522 -- ??? Note that this type does not guarantee same alignment as all
9523 -- derived types
9525 -- Note: for the freezing circuitry, this looks like a record extension,
9526 -- and so we need to make sure that the scalar storage order is the same
9527 -- as that of the parent type. (This does not change anything for the
9528 -- representation of the extension part.)
9530 function Make_CW_Equivalent_Type
9531 (T : Entity_Id;
9532 E : Node_Id) return Entity_Id
9534 Loc : constant Source_Ptr := Sloc (E);
9535 Root_Typ : constant Entity_Id := Root_Type (T);
9536 Root_Utyp : constant Entity_Id := Underlying_Type (Root_Typ);
9537 List_Def : constant List_Id := Empty_List;
9538 Comp_List : constant List_Id := New_List;
9539 Equiv_Type : Entity_Id;
9540 Range_Type : Entity_Id;
9541 Str_Type : Entity_Id;
9542 Constr_Root : Entity_Id;
9543 Sizexpr : Node_Id;
9545 begin
9546 -- If the root type is already constrained, there are no discriminants
9547 -- in the expression.
9549 if not Has_Discriminants (Root_Typ)
9550 or else Is_Constrained (Root_Typ)
9551 then
9552 Constr_Root := Root_Typ;
9554 -- At this point in the expansion, nonlimited view of the type
9555 -- must be available, otherwise the error will be reported later.
9557 if From_Limited_With (Constr_Root)
9558 and then Present (Non_Limited_View (Constr_Root))
9559 then
9560 Constr_Root := Non_Limited_View (Constr_Root);
9561 end if;
9563 else
9564 Constr_Root := Make_Temporary (Loc, 'R');
9566 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9568 Append_To (List_Def,
9569 Make_Subtype_Declaration (Loc,
9570 Defining_Identifier => Constr_Root,
9571 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9572 end if;
9574 -- Generate the range subtype declaration
9576 Range_Type := Make_Temporary (Loc, 'G');
9578 if not Is_Interface (Root_Typ) then
9580 -- subtype rg__xx is
9581 -- Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
9583 Sizexpr :=
9584 Make_Op_Subtract (Loc,
9585 Left_Opnd =>
9586 Make_Attribute_Reference (Loc,
9587 Prefix =>
9588 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9589 Attribute_Name => Name_Size),
9590 Right_Opnd =>
9591 Make_Attribute_Reference (Loc,
9592 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9593 Attribute_Name => Name_Object_Size));
9594 else
9595 -- subtype rg__xx is
9596 -- Storage_Offset range 1 .. Expr'size / Storage_Unit
9598 Sizexpr :=
9599 Make_Attribute_Reference (Loc,
9600 Prefix =>
9601 OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9602 Attribute_Name => Name_Size);
9603 end if;
9605 Set_Paren_Count (Sizexpr, 1);
9607 Append_To (List_Def,
9608 Make_Subtype_Declaration (Loc,
9609 Defining_Identifier => Range_Type,
9610 Subtype_Indication =>
9611 Make_Subtype_Indication (Loc,
9612 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9613 Constraint => Make_Range_Constraint (Loc,
9614 Range_Expression =>
9615 Make_Range (Loc,
9616 Low_Bound => Make_Integer_Literal (Loc, 1),
9617 High_Bound =>
9618 Make_Op_Divide (Loc,
9619 Left_Opnd => Sizexpr,
9620 Right_Opnd => Make_Integer_Literal (Loc,
9621 Intval => System_Storage_Unit)))))));
9623 -- subtype str__nn is Storage_Array (rg__x);
9625 Str_Type := Make_Temporary (Loc, 'S');
9626 Append_To (List_Def,
9627 Make_Subtype_Declaration (Loc,
9628 Defining_Identifier => Str_Type,
9629 Subtype_Indication =>
9630 Make_Subtype_Indication (Loc,
9631 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9632 Constraint =>
9633 Make_Index_Or_Discriminant_Constraint (Loc,
9634 Constraints =>
9635 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9637 -- type Equiv_T is record
9638 -- [ _parent : Tnn; ]
9639 -- E : Str_Type;
9640 -- end Equiv_T;
9642 Equiv_Type := Make_Temporary (Loc, 'T');
9643 Mutate_Ekind (Equiv_Type, E_Record_Type);
9644 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9646 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9647 -- treatment for this type. In particular, even though _parent's type
9648 -- is a controlled type or contains controlled components, we do not
9649 -- want to set Has_Controlled_Component on it to avoid making it gain
9650 -- an unwanted _controller component.
9652 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9654 -- A class-wide equivalent type does not require initialization
9656 Set_Suppress_Initialization (Equiv_Type);
9658 if not Is_Interface (Root_Typ) then
9659 Append_To (Comp_List,
9660 Make_Component_Declaration (Loc,
9661 Defining_Identifier =>
9662 Make_Defining_Identifier (Loc, Name_uParent),
9663 Component_Definition =>
9664 Make_Component_Definition (Loc,
9665 Aliased_Present => False,
9666 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9668 Set_Reverse_Storage_Order
9669 (Equiv_Type, Reverse_Storage_Order (Base_Type (Root_Utyp)));
9670 Set_Reverse_Bit_Order
9671 (Equiv_Type, Reverse_Bit_Order (Base_Type (Root_Utyp)));
9672 end if;
9674 Append_To (Comp_List,
9675 Make_Component_Declaration (Loc,
9676 Defining_Identifier => Make_Temporary (Loc, 'C'),
9677 Component_Definition =>
9678 Make_Component_Definition (Loc,
9679 Aliased_Present => False,
9680 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9682 Append_To (List_Def,
9683 Make_Full_Type_Declaration (Loc,
9684 Defining_Identifier => Equiv_Type,
9685 Type_Definition =>
9686 Make_Record_Definition (Loc,
9687 Component_List =>
9688 Make_Component_List (Loc,
9689 Component_Items => Comp_List,
9690 Variant_Part => Empty))));
9692 -- Suppress all checks during the analysis of the expanded code to avoid
9693 -- the generation of spurious warnings under ZFP run-time.
9695 Insert_Actions (E, List_Def, Suppress => All_Checks);
9696 return Equiv_Type;
9697 end Make_CW_Equivalent_Type;
9699 -------------------------
9700 -- Make_Invariant_Call --
9701 -------------------------
9703 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9704 Loc : constant Source_Ptr := Sloc (Expr);
9705 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9706 pragma Assert (Has_Invariants (Typ));
9707 Proc_Id : constant Entity_Id := Invariant_Procedure (Typ);
9708 pragma Assert (Present (Proc_Id));
9709 begin
9710 -- The invariant procedure has a null body if assertions are disabled or
9711 -- Assertion_Policy Ignore is in effect. In that case, generate a null
9712 -- statement instead of a call to the invariant procedure.
9714 if Has_Null_Body (Proc_Id) then
9715 return Make_Null_Statement (Loc);
9716 else
9717 return
9718 Make_Procedure_Call_Statement (Loc,
9719 Name => New_Occurrence_Of (Proc_Id, Loc),
9720 Parameter_Associations => New_List (Relocate_Node (Expr)));
9721 end if;
9722 end Make_Invariant_Call;
9724 ------------------------
9725 -- Make_Literal_Range --
9726 ------------------------
9728 function Make_Literal_Range
9729 (Loc : Source_Ptr;
9730 Literal_Typ : Entity_Id) return Node_Id
9732 Lo : constant Node_Id :=
9733 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9734 Index : constant Entity_Id := Etype (Lo);
9735 Length_Expr : constant Node_Id :=
9736 Make_Op_Subtract (Loc,
9737 Left_Opnd =>
9738 Make_Integer_Literal (Loc,
9739 Intval => String_Literal_Length (Literal_Typ)),
9740 Right_Opnd => Make_Integer_Literal (Loc, 1));
9742 Hi : Node_Id;
9744 begin
9745 Set_Analyzed (Lo, False);
9747 if Is_Integer_Type (Index) then
9748 Hi :=
9749 Make_Op_Add (Loc,
9750 Left_Opnd => New_Copy_Tree (Lo),
9751 Right_Opnd => Length_Expr);
9752 else
9753 Hi :=
9754 Make_Attribute_Reference (Loc,
9755 Attribute_Name => Name_Val,
9756 Prefix => New_Occurrence_Of (Index, Loc),
9757 Expressions => New_List (
9758 Make_Op_Add (Loc,
9759 Left_Opnd =>
9760 Make_Attribute_Reference (Loc,
9761 Attribute_Name => Name_Pos,
9762 Prefix => New_Occurrence_Of (Index, Loc),
9763 Expressions => New_List (New_Copy_Tree (Lo))),
9764 Right_Opnd => Length_Expr)));
9765 end if;
9767 return
9768 Make_Range (Loc,
9769 Low_Bound => Lo,
9770 High_Bound => Hi);
9771 end Make_Literal_Range;
9773 --------------------------
9774 -- Make_Non_Empty_Check --
9775 --------------------------
9777 function Make_Non_Empty_Check
9778 (Loc : Source_Ptr;
9779 N : Node_Id) return Node_Id
9781 begin
9782 return
9783 Make_Op_Ne (Loc,
9784 Left_Opnd =>
9785 Make_Attribute_Reference (Loc,
9786 Attribute_Name => Name_Length,
9787 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9788 Right_Opnd =>
9789 Make_Integer_Literal (Loc, 0));
9790 end Make_Non_Empty_Check;
9792 -------------------------
9793 -- Make_Predicate_Call --
9794 -------------------------
9796 -- WARNING: This routine manages Ghost regions. Return statements must be
9797 -- replaced by gotos which jump to the end of the routine and restore the
9798 -- Ghost mode.
9800 function Make_Predicate_Call
9801 (Typ : Entity_Id;
9802 Expr : Node_Id;
9803 Mem : Boolean := False) return Node_Id
9805 Loc : constant Source_Ptr := Sloc (Expr);
9807 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9808 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9809 -- Save the Ghost-related attributes to restore on exit
9811 Call : Node_Id;
9812 Func_Id : Entity_Id;
9814 begin
9815 Func_Id := Predicate_Function (Typ);
9816 pragma Assert (Present (Func_Id));
9818 -- The related type may be subject to pragma Ghost. Set the mode now to
9819 -- ensure that the call is properly marked as Ghost.
9821 Set_Ghost_Mode (Typ);
9823 -- Call special membership version if requested and available
9825 if Mem and then Present (Predicate_Function_M (Typ)) then
9826 Func_Id := Predicate_Function_M (Typ);
9827 end if;
9829 -- Case of calling normal predicate function
9831 -- If the type is tagged, the expression may be class-wide, in which
9832 -- case it has to be converted to its root type, given that the
9833 -- generated predicate function is not dispatching. The conversion is
9834 -- type-safe and does not need validation, which matters when private
9835 -- extensions are involved.
9837 if Is_Tagged_Type (Typ) then
9838 Call :=
9839 Make_Function_Call (Loc,
9840 Name => New_Occurrence_Of (Func_Id, Loc),
9841 Parameter_Associations =>
9842 New_List (OK_Convert_To (Typ, Relocate_Node (Expr))));
9843 else
9844 Call :=
9845 Make_Function_Call (Loc,
9846 Name => New_Occurrence_Of (Func_Id, Loc),
9847 Parameter_Associations => New_List (Relocate_Node (Expr)));
9848 end if;
9850 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9852 return Call;
9853 end Make_Predicate_Call;
9855 --------------------------
9856 -- Make_Predicate_Check --
9857 --------------------------
9859 function Make_Predicate_Check
9860 (Typ : Entity_Id;
9861 Expr : Node_Id) return Node_Id
9863 Loc : constant Source_Ptr := Sloc (Expr);
9865 procedure Add_Failure_Expression (Args : List_Id);
9866 -- Add the failure expression of pragma Predicate_Failure (if any) to
9867 -- list Args.
9869 ----------------------------
9870 -- Add_Failure_Expression --
9871 ----------------------------
9873 procedure Add_Failure_Expression (Args : List_Id) is
9874 function Failure_Expression return Node_Id;
9875 pragma Inline (Failure_Expression);
9876 -- Find aspect or pragma Predicate_Failure that applies to type Typ
9877 -- and return its expression. Return Empty if no such annotation is
9878 -- available.
9880 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean;
9881 pragma Inline (Is_OK_PF_Aspect);
9882 -- Determine whether aspect Asp is a suitable Predicate_Failure
9883 -- aspect that applies to type Typ.
9885 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean;
9886 pragma Inline (Is_OK_PF_Pragma);
9887 -- Determine whether pragma Prag is a suitable Predicate_Failure
9888 -- pragma that applies to type Typ.
9890 procedure Replace_Subtype_Reference (N : Node_Id);
9891 -- Replace the current instance of type Typ denoted by N with
9892 -- expression Expr.
9894 ------------------------
9895 -- Failure_Expression --
9896 ------------------------
9898 function Failure_Expression return Node_Id is
9899 Item : Node_Id;
9901 begin
9902 -- The management of the rep item chain involves "inheritance" of
9903 -- parent type chains. If a parent [sub]type is already subject to
9904 -- pragma Predicate_Failure, then the pragma will also appear in
9905 -- the chain of the child [sub]type, which in turn may possess a
9906 -- pragma of its own. Avoid order-dependent issues by inspecting
9907 -- the rep item chain directly. Note that routine Get_Pragma may
9908 -- return a parent pragma.
9910 Item := First_Rep_Item (Typ);
9911 while Present (Item) loop
9913 -- Predicate_Failure appears as an aspect
9915 if Nkind (Item) = N_Aspect_Specification
9916 and then Is_OK_PF_Aspect (Item)
9917 then
9918 return Expression (Item);
9920 -- Predicate_Failure appears as a pragma
9922 elsif Nkind (Item) = N_Pragma
9923 and then Is_OK_PF_Pragma (Item)
9924 then
9925 return
9926 Get_Pragma_Arg
9927 (Next (First (Pragma_Argument_Associations (Item))));
9928 end if;
9930 Next_Rep_Item (Item);
9931 end loop;
9933 return Empty;
9934 end Failure_Expression;
9936 ---------------------
9937 -- Is_OK_PF_Aspect --
9938 ---------------------
9940 function Is_OK_PF_Aspect (Asp : Node_Id) return Boolean is
9941 begin
9942 -- To qualify, the aspect must apply to the type subjected to the
9943 -- predicate check.
9945 return
9946 Chars (Identifier (Asp)) = Name_Predicate_Failure
9947 and then Present (Entity (Asp))
9948 and then Entity (Asp) = Typ;
9949 end Is_OK_PF_Aspect;
9951 ---------------------
9952 -- Is_OK_PF_Pragma --
9953 ---------------------
9955 function Is_OK_PF_Pragma (Prag : Node_Id) return Boolean is
9956 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9957 Typ_Arg : Node_Id;
9959 begin
9960 -- Nothing to do when the pragma does not denote Predicate_Failure
9962 if Pragma_Name (Prag) /= Name_Predicate_Failure then
9963 return False;
9965 -- Nothing to do when the pragma lacks arguments, in which case it
9966 -- is illegal.
9968 elsif No (Args) or else Is_Empty_List (Args) then
9969 return False;
9970 end if;
9972 Typ_Arg := Get_Pragma_Arg (First (Args));
9974 -- To qualify, the local name argument of the pragma must denote
9975 -- the type subjected to the predicate check.
9977 return
9978 Is_Entity_Name (Typ_Arg)
9979 and then Present (Entity (Typ_Arg))
9980 and then Entity (Typ_Arg) = Typ;
9981 end Is_OK_PF_Pragma;
9983 --------------------------------
9984 -- Replace_Subtype_Reference --
9985 --------------------------------
9987 procedure Replace_Subtype_Reference (N : Node_Id) is
9988 begin
9989 Rewrite (N, New_Copy_Tree (Expr));
9990 end Replace_Subtype_Reference;
9992 procedure Replace_Subtype_References is
9993 new Replace_Type_References_Generic (Replace_Subtype_Reference);
9995 -- Local variables
9997 PF_Expr : constant Node_Id := Failure_Expression;
9998 Expr : Node_Id;
10000 -- Start of processing for Add_Failure_Expression
10002 begin
10003 if Present (PF_Expr) then
10005 -- Replace any occurrences of the current instance of the type
10006 -- with the object subjected to the predicate check.
10008 Expr := New_Copy_Tree (PF_Expr);
10009 Replace_Subtype_References (Expr, Typ);
10011 -- The failure expression appears as the third argument of the
10012 -- Check pragma.
10014 Append_To (Args,
10015 Make_Pragma_Argument_Association (Loc,
10016 Expression => Expr));
10017 end if;
10018 end Add_Failure_Expression;
10020 -- Local variables
10022 Args : List_Id;
10023 Nam : Name_Id;
10025 -- Start of processing for Make_Predicate_Check
10027 begin
10028 -- If predicate checks are suppressed, then return a null statement. For
10029 -- this call, we check only the scope setting. If the caller wants to
10030 -- check a specific entity's setting, they must do it manually.
10032 if Predicate_Checks_Suppressed (Empty) then
10033 return Make_Null_Statement (Loc);
10034 end if;
10036 -- Do not generate a check within stream functions and the like.
10038 if not Predicate_Check_In_Scope (Expr) then
10039 return Make_Null_Statement (Loc);
10040 end if;
10042 -- Compute proper name to use, we need to get this right so that the
10043 -- right set of check policies apply to the Check pragma we are making.
10045 if Has_Dynamic_Predicate_Aspect (Typ) then
10046 Nam := Name_Dynamic_Predicate;
10047 elsif Has_Static_Predicate_Aspect (Typ) then
10048 Nam := Name_Static_Predicate;
10049 else
10050 Nam := Name_Predicate;
10051 end if;
10053 Args := New_List (
10054 Make_Pragma_Argument_Association (Loc,
10055 Expression => Make_Identifier (Loc, Nam)),
10056 Make_Pragma_Argument_Association (Loc,
10057 Expression => Make_Predicate_Call (Typ, Expr)));
10059 -- If the subtype is subject to pragma Predicate_Failure, add the
10060 -- failure expression as an additional parameter.
10062 Add_Failure_Expression (Args);
10064 return
10065 Make_Pragma (Loc,
10066 Chars => Name_Check,
10067 Pragma_Argument_Associations => Args);
10068 end Make_Predicate_Check;
10070 ----------------------------
10071 -- Make_Subtype_From_Expr --
10072 ----------------------------
10074 -- 1. If Expr is an unconstrained array expression, creates
10075 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
10077 -- 2. If Expr is a unconstrained discriminated type expression, creates
10078 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
10080 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
10082 function Make_Subtype_From_Expr
10083 (E : Node_Id;
10084 Unc_Typ : Entity_Id;
10085 Related_Id : Entity_Id := Empty) return Node_Id
10087 List_Constr : constant List_Id := New_List;
10088 Loc : constant Source_Ptr := Sloc (E);
10089 D : Entity_Id;
10090 Full_Exp : Node_Id;
10091 Full_Subtyp : Entity_Id;
10092 High_Bound : Entity_Id;
10093 Index_Typ : Entity_Id;
10094 Low_Bound : Entity_Id;
10095 Priv_Subtyp : Entity_Id;
10096 Utyp : Entity_Id;
10098 begin
10099 if Is_Private_Type (Unc_Typ)
10100 and then Has_Unknown_Discriminants (Unc_Typ)
10101 then
10102 -- The caller requests a unique external name for both the private
10103 -- and the full subtype.
10105 if Present (Related_Id) then
10106 Full_Subtyp :=
10107 Make_Defining_Identifier (Loc,
10108 Chars => New_External_Name (Chars (Related_Id), 'C'));
10109 Priv_Subtyp :=
10110 Make_Defining_Identifier (Loc,
10111 Chars => New_External_Name (Chars (Related_Id), 'P'));
10113 else
10114 Full_Subtyp := Make_Temporary (Loc, 'C');
10115 Priv_Subtyp := Make_Temporary (Loc, 'P');
10116 end if;
10118 -- Prepare the subtype completion. Use the base type to find the
10119 -- underlying type because the type may be a generic actual or an
10120 -- explicit subtype.
10122 Utyp := Underlying_Type (Base_Type (Unc_Typ));
10124 Full_Exp :=
10125 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
10126 Set_Parent (Full_Exp, Parent (E));
10128 Insert_Action (E,
10129 Make_Subtype_Declaration (Loc,
10130 Defining_Identifier => Full_Subtyp,
10131 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
10133 -- Define the dummy private subtype
10135 Mutate_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
10136 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
10137 Set_Scope (Priv_Subtyp, Full_Subtyp);
10138 Set_Is_Constrained (Priv_Subtyp);
10139 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
10140 Set_Is_Itype (Priv_Subtyp);
10141 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
10143 if Is_Tagged_Type (Priv_Subtyp) then
10144 Set_Class_Wide_Type
10145 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
10146 Set_Direct_Primitive_Operations (Priv_Subtyp,
10147 Direct_Primitive_Operations (Unc_Typ));
10148 end if;
10150 Set_Full_View (Priv_Subtyp, Full_Subtyp);
10152 return New_Occurrence_Of (Priv_Subtyp, Loc);
10154 elsif Is_Array_Type (Unc_Typ) then
10155 Index_Typ := First_Index (Unc_Typ);
10156 for J in 1 .. Number_Dimensions (Unc_Typ) loop
10158 -- Capture the bounds of each index constraint in case the context
10159 -- is an object declaration of an unconstrained type initialized
10160 -- by a function call:
10162 -- Obj : Unconstr_Typ := Func_Call;
10164 -- This scenario requires secondary scope management and the index
10165 -- constraint cannot depend on the temporary used to capture the
10166 -- result of the function call.
10168 -- SS_Mark;
10169 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
10170 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
10171 -- Obj : S := Temp.all;
10172 -- SS_Release; -- Temp is gone at this point, bounds of S are
10173 -- -- non existent.
10175 -- Generate:
10176 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
10178 Low_Bound := Make_Temporary (Loc, 'B');
10179 Insert_Action (E,
10180 Make_Object_Declaration (Loc,
10181 Defining_Identifier => Low_Bound,
10182 Object_Definition =>
10183 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10184 Constant_Present => True,
10185 Expression =>
10186 Make_Attribute_Reference (Loc,
10187 Prefix => Duplicate_Subexpr_No_Checks (E),
10188 Attribute_Name => Name_First,
10189 Expressions => New_List (
10190 Make_Integer_Literal (Loc, J)))));
10192 -- Generate:
10193 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
10195 High_Bound := Make_Temporary (Loc, 'B');
10196 Insert_Action (E,
10197 Make_Object_Declaration (Loc,
10198 Defining_Identifier => High_Bound,
10199 Object_Definition =>
10200 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10201 Constant_Present => True,
10202 Expression =>
10203 Make_Attribute_Reference (Loc,
10204 Prefix => Duplicate_Subexpr_No_Checks (E),
10205 Attribute_Name => Name_Last,
10206 Expressions => New_List (
10207 Make_Integer_Literal (Loc, J)))));
10209 Append_To (List_Constr,
10210 Make_Range (Loc,
10211 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
10212 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
10214 Next_Index (Index_Typ);
10215 end loop;
10217 elsif Is_Class_Wide_Type (Unc_Typ) then
10218 declare
10219 CW_Subtype : Entity_Id;
10220 EQ_Typ : Entity_Id := Empty;
10222 begin
10223 -- A class-wide equivalent type is not needed on VM targets
10224 -- because the VM back-ends handle the class-wide object
10225 -- initialization itself (and doesn't need or want the
10226 -- additional intermediate type to handle the assignment).
10228 if Expander_Active and then Tagged_Type_Expansion then
10230 -- If this is the class-wide type of a completion that is a
10231 -- record subtype, set the type of the class-wide type to be
10232 -- the full base type, for use in the expanded code for the
10233 -- equivalent type. Should this be done earlier when the
10234 -- completion is analyzed ???
10236 if Is_Private_Type (Etype (Unc_Typ))
10237 and then
10238 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
10239 then
10240 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
10241 end if;
10243 EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
10244 end if;
10246 CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
10247 Set_Equivalent_Type (CW_Subtype, EQ_Typ);
10248 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
10250 return New_Occurrence_Of (CW_Subtype, Loc);
10251 end;
10253 -- Indefinite record type with discriminants
10255 else
10256 D := First_Discriminant (Unc_Typ);
10257 while Present (D) loop
10258 Append_To (List_Constr,
10259 Make_Selected_Component (Loc,
10260 Prefix => Duplicate_Subexpr_No_Checks (E),
10261 Selector_Name => New_Occurrence_Of (D, Loc)));
10263 Next_Discriminant (D);
10264 end loop;
10265 end if;
10267 return
10268 Make_Subtype_Indication (Loc,
10269 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
10270 Constraint =>
10271 Make_Index_Or_Discriminant_Constraint (Loc,
10272 Constraints => List_Constr));
10273 end Make_Subtype_From_Expr;
10275 -----------------------------
10276 -- Make_Variant_Comparison --
10277 -----------------------------
10279 function Make_Variant_Comparison
10280 (Loc : Source_Ptr;
10281 Mode : Name_Id;
10282 Curr_Val : Node_Id;
10283 Old_Val : Node_Id) return Node_Id
10285 begin
10286 if Mode = Name_Increases then
10287 return Make_Op_Gt (Loc, Curr_Val, Old_Val);
10288 else pragma Assert (Mode = Name_Decreases);
10289 return Make_Op_Lt (Loc, Curr_Val, Old_Val);
10290 end if;
10291 end Make_Variant_Comparison;
10293 -----------------
10294 -- Map_Formals --
10295 -----------------
10297 procedure Map_Formals
10298 (Parent_Subp : Entity_Id;
10299 Derived_Subp : Entity_Id;
10300 Force_Update : Boolean := False)
10302 Par_Formal : Entity_Id := First_Formal (Parent_Subp);
10303 Subp_Formal : Entity_Id := First_Formal (Derived_Subp);
10305 begin
10306 if Force_Update then
10307 Type_Map.Set (Parent_Subp, Derived_Subp);
10308 end if;
10310 -- At this stage either we are under regular processing and the caller
10311 -- has previously ensured that these primitives are already mapped (by
10312 -- means of calling previously to Update_Primitives_Mapping), or we are
10313 -- processing a late-overriding primitive and Force_Update updated above
10314 -- the mapping of these primitives.
10316 while Present (Par_Formal) and then Present (Subp_Formal) loop
10317 Type_Map.Set (Par_Formal, Subp_Formal);
10318 Next_Formal (Par_Formal);
10319 Next_Formal (Subp_Formal);
10320 end loop;
10321 end Map_Formals;
10323 ---------------
10324 -- Map_Types --
10325 ---------------
10327 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
10329 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
10330 -- avoid deep indentation of code.
10332 -- NOTE: Routines which deal with discriminant mapping operate on the
10333 -- [underlying/record] full view of various types because those views
10334 -- contain all discriminants and stored constraints.
10336 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
10337 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
10338 -- overriding chain starting from Prim whose dispatching type is parent
10339 -- type Par_Typ and add a mapping between the result and primitive Prim.
10341 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
10342 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
10343 -- the inheritance or overriding chain of subprogram Subp. Return Empty
10344 -- if no such primitive is available.
10346 function Build_Chain
10347 (Par_Typ : Entity_Id;
10348 Deriv_Typ : Entity_Id) return Elist_Id;
10349 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
10350 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
10351 -- list has the form:
10353 -- head tail
10354 -- v v
10355 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
10357 -- Note that Par_Typ is not part of the resulting derivation chain
10359 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
10360 -- Return the view of type Typ which could potentially contains either
10361 -- the discriminants or stored constraints of the type.
10363 function Find_Discriminant_Value
10364 (Discr : Entity_Id;
10365 Par_Typ : Entity_Id;
10366 Deriv_Typ : Entity_Id;
10367 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
10368 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
10369 -- in the derivation chain starting from parent type Par_Typ leading to
10370 -- derived type Deriv_Typ. The returned value is one of the following:
10372 -- * An entity which is either a discriminant or a nondiscriminant
10373 -- name, and renames/constraints Discr.
10375 -- * An expression which constraints Discr
10377 -- Typ_Elmt is an element of the derivation chain created by routine
10378 -- Build_Chain and denotes the current ancestor being examined.
10380 procedure Map_Discriminants
10381 (Par_Typ : Entity_Id;
10382 Deriv_Typ : Entity_Id);
10383 -- Map each discriminant of type Par_Typ to a meaningful constraint
10384 -- from the point of view of type Deriv_Typ.
10386 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
10387 -- Map each primitive of type Par_Typ to a corresponding primitive of
10388 -- type Deriv_Typ.
10390 -------------------
10391 -- Add_Primitive --
10392 -------------------
10394 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
10395 Par_Prim : Entity_Id;
10397 begin
10398 -- Inspect the inheritance chain through the Alias attribute and the
10399 -- overriding chain through the Overridden_Operation looking for an
10400 -- ancestor primitive with the appropriate dispatching type.
10402 Par_Prim := Prim;
10403 while Present (Par_Prim) loop
10404 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
10405 Par_Prim := Ancestor_Primitive (Par_Prim);
10406 end loop;
10408 -- Create a mapping of the form:
10410 -- parent type primitive -> derived type primitive
10412 if Present (Par_Prim) then
10413 Type_Map.Set (Par_Prim, Prim);
10414 end if;
10415 end Add_Primitive;
10417 ------------------------
10418 -- Ancestor_Primitive --
10419 ------------------------
10421 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
10422 Inher_Prim : constant Entity_Id := Alias (Subp);
10423 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
10425 begin
10426 -- The current subprogram overrides an ancestor primitive
10428 if Present (Over_Prim) then
10429 return Over_Prim;
10431 -- The current subprogram is an internally generated alias of an
10432 -- inherited ancestor primitive.
10434 elsif Present (Inher_Prim) then
10435 -- It is possible that an internally generated alias could be
10436 -- set to a subprogram which overrides the same aliased primitive,
10437 -- so return Empty in this case.
10439 if Ancestor_Primitive (Inher_Prim) = Subp then
10440 return Empty;
10441 end if;
10443 return Inher_Prim;
10445 -- Otherwise the current subprogram is the root of the inheritance or
10446 -- overriding chain.
10448 else
10449 return Empty;
10450 end if;
10451 end Ancestor_Primitive;
10453 -----------------
10454 -- Build_Chain --
10455 -----------------
10457 function Build_Chain
10458 (Par_Typ : Entity_Id;
10459 Deriv_Typ : Entity_Id) return Elist_Id
10461 Anc_Typ : Entity_Id;
10462 Chain : Elist_Id;
10463 Curr_Typ : Entity_Id;
10465 begin
10466 Chain := New_Elmt_List;
10468 -- Add the derived type to the derivation chain
10470 Prepend_Elmt (Deriv_Typ, Chain);
10472 -- Examine all ancestors starting from the derived type climbing
10473 -- towards parent type Par_Typ.
10475 Curr_Typ := Deriv_Typ;
10476 loop
10477 -- Handle the case where the current type is a record which
10478 -- derives from a subtype.
10480 -- subtype Sub_Typ is Par_Typ ...
10481 -- type Deriv_Typ is Sub_Typ ...
10483 if Ekind (Curr_Typ) = E_Record_Type
10484 and then Present (Parent_Subtype (Curr_Typ))
10485 then
10486 Anc_Typ := Parent_Subtype (Curr_Typ);
10488 -- Handle the case where the current type is a record subtype of
10489 -- another subtype.
10491 -- subtype Sub_Typ1 is Par_Typ ...
10492 -- subtype Sub_Typ2 is Sub_Typ1 ...
10494 elsif Ekind (Curr_Typ) = E_Record_Subtype
10495 and then Present (Cloned_Subtype (Curr_Typ))
10496 then
10497 Anc_Typ := Cloned_Subtype (Curr_Typ);
10499 -- Otherwise use the direct parent type
10501 else
10502 Anc_Typ := Etype (Curr_Typ);
10503 end if;
10505 -- Use the first subtype when dealing with itypes
10507 if Is_Itype (Anc_Typ) then
10508 Anc_Typ := First_Subtype (Anc_Typ);
10509 end if;
10511 -- Work with the view which contains the discriminants and stored
10512 -- constraints.
10514 Anc_Typ := Discriminated_View (Anc_Typ);
10516 -- Stop the climb when either the parent type has been reached or
10517 -- there are no more ancestors left to examine.
10519 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
10521 Prepend_Unique_Elmt (Anc_Typ, Chain);
10522 Curr_Typ := Anc_Typ;
10523 end loop;
10525 return Chain;
10526 end Build_Chain;
10528 ------------------------
10529 -- Discriminated_View --
10530 ------------------------
10532 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
10533 T : Entity_Id;
10535 begin
10536 T := Typ;
10538 -- Use the [underlying] full view when dealing with private types
10539 -- because the view contains all inherited discriminants or stored
10540 -- constraints.
10542 if Is_Private_Type (T) then
10543 if Present (Underlying_Full_View (T)) then
10544 T := Underlying_Full_View (T);
10546 elsif Present (Full_View (T)) then
10547 T := Full_View (T);
10548 end if;
10549 end if;
10551 -- Use the underlying record view when the type is an extenstion of
10552 -- a parent type with unknown discriminants because the view contains
10553 -- all inherited discriminants or stored constraints.
10555 if Ekind (T) = E_Record_Type
10556 and then Present (Underlying_Record_View (T))
10557 then
10558 T := Underlying_Record_View (T);
10559 end if;
10561 return T;
10562 end Discriminated_View;
10564 -----------------------------
10565 -- Find_Discriminant_Value --
10566 -----------------------------
10568 function Find_Discriminant_Value
10569 (Discr : Entity_Id;
10570 Par_Typ : Entity_Id;
10571 Deriv_Typ : Entity_Id;
10572 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
10574 Discr_Pos : constant Uint := Discriminant_Number (Discr);
10575 Typ : constant Entity_Id := Node (Typ_Elmt);
10577 function Find_Constraint_Value
10578 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
10579 -- Given constraint Constr, find what it denotes. This is either:
10581 -- * An entity which is either a discriminant or a name
10583 -- * An expression
10585 ---------------------------
10586 -- Find_Constraint_Value --
10587 ---------------------------
10589 function Find_Constraint_Value
10590 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
10592 begin
10593 if Nkind (Constr) in N_Entity then
10595 -- The constraint denotes a discriminant of the curren type
10596 -- which renames the ancestor discriminant:
10598 -- vv
10599 -- type Typ (D1 : ...; DN : ...) is
10600 -- new Anc (Discr => D1) with ...
10601 -- ^^
10603 if Ekind (Constr) = E_Discriminant then
10605 -- The discriminant belongs to derived type Deriv_Typ. This
10606 -- is the final value for the ancestor discriminant as the
10607 -- derivations chain has been fully exhausted.
10609 if Typ = Deriv_Typ then
10610 return Constr;
10612 -- Otherwise the discriminant may be renamed or constrained
10613 -- at a lower level. Continue looking down the derivation
10614 -- chain.
10616 else
10617 return
10618 Find_Discriminant_Value
10619 (Discr => Constr,
10620 Par_Typ => Par_Typ,
10621 Deriv_Typ => Deriv_Typ,
10622 Typ_Elmt => Next_Elmt (Typ_Elmt));
10623 end if;
10625 -- Otherwise the constraint denotes a reference to some name
10626 -- which results in a Stored discriminant:
10628 -- vvvv
10629 -- Name : ...;
10630 -- type Typ (D1 : ...; DN : ...) is
10631 -- new Anc (Discr => Name) with ...
10632 -- ^^^^
10634 -- Return the name as this is the proper constraint of the
10635 -- discriminant.
10637 else
10638 return Constr;
10639 end if;
10641 -- The constraint denotes a reference to a name
10643 elsif Is_Entity_Name (Constr) then
10644 return Find_Constraint_Value (Entity (Constr));
10646 -- Otherwise the current constraint is an expression which yields
10647 -- a Stored discriminant:
10649 -- type Typ (D1 : ...; DN : ...) is
10650 -- new Anc (Discr => <expression>) with ...
10651 -- ^^^^^^^^^^
10653 -- Return the expression as this is the proper constraint of the
10654 -- discriminant.
10656 else
10657 return Constr;
10658 end if;
10659 end Find_Constraint_Value;
10661 -- Local variables
10663 Constrs : constant Elist_Id := Stored_Constraint (Typ);
10665 Constr_Elmt : Elmt_Id;
10666 Pos : Uint;
10667 Typ_Discr : Entity_Id;
10669 -- Start of processing for Find_Discriminant_Value
10671 begin
10672 -- The algorithm for finding the value of a discriminant works as
10673 -- follows. First, it recreates the derivation chain from Par_Typ
10674 -- to Deriv_Typ as a list:
10676 -- Par_Typ (shown for completeness)
10677 -- v
10678 -- Ancestor_N <-- head of chain
10679 -- v
10680 -- Ancestor_1
10681 -- v
10682 -- Deriv_Typ <-- tail of chain
10684 -- The algorithm then traces the fate of a parent discriminant down
10685 -- the derivation chain. At each derivation level, the discriminant
10686 -- may be either inherited or constrained.
10688 -- 1) Discriminant is inherited: there are two cases, depending on
10689 -- which type is inheriting.
10691 -- 1.1) Deriv_Typ is inheriting:
10693 -- type Ancestor (D_1 : ...) is tagged ...
10694 -- type Deriv_Typ is new Ancestor ...
10696 -- In this case the inherited discriminant is the final value of
10697 -- the parent discriminant because the end of the derivation chain
10698 -- has been reached.
10700 -- 1.2) Some other type is inheriting:
10702 -- type Ancestor_1 (D_1 : ...) is tagged ...
10703 -- type Ancestor_2 is new Ancestor_1 ...
10705 -- In this case the algorithm continues to trace the fate of the
10706 -- inherited discriminant down the derivation chain because it may
10707 -- be further inherited or constrained.
10709 -- 2) Discriminant is constrained: there are three cases, depending
10710 -- on what the constraint is.
10712 -- 2.1) The constraint is another discriminant (aka renaming):
10714 -- type Ancestor_1 (D_1 : ...) is tagged ...
10715 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10717 -- In this case the constraining discriminant becomes the one to
10718 -- track down the derivation chain. The algorithm already knows
10719 -- that D_2 constrains D_1, therefore if the algorithm finds the
10720 -- value of D_2, then this would also be the value for D_1.
10722 -- 2.2) The constraint is a name (aka Stored):
10724 -- Name : ...
10725 -- type Ancestor_1 (D_1 : ...) is tagged ...
10726 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10728 -- In this case the name is the final value of D_1 because the
10729 -- discriminant cannot be further constrained.
10731 -- 2.3) The constraint is an expression (aka Stored):
10733 -- type Ancestor_1 (D_1 : ...) is tagged ...
10734 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10736 -- Similar to 2.2, the expression is the final value of D_1
10738 Pos := Uint_1;
10740 -- When a derived type constrains its parent type, all constaints
10741 -- appear in the Stored_Constraint list. Examine the list looking
10742 -- for a positional match.
10744 if Present (Constrs) then
10745 Constr_Elmt := First_Elmt (Constrs);
10746 while Present (Constr_Elmt) loop
10748 -- The position of the current constraint matches that of the
10749 -- ancestor discriminant.
10751 if Pos = Discr_Pos then
10752 return Find_Constraint_Value (Node (Constr_Elmt));
10753 end if;
10755 Next_Elmt (Constr_Elmt);
10756 Pos := Pos + 1;
10757 end loop;
10759 -- Otherwise the derived type does not constraint its parent type in
10760 -- which case it inherits the parent discriminants.
10762 else
10763 Typ_Discr := First_Discriminant (Typ);
10764 while Present (Typ_Discr) loop
10766 -- The position of the current discriminant matches that of the
10767 -- ancestor discriminant.
10769 if Pos = Discr_Pos then
10770 return Find_Constraint_Value (Typ_Discr);
10771 end if;
10773 Next_Discriminant (Typ_Discr);
10774 Pos := Pos + 1;
10775 end loop;
10776 end if;
10778 -- A discriminant must always have a corresponding value. This is
10779 -- either another discriminant, a name, or an expression. If this
10780 -- point is reached, them most likely the derivation chain employs
10781 -- the wrong views of types.
10783 pragma Assert (False);
10785 return Empty;
10786 end Find_Discriminant_Value;
10788 -----------------------
10789 -- Map_Discriminants --
10790 -----------------------
10792 procedure Map_Discriminants
10793 (Par_Typ : Entity_Id;
10794 Deriv_Typ : Entity_Id)
10796 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10798 Discr : Entity_Id;
10799 Discr_Val : Node_Or_Entity_Id;
10801 begin
10802 -- Examine each discriminant of parent type Par_Typ and find a
10803 -- suitable value for it from the point of view of derived type
10804 -- Deriv_Typ.
10806 if Has_Discriminants (Par_Typ) then
10807 Discr := First_Discriminant (Par_Typ);
10808 while Present (Discr) loop
10809 Discr_Val :=
10810 Find_Discriminant_Value
10811 (Discr => Discr,
10812 Par_Typ => Par_Typ,
10813 Deriv_Typ => Deriv_Typ,
10814 Typ_Elmt => First_Elmt (Deriv_Chain));
10816 -- Create a mapping of the form:
10818 -- parent type discriminant -> value
10820 Type_Map.Set (Discr, Discr_Val);
10822 Next_Discriminant (Discr);
10823 end loop;
10824 end if;
10825 end Map_Discriminants;
10827 --------------------
10828 -- Map_Primitives --
10829 --------------------
10831 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10832 Deriv_Prim : Entity_Id;
10833 Par_Prim : Entity_Id;
10834 Par_Prims : Elist_Id;
10835 Prim_Elmt : Elmt_Id;
10837 begin
10838 -- Inspect the primitives of the derived type and determine whether
10839 -- they relate to the primitives of the parent type. If there is a
10840 -- meaningful relation, create a mapping of the form:
10842 -- parent type primitive -> derived type primitive
10844 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10845 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10846 while Present (Prim_Elmt) loop
10847 Deriv_Prim := Node (Prim_Elmt);
10849 if Is_Subprogram (Deriv_Prim)
10850 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10851 then
10852 Add_Primitive (Deriv_Prim, Par_Typ);
10853 end if;
10855 Next_Elmt (Prim_Elmt);
10856 end loop;
10857 end if;
10859 -- If the parent operation is an interface operation, the overriding
10860 -- indicator is not present. Instead, we get from the interface
10861 -- operation the primitive of the current type that implements it.
10863 if Is_Interface (Par_Typ) then
10864 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10866 if Present (Par_Prims) then
10867 Prim_Elmt := First_Elmt (Par_Prims);
10869 while Present (Prim_Elmt) loop
10870 Par_Prim := Node (Prim_Elmt);
10871 Deriv_Prim :=
10872 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10874 if Present (Deriv_Prim) then
10875 Type_Map.Set (Par_Prim, Deriv_Prim);
10876 end if;
10878 Next_Elmt (Prim_Elmt);
10879 end loop;
10880 end if;
10881 end if;
10882 end Map_Primitives;
10884 -- Start of processing for Map_Types
10886 begin
10887 -- Nothing to do if there are no types to work with
10889 if No (Parent_Type) or else No (Derived_Type) then
10890 return;
10892 -- Nothing to do if the mapping already exists
10894 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10895 return;
10897 -- Nothing to do if both types are not tagged. Note that untagged types
10898 -- do not have primitive operations and their discriminants are already
10899 -- handled by gigi.
10901 elsif not Is_Tagged_Type (Parent_Type)
10902 or else not Is_Tagged_Type (Derived_Type)
10903 then
10904 return;
10905 end if;
10907 -- Create a mapping of the form
10909 -- parent type -> derived type
10911 -- to prevent any subsequent attempts to produce the same relations
10913 Type_Map.Set (Parent_Type, Derived_Type);
10915 -- Create mappings of the form
10917 -- parent type discriminant -> derived type discriminant
10918 -- <or>
10919 -- parent type discriminant -> constraint
10921 -- Note that mapping of discriminants breaks privacy because it needs to
10922 -- work with those views which contains the discriminants and any stored
10923 -- constraints.
10925 Map_Discriminants
10926 (Par_Typ => Discriminated_View (Parent_Type),
10927 Deriv_Typ => Discriminated_View (Derived_Type));
10929 -- Create mappings of the form
10931 -- parent type primitive -> derived type primitive
10933 Map_Primitives
10934 (Par_Typ => Parent_Type,
10935 Deriv_Typ => Derived_Type);
10936 end Map_Types;
10938 ----------------------------
10939 -- Matching_Standard_Type --
10940 ----------------------------
10942 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10943 pragma Assert (Is_Scalar_Type (Typ));
10944 Siz : constant Uint := Esize (Typ);
10946 begin
10947 -- Floating-point cases
10949 if Is_Floating_Point_Type (Typ) then
10950 if Siz <= Esize (Standard_Short_Float) then
10951 return Standard_Short_Float;
10952 elsif Siz <= Esize (Standard_Float) then
10953 return Standard_Float;
10954 elsif Siz <= Esize (Standard_Long_Float) then
10955 return Standard_Long_Float;
10956 elsif Siz <= Esize (Standard_Long_Long_Float) then
10957 return Standard_Long_Long_Float;
10958 else
10959 raise Program_Error;
10960 end if;
10962 -- Integer cases (includes fixed-point types)
10964 -- Unsigned integer cases (includes normal enumeration types)
10966 else
10967 return Small_Integer_Type_For (Siz, Is_Unsigned_Type (Typ));
10968 end if;
10969 end Matching_Standard_Type;
10971 -----------------------------
10972 -- May_Generate_Large_Temp --
10973 -----------------------------
10975 -- At the current time, the only types that we return False for (i.e. where
10976 -- we decide we know they cannot generate large temps) are ones where we
10977 -- know the size is 256 bits or less at compile time, and we are still not
10978 -- doing a thorough job on arrays and records.
10980 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10981 begin
10982 if not Size_Known_At_Compile_Time (Typ) then
10983 return False;
10984 end if;
10986 if Known_Esize (Typ) and then Esize (Typ) <= 256 then
10987 return False;
10988 end if;
10990 if Is_Array_Type (Typ)
10991 and then Present (Packed_Array_Impl_Type (Typ))
10992 then
10993 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10994 end if;
10996 return True;
10997 end May_Generate_Large_Temp;
10999 --------------------------------------------
11000 -- Needs_Conditional_Null_Excluding_Check --
11001 --------------------------------------------
11003 function Needs_Conditional_Null_Excluding_Check
11004 (Typ : Entity_Id) return Boolean
11006 begin
11007 return
11008 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
11009 end Needs_Conditional_Null_Excluding_Check;
11011 ----------------------------
11012 -- Needs_Constant_Address --
11013 ----------------------------
11015 function Needs_Constant_Address
11016 (Decl : Node_Id;
11017 Typ : Entity_Id) return Boolean
11019 begin
11020 -- If we have no initialization of any kind, then we don't need to place
11021 -- any restrictions on the address clause, because the object will be
11022 -- elaborated after the address clause is evaluated. This happens if the
11023 -- declaration has no initial expression, or the type has no implicit
11024 -- initialization, or the object is imported.
11026 -- The same holds for all initialized scalar types and all access types.
11027 -- Packed bit array types of size up to the maximum integer size are
11028 -- represented using a modular type with an initialization (to zero) and
11029 -- can be processed like other initialized scalar types.
11031 -- If the type is controlled, code to attach the object to a
11032 -- finalization chain is generated at the point of declaration, and
11033 -- therefore the elaboration of the object cannot be delayed: the
11034 -- address expression must be a constant.
11036 if No (Expression (Decl))
11037 and then not Needs_Finalization (Typ)
11038 and then
11039 (not Has_Non_Null_Base_Init_Proc (Typ)
11040 or else Is_Imported (Defining_Identifier (Decl)))
11041 then
11042 return False;
11044 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
11045 or else Is_Access_Type (Typ)
11046 or else
11047 (Is_Bit_Packed_Array (Typ)
11048 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
11049 then
11050 return False;
11052 else
11053 -- Otherwise, we require the address clause to be constant because
11054 -- the call to the initialization procedure (or the attach code) has
11055 -- to happen at the point of the declaration.
11057 -- Actually the IP call has been moved to the freeze actions anyway,
11058 -- so maybe we can relax this restriction???
11060 return True;
11061 end if;
11062 end Needs_Constant_Address;
11064 ----------------------------
11065 -- New_Class_Wide_Subtype --
11066 ----------------------------
11068 function New_Class_Wide_Subtype
11069 (CW_Typ : Entity_Id;
11070 N : Node_Id) return Entity_Id
11072 Res : constant Entity_Id := Create_Itype (E_Void, N);
11074 -- Capture relevant attributes of the class-wide subtype which must be
11075 -- restored after the copy.
11077 Res_Chars : constant Name_Id := Chars (Res);
11078 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
11079 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
11080 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
11081 Res_Scope : constant Entity_Id := Scope (Res);
11083 begin
11084 Copy_Node (CW_Typ, Res);
11086 -- Restore the relevant attributes of the class-wide subtype
11088 Set_Chars (Res, Res_Chars);
11089 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
11090 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
11091 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
11092 Set_Scope (Res, Res_Scope);
11094 -- Decorate the class-wide subtype
11096 Set_Associated_Node_For_Itype (Res, N);
11097 Set_Comes_From_Source (Res, False);
11098 Mutate_Ekind (Res, E_Class_Wide_Subtype);
11099 Set_Etype (Res, Base_Type (CW_Typ));
11100 Set_Freeze_Node (Res, Empty);
11101 Set_Is_Frozen (Res, False);
11102 Set_Is_Itype (Res);
11103 Set_Is_Public (Res, False);
11104 Set_Next_Entity (Res, Empty);
11105 Set_Prev_Entity (Res, Empty);
11106 Set_Sloc (Res, Sloc (N));
11108 Set_Public_Status (Res);
11110 return Res;
11111 end New_Class_Wide_Subtype;
11113 -----------------------------------
11114 -- OK_To_Do_Constant_Replacement --
11115 -----------------------------------
11117 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
11118 ES : constant Entity_Id := Scope (E);
11119 CS : Entity_Id;
11121 begin
11122 -- Do not replace statically allocated objects, because they may be
11123 -- modified outside the current scope.
11125 if Is_Statically_Allocated (E) then
11126 return False;
11128 -- Do not replace aliased or volatile objects, since we don't know what
11129 -- else might change the value.
11131 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
11132 return False;
11134 -- Debug flag -gnatdM disconnects this optimization
11136 elsif Debug_Flag_MM then
11137 return False;
11139 -- Otherwise check scopes
11141 else
11142 CS := Current_Scope;
11144 loop
11145 -- If we are in right scope, replacement is safe
11147 if CS = ES then
11148 return True;
11150 -- Packages do not affect the determination of safety
11152 elsif Ekind (CS) = E_Package then
11153 exit when CS = Standard_Standard;
11154 CS := Scope (CS);
11156 -- Blocks do not affect the determination of safety
11158 elsif Ekind (CS) = E_Block then
11159 CS := Scope (CS);
11161 -- Loops do not affect the determination of safety. Note that we
11162 -- kill all current values on entry to a loop, so we are just
11163 -- talking about processing within a loop here.
11165 elsif Ekind (CS) = E_Loop then
11166 CS := Scope (CS);
11168 -- Otherwise, the reference is dubious, and we cannot be sure that
11169 -- it is safe to do the replacement.
11171 else
11172 exit;
11173 end if;
11174 end loop;
11176 return False;
11177 end if;
11178 end OK_To_Do_Constant_Replacement;
11180 ------------------------------------
11181 -- Possible_Bit_Aligned_Component --
11182 ------------------------------------
11184 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
11185 begin
11186 -- Do not process an unanalyzed node because it is not yet decorated and
11187 -- most checks performed below will fail.
11189 if not Analyzed (N) then
11190 return False;
11191 end if;
11193 -- There are never alignment issues in CodePeer mode
11195 if CodePeer_Mode then
11196 return False;
11197 end if;
11199 case Nkind (N) is
11201 -- Case of indexed component
11203 when N_Indexed_Component =>
11204 declare
11205 P : constant Node_Id := Prefix (N);
11206 Ptyp : constant Entity_Id := Etype (P);
11208 begin
11209 -- If we know the component size and it is not larger than the
11210 -- maximum integer size, then we are OK. The back end does the
11211 -- assignment of small misaligned objects correctly.
11213 if Known_Static_Component_Size (Ptyp)
11214 and then Component_Size (Ptyp) <= System_Max_Integer_Size
11215 then
11216 return False;
11218 -- Otherwise, we need to test the prefix, to see if we are
11219 -- indexing from a possibly unaligned component.
11221 else
11222 return Possible_Bit_Aligned_Component (P);
11223 end if;
11224 end;
11226 -- Case of selected component
11228 when N_Selected_Component =>
11229 declare
11230 P : constant Node_Id := Prefix (N);
11231 Comp : constant Entity_Id := Entity (Selector_Name (N));
11233 begin
11234 -- This is the crucial test: if the component itself causes
11235 -- trouble, then we can stop and return True.
11237 if Component_May_Be_Bit_Aligned (Comp) then
11238 return True;
11240 -- Otherwise, we need to test the prefix, to see if we are
11241 -- selecting from a possibly unaligned component.
11243 else
11244 return Possible_Bit_Aligned_Component (P);
11245 end if;
11246 end;
11248 -- For a slice, test the prefix, if that is possibly misaligned,
11249 -- then for sure the slice is.
11251 when N_Slice =>
11252 return Possible_Bit_Aligned_Component (Prefix (N));
11254 -- For an unchecked conversion, check whether the expression may
11255 -- be bit aligned.
11257 when N_Unchecked_Type_Conversion =>
11258 return Possible_Bit_Aligned_Component (Expression (N));
11260 -- If we have none of the above, it means that we have fallen off the
11261 -- top testing prefixes recursively, and we now have a stand alone
11262 -- object, where we don't have a problem, unless this is a renaming,
11263 -- in which case we need to look into the renamed object.
11265 when others =>
11266 if Is_Entity_Name (N)
11267 and then Is_Object (Entity (N))
11268 and then Present (Renamed_Object (Entity (N)))
11269 then
11270 return
11271 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
11272 else
11273 return False;
11274 end if;
11275 end case;
11276 end Possible_Bit_Aligned_Component;
11278 -----------------------------------------------
11279 -- Process_Statements_For_Controlled_Objects --
11280 -----------------------------------------------
11282 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
11283 Loc : constant Source_Ptr := Sloc (N);
11285 function Are_Wrapped (L : List_Id) return Boolean;
11286 -- Determine whether list L contains only one statement which is a block
11288 function Wrap_Statements_In_Block
11289 (L : List_Id;
11290 Scop : Entity_Id := Current_Scope) return Node_Id;
11291 -- Given a list of statements L, wrap it in a block statement and return
11292 -- the generated node. Scop is either the current scope or the scope of
11293 -- the context (if applicable).
11295 -----------------
11296 -- Are_Wrapped --
11297 -----------------
11299 function Are_Wrapped (L : List_Id) return Boolean is
11300 Stmt : constant Node_Id := First (L);
11301 begin
11302 return
11303 Present (Stmt)
11304 and then No (Next (Stmt))
11305 and then Nkind (Stmt) = N_Block_Statement;
11306 end Are_Wrapped;
11308 ------------------------------
11309 -- Wrap_Statements_In_Block --
11310 ------------------------------
11312 function Wrap_Statements_In_Block
11313 (L : List_Id;
11314 Scop : Entity_Id := Current_Scope) return Node_Id
11316 Block_Id : Entity_Id;
11317 Block_Nod : Node_Id;
11318 Iter_Loop : Entity_Id;
11320 begin
11321 Block_Nod :=
11322 Make_Block_Statement (Loc,
11323 Declarations => No_List,
11324 Handled_Statement_Sequence =>
11325 Make_Handled_Sequence_Of_Statements (Loc,
11326 Statements => L));
11328 -- Create a label for the block in case the block needs to manage the
11329 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
11331 Add_Block_Identifier (Block_Nod, Block_Id);
11333 -- When wrapping the statements of an iterator loop, check whether
11334 -- the loop requires secondary stack management and if so, propagate
11335 -- the appropriate flags to the block. This ensures that the cursor
11336 -- is properly cleaned up at each iteration of the loop.
11338 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
11340 if Present (Iter_Loop) then
11341 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
11343 -- Secondary stack reclamation is suppressed when the associated
11344 -- iterator loop contains a return statement which uses the stack.
11346 Set_Sec_Stack_Needed_For_Return
11347 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
11348 end if;
11350 return Block_Nod;
11351 end Wrap_Statements_In_Block;
11353 -- Local variables
11355 Block : Node_Id;
11357 -- Start of processing for Process_Statements_For_Controlled_Objects
11359 begin
11360 -- Whenever a non-handled statement list is wrapped in a block, the
11361 -- block must be explicitly analyzed to redecorate all entities in the
11362 -- list and ensure that a finalizer is properly built.
11364 case Nkind (N) is
11365 when N_Conditional_Entry_Call
11366 | N_Elsif_Part
11367 | N_If_Statement
11368 | N_Selective_Accept
11370 -- Check the "then statements" for elsif parts and if statements
11372 if Nkind (N) in N_Elsif_Part | N_If_Statement
11373 and then not Is_Empty_List (Then_Statements (N))
11374 and then not Are_Wrapped (Then_Statements (N))
11375 and then Requires_Cleanup_Actions
11376 (L => Then_Statements (N),
11377 Lib_Level => False,
11378 Nested_Constructs => False)
11379 then
11380 Block := Wrap_Statements_In_Block (Then_Statements (N));
11381 Set_Then_Statements (N, New_List (Block));
11383 Analyze (Block);
11384 end if;
11386 -- Check the "else statements" for conditional entry calls, if
11387 -- statements and selective accepts.
11389 if Nkind (N) in
11390 N_Conditional_Entry_Call | N_If_Statement | N_Selective_Accept
11391 and then not Is_Empty_List (Else_Statements (N))
11392 and then not Are_Wrapped (Else_Statements (N))
11393 and then Requires_Cleanup_Actions
11394 (L => Else_Statements (N),
11395 Lib_Level => False,
11396 Nested_Constructs => False)
11397 then
11398 Block := Wrap_Statements_In_Block (Else_Statements (N));
11399 Set_Else_Statements (N, New_List (Block));
11401 Analyze (Block);
11402 end if;
11404 when N_Abortable_Part
11405 | N_Accept_Alternative
11406 | N_Case_Statement_Alternative
11407 | N_Delay_Alternative
11408 | N_Entry_Call_Alternative
11409 | N_Exception_Handler
11410 | N_Loop_Statement
11411 | N_Triggering_Alternative
11413 if not Is_Empty_List (Statements (N))
11414 and then not Are_Wrapped (Statements (N))
11415 and then Requires_Cleanup_Actions
11416 (L => Statements (N),
11417 Lib_Level => False,
11418 Nested_Constructs => False)
11419 then
11420 if Nkind (N) = N_Loop_Statement
11421 and then Present (Identifier (N))
11422 then
11423 Block :=
11424 Wrap_Statements_In_Block
11425 (L => Statements (N),
11426 Scop => Entity (Identifier (N)));
11427 else
11428 Block := Wrap_Statements_In_Block (Statements (N));
11429 end if;
11431 Set_Statements (N, New_List (Block));
11432 Analyze (Block);
11433 end if;
11435 -- Could be e.g. a loop that was transformed into a block or null
11436 -- statement. Do nothing for terminate alternatives.
11438 when N_Block_Statement
11439 | N_Null_Statement
11440 | N_Terminate_Alternative
11442 null;
11444 when others =>
11445 raise Program_Error;
11446 end case;
11447 end Process_Statements_For_Controlled_Objects;
11449 ------------------
11450 -- Power_Of_Two --
11451 ------------------
11453 function Power_Of_Two (N : Node_Id) return Nat is
11454 Typ : constant Entity_Id := Etype (N);
11455 pragma Assert (Is_Integer_Type (Typ));
11457 Siz : constant Nat := UI_To_Int (Esize (Typ));
11458 Val : Uint;
11460 begin
11461 if not Compile_Time_Known_Value (N) then
11462 return 0;
11464 else
11465 Val := Expr_Value (N);
11466 for J in 1 .. Siz - 1 loop
11467 if Val = Uint_2 ** J then
11468 return J;
11469 end if;
11470 end loop;
11472 return 0;
11473 end if;
11474 end Power_Of_Two;
11476 ----------------------
11477 -- Remove_Init_Call --
11478 ----------------------
11480 function Remove_Init_Call
11481 (Var : Entity_Id;
11482 Rep_Clause : Node_Id) return Node_Id
11484 Par : constant Node_Id := Parent (Var);
11485 Typ : constant Entity_Id := Etype (Var);
11487 Init_Proc : Entity_Id;
11488 -- Initialization procedure for Typ
11490 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
11491 -- Look for init call for Var starting at From and scanning the
11492 -- enclosing list until Rep_Clause or the end of the list is reached.
11494 ----------------------------
11495 -- Find_Init_Call_In_List --
11496 ----------------------------
11498 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
11499 Init_Call : Node_Id;
11501 begin
11502 Init_Call := From;
11503 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
11504 if Nkind (Init_Call) = N_Procedure_Call_Statement
11505 and then Is_Entity_Name (Name (Init_Call))
11506 and then Entity (Name (Init_Call)) = Init_Proc
11507 then
11508 return Init_Call;
11509 end if;
11511 Next (Init_Call);
11512 end loop;
11514 return Empty;
11515 end Find_Init_Call_In_List;
11517 Init_Call : Node_Id;
11519 -- Start of processing for Remove_Init_Call
11521 begin
11522 if Present (Initialization_Statements (Var)) then
11523 Init_Call := Initialization_Statements (Var);
11524 Set_Initialization_Statements (Var, Empty);
11526 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
11528 -- No init proc for the type, so obviously no call to be found
11530 return Empty;
11532 else
11533 -- We might be able to handle other cases below by just properly
11534 -- setting Initialization_Statements at the point where the init proc
11535 -- call is generated???
11537 Init_Proc := Base_Init_Proc (Typ);
11539 -- First scan the list containing the declaration of Var
11541 Init_Call := Find_Init_Call_In_List (From => Next (Par));
11543 -- If not found, also look on Var's freeze actions list, if any,
11544 -- since the init call may have been moved there (case of an address
11545 -- clause applying to Var).
11547 if No (Init_Call) and then Present (Freeze_Node (Var)) then
11548 Init_Call :=
11549 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
11550 end if;
11552 -- If the initialization call has actuals that use the secondary
11553 -- stack, the call may have been wrapped into a temporary block, in
11554 -- which case the block itself has to be removed.
11556 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
11557 declare
11558 Blk : constant Node_Id := Next (Par);
11559 begin
11560 if Present
11561 (Find_Init_Call_In_List
11562 (First (Statements (Handled_Statement_Sequence (Blk)))))
11563 then
11564 Init_Call := Blk;
11565 end if;
11566 end;
11567 end if;
11568 end if;
11570 if Present (Init_Call) then
11571 -- If restrictions have forbidden Aborts, the initialization call
11572 -- for objects that require deep initialization has not been wrapped
11573 -- into the following block (see Exp_Ch3, Default_Initialize_Object)
11574 -- so if present remove it as well, and include the IP call in it,
11575 -- in the rare case the caller may need to simply displace the
11576 -- initialization, as is done for a later address specification.
11578 if Nkind (Next (Init_Call)) = N_Block_Statement
11579 and then Is_Initialization_Block (Next (Init_Call))
11580 then
11581 declare
11582 IP_Call : constant Node_Id := Init_Call;
11583 begin
11584 Init_Call := Next (IP_Call);
11585 Remove (IP_Call);
11586 Prepend (IP_Call,
11587 Statements (Handled_Statement_Sequence (Init_Call)));
11588 end;
11589 end if;
11591 Remove (Init_Call);
11592 end if;
11594 return Init_Call;
11595 end Remove_Init_Call;
11597 -------------------------
11598 -- Remove_Side_Effects --
11599 -------------------------
11601 procedure Remove_Side_Effects
11602 (Exp : Node_Id;
11603 Name_Req : Boolean := False;
11604 Renaming_Req : Boolean := False;
11605 Variable_Ref : Boolean := False;
11606 Related_Id : Entity_Id := Empty;
11607 Is_Low_Bound : Boolean := False;
11608 Is_High_Bound : Boolean := False;
11609 Discr_Number : Int := 0;
11610 Check_Side_Effects : Boolean := True)
11612 function Build_Temporary
11613 (Loc : Source_Ptr;
11614 Id : Character;
11615 Related_Nod : Node_Id := Empty) return Entity_Id;
11616 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11617 -- is present (xxx is taken from the Chars field of Related_Nod),
11618 -- otherwise it generates an internal temporary. The created temporary
11619 -- entity is marked as internal.
11621 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean;
11622 -- Computes whether a side effect is possible in SPARK, which should
11623 -- be handled by removing it from the expression for GNATprove. Note
11624 -- that other side effects related to volatile variables are handled
11625 -- separately.
11627 ---------------------
11628 -- Build_Temporary --
11629 ---------------------
11631 function Build_Temporary
11632 (Loc : Source_Ptr;
11633 Id : Character;
11634 Related_Nod : Node_Id := Empty) return Entity_Id
11636 Temp_Id : Entity_Id;
11637 Temp_Nam : Name_Id;
11638 Should_Set_Related_Expression : Boolean := False;
11640 begin
11641 -- The context requires an external symbol : expression is
11642 -- the bound of an array, or a discriminant value. We create
11643 -- a unique string using the related entity and an appropriate
11644 -- suffix, rather than a numeric serial number (used for internal
11645 -- entities) that may vary depending on compilation options, in
11646 -- particular on the Assertions_Enabled mode. This avoids spurious
11647 -- link errors.
11649 if Present (Related_Id) then
11650 if Is_Low_Bound then
11651 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
11653 elsif Is_High_Bound then
11654 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
11656 else
11657 pragma Assert (Discr_Number > 0);
11659 -- We don't have any intelligible way of printing T_DISCR in
11660 -- CodePeer. Thus, set a related expression in this case.
11662 Should_Set_Related_Expression := True;
11664 -- Use fully qualified name to avoid ambiguities.
11666 Temp_Nam :=
11667 New_External_Name
11668 (Get_Qualified_Name (Related_Id), "_DISCR", Discr_Number);
11669 end if;
11671 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
11673 if Should_Set_Related_Expression then
11674 Set_Related_Expression (Temp_Id, Related_Nod);
11675 end if;
11677 -- Otherwise generate an internal temporary
11679 else
11680 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
11681 end if;
11683 Set_Is_Internal (Temp_Id);
11685 return Temp_Id;
11686 end Build_Temporary;
11688 -----------------------------------
11689 -- Possible_Side_Effect_In_SPARK --
11690 -----------------------------------
11692 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean is
11693 begin
11694 -- Side-effect removal in SPARK should only occur when not inside a
11695 -- generic and not doing a preanalysis, inside an object renaming or
11696 -- a type declaration or a for-loop iteration scheme.
11698 return not Inside_A_Generic
11699 and then Full_Analysis
11700 and then Nkind (Enclosing_Declaration (Exp)) in
11701 N_Component_Declaration
11702 | N_Full_Type_Declaration
11703 | N_Iterator_Specification
11704 | N_Loop_Parameter_Specification
11705 | N_Object_Renaming_Declaration
11706 | N_Subtype_Declaration;
11707 end Possible_Side_Effect_In_SPARK;
11709 -- Local variables
11711 Loc : constant Source_Ptr := Sloc (Exp);
11712 Exp_Type : constant Entity_Id := Etype (Exp);
11713 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
11714 Def_Id : Entity_Id;
11715 E : Node_Id;
11716 New_Exp : Node_Id;
11717 Ptr_Typ_Decl : Node_Id;
11718 Ref_Type : Entity_Id;
11719 Res : Node_Id;
11721 -- Start of processing for Remove_Side_Effects
11723 begin
11724 -- Handle cases in which there is nothing to do. In GNATprove mode,
11725 -- removal of side effects is useful for the light expansion of
11726 -- renamings.
11728 if not Expander_Active
11729 and then not
11730 (GNATprove_Mode and then Possible_Side_Effect_In_SPARK (Exp))
11731 then
11732 return;
11734 -- Cannot generate temporaries if the invocation to remove side effects
11735 -- was issued too early and the type of the expression is not resolved
11736 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11737 -- Remove_Side_Effects).
11739 elsif No (Exp_Type)
11740 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11741 then
11742 return;
11744 -- Nothing to do if prior expansion determined that a function call does
11745 -- not require side effect removal.
11747 elsif Nkind (Exp) = N_Function_Call
11748 and then No_Side_Effect_Removal (Exp)
11749 then
11750 return;
11752 -- No action needed for side-effect free expressions
11754 elsif Check_Side_Effects
11755 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11756 then
11757 return;
11759 -- Generating C code we cannot remove side effect of function returning
11760 -- class-wide types since there is no secondary stack (required to use
11761 -- 'reference).
11763 elsif Modify_Tree_For_C
11764 and then Nkind (Exp) = N_Function_Call
11765 and then Is_Class_Wide_Type (Etype (Exp))
11766 then
11767 return;
11768 end if;
11770 -- The remaining processing is done with all checks suppressed
11772 -- Note: from now on, don't use return statements, instead do a goto
11773 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11775 Scope_Suppress.Suppress := (others => True);
11777 -- If this is a side-effect free attribute reference whose expressions
11778 -- are also side-effect free and whose prefix is not a name, remove the
11779 -- side effects of the prefix. A copy of the prefix is required in this
11780 -- case and it is better not to make an additional one for the attribute
11781 -- itself, because the return type of many of them is universal integer,
11782 -- which is a very large type for a temporary.
11783 -- The prefix of an attribute reference Reduce may be syntactically an
11784 -- aggregate, but will be expanded into a loop, so no need to remove
11785 -- side-effects.
11787 if Nkind (Exp) = N_Attribute_Reference
11788 and then Side_Effect_Free_Attribute (Attribute_Name (Exp))
11789 and then Side_Effect_Free (Expressions (Exp), Name_Req, Variable_Ref)
11790 and then (Attribute_Name (Exp) /= Name_Reduce
11791 or else Nkind (Prefix (Exp)) /= N_Aggregate)
11792 and then not Is_Name_Reference (Prefix (Exp))
11793 then
11794 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11795 goto Leave;
11797 -- If this is an elementary or a small not-by-reference record type, and
11798 -- we need to capture the value, just make a constant; this is cheap and
11799 -- objects of both kinds of types can be bit aligned, so it might not be
11800 -- possible to generate a reference to them. Likewise if this is not a
11801 -- name reference, except for a type conversion, because we would enter
11802 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11803 -- type has predicates (and type conversions need a specific treatment
11804 -- anyway, see below). Also do it if we have a volatile reference and
11805 -- Name_Req is not set (see comments for Side_Effect_Free).
11807 elsif (Is_Elementary_Type (Exp_Type)
11808 or else (Is_Record_Type (Exp_Type)
11809 and then Known_Static_RM_Size (Exp_Type)
11810 and then RM_Size (Exp_Type) <= System_Max_Integer_Size
11811 and then not Has_Discriminants (Exp_Type)
11812 and then not Is_By_Reference_Type (Exp_Type)))
11813 and then (Variable_Ref
11814 or else (not Is_Name_Reference (Exp)
11815 and then Nkind (Exp) /= N_Type_Conversion)
11816 or else (not Name_Req
11817 and then Is_Volatile_Reference (Exp)))
11818 then
11819 Def_Id := Build_Temporary (Loc, 'R', Exp);
11820 Set_Etype (Def_Id, Exp_Type);
11821 Res := New_Occurrence_Of (Def_Id, Loc);
11823 -- If the expression is a packed reference, it must be reanalyzed and
11824 -- expanded, depending on context. This is the case for actuals where
11825 -- a constraint check may capture the actual before expansion of the
11826 -- call is complete.
11828 if Nkind (Exp) = N_Indexed_Component
11829 and then Is_Packed (Etype (Prefix (Exp)))
11830 then
11831 Set_Analyzed (Exp, False);
11832 Set_Analyzed (Prefix (Exp), False);
11833 end if;
11835 -- Generate:
11836 -- Rnn : Exp_Type renames Expr;
11838 -- In GNATprove mode, we prefer to use renamings for intermediate
11839 -- variables to definition of constants, due to the implicit move
11840 -- operation that such a constant definition causes as part of the
11841 -- support in GNATprove for ownership pointers. Hence, we generate
11842 -- a renaming for a reference to an object of a nonscalar type.
11844 if Renaming_Req
11845 or else (GNATprove_Mode
11846 and then Is_Object_Reference (Exp)
11847 and then not Is_Scalar_Type (Exp_Type))
11848 then
11849 E :=
11850 Make_Object_Renaming_Declaration (Loc,
11851 Defining_Identifier => Def_Id,
11852 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11853 Name => Relocate_Node (Exp));
11855 -- Generate:
11856 -- Rnn : constant Exp_Type := Expr;
11858 else
11859 E :=
11860 Make_Object_Declaration (Loc,
11861 Defining_Identifier => Def_Id,
11862 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11863 Constant_Present => True,
11864 Expression => Relocate_Node (Exp));
11866 Set_Assignment_OK (E);
11867 end if;
11869 Insert_Action (Exp, E);
11871 -- If the expression has the form v.all then we can just capture the
11872 -- pointer, and then do an explicit dereference on the result, but
11873 -- this is not right if this is a volatile reference.
11875 elsif Nkind (Exp) = N_Explicit_Dereference
11876 and then not Is_Volatile_Reference (Exp)
11877 then
11878 Def_Id := Build_Temporary (Loc, 'R', Exp);
11879 Res :=
11880 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11882 Insert_Action (Exp,
11883 Make_Object_Declaration (Loc,
11884 Defining_Identifier => Def_Id,
11885 Object_Definition =>
11886 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11887 Constant_Present => True,
11888 Expression => Relocate_Node (Prefix (Exp))));
11890 -- Similar processing for an unchecked conversion of an expression of
11891 -- the form v.all, where we want the same kind of treatment.
11893 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11894 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11895 then
11896 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11897 goto Leave;
11899 -- If this is a type conversion, leave the type conversion and remove
11900 -- side effects in the expression, unless it is of universal integer,
11901 -- which is a very large type for a temporary. This is important in
11902 -- several circumstances: for change of representations and also when
11903 -- this is a view conversion to a smaller object, where gigi can end
11904 -- up creating its own temporary of the wrong size.
11906 elsif Nkind (Exp) = N_Type_Conversion
11907 and then Etype (Expression (Exp)) /= Universal_Integer
11908 then
11909 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11911 -- Generating C code the type conversion of an access to constrained
11912 -- array type into an access to unconstrained array type involves
11913 -- initializing a fat pointer and the expression must be free of
11914 -- side effects to safely compute its bounds.
11916 if Modify_Tree_For_C
11917 and then Is_Access_Type (Etype (Exp))
11918 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11919 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11920 then
11921 Def_Id := Build_Temporary (Loc, 'R', Exp);
11922 Set_Etype (Def_Id, Exp_Type);
11923 Res := New_Occurrence_Of (Def_Id, Loc);
11925 Insert_Action (Exp,
11926 Make_Object_Declaration (Loc,
11927 Defining_Identifier => Def_Id,
11928 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11929 Constant_Present => True,
11930 Expression => Relocate_Node (Exp)));
11931 else
11932 goto Leave;
11933 end if;
11935 -- If this is an unchecked conversion that Gigi can't handle, make
11936 -- a copy or a use a renaming to capture the value.
11938 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11939 and then not Safe_Unchecked_Type_Conversion (Exp)
11940 then
11941 if CW_Or_Has_Controlled_Part (Exp_Type) then
11943 -- Use a renaming to capture the expression, rather than create
11944 -- a controlled temporary.
11946 Def_Id := Build_Temporary (Loc, 'R', Exp);
11947 Res := New_Occurrence_Of (Def_Id, Loc);
11949 Insert_Action (Exp,
11950 Make_Object_Renaming_Declaration (Loc,
11951 Defining_Identifier => Def_Id,
11952 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11953 Name => Relocate_Node (Exp)));
11955 else
11956 Def_Id := Build_Temporary (Loc, 'R', Exp);
11957 Set_Etype (Def_Id, Exp_Type);
11958 Res := New_Occurrence_Of (Def_Id, Loc);
11960 E :=
11961 Make_Object_Declaration (Loc,
11962 Defining_Identifier => Def_Id,
11963 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11964 Constant_Present => not Is_Variable (Exp),
11965 Expression => Relocate_Node (Exp));
11967 Set_Assignment_OK (E);
11968 Insert_Action (Exp, E);
11969 end if;
11971 -- If this is a packed array component or a selected component with a
11972 -- nonstandard representation, we cannot generate a reference because
11973 -- the component may be unaligned, so we must use a renaming and this
11974 -- renaming is handled by the front end, as the back end may balk at
11975 -- the nonstandard representation (see Evaluation_Required in Exp_Ch8).
11977 elsif Nkind (Exp) in N_Indexed_Component | N_Selected_Component
11978 and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
11979 then
11980 Def_Id := Build_Temporary (Loc, 'R', Exp);
11981 Res := New_Occurrence_Of (Def_Id, Loc);
11983 Insert_Action (Exp,
11984 Make_Object_Renaming_Declaration (Loc,
11985 Defining_Identifier => Def_Id,
11986 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11987 Name => Relocate_Node (Exp)));
11989 -- For an expression that denotes a name, we can use a renaming scheme.
11990 -- This is needed for correctness in the case of a volatile object of
11991 -- a nonvolatile type because the Make_Reference call of the "default"
11992 -- approach would generate an illegal access value (an access value
11993 -- cannot designate such an object - see Analyze_Reference).
11995 elsif Is_Name_Reference (Exp)
11997 -- We skip using this scheme if we have an object of a volatile
11998 -- type and we do not have Name_Req set true (see comments for
11999 -- Side_Effect_Free).
12001 and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
12002 then
12003 Def_Id := Build_Temporary (Loc, 'R', Exp);
12004 Res := New_Occurrence_Of (Def_Id, Loc);
12006 Insert_Action (Exp,
12007 Make_Object_Renaming_Declaration (Loc,
12008 Defining_Identifier => Def_Id,
12009 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12010 Name => Relocate_Node (Exp)));
12012 -- Avoid generating a variable-sized temporary, by generating the
12013 -- reference just for the function call. The transformation could be
12014 -- refined to apply only when the array component is constrained by a
12015 -- discriminant???
12017 elsif Nkind (Exp) = N_Selected_Component
12018 and then Nkind (Prefix (Exp)) = N_Function_Call
12019 and then Is_Array_Type (Exp_Type)
12020 then
12021 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
12022 goto Leave;
12024 -- Otherwise we generate a reference to the expression
12026 else
12027 -- When generating C code we cannot consider side effect free object
12028 -- declarations that have discriminants and are initialized by means
12029 -- of a function call since on this target there is no secondary
12030 -- stack to store the return value and the expander may generate an
12031 -- extra call to the function to compute the discriminant value. In
12032 -- addition, for targets that have secondary stack, the expansion of
12033 -- functions with side effects involves the generation of an access
12034 -- type to capture the return value stored in the secondary stack;
12035 -- by contrast when generating C code such expansion generates an
12036 -- internal object declaration (no access type involved) which must
12037 -- be identified here to avoid entering into a never-ending loop
12038 -- generating internal object declarations.
12040 if Modify_Tree_For_C
12041 and then Nkind (Parent (Exp)) = N_Object_Declaration
12042 and then
12043 (Nkind (Exp) /= N_Function_Call
12044 or else not Has_Discriminants (Exp_Type)
12045 or else Is_Internal_Name
12046 (Chars (Defining_Identifier (Parent (Exp)))))
12047 then
12048 goto Leave;
12049 end if;
12051 -- Special processing for function calls that return a limited type.
12052 -- We need to build a declaration that will enable build-in-place
12053 -- expansion of the call. This is not done if the context is already
12054 -- an object declaration, to prevent infinite recursion.
12056 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
12057 -- to accommodate functions returning limited objects by reference.
12059 if Ada_Version >= Ada_2005
12060 and then Nkind (Exp) = N_Function_Call
12061 and then Is_Limited_View (Etype (Exp))
12062 and then Nkind (Parent (Exp)) /= N_Object_Declaration
12063 then
12064 declare
12065 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
12066 Decl : Node_Id;
12068 begin
12069 Decl :=
12070 Make_Object_Declaration (Loc,
12071 Defining_Identifier => Obj,
12072 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12073 Expression => Relocate_Node (Exp));
12075 Insert_Action (Exp, Decl);
12076 Set_Etype (Obj, Exp_Type);
12077 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
12078 goto Leave;
12079 end;
12080 end if;
12082 Def_Id := Build_Temporary (Loc, 'R', Exp);
12084 -- The regular expansion of functions with side effects involves the
12085 -- generation of an access type to capture the return value found on
12086 -- the secondary stack. Since SPARK (and why) cannot process access
12087 -- types, use a different approach which ignores the secondary stack
12088 -- and "copies" the returned object.
12089 -- When generating C code, no need for a 'reference since the
12090 -- secondary stack is not supported.
12092 if GNATprove_Mode or Modify_Tree_For_C then
12093 Res := New_Occurrence_Of (Def_Id, Loc);
12094 Ref_Type := Exp_Type;
12096 -- Regular expansion utilizing an access type and 'reference
12098 else
12099 Res :=
12100 Make_Explicit_Dereference (Loc,
12101 Prefix => New_Occurrence_Of (Def_Id, Loc));
12103 -- Generate:
12104 -- type Ann is access all <Exp_Type>;
12106 Ref_Type := Make_Temporary (Loc, 'A');
12108 Ptr_Typ_Decl :=
12109 Make_Full_Type_Declaration (Loc,
12110 Defining_Identifier => Ref_Type,
12111 Type_Definition =>
12112 Make_Access_To_Object_Definition (Loc,
12113 All_Present => True,
12114 Subtype_Indication =>
12115 New_Occurrence_Of (Exp_Type, Loc)));
12117 Insert_Action (Exp, Ptr_Typ_Decl);
12118 end if;
12120 E := Exp;
12121 if Nkind (E) = N_Explicit_Dereference then
12122 New_Exp := Relocate_Node (Prefix (E));
12124 else
12125 E := Relocate_Node (E);
12127 -- Do not generate a 'reference in SPARK mode or C generation
12128 -- since the access type is not created in the first place.
12130 if GNATprove_Mode or Modify_Tree_For_C then
12131 New_Exp := E;
12133 -- Otherwise generate reference, marking the value as non-null
12134 -- since we know it cannot be null and we don't want a check.
12136 else
12137 New_Exp := Make_Reference (Loc, E);
12138 Set_Is_Known_Non_Null (Def_Id);
12139 end if;
12140 end if;
12142 if Is_Delayed_Aggregate (E) then
12144 -- The expansion of nested aggregates is delayed until the
12145 -- enclosing aggregate is expanded. As aggregates are often
12146 -- qualified, the predicate applies to qualified expressions as
12147 -- well, indicating that the enclosing aggregate has not been
12148 -- expanded yet. At this point the aggregate is part of a
12149 -- stand-alone declaration, and must be fully expanded.
12151 if Nkind (E) = N_Qualified_Expression then
12152 Set_Expansion_Delayed (Expression (E), False);
12153 Set_Analyzed (Expression (E), False);
12154 else
12155 Set_Expansion_Delayed (E, False);
12156 end if;
12158 Set_Analyzed (E, False);
12159 end if;
12161 -- Generating C code of object declarations that have discriminants
12162 -- and are initialized by means of a function call we propagate the
12163 -- discriminants of the parent type to the internally built object.
12164 -- This is needed to avoid generating an extra call to the called
12165 -- function.
12167 -- For example, if we generate here the following declaration, it
12168 -- will be expanded later adding an extra call to evaluate the value
12169 -- of the discriminant (needed to compute the size of the object).
12171 -- type Rec (D : Integer) is ...
12172 -- Obj : constant Rec := SomeFunc;
12174 if Modify_Tree_For_C
12175 and then Nkind (Parent (Exp)) = N_Object_Declaration
12176 and then Has_Discriminants (Exp_Type)
12177 and then Nkind (Exp) = N_Function_Call
12178 then
12179 Insert_Action (Exp,
12180 Make_Object_Declaration (Loc,
12181 Defining_Identifier => Def_Id,
12182 Object_Definition => New_Copy_Tree
12183 (Object_Definition (Parent (Exp))),
12184 Constant_Present => True,
12185 Expression => New_Exp));
12186 else
12187 Insert_Action (Exp,
12188 Make_Object_Declaration (Loc,
12189 Defining_Identifier => Def_Id,
12190 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
12191 Constant_Present => True,
12192 Expression => New_Exp));
12193 end if;
12194 end if;
12196 -- Preserve the Assignment_OK flag in all copies, since at least one
12197 -- copy may be used in a context where this flag must be set (otherwise
12198 -- why would the flag be set in the first place).
12200 Set_Assignment_OK (Res, Assignment_OK (Exp));
12202 -- Preserve the Do_Range_Check flag in all copies
12204 Set_Do_Range_Check (Res, Do_Range_Check (Exp));
12206 -- Finally rewrite the original expression and we are done
12208 Rewrite (Exp, Res);
12209 Analyze_And_Resolve (Exp, Exp_Type);
12211 <<Leave>>
12212 Scope_Suppress := Svg_Suppress;
12213 end Remove_Side_Effects;
12215 ------------------------
12216 -- Replace_References --
12217 ------------------------
12219 procedure Replace_References
12220 (Expr : Node_Id;
12221 Par_Typ : Entity_Id;
12222 Deriv_Typ : Entity_Id;
12223 Par_Obj : Entity_Id := Empty;
12224 Deriv_Obj : Entity_Id := Empty)
12226 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
12227 -- Determine whether node Ref denotes some component of Deriv_Obj
12229 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
12230 -- Substitute a reference to an entity with the corresponding value
12231 -- stored in table Type_Map.
12233 function Type_Of_Formal
12234 (Call : Node_Id;
12235 Actual : Node_Id) return Entity_Id;
12236 -- Find the type of the formal parameter which corresponds to actual
12237 -- parameter Actual in subprogram call Call.
12239 ----------------------
12240 -- Is_Deriv_Obj_Ref --
12241 ----------------------
12243 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
12244 Par : constant Node_Id := Parent (Ref);
12246 begin
12247 -- Detect the folowing selected component form:
12249 -- Deriv_Obj.(something)
12251 return
12252 Nkind (Par) = N_Selected_Component
12253 and then Is_Entity_Name (Prefix (Par))
12254 and then Entity (Prefix (Par)) = Deriv_Obj;
12255 end Is_Deriv_Obj_Ref;
12257 -----------------
12258 -- Replace_Ref --
12259 -----------------
12261 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
12262 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
12263 -- Reset the Controlling_Argument of all function calls that
12264 -- encapsulate node From_Arg.
12266 ----------------------------------
12267 -- Remove_Controlling_Arguments --
12268 ----------------------------------
12270 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
12271 Par : Node_Id;
12273 begin
12274 Par := From_Arg;
12275 while Present (Par) loop
12276 if Nkind (Par) = N_Function_Call
12277 and then Present (Controlling_Argument (Par))
12278 then
12279 Set_Controlling_Argument (Par, Empty);
12281 -- Prevent the search from going too far
12283 elsif Is_Body_Or_Package_Declaration (Par) then
12284 exit;
12285 end if;
12287 Par := Parent (Par);
12288 end loop;
12289 end Remove_Controlling_Arguments;
12291 -- Local variables
12293 Context : constant Node_Id :=
12294 (if No (Ref) then Empty else Parent (Ref));
12296 Loc : constant Source_Ptr := Sloc (Ref);
12297 Ref_Id : Entity_Id;
12298 Result : Traverse_Result;
12300 New_Ref : Node_Id;
12301 -- The new reference which is intended to substitute the old one
12303 Old_Ref : Node_Id;
12304 -- The reference designated for replacement. In certain cases this
12305 -- may be a node other than Ref.
12307 Val : Node_Or_Entity_Id;
12308 -- The corresponding value of Ref from the type map
12310 -- Start of processing for Replace_Ref
12312 begin
12313 -- Assume that the input reference is to be replaced and that the
12314 -- traversal should examine the children of the reference.
12316 Old_Ref := Ref;
12317 Result := OK;
12319 -- The input denotes a meaningful reference
12321 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
12322 Ref_Id := Entity (Ref);
12323 Val := Type_Map.Get (Ref_Id);
12325 -- The reference has a corresponding value in the type map, a
12326 -- substitution is possible.
12328 if Present (Val) then
12330 -- The reference denotes a discriminant
12332 if Ekind (Ref_Id) = E_Discriminant then
12333 if Nkind (Val) in N_Entity then
12335 -- The value denotes another discriminant. Replace as
12336 -- follows:
12338 -- _object.Discr -> _object.Val
12340 if Ekind (Val) = E_Discriminant then
12341 New_Ref := New_Occurrence_Of (Val, Loc);
12343 -- Otherwise the value denotes the entity of a name which
12344 -- constraints the discriminant. Replace as follows:
12346 -- _object.Discr -> Val
12348 else
12349 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
12351 New_Ref := New_Occurrence_Of (Val, Loc);
12352 Old_Ref := Parent (Old_Ref);
12353 end if;
12355 -- Otherwise the value denotes an arbitrary expression which
12356 -- constraints the discriminant. Replace as follows:
12358 -- _object.Discr -> Val
12360 else
12361 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
12363 New_Ref := New_Copy_Tree (Val);
12364 Old_Ref := Parent (Old_Ref);
12365 end if;
12367 -- Otherwise the reference denotes a primitive. Replace as
12368 -- follows:
12370 -- Primitive -> Val
12372 else
12373 pragma Assert (Nkind (Val) in N_Entity);
12374 New_Ref := New_Occurrence_Of (Val, Loc);
12375 end if;
12377 -- The reference mentions the _object parameter of the parent
12378 -- type's DIC or type invariant procedure. Replace as follows:
12380 -- _object -> _object
12382 elsif Present (Par_Obj)
12383 and then Present (Deriv_Obj)
12384 and then Ref_Id = Par_Obj
12385 then
12386 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
12388 -- The type of the _object parameter is class-wide when the
12389 -- expression comes from an assertion pragma that applies to
12390 -- an abstract parent type or an interface. The class-wide type
12391 -- facilitates the preanalysis of the expression by treating
12392 -- calls to abstract primitives that mention the current
12393 -- instance of the type as dispatching. Once the calls are
12394 -- remapped to invoke overriding or inherited primitives, the
12395 -- calls no longer need to be dispatching. Examine all function
12396 -- calls that encapsulate the _object parameter and reset their
12397 -- Controlling_Argument attribute.
12399 if Is_Class_Wide_Type (Etype (Par_Obj))
12400 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
12401 then
12402 Remove_Controlling_Arguments (Old_Ref);
12403 end if;
12405 -- The reference to _object acts as an actual parameter in a
12406 -- subprogram call which may be invoking a primitive of the
12407 -- parent type:
12409 -- Primitive (... _object ...);
12411 -- The parent type primitive may not be overridden nor
12412 -- inherited when it is declared after the derived type
12413 -- definition:
12415 -- type Parent is tagged private;
12416 -- type Child is new Parent with private;
12417 -- procedure Primitive (Obj : Parent);
12419 -- In this scenario the _object parameter is converted to the
12420 -- parent type. Due to complications with partial/full views
12421 -- and view swaps, the parent type is taken from the formal
12422 -- parameter of the subprogram being called.
12424 if Nkind (Context) in N_Subprogram_Call
12425 and then No (Type_Map.Get (Entity (Name (Context))))
12426 then
12427 declare
12428 -- We need to use the Original_Node of the callee, in
12429 -- case it was already modified. Note that we are using
12430 -- Traverse_Proc to walk the tree, and it is defined to
12431 -- walk subtrees in an arbitrary order.
12433 Callee : constant Entity_Id :=
12434 Entity (Original_Node (Name (Context)));
12435 begin
12436 if No (Type_Map.Get (Callee)) then
12437 New_Ref :=
12438 Convert_To
12439 (Type_Of_Formal (Context, Old_Ref), New_Ref);
12441 -- Do not process the generated type conversion
12442 -- because both the parent type and the derived type
12443 -- are in the Type_Map table. This will clobber the
12444 -- type conversion by resetting its subtype mark.
12446 Result := Skip;
12447 end if;
12448 end;
12449 end if;
12451 -- Otherwise there is nothing to replace
12453 else
12454 New_Ref := Empty;
12455 end if;
12457 if Present (New_Ref) then
12458 Rewrite (Old_Ref, New_Ref);
12460 -- Update the return type when the context of the reference
12461 -- acts as the name of a function call. Note that the update
12462 -- should not be performed when the reference appears as an
12463 -- actual in the call.
12465 if Nkind (Context) = N_Function_Call
12466 and then Name (Context) = Old_Ref
12467 then
12468 Set_Etype (Context, Etype (Val));
12469 end if;
12470 end if;
12471 end if;
12473 -- Reanalyze the reference due to potential replacements
12475 if Nkind (Old_Ref) in N_Has_Etype then
12476 Set_Analyzed (Old_Ref, False);
12477 end if;
12479 return Result;
12480 end Replace_Ref;
12482 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
12484 --------------------
12485 -- Type_Of_Formal --
12486 --------------------
12488 function Type_Of_Formal
12489 (Call : Node_Id;
12490 Actual : Node_Id) return Entity_Id
12492 A : Node_Id;
12493 F : Entity_Id;
12495 begin
12496 -- Examine the list of actual and formal parameters in parallel
12498 A := First (Parameter_Associations (Call));
12499 F := First_Formal (Entity (Name (Call)));
12500 while Present (A) and then Present (F) loop
12501 if A = Actual then
12502 return Etype (F);
12503 end if;
12505 Next (A);
12506 Next_Formal (F);
12507 end loop;
12509 -- The actual parameter must always have a corresponding formal
12511 pragma Assert (False);
12513 return Empty;
12514 end Type_Of_Formal;
12516 -- Start of processing for Replace_References
12518 begin
12519 -- Map the attributes of the parent type to the proper corresponding
12520 -- attributes of the derived type.
12522 Map_Types
12523 (Parent_Type => Par_Typ,
12524 Derived_Type => Deriv_Typ);
12526 -- Inspect the input expression and perform substitutions where
12527 -- necessary.
12529 Replace_Refs (Expr);
12530 end Replace_References;
12532 -----------------------------
12533 -- Replace_Type_References --
12534 -----------------------------
12536 procedure Replace_Type_References
12537 (Expr : Node_Id;
12538 Typ : Entity_Id;
12539 Obj_Id : Entity_Id)
12541 procedure Replace_Type_Ref (N : Node_Id);
12542 -- Substitute a single reference of the current instance of type Typ
12543 -- with a reference to Obj_Id.
12545 ----------------------
12546 -- Replace_Type_Ref --
12547 ----------------------
12549 procedure Replace_Type_Ref (N : Node_Id) is
12550 begin
12551 -- Decorate the reference to Typ even though it may be rewritten
12552 -- further down. This is done so that routines which examine
12553 -- properties of the Original_Node have some semantic information.
12555 if Nkind (N) = N_Identifier then
12556 Set_Entity (N, Typ);
12557 Set_Etype (N, Typ);
12559 elsif Nkind (N) = N_Selected_Component then
12560 Analyze (Prefix (N));
12561 Set_Entity (Selector_Name (N), Typ);
12562 Set_Etype (Selector_Name (N), Typ);
12563 end if;
12565 -- Perform the following substitution:
12567 -- Typ --> _object
12569 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
12570 Set_Comes_From_Source (N, True);
12571 end Replace_Type_Ref;
12573 procedure Replace_Type_Refs is
12574 new Replace_Type_References_Generic (Replace_Type_Ref);
12576 -- Start of processing for Replace_Type_References
12578 begin
12579 Replace_Type_Refs (Expr, Typ);
12580 end Replace_Type_References;
12582 ---------------------------
12583 -- Represented_As_Scalar --
12584 ---------------------------
12586 function Represented_As_Scalar (T : Entity_Id) return Boolean is
12587 UT : constant Entity_Id := Underlying_Type (T);
12588 begin
12589 return Is_Scalar_Type (UT)
12590 or else (Is_Bit_Packed_Array (UT)
12591 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
12592 end Represented_As_Scalar;
12594 ------------------------------
12595 -- Requires_Cleanup_Actions --
12596 ------------------------------
12598 function Requires_Cleanup_Actions
12599 (N : Node_Id;
12600 Lib_Level : Boolean) return Boolean
12602 At_Lib_Level : constant Boolean :=
12603 Lib_Level
12604 and then Nkind (N) in N_Package_Body | N_Package_Specification;
12605 -- N is at the library level if the top-most context is a package and
12606 -- the path taken to reach N does not include nonpackage constructs.
12608 begin
12609 case Nkind (N) is
12610 when N_Accept_Statement
12611 | N_Block_Statement
12612 | N_Entry_Body
12613 | N_Package_Body
12614 | N_Protected_Body
12615 | N_Subprogram_Body
12616 | N_Task_Body
12618 return
12619 Requires_Cleanup_Actions
12620 (L => Declarations (N),
12621 Lib_Level => At_Lib_Level,
12622 Nested_Constructs => True)
12623 or else
12624 (Present (Handled_Statement_Sequence (N))
12625 and then
12626 Requires_Cleanup_Actions
12627 (L =>
12628 Statements (Handled_Statement_Sequence (N)),
12629 Lib_Level => At_Lib_Level,
12630 Nested_Constructs => True));
12632 -- Extended return statements are the same as the above, except that
12633 -- there is no Declarations field. We do not want to clean up the
12634 -- Return_Object_Declarations.
12636 when N_Extended_Return_Statement =>
12637 return
12638 Present (Handled_Statement_Sequence (N))
12639 and then Requires_Cleanup_Actions
12640 (L =>
12641 Statements (Handled_Statement_Sequence (N)),
12642 Lib_Level => At_Lib_Level,
12643 Nested_Constructs => True);
12645 when N_Package_Specification =>
12646 return
12647 Requires_Cleanup_Actions
12648 (L => Visible_Declarations (N),
12649 Lib_Level => At_Lib_Level,
12650 Nested_Constructs => True)
12651 or else
12652 Requires_Cleanup_Actions
12653 (L => Private_Declarations (N),
12654 Lib_Level => At_Lib_Level,
12655 Nested_Constructs => True);
12657 when others =>
12658 raise Program_Error;
12659 end case;
12660 end Requires_Cleanup_Actions;
12662 ------------------------------
12663 -- Requires_Cleanup_Actions --
12664 ------------------------------
12666 function Requires_Cleanup_Actions
12667 (L : List_Id;
12668 Lib_Level : Boolean;
12669 Nested_Constructs : Boolean) return Boolean
12671 Decl : Node_Id;
12672 Expr : Node_Id;
12673 Obj_Id : Entity_Id;
12674 Obj_Typ : Entity_Id;
12675 Pack_Id : Entity_Id;
12676 Typ : Entity_Id;
12678 begin
12679 if No (L) or else Is_Empty_List (L) then
12680 return False;
12681 end if;
12683 Decl := First (L);
12684 while Present (Decl) loop
12686 -- Library-level tagged types
12688 if Nkind (Decl) = N_Full_Type_Declaration then
12689 Typ := Defining_Identifier (Decl);
12691 -- Ignored Ghost types do not need any cleanup actions because
12692 -- they will not appear in the final tree.
12694 if Is_Ignored_Ghost_Entity (Typ) then
12695 null;
12697 elsif Is_Tagged_Type (Typ)
12698 and then Is_Library_Level_Entity (Typ)
12699 and then Convention (Typ) = Convention_Ada
12700 and then Present (Access_Disp_Table (Typ))
12701 and then RTE_Available (RE_Unregister_Tag)
12702 and then not Is_Abstract_Type (Typ)
12703 and then not No_Run_Time_Mode
12704 then
12705 return True;
12706 end if;
12708 -- Regular object declarations
12710 elsif Nkind (Decl) = N_Object_Declaration then
12711 Obj_Id := Defining_Identifier (Decl);
12712 Obj_Typ := Base_Type (Etype (Obj_Id));
12713 Expr := Expression (Decl);
12715 -- Bypass any form of processing for objects which have their
12716 -- finalization disabled. This applies only to objects at the
12717 -- library level.
12719 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12720 null;
12722 -- Finalization of transient objects are treated separately in
12723 -- order to handle sensitive cases. These include:
12725 -- * Aggregate expansion
12726 -- * If, case, and expression with actions expansion
12727 -- * Transient scopes
12729 -- If one of those contexts has marked the transient object as
12730 -- ignored, do not generate finalization actions for it.
12732 elsif Is_Finalized_Transient (Obj_Id)
12733 or else Is_Ignored_Transient (Obj_Id)
12734 then
12735 null;
12737 -- Ignored Ghost objects do not need any cleanup actions because
12738 -- they will not appear in the final tree.
12740 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12741 null;
12743 -- The object is of the form:
12744 -- Obj : [constant] Typ [:= Expr];
12746 -- Do not process tag-to-class-wide conversions because they do
12747 -- not yield an object. Do not process the incomplete view of a
12748 -- deferred constant. Note that an object initialized by means
12749 -- of a build-in-place function call may appear as a deferred
12750 -- constant after expansion activities. These kinds of objects
12751 -- must be finalized.
12753 elsif not Is_Imported (Obj_Id)
12754 and then Needs_Finalization (Obj_Typ)
12755 and then not Is_Tag_To_Class_Wide_Conversion (Obj_Id)
12756 and then not (Ekind (Obj_Id) = E_Constant
12757 and then not Has_Completion (Obj_Id)
12758 and then No (BIP_Initialization_Call (Obj_Id)))
12759 then
12760 return True;
12762 -- The object is of the form:
12763 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12765 -- Obj : Access_Typ :=
12766 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12768 elsif Is_Access_Type (Obj_Typ)
12769 and then Needs_Finalization
12770 (Available_View (Designated_Type (Obj_Typ)))
12771 and then Present (Expr)
12772 and then
12773 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12774 or else
12775 (Is_Non_BIP_Func_Call (Expr)
12776 and then not Is_Related_To_Func_Return (Obj_Id)))
12777 then
12778 return True;
12780 -- Processing for "hook" objects generated for transient objects
12781 -- declared inside an Expression_With_Actions.
12783 elsif Is_Access_Type (Obj_Typ)
12784 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12785 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12786 N_Object_Declaration
12787 then
12788 return True;
12790 -- Processing for intermediate results of if expressions where
12791 -- one of the alternatives uses a controlled function call.
12793 elsif Is_Access_Type (Obj_Typ)
12794 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12795 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12796 N_Defining_Identifier
12797 and then Present (Expr)
12798 and then Nkind (Expr) = N_Null
12799 then
12800 return True;
12802 -- Simple protected objects which use type System.Tasking.
12803 -- Protected_Objects.Protection to manage their locks should be
12804 -- treated as controlled since they require manual cleanup.
12806 elsif Ekind (Obj_Id) = E_Variable
12807 and then (Is_Simple_Protected_Type (Obj_Typ)
12808 or else Has_Simple_Protected_Object (Obj_Typ))
12809 then
12810 return True;
12811 end if;
12813 -- Specific cases of object renamings
12815 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12816 Obj_Id := Defining_Identifier (Decl);
12817 Obj_Typ := Base_Type (Etype (Obj_Id));
12819 -- Bypass any form of processing for objects which have their
12820 -- finalization disabled. This applies only to objects at the
12821 -- library level.
12823 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12824 null;
12826 -- Ignored Ghost object renamings do not need any cleanup actions
12827 -- because they will not appear in the final tree.
12829 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12830 null;
12832 -- Return object of a build-in-place function. This case is
12833 -- recognized and marked by the expansion of an extended return
12834 -- statement (see Expand_N_Extended_Return_Statement).
12836 elsif Needs_Finalization (Obj_Typ)
12837 and then Is_Return_Object (Obj_Id)
12838 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12839 then
12840 return True;
12842 -- Detect a case where a source object has been initialized by
12843 -- a controlled function call or another object which was later
12844 -- rewritten as a class-wide conversion of Ada.Tags.Displace.
12846 -- Obj1 : CW_Type := Src_Obj;
12847 -- Obj2 : CW_Type := Function_Call (...);
12849 -- Obj1 : CW_Type renames (... Ada.Tags.Displace (Src_Obj));
12850 -- Tmp : ... := Function_Call (...)'reference;
12851 -- Obj2 : CW_Type renames (... Ada.Tags.Displace (Tmp));
12853 elsif Is_Displacement_Of_Object_Or_Function_Result (Obj_Id) then
12854 return True;
12855 end if;
12857 -- Inspect the freeze node of an access-to-controlled type and look
12858 -- for a delayed finalization master. This case arises when the
12859 -- freeze actions are inserted at a later time than the expansion of
12860 -- the context. Since Build_Finalizer is never called on a single
12861 -- construct twice, the master will be ultimately left out and never
12862 -- finalized. This is also needed for freeze actions of designated
12863 -- types themselves, since in some cases the finalization master is
12864 -- associated with a designated type's freeze node rather than that
12865 -- of the access type (see handling for freeze actions in
12866 -- Build_Finalization_Master).
12868 elsif Nkind (Decl) = N_Freeze_Entity
12869 and then Present (Actions (Decl))
12870 then
12871 Typ := Entity (Decl);
12873 -- Freeze nodes for ignored Ghost types do not need cleanup
12874 -- actions because they will never appear in the final tree.
12876 if Is_Ignored_Ghost_Entity (Typ) then
12877 null;
12879 elsif ((Is_Access_Object_Type (Typ)
12880 and then Needs_Finalization
12881 (Available_View (Designated_Type (Typ))))
12882 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12883 and then Requires_Cleanup_Actions
12884 (Actions (Decl), Lib_Level, Nested_Constructs)
12885 then
12886 return True;
12887 end if;
12889 -- Nested package declarations
12891 elsif Nested_Constructs
12892 and then Nkind (Decl) = N_Package_Declaration
12893 then
12894 Pack_Id := Defining_Entity (Decl);
12896 -- Do not inspect an ignored Ghost package because all code found
12897 -- within will not appear in the final tree.
12899 if Is_Ignored_Ghost_Entity (Pack_Id) then
12900 null;
12902 elsif Ekind (Pack_Id) /= E_Generic_Package
12903 and then Requires_Cleanup_Actions
12904 (Specification (Decl), Lib_Level)
12905 then
12906 return True;
12907 end if;
12909 -- Nested package bodies
12911 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12913 -- Do not inspect an ignored Ghost package body because all code
12914 -- found within will not appear in the final tree.
12916 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12917 null;
12919 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12920 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12921 then
12922 return True;
12923 end if;
12925 elsif Nkind (Decl) = N_Block_Statement
12926 and then
12928 -- Handle a rare case caused by a controlled transient object
12929 -- created as part of a record init proc. The variable is wrapped
12930 -- in a block, but the block is not associated with a transient
12931 -- scope.
12933 (Inside_Init_Proc
12935 -- Handle the case where the original context has been wrapped in
12936 -- a block to avoid interference between exception handlers and
12937 -- At_End handlers. Treat the block as transparent and process its
12938 -- contents.
12940 or else Is_Finalization_Wrapper (Decl))
12941 then
12942 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12943 return True;
12944 end if;
12945 end if;
12947 Next (Decl);
12948 end loop;
12950 return False;
12951 end Requires_Cleanup_Actions;
12953 ------------------------------------
12954 -- Safe_Unchecked_Type_Conversion --
12955 ------------------------------------
12957 -- Note: this function knows quite a bit about the exact requirements of
12958 -- Gigi with respect to unchecked type conversions, and its code must be
12959 -- coordinated with any changes in Gigi in this area.
12961 -- The above requirements should be documented in Sinfo ???
12963 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12964 Otyp : Entity_Id;
12965 Ityp : Entity_Id;
12966 Oalign : Uint;
12967 Ialign : Uint;
12968 Pexp : constant Node_Id := Parent (Exp);
12970 begin
12971 -- If the expression is the RHS of an assignment or object declaration
12972 -- we are always OK because there will always be a target.
12974 -- Object renaming declarations, (generated for view conversions of
12975 -- actuals in inlined calls), like object declarations, provide an
12976 -- explicit type, and are safe as well.
12978 if (Nkind (Pexp) = N_Assignment_Statement
12979 and then Expression (Pexp) = Exp)
12980 or else Nkind (Pexp)
12981 in N_Object_Declaration | N_Object_Renaming_Declaration
12982 then
12983 return True;
12985 -- If the expression is the prefix of an N_Selected_Component we should
12986 -- also be OK because GCC knows to look inside the conversion except if
12987 -- the type is discriminated. We assume that we are OK anyway if the
12988 -- type is not set yet or if it is controlled since we can't afford to
12989 -- introduce a temporary in this case.
12991 elsif Nkind (Pexp) = N_Selected_Component
12992 and then Prefix (Pexp) = Exp
12993 then
12994 return No (Etype (Pexp))
12995 or else not Is_Type (Etype (Pexp))
12996 or else not Has_Discriminants (Etype (Pexp))
12997 or else Is_Constrained (Etype (Pexp));
12998 end if;
13000 -- Set the output type, this comes from Etype if it is set, otherwise we
13001 -- take it from the subtype mark, which we assume was already fully
13002 -- analyzed.
13004 if Present (Etype (Exp)) then
13005 Otyp := Etype (Exp);
13006 else
13007 Otyp := Entity (Subtype_Mark (Exp));
13008 end if;
13010 -- The input type always comes from the expression, and we assume this
13011 -- is indeed always analyzed, so we can simply get the Etype.
13013 Ityp := Etype (Expression (Exp));
13015 -- Initialize alignments to unknown so far
13017 Oalign := No_Uint;
13018 Ialign := No_Uint;
13020 -- Replace a concurrent type by its corresponding record type and each
13021 -- type by its underlying type and do the tests on those. The original
13022 -- type may be a private type whose completion is a concurrent type, so
13023 -- find the underlying type first.
13025 if Present (Underlying_Type (Otyp)) then
13026 Otyp := Underlying_Type (Otyp);
13027 end if;
13029 if Present (Underlying_Type (Ityp)) then
13030 Ityp := Underlying_Type (Ityp);
13031 end if;
13033 if Is_Concurrent_Type (Otyp) then
13034 Otyp := Corresponding_Record_Type (Otyp);
13035 end if;
13037 if Is_Concurrent_Type (Ityp) then
13038 Ityp := Corresponding_Record_Type (Ityp);
13039 end if;
13041 -- If the base types are the same, we know there is no problem since
13042 -- this conversion will be a noop.
13044 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
13045 return True;
13047 -- Same if this is an upwards conversion of an untagged type, and there
13048 -- are no constraints involved (could be more general???)
13050 elsif Etype (Ityp) = Otyp
13051 and then not Is_Tagged_Type (Ityp)
13052 and then not Has_Discriminants (Ityp)
13053 and then No (First_Rep_Item (Base_Type (Ityp)))
13054 then
13055 return True;
13057 -- If the expression has an access type (object or subprogram) we assume
13058 -- that the conversion is safe, because the size of the target is safe,
13059 -- even if it is a record (which might be treated as having unknown size
13060 -- at this point).
13062 elsif Is_Access_Type (Ityp) then
13063 return True;
13065 -- If the size of output type is known at compile time, there is never
13066 -- a problem. Note that unconstrained records are considered to be of
13067 -- known size, but we can't consider them that way here, because we are
13068 -- talking about the actual size of the object.
13070 -- We also make sure that in addition to the size being known, we do not
13071 -- have a case which might generate an embarrassingly large temp in
13072 -- stack checking mode.
13074 elsif Size_Known_At_Compile_Time (Otyp)
13075 and then
13076 (not Stack_Checking_Enabled
13077 or else not May_Generate_Large_Temp (Otyp))
13078 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
13079 then
13080 return True;
13082 -- If either type is tagged, then we know the alignment is OK so Gigi
13083 -- will be able to use pointer punning.
13085 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
13086 return True;
13088 -- If either type is a limited record type, we cannot do a copy, so say
13089 -- safe since there's nothing else we can do.
13091 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
13092 return True;
13094 -- Conversions to and from packed array types are always ignored and
13095 -- hence are safe.
13097 elsif Is_Packed_Array_Impl_Type (Otyp)
13098 or else Is_Packed_Array_Impl_Type (Ityp)
13099 then
13100 return True;
13101 end if;
13103 -- The only other cases known to be safe is if the input type's
13104 -- alignment is known to be at least the maximum alignment for the
13105 -- target or if both alignments are known and the output type's
13106 -- alignment is no stricter than the input's. We can use the component
13107 -- type alignment for an array if a type is an unpacked array type.
13109 if Present (Alignment_Clause (Otyp)) then
13110 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
13112 elsif Is_Array_Type (Otyp)
13113 and then Present (Alignment_Clause (Component_Type (Otyp)))
13114 then
13115 Oalign := Expr_Value (Expression (Alignment_Clause
13116 (Component_Type (Otyp))));
13117 end if;
13119 if Present (Alignment_Clause (Ityp)) then
13120 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
13122 elsif Is_Array_Type (Ityp)
13123 and then Present (Alignment_Clause (Component_Type (Ityp)))
13124 then
13125 Ialign := Expr_Value (Expression (Alignment_Clause
13126 (Component_Type (Ityp))));
13127 end if;
13129 if Present (Ialign) and then Ialign > Maximum_Alignment then
13130 return True;
13132 elsif Present (Ialign)
13133 and then Present (Oalign)
13134 and then Ialign <= Oalign
13135 then
13136 return True;
13138 -- Otherwise, Gigi cannot handle this and we must make a temporary
13140 else
13141 return False;
13142 end if;
13143 end Safe_Unchecked_Type_Conversion;
13145 ---------------------------------
13146 -- Set_Current_Value_Condition --
13147 ---------------------------------
13149 -- Note: the implementation of this procedure is very closely tied to the
13150 -- implementation of Get_Current_Value_Condition. Here we set required
13151 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
13152 -- them, so they must have a consistent view.
13154 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
13156 procedure Set_Entity_Current_Value (N : Node_Id);
13157 -- If N is an entity reference, where the entity is of an appropriate
13158 -- kind, then set the current value of this entity to Cnode, unless
13159 -- there is already a definite value set there.
13161 procedure Set_Expression_Current_Value (N : Node_Id);
13162 -- If N is of an appropriate form, sets an appropriate entry in current
13163 -- value fields of relevant entities. Multiple entities can be affected
13164 -- in the case of an AND or AND THEN.
13166 ------------------------------
13167 -- Set_Entity_Current_Value --
13168 ------------------------------
13170 procedure Set_Entity_Current_Value (N : Node_Id) is
13171 begin
13172 if Is_Entity_Name (N) then
13173 declare
13174 Ent : constant Entity_Id := Entity (N);
13176 begin
13177 -- Don't capture if not safe to do so
13179 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
13180 return;
13181 end if;
13183 -- Here we have a case where the Current_Value field may need
13184 -- to be set. We set it if it is not already set to a compile
13185 -- time expression value.
13187 -- Note that this represents a decision that one condition
13188 -- blots out another previous one. That's certainly right if
13189 -- they occur at the same level. If the second one is nested,
13190 -- then the decision is neither right nor wrong (it would be
13191 -- equally OK to leave the outer one in place, or take the new
13192 -- inner one). Really we should record both, but our data
13193 -- structures are not that elaborate.
13195 if Nkind (Current_Value (Ent)) not in N_Subexpr then
13196 Set_Current_Value (Ent, Cnode);
13197 end if;
13198 end;
13199 end if;
13200 end Set_Entity_Current_Value;
13202 ----------------------------------
13203 -- Set_Expression_Current_Value --
13204 ----------------------------------
13206 procedure Set_Expression_Current_Value (N : Node_Id) is
13207 Cond : Node_Id;
13209 begin
13210 Cond := N;
13212 -- Loop to deal with (ignore for now) any NOT operators present. The
13213 -- presence of NOT operators will be handled properly when we call
13214 -- Get_Current_Value_Condition.
13216 while Nkind (Cond) = N_Op_Not loop
13217 Cond := Right_Opnd (Cond);
13218 end loop;
13220 -- For an AND or AND THEN, recursively process operands
13222 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
13223 Set_Expression_Current_Value (Left_Opnd (Cond));
13224 Set_Expression_Current_Value (Right_Opnd (Cond));
13225 return;
13226 end if;
13228 -- Check possible relational operator
13230 if Nkind (Cond) in N_Op_Compare then
13231 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
13232 Set_Entity_Current_Value (Left_Opnd (Cond));
13233 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
13234 Set_Entity_Current_Value (Right_Opnd (Cond));
13235 end if;
13237 elsif Nkind (Cond) in N_Type_Conversion
13238 | N_Qualified_Expression
13239 | N_Expression_With_Actions
13240 then
13241 Set_Expression_Current_Value (Expression (Cond));
13243 -- Check possible boolean variable reference
13245 else
13246 Set_Entity_Current_Value (Cond);
13247 end if;
13248 end Set_Expression_Current_Value;
13250 -- Start of processing for Set_Current_Value_Condition
13252 begin
13253 Set_Expression_Current_Value (Condition (Cnode));
13254 end Set_Current_Value_Condition;
13256 --------------------------
13257 -- Set_Elaboration_Flag --
13258 --------------------------
13260 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
13261 Loc : constant Source_Ptr := Sloc (N);
13262 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
13263 Asn : Node_Id;
13265 begin
13266 if Present (Ent) then
13268 -- Nothing to do if at the compilation unit level, because in this
13269 -- case the flag is set by the binder generated elaboration routine.
13271 if Nkind (Parent (N)) = N_Compilation_Unit then
13272 null;
13274 -- Here we do need to generate an assignment statement
13276 else
13277 Check_Restriction (No_Elaboration_Code, N);
13279 Asn :=
13280 Make_Assignment_Statement (Loc,
13281 Name => New_Occurrence_Of (Ent, Loc),
13282 Expression => Make_Integer_Literal (Loc, Uint_1));
13284 -- Mark the assignment statement as elaboration code. This allows
13285 -- the early call region mechanism (see Sem_Elab) to properly
13286 -- ignore such assignments even though they are nonpreelaborable
13287 -- code.
13289 Set_Is_Elaboration_Code (Asn);
13291 if Nkind (Parent (N)) = N_Subunit then
13292 Insert_After (Corresponding_Stub (Parent (N)), Asn);
13293 else
13294 Insert_After (N, Asn);
13295 end if;
13297 Analyze (Asn);
13299 -- Kill current value indication. This is necessary because the
13300 -- tests of this flag are inserted out of sequence and must not
13301 -- pick up bogus indications of the wrong constant value.
13303 Set_Current_Value (Ent, Empty);
13305 -- If the subprogram is in the current declarative part and
13306 -- 'access has been applied to it, generate an elaboration
13307 -- check at the beginning of the declarations of the body.
13309 if Nkind (N) = N_Subprogram_Body
13310 and then Address_Taken (Spec_Id)
13311 and then
13312 Ekind (Scope (Spec_Id)) in E_Block | E_Procedure | E_Function
13313 then
13314 declare
13315 Loc : constant Source_Ptr := Sloc (N);
13316 Decls : constant List_Id := Declarations (N);
13317 Chk : Node_Id;
13319 begin
13320 -- No need to generate this check if first entry in the
13321 -- declaration list is a raise of Program_Error now.
13323 if Present (Decls)
13324 and then Nkind (First (Decls)) = N_Raise_Program_Error
13325 then
13326 return;
13327 end if;
13329 -- Otherwise generate the check
13331 Chk :=
13332 Make_Raise_Program_Error (Loc,
13333 Condition =>
13334 Make_Op_Eq (Loc,
13335 Left_Opnd => New_Occurrence_Of (Ent, Loc),
13336 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
13337 Reason => PE_Access_Before_Elaboration);
13339 if No (Decls) then
13340 Set_Declarations (N, New_List (Chk));
13341 else
13342 Prepend (Chk, Decls);
13343 end if;
13345 Analyze (Chk);
13346 end;
13347 end if;
13348 end if;
13349 end if;
13350 end Set_Elaboration_Flag;
13352 ----------------------------
13353 -- Set_Renamed_Subprogram --
13354 ----------------------------
13356 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
13357 begin
13358 -- If input node is an identifier, we can just reset it
13360 if Nkind (N) = N_Identifier then
13361 Set_Chars (N, Chars (E));
13362 Set_Entity (N, E);
13364 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
13366 else
13367 declare
13368 CS : constant Boolean := Comes_From_Source (N);
13369 begin
13370 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
13371 Set_Entity (N, E);
13372 Set_Comes_From_Source (N, CS);
13373 Set_Analyzed (N, True);
13374 end;
13375 end if;
13376 end Set_Renamed_Subprogram;
13378 ----------------------
13379 -- Side_Effect_Free --
13380 ----------------------
13382 function Side_Effect_Free
13383 (N : Node_Id;
13384 Name_Req : Boolean := False;
13385 Variable_Ref : Boolean := False) return Boolean
13387 Typ : constant Entity_Id := Etype (N);
13388 -- Result type of the expression
13390 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
13391 -- The argument N is a construct where the Prefix is dereferenced if it
13392 -- is an access type and the result is a variable. The call returns True
13393 -- if the construct is side effect free (not considering side effects in
13394 -- other than the prefix which are to be tested by the caller).
13396 function Within_In_Parameter (N : Node_Id) return Boolean;
13397 -- Determines if N is a subcomponent of a composite in-parameter. If so,
13398 -- N is not side-effect free when the actual is global and modifiable
13399 -- indirectly from within a subprogram, because it may be passed by
13400 -- reference. The front-end must be conservative here and assume that
13401 -- this may happen with any array or record type. On the other hand, we
13402 -- cannot create temporaries for all expressions for which this
13403 -- condition is true, for various reasons that might require clearing up
13404 -- ??? For example, discriminant references that appear out of place, or
13405 -- spurious type errors with class-wide expressions. As a result, we
13406 -- limit the transformation to loop bounds, which is so far the only
13407 -- case that requires it.
13409 -----------------------------
13410 -- Safe_Prefixed_Reference --
13411 -----------------------------
13413 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
13414 begin
13415 -- If prefix is not side effect free, definitely not safe
13417 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
13418 return False;
13420 -- If the prefix is of an access type that is not access-to-constant,
13421 -- then this construct is a variable reference, which means it is to
13422 -- be considered to have side effects if Variable_Ref is set True.
13424 elsif Is_Access_Type (Etype (Prefix (N)))
13425 and then not Is_Access_Constant (Etype (Prefix (N)))
13426 and then Variable_Ref
13427 then
13428 -- Exception is a prefix that is the result of a previous removal
13429 -- of side effects.
13431 return Is_Entity_Name (Prefix (N))
13432 and then not Comes_From_Source (Prefix (N))
13433 and then Ekind (Entity (Prefix (N))) = E_Constant
13434 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
13436 -- If the prefix is an explicit dereference then this construct is a
13437 -- variable reference, which means it is to be considered to have
13438 -- side effects if Variable_Ref is True.
13440 -- We do NOT exclude dereferences of access-to-constant types because
13441 -- we handle them as constant view of variables.
13443 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
13444 and then Variable_Ref
13445 then
13446 return False;
13448 -- Note: The following test is the simplest way of solving a complex
13449 -- problem uncovered by the following test (Side effect on loop bound
13450 -- that is a subcomponent of a global variable:
13452 -- with Text_Io; use Text_Io;
13453 -- procedure Tloop is
13454 -- type X is
13455 -- record
13456 -- V : Natural := 4;
13457 -- S : String (1..5) := (others => 'a');
13458 -- end record;
13459 -- X1 : X;
13461 -- procedure Modi;
13463 -- generic
13464 -- with procedure Action;
13465 -- procedure Loop_G (Arg : X; Msg : String)
13467 -- procedure Loop_G (Arg : X; Msg : String) is
13468 -- begin
13469 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
13470 -- & Natural'Image (Arg.V));
13471 -- for Index in 1 .. Arg.V loop
13472 -- Text_Io.Put_Line
13473 -- (Natural'Image (Index) & " " & Arg.S (Index));
13474 -- if Index > 2 then
13475 -- Modi;
13476 -- end if;
13477 -- end loop;
13478 -- Put_Line ("end loop_g " & Msg);
13479 -- end;
13481 -- procedure Loop1 is new Loop_G (Modi);
13482 -- procedure Modi is
13483 -- begin
13484 -- X1.V := 1;
13485 -- Loop1 (X1, "from modi");
13486 -- end;
13488 -- begin
13489 -- Loop1 (X1, "initial");
13490 -- end;
13492 -- The output of the above program should be:
13494 -- begin loop_g initial will loop till: 4
13495 -- 1 a
13496 -- 2 a
13497 -- 3 a
13498 -- begin loop_g from modi will loop till: 1
13499 -- 1 a
13500 -- end loop_g from modi
13501 -- 4 a
13502 -- begin loop_g from modi will loop till: 1
13503 -- 1 a
13504 -- end loop_g from modi
13505 -- end loop_g initial
13507 -- If a loop bound is a subcomponent of a global variable, a
13508 -- modification of that variable within the loop may incorrectly
13509 -- affect the execution of the loop.
13511 elsif Parent_Kind (Parent (N)) = N_Loop_Parameter_Specification
13512 and then Within_In_Parameter (Prefix (N))
13513 and then Variable_Ref
13514 then
13515 return False;
13517 -- All other cases are side effect free
13519 else
13520 return True;
13521 end if;
13522 end Safe_Prefixed_Reference;
13524 -------------------------
13525 -- Within_In_Parameter --
13526 -------------------------
13528 function Within_In_Parameter (N : Node_Id) return Boolean is
13529 begin
13530 if not Comes_From_Source (N) then
13531 return False;
13533 elsif Is_Entity_Name (N) then
13534 return Ekind (Entity (N)) = E_In_Parameter;
13536 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
13537 return Within_In_Parameter (Prefix (N));
13539 else
13540 return False;
13541 end if;
13542 end Within_In_Parameter;
13544 -- Start of processing for Side_Effect_Free
13546 begin
13547 -- If volatile reference, always consider it to have side effects
13549 if Is_Volatile_Reference (N) then
13550 return False;
13551 end if;
13553 -- Note on checks that could raise Constraint_Error. Strictly, if we
13554 -- take advantage of 11.6, these checks do not count as side effects.
13555 -- However, we would prefer to consider that they are side effects,
13556 -- since the back end CSE does not work very well on expressions which
13557 -- can raise Constraint_Error. On the other hand if we don't consider
13558 -- them to be side effect free, then we get some awkward expansions
13559 -- in -gnato mode, resulting in code insertions at a point where we
13560 -- do not have a clear model for performing the insertions.
13562 -- Special handling for entity names
13564 if Is_Entity_Name (N) then
13566 -- A type reference is always side effect free
13568 if Is_Type (Entity (N)) then
13569 return True;
13571 -- Variables are considered to be a side effect if Variable_Ref
13572 -- is set or if we have a volatile reference and Name_Req is off.
13573 -- If Name_Req is True then we can't help returning a name which
13574 -- effectively allows multiple references in any case.
13576 elsif Is_Variable (N, Use_Original_Node => False) then
13577 return not Variable_Ref
13578 and then (not Is_Volatile_Reference (N) or else Name_Req);
13580 -- Any other entity (e.g. a subtype name) is definitely side
13581 -- effect free.
13583 else
13584 return True;
13585 end if;
13587 -- A value known at compile time is always side effect free
13589 elsif Compile_Time_Known_Value (N) then
13590 return True;
13592 -- A variable renaming is not side-effect free, because the renaming
13593 -- will function like a macro in the front-end in some cases, and an
13594 -- assignment can modify the component designated by N, so we need to
13595 -- create a temporary for it.
13597 -- The guard testing for Entity being present is needed at least in
13598 -- the case of rewritten predicate expressions, and may well also be
13599 -- appropriate elsewhere. Obviously we can't go testing the entity
13600 -- field if it does not exist, so it's reasonable to say that this is
13601 -- not the renaming case if it does not exist.
13603 elsif Is_Entity_Name (Original_Node (N))
13604 and then Present (Entity (Original_Node (N)))
13605 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
13606 and then Ekind (Entity (Original_Node (N))) /= E_Constant
13607 then
13608 declare
13609 RO : constant Node_Id :=
13610 Renamed_Object (Entity (Original_Node (N)));
13612 begin
13613 -- If the renamed object is an indexed component, or an
13614 -- explicit dereference, then the designated object could
13615 -- be modified by an assignment.
13617 if Nkind (RO) in N_Indexed_Component | N_Explicit_Dereference then
13618 return False;
13620 -- A selected component must have a safe prefix
13622 elsif Nkind (RO) = N_Selected_Component then
13623 return Safe_Prefixed_Reference (RO);
13625 -- In all other cases, designated object cannot be changed so
13626 -- we are side effect free.
13628 else
13629 return True;
13630 end if;
13631 end;
13633 -- Remove_Side_Effects generates an object renaming declaration to
13634 -- capture the expression of a class-wide expression. In VM targets
13635 -- the frontend performs no expansion for dispatching calls to
13636 -- class- wide types since they are handled by the VM. Hence, we must
13637 -- locate here if this node corresponds to a previous invocation of
13638 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13640 elsif not Tagged_Type_Expansion
13641 and then not Comes_From_Source (N)
13642 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
13643 and then Is_Class_Wide_Type (Typ)
13644 then
13645 return True;
13647 -- Generating C the type conversion of an access to constrained array
13648 -- type into an access to unconstrained array type involves initializing
13649 -- a fat pointer and the expression cannot be assumed to be free of side
13650 -- effects since it must referenced several times to compute its bounds.
13652 elsif Modify_Tree_For_C
13653 and then Nkind (N) = N_Type_Conversion
13654 and then Is_Access_Type (Typ)
13655 and then Is_Array_Type (Designated_Type (Typ))
13656 and then not Is_Constrained (Designated_Type (Typ))
13657 then
13658 return False;
13659 end if;
13661 -- For other than entity names and compile time known values,
13662 -- check the node kind for special processing.
13664 case Nkind (N) is
13666 -- An attribute reference is side-effect free if its expressions
13667 -- are side-effect free and its prefix is side-effect free or is
13668 -- an entity reference.
13670 when N_Attribute_Reference =>
13671 return Side_Effect_Free_Attribute (Attribute_Name (N))
13672 and then
13673 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13674 and then
13675 (Is_Entity_Name (Prefix (N))
13676 or else
13677 Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref));
13679 -- A binary operator is side effect free if and both operands are
13680 -- side effect free. For this purpose binary operators include
13681 -- short circuit forms.
13683 when N_Binary_Op
13684 | N_Short_Circuit
13686 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13687 and then
13688 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13690 -- Membership tests may have either Right_Opnd or Alternatives set
13692 when N_Membership_Test =>
13693 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13694 and then
13695 (if Present (Right_Opnd (N))
13696 then Side_Effect_Free
13697 (Right_Opnd (N), Name_Req, Variable_Ref)
13698 else Side_Effect_Free
13699 (Alternatives (N), Name_Req, Variable_Ref));
13701 -- An explicit dereference is side effect free only if it is
13702 -- a side effect free prefixed reference.
13704 when N_Explicit_Dereference =>
13705 return Safe_Prefixed_Reference (N);
13707 -- An expression with action is side effect free if its expression
13708 -- is side effect free and it has no actions.
13710 when N_Expression_With_Actions =>
13711 return
13712 Is_Empty_List (Actions (N))
13713 and then Side_Effect_Free
13714 (Expression (N), Name_Req, Variable_Ref);
13716 -- A call to _rep_to_pos is side effect free, since we generate
13717 -- this pure function call ourselves. Moreover it is critically
13718 -- important to make this exception, since otherwise we can have
13719 -- discriminants in array components which don't look side effect
13720 -- free in the case of an array whose index type is an enumeration
13721 -- type with an enumeration rep clause.
13723 -- All other function calls are not side effect free
13725 when N_Function_Call =>
13726 return
13727 Nkind (Name (N)) = N_Identifier
13728 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
13729 and then Side_Effect_Free
13730 (First (Parameter_Associations (N)),
13731 Name_Req, Variable_Ref);
13733 -- An IF expression is side effect free if it's of a scalar type, and
13734 -- all its components are all side effect free (conditions and then
13735 -- actions and else actions). We restrict to scalar types, since it
13736 -- is annoying to deal with things like (if A then B else C)'First
13737 -- where the type involved is a string type.
13739 when N_If_Expression =>
13740 return
13741 Is_Scalar_Type (Typ)
13742 and then Side_Effect_Free
13743 (Expressions (N), Name_Req, Variable_Ref);
13745 -- An indexed component is side effect free if it is a side
13746 -- effect free prefixed reference and all the indexing
13747 -- expressions are side effect free.
13749 when N_Indexed_Component =>
13750 return
13751 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13752 and then Safe_Prefixed_Reference (N);
13754 -- A type qualification, type conversion, or unchecked expression is
13755 -- side effect free if the expression is side effect free.
13757 when N_Qualified_Expression
13758 | N_Type_Conversion
13759 | N_Unchecked_Expression
13761 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13763 -- A selected component is side effect free only if it is a side
13764 -- effect free prefixed reference.
13766 when N_Selected_Component =>
13767 return Safe_Prefixed_Reference (N);
13769 -- A range is side effect free if the bounds are side effect free
13771 when N_Range =>
13772 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13773 and then
13774 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13776 -- A slice is side effect free if it is a side effect free
13777 -- prefixed reference and the bounds are side effect free.
13779 when N_Slice =>
13780 return
13781 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13782 and then Safe_Prefixed_Reference (N);
13784 -- A unary operator is side effect free if the operand
13785 -- is side effect free.
13787 when N_Unary_Op =>
13788 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13790 -- An unchecked type conversion is side effect free only if it
13791 -- is safe and its argument is side effect free.
13793 when N_Unchecked_Type_Conversion =>
13794 return
13795 Safe_Unchecked_Type_Conversion (N)
13796 and then Side_Effect_Free
13797 (Expression (N), Name_Req, Variable_Ref);
13799 -- A literal is side effect free
13801 when N_Character_Literal
13802 | N_Integer_Literal
13803 | N_Real_Literal
13804 | N_String_Literal
13806 return True;
13808 -- An aggregate is side effect free if all its values are compile
13809 -- time known.
13811 when N_Aggregate =>
13812 return Compile_Time_Known_Aggregate (N);
13814 -- We consider that anything else has side effects. This is a bit
13815 -- crude, but we are pretty close for most common cases, and we
13816 -- are certainly correct (i.e. we never return True when the
13817 -- answer should be False).
13819 when others =>
13820 return False;
13821 end case;
13822 end Side_Effect_Free;
13824 -- A list is side effect free if all elements of the list are side
13825 -- effect free.
13827 function Side_Effect_Free
13828 (L : List_Id;
13829 Name_Req : Boolean := False;
13830 Variable_Ref : Boolean := False) return Boolean
13832 N : Node_Id;
13834 begin
13835 if L = No_List or else L = Error_List then
13836 return True;
13838 else
13839 N := First (L);
13840 while Present (N) loop
13841 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13842 return False;
13843 else
13844 Next (N);
13845 end if;
13846 end loop;
13848 return True;
13849 end if;
13850 end Side_Effect_Free;
13852 --------------------------------
13853 -- Side_Effect_Free_Attribute --
13854 --------------------------------
13856 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean is
13857 begin
13858 case Name is
13859 when Name_Input =>
13860 return False;
13862 when Name_Image
13863 | Name_Img
13864 | Name_Wide_Image
13865 | Name_Wide_Wide_Image
13867 -- CodePeer doesn't want to see replicated copies of 'Image calls
13869 return not CodePeer_Mode;
13871 when others =>
13872 return True;
13873 end case;
13874 end Side_Effect_Free_Attribute;
13876 ----------------------------------
13877 -- Silly_Boolean_Array_Not_Test --
13878 ----------------------------------
13880 -- This procedure implements an odd and silly test. We explicitly check
13881 -- for the case where the 'First of the component type is equal to the
13882 -- 'Last of this component type, and if this is the case, we make sure
13883 -- that constraint error is raised. The reason is that the NOT is bound
13884 -- to cause CE in this case, and we will not otherwise catch it.
13886 -- No such check is required for AND and OR, since for both these cases
13887 -- False op False = False, and True op True = True. For the XOR case,
13888 -- see Silly_Boolean_Array_Xor_Test.
13890 -- Believe it or not, this was reported as a bug. Note that nearly always,
13891 -- the test will evaluate statically to False, so the code will be
13892 -- statically removed, and no extra overhead caused.
13894 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13895 Loc : constant Source_Ptr := Sloc (N);
13896 CT : constant Entity_Id := Component_Type (T);
13898 begin
13899 -- The check we install is
13901 -- constraint_error when
13902 -- component_type'first = component_type'last
13903 -- and then array_type'Length /= 0)
13905 -- We need the last guard because we don't want to raise CE for empty
13906 -- arrays since no out of range values result. (Empty arrays with a
13907 -- component type of True .. True -- very useful -- even the ACATS
13908 -- does not test that marginal case).
13910 Insert_Action (N,
13911 Make_Raise_Constraint_Error (Loc,
13912 Condition =>
13913 Make_And_Then (Loc,
13914 Left_Opnd =>
13915 Make_Op_Eq (Loc,
13916 Left_Opnd =>
13917 Make_Attribute_Reference (Loc,
13918 Prefix => New_Occurrence_Of (CT, Loc),
13919 Attribute_Name => Name_First),
13921 Right_Opnd =>
13922 Make_Attribute_Reference (Loc,
13923 Prefix => New_Occurrence_Of (CT, Loc),
13924 Attribute_Name => Name_Last)),
13926 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13927 Reason => CE_Range_Check_Failed));
13928 end Silly_Boolean_Array_Not_Test;
13930 ----------------------------------
13931 -- Silly_Boolean_Array_Xor_Test --
13932 ----------------------------------
13934 -- This procedure implements an odd and silly test. We explicitly check
13935 -- for the XOR case where the component type is True .. True, since this
13936 -- will raise constraint error. A special check is required since CE
13937 -- will not be generated otherwise (cf Expand_Packed_Not).
13939 -- No such check is required for AND and OR, since for both these cases
13940 -- False op False = False, and True op True = True, and no check is
13941 -- required for the case of False .. False, since False xor False = False.
13942 -- See also Silly_Boolean_Array_Not_Test
13944 procedure Silly_Boolean_Array_Xor_Test
13945 (N : Node_Id;
13946 R : Node_Id;
13947 T : Entity_Id)
13949 Loc : constant Source_Ptr := Sloc (N);
13950 CT : constant Entity_Id := Component_Type (T);
13952 begin
13953 -- The check we install is
13955 -- constraint_error when
13956 -- Boolean (component_type'First)
13957 -- and then Boolean (component_type'Last)
13958 -- and then array_type'Length /= 0)
13960 -- We need the last guard because we don't want to raise CE for empty
13961 -- arrays since no out of range values result (Empty arrays with a
13962 -- component type of True .. True -- very useful -- even the ACATS
13963 -- does not test that marginal case).
13965 Insert_Action (N,
13966 Make_Raise_Constraint_Error (Loc,
13967 Condition =>
13968 Make_And_Then (Loc,
13969 Left_Opnd =>
13970 Make_And_Then (Loc,
13971 Left_Opnd =>
13972 Convert_To (Standard_Boolean,
13973 Make_Attribute_Reference (Loc,
13974 Prefix => New_Occurrence_Of (CT, Loc),
13975 Attribute_Name => Name_First)),
13977 Right_Opnd =>
13978 Convert_To (Standard_Boolean,
13979 Make_Attribute_Reference (Loc,
13980 Prefix => New_Occurrence_Of (CT, Loc),
13981 Attribute_Name => Name_Last))),
13983 Right_Opnd => Make_Non_Empty_Check (Loc, R)),
13984 Reason => CE_Range_Check_Failed));
13985 end Silly_Boolean_Array_Xor_Test;
13987 ----------------------------
13988 -- Small_Integer_Type_For --
13989 ----------------------------
13991 function Small_Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id
13993 begin
13994 pragma Assert (S <= System_Max_Integer_Size);
13996 if S <= Standard_Short_Short_Integer_Size then
13997 if Uns then
13998 return Standard_Short_Short_Unsigned;
13999 else
14000 return Standard_Short_Short_Integer;
14001 end if;
14003 elsif S <= Standard_Short_Integer_Size then
14004 if Uns then
14005 return Standard_Short_Unsigned;
14006 else
14007 return Standard_Short_Integer;
14008 end if;
14010 elsif S <= Standard_Integer_Size then
14011 if Uns then
14012 return Standard_Unsigned;
14013 else
14014 return Standard_Integer;
14015 end if;
14017 elsif S <= Standard_Long_Integer_Size then
14018 if Uns then
14019 return Standard_Long_Unsigned;
14020 else
14021 return Standard_Long_Integer;
14022 end if;
14024 elsif S <= Standard_Long_Long_Integer_Size then
14025 if Uns then
14026 return Standard_Long_Long_Unsigned;
14027 else
14028 return Standard_Long_Long_Integer;
14029 end if;
14031 elsif S <= Standard_Long_Long_Long_Integer_Size then
14032 if Uns then
14033 return Standard_Long_Long_Long_Unsigned;
14034 else
14035 return Standard_Long_Long_Long_Integer;
14036 end if;
14038 else
14039 raise Program_Error;
14040 end if;
14041 end Small_Integer_Type_For;
14043 -------------------
14044 -- Type_Map_Hash --
14045 -------------------
14047 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
14048 begin
14049 return Type_Map_Header (Id mod Type_Map_Size);
14050 end Type_Map_Hash;
14052 ------------------------------------------
14053 -- Type_May_Have_Bit_Aligned_Components --
14054 ------------------------------------------
14056 function Type_May_Have_Bit_Aligned_Components
14057 (Typ : Entity_Id) return Boolean
14059 begin
14060 -- Array type, check component type
14062 if Is_Array_Type (Typ) then
14063 return
14064 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
14066 -- Record type, check components
14068 elsif Is_Record_Type (Typ) then
14069 declare
14070 E : Entity_Id;
14072 begin
14073 E := First_Component_Or_Discriminant (Typ);
14074 while Present (E) loop
14075 -- This is the crucial test: if the component itself causes
14076 -- trouble, then we can stop and return True.
14078 if Component_May_Be_Bit_Aligned (E) then
14079 return True;
14080 end if;
14082 -- Otherwise, we need to test its type, to see if it may
14083 -- itself contain a troublesome component.
14085 if Type_May_Have_Bit_Aligned_Components (Etype (E)) then
14086 return True;
14087 end if;
14089 Next_Component_Or_Discriminant (E);
14090 end loop;
14092 return False;
14093 end;
14095 -- Type other than array or record is always OK
14097 else
14098 return False;
14099 end if;
14100 end Type_May_Have_Bit_Aligned_Components;
14102 -------------------------------
14103 -- Update_Primitives_Mapping --
14104 -------------------------------
14106 procedure Update_Primitives_Mapping
14107 (Inher_Id : Entity_Id;
14108 Subp_Id : Entity_Id)
14110 Parent_Type : constant Entity_Id := Find_Dispatching_Type (Inher_Id);
14111 Derived_Type : constant Entity_Id := Find_Dispatching_Type (Subp_Id);
14113 begin
14114 pragma Assert (Parent_Type /= Derived_Type);
14115 Map_Types (Parent_Type, Derived_Type);
14116 end Update_Primitives_Mapping;
14118 ----------------------------------
14119 -- Within_Case_Or_If_Expression --
14120 ----------------------------------
14122 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
14123 Par : Node_Id;
14125 begin
14126 -- Locate an enclosing case or if expression. Note that these constructs
14127 -- can be expanded into Expression_With_Actions, hence the test of the
14128 -- original node.
14130 Par := Parent (N);
14131 while Present (Par) loop
14132 if Nkind (Original_Node (Par)) in N_Case_Expression | N_If_Expression
14133 then
14134 return True;
14136 -- Prevent the search from going too far
14138 elsif Is_Body_Or_Package_Declaration (Par) then
14139 return False;
14140 end if;
14142 Par := Parent (Par);
14143 end loop;
14145 return False;
14146 end Within_Case_Or_If_Expression;
14148 ------------------------------
14149 -- Predicate_Check_In_Scope --
14150 ------------------------------
14152 function Predicate_Check_In_Scope (N : Node_Id) return Boolean is
14153 S : Entity_Id;
14155 begin
14156 S := Current_Scope;
14157 while Present (S) and then not Is_Subprogram (S) loop
14158 S := Scope (S);
14159 end loop;
14161 if Present (S) then
14163 -- Predicate checks should only be enabled in init procs for
14164 -- expressions coming from source.
14166 if Is_Init_Proc (S) then
14167 return Comes_From_Source (N);
14169 elsif Get_TSS_Name (S) /= TSS_Null
14170 and then not Is_Predicate_Function (S)
14171 and then not Is_Predicate_Function_M (S)
14172 then
14173 return False;
14174 end if;
14175 end if;
14177 return True;
14178 end Predicate_Check_In_Scope;
14180 end Exp_Util;