libatomic: Handle AVX+CX16 ZHAOXIN like Intel for 16b atomic [PR104688]
[official-gcc.git] / gcc / ada / exp_util.adb
blobfcb62a64e706c086d97367716f5ff340c83fa966
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 Stand; use Stand;
64 with Stringt; use Stringt;
65 with Tbuild; use Tbuild;
66 with Ttypes; use Ttypes;
67 with Validsw; use Validsw;
68 with Warnsw; use Warnsw;
70 with GNAT.HTable;
71 package body Exp_Util is
73 ---------------------------------------------------------
74 -- Handling of inherited class-wide pre/postconditions --
75 ---------------------------------------------------------
77 -- Following AI12-0113, the expression for a class-wide condition is
78 -- transformed for a subprogram that inherits it, by replacing calls
79 -- to primitive operations of the original controlling type into the
80 -- corresponding overriding operations of the derived type. The following
81 -- hash table manages this mapping, and is expanded on demand whenever
82 -- such inherited expression needs to be constructed.
84 -- The mapping is also used to check whether an inherited operation has
85 -- a condition that depends on overridden operations. For such an
86 -- operation we must create a wrapper that is then treated as a normal
87 -- overriding. In SPARK mode such operations are illegal.
89 -- For a given root type there may be several type extensions with their
90 -- own overriding operations, so at various times a given operation of
91 -- the root will be mapped into different overridings. The root type is
92 -- also mapped into the current type extension to indicate that its
93 -- operations are mapped into the overriding operations of that current
94 -- type extension.
96 -- The contents of the map are as follows:
98 -- Key Value
100 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
101 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
102 -- Discriminant (Entity_Id) Expression (Node_Id)
103 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
104 -- Type (Entity_Id) Type (Entity_Id)
106 Type_Map_Size : constant := 511;
108 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
109 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
111 package Type_Map is new GNAT.HTable.Simple_HTable
112 (Header_Num => Type_Map_Header,
113 Key => Entity_Id,
114 Element => Node_Or_Entity_Id,
115 No_Element => Empty,
116 Hash => Type_Map_Hash,
117 Equal => "=");
119 -----------------------
120 -- Local Subprograms --
121 -----------------------
123 function Build_Task_Array_Image
124 (Loc : Source_Ptr;
125 Id_Ref : Node_Id;
126 A_Type : Entity_Id;
127 Dyn : Boolean := False) return Node_Id;
128 -- Build function to generate the image string for a task that is an array
129 -- component, concatenating the images of each index. To avoid storage
130 -- leaks, the string is built with successive slice assignments. The flag
131 -- Dyn indicates whether this is called for the initialization procedure of
132 -- an array of tasks, or for the name of a dynamically created task that is
133 -- assigned to an indexed component.
135 function Build_Task_Image_Function
136 (Loc : Source_Ptr;
137 Decls : List_Id;
138 Stats : List_Id;
139 Res : Entity_Id) return Node_Id;
140 -- Common processing for Task_Array_Image and Task_Record_Image. Build
141 -- function body that computes image.
143 procedure Build_Task_Image_Prefix
144 (Loc : Source_Ptr;
145 Len : out Entity_Id;
146 Res : out Entity_Id;
147 Pos : out Entity_Id;
148 Prefix : Entity_Id;
149 Sum : Node_Id;
150 Decls : List_Id;
151 Stats : List_Id);
152 -- Common processing for Task_Array_Image and Task_Record_Image. Create
153 -- local variables and assign prefix of name to result string.
155 function Build_Task_Record_Image
156 (Loc : Source_Ptr;
157 Id_Ref : Node_Id;
158 Dyn : Boolean := False) return Node_Id;
159 -- Build function to generate the image string for a task that is a record
160 -- component. Concatenate name of variable with that of selector. The flag
161 -- Dyn indicates whether this is called for the initialization procedure of
162 -- record with task components, or for a dynamically created task that is
163 -- assigned to a selected component.
165 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
166 -- Force evaluation of bounds of a slice, which may be given by a range
167 -- or by a subtype indication with or without a constraint.
169 function Is_Uninitialized_Aggregate
170 (Exp : Node_Id;
171 T : Entity_Id) return Boolean;
172 -- Determine whether an array aggregate used in an object declaration
173 -- is uninitialized, when the aggregate is declared with a box and
174 -- the component type has no default value. Such an aggregate can be
175 -- optimized away to prevent the copying of uninitialized data, and
176 -- the bounds of the aggregate can be propagated directly to the
177 -- object declaration.
179 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
180 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
181 -- an assertion expression that should be verified at run time.
183 function Make_Literal_Range
184 (Loc : Source_Ptr;
185 Literal_Typ : Entity_Id) return Node_Id;
186 -- Produce a Range node whose bounds are:
187 -- Low_Bound (Literal_Type) ..
188 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
189 -- this is used for expanding declarations like X : String := "sdfgdfg";
191 -- If the index type of the target array is not integer, we generate:
192 -- Low_Bound (Literal_Type) ..
193 -- Literal_Type'Val
194 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
195 -- + (Length (Literal_Typ) -1))
197 function Make_Non_Empty_Check
198 (Loc : Source_Ptr;
199 N : Node_Id) return Node_Id;
200 -- Produce a boolean expression checking that the unidimensional array
201 -- node N is not empty.
203 function New_Class_Wide_Subtype
204 (CW_Typ : Entity_Id;
205 N : Node_Id) return Entity_Id;
206 -- Create an implicit subtype of CW_Typ attached to node N
208 function Requires_Cleanup_Actions
209 (L : List_Id;
210 Lib_Level : Boolean;
211 Nested_Constructs : Boolean) return Boolean;
212 -- Given a list L, determine whether it contains one of the following:
214 -- 1) controlled objects
215 -- 2) library-level tagged types
217 -- Lib_Level is True when the list comes from a construct at the library
218 -- level, and False otherwise. Nested_Constructs is True when any nested
219 -- packages declared in L must be processed, and False otherwise.
221 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean;
222 -- Return True if the evaluation of the given attribute is considered
223 -- side-effect-free, independently of its prefix and expressions.
225 -------------------------------------
226 -- Activate_Atomic_Synchronization --
227 -------------------------------------
229 procedure Activate_Atomic_Synchronization (N : Node_Id) is
230 Msg_Node : Node_Id;
232 begin
233 case Nkind (Parent (N)) is
235 -- Check for cases of appearing in the prefix of a construct where we
236 -- don't need atomic synchronization for this kind of usage.
238 when
239 -- Nothing to do if we are the prefix of an attribute, since we
240 -- do not want an atomic sync operation for things like 'Size.
242 N_Attribute_Reference
244 -- The N_Reference node is like an attribute
246 | N_Reference
248 -- Nothing to do for a reference to a component (or components)
249 -- of a composite object. Only reads and updates of the object
250 -- as a whole require atomic synchronization (RM C.6 (15)).
252 | N_Indexed_Component
253 | N_Selected_Component
254 | N_Slice
256 -- For all the above cases, nothing to do if we are the prefix
258 if Prefix (Parent (N)) = N then
259 return;
260 end if;
262 when others =>
263 null;
264 end case;
266 -- Nothing to do for the identifier in an object renaming declaration,
267 -- the renaming itself does not need atomic synchronization.
269 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
270 return;
271 end if;
273 -- Go ahead and set the flag
275 Set_Atomic_Sync_Required (N);
277 -- Generate info message if requested
279 if Warn_On_Atomic_Synchronization then
280 case Nkind (N) is
281 when N_Identifier =>
282 Msg_Node := N;
284 when N_Expanded_Name
285 | N_Selected_Component
287 Msg_Node := Selector_Name (N);
289 when N_Explicit_Dereference
290 | N_Indexed_Component
292 Msg_Node := Empty;
294 when others =>
295 pragma Assert (False);
296 return;
297 end case;
299 if Present (Msg_Node) then
300 Error_Msg_N
301 ("atomic synchronization set for &?.n?", Msg_Node);
302 else
303 Error_Msg_N
304 ("atomic synchronization set?.n?", N);
305 end if;
306 end if;
307 end Activate_Atomic_Synchronization;
309 ----------------------
310 -- Adjust_Condition --
311 ----------------------
313 procedure Adjust_Condition (N : Node_Id) is
315 function Is_Hardbool_Type (T : Entity_Id) return Boolean;
316 -- Return True iff T is a type annotated with the
317 -- Machine_Attribute pragma "hardbool".
319 ----------------------
320 -- Is_Hardbool_Type --
321 ----------------------
323 function Is_Hardbool_Type (T : Entity_Id) return Boolean is
325 function Find_Hardbool_Pragma
326 (Id : Entity_Id) return Node_Id;
327 -- Return a Rep_Item associated with entity Id that
328 -- corresponds to the Hardbool Machine_Attribute pragma, if
329 -- any, or Empty otherwise.
331 function Pragma_Arg_To_String (Item : Node_Id) return String is
332 (To_String (Strval (Expr_Value_S (Item))));
333 -- Return the pragma argument Item as a String
335 function Hardbool_Pragma_P (Item : Node_Id) return Boolean is
336 (Nkind (Item) = N_Pragma
337 and then
338 Pragma_Name (Item) = Name_Machine_Attribute
339 and then
340 Pragma_Arg_To_String
341 (Get_Pragma_Arg
342 (Next (First (Pragma_Argument_Associations (Item)))))
343 = "hardbool");
344 -- Return True iff representation Item is a "hardbool"
345 -- Machine_Attribute pragma.
347 --------------------------
348 -- Find_Hardbool_Pragma --
349 --------------------------
351 function Find_Hardbool_Pragma
352 (Id : Entity_Id) return Node_Id
354 Item : Node_Id;
356 begin
357 if not Has_Gigi_Rep_Item (Id) then
358 return Empty;
359 end if;
361 Item := First_Rep_Item (Id);
362 while Present (Item) loop
363 if Hardbool_Pragma_P (Item) then
364 return Item;
365 end if;
366 Item := Next_Rep_Item (Item);
367 end loop;
369 return Empty;
370 end Find_Hardbool_Pragma;
372 -- Start of processing for Is_Hardbool_Type
374 begin
375 return Present (Find_Hardbool_Pragma (T));
376 end Is_Hardbool_Type;
378 -- Start of processing for Adjust_Condition
380 begin
381 if No (N) then
382 return;
383 end if;
385 declare
386 Loc : constant Source_Ptr := Sloc (N);
387 T : constant Entity_Id := Etype (N);
389 begin
390 -- Defend against a call where the argument has no type, or has a
391 -- type that is not Boolean. This can occur because of prior errors.
393 if No (T) or else not Is_Boolean_Type (T) then
394 return;
395 end if;
397 -- Apply validity checking if needed
399 if Validity_Checks_On
400 and then
401 (Validity_Check_Tests or else Is_Hardbool_Type (T))
403 -- no check needed here if validity has already been checked
404 and then not
405 (Validity_Check_Operands and then
406 (Nkind (N) in N_Op or else Nkind (Parent (N)) in N_Op))
407 then
408 Ensure_Valid (N);
409 end if;
411 -- Immediate return if standard boolean, the most common case,
412 -- where nothing needs to be done.
414 if Base_Type (T) = Standard_Boolean then
415 return;
416 end if;
418 -- Case of zero/nonzero semantics or nonstandard enumeration
419 -- representation. In each case, we rewrite the node as:
421 -- ityp!(N) /= False'Enum_Rep
423 -- where ityp is an integer type with large enough size to hold any
424 -- value of type T.
426 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
427 Rewrite (N,
428 Make_Op_Ne (Loc,
429 Left_Opnd =>
430 Unchecked_Convert_To
431 (Integer_Type_For (Esize (T), Uns => False), N),
432 Right_Opnd =>
433 Make_Attribute_Reference (Loc,
434 Attribute_Name => Name_Enum_Rep,
435 Prefix =>
436 New_Occurrence_Of (First_Literal (T), Loc))));
437 Analyze_And_Resolve (N, Standard_Boolean);
439 else
440 Rewrite (N, Convert_To (Standard_Boolean, N));
441 Analyze_And_Resolve (N, Standard_Boolean);
442 end if;
443 end;
444 end Adjust_Condition;
446 ------------------------
447 -- Adjust_Result_Type --
448 ------------------------
450 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
451 begin
452 -- Ignore call if current type is not Standard.Boolean
454 if Etype (N) /= Standard_Boolean then
455 return;
456 end if;
458 -- If result is already of correct type, nothing to do. Note that
459 -- this will get the most common case where everything has a type
460 -- of Standard.Boolean.
462 if Base_Type (T) = Standard_Boolean then
463 return;
465 else
466 declare
467 KP : constant Node_Kind := Nkind (Parent (N));
469 begin
470 -- If result is to be used as a Condition in the syntax, no need
471 -- to convert it back, since if it was changed to Standard.Boolean
472 -- using Adjust_Condition, that is just fine for this usage.
474 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
475 return;
477 -- If result is an operand of another logical operation, no need
478 -- to reset its type, since Standard.Boolean is just fine, and
479 -- such operations always do Adjust_Condition on their operands.
481 elsif KP in N_Op_Boolean
482 or else KP in N_Short_Circuit
483 or else KP = N_Op_Not
484 or else (KP in N_Type_Conversion
485 | N_Unchecked_Type_Conversion
486 and then Is_Boolean_Type (Etype (Parent (N))))
487 then
488 return;
490 -- Otherwise we perform a conversion from the current type, which
491 -- must be Standard.Boolean, to the desired type. Use the base
492 -- type to prevent spurious constraint checks that are extraneous
493 -- to the transformation. The type and its base have the same
494 -- representation, standard or otherwise.
496 else
497 Set_Analyzed (N);
498 Rewrite (N, Convert_To (Base_Type (T), N));
499 Analyze_And_Resolve (N, Base_Type (T));
500 end if;
501 end;
502 end if;
503 end Adjust_Result_Type;
505 --------------------------
506 -- Append_Freeze_Action --
507 --------------------------
509 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
510 Fnode : Node_Id;
512 begin
513 Ensure_Freeze_Node (T);
514 Fnode := Freeze_Node (T);
516 if No (Actions (Fnode)) then
517 Set_Actions (Fnode, New_List (N));
518 else
519 Append (N, Actions (Fnode));
520 end if;
521 end Append_Freeze_Action;
523 ---------------------------
524 -- Append_Freeze_Actions --
525 ---------------------------
527 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
528 Fnode : Node_Id;
530 begin
531 if No (L) then
532 return;
533 end if;
535 Ensure_Freeze_Node (T);
536 Fnode := Freeze_Node (T);
538 if No (Actions (Fnode)) then
539 Set_Actions (Fnode, L);
540 else
541 Append_List (L, Actions (Fnode));
542 end if;
543 end Append_Freeze_Actions;
545 ----------------------------------------
546 -- Attribute_Constrained_Static_Value --
547 ----------------------------------------
549 function Attribute_Constrained_Static_Value (Pref : Node_Id) return Boolean
551 Ptyp : constant Entity_Id := Etype (Pref);
552 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
554 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
555 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
556 -- view of an aliased object whose subtype is constrained.
558 ---------------------------------
559 -- Is_Constrained_Aliased_View --
560 ---------------------------------
562 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
563 E : Entity_Id;
565 begin
566 if Is_Entity_Name (Obj) then
567 E := Entity (Obj);
569 if Present (Renamed_Object (E)) then
570 return Is_Constrained_Aliased_View (Renamed_Object (E));
571 else
572 return Is_Aliased (E) and then Is_Constrained (Etype (E));
573 end if;
575 else
576 return Is_Aliased_View (Obj)
577 and then
578 (Is_Constrained (Etype (Obj))
579 or else
580 (Nkind (Obj) = N_Explicit_Dereference
581 and then
582 not Object_Type_Has_Constrained_Partial_View
583 (Typ => Base_Type (Etype (Obj)),
584 Scop => Current_Scope)));
585 end if;
586 end Is_Constrained_Aliased_View;
588 -- Start of processing for Attribute_Constrained_Static_Value
590 begin
591 -- We are in a case where the attribute is known statically, and
592 -- implicit dereferences have been rewritten.
594 pragma Assert
595 (not (Present (Formal_Ent)
596 and then Ekind (Formal_Ent) /= E_Constant
597 and then Present (Extra_Constrained (Formal_Ent)))
598 and then
599 not (Is_Access_Type (Etype (Pref))
600 and then (not Is_Entity_Name (Pref)
601 or else Is_Object (Entity (Pref))))
602 and then
603 not (Nkind (Pref) = N_Identifier
604 and then Ekind (Entity (Pref)) = E_Variable
605 and then Present (Extra_Constrained (Entity (Pref)))));
607 if Is_Entity_Name (Pref) then
608 declare
609 Ent : constant Entity_Id := Entity (Pref);
610 Res : Boolean;
612 begin
613 -- (RM J.4) obsolescent cases
615 if Is_Type (Ent) then
617 -- Private type
619 if Is_Private_Type (Ent) then
620 Res := not Has_Discriminants (Ent)
621 or else Is_Constrained (Ent);
623 -- It not a private type, must be a generic actual type
624 -- that corresponded to a private type. We know that this
625 -- correspondence holds, since otherwise the reference
626 -- within the generic template would have been illegal.
628 else
629 if Is_Composite_Type (Underlying_Type (Ent)) then
630 Res := Is_Constrained (Ent);
631 else
632 Res := True;
633 end if;
634 end if;
636 else
638 -- If the prefix is not a variable or is aliased, then
639 -- definitely true; if it's a formal parameter without an
640 -- associated extra formal, then treat it as constrained.
642 -- Ada 2005 (AI-363): An aliased prefix must be known to be
643 -- constrained in order to set the attribute to True.
645 if not Is_Variable (Pref)
646 or else Present (Formal_Ent)
647 or else (Ada_Version < Ada_2005
648 and then Is_Aliased_View (Pref))
649 or else (Ada_Version >= Ada_2005
650 and then Is_Constrained_Aliased_View (Pref))
651 then
652 Res := True;
654 -- Variable case, look at type to see if it is constrained.
655 -- Note that the one case where this is not accurate (the
656 -- procedure formal case), has been handled above.
658 -- We use the Underlying_Type here (and below) in case the
659 -- type is private without discriminants, but the full type
660 -- has discriminants. This case is illegal, but we generate
661 -- it internally for passing to the Extra_Constrained
662 -- parameter.
664 else
665 -- In Ada 2012, test for case of a limited tagged type,
666 -- in which case the attribute is always required to
667 -- return True. The underlying type is tested, to make
668 -- sure we also return True for cases where there is an
669 -- unconstrained object with an untagged limited partial
670 -- view which has defaulted discriminants (such objects
671 -- always produce a False in earlier versions of
672 -- Ada). (Ada 2012: AI05-0214)
674 Res :=
675 Is_Constrained (Underlying_Type (Etype (Ent)))
676 or else
677 (Ada_Version >= Ada_2012
678 and then Is_Tagged_Type (Underlying_Type (Ptyp))
679 and then Is_Limited_Type (Ptyp));
680 end if;
681 end if;
683 return Res;
684 end;
686 -- Prefix is not an entity name. These are also cases where we can
687 -- always tell at compile time by looking at the form and type of the
688 -- prefix. If an explicit dereference of an object with constrained
689 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
690 -- underlying type is a limited tagged type, then Constrained is
691 -- required to always return True (Ada 2012: AI05-0214).
693 else
694 return not Is_Variable (Pref)
695 or else
696 (Nkind (Pref) = N_Explicit_Dereference
697 and then
698 not Object_Type_Has_Constrained_Partial_View
699 (Typ => Base_Type (Ptyp),
700 Scop => Current_Scope))
701 or else Is_Constrained (Underlying_Type (Ptyp))
702 or else (Ada_Version >= Ada_2012
703 and then Is_Tagged_Type (Underlying_Type (Ptyp))
704 and then Is_Limited_Type (Ptyp));
705 end if;
706 end Attribute_Constrained_Static_Value;
708 ------------------------------------
709 -- Build_Allocate_Deallocate_Proc --
710 ------------------------------------
712 procedure Build_Allocate_Deallocate_Proc
713 (N : Node_Id;
714 Mark : Node_Id := Empty)
716 Is_Allocate : constant Boolean := Nkind (N) /= N_Free_Statement;
718 function Find_Object (E : Node_Id) return Node_Id;
719 -- Given an arbitrary expression of an allocator, try to find an object
720 -- reference in it, otherwise return the original expression.
722 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
723 -- Determine whether subprogram Subp denotes a custom allocate or
724 -- deallocate.
726 -----------------
727 -- Find_Object --
728 -----------------
730 function Find_Object (E : Node_Id) return Node_Id is
731 Expr : Node_Id;
733 begin
734 pragma Assert (Is_Allocate);
736 Expr := E;
737 loop
738 if Nkind (Expr) = N_Explicit_Dereference then
739 Expr := Prefix (Expr);
741 elsif Nkind (Expr) = N_Qualified_Expression then
742 Expr := Expression (Expr);
744 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
746 -- When interface class-wide types are involved in allocation,
747 -- the expander introduces several levels of address arithmetic
748 -- to perform dispatch table displacement. In this scenario the
749 -- object appears as:
751 -- Tag_Ptr (Base_Address (<object>'Address))
753 -- Detect this case and utilize the whole expression as the
754 -- "object" since it now points to the proper dispatch table.
756 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
757 exit;
759 -- Continue to strip the object
761 else
762 Expr := Expression (Expr);
763 end if;
765 else
766 exit;
767 end if;
768 end loop;
770 return Expr;
771 end Find_Object;
773 ---------------------------------
774 -- Is_Allocate_Deallocate_Proc --
775 ---------------------------------
777 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
778 begin
779 -- Look for a subprogram body with only one statement which is a
780 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
782 if Ekind (Subp) = E_Procedure
783 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
784 then
785 declare
786 HSS : constant Node_Id :=
787 Handled_Statement_Sequence (Parent (Parent (Subp)));
788 Proc : Entity_Id;
790 begin
791 if Present (Statements (HSS))
792 and then Nkind (First (Statements (HSS))) =
793 N_Procedure_Call_Statement
794 then
795 Proc := Entity (Name (First (Statements (HSS))));
797 return
798 Is_RTE (Proc, RE_Allocate_Any_Controlled)
799 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
800 end if;
801 end;
802 end if;
804 return False;
805 end Is_Allocate_Deallocate_Proc;
807 -- Local variables
809 Desig_Typ : Entity_Id;
810 Expr : Node_Id;
811 Needs_Fin : Boolean;
812 Pool_Id : Entity_Id;
813 Proc_To_Call : Node_Id;
814 Ptr_Typ : Entity_Id;
815 Use_Secondary_Stack_Pool : Boolean;
817 -- Start of processing for Build_Allocate_Deallocate_Proc
819 begin
820 -- Obtain the attributes of the allocation
822 if Is_Allocate then
823 if Nkind (N) in N_Assignment_Statement | N_Object_Declaration then
824 Expr := Expression (N);
825 else
826 Expr := N;
827 end if;
829 -- Deal with type conversions created for interface types
831 if Nkind (Expr) = N_Unchecked_Type_Conversion then
832 Expr := Expression (Expr);
833 end if;
835 -- In certain cases, an allocator with a qualified expression may be
836 -- relocated and used as the initialization expression of a temporary
837 -- and the analysis of the declaration of this temporary may in turn
838 -- create another temporary:
840 -- before:
841 -- Obj : Ptr_Typ := new Desig_Typ'(...);
843 -- after:
844 -- Tmp2 : Ptr_Typ := new Desig_Typ'(...);
845 -- [constraint_error when Tmp2...]
846 -- Tmp1 : Ptr_Typ := Tmp2
847 -- Obj : Ptr_Typ := Tmp1;
849 -- Detect this case where we are invoked on Tmp1's declaration by
850 -- recognizing Tmp2 and then proceed to its declaration instead.
852 if Nkind (Expr) = N_Identifier
853 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
854 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
855 then
856 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), Mark);
857 return;
858 end if;
860 pragma Assert (Nkind (Expr) = N_Allocator);
862 Ptr_Typ := Base_Type (Etype (Expr));
863 Proc_To_Call := Procedure_To_Call (Expr);
865 -- Obtain the attributes of the deallocation
867 else
868 Expr := Expression (N);
869 Ptr_Typ := Base_Type (Etype (Expr));
870 Proc_To_Call := Procedure_To_Call (N);
871 end if;
873 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
874 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
876 -- Handle concurrent types
878 if Is_Concurrent_Type (Desig_Typ)
879 and then Present (Corresponding_Record_Type (Desig_Typ))
880 then
881 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
882 end if;
884 Use_Secondary_Stack_Pool :=
885 Is_RTE (Pool_Id, RE_SS_Pool)
886 or else (Nkind (Expr) = N_Allocator
887 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool));
889 -- Do not process allocations / deallocations without a pool
891 if No (Pool_Id) then
892 return;
894 -- Do not process allocations from the return stack
896 elsif Is_RTE (Pool_Id, RE_RS_Pool) then
897 return;
899 -- Do not process allocations on / deallocations from the secondary
900 -- stack, except for access types used to implement indirect temps.
902 elsif Use_Secondary_Stack_Pool
903 and then not Old_Attr_Util.Indirect_Temps
904 .Is_Access_Type_For_Indirect_Temp (Ptr_Typ)
905 then
906 return;
908 -- Optimize the case where we are using the default Global_Pool_Object,
909 -- and we don't need the heavy finalization machinery.
911 elsif Is_RTE (Pool_Id, RE_Global_Pool_Object)
912 and then not Needs_Finalization (Desig_Typ)
913 then
914 return;
916 -- Do not replicate the machinery if the allocator / free has already
917 -- been expanded and has a custom Allocate / Deallocate.
919 elsif Present (Proc_To_Call)
920 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
921 then
922 return;
923 end if;
925 -- Finalization actions are required when the object to be allocated or
926 -- deallocated needs these actions and the associated access type is not
927 -- subject to pragma No_Heap_Finalization.
929 Needs_Fin :=
930 Needs_Finalization (Desig_Typ)
931 and then not Has_Relaxed_Finalization (Desig_Typ)
932 and then not No_Heap_Finalization (Ptr_Typ);
934 -- The allocation/deallocation of a controlled object must be associated
935 -- with an attachment to/detachment from a finalization collection, but
936 -- the implementation cannot guarantee this property for every anonymous
937 -- access type, see Build_Anonymous_Collection.
939 if Needs_Fin and then No (Finalization_Collection (Ptr_Typ)) then
940 pragma Assert (Ekind (Ptr_Typ) = E_Anonymous_Access_Type);
941 Needs_Fin := False;
942 end if;
944 if Needs_Fin then
946 -- Do nothing if the access type may never allocate / deallocate
947 -- objects.
949 if No_Pool_Assigned (Ptr_Typ) then
950 return;
951 end if;
953 -- The only other kind of allocation / deallocation supported by this
954 -- routine is on / from a subpool.
956 elsif Nkind (Expr) = N_Allocator
957 and then No (Subpool_Handle_Name (Expr))
958 then
959 return;
960 end if;
962 declare
963 Loc : constant Source_Ptr := Sloc (N);
964 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
965 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
966 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
967 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
969 Actuals : List_Id;
970 Alloc_Expr : Node_Id := Empty;
971 Fin_Coll_Id : Entity_Id;
972 Proc_To_Call : Entity_Id;
973 Ptr_Coll_Id : Entity_Id;
974 Subpool : Node_Id := Empty;
976 begin
977 -- When we are building an allocator procedure, extract the allocator
978 -- node for later processing and calculation of alignment.
980 if Is_Allocate then
981 -- Extract the qualified expression if there is one from the
982 -- allocator.
984 if Nkind (Expression (Expr)) = N_Qualified_Expression then
985 Alloc_Expr := Expression (Expr);
986 end if;
987 end if;
989 -- Step 1: Construct all the actuals for the call to library routine
990 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
992 -- a) Storage pool
994 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
996 if Is_Allocate then
998 -- b) Subpool
1000 if Nkind (Expr) = N_Allocator then
1001 Subpool := Subpool_Handle_Name (Expr);
1002 end if;
1004 -- If a subpool is present it can be an arbitrary name, so make
1005 -- the actual by copying the tree.
1007 if Present (Subpool) then
1008 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
1009 else
1010 Append_To (Actuals, Make_Null (Loc));
1011 end if;
1013 -- c) Finalization collection
1015 Fin_Coll_Id := Make_Temporary (Loc, 'C');
1016 Ptr_Coll_Id := Finalization_Collection (Ptr_Typ);
1018 -- Create the temporary which represents the collection of
1019 -- the expression. Generate:
1021 -- C : Finalization_Collection_Ptr :=
1022 -- Finalization_Collection (Ptr_Typ)'Access
1024 -- Handle the case where a collection is actually a pointer
1025 -- to a collection. This arises in build-in-place functions.
1027 Insert_Action (N,
1028 Make_Object_Declaration (Loc,
1029 Defining_Identifier => Fin_Coll_Id,
1030 Object_Definition =>
1031 New_Occurrence_Of
1032 (RTE (RE_Finalization_Collection_Ptr), Loc),
1033 Expression =>
1034 (if not Needs_Fin
1035 then Make_Null (Loc)
1036 elsif Is_Access_Type (Etype (Ptr_Coll_Id))
1037 then New_Occurrence_Of (Ptr_Coll_Id, Loc)
1038 else
1039 Make_Attribute_Reference (Loc,
1040 Prefix =>
1041 New_Occurrence_Of (Ptr_Coll_Id, Loc),
1042 Attribute_Name => Name_Unrestricted_Access))));
1044 Append_To (Actuals, New_Occurrence_Of (Fin_Coll_Id, Loc));
1045 end if;
1047 -- d) Address
1048 -- e) Storage_Size
1049 -- f) Alignment
1051 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
1052 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
1054 -- Class-wide allocations without expressions and non-class-wide
1055 -- allocations can be performed without getting the alignment from
1056 -- the type's Type Specific Record.
1058 if (Is_Allocate and then No (Alloc_Expr))
1059 or else not Is_Class_Wide_Type (Desig_Typ)
1060 then
1061 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
1063 -- For operations on class-wide types we obtain the value of
1064 -- alignment from the Type Specific Record of the relevant object.
1065 -- This is needed because the frontend expansion of class-wide types
1066 -- into equivalent types confuses the back end.
1068 else
1069 -- Generate:
1070 -- Obj.all'Alignment
1071 -- or
1072 -- Alloc_Expr'Alignment
1074 -- ... because 'Alignment applied to class-wide types is expanded
1075 -- into the code that reads the value of alignment from the TSD
1076 -- (see Expand_N_Attribute_Reference)
1078 Append_To (Actuals,
1079 Unchecked_Convert_To (RTE (RE_Storage_Offset),
1080 Make_Attribute_Reference (Loc,
1081 Prefix =>
1082 (if No (Alloc_Expr) then
1083 Make_Explicit_Dereference (Loc, Relocate_Node (Expr))
1084 else
1085 Relocate_Node (Expression (Alloc_Expr))),
1086 Attribute_Name => Name_Alignment)));
1087 end if;
1089 -- g) Is_Controlled
1091 if Needs_Fin then
1092 Is_Controlled : declare
1093 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
1095 Flag_Expr : Node_Id;
1096 Param : Node_Id;
1097 Pref : Node_Id;
1098 Temp : Node_Id;
1100 begin
1101 if Is_Allocate then
1102 Temp := Find_Object (Expression (Expr));
1103 else
1104 Temp := Expr;
1105 end if;
1107 -- Processing for allocations where the expression is a subtype
1108 -- indication.
1110 if Is_Allocate
1111 and then Is_Entity_Name (Temp)
1112 and then Is_Type (Entity (Temp))
1113 then
1114 Flag_Expr :=
1115 New_Occurrence_Of
1116 (Boolean_Literals
1117 (Needs_Finalization (Entity (Temp))), Loc);
1119 -- The allocation / deallocation of a class-wide object relies
1120 -- on a runtime check to determine whether the object is truly
1121 -- controlled or not. Depending on this check, the finalization
1122 -- machinery will request or reclaim extra storage reserved for
1123 -- a list header.
1125 elsif Is_Class_Wide_Type (Desig_Typ) then
1127 -- Detect a special case where interface class-wide types
1128 -- are involved as the object appears as:
1130 -- Tag_Ptr (Base_Address (<object>'Address))
1132 -- The expression already yields the proper tag, generate:
1134 -- Temp.all
1136 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
1137 Param :=
1138 Make_Explicit_Dereference (Loc,
1139 Prefix => Relocate_Node (Temp));
1141 -- In the default case, obtain the tag of the object about
1142 -- to be allocated / deallocated. Generate:
1144 -- Temp'Tag
1146 -- If the object is an unchecked conversion (typically to
1147 -- an access to class-wide type), we must preserve the
1148 -- conversion to ensure that the object is seen as tagged
1149 -- in the code that follows.
1151 else
1152 Pref := Temp;
1154 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
1155 then
1156 Pref := Parent (Pref);
1157 end if;
1159 Param :=
1160 Make_Attribute_Reference (Loc,
1161 Prefix => Relocate_Node (Pref),
1162 Attribute_Name => Name_Tag);
1163 end if;
1165 -- Generate:
1166 -- Needs_Finalization (<Param>)
1168 Flag_Expr :=
1169 Make_Function_Call (Loc,
1170 Name =>
1171 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
1172 Parameter_Associations => New_List (Param));
1174 -- Processing for generic actuals
1176 elsif Is_Generic_Actual_Type (Desig_Typ) then
1177 Flag_Expr :=
1178 New_Occurrence_Of (Boolean_Literals
1179 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
1181 -- The object does not require any specialized checks, it is
1182 -- known to be controlled.
1184 else
1185 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
1186 end if;
1188 -- Create the temporary which represents the finalization state
1189 -- of the expression. Generate:
1191 -- F : constant Boolean := <Flag_Expr>;
1193 Insert_Action (N,
1194 Make_Object_Declaration (Loc,
1195 Defining_Identifier => Flag_Id,
1196 Constant_Present => True,
1197 Object_Definition =>
1198 New_Occurrence_Of (Standard_Boolean, Loc),
1199 Expression => Flag_Expr));
1201 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
1203 -- Finalize_Address is not generated in CodePeer mode because
1204 -- the body contains address arithmetic. So we don't want to
1205 -- generate the attach or detach in this case.
1207 if CodePeer_Mode then
1208 null;
1210 -- Nothing to generate if the flag is statically false
1212 elsif Is_Entity_Name (Flag_Expr)
1213 and then Entity (Flag_Expr) = Standard_False
1214 then
1215 null;
1217 -- Generate:
1218 -- if F then
1219 -- Attach_Object_To_Collection
1220 -- (Temp.all'Address,
1221 -- Desig_Typ_FD'Access,
1222 -- Fin_Coll_Id.all);
1223 -- end if;
1225 elsif Is_Allocate then
1226 declare
1227 Stmt : Node_Id;
1228 Temp : Entity_Id;
1230 begin
1231 -- The original allocator must have been rewritten by
1232 -- the caller at this point and a temporary introduced.
1234 case Nkind (N) is
1235 when N_Assignment_Statement =>
1236 Temp := New_Copy_Tree (Name (N));
1238 when N_Object_Declaration =>
1239 Temp :=
1240 New_Occurrence_Of (Defining_Identifier (N), Loc);
1242 when others =>
1243 raise Program_Error;
1244 end case;
1246 Stmt :=
1247 Make_If_Statement (Loc,
1248 Condition =>
1249 New_Occurrence_Of (Flag_Id, Loc),
1250 Then_Statements => New_List (
1251 Make_Procedure_Call_Statement (Loc,
1252 Name =>
1253 New_Occurrence_Of
1254 (RTE (RE_Attach_Object_To_Collection), Loc),
1255 Parameter_Associations => New_List (
1256 Make_Address_For_Finalize (Loc,
1257 Make_Explicit_Dereference (Loc, Temp),
1258 Desig_Typ),
1259 Make_Attribute_Reference (Loc,
1260 Prefix =>
1261 New_Occurrence_Of
1262 (Finalize_Address (Desig_Typ), Loc),
1263 Attribute_Name => Name_Unrestricted_Access),
1264 Make_Explicit_Dereference (Loc,
1265 New_Occurrence_Of (Fin_Coll_Id, Loc))))));
1267 -- If we have a mark past the initialization, then insert
1268 -- the statement there, otherwise insert after either the
1269 -- assignment or the last initialization statement of the
1270 -- declaration of the temporary.
1272 if Present (Mark) then
1273 Insert_Action (Mark, Stmt, Suppress => All_Checks);
1275 elsif Nkind (N) = N_Assignment_Statement then
1276 Insert_After_And_Analyze
1277 (N, Stmt, Suppress => All_Checks);
1279 else
1280 Insert_After_And_Analyze
1281 (Find_Last_Init (N), Stmt, Suppress => All_Checks);
1282 end if;
1283 end;
1285 -- Generate:
1286 -- if F then
1287 -- Detach_Object_From_Collection (Temp.all'Address);
1288 -- end if;
1290 else
1291 Insert_Action (N,
1292 Make_If_Statement (Loc,
1293 Condition => New_Occurrence_Of (Flag_Id, Loc),
1294 Then_Statements => New_List (
1295 Make_Procedure_Call_Statement (Loc,
1296 Name =>
1297 New_Occurrence_Of
1298 (RTE (RE_Detach_Object_From_Collection), Loc),
1299 Parameter_Associations => New_List (
1300 Make_Address_For_Finalize (Loc,
1301 Make_Explicit_Dereference (Loc,
1302 New_Occurrence_Of
1303 (Entity (Expression (N)), Loc)),
1304 Desig_Typ))))),
1305 Suppress => All_Checks);
1306 end if;
1308 end Is_Controlled;
1310 -- The object is not controlled
1312 else
1313 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
1314 end if;
1316 -- h) On_Subpool
1318 if Is_Allocate then
1319 Append_To (Actuals,
1320 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
1321 end if;
1323 -- Step 2: Build a wrapper Allocate / Deallocate which internally
1324 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
1326 -- Select the proper routine to call
1328 if Is_Allocate then
1329 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
1330 else
1331 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
1332 end if;
1334 -- Create a custom Allocate/Deallocate routine which has identical
1335 -- profile to that of System.Storage_Pools, except for a secondary
1336 -- stack allocation where the profile must be identical to that of
1337 -- the System.Secondary_Stack.SS_Allocate procedure (deallocation
1338 -- is not supported for the secondary stack).
1340 declare
1341 function Pool_Param return Node_Id is (
1342 Make_Parameter_Specification (Loc,
1343 Defining_Identifier => Make_Temporary (Loc, 'P'),
1344 Parameter_Type =>
1345 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)));
1346 -- P : Root_Storage_Pool
1348 function Address_Param return Node_Id is (
1349 Make_Parameter_Specification (Loc,
1350 Defining_Identifier => Addr_Id,
1351 Out_Present => Is_Allocate,
1352 Parameter_Type =>
1353 New_Occurrence_Of (RTE (RE_Address), Loc)));
1354 -- A : [out] Address
1356 function Size_Param return Node_Id is (
1357 Make_Parameter_Specification (Loc,
1358 Defining_Identifier => Size_Id,
1359 Parameter_Type =>
1360 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1361 -- S : Storage_Count
1363 function Alignment_Param return Node_Id is (
1364 Make_Parameter_Specification (Loc,
1365 Defining_Identifier => Alig_Id,
1366 Parameter_Type =>
1367 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1368 -- L : Storage_Count
1370 Formal_Params : constant List_Id :=
1371 (if Use_Secondary_Stack_Pool
1372 then New_List (Address_Param, Size_Param, Alignment_Param)
1373 else
1374 New_List
1375 (Pool_Param, Address_Param, Size_Param, Alignment_Param));
1376 -- The list of formal parameters of the routine
1378 begin
1379 Insert_Action (N,
1380 Make_Subprogram_Body (Loc,
1381 Specification =>
1382 -- procedure Pnn
1383 Make_Procedure_Specification (Loc,
1384 Defining_Unit_Name => Proc_Id,
1385 Parameter_Specifications => Formal_Params),
1387 Declarations => No_List,
1389 Handled_Statement_Sequence =>
1390 Make_Handled_Sequence_Of_Statements (Loc,
1391 Statements => New_List (
1392 Make_Procedure_Call_Statement (Loc,
1393 Name =>
1394 New_Occurrence_Of (Proc_To_Call, Loc),
1395 Parameter_Associations => Actuals)))),
1396 Suppress => All_Checks);
1397 end;
1399 -- The newly generated Allocate / Deallocate becomes the default
1400 -- procedure to call when the back end processes the allocation /
1401 -- deallocation.
1403 if Is_Allocate then
1404 Set_Procedure_To_Call (Expr, Proc_Id);
1405 else
1406 Set_Procedure_To_Call (N, Proc_Id);
1407 end if;
1408 end;
1409 end Build_Allocate_Deallocate_Proc;
1411 -------------------------------
1412 -- Build_Abort_Undefer_Block --
1413 -------------------------------
1415 function Build_Abort_Undefer_Block
1416 (Loc : Source_Ptr;
1417 Stmts : List_Id;
1418 Context : Node_Id) return Node_Id
1420 Exceptions_OK : constant Boolean :=
1421 not Restriction_Active (No_Exception_Propagation);
1423 AUD : Entity_Id;
1424 Blk : Node_Id;
1425 Blk_Id : Entity_Id;
1426 HSS : Node_Id;
1428 begin
1429 -- The block should be generated only when undeferring abort in the
1430 -- context of a potential exception.
1432 pragma Assert (Abort_Allowed and Exceptions_OK);
1434 -- Generate:
1435 -- begin
1436 -- <Stmts>
1437 -- at end
1438 -- Abort_Undefer_Direct;
1439 -- end;
1441 AUD := RTE (RE_Abort_Undefer_Direct);
1443 HSS :=
1444 Make_Handled_Sequence_Of_Statements (Loc,
1445 Statements => Stmts,
1446 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1448 Blk :=
1449 Make_Block_Statement (Loc,
1450 Handled_Statement_Sequence => HSS);
1451 Set_Is_Abort_Block (Blk);
1453 Add_Block_Identifier (Blk, Blk_Id);
1454 Expand_At_End_Handler (HSS, Blk_Id);
1456 -- Present the Abort_Undefer_Direct function to the back end to inline
1457 -- the call to the routine.
1459 Add_Inlined_Body (AUD, Context);
1461 return Blk;
1462 end Build_Abort_Undefer_Block;
1464 ---------------------------------
1465 -- Build_Class_Wide_Expression --
1466 ---------------------------------
1468 procedure Build_Class_Wide_Expression
1469 (Pragma_Or_Expr : Node_Id;
1470 Subp : Entity_Id;
1471 Par_Subp : Entity_Id;
1472 Adjust_Sloc : Boolean)
1474 function Replace_Entity (N : Node_Id) return Traverse_Result;
1475 -- Replace reference to formal of inherited operation or to primitive
1476 -- operation of root type, with corresponding entity for derived type,
1477 -- when constructing the class-wide condition of an overriding
1478 -- subprogram.
1480 --------------------
1481 -- Replace_Entity --
1482 --------------------
1484 function Replace_Entity (N : Node_Id) return Traverse_Result is
1485 New_E : Entity_Id;
1487 begin
1488 if Adjust_Sloc then
1489 Adjust_Inherited_Pragma_Sloc (N);
1490 end if;
1492 if Nkind (N) in N_Identifier | N_Expanded_Name | N_Operator_Symbol
1493 and then Present (Entity (N))
1494 and then
1495 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1496 and then
1497 (Nkind (Parent (N)) /= N_Attribute_Reference
1498 or else Attribute_Name (Parent (N)) /= Name_Class)
1499 then
1500 -- The replacement does not apply to dispatching calls within the
1501 -- condition, but only to calls whose static tag is that of the
1502 -- parent type.
1504 if Is_Subprogram (Entity (N))
1505 and then Nkind (Parent (N)) = N_Function_Call
1506 and then Present (Controlling_Argument (Parent (N)))
1507 then
1508 return OK;
1509 end if;
1511 -- Determine whether entity has a renaming
1513 New_E := Type_Map.Get (Entity (N));
1515 if Present (New_E) then
1516 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1517 end if;
1519 -- Update type of function call node, which should be the same as
1520 -- the function's return type.
1522 if Is_Subprogram (Entity (N))
1523 and then Nkind (Parent (N)) = N_Function_Call
1524 then
1525 Set_Etype (Parent (N), Etype (Entity (N)));
1526 end if;
1528 -- The whole expression will be reanalyzed
1530 elsif Nkind (N) in N_Has_Etype then
1531 Set_Analyzed (N, False);
1532 end if;
1534 return OK;
1535 end Replace_Entity;
1537 procedure Replace_Condition_Entities is
1538 new Traverse_Proc (Replace_Entity);
1540 -- Local variables
1542 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Par_Subp);
1543 Subp_Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1545 -- Start of processing for Build_Class_Wide_Expression
1547 begin
1548 pragma Assert (Par_Typ /= Subp_Typ);
1550 Update_Primitives_Mapping (Par_Subp, Subp);
1551 Map_Formals (Par_Subp, Subp);
1552 Replace_Condition_Entities (Pragma_Or_Expr);
1553 end Build_Class_Wide_Expression;
1555 --------------------
1556 -- Build_DIC_Call --
1557 --------------------
1559 function Build_DIC_Call
1560 (Loc : Source_Ptr;
1561 Obj_Name : Node_Id;
1562 Typ : Entity_Id) return Node_Id
1564 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1565 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1567 begin
1568 -- The DIC procedure has a null body if assertions are disabled or
1569 -- Assertion_Policy Ignore is in effect. In that case, it would be
1570 -- nice to generate a null statement instead of a call to the DIC
1571 -- procedure, but doing that seems to interfere with the determination
1572 -- of ECRs (early call regions) in SPARK. ???
1574 return
1575 Make_Procedure_Call_Statement (Loc,
1576 Name => New_Occurrence_Of (Proc_Id, Loc),
1577 Parameter_Associations => New_List (
1578 Unchecked_Convert_To (Formal_Typ, Obj_Name)));
1579 end Build_DIC_Call;
1581 ------------------------------
1582 -- Build_DIC_Procedure_Body --
1583 ------------------------------
1585 -- WARNING: This routine manages Ghost regions. Return statements must be
1586 -- replaced by gotos which jump to the end of the routine and restore the
1587 -- Ghost mode.
1589 procedure Build_DIC_Procedure_Body
1590 (Typ : Entity_Id;
1591 Partial_DIC : Boolean := False)
1593 Pragmas_Seen : Elist_Id := No_Elist;
1594 -- This list contains all DIC pragmas processed so far. The list is used
1595 -- to avoid redundant Default_Initial_Condition checks.
1597 procedure Add_DIC_Check
1598 (DIC_Prag : Node_Id;
1599 DIC_Expr : Node_Id;
1600 Stmts : in out List_Id);
1601 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1602 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1603 -- is added to list Stmts.
1605 procedure Add_Inherited_DIC
1606 (DIC_Prag : Node_Id;
1607 Par_Typ : Entity_Id;
1608 Deriv_Typ : Entity_Id;
1609 Stmts : in out List_Id);
1610 -- Add a runtime check to verify the assertion expression of inherited
1611 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1612 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1613 -- pragma. All generated code is added to list Stmts.
1615 procedure Add_Inherited_Tagged_DIC
1616 (DIC_Prag : Node_Id;
1617 Expr : Node_Id;
1618 Stmts : in out List_Id);
1619 -- Add a runtime check to verify assertion expression DIC_Expr of
1620 -- inherited pragma DIC_Prag. This routine applies class-wide pre-
1621 -- and postcondition-like runtime semantics to the check. Expr is
1622 -- the assertion expression after substitution has been performed
1623 -- (via Replace_References). All generated code is added to list Stmts.
1625 procedure Add_Inherited_DICs
1626 (T : Entity_Id;
1627 Priv_Typ : Entity_Id;
1628 Full_Typ : Entity_Id;
1629 Obj_Id : Entity_Id;
1630 Checks : in out List_Id);
1631 -- Generate a DIC check for each inherited Default_Initial_Condition
1632 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
1633 -- the partial and full view of the parent type. Obj_Id denotes the
1634 -- entity of the _object formal parameter of the DIC procedure. All
1635 -- created checks are added to list Checks.
1637 procedure Add_Own_DIC
1638 (DIC_Prag : Node_Id;
1639 DIC_Typ : Entity_Id;
1640 Obj_Id : Entity_Id;
1641 Stmts : in out List_Id);
1642 -- Add a runtime check to verify the assertion expression of pragma
1643 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. Obj_Id is the
1644 -- object to substitute in the assertion expression for any references
1645 -- to the current instance of the type All generated code is added to
1646 -- list Stmts.
1648 procedure Add_Parent_DICs
1649 (T : Entity_Id;
1650 Obj_Id : Entity_Id;
1651 Checks : in out List_Id);
1652 -- Generate a Default_Initial_Condition check for each inherited DIC
1653 -- aspect coming from all parent types of type T. Obj_Id denotes the
1654 -- entity of the _object formal parameter of the DIC procedure. All
1655 -- created checks are added to list Checks.
1657 -------------------
1658 -- Add_DIC_Check --
1659 -------------------
1661 procedure Add_DIC_Check
1662 (DIC_Prag : Node_Id;
1663 DIC_Expr : Node_Id;
1664 Stmts : in out List_Id)
1666 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1667 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1669 begin
1670 -- The DIC pragma is ignored, nothing left to do
1672 if Is_Ignored (DIC_Prag) then
1673 null;
1675 -- Otherwise the DIC expression must be checked at run time.
1676 -- Generate:
1678 -- pragma Check (<Nam>, <DIC_Expr>);
1680 else
1681 Append_New_To (Stmts,
1682 Make_Pragma (Loc,
1683 Pragma_Identifier =>
1684 Make_Identifier (Loc, Name_Check),
1686 Pragma_Argument_Associations => New_List (
1687 Make_Pragma_Argument_Association (Loc,
1688 Expression => Make_Identifier (Loc, Nam)),
1690 Make_Pragma_Argument_Association (Loc,
1691 Expression => DIC_Expr))));
1692 end if;
1694 -- Add the pragma to the list of processed pragmas
1696 Append_New_Elmt (DIC_Prag, Pragmas_Seen);
1697 end Add_DIC_Check;
1699 -----------------------
1700 -- Add_Inherited_DIC --
1701 -----------------------
1703 procedure Add_Inherited_DIC
1704 (DIC_Prag : Node_Id;
1705 Par_Typ : Entity_Id;
1706 Deriv_Typ : Entity_Id;
1707 Stmts : in out List_Id)
1709 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1710 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1711 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1712 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1713 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1715 begin
1716 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1718 -- Verify the inherited DIC assertion expression by calling the DIC
1719 -- procedure of the parent type.
1721 -- Generate:
1722 -- <Par_Typ>DIC (Par_Typ (_object));
1724 Append_New_To (Stmts,
1725 Make_Procedure_Call_Statement (Loc,
1726 Name => New_Occurrence_Of (Par_Proc, Loc),
1727 Parameter_Associations => New_List (
1728 Convert_To
1729 (Typ => Etype (Par_Obj),
1730 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1731 end Add_Inherited_DIC;
1733 ------------------------------
1734 -- Add_Inherited_Tagged_DIC --
1735 ------------------------------
1737 procedure Add_Inherited_Tagged_DIC
1738 (DIC_Prag : Node_Id;
1739 Expr : Node_Id;
1740 Stmts : in out List_Id)
1742 begin
1743 -- Once the DIC assertion expression is fully processed, add a check
1744 -- to the statements of the DIC procedure.
1746 Add_DIC_Check
1747 (DIC_Prag => DIC_Prag,
1748 DIC_Expr => Expr,
1749 Stmts => Stmts);
1750 end Add_Inherited_Tagged_DIC;
1752 ------------------------
1753 -- Add_Inherited_DICs --
1754 ------------------------
1756 procedure Add_Inherited_DICs
1757 (T : Entity_Id;
1758 Priv_Typ : Entity_Id;
1759 Full_Typ : Entity_Id;
1760 Obj_Id : Entity_Id;
1761 Checks : in out List_Id)
1763 Deriv_Typ : Entity_Id;
1764 Expr : Node_Id;
1765 Prag : Node_Id;
1766 Prag_Expr : Node_Id;
1767 Prag_Expr_Arg : Node_Id;
1768 Prag_Typ : Node_Id;
1769 Prag_Typ_Arg : Node_Id;
1771 Par_Proc : Entity_Id;
1772 -- The "partial" invariant procedure of Par_Typ
1774 Par_Typ : Entity_Id;
1775 -- The suitable view of the parent type used in the substitution of
1776 -- type attributes.
1778 begin
1779 if No (Priv_Typ) and then No (Full_Typ) then
1780 return;
1781 end if;
1783 -- When the type inheriting the class-wide invariant is a concurrent
1784 -- type, use the corresponding record type because it contains all
1785 -- primitive operations of the concurrent type and allows for proper
1786 -- substitution.
1788 if Is_Concurrent_Type (T) then
1789 Deriv_Typ := Corresponding_Record_Type (T);
1790 else
1791 Deriv_Typ := T;
1792 end if;
1794 pragma Assert (Present (Deriv_Typ));
1796 -- Determine which rep item chain to use. Precedence is given to that
1797 -- of the parent type's partial view since it usually carries all the
1798 -- class-wide invariants.
1800 if Present (Priv_Typ) then
1801 Prag := First_Rep_Item (Priv_Typ);
1802 else
1803 Prag := First_Rep_Item (Full_Typ);
1804 end if;
1806 while Present (Prag) loop
1807 if Nkind (Prag) = N_Pragma
1808 and then Pragma_Name (Prag) = Name_Default_Initial_Condition
1809 then
1810 -- Nothing to do if the pragma was already processed
1812 if Contains (Pragmas_Seen, Prag) then
1813 return;
1814 end if;
1816 -- Extract arguments of the Default_Initial_Condition pragma
1818 Prag_Expr_Arg := First (Pragma_Argument_Associations (Prag));
1819 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
1821 -- Pick up the implicit second argument of the pragma, which
1822 -- indicates the type that the pragma applies to.
1824 Prag_Typ_Arg := Next (Prag_Expr_Arg);
1825 if Present (Prag_Typ_Arg) then
1826 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
1827 else
1828 Prag_Typ := Empty;
1829 end if;
1831 -- The pragma applies to the partial view of the parent type
1833 if Present (Priv_Typ)
1834 and then Present (Prag_Typ)
1835 and then Entity (Prag_Typ) = Priv_Typ
1836 then
1837 Par_Typ := Priv_Typ;
1839 -- The pragma applies to the full view of the parent type
1841 elsif Present (Full_Typ)
1842 and then Present (Prag_Typ)
1843 and then Entity (Prag_Typ) = Full_Typ
1844 then
1845 Par_Typ := Full_Typ;
1847 -- Otherwise the pragma does not belong to the parent type and
1848 -- should not be considered.
1850 else
1851 return;
1852 end if;
1854 -- Substitute references in the DIC expression that are related
1855 -- to the partial type with corresponding references related to
1856 -- the derived type (call to Replace_References below).
1858 Expr := New_Copy_Tree (Prag_Expr);
1860 Par_Proc := Partial_DIC_Procedure (Par_Typ);
1862 -- If there's not a partial DIC procedure (such as when a
1863 -- full type doesn't have its own DIC, but is inherited from
1864 -- a type with DIC), get the full DIC procedure.
1866 if No (Par_Proc) then
1867 Par_Proc := DIC_Procedure (Par_Typ);
1868 end if;
1870 Replace_References
1871 (Expr => Expr,
1872 Par_Typ => Par_Typ,
1873 Deriv_Typ => Deriv_Typ,
1874 Par_Obj => First_Formal (Par_Proc),
1875 Deriv_Obj => Obj_Id);
1877 -- Why are there different actions depending on whether T is
1878 -- tagged? Can these be unified? ???
1880 if Is_Tagged_Type (T) then
1881 Add_Inherited_Tagged_DIC
1882 (DIC_Prag => Prag,
1883 Expr => Expr,
1884 Stmts => Checks);
1886 else
1887 Add_Inherited_DIC
1888 (DIC_Prag => Prag,
1889 Par_Typ => Par_Typ,
1890 Deriv_Typ => Deriv_Typ,
1891 Stmts => Checks);
1892 end if;
1894 -- Leave as soon as we get a DIC pragma, since we'll visit
1895 -- the pragmas of the parents, so will get to any "inherited"
1896 -- pragmas that way.
1898 return;
1899 end if;
1901 Next_Rep_Item (Prag);
1902 end loop;
1903 end Add_Inherited_DICs;
1905 -----------------
1906 -- Add_Own_DIC --
1907 -----------------
1909 procedure Add_Own_DIC
1910 (DIC_Prag : Node_Id;
1911 DIC_Typ : Entity_Id;
1912 Obj_Id : Entity_Id;
1913 Stmts : in out List_Id)
1915 DIC_Args : constant List_Id :=
1916 Pragma_Argument_Associations (DIC_Prag);
1917 DIC_Arg : constant Node_Id := First (DIC_Args);
1918 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1919 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1921 -- Local variables
1923 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1925 Expr : Node_Id;
1927 -- Start of processing for Add_Own_DIC
1929 begin
1930 pragma Assert (Present (DIC_Expr));
1932 -- We need to preanalyze the expression itself inside a generic to
1933 -- be able to capture global references present in it.
1935 if Inside_A_Generic then
1936 Expr := DIC_Expr;
1937 else
1938 Expr := New_Copy_Tree (DIC_Expr);
1939 end if;
1941 -- Perform the following substitution:
1943 -- * Replace the current instance of DIC_Typ with a reference to
1944 -- the _object formal parameter of the DIC procedure.
1946 Replace_Type_References
1947 (Expr => Expr,
1948 Typ => DIC_Typ,
1949 Obj_Id => Obj_Id);
1951 -- Preanalyze the DIC expression to detect errors and at the same
1952 -- time capture the visibility of the proper package part.
1954 Set_Parent (Expr, Typ_Decl);
1955 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1957 -- Save a copy of the expression with all replacements and analysis
1958 -- already taken place in case a derived type inherits the pragma.
1959 -- The copy will be used as the foundation of the derived type's own
1960 -- version of the DIC assertion expression.
1962 if Is_Tagged_Type (DIC_Typ) then
1963 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1964 end if;
1966 -- If the pragma comes from an aspect specification, replace the
1967 -- saved expression because all type references must be substituted
1968 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1969 -- routines.
1971 if Present (DIC_Asp) then
1972 Set_Expression_Copy (DIC_Asp, New_Copy_Tree (Expr));
1973 end if;
1975 -- Once the DIC assertion expression is fully processed, add a check
1976 -- to the statements of the DIC procedure (unless the type is an
1977 -- abstract type, in which case we don't want the possibility of
1978 -- generating a call to an abstract function of the type; such DIC
1979 -- procedures can never be called in any case, so not generating the
1980 -- check at all is OK).
1982 if not Is_Abstract_Type (DIC_Typ) or else GNATprove_Mode then
1983 Add_DIC_Check
1984 (DIC_Prag => DIC_Prag,
1985 DIC_Expr => Expr,
1986 Stmts => Stmts);
1987 end if;
1988 end Add_Own_DIC;
1990 ---------------------
1991 -- Add_Parent_DICs --
1992 ---------------------
1994 procedure Add_Parent_DICs
1995 (T : Entity_Id;
1996 Obj_Id : Entity_Id;
1997 Checks : in out List_Id)
1999 Dummy_1 : Entity_Id;
2000 Dummy_2 : Entity_Id;
2002 Curr_Typ : Entity_Id;
2003 -- The entity of the current type being examined
2005 Full_Typ : Entity_Id;
2006 -- The full view of Par_Typ
2008 Par_Typ : Entity_Id;
2009 -- The entity of the parent type
2011 Priv_Typ : Entity_Id;
2012 -- The partial view of Par_Typ
2014 Op_Node : Elmt_Id;
2015 Par_Prim : Entity_Id;
2016 Prim : Entity_Id;
2018 begin
2019 -- Map the overridden primitive to the overriding one; required by
2020 -- Replace_References (called by Add_Inherited_DICs) to handle calls
2021 -- to parent primitives.
2023 Op_Node := First_Elmt (Primitive_Operations (T));
2024 while Present (Op_Node) loop
2025 Prim := Node (Op_Node);
2027 if Present (Overridden_Operation (Prim))
2028 and then Comes_From_Source (Prim)
2029 then
2030 Par_Prim := Overridden_Operation (Prim);
2032 -- Create a mapping of the form:
2033 -- parent type primitive -> derived type primitive
2035 Type_Map.Set (Par_Prim, Prim);
2036 end if;
2038 Next_Elmt (Op_Node);
2039 end loop;
2041 -- Climb the parent type chain
2043 Curr_Typ := T;
2044 loop
2045 -- Do not consider subtypes, as they inherit the DICs from their
2046 -- base types.
2048 Par_Typ := Base_Type (Etype (Base_Type (Curr_Typ)));
2050 -- Stop the climb once the root of the parent chain is
2051 -- reached.
2053 exit when Curr_Typ = Par_Typ;
2055 -- Process the DICs of the parent type
2057 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
2059 -- Only try to inherit a DIC pragma from the parent type Par_Typ
2060 -- if it Has_Own_DIC pragma. The loop will proceed up the parent
2061 -- chain to find all types that have their own DIC.
2063 if Has_Own_DIC (Par_Typ) then
2064 Add_Inherited_DICs
2065 (T => T,
2066 Priv_Typ => Priv_Typ,
2067 Full_Typ => Full_Typ,
2068 Obj_Id => Obj_Id,
2069 Checks => Checks);
2070 end if;
2072 Curr_Typ := Par_Typ;
2073 end loop;
2074 end Add_Parent_DICs;
2076 -- Local variables
2078 Loc : constant Source_Ptr := Sloc (Typ);
2080 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2081 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2082 -- Save the Ghost-related attributes to restore on exit
2084 DIC_Prag : Node_Id;
2085 DIC_Typ : Entity_Id;
2086 Dummy_1 : Entity_Id;
2087 Dummy_2 : Entity_Id;
2088 Proc_Body : Node_Id;
2089 Proc_Body_Id : Entity_Id;
2090 Proc_Decl : Node_Id;
2091 Proc_Id : Entity_Id;
2092 Stmts : List_Id := No_List;
2094 CRec_Typ : Entity_Id := Empty;
2095 -- The corresponding record type of Full_Typ
2097 Full_Typ : Entity_Id := Empty;
2098 -- The full view of the working type
2100 Obj_Id : Entity_Id := Empty;
2101 -- The _object formal parameter of the invariant procedure
2103 Part_Proc : Entity_Id := Empty;
2104 -- The entity of the "partial" invariant procedure
2106 Priv_Typ : Entity_Id := Empty;
2107 -- The partial view of the working type
2109 Work_Typ : Entity_Id;
2110 -- The working type
2112 -- Start of processing for Build_DIC_Procedure_Body
2114 begin
2115 Work_Typ := Base_Type (Typ);
2117 -- Do not process class-wide types as these are Itypes, but lack a first
2118 -- subtype (see below).
2120 if Is_Class_Wide_Type (Work_Typ) then
2121 return;
2123 -- Do not process the underlying full view of a private type. There is
2124 -- no way to get back to the partial view, plus the body will be built
2125 -- by the full view or the base type.
2127 elsif Is_Underlying_Full_View (Work_Typ) then
2128 return;
2130 -- Use the first subtype when dealing with implicit base types
2132 elsif Is_Itype (Work_Typ) then
2133 Work_Typ := First_Subtype (Work_Typ);
2135 -- The input denotes the corresponding record type of a protected or a
2136 -- task type. Work with the concurrent type because the corresponding
2137 -- record type may not be visible to clients of the type.
2139 elsif Ekind (Work_Typ) = E_Record_Type
2140 and then Is_Concurrent_Record_Type (Work_Typ)
2141 then
2142 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2143 end if;
2145 -- The working type may be subject to pragma Ghost. Set the mode now to
2146 -- ensure that the DIC procedure is properly marked as Ghost.
2148 Set_Ghost_Mode (Work_Typ);
2150 -- The working type must be either define a DIC pragma of its own or
2151 -- inherit one from a parent type.
2153 pragma Assert (Has_DIC (Work_Typ));
2155 -- Recover the type which defines the DIC pragma. This is either the
2156 -- working type itself or a parent type when the pragma is inherited.
2158 DIC_Typ := Find_DIC_Type (Work_Typ);
2159 pragma Assert (Present (DIC_Typ));
2161 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2162 pragma Assert (Present (DIC_Prag));
2164 -- Nothing to do if pragma DIC appears without an argument or its sole
2165 -- argument is "null".
2167 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2168 goto Leave;
2169 end if;
2171 -- Obtain both views of the type
2173 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy_1, CRec_Typ);
2175 -- The caller requests a body for the partial DIC procedure
2177 if Partial_DIC then
2178 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2180 -- The "full" DIC procedure body was already created
2182 -- Create a declaration for the "partial" DIC procedure if it
2183 -- is not available.
2185 if No (Proc_Id) then
2186 Build_DIC_Procedure_Declaration
2187 (Typ => Work_Typ,
2188 Partial_DIC => True);
2190 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2191 end if;
2193 -- The caller requests a body for the "full" DIC procedure
2195 else
2196 Proc_Id := DIC_Procedure (Work_Typ);
2197 Part_Proc := Partial_DIC_Procedure (Work_Typ);
2199 -- Create a declaration for the "full" DIC procedure if it is
2200 -- not available.
2202 if No (Proc_Id) then
2203 Build_DIC_Procedure_Declaration (Work_Typ);
2204 Proc_Id := DIC_Procedure (Work_Typ);
2205 end if;
2206 end if;
2208 -- At this point there should be a DIC procedure declaration
2210 pragma Assert (Present (Proc_Id));
2211 Proc_Decl := Unit_Declaration_Node (Proc_Id);
2213 -- Nothing to do if the DIC procedure already has a body
2215 if Present (Corresponding_Body (Proc_Decl)) then
2216 goto Leave;
2217 end if;
2219 -- Emulate the environment of the DIC procedure by installing its scope
2220 -- and formal parameters.
2222 Push_Scope (Proc_Id);
2223 Install_Formals (Proc_Id);
2225 Obj_Id := First_Formal (Proc_Id);
2226 pragma Assert (Present (Obj_Id));
2228 -- The "partial" DIC procedure verifies the DICs of the partial view
2229 -- only.
2231 if Partial_DIC then
2232 pragma Assert (Present (Priv_Typ));
2234 if Has_Own_DIC (Work_Typ) then -- If we're testing this then maybe
2235 Add_Own_DIC -- we shouldn't be calling Find_DIC_Typ above???
2236 (DIC_Prag => DIC_Prag,
2237 DIC_Typ => DIC_Typ, -- Should this just be Work_Typ???
2238 Obj_Id => Obj_Id,
2239 Stmts => Stmts);
2240 end if;
2242 -- Otherwise, the "full" DIC procedure verifies the DICs inherited from
2243 -- parent types, as well as indirectly verifying the DICs of the partial
2244 -- view by calling the "partial" DIC procedure.
2246 else
2247 -- Check the DIC of the partial view by calling the "partial" DIC
2248 -- procedure, unless the partial DIC body is empty. Generate:
2250 -- <Work_Typ>Partial_DIC (_object);
2252 if Present (Part_Proc) and then not Has_Null_Body (Part_Proc) then
2253 Append_New_To (Stmts,
2254 Make_Procedure_Call_Statement (Loc,
2255 Name => New_Occurrence_Of (Part_Proc, Loc),
2256 Parameter_Associations => New_List (
2257 New_Occurrence_Of (Obj_Id, Loc))));
2258 end if;
2260 -- Process inherited Default_Initial_Conditions for all parent types
2262 Add_Parent_DICs (Work_Typ, Obj_Id, Stmts);
2263 end if;
2265 End_Scope;
2267 -- Produce an empty completing body in the following cases:
2268 -- * Assertions are disabled
2269 -- * The DIC Assertion_Policy is Ignore
2271 if No (Stmts) then
2272 Stmts := New_List (Make_Null_Statement (Loc));
2273 end if;
2275 -- Generate:
2276 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
2277 -- begin
2278 -- <Stmts>
2279 -- end <Work_Typ>DIC;
2281 Proc_Body :=
2282 Make_Subprogram_Body (Loc,
2283 Specification =>
2284 Copy_Subprogram_Spec (Parent (Proc_Id)),
2285 Declarations => Empty_List,
2286 Handled_Statement_Sequence =>
2287 Make_Handled_Sequence_Of_Statements (Loc,
2288 Statements => Stmts));
2289 Proc_Body_Id := Defining_Entity (Proc_Body);
2291 -- Perform minor decoration in case the body is not analyzed
2293 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
2294 Set_Etype (Proc_Body_Id, Standard_Void_Type);
2295 Set_Scope (Proc_Body_Id, Current_Scope);
2296 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
2297 Set_SPARK_Pragma_Inherited
2298 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
2300 -- Link both spec and body to avoid generating duplicates
2302 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
2303 Set_Corresponding_Spec (Proc_Body, Proc_Id);
2305 -- The body should not be inserted into the tree when the context
2306 -- is a generic unit because it is not part of the template.
2307 -- Note that the body must still be generated in order to resolve the
2308 -- DIC assertion expression.
2310 if Inside_A_Generic then
2311 null;
2313 -- Semi-insert the body into the tree for GNATprove by setting its
2314 -- Parent field. This allows for proper upstream tree traversals.
2316 elsif GNATprove_Mode then
2317 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
2319 -- Otherwise the body is part of the freezing actions of the working
2320 -- type.
2322 else
2323 Append_Freeze_Action (Work_Typ, Proc_Body);
2324 end if;
2326 <<Leave>>
2327 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2328 end Build_DIC_Procedure_Body;
2330 -------------------------------------
2331 -- Build_DIC_Procedure_Declaration --
2332 -------------------------------------
2334 -- WARNING: This routine manages Ghost regions. Return statements must be
2335 -- replaced by gotos which jump to the end of the routine and restore the
2336 -- Ghost mode.
2338 procedure Build_DIC_Procedure_Declaration
2339 (Typ : Entity_Id;
2340 Partial_DIC : Boolean := False)
2342 Loc : constant Source_Ptr := Sloc (Typ);
2344 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2345 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2346 -- Save the Ghost-related attributes to restore on exit
2348 DIC_Prag : Node_Id;
2349 DIC_Typ : Entity_Id;
2350 Proc_Decl : Node_Id;
2351 Proc_Id : Entity_Id;
2352 Proc_Nam : Name_Id;
2353 Typ_Decl : Node_Id;
2355 CRec_Typ : Entity_Id;
2356 -- The corresponding record type of Full_Typ
2358 Full_Typ : Entity_Id;
2359 -- The full view of working type
2361 Obj_Id : Entity_Id;
2362 -- The _object formal parameter of the DIC procedure
2364 Priv_Typ : Entity_Id;
2365 -- The partial view of working type
2367 UFull_Typ : Entity_Id;
2368 -- The underlying full view of Full_Typ
2370 Work_Typ : Entity_Id;
2371 -- The working type
2373 begin
2374 Work_Typ := Base_Type (Typ);
2376 -- Do not process class-wide types as these are Itypes, but lack a first
2377 -- subtype (see below).
2379 if Is_Class_Wide_Type (Work_Typ) then
2380 return;
2382 -- Do not process the underlying full view of a private type. There is
2383 -- no way to get back to the partial view, plus the body will be built
2384 -- by the full view or the base type.
2386 elsif Is_Underlying_Full_View (Work_Typ) then
2387 return;
2389 -- Use the first subtype when dealing with various base types
2391 elsif Is_Itype (Work_Typ) then
2392 Work_Typ := First_Subtype (Work_Typ);
2394 -- The input denotes the corresponding record type of a protected or a
2395 -- task type. Work with the concurrent type because the corresponding
2396 -- record type may not be visible to clients of the type.
2398 elsif Ekind (Work_Typ) = E_Record_Type
2399 and then Is_Concurrent_Record_Type (Work_Typ)
2400 then
2401 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2402 end if;
2404 -- The working type may be subject to pragma Ghost. Set the mode now to
2405 -- ensure that the DIC procedure is properly marked as Ghost.
2407 Set_Ghost_Mode (Work_Typ);
2409 -- The type must be either subject to a DIC pragma or inherit one from a
2410 -- parent type.
2412 pragma Assert (Has_DIC (Work_Typ));
2414 -- Recover the type which defines the DIC pragma. This is either the
2415 -- working type itself or a parent type when the pragma is inherited.
2417 DIC_Typ := Find_DIC_Type (Work_Typ);
2418 pragma Assert (Present (DIC_Typ));
2420 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2421 pragma Assert (Present (DIC_Prag));
2423 -- Nothing to do if pragma DIC appears without an argument or its sole
2424 -- argument is "null".
2426 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2427 goto Leave;
2428 end if;
2430 -- Nothing to do if the type already has a "partial" DIC procedure
2432 if Partial_DIC then
2433 if Present (Partial_DIC_Procedure (Work_Typ)) then
2434 goto Leave;
2435 end if;
2437 -- Nothing to do if the type already has a "full" DIC procedure
2439 elsif Present (DIC_Procedure (Work_Typ)) then
2440 goto Leave;
2441 end if;
2443 -- The caller requests the declaration of the "partial" DIC procedure
2445 if Partial_DIC then
2446 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_DIC");
2448 -- Otherwise the caller requests the declaration of the "full" DIC
2449 -- procedure.
2451 else
2452 Proc_Nam := New_External_Name (Chars (Work_Typ), "DIC");
2453 end if;
2455 Proc_Id :=
2456 Make_Defining_Identifier (Loc, Chars => Proc_Nam);
2458 -- Perform minor decoration in case the declaration is not analyzed
2460 Mutate_Ekind (Proc_Id, E_Procedure);
2461 Set_Etype (Proc_Id, Standard_Void_Type);
2462 Set_Is_DIC_Procedure (Proc_Id);
2463 Set_Scope (Proc_Id, Current_Scope);
2464 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
2465 Set_SPARK_Pragma_Inherited (Proc_Id);
2467 Set_DIC_Procedure (Work_Typ, Proc_Id);
2469 -- The DIC procedure requires debug info when the assertion expression
2470 -- is subject to Source Coverage Obligations.
2472 if Generate_SCO then
2473 Set_Debug_Info_Needed (Proc_Id);
2474 end if;
2476 -- Obtain all views of the input type
2478 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
2480 -- Associate the DIC procedure and various flags with all views
2482 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
2483 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
2484 Propagate_DIC_Attributes (UFull_Typ, From_Typ => Work_Typ);
2485 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
2487 -- The declaration of the DIC procedure must be inserted after the
2488 -- declaration of the partial view as this allows for proper external
2489 -- visibility.
2491 if Present (Priv_Typ) then
2492 Typ_Decl := Declaration_Node (Priv_Typ);
2494 -- Derived types with the full view as parent do not have a partial
2495 -- view. Insert the DIC procedure after the derived type.
2497 else
2498 Typ_Decl := Declaration_Node (Full_Typ);
2499 end if;
2501 -- The type should have a declarative node
2503 pragma Assert (Present (Typ_Decl));
2505 -- Create the formal parameter which emulates the variable-like behavior
2506 -- of the type's current instance.
2508 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
2510 -- Perform minor decoration in case the declaration is not analyzed
2512 Mutate_Ekind (Obj_Id, E_In_Parameter);
2513 Set_Etype (Obj_Id, Work_Typ);
2514 Set_Scope (Obj_Id, Proc_Id);
2516 Set_First_Entity (Proc_Id, Obj_Id);
2517 Set_Last_Entity (Proc_Id, Obj_Id);
2519 -- Generate:
2520 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
2522 Proc_Decl :=
2523 Make_Subprogram_Declaration (Loc,
2524 Specification =>
2525 Make_Procedure_Specification (Loc,
2526 Defining_Unit_Name => Proc_Id,
2527 Parameter_Specifications => New_List (
2528 Make_Parameter_Specification (Loc,
2529 Defining_Identifier => Obj_Id,
2530 Parameter_Type =>
2531 New_Occurrence_Of (Work_Typ, Loc)))));
2533 -- The declaration should not be inserted into the tree when the context
2534 -- is a generic unit because it is not part of the template.
2536 if Inside_A_Generic then
2537 null;
2539 -- Semi-insert the declaration into the tree for GNATprove by setting
2540 -- its Parent field. This allows for proper upstream tree traversals.
2542 elsif GNATprove_Mode then
2543 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2545 -- Otherwise insert the declaration
2547 else
2548 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2549 end if;
2551 <<Leave>>
2552 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2553 end Build_DIC_Procedure_Declaration;
2555 ------------------------------------
2556 -- Build_Invariant_Procedure_Body --
2557 ------------------------------------
2559 -- WARNING: This routine manages Ghost regions. Return statements must be
2560 -- replaced by gotos which jump to the end of the routine and restore the
2561 -- Ghost mode.
2563 procedure Build_Invariant_Procedure_Body
2564 (Typ : Entity_Id;
2565 Partial_Invariant : Boolean := False)
2567 Loc : constant Source_Ptr := Sloc (Typ);
2569 Pragmas_Seen : Elist_Id := No_Elist;
2570 -- This list contains all invariant pragmas processed so far. The list
2571 -- is used to avoid generating redundant invariant checks.
2573 Produced_Check : Boolean := False;
2574 -- This flag tracks whether the type has produced at least one invariant
2575 -- check. The flag is used as a sanity check at the end of the routine.
2577 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2578 -- intentionally unnested to avoid deep indentation of code.
2580 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2581 -- they emit checks, loops (for arrays) and case statements (for record
2582 -- variant parts) only when there are invariants to verify. This keeps
2583 -- the body of the invariant procedure free of useless code.
2585 procedure Add_Array_Component_Invariants
2586 (T : Entity_Id;
2587 Obj_Id : Entity_Id;
2588 Checks : in out List_Id);
2589 -- Generate an invariant check for each component of array type T.
2590 -- Obj_Id denotes the entity of the _object formal parameter of the
2591 -- invariant procedure. All created checks are added to list Checks.
2593 procedure Add_Inherited_Invariants
2594 (T : Entity_Id;
2595 Priv_Typ : Entity_Id;
2596 Full_Typ : Entity_Id;
2597 Obj_Id : Entity_Id;
2598 Checks : in out List_Id);
2599 -- Generate an invariant check for each inherited class-wide invariant
2600 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2601 -- the partial and full view of the parent type. Obj_Id denotes the
2602 -- entity of the _object formal parameter of the invariant procedure.
2603 -- All created checks are added to list Checks.
2605 procedure Add_Interface_Invariants
2606 (T : Entity_Id;
2607 Obj_Id : Entity_Id;
2608 Checks : in out List_Id);
2609 -- Generate an invariant check for each inherited class-wide invariant
2610 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2611 -- entity of the _object formal parameter of the invariant procedure.
2612 -- All created checks are added to list Checks.
2614 procedure Add_Invariant_Check
2615 (Prag : Node_Id;
2616 Expr : Node_Id;
2617 Checks : in out List_Id;
2618 Inherited : Boolean := False);
2619 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2620 -- verify assertion expression Expr of pragma Prag. All generated code
2621 -- is added to list Checks. Flag Inherited should be set when the pragma
2622 -- is inherited from a parent or interface type.
2624 procedure Add_Own_Invariants
2625 (T : Entity_Id;
2626 Obj_Id : Entity_Id;
2627 Checks : in out List_Id;
2628 Priv_Item : Node_Id := Empty);
2629 -- Generate an invariant check for each invariant found for type T.
2630 -- Obj_Id denotes the entity of the _object formal parameter of the
2631 -- invariant procedure. All created checks are added to list Checks.
2632 -- Priv_Item denotes the first rep item of the private type.
2634 procedure Add_Parent_Invariants
2635 (T : Entity_Id;
2636 Obj_Id : Entity_Id;
2637 Checks : in out List_Id);
2638 -- Generate an invariant check for each inherited class-wide invariant
2639 -- coming from all parent types of type T. Obj_Id denotes the entity of
2640 -- the _object formal parameter of the invariant procedure. All created
2641 -- checks are added to list Checks.
2643 procedure Add_Record_Component_Invariants
2644 (T : Entity_Id;
2645 Obj_Id : Entity_Id;
2646 Checks : in out List_Id);
2647 -- Generate an invariant check for each component of record type T.
2648 -- Obj_Id denotes the entity of the _object formal parameter of the
2649 -- invariant procedure. All created checks are added to list Checks.
2651 ------------------------------------
2652 -- Add_Array_Component_Invariants --
2653 ------------------------------------
2655 procedure Add_Array_Component_Invariants
2656 (T : Entity_Id;
2657 Obj_Id : Entity_Id;
2658 Checks : in out List_Id)
2660 Comp_Typ : constant Entity_Id := Component_Type (T);
2661 Dims : constant Pos := Number_Dimensions (T);
2663 procedure Process_Array_Component
2664 (Indices : List_Id;
2665 Comp_Checks : in out List_Id);
2666 -- Generate an invariant check for an array component identified by
2667 -- the indices in list Indices. All created checks are added to list
2668 -- Comp_Checks.
2670 procedure Process_One_Dimension
2671 (Dim : Pos;
2672 Indices : List_Id;
2673 Dim_Checks : in out List_Id);
2674 -- Generate a loop over the Nth dimension Dim of an array type. List
2675 -- Indices contains all array indices for the dimension. All created
2676 -- checks are added to list Dim_Checks.
2678 -----------------------------
2679 -- Process_Array_Component --
2680 -----------------------------
2682 procedure Process_Array_Component
2683 (Indices : List_Id;
2684 Comp_Checks : in out List_Id)
2686 Proc_Id : Entity_Id;
2688 begin
2689 if Has_Invariants (Comp_Typ) then
2691 -- In GNATprove mode, the component invariants are checked by
2692 -- other means. They should not be added to the array type
2693 -- invariant procedure, so that the procedure can be used to
2694 -- check the array type invariants if any.
2696 if GNATprove_Mode then
2697 null;
2699 else
2700 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2702 -- The component type should have an invariant procedure
2703 -- if it has invariants of its own or inherits class-wide
2704 -- invariants from parent or interface types.
2706 pragma Assert (Present (Proc_Id));
2708 -- Generate:
2709 -- <Comp_Typ>Invariant (_object (<Indices>));
2711 -- The invariant procedure has a null body if assertions are
2712 -- disabled or Assertion_Policy Ignore is in effect.
2714 if not Has_Null_Body (Proc_Id) then
2715 Append_New_To (Comp_Checks,
2716 Make_Procedure_Call_Statement (Loc,
2717 Name =>
2718 New_Occurrence_Of (Proc_Id, Loc),
2719 Parameter_Associations => New_List (
2720 Make_Indexed_Component (Loc,
2721 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2722 Expressions => New_Copy_List (Indices)))));
2723 end if;
2724 end if;
2726 Produced_Check := True;
2727 end if;
2728 end Process_Array_Component;
2730 ---------------------------
2731 -- Process_One_Dimension --
2732 ---------------------------
2734 procedure Process_One_Dimension
2735 (Dim : Pos;
2736 Indices : List_Id;
2737 Dim_Checks : in out List_Id)
2739 Comp_Checks : List_Id := No_List;
2740 Index : Entity_Id;
2742 begin
2743 -- Generate the invariant checks for the array component after all
2744 -- dimensions have produced their respective loops.
2746 if Dim > Dims then
2747 Process_Array_Component
2748 (Indices => Indices,
2749 Comp_Checks => Dim_Checks);
2751 -- Otherwise create a loop for the current dimension
2753 else
2754 -- Create a new loop variable for each dimension
2756 Index :=
2757 Make_Defining_Identifier (Loc,
2758 Chars => New_External_Name ('I', Dim));
2759 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2761 Process_One_Dimension
2762 (Dim => Dim + 1,
2763 Indices => Indices,
2764 Dim_Checks => Comp_Checks);
2766 -- Generate:
2767 -- for I<Dim> in _object'Range (<Dim>) loop
2768 -- <Comp_Checks>
2769 -- end loop;
2771 -- Note that the invariant procedure may have a null body if
2772 -- assertions are disabled or Assertion_Policy Ignore is in
2773 -- effect.
2775 if Present (Comp_Checks) then
2776 Append_New_To (Dim_Checks,
2777 Make_Implicit_Loop_Statement (T,
2778 Identifier => Empty,
2779 Iteration_Scheme =>
2780 Make_Iteration_Scheme (Loc,
2781 Loop_Parameter_Specification =>
2782 Make_Loop_Parameter_Specification (Loc,
2783 Defining_Identifier => Index,
2784 Discrete_Subtype_Definition =>
2785 Make_Attribute_Reference (Loc,
2786 Prefix =>
2787 New_Occurrence_Of (Obj_Id, Loc),
2788 Attribute_Name => Name_Range,
2789 Expressions => New_List (
2790 Make_Integer_Literal (Loc, Dim))))),
2791 Statements => Comp_Checks));
2792 end if;
2793 end if;
2794 end Process_One_Dimension;
2796 -- Start of processing for Add_Array_Component_Invariants
2798 begin
2799 Process_One_Dimension
2800 (Dim => 1,
2801 Indices => New_List,
2802 Dim_Checks => Checks);
2803 end Add_Array_Component_Invariants;
2805 ------------------------------
2806 -- Add_Inherited_Invariants --
2807 ------------------------------
2809 procedure Add_Inherited_Invariants
2810 (T : Entity_Id;
2811 Priv_Typ : Entity_Id;
2812 Full_Typ : Entity_Id;
2813 Obj_Id : Entity_Id;
2814 Checks : in out List_Id)
2816 Deriv_Typ : Entity_Id;
2817 Expr : Node_Id;
2818 Prag : Node_Id;
2819 Prag_Expr : Node_Id;
2820 Prag_Expr_Arg : Node_Id;
2821 Prag_Typ : Node_Id;
2822 Prag_Typ_Arg : Node_Id;
2824 Par_Proc : Entity_Id;
2825 -- The "partial" invariant procedure of Par_Typ
2827 Par_Typ : Entity_Id;
2828 -- The suitable view of the parent type used in the substitution of
2829 -- type attributes.
2831 begin
2832 if No (Priv_Typ) and then No (Full_Typ) then
2833 return;
2834 end if;
2836 -- When the type inheriting the class-wide invariant is a concurrent
2837 -- type, use the corresponding record type because it contains all
2838 -- primitive operations of the concurrent type and allows for proper
2839 -- substitution.
2841 if Is_Concurrent_Type (T) then
2842 Deriv_Typ := Corresponding_Record_Type (T);
2843 else
2844 Deriv_Typ := T;
2845 end if;
2847 pragma Assert (Present (Deriv_Typ));
2849 -- Determine which rep item chain to use. Precedence is given to that
2850 -- of the parent type's partial view since it usually carries all the
2851 -- class-wide invariants.
2853 if Present (Priv_Typ) then
2854 Prag := First_Rep_Item (Priv_Typ);
2855 else
2856 Prag := First_Rep_Item (Full_Typ);
2857 end if;
2859 while Present (Prag) loop
2860 if Nkind (Prag) = N_Pragma
2861 and then Pragma_Name (Prag) = Name_Invariant
2862 then
2863 -- Nothing to do if the pragma was already processed
2865 if Contains (Pragmas_Seen, Prag) then
2866 return;
2868 -- Nothing to do when the caller requests the processing of all
2869 -- inherited class-wide invariants, but the pragma does not
2870 -- fall in this category.
2872 elsif not Class_Present (Prag) then
2873 return;
2874 end if;
2876 -- Extract the arguments of the invariant pragma
2878 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2879 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2880 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2881 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2883 -- The pragma applies to the partial view of the parent type
2885 if Present (Priv_Typ)
2886 and then Entity (Prag_Typ) = Priv_Typ
2887 then
2888 Par_Typ := Priv_Typ;
2890 -- The pragma applies to the full view of the parent type
2892 elsif Present (Full_Typ)
2893 and then Entity (Prag_Typ) = Full_Typ
2894 then
2895 Par_Typ := Full_Typ;
2897 -- Otherwise the pragma does not belong to the parent type and
2898 -- should not be considered.
2900 else
2901 return;
2902 end if;
2904 -- Perform the following substitutions:
2906 -- * Replace a reference to the _object parameter of the
2907 -- parent type's partial invariant procedure with a
2908 -- reference to the _object parameter of the derived
2909 -- type's full invariant procedure.
2911 -- * Replace a reference to a discriminant of the parent type
2912 -- with a suitable value from the point of view of the
2913 -- derived type.
2915 -- * Replace a call to an overridden parent primitive with a
2916 -- call to the overriding derived type primitive.
2918 -- * Replace a call to an inherited parent primitive with a
2919 -- call to the internally-generated inherited derived type
2920 -- primitive.
2922 Expr := New_Copy_Tree (Prag_Expr);
2924 -- The parent type must have a "partial" invariant procedure
2925 -- because class-wide invariants are captured exclusively by
2926 -- it.
2928 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2929 pragma Assert (Present (Par_Proc));
2931 Replace_References
2932 (Expr => Expr,
2933 Par_Typ => Par_Typ,
2934 Deriv_Typ => Deriv_Typ,
2935 Par_Obj => First_Formal (Par_Proc),
2936 Deriv_Obj => Obj_Id);
2938 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2939 end if;
2941 Next_Rep_Item (Prag);
2942 end loop;
2943 end Add_Inherited_Invariants;
2945 ------------------------------
2946 -- Add_Interface_Invariants --
2947 ------------------------------
2949 procedure Add_Interface_Invariants
2950 (T : Entity_Id;
2951 Obj_Id : Entity_Id;
2952 Checks : in out List_Id)
2954 Iface_Elmt : Elmt_Id;
2955 Ifaces : Elist_Id;
2957 begin
2958 -- Generate an invariant check for each class-wide invariant coming
2959 -- from all interfaces implemented by type T.
2961 if Is_Tagged_Type (T) then
2962 Collect_Interfaces (T, Ifaces);
2964 -- Process the class-wide invariants of all implemented interfaces
2966 Iface_Elmt := First_Elmt (Ifaces);
2967 while Present (Iface_Elmt) loop
2969 -- The Full_Typ parameter is intentionally left Empty because
2970 -- interfaces are treated as the partial view of a private type
2971 -- in order to achieve uniformity with the general case.
2973 Add_Inherited_Invariants
2974 (T => T,
2975 Priv_Typ => Node (Iface_Elmt),
2976 Full_Typ => Empty,
2977 Obj_Id => Obj_Id,
2978 Checks => Checks);
2980 Next_Elmt (Iface_Elmt);
2981 end loop;
2982 end if;
2983 end Add_Interface_Invariants;
2985 -------------------------
2986 -- Add_Invariant_Check --
2987 -------------------------
2989 procedure Add_Invariant_Check
2990 (Prag : Node_Id;
2991 Expr : Node_Id;
2992 Checks : in out List_Id;
2993 Inherited : Boolean := False)
2995 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2996 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2997 Ploc : constant Source_Ptr := Sloc (Prag);
2998 Str_Arg : constant Node_Id := Next (Next (First (Args)));
3000 Assoc : List_Id;
3001 Str : String_Id;
3003 begin
3004 -- The invariant is ignored, nothing left to do
3006 if Is_Ignored (Prag) then
3007 null;
3009 -- Otherwise the invariant is checked. Build a pragma Check to verify
3010 -- the expression at run time.
3012 else
3013 Assoc := New_List (
3014 Make_Pragma_Argument_Association (Ploc,
3015 Expression => Make_Identifier (Ploc, Nam)),
3016 Make_Pragma_Argument_Association (Ploc,
3017 Expression => Expr));
3019 -- Handle the String argument (if any)
3021 if Present (Str_Arg) then
3022 Str := Strval (Get_Pragma_Arg (Str_Arg));
3024 -- When inheriting an invariant, modify the message from
3025 -- "failed invariant" to "failed inherited invariant".
3027 if Inherited then
3028 String_To_Name_Buffer (Str);
3030 if Name_Buffer (1 .. 16) = "failed invariant" then
3031 Insert_Str_In_Name_Buffer ("inherited ", 8);
3032 Str := String_From_Name_Buffer;
3033 end if;
3034 end if;
3036 Append_To (Assoc,
3037 Make_Pragma_Argument_Association (Ploc,
3038 Expression => Make_String_Literal (Ploc, Str)));
3039 end if;
3041 -- Generate:
3042 -- pragma Check (<Nam>, <Expr>, <Str>);
3044 Append_New_To (Checks,
3045 Make_Pragma (Ploc,
3046 Chars => Name_Check,
3047 Pragma_Argument_Associations => Assoc));
3048 end if;
3050 -- Output an info message when inheriting an invariant and the
3051 -- listing option is enabled.
3053 if Inherited and List_Inherited_Aspects then
3054 Error_Msg_Sloc := Sloc (Prag);
3055 Error_Msg_N
3056 ("info: & inherits `Invariant''Class` aspect from #?.l?", Typ);
3057 end if;
3059 -- Add the pragma to the list of processed pragmas
3061 Append_New_Elmt (Prag, Pragmas_Seen);
3062 Produced_Check := True;
3063 end Add_Invariant_Check;
3065 ---------------------------
3066 -- Add_Parent_Invariants --
3067 ---------------------------
3069 procedure Add_Parent_Invariants
3070 (T : Entity_Id;
3071 Obj_Id : Entity_Id;
3072 Checks : in out List_Id)
3074 Dummy_1 : Entity_Id;
3075 Dummy_2 : Entity_Id;
3077 Curr_Typ : Entity_Id;
3078 -- The entity of the current type being examined
3080 Full_Typ : Entity_Id;
3081 -- The full view of Par_Typ
3083 Par_Typ : Entity_Id;
3084 -- The entity of the parent type
3086 Priv_Typ : Entity_Id;
3087 -- The partial view of Par_Typ
3089 begin
3090 -- Do not process array types because they cannot have true parent
3091 -- types. This also prevents the generation of a duplicate invariant
3092 -- check when the input type is an array base type because its Etype
3093 -- denotes the first subtype, both of which share the same component
3094 -- type.
3096 if Is_Array_Type (T) then
3097 return;
3098 end if;
3100 -- Climb the parent type chain
3102 Curr_Typ := T;
3103 loop
3104 -- Do not consider subtypes as they inherit the invariants
3105 -- from their base types.
3107 Par_Typ := Base_Type (Etype (Curr_Typ));
3109 -- Stop the climb once the root of the parent chain is
3110 -- reached.
3112 exit when Curr_Typ = Par_Typ;
3114 -- Process the class-wide invariants of the parent type
3116 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
3118 -- Process the elements of an array type
3120 if Is_Array_Type (Full_Typ) then
3121 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
3123 -- Process the components of a record type
3125 elsif Ekind (Full_Typ) = E_Record_Type then
3126 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
3127 end if;
3129 Add_Inherited_Invariants
3130 (T => T,
3131 Priv_Typ => Priv_Typ,
3132 Full_Typ => Full_Typ,
3133 Obj_Id => Obj_Id,
3134 Checks => Checks);
3136 Curr_Typ := Par_Typ;
3137 end loop;
3138 end Add_Parent_Invariants;
3140 ------------------------
3141 -- Add_Own_Invariants --
3142 ------------------------
3144 procedure Add_Own_Invariants
3145 (T : Entity_Id;
3146 Obj_Id : Entity_Id;
3147 Checks : in out List_Id;
3148 Priv_Item : Node_Id := Empty)
3150 Expr : Node_Id;
3151 Prag : Node_Id;
3152 Prag_Asp : Node_Id;
3153 Prag_Expr : Node_Id;
3154 Prag_Expr_Arg : Node_Id;
3155 Prag_Typ : Node_Id;
3156 Prag_Typ_Arg : Node_Id;
3158 begin
3159 if No (T) then
3160 return;
3161 end if;
3163 Prag := First_Rep_Item (T);
3164 while Present (Prag) loop
3165 if Nkind (Prag) = N_Pragma
3166 and then Pragma_Name (Prag) = Name_Invariant
3167 then
3168 -- Stop the traversal of the rep item chain once a specific
3169 -- item is encountered.
3171 if Present (Priv_Item) and then Prag = Priv_Item then
3172 exit;
3173 end if;
3175 -- Nothing to do if the pragma was already processed
3177 if Contains (Pragmas_Seen, Prag) then
3178 return;
3179 end if;
3181 -- Extract the arguments of the invariant pragma
3183 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
3184 Prag_Expr_Arg := Next (Prag_Typ_Arg);
3185 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
3186 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
3187 Prag_Asp := Corresponding_Aspect (Prag);
3189 -- Verify the pragma belongs to T, otherwise the pragma applies
3190 -- to a parent type in which case it will be processed later by
3191 -- Add_Parent_Invariants or Add_Interface_Invariants.
3193 if Entity (Prag_Typ) /= T then
3194 return;
3195 end if;
3197 -- We need to preanalyze the expression itself inside a generic
3198 -- to be able to capture global references present in it.
3200 if Inside_A_Generic then
3201 Expr := Prag_Expr;
3202 else
3203 Expr := New_Copy_Tree (Prag_Expr);
3204 end if;
3206 -- Substitute all references to type T with references to the
3207 -- _object formal parameter.
3209 Replace_Type_References (Expr, T, Obj_Id);
3211 -- Preanalyze the invariant expression to detect errors and at
3212 -- the same time capture the visibility of the proper package
3213 -- part.
3215 Set_Parent (Expr, Parent (Prag_Expr));
3216 Preanalyze_Assert_Expression (Expr, Any_Boolean);
3218 -- Save a copy of the expression when T is tagged to detect
3219 -- errors and capture the visibility of the proper package part
3220 -- for the generation of inherited type invariants.
3222 if Is_Tagged_Type (T) then
3223 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
3224 end if;
3226 -- If the pragma comes from an aspect specification, replace
3227 -- the saved expression because all type references must be
3228 -- substituted for the call to Preanalyze_Spec_Expression in
3229 -- Check_Aspect_At_xxx routines.
3231 if Present (Prag_Asp) then
3232 Set_Expression_Copy (Prag_Asp, New_Copy_Tree (Expr));
3233 end if;
3235 Add_Invariant_Check (Prag, Expr, Checks);
3236 end if;
3238 Next_Rep_Item (Prag);
3239 end loop;
3240 end Add_Own_Invariants;
3242 -------------------------------------
3243 -- Add_Record_Component_Invariants --
3244 -------------------------------------
3246 procedure Add_Record_Component_Invariants
3247 (T : Entity_Id;
3248 Obj_Id : Entity_Id;
3249 Checks : in out List_Id)
3251 procedure Process_Component_List
3252 (Comp_List : Node_Id;
3253 CL_Checks : in out List_Id);
3254 -- Generate invariant checks for all record components found in
3255 -- component list Comp_List, including variant parts. All created
3256 -- checks are added to list CL_Checks.
3258 procedure Process_Record_Component
3259 (Comp_Id : Entity_Id;
3260 Comp_Checks : in out List_Id);
3261 -- Generate an invariant check for a record component identified by
3262 -- Comp_Id. All created checks are added to list Comp_Checks.
3264 ----------------------------
3265 -- Process_Component_List --
3266 ----------------------------
3268 procedure Process_Component_List
3269 (Comp_List : Node_Id;
3270 CL_Checks : in out List_Id)
3272 Comp : Node_Id;
3273 Var : Node_Id;
3274 Var_Alts : List_Id := No_List;
3275 Var_Checks : List_Id := No_List;
3276 Var_Stmts : List_Id;
3278 Produced_Variant_Check : Boolean := False;
3279 -- This flag tracks whether the component has produced at least
3280 -- one invariant check.
3282 begin
3283 -- Traverse the component items
3285 Comp := First (Component_Items (Comp_List));
3286 while Present (Comp) loop
3287 if Nkind (Comp) = N_Component_Declaration then
3289 -- Generate the component invariant check
3291 Process_Record_Component
3292 (Comp_Id => Defining_Entity (Comp),
3293 Comp_Checks => CL_Checks);
3294 end if;
3296 Next (Comp);
3297 end loop;
3299 -- Traverse the variant part
3301 if Present (Variant_Part (Comp_List)) then
3302 Var := First (Variants (Variant_Part (Comp_List)));
3303 while Present (Var) loop
3304 Var_Checks := No_List;
3306 -- Generate invariant checks for all components and variant
3307 -- parts that qualify.
3309 Process_Component_List
3310 (Comp_List => Component_List (Var),
3311 CL_Checks => Var_Checks);
3313 -- The components of the current variant produced at least
3314 -- one invariant check.
3316 if Present (Var_Checks) then
3317 Var_Stmts := Var_Checks;
3318 Produced_Variant_Check := True;
3320 -- Otherwise there are either no components with invariants,
3321 -- assertions are disabled, or Assertion_Policy Ignore is in
3322 -- effect.
3324 else
3325 Var_Stmts := New_List (Make_Null_Statement (Loc));
3326 end if;
3328 Append_New_To (Var_Alts,
3329 Make_Case_Statement_Alternative (Loc,
3330 Discrete_Choices =>
3331 New_Copy_List (Discrete_Choices (Var)),
3332 Statements => Var_Stmts));
3334 Next (Var);
3335 end loop;
3337 -- Create a case statement which verifies the invariant checks
3338 -- of a particular component list depending on the discriminant
3339 -- values only when there is at least one real invariant check.
3341 if Produced_Variant_Check then
3342 Append_New_To (CL_Checks,
3343 Make_Case_Statement (Loc,
3344 Expression =>
3345 Make_Selected_Component (Loc,
3346 Prefix => New_Occurrence_Of (Obj_Id, Loc),
3347 Selector_Name =>
3348 New_Occurrence_Of
3349 (Entity (Name (Variant_Part (Comp_List))), Loc)),
3350 Alternatives => Var_Alts));
3351 end if;
3352 end if;
3353 end Process_Component_List;
3355 ------------------------------
3356 -- Process_Record_Component --
3357 ------------------------------
3359 procedure Process_Record_Component
3360 (Comp_Id : Entity_Id;
3361 Comp_Checks : in out List_Id)
3363 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
3364 Proc_Id : Entity_Id;
3366 Produced_Component_Check : Boolean := False;
3367 -- This flag tracks whether the component has produced at least
3368 -- one invariant check.
3370 begin
3371 -- Nothing to do for internal component _parent. Note that it is
3372 -- not desirable to check whether the component comes from source
3373 -- because protected type components are relocated to an internal
3374 -- corresponding record, but still need processing.
3376 if Chars (Comp_Id) = Name_uParent then
3377 return;
3378 end if;
3380 -- Verify the invariant of the component. Note that an access
3381 -- type may have an invariant when it acts as the full view of a
3382 -- private type and the invariant appears on the partial view. In
3383 -- this case verify the access value itself.
3385 if Has_Invariants (Comp_Typ) then
3387 -- In GNATprove mode, the component invariants are checked by
3388 -- other means. They should not be added to the record type
3389 -- invariant procedure, so that the procedure can be used to
3390 -- check the record type invariants if any.
3392 if GNATprove_Mode then
3393 null;
3395 else
3396 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
3398 -- The component type should have an invariant procedure
3399 -- if it has invariants of its own or inherits class-wide
3400 -- invariants from parent or interface types.
3402 -- However, given that the invariant procedure is built by
3403 -- the expander, it is not available compiling generic units
3404 -- or when the sources have errors, since expansion is then
3405 -- disabled.
3407 pragma Assert (Present (Proc_Id)
3408 or else not Expander_Active);
3410 -- Generate:
3411 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
3413 -- Note that the invariant procedure may have a null body if
3414 -- assertions are disabled or Assertion_Policy Ignore is in
3415 -- effect.
3417 if Present (Proc_Id)
3418 and then not Has_Null_Body (Proc_Id)
3419 then
3420 Append_New_To (Comp_Checks,
3421 Make_Procedure_Call_Statement (Loc,
3422 Name =>
3423 New_Occurrence_Of (Proc_Id, Loc),
3424 Parameter_Associations => New_List (
3425 Make_Selected_Component (Loc,
3426 Prefix =>
3427 Unchecked_Convert_To
3428 (T, New_Occurrence_Of (Obj_Id, Loc)),
3429 Selector_Name =>
3430 New_Occurrence_Of (Comp_Id, Loc)))));
3431 end if;
3432 end if;
3434 Produced_Check := True;
3435 Produced_Component_Check := True;
3436 end if;
3438 if Produced_Component_Check and then Has_Unchecked_Union (T) then
3439 Error_Msg_NE
3440 ("invariants cannot be checked on components of "
3441 & "unchecked_union type &??", Comp_Id, T);
3442 end if;
3443 end Process_Record_Component;
3445 -- Local variables
3447 Comps : Node_Id;
3448 Def : Node_Id;
3450 -- Start of processing for Add_Record_Component_Invariants
3452 begin
3453 -- An untagged derived type inherits the components of its parent
3454 -- type. In order to avoid creating redundant invariant checks, do
3455 -- not process the components now. Instead wait until the ultimate
3456 -- parent of the untagged derivation chain is reached.
3458 if not Is_Untagged_Derivation (T) then
3459 Def := Type_Definition (Parent (T));
3461 if Nkind (Def) = N_Derived_Type_Definition then
3462 Def := Record_Extension_Part (Def);
3463 end if;
3465 pragma Assert (Nkind (Def) = N_Record_Definition);
3466 Comps := Component_List (Def);
3468 if Present (Comps) then
3469 Process_Component_List
3470 (Comp_List => Comps,
3471 CL_Checks => Checks);
3472 end if;
3473 end if;
3474 end Add_Record_Component_Invariants;
3476 -- Local variables
3478 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3479 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3480 -- Save the Ghost-related attributes to restore on exit
3482 Dummy : Entity_Id;
3483 Priv_Item : Node_Id;
3484 Proc_Body : Node_Id;
3485 Proc_Body_Id : Entity_Id;
3486 Proc_Decl : Node_Id;
3487 Proc_Id : Entity_Id;
3488 Stmts : List_Id := No_List;
3490 CRec_Typ : Entity_Id := Empty;
3491 -- The corresponding record type of Full_Typ
3493 Full_Proc : Entity_Id := Empty;
3494 -- The entity of the "full" invariant procedure
3496 Full_Typ : Entity_Id := Empty;
3497 -- The full view of the working type
3499 Obj_Id : Entity_Id := Empty;
3500 -- The _object formal parameter of the invariant procedure
3502 Part_Proc : Entity_Id := Empty;
3503 -- The entity of the "partial" invariant procedure
3505 Priv_Typ : Entity_Id := Empty;
3506 -- The partial view of the working type
3508 Work_Typ : Entity_Id := Empty;
3509 -- The working type
3511 -- Start of processing for Build_Invariant_Procedure_Body
3513 begin
3514 Work_Typ := Typ;
3516 -- Do not process the underlying full view of a private type. There is
3517 -- no way to get back to the partial view, plus the body will be built
3518 -- by the full view or the base type.
3520 if Is_Underlying_Full_View (Work_Typ) then
3521 return;
3523 -- The input type denotes the implementation base type of a constrained
3524 -- array type. Work with the first subtype as all invariant pragmas are
3525 -- on its rep item chain.
3527 elsif Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3528 Work_Typ := First_Subtype (Work_Typ);
3530 -- The input type denotes the corresponding record type of a protected
3531 -- or task type. Work with the concurrent type because the corresponding
3532 -- record type may not be visible to clients of the type.
3534 elsif Ekind (Work_Typ) = E_Record_Type
3535 and then Is_Concurrent_Record_Type (Work_Typ)
3536 then
3537 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3538 end if;
3540 -- The working type may be subject to pragma Ghost. Set the mode now to
3541 -- ensure that the invariant procedure is properly marked as Ghost.
3543 Set_Ghost_Mode (Work_Typ);
3545 -- The type must either have invariants of its own, inherit class-wide
3546 -- invariants from parent types or interfaces, or be an array or record
3547 -- type whose components have invariants.
3549 pragma Assert (Has_Invariants (Work_Typ));
3551 -- Interfaces are treated as the partial view of a private type in order
3552 -- to achieve uniformity with the general case.
3554 if Is_Interface (Work_Typ) then
3555 Priv_Typ := Work_Typ;
3557 -- Otherwise obtain both views of the type
3559 else
3560 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3561 end if;
3563 -- The caller requests a body for the partial invariant procedure
3565 if Partial_Invariant then
3566 Full_Proc := Invariant_Procedure (Work_Typ);
3567 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3569 -- The "full" invariant procedure body was already created
3571 if Present (Full_Proc)
3572 and then Present
3573 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3574 then
3575 -- This scenario happens only when the type is an untagged
3576 -- derivation from a private parent and the underlying full
3577 -- view was processed before the partial view.
3579 pragma Assert
3580 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3582 -- Nothing to do because the processing of the underlying full
3583 -- view already checked the invariants of the partial view.
3585 goto Leave;
3586 end if;
3588 -- Create a declaration for the "partial" invariant procedure if it
3589 -- is not available.
3591 if No (Proc_Id) then
3592 Build_Invariant_Procedure_Declaration
3593 (Typ => Work_Typ,
3594 Partial_Invariant => True);
3596 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3597 end if;
3599 -- The caller requests a body for the "full" invariant procedure
3601 else
3602 Proc_Id := Invariant_Procedure (Work_Typ);
3603 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3605 -- Create a declaration for the "full" invariant procedure if it is
3606 -- not available.
3608 if No (Proc_Id) then
3609 Build_Invariant_Procedure_Declaration (Work_Typ);
3610 Proc_Id := Invariant_Procedure (Work_Typ);
3611 end if;
3612 end if;
3614 -- At this point there should be an invariant procedure declaration
3616 pragma Assert (Present (Proc_Id));
3617 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3619 -- Nothing to do if the invariant procedure already has a body
3621 if Present (Corresponding_Body (Proc_Decl)) then
3622 goto Leave;
3623 end if;
3625 -- Emulate the environment of the invariant procedure by installing its
3626 -- scope and formal parameters. Note that this is not needed, but having
3627 -- the scope installed helps with the detection of invariant-related
3628 -- errors.
3630 Push_Scope (Proc_Id);
3631 Install_Formals (Proc_Id);
3633 Obj_Id := First_Formal (Proc_Id);
3634 pragma Assert (Present (Obj_Id));
3636 -- The "partial" invariant procedure verifies the invariants of the
3637 -- partial view only.
3639 if Partial_Invariant then
3640 pragma Assert (Present (Priv_Typ));
3642 Add_Own_Invariants
3643 (T => Priv_Typ,
3644 Obj_Id => Obj_Id,
3645 Checks => Stmts);
3647 -- Otherwise the "full" invariant procedure verifies the invariants of
3648 -- the full view, all array or record components, as well as class-wide
3649 -- invariants inherited from parent types or interfaces. In addition, it
3650 -- indirectly verifies the invariants of the partial view by calling the
3651 -- "partial" invariant procedure.
3653 else
3654 pragma Assert (Present (Full_Typ));
3656 -- Check the invariants of the partial view by calling the "partial"
3657 -- invariant procedure. Generate:
3659 -- <Work_Typ>Partial_Invariant (_object);
3661 if Present (Part_Proc) then
3662 Append_New_To (Stmts,
3663 Make_Procedure_Call_Statement (Loc,
3664 Name => New_Occurrence_Of (Part_Proc, Loc),
3665 Parameter_Associations => New_List (
3666 New_Occurrence_Of (Obj_Id, Loc))));
3668 Produced_Check := True;
3669 end if;
3671 Priv_Item := Empty;
3673 -- Derived subtypes do not have a partial view
3675 if Present (Priv_Typ) then
3677 -- The processing of the "full" invariant procedure intentionally
3678 -- skips the partial view because a) this may result in changes of
3679 -- visibility and b) lead to duplicate checks. However, when the
3680 -- full view is the underlying full view of an untagged derived
3681 -- type whose parent type is private, partial invariants appear on
3682 -- the rep item chain of the partial view only.
3684 -- package Pack_1 is
3685 -- type Root ... is private;
3686 -- private
3687 -- <full view of Root>
3688 -- end Pack_1;
3690 -- with Pack_1;
3691 -- package Pack_2 is
3692 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3693 -- <underlying full view of Child>
3694 -- end Pack_2;
3696 -- As a result, the processing of the full view must also consider
3697 -- all invariants of the partial view.
3699 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3700 null;
3702 -- Otherwise the invariants of the partial view are ignored
3704 else
3705 -- Note that the rep item chain is shared between the partial
3706 -- and full views of a type. To avoid processing the invariants
3707 -- of the partial view, signal the logic to stop when the first
3708 -- rep item of the partial view has been reached.
3710 Priv_Item := First_Rep_Item (Priv_Typ);
3712 -- Ignore the invariants of the partial view by eliminating the
3713 -- view.
3715 Priv_Typ := Empty;
3716 end if;
3717 end if;
3719 -- Process the invariants of the full view and in certain cases those
3720 -- of the partial view. This also handles any invariants on array or
3721 -- record components.
3723 Add_Own_Invariants
3724 (T => Priv_Typ,
3725 Obj_Id => Obj_Id,
3726 Checks => Stmts,
3727 Priv_Item => Priv_Item);
3729 Add_Own_Invariants
3730 (T => Full_Typ,
3731 Obj_Id => Obj_Id,
3732 Checks => Stmts,
3733 Priv_Item => Priv_Item);
3735 -- Process the elements of an array type
3737 if Is_Array_Type (Full_Typ) then
3738 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3740 -- Process the components of a record type
3742 elsif Ekind (Full_Typ) = E_Record_Type then
3743 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3745 -- Process the components of a corresponding record
3747 elsif Present (CRec_Typ) then
3748 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3749 end if;
3751 -- Process the inherited class-wide invariants of all parent types.
3752 -- This also handles any invariants on record components.
3754 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3756 -- Process the inherited class-wide invariants of all implemented
3757 -- interface types.
3759 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3760 end if;
3762 End_Scope;
3764 -- At this point there should be at least one invariant check. If this
3765 -- is not the case, then the invariant-related flags were not properly
3766 -- set, or there is a missing invariant procedure on one of the array
3767 -- or record components.
3769 pragma Assert (Produced_Check);
3771 -- Account for the case where assertions are disabled or all invariant
3772 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3773 -- empty body.
3775 if No (Stmts) then
3776 Stmts := New_List (Make_Null_Statement (Loc));
3777 end if;
3779 -- Generate:
3780 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3781 -- begin
3782 -- <Stmts>
3783 -- end <Work_Typ>[Partial_]Invariant;
3785 Proc_Body :=
3786 Make_Subprogram_Body (Loc,
3787 Specification =>
3788 Copy_Subprogram_Spec (Parent (Proc_Id)),
3789 Declarations => Empty_List,
3790 Handled_Statement_Sequence =>
3791 Make_Handled_Sequence_Of_Statements (Loc,
3792 Statements => Stmts));
3793 Proc_Body_Id := Defining_Entity (Proc_Body);
3795 -- Perform minor decoration in case the body is not analyzed
3797 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
3798 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3799 Set_Scope (Proc_Body_Id, Current_Scope);
3801 -- Link both spec and body to avoid generating duplicates
3803 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3804 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3806 -- The body should not be inserted into the tree when the context is
3807 -- a generic unit because it is not part of the template. Note
3808 -- that the body must still be generated in order to resolve the
3809 -- invariants.
3811 if Inside_A_Generic then
3812 null;
3814 -- Semi-insert the body into the tree for GNATprove by setting its
3815 -- Parent field. This allows for proper upstream tree traversals.
3817 elsif GNATprove_Mode then
3818 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3820 -- Otherwise the body is part of the freezing actions of the type
3822 else
3823 Append_Freeze_Action (Work_Typ, Proc_Body);
3824 end if;
3826 <<Leave>>
3827 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3828 end Build_Invariant_Procedure_Body;
3830 -------------------------------------------
3831 -- Build_Invariant_Procedure_Declaration --
3832 -------------------------------------------
3834 -- WARNING: This routine manages Ghost regions. Return statements must be
3835 -- replaced by gotos which jump to the end of the routine and restore the
3836 -- Ghost mode.
3838 procedure Build_Invariant_Procedure_Declaration
3839 (Typ : Entity_Id;
3840 Partial_Invariant : Boolean := False)
3842 Loc : constant Source_Ptr := Sloc (Typ);
3844 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3845 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3846 -- Save the Ghost-related attributes to restore on exit
3848 Proc_Decl : Node_Id;
3849 Proc_Id : Entity_Id;
3850 Proc_Nam : Name_Id;
3851 Typ_Decl : Node_Id;
3853 CRec_Typ : Entity_Id;
3854 -- The corresponding record type of Full_Typ
3856 Full_Typ : Entity_Id;
3857 -- The full view of working type
3859 Obj_Id : Entity_Id;
3860 -- The _object formal parameter of the invariant procedure
3862 Obj_Typ : Entity_Id;
3863 -- The type of the _object formal parameter
3865 Priv_Typ : Entity_Id;
3866 -- The partial view of working type
3868 UFull_Typ : Entity_Id;
3869 -- The underlying full view of Full_Typ
3871 Work_Typ : Entity_Id;
3872 -- The working type
3874 begin
3875 Work_Typ := Typ;
3877 -- The input type denotes the implementation base type of a constrained
3878 -- array type. Work with the first subtype as all invariant pragmas are
3879 -- on its rep item chain.
3881 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3882 Work_Typ := First_Subtype (Work_Typ);
3884 -- The input denotes the corresponding record type of a protected or a
3885 -- task type. Work with the concurrent type because the corresponding
3886 -- record type may not be visible to clients of the type.
3888 elsif Ekind (Work_Typ) = E_Record_Type
3889 and then Is_Concurrent_Record_Type (Work_Typ)
3890 then
3891 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3892 end if;
3894 -- The working type may be subject to pragma Ghost. Set the mode now to
3895 -- ensure that the invariant procedure is properly marked as Ghost.
3897 Set_Ghost_Mode (Work_Typ);
3899 -- The type must either have invariants of its own, inherit class-wide
3900 -- invariants from parent or interface types, or be an array or record
3901 -- type whose components have invariants.
3903 pragma Assert (Has_Invariants (Work_Typ));
3905 -- Nothing to do if the type already has a "partial" invariant procedure
3907 if Partial_Invariant then
3908 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3909 goto Leave;
3910 end if;
3912 -- Nothing to do if the type already has a "full" invariant procedure
3914 elsif Present (Invariant_Procedure (Work_Typ)) then
3915 goto Leave;
3916 end if;
3918 -- The caller requests the declaration of the "partial" invariant
3919 -- procedure.
3921 if Partial_Invariant then
3922 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3924 -- Otherwise the caller requests the declaration of the "full" invariant
3925 -- procedure.
3927 else
3928 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3929 end if;
3931 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3933 -- Perform minor decoration in case the declaration is not analyzed
3935 Mutate_Ekind (Proc_Id, E_Procedure);
3936 Set_Etype (Proc_Id, Standard_Void_Type);
3937 Set_Scope (Proc_Id, Current_Scope);
3939 if Partial_Invariant then
3940 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3941 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3942 else
3943 Set_Is_Invariant_Procedure (Proc_Id);
3944 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3945 end if;
3947 -- The invariant procedure requires debug info when the invariants are
3948 -- subject to Source Coverage Obligations.
3950 if Generate_SCO then
3951 Set_Debug_Info_Needed (Proc_Id);
3952 end if;
3954 -- Obtain all views of the input type
3956 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
3958 -- Associate the invariant procedure and various flags with all views
3960 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3961 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3962 Propagate_Invariant_Attributes (UFull_Typ, From_Typ => Work_Typ);
3963 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3965 -- The declaration of the invariant procedure is inserted after the
3966 -- declaration of the partial view as this allows for proper external
3967 -- visibility.
3969 if Present (Priv_Typ) then
3970 Typ_Decl := Declaration_Node (Priv_Typ);
3972 -- Anonymous arrays in object declarations have no explicit declaration
3973 -- so use the related object declaration as the insertion point.
3975 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3976 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3978 -- Derived types with the full view as parent do not have a partial
3979 -- view. Insert the invariant procedure after the derived type.
3981 else
3982 Typ_Decl := Declaration_Node (Full_Typ);
3983 end if;
3985 -- The type should have a declarative node
3987 pragma Assert (Present (Typ_Decl));
3989 -- Create the formal parameter which emulates the variable-like behavior
3990 -- of the current type instance.
3992 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3994 -- When generating an invariant procedure declaration for an abstract
3995 -- type (including interfaces), use the class-wide type as the _object
3996 -- type. This has several desirable effects:
3998 -- * The invariant procedure does not become a primitive of the type.
3999 -- This eliminates the need to either special case the treatment of
4000 -- invariant procedures, or to make it a predefined primitive and
4001 -- force every derived type to potentially provide an empty body.
4003 -- * The invariant procedure does not need to be declared as abstract.
4004 -- This allows for a proper body, which in turn avoids redundant
4005 -- processing of the same invariants for types with multiple views.
4007 -- * The class-wide type allows for calls to abstract primitives
4008 -- within a nonabstract subprogram. The calls are treated as
4009 -- dispatching and require additional processing when they are
4010 -- remapped to call primitives of derived types. See routine
4011 -- Replace_References for details.
4013 if Is_Abstract_Type (Work_Typ) then
4014 Obj_Typ := Class_Wide_Type (Work_Typ);
4015 else
4016 Obj_Typ := Work_Typ;
4017 end if;
4019 -- Perform minor decoration in case the declaration is not analyzed
4021 Mutate_Ekind (Obj_Id, E_In_Parameter);
4022 Set_Etype (Obj_Id, Obj_Typ);
4023 Set_Scope (Obj_Id, Proc_Id);
4025 Set_First_Entity (Proc_Id, Obj_Id);
4026 Set_Last_Entity (Proc_Id, Obj_Id);
4028 -- Generate:
4029 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
4031 Proc_Decl :=
4032 Make_Subprogram_Declaration (Loc,
4033 Specification =>
4034 Make_Procedure_Specification (Loc,
4035 Defining_Unit_Name => Proc_Id,
4036 Parameter_Specifications => New_List (
4037 Make_Parameter_Specification (Loc,
4038 Defining_Identifier => Obj_Id,
4039 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
4041 -- The declaration should not be inserted into the tree when the context
4042 -- is a generic unit because it is not part of the template.
4044 if Inside_A_Generic then
4045 null;
4047 -- Semi-insert the declaration into the tree for GNATprove by setting
4048 -- its Parent field. This allows for proper upstream tree traversals.
4050 elsif GNATprove_Mode then
4051 Set_Parent (Proc_Decl, Parent (Typ_Decl));
4053 -- Otherwise insert the declaration
4055 else
4056 pragma Assert (Present (Typ_Decl));
4057 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
4058 end if;
4060 <<Leave>>
4061 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4062 end Build_Invariant_Procedure_Declaration;
4064 --------------------------
4065 -- Build_Procedure_Form --
4066 --------------------------
4068 procedure Build_Procedure_Form (N : Node_Id) is
4069 Loc : constant Source_Ptr := Sloc (N);
4070 Subp : constant Entity_Id := Defining_Entity (N);
4072 Func_Formal : Entity_Id;
4073 Proc_Formals : List_Id;
4074 Proc_Decl : Node_Id;
4076 begin
4077 -- No action needed if this transformation was already done, or in case
4078 -- of subprogram renaming declarations.
4080 if Nkind (Specification (N)) = N_Procedure_Specification
4081 or else Nkind (N) = N_Subprogram_Renaming_Declaration
4082 then
4083 return;
4084 end if;
4086 -- Ditto when dealing with an expression function, where both the
4087 -- original expression and the generated declaration end up being
4088 -- expanded here.
4090 if Rewritten_For_C (Subp) then
4091 return;
4092 end if;
4094 Proc_Formals := New_List;
4096 -- Create a list of formal parameters with the same types as the
4097 -- function.
4099 Func_Formal := First_Formal (Subp);
4100 while Present (Func_Formal) loop
4101 Append_To (Proc_Formals,
4102 Make_Parameter_Specification (Loc,
4103 Defining_Identifier =>
4104 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
4105 Parameter_Type =>
4106 New_Occurrence_Of (Etype (Func_Formal), Loc)));
4108 Next_Formal (Func_Formal);
4109 end loop;
4111 -- Add an extra out parameter to carry the function result
4113 Append_To (Proc_Formals,
4114 Make_Parameter_Specification (Loc,
4115 Defining_Identifier =>
4116 Make_Defining_Identifier (Loc, Name_UP_RESULT),
4117 Out_Present => True,
4118 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
4120 -- The new procedure declaration is inserted before the function
4121 -- declaration. The processing in Build_Procedure_Body_Form relies on
4122 -- this order. Note that we insert before because in the case of a
4123 -- function body with no separate spec, we do not want to insert the
4124 -- new spec after the body which will later get rewritten.
4126 Proc_Decl :=
4127 Make_Subprogram_Declaration (Loc,
4128 Specification =>
4129 Make_Procedure_Specification (Loc,
4130 Defining_Unit_Name =>
4131 Make_Defining_Identifier (Loc, Chars (Subp)),
4132 Parameter_Specifications => Proc_Formals));
4134 Insert_Before_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
4136 -- Entity of procedure must remain invisible so that it does not
4137 -- overload subsequent references to the original function.
4139 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
4141 -- Mark the function as having a procedure form and link the function
4142 -- and its internally built procedure.
4144 Set_Rewritten_For_C (Subp);
4145 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
4146 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
4147 end Build_Procedure_Form;
4149 ------------------------
4150 -- Build_Runtime_Call --
4151 ------------------------
4153 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
4154 begin
4155 -- If entity is not available, we can skip making the call (this avoids
4156 -- junk duplicated error messages in a number of cases).
4158 if not RTE_Available (RE) then
4159 return Make_Null_Statement (Loc);
4160 else
4161 return
4162 Make_Procedure_Call_Statement (Loc,
4163 Name => New_Occurrence_Of (RTE (RE), Loc));
4164 end if;
4165 end Build_Runtime_Call;
4167 ------------------------
4168 -- Build_SS_Mark_Call --
4169 ------------------------
4171 function Build_SS_Mark_Call
4172 (Loc : Source_Ptr;
4173 Mark : Entity_Id) return Node_Id
4175 begin
4176 -- Generate:
4177 -- Mark : constant Mark_Id := SS_Mark;
4179 return
4180 Make_Object_Declaration (Loc,
4181 Defining_Identifier => Mark,
4182 Constant_Present => True,
4183 Object_Definition =>
4184 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
4185 Expression =>
4186 Make_Function_Call (Loc,
4187 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
4188 end Build_SS_Mark_Call;
4190 ---------------------------
4191 -- Build_SS_Release_Call --
4192 ---------------------------
4194 function Build_SS_Release_Call
4195 (Loc : Source_Ptr;
4196 Mark : Entity_Id) return Node_Id
4198 begin
4199 -- Generate:
4200 -- SS_Release (Mark);
4202 return
4203 Make_Procedure_Call_Statement (Loc,
4204 Name =>
4205 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
4206 Parameter_Associations => New_List (
4207 New_Occurrence_Of (Mark, Loc)));
4208 end Build_SS_Release_Call;
4210 ----------------------------
4211 -- Build_Task_Array_Image --
4212 ----------------------------
4214 -- This function generates the body for a function that constructs the
4215 -- image string for a task that is an array component. The function is
4216 -- local to the init proc for the array type, and is called for each one
4217 -- of the components. The constructed image has the form of an indexed
4218 -- component, whose prefix is the outer variable of the array type.
4219 -- The n-dimensional array type has known indexes Index, Index2...
4221 -- Id_Ref is an indexed component form created by the enclosing init proc.
4222 -- Its successive indexes are Val1, Val2, ... which are the loop variables
4223 -- in the loops that call the individual task init proc on each component.
4225 -- The generated function has the following structure:
4227 -- function F return String is
4228 -- Pref : String renames Task_Name;
4229 -- T1 : constant String := Index1'Image (Val1);
4230 -- ...
4231 -- Tn : constant String := Indexn'Image (Valn);
4232 -- Len : constant Integer :=
4233 -- Pref'Length + T1'Length + ... + Tn'Length + n + 1;
4234 -- -- Len includes commas and the end parentheses
4236 -- Res : String (1 .. Len);
4237 -- Pos : Integer := Pref'Length;
4239 -- begin
4240 -- Res (1 .. Pos) := Pref;
4241 -- Pos := Pos + 1;
4242 -- Res (Pos) := '(';
4243 -- Pos := Pos + 1;
4244 -- Res (Pos .. Pos + T1'Length - 1) := T1;
4245 -- Pos := Pos + T1'Length;
4246 -- Res (Pos) := '.';
4247 -- Pos := Pos + 1;
4248 -- ...
4249 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
4250 -- Res (Len) := ')';
4252 -- return Res;
4253 -- end F;
4255 -- Needless to say, multidimensional arrays of tasks are rare enough that
4256 -- the bulkiness of this code is not really a concern.
4258 function Build_Task_Array_Image
4259 (Loc : Source_Ptr;
4260 Id_Ref : Node_Id;
4261 A_Type : Entity_Id;
4262 Dyn : Boolean := False) return Node_Id
4264 Dims : constant Nat := Number_Dimensions (A_Type);
4265 -- Number of dimensions for array of tasks
4267 Temps : array (1 .. Dims) of Entity_Id;
4268 -- Array of temporaries to hold string for each index
4270 Indx : Node_Id;
4271 -- Index expression
4273 Len : Entity_Id;
4274 -- Total length of generated name
4276 Pos : Entity_Id;
4277 -- Running index for substring assignments
4279 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4280 -- Name of enclosing variable, prefix of resulting name
4282 Res : Entity_Id;
4283 -- String to hold result
4285 Val : Node_Id;
4286 -- Value of successive indexes
4288 Sum : Node_Id;
4289 -- Expression to compute total size of string
4291 T : Entity_Id;
4292 -- Entity for name at one index position
4294 Decls : constant List_Id := New_List;
4295 Stats : constant List_Id := New_List;
4297 begin
4298 -- For a dynamic task, the name comes from the target variable. For a
4299 -- static one it is a formal of the enclosing init proc.
4301 if Dyn then
4302 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4303 Append_To (Decls,
4304 Make_Object_Declaration (Loc,
4305 Defining_Identifier => Pref,
4306 Constant_Present => True,
4307 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4308 Expression =>
4309 Make_String_Literal (Loc,
4310 Strval => String_From_Name_Buffer)));
4312 else
4313 Append_To (Decls,
4314 Make_Object_Renaming_Declaration (Loc,
4315 Defining_Identifier => Pref,
4316 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4317 Name => Make_Identifier (Loc, Name_uTask_Name)));
4318 end if;
4320 Indx := First_Index (A_Type);
4321 Val := First (Expressions (Id_Ref));
4323 for J in 1 .. Dims loop
4324 T := Make_Temporary (Loc, 'T');
4325 Temps (J) := T;
4327 Append_To (Decls,
4328 Make_Object_Declaration (Loc,
4329 Defining_Identifier => T,
4330 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4331 Constant_Present => True,
4332 Expression =>
4333 Make_Attribute_Reference (Loc,
4334 Attribute_Name => Name_Image,
4335 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
4336 Expressions => New_List (New_Copy_Tree (Val)))));
4338 Next_Index (Indx);
4339 Next (Val);
4340 end loop;
4342 Sum := Make_Integer_Literal (Loc, Dims + 1);
4344 Sum :=
4345 Make_Op_Add (Loc,
4346 Left_Opnd => Sum,
4347 Right_Opnd =>
4348 Make_Attribute_Reference (Loc,
4349 Attribute_Name => Name_Length,
4350 Prefix => New_Occurrence_Of (Pref, Loc),
4351 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4353 for J in 1 .. Dims loop
4354 Sum :=
4355 Make_Op_Add (Loc,
4356 Left_Opnd => Sum,
4357 Right_Opnd =>
4358 Make_Attribute_Reference (Loc,
4359 Attribute_Name => Name_Length,
4360 Prefix =>
4361 New_Occurrence_Of (Temps (J), Loc),
4362 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4363 end loop;
4365 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4367 Set_Character_Literal_Name (Get_Char_Code ('('));
4369 Append_To (Stats,
4370 Make_Assignment_Statement (Loc,
4371 Name =>
4372 Make_Indexed_Component (Loc,
4373 Prefix => New_Occurrence_Of (Res, Loc),
4374 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4375 Expression =>
4376 Make_Character_Literal (Loc,
4377 Chars => Name_Find,
4378 Char_Literal_Value => UI_From_CC (Get_Char_Code ('(')))));
4380 Append_To (Stats,
4381 Make_Assignment_Statement (Loc,
4382 Name => New_Occurrence_Of (Pos, Loc),
4383 Expression =>
4384 Make_Op_Add (Loc,
4385 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4386 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4388 for J in 1 .. Dims loop
4390 Append_To (Stats,
4391 Make_Assignment_Statement (Loc,
4392 Name =>
4393 Make_Slice (Loc,
4394 Prefix => New_Occurrence_Of (Res, Loc),
4395 Discrete_Range =>
4396 Make_Range (Loc,
4397 Low_Bound => New_Occurrence_Of (Pos, Loc),
4398 High_Bound =>
4399 Make_Op_Subtract (Loc,
4400 Left_Opnd =>
4401 Make_Op_Add (Loc,
4402 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4403 Right_Opnd =>
4404 Make_Attribute_Reference (Loc,
4405 Attribute_Name => Name_Length,
4406 Prefix =>
4407 New_Occurrence_Of (Temps (J), Loc),
4408 Expressions =>
4409 New_List (Make_Integer_Literal (Loc, 1)))),
4410 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
4412 Expression => New_Occurrence_Of (Temps (J), Loc)));
4414 if J < Dims then
4415 Append_To (Stats,
4416 Make_Assignment_Statement (Loc,
4417 Name => New_Occurrence_Of (Pos, Loc),
4418 Expression =>
4419 Make_Op_Add (Loc,
4420 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4421 Right_Opnd =>
4422 Make_Attribute_Reference (Loc,
4423 Attribute_Name => Name_Length,
4424 Prefix => New_Occurrence_Of (Temps (J), Loc),
4425 Expressions =>
4426 New_List (Make_Integer_Literal (Loc, 1))))));
4428 Set_Character_Literal_Name (Get_Char_Code (','));
4430 Append_To (Stats,
4431 Make_Assignment_Statement (Loc,
4432 Name => Make_Indexed_Component (Loc,
4433 Prefix => New_Occurrence_Of (Res, Loc),
4434 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4435 Expression =>
4436 Make_Character_Literal (Loc,
4437 Chars => Name_Find,
4438 Char_Literal_Value => UI_From_CC (Get_Char_Code (',')))));
4440 Append_To (Stats,
4441 Make_Assignment_Statement (Loc,
4442 Name => New_Occurrence_Of (Pos, Loc),
4443 Expression =>
4444 Make_Op_Add (Loc,
4445 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4446 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4447 end if;
4448 end loop;
4450 Set_Character_Literal_Name (Get_Char_Code (')'));
4452 Append_To (Stats,
4453 Make_Assignment_Statement (Loc,
4454 Name =>
4455 Make_Indexed_Component (Loc,
4456 Prefix => New_Occurrence_Of (Res, Loc),
4457 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
4458 Expression =>
4459 Make_Character_Literal (Loc,
4460 Chars => Name_Find,
4461 Char_Literal_Value => UI_From_CC (Get_Char_Code (')')))));
4462 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4463 end Build_Task_Array_Image;
4465 ----------------------------
4466 -- Build_Task_Image_Decls --
4467 ----------------------------
4469 function Build_Task_Image_Decls
4470 (Loc : Source_Ptr;
4471 Id_Ref : Node_Id;
4472 A_Type : Entity_Id;
4473 In_Init_Proc : Boolean := False) return List_Id
4475 Decls : constant List_Id := New_List;
4476 T_Id : Entity_Id := Empty;
4477 Decl : Node_Id;
4478 Expr : Node_Id := Empty;
4479 Fun : Node_Id := Empty;
4480 Is_Dyn : constant Boolean :=
4481 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
4482 and then
4483 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
4485 Component_Suffix_Index : constant Int :=
4486 (if In_Init_Proc then -1 else 0);
4487 -- If an init proc calls Build_Task_Image_Decls twice for its
4488 -- _Parent component (to split early/late initialization), we don't
4489 -- want two decls with the same name. Hence, the -1 suffix.
4491 begin
4492 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
4493 -- generate a dummy declaration only.
4495 if Restriction_Active (No_Implicit_Heap_Allocations)
4496 or else Global_Discard_Names
4497 then
4498 T_Id := Make_Temporary (Loc, 'J');
4499 Name_Len := 0;
4501 return
4502 New_List (
4503 Make_Object_Declaration (Loc,
4504 Defining_Identifier => T_Id,
4505 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4506 Expression =>
4507 Make_String_Literal (Loc,
4508 Strval => String_From_Name_Buffer)));
4510 else
4511 if Nkind (Id_Ref) = N_Identifier
4512 or else Nkind (Id_Ref) = N_Defining_Identifier
4513 then
4514 -- For a simple variable, the image of the task is built from
4515 -- the name of the variable. To avoid possible conflict with the
4516 -- anonymous type created for a single protected object, add a
4517 -- numeric suffix.
4519 T_Id :=
4520 Make_Defining_Identifier (Loc,
4521 New_External_Name (Chars (Id_Ref), 'T', 1));
4523 Get_Name_String (Chars (Id_Ref));
4525 Expr :=
4526 Make_String_Literal (Loc,
4527 Strval => String_From_Name_Buffer);
4529 elsif Nkind (Id_Ref) = N_Selected_Component then
4530 T_Id :=
4531 Make_Defining_Identifier (Loc,
4532 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T',
4533 Suffix_Index => Component_Suffix_Index));
4534 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
4536 elsif Nkind (Id_Ref) = N_Indexed_Component then
4537 T_Id :=
4538 Make_Defining_Identifier (Loc,
4539 New_External_Name (Chars (A_Type), 'N'));
4541 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
4542 end if;
4543 end if;
4545 if Present (Fun) then
4546 Append (Fun, Decls);
4547 Expr := Make_Function_Call (Loc,
4548 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
4550 if not In_Init_Proc then
4551 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4552 end if;
4553 end if;
4555 Decl := Make_Object_Declaration (Loc,
4556 Defining_Identifier => T_Id,
4557 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4558 Constant_Present => True,
4559 Expression => Expr);
4561 Append (Decl, Decls);
4562 return Decls;
4563 end Build_Task_Image_Decls;
4565 -------------------------------
4566 -- Build_Task_Image_Function --
4567 -------------------------------
4569 function Build_Task_Image_Function
4570 (Loc : Source_Ptr;
4571 Decls : List_Id;
4572 Stats : List_Id;
4573 Res : Entity_Id) return Node_Id
4575 Spec : Node_Id;
4577 begin
4578 Append_To (Stats,
4579 Make_Simple_Return_Statement (Loc,
4580 Expression => New_Occurrence_Of (Res, Loc)));
4582 Spec := Make_Function_Specification (Loc,
4583 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4584 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4586 -- Calls to 'Image use the secondary stack, which must be cleaned up
4587 -- after the task name is built.
4589 return Make_Subprogram_Body (Loc,
4590 Specification => Spec,
4591 Declarations => Decls,
4592 Handled_Statement_Sequence =>
4593 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4594 end Build_Task_Image_Function;
4596 -----------------------------
4597 -- Build_Task_Image_Prefix --
4598 -----------------------------
4600 procedure Build_Task_Image_Prefix
4601 (Loc : Source_Ptr;
4602 Len : out Entity_Id;
4603 Res : out Entity_Id;
4604 Pos : out Entity_Id;
4605 Prefix : Entity_Id;
4606 Sum : Node_Id;
4607 Decls : List_Id;
4608 Stats : List_Id)
4610 begin
4611 Len := Make_Temporary (Loc, 'L', Sum);
4613 Append_To (Decls,
4614 Make_Object_Declaration (Loc,
4615 Defining_Identifier => Len,
4616 Constant_Present => True,
4617 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4618 Expression => Sum));
4620 Res := Make_Temporary (Loc, 'R');
4622 Append_To (Decls,
4623 Make_Object_Declaration (Loc,
4624 Defining_Identifier => Res,
4625 Object_Definition =>
4626 Make_Subtype_Indication (Loc,
4627 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4628 Constraint =>
4629 Make_Index_Or_Discriminant_Constraint (Loc,
4630 Constraints =>
4631 New_List (
4632 Make_Range (Loc,
4633 Low_Bound => Make_Integer_Literal (Loc, 1),
4634 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4636 -- Indicate that the result is an internal temporary, so it does not
4637 -- receive a bogus initialization when declaration is expanded. This
4638 -- is both efficient, and prevents anomalies in the handling of
4639 -- dynamic objects on the secondary stack.
4641 Set_Is_Internal (Res);
4642 Pos := Make_Temporary (Loc, 'P');
4644 Append_To (Decls,
4645 Make_Object_Declaration (Loc,
4646 Defining_Identifier => Pos,
4647 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4649 -- Pos := Prefix'Length;
4651 Append_To (Stats,
4652 Make_Assignment_Statement (Loc,
4653 Name => New_Occurrence_Of (Pos, Loc),
4654 Expression =>
4655 Make_Attribute_Reference (Loc,
4656 Attribute_Name => Name_Length,
4657 Prefix => New_Occurrence_Of (Prefix, Loc),
4658 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4660 -- Res (1 .. Pos) := Prefix;
4662 Append_To (Stats,
4663 Make_Assignment_Statement (Loc,
4664 Name =>
4665 Make_Slice (Loc,
4666 Prefix => New_Occurrence_Of (Res, Loc),
4667 Discrete_Range =>
4668 Make_Range (Loc,
4669 Low_Bound => Make_Integer_Literal (Loc, 1),
4670 High_Bound => New_Occurrence_Of (Pos, Loc))),
4672 Expression => New_Occurrence_Of (Prefix, Loc)));
4674 Append_To (Stats,
4675 Make_Assignment_Statement (Loc,
4676 Name => New_Occurrence_Of (Pos, Loc),
4677 Expression =>
4678 Make_Op_Add (Loc,
4679 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4680 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4681 end Build_Task_Image_Prefix;
4683 -----------------------------
4684 -- Build_Task_Record_Image --
4685 -----------------------------
4687 function Build_Task_Record_Image
4688 (Loc : Source_Ptr;
4689 Id_Ref : Node_Id;
4690 Dyn : Boolean := False) return Node_Id
4692 Len : Entity_Id;
4693 -- Total length of generated name
4695 Pos : Entity_Id;
4696 -- Index into result
4698 Res : Entity_Id;
4699 -- String to hold result
4701 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4702 -- Name of enclosing variable, prefix of resulting name
4704 Sum : Node_Id;
4705 -- Expression to compute total size of string
4707 Sel : Entity_Id;
4708 -- Entity for selector name
4710 Decls : constant List_Id := New_List;
4711 Stats : constant List_Id := New_List;
4713 begin
4714 -- For a dynamic task, the name comes from the target variable. For a
4715 -- static one it is a formal of the enclosing init proc.
4717 if Dyn then
4718 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4719 Append_To (Decls,
4720 Make_Object_Declaration (Loc,
4721 Defining_Identifier => Pref,
4722 Constant_Present => True,
4723 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4724 Expression =>
4725 Make_String_Literal (Loc,
4726 Strval => String_From_Name_Buffer)));
4728 else
4729 Append_To (Decls,
4730 Make_Object_Renaming_Declaration (Loc,
4731 Defining_Identifier => Pref,
4732 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4733 Name => Make_Identifier (Loc, Name_uTask_Name)));
4734 end if;
4736 Sel := Make_Temporary (Loc, 'S');
4738 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4740 Append_To (Decls,
4741 Make_Object_Declaration (Loc,
4742 Defining_Identifier => Sel,
4743 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4744 Expression =>
4745 Make_String_Literal (Loc,
4746 Strval => String_From_Name_Buffer)));
4748 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4750 Sum :=
4751 Make_Op_Add (Loc,
4752 Left_Opnd => Sum,
4753 Right_Opnd =>
4754 Make_Attribute_Reference (Loc,
4755 Attribute_Name => Name_Length,
4756 Prefix =>
4757 New_Occurrence_Of (Pref, Loc),
4758 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4760 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4762 Set_Character_Literal_Name (Get_Char_Code ('.'));
4764 -- Res (Pos) := '.';
4766 Append_To (Stats,
4767 Make_Assignment_Statement (Loc,
4768 Name => Make_Indexed_Component (Loc,
4769 Prefix => New_Occurrence_Of (Res, Loc),
4770 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4771 Expression =>
4772 Make_Character_Literal (Loc,
4773 Chars => Name_Find,
4774 Char_Literal_Value =>
4775 UI_From_CC (Get_Char_Code ('.')))));
4777 Append_To (Stats,
4778 Make_Assignment_Statement (Loc,
4779 Name => New_Occurrence_Of (Pos, Loc),
4780 Expression =>
4781 Make_Op_Add (Loc,
4782 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4783 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4785 -- Res (Pos .. Len) := Selector;
4787 Append_To (Stats,
4788 Make_Assignment_Statement (Loc,
4789 Name => Make_Slice (Loc,
4790 Prefix => New_Occurrence_Of (Res, Loc),
4791 Discrete_Range =>
4792 Make_Range (Loc,
4793 Low_Bound => New_Occurrence_Of (Pos, Loc),
4794 High_Bound => New_Occurrence_Of (Len, Loc))),
4795 Expression => New_Occurrence_Of (Sel, Loc)));
4797 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4798 end Build_Task_Record_Image;
4800 ----------------------------------------
4801 -- Build_Temporary_On_Secondary_Stack --
4802 ----------------------------------------
4804 function Build_Temporary_On_Secondary_Stack
4805 (Loc : Source_Ptr;
4806 Typ : Entity_Id;
4807 Code : List_Id) return Entity_Id
4809 Acc_Typ : Entity_Id;
4810 Alloc : Node_Id;
4811 Alloc_Obj : Entity_Id;
4813 begin
4814 pragma Assert (RTE_Available (RE_SS_Pool)
4815 and then not Needs_Finalization (Typ));
4817 Acc_Typ := Make_Temporary (Loc, 'A');
4818 Mutate_Ekind (Acc_Typ, E_Access_Type);
4819 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
4821 Append_To (Code,
4822 Make_Full_Type_Declaration (Loc,
4823 Defining_Identifier => Acc_Typ,
4824 Type_Definition =>
4825 Make_Access_To_Object_Definition (Loc,
4826 All_Present => True,
4827 Subtype_Indication =>
4828 New_Occurrence_Of (Typ, Loc))));
4830 Alloc :=
4831 Make_Allocator (Loc, Expression => New_Occurrence_Of (Typ, Loc));
4832 Set_No_Initialization (Alloc);
4834 Alloc_Obj := Make_Temporary (Loc, 'R');
4836 Append_To (Code,
4837 Make_Object_Declaration (Loc,
4838 Defining_Identifier => Alloc_Obj,
4839 Constant_Present => True,
4840 Object_Definition =>
4841 New_Occurrence_Of (Acc_Typ, Loc),
4842 Expression => Alloc));
4844 Set_Uses_Sec_Stack (Current_Scope);
4846 return Alloc_Obj;
4847 end Build_Temporary_On_Secondary_Stack;
4849 -----------------------------
4850 -- Check_Float_Op_Overflow --
4851 -----------------------------
4853 procedure Check_Float_Op_Overflow (N : Node_Id) is
4854 begin
4855 -- Return if no check needed
4857 if not Is_Floating_Point_Type (Etype (N))
4858 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4860 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4861 -- and do not expand the code for float overflow checking.
4863 or else CodePeer_Mode
4864 then
4865 return;
4866 end if;
4868 -- Otherwise we replace the expression by
4870 -- do Tnn : constant ftype := expression;
4871 -- constraint_error when not Tnn'Valid;
4872 -- in Tnn;
4874 declare
4875 Loc : constant Source_Ptr := Sloc (N);
4876 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4877 Typ : constant Entity_Id := Etype (N);
4879 begin
4880 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4881 -- right here. We also set the node as analyzed to prevent infinite
4882 -- recursion from repeating the operation in the expansion.
4884 Set_Do_Overflow_Check (N, False);
4885 Set_Analyzed (N, True);
4887 -- Do the rewrite to include the check
4889 Rewrite (N,
4890 Make_Expression_With_Actions (Loc,
4891 Actions => New_List (
4892 Make_Object_Declaration (Loc,
4893 Defining_Identifier => Tnn,
4894 Object_Definition => New_Occurrence_Of (Typ, Loc),
4895 Constant_Present => True,
4896 Expression => Relocate_Node (N)),
4897 Make_Raise_Constraint_Error (Loc,
4898 Condition =>
4899 Make_Op_Not (Loc,
4900 Right_Opnd =>
4901 Make_Attribute_Reference (Loc,
4902 Prefix => New_Occurrence_Of (Tnn, Loc),
4903 Attribute_Name => Name_Valid)),
4904 Reason => CE_Overflow_Check_Failed)),
4905 Expression => New_Occurrence_Of (Tnn, Loc)));
4907 Analyze_And_Resolve (N, Typ);
4908 end;
4909 end Check_Float_Op_Overflow;
4911 ----------------------------------
4912 -- Component_May_Be_Bit_Aligned --
4913 ----------------------------------
4915 function Component_May_Be_Bit_Aligned
4916 (Comp : Entity_Id;
4917 For_Slice : Boolean := False) return Boolean
4919 UT : Entity_Id;
4921 begin
4922 -- If no component clause, then everything is fine, since the back end
4923 -- never misaligns from byte boundaries by default, even if there is a
4924 -- pragma Pack for the record.
4926 if No (Comp) or else No (Component_Clause (Comp)) then
4927 return False;
4928 end if;
4930 UT := Underlying_Type (Etype (Comp));
4932 -- It is only array and record types that cause trouble
4934 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4935 return False;
4937 -- If we know that we have a small (at most the maximum integer size)
4938 -- bit-packed array or record without variant part, then everything is
4939 -- fine, since the back end can handle these cases correctly, except if
4940 -- a slice is involved.
4942 elsif Known_Esize (Comp)
4943 and then Esize (Comp) <= System_Max_Integer_Size
4944 and then (Is_Bit_Packed_Array (UT)
4945 or else (Is_Record_Type (UT)
4946 and then not Has_Variant_Part (UT)))
4947 and then not For_Slice
4948 then
4949 return False;
4951 elsif not Known_Normalized_First_Bit (Comp) then
4952 return True;
4954 -- Otherwise if the component is not byte aligned, we know we have the
4955 -- nasty unaligned case.
4957 elsif Normalized_First_Bit (Comp) /= Uint_0
4958 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4959 then
4960 return True;
4962 -- If we are large and byte aligned, then OK at this level
4964 else
4965 return False;
4966 end if;
4967 end Component_May_Be_Bit_Aligned;
4969 -------------------------------
4970 -- Convert_To_Actual_Subtype --
4971 -------------------------------
4973 procedure Convert_To_Actual_Subtype (Exp : Node_Id) is
4974 Act_ST : Entity_Id;
4976 begin
4977 Act_ST := Get_Actual_Subtype (Exp);
4979 if Act_ST = Etype (Exp) then
4980 return;
4981 else
4982 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4983 Analyze_And_Resolve (Exp, Act_ST);
4984 end if;
4985 end Convert_To_Actual_Subtype;
4987 -----------------------------------
4988 -- Corresponding_Runtime_Package --
4989 -----------------------------------
4991 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4992 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4993 -- Return True if protected type T has one entry and the maximum queue
4994 -- length is one.
4996 --------------------------------
4997 -- Has_One_Entry_And_No_Queue --
4998 --------------------------------
5000 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
5001 Item : Entity_Id;
5002 Is_First : Boolean := True;
5004 begin
5005 Item := First_Entity (T);
5006 while Present (Item) loop
5007 if Is_Entry (Item) then
5009 -- The protected type has more than one entry
5011 if not Is_First then
5012 return False;
5013 end if;
5015 -- The queue length is not one
5017 if not Restriction_Active (No_Entry_Queue)
5018 and then Get_Max_Queue_Length (Item) /= Uint_1
5019 then
5020 return False;
5021 end if;
5023 Is_First := False;
5024 end if;
5026 Next_Entity (Item);
5027 end loop;
5029 return True;
5030 end Has_One_Entry_And_No_Queue;
5032 -- Local variables
5034 Pkg_Id : RTU_Id := RTU_Null;
5036 -- Start of processing for Corresponding_Runtime_Package
5038 begin
5039 pragma Assert (Is_Concurrent_Type (Typ));
5041 if Is_Protected_Type (Typ) then
5042 if Has_Entries (Typ)
5044 -- A protected type without entries that covers an interface and
5045 -- overrides the abstract routines with protected procedures is
5046 -- considered equivalent to a protected type with entries in the
5047 -- context of dispatching select statements. It is sufficient to
5048 -- check for the presence of an interface list in the declaration
5049 -- node to recognize this case.
5051 or else Present (Interface_List (Parent (Typ)))
5053 -- Protected types with interrupt handlers (when not using a
5054 -- restricted profile) are also considered equivalent to
5055 -- protected types with entries. The types which are used
5056 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
5057 -- are derived from Protection_Entries.
5059 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
5060 or else Has_Interrupt_Handler (Typ)
5061 then
5062 if Abort_Allowed
5063 or else Restriction_Active (No_Select_Statements) = False
5064 or else not Has_One_Entry_And_No_Queue (Typ)
5065 or else (Has_Attach_Handler (Typ)
5066 and then not Restricted_Profile)
5067 then
5068 Pkg_Id := System_Tasking_Protected_Objects_Entries;
5069 else
5070 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
5071 end if;
5073 else
5074 Pkg_Id := System_Tasking_Protected_Objects;
5075 end if;
5076 end if;
5078 return Pkg_Id;
5079 end Corresponding_Runtime_Package;
5081 -----------------------------------
5082 -- Current_Sem_Unit_Declarations --
5083 -----------------------------------
5085 function Current_Sem_Unit_Declarations return List_Id is
5086 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
5087 Decls : List_Id;
5089 begin
5090 -- If the current unit is a package body, locate the visible
5091 -- declarations of the package spec.
5093 if Nkind (U) = N_Package_Body then
5094 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
5095 end if;
5097 if Nkind (U) = N_Package_Declaration then
5098 U := Specification (U);
5099 Decls := Visible_Declarations (U);
5101 if No (Decls) then
5102 Decls := New_List;
5103 Set_Visible_Declarations (U, Decls);
5104 end if;
5106 else
5107 Decls := Declarations (U);
5109 if No (Decls) then
5110 Decls := New_List;
5111 Set_Declarations (U, Decls);
5112 end if;
5113 end if;
5115 return Decls;
5116 end Current_Sem_Unit_Declarations;
5118 -----------------------
5119 -- Duplicate_Subexpr --
5120 -----------------------
5122 function Duplicate_Subexpr
5123 (Exp : Node_Id;
5124 Name_Req : Boolean := False;
5125 Renaming_Req : Boolean := False) return Node_Id
5127 begin
5128 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5129 return New_Copy_Tree (Exp);
5130 end Duplicate_Subexpr;
5132 ---------------------------------
5133 -- Duplicate_Subexpr_No_Checks --
5134 ---------------------------------
5136 function Duplicate_Subexpr_No_Checks
5137 (Exp : Node_Id;
5138 Name_Req : Boolean := False;
5139 Renaming_Req : Boolean := False;
5140 Related_Id : Entity_Id := Empty;
5141 Is_Low_Bound : Boolean := False;
5142 Is_High_Bound : Boolean := False) return Node_Id
5144 New_Exp : Node_Id;
5146 begin
5147 Remove_Side_Effects
5148 (Exp => Exp,
5149 Name_Req => Name_Req,
5150 Renaming_Req => Renaming_Req,
5151 Related_Id => Related_Id,
5152 Is_Low_Bound => Is_Low_Bound,
5153 Is_High_Bound => Is_High_Bound);
5155 New_Exp := New_Copy_Tree (Exp);
5156 Remove_Checks (New_Exp);
5157 return New_Exp;
5158 end Duplicate_Subexpr_No_Checks;
5160 -----------------------------------
5161 -- Duplicate_Subexpr_Move_Checks --
5162 -----------------------------------
5164 function Duplicate_Subexpr_Move_Checks
5165 (Exp : Node_Id;
5166 Name_Req : Boolean := False;
5167 Renaming_Req : Boolean := False) return Node_Id
5169 New_Exp : Node_Id;
5171 begin
5172 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5173 New_Exp := New_Copy_Tree (Exp);
5174 Remove_Checks (Exp);
5175 return New_Exp;
5176 end Duplicate_Subexpr_Move_Checks;
5178 -------------------------
5179 -- Enclosing_Init_Proc --
5180 -------------------------
5182 function Enclosing_Init_Proc return Entity_Id is
5183 S : Entity_Id;
5185 begin
5186 S := Current_Scope;
5187 while Present (S) and then S /= Standard_Standard loop
5188 if Is_Init_Proc (S) then
5189 return S;
5190 else
5191 S := Scope (S);
5192 end if;
5193 end loop;
5195 return Empty;
5196 end Enclosing_Init_Proc;
5198 --------------------
5199 -- Ensure_Defined --
5200 --------------------
5202 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
5203 IR : Node_Id;
5205 begin
5206 -- An itype reference must only be created if this is a local itype, so
5207 -- that gigi can elaborate it on the proper objstack.
5209 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
5210 IR := Make_Itype_Reference (Sloc (N));
5211 Set_Itype (IR, Typ);
5212 Insert_Action (N, IR);
5213 end if;
5214 end Ensure_Defined;
5216 -------------------
5217 -- Evaluate_Name --
5218 -------------------
5220 procedure Evaluate_Name (Nam : Node_Id) is
5221 begin
5222 case Nkind (Nam) is
5223 -- For an aggregate, force its evaluation
5225 when N_Aggregate =>
5226 Force_Evaluation (Nam);
5228 -- For an attribute reference or an indexed component, evaluate the
5229 -- prefix, which is itself a name, recursively, and then force the
5230 -- evaluation of all the subscripts (or attribute expressions).
5232 when N_Attribute_Reference
5233 | N_Indexed_Component
5235 Evaluate_Name (Prefix (Nam));
5237 declare
5238 E : Node_Id;
5240 begin
5241 E := First (Expressions (Nam));
5242 while Present (E) loop
5243 Force_Evaluation (E);
5245 if Is_Rewrite_Substitution (E) then
5246 Set_Do_Range_Check
5247 (E, Do_Range_Check (Original_Node (E)));
5248 end if;
5250 Next (E);
5251 end loop;
5252 end;
5254 -- For an explicit dereference, we simply force the evaluation of
5255 -- the name expression. The dereference provides a value that is the
5256 -- address for the renamed object, and it is precisely this value
5257 -- that we want to preserve.
5259 when N_Explicit_Dereference =>
5260 Force_Evaluation (Prefix (Nam));
5262 -- For a function call, we evaluate the call; same for an operator
5264 when N_Function_Call
5265 | N_Op
5267 Force_Evaluation (Nam);
5269 -- For a qualified expression, we evaluate the expression
5271 when N_Qualified_Expression =>
5272 Evaluate_Name (Expression (Nam));
5274 -- For a selected component, we simply evaluate the prefix
5276 when N_Selected_Component =>
5277 Evaluate_Name (Prefix (Nam));
5279 -- For a slice, we evaluate the prefix, as for the indexed component
5280 -- case and then, if there is a range present, either directly or as
5281 -- the constraint of a discrete subtype indication, we evaluate the
5282 -- two bounds of this range.
5284 when N_Slice =>
5285 Evaluate_Name (Prefix (Nam));
5286 Evaluate_Slice_Bounds (Nam);
5288 -- For a type conversion, the expression of the conversion must be
5289 -- the name of an object, and we simply need to evaluate this name.
5291 when N_Type_Conversion =>
5292 Evaluate_Name (Expression (Nam));
5294 -- The remaining cases are direct name and character literal. In all
5295 -- these cases, we do nothing, since we want to reevaluate each time
5296 -- the renamed object is used. ??? There are more remaining cases, at
5297 -- least in the GNATprove_Mode, where this routine is called in more
5298 -- contexts than in GNAT.
5300 when others =>
5301 null;
5302 end case;
5303 end Evaluate_Name;
5305 ---------------------------
5306 -- Evaluate_Slice_Bounds --
5307 ---------------------------
5309 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
5310 DR : constant Node_Id := Discrete_Range (Slice);
5311 Constr : Node_Id;
5312 Rexpr : Node_Id;
5314 begin
5315 if Nkind (DR) = N_Range then
5316 Force_Evaluation (Low_Bound (DR));
5317 Force_Evaluation (High_Bound (DR));
5319 elsif Nkind (DR) = N_Subtype_Indication then
5320 Constr := Constraint (DR);
5322 if Nkind (Constr) = N_Range_Constraint then
5323 Rexpr := Range_Expression (Constr);
5325 Force_Evaluation (Low_Bound (Rexpr));
5326 Force_Evaluation (High_Bound (Rexpr));
5327 end if;
5328 end if;
5329 end Evaluate_Slice_Bounds;
5331 ---------------------
5332 -- Evolve_And_Then --
5333 ---------------------
5335 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
5336 begin
5337 if No (Cond) then
5338 Cond := Cond1;
5339 else
5340 Cond :=
5341 Make_And_Then (Sloc (Cond1),
5342 Left_Opnd => Cond,
5343 Right_Opnd => Cond1);
5344 end if;
5345 end Evolve_And_Then;
5347 --------------------
5348 -- Evolve_Or_Else --
5349 --------------------
5351 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
5352 begin
5353 if No (Cond) then
5354 Cond := Cond1;
5355 else
5356 Cond :=
5357 Make_Or_Else (Sloc (Cond1),
5358 Left_Opnd => Cond,
5359 Right_Opnd => Cond1);
5360 end if;
5361 end Evolve_Or_Else;
5363 -------------------------------
5364 -- Expand_Sliding_Conversion --
5365 -------------------------------
5367 procedure Expand_Sliding_Conversion (N : Node_Id; Arr_Typ : Entity_Id) is
5369 pragma Assert (Is_Array_Type (Arr_Typ)
5370 and then not Is_Constrained (Arr_Typ)
5371 and then Is_Fixed_Lower_Bound_Array_Subtype (Arr_Typ));
5373 Constraints : List_Id;
5374 Index : Node_Id := First_Index (Arr_Typ);
5375 Loc : constant Source_Ptr := Sloc (N);
5376 Subt_Decl : Node_Id;
5377 Subt : Entity_Id;
5378 Subt_Low : Node_Id;
5379 Subt_High : Node_Id;
5381 Act_Subt : Entity_Id;
5382 Act_Index : Node_Id;
5383 Act_Low : Node_Id;
5384 Act_High : Node_Id;
5385 Adjust_Incr : Node_Id;
5386 Dimension : Int := 0;
5387 All_FLBs_Match : Boolean := True;
5389 begin
5390 -- This procedure is called during semantic analysis, and we only expand
5391 -- a sliding conversion when Expander_Active, to avoid doing it during
5392 -- preanalysis (which can lead to problems with the target subtype not
5393 -- getting properly expanded during later full analysis). Also, sliding
5394 -- should never be needed for string literals, because their bounds are
5395 -- determined directly based on the fixed lower bound of Arr_Typ and
5396 -- their length.
5398 if Expander_Active and then Nkind (N) /= N_String_Literal then
5399 Constraints := New_List;
5401 Act_Subt := Get_Actual_Subtype (N);
5402 Act_Index := First_Index (Act_Subt);
5404 -- Loop over the indexes of the fixed-lower-bound array type or
5405 -- subtype to build up an index constraint for constructing the
5406 -- subtype that will be the target of a conversion of the array
5407 -- object that may need a sliding conversion.
5409 while Present (Index) loop
5410 pragma Assert (Present (Act_Index));
5412 Dimension := Dimension + 1;
5414 Get_Index_Bounds (Act_Index, Act_Low, Act_High);
5416 -- If Index defines a normal unconstrained range (range <>),
5417 -- then we will simply use the bounds of the actual subtype's
5418 -- corresponding index range.
5420 if not Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index)) then
5421 Subt_Low := Act_Low;
5422 Subt_High := Act_High;
5424 -- Otherwise, a range will be created with a low bound given by
5425 -- the fixed lower bound of the array subtype's index, and with
5426 -- high bound given by (Actual'Length + fixed lower bound - 1).
5428 else
5429 if Nkind (Index) = N_Subtype_Indication then
5430 Subt_Low :=
5431 New_Copy_Tree
5432 (Low_Bound (Range_Expression (Constraint (Index))));
5433 else
5434 pragma Assert (Nkind (Index) = N_Range);
5436 Subt_Low := New_Copy_Tree (Low_Bound (Index));
5437 end if;
5439 -- If either we have a nonstatic lower bound, or the target and
5440 -- source subtypes are statically known to have unequal lower
5441 -- bounds, then we will need to make a subtype conversion to
5442 -- slide the bounds. However, if all of the indexes' lower
5443 -- bounds are static and known to be equal (the common case),
5444 -- then no conversion will be needed, and we'll end up not
5445 -- creating the subtype or the conversion (though we still
5446 -- build up the index constraint, which will simply be unused).
5448 if not (Compile_Time_Known_Value (Subt_Low)
5449 and then Compile_Time_Known_Value (Act_Low))
5450 or else Expr_Value (Subt_Low) /= Expr_Value (Act_Low)
5451 then
5452 All_FLBs_Match := False;
5453 end if;
5455 -- Apply 'Pos to lower bound, which may be of an enumeration
5456 -- type, before subtracting.
5458 Adjust_Incr :=
5459 Make_Op_Subtract (Loc,
5460 Make_Attribute_Reference (Loc,
5461 Prefix =>
5462 New_Occurrence_Of (Etype (Act_Index), Loc),
5463 Attribute_Name =>
5464 Name_Pos,
5465 Expressions =>
5466 New_List (New_Copy_Tree (Subt_Low))),
5467 Make_Integer_Literal (Loc, 1));
5469 -- Apply 'Val to the result of adding the increment to the
5470 -- length, to handle indexes of enumeration types.
5472 Subt_High :=
5473 Make_Attribute_Reference (Loc,
5474 Prefix =>
5475 New_Occurrence_Of (Etype (Act_Index), Loc),
5476 Attribute_Name =>
5477 Name_Val,
5478 Expressions =>
5479 New_List (Make_Op_Add (Loc,
5480 Make_Attribute_Reference (Loc,
5481 Prefix =>
5482 New_Occurrence_Of (Act_Subt, Loc),
5483 Attribute_Name =>
5484 Name_Length,
5485 Expressions =>
5486 New_List
5487 (Make_Integer_Literal
5488 (Loc, Dimension))),
5489 Adjust_Incr)));
5490 end if;
5492 Append (Make_Range (Loc, Subt_Low, Subt_High), Constraints);
5494 Next (Index);
5495 Next (Act_Index);
5496 end loop;
5498 -- If for each index with a fixed lower bound (FLB), the lower bound
5499 -- of the corresponding index of the actual subtype is statically
5500 -- known be equal to the FLB, then a sliding conversion isn't needed
5501 -- at all, so just return without building a subtype or conversion.
5503 if All_FLBs_Match then
5504 return;
5505 end if;
5507 -- A sliding conversion is needed, so create the target subtype using
5508 -- the index constraint created above, and rewrite the expression
5509 -- as a conversion to that subtype.
5511 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
5512 Set_Is_Internal (Subt);
5514 Subt_Decl :=
5515 Make_Subtype_Declaration (Loc,
5516 Defining_Identifier => Subt,
5517 Subtype_Indication =>
5518 Make_Subtype_Indication (Loc,
5519 Subtype_Mark =>
5520 New_Occurrence_Of (Arr_Typ, Loc),
5521 Constraint =>
5522 Make_Index_Or_Discriminant_Constraint (Loc,
5523 Constraints => Constraints)));
5525 Mark_Rewrite_Insertion (Subt_Decl);
5527 -- The actual subtype is an Itype, so we analyze the declaration,
5528 -- but do not attach it to the tree.
5530 Set_Parent (Subt_Decl, N);
5531 Set_Is_Itype (Subt);
5532 Analyze (Subt_Decl, Suppress => All_Checks);
5533 Set_Associated_Node_For_Itype (Subt, N);
5534 Set_Has_Delayed_Freeze (Subt, False);
5536 -- We need to freeze the actual subtype immediately. This is needed
5537 -- because otherwise this Itype will not get frozen at all, and it is
5538 -- always safe to freeze on creation because any associated types
5539 -- must be frozen at this point.
5541 Freeze_Itype (Subt, N);
5543 Rewrite (N,
5544 Make_Type_Conversion (Loc,
5545 Subtype_Mark =>
5546 New_Occurrence_Of (Subt, Loc),
5547 Expression => Relocate_Node (N)));
5548 Analyze (N);
5549 end if;
5550 end Expand_Sliding_Conversion;
5552 -----------------------------------------
5553 -- Expand_Static_Predicates_In_Choices --
5554 -----------------------------------------
5556 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
5557 pragma Assert (Nkind (N) in N_Case_Statement_Alternative | N_Variant);
5559 Choices : List_Id := Discrete_Choices (N);
5561 Choice : Node_Id;
5562 Next_C : Node_Id;
5563 P : Node_Id;
5564 C : Node_Id;
5566 begin
5567 -- If this is an "others" alternative, we need to process any static
5568 -- predicates in its Others_Discrete_Choices.
5570 if Nkind (First (Choices)) = N_Others_Choice then
5571 Choices := Others_Discrete_Choices (First (Choices));
5572 end if;
5574 Choice := First (Choices);
5575 while Present (Choice) loop
5576 Next_C := Next (Choice);
5578 -- Check for name of subtype with static predicate
5580 if Is_Entity_Name (Choice)
5581 and then Is_Type (Entity (Choice))
5582 and then Has_Predicates (Entity (Choice))
5583 then
5584 -- Loop through entries in predicate list, converting to choices
5585 -- and inserting in the list before the current choice. Note that
5586 -- if the list is empty, corresponding to a False predicate, then
5587 -- no choices are inserted.
5589 P := First (Static_Discrete_Predicate (Entity (Choice)));
5590 while Present (P) loop
5592 -- If low bound and high bounds are equal, copy simple choice
5594 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
5595 C := New_Copy (Low_Bound (P));
5597 -- Otherwise copy a range
5599 else
5600 C := New_Copy (P);
5601 end if;
5603 -- Change Sloc to referencing choice (rather than the Sloc of
5604 -- the predicate declaration element itself).
5606 Set_Sloc (C, Sloc (Choice));
5607 Insert_Before (Choice, C);
5608 Next (P);
5609 end loop;
5611 -- Delete the predicated entry
5613 Remove (Choice);
5614 end if;
5616 -- Move to next choice to check
5618 Choice := Next_C;
5619 end loop;
5621 Set_Has_SP_Choice (N, False);
5622 end Expand_Static_Predicates_In_Choices;
5624 ------------------------------
5625 -- Expand_Subtype_From_Expr --
5626 ------------------------------
5628 -- This function is applicable for both static and dynamic allocation of
5629 -- objects which are constrained by an initial expression. Basically it
5630 -- transforms an unconstrained subtype indication into a constrained one.
5632 -- The expression may also be transformed in certain cases in order to
5633 -- avoid multiple evaluation. In the static allocation case, the general
5634 -- scheme is:
5636 -- Val : T := Expr;
5638 -- is transformed into
5640 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5642 -- Here are the main cases :
5644 -- <if Expr is a Slice>
5645 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5647 -- <elsif Expr is a String Literal>
5648 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5650 -- <elsif Expr is Constrained>
5651 -- subtype T is Type_Of_Expr
5652 -- Val : T := Expr;
5654 -- <elsif Expr is an entity_name>
5655 -- Val : T (constraints taken from Expr) := Expr;
5657 -- <else>
5658 -- type Axxx is access all T;
5659 -- Rval : Axxx := Expr'ref;
5660 -- Val : T (constraints taken from Rval) := Rval.all;
5662 -- ??? note: when the Expression is allocated in the secondary stack
5663 -- we could use it directly instead of copying it by declaring
5664 -- Val : T (...) renames Rval.all
5666 procedure Expand_Subtype_From_Expr
5667 (N : Node_Id;
5668 Unc_Type : Entity_Id;
5669 Subtype_Indic : Node_Id;
5670 Exp : Node_Id;
5671 Related_Id : Entity_Id := Empty)
5673 Loc : constant Source_Ptr := Sloc (N);
5674 Exp_Typ : constant Entity_Id := Etype (Exp);
5675 T : Entity_Id;
5677 begin
5678 -- In general we cannot build the subtype if expansion is disabled,
5679 -- because internal entities may not have been defined. However, to
5680 -- avoid some cascaded errors, we try to continue when the expression is
5681 -- an array (or string), because it is safe to compute the bounds. It is
5682 -- in fact required to do so even in a generic context, because there
5683 -- may be constants that depend on the bounds of a string literal, both
5684 -- standard string types and more generally arrays of characters.
5686 -- In GNATprove mode, these extra subtypes are not needed, unless Exp is
5687 -- a static expression. In that case, the subtype will be constrained
5688 -- while the original type might be unconstrained, so expanding the type
5689 -- is necessary both for passing legality checks in GNAT and for precise
5690 -- analysis in GNATprove.
5692 if GNATprove_Mode and then not Is_Static_Expression (Exp) then
5693 return;
5694 end if;
5696 if not Expander_Active
5697 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5698 then
5699 return;
5700 end if;
5702 if Nkind (Exp) = N_Slice then
5703 declare
5704 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5706 begin
5707 Rewrite (Subtype_Indic,
5708 Make_Subtype_Indication (Loc,
5709 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5710 Constraint =>
5711 Make_Index_Or_Discriminant_Constraint (Loc,
5712 Constraints => New_List
5713 (New_Occurrence_Of (Slice_Type, Loc)))));
5715 -- This subtype indication may be used later for constraint checks
5716 -- we better make sure that if a variable was used as a bound of
5717 -- the original slice, its value is frozen.
5719 Evaluate_Slice_Bounds (Exp);
5720 end;
5722 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5723 Rewrite (Subtype_Indic,
5724 Make_Subtype_Indication (Loc,
5725 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5726 Constraint =>
5727 Make_Index_Or_Discriminant_Constraint (Loc,
5728 Constraints => New_List (
5729 Make_Literal_Range (Loc,
5730 Literal_Typ => Exp_Typ)))));
5732 -- If the type of the expression is an internally generated type it
5733 -- may not be necessary to create a new subtype. However there are two
5734 -- exceptions: references to the current instances, and aliased array
5735 -- object declarations for which the back end has to create a template.
5737 elsif Is_Constrained (Exp_Typ)
5738 and then not Is_Class_Wide_Type (Unc_Type)
5739 and then
5740 (Nkind (N) /= N_Object_Declaration
5741 or else not Is_Entity_Name (Expression (N))
5742 or else not Comes_From_Source (Entity (Expression (N)))
5743 or else not Is_Array_Type (Exp_Typ)
5744 or else not Aliased_Present (N))
5745 then
5746 if Is_Itype (Exp_Typ)
5748 -- When this is for an object declaration, the caller may want to
5749 -- set Is_Constr_Subt_For_U_Nominal on the subtype, so we must make
5750 -- sure that either the subtype has been built for the expression,
5751 -- typically for an aggregate, or the flag is already set on it;
5752 -- otherwise it could end up being set on the nominal constrained
5753 -- subtype of an object and thus later cause the failure to detect
5754 -- non-statically-matching subtypes on 'Access of this object.
5756 and then (Nkind (N) /= N_Object_Declaration
5757 or else Nkind (Original_Node (Exp)) = N_Aggregate
5758 or else Is_Constr_Subt_For_U_Nominal (Exp_Typ))
5759 then
5760 -- Within an initialization procedure, a selected component
5761 -- denotes a component of the enclosing record, and it appears as
5762 -- an actual in a call to its own initialization procedure. If
5763 -- this component depends on the outer discriminant, we must
5764 -- generate the proper actual subtype for it.
5766 if Nkind (Exp) = N_Selected_Component
5767 and then Within_Init_Proc
5768 then
5769 declare
5770 Decl : constant Node_Id :=
5771 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5772 begin
5773 if Present (Decl) then
5774 Insert_Action (N, Decl);
5775 T := Defining_Identifier (Decl);
5776 else
5777 T := Exp_Typ;
5778 end if;
5779 end;
5781 -- No need to generate a new subtype
5783 else
5784 T := Exp_Typ;
5785 end if;
5787 else
5788 T := Make_Temporary (Loc, 'T');
5790 Insert_Action (N,
5791 Make_Subtype_Declaration (Loc,
5792 Defining_Identifier => T,
5793 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5795 -- This type is marked as an itype even though it has an explicit
5796 -- declaration since otherwise Is_Generic_Actual_Type can get
5797 -- set, resulting in the generation of spurious errors. (See
5798 -- sem_ch8.Analyze_Package_Renaming and Sem_Type.Covers.)
5800 Set_Is_Itype (T);
5801 Set_Associated_Node_For_Itype (T, Exp);
5802 end if;
5804 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5806 -- Nothing needs to be done for private types with unknown discriminants
5807 -- if the underlying type is not an unconstrained composite type or it
5808 -- is an unchecked union.
5810 elsif Is_Private_Type (Unc_Type)
5811 and then Has_Unknown_Discriminants (Unc_Type)
5812 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5813 or else Is_Constrained (Underlying_Type (Unc_Type))
5814 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5815 then
5816 null;
5818 -- Case of derived type with unknown discriminants where the parent type
5819 -- also has unknown discriminants.
5821 elsif Is_Record_Type (Unc_Type)
5822 and then not Is_Class_Wide_Type (Unc_Type)
5823 and then Has_Unknown_Discriminants (Unc_Type)
5824 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5825 then
5826 -- Nothing to be done if no underlying record view available
5828 -- If this is a limited type derived from a type with unknown
5829 -- discriminants, do not expand either, so that subsequent expansion
5830 -- of the call can add build-in-place parameters to call.
5832 if No (Underlying_Record_View (Unc_Type))
5833 or else Is_Limited_Type (Unc_Type)
5834 then
5835 null;
5837 -- Otherwise use the Underlying_Record_View to create the proper
5838 -- constrained subtype for an object of a derived type with unknown
5839 -- discriminants.
5841 else
5842 Rewrite (Subtype_Indic,
5843 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5844 end if;
5846 -- Renamings of class-wide interface types require no equivalent
5847 -- constrained type declarations because we only need to reference
5848 -- the tag component associated with the interface. The same is
5849 -- presumably true for class-wide types in general, so this test
5850 -- is broadened to include all class-wide renamings, which also
5851 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5852 -- (Is this really correct, or are there some cases of class-wide
5853 -- renamings that require action in this procedure???)
5855 elsif Present (N)
5856 and then Nkind (N) = N_Object_Renaming_Declaration
5857 and then Is_Class_Wide_Type (Unc_Type)
5858 then
5859 null;
5861 -- In Ada 95 nothing to be done if the type of the expression is limited
5862 -- because in this case the expression cannot be copied, and its use can
5863 -- only be by reference.
5865 -- In Ada 2005 the context can be an object declaration whose expression
5866 -- is a function that returns in place. If the nominal subtype has
5867 -- unknown discriminants, the call still provides constraints on the
5868 -- object, and we have to create an actual subtype from it.
5870 -- If the type is class-wide, the expression is dynamically tagged and
5871 -- we do not create an actual subtype either. Ditto for an interface.
5872 -- For now this applies only if the type is immutably limited, and the
5873 -- function being called is build-in-place. This will have to be revised
5874 -- when build-in-place functions are generalized to other types.
5876 elsif Is_Inherently_Limited_Type (Exp_Typ)
5877 and then
5878 (Is_Class_Wide_Type (Exp_Typ)
5879 or else Is_Interface (Exp_Typ)
5880 or else not Has_Unknown_Discriminants (Exp_Typ)
5881 or else not Is_Composite_Type (Unc_Type))
5882 then
5883 null;
5885 -- For limited objects initialized with build-in-place function calls,
5886 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5887 -- node in the expression initializing the object, which breaks the
5888 -- circuitry that detects and adds the additional arguments to the
5889 -- called function.
5891 elsif Is_Build_In_Place_Function_Call (Exp) then
5892 null;
5894 -- If the expression is an uninitialized aggregate, no need to build
5895 -- a subtype from the expression, because this may require the use of
5896 -- dynamic memory to create the object.
5898 elsif Is_Uninitialized_Aggregate (Exp, Exp_Typ) then
5899 Rewrite (Subtype_Indic, New_Occurrence_Of (Etype (Exp), Sloc (N)));
5900 if Nkind (N) = N_Object_Declaration then
5901 Set_Expression (N, Empty);
5902 Set_No_Initialization (N);
5903 end if;
5905 else
5906 Rewrite (Subtype_Indic,
5907 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5908 end if;
5909 end Expand_Subtype_From_Expr;
5911 ---------------------------------------------
5912 -- Expression_Contains_Primitives_Calls_Of --
5913 ---------------------------------------------
5915 function Expression_Contains_Primitives_Calls_Of
5916 (Expr : Node_Id;
5917 Typ : Entity_Id) return Boolean
5919 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5921 Calls_OK : Boolean := False;
5922 -- This flag is set to True when expression Expr contains at least one
5923 -- call to a nondispatching primitive function of Typ.
5925 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5926 -- Search for nondispatching calls to primitive functions of type Typ
5928 ----------------------------
5929 -- Search_Primitive_Calls --
5930 ----------------------------
5932 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5933 Disp_Typ : Entity_Id;
5934 Subp : Entity_Id;
5936 begin
5937 -- Detect a function call that could denote a nondispatching
5938 -- primitive of the input type.
5940 if Nkind (N) = N_Function_Call
5941 and then Is_Entity_Name (Name (N))
5942 then
5943 Subp := Entity (Name (N));
5945 -- Do not consider function calls with a controlling argument, as
5946 -- those are always dispatching calls.
5948 if Is_Dispatching_Operation (Subp)
5949 and then No (Controlling_Argument (N))
5950 then
5951 Disp_Typ := Find_Dispatching_Type (Subp);
5953 -- To qualify as a suitable primitive, the dispatching type of
5954 -- the function must be the input type.
5956 if Present (Disp_Typ)
5957 and then Unique_Entity (Disp_Typ) = U_Typ
5958 then
5959 Calls_OK := True;
5961 -- There is no need to continue the traversal, as one such
5962 -- call suffices.
5964 return Abandon;
5965 end if;
5966 end if;
5967 end if;
5969 return OK;
5970 end Search_Primitive_Calls;
5972 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5974 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5976 begin
5977 Search_Calls (Expr);
5978 return Calls_OK;
5979 end Expression_Contains_Primitives_Calls_Of;
5981 ----------------------
5982 -- Finalize_Address --
5983 ----------------------
5985 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5986 Btyp : constant Entity_Id := Base_Type (Typ);
5987 Utyp : Entity_Id := Typ;
5989 begin
5990 -- Handle protected class-wide or task class-wide types
5992 if Is_Class_Wide_Type (Utyp) then
5993 if Is_Concurrent_Type (Root_Type (Utyp)) then
5994 Utyp := Root_Type (Utyp);
5996 elsif Is_Private_Type (Root_Type (Utyp))
5997 and then Present (Full_View (Root_Type (Utyp)))
5998 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5999 then
6000 Utyp := Full_View (Root_Type (Utyp));
6001 end if;
6002 end if;
6004 -- Handle private types
6006 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
6007 Utyp := Full_View (Utyp);
6008 end if;
6010 -- Handle protected and task types
6012 if Is_Concurrent_Type (Utyp)
6013 and then Present (Corresponding_Record_Type (Utyp))
6014 then
6015 Utyp := Corresponding_Record_Type (Utyp);
6016 end if;
6018 Utyp := Underlying_Type (Base_Type (Utyp));
6020 -- Deal with untagged derivation of private views. If the parent is
6021 -- now known to be protected, the finalization routine is the one
6022 -- defined on the corresponding record of the ancestor (corresponding
6023 -- records do not automatically inherit operations, but maybe they
6024 -- should???)
6026 if Is_Untagged_Derivation (Btyp) then
6027 if Is_Protected_Type (Btyp) then
6028 Utyp := Corresponding_Record_Type (Root_Type (Btyp));
6030 else
6031 Utyp := Underlying_Type (Root_Type (Btyp));
6033 if Is_Protected_Type (Utyp) then
6034 Utyp := Corresponding_Record_Type (Utyp);
6035 end if;
6036 end if;
6037 end if;
6039 -- If the underlying_type is a subtype, we are dealing with the
6040 -- completion of a private type. We need to access the base type and
6041 -- generate a conversion to it.
6043 if Utyp /= Base_Type (Utyp) then
6044 pragma Assert (Is_Private_Type (Typ));
6046 Utyp := Base_Type (Utyp);
6047 end if;
6049 -- When dealing with an internally built full view for a type with
6050 -- unknown discriminants, use the original record type.
6052 if Is_Underlying_Record_View (Utyp) then
6053 Utyp := Etype (Utyp);
6054 end if;
6056 return TSS (Utyp, TSS_Finalize_Address);
6057 end Finalize_Address;
6059 -----------------------------
6060 -- Find_Controlled_Prim_Op --
6061 -----------------------------
6063 function Find_Controlled_Prim_Op
6064 (T : Entity_Id; Name : Name_Id) return Entity_Id
6066 Op_Name : constant Name_Id := Name_Of_Controlled_Prim_Op (T, Name);
6068 begin
6069 if Op_Name = No_Name then
6070 return Empty;
6071 end if;
6073 return Find_Optional_Prim_Op (T, Op_Name);
6074 end Find_Controlled_Prim_Op;
6076 ------------------------
6077 -- Find_Interface_ADT --
6078 ------------------------
6080 function Find_Interface_ADT
6081 (T : Entity_Id;
6082 Iface : Entity_Id) return Elmt_Id
6084 ADT : Elmt_Id;
6085 Typ : Entity_Id := T;
6087 begin
6088 pragma Assert (Is_Interface (Iface));
6090 -- Handle private types
6092 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6093 Typ := Full_View (Typ);
6094 end if;
6096 -- Handle access types
6098 if Is_Access_Type (Typ) then
6099 Typ := Designated_Type (Typ);
6100 end if;
6102 -- Handle task and protected types implementing interfaces
6104 if Is_Concurrent_Type (Typ) then
6105 Typ := Corresponding_Record_Type (Typ);
6106 end if;
6108 pragma Assert
6109 (not Is_Class_Wide_Type (Typ)
6110 and then Ekind (Typ) /= E_Incomplete_Type);
6112 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6113 return First_Elmt (Access_Disp_Table (Typ));
6115 else
6116 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
6117 while Present (ADT)
6118 and then Present (Related_Type (Node (ADT)))
6119 and then Related_Type (Node (ADT)) /= Iface
6120 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
6121 Use_Full_View => True)
6122 loop
6123 Next_Elmt (ADT);
6124 end loop;
6126 pragma Assert (Present (Related_Type (Node (ADT))));
6127 return ADT;
6128 end if;
6129 end Find_Interface_ADT;
6131 ------------------------
6132 -- Find_Interface_Tag --
6133 ------------------------
6135 function Find_Interface_Tag
6136 (T : Entity_Id;
6137 Iface : Entity_Id) return Entity_Id
6139 AI_Tag : Entity_Id := Empty;
6140 Found : Boolean := False;
6141 Typ : Entity_Id := T;
6143 procedure Find_Tag (Typ : Entity_Id);
6144 -- Internal subprogram used to recursively climb to the ancestors
6146 --------------
6147 -- Find_Tag --
6148 --------------
6150 procedure Find_Tag (Typ : Entity_Id) is
6151 AI_Elmt : Elmt_Id;
6152 AI : Node_Id;
6154 begin
6155 -- This routine does not handle the case in which the interface is an
6156 -- ancestor of Typ. That case is handled by the enclosing subprogram.
6158 pragma Assert (Typ /= Iface);
6160 -- Climb to the root type handling private types
6162 if Present (Full_View (Etype (Typ))) then
6163 if Full_View (Etype (Typ)) /= Typ then
6164 Find_Tag (Full_View (Etype (Typ)));
6165 end if;
6167 elsif Etype (Typ) /= Typ then
6168 Find_Tag (Etype (Typ));
6169 end if;
6171 -- Traverse the list of interfaces implemented by the type
6173 if not Found
6174 and then Present (Interfaces (Typ))
6175 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
6176 then
6177 -- Skip the tag associated with the primary table
6179 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
6180 pragma Assert (Present (AI_Tag));
6182 AI_Elmt := First_Elmt (Interfaces (Typ));
6183 while Present (AI_Elmt) loop
6184 AI := Node (AI_Elmt);
6186 if AI = Iface
6187 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
6188 then
6189 Found := True;
6190 return;
6191 end if;
6193 AI_Tag := Next_Tag_Component (AI_Tag);
6194 Next_Elmt (AI_Elmt);
6195 end loop;
6196 end if;
6197 end Find_Tag;
6199 -- Start of processing for Find_Interface_Tag
6201 begin
6202 pragma Assert (Is_Interface (Iface));
6204 -- Handle access types
6206 if Is_Access_Type (Typ) then
6207 Typ := Designated_Type (Typ);
6208 end if;
6210 -- Handle class-wide types
6212 if Is_Class_Wide_Type (Typ) then
6213 Typ := Root_Type (Typ);
6214 end if;
6216 -- Handle private types
6218 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6219 Typ := Full_View (Typ);
6220 end if;
6222 -- Handle entities from the limited view
6224 if Ekind (Typ) = E_Incomplete_Type then
6225 pragma Assert (Present (Non_Limited_View (Typ)));
6226 Typ := Non_Limited_View (Typ);
6227 end if;
6229 -- Handle task and protected types implementing interfaces
6231 if Is_Concurrent_Type (Typ) then
6232 Typ := Corresponding_Record_Type (Typ);
6233 end if;
6235 -- If the interface is an ancestor of the type, then it shared the
6236 -- primary dispatch table.
6238 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6239 return First_Tag_Component (Typ);
6241 -- Otherwise we need to search for its associated tag component
6243 else
6244 Find_Tag (Typ);
6245 return AI_Tag;
6246 end if;
6247 end Find_Interface_Tag;
6249 --------------------
6250 -- Find_Last_Init --
6251 --------------------
6253 function Find_Last_Init (Decl : Node_Id) return Node_Id is
6254 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
6256 Init_Typ : Entity_Id;
6257 -- The initialization type of the related object declaration. Note
6258 -- that this is not necessarily the same type as Obj_Typ because of
6259 -- possible type derivations.
6261 Obj_Typ : Entity_Id;
6262 -- The (designated) type of the object declaration
6264 function Find_Last_Init_In_Block (Blk : Node_Id) return Node_Id;
6265 -- Find the last initialization call within the statements of block Blk
6267 function Is_Init_Call (N : Node_Id) return Boolean;
6268 -- Determine whether node N denotes one of the initialization procedures
6269 -- of types Init_Typ or Typ.
6271 function Next_Suitable_Statement (Stmt : Node_Id) return Node_Id;
6272 -- Obtain the next statement which follows list member Stmt while
6273 -- ignoring artifacts related to access-before-elaboration checks.
6275 -----------------------------
6276 -- Find_Last_Init_In_Block --
6277 -----------------------------
6279 function Find_Last_Init_In_Block (Blk : Node_Id) return Node_Id is
6280 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
6282 Stmt : Node_Id;
6284 begin
6285 -- Examine the individual statements of the block in reverse to
6286 -- locate the last initialization call.
6288 if Present (HSS) and then Present (Statements (HSS)) then
6289 Stmt := Last (Statements (HSS));
6291 while Present (Stmt) loop
6292 -- Peek inside nested blocks in case aborts are allowed
6294 if Nkind (Stmt) = N_Block_Statement then
6295 return Find_Last_Init_In_Block (Stmt);
6297 elsif Is_Init_Call (Stmt) then
6298 return Stmt;
6299 end if;
6301 Prev (Stmt);
6302 end loop;
6303 end if;
6305 return Empty;
6306 end Find_Last_Init_In_Block;
6308 ------------------
6309 -- Is_Init_Call --
6310 ------------------
6312 function Is_Init_Call (N : Node_Id) return Boolean is
6313 function Is_Init_Proc_Of
6314 (Subp : Entity_Id;
6315 Typ : Entity_Id) return Boolean;
6316 -- Determine whether subprogram Subp_Id is a valid init proc of
6317 -- type Typ.
6319 ---------------------
6320 -- Is_Init_Proc_Of --
6321 ---------------------
6323 function Is_Init_Proc_Of
6324 (Subp : Entity_Id;
6325 Typ : Entity_Id) return Boolean
6327 Deep_Init : Entity_Id := Empty;
6328 Prim_Init : Entity_Id := Empty;
6329 Type_Init : Entity_Id := Empty;
6331 begin
6332 -- Obtain all possible initialization routines of the
6333 -- related type and try to match the subprogram entity
6334 -- against one of them.
6336 -- Deep_Initialize
6338 Deep_Init := TSS (Typ, TSS_Deep_Initialize);
6340 -- Primitive Initialize
6342 if Is_Controlled (Typ) then
6343 Prim_Init := Find_Controlled_Prim_Op (Typ, Name_Initialize);
6345 if Present (Prim_Init) then
6346 Prim_Init := Ultimate_Alias (Prim_Init);
6347 end if;
6348 end if;
6350 -- Type initialization routine
6352 if Has_Non_Null_Base_Init_Proc (Typ) then
6353 Type_Init := Base_Init_Proc (Typ);
6354 end if;
6356 return
6357 (Present (Deep_Init) and then Subp = Deep_Init)
6358 or else
6359 (Present (Prim_Init) and then Subp = Prim_Init)
6360 or else
6361 (Present (Type_Init) and then Subp = Type_Init);
6362 end Is_Init_Proc_Of;
6364 -- Local variables
6366 Call_Id : Entity_Id;
6368 -- Start of processing for Is_Init_Call
6370 begin
6371 if Nkind (N) = N_Procedure_Call_Statement
6372 and then Is_Entity_Name (Name (N))
6373 then
6374 Call_Id := Entity (Name (N));
6376 -- Consider both the type of the object declaration and its
6377 -- related initialization type.
6379 return
6380 Is_Init_Proc_Of (Call_Id, Init_Typ)
6381 or else
6382 Is_Init_Proc_Of (Call_Id, Obj_Typ);
6383 end if;
6385 return False;
6386 end Is_Init_Call;
6388 -----------------------------
6389 -- Next_Suitable_Statement --
6390 -----------------------------
6392 function Next_Suitable_Statement (Stmt : Node_Id) return Node_Id is
6393 Result : Node_Id;
6395 begin
6396 -- Skip call markers and Program_Error raises installed by the
6397 -- ABE mechanism.
6399 Result := Next (Stmt);
6400 while Present (Result) loop
6401 exit when Nkind (Result) not in
6402 N_Call_Marker | N_Raise_Program_Error;
6404 Next (Result);
6405 end loop;
6407 return Result;
6408 end Next_Suitable_Statement;
6410 -- Local variables
6412 Call : Node_Id;
6413 Last_Init : Node_Id;
6414 Stmt : Node_Id;
6415 Stmt_2 : Node_Id;
6417 Deep_Init_Found : Boolean := False;
6418 -- A flag set when a call to [Deep_]Initialize has been found
6420 -- Start of processing for Find_Last_Init
6422 begin
6423 Last_Init := Decl;
6425 -- Objects that capture controlled function results do not require
6426 -- initialization.
6428 if Nkind (Decl) = N_Object_Declaration
6429 and then Nkind (Expression (Decl)) = N_Reference
6430 then
6431 return Last_Init;
6432 end if;
6434 Obj_Typ := Base_Type (Etype (Obj_Id));
6436 if Is_Access_Type (Obj_Typ) then
6437 Obj_Typ := Base_Type (Available_View (Designated_Type (Obj_Typ)));
6438 end if;
6440 -- Handle the initialization type of the object declaration
6442 if Is_Class_Wide_Type (Obj_Typ)
6443 and then Nkind (Decl) = N_Object_Declaration
6444 and then Nkind (Expression (Decl)) = N_Allocator
6445 then
6446 Init_Typ := Base_Type (Etype (Expression (Expression (Decl))));
6447 else
6448 Init_Typ := Obj_Typ;
6449 end if;
6451 loop
6452 if Is_Private_Type (Init_Typ)
6453 and then Present (Full_View (Init_Typ))
6454 then
6455 Init_Typ := Base_Type (Full_View (Init_Typ));
6457 elsif Is_Concurrent_Type (Init_Typ)
6458 and then Present (Corresponding_Record_Type (Init_Typ))
6459 then
6460 Init_Typ := Corresponding_Record_Type (Init_Typ);
6462 elsif Is_Untagged_Derivation (Init_Typ) then
6463 Init_Typ := Root_Type (Init_Typ);
6465 else
6466 exit;
6467 end if;
6468 end loop;
6470 if Present (Freeze_Node (Obj_Id)) then
6471 Stmt := First (Actions (Freeze_Node (Obj_Id)));
6472 else
6473 Stmt := Next_Suitable_Statement (Decl);
6474 end if;
6476 -- For an object with suppressed initialization, we check whether
6477 -- there is in fact no initialization expression. If there is not,
6478 -- then this is an object declaration that has been turned into a
6479 -- different object declaration that calls the build-in-place
6480 -- function in a 'Reference attribute, as in "F(...)'Reference".
6481 -- We search for that later object declaration, so that the
6482 -- attachment will be inserted after the call. Otherwise, if the
6483 -- call raises an exception, we will finalize the (uninitialized)
6484 -- object, which is wrong.
6486 if Nkind (Decl) = N_Object_Declaration
6487 and then No_Initialization (Decl)
6488 then
6489 if No (Expression (Last_Init)) then
6490 loop
6491 Next (Last_Init);
6493 exit when No (Last_Init);
6494 exit when Nkind (Last_Init) = N_Object_Declaration
6495 and then Nkind (Expression (Last_Init)) = N_Reference
6496 and then Nkind (Prefix (Expression (Last_Init))) =
6497 N_Function_Call
6498 and then Is_Expanded_Build_In_Place_Call
6499 (Prefix (Expression (Last_Init)));
6500 end loop;
6501 end if;
6503 return Last_Init;
6505 -- If the initialization is in the declaration, we're done, so
6506 -- early return if we have no more statements or they have been
6507 -- rewritten, which means that they were in the source code.
6509 elsif No (Stmt) or else Original_Node (Stmt) /= Stmt then
6510 return Last_Init;
6512 -- In all other cases the initialization calls follow the related
6513 -- object. The general structure of object initialization built by
6514 -- routine Default_Initialize_Object is as follows:
6516 -- [begin -- aborts allowed
6517 -- Abort_Defer;]
6518 -- Type_Init_Proc (Obj);
6519 -- [begin] -- exceptions allowed
6520 -- Deep_Initialize (Obj);
6521 -- [exception -- exceptions allowed
6522 -- when others =>
6523 -- Deep_Finalize (Obj, Self => False);
6524 -- raise;
6525 -- end;]
6526 -- [at end -- aborts allowed
6527 -- Abort_Undefer;
6528 -- end;]
6530 -- When aborts are allowed, the initialization calls are housed
6531 -- within a block.
6533 elsif Nkind (Stmt) = N_Block_Statement then
6534 Call := Find_Last_Init_In_Block (Stmt);
6536 if Present (Call) then
6537 Last_Init := Call;
6538 end if;
6540 -- Otherwise the initialization calls follow the related object
6542 else
6543 Stmt_2 := Next_Suitable_Statement (Stmt);
6545 -- Check for an optional call to Deep_Initialize which may
6546 -- appear within a block depending on whether the object has
6547 -- controlled components.
6549 if Present (Stmt_2) then
6550 if Nkind (Stmt_2) = N_Block_Statement then
6551 Call := Find_Last_Init_In_Block (Stmt_2);
6553 if Present (Call) then
6554 Deep_Init_Found := True;
6555 Last_Init := Call;
6556 end if;
6558 elsif Is_Init_Call (Stmt_2) then
6559 Deep_Init_Found := True;
6560 Last_Init := Stmt_2;
6561 end if;
6562 end if;
6564 -- If the object lacks a call to Deep_Initialize, then it must
6565 -- have a call to its related type init proc.
6567 if not Deep_Init_Found and then Is_Init_Call (Stmt) then
6568 Last_Init := Stmt;
6569 end if;
6570 end if;
6572 return Last_Init;
6573 end Find_Last_Init;
6575 ---------------------------
6576 -- Find_Optional_Prim_Op --
6577 ---------------------------
6579 function Find_Optional_Prim_Op
6580 (T : Entity_Id; Name : Name_Id) return Entity_Id
6582 Prim : Elmt_Id;
6583 Typ : Entity_Id := T;
6584 Op : Entity_Id;
6586 begin
6587 if Is_Class_Wide_Type (Typ) then
6588 Typ := Root_Type (Typ);
6589 end if;
6591 Typ := Underlying_Type (Typ);
6593 -- We cannot find the operation if there is no full view available
6595 if No (Typ) then
6596 return Empty;
6597 end if;
6599 -- Loop through primitive operations
6601 Prim := First_Elmt (Primitive_Operations (Typ));
6602 while Present (Prim) loop
6603 Op := Node (Prim);
6605 -- We can retrieve primitive operations by name if it is an internal
6606 -- name. For equality we must check that both of its operands have
6607 -- the same type, to avoid confusion with user-defined equalities
6608 -- than may have a asymmetric signature.
6610 exit when Chars (Op) = Name
6611 and then
6612 (Name /= Name_Op_Eq
6613 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
6615 Next_Elmt (Prim);
6616 end loop;
6618 return Node (Prim); -- Empty if not found
6619 end Find_Optional_Prim_Op;
6621 ---------------------------
6622 -- Find_Optional_Prim_Op --
6623 ---------------------------
6625 function Find_Optional_Prim_Op
6626 (T : Entity_Id;
6627 Name : TSS_Name_Type) return Entity_Id
6629 Inher_Op : Entity_Id := Empty;
6630 Own_Op : Entity_Id := Empty;
6631 Prim_Elmt : Elmt_Id;
6632 Prim_Id : Entity_Id;
6633 Typ : Entity_Id := T;
6635 begin
6636 if Is_Class_Wide_Type (Typ) then
6637 Typ := Root_Type (Typ);
6638 end if;
6640 Typ := Underlying_Type (Typ);
6642 -- This search is based on the assertion that the dispatching version
6643 -- of the TSS routine always precedes the real primitive.
6645 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6646 while Present (Prim_Elmt) loop
6647 Prim_Id := Node (Prim_Elmt);
6649 if Is_TSS (Prim_Id, Name) then
6650 if Present (Alias (Prim_Id)) then
6651 Inher_Op := Prim_Id;
6652 else
6653 Own_Op := Prim_Id;
6654 end if;
6655 end if;
6657 Next_Elmt (Prim_Elmt);
6658 end loop;
6660 if Present (Own_Op) then
6661 return Own_Op;
6662 elsif Present (Inher_Op) then
6663 return Inher_Op;
6664 else
6665 return Empty;
6666 end if;
6667 end Find_Optional_Prim_Op;
6669 ------------------
6670 -- Find_Prim_Op --
6671 ------------------
6673 function Find_Prim_Op
6674 (T : Entity_Id; Name : Name_Id) return Entity_Id
6676 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6677 begin
6678 if No (Result) then
6679 raise Program_Error;
6680 end if;
6682 return Result;
6683 end Find_Prim_Op;
6685 ------------------
6686 -- Find_Prim_Op --
6687 ------------------
6689 function Find_Prim_Op
6690 (T : Entity_Id;
6691 Name : TSS_Name_Type) return Entity_Id
6693 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6694 begin
6695 if No (Result) then
6696 raise Program_Error;
6697 end if;
6699 return Result;
6700 end Find_Prim_Op;
6702 ----------------------------
6703 -- Find_Protection_Object --
6704 ----------------------------
6706 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
6707 S : Entity_Id;
6709 begin
6710 S := Scop;
6711 while Present (S) loop
6712 if Ekind (S) in E_Entry | E_Entry_Family | E_Function | E_Procedure
6713 and then Present (Protection_Object (S))
6714 then
6715 return Protection_Object (S);
6716 end if;
6718 S := Scope (S);
6719 end loop;
6721 -- If we do not find a Protection object in the scope chain, then
6722 -- something has gone wrong, most likely the object was never created.
6724 raise Program_Error;
6725 end Find_Protection_Object;
6727 --------------------------
6728 -- Find_Protection_Type --
6729 --------------------------
6731 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
6732 Comp : Entity_Id;
6733 Typ : Entity_Id := Conc_Typ;
6735 begin
6736 if Is_Concurrent_Type (Typ) then
6737 Typ := Corresponding_Record_Type (Typ);
6738 end if;
6740 -- Since restriction violations are not considered serious errors, the
6741 -- expander remains active, but may leave the corresponding record type
6742 -- malformed. In such cases, component _object is not available so do
6743 -- not look for it.
6745 if not Analyzed (Typ) then
6746 return Empty;
6747 end if;
6749 Comp := First_Component (Typ);
6750 while Present (Comp) loop
6751 if Chars (Comp) = Name_uObject then
6752 return Base_Type (Etype (Comp));
6753 end if;
6755 Next_Component (Comp);
6756 end loop;
6758 -- The corresponding record of a protected type should always have an
6759 -- _object field.
6761 raise Program_Error;
6762 end Find_Protection_Type;
6764 function Find_Storage_Op
6765 (Typ : Entity_Id;
6766 Nam : Name_Id) return Entity_Id
6768 use Sem_Util.Storage_Model_Support;
6770 begin
6771 if Has_Storage_Model_Type_Aspect (Typ) then
6772 return Get_Storage_Model_Type_Entity (Typ, Nam);
6774 -- Otherwise we assume that Typ is a descendant of Root_Storage_Pool
6776 else
6777 return Find_Prim_Op (Typ, Nam);
6778 end if;
6779 end Find_Storage_Op;
6781 -----------------------
6782 -- Find_Hook_Context --
6783 -----------------------
6785 function Find_Hook_Context (N : Node_Id) return Node_Id is
6786 Par : Node_Id;
6787 Top : Node_Id;
6789 Wrapped_Node : Node_Id;
6790 -- Note: if we are in a transient scope, we want to reuse it as
6791 -- the context for actions insertion, if possible. But if N is itself
6792 -- part of the stored actions for the current transient scope,
6793 -- then we need to insert at the appropriate (inner) location in
6794 -- the not as an action on Node_To_Be_Wrapped.
6796 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
6798 begin
6799 -- When the node is inside a case/if expression, the lifetime of any
6800 -- temporary controlled object is extended. Find a suitable insertion
6801 -- node by locating the topmost case or if expressions.
6803 if In_Cond_Expr then
6804 Par := N;
6805 Top := N;
6806 while Present (Par) loop
6807 if Nkind (Original_Node (Par)) in
6808 N_Case_Expression | N_If_Expression
6809 then
6810 Top := Par;
6812 -- Prevent the search from going too far
6814 elsif Is_Body_Or_Package_Declaration (Par) then
6815 exit;
6816 end if;
6818 Par := Parent (Par);
6819 end loop;
6821 -- The topmost case or if expression is now recovered, but it may
6822 -- still not be the correct place to add generated code. Climb to
6823 -- find a parent that is part of a declarative or statement list,
6824 -- and is not a list of actuals in a call.
6826 Par := Top;
6827 while Present (Par) loop
6828 if Is_List_Member (Par)
6829 and then Nkind (Par) not in N_Component_Association
6830 | N_Discriminant_Association
6831 | N_Parameter_Association
6832 | N_Pragma_Argument_Association
6833 | N_Aggregate
6834 | N_Delta_Aggregate
6835 | N_Extension_Aggregate
6836 and then Nkind (Parent (Par)) not in N_Function_Call
6837 | N_Procedure_Call_Statement
6838 | N_Entry_Call_Statement
6839 | N_Aggregate
6840 | N_Delta_Aggregate
6841 | N_Extension_Aggregate
6842 then
6843 return Par;
6845 -- Prevent the search from going too far
6847 elsif Is_Body_Or_Package_Declaration (Par) then
6848 exit;
6849 end if;
6851 Par := Parent (Par);
6852 end loop;
6854 return Par;
6856 else
6857 Par := N;
6858 while Present (Par) loop
6860 -- Keep climbing past various operators
6862 if Nkind (Parent (Par)) in N_Op
6863 or else Nkind (Parent (Par)) in N_And_Then | N_Or_Else
6864 then
6865 Par := Parent (Par);
6866 else
6867 exit;
6868 end if;
6869 end loop;
6871 Top := Par;
6873 -- The node may be located in a pragma in which case return the
6874 -- pragma itself:
6876 -- pragma Precondition (... and then Ctrl_Func_Call ...);
6878 -- Similar case occurs when the node is related to an object
6879 -- declaration or assignment:
6881 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
6883 -- Another case to consider is when the node is part of a return
6884 -- statement:
6886 -- return ... and then Ctrl_Func_Call ...;
6888 -- Another case is when the node acts as a formal in a procedure
6889 -- call statement:
6891 -- Proc (... and then Ctrl_Func_Call ...);
6893 if Scope_Is_Transient then
6894 Wrapped_Node := Node_To_Be_Wrapped;
6895 else
6896 Wrapped_Node := Empty;
6897 end if;
6899 while Present (Par) loop
6900 if Par = Wrapped_Node
6901 or else Nkind (Par) in N_Assignment_Statement
6902 | N_Object_Declaration
6903 | N_Pragma
6904 | N_Procedure_Call_Statement
6905 | N_Simple_Return_Statement
6906 then
6907 return Par;
6909 -- Prevent the search from going too far
6911 elsif Is_Body_Or_Package_Declaration (Par) then
6912 exit;
6913 end if;
6915 Par := Parent (Par);
6916 end loop;
6918 -- Return the topmost short circuit operator
6920 return Top;
6921 end if;
6922 end Find_Hook_Context;
6924 ------------------------------
6925 -- Following_Address_Clause --
6926 ------------------------------
6928 function Following_Address_Clause (D : Node_Id) return Node_Id is
6929 Id : constant Entity_Id := Defining_Identifier (D);
6930 Result : Node_Id;
6931 Par : Node_Id;
6933 function Check_Decls (D : Node_Id) return Node_Id;
6934 -- This internal function differs from the main function in that it
6935 -- gets called to deal with a following package private part, and
6936 -- it checks declarations starting with D (the main function checks
6937 -- declarations following D). If D is Empty, then Empty is returned.
6939 -----------------
6940 -- Check_Decls --
6941 -----------------
6943 function Check_Decls (D : Node_Id) return Node_Id is
6944 Decl : Node_Id;
6946 begin
6947 Decl := D;
6948 while Present (Decl) loop
6949 if Nkind (Decl) = N_At_Clause
6950 and then Chars (Identifier (Decl)) = Chars (Id)
6951 then
6952 return Decl;
6954 elsif Nkind (Decl) = N_Attribute_Definition_Clause
6955 and then Chars (Decl) = Name_Address
6956 and then Chars (Name (Decl)) = Chars (Id)
6957 then
6958 return Decl;
6959 end if;
6961 Next (Decl);
6962 end loop;
6964 -- Otherwise not found, return Empty
6966 return Empty;
6967 end Check_Decls;
6969 -- Start of processing for Following_Address_Clause
6971 begin
6972 -- If parser detected no address clause for the identifier in question,
6973 -- then the answer is a quick NO, without the need for a search.
6975 if not Get_Name_Table_Boolean1 (Chars (Id)) then
6976 return Empty;
6977 end if;
6979 -- Otherwise search current declarative unit
6981 Result := Check_Decls (Next (D));
6983 if Present (Result) then
6984 return Result;
6985 end if;
6987 -- Check for possible package private part following
6989 Par := Parent (D);
6991 if Nkind (Par) = N_Package_Specification
6992 and then Visible_Declarations (Par) = List_Containing (D)
6993 and then Present (Private_Declarations (Par))
6994 then
6995 -- Private part present, check declarations there
6997 return Check_Decls (First (Private_Declarations (Par)));
6999 else
7000 -- No private part, clause not found, return Empty
7002 return Empty;
7003 end if;
7004 end Following_Address_Clause;
7006 ----------------------
7007 -- Force_Evaluation --
7008 ----------------------
7010 procedure Force_Evaluation
7011 (Exp : Node_Id;
7012 Name_Req : Boolean := False;
7013 Related_Id : Entity_Id := Empty;
7014 Is_Low_Bound : Boolean := False;
7015 Is_High_Bound : Boolean := False;
7016 Discr_Number : Int := 0;
7017 Mode : Force_Evaluation_Mode := Relaxed)
7019 begin
7020 Remove_Side_Effects
7021 (Exp => Exp,
7022 Name_Req => Name_Req,
7023 Variable_Ref => True,
7024 Renaming_Req => False,
7025 Related_Id => Related_Id,
7026 Is_Low_Bound => Is_Low_Bound,
7027 Is_High_Bound => Is_High_Bound,
7028 Discr_Number => Discr_Number,
7029 Check_Side_Effects =>
7030 Is_Static_Expression (Exp)
7031 or else Mode = Relaxed);
7032 end Force_Evaluation;
7034 ---------------------------------
7035 -- Fully_Qualified_Name_String --
7036 ---------------------------------
7038 function Fully_Qualified_Name_String
7039 (E : Entity_Id;
7040 Append_NUL : Boolean := True) return String_Id
7042 procedure Internal_Full_Qualified_Name (E : Entity_Id);
7043 -- Compute recursively the qualified name without NUL at the end, adding
7044 -- it to the currently started string being generated
7046 ----------------------------------
7047 -- Internal_Full_Qualified_Name --
7048 ----------------------------------
7050 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
7051 Ent : Entity_Id;
7053 begin
7054 -- Deal properly with child units
7056 if Nkind (E) = N_Defining_Program_Unit_Name then
7057 Ent := Defining_Identifier (E);
7058 else
7059 Ent := E;
7060 end if;
7062 -- Compute qualification recursively (only "Standard" has no scope)
7064 if Present (Scope (Scope (Ent))) then
7065 Internal_Full_Qualified_Name (Scope (Ent));
7066 Store_String_Char (Get_Char_Code ('.'));
7067 end if;
7069 -- Every entity should have a name except some expanded blocks
7070 -- don't bother about those.
7072 if Chars (Ent) = No_Name then
7073 return;
7074 end if;
7076 -- Generates the entity name in upper case
7078 Get_Decoded_Name_String (Chars (Ent));
7079 Set_Casing (All_Upper_Case);
7080 Store_String_Chars (Name_Buffer (1 .. Name_Len));
7081 return;
7082 end Internal_Full_Qualified_Name;
7084 -- Start of processing for Full_Qualified_Name
7086 begin
7087 Start_String;
7088 Internal_Full_Qualified_Name (E);
7090 if Append_NUL then
7091 Store_String_Char (Get_Char_Code (ASCII.NUL));
7092 end if;
7094 return End_String;
7095 end Fully_Qualified_Name_String;
7097 ---------------------------------
7098 -- Get_Current_Value_Condition --
7099 ---------------------------------
7101 -- Note: the implementation of this procedure is very closely tied to the
7102 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
7103 -- interpret Current_Value fields set by the Set procedure, so the two
7104 -- procedures need to be closely coordinated.
7106 procedure Get_Current_Value_Condition
7107 (Var : Node_Id;
7108 Op : out Node_Kind;
7109 Val : out Node_Id)
7111 Loc : constant Source_Ptr := Sloc (Var);
7112 Ent : constant Entity_Id := Entity (Var);
7114 procedure Process_Current_Value_Condition (N : Node_Id; S : Boolean);
7115 -- N is an expression which holds either True (S = True) or False (S =
7116 -- False) in the condition. This procedure digs out the expression and
7117 -- if it refers to Ent, sets Op and Val appropriately.
7119 -------------------------------------
7120 -- Process_Current_Value_Condition --
7121 -------------------------------------
7123 procedure Process_Current_Value_Condition
7124 (N : Node_Id;
7125 S : Boolean)
7127 Cond : Node_Id;
7128 Prev_Cond : Node_Id;
7129 Sens : Boolean;
7131 begin
7132 Cond := N;
7133 Sens := S;
7135 loop
7136 Prev_Cond := Cond;
7138 -- Deal with NOT operators, inverting sense
7140 while Nkind (Cond) = N_Op_Not loop
7141 Cond := Right_Opnd (Cond);
7142 Sens := not Sens;
7143 end loop;
7145 -- Deal with conversions, qualifications, and expressions with
7146 -- actions.
7148 while Nkind (Cond) in N_Type_Conversion
7149 | N_Qualified_Expression
7150 | N_Expression_With_Actions
7151 loop
7152 Cond := Expression (Cond);
7153 end loop;
7155 exit when Cond = Prev_Cond;
7156 end loop;
7158 -- Deal with AND THEN and AND cases
7160 if Nkind (Cond) in N_And_Then | N_Op_And then
7162 -- Don't ever try to invert a condition that is of the form of an
7163 -- AND or AND THEN (since we are not doing sufficiently general
7164 -- processing to allow this).
7166 if Sens = False then
7167 Op := N_Empty;
7168 Val := Empty;
7169 return;
7170 end if;
7172 -- Recursively process AND and AND THEN branches
7174 Process_Current_Value_Condition (Left_Opnd (Cond), True);
7175 pragma Assert (Op'Valid);
7177 if Op /= N_Empty then
7178 return;
7179 end if;
7181 Process_Current_Value_Condition (Right_Opnd (Cond), True);
7182 return;
7184 -- Case of relational operator
7186 elsif Nkind (Cond) in N_Op_Compare then
7187 Op := Nkind (Cond);
7189 -- Invert sense of test if inverted test
7191 if Sens = False then
7192 case Op is
7193 when N_Op_Eq => Op := N_Op_Ne;
7194 when N_Op_Ne => Op := N_Op_Eq;
7195 when N_Op_Lt => Op := N_Op_Ge;
7196 when N_Op_Gt => Op := N_Op_Le;
7197 when N_Op_Le => Op := N_Op_Gt;
7198 when N_Op_Ge => Op := N_Op_Lt;
7199 when others => raise Program_Error;
7200 end case;
7201 end if;
7203 -- Case of entity op value
7205 if Is_Entity_Name (Left_Opnd (Cond))
7206 and then Ent = Entity (Left_Opnd (Cond))
7207 and then Compile_Time_Known_Value (Right_Opnd (Cond))
7208 then
7209 Val := Right_Opnd (Cond);
7211 -- Case of value op entity
7213 elsif Is_Entity_Name (Right_Opnd (Cond))
7214 and then Ent = Entity (Right_Opnd (Cond))
7215 and then Compile_Time_Known_Value (Left_Opnd (Cond))
7216 then
7217 Val := Left_Opnd (Cond);
7219 -- We are effectively swapping operands
7221 case Op is
7222 when N_Op_Eq => null;
7223 when N_Op_Ne => null;
7224 when N_Op_Lt => Op := N_Op_Gt;
7225 when N_Op_Gt => Op := N_Op_Lt;
7226 when N_Op_Le => Op := N_Op_Ge;
7227 when N_Op_Ge => Op := N_Op_Le;
7228 when others => raise Program_Error;
7229 end case;
7231 else
7232 Op := N_Empty;
7233 end if;
7235 return;
7237 elsif Nkind (Cond) in N_Type_Conversion
7238 | N_Qualified_Expression
7239 | N_Expression_With_Actions
7240 then
7241 Cond := Expression (Cond);
7243 -- Case of Boolean variable reference, return as though the
7244 -- reference had said var = True.
7246 else
7247 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
7248 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
7250 if Sens = False then
7251 Op := N_Op_Ne;
7252 else
7253 Op := N_Op_Eq;
7254 end if;
7255 end if;
7256 end if;
7257 end Process_Current_Value_Condition;
7259 -- Start of processing for Get_Current_Value_Condition
7261 begin
7262 Op := N_Empty;
7263 Val := Empty;
7265 -- Immediate return, nothing doing, if this is not an object
7267 if not Is_Object (Ent) then
7268 return;
7269 end if;
7271 -- In GNATprove mode we don't want to use current value optimizer, in
7272 -- particular for loop invariant expressions and other assertions that
7273 -- act as cut points for proof. The optimizer often folds expressions
7274 -- into True/False where they trivially follow from the previous
7275 -- assignments, but this deprives proof from the information needed to
7276 -- discharge checks that are beyond the scope of the value optimizer.
7278 if GNATprove_Mode then
7279 return;
7280 end if;
7282 -- Otherwise examine current value
7284 declare
7285 CV : constant Node_Id := Current_Value (Ent);
7286 Sens : Boolean;
7287 Stm : Node_Id;
7289 begin
7290 -- If statement. Condition is known true in THEN section, known False
7291 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
7293 if Nkind (CV) = N_If_Statement then
7295 -- Before start of IF statement
7297 if Loc < Sloc (CV) then
7298 return;
7300 -- In condition of IF statement
7302 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7303 return;
7305 -- After end of IF statement
7307 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
7308 return;
7309 end if;
7311 -- At this stage we know that we are within the IF statement, but
7312 -- unfortunately, the tree does not record the SLOC of the ELSE so
7313 -- we cannot use a simple SLOC comparison to distinguish between
7314 -- the then/else statements, so we have to climb the tree.
7316 declare
7317 N : Node_Id;
7319 begin
7320 N := Parent (Var);
7321 while Parent (N) /= CV loop
7322 N := Parent (N);
7324 -- If we fall off the top of the tree, then that's odd, but
7325 -- perhaps it could occur in some error situation, and the
7326 -- safest response is simply to assume that the outcome of
7327 -- the condition is unknown. No point in bombing during an
7328 -- attempt to optimize things.
7330 if No (N) then
7331 return;
7332 end if;
7333 end loop;
7335 -- Now we have N pointing to a node whose parent is the IF
7336 -- statement in question, so now we can tell if we are within
7337 -- the THEN statements.
7339 if Is_List_Member (N)
7340 and then List_Containing (N) = Then_Statements (CV)
7341 then
7342 Sens := True;
7344 -- If the variable reference does not come from source, we
7345 -- cannot reliably tell whether it appears in the else part.
7346 -- In particular, if it appears in generated code for a node
7347 -- that requires finalization, it may be attached to a list
7348 -- that has not been yet inserted into the code. For now,
7349 -- treat it as unknown.
7351 elsif not Comes_From_Source (N) then
7352 return;
7354 -- Otherwise we must be in ELSIF or ELSE part
7356 else
7357 Sens := False;
7358 end if;
7359 end;
7361 -- ELSIF part. Condition is known true within the referenced
7362 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
7363 -- and unknown before the ELSE part or after the IF statement.
7365 elsif Nkind (CV) = N_Elsif_Part then
7367 -- if the Elsif_Part had condition_actions, the elsif has been
7368 -- rewritten as a nested if, and the original elsif_part is
7369 -- detached from the tree, so there is no way to obtain useful
7370 -- information on the current value of the variable.
7371 -- Can this be improved ???
7373 if No (Parent (CV)) then
7374 return;
7375 end if;
7377 Stm := Parent (CV);
7379 -- If the tree has been otherwise rewritten there is nothing
7380 -- else to be done either.
7382 if Nkind (Stm) /= N_If_Statement then
7383 return;
7384 end if;
7386 -- Before start of ELSIF part
7388 if Loc < Sloc (CV) then
7389 return;
7391 -- In condition of ELSIF part
7393 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7394 return;
7396 -- After end of IF statement
7398 elsif Loc >= Sloc (Stm) +
7399 Text_Ptr (UI_To_Int (End_Span (Stm)))
7400 then
7401 return;
7402 end if;
7404 -- Again we lack the SLOC of the ELSE, so we need to climb the
7405 -- tree to see if we are within the ELSIF part in question.
7407 declare
7408 N : Node_Id;
7410 begin
7411 N := Parent (Var);
7412 while Parent (N) /= Stm loop
7413 N := Parent (N);
7415 -- If we fall off the top of the tree, then that's odd, but
7416 -- perhaps it could occur in some error situation, and the
7417 -- safest response is simply to assume that the outcome of
7418 -- the condition is unknown. No point in bombing during an
7419 -- attempt to optimize things.
7421 if No (N) then
7422 return;
7423 end if;
7424 end loop;
7426 -- Now we have N pointing to a node whose parent is the IF
7427 -- statement in question, so see if is the ELSIF part we want.
7428 -- the THEN statements.
7430 if N = CV then
7431 Sens := True;
7433 -- Otherwise we must be in subsequent ELSIF or ELSE part
7435 else
7436 Sens := False;
7437 end if;
7438 end;
7440 -- Iteration scheme of while loop. The condition is known to be
7441 -- true within the body of the loop.
7443 elsif Nkind (CV) = N_Iteration_Scheme then
7444 declare
7445 Loop_Stmt : constant Node_Id := Parent (CV);
7447 begin
7448 -- Before start of body of loop
7450 if Loc < Sloc (Loop_Stmt) then
7451 return;
7453 -- In condition of while loop
7455 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7456 return;
7458 -- After end of LOOP statement
7460 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
7461 return;
7463 -- We are within the body of the loop
7465 else
7466 Sens := True;
7467 end if;
7468 end;
7470 -- All other cases of Current_Value settings
7472 else
7473 return;
7474 end if;
7476 -- If we fall through here, then we have a reportable condition, Sens
7477 -- is True if the condition is true and False if it needs inverting.
7479 Process_Current_Value_Condition (Condition (CV), Sens);
7480 end;
7481 end Get_Current_Value_Condition;
7483 -----------------------
7484 -- Get_Index_Subtype --
7485 -----------------------
7487 function Get_Index_Subtype (N : Node_Id) return Entity_Id is
7488 P_Type : Entity_Id := Etype (Prefix (N));
7489 Indx : Node_Id;
7490 J : Int;
7492 begin
7493 if Is_Access_Type (P_Type) then
7494 P_Type := Designated_Type (P_Type);
7495 end if;
7497 if No (Expressions (N)) then
7498 J := 1;
7499 else
7500 J := UI_To_Int (Expr_Value (First (Expressions (N))));
7501 end if;
7503 Indx := First_Index (P_Type);
7504 while J > 1 loop
7505 Next_Index (Indx);
7506 J := J - 1;
7507 end loop;
7509 return Etype (Indx);
7510 end Get_Index_Subtype;
7512 -----------------------
7513 -- Get_Mapped_Entity --
7514 -----------------------
7516 function Get_Mapped_Entity (E : Entity_Id) return Entity_Id is
7517 begin
7518 return Type_Map.Get (E);
7519 end Get_Mapped_Entity;
7521 ---------------------
7522 -- Get_Stream_Size --
7523 ---------------------
7525 function Get_Stream_Size (E : Entity_Id) return Uint is
7526 begin
7527 -- If we have a Stream_Size clause for this type use it
7529 if Has_Stream_Size_Clause (E) then
7530 return Static_Integer (Expression (Stream_Size_Clause (E)));
7532 -- Otherwise the Stream_Size is the size of the type
7534 else
7535 return Esize (E);
7536 end if;
7537 end Get_Stream_Size;
7539 ---------------------------
7540 -- Has_Access_Constraint --
7541 ---------------------------
7543 function Has_Access_Constraint (E : Entity_Id) return Boolean is
7544 Disc : Entity_Id;
7545 T : constant Entity_Id := Etype (E);
7547 begin
7548 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
7549 Disc := First_Discriminant (T);
7550 while Present (Disc) loop
7551 if Is_Access_Type (Etype (Disc)) then
7552 return True;
7553 end if;
7555 Next_Discriminant (Disc);
7556 end loop;
7558 return False;
7559 else
7560 return False;
7561 end if;
7562 end Has_Access_Constraint;
7564 ---------------------
7565 -- Has_Tag_Of_Type --
7566 ---------------------
7568 function Has_Tag_Of_Type (Exp : Node_Id) return Boolean is
7569 Typ : constant Entity_Id := Etype (Exp);
7571 begin
7572 pragma Assert (Is_Tagged_Type (Typ));
7574 -- The tag of an object of a class-wide type is that of its
7575 -- initialization expression.
7577 if Is_Class_Wide_Type (Typ) then
7578 return False;
7579 end if;
7581 -- The tag of a stand-alone object of a specific tagged type T
7582 -- identifies T.
7584 if Is_Entity_Name (Exp)
7585 and then Ekind (Entity (Exp)) in E_Constant | E_Variable
7586 then
7587 return True;
7589 else
7590 case Nkind (Exp) is
7591 -- The tag of a component or an aggregate of a specific tagged
7592 -- type T identifies T.
7594 when N_Indexed_Component
7595 | N_Selected_Component
7596 | N_Aggregate
7597 | N_Extension_Aggregate
7599 return True;
7601 -- The tag of the result returned by a function whose result
7602 -- type is a specific tagged type T identifies T.
7604 when N_Function_Call =>
7605 return True;
7607 when N_Explicit_Dereference =>
7608 return Is_Captured_Function_Call (Exp);
7610 -- For a tagged type, the operand of a qualified expression
7611 -- shall resolve to be of the type of the expression.
7613 when N_Qualified_Expression =>
7614 return Has_Tag_Of_Type (Expression (Exp));
7616 when others =>
7617 return False;
7618 end case;
7619 end if;
7620 end Has_Tag_Of_Type;
7622 --------------------
7623 -- Homonym_Number --
7624 --------------------
7626 function Homonym_Number (Subp : Entity_Id) return Pos is
7627 Hom : Entity_Id := Homonym (Subp);
7628 Count : Pos := 1;
7630 begin
7631 while Present (Hom) loop
7632 if Scope (Hom) = Scope (Subp) then
7633 Count := Count + 1;
7634 end if;
7636 Hom := Homonym (Hom);
7637 end loop;
7639 return Count;
7640 end Homonym_Number;
7642 -----------------------------------
7643 -- In_Library_Level_Package_Body --
7644 -----------------------------------
7646 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
7647 begin
7648 -- First determine whether the entity appears at the library level, then
7649 -- look at the containing unit.
7651 if Is_Library_Level_Entity (Id) then
7652 declare
7653 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
7655 begin
7656 return Nkind (Unit (Container)) = N_Package_Body;
7657 end;
7658 end if;
7660 return False;
7661 end In_Library_Level_Package_Body;
7663 ------------------------------
7664 -- In_Unconditional_Context --
7665 ------------------------------
7667 function In_Unconditional_Context (Node : Node_Id) return Boolean is
7668 P : Node_Id;
7670 begin
7671 P := Node;
7672 while Present (P) loop
7673 case Nkind (P) is
7674 when N_Subprogram_Body => return True;
7675 when N_If_Statement => return False;
7676 when N_Loop_Statement => return False;
7677 when N_Case_Statement => return False;
7678 when others => P := Parent (P);
7679 end case;
7680 end loop;
7682 return False;
7683 end In_Unconditional_Context;
7685 ----------------------------
7686 -- Init_Proc_Level_Formal --
7687 ----------------------------
7689 function Init_Proc_Level_Formal (Proc : Entity_Id) return Entity_Id is
7690 Form : Entity_Id;
7692 begin
7693 -- Go through the formals of the initialization procedure Proc to find
7694 -- the extra accessibility level parameter associated with the object
7695 -- being initialized.
7697 Form := First_Formal (Proc);
7698 while Present (Form) loop
7699 if Chars (Form) = Name_uInit_Level then
7700 return Form;
7701 end if;
7703 Next_Formal (Form);
7704 end loop;
7706 -- No formal was found, return Empty
7708 return Empty;
7709 end Init_Proc_Level_Formal;
7711 -------------------
7712 -- Insert_Action --
7713 -------------------
7715 procedure Insert_Action
7716 (Assoc_Node : Node_Id;
7717 Ins_Action : Node_Id;
7718 Spec_Expr_OK : Boolean := False)
7720 begin
7721 if Present (Ins_Action) then
7722 Insert_Actions
7723 (Assoc_Node => Assoc_Node,
7724 Ins_Actions => New_List (Ins_Action),
7725 Spec_Expr_OK => Spec_Expr_OK);
7726 end if;
7727 end Insert_Action;
7729 -- Version with check(s) suppressed
7731 procedure Insert_Action
7732 (Assoc_Node : Node_Id;
7733 Ins_Action : Node_Id;
7734 Suppress : Check_Id;
7735 Spec_Expr_OK : Boolean := False)
7737 begin
7738 Insert_Actions
7739 (Assoc_Node => Assoc_Node,
7740 Ins_Actions => New_List (Ins_Action),
7741 Suppress => Suppress,
7742 Spec_Expr_OK => Spec_Expr_OK);
7743 end Insert_Action;
7745 -------------------------
7746 -- Insert_Action_After --
7747 -------------------------
7749 procedure Insert_Action_After
7750 (Assoc_Node : Node_Id;
7751 Ins_Action : Node_Id)
7753 begin
7754 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
7755 end Insert_Action_After;
7757 --------------------
7758 -- Insert_Actions --
7759 --------------------
7761 procedure Insert_Actions
7762 (Assoc_Node : Node_Id;
7763 Ins_Actions : List_Id;
7764 Spec_Expr_OK : Boolean := False)
7766 N : Node_Id;
7767 P : Node_Id;
7769 Wrapped_Node : Node_Id := Empty;
7771 begin
7772 if Is_Empty_List (Ins_Actions) then
7773 return;
7774 end if;
7776 -- Insert the action when the context is "Handling of Default and Per-
7777 -- Object Expressions" only when requested by the caller.
7779 if Spec_Expr_OK then
7780 null;
7782 -- Ignore insert of actions from inside default expression (or other
7783 -- similar "spec expression") in the special spec-expression analyze
7784 -- mode. Any insertions at this point have no relevance, since we are
7785 -- only doing the analyze to freeze the types of any static expressions.
7786 -- See section "Handling of Default and Per-Object Expressions" in the
7787 -- spec of package Sem for further details.
7789 elsif In_Spec_Expression then
7790 return;
7791 end if;
7793 -- If the action derives from stuff inside a record, then the actions
7794 -- are attached to the current scope, to be inserted and analyzed on
7795 -- exit from the scope. The reason for this is that we may also be
7796 -- generating freeze actions at the same time, and they must eventually
7797 -- be elaborated in the correct order.
7799 if Is_Record_Type (Current_Scope)
7800 and then not Is_Frozen (Current_Scope)
7801 then
7802 if No (Scope_Stack.Table
7803 (Scope_Stack.Last).Pending_Freeze_Actions)
7804 then
7805 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
7806 Ins_Actions;
7807 else
7808 Append_List
7809 (Ins_Actions,
7810 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
7811 end if;
7813 return;
7814 end if;
7816 -- We now intend to climb up the tree to find the right point to
7817 -- insert the actions. We start at Assoc_Node, unless this node is a
7818 -- subexpression in which case we start with its parent. We do this for
7819 -- two reasons. First it speeds things up. Second, if Assoc_Node is
7820 -- itself one of the special nodes like N_And_Then, then we assume that
7821 -- an initial request to insert actions for such a node does not expect
7822 -- the actions to get deposited in the node for later handling when the
7823 -- node is expanded, since clearly the node is being dealt with by the
7824 -- caller. Note that in the subexpression case, N is always the child we
7825 -- came from.
7827 -- N_Raise_xxx_Error is an annoying special case, it is a statement
7828 -- if it has type Standard_Void_Type, and a subexpression otherwise.
7829 -- Procedure calls, and similarly procedure attribute references, are
7830 -- also statements.
7832 if Nkind (Assoc_Node) in N_Subexpr
7833 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
7834 or else Etype (Assoc_Node) /= Standard_Void_Type)
7835 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
7836 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
7837 or else not Is_Procedure_Attribute_Name
7838 (Attribute_Name (Assoc_Node)))
7839 then
7840 N := Assoc_Node;
7841 P := Parent (Assoc_Node);
7843 -- Nonsubexpression case. Note that N is initially Empty in this case
7844 -- (N is only guaranteed non-Empty in the subexpr case).
7846 else
7847 N := Empty;
7848 P := Assoc_Node;
7849 end if;
7851 -- Capture root of the transient scope
7853 if Scope_Is_Transient then
7854 Wrapped_Node := Node_To_Be_Wrapped;
7855 end if;
7857 loop
7858 pragma Assert (Present (P));
7860 -- Make sure that inserted actions stay in the transient scope
7862 if Present (Wrapped_Node) and then N = Wrapped_Node then
7863 Store_Before_Actions_In_Scope (Ins_Actions);
7864 return;
7865 end if;
7867 case Nkind (P) is
7869 -- Case of right operand of AND THEN or OR ELSE. Put the actions
7870 -- in the Actions field of the right operand. They will be moved
7871 -- out further when the AND THEN or OR ELSE operator is expanded.
7872 -- Nothing special needs to be done for the left operand since
7873 -- in that case the actions are executed unconditionally.
7875 when N_Short_Circuit =>
7876 if N = Right_Opnd (P) then
7878 -- We are now going to either append the actions to the
7879 -- actions field of the short-circuit operation. We will
7880 -- also analyze the actions now.
7882 -- This analysis is really too early, the proper thing would
7883 -- be to just park them there now, and only analyze them if
7884 -- we find we really need them, and to it at the proper
7885 -- final insertion point. However attempting to this proved
7886 -- tricky, so for now we just kill current values before and
7887 -- after the analyze call to make sure we avoid peculiar
7888 -- optimizations from this out of order insertion.
7890 Kill_Current_Values;
7892 -- If P has already been expanded, we can't park new actions
7893 -- on it, so we need to expand them immediately, introducing
7894 -- an Expression_With_Actions. N can't be an expression
7895 -- with actions, or else then the actions would have been
7896 -- inserted at an inner level.
7898 if Analyzed (P) then
7899 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
7900 Rewrite (N,
7901 Make_Expression_With_Actions (Sloc (N),
7902 Actions => Ins_Actions,
7903 Expression => Relocate_Node (N)));
7904 Analyze_And_Resolve (N);
7906 elsif Present (Actions (P)) then
7907 Insert_List_After_And_Analyze
7908 (Last (Actions (P)), Ins_Actions);
7909 else
7910 Set_Actions (P, Ins_Actions);
7911 Analyze_List (Actions (P));
7912 end if;
7914 Kill_Current_Values;
7916 return;
7917 end if;
7919 -- Then or Else dependent expression of an if expression. Add
7920 -- actions to Then_Actions or Else_Actions field as appropriate.
7921 -- The actions will be moved further out when the if is expanded.
7923 when N_If_Expression =>
7924 declare
7925 ThenX : constant Node_Id := Next (First (Expressions (P)));
7926 ElseX : constant Node_Id := Next (ThenX);
7928 begin
7929 -- If the enclosing expression is already analyzed, as
7930 -- is the case for nested elaboration checks, insert the
7931 -- conditional further out.
7933 if Analyzed (P) then
7934 null;
7936 -- Actions belong to the then expression, temporarily place
7937 -- them as Then_Actions of the if expression. They will be
7938 -- moved to the proper place later when the if expression is
7939 -- expanded.
7941 elsif N = ThenX then
7942 if Present (Then_Actions (P)) then
7943 Insert_List_After_And_Analyze
7944 (Last (Then_Actions (P)), Ins_Actions);
7945 else
7946 Set_Then_Actions (P, Ins_Actions);
7947 Analyze_List (Then_Actions (P));
7948 end if;
7950 return;
7952 -- Else_Actions is treated the same as Then_Actions above
7954 elsif N = ElseX then
7955 if Present (Else_Actions (P)) then
7956 Insert_List_After_And_Analyze
7957 (Last (Else_Actions (P)), Ins_Actions);
7958 else
7959 Set_Else_Actions (P, Ins_Actions);
7960 Analyze_List (Else_Actions (P));
7961 end if;
7963 return;
7965 -- Actions belong to the condition. In this case they are
7966 -- unconditionally executed, and so we can continue the
7967 -- search for the proper insert point.
7969 else
7970 null;
7971 end if;
7972 end;
7974 -- Alternative of case expression, we place the action in the
7975 -- Actions field of the case expression alternative, this will
7976 -- be handled when the case expression is expanded.
7978 when N_Case_Expression_Alternative =>
7979 if Present (Actions (P)) then
7980 Insert_List_After_And_Analyze
7981 (Last (Actions (P)), Ins_Actions);
7982 else
7983 Set_Actions (P, Ins_Actions);
7984 Analyze_List (Actions (P));
7985 end if;
7987 return;
7989 -- Case of appearing within an Expressions_With_Actions node. When
7990 -- the new actions come from the expression of the expression with
7991 -- actions, they must be added to the existing actions. The other
7992 -- alternative is when the new actions are related to one of the
7993 -- existing actions of the expression with actions, and should
7994 -- never reach here: if actions are inserted on a statement
7995 -- within the Actions of an expression with actions, or on some
7996 -- subexpression of such a statement, then the outermost proper
7997 -- insertion point is right before the statement, and we should
7998 -- never climb up as far as the N_Expression_With_Actions itself.
8000 when N_Expression_With_Actions =>
8001 if N = Expression (P) then
8002 if Is_Empty_List (Actions (P)) then
8003 Append_List_To (Actions (P), Ins_Actions);
8004 Analyze_List (Actions (P));
8005 else
8006 Insert_List_After_And_Analyze
8007 (Last (Actions (P)), Ins_Actions);
8008 end if;
8010 return;
8012 else
8013 raise Program_Error;
8014 end if;
8016 -- Case of appearing in the condition of a while expression or
8017 -- elsif. We insert the actions into the Condition_Actions field.
8018 -- They will be moved further out when the while loop or elsif
8019 -- is analyzed.
8021 when N_Elsif_Part
8022 | N_Iteration_Scheme
8024 if Present (Condition (P)) and then N = Condition (P) then
8025 if Present (Condition_Actions (P)) then
8026 Insert_List_After_And_Analyze
8027 (Last (Condition_Actions (P)), Ins_Actions);
8028 else
8029 Set_Condition_Actions (P, Ins_Actions);
8031 -- Set the parent of the insert actions explicitly. This
8032 -- is not a syntactic field, but we need the parent field
8033 -- set, in particular so that freeze can understand that
8034 -- it is dealing with condition actions, and properly
8035 -- insert the freezing actions.
8037 Set_Parent (Ins_Actions, P);
8038 Analyze_List (Condition_Actions (P));
8039 end if;
8041 return;
8042 end if;
8044 -- Statements, declarations, pragmas, representation clauses
8046 when
8047 -- Statements
8049 N_Procedure_Call_Statement
8050 | N_Statement_Other_Than_Procedure_Call
8052 -- Pragmas
8054 | N_Pragma
8056 -- Representation_Clause
8058 | N_At_Clause
8059 | N_Attribute_Definition_Clause
8060 | N_Enumeration_Representation_Clause
8061 | N_Record_Representation_Clause
8063 -- Declarations
8065 | N_Abstract_Subprogram_Declaration
8066 | N_Entry_Body
8067 | N_Exception_Declaration
8068 | N_Exception_Renaming_Declaration
8069 | N_Expression_Function
8070 | N_Formal_Abstract_Subprogram_Declaration
8071 | N_Formal_Concrete_Subprogram_Declaration
8072 | N_Formal_Object_Declaration
8073 | N_Formal_Type_Declaration
8074 | N_Full_Type_Declaration
8075 | N_Function_Instantiation
8076 | N_Generic_Function_Renaming_Declaration
8077 | N_Generic_Package_Declaration
8078 | N_Generic_Package_Renaming_Declaration
8079 | N_Generic_Procedure_Renaming_Declaration
8080 | N_Generic_Subprogram_Declaration
8081 | N_Implicit_Label_Declaration
8082 | N_Incomplete_Type_Declaration
8083 | N_Number_Declaration
8084 | N_Object_Declaration
8085 | N_Object_Renaming_Declaration
8086 | N_Package_Body
8087 | N_Package_Body_Stub
8088 | N_Package_Declaration
8089 | N_Package_Instantiation
8090 | N_Package_Renaming_Declaration
8091 | N_Private_Extension_Declaration
8092 | N_Private_Type_Declaration
8093 | N_Procedure_Instantiation
8094 | N_Protected_Body
8095 | N_Protected_Body_Stub
8096 | N_Single_Task_Declaration
8097 | N_Subprogram_Body
8098 | N_Subprogram_Body_Stub
8099 | N_Subprogram_Declaration
8100 | N_Subprogram_Renaming_Declaration
8101 | N_Subtype_Declaration
8102 | N_Task_Body
8103 | N_Task_Body_Stub
8105 -- Other things that can occur in stmt or decl lists
8107 | N_Itype_Reference
8109 -- Use clauses can appear in lists of declarations
8111 | N_Use_Package_Clause
8112 | N_Use_Type_Clause
8114 -- Freeze entity behaves like a declaration or statement
8116 | N_Freeze_Entity
8117 | N_Freeze_Generic_Entity
8119 -- Do not insert here if the item is not a list member (this
8120 -- happens for example with a triggering statement, and the
8121 -- proper approach is to insert before the entire select).
8123 if not Is_List_Member (P) then
8124 null;
8126 -- Do not insert if parent of P is an N_Component_Association
8127 -- node (i.e. we are in the context of an N_Aggregate or
8128 -- N_Extension_Aggregate node. In this case we want to insert
8129 -- before the entire aggregate.
8131 elsif Nkind (Parent (P)) = N_Component_Association then
8132 null;
8134 -- Do not insert if the parent of P is either an N_Variant node
8135 -- or an N_Record_Definition node, meaning in either case that
8136 -- P is a member of a component list, and that therefore the
8137 -- actions should be inserted outside the complete record
8138 -- declaration.
8140 elsif Nkind (Parent (P)) in N_Variant | N_Record_Definition then
8141 null;
8143 -- Do not insert freeze nodes within the loop generated for
8144 -- an aggregate, because they may be elaborated too late for
8145 -- subsequent use in the back end: within a package spec the
8146 -- loop is part of the elaboration procedure and is only
8147 -- elaborated during the second pass.
8149 -- If the loop comes from source, or the entity is local to the
8150 -- loop itself it must remain within.
8152 elsif Nkind (Parent (P)) = N_Loop_Statement
8153 and then not Comes_From_Source (Parent (P))
8154 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
8155 and then
8156 Scope (Entity (First (Ins_Actions))) /= Current_Scope
8157 then
8158 null;
8160 -- Otherwise we can go ahead and do the insertion
8162 elsif P = Wrapped_Node then
8163 Store_Before_Actions_In_Scope (Ins_Actions);
8164 return;
8166 else
8167 Insert_List_Before_And_Analyze (P, Ins_Actions);
8168 return;
8169 end if;
8171 -- the expansion of Task and protected type declarations can
8172 -- create declarations for temporaries which, like other actions
8173 -- are inserted and analyzed before the current declaraation.
8174 -- However, the current scope is the synchronized type, and
8175 -- for unnesting it is critical that the proper scope for these
8176 -- generated entities be the enclosing one.
8178 when N_Task_Type_Declaration
8179 | N_Protected_Type_Declaration =>
8181 Push_Scope (Scope (Current_Scope));
8182 Insert_List_Before_And_Analyze (P, Ins_Actions);
8183 Pop_Scope;
8184 return;
8186 -- A special case, N_Raise_xxx_Error can act either as a statement
8187 -- or a subexpression. We tell the difference by looking at the
8188 -- Etype. It is set to Standard_Void_Type in the statement case.
8190 when N_Raise_xxx_Error =>
8191 if Etype (P) = Standard_Void_Type then
8192 if P = Wrapped_Node then
8193 Store_Before_Actions_In_Scope (Ins_Actions);
8194 else
8195 Insert_List_Before_And_Analyze (P, Ins_Actions);
8196 end if;
8198 return;
8200 -- In the subexpression case, keep climbing
8202 else
8203 null;
8204 end if;
8206 -- If a component association appears within a loop created for
8207 -- an array aggregate, attach the actions to the association so
8208 -- they can be subsequently inserted within the loop. For other
8209 -- component associations insert outside of the aggregate. For
8210 -- an association that will generate a loop, its Loop_Actions
8211 -- attribute is already initialized (see exp_aggr.adb).
8213 -- The list of Loop_Actions can in turn generate additional ones,
8214 -- that are inserted before the associated node. If the associated
8215 -- node is outside the aggregate, the new actions are collected
8216 -- at the end of the Loop_Actions, to respect the order in which
8217 -- they are to be elaborated.
8219 when N_Component_Association
8220 | N_Iterated_Component_Association
8221 | N_Iterated_Element_Association
8223 if Nkind (Parent (P)) in N_Aggregate | N_Delta_Aggregate
8225 -- We must not climb up out of an N_Iterated_xxx_Association
8226 -- because the actions might contain references to the loop
8227 -- parameter, except if we come from the Discrete_Choices of
8228 -- N_Iterated_Component_Association which cannot contain any.
8229 -- But it turns out that setting the Loop_Actions field in
8230 -- the case of an N_Component_Association when the field was
8231 -- not already set can lead to gigi assertion failures that
8232 -- are presumably due to malformed trees, so don't do that.
8234 and then (Nkind (P) /= N_Iterated_Component_Association
8235 or else not Is_List_Member (N)
8236 or else
8237 List_Containing (N) /= Discrete_Choices (P))
8238 and then (Nkind (P) /= N_Component_Association
8239 or else Present (Loop_Actions (P)))
8240 then
8241 if Is_Empty_List (Loop_Actions (P)) then
8242 Set_Loop_Actions (P, Ins_Actions);
8243 Analyze_List (Ins_Actions);
8244 else
8245 declare
8246 Decl : Node_Id;
8248 begin
8249 -- Check whether these actions were generated by a
8250 -- declaration that is part of the Loop_Actions for
8251 -- the component_association.
8253 Decl := Assoc_Node;
8254 while Present (Decl) loop
8255 exit when Parent (Decl) = P
8256 and then Is_List_Member (Decl)
8257 and then
8258 List_Containing (Decl) = Loop_Actions (P);
8259 Decl := Parent (Decl);
8260 end loop;
8262 if Present (Decl) then
8263 Insert_List_Before_And_Analyze
8264 (Decl, Ins_Actions);
8265 else
8266 Insert_List_After_And_Analyze
8267 (Last (Loop_Actions (P)), Ins_Actions);
8268 end if;
8269 end;
8270 end if;
8272 return;
8274 else
8275 null;
8276 end if;
8278 -- Special case: an attribute denoting a procedure call
8280 when N_Attribute_Reference =>
8281 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
8282 if P = Wrapped_Node then
8283 Store_Before_Actions_In_Scope (Ins_Actions);
8284 else
8285 Insert_List_Before_And_Analyze (P, Ins_Actions);
8286 end if;
8288 return;
8290 -- In the subexpression case, keep climbing
8292 else
8293 null;
8294 end if;
8296 -- Special case: a marker
8298 when N_Call_Marker
8299 | N_Variable_Reference_Marker
8301 if Is_List_Member (P) then
8302 Insert_List_Before_And_Analyze (P, Ins_Actions);
8303 return;
8304 end if;
8306 -- A contract node should not belong to the tree
8308 when N_Contract =>
8309 raise Program_Error;
8311 -- For all other node types, keep climbing tree
8313 when N_Abortable_Part
8314 | N_Accept_Alternative
8315 | N_Access_Definition
8316 | N_Access_Function_Definition
8317 | N_Access_Procedure_Definition
8318 | N_Access_To_Object_Definition
8319 | N_Aggregate
8320 | N_Allocator
8321 | N_Aspect_Specification
8322 | N_Case_Expression
8323 | N_Case_Statement_Alternative
8324 | N_Character_Literal
8325 | N_Compilation_Unit
8326 | N_Compilation_Unit_Aux
8327 | N_Component_Clause
8328 | N_Component_Declaration
8329 | N_Component_Definition
8330 | N_Component_List
8331 | N_Constrained_Array_Definition
8332 | N_Decimal_Fixed_Point_Definition
8333 | N_Defining_Character_Literal
8334 | N_Defining_Identifier
8335 | N_Defining_Operator_Symbol
8336 | N_Defining_Program_Unit_Name
8337 | N_Delay_Alternative
8338 | N_Delta_Aggregate
8339 | N_Delta_Constraint
8340 | N_Derived_Type_Definition
8341 | N_Designator
8342 | N_Digits_Constraint
8343 | N_Discriminant_Association
8344 | N_Discriminant_Specification
8345 | N_Empty
8346 | N_Entry_Body_Formal_Part
8347 | N_Entry_Call_Alternative
8348 | N_Entry_Declaration
8349 | N_Entry_Index_Specification
8350 | N_Enumeration_Type_Definition
8351 | N_Error
8352 | N_Exception_Handler
8353 | N_Expanded_Name
8354 | N_Explicit_Dereference
8355 | N_Extension_Aggregate
8356 | N_Floating_Point_Definition
8357 | N_Formal_Decimal_Fixed_Point_Definition
8358 | N_Formal_Derived_Type_Definition
8359 | N_Formal_Discrete_Type_Definition
8360 | N_Formal_Floating_Point_Definition
8361 | N_Formal_Modular_Type_Definition
8362 | N_Formal_Ordinary_Fixed_Point_Definition
8363 | N_Formal_Package_Declaration
8364 | N_Formal_Private_Type_Definition
8365 | N_Formal_Incomplete_Type_Definition
8366 | N_Formal_Signed_Integer_Type_Definition
8367 | N_Function_Call
8368 | N_Function_Specification
8369 | N_Generic_Association
8370 | N_Handled_Sequence_Of_Statements
8371 | N_Identifier
8372 | N_In
8373 | N_Index_Or_Discriminant_Constraint
8374 | N_Indexed_Component
8375 | N_Integer_Literal
8376 | N_Iterator_Specification
8377 | N_Interpolated_String_Literal
8378 | N_Label
8379 | N_Loop_Parameter_Specification
8380 | N_Mod_Clause
8381 | N_Modular_Type_Definition
8382 | N_Not_In
8383 | N_Null
8384 | N_Op_Abs
8385 | N_Op_Add
8386 | N_Op_And
8387 | N_Op_Concat
8388 | N_Op_Divide
8389 | N_Op_Eq
8390 | N_Op_Expon
8391 | N_Op_Ge
8392 | N_Op_Gt
8393 | N_Op_Le
8394 | N_Op_Lt
8395 | N_Op_Minus
8396 | N_Op_Mod
8397 | N_Op_Multiply
8398 | N_Op_Ne
8399 | N_Op_Not
8400 | N_Op_Or
8401 | N_Op_Plus
8402 | N_Op_Rem
8403 | N_Op_Rotate_Left
8404 | N_Op_Rotate_Right
8405 | N_Op_Shift_Left
8406 | N_Op_Shift_Right
8407 | N_Op_Shift_Right_Arithmetic
8408 | N_Op_Subtract
8409 | N_Op_Xor
8410 | N_Operator_Symbol
8411 | N_Ordinary_Fixed_Point_Definition
8412 | N_Others_Choice
8413 | N_Package_Specification
8414 | N_Parameter_Association
8415 | N_Parameter_Specification
8416 | N_Pop_Constraint_Error_Label
8417 | N_Pop_Program_Error_Label
8418 | N_Pop_Storage_Error_Label
8419 | N_Pragma_Argument_Association
8420 | N_Procedure_Specification
8421 | N_Protected_Definition
8422 | N_Push_Constraint_Error_Label
8423 | N_Push_Program_Error_Label
8424 | N_Push_Storage_Error_Label
8425 | N_Qualified_Expression
8426 | N_Quantified_Expression
8427 | N_Raise_Expression
8428 | N_Range
8429 | N_Range_Constraint
8430 | N_Real_Literal
8431 | N_Real_Range_Specification
8432 | N_Record_Definition
8433 | N_Reference
8434 | N_SCIL_Dispatch_Table_Tag_Init
8435 | N_SCIL_Dispatching_Call
8436 | N_SCIL_Membership_Test
8437 | N_Selected_Component
8438 | N_Signed_Integer_Type_Definition
8439 | N_Single_Protected_Declaration
8440 | N_Slice
8441 | N_String_Literal
8442 | N_Subtype_Indication
8443 | N_Subunit
8444 | N_Target_Name
8445 | N_Task_Definition
8446 | N_Terminate_Alternative
8447 | N_Triggering_Alternative
8448 | N_Type_Conversion
8449 | N_Unchecked_Expression
8450 | N_Unchecked_Type_Conversion
8451 | N_Unconstrained_Array_Definition
8452 | N_Unused_At_End
8453 | N_Unused_At_Start
8454 | N_Variant
8455 | N_Variant_Part
8456 | N_Validate_Unchecked_Conversion
8457 | N_With_Clause
8459 null;
8460 end case;
8462 -- If we fall through above tests, keep climbing tree
8464 N := P;
8466 if Nkind (Parent (N)) = N_Subunit then
8468 -- This is the proper body corresponding to a stub. Insertion must
8469 -- be done at the point of the stub, which is in the declarative
8470 -- part of the parent unit.
8472 P := Corresponding_Stub (Parent (N));
8474 else
8475 P := Parent (N);
8476 end if;
8477 end loop;
8478 end Insert_Actions;
8480 -- Version with check(s) suppressed
8482 procedure Insert_Actions
8483 (Assoc_Node : Node_Id;
8484 Ins_Actions : List_Id;
8485 Suppress : Check_Id;
8486 Spec_Expr_OK : Boolean := False)
8488 begin
8489 if Suppress = All_Checks then
8490 declare
8491 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
8492 begin
8493 Scope_Suppress.Suppress := (others => True);
8494 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8495 Scope_Suppress.Suppress := Sva;
8496 end;
8498 else
8499 declare
8500 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
8501 begin
8502 Scope_Suppress.Suppress (Suppress) := True;
8503 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8504 Scope_Suppress.Suppress (Suppress) := Svg;
8505 end;
8506 end if;
8507 end Insert_Actions;
8509 --------------------------
8510 -- Insert_Actions_After --
8511 --------------------------
8513 procedure Insert_Actions_After
8514 (Assoc_Node : Node_Id;
8515 Ins_Actions : List_Id)
8517 begin
8518 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
8519 Store_After_Actions_In_Scope (Ins_Actions);
8520 else
8521 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
8522 end if;
8523 end Insert_Actions_After;
8525 ---------------------------------
8526 -- Insert_Library_Level_Action --
8527 ---------------------------------
8529 procedure Insert_Library_Level_Action (N : Node_Id) is
8530 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8532 begin
8533 Push_Scope (Cunit_Entity (Current_Sem_Unit));
8534 -- And not Main_Unit as previously. If the main unit is a body,
8535 -- the scope needed to analyze the actions is the entity of the
8536 -- corresponding declaration.
8538 if No (Actions (Aux)) then
8539 Set_Actions (Aux, New_List (N));
8540 else
8541 Append (N, Actions (Aux));
8542 end if;
8544 Analyze (N);
8545 Pop_Scope;
8546 end Insert_Library_Level_Action;
8548 ----------------------------------
8549 -- Insert_Library_Level_Actions --
8550 ----------------------------------
8552 procedure Insert_Library_Level_Actions (L : List_Id) is
8553 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8555 begin
8556 if Is_Non_Empty_List (L) then
8557 Push_Scope (Cunit_Entity (Main_Unit));
8558 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
8560 if No (Actions (Aux)) then
8561 Set_Actions (Aux, L);
8562 Analyze_List (L);
8563 else
8564 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
8565 end if;
8567 Pop_Scope;
8568 end if;
8569 end Insert_Library_Level_Actions;
8571 ----------------------
8572 -- Inside_Init_Proc --
8573 ----------------------
8575 function Inside_Init_Proc return Boolean is
8576 begin
8577 return Present (Enclosing_Init_Proc);
8578 end Inside_Init_Proc;
8580 ----------------------
8581 -- Integer_Type_For --
8582 ----------------------
8584 function Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id is
8585 begin
8586 pragma Assert
8587 (Standard_Long_Integer_Size in
8588 Standard_Integer_Size | Standard_Long_Long_Integer_Size);
8589 -- So we don't need to check for Standard_Long_Integer_Size below
8590 pragma Assert (S <= System_Max_Integer_Size);
8592 -- This is the canonical 32-bit type
8594 if S <= Standard_Integer_Size then
8595 if Uns then
8596 return Standard_Unsigned;
8597 else
8598 return Standard_Integer;
8599 end if;
8601 -- This is the canonical 64-bit type
8603 elsif S <= Standard_Long_Long_Integer_Size then
8604 if Uns then
8605 return Standard_Long_Long_Unsigned;
8606 else
8607 return Standard_Long_Long_Integer;
8608 end if;
8610 -- This is the canonical 128-bit type
8612 elsif S <= Standard_Long_Long_Long_Integer_Size then
8613 if Uns then
8614 return Standard_Long_Long_Long_Unsigned;
8615 else
8616 return Standard_Long_Long_Long_Integer;
8617 end if;
8619 else
8620 raise Program_Error;
8621 end if;
8622 end Integer_Type_For;
8624 -------------------------------
8625 -- Is_Captured_Function_Call --
8626 -------------------------------
8628 function Is_Captured_Function_Call (N : Node_Id) return Boolean is
8629 begin
8630 if Nkind (N) = N_Explicit_Dereference
8631 and then Is_Entity_Name (Prefix (N))
8632 and then Ekind (Entity (Prefix (N))) = E_Constant
8633 then
8634 declare
8635 Value : constant Node_Id := Constant_Value (Entity (Prefix (N)));
8637 begin
8638 return Present (Value)
8639 and then Nkind (Value) = N_Reference
8640 and then Nkind (Prefix (Value)) = N_Function_Call;
8641 end;
8643 else
8644 return False;
8645 end if;
8646 end Is_Captured_Function_Call;
8648 ------------------------------
8649 -- Is_Finalizable_Transient --
8650 ------------------------------
8652 function Is_Finalizable_Transient
8653 (Decl : Node_Id;
8654 N : Node_Id) return Boolean
8656 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
8657 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
8659 function Initialized_By_Aliased_BIP_Func_Call
8660 (Trans_Id : Entity_Id) return Boolean;
8661 -- Determine whether transient object Trans_Id is initialized by a
8662 -- build-in-place function call where the BIPalloc parameter either
8663 -- does not exist or is Caller_Allocation, and BIPaccess is not null.
8664 -- This case creates an aliasing between the returned value and the
8665 -- value denoted by BIPaccess.
8667 function Initialized_By_Reference (Trans_Id : Entity_Id) return Boolean;
8668 -- Determine whether transient object Trans_Id is initialized by a
8669 -- reference to another object. This is the only case where we can
8670 -- possibly finalize a transient object through an access value.
8672 function Is_Aliased
8673 (Trans_Id : Entity_Id;
8674 First_Stmt : Node_Id) return Boolean;
8675 -- Determine whether transient object Trans_Id has been renamed or
8676 -- aliased through 'reference in the statement list starting from
8677 -- First_Stmt.
8679 function Is_Indexed_Container
8680 (Trans_Id : Entity_Id;
8681 First_Stmt : Node_Id) return Boolean;
8682 -- Determine whether transient object Trans_Id denotes a container which
8683 -- is in the process of being indexed in the statement list starting
8684 -- from First_Stmt.
8686 function Is_Iterated_Container
8687 (Trans_Id : Entity_Id;
8688 First_Stmt : Node_Id) return Boolean;
8689 -- Determine whether transient object Trans_Id denotes a container which
8690 -- is in the process of being iterated in the statement list starting
8691 -- from First_Stmt.
8693 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean;
8694 -- Return True if N is directly part of a build-in-place return
8695 -- statement.
8697 ------------------------------------------
8698 -- Initialized_By_Aliased_BIP_Func_Call --
8699 ------------------------------------------
8701 function Initialized_By_Aliased_BIP_Func_Call
8702 (Trans_Id : Entity_Id) return Boolean
8704 Call : Node_Id := Expression (Parent (Trans_Id));
8706 begin
8707 -- Build-in-place calls usually appear in 'reference format
8709 if Nkind (Call) = N_Reference then
8710 Call := Prefix (Call);
8711 end if;
8713 Call := Unqual_Conv (Call);
8715 -- We search for a formal with a matching suffix. We can't search
8716 -- for the full name, because of the code at the end of Sem_Ch6.-
8717 -- Create_Extra_Formals, which copies the Extra_Formals over to
8718 -- the Alias of an instance, which will cause the formals to have
8719 -- "incorrect" names. See also Exp_Ch6.Build_In_Place_Formal.
8721 if Is_Build_In_Place_Function_Call (Call) then
8722 declare
8723 Caller_Allocation_Val : constant Uint :=
8724 UI_From_Int (BIP_Allocation_Form'Pos (Caller_Allocation));
8725 Access_Suffix : constant String :=
8726 BIP_Formal_Suffix (BIP_Object_Access);
8727 Alloc_Suffix : constant String :=
8728 BIP_Formal_Suffix (BIP_Alloc_Form);
8730 function Has_Suffix (Name, Suffix : String) return Boolean;
8731 -- Return True if Name has suffix Suffix
8733 ----------------
8734 -- Has_Suffix --
8735 ----------------
8737 function Has_Suffix (Name, Suffix : String) return Boolean is
8738 Len : constant Natural := Suffix'Length;
8740 begin
8741 return Name'Length > Len
8742 and then Name (Name'Last - Len + 1 .. Name'Last) = Suffix;
8743 end Has_Suffix;
8745 Access_OK : Boolean := False;
8746 Alloc_OK : Boolean := True;
8747 Param : Node_Id;
8749 begin
8750 -- Examine all parameter associations of the function call
8752 Param := First (Parameter_Associations (Call));
8754 while Present (Param) loop
8755 if Nkind (Param) = N_Parameter_Association
8756 and then Nkind (Selector_Name (Param)) = N_Identifier
8757 then
8758 declare
8759 Actual : constant Node_Id
8760 := Explicit_Actual_Parameter (Param);
8761 Formal : constant Node_Id
8762 := Selector_Name (Param);
8763 Name : constant String
8764 := Get_Name_String (Chars (Formal));
8766 begin
8767 -- A nonnull BIPaccess has been found
8769 if Has_Suffix (Name, Access_Suffix)
8770 and then Nkind (Actual) /= N_Null
8771 then
8772 Access_OK := True;
8774 -- A BIPalloc has been found
8776 elsif Has_Suffix (Name, Alloc_Suffix)
8777 and then Nkind (Actual) = N_Integer_Literal
8778 then
8779 Alloc_OK := Intval (Actual) = Caller_Allocation_Val;
8780 end if;
8781 end;
8782 end if;
8784 Next (Param);
8785 end loop;
8787 return Access_OK and Alloc_OK;
8788 end;
8789 end if;
8791 return False;
8792 end Initialized_By_Aliased_BIP_Func_Call;
8794 ------------------------------
8795 -- Initialized_By_Reference --
8796 ------------------------------
8798 function Initialized_By_Reference (Trans_Id : Entity_Id) return Boolean
8800 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8802 begin
8803 return Present (Expr) and then Nkind (Expr) = N_Reference;
8804 end Initialized_By_Reference;
8806 ----------------
8807 -- Is_Aliased --
8808 ----------------
8810 function Is_Aliased
8811 (Trans_Id : Entity_Id;
8812 First_Stmt : Node_Id) return Boolean
8814 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
8815 -- Given an object renaming declaration, retrieve the entity within
8816 -- the renamed name, recursively if this entity is itself a renaming.
8817 -- Return Empty if the renamed name contains anything other than a
8818 -- variable or a constant.
8820 -------------------------
8821 -- Find_Renamed_Object --
8822 -------------------------
8824 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
8825 Ren_Obj : Node_Id := Empty;
8827 function Find_Object (N : Node_Id) return Traverse_Result;
8828 -- Try to detect an object which is either a constant or a
8829 -- variable.
8831 -----------------
8832 -- Find_Object --
8833 -----------------
8835 function Find_Object (N : Node_Id) return Traverse_Result is
8836 begin
8837 -- Stop the search once a constant or a variable has been
8838 -- detected.
8840 if Nkind (N) = N_Identifier
8841 and then Present (Entity (N))
8842 and then Ekind (Entity (N)) in E_Constant | E_Variable
8843 then
8844 Ren_Obj := Entity (N);
8845 return Abandon;
8846 end if;
8848 return OK;
8849 end Find_Object;
8851 procedure Search is new Traverse_Proc (Find_Object);
8853 -- Local variables
8855 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
8857 -- Start of processing for Find_Renamed_Object
8859 begin
8860 -- Actions related to dispatching calls may appear as renamings of
8861 -- tags. Do not process this type of renaming because it does not
8862 -- use the actual value of the object.
8864 if not Is_RTE (Typ, RE_Tag_Ptr) then
8865 Search (Name (Ren_Decl));
8866 end if;
8868 -- For renamings generated by Expand_N_Object_Declaration to deal
8869 -- with (class-wide) interface objects, there is an intermediate
8870 -- temporary of an anonymous access type used to hold the result
8871 -- of the displacement of the address of the renamed object.
8873 if Present (Ren_Obj)
8874 and then Ekind (Ren_Obj) = E_Constant
8875 and then Is_Itype (Etype (Ren_Obj))
8876 and then Ekind (Etype (Ren_Obj)) = E_Anonymous_Access_Type
8877 and then
8878 Is_Class_Wide_Type (Directly_Designated_Type (Etype (Ren_Obj)))
8879 and then
8880 Is_Interface (Directly_Designated_Type (Etype (Ren_Obj)))
8881 then
8882 Search (Constant_Value (Ren_Obj));
8883 end if;
8885 -- Recurse if Ren_Obj is itself a renaming
8887 if Present (Ren_Obj)
8888 and then Ekind (Ren_Obj) in E_Constant | E_Variable
8889 and then Present (Renamed_Object (Ren_Obj))
8890 then
8891 return Find_Renamed_Object (Declaration_Node (Ren_Obj));
8892 else
8893 return Ren_Obj;
8894 end if;
8895 end Find_Renamed_Object;
8897 -- Local variables
8899 Expr : Node_Id;
8900 Ren_Obj : Entity_Id;
8901 Stmt : Node_Id;
8903 -- Start of processing for Is_Aliased
8905 begin
8906 -- Examine the statements following the controlled object and look
8907 -- for various forms of aliasing.
8909 Stmt := First_Stmt;
8910 while Present (Stmt) loop
8911 -- Transient objects initialized by a reference are finalized
8912 -- (see Initialized_By_Reference above), so we must make sure
8913 -- not to finalize the referenced object twice. And we cannot
8914 -- finalize it at all if it is referenced by the nontransient
8915 -- object serviced by the transient scope.
8917 if Nkind (Stmt) = N_Object_Declaration then
8918 Expr := Expression (Stmt);
8920 -- Aliasing of the form:
8921 -- Obj : ... := Trans_Id'reference;
8923 if Present (Expr)
8924 and then Nkind (Expr) = N_Reference
8925 and then Is_Entity_Name (Prefix (Expr))
8926 and then Entity (Prefix (Expr)) = Trans_Id
8927 then
8928 return True;
8929 end if;
8931 -- (Transient) renamings are never finalized so we need not bother
8932 -- about finalizing transient renamed objects twice. Therefore, we
8933 -- we only need to look at the nontransient object serviced by the
8934 -- transient scope, if it exists and is declared as a renaming.
8936 elsif Nkind (Stmt) = N_Object_Renaming_Declaration
8937 and then Stmt = N
8938 then
8939 Ren_Obj := Find_Renamed_Object (Stmt);
8941 -- Aliasing of the form:
8942 -- Obj : ... renames ... Trans_Id ...;
8944 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8945 return True;
8946 end if;
8947 end if;
8949 Next (Stmt);
8950 end loop;
8952 return False;
8953 end Is_Aliased;
8955 --------------------------
8956 -- Is_Indexed_Container --
8957 --------------------------
8959 function Is_Indexed_Container
8960 (Trans_Id : Entity_Id;
8961 First_Stmt : Node_Id) return Boolean
8963 Aspect : Node_Id;
8964 Call : Node_Id;
8965 Index : Entity_Id;
8966 Param : Node_Id;
8967 Stmt : Node_Id;
8968 Typ : Entity_Id;
8970 begin
8971 -- It is not possible to iterate over containers in non-Ada 2012 code
8973 if Ada_Version < Ada_2012 then
8974 return False;
8975 end if;
8977 Typ := Etype (Trans_Id);
8979 -- Handle access type created for the reference below
8981 if Is_Access_Type (Typ) then
8982 Typ := Designated_Type (Typ);
8983 end if;
8985 -- Look for aspect Constant_Indexing. It may be part of a type
8986 -- declaration for a container, or inherited from a base type
8987 -- or parent type.
8989 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Constant_Indexing);
8991 if Present (Aspect) then
8992 Index := Entity (Aspect);
8994 -- Examine the statements following the container object and
8995 -- look for a call to the default indexing routine where the
8996 -- first parameter is the transient. Such a call appears as:
8998 -- It : Access_To_Constant_Reference_Type :=
8999 -- Constant_Indexing (Trans_Id.all, ...)'reference;
9001 Stmt := First_Stmt;
9002 while Present (Stmt) loop
9004 -- Detect an object declaration which is initialized by a
9005 -- controlled function call.
9007 if Nkind (Stmt) = N_Object_Declaration
9008 and then Present (Expression (Stmt))
9009 and then Nkind (Expression (Stmt)) = N_Reference
9010 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
9011 then
9012 Call := Prefix (Expression (Stmt));
9014 -- The call must invoke the default indexing routine of
9015 -- the container and the transient object must appear as
9016 -- the first actual parameter. Skip any calls whose names
9017 -- are not entities.
9019 if Is_Entity_Name (Name (Call))
9020 and then Entity (Name (Call)) = Index
9021 and then Present (Parameter_Associations (Call))
9022 then
9023 Param := First (Parameter_Associations (Call));
9025 if Nkind (Param) = N_Explicit_Dereference
9026 and then Entity (Prefix (Param)) = Trans_Id
9027 then
9028 return True;
9029 end if;
9030 end if;
9031 end if;
9033 Next (Stmt);
9034 end loop;
9035 end if;
9037 return False;
9038 end Is_Indexed_Container;
9040 ---------------------------
9041 -- Is_Iterated_Container --
9042 ---------------------------
9044 function Is_Iterated_Container
9045 (Trans_Id : Entity_Id;
9046 First_Stmt : Node_Id) return Boolean
9048 Aspect : Node_Id;
9049 Call : Node_Id;
9050 Iter : Entity_Id;
9051 Param : Node_Id;
9052 Stmt : Node_Id;
9053 Typ : Entity_Id;
9055 begin
9056 -- It is not possible to iterate over containers in non-Ada 2012 code
9058 if Ada_Version < Ada_2012 then
9059 return False;
9060 end if;
9062 Typ := Etype (Trans_Id);
9064 -- Handle access type created for the reference below
9066 if Is_Access_Type (Typ) then
9067 Typ := Designated_Type (Typ);
9068 end if;
9070 -- Look for aspect Default_Iterator. It may be part of a type
9071 -- declaration for a container, or inherited from a base type
9072 -- or parent type.
9074 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
9076 if Present (Aspect) then
9077 Iter := Entity (Aspect);
9079 -- Examine the statements following the container object and
9080 -- look for a call to the default iterate routine where the
9081 -- first parameter is the transient. Such a call appears as:
9083 -- It : Access_To_CW_Iterator :=
9084 -- Iterate (Trans_Id.all, ...)'reference;
9086 Stmt := First_Stmt;
9087 while Present (Stmt) loop
9089 -- Detect an object declaration which is initialized by a
9090 -- controlled function call.
9092 if Nkind (Stmt) = N_Object_Declaration
9093 and then Present (Expression (Stmt))
9094 and then Nkind (Expression (Stmt)) = N_Reference
9095 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
9096 then
9097 Call := Prefix (Expression (Stmt));
9099 -- The call must invoke the default iterate routine of
9100 -- the container and the transient object must appear as
9101 -- the first actual parameter. Skip any calls whose names
9102 -- are not entities.
9104 if Is_Entity_Name (Name (Call))
9105 and then Entity (Name (Call)) = Iter
9106 and then Present (Parameter_Associations (Call))
9107 then
9108 Param := First (Parameter_Associations (Call));
9110 if Nkind (Param) = N_Explicit_Dereference
9111 and then Entity (Prefix (Param)) = Trans_Id
9112 then
9113 return True;
9114 end if;
9115 end if;
9116 end if;
9118 Next (Stmt);
9119 end loop;
9120 end if;
9122 return False;
9123 end Is_Iterated_Container;
9125 -------------------------------------
9126 -- Is_Part_Of_BIP_Return_Statement --
9127 -------------------------------------
9129 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean is
9130 Subp : constant Entity_Id := Current_Subprogram;
9131 Context : Node_Id;
9132 begin
9133 -- First check if N is part of a BIP function
9135 if No (Subp)
9136 or else not Is_Build_In_Place_Function (Subp)
9137 then
9138 return False;
9139 end if;
9141 -- Then check whether N is a complete part of a return statement
9142 -- Should we consider other node kinds to go up the tree???
9144 Context := N;
9145 loop
9146 case Nkind (Context) is
9147 when N_Expression_With_Actions => Context := Parent (Context);
9148 when N_Simple_Return_Statement => return True;
9149 when others => return False;
9150 end case;
9151 end loop;
9152 end Is_Part_Of_BIP_Return_Statement;
9154 -- Local variables
9156 Desig : Entity_Id := Obj_Typ;
9158 -- Start of processing for Is_Finalizable_Transient
9160 begin
9161 -- Handle access types
9163 if Is_Access_Type (Desig) then
9164 Desig := Available_View (Designated_Type (Desig));
9165 end if;
9167 return
9168 Ekind (Obj_Id) in E_Constant | E_Variable
9169 and then Needs_Finalization (Desig)
9170 and then Nkind (N) /= N_Simple_Return_Statement
9171 and then not Is_Part_Of_BIP_Return_Statement (N)
9173 -- Do not consider a transient object that was already processed
9175 and then not Is_Finalized_Transient (Obj_Id)
9177 -- Do not consider renamed or 'reference-d transient objects because
9178 -- the act of renaming extends the object's lifetime.
9180 and then not Is_Aliased (Obj_Id, Decl)
9182 -- If the transient object is of an access type, check that it is
9183 -- initialized by a reference to another object.
9185 and then (not Is_Access_Type (Obj_Typ)
9186 or else Initialized_By_Reference (Obj_Id))
9188 -- Do not consider transient objects which act as indirect aliases
9189 -- of build-in-place function results.
9191 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
9193 -- Do not consider iterators because those are treated as normal
9194 -- controlled objects and are processed by the usual finalization
9195 -- machinery. This avoids the double finalization of an iterator.
9197 and then not Is_Iterator (Desig)
9199 -- Do not consider containers in the context of iterator loops. Such
9200 -- transient objects must exist for as long as the loop is around,
9201 -- otherwise any operation carried out by the iterator will fail.
9203 and then not Is_Iterated_Container (Obj_Id, Decl)
9205 -- Likewise for indexed containers in the context of iterator loops
9207 and then not Is_Indexed_Container (Obj_Id, Decl);
9208 end Is_Finalizable_Transient;
9210 ---------------------------------
9211 -- Is_Fully_Repped_Tagged_Type --
9212 ---------------------------------
9214 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
9215 U : constant Entity_Id := Underlying_Type (T);
9216 Comp : Entity_Id;
9218 begin
9219 if No (U) or else not Is_Tagged_Type (U) then
9220 return False;
9221 elsif Has_Discriminants (U) then
9222 return False;
9223 elsif not Has_Specified_Layout (U) then
9224 return False;
9225 end if;
9227 -- Here we have a tagged type, see if it has any component (other than
9228 -- tag and parent) with no component_clause. If so, we return False.
9230 Comp := First_Component (U);
9231 while Present (Comp) loop
9232 if not Is_Tag (Comp)
9233 and then Chars (Comp) /= Name_uParent
9234 and then No (Component_Clause (Comp))
9235 then
9236 return False;
9237 else
9238 Next_Component (Comp);
9239 end if;
9240 end loop;
9242 -- All components have clauses
9244 return True;
9245 end Is_Fully_Repped_Tagged_Type;
9247 ----------------------------------
9248 -- Is_Library_Level_Tagged_Type --
9249 ----------------------------------
9251 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
9252 begin
9253 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
9254 end Is_Library_Level_Tagged_Type;
9256 --------------------------
9257 -- Is_Non_BIP_Func_Call --
9258 --------------------------
9260 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
9261 begin
9262 -- The expected call is of the format
9264 -- Func_Call'reference
9266 return
9267 Nkind (Expr) = N_Reference
9268 and then Nkind (Prefix (Expr)) = N_Function_Call
9269 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
9270 end Is_Non_BIP_Func_Call;
9272 ----------------------------------
9273 -- Is_Possibly_Unaligned_Object --
9274 ----------------------------------
9276 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
9277 T : constant Entity_Id := Etype (N);
9279 begin
9280 -- If renamed object, apply test to underlying object
9282 if Is_Entity_Name (N)
9283 and then Is_Object (Entity (N))
9284 and then Present (Renamed_Object (Entity (N)))
9285 then
9286 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
9287 end if;
9289 -- Tagged and controlled types and aliased types are always aligned, as
9290 -- are concurrent types.
9292 if Is_Aliased (T)
9293 or else Has_Controlled_Component (T)
9294 or else Is_Concurrent_Type (T)
9295 or else Is_Tagged_Type (T)
9296 or else Is_Controlled (T)
9297 then
9298 return False;
9299 end if;
9301 -- If this is an element of a packed array, may be unaligned
9303 if Is_Ref_To_Bit_Packed_Array (N) then
9304 return True;
9305 end if;
9307 -- Case of indexed component reference: test whether prefix is unaligned
9309 if Nkind (N) = N_Indexed_Component then
9310 return Is_Possibly_Unaligned_Object (Prefix (N));
9312 -- Case of selected component reference
9314 elsif Nkind (N) = N_Selected_Component then
9315 declare
9316 P : constant Node_Id := Prefix (N);
9317 C : constant Entity_Id := Entity (Selector_Name (N));
9318 M : Nat;
9319 S : Nat;
9321 begin
9322 -- If component reference is for an array with nonstatic bounds,
9323 -- then it is always aligned: we can only process unaligned arrays
9324 -- with static bounds (more precisely compile time known bounds).
9326 if Is_Array_Type (T)
9327 and then not Compile_Time_Known_Bounds (T)
9328 then
9329 return False;
9330 end if;
9332 -- If component is aliased, it is definitely properly aligned
9334 if Is_Aliased (C) then
9335 return False;
9336 end if;
9338 -- If component is for a type implemented as a scalar, and the
9339 -- record is packed, and the component is other than the first
9340 -- component of the record, then the component may be unaligned.
9342 if Is_Packed (Etype (P))
9343 and then Represented_As_Scalar (Etype (C))
9344 and then First_Entity (Scope (C)) /= C
9345 then
9346 return True;
9347 end if;
9349 -- Compute maximum possible alignment for T
9351 -- If alignment is known, then that settles things
9353 if Known_Alignment (T) then
9354 M := UI_To_Int (Alignment (T));
9356 -- If alignment is not known, tentatively set max alignment
9358 else
9359 M := Ttypes.Maximum_Alignment;
9361 -- We can reduce this if the Esize is known since the default
9362 -- alignment will never be more than the smallest power of 2
9363 -- that does not exceed this Esize value.
9365 if Known_Esize (T) then
9366 S := UI_To_Int (Esize (T));
9368 while (M / 2) >= S loop
9369 M := M / 2;
9370 end loop;
9371 end if;
9372 end if;
9374 -- Case of component clause present which may specify an
9375 -- unaligned position.
9377 if Present (Component_Clause (C)) then
9379 -- Otherwise we can do a test to make sure that the actual
9380 -- start position in the record, and the length, are both
9381 -- consistent with the required alignment. If not, we know
9382 -- that we are unaligned.
9384 declare
9385 Align_In_Bits : constant Nat := M * System_Storage_Unit;
9386 Comp : Entity_Id;
9388 begin
9389 Comp := C;
9391 -- For a component inherited in a record extension, the
9392 -- clause is inherited but position and size are not set.
9394 if Is_Base_Type (Etype (P))
9395 and then Is_Tagged_Type (Etype (P))
9396 and then Present (Original_Record_Component (Comp))
9397 then
9398 Comp := Original_Record_Component (Comp);
9399 end if;
9401 if Component_Bit_Offset (Comp) mod Align_In_Bits /= 0
9402 or else Esize (Comp) mod Align_In_Bits /= 0
9403 then
9404 return True;
9405 end if;
9406 end;
9407 end if;
9409 -- Otherwise, for a component reference, test prefix
9411 return Is_Possibly_Unaligned_Object (P);
9412 end;
9414 -- If not a component reference, must be aligned
9416 else
9417 return False;
9418 end if;
9419 end Is_Possibly_Unaligned_Object;
9421 ---------------------------------
9422 -- Is_Possibly_Unaligned_Slice --
9423 ---------------------------------
9425 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
9426 begin
9427 -- Go to renamed object
9429 if Is_Entity_Name (N)
9430 and then Is_Object (Entity (N))
9431 and then Present (Renamed_Object (Entity (N)))
9432 then
9433 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
9434 end if;
9436 -- The reference must be a slice
9438 if Nkind (N) /= N_Slice then
9439 return False;
9440 end if;
9442 -- If it is a slice, then look at the array type being sliced
9444 declare
9445 Sarr : constant Node_Id := Prefix (N);
9446 -- Prefix of the slice, i.e. the array being sliced
9448 Styp : constant Entity_Id := Etype (Prefix (N));
9449 -- Type of the array being sliced
9451 Pref : Node_Id;
9452 Ptyp : Entity_Id;
9454 begin
9455 -- The problems arise if the array object that is being sliced
9456 -- is a component of a record or array, and we cannot guarantee
9457 -- the alignment of the array within its containing object.
9459 -- To investigate this, we look at successive prefixes to see
9460 -- if we have a worrisome indexed or selected component.
9462 Pref := Sarr;
9463 loop
9464 -- Case of array is part of an indexed component reference
9466 if Nkind (Pref) = N_Indexed_Component then
9467 Ptyp := Etype (Prefix (Pref));
9469 -- The only problematic case is when the array is packed, in
9470 -- which case we really know nothing about the alignment of
9471 -- individual components.
9473 if Is_Bit_Packed_Array (Ptyp) then
9474 return True;
9475 end if;
9477 -- Case of array is part of a selected component reference
9479 elsif Nkind (Pref) = N_Selected_Component then
9480 Ptyp := Etype (Prefix (Pref));
9482 -- We are definitely in trouble if the record in question
9483 -- has an alignment, and either we know this alignment is
9484 -- inconsistent with the alignment of the slice, or we don't
9485 -- know what the alignment of the slice should be. But this
9486 -- really matters only if the target has strict alignment.
9488 if Target_Strict_Alignment
9489 and then Known_Alignment (Ptyp)
9490 and then (not Known_Alignment (Styp)
9491 or else Alignment (Styp) > Alignment (Ptyp))
9492 then
9493 return True;
9494 end if;
9496 -- We are in potential trouble if the record type is packed.
9497 -- We could special case when we know that the array is the
9498 -- first component, but that's not such a simple case ???
9500 if Is_Packed (Ptyp) then
9501 return True;
9502 end if;
9504 -- We are in trouble if there is a component clause, and
9505 -- either we do not know the alignment of the slice, or
9506 -- the alignment of the slice is inconsistent with the
9507 -- bit position specified by the component clause.
9509 declare
9510 Field : constant Entity_Id := Entity (Selector_Name (Pref));
9511 begin
9512 if Present (Component_Clause (Field))
9513 and then
9514 (not Known_Alignment (Styp)
9515 or else
9516 (Component_Bit_Offset (Field) mod
9517 (System_Storage_Unit * Alignment (Styp))) /= 0)
9518 then
9519 return True;
9520 end if;
9521 end;
9523 -- For cases other than selected or indexed components we know we
9524 -- are OK, since no issues arise over alignment.
9526 else
9527 return False;
9528 end if;
9530 -- We processed an indexed component or selected component
9531 -- reference that looked safe, so keep checking prefixes.
9533 Pref := Prefix (Pref);
9534 end loop;
9535 end;
9536 end Is_Possibly_Unaligned_Slice;
9538 -------------------------------
9539 -- Is_Related_To_Func_Return --
9540 -------------------------------
9542 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
9543 Expr : constant Node_Id := Related_Expression (Id);
9544 begin
9545 -- In the case of a function with a class-wide result that returns
9546 -- a call to a function with a specific result, we introduce a
9547 -- type conversion for the return expression. We do not want that
9548 -- type conversion to influence the result of this function.
9550 return
9551 Present (Expr)
9552 and then Nkind (Unqual_Conv (Expr)) = N_Explicit_Dereference
9553 and then (Nkind (Parent (Expr)) = N_Simple_Return_Statement
9554 or else
9555 (Nkind (Parent (Expr)) in N_Object_Declaration
9556 | N_Object_Renaming_Declaration
9557 and then
9558 Is_Return_Object (Defining_Entity (Parent (Expr)))));
9559 end Is_Related_To_Func_Return;
9561 --------------------------------
9562 -- Is_Ref_To_Bit_Packed_Array --
9563 --------------------------------
9565 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
9566 Result : Boolean;
9567 Expr : Node_Id;
9569 begin
9570 if Is_Entity_Name (N)
9571 and then Is_Object (Entity (N))
9572 and then Present (Renamed_Object (Entity (N)))
9573 then
9574 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
9575 end if;
9577 if Nkind (N) in N_Indexed_Component | N_Selected_Component then
9578 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
9579 Result := True;
9580 else
9581 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
9582 end if;
9584 if Result and then Nkind (N) = N_Indexed_Component then
9585 Expr := First (Expressions (N));
9586 while Present (Expr) loop
9587 Force_Evaluation (Expr);
9588 Next (Expr);
9589 end loop;
9590 end if;
9592 return Result;
9594 else
9595 return False;
9596 end if;
9597 end Is_Ref_To_Bit_Packed_Array;
9599 --------------------------------
9600 -- Is_Ref_To_Bit_Packed_Slice --
9601 --------------------------------
9603 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
9604 begin
9605 if Nkind (N) = N_Type_Conversion then
9606 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
9608 elsif Is_Entity_Name (N)
9609 and then Is_Object (Entity (N))
9610 and then Present (Renamed_Object (Entity (N)))
9611 then
9612 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
9614 elsif Nkind (N) = N_Slice
9615 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
9616 then
9617 return True;
9619 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9620 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
9622 else
9623 return False;
9624 end if;
9625 end Is_Ref_To_Bit_Packed_Slice;
9627 -----------------------
9628 -- Is_Renamed_Object --
9629 -----------------------
9631 function Is_Renamed_Object (N : Node_Id) return Boolean is
9632 Pnod : constant Node_Id := Parent (N);
9633 Kind : constant Node_Kind := Nkind (Pnod);
9634 begin
9635 if Kind = N_Object_Renaming_Declaration then
9636 return True;
9637 elsif Kind in N_Indexed_Component | N_Selected_Component then
9638 return Is_Renamed_Object (Pnod);
9639 else
9640 return False;
9641 end if;
9642 end Is_Renamed_Object;
9644 --------------------------------------
9645 -- Is_Secondary_Stack_BIP_Func_Call --
9646 --------------------------------------
9648 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
9649 Actual : Node_Id;
9650 Call : Node_Id := Expr;
9651 Formal : Node_Id;
9652 Param : Node_Id;
9654 begin
9655 -- Build-in-place calls usually appear in 'reference format. Note that
9656 -- the accessibility check machinery may add an extra 'reference due to
9657 -- side-effect removal.
9659 while Nkind (Call) = N_Reference loop
9660 Call := Prefix (Call);
9661 end loop;
9663 Call := Unqual_Conv (Call);
9665 if Is_Build_In_Place_Function_Call (Call) then
9667 -- Examine all parameter associations of the function call
9669 Param := First (Parameter_Associations (Call));
9670 while Present (Param) loop
9671 if Nkind (Param) = N_Parameter_Association then
9672 Formal := Selector_Name (Param);
9673 Actual := Explicit_Actual_Parameter (Param);
9675 -- A match for BIPalloc => 2 has been found
9677 if Is_Build_In_Place_Entity (Formal)
9678 and then BIP_Suffix_Kind (Formal) = BIP_Alloc_Form
9679 and then Nkind (Actual) = N_Integer_Literal
9680 and then Intval (Actual) = Uint_2
9681 then
9682 return True;
9683 end if;
9684 end if;
9686 Next (Param);
9687 end loop;
9688 end if;
9690 return False;
9691 end Is_Secondary_Stack_BIP_Func_Call;
9693 ------------------------------
9694 -- Is_Secondary_Stack_Thunk --
9695 ------------------------------
9697 function Is_Secondary_Stack_Thunk (Id : Entity_Id) return Boolean is
9698 begin
9699 return Ekind (Id) = E_Function
9700 and then Is_Thunk (Id)
9701 and then Has_Controlling_Result (Id);
9702 end Is_Secondary_Stack_Thunk;
9704 ----------------------------
9705 -- Is_Statically_Disabled --
9706 ----------------------------
9708 function Is_Statically_Disabled
9709 (N : Node_Id;
9710 Value : Boolean;
9711 Include_Valid : Boolean)
9712 return Boolean
9714 function Is_Discrete_Literal (N : Node_Id) return Boolean;
9715 -- Returns whether N is an integer, character or enumeration literal
9717 -------------------------
9718 -- Is_Discrete_Literal --
9719 -------------------------
9721 function Is_Discrete_Literal (N : Node_Id) return Boolean is
9722 (Nkind (N) in N_Integer_Literal | N_Character_Literal
9723 or else (Nkind (N) in N_Identifier | N_Expanded_Name
9724 and then Ekind (Entity (N)) = E_Enumeration_Literal));
9726 Expr_N : constant Node_Id :=
9727 (if Is_Static_Expression (N)
9728 and then Entity (N) in Standard_True | Standard_False
9729 and then Is_Rewrite_Substitution (N)
9730 then Original_Node (N)
9731 else N);
9733 -- Start of processing for Is_Statically_Disabled
9735 begin
9736 -- A "statically disabled" condition which evaluates to Value is either:
9738 case Nkind (Expr_N) is
9740 -- an AND or AND THEN operator when:
9741 -- - Value is True and both operands are statically disabled
9742 -- conditions evaluated to True.
9743 -- - Value is False and at least one operand is a statically disabled
9744 -- condition evaluated to False.
9746 when N_Op_And | N_And_Then =>
9747 return
9748 (if Value then
9749 (Is_Statically_Disabled
9750 (Left_Opnd (Expr_N), Value, Include_Valid)
9751 and then Is_Statically_Disabled
9752 (Right_Opnd (Expr_N), Value, Include_Valid))
9753 else
9754 (Is_Statically_Disabled
9755 (Left_Opnd (Expr_N), Value, Include_Valid)
9756 or else Is_Statically_Disabled
9757 (Right_Opnd (Expr_N), Value, Include_Valid)));
9759 -- an OR or OR ELSE operator when:
9760 -- - Value is True and at least one operand is a statically disabled
9761 -- condition evaluated to True.
9762 -- - Value is False and both operands are statically disabled
9763 -- conditions evaluated to False.
9765 when N_Op_Or | N_Or_Else =>
9766 return
9767 (if Value then
9768 (Is_Statically_Disabled
9769 (Left_Opnd (Expr_N), Value, Include_Valid)
9770 or else Is_Statically_Disabled
9771 (Right_Opnd (Expr_N), Value, Include_Valid))
9772 else
9773 (Is_Statically_Disabled
9774 (Left_Opnd (Expr_N), Value, Include_Valid)
9775 and then Is_Statically_Disabled
9776 (Right_Opnd (Expr_N), Value, Include_Valid)));
9778 -- a NOT operator when the right operand is a statically disabled
9779 -- condition evaluated to the negation of Value.
9781 when N_Op_Not =>
9782 return Is_Statically_Disabled
9783 (Right_Opnd (Expr_N), not Value, Include_Valid);
9785 -- a static constant when it is of a boolean type with aspect
9786 -- Warnings Off.
9788 when N_Identifier | N_Expanded_Name =>
9789 return Is_Static_Expression (Expr_N)
9790 and then Value = Is_True (Expr_Value (Expr_N))
9791 and then Ekind (Entity (Expr_N)) = E_Constant
9792 and then Has_Warnings_Off (Entity (Expr_N));
9794 -- a relational_operator where one operand is a static constant with
9795 -- aspect Warnings Off and the other operand is a literal of the
9796 -- corresponding type.
9798 when N_Op_Compare =>
9799 declare
9800 Left : constant Node_Id := Left_Opnd (Expr_N);
9801 Right : constant Node_Id := Right_Opnd (Expr_N);
9802 begin
9803 return
9804 Is_Static_Expression (N)
9805 and then Value = Is_True (Expr_Value (N))
9806 and then
9807 ((Is_Discrete_Literal (Right)
9808 and then Nkind (Left) in N_Identifier
9809 | N_Expanded_Name
9810 and then Ekind (Entity (Left)) = E_Constant
9811 and then Has_Warnings_Off (Entity (Left)))
9812 or else
9813 (Is_Discrete_Literal (Left)
9814 and then Nkind (Right) in N_Identifier
9815 | N_Expanded_Name
9816 and then Ekind (Entity (Right)) = E_Constant
9817 and then Has_Warnings_Off (Entity (Right))));
9818 end;
9820 -- a reference to 'Valid or 'Valid_Scalar if Include_Valid is True
9822 when N_Attribute_Reference =>
9823 return Include_Valid
9824 and then Get_Attribute_Id (Attribute_Name (Expr_N)) in
9825 Attribute_Valid | Attribute_Valid_Scalars
9826 and then Value;
9828 when others =>
9829 return False;
9830 end case;
9831 end Is_Statically_Disabled;
9833 --------------------------------
9834 -- Is_Uninitialized_Aggregate --
9835 --------------------------------
9837 function Is_Uninitialized_Aggregate
9838 (Exp : Node_Id;
9839 T : Entity_Id) return Boolean
9841 Comp : Node_Id;
9842 Comp_Type : Entity_Id;
9843 Typ : Entity_Id;
9845 begin
9846 if Nkind (Exp) /= N_Aggregate then
9847 return False;
9848 end if;
9850 Preanalyze_And_Resolve (Exp, T);
9851 Typ := Etype (Exp);
9853 if No (Typ)
9854 or else Ekind (Typ) /= E_Array_Subtype
9855 or else Present (Expressions (Exp))
9856 or else No (Component_Associations (Exp))
9857 then
9858 return False;
9859 else
9860 Comp_Type := Component_Type (Typ);
9861 Comp := First (Component_Associations (Exp));
9863 if not Box_Present (Comp)
9864 or else Present (Next (Comp))
9865 then
9866 return False;
9867 end if;
9869 return Is_Scalar_Type (Comp_Type)
9870 and then No (Default_Aspect_Component_Value (Typ));
9871 end if;
9872 end Is_Uninitialized_Aggregate;
9874 ----------------------------
9875 -- Is_Untagged_Derivation --
9876 ----------------------------
9878 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
9879 begin
9880 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
9881 or else
9882 (Is_Private_Type (T) and then Present (Full_View (T))
9883 and then not Is_Tagged_Type (Full_View (T))
9884 and then Is_Derived_Type (Full_View (T))
9885 and then Etype (Full_View (T)) /= T);
9886 end Is_Untagged_Derivation;
9888 ------------------------------------
9889 -- Is_Untagged_Private_Derivation --
9890 ------------------------------------
9892 function Is_Untagged_Private_Derivation
9893 (Priv_Typ : Entity_Id;
9894 Full_Typ : Entity_Id) return Boolean
9896 begin
9897 return
9898 Present (Priv_Typ)
9899 and then Is_Untagged_Derivation (Priv_Typ)
9900 and then Is_Private_Type (Etype (Priv_Typ))
9901 and then Present (Full_Typ)
9902 and then Is_Itype (Full_Typ);
9903 end Is_Untagged_Private_Derivation;
9905 ------------------------------
9906 -- Is_Verifiable_DIC_Pragma --
9907 ------------------------------
9909 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
9910 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9912 begin
9913 -- To qualify as verifiable, a DIC pragma must have a non-null argument
9915 return
9916 Present (Args)
9918 -- If there are args, but the first arg is Empty, then treat the
9919 -- pragma the same as having no args (there may be a second arg that
9920 -- is an implicitly added type arg, and Empty is a placeholder).
9922 and then Present (Get_Pragma_Arg (First (Args)))
9924 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
9925 end Is_Verifiable_DIC_Pragma;
9927 ---------------------------
9928 -- Is_Volatile_Reference --
9929 ---------------------------
9931 function Is_Volatile_Reference (N : Node_Id) return Boolean is
9932 begin
9933 -- Only source references are to be treated as volatile, internally
9934 -- generated stuff cannot have volatile external effects.
9936 if not Comes_From_Source (N) then
9937 return False;
9939 -- Never true for reference to a type
9941 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9942 return False;
9944 -- Never true for a compile time known constant
9946 elsif Compile_Time_Known_Value (N) then
9947 return False;
9949 -- True if object reference with volatile type
9951 elsif Is_Volatile_Object_Ref (N) then
9952 return True;
9954 -- True if reference to volatile entity
9956 elsif Is_Entity_Name (N) then
9957 return Treat_As_Volatile (Entity (N));
9959 -- True for slice of volatile array
9961 elsif Nkind (N) = N_Slice then
9962 return Is_Volatile_Reference (Prefix (N));
9964 -- True if volatile component
9966 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9967 if (Is_Entity_Name (Prefix (N))
9968 and then Has_Volatile_Components (Entity (Prefix (N))))
9969 or else (Present (Etype (Prefix (N)))
9970 and then Has_Volatile_Components (Etype (Prefix (N))))
9971 then
9972 return True;
9973 else
9974 return Is_Volatile_Reference (Prefix (N));
9975 end if;
9977 -- Otherwise false
9979 else
9980 return False;
9981 end if;
9982 end Is_Volatile_Reference;
9984 --------------------
9985 -- Kill_Dead_Code --
9986 --------------------
9988 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
9989 W : Boolean := Warn;
9990 -- Set False if warnings suppressed
9992 begin
9993 if Present (N) then
9994 Remove_Warning_Messages (N);
9996 -- Update the internal structures of the ABE mechanism in case the
9997 -- dead node is an elaboration scenario.
9999 Kill_Elaboration_Scenario (N);
10001 -- Generate warning if appropriate
10003 if W then
10005 -- We suppress the warning if this code is under control of an
10006 -- if/case statement and either
10007 -- a) we are in an instance and the condition/selector
10008 -- has a statically known value; or
10009 -- b) the selector of a case statement is a simple identifier
10010 -- and warnings off is set for this identifier; or
10011 -- c) the condition of an if statement is a "statically
10012 -- disabled" condition which evaluates to False as described
10013 -- in section 7.3.2 of SPARK User's Guide.
10014 -- Dead code is common and reasonable in instances, so we don't
10015 -- want a warning in that case.
10017 declare
10018 C : Node_Id := Empty;
10019 begin
10020 if Nkind (Parent (N)) = N_If_Statement then
10021 C := Condition (Parent (N));
10023 if Is_Statically_Disabled
10024 (C, Value => False, Include_Valid => False)
10025 then
10026 W := False;
10027 end if;
10029 elsif Nkind (Parent (N)) = N_Case_Statement_Alternative then
10030 C := Expression (Parent (Parent (N)));
10032 if Nkind (C) = N_Identifier
10033 and then Present (Entity (C))
10034 and then Has_Warnings_Off (Entity (C))
10035 then
10036 W := False;
10037 end if;
10038 end if;
10040 if Present (C)
10041 and then (In_Instance and Compile_Time_Known_Value (C))
10042 then
10043 W := False;
10044 end if;
10045 end;
10047 -- Generate warning if not suppressed
10049 if W then
10050 Error_Msg_F
10051 ("?t?this code can never be executed and has been deleted!",
10053 end if;
10054 end if;
10056 -- Recurse into block statements and bodies to process declarations
10057 -- and statements.
10059 if Nkind (N) = N_Block_Statement
10060 or else Nkind (N) = N_Subprogram_Body
10061 or else Nkind (N) = N_Package_Body
10062 then
10063 Kill_Dead_Code (Declarations (N), False);
10064 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
10066 if Nkind (N) = N_Subprogram_Body then
10067 Set_Is_Eliminated (Defining_Entity (N));
10068 end if;
10070 elsif Nkind (N) = N_Package_Declaration then
10071 Kill_Dead_Code (Visible_Declarations (Specification (N)));
10072 Kill_Dead_Code (Private_Declarations (Specification (N)));
10074 -- ??? After this point, Delete_Tree has been called on all
10075 -- declarations in Specification (N), so references to entities
10076 -- therein look suspicious.
10078 declare
10079 E : Entity_Id := First_Entity (Defining_Entity (N));
10081 begin
10082 while Present (E) loop
10083 if Ekind (E) = E_Operator then
10084 Set_Is_Eliminated (E);
10085 end if;
10087 Next_Entity (E);
10088 end loop;
10089 end;
10091 -- Recurse into composite statement to kill individual statements in
10092 -- particular instantiations.
10094 elsif Nkind (N) = N_If_Statement then
10095 Kill_Dead_Code (Then_Statements (N));
10096 Kill_Dead_Code (Elsif_Parts (N));
10097 Kill_Dead_Code (Else_Statements (N));
10099 elsif Nkind (N) = N_Loop_Statement then
10100 Kill_Dead_Code (Statements (N));
10102 elsif Nkind (N) = N_Case_Statement then
10103 declare
10104 Alt : Node_Id;
10105 begin
10106 Alt := First (Alternatives (N));
10107 while Present (Alt) loop
10108 Kill_Dead_Code (Statements (Alt));
10109 Next (Alt);
10110 end loop;
10111 end;
10113 elsif Nkind (N) = N_Case_Statement_Alternative then
10114 Kill_Dead_Code (Statements (N));
10116 -- Deal with dead instances caused by deleting instantiations
10118 elsif Nkind (N) in N_Generic_Instantiation then
10119 Remove_Dead_Instance (N);
10120 end if;
10121 end if;
10122 end Kill_Dead_Code;
10124 -- Case where argument is a list of nodes to be killed
10126 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
10127 N : Node_Id;
10128 W : Boolean;
10130 begin
10131 W := Warn;
10133 N := First (L);
10134 while Present (N) loop
10135 Kill_Dead_Code (N, W);
10136 W := False;
10137 Next (N);
10138 end loop;
10139 end Kill_Dead_Code;
10141 -----------------------------
10142 -- Make_CW_Equivalent_Type --
10143 -----------------------------
10145 -- Create a record type used as an equivalent of any member of the class
10146 -- which takes its size from exp.
10148 -- Generate the following code:
10150 -- type Equiv_T is record
10151 -- _parent : T (List of discriminant constraints taken from Exp);
10152 -- Cnn : Storage_Array (1 .. (Exp'size - Typ'object_size)/Storage_Unit);
10153 -- end Equiv_T;
10155 -- Note that this type does not guarantee same alignment as all derived
10156 -- types.
10158 -- Note: for the freezing circuitry, this looks like a record extension,
10159 -- and so we need to make sure that the scalar storage order is the same
10160 -- as that of the parent type. (This does not change anything for the
10161 -- representation of the extension part.)
10163 function Make_CW_Equivalent_Type
10164 (T : Entity_Id;
10165 E : Node_Id;
10166 List_Def : out List_Id) return Entity_Id
10168 Loc : constant Source_Ptr := Sloc (E);
10169 Root_Typ : constant Entity_Id := Root_Type (T);
10170 Root_Utyp : constant Entity_Id := Underlying_Type (Root_Typ);
10171 Comp_List : constant List_Id := New_List;
10173 Equiv_Type : Entity_Id;
10174 Range_Type : Entity_Id;
10175 Str_Type : Entity_Id;
10176 Constr_Root : Entity_Id;
10177 Size_Attr : Node_Id;
10178 Size_Expr : Node_Id;
10180 begin
10181 List_Def := New_List;
10183 -- If the root type is already constrained, there are no discriminants
10184 -- in the expression.
10186 if not Has_Discriminants (Root_Typ)
10187 or else Is_Constrained (Root_Typ)
10188 then
10189 Constr_Root := Root_Typ;
10191 -- At this point in the expansion, nonlimited view of the type
10192 -- must be available, otherwise the error will be reported later.
10194 if From_Limited_With (Constr_Root)
10195 and then Present (Non_Limited_View (Constr_Root))
10196 then
10197 Constr_Root := Non_Limited_View (Constr_Root);
10198 end if;
10200 else
10201 Constr_Root := Make_Temporary (Loc, 'R');
10203 -- subtype cstr__n is T (List of discr constraints taken from Exp)
10205 Append_To (List_Def,
10206 Make_Subtype_Declaration (Loc,
10207 Defining_Identifier => Constr_Root,
10208 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
10209 end if;
10211 -- Generate the range subtype declaration
10213 Range_Type := Make_Temporary (Loc, 'G');
10215 -- If the expression is known to have the tag of its type, then we can
10216 -- use it directly for the prefix of the Size attribute; otherwise we
10217 -- need to convert it first to the class-wide type to force a call to
10218 -- the _Size primitive operation.
10220 if No (E) then
10221 Size_Attr := Make_Integer_Literal (Loc, RM_Size (T));
10223 elsif Has_Tag_Of_Type (E) then
10224 if not Has_Discriminants (Etype (E))
10225 or else Is_Constrained (Etype (E))
10226 then
10227 Size_Attr :=
10228 Make_Attribute_Reference (Loc,
10229 Prefix => New_Occurrence_Of (Etype (E), Loc),
10230 Attribute_Name => Name_Object_Size);
10232 else
10233 Size_Attr :=
10234 Make_Attribute_Reference (Loc,
10235 Prefix => Duplicate_Subexpr_No_Checks (E),
10236 Attribute_Name => Name_Size);
10237 end if;
10239 else
10240 Size_Attr :=
10241 Make_Attribute_Reference (Loc,
10242 Prefix => OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
10243 Attribute_Name => Name_Size);
10244 end if;
10246 if not Is_Interface (Root_Typ) and then Present (E) then
10248 -- subtype rg__xx is
10249 -- Storage_Offset range 1 .. (Exp'size - Typ'object_size)
10250 -- / Storage_Unit
10252 Size_Expr :=
10253 Make_Op_Subtract (Loc,
10254 Left_Opnd => Size_Attr,
10255 Right_Opnd =>
10256 Make_Attribute_Reference (Loc,
10257 Prefix => New_Occurrence_Of (Constr_Root, Loc),
10258 Attribute_Name => Name_Object_Size));
10259 else
10260 -- subtype rg__xx is
10261 -- Storage_Offset range 1 .. (Exp'size - Ada.Tags.Tag'object_size)
10262 -- / Storage_Unit
10264 Size_Expr :=
10265 Make_Op_Subtract (Loc,
10266 Left_Opnd => Size_Attr,
10267 Right_Opnd =>
10268 Make_Attribute_Reference (Loc,
10269 Prefix => New_Occurrence_Of (RTE (RE_Tag), Loc),
10270 Attribute_Name => Name_Object_Size));
10271 end if;
10273 Set_Paren_Count (Size_Expr, 1);
10275 Append_To (List_Def,
10276 Make_Subtype_Declaration (Loc,
10277 Defining_Identifier => Range_Type,
10278 Subtype_Indication =>
10279 Make_Subtype_Indication (Loc,
10280 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
10281 Constraint => Make_Range_Constraint (Loc,
10282 Range_Expression =>
10283 Make_Range (Loc,
10284 Low_Bound => Make_Integer_Literal (Loc, 1),
10285 High_Bound =>
10286 Make_Op_Divide (Loc,
10287 Left_Opnd => Size_Expr,
10288 Right_Opnd => Make_Integer_Literal (Loc,
10289 Intval => System_Storage_Unit)))))));
10291 -- subtype str__nn is Storage_Array (rg__x);
10293 Str_Type := Make_Temporary (Loc, 'S');
10294 Append_To (List_Def,
10295 Make_Subtype_Declaration (Loc,
10296 Defining_Identifier => Str_Type,
10297 Subtype_Indication =>
10298 Make_Subtype_Indication (Loc,
10299 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
10300 Constraint =>
10301 Make_Index_Or_Discriminant_Constraint (Loc,
10302 Constraints =>
10303 New_List (New_Occurrence_Of (Range_Type, Loc))))));
10305 -- type Equiv_T is record
10306 -- _Parent : Snn; -- not interface
10307 -- _Tag : Ada.Tags.Tag -- interface
10308 -- Cnn : Str_Type;
10309 -- end Equiv_T;
10311 Equiv_Type := Make_Temporary (Loc, 'T');
10312 Mutate_Ekind (Equiv_Type, E_Record_Type);
10314 if not Is_Interface (Root_Typ) then
10315 Set_Parent_Subtype (Equiv_Type, Constr_Root);
10316 end if;
10318 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
10319 -- treatment for this type. In particular, even though _parent's type
10320 -- is a controlled type or contains controlled components, we do not
10321 -- want to set Has_Controlled_Component on it to avoid making it gain
10322 -- an unwanted _controller component.
10324 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
10326 -- A class-wide equivalent type does not require initialization unless
10327 -- no expression is present - in which case initialization gets
10328 -- generated as part of the mutably tagged type machinery.
10330 if Present (E) then
10331 Set_Suppress_Initialization (Equiv_Type);
10332 end if;
10334 if not Is_Interface (Root_Typ) and Present (E) then
10335 Append_To (Comp_List,
10336 Make_Component_Declaration (Loc,
10337 Defining_Identifier =>
10338 Make_Defining_Identifier (Loc, Name_uParent),
10339 Component_Definition =>
10340 Make_Component_Definition (Loc,
10341 Aliased_Present => False,
10342 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
10344 Set_Reverse_Storage_Order
10345 (Equiv_Type, Reverse_Storage_Order (Base_Type (Root_Utyp)));
10346 Set_Reverse_Bit_Order
10347 (Equiv_Type, Reverse_Bit_Order (Base_Type (Root_Utyp)));
10349 else
10350 Append_To (Comp_List,
10351 Make_Component_Declaration (Loc,
10352 Defining_Identifier =>
10353 Make_Defining_Identifier (Loc, Name_uTag),
10354 Component_Definition =>
10355 Make_Component_Definition (Loc,
10356 Aliased_Present => False,
10357 Subtype_Indication =>
10358 New_Occurrence_Of (RTE (RE_Tag), Loc))));
10360 Set_Is_Tag (Defining_Identifier (Last (Comp_List)));
10361 end if;
10363 Append_To (Comp_List,
10364 Make_Component_Declaration (Loc,
10365 Defining_Identifier => Make_Temporary (Loc, 'C'),
10366 Component_Definition =>
10367 Make_Component_Definition (Loc,
10368 Aliased_Present => False,
10369 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
10371 Append_To (List_Def,
10372 Make_Full_Type_Declaration (Loc,
10373 Defining_Identifier => Equiv_Type,
10374 Type_Definition =>
10375 Make_Record_Definition (Loc,
10376 Component_List =>
10377 Make_Component_List (Loc,
10378 Component_Items => Comp_List,
10379 Variant_Part => Empty))));
10381 return Equiv_Type;
10382 end Make_CW_Equivalent_Type;
10384 -------------------------
10385 -- Make_Invariant_Call --
10386 -------------------------
10388 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
10389 Loc : constant Source_Ptr := Sloc (Expr);
10390 Typ : constant Entity_Id := Base_Type (Etype (Expr));
10391 pragma Assert (Has_Invariants (Typ));
10392 Proc_Id : constant Entity_Id := Invariant_Procedure (Typ);
10393 pragma Assert (Present (Proc_Id));
10394 Inv_Typ : constant Entity_Id
10395 := Base_Type (Etype (First_Formal (Proc_Id)));
10397 Arg : Node_Id;
10399 begin
10400 -- The invariant procedure has a null body if assertions are disabled or
10401 -- Assertion_Policy Ignore is in effect. In that case, generate a null
10402 -- statement instead of a call to the invariant procedure.
10404 if Has_Null_Body (Proc_Id) then
10405 return Make_Null_Statement (Loc);
10407 else
10408 -- As done elsewhere, for example in Build_Initialization_Call, we
10409 -- may need to bridge the gap between views of the type.
10411 if Inv_Typ /= Typ then
10412 Arg := OK_Convert_To (Inv_Typ, Expr);
10413 else
10414 Arg := Relocate_Node (Expr);
10415 end if;
10417 return
10418 Make_Procedure_Call_Statement (Loc,
10419 Name => New_Occurrence_Of (Proc_Id, Loc),
10420 Parameter_Associations => New_List (Arg));
10421 end if;
10422 end Make_Invariant_Call;
10424 ------------------------
10425 -- Make_Literal_Range --
10426 ------------------------
10428 function Make_Literal_Range
10429 (Loc : Source_Ptr;
10430 Literal_Typ : Entity_Id) return Node_Id
10432 Lo : constant Node_Id :=
10433 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
10434 Index : constant Entity_Id := Etype (Lo);
10435 Length_Expr : constant Node_Id :=
10436 Make_Op_Subtract (Loc,
10437 Left_Opnd =>
10438 Make_Integer_Literal (Loc,
10439 Intval => String_Literal_Length (Literal_Typ)),
10440 Right_Opnd => Make_Integer_Literal (Loc, 1));
10442 Hi : Node_Id;
10444 begin
10445 Set_Analyzed (Lo, False);
10447 if Is_Integer_Type (Index) then
10448 Hi :=
10449 Make_Op_Add (Loc,
10450 Left_Opnd => New_Copy_Tree (Lo),
10451 Right_Opnd => Length_Expr);
10452 else
10453 Hi :=
10454 Make_Attribute_Reference (Loc,
10455 Attribute_Name => Name_Val,
10456 Prefix => New_Occurrence_Of (Index, Loc),
10457 Expressions => New_List (
10458 Make_Op_Add (Loc,
10459 Left_Opnd =>
10460 Make_Attribute_Reference (Loc,
10461 Attribute_Name => Name_Pos,
10462 Prefix => New_Occurrence_Of (Index, Loc),
10463 Expressions => New_List (New_Copy_Tree (Lo))),
10464 Right_Opnd => Length_Expr)));
10465 end if;
10467 return
10468 Make_Range (Loc,
10469 Low_Bound => Lo,
10470 High_Bound => Hi);
10471 end Make_Literal_Range;
10473 --------------------------
10474 -- Make_Non_Empty_Check --
10475 --------------------------
10477 function Make_Non_Empty_Check
10478 (Loc : Source_Ptr;
10479 N : Node_Id) return Node_Id
10481 begin
10482 return
10483 Make_Op_Ne (Loc,
10484 Left_Opnd =>
10485 Make_Attribute_Reference (Loc,
10486 Attribute_Name => Name_Length,
10487 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
10488 Right_Opnd =>
10489 Make_Integer_Literal (Loc, 0));
10490 end Make_Non_Empty_Check;
10492 -------------------------
10493 -- Make_Predicate_Call --
10494 -------------------------
10496 -- WARNING: This routine manages Ghost regions. Return statements must be
10497 -- replaced by gotos which jump to the end of the routine and restore the
10498 -- Ghost mode.
10500 function Make_Predicate_Call
10501 (Typ : Entity_Id;
10502 Expr : Node_Id;
10503 Static_Mem : Boolean := False;
10504 Dynamic_Mem : Node_Id := Empty) return Node_Id
10506 Loc : constant Source_Ptr := Sloc (Expr);
10508 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10509 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10510 -- Save the Ghost-related attributes to restore on exit
10512 Call : Node_Id;
10513 Func_Id : Entity_Id;
10514 Param_Assocs : List_Id;
10515 begin
10516 Func_Id := Predicate_Function (Typ);
10517 pragma Assert (Present (Func_Id));
10519 -- The related type may be subject to pragma Ghost. Set the mode now to
10520 -- ensure that the call is properly marked as Ghost.
10522 Set_Ghost_Mode (Typ);
10524 -- Case of calling normal predicate function
10526 -- If the type is tagged, the expression may be class-wide, in which
10527 -- case it has to be converted to its root type, given that the
10528 -- generated predicate function is not dispatching. The conversion is
10529 -- type-safe and does not need validation, which matters when private
10530 -- extensions are involved.
10532 if Is_Tagged_Type (Typ) then
10533 Param_Assocs := New_List (OK_Convert_To (Typ, Relocate_Node (Expr)));
10534 else
10535 Param_Assocs := New_List (Relocate_Node (Expr));
10536 end if;
10538 if Predicate_Function_Needs_Membership_Parameter (Typ) then
10539 -- Pass in parameter indicating whether this call is for a
10540 -- membership test.
10541 Append ((if Present (Dynamic_Mem)
10542 then Dynamic_Mem
10543 else New_Occurrence_Of
10544 (Boolean_Literals (Static_Mem), Loc)),
10545 Param_Assocs);
10546 end if;
10548 Call :=
10549 Make_Function_Call (Loc,
10550 Name => New_Occurrence_Of (Func_Id, Loc),
10551 Parameter_Associations => Param_Assocs);
10553 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10555 return Call;
10556 end Make_Predicate_Call;
10558 --------------------------
10559 -- Make_Predicate_Check --
10560 --------------------------
10562 function Make_Predicate_Check
10563 (Typ : Entity_Id;
10564 Expr : Node_Id) return Node_Id
10566 Loc : constant Source_Ptr := Sloc (Expr);
10568 -- Local variables
10570 Args : List_Id;
10571 Nam : Name_Id;
10573 -- Start of processing for Make_Predicate_Check
10575 begin
10576 -- If predicate checks are suppressed, then return a null statement. For
10577 -- this call, we check only the scope setting. If the caller wants to
10578 -- check a specific entity's setting, they must do it manually.
10580 if Predicate_Checks_Suppressed (Empty) then
10581 return Make_Null_Statement (Loc);
10582 end if;
10584 -- Do not generate a check within stream functions and the like.
10586 if not Predicate_Check_In_Scope (Expr) then
10587 return Make_Null_Statement (Loc);
10588 end if;
10590 -- Compute proper name to use, we need to get this right so that the
10591 -- right set of check policies apply to the Check pragma we are making.
10592 -- The presence or not of a Ghost_Predicate does not influence the
10593 -- choice of the applicable check policy.
10595 if Has_Dynamic_Predicate_Aspect (Typ) then
10596 Nam := Name_Dynamic_Predicate;
10597 elsif Has_Static_Predicate_Aspect (Typ) then
10598 Nam := Name_Static_Predicate;
10599 else
10600 Nam := Name_Predicate;
10601 end if;
10603 Args := New_List (
10604 Make_Pragma_Argument_Association (Loc,
10605 Expression => Make_Identifier (Loc, Nam)),
10606 Make_Pragma_Argument_Association (Loc,
10607 Expression => Make_Predicate_Call (Typ, Expr)));
10609 -- If the subtype is subject to pragma Predicate_Failure, add the
10610 -- failure expression as an additional parameter.
10612 return
10613 Make_Pragma (Loc,
10614 Chars => Name_Check,
10615 Pragma_Argument_Associations => Args);
10616 end Make_Predicate_Check;
10618 ----------------------------
10619 -- Make_Subtype_From_Expr --
10620 ----------------------------
10622 -- 1. If Expr is an unconstrained array expression, creates
10623 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
10625 -- 2. If Expr is a unconstrained discriminated type expression, creates
10626 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
10628 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
10630 function Make_Subtype_From_Expr
10631 (E : Node_Id;
10632 Unc_Typ : Entity_Id;
10633 Related_Id : Entity_Id := Empty) return Node_Id
10635 List_Constr : constant List_Id := New_List;
10636 Loc : constant Source_Ptr := Sloc (E);
10637 D : Entity_Id;
10638 Full_Exp : Node_Id;
10639 Full_Subtyp : Entity_Id;
10640 High_Bound : Entity_Id;
10641 Index_Typ : Entity_Id;
10642 Low_Bound : Entity_Id;
10643 Priv_Subtyp : Entity_Id;
10644 Utyp : Entity_Id;
10646 begin
10647 if Is_Private_Type (Unc_Typ)
10648 and then Has_Unknown_Discriminants (Unc_Typ)
10649 then
10650 -- The caller requests a unique external name for both the private
10651 -- and the full subtype.
10653 if Present (Related_Id) then
10654 Full_Subtyp :=
10655 Make_Defining_Identifier (Loc,
10656 Chars => New_External_Name (Chars (Related_Id), 'C'));
10657 Priv_Subtyp :=
10658 Make_Defining_Identifier (Loc,
10659 Chars => New_External_Name (Chars (Related_Id), 'P'));
10661 else
10662 Full_Subtyp := Make_Temporary (Loc, 'C');
10663 Priv_Subtyp := Make_Temporary (Loc, 'P');
10664 end if;
10666 -- Prepare the subtype completion. Use the base type to find the
10667 -- underlying type because the type may be a generic actual or an
10668 -- explicit subtype.
10670 Utyp := Underlying_Type (Base_Type (Unc_Typ));
10672 Full_Exp :=
10673 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
10674 Set_Parent (Full_Exp, Parent (E));
10676 Insert_Action (E,
10677 Make_Subtype_Declaration (Loc,
10678 Defining_Identifier => Full_Subtyp,
10679 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
10681 -- Define the dummy private subtype
10683 Mutate_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
10684 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
10685 Set_Scope (Priv_Subtyp, Full_Subtyp);
10686 Set_Is_Constrained (Priv_Subtyp);
10687 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
10688 Set_Is_Itype (Priv_Subtyp);
10689 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
10691 Set_Direct_Primitive_Operations
10692 (Priv_Subtyp, Direct_Primitive_Operations (Unc_Typ));
10694 Set_Full_View (Priv_Subtyp, Full_Subtyp);
10696 return New_Occurrence_Of (Priv_Subtyp, Loc);
10698 elsif Is_Array_Type (Unc_Typ) then
10699 Index_Typ := First_Index (Unc_Typ);
10700 for J in 1 .. Number_Dimensions (Unc_Typ) loop
10702 -- Capture the bounds of each index constraint in case the context
10703 -- is an object declaration of an unconstrained type initialized
10704 -- by a function call:
10706 -- Obj : Unconstr_Typ := Func_Call;
10708 -- This scenario requires secondary scope management and the index
10709 -- constraint cannot depend on the temporary used to capture the
10710 -- result of the function call.
10712 -- SS_Mark;
10713 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
10714 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
10715 -- Obj : S := Temp.all;
10716 -- SS_Release; -- Temp is gone at this point, bounds of S are
10717 -- -- non existent.
10719 -- Generate:
10720 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
10722 Low_Bound := Make_Temporary (Loc, 'B');
10723 Insert_Action (E,
10724 Make_Object_Declaration (Loc,
10725 Defining_Identifier => Low_Bound,
10726 Object_Definition =>
10727 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10728 Constant_Present => True,
10729 Expression =>
10730 Make_Attribute_Reference (Loc,
10731 Prefix => Duplicate_Subexpr_No_Checks (E),
10732 Attribute_Name => Name_First,
10733 Expressions => New_List (
10734 Make_Integer_Literal (Loc, J)))));
10736 -- Generate:
10737 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
10739 High_Bound := Make_Temporary (Loc, 'B');
10740 Insert_Action (E,
10741 Make_Object_Declaration (Loc,
10742 Defining_Identifier => High_Bound,
10743 Object_Definition =>
10744 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10745 Constant_Present => True,
10746 Expression =>
10747 Make_Attribute_Reference (Loc,
10748 Prefix => Duplicate_Subexpr_No_Checks (E),
10749 Attribute_Name => Name_Last,
10750 Expressions => New_List (
10751 Make_Integer_Literal (Loc, J)))));
10753 Append_To (List_Constr,
10754 Make_Range (Loc,
10755 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
10756 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
10758 Next_Index (Index_Typ);
10759 end loop;
10761 elsif Is_Class_Wide_Type (Unc_Typ) then
10762 declare
10763 CW_Subtype : constant Entity_Id :=
10764 New_Class_Wide_Subtype (Unc_Typ, E);
10765 Equiv_Def : List_Id;
10767 begin
10768 -- A class-wide equivalent type is not needed on VM targets
10769 -- because the VM back-ends handle the class-wide object
10770 -- initialization itself (and doesn't need or want the
10771 -- additional intermediate type to handle the assignment).
10773 if Expander_Active and then Tagged_Type_Expansion then
10775 -- If this is the class-wide type of a completion that is a
10776 -- record subtype, set the type of the class-wide type to be
10777 -- the full base type, for use in the expanded code for the
10778 -- equivalent type. Should this be done earlier when the
10779 -- completion is analyzed ???
10781 if Is_Private_Type (Etype (Unc_Typ))
10782 and then
10783 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
10784 then
10785 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
10786 end if;
10788 Set_Equivalent_Type
10789 (CW_Subtype, Make_CW_Equivalent_Type (Unc_Typ, E, Equiv_Def));
10791 -- Suppress all checks during the analysis of the expanded
10792 -- code to avoid the generation of spurious warnings under
10793 -- ZFP run-time.
10795 Insert_Actions
10796 (E, Equiv_Def, Suppress => All_Checks);
10797 end if;
10799 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
10801 return New_Occurrence_Of (CW_Subtype, Loc);
10802 end;
10804 -- Indefinite record type with discriminants
10806 else
10807 D := First_Discriminant (Unc_Typ);
10808 while Present (D) loop
10809 Append_To (List_Constr,
10810 Make_Selected_Component (Loc,
10811 Prefix => Duplicate_Subexpr_No_Checks (E),
10812 Selector_Name => New_Occurrence_Of (D, Loc)));
10814 Next_Discriminant (D);
10815 end loop;
10816 end if;
10818 return
10819 Make_Subtype_Indication (Loc,
10820 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
10821 Constraint =>
10822 Make_Index_Or_Discriminant_Constraint (Loc,
10823 Constraints => List_Constr));
10824 end Make_Subtype_From_Expr;
10826 -----------------------------------
10827 -- Make_Tag_Assignment_From_Type --
10828 -----------------------------------
10830 function Make_Tag_Assignment_From_Type
10831 (Loc : Source_Ptr;
10832 Target : Node_Id;
10833 Typ : Entity_Id) return Node_Id
10835 Nam : constant Node_Id :=
10836 Make_Selected_Component (Loc,
10837 Prefix => Target,
10838 Selector_Name =>
10839 New_Occurrence_Of (First_Tag_Component (Typ), Loc));
10841 begin
10842 Set_Assignment_OK (Nam);
10844 return
10845 Make_Assignment_Statement (Loc,
10846 Name => Nam,
10847 Expression =>
10848 Unchecked_Convert_To (RTE (RE_Tag),
10849 New_Occurrence_Of
10850 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc)));
10851 end Make_Tag_Assignment_From_Type;
10853 -----------------------------
10854 -- Make_Variant_Comparison --
10855 -----------------------------
10857 function Make_Variant_Comparison
10858 (Loc : Source_Ptr;
10859 Typ : Entity_Id;
10860 Mode : Name_Id;
10861 Curr_Val : Node_Id;
10862 Old_Val : Node_Id) return Node_Id
10864 function Big_Integer_Lt return Entity_Id;
10865 -- Returns the entity of the predefined "<" function from
10866 -- Ada.Numerics.Big_Numbers.Big_Integers.
10868 --------------------
10869 -- Big_Integer_Lt --
10870 --------------------
10872 function Big_Integer_Lt return Entity_Id is
10873 Big_Integers : constant Entity_Id :=
10874 RTU_Entity (Ada_Numerics_Big_Numbers_Big_Integers);
10876 E : Entity_Id := First_Entity (Big_Integers);
10878 begin
10879 while Present (E) loop
10880 if Chars (E) = Name_Op_Lt then
10881 return E;
10882 end if;
10883 Next_Entity (E);
10884 end loop;
10886 raise Program_Error;
10887 end Big_Integer_Lt;
10889 -- Start of processing for Make_Variant_Comparison
10891 begin
10892 if Mode = Name_Increases then
10893 return Make_Op_Gt (Loc, Curr_Val, Old_Val);
10895 else pragma Assert (Mode = Name_Decreases);
10897 -- For discrete expressions use the "<" operator
10899 if Is_Discrete_Type (Typ) then
10900 return Make_Op_Lt (Loc, Curr_Val, Old_Val);
10902 -- For Big_Integer expressions use the "<" function, because the
10903 -- operator on private type might not be visible and won't be
10904 -- resolved.
10906 else pragma Assert (Is_RTE (Base_Type (Typ), RE_Big_Integer));
10907 return
10908 Make_Function_Call (Loc,
10909 Name =>
10910 New_Occurrence_Of (Big_Integer_Lt, Loc),
10911 Parameter_Associations =>
10912 New_List (Curr_Val, Old_Val));
10913 end if;
10914 end if;
10915 end Make_Variant_Comparison;
10917 -----------------
10918 -- Map_Formals --
10919 -----------------
10921 procedure Map_Formals
10922 (Parent_Subp : Entity_Id;
10923 Derived_Subp : Entity_Id;
10924 Force_Update : Boolean := False)
10926 Par_Formal : Entity_Id := First_Formal (Parent_Subp);
10927 Subp_Formal : Entity_Id := First_Formal (Derived_Subp);
10929 begin
10930 if Force_Update then
10931 Type_Map.Set (Parent_Subp, Derived_Subp);
10932 end if;
10934 -- At this stage either we are under regular processing and the caller
10935 -- has previously ensured that these primitives are already mapped (by
10936 -- means of calling previously to Update_Primitives_Mapping), or we are
10937 -- processing a late-overriding primitive and Force_Update updated above
10938 -- the mapping of these primitives.
10940 while Present (Par_Formal) and then Present (Subp_Formal) loop
10941 Type_Map.Set (Par_Formal, Subp_Formal);
10942 Next_Formal (Par_Formal);
10943 Next_Formal (Subp_Formal);
10944 end loop;
10945 end Map_Formals;
10947 ---------------
10948 -- Map_Types --
10949 ---------------
10951 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
10953 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
10954 -- avoid deep indentation of code.
10956 -- NOTE: Routines which deal with discriminant mapping operate on the
10957 -- [underlying/record] full view of various types because those views
10958 -- contain all discriminants and stored constraints.
10960 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
10961 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
10962 -- overriding chain starting from Prim whose dispatching type is parent
10963 -- type Par_Typ and add a mapping between the result and primitive Prim.
10965 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
10966 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
10967 -- the inheritance or overriding chain of subprogram Subp. Return Empty
10968 -- if no such primitive is available.
10970 function Build_Chain
10971 (Par_Typ : Entity_Id;
10972 Deriv_Typ : Entity_Id) return Elist_Id;
10973 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
10974 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
10975 -- list has the form:
10977 -- head tail
10978 -- v v
10979 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
10981 -- Note that Par_Typ is not part of the resulting derivation chain
10983 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
10984 -- Return the view of type Typ which could potentially contains either
10985 -- the discriminants or stored constraints of the type.
10987 function Find_Discriminant_Value
10988 (Discr : Entity_Id;
10989 Par_Typ : Entity_Id;
10990 Deriv_Typ : Entity_Id;
10991 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
10992 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
10993 -- in the derivation chain starting from parent type Par_Typ leading to
10994 -- derived type Deriv_Typ. The returned value is one of the following:
10996 -- * An entity which is either a discriminant or a nondiscriminant
10997 -- name, and renames/constraints Discr.
10999 -- * An expression which constraints Discr
11001 -- Typ_Elmt is an element of the derivation chain created by routine
11002 -- Build_Chain and denotes the current ancestor being examined.
11004 procedure Map_Discriminants
11005 (Par_Typ : Entity_Id;
11006 Deriv_Typ : Entity_Id);
11007 -- Map each discriminant of type Par_Typ to a meaningful constraint
11008 -- from the point of view of type Deriv_Typ.
11010 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
11011 -- Map each primitive of type Par_Typ to a corresponding primitive of
11012 -- type Deriv_Typ.
11014 -------------------
11015 -- Add_Primitive --
11016 -------------------
11018 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
11019 Par_Prim : Entity_Id;
11021 begin
11022 -- Inspect the inheritance chain through the Alias attribute and the
11023 -- overriding chain through the Overridden_Operation looking for an
11024 -- ancestor primitive with the appropriate dispatching type.
11026 Par_Prim := Prim;
11027 while Present (Par_Prim) loop
11028 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
11029 Par_Prim := Ancestor_Primitive (Par_Prim);
11030 end loop;
11032 -- Create a mapping of the form:
11034 -- parent type primitive -> derived type primitive
11036 if Present (Par_Prim) then
11037 Type_Map.Set (Par_Prim, Prim);
11038 end if;
11039 end Add_Primitive;
11041 ------------------------
11042 -- Ancestor_Primitive --
11043 ------------------------
11045 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
11046 Inher_Prim : constant Entity_Id := Alias (Subp);
11047 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
11049 begin
11050 -- The current subprogram overrides an ancestor primitive
11052 if Present (Over_Prim) then
11053 return Over_Prim;
11055 -- The current subprogram is an internally generated alias of an
11056 -- inherited ancestor primitive.
11058 elsif Present (Inher_Prim) then
11059 -- It is possible that an internally generated alias could be
11060 -- set to a subprogram which overrides the same aliased primitive,
11061 -- so return Empty in this case.
11063 if Ancestor_Primitive (Inher_Prim) = Subp then
11064 return Empty;
11065 end if;
11067 return Inher_Prim;
11069 -- Otherwise the current subprogram is the root of the inheritance or
11070 -- overriding chain.
11072 else
11073 return Empty;
11074 end if;
11075 end Ancestor_Primitive;
11077 -----------------
11078 -- Build_Chain --
11079 -----------------
11081 function Build_Chain
11082 (Par_Typ : Entity_Id;
11083 Deriv_Typ : Entity_Id) return Elist_Id
11085 Anc_Typ : Entity_Id;
11086 Chain : Elist_Id;
11087 Curr_Typ : Entity_Id;
11089 begin
11090 Chain := New_Elmt_List;
11092 -- Add the derived type to the derivation chain
11094 Prepend_Elmt (Deriv_Typ, Chain);
11096 -- Examine all ancestors starting from the derived type climbing
11097 -- towards parent type Par_Typ.
11099 Curr_Typ := Deriv_Typ;
11100 loop
11101 -- Handle the case where the current type is a record which
11102 -- derives from a subtype.
11104 -- subtype Sub_Typ is Par_Typ ...
11105 -- type Deriv_Typ is Sub_Typ ...
11107 if Ekind (Curr_Typ) = E_Record_Type
11108 and then Present (Parent_Subtype (Curr_Typ))
11109 then
11110 Anc_Typ := Parent_Subtype (Curr_Typ);
11112 -- Handle the case where the current type is a record subtype of
11113 -- another subtype.
11115 -- subtype Sub_Typ1 is Par_Typ ...
11116 -- subtype Sub_Typ2 is Sub_Typ1 ...
11118 elsif Ekind (Curr_Typ) = E_Record_Subtype
11119 and then Present (Cloned_Subtype (Curr_Typ))
11120 then
11121 Anc_Typ := Cloned_Subtype (Curr_Typ);
11123 -- Otherwise use the direct parent type
11125 else
11126 Anc_Typ := Etype (Curr_Typ);
11127 end if;
11129 -- Use the first subtype when dealing with itypes
11131 if Is_Itype (Anc_Typ) then
11132 Anc_Typ := First_Subtype (Anc_Typ);
11133 end if;
11135 -- Work with the view which contains the discriminants and stored
11136 -- constraints.
11138 Anc_Typ := Discriminated_View (Anc_Typ);
11140 -- Stop the climb when either the parent type has been reached or
11141 -- there are no more ancestors left to examine.
11143 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
11145 Prepend_Unique_Elmt (Anc_Typ, Chain);
11146 Curr_Typ := Anc_Typ;
11147 end loop;
11149 return Chain;
11150 end Build_Chain;
11152 ------------------------
11153 -- Discriminated_View --
11154 ------------------------
11156 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
11157 T : Entity_Id;
11159 begin
11160 T := Typ;
11162 -- Use the [underlying] full view when dealing with private types
11163 -- because the view contains all inherited discriminants or stored
11164 -- constraints.
11166 if Is_Private_Type (T) then
11167 if Present (Underlying_Full_View (T)) then
11168 T := Underlying_Full_View (T);
11170 elsif Present (Full_View (T)) then
11171 T := Full_View (T);
11172 end if;
11173 end if;
11175 -- Use the underlying record view when the type is an extenstion of
11176 -- a parent type with unknown discriminants because the view contains
11177 -- all inherited discriminants or stored constraints.
11179 if Ekind (T) = E_Record_Type
11180 and then Present (Underlying_Record_View (T))
11181 then
11182 T := Underlying_Record_View (T);
11183 end if;
11185 return T;
11186 end Discriminated_View;
11188 -----------------------------
11189 -- Find_Discriminant_Value --
11190 -----------------------------
11192 function Find_Discriminant_Value
11193 (Discr : Entity_Id;
11194 Par_Typ : Entity_Id;
11195 Deriv_Typ : Entity_Id;
11196 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
11198 Discr_Pos : constant Uint := Discriminant_Number (Discr);
11199 Typ : constant Entity_Id := Node (Typ_Elmt);
11201 function Find_Constraint_Value
11202 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
11203 -- Given constraint Constr, find what it denotes. This is either:
11205 -- * An entity which is either a discriminant or a name
11207 -- * An expression
11209 ---------------------------
11210 -- Find_Constraint_Value --
11211 ---------------------------
11213 function Find_Constraint_Value
11214 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
11216 begin
11217 if Nkind (Constr) in N_Entity then
11219 -- The constraint denotes a discriminant of the curren type
11220 -- which renames the ancestor discriminant:
11222 -- vv
11223 -- type Typ (D1 : ...; DN : ...) is
11224 -- new Anc (Discr => D1) with ...
11225 -- ^^
11227 if Ekind (Constr) = E_Discriminant then
11229 -- The discriminant belongs to derived type Deriv_Typ. This
11230 -- is the final value for the ancestor discriminant as the
11231 -- derivations chain has been fully exhausted.
11233 if Typ = Deriv_Typ then
11234 return Constr;
11236 -- Otherwise the discriminant may be renamed or constrained
11237 -- at a lower level. Continue looking down the derivation
11238 -- chain.
11240 else
11241 return
11242 Find_Discriminant_Value
11243 (Discr => Constr,
11244 Par_Typ => Par_Typ,
11245 Deriv_Typ => Deriv_Typ,
11246 Typ_Elmt => Next_Elmt (Typ_Elmt));
11247 end if;
11249 -- Otherwise the constraint denotes a reference to some name
11250 -- which results in a Stored discriminant:
11252 -- vvvv
11253 -- Name : ...;
11254 -- type Typ (D1 : ...; DN : ...) is
11255 -- new Anc (Discr => Name) with ...
11256 -- ^^^^
11258 -- Return the name as this is the proper constraint of the
11259 -- discriminant.
11261 else
11262 return Constr;
11263 end if;
11265 -- The constraint denotes a reference to a name
11267 elsif Is_Entity_Name (Constr) then
11268 return Find_Constraint_Value (Entity (Constr));
11270 -- Otherwise the current constraint is an expression which yields
11271 -- a Stored discriminant:
11273 -- type Typ (D1 : ...; DN : ...) is
11274 -- new Anc (Discr => <expression>) with ...
11275 -- ^^^^^^^^^^
11277 -- Return the expression as this is the proper constraint of the
11278 -- discriminant.
11280 else
11281 return Constr;
11282 end if;
11283 end Find_Constraint_Value;
11285 -- Local variables
11287 Constrs : constant Elist_Id := Stored_Constraint (Typ);
11289 Constr_Elmt : Elmt_Id;
11290 Pos : Uint;
11291 Typ_Discr : Entity_Id;
11293 -- Start of processing for Find_Discriminant_Value
11295 begin
11296 -- The algorithm for finding the value of a discriminant works as
11297 -- follows. First, it recreates the derivation chain from Par_Typ
11298 -- to Deriv_Typ as a list:
11300 -- Par_Typ (shown for completeness)
11301 -- v
11302 -- Ancestor_N <-- head of chain
11303 -- v
11304 -- Ancestor_1
11305 -- v
11306 -- Deriv_Typ <-- tail of chain
11308 -- The algorithm then traces the fate of a parent discriminant down
11309 -- the derivation chain. At each derivation level, the discriminant
11310 -- may be either inherited or constrained.
11312 -- 1) Discriminant is inherited: there are two cases, depending on
11313 -- which type is inheriting.
11315 -- 1.1) Deriv_Typ is inheriting:
11317 -- type Ancestor (D_1 : ...) is tagged ...
11318 -- type Deriv_Typ is new Ancestor ...
11320 -- In this case the inherited discriminant is the final value of
11321 -- the parent discriminant because the end of the derivation chain
11322 -- has been reached.
11324 -- 1.2) Some other type is inheriting:
11326 -- type Ancestor_1 (D_1 : ...) is tagged ...
11327 -- type Ancestor_2 is new Ancestor_1 ...
11329 -- In this case the algorithm continues to trace the fate of the
11330 -- inherited discriminant down the derivation chain because it may
11331 -- be further inherited or constrained.
11333 -- 2) Discriminant is constrained: there are three cases, depending
11334 -- on what the constraint is.
11336 -- 2.1) The constraint is another discriminant (aka renaming):
11338 -- type Ancestor_1 (D_1 : ...) is tagged ...
11339 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
11341 -- In this case the constraining discriminant becomes the one to
11342 -- track down the derivation chain. The algorithm already knows
11343 -- that D_2 constrains D_1, therefore if the algorithm finds the
11344 -- value of D_2, then this would also be the value for D_1.
11346 -- 2.2) The constraint is a name (aka Stored):
11348 -- Name : ...
11349 -- type Ancestor_1 (D_1 : ...) is tagged ...
11350 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
11352 -- In this case the name is the final value of D_1 because the
11353 -- discriminant cannot be further constrained.
11355 -- 2.3) The constraint is an expression (aka Stored):
11357 -- type Ancestor_1 (D_1 : ...) is tagged ...
11358 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
11360 -- Similar to 2.2, the expression is the final value of D_1
11362 Pos := Uint_1;
11364 -- When a derived type constrains its parent type, all constaints
11365 -- appear in the Stored_Constraint list. Examine the list looking
11366 -- for a positional match.
11368 if Present (Constrs) then
11369 Constr_Elmt := First_Elmt (Constrs);
11370 while Present (Constr_Elmt) loop
11372 -- The position of the current constraint matches that of the
11373 -- ancestor discriminant.
11375 if Pos = Discr_Pos then
11376 return Find_Constraint_Value (Node (Constr_Elmt));
11377 end if;
11379 Next_Elmt (Constr_Elmt);
11380 Pos := Pos + 1;
11381 end loop;
11383 -- Otherwise the derived type does not constraint its parent type in
11384 -- which case it inherits the parent discriminants.
11386 else
11387 Typ_Discr := First_Discriminant (Typ);
11388 while Present (Typ_Discr) loop
11390 -- The position of the current discriminant matches that of the
11391 -- ancestor discriminant.
11393 if Pos = Discr_Pos then
11394 return Find_Constraint_Value (Typ_Discr);
11395 end if;
11397 Next_Discriminant (Typ_Discr);
11398 Pos := Pos + 1;
11399 end loop;
11400 end if;
11402 -- A discriminant must always have a corresponding value. This is
11403 -- either another discriminant, a name, or an expression. If this
11404 -- point is reached, them most likely the derivation chain employs
11405 -- the wrong views of types.
11407 pragma Assert (False);
11409 return Empty;
11410 end Find_Discriminant_Value;
11412 -----------------------
11413 -- Map_Discriminants --
11414 -----------------------
11416 procedure Map_Discriminants
11417 (Par_Typ : Entity_Id;
11418 Deriv_Typ : Entity_Id)
11420 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
11422 Discr : Entity_Id;
11423 Discr_Val : Node_Or_Entity_Id;
11425 begin
11426 -- Examine each discriminant of parent type Par_Typ and find a
11427 -- suitable value for it from the point of view of derived type
11428 -- Deriv_Typ.
11430 if Has_Discriminants (Par_Typ) then
11431 Discr := First_Discriminant (Par_Typ);
11432 while Present (Discr) loop
11433 Discr_Val :=
11434 Find_Discriminant_Value
11435 (Discr => Discr,
11436 Par_Typ => Par_Typ,
11437 Deriv_Typ => Deriv_Typ,
11438 Typ_Elmt => First_Elmt (Deriv_Chain));
11440 -- Create a mapping of the form:
11442 -- parent type discriminant -> value
11444 Type_Map.Set (Discr, Discr_Val);
11446 Next_Discriminant (Discr);
11447 end loop;
11448 end if;
11449 end Map_Discriminants;
11451 --------------------
11452 -- Map_Primitives --
11453 --------------------
11455 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
11456 Deriv_Prim : Entity_Id;
11457 Par_Prim : Entity_Id;
11458 Par_Prims : Elist_Id;
11459 Prim_Elmt : Elmt_Id;
11461 begin
11462 -- Inspect the primitives of the derived type and determine whether
11463 -- they relate to the primitives of the parent type. If there is a
11464 -- meaningful relation, create a mapping of the form:
11466 -- parent type primitive -> derived type primitive
11468 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
11469 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
11470 while Present (Prim_Elmt) loop
11471 Deriv_Prim := Node (Prim_Elmt);
11473 if Is_Subprogram (Deriv_Prim)
11474 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
11475 then
11476 Add_Primitive (Deriv_Prim, Par_Typ);
11477 end if;
11479 Next_Elmt (Prim_Elmt);
11480 end loop;
11481 end if;
11483 -- If the parent operation is an interface operation, the overriding
11484 -- indicator is not present. Instead, we get from the interface
11485 -- operation the primitive of the current type that implements it.
11487 if Is_Interface (Par_Typ) then
11488 Par_Prims := Collect_Primitive_Operations (Par_Typ);
11490 if Present (Par_Prims) then
11491 Prim_Elmt := First_Elmt (Par_Prims);
11493 while Present (Prim_Elmt) loop
11494 Par_Prim := Node (Prim_Elmt);
11495 Deriv_Prim :=
11496 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
11498 if Present (Deriv_Prim) then
11499 Type_Map.Set (Par_Prim, Deriv_Prim);
11500 end if;
11502 Next_Elmt (Prim_Elmt);
11503 end loop;
11504 end if;
11505 end if;
11506 end Map_Primitives;
11508 -- Start of processing for Map_Types
11510 begin
11511 -- Nothing to do if there are no types to work with
11513 if No (Parent_Type) or else No (Derived_Type) then
11514 return;
11516 -- Nothing to do if the mapping already exists
11518 elsif Type_Map.Get (Parent_Type) = Derived_Type then
11519 return;
11521 -- Nothing to do if both types are not tagged. Note that untagged types
11522 -- do not have primitive operations and their discriminants are already
11523 -- handled by gigi.
11525 elsif not Is_Tagged_Type (Parent_Type)
11526 or else not Is_Tagged_Type (Derived_Type)
11527 then
11528 return;
11529 end if;
11531 -- Create a mapping of the form
11533 -- parent type -> derived type
11535 -- to prevent any subsequent attempts to produce the same relations
11537 Type_Map.Set (Parent_Type, Derived_Type);
11539 -- Create mappings of the form
11541 -- parent type discriminant -> derived type discriminant
11542 -- <or>
11543 -- parent type discriminant -> constraint
11545 -- Note that mapping of discriminants breaks privacy because it needs to
11546 -- work with those views which contains the discriminants and any stored
11547 -- constraints.
11549 Map_Discriminants
11550 (Par_Typ => Discriminated_View (Parent_Type),
11551 Deriv_Typ => Discriminated_View (Derived_Type));
11553 -- Create mappings of the form
11555 -- parent type primitive -> derived type primitive
11557 Map_Primitives
11558 (Par_Typ => Parent_Type,
11559 Deriv_Typ => Derived_Type);
11560 end Map_Types;
11562 ----------------------------
11563 -- Matching_Standard_Type --
11564 ----------------------------
11566 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
11567 pragma Assert (Is_Scalar_Type (Typ));
11568 Siz : constant Uint := Esize (Typ);
11570 begin
11571 -- Floating-point cases
11573 if Is_Floating_Point_Type (Typ) then
11574 if Siz <= Esize (Standard_Short_Float) then
11575 return Standard_Short_Float;
11576 elsif Siz <= Esize (Standard_Float) then
11577 return Standard_Float;
11578 elsif Siz <= Esize (Standard_Long_Float) then
11579 return Standard_Long_Float;
11580 elsif Siz <= Esize (Standard_Long_Long_Float) then
11581 return Standard_Long_Long_Float;
11582 else
11583 raise Program_Error;
11584 end if;
11586 -- Integer cases (includes fixed-point types)
11588 -- Unsigned integer cases (includes normal enumeration types)
11590 else
11591 return Small_Integer_Type_For (Siz, Is_Unsigned_Type (Typ));
11592 end if;
11593 end Matching_Standard_Type;
11595 -----------------------------
11596 -- May_Generate_Large_Temp --
11597 -----------------------------
11599 -- At the current time, the only types that we return False for (i.e. where
11600 -- we decide we know they cannot generate large temps) are ones where we
11601 -- know the size is 256 bits or less at compile time, and we are still not
11602 -- doing a thorough job on arrays and records.
11604 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
11605 begin
11606 if not Size_Known_At_Compile_Time (Typ) then
11607 return False;
11608 end if;
11610 if Known_Esize (Typ) and then Esize (Typ) <= 256 then
11611 return False;
11612 end if;
11614 if Is_Array_Type (Typ)
11615 and then Present (Packed_Array_Impl_Type (Typ))
11616 then
11617 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
11618 end if;
11620 return True;
11621 end May_Generate_Large_Temp;
11623 --------------------------------
11624 -- Name_Of_Controlled_Prim_Op --
11625 --------------------------------
11627 function Name_Of_Controlled_Prim_Op
11628 (Typ : Entity_Id;
11629 Nam : Name_Id) return Name_Id
11631 begin
11632 pragma Assert (Is_Controlled (Typ));
11634 -- The aspect Finalizable may change the name of the primitives when
11635 -- present, but it's a GNAT extension.
11637 if All_Extensions_Allowed then
11638 declare
11639 Rep : constant Node_Id
11640 := Get_Rep_Item (Typ, Name_Finalizable, Check_Parents => True);
11642 Assoc : Node_Id;
11644 begin
11645 if Present (Rep) then
11646 Assoc := First (Component_Associations (Expression (Rep)));
11647 while Present (Assoc) loop
11648 if Chars (First (Choices (Assoc))) = Nam then
11649 return Chars (Expression (Assoc));
11650 end if;
11652 Next (Assoc);
11653 end loop;
11655 return No_Name;
11656 end if;
11657 end;
11658 end if;
11660 return Nam;
11661 end Name_Of_Controlled_Prim_Op;
11663 --------------------------------------------
11664 -- Needs_Conditional_Null_Excluding_Check --
11665 --------------------------------------------
11667 function Needs_Conditional_Null_Excluding_Check
11668 (Typ : Entity_Id) return Boolean
11670 begin
11671 return
11672 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
11673 end Needs_Conditional_Null_Excluding_Check;
11675 ----------------------------
11676 -- Needs_Constant_Address --
11677 ----------------------------
11679 function Needs_Constant_Address
11680 (Decl : Node_Id;
11681 Typ : Entity_Id) return Boolean
11683 begin
11684 -- If we have no initialization of any kind, then we don't need to place
11685 -- any restrictions on the address clause, because the object will be
11686 -- elaborated after the address clause is evaluated. This happens if the
11687 -- declaration has no initial expression, or the type has no implicit
11688 -- initialization, or the object is imported.
11690 -- The same holds for all initialized scalar types and all access types.
11691 -- Packed bit array types of size up to the maximum integer size are
11692 -- represented using a modular type with an initialization (to zero) and
11693 -- can be processed like other initialized scalar types.
11695 -- If the type is controlled, code to attach the object to a
11696 -- finalization chain is generated at the point of declaration, and
11697 -- therefore the elaboration of the object cannot be delayed: the
11698 -- address expression must be a constant.
11700 if No (Expression (Decl))
11701 and then not Needs_Finalization (Typ)
11702 and then
11703 (not Has_Non_Null_Base_Init_Proc (Typ)
11704 or else Is_Imported (Defining_Identifier (Decl)))
11705 then
11706 return False;
11708 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
11709 or else Is_Access_Type (Typ)
11710 or else
11711 (Is_Bit_Packed_Array (Typ)
11712 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
11713 then
11714 return False;
11716 else
11717 -- Otherwise, we require the address clause to be constant because
11718 -- the call to the initialization procedure (or the attach code) has
11719 -- to happen at the point of the declaration.
11721 -- Actually the IP call has been moved to the freeze actions anyway,
11722 -- so maybe we can relax this restriction???
11724 return True;
11725 end if;
11726 end Needs_Constant_Address;
11728 ----------------------------
11729 -- New_Class_Wide_Subtype --
11730 ----------------------------
11732 function New_Class_Wide_Subtype
11733 (CW_Typ : Entity_Id;
11734 N : Node_Id) return Entity_Id
11736 Res : constant Entity_Id := Create_Itype (E_Void, N);
11738 -- Capture relevant attributes of the class-wide subtype which must be
11739 -- restored after the copy.
11741 Res_Chars : constant Name_Id := Chars (Res);
11742 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
11743 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
11744 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
11745 Res_Scope : constant Entity_Id := Scope (Res);
11747 begin
11748 Copy_Node (CW_Typ, Res);
11750 -- Restore the relevant attributes of the class-wide subtype
11752 Set_Chars (Res, Res_Chars);
11753 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
11754 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
11755 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
11756 Set_Scope (Res, Res_Scope);
11758 -- Decorate the class-wide subtype
11760 Set_Associated_Node_For_Itype (Res, N);
11761 Set_Comes_From_Source (Res, False);
11762 Mutate_Ekind (Res, E_Class_Wide_Subtype);
11763 Set_Etype (Res, Base_Type (CW_Typ));
11764 Set_Freeze_Node (Res, Empty);
11765 Set_Is_Frozen (Res, False);
11766 Set_Is_Itype (Res);
11767 Set_Is_Public (Res, False);
11768 Set_Next_Entity (Res, Empty);
11769 Set_Prev_Entity (Res, Empty);
11770 Set_Sloc (Res, Sloc (N));
11772 Set_Public_Status (Res);
11774 return Res;
11775 end New_Class_Wide_Subtype;
11777 -----------------------------------
11778 -- OK_To_Do_Constant_Replacement --
11779 -----------------------------------
11781 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
11782 ES : constant Entity_Id := Scope (E);
11783 CS : Entity_Id;
11785 begin
11786 -- Do not replace statically allocated objects, because they may be
11787 -- modified outside the current scope.
11789 if Is_Statically_Allocated (E) then
11790 return False;
11792 -- Do not replace aliased or volatile objects, since we don't know what
11793 -- else might change the value.
11795 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
11796 return False;
11798 -- Debug flag -gnatdM disconnects this optimization
11800 elsif Debug_Flag_MM then
11801 return False;
11803 -- Otherwise check scopes
11805 else
11806 CS := Current_Scope;
11808 loop
11809 -- If we are in right scope, replacement is safe
11811 if CS = ES then
11812 return True;
11814 -- Packages do not affect the determination of safety
11816 elsif Ekind (CS) = E_Package then
11817 exit when CS = Standard_Standard;
11818 CS := Scope (CS);
11820 -- Blocks do not affect the determination of safety
11822 elsif Ekind (CS) = E_Block then
11823 CS := Scope (CS);
11825 -- Loops do not affect the determination of safety. Note that we
11826 -- kill all current values on entry to a loop, so we are just
11827 -- talking about processing within a loop here.
11829 elsif Ekind (CS) = E_Loop then
11830 CS := Scope (CS);
11832 -- Otherwise, the reference is dubious, and we cannot be sure that
11833 -- it is safe to do the replacement.
11835 else
11836 exit;
11837 end if;
11838 end loop;
11840 return False;
11841 end if;
11842 end OK_To_Do_Constant_Replacement;
11844 ------------------------------------
11845 -- Possible_Bit_Aligned_Component --
11846 ------------------------------------
11848 function Possible_Bit_Aligned_Component
11849 (N : Node_Id;
11850 For_Slice : Boolean := False) return Boolean
11852 begin
11853 -- Do not process an unanalyzed node because it is not yet decorated and
11854 -- most checks performed below will fail.
11856 if not Analyzed (N) then
11857 return False;
11858 end if;
11860 -- There are never alignment issues in CodePeer mode
11862 if CodePeer_Mode then
11863 return False;
11864 end if;
11866 case Nkind (N) is
11868 -- Case of indexed component
11870 when N_Indexed_Component =>
11871 declare
11872 P : constant Node_Id := Prefix (N);
11873 Ptyp : constant Entity_Id := Etype (P);
11875 begin
11876 -- If we know the component size and it is not larger than the
11877 -- maximum integer size, then we are OK. The back end does the
11878 -- assignment of small misaligned objects correctly.
11880 if Known_Static_Component_Size (Ptyp)
11881 and then Component_Size (Ptyp) <= System_Max_Integer_Size
11882 then
11883 return False;
11885 -- Otherwise, we need to test the prefix, to see if we are
11886 -- indexing from a possibly unaligned component.
11888 else
11889 return Possible_Bit_Aligned_Component (P, For_Slice);
11890 end if;
11891 end;
11893 -- Case of selected component
11895 when N_Selected_Component =>
11896 declare
11897 P : constant Node_Id := Prefix (N);
11898 Comp : constant Entity_Id := Entity (Selector_Name (N));
11900 begin
11901 -- This is the crucial test: if the component itself causes
11902 -- trouble, then we can stop and return True.
11904 if Component_May_Be_Bit_Aligned (Comp, For_Slice) then
11905 return True;
11907 -- Otherwise, we need to test the prefix, to see if we are
11908 -- selecting from a possibly unaligned component.
11910 else
11911 return Possible_Bit_Aligned_Component (P, For_Slice);
11912 end if;
11913 end;
11915 -- For a slice, test the prefix, if that is possibly misaligned,
11916 -- then for sure the slice is.
11918 when N_Slice =>
11919 return Possible_Bit_Aligned_Component (Prefix (N), True);
11921 -- For an unchecked conversion, check whether the expression may
11922 -- be bit aligned.
11924 when N_Unchecked_Type_Conversion =>
11925 return Possible_Bit_Aligned_Component (Expression (N), For_Slice);
11927 -- If we have none of the above, it means that we have fallen off the
11928 -- top testing prefixes recursively, and we now have a stand alone
11929 -- object, where we don't have a problem, unless this is a renaming,
11930 -- in which case we need to look into the renamed object.
11932 when others =>
11933 return Is_Entity_Name (N)
11934 and then Is_Object (Entity (N))
11935 and then Present (Renamed_Object (Entity (N)))
11936 and then Possible_Bit_Aligned_Component
11937 (Renamed_Object (Entity (N)), For_Slice);
11938 end case;
11939 end Possible_Bit_Aligned_Component;
11941 -----------------------------------------------
11942 -- Process_Statements_For_Controlled_Objects --
11943 -----------------------------------------------
11945 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
11946 Loc : constant Source_Ptr := Sloc (N);
11948 function Are_Wrapped (L : List_Id) return Boolean;
11949 -- Determine whether list L contains only one statement which is a block
11951 function Wrap_Statements_In_Block
11952 (L : List_Id;
11953 Scop : Entity_Id := Current_Scope) return Node_Id;
11954 -- Given a list of statements L, wrap it in a block statement and return
11955 -- the generated node. Scop is either the current scope or the scope of
11956 -- the context (if applicable).
11958 -----------------
11959 -- Are_Wrapped --
11960 -----------------
11962 function Are_Wrapped (L : List_Id) return Boolean is
11963 Stmt : constant Node_Id := First (L);
11964 begin
11965 return
11966 Present (Stmt)
11967 and then No (Next (Stmt))
11968 and then Nkind (Stmt) = N_Block_Statement;
11969 end Are_Wrapped;
11971 ------------------------------
11972 -- Wrap_Statements_In_Block --
11973 ------------------------------
11975 function Wrap_Statements_In_Block
11976 (L : List_Id;
11977 Scop : Entity_Id := Current_Scope) return Node_Id
11979 Block_Id : Entity_Id;
11980 Block_Nod : Node_Id;
11981 Iter_Loop : Entity_Id;
11983 begin
11984 Block_Nod :=
11985 Make_Block_Statement (Loc,
11986 Declarations => No_List,
11987 Handled_Statement_Sequence =>
11988 Make_Handled_Sequence_Of_Statements (Loc,
11989 Statements => L));
11991 -- Create a label for the block in case the block needs to manage the
11992 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
11994 Add_Block_Identifier (Block_Nod, Block_Id, Scop);
11996 -- When wrapping the statements of an iterator loop, check whether
11997 -- the loop requires secondary stack management and if so, propagate
11998 -- the appropriate flags to the block. This ensures that the cursor
11999 -- is properly cleaned up at each iteration of the loop.
12001 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
12003 if Present (Iter_Loop) then
12004 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
12006 -- Secondary stack reclamation is suppressed when the associated
12007 -- iterator loop contains a return statement which uses the stack.
12009 Set_Sec_Stack_Needed_For_Return
12010 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
12011 end if;
12013 return Block_Nod;
12014 end Wrap_Statements_In_Block;
12016 -- Local variables
12018 Block : Node_Id;
12020 -- Start of processing for Process_Statements_For_Controlled_Objects
12022 begin
12023 -- Whenever a non-handled statement list is wrapped in a block, the
12024 -- block must be explicitly analyzed to redecorate all entities in the
12025 -- list and ensure that a finalizer is properly built.
12027 case Nkind (N) is
12028 when N_Conditional_Entry_Call
12029 | N_Elsif_Part
12030 | N_If_Statement
12031 | N_Selective_Accept
12033 -- Check the "then statements" for elsif parts and if statements
12035 if Nkind (N) in N_Elsif_Part | N_If_Statement
12036 and then not Is_Empty_List (Then_Statements (N))
12037 and then not Are_Wrapped (Then_Statements (N))
12038 and then Requires_Cleanup_Actions
12039 (L => Then_Statements (N),
12040 Lib_Level => False,
12041 Nested_Constructs => False)
12042 then
12043 Block := Wrap_Statements_In_Block (Then_Statements (N));
12044 Set_Then_Statements (N, New_List (Block));
12046 Analyze (Block);
12047 end if;
12049 -- Check the "else statements" for conditional entry calls, if
12050 -- statements and selective accepts.
12052 if Nkind (N) in
12053 N_Conditional_Entry_Call | N_If_Statement | N_Selective_Accept
12054 and then not Is_Empty_List (Else_Statements (N))
12055 and then not Are_Wrapped (Else_Statements (N))
12056 and then Requires_Cleanup_Actions
12057 (L => Else_Statements (N),
12058 Lib_Level => False,
12059 Nested_Constructs => False)
12060 then
12061 Block := Wrap_Statements_In_Block (Else_Statements (N));
12062 Set_Else_Statements (N, New_List (Block));
12064 Analyze (Block);
12065 end if;
12067 when N_Abortable_Part
12068 | N_Accept_Alternative
12069 | N_Case_Statement_Alternative
12070 | N_Delay_Alternative
12071 | N_Entry_Call_Alternative
12072 | N_Exception_Handler
12073 | N_Loop_Statement
12074 | N_Triggering_Alternative
12076 if not Is_Empty_List (Statements (N))
12077 and then not Are_Wrapped (Statements (N))
12078 and then Requires_Cleanup_Actions
12079 (L => Statements (N),
12080 Lib_Level => False,
12081 Nested_Constructs => False)
12082 then
12083 if Nkind (N) = N_Loop_Statement
12084 and then Present (Identifier (N))
12085 then
12086 Block :=
12087 Wrap_Statements_In_Block
12088 (L => Statements (N),
12089 Scop => Entity (Identifier (N)));
12090 else
12091 Block := Wrap_Statements_In_Block (Statements (N));
12092 end if;
12094 Set_Statements (N, New_List (Block));
12095 Analyze (Block);
12096 end if;
12098 -- Could be e.g. a loop that was transformed into a block or null
12099 -- statement. Do nothing for terminate alternatives.
12101 when N_Block_Statement
12102 | N_Null_Statement
12103 | N_Terminate_Alternative
12105 null;
12107 when others =>
12108 raise Program_Error;
12109 end case;
12110 end Process_Statements_For_Controlled_Objects;
12112 ------------------
12113 -- Power_Of_Two --
12114 ------------------
12116 function Power_Of_Two (N : Node_Id) return Nat is
12117 Typ : constant Entity_Id := Etype (N);
12118 pragma Assert (Is_Integer_Type (Typ));
12120 Siz : constant Nat := UI_To_Int (Esize (Typ));
12121 Val : Uint;
12123 begin
12124 if not Compile_Time_Known_Value (N) then
12125 return 0;
12127 else
12128 Val := Expr_Value (N);
12129 for J in 1 .. Siz - 1 loop
12130 if Val = Uint_2 ** J then
12131 return J;
12132 end if;
12133 end loop;
12135 return 0;
12136 end if;
12137 end Power_Of_Two;
12139 ----------------------
12140 -- Remove_Init_Call --
12141 ----------------------
12143 function Remove_Init_Call
12144 (Var : Entity_Id;
12145 Rep_Clause : Node_Id) return Node_Id
12147 Par : constant Node_Id := Parent (Var);
12148 Typ : constant Entity_Id := Etype (Var);
12150 Init_Proc : Entity_Id;
12151 -- Initialization procedure for Typ
12153 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
12154 -- Look for init call for Var starting at From and scanning the
12155 -- enclosing list until Rep_Clause or the end of the list is reached.
12157 ----------------------------
12158 -- Find_Init_Call_In_List --
12159 ----------------------------
12161 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
12162 Init_Call : Node_Id;
12164 begin
12165 Init_Call := From;
12166 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
12167 if Nkind (Init_Call) = N_Procedure_Call_Statement
12168 and then Is_Entity_Name (Name (Init_Call))
12169 and then Entity (Name (Init_Call)) = Init_Proc
12170 then
12171 return Init_Call;
12172 end if;
12174 Next (Init_Call);
12175 end loop;
12177 return Empty;
12178 end Find_Init_Call_In_List;
12180 Init_Call : Node_Id;
12182 -- Start of processing for Remove_Init_Call
12184 begin
12185 if Present (Initialization_Statements (Var)) then
12186 Init_Call := Initialization_Statements (Var);
12187 Set_Initialization_Statements (Var, Empty);
12189 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
12191 -- No init proc for the type, so obviously no call to be found
12193 return Empty;
12195 else
12196 -- We might be able to handle other cases below by just properly
12197 -- setting Initialization_Statements at the point where the init proc
12198 -- call is generated???
12200 Init_Proc := Base_Init_Proc (Typ);
12202 -- First scan the list containing the declaration of Var
12204 Init_Call := Find_Init_Call_In_List (From => Next (Par));
12206 -- If not found, also look on Var's freeze actions list, if any,
12207 -- since the init call may have been moved there (case of an address
12208 -- clause applying to Var).
12210 if No (Init_Call) and then Present (Freeze_Node (Var)) then
12211 Init_Call :=
12212 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
12213 end if;
12215 -- If the initialization call has actuals that use the secondary
12216 -- stack, the call may have been wrapped into a temporary block, in
12217 -- which case the block itself has to be removed.
12219 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
12220 declare
12221 Blk : constant Node_Id := Next (Par);
12222 begin
12223 if Present
12224 (Find_Init_Call_In_List
12225 (First (Statements (Handled_Statement_Sequence (Blk)))))
12226 then
12227 Init_Call := Blk;
12228 end if;
12229 end;
12230 end if;
12231 end if;
12233 if Present (Init_Call) then
12234 -- If restrictions have forbidden Aborts, the initialization call
12235 -- for objects that require deep initialization has not been wrapped
12236 -- into the following block (see Exp_Ch3, Default_Initialize_Object)
12237 -- so if present remove it as well, and include the IP call in it,
12238 -- in the rare case the caller may need to simply displace the
12239 -- initialization, as is done for a later address specification.
12241 if Nkind (Next (Init_Call)) = N_Block_Statement
12242 and then Is_Initialization_Block (Next (Init_Call))
12243 then
12244 declare
12245 IP_Call : constant Node_Id := Init_Call;
12246 begin
12247 Init_Call := Next (IP_Call);
12248 Remove (IP_Call);
12249 Prepend (IP_Call,
12250 Statements (Handled_Statement_Sequence (Init_Call)));
12251 end;
12252 end if;
12254 Remove (Init_Call);
12255 end if;
12257 return Init_Call;
12258 end Remove_Init_Call;
12260 -------------------------
12261 -- Remove_Side_Effects --
12262 -------------------------
12264 procedure Remove_Side_Effects
12265 (Exp : Node_Id;
12266 Name_Req : Boolean := False;
12267 Renaming_Req : Boolean := False;
12268 Variable_Ref : Boolean := False;
12269 Related_Id : Entity_Id := Empty;
12270 Is_Low_Bound : Boolean := False;
12271 Is_High_Bound : Boolean := False;
12272 Discr_Number : Int := 0;
12273 Check_Side_Effects : Boolean := True)
12275 function Build_Temporary
12276 (Loc : Source_Ptr;
12277 Id : Character;
12278 Related_Nod : Node_Id := Empty) return Entity_Id;
12279 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
12280 -- is present (xxx is taken from the Chars field of Related_Nod),
12281 -- otherwise it generates an internal temporary. The created temporary
12282 -- entity is marked as internal.
12284 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean;
12285 -- Computes whether a side effect is possible in SPARK, which should
12286 -- be handled by removing it from the expression for GNATprove. Note
12287 -- that other side effects related to volatile variables are handled
12288 -- separately.
12290 ---------------------
12291 -- Build_Temporary --
12292 ---------------------
12294 function Build_Temporary
12295 (Loc : Source_Ptr;
12296 Id : Character;
12297 Related_Nod : Node_Id := Empty) return Entity_Id
12299 Temp_Id : Entity_Id;
12300 Temp_Nam : Name_Id;
12301 Should_Set_Related_Expression : Boolean := False;
12303 begin
12304 -- The context requires an external symbol : expression is
12305 -- the bound of an array, or a discriminant value. We create
12306 -- a unique string using the related entity and an appropriate
12307 -- suffix, rather than a numeric serial number (used for internal
12308 -- entities) that may vary depending on compilation options, in
12309 -- particular on the Assertions_Enabled mode. This avoids spurious
12310 -- link errors.
12312 if Present (Related_Id) then
12313 if Is_Low_Bound then
12314 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
12316 elsif Is_High_Bound then
12317 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
12319 else
12320 pragma Assert (Discr_Number > 0);
12322 -- We don't have any intelligible way of printing T_DISCR in
12323 -- CodePeer. Thus, set a related expression in this case.
12325 Should_Set_Related_Expression := True;
12327 -- Use fully qualified name to avoid ambiguities.
12329 Temp_Nam :=
12330 New_External_Name
12331 (Get_Qualified_Name (Related_Id), "_DISCR", Discr_Number);
12332 end if;
12334 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
12336 if Should_Set_Related_Expression then
12337 Set_Related_Expression (Temp_Id, Related_Nod);
12338 end if;
12340 -- Otherwise generate an internal temporary
12342 else
12343 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
12344 end if;
12346 Set_Is_Internal (Temp_Id);
12348 return Temp_Id;
12349 end Build_Temporary;
12351 -----------------------------------
12352 -- Possible_Side_Effect_In_SPARK --
12353 -----------------------------------
12355 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean is
12356 begin
12357 -- Side-effect removal in SPARK should only occur when not inside a
12358 -- generic and not doing a preanalysis, inside an object renaming or
12359 -- a type declaration or a for-loop iteration scheme.
12361 if not Inside_A_Generic
12362 and then Full_Analysis
12363 then
12365 case Nkind (Enclosing_Declaration (Exp)) is
12366 when N_Component_Declaration
12367 | N_Full_Type_Declaration
12368 | N_Iterator_Specification
12369 | N_Loop_Parameter_Specification
12370 | N_Object_Renaming_Declaration
12372 return True;
12374 -- If the expression belongs to an itype declaration, then
12375 -- check if side effects are allowed in the original
12376 -- associated node.
12378 when N_Subtype_Declaration =>
12379 declare
12380 Subt : constant Entity_Id :=
12381 Defining_Identifier (Enclosing_Declaration (Exp));
12382 begin
12383 if Is_Itype (Subt) then
12385 -- When this routine is called while the itype
12386 -- is being created, the entity might not yet be
12387 -- decorated with the associated node, but will
12388 -- have the related expression.
12390 if Present (Associated_Node_For_Itype (Subt)) then
12391 return
12392 Possible_Side_Effect_In_SPARK
12393 (Associated_Node_For_Itype (Subt));
12395 else
12396 return
12397 Possible_Side_Effect_In_SPARK
12398 (Related_Expression (Subt));
12399 end if;
12400 else
12401 return True;
12402 end if;
12403 end;
12405 when others =>
12406 return False;
12407 end case;
12408 else
12409 return False;
12410 end if;
12411 end Possible_Side_Effect_In_SPARK;
12413 -- Local variables
12415 Loc : constant Source_Ptr := Sloc (Exp);
12416 Exp_Type : constant Entity_Id := Etype (Exp);
12417 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
12418 Def_Id : Entity_Id;
12419 E : Node_Id;
12420 New_Exp : Node_Id;
12421 Ptr_Typ_Decl : Node_Id;
12422 Ref_Type : Entity_Id;
12423 Res : Node_Id;
12425 -- Start of processing for Remove_Side_Effects
12427 begin
12428 -- Handle cases in which there is nothing to do. In GNATprove mode,
12429 -- removal of side effects is useful for the light expansion of
12430 -- renamings.
12432 if not Expander_Active
12433 and then not
12434 (GNATprove_Mode and then Possible_Side_Effect_In_SPARK (Exp))
12435 then
12436 return;
12438 -- Cannot generate temporaries if the invocation to remove side effects
12439 -- was issued too early and the type of the expression is not resolved
12440 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
12441 -- Remove_Side_Effects).
12443 elsif No (Exp_Type)
12444 or else Ekind (Exp_Type) = E_Access_Attribute_Type
12445 then
12446 return;
12448 -- No action needed for side-effect-free expressions
12450 elsif Check_Side_Effects
12451 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
12452 then
12453 return;
12455 -- Generating C code we cannot remove side effect of function returning
12456 -- class-wide types since there is no secondary stack (required to use
12457 -- 'reference).
12459 elsif Modify_Tree_For_C
12460 and then Nkind (Exp) = N_Function_Call
12461 and then Is_Class_Wide_Type (Etype (Exp))
12462 then
12463 return;
12464 end if;
12466 -- The remaining processing is done with all checks suppressed
12468 -- Note: from now on, don't use return statements, instead do a goto
12469 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
12471 Scope_Suppress.Suppress := (others => True);
12473 -- If this is a side-effect-free attribute reference whose expressions
12474 -- are also side-effect-free and whose prefix is not a name, remove the
12475 -- side effects of the prefix. A copy of the prefix is required in this
12476 -- case and it is better not to make an additional one for the attribute
12477 -- itself, because the return type of many of them is universal integer,
12478 -- which is a very large type for a temporary.
12479 -- The prefix of an attribute reference Reduce may be syntactically an
12480 -- aggregate, but will be expanded into a loop, so no need to remove
12481 -- side effects.
12483 if Nkind (Exp) = N_Attribute_Reference
12484 and then Side_Effect_Free_Attribute (Attribute_Name (Exp))
12485 and then Side_Effect_Free (Expressions (Exp), Name_Req, Variable_Ref)
12486 and then (Attribute_Name (Exp) /= Name_Reduce
12487 or else Nkind (Prefix (Exp)) /= N_Aggregate)
12488 and then not Is_Name_Reference (Prefix (Exp))
12489 then
12490 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
12491 goto Leave;
12493 -- If this is an elementary or a small not-by-reference record type, and
12494 -- we need to capture the value, just make a constant; this is cheap and
12495 -- objects of both kinds of types can be bit aligned, so it might not be
12496 -- possible to generate a reference to them. Likewise if this is not a
12497 -- name reference, except for a type conversion, because we would enter
12498 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
12499 -- type has predicates (and type conversions need a specific treatment
12500 -- anyway, see below). Also do it if we have a volatile reference and
12501 -- Name_Req is not set (see comments for Side_Effect_Free).
12503 elsif (Is_Elementary_Type (Exp_Type)
12504 or else (Is_Record_Type (Exp_Type)
12505 and then Known_Static_RM_Size (Exp_Type)
12506 and then RM_Size (Exp_Type) <= System_Max_Integer_Size
12507 and then not Has_Discriminants (Exp_Type)
12508 and then not Is_By_Reference_Type (Exp_Type)))
12509 and then (Variable_Ref
12510 or else (not Is_Name_Reference (Exp)
12511 and then Nkind (Exp) /= N_Type_Conversion)
12512 or else (not Name_Req
12513 and then Is_Volatile_Reference (Exp)))
12514 then
12515 Def_Id := Build_Temporary (Loc, 'R', Exp);
12516 Set_Etype (Def_Id, Exp_Type);
12517 Res := New_Occurrence_Of (Def_Id, Loc);
12519 -- If the expression is a packed reference, it must be reanalyzed and
12520 -- expanded, depending on context. This is the case for actuals where
12521 -- a constraint check may capture the actual before expansion of the
12522 -- call is complete.
12524 if Nkind (Exp) = N_Indexed_Component
12525 and then Is_Packed (Etype (Prefix (Exp)))
12526 then
12527 Set_Analyzed (Exp, False);
12528 Set_Analyzed (Prefix (Exp), False);
12529 end if;
12531 -- Generate:
12532 -- Rnn : Exp_Type renames Expr;
12534 -- In GNATprove mode, we prefer to use renamings for intermediate
12535 -- variables to definition of constants, due to the implicit move
12536 -- operation that such a constant definition causes as part of the
12537 -- support in GNATprove for ownership pointers. Hence, we generate
12538 -- a renaming for a reference to an object of a nonscalar type.
12540 if Renaming_Req
12541 or else (GNATprove_Mode
12542 and then Is_Object_Reference (Exp)
12543 and then not Is_Scalar_Type (Exp_Type))
12544 then
12545 E :=
12546 Make_Object_Renaming_Declaration (Loc,
12547 Defining_Identifier => Def_Id,
12548 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12549 Name => Relocate_Node (Exp));
12551 -- Generate:
12552 -- Rnn : constant Exp_Type := Expr;
12554 else
12555 E :=
12556 Make_Object_Declaration (Loc,
12557 Defining_Identifier => Def_Id,
12558 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12559 Constant_Present => True,
12560 Expression => Relocate_Node (Exp));
12562 Set_Assignment_OK (E);
12563 end if;
12565 Insert_Action (Exp, E);
12567 -- If the expression has the form v.all then we can just capture the
12568 -- pointer, and then do an explicit dereference on the result, but
12569 -- this is not right if this is a volatile reference.
12571 elsif Nkind (Exp) = N_Explicit_Dereference
12572 and then not Is_Volatile_Reference (Exp)
12573 then
12574 Def_Id := Build_Temporary (Loc, 'R', Exp);
12575 Res :=
12576 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
12578 Insert_Action (Exp,
12579 Make_Object_Declaration (Loc,
12580 Defining_Identifier => Def_Id,
12581 Object_Definition =>
12582 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
12583 Constant_Present => True,
12584 Expression => Relocate_Node (Prefix (Exp))));
12586 -- Similar processing for an unchecked conversion of an expression of
12587 -- the form v.all, where we want the same kind of treatment.
12589 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
12590 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
12591 then
12592 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
12593 goto Leave;
12595 -- If this is a type conversion, leave the type conversion and remove
12596 -- side effects in the expression, unless it is of universal integer,
12597 -- which is a very large type for a temporary. This is important in
12598 -- several circumstances: for change of representations and also when
12599 -- this is a view conversion to a smaller object, where gigi can end
12600 -- up creating its own temporary of the wrong size.
12602 elsif Nkind (Exp) = N_Type_Conversion
12603 and then Etype (Expression (Exp)) /= Universal_Integer
12604 then
12605 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
12607 -- Generating C code the type conversion of an access to constrained
12608 -- array type into an access to unconstrained array type involves
12609 -- initializing a fat pointer and the expression must be free of
12610 -- side effects to safely compute its bounds.
12612 if Modify_Tree_For_C
12613 and then Is_Access_Type (Etype (Exp))
12614 and then Is_Array_Type (Designated_Type (Etype (Exp)))
12615 and then not Is_Constrained (Designated_Type (Etype (Exp)))
12616 then
12617 Def_Id := Build_Temporary (Loc, 'R', Exp);
12618 Set_Etype (Def_Id, Exp_Type);
12619 Res := New_Occurrence_Of (Def_Id, Loc);
12621 Insert_Action (Exp,
12622 Make_Object_Declaration (Loc,
12623 Defining_Identifier => Def_Id,
12624 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12625 Constant_Present => True,
12626 Expression => Relocate_Node (Exp)));
12627 else
12628 goto Leave;
12629 end if;
12631 -- If this is an unchecked conversion that Gigi can't handle, make
12632 -- a copy or a use a renaming to capture the value.
12634 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
12635 and then not Safe_Unchecked_Type_Conversion (Exp)
12636 then
12637 if CW_Or_Needs_Finalization (Exp_Type) then
12639 -- Use a renaming to capture the expression, rather than create
12640 -- a controlled temporary.
12642 Def_Id := Build_Temporary (Loc, 'R', Exp);
12643 Res := New_Occurrence_Of (Def_Id, Loc);
12645 Insert_Action (Exp,
12646 Make_Object_Renaming_Declaration (Loc,
12647 Defining_Identifier => Def_Id,
12648 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12649 Name => Relocate_Node (Exp)));
12651 else
12652 Def_Id := Build_Temporary (Loc, 'R', Exp);
12653 Set_Etype (Def_Id, Exp_Type);
12654 Res := New_Occurrence_Of (Def_Id, Loc);
12656 E :=
12657 Make_Object_Declaration (Loc,
12658 Defining_Identifier => Def_Id,
12659 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12660 Constant_Present => not Is_Variable (Exp),
12661 Expression => Relocate_Node (Exp));
12663 Set_Assignment_OK (E);
12664 Insert_Action (Exp, E);
12665 end if;
12667 -- If this is a packed array component or a selected component with a
12668 -- nonstandard representation, we cannot generate a reference because
12669 -- the component may be unaligned, so we must use a renaming and this
12670 -- renaming is handled by the front end, as the back end may balk at
12671 -- the nonstandard representation (see Evaluation_Required in Exp_Ch8).
12673 elsif (Nkind (Exp) in N_Indexed_Component | N_Selected_Component
12674 and then Has_Non_Standard_Rep (Etype (Prefix (Exp))))
12676 -- For an expression that denotes a name, we can use a renaming
12677 -- scheme. This is needed for correctness in the case of a volatile
12678 -- object of a nonvolatile type because the Make_Reference call of the
12679 -- "default" approach would generate an illegal access value (an
12680 -- access value cannot designate such an object - see
12681 -- Analyze_Reference).
12683 or else (Is_Name_Reference (Exp)
12685 -- We skip using this scheme if we have an object of a volatile
12686 -- type and we do not have Name_Req set true (see comments for
12687 -- Side_Effect_Free).
12689 and then (Name_Req or else not Treat_As_Volatile (Exp_Type)))
12690 then
12691 Def_Id := Build_Temporary (Loc, 'R', Exp);
12692 Res := New_Occurrence_Of (Def_Id, Loc);
12694 Insert_Action (Exp,
12695 Make_Object_Renaming_Declaration (Loc,
12696 Defining_Identifier => Def_Id,
12697 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12698 Name => Relocate_Node (Exp)));
12700 -- Avoid generating a variable-sized temporary, by generating the
12701 -- reference just for the function call. The transformation could be
12702 -- refined to apply only when the array component is constrained by a
12703 -- discriminant???
12705 elsif Nkind (Exp) = N_Selected_Component
12706 and then Nkind (Prefix (Exp)) = N_Function_Call
12707 and then Is_Array_Type (Exp_Type)
12708 then
12709 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
12710 goto Leave;
12712 -- Otherwise we generate a reference to the expression
12714 else
12715 -- When generating C code we cannot consider side-effect-free object
12716 -- declarations that have discriminants and are initialized by means
12717 -- of a function call since on this target there is no secondary
12718 -- stack to store the return value and the expander may generate an
12719 -- extra call to the function to compute the discriminant value. In
12720 -- addition, for targets that have secondary stack, the expansion of
12721 -- functions with side effects involves the generation of an access
12722 -- type to capture the return value stored in the secondary stack;
12723 -- by contrast when generating C code such expansion generates an
12724 -- internal object declaration (no access type involved) which must
12725 -- be identified here to avoid entering into a never-ending loop
12726 -- generating internal object declarations.
12728 if Modify_Tree_For_C
12729 and then Nkind (Parent (Exp)) = N_Object_Declaration
12730 and then
12731 (Nkind (Exp) /= N_Function_Call
12732 or else not Has_Discriminants (Exp_Type)
12733 or else Is_Internal_Name
12734 (Chars (Defining_Identifier (Parent (Exp)))))
12735 then
12736 goto Leave;
12737 end if;
12739 -- Special processing for function calls that return a limited type.
12740 -- We need to build a declaration that will enable build-in-place
12741 -- expansion of the call. This is not done if the context is already
12742 -- an object declaration, to prevent infinite recursion.
12744 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
12745 -- to accommodate functions returning limited objects by reference.
12747 if Ada_Version >= Ada_2005
12748 and then Nkind (Exp) = N_Function_Call
12749 and then Is_Inherently_Limited_Type (Etype (Exp))
12750 and then Nkind (Parent (Exp)) /= N_Object_Declaration
12751 then
12752 declare
12753 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
12754 Decl : Node_Id;
12756 begin
12757 Decl :=
12758 Make_Object_Declaration (Loc,
12759 Defining_Identifier => Obj,
12760 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12761 Expression => Relocate_Node (Exp));
12763 Insert_Action (Exp, Decl);
12764 Set_Etype (Obj, Exp_Type);
12765 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
12766 goto Leave;
12767 end;
12768 end if;
12770 Def_Id := Build_Temporary (Loc, 'R', Exp);
12772 -- The regular expansion of functions with side effects involves the
12773 -- generation of an access type to capture the return value found on
12774 -- the secondary stack. Since SPARK (and why) cannot process access
12775 -- types, use a different approach which ignores the secondary stack
12776 -- and "copies" the returned object.
12777 -- When generating C code, no need for a 'reference since the
12778 -- secondary stack is not supported.
12780 if GNATprove_Mode or Modify_Tree_For_C then
12781 Res := New_Occurrence_Of (Def_Id, Loc);
12782 Ref_Type := Exp_Type;
12784 -- Regular expansion utilizing an access type and 'reference
12786 else
12787 Res :=
12788 Make_Explicit_Dereference (Loc,
12789 Prefix => New_Occurrence_Of (Def_Id, Loc));
12791 -- Generate:
12792 -- type Ann is access all <Exp_Type>;
12794 Ref_Type := Make_Temporary (Loc, 'A');
12796 Ptr_Typ_Decl :=
12797 Make_Full_Type_Declaration (Loc,
12798 Defining_Identifier => Ref_Type,
12799 Type_Definition =>
12800 Make_Access_To_Object_Definition (Loc,
12801 All_Present => True,
12802 Subtype_Indication =>
12803 New_Occurrence_Of (Exp_Type, Loc)));
12805 Insert_Action (Exp, Ptr_Typ_Decl);
12806 end if;
12808 E := Exp;
12809 if Nkind (E) = N_Explicit_Dereference then
12810 New_Exp := Relocate_Node (Prefix (E));
12812 else
12813 E := Relocate_Node (E);
12815 -- Do not generate a 'reference in SPARK mode or C generation
12816 -- since the access type is not created in the first place.
12818 if GNATprove_Mode or Modify_Tree_For_C then
12819 New_Exp := E;
12821 -- Otherwise generate reference, marking the value as non-null
12822 -- since we know it cannot be null and we don't want a check.
12824 else
12825 -- Make_Reference assumes that the referenced
12826 -- object satisfies the constraints of the designated
12827 -- subtype of the access type. Ensure that this assumption
12828 -- holds by introducing a qualified expression if needed.
12830 if not Analyzed (Exp)
12831 and then Nkind (Exp) = N_Aggregate
12832 and then (Is_Array_Type (Exp_Type)
12833 or else Has_Discriminants (Exp_Type))
12834 and then Is_Constrained (Exp_Type)
12835 then
12836 -- Do not suppress checks associated with the qualified
12837 -- expression we are about to introduce (unless those
12838 -- checks were already suppressed when Remove_Side_Effects
12839 -- was called).
12841 if Is_Array_Type (Exp_Type) then
12842 Scope_Suppress.Suppress (Length_Check)
12843 := Svg_Suppress.Suppress (Length_Check);
12844 else
12845 Scope_Suppress.Suppress (Discriminant_Check)
12846 := Svg_Suppress.Suppress (Discriminant_Check);
12847 end if;
12849 E := Make_Qualified_Expression (Loc,
12850 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
12851 Expression => E);
12852 end if;
12854 New_Exp := Make_Reference (Loc, E);
12855 Set_Is_Known_Non_Null (Def_Id);
12856 end if;
12857 end if;
12859 if Is_Delayed_Aggregate (E) then
12861 -- The expansion of nested aggregates is delayed until the
12862 -- enclosing aggregate is expanded. As aggregates are often
12863 -- qualified, the predicate applies to qualified expressions as
12864 -- well, indicating that the enclosing aggregate has not been
12865 -- expanded yet. At this point the aggregate is part of a
12866 -- stand-alone declaration, and must be fully expanded.
12868 if Nkind (E) = N_Qualified_Expression then
12869 Set_Expansion_Delayed (Expression (E), False);
12870 Set_Analyzed (Expression (E), False);
12871 else
12872 Set_Expansion_Delayed (E, False);
12873 end if;
12875 Set_Analyzed (E, False);
12876 end if;
12878 -- Generating C code of object declarations that have discriminants
12879 -- and are initialized by means of a function call we propagate the
12880 -- discriminants of the parent type to the internally built object.
12881 -- This is needed to avoid generating an extra call to the called
12882 -- function.
12884 -- For example, if we generate here the following declaration, it
12885 -- will be expanded later adding an extra call to evaluate the value
12886 -- of the discriminant (needed to compute the size of the object).
12888 -- type Rec (D : Integer) is ...
12889 -- Obj : constant Rec := SomeFunc;
12891 if Modify_Tree_For_C
12892 and then Nkind (Parent (Exp)) = N_Object_Declaration
12893 and then Has_Discriminants (Exp_Type)
12894 and then Nkind (Exp) = N_Function_Call
12895 then
12896 Insert_Action (Exp,
12897 Make_Object_Declaration (Loc,
12898 Defining_Identifier => Def_Id,
12899 Object_Definition => New_Copy_Tree
12900 (Object_Definition (Parent (Exp))),
12901 Constant_Present => True,
12902 Expression => New_Exp));
12903 else
12904 Insert_Action (Exp,
12905 Make_Object_Declaration (Loc,
12906 Defining_Identifier => Def_Id,
12907 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
12908 Constant_Present => True,
12909 Expression => New_Exp));
12910 end if;
12911 end if;
12913 -- Preserve the Assignment_OK flag in all copies, since at least one
12914 -- copy may be used in a context where this flag must be set (otherwise
12915 -- why would the flag be set in the first place).
12917 Set_Assignment_OK (Res, Assignment_OK (Exp));
12919 -- Preserve the Do_Range_Check flag in all copies
12921 Set_Do_Range_Check (Res, Do_Range_Check (Exp));
12923 -- Finally rewrite the original expression and we are done
12925 Rewrite (Exp, Res);
12926 Analyze_And_Resolve (Exp, Exp_Type);
12928 <<Leave>>
12929 Scope_Suppress := Svg_Suppress;
12930 end Remove_Side_Effects;
12932 ------------------------
12933 -- Replace_References --
12934 ------------------------
12936 procedure Replace_References
12937 (Expr : Node_Id;
12938 Par_Typ : Entity_Id;
12939 Deriv_Typ : Entity_Id;
12940 Par_Obj : Entity_Id := Empty;
12941 Deriv_Obj : Entity_Id := Empty)
12943 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
12944 -- Determine whether node Ref denotes some component of Deriv_Obj
12946 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
12947 -- Substitute a reference to an entity with the corresponding value
12948 -- stored in table Type_Map.
12950 function Type_Of_Formal
12951 (Call : Node_Id;
12952 Actual : Node_Id) return Entity_Id;
12953 -- Find the type of the formal parameter which corresponds to actual
12954 -- parameter Actual in subprogram call Call.
12956 ----------------------
12957 -- Is_Deriv_Obj_Ref --
12958 ----------------------
12960 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
12961 Par : constant Node_Id := Parent (Ref);
12963 begin
12964 -- Detect the folowing selected component form:
12966 -- Deriv_Obj.(something)
12968 return
12969 Nkind (Par) = N_Selected_Component
12970 and then Is_Entity_Name (Prefix (Par))
12971 and then Entity (Prefix (Par)) = Deriv_Obj;
12972 end Is_Deriv_Obj_Ref;
12974 -----------------
12975 -- Replace_Ref --
12976 -----------------
12978 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
12979 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
12980 -- Reset the Controlling_Argument of all function calls that
12981 -- encapsulate node From_Arg.
12983 ----------------------------------
12984 -- Remove_Controlling_Arguments --
12985 ----------------------------------
12987 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
12988 Par : Node_Id;
12990 begin
12991 Par := From_Arg;
12992 while Present (Par) loop
12993 if Nkind (Par) = N_Function_Call
12994 and then Present (Controlling_Argument (Par))
12995 then
12996 Set_Controlling_Argument (Par, Empty);
12998 -- Prevent the search from going too far
13000 elsif Is_Body_Or_Package_Declaration (Par) then
13001 exit;
13002 end if;
13004 Par := Parent (Par);
13005 end loop;
13006 end Remove_Controlling_Arguments;
13008 -- Local variables
13010 Context : constant Node_Id :=
13011 (if No (Ref) then Empty else Parent (Ref));
13013 Loc : constant Source_Ptr := Sloc (Ref);
13014 Ref_Id : Entity_Id;
13015 Result : Traverse_Result;
13017 New_Ref : Node_Id;
13018 -- The new reference which is intended to substitute the old one
13020 Old_Ref : Node_Id;
13021 -- The reference designated for replacement. In certain cases this
13022 -- may be a node other than Ref.
13024 Val : Node_Or_Entity_Id;
13025 -- The corresponding value of Ref from the type map
13027 -- Start of processing for Replace_Ref
13029 begin
13030 -- Assume that the input reference is to be replaced and that the
13031 -- traversal should examine the children of the reference.
13033 Old_Ref := Ref;
13034 Result := OK;
13036 -- The input denotes a meaningful reference
13038 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
13039 Ref_Id := Entity (Ref);
13040 Val := Type_Map.Get (Ref_Id);
13042 -- The reference has a corresponding value in the type map, a
13043 -- substitution is possible.
13045 if Present (Val) then
13047 -- The reference denotes a discriminant
13049 if Ekind (Ref_Id) = E_Discriminant then
13050 if Nkind (Val) in N_Entity then
13052 -- The value denotes another discriminant. Replace as
13053 -- follows:
13055 -- _object.Discr -> _object.Val
13057 if Ekind (Val) = E_Discriminant then
13058 New_Ref := New_Occurrence_Of (Val, Loc);
13060 -- Otherwise the value denotes the entity of a name which
13061 -- constraints the discriminant. Replace as follows:
13063 -- _object.Discr -> Val
13065 else
13066 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
13068 New_Ref := New_Occurrence_Of (Val, Loc);
13069 Old_Ref := Parent (Old_Ref);
13070 end if;
13072 -- Otherwise the value denotes an arbitrary expression which
13073 -- constraints the discriminant. Replace as follows:
13075 -- _object.Discr -> Val
13077 else
13078 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
13080 New_Ref := New_Copy_Tree (Val);
13081 Old_Ref := Parent (Old_Ref);
13082 end if;
13084 -- Otherwise the reference denotes a primitive. Replace as
13085 -- follows:
13087 -- Primitive -> Val
13089 else
13090 pragma Assert (Nkind (Val) in N_Entity);
13091 New_Ref := New_Occurrence_Of (Val, Loc);
13092 end if;
13094 -- The reference mentions the _object parameter of the parent
13095 -- type's DIC or type invariant procedure. Replace as follows:
13097 -- _object -> _object
13099 elsif Present (Par_Obj)
13100 and then Present (Deriv_Obj)
13101 and then Ref_Id = Par_Obj
13102 then
13103 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
13105 -- The type of the _object parameter is class-wide when the
13106 -- expression comes from an assertion pragma that applies to
13107 -- an abstract parent type or an interface. The class-wide type
13108 -- facilitates the preanalysis of the expression by treating
13109 -- calls to abstract primitives that mention the current
13110 -- instance of the type as dispatching. Once the calls are
13111 -- remapped to invoke overriding or inherited primitives, the
13112 -- calls no longer need to be dispatching. Examine all function
13113 -- calls that encapsulate the _object parameter and reset their
13114 -- Controlling_Argument attribute.
13116 if Is_Class_Wide_Type (Etype (Par_Obj))
13117 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
13118 then
13119 Remove_Controlling_Arguments (Old_Ref);
13120 end if;
13122 -- The reference to _object acts as an actual parameter in a
13123 -- subprogram call which may be invoking a primitive of the
13124 -- parent type:
13126 -- Primitive (... _object ...);
13128 -- The parent type primitive may not be overridden nor
13129 -- inherited when it is declared after the derived type
13130 -- definition:
13132 -- type Parent is tagged private;
13133 -- type Child is new Parent with private;
13134 -- procedure Primitive (Obj : Parent);
13136 -- In this scenario the _object parameter is converted to the
13137 -- parent type. Due to complications with partial/full views
13138 -- and view swaps, the parent type is taken from the formal
13139 -- parameter of the subprogram being called.
13141 if Nkind (Context) in N_Subprogram_Call
13142 and then No (Type_Map.Get (Entity (Name (Context))))
13143 then
13144 declare
13145 -- We need to use the Original_Node of the callee, in
13146 -- case it was already modified. Note that we are using
13147 -- Traverse_Proc to walk the tree, and it is defined to
13148 -- walk subtrees in an arbitrary order.
13150 Callee : constant Entity_Id :=
13151 Entity (Original_Node (Name (Context)));
13152 begin
13153 if No (Type_Map.Get (Callee)) then
13154 New_Ref :=
13155 Convert_To
13156 (Type_Of_Formal (Context, Old_Ref), New_Ref);
13158 -- Do not process the generated type conversion
13159 -- because both the parent type and the derived type
13160 -- are in the Type_Map table. This will clobber the
13161 -- type conversion by resetting its subtype mark.
13163 Result := Skip;
13164 end if;
13165 end;
13166 end if;
13168 -- Otherwise there is nothing to replace
13170 else
13171 New_Ref := Empty;
13172 end if;
13174 if Present (New_Ref) then
13175 Rewrite (Old_Ref, New_Ref);
13177 -- Update the return type when the context of the reference
13178 -- acts as the name of a function call. Note that the update
13179 -- should not be performed when the reference appears as an
13180 -- actual in the call.
13182 if Nkind (Context) = N_Function_Call
13183 and then Name (Context) = Old_Ref
13184 then
13185 Set_Etype (Context, Etype (Val));
13186 end if;
13187 end if;
13188 end if;
13190 -- Reanalyze the reference due to potential replacements
13192 if Nkind (Old_Ref) in N_Has_Etype then
13193 Set_Analyzed (Old_Ref, False);
13194 end if;
13196 return Result;
13197 end Replace_Ref;
13199 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
13201 --------------------
13202 -- Type_Of_Formal --
13203 --------------------
13205 function Type_Of_Formal
13206 (Call : Node_Id;
13207 Actual : Node_Id) return Entity_Id
13209 A : Node_Id;
13210 F : Entity_Id;
13212 begin
13213 -- Examine the list of actual and formal parameters in parallel
13215 A := First (Parameter_Associations (Call));
13216 F := First_Formal (Entity (Name (Call)));
13217 while Present (A) and then Present (F) loop
13218 if A = Actual then
13219 return Etype (F);
13220 end if;
13222 Next (A);
13223 Next_Formal (F);
13224 end loop;
13226 -- The actual parameter must always have a corresponding formal
13228 pragma Assert (False);
13230 return Empty;
13231 end Type_Of_Formal;
13233 -- Start of processing for Replace_References
13235 begin
13236 -- Map the attributes of the parent type to the proper corresponding
13237 -- attributes of the derived type.
13239 Map_Types
13240 (Parent_Type => Par_Typ,
13241 Derived_Type => Deriv_Typ);
13243 -- Inspect the input expression and perform substitutions where
13244 -- necessary.
13246 Replace_Refs (Expr);
13247 end Replace_References;
13249 -----------------------------
13250 -- Replace_Type_References --
13251 -----------------------------
13253 procedure Replace_Type_References
13254 (Expr : Node_Id;
13255 Typ : Entity_Id;
13256 Obj_Id : Entity_Id)
13258 procedure Replace_Type_Ref (N : Node_Id);
13259 -- Substitute a single reference of the current instance of type Typ
13260 -- with a reference to Obj_Id.
13262 ----------------------
13263 -- Replace_Type_Ref --
13264 ----------------------
13266 procedure Replace_Type_Ref (N : Node_Id) is
13267 begin
13268 -- Decorate the reference to Typ even though it may be rewritten
13269 -- further down. This is done so that routines which examine
13270 -- properties of the Original_Node have some semantic information.
13272 if Nkind (N) = N_Identifier then
13273 Set_Entity (N, Typ);
13274 Set_Etype (N, Typ);
13276 elsif Nkind (N) = N_Selected_Component then
13277 Analyze (Prefix (N));
13278 Set_Entity (Selector_Name (N), Typ);
13279 Set_Etype (Selector_Name (N), Typ);
13280 end if;
13282 -- Perform the following substitution:
13284 -- Typ --> _object
13286 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
13287 Set_Comes_From_Source (N, True);
13288 end Replace_Type_Ref;
13290 procedure Replace_Type_Refs is
13291 new Replace_Type_References_Generic (Replace_Type_Ref);
13293 -- Start of processing for Replace_Type_References
13295 begin
13296 Replace_Type_Refs (Expr, Typ);
13297 end Replace_Type_References;
13299 ---------------------------
13300 -- Represented_As_Scalar --
13301 ---------------------------
13303 function Represented_As_Scalar (T : Entity_Id) return Boolean is
13304 UT : constant Entity_Id := Underlying_Type (T);
13305 begin
13306 return Is_Scalar_Type (UT)
13307 or else (Is_Bit_Packed_Array (UT)
13308 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
13309 end Represented_As_Scalar;
13311 ------------------------------
13312 -- Requires_Cleanup_Actions --
13313 ------------------------------
13315 function Requires_Cleanup_Actions
13316 (N : Node_Id;
13317 Lib_Level : Boolean) return Boolean
13319 At_Lib_Level : constant Boolean :=
13320 Lib_Level
13321 and then Nkind (N) in N_Package_Body | N_Package_Specification;
13322 -- N is at the library level if the top-most context is a package and
13323 -- the path taken to reach N does not include nonpackage constructs.
13325 begin
13326 case Nkind (N) is
13327 when N_Accept_Statement
13328 | N_Block_Statement
13329 | N_Entry_Body
13330 | N_Package_Body
13331 | N_Subprogram_Body
13332 | N_Task_Body
13334 return
13335 Requires_Cleanup_Actions
13336 (L => Declarations (N),
13337 Lib_Level => At_Lib_Level,
13338 Nested_Constructs => True)
13339 or else
13340 (Present (Handled_Statement_Sequence (N))
13341 and then
13342 Requires_Cleanup_Actions
13343 (L =>
13344 Statements (Handled_Statement_Sequence (N)),
13345 Lib_Level => At_Lib_Level,
13346 Nested_Constructs => True));
13348 -- Extended return statements are the same as the above, except that
13349 -- there is no Declarations field. We do not want to clean up the
13350 -- Return_Object_Declarations.
13352 when N_Extended_Return_Statement =>
13353 return
13354 Present (Handled_Statement_Sequence (N))
13355 and then Requires_Cleanup_Actions
13356 (L =>
13357 Statements (Handled_Statement_Sequence (N)),
13358 Lib_Level => At_Lib_Level,
13359 Nested_Constructs => True);
13361 when N_Package_Specification =>
13362 return
13363 Requires_Cleanup_Actions
13364 (L => Visible_Declarations (N),
13365 Lib_Level => At_Lib_Level,
13366 Nested_Constructs => True)
13367 or else
13368 Requires_Cleanup_Actions
13369 (L => Private_Declarations (N),
13370 Lib_Level => At_Lib_Level,
13371 Nested_Constructs => True);
13373 when others =>
13374 raise Program_Error;
13375 end case;
13376 end Requires_Cleanup_Actions;
13378 ------------------------------
13379 -- Requires_Cleanup_Actions --
13380 ------------------------------
13382 function Requires_Cleanup_Actions
13383 (L : List_Id;
13384 Lib_Level : Boolean;
13385 Nested_Constructs : Boolean) return Boolean
13387 Decl : Node_Id;
13388 Expr : Node_Id;
13389 Obj_Id : Entity_Id;
13390 Obj_Typ : Entity_Id;
13391 Pack_Id : Entity_Id;
13392 Typ : Entity_Id;
13394 begin
13395 Decl := First (L);
13396 while Present (Decl) loop
13398 -- Library-level tagged types
13400 if Nkind (Decl) = N_Full_Type_Declaration then
13401 Typ := Defining_Identifier (Decl);
13403 -- Ignored Ghost types do not need any cleanup actions because
13404 -- they will not appear in the final tree.
13406 if Is_Ignored_Ghost_Entity (Typ) then
13407 null;
13409 elsif Is_Tagged_Type (Typ)
13410 and then Is_Library_Level_Entity (Typ)
13411 and then Convention (Typ) = Convention_Ada
13412 and then Present (Access_Disp_Table (Typ))
13413 and then not Is_Abstract_Type (Typ)
13414 and then not No_Run_Time_Mode
13415 and then not Restriction_Active (No_Tagged_Type_Registration)
13416 and then RTE_Available (RE_Unregister_Tag)
13417 then
13418 return True;
13419 end if;
13421 -- Regular object declarations
13423 elsif Nkind (Decl) = N_Object_Declaration then
13424 Obj_Id := Defining_Identifier (Decl);
13425 Obj_Typ := Base_Type (Etype (Obj_Id));
13426 Expr := Expression (Decl);
13428 -- Bypass any form of processing for objects which have their
13429 -- finalization disabled. This applies only to objects at the
13430 -- library level.
13432 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
13433 null;
13435 -- Finalization of transient objects is treated separately in
13436 -- order to handle sensitive cases. These include:
13438 -- * Conditional expressions
13439 -- * Expressions with actions
13440 -- * Transient scopes
13442 elsif Is_Finalized_Transient (Obj_Id) then
13443 null;
13445 -- Finalization of specific objects is also treated separately
13447 elsif Is_Ignored_For_Finalization (Obj_Id) then
13448 null;
13450 -- Conversely, if one of the above cases created a Master_Node,
13451 -- finalization actions are required for the associated object.
13453 elsif Ekind (Obj_Id) = E_Variable
13454 and then Is_RTE (Obj_Typ, RE_Master_Node)
13455 then
13456 return True;
13458 -- Ignored Ghost objects do not need any cleanup actions because
13459 -- they will not appear in the final tree.
13461 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
13462 null;
13464 -- The object is of the form:
13465 -- Obj : [constant] Typ [:= Expr];
13467 -- Do not process the incomplete view of a deferred constant.
13468 -- Note that an object initialized by means of a BIP function
13469 -- call may appear as a deferred constant after expansion
13470 -- activities. These kinds of objects must be finalized.
13472 elsif not Is_Imported (Obj_Id)
13473 and then Needs_Finalization (Obj_Typ)
13474 and then not (Ekind (Obj_Id) = E_Constant
13475 and then not Has_Completion (Obj_Id)
13476 and then No (BIP_Initialization_Call (Obj_Id)))
13477 then
13478 return True;
13480 -- The object is of the form:
13481 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
13483 -- Obj : Access_Typ :=
13484 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
13486 elsif Is_Access_Type (Obj_Typ)
13487 and then Needs_Finalization
13488 (Available_View (Designated_Type (Obj_Typ)))
13489 and then Present (Expr)
13490 and then
13491 (Is_Secondary_Stack_BIP_Func_Call (Expr)
13492 or else
13493 (Is_Non_BIP_Func_Call (Expr)
13494 and then not Is_Related_To_Func_Return (Obj_Id)))
13495 then
13496 return True;
13498 -- Simple protected objects which use the type System.Tasking.
13499 -- Protected_Objects.Protection to manage their locks should be
13500 -- treated as controlled since they require manual cleanup, but
13501 -- not for restricted run-time libraries (Ravenscar), see also
13502 -- Cleanup_Protected_Object in Exp_Ch7.
13504 -- The only exception is illustrated in the following example:
13506 -- package Pkg is
13507 -- type Ctrl is new Controlled ...
13508 -- procedure Finalize (Obj : in out Ctrl);
13509 -- Lib_Obj : Ctrl;
13510 -- end Pkg;
13512 -- package body Pkg is
13513 -- protected Prot is
13514 -- procedure Do_Something (Obj : in out Ctrl);
13515 -- end Prot;
13517 -- protected body Prot is
13518 -- procedure Do_Something (Obj : in out Ctrl) is ...
13519 -- end Prot;
13521 -- procedure Finalize (Obj : in out Ctrl) is
13522 -- begin
13523 -- Prot.Do_Something (Obj);
13524 -- end Finalize;
13525 -- end Pkg;
13527 -- Since for the most part entities in package bodies depend on
13528 -- those in package specs, Prot's lock should be cleaned up
13529 -- first. The subsequent cleanup of the spec finalizes Lib_Obj.
13530 -- This act however attempts to invoke Do_Something and fails
13531 -- because the lock has disappeared.
13533 elsif Ekind (Obj_Id) = E_Variable
13534 and then not In_Library_Level_Package_Body (Obj_Id)
13535 and then Has_Simple_Protected_Object (Obj_Typ)
13536 and then not Restricted_Profile
13537 then
13538 return True;
13539 end if;
13541 -- Inspect the freeze node of an access-to-controlled type and look
13542 -- for a delayed finalization collection. This case arises when the
13543 -- freeze actions are inserted at a later time than the expansion of
13544 -- the context. Since Build_Finalizer is never called on a single
13545 -- construct twice, the collection would be ultimately left out and
13546 -- never finalized. This is also needed for the freeze actions of
13547 -- designated types themselves, since in some cases the finalization
13548 -- collection is associated with a designated type's freeze node
13549 -- rather than that of the access type (see handling for freeze
13550 -- actions in Build_Finalization_Collection).
13552 elsif Nkind (Decl) = N_Freeze_Entity
13553 and then Present (Actions (Decl))
13554 then
13555 Typ := Entity (Decl);
13557 -- Freeze nodes for ignored Ghost types do not need cleanup
13558 -- actions because they will never appear in the final tree.
13560 if Is_Ignored_Ghost_Entity (Typ) then
13561 null;
13563 elsif ((Is_Access_Object_Type (Typ)
13564 and then Needs_Finalization
13565 (Available_View (Designated_Type (Typ))))
13566 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
13567 and then Requires_Cleanup_Actions
13568 (Actions (Decl), Lib_Level, Nested_Constructs)
13569 then
13570 return True;
13571 end if;
13573 -- Nested package declarations
13575 elsif Nested_Constructs
13576 and then Nkind (Decl) = N_Package_Declaration
13577 then
13578 Pack_Id := Defining_Entity (Decl);
13580 -- Do not inspect an ignored Ghost package because all code found
13581 -- within will not appear in the final tree.
13583 if Is_Ignored_Ghost_Entity (Pack_Id) then
13584 null;
13586 elsif Ekind (Pack_Id) /= E_Generic_Package
13587 and then Requires_Cleanup_Actions
13588 (Specification (Decl), Lib_Level)
13589 then
13590 return True;
13591 end if;
13593 -- Nested package bodies
13595 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
13597 -- Do not inspect an ignored Ghost package body because all code
13598 -- found within will not appear in the final tree.
13600 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
13601 null;
13603 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13604 and then Requires_Cleanup_Actions (Decl, Lib_Level)
13605 then
13606 return True;
13607 end if;
13608 end if;
13610 Next (Decl);
13611 end loop;
13613 return False;
13614 end Requires_Cleanup_Actions;
13616 ------------------------------------
13617 -- Safe_Unchecked_Type_Conversion --
13618 ------------------------------------
13620 -- Note: this function knows quite a bit about the exact requirements of
13621 -- Gigi with respect to unchecked type conversions, and its code must be
13622 -- coordinated with any changes in Gigi in this area.
13624 -- The above requirements should be documented in Sinfo ???
13626 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
13627 Otyp : Entity_Id;
13628 Ityp : Entity_Id;
13629 Oalign : Uint;
13630 Ialign : Uint;
13631 Pexp : constant Node_Id := Parent (Exp);
13633 begin
13634 -- If the expression is the RHS of an assignment or object declaration
13635 -- we are always OK because there will always be a target.
13637 -- Object renaming declarations, (generated for view conversions of
13638 -- actuals in inlined calls), like object declarations, provide an
13639 -- explicit type, and are safe as well.
13641 if (Nkind (Pexp) = N_Assignment_Statement
13642 and then Expression (Pexp) = Exp)
13643 or else Nkind (Pexp)
13644 in N_Object_Declaration | N_Object_Renaming_Declaration
13645 then
13646 return True;
13648 -- If the expression is the prefix of an N_Selected_Component we should
13649 -- also be OK because GCC knows to look inside the conversion except if
13650 -- the type is discriminated. We assume that we are OK anyway if the
13651 -- type is not set yet or if it is controlled since we can't afford to
13652 -- introduce a temporary in this case.
13654 elsif Nkind (Pexp) = N_Selected_Component
13655 and then Prefix (Pexp) = Exp
13656 then
13657 return No (Etype (Pexp))
13658 or else not Is_Type (Etype (Pexp))
13659 or else not Has_Discriminants (Etype (Pexp))
13660 or else Is_Constrained (Etype (Pexp));
13661 end if;
13663 -- Set the output type, this comes from Etype if it is set, otherwise we
13664 -- take it from the subtype mark, which we assume was already fully
13665 -- analyzed.
13667 if Present (Etype (Exp)) then
13668 Otyp := Etype (Exp);
13669 else
13670 Otyp := Entity (Subtype_Mark (Exp));
13671 end if;
13673 -- The input type always comes from the expression, and we assume this
13674 -- is indeed always analyzed, so we can simply get the Etype.
13676 Ityp := Etype (Expression (Exp));
13678 -- Initialize alignments to unknown so far
13680 Oalign := No_Uint;
13681 Ialign := No_Uint;
13683 -- Replace a concurrent type by its corresponding record type and each
13684 -- type by its underlying type and do the tests on those. The original
13685 -- type may be a private type whose completion is a concurrent type, so
13686 -- find the underlying type first.
13688 if Present (Underlying_Type (Otyp)) then
13689 Otyp := Underlying_Type (Otyp);
13690 end if;
13692 if Present (Underlying_Type (Ityp)) then
13693 Ityp := Underlying_Type (Ityp);
13694 end if;
13696 if Is_Concurrent_Type (Otyp) then
13697 Otyp := Corresponding_Record_Type (Otyp);
13698 end if;
13700 if Is_Concurrent_Type (Ityp) then
13701 Ityp := Corresponding_Record_Type (Ityp);
13702 end if;
13704 -- If the base types are the same, we know there is no problem since
13705 -- this conversion will be a noop.
13707 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
13708 return True;
13710 -- Same if this is an upwards conversion of an untagged type, and there
13711 -- are no constraints involved (could be more general???)
13713 elsif Etype (Ityp) = Otyp
13714 and then not Is_Tagged_Type (Ityp)
13715 and then not Has_Discriminants (Ityp)
13716 and then No (First_Rep_Item (Base_Type (Ityp)))
13717 then
13718 return True;
13720 -- If the expression has an access type (object or subprogram) we assume
13721 -- that the conversion is safe, because the size of the target is safe,
13722 -- even if it is a record (which might be treated as having unknown size
13723 -- at this point).
13725 elsif Is_Access_Type (Ityp) then
13726 return True;
13728 -- If the size of output type is known at compile time, there is never
13729 -- a problem. Note that unconstrained records are considered to be of
13730 -- known size, but we can't consider them that way here, because we are
13731 -- talking about the actual size of the object.
13733 -- We also make sure that in addition to the size being known, we do not
13734 -- have a case which might generate an embarrassingly large temp in
13735 -- stack checking mode.
13737 elsif Size_Known_At_Compile_Time (Otyp)
13738 and then
13739 (not Stack_Checking_Enabled
13740 or else not May_Generate_Large_Temp (Otyp))
13741 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
13742 then
13743 return True;
13745 -- If either type is tagged, then we know the alignment is OK so Gigi
13746 -- will be able to use pointer punning.
13748 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
13749 return True;
13751 -- If either type is a limited record type, we cannot do a copy, so say
13752 -- safe since there's nothing else we can do.
13754 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
13755 return True;
13757 -- Conversions to and from packed array types are always ignored and
13758 -- hence are safe.
13760 elsif Is_Packed_Array_Impl_Type (Otyp)
13761 or else Is_Packed_Array_Impl_Type (Ityp)
13762 then
13763 return True;
13764 end if;
13766 -- The only other cases known to be safe is if the input type's
13767 -- alignment is known to be at least the maximum alignment for the
13768 -- target or if both alignments are known and the output type's
13769 -- alignment is no stricter than the input's. We can use the component
13770 -- type alignment for an array if a type is an unpacked array type.
13772 if Present (Alignment_Clause (Otyp)) then
13773 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
13775 elsif Is_Array_Type (Otyp)
13776 and then Present (Alignment_Clause (Component_Type (Otyp)))
13777 then
13778 Oalign := Expr_Value (Expression (Alignment_Clause
13779 (Component_Type (Otyp))));
13780 end if;
13782 if Present (Alignment_Clause (Ityp)) then
13783 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
13785 elsif Is_Array_Type (Ityp)
13786 and then Present (Alignment_Clause (Component_Type (Ityp)))
13787 then
13788 Ialign := Expr_Value (Expression (Alignment_Clause
13789 (Component_Type (Ityp))));
13790 end if;
13792 if Present (Ialign) and then Ialign > Maximum_Alignment then
13793 return True;
13795 elsif Present (Ialign)
13796 and then Present (Oalign)
13797 and then Ialign <= Oalign
13798 then
13799 return True;
13801 -- Otherwise, Gigi cannot handle this and we must make a temporary
13803 else
13804 return False;
13805 end if;
13806 end Safe_Unchecked_Type_Conversion;
13808 ---------------------------------
13809 -- Set_Current_Value_Condition --
13810 ---------------------------------
13812 -- Note: the implementation of this procedure is very closely tied to the
13813 -- implementation of Get_Current_Value_Condition. Here we set required
13814 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
13815 -- them, so they must have a consistent view.
13817 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
13819 procedure Set_Entity_Current_Value (N : Node_Id);
13820 -- If N is an entity reference, where the entity is of an appropriate
13821 -- kind, then set the current value of this entity to Cnode, unless
13822 -- there is already a definite value set there.
13824 procedure Set_Expression_Current_Value (N : Node_Id);
13825 -- If N is of an appropriate form, sets an appropriate entry in current
13826 -- value fields of relevant entities. Multiple entities can be affected
13827 -- in the case of an AND or AND THEN.
13829 ------------------------------
13830 -- Set_Entity_Current_Value --
13831 ------------------------------
13833 procedure Set_Entity_Current_Value (N : Node_Id) is
13834 begin
13835 if Is_Entity_Name (N) then
13836 declare
13837 Ent : constant Entity_Id := Entity (N);
13839 begin
13840 -- Don't capture if not safe to do so
13842 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
13843 return;
13844 end if;
13846 -- Here we have a case where the Current_Value field may need
13847 -- to be set. We set it if it is not already set to a compile
13848 -- time expression value.
13850 -- Note that this represents a decision that one condition
13851 -- blots out another previous one. That's certainly right if
13852 -- they occur at the same level. If the second one is nested,
13853 -- then the decision is neither right nor wrong (it would be
13854 -- equally OK to leave the outer one in place, or take the new
13855 -- inner one). Really we should record both, but our data
13856 -- structures are not that elaborate.
13858 if Nkind (Current_Value (Ent)) not in N_Subexpr then
13859 Set_Current_Value (Ent, Cnode);
13860 end if;
13861 end;
13862 end if;
13863 end Set_Entity_Current_Value;
13865 ----------------------------------
13866 -- Set_Expression_Current_Value --
13867 ----------------------------------
13869 procedure Set_Expression_Current_Value (N : Node_Id) is
13870 Cond : Node_Id;
13872 begin
13873 Cond := N;
13875 -- Loop to deal with (ignore for now) any NOT operators present. The
13876 -- presence of NOT operators will be handled properly when we call
13877 -- Get_Current_Value_Condition.
13879 while Nkind (Cond) = N_Op_Not loop
13880 Cond := Right_Opnd (Cond);
13881 end loop;
13883 -- For an AND or AND THEN, recursively process operands
13885 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
13886 Set_Expression_Current_Value (Left_Opnd (Cond));
13887 Set_Expression_Current_Value (Right_Opnd (Cond));
13888 return;
13889 end if;
13891 -- Check possible relational operator
13893 if Nkind (Cond) in N_Op_Compare then
13894 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
13895 Set_Entity_Current_Value (Left_Opnd (Cond));
13896 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
13897 Set_Entity_Current_Value (Right_Opnd (Cond));
13898 end if;
13900 elsif Nkind (Cond) in N_Type_Conversion
13901 | N_Qualified_Expression
13902 | N_Expression_With_Actions
13903 then
13904 Set_Expression_Current_Value (Expression (Cond));
13906 -- Check possible boolean variable reference
13908 else
13909 Set_Entity_Current_Value (Cond);
13910 end if;
13911 end Set_Expression_Current_Value;
13913 -- Start of processing for Set_Current_Value_Condition
13915 begin
13916 Set_Expression_Current_Value (Condition (Cnode));
13917 end Set_Current_Value_Condition;
13919 --------------------------
13920 -- Set_Elaboration_Flag --
13921 --------------------------
13923 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
13924 Loc : constant Source_Ptr := Sloc (N);
13925 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
13926 Asn : Node_Id;
13928 begin
13929 if Present (Ent) then
13931 -- Nothing to do if at the compilation unit level, because in this
13932 -- case the flag is set by the binder generated elaboration routine.
13934 if Nkind (Parent (N)) = N_Compilation_Unit then
13935 null;
13937 -- Here we do need to generate an assignment statement
13939 else
13940 Check_Restriction (No_Elaboration_Code, N);
13942 Asn :=
13943 Make_Assignment_Statement (Loc,
13944 Name => New_Occurrence_Of (Ent, Loc),
13945 Expression => Make_Integer_Literal (Loc, Uint_1));
13947 -- Mark the assignment statement as elaboration code. This allows
13948 -- the early call region mechanism (see Sem_Elab) to properly
13949 -- ignore such assignments even though they are nonpreelaborable
13950 -- code.
13952 Set_Is_Elaboration_Code (Asn);
13954 if Nkind (Parent (N)) = N_Subunit then
13955 Insert_After (Corresponding_Stub (Parent (N)), Asn);
13956 else
13957 Insert_After (N, Asn);
13958 end if;
13960 Analyze (Asn);
13962 -- Kill current value indication. This is necessary because the
13963 -- tests of this flag are inserted out of sequence and must not
13964 -- pick up bogus indications of the wrong constant value.
13966 Set_Current_Value (Ent, Empty);
13968 -- If the subprogram is in the current declarative part and
13969 -- 'access has been applied to it, generate an elaboration
13970 -- check at the beginning of the declarations of the body.
13972 if Nkind (N) = N_Subprogram_Body
13973 and then Address_Taken (Spec_Id)
13974 and then
13975 Ekind (Scope (Spec_Id)) in E_Block | E_Procedure | E_Function
13976 then
13977 declare
13978 Loc : constant Source_Ptr := Sloc (N);
13979 Decls : constant List_Id := Declarations (N);
13980 Chk : Node_Id;
13982 begin
13983 -- No need to generate this check if first entry in the
13984 -- declaration list is a raise of Program_Error now.
13986 if Present (Decls)
13987 and then Nkind (First (Decls)) = N_Raise_Program_Error
13988 then
13989 return;
13990 end if;
13992 -- Otherwise generate the check
13994 Chk :=
13995 Make_Raise_Program_Error (Loc,
13996 Condition =>
13997 Make_Op_Eq (Loc,
13998 Left_Opnd => New_Occurrence_Of (Ent, Loc),
13999 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
14000 Reason => PE_Access_Before_Elaboration);
14002 if No (Decls) then
14003 Set_Declarations (N, New_List (Chk));
14004 else
14005 Prepend (Chk, Decls);
14006 end if;
14008 Analyze (Chk);
14009 end;
14010 end if;
14011 end if;
14012 end if;
14013 end Set_Elaboration_Flag;
14015 ----------------------------
14016 -- Set_Renamed_Subprogram --
14017 ----------------------------
14019 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
14020 begin
14021 -- If input node is an identifier, we can just reset it
14023 if Nkind (N) = N_Identifier then
14024 Set_Chars (N, Chars (E));
14025 Set_Entity (N, E);
14027 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
14029 else
14030 declare
14031 CS : constant Boolean := Comes_From_Source (N);
14032 begin
14033 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
14034 Set_Entity (N, E);
14035 Set_Comes_From_Source (N, CS);
14036 Set_Analyzed (N, True);
14037 end;
14038 end if;
14039 end Set_Renamed_Subprogram;
14041 ----------------------
14042 -- Side_Effect_Free --
14043 ----------------------
14045 function Side_Effect_Free
14046 (N : Node_Id;
14047 Name_Req : Boolean := False;
14048 Variable_Ref : Boolean := False) return Boolean
14050 Typ : constant Entity_Id := Etype (N);
14051 -- Result type of the expression
14053 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
14054 -- The argument N is a construct where the Prefix is dereferenced if it
14055 -- is an access type and the result is a variable. The call returns True
14056 -- if the construct is side-effect-free (not considering side effects in
14057 -- other than the prefix which are to be tested by the caller).
14059 function Within_In_Parameter (N : Node_Id) return Boolean;
14060 -- Determines if N is a subcomponent of a composite in-parameter. If so,
14061 -- N is not side-effect-free when the actual is global and modifiable
14062 -- indirectly from within a subprogram, because it may be passed by
14063 -- reference. The front-end must be conservative here and assume that
14064 -- this may happen with any array or record type. On the other hand, we
14065 -- cannot create temporaries for all expressions for which this
14066 -- condition is true, for various reasons that might require clearing up
14067 -- ??? For example, discriminant references that appear out of place, or
14068 -- spurious type errors with class-wide expressions. As a result, we
14069 -- limit the transformation to loop bounds, which is so far the only
14070 -- case that requires it.
14072 -----------------------------
14073 -- Safe_Prefixed_Reference --
14074 -----------------------------
14076 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
14077 begin
14078 -- If prefix is not side-effect-free, definitely not safe
14080 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
14081 return False;
14083 -- If the prefix is of an access type that is not access-to-constant,
14084 -- then this construct is a variable reference, which means it is to
14085 -- be considered to have side effects if Variable_Ref is set True.
14087 elsif Is_Access_Type (Etype (Prefix (N)))
14088 and then not Is_Access_Constant (Etype (Prefix (N)))
14089 and then Variable_Ref
14090 then
14091 -- Exception is a prefix that is the result of a previous removal
14092 -- of side effects.
14094 return Is_Entity_Name (Prefix (N))
14095 and then not Comes_From_Source (Prefix (N))
14096 and then Ekind (Entity (Prefix (N))) = E_Constant
14097 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
14099 -- If the prefix is an explicit dereference then this construct is a
14100 -- variable reference, which means it is to be considered to have
14101 -- side effects if Variable_Ref is True.
14103 -- We do NOT exclude dereferences of access-to-constant types because
14104 -- we handle them as constant view of variables.
14106 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
14107 and then Variable_Ref
14108 then
14109 return False;
14111 -- Note: The following test is the simplest way of solving a complex
14112 -- problem uncovered by the following test (Side effect on loop bound
14113 -- that is a subcomponent of a global variable:
14115 -- with Text_Io; use Text_Io;
14116 -- procedure Tloop is
14117 -- type X is
14118 -- record
14119 -- V : Natural := 4;
14120 -- S : String (1..5) := (others => 'a');
14121 -- end record;
14122 -- X1 : X;
14124 -- procedure Modi;
14126 -- generic
14127 -- with procedure Action;
14128 -- procedure Loop_G (Arg : X; Msg : String)
14130 -- procedure Loop_G (Arg : X; Msg : String) is
14131 -- begin
14132 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
14133 -- & Natural'Image (Arg.V));
14134 -- for Index in 1 .. Arg.V loop
14135 -- Text_Io.Put_Line
14136 -- (Natural'Image (Index) & " " & Arg.S (Index));
14137 -- if Index > 2 then
14138 -- Modi;
14139 -- end if;
14140 -- end loop;
14141 -- Put_Line ("end loop_g " & Msg);
14142 -- end;
14144 -- procedure Loop1 is new Loop_G (Modi);
14145 -- procedure Modi is
14146 -- begin
14147 -- X1.V := 1;
14148 -- Loop1 (X1, "from modi");
14149 -- end;
14151 -- begin
14152 -- Loop1 (X1, "initial");
14153 -- end;
14155 -- The output of the above program should be:
14157 -- begin loop_g initial will loop till: 4
14158 -- 1 a
14159 -- 2 a
14160 -- 3 a
14161 -- begin loop_g from modi will loop till: 1
14162 -- 1 a
14163 -- end loop_g from modi
14164 -- 4 a
14165 -- begin loop_g from modi will loop till: 1
14166 -- 1 a
14167 -- end loop_g from modi
14168 -- end loop_g initial
14170 -- If a loop bound is a subcomponent of a global variable, a
14171 -- modification of that variable within the loop may incorrectly
14172 -- affect the execution of the loop.
14174 elsif Parent_Kind (Parent (N)) = N_Loop_Parameter_Specification
14175 and then Within_In_Parameter (Prefix (N))
14176 and then Variable_Ref
14177 then
14178 return False;
14180 -- All other cases are side-effect-free
14182 else
14183 return True;
14184 end if;
14185 end Safe_Prefixed_Reference;
14187 -------------------------
14188 -- Within_In_Parameter --
14189 -------------------------
14191 function Within_In_Parameter (N : Node_Id) return Boolean is
14192 begin
14193 if not Comes_From_Source (N) then
14194 return False;
14196 elsif Is_Entity_Name (N) then
14197 return Ekind (Entity (N)) = E_In_Parameter;
14199 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
14200 return Within_In_Parameter (Prefix (N));
14202 else
14203 return False;
14204 end if;
14205 end Within_In_Parameter;
14207 -- Start of processing for Side_Effect_Free
14209 begin
14210 -- If volatile reference, always consider it to have side effects
14212 if Is_Volatile_Reference (N) then
14213 return False;
14214 end if;
14216 -- Note on checks that could raise Constraint_Error. Strictly, if we
14217 -- take advantage of 11.6, these checks do not count as side effects.
14218 -- However, we would prefer to consider that they are side effects,
14219 -- since the back end CSE does not work very well on expressions which
14220 -- can raise Constraint_Error. On the other hand if we don't consider
14221 -- them to be side-effect-free, then we get some awkward expansions
14222 -- in -gnato mode, resulting in code insertions at a point where we
14223 -- do not have a clear model for performing the insertions.
14225 -- Special handling for entity names
14227 if Is_Entity_Name (N) then
14229 -- A type reference is always side-effect-free
14231 if Is_Type (Entity (N)) then
14232 return True;
14234 -- Variables are considered to be a side effect if Variable_Ref
14235 -- is set or if we have a volatile reference and Name_Req is off.
14236 -- If Name_Req is True then we can't help returning a name which
14237 -- effectively allows multiple references in any case.
14239 elsif Is_Variable (N, Use_Original_Node => False) then
14240 return not Variable_Ref
14241 and then (not Is_Volatile_Reference (N) or else Name_Req);
14243 -- Any other entity (e.g. a subtype name) is definitely side
14244 -- effect free.
14246 else
14247 return True;
14248 end if;
14250 -- A value known at compile time is always side-effect-free
14252 elsif Compile_Time_Known_Value (N) then
14253 return True;
14255 -- A variable renaming is not side-effect-free, because the renaming
14256 -- will function like a macro in the front-end in some cases, and an
14257 -- assignment can modify the component designated by N, so we need to
14258 -- create a temporary for it.
14260 -- The guard testing for Entity being present is needed at least in
14261 -- the case of rewritten predicate expressions, and may well also be
14262 -- appropriate elsewhere. Obviously we can't go testing the entity
14263 -- field if it does not exist, so it's reasonable to say that this is
14264 -- not the renaming case if it does not exist.
14266 elsif Is_Entity_Name (Original_Node (N))
14267 and then Present (Entity (Original_Node (N)))
14268 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
14269 and then Ekind (Entity (Original_Node (N))) /= E_Constant
14270 then
14271 declare
14272 RO : constant Node_Id :=
14273 Renamed_Object (Entity (Original_Node (N)));
14275 begin
14276 -- If the renamed object is an indexed component, or an
14277 -- explicit dereference, then the designated object could
14278 -- be modified by an assignment.
14280 if Nkind (RO) in N_Indexed_Component | N_Explicit_Dereference then
14281 return False;
14283 -- A selected component must have a safe prefix
14285 elsif Nkind (RO) = N_Selected_Component then
14286 return Safe_Prefixed_Reference (RO);
14288 -- In all other cases, designated object cannot be changed so
14289 -- we are side-effect-free.
14291 else
14292 return True;
14293 end if;
14294 end;
14296 -- Remove_Side_Effects generates an object renaming declaration to
14297 -- capture the expression of a class-wide expression. In VM targets
14298 -- the frontend performs no expansion for dispatching calls to
14299 -- class- wide types since they are handled by the VM. Hence, we must
14300 -- locate here if this node corresponds to a previous invocation of
14301 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
14303 elsif not Tagged_Type_Expansion
14304 and then not Comes_From_Source (N)
14305 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
14306 and then Is_Class_Wide_Type (Typ)
14307 then
14308 return True;
14310 -- Generating C the type conversion of an access to constrained array
14311 -- type into an access to unconstrained array type involves initializing
14312 -- a fat pointer and the expression cannot be assumed to be free of side
14313 -- effects since it must referenced several times to compute its bounds.
14315 elsif Modify_Tree_For_C
14316 and then Nkind (N) = N_Type_Conversion
14317 and then Is_Access_Type (Typ)
14318 and then Is_Array_Type (Designated_Type (Typ))
14319 and then not Is_Constrained (Designated_Type (Typ))
14320 then
14321 return False;
14322 end if;
14324 -- For other than entity names and compile time known values,
14325 -- check the node kind for special processing.
14327 case Nkind (N) is
14329 -- An attribute reference is side-effect-free if its expressions
14330 -- are side-effect-free and its prefix is side-effect-free or is
14331 -- an entity reference.
14333 when N_Attribute_Reference =>
14334 return Side_Effect_Free_Attribute (Attribute_Name (N))
14335 and then
14336 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
14337 and then
14338 (Is_Entity_Name (Prefix (N))
14339 or else
14340 Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref));
14342 -- A binary operator is side-effect-free if and both operands are
14343 -- side-effect-free. For this purpose binary operators include
14344 -- short circuit forms.
14346 when N_Binary_Op
14347 | N_Short_Circuit
14349 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
14350 and then
14351 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
14353 -- Membership tests may have either Right_Opnd or Alternatives set
14355 when N_Membership_Test =>
14356 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
14357 and then
14358 (if Present (Right_Opnd (N))
14359 then Side_Effect_Free
14360 (Right_Opnd (N), Name_Req, Variable_Ref)
14361 else Side_Effect_Free
14362 (Alternatives (N), Name_Req, Variable_Ref));
14364 -- An explicit dereference is side-effect-free only if it is
14365 -- a side-effect-free prefixed reference.
14367 when N_Explicit_Dereference =>
14368 return Safe_Prefixed_Reference (N);
14370 -- An expression with action is side-effect-free if its expression
14371 -- is side-effect-free and it has no actions.
14373 when N_Expression_With_Actions =>
14374 return
14375 Is_Empty_List (Actions (N))
14376 and then Side_Effect_Free
14377 (Expression (N), Name_Req, Variable_Ref);
14379 -- A call to _rep_to_pos is side-effect-free, since we generate
14380 -- this pure function call ourselves. Moreover it is critically
14381 -- important to make this exception, since otherwise we can have
14382 -- discriminants in array components which don't look side-effect
14383 -- free in the case of an array whose index type is an enumeration
14384 -- type with an enumeration rep clause.
14386 -- All other function calls are not side-effect-free
14388 when N_Function_Call =>
14389 return
14390 Nkind (Name (N)) = N_Identifier
14391 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
14392 and then Side_Effect_Free
14393 (First (Parameter_Associations (N)),
14394 Name_Req, Variable_Ref);
14396 -- An IF expression is side-effect-free if it's of a scalar type, and
14397 -- all its components are all side-effect-free (conditions and then
14398 -- actions and else actions). We restrict to scalar types, since it
14399 -- is annoying to deal with things like (if A then B else C)'First
14400 -- where the type involved is a string type.
14402 when N_If_Expression =>
14403 return
14404 Is_Scalar_Type (Typ)
14405 and then Side_Effect_Free
14406 (Expressions (N), Name_Req, Variable_Ref);
14408 -- An indexed component is side-effect-free if it is a side
14409 -- effect free prefixed reference and all the indexing
14410 -- expressions are side-effect-free.
14412 when N_Indexed_Component =>
14413 return
14414 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
14415 and then Safe_Prefixed_Reference (N);
14417 -- A type qualification, type conversion, or unchecked expression is
14418 -- side-effect-free if the expression is side-effect-free.
14420 when N_Qualified_Expression
14421 | N_Type_Conversion
14422 | N_Unchecked_Expression
14424 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
14426 -- A selected component is side-effect-free only if it is a side
14427 -- effect free prefixed reference.
14429 when N_Selected_Component =>
14430 return Safe_Prefixed_Reference (N);
14432 -- A range is side-effect-free if the bounds are side-effect-free
14434 when N_Range =>
14435 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
14436 and then
14437 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
14439 -- A slice is side-effect-free if it is a side-effect-free
14440 -- prefixed reference and the bounds are side-effect-free.
14442 when N_Slice =>
14443 return
14444 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
14445 and then Safe_Prefixed_Reference (N);
14447 -- A unary operator is side-effect-free if the operand
14448 -- is side-effect-free.
14450 when N_Unary_Op =>
14451 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
14453 -- An unchecked type conversion is side-effect-free only if it
14454 -- is safe and its argument is side-effect-free.
14456 when N_Unchecked_Type_Conversion =>
14457 return
14458 Safe_Unchecked_Type_Conversion (N)
14459 and then Side_Effect_Free
14460 (Expression (N), Name_Req, Variable_Ref);
14462 -- A literal is side-effect-free
14464 when N_Character_Literal
14465 | N_Integer_Literal
14466 | N_Real_Literal
14467 | N_String_Literal
14469 return True;
14471 -- An aggregate is side-effect-free if all its values are compile
14472 -- time known.
14474 when N_Aggregate =>
14475 return Compile_Time_Known_Aggregate (N);
14477 -- We consider that anything else has side effects. This is a bit
14478 -- crude, but we are pretty close for most common cases, and we
14479 -- are certainly correct (i.e. we never return True when the
14480 -- answer should be False).
14482 when others =>
14483 return False;
14484 end case;
14485 end Side_Effect_Free;
14487 -- A list is side-effect-free if all elements of the list are side
14488 -- effect free.
14490 function Side_Effect_Free
14491 (L : List_Id;
14492 Name_Req : Boolean := False;
14493 Variable_Ref : Boolean := False) return Boolean
14495 N : Node_Id;
14497 begin
14498 if L = No_List or else L = Error_List then
14499 return True;
14501 else
14502 N := First (L);
14503 while Present (N) loop
14504 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
14505 return False;
14506 else
14507 Next (N);
14508 end if;
14509 end loop;
14511 return True;
14512 end if;
14513 end Side_Effect_Free;
14515 --------------------------------
14516 -- Side_Effect_Free_Attribute --
14517 --------------------------------
14519 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean is
14520 begin
14521 case Name is
14522 when Name_Input =>
14523 return False;
14525 when Name_Image
14526 | Name_Img
14527 | Name_Wide_Image
14528 | Name_Wide_Wide_Image
14530 -- CodePeer doesn't want to see replicated copies of 'Image calls
14532 return not CodePeer_Mode;
14534 when others =>
14535 return True;
14536 end case;
14537 end Side_Effect_Free_Attribute;
14539 ----------------------------------
14540 -- Silly_Boolean_Array_Not_Test --
14541 ----------------------------------
14543 -- This procedure implements an odd and silly test. We explicitly check
14544 -- for the case where the 'First of the component type is equal to the
14545 -- 'Last of this component type, and if this is the case, we make sure
14546 -- that constraint error is raised. The reason is that the NOT is bound
14547 -- to cause CE in this case, and we will not otherwise catch it.
14549 -- No such check is required for AND and OR, since for both these cases
14550 -- False op False = False, and True op True = True. For the XOR case,
14551 -- see Silly_Boolean_Array_Xor_Test.
14553 -- Believe it or not, this was reported as a bug. Note that nearly always,
14554 -- the test will evaluate statically to False, so the code will be
14555 -- statically removed, and no extra overhead caused.
14557 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
14558 Loc : constant Source_Ptr := Sloc (N);
14559 CT : constant Entity_Id := Component_Type (T);
14561 begin
14562 -- The check we install is
14564 -- constraint_error when
14565 -- component_type'first = component_type'last
14566 -- and then array_type'Length /= 0)
14568 -- We need the last guard because we don't want to raise CE for empty
14569 -- arrays since no out of range values result. (Empty arrays with a
14570 -- component type of True .. True -- very useful -- even the ACATS
14571 -- does not test that marginal case).
14573 Insert_Action (N,
14574 Make_Raise_Constraint_Error (Loc,
14575 Condition =>
14576 Make_And_Then (Loc,
14577 Left_Opnd =>
14578 Make_Op_Eq (Loc,
14579 Left_Opnd =>
14580 Make_Attribute_Reference (Loc,
14581 Prefix => New_Occurrence_Of (CT, Loc),
14582 Attribute_Name => Name_First),
14584 Right_Opnd =>
14585 Make_Attribute_Reference (Loc,
14586 Prefix => New_Occurrence_Of (CT, Loc),
14587 Attribute_Name => Name_Last)),
14589 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
14590 Reason => CE_Range_Check_Failed));
14591 end Silly_Boolean_Array_Not_Test;
14593 ----------------------------------
14594 -- Silly_Boolean_Array_Xor_Test --
14595 ----------------------------------
14597 -- This procedure implements an odd and silly test. We explicitly check
14598 -- for the XOR case where the component type is True .. True, since this
14599 -- will raise constraint error. A special check is required since CE
14600 -- will not be generated otherwise (cf Expand_Packed_Not).
14602 -- No such check is required for AND and OR, since for both these cases
14603 -- False op False = False, and True op True = True, and no check is
14604 -- required for the case of False .. False, since False xor False = False.
14605 -- See also Silly_Boolean_Array_Not_Test
14607 procedure Silly_Boolean_Array_Xor_Test
14608 (N : Node_Id;
14609 R : Node_Id;
14610 T : Entity_Id)
14612 Loc : constant Source_Ptr := Sloc (N);
14613 CT : constant Entity_Id := Component_Type (T);
14615 begin
14616 -- The check we install is
14618 -- constraint_error when
14619 -- Boolean (component_type'First)
14620 -- and then Boolean (component_type'Last)
14621 -- and then array_type'Length /= 0)
14623 -- We need the last guard because we don't want to raise CE for empty
14624 -- arrays since no out of range values result (Empty arrays with a
14625 -- component type of True .. True -- very useful -- even the ACATS
14626 -- does not test that marginal case).
14628 Insert_Action (N,
14629 Make_Raise_Constraint_Error (Loc,
14630 Condition =>
14631 Make_And_Then (Loc,
14632 Left_Opnd =>
14633 Make_And_Then (Loc,
14634 Left_Opnd =>
14635 Convert_To (Standard_Boolean,
14636 Make_Attribute_Reference (Loc,
14637 Prefix => New_Occurrence_Of (CT, Loc),
14638 Attribute_Name => Name_First)),
14640 Right_Opnd =>
14641 Convert_To (Standard_Boolean,
14642 Make_Attribute_Reference (Loc,
14643 Prefix => New_Occurrence_Of (CT, Loc),
14644 Attribute_Name => Name_Last))),
14646 Right_Opnd => Make_Non_Empty_Check (Loc, R)),
14647 Reason => CE_Range_Check_Failed));
14648 end Silly_Boolean_Array_Xor_Test;
14650 ----------------------------
14651 -- Small_Integer_Type_For --
14652 ----------------------------
14654 function Small_Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id
14656 begin
14657 -- The only difference between this and Integer_Type_For is that this
14658 -- can return small (8- or 16-bit) types.
14660 if S <= Standard_Short_Short_Integer_Size then
14661 if Uns then
14662 return Standard_Short_Short_Unsigned;
14663 else
14664 return Standard_Short_Short_Integer;
14665 end if;
14667 elsif S <= Standard_Short_Integer_Size then
14668 if Uns then
14669 return Standard_Short_Unsigned;
14670 else
14671 return Standard_Short_Integer;
14672 end if;
14674 else
14675 return Integer_Type_For (S, Uns);
14676 end if;
14677 end Small_Integer_Type_For;
14679 ------------------
14680 -- Thunk_Target --
14681 ------------------
14683 function Thunk_Target (Thunk : Entity_Id) return Entity_Id is
14684 Target : Entity_Id := Thunk;
14686 begin
14687 pragma Assert (Is_Thunk (Thunk));
14689 while Is_Thunk (Target) loop
14690 Target := Thunk_Entity (Target);
14691 end loop;
14693 return Target;
14694 end Thunk_Target;
14696 -------------------
14697 -- Type_Map_Hash --
14698 -------------------
14700 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
14701 begin
14702 return Type_Map_Header (Id mod Type_Map_Size);
14703 end Type_Map_Hash;
14705 ------------------------------------------
14706 -- Type_May_Have_Bit_Aligned_Components --
14707 ------------------------------------------
14709 function Type_May_Have_Bit_Aligned_Components
14710 (Typ : Entity_Id) return Boolean
14712 begin
14713 -- Array type, check component type
14715 if Is_Array_Type (Typ) then
14716 return
14717 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
14719 -- Record type, check components
14721 elsif Is_Record_Type (Typ) then
14722 declare
14723 E : Entity_Id;
14725 begin
14726 E := First_Component_Or_Discriminant (Typ);
14727 while Present (E) loop
14728 -- This is the crucial test: if the component itself causes
14729 -- trouble, then we can stop and return True.
14731 if Component_May_Be_Bit_Aligned (E) then
14732 return True;
14733 end if;
14735 -- Otherwise, we need to test its type, to see if it may
14736 -- itself contain a troublesome component.
14738 if Type_May_Have_Bit_Aligned_Components (Etype (E)) then
14739 return True;
14740 end if;
14742 Next_Component_Or_Discriminant (E);
14743 end loop;
14745 return False;
14746 end;
14748 -- Type other than array or record is always OK
14750 else
14751 return False;
14752 end if;
14753 end Type_May_Have_Bit_Aligned_Components;
14755 -------------------------------
14756 -- Update_Primitives_Mapping --
14757 -------------------------------
14759 procedure Update_Primitives_Mapping
14760 (Inher_Id : Entity_Id;
14761 Subp_Id : Entity_Id)
14763 Parent_Type : constant Entity_Id := Find_Dispatching_Type (Inher_Id);
14764 Derived_Type : constant Entity_Id := Find_Dispatching_Type (Subp_Id);
14766 begin
14767 pragma Assert (Parent_Type /= Derived_Type);
14768 Map_Types (Parent_Type, Derived_Type);
14769 end Update_Primitives_Mapping;
14771 ----------------------------------
14772 -- Within_Case_Or_If_Expression --
14773 ----------------------------------
14775 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
14776 Nod : Node_Id;
14777 Par : Node_Id;
14779 begin
14780 -- Locate an enclosing case or if expression. Note that these constructs
14781 -- can be expanded into Expression_With_Actions, hence the test of the
14782 -- original node.
14784 Nod := N;
14785 Par := Parent (Nod);
14787 while Present (Par) loop
14788 if Nkind (Original_Node (Par)) = N_Case_Expression
14789 and then Nod /= Expression (Original_Node (Par))
14790 then
14791 return True;
14793 elsif Nkind (Original_Node (Par)) = N_If_Expression
14794 and then Nod /= First (Expressions (Original_Node (Par)))
14795 then
14796 return True;
14798 -- Stop at contexts where temporaries may be contained
14800 elsif Nkind (Par) in N_Aggregate
14801 | N_Delta_Aggregate
14802 | N_Extension_Aggregate
14803 | N_Block_Statement
14804 | N_Loop_Statement
14805 then
14806 return False;
14808 -- Prevent the search from going too far
14810 elsif Is_Body_Or_Package_Declaration (Par) then
14811 return False;
14812 end if;
14814 Nod := Par;
14815 Par := Parent (Nod);
14816 end loop;
14818 return False;
14819 end Within_Case_Or_If_Expression;
14821 ------------------------------
14822 -- Predicate_Check_In_Scope --
14823 ------------------------------
14825 function Predicate_Check_In_Scope (N : Node_Id) return Boolean is
14826 S : Entity_Id;
14828 begin
14829 S := Current_Scope;
14830 while Present (S) and then not Is_Subprogram (S) loop
14831 S := Scope (S);
14832 end loop;
14834 if Present (S) then
14836 -- Predicate checks should only be enabled in init procs for
14837 -- expressions coming from source.
14839 if Is_Init_Proc (S) then
14840 return Comes_From_Source (N);
14842 elsif Get_TSS_Name (S) /= TSS_Null
14843 and then not Is_Predicate_Function (S)
14844 then
14845 return False;
14846 end if;
14847 end if;
14849 return True;
14850 end Predicate_Check_In_Scope;
14852 end Exp_Util;