hppa: Fix LO_SUM DLTIND14R address support in PRINT_OPERAND_ADDRESS
[official-gcc.git] / gcc / ada / exp_util.adb
blob04d114694ab658395a33cf05da965313431eee9e
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-2024, 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 Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Aggr; use Exp_Aggr;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch11; use Exp_Ch11;
39 with Freeze; use Freeze;
40 with Ghost; use Ghost;
41 with Inline; use Inline;
42 with Itypes; use Itypes;
43 with Lib; use Lib;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch12; use Sem_Ch12;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Elab; use Sem_Elab;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
61 with Sem_Util; use Sem_Util;
62 with Sinfo.Utils; use Sinfo.Utils;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Stringt; use Stringt;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Validsw; use Validsw;
69 with Warnsw; use Warnsw;
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_Uninitialized_Aggregate
171 (Exp : Node_Id;
172 T : Entity_Id) return Boolean;
173 -- Determine whether an array aggregate used in an object declaration
174 -- is uninitialized, when the aggregate is declared with a box and
175 -- the component type has no default value. Such an aggregate can be
176 -- optimized away to prevent the copying of uninitialized data, and
177 -- the bounds of the aggregate can be propagated directly to the
178 -- object declaration.
180 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
181 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
182 -- an assertion expression that should be verified at run time.
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
332 function Is_Hardbool_Type (T : Entity_Id) return Boolean;
333 -- Return True iff T is a type annotated with the
334 -- Machine_Attribute pragma "hardbool".
336 ----------------------
337 -- Is_Hardbool_Type --
338 ----------------------
340 function Is_Hardbool_Type (T : Entity_Id) return Boolean is
342 function Find_Hardbool_Pragma
343 (Id : Entity_Id) return Node_Id;
344 -- Return a Rep_Item associated with entity Id that
345 -- corresponds to the Hardbool Machine_Attribute pragma, if
346 -- any, or Empty otherwise.
348 function Pragma_Arg_To_String (Item : Node_Id) return String is
349 (To_String (Strval (Expr_Value_S (Item))));
350 -- Return the pragma argument Item as a String
352 function Hardbool_Pragma_P (Item : Node_Id) return Boolean is
353 (Nkind (Item) = N_Pragma
354 and then
355 Pragma_Name (Item) = Name_Machine_Attribute
356 and then
357 Pragma_Arg_To_String
358 (Get_Pragma_Arg
359 (Next (First (Pragma_Argument_Associations (Item)))))
360 = "hardbool");
361 -- Return True iff representation Item is a "hardbool"
362 -- Machine_Attribute pragma.
364 --------------------------
365 -- Find_Hardbool_Pragma --
366 --------------------------
368 function Find_Hardbool_Pragma
369 (Id : Entity_Id) return Node_Id
371 Item : Node_Id;
373 begin
374 if not Has_Gigi_Rep_Item (Id) then
375 return Empty;
376 end if;
378 Item := First_Rep_Item (Id);
379 while Present (Item) loop
380 if Hardbool_Pragma_P (Item) then
381 return Item;
382 end if;
383 Item := Next_Rep_Item (Item);
384 end loop;
386 return Empty;
387 end Find_Hardbool_Pragma;
389 -- Start of processing for Is_Hardbool_Type
391 begin
392 return Present (Find_Hardbool_Pragma (T));
393 end Is_Hardbool_Type;
395 -- Start of processing for Adjust_Condition
397 begin
398 if No (N) then
399 return;
400 end if;
402 declare
403 Loc : constant Source_Ptr := Sloc (N);
404 T : constant Entity_Id := Etype (N);
406 begin
407 -- Defend against a call where the argument has no type, or has a
408 -- type that is not Boolean. This can occur because of prior errors.
410 if No (T) or else not Is_Boolean_Type (T) then
411 return;
412 end if;
414 -- Apply validity checking if needed
416 if Validity_Checks_On
417 and then
418 (Validity_Check_Tests or else Is_Hardbool_Type (T))
419 then
420 Ensure_Valid (N);
421 end if;
423 -- Immediate return if standard boolean, the most common case,
424 -- where nothing needs to be done.
426 if Base_Type (T) = Standard_Boolean then
427 return;
428 end if;
430 -- Case of zero/nonzero semantics or nonstandard enumeration
431 -- representation. In each case, we rewrite the node as:
433 -- ityp!(N) /= False'Enum_Rep
435 -- where ityp is an integer type with large enough size to hold any
436 -- value of type T.
438 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
439 Rewrite (N,
440 Make_Op_Ne (Loc,
441 Left_Opnd =>
442 Unchecked_Convert_To
443 (Integer_Type_For (Esize (T), Uns => False), N),
444 Right_Opnd =>
445 Make_Attribute_Reference (Loc,
446 Attribute_Name => Name_Enum_Rep,
447 Prefix =>
448 New_Occurrence_Of (First_Literal (T), Loc))));
449 Analyze_And_Resolve (N, Standard_Boolean);
451 else
452 Rewrite (N, Convert_To (Standard_Boolean, N));
453 Analyze_And_Resolve (N, Standard_Boolean);
454 end if;
455 end;
456 end Adjust_Condition;
458 ------------------------
459 -- Adjust_Result_Type --
460 ------------------------
462 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
463 begin
464 -- Ignore call if current type is not Standard.Boolean
466 if Etype (N) /= Standard_Boolean then
467 return;
468 end if;
470 -- If result is already of correct type, nothing to do. Note that
471 -- this will get the most common case where everything has a type
472 -- of Standard.Boolean.
474 if Base_Type (T) = Standard_Boolean then
475 return;
477 else
478 declare
479 KP : constant Node_Kind := Nkind (Parent (N));
481 begin
482 -- If result is to be used as a Condition in the syntax, no need
483 -- to convert it back, since if it was changed to Standard.Boolean
484 -- using Adjust_Condition, that is just fine for this usage.
486 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
487 return;
489 -- If result is an operand of another logical operation, no need
490 -- to reset its type, since Standard.Boolean is just fine, and
491 -- such operations always do Adjust_Condition on their operands.
493 elsif KP in N_Op_Boolean
494 or else KP in N_Short_Circuit
495 or else KP = N_Op_Not
496 or else (KP in N_Type_Conversion
497 | N_Unchecked_Type_Conversion
498 and then Is_Boolean_Type (Etype (Parent (N))))
499 then
500 return;
502 -- Otherwise we perform a conversion from the current type, which
503 -- must be Standard.Boolean, to the desired type. Use the base
504 -- type to prevent spurious constraint checks that are extraneous
505 -- to the transformation. The type and its base have the same
506 -- representation, standard or otherwise.
508 else
509 Set_Analyzed (N);
510 Rewrite (N, Convert_To (Base_Type (T), N));
511 Analyze_And_Resolve (N, Base_Type (T));
512 end if;
513 end;
514 end if;
515 end Adjust_Result_Type;
517 --------------------------
518 -- Append_Freeze_Action --
519 --------------------------
521 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
522 Fnode : Node_Id;
524 begin
525 Ensure_Freeze_Node (T);
526 Fnode := Freeze_Node (T);
528 if No (Actions (Fnode)) then
529 Set_Actions (Fnode, New_List (N));
530 else
531 Append (N, Actions (Fnode));
532 end if;
533 end Append_Freeze_Action;
535 ---------------------------
536 -- Append_Freeze_Actions --
537 ---------------------------
539 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
540 Fnode : Node_Id;
542 begin
543 if No (L) then
544 return;
545 end if;
547 Ensure_Freeze_Node (T);
548 Fnode := Freeze_Node (T);
550 if No (Actions (Fnode)) then
551 Set_Actions (Fnode, L);
552 else
553 Append_List (L, Actions (Fnode));
554 end if;
555 end Append_Freeze_Actions;
557 ----------------------------------------
558 -- Attribute_Constrained_Static_Value --
559 ----------------------------------------
561 function Attribute_Constrained_Static_Value (Pref : Node_Id) return Boolean
563 Ptyp : constant Entity_Id := Etype (Pref);
564 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
566 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
567 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
568 -- view of an aliased object whose subtype is constrained.
570 ---------------------------------
571 -- Is_Constrained_Aliased_View --
572 ---------------------------------
574 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
575 E : Entity_Id;
577 begin
578 if Is_Entity_Name (Obj) then
579 E := Entity (Obj);
581 if Present (Renamed_Object (E)) then
582 return Is_Constrained_Aliased_View (Renamed_Object (E));
583 else
584 return Is_Aliased (E) and then Is_Constrained (Etype (E));
585 end if;
587 else
588 return Is_Aliased_View (Obj)
589 and then
590 (Is_Constrained (Etype (Obj))
591 or else
592 (Nkind (Obj) = N_Explicit_Dereference
593 and then
594 not Object_Type_Has_Constrained_Partial_View
595 (Typ => Base_Type (Etype (Obj)),
596 Scop => Current_Scope)));
597 end if;
598 end Is_Constrained_Aliased_View;
600 -- Start of processing for Attribute_Constrained_Static_Value
602 begin
603 -- We are in a case where the attribute is known statically, and
604 -- implicit dereferences have been rewritten.
606 pragma Assert
607 (not (Present (Formal_Ent)
608 and then Ekind (Formal_Ent) /= E_Constant
609 and then Present (Extra_Constrained (Formal_Ent)))
610 and then
611 not (Is_Access_Type (Etype (Pref))
612 and then (not Is_Entity_Name (Pref)
613 or else Is_Object (Entity (Pref))))
614 and then
615 not (Nkind (Pref) = N_Identifier
616 and then Ekind (Entity (Pref)) = E_Variable
617 and then Present (Extra_Constrained (Entity (Pref)))));
619 if Is_Entity_Name (Pref) then
620 declare
621 Ent : constant Entity_Id := Entity (Pref);
622 Res : Boolean;
624 begin
625 -- (RM J.4) obsolescent cases
627 if Is_Type (Ent) then
629 -- Private type
631 if Is_Private_Type (Ent) then
632 Res := not Has_Discriminants (Ent)
633 or else Is_Constrained (Ent);
635 -- It not a private type, must be a generic actual type
636 -- that corresponded to a private type. We know that this
637 -- correspondence holds, since otherwise the reference
638 -- within the generic template would have been illegal.
640 else
641 if Is_Composite_Type (Underlying_Type (Ent)) then
642 Res := Is_Constrained (Ent);
643 else
644 Res := True;
645 end if;
646 end if;
648 else
650 -- If the prefix is not a variable or is aliased, then
651 -- definitely true; if it's a formal parameter without an
652 -- associated extra formal, then treat it as constrained.
654 -- Ada 2005 (AI-363): An aliased prefix must be known to be
655 -- constrained in order to set the attribute to True.
657 if not Is_Variable (Pref)
658 or else Present (Formal_Ent)
659 or else (Ada_Version < Ada_2005
660 and then Is_Aliased_View (Pref))
661 or else (Ada_Version >= Ada_2005
662 and then Is_Constrained_Aliased_View (Pref))
663 then
664 Res := True;
666 -- Variable case, look at type to see if it is constrained.
667 -- Note that the one case where this is not accurate (the
668 -- procedure formal case), has been handled above.
670 -- We use the Underlying_Type here (and below) in case the
671 -- type is private without discriminants, but the full type
672 -- has discriminants. This case is illegal, but we generate
673 -- it internally for passing to the Extra_Constrained
674 -- parameter.
676 else
677 -- In Ada 2012, test for case of a limited tagged type,
678 -- in which case the attribute is always required to
679 -- return True. The underlying type is tested, to make
680 -- sure we also return True for cases where there is an
681 -- unconstrained object with an untagged limited partial
682 -- view which has defaulted discriminants (such objects
683 -- always produce a False in earlier versions of
684 -- Ada). (Ada 2012: AI05-0214)
686 Res :=
687 Is_Constrained (Underlying_Type (Etype (Ent)))
688 or else
689 (Ada_Version >= Ada_2012
690 and then Is_Tagged_Type (Underlying_Type (Ptyp))
691 and then Is_Limited_Type (Ptyp));
692 end if;
693 end if;
695 return Res;
696 end;
698 -- Prefix is not an entity name. These are also cases where we can
699 -- always tell at compile time by looking at the form and type of the
700 -- prefix. If an explicit dereference of an object with constrained
701 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
702 -- underlying type is a limited tagged type, then Constrained is
703 -- required to always return True (Ada 2012: AI05-0214).
705 else
706 return not Is_Variable (Pref)
707 or else
708 (Nkind (Pref) = N_Explicit_Dereference
709 and then
710 not Object_Type_Has_Constrained_Partial_View
711 (Typ => Base_Type (Ptyp),
712 Scop => Current_Scope))
713 or else Is_Constrained (Underlying_Type (Ptyp))
714 or else (Ada_Version >= Ada_2012
715 and then Is_Tagged_Type (Underlying_Type (Ptyp))
716 and then Is_Limited_Type (Ptyp));
717 end if;
718 end Attribute_Constrained_Static_Value;
720 ------------------------------------
721 -- Build_Allocate_Deallocate_Proc --
722 ------------------------------------
724 procedure Build_Allocate_Deallocate_Proc
725 (N : Node_Id;
726 Is_Allocate : Boolean)
728 function Find_Object (E : Node_Id) return Node_Id;
729 -- Given an arbitrary expression of an allocator, try to find an object
730 -- reference in it, otherwise return the original expression.
732 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
733 -- Determine whether subprogram Subp denotes a custom allocate or
734 -- deallocate.
736 -----------------
737 -- Find_Object --
738 -----------------
740 function Find_Object (E : Node_Id) return Node_Id is
741 Expr : Node_Id;
743 begin
744 pragma Assert (Is_Allocate);
746 Expr := E;
747 loop
748 if Nkind (Expr) = N_Explicit_Dereference then
749 Expr := Prefix (Expr);
751 elsif Nkind (Expr) = N_Qualified_Expression then
752 Expr := Expression (Expr);
754 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
756 -- When interface class-wide types are involved in allocation,
757 -- the expander introduces several levels of address arithmetic
758 -- to perform dispatch table displacement. In this scenario the
759 -- object appears as:
761 -- Tag_Ptr (Base_Address (<object>'Address))
763 -- Detect this case and utilize the whole expression as the
764 -- "object" since it now points to the proper dispatch table.
766 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
767 exit;
769 -- Continue to strip the object
771 else
772 Expr := Expression (Expr);
773 end if;
775 else
776 exit;
777 end if;
778 end loop;
780 return Expr;
781 end Find_Object;
783 ---------------------------------
784 -- Is_Allocate_Deallocate_Proc --
785 ---------------------------------
787 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
788 begin
789 -- Look for a subprogram body with only one statement which is a
790 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
792 if Ekind (Subp) = E_Procedure
793 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
794 then
795 declare
796 HSS : constant Node_Id :=
797 Handled_Statement_Sequence (Parent (Parent (Subp)));
798 Proc : Entity_Id;
800 begin
801 if Present (Statements (HSS))
802 and then Nkind (First (Statements (HSS))) =
803 N_Procedure_Call_Statement
804 then
805 Proc := Entity (Name (First (Statements (HSS))));
807 return
808 Is_RTE (Proc, RE_Allocate_Any_Controlled)
809 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
810 end if;
811 end;
812 end if;
814 return False;
815 end Is_Allocate_Deallocate_Proc;
817 -- Local variables
819 Desig_Typ : Entity_Id;
820 Expr : Node_Id;
821 Needs_Fin : Boolean;
822 Pool_Id : Entity_Id;
823 Proc_To_Call : Node_Id := Empty;
824 Ptr_Typ : Entity_Id;
825 Use_Secondary_Stack_Pool : Boolean;
827 -- Start of processing for Build_Allocate_Deallocate_Proc
829 begin
830 -- Obtain the attributes of the allocation / deallocation
832 if Nkind (N) = N_Free_Statement then
833 Expr := Expression (N);
834 Ptr_Typ := Base_Type (Etype (Expr));
835 Proc_To_Call := Procedure_To_Call (N);
837 else
838 if Nkind (N) = N_Object_Declaration then
839 Expr := Expression (N);
840 else
841 Expr := N;
842 end if;
844 -- In certain cases an allocator with a qualified expression may
845 -- be relocated and used as the initialization expression of a
846 -- temporary:
848 -- before:
849 -- Obj : Ptr_Typ := new Desig_Typ'(...);
851 -- after:
852 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
853 -- Obj : Ptr_Typ := Tmp;
855 -- Since the allocator is always marked as analyzed to avoid infinite
856 -- expansion, it will never be processed by this routine given that
857 -- the designated type needs finalization actions. Detect this case
858 -- and complete the expansion of the allocator.
860 if Nkind (Expr) = N_Identifier
861 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
862 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
863 then
864 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
865 return;
866 end if;
868 -- The allocator may have been rewritten into something else in which
869 -- case the expansion performed by this routine does not apply.
871 if Nkind (Expr) /= N_Allocator then
872 return;
873 end if;
875 Ptr_Typ := Base_Type (Etype (Expr));
876 Proc_To_Call := Procedure_To_Call (Expr);
877 end if;
879 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
880 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
882 -- Handle concurrent types
884 if Is_Concurrent_Type (Desig_Typ)
885 and then Present (Corresponding_Record_Type (Desig_Typ))
886 then
887 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
888 end if;
890 Use_Secondary_Stack_Pool :=
891 Is_RTE (Pool_Id, RE_SS_Pool)
892 or else (Nkind (Expr) = N_Allocator
893 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool));
895 -- Do not process allocations / deallocations without a pool
897 if No (Pool_Id) then
898 return;
900 -- Do not process allocations from the return stack
902 elsif Is_RTE (Pool_Id, RE_RS_Pool) then
903 return;
905 -- Do not process allocations on / deallocations from the secondary
906 -- stack, except for access types used to implement indirect temps.
908 elsif Use_Secondary_Stack_Pool
909 and then not Old_Attr_Util.Indirect_Temps
910 .Is_Access_Type_For_Indirect_Temp (Ptr_Typ)
911 then
912 return;
914 -- Optimize the case where we are using the default Global_Pool_Object,
915 -- and we don't need the heavy finalization machinery.
917 elsif Is_RTE (Pool_Id, RE_Global_Pool_Object)
918 and then not Needs_Finalization (Desig_Typ)
919 then
920 return;
922 -- Do not replicate the machinery if the allocator / free has already
923 -- been expanded and has a custom Allocate / Deallocate.
925 elsif Present (Proc_To_Call)
926 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
927 then
928 return;
929 end if;
931 -- Finalization actions are required when the object to be allocated or
932 -- deallocated needs these actions and the associated access type is not
933 -- subject to pragma No_Heap_Finalization.
935 Needs_Fin :=
936 Needs_Finalization (Desig_Typ)
937 and then not No_Heap_Finalization (Ptr_Typ);
939 -- The allocation/deallocation of a controlled object must be associated
940 -- with an attachment to/detachment from a finalization master, but the
941 -- implementation cannot guarantee this property for every anonymous
942 -- access tyoe, see Build_Anonymous_Collection.
944 if Needs_Fin and then No (Finalization_Master (Ptr_Typ)) then
945 pragma Assert (Ekind (Ptr_Typ) = E_Anonymous_Access_Type);
946 Needs_Fin := False;
947 end if;
949 if Needs_Fin then
951 -- Do nothing if the access type may never allocate / deallocate
952 -- objects.
954 if No_Pool_Assigned (Ptr_Typ) then
955 return;
956 end if;
958 -- The only other kind of allocation / deallocation supported by this
959 -- routine is on / from a subpool.
961 elsif Nkind (Expr) = N_Allocator
962 and then No (Subpool_Handle_Name (Expr))
963 then
964 return;
965 end if;
967 declare
968 Loc : constant Source_Ptr := Sloc (N);
969 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
970 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
971 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
972 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
974 Actuals : List_Id;
975 Alloc_Nod : Node_Id := Empty;
976 Alloc_Expr : Node_Id := Empty;
977 Fin_Addr_Id : Entity_Id;
978 Fin_Mas_Act : Node_Id;
979 Fin_Mas_Id : Entity_Id;
980 Proc_To_Call : Entity_Id;
981 Subpool : Node_Id := Empty;
983 begin
984 -- When we are building an allocator procedure, extract the allocator
985 -- node for later processing and calculation of alignment.
987 if Is_Allocate then
989 if Nkind (Expr) = N_Allocator then
990 Alloc_Nod := Expr;
992 -- When Expr is an object declaration we have to examine its
993 -- expression.
995 elsif Nkind (Expr) = N_Object_Declaration
996 and then Nkind (Expression (Expr)) = N_Allocator
997 then
998 Alloc_Nod := Expression (Expr);
1000 -- Otherwise, we raise an error because we should have found one
1002 else
1003 raise Program_Error;
1004 end if;
1006 -- Extract the qualified expression if there is one from the
1007 -- allocator.
1009 if Nkind (Expression (Alloc_Nod)) = N_Qualified_Expression then
1010 Alloc_Expr := Expression (Alloc_Nod);
1011 end if;
1012 end if;
1014 -- Step 1: Construct all the actuals for the call to library routine
1015 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
1017 -- a) Storage pool
1019 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
1021 if Is_Allocate then
1023 -- b) Subpool
1025 if Nkind (Expr) = N_Allocator then
1026 Subpool := Subpool_Handle_Name (Expr);
1027 end if;
1029 -- If a subpool is present it can be an arbitrary name, so make
1030 -- the actual by copying the tree.
1032 if Present (Subpool) then
1033 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
1034 else
1035 Append_To (Actuals, Make_Null (Loc));
1036 end if;
1038 -- c) Finalization master
1040 if Needs_Fin then
1041 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
1042 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
1044 -- Handle the case where the master is actually a pointer to a
1045 -- master. This case arises in build-in-place functions.
1047 if Is_Access_Type (Etype (Fin_Mas_Id)) then
1048 Append_To (Actuals, Fin_Mas_Act);
1049 else
1050 Append_To (Actuals,
1051 Make_Attribute_Reference (Loc,
1052 Prefix => Fin_Mas_Act,
1053 Attribute_Name => Name_Unrestricted_Access));
1054 end if;
1055 else
1056 Append_To (Actuals, Make_Null (Loc));
1057 end if;
1059 -- d) Finalize_Address
1061 -- Primitive Finalize_Address is never generated in CodePeer mode
1062 -- since it contains an Unchecked_Conversion.
1064 if Needs_Fin and then not CodePeer_Mode then
1065 Fin_Addr_Id := Finalize_Address (Desig_Typ);
1066 pragma Assert (Present (Fin_Addr_Id));
1068 Append_To (Actuals,
1069 Make_Attribute_Reference (Loc,
1070 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
1071 Attribute_Name => Name_Unrestricted_Access));
1072 else
1073 Append_To (Actuals, Make_Null (Loc));
1074 end if;
1075 end if;
1077 -- e) Address
1078 -- f) Storage_Size
1079 -- g) Alignment
1081 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
1082 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
1084 -- Class-wide allocations without expressions and non-class-wide
1085 -- allocations can be performed without getting the alignment from
1086 -- the type's Type Specific Record.
1088 if ((Is_Allocate and then No (Alloc_Expr))
1089 or else
1090 not Is_Class_Wide_Type (Desig_Typ))
1091 and then not Use_Secondary_Stack_Pool
1092 then
1093 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
1095 -- For operations on class-wide types we obtain the value of
1096 -- alignment from the Type Specific Record of the relevant object.
1097 -- This is needed because the frontend expansion of class-wide types
1098 -- into equivalent types confuses the back end.
1100 else
1101 -- Generate:
1102 -- Obj.all'Alignment
1103 -- or
1104 -- Alloc_Expr'Alignment
1106 -- ... because 'Alignment applied to class-wide types is expanded
1107 -- into the code that reads the value of alignment from the TSD
1108 -- (see Expand_N_Attribute_Reference)
1110 -- In the Use_Secondary_Stack_Pool case, Alig_Id is not
1111 -- passed in and therefore must not be referenced.
1113 Append_To (Actuals,
1114 Unchecked_Convert_To (RTE (RE_Storage_Offset),
1115 Make_Attribute_Reference (Loc,
1116 Prefix =>
1117 (if No (Alloc_Expr) then
1118 Make_Explicit_Dereference (Loc, Relocate_Node (Expr))
1119 else
1120 Relocate_Node (Expression (Alloc_Expr))),
1121 Attribute_Name => Name_Alignment)));
1122 end if;
1124 -- h) Is_Controlled
1126 if Needs_Fin then
1127 Is_Controlled : declare
1128 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
1129 Flag_Expr : Node_Id;
1130 Param : Node_Id;
1131 Pref : Node_Id;
1132 Temp : Node_Id;
1134 begin
1135 if Is_Allocate then
1136 Temp := Find_Object (Expression (Expr));
1137 else
1138 Temp := Expr;
1139 end if;
1141 -- Processing for allocations where the expression is a subtype
1142 -- indication.
1144 if Is_Allocate
1145 and then Is_Entity_Name (Temp)
1146 and then Is_Type (Entity (Temp))
1147 then
1148 Flag_Expr :=
1149 New_Occurrence_Of
1150 (Boolean_Literals
1151 (Needs_Finalization (Entity (Temp))), Loc);
1153 -- The allocation / deallocation of a class-wide object relies
1154 -- on a runtime check to determine whether the object is truly
1155 -- controlled or not. Depending on this check, the finalization
1156 -- machinery will request or reclaim extra storage reserved for
1157 -- a list header.
1159 elsif Is_Class_Wide_Type (Desig_Typ) then
1161 -- Detect a special case where interface class-wide types
1162 -- are involved as the object appears as:
1164 -- Tag_Ptr (Base_Address (<object>'Address))
1166 -- The expression already yields the proper tag, generate:
1168 -- Temp.all
1170 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
1171 Param :=
1172 Make_Explicit_Dereference (Loc,
1173 Prefix => Relocate_Node (Temp));
1175 -- In the default case, obtain the tag of the object about
1176 -- to be allocated / deallocated. Generate:
1178 -- Temp'Tag
1180 -- If the object is an unchecked conversion (typically to
1181 -- an access to class-wide type), we must preserve the
1182 -- conversion to ensure that the object is seen as tagged
1183 -- in the code that follows.
1185 else
1186 Pref := Temp;
1188 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
1189 then
1190 Pref := Parent (Pref);
1191 end if;
1193 Param :=
1194 Make_Attribute_Reference (Loc,
1195 Prefix => Relocate_Node (Pref),
1196 Attribute_Name => Name_Tag);
1197 end if;
1199 -- Generate:
1200 -- Needs_Finalization (<Param>)
1202 Flag_Expr :=
1203 Make_Function_Call (Loc,
1204 Name =>
1205 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
1206 Parameter_Associations => New_List (Param));
1208 -- Processing for generic actuals
1210 elsif Is_Generic_Actual_Type (Desig_Typ) then
1211 Flag_Expr :=
1212 New_Occurrence_Of (Boolean_Literals
1213 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
1215 -- The object does not require any specialized checks, it is
1216 -- known to be controlled.
1218 else
1219 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
1220 end if;
1222 -- Create the temporary which represents the finalization state
1223 -- of the expression. Generate:
1225 -- F : constant Boolean := <Flag_Expr>;
1227 Insert_Action (N,
1228 Make_Object_Declaration (Loc,
1229 Defining_Identifier => Flag_Id,
1230 Constant_Present => True,
1231 Object_Definition =>
1232 New_Occurrence_Of (Standard_Boolean, Loc),
1233 Expression => Flag_Expr));
1235 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
1236 end Is_Controlled;
1238 -- The object is not controlled
1240 else
1241 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
1242 end if;
1244 -- i) On_Subpool
1246 if Is_Allocate then
1247 Append_To (Actuals,
1248 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
1249 end if;
1251 -- Step 2: Build a wrapper Allocate / Deallocate which internally
1252 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
1254 -- Select the proper routine to call
1256 if Is_Allocate then
1257 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
1258 else
1259 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
1260 end if;
1262 -- Create a custom Allocate / Deallocate routine which has identical
1263 -- profile to that of System.Storage_Pools.
1265 declare
1266 -- P : Root_Storage_Pool
1267 function Pool_Param return Node_Id is (
1268 Make_Parameter_Specification (Loc,
1269 Defining_Identifier => Make_Temporary (Loc, 'P'),
1270 Parameter_Type =>
1271 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)));
1273 -- A : [out] Address
1274 function Address_Param return Node_Id is (
1275 Make_Parameter_Specification (Loc,
1276 Defining_Identifier => Addr_Id,
1277 Out_Present => Is_Allocate,
1278 Parameter_Type =>
1279 New_Occurrence_Of (RTE (RE_Address), Loc)));
1281 -- S : Storage_Count
1282 function Size_Param return Node_Id is (
1283 Make_Parameter_Specification (Loc,
1284 Defining_Identifier => Size_Id,
1285 Parameter_Type =>
1286 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1288 -- L : Storage_Count
1289 function Alignment_Param return Node_Id is (
1290 Make_Parameter_Specification (Loc,
1291 Defining_Identifier => Alig_Id,
1292 Parameter_Type =>
1293 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1295 Formal_Params : List_Id;
1296 begin
1297 if Use_Secondary_Stack_Pool then
1298 -- Gigi expects a different profile in the Secondary_Stack_Pool
1299 -- case. There must be no uses of the two missing formals
1300 -- (i.e., Pool_Param and Alignment_Param) in this case.
1301 Formal_Params := New_List
1302 (Address_Param, Size_Param, Alignment_Param);
1303 else
1304 Formal_Params := New_List (
1305 Pool_Param, Address_Param, Size_Param, Alignment_Param);
1306 end if;
1308 Insert_Action (N,
1309 Make_Subprogram_Body (Loc,
1310 Specification =>
1311 -- procedure Pnn
1312 Make_Procedure_Specification (Loc,
1313 Defining_Unit_Name => Proc_Id,
1314 Parameter_Specifications => Formal_Params),
1316 Declarations => No_List,
1318 Handled_Statement_Sequence =>
1319 Make_Handled_Sequence_Of_Statements (Loc,
1320 Statements => New_List (
1321 Make_Procedure_Call_Statement (Loc,
1322 Name =>
1323 New_Occurrence_Of (Proc_To_Call, Loc),
1324 Parameter_Associations => Actuals)))),
1325 Suppress => All_Checks);
1326 end;
1328 -- The newly generated Allocate / Deallocate becomes the default
1329 -- procedure to call when the back end processes the allocation /
1330 -- deallocation.
1332 if Is_Allocate then
1333 Set_Procedure_To_Call (Expr, Proc_Id);
1334 else
1335 Set_Procedure_To_Call (N, Proc_Id);
1336 end if;
1337 end;
1338 end Build_Allocate_Deallocate_Proc;
1340 -------------------------------
1341 -- Build_Abort_Undefer_Block --
1342 -------------------------------
1344 function Build_Abort_Undefer_Block
1345 (Loc : Source_Ptr;
1346 Stmts : List_Id;
1347 Context : Node_Id) return Node_Id
1349 Exceptions_OK : constant Boolean :=
1350 not Restriction_Active (No_Exception_Propagation);
1352 AUD : Entity_Id;
1353 Blk : Node_Id;
1354 Blk_Id : Entity_Id;
1355 HSS : Node_Id;
1357 begin
1358 -- The block should be generated only when undeferring abort in the
1359 -- context of a potential exception.
1361 pragma Assert (Abort_Allowed and Exceptions_OK);
1363 -- Generate:
1364 -- begin
1365 -- <Stmts>
1366 -- at end
1367 -- Abort_Undefer_Direct;
1368 -- end;
1370 AUD := RTE (RE_Abort_Undefer_Direct);
1372 HSS :=
1373 Make_Handled_Sequence_Of_Statements (Loc,
1374 Statements => Stmts,
1375 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1377 Blk :=
1378 Make_Block_Statement (Loc,
1379 Handled_Statement_Sequence => HSS);
1380 Set_Is_Abort_Block (Blk);
1382 Add_Block_Identifier (Blk, Blk_Id);
1383 Expand_At_End_Handler (HSS, Blk_Id);
1385 -- Present the Abort_Undefer_Direct function to the back end to inline
1386 -- the call to the routine.
1388 Add_Inlined_Body (AUD, Context);
1390 return Blk;
1391 end Build_Abort_Undefer_Block;
1393 ---------------------------------
1394 -- Build_Class_Wide_Expression --
1395 ---------------------------------
1397 procedure Build_Class_Wide_Expression
1398 (Pragma_Or_Expr : Node_Id;
1399 Subp : Entity_Id;
1400 Par_Subp : Entity_Id;
1401 Adjust_Sloc : Boolean)
1403 function Replace_Entity (N : Node_Id) return Traverse_Result;
1404 -- Replace reference to formal of inherited operation or to primitive
1405 -- operation of root type, with corresponding entity for derived type,
1406 -- when constructing the class-wide condition of an overriding
1407 -- subprogram.
1409 --------------------
1410 -- Replace_Entity --
1411 --------------------
1413 function Replace_Entity (N : Node_Id) return Traverse_Result is
1414 New_E : Entity_Id;
1416 begin
1417 if Adjust_Sloc then
1418 Adjust_Inherited_Pragma_Sloc (N);
1419 end if;
1421 if Nkind (N) in N_Identifier | N_Expanded_Name | N_Operator_Symbol
1422 and then Present (Entity (N))
1423 and then
1424 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1425 and then
1426 (Nkind (Parent (N)) /= N_Attribute_Reference
1427 or else Attribute_Name (Parent (N)) /= Name_Class)
1428 then
1429 -- The replacement does not apply to dispatching calls within the
1430 -- condition, but only to calls whose static tag is that of the
1431 -- parent type.
1433 if Is_Subprogram (Entity (N))
1434 and then Nkind (Parent (N)) = N_Function_Call
1435 and then Present (Controlling_Argument (Parent (N)))
1436 then
1437 return OK;
1438 end if;
1440 -- Determine whether entity has a renaming
1442 New_E := Type_Map.Get (Entity (N));
1444 if Present (New_E) then
1445 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1446 end if;
1448 -- Update type of function call node, which should be the same as
1449 -- the function's return type.
1451 if Is_Subprogram (Entity (N))
1452 and then Nkind (Parent (N)) = N_Function_Call
1453 then
1454 Set_Etype (Parent (N), Etype (Entity (N)));
1455 end if;
1457 -- The whole expression will be reanalyzed
1459 elsif Nkind (N) in N_Has_Etype then
1460 Set_Analyzed (N, False);
1461 end if;
1463 return OK;
1464 end Replace_Entity;
1466 procedure Replace_Condition_Entities is
1467 new Traverse_Proc (Replace_Entity);
1469 -- Local variables
1471 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Par_Subp);
1472 Subp_Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1474 -- Start of processing for Build_Class_Wide_Expression
1476 begin
1477 pragma Assert (Par_Typ /= Subp_Typ);
1479 Update_Primitives_Mapping (Par_Subp, Subp);
1480 Map_Formals (Par_Subp, Subp);
1481 Replace_Condition_Entities (Pragma_Or_Expr);
1482 end Build_Class_Wide_Expression;
1484 --------------------
1485 -- Build_DIC_Call --
1486 --------------------
1488 function Build_DIC_Call
1489 (Loc : Source_Ptr;
1490 Obj_Name : Node_Id;
1491 Typ : Entity_Id) return Node_Id
1493 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1494 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1496 begin
1497 -- The DIC procedure has a null body if assertions are disabled or
1498 -- Assertion_Policy Ignore is in effect. In that case, it would be
1499 -- nice to generate a null statement instead of a call to the DIC
1500 -- procedure, but doing that seems to interfere with the determination
1501 -- of ECRs (early call regions) in SPARK. ???
1503 return
1504 Make_Procedure_Call_Statement (Loc,
1505 Name => New_Occurrence_Of (Proc_Id, Loc),
1506 Parameter_Associations => New_List (
1507 Unchecked_Convert_To (Formal_Typ, Obj_Name)));
1508 end Build_DIC_Call;
1510 ------------------------------
1511 -- Build_DIC_Procedure_Body --
1512 ------------------------------
1514 -- WARNING: This routine manages Ghost regions. Return statements must be
1515 -- replaced by gotos which jump to the end of the routine and restore the
1516 -- Ghost mode.
1518 procedure Build_DIC_Procedure_Body
1519 (Typ : Entity_Id;
1520 Partial_DIC : Boolean := False)
1522 Pragmas_Seen : Elist_Id := No_Elist;
1523 -- This list contains all DIC pragmas processed so far. The list is used
1524 -- to avoid redundant Default_Initial_Condition checks.
1526 procedure Add_DIC_Check
1527 (DIC_Prag : Node_Id;
1528 DIC_Expr : Node_Id;
1529 Stmts : in out List_Id);
1530 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1531 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1532 -- is added to list Stmts.
1534 procedure Add_Inherited_DIC
1535 (DIC_Prag : Node_Id;
1536 Par_Typ : Entity_Id;
1537 Deriv_Typ : Entity_Id;
1538 Stmts : in out List_Id);
1539 -- Add a runtime check to verify the assertion expression of inherited
1540 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1541 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1542 -- pragma. All generated code is added to list Stmts.
1544 procedure Add_Inherited_Tagged_DIC
1545 (DIC_Prag : Node_Id;
1546 Expr : Node_Id;
1547 Stmts : in out List_Id);
1548 -- Add a runtime check to verify assertion expression DIC_Expr of
1549 -- inherited pragma DIC_Prag. This routine applies class-wide pre-
1550 -- and postcondition-like runtime semantics to the check. Expr is
1551 -- the assertion expression after substitution has been performed
1552 -- (via Replace_References). All generated code is added to list Stmts.
1554 procedure Add_Inherited_DICs
1555 (T : Entity_Id;
1556 Priv_Typ : Entity_Id;
1557 Full_Typ : Entity_Id;
1558 Obj_Id : Entity_Id;
1559 Checks : in out List_Id);
1560 -- Generate a DIC check for each inherited Default_Initial_Condition
1561 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
1562 -- the partial and full view of the parent type. Obj_Id denotes the
1563 -- entity of the _object formal parameter of the DIC procedure. All
1564 -- created checks are added to list Checks.
1566 procedure Add_Own_DIC
1567 (DIC_Prag : Node_Id;
1568 DIC_Typ : Entity_Id;
1569 Obj_Id : Entity_Id;
1570 Stmts : in out List_Id);
1571 -- Add a runtime check to verify the assertion expression of pragma
1572 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. Obj_Id is the
1573 -- object to substitute in the assertion expression for any references
1574 -- to the current instance of the type All generated code is added to
1575 -- list Stmts.
1577 procedure Add_Parent_DICs
1578 (T : Entity_Id;
1579 Obj_Id : Entity_Id;
1580 Checks : in out List_Id);
1581 -- Generate a Default_Initial_Condition check for each inherited DIC
1582 -- aspect coming from all parent types of type T. Obj_Id denotes the
1583 -- entity of the _object formal parameter of the DIC procedure. All
1584 -- created checks are added to list Checks.
1586 -------------------
1587 -- Add_DIC_Check --
1588 -------------------
1590 procedure Add_DIC_Check
1591 (DIC_Prag : Node_Id;
1592 DIC_Expr : Node_Id;
1593 Stmts : in out List_Id)
1595 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1596 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1598 begin
1599 -- The DIC pragma is ignored, nothing left to do
1601 if Is_Ignored (DIC_Prag) then
1602 null;
1604 -- Otherwise the DIC expression must be checked at run time.
1605 -- Generate:
1607 -- pragma Check (<Nam>, <DIC_Expr>);
1609 else
1610 Append_New_To (Stmts,
1611 Make_Pragma (Loc,
1612 Pragma_Identifier =>
1613 Make_Identifier (Loc, Name_Check),
1615 Pragma_Argument_Associations => New_List (
1616 Make_Pragma_Argument_Association (Loc,
1617 Expression => Make_Identifier (Loc, Nam)),
1619 Make_Pragma_Argument_Association (Loc,
1620 Expression => DIC_Expr))));
1621 end if;
1623 -- Add the pragma to the list of processed pragmas
1625 Append_New_Elmt (DIC_Prag, Pragmas_Seen);
1626 end Add_DIC_Check;
1628 -----------------------
1629 -- Add_Inherited_DIC --
1630 -----------------------
1632 procedure Add_Inherited_DIC
1633 (DIC_Prag : Node_Id;
1634 Par_Typ : Entity_Id;
1635 Deriv_Typ : Entity_Id;
1636 Stmts : in out List_Id)
1638 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1639 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1640 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1641 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1642 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1644 begin
1645 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1647 -- Verify the inherited DIC assertion expression by calling the DIC
1648 -- procedure of the parent type.
1650 -- Generate:
1651 -- <Par_Typ>DIC (Par_Typ (_object));
1653 Append_New_To (Stmts,
1654 Make_Procedure_Call_Statement (Loc,
1655 Name => New_Occurrence_Of (Par_Proc, Loc),
1656 Parameter_Associations => New_List (
1657 Convert_To
1658 (Typ => Etype (Par_Obj),
1659 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1660 end Add_Inherited_DIC;
1662 ------------------------------
1663 -- Add_Inherited_Tagged_DIC --
1664 ------------------------------
1666 procedure Add_Inherited_Tagged_DIC
1667 (DIC_Prag : Node_Id;
1668 Expr : Node_Id;
1669 Stmts : in out List_Id)
1671 begin
1672 -- Once the DIC assertion expression is fully processed, add a check
1673 -- to the statements of the DIC procedure.
1675 Add_DIC_Check
1676 (DIC_Prag => DIC_Prag,
1677 DIC_Expr => Expr,
1678 Stmts => Stmts);
1679 end Add_Inherited_Tagged_DIC;
1681 ------------------------
1682 -- Add_Inherited_DICs --
1683 ------------------------
1685 procedure Add_Inherited_DICs
1686 (T : Entity_Id;
1687 Priv_Typ : Entity_Id;
1688 Full_Typ : Entity_Id;
1689 Obj_Id : Entity_Id;
1690 Checks : in out List_Id)
1692 Deriv_Typ : Entity_Id;
1693 Expr : Node_Id;
1694 Prag : Node_Id;
1695 Prag_Expr : Node_Id;
1696 Prag_Expr_Arg : Node_Id;
1697 Prag_Typ : Node_Id;
1698 Prag_Typ_Arg : Node_Id;
1700 Par_Proc : Entity_Id;
1701 -- The "partial" invariant procedure of Par_Typ
1703 Par_Typ : Entity_Id;
1704 -- The suitable view of the parent type used in the substitution of
1705 -- type attributes.
1707 begin
1708 if No (Priv_Typ) and then No (Full_Typ) then
1709 return;
1710 end if;
1712 -- When the type inheriting the class-wide invariant is a concurrent
1713 -- type, use the corresponding record type because it contains all
1714 -- primitive operations of the concurrent type and allows for proper
1715 -- substitution.
1717 if Is_Concurrent_Type (T) then
1718 Deriv_Typ := Corresponding_Record_Type (T);
1719 else
1720 Deriv_Typ := T;
1721 end if;
1723 pragma Assert (Present (Deriv_Typ));
1725 -- Determine which rep item chain to use. Precedence is given to that
1726 -- of the parent type's partial view since it usually carries all the
1727 -- class-wide invariants.
1729 if Present (Priv_Typ) then
1730 Prag := First_Rep_Item (Priv_Typ);
1731 else
1732 Prag := First_Rep_Item (Full_Typ);
1733 end if;
1735 while Present (Prag) loop
1736 if Nkind (Prag) = N_Pragma
1737 and then Pragma_Name (Prag) = Name_Default_Initial_Condition
1738 then
1739 -- Nothing to do if the pragma was already processed
1741 if Contains (Pragmas_Seen, Prag) then
1742 return;
1743 end if;
1745 -- Extract arguments of the Default_Initial_Condition pragma
1747 Prag_Expr_Arg := First (Pragma_Argument_Associations (Prag));
1748 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
1750 -- Pick up the implicit second argument of the pragma, which
1751 -- indicates the type that the pragma applies to.
1753 Prag_Typ_Arg := Next (Prag_Expr_Arg);
1754 if Present (Prag_Typ_Arg) then
1755 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
1756 else
1757 Prag_Typ := Empty;
1758 end if;
1760 -- The pragma applies to the partial view of the parent type
1762 if Present (Priv_Typ)
1763 and then Present (Prag_Typ)
1764 and then Entity (Prag_Typ) = Priv_Typ
1765 then
1766 Par_Typ := Priv_Typ;
1768 -- The pragma applies to the full view of the parent type
1770 elsif Present (Full_Typ)
1771 and then Present (Prag_Typ)
1772 and then Entity (Prag_Typ) = Full_Typ
1773 then
1774 Par_Typ := Full_Typ;
1776 -- Otherwise the pragma does not belong to the parent type and
1777 -- should not be considered.
1779 else
1780 return;
1781 end if;
1783 -- Substitute references in the DIC expression that are related
1784 -- to the partial type with corresponding references related to
1785 -- the derived type (call to Replace_References below).
1787 Expr := New_Copy_Tree (Prag_Expr);
1789 Par_Proc := Partial_DIC_Procedure (Par_Typ);
1791 -- If there's not a partial DIC procedure (such as when a
1792 -- full type doesn't have its own DIC, but is inherited from
1793 -- a type with DIC), get the full DIC procedure.
1795 if No (Par_Proc) then
1796 Par_Proc := DIC_Procedure (Par_Typ);
1797 end if;
1799 Replace_References
1800 (Expr => Expr,
1801 Par_Typ => Par_Typ,
1802 Deriv_Typ => Deriv_Typ,
1803 Par_Obj => First_Formal (Par_Proc),
1804 Deriv_Obj => Obj_Id);
1806 -- Why are there different actions depending on whether T is
1807 -- tagged? Can these be unified? ???
1809 if Is_Tagged_Type (T) then
1810 Add_Inherited_Tagged_DIC
1811 (DIC_Prag => Prag,
1812 Expr => Expr,
1813 Stmts => Checks);
1815 else
1816 Add_Inherited_DIC
1817 (DIC_Prag => Prag,
1818 Par_Typ => Par_Typ,
1819 Deriv_Typ => Deriv_Typ,
1820 Stmts => Checks);
1821 end if;
1823 -- Leave as soon as we get a DIC pragma, since we'll visit
1824 -- the pragmas of the parents, so will get to any "inherited"
1825 -- pragmas that way.
1827 return;
1828 end if;
1830 Next_Rep_Item (Prag);
1831 end loop;
1832 end Add_Inherited_DICs;
1834 -----------------
1835 -- Add_Own_DIC --
1836 -----------------
1838 procedure Add_Own_DIC
1839 (DIC_Prag : Node_Id;
1840 DIC_Typ : Entity_Id;
1841 Obj_Id : Entity_Id;
1842 Stmts : in out List_Id)
1844 DIC_Args : constant List_Id :=
1845 Pragma_Argument_Associations (DIC_Prag);
1846 DIC_Arg : constant Node_Id := First (DIC_Args);
1847 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1848 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1850 -- Local variables
1852 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1854 Expr : Node_Id;
1856 -- Start of processing for Add_Own_DIC
1858 begin
1859 pragma Assert (Present (DIC_Expr));
1861 -- We need to preanalyze the expression itself inside a generic to
1862 -- be able to capture global references present in it.
1864 if Inside_A_Generic then
1865 Expr := DIC_Expr;
1866 else
1867 Expr := New_Copy_Tree (DIC_Expr);
1868 end if;
1870 -- Perform the following substitution:
1872 -- * Replace the current instance of DIC_Typ with a reference to
1873 -- the _object formal parameter of the DIC procedure.
1875 Replace_Type_References
1876 (Expr => Expr,
1877 Typ => DIC_Typ,
1878 Obj_Id => Obj_Id);
1880 -- Preanalyze the DIC expression to detect errors and at the same
1881 -- time capture the visibility of the proper package part.
1883 Set_Parent (Expr, Typ_Decl);
1884 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1886 -- Save a copy of the expression with all replacements and analysis
1887 -- already taken place in case a derived type inherits the pragma.
1888 -- The copy will be used as the foundation of the derived type's own
1889 -- version of the DIC assertion expression.
1891 if Is_Tagged_Type (DIC_Typ) then
1892 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1893 end if;
1895 -- If the pragma comes from an aspect specification, replace the
1896 -- saved expression because all type references must be substituted
1897 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1898 -- routines.
1900 if Present (DIC_Asp) then
1901 Set_Expression_Copy (DIC_Asp, New_Copy_Tree (Expr));
1902 end if;
1904 -- Once the DIC assertion expression is fully processed, add a check
1905 -- to the statements of the DIC procedure (unless the type is an
1906 -- abstract type, in which case we don't want the possibility of
1907 -- generating a call to an abstract function of the type; such DIC
1908 -- procedures can never be called in any case, so not generating the
1909 -- check at all is OK).
1911 if not Is_Abstract_Type (DIC_Typ) or else GNATprove_Mode then
1912 Add_DIC_Check
1913 (DIC_Prag => DIC_Prag,
1914 DIC_Expr => Expr,
1915 Stmts => Stmts);
1916 end if;
1917 end Add_Own_DIC;
1919 ---------------------
1920 -- Add_Parent_DICs --
1921 ---------------------
1923 procedure Add_Parent_DICs
1924 (T : Entity_Id;
1925 Obj_Id : Entity_Id;
1926 Checks : in out List_Id)
1928 Dummy_1 : Entity_Id;
1929 Dummy_2 : Entity_Id;
1931 Curr_Typ : Entity_Id;
1932 -- The entity of the current type being examined
1934 Full_Typ : Entity_Id;
1935 -- The full view of Par_Typ
1937 Par_Typ : Entity_Id;
1938 -- The entity of the parent type
1940 Priv_Typ : Entity_Id;
1941 -- The partial view of Par_Typ
1943 Op_Node : Elmt_Id;
1944 Par_Prim : Entity_Id;
1945 Prim : Entity_Id;
1947 begin
1948 -- Map the overridden primitive to the overriding one; required by
1949 -- Replace_References (called by Add_Inherited_DICs) to handle calls
1950 -- to parent primitives.
1952 Op_Node := First_Elmt (Primitive_Operations (T));
1953 while Present (Op_Node) loop
1954 Prim := Node (Op_Node);
1956 if Present (Overridden_Operation (Prim))
1957 and then Comes_From_Source (Prim)
1958 then
1959 Par_Prim := Overridden_Operation (Prim);
1961 -- Create a mapping of the form:
1962 -- parent type primitive -> derived type primitive
1964 Type_Map.Set (Par_Prim, Prim);
1965 end if;
1967 Next_Elmt (Op_Node);
1968 end loop;
1970 -- Climb the parent type chain
1972 Curr_Typ := T;
1973 loop
1974 -- Do not consider subtypes, as they inherit the DICs from their
1975 -- base types.
1977 Par_Typ := Base_Type (Etype (Base_Type (Curr_Typ)));
1979 -- Stop the climb once the root of the parent chain is
1980 -- reached.
1982 exit when Curr_Typ = Par_Typ;
1984 -- Process the DICs of the parent type
1986 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
1988 -- Only try to inherit a DIC pragma from the parent type Par_Typ
1989 -- if it Has_Own_DIC pragma. The loop will proceed up the parent
1990 -- chain to find all types that have their own DIC.
1992 if Has_Own_DIC (Par_Typ) then
1993 Add_Inherited_DICs
1994 (T => T,
1995 Priv_Typ => Priv_Typ,
1996 Full_Typ => Full_Typ,
1997 Obj_Id => Obj_Id,
1998 Checks => Checks);
1999 end if;
2001 Curr_Typ := Par_Typ;
2002 end loop;
2003 end Add_Parent_DICs;
2005 -- Local variables
2007 Loc : constant Source_Ptr := Sloc (Typ);
2009 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2010 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2011 -- Save the Ghost-related attributes to restore on exit
2013 DIC_Prag : Node_Id;
2014 DIC_Typ : Entity_Id;
2015 Dummy_1 : Entity_Id;
2016 Dummy_2 : Entity_Id;
2017 Proc_Body : Node_Id;
2018 Proc_Body_Id : Entity_Id;
2019 Proc_Decl : Node_Id;
2020 Proc_Id : Entity_Id;
2021 Stmts : List_Id := No_List;
2023 CRec_Typ : Entity_Id := Empty;
2024 -- The corresponding record type of Full_Typ
2026 Full_Typ : Entity_Id := Empty;
2027 -- The full view of the working type
2029 Obj_Id : Entity_Id := Empty;
2030 -- The _object formal parameter of the invariant procedure
2032 Part_Proc : Entity_Id := Empty;
2033 -- The entity of the "partial" invariant procedure
2035 Priv_Typ : Entity_Id := Empty;
2036 -- The partial view of the working type
2038 Work_Typ : Entity_Id;
2039 -- The working type
2041 -- Start of processing for Build_DIC_Procedure_Body
2043 begin
2044 Work_Typ := Base_Type (Typ);
2046 -- Do not process class-wide types as these are Itypes, but lack a first
2047 -- subtype (see below).
2049 if Is_Class_Wide_Type (Work_Typ) then
2050 return;
2052 -- Do not process the underlying full view of a private type. There is
2053 -- no way to get back to the partial view, plus the body will be built
2054 -- by the full view or the base type.
2056 elsif Is_Underlying_Full_View (Work_Typ) then
2057 return;
2059 -- Use the first subtype when dealing with implicit base types
2061 elsif Is_Itype (Work_Typ) then
2062 Work_Typ := First_Subtype (Work_Typ);
2064 -- The input denotes the corresponding record type of a protected or a
2065 -- task type. Work with the concurrent type because the corresponding
2066 -- record type may not be visible to clients of the type.
2068 elsif Ekind (Work_Typ) = E_Record_Type
2069 and then Is_Concurrent_Record_Type (Work_Typ)
2070 then
2071 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2072 end if;
2074 -- The working type may be subject to pragma Ghost. Set the mode now to
2075 -- ensure that the DIC procedure is properly marked as Ghost.
2077 Set_Ghost_Mode (Work_Typ);
2079 -- The working type must be either define a DIC pragma of its own or
2080 -- inherit one from a parent type.
2082 pragma Assert (Has_DIC (Work_Typ));
2084 -- Recover the type which defines the DIC pragma. This is either the
2085 -- working type itself or a parent type when the pragma is inherited.
2087 DIC_Typ := Find_DIC_Type (Work_Typ);
2088 pragma Assert (Present (DIC_Typ));
2090 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2091 pragma Assert (Present (DIC_Prag));
2093 -- Nothing to do if pragma DIC appears without an argument or its sole
2094 -- argument is "null".
2096 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2097 goto Leave;
2098 end if;
2100 -- Obtain both views of the type
2102 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy_1, CRec_Typ);
2104 -- The caller requests a body for the partial DIC procedure
2106 if Partial_DIC then
2107 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2109 -- The "full" DIC procedure body was already created
2111 -- Create a declaration for the "partial" DIC procedure if it
2112 -- is not available.
2114 if No (Proc_Id) then
2115 Build_DIC_Procedure_Declaration
2116 (Typ => Work_Typ,
2117 Partial_DIC => True);
2119 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2120 end if;
2122 -- The caller requests a body for the "full" DIC procedure
2124 else
2125 Proc_Id := DIC_Procedure (Work_Typ);
2126 Part_Proc := Partial_DIC_Procedure (Work_Typ);
2128 -- Create a declaration for the "full" DIC procedure if it is
2129 -- not available.
2131 if No (Proc_Id) then
2132 Build_DIC_Procedure_Declaration (Work_Typ);
2133 Proc_Id := DIC_Procedure (Work_Typ);
2134 end if;
2135 end if;
2137 -- At this point there should be a DIC procedure declaration
2139 pragma Assert (Present (Proc_Id));
2140 Proc_Decl := Unit_Declaration_Node (Proc_Id);
2142 -- Nothing to do if the DIC procedure already has a body
2144 if Present (Corresponding_Body (Proc_Decl)) then
2145 goto Leave;
2146 end if;
2148 -- Emulate the environment of the DIC procedure by installing its scope
2149 -- and formal parameters.
2151 Push_Scope (Proc_Id);
2152 Install_Formals (Proc_Id);
2154 Obj_Id := First_Formal (Proc_Id);
2155 pragma Assert (Present (Obj_Id));
2157 -- The "partial" DIC procedure verifies the DICs of the partial view
2158 -- only.
2160 if Partial_DIC then
2161 pragma Assert (Present (Priv_Typ));
2163 if Has_Own_DIC (Work_Typ) then -- If we're testing this then maybe
2164 Add_Own_DIC -- we shouldn't be calling Find_DIC_Typ above???
2165 (DIC_Prag => DIC_Prag,
2166 DIC_Typ => DIC_Typ, -- Should this just be Work_Typ???
2167 Obj_Id => Obj_Id,
2168 Stmts => Stmts);
2169 end if;
2171 -- Otherwise, the "full" DIC procedure verifies the DICs inherited from
2172 -- parent types, as well as indirectly verifying the DICs of the partial
2173 -- view by calling the "partial" DIC procedure.
2175 else
2176 -- Check the DIC of the partial view by calling the "partial" DIC
2177 -- procedure, unless the partial DIC body is empty. Generate:
2179 -- <Work_Typ>Partial_DIC (_object);
2181 if Present (Part_Proc) and then not Has_Null_Body (Part_Proc) then
2182 Append_New_To (Stmts,
2183 Make_Procedure_Call_Statement (Loc,
2184 Name => New_Occurrence_Of (Part_Proc, Loc),
2185 Parameter_Associations => New_List (
2186 New_Occurrence_Of (Obj_Id, Loc))));
2187 end if;
2189 -- Process inherited Default_Initial_Conditions for all parent types
2191 Add_Parent_DICs (Work_Typ, Obj_Id, Stmts);
2192 end if;
2194 End_Scope;
2196 -- Produce an empty completing body in the following cases:
2197 -- * Assertions are disabled
2198 -- * The DIC Assertion_Policy is Ignore
2200 if No (Stmts) then
2201 Stmts := New_List (Make_Null_Statement (Loc));
2202 end if;
2204 -- Generate:
2205 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
2206 -- begin
2207 -- <Stmts>
2208 -- end <Work_Typ>DIC;
2210 Proc_Body :=
2211 Make_Subprogram_Body (Loc,
2212 Specification =>
2213 Copy_Subprogram_Spec (Parent (Proc_Id)),
2214 Declarations => Empty_List,
2215 Handled_Statement_Sequence =>
2216 Make_Handled_Sequence_Of_Statements (Loc,
2217 Statements => Stmts));
2218 Proc_Body_Id := Defining_Entity (Proc_Body);
2220 -- Perform minor decoration in case the body is not analyzed
2222 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
2223 Set_Etype (Proc_Body_Id, Standard_Void_Type);
2224 Set_Scope (Proc_Body_Id, Current_Scope);
2225 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
2226 Set_SPARK_Pragma_Inherited
2227 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
2229 -- Link both spec and body to avoid generating duplicates
2231 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
2232 Set_Corresponding_Spec (Proc_Body, Proc_Id);
2234 -- The body should not be inserted into the tree when the context
2235 -- is a generic unit because it is not part of the template.
2236 -- Note that the body must still be generated in order to resolve the
2237 -- DIC assertion expression.
2239 if Inside_A_Generic then
2240 null;
2242 -- Semi-insert the body into the tree for GNATprove by setting its
2243 -- Parent field. This allows for proper upstream tree traversals.
2245 elsif GNATprove_Mode then
2246 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
2248 -- Otherwise the body is part of the freezing actions of the working
2249 -- type.
2251 else
2252 Append_Freeze_Action (Work_Typ, Proc_Body);
2253 end if;
2255 <<Leave>>
2256 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2257 end Build_DIC_Procedure_Body;
2259 -------------------------------------
2260 -- Build_DIC_Procedure_Declaration --
2261 -------------------------------------
2263 -- WARNING: This routine manages Ghost regions. Return statements must be
2264 -- replaced by gotos which jump to the end of the routine and restore the
2265 -- Ghost mode.
2267 procedure Build_DIC_Procedure_Declaration
2268 (Typ : Entity_Id;
2269 Partial_DIC : Boolean := False)
2271 Loc : constant Source_Ptr := Sloc (Typ);
2273 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2274 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2275 -- Save the Ghost-related attributes to restore on exit
2277 DIC_Prag : Node_Id;
2278 DIC_Typ : Entity_Id;
2279 Proc_Decl : Node_Id;
2280 Proc_Id : Entity_Id;
2281 Proc_Nam : Name_Id;
2282 Typ_Decl : Node_Id;
2284 CRec_Typ : Entity_Id;
2285 -- The corresponding record type of Full_Typ
2287 Full_Typ : Entity_Id;
2288 -- The full view of working type
2290 Obj_Id : Entity_Id;
2291 -- The _object formal parameter of the DIC procedure
2293 Priv_Typ : Entity_Id;
2294 -- The partial view of working type
2296 UFull_Typ : Entity_Id;
2297 -- The underlying full view of Full_Typ
2299 Work_Typ : Entity_Id;
2300 -- The working type
2302 begin
2303 Work_Typ := Base_Type (Typ);
2305 -- Do not process class-wide types as these are Itypes, but lack a first
2306 -- subtype (see below).
2308 if Is_Class_Wide_Type (Work_Typ) then
2309 return;
2311 -- Do not process the underlying full view of a private type. There is
2312 -- no way to get back to the partial view, plus the body will be built
2313 -- by the full view or the base type.
2315 elsif Is_Underlying_Full_View (Work_Typ) then
2316 return;
2318 -- Use the first subtype when dealing with various base types
2320 elsif Is_Itype (Work_Typ) then
2321 Work_Typ := First_Subtype (Work_Typ);
2323 -- The input denotes the corresponding record type of a protected or a
2324 -- task type. Work with the concurrent type because the corresponding
2325 -- record type may not be visible to clients of the type.
2327 elsif Ekind (Work_Typ) = E_Record_Type
2328 and then Is_Concurrent_Record_Type (Work_Typ)
2329 then
2330 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2331 end if;
2333 -- The working type may be subject to pragma Ghost. Set the mode now to
2334 -- ensure that the DIC procedure is properly marked as Ghost.
2336 Set_Ghost_Mode (Work_Typ);
2338 -- The type must be either subject to a DIC pragma or inherit one from a
2339 -- parent type.
2341 pragma Assert (Has_DIC (Work_Typ));
2343 -- Recover the type which defines the DIC pragma. This is either the
2344 -- working type itself or a parent type when the pragma is inherited.
2346 DIC_Typ := Find_DIC_Type (Work_Typ);
2347 pragma Assert (Present (DIC_Typ));
2349 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2350 pragma Assert (Present (DIC_Prag));
2352 -- Nothing to do if pragma DIC appears without an argument or its sole
2353 -- argument is "null".
2355 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2356 goto Leave;
2357 end if;
2359 -- Nothing to do if the type already has a "partial" DIC procedure
2361 if Partial_DIC then
2362 if Present (Partial_DIC_Procedure (Work_Typ)) then
2363 goto Leave;
2364 end if;
2366 -- Nothing to do if the type already has a "full" DIC procedure
2368 elsif Present (DIC_Procedure (Work_Typ)) then
2369 goto Leave;
2370 end if;
2372 -- The caller requests the declaration of the "partial" DIC procedure
2374 if Partial_DIC then
2375 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_DIC");
2377 -- Otherwise the caller requests the declaration of the "full" DIC
2378 -- procedure.
2380 else
2381 Proc_Nam := New_External_Name (Chars (Work_Typ), "DIC");
2382 end if;
2384 Proc_Id :=
2385 Make_Defining_Identifier (Loc, Chars => Proc_Nam);
2387 -- Perform minor decoration in case the declaration is not analyzed
2389 Mutate_Ekind (Proc_Id, E_Procedure);
2390 Set_Etype (Proc_Id, Standard_Void_Type);
2391 Set_Is_DIC_Procedure (Proc_Id);
2392 Set_Scope (Proc_Id, Current_Scope);
2393 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
2394 Set_SPARK_Pragma_Inherited (Proc_Id);
2396 Set_DIC_Procedure (Work_Typ, Proc_Id);
2398 -- The DIC procedure requires debug info when the assertion expression
2399 -- is subject to Source Coverage Obligations.
2401 if Generate_SCO then
2402 Set_Debug_Info_Needed (Proc_Id);
2403 end if;
2405 -- Obtain all views of the input type
2407 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
2409 -- Associate the DIC procedure and various flags with all views
2411 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
2412 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
2413 Propagate_DIC_Attributes (UFull_Typ, From_Typ => Work_Typ);
2414 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
2416 -- The declaration of the DIC procedure must be inserted after the
2417 -- declaration of the partial view as this allows for proper external
2418 -- visibility.
2420 if Present (Priv_Typ) then
2421 Typ_Decl := Declaration_Node (Priv_Typ);
2423 -- Derived types with the full view as parent do not have a partial
2424 -- view. Insert the DIC procedure after the derived type.
2426 else
2427 Typ_Decl := Declaration_Node (Full_Typ);
2428 end if;
2430 -- The type should have a declarative node
2432 pragma Assert (Present (Typ_Decl));
2434 -- Create the formal parameter which emulates the variable-like behavior
2435 -- of the type's current instance.
2437 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
2439 -- Perform minor decoration in case the declaration is not analyzed
2441 Mutate_Ekind (Obj_Id, E_In_Parameter);
2442 Set_Etype (Obj_Id, Work_Typ);
2443 Set_Scope (Obj_Id, Proc_Id);
2445 Set_First_Entity (Proc_Id, Obj_Id);
2446 Set_Last_Entity (Proc_Id, Obj_Id);
2448 -- Generate:
2449 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
2451 Proc_Decl :=
2452 Make_Subprogram_Declaration (Loc,
2453 Specification =>
2454 Make_Procedure_Specification (Loc,
2455 Defining_Unit_Name => Proc_Id,
2456 Parameter_Specifications => New_List (
2457 Make_Parameter_Specification (Loc,
2458 Defining_Identifier => Obj_Id,
2459 Parameter_Type =>
2460 New_Occurrence_Of (Work_Typ, Loc)))));
2462 -- The declaration should not be inserted into the tree when the context
2463 -- is a generic unit because it is not part of the template.
2465 if Inside_A_Generic then
2466 null;
2468 -- Semi-insert the declaration into the tree for GNATprove by setting
2469 -- its Parent field. This allows for proper upstream tree traversals.
2471 elsif GNATprove_Mode then
2472 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2474 -- Otherwise insert the declaration
2476 else
2477 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2478 end if;
2480 <<Leave>>
2481 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2482 end Build_DIC_Procedure_Declaration;
2484 ------------------------------------
2485 -- Build_Invariant_Procedure_Body --
2486 ------------------------------------
2488 -- WARNING: This routine manages Ghost regions. Return statements must be
2489 -- replaced by gotos which jump to the end of the routine and restore the
2490 -- Ghost mode.
2492 procedure Build_Invariant_Procedure_Body
2493 (Typ : Entity_Id;
2494 Partial_Invariant : Boolean := False)
2496 Loc : constant Source_Ptr := Sloc (Typ);
2498 Pragmas_Seen : Elist_Id := No_Elist;
2499 -- This list contains all invariant pragmas processed so far. The list
2500 -- is used to avoid generating redundant invariant checks.
2502 Produced_Check : Boolean := False;
2503 -- This flag tracks whether the type has produced at least one invariant
2504 -- check. The flag is used as a sanity check at the end of the routine.
2506 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2507 -- intentionally unnested to avoid deep indentation of code.
2509 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2510 -- they emit checks, loops (for arrays) and case statements (for record
2511 -- variant parts) only when there are invariants to verify. This keeps
2512 -- the body of the invariant procedure free of useless code.
2514 procedure Add_Array_Component_Invariants
2515 (T : Entity_Id;
2516 Obj_Id : Entity_Id;
2517 Checks : in out List_Id);
2518 -- Generate an invariant check for each component of array type T.
2519 -- Obj_Id denotes the entity of the _object formal parameter of the
2520 -- invariant procedure. All created checks are added to list Checks.
2522 procedure Add_Inherited_Invariants
2523 (T : Entity_Id;
2524 Priv_Typ : Entity_Id;
2525 Full_Typ : Entity_Id;
2526 Obj_Id : Entity_Id;
2527 Checks : in out List_Id);
2528 -- Generate an invariant check for each inherited class-wide invariant
2529 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2530 -- the partial and full view of the parent type. Obj_Id denotes the
2531 -- entity of the _object formal parameter of the invariant procedure.
2532 -- All created checks are added to list Checks.
2534 procedure Add_Interface_Invariants
2535 (T : Entity_Id;
2536 Obj_Id : Entity_Id;
2537 Checks : in out List_Id);
2538 -- Generate an invariant check for each inherited class-wide invariant
2539 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2540 -- entity of the _object formal parameter of the invariant procedure.
2541 -- All created checks are added to list Checks.
2543 procedure Add_Invariant_Check
2544 (Prag : Node_Id;
2545 Expr : Node_Id;
2546 Checks : in out List_Id;
2547 Inherited : Boolean := False);
2548 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2549 -- verify assertion expression Expr of pragma Prag. All generated code
2550 -- is added to list Checks. Flag Inherited should be set when the pragma
2551 -- is inherited from a parent or interface type.
2553 procedure Add_Own_Invariants
2554 (T : Entity_Id;
2555 Obj_Id : Entity_Id;
2556 Checks : in out List_Id;
2557 Priv_Item : Node_Id := Empty);
2558 -- Generate an invariant check for each invariant found for type T.
2559 -- Obj_Id denotes the entity of the _object formal parameter of the
2560 -- invariant procedure. All created checks are added to list Checks.
2561 -- Priv_Item denotes the first rep item of the private type.
2563 procedure Add_Parent_Invariants
2564 (T : Entity_Id;
2565 Obj_Id : Entity_Id;
2566 Checks : in out List_Id);
2567 -- Generate an invariant check for each inherited class-wide invariant
2568 -- coming from all parent types of type T. Obj_Id denotes the entity of
2569 -- the _object formal parameter of the invariant procedure. All created
2570 -- checks are added to list Checks.
2572 procedure Add_Record_Component_Invariants
2573 (T : Entity_Id;
2574 Obj_Id : Entity_Id;
2575 Checks : in out List_Id);
2576 -- Generate an invariant check for each component of record type T.
2577 -- Obj_Id denotes the entity of the _object formal parameter of the
2578 -- invariant procedure. All created checks are added to list Checks.
2580 ------------------------------------
2581 -- Add_Array_Component_Invariants --
2582 ------------------------------------
2584 procedure Add_Array_Component_Invariants
2585 (T : Entity_Id;
2586 Obj_Id : Entity_Id;
2587 Checks : in out List_Id)
2589 Comp_Typ : constant Entity_Id := Component_Type (T);
2590 Dims : constant Pos := Number_Dimensions (T);
2592 procedure Process_Array_Component
2593 (Indices : List_Id;
2594 Comp_Checks : in out List_Id);
2595 -- Generate an invariant check for an array component identified by
2596 -- the indices in list Indices. All created checks are added to list
2597 -- Comp_Checks.
2599 procedure Process_One_Dimension
2600 (Dim : Pos;
2601 Indices : List_Id;
2602 Dim_Checks : in out List_Id);
2603 -- Generate a loop over the Nth dimension Dim of an array type. List
2604 -- Indices contains all array indices for the dimension. All created
2605 -- checks are added to list Dim_Checks.
2607 -----------------------------
2608 -- Process_Array_Component --
2609 -----------------------------
2611 procedure Process_Array_Component
2612 (Indices : List_Id;
2613 Comp_Checks : in out List_Id)
2615 Proc_Id : Entity_Id;
2617 begin
2618 if Has_Invariants (Comp_Typ) then
2620 -- In GNATprove mode, the component invariants are checked by
2621 -- other means. They should not be added to the array type
2622 -- invariant procedure, so that the procedure can be used to
2623 -- check the array type invariants if any.
2625 if GNATprove_Mode then
2626 null;
2628 else
2629 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2631 -- The component type should have an invariant procedure
2632 -- if it has invariants of its own or inherits class-wide
2633 -- invariants from parent or interface types.
2635 pragma Assert (Present (Proc_Id));
2637 -- Generate:
2638 -- <Comp_Typ>Invariant (_object (<Indices>));
2640 -- The invariant procedure has a null body if assertions are
2641 -- disabled or Assertion_Policy Ignore is in effect.
2643 if not Has_Null_Body (Proc_Id) then
2644 Append_New_To (Comp_Checks,
2645 Make_Procedure_Call_Statement (Loc,
2646 Name =>
2647 New_Occurrence_Of (Proc_Id, Loc),
2648 Parameter_Associations => New_List (
2649 Make_Indexed_Component (Loc,
2650 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2651 Expressions => New_Copy_List (Indices)))));
2652 end if;
2653 end if;
2655 Produced_Check := True;
2656 end if;
2657 end Process_Array_Component;
2659 ---------------------------
2660 -- Process_One_Dimension --
2661 ---------------------------
2663 procedure Process_One_Dimension
2664 (Dim : Pos;
2665 Indices : List_Id;
2666 Dim_Checks : in out List_Id)
2668 Comp_Checks : List_Id := No_List;
2669 Index : Entity_Id;
2671 begin
2672 -- Generate the invariant checks for the array component after all
2673 -- dimensions have produced their respective loops.
2675 if Dim > Dims then
2676 Process_Array_Component
2677 (Indices => Indices,
2678 Comp_Checks => Dim_Checks);
2680 -- Otherwise create a loop for the current dimension
2682 else
2683 -- Create a new loop variable for each dimension
2685 Index :=
2686 Make_Defining_Identifier (Loc,
2687 Chars => New_External_Name ('I', Dim));
2688 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2690 Process_One_Dimension
2691 (Dim => Dim + 1,
2692 Indices => Indices,
2693 Dim_Checks => Comp_Checks);
2695 -- Generate:
2696 -- for I<Dim> in _object'Range (<Dim>) loop
2697 -- <Comp_Checks>
2698 -- end loop;
2700 -- Note that the invariant procedure may have a null body if
2701 -- assertions are disabled or Assertion_Policy Ignore is in
2702 -- effect.
2704 if Present (Comp_Checks) then
2705 Append_New_To (Dim_Checks,
2706 Make_Implicit_Loop_Statement (T,
2707 Identifier => Empty,
2708 Iteration_Scheme =>
2709 Make_Iteration_Scheme (Loc,
2710 Loop_Parameter_Specification =>
2711 Make_Loop_Parameter_Specification (Loc,
2712 Defining_Identifier => Index,
2713 Discrete_Subtype_Definition =>
2714 Make_Attribute_Reference (Loc,
2715 Prefix =>
2716 New_Occurrence_Of (Obj_Id, Loc),
2717 Attribute_Name => Name_Range,
2718 Expressions => New_List (
2719 Make_Integer_Literal (Loc, Dim))))),
2720 Statements => Comp_Checks));
2721 end if;
2722 end if;
2723 end Process_One_Dimension;
2725 -- Start of processing for Add_Array_Component_Invariants
2727 begin
2728 Process_One_Dimension
2729 (Dim => 1,
2730 Indices => New_List,
2731 Dim_Checks => Checks);
2732 end Add_Array_Component_Invariants;
2734 ------------------------------
2735 -- Add_Inherited_Invariants --
2736 ------------------------------
2738 procedure Add_Inherited_Invariants
2739 (T : Entity_Id;
2740 Priv_Typ : Entity_Id;
2741 Full_Typ : Entity_Id;
2742 Obj_Id : Entity_Id;
2743 Checks : in out List_Id)
2745 Deriv_Typ : Entity_Id;
2746 Expr : Node_Id;
2747 Prag : Node_Id;
2748 Prag_Expr : Node_Id;
2749 Prag_Expr_Arg : Node_Id;
2750 Prag_Typ : Node_Id;
2751 Prag_Typ_Arg : Node_Id;
2753 Par_Proc : Entity_Id;
2754 -- The "partial" invariant procedure of Par_Typ
2756 Par_Typ : Entity_Id;
2757 -- The suitable view of the parent type used in the substitution of
2758 -- type attributes.
2760 begin
2761 if No (Priv_Typ) and then No (Full_Typ) then
2762 return;
2763 end if;
2765 -- When the type inheriting the class-wide invariant is a concurrent
2766 -- type, use the corresponding record type because it contains all
2767 -- primitive operations of the concurrent type and allows for proper
2768 -- substitution.
2770 if Is_Concurrent_Type (T) then
2771 Deriv_Typ := Corresponding_Record_Type (T);
2772 else
2773 Deriv_Typ := T;
2774 end if;
2776 pragma Assert (Present (Deriv_Typ));
2778 -- Determine which rep item chain to use. Precedence is given to that
2779 -- of the parent type's partial view since it usually carries all the
2780 -- class-wide invariants.
2782 if Present (Priv_Typ) then
2783 Prag := First_Rep_Item (Priv_Typ);
2784 else
2785 Prag := First_Rep_Item (Full_Typ);
2786 end if;
2788 while Present (Prag) loop
2789 if Nkind (Prag) = N_Pragma
2790 and then Pragma_Name (Prag) = Name_Invariant
2791 then
2792 -- Nothing to do if the pragma was already processed
2794 if Contains (Pragmas_Seen, Prag) then
2795 return;
2797 -- Nothing to do when the caller requests the processing of all
2798 -- inherited class-wide invariants, but the pragma does not
2799 -- fall in this category.
2801 elsif not Class_Present (Prag) then
2802 return;
2803 end if;
2805 -- Extract the arguments of the invariant pragma
2807 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2808 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2809 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2810 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2812 -- The pragma applies to the partial view of the parent type
2814 if Present (Priv_Typ)
2815 and then Entity (Prag_Typ) = Priv_Typ
2816 then
2817 Par_Typ := Priv_Typ;
2819 -- The pragma applies to the full view of the parent type
2821 elsif Present (Full_Typ)
2822 and then Entity (Prag_Typ) = Full_Typ
2823 then
2824 Par_Typ := Full_Typ;
2826 -- Otherwise the pragma does not belong to the parent type and
2827 -- should not be considered.
2829 else
2830 return;
2831 end if;
2833 -- Perform the following substitutions:
2835 -- * Replace a reference to the _object parameter of the
2836 -- parent type's partial invariant procedure with a
2837 -- reference to the _object parameter of the derived
2838 -- type's full invariant procedure.
2840 -- * Replace a reference to a discriminant of the parent type
2841 -- with a suitable value from the point of view of the
2842 -- derived type.
2844 -- * Replace a call to an overridden parent primitive with a
2845 -- call to the overriding derived type primitive.
2847 -- * Replace a call to an inherited parent primitive with a
2848 -- call to the internally-generated inherited derived type
2849 -- primitive.
2851 Expr := New_Copy_Tree (Prag_Expr);
2853 -- The parent type must have a "partial" invariant procedure
2854 -- because class-wide invariants are captured exclusively by
2855 -- it.
2857 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2858 pragma Assert (Present (Par_Proc));
2860 Replace_References
2861 (Expr => Expr,
2862 Par_Typ => Par_Typ,
2863 Deriv_Typ => Deriv_Typ,
2864 Par_Obj => First_Formal (Par_Proc),
2865 Deriv_Obj => Obj_Id);
2867 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2868 end if;
2870 Next_Rep_Item (Prag);
2871 end loop;
2872 end Add_Inherited_Invariants;
2874 ------------------------------
2875 -- Add_Interface_Invariants --
2876 ------------------------------
2878 procedure Add_Interface_Invariants
2879 (T : Entity_Id;
2880 Obj_Id : Entity_Id;
2881 Checks : in out List_Id)
2883 Iface_Elmt : Elmt_Id;
2884 Ifaces : Elist_Id;
2886 begin
2887 -- Generate an invariant check for each class-wide invariant coming
2888 -- from all interfaces implemented by type T.
2890 if Is_Tagged_Type (T) then
2891 Collect_Interfaces (T, Ifaces);
2893 -- Process the class-wide invariants of all implemented interfaces
2895 Iface_Elmt := First_Elmt (Ifaces);
2896 while Present (Iface_Elmt) loop
2898 -- The Full_Typ parameter is intentionally left Empty because
2899 -- interfaces are treated as the partial view of a private type
2900 -- in order to achieve uniformity with the general case.
2902 Add_Inherited_Invariants
2903 (T => T,
2904 Priv_Typ => Node (Iface_Elmt),
2905 Full_Typ => Empty,
2906 Obj_Id => Obj_Id,
2907 Checks => Checks);
2909 Next_Elmt (Iface_Elmt);
2910 end loop;
2911 end if;
2912 end Add_Interface_Invariants;
2914 -------------------------
2915 -- Add_Invariant_Check --
2916 -------------------------
2918 procedure Add_Invariant_Check
2919 (Prag : Node_Id;
2920 Expr : Node_Id;
2921 Checks : in out List_Id;
2922 Inherited : Boolean := False)
2924 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2925 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2926 Ploc : constant Source_Ptr := Sloc (Prag);
2927 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2929 Assoc : List_Id;
2930 Str : String_Id;
2932 begin
2933 -- The invariant is ignored, nothing left to do
2935 if Is_Ignored (Prag) then
2936 null;
2938 -- Otherwise the invariant is checked. Build a pragma Check to verify
2939 -- the expression at run time.
2941 else
2942 Assoc := New_List (
2943 Make_Pragma_Argument_Association (Ploc,
2944 Expression => Make_Identifier (Ploc, Nam)),
2945 Make_Pragma_Argument_Association (Ploc,
2946 Expression => Expr));
2948 -- Handle the String argument (if any)
2950 if Present (Str_Arg) then
2951 Str := Strval (Get_Pragma_Arg (Str_Arg));
2953 -- When inheriting an invariant, modify the message from
2954 -- "failed invariant" to "failed inherited invariant".
2956 if Inherited then
2957 String_To_Name_Buffer (Str);
2959 if Name_Buffer (1 .. 16) = "failed invariant" then
2960 Insert_Str_In_Name_Buffer ("inherited ", 8);
2961 Str := String_From_Name_Buffer;
2962 end if;
2963 end if;
2965 Append_To (Assoc,
2966 Make_Pragma_Argument_Association (Ploc,
2967 Expression => Make_String_Literal (Ploc, Str)));
2968 end if;
2970 -- Generate:
2971 -- pragma Check (<Nam>, <Expr>, <Str>);
2973 Append_New_To (Checks,
2974 Make_Pragma (Ploc,
2975 Chars => Name_Check,
2976 Pragma_Argument_Associations => Assoc));
2977 end if;
2979 -- Output an info message when inheriting an invariant and the
2980 -- listing option is enabled.
2982 if Inherited and List_Inherited_Aspects then
2983 Error_Msg_Sloc := Sloc (Prag);
2984 Error_Msg_N
2985 ("info: & inherits `Invariant''Class` aspect from #?.l?", Typ);
2986 end if;
2988 -- Add the pragma to the list of processed pragmas
2990 Append_New_Elmt (Prag, Pragmas_Seen);
2991 Produced_Check := True;
2992 end Add_Invariant_Check;
2994 ---------------------------
2995 -- Add_Parent_Invariants --
2996 ---------------------------
2998 procedure Add_Parent_Invariants
2999 (T : Entity_Id;
3000 Obj_Id : Entity_Id;
3001 Checks : in out List_Id)
3003 Dummy_1 : Entity_Id;
3004 Dummy_2 : Entity_Id;
3006 Curr_Typ : Entity_Id;
3007 -- The entity of the current type being examined
3009 Full_Typ : Entity_Id;
3010 -- The full view of Par_Typ
3012 Par_Typ : Entity_Id;
3013 -- The entity of the parent type
3015 Priv_Typ : Entity_Id;
3016 -- The partial view of Par_Typ
3018 begin
3019 -- Do not process array types because they cannot have true parent
3020 -- types. This also prevents the generation of a duplicate invariant
3021 -- check when the input type is an array base type because its Etype
3022 -- denotes the first subtype, both of which share the same component
3023 -- type.
3025 if Is_Array_Type (T) then
3026 return;
3027 end if;
3029 -- Climb the parent type chain
3031 Curr_Typ := T;
3032 loop
3033 -- Do not consider subtypes as they inherit the invariants
3034 -- from their base types.
3036 Par_Typ := Base_Type (Etype (Curr_Typ));
3038 -- Stop the climb once the root of the parent chain is
3039 -- reached.
3041 exit when Curr_Typ = Par_Typ;
3043 -- Process the class-wide invariants of the parent type
3045 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
3047 -- Process the elements of an array type
3049 if Is_Array_Type (Full_Typ) then
3050 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
3052 -- Process the components of a record type
3054 elsif Ekind (Full_Typ) = E_Record_Type then
3055 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
3056 end if;
3058 Add_Inherited_Invariants
3059 (T => T,
3060 Priv_Typ => Priv_Typ,
3061 Full_Typ => Full_Typ,
3062 Obj_Id => Obj_Id,
3063 Checks => Checks);
3065 Curr_Typ := Par_Typ;
3066 end loop;
3067 end Add_Parent_Invariants;
3069 ------------------------
3070 -- Add_Own_Invariants --
3071 ------------------------
3073 procedure Add_Own_Invariants
3074 (T : Entity_Id;
3075 Obj_Id : Entity_Id;
3076 Checks : in out List_Id;
3077 Priv_Item : Node_Id := Empty)
3079 Expr : Node_Id;
3080 Prag : Node_Id;
3081 Prag_Asp : Node_Id;
3082 Prag_Expr : Node_Id;
3083 Prag_Expr_Arg : Node_Id;
3084 Prag_Typ : Node_Id;
3085 Prag_Typ_Arg : Node_Id;
3087 begin
3088 if No (T) then
3089 return;
3090 end if;
3092 Prag := First_Rep_Item (T);
3093 while Present (Prag) loop
3094 if Nkind (Prag) = N_Pragma
3095 and then Pragma_Name (Prag) = Name_Invariant
3096 then
3097 -- Stop the traversal of the rep item chain once a specific
3098 -- item is encountered.
3100 if Present (Priv_Item) and then Prag = Priv_Item then
3101 exit;
3102 end if;
3104 -- Nothing to do if the pragma was already processed
3106 if Contains (Pragmas_Seen, Prag) then
3107 return;
3108 end if;
3110 -- Extract the arguments of the invariant pragma
3112 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
3113 Prag_Expr_Arg := Next (Prag_Typ_Arg);
3114 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
3115 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
3116 Prag_Asp := Corresponding_Aspect (Prag);
3118 -- Verify the pragma belongs to T, otherwise the pragma applies
3119 -- to a parent type in which case it will be processed later by
3120 -- Add_Parent_Invariants or Add_Interface_Invariants.
3122 if Entity (Prag_Typ) /= T then
3123 return;
3124 end if;
3126 -- We need to preanalyze the expression itself inside a generic
3127 -- to be able to capture global references present in it.
3129 if Inside_A_Generic then
3130 Expr := Prag_Expr;
3131 else
3132 Expr := New_Copy_Tree (Prag_Expr);
3133 end if;
3135 -- Substitute all references to type T with references to the
3136 -- _object formal parameter.
3138 Replace_Type_References (Expr, T, Obj_Id);
3140 -- Preanalyze the invariant expression to detect errors and at
3141 -- the same time capture the visibility of the proper package
3142 -- part.
3144 Set_Parent (Expr, Parent (Prag_Expr));
3145 Preanalyze_Assert_Expression (Expr, Any_Boolean);
3147 -- Save a copy of the expression when T is tagged to detect
3148 -- errors and capture the visibility of the proper package part
3149 -- for the generation of inherited type invariants.
3151 if Is_Tagged_Type (T) then
3152 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
3153 end if;
3155 -- If the pragma comes from an aspect specification, replace
3156 -- the saved expression because all type references must be
3157 -- substituted for the call to Preanalyze_Spec_Expression in
3158 -- Check_Aspect_At_xxx routines.
3160 if Present (Prag_Asp) then
3161 Set_Expression_Copy (Prag_Asp, New_Copy_Tree (Expr));
3162 end if;
3164 Add_Invariant_Check (Prag, Expr, Checks);
3165 end if;
3167 Next_Rep_Item (Prag);
3168 end loop;
3169 end Add_Own_Invariants;
3171 -------------------------------------
3172 -- Add_Record_Component_Invariants --
3173 -------------------------------------
3175 procedure Add_Record_Component_Invariants
3176 (T : Entity_Id;
3177 Obj_Id : Entity_Id;
3178 Checks : in out List_Id)
3180 procedure Process_Component_List
3181 (Comp_List : Node_Id;
3182 CL_Checks : in out List_Id);
3183 -- Generate invariant checks for all record components found in
3184 -- component list Comp_List, including variant parts. All created
3185 -- checks are added to list CL_Checks.
3187 procedure Process_Record_Component
3188 (Comp_Id : Entity_Id;
3189 Comp_Checks : in out List_Id);
3190 -- Generate an invariant check for a record component identified by
3191 -- Comp_Id. All created checks are added to list Comp_Checks.
3193 ----------------------------
3194 -- Process_Component_List --
3195 ----------------------------
3197 procedure Process_Component_List
3198 (Comp_List : Node_Id;
3199 CL_Checks : in out List_Id)
3201 Comp : Node_Id;
3202 Var : Node_Id;
3203 Var_Alts : List_Id := No_List;
3204 Var_Checks : List_Id := No_List;
3205 Var_Stmts : List_Id;
3207 Produced_Variant_Check : Boolean := False;
3208 -- This flag tracks whether the component has produced at least
3209 -- one invariant check.
3211 begin
3212 -- Traverse the component items
3214 Comp := First (Component_Items (Comp_List));
3215 while Present (Comp) loop
3216 if Nkind (Comp) = N_Component_Declaration then
3218 -- Generate the component invariant check
3220 Process_Record_Component
3221 (Comp_Id => Defining_Entity (Comp),
3222 Comp_Checks => CL_Checks);
3223 end if;
3225 Next (Comp);
3226 end loop;
3228 -- Traverse the variant part
3230 if Present (Variant_Part (Comp_List)) then
3231 Var := First (Variants (Variant_Part (Comp_List)));
3232 while Present (Var) loop
3233 Var_Checks := No_List;
3235 -- Generate invariant checks for all components and variant
3236 -- parts that qualify.
3238 Process_Component_List
3239 (Comp_List => Component_List (Var),
3240 CL_Checks => Var_Checks);
3242 -- The components of the current variant produced at least
3243 -- one invariant check.
3245 if Present (Var_Checks) then
3246 Var_Stmts := Var_Checks;
3247 Produced_Variant_Check := True;
3249 -- Otherwise there are either no components with invariants,
3250 -- assertions are disabled, or Assertion_Policy Ignore is in
3251 -- effect.
3253 else
3254 Var_Stmts := New_List (Make_Null_Statement (Loc));
3255 end if;
3257 Append_New_To (Var_Alts,
3258 Make_Case_Statement_Alternative (Loc,
3259 Discrete_Choices =>
3260 New_Copy_List (Discrete_Choices (Var)),
3261 Statements => Var_Stmts));
3263 Next (Var);
3264 end loop;
3266 -- Create a case statement which verifies the invariant checks
3267 -- of a particular component list depending on the discriminant
3268 -- values only when there is at least one real invariant check.
3270 if Produced_Variant_Check then
3271 Append_New_To (CL_Checks,
3272 Make_Case_Statement (Loc,
3273 Expression =>
3274 Make_Selected_Component (Loc,
3275 Prefix => New_Occurrence_Of (Obj_Id, Loc),
3276 Selector_Name =>
3277 New_Occurrence_Of
3278 (Entity (Name (Variant_Part (Comp_List))), Loc)),
3279 Alternatives => Var_Alts));
3280 end if;
3281 end if;
3282 end Process_Component_List;
3284 ------------------------------
3285 -- Process_Record_Component --
3286 ------------------------------
3288 procedure Process_Record_Component
3289 (Comp_Id : Entity_Id;
3290 Comp_Checks : in out List_Id)
3292 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
3293 Proc_Id : Entity_Id;
3295 Produced_Component_Check : Boolean := False;
3296 -- This flag tracks whether the component has produced at least
3297 -- one invariant check.
3299 begin
3300 -- Nothing to do for internal component _parent. Note that it is
3301 -- not desirable to check whether the component comes from source
3302 -- because protected type components are relocated to an internal
3303 -- corresponding record, but still need processing.
3305 if Chars (Comp_Id) = Name_uParent then
3306 return;
3307 end if;
3309 -- Verify the invariant of the component. Note that an access
3310 -- type may have an invariant when it acts as the full view of a
3311 -- private type and the invariant appears on the partial view. In
3312 -- this case verify the access value itself.
3314 if Has_Invariants (Comp_Typ) then
3316 -- In GNATprove mode, the component invariants are checked by
3317 -- other means. They should not be added to the record type
3318 -- invariant procedure, so that the procedure can be used to
3319 -- check the record type invariants if any.
3321 if GNATprove_Mode then
3322 null;
3324 else
3325 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
3327 -- The component type should have an invariant procedure
3328 -- if it has invariants of its own or inherits class-wide
3329 -- invariants from parent or interface types.
3331 -- However, given that the invariant procedure is built by
3332 -- the expander, it is not available compiling generic units
3333 -- or when the sources have errors, since expansion is then
3334 -- disabled.
3336 pragma Assert (Present (Proc_Id)
3337 or else not Expander_Active);
3339 -- Generate:
3340 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
3342 -- Note that the invariant procedure may have a null body if
3343 -- assertions are disabled or Assertion_Policy Ignore is in
3344 -- effect.
3346 if Present (Proc_Id)
3347 and then not Has_Null_Body (Proc_Id)
3348 then
3349 Append_New_To (Comp_Checks,
3350 Make_Procedure_Call_Statement (Loc,
3351 Name =>
3352 New_Occurrence_Of (Proc_Id, Loc),
3353 Parameter_Associations => New_List (
3354 Make_Selected_Component (Loc,
3355 Prefix =>
3356 Unchecked_Convert_To
3357 (T, New_Occurrence_Of (Obj_Id, Loc)),
3358 Selector_Name =>
3359 New_Occurrence_Of (Comp_Id, Loc)))));
3360 end if;
3361 end if;
3363 Produced_Check := True;
3364 Produced_Component_Check := True;
3365 end if;
3367 if Produced_Component_Check and then Has_Unchecked_Union (T) then
3368 Error_Msg_NE
3369 ("invariants cannot be checked on components of "
3370 & "unchecked_union type &??", Comp_Id, T);
3371 end if;
3372 end Process_Record_Component;
3374 -- Local variables
3376 Comps : Node_Id;
3377 Def : Node_Id;
3379 -- Start of processing for Add_Record_Component_Invariants
3381 begin
3382 -- An untagged derived type inherits the components of its parent
3383 -- type. In order to avoid creating redundant invariant checks, do
3384 -- not process the components now. Instead wait until the ultimate
3385 -- parent of the untagged derivation chain is reached.
3387 if not Is_Untagged_Derivation (T) then
3388 Def := Type_Definition (Parent (T));
3390 if Nkind (Def) = N_Derived_Type_Definition then
3391 Def := Record_Extension_Part (Def);
3392 end if;
3394 pragma Assert (Nkind (Def) = N_Record_Definition);
3395 Comps := Component_List (Def);
3397 if Present (Comps) then
3398 Process_Component_List
3399 (Comp_List => Comps,
3400 CL_Checks => Checks);
3401 end if;
3402 end if;
3403 end Add_Record_Component_Invariants;
3405 -- Local variables
3407 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3408 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3409 -- Save the Ghost-related attributes to restore on exit
3411 Dummy : Entity_Id;
3412 Priv_Item : Node_Id;
3413 Proc_Body : Node_Id;
3414 Proc_Body_Id : Entity_Id;
3415 Proc_Decl : Node_Id;
3416 Proc_Id : Entity_Id;
3417 Stmts : List_Id := No_List;
3419 CRec_Typ : Entity_Id := Empty;
3420 -- The corresponding record type of Full_Typ
3422 Full_Proc : Entity_Id := Empty;
3423 -- The entity of the "full" invariant procedure
3425 Full_Typ : Entity_Id := Empty;
3426 -- The full view of the working type
3428 Obj_Id : Entity_Id := Empty;
3429 -- The _object formal parameter of the invariant procedure
3431 Part_Proc : Entity_Id := Empty;
3432 -- The entity of the "partial" invariant procedure
3434 Priv_Typ : Entity_Id := Empty;
3435 -- The partial view of the working type
3437 Work_Typ : Entity_Id := Empty;
3438 -- The working type
3440 -- Start of processing for Build_Invariant_Procedure_Body
3442 begin
3443 Work_Typ := Typ;
3445 -- Do not process the underlying full view of a private type. There is
3446 -- no way to get back to the partial view, plus the body will be built
3447 -- by the full view or the base type.
3449 if Is_Underlying_Full_View (Work_Typ) then
3450 return;
3452 -- The input type denotes the implementation base type of a constrained
3453 -- array type. Work with the first subtype as all invariant pragmas are
3454 -- on its rep item chain.
3456 elsif Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3457 Work_Typ := First_Subtype (Work_Typ);
3459 -- The input type denotes the corresponding record type of a protected
3460 -- or task type. Work with the concurrent type because the corresponding
3461 -- record type may not be visible to clients of the type.
3463 elsif Ekind (Work_Typ) = E_Record_Type
3464 and then Is_Concurrent_Record_Type (Work_Typ)
3465 then
3466 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3467 end if;
3469 -- The working type may be subject to pragma Ghost. Set the mode now to
3470 -- ensure that the invariant procedure is properly marked as Ghost.
3472 Set_Ghost_Mode (Work_Typ);
3474 -- The type must either have invariants of its own, inherit class-wide
3475 -- invariants from parent types or interfaces, or be an array or record
3476 -- type whose components have invariants.
3478 pragma Assert (Has_Invariants (Work_Typ));
3480 -- Interfaces are treated as the partial view of a private type in order
3481 -- to achieve uniformity with the general case.
3483 if Is_Interface (Work_Typ) then
3484 Priv_Typ := Work_Typ;
3486 -- Otherwise obtain both views of the type
3488 else
3489 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3490 end if;
3492 -- The caller requests a body for the partial invariant procedure
3494 if Partial_Invariant then
3495 Full_Proc := Invariant_Procedure (Work_Typ);
3496 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3498 -- The "full" invariant procedure body was already created
3500 if Present (Full_Proc)
3501 and then Present
3502 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3503 then
3504 -- This scenario happens only when the type is an untagged
3505 -- derivation from a private parent and the underlying full
3506 -- view was processed before the partial view.
3508 pragma Assert
3509 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3511 -- Nothing to do because the processing of the underlying full
3512 -- view already checked the invariants of the partial view.
3514 goto Leave;
3515 end if;
3517 -- Create a declaration for the "partial" invariant procedure if it
3518 -- is not available.
3520 if No (Proc_Id) then
3521 Build_Invariant_Procedure_Declaration
3522 (Typ => Work_Typ,
3523 Partial_Invariant => True);
3525 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3526 end if;
3528 -- The caller requests a body for the "full" invariant procedure
3530 else
3531 Proc_Id := Invariant_Procedure (Work_Typ);
3532 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3534 -- Create a declaration for the "full" invariant procedure if it is
3535 -- not available.
3537 if No (Proc_Id) then
3538 Build_Invariant_Procedure_Declaration (Work_Typ);
3539 Proc_Id := Invariant_Procedure (Work_Typ);
3540 end if;
3541 end if;
3543 -- At this point there should be an invariant procedure declaration
3545 pragma Assert (Present (Proc_Id));
3546 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3548 -- Nothing to do if the invariant procedure already has a body
3550 if Present (Corresponding_Body (Proc_Decl)) then
3551 goto Leave;
3552 end if;
3554 -- Emulate the environment of the invariant procedure by installing its
3555 -- scope and formal parameters. Note that this is not needed, but having
3556 -- the scope installed helps with the detection of invariant-related
3557 -- errors.
3559 Push_Scope (Proc_Id);
3560 Install_Formals (Proc_Id);
3562 Obj_Id := First_Formal (Proc_Id);
3563 pragma Assert (Present (Obj_Id));
3565 -- The "partial" invariant procedure verifies the invariants of the
3566 -- partial view only.
3568 if Partial_Invariant then
3569 pragma Assert (Present (Priv_Typ));
3571 Add_Own_Invariants
3572 (T => Priv_Typ,
3573 Obj_Id => Obj_Id,
3574 Checks => Stmts);
3576 -- Otherwise the "full" invariant procedure verifies the invariants of
3577 -- the full view, all array or record components, as well as class-wide
3578 -- invariants inherited from parent types or interfaces. In addition, it
3579 -- indirectly verifies the invariants of the partial view by calling the
3580 -- "partial" invariant procedure.
3582 else
3583 pragma Assert (Present (Full_Typ));
3585 -- Check the invariants of the partial view by calling the "partial"
3586 -- invariant procedure. Generate:
3588 -- <Work_Typ>Partial_Invariant (_object);
3590 if Present (Part_Proc) then
3591 Append_New_To (Stmts,
3592 Make_Procedure_Call_Statement (Loc,
3593 Name => New_Occurrence_Of (Part_Proc, Loc),
3594 Parameter_Associations => New_List (
3595 New_Occurrence_Of (Obj_Id, Loc))));
3597 Produced_Check := True;
3598 end if;
3600 Priv_Item := Empty;
3602 -- Derived subtypes do not have a partial view
3604 if Present (Priv_Typ) then
3606 -- The processing of the "full" invariant procedure intentionally
3607 -- skips the partial view because a) this may result in changes of
3608 -- visibility and b) lead to duplicate checks. However, when the
3609 -- full view is the underlying full view of an untagged derived
3610 -- type whose parent type is private, partial invariants appear on
3611 -- the rep item chain of the partial view only.
3613 -- package Pack_1 is
3614 -- type Root ... is private;
3615 -- private
3616 -- <full view of Root>
3617 -- end Pack_1;
3619 -- with Pack_1;
3620 -- package Pack_2 is
3621 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3622 -- <underlying full view of Child>
3623 -- end Pack_2;
3625 -- As a result, the processing of the full view must also consider
3626 -- all invariants of the partial view.
3628 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3629 null;
3631 -- Otherwise the invariants of the partial view are ignored
3633 else
3634 -- Note that the rep item chain is shared between the partial
3635 -- and full views of a type. To avoid processing the invariants
3636 -- of the partial view, signal the logic to stop when the first
3637 -- rep item of the partial view has been reached.
3639 Priv_Item := First_Rep_Item (Priv_Typ);
3641 -- Ignore the invariants of the partial view by eliminating the
3642 -- view.
3644 Priv_Typ := Empty;
3645 end if;
3646 end if;
3648 -- Process the invariants of the full view and in certain cases those
3649 -- of the partial view. This also handles any invariants on array or
3650 -- record components.
3652 Add_Own_Invariants
3653 (T => Priv_Typ,
3654 Obj_Id => Obj_Id,
3655 Checks => Stmts,
3656 Priv_Item => Priv_Item);
3658 Add_Own_Invariants
3659 (T => Full_Typ,
3660 Obj_Id => Obj_Id,
3661 Checks => Stmts,
3662 Priv_Item => Priv_Item);
3664 -- Process the elements of an array type
3666 if Is_Array_Type (Full_Typ) then
3667 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3669 -- Process the components of a record type
3671 elsif Ekind (Full_Typ) = E_Record_Type then
3672 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3674 -- Process the components of a corresponding record
3676 elsif Present (CRec_Typ) then
3677 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3678 end if;
3680 -- Process the inherited class-wide invariants of all parent types.
3681 -- This also handles any invariants on record components.
3683 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3685 -- Process the inherited class-wide invariants of all implemented
3686 -- interface types.
3688 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3689 end if;
3691 End_Scope;
3693 -- At this point there should be at least one invariant check. If this
3694 -- is not the case, then the invariant-related flags were not properly
3695 -- set, or there is a missing invariant procedure on one of the array
3696 -- or record components.
3698 pragma Assert (Produced_Check);
3700 -- Account for the case where assertions are disabled or all invariant
3701 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3702 -- empty body.
3704 if No (Stmts) then
3705 Stmts := New_List (Make_Null_Statement (Loc));
3706 end if;
3708 -- Generate:
3709 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3710 -- begin
3711 -- <Stmts>
3712 -- end <Work_Typ>[Partial_]Invariant;
3714 Proc_Body :=
3715 Make_Subprogram_Body (Loc,
3716 Specification =>
3717 Copy_Subprogram_Spec (Parent (Proc_Id)),
3718 Declarations => Empty_List,
3719 Handled_Statement_Sequence =>
3720 Make_Handled_Sequence_Of_Statements (Loc,
3721 Statements => Stmts));
3722 Proc_Body_Id := Defining_Entity (Proc_Body);
3724 -- Perform minor decoration in case the body is not analyzed
3726 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
3727 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3728 Set_Scope (Proc_Body_Id, Current_Scope);
3730 -- Link both spec and body to avoid generating duplicates
3732 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3733 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3735 -- The body should not be inserted into the tree when the context is
3736 -- a generic unit because it is not part of the template. Note
3737 -- that the body must still be generated in order to resolve the
3738 -- invariants.
3740 if Inside_A_Generic then
3741 null;
3743 -- Semi-insert the body into the tree for GNATprove by setting its
3744 -- Parent field. This allows for proper upstream tree traversals.
3746 elsif GNATprove_Mode then
3747 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3749 -- Otherwise the body is part of the freezing actions of the type
3751 else
3752 Append_Freeze_Action (Work_Typ, Proc_Body);
3753 end if;
3755 <<Leave>>
3756 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3757 end Build_Invariant_Procedure_Body;
3759 -------------------------------------------
3760 -- Build_Invariant_Procedure_Declaration --
3761 -------------------------------------------
3763 -- WARNING: This routine manages Ghost regions. Return statements must be
3764 -- replaced by gotos which jump to the end of the routine and restore the
3765 -- Ghost mode.
3767 procedure Build_Invariant_Procedure_Declaration
3768 (Typ : Entity_Id;
3769 Partial_Invariant : Boolean := False)
3771 Loc : constant Source_Ptr := Sloc (Typ);
3773 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3774 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3775 -- Save the Ghost-related attributes to restore on exit
3777 Proc_Decl : Node_Id;
3778 Proc_Id : Entity_Id;
3779 Proc_Nam : Name_Id;
3780 Typ_Decl : Node_Id;
3782 CRec_Typ : Entity_Id;
3783 -- The corresponding record type of Full_Typ
3785 Full_Typ : Entity_Id;
3786 -- The full view of working type
3788 Obj_Id : Entity_Id;
3789 -- The _object formal parameter of the invariant procedure
3791 Obj_Typ : Entity_Id;
3792 -- The type of the _object formal parameter
3794 Priv_Typ : Entity_Id;
3795 -- The partial view of working type
3797 UFull_Typ : Entity_Id;
3798 -- The underlying full view of Full_Typ
3800 Work_Typ : Entity_Id;
3801 -- The working type
3803 begin
3804 Work_Typ := Typ;
3806 -- The input type denotes the implementation base type of a constrained
3807 -- array type. Work with the first subtype as all invariant pragmas are
3808 -- on its rep item chain.
3810 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3811 Work_Typ := First_Subtype (Work_Typ);
3813 -- The input denotes the corresponding record type of a protected or a
3814 -- task type. Work with the concurrent type because the corresponding
3815 -- record type may not be visible to clients of the type.
3817 elsif Ekind (Work_Typ) = E_Record_Type
3818 and then Is_Concurrent_Record_Type (Work_Typ)
3819 then
3820 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3821 end if;
3823 -- The working type may be subject to pragma Ghost. Set the mode now to
3824 -- ensure that the invariant procedure is properly marked as Ghost.
3826 Set_Ghost_Mode (Work_Typ);
3828 -- The type must either have invariants of its own, inherit class-wide
3829 -- invariants from parent or interface types, or be an array or record
3830 -- type whose components have invariants.
3832 pragma Assert (Has_Invariants (Work_Typ));
3834 -- Nothing to do if the type already has a "partial" invariant procedure
3836 if Partial_Invariant then
3837 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3838 goto Leave;
3839 end if;
3841 -- Nothing to do if the type already has a "full" invariant procedure
3843 elsif Present (Invariant_Procedure (Work_Typ)) then
3844 goto Leave;
3845 end if;
3847 -- The caller requests the declaration of the "partial" invariant
3848 -- procedure.
3850 if Partial_Invariant then
3851 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3853 -- Otherwise the caller requests the declaration of the "full" invariant
3854 -- procedure.
3856 else
3857 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3858 end if;
3860 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3862 -- Perform minor decoration in case the declaration is not analyzed
3864 Mutate_Ekind (Proc_Id, E_Procedure);
3865 Set_Etype (Proc_Id, Standard_Void_Type);
3866 Set_Scope (Proc_Id, Current_Scope);
3868 if Partial_Invariant then
3869 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3870 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3871 else
3872 Set_Is_Invariant_Procedure (Proc_Id);
3873 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3874 end if;
3876 -- The invariant procedure requires debug info when the invariants are
3877 -- subject to Source Coverage Obligations.
3879 if Generate_SCO then
3880 Set_Debug_Info_Needed (Proc_Id);
3881 end if;
3883 -- Obtain all views of the input type
3885 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
3887 -- Associate the invariant procedure and various flags with all views
3889 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3890 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3891 Propagate_Invariant_Attributes (UFull_Typ, From_Typ => Work_Typ);
3892 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3894 -- The declaration of the invariant procedure is inserted after the
3895 -- declaration of the partial view as this allows for proper external
3896 -- visibility.
3898 if Present (Priv_Typ) then
3899 Typ_Decl := Declaration_Node (Priv_Typ);
3901 -- Anonymous arrays in object declarations have no explicit declaration
3902 -- so use the related object declaration as the insertion point.
3904 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3905 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3907 -- Derived types with the full view as parent do not have a partial
3908 -- view. Insert the invariant procedure after the derived type.
3910 else
3911 Typ_Decl := Declaration_Node (Full_Typ);
3912 end if;
3914 -- The type should have a declarative node
3916 pragma Assert (Present (Typ_Decl));
3918 -- Create the formal parameter which emulates the variable-like behavior
3919 -- of the current type instance.
3921 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3923 -- When generating an invariant procedure declaration for an abstract
3924 -- type (including interfaces), use the class-wide type as the _object
3925 -- type. This has several desirable effects:
3927 -- * The invariant procedure does not become a primitive of the type.
3928 -- This eliminates the need to either special case the treatment of
3929 -- invariant procedures, or to make it a predefined primitive and
3930 -- force every derived type to potentially provide an empty body.
3932 -- * The invariant procedure does not need to be declared as abstract.
3933 -- This allows for a proper body, which in turn avoids redundant
3934 -- processing of the same invariants for types with multiple views.
3936 -- * The class-wide type allows for calls to abstract primitives
3937 -- within a nonabstract subprogram. The calls are treated as
3938 -- dispatching and require additional processing when they are
3939 -- remapped to call primitives of derived types. See routine
3940 -- Replace_References for details.
3942 if Is_Abstract_Type (Work_Typ) then
3943 Obj_Typ := Class_Wide_Type (Work_Typ);
3944 else
3945 Obj_Typ := Work_Typ;
3946 end if;
3948 -- Perform minor decoration in case the declaration is not analyzed
3950 Mutate_Ekind (Obj_Id, E_In_Parameter);
3951 Set_Etype (Obj_Id, Obj_Typ);
3952 Set_Scope (Obj_Id, Proc_Id);
3954 Set_First_Entity (Proc_Id, Obj_Id);
3955 Set_Last_Entity (Proc_Id, Obj_Id);
3957 -- Generate:
3958 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3960 Proc_Decl :=
3961 Make_Subprogram_Declaration (Loc,
3962 Specification =>
3963 Make_Procedure_Specification (Loc,
3964 Defining_Unit_Name => Proc_Id,
3965 Parameter_Specifications => New_List (
3966 Make_Parameter_Specification (Loc,
3967 Defining_Identifier => Obj_Id,
3968 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3970 -- The declaration should not be inserted into the tree when the context
3971 -- is a generic unit because it is not part of the template.
3973 if Inside_A_Generic then
3974 null;
3976 -- Semi-insert the declaration into the tree for GNATprove by setting
3977 -- its Parent field. This allows for proper upstream tree traversals.
3979 elsif GNATprove_Mode then
3980 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3982 -- Otherwise insert the declaration
3984 else
3985 pragma Assert (Present (Typ_Decl));
3986 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3987 end if;
3989 <<Leave>>
3990 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3991 end Build_Invariant_Procedure_Declaration;
3993 --------------------------
3994 -- Build_Procedure_Form --
3995 --------------------------
3997 procedure Build_Procedure_Form (N : Node_Id) is
3998 Loc : constant Source_Ptr := Sloc (N);
3999 Subp : constant Entity_Id := Defining_Entity (N);
4001 Func_Formal : Entity_Id;
4002 Proc_Formals : List_Id;
4003 Proc_Decl : Node_Id;
4005 begin
4006 -- No action needed if this transformation was already done, or in case
4007 -- of subprogram renaming declarations.
4009 if Nkind (Specification (N)) = N_Procedure_Specification
4010 or else Nkind (N) = N_Subprogram_Renaming_Declaration
4011 then
4012 return;
4013 end if;
4015 -- Ditto when dealing with an expression function, where both the
4016 -- original expression and the generated declaration end up being
4017 -- expanded here.
4019 if Rewritten_For_C (Subp) then
4020 return;
4021 end if;
4023 Proc_Formals := New_List;
4025 -- Create a list of formal parameters with the same types as the
4026 -- function.
4028 Func_Formal := First_Formal (Subp);
4029 while Present (Func_Formal) loop
4030 Append_To (Proc_Formals,
4031 Make_Parameter_Specification (Loc,
4032 Defining_Identifier =>
4033 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
4034 Parameter_Type =>
4035 New_Occurrence_Of (Etype (Func_Formal), Loc)));
4037 Next_Formal (Func_Formal);
4038 end loop;
4040 -- Add an extra out parameter to carry the function result
4042 Append_To (Proc_Formals,
4043 Make_Parameter_Specification (Loc,
4044 Defining_Identifier =>
4045 Make_Defining_Identifier (Loc, Name_UP_RESULT),
4046 Out_Present => True,
4047 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
4049 -- The new procedure declaration is inserted before the function
4050 -- declaration. The processing in Build_Procedure_Body_Form relies on
4051 -- this order. Note that we insert before because in the case of a
4052 -- function body with no separate spec, we do not want to insert the
4053 -- new spec after the body which will later get rewritten.
4055 Proc_Decl :=
4056 Make_Subprogram_Declaration (Loc,
4057 Specification =>
4058 Make_Procedure_Specification (Loc,
4059 Defining_Unit_Name =>
4060 Make_Defining_Identifier (Loc, Chars (Subp)),
4061 Parameter_Specifications => Proc_Formals));
4063 Insert_Before_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
4065 -- Entity of procedure must remain invisible so that it does not
4066 -- overload subsequent references to the original function.
4068 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
4070 -- Mark the function as having a procedure form and link the function
4071 -- and its internally built procedure.
4073 Set_Rewritten_For_C (Subp);
4074 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
4075 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
4076 end Build_Procedure_Form;
4078 ------------------------
4079 -- Build_Runtime_Call --
4080 ------------------------
4082 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
4083 begin
4084 -- If entity is not available, we can skip making the call (this avoids
4085 -- junk duplicated error messages in a number of cases).
4087 if not RTE_Available (RE) then
4088 return Make_Null_Statement (Loc);
4089 else
4090 return
4091 Make_Procedure_Call_Statement (Loc,
4092 Name => New_Occurrence_Of (RTE (RE), Loc));
4093 end if;
4094 end Build_Runtime_Call;
4096 ------------------------
4097 -- Build_SS_Mark_Call --
4098 ------------------------
4100 function Build_SS_Mark_Call
4101 (Loc : Source_Ptr;
4102 Mark : Entity_Id) return Node_Id
4104 begin
4105 -- Generate:
4106 -- Mark : constant Mark_Id := SS_Mark;
4108 return
4109 Make_Object_Declaration (Loc,
4110 Defining_Identifier => Mark,
4111 Constant_Present => True,
4112 Object_Definition =>
4113 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
4114 Expression =>
4115 Make_Function_Call (Loc,
4116 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
4117 end Build_SS_Mark_Call;
4119 ---------------------------
4120 -- Build_SS_Release_Call --
4121 ---------------------------
4123 function Build_SS_Release_Call
4124 (Loc : Source_Ptr;
4125 Mark : Entity_Id) return Node_Id
4127 begin
4128 -- Generate:
4129 -- SS_Release (Mark);
4131 return
4132 Make_Procedure_Call_Statement (Loc,
4133 Name =>
4134 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
4135 Parameter_Associations => New_List (
4136 New_Occurrence_Of (Mark, Loc)));
4137 end Build_SS_Release_Call;
4139 ----------------------------
4140 -- Build_Task_Array_Image --
4141 ----------------------------
4143 -- This function generates the body for a function that constructs the
4144 -- image string for a task that is an array component. The function is
4145 -- local to the init proc for the array type, and is called for each one
4146 -- of the components. The constructed image has the form of an indexed
4147 -- component, whose prefix is the outer variable of the array type.
4148 -- The n-dimensional array type has known indexes Index, Index2...
4150 -- Id_Ref is an indexed component form created by the enclosing init proc.
4151 -- Its successive indexes are Val1, Val2, ... which are the loop variables
4152 -- in the loops that call the individual task init proc on each component.
4154 -- The generated function has the following structure:
4156 -- function F return String is
4157 -- Pref : String renames Task_Name;
4158 -- T1 : constant String := Index1'Image (Val1);
4159 -- ...
4160 -- Tn : constant String := Indexn'Image (Valn);
4161 -- Len : constant Integer :=
4162 -- Pref'Length + T1'Length + ... + Tn'Length + n + 1;
4163 -- -- Len includes commas and the end parentheses
4165 -- Res : String (1 .. Len);
4166 -- Pos : Integer := Pref'Length;
4168 -- begin
4169 -- Res (1 .. Pos) := Pref;
4170 -- Pos := Pos + 1;
4171 -- Res (Pos) := '(';
4172 -- Pos := Pos + 1;
4173 -- Res (Pos .. Pos + T1'Length - 1) := T1;
4174 -- Pos := Pos + T1'Length;
4175 -- Res (Pos) := '.';
4176 -- Pos := Pos + 1;
4177 -- ...
4178 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
4179 -- Res (Len) := ')';
4181 -- return Res;
4182 -- end F;
4184 -- Needless to say, multidimensional arrays of tasks are rare enough that
4185 -- the bulkiness of this code is not really a concern.
4187 function Build_Task_Array_Image
4188 (Loc : Source_Ptr;
4189 Id_Ref : Node_Id;
4190 A_Type : Entity_Id;
4191 Dyn : Boolean := False) return Node_Id
4193 Dims : constant Nat := Number_Dimensions (A_Type);
4194 -- Number of dimensions for array of tasks
4196 Temps : array (1 .. Dims) of Entity_Id;
4197 -- Array of temporaries to hold string for each index
4199 Indx : Node_Id;
4200 -- Index expression
4202 Len : Entity_Id;
4203 -- Total length of generated name
4205 Pos : Entity_Id;
4206 -- Running index for substring assignments
4208 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4209 -- Name of enclosing variable, prefix of resulting name
4211 Res : Entity_Id;
4212 -- String to hold result
4214 Val : Node_Id;
4215 -- Value of successive indexes
4217 Sum : Node_Id;
4218 -- Expression to compute total size of string
4220 T : Entity_Id;
4221 -- Entity for name at one index position
4223 Decls : constant List_Id := New_List;
4224 Stats : constant List_Id := New_List;
4226 begin
4227 -- For a dynamic task, the name comes from the target variable. For a
4228 -- static one it is a formal of the enclosing init proc.
4230 if Dyn then
4231 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4232 Append_To (Decls,
4233 Make_Object_Declaration (Loc,
4234 Defining_Identifier => Pref,
4235 Constant_Present => True,
4236 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4237 Expression =>
4238 Make_String_Literal (Loc,
4239 Strval => String_From_Name_Buffer)));
4241 else
4242 Append_To (Decls,
4243 Make_Object_Renaming_Declaration (Loc,
4244 Defining_Identifier => Pref,
4245 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4246 Name => Make_Identifier (Loc, Name_uTask_Name)));
4247 end if;
4249 Indx := First_Index (A_Type);
4250 Val := First (Expressions (Id_Ref));
4252 for J in 1 .. Dims loop
4253 T := Make_Temporary (Loc, 'T');
4254 Temps (J) := T;
4256 Append_To (Decls,
4257 Make_Object_Declaration (Loc,
4258 Defining_Identifier => T,
4259 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4260 Constant_Present => True,
4261 Expression =>
4262 Make_Attribute_Reference (Loc,
4263 Attribute_Name => Name_Image,
4264 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
4265 Expressions => New_List (New_Copy_Tree (Val)))));
4267 Next_Index (Indx);
4268 Next (Val);
4269 end loop;
4271 Sum := Make_Integer_Literal (Loc, Dims + 1);
4273 Sum :=
4274 Make_Op_Add (Loc,
4275 Left_Opnd => Sum,
4276 Right_Opnd =>
4277 Make_Attribute_Reference (Loc,
4278 Attribute_Name => Name_Length,
4279 Prefix => New_Occurrence_Of (Pref, Loc),
4280 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4282 for J in 1 .. Dims loop
4283 Sum :=
4284 Make_Op_Add (Loc,
4285 Left_Opnd => Sum,
4286 Right_Opnd =>
4287 Make_Attribute_Reference (Loc,
4288 Attribute_Name => Name_Length,
4289 Prefix =>
4290 New_Occurrence_Of (Temps (J), Loc),
4291 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4292 end loop;
4294 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4296 Set_Character_Literal_Name (Get_Char_Code ('('));
4298 Append_To (Stats,
4299 Make_Assignment_Statement (Loc,
4300 Name =>
4301 Make_Indexed_Component (Loc,
4302 Prefix => New_Occurrence_Of (Res, Loc),
4303 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4304 Expression =>
4305 Make_Character_Literal (Loc,
4306 Chars => Name_Find,
4307 Char_Literal_Value => UI_From_CC (Get_Char_Code ('(')))));
4309 Append_To (Stats,
4310 Make_Assignment_Statement (Loc,
4311 Name => New_Occurrence_Of (Pos, Loc),
4312 Expression =>
4313 Make_Op_Add (Loc,
4314 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4315 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4317 for J in 1 .. Dims loop
4319 Append_To (Stats,
4320 Make_Assignment_Statement (Loc,
4321 Name =>
4322 Make_Slice (Loc,
4323 Prefix => New_Occurrence_Of (Res, Loc),
4324 Discrete_Range =>
4325 Make_Range (Loc,
4326 Low_Bound => New_Occurrence_Of (Pos, Loc),
4327 High_Bound =>
4328 Make_Op_Subtract (Loc,
4329 Left_Opnd =>
4330 Make_Op_Add (Loc,
4331 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4332 Right_Opnd =>
4333 Make_Attribute_Reference (Loc,
4334 Attribute_Name => Name_Length,
4335 Prefix =>
4336 New_Occurrence_Of (Temps (J), Loc),
4337 Expressions =>
4338 New_List (Make_Integer_Literal (Loc, 1)))),
4339 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
4341 Expression => New_Occurrence_Of (Temps (J), Loc)));
4343 if J < Dims then
4344 Append_To (Stats,
4345 Make_Assignment_Statement (Loc,
4346 Name => New_Occurrence_Of (Pos, Loc),
4347 Expression =>
4348 Make_Op_Add (Loc,
4349 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4350 Right_Opnd =>
4351 Make_Attribute_Reference (Loc,
4352 Attribute_Name => Name_Length,
4353 Prefix => New_Occurrence_Of (Temps (J), Loc),
4354 Expressions =>
4355 New_List (Make_Integer_Literal (Loc, 1))))));
4357 Set_Character_Literal_Name (Get_Char_Code (','));
4359 Append_To (Stats,
4360 Make_Assignment_Statement (Loc,
4361 Name => Make_Indexed_Component (Loc,
4362 Prefix => New_Occurrence_Of (Res, Loc),
4363 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4364 Expression =>
4365 Make_Character_Literal (Loc,
4366 Chars => Name_Find,
4367 Char_Literal_Value => UI_From_CC (Get_Char_Code (',')))));
4369 Append_To (Stats,
4370 Make_Assignment_Statement (Loc,
4371 Name => New_Occurrence_Of (Pos, Loc),
4372 Expression =>
4373 Make_Op_Add (Loc,
4374 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4375 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4376 end if;
4377 end loop;
4379 Set_Character_Literal_Name (Get_Char_Code (')'));
4381 Append_To (Stats,
4382 Make_Assignment_Statement (Loc,
4383 Name =>
4384 Make_Indexed_Component (Loc,
4385 Prefix => New_Occurrence_Of (Res, Loc),
4386 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
4387 Expression =>
4388 Make_Character_Literal (Loc,
4389 Chars => Name_Find,
4390 Char_Literal_Value => UI_From_CC (Get_Char_Code (')')))));
4391 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4392 end Build_Task_Array_Image;
4394 ----------------------------
4395 -- Build_Task_Image_Decls --
4396 ----------------------------
4398 function Build_Task_Image_Decls
4399 (Loc : Source_Ptr;
4400 Id_Ref : Node_Id;
4401 A_Type : Entity_Id;
4402 In_Init_Proc : Boolean := False) return List_Id
4404 Decls : constant List_Id := New_List;
4405 T_Id : Entity_Id := Empty;
4406 Decl : Node_Id;
4407 Expr : Node_Id := Empty;
4408 Fun : Node_Id := Empty;
4409 Is_Dyn : constant Boolean :=
4410 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
4411 and then
4412 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
4414 Component_Suffix_Index : constant Int :=
4415 (if In_Init_Proc then -1 else 0);
4416 -- If an init proc calls Build_Task_Image_Decls twice for its
4417 -- _Parent component (to split early/late initialization), we don't
4418 -- want two decls with the same name. Hence, the -1 suffix.
4420 begin
4421 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
4422 -- generate a dummy declaration only.
4424 if Restriction_Active (No_Implicit_Heap_Allocations)
4425 or else Global_Discard_Names
4426 then
4427 T_Id := Make_Temporary (Loc, 'J');
4428 Name_Len := 0;
4430 return
4431 New_List (
4432 Make_Object_Declaration (Loc,
4433 Defining_Identifier => T_Id,
4434 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4435 Expression =>
4436 Make_String_Literal (Loc,
4437 Strval => String_From_Name_Buffer)));
4439 else
4440 if Nkind (Id_Ref) = N_Identifier
4441 or else Nkind (Id_Ref) = N_Defining_Identifier
4442 then
4443 -- For a simple variable, the image of the task is built from
4444 -- the name of the variable. To avoid possible conflict with the
4445 -- anonymous type created for a single protected object, add a
4446 -- numeric suffix.
4448 T_Id :=
4449 Make_Defining_Identifier (Loc,
4450 New_External_Name (Chars (Id_Ref), 'T', 1));
4452 Get_Name_String (Chars (Id_Ref));
4454 Expr :=
4455 Make_String_Literal (Loc,
4456 Strval => String_From_Name_Buffer);
4458 elsif Nkind (Id_Ref) = N_Selected_Component then
4459 T_Id :=
4460 Make_Defining_Identifier (Loc,
4461 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T',
4462 Suffix_Index => Component_Suffix_Index));
4463 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
4465 elsif Nkind (Id_Ref) = N_Indexed_Component then
4466 T_Id :=
4467 Make_Defining_Identifier (Loc,
4468 New_External_Name (Chars (A_Type), 'N'));
4470 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
4471 end if;
4472 end if;
4474 if Present (Fun) then
4475 Append (Fun, Decls);
4476 Expr := Make_Function_Call (Loc,
4477 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
4479 if not In_Init_Proc then
4480 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4481 end if;
4482 end if;
4484 Decl := Make_Object_Declaration (Loc,
4485 Defining_Identifier => T_Id,
4486 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4487 Constant_Present => True,
4488 Expression => Expr);
4490 Append (Decl, Decls);
4491 return Decls;
4492 end Build_Task_Image_Decls;
4494 -------------------------------
4495 -- Build_Task_Image_Function --
4496 -------------------------------
4498 function Build_Task_Image_Function
4499 (Loc : Source_Ptr;
4500 Decls : List_Id;
4501 Stats : List_Id;
4502 Res : Entity_Id) return Node_Id
4504 Spec : Node_Id;
4506 begin
4507 Append_To (Stats,
4508 Make_Simple_Return_Statement (Loc,
4509 Expression => New_Occurrence_Of (Res, Loc)));
4511 Spec := Make_Function_Specification (Loc,
4512 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4513 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4515 -- Calls to 'Image use the secondary stack, which must be cleaned up
4516 -- after the task name is built.
4518 return Make_Subprogram_Body (Loc,
4519 Specification => Spec,
4520 Declarations => Decls,
4521 Handled_Statement_Sequence =>
4522 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4523 end Build_Task_Image_Function;
4525 -----------------------------
4526 -- Build_Task_Image_Prefix --
4527 -----------------------------
4529 procedure Build_Task_Image_Prefix
4530 (Loc : Source_Ptr;
4531 Len : out Entity_Id;
4532 Res : out Entity_Id;
4533 Pos : out Entity_Id;
4534 Prefix : Entity_Id;
4535 Sum : Node_Id;
4536 Decls : List_Id;
4537 Stats : List_Id)
4539 begin
4540 Len := Make_Temporary (Loc, 'L', Sum);
4542 Append_To (Decls,
4543 Make_Object_Declaration (Loc,
4544 Defining_Identifier => Len,
4545 Constant_Present => True,
4546 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4547 Expression => Sum));
4549 Res := Make_Temporary (Loc, 'R');
4551 Append_To (Decls,
4552 Make_Object_Declaration (Loc,
4553 Defining_Identifier => Res,
4554 Object_Definition =>
4555 Make_Subtype_Indication (Loc,
4556 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4557 Constraint =>
4558 Make_Index_Or_Discriminant_Constraint (Loc,
4559 Constraints =>
4560 New_List (
4561 Make_Range (Loc,
4562 Low_Bound => Make_Integer_Literal (Loc, 1),
4563 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4565 -- Indicate that the result is an internal temporary, so it does not
4566 -- receive a bogus initialization when declaration is expanded. This
4567 -- is both efficient, and prevents anomalies in the handling of
4568 -- dynamic objects on the secondary stack.
4570 Set_Is_Internal (Res);
4571 Pos := Make_Temporary (Loc, 'P');
4573 Append_To (Decls,
4574 Make_Object_Declaration (Loc,
4575 Defining_Identifier => Pos,
4576 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4578 -- Pos := Prefix'Length;
4580 Append_To (Stats,
4581 Make_Assignment_Statement (Loc,
4582 Name => New_Occurrence_Of (Pos, Loc),
4583 Expression =>
4584 Make_Attribute_Reference (Loc,
4585 Attribute_Name => Name_Length,
4586 Prefix => New_Occurrence_Of (Prefix, Loc),
4587 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4589 -- Res (1 .. Pos) := Prefix;
4591 Append_To (Stats,
4592 Make_Assignment_Statement (Loc,
4593 Name =>
4594 Make_Slice (Loc,
4595 Prefix => New_Occurrence_Of (Res, Loc),
4596 Discrete_Range =>
4597 Make_Range (Loc,
4598 Low_Bound => Make_Integer_Literal (Loc, 1),
4599 High_Bound => New_Occurrence_Of (Pos, Loc))),
4601 Expression => New_Occurrence_Of (Prefix, Loc)));
4603 Append_To (Stats,
4604 Make_Assignment_Statement (Loc,
4605 Name => New_Occurrence_Of (Pos, Loc),
4606 Expression =>
4607 Make_Op_Add (Loc,
4608 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4609 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4610 end Build_Task_Image_Prefix;
4612 -----------------------------
4613 -- Build_Task_Record_Image --
4614 -----------------------------
4616 function Build_Task_Record_Image
4617 (Loc : Source_Ptr;
4618 Id_Ref : Node_Id;
4619 Dyn : Boolean := False) return Node_Id
4621 Len : Entity_Id;
4622 -- Total length of generated name
4624 Pos : Entity_Id;
4625 -- Index into result
4627 Res : Entity_Id;
4628 -- String to hold result
4630 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4631 -- Name of enclosing variable, prefix of resulting name
4633 Sum : Node_Id;
4634 -- Expression to compute total size of string
4636 Sel : Entity_Id;
4637 -- Entity for selector name
4639 Decls : constant List_Id := New_List;
4640 Stats : constant List_Id := New_List;
4642 begin
4643 -- For a dynamic task, the name comes from the target variable. For a
4644 -- static one it is a formal of the enclosing init proc.
4646 if Dyn then
4647 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4648 Append_To (Decls,
4649 Make_Object_Declaration (Loc,
4650 Defining_Identifier => Pref,
4651 Constant_Present => True,
4652 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4653 Expression =>
4654 Make_String_Literal (Loc,
4655 Strval => String_From_Name_Buffer)));
4657 else
4658 Append_To (Decls,
4659 Make_Object_Renaming_Declaration (Loc,
4660 Defining_Identifier => Pref,
4661 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4662 Name => Make_Identifier (Loc, Name_uTask_Name)));
4663 end if;
4665 Sel := Make_Temporary (Loc, 'S');
4667 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4669 Append_To (Decls,
4670 Make_Object_Declaration (Loc,
4671 Defining_Identifier => Sel,
4672 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4673 Expression =>
4674 Make_String_Literal (Loc,
4675 Strval => String_From_Name_Buffer)));
4677 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4679 Sum :=
4680 Make_Op_Add (Loc,
4681 Left_Opnd => Sum,
4682 Right_Opnd =>
4683 Make_Attribute_Reference (Loc,
4684 Attribute_Name => Name_Length,
4685 Prefix =>
4686 New_Occurrence_Of (Pref, Loc),
4687 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4689 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4691 Set_Character_Literal_Name (Get_Char_Code ('.'));
4693 -- Res (Pos) := '.';
4695 Append_To (Stats,
4696 Make_Assignment_Statement (Loc,
4697 Name => Make_Indexed_Component (Loc,
4698 Prefix => New_Occurrence_Of (Res, Loc),
4699 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4700 Expression =>
4701 Make_Character_Literal (Loc,
4702 Chars => Name_Find,
4703 Char_Literal_Value =>
4704 UI_From_CC (Get_Char_Code ('.')))));
4706 Append_To (Stats,
4707 Make_Assignment_Statement (Loc,
4708 Name => New_Occurrence_Of (Pos, Loc),
4709 Expression =>
4710 Make_Op_Add (Loc,
4711 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4712 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4714 -- Res (Pos .. Len) := Selector;
4716 Append_To (Stats,
4717 Make_Assignment_Statement (Loc,
4718 Name => Make_Slice (Loc,
4719 Prefix => New_Occurrence_Of (Res, Loc),
4720 Discrete_Range =>
4721 Make_Range (Loc,
4722 Low_Bound => New_Occurrence_Of (Pos, Loc),
4723 High_Bound => New_Occurrence_Of (Len, Loc))),
4724 Expression => New_Occurrence_Of (Sel, Loc)));
4726 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4727 end Build_Task_Record_Image;
4729 ----------------------------------------
4730 -- Build_Temporary_On_Secondary_Stack --
4731 ----------------------------------------
4733 function Build_Temporary_On_Secondary_Stack
4734 (Loc : Source_Ptr;
4735 Typ : Entity_Id;
4736 Code : List_Id) return Entity_Id
4738 Acc_Typ : Entity_Id;
4739 Alloc : Node_Id;
4740 Alloc_Obj : Entity_Id;
4742 begin
4743 pragma Assert (RTE_Available (RE_SS_Pool)
4744 and then not Needs_Finalization (Typ));
4746 Acc_Typ := Make_Temporary (Loc, 'A');
4747 Mutate_Ekind (Acc_Typ, E_Access_Type);
4748 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
4750 Append_To (Code,
4751 Make_Full_Type_Declaration (Loc,
4752 Defining_Identifier => Acc_Typ,
4753 Type_Definition =>
4754 Make_Access_To_Object_Definition (Loc,
4755 All_Present => True,
4756 Subtype_Indication =>
4757 New_Occurrence_Of (Typ, Loc))));
4759 Alloc :=
4760 Make_Allocator (Loc, Expression => New_Occurrence_Of (Typ, Loc));
4761 Set_No_Initialization (Alloc);
4763 Alloc_Obj := Make_Temporary (Loc, 'R');
4765 Append_To (Code,
4766 Make_Object_Declaration (Loc,
4767 Defining_Identifier => Alloc_Obj,
4768 Constant_Present => True,
4769 Object_Definition =>
4770 New_Occurrence_Of (Acc_Typ, Loc),
4771 Expression => Alloc));
4773 Set_Uses_Sec_Stack (Current_Scope);
4775 return Alloc_Obj;
4776 end Build_Temporary_On_Secondary_Stack;
4778 ---------------------------------------
4779 -- Build_Transient_Object_Statements --
4780 ---------------------------------------
4782 procedure Build_Transient_Object_Statements
4783 (Obj_Decl : Node_Id;
4784 Fin_Call : out Node_Id;
4785 Hook_Assign : out Node_Id;
4786 Hook_Clear : out Node_Id;
4787 Hook_Decl : out Node_Id;
4788 Ptr_Decl : out Node_Id;
4789 Finalize_Obj : Boolean := True)
4791 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4792 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4793 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4795 Desig_Typ : Entity_Id;
4796 Hook_Expr : Node_Id;
4797 Hook_Id : Entity_Id;
4798 Obj_Ref : Node_Id;
4799 Ptr_Typ : Entity_Id;
4801 begin
4802 -- Recover the type of the object
4804 Desig_Typ := Obj_Typ;
4806 if Is_Access_Type (Desig_Typ) then
4807 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4808 end if;
4810 -- Create an access type which provides a reference to the transient
4811 -- object. Generate:
4813 -- type Ptr_Typ is access all Desig_Typ;
4815 Ptr_Typ := Make_Temporary (Loc, 'A');
4816 Mutate_Ekind (Ptr_Typ, E_General_Access_Type);
4817 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4819 Ptr_Decl :=
4820 Make_Full_Type_Declaration (Loc,
4821 Defining_Identifier => Ptr_Typ,
4822 Type_Definition =>
4823 Make_Access_To_Object_Definition (Loc,
4824 All_Present => True,
4825 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4827 -- Create a temporary check which acts as a hook to the transient
4828 -- object. Generate:
4830 -- Hook : Ptr_Typ := null;
4832 Hook_Id := Make_Temporary (Loc, 'T');
4833 Mutate_Ekind (Hook_Id, E_Variable);
4834 Set_Etype (Hook_Id, Ptr_Typ);
4836 Hook_Decl :=
4837 Make_Object_Declaration (Loc,
4838 Defining_Identifier => Hook_Id,
4839 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4840 Expression => Make_Null (Loc));
4842 -- Mark the temporary as a hook. This signals the machinery in
4843 -- Build_Finalizer to recognize this special case.
4845 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4847 -- Hook the transient object to the temporary. Generate:
4849 -- Hook := Ptr_Typ (Obj_Id);
4850 -- <or>
4851 -- Hool := Obj_Id'Unrestricted_Access;
4853 if Is_Access_Type (Obj_Typ) then
4854 Hook_Expr :=
4855 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4856 else
4857 Hook_Expr :=
4858 Make_Attribute_Reference (Loc,
4859 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4860 Attribute_Name => Name_Unrestricted_Access);
4861 end if;
4863 Hook_Assign :=
4864 Make_Assignment_Statement (Loc,
4865 Name => New_Occurrence_Of (Hook_Id, Loc),
4866 Expression => Hook_Expr);
4868 -- Crear the hook prior to finalizing the object. Generate:
4870 -- Hook := null;
4872 Hook_Clear :=
4873 Make_Assignment_Statement (Loc,
4874 Name => New_Occurrence_Of (Hook_Id, Loc),
4875 Expression => Make_Null (Loc));
4877 -- Finalize the object. Generate:
4879 -- [Deep_]Finalize (Obj_Ref[.all]);
4881 if Finalize_Obj then
4882 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4884 if Is_Access_Type (Obj_Typ) then
4885 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4886 Set_Etype (Obj_Ref, Desig_Typ);
4887 end if;
4889 Fin_Call :=
4890 Make_Final_Call
4891 (Obj_Ref => Obj_Ref,
4892 Typ => Desig_Typ);
4894 -- Otherwise finalize the hook. Generate:
4896 -- [Deep_]Finalize (Hook.all);
4898 else
4899 Fin_Call :=
4900 Make_Final_Call (
4901 Obj_Ref =>
4902 Make_Explicit_Dereference (Loc,
4903 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4904 Typ => Desig_Typ);
4905 end if;
4906 end Build_Transient_Object_Statements;
4908 -----------------------------
4909 -- Check_Float_Op_Overflow --
4910 -----------------------------
4912 procedure Check_Float_Op_Overflow (N : Node_Id) is
4913 begin
4914 -- Return if no check needed
4916 if not Is_Floating_Point_Type (Etype (N))
4917 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4919 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4920 -- and do not expand the code for float overflow checking.
4922 or else CodePeer_Mode
4923 then
4924 return;
4925 end if;
4927 -- Otherwise we replace the expression by
4929 -- do Tnn : constant ftype := expression;
4930 -- constraint_error when not Tnn'Valid;
4931 -- in Tnn;
4933 declare
4934 Loc : constant Source_Ptr := Sloc (N);
4935 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4936 Typ : constant Entity_Id := Etype (N);
4938 begin
4939 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4940 -- right here. We also set the node as analyzed to prevent infinite
4941 -- recursion from repeating the operation in the expansion.
4943 Set_Do_Overflow_Check (N, False);
4944 Set_Analyzed (N, True);
4946 -- Do the rewrite to include the check
4948 Rewrite (N,
4949 Make_Expression_With_Actions (Loc,
4950 Actions => New_List (
4951 Make_Object_Declaration (Loc,
4952 Defining_Identifier => Tnn,
4953 Object_Definition => New_Occurrence_Of (Typ, Loc),
4954 Constant_Present => True,
4955 Expression => Relocate_Node (N)),
4956 Make_Raise_Constraint_Error (Loc,
4957 Condition =>
4958 Make_Op_Not (Loc,
4959 Right_Opnd =>
4960 Make_Attribute_Reference (Loc,
4961 Prefix => New_Occurrence_Of (Tnn, Loc),
4962 Attribute_Name => Name_Valid)),
4963 Reason => CE_Overflow_Check_Failed)),
4964 Expression => New_Occurrence_Of (Tnn, Loc)));
4966 Analyze_And_Resolve (N, Typ);
4967 end;
4968 end Check_Float_Op_Overflow;
4970 ----------------------------------
4971 -- Component_May_Be_Bit_Aligned --
4972 ----------------------------------
4974 function Component_May_Be_Bit_Aligned
4975 (Comp : Entity_Id;
4976 For_Slice : Boolean := False) return Boolean
4978 UT : Entity_Id;
4980 begin
4981 -- If no component clause, then everything is fine, since the back end
4982 -- never misaligns from byte boundaries by default, even if there is a
4983 -- pragma Pack for the record.
4985 if No (Comp) or else No (Component_Clause (Comp)) then
4986 return False;
4987 end if;
4989 UT := Underlying_Type (Etype (Comp));
4991 -- It is only array and record types that cause trouble
4993 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4994 return False;
4996 -- If we know that we have a small (at most the maximum integer size)
4997 -- bit-packed array or record without variant part, then everything is
4998 -- fine, since the back end can handle these cases correctly, except if
4999 -- a slice is involved.
5001 elsif Known_Esize (Comp)
5002 and then Esize (Comp) <= System_Max_Integer_Size
5003 and then (Is_Bit_Packed_Array (UT)
5004 or else (Is_Record_Type (UT)
5005 and then not Has_Variant_Part (UT)))
5006 and then not For_Slice
5007 then
5008 return False;
5010 elsif not Known_Normalized_First_Bit (Comp) then
5011 return True;
5013 -- Otherwise if the component is not byte aligned, we know we have the
5014 -- nasty unaligned case.
5016 elsif Normalized_First_Bit (Comp) /= Uint_0
5017 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
5018 then
5019 return True;
5021 -- If we are large and byte aligned, then OK at this level
5023 else
5024 return False;
5025 end if;
5026 end Component_May_Be_Bit_Aligned;
5028 -------------------------------
5029 -- Convert_To_Actual_Subtype --
5030 -------------------------------
5032 procedure Convert_To_Actual_Subtype (Exp : Node_Id) is
5033 Act_ST : Entity_Id;
5035 begin
5036 Act_ST := Get_Actual_Subtype (Exp);
5038 if Act_ST = Etype (Exp) then
5039 return;
5040 else
5041 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
5042 Analyze_And_Resolve (Exp, Act_ST);
5043 end if;
5044 end Convert_To_Actual_Subtype;
5046 -----------------------------------
5047 -- Corresponding_Runtime_Package --
5048 -----------------------------------
5050 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
5051 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
5052 -- Return True if protected type T has one entry and the maximum queue
5053 -- length is one.
5055 --------------------------------
5056 -- Has_One_Entry_And_No_Queue --
5057 --------------------------------
5059 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
5060 Item : Entity_Id;
5061 Is_First : Boolean := True;
5063 begin
5064 Item := First_Entity (T);
5065 while Present (Item) loop
5066 if Is_Entry (Item) then
5068 -- The protected type has more than one entry
5070 if not Is_First then
5071 return False;
5072 end if;
5074 -- The queue length is not one
5076 if not Restriction_Active (No_Entry_Queue)
5077 and then Get_Max_Queue_Length (Item) /= Uint_1
5078 then
5079 return False;
5080 end if;
5082 Is_First := False;
5083 end if;
5085 Next_Entity (Item);
5086 end loop;
5088 return True;
5089 end Has_One_Entry_And_No_Queue;
5091 -- Local variables
5093 Pkg_Id : RTU_Id := RTU_Null;
5095 -- Start of processing for Corresponding_Runtime_Package
5097 begin
5098 pragma Assert (Is_Concurrent_Type (Typ));
5100 if Is_Protected_Type (Typ) then
5101 if Has_Entries (Typ)
5103 -- A protected type without entries that covers an interface and
5104 -- overrides the abstract routines with protected procedures is
5105 -- considered equivalent to a protected type with entries in the
5106 -- context of dispatching select statements. It is sufficient to
5107 -- check for the presence of an interface list in the declaration
5108 -- node to recognize this case.
5110 or else Present (Interface_List (Parent (Typ)))
5112 -- Protected types with interrupt handlers (when not using a
5113 -- restricted profile) are also considered equivalent to
5114 -- protected types with entries. The types which are used
5115 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
5116 -- are derived from Protection_Entries.
5118 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
5119 or else Has_Interrupt_Handler (Typ)
5120 then
5121 if Abort_Allowed
5122 or else Restriction_Active (No_Select_Statements) = False
5123 or else not Has_One_Entry_And_No_Queue (Typ)
5124 or else (Has_Attach_Handler (Typ)
5125 and then not Restricted_Profile)
5126 then
5127 Pkg_Id := System_Tasking_Protected_Objects_Entries;
5128 else
5129 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
5130 end if;
5132 else
5133 Pkg_Id := System_Tasking_Protected_Objects;
5134 end if;
5135 end if;
5137 return Pkg_Id;
5138 end Corresponding_Runtime_Package;
5140 -----------------------------------
5141 -- Current_Sem_Unit_Declarations --
5142 -----------------------------------
5144 function Current_Sem_Unit_Declarations return List_Id is
5145 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
5146 Decls : List_Id;
5148 begin
5149 -- If the current unit is a package body, locate the visible
5150 -- declarations of the package spec.
5152 if Nkind (U) = N_Package_Body then
5153 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
5154 end if;
5156 if Nkind (U) = N_Package_Declaration then
5157 U := Specification (U);
5158 Decls := Visible_Declarations (U);
5160 if No (Decls) then
5161 Decls := New_List;
5162 Set_Visible_Declarations (U, Decls);
5163 end if;
5165 else
5166 Decls := Declarations (U);
5168 if No (Decls) then
5169 Decls := New_List;
5170 Set_Declarations (U, Decls);
5171 end if;
5172 end if;
5174 return Decls;
5175 end Current_Sem_Unit_Declarations;
5177 -----------------------
5178 -- Duplicate_Subexpr --
5179 -----------------------
5181 function Duplicate_Subexpr
5182 (Exp : Node_Id;
5183 Name_Req : Boolean := False;
5184 Renaming_Req : Boolean := False) return Node_Id
5186 begin
5187 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5188 return New_Copy_Tree (Exp);
5189 end Duplicate_Subexpr;
5191 ---------------------------------
5192 -- Duplicate_Subexpr_No_Checks --
5193 ---------------------------------
5195 function Duplicate_Subexpr_No_Checks
5196 (Exp : Node_Id;
5197 Name_Req : Boolean := False;
5198 Renaming_Req : Boolean := False;
5199 Related_Id : Entity_Id := Empty;
5200 Is_Low_Bound : Boolean := False;
5201 Is_High_Bound : Boolean := False) return Node_Id
5203 New_Exp : Node_Id;
5205 begin
5206 Remove_Side_Effects
5207 (Exp => Exp,
5208 Name_Req => Name_Req,
5209 Renaming_Req => Renaming_Req,
5210 Related_Id => Related_Id,
5211 Is_Low_Bound => Is_Low_Bound,
5212 Is_High_Bound => Is_High_Bound);
5214 New_Exp := New_Copy_Tree (Exp);
5215 Remove_Checks (New_Exp);
5216 return New_Exp;
5217 end Duplicate_Subexpr_No_Checks;
5219 -----------------------------------
5220 -- Duplicate_Subexpr_Move_Checks --
5221 -----------------------------------
5223 function Duplicate_Subexpr_Move_Checks
5224 (Exp : Node_Id;
5225 Name_Req : Boolean := False;
5226 Renaming_Req : Boolean := False) return Node_Id
5228 New_Exp : Node_Id;
5230 begin
5231 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5232 New_Exp := New_Copy_Tree (Exp);
5233 Remove_Checks (Exp);
5234 return New_Exp;
5235 end Duplicate_Subexpr_Move_Checks;
5237 -------------------------
5238 -- Enclosing_Init_Proc --
5239 -------------------------
5241 function Enclosing_Init_Proc return Entity_Id is
5242 S : Entity_Id;
5244 begin
5245 S := Current_Scope;
5246 while Present (S) and then S /= Standard_Standard loop
5247 if Is_Init_Proc (S) then
5248 return S;
5249 else
5250 S := Scope (S);
5251 end if;
5252 end loop;
5254 return Empty;
5255 end Enclosing_Init_Proc;
5257 --------------------
5258 -- Ensure_Defined --
5259 --------------------
5261 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
5262 IR : Node_Id;
5264 begin
5265 -- An itype reference must only be created if this is a local itype, so
5266 -- that gigi can elaborate it on the proper objstack.
5268 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
5269 IR := Make_Itype_Reference (Sloc (N));
5270 Set_Itype (IR, Typ);
5271 Insert_Action (N, IR);
5272 end if;
5273 end Ensure_Defined;
5275 -------------------
5276 -- Evaluate_Name --
5277 -------------------
5279 procedure Evaluate_Name (Nam : Node_Id) is
5280 begin
5281 case Nkind (Nam) is
5282 -- For an aggregate, force its evaluation
5284 when N_Aggregate =>
5285 Force_Evaluation (Nam);
5287 -- For an attribute reference or an indexed component, evaluate the
5288 -- prefix, which is itself a name, recursively, and then force the
5289 -- evaluation of all the subscripts (or attribute expressions).
5291 when N_Attribute_Reference
5292 | N_Indexed_Component
5294 Evaluate_Name (Prefix (Nam));
5296 declare
5297 E : Node_Id;
5299 begin
5300 E := First (Expressions (Nam));
5301 while Present (E) loop
5302 Force_Evaluation (E);
5304 if Is_Rewrite_Substitution (E) then
5305 Set_Do_Range_Check
5306 (E, Do_Range_Check (Original_Node (E)));
5307 end if;
5309 Next (E);
5310 end loop;
5311 end;
5313 -- For an explicit dereference, we simply force the evaluation of
5314 -- the name expression. The dereference provides a value that is the
5315 -- address for the renamed object, and it is precisely this value
5316 -- that we want to preserve.
5318 when N_Explicit_Dereference =>
5319 Force_Evaluation (Prefix (Nam));
5321 -- For a function call, we evaluate the call; same for an operator
5323 when N_Function_Call
5324 | N_Op
5326 Force_Evaluation (Nam);
5328 -- For a qualified expression, we evaluate the expression
5330 when N_Qualified_Expression =>
5331 Evaluate_Name (Expression (Nam));
5333 -- For a selected component, we simply evaluate the prefix
5335 when N_Selected_Component =>
5336 Evaluate_Name (Prefix (Nam));
5338 -- For a slice, we evaluate the prefix, as for the indexed component
5339 -- case and then, if there is a range present, either directly or as
5340 -- the constraint of a discrete subtype indication, we evaluate the
5341 -- two bounds of this range.
5343 when N_Slice =>
5344 Evaluate_Name (Prefix (Nam));
5345 Evaluate_Slice_Bounds (Nam);
5347 -- For a type conversion, the expression of the conversion must be
5348 -- the name of an object, and we simply need to evaluate this name.
5350 when N_Type_Conversion =>
5351 Evaluate_Name (Expression (Nam));
5353 -- The remaining cases are direct name and character literal. In all
5354 -- these cases, we do nothing, since we want to reevaluate each time
5355 -- the renamed object is used. ??? There are more remaining cases, at
5356 -- least in the GNATprove_Mode, where this routine is called in more
5357 -- contexts than in GNAT.
5359 when others =>
5360 null;
5361 end case;
5362 end Evaluate_Name;
5364 ---------------------------
5365 -- Evaluate_Slice_Bounds --
5366 ---------------------------
5368 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
5369 DR : constant Node_Id := Discrete_Range (Slice);
5370 Constr : Node_Id;
5371 Rexpr : Node_Id;
5373 begin
5374 if Nkind (DR) = N_Range then
5375 Force_Evaluation (Low_Bound (DR));
5376 Force_Evaluation (High_Bound (DR));
5378 elsif Nkind (DR) = N_Subtype_Indication then
5379 Constr := Constraint (DR);
5381 if Nkind (Constr) = N_Range_Constraint then
5382 Rexpr := Range_Expression (Constr);
5384 Force_Evaluation (Low_Bound (Rexpr));
5385 Force_Evaluation (High_Bound (Rexpr));
5386 end if;
5387 end if;
5388 end Evaluate_Slice_Bounds;
5390 ---------------------
5391 -- Evolve_And_Then --
5392 ---------------------
5394 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
5395 begin
5396 if No (Cond) then
5397 Cond := Cond1;
5398 else
5399 Cond :=
5400 Make_And_Then (Sloc (Cond1),
5401 Left_Opnd => Cond,
5402 Right_Opnd => Cond1);
5403 end if;
5404 end Evolve_And_Then;
5406 --------------------
5407 -- Evolve_Or_Else --
5408 --------------------
5410 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
5411 begin
5412 if No (Cond) then
5413 Cond := Cond1;
5414 else
5415 Cond :=
5416 Make_Or_Else (Sloc (Cond1),
5417 Left_Opnd => Cond,
5418 Right_Opnd => Cond1);
5419 end if;
5420 end Evolve_Or_Else;
5422 -------------------------------
5423 -- Expand_Sliding_Conversion --
5424 -------------------------------
5426 procedure Expand_Sliding_Conversion (N : Node_Id; Arr_Typ : Entity_Id) is
5428 pragma Assert (Is_Array_Type (Arr_Typ)
5429 and then not Is_Constrained (Arr_Typ)
5430 and then Is_Fixed_Lower_Bound_Array_Subtype (Arr_Typ));
5432 Constraints : List_Id;
5433 Index : Node_Id := First_Index (Arr_Typ);
5434 Loc : constant Source_Ptr := Sloc (N);
5435 Subt_Decl : Node_Id;
5436 Subt : Entity_Id;
5437 Subt_Low : Node_Id;
5438 Subt_High : Node_Id;
5440 Act_Subt : Entity_Id;
5441 Act_Index : Node_Id;
5442 Act_Low : Node_Id;
5443 Act_High : Node_Id;
5444 Adjust_Incr : Node_Id;
5445 Dimension : Int := 0;
5446 All_FLBs_Match : Boolean := True;
5448 begin
5449 -- This procedure is called during semantic analysis, and we only expand
5450 -- a sliding conversion when Expander_Active, to avoid doing it during
5451 -- preanalysis (which can lead to problems with the target subtype not
5452 -- getting properly expanded during later full analysis). Also, sliding
5453 -- should never be needed for string literals, because their bounds are
5454 -- determined directly based on the fixed lower bound of Arr_Typ and
5455 -- their length.
5457 if Expander_Active and then Nkind (N) /= N_String_Literal then
5458 Constraints := New_List;
5460 Act_Subt := Get_Actual_Subtype (N);
5461 Act_Index := First_Index (Act_Subt);
5463 -- Loop over the indexes of the fixed-lower-bound array type or
5464 -- subtype to build up an index constraint for constructing the
5465 -- subtype that will be the target of a conversion of the array
5466 -- object that may need a sliding conversion.
5468 while Present (Index) loop
5469 pragma Assert (Present (Act_Index));
5471 Dimension := Dimension + 1;
5473 Get_Index_Bounds (Act_Index, Act_Low, Act_High);
5475 -- If Index defines a normal unconstrained range (range <>),
5476 -- then we will simply use the bounds of the actual subtype's
5477 -- corresponding index range.
5479 if not Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index)) then
5480 Subt_Low := Act_Low;
5481 Subt_High := Act_High;
5483 -- Otherwise, a range will be created with a low bound given by
5484 -- the fixed lower bound of the array subtype's index, and with
5485 -- high bound given by (Actual'Length + fixed lower bound - 1).
5487 else
5488 if Nkind (Index) = N_Subtype_Indication then
5489 Subt_Low :=
5490 New_Copy_Tree
5491 (Low_Bound (Range_Expression (Constraint (Index))));
5492 else
5493 pragma Assert (Nkind (Index) = N_Range);
5495 Subt_Low := New_Copy_Tree (Low_Bound (Index));
5496 end if;
5498 -- If either we have a nonstatic lower bound, or the target and
5499 -- source subtypes are statically known to have unequal lower
5500 -- bounds, then we will need to make a subtype conversion to
5501 -- slide the bounds. However, if all of the indexes' lower
5502 -- bounds are static and known to be equal (the common case),
5503 -- then no conversion will be needed, and we'll end up not
5504 -- creating the subtype or the conversion (though we still
5505 -- build up the index constraint, which will simply be unused).
5507 if not (Compile_Time_Known_Value (Subt_Low)
5508 and then Compile_Time_Known_Value (Act_Low))
5509 or else Expr_Value (Subt_Low) /= Expr_Value (Act_Low)
5510 then
5511 All_FLBs_Match := False;
5512 end if;
5514 -- Apply 'Pos to lower bound, which may be of an enumeration
5515 -- type, before subtracting.
5517 Adjust_Incr :=
5518 Make_Op_Subtract (Loc,
5519 Make_Attribute_Reference (Loc,
5520 Prefix =>
5521 New_Occurrence_Of (Etype (Act_Index), Loc),
5522 Attribute_Name =>
5523 Name_Pos,
5524 Expressions =>
5525 New_List (New_Copy_Tree (Subt_Low))),
5526 Make_Integer_Literal (Loc, 1));
5528 -- Apply 'Val to the result of adding the increment to the
5529 -- length, to handle indexes of enumeration types.
5531 Subt_High :=
5532 Make_Attribute_Reference (Loc,
5533 Prefix =>
5534 New_Occurrence_Of (Etype (Act_Index), Loc),
5535 Attribute_Name =>
5536 Name_Val,
5537 Expressions =>
5538 New_List (Make_Op_Add (Loc,
5539 Make_Attribute_Reference (Loc,
5540 Prefix =>
5541 New_Occurrence_Of (Act_Subt, Loc),
5542 Attribute_Name =>
5543 Name_Length,
5544 Expressions =>
5545 New_List
5546 (Make_Integer_Literal
5547 (Loc, Dimension))),
5548 Adjust_Incr)));
5549 end if;
5551 Append (Make_Range (Loc, Subt_Low, Subt_High), Constraints);
5553 Next (Index);
5554 Next (Act_Index);
5555 end loop;
5557 -- If for each index with a fixed lower bound (FLB), the lower bound
5558 -- of the corresponding index of the actual subtype is statically
5559 -- known be equal to the FLB, then a sliding conversion isn't needed
5560 -- at all, so just return without building a subtype or conversion.
5562 if All_FLBs_Match then
5563 return;
5564 end if;
5566 -- A sliding conversion is needed, so create the target subtype using
5567 -- the index constraint created above, and rewrite the expression
5568 -- as a conversion to that subtype.
5570 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
5571 Set_Is_Internal (Subt);
5573 Subt_Decl :=
5574 Make_Subtype_Declaration (Loc,
5575 Defining_Identifier => Subt,
5576 Subtype_Indication =>
5577 Make_Subtype_Indication (Loc,
5578 Subtype_Mark =>
5579 New_Occurrence_Of (Arr_Typ, Loc),
5580 Constraint =>
5581 Make_Index_Or_Discriminant_Constraint (Loc,
5582 Constraints => Constraints)));
5584 Mark_Rewrite_Insertion (Subt_Decl);
5586 -- The actual subtype is an Itype, so we analyze the declaration,
5587 -- but do not attach it to the tree.
5589 Set_Parent (Subt_Decl, N);
5590 Set_Is_Itype (Subt);
5591 Analyze (Subt_Decl, Suppress => All_Checks);
5592 Set_Associated_Node_For_Itype (Subt, N);
5593 Set_Has_Delayed_Freeze (Subt, False);
5595 -- We need to freeze the actual subtype immediately. This is needed
5596 -- because otherwise this Itype will not get frozen at all, and it is
5597 -- always safe to freeze on creation because any associated types
5598 -- must be frozen at this point.
5600 Freeze_Itype (Subt, N);
5602 Rewrite (N,
5603 Make_Type_Conversion (Loc,
5604 Subtype_Mark =>
5605 New_Occurrence_Of (Subt, Loc),
5606 Expression => Relocate_Node (N)));
5607 Analyze (N);
5608 end if;
5609 end Expand_Sliding_Conversion;
5611 -----------------------------------------
5612 -- Expand_Static_Predicates_In_Choices --
5613 -----------------------------------------
5615 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
5616 pragma Assert (Nkind (N) in N_Case_Statement_Alternative | N_Variant);
5618 Choices : List_Id := Discrete_Choices (N);
5620 Choice : Node_Id;
5621 Next_C : Node_Id;
5622 P : Node_Id;
5623 C : Node_Id;
5625 begin
5626 -- If this is an "others" alternative, we need to process any static
5627 -- predicates in its Others_Discrete_Choices.
5629 if Nkind (First (Choices)) = N_Others_Choice then
5630 Choices := Others_Discrete_Choices (First (Choices));
5631 end if;
5633 Choice := First (Choices);
5634 while Present (Choice) loop
5635 Next_C := Next (Choice);
5637 -- Check for name of subtype with static predicate
5639 if Is_Entity_Name (Choice)
5640 and then Is_Type (Entity (Choice))
5641 and then Has_Predicates (Entity (Choice))
5642 then
5643 -- Loop through entries in predicate list, converting to choices
5644 -- and inserting in the list before the current choice. Note that
5645 -- if the list is empty, corresponding to a False predicate, then
5646 -- no choices are inserted.
5648 P := First (Static_Discrete_Predicate (Entity (Choice)));
5649 while Present (P) loop
5651 -- If low bound and high bounds are equal, copy simple choice
5653 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
5654 C := New_Copy (Low_Bound (P));
5656 -- Otherwise copy a range
5658 else
5659 C := New_Copy (P);
5660 end if;
5662 -- Change Sloc to referencing choice (rather than the Sloc of
5663 -- the predicate declaration element itself).
5665 Set_Sloc (C, Sloc (Choice));
5666 Insert_Before (Choice, C);
5667 Next (P);
5668 end loop;
5670 -- Delete the predicated entry
5672 Remove (Choice);
5673 end if;
5675 -- Move to next choice to check
5677 Choice := Next_C;
5678 end loop;
5680 Set_Has_SP_Choice (N, False);
5681 end Expand_Static_Predicates_In_Choices;
5683 ------------------------------
5684 -- Expand_Subtype_From_Expr --
5685 ------------------------------
5687 -- This function is applicable for both static and dynamic allocation of
5688 -- objects which are constrained by an initial expression. Basically it
5689 -- transforms an unconstrained subtype indication into a constrained one.
5691 -- The expression may also be transformed in certain cases in order to
5692 -- avoid multiple evaluation. In the static allocation case, the general
5693 -- scheme is:
5695 -- Val : T := Expr;
5697 -- is transformed into
5699 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5701 -- Here are the main cases :
5703 -- <if Expr is a Slice>
5704 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5706 -- <elsif Expr is a String Literal>
5707 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5709 -- <elsif Expr is Constrained>
5710 -- subtype T is Type_Of_Expr
5711 -- Val : T := Expr;
5713 -- <elsif Expr is an entity_name>
5714 -- Val : T (constraints taken from Expr) := Expr;
5716 -- <else>
5717 -- type Axxx is access all T;
5718 -- Rval : Axxx := Expr'ref;
5719 -- Val : T (constraints taken from Rval) := Rval.all;
5721 -- ??? note: when the Expression is allocated in the secondary stack
5722 -- we could use it directly instead of copying it by declaring
5723 -- Val : T (...) renames Rval.all
5725 procedure Expand_Subtype_From_Expr
5726 (N : Node_Id;
5727 Unc_Type : Entity_Id;
5728 Subtype_Indic : Node_Id;
5729 Exp : Node_Id;
5730 Related_Id : Entity_Id := Empty)
5732 Loc : constant Source_Ptr := Sloc (N);
5733 Exp_Typ : constant Entity_Id := Etype (Exp);
5734 T : Entity_Id;
5736 begin
5737 -- In general we cannot build the subtype if expansion is disabled,
5738 -- because internal entities may not have been defined. However, to
5739 -- avoid some cascaded errors, we try to continue when the expression is
5740 -- an array (or string), because it is safe to compute the bounds. It is
5741 -- in fact required to do so even in a generic context, because there
5742 -- may be constants that depend on the bounds of a string literal, both
5743 -- standard string types and more generally arrays of characters.
5745 -- In GNATprove mode, these extra subtypes are not needed, unless Exp is
5746 -- a static expression. In that case, the subtype will be constrained
5747 -- while the original type might be unconstrained, so expanding the type
5748 -- is necessary both for passing legality checks in GNAT and for precise
5749 -- analysis in GNATprove.
5751 if GNATprove_Mode and then not Is_Static_Expression (Exp) then
5752 return;
5753 end if;
5755 if not Expander_Active
5756 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5757 then
5758 return;
5759 end if;
5761 if Nkind (Exp) = N_Slice then
5762 declare
5763 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5765 begin
5766 Rewrite (Subtype_Indic,
5767 Make_Subtype_Indication (Loc,
5768 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5769 Constraint =>
5770 Make_Index_Or_Discriminant_Constraint (Loc,
5771 Constraints => New_List
5772 (New_Occurrence_Of (Slice_Type, Loc)))));
5774 -- This subtype indication may be used later for constraint checks
5775 -- we better make sure that if a variable was used as a bound of
5776 -- the original slice, its value is frozen.
5778 Evaluate_Slice_Bounds (Exp);
5779 end;
5781 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5782 Rewrite (Subtype_Indic,
5783 Make_Subtype_Indication (Loc,
5784 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5785 Constraint =>
5786 Make_Index_Or_Discriminant_Constraint (Loc,
5787 Constraints => New_List (
5788 Make_Literal_Range (Loc,
5789 Literal_Typ => Exp_Typ)))));
5791 -- If the type of the expression is an internally generated type it
5792 -- may not be necessary to create a new subtype. However there are two
5793 -- exceptions: references to the current instances, and aliased array
5794 -- object declarations for which the back end has to create a template.
5796 elsif Is_Constrained (Exp_Typ)
5797 and then not Is_Class_Wide_Type (Unc_Type)
5798 and then
5799 (Nkind (N) /= N_Object_Declaration
5800 or else not Is_Entity_Name (Expression (N))
5801 or else not Comes_From_Source (Entity (Expression (N)))
5802 or else not Is_Array_Type (Exp_Typ)
5803 or else not Aliased_Present (N))
5804 then
5805 if Is_Itype (Exp_Typ)
5807 -- When this is for an object declaration, the caller may want to
5808 -- set Is_Constr_Subt_For_U_Nominal on the subtype, so we must make
5809 -- sure that either the subtype has been built for the expression,
5810 -- typically for an aggregate, or the flag is already set on it;
5811 -- otherwise it could end up being set on the nominal constrained
5812 -- subtype of an object and thus later cause the failure to detect
5813 -- non-statically-matching subtypes on 'Access of this object.
5815 and then (Nkind (N) /= N_Object_Declaration
5816 or else Nkind (Original_Node (Exp)) = N_Aggregate
5817 or else Is_Constr_Subt_For_U_Nominal (Exp_Typ))
5818 then
5819 -- Within an initialization procedure, a selected component
5820 -- denotes a component of the enclosing record, and it appears as
5821 -- an actual in a call to its own initialization procedure. If
5822 -- this component depends on the outer discriminant, we must
5823 -- generate the proper actual subtype for it.
5825 if Nkind (Exp) = N_Selected_Component
5826 and then Within_Init_Proc
5827 then
5828 declare
5829 Decl : constant Node_Id :=
5830 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5831 begin
5832 if Present (Decl) then
5833 Insert_Action (N, Decl);
5834 T := Defining_Identifier (Decl);
5835 else
5836 T := Exp_Typ;
5837 end if;
5838 end;
5840 -- No need to generate a new subtype
5842 else
5843 T := Exp_Typ;
5844 end if;
5846 else
5847 T := Make_Temporary (Loc, 'T');
5849 Insert_Action (N,
5850 Make_Subtype_Declaration (Loc,
5851 Defining_Identifier => T,
5852 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5854 -- This type is marked as an itype even though it has an explicit
5855 -- declaration since otherwise Is_Generic_Actual_Type can get
5856 -- set, resulting in the generation of spurious errors. (See
5857 -- sem_ch8.Analyze_Package_Renaming and Sem_Type.Covers.)
5859 Set_Is_Itype (T);
5860 Set_Associated_Node_For_Itype (T, Exp);
5861 end if;
5863 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5865 -- Nothing needs to be done for private types with unknown discriminants
5866 -- if the underlying type is not an unconstrained composite type or it
5867 -- is an unchecked union.
5869 elsif Is_Private_Type (Unc_Type)
5870 and then Has_Unknown_Discriminants (Unc_Type)
5871 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5872 or else Is_Constrained (Underlying_Type (Unc_Type))
5873 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5874 then
5875 null;
5877 -- Case of derived type with unknown discriminants where the parent type
5878 -- also has unknown discriminants.
5880 elsif Is_Record_Type (Unc_Type)
5881 and then not Is_Class_Wide_Type (Unc_Type)
5882 and then Has_Unknown_Discriminants (Unc_Type)
5883 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5884 then
5885 -- Nothing to be done if no underlying record view available
5887 -- If this is a limited type derived from a type with unknown
5888 -- discriminants, do not expand either, so that subsequent expansion
5889 -- of the call can add build-in-place parameters to call.
5891 if No (Underlying_Record_View (Unc_Type))
5892 or else Is_Limited_Type (Unc_Type)
5893 then
5894 null;
5896 -- Otherwise use the Underlying_Record_View to create the proper
5897 -- constrained subtype for an object of a derived type with unknown
5898 -- discriminants.
5900 else
5901 Rewrite (Subtype_Indic,
5902 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5903 end if;
5905 -- Renamings of class-wide interface types require no equivalent
5906 -- constrained type declarations because we only need to reference
5907 -- the tag component associated with the interface. The same is
5908 -- presumably true for class-wide types in general, so this test
5909 -- is broadened to include all class-wide renamings, which also
5910 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5911 -- (Is this really correct, or are there some cases of class-wide
5912 -- renamings that require action in this procedure???)
5914 elsif Present (N)
5915 and then Nkind (N) = N_Object_Renaming_Declaration
5916 and then Is_Class_Wide_Type (Unc_Type)
5917 then
5918 null;
5920 -- In Ada 95 nothing to be done if the type of the expression is limited
5921 -- because in this case the expression cannot be copied, and its use can
5922 -- only be by reference.
5924 -- In Ada 2005 the context can be an object declaration whose expression
5925 -- is a function that returns in place. If the nominal subtype has
5926 -- unknown discriminants, the call still provides constraints on the
5927 -- object, and we have to create an actual subtype from it.
5929 -- If the type is class-wide, the expression is dynamically tagged and
5930 -- we do not create an actual subtype either. Ditto for an interface.
5931 -- For now this applies only if the type is immutably limited, and the
5932 -- function being called is build-in-place. This will have to be revised
5933 -- when build-in-place functions are generalized to other types.
5935 elsif Is_Inherently_Limited_Type (Exp_Typ)
5936 and then
5937 (Is_Class_Wide_Type (Exp_Typ)
5938 or else Is_Interface (Exp_Typ)
5939 or else not Has_Unknown_Discriminants (Exp_Typ)
5940 or else not Is_Composite_Type (Unc_Type))
5941 then
5942 null;
5944 -- For limited objects initialized with build-in-place function calls,
5945 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5946 -- node in the expression initializing the object, which breaks the
5947 -- circuitry that detects and adds the additional arguments to the
5948 -- called function.
5950 elsif Is_Build_In_Place_Function_Call (Exp) then
5951 null;
5953 -- If the expression is an uninitialized aggregate, no need to build
5954 -- a subtype from the expression, because this may require the use of
5955 -- dynamic memory to create the object.
5957 elsif Is_Uninitialized_Aggregate (Exp, Exp_Typ) then
5958 Rewrite (Subtype_Indic, New_Occurrence_Of (Etype (Exp), Sloc (N)));
5959 if Nkind (N) = N_Object_Declaration then
5960 Set_Expression (N, Empty);
5961 Set_No_Initialization (N);
5962 end if;
5964 else
5965 Rewrite (Subtype_Indic,
5966 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5967 end if;
5968 end Expand_Subtype_From_Expr;
5970 ---------------------------------------------
5971 -- Expression_Contains_Primitives_Calls_Of --
5972 ---------------------------------------------
5974 function Expression_Contains_Primitives_Calls_Of
5975 (Expr : Node_Id;
5976 Typ : Entity_Id) return Boolean
5978 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5980 Calls_OK : Boolean := False;
5981 -- This flag is set to True when expression Expr contains at least one
5982 -- call to a nondispatching primitive function of Typ.
5984 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5985 -- Search for nondispatching calls to primitive functions of type Typ
5987 ----------------------------
5988 -- Search_Primitive_Calls --
5989 ----------------------------
5991 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5992 Disp_Typ : Entity_Id;
5993 Subp : Entity_Id;
5995 begin
5996 -- Detect a function call that could denote a nondispatching
5997 -- primitive of the input type.
5999 if Nkind (N) = N_Function_Call
6000 and then Is_Entity_Name (Name (N))
6001 then
6002 Subp := Entity (Name (N));
6004 -- Do not consider function calls with a controlling argument, as
6005 -- those are always dispatching calls.
6007 if Is_Dispatching_Operation (Subp)
6008 and then No (Controlling_Argument (N))
6009 then
6010 Disp_Typ := Find_Dispatching_Type (Subp);
6012 -- To qualify as a suitable primitive, the dispatching type of
6013 -- the function must be the input type.
6015 if Present (Disp_Typ)
6016 and then Unique_Entity (Disp_Typ) = U_Typ
6017 then
6018 Calls_OK := True;
6020 -- There is no need to continue the traversal, as one such
6021 -- call suffices.
6023 return Abandon;
6024 end if;
6025 end if;
6026 end if;
6028 return OK;
6029 end Search_Primitive_Calls;
6031 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
6033 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
6035 begin
6036 Search_Calls (Expr);
6037 return Calls_OK;
6038 end Expression_Contains_Primitives_Calls_Of;
6040 ----------------------
6041 -- Finalize_Address --
6042 ----------------------
6044 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
6045 Btyp : constant Entity_Id := Base_Type (Typ);
6046 Utyp : Entity_Id := Typ;
6048 begin
6049 -- Handle protected class-wide or task class-wide types
6051 if Is_Class_Wide_Type (Utyp) then
6052 if Is_Concurrent_Type (Root_Type (Utyp)) then
6053 Utyp := Root_Type (Utyp);
6055 elsif Is_Private_Type (Root_Type (Utyp))
6056 and then Present (Full_View (Root_Type (Utyp)))
6057 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
6058 then
6059 Utyp := Full_View (Root_Type (Utyp));
6060 end if;
6061 end if;
6063 -- Handle private types
6065 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
6066 Utyp := Full_View (Utyp);
6067 end if;
6069 -- Handle protected and task types
6071 if Is_Concurrent_Type (Utyp)
6072 and then Present (Corresponding_Record_Type (Utyp))
6073 then
6074 Utyp := Corresponding_Record_Type (Utyp);
6075 end if;
6077 Utyp := Underlying_Type (Base_Type (Utyp));
6079 -- Deal with untagged derivation of private views. If the parent is
6080 -- now known to be protected, the finalization routine is the one
6081 -- defined on the corresponding record of the ancestor (corresponding
6082 -- records do not automatically inherit operations, but maybe they
6083 -- should???)
6085 if Is_Untagged_Derivation (Btyp) then
6086 if Is_Protected_Type (Btyp) then
6087 Utyp := Corresponding_Record_Type (Root_Type (Btyp));
6089 else
6090 Utyp := Underlying_Type (Root_Type (Btyp));
6092 if Is_Protected_Type (Utyp) then
6093 Utyp := Corresponding_Record_Type (Utyp);
6094 end if;
6095 end if;
6096 end if;
6098 -- If the underlying_type is a subtype, we are dealing with the
6099 -- completion of a private type. We need to access the base type and
6100 -- generate a conversion to it.
6102 if Utyp /= Base_Type (Utyp) then
6103 pragma Assert (Is_Private_Type (Typ));
6105 Utyp := Base_Type (Utyp);
6106 end if;
6108 -- When dealing with an internally built full view for a type with
6109 -- unknown discriminants, use the original record type.
6111 if Is_Underlying_Record_View (Utyp) then
6112 Utyp := Etype (Utyp);
6113 end if;
6115 return TSS (Utyp, TSS_Finalize_Address);
6116 end Finalize_Address;
6118 ------------------------
6119 -- Find_Interface_ADT --
6120 ------------------------
6122 function Find_Interface_ADT
6123 (T : Entity_Id;
6124 Iface : Entity_Id) return Elmt_Id
6126 ADT : Elmt_Id;
6127 Typ : Entity_Id := T;
6129 begin
6130 pragma Assert (Is_Interface (Iface));
6132 -- Handle private types
6134 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6135 Typ := Full_View (Typ);
6136 end if;
6138 -- Handle access types
6140 if Is_Access_Type (Typ) then
6141 Typ := Designated_Type (Typ);
6142 end if;
6144 -- Handle task and protected types implementing interfaces
6146 if Is_Concurrent_Type (Typ) then
6147 Typ := Corresponding_Record_Type (Typ);
6148 end if;
6150 pragma Assert
6151 (not Is_Class_Wide_Type (Typ)
6152 and then Ekind (Typ) /= E_Incomplete_Type);
6154 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6155 return First_Elmt (Access_Disp_Table (Typ));
6157 else
6158 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
6159 while Present (ADT)
6160 and then Present (Related_Type (Node (ADT)))
6161 and then Related_Type (Node (ADT)) /= Iface
6162 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
6163 Use_Full_View => True)
6164 loop
6165 Next_Elmt (ADT);
6166 end loop;
6168 pragma Assert (Present (Related_Type (Node (ADT))));
6169 return ADT;
6170 end if;
6171 end Find_Interface_ADT;
6173 ------------------------
6174 -- Find_Interface_Tag --
6175 ------------------------
6177 function Find_Interface_Tag
6178 (T : Entity_Id;
6179 Iface : Entity_Id) return Entity_Id
6181 AI_Tag : Entity_Id := Empty;
6182 Found : Boolean := False;
6183 Typ : Entity_Id := T;
6185 procedure Find_Tag (Typ : Entity_Id);
6186 -- Internal subprogram used to recursively climb to the ancestors
6188 --------------
6189 -- Find_Tag --
6190 --------------
6192 procedure Find_Tag (Typ : Entity_Id) is
6193 AI_Elmt : Elmt_Id;
6194 AI : Node_Id;
6196 begin
6197 -- This routine does not handle the case in which the interface is an
6198 -- ancestor of Typ. That case is handled by the enclosing subprogram.
6200 pragma Assert (Typ /= Iface);
6202 -- Climb to the root type handling private types
6204 if Present (Full_View (Etype (Typ))) then
6205 if Full_View (Etype (Typ)) /= Typ then
6206 Find_Tag (Full_View (Etype (Typ)));
6207 end if;
6209 elsif Etype (Typ) /= Typ then
6210 Find_Tag (Etype (Typ));
6211 end if;
6213 -- Traverse the list of interfaces implemented by the type
6215 if not Found
6216 and then Present (Interfaces (Typ))
6217 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
6218 then
6219 -- Skip the tag associated with the primary table
6221 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
6222 pragma Assert (Present (AI_Tag));
6224 AI_Elmt := First_Elmt (Interfaces (Typ));
6225 while Present (AI_Elmt) loop
6226 AI := Node (AI_Elmt);
6228 if AI = Iface
6229 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
6230 then
6231 Found := True;
6232 return;
6233 end if;
6235 AI_Tag := Next_Tag_Component (AI_Tag);
6236 Next_Elmt (AI_Elmt);
6237 end loop;
6238 end if;
6239 end Find_Tag;
6241 -- Start of processing for Find_Interface_Tag
6243 begin
6244 pragma Assert (Is_Interface (Iface));
6246 -- Handle access types
6248 if Is_Access_Type (Typ) then
6249 Typ := Designated_Type (Typ);
6250 end if;
6252 -- Handle class-wide types
6254 if Is_Class_Wide_Type (Typ) then
6255 Typ := Root_Type (Typ);
6256 end if;
6258 -- Handle private types
6260 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6261 Typ := Full_View (Typ);
6262 end if;
6264 -- Handle entities from the limited view
6266 if Ekind (Typ) = E_Incomplete_Type then
6267 pragma Assert (Present (Non_Limited_View (Typ)));
6268 Typ := Non_Limited_View (Typ);
6269 end if;
6271 -- Handle task and protected types implementing interfaces
6273 if Is_Concurrent_Type (Typ) then
6274 Typ := Corresponding_Record_Type (Typ);
6275 end if;
6277 -- If the interface is an ancestor of the type, then it shared the
6278 -- primary dispatch table.
6280 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6281 return First_Tag_Component (Typ);
6283 -- Otherwise we need to search for its associated tag component
6285 else
6286 Find_Tag (Typ);
6287 return AI_Tag;
6288 end if;
6289 end Find_Interface_Tag;
6291 ---------------------------
6292 -- Find_Optional_Prim_Op --
6293 ---------------------------
6295 function Find_Optional_Prim_Op
6296 (T : Entity_Id; Name : Name_Id) return Entity_Id
6298 Prim : Elmt_Id;
6299 Typ : Entity_Id := T;
6300 Op : Entity_Id;
6302 begin
6303 if Is_Class_Wide_Type (Typ) then
6304 Typ := Root_Type (Typ);
6305 end if;
6307 Typ := Underlying_Type (Typ);
6309 -- We cannot find the operation if there is no full view available
6311 if No (Typ) then
6312 return Empty;
6313 end if;
6315 -- Loop through primitive operations
6317 Prim := First_Elmt (Primitive_Operations (Typ));
6318 while Present (Prim) loop
6319 Op := Node (Prim);
6321 -- We can retrieve primitive operations by name if it is an internal
6322 -- name. For equality we must check that both of its operands have
6323 -- the same type, to avoid confusion with user-defined equalities
6324 -- than may have a asymmetric signature.
6326 exit when Chars (Op) = Name
6327 and then
6328 (Name /= Name_Op_Eq
6329 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
6331 Next_Elmt (Prim);
6332 end loop;
6334 return Node (Prim); -- Empty if not found
6335 end Find_Optional_Prim_Op;
6337 ---------------------------
6338 -- Find_Optional_Prim_Op --
6339 ---------------------------
6341 function Find_Optional_Prim_Op
6342 (T : Entity_Id;
6343 Name : TSS_Name_Type) return Entity_Id
6345 Inher_Op : Entity_Id := Empty;
6346 Own_Op : Entity_Id := Empty;
6347 Prim_Elmt : Elmt_Id;
6348 Prim_Id : Entity_Id;
6349 Typ : Entity_Id := T;
6351 begin
6352 if Is_Class_Wide_Type (Typ) then
6353 Typ := Root_Type (Typ);
6354 end if;
6356 Typ := Underlying_Type (Typ);
6358 -- This search is based on the assertion that the dispatching version
6359 -- of the TSS routine always precedes the real primitive.
6361 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6362 while Present (Prim_Elmt) loop
6363 Prim_Id := Node (Prim_Elmt);
6365 if Is_TSS (Prim_Id, Name) then
6366 if Present (Alias (Prim_Id)) then
6367 Inher_Op := Prim_Id;
6368 else
6369 Own_Op := Prim_Id;
6370 end if;
6371 end if;
6373 Next_Elmt (Prim_Elmt);
6374 end loop;
6376 if Present (Own_Op) then
6377 return Own_Op;
6378 elsif Present (Inher_Op) then
6379 return Inher_Op;
6380 else
6381 return Empty;
6382 end if;
6383 end Find_Optional_Prim_Op;
6385 ------------------
6386 -- Find_Prim_Op --
6387 ------------------
6389 function Find_Prim_Op
6390 (T : Entity_Id; Name : Name_Id) return Entity_Id
6392 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6393 begin
6394 if No (Result) then
6395 raise Program_Error;
6396 end if;
6398 return Result;
6399 end Find_Prim_Op;
6401 ------------------
6402 -- Find_Prim_Op --
6403 ------------------
6405 function Find_Prim_Op
6406 (T : Entity_Id;
6407 Name : TSS_Name_Type) return Entity_Id
6409 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6410 begin
6411 if No (Result) then
6412 raise Program_Error;
6413 end if;
6415 return Result;
6416 end Find_Prim_Op;
6418 ----------------------------
6419 -- Find_Protection_Object --
6420 ----------------------------
6422 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
6423 S : Entity_Id;
6425 begin
6426 S := Scop;
6427 while Present (S) loop
6428 if Ekind (S) in E_Entry | E_Entry_Family | E_Function | E_Procedure
6429 and then Present (Protection_Object (S))
6430 then
6431 return Protection_Object (S);
6432 end if;
6434 S := Scope (S);
6435 end loop;
6437 -- If we do not find a Protection object in the scope chain, then
6438 -- something has gone wrong, most likely the object was never created.
6440 raise Program_Error;
6441 end Find_Protection_Object;
6443 --------------------------
6444 -- Find_Protection_Type --
6445 --------------------------
6447 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
6448 Comp : Entity_Id;
6449 Typ : Entity_Id := Conc_Typ;
6451 begin
6452 if Is_Concurrent_Type (Typ) then
6453 Typ := Corresponding_Record_Type (Typ);
6454 end if;
6456 -- Since restriction violations are not considered serious errors, the
6457 -- expander remains active, but may leave the corresponding record type
6458 -- malformed. In such cases, component _object is not available so do
6459 -- not look for it.
6461 if not Analyzed (Typ) then
6462 return Empty;
6463 end if;
6465 Comp := First_Component (Typ);
6466 while Present (Comp) loop
6467 if Chars (Comp) = Name_uObject then
6468 return Base_Type (Etype (Comp));
6469 end if;
6471 Next_Component (Comp);
6472 end loop;
6474 -- The corresponding record of a protected type should always have an
6475 -- _object field.
6477 raise Program_Error;
6478 end Find_Protection_Type;
6480 function Find_Storage_Op
6481 (Typ : Entity_Id;
6482 Nam : Name_Id) return Entity_Id
6484 use Sem_Util.Storage_Model_Support;
6486 begin
6487 if Has_Storage_Model_Type_Aspect (Typ) then
6488 return Get_Storage_Model_Type_Entity (Typ, Nam);
6490 -- Otherwise we assume that Typ is a descendant of Root_Storage_Pool
6492 else
6493 return Find_Prim_Op (Typ, Nam);
6494 end if;
6495 end Find_Storage_Op;
6497 -----------------------
6498 -- Find_Hook_Context --
6499 -----------------------
6501 function Find_Hook_Context (N : Node_Id) return Node_Id is
6502 Par : Node_Id;
6503 Top : Node_Id;
6505 Wrapped_Node : Node_Id;
6506 -- Note: if we are in a transient scope, we want to reuse it as
6507 -- the context for actions insertion, if possible. But if N is itself
6508 -- part of the stored actions for the current transient scope,
6509 -- then we need to insert at the appropriate (inner) location in
6510 -- the not as an action on Node_To_Be_Wrapped.
6512 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
6514 begin
6515 -- When the node is inside a case/if expression, the lifetime of any
6516 -- temporary controlled object is extended. Find a suitable insertion
6517 -- node by locating the topmost case or if expressions.
6519 if In_Cond_Expr then
6520 Par := N;
6521 Top := N;
6522 while Present (Par) loop
6523 if Nkind (Original_Node (Par)) in
6524 N_Case_Expression | N_If_Expression
6525 then
6526 Top := Par;
6528 -- Prevent the search from going too far
6530 elsif Is_Body_Or_Package_Declaration (Par) then
6531 exit;
6532 end if;
6534 Par := Parent (Par);
6535 end loop;
6537 -- The topmost case or if expression is now recovered, but it may
6538 -- still not be the correct place to add generated code. Climb to
6539 -- find a parent that is part of a declarative or statement list,
6540 -- and is not a list of actuals in a call.
6542 Par := Top;
6543 while Present (Par) loop
6544 if Is_List_Member (Par)
6545 and then Nkind (Par) not in N_Component_Association
6546 | N_Discriminant_Association
6547 | N_Parameter_Association
6548 | N_Pragma_Argument_Association
6549 | N_Aggregate
6550 | N_Delta_Aggregate
6551 | N_Extension_Aggregate
6552 and then Nkind (Parent (Par)) not in N_Function_Call
6553 | N_Procedure_Call_Statement
6554 | N_Entry_Call_Statement
6556 then
6557 return Par;
6559 -- Prevent the search from going too far
6561 elsif Is_Body_Or_Package_Declaration (Par) then
6562 exit;
6563 end if;
6565 Par := Parent (Par);
6566 end loop;
6568 return Par;
6570 else
6571 Par := N;
6572 while Present (Par) loop
6574 -- Keep climbing past various operators
6576 if Nkind (Parent (Par)) in N_Op
6577 or else Nkind (Parent (Par)) in N_And_Then | N_Or_Else
6578 then
6579 Par := Parent (Par);
6580 else
6581 exit;
6582 end if;
6583 end loop;
6585 Top := Par;
6587 -- The node may be located in a pragma in which case return the
6588 -- pragma itself:
6590 -- pragma Precondition (... and then Ctrl_Func_Call ...);
6592 -- Similar case occurs when the node is related to an object
6593 -- declaration or assignment:
6595 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
6597 -- Another case to consider is when the node is part of a return
6598 -- statement:
6600 -- return ... and then Ctrl_Func_Call ...;
6602 -- Another case is when the node acts as a formal in a procedure
6603 -- call statement:
6605 -- Proc (... and then Ctrl_Func_Call ...);
6607 if Scope_Is_Transient then
6608 Wrapped_Node := Node_To_Be_Wrapped;
6609 else
6610 Wrapped_Node := Empty;
6611 end if;
6613 while Present (Par) loop
6614 if Par = Wrapped_Node
6615 or else Nkind (Par) in N_Assignment_Statement
6616 | N_Object_Declaration
6617 | N_Pragma
6618 | N_Procedure_Call_Statement
6619 | N_Simple_Return_Statement
6620 then
6621 return Par;
6623 -- Prevent the search from going too far
6625 elsif Is_Body_Or_Package_Declaration (Par) then
6626 exit;
6627 end if;
6629 Par := Parent (Par);
6630 end loop;
6632 -- Return the topmost short circuit operator
6634 return Top;
6635 end if;
6636 end Find_Hook_Context;
6638 ------------------------------
6639 -- Following_Address_Clause --
6640 ------------------------------
6642 function Following_Address_Clause (D : Node_Id) return Node_Id is
6643 Id : constant Entity_Id := Defining_Identifier (D);
6644 Result : Node_Id;
6645 Par : Node_Id;
6647 function Check_Decls (D : Node_Id) return Node_Id;
6648 -- This internal function differs from the main function in that it
6649 -- gets called to deal with a following package private part, and
6650 -- it checks declarations starting with D (the main function checks
6651 -- declarations following D). If D is Empty, then Empty is returned.
6653 -----------------
6654 -- Check_Decls --
6655 -----------------
6657 function Check_Decls (D : Node_Id) return Node_Id is
6658 Decl : Node_Id;
6660 begin
6661 Decl := D;
6662 while Present (Decl) loop
6663 if Nkind (Decl) = N_At_Clause
6664 and then Chars (Identifier (Decl)) = Chars (Id)
6665 then
6666 return Decl;
6668 elsif Nkind (Decl) = N_Attribute_Definition_Clause
6669 and then Chars (Decl) = Name_Address
6670 and then Chars (Name (Decl)) = Chars (Id)
6671 then
6672 return Decl;
6673 end if;
6675 Next (Decl);
6676 end loop;
6678 -- Otherwise not found, return Empty
6680 return Empty;
6681 end Check_Decls;
6683 -- Start of processing for Following_Address_Clause
6685 begin
6686 -- If parser detected no address clause for the identifier in question,
6687 -- then the answer is a quick NO, without the need for a search.
6689 if not Get_Name_Table_Boolean1 (Chars (Id)) then
6690 return Empty;
6691 end if;
6693 -- Otherwise search current declarative unit
6695 Result := Check_Decls (Next (D));
6697 if Present (Result) then
6698 return Result;
6699 end if;
6701 -- Check for possible package private part following
6703 Par := Parent (D);
6705 if Nkind (Par) = N_Package_Specification
6706 and then Visible_Declarations (Par) = List_Containing (D)
6707 and then Present (Private_Declarations (Par))
6708 then
6709 -- Private part present, check declarations there
6711 return Check_Decls (First (Private_Declarations (Par)));
6713 else
6714 -- No private part, clause not found, return Empty
6716 return Empty;
6717 end if;
6718 end Following_Address_Clause;
6720 ----------------------
6721 -- Force_Evaluation --
6722 ----------------------
6724 procedure Force_Evaluation
6725 (Exp : Node_Id;
6726 Name_Req : Boolean := False;
6727 Related_Id : Entity_Id := Empty;
6728 Is_Low_Bound : Boolean := False;
6729 Is_High_Bound : Boolean := False;
6730 Discr_Number : Int := 0;
6731 Mode : Force_Evaluation_Mode := Relaxed)
6733 begin
6734 Remove_Side_Effects
6735 (Exp => Exp,
6736 Name_Req => Name_Req,
6737 Variable_Ref => True,
6738 Renaming_Req => False,
6739 Related_Id => Related_Id,
6740 Is_Low_Bound => Is_Low_Bound,
6741 Is_High_Bound => Is_High_Bound,
6742 Discr_Number => Discr_Number,
6743 Check_Side_Effects =>
6744 Is_Static_Expression (Exp)
6745 or else Mode = Relaxed);
6746 end Force_Evaluation;
6748 ---------------------------------
6749 -- Fully_Qualified_Name_String --
6750 ---------------------------------
6752 function Fully_Qualified_Name_String
6753 (E : Entity_Id;
6754 Append_NUL : Boolean := True) return String_Id
6756 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6757 -- Compute recursively the qualified name without NUL at the end, adding
6758 -- it to the currently started string being generated
6760 ----------------------------------
6761 -- Internal_Full_Qualified_Name --
6762 ----------------------------------
6764 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6765 Ent : Entity_Id;
6767 begin
6768 -- Deal properly with child units
6770 if Nkind (E) = N_Defining_Program_Unit_Name then
6771 Ent := Defining_Identifier (E);
6772 else
6773 Ent := E;
6774 end if;
6776 -- Compute qualification recursively (only "Standard" has no scope)
6778 if Present (Scope (Scope (Ent))) then
6779 Internal_Full_Qualified_Name (Scope (Ent));
6780 Store_String_Char (Get_Char_Code ('.'));
6781 end if;
6783 -- Every entity should have a name except some expanded blocks
6784 -- don't bother about those.
6786 if Chars (Ent) = No_Name then
6787 return;
6788 end if;
6790 -- Generates the entity name in upper case
6792 Get_Decoded_Name_String (Chars (Ent));
6793 Set_Casing (All_Upper_Case);
6794 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6795 return;
6796 end Internal_Full_Qualified_Name;
6798 -- Start of processing for Full_Qualified_Name
6800 begin
6801 Start_String;
6802 Internal_Full_Qualified_Name (E);
6804 if Append_NUL then
6805 Store_String_Char (Get_Char_Code (ASCII.NUL));
6806 end if;
6808 return End_String;
6809 end Fully_Qualified_Name_String;
6811 ---------------------------------
6812 -- Get_Current_Value_Condition --
6813 ---------------------------------
6815 -- Note: the implementation of this procedure is very closely tied to the
6816 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6817 -- interpret Current_Value fields set by the Set procedure, so the two
6818 -- procedures need to be closely coordinated.
6820 procedure Get_Current_Value_Condition
6821 (Var : Node_Id;
6822 Op : out Node_Kind;
6823 Val : out Node_Id)
6825 Loc : constant Source_Ptr := Sloc (Var);
6826 Ent : constant Entity_Id := Entity (Var);
6828 procedure Process_Current_Value_Condition (N : Node_Id; S : Boolean);
6829 -- N is an expression which holds either True (S = True) or False (S =
6830 -- False) in the condition. This procedure digs out the expression and
6831 -- if it refers to Ent, sets Op and Val appropriately.
6833 -------------------------------------
6834 -- Process_Current_Value_Condition --
6835 -------------------------------------
6837 procedure Process_Current_Value_Condition
6838 (N : Node_Id;
6839 S : Boolean)
6841 Cond : Node_Id;
6842 Prev_Cond : Node_Id;
6843 Sens : Boolean;
6845 begin
6846 Cond := N;
6847 Sens := S;
6849 loop
6850 Prev_Cond := Cond;
6852 -- Deal with NOT operators, inverting sense
6854 while Nkind (Cond) = N_Op_Not loop
6855 Cond := Right_Opnd (Cond);
6856 Sens := not Sens;
6857 end loop;
6859 -- Deal with conversions, qualifications, and expressions with
6860 -- actions.
6862 while Nkind (Cond) in N_Type_Conversion
6863 | N_Qualified_Expression
6864 | N_Expression_With_Actions
6865 loop
6866 Cond := Expression (Cond);
6867 end loop;
6869 exit when Cond = Prev_Cond;
6870 end loop;
6872 -- Deal with AND THEN and AND cases
6874 if Nkind (Cond) in N_And_Then | N_Op_And then
6876 -- Don't ever try to invert a condition that is of the form of an
6877 -- AND or AND THEN (since we are not doing sufficiently general
6878 -- processing to allow this).
6880 if Sens = False then
6881 Op := N_Empty;
6882 Val := Empty;
6883 return;
6884 end if;
6886 -- Recursively process AND and AND THEN branches
6888 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6889 pragma Assert (Op'Valid);
6891 if Op /= N_Empty then
6892 return;
6893 end if;
6895 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6896 return;
6898 -- Case of relational operator
6900 elsif Nkind (Cond) in N_Op_Compare then
6901 Op := Nkind (Cond);
6903 -- Invert sense of test if inverted test
6905 if Sens = False then
6906 case Op is
6907 when N_Op_Eq => Op := N_Op_Ne;
6908 when N_Op_Ne => Op := N_Op_Eq;
6909 when N_Op_Lt => Op := N_Op_Ge;
6910 when N_Op_Gt => Op := N_Op_Le;
6911 when N_Op_Le => Op := N_Op_Gt;
6912 when N_Op_Ge => Op := N_Op_Lt;
6913 when others => raise Program_Error;
6914 end case;
6915 end if;
6917 -- Case of entity op value
6919 if Is_Entity_Name (Left_Opnd (Cond))
6920 and then Ent = Entity (Left_Opnd (Cond))
6921 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6922 then
6923 Val := Right_Opnd (Cond);
6925 -- Case of value op entity
6927 elsif Is_Entity_Name (Right_Opnd (Cond))
6928 and then Ent = Entity (Right_Opnd (Cond))
6929 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6930 then
6931 Val := Left_Opnd (Cond);
6933 -- We are effectively swapping operands
6935 case Op is
6936 when N_Op_Eq => null;
6937 when N_Op_Ne => null;
6938 when N_Op_Lt => Op := N_Op_Gt;
6939 when N_Op_Gt => Op := N_Op_Lt;
6940 when N_Op_Le => Op := N_Op_Ge;
6941 when N_Op_Ge => Op := N_Op_Le;
6942 when others => raise Program_Error;
6943 end case;
6945 else
6946 Op := N_Empty;
6947 end if;
6949 return;
6951 elsif Nkind (Cond) in N_Type_Conversion
6952 | N_Qualified_Expression
6953 | N_Expression_With_Actions
6954 then
6955 Cond := Expression (Cond);
6957 -- Case of Boolean variable reference, return as though the
6958 -- reference had said var = True.
6960 else
6961 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6962 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6964 if Sens = False then
6965 Op := N_Op_Ne;
6966 else
6967 Op := N_Op_Eq;
6968 end if;
6969 end if;
6970 end if;
6971 end Process_Current_Value_Condition;
6973 -- Start of processing for Get_Current_Value_Condition
6975 begin
6976 Op := N_Empty;
6977 Val := Empty;
6979 -- Immediate return, nothing doing, if this is not an object
6981 if not Is_Object (Ent) then
6982 return;
6983 end if;
6985 -- In GNATprove mode we don't want to use current value optimizer, in
6986 -- particular for loop invariant expressions and other assertions that
6987 -- act as cut points for proof. The optimizer often folds expressions
6988 -- into True/False where they trivially follow from the previous
6989 -- assignments, but this deprives proof from the information needed to
6990 -- discharge checks that are beyond the scope of the value optimizer.
6992 if GNATprove_Mode then
6993 return;
6994 end if;
6996 -- Otherwise examine current value
6998 declare
6999 CV : constant Node_Id := Current_Value (Ent);
7000 Sens : Boolean;
7001 Stm : Node_Id;
7003 begin
7004 -- If statement. Condition is known true in THEN section, known False
7005 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
7007 if Nkind (CV) = N_If_Statement then
7009 -- Before start of IF statement
7011 if Loc < Sloc (CV) then
7012 return;
7014 -- In condition of IF statement
7016 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7017 return;
7019 -- After end of IF statement
7021 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
7022 return;
7023 end if;
7025 -- At this stage we know that we are within the IF statement, but
7026 -- unfortunately, the tree does not record the SLOC of the ELSE so
7027 -- we cannot use a simple SLOC comparison to distinguish between
7028 -- the then/else statements, so we have to climb the tree.
7030 declare
7031 N : Node_Id;
7033 begin
7034 N := Parent (Var);
7035 while Parent (N) /= CV loop
7036 N := Parent (N);
7038 -- If we fall off the top of the tree, then that's odd, but
7039 -- perhaps it could occur in some error situation, and the
7040 -- safest response is simply to assume that the outcome of
7041 -- the condition is unknown. No point in bombing during an
7042 -- attempt to optimize things.
7044 if No (N) then
7045 return;
7046 end if;
7047 end loop;
7049 -- Now we have N pointing to a node whose parent is the IF
7050 -- statement in question, so now we can tell if we are within
7051 -- the THEN statements.
7053 if Is_List_Member (N)
7054 and then List_Containing (N) = Then_Statements (CV)
7055 then
7056 Sens := True;
7058 -- If the variable reference does not come from source, we
7059 -- cannot reliably tell whether it appears in the else part.
7060 -- In particular, if it appears in generated code for a node
7061 -- that requires finalization, it may be attached to a list
7062 -- that has not been yet inserted into the code. For now,
7063 -- treat it as unknown.
7065 elsif not Comes_From_Source (N) then
7066 return;
7068 -- Otherwise we must be in ELSIF or ELSE part
7070 else
7071 Sens := False;
7072 end if;
7073 end;
7075 -- ELSIF part. Condition is known true within the referenced
7076 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
7077 -- and unknown before the ELSE part or after the IF statement.
7079 elsif Nkind (CV) = N_Elsif_Part then
7081 -- if the Elsif_Part had condition_actions, the elsif has been
7082 -- rewritten as a nested if, and the original elsif_part is
7083 -- detached from the tree, so there is no way to obtain useful
7084 -- information on the current value of the variable.
7085 -- Can this be improved ???
7087 if No (Parent (CV)) then
7088 return;
7089 end if;
7091 Stm := Parent (CV);
7093 -- If the tree has been otherwise rewritten there is nothing
7094 -- else to be done either.
7096 if Nkind (Stm) /= N_If_Statement then
7097 return;
7098 end if;
7100 -- Before start of ELSIF part
7102 if Loc < Sloc (CV) then
7103 return;
7105 -- In condition of ELSIF part
7107 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7108 return;
7110 -- After end of IF statement
7112 elsif Loc >= Sloc (Stm) +
7113 Text_Ptr (UI_To_Int (End_Span (Stm)))
7114 then
7115 return;
7116 end if;
7118 -- Again we lack the SLOC of the ELSE, so we need to climb the
7119 -- tree to see if we are within the ELSIF part in question.
7121 declare
7122 N : Node_Id;
7124 begin
7125 N := Parent (Var);
7126 while Parent (N) /= Stm loop
7127 N := Parent (N);
7129 -- If we fall off the top of the tree, then that's odd, but
7130 -- perhaps it could occur in some error situation, and the
7131 -- safest response is simply to assume that the outcome of
7132 -- the condition is unknown. No point in bombing during an
7133 -- attempt to optimize things.
7135 if No (N) then
7136 return;
7137 end if;
7138 end loop;
7140 -- Now we have N pointing to a node whose parent is the IF
7141 -- statement in question, so see if is the ELSIF part we want.
7142 -- the THEN statements.
7144 if N = CV then
7145 Sens := True;
7147 -- Otherwise we must be in subsequent ELSIF or ELSE part
7149 else
7150 Sens := False;
7151 end if;
7152 end;
7154 -- Iteration scheme of while loop. The condition is known to be
7155 -- true within the body of the loop.
7157 elsif Nkind (CV) = N_Iteration_Scheme then
7158 declare
7159 Loop_Stmt : constant Node_Id := Parent (CV);
7161 begin
7162 -- Before start of body of loop
7164 if Loc < Sloc (Loop_Stmt) then
7165 return;
7167 -- In condition of while loop
7169 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7170 return;
7172 -- After end of LOOP statement
7174 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
7175 return;
7177 -- We are within the body of the loop
7179 else
7180 Sens := True;
7181 end if;
7182 end;
7184 -- All other cases of Current_Value settings
7186 else
7187 return;
7188 end if;
7190 -- If we fall through here, then we have a reportable condition, Sens
7191 -- is True if the condition is true and False if it needs inverting.
7193 Process_Current_Value_Condition (Condition (CV), Sens);
7194 end;
7195 end Get_Current_Value_Condition;
7197 -----------------------
7198 -- Get_Index_Subtype --
7199 -----------------------
7201 function Get_Index_Subtype (N : Node_Id) return Entity_Id is
7202 P_Type : Entity_Id := Etype (Prefix (N));
7203 Indx : Node_Id;
7204 J : Int;
7206 begin
7207 if Is_Access_Type (P_Type) then
7208 P_Type := Designated_Type (P_Type);
7209 end if;
7211 if No (Expressions (N)) then
7212 J := 1;
7213 else
7214 J := UI_To_Int (Expr_Value (First (Expressions (N))));
7215 end if;
7217 Indx := First_Index (P_Type);
7218 while J > 1 loop
7219 Next_Index (Indx);
7220 J := J - 1;
7221 end loop;
7223 return Etype (Indx);
7224 end Get_Index_Subtype;
7226 -----------------------
7227 -- Get_Mapped_Entity --
7228 -----------------------
7230 function Get_Mapped_Entity (E : Entity_Id) return Entity_Id is
7231 begin
7232 return Type_Map.Get (E);
7233 end Get_Mapped_Entity;
7235 ---------------------
7236 -- Get_Stream_Size --
7237 ---------------------
7239 function Get_Stream_Size (E : Entity_Id) return Uint is
7240 begin
7241 -- If we have a Stream_Size clause for this type use it
7243 if Has_Stream_Size_Clause (E) then
7244 return Static_Integer (Expression (Stream_Size_Clause (E)));
7246 -- Otherwise the Stream_Size is the size of the type
7248 else
7249 return Esize (E);
7250 end if;
7251 end Get_Stream_Size;
7253 ---------------------------
7254 -- Has_Access_Constraint --
7255 ---------------------------
7257 function Has_Access_Constraint (E : Entity_Id) return Boolean is
7258 Disc : Entity_Id;
7259 T : constant Entity_Id := Etype (E);
7261 begin
7262 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
7263 Disc := First_Discriminant (T);
7264 while Present (Disc) loop
7265 if Is_Access_Type (Etype (Disc)) then
7266 return True;
7267 end if;
7269 Next_Discriminant (Disc);
7270 end loop;
7272 return False;
7273 else
7274 return False;
7275 end if;
7276 end Has_Access_Constraint;
7278 ---------------------
7279 -- Has_Tag_Of_Type --
7280 ---------------------
7282 function Has_Tag_Of_Type (Exp : Node_Id) return Boolean is
7283 Typ : constant Entity_Id := Etype (Exp);
7285 begin
7286 pragma Assert (Is_Tagged_Type (Typ));
7288 -- The tag of an object of a class-wide type is that of its
7289 -- initialization expression.
7291 if Is_Class_Wide_Type (Typ) then
7292 return False;
7293 end if;
7295 -- The tag of a stand-alone object of a specific tagged type T
7296 -- identifies T.
7298 if Is_Entity_Name (Exp)
7299 and then Ekind (Entity (Exp)) in E_Constant | E_Variable
7300 then
7301 return True;
7303 else
7304 case Nkind (Exp) is
7305 -- The tag of a component or an aggregate of a specific tagged
7306 -- type T identifies T.
7308 when N_Indexed_Component
7309 | N_Selected_Component
7310 | N_Aggregate
7311 | N_Extension_Aggregate
7313 return True;
7315 -- The tag of the result returned by a function whose result
7316 -- type is a specific tagged type T identifies T.
7318 when N_Function_Call =>
7319 return True;
7321 when N_Explicit_Dereference =>
7322 return Is_Captured_Function_Call (Exp);
7324 -- For a tagged type, the operand of a qualified expression
7325 -- shall resolve to be of the type of the expression.
7327 when N_Qualified_Expression =>
7328 return Has_Tag_Of_Type (Expression (Exp));
7330 when others =>
7331 return False;
7332 end case;
7333 end if;
7334 end Has_Tag_Of_Type;
7336 --------------------
7337 -- Homonym_Number --
7338 --------------------
7340 function Homonym_Number (Subp : Entity_Id) return Pos is
7341 Hom : Entity_Id := Homonym (Subp);
7342 Count : Pos := 1;
7344 begin
7345 while Present (Hom) loop
7346 if Scope (Hom) = Scope (Subp) then
7347 Count := Count + 1;
7348 end if;
7350 Hom := Homonym (Hom);
7351 end loop;
7353 return Count;
7354 end Homonym_Number;
7356 -----------------------------------
7357 -- In_Library_Level_Package_Body --
7358 -----------------------------------
7360 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
7361 begin
7362 -- First determine whether the entity appears at the library level, then
7363 -- look at the containing unit.
7365 if Is_Library_Level_Entity (Id) then
7366 declare
7367 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
7369 begin
7370 return Nkind (Unit (Container)) = N_Package_Body;
7371 end;
7372 end if;
7374 return False;
7375 end In_Library_Level_Package_Body;
7377 ------------------------------
7378 -- In_Unconditional_Context --
7379 ------------------------------
7381 function In_Unconditional_Context (Node : Node_Id) return Boolean is
7382 P : Node_Id;
7384 begin
7385 P := Node;
7386 while Present (P) loop
7387 case Nkind (P) is
7388 when N_Subprogram_Body => return True;
7389 when N_If_Statement => return False;
7390 when N_Loop_Statement => return False;
7391 when N_Case_Statement => return False;
7392 when others => P := Parent (P);
7393 end case;
7394 end loop;
7396 return False;
7397 end In_Unconditional_Context;
7399 -------------------
7400 -- Insert_Action --
7401 -------------------
7403 procedure Insert_Action
7404 (Assoc_Node : Node_Id;
7405 Ins_Action : Node_Id;
7406 Spec_Expr_OK : Boolean := False)
7408 begin
7409 if Present (Ins_Action) then
7410 Insert_Actions
7411 (Assoc_Node => Assoc_Node,
7412 Ins_Actions => New_List (Ins_Action),
7413 Spec_Expr_OK => Spec_Expr_OK);
7414 end if;
7415 end Insert_Action;
7417 -- Version with check(s) suppressed
7419 procedure Insert_Action
7420 (Assoc_Node : Node_Id;
7421 Ins_Action : Node_Id;
7422 Suppress : Check_Id;
7423 Spec_Expr_OK : Boolean := False)
7425 begin
7426 Insert_Actions
7427 (Assoc_Node => Assoc_Node,
7428 Ins_Actions => New_List (Ins_Action),
7429 Suppress => Suppress,
7430 Spec_Expr_OK => Spec_Expr_OK);
7431 end Insert_Action;
7433 -------------------------
7434 -- Insert_Action_After --
7435 -------------------------
7437 procedure Insert_Action_After
7438 (Assoc_Node : Node_Id;
7439 Ins_Action : Node_Id)
7441 begin
7442 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
7443 end Insert_Action_After;
7445 --------------------
7446 -- Insert_Actions --
7447 --------------------
7449 procedure Insert_Actions
7450 (Assoc_Node : Node_Id;
7451 Ins_Actions : List_Id;
7452 Spec_Expr_OK : Boolean := False)
7454 N : Node_Id;
7455 P : Node_Id;
7457 Wrapped_Node : Node_Id := Empty;
7459 begin
7460 if Is_Empty_List (Ins_Actions) then
7461 return;
7462 end if;
7464 -- Insert the action when the context is "Handling of Default and Per-
7465 -- Object Expressions" only when requested by the caller.
7467 if Spec_Expr_OK then
7468 null;
7470 -- Ignore insert of actions from inside default expression (or other
7471 -- similar "spec expression") in the special spec-expression analyze
7472 -- mode. Any insertions at this point have no relevance, since we are
7473 -- only doing the analyze to freeze the types of any static expressions.
7474 -- See section "Handling of Default and Per-Object Expressions" in the
7475 -- spec of package Sem for further details.
7477 elsif In_Spec_Expression then
7478 return;
7479 end if;
7481 -- If the action derives from stuff inside a record, then the actions
7482 -- are attached to the current scope, to be inserted and analyzed on
7483 -- exit from the scope. The reason for this is that we may also be
7484 -- generating freeze actions at the same time, and they must eventually
7485 -- be elaborated in the correct order.
7487 if Is_Record_Type (Current_Scope)
7488 and then not Is_Frozen (Current_Scope)
7489 then
7490 if No (Scope_Stack.Table
7491 (Scope_Stack.Last).Pending_Freeze_Actions)
7492 then
7493 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
7494 Ins_Actions;
7495 else
7496 Append_List
7497 (Ins_Actions,
7498 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
7499 end if;
7501 return;
7502 end if;
7504 -- We now intend to climb up the tree to find the right point to
7505 -- insert the actions. We start at Assoc_Node, unless this node is a
7506 -- subexpression in which case we start with its parent. We do this for
7507 -- two reasons. First it speeds things up. Second, if Assoc_Node is
7508 -- itself one of the special nodes like N_And_Then, then we assume that
7509 -- an initial request to insert actions for such a node does not expect
7510 -- the actions to get deposited in the node for later handling when the
7511 -- node is expanded, since clearly the node is being dealt with by the
7512 -- caller. Note that in the subexpression case, N is always the child we
7513 -- came from.
7515 -- N_Raise_xxx_Error is an annoying special case, it is a statement
7516 -- if it has type Standard_Void_Type, and a subexpression otherwise.
7517 -- Procedure calls, and similarly procedure attribute references, are
7518 -- also statements.
7520 if Nkind (Assoc_Node) in N_Subexpr
7521 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
7522 or else Etype (Assoc_Node) /= Standard_Void_Type)
7523 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
7524 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
7525 or else not Is_Procedure_Attribute_Name
7526 (Attribute_Name (Assoc_Node)))
7527 then
7528 N := Assoc_Node;
7529 P := Parent (Assoc_Node);
7531 -- Nonsubexpression case. Note that N is initially Empty in this case
7532 -- (N is only guaranteed non-Empty in the subexpr case).
7534 else
7535 N := Empty;
7536 P := Assoc_Node;
7537 end if;
7539 -- Capture root of the transient scope
7541 if Scope_Is_Transient then
7542 Wrapped_Node := Node_To_Be_Wrapped;
7543 end if;
7545 loop
7546 pragma Assert (Present (P));
7548 -- Make sure that inserted actions stay in the transient scope
7550 if Present (Wrapped_Node) and then N = Wrapped_Node then
7551 Store_Before_Actions_In_Scope (Ins_Actions);
7552 return;
7553 end if;
7555 case Nkind (P) is
7557 -- Case of right operand of AND THEN or OR ELSE. Put the actions
7558 -- in the Actions field of the right operand. They will be moved
7559 -- out further when the AND THEN or OR ELSE operator is expanded.
7560 -- Nothing special needs to be done for the left operand since
7561 -- in that case the actions are executed unconditionally.
7563 when N_Short_Circuit =>
7564 if N = Right_Opnd (P) then
7566 -- We are now going to either append the actions to the
7567 -- actions field of the short-circuit operation. We will
7568 -- also analyze the actions now.
7570 -- This analysis is really too early, the proper thing would
7571 -- be to just park them there now, and only analyze them if
7572 -- we find we really need them, and to it at the proper
7573 -- final insertion point. However attempting to this proved
7574 -- tricky, so for now we just kill current values before and
7575 -- after the analyze call to make sure we avoid peculiar
7576 -- optimizations from this out of order insertion.
7578 Kill_Current_Values;
7580 -- If P has already been expanded, we can't park new actions
7581 -- on it, so we need to expand them immediately, introducing
7582 -- an Expression_With_Actions. N can't be an expression
7583 -- with actions, or else then the actions would have been
7584 -- inserted at an inner level.
7586 if Analyzed (P) then
7587 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
7588 Rewrite (N,
7589 Make_Expression_With_Actions (Sloc (N),
7590 Actions => Ins_Actions,
7591 Expression => Relocate_Node (N)));
7592 Analyze_And_Resolve (N);
7594 elsif Present (Actions (P)) then
7595 Insert_List_After_And_Analyze
7596 (Last (Actions (P)), Ins_Actions);
7597 else
7598 Set_Actions (P, Ins_Actions);
7599 Analyze_List (Actions (P));
7600 end if;
7602 Kill_Current_Values;
7604 return;
7605 end if;
7607 -- Then or Else dependent expression of an if expression. Add
7608 -- actions to Then_Actions or Else_Actions field as appropriate.
7609 -- The actions will be moved further out when the if is expanded.
7611 when N_If_Expression =>
7612 declare
7613 ThenX : constant Node_Id := Next (First (Expressions (P)));
7614 ElseX : constant Node_Id := Next (ThenX);
7616 begin
7617 -- If the enclosing expression is already analyzed, as
7618 -- is the case for nested elaboration checks, insert the
7619 -- conditional further out.
7621 if Analyzed (P) then
7622 null;
7624 -- Actions belong to the then expression, temporarily place
7625 -- them as Then_Actions of the if expression. They will be
7626 -- moved to the proper place later when the if expression is
7627 -- expanded.
7629 elsif N = ThenX then
7630 if Present (Then_Actions (P)) then
7631 Insert_List_After_And_Analyze
7632 (Last (Then_Actions (P)), Ins_Actions);
7633 else
7634 Set_Then_Actions (P, Ins_Actions);
7635 Analyze_List (Then_Actions (P));
7636 end if;
7638 return;
7640 -- Else_Actions is treated the same as Then_Actions above
7642 elsif N = ElseX then
7643 if Present (Else_Actions (P)) then
7644 Insert_List_After_And_Analyze
7645 (Last (Else_Actions (P)), Ins_Actions);
7646 else
7647 Set_Else_Actions (P, Ins_Actions);
7648 Analyze_List (Else_Actions (P));
7649 end if;
7651 return;
7653 -- Actions belong to the condition. In this case they are
7654 -- unconditionally executed, and so we can continue the
7655 -- search for the proper insert point.
7657 else
7658 null;
7659 end if;
7660 end;
7662 -- Alternative of case expression, we place the action in the
7663 -- Actions field of the case expression alternative, this will
7664 -- be handled when the case expression is expanded.
7666 when N_Case_Expression_Alternative =>
7667 if Present (Actions (P)) then
7668 Insert_List_After_And_Analyze
7669 (Last (Actions (P)), Ins_Actions);
7670 else
7671 Set_Actions (P, Ins_Actions);
7672 Analyze_List (Actions (P));
7673 end if;
7675 return;
7677 -- Case of appearing within an Expressions_With_Actions node. When
7678 -- the new actions come from the expression of the expression with
7679 -- actions, they must be added to the existing actions. The other
7680 -- alternative is when the new actions are related to one of the
7681 -- existing actions of the expression with actions, and should
7682 -- never reach here: if actions are inserted on a statement
7683 -- within the Actions of an expression with actions, or on some
7684 -- subexpression of such a statement, then the outermost proper
7685 -- insertion point is right before the statement, and we should
7686 -- never climb up as far as the N_Expression_With_Actions itself.
7688 when N_Expression_With_Actions =>
7689 if N = Expression (P) then
7690 if Is_Empty_List (Actions (P)) then
7691 Append_List_To (Actions (P), Ins_Actions);
7692 Analyze_List (Actions (P));
7693 else
7694 Insert_List_After_And_Analyze
7695 (Last (Actions (P)), Ins_Actions);
7696 end if;
7698 return;
7700 else
7701 raise Program_Error;
7702 end if;
7704 -- Case of appearing in the condition of a while expression or
7705 -- elsif. We insert the actions into the Condition_Actions field.
7706 -- They will be moved further out when the while loop or elsif
7707 -- is analyzed.
7709 when N_Elsif_Part
7710 | N_Iteration_Scheme
7712 if Present (Condition (P)) and then N = Condition (P) then
7713 if Present (Condition_Actions (P)) then
7714 Insert_List_After_And_Analyze
7715 (Last (Condition_Actions (P)), Ins_Actions);
7716 else
7717 Set_Condition_Actions (P, Ins_Actions);
7719 -- Set the parent of the insert actions explicitly. This
7720 -- is not a syntactic field, but we need the parent field
7721 -- set, in particular so that freeze can understand that
7722 -- it is dealing with condition actions, and properly
7723 -- insert the freezing actions.
7725 Set_Parent (Ins_Actions, P);
7726 Analyze_List (Condition_Actions (P));
7727 end if;
7729 return;
7730 end if;
7732 -- Statements, declarations, pragmas, representation clauses
7734 when
7735 -- Statements
7737 N_Procedure_Call_Statement
7738 | N_Statement_Other_Than_Procedure_Call
7740 -- Pragmas
7742 | N_Pragma
7744 -- Representation_Clause
7746 | N_At_Clause
7747 | N_Attribute_Definition_Clause
7748 | N_Enumeration_Representation_Clause
7749 | N_Record_Representation_Clause
7751 -- Declarations
7753 | N_Abstract_Subprogram_Declaration
7754 | N_Entry_Body
7755 | N_Exception_Declaration
7756 | N_Exception_Renaming_Declaration
7757 | N_Expression_Function
7758 | N_Formal_Abstract_Subprogram_Declaration
7759 | N_Formal_Concrete_Subprogram_Declaration
7760 | N_Formal_Object_Declaration
7761 | N_Formal_Type_Declaration
7762 | N_Full_Type_Declaration
7763 | N_Function_Instantiation
7764 | N_Generic_Function_Renaming_Declaration
7765 | N_Generic_Package_Declaration
7766 | N_Generic_Package_Renaming_Declaration
7767 | N_Generic_Procedure_Renaming_Declaration
7768 | N_Generic_Subprogram_Declaration
7769 | N_Implicit_Label_Declaration
7770 | N_Incomplete_Type_Declaration
7771 | N_Number_Declaration
7772 | N_Object_Declaration
7773 | N_Object_Renaming_Declaration
7774 | N_Package_Body
7775 | N_Package_Body_Stub
7776 | N_Package_Declaration
7777 | N_Package_Instantiation
7778 | N_Package_Renaming_Declaration
7779 | N_Private_Extension_Declaration
7780 | N_Private_Type_Declaration
7781 | N_Procedure_Instantiation
7782 | N_Protected_Body
7783 | N_Protected_Body_Stub
7784 | N_Single_Task_Declaration
7785 | N_Subprogram_Body
7786 | N_Subprogram_Body_Stub
7787 | N_Subprogram_Declaration
7788 | N_Subprogram_Renaming_Declaration
7789 | N_Subtype_Declaration
7790 | N_Task_Body
7791 | N_Task_Body_Stub
7793 -- Use clauses can appear in lists of declarations
7795 | N_Use_Package_Clause
7796 | N_Use_Type_Clause
7798 -- Freeze entity behaves like a declaration or statement
7800 | N_Freeze_Entity
7801 | N_Freeze_Generic_Entity
7803 -- Do not insert here if the item is not a list member (this
7804 -- happens for example with a triggering statement, and the
7805 -- proper approach is to insert before the entire select).
7807 if not Is_List_Member (P) then
7808 null;
7810 -- Do not insert if parent of P is an N_Component_Association
7811 -- node (i.e. we are in the context of an N_Aggregate or
7812 -- N_Extension_Aggregate node. In this case we want to insert
7813 -- before the entire aggregate.
7815 elsif Nkind (Parent (P)) = N_Component_Association then
7816 null;
7818 -- Do not insert if the parent of P is either an N_Variant node
7819 -- or an N_Record_Definition node, meaning in either case that
7820 -- P is a member of a component list, and that therefore the
7821 -- actions should be inserted outside the complete record
7822 -- declaration.
7824 elsif Nkind (Parent (P)) in N_Variant | N_Record_Definition then
7825 null;
7827 -- Do not insert freeze nodes within the loop generated for
7828 -- an aggregate, because they may be elaborated too late for
7829 -- subsequent use in the back end: within a package spec the
7830 -- loop is part of the elaboration procedure and is only
7831 -- elaborated during the second pass.
7833 -- If the loop comes from source, or the entity is local to the
7834 -- loop itself it must remain within.
7836 elsif Nkind (Parent (P)) = N_Loop_Statement
7837 and then not Comes_From_Source (Parent (P))
7838 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7839 and then
7840 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7841 then
7842 null;
7844 -- Otherwise we can go ahead and do the insertion
7846 elsif P = Wrapped_Node then
7847 Store_Before_Actions_In_Scope (Ins_Actions);
7848 return;
7850 else
7851 Insert_List_Before_And_Analyze (P, Ins_Actions);
7852 return;
7853 end if;
7855 -- the expansion of Task and protected type declarations can
7856 -- create declarations for temporaries which, like other actions
7857 -- are inserted and analyzed before the current declaraation.
7858 -- However, the current scope is the synchronized type, and
7859 -- for unnesting it is critical that the proper scope for these
7860 -- generated entities be the enclosing one.
7862 when N_Task_Type_Declaration
7863 | N_Protected_Type_Declaration =>
7865 Push_Scope (Scope (Current_Scope));
7866 Insert_List_Before_And_Analyze (P, Ins_Actions);
7867 Pop_Scope;
7868 return;
7870 -- A special case, N_Raise_xxx_Error can act either as a statement
7871 -- or a subexpression. We tell the difference by looking at the
7872 -- Etype. It is set to Standard_Void_Type in the statement case.
7874 when N_Raise_xxx_Error =>
7875 if Etype (P) = Standard_Void_Type then
7876 if P = Wrapped_Node then
7877 Store_Before_Actions_In_Scope (Ins_Actions);
7878 else
7879 Insert_List_Before_And_Analyze (P, Ins_Actions);
7880 end if;
7882 return;
7884 -- In the subexpression case, keep climbing
7886 else
7887 null;
7888 end if;
7890 -- If a component association appears within a loop created for
7891 -- an array aggregate, attach the actions to the association so
7892 -- they can be subsequently inserted within the loop. For other
7893 -- component associations insert outside of the aggregate. For
7894 -- an association that will generate a loop, its Loop_Actions
7895 -- attribute is already initialized (see exp_aggr.adb).
7897 -- The list of Loop_Actions can in turn generate additional ones,
7898 -- that are inserted before the associated node. If the associated
7899 -- node is outside the aggregate, the new actions are collected
7900 -- at the end of the Loop_Actions, to respect the order in which
7901 -- they are to be elaborated.
7903 when N_Component_Association
7904 | N_Iterated_Component_Association
7905 | N_Iterated_Element_Association
7907 if Nkind (Parent (P)) in N_Aggregate | N_Delta_Aggregate
7909 -- We must not climb up out of an N_Iterated_xxx_Association
7910 -- because the actions might contain references to the loop
7911 -- parameter, except if we come from the Discrete_Choices of
7912 -- N_Iterated_Component_Association which cannot contain any.
7913 -- But it turns out that setting the Loop_Actions field in
7914 -- the case of an N_Component_Association when the field was
7915 -- not already set can lead to gigi assertion failures that
7916 -- are presumably due to malformed trees, so don't do that.
7918 and then (Nkind (P) /= N_Iterated_Component_Association
7919 or else not Is_List_Member (N)
7920 or else
7921 List_Containing (N) /= Discrete_Choices (P))
7922 and then (Nkind (P) /= N_Component_Association
7923 or else Present (Loop_Actions (P)))
7924 then
7925 if Is_Empty_List (Loop_Actions (P)) then
7926 Set_Loop_Actions (P, Ins_Actions);
7927 Analyze_List (Ins_Actions);
7928 else
7929 declare
7930 Decl : Node_Id;
7932 begin
7933 -- Check whether these actions were generated by a
7934 -- declaration that is part of the Loop_Actions for
7935 -- the component_association.
7937 Decl := Assoc_Node;
7938 while Present (Decl) loop
7939 exit when Parent (Decl) = P
7940 and then Is_List_Member (Decl)
7941 and then
7942 List_Containing (Decl) = Loop_Actions (P);
7943 Decl := Parent (Decl);
7944 end loop;
7946 if Present (Decl) then
7947 Insert_List_Before_And_Analyze
7948 (Decl, Ins_Actions);
7949 else
7950 Insert_List_After_And_Analyze
7951 (Last (Loop_Actions (P)), Ins_Actions);
7952 end if;
7953 end;
7954 end if;
7956 return;
7958 else
7959 null;
7960 end if;
7962 -- Special case: an attribute denoting a procedure call
7964 when N_Attribute_Reference =>
7965 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7966 if P = Wrapped_Node then
7967 Store_Before_Actions_In_Scope (Ins_Actions);
7968 else
7969 Insert_List_Before_And_Analyze (P, Ins_Actions);
7970 end if;
7972 return;
7974 -- In the subexpression case, keep climbing
7976 else
7977 null;
7978 end if;
7980 -- Special case: a marker
7982 when N_Call_Marker
7983 | N_Variable_Reference_Marker
7985 if Is_List_Member (P) then
7986 Insert_List_Before_And_Analyze (P, Ins_Actions);
7987 return;
7988 end if;
7990 -- A contract node should not belong to the tree
7992 when N_Contract =>
7993 raise Program_Error;
7995 -- For all other node types, keep climbing tree
7997 when N_Abortable_Part
7998 | N_Accept_Alternative
7999 | N_Access_Definition
8000 | N_Access_Function_Definition
8001 | N_Access_Procedure_Definition
8002 | N_Access_To_Object_Definition
8003 | N_Aggregate
8004 | N_Allocator
8005 | N_Aspect_Specification
8006 | N_Case_Expression
8007 | N_Case_Statement_Alternative
8008 | N_Character_Literal
8009 | N_Compilation_Unit
8010 | N_Compilation_Unit_Aux
8011 | N_Component_Clause
8012 | N_Component_Declaration
8013 | N_Component_Definition
8014 | N_Component_List
8015 | N_Constrained_Array_Definition
8016 | N_Decimal_Fixed_Point_Definition
8017 | N_Defining_Character_Literal
8018 | N_Defining_Identifier
8019 | N_Defining_Operator_Symbol
8020 | N_Defining_Program_Unit_Name
8021 | N_Delay_Alternative
8022 | N_Delta_Aggregate
8023 | N_Delta_Constraint
8024 | N_Derived_Type_Definition
8025 | N_Designator
8026 | N_Digits_Constraint
8027 | N_Discriminant_Association
8028 | N_Discriminant_Specification
8029 | N_Empty
8030 | N_Entry_Body_Formal_Part
8031 | N_Entry_Call_Alternative
8032 | N_Entry_Declaration
8033 | N_Entry_Index_Specification
8034 | N_Enumeration_Type_Definition
8035 | N_Error
8036 | N_Exception_Handler
8037 | N_Expanded_Name
8038 | N_Explicit_Dereference
8039 | N_Extension_Aggregate
8040 | N_Floating_Point_Definition
8041 | N_Formal_Decimal_Fixed_Point_Definition
8042 | N_Formal_Derived_Type_Definition
8043 | N_Formal_Discrete_Type_Definition
8044 | N_Formal_Floating_Point_Definition
8045 | N_Formal_Modular_Type_Definition
8046 | N_Formal_Ordinary_Fixed_Point_Definition
8047 | N_Formal_Package_Declaration
8048 | N_Formal_Private_Type_Definition
8049 | N_Formal_Incomplete_Type_Definition
8050 | N_Formal_Signed_Integer_Type_Definition
8051 | N_Function_Call
8052 | N_Function_Specification
8053 | N_Generic_Association
8054 | N_Handled_Sequence_Of_Statements
8055 | N_Identifier
8056 | N_In
8057 | N_Index_Or_Discriminant_Constraint
8058 | N_Indexed_Component
8059 | N_Integer_Literal
8060 | N_Iterator_Specification
8061 | N_Interpolated_String_Literal
8062 | N_Itype_Reference
8063 | N_Label
8064 | N_Loop_Parameter_Specification
8065 | N_Mod_Clause
8066 | N_Modular_Type_Definition
8067 | N_Not_In
8068 | N_Null
8069 | N_Op_Abs
8070 | N_Op_Add
8071 | N_Op_And
8072 | N_Op_Concat
8073 | N_Op_Divide
8074 | N_Op_Eq
8075 | N_Op_Expon
8076 | N_Op_Ge
8077 | N_Op_Gt
8078 | N_Op_Le
8079 | N_Op_Lt
8080 | N_Op_Minus
8081 | N_Op_Mod
8082 | N_Op_Multiply
8083 | N_Op_Ne
8084 | N_Op_Not
8085 | N_Op_Or
8086 | N_Op_Plus
8087 | N_Op_Rem
8088 | N_Op_Rotate_Left
8089 | N_Op_Rotate_Right
8090 | N_Op_Shift_Left
8091 | N_Op_Shift_Right
8092 | N_Op_Shift_Right_Arithmetic
8093 | N_Op_Subtract
8094 | N_Op_Xor
8095 | N_Operator_Symbol
8096 | N_Ordinary_Fixed_Point_Definition
8097 | N_Others_Choice
8098 | N_Package_Specification
8099 | N_Parameter_Association
8100 | N_Parameter_Specification
8101 | N_Pop_Constraint_Error_Label
8102 | N_Pop_Program_Error_Label
8103 | N_Pop_Storage_Error_Label
8104 | N_Pragma_Argument_Association
8105 | N_Procedure_Specification
8106 | N_Protected_Definition
8107 | N_Push_Constraint_Error_Label
8108 | N_Push_Program_Error_Label
8109 | N_Push_Storage_Error_Label
8110 | N_Qualified_Expression
8111 | N_Quantified_Expression
8112 | N_Raise_Expression
8113 | N_Range
8114 | N_Range_Constraint
8115 | N_Real_Literal
8116 | N_Real_Range_Specification
8117 | N_Record_Definition
8118 | N_Reference
8119 | N_SCIL_Dispatch_Table_Tag_Init
8120 | N_SCIL_Dispatching_Call
8121 | N_SCIL_Membership_Test
8122 | N_Selected_Component
8123 | N_Signed_Integer_Type_Definition
8124 | N_Single_Protected_Declaration
8125 | N_Slice
8126 | N_String_Literal
8127 | N_Subtype_Indication
8128 | N_Subunit
8129 | N_Target_Name
8130 | N_Task_Definition
8131 | N_Terminate_Alternative
8132 | N_Triggering_Alternative
8133 | N_Type_Conversion
8134 | N_Unchecked_Expression
8135 | N_Unchecked_Type_Conversion
8136 | N_Unconstrained_Array_Definition
8137 | N_Unused_At_End
8138 | N_Unused_At_Start
8139 | N_Variant
8140 | N_Variant_Part
8141 | N_Validate_Unchecked_Conversion
8142 | N_With_Clause
8144 null;
8145 end case;
8147 -- If we fall through above tests, keep climbing tree
8149 N := P;
8151 if Nkind (Parent (N)) = N_Subunit then
8153 -- This is the proper body corresponding to a stub. Insertion must
8154 -- be done at the point of the stub, which is in the declarative
8155 -- part of the parent unit.
8157 P := Corresponding_Stub (Parent (N));
8159 else
8160 P := Parent (N);
8161 end if;
8162 end loop;
8163 end Insert_Actions;
8165 -- Version with check(s) suppressed
8167 procedure Insert_Actions
8168 (Assoc_Node : Node_Id;
8169 Ins_Actions : List_Id;
8170 Suppress : Check_Id;
8171 Spec_Expr_OK : Boolean := False)
8173 begin
8174 if Suppress = All_Checks then
8175 declare
8176 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
8177 begin
8178 Scope_Suppress.Suppress := (others => True);
8179 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8180 Scope_Suppress.Suppress := Sva;
8181 end;
8183 else
8184 declare
8185 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
8186 begin
8187 Scope_Suppress.Suppress (Suppress) := True;
8188 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8189 Scope_Suppress.Suppress (Suppress) := Svg;
8190 end;
8191 end if;
8192 end Insert_Actions;
8194 --------------------------
8195 -- Insert_Actions_After --
8196 --------------------------
8198 procedure Insert_Actions_After
8199 (Assoc_Node : Node_Id;
8200 Ins_Actions : List_Id)
8202 begin
8203 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
8204 Store_After_Actions_In_Scope (Ins_Actions);
8205 else
8206 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
8207 end if;
8208 end Insert_Actions_After;
8210 ---------------------------------
8211 -- Insert_Library_Level_Action --
8212 ---------------------------------
8214 procedure Insert_Library_Level_Action (N : Node_Id) is
8215 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8217 begin
8218 Push_Scope (Cunit_Entity (Current_Sem_Unit));
8219 -- And not Main_Unit as previously. If the main unit is a body,
8220 -- the scope needed to analyze the actions is the entity of the
8221 -- corresponding declaration.
8223 if No (Actions (Aux)) then
8224 Set_Actions (Aux, New_List (N));
8225 else
8226 Append (N, Actions (Aux));
8227 end if;
8229 Analyze (N);
8230 Pop_Scope;
8231 end Insert_Library_Level_Action;
8233 ----------------------------------
8234 -- Insert_Library_Level_Actions --
8235 ----------------------------------
8237 procedure Insert_Library_Level_Actions (L : List_Id) is
8238 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8240 begin
8241 if Is_Non_Empty_List (L) then
8242 Push_Scope (Cunit_Entity (Main_Unit));
8243 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
8245 if No (Actions (Aux)) then
8246 Set_Actions (Aux, L);
8247 Analyze_List (L);
8248 else
8249 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
8250 end if;
8252 Pop_Scope;
8253 end if;
8254 end Insert_Library_Level_Actions;
8256 ----------------------
8257 -- Inside_Init_Proc --
8258 ----------------------
8260 function Inside_Init_Proc return Boolean is
8261 begin
8262 return Present (Enclosing_Init_Proc);
8263 end Inside_Init_Proc;
8265 ----------------------
8266 -- Integer_Type_For --
8267 ----------------------
8269 function Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id is
8270 begin
8271 pragma Assert
8272 (Standard_Long_Integer_Size in
8273 Standard_Integer_Size | Standard_Long_Long_Integer_Size);
8274 -- So we don't need to check for Standard_Long_Integer_Size below
8275 pragma Assert (S <= System_Max_Integer_Size);
8277 -- This is the canonical 32-bit type
8279 if S <= Standard_Integer_Size then
8280 if Uns then
8281 return Standard_Unsigned;
8282 else
8283 return Standard_Integer;
8284 end if;
8286 -- This is the canonical 64-bit type
8288 elsif S <= Standard_Long_Long_Integer_Size then
8289 if Uns then
8290 return Standard_Long_Long_Unsigned;
8291 else
8292 return Standard_Long_Long_Integer;
8293 end if;
8295 -- This is the canonical 128-bit type
8297 elsif S <= Standard_Long_Long_Long_Integer_Size then
8298 if Uns then
8299 return Standard_Long_Long_Long_Unsigned;
8300 else
8301 return Standard_Long_Long_Long_Integer;
8302 end if;
8304 else
8305 raise Program_Error;
8306 end if;
8307 end Integer_Type_For;
8309 -------------------------------
8310 -- Is_Captured_Function_Call --
8311 -------------------------------
8313 function Is_Captured_Function_Call (N : Node_Id) return Boolean is
8314 begin
8315 if Nkind (N) = N_Explicit_Dereference
8316 and then Is_Entity_Name (Prefix (N))
8317 and then Ekind (Entity (Prefix (N))) = E_Constant
8318 then
8319 declare
8320 Value : constant Node_Id := Constant_Value (Entity (Prefix (N)));
8322 begin
8323 return Present (Value)
8324 and then Nkind (Value) = N_Reference
8325 and then Nkind (Prefix (Value)) = N_Function_Call;
8326 end;
8328 else
8329 return False;
8330 end if;
8331 end Is_Captured_Function_Call;
8333 ------------------------------
8334 -- Is_Finalizable_Transient --
8335 ------------------------------
8337 function Is_Finalizable_Transient
8338 (Decl : Node_Id;
8339 Rel_Node : Node_Id) return Boolean
8341 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
8342 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
8344 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
8345 -- Determine whether transient object Trans_Id is initialized either
8346 -- by a function call which returns an access type or simply renames
8347 -- another pointer.
8349 function Initialized_By_Aliased_BIP_Func_Call
8350 (Trans_Id : Entity_Id) return Boolean;
8351 -- Determine whether transient object Trans_Id is initialized by a
8352 -- build-in-place function call where the BIPalloc parameter either
8353 -- does not exist or is Caller_Allocation, and BIPaccess is not null.
8354 -- This case creates an aliasing between the returned value and the
8355 -- value denoted by BIPaccess.
8357 function Is_Aliased
8358 (Trans_Id : Entity_Id;
8359 First_Stmt : Node_Id) return Boolean;
8360 -- Determine whether transient object Trans_Id has been renamed or
8361 -- aliased through 'reference in the statement list starting from
8362 -- First_Stmt.
8364 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
8365 -- Determine whether transient object Trans_Id is allocated on the heap
8367 function Is_Indexed_Container
8368 (Trans_Id : Entity_Id;
8369 First_Stmt : Node_Id) return Boolean;
8370 -- Determine whether transient object Trans_Id denotes a container which
8371 -- is in the process of being indexed in the statement list starting
8372 -- from First_Stmt.
8374 function Is_Iterated_Container
8375 (Trans_Id : Entity_Id;
8376 First_Stmt : Node_Id) return Boolean;
8377 -- Determine whether transient object Trans_Id denotes a container which
8378 -- is in the process of being iterated in the statement list starting
8379 -- from First_Stmt.
8381 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean;
8382 -- Return True if N is directly part of a build-in-place return
8383 -- statement.
8385 ---------------------------
8386 -- Initialized_By_Access --
8387 ---------------------------
8389 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
8390 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8392 begin
8393 return
8394 Present (Expr)
8395 and then Nkind (Expr) /= N_Reference
8396 and then Is_Access_Type (Etype (Expr));
8397 end Initialized_By_Access;
8399 ------------------------------------------
8400 -- Initialized_By_Aliased_BIP_Func_Call --
8401 ------------------------------------------
8403 function Initialized_By_Aliased_BIP_Func_Call
8404 (Trans_Id : Entity_Id) return Boolean
8406 Call : Node_Id := Expression (Parent (Trans_Id));
8408 begin
8409 -- Build-in-place calls usually appear in 'reference format
8411 if Nkind (Call) = N_Reference then
8412 Call := Prefix (Call);
8413 end if;
8415 Call := Unqual_Conv (Call);
8417 -- We search for a formal with a matching suffix. We can't search
8418 -- for the full name, because of the code at the end of Sem_Ch6.-
8419 -- Create_Extra_Formals, which copies the Extra_Formals over to
8420 -- the Alias of an instance, which will cause the formals to have
8421 -- "incorrect" names. See also Exp_Ch6.Build_In_Place_Formal.
8423 if Is_Build_In_Place_Function_Call (Call) then
8424 declare
8425 Caller_Allocation_Val : constant Uint :=
8426 UI_From_Int (BIP_Allocation_Form'Pos (Caller_Allocation));
8427 Access_Suffix : constant String :=
8428 BIP_Formal_Suffix (BIP_Object_Access);
8429 Alloc_Suffix : constant String :=
8430 BIP_Formal_Suffix (BIP_Alloc_Form);
8432 function Has_Suffix (Name, Suffix : String) return Boolean;
8433 -- Return True if Name has suffix Suffix
8435 ----------------
8436 -- Has_Suffix --
8437 ----------------
8439 function Has_Suffix (Name, Suffix : String) return Boolean is
8440 Len : constant Natural := Suffix'Length;
8442 begin
8443 return Name'Length > Len
8444 and then Name (Name'Last - Len + 1 .. Name'Last) = Suffix;
8445 end Has_Suffix;
8447 Access_OK : Boolean := False;
8448 Alloc_OK : Boolean := True;
8449 Param : Node_Id;
8451 begin
8452 -- Examine all parameter associations of the function call
8454 Param := First (Parameter_Associations (Call));
8456 while Present (Param) loop
8457 if Nkind (Param) = N_Parameter_Association
8458 and then Nkind (Selector_Name (Param)) = N_Identifier
8459 then
8460 declare
8461 Actual : constant Node_Id
8462 := Explicit_Actual_Parameter (Param);
8463 Formal : constant Node_Id
8464 := Selector_Name (Param);
8465 Name : constant String
8466 := Get_Name_String (Chars (Formal));
8468 begin
8469 -- A nonnull BIPaccess has been found
8471 if Has_Suffix (Name, Access_Suffix)
8472 and then Nkind (Actual) /= N_Null
8473 then
8474 Access_OK := True;
8476 -- A BIPalloc has been found
8478 elsif Has_Suffix (Name, Alloc_Suffix)
8479 and then Nkind (Actual) = N_Integer_Literal
8480 then
8481 Alloc_OK := Intval (Actual) = Caller_Allocation_Val;
8482 end if;
8483 end;
8484 end if;
8486 Next (Param);
8487 end loop;
8489 return Access_OK and Alloc_OK;
8490 end;
8491 end if;
8493 return False;
8494 end Initialized_By_Aliased_BIP_Func_Call;
8496 ----------------
8497 -- Is_Aliased --
8498 ----------------
8500 function Is_Aliased
8501 (Trans_Id : Entity_Id;
8502 First_Stmt : Node_Id) return Boolean
8504 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
8505 -- Given an object renaming declaration, retrieve the entity of the
8506 -- renamed name. Return Empty if the renamed name is anything other
8507 -- than a variable or a constant.
8509 -------------------------
8510 -- Find_Renamed_Object --
8511 -------------------------
8513 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
8514 Ren_Obj : Node_Id := Empty;
8516 function Find_Object (N : Node_Id) return Traverse_Result;
8517 -- Try to detect an object which is either a constant or a
8518 -- variable.
8520 -----------------
8521 -- Find_Object --
8522 -----------------
8524 function Find_Object (N : Node_Id) return Traverse_Result is
8525 begin
8526 -- Stop the search once a constant or a variable has been
8527 -- detected.
8529 if Nkind (N) = N_Identifier
8530 and then Present (Entity (N))
8531 and then Ekind (Entity (N)) in E_Constant | E_Variable
8532 then
8533 Ren_Obj := Entity (N);
8534 return Abandon;
8535 end if;
8537 return OK;
8538 end Find_Object;
8540 procedure Search is new Traverse_Proc (Find_Object);
8542 -- Local variables
8544 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
8546 -- Start of processing for Find_Renamed_Object
8548 begin
8549 -- Actions related to dispatching calls may appear as renamings of
8550 -- tags. Do not process this type of renaming because it does not
8551 -- use the actual value of the object.
8553 if not Is_RTE (Typ, RE_Tag_Ptr) then
8554 Search (Name (Ren_Decl));
8555 end if;
8557 -- For renamings generated by Expand_N_Object_Declaration to deal
8558 -- with (class-wide) interface objects, there is an intermediate
8559 -- temporary of an anonymous access type used to hold the result
8560 -- of the displacement of the address of the renamed object.
8562 if Present (Ren_Obj)
8563 and then Ekind (Ren_Obj) = E_Constant
8564 and then Is_Itype (Etype (Ren_Obj))
8565 and then Ekind (Etype (Ren_Obj)) = E_Anonymous_Access_Type
8566 and then
8567 Is_Class_Wide_Type (Directly_Designated_Type (Etype (Ren_Obj)))
8568 and then
8569 Is_Interface (Directly_Designated_Type (Etype (Ren_Obj)))
8570 then
8571 Search (Constant_Value (Ren_Obj));
8572 end if;
8574 return Ren_Obj;
8575 end Find_Renamed_Object;
8577 -- Local variables
8579 Expr : Node_Id;
8580 Ren_Obj : Entity_Id;
8581 Stmt : Node_Id;
8583 -- Start of processing for Is_Aliased
8585 begin
8586 -- A controlled transient object is not considered aliased when it
8587 -- appears inside an expression_with_actions node even when there are
8588 -- explicit aliases of it:
8590 -- do
8591 -- Trans_Id : Ctrl_Typ ...; -- transient object
8592 -- Alias : ... := Trans_Id; -- object is aliased
8593 -- Val : constant Boolean :=
8594 -- ... Alias ...; -- aliasing ends
8595 -- <finalize Trans_Id> -- object safe to finalize
8596 -- in Val end;
8598 -- Expansion ensures that all aliases are encapsulated in the actions
8599 -- list and do not leak to the expression by forcing the evaluation
8600 -- of the expression.
8602 if Nkind (Rel_Node) = N_Expression_With_Actions then
8603 return False;
8605 -- Otherwise examine the statements after the controlled transient
8606 -- object and look for various forms of aliasing.
8608 else
8609 Stmt := First_Stmt;
8610 while Present (Stmt) loop
8611 if Nkind (Stmt) = N_Object_Declaration then
8612 Expr := Expression (Stmt);
8614 -- Aliasing of the form:
8615 -- Obj : ... := Trans_Id'reference;
8617 if Present (Expr)
8618 and then Nkind (Expr) = N_Reference
8619 and then Nkind (Prefix (Expr)) = N_Identifier
8620 and then Entity (Prefix (Expr)) = Trans_Id
8621 then
8622 return True;
8623 end if;
8625 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8626 Ren_Obj := Find_Renamed_Object (Stmt);
8628 -- Aliasing of the form:
8629 -- Obj : ... renames ... Trans_Id ...;
8631 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8632 return True;
8633 end if;
8634 end if;
8636 Next (Stmt);
8637 end loop;
8639 return False;
8640 end if;
8641 end Is_Aliased;
8643 ------------------
8644 -- Is_Allocated --
8645 ------------------
8647 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8648 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8649 begin
8650 return
8651 Is_Access_Type (Etype (Trans_Id))
8652 and then Present (Expr)
8653 and then Nkind (Expr) = N_Allocator;
8654 end Is_Allocated;
8656 --------------------------
8657 -- Is_Indexed_Container --
8658 --------------------------
8660 function Is_Indexed_Container
8661 (Trans_Id : Entity_Id;
8662 First_Stmt : Node_Id) return Boolean
8664 Aspect : Node_Id;
8665 Call : Node_Id;
8666 Index : Entity_Id;
8667 Param : Node_Id;
8668 Stmt : Node_Id;
8669 Typ : Entity_Id;
8671 begin
8672 -- It is not possible to iterate over containers in non-Ada 2012 code
8674 if Ada_Version < Ada_2012 then
8675 return False;
8676 end if;
8678 Typ := Etype (Trans_Id);
8680 -- Handle access type created for the reference below
8682 if Is_Access_Type (Typ) then
8683 Typ := Designated_Type (Typ);
8684 end if;
8686 -- Look for aspect Constant_Indexing. It may be part of a type
8687 -- declaration for a container, or inherited from a base type
8688 -- or parent type.
8690 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Constant_Indexing);
8692 if Present (Aspect) then
8693 Index := Entity (Aspect);
8695 -- Examine the statements following the container object and
8696 -- look for a call to the default indexing routine where the
8697 -- first parameter is the transient. Such a call appears as:
8699 -- It : Access_To_Constant_Reference_Type :=
8700 -- Constant_Indexing (Trans_Id.all, ...)'reference;
8702 Stmt := First_Stmt;
8703 while Present (Stmt) loop
8705 -- Detect an object declaration which is initialized by a
8706 -- controlled function call.
8708 if Nkind (Stmt) = N_Object_Declaration
8709 and then Present (Expression (Stmt))
8710 and then Nkind (Expression (Stmt)) = N_Reference
8711 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8712 then
8713 Call := Prefix (Expression (Stmt));
8715 -- The call must invoke the default indexing routine of
8716 -- the container and the transient object must appear as
8717 -- the first actual parameter. Skip any calls whose names
8718 -- are not entities.
8720 if Is_Entity_Name (Name (Call))
8721 and then Entity (Name (Call)) = Index
8722 and then Present (Parameter_Associations (Call))
8723 then
8724 Param := First (Parameter_Associations (Call));
8726 if Nkind (Param) = N_Explicit_Dereference
8727 and then Entity (Prefix (Param)) = Trans_Id
8728 then
8729 return True;
8730 end if;
8731 end if;
8732 end if;
8734 Next (Stmt);
8735 end loop;
8736 end if;
8738 return False;
8739 end Is_Indexed_Container;
8741 ---------------------------
8742 -- Is_Iterated_Container --
8743 ---------------------------
8745 function Is_Iterated_Container
8746 (Trans_Id : Entity_Id;
8747 First_Stmt : Node_Id) return Boolean
8749 Aspect : Node_Id;
8750 Call : Node_Id;
8751 Iter : Entity_Id;
8752 Param : Node_Id;
8753 Stmt : Node_Id;
8754 Typ : Entity_Id;
8756 begin
8757 -- It is not possible to iterate over containers in non-Ada 2012 code
8759 if Ada_Version < Ada_2012 then
8760 return False;
8761 end if;
8763 Typ := Etype (Trans_Id);
8765 -- Handle access type created for the reference below
8767 if Is_Access_Type (Typ) then
8768 Typ := Designated_Type (Typ);
8769 end if;
8771 -- Look for aspect Default_Iterator. It may be part of a type
8772 -- declaration for a container, or inherited from a base type
8773 -- or parent type.
8775 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8777 if Present (Aspect) then
8778 Iter := Entity (Aspect);
8780 -- Examine the statements following the container object and
8781 -- look for a call to the default iterate routine where the
8782 -- first parameter is the transient. Such a call appears as:
8784 -- It : Access_To_CW_Iterator :=
8785 -- Iterate (Trans_Id.all, ...)'reference;
8787 Stmt := First_Stmt;
8788 while Present (Stmt) loop
8790 -- Detect an object declaration which is initialized by a
8791 -- controlled function call.
8793 if Nkind (Stmt) = N_Object_Declaration
8794 and then Present (Expression (Stmt))
8795 and then Nkind (Expression (Stmt)) = N_Reference
8796 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8797 then
8798 Call := Prefix (Expression (Stmt));
8800 -- The call must invoke the default iterate routine of
8801 -- the container and the transient object must appear as
8802 -- the first actual parameter. Skip any calls whose names
8803 -- are not entities.
8805 if Is_Entity_Name (Name (Call))
8806 and then Entity (Name (Call)) = Iter
8807 and then Present (Parameter_Associations (Call))
8808 then
8809 Param := First (Parameter_Associations (Call));
8811 if Nkind (Param) = N_Explicit_Dereference
8812 and then Entity (Prefix (Param)) = Trans_Id
8813 then
8814 return True;
8815 end if;
8816 end if;
8817 end if;
8819 Next (Stmt);
8820 end loop;
8821 end if;
8823 return False;
8824 end Is_Iterated_Container;
8826 -------------------------------------
8827 -- Is_Part_Of_BIP_Return_Statement --
8828 -------------------------------------
8830 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean is
8831 Subp : constant Entity_Id := Current_Subprogram;
8832 Context : Node_Id;
8833 begin
8834 -- First check if N is part of a BIP function
8836 if No (Subp)
8837 or else not Is_Build_In_Place_Function (Subp)
8838 then
8839 return False;
8840 end if;
8842 -- Then check whether N is a complete part of a return statement
8843 -- Should we consider other node kinds to go up the tree???
8845 Context := N;
8846 loop
8847 case Nkind (Context) is
8848 when N_Expression_With_Actions => Context := Parent (Context);
8849 when N_Simple_Return_Statement => return True;
8850 when others => return False;
8851 end case;
8852 end loop;
8853 end Is_Part_Of_BIP_Return_Statement;
8855 -- Local variables
8857 Desig : Entity_Id := Obj_Typ;
8859 -- Start of processing for Is_Finalizable_Transient
8861 begin
8862 -- Handle access types
8864 if Is_Access_Type (Desig) then
8865 Desig := Available_View (Designated_Type (Desig));
8866 end if;
8868 return
8869 Ekind (Obj_Id) in E_Constant | E_Variable
8870 and then Needs_Finalization (Desig)
8871 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8872 and then not Is_Part_Of_BIP_Return_Statement (Rel_Node)
8874 -- Do not consider a transient object that was already processed
8876 and then not Is_Finalized_Transient (Obj_Id)
8878 -- Do not consider renamed or 'reference-d transient objects because
8879 -- the act of renaming extends the object's lifetime.
8881 and then not Is_Aliased (Obj_Id, Decl)
8883 -- Do not consider transient objects allocated on the heap since
8884 -- they are attached to a finalization master.
8886 and then not Is_Allocated (Obj_Id)
8888 -- If the transient object is a pointer, check that it is not
8889 -- initialized by a function that returns a pointer or acts as a
8890 -- renaming of another pointer.
8892 and then not
8893 (Is_Access_Type (Obj_Typ) and then Initialized_By_Access (Obj_Id))
8895 -- Do not consider transient objects which act as indirect aliases
8896 -- of build-in-place function results.
8898 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8900 -- Do not consider iterators because those are treated as normal
8901 -- controlled objects and are processed by the usual finalization
8902 -- machinery. This avoids the double finalization of an iterator.
8904 and then not Is_Iterator (Desig)
8906 -- Do not consider containers in the context of iterator loops. Such
8907 -- transient objects must exist for as long as the loop is around,
8908 -- otherwise any operation carried out by the iterator will fail.
8910 and then not Is_Iterated_Container (Obj_Id, Decl)
8912 -- Likewise for indexed containers in the context of iterator loops
8914 and then not Is_Indexed_Container (Obj_Id, Decl);
8915 end Is_Finalizable_Transient;
8917 ---------------------------------
8918 -- Is_Fully_Repped_Tagged_Type --
8919 ---------------------------------
8921 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8922 U : constant Entity_Id := Underlying_Type (T);
8923 Comp : Entity_Id;
8925 begin
8926 if No (U) or else not Is_Tagged_Type (U) then
8927 return False;
8928 elsif Has_Discriminants (U) then
8929 return False;
8930 elsif not Has_Specified_Layout (U) then
8931 return False;
8932 end if;
8934 -- Here we have a tagged type, see if it has any component (other than
8935 -- tag and parent) with no component_clause. If so, we return False.
8937 Comp := First_Component (U);
8938 while Present (Comp) loop
8939 if not Is_Tag (Comp)
8940 and then Chars (Comp) /= Name_uParent
8941 and then No (Component_Clause (Comp))
8942 then
8943 return False;
8944 else
8945 Next_Component (Comp);
8946 end if;
8947 end loop;
8949 -- All components have clauses
8951 return True;
8952 end Is_Fully_Repped_Tagged_Type;
8954 ----------------------------------
8955 -- Is_Library_Level_Tagged_Type --
8956 ----------------------------------
8958 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8959 begin
8960 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8961 end Is_Library_Level_Tagged_Type;
8963 --------------------------
8964 -- Is_Non_BIP_Func_Call --
8965 --------------------------
8967 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8968 begin
8969 -- The expected call is of the format
8971 -- Func_Call'reference
8973 return
8974 Nkind (Expr) = N_Reference
8975 and then Nkind (Prefix (Expr)) = N_Function_Call
8976 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8977 end Is_Non_BIP_Func_Call;
8979 ----------------------------------
8980 -- Is_Possibly_Unaligned_Object --
8981 ----------------------------------
8983 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8984 T : constant Entity_Id := Etype (N);
8986 begin
8987 -- If renamed object, apply test to underlying object
8989 if Is_Entity_Name (N)
8990 and then Is_Object (Entity (N))
8991 and then Present (Renamed_Object (Entity (N)))
8992 then
8993 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8994 end if;
8996 -- Tagged and controlled types and aliased types are always aligned, as
8997 -- are concurrent types.
8999 if Is_Aliased (T)
9000 or else Has_Controlled_Component (T)
9001 or else Is_Concurrent_Type (T)
9002 or else Is_Tagged_Type (T)
9003 or else Is_Controlled (T)
9004 then
9005 return False;
9006 end if;
9008 -- If this is an element of a packed array, may be unaligned
9010 if Is_Ref_To_Bit_Packed_Array (N) then
9011 return True;
9012 end if;
9014 -- Case of indexed component reference: test whether prefix is unaligned
9016 if Nkind (N) = N_Indexed_Component then
9017 return Is_Possibly_Unaligned_Object (Prefix (N));
9019 -- Case of selected component reference
9021 elsif Nkind (N) = N_Selected_Component then
9022 declare
9023 P : constant Node_Id := Prefix (N);
9024 C : constant Entity_Id := Entity (Selector_Name (N));
9025 M : Nat;
9026 S : Nat;
9028 begin
9029 -- If component reference is for an array with nonstatic bounds,
9030 -- then it is always aligned: we can only process unaligned arrays
9031 -- with static bounds (more precisely compile time known bounds).
9033 if Is_Array_Type (T)
9034 and then not Compile_Time_Known_Bounds (T)
9035 then
9036 return False;
9037 end if;
9039 -- If component is aliased, it is definitely properly aligned
9041 if Is_Aliased (C) then
9042 return False;
9043 end if;
9045 -- If component is for a type implemented as a scalar, and the
9046 -- record is packed, and the component is other than the first
9047 -- component of the record, then the component may be unaligned.
9049 if Is_Packed (Etype (P))
9050 and then Represented_As_Scalar (Etype (C))
9051 and then First_Entity (Scope (C)) /= C
9052 then
9053 return True;
9054 end if;
9056 -- Compute maximum possible alignment for T
9058 -- If alignment is known, then that settles things
9060 if Known_Alignment (T) then
9061 M := UI_To_Int (Alignment (T));
9063 -- If alignment is not known, tentatively set max alignment
9065 else
9066 M := Ttypes.Maximum_Alignment;
9068 -- We can reduce this if the Esize is known since the default
9069 -- alignment will never be more than the smallest power of 2
9070 -- that does not exceed this Esize value.
9072 if Known_Esize (T) then
9073 S := UI_To_Int (Esize (T));
9075 while (M / 2) >= S loop
9076 M := M / 2;
9077 end loop;
9078 end if;
9079 end if;
9081 -- Case of component clause present which may specify an
9082 -- unaligned position.
9084 if Present (Component_Clause (C)) then
9086 -- Otherwise we can do a test to make sure that the actual
9087 -- start position in the record, and the length, are both
9088 -- consistent with the required alignment. If not, we know
9089 -- that we are unaligned.
9091 declare
9092 Align_In_Bits : constant Nat := M * System_Storage_Unit;
9093 Comp : Entity_Id;
9095 begin
9096 Comp := C;
9098 -- For a component inherited in a record extension, the
9099 -- clause is inherited but position and size are not set.
9101 if Is_Base_Type (Etype (P))
9102 and then Is_Tagged_Type (Etype (P))
9103 and then Present (Original_Record_Component (Comp))
9104 then
9105 Comp := Original_Record_Component (Comp);
9106 end if;
9108 if Component_Bit_Offset (Comp) mod Align_In_Bits /= 0
9109 or else Esize (Comp) mod Align_In_Bits /= 0
9110 then
9111 return True;
9112 end if;
9113 end;
9114 end if;
9116 -- Otherwise, for a component reference, test prefix
9118 return Is_Possibly_Unaligned_Object (P);
9119 end;
9121 -- If not a component reference, must be aligned
9123 else
9124 return False;
9125 end if;
9126 end Is_Possibly_Unaligned_Object;
9128 ---------------------------------
9129 -- Is_Possibly_Unaligned_Slice --
9130 ---------------------------------
9132 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
9133 begin
9134 -- Go to renamed object
9136 if Is_Entity_Name (N)
9137 and then Is_Object (Entity (N))
9138 and then Present (Renamed_Object (Entity (N)))
9139 then
9140 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
9141 end if;
9143 -- The reference must be a slice
9145 if Nkind (N) /= N_Slice then
9146 return False;
9147 end if;
9149 -- If it is a slice, then look at the array type being sliced
9151 declare
9152 Sarr : constant Node_Id := Prefix (N);
9153 -- Prefix of the slice, i.e. the array being sliced
9155 Styp : constant Entity_Id := Etype (Prefix (N));
9156 -- Type of the array being sliced
9158 Pref : Node_Id;
9159 Ptyp : Entity_Id;
9161 begin
9162 -- The problems arise if the array object that is being sliced
9163 -- is a component of a record or array, and we cannot guarantee
9164 -- the alignment of the array within its containing object.
9166 -- To investigate this, we look at successive prefixes to see
9167 -- if we have a worrisome indexed or selected component.
9169 Pref := Sarr;
9170 loop
9171 -- Case of array is part of an indexed component reference
9173 if Nkind (Pref) = N_Indexed_Component then
9174 Ptyp := Etype (Prefix (Pref));
9176 -- The only problematic case is when the array is packed, in
9177 -- which case we really know nothing about the alignment of
9178 -- individual components.
9180 if Is_Bit_Packed_Array (Ptyp) then
9181 return True;
9182 end if;
9184 -- Case of array is part of a selected component reference
9186 elsif Nkind (Pref) = N_Selected_Component then
9187 Ptyp := Etype (Prefix (Pref));
9189 -- We are definitely in trouble if the record in question
9190 -- has an alignment, and either we know this alignment is
9191 -- inconsistent with the alignment of the slice, or we don't
9192 -- know what the alignment of the slice should be. But this
9193 -- really matters only if the target has strict alignment.
9195 if Target_Strict_Alignment
9196 and then Known_Alignment (Ptyp)
9197 and then (not Known_Alignment (Styp)
9198 or else Alignment (Styp) > Alignment (Ptyp))
9199 then
9200 return True;
9201 end if;
9203 -- We are in potential trouble if the record type is packed.
9204 -- We could special case when we know that the array is the
9205 -- first component, but that's not such a simple case ???
9207 if Is_Packed (Ptyp) then
9208 return True;
9209 end if;
9211 -- We are in trouble if there is a component clause, and
9212 -- either we do not know the alignment of the slice, or
9213 -- the alignment of the slice is inconsistent with the
9214 -- bit position specified by the component clause.
9216 declare
9217 Field : constant Entity_Id := Entity (Selector_Name (Pref));
9218 begin
9219 if Present (Component_Clause (Field))
9220 and then
9221 (not Known_Alignment (Styp)
9222 or else
9223 (Component_Bit_Offset (Field) mod
9224 (System_Storage_Unit * Alignment (Styp))) /= 0)
9225 then
9226 return True;
9227 end if;
9228 end;
9230 -- For cases other than selected or indexed components we know we
9231 -- are OK, since no issues arise over alignment.
9233 else
9234 return False;
9235 end if;
9237 -- We processed an indexed component or selected component
9238 -- reference that looked safe, so keep checking prefixes.
9240 Pref := Prefix (Pref);
9241 end loop;
9242 end;
9243 end Is_Possibly_Unaligned_Slice;
9245 -------------------------------
9246 -- Is_Related_To_Func_Return --
9247 -------------------------------
9249 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
9250 Expr : constant Node_Id := Related_Expression (Id);
9251 begin
9252 -- In the case of a function with a class-wide result that returns
9253 -- a call to a function with a specific result, we introduce a
9254 -- type conversion for the return expression. We do not want that
9255 -- type conversion to influence the result of this function.
9257 return
9258 Present (Expr)
9259 and then Nkind (Unqual_Conv (Expr)) = N_Explicit_Dereference
9260 and then (Nkind (Parent (Expr)) = N_Simple_Return_Statement
9261 or else
9262 (Nkind (Parent (Expr)) in N_Object_Declaration
9263 | N_Object_Renaming_Declaration
9264 and then
9265 Is_Return_Object (Defining_Entity (Parent (Expr)))));
9266 end Is_Related_To_Func_Return;
9268 --------------------------------
9269 -- Is_Ref_To_Bit_Packed_Array --
9270 --------------------------------
9272 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
9273 Result : Boolean;
9274 Expr : Node_Id;
9276 begin
9277 if Is_Entity_Name (N)
9278 and then Is_Object (Entity (N))
9279 and then Present (Renamed_Object (Entity (N)))
9280 then
9281 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
9282 end if;
9284 if Nkind (N) in N_Indexed_Component | N_Selected_Component then
9285 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
9286 Result := True;
9287 else
9288 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
9289 end if;
9291 if Result and then Nkind (N) = N_Indexed_Component then
9292 Expr := First (Expressions (N));
9293 while Present (Expr) loop
9294 Force_Evaluation (Expr);
9295 Next (Expr);
9296 end loop;
9297 end if;
9299 return Result;
9301 else
9302 return False;
9303 end if;
9304 end Is_Ref_To_Bit_Packed_Array;
9306 --------------------------------
9307 -- Is_Ref_To_Bit_Packed_Slice --
9308 --------------------------------
9310 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
9311 begin
9312 if Nkind (N) = N_Type_Conversion then
9313 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
9315 elsif Is_Entity_Name (N)
9316 and then Is_Object (Entity (N))
9317 and then Present (Renamed_Object (Entity (N)))
9318 then
9319 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
9321 elsif Nkind (N) = N_Slice
9322 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
9323 then
9324 return True;
9326 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9327 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
9329 else
9330 return False;
9331 end if;
9332 end Is_Ref_To_Bit_Packed_Slice;
9334 -----------------------
9335 -- Is_Renamed_Object --
9336 -----------------------
9338 function Is_Renamed_Object (N : Node_Id) return Boolean is
9339 Pnod : constant Node_Id := Parent (N);
9340 Kind : constant Node_Kind := Nkind (Pnod);
9341 begin
9342 if Kind = N_Object_Renaming_Declaration then
9343 return True;
9344 elsif Kind in N_Indexed_Component | N_Selected_Component then
9345 return Is_Renamed_Object (Pnod);
9346 else
9347 return False;
9348 end if;
9349 end Is_Renamed_Object;
9351 --------------------------------------
9352 -- Is_Secondary_Stack_BIP_Func_Call --
9353 --------------------------------------
9355 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
9356 Actual : Node_Id;
9357 Call : Node_Id := Expr;
9358 Formal : Node_Id;
9359 Param : Node_Id;
9361 begin
9362 -- Build-in-place calls usually appear in 'reference format. Note that
9363 -- the accessibility check machinery may add an extra 'reference due to
9364 -- side-effect removal.
9366 while Nkind (Call) = N_Reference loop
9367 Call := Prefix (Call);
9368 end loop;
9370 Call := Unqual_Conv (Call);
9372 if Is_Build_In_Place_Function_Call (Call) then
9374 -- Examine all parameter associations of the function call
9376 Param := First (Parameter_Associations (Call));
9377 while Present (Param) loop
9378 if Nkind (Param) = N_Parameter_Association then
9379 Formal := Selector_Name (Param);
9380 Actual := Explicit_Actual_Parameter (Param);
9382 -- A match for BIPalloc => 2 has been found
9384 if Is_Build_In_Place_Entity (Formal)
9385 and then BIP_Suffix_Kind (Formal) = BIP_Alloc_Form
9386 and then Nkind (Actual) = N_Integer_Literal
9387 and then Intval (Actual) = Uint_2
9388 then
9389 return True;
9390 end if;
9391 end if;
9393 Next (Param);
9394 end loop;
9395 end if;
9397 return False;
9398 end Is_Secondary_Stack_BIP_Func_Call;
9400 ------------------------------
9401 -- Is_Secondary_Stack_Thunk --
9402 ------------------------------
9404 function Is_Secondary_Stack_Thunk (Id : Entity_Id) return Boolean is
9405 begin
9406 return Ekind (Id) = E_Function
9407 and then Is_Thunk (Id)
9408 and then Has_Controlling_Result (Id);
9409 end Is_Secondary_Stack_Thunk;
9411 ----------------------------
9412 -- Is_Statically_Disabled --
9413 ----------------------------
9415 function Is_Statically_Disabled
9416 (N : Node_Id;
9417 Value : Boolean;
9418 Include_Valid : Boolean)
9419 return Boolean
9421 function Is_Discrete_Literal (N : Node_Id) return Boolean;
9422 -- Returns whether N is an integer, character or enumeration literal
9424 -------------------------
9425 -- Is_Discrete_Literal --
9426 -------------------------
9428 function Is_Discrete_Literal (N : Node_Id) return Boolean is
9429 (Nkind (N) in N_Integer_Literal | N_Character_Literal
9430 or else (Nkind (N) in N_Identifier | N_Expanded_Name
9431 and then Ekind (Entity (N)) = E_Enumeration_Literal));
9433 Expr_N : constant Node_Id :=
9434 (if Is_Static_Expression (N)
9435 and then Entity (N) in Standard_True | Standard_False
9436 and then Is_Rewrite_Substitution (N)
9437 then Original_Node (N)
9438 else N);
9440 -- Start of processing for Is_Statically_Disabled
9442 begin
9443 -- A "statically disabled" condition which evaluates to Value is either:
9445 case Nkind (Expr_N) is
9447 -- an AND or AND THEN operator when:
9448 -- - Value is True and both operands are statically disabled
9449 -- conditions evaluated to True.
9450 -- - Value is False and at least one operand is a statically disabled
9451 -- condition evaluated to False.
9453 when N_Op_And | N_And_Then =>
9454 return
9455 (if Value then
9456 (Is_Statically_Disabled
9457 (Left_Opnd (Expr_N), Value, Include_Valid)
9458 and then Is_Statically_Disabled
9459 (Right_Opnd (Expr_N), Value, Include_Valid))
9460 else
9461 (Is_Statically_Disabled
9462 (Left_Opnd (Expr_N), Value, Include_Valid)
9463 or else Is_Statically_Disabled
9464 (Right_Opnd (Expr_N), Value, Include_Valid)));
9466 -- an OR or OR ELSE operator when:
9467 -- - Value is True and at least one operand is a statically disabled
9468 -- condition evaluated to True.
9469 -- - Value is False and both operands are statically disabled
9470 -- conditions evaluated to False.
9472 when N_Op_Or | N_Or_Else =>
9473 return
9474 (if Value then
9475 (Is_Statically_Disabled
9476 (Left_Opnd (Expr_N), Value, Include_Valid)
9477 or else Is_Statically_Disabled
9478 (Right_Opnd (Expr_N), Value, Include_Valid))
9479 else
9480 (Is_Statically_Disabled
9481 (Left_Opnd (Expr_N), Value, Include_Valid)
9482 and then Is_Statically_Disabled
9483 (Right_Opnd (Expr_N), Value, Include_Valid)));
9485 -- a NOT operator when the right operand is a statically disabled
9486 -- condition evaluated to the negation of Value.
9488 when N_Op_Not =>
9489 return Is_Statically_Disabled
9490 (Right_Opnd (Expr_N), not Value, Include_Valid);
9492 -- a static constant when it is of a boolean type with aspect
9493 -- Warnings Off.
9495 when N_Identifier | N_Expanded_Name =>
9496 return Is_Static_Expression (Expr_N)
9497 and then Value = Is_True (Expr_Value (Expr_N))
9498 and then Ekind (Entity (Expr_N)) = E_Constant
9499 and then Has_Warnings_Off (Entity (Expr_N));
9501 -- a relational_operator where one operand is a static constant with
9502 -- aspect Warnings Off and the other operand is a literal of the
9503 -- corresponding type.
9505 when N_Op_Compare =>
9506 declare
9507 Left : constant Node_Id := Left_Opnd (Expr_N);
9508 Right : constant Node_Id := Right_Opnd (Expr_N);
9509 begin
9510 return
9511 Is_Static_Expression (N)
9512 and then Value = Is_True (Expr_Value (N))
9513 and then
9514 ((Is_Discrete_Literal (Right)
9515 and then Nkind (Left) in N_Identifier
9516 | N_Expanded_Name
9517 and then Ekind (Entity (Left)) = E_Constant
9518 and then Has_Warnings_Off (Entity (Left)))
9519 or else
9520 (Is_Discrete_Literal (Left)
9521 and then Nkind (Right) in N_Identifier
9522 | N_Expanded_Name
9523 and then Ekind (Entity (Right)) = E_Constant
9524 and then Has_Warnings_Off (Entity (Right))));
9525 end;
9527 -- a reference to 'Valid or 'Valid_Scalar if Include_Valid is True
9529 when N_Attribute_Reference =>
9530 return Include_Valid
9531 and then Get_Attribute_Id (Attribute_Name (Expr_N)) in
9532 Attribute_Valid | Attribute_Valid_Scalars
9533 and then Value;
9535 when others =>
9536 return False;
9537 end case;
9538 end Is_Statically_Disabled;
9540 --------------------------------
9541 -- Is_Uninitialized_Aggregate --
9542 --------------------------------
9544 function Is_Uninitialized_Aggregate
9545 (Exp : Node_Id;
9546 T : Entity_Id) return Boolean
9548 Comp : Node_Id;
9549 Comp_Type : Entity_Id;
9550 Typ : Entity_Id;
9552 begin
9553 if Nkind (Exp) /= N_Aggregate then
9554 return False;
9555 end if;
9557 Preanalyze_And_Resolve (Exp, T);
9558 Typ := Etype (Exp);
9560 if No (Typ)
9561 or else Ekind (Typ) /= E_Array_Subtype
9562 or else Present (Expressions (Exp))
9563 or else No (Component_Associations (Exp))
9564 then
9565 return False;
9566 else
9567 Comp_Type := Component_Type (Typ);
9568 Comp := First (Component_Associations (Exp));
9570 if not Box_Present (Comp)
9571 or else Present (Next (Comp))
9572 then
9573 return False;
9574 end if;
9576 return Is_Scalar_Type (Comp_Type)
9577 and then No (Default_Aspect_Component_Value (Typ));
9578 end if;
9579 end Is_Uninitialized_Aggregate;
9581 ----------------------------
9582 -- Is_Untagged_Derivation --
9583 ----------------------------
9585 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
9586 begin
9587 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
9588 or else
9589 (Is_Private_Type (T) and then Present (Full_View (T))
9590 and then not Is_Tagged_Type (Full_View (T))
9591 and then Is_Derived_Type (Full_View (T))
9592 and then Etype (Full_View (T)) /= T);
9593 end Is_Untagged_Derivation;
9595 ------------------------------------
9596 -- Is_Untagged_Private_Derivation --
9597 ------------------------------------
9599 function Is_Untagged_Private_Derivation
9600 (Priv_Typ : Entity_Id;
9601 Full_Typ : Entity_Id) return Boolean
9603 begin
9604 return
9605 Present (Priv_Typ)
9606 and then Is_Untagged_Derivation (Priv_Typ)
9607 and then Is_Private_Type (Etype (Priv_Typ))
9608 and then Present (Full_Typ)
9609 and then Is_Itype (Full_Typ);
9610 end Is_Untagged_Private_Derivation;
9612 ------------------------------
9613 -- Is_Verifiable_DIC_Pragma --
9614 ------------------------------
9616 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
9617 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9619 begin
9620 -- To qualify as verifiable, a DIC pragma must have a non-null argument
9622 return
9623 Present (Args)
9625 -- If there are args, but the first arg is Empty, then treat the
9626 -- pragma the same as having no args (there may be a second arg that
9627 -- is an implicitly added type arg, and Empty is a placeholder).
9629 and then Present (Get_Pragma_Arg (First (Args)))
9631 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
9632 end Is_Verifiable_DIC_Pragma;
9634 ---------------------------
9635 -- Is_Volatile_Reference --
9636 ---------------------------
9638 function Is_Volatile_Reference (N : Node_Id) return Boolean is
9639 begin
9640 -- Only source references are to be treated as volatile, internally
9641 -- generated stuff cannot have volatile external effects.
9643 if not Comes_From_Source (N) then
9644 return False;
9646 -- Never true for reference to a type
9648 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9649 return False;
9651 -- Never true for a compile time known constant
9653 elsif Compile_Time_Known_Value (N) then
9654 return False;
9656 -- True if object reference with volatile type
9658 elsif Is_Volatile_Object_Ref (N) then
9659 return True;
9661 -- True if reference to volatile entity
9663 elsif Is_Entity_Name (N) then
9664 return Treat_As_Volatile (Entity (N));
9666 -- True for slice of volatile array
9668 elsif Nkind (N) = N_Slice then
9669 return Is_Volatile_Reference (Prefix (N));
9671 -- True if volatile component
9673 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9674 if (Is_Entity_Name (Prefix (N))
9675 and then Has_Volatile_Components (Entity (Prefix (N))))
9676 or else (Present (Etype (Prefix (N)))
9677 and then Has_Volatile_Components (Etype (Prefix (N))))
9678 then
9679 return True;
9680 else
9681 return Is_Volatile_Reference (Prefix (N));
9682 end if;
9684 -- Otherwise false
9686 else
9687 return False;
9688 end if;
9689 end Is_Volatile_Reference;
9691 --------------------
9692 -- Kill_Dead_Code --
9693 --------------------
9695 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
9696 W : Boolean := Warn;
9697 -- Set False if warnings suppressed
9699 begin
9700 if Present (N) then
9701 Remove_Warning_Messages (N);
9703 -- Update the internal structures of the ABE mechanism in case the
9704 -- dead node is an elaboration scenario.
9706 Kill_Elaboration_Scenario (N);
9708 -- Generate warning if appropriate
9710 if W then
9712 -- We suppress the warning if this code is under control of an
9713 -- if/case statement and either
9714 -- a) we are in an instance and the condition/selector
9715 -- has a statically known value; or
9716 -- b) the selector of a case statement is a simple identifier
9717 -- and warnings off is set for this identifier; or
9718 -- c) the condition of an if statement is a "statically
9719 -- disabled" condition which evaluates to False as described
9720 -- in section 7.3.2 of SPARK User's Guide.
9721 -- Dead code is common and reasonable in instances, so we don't
9722 -- want a warning in that case.
9724 declare
9725 C : Node_Id := Empty;
9726 begin
9727 if Nkind (Parent (N)) = N_If_Statement then
9728 C := Condition (Parent (N));
9730 if Is_Statically_Disabled
9731 (C, Value => False, Include_Valid => False)
9732 then
9733 W := False;
9734 end if;
9736 elsif Nkind (Parent (N)) = N_Case_Statement_Alternative then
9737 C := Expression (Parent (Parent (N)));
9739 if Nkind (C) = N_Identifier
9740 and then Present (Entity (C))
9741 and then Has_Warnings_Off (Entity (C))
9742 then
9743 W := False;
9744 end if;
9745 end if;
9747 if Present (C)
9748 and then (In_Instance and Compile_Time_Known_Value (C))
9749 then
9750 W := False;
9751 end if;
9752 end;
9754 -- Generate warning if not suppressed
9756 if W then
9757 Error_Msg_F
9758 ("?t?this code can never be executed and has been deleted!",
9760 end if;
9761 end if;
9763 -- Recurse into block statements and bodies to process declarations
9764 -- and statements.
9766 if Nkind (N) = N_Block_Statement
9767 or else Nkind (N) = N_Subprogram_Body
9768 or else Nkind (N) = N_Package_Body
9769 then
9770 Kill_Dead_Code (Declarations (N), False);
9771 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
9773 if Nkind (N) = N_Subprogram_Body then
9774 Set_Is_Eliminated (Defining_Entity (N));
9775 end if;
9777 elsif Nkind (N) = N_Package_Declaration then
9778 Kill_Dead_Code (Visible_Declarations (Specification (N)));
9779 Kill_Dead_Code (Private_Declarations (Specification (N)));
9781 -- ??? After this point, Delete_Tree has been called on all
9782 -- declarations in Specification (N), so references to entities
9783 -- therein look suspicious.
9785 declare
9786 E : Entity_Id := First_Entity (Defining_Entity (N));
9788 begin
9789 while Present (E) loop
9790 if Ekind (E) = E_Operator then
9791 Set_Is_Eliminated (E);
9792 end if;
9794 Next_Entity (E);
9795 end loop;
9796 end;
9798 -- Recurse into composite statement to kill individual statements in
9799 -- particular instantiations.
9801 elsif Nkind (N) = N_If_Statement then
9802 Kill_Dead_Code (Then_Statements (N));
9803 Kill_Dead_Code (Elsif_Parts (N));
9804 Kill_Dead_Code (Else_Statements (N));
9806 elsif Nkind (N) = N_Loop_Statement then
9807 Kill_Dead_Code (Statements (N));
9809 elsif Nkind (N) = N_Case_Statement then
9810 declare
9811 Alt : Node_Id;
9812 begin
9813 Alt := First (Alternatives (N));
9814 while Present (Alt) loop
9815 Kill_Dead_Code (Statements (Alt));
9816 Next (Alt);
9817 end loop;
9818 end;
9820 elsif Nkind (N) = N_Case_Statement_Alternative then
9821 Kill_Dead_Code (Statements (N));
9823 -- Deal with dead instances caused by deleting instantiations
9825 elsif Nkind (N) in N_Generic_Instantiation then
9826 Remove_Dead_Instance (N);
9827 end if;
9828 end if;
9829 end Kill_Dead_Code;
9831 -- Case where argument is a list of nodes to be killed
9833 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
9834 N : Node_Id;
9835 W : Boolean;
9837 begin
9838 W := Warn;
9840 N := First (L);
9841 while Present (N) loop
9842 Kill_Dead_Code (N, W);
9843 W := False;
9844 Next (N);
9845 end loop;
9846 end Kill_Dead_Code;
9848 -----------------------------
9849 -- Make_CW_Equivalent_Type --
9850 -----------------------------
9852 -- Create a record type used as an equivalent of any member of the class
9853 -- which takes its size from exp.
9855 -- Generate the following code:
9857 -- type Equiv_T is record
9858 -- _parent : T (List of discriminant constraints taken from Exp);
9859 -- Cnn : Storage_Array (1 .. (Exp'size - Typ'object_size)/Storage_Unit);
9860 -- end Equiv_T;
9862 -- Note that this type does not guarantee same alignment as all derived
9863 -- types.
9865 -- Note: for the freezing circuitry, this looks like a record extension,
9866 -- and so we need to make sure that the scalar storage order is the same
9867 -- as that of the parent type. (This does not change anything for the
9868 -- representation of the extension part.)
9870 function Make_CW_Equivalent_Type
9871 (T : Entity_Id;
9872 E : Node_Id) return Entity_Id
9874 Loc : constant Source_Ptr := Sloc (E);
9875 Root_Typ : constant Entity_Id := Root_Type (T);
9876 Root_Utyp : constant Entity_Id := Underlying_Type (Root_Typ);
9877 List_Def : constant List_Id := Empty_List;
9878 Comp_List : constant List_Id := New_List;
9880 Equiv_Type : Entity_Id;
9881 Range_Type : Entity_Id;
9882 Str_Type : Entity_Id;
9883 Constr_Root : Entity_Id;
9884 Size_Attr : Node_Id;
9885 Size_Expr : Node_Id;
9887 begin
9888 -- If the root type is already constrained, there are no discriminants
9889 -- in the expression.
9891 if not Has_Discriminants (Root_Typ)
9892 or else Is_Constrained (Root_Typ)
9893 then
9894 Constr_Root := Root_Typ;
9896 -- At this point in the expansion, nonlimited view of the type
9897 -- must be available, otherwise the error will be reported later.
9899 if From_Limited_With (Constr_Root)
9900 and then Present (Non_Limited_View (Constr_Root))
9901 then
9902 Constr_Root := Non_Limited_View (Constr_Root);
9903 end if;
9905 else
9906 Constr_Root := Make_Temporary (Loc, 'R');
9908 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9910 Append_To (List_Def,
9911 Make_Subtype_Declaration (Loc,
9912 Defining_Identifier => Constr_Root,
9913 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9914 end if;
9916 -- Generate the range subtype declaration
9918 Range_Type := Make_Temporary (Loc, 'G');
9920 -- If the expression is known to have the tag of its type, then we can
9921 -- use it directly for the prefix of the Size attribute; otherwise we
9922 -- need to convert it first to the class-wide type to force a call to
9923 -- the _Size primitive operation.
9925 if Has_Tag_Of_Type (E) then
9926 if not Has_Discriminants (Etype (E))
9927 or else Is_Constrained (Etype (E))
9928 then
9929 Size_Attr :=
9930 Make_Attribute_Reference (Loc,
9931 Prefix => New_Occurrence_Of (Etype (E), Loc),
9932 Attribute_Name => Name_Object_Size);
9934 else
9935 Size_Attr :=
9936 Make_Attribute_Reference (Loc,
9937 Prefix => Duplicate_Subexpr_No_Checks (E),
9938 Attribute_Name => Name_Size);
9939 end if;
9941 else
9942 Size_Attr :=
9943 Make_Attribute_Reference (Loc,
9944 Prefix => OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9945 Attribute_Name => Name_Size);
9946 end if;
9948 if not Is_Interface (Root_Typ) then
9950 -- subtype rg__xx is
9951 -- Storage_Offset range 1 .. (Exp'size - Typ'object_size)
9952 -- / Storage_Unit
9954 Size_Expr :=
9955 Make_Op_Subtract (Loc,
9956 Left_Opnd => Size_Attr,
9957 Right_Opnd =>
9958 Make_Attribute_Reference (Loc,
9959 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9960 Attribute_Name => Name_Object_Size));
9961 else
9962 -- subtype rg__xx is
9963 -- Storage_Offset range 1 .. (Exp'size - Ada.Tags.Tag'object_size)
9964 -- / Storage_Unit
9966 Size_Expr :=
9967 Make_Op_Subtract (Loc,
9968 Left_Opnd => Size_Attr,
9969 Right_Opnd =>
9970 Make_Attribute_Reference (Loc,
9971 Prefix => New_Occurrence_Of (RTE (RE_Tag), Loc),
9972 Attribute_Name => Name_Object_Size));
9973 end if;
9975 Set_Paren_Count (Size_Expr, 1);
9977 Append_To (List_Def,
9978 Make_Subtype_Declaration (Loc,
9979 Defining_Identifier => Range_Type,
9980 Subtype_Indication =>
9981 Make_Subtype_Indication (Loc,
9982 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9983 Constraint => Make_Range_Constraint (Loc,
9984 Range_Expression =>
9985 Make_Range (Loc,
9986 Low_Bound => Make_Integer_Literal (Loc, 1),
9987 High_Bound =>
9988 Make_Op_Divide (Loc,
9989 Left_Opnd => Size_Expr,
9990 Right_Opnd => Make_Integer_Literal (Loc,
9991 Intval => System_Storage_Unit)))))));
9993 -- subtype str__nn is Storage_Array (rg__x);
9995 Str_Type := Make_Temporary (Loc, 'S');
9996 Append_To (List_Def,
9997 Make_Subtype_Declaration (Loc,
9998 Defining_Identifier => Str_Type,
9999 Subtype_Indication =>
10000 Make_Subtype_Indication (Loc,
10001 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
10002 Constraint =>
10003 Make_Index_Or_Discriminant_Constraint (Loc,
10004 Constraints =>
10005 New_List (New_Occurrence_Of (Range_Type, Loc))))));
10007 -- type Equiv_T is record
10008 -- _Parent : Snn; -- not interface
10009 -- _Tag : Ada.Tags.Tag -- interface
10010 -- Cnn : Str_Type;
10011 -- end Equiv_T;
10013 Equiv_Type := Make_Temporary (Loc, 'T');
10014 Mutate_Ekind (Equiv_Type, E_Record_Type);
10016 if not Is_Interface (Root_Typ) then
10017 Set_Parent_Subtype (Equiv_Type, Constr_Root);
10018 end if;
10020 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
10021 -- treatment for this type. In particular, even though _parent's type
10022 -- is a controlled type or contains controlled components, we do not
10023 -- want to set Has_Controlled_Component on it to avoid making it gain
10024 -- an unwanted _controller component.
10026 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
10028 -- A class-wide equivalent type does not require initialization
10030 Set_Suppress_Initialization (Equiv_Type);
10032 if not Is_Interface (Root_Typ) then
10033 Append_To (Comp_List,
10034 Make_Component_Declaration (Loc,
10035 Defining_Identifier =>
10036 Make_Defining_Identifier (Loc, Name_uParent),
10037 Component_Definition =>
10038 Make_Component_Definition (Loc,
10039 Aliased_Present => False,
10040 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
10042 Set_Reverse_Storage_Order
10043 (Equiv_Type, Reverse_Storage_Order (Base_Type (Root_Utyp)));
10044 Set_Reverse_Bit_Order
10045 (Equiv_Type, Reverse_Bit_Order (Base_Type (Root_Utyp)));
10047 else
10048 Append_To (Comp_List,
10049 Make_Component_Declaration (Loc,
10050 Defining_Identifier =>
10051 Make_Defining_Identifier (Loc, Name_uTag),
10052 Component_Definition =>
10053 Make_Component_Definition (Loc,
10054 Aliased_Present => False,
10055 Subtype_Indication =>
10056 New_Occurrence_Of (RTE (RE_Tag), Loc))));
10057 end if;
10059 Append_To (Comp_List,
10060 Make_Component_Declaration (Loc,
10061 Defining_Identifier => Make_Temporary (Loc, 'C'),
10062 Component_Definition =>
10063 Make_Component_Definition (Loc,
10064 Aliased_Present => False,
10065 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
10067 Append_To (List_Def,
10068 Make_Full_Type_Declaration (Loc,
10069 Defining_Identifier => Equiv_Type,
10070 Type_Definition =>
10071 Make_Record_Definition (Loc,
10072 Component_List =>
10073 Make_Component_List (Loc,
10074 Component_Items => Comp_List,
10075 Variant_Part => Empty))));
10077 -- Suppress all checks during the analysis of the expanded code to avoid
10078 -- the generation of spurious warnings under ZFP run-time.
10080 Insert_Actions (E, List_Def, Suppress => All_Checks);
10082 -- In the case of an interface type mark the tag for First_Tag_Component
10084 if Is_Interface (Root_Typ) then
10085 Set_Is_Tag (First_Entity (Equiv_Type));
10086 end if;
10088 return Equiv_Type;
10089 end Make_CW_Equivalent_Type;
10091 -------------------------
10092 -- Make_Invariant_Call --
10093 -------------------------
10095 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
10096 Loc : constant Source_Ptr := Sloc (Expr);
10097 Typ : constant Entity_Id := Base_Type (Etype (Expr));
10098 pragma Assert (Has_Invariants (Typ));
10099 Proc_Id : constant Entity_Id := Invariant_Procedure (Typ);
10100 pragma Assert (Present (Proc_Id));
10101 Inv_Typ : constant Entity_Id
10102 := Base_Type (Etype (First_Formal (Proc_Id)));
10104 Arg : Node_Id;
10106 begin
10107 -- The invariant procedure has a null body if assertions are disabled or
10108 -- Assertion_Policy Ignore is in effect. In that case, generate a null
10109 -- statement instead of a call to the invariant procedure.
10111 if Has_Null_Body (Proc_Id) then
10112 return Make_Null_Statement (Loc);
10114 else
10115 -- As done elsewhere, for example in Build_Initialization_Call, we
10116 -- may need to bridge the gap between views of the type.
10118 if Inv_Typ /= Typ then
10119 Arg := OK_Convert_To (Inv_Typ, Expr);
10120 else
10121 Arg := Relocate_Node (Expr);
10122 end if;
10124 return
10125 Make_Procedure_Call_Statement (Loc,
10126 Name => New_Occurrence_Of (Proc_Id, Loc),
10127 Parameter_Associations => New_List (Arg));
10128 end if;
10129 end Make_Invariant_Call;
10131 ------------------------
10132 -- Make_Literal_Range --
10133 ------------------------
10135 function Make_Literal_Range
10136 (Loc : Source_Ptr;
10137 Literal_Typ : Entity_Id) return Node_Id
10139 Lo : constant Node_Id :=
10140 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
10141 Index : constant Entity_Id := Etype (Lo);
10142 Length_Expr : constant Node_Id :=
10143 Make_Op_Subtract (Loc,
10144 Left_Opnd =>
10145 Make_Integer_Literal (Loc,
10146 Intval => String_Literal_Length (Literal_Typ)),
10147 Right_Opnd => Make_Integer_Literal (Loc, 1));
10149 Hi : Node_Id;
10151 begin
10152 Set_Analyzed (Lo, False);
10154 if Is_Integer_Type (Index) then
10155 Hi :=
10156 Make_Op_Add (Loc,
10157 Left_Opnd => New_Copy_Tree (Lo),
10158 Right_Opnd => Length_Expr);
10159 else
10160 Hi :=
10161 Make_Attribute_Reference (Loc,
10162 Attribute_Name => Name_Val,
10163 Prefix => New_Occurrence_Of (Index, Loc),
10164 Expressions => New_List (
10165 Make_Op_Add (Loc,
10166 Left_Opnd =>
10167 Make_Attribute_Reference (Loc,
10168 Attribute_Name => Name_Pos,
10169 Prefix => New_Occurrence_Of (Index, Loc),
10170 Expressions => New_List (New_Copy_Tree (Lo))),
10171 Right_Opnd => Length_Expr)));
10172 end if;
10174 return
10175 Make_Range (Loc,
10176 Low_Bound => Lo,
10177 High_Bound => Hi);
10178 end Make_Literal_Range;
10180 --------------------------
10181 -- Make_Non_Empty_Check --
10182 --------------------------
10184 function Make_Non_Empty_Check
10185 (Loc : Source_Ptr;
10186 N : Node_Id) return Node_Id
10188 begin
10189 return
10190 Make_Op_Ne (Loc,
10191 Left_Opnd =>
10192 Make_Attribute_Reference (Loc,
10193 Attribute_Name => Name_Length,
10194 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
10195 Right_Opnd =>
10196 Make_Integer_Literal (Loc, 0));
10197 end Make_Non_Empty_Check;
10199 -------------------------
10200 -- Make_Predicate_Call --
10201 -------------------------
10203 -- WARNING: This routine manages Ghost regions. Return statements must be
10204 -- replaced by gotos which jump to the end of the routine and restore the
10205 -- Ghost mode.
10207 function Make_Predicate_Call
10208 (Typ : Entity_Id;
10209 Expr : Node_Id;
10210 Static_Mem : Boolean := False;
10211 Dynamic_Mem : Node_Id := Empty) return Node_Id
10213 Loc : constant Source_Ptr := Sloc (Expr);
10215 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10216 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10217 -- Save the Ghost-related attributes to restore on exit
10219 Call : Node_Id;
10220 Func_Id : Entity_Id;
10221 Param_Assocs : List_Id;
10222 begin
10223 Func_Id := Predicate_Function (Typ);
10224 pragma Assert (Present (Func_Id));
10226 -- The related type may be subject to pragma Ghost. Set the mode now to
10227 -- ensure that the call is properly marked as Ghost.
10229 Set_Ghost_Mode (Typ);
10231 -- Case of calling normal predicate function
10233 -- If the type is tagged, the expression may be class-wide, in which
10234 -- case it has to be converted to its root type, given that the
10235 -- generated predicate function is not dispatching. The conversion is
10236 -- type-safe and does not need validation, which matters when private
10237 -- extensions are involved.
10239 if Is_Tagged_Type (Typ) then
10240 Param_Assocs := New_List (OK_Convert_To (Typ, Relocate_Node (Expr)));
10241 else
10242 Param_Assocs := New_List (Relocate_Node (Expr));
10243 end if;
10245 if Predicate_Function_Needs_Membership_Parameter (Typ) then
10246 -- Pass in parameter indicating whether this call is for a
10247 -- membership test.
10248 Append ((if Present (Dynamic_Mem)
10249 then Dynamic_Mem
10250 else New_Occurrence_Of
10251 (Boolean_Literals (Static_Mem), Loc)),
10252 Param_Assocs);
10253 end if;
10255 Call :=
10256 Make_Function_Call (Loc,
10257 Name => New_Occurrence_Of (Func_Id, Loc),
10258 Parameter_Associations => Param_Assocs);
10260 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10262 return Call;
10263 end Make_Predicate_Call;
10265 --------------------------
10266 -- Make_Predicate_Check --
10267 --------------------------
10269 function Make_Predicate_Check
10270 (Typ : Entity_Id;
10271 Expr : Node_Id) return Node_Id
10273 Loc : constant Source_Ptr := Sloc (Expr);
10275 -- Local variables
10277 Args : List_Id;
10278 Nam : Name_Id;
10280 -- Start of processing for Make_Predicate_Check
10282 begin
10283 -- If predicate checks are suppressed, then return a null statement. For
10284 -- this call, we check only the scope setting. If the caller wants to
10285 -- check a specific entity's setting, they must do it manually.
10287 if Predicate_Checks_Suppressed (Empty) then
10288 return Make_Null_Statement (Loc);
10289 end if;
10291 -- Do not generate a check within stream functions and the like.
10293 if not Predicate_Check_In_Scope (Expr) then
10294 return Make_Null_Statement (Loc);
10295 end if;
10297 -- Compute proper name to use, we need to get this right so that the
10298 -- right set of check policies apply to the Check pragma we are making.
10299 -- The presence or not of a Ghost_Predicate does not influence the
10300 -- choice of the applicable check policy.
10302 if Has_Dynamic_Predicate_Aspect (Typ) then
10303 Nam := Name_Dynamic_Predicate;
10304 elsif Has_Static_Predicate_Aspect (Typ) then
10305 Nam := Name_Static_Predicate;
10306 else
10307 Nam := Name_Predicate;
10308 end if;
10310 Args := New_List (
10311 Make_Pragma_Argument_Association (Loc,
10312 Expression => Make_Identifier (Loc, Nam)),
10313 Make_Pragma_Argument_Association (Loc,
10314 Expression => Make_Predicate_Call (Typ, Expr)));
10316 -- If the subtype is subject to pragma Predicate_Failure, add the
10317 -- failure expression as an additional parameter.
10319 return
10320 Make_Pragma (Loc,
10321 Chars => Name_Check,
10322 Pragma_Argument_Associations => Args);
10323 end Make_Predicate_Check;
10325 ----------------------------
10326 -- Make_Subtype_From_Expr --
10327 ----------------------------
10329 -- 1. If Expr is an unconstrained array expression, creates
10330 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
10332 -- 2. If Expr is a unconstrained discriminated type expression, creates
10333 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
10335 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
10337 function Make_Subtype_From_Expr
10338 (E : Node_Id;
10339 Unc_Typ : Entity_Id;
10340 Related_Id : Entity_Id := Empty) return Node_Id
10342 List_Constr : constant List_Id := New_List;
10343 Loc : constant Source_Ptr := Sloc (E);
10344 D : Entity_Id;
10345 Full_Exp : Node_Id;
10346 Full_Subtyp : Entity_Id;
10347 High_Bound : Entity_Id;
10348 Index_Typ : Entity_Id;
10349 Low_Bound : Entity_Id;
10350 Priv_Subtyp : Entity_Id;
10351 Utyp : Entity_Id;
10353 begin
10354 if Is_Private_Type (Unc_Typ)
10355 and then Has_Unknown_Discriminants (Unc_Typ)
10356 then
10357 -- The caller requests a unique external name for both the private
10358 -- and the full subtype.
10360 if Present (Related_Id) then
10361 Full_Subtyp :=
10362 Make_Defining_Identifier (Loc,
10363 Chars => New_External_Name (Chars (Related_Id), 'C'));
10364 Priv_Subtyp :=
10365 Make_Defining_Identifier (Loc,
10366 Chars => New_External_Name (Chars (Related_Id), 'P'));
10368 else
10369 Full_Subtyp := Make_Temporary (Loc, 'C');
10370 Priv_Subtyp := Make_Temporary (Loc, 'P');
10371 end if;
10373 -- Prepare the subtype completion. Use the base type to find the
10374 -- underlying type because the type may be a generic actual or an
10375 -- explicit subtype.
10377 Utyp := Underlying_Type (Base_Type (Unc_Typ));
10379 Full_Exp :=
10380 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
10381 Set_Parent (Full_Exp, Parent (E));
10383 Insert_Action (E,
10384 Make_Subtype_Declaration (Loc,
10385 Defining_Identifier => Full_Subtyp,
10386 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
10388 -- Define the dummy private subtype
10390 Mutate_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
10391 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
10392 Set_Scope (Priv_Subtyp, Full_Subtyp);
10393 Set_Is_Constrained (Priv_Subtyp);
10394 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
10395 Set_Is_Itype (Priv_Subtyp);
10396 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
10398 if Is_Tagged_Type (Priv_Subtyp) then
10399 Set_Class_Wide_Type
10400 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
10401 Set_Direct_Primitive_Operations (Priv_Subtyp,
10402 Direct_Primitive_Operations (Unc_Typ));
10403 end if;
10405 Set_Full_View (Priv_Subtyp, Full_Subtyp);
10407 return New_Occurrence_Of (Priv_Subtyp, Loc);
10409 elsif Is_Array_Type (Unc_Typ) then
10410 Index_Typ := First_Index (Unc_Typ);
10411 for J in 1 .. Number_Dimensions (Unc_Typ) loop
10413 -- Capture the bounds of each index constraint in case the context
10414 -- is an object declaration of an unconstrained type initialized
10415 -- by a function call:
10417 -- Obj : Unconstr_Typ := Func_Call;
10419 -- This scenario requires secondary scope management and the index
10420 -- constraint cannot depend on the temporary used to capture the
10421 -- result of the function call.
10423 -- SS_Mark;
10424 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
10425 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
10426 -- Obj : S := Temp.all;
10427 -- SS_Release; -- Temp is gone at this point, bounds of S are
10428 -- -- non existent.
10430 -- Generate:
10431 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
10433 Low_Bound := Make_Temporary (Loc, 'B');
10434 Insert_Action (E,
10435 Make_Object_Declaration (Loc,
10436 Defining_Identifier => Low_Bound,
10437 Object_Definition =>
10438 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10439 Constant_Present => True,
10440 Expression =>
10441 Make_Attribute_Reference (Loc,
10442 Prefix => Duplicate_Subexpr_No_Checks (E),
10443 Attribute_Name => Name_First,
10444 Expressions => New_List (
10445 Make_Integer_Literal (Loc, J)))));
10447 -- Generate:
10448 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
10450 High_Bound := Make_Temporary (Loc, 'B');
10451 Insert_Action (E,
10452 Make_Object_Declaration (Loc,
10453 Defining_Identifier => High_Bound,
10454 Object_Definition =>
10455 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10456 Constant_Present => True,
10457 Expression =>
10458 Make_Attribute_Reference (Loc,
10459 Prefix => Duplicate_Subexpr_No_Checks (E),
10460 Attribute_Name => Name_Last,
10461 Expressions => New_List (
10462 Make_Integer_Literal (Loc, J)))));
10464 Append_To (List_Constr,
10465 Make_Range (Loc,
10466 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
10467 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
10469 Next_Index (Index_Typ);
10470 end loop;
10472 elsif Is_Class_Wide_Type (Unc_Typ) then
10473 declare
10474 CW_Subtype : constant Entity_Id :=
10475 New_Class_Wide_Subtype (Unc_Typ, E);
10477 begin
10478 -- A class-wide equivalent type is not needed on VM targets
10479 -- because the VM back-ends handle the class-wide object
10480 -- initialization itself (and doesn't need or want the
10481 -- additional intermediate type to handle the assignment).
10483 if Expander_Active and then Tagged_Type_Expansion then
10485 -- If this is the class-wide type of a completion that is a
10486 -- record subtype, set the type of the class-wide type to be
10487 -- the full base type, for use in the expanded code for the
10488 -- equivalent type. Should this be done earlier when the
10489 -- completion is analyzed ???
10491 if Is_Private_Type (Etype (Unc_Typ))
10492 and then
10493 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
10494 then
10495 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
10496 end if;
10498 Set_Equivalent_Type
10499 (CW_Subtype, Make_CW_Equivalent_Type (Unc_Typ, E));
10500 end if;
10502 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
10504 return New_Occurrence_Of (CW_Subtype, Loc);
10505 end;
10507 -- Indefinite record type with discriminants
10509 else
10510 D := First_Discriminant (Unc_Typ);
10511 while Present (D) loop
10512 Append_To (List_Constr,
10513 Make_Selected_Component (Loc,
10514 Prefix => Duplicate_Subexpr_No_Checks (E),
10515 Selector_Name => New_Occurrence_Of (D, Loc)));
10517 Next_Discriminant (D);
10518 end loop;
10519 end if;
10521 return
10522 Make_Subtype_Indication (Loc,
10523 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
10524 Constraint =>
10525 Make_Index_Or_Discriminant_Constraint (Loc,
10526 Constraints => List_Constr));
10527 end Make_Subtype_From_Expr;
10529 -----------------------------------
10530 -- Make_Tag_Assignment_From_Type --
10531 -----------------------------------
10533 function Make_Tag_Assignment_From_Type
10534 (Loc : Source_Ptr;
10535 Target : Node_Id;
10536 Typ : Entity_Id) return Node_Id
10538 Nam : constant Node_Id :=
10539 Make_Selected_Component (Loc,
10540 Prefix => Target,
10541 Selector_Name =>
10542 New_Occurrence_Of (First_Tag_Component (Typ), Loc));
10544 begin
10545 Set_Assignment_OK (Nam);
10547 return
10548 Make_Assignment_Statement (Loc,
10549 Name => Nam,
10550 Expression =>
10551 Unchecked_Convert_To (RTE (RE_Tag),
10552 New_Occurrence_Of
10553 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)));
10554 end Make_Tag_Assignment_From_Type;
10556 -----------------------------
10557 -- Make_Variant_Comparison --
10558 -----------------------------
10560 function Make_Variant_Comparison
10561 (Loc : Source_Ptr;
10562 Typ : Entity_Id;
10563 Mode : Name_Id;
10564 Curr_Val : Node_Id;
10565 Old_Val : Node_Id) return Node_Id
10567 function Big_Integer_Lt return Entity_Id;
10568 -- Returns the entity of the predefined "<" function from
10569 -- Ada.Numerics.Big_Numbers.Big_Integers.
10571 --------------------
10572 -- Big_Integer_Lt --
10573 --------------------
10575 function Big_Integer_Lt return Entity_Id is
10576 Big_Integers : constant Entity_Id :=
10577 RTU_Entity (Ada_Numerics_Big_Numbers_Big_Integers);
10579 E : Entity_Id := First_Entity (Big_Integers);
10581 begin
10582 while Present (E) loop
10583 if Chars (E) = Name_Op_Lt then
10584 return E;
10585 end if;
10586 Next_Entity (E);
10587 end loop;
10589 raise Program_Error;
10590 end Big_Integer_Lt;
10592 -- Start of processing for Make_Variant_Comparison
10594 begin
10595 if Mode = Name_Increases then
10596 return Make_Op_Gt (Loc, Curr_Val, Old_Val);
10598 else pragma Assert (Mode = Name_Decreases);
10600 -- For discrete expressions use the "<" operator
10602 if Is_Discrete_Type (Typ) then
10603 return Make_Op_Lt (Loc, Curr_Val, Old_Val);
10605 -- For Big_Integer expressions use the "<" function, because the
10606 -- operator on private type might not be visible and won't be
10607 -- resolved.
10609 else pragma Assert (Is_RTE (Base_Type (Typ), RE_Big_Integer));
10610 return
10611 Make_Function_Call (Loc,
10612 Name =>
10613 New_Occurrence_Of (Big_Integer_Lt, Loc),
10614 Parameter_Associations =>
10615 New_List (Curr_Val, Old_Val));
10616 end if;
10617 end if;
10618 end Make_Variant_Comparison;
10620 -----------------
10621 -- Map_Formals --
10622 -----------------
10624 procedure Map_Formals
10625 (Parent_Subp : Entity_Id;
10626 Derived_Subp : Entity_Id;
10627 Force_Update : Boolean := False)
10629 Par_Formal : Entity_Id := First_Formal (Parent_Subp);
10630 Subp_Formal : Entity_Id := First_Formal (Derived_Subp);
10632 begin
10633 if Force_Update then
10634 Type_Map.Set (Parent_Subp, Derived_Subp);
10635 end if;
10637 -- At this stage either we are under regular processing and the caller
10638 -- has previously ensured that these primitives are already mapped (by
10639 -- means of calling previously to Update_Primitives_Mapping), or we are
10640 -- processing a late-overriding primitive and Force_Update updated above
10641 -- the mapping of these primitives.
10643 while Present (Par_Formal) and then Present (Subp_Formal) loop
10644 Type_Map.Set (Par_Formal, Subp_Formal);
10645 Next_Formal (Par_Formal);
10646 Next_Formal (Subp_Formal);
10647 end loop;
10648 end Map_Formals;
10650 ---------------
10651 -- Map_Types --
10652 ---------------
10654 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
10656 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
10657 -- avoid deep indentation of code.
10659 -- NOTE: Routines which deal with discriminant mapping operate on the
10660 -- [underlying/record] full view of various types because those views
10661 -- contain all discriminants and stored constraints.
10663 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
10664 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
10665 -- overriding chain starting from Prim whose dispatching type is parent
10666 -- type Par_Typ and add a mapping between the result and primitive Prim.
10668 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
10669 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
10670 -- the inheritance or overriding chain of subprogram Subp. Return Empty
10671 -- if no such primitive is available.
10673 function Build_Chain
10674 (Par_Typ : Entity_Id;
10675 Deriv_Typ : Entity_Id) return Elist_Id;
10676 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
10677 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
10678 -- list has the form:
10680 -- head tail
10681 -- v v
10682 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
10684 -- Note that Par_Typ is not part of the resulting derivation chain
10686 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
10687 -- Return the view of type Typ which could potentially contains either
10688 -- the discriminants or stored constraints of the type.
10690 function Find_Discriminant_Value
10691 (Discr : Entity_Id;
10692 Par_Typ : Entity_Id;
10693 Deriv_Typ : Entity_Id;
10694 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
10695 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
10696 -- in the derivation chain starting from parent type Par_Typ leading to
10697 -- derived type Deriv_Typ. The returned value is one of the following:
10699 -- * An entity which is either a discriminant or a nondiscriminant
10700 -- name, and renames/constraints Discr.
10702 -- * An expression which constraints Discr
10704 -- Typ_Elmt is an element of the derivation chain created by routine
10705 -- Build_Chain and denotes the current ancestor being examined.
10707 procedure Map_Discriminants
10708 (Par_Typ : Entity_Id;
10709 Deriv_Typ : Entity_Id);
10710 -- Map each discriminant of type Par_Typ to a meaningful constraint
10711 -- from the point of view of type Deriv_Typ.
10713 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
10714 -- Map each primitive of type Par_Typ to a corresponding primitive of
10715 -- type Deriv_Typ.
10717 -------------------
10718 -- Add_Primitive --
10719 -------------------
10721 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
10722 Par_Prim : Entity_Id;
10724 begin
10725 -- Inspect the inheritance chain through the Alias attribute and the
10726 -- overriding chain through the Overridden_Operation looking for an
10727 -- ancestor primitive with the appropriate dispatching type.
10729 Par_Prim := Prim;
10730 while Present (Par_Prim) loop
10731 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
10732 Par_Prim := Ancestor_Primitive (Par_Prim);
10733 end loop;
10735 -- Create a mapping of the form:
10737 -- parent type primitive -> derived type primitive
10739 if Present (Par_Prim) then
10740 Type_Map.Set (Par_Prim, Prim);
10741 end if;
10742 end Add_Primitive;
10744 ------------------------
10745 -- Ancestor_Primitive --
10746 ------------------------
10748 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
10749 Inher_Prim : constant Entity_Id := Alias (Subp);
10750 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
10752 begin
10753 -- The current subprogram overrides an ancestor primitive
10755 if Present (Over_Prim) then
10756 return Over_Prim;
10758 -- The current subprogram is an internally generated alias of an
10759 -- inherited ancestor primitive.
10761 elsif Present (Inher_Prim) then
10762 -- It is possible that an internally generated alias could be
10763 -- set to a subprogram which overrides the same aliased primitive,
10764 -- so return Empty in this case.
10766 if Ancestor_Primitive (Inher_Prim) = Subp then
10767 return Empty;
10768 end if;
10770 return Inher_Prim;
10772 -- Otherwise the current subprogram is the root of the inheritance or
10773 -- overriding chain.
10775 else
10776 return Empty;
10777 end if;
10778 end Ancestor_Primitive;
10780 -----------------
10781 -- Build_Chain --
10782 -----------------
10784 function Build_Chain
10785 (Par_Typ : Entity_Id;
10786 Deriv_Typ : Entity_Id) return Elist_Id
10788 Anc_Typ : Entity_Id;
10789 Chain : Elist_Id;
10790 Curr_Typ : Entity_Id;
10792 begin
10793 Chain := New_Elmt_List;
10795 -- Add the derived type to the derivation chain
10797 Prepend_Elmt (Deriv_Typ, Chain);
10799 -- Examine all ancestors starting from the derived type climbing
10800 -- towards parent type Par_Typ.
10802 Curr_Typ := Deriv_Typ;
10803 loop
10804 -- Handle the case where the current type is a record which
10805 -- derives from a subtype.
10807 -- subtype Sub_Typ is Par_Typ ...
10808 -- type Deriv_Typ is Sub_Typ ...
10810 if Ekind (Curr_Typ) = E_Record_Type
10811 and then Present (Parent_Subtype (Curr_Typ))
10812 then
10813 Anc_Typ := Parent_Subtype (Curr_Typ);
10815 -- Handle the case where the current type is a record subtype of
10816 -- another subtype.
10818 -- subtype Sub_Typ1 is Par_Typ ...
10819 -- subtype Sub_Typ2 is Sub_Typ1 ...
10821 elsif Ekind (Curr_Typ) = E_Record_Subtype
10822 and then Present (Cloned_Subtype (Curr_Typ))
10823 then
10824 Anc_Typ := Cloned_Subtype (Curr_Typ);
10826 -- Otherwise use the direct parent type
10828 else
10829 Anc_Typ := Etype (Curr_Typ);
10830 end if;
10832 -- Use the first subtype when dealing with itypes
10834 if Is_Itype (Anc_Typ) then
10835 Anc_Typ := First_Subtype (Anc_Typ);
10836 end if;
10838 -- Work with the view which contains the discriminants and stored
10839 -- constraints.
10841 Anc_Typ := Discriminated_View (Anc_Typ);
10843 -- Stop the climb when either the parent type has been reached or
10844 -- there are no more ancestors left to examine.
10846 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
10848 Prepend_Unique_Elmt (Anc_Typ, Chain);
10849 Curr_Typ := Anc_Typ;
10850 end loop;
10852 return Chain;
10853 end Build_Chain;
10855 ------------------------
10856 -- Discriminated_View --
10857 ------------------------
10859 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
10860 T : Entity_Id;
10862 begin
10863 T := Typ;
10865 -- Use the [underlying] full view when dealing with private types
10866 -- because the view contains all inherited discriminants or stored
10867 -- constraints.
10869 if Is_Private_Type (T) then
10870 if Present (Underlying_Full_View (T)) then
10871 T := Underlying_Full_View (T);
10873 elsif Present (Full_View (T)) then
10874 T := Full_View (T);
10875 end if;
10876 end if;
10878 -- Use the underlying record view when the type is an extenstion of
10879 -- a parent type with unknown discriminants because the view contains
10880 -- all inherited discriminants or stored constraints.
10882 if Ekind (T) = E_Record_Type
10883 and then Present (Underlying_Record_View (T))
10884 then
10885 T := Underlying_Record_View (T);
10886 end if;
10888 return T;
10889 end Discriminated_View;
10891 -----------------------------
10892 -- Find_Discriminant_Value --
10893 -----------------------------
10895 function Find_Discriminant_Value
10896 (Discr : Entity_Id;
10897 Par_Typ : Entity_Id;
10898 Deriv_Typ : Entity_Id;
10899 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
10901 Discr_Pos : constant Uint := Discriminant_Number (Discr);
10902 Typ : constant Entity_Id := Node (Typ_Elmt);
10904 function Find_Constraint_Value
10905 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
10906 -- Given constraint Constr, find what it denotes. This is either:
10908 -- * An entity which is either a discriminant or a name
10910 -- * An expression
10912 ---------------------------
10913 -- Find_Constraint_Value --
10914 ---------------------------
10916 function Find_Constraint_Value
10917 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
10919 begin
10920 if Nkind (Constr) in N_Entity then
10922 -- The constraint denotes a discriminant of the curren type
10923 -- which renames the ancestor discriminant:
10925 -- vv
10926 -- type Typ (D1 : ...; DN : ...) is
10927 -- new Anc (Discr => D1) with ...
10928 -- ^^
10930 if Ekind (Constr) = E_Discriminant then
10932 -- The discriminant belongs to derived type Deriv_Typ. This
10933 -- is the final value for the ancestor discriminant as the
10934 -- derivations chain has been fully exhausted.
10936 if Typ = Deriv_Typ then
10937 return Constr;
10939 -- Otherwise the discriminant may be renamed or constrained
10940 -- at a lower level. Continue looking down the derivation
10941 -- chain.
10943 else
10944 return
10945 Find_Discriminant_Value
10946 (Discr => Constr,
10947 Par_Typ => Par_Typ,
10948 Deriv_Typ => Deriv_Typ,
10949 Typ_Elmt => Next_Elmt (Typ_Elmt));
10950 end if;
10952 -- Otherwise the constraint denotes a reference to some name
10953 -- which results in a Stored discriminant:
10955 -- vvvv
10956 -- Name : ...;
10957 -- type Typ (D1 : ...; DN : ...) is
10958 -- new Anc (Discr => Name) with ...
10959 -- ^^^^
10961 -- Return the name as this is the proper constraint of the
10962 -- discriminant.
10964 else
10965 return Constr;
10966 end if;
10968 -- The constraint denotes a reference to a name
10970 elsif Is_Entity_Name (Constr) then
10971 return Find_Constraint_Value (Entity (Constr));
10973 -- Otherwise the current constraint is an expression which yields
10974 -- a Stored discriminant:
10976 -- type Typ (D1 : ...; DN : ...) is
10977 -- new Anc (Discr => <expression>) with ...
10978 -- ^^^^^^^^^^
10980 -- Return the expression as this is the proper constraint of the
10981 -- discriminant.
10983 else
10984 return Constr;
10985 end if;
10986 end Find_Constraint_Value;
10988 -- Local variables
10990 Constrs : constant Elist_Id := Stored_Constraint (Typ);
10992 Constr_Elmt : Elmt_Id;
10993 Pos : Uint;
10994 Typ_Discr : Entity_Id;
10996 -- Start of processing for Find_Discriminant_Value
10998 begin
10999 -- The algorithm for finding the value of a discriminant works as
11000 -- follows. First, it recreates the derivation chain from Par_Typ
11001 -- to Deriv_Typ as a list:
11003 -- Par_Typ (shown for completeness)
11004 -- v
11005 -- Ancestor_N <-- head of chain
11006 -- v
11007 -- Ancestor_1
11008 -- v
11009 -- Deriv_Typ <-- tail of chain
11011 -- The algorithm then traces the fate of a parent discriminant down
11012 -- the derivation chain. At each derivation level, the discriminant
11013 -- may be either inherited or constrained.
11015 -- 1) Discriminant is inherited: there are two cases, depending on
11016 -- which type is inheriting.
11018 -- 1.1) Deriv_Typ is inheriting:
11020 -- type Ancestor (D_1 : ...) is tagged ...
11021 -- type Deriv_Typ is new Ancestor ...
11023 -- In this case the inherited discriminant is the final value of
11024 -- the parent discriminant because the end of the derivation chain
11025 -- has been reached.
11027 -- 1.2) Some other type is inheriting:
11029 -- type Ancestor_1 (D_1 : ...) is tagged ...
11030 -- type Ancestor_2 is new Ancestor_1 ...
11032 -- In this case the algorithm continues to trace the fate of the
11033 -- inherited discriminant down the derivation chain because it may
11034 -- be further inherited or constrained.
11036 -- 2) Discriminant is constrained: there are three cases, depending
11037 -- on what the constraint is.
11039 -- 2.1) The constraint is another discriminant (aka renaming):
11041 -- type Ancestor_1 (D_1 : ...) is tagged ...
11042 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
11044 -- In this case the constraining discriminant becomes the one to
11045 -- track down the derivation chain. The algorithm already knows
11046 -- that D_2 constrains D_1, therefore if the algorithm finds the
11047 -- value of D_2, then this would also be the value for D_1.
11049 -- 2.2) The constraint is a name (aka Stored):
11051 -- Name : ...
11052 -- type Ancestor_1 (D_1 : ...) is tagged ...
11053 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
11055 -- In this case the name is the final value of D_1 because the
11056 -- discriminant cannot be further constrained.
11058 -- 2.3) The constraint is an expression (aka Stored):
11060 -- type Ancestor_1 (D_1 : ...) is tagged ...
11061 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
11063 -- Similar to 2.2, the expression is the final value of D_1
11065 Pos := Uint_1;
11067 -- When a derived type constrains its parent type, all constaints
11068 -- appear in the Stored_Constraint list. Examine the list looking
11069 -- for a positional match.
11071 if Present (Constrs) then
11072 Constr_Elmt := First_Elmt (Constrs);
11073 while Present (Constr_Elmt) loop
11075 -- The position of the current constraint matches that of the
11076 -- ancestor discriminant.
11078 if Pos = Discr_Pos then
11079 return Find_Constraint_Value (Node (Constr_Elmt));
11080 end if;
11082 Next_Elmt (Constr_Elmt);
11083 Pos := Pos + 1;
11084 end loop;
11086 -- Otherwise the derived type does not constraint its parent type in
11087 -- which case it inherits the parent discriminants.
11089 else
11090 Typ_Discr := First_Discriminant (Typ);
11091 while Present (Typ_Discr) loop
11093 -- The position of the current discriminant matches that of the
11094 -- ancestor discriminant.
11096 if Pos = Discr_Pos then
11097 return Find_Constraint_Value (Typ_Discr);
11098 end if;
11100 Next_Discriminant (Typ_Discr);
11101 Pos := Pos + 1;
11102 end loop;
11103 end if;
11105 -- A discriminant must always have a corresponding value. This is
11106 -- either another discriminant, a name, or an expression. If this
11107 -- point is reached, them most likely the derivation chain employs
11108 -- the wrong views of types.
11110 pragma Assert (False);
11112 return Empty;
11113 end Find_Discriminant_Value;
11115 -----------------------
11116 -- Map_Discriminants --
11117 -----------------------
11119 procedure Map_Discriminants
11120 (Par_Typ : Entity_Id;
11121 Deriv_Typ : Entity_Id)
11123 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
11125 Discr : Entity_Id;
11126 Discr_Val : Node_Or_Entity_Id;
11128 begin
11129 -- Examine each discriminant of parent type Par_Typ and find a
11130 -- suitable value for it from the point of view of derived type
11131 -- Deriv_Typ.
11133 if Has_Discriminants (Par_Typ) then
11134 Discr := First_Discriminant (Par_Typ);
11135 while Present (Discr) loop
11136 Discr_Val :=
11137 Find_Discriminant_Value
11138 (Discr => Discr,
11139 Par_Typ => Par_Typ,
11140 Deriv_Typ => Deriv_Typ,
11141 Typ_Elmt => First_Elmt (Deriv_Chain));
11143 -- Create a mapping of the form:
11145 -- parent type discriminant -> value
11147 Type_Map.Set (Discr, Discr_Val);
11149 Next_Discriminant (Discr);
11150 end loop;
11151 end if;
11152 end Map_Discriminants;
11154 --------------------
11155 -- Map_Primitives --
11156 --------------------
11158 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
11159 Deriv_Prim : Entity_Id;
11160 Par_Prim : Entity_Id;
11161 Par_Prims : Elist_Id;
11162 Prim_Elmt : Elmt_Id;
11164 begin
11165 -- Inspect the primitives of the derived type and determine whether
11166 -- they relate to the primitives of the parent type. If there is a
11167 -- meaningful relation, create a mapping of the form:
11169 -- parent type primitive -> derived type primitive
11171 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
11172 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
11173 while Present (Prim_Elmt) loop
11174 Deriv_Prim := Node (Prim_Elmt);
11176 if Is_Subprogram (Deriv_Prim)
11177 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
11178 then
11179 Add_Primitive (Deriv_Prim, Par_Typ);
11180 end if;
11182 Next_Elmt (Prim_Elmt);
11183 end loop;
11184 end if;
11186 -- If the parent operation is an interface operation, the overriding
11187 -- indicator is not present. Instead, we get from the interface
11188 -- operation the primitive of the current type that implements it.
11190 if Is_Interface (Par_Typ) then
11191 Par_Prims := Collect_Primitive_Operations (Par_Typ);
11193 if Present (Par_Prims) then
11194 Prim_Elmt := First_Elmt (Par_Prims);
11196 while Present (Prim_Elmt) loop
11197 Par_Prim := Node (Prim_Elmt);
11198 Deriv_Prim :=
11199 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
11201 if Present (Deriv_Prim) then
11202 Type_Map.Set (Par_Prim, Deriv_Prim);
11203 end if;
11205 Next_Elmt (Prim_Elmt);
11206 end loop;
11207 end if;
11208 end if;
11209 end Map_Primitives;
11211 -- Start of processing for Map_Types
11213 begin
11214 -- Nothing to do if there are no types to work with
11216 if No (Parent_Type) or else No (Derived_Type) then
11217 return;
11219 -- Nothing to do if the mapping already exists
11221 elsif Type_Map.Get (Parent_Type) = Derived_Type then
11222 return;
11224 -- Nothing to do if both types are not tagged. Note that untagged types
11225 -- do not have primitive operations and their discriminants are already
11226 -- handled by gigi.
11228 elsif not Is_Tagged_Type (Parent_Type)
11229 or else not Is_Tagged_Type (Derived_Type)
11230 then
11231 return;
11232 end if;
11234 -- Create a mapping of the form
11236 -- parent type -> derived type
11238 -- to prevent any subsequent attempts to produce the same relations
11240 Type_Map.Set (Parent_Type, Derived_Type);
11242 -- Create mappings of the form
11244 -- parent type discriminant -> derived type discriminant
11245 -- <or>
11246 -- parent type discriminant -> constraint
11248 -- Note that mapping of discriminants breaks privacy because it needs to
11249 -- work with those views which contains the discriminants and any stored
11250 -- constraints.
11252 Map_Discriminants
11253 (Par_Typ => Discriminated_View (Parent_Type),
11254 Deriv_Typ => Discriminated_View (Derived_Type));
11256 -- Create mappings of the form
11258 -- parent type primitive -> derived type primitive
11260 Map_Primitives
11261 (Par_Typ => Parent_Type,
11262 Deriv_Typ => Derived_Type);
11263 end Map_Types;
11265 ----------------------------
11266 -- Matching_Standard_Type --
11267 ----------------------------
11269 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
11270 pragma Assert (Is_Scalar_Type (Typ));
11271 Siz : constant Uint := Esize (Typ);
11273 begin
11274 -- Floating-point cases
11276 if Is_Floating_Point_Type (Typ) then
11277 if Siz <= Esize (Standard_Short_Float) then
11278 return Standard_Short_Float;
11279 elsif Siz <= Esize (Standard_Float) then
11280 return Standard_Float;
11281 elsif Siz <= Esize (Standard_Long_Float) then
11282 return Standard_Long_Float;
11283 elsif Siz <= Esize (Standard_Long_Long_Float) then
11284 return Standard_Long_Long_Float;
11285 else
11286 raise Program_Error;
11287 end if;
11289 -- Integer cases (includes fixed-point types)
11291 -- Unsigned integer cases (includes normal enumeration types)
11293 else
11294 return Small_Integer_Type_For (Siz, Is_Unsigned_Type (Typ));
11295 end if;
11296 end Matching_Standard_Type;
11298 -----------------------------
11299 -- May_Generate_Large_Temp --
11300 -----------------------------
11302 -- At the current time, the only types that we return False for (i.e. where
11303 -- we decide we know they cannot generate large temps) are ones where we
11304 -- know the size is 256 bits or less at compile time, and we are still not
11305 -- doing a thorough job on arrays and records.
11307 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
11308 begin
11309 if not Size_Known_At_Compile_Time (Typ) then
11310 return False;
11311 end if;
11313 if Known_Esize (Typ) and then Esize (Typ) <= 256 then
11314 return False;
11315 end if;
11317 if Is_Array_Type (Typ)
11318 and then Present (Packed_Array_Impl_Type (Typ))
11319 then
11320 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
11321 end if;
11323 return True;
11324 end May_Generate_Large_Temp;
11326 --------------------------------------------
11327 -- Needs_Conditional_Null_Excluding_Check --
11328 --------------------------------------------
11330 function Needs_Conditional_Null_Excluding_Check
11331 (Typ : Entity_Id) return Boolean
11333 begin
11334 return
11335 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
11336 end Needs_Conditional_Null_Excluding_Check;
11338 ----------------------------
11339 -- Needs_Constant_Address --
11340 ----------------------------
11342 function Needs_Constant_Address
11343 (Decl : Node_Id;
11344 Typ : Entity_Id) return Boolean
11346 begin
11347 -- If we have no initialization of any kind, then we don't need to place
11348 -- any restrictions on the address clause, because the object will be
11349 -- elaborated after the address clause is evaluated. This happens if the
11350 -- declaration has no initial expression, or the type has no implicit
11351 -- initialization, or the object is imported.
11353 -- The same holds for all initialized scalar types and all access types.
11354 -- Packed bit array types of size up to the maximum integer size are
11355 -- represented using a modular type with an initialization (to zero) and
11356 -- can be processed like other initialized scalar types.
11358 -- If the type is controlled, code to attach the object to a
11359 -- finalization chain is generated at the point of declaration, and
11360 -- therefore the elaboration of the object cannot be delayed: the
11361 -- address expression must be a constant.
11363 if No (Expression (Decl))
11364 and then not Needs_Finalization (Typ)
11365 and then
11366 (not Has_Non_Null_Base_Init_Proc (Typ)
11367 or else Is_Imported (Defining_Identifier (Decl)))
11368 then
11369 return False;
11371 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
11372 or else Is_Access_Type (Typ)
11373 or else
11374 (Is_Bit_Packed_Array (Typ)
11375 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
11376 then
11377 return False;
11379 else
11380 -- Otherwise, we require the address clause to be constant because
11381 -- the call to the initialization procedure (or the attach code) has
11382 -- to happen at the point of the declaration.
11384 -- Actually the IP call has been moved to the freeze actions anyway,
11385 -- so maybe we can relax this restriction???
11387 return True;
11388 end if;
11389 end Needs_Constant_Address;
11391 ----------------------------
11392 -- New_Class_Wide_Subtype --
11393 ----------------------------
11395 function New_Class_Wide_Subtype
11396 (CW_Typ : Entity_Id;
11397 N : Node_Id) return Entity_Id
11399 Res : constant Entity_Id := Create_Itype (E_Void, N);
11401 -- Capture relevant attributes of the class-wide subtype which must be
11402 -- restored after the copy.
11404 Res_Chars : constant Name_Id := Chars (Res);
11405 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
11406 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
11407 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
11408 Res_Scope : constant Entity_Id := Scope (Res);
11410 begin
11411 Copy_Node (CW_Typ, Res);
11413 -- Restore the relevant attributes of the class-wide subtype
11415 Set_Chars (Res, Res_Chars);
11416 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
11417 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
11418 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
11419 Set_Scope (Res, Res_Scope);
11421 -- Decorate the class-wide subtype
11423 Set_Associated_Node_For_Itype (Res, N);
11424 Set_Comes_From_Source (Res, False);
11425 Mutate_Ekind (Res, E_Class_Wide_Subtype);
11426 Set_Etype (Res, Base_Type (CW_Typ));
11427 Set_Freeze_Node (Res, Empty);
11428 Set_Is_Frozen (Res, False);
11429 Set_Is_Itype (Res);
11430 Set_Is_Public (Res, False);
11431 Set_Next_Entity (Res, Empty);
11432 Set_Prev_Entity (Res, Empty);
11433 Set_Sloc (Res, Sloc (N));
11435 Set_Public_Status (Res);
11437 return Res;
11438 end New_Class_Wide_Subtype;
11440 -----------------------------------
11441 -- OK_To_Do_Constant_Replacement --
11442 -----------------------------------
11444 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
11445 ES : constant Entity_Id := Scope (E);
11446 CS : Entity_Id;
11448 begin
11449 -- Do not replace statically allocated objects, because they may be
11450 -- modified outside the current scope.
11452 if Is_Statically_Allocated (E) then
11453 return False;
11455 -- Do not replace aliased or volatile objects, since we don't know what
11456 -- else might change the value.
11458 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
11459 return False;
11461 -- Debug flag -gnatdM disconnects this optimization
11463 elsif Debug_Flag_MM then
11464 return False;
11466 -- Otherwise check scopes
11468 else
11469 CS := Current_Scope;
11471 loop
11472 -- If we are in right scope, replacement is safe
11474 if CS = ES then
11475 return True;
11477 -- Packages do not affect the determination of safety
11479 elsif Ekind (CS) = E_Package then
11480 exit when CS = Standard_Standard;
11481 CS := Scope (CS);
11483 -- Blocks do not affect the determination of safety
11485 elsif Ekind (CS) = E_Block then
11486 CS := Scope (CS);
11488 -- Loops do not affect the determination of safety. Note that we
11489 -- kill all current values on entry to a loop, so we are just
11490 -- talking about processing within a loop here.
11492 elsif Ekind (CS) = E_Loop then
11493 CS := Scope (CS);
11495 -- Otherwise, the reference is dubious, and we cannot be sure that
11496 -- it is safe to do the replacement.
11498 else
11499 exit;
11500 end if;
11501 end loop;
11503 return False;
11504 end if;
11505 end OK_To_Do_Constant_Replacement;
11507 ------------------------------------
11508 -- Possible_Bit_Aligned_Component --
11509 ------------------------------------
11511 function Possible_Bit_Aligned_Component
11512 (N : Node_Id;
11513 For_Slice : Boolean := False) return Boolean
11515 begin
11516 -- Do not process an unanalyzed node because it is not yet decorated and
11517 -- most checks performed below will fail.
11519 if not Analyzed (N) then
11520 return False;
11521 end if;
11523 -- There are never alignment issues in CodePeer mode
11525 if CodePeer_Mode then
11526 return False;
11527 end if;
11529 case Nkind (N) is
11531 -- Case of indexed component
11533 when N_Indexed_Component =>
11534 declare
11535 P : constant Node_Id := Prefix (N);
11536 Ptyp : constant Entity_Id := Etype (P);
11538 begin
11539 -- If we know the component size and it is not larger than the
11540 -- maximum integer size, then we are OK. The back end does the
11541 -- assignment of small misaligned objects correctly.
11543 if Known_Static_Component_Size (Ptyp)
11544 and then Component_Size (Ptyp) <= System_Max_Integer_Size
11545 then
11546 return False;
11548 -- Otherwise, we need to test the prefix, to see if we are
11549 -- indexing from a possibly unaligned component.
11551 else
11552 return Possible_Bit_Aligned_Component (P, For_Slice);
11553 end if;
11554 end;
11556 -- Case of selected component
11558 when N_Selected_Component =>
11559 declare
11560 P : constant Node_Id := Prefix (N);
11561 Comp : constant Entity_Id := Entity (Selector_Name (N));
11563 begin
11564 -- This is the crucial test: if the component itself causes
11565 -- trouble, then we can stop and return True.
11567 if Component_May_Be_Bit_Aligned (Comp, For_Slice) then
11568 return True;
11570 -- Otherwise, we need to test the prefix, to see if we are
11571 -- selecting from a possibly unaligned component.
11573 else
11574 return Possible_Bit_Aligned_Component (P, For_Slice);
11575 end if;
11576 end;
11578 -- For a slice, test the prefix, if that is possibly misaligned,
11579 -- then for sure the slice is.
11581 when N_Slice =>
11582 return Possible_Bit_Aligned_Component (Prefix (N), True);
11584 -- For an unchecked conversion, check whether the expression may
11585 -- be bit aligned.
11587 when N_Unchecked_Type_Conversion =>
11588 return Possible_Bit_Aligned_Component (Expression (N), For_Slice);
11590 -- If we have none of the above, it means that we have fallen off the
11591 -- top testing prefixes recursively, and we now have a stand alone
11592 -- object, where we don't have a problem, unless this is a renaming,
11593 -- in which case we need to look into the renamed object.
11595 when others =>
11596 return Is_Entity_Name (N)
11597 and then Is_Object (Entity (N))
11598 and then Present (Renamed_Object (Entity (N)))
11599 and then Possible_Bit_Aligned_Component
11600 (Renamed_Object (Entity (N)), For_Slice);
11601 end case;
11602 end Possible_Bit_Aligned_Component;
11604 -----------------------------------------------
11605 -- Process_Statements_For_Controlled_Objects --
11606 -----------------------------------------------
11608 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
11609 Loc : constant Source_Ptr := Sloc (N);
11611 function Are_Wrapped (L : List_Id) return Boolean;
11612 -- Determine whether list L contains only one statement which is a block
11614 function Wrap_Statements_In_Block
11615 (L : List_Id;
11616 Scop : Entity_Id := Current_Scope) return Node_Id;
11617 -- Given a list of statements L, wrap it in a block statement and return
11618 -- the generated node. Scop is either the current scope or the scope of
11619 -- the context (if applicable).
11621 -----------------
11622 -- Are_Wrapped --
11623 -----------------
11625 function Are_Wrapped (L : List_Id) return Boolean is
11626 Stmt : constant Node_Id := First (L);
11627 begin
11628 return
11629 Present (Stmt)
11630 and then No (Next (Stmt))
11631 and then Nkind (Stmt) = N_Block_Statement;
11632 end Are_Wrapped;
11634 ------------------------------
11635 -- Wrap_Statements_In_Block --
11636 ------------------------------
11638 function Wrap_Statements_In_Block
11639 (L : List_Id;
11640 Scop : Entity_Id := Current_Scope) return Node_Id
11642 Block_Id : Entity_Id;
11643 Block_Nod : Node_Id;
11644 Iter_Loop : Entity_Id;
11646 begin
11647 Block_Nod :=
11648 Make_Block_Statement (Loc,
11649 Declarations => No_List,
11650 Handled_Statement_Sequence =>
11651 Make_Handled_Sequence_Of_Statements (Loc,
11652 Statements => L));
11654 -- Create a label for the block in case the block needs to manage the
11655 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
11657 Add_Block_Identifier (Block_Nod, Block_Id, Scop);
11659 -- When wrapping the statements of an iterator loop, check whether
11660 -- the loop requires secondary stack management and if so, propagate
11661 -- the appropriate flags to the block. This ensures that the cursor
11662 -- is properly cleaned up at each iteration of the loop.
11664 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
11666 if Present (Iter_Loop) then
11667 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
11669 -- Secondary stack reclamation is suppressed when the associated
11670 -- iterator loop contains a return statement which uses the stack.
11672 Set_Sec_Stack_Needed_For_Return
11673 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
11674 end if;
11676 return Block_Nod;
11677 end Wrap_Statements_In_Block;
11679 -- Local variables
11681 Block : Node_Id;
11683 -- Start of processing for Process_Statements_For_Controlled_Objects
11685 begin
11686 -- Whenever a non-handled statement list is wrapped in a block, the
11687 -- block must be explicitly analyzed to redecorate all entities in the
11688 -- list and ensure that a finalizer is properly built.
11690 case Nkind (N) is
11691 when N_Conditional_Entry_Call
11692 | N_Elsif_Part
11693 | N_If_Statement
11694 | N_Selective_Accept
11696 -- Check the "then statements" for elsif parts and if statements
11698 if Nkind (N) in N_Elsif_Part | N_If_Statement
11699 and then not Is_Empty_List (Then_Statements (N))
11700 and then not Are_Wrapped (Then_Statements (N))
11701 and then Requires_Cleanup_Actions
11702 (L => Then_Statements (N),
11703 Lib_Level => False,
11704 Nested_Constructs => False)
11705 then
11706 Block := Wrap_Statements_In_Block (Then_Statements (N));
11707 Set_Then_Statements (N, New_List (Block));
11709 Analyze (Block);
11710 end if;
11712 -- Check the "else statements" for conditional entry calls, if
11713 -- statements and selective accepts.
11715 if Nkind (N) in
11716 N_Conditional_Entry_Call | N_If_Statement | N_Selective_Accept
11717 and then not Is_Empty_List (Else_Statements (N))
11718 and then not Are_Wrapped (Else_Statements (N))
11719 and then Requires_Cleanup_Actions
11720 (L => Else_Statements (N),
11721 Lib_Level => False,
11722 Nested_Constructs => False)
11723 then
11724 Block := Wrap_Statements_In_Block (Else_Statements (N));
11725 Set_Else_Statements (N, New_List (Block));
11727 Analyze (Block);
11728 end if;
11730 when N_Abortable_Part
11731 | N_Accept_Alternative
11732 | N_Case_Statement_Alternative
11733 | N_Delay_Alternative
11734 | N_Entry_Call_Alternative
11735 | N_Exception_Handler
11736 | N_Loop_Statement
11737 | N_Triggering_Alternative
11739 if not Is_Empty_List (Statements (N))
11740 and then not Are_Wrapped (Statements (N))
11741 and then Requires_Cleanup_Actions
11742 (L => Statements (N),
11743 Lib_Level => False,
11744 Nested_Constructs => False)
11745 then
11746 if Nkind (N) = N_Loop_Statement
11747 and then Present (Identifier (N))
11748 then
11749 Block :=
11750 Wrap_Statements_In_Block
11751 (L => Statements (N),
11752 Scop => Entity (Identifier (N)));
11753 else
11754 Block := Wrap_Statements_In_Block (Statements (N));
11755 end if;
11757 Set_Statements (N, New_List (Block));
11758 Analyze (Block);
11759 end if;
11761 -- Could be e.g. a loop that was transformed into a block or null
11762 -- statement. Do nothing for terminate alternatives.
11764 when N_Block_Statement
11765 | N_Null_Statement
11766 | N_Terminate_Alternative
11768 null;
11770 when others =>
11771 raise Program_Error;
11772 end case;
11773 end Process_Statements_For_Controlled_Objects;
11775 ------------------
11776 -- Power_Of_Two --
11777 ------------------
11779 function Power_Of_Two (N : Node_Id) return Nat is
11780 Typ : constant Entity_Id := Etype (N);
11781 pragma Assert (Is_Integer_Type (Typ));
11783 Siz : constant Nat := UI_To_Int (Esize (Typ));
11784 Val : Uint;
11786 begin
11787 if not Compile_Time_Known_Value (N) then
11788 return 0;
11790 else
11791 Val := Expr_Value (N);
11792 for J in 1 .. Siz - 1 loop
11793 if Val = Uint_2 ** J then
11794 return J;
11795 end if;
11796 end loop;
11798 return 0;
11799 end if;
11800 end Power_Of_Two;
11802 ----------------------
11803 -- Remove_Init_Call --
11804 ----------------------
11806 function Remove_Init_Call
11807 (Var : Entity_Id;
11808 Rep_Clause : Node_Id) return Node_Id
11810 Par : constant Node_Id := Parent (Var);
11811 Typ : constant Entity_Id := Etype (Var);
11813 Init_Proc : Entity_Id;
11814 -- Initialization procedure for Typ
11816 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
11817 -- Look for init call for Var starting at From and scanning the
11818 -- enclosing list until Rep_Clause or the end of the list is reached.
11820 ----------------------------
11821 -- Find_Init_Call_In_List --
11822 ----------------------------
11824 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
11825 Init_Call : Node_Id;
11827 begin
11828 Init_Call := From;
11829 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
11830 if Nkind (Init_Call) = N_Procedure_Call_Statement
11831 and then Is_Entity_Name (Name (Init_Call))
11832 and then Entity (Name (Init_Call)) = Init_Proc
11833 then
11834 return Init_Call;
11835 end if;
11837 Next (Init_Call);
11838 end loop;
11840 return Empty;
11841 end Find_Init_Call_In_List;
11843 Init_Call : Node_Id;
11845 -- Start of processing for Remove_Init_Call
11847 begin
11848 if Present (Initialization_Statements (Var)) then
11849 Init_Call := Initialization_Statements (Var);
11850 Set_Initialization_Statements (Var, Empty);
11852 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
11854 -- No init proc for the type, so obviously no call to be found
11856 return Empty;
11858 else
11859 -- We might be able to handle other cases below by just properly
11860 -- setting Initialization_Statements at the point where the init proc
11861 -- call is generated???
11863 Init_Proc := Base_Init_Proc (Typ);
11865 -- First scan the list containing the declaration of Var
11867 Init_Call := Find_Init_Call_In_List (From => Next (Par));
11869 -- If not found, also look on Var's freeze actions list, if any,
11870 -- since the init call may have been moved there (case of an address
11871 -- clause applying to Var).
11873 if No (Init_Call) and then Present (Freeze_Node (Var)) then
11874 Init_Call :=
11875 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
11876 end if;
11878 -- If the initialization call has actuals that use the secondary
11879 -- stack, the call may have been wrapped into a temporary block, in
11880 -- which case the block itself has to be removed.
11882 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
11883 declare
11884 Blk : constant Node_Id := Next (Par);
11885 begin
11886 if Present
11887 (Find_Init_Call_In_List
11888 (First (Statements (Handled_Statement_Sequence (Blk)))))
11889 then
11890 Init_Call := Blk;
11891 end if;
11892 end;
11893 end if;
11894 end if;
11896 if Present (Init_Call) then
11897 -- If restrictions have forbidden Aborts, the initialization call
11898 -- for objects that require deep initialization has not been wrapped
11899 -- into the following block (see Exp_Ch3, Default_Initialize_Object)
11900 -- so if present remove it as well, and include the IP call in it,
11901 -- in the rare case the caller may need to simply displace the
11902 -- initialization, as is done for a later address specification.
11904 if Nkind (Next (Init_Call)) = N_Block_Statement
11905 and then Is_Initialization_Block (Next (Init_Call))
11906 then
11907 declare
11908 IP_Call : constant Node_Id := Init_Call;
11909 begin
11910 Init_Call := Next (IP_Call);
11911 Remove (IP_Call);
11912 Prepend (IP_Call,
11913 Statements (Handled_Statement_Sequence (Init_Call)));
11914 end;
11915 end if;
11917 Remove (Init_Call);
11918 end if;
11920 return Init_Call;
11921 end Remove_Init_Call;
11923 -------------------------
11924 -- Remove_Side_Effects --
11925 -------------------------
11927 procedure Remove_Side_Effects
11928 (Exp : Node_Id;
11929 Name_Req : Boolean := False;
11930 Renaming_Req : Boolean := False;
11931 Variable_Ref : Boolean := False;
11932 Related_Id : Entity_Id := Empty;
11933 Is_Low_Bound : Boolean := False;
11934 Is_High_Bound : Boolean := False;
11935 Discr_Number : Int := 0;
11936 Check_Side_Effects : Boolean := True)
11938 function Build_Temporary
11939 (Loc : Source_Ptr;
11940 Id : Character;
11941 Related_Nod : Node_Id := Empty) return Entity_Id;
11942 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11943 -- is present (xxx is taken from the Chars field of Related_Nod),
11944 -- otherwise it generates an internal temporary. The created temporary
11945 -- entity is marked as internal.
11947 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean;
11948 -- Computes whether a side effect is possible in SPARK, which should
11949 -- be handled by removing it from the expression for GNATprove. Note
11950 -- that other side effects related to volatile variables are handled
11951 -- separately.
11953 ---------------------
11954 -- Build_Temporary --
11955 ---------------------
11957 function Build_Temporary
11958 (Loc : Source_Ptr;
11959 Id : Character;
11960 Related_Nod : Node_Id := Empty) return Entity_Id
11962 Temp_Id : Entity_Id;
11963 Temp_Nam : Name_Id;
11964 Should_Set_Related_Expression : Boolean := False;
11966 begin
11967 -- The context requires an external symbol : expression is
11968 -- the bound of an array, or a discriminant value. We create
11969 -- a unique string using the related entity and an appropriate
11970 -- suffix, rather than a numeric serial number (used for internal
11971 -- entities) that may vary depending on compilation options, in
11972 -- particular on the Assertions_Enabled mode. This avoids spurious
11973 -- link errors.
11975 if Present (Related_Id) then
11976 if Is_Low_Bound then
11977 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
11979 elsif Is_High_Bound then
11980 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
11982 else
11983 pragma Assert (Discr_Number > 0);
11985 -- We don't have any intelligible way of printing T_DISCR in
11986 -- CodePeer. Thus, set a related expression in this case.
11988 Should_Set_Related_Expression := True;
11990 -- Use fully qualified name to avoid ambiguities.
11992 Temp_Nam :=
11993 New_External_Name
11994 (Get_Qualified_Name (Related_Id), "_DISCR", Discr_Number);
11995 end if;
11997 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
11999 if Should_Set_Related_Expression then
12000 Set_Related_Expression (Temp_Id, Related_Nod);
12001 end if;
12003 -- Otherwise generate an internal temporary
12005 else
12006 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
12007 end if;
12009 Set_Is_Internal (Temp_Id);
12011 return Temp_Id;
12012 end Build_Temporary;
12014 -----------------------------------
12015 -- Possible_Side_Effect_In_SPARK --
12016 -----------------------------------
12018 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean is
12019 begin
12020 -- Side-effect removal in SPARK should only occur when not inside a
12021 -- generic and not doing a preanalysis, inside an object renaming or
12022 -- a type declaration or a for-loop iteration scheme.
12024 if not Inside_A_Generic
12025 and then Full_Analysis
12026 then
12028 case Nkind (Enclosing_Declaration (Exp)) is
12029 when N_Component_Declaration
12030 | N_Full_Type_Declaration
12031 | N_Iterator_Specification
12032 | N_Loop_Parameter_Specification
12033 | N_Object_Renaming_Declaration
12035 return True;
12037 -- If the expression belongs to an itype declaration, then
12038 -- check if side effects are allowed in the original
12039 -- associated node.
12041 when N_Subtype_Declaration =>
12042 declare
12043 Subt : constant Entity_Id :=
12044 Defining_Identifier (Enclosing_Declaration (Exp));
12045 begin
12046 if Is_Itype (Subt) then
12048 -- When this routine is called while the itype
12049 -- is being created, the entity might not yet be
12050 -- decorated with the associated node, but should
12051 -- have the related expression.
12053 if Present (Associated_Node_For_Itype (Subt)) then
12054 return
12055 Possible_Side_Effect_In_SPARK
12056 (Associated_Node_For_Itype (Subt));
12058 elsif Present (Related_Expression (Subt)) then
12059 return
12060 Possible_Side_Effect_In_SPARK
12061 (Related_Expression (Subt));
12063 -- When the itype doesn't have any indication of its
12064 -- origin (which currently only happens for packed
12065 -- array types created by freezing that shouldn't
12066 -- be picked by GNATprove anyway), then we can
12067 -- conservatively assume that the expression can
12068 -- be kept as it appears in the source code.
12070 else
12071 pragma Assert (Is_Packed_Array_Impl_Type (Subt));
12072 return False;
12073 end if;
12074 else
12075 return True;
12076 end if;
12077 end;
12079 when others =>
12080 return False;
12081 end case;
12082 else
12083 return False;
12084 end if;
12085 end Possible_Side_Effect_In_SPARK;
12087 -- Local variables
12089 Loc : constant Source_Ptr := Sloc (Exp);
12090 Exp_Type : constant Entity_Id := Etype (Exp);
12091 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
12092 Def_Id : Entity_Id;
12093 E : Node_Id;
12094 New_Exp : Node_Id;
12095 Ptr_Typ_Decl : Node_Id;
12096 Ref_Type : Entity_Id;
12097 Res : Node_Id;
12099 -- Start of processing for Remove_Side_Effects
12101 begin
12102 -- Handle cases in which there is nothing to do. In GNATprove mode,
12103 -- removal of side effects is useful for the light expansion of
12104 -- renamings.
12106 if not Expander_Active
12107 and then not
12108 (GNATprove_Mode and then Possible_Side_Effect_In_SPARK (Exp))
12109 then
12110 return;
12112 -- Cannot generate temporaries if the invocation to remove side effects
12113 -- was issued too early and the type of the expression is not resolved
12114 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
12115 -- Remove_Side_Effects).
12117 elsif No (Exp_Type)
12118 or else Ekind (Exp_Type) = E_Access_Attribute_Type
12119 then
12120 return;
12122 -- No action needed for side-effect-free expressions
12124 elsif Check_Side_Effects
12125 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
12126 then
12127 return;
12129 -- Generating C code we cannot remove side effect of function returning
12130 -- class-wide types since there is no secondary stack (required to use
12131 -- 'reference).
12133 elsif Modify_Tree_For_C
12134 and then Nkind (Exp) = N_Function_Call
12135 and then Is_Class_Wide_Type (Etype (Exp))
12136 then
12137 return;
12138 end if;
12140 -- The remaining processing is done with all checks suppressed
12142 -- Note: from now on, don't use return statements, instead do a goto
12143 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
12145 Scope_Suppress.Suppress := (others => True);
12147 -- If this is a side-effect-free attribute reference whose expressions
12148 -- are also side-effect-free and whose prefix is not a name, remove the
12149 -- side effects of the prefix. A copy of the prefix is required in this
12150 -- case and it is better not to make an additional one for the attribute
12151 -- itself, because the return type of many of them is universal integer,
12152 -- which is a very large type for a temporary.
12153 -- The prefix of an attribute reference Reduce may be syntactically an
12154 -- aggregate, but will be expanded into a loop, so no need to remove
12155 -- side effects.
12157 if Nkind (Exp) = N_Attribute_Reference
12158 and then Side_Effect_Free_Attribute (Attribute_Name (Exp))
12159 and then Side_Effect_Free (Expressions (Exp), Name_Req, Variable_Ref)
12160 and then (Attribute_Name (Exp) /= Name_Reduce
12161 or else Nkind (Prefix (Exp)) /= N_Aggregate)
12162 and then not Is_Name_Reference (Prefix (Exp))
12163 then
12164 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
12165 goto Leave;
12167 -- If this is an elementary or a small not-by-reference record type, and
12168 -- we need to capture the value, just make a constant; this is cheap and
12169 -- objects of both kinds of types can be bit aligned, so it might not be
12170 -- possible to generate a reference to them. Likewise if this is not a
12171 -- name reference, except for a type conversion, because we would enter
12172 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
12173 -- type has predicates (and type conversions need a specific treatment
12174 -- anyway, see below). Also do it if we have a volatile reference and
12175 -- Name_Req is not set (see comments for Side_Effect_Free).
12177 elsif (Is_Elementary_Type (Exp_Type)
12178 or else (Is_Record_Type (Exp_Type)
12179 and then Known_Static_RM_Size (Exp_Type)
12180 and then RM_Size (Exp_Type) <= System_Max_Integer_Size
12181 and then not Has_Discriminants (Exp_Type)
12182 and then not Is_By_Reference_Type (Exp_Type)))
12183 and then (Variable_Ref
12184 or else (not Is_Name_Reference (Exp)
12185 and then Nkind (Exp) /= N_Type_Conversion)
12186 or else (not Name_Req
12187 and then Is_Volatile_Reference (Exp)))
12188 then
12189 Def_Id := Build_Temporary (Loc, 'R', Exp);
12190 Set_Etype (Def_Id, Exp_Type);
12191 Res := New_Occurrence_Of (Def_Id, Loc);
12193 -- If the expression is a packed reference, it must be reanalyzed and
12194 -- expanded, depending on context. This is the case for actuals where
12195 -- a constraint check may capture the actual before expansion of the
12196 -- call is complete.
12198 if Nkind (Exp) = N_Indexed_Component
12199 and then Is_Packed (Etype (Prefix (Exp)))
12200 then
12201 Set_Analyzed (Exp, False);
12202 Set_Analyzed (Prefix (Exp), False);
12203 end if;
12205 -- Generate:
12206 -- Rnn : Exp_Type renames Expr;
12208 -- In GNATprove mode, we prefer to use renamings for intermediate
12209 -- variables to definition of constants, due to the implicit move
12210 -- operation that such a constant definition causes as part of the
12211 -- support in GNATprove for ownership pointers. Hence, we generate
12212 -- a renaming for a reference to an object of a nonscalar type.
12214 if Renaming_Req
12215 or else (GNATprove_Mode
12216 and then Is_Object_Reference (Exp)
12217 and then not Is_Scalar_Type (Exp_Type))
12218 then
12219 E :=
12220 Make_Object_Renaming_Declaration (Loc,
12221 Defining_Identifier => Def_Id,
12222 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12223 Name => Relocate_Node (Exp));
12225 -- Generate:
12226 -- Rnn : constant Exp_Type := Expr;
12228 else
12229 E :=
12230 Make_Object_Declaration (Loc,
12231 Defining_Identifier => Def_Id,
12232 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12233 Constant_Present => True,
12234 Expression => Relocate_Node (Exp));
12236 Set_Assignment_OK (E);
12237 end if;
12239 Insert_Action (Exp, E);
12241 -- If the expression has the form v.all then we can just capture the
12242 -- pointer, and then do an explicit dereference on the result, but
12243 -- this is not right if this is a volatile reference.
12245 elsif Nkind (Exp) = N_Explicit_Dereference
12246 and then not Is_Volatile_Reference (Exp)
12247 then
12248 Def_Id := Build_Temporary (Loc, 'R', Exp);
12249 Res :=
12250 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
12252 Insert_Action (Exp,
12253 Make_Object_Declaration (Loc,
12254 Defining_Identifier => Def_Id,
12255 Object_Definition =>
12256 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
12257 Constant_Present => True,
12258 Expression => Relocate_Node (Prefix (Exp))));
12260 -- Similar processing for an unchecked conversion of an expression of
12261 -- the form v.all, where we want the same kind of treatment.
12263 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
12264 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
12265 then
12266 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
12267 goto Leave;
12269 -- If this is a type conversion, leave the type conversion and remove
12270 -- side effects in the expression, unless it is of universal integer,
12271 -- which is a very large type for a temporary. This is important in
12272 -- several circumstances: for change of representations and also when
12273 -- this is a view conversion to a smaller object, where gigi can end
12274 -- up creating its own temporary of the wrong size.
12276 elsif Nkind (Exp) = N_Type_Conversion
12277 and then Etype (Expression (Exp)) /= Universal_Integer
12278 then
12279 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
12281 -- Generating C code the type conversion of an access to constrained
12282 -- array type into an access to unconstrained array type involves
12283 -- initializing a fat pointer and the expression must be free of
12284 -- side effects to safely compute its bounds.
12286 if Modify_Tree_For_C
12287 and then Is_Access_Type (Etype (Exp))
12288 and then Is_Array_Type (Designated_Type (Etype (Exp)))
12289 and then not Is_Constrained (Designated_Type (Etype (Exp)))
12290 then
12291 Def_Id := Build_Temporary (Loc, 'R', Exp);
12292 Set_Etype (Def_Id, Exp_Type);
12293 Res := New_Occurrence_Of (Def_Id, Loc);
12295 Insert_Action (Exp,
12296 Make_Object_Declaration (Loc,
12297 Defining_Identifier => Def_Id,
12298 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12299 Constant_Present => True,
12300 Expression => Relocate_Node (Exp)));
12301 else
12302 goto Leave;
12303 end if;
12305 -- If this is an unchecked conversion that Gigi can't handle, make
12306 -- a copy or a use a renaming to capture the value.
12308 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
12309 and then not Safe_Unchecked_Type_Conversion (Exp)
12310 then
12311 if CW_Or_Needs_Finalization (Exp_Type) then
12313 -- Use a renaming to capture the expression, rather than create
12314 -- a controlled temporary.
12316 Def_Id := Build_Temporary (Loc, 'R', Exp);
12317 Res := New_Occurrence_Of (Def_Id, Loc);
12319 Insert_Action (Exp,
12320 Make_Object_Renaming_Declaration (Loc,
12321 Defining_Identifier => Def_Id,
12322 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12323 Name => Relocate_Node (Exp)));
12325 else
12326 Def_Id := Build_Temporary (Loc, 'R', Exp);
12327 Set_Etype (Def_Id, Exp_Type);
12328 Res := New_Occurrence_Of (Def_Id, Loc);
12330 E :=
12331 Make_Object_Declaration (Loc,
12332 Defining_Identifier => Def_Id,
12333 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12334 Constant_Present => not Is_Variable (Exp),
12335 Expression => Relocate_Node (Exp));
12337 Set_Assignment_OK (E);
12338 Insert_Action (Exp, E);
12339 end if;
12341 -- If this is a packed array component or a selected component with a
12342 -- nonstandard representation, we cannot generate a reference because
12343 -- the component may be unaligned, so we must use a renaming and this
12344 -- renaming is handled by the front end, as the back end may balk at
12345 -- the nonstandard representation (see Evaluation_Required in Exp_Ch8).
12347 elsif (Nkind (Exp) in N_Indexed_Component | N_Selected_Component
12348 and then Has_Non_Standard_Rep (Etype (Prefix (Exp))))
12350 -- For an expression that denotes a name, we can use a renaming
12351 -- scheme. This is needed for correctness in the case of a volatile
12352 -- object of a nonvolatile type because the Make_Reference call of the
12353 -- "default" approach would generate an illegal access value (an
12354 -- access value cannot designate such an object - see
12355 -- Analyze_Reference).
12357 or else (Is_Name_Reference (Exp)
12359 -- We skip using this scheme if we have an object of a volatile
12360 -- type and we do not have Name_Req set true (see comments for
12361 -- Side_Effect_Free).
12363 and then (Name_Req or else not Treat_As_Volatile (Exp_Type)))
12364 then
12365 Def_Id := Build_Temporary (Loc, 'R', Exp);
12366 Res := New_Occurrence_Of (Def_Id, Loc);
12368 Insert_Action (Exp,
12369 Make_Object_Renaming_Declaration (Loc,
12370 Defining_Identifier => Def_Id,
12371 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12372 Name => Relocate_Node (Exp)));
12374 -- Avoid generating a variable-sized temporary, by generating the
12375 -- reference just for the function call. The transformation could be
12376 -- refined to apply only when the array component is constrained by a
12377 -- discriminant???
12379 elsif Nkind (Exp) = N_Selected_Component
12380 and then Nkind (Prefix (Exp)) = N_Function_Call
12381 and then Is_Array_Type (Exp_Type)
12382 then
12383 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
12384 goto Leave;
12386 -- Otherwise we generate a reference to the expression
12388 else
12389 -- When generating C code we cannot consider side-effect-free object
12390 -- declarations that have discriminants and are initialized by means
12391 -- of a function call since on this target there is no secondary
12392 -- stack to store the return value and the expander may generate an
12393 -- extra call to the function to compute the discriminant value. In
12394 -- addition, for targets that have secondary stack, the expansion of
12395 -- functions with side effects involves the generation of an access
12396 -- type to capture the return value stored in the secondary stack;
12397 -- by contrast when generating C code such expansion generates an
12398 -- internal object declaration (no access type involved) which must
12399 -- be identified here to avoid entering into a never-ending loop
12400 -- generating internal object declarations.
12402 if Modify_Tree_For_C
12403 and then Nkind (Parent (Exp)) = N_Object_Declaration
12404 and then
12405 (Nkind (Exp) /= N_Function_Call
12406 or else not Has_Discriminants (Exp_Type)
12407 or else Is_Internal_Name
12408 (Chars (Defining_Identifier (Parent (Exp)))))
12409 then
12410 goto Leave;
12411 end if;
12413 -- Special processing for function calls that return a limited type.
12414 -- We need to build a declaration that will enable build-in-place
12415 -- expansion of the call. This is not done if the context is already
12416 -- an object declaration, to prevent infinite recursion.
12418 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
12419 -- to accommodate functions returning limited objects by reference.
12421 if Ada_Version >= Ada_2005
12422 and then Nkind (Exp) = N_Function_Call
12423 and then Is_Inherently_Limited_Type (Etype (Exp))
12424 and then Nkind (Parent (Exp)) /= N_Object_Declaration
12425 then
12426 declare
12427 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
12428 Decl : Node_Id;
12430 begin
12431 Decl :=
12432 Make_Object_Declaration (Loc,
12433 Defining_Identifier => Obj,
12434 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12435 Expression => Relocate_Node (Exp));
12437 Insert_Action (Exp, Decl);
12438 Set_Etype (Obj, Exp_Type);
12439 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
12440 goto Leave;
12441 end;
12442 end if;
12444 Def_Id := Build_Temporary (Loc, 'R', Exp);
12446 -- The regular expansion of functions with side effects involves the
12447 -- generation of an access type to capture the return value found on
12448 -- the secondary stack. Since SPARK (and why) cannot process access
12449 -- types, use a different approach which ignores the secondary stack
12450 -- and "copies" the returned object.
12451 -- When generating C code, no need for a 'reference since the
12452 -- secondary stack is not supported.
12454 if GNATprove_Mode or Modify_Tree_For_C then
12455 Res := New_Occurrence_Of (Def_Id, Loc);
12456 Ref_Type := Exp_Type;
12458 -- Regular expansion utilizing an access type and 'reference
12460 else
12461 Res :=
12462 Make_Explicit_Dereference (Loc,
12463 Prefix => New_Occurrence_Of (Def_Id, Loc));
12465 -- Generate:
12466 -- type Ann is access all <Exp_Type>;
12468 Ref_Type := Make_Temporary (Loc, 'A');
12470 Ptr_Typ_Decl :=
12471 Make_Full_Type_Declaration (Loc,
12472 Defining_Identifier => Ref_Type,
12473 Type_Definition =>
12474 Make_Access_To_Object_Definition (Loc,
12475 All_Present => True,
12476 Subtype_Indication =>
12477 New_Occurrence_Of (Exp_Type, Loc)));
12479 Insert_Action (Exp, Ptr_Typ_Decl);
12480 end if;
12482 E := Exp;
12483 if Nkind (E) = N_Explicit_Dereference then
12484 New_Exp := Relocate_Node (Prefix (E));
12486 else
12487 E := Relocate_Node (E);
12489 -- Do not generate a 'reference in SPARK mode or C generation
12490 -- since the access type is not created in the first place.
12492 if GNATprove_Mode or Modify_Tree_For_C then
12493 New_Exp := E;
12495 -- Otherwise generate reference, marking the value as non-null
12496 -- since we know it cannot be null and we don't want a check.
12498 else
12499 New_Exp := Make_Reference (Loc, E);
12500 Set_Is_Known_Non_Null (Def_Id);
12501 end if;
12502 end if;
12504 if Is_Delayed_Aggregate (E) then
12506 -- The expansion of nested aggregates is delayed until the
12507 -- enclosing aggregate is expanded. As aggregates are often
12508 -- qualified, the predicate applies to qualified expressions as
12509 -- well, indicating that the enclosing aggregate has not been
12510 -- expanded yet. At this point the aggregate is part of a
12511 -- stand-alone declaration, and must be fully expanded.
12513 if Nkind (E) = N_Qualified_Expression then
12514 Set_Expansion_Delayed (Expression (E), False);
12515 Set_Analyzed (Expression (E), False);
12516 else
12517 Set_Expansion_Delayed (E, False);
12518 end if;
12520 Set_Analyzed (E, False);
12521 end if;
12523 -- Generating C code of object declarations that have discriminants
12524 -- and are initialized by means of a function call we propagate the
12525 -- discriminants of the parent type to the internally built object.
12526 -- This is needed to avoid generating an extra call to the called
12527 -- function.
12529 -- For example, if we generate here the following declaration, it
12530 -- will be expanded later adding an extra call to evaluate the value
12531 -- of the discriminant (needed to compute the size of the object).
12533 -- type Rec (D : Integer) is ...
12534 -- Obj : constant Rec := SomeFunc;
12536 if Modify_Tree_For_C
12537 and then Nkind (Parent (Exp)) = N_Object_Declaration
12538 and then Has_Discriminants (Exp_Type)
12539 and then Nkind (Exp) = N_Function_Call
12540 then
12541 Insert_Action (Exp,
12542 Make_Object_Declaration (Loc,
12543 Defining_Identifier => Def_Id,
12544 Object_Definition => New_Copy_Tree
12545 (Object_Definition (Parent (Exp))),
12546 Constant_Present => True,
12547 Expression => New_Exp));
12548 else
12549 Insert_Action (Exp,
12550 Make_Object_Declaration (Loc,
12551 Defining_Identifier => Def_Id,
12552 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
12553 Constant_Present => True,
12554 Expression => New_Exp));
12555 end if;
12556 end if;
12558 -- Preserve the Assignment_OK flag in all copies, since at least one
12559 -- copy may be used in a context where this flag must be set (otherwise
12560 -- why would the flag be set in the first place).
12562 Set_Assignment_OK (Res, Assignment_OK (Exp));
12564 -- Preserve the Do_Range_Check flag in all copies
12566 Set_Do_Range_Check (Res, Do_Range_Check (Exp));
12568 -- Finally rewrite the original expression and we are done
12570 Rewrite (Exp, Res);
12571 Analyze_And_Resolve (Exp, Exp_Type);
12573 <<Leave>>
12574 Scope_Suppress := Svg_Suppress;
12575 end Remove_Side_Effects;
12577 ------------------------
12578 -- Replace_References --
12579 ------------------------
12581 procedure Replace_References
12582 (Expr : Node_Id;
12583 Par_Typ : Entity_Id;
12584 Deriv_Typ : Entity_Id;
12585 Par_Obj : Entity_Id := Empty;
12586 Deriv_Obj : Entity_Id := Empty)
12588 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
12589 -- Determine whether node Ref denotes some component of Deriv_Obj
12591 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
12592 -- Substitute a reference to an entity with the corresponding value
12593 -- stored in table Type_Map.
12595 function Type_Of_Formal
12596 (Call : Node_Id;
12597 Actual : Node_Id) return Entity_Id;
12598 -- Find the type of the formal parameter which corresponds to actual
12599 -- parameter Actual in subprogram call Call.
12601 ----------------------
12602 -- Is_Deriv_Obj_Ref --
12603 ----------------------
12605 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
12606 Par : constant Node_Id := Parent (Ref);
12608 begin
12609 -- Detect the folowing selected component form:
12611 -- Deriv_Obj.(something)
12613 return
12614 Nkind (Par) = N_Selected_Component
12615 and then Is_Entity_Name (Prefix (Par))
12616 and then Entity (Prefix (Par)) = Deriv_Obj;
12617 end Is_Deriv_Obj_Ref;
12619 -----------------
12620 -- Replace_Ref --
12621 -----------------
12623 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
12624 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
12625 -- Reset the Controlling_Argument of all function calls that
12626 -- encapsulate node From_Arg.
12628 ----------------------------------
12629 -- Remove_Controlling_Arguments --
12630 ----------------------------------
12632 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
12633 Par : Node_Id;
12635 begin
12636 Par := From_Arg;
12637 while Present (Par) loop
12638 if Nkind (Par) = N_Function_Call
12639 and then Present (Controlling_Argument (Par))
12640 then
12641 Set_Controlling_Argument (Par, Empty);
12643 -- Prevent the search from going too far
12645 elsif Is_Body_Or_Package_Declaration (Par) then
12646 exit;
12647 end if;
12649 Par := Parent (Par);
12650 end loop;
12651 end Remove_Controlling_Arguments;
12653 -- Local variables
12655 Context : constant Node_Id :=
12656 (if No (Ref) then Empty else Parent (Ref));
12658 Loc : constant Source_Ptr := Sloc (Ref);
12659 Ref_Id : Entity_Id;
12660 Result : Traverse_Result;
12662 New_Ref : Node_Id;
12663 -- The new reference which is intended to substitute the old one
12665 Old_Ref : Node_Id;
12666 -- The reference designated for replacement. In certain cases this
12667 -- may be a node other than Ref.
12669 Val : Node_Or_Entity_Id;
12670 -- The corresponding value of Ref from the type map
12672 -- Start of processing for Replace_Ref
12674 begin
12675 -- Assume that the input reference is to be replaced and that the
12676 -- traversal should examine the children of the reference.
12678 Old_Ref := Ref;
12679 Result := OK;
12681 -- The input denotes a meaningful reference
12683 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
12684 Ref_Id := Entity (Ref);
12685 Val := Type_Map.Get (Ref_Id);
12687 -- The reference has a corresponding value in the type map, a
12688 -- substitution is possible.
12690 if Present (Val) then
12692 -- The reference denotes a discriminant
12694 if Ekind (Ref_Id) = E_Discriminant then
12695 if Nkind (Val) in N_Entity then
12697 -- The value denotes another discriminant. Replace as
12698 -- follows:
12700 -- _object.Discr -> _object.Val
12702 if Ekind (Val) = E_Discriminant then
12703 New_Ref := New_Occurrence_Of (Val, Loc);
12705 -- Otherwise the value denotes the entity of a name which
12706 -- constraints the discriminant. Replace as follows:
12708 -- _object.Discr -> Val
12710 else
12711 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
12713 New_Ref := New_Occurrence_Of (Val, Loc);
12714 Old_Ref := Parent (Old_Ref);
12715 end if;
12717 -- Otherwise the value denotes an arbitrary expression which
12718 -- constraints the discriminant. Replace as follows:
12720 -- _object.Discr -> Val
12722 else
12723 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
12725 New_Ref := New_Copy_Tree (Val);
12726 Old_Ref := Parent (Old_Ref);
12727 end if;
12729 -- Otherwise the reference denotes a primitive. Replace as
12730 -- follows:
12732 -- Primitive -> Val
12734 else
12735 pragma Assert (Nkind (Val) in N_Entity);
12736 New_Ref := New_Occurrence_Of (Val, Loc);
12737 end if;
12739 -- The reference mentions the _object parameter of the parent
12740 -- type's DIC or type invariant procedure. Replace as follows:
12742 -- _object -> _object
12744 elsif Present (Par_Obj)
12745 and then Present (Deriv_Obj)
12746 and then Ref_Id = Par_Obj
12747 then
12748 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
12750 -- The type of the _object parameter is class-wide when the
12751 -- expression comes from an assertion pragma that applies to
12752 -- an abstract parent type or an interface. The class-wide type
12753 -- facilitates the preanalysis of the expression by treating
12754 -- calls to abstract primitives that mention the current
12755 -- instance of the type as dispatching. Once the calls are
12756 -- remapped to invoke overriding or inherited primitives, the
12757 -- calls no longer need to be dispatching. Examine all function
12758 -- calls that encapsulate the _object parameter and reset their
12759 -- Controlling_Argument attribute.
12761 if Is_Class_Wide_Type (Etype (Par_Obj))
12762 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
12763 then
12764 Remove_Controlling_Arguments (Old_Ref);
12765 end if;
12767 -- The reference to _object acts as an actual parameter in a
12768 -- subprogram call which may be invoking a primitive of the
12769 -- parent type:
12771 -- Primitive (... _object ...);
12773 -- The parent type primitive may not be overridden nor
12774 -- inherited when it is declared after the derived type
12775 -- definition:
12777 -- type Parent is tagged private;
12778 -- type Child is new Parent with private;
12779 -- procedure Primitive (Obj : Parent);
12781 -- In this scenario the _object parameter is converted to the
12782 -- parent type. Due to complications with partial/full views
12783 -- and view swaps, the parent type is taken from the formal
12784 -- parameter of the subprogram being called.
12786 if Nkind (Context) in N_Subprogram_Call
12787 and then No (Type_Map.Get (Entity (Name (Context))))
12788 then
12789 declare
12790 -- We need to use the Original_Node of the callee, in
12791 -- case it was already modified. Note that we are using
12792 -- Traverse_Proc to walk the tree, and it is defined to
12793 -- walk subtrees in an arbitrary order.
12795 Callee : constant Entity_Id :=
12796 Entity (Original_Node (Name (Context)));
12797 begin
12798 if No (Type_Map.Get (Callee)) then
12799 New_Ref :=
12800 Convert_To
12801 (Type_Of_Formal (Context, Old_Ref), New_Ref);
12803 -- Do not process the generated type conversion
12804 -- because both the parent type and the derived type
12805 -- are in the Type_Map table. This will clobber the
12806 -- type conversion by resetting its subtype mark.
12808 Result := Skip;
12809 end if;
12810 end;
12811 end if;
12813 -- Otherwise there is nothing to replace
12815 else
12816 New_Ref := Empty;
12817 end if;
12819 if Present (New_Ref) then
12820 Rewrite (Old_Ref, New_Ref);
12822 -- Update the return type when the context of the reference
12823 -- acts as the name of a function call. Note that the update
12824 -- should not be performed when the reference appears as an
12825 -- actual in the call.
12827 if Nkind (Context) = N_Function_Call
12828 and then Name (Context) = Old_Ref
12829 then
12830 Set_Etype (Context, Etype (Val));
12831 end if;
12832 end if;
12833 end if;
12835 -- Reanalyze the reference due to potential replacements
12837 if Nkind (Old_Ref) in N_Has_Etype then
12838 Set_Analyzed (Old_Ref, False);
12839 end if;
12841 return Result;
12842 end Replace_Ref;
12844 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
12846 --------------------
12847 -- Type_Of_Formal --
12848 --------------------
12850 function Type_Of_Formal
12851 (Call : Node_Id;
12852 Actual : Node_Id) return Entity_Id
12854 A : Node_Id;
12855 F : Entity_Id;
12857 begin
12858 -- Examine the list of actual and formal parameters in parallel
12860 A := First (Parameter_Associations (Call));
12861 F := First_Formal (Entity (Name (Call)));
12862 while Present (A) and then Present (F) loop
12863 if A = Actual then
12864 return Etype (F);
12865 end if;
12867 Next (A);
12868 Next_Formal (F);
12869 end loop;
12871 -- The actual parameter must always have a corresponding formal
12873 pragma Assert (False);
12875 return Empty;
12876 end Type_Of_Formal;
12878 -- Start of processing for Replace_References
12880 begin
12881 -- Map the attributes of the parent type to the proper corresponding
12882 -- attributes of the derived type.
12884 Map_Types
12885 (Parent_Type => Par_Typ,
12886 Derived_Type => Deriv_Typ);
12888 -- Inspect the input expression and perform substitutions where
12889 -- necessary.
12891 Replace_Refs (Expr);
12892 end Replace_References;
12894 -----------------------------
12895 -- Replace_Type_References --
12896 -----------------------------
12898 procedure Replace_Type_References
12899 (Expr : Node_Id;
12900 Typ : Entity_Id;
12901 Obj_Id : Entity_Id)
12903 procedure Replace_Type_Ref (N : Node_Id);
12904 -- Substitute a single reference of the current instance of type Typ
12905 -- with a reference to Obj_Id.
12907 ----------------------
12908 -- Replace_Type_Ref --
12909 ----------------------
12911 procedure Replace_Type_Ref (N : Node_Id) is
12912 begin
12913 -- Decorate the reference to Typ even though it may be rewritten
12914 -- further down. This is done so that routines which examine
12915 -- properties of the Original_Node have some semantic information.
12917 if Nkind (N) = N_Identifier then
12918 Set_Entity (N, Typ);
12919 Set_Etype (N, Typ);
12921 elsif Nkind (N) = N_Selected_Component then
12922 Analyze (Prefix (N));
12923 Set_Entity (Selector_Name (N), Typ);
12924 Set_Etype (Selector_Name (N), Typ);
12925 end if;
12927 -- Perform the following substitution:
12929 -- Typ --> _object
12931 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
12932 Set_Comes_From_Source (N, True);
12933 end Replace_Type_Ref;
12935 procedure Replace_Type_Refs is
12936 new Replace_Type_References_Generic (Replace_Type_Ref);
12938 -- Start of processing for Replace_Type_References
12940 begin
12941 Replace_Type_Refs (Expr, Typ);
12942 end Replace_Type_References;
12944 ---------------------------
12945 -- Represented_As_Scalar --
12946 ---------------------------
12948 function Represented_As_Scalar (T : Entity_Id) return Boolean is
12949 UT : constant Entity_Id := Underlying_Type (T);
12950 begin
12951 return Is_Scalar_Type (UT)
12952 or else (Is_Bit_Packed_Array (UT)
12953 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
12954 end Represented_As_Scalar;
12956 ------------------------------
12957 -- Requires_Cleanup_Actions --
12958 ------------------------------
12960 function Requires_Cleanup_Actions
12961 (N : Node_Id;
12962 Lib_Level : Boolean) return Boolean
12964 At_Lib_Level : constant Boolean :=
12965 Lib_Level
12966 and then Nkind (N) in N_Package_Body | N_Package_Specification;
12967 -- N is at the library level if the top-most context is a package and
12968 -- the path taken to reach N does not include nonpackage constructs.
12970 begin
12971 case Nkind (N) is
12972 when N_Accept_Statement
12973 | N_Block_Statement
12974 | N_Entry_Body
12975 | N_Package_Body
12976 | N_Subprogram_Body
12977 | N_Task_Body
12979 return
12980 Requires_Cleanup_Actions
12981 (L => Declarations (N),
12982 Lib_Level => At_Lib_Level,
12983 Nested_Constructs => True)
12984 or else
12985 (Present (Handled_Statement_Sequence (N))
12986 and then
12987 Requires_Cleanup_Actions
12988 (L =>
12989 Statements (Handled_Statement_Sequence (N)),
12990 Lib_Level => At_Lib_Level,
12991 Nested_Constructs => True));
12993 -- Extended return statements are the same as the above, except that
12994 -- there is no Declarations field. We do not want to clean up the
12995 -- Return_Object_Declarations.
12997 when N_Extended_Return_Statement =>
12998 return
12999 Present (Handled_Statement_Sequence (N))
13000 and then Requires_Cleanup_Actions
13001 (L =>
13002 Statements (Handled_Statement_Sequence (N)),
13003 Lib_Level => At_Lib_Level,
13004 Nested_Constructs => True);
13006 when N_Package_Specification =>
13007 return
13008 Requires_Cleanup_Actions
13009 (L => Visible_Declarations (N),
13010 Lib_Level => At_Lib_Level,
13011 Nested_Constructs => True)
13012 or else
13013 Requires_Cleanup_Actions
13014 (L => Private_Declarations (N),
13015 Lib_Level => At_Lib_Level,
13016 Nested_Constructs => True);
13018 when others =>
13019 raise Program_Error;
13020 end case;
13021 end Requires_Cleanup_Actions;
13023 ------------------------------
13024 -- Requires_Cleanup_Actions --
13025 ------------------------------
13027 function Requires_Cleanup_Actions
13028 (L : List_Id;
13029 Lib_Level : Boolean;
13030 Nested_Constructs : Boolean) return Boolean
13032 Decl : Node_Id;
13033 Expr : Node_Id;
13034 Obj_Id : Entity_Id;
13035 Obj_Typ : Entity_Id;
13036 Pack_Id : Entity_Id;
13037 Typ : Entity_Id;
13039 begin
13040 Decl := First (L);
13041 while Present (Decl) loop
13043 -- Library-level tagged types
13045 if Nkind (Decl) = N_Full_Type_Declaration then
13046 Typ := Defining_Identifier (Decl);
13048 -- Ignored Ghost types do not need any cleanup actions because
13049 -- they will not appear in the final tree.
13051 if Is_Ignored_Ghost_Entity (Typ) then
13052 null;
13054 elsif Is_Tagged_Type (Typ)
13055 and then Is_Library_Level_Entity (Typ)
13056 and then Convention (Typ) = Convention_Ada
13057 and then Present (Access_Disp_Table (Typ))
13058 and then not Is_Abstract_Type (Typ)
13059 and then not No_Run_Time_Mode
13060 and then not Restriction_Active (No_Tagged_Type_Registration)
13061 and then RTE_Available (RE_Unregister_Tag)
13062 then
13063 return True;
13064 end if;
13066 -- Regular object declarations
13068 elsif Nkind (Decl) = N_Object_Declaration then
13069 Obj_Id := Defining_Identifier (Decl);
13070 Obj_Typ := Base_Type (Etype (Obj_Id));
13071 Expr := Expression (Decl);
13073 -- Bypass any form of processing for objects which have their
13074 -- finalization disabled. This applies only to objects at the
13075 -- library level.
13077 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
13078 null;
13080 -- Finalization of transient objects is treated separately in
13081 -- order to handle sensitive cases. These include:
13083 -- * Conditional expressions
13084 -- * Expressions with actions
13085 -- * Transient scopes
13087 elsif Is_Finalized_Transient (Obj_Id) then
13088 null;
13090 -- Finalization of specific objects is also treated separately
13092 elsif Is_Ignored_For_Finalization (Obj_Id) then
13093 null;
13095 -- Ignored Ghost objects do not need any cleanup actions because
13096 -- they will not appear in the final tree.
13098 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
13099 null;
13101 -- The object is of the form:
13102 -- Obj : [constant] Typ [:= Expr];
13104 -- Do not process the incomplete view of a deferred constant.
13105 -- Note that an object initialized by means of a BIP function
13106 -- call may appear as a deferred constant after expansion
13107 -- activities. These kinds of objects must be finalized.
13109 elsif not Is_Imported (Obj_Id)
13110 and then Needs_Finalization (Obj_Typ)
13111 and then not (Ekind (Obj_Id) = E_Constant
13112 and then not Has_Completion (Obj_Id)
13113 and then No (BIP_Initialization_Call (Obj_Id)))
13114 then
13115 return True;
13117 -- The object is of the form:
13118 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
13120 -- Obj : Access_Typ :=
13121 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
13123 elsif Is_Access_Type (Obj_Typ)
13124 and then Needs_Finalization
13125 (Available_View (Designated_Type (Obj_Typ)))
13126 and then Present (Expr)
13127 and then
13128 (Is_Secondary_Stack_BIP_Func_Call (Expr)
13129 or else
13130 (Is_Non_BIP_Func_Call (Expr)
13131 and then not Is_Related_To_Func_Return (Obj_Id)))
13132 then
13133 return True;
13135 -- Processing for "hook" objects generated for transient objects
13136 -- declared inside an Expression_With_Actions.
13138 elsif Is_Access_Type (Obj_Typ)
13139 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
13140 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
13141 N_Object_Declaration
13142 then
13143 return True;
13145 -- Processing for intermediate results of if expressions where
13146 -- one of the alternatives uses a controlled function call.
13148 elsif Is_Access_Type (Obj_Typ)
13149 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
13150 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
13151 N_Defining_Identifier
13152 and then Present (Expr)
13153 and then Nkind (Expr) = N_Null
13154 then
13155 return True;
13157 -- Simple protected objects which use type System.Tasking.
13158 -- Protected_Objects.Protection to manage their locks should be
13159 -- treated as controlled since they require manual cleanup.
13160 -- The only exception is illustrated in the following example:
13162 -- package Pkg is
13163 -- type Ctrl is new Controlled ...
13164 -- procedure Finalize (Obj : in out Ctrl);
13165 -- Lib_Obj : Ctrl;
13166 -- end Pkg;
13168 -- package body Pkg is
13169 -- protected Prot is
13170 -- procedure Do_Something (Obj : in out Ctrl);
13171 -- end Prot;
13173 -- protected body Prot is
13174 -- procedure Do_Something (Obj : in out Ctrl) is ...
13175 -- end Prot;
13177 -- procedure Finalize (Obj : in out Ctrl) is
13178 -- begin
13179 -- Prot.Do_Something (Obj);
13180 -- end Finalize;
13181 -- end Pkg;
13183 -- Since for the most part entities in package bodies depend on
13184 -- those in package specs, Prot's lock should be cleaned up
13185 -- first. The subsequent cleanup of the spec finalizes Lib_Obj.
13186 -- This act however attempts to invoke Do_Something and fails
13187 -- because the lock has disappeared.
13189 elsif Ekind (Obj_Id) = E_Variable
13190 and then not In_Library_Level_Package_Body (Obj_Id)
13191 and then Has_Simple_Protected_Object (Obj_Typ)
13192 then
13193 return True;
13194 end if;
13196 -- Specific cases of object renamings
13198 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
13199 Obj_Id := Defining_Identifier (Decl);
13200 Obj_Typ := Base_Type (Etype (Obj_Id));
13202 -- Bypass any form of processing for objects which have their
13203 -- finalization disabled. This applies only to objects at the
13204 -- library level.
13206 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
13207 null;
13209 -- Ignored Ghost object renamings do not need any cleanup actions
13210 -- because they will not appear in the final tree.
13212 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
13213 null;
13215 -- Return object of extended return statements. This case is
13216 -- recognized and marked by the expansion of extended return
13217 -- statements (see Expand_N_Extended_Return_Statement).
13219 elsif Needs_Finalization (Obj_Typ)
13220 and then Is_Return_Object (Obj_Id)
13221 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
13222 then
13223 return True;
13224 end if;
13226 -- Inspect the freeze node of an access-to-controlled type and look
13227 -- for a delayed finalization master. This case arises when the
13228 -- freeze actions are inserted at a later time than the expansion of
13229 -- the context. Since Build_Finalizer is never called on a single
13230 -- construct twice, the master will be ultimately left out and never
13231 -- finalized. This is also needed for freeze actions of designated
13232 -- types themselves, since in some cases the finalization master is
13233 -- associated with a designated type's freeze node rather than that
13234 -- of the access type (see handling for freeze actions in
13235 -- Build_Finalization_Master).
13237 elsif Nkind (Decl) = N_Freeze_Entity
13238 and then Present (Actions (Decl))
13239 then
13240 Typ := Entity (Decl);
13242 -- Freeze nodes for ignored Ghost types do not need cleanup
13243 -- actions because they will never appear in the final tree.
13245 if Is_Ignored_Ghost_Entity (Typ) then
13246 null;
13248 elsif ((Is_Access_Object_Type (Typ)
13249 and then Needs_Finalization
13250 (Available_View (Designated_Type (Typ))))
13251 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
13252 and then Requires_Cleanup_Actions
13253 (Actions (Decl), Lib_Level, Nested_Constructs)
13254 then
13255 return True;
13256 end if;
13258 -- Nested package declarations
13260 elsif Nested_Constructs
13261 and then Nkind (Decl) = N_Package_Declaration
13262 then
13263 Pack_Id := Defining_Entity (Decl);
13265 -- Do not inspect an ignored Ghost package because all code found
13266 -- within will not appear in the final tree.
13268 if Is_Ignored_Ghost_Entity (Pack_Id) then
13269 null;
13271 elsif Ekind (Pack_Id) /= E_Generic_Package
13272 and then Requires_Cleanup_Actions
13273 (Specification (Decl), Lib_Level)
13274 then
13275 return True;
13276 end if;
13278 -- Nested package bodies
13280 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
13282 -- Do not inspect an ignored Ghost package body because all code
13283 -- found within will not appear in the final tree.
13285 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
13286 null;
13288 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13289 and then Requires_Cleanup_Actions (Decl, Lib_Level)
13290 then
13291 return True;
13292 end if;
13293 end if;
13295 Next (Decl);
13296 end loop;
13298 return False;
13299 end Requires_Cleanup_Actions;
13301 ------------------------------------
13302 -- Safe_Unchecked_Type_Conversion --
13303 ------------------------------------
13305 -- Note: this function knows quite a bit about the exact requirements of
13306 -- Gigi with respect to unchecked type conversions, and its code must be
13307 -- coordinated with any changes in Gigi in this area.
13309 -- The above requirements should be documented in Sinfo ???
13311 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
13312 Otyp : Entity_Id;
13313 Ityp : Entity_Id;
13314 Oalign : Uint;
13315 Ialign : Uint;
13316 Pexp : constant Node_Id := Parent (Exp);
13318 begin
13319 -- If the expression is the RHS of an assignment or object declaration
13320 -- we are always OK because there will always be a target.
13322 -- Object renaming declarations, (generated for view conversions of
13323 -- actuals in inlined calls), like object declarations, provide an
13324 -- explicit type, and are safe as well.
13326 if (Nkind (Pexp) = N_Assignment_Statement
13327 and then Expression (Pexp) = Exp)
13328 or else Nkind (Pexp)
13329 in N_Object_Declaration | N_Object_Renaming_Declaration
13330 then
13331 return True;
13333 -- If the expression is the prefix of an N_Selected_Component we should
13334 -- also be OK because GCC knows to look inside the conversion except if
13335 -- the type is discriminated. We assume that we are OK anyway if the
13336 -- type is not set yet or if it is controlled since we can't afford to
13337 -- introduce a temporary in this case.
13339 elsif Nkind (Pexp) = N_Selected_Component
13340 and then Prefix (Pexp) = Exp
13341 then
13342 return No (Etype (Pexp))
13343 or else not Is_Type (Etype (Pexp))
13344 or else not Has_Discriminants (Etype (Pexp))
13345 or else Is_Constrained (Etype (Pexp));
13346 end if;
13348 -- Set the output type, this comes from Etype if it is set, otherwise we
13349 -- take it from the subtype mark, which we assume was already fully
13350 -- analyzed.
13352 if Present (Etype (Exp)) then
13353 Otyp := Etype (Exp);
13354 else
13355 Otyp := Entity (Subtype_Mark (Exp));
13356 end if;
13358 -- The input type always comes from the expression, and we assume this
13359 -- is indeed always analyzed, so we can simply get the Etype.
13361 Ityp := Etype (Expression (Exp));
13363 -- Initialize alignments to unknown so far
13365 Oalign := No_Uint;
13366 Ialign := No_Uint;
13368 -- Replace a concurrent type by its corresponding record type and each
13369 -- type by its underlying type and do the tests on those. The original
13370 -- type may be a private type whose completion is a concurrent type, so
13371 -- find the underlying type first.
13373 if Present (Underlying_Type (Otyp)) then
13374 Otyp := Underlying_Type (Otyp);
13375 end if;
13377 if Present (Underlying_Type (Ityp)) then
13378 Ityp := Underlying_Type (Ityp);
13379 end if;
13381 if Is_Concurrent_Type (Otyp) then
13382 Otyp := Corresponding_Record_Type (Otyp);
13383 end if;
13385 if Is_Concurrent_Type (Ityp) then
13386 Ityp := Corresponding_Record_Type (Ityp);
13387 end if;
13389 -- If the base types are the same, we know there is no problem since
13390 -- this conversion will be a noop.
13392 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
13393 return True;
13395 -- Same if this is an upwards conversion of an untagged type, and there
13396 -- are no constraints involved (could be more general???)
13398 elsif Etype (Ityp) = Otyp
13399 and then not Is_Tagged_Type (Ityp)
13400 and then not Has_Discriminants (Ityp)
13401 and then No (First_Rep_Item (Base_Type (Ityp)))
13402 then
13403 return True;
13405 -- If the expression has an access type (object or subprogram) we assume
13406 -- that the conversion is safe, because the size of the target is safe,
13407 -- even if it is a record (which might be treated as having unknown size
13408 -- at this point).
13410 elsif Is_Access_Type (Ityp) then
13411 return True;
13413 -- If the size of output type is known at compile time, there is never
13414 -- a problem. Note that unconstrained records are considered to be of
13415 -- known size, but we can't consider them that way here, because we are
13416 -- talking about the actual size of the object.
13418 -- We also make sure that in addition to the size being known, we do not
13419 -- have a case which might generate an embarrassingly large temp in
13420 -- stack checking mode.
13422 elsif Size_Known_At_Compile_Time (Otyp)
13423 and then
13424 (not Stack_Checking_Enabled
13425 or else not May_Generate_Large_Temp (Otyp))
13426 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
13427 then
13428 return True;
13430 -- If either type is tagged, then we know the alignment is OK so Gigi
13431 -- will be able to use pointer punning.
13433 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
13434 return True;
13436 -- If either type is a limited record type, we cannot do a copy, so say
13437 -- safe since there's nothing else we can do.
13439 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
13440 return True;
13442 -- Conversions to and from packed array types are always ignored and
13443 -- hence are safe.
13445 elsif Is_Packed_Array_Impl_Type (Otyp)
13446 or else Is_Packed_Array_Impl_Type (Ityp)
13447 then
13448 return True;
13449 end if;
13451 -- The only other cases known to be safe is if the input type's
13452 -- alignment is known to be at least the maximum alignment for the
13453 -- target or if both alignments are known and the output type's
13454 -- alignment is no stricter than the input's. We can use the component
13455 -- type alignment for an array if a type is an unpacked array type.
13457 if Present (Alignment_Clause (Otyp)) then
13458 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
13460 elsif Is_Array_Type (Otyp)
13461 and then Present (Alignment_Clause (Component_Type (Otyp)))
13462 then
13463 Oalign := Expr_Value (Expression (Alignment_Clause
13464 (Component_Type (Otyp))));
13465 end if;
13467 if Present (Alignment_Clause (Ityp)) then
13468 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
13470 elsif Is_Array_Type (Ityp)
13471 and then Present (Alignment_Clause (Component_Type (Ityp)))
13472 then
13473 Ialign := Expr_Value (Expression (Alignment_Clause
13474 (Component_Type (Ityp))));
13475 end if;
13477 if Present (Ialign) and then Ialign > Maximum_Alignment then
13478 return True;
13480 elsif Present (Ialign)
13481 and then Present (Oalign)
13482 and then Ialign <= Oalign
13483 then
13484 return True;
13486 -- Otherwise, Gigi cannot handle this and we must make a temporary
13488 else
13489 return False;
13490 end if;
13491 end Safe_Unchecked_Type_Conversion;
13493 ---------------------------------
13494 -- Set_Current_Value_Condition --
13495 ---------------------------------
13497 -- Note: the implementation of this procedure is very closely tied to the
13498 -- implementation of Get_Current_Value_Condition. Here we set required
13499 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
13500 -- them, so they must have a consistent view.
13502 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
13504 procedure Set_Entity_Current_Value (N : Node_Id);
13505 -- If N is an entity reference, where the entity is of an appropriate
13506 -- kind, then set the current value of this entity to Cnode, unless
13507 -- there is already a definite value set there.
13509 procedure Set_Expression_Current_Value (N : Node_Id);
13510 -- If N is of an appropriate form, sets an appropriate entry in current
13511 -- value fields of relevant entities. Multiple entities can be affected
13512 -- in the case of an AND or AND THEN.
13514 ------------------------------
13515 -- Set_Entity_Current_Value --
13516 ------------------------------
13518 procedure Set_Entity_Current_Value (N : Node_Id) is
13519 begin
13520 if Is_Entity_Name (N) then
13521 declare
13522 Ent : constant Entity_Id := Entity (N);
13524 begin
13525 -- Don't capture if not safe to do so
13527 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
13528 return;
13529 end if;
13531 -- Here we have a case where the Current_Value field may need
13532 -- to be set. We set it if it is not already set to a compile
13533 -- time expression value.
13535 -- Note that this represents a decision that one condition
13536 -- blots out another previous one. That's certainly right if
13537 -- they occur at the same level. If the second one is nested,
13538 -- then the decision is neither right nor wrong (it would be
13539 -- equally OK to leave the outer one in place, or take the new
13540 -- inner one). Really we should record both, but our data
13541 -- structures are not that elaborate.
13543 if Nkind (Current_Value (Ent)) not in N_Subexpr then
13544 Set_Current_Value (Ent, Cnode);
13545 end if;
13546 end;
13547 end if;
13548 end Set_Entity_Current_Value;
13550 ----------------------------------
13551 -- Set_Expression_Current_Value --
13552 ----------------------------------
13554 procedure Set_Expression_Current_Value (N : Node_Id) is
13555 Cond : Node_Id;
13557 begin
13558 Cond := N;
13560 -- Loop to deal with (ignore for now) any NOT operators present. The
13561 -- presence of NOT operators will be handled properly when we call
13562 -- Get_Current_Value_Condition.
13564 while Nkind (Cond) = N_Op_Not loop
13565 Cond := Right_Opnd (Cond);
13566 end loop;
13568 -- For an AND or AND THEN, recursively process operands
13570 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
13571 Set_Expression_Current_Value (Left_Opnd (Cond));
13572 Set_Expression_Current_Value (Right_Opnd (Cond));
13573 return;
13574 end if;
13576 -- Check possible relational operator
13578 if Nkind (Cond) in N_Op_Compare then
13579 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
13580 Set_Entity_Current_Value (Left_Opnd (Cond));
13581 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
13582 Set_Entity_Current_Value (Right_Opnd (Cond));
13583 end if;
13585 elsif Nkind (Cond) in N_Type_Conversion
13586 | N_Qualified_Expression
13587 | N_Expression_With_Actions
13588 then
13589 Set_Expression_Current_Value (Expression (Cond));
13591 -- Check possible boolean variable reference
13593 else
13594 Set_Entity_Current_Value (Cond);
13595 end if;
13596 end Set_Expression_Current_Value;
13598 -- Start of processing for Set_Current_Value_Condition
13600 begin
13601 Set_Expression_Current_Value (Condition (Cnode));
13602 end Set_Current_Value_Condition;
13604 --------------------------
13605 -- Set_Elaboration_Flag --
13606 --------------------------
13608 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
13609 Loc : constant Source_Ptr := Sloc (N);
13610 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
13611 Asn : Node_Id;
13613 begin
13614 if Present (Ent) then
13616 -- Nothing to do if at the compilation unit level, because in this
13617 -- case the flag is set by the binder generated elaboration routine.
13619 if Nkind (Parent (N)) = N_Compilation_Unit then
13620 null;
13622 -- Here we do need to generate an assignment statement
13624 else
13625 Check_Restriction (No_Elaboration_Code, N);
13627 Asn :=
13628 Make_Assignment_Statement (Loc,
13629 Name => New_Occurrence_Of (Ent, Loc),
13630 Expression => Make_Integer_Literal (Loc, Uint_1));
13632 -- Mark the assignment statement as elaboration code. This allows
13633 -- the early call region mechanism (see Sem_Elab) to properly
13634 -- ignore such assignments even though they are nonpreelaborable
13635 -- code.
13637 Set_Is_Elaboration_Code (Asn);
13639 if Nkind (Parent (N)) = N_Subunit then
13640 Insert_After (Corresponding_Stub (Parent (N)), Asn);
13641 else
13642 Insert_After (N, Asn);
13643 end if;
13645 Analyze (Asn);
13647 -- Kill current value indication. This is necessary because the
13648 -- tests of this flag are inserted out of sequence and must not
13649 -- pick up bogus indications of the wrong constant value.
13651 Set_Current_Value (Ent, Empty);
13653 -- If the subprogram is in the current declarative part and
13654 -- 'access has been applied to it, generate an elaboration
13655 -- check at the beginning of the declarations of the body.
13657 if Nkind (N) = N_Subprogram_Body
13658 and then Address_Taken (Spec_Id)
13659 and then
13660 Ekind (Scope (Spec_Id)) in E_Block | E_Procedure | E_Function
13661 then
13662 declare
13663 Loc : constant Source_Ptr := Sloc (N);
13664 Decls : constant List_Id := Declarations (N);
13665 Chk : Node_Id;
13667 begin
13668 -- No need to generate this check if first entry in the
13669 -- declaration list is a raise of Program_Error now.
13671 if Present (Decls)
13672 and then Nkind (First (Decls)) = N_Raise_Program_Error
13673 then
13674 return;
13675 end if;
13677 -- Otherwise generate the check
13679 Chk :=
13680 Make_Raise_Program_Error (Loc,
13681 Condition =>
13682 Make_Op_Eq (Loc,
13683 Left_Opnd => New_Occurrence_Of (Ent, Loc),
13684 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
13685 Reason => PE_Access_Before_Elaboration);
13687 if No (Decls) then
13688 Set_Declarations (N, New_List (Chk));
13689 else
13690 Prepend (Chk, Decls);
13691 end if;
13693 Analyze (Chk);
13694 end;
13695 end if;
13696 end if;
13697 end if;
13698 end Set_Elaboration_Flag;
13700 ----------------------------
13701 -- Set_Renamed_Subprogram --
13702 ----------------------------
13704 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
13705 begin
13706 -- If input node is an identifier, we can just reset it
13708 if Nkind (N) = N_Identifier then
13709 Set_Chars (N, Chars (E));
13710 Set_Entity (N, E);
13712 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
13714 else
13715 declare
13716 CS : constant Boolean := Comes_From_Source (N);
13717 begin
13718 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
13719 Set_Entity (N, E);
13720 Set_Comes_From_Source (N, CS);
13721 Set_Analyzed (N, True);
13722 end;
13723 end if;
13724 end Set_Renamed_Subprogram;
13726 ----------------------
13727 -- Side_Effect_Free --
13728 ----------------------
13730 function Side_Effect_Free
13731 (N : Node_Id;
13732 Name_Req : Boolean := False;
13733 Variable_Ref : Boolean := False) return Boolean
13735 Typ : constant Entity_Id := Etype (N);
13736 -- Result type of the expression
13738 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
13739 -- The argument N is a construct where the Prefix is dereferenced if it
13740 -- is an access type and the result is a variable. The call returns True
13741 -- if the construct is side-effect-free (not considering side effects in
13742 -- other than the prefix which are to be tested by the caller).
13744 function Within_In_Parameter (N : Node_Id) return Boolean;
13745 -- Determines if N is a subcomponent of a composite in-parameter. If so,
13746 -- N is not side-effect-free when the actual is global and modifiable
13747 -- indirectly from within a subprogram, because it may be passed by
13748 -- reference. The front-end must be conservative here and assume that
13749 -- this may happen with any array or record type. On the other hand, we
13750 -- cannot create temporaries for all expressions for which this
13751 -- condition is true, for various reasons that might require clearing up
13752 -- ??? For example, discriminant references that appear out of place, or
13753 -- spurious type errors with class-wide expressions. As a result, we
13754 -- limit the transformation to loop bounds, which is so far the only
13755 -- case that requires it.
13757 -----------------------------
13758 -- Safe_Prefixed_Reference --
13759 -----------------------------
13761 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
13762 begin
13763 -- If prefix is not side-effect-free, definitely not safe
13765 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
13766 return False;
13768 -- If the prefix is of an access type that is not access-to-constant,
13769 -- then this construct is a variable reference, which means it is to
13770 -- be considered to have side effects if Variable_Ref is set True.
13772 elsif Is_Access_Type (Etype (Prefix (N)))
13773 and then not Is_Access_Constant (Etype (Prefix (N)))
13774 and then Variable_Ref
13775 then
13776 -- Exception is a prefix that is the result of a previous removal
13777 -- of side effects.
13779 return Is_Entity_Name (Prefix (N))
13780 and then not Comes_From_Source (Prefix (N))
13781 and then Ekind (Entity (Prefix (N))) = E_Constant
13782 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
13784 -- If the prefix is an explicit dereference then this construct is a
13785 -- variable reference, which means it is to be considered to have
13786 -- side effects if Variable_Ref is True.
13788 -- We do NOT exclude dereferences of access-to-constant types because
13789 -- we handle them as constant view of variables.
13791 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
13792 and then Variable_Ref
13793 then
13794 return False;
13796 -- Note: The following test is the simplest way of solving a complex
13797 -- problem uncovered by the following test (Side effect on loop bound
13798 -- that is a subcomponent of a global variable:
13800 -- with Text_Io; use Text_Io;
13801 -- procedure Tloop is
13802 -- type X is
13803 -- record
13804 -- V : Natural := 4;
13805 -- S : String (1..5) := (others => 'a');
13806 -- end record;
13807 -- X1 : X;
13809 -- procedure Modi;
13811 -- generic
13812 -- with procedure Action;
13813 -- procedure Loop_G (Arg : X; Msg : String)
13815 -- procedure Loop_G (Arg : X; Msg : String) is
13816 -- begin
13817 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
13818 -- & Natural'Image (Arg.V));
13819 -- for Index in 1 .. Arg.V loop
13820 -- Text_Io.Put_Line
13821 -- (Natural'Image (Index) & " " & Arg.S (Index));
13822 -- if Index > 2 then
13823 -- Modi;
13824 -- end if;
13825 -- end loop;
13826 -- Put_Line ("end loop_g " & Msg);
13827 -- end;
13829 -- procedure Loop1 is new Loop_G (Modi);
13830 -- procedure Modi is
13831 -- begin
13832 -- X1.V := 1;
13833 -- Loop1 (X1, "from modi");
13834 -- end;
13836 -- begin
13837 -- Loop1 (X1, "initial");
13838 -- end;
13840 -- The output of the above program should be:
13842 -- begin loop_g initial will loop till: 4
13843 -- 1 a
13844 -- 2 a
13845 -- 3 a
13846 -- begin loop_g from modi will loop till: 1
13847 -- 1 a
13848 -- end loop_g from modi
13849 -- 4 a
13850 -- begin loop_g from modi will loop till: 1
13851 -- 1 a
13852 -- end loop_g from modi
13853 -- end loop_g initial
13855 -- If a loop bound is a subcomponent of a global variable, a
13856 -- modification of that variable within the loop may incorrectly
13857 -- affect the execution of the loop.
13859 elsif Parent_Kind (Parent (N)) = N_Loop_Parameter_Specification
13860 and then Within_In_Parameter (Prefix (N))
13861 and then Variable_Ref
13862 then
13863 return False;
13865 -- All other cases are side-effect-free
13867 else
13868 return True;
13869 end if;
13870 end Safe_Prefixed_Reference;
13872 -------------------------
13873 -- Within_In_Parameter --
13874 -------------------------
13876 function Within_In_Parameter (N : Node_Id) return Boolean is
13877 begin
13878 if not Comes_From_Source (N) then
13879 return False;
13881 elsif Is_Entity_Name (N) then
13882 return Ekind (Entity (N)) = E_In_Parameter;
13884 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
13885 return Within_In_Parameter (Prefix (N));
13887 else
13888 return False;
13889 end if;
13890 end Within_In_Parameter;
13892 -- Start of processing for Side_Effect_Free
13894 begin
13895 -- If volatile reference, always consider it to have side effects
13897 if Is_Volatile_Reference (N) then
13898 return False;
13899 end if;
13901 -- Note on checks that could raise Constraint_Error. Strictly, if we
13902 -- take advantage of 11.6, these checks do not count as side effects.
13903 -- However, we would prefer to consider that they are side effects,
13904 -- since the back end CSE does not work very well on expressions which
13905 -- can raise Constraint_Error. On the other hand if we don't consider
13906 -- them to be side-effect-free, then we get some awkward expansions
13907 -- in -gnato mode, resulting in code insertions at a point where we
13908 -- do not have a clear model for performing the insertions.
13910 -- Special handling for entity names
13912 if Is_Entity_Name (N) then
13914 -- A type reference is always side-effect-free
13916 if Is_Type (Entity (N)) then
13917 return True;
13919 -- Variables are considered to be a side effect if Variable_Ref
13920 -- is set or if we have a volatile reference and Name_Req is off.
13921 -- If Name_Req is True then we can't help returning a name which
13922 -- effectively allows multiple references in any case.
13924 elsif Is_Variable (N, Use_Original_Node => False) then
13925 return not Variable_Ref
13926 and then (not Is_Volatile_Reference (N) or else Name_Req);
13928 -- Any other entity (e.g. a subtype name) is definitely side
13929 -- effect free.
13931 else
13932 return True;
13933 end if;
13935 -- A value known at compile time is always side-effect-free
13937 elsif Compile_Time_Known_Value (N) then
13938 return True;
13940 -- A variable renaming is not side-effect-free, because the renaming
13941 -- will function like a macro in the front-end in some cases, and an
13942 -- assignment can modify the component designated by N, so we need to
13943 -- create a temporary for it.
13945 -- The guard testing for Entity being present is needed at least in
13946 -- the case of rewritten predicate expressions, and may well also be
13947 -- appropriate elsewhere. Obviously we can't go testing the entity
13948 -- field if it does not exist, so it's reasonable to say that this is
13949 -- not the renaming case if it does not exist.
13951 elsif Is_Entity_Name (Original_Node (N))
13952 and then Present (Entity (Original_Node (N)))
13953 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
13954 and then Ekind (Entity (Original_Node (N))) /= E_Constant
13955 then
13956 declare
13957 RO : constant Node_Id :=
13958 Renamed_Object (Entity (Original_Node (N)));
13960 begin
13961 -- If the renamed object is an indexed component, or an
13962 -- explicit dereference, then the designated object could
13963 -- be modified by an assignment.
13965 if Nkind (RO) in N_Indexed_Component | N_Explicit_Dereference then
13966 return False;
13968 -- A selected component must have a safe prefix
13970 elsif Nkind (RO) = N_Selected_Component then
13971 return Safe_Prefixed_Reference (RO);
13973 -- In all other cases, designated object cannot be changed so
13974 -- we are side-effect-free.
13976 else
13977 return True;
13978 end if;
13979 end;
13981 -- Remove_Side_Effects generates an object renaming declaration to
13982 -- capture the expression of a class-wide expression. In VM targets
13983 -- the frontend performs no expansion for dispatching calls to
13984 -- class- wide types since they are handled by the VM. Hence, we must
13985 -- locate here if this node corresponds to a previous invocation of
13986 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13988 elsif not Tagged_Type_Expansion
13989 and then not Comes_From_Source (N)
13990 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
13991 and then Is_Class_Wide_Type (Typ)
13992 then
13993 return True;
13995 -- Generating C the type conversion of an access to constrained array
13996 -- type into an access to unconstrained array type involves initializing
13997 -- a fat pointer and the expression cannot be assumed to be free of side
13998 -- effects since it must referenced several times to compute its bounds.
14000 elsif Modify_Tree_For_C
14001 and then Nkind (N) = N_Type_Conversion
14002 and then Is_Access_Type (Typ)
14003 and then Is_Array_Type (Designated_Type (Typ))
14004 and then not Is_Constrained (Designated_Type (Typ))
14005 then
14006 return False;
14007 end if;
14009 -- For other than entity names and compile time known values,
14010 -- check the node kind for special processing.
14012 case Nkind (N) is
14014 -- An attribute reference is side-effect-free if its expressions
14015 -- are side-effect-free and its prefix is side-effect-free or is
14016 -- an entity reference.
14018 when N_Attribute_Reference =>
14019 return Side_Effect_Free_Attribute (Attribute_Name (N))
14020 and then
14021 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
14022 and then
14023 (Is_Entity_Name (Prefix (N))
14024 or else
14025 Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref));
14027 -- A binary operator is side-effect-free if and both operands are
14028 -- side-effect-free. For this purpose binary operators include
14029 -- short circuit forms.
14031 when N_Binary_Op
14032 | N_Short_Circuit
14034 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
14035 and then
14036 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
14038 -- Membership tests may have either Right_Opnd or Alternatives set
14040 when N_Membership_Test =>
14041 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
14042 and then
14043 (if Present (Right_Opnd (N))
14044 then Side_Effect_Free
14045 (Right_Opnd (N), Name_Req, Variable_Ref)
14046 else Side_Effect_Free
14047 (Alternatives (N), Name_Req, Variable_Ref));
14049 -- An explicit dereference is side-effect-free only if it is
14050 -- a side-effect-free prefixed reference.
14052 when N_Explicit_Dereference =>
14053 return Safe_Prefixed_Reference (N);
14055 -- An expression with action is side-effect-free if its expression
14056 -- is side-effect-free and it has no actions.
14058 when N_Expression_With_Actions =>
14059 return
14060 Is_Empty_List (Actions (N))
14061 and then Side_Effect_Free
14062 (Expression (N), Name_Req, Variable_Ref);
14064 -- A call to _rep_to_pos is side-effect-free, since we generate
14065 -- this pure function call ourselves. Moreover it is critically
14066 -- important to make this exception, since otherwise we can have
14067 -- discriminants in array components which don't look side-effect
14068 -- free in the case of an array whose index type is an enumeration
14069 -- type with an enumeration rep clause.
14071 -- All other function calls are not side-effect-free
14073 when N_Function_Call =>
14074 return
14075 Nkind (Name (N)) = N_Identifier
14076 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
14077 and then Side_Effect_Free
14078 (First (Parameter_Associations (N)),
14079 Name_Req, Variable_Ref);
14081 -- An IF expression is side-effect-free if it's of a scalar type, and
14082 -- all its components are all side-effect-free (conditions and then
14083 -- actions and else actions). We restrict to scalar types, since it
14084 -- is annoying to deal with things like (if A then B else C)'First
14085 -- where the type involved is a string type.
14087 when N_If_Expression =>
14088 return
14089 Is_Scalar_Type (Typ)
14090 and then Side_Effect_Free
14091 (Expressions (N), Name_Req, Variable_Ref);
14093 -- An indexed component is side-effect-free if it is a side
14094 -- effect free prefixed reference and all the indexing
14095 -- expressions are side-effect-free.
14097 when N_Indexed_Component =>
14098 return
14099 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
14100 and then Safe_Prefixed_Reference (N);
14102 -- A type qualification, type conversion, or unchecked expression is
14103 -- side-effect-free if the expression is side-effect-free.
14105 when N_Qualified_Expression
14106 | N_Type_Conversion
14107 | N_Unchecked_Expression
14109 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
14111 -- A selected component is side-effect-free only if it is a side
14112 -- effect free prefixed reference.
14114 when N_Selected_Component =>
14115 return Safe_Prefixed_Reference (N);
14117 -- A range is side-effect-free if the bounds are side-effect-free
14119 when N_Range =>
14120 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
14121 and then
14122 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
14124 -- A slice is side-effect-free if it is a side-effect-free
14125 -- prefixed reference and the bounds are side-effect-free.
14127 when N_Slice =>
14128 return
14129 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
14130 and then Safe_Prefixed_Reference (N);
14132 -- A unary operator is side-effect-free if the operand
14133 -- is side-effect-free.
14135 when N_Unary_Op =>
14136 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
14138 -- An unchecked type conversion is side-effect-free only if it
14139 -- is safe and its argument is side-effect-free.
14141 when N_Unchecked_Type_Conversion =>
14142 return
14143 Safe_Unchecked_Type_Conversion (N)
14144 and then Side_Effect_Free
14145 (Expression (N), Name_Req, Variable_Ref);
14147 -- A literal is side-effect-free
14149 when N_Character_Literal
14150 | N_Integer_Literal
14151 | N_Real_Literal
14152 | N_String_Literal
14154 return True;
14156 -- An aggregate is side-effect-free if all its values are compile
14157 -- time known.
14159 when N_Aggregate =>
14160 return Compile_Time_Known_Aggregate (N);
14162 -- We consider that anything else has side effects. This is a bit
14163 -- crude, but we are pretty close for most common cases, and we
14164 -- are certainly correct (i.e. we never return True when the
14165 -- answer should be False).
14167 when others =>
14168 return False;
14169 end case;
14170 end Side_Effect_Free;
14172 -- A list is side-effect-free if all elements of the list are side
14173 -- effect free.
14175 function Side_Effect_Free
14176 (L : List_Id;
14177 Name_Req : Boolean := False;
14178 Variable_Ref : Boolean := False) return Boolean
14180 N : Node_Id;
14182 begin
14183 if L = No_List or else L = Error_List then
14184 return True;
14186 else
14187 N := First (L);
14188 while Present (N) loop
14189 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
14190 return False;
14191 else
14192 Next (N);
14193 end if;
14194 end loop;
14196 return True;
14197 end if;
14198 end Side_Effect_Free;
14200 --------------------------------
14201 -- Side_Effect_Free_Attribute --
14202 --------------------------------
14204 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean is
14205 begin
14206 case Name is
14207 when Name_Input =>
14208 return False;
14210 when Name_Image
14211 | Name_Img
14212 | Name_Wide_Image
14213 | Name_Wide_Wide_Image
14215 -- CodePeer doesn't want to see replicated copies of 'Image calls
14217 return not CodePeer_Mode;
14219 when others =>
14220 return True;
14221 end case;
14222 end Side_Effect_Free_Attribute;
14224 ----------------------------------
14225 -- Silly_Boolean_Array_Not_Test --
14226 ----------------------------------
14228 -- This procedure implements an odd and silly test. We explicitly check
14229 -- for the case where the 'First of the component type is equal to the
14230 -- 'Last of this component type, and if this is the case, we make sure
14231 -- that constraint error is raised. The reason is that the NOT is bound
14232 -- to cause CE in this case, and we will not otherwise catch it.
14234 -- No such check is required for AND and OR, since for both these cases
14235 -- False op False = False, and True op True = True. For the XOR case,
14236 -- see Silly_Boolean_Array_Xor_Test.
14238 -- Believe it or not, this was reported as a bug. Note that nearly always,
14239 -- the test will evaluate statically to False, so the code will be
14240 -- statically removed, and no extra overhead caused.
14242 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
14243 Loc : constant Source_Ptr := Sloc (N);
14244 CT : constant Entity_Id := Component_Type (T);
14246 begin
14247 -- The check we install is
14249 -- constraint_error when
14250 -- component_type'first = component_type'last
14251 -- and then array_type'Length /= 0)
14253 -- We need the last guard because we don't want to raise CE for empty
14254 -- arrays since no out of range values result. (Empty arrays with a
14255 -- component type of True .. True -- very useful -- even the ACATS
14256 -- does not test that marginal case).
14258 Insert_Action (N,
14259 Make_Raise_Constraint_Error (Loc,
14260 Condition =>
14261 Make_And_Then (Loc,
14262 Left_Opnd =>
14263 Make_Op_Eq (Loc,
14264 Left_Opnd =>
14265 Make_Attribute_Reference (Loc,
14266 Prefix => New_Occurrence_Of (CT, Loc),
14267 Attribute_Name => Name_First),
14269 Right_Opnd =>
14270 Make_Attribute_Reference (Loc,
14271 Prefix => New_Occurrence_Of (CT, Loc),
14272 Attribute_Name => Name_Last)),
14274 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
14275 Reason => CE_Range_Check_Failed));
14276 end Silly_Boolean_Array_Not_Test;
14278 ----------------------------------
14279 -- Silly_Boolean_Array_Xor_Test --
14280 ----------------------------------
14282 -- This procedure implements an odd and silly test. We explicitly check
14283 -- for the XOR case where the component type is True .. True, since this
14284 -- will raise constraint error. A special check is required since CE
14285 -- will not be generated otherwise (cf Expand_Packed_Not).
14287 -- No such check is required for AND and OR, since for both these cases
14288 -- False op False = False, and True op True = True, and no check is
14289 -- required for the case of False .. False, since False xor False = False.
14290 -- See also Silly_Boolean_Array_Not_Test
14292 procedure Silly_Boolean_Array_Xor_Test
14293 (N : Node_Id;
14294 R : Node_Id;
14295 T : Entity_Id)
14297 Loc : constant Source_Ptr := Sloc (N);
14298 CT : constant Entity_Id := Component_Type (T);
14300 begin
14301 -- The check we install is
14303 -- constraint_error when
14304 -- Boolean (component_type'First)
14305 -- and then Boolean (component_type'Last)
14306 -- and then array_type'Length /= 0)
14308 -- We need the last guard because we don't want to raise CE for empty
14309 -- arrays since no out of range values result (Empty arrays with a
14310 -- component type of True .. True -- very useful -- even the ACATS
14311 -- does not test that marginal case).
14313 Insert_Action (N,
14314 Make_Raise_Constraint_Error (Loc,
14315 Condition =>
14316 Make_And_Then (Loc,
14317 Left_Opnd =>
14318 Make_And_Then (Loc,
14319 Left_Opnd =>
14320 Convert_To (Standard_Boolean,
14321 Make_Attribute_Reference (Loc,
14322 Prefix => New_Occurrence_Of (CT, Loc),
14323 Attribute_Name => Name_First)),
14325 Right_Opnd =>
14326 Convert_To (Standard_Boolean,
14327 Make_Attribute_Reference (Loc,
14328 Prefix => New_Occurrence_Of (CT, Loc),
14329 Attribute_Name => Name_Last))),
14331 Right_Opnd => Make_Non_Empty_Check (Loc, R)),
14332 Reason => CE_Range_Check_Failed));
14333 end Silly_Boolean_Array_Xor_Test;
14335 ----------------------------
14336 -- Small_Integer_Type_For --
14337 ----------------------------
14339 function Small_Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id
14341 begin
14342 -- The only difference between this and Integer_Type_For is that this
14343 -- can return small (8- or 16-bit) types.
14345 if S <= Standard_Short_Short_Integer_Size then
14346 if Uns then
14347 return Standard_Short_Short_Unsigned;
14348 else
14349 return Standard_Short_Short_Integer;
14350 end if;
14352 elsif S <= Standard_Short_Integer_Size then
14353 if Uns then
14354 return Standard_Short_Unsigned;
14355 else
14356 return Standard_Short_Integer;
14357 end if;
14359 else
14360 return Integer_Type_For (S, Uns);
14361 end if;
14362 end Small_Integer_Type_For;
14364 ------------------
14365 -- Thunk_Target --
14366 ------------------
14368 function Thunk_Target (Thunk : Entity_Id) return Entity_Id is
14369 Target : Entity_Id := Thunk;
14371 begin
14372 pragma Assert (Is_Thunk (Thunk));
14374 while Is_Thunk (Target) loop
14375 Target := Thunk_Entity (Target);
14376 end loop;
14378 return Target;
14379 end Thunk_Target;
14381 -------------------
14382 -- Type_Map_Hash --
14383 -------------------
14385 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
14386 begin
14387 return Type_Map_Header (Id mod Type_Map_Size);
14388 end Type_Map_Hash;
14390 ------------------------------------------
14391 -- Type_May_Have_Bit_Aligned_Components --
14392 ------------------------------------------
14394 function Type_May_Have_Bit_Aligned_Components
14395 (Typ : Entity_Id) return Boolean
14397 begin
14398 -- Array type, check component type
14400 if Is_Array_Type (Typ) then
14401 return
14402 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
14404 -- Record type, check components
14406 elsif Is_Record_Type (Typ) then
14407 declare
14408 E : Entity_Id;
14410 begin
14411 E := First_Component_Or_Discriminant (Typ);
14412 while Present (E) loop
14413 -- This is the crucial test: if the component itself causes
14414 -- trouble, then we can stop and return True.
14416 if Component_May_Be_Bit_Aligned (E) then
14417 return True;
14418 end if;
14420 -- Otherwise, we need to test its type, to see if it may
14421 -- itself contain a troublesome component.
14423 if Type_May_Have_Bit_Aligned_Components (Etype (E)) then
14424 return True;
14425 end if;
14427 Next_Component_Or_Discriminant (E);
14428 end loop;
14430 return False;
14431 end;
14433 -- Type other than array or record is always OK
14435 else
14436 return False;
14437 end if;
14438 end Type_May_Have_Bit_Aligned_Components;
14440 -------------------------------
14441 -- Update_Primitives_Mapping --
14442 -------------------------------
14444 procedure Update_Primitives_Mapping
14445 (Inher_Id : Entity_Id;
14446 Subp_Id : Entity_Id)
14448 Parent_Type : constant Entity_Id := Find_Dispatching_Type (Inher_Id);
14449 Derived_Type : constant Entity_Id := Find_Dispatching_Type (Subp_Id);
14451 begin
14452 pragma Assert (Parent_Type /= Derived_Type);
14453 Map_Types (Parent_Type, Derived_Type);
14454 end Update_Primitives_Mapping;
14456 ----------------------------------
14457 -- Within_Case_Or_If_Expression --
14458 ----------------------------------
14460 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
14461 Nod : Node_Id;
14462 Par : Node_Id;
14464 begin
14465 -- Locate an enclosing case or if expression. Note that these constructs
14466 -- can be expanded into Expression_With_Actions, hence the test of the
14467 -- original node.
14469 Nod := N;
14470 Par := Parent (Nod);
14472 while Present (Par) loop
14473 if Nkind (Original_Node (Par)) = N_Case_Expression
14474 and then Nod /= Expression (Original_Node (Par))
14475 then
14476 return True;
14478 elsif Nkind (Original_Node (Par)) = N_If_Expression
14479 and then Nod /= First (Expressions (Original_Node (Par)))
14480 then
14481 return True;
14483 -- Stop at contexts where temporaries may be contained
14485 elsif Nkind (Par) in N_Aggregate
14486 | N_Delta_Aggregate
14487 | N_Extension_Aggregate
14488 | N_Block_Statement
14489 | N_Loop_Statement
14490 then
14491 return False;
14493 -- Prevent the search from going too far
14495 elsif Is_Body_Or_Package_Declaration (Par) then
14496 return False;
14497 end if;
14499 Nod := Par;
14500 Par := Parent (Nod);
14501 end loop;
14503 return False;
14504 end Within_Case_Or_If_Expression;
14506 ------------------------------
14507 -- Predicate_Check_In_Scope --
14508 ------------------------------
14510 function Predicate_Check_In_Scope (N : Node_Id) return Boolean is
14511 S : Entity_Id;
14513 begin
14514 S := Current_Scope;
14515 while Present (S) and then not Is_Subprogram (S) loop
14516 S := Scope (S);
14517 end loop;
14519 if Present (S) then
14521 -- Predicate checks should only be enabled in init procs for
14522 -- expressions coming from source.
14524 if Is_Init_Proc (S) then
14525 return Comes_From_Source (N);
14527 elsif Get_TSS_Name (S) /= TSS_Null
14528 and then not Is_Predicate_Function (S)
14529 then
14530 return False;
14531 end if;
14532 end if;
14534 return True;
14535 end Predicate_Check_In_Scope;
14537 end Exp_Util;