Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / exp_util.adb
blob80c01bf40fd27cd639bb9d7d7ed11fbb076e03b5
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-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Aggr; use Exp_Aggr;
37 with Exp_Ch6; use Exp_Ch6;
38 with Exp_Ch7; use Exp_Ch7;
39 with Exp_Ch11; use Exp_Ch11;
40 with Freeze; use Freeze;
41 with Ghost; use Ghost;
42 with Inline; use Inline;
43 with Itypes; use Itypes;
44 with Lib; use Lib;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch12; use Sem_Ch12;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Type; use Sem_Type;
62 with Sem_Util; use Sem_Util;
63 with Sinfo.Utils; use Sinfo.Utils;
64 with Snames; use Snames;
65 with Stand; use Stand;
66 with Stringt; use Stringt;
67 with Tbuild; use Tbuild;
68 with Ttypes; use Ttypes;
69 with Validsw; use Validsw;
70 with Warnsw; use Warnsw;
72 with GNAT.HTable;
73 package body Exp_Util is
75 ---------------------------------------------------------
76 -- Handling of inherited class-wide pre/postconditions --
77 ---------------------------------------------------------
79 -- Following AI12-0113, the expression for a class-wide condition is
80 -- transformed for a subprogram that inherits it, by replacing calls
81 -- to primitive operations of the original controlling type into the
82 -- corresponding overriding operations of the derived type. The following
83 -- hash table manages this mapping, and is expanded on demand whenever
84 -- such inherited expression needs to be constructed.
86 -- The mapping is also used to check whether an inherited operation has
87 -- a condition that depends on overridden operations. For such an
88 -- operation we must create a wrapper that is then treated as a normal
89 -- overriding. In SPARK mode such operations are illegal.
91 -- For a given root type there may be several type extensions with their
92 -- own overriding operations, so at various times a given operation of
93 -- the root will be mapped into different overridings. The root type is
94 -- also mapped into the current type extension to indicate that its
95 -- operations are mapped into the overriding operations of that current
96 -- type extension.
98 -- The contents of the map are as follows:
100 -- Key Value
102 -- Discriminant (Entity_Id) Discriminant (Entity_Id)
103 -- Discriminant (Entity_Id) Non-discriminant name (Entity_Id)
104 -- Discriminant (Entity_Id) Expression (Node_Id)
105 -- Primitive subprogram (Entity_Id) Primitive subprogram (Entity_Id)
106 -- Type (Entity_Id) Type (Entity_Id)
108 Type_Map_Size : constant := 511;
110 subtype Type_Map_Header is Integer range 0 .. Type_Map_Size - 1;
111 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header;
113 package Type_Map is new GNAT.HTable.Simple_HTable
114 (Header_Num => Type_Map_Header,
115 Key => Entity_Id,
116 Element => Node_Or_Entity_Id,
117 No_Element => Empty,
118 Hash => Type_Map_Hash,
119 Equal => "=");
121 -----------------------
122 -- Local Subprograms --
123 -----------------------
125 function Build_Task_Array_Image
126 (Loc : Source_Ptr;
127 Id_Ref : Node_Id;
128 A_Type : Entity_Id;
129 Dyn : Boolean := False) return Node_Id;
130 -- Build function to generate the image string for a task that is an array
131 -- component, concatenating the images of each index. To avoid storage
132 -- leaks, the string is built with successive slice assignments. The flag
133 -- Dyn indicates whether this is called for the initialization procedure of
134 -- an array of tasks, or for the name of a dynamically created task that is
135 -- assigned to an indexed component.
137 function Build_Task_Image_Function
138 (Loc : Source_Ptr;
139 Decls : List_Id;
140 Stats : List_Id;
141 Res : Entity_Id) return Node_Id;
142 -- Common processing for Task_Array_Image and Task_Record_Image. Build
143 -- function body that computes image.
145 procedure Build_Task_Image_Prefix
146 (Loc : Source_Ptr;
147 Len : out Entity_Id;
148 Res : out Entity_Id;
149 Pos : out Entity_Id;
150 Prefix : Entity_Id;
151 Sum : Node_Id;
152 Decls : List_Id;
153 Stats : List_Id);
154 -- Common processing for Task_Array_Image and Task_Record_Image. Create
155 -- local variables and assign prefix of name to result string.
157 function Build_Task_Record_Image
158 (Loc : Source_Ptr;
159 Id_Ref : Node_Id;
160 Dyn : Boolean := False) return Node_Id;
161 -- Build function to generate the image string for a task that is a record
162 -- component. Concatenate name of variable with that of selector. The flag
163 -- Dyn indicates whether this is called for the initialization procedure of
164 -- record with task components, or for a dynamically created task that is
165 -- assigned to a selected component.
167 procedure Evaluate_Slice_Bounds (Slice : Node_Id);
168 -- Force evaluation of bounds of a slice, which may be given by a range
169 -- or by a subtype indication with or without a constraint.
171 function Is_Uninitialized_Aggregate
172 (Exp : Node_Id;
173 T : Entity_Id) return Boolean;
174 -- Determine whether an array aggregate used in an object declaration
175 -- is uninitialized, when the aggregate is declared with a box and
176 -- the component type has no default value. Such an aggregate can be
177 -- optimized away to prevent the copying of uninitialized data, and
178 -- the bounds of the aggregate can be propagated directly to the
179 -- object declaration.
181 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean;
182 -- Determine whether pragma Default_Initial_Condition denoted by Prag has
183 -- an assertion expression that should be verified at run time.
185 function Make_CW_Equivalent_Type
186 (T : Entity_Id;
187 E : Node_Id) return Entity_Id;
188 -- T is a class-wide type entity, E is the initial expression node that
189 -- constrains T in case such as: " X: T := E" or "new T'(E)". This function
190 -- returns the entity of the Equivalent type and inserts on the fly the
191 -- necessary declaration such as:
193 -- type anon is record
194 -- _parent : Root_Type (T); constrained with E discriminants (if any)
195 -- Extension : String (1 .. expr to match size of E);
196 -- end record;
198 -- This record is compatible with any object of the class of T thanks to
199 -- the first field and has the same size as E thanks to the second.
201 function Make_Literal_Range
202 (Loc : Source_Ptr;
203 Literal_Typ : Entity_Id) return Node_Id;
204 -- Produce a Range node whose bounds are:
205 -- Low_Bound (Literal_Type) ..
206 -- Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
207 -- this is used for expanding declarations like X : String := "sdfgdfg";
209 -- If the index type of the target array is not integer, we generate:
210 -- Low_Bound (Literal_Type) ..
211 -- Literal_Type'Val
212 -- (Literal_Type'Pos (Low_Bound (Literal_Type))
213 -- + (Length (Literal_Typ) -1))
215 function Make_Non_Empty_Check
216 (Loc : Source_Ptr;
217 N : Node_Id) return Node_Id;
218 -- Produce a boolean expression checking that the unidimensional array
219 -- node N is not empty.
221 function New_Class_Wide_Subtype
222 (CW_Typ : Entity_Id;
223 N : Node_Id) return Entity_Id;
224 -- Create an implicit subtype of CW_Typ attached to node N
226 function Requires_Cleanup_Actions
227 (L : List_Id;
228 Lib_Level : Boolean;
229 Nested_Constructs : Boolean) return Boolean;
230 -- Given a list L, determine whether it contains one of the following:
232 -- 1) controlled objects
233 -- 2) library-level tagged types
235 -- Lib_Level is True when the list comes from a construct at the library
236 -- level, and False otherwise. Nested_Constructs is True when any nested
237 -- packages declared in L must be processed, and False otherwise.
239 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean;
240 -- Return True if the evaluation of the given attribute is considered
241 -- side-effect free, independently of its prefix and expressions.
243 -------------------------------------
244 -- Activate_Atomic_Synchronization --
245 -------------------------------------
247 procedure Activate_Atomic_Synchronization (N : Node_Id) is
248 Msg_Node : Node_Id;
250 begin
251 case Nkind (Parent (N)) is
253 -- Check for cases of appearing in the prefix of a construct where we
254 -- don't need atomic synchronization for this kind of usage.
256 when
257 -- Nothing to do if we are the prefix of an attribute, since we
258 -- do not want an atomic sync operation for things like 'Size.
260 N_Attribute_Reference
262 -- The N_Reference node is like an attribute
264 | N_Reference
266 -- Nothing to do for a reference to a component (or components)
267 -- of a composite object. Only reads and updates of the object
268 -- as a whole require atomic synchronization (RM C.6 (15)).
270 | N_Indexed_Component
271 | N_Selected_Component
272 | N_Slice
274 -- For all the above cases, nothing to do if we are the prefix
276 if Prefix (Parent (N)) = N then
277 return;
278 end if;
280 when others =>
281 null;
282 end case;
284 -- Nothing to do for the identifier in an object renaming declaration,
285 -- the renaming itself does not need atomic synchronization.
287 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
288 return;
289 end if;
291 -- Go ahead and set the flag
293 Set_Atomic_Sync_Required (N);
295 -- Generate info message if requested
297 if Warn_On_Atomic_Synchronization then
298 case Nkind (N) is
299 when N_Identifier =>
300 Msg_Node := N;
302 when N_Expanded_Name
303 | N_Selected_Component
305 Msg_Node := Selector_Name (N);
307 when N_Explicit_Dereference
308 | N_Indexed_Component
310 Msg_Node := Empty;
312 when others =>
313 pragma Assert (False);
314 return;
315 end case;
317 if Present (Msg_Node) then
318 Error_Msg_N
319 ("info: atomic synchronization set for &?.n?", Msg_Node);
320 else
321 Error_Msg_N
322 ("info: atomic synchronization set?.n?", N);
323 end if;
324 end if;
325 end Activate_Atomic_Synchronization;
327 ----------------------
328 -- Adjust_Condition --
329 ----------------------
331 procedure Adjust_Condition (N : Node_Id) is
333 function Is_Hardbool_Type (T : Entity_Id) return Boolean;
334 -- Return True iff T is a type annotated with the
335 -- Machine_Attribute pragma "hardbool".
337 ----------------------
338 -- Is_Hardbool_Type --
339 ----------------------
341 function Is_Hardbool_Type (T : Entity_Id) return Boolean is
343 function Find_Hardbool_Pragma
344 (Id : Entity_Id) return Node_Id;
345 -- Return a Rep_Item associated with entity Id that
346 -- corresponds to the Hardbool Machine_Attribute pragma, if
347 -- any, or Empty otherwise.
349 function Pragma_Arg_To_String (Item : Node_Id) return String is
350 (To_String (Strval (Expr_Value_S (Item))));
351 -- Return the pragma argument Item as a String
353 function Hardbool_Pragma_P (Item : Node_Id) return Boolean is
354 (Nkind (Item) = N_Pragma
355 and then
356 Pragma_Name (Item) = Name_Machine_Attribute
357 and then
358 Pragma_Arg_To_String
359 (Get_Pragma_Arg
360 (Next (First (Pragma_Argument_Associations (Item)))))
361 = "hardbool");
362 -- Return True iff representation Item is a "hardbool"
363 -- Machine_Attribute pragma.
365 --------------------------
366 -- Find_Hardbool_Pragma --
367 --------------------------
369 function Find_Hardbool_Pragma
370 (Id : Entity_Id) return Node_Id
372 Item : Node_Id;
374 begin
375 if not Has_Gigi_Rep_Item (Id) then
376 return Empty;
377 end if;
379 Item := First_Rep_Item (Id);
380 while Present (Item) loop
381 if Hardbool_Pragma_P (Item) then
382 return Item;
383 end if;
384 Item := Next_Rep_Item (Item);
385 end loop;
387 return Empty;
388 end Find_Hardbool_Pragma;
390 -- Start of processing for Is_Hardbool_Type
392 begin
393 return Present (Find_Hardbool_Pragma (T));
394 end Is_Hardbool_Type;
396 -- Start of processing for Adjust_Condition
398 begin
399 if No (N) then
400 return;
401 end if;
403 declare
404 Loc : constant Source_Ptr := Sloc (N);
405 T : constant Entity_Id := Etype (N);
407 begin
408 -- Defend against a call where the argument has no type, or has a
409 -- type that is not Boolean. This can occur because of prior errors.
411 if No (T) or else not Is_Boolean_Type (T) then
412 return;
413 end if;
415 -- Apply validity checking if needed
417 if Validity_Checks_On
418 and then
419 (Validity_Check_Tests or else Is_Hardbool_Type (T))
420 then
421 Ensure_Valid (N);
422 end if;
424 -- Immediate return if standard boolean, the most common case,
425 -- where nothing needs to be done.
427 if Base_Type (T) = Standard_Boolean then
428 return;
429 end if;
431 -- Case of zero/nonzero semantics or nonstandard enumeration
432 -- representation. In each case, we rewrite the node as:
434 -- ityp!(N) /= False'Enum_Rep
436 -- where ityp is an integer type with large enough size to hold any
437 -- value of type T.
439 if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
440 Rewrite (N,
441 Make_Op_Ne (Loc,
442 Left_Opnd =>
443 Unchecked_Convert_To
444 (Integer_Type_For (Esize (T), Uns => False), N),
445 Right_Opnd =>
446 Make_Attribute_Reference (Loc,
447 Attribute_Name => Name_Enum_Rep,
448 Prefix =>
449 New_Occurrence_Of (First_Literal (T), Loc))));
450 Analyze_And_Resolve (N, Standard_Boolean);
452 else
453 Rewrite (N, Convert_To (Standard_Boolean, N));
454 Analyze_And_Resolve (N, Standard_Boolean);
455 end if;
456 end;
457 end Adjust_Condition;
459 ------------------------
460 -- Adjust_Result_Type --
461 ------------------------
463 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
464 begin
465 -- Ignore call if current type is not Standard.Boolean
467 if Etype (N) /= Standard_Boolean then
468 return;
469 end if;
471 -- If result is already of correct type, nothing to do. Note that
472 -- this will get the most common case where everything has a type
473 -- of Standard.Boolean.
475 if Base_Type (T) = Standard_Boolean then
476 return;
478 else
479 declare
480 KP : constant Node_Kind := Nkind (Parent (N));
482 begin
483 -- If result is to be used as a Condition in the syntax, no need
484 -- to convert it back, since if it was changed to Standard.Boolean
485 -- using Adjust_Condition, that is just fine for this usage.
487 if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
488 return;
490 -- If result is an operand of another logical operation, no need
491 -- to reset its type, since Standard.Boolean is just fine, and
492 -- such operations always do Adjust_Condition on their operands.
494 elsif KP in N_Op_Boolean
495 or else KP in N_Short_Circuit
496 or else KP = N_Op_Not
497 or else (KP in N_Type_Conversion
498 | N_Unchecked_Type_Conversion
499 and then Is_Boolean_Type (Etype (Parent (N))))
500 then
501 return;
503 -- Otherwise we perform a conversion from the current type, which
504 -- must be Standard.Boolean, to the desired type. Use the base
505 -- type to prevent spurious constraint checks that are extraneous
506 -- to the transformation. The type and its base have the same
507 -- representation, standard or otherwise.
509 else
510 Set_Analyzed (N);
511 Rewrite (N, Convert_To (Base_Type (T), N));
512 Analyze_And_Resolve (N, Base_Type (T));
513 end if;
514 end;
515 end if;
516 end Adjust_Result_Type;
518 --------------------------
519 -- Append_Freeze_Action --
520 --------------------------
522 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
523 Fnode : Node_Id;
525 begin
526 Ensure_Freeze_Node (T);
527 Fnode := Freeze_Node (T);
529 if No (Actions (Fnode)) then
530 Set_Actions (Fnode, New_List (N));
531 else
532 Append (N, Actions (Fnode));
533 end if;
534 end Append_Freeze_Action;
536 ---------------------------
537 -- Append_Freeze_Actions --
538 ---------------------------
540 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
541 Fnode : Node_Id;
543 begin
544 if No (L) then
545 return;
546 end if;
548 Ensure_Freeze_Node (T);
549 Fnode := Freeze_Node (T);
551 if No (Actions (Fnode)) then
552 Set_Actions (Fnode, L);
553 else
554 Append_List (L, Actions (Fnode));
555 end if;
556 end Append_Freeze_Actions;
558 ----------------------------------------
559 -- Attribute_Constrained_Static_Value --
560 ----------------------------------------
562 function Attribute_Constrained_Static_Value (Pref : Node_Id) return Boolean
564 Ptyp : constant Entity_Id := Etype (Pref);
565 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
567 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
568 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
569 -- view of an aliased object whose subtype is constrained.
571 ---------------------------------
572 -- Is_Constrained_Aliased_View --
573 ---------------------------------
575 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
576 E : Entity_Id;
578 begin
579 if Is_Entity_Name (Obj) then
580 E := Entity (Obj);
582 if Present (Renamed_Object (E)) then
583 return Is_Constrained_Aliased_View (Renamed_Object (E));
584 else
585 return Is_Aliased (E) and then Is_Constrained (Etype (E));
586 end if;
588 else
589 return Is_Aliased_View (Obj)
590 and then
591 (Is_Constrained (Etype (Obj))
592 or else
593 (Nkind (Obj) = N_Explicit_Dereference
594 and then
595 not Object_Type_Has_Constrained_Partial_View
596 (Typ => Base_Type (Etype (Obj)),
597 Scop => Current_Scope)));
598 end if;
599 end Is_Constrained_Aliased_View;
601 -- Start of processing for Attribute_Constrained_Static_Value
603 begin
604 -- We are in a case where the attribute is known statically, and
605 -- implicit dereferences have been rewritten.
607 pragma Assert
608 (not (Present (Formal_Ent)
609 and then Ekind (Formal_Ent) /= E_Constant
610 and then Present (Extra_Constrained (Formal_Ent)))
611 and then
612 not (Is_Access_Type (Etype (Pref))
613 and then (not Is_Entity_Name (Pref)
614 or else Is_Object (Entity (Pref))))
615 and then
616 not (Nkind (Pref) = N_Identifier
617 and then Ekind (Entity (Pref)) = E_Variable
618 and then Present (Extra_Constrained (Entity (Pref)))));
620 if Is_Entity_Name (Pref) then
621 declare
622 Ent : constant Entity_Id := Entity (Pref);
623 Res : Boolean;
625 begin
626 -- (RM J.4) obsolescent cases
628 if Is_Type (Ent) then
630 -- Private type
632 if Is_Private_Type (Ent) then
633 Res := not Has_Discriminants (Ent)
634 or else Is_Constrained (Ent);
636 -- It not a private type, must be a generic actual type
637 -- that corresponded to a private type. We know that this
638 -- correspondence holds, since otherwise the reference
639 -- within the generic template would have been illegal.
641 else
642 if Is_Composite_Type (Underlying_Type (Ent)) then
643 Res := Is_Constrained (Ent);
644 else
645 Res := True;
646 end if;
647 end if;
649 else
651 -- If the prefix is not a variable or is aliased, then
652 -- definitely true; if it's a formal parameter without an
653 -- associated extra formal, then treat it as constrained.
655 -- Ada 2005 (AI-363): An aliased prefix must be known to be
656 -- constrained in order to set the attribute to True.
658 if not Is_Variable (Pref)
659 or else Present (Formal_Ent)
660 or else (Ada_Version < Ada_2005
661 and then Is_Aliased_View (Pref))
662 or else (Ada_Version >= Ada_2005
663 and then Is_Constrained_Aliased_View (Pref))
664 then
665 Res := True;
667 -- Variable case, look at type to see if it is constrained.
668 -- Note that the one case where this is not accurate (the
669 -- procedure formal case), has been handled above.
671 -- We use the Underlying_Type here (and below) in case the
672 -- type is private without discriminants, but the full type
673 -- has discriminants. This case is illegal, but we generate
674 -- it internally for passing to the Extra_Constrained
675 -- parameter.
677 else
678 -- In Ada 2012, test for case of a limited tagged type,
679 -- in which case the attribute is always required to
680 -- return True. The underlying type is tested, to make
681 -- sure we also return True for cases where there is an
682 -- unconstrained object with an untagged limited partial
683 -- view which has defaulted discriminants (such objects
684 -- always produce a False in earlier versions of
685 -- Ada). (Ada 2012: AI05-0214)
687 Res :=
688 Is_Constrained (Underlying_Type (Etype (Ent)))
689 or else
690 (Ada_Version >= Ada_2012
691 and then Is_Tagged_Type (Underlying_Type (Ptyp))
692 and then Is_Limited_Type (Ptyp));
693 end if;
694 end if;
696 return Res;
697 end;
699 -- Prefix is not an entity name. These are also cases where we can
700 -- always tell at compile time by looking at the form and type of the
701 -- prefix. If an explicit dereference of an object with constrained
702 -- partial view, this is unconstrained (Ada 2005: AI95-0363). If the
703 -- underlying type is a limited tagged type, then Constrained is
704 -- required to always return True (Ada 2012: AI05-0214).
706 else
707 return not Is_Variable (Pref)
708 or else
709 (Nkind (Pref) = N_Explicit_Dereference
710 and then
711 not Object_Type_Has_Constrained_Partial_View
712 (Typ => Base_Type (Ptyp),
713 Scop => Current_Scope))
714 or else Is_Constrained (Underlying_Type (Ptyp))
715 or else (Ada_Version >= Ada_2012
716 and then Is_Tagged_Type (Underlying_Type (Ptyp))
717 and then Is_Limited_Type (Ptyp));
718 end if;
719 end Attribute_Constrained_Static_Value;
721 ------------------------------------
722 -- Build_Allocate_Deallocate_Proc --
723 ------------------------------------
725 procedure Build_Allocate_Deallocate_Proc
726 (N : Node_Id;
727 Is_Allocate : Boolean)
729 function Find_Object (E : Node_Id) return Node_Id;
730 -- Given an arbitrary expression of an allocator, try to find an object
731 -- reference in it, otherwise return the original expression.
733 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean;
734 -- Determine whether subprogram Subp denotes a custom allocate or
735 -- deallocate.
737 -----------------
738 -- Find_Object --
739 -----------------
741 function Find_Object (E : Node_Id) return Node_Id is
742 Expr : Node_Id;
744 begin
745 pragma Assert (Is_Allocate);
747 Expr := E;
748 loop
749 if Nkind (Expr) = N_Explicit_Dereference then
750 Expr := Prefix (Expr);
752 elsif Nkind (Expr) = N_Qualified_Expression then
753 Expr := Expression (Expr);
755 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
757 -- When interface class-wide types are involved in allocation,
758 -- the expander introduces several levels of address arithmetic
759 -- to perform dispatch table displacement. In this scenario the
760 -- object appears as:
762 -- Tag_Ptr (Base_Address (<object>'Address))
764 -- Detect this case and utilize the whole expression as the
765 -- "object" since it now points to the proper dispatch table.
767 if Is_RTE (Etype (Expr), RE_Tag_Ptr) then
768 exit;
770 -- Continue to strip the object
772 else
773 Expr := Expression (Expr);
774 end if;
776 else
777 exit;
778 end if;
779 end loop;
781 return Expr;
782 end Find_Object;
784 ---------------------------------
785 -- Is_Allocate_Deallocate_Proc --
786 ---------------------------------
788 function Is_Allocate_Deallocate_Proc (Subp : Entity_Id) return Boolean is
789 begin
790 -- Look for a subprogram body with only one statement which is a
791 -- call to Allocate_Any_Controlled / Deallocate_Any_Controlled.
793 if Ekind (Subp) = E_Procedure
794 and then Nkind (Parent (Parent (Subp))) = N_Subprogram_Body
795 then
796 declare
797 HSS : constant Node_Id :=
798 Handled_Statement_Sequence (Parent (Parent (Subp)));
799 Proc : Entity_Id;
801 begin
802 if Present (Statements (HSS))
803 and then Nkind (First (Statements (HSS))) =
804 N_Procedure_Call_Statement
805 then
806 Proc := Entity (Name (First (Statements (HSS))));
808 return
809 Is_RTE (Proc, RE_Allocate_Any_Controlled)
810 or else Is_RTE (Proc, RE_Deallocate_Any_Controlled);
811 end if;
812 end;
813 end if;
815 return False;
816 end Is_Allocate_Deallocate_Proc;
818 -- Local variables
820 Desig_Typ : Entity_Id;
821 Expr : Node_Id;
822 Needs_Fin : Boolean;
823 Pool_Id : Entity_Id;
824 Proc_To_Call : Node_Id := Empty;
825 Ptr_Typ : Entity_Id;
826 Use_Secondary_Stack_Pool : Boolean;
828 -- Start of processing for Build_Allocate_Deallocate_Proc
830 begin
831 -- Obtain the attributes of the allocation / deallocation
833 if Nkind (N) = N_Free_Statement then
834 Expr := Expression (N);
835 Ptr_Typ := Base_Type (Etype (Expr));
836 Proc_To_Call := Procedure_To_Call (N);
838 else
839 if Nkind (N) = N_Object_Declaration then
840 Expr := Expression (N);
841 else
842 Expr := N;
843 end if;
845 -- In certain cases an allocator with a qualified expression may
846 -- be relocated and used as the initialization expression of a
847 -- temporary:
849 -- before:
850 -- Obj : Ptr_Typ := new Desig_Typ'(...);
852 -- after:
853 -- Tmp : Ptr_Typ := new Desig_Typ'(...);
854 -- Obj : Ptr_Typ := Tmp;
856 -- Since the allocator is always marked as analyzed to avoid infinite
857 -- expansion, it will never be processed by this routine given that
858 -- the designated type needs finalization actions. Detect this case
859 -- and complete the expansion of the allocator.
861 if Nkind (Expr) = N_Identifier
862 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
863 and then Nkind (Expression (Parent (Entity (Expr)))) = N_Allocator
864 then
865 Build_Allocate_Deallocate_Proc (Parent (Entity (Expr)), True);
866 return;
867 end if;
869 -- The allocator may have been rewritten into something else in which
870 -- case the expansion performed by this routine does not apply.
872 if Nkind (Expr) /= N_Allocator then
873 return;
874 end if;
876 Ptr_Typ := Base_Type (Etype (Expr));
877 Proc_To_Call := Procedure_To_Call (Expr);
878 end if;
880 Pool_Id := Associated_Storage_Pool (Ptr_Typ);
881 Desig_Typ := Available_View (Designated_Type (Ptr_Typ));
883 -- Handle concurrent types
885 if Is_Concurrent_Type (Desig_Typ)
886 and then Present (Corresponding_Record_Type (Desig_Typ))
887 then
888 Desig_Typ := Corresponding_Record_Type (Desig_Typ);
889 end if;
891 Use_Secondary_Stack_Pool :=
892 Is_RTE (Pool_Id, RE_SS_Pool)
893 or else (Nkind (Expr) = N_Allocator
894 and then Is_RTE (Storage_Pool (Expr), RE_SS_Pool));
896 -- Do not process allocations / deallocations without a pool
898 if No (Pool_Id) then
899 return;
901 -- Do not process allocations from the return stack
903 elsif Is_RTE (Pool_Id, RE_RS_Pool) then
904 return;
906 -- Do not process allocations on / deallocations from the secondary
907 -- stack, except for access types used to implement indirect temps.
909 elsif Use_Secondary_Stack_Pool
910 and then not Old_Attr_Util.Indirect_Temps
911 .Is_Access_Type_For_Indirect_Temp (Ptr_Typ)
912 then
913 return;
915 -- Optimize the case where we are using the default Global_Pool_Object,
916 -- and we don't need the heavy finalization machinery.
918 elsif Is_RTE (Pool_Id, RE_Global_Pool_Object)
919 and then not Needs_Finalization (Desig_Typ)
920 then
921 return;
923 -- Do not replicate the machinery if the allocator / free has already
924 -- been expanded and has a custom Allocate / Deallocate.
926 elsif Present (Proc_To_Call)
927 and then Is_Allocate_Deallocate_Proc (Proc_To_Call)
928 then
929 return;
930 end if;
932 -- Finalization actions are required when the object to be allocated or
933 -- deallocated needs these actions and the associated access type is not
934 -- subject to pragma No_Heap_Finalization.
936 Needs_Fin :=
937 Needs_Finalization (Desig_Typ)
938 and then not No_Heap_Finalization (Ptr_Typ);
940 if Needs_Fin then
942 -- Do nothing if the access type may never allocate / deallocate
943 -- objects.
945 if No_Pool_Assigned (Ptr_Typ) then
946 return;
947 end if;
949 -- The allocation / deallocation of a controlled object must be
950 -- chained on / detached from a finalization master.
952 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
954 -- The only other kind of allocation / deallocation supported by this
955 -- routine is on / from a subpool.
957 elsif Nkind (Expr) = N_Allocator
958 and then No (Subpool_Handle_Name (Expr))
959 then
960 return;
961 end if;
963 declare
964 Loc : constant Source_Ptr := Sloc (N);
965 Addr_Id : constant Entity_Id := Make_Temporary (Loc, 'A');
966 Alig_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
967 Proc_Id : constant Entity_Id := Make_Temporary (Loc, 'P');
968 Size_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
970 Actuals : List_Id;
971 Alloc_Nod : Node_Id := Empty;
972 Alloc_Expr : Node_Id := Empty;
973 Fin_Addr_Id : Entity_Id;
974 Fin_Mas_Act : Node_Id;
975 Fin_Mas_Id : Entity_Id;
976 Proc_To_Call : Entity_Id;
977 Subpool : Node_Id := Empty;
979 begin
980 -- When we are building an allocator procedure, extract the allocator
981 -- node for later processing and calculation of alignment.
983 if Is_Allocate then
985 if Nkind (Expr) = N_Allocator then
986 Alloc_Nod := Expr;
988 -- When Expr is an object declaration we have to examine its
989 -- expression.
991 elsif Nkind (Expr) = N_Object_Declaration
992 and then Nkind (Expression (Expr)) = N_Allocator
993 then
994 Alloc_Nod := Expression (Expr);
996 -- Otherwise, we raise an error because we should have found one
998 else
999 raise Program_Error;
1000 end if;
1002 -- Extract the qualified expression if there is one from the
1003 -- allocator.
1005 if Nkind (Expression (Alloc_Nod)) = N_Qualified_Expression then
1006 Alloc_Expr := Expression (Alloc_Nod);
1007 end if;
1008 end if;
1010 -- Step 1: Construct all the actuals for the call to library routine
1011 -- Allocate_Any_Controlled / Deallocate_Any_Controlled.
1013 -- a) Storage pool
1015 Actuals := New_List (New_Occurrence_Of (Pool_Id, Loc));
1017 if Is_Allocate then
1019 -- b) Subpool
1021 if Nkind (Expr) = N_Allocator then
1022 Subpool := Subpool_Handle_Name (Expr);
1023 end if;
1025 -- If a subpool is present it can be an arbitrary name, so make
1026 -- the actual by copying the tree.
1028 if Present (Subpool) then
1029 Append_To (Actuals, New_Copy_Tree (Subpool, New_Sloc => Loc));
1030 else
1031 Append_To (Actuals, Make_Null (Loc));
1032 end if;
1034 -- c) Finalization master
1036 if Needs_Fin then
1037 Fin_Mas_Id := Finalization_Master (Ptr_Typ);
1038 Fin_Mas_Act := New_Occurrence_Of (Fin_Mas_Id, Loc);
1040 -- Handle the case where the master is actually a pointer to a
1041 -- master. This case arises in build-in-place functions.
1043 if Is_Access_Type (Etype (Fin_Mas_Id)) then
1044 Append_To (Actuals, Fin_Mas_Act);
1045 else
1046 Append_To (Actuals,
1047 Make_Attribute_Reference (Loc,
1048 Prefix => Fin_Mas_Act,
1049 Attribute_Name => Name_Unrestricted_Access));
1050 end if;
1051 else
1052 Append_To (Actuals, Make_Null (Loc));
1053 end if;
1055 -- d) Finalize_Address
1057 -- Primitive Finalize_Address is never generated in CodePeer mode
1058 -- since it contains an Unchecked_Conversion.
1060 if Needs_Fin and then not CodePeer_Mode then
1061 Fin_Addr_Id := Finalize_Address (Desig_Typ);
1062 pragma Assert (Present (Fin_Addr_Id));
1064 Append_To (Actuals,
1065 Make_Attribute_Reference (Loc,
1066 Prefix => New_Occurrence_Of (Fin_Addr_Id, Loc),
1067 Attribute_Name => Name_Unrestricted_Access));
1068 else
1069 Append_To (Actuals, Make_Null (Loc));
1070 end if;
1071 end if;
1073 -- e) Address
1074 -- f) Storage_Size
1075 -- g) Alignment
1077 Append_To (Actuals, New_Occurrence_Of (Addr_Id, Loc));
1078 Append_To (Actuals, New_Occurrence_Of (Size_Id, Loc));
1080 -- Class-wide allocations without expressions and non-class-wide
1081 -- allocations can be performed without getting the alignment from
1082 -- the type's Type Specific Record.
1084 if ((Is_Allocate and then No (Alloc_Expr))
1085 or else
1086 not Is_Class_Wide_Type (Desig_Typ))
1087 and then not Use_Secondary_Stack_Pool
1088 then
1089 Append_To (Actuals, New_Occurrence_Of (Alig_Id, Loc));
1091 -- For operations on class-wide types we obtain the value of
1092 -- alignment from the Type Specific Record of the relevant object.
1093 -- This is needed because the frontend expansion of class-wide types
1094 -- into equivalent types confuses the back end.
1096 else
1097 -- Generate:
1098 -- Obj.all'Alignment
1099 -- or
1100 -- Alloc_Expr'Alignment
1102 -- ... because 'Alignment applied to class-wide types is expanded
1103 -- into the code that reads the value of alignment from the TSD
1104 -- (see Expand_N_Attribute_Reference)
1106 -- In the Use_Secondary_Stack_Pool case, Alig_Id is not
1107 -- passed in and therefore must not be referenced.
1109 Append_To (Actuals,
1110 Unchecked_Convert_To (RTE (RE_Storage_Offset),
1111 Make_Attribute_Reference (Loc,
1112 Prefix =>
1113 (if No (Alloc_Expr) then
1114 Make_Explicit_Dereference (Loc, Relocate_Node (Expr))
1115 else
1116 Relocate_Node (Expression (Alloc_Expr))),
1117 Attribute_Name => Name_Alignment)));
1118 end if;
1120 -- h) Is_Controlled
1122 if Needs_Fin then
1123 Is_Controlled : declare
1124 Flag_Id : constant Entity_Id := Make_Temporary (Loc, 'F');
1125 Flag_Expr : Node_Id;
1126 Param : Node_Id;
1127 Pref : Node_Id;
1128 Temp : Node_Id;
1130 begin
1131 if Is_Allocate then
1132 Temp := Find_Object (Expression (Expr));
1133 else
1134 Temp := Expr;
1135 end if;
1137 -- Processing for allocations where the expression is a subtype
1138 -- indication.
1140 if Is_Allocate
1141 and then Is_Entity_Name (Temp)
1142 and then Is_Type (Entity (Temp))
1143 then
1144 Flag_Expr :=
1145 New_Occurrence_Of
1146 (Boolean_Literals
1147 (Needs_Finalization (Entity (Temp))), Loc);
1149 -- The allocation / deallocation of a class-wide object relies
1150 -- on a runtime check to determine whether the object is truly
1151 -- controlled or not. Depending on this check, the finalization
1152 -- machinery will request or reclaim extra storage reserved for
1153 -- a list header.
1155 elsif Is_Class_Wide_Type (Desig_Typ) then
1157 -- Detect a special case where interface class-wide types
1158 -- are involved as the object appears as:
1160 -- Tag_Ptr (Base_Address (<object>'Address))
1162 -- The expression already yields the proper tag, generate:
1164 -- Temp.all
1166 if Is_RTE (Etype (Temp), RE_Tag_Ptr) then
1167 Param :=
1168 Make_Explicit_Dereference (Loc,
1169 Prefix => Relocate_Node (Temp));
1171 -- In the default case, obtain the tag of the object about
1172 -- to be allocated / deallocated. Generate:
1174 -- Temp'Tag
1176 -- If the object is an unchecked conversion (typically to
1177 -- an access to class-wide type), we must preserve the
1178 -- conversion to ensure that the object is seen as tagged
1179 -- in the code that follows.
1181 else
1182 Pref := Temp;
1184 if Nkind (Parent (Pref)) = N_Unchecked_Type_Conversion
1185 then
1186 Pref := Parent (Pref);
1187 end if;
1189 Param :=
1190 Make_Attribute_Reference (Loc,
1191 Prefix => Relocate_Node (Pref),
1192 Attribute_Name => Name_Tag);
1193 end if;
1195 -- Generate:
1196 -- Needs_Finalization (<Param>)
1198 Flag_Expr :=
1199 Make_Function_Call (Loc,
1200 Name =>
1201 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
1202 Parameter_Associations => New_List (Param));
1204 -- Processing for generic actuals
1206 elsif Is_Generic_Actual_Type (Desig_Typ) then
1207 Flag_Expr :=
1208 New_Occurrence_Of (Boolean_Literals
1209 (Needs_Finalization (Base_Type (Desig_Typ))), Loc);
1211 -- The object does not require any specialized checks, it is
1212 -- known to be controlled.
1214 else
1215 Flag_Expr := New_Occurrence_Of (Standard_True, Loc);
1216 end if;
1218 -- Create the temporary which represents the finalization state
1219 -- of the expression. Generate:
1221 -- F : constant Boolean := <Flag_Expr>;
1223 Insert_Action (N,
1224 Make_Object_Declaration (Loc,
1225 Defining_Identifier => Flag_Id,
1226 Constant_Present => True,
1227 Object_Definition =>
1228 New_Occurrence_Of (Standard_Boolean, Loc),
1229 Expression => Flag_Expr));
1231 Append_To (Actuals, New_Occurrence_Of (Flag_Id, Loc));
1232 end Is_Controlled;
1234 -- The object is not controlled
1236 else
1237 Append_To (Actuals, New_Occurrence_Of (Standard_False, Loc));
1238 end if;
1240 -- i) On_Subpool
1242 if Is_Allocate then
1243 Append_To (Actuals,
1244 New_Occurrence_Of (Boolean_Literals (Present (Subpool)), Loc));
1245 end if;
1247 -- Step 2: Build a wrapper Allocate / Deallocate which internally
1248 -- calls Allocate_Any_Controlled / Deallocate_Any_Controlled.
1250 -- Select the proper routine to call
1252 if Is_Allocate then
1253 Proc_To_Call := RTE (RE_Allocate_Any_Controlled);
1254 else
1255 Proc_To_Call := RTE (RE_Deallocate_Any_Controlled);
1256 end if;
1258 -- Create a custom Allocate / Deallocate routine which has identical
1259 -- profile to that of System.Storage_Pools.
1261 declare
1262 -- P : Root_Storage_Pool
1263 function Pool_Param return Node_Id is (
1264 Make_Parameter_Specification (Loc,
1265 Defining_Identifier => Make_Temporary (Loc, 'P'),
1266 Parameter_Type =>
1267 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc)));
1269 -- A : [out] Address
1270 function Address_Param return Node_Id is (
1271 Make_Parameter_Specification (Loc,
1272 Defining_Identifier => Addr_Id,
1273 Out_Present => Is_Allocate,
1274 Parameter_Type =>
1275 New_Occurrence_Of (RTE (RE_Address), Loc)));
1277 -- S : Storage_Count
1278 function Size_Param return Node_Id is (
1279 Make_Parameter_Specification (Loc,
1280 Defining_Identifier => Size_Id,
1281 Parameter_Type =>
1282 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1284 -- L : Storage_Count
1285 function Alignment_Param return Node_Id is (
1286 Make_Parameter_Specification (Loc,
1287 Defining_Identifier => Alig_Id,
1288 Parameter_Type =>
1289 New_Occurrence_Of (RTE (RE_Storage_Count), Loc)));
1291 Formal_Params : List_Id;
1292 begin
1293 if Use_Secondary_Stack_Pool then
1294 -- Gigi expects a different profile in the Secondary_Stack_Pool
1295 -- case. There must be no uses of the two missing formals
1296 -- (i.e., Pool_Param and Alignment_Param) in this case.
1297 Formal_Params := New_List
1298 (Address_Param, Size_Param, Alignment_Param);
1299 else
1300 Formal_Params := New_List (
1301 Pool_Param, Address_Param, Size_Param, Alignment_Param);
1302 end if;
1304 Insert_Action (N,
1305 Make_Subprogram_Body (Loc,
1306 Specification =>
1307 -- procedure Pnn
1308 Make_Procedure_Specification (Loc,
1309 Defining_Unit_Name => Proc_Id,
1310 Parameter_Specifications => Formal_Params),
1312 Declarations => No_List,
1314 Handled_Statement_Sequence =>
1315 Make_Handled_Sequence_Of_Statements (Loc,
1316 Statements => New_List (
1317 Make_Procedure_Call_Statement (Loc,
1318 Name =>
1319 New_Occurrence_Of (Proc_To_Call, Loc),
1320 Parameter_Associations => Actuals)))),
1321 Suppress => All_Checks);
1322 end;
1324 -- The newly generated Allocate / Deallocate becomes the default
1325 -- procedure to call when the back end processes the allocation /
1326 -- deallocation.
1328 if Is_Allocate then
1329 Set_Procedure_To_Call (Expr, Proc_Id);
1330 else
1331 Set_Procedure_To_Call (N, Proc_Id);
1332 end if;
1333 end;
1334 end Build_Allocate_Deallocate_Proc;
1336 -------------------------------
1337 -- Build_Abort_Undefer_Block --
1338 -------------------------------
1340 function Build_Abort_Undefer_Block
1341 (Loc : Source_Ptr;
1342 Stmts : List_Id;
1343 Context : Node_Id) return Node_Id
1345 Exceptions_OK : constant Boolean :=
1346 not Restriction_Active (No_Exception_Propagation);
1348 AUD : Entity_Id;
1349 Blk : Node_Id;
1350 Blk_Id : Entity_Id;
1351 HSS : Node_Id;
1353 begin
1354 -- The block should be generated only when undeferring abort in the
1355 -- context of a potential exception.
1357 pragma Assert (Abort_Allowed and Exceptions_OK);
1359 -- Generate:
1360 -- begin
1361 -- <Stmts>
1362 -- at end
1363 -- Abort_Undefer_Direct;
1364 -- end;
1366 AUD := RTE (RE_Abort_Undefer_Direct);
1368 HSS :=
1369 Make_Handled_Sequence_Of_Statements (Loc,
1370 Statements => Stmts,
1371 At_End_Proc => New_Occurrence_Of (AUD, Loc));
1373 Blk :=
1374 Make_Block_Statement (Loc,
1375 Handled_Statement_Sequence => HSS);
1376 Set_Is_Abort_Block (Blk);
1378 Add_Block_Identifier (Blk, Blk_Id);
1379 Expand_At_End_Handler (HSS, Blk_Id);
1381 -- Present the Abort_Undefer_Direct function to the back end to inline
1382 -- the call to the routine.
1384 Add_Inlined_Body (AUD, Context);
1386 return Blk;
1387 end Build_Abort_Undefer_Block;
1389 ---------------------------------
1390 -- Build_Class_Wide_Expression --
1391 ---------------------------------
1393 procedure Build_Class_Wide_Expression
1394 (Pragma_Or_Expr : Node_Id;
1395 Subp : Entity_Id;
1396 Par_Subp : Entity_Id;
1397 Adjust_Sloc : Boolean)
1399 function Replace_Entity (N : Node_Id) return Traverse_Result;
1400 -- Replace reference to formal of inherited operation or to primitive
1401 -- operation of root type, with corresponding entity for derived type,
1402 -- when constructing the class-wide condition of an overriding
1403 -- subprogram.
1405 --------------------
1406 -- Replace_Entity --
1407 --------------------
1409 function Replace_Entity (N : Node_Id) return Traverse_Result is
1410 New_E : Entity_Id;
1412 begin
1413 if Adjust_Sloc then
1414 Adjust_Inherited_Pragma_Sloc (N);
1415 end if;
1417 if Nkind (N) in N_Identifier | N_Expanded_Name | N_Operator_Symbol
1418 and then Present (Entity (N))
1419 and then
1420 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1421 and then
1422 (Nkind (Parent (N)) /= N_Attribute_Reference
1423 or else Attribute_Name (Parent (N)) /= Name_Class)
1424 then
1425 -- The replacement does not apply to dispatching calls within the
1426 -- condition, but only to calls whose static tag is that of the
1427 -- parent type.
1429 if Is_Subprogram (Entity (N))
1430 and then Nkind (Parent (N)) = N_Function_Call
1431 and then Present (Controlling_Argument (Parent (N)))
1432 then
1433 return OK;
1434 end if;
1436 -- Determine whether entity has a renaming
1438 New_E := Type_Map.Get (Entity (N));
1440 if Present (New_E) then
1441 Rewrite (N, New_Occurrence_Of (New_E, Sloc (N)));
1442 end if;
1444 -- Update type of function call node, which should be the same as
1445 -- the function's return type.
1447 if Is_Subprogram (Entity (N))
1448 and then Nkind (Parent (N)) = N_Function_Call
1449 then
1450 Set_Etype (Parent (N), Etype (Entity (N)));
1451 end if;
1453 -- The whole expression will be reanalyzed
1455 elsif Nkind (N) in N_Has_Etype then
1456 Set_Analyzed (N, False);
1457 end if;
1459 return OK;
1460 end Replace_Entity;
1462 procedure Replace_Condition_Entities is
1463 new Traverse_Proc (Replace_Entity);
1465 -- Local variables
1467 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Par_Subp);
1468 Subp_Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
1470 -- Start of processing for Build_Class_Wide_Expression
1472 begin
1473 pragma Assert (Par_Typ /= Subp_Typ);
1475 Update_Primitives_Mapping (Par_Subp, Subp);
1476 Map_Formals (Par_Subp, Subp);
1477 Replace_Condition_Entities (Pragma_Or_Expr);
1478 end Build_Class_Wide_Expression;
1480 --------------------
1481 -- Build_DIC_Call --
1482 --------------------
1484 function Build_DIC_Call
1485 (Loc : Source_Ptr;
1486 Obj_Name : Node_Id;
1487 Typ : Entity_Id) return Node_Id
1489 Proc_Id : constant Entity_Id := DIC_Procedure (Typ);
1490 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1492 begin
1493 -- The DIC procedure has a null body if assertions are disabled or
1494 -- Assertion_Policy Ignore is in effect. In that case, it would be
1495 -- nice to generate a null statement instead of a call to the DIC
1496 -- procedure, but doing that seems to interfere with the determination
1497 -- of ECRs (early call regions) in SPARK. ???
1499 return
1500 Make_Procedure_Call_Statement (Loc,
1501 Name => New_Occurrence_Of (Proc_Id, Loc),
1502 Parameter_Associations => New_List (
1503 Unchecked_Convert_To (Formal_Typ, Obj_Name)));
1504 end Build_DIC_Call;
1506 ------------------------------
1507 -- Build_DIC_Procedure_Body --
1508 ------------------------------
1510 -- WARNING: This routine manages Ghost regions. Return statements must be
1511 -- replaced by gotos which jump to the end of the routine and restore the
1512 -- Ghost mode.
1514 procedure Build_DIC_Procedure_Body
1515 (Typ : Entity_Id;
1516 Partial_DIC : Boolean := False)
1518 Pragmas_Seen : Elist_Id := No_Elist;
1519 -- This list contains all DIC pragmas processed so far. The list is used
1520 -- to avoid redundant Default_Initial_Condition checks.
1522 procedure Add_DIC_Check
1523 (DIC_Prag : Node_Id;
1524 DIC_Expr : Node_Id;
1525 Stmts : in out List_Id);
1526 -- Subsidiary to all Add_xxx_DIC routines. Add a runtime check to verify
1527 -- assertion expression DIC_Expr of pragma DIC_Prag. All generated code
1528 -- is added to list Stmts.
1530 procedure Add_Inherited_DIC
1531 (DIC_Prag : Node_Id;
1532 Par_Typ : Entity_Id;
1533 Deriv_Typ : Entity_Id;
1534 Stmts : in out List_Id);
1535 -- Add a runtime check to verify the assertion expression of inherited
1536 -- pragma DIC_Prag. Par_Typ is parent type, which is also the owner of
1537 -- the DIC pragma. Deriv_Typ is the derived type inheriting the DIC
1538 -- pragma. All generated code is added to list Stmts.
1540 procedure Add_Inherited_Tagged_DIC
1541 (DIC_Prag : Node_Id;
1542 Expr : Node_Id;
1543 Stmts : in out List_Id);
1544 -- Add a runtime check to verify assertion expression DIC_Expr of
1545 -- inherited pragma DIC_Prag. This routine applies class-wide pre-
1546 -- and postcondition-like runtime semantics to the check. Expr is
1547 -- the assertion expression after substitution has been performed
1548 -- (via Replace_References). All generated code is added to list Stmts.
1550 procedure Add_Inherited_DICs
1551 (T : Entity_Id;
1552 Priv_Typ : Entity_Id;
1553 Full_Typ : Entity_Id;
1554 Obj_Id : Entity_Id;
1555 Checks : in out List_Id);
1556 -- Generate a DIC check for each inherited Default_Initial_Condition
1557 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
1558 -- the partial and full view of the parent type. Obj_Id denotes the
1559 -- entity of the _object formal parameter of the DIC procedure. All
1560 -- created checks are added to list Checks.
1562 procedure Add_Own_DIC
1563 (DIC_Prag : Node_Id;
1564 DIC_Typ : Entity_Id;
1565 Obj_Id : Entity_Id;
1566 Stmts : in out List_Id);
1567 -- Add a runtime check to verify the assertion expression of pragma
1568 -- DIC_Prag. DIC_Typ is the owner of the DIC pragma. Obj_Id is the
1569 -- object to substitute in the assertion expression for any references
1570 -- to the current instance of the type All generated code is added to
1571 -- list Stmts.
1573 procedure Add_Parent_DICs
1574 (T : Entity_Id;
1575 Obj_Id : Entity_Id;
1576 Checks : in out List_Id);
1577 -- Generate a Default_Initial_Condition check for each inherited DIC
1578 -- aspect coming from all parent types of type T. Obj_Id denotes the
1579 -- entity of the _object formal parameter of the DIC procedure. All
1580 -- created checks are added to list Checks.
1582 -------------------
1583 -- Add_DIC_Check --
1584 -------------------
1586 procedure Add_DIC_Check
1587 (DIC_Prag : Node_Id;
1588 DIC_Expr : Node_Id;
1589 Stmts : in out List_Id)
1591 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1592 Nam : constant Name_Id := Original_Aspect_Pragma_Name (DIC_Prag);
1594 begin
1595 -- The DIC pragma is ignored, nothing left to do
1597 if Is_Ignored (DIC_Prag) then
1598 null;
1600 -- Otherwise the DIC expression must be checked at run time.
1601 -- Generate:
1603 -- pragma Check (<Nam>, <DIC_Expr>);
1605 else
1606 Append_New_To (Stmts,
1607 Make_Pragma (Loc,
1608 Pragma_Identifier =>
1609 Make_Identifier (Loc, Name_Check),
1611 Pragma_Argument_Associations => New_List (
1612 Make_Pragma_Argument_Association (Loc,
1613 Expression => Make_Identifier (Loc, Nam)),
1615 Make_Pragma_Argument_Association (Loc,
1616 Expression => DIC_Expr))));
1617 end if;
1619 -- Add the pragma to the list of processed pragmas
1621 Append_New_Elmt (DIC_Prag, Pragmas_Seen);
1622 end Add_DIC_Check;
1624 -----------------------
1625 -- Add_Inherited_DIC --
1626 -----------------------
1628 procedure Add_Inherited_DIC
1629 (DIC_Prag : Node_Id;
1630 Par_Typ : Entity_Id;
1631 Deriv_Typ : Entity_Id;
1632 Stmts : in out List_Id)
1634 Deriv_Proc : constant Entity_Id := DIC_Procedure (Deriv_Typ);
1635 Deriv_Obj : constant Entity_Id := First_Entity (Deriv_Proc);
1636 Par_Proc : constant Entity_Id := DIC_Procedure (Par_Typ);
1637 Par_Obj : constant Entity_Id := First_Entity (Par_Proc);
1638 Loc : constant Source_Ptr := Sloc (DIC_Prag);
1640 begin
1641 pragma Assert (Present (Deriv_Proc) and then Present (Par_Proc));
1643 -- Verify the inherited DIC assertion expression by calling the DIC
1644 -- procedure of the parent type.
1646 -- Generate:
1647 -- <Par_Typ>DIC (Par_Typ (_object));
1649 Append_New_To (Stmts,
1650 Make_Procedure_Call_Statement (Loc,
1651 Name => New_Occurrence_Of (Par_Proc, Loc),
1652 Parameter_Associations => New_List (
1653 Convert_To
1654 (Typ => Etype (Par_Obj),
1655 Expr => New_Occurrence_Of (Deriv_Obj, Loc)))));
1656 end Add_Inherited_DIC;
1658 ------------------------------
1659 -- Add_Inherited_Tagged_DIC --
1660 ------------------------------
1662 procedure Add_Inherited_Tagged_DIC
1663 (DIC_Prag : Node_Id;
1664 Expr : Node_Id;
1665 Stmts : in out List_Id)
1667 begin
1668 -- Once the DIC assertion expression is fully processed, add a check
1669 -- to the statements of the DIC procedure.
1671 Add_DIC_Check
1672 (DIC_Prag => DIC_Prag,
1673 DIC_Expr => Expr,
1674 Stmts => Stmts);
1675 end Add_Inherited_Tagged_DIC;
1677 ------------------------
1678 -- Add_Inherited_DICs --
1679 ------------------------
1681 procedure Add_Inherited_DICs
1682 (T : Entity_Id;
1683 Priv_Typ : Entity_Id;
1684 Full_Typ : Entity_Id;
1685 Obj_Id : Entity_Id;
1686 Checks : in out List_Id)
1688 Deriv_Typ : Entity_Id;
1689 Expr : Node_Id;
1690 Prag : Node_Id;
1691 Prag_Expr : Node_Id;
1692 Prag_Expr_Arg : Node_Id;
1693 Prag_Typ : Node_Id;
1694 Prag_Typ_Arg : Node_Id;
1696 Par_Proc : Entity_Id;
1697 -- The "partial" invariant procedure of Par_Typ
1699 Par_Typ : Entity_Id;
1700 -- The suitable view of the parent type used in the substitution of
1701 -- type attributes.
1703 begin
1704 if No (Priv_Typ) and then No (Full_Typ) then
1705 return;
1706 end if;
1708 -- When the type inheriting the class-wide invariant is a concurrent
1709 -- type, use the corresponding record type because it contains all
1710 -- primitive operations of the concurrent type and allows for proper
1711 -- substitution.
1713 if Is_Concurrent_Type (T) then
1714 Deriv_Typ := Corresponding_Record_Type (T);
1715 else
1716 Deriv_Typ := T;
1717 end if;
1719 pragma Assert (Present (Deriv_Typ));
1721 -- Determine which rep item chain to use. Precedence is given to that
1722 -- of the parent type's partial view since it usually carries all the
1723 -- class-wide invariants.
1725 if Present (Priv_Typ) then
1726 Prag := First_Rep_Item (Priv_Typ);
1727 else
1728 Prag := First_Rep_Item (Full_Typ);
1729 end if;
1731 while Present (Prag) loop
1732 if Nkind (Prag) = N_Pragma
1733 and then Pragma_Name (Prag) = Name_Default_Initial_Condition
1734 then
1735 -- Nothing to do if the pragma was already processed
1737 if Contains (Pragmas_Seen, Prag) then
1738 return;
1739 end if;
1741 -- Extract arguments of the Default_Initial_Condition pragma
1743 Prag_Expr_Arg := First (Pragma_Argument_Associations (Prag));
1744 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
1746 -- Pick up the implicit second argument of the pragma, which
1747 -- indicates the type that the pragma applies to.
1749 Prag_Typ_Arg := Next (Prag_Expr_Arg);
1750 if Present (Prag_Typ_Arg) then
1751 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
1752 else
1753 Prag_Typ := Empty;
1754 end if;
1756 -- The pragma applies to the partial view of the parent type
1758 if Present (Priv_Typ)
1759 and then Present (Prag_Typ)
1760 and then Entity (Prag_Typ) = Priv_Typ
1761 then
1762 Par_Typ := Priv_Typ;
1764 -- The pragma applies to the full view of the parent type
1766 elsif Present (Full_Typ)
1767 and then Present (Prag_Typ)
1768 and then Entity (Prag_Typ) = Full_Typ
1769 then
1770 Par_Typ := Full_Typ;
1772 -- Otherwise the pragma does not belong to the parent type and
1773 -- should not be considered.
1775 else
1776 return;
1777 end if;
1779 -- Substitute references in the DIC expression that are related
1780 -- to the partial type with corresponding references related to
1781 -- the derived type (call to Replace_References below).
1783 Expr := New_Copy_Tree (Prag_Expr);
1785 Par_Proc := Partial_DIC_Procedure (Par_Typ);
1787 -- If there's not a partial DIC procedure (such as when a
1788 -- full type doesn't have its own DIC, but is inherited from
1789 -- a type with DIC), get the full DIC procedure.
1791 if No (Par_Proc) then
1792 Par_Proc := DIC_Procedure (Par_Typ);
1793 end if;
1795 Replace_References
1796 (Expr => Expr,
1797 Par_Typ => Par_Typ,
1798 Deriv_Typ => Deriv_Typ,
1799 Par_Obj => First_Formal (Par_Proc),
1800 Deriv_Obj => Obj_Id);
1802 -- Why are there different actions depending on whether T is
1803 -- tagged? Can these be unified? ???
1805 if Is_Tagged_Type (T) then
1806 Add_Inherited_Tagged_DIC
1807 (DIC_Prag => Prag,
1808 Expr => Expr,
1809 Stmts => Checks);
1811 else
1812 Add_Inherited_DIC
1813 (DIC_Prag => Prag,
1814 Par_Typ => Par_Typ,
1815 Deriv_Typ => Deriv_Typ,
1816 Stmts => Checks);
1817 end if;
1819 -- Leave as soon as we get a DIC pragma, since we'll visit
1820 -- the pragmas of the parents, so will get to any "inherited"
1821 -- pragmas that way.
1823 return;
1824 end if;
1826 Next_Rep_Item (Prag);
1827 end loop;
1828 end Add_Inherited_DICs;
1830 -----------------
1831 -- Add_Own_DIC --
1832 -----------------
1834 procedure Add_Own_DIC
1835 (DIC_Prag : Node_Id;
1836 DIC_Typ : Entity_Id;
1837 Obj_Id : Entity_Id;
1838 Stmts : in out List_Id)
1840 DIC_Args : constant List_Id :=
1841 Pragma_Argument_Associations (DIC_Prag);
1842 DIC_Arg : constant Node_Id := First (DIC_Args);
1843 DIC_Asp : constant Node_Id := Corresponding_Aspect (DIC_Prag);
1844 DIC_Expr : constant Node_Id := Get_Pragma_Arg (DIC_Arg);
1846 -- Local variables
1848 Typ_Decl : constant Node_Id := Declaration_Node (DIC_Typ);
1850 Expr : Node_Id;
1852 -- Start of processing for Add_Own_DIC
1854 begin
1855 pragma Assert (Present (DIC_Expr));
1856 Expr := New_Copy_Tree (DIC_Expr);
1858 -- Perform the following substitution:
1860 -- * Replace the current instance of DIC_Typ with a reference to
1861 -- the _object formal parameter of the DIC procedure.
1863 Replace_Type_References
1864 (Expr => Expr,
1865 Typ => DIC_Typ,
1866 Obj_Id => Obj_Id);
1868 -- Preanalyze the DIC expression to detect errors and at the same
1869 -- time capture the visibility of the proper package part.
1871 Set_Parent (Expr, Typ_Decl);
1872 Preanalyze_Assert_Expression (Expr, Any_Boolean);
1874 -- Save a copy of the expression with all replacements and analysis
1875 -- already taken place in case a derived type inherits the pragma.
1876 -- The copy will be used as the foundation of the derived type's own
1877 -- version of the DIC assertion expression.
1879 if Is_Tagged_Type (DIC_Typ) then
1880 Set_Expression_Copy (DIC_Arg, New_Copy_Tree (Expr));
1881 end if;
1883 -- If the pragma comes from an aspect specification, replace the
1884 -- saved expression because all type references must be substituted
1885 -- for the call to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
1886 -- routines.
1888 if Present (DIC_Asp) then
1889 Set_Entity (Identifier (DIC_Asp), New_Copy_Tree (Expr));
1890 end if;
1892 -- Once the DIC assertion expression is fully processed, add a check
1893 -- to the statements of the DIC procedure (unless the type is an
1894 -- abstract type, in which case we don't want the possibility of
1895 -- generating a call to an abstract function of the type; such DIC
1896 -- procedures can never be called in any case, so not generating the
1897 -- check at all is OK).
1899 if not Is_Abstract_Type (DIC_Typ) or else GNATprove_Mode then
1900 Add_DIC_Check
1901 (DIC_Prag => DIC_Prag,
1902 DIC_Expr => Expr,
1903 Stmts => Stmts);
1904 end if;
1905 end Add_Own_DIC;
1907 ---------------------
1908 -- Add_Parent_DICs --
1909 ---------------------
1911 procedure Add_Parent_DICs
1912 (T : Entity_Id;
1913 Obj_Id : Entity_Id;
1914 Checks : in out List_Id)
1916 Dummy_1 : Entity_Id;
1917 Dummy_2 : Entity_Id;
1919 Curr_Typ : Entity_Id;
1920 -- The entity of the current type being examined
1922 Full_Typ : Entity_Id;
1923 -- The full view of Par_Typ
1925 Par_Typ : Entity_Id;
1926 -- The entity of the parent type
1928 Priv_Typ : Entity_Id;
1929 -- The partial view of Par_Typ
1931 Op_Node : Elmt_Id;
1932 Par_Prim : Entity_Id;
1933 Prim : Entity_Id;
1935 begin
1936 -- Map the overridden primitive to the overriding one; required by
1937 -- Replace_References (called by Add_Inherited_DICs) to handle calls
1938 -- to parent primitives.
1940 Op_Node := First_Elmt (Primitive_Operations (T));
1941 while Present (Op_Node) loop
1942 Prim := Node (Op_Node);
1944 if Present (Overridden_Operation (Prim))
1945 and then Comes_From_Source (Prim)
1946 then
1947 Par_Prim := Overridden_Operation (Prim);
1949 -- Create a mapping of the form:
1950 -- parent type primitive -> derived type primitive
1952 Type_Map.Set (Par_Prim, Prim);
1953 end if;
1955 Next_Elmt (Op_Node);
1956 end loop;
1958 -- Climb the parent type chain
1960 Curr_Typ := T;
1961 loop
1962 -- Do not consider subtypes, as they inherit the DICs from their
1963 -- base types.
1965 Par_Typ := Base_Type (Etype (Base_Type (Curr_Typ)));
1967 -- Stop the climb once the root of the parent chain is
1968 -- reached.
1970 exit when Curr_Typ = Par_Typ;
1972 -- Process the DICs of the parent type
1974 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
1976 -- Only try to inherit a DIC pragma from the parent type Par_Typ
1977 -- if it Has_Own_DIC pragma. The loop will proceed up the parent
1978 -- chain to find all types that have their own DIC.
1980 if Has_Own_DIC (Par_Typ) then
1981 Add_Inherited_DICs
1982 (T => T,
1983 Priv_Typ => Priv_Typ,
1984 Full_Typ => Full_Typ,
1985 Obj_Id => Obj_Id,
1986 Checks => Checks);
1987 end if;
1989 Curr_Typ := Par_Typ;
1990 end loop;
1991 end Add_Parent_DICs;
1993 -- Local variables
1995 Loc : constant Source_Ptr := Sloc (Typ);
1997 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1998 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1999 -- Save the Ghost-related attributes to restore on exit
2001 DIC_Prag : Node_Id;
2002 DIC_Typ : Entity_Id;
2003 Dummy_1 : Entity_Id;
2004 Dummy_2 : Entity_Id;
2005 Proc_Body : Node_Id;
2006 Proc_Body_Id : Entity_Id;
2007 Proc_Decl : Node_Id;
2008 Proc_Id : Entity_Id;
2009 Stmts : List_Id := No_List;
2011 CRec_Typ : Entity_Id := Empty;
2012 -- The corresponding record type of Full_Typ
2014 Full_Typ : Entity_Id := Empty;
2015 -- The full view of the working type
2017 Obj_Id : Entity_Id := Empty;
2018 -- The _object formal parameter of the invariant procedure
2020 Part_Proc : Entity_Id := Empty;
2021 -- The entity of the "partial" invariant procedure
2023 Priv_Typ : Entity_Id := Empty;
2024 -- The partial view of the working type
2026 Work_Typ : Entity_Id;
2027 -- The working type
2029 -- Start of processing for Build_DIC_Procedure_Body
2031 begin
2032 Work_Typ := Base_Type (Typ);
2034 -- Do not process class-wide types as these are Itypes, but lack a first
2035 -- subtype (see below).
2037 if Is_Class_Wide_Type (Work_Typ) then
2038 return;
2040 -- Do not process the underlying full view of a private type. There is
2041 -- no way to get back to the partial view, plus the body will be built
2042 -- by the full view or the base type.
2044 elsif Is_Underlying_Full_View (Work_Typ) then
2045 return;
2047 -- Use the first subtype when dealing with implicit base types
2049 elsif Is_Itype (Work_Typ) then
2050 Work_Typ := First_Subtype (Work_Typ);
2052 -- The input denotes the corresponding record type of a protected or a
2053 -- task type. Work with the concurrent type because the corresponding
2054 -- record type may not be visible to clients of the type.
2056 elsif Ekind (Work_Typ) = E_Record_Type
2057 and then Is_Concurrent_Record_Type (Work_Typ)
2058 then
2059 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2060 end if;
2062 -- The working type may be subject to pragma Ghost. Set the mode now to
2063 -- ensure that the DIC procedure is properly marked as Ghost.
2065 Set_Ghost_Mode (Work_Typ);
2067 -- The working type must be either define a DIC pragma of its own or
2068 -- inherit one from a parent type.
2070 pragma Assert (Has_DIC (Work_Typ));
2072 -- Recover the type which defines the DIC pragma. This is either the
2073 -- working type itself or a parent type when the pragma is inherited.
2075 DIC_Typ := Find_DIC_Type (Work_Typ);
2076 pragma Assert (Present (DIC_Typ));
2078 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2079 pragma Assert (Present (DIC_Prag));
2081 -- Nothing to do if pragma DIC appears without an argument or its sole
2082 -- argument is "null".
2084 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2085 goto Leave;
2086 end if;
2088 -- Obtain both views of the type
2090 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy_1, CRec_Typ);
2092 -- The caller requests a body for the partial DIC procedure
2094 if Partial_DIC then
2095 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2097 -- The "full" DIC procedure body was already created
2099 -- Create a declaration for the "partial" DIC procedure if it
2100 -- is not available.
2102 if No (Proc_Id) then
2103 Build_DIC_Procedure_Declaration
2104 (Typ => Work_Typ,
2105 Partial_DIC => True);
2107 Proc_Id := Partial_DIC_Procedure (Work_Typ);
2108 end if;
2110 -- The caller requests a body for the "full" DIC procedure
2112 else
2113 Proc_Id := DIC_Procedure (Work_Typ);
2114 Part_Proc := Partial_DIC_Procedure (Work_Typ);
2116 -- Create a declaration for the "full" DIC procedure if it is
2117 -- not available.
2119 if No (Proc_Id) then
2120 Build_DIC_Procedure_Declaration (Work_Typ);
2121 Proc_Id := DIC_Procedure (Work_Typ);
2122 end if;
2123 end if;
2125 -- At this point there should be a DIC procedure declaration
2127 pragma Assert (Present (Proc_Id));
2128 Proc_Decl := Unit_Declaration_Node (Proc_Id);
2130 -- Nothing to do if the DIC procedure already has a body
2132 if Present (Corresponding_Body (Proc_Decl)) then
2133 goto Leave;
2134 end if;
2136 -- Emulate the environment of the DIC procedure by installing its scope
2137 -- and formal parameters.
2139 Push_Scope (Proc_Id);
2140 Install_Formals (Proc_Id);
2142 Obj_Id := First_Formal (Proc_Id);
2143 pragma Assert (Present (Obj_Id));
2145 -- The "partial" DIC procedure verifies the DICs of the partial view
2146 -- only.
2148 if Partial_DIC then
2149 pragma Assert (Present (Priv_Typ));
2151 if Has_Own_DIC (Work_Typ) then -- If we're testing this then maybe
2152 Add_Own_DIC -- we shouldn't be calling Find_DIC_Typ above???
2153 (DIC_Prag => DIC_Prag,
2154 DIC_Typ => DIC_Typ, -- Should this just be Work_Typ???
2155 Obj_Id => Obj_Id,
2156 Stmts => Stmts);
2157 end if;
2159 -- Otherwise, the "full" DIC procedure verifies the DICs inherited from
2160 -- parent types, as well as indirectly verifying the DICs of the partial
2161 -- view by calling the "partial" DIC procedure.
2163 else
2164 -- Check the DIC of the partial view by calling the "partial" DIC
2165 -- procedure, unless the partial DIC body is empty. Generate:
2167 -- <Work_Typ>Partial_DIC (_object);
2169 if Present (Part_Proc) and then not Has_Null_Body (Part_Proc) then
2170 Append_New_To (Stmts,
2171 Make_Procedure_Call_Statement (Loc,
2172 Name => New_Occurrence_Of (Part_Proc, Loc),
2173 Parameter_Associations => New_List (
2174 New_Occurrence_Of (Obj_Id, Loc))));
2175 end if;
2177 -- Process inherited Default_Initial_Conditions for all parent types
2179 Add_Parent_DICs (Work_Typ, Obj_Id, Stmts);
2180 end if;
2182 End_Scope;
2184 -- Produce an empty completing body in the following cases:
2185 -- * Assertions are disabled
2186 -- * The DIC Assertion_Policy is Ignore
2188 if No (Stmts) then
2189 Stmts := New_List (Make_Null_Statement (Loc));
2190 end if;
2192 -- Generate:
2193 -- procedure <Work_Typ>DIC (_object : <Work_Typ>) is
2194 -- begin
2195 -- <Stmts>
2196 -- end <Work_Typ>DIC;
2198 Proc_Body :=
2199 Make_Subprogram_Body (Loc,
2200 Specification =>
2201 Copy_Subprogram_Spec (Parent (Proc_Id)),
2202 Declarations => Empty_List,
2203 Handled_Statement_Sequence =>
2204 Make_Handled_Sequence_Of_Statements (Loc,
2205 Statements => Stmts));
2206 Proc_Body_Id := Defining_Entity (Proc_Body);
2208 -- Perform minor decoration in case the body is not analyzed
2210 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
2211 Set_Etype (Proc_Body_Id, Standard_Void_Type);
2212 Set_Scope (Proc_Body_Id, Current_Scope);
2213 Set_SPARK_Pragma (Proc_Body_Id, SPARK_Pragma (Proc_Id));
2214 Set_SPARK_Pragma_Inherited
2215 (Proc_Body_Id, SPARK_Pragma_Inherited (Proc_Id));
2217 -- Link both spec and body to avoid generating duplicates
2219 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
2220 Set_Corresponding_Spec (Proc_Body, Proc_Id);
2222 -- The body should not be inserted into the tree when the context
2223 -- is a generic unit because it is not part of the template.
2224 -- Note that the body must still be generated in order to resolve the
2225 -- DIC assertion expression.
2227 if Inside_A_Generic then
2228 null;
2230 -- Semi-insert the body into the tree for GNATprove by setting its
2231 -- Parent field. This allows for proper upstream tree traversals.
2233 elsif GNATprove_Mode then
2234 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
2236 -- Otherwise the body is part of the freezing actions of the working
2237 -- type.
2239 else
2240 Append_Freeze_Action (Work_Typ, Proc_Body);
2241 end if;
2243 <<Leave>>
2244 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2245 end Build_DIC_Procedure_Body;
2247 -------------------------------------
2248 -- Build_DIC_Procedure_Declaration --
2249 -------------------------------------
2251 -- WARNING: This routine manages Ghost regions. Return statements must be
2252 -- replaced by gotos which jump to the end of the routine and restore the
2253 -- Ghost mode.
2255 procedure Build_DIC_Procedure_Declaration
2256 (Typ : Entity_Id;
2257 Partial_DIC : Boolean := False)
2259 Loc : constant Source_Ptr := Sloc (Typ);
2261 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2262 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2263 -- Save the Ghost-related attributes to restore on exit
2265 DIC_Prag : Node_Id;
2266 DIC_Typ : Entity_Id;
2267 Proc_Decl : Node_Id;
2268 Proc_Id : Entity_Id;
2269 Proc_Nam : Name_Id;
2270 Typ_Decl : Node_Id;
2272 CRec_Typ : Entity_Id;
2273 -- The corresponding record type of Full_Typ
2275 Full_Typ : Entity_Id;
2276 -- The full view of working type
2278 Obj_Id : Entity_Id;
2279 -- The _object formal parameter of the DIC procedure
2281 Priv_Typ : Entity_Id;
2282 -- The partial view of working type
2284 UFull_Typ : Entity_Id;
2285 -- The underlying full view of Full_Typ
2287 Work_Typ : Entity_Id;
2288 -- The working type
2290 begin
2291 Work_Typ := Base_Type (Typ);
2293 -- Do not process class-wide types as these are Itypes, but lack a first
2294 -- subtype (see below).
2296 if Is_Class_Wide_Type (Work_Typ) then
2297 return;
2299 -- Do not process the underlying full view of a private type. There is
2300 -- no way to get back to the partial view, plus the body will be built
2301 -- by the full view or the base type.
2303 elsif Is_Underlying_Full_View (Work_Typ) then
2304 return;
2306 -- Use the first subtype when dealing with various base types
2308 elsif Is_Itype (Work_Typ) then
2309 Work_Typ := First_Subtype (Work_Typ);
2311 -- The input denotes the corresponding record type of a protected or a
2312 -- task type. Work with the concurrent type because the corresponding
2313 -- record type may not be visible to clients of the type.
2315 elsif Ekind (Work_Typ) = E_Record_Type
2316 and then Is_Concurrent_Record_Type (Work_Typ)
2317 then
2318 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
2319 end if;
2321 -- The working type may be subject to pragma Ghost. Set the mode now to
2322 -- ensure that the DIC procedure is properly marked as Ghost.
2324 Set_Ghost_Mode (Work_Typ);
2326 -- The type must be either subject to a DIC pragma or inherit one from a
2327 -- parent type.
2329 pragma Assert (Has_DIC (Work_Typ));
2331 -- Recover the type which defines the DIC pragma. This is either the
2332 -- working type itself or a parent type when the pragma is inherited.
2334 DIC_Typ := Find_DIC_Type (Work_Typ);
2335 pragma Assert (Present (DIC_Typ));
2337 DIC_Prag := Get_Pragma (DIC_Typ, Pragma_Default_Initial_Condition);
2338 pragma Assert (Present (DIC_Prag));
2340 -- Nothing to do if pragma DIC appears without an argument or its sole
2341 -- argument is "null".
2343 if not Is_Verifiable_DIC_Pragma (DIC_Prag) then
2344 goto Leave;
2345 end if;
2347 -- Nothing to do if the type already has a "partial" DIC procedure
2349 if Partial_DIC then
2350 if Present (Partial_DIC_Procedure (Work_Typ)) then
2351 goto Leave;
2352 end if;
2354 -- Nothing to do if the type already has a "full" DIC procedure
2356 elsif Present (DIC_Procedure (Work_Typ)) then
2357 goto Leave;
2358 end if;
2360 -- The caller requests the declaration of the "partial" DIC procedure
2362 if Partial_DIC then
2363 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_DIC");
2365 -- Otherwise the caller requests the declaration of the "full" DIC
2366 -- procedure.
2368 else
2369 Proc_Nam := New_External_Name (Chars (Work_Typ), "DIC");
2370 end if;
2372 Proc_Id :=
2373 Make_Defining_Identifier (Loc, Chars => Proc_Nam);
2375 -- Perform minor decoration in case the declaration is not analyzed
2377 Mutate_Ekind (Proc_Id, E_Procedure);
2378 Set_Etype (Proc_Id, Standard_Void_Type);
2379 Set_Is_DIC_Procedure (Proc_Id);
2380 Set_Scope (Proc_Id, Current_Scope);
2381 Set_SPARK_Pragma (Proc_Id, SPARK_Mode_Pragma);
2382 Set_SPARK_Pragma_Inherited (Proc_Id);
2384 Set_DIC_Procedure (Work_Typ, Proc_Id);
2386 -- The DIC procedure requires debug info when the assertion expression
2387 -- is subject to Source Coverage Obligations.
2389 if Generate_SCO then
2390 Set_Debug_Info_Needed (Proc_Id);
2391 end if;
2393 -- Obtain all views of the input type
2395 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
2397 -- Associate the DIC procedure and various flags with all views
2399 Propagate_DIC_Attributes (Priv_Typ, From_Typ => Work_Typ);
2400 Propagate_DIC_Attributes (Full_Typ, From_Typ => Work_Typ);
2401 Propagate_DIC_Attributes (UFull_Typ, From_Typ => Work_Typ);
2402 Propagate_DIC_Attributes (CRec_Typ, From_Typ => Work_Typ);
2404 -- The declaration of the DIC procedure must be inserted after the
2405 -- declaration of the partial view as this allows for proper external
2406 -- visibility.
2408 if Present (Priv_Typ) then
2409 Typ_Decl := Declaration_Node (Priv_Typ);
2411 -- Derived types with the full view as parent do not have a partial
2412 -- view. Insert the DIC procedure after the derived type.
2414 else
2415 Typ_Decl := Declaration_Node (Full_Typ);
2416 end if;
2418 -- The type should have a declarative node
2420 pragma Assert (Present (Typ_Decl));
2422 -- Create the formal parameter which emulates the variable-like behavior
2423 -- of the type's current instance.
2425 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
2427 -- Perform minor decoration in case the declaration is not analyzed
2429 Mutate_Ekind (Obj_Id, E_In_Parameter);
2430 Set_Etype (Obj_Id, Work_Typ);
2431 Set_Scope (Obj_Id, Proc_Id);
2433 Set_First_Entity (Proc_Id, Obj_Id);
2434 Set_Last_Entity (Proc_Id, Obj_Id);
2436 -- Generate:
2437 -- procedure <Work_Typ>DIC (_object : <Work_Typ>);
2439 Proc_Decl :=
2440 Make_Subprogram_Declaration (Loc,
2441 Specification =>
2442 Make_Procedure_Specification (Loc,
2443 Defining_Unit_Name => Proc_Id,
2444 Parameter_Specifications => New_List (
2445 Make_Parameter_Specification (Loc,
2446 Defining_Identifier => Obj_Id,
2447 Parameter_Type =>
2448 New_Occurrence_Of (Work_Typ, Loc)))));
2450 -- The declaration should not be inserted into the tree when the context
2451 -- is a generic unit because it is not part of the template.
2453 if Inside_A_Generic then
2454 null;
2456 -- Semi-insert the declaration into the tree for GNATprove by setting
2457 -- its Parent field. This allows for proper upstream tree traversals.
2459 elsif GNATprove_Mode then
2460 Set_Parent (Proc_Decl, Parent (Typ_Decl));
2462 -- Otherwise insert the declaration
2464 else
2465 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
2466 end if;
2468 <<Leave>>
2469 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2470 end Build_DIC_Procedure_Declaration;
2472 ------------------------------------
2473 -- Build_Invariant_Procedure_Body --
2474 ------------------------------------
2476 -- WARNING: This routine manages Ghost regions. Return statements must be
2477 -- replaced by gotos which jump to the end of the routine and restore the
2478 -- Ghost mode.
2480 procedure Build_Invariant_Procedure_Body
2481 (Typ : Entity_Id;
2482 Partial_Invariant : Boolean := False)
2484 Loc : constant Source_Ptr := Sloc (Typ);
2486 Pragmas_Seen : Elist_Id := No_Elist;
2487 -- This list contains all invariant pragmas processed so far. The list
2488 -- is used to avoid generating redundant invariant checks.
2490 Produced_Check : Boolean := False;
2491 -- This flag tracks whether the type has produced at least one invariant
2492 -- check. The flag is used as a sanity check at the end of the routine.
2494 -- NOTE: most of the routines in Build_Invariant_Procedure_Body are
2495 -- intentionally unnested to avoid deep indentation of code.
2497 -- NOTE: all Add_xxx_Invariants routines are reactive. In other words
2498 -- they emit checks, loops (for arrays) and case statements (for record
2499 -- variant parts) only when there are invariants to verify. This keeps
2500 -- the body of the invariant procedure free of useless code.
2502 procedure Add_Array_Component_Invariants
2503 (T : Entity_Id;
2504 Obj_Id : Entity_Id;
2505 Checks : in out List_Id);
2506 -- Generate an invariant check for each component of array type T.
2507 -- Obj_Id denotes the entity of the _object formal parameter of the
2508 -- invariant procedure. All created checks are added to list Checks.
2510 procedure Add_Inherited_Invariants
2511 (T : Entity_Id;
2512 Priv_Typ : Entity_Id;
2513 Full_Typ : Entity_Id;
2514 Obj_Id : Entity_Id;
2515 Checks : in out List_Id);
2516 -- Generate an invariant check for each inherited class-wide invariant
2517 -- coming from all parent types of type T. Priv_Typ and Full_Typ denote
2518 -- the partial and full view of the parent type. Obj_Id denotes the
2519 -- entity of the _object formal parameter of the invariant procedure.
2520 -- All created checks are added to list Checks.
2522 procedure Add_Interface_Invariants
2523 (T : Entity_Id;
2524 Obj_Id : Entity_Id;
2525 Checks : in out List_Id);
2526 -- Generate an invariant check for each inherited class-wide invariant
2527 -- coming from all interfaces implemented by type T. Obj_Id denotes the
2528 -- entity of the _object formal parameter of the invariant procedure.
2529 -- All created checks are added to list Checks.
2531 procedure Add_Invariant_Check
2532 (Prag : Node_Id;
2533 Expr : Node_Id;
2534 Checks : in out List_Id;
2535 Inherited : Boolean := False);
2536 -- Subsidiary to all Add_xxx_Invariant routines. Add a runtime check to
2537 -- verify assertion expression Expr of pragma Prag. All generated code
2538 -- is added to list Checks. Flag Inherited should be set when the pragma
2539 -- is inherited from a parent or interface type.
2541 procedure Add_Own_Invariants
2542 (T : Entity_Id;
2543 Obj_Id : Entity_Id;
2544 Checks : in out List_Id;
2545 Priv_Item : Node_Id := Empty);
2546 -- Generate an invariant check for each invariant found for type T.
2547 -- Obj_Id denotes the entity of the _object formal parameter of the
2548 -- invariant procedure. All created checks are added to list Checks.
2549 -- Priv_Item denotes the first rep item of the private type.
2551 procedure Add_Parent_Invariants
2552 (T : Entity_Id;
2553 Obj_Id : Entity_Id;
2554 Checks : in out List_Id);
2555 -- Generate an invariant check for each inherited class-wide invariant
2556 -- coming from all parent types of type T. Obj_Id denotes the entity of
2557 -- the _object formal parameter of the invariant procedure. All created
2558 -- checks are added to list Checks.
2560 procedure Add_Record_Component_Invariants
2561 (T : Entity_Id;
2562 Obj_Id : Entity_Id;
2563 Checks : in out List_Id);
2564 -- Generate an invariant check for each component of record type T.
2565 -- Obj_Id denotes the entity of the _object formal parameter of the
2566 -- invariant procedure. All created checks are added to list Checks.
2568 ------------------------------------
2569 -- Add_Array_Component_Invariants --
2570 ------------------------------------
2572 procedure Add_Array_Component_Invariants
2573 (T : Entity_Id;
2574 Obj_Id : Entity_Id;
2575 Checks : in out List_Id)
2577 Comp_Typ : constant Entity_Id := Component_Type (T);
2578 Dims : constant Pos := Number_Dimensions (T);
2580 procedure Process_Array_Component
2581 (Indices : List_Id;
2582 Comp_Checks : in out List_Id);
2583 -- Generate an invariant check for an array component identified by
2584 -- the indices in list Indices. All created checks are added to list
2585 -- Comp_Checks.
2587 procedure Process_One_Dimension
2588 (Dim : Pos;
2589 Indices : List_Id;
2590 Dim_Checks : in out List_Id);
2591 -- Generate a loop over the Nth dimension Dim of an array type. List
2592 -- Indices contains all array indices for the dimension. All created
2593 -- checks are added to list Dim_Checks.
2595 -----------------------------
2596 -- Process_Array_Component --
2597 -----------------------------
2599 procedure Process_Array_Component
2600 (Indices : List_Id;
2601 Comp_Checks : in out List_Id)
2603 Proc_Id : Entity_Id;
2605 begin
2606 if Has_Invariants (Comp_Typ) then
2608 -- In GNATprove mode, the component invariants are checked by
2609 -- other means. They should not be added to the array type
2610 -- invariant procedure, so that the procedure can be used to
2611 -- check the array type invariants if any.
2613 if GNATprove_Mode then
2614 null;
2616 else
2617 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
2619 -- The component type should have an invariant procedure
2620 -- if it has invariants of its own or inherits class-wide
2621 -- invariants from parent or interface types.
2623 pragma Assert (Present (Proc_Id));
2625 -- Generate:
2626 -- <Comp_Typ>Invariant (_object (<Indices>));
2628 -- The invariant procedure has a null body if assertions are
2629 -- disabled or Assertion_Policy Ignore is in effect.
2631 if not Has_Null_Body (Proc_Id) then
2632 Append_New_To (Comp_Checks,
2633 Make_Procedure_Call_Statement (Loc,
2634 Name =>
2635 New_Occurrence_Of (Proc_Id, Loc),
2636 Parameter_Associations => New_List (
2637 Make_Indexed_Component (Loc,
2638 Prefix => New_Occurrence_Of (Obj_Id, Loc),
2639 Expressions => New_Copy_List (Indices)))));
2640 end if;
2641 end if;
2643 Produced_Check := True;
2644 end if;
2645 end Process_Array_Component;
2647 ---------------------------
2648 -- Process_One_Dimension --
2649 ---------------------------
2651 procedure Process_One_Dimension
2652 (Dim : Pos;
2653 Indices : List_Id;
2654 Dim_Checks : in out List_Id)
2656 Comp_Checks : List_Id := No_List;
2657 Index : Entity_Id;
2659 begin
2660 -- Generate the invariant checks for the array component after all
2661 -- dimensions have produced their respective loops.
2663 if Dim > Dims then
2664 Process_Array_Component
2665 (Indices => Indices,
2666 Comp_Checks => Dim_Checks);
2668 -- Otherwise create a loop for the current dimension
2670 else
2671 -- Create a new loop variable for each dimension
2673 Index :=
2674 Make_Defining_Identifier (Loc,
2675 Chars => New_External_Name ('I', Dim));
2676 Append_To (Indices, New_Occurrence_Of (Index, Loc));
2678 Process_One_Dimension
2679 (Dim => Dim + 1,
2680 Indices => Indices,
2681 Dim_Checks => Comp_Checks);
2683 -- Generate:
2684 -- for I<Dim> in _object'Range (<Dim>) loop
2685 -- <Comp_Checks>
2686 -- end loop;
2688 -- Note that the invariant procedure may have a null body if
2689 -- assertions are disabled or Assertion_Policy Ignore is in
2690 -- effect.
2692 if Present (Comp_Checks) then
2693 Append_New_To (Dim_Checks,
2694 Make_Implicit_Loop_Statement (T,
2695 Identifier => Empty,
2696 Iteration_Scheme =>
2697 Make_Iteration_Scheme (Loc,
2698 Loop_Parameter_Specification =>
2699 Make_Loop_Parameter_Specification (Loc,
2700 Defining_Identifier => Index,
2701 Discrete_Subtype_Definition =>
2702 Make_Attribute_Reference (Loc,
2703 Prefix =>
2704 New_Occurrence_Of (Obj_Id, Loc),
2705 Attribute_Name => Name_Range,
2706 Expressions => New_List (
2707 Make_Integer_Literal (Loc, Dim))))),
2708 Statements => Comp_Checks));
2709 end if;
2710 end if;
2711 end Process_One_Dimension;
2713 -- Start of processing for Add_Array_Component_Invariants
2715 begin
2716 Process_One_Dimension
2717 (Dim => 1,
2718 Indices => New_List,
2719 Dim_Checks => Checks);
2720 end Add_Array_Component_Invariants;
2722 ------------------------------
2723 -- Add_Inherited_Invariants --
2724 ------------------------------
2726 procedure Add_Inherited_Invariants
2727 (T : Entity_Id;
2728 Priv_Typ : Entity_Id;
2729 Full_Typ : Entity_Id;
2730 Obj_Id : Entity_Id;
2731 Checks : in out List_Id)
2733 Deriv_Typ : Entity_Id;
2734 Expr : Node_Id;
2735 Prag : Node_Id;
2736 Prag_Expr : Node_Id;
2737 Prag_Expr_Arg : Node_Id;
2738 Prag_Typ : Node_Id;
2739 Prag_Typ_Arg : Node_Id;
2741 Par_Proc : Entity_Id;
2742 -- The "partial" invariant procedure of Par_Typ
2744 Par_Typ : Entity_Id;
2745 -- The suitable view of the parent type used in the substitution of
2746 -- type attributes.
2748 begin
2749 if No (Priv_Typ) and then No (Full_Typ) then
2750 return;
2751 end if;
2753 -- When the type inheriting the class-wide invariant is a concurrent
2754 -- type, use the corresponding record type because it contains all
2755 -- primitive operations of the concurrent type and allows for proper
2756 -- substitution.
2758 if Is_Concurrent_Type (T) then
2759 Deriv_Typ := Corresponding_Record_Type (T);
2760 else
2761 Deriv_Typ := T;
2762 end if;
2764 pragma Assert (Present (Deriv_Typ));
2766 -- Determine which rep item chain to use. Precedence is given to that
2767 -- of the parent type's partial view since it usually carries all the
2768 -- class-wide invariants.
2770 if Present (Priv_Typ) then
2771 Prag := First_Rep_Item (Priv_Typ);
2772 else
2773 Prag := First_Rep_Item (Full_Typ);
2774 end if;
2776 while Present (Prag) loop
2777 if Nkind (Prag) = N_Pragma
2778 and then Pragma_Name (Prag) = Name_Invariant
2779 then
2780 -- Nothing to do if the pragma was already processed
2782 if Contains (Pragmas_Seen, Prag) then
2783 return;
2785 -- Nothing to do when the caller requests the processing of all
2786 -- inherited class-wide invariants, but the pragma does not
2787 -- fall in this category.
2789 elsif not Class_Present (Prag) then
2790 return;
2791 end if;
2793 -- Extract the arguments of the invariant pragma
2795 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
2796 Prag_Expr_Arg := Next (Prag_Typ_Arg);
2797 Prag_Expr := Expression_Copy (Prag_Expr_Arg);
2798 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
2800 -- The pragma applies to the partial view of the parent type
2802 if Present (Priv_Typ)
2803 and then Entity (Prag_Typ) = Priv_Typ
2804 then
2805 Par_Typ := Priv_Typ;
2807 -- The pragma applies to the full view of the parent type
2809 elsif Present (Full_Typ)
2810 and then Entity (Prag_Typ) = Full_Typ
2811 then
2812 Par_Typ := Full_Typ;
2814 -- Otherwise the pragma does not belong to the parent type and
2815 -- should not be considered.
2817 else
2818 return;
2819 end if;
2821 -- Perform the following substitutions:
2823 -- * Replace a reference to the _object parameter of the
2824 -- parent type's partial invariant procedure with a
2825 -- reference to the _object parameter of the derived
2826 -- type's full invariant procedure.
2828 -- * Replace a reference to a discriminant of the parent type
2829 -- with a suitable value from the point of view of the
2830 -- derived type.
2832 -- * Replace a call to an overridden parent primitive with a
2833 -- call to the overriding derived type primitive.
2835 -- * Replace a call to an inherited parent primitive with a
2836 -- call to the internally-generated inherited derived type
2837 -- primitive.
2839 Expr := New_Copy_Tree (Prag_Expr);
2841 -- The parent type must have a "partial" invariant procedure
2842 -- because class-wide invariants are captured exclusively by
2843 -- it.
2845 Par_Proc := Partial_Invariant_Procedure (Par_Typ);
2846 pragma Assert (Present (Par_Proc));
2848 Replace_References
2849 (Expr => Expr,
2850 Par_Typ => Par_Typ,
2851 Deriv_Typ => Deriv_Typ,
2852 Par_Obj => First_Formal (Par_Proc),
2853 Deriv_Obj => Obj_Id);
2855 Add_Invariant_Check (Prag, Expr, Checks, Inherited => True);
2856 end if;
2858 Next_Rep_Item (Prag);
2859 end loop;
2860 end Add_Inherited_Invariants;
2862 ------------------------------
2863 -- Add_Interface_Invariants --
2864 ------------------------------
2866 procedure Add_Interface_Invariants
2867 (T : Entity_Id;
2868 Obj_Id : Entity_Id;
2869 Checks : in out List_Id)
2871 Iface_Elmt : Elmt_Id;
2872 Ifaces : Elist_Id;
2874 begin
2875 -- Generate an invariant check for each class-wide invariant coming
2876 -- from all interfaces implemented by type T.
2878 if Is_Tagged_Type (T) then
2879 Collect_Interfaces (T, Ifaces);
2881 -- Process the class-wide invariants of all implemented interfaces
2883 Iface_Elmt := First_Elmt (Ifaces);
2884 while Present (Iface_Elmt) loop
2886 -- The Full_Typ parameter is intentionally left Empty because
2887 -- interfaces are treated as the partial view of a private type
2888 -- in order to achieve uniformity with the general case.
2890 Add_Inherited_Invariants
2891 (T => T,
2892 Priv_Typ => Node (Iface_Elmt),
2893 Full_Typ => Empty,
2894 Obj_Id => Obj_Id,
2895 Checks => Checks);
2897 Next_Elmt (Iface_Elmt);
2898 end loop;
2899 end if;
2900 end Add_Interface_Invariants;
2902 -------------------------
2903 -- Add_Invariant_Check --
2904 -------------------------
2906 procedure Add_Invariant_Check
2907 (Prag : Node_Id;
2908 Expr : Node_Id;
2909 Checks : in out List_Id;
2910 Inherited : Boolean := False)
2912 Args : constant List_Id := Pragma_Argument_Associations (Prag);
2913 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
2914 Ploc : constant Source_Ptr := Sloc (Prag);
2915 Str_Arg : constant Node_Id := Next (Next (First (Args)));
2917 Assoc : List_Id;
2918 Str : String_Id;
2920 begin
2921 -- The invariant is ignored, nothing left to do
2923 if Is_Ignored (Prag) then
2924 null;
2926 -- Otherwise the invariant is checked. Build a pragma Check to verify
2927 -- the expression at run time.
2929 else
2930 Assoc := New_List (
2931 Make_Pragma_Argument_Association (Ploc,
2932 Expression => Make_Identifier (Ploc, Nam)),
2933 Make_Pragma_Argument_Association (Ploc,
2934 Expression => Expr));
2936 -- Handle the String argument (if any)
2938 if Present (Str_Arg) then
2939 Str := Strval (Get_Pragma_Arg (Str_Arg));
2941 -- When inheriting an invariant, modify the message from
2942 -- "failed invariant" to "failed inherited invariant".
2944 if Inherited then
2945 String_To_Name_Buffer (Str);
2947 if Name_Buffer (1 .. 16) = "failed invariant" then
2948 Insert_Str_In_Name_Buffer ("inherited ", 8);
2949 Str := String_From_Name_Buffer;
2950 end if;
2951 end if;
2953 Append_To (Assoc,
2954 Make_Pragma_Argument_Association (Ploc,
2955 Expression => Make_String_Literal (Ploc, Str)));
2956 end if;
2958 -- Generate:
2959 -- pragma Check (<Nam>, <Expr>, <Str>);
2961 Append_New_To (Checks,
2962 Make_Pragma (Ploc,
2963 Chars => Name_Check,
2964 Pragma_Argument_Associations => Assoc));
2965 end if;
2967 -- Output an info message when inheriting an invariant and the
2968 -- listing option is enabled.
2970 if Inherited and List_Inherited_Aspects then
2971 Error_Msg_Sloc := Sloc (Prag);
2972 Error_Msg_N
2973 ("info: & inherits `Invariant''Class` aspect from #?.l?", Typ);
2974 end if;
2976 -- Add the pragma to the list of processed pragmas
2978 Append_New_Elmt (Prag, Pragmas_Seen);
2979 Produced_Check := True;
2980 end Add_Invariant_Check;
2982 ---------------------------
2983 -- Add_Parent_Invariants --
2984 ---------------------------
2986 procedure Add_Parent_Invariants
2987 (T : Entity_Id;
2988 Obj_Id : Entity_Id;
2989 Checks : in out List_Id)
2991 Dummy_1 : Entity_Id;
2992 Dummy_2 : Entity_Id;
2994 Curr_Typ : Entity_Id;
2995 -- The entity of the current type being examined
2997 Full_Typ : Entity_Id;
2998 -- The full view of Par_Typ
3000 Par_Typ : Entity_Id;
3001 -- The entity of the parent type
3003 Priv_Typ : Entity_Id;
3004 -- The partial view of Par_Typ
3006 begin
3007 -- Do not process array types because they cannot have true parent
3008 -- types. This also prevents the generation of a duplicate invariant
3009 -- check when the input type is an array base type because its Etype
3010 -- denotes the first subtype, both of which share the same component
3011 -- type.
3013 if Is_Array_Type (T) then
3014 return;
3015 end if;
3017 -- Climb the parent type chain
3019 Curr_Typ := T;
3020 loop
3021 -- Do not consider subtypes as they inherit the invariants
3022 -- from their base types.
3024 Par_Typ := Base_Type (Etype (Curr_Typ));
3026 -- Stop the climb once the root of the parent chain is
3027 -- reached.
3029 exit when Curr_Typ = Par_Typ;
3031 -- Process the class-wide invariants of the parent type
3033 Get_Views (Par_Typ, Priv_Typ, Full_Typ, Dummy_1, Dummy_2);
3035 -- Process the elements of an array type
3037 if Is_Array_Type (Full_Typ) then
3038 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Checks);
3040 -- Process the components of a record type
3042 elsif Ekind (Full_Typ) = E_Record_Type then
3043 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Checks);
3044 end if;
3046 Add_Inherited_Invariants
3047 (T => T,
3048 Priv_Typ => Priv_Typ,
3049 Full_Typ => Full_Typ,
3050 Obj_Id => Obj_Id,
3051 Checks => Checks);
3053 Curr_Typ := Par_Typ;
3054 end loop;
3055 end Add_Parent_Invariants;
3057 ------------------------
3058 -- Add_Own_Invariants --
3059 ------------------------
3061 procedure Add_Own_Invariants
3062 (T : Entity_Id;
3063 Obj_Id : Entity_Id;
3064 Checks : in out List_Id;
3065 Priv_Item : Node_Id := Empty)
3067 Expr : Node_Id;
3068 Prag : Node_Id;
3069 Prag_Asp : Node_Id;
3070 Prag_Expr : Node_Id;
3071 Prag_Expr_Arg : Node_Id;
3072 Prag_Typ : Node_Id;
3073 Prag_Typ_Arg : Node_Id;
3075 begin
3076 if No (T) then
3077 return;
3078 end if;
3080 Prag := First_Rep_Item (T);
3081 while Present (Prag) loop
3082 if Nkind (Prag) = N_Pragma
3083 and then Pragma_Name (Prag) = Name_Invariant
3084 then
3085 -- Stop the traversal of the rep item chain once a specific
3086 -- item is encountered.
3088 if Present (Priv_Item) and then Prag = Priv_Item then
3089 exit;
3090 end if;
3092 -- Nothing to do if the pragma was already processed
3094 if Contains (Pragmas_Seen, Prag) then
3095 return;
3096 end if;
3098 -- Extract the arguments of the invariant pragma
3100 Prag_Typ_Arg := First (Pragma_Argument_Associations (Prag));
3101 Prag_Expr_Arg := Next (Prag_Typ_Arg);
3102 Prag_Expr := Get_Pragma_Arg (Prag_Expr_Arg);
3103 Prag_Typ := Get_Pragma_Arg (Prag_Typ_Arg);
3104 Prag_Asp := Corresponding_Aspect (Prag);
3106 -- Verify the pragma belongs to T, otherwise the pragma applies
3107 -- to a parent type in which case it will be processed later by
3108 -- Add_Parent_Invariants or Add_Interface_Invariants.
3110 if Entity (Prag_Typ) /= T then
3111 return;
3112 end if;
3114 Expr := New_Copy_Tree (Prag_Expr);
3116 -- Substitute all references to type T with references to the
3117 -- _object formal parameter.
3119 Replace_Type_References (Expr, T, Obj_Id);
3121 -- Preanalyze the invariant expression to detect errors and at
3122 -- the same time capture the visibility of the proper package
3123 -- part.
3125 Set_Parent (Expr, Parent (Prag_Expr));
3126 Preanalyze_Assert_Expression (Expr, Any_Boolean);
3128 -- Save a copy of the expression when T is tagged to detect
3129 -- errors and capture the visibility of the proper package part
3130 -- for the generation of inherited type invariants.
3132 if Is_Tagged_Type (T) then
3133 Set_Expression_Copy (Prag_Expr_Arg, New_Copy_Tree (Expr));
3134 end if;
3136 -- If the pragma comes from an aspect specification, replace
3137 -- the saved expression because all type references must be
3138 -- substituted for the call to Preanalyze_Spec_Expression in
3139 -- Check_Aspect_At_xxx routines.
3141 if Present (Prag_Asp) then
3142 Set_Entity (Identifier (Prag_Asp), New_Copy_Tree (Expr));
3143 end if;
3145 Add_Invariant_Check (Prag, Expr, Checks);
3146 end if;
3148 Next_Rep_Item (Prag);
3149 end loop;
3150 end Add_Own_Invariants;
3152 -------------------------------------
3153 -- Add_Record_Component_Invariants --
3154 -------------------------------------
3156 procedure Add_Record_Component_Invariants
3157 (T : Entity_Id;
3158 Obj_Id : Entity_Id;
3159 Checks : in out List_Id)
3161 procedure Process_Component_List
3162 (Comp_List : Node_Id;
3163 CL_Checks : in out List_Id);
3164 -- Generate invariant checks for all record components found in
3165 -- component list Comp_List, including variant parts. All created
3166 -- checks are added to list CL_Checks.
3168 procedure Process_Record_Component
3169 (Comp_Id : Entity_Id;
3170 Comp_Checks : in out List_Id);
3171 -- Generate an invariant check for a record component identified by
3172 -- Comp_Id. All created checks are added to list Comp_Checks.
3174 ----------------------------
3175 -- Process_Component_List --
3176 ----------------------------
3178 procedure Process_Component_List
3179 (Comp_List : Node_Id;
3180 CL_Checks : in out List_Id)
3182 Comp : Node_Id;
3183 Var : Node_Id;
3184 Var_Alts : List_Id := No_List;
3185 Var_Checks : List_Id := No_List;
3186 Var_Stmts : List_Id;
3188 Produced_Variant_Check : Boolean := False;
3189 -- This flag tracks whether the component has produced at least
3190 -- one invariant check.
3192 begin
3193 -- Traverse the component items
3195 Comp := First (Component_Items (Comp_List));
3196 while Present (Comp) loop
3197 if Nkind (Comp) = N_Component_Declaration then
3199 -- Generate the component invariant check
3201 Process_Record_Component
3202 (Comp_Id => Defining_Entity (Comp),
3203 Comp_Checks => CL_Checks);
3204 end if;
3206 Next (Comp);
3207 end loop;
3209 -- Traverse the variant part
3211 if Present (Variant_Part (Comp_List)) then
3212 Var := First (Variants (Variant_Part (Comp_List)));
3213 while Present (Var) loop
3214 Var_Checks := No_List;
3216 -- Generate invariant checks for all components and variant
3217 -- parts that qualify.
3219 Process_Component_List
3220 (Comp_List => Component_List (Var),
3221 CL_Checks => Var_Checks);
3223 -- The components of the current variant produced at least
3224 -- one invariant check.
3226 if Present (Var_Checks) then
3227 Var_Stmts := Var_Checks;
3228 Produced_Variant_Check := True;
3230 -- Otherwise there are either no components with invariants,
3231 -- assertions are disabled, or Assertion_Policy Ignore is in
3232 -- effect.
3234 else
3235 Var_Stmts := New_List (Make_Null_Statement (Loc));
3236 end if;
3238 Append_New_To (Var_Alts,
3239 Make_Case_Statement_Alternative (Loc,
3240 Discrete_Choices =>
3241 New_Copy_List (Discrete_Choices (Var)),
3242 Statements => Var_Stmts));
3244 Next (Var);
3245 end loop;
3247 -- Create a case statement which verifies the invariant checks
3248 -- of a particular component list depending on the discriminant
3249 -- values only when there is at least one real invariant check.
3251 if Produced_Variant_Check then
3252 Append_New_To (CL_Checks,
3253 Make_Case_Statement (Loc,
3254 Expression =>
3255 Make_Selected_Component (Loc,
3256 Prefix => New_Occurrence_Of (Obj_Id, Loc),
3257 Selector_Name =>
3258 New_Occurrence_Of
3259 (Entity (Name (Variant_Part (Comp_List))), Loc)),
3260 Alternatives => Var_Alts));
3261 end if;
3262 end if;
3263 end Process_Component_List;
3265 ------------------------------
3266 -- Process_Record_Component --
3267 ------------------------------
3269 procedure Process_Record_Component
3270 (Comp_Id : Entity_Id;
3271 Comp_Checks : in out List_Id)
3273 Comp_Typ : constant Entity_Id := Etype (Comp_Id);
3274 Proc_Id : Entity_Id;
3276 Produced_Component_Check : Boolean := False;
3277 -- This flag tracks whether the component has produced at least
3278 -- one invariant check.
3280 begin
3281 -- Nothing to do for internal component _parent. Note that it is
3282 -- not desirable to check whether the component comes from source
3283 -- because protected type components are relocated to an internal
3284 -- corresponding record, but still need processing.
3286 if Chars (Comp_Id) = Name_uParent then
3287 return;
3288 end if;
3290 -- Verify the invariant of the component. Note that an access
3291 -- type may have an invariant when it acts as the full view of a
3292 -- private type and the invariant appears on the partial view. In
3293 -- this case verify the access value itself.
3295 if Has_Invariants (Comp_Typ) then
3297 -- In GNATprove mode, the component invariants are checked by
3298 -- other means. They should not be added to the record type
3299 -- invariant procedure, so that the procedure can be used to
3300 -- check the record type invariants if any.
3302 if GNATprove_Mode then
3303 null;
3305 else
3306 Proc_Id := Invariant_Procedure (Base_Type (Comp_Typ));
3308 -- The component type should have an invariant procedure
3309 -- if it has invariants of its own or inherits class-wide
3310 -- invariants from parent or interface types.
3312 pragma Assert (Present (Proc_Id));
3314 -- Generate:
3315 -- <Comp_Typ>Invariant (T (_object).<Comp_Id>);
3317 -- Note that the invariant procedure may have a null body if
3318 -- assertions are disabled or Assertion_Policy Ignore is in
3319 -- effect.
3321 if not Has_Null_Body (Proc_Id) then
3322 Append_New_To (Comp_Checks,
3323 Make_Procedure_Call_Statement (Loc,
3324 Name =>
3325 New_Occurrence_Of (Proc_Id, Loc),
3326 Parameter_Associations => New_List (
3327 Make_Selected_Component (Loc,
3328 Prefix =>
3329 Unchecked_Convert_To
3330 (T, New_Occurrence_Of (Obj_Id, Loc)),
3331 Selector_Name =>
3332 New_Occurrence_Of (Comp_Id, Loc)))));
3333 end if;
3334 end if;
3336 Produced_Check := True;
3337 Produced_Component_Check := True;
3338 end if;
3340 if Produced_Component_Check and then Has_Unchecked_Union (T) then
3341 Error_Msg_NE
3342 ("invariants cannot be checked on components of "
3343 & "unchecked_union type &??", Comp_Id, T);
3344 end if;
3345 end Process_Record_Component;
3347 -- Local variables
3349 Comps : Node_Id;
3350 Def : Node_Id;
3352 -- Start of processing for Add_Record_Component_Invariants
3354 begin
3355 -- An untagged derived type inherits the components of its parent
3356 -- type. In order to avoid creating redundant invariant checks, do
3357 -- not process the components now. Instead wait until the ultimate
3358 -- parent of the untagged derivation chain is reached.
3360 if not Is_Untagged_Derivation (T) then
3361 Def := Type_Definition (Parent (T));
3363 if Nkind (Def) = N_Derived_Type_Definition then
3364 Def := Record_Extension_Part (Def);
3365 end if;
3367 pragma Assert (Nkind (Def) = N_Record_Definition);
3368 Comps := Component_List (Def);
3370 if Present (Comps) then
3371 Process_Component_List
3372 (Comp_List => Comps,
3373 CL_Checks => Checks);
3374 end if;
3375 end if;
3376 end Add_Record_Component_Invariants;
3378 -- Local variables
3380 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3381 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3382 -- Save the Ghost-related attributes to restore on exit
3384 Dummy : Entity_Id;
3385 Priv_Item : Node_Id;
3386 Proc_Body : Node_Id;
3387 Proc_Body_Id : Entity_Id;
3388 Proc_Decl : Node_Id;
3389 Proc_Id : Entity_Id;
3390 Stmts : List_Id := No_List;
3392 CRec_Typ : Entity_Id := Empty;
3393 -- The corresponding record type of Full_Typ
3395 Full_Proc : Entity_Id := Empty;
3396 -- The entity of the "full" invariant procedure
3398 Full_Typ : Entity_Id := Empty;
3399 -- The full view of the working type
3401 Obj_Id : Entity_Id := Empty;
3402 -- The _object formal parameter of the invariant procedure
3404 Part_Proc : Entity_Id := Empty;
3405 -- The entity of the "partial" invariant procedure
3407 Priv_Typ : Entity_Id := Empty;
3408 -- The partial view of the working type
3410 Work_Typ : Entity_Id := Empty;
3411 -- The working type
3413 -- Start of processing for Build_Invariant_Procedure_Body
3415 begin
3416 Work_Typ := Typ;
3418 -- Do not process the underlying full view of a private type. There is
3419 -- no way to get back to the partial view, plus the body will be built
3420 -- by the full view or the base type.
3422 if Is_Underlying_Full_View (Work_Typ) then
3423 return;
3425 -- The input type denotes the implementation base type of a constrained
3426 -- array type. Work with the first subtype as all invariant pragmas are
3427 -- on its rep item chain.
3429 elsif Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3430 Work_Typ := First_Subtype (Work_Typ);
3432 -- The input type denotes the corresponding record type of a protected
3433 -- or task type. Work with the concurrent type because the corresponding
3434 -- record type may not be visible to clients of the type.
3436 elsif Ekind (Work_Typ) = E_Record_Type
3437 and then Is_Concurrent_Record_Type (Work_Typ)
3438 then
3439 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3440 end if;
3442 -- The working type may be subject to pragma Ghost. Set the mode now to
3443 -- ensure that the invariant procedure is properly marked as Ghost.
3445 Set_Ghost_Mode (Work_Typ);
3447 -- The type must either have invariants of its own, inherit class-wide
3448 -- invariants from parent types or interfaces, or be an array or record
3449 -- type whose components have invariants.
3451 pragma Assert (Has_Invariants (Work_Typ));
3453 -- Interfaces are treated as the partial view of a private type in order
3454 -- to achieve uniformity with the general case.
3456 if Is_Interface (Work_Typ) then
3457 Priv_Typ := Work_Typ;
3459 -- Otherwise obtain both views of the type
3461 else
3462 Get_Views (Work_Typ, Priv_Typ, Full_Typ, Dummy, CRec_Typ);
3463 end if;
3465 -- The caller requests a body for the partial invariant procedure
3467 if Partial_Invariant then
3468 Full_Proc := Invariant_Procedure (Work_Typ);
3469 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3471 -- The "full" invariant procedure body was already created
3473 if Present (Full_Proc)
3474 and then Present
3475 (Corresponding_Body (Unit_Declaration_Node (Full_Proc)))
3476 then
3477 -- This scenario happens only when the type is an untagged
3478 -- derivation from a private parent and the underlying full
3479 -- view was processed before the partial view.
3481 pragma Assert
3482 (Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ));
3484 -- Nothing to do because the processing of the underlying full
3485 -- view already checked the invariants of the partial view.
3487 goto Leave;
3488 end if;
3490 -- Create a declaration for the "partial" invariant procedure if it
3491 -- is not available.
3493 if No (Proc_Id) then
3494 Build_Invariant_Procedure_Declaration
3495 (Typ => Work_Typ,
3496 Partial_Invariant => True);
3498 Proc_Id := Partial_Invariant_Procedure (Work_Typ);
3499 end if;
3501 -- The caller requests a body for the "full" invariant procedure
3503 else
3504 Proc_Id := Invariant_Procedure (Work_Typ);
3505 Part_Proc := Partial_Invariant_Procedure (Work_Typ);
3507 -- Create a declaration for the "full" invariant procedure if it is
3508 -- not available.
3510 if No (Proc_Id) then
3511 Build_Invariant_Procedure_Declaration (Work_Typ);
3512 Proc_Id := Invariant_Procedure (Work_Typ);
3513 end if;
3514 end if;
3516 -- At this point there should be an invariant procedure declaration
3518 pragma Assert (Present (Proc_Id));
3519 Proc_Decl := Unit_Declaration_Node (Proc_Id);
3521 -- Nothing to do if the invariant procedure already has a body
3523 if Present (Corresponding_Body (Proc_Decl)) then
3524 goto Leave;
3525 end if;
3527 -- Emulate the environment of the invariant procedure by installing its
3528 -- scope and formal parameters. Note that this is not needed, but having
3529 -- the scope installed helps with the detection of invariant-related
3530 -- errors.
3532 Push_Scope (Proc_Id);
3533 Install_Formals (Proc_Id);
3535 Obj_Id := First_Formal (Proc_Id);
3536 pragma Assert (Present (Obj_Id));
3538 -- The "partial" invariant procedure verifies the invariants of the
3539 -- partial view only.
3541 if Partial_Invariant then
3542 pragma Assert (Present (Priv_Typ));
3544 Add_Own_Invariants
3545 (T => Priv_Typ,
3546 Obj_Id => Obj_Id,
3547 Checks => Stmts);
3549 -- Otherwise the "full" invariant procedure verifies the invariants of
3550 -- the full view, all array or record components, as well as class-wide
3551 -- invariants inherited from parent types or interfaces. In addition, it
3552 -- indirectly verifies the invariants of the partial view by calling the
3553 -- "partial" invariant procedure.
3555 else
3556 pragma Assert (Present (Full_Typ));
3558 -- Check the invariants of the partial view by calling the "partial"
3559 -- invariant procedure. Generate:
3561 -- <Work_Typ>Partial_Invariant (_object);
3563 if Present (Part_Proc) then
3564 Append_New_To (Stmts,
3565 Make_Procedure_Call_Statement (Loc,
3566 Name => New_Occurrence_Of (Part_Proc, Loc),
3567 Parameter_Associations => New_List (
3568 New_Occurrence_Of (Obj_Id, Loc))));
3570 Produced_Check := True;
3571 end if;
3573 Priv_Item := Empty;
3575 -- Derived subtypes do not have a partial view
3577 if Present (Priv_Typ) then
3579 -- The processing of the "full" invariant procedure intentionally
3580 -- skips the partial view because a) this may result in changes of
3581 -- visibility and b) lead to duplicate checks. However, when the
3582 -- full view is the underlying full view of an untagged derived
3583 -- type whose parent type is private, partial invariants appear on
3584 -- the rep item chain of the partial view only.
3586 -- package Pack_1 is
3587 -- type Root ... is private;
3588 -- private
3589 -- <full view of Root>
3590 -- end Pack_1;
3592 -- with Pack_1;
3593 -- package Pack_2 is
3594 -- type Child is new Pack_1.Root with Type_Invariant => ...;
3595 -- <underlying full view of Child>
3596 -- end Pack_2;
3598 -- As a result, the processing of the full view must also consider
3599 -- all invariants of the partial view.
3601 if Is_Untagged_Private_Derivation (Priv_Typ, Full_Typ) then
3602 null;
3604 -- Otherwise the invariants of the partial view are ignored
3606 else
3607 -- Note that the rep item chain is shared between the partial
3608 -- and full views of a type. To avoid processing the invariants
3609 -- of the partial view, signal the logic to stop when the first
3610 -- rep item of the partial view has been reached.
3612 Priv_Item := First_Rep_Item (Priv_Typ);
3614 -- Ignore the invariants of the partial view by eliminating the
3615 -- view.
3617 Priv_Typ := Empty;
3618 end if;
3619 end if;
3621 -- Process the invariants of the full view and in certain cases those
3622 -- of the partial view. This also handles any invariants on array or
3623 -- record components.
3625 Add_Own_Invariants
3626 (T => Priv_Typ,
3627 Obj_Id => Obj_Id,
3628 Checks => Stmts,
3629 Priv_Item => Priv_Item);
3631 Add_Own_Invariants
3632 (T => Full_Typ,
3633 Obj_Id => Obj_Id,
3634 Checks => Stmts,
3635 Priv_Item => Priv_Item);
3637 -- Process the elements of an array type
3639 if Is_Array_Type (Full_Typ) then
3640 Add_Array_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3642 -- Process the components of a record type
3644 elsif Ekind (Full_Typ) = E_Record_Type then
3645 Add_Record_Component_Invariants (Full_Typ, Obj_Id, Stmts);
3647 -- Process the components of a corresponding record
3649 elsif Present (CRec_Typ) then
3650 Add_Record_Component_Invariants (CRec_Typ, Obj_Id, Stmts);
3651 end if;
3653 -- Process the inherited class-wide invariants of all parent types.
3654 -- This also handles any invariants on record components.
3656 Add_Parent_Invariants (Full_Typ, Obj_Id, Stmts);
3658 -- Process the inherited class-wide invariants of all implemented
3659 -- interface types.
3661 Add_Interface_Invariants (Full_Typ, Obj_Id, Stmts);
3662 end if;
3664 End_Scope;
3666 -- At this point there should be at least one invariant check. If this
3667 -- is not the case, then the invariant-related flags were not properly
3668 -- set, or there is a missing invariant procedure on one of the array
3669 -- or record components.
3671 pragma Assert (Produced_Check);
3673 -- Account for the case where assertions are disabled or all invariant
3674 -- checks are subject to Assertion_Policy Ignore. Produce a completing
3675 -- empty body.
3677 if No (Stmts) then
3678 Stmts := New_List (Make_Null_Statement (Loc));
3679 end if;
3681 -- Generate:
3682 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>) is
3683 -- begin
3684 -- <Stmts>
3685 -- end <Work_Typ>[Partial_]Invariant;
3687 Proc_Body :=
3688 Make_Subprogram_Body (Loc,
3689 Specification =>
3690 Copy_Subprogram_Spec (Parent (Proc_Id)),
3691 Declarations => Empty_List,
3692 Handled_Statement_Sequence =>
3693 Make_Handled_Sequence_Of_Statements (Loc,
3694 Statements => Stmts));
3695 Proc_Body_Id := Defining_Entity (Proc_Body);
3697 -- Perform minor decoration in case the body is not analyzed
3699 Mutate_Ekind (Proc_Body_Id, E_Subprogram_Body);
3700 Set_Etype (Proc_Body_Id, Standard_Void_Type);
3701 Set_Scope (Proc_Body_Id, Current_Scope);
3703 -- Link both spec and body to avoid generating duplicates
3705 Set_Corresponding_Body (Proc_Decl, Proc_Body_Id);
3706 Set_Corresponding_Spec (Proc_Body, Proc_Id);
3708 -- The body should not be inserted into the tree when the context is
3709 -- a generic unit because it is not part of the template. Note
3710 -- that the body must still be generated in order to resolve the
3711 -- invariants.
3713 if Inside_A_Generic then
3714 null;
3716 -- Semi-insert the body into the tree for GNATprove by setting its
3717 -- Parent field. This allows for proper upstream tree traversals.
3719 elsif GNATprove_Mode then
3720 Set_Parent (Proc_Body, Parent (Declaration_Node (Work_Typ)));
3722 -- Otherwise the body is part of the freezing actions of the type
3724 else
3725 Append_Freeze_Action (Work_Typ, Proc_Body);
3726 end if;
3728 <<Leave>>
3729 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3730 end Build_Invariant_Procedure_Body;
3732 -------------------------------------------
3733 -- Build_Invariant_Procedure_Declaration --
3734 -------------------------------------------
3736 -- WARNING: This routine manages Ghost regions. Return statements must be
3737 -- replaced by gotos which jump to the end of the routine and restore the
3738 -- Ghost mode.
3740 procedure Build_Invariant_Procedure_Declaration
3741 (Typ : Entity_Id;
3742 Partial_Invariant : Boolean := False)
3744 Loc : constant Source_Ptr := Sloc (Typ);
3746 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3747 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3748 -- Save the Ghost-related attributes to restore on exit
3750 Proc_Decl : Node_Id;
3751 Proc_Id : Entity_Id;
3752 Proc_Nam : Name_Id;
3753 Typ_Decl : Node_Id;
3755 CRec_Typ : Entity_Id;
3756 -- The corresponding record type of Full_Typ
3758 Full_Typ : Entity_Id;
3759 -- The full view of working type
3761 Obj_Id : Entity_Id;
3762 -- The _object formal parameter of the invariant procedure
3764 Obj_Typ : Entity_Id;
3765 -- The type of the _object formal parameter
3767 Priv_Typ : Entity_Id;
3768 -- The partial view of working type
3770 UFull_Typ : Entity_Id;
3771 -- The underlying full view of Full_Typ
3773 Work_Typ : Entity_Id;
3774 -- The working type
3776 begin
3777 Work_Typ := Typ;
3779 -- The input type denotes the implementation base type of a constrained
3780 -- array type. Work with the first subtype as all invariant pragmas are
3781 -- on its rep item chain.
3783 if Ekind (Work_Typ) = E_Array_Type and then Is_Itype (Work_Typ) then
3784 Work_Typ := First_Subtype (Work_Typ);
3786 -- The input denotes the corresponding record type of a protected or a
3787 -- task type. Work with the concurrent type because the corresponding
3788 -- record type may not be visible to clients of the type.
3790 elsif Ekind (Work_Typ) = E_Record_Type
3791 and then Is_Concurrent_Record_Type (Work_Typ)
3792 then
3793 Work_Typ := Corresponding_Concurrent_Type (Work_Typ);
3794 end if;
3796 -- The working type may be subject to pragma Ghost. Set the mode now to
3797 -- ensure that the invariant procedure is properly marked as Ghost.
3799 Set_Ghost_Mode (Work_Typ);
3801 -- The type must either have invariants of its own, inherit class-wide
3802 -- invariants from parent or interface types, or be an array or record
3803 -- type whose components have invariants.
3805 pragma Assert (Has_Invariants (Work_Typ));
3807 -- Nothing to do if the type already has a "partial" invariant procedure
3809 if Partial_Invariant then
3810 if Present (Partial_Invariant_Procedure (Work_Typ)) then
3811 goto Leave;
3812 end if;
3814 -- Nothing to do if the type already has a "full" invariant procedure
3816 elsif Present (Invariant_Procedure (Work_Typ)) then
3817 goto Leave;
3818 end if;
3820 -- The caller requests the declaration of the "partial" invariant
3821 -- procedure.
3823 if Partial_Invariant then
3824 Proc_Nam := New_External_Name (Chars (Work_Typ), "Partial_Invariant");
3826 -- Otherwise the caller requests the declaration of the "full" invariant
3827 -- procedure.
3829 else
3830 Proc_Nam := New_External_Name (Chars (Work_Typ), "Invariant");
3831 end if;
3833 Proc_Id := Make_Defining_Identifier (Loc, Chars => Proc_Nam);
3835 -- Perform minor decoration in case the declaration is not analyzed
3837 Mutate_Ekind (Proc_Id, E_Procedure);
3838 Set_Etype (Proc_Id, Standard_Void_Type);
3839 Set_Scope (Proc_Id, Current_Scope);
3841 if Partial_Invariant then
3842 Set_Is_Partial_Invariant_Procedure (Proc_Id);
3843 Set_Partial_Invariant_Procedure (Work_Typ, Proc_Id);
3844 else
3845 Set_Is_Invariant_Procedure (Proc_Id);
3846 Set_Invariant_Procedure (Work_Typ, Proc_Id);
3847 end if;
3849 -- The invariant procedure requires debug info when the invariants are
3850 -- subject to Source Coverage Obligations.
3852 if Generate_SCO then
3853 Set_Debug_Info_Needed (Proc_Id);
3854 end if;
3856 -- Obtain all views of the input type
3858 Get_Views (Work_Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
3860 -- Associate the invariant procedure and various flags with all views
3862 Propagate_Invariant_Attributes (Priv_Typ, From_Typ => Work_Typ);
3863 Propagate_Invariant_Attributes (Full_Typ, From_Typ => Work_Typ);
3864 Propagate_Invariant_Attributes (UFull_Typ, From_Typ => Work_Typ);
3865 Propagate_Invariant_Attributes (CRec_Typ, From_Typ => Work_Typ);
3867 -- The declaration of the invariant procedure is inserted after the
3868 -- declaration of the partial view as this allows for proper external
3869 -- visibility.
3871 if Present (Priv_Typ) then
3872 Typ_Decl := Declaration_Node (Priv_Typ);
3874 -- Anonymous arrays in object declarations have no explicit declaration
3875 -- so use the related object declaration as the insertion point.
3877 elsif Is_Itype (Work_Typ) and then Is_Array_Type (Work_Typ) then
3878 Typ_Decl := Associated_Node_For_Itype (Work_Typ);
3880 -- Derived types with the full view as parent do not have a partial
3881 -- view. Insert the invariant procedure after the derived type.
3883 else
3884 Typ_Decl := Declaration_Node (Full_Typ);
3885 end if;
3887 -- The type should have a declarative node
3889 pragma Assert (Present (Typ_Decl));
3891 -- Create the formal parameter which emulates the variable-like behavior
3892 -- of the current type instance.
3894 Obj_Id := Make_Defining_Identifier (Loc, Chars => Name_uObject);
3896 -- When generating an invariant procedure declaration for an abstract
3897 -- type (including interfaces), use the class-wide type as the _object
3898 -- type. This has several desirable effects:
3900 -- * The invariant procedure does not become a primitive of the type.
3901 -- This eliminates the need to either special case the treatment of
3902 -- invariant procedures, or to make it a predefined primitive and
3903 -- force every derived type to potentially provide an empty body.
3905 -- * The invariant procedure does not need to be declared as abstract.
3906 -- This allows for a proper body, which in turn avoids redundant
3907 -- processing of the same invariants for types with multiple views.
3909 -- * The class-wide type allows for calls to abstract primitives
3910 -- within a nonabstract subprogram. The calls are treated as
3911 -- dispatching and require additional processing when they are
3912 -- remapped to call primitives of derived types. See routine
3913 -- Replace_References for details.
3915 if Is_Abstract_Type (Work_Typ) then
3916 Obj_Typ := Class_Wide_Type (Work_Typ);
3917 else
3918 Obj_Typ := Work_Typ;
3919 end if;
3921 -- Perform minor decoration in case the declaration is not analyzed
3923 Mutate_Ekind (Obj_Id, E_In_Parameter);
3924 Set_Etype (Obj_Id, Obj_Typ);
3925 Set_Scope (Obj_Id, Proc_Id);
3927 Set_First_Entity (Proc_Id, Obj_Id);
3928 Set_Last_Entity (Proc_Id, Obj_Id);
3930 -- Generate:
3931 -- procedure <Work_Typ>[Partial_]Invariant (_object : <Obj_Typ>);
3933 Proc_Decl :=
3934 Make_Subprogram_Declaration (Loc,
3935 Specification =>
3936 Make_Procedure_Specification (Loc,
3937 Defining_Unit_Name => Proc_Id,
3938 Parameter_Specifications => New_List (
3939 Make_Parameter_Specification (Loc,
3940 Defining_Identifier => Obj_Id,
3941 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc)))));
3943 -- The declaration should not be inserted into the tree when the context
3944 -- is a generic unit because it is not part of the template.
3946 if Inside_A_Generic then
3947 null;
3949 -- Semi-insert the declaration into the tree for GNATprove by setting
3950 -- its Parent field. This allows for proper upstream tree traversals.
3952 elsif GNATprove_Mode then
3953 Set_Parent (Proc_Decl, Parent (Typ_Decl));
3955 -- Otherwise insert the declaration
3957 else
3958 pragma Assert (Present (Typ_Decl));
3959 Insert_After_And_Analyze (Typ_Decl, Proc_Decl);
3960 end if;
3962 <<Leave>>
3963 Restore_Ghost_Region (Saved_GM, Saved_IGR);
3964 end Build_Invariant_Procedure_Declaration;
3966 --------------------------
3967 -- Build_Procedure_Form --
3968 --------------------------
3970 procedure Build_Procedure_Form (N : Node_Id) is
3971 Loc : constant Source_Ptr := Sloc (N);
3972 Subp : constant Entity_Id := Defining_Entity (N);
3974 Func_Formal : Entity_Id;
3975 Proc_Formals : List_Id;
3976 Proc_Decl : Node_Id;
3978 begin
3979 -- No action needed if this transformation was already done, or in case
3980 -- of subprogram renaming declarations.
3982 if Nkind (Specification (N)) = N_Procedure_Specification
3983 or else Nkind (N) = N_Subprogram_Renaming_Declaration
3984 then
3985 return;
3986 end if;
3988 -- Ditto when dealing with an expression function, where both the
3989 -- original expression and the generated declaration end up being
3990 -- expanded here.
3992 if Rewritten_For_C (Subp) then
3993 return;
3994 end if;
3996 Proc_Formals := New_List;
3998 -- Create a list of formal parameters with the same types as the
3999 -- function.
4001 Func_Formal := First_Formal (Subp);
4002 while Present (Func_Formal) loop
4003 Append_To (Proc_Formals,
4004 Make_Parameter_Specification (Loc,
4005 Defining_Identifier =>
4006 Make_Defining_Identifier (Loc, Chars (Func_Formal)),
4007 Parameter_Type =>
4008 New_Occurrence_Of (Etype (Func_Formal), Loc)));
4010 Next_Formal (Func_Formal);
4011 end loop;
4013 -- Add an extra out parameter to carry the function result
4015 Append_To (Proc_Formals,
4016 Make_Parameter_Specification (Loc,
4017 Defining_Identifier =>
4018 Make_Defining_Identifier (Loc, Name_UP_RESULT),
4019 Out_Present => True,
4020 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
4022 -- The new procedure declaration is inserted before the function
4023 -- declaration. The processing in Build_Procedure_Body_Form relies on
4024 -- this order. Note that we insert before because in the case of a
4025 -- function body with no separate spec, we do not want to insert the
4026 -- new spec after the body which will later get rewritten.
4028 Proc_Decl :=
4029 Make_Subprogram_Declaration (Loc,
4030 Specification =>
4031 Make_Procedure_Specification (Loc,
4032 Defining_Unit_Name =>
4033 Make_Defining_Identifier (Loc, Chars (Subp)),
4034 Parameter_Specifications => Proc_Formals));
4036 Insert_Before_And_Analyze (Unit_Declaration_Node (Subp), Proc_Decl);
4038 -- Entity of procedure must remain invisible so that it does not
4039 -- overload subsequent references to the original function.
4041 Set_Is_Immediately_Visible (Defining_Entity (Proc_Decl), False);
4043 -- Mark the function as having a procedure form and link the function
4044 -- and its internally built procedure.
4046 Set_Rewritten_For_C (Subp);
4047 Set_Corresponding_Procedure (Subp, Defining_Entity (Proc_Decl));
4048 Set_Corresponding_Function (Defining_Entity (Proc_Decl), Subp);
4049 end Build_Procedure_Form;
4051 ------------------------
4052 -- Build_Runtime_Call --
4053 ------------------------
4055 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
4056 begin
4057 -- If entity is not available, we can skip making the call (this avoids
4058 -- junk duplicated error messages in a number of cases).
4060 if not RTE_Available (RE) then
4061 return Make_Null_Statement (Loc);
4062 else
4063 return
4064 Make_Procedure_Call_Statement (Loc,
4065 Name => New_Occurrence_Of (RTE (RE), Loc));
4066 end if;
4067 end Build_Runtime_Call;
4069 ------------------------
4070 -- Build_SS_Mark_Call --
4071 ------------------------
4073 function Build_SS_Mark_Call
4074 (Loc : Source_Ptr;
4075 Mark : Entity_Id) return Node_Id
4077 begin
4078 -- Generate:
4079 -- Mark : constant Mark_Id := SS_Mark;
4081 return
4082 Make_Object_Declaration (Loc,
4083 Defining_Identifier => Mark,
4084 Constant_Present => True,
4085 Object_Definition =>
4086 New_Occurrence_Of (RTE (RE_Mark_Id), Loc),
4087 Expression =>
4088 Make_Function_Call (Loc,
4089 Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)));
4090 end Build_SS_Mark_Call;
4092 ---------------------------
4093 -- Build_SS_Release_Call --
4094 ---------------------------
4096 function Build_SS_Release_Call
4097 (Loc : Source_Ptr;
4098 Mark : Entity_Id) return Node_Id
4100 begin
4101 -- Generate:
4102 -- SS_Release (Mark);
4104 return
4105 Make_Procedure_Call_Statement (Loc,
4106 Name =>
4107 New_Occurrence_Of (RTE (RE_SS_Release), Loc),
4108 Parameter_Associations => New_List (
4109 New_Occurrence_Of (Mark, Loc)));
4110 end Build_SS_Release_Call;
4112 ----------------------------
4113 -- Build_Task_Array_Image --
4114 ----------------------------
4116 -- This function generates the body for a function that constructs the
4117 -- image string for a task that is an array component. The function is
4118 -- local to the init proc for the array type, and is called for each one
4119 -- of the components. The constructed image has the form of an indexed
4120 -- component, whose prefix is the outer variable of the array type.
4121 -- The n-dimensional array type has known indexes Index, Index2...
4123 -- Id_Ref is an indexed component form created by the enclosing init proc.
4124 -- Its successive indexes are Val1, Val2, ... which are the loop variables
4125 -- in the loops that call the individual task init proc on each component.
4127 -- The generated function has the following structure:
4129 -- function F return String is
4130 -- Pref : String renames Task_Name;
4131 -- T1 : constant String := Index1'Image (Val1);
4132 -- ...
4133 -- Tn : constant String := Indexn'Image (Valn);
4134 -- Len : constant Integer :=
4135 -- Pref'Length + T1'Length + ... + Tn'Length + n + 1;
4136 -- -- Len includes commas and the end parentheses
4138 -- Res : String (1 .. Len);
4139 -- Pos : Integer := Pref'Length;
4141 -- begin
4142 -- Res (1 .. Pos) := Pref;
4143 -- Pos := Pos + 1;
4144 -- Res (Pos) := '(';
4145 -- Pos := Pos + 1;
4146 -- Res (Pos .. Pos + T1'Length - 1) := T1;
4147 -- Pos := Pos + T1'Length;
4148 -- Res (Pos) := '.';
4149 -- Pos := Pos + 1;
4150 -- ...
4151 -- Res (Pos .. Pos + Tn'Length - 1) := Tn;
4152 -- Res (Len) := ')';
4154 -- return Res;
4155 -- end F;
4157 -- Needless to say, multidimensional arrays of tasks are rare enough that
4158 -- the bulkiness of this code is not really a concern.
4160 function Build_Task_Array_Image
4161 (Loc : Source_Ptr;
4162 Id_Ref : Node_Id;
4163 A_Type : Entity_Id;
4164 Dyn : Boolean := False) return Node_Id
4166 Dims : constant Nat := Number_Dimensions (A_Type);
4167 -- Number of dimensions for array of tasks
4169 Temps : array (1 .. Dims) of Entity_Id;
4170 -- Array of temporaries to hold string for each index
4172 Indx : Node_Id;
4173 -- Index expression
4175 Len : Entity_Id;
4176 -- Total length of generated name
4178 Pos : Entity_Id;
4179 -- Running index for substring assignments
4181 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4182 -- Name of enclosing variable, prefix of resulting name
4184 Res : Entity_Id;
4185 -- String to hold result
4187 Val : Node_Id;
4188 -- Value of successive indexes
4190 Sum : Node_Id;
4191 -- Expression to compute total size of string
4193 T : Entity_Id;
4194 -- Entity for name at one index position
4196 Decls : constant List_Id := New_List;
4197 Stats : constant List_Id := New_List;
4199 begin
4200 -- For a dynamic task, the name comes from the target variable. For a
4201 -- static one it is a formal of the enclosing init proc.
4203 if Dyn then
4204 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4205 Append_To (Decls,
4206 Make_Object_Declaration (Loc,
4207 Defining_Identifier => Pref,
4208 Constant_Present => True,
4209 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4210 Expression =>
4211 Make_String_Literal (Loc,
4212 Strval => String_From_Name_Buffer)));
4214 else
4215 Append_To (Decls,
4216 Make_Object_Renaming_Declaration (Loc,
4217 Defining_Identifier => Pref,
4218 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4219 Name => Make_Identifier (Loc, Name_uTask_Name)));
4220 end if;
4222 Indx := First_Index (A_Type);
4223 Val := First (Expressions (Id_Ref));
4225 for J in 1 .. Dims loop
4226 T := Make_Temporary (Loc, 'T');
4227 Temps (J) := T;
4229 Append_To (Decls,
4230 Make_Object_Declaration (Loc,
4231 Defining_Identifier => T,
4232 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4233 Constant_Present => True,
4234 Expression =>
4235 Make_Attribute_Reference (Loc,
4236 Attribute_Name => Name_Image,
4237 Prefix => New_Occurrence_Of (Etype (Indx), Loc),
4238 Expressions => New_List (New_Copy_Tree (Val)))));
4240 Next_Index (Indx);
4241 Next (Val);
4242 end loop;
4244 Sum := Make_Integer_Literal (Loc, Dims + 1);
4246 Sum :=
4247 Make_Op_Add (Loc,
4248 Left_Opnd => Sum,
4249 Right_Opnd =>
4250 Make_Attribute_Reference (Loc,
4251 Attribute_Name => Name_Length,
4252 Prefix => New_Occurrence_Of (Pref, Loc),
4253 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4255 for J in 1 .. Dims loop
4256 Sum :=
4257 Make_Op_Add (Loc,
4258 Left_Opnd => Sum,
4259 Right_Opnd =>
4260 Make_Attribute_Reference (Loc,
4261 Attribute_Name => Name_Length,
4262 Prefix =>
4263 New_Occurrence_Of (Temps (J), Loc),
4264 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4265 end loop;
4267 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4269 Set_Character_Literal_Name (Get_Char_Code ('('));
4271 Append_To (Stats,
4272 Make_Assignment_Statement (Loc,
4273 Name =>
4274 Make_Indexed_Component (Loc,
4275 Prefix => New_Occurrence_Of (Res, Loc),
4276 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4277 Expression =>
4278 Make_Character_Literal (Loc,
4279 Chars => Name_Find,
4280 Char_Literal_Value => UI_From_CC (Get_Char_Code ('(')))));
4282 Append_To (Stats,
4283 Make_Assignment_Statement (Loc,
4284 Name => New_Occurrence_Of (Pos, Loc),
4285 Expression =>
4286 Make_Op_Add (Loc,
4287 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4288 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4290 for J in 1 .. Dims loop
4292 Append_To (Stats,
4293 Make_Assignment_Statement (Loc,
4294 Name =>
4295 Make_Slice (Loc,
4296 Prefix => New_Occurrence_Of (Res, Loc),
4297 Discrete_Range =>
4298 Make_Range (Loc,
4299 Low_Bound => New_Occurrence_Of (Pos, Loc),
4300 High_Bound =>
4301 Make_Op_Subtract (Loc,
4302 Left_Opnd =>
4303 Make_Op_Add (Loc,
4304 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4305 Right_Opnd =>
4306 Make_Attribute_Reference (Loc,
4307 Attribute_Name => Name_Length,
4308 Prefix =>
4309 New_Occurrence_Of (Temps (J), Loc),
4310 Expressions =>
4311 New_List (Make_Integer_Literal (Loc, 1)))),
4312 Right_Opnd => Make_Integer_Literal (Loc, 1)))),
4314 Expression => New_Occurrence_Of (Temps (J), Loc)));
4316 if J < Dims then
4317 Append_To (Stats,
4318 Make_Assignment_Statement (Loc,
4319 Name => New_Occurrence_Of (Pos, Loc),
4320 Expression =>
4321 Make_Op_Add (Loc,
4322 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4323 Right_Opnd =>
4324 Make_Attribute_Reference (Loc,
4325 Attribute_Name => Name_Length,
4326 Prefix => New_Occurrence_Of (Temps (J), Loc),
4327 Expressions =>
4328 New_List (Make_Integer_Literal (Loc, 1))))));
4330 Set_Character_Literal_Name (Get_Char_Code (','));
4332 Append_To (Stats,
4333 Make_Assignment_Statement (Loc,
4334 Name => Make_Indexed_Component (Loc,
4335 Prefix => New_Occurrence_Of (Res, Loc),
4336 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4337 Expression =>
4338 Make_Character_Literal (Loc,
4339 Chars => Name_Find,
4340 Char_Literal_Value => UI_From_CC (Get_Char_Code (',')))));
4342 Append_To (Stats,
4343 Make_Assignment_Statement (Loc,
4344 Name => New_Occurrence_Of (Pos, Loc),
4345 Expression =>
4346 Make_Op_Add (Loc,
4347 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4348 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4349 end if;
4350 end loop;
4352 Set_Character_Literal_Name (Get_Char_Code (')'));
4354 Append_To (Stats,
4355 Make_Assignment_Statement (Loc,
4356 Name =>
4357 Make_Indexed_Component (Loc,
4358 Prefix => New_Occurrence_Of (Res, Loc),
4359 Expressions => New_List (New_Occurrence_Of (Len, Loc))),
4360 Expression =>
4361 Make_Character_Literal (Loc,
4362 Chars => Name_Find,
4363 Char_Literal_Value => UI_From_CC (Get_Char_Code (')')))));
4364 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4365 end Build_Task_Array_Image;
4367 ----------------------------
4368 -- Build_Task_Image_Decls --
4369 ----------------------------
4371 function Build_Task_Image_Decls
4372 (Loc : Source_Ptr;
4373 Id_Ref : Node_Id;
4374 A_Type : Entity_Id;
4375 In_Init_Proc : Boolean := False) return List_Id
4377 Decls : constant List_Id := New_List;
4378 T_Id : Entity_Id := Empty;
4379 Decl : Node_Id;
4380 Expr : Node_Id := Empty;
4381 Fun : Node_Id := Empty;
4382 Is_Dyn : constant Boolean :=
4383 Nkind (Parent (Id_Ref)) = N_Assignment_Statement
4384 and then
4385 Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
4387 Component_Suffix_Index : constant Int :=
4388 (if In_Init_Proc then -1 else 0);
4389 -- If an init proc calls Build_Task_Image_Decls twice for its
4390 -- _Parent component (to split early/late initialization), we don't
4391 -- want two decls with the same name. Hence, the -1 suffix.
4393 begin
4394 -- If Discard_Names or No_Implicit_Heap_Allocations are in effect,
4395 -- generate a dummy declaration only.
4397 if Restriction_Active (No_Implicit_Heap_Allocations)
4398 or else Global_Discard_Names
4399 then
4400 T_Id := Make_Temporary (Loc, 'J');
4401 Name_Len := 0;
4403 return
4404 New_List (
4405 Make_Object_Declaration (Loc,
4406 Defining_Identifier => T_Id,
4407 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4408 Expression =>
4409 Make_String_Literal (Loc,
4410 Strval => String_From_Name_Buffer)));
4412 else
4413 if Nkind (Id_Ref) = N_Identifier
4414 or else Nkind (Id_Ref) = N_Defining_Identifier
4415 then
4416 -- For a simple variable, the image of the task is built from
4417 -- the name of the variable. To avoid possible conflict with the
4418 -- anonymous type created for a single protected object, add a
4419 -- numeric suffix.
4421 T_Id :=
4422 Make_Defining_Identifier (Loc,
4423 New_External_Name (Chars (Id_Ref), 'T', 1));
4425 Get_Name_String (Chars (Id_Ref));
4427 Expr :=
4428 Make_String_Literal (Loc,
4429 Strval => String_From_Name_Buffer);
4431 elsif Nkind (Id_Ref) = N_Selected_Component then
4432 T_Id :=
4433 Make_Defining_Identifier (Loc,
4434 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T',
4435 Suffix_Index => Component_Suffix_Index));
4436 Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
4438 elsif Nkind (Id_Ref) = N_Indexed_Component then
4439 T_Id :=
4440 Make_Defining_Identifier (Loc,
4441 New_External_Name (Chars (A_Type), 'N'));
4443 Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
4444 end if;
4445 end if;
4447 if Present (Fun) then
4448 Append (Fun, Decls);
4449 Expr := Make_Function_Call (Loc,
4450 Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
4452 if not In_Init_Proc then
4453 Set_Uses_Sec_Stack (Defining_Entity (Fun));
4454 end if;
4455 end if;
4457 Decl := Make_Object_Declaration (Loc,
4458 Defining_Identifier => T_Id,
4459 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4460 Constant_Present => True,
4461 Expression => Expr);
4463 Append (Decl, Decls);
4464 return Decls;
4465 end Build_Task_Image_Decls;
4467 -------------------------------
4468 -- Build_Task_Image_Function --
4469 -------------------------------
4471 function Build_Task_Image_Function
4472 (Loc : Source_Ptr;
4473 Decls : List_Id;
4474 Stats : List_Id;
4475 Res : Entity_Id) return Node_Id
4477 Spec : Node_Id;
4479 begin
4480 Append_To (Stats,
4481 Make_Simple_Return_Statement (Loc,
4482 Expression => New_Occurrence_Of (Res, Loc)));
4484 Spec := Make_Function_Specification (Loc,
4485 Defining_Unit_Name => Make_Temporary (Loc, 'F'),
4486 Result_Definition => New_Occurrence_Of (Standard_String, Loc));
4488 -- Calls to 'Image use the secondary stack, which must be cleaned up
4489 -- after the task name is built.
4491 return Make_Subprogram_Body (Loc,
4492 Specification => Spec,
4493 Declarations => Decls,
4494 Handled_Statement_Sequence =>
4495 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
4496 end Build_Task_Image_Function;
4498 -----------------------------
4499 -- Build_Task_Image_Prefix --
4500 -----------------------------
4502 procedure Build_Task_Image_Prefix
4503 (Loc : Source_Ptr;
4504 Len : out Entity_Id;
4505 Res : out Entity_Id;
4506 Pos : out Entity_Id;
4507 Prefix : Entity_Id;
4508 Sum : Node_Id;
4509 Decls : List_Id;
4510 Stats : List_Id)
4512 begin
4513 Len := Make_Temporary (Loc, 'L', Sum);
4515 Append_To (Decls,
4516 Make_Object_Declaration (Loc,
4517 Defining_Identifier => Len,
4518 Constant_Present => True,
4519 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
4520 Expression => Sum));
4522 Res := Make_Temporary (Loc, 'R');
4524 Append_To (Decls,
4525 Make_Object_Declaration (Loc,
4526 Defining_Identifier => Res,
4527 Object_Definition =>
4528 Make_Subtype_Indication (Loc,
4529 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4530 Constraint =>
4531 Make_Index_Or_Discriminant_Constraint (Loc,
4532 Constraints =>
4533 New_List (
4534 Make_Range (Loc,
4535 Low_Bound => Make_Integer_Literal (Loc, 1),
4536 High_Bound => New_Occurrence_Of (Len, Loc)))))));
4538 -- Indicate that the result is an internal temporary, so it does not
4539 -- receive a bogus initialization when declaration is expanded. This
4540 -- is both efficient, and prevents anomalies in the handling of
4541 -- dynamic objects on the secondary stack.
4543 Set_Is_Internal (Res);
4544 Pos := Make_Temporary (Loc, 'P');
4546 Append_To (Decls,
4547 Make_Object_Declaration (Loc,
4548 Defining_Identifier => Pos,
4549 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc)));
4551 -- Pos := Prefix'Length;
4553 Append_To (Stats,
4554 Make_Assignment_Statement (Loc,
4555 Name => New_Occurrence_Of (Pos, Loc),
4556 Expression =>
4557 Make_Attribute_Reference (Loc,
4558 Attribute_Name => Name_Length,
4559 Prefix => New_Occurrence_Of (Prefix, Loc),
4560 Expressions => New_List (Make_Integer_Literal (Loc, 1)))));
4562 -- Res (1 .. Pos) := Prefix;
4564 Append_To (Stats,
4565 Make_Assignment_Statement (Loc,
4566 Name =>
4567 Make_Slice (Loc,
4568 Prefix => New_Occurrence_Of (Res, Loc),
4569 Discrete_Range =>
4570 Make_Range (Loc,
4571 Low_Bound => Make_Integer_Literal (Loc, 1),
4572 High_Bound => New_Occurrence_Of (Pos, Loc))),
4574 Expression => New_Occurrence_Of (Prefix, Loc)));
4576 Append_To (Stats,
4577 Make_Assignment_Statement (Loc,
4578 Name => New_Occurrence_Of (Pos, Loc),
4579 Expression =>
4580 Make_Op_Add (Loc,
4581 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4582 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4583 end Build_Task_Image_Prefix;
4585 -----------------------------
4586 -- Build_Task_Record_Image --
4587 -----------------------------
4589 function Build_Task_Record_Image
4590 (Loc : Source_Ptr;
4591 Id_Ref : Node_Id;
4592 Dyn : Boolean := False) return Node_Id
4594 Len : Entity_Id;
4595 -- Total length of generated name
4597 Pos : Entity_Id;
4598 -- Index into result
4600 Res : Entity_Id;
4601 -- String to hold result
4603 Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
4604 -- Name of enclosing variable, prefix of resulting name
4606 Sum : Node_Id;
4607 -- Expression to compute total size of string
4609 Sel : Entity_Id;
4610 -- Entity for selector name
4612 Decls : constant List_Id := New_List;
4613 Stats : constant List_Id := New_List;
4615 begin
4616 -- For a dynamic task, the name comes from the target variable. For a
4617 -- static one it is a formal of the enclosing init proc.
4619 if Dyn then
4620 Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
4621 Append_To (Decls,
4622 Make_Object_Declaration (Loc,
4623 Defining_Identifier => Pref,
4624 Constant_Present => True,
4625 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4626 Expression =>
4627 Make_String_Literal (Loc,
4628 Strval => String_From_Name_Buffer)));
4630 else
4631 Append_To (Decls,
4632 Make_Object_Renaming_Declaration (Loc,
4633 Defining_Identifier => Pref,
4634 Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
4635 Name => Make_Identifier (Loc, Name_uTask_Name)));
4636 end if;
4638 Sel := Make_Temporary (Loc, 'S');
4640 Get_Name_String (Chars (Selector_Name (Id_Ref)));
4642 Append_To (Decls,
4643 Make_Object_Declaration (Loc,
4644 Defining_Identifier => Sel,
4645 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
4646 Expression =>
4647 Make_String_Literal (Loc,
4648 Strval => String_From_Name_Buffer)));
4650 Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
4652 Sum :=
4653 Make_Op_Add (Loc,
4654 Left_Opnd => Sum,
4655 Right_Opnd =>
4656 Make_Attribute_Reference (Loc,
4657 Attribute_Name => Name_Length,
4658 Prefix =>
4659 New_Occurrence_Of (Pref, Loc),
4660 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
4662 Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
4664 Set_Character_Literal_Name (Get_Char_Code ('.'));
4666 -- Res (Pos) := '.';
4668 Append_To (Stats,
4669 Make_Assignment_Statement (Loc,
4670 Name => Make_Indexed_Component (Loc,
4671 Prefix => New_Occurrence_Of (Res, Loc),
4672 Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
4673 Expression =>
4674 Make_Character_Literal (Loc,
4675 Chars => Name_Find,
4676 Char_Literal_Value =>
4677 UI_From_CC (Get_Char_Code ('.')))));
4679 Append_To (Stats,
4680 Make_Assignment_Statement (Loc,
4681 Name => New_Occurrence_Of (Pos, Loc),
4682 Expression =>
4683 Make_Op_Add (Loc,
4684 Left_Opnd => New_Occurrence_Of (Pos, Loc),
4685 Right_Opnd => Make_Integer_Literal (Loc, 1))));
4687 -- Res (Pos .. Len) := Selector;
4689 Append_To (Stats,
4690 Make_Assignment_Statement (Loc,
4691 Name => Make_Slice (Loc,
4692 Prefix => New_Occurrence_Of (Res, Loc),
4693 Discrete_Range =>
4694 Make_Range (Loc,
4695 Low_Bound => New_Occurrence_Of (Pos, Loc),
4696 High_Bound => New_Occurrence_Of (Len, Loc))),
4697 Expression => New_Occurrence_Of (Sel, Loc)));
4699 return Build_Task_Image_Function (Loc, Decls, Stats, Res);
4700 end Build_Task_Record_Image;
4702 ---------------------------------------
4703 -- Build_Transient_Object_Statements --
4704 ---------------------------------------
4706 procedure Build_Transient_Object_Statements
4707 (Obj_Decl : Node_Id;
4708 Fin_Call : out Node_Id;
4709 Hook_Assign : out Node_Id;
4710 Hook_Clear : out Node_Id;
4711 Hook_Decl : out Node_Id;
4712 Ptr_Decl : out Node_Id;
4713 Finalize_Obj : Boolean := True)
4715 Loc : constant Source_Ptr := Sloc (Obj_Decl);
4716 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
4717 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
4719 Desig_Typ : Entity_Id;
4720 Hook_Expr : Node_Id;
4721 Hook_Id : Entity_Id;
4722 Obj_Ref : Node_Id;
4723 Ptr_Typ : Entity_Id;
4725 begin
4726 -- Recover the type of the object
4728 Desig_Typ := Obj_Typ;
4730 if Is_Access_Type (Desig_Typ) then
4731 Desig_Typ := Available_View (Designated_Type (Desig_Typ));
4732 end if;
4734 -- Create an access type which provides a reference to the transient
4735 -- object. Generate:
4737 -- type Ptr_Typ is access all Desig_Typ;
4739 Ptr_Typ := Make_Temporary (Loc, 'A');
4740 Mutate_Ekind (Ptr_Typ, E_General_Access_Type);
4741 Set_Directly_Designated_Type (Ptr_Typ, Desig_Typ);
4743 Ptr_Decl :=
4744 Make_Full_Type_Declaration (Loc,
4745 Defining_Identifier => Ptr_Typ,
4746 Type_Definition =>
4747 Make_Access_To_Object_Definition (Loc,
4748 All_Present => True,
4749 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)));
4751 -- Create a temporary check which acts as a hook to the transient
4752 -- object. Generate:
4754 -- Hook : Ptr_Typ := null;
4756 Hook_Id := Make_Temporary (Loc, 'T');
4757 Mutate_Ekind (Hook_Id, E_Variable);
4758 Set_Etype (Hook_Id, Ptr_Typ);
4760 Hook_Decl :=
4761 Make_Object_Declaration (Loc,
4762 Defining_Identifier => Hook_Id,
4763 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
4764 Expression => Make_Null (Loc));
4766 -- Mark the temporary as a hook. This signals the machinery in
4767 -- Build_Finalizer to recognize this special case.
4769 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Obj_Decl);
4771 -- Hook the transient object to the temporary. Generate:
4773 -- Hook := Ptr_Typ (Obj_Id);
4774 -- <or>
4775 -- Hool := Obj_Id'Unrestricted_Access;
4777 if Is_Access_Type (Obj_Typ) then
4778 Hook_Expr :=
4779 Unchecked_Convert_To (Ptr_Typ, New_Occurrence_Of (Obj_Id, Loc));
4780 else
4781 Hook_Expr :=
4782 Make_Attribute_Reference (Loc,
4783 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4784 Attribute_Name => Name_Unrestricted_Access);
4785 end if;
4787 Hook_Assign :=
4788 Make_Assignment_Statement (Loc,
4789 Name => New_Occurrence_Of (Hook_Id, Loc),
4790 Expression => Hook_Expr);
4792 -- Crear the hook prior to finalizing the object. Generate:
4794 -- Hook := null;
4796 Hook_Clear :=
4797 Make_Assignment_Statement (Loc,
4798 Name => New_Occurrence_Of (Hook_Id, Loc),
4799 Expression => Make_Null (Loc));
4801 -- Finalize the object. Generate:
4803 -- [Deep_]Finalize (Obj_Ref[.all]);
4805 if Finalize_Obj then
4806 Obj_Ref := New_Occurrence_Of (Obj_Id, Loc);
4808 if Is_Access_Type (Obj_Typ) then
4809 Obj_Ref := Make_Explicit_Dereference (Loc, Obj_Ref);
4810 Set_Etype (Obj_Ref, Desig_Typ);
4811 end if;
4813 Fin_Call :=
4814 Make_Final_Call
4815 (Obj_Ref => Obj_Ref,
4816 Typ => Desig_Typ);
4818 -- Otherwise finalize the hook. Generate:
4820 -- [Deep_]Finalize (Hook.all);
4822 else
4823 Fin_Call :=
4824 Make_Final_Call (
4825 Obj_Ref =>
4826 Make_Explicit_Dereference (Loc,
4827 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
4828 Typ => Desig_Typ);
4829 end if;
4830 end Build_Transient_Object_Statements;
4832 -----------------------------
4833 -- Check_Float_Op_Overflow --
4834 -----------------------------
4836 procedure Check_Float_Op_Overflow (N : Node_Id) is
4837 begin
4838 -- Return if no check needed
4840 if not Is_Floating_Point_Type (Etype (N))
4841 or else not (Do_Overflow_Check (N) and then Check_Float_Overflow)
4843 -- In CodePeer_Mode, rely on the overflow check flag being set instead
4844 -- and do not expand the code for float overflow checking.
4846 or else CodePeer_Mode
4847 then
4848 return;
4849 end if;
4851 -- Otherwise we replace the expression by
4853 -- do Tnn : constant ftype := expression;
4854 -- constraint_error when not Tnn'Valid;
4855 -- in Tnn;
4857 declare
4858 Loc : constant Source_Ptr := Sloc (N);
4859 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
4860 Typ : constant Entity_Id := Etype (N);
4862 begin
4863 -- Turn off the Do_Overflow_Check flag, since we are doing that work
4864 -- right here. We also set the node as analyzed to prevent infinite
4865 -- recursion from repeating the operation in the expansion.
4867 Set_Do_Overflow_Check (N, False);
4868 Set_Analyzed (N, True);
4870 -- Do the rewrite to include the check
4872 Rewrite (N,
4873 Make_Expression_With_Actions (Loc,
4874 Actions => New_List (
4875 Make_Object_Declaration (Loc,
4876 Defining_Identifier => Tnn,
4877 Object_Definition => New_Occurrence_Of (Typ, Loc),
4878 Constant_Present => True,
4879 Expression => Relocate_Node (N)),
4880 Make_Raise_Constraint_Error (Loc,
4881 Condition =>
4882 Make_Op_Not (Loc,
4883 Right_Opnd =>
4884 Make_Attribute_Reference (Loc,
4885 Prefix => New_Occurrence_Of (Tnn, Loc),
4886 Attribute_Name => Name_Valid)),
4887 Reason => CE_Overflow_Check_Failed)),
4888 Expression => New_Occurrence_Of (Tnn, Loc)));
4890 Analyze_And_Resolve (N, Typ);
4891 end;
4892 end Check_Float_Op_Overflow;
4894 ----------------------------------
4895 -- Component_May_Be_Bit_Aligned --
4896 ----------------------------------
4898 function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
4899 UT : Entity_Id;
4901 begin
4902 -- If no component clause, then everything is fine, since the back end
4903 -- never misaligns from byte boundaries by default, even if there is a
4904 -- pragma Pack for the record.
4906 if No (Comp) or else No (Component_Clause (Comp)) then
4907 return False;
4908 end if;
4910 UT := Underlying_Type (Etype (Comp));
4912 -- It is only array and record types that cause trouble
4914 if not Is_Record_Type (UT) and then not Is_Array_Type (UT) then
4915 return False;
4917 -- If we know that we have a small (at most the maximum integer size)
4918 -- record or bit-packed array, then everything is fine, since the back
4919 -- end can handle these cases correctly.
4921 elsif Known_Esize (Comp)
4922 and then Esize (Comp) <= System_Max_Integer_Size
4923 and then (Is_Record_Type (UT) or else Is_Bit_Packed_Array (UT))
4924 then
4925 return False;
4927 elsif not Known_Normalized_First_Bit (Comp) then
4928 return True;
4930 -- Otherwise if the component is not byte aligned, we know we have the
4931 -- nasty unaligned case.
4933 elsif Normalized_First_Bit (Comp) /= Uint_0
4934 or else Esize (Comp) mod System_Storage_Unit /= Uint_0
4935 then
4936 return True;
4938 -- If we are large and byte aligned, then OK at this level
4940 else
4941 return False;
4942 end if;
4943 end Component_May_Be_Bit_Aligned;
4945 -------------------------------
4946 -- Convert_To_Actual_Subtype --
4947 -------------------------------
4949 procedure Convert_To_Actual_Subtype (Exp : Node_Id) is
4950 Act_ST : Entity_Id;
4952 begin
4953 Act_ST := Get_Actual_Subtype (Exp);
4955 if Act_ST = Etype (Exp) then
4956 return;
4957 else
4958 Rewrite (Exp, Convert_To (Act_ST, Relocate_Node (Exp)));
4959 Analyze_And_Resolve (Exp, Act_ST);
4960 end if;
4961 end Convert_To_Actual_Subtype;
4963 -----------------------------------
4964 -- Corresponding_Runtime_Package --
4965 -----------------------------------
4967 function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
4968 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean;
4969 -- Return True if protected type T has one entry and the maximum queue
4970 -- length is one.
4972 --------------------------------
4973 -- Has_One_Entry_And_No_Queue --
4974 --------------------------------
4976 function Has_One_Entry_And_No_Queue (T : Entity_Id) return Boolean is
4977 Item : Entity_Id;
4978 Is_First : Boolean := True;
4980 begin
4981 Item := First_Entity (T);
4982 while Present (Item) loop
4983 if Is_Entry (Item) then
4985 -- The protected type has more than one entry
4987 if not Is_First then
4988 return False;
4989 end if;
4991 -- The queue length is not one
4993 if not Restriction_Active (No_Entry_Queue)
4994 and then Get_Max_Queue_Length (Item) /= Uint_1
4995 then
4996 return False;
4997 end if;
4999 Is_First := False;
5000 end if;
5002 Next_Entity (Item);
5003 end loop;
5005 return True;
5006 end Has_One_Entry_And_No_Queue;
5008 -- Local variables
5010 Pkg_Id : RTU_Id := RTU_Null;
5012 -- Start of processing for Corresponding_Runtime_Package
5014 begin
5015 pragma Assert (Is_Concurrent_Type (Typ));
5017 if Is_Protected_Type (Typ) then
5018 if Has_Entries (Typ)
5020 -- A protected type without entries that covers an interface and
5021 -- overrides the abstract routines with protected procedures is
5022 -- considered equivalent to a protected type with entries in the
5023 -- context of dispatching select statements. It is sufficient to
5024 -- check for the presence of an interface list in the declaration
5025 -- node to recognize this case.
5027 or else Present (Interface_List (Parent (Typ)))
5029 -- Protected types with interrupt handlers (when not using a
5030 -- restricted profile) are also considered equivalent to
5031 -- protected types with entries. The types which are used
5032 -- (Static_Interrupt_Protection and Dynamic_Interrupt_Protection)
5033 -- are derived from Protection_Entries.
5035 or else (Has_Attach_Handler (Typ) and then not Restricted_Profile)
5036 or else Has_Interrupt_Handler (Typ)
5037 then
5038 if Abort_Allowed
5039 or else Restriction_Active (No_Select_Statements) = False
5040 or else not Has_One_Entry_And_No_Queue (Typ)
5041 or else (Has_Attach_Handler (Typ)
5042 and then not Restricted_Profile)
5043 then
5044 Pkg_Id := System_Tasking_Protected_Objects_Entries;
5045 else
5046 Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
5047 end if;
5049 else
5050 Pkg_Id := System_Tasking_Protected_Objects;
5051 end if;
5052 end if;
5054 return Pkg_Id;
5055 end Corresponding_Runtime_Package;
5057 -----------------------------------
5058 -- Current_Sem_Unit_Declarations --
5059 -----------------------------------
5061 function Current_Sem_Unit_Declarations return List_Id is
5062 U : Node_Id := Unit (Cunit (Current_Sem_Unit));
5063 Decls : List_Id;
5065 begin
5066 -- If the current unit is a package body, locate the visible
5067 -- declarations of the package spec.
5069 if Nkind (U) = N_Package_Body then
5070 U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
5071 end if;
5073 if Nkind (U) = N_Package_Declaration then
5074 U := Specification (U);
5075 Decls := Visible_Declarations (U);
5077 if No (Decls) then
5078 Decls := New_List;
5079 Set_Visible_Declarations (U, Decls);
5080 end if;
5082 else
5083 Decls := Declarations (U);
5085 if No (Decls) then
5086 Decls := New_List;
5087 Set_Declarations (U, Decls);
5088 end if;
5089 end if;
5091 return Decls;
5092 end Current_Sem_Unit_Declarations;
5094 -----------------------
5095 -- Duplicate_Subexpr --
5096 -----------------------
5098 function Duplicate_Subexpr
5099 (Exp : Node_Id;
5100 Name_Req : Boolean := False;
5101 Renaming_Req : Boolean := False) return Node_Id
5103 begin
5104 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5105 return New_Copy_Tree (Exp);
5106 end Duplicate_Subexpr;
5108 ---------------------------------
5109 -- Duplicate_Subexpr_No_Checks --
5110 ---------------------------------
5112 function Duplicate_Subexpr_No_Checks
5113 (Exp : Node_Id;
5114 Name_Req : Boolean := False;
5115 Renaming_Req : Boolean := False;
5116 Related_Id : Entity_Id := Empty;
5117 Is_Low_Bound : Boolean := False;
5118 Is_High_Bound : Boolean := False) return Node_Id
5120 New_Exp : Node_Id;
5122 begin
5123 Remove_Side_Effects
5124 (Exp => Exp,
5125 Name_Req => Name_Req,
5126 Renaming_Req => Renaming_Req,
5127 Related_Id => Related_Id,
5128 Is_Low_Bound => Is_Low_Bound,
5129 Is_High_Bound => Is_High_Bound);
5131 New_Exp := New_Copy_Tree (Exp);
5132 Remove_Checks (New_Exp);
5133 return New_Exp;
5134 end Duplicate_Subexpr_No_Checks;
5136 -----------------------------------
5137 -- Duplicate_Subexpr_Move_Checks --
5138 -----------------------------------
5140 function Duplicate_Subexpr_Move_Checks
5141 (Exp : Node_Id;
5142 Name_Req : Boolean := False;
5143 Renaming_Req : Boolean := False) return Node_Id
5145 New_Exp : Node_Id;
5147 begin
5148 Remove_Side_Effects (Exp, Name_Req, Renaming_Req);
5149 New_Exp := New_Copy_Tree (Exp);
5150 Remove_Checks (Exp);
5151 return New_Exp;
5152 end Duplicate_Subexpr_Move_Checks;
5154 -------------------------
5155 -- Enclosing_Init_Proc --
5156 -------------------------
5158 function Enclosing_Init_Proc return Entity_Id is
5159 S : Entity_Id;
5161 begin
5162 S := Current_Scope;
5163 while Present (S) and then S /= Standard_Standard loop
5164 if Is_Init_Proc (S) then
5165 return S;
5166 else
5167 S := Scope (S);
5168 end if;
5169 end loop;
5171 return Empty;
5172 end Enclosing_Init_Proc;
5174 --------------------
5175 -- Ensure_Defined --
5176 --------------------
5178 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
5179 IR : Node_Id;
5181 begin
5182 -- An itype reference must only be created if this is a local itype, so
5183 -- that gigi can elaborate it on the proper objstack.
5185 if Is_Itype (Typ) and then Scope (Typ) = Current_Scope then
5186 IR := Make_Itype_Reference (Sloc (N));
5187 Set_Itype (IR, Typ);
5188 Insert_Action (N, IR);
5189 end if;
5190 end Ensure_Defined;
5192 -------------------
5193 -- Evaluate_Name --
5194 -------------------
5196 procedure Evaluate_Name (Nam : Node_Id) is
5197 begin
5198 case Nkind (Nam) is
5199 -- For an aggregate, force its evaluation
5201 when N_Aggregate =>
5202 Force_Evaluation (Nam);
5204 -- For an attribute reference or an indexed component, evaluate the
5205 -- prefix, which is itself a name, recursively, and then force the
5206 -- evaluation of all the subscripts (or attribute expressions).
5208 when N_Attribute_Reference
5209 | N_Indexed_Component
5211 Evaluate_Name (Prefix (Nam));
5213 declare
5214 E : Node_Id;
5216 begin
5217 E := First (Expressions (Nam));
5218 while Present (E) loop
5219 Force_Evaluation (E);
5221 if Is_Rewrite_Substitution (E) then
5222 Set_Do_Range_Check
5223 (E, Do_Range_Check (Original_Node (E)));
5224 end if;
5226 Next (E);
5227 end loop;
5228 end;
5230 -- For an explicit dereference, we simply force the evaluation of
5231 -- the name expression. The dereference provides a value that is the
5232 -- address for the renamed object, and it is precisely this value
5233 -- that we want to preserve.
5235 when N_Explicit_Dereference =>
5236 Force_Evaluation (Prefix (Nam));
5238 -- For a function call, we evaluate the call; same for an operator
5240 when N_Function_Call
5241 | N_Op
5243 Force_Evaluation (Nam);
5245 -- For a qualified expression, we evaluate the expression
5247 when N_Qualified_Expression =>
5248 Evaluate_Name (Expression (Nam));
5250 -- For a selected component, we simply evaluate the prefix
5252 when N_Selected_Component =>
5253 Evaluate_Name (Prefix (Nam));
5255 -- For a slice, we evaluate the prefix, as for the indexed component
5256 -- case and then, if there is a range present, either directly or as
5257 -- the constraint of a discrete subtype indication, we evaluate the
5258 -- two bounds of this range.
5260 when N_Slice =>
5261 Evaluate_Name (Prefix (Nam));
5262 Evaluate_Slice_Bounds (Nam);
5264 -- For a type conversion, the expression of the conversion must be
5265 -- the name of an object, and we simply need to evaluate this name.
5267 when N_Type_Conversion =>
5268 Evaluate_Name (Expression (Nam));
5270 -- The remaining cases are direct name and character literal. In all
5271 -- these cases, we do nothing, since we want to reevaluate each time
5272 -- the renamed object is used. ??? There are more remaining cases, at
5273 -- least in the GNATprove_Mode, where this routine is called in more
5274 -- contexts than in GNAT.
5276 when others =>
5277 null;
5278 end case;
5279 end Evaluate_Name;
5281 ---------------------------
5282 -- Evaluate_Slice_Bounds --
5283 ---------------------------
5285 procedure Evaluate_Slice_Bounds (Slice : Node_Id) is
5286 DR : constant Node_Id := Discrete_Range (Slice);
5287 Constr : Node_Id;
5288 Rexpr : Node_Id;
5290 begin
5291 if Nkind (DR) = N_Range then
5292 Force_Evaluation (Low_Bound (DR));
5293 Force_Evaluation (High_Bound (DR));
5295 elsif Nkind (DR) = N_Subtype_Indication then
5296 Constr := Constraint (DR);
5298 if Nkind (Constr) = N_Range_Constraint then
5299 Rexpr := Range_Expression (Constr);
5301 Force_Evaluation (Low_Bound (Rexpr));
5302 Force_Evaluation (High_Bound (Rexpr));
5303 end if;
5304 end if;
5305 end Evaluate_Slice_Bounds;
5307 ---------------------
5308 -- Evolve_And_Then --
5309 ---------------------
5311 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
5312 begin
5313 if No (Cond) then
5314 Cond := Cond1;
5315 else
5316 Cond :=
5317 Make_And_Then (Sloc (Cond1),
5318 Left_Opnd => Cond,
5319 Right_Opnd => Cond1);
5320 end if;
5321 end Evolve_And_Then;
5323 --------------------
5324 -- Evolve_Or_Else --
5325 --------------------
5327 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
5328 begin
5329 if No (Cond) then
5330 Cond := Cond1;
5331 else
5332 Cond :=
5333 Make_Or_Else (Sloc (Cond1),
5334 Left_Opnd => Cond,
5335 Right_Opnd => Cond1);
5336 end if;
5337 end Evolve_Or_Else;
5339 -------------------------------
5340 -- Expand_Sliding_Conversion --
5341 -------------------------------
5343 procedure Expand_Sliding_Conversion (N : Node_Id; Arr_Typ : Entity_Id) is
5345 pragma Assert (Is_Array_Type (Arr_Typ)
5346 and then not Is_Constrained (Arr_Typ)
5347 and then Is_Fixed_Lower_Bound_Array_Subtype (Arr_Typ));
5349 Constraints : List_Id;
5350 Index : Node_Id := First_Index (Arr_Typ);
5351 Loc : constant Source_Ptr := Sloc (N);
5352 Subt_Decl : Node_Id;
5353 Subt : Entity_Id;
5354 Subt_Low : Node_Id;
5355 Subt_High : Node_Id;
5357 Act_Subt : Entity_Id;
5358 Act_Index : Node_Id;
5359 Act_Low : Node_Id;
5360 Act_High : Node_Id;
5361 Adjust_Incr : Node_Id;
5362 Dimension : Int := 0;
5363 All_FLBs_Match : Boolean := True;
5365 begin
5366 -- This procedure is called during semantic analysis, and we only expand
5367 -- a sliding conversion when Expander_Active, to avoid doing it during
5368 -- preanalysis (which can lead to problems with the target subtype not
5369 -- getting properly expanded during later full analysis). Also, sliding
5370 -- should never be needed for string literals, because their bounds are
5371 -- determined directly based on the fixed lower bound of Arr_Typ and
5372 -- their length.
5374 if Expander_Active and then Nkind (N) /= N_String_Literal then
5375 Constraints := New_List;
5377 Act_Subt := Get_Actual_Subtype (N);
5378 Act_Index := First_Index (Act_Subt);
5380 -- Loop over the indexes of the fixed-lower-bound array type or
5381 -- subtype to build up an index constraint for constructing the
5382 -- subtype that will be the target of a conversion of the array
5383 -- object that may need a sliding conversion.
5385 while Present (Index) loop
5386 pragma Assert (Present (Act_Index));
5388 Dimension := Dimension + 1;
5390 Get_Index_Bounds (Act_Index, Act_Low, Act_High);
5392 -- If Index defines a normal unconstrained range (range <>),
5393 -- then we will simply use the bounds of the actual subtype's
5394 -- corresponding index range.
5396 if not Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index)) then
5397 Subt_Low := Act_Low;
5398 Subt_High := Act_High;
5400 -- Otherwise, a range will be created with a low bound given by
5401 -- the fixed lower bound of the array subtype's index, and with
5402 -- high bound given by (Actual'Length + fixed lower bound - 1).
5404 else
5405 if Nkind (Index) = N_Subtype_Indication then
5406 Subt_Low :=
5407 New_Copy_Tree
5408 (Low_Bound (Range_Expression (Constraint (Index))));
5409 else
5410 pragma Assert (Nkind (Index) = N_Range);
5412 Subt_Low := New_Copy_Tree (Low_Bound (Index));
5413 end if;
5415 -- If either we have a nonstatic lower bound, or the target and
5416 -- source subtypes are statically known to have unequal lower
5417 -- bounds, then we will need to make a subtype conversion to
5418 -- slide the bounds. However, if all of the indexes' lower
5419 -- bounds are static and known to be equal (the common case),
5420 -- then no conversion will be needed, and we'll end up not
5421 -- creating the subtype or the conversion (though we still
5422 -- build up the index constraint, which will simply be unused).
5424 if not (Compile_Time_Known_Value (Subt_Low)
5425 and then Compile_Time_Known_Value (Act_Low))
5426 or else Expr_Value (Subt_Low) /= Expr_Value (Act_Low)
5427 then
5428 All_FLBs_Match := False;
5429 end if;
5431 -- Apply 'Pos to lower bound, which may be of an enumeration
5432 -- type, before subtracting.
5434 Adjust_Incr :=
5435 Make_Op_Subtract (Loc,
5436 Make_Attribute_Reference (Loc,
5437 Prefix =>
5438 New_Occurrence_Of (Etype (Act_Index), Loc),
5439 Attribute_Name =>
5440 Name_Pos,
5441 Expressions =>
5442 New_List (New_Copy_Tree (Subt_Low))),
5443 Make_Integer_Literal (Loc, 1));
5445 -- Apply 'Val to the result of adding the increment to the
5446 -- length, to handle indexes of enumeration types.
5448 Subt_High :=
5449 Make_Attribute_Reference (Loc,
5450 Prefix =>
5451 New_Occurrence_Of (Etype (Act_Index), Loc),
5452 Attribute_Name =>
5453 Name_Val,
5454 Expressions =>
5455 New_List (Make_Op_Add (Loc,
5456 Make_Attribute_Reference (Loc,
5457 Prefix =>
5458 New_Occurrence_Of (Act_Subt, Loc),
5459 Attribute_Name =>
5460 Name_Length,
5461 Expressions =>
5462 New_List
5463 (Make_Integer_Literal
5464 (Loc, Dimension))),
5465 Adjust_Incr)));
5466 end if;
5468 Append (Make_Range (Loc, Subt_Low, Subt_High), Constraints);
5470 Next (Index);
5471 Next (Act_Index);
5472 end loop;
5474 -- If for each index with a fixed lower bound (FLB), the lower bound
5475 -- of the corresponding index of the actual subtype is statically
5476 -- known be equal to the FLB, then a sliding conversion isn't needed
5477 -- at all, so just return without building a subtype or conversion.
5479 if All_FLBs_Match then
5480 return;
5481 end if;
5483 -- A sliding conversion is needed, so create the target subtype using
5484 -- the index constraint created above, and rewrite the expression
5485 -- as a conversion to that subtype.
5487 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
5488 Set_Is_Internal (Subt);
5490 Subt_Decl :=
5491 Make_Subtype_Declaration (Loc,
5492 Defining_Identifier => Subt,
5493 Subtype_Indication =>
5494 Make_Subtype_Indication (Loc,
5495 Subtype_Mark =>
5496 New_Occurrence_Of (Arr_Typ, Loc),
5497 Constraint =>
5498 Make_Index_Or_Discriminant_Constraint (Loc,
5499 Constraints => Constraints)));
5501 Mark_Rewrite_Insertion (Subt_Decl);
5503 -- The actual subtype is an Itype, so we analyze the declaration,
5504 -- but do not attach it to the tree.
5506 Set_Parent (Subt_Decl, N);
5507 Set_Is_Itype (Subt);
5508 Analyze (Subt_Decl, Suppress => All_Checks);
5509 Set_Associated_Node_For_Itype (Subt, N);
5510 Set_Has_Delayed_Freeze (Subt, False);
5512 -- We need to freeze the actual subtype immediately. This is needed
5513 -- because otherwise this Itype will not get frozen at all, and it is
5514 -- always safe to freeze on creation because any associated types
5515 -- must be frozen at this point.
5517 Freeze_Itype (Subt, N);
5519 Rewrite (N,
5520 Make_Type_Conversion (Loc,
5521 Subtype_Mark =>
5522 New_Occurrence_Of (Subt, Loc),
5523 Expression => Relocate_Node (N)));
5524 Analyze (N);
5525 end if;
5526 end Expand_Sliding_Conversion;
5528 -----------------------------------------
5529 -- Expand_Static_Predicates_In_Choices --
5530 -----------------------------------------
5532 procedure Expand_Static_Predicates_In_Choices (N : Node_Id) is
5533 pragma Assert (Nkind (N) in N_Case_Statement_Alternative | N_Variant);
5535 Choices : List_Id := Discrete_Choices (N);
5537 Choice : Node_Id;
5538 Next_C : Node_Id;
5539 P : Node_Id;
5540 C : Node_Id;
5542 begin
5543 -- If this is an "others" alternative, we need to process any static
5544 -- predicates in its Others_Discrete_Choices.
5546 if Nkind (First (Choices)) = N_Others_Choice then
5547 Choices := Others_Discrete_Choices (First (Choices));
5548 end if;
5550 Choice := First (Choices);
5551 while Present (Choice) loop
5552 Next_C := Next (Choice);
5554 -- Check for name of subtype with static predicate
5556 if Is_Entity_Name (Choice)
5557 and then Is_Type (Entity (Choice))
5558 and then Has_Predicates (Entity (Choice))
5559 then
5560 -- Loop through entries in predicate list, converting to choices
5561 -- and inserting in the list before the current choice. Note that
5562 -- if the list is empty, corresponding to a False predicate, then
5563 -- no choices are inserted.
5565 P := First (Static_Discrete_Predicate (Entity (Choice)));
5566 while Present (P) loop
5568 -- If low bound and high bounds are equal, copy simple choice
5570 if Expr_Value (Low_Bound (P)) = Expr_Value (High_Bound (P)) then
5571 C := New_Copy (Low_Bound (P));
5573 -- Otherwise copy a range
5575 else
5576 C := New_Copy (P);
5577 end if;
5579 -- Change Sloc to referencing choice (rather than the Sloc of
5580 -- the predicate declaration element itself).
5582 Set_Sloc (C, Sloc (Choice));
5583 Insert_Before (Choice, C);
5584 Next (P);
5585 end loop;
5587 -- Delete the predicated entry
5589 Remove (Choice);
5590 end if;
5592 -- Move to next choice to check
5594 Choice := Next_C;
5595 end loop;
5597 Set_Has_SP_Choice (N, False);
5598 end Expand_Static_Predicates_In_Choices;
5600 ------------------------------
5601 -- Expand_Subtype_From_Expr --
5602 ------------------------------
5604 -- This function is applicable for both static and dynamic allocation of
5605 -- objects which are constrained by an initial expression. Basically it
5606 -- transforms an unconstrained subtype indication into a constrained one.
5608 -- The expression may also be transformed in certain cases in order to
5609 -- avoid multiple evaluation. In the static allocation case, the general
5610 -- scheme is:
5612 -- Val : T := Expr;
5614 -- is transformed into
5616 -- Val : Constrained_Subtype_Of_T := Maybe_Modified_Expr;
5618 -- Here are the main cases :
5620 -- <if Expr is a Slice>
5621 -- Val : T ([Index_Subtype (Expr)]) := Expr;
5623 -- <elsif Expr is a String Literal>
5624 -- Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
5626 -- <elsif Expr is Constrained>
5627 -- subtype T is Type_Of_Expr
5628 -- Val : T := Expr;
5630 -- <elsif Expr is an entity_name>
5631 -- Val : T (constraints taken from Expr) := Expr;
5633 -- <else>
5634 -- type Axxx is access all T;
5635 -- Rval : Axxx := Expr'ref;
5636 -- Val : T (constraints taken from Rval) := Rval.all;
5638 -- ??? note: when the Expression is allocated in the secondary stack
5639 -- we could use it directly instead of copying it by declaring
5640 -- Val : T (...) renames Rval.all
5642 procedure Expand_Subtype_From_Expr
5643 (N : Node_Id;
5644 Unc_Type : Entity_Id;
5645 Subtype_Indic : Node_Id;
5646 Exp : Node_Id;
5647 Related_Id : Entity_Id := Empty)
5649 Loc : constant Source_Ptr := Sloc (N);
5650 Exp_Typ : constant Entity_Id := Etype (Exp);
5651 T : Entity_Id;
5653 begin
5654 -- In general we cannot build the subtype if expansion is disabled,
5655 -- because internal entities may not have been defined. However, to
5656 -- avoid some cascaded errors, we try to continue when the expression is
5657 -- an array (or string), because it is safe to compute the bounds. It is
5658 -- in fact required to do so even in a generic context, because there
5659 -- may be constants that depend on the bounds of a string literal, both
5660 -- standard string types and more generally arrays of characters.
5662 -- In GNATprove mode, these extra subtypes are not needed, unless Exp is
5663 -- a static expression. In that case, the subtype will be constrained
5664 -- while the original type might be unconstrained, so expanding the type
5665 -- is necessary both for passing legality checks in GNAT and for precise
5666 -- analysis in GNATprove.
5668 if GNATprove_Mode and then not Is_Static_Expression (Exp) then
5669 return;
5670 end if;
5672 if not Expander_Active
5673 and then (No (Etype (Exp)) or else not Is_String_Type (Etype (Exp)))
5674 then
5675 return;
5676 end if;
5678 if Nkind (Exp) = N_Slice then
5679 declare
5680 Slice_Type : constant Entity_Id := Etype (First_Index (Exp_Typ));
5682 begin
5683 Rewrite (Subtype_Indic,
5684 Make_Subtype_Indication (Loc,
5685 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5686 Constraint =>
5687 Make_Index_Or_Discriminant_Constraint (Loc,
5688 Constraints => New_List
5689 (New_Occurrence_Of (Slice_Type, Loc)))));
5691 -- This subtype indication may be used later for constraint checks
5692 -- we better make sure that if a variable was used as a bound of
5693 -- the original slice, its value is frozen.
5695 Evaluate_Slice_Bounds (Exp);
5696 end;
5698 elsif Ekind (Exp_Typ) = E_String_Literal_Subtype then
5699 Rewrite (Subtype_Indic,
5700 Make_Subtype_Indication (Loc,
5701 Subtype_Mark => New_Occurrence_Of (Unc_Type, Loc),
5702 Constraint =>
5703 Make_Index_Or_Discriminant_Constraint (Loc,
5704 Constraints => New_List (
5705 Make_Literal_Range (Loc,
5706 Literal_Typ => Exp_Typ)))));
5708 -- If the type of the expression is an internally generated type it
5709 -- may not be necessary to create a new subtype. However there are two
5710 -- exceptions: references to the current instances, and aliased array
5711 -- object declarations for which the back end has to create a template.
5713 elsif Is_Constrained (Exp_Typ)
5714 and then not Is_Class_Wide_Type (Unc_Type)
5715 and then
5716 (Nkind (N) /= N_Object_Declaration
5717 or else not Is_Entity_Name (Expression (N))
5718 or else not Comes_From_Source (Entity (Expression (N)))
5719 or else not Is_Array_Type (Exp_Typ)
5720 or else not Aliased_Present (N))
5721 then
5722 if Is_Itype (Exp_Typ)
5724 -- When this is for an object declaration, the caller may want to
5725 -- set Is_Constr_Subt_For_U_Nominal on the subtype, so we must make
5726 -- sure that either the subtype has been built for the expression,
5727 -- typically for an aggregate, or the flag is already set on it;
5728 -- otherwise it could end up being set on the nominal constrained
5729 -- subtype of an object and thus later cause the failure to detect
5730 -- non-statically-matching subtypes on 'Access of this object.
5732 and then (Nkind (N) /= N_Object_Declaration
5733 or else Nkind (Original_Node (Exp)) = N_Aggregate
5734 or else Is_Constr_Subt_For_U_Nominal (Exp_Typ))
5735 then
5736 -- Within an initialization procedure, a selected component
5737 -- denotes a component of the enclosing record, and it appears as
5738 -- an actual in a call to its own initialization procedure. If
5739 -- this component depends on the outer discriminant, we must
5740 -- generate the proper actual subtype for it.
5742 if Nkind (Exp) = N_Selected_Component
5743 and then Within_Init_Proc
5744 then
5745 declare
5746 Decl : constant Node_Id :=
5747 Build_Actual_Subtype_Of_Component (Exp_Typ, Exp);
5748 begin
5749 if Present (Decl) then
5750 Insert_Action (N, Decl);
5751 T := Defining_Identifier (Decl);
5752 else
5753 T := Exp_Typ;
5754 end if;
5755 end;
5757 -- No need to generate a new subtype
5759 else
5760 T := Exp_Typ;
5761 end if;
5763 else
5764 T := Make_Temporary (Loc, 'T');
5766 Insert_Action (N,
5767 Make_Subtype_Declaration (Loc,
5768 Defining_Identifier => T,
5769 Subtype_Indication => New_Occurrence_Of (Exp_Typ, Loc)));
5771 -- This type is marked as an itype even though it has an explicit
5772 -- declaration since otherwise Is_Generic_Actual_Type can get
5773 -- set, resulting in the generation of spurious errors. (See
5774 -- sem_ch8.Analyze_Package_Renaming and Sem_Type.Covers.)
5776 Set_Is_Itype (T);
5777 Set_Associated_Node_For_Itype (T, Exp);
5778 end if;
5780 Rewrite (Subtype_Indic, New_Occurrence_Of (T, Loc));
5782 -- Nothing needs to be done for private types with unknown discriminants
5783 -- if the underlying type is not an unconstrained composite type or it
5784 -- is an unchecked union.
5786 elsif Is_Private_Type (Unc_Type)
5787 and then Has_Unknown_Discriminants (Unc_Type)
5788 and then (not Is_Composite_Type (Underlying_Type (Unc_Type))
5789 or else Is_Constrained (Underlying_Type (Unc_Type))
5790 or else Is_Unchecked_Union (Underlying_Type (Unc_Type)))
5791 then
5792 null;
5794 -- Case of derived type with unknown discriminants where the parent type
5795 -- also has unknown discriminants.
5797 elsif Is_Record_Type (Unc_Type)
5798 and then not Is_Class_Wide_Type (Unc_Type)
5799 and then Has_Unknown_Discriminants (Unc_Type)
5800 and then Has_Unknown_Discriminants (Underlying_Type (Unc_Type))
5801 then
5802 -- Nothing to be done if no underlying record view available
5804 -- If this is a limited type derived from a type with unknown
5805 -- discriminants, do not expand either, so that subsequent expansion
5806 -- of the call can add build-in-place parameters to call.
5808 if No (Underlying_Record_View (Unc_Type))
5809 or else Is_Limited_Type (Unc_Type)
5810 then
5811 null;
5813 -- Otherwise use the Underlying_Record_View to create the proper
5814 -- constrained subtype for an object of a derived type with unknown
5815 -- discriminants.
5817 else
5818 Rewrite (Subtype_Indic,
5819 Make_Subtype_From_Expr (Exp, Underlying_Record_View (Unc_Type)));
5820 end if;
5822 -- Renamings of class-wide interface types require no equivalent
5823 -- constrained type declarations because we only need to reference
5824 -- the tag component associated with the interface. The same is
5825 -- presumably true for class-wide types in general, so this test
5826 -- is broadened to include all class-wide renamings, which also
5827 -- avoids cases of unbounded recursion in Remove_Side_Effects.
5828 -- (Is this really correct, or are there some cases of class-wide
5829 -- renamings that require action in this procedure???)
5831 elsif Present (N)
5832 and then Nkind (N) = N_Object_Renaming_Declaration
5833 and then Is_Class_Wide_Type (Unc_Type)
5834 then
5835 null;
5837 -- In Ada 95 nothing to be done if the type of the expression is limited
5838 -- because in this case the expression cannot be copied, and its use can
5839 -- only be by reference.
5841 -- In Ada 2005 the context can be an object declaration whose expression
5842 -- is a function that returns in place. If the nominal subtype has
5843 -- unknown discriminants, the call still provides constraints on the
5844 -- object, and we have to create an actual subtype from it.
5846 -- If the type is class-wide, the expression is dynamically tagged and
5847 -- we do not create an actual subtype either. Ditto for an interface.
5848 -- For now this applies only if the type is immutably limited, and the
5849 -- function being called is build-in-place. This will have to be revised
5850 -- when build-in-place functions are generalized to other types.
5852 elsif Is_Limited_View (Exp_Typ)
5853 and then
5854 (Is_Class_Wide_Type (Exp_Typ)
5855 or else Is_Interface (Exp_Typ)
5856 or else not Has_Unknown_Discriminants (Exp_Typ)
5857 or else not Is_Composite_Type (Unc_Type))
5858 then
5859 null;
5861 -- For limited objects initialized with build-in-place function calls,
5862 -- nothing to be done; otherwise we prematurely introduce an N_Reference
5863 -- node in the expression initializing the object, which breaks the
5864 -- circuitry that detects and adds the additional arguments to the
5865 -- called function.
5867 elsif Is_Build_In_Place_Function_Call (Exp) then
5868 null;
5870 -- If the expression is an uninitialized aggregate, no need to build
5871 -- a subtype from the expression, because this may require the use of
5872 -- dynamic memory to create the object.
5874 elsif Is_Uninitialized_Aggregate (Exp, Exp_Typ) then
5875 Rewrite (Subtype_Indic, New_Occurrence_Of (Etype (Exp), Sloc (N)));
5876 if Nkind (N) = N_Object_Declaration then
5877 Set_Expression (N, Empty);
5878 Set_No_Initialization (N);
5879 end if;
5881 else
5882 Rewrite (Subtype_Indic,
5883 Make_Subtype_From_Expr (Exp, Unc_Type, Related_Id));
5884 end if;
5885 end Expand_Subtype_From_Expr;
5887 ---------------------------------------------
5888 -- Expression_Contains_Primitives_Calls_Of --
5889 ---------------------------------------------
5891 function Expression_Contains_Primitives_Calls_Of
5892 (Expr : Node_Id;
5893 Typ : Entity_Id) return Boolean
5895 U_Typ : constant Entity_Id := Unique_Entity (Typ);
5897 Calls_OK : Boolean := False;
5898 -- This flag is set to True when expression Expr contains at least one
5899 -- call to a nondispatching primitive function of Typ.
5901 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result;
5902 -- Search for nondispatching calls to primitive functions of type Typ
5904 ----------------------------
5905 -- Search_Primitive_Calls --
5906 ----------------------------
5908 function Search_Primitive_Calls (N : Node_Id) return Traverse_Result is
5909 Disp_Typ : Entity_Id;
5910 Subp : Entity_Id;
5912 begin
5913 -- Detect a function call that could denote a nondispatching
5914 -- primitive of the input type.
5916 if Nkind (N) = N_Function_Call
5917 and then Is_Entity_Name (Name (N))
5918 then
5919 Subp := Entity (Name (N));
5921 -- Do not consider function calls with a controlling argument, as
5922 -- those are always dispatching calls.
5924 if Is_Dispatching_Operation (Subp)
5925 and then No (Controlling_Argument (N))
5926 then
5927 Disp_Typ := Find_Dispatching_Type (Subp);
5929 -- To qualify as a suitable primitive, the dispatching type of
5930 -- the function must be the input type.
5932 if Present (Disp_Typ)
5933 and then Unique_Entity (Disp_Typ) = U_Typ
5934 then
5935 Calls_OK := True;
5937 -- There is no need to continue the traversal, as one such
5938 -- call suffices.
5940 return Abandon;
5941 end if;
5942 end if;
5943 end if;
5945 return OK;
5946 end Search_Primitive_Calls;
5948 procedure Search_Calls is new Traverse_Proc (Search_Primitive_Calls);
5950 -- Start of processing for Expression_Contains_Primitives_Calls_Of_Type
5952 begin
5953 Search_Calls (Expr);
5954 return Calls_OK;
5955 end Expression_Contains_Primitives_Calls_Of;
5957 ----------------------
5958 -- Finalize_Address --
5959 ----------------------
5961 function Finalize_Address (Typ : Entity_Id) return Entity_Id is
5962 Btyp : constant Entity_Id := Base_Type (Typ);
5963 Utyp : Entity_Id := Typ;
5965 begin
5966 -- Handle protected class-wide or task class-wide types
5968 if Is_Class_Wide_Type (Utyp) then
5969 if Is_Concurrent_Type (Root_Type (Utyp)) then
5970 Utyp := Root_Type (Utyp);
5972 elsif Is_Private_Type (Root_Type (Utyp))
5973 and then Present (Full_View (Root_Type (Utyp)))
5974 and then Is_Concurrent_Type (Full_View (Root_Type (Utyp)))
5975 then
5976 Utyp := Full_View (Root_Type (Utyp));
5977 end if;
5978 end if;
5980 -- Handle private types
5982 if Is_Private_Type (Utyp) and then Present (Full_View (Utyp)) then
5983 Utyp := Full_View (Utyp);
5984 end if;
5986 -- Handle protected and task types
5988 if Is_Concurrent_Type (Utyp)
5989 and then Present (Corresponding_Record_Type (Utyp))
5990 then
5991 Utyp := Corresponding_Record_Type (Utyp);
5992 end if;
5994 Utyp := Underlying_Type (Base_Type (Utyp));
5996 -- Deal with untagged derivation of private views. If the parent is
5997 -- now known to be protected, the finalization routine is the one
5998 -- defined on the corresponding record of the ancestor (corresponding
5999 -- records do not automatically inherit operations, but maybe they
6000 -- should???)
6002 if Is_Untagged_Derivation (Btyp) then
6003 if Is_Protected_Type (Btyp) then
6004 Utyp := Corresponding_Record_Type (Root_Type (Btyp));
6006 else
6007 Utyp := Underlying_Type (Root_Type (Btyp));
6009 if Is_Protected_Type (Utyp) then
6010 Utyp := Corresponding_Record_Type (Utyp);
6011 end if;
6012 end if;
6013 end if;
6015 -- If the underlying_type is a subtype, we are dealing with the
6016 -- completion of a private type. We need to access the base type and
6017 -- generate a conversion to it.
6019 if Utyp /= Base_Type (Utyp) then
6020 pragma Assert (Is_Private_Type (Typ));
6022 Utyp := Base_Type (Utyp);
6023 end if;
6025 -- When dealing with an internally built full view for a type with
6026 -- unknown discriminants, use the original record type.
6028 if Is_Underlying_Record_View (Utyp) then
6029 Utyp := Etype (Utyp);
6030 end if;
6032 return TSS (Utyp, TSS_Finalize_Address);
6033 end Finalize_Address;
6035 ------------------------
6036 -- Find_Interface_ADT --
6037 ------------------------
6039 function Find_Interface_ADT
6040 (T : Entity_Id;
6041 Iface : Entity_Id) return Elmt_Id
6043 ADT : Elmt_Id;
6044 Typ : Entity_Id := T;
6046 begin
6047 pragma Assert (Is_Interface (Iface));
6049 -- Handle private types
6051 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6052 Typ := Full_View (Typ);
6053 end if;
6055 -- Handle access types
6057 if Is_Access_Type (Typ) then
6058 Typ := Designated_Type (Typ);
6059 end if;
6061 -- Handle task and protected types implementing interfaces
6063 if Is_Concurrent_Type (Typ) then
6064 Typ := Corresponding_Record_Type (Typ);
6065 end if;
6067 pragma Assert
6068 (not Is_Class_Wide_Type (Typ)
6069 and then Ekind (Typ) /= E_Incomplete_Type);
6071 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6072 return First_Elmt (Access_Disp_Table (Typ));
6074 else
6075 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Typ))));
6076 while Present (ADT)
6077 and then Present (Related_Type (Node (ADT)))
6078 and then Related_Type (Node (ADT)) /= Iface
6079 and then not Is_Ancestor (Iface, Related_Type (Node (ADT)),
6080 Use_Full_View => True)
6081 loop
6082 Next_Elmt (ADT);
6083 end loop;
6085 pragma Assert (Present (Related_Type (Node (ADT))));
6086 return ADT;
6087 end if;
6088 end Find_Interface_ADT;
6090 ------------------------
6091 -- Find_Interface_Tag --
6092 ------------------------
6094 function Find_Interface_Tag
6095 (T : Entity_Id;
6096 Iface : Entity_Id) return Entity_Id
6098 AI_Tag : Entity_Id := Empty;
6099 Found : Boolean := False;
6100 Typ : Entity_Id := T;
6102 procedure Find_Tag (Typ : Entity_Id);
6103 -- Internal subprogram used to recursively climb to the ancestors
6105 --------------
6106 -- Find_Tag --
6107 --------------
6109 procedure Find_Tag (Typ : Entity_Id) is
6110 AI_Elmt : Elmt_Id;
6111 AI : Node_Id;
6113 begin
6114 -- This routine does not handle the case in which the interface is an
6115 -- ancestor of Typ. That case is handled by the enclosing subprogram.
6117 pragma Assert (Typ /= Iface);
6119 -- Climb to the root type handling private types
6121 if Present (Full_View (Etype (Typ))) then
6122 if Full_View (Etype (Typ)) /= Typ then
6123 Find_Tag (Full_View (Etype (Typ)));
6124 end if;
6126 elsif Etype (Typ) /= Typ then
6127 Find_Tag (Etype (Typ));
6128 end if;
6130 -- Traverse the list of interfaces implemented by the type
6132 if not Found
6133 and then Present (Interfaces (Typ))
6134 and then not (Is_Empty_Elmt_List (Interfaces (Typ)))
6135 then
6136 -- Skip the tag associated with the primary table
6138 AI_Tag := Next_Tag_Component (First_Tag_Component (Typ));
6139 pragma Assert (Present (AI_Tag));
6141 AI_Elmt := First_Elmt (Interfaces (Typ));
6142 while Present (AI_Elmt) loop
6143 AI := Node (AI_Elmt);
6145 if AI = Iface
6146 or else Is_Ancestor (Iface, AI, Use_Full_View => True)
6147 then
6148 Found := True;
6149 return;
6150 end if;
6152 AI_Tag := Next_Tag_Component (AI_Tag);
6153 Next_Elmt (AI_Elmt);
6154 end loop;
6155 end if;
6156 end Find_Tag;
6158 -- Start of processing for Find_Interface_Tag
6160 begin
6161 pragma Assert (Is_Interface (Iface));
6163 -- Handle access types
6165 if Is_Access_Type (Typ) then
6166 Typ := Designated_Type (Typ);
6167 end if;
6169 -- Handle class-wide types
6171 if Is_Class_Wide_Type (Typ) then
6172 Typ := Root_Type (Typ);
6173 end if;
6175 -- Handle private types
6177 if Has_Private_Declaration (Typ) and then Present (Full_View (Typ)) then
6178 Typ := Full_View (Typ);
6179 end if;
6181 -- Handle entities from the limited view
6183 if Ekind (Typ) = E_Incomplete_Type then
6184 pragma Assert (Present (Non_Limited_View (Typ)));
6185 Typ := Non_Limited_View (Typ);
6186 end if;
6188 -- Handle task and protected types implementing interfaces
6190 if Is_Concurrent_Type (Typ) then
6191 Typ := Corresponding_Record_Type (Typ);
6192 end if;
6194 -- If the interface is an ancestor of the type, then it shared the
6195 -- primary dispatch table.
6197 if Is_Ancestor (Iface, Typ, Use_Full_View => True) then
6198 return First_Tag_Component (Typ);
6200 -- Otherwise we need to search for its associated tag component
6202 else
6203 Find_Tag (Typ);
6204 return AI_Tag;
6205 end if;
6206 end Find_Interface_Tag;
6208 ---------------------------
6209 -- Find_Optional_Prim_Op --
6210 ---------------------------
6212 function Find_Optional_Prim_Op
6213 (T : Entity_Id; Name : Name_Id) return Entity_Id
6215 Prim : Elmt_Id;
6216 Typ : Entity_Id := T;
6217 Op : Entity_Id;
6219 begin
6220 if Is_Class_Wide_Type (Typ) then
6221 Typ := Root_Type (Typ);
6222 end if;
6224 Typ := Underlying_Type (Typ);
6226 -- Loop through primitive operations
6228 Prim := First_Elmt (Primitive_Operations (Typ));
6229 while Present (Prim) loop
6230 Op := Node (Prim);
6232 -- We can retrieve primitive operations by name if it is an internal
6233 -- name. For equality we must check that both of its operands have
6234 -- the same type, to avoid confusion with user-defined equalities
6235 -- than may have a asymmetric signature.
6237 exit when Chars (Op) = Name
6238 and then
6239 (Name /= Name_Op_Eq
6240 or else Etype (First_Formal (Op)) = Etype (Last_Formal (Op)));
6242 Next_Elmt (Prim);
6243 end loop;
6245 return Node (Prim); -- Empty if not found
6246 end Find_Optional_Prim_Op;
6248 ---------------------------
6249 -- Find_Optional_Prim_Op --
6250 ---------------------------
6252 function Find_Optional_Prim_Op
6253 (T : Entity_Id;
6254 Name : TSS_Name_Type) return Entity_Id
6256 Inher_Op : Entity_Id := Empty;
6257 Own_Op : Entity_Id := Empty;
6258 Prim_Elmt : Elmt_Id;
6259 Prim_Id : Entity_Id;
6260 Typ : Entity_Id := T;
6262 begin
6263 if Is_Class_Wide_Type (Typ) then
6264 Typ := Root_Type (Typ);
6265 end if;
6267 Typ := Underlying_Type (Typ);
6269 -- This search is based on the assertion that the dispatching version
6270 -- of the TSS routine always precedes the real primitive.
6272 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
6273 while Present (Prim_Elmt) loop
6274 Prim_Id := Node (Prim_Elmt);
6276 if Is_TSS (Prim_Id, Name) then
6277 if Present (Alias (Prim_Id)) then
6278 Inher_Op := Prim_Id;
6279 else
6280 Own_Op := Prim_Id;
6281 end if;
6282 end if;
6284 Next_Elmt (Prim_Elmt);
6285 end loop;
6287 if Present (Own_Op) then
6288 return Own_Op;
6289 elsif Present (Inher_Op) then
6290 return Inher_Op;
6291 else
6292 return Empty;
6293 end if;
6294 end Find_Optional_Prim_Op;
6296 ------------------
6297 -- Find_Prim_Op --
6298 ------------------
6300 function Find_Prim_Op
6301 (T : Entity_Id; Name : Name_Id) return Entity_Id
6303 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6304 begin
6305 if No (Result) then
6306 raise Program_Error;
6307 end if;
6309 return Result;
6310 end Find_Prim_Op;
6312 ------------------
6313 -- Find_Prim_Op --
6314 ------------------
6316 function Find_Prim_Op
6317 (T : Entity_Id;
6318 Name : TSS_Name_Type) return Entity_Id
6320 Result : constant Entity_Id := Find_Optional_Prim_Op (T, Name);
6321 begin
6322 if No (Result) then
6323 raise Program_Error;
6324 end if;
6326 return Result;
6327 end Find_Prim_Op;
6329 ----------------------------
6330 -- Find_Protection_Object --
6331 ----------------------------
6333 function Find_Protection_Object (Scop : Entity_Id) return Entity_Id is
6334 S : Entity_Id;
6336 begin
6337 S := Scop;
6338 while Present (S) loop
6339 if Ekind (S) in E_Entry | E_Entry_Family | E_Function | E_Procedure
6340 and then Present (Protection_Object (S))
6341 then
6342 return Protection_Object (S);
6343 end if;
6345 S := Scope (S);
6346 end loop;
6348 -- If we do not find a Protection object in the scope chain, then
6349 -- something has gone wrong, most likely the object was never created.
6351 raise Program_Error;
6352 end Find_Protection_Object;
6354 --------------------------
6355 -- Find_Protection_Type --
6356 --------------------------
6358 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
6359 Comp : Entity_Id;
6360 Typ : Entity_Id := Conc_Typ;
6362 begin
6363 if Is_Concurrent_Type (Typ) then
6364 Typ := Corresponding_Record_Type (Typ);
6365 end if;
6367 -- Since restriction violations are not considered serious errors, the
6368 -- expander remains active, but may leave the corresponding record type
6369 -- malformed. In such cases, component _object is not available so do
6370 -- not look for it.
6372 if not Analyzed (Typ) then
6373 return Empty;
6374 end if;
6376 Comp := First_Component (Typ);
6377 while Present (Comp) loop
6378 if Chars (Comp) = Name_uObject then
6379 return Base_Type (Etype (Comp));
6380 end if;
6382 Next_Component (Comp);
6383 end loop;
6385 -- The corresponding record of a protected type should always have an
6386 -- _object field.
6388 raise Program_Error;
6389 end Find_Protection_Type;
6391 function Find_Storage_Op
6392 (Typ : Entity_Id;
6393 Nam : Name_Id) return Entity_Id
6395 use Sem_Util.Storage_Model_Support;
6397 begin
6398 if Has_Storage_Model_Type_Aspect (Typ) then
6399 return Get_Storage_Model_Type_Entity (Typ, Nam);
6401 -- Otherwise we assume that Typ is a descendant of Root_Storage_Pool
6403 else
6404 return Find_Prim_Op (Typ, Nam);
6405 end if;
6406 end Find_Storage_Op;
6408 -----------------------
6409 -- Find_Hook_Context --
6410 -----------------------
6412 function Find_Hook_Context (N : Node_Id) return Node_Id is
6413 Par : Node_Id;
6414 Top : Node_Id;
6416 Wrapped_Node : Node_Id;
6417 -- Note: if we are in a transient scope, we want to reuse it as
6418 -- the context for actions insertion, if possible. But if N is itself
6419 -- part of the stored actions for the current transient scope,
6420 -- then we need to insert at the appropriate (inner) location in
6421 -- the not as an action on Node_To_Be_Wrapped.
6423 In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N);
6425 begin
6426 -- When the node is inside a case/if expression, the lifetime of any
6427 -- temporary controlled object is extended. Find a suitable insertion
6428 -- node by locating the topmost case or if expressions.
6430 if In_Cond_Expr then
6431 Par := N;
6432 Top := N;
6433 while Present (Par) loop
6434 if Nkind (Original_Node (Par)) in
6435 N_Case_Expression | N_If_Expression
6436 then
6437 Top := Par;
6439 -- Prevent the search from going too far
6441 elsif Is_Body_Or_Package_Declaration (Par) then
6442 exit;
6443 end if;
6445 Par := Parent (Par);
6446 end loop;
6448 -- The topmost case or if expression is now recovered, but it may
6449 -- still not be the correct place to add generated code. Climb to
6450 -- find a parent that is part of a declarative or statement list,
6451 -- and is not a list of actuals in a call.
6453 Par := Top;
6454 while Present (Par) loop
6455 if Is_List_Member (Par)
6456 and then Nkind (Par) not in N_Component_Association
6457 | N_Discriminant_Association
6458 | N_Parameter_Association
6459 | N_Pragma_Argument_Association
6460 | N_Aggregate
6461 | N_Delta_Aggregate
6462 | N_Extension_Aggregate
6463 and then Nkind (Parent (Par)) not in N_Function_Call
6464 | N_Procedure_Call_Statement
6465 | N_Entry_Call_Statement
6467 then
6468 return Par;
6470 -- Prevent the search from going too far
6472 elsif Is_Body_Or_Package_Declaration (Par) then
6473 exit;
6474 end if;
6476 Par := Parent (Par);
6477 end loop;
6479 return Par;
6481 else
6482 Par := N;
6483 while Present (Par) loop
6485 -- Keep climbing past various operators
6487 if Nkind (Parent (Par)) in N_Op
6488 or else Nkind (Parent (Par)) in N_And_Then | N_Or_Else
6489 then
6490 Par := Parent (Par);
6491 else
6492 exit;
6493 end if;
6494 end loop;
6496 Top := Par;
6498 -- The node may be located in a pragma in which case return the
6499 -- pragma itself:
6501 -- pragma Precondition (... and then Ctrl_Func_Call ...);
6503 -- Similar case occurs when the node is related to an object
6504 -- declaration or assignment:
6506 -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...;
6508 -- Another case to consider is when the node is part of a return
6509 -- statement:
6511 -- return ... and then Ctrl_Func_Call ...;
6513 -- Another case is when the node acts as a formal in a procedure
6514 -- call statement:
6516 -- Proc (... and then Ctrl_Func_Call ...);
6518 if Scope_Is_Transient then
6519 Wrapped_Node := Node_To_Be_Wrapped;
6520 else
6521 Wrapped_Node := Empty;
6522 end if;
6524 while Present (Par) loop
6525 if Par = Wrapped_Node
6526 or else Nkind (Par) in N_Assignment_Statement
6527 | N_Object_Declaration
6528 | N_Pragma
6529 | N_Procedure_Call_Statement
6530 | N_Simple_Return_Statement
6531 then
6532 return Par;
6534 -- Prevent the search from going too far
6536 elsif Is_Body_Or_Package_Declaration (Par) then
6537 exit;
6538 end if;
6540 Par := Parent (Par);
6541 end loop;
6543 -- Return the topmost short circuit operator
6545 return Top;
6546 end if;
6547 end Find_Hook_Context;
6549 ------------------------------
6550 -- Following_Address_Clause --
6551 ------------------------------
6553 function Following_Address_Clause (D : Node_Id) return Node_Id is
6554 Id : constant Entity_Id := Defining_Identifier (D);
6555 Result : Node_Id;
6556 Par : Node_Id;
6558 function Check_Decls (D : Node_Id) return Node_Id;
6559 -- This internal function differs from the main function in that it
6560 -- gets called to deal with a following package private part, and
6561 -- it checks declarations starting with D (the main function checks
6562 -- declarations following D). If D is Empty, then Empty is returned.
6564 -----------------
6565 -- Check_Decls --
6566 -----------------
6568 function Check_Decls (D : Node_Id) return Node_Id is
6569 Decl : Node_Id;
6571 begin
6572 Decl := D;
6573 while Present (Decl) loop
6574 if Nkind (Decl) = N_At_Clause
6575 and then Chars (Identifier (Decl)) = Chars (Id)
6576 then
6577 return Decl;
6579 elsif Nkind (Decl) = N_Attribute_Definition_Clause
6580 and then Chars (Decl) = Name_Address
6581 and then Chars (Name (Decl)) = Chars (Id)
6582 then
6583 return Decl;
6584 end if;
6586 Next (Decl);
6587 end loop;
6589 -- Otherwise not found, return Empty
6591 return Empty;
6592 end Check_Decls;
6594 -- Start of processing for Following_Address_Clause
6596 begin
6597 -- If parser detected no address clause for the identifier in question,
6598 -- then the answer is a quick NO, without the need for a search.
6600 if not Get_Name_Table_Boolean1 (Chars (Id)) then
6601 return Empty;
6602 end if;
6604 -- Otherwise search current declarative unit
6606 Result := Check_Decls (Next (D));
6608 if Present (Result) then
6609 return Result;
6610 end if;
6612 -- Check for possible package private part following
6614 Par := Parent (D);
6616 if Nkind (Par) = N_Package_Specification
6617 and then Visible_Declarations (Par) = List_Containing (D)
6618 and then Present (Private_Declarations (Par))
6619 then
6620 -- Private part present, check declarations there
6622 return Check_Decls (First (Private_Declarations (Par)));
6624 else
6625 -- No private part, clause not found, return Empty
6627 return Empty;
6628 end if;
6629 end Following_Address_Clause;
6631 ----------------------
6632 -- Force_Evaluation --
6633 ----------------------
6635 procedure Force_Evaluation
6636 (Exp : Node_Id;
6637 Name_Req : Boolean := False;
6638 Related_Id : Entity_Id := Empty;
6639 Is_Low_Bound : Boolean := False;
6640 Is_High_Bound : Boolean := False;
6641 Discr_Number : Int := 0;
6642 Mode : Force_Evaluation_Mode := Relaxed)
6644 begin
6645 Remove_Side_Effects
6646 (Exp => Exp,
6647 Name_Req => Name_Req,
6648 Variable_Ref => True,
6649 Renaming_Req => False,
6650 Related_Id => Related_Id,
6651 Is_Low_Bound => Is_Low_Bound,
6652 Is_High_Bound => Is_High_Bound,
6653 Discr_Number => Discr_Number,
6654 Check_Side_Effects =>
6655 Is_Static_Expression (Exp)
6656 or else Mode = Relaxed);
6657 end Force_Evaluation;
6659 ---------------------------------
6660 -- Fully_Qualified_Name_String --
6661 ---------------------------------
6663 function Fully_Qualified_Name_String
6664 (E : Entity_Id;
6665 Append_NUL : Boolean := True) return String_Id
6667 procedure Internal_Full_Qualified_Name (E : Entity_Id);
6668 -- Compute recursively the qualified name without NUL at the end, adding
6669 -- it to the currently started string being generated
6671 ----------------------------------
6672 -- Internal_Full_Qualified_Name --
6673 ----------------------------------
6675 procedure Internal_Full_Qualified_Name (E : Entity_Id) is
6676 Ent : Entity_Id;
6678 begin
6679 -- Deal properly with child units
6681 if Nkind (E) = N_Defining_Program_Unit_Name then
6682 Ent := Defining_Identifier (E);
6683 else
6684 Ent := E;
6685 end if;
6687 -- Compute qualification recursively (only "Standard" has no scope)
6689 if Present (Scope (Scope (Ent))) then
6690 Internal_Full_Qualified_Name (Scope (Ent));
6691 Store_String_Char (Get_Char_Code ('.'));
6692 end if;
6694 -- Every entity should have a name except some expanded blocks
6695 -- don't bother about those.
6697 if Chars (Ent) = No_Name then
6698 return;
6699 end if;
6701 -- Generates the entity name in upper case
6703 Get_Decoded_Name_String (Chars (Ent));
6704 Set_Casing (All_Upper_Case);
6705 Store_String_Chars (Name_Buffer (1 .. Name_Len));
6706 return;
6707 end Internal_Full_Qualified_Name;
6709 -- Start of processing for Full_Qualified_Name
6711 begin
6712 Start_String;
6713 Internal_Full_Qualified_Name (E);
6715 if Append_NUL then
6716 Store_String_Char (Get_Char_Code (ASCII.NUL));
6717 end if;
6719 return End_String;
6720 end Fully_Qualified_Name_String;
6722 ---------------------------------
6723 -- Get_Current_Value_Condition --
6724 ---------------------------------
6726 -- Note: the implementation of this procedure is very closely tied to the
6727 -- implementation of Set_Current_Value_Condition. In the Get procedure, we
6728 -- interpret Current_Value fields set by the Set procedure, so the two
6729 -- procedures need to be closely coordinated.
6731 procedure Get_Current_Value_Condition
6732 (Var : Node_Id;
6733 Op : out Node_Kind;
6734 Val : out Node_Id)
6736 Loc : constant Source_Ptr := Sloc (Var);
6737 Ent : constant Entity_Id := Entity (Var);
6739 procedure Process_Current_Value_Condition (N : Node_Id; S : Boolean);
6740 -- N is an expression which holds either True (S = True) or False (S =
6741 -- False) in the condition. This procedure digs out the expression and
6742 -- if it refers to Ent, sets Op and Val appropriately.
6744 -------------------------------------
6745 -- Process_Current_Value_Condition --
6746 -------------------------------------
6748 procedure Process_Current_Value_Condition
6749 (N : Node_Id;
6750 S : Boolean)
6752 Cond : Node_Id;
6753 Prev_Cond : Node_Id;
6754 Sens : Boolean;
6756 begin
6757 Cond := N;
6758 Sens := S;
6760 loop
6761 Prev_Cond := Cond;
6763 -- Deal with NOT operators, inverting sense
6765 while Nkind (Cond) = N_Op_Not loop
6766 Cond := Right_Opnd (Cond);
6767 Sens := not Sens;
6768 end loop;
6770 -- Deal with conversions, qualifications, and expressions with
6771 -- actions.
6773 while Nkind (Cond) in N_Type_Conversion
6774 | N_Qualified_Expression
6775 | N_Expression_With_Actions
6776 loop
6777 Cond := Expression (Cond);
6778 end loop;
6780 exit when Cond = Prev_Cond;
6781 end loop;
6783 -- Deal with AND THEN and AND cases
6785 if Nkind (Cond) in N_And_Then | N_Op_And then
6787 -- Don't ever try to invert a condition that is of the form of an
6788 -- AND or AND THEN (since we are not doing sufficiently general
6789 -- processing to allow this).
6791 if Sens = False then
6792 Op := N_Empty;
6793 Val := Empty;
6794 return;
6795 end if;
6797 -- Recursively process AND and AND THEN branches
6799 Process_Current_Value_Condition (Left_Opnd (Cond), True);
6800 pragma Assert (Op'Valid);
6802 if Op /= N_Empty then
6803 return;
6804 end if;
6806 Process_Current_Value_Condition (Right_Opnd (Cond), True);
6807 return;
6809 -- Case of relational operator
6811 elsif Nkind (Cond) in N_Op_Compare then
6812 Op := Nkind (Cond);
6814 -- Invert sense of test if inverted test
6816 if Sens = False then
6817 case Op is
6818 when N_Op_Eq => Op := N_Op_Ne;
6819 when N_Op_Ne => Op := N_Op_Eq;
6820 when N_Op_Lt => Op := N_Op_Ge;
6821 when N_Op_Gt => Op := N_Op_Le;
6822 when N_Op_Le => Op := N_Op_Gt;
6823 when N_Op_Ge => Op := N_Op_Lt;
6824 when others => raise Program_Error;
6825 end case;
6826 end if;
6828 -- Case of entity op value
6830 if Is_Entity_Name (Left_Opnd (Cond))
6831 and then Ent = Entity (Left_Opnd (Cond))
6832 and then Compile_Time_Known_Value (Right_Opnd (Cond))
6833 then
6834 Val := Right_Opnd (Cond);
6836 -- Case of value op entity
6838 elsif Is_Entity_Name (Right_Opnd (Cond))
6839 and then Ent = Entity (Right_Opnd (Cond))
6840 and then Compile_Time_Known_Value (Left_Opnd (Cond))
6841 then
6842 Val := Left_Opnd (Cond);
6844 -- We are effectively swapping operands
6846 case Op is
6847 when N_Op_Eq => null;
6848 when N_Op_Ne => null;
6849 when N_Op_Lt => Op := N_Op_Gt;
6850 when N_Op_Gt => Op := N_Op_Lt;
6851 when N_Op_Le => Op := N_Op_Ge;
6852 when N_Op_Ge => Op := N_Op_Le;
6853 when others => raise Program_Error;
6854 end case;
6856 else
6857 Op := N_Empty;
6858 end if;
6860 return;
6862 elsif Nkind (Cond) in N_Type_Conversion
6863 | N_Qualified_Expression
6864 | N_Expression_With_Actions
6865 then
6866 Cond := Expression (Cond);
6868 -- Case of Boolean variable reference, return as though the
6869 -- reference had said var = True.
6871 else
6872 if Is_Entity_Name (Cond) and then Ent = Entity (Cond) then
6873 Val := New_Occurrence_Of (Standard_True, Sloc (Cond));
6875 if Sens = False then
6876 Op := N_Op_Ne;
6877 else
6878 Op := N_Op_Eq;
6879 end if;
6880 end if;
6881 end if;
6882 end Process_Current_Value_Condition;
6884 -- Start of processing for Get_Current_Value_Condition
6886 begin
6887 Op := N_Empty;
6888 Val := Empty;
6890 -- Immediate return, nothing doing, if this is not an object
6892 if not Is_Object (Ent) then
6893 return;
6894 end if;
6896 -- In GNATprove mode we don't want to use current value optimizer, in
6897 -- particular for loop invariant expressions and other assertions that
6898 -- act as cut points for proof. The optimizer often folds expressions
6899 -- into True/False where they trivially follow from the previous
6900 -- assignments, but this deprives proof from the information needed to
6901 -- discharge checks that are beyond the scope of the value optimizer.
6903 if GNATprove_Mode then
6904 return;
6905 end if;
6907 -- Otherwise examine current value
6909 declare
6910 CV : constant Node_Id := Current_Value (Ent);
6911 Sens : Boolean;
6912 Stm : Node_Id;
6914 begin
6915 -- If statement. Condition is known true in THEN section, known False
6916 -- in any ELSIF or ELSE part, and unknown outside the IF statement.
6918 if Nkind (CV) = N_If_Statement then
6920 -- Before start of IF statement
6922 if Loc < Sloc (CV) then
6923 return;
6925 -- In condition of IF statement
6927 elsif In_Subtree (N => Var, Root => Condition (CV)) then
6928 return;
6930 -- After end of IF statement
6932 elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
6933 return;
6934 end if;
6936 -- At this stage we know that we are within the IF statement, but
6937 -- unfortunately, the tree does not record the SLOC of the ELSE so
6938 -- we cannot use a simple SLOC comparison to distinguish between
6939 -- the then/else statements, so we have to climb the tree.
6941 declare
6942 N : Node_Id;
6944 begin
6945 N := Parent (Var);
6946 while Parent (N) /= CV loop
6947 N := Parent (N);
6949 -- If we fall off the top of the tree, then that's odd, but
6950 -- perhaps it could occur in some error situation, and the
6951 -- safest response is simply to assume that the outcome of
6952 -- the condition is unknown. No point in bombing during an
6953 -- attempt to optimize things.
6955 if No (N) then
6956 return;
6957 end if;
6958 end loop;
6960 -- Now we have N pointing to a node whose parent is the IF
6961 -- statement in question, so now we can tell if we are within
6962 -- the THEN statements.
6964 if Is_List_Member (N)
6965 and then List_Containing (N) = Then_Statements (CV)
6966 then
6967 Sens := True;
6969 -- If the variable reference does not come from source, we
6970 -- cannot reliably tell whether it appears in the else part.
6971 -- In particular, if it appears in generated code for a node
6972 -- that requires finalization, it may be attached to a list
6973 -- that has not been yet inserted into the code. For now,
6974 -- treat it as unknown.
6976 elsif not Comes_From_Source (N) then
6977 return;
6979 -- Otherwise we must be in ELSIF or ELSE part
6981 else
6982 Sens := False;
6983 end if;
6984 end;
6986 -- ELSIF part. Condition is known true within the referenced
6987 -- ELSIF, known False in any subsequent ELSIF or ELSE part,
6988 -- and unknown before the ELSE part or after the IF statement.
6990 elsif Nkind (CV) = N_Elsif_Part then
6992 -- if the Elsif_Part had condition_actions, the elsif has been
6993 -- rewritten as a nested if, and the original elsif_part is
6994 -- detached from the tree, so there is no way to obtain useful
6995 -- information on the current value of the variable.
6996 -- Can this be improved ???
6998 if No (Parent (CV)) then
6999 return;
7000 end if;
7002 Stm := Parent (CV);
7004 -- If the tree has been otherwise rewritten there is nothing
7005 -- else to be done either.
7007 if Nkind (Stm) /= N_If_Statement then
7008 return;
7009 end if;
7011 -- Before start of ELSIF part
7013 if Loc < Sloc (CV) then
7014 return;
7016 -- In condition of ELSIF part
7018 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7019 return;
7021 -- After end of IF statement
7023 elsif Loc >= Sloc (Stm) +
7024 Text_Ptr (UI_To_Int (End_Span (Stm)))
7025 then
7026 return;
7027 end if;
7029 -- Again we lack the SLOC of the ELSE, so we need to climb the
7030 -- tree to see if we are within the ELSIF part in question.
7032 declare
7033 N : Node_Id;
7035 begin
7036 N := Parent (Var);
7037 while Parent (N) /= Stm loop
7038 N := Parent (N);
7040 -- If we fall off the top of the tree, then that's odd, but
7041 -- perhaps it could occur in some error situation, and the
7042 -- safest response is simply to assume that the outcome of
7043 -- the condition is unknown. No point in bombing during an
7044 -- attempt to optimize things.
7046 if No (N) then
7047 return;
7048 end if;
7049 end loop;
7051 -- Now we have N pointing to a node whose parent is the IF
7052 -- statement in question, so see if is the ELSIF part we want.
7053 -- the THEN statements.
7055 if N = CV then
7056 Sens := True;
7058 -- Otherwise we must be in subsequent ELSIF or ELSE part
7060 else
7061 Sens := False;
7062 end if;
7063 end;
7065 -- Iteration scheme of while loop. The condition is known to be
7066 -- true within the body of the loop.
7068 elsif Nkind (CV) = N_Iteration_Scheme then
7069 declare
7070 Loop_Stmt : constant Node_Id := Parent (CV);
7072 begin
7073 -- Before start of body of loop
7075 if Loc < Sloc (Loop_Stmt) then
7076 return;
7078 -- In condition of while loop
7080 elsif In_Subtree (N => Var, Root => Condition (CV)) then
7081 return;
7083 -- After end of LOOP statement
7085 elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
7086 return;
7088 -- We are within the body of the loop
7090 else
7091 Sens := True;
7092 end if;
7093 end;
7095 -- All other cases of Current_Value settings
7097 else
7098 return;
7099 end if;
7101 -- If we fall through here, then we have a reportable condition, Sens
7102 -- is True if the condition is true and False if it needs inverting.
7104 Process_Current_Value_Condition (Condition (CV), Sens);
7105 end;
7106 end Get_Current_Value_Condition;
7108 -----------------------
7109 -- Get_Index_Subtype --
7110 -----------------------
7112 function Get_Index_Subtype (N : Node_Id) return Entity_Id is
7113 P_Type : Entity_Id := Etype (Prefix (N));
7114 Indx : Node_Id;
7115 J : Int;
7117 begin
7118 if Is_Access_Type (P_Type) then
7119 P_Type := Designated_Type (P_Type);
7120 end if;
7122 if No (Expressions (N)) then
7123 J := 1;
7124 else
7125 J := UI_To_Int (Expr_Value (First (Expressions (N))));
7126 end if;
7128 Indx := First_Index (P_Type);
7129 while J > 1 loop
7130 Next_Index (Indx);
7131 J := J - 1;
7132 end loop;
7134 return Etype (Indx);
7135 end Get_Index_Subtype;
7137 -----------------------
7138 -- Get_Mapped_Entity --
7139 -----------------------
7141 function Get_Mapped_Entity (E : Entity_Id) return Entity_Id is
7142 begin
7143 return Type_Map.Get (E);
7144 end Get_Mapped_Entity;
7146 ---------------------
7147 -- Get_Stream_Size --
7148 ---------------------
7150 function Get_Stream_Size (E : Entity_Id) return Uint is
7151 begin
7152 -- If we have a Stream_Size clause for this type use it
7154 if Has_Stream_Size_Clause (E) then
7155 return Static_Integer (Expression (Stream_Size_Clause (E)));
7157 -- Otherwise the Stream_Size is the size of the type
7159 else
7160 return Esize (E);
7161 end if;
7162 end Get_Stream_Size;
7164 ---------------------------
7165 -- Has_Access_Constraint --
7166 ---------------------------
7168 function Has_Access_Constraint (E : Entity_Id) return Boolean is
7169 Disc : Entity_Id;
7170 T : constant Entity_Id := Etype (E);
7172 begin
7173 if Has_Per_Object_Constraint (E) and then Has_Discriminants (T) then
7174 Disc := First_Discriminant (T);
7175 while Present (Disc) loop
7176 if Is_Access_Type (Etype (Disc)) then
7177 return True;
7178 end if;
7180 Next_Discriminant (Disc);
7181 end loop;
7183 return False;
7184 else
7185 return False;
7186 end if;
7187 end Has_Access_Constraint;
7189 ---------------------
7190 -- Has_Tag_Of_Type --
7191 ---------------------
7193 function Has_Tag_Of_Type (Exp : Node_Id) return Boolean is
7194 Typ : constant Entity_Id := Etype (Exp);
7196 begin
7197 pragma Assert (Is_Tagged_Type (Typ));
7199 -- The tag of an object of a class-wide type is that of its
7200 -- initialization expression.
7202 if Is_Class_Wide_Type (Typ) then
7203 return False;
7204 end if;
7206 -- The tag of a stand-alone object of a specific tagged type T
7207 -- identifies T.
7209 if Is_Entity_Name (Exp)
7210 and then Ekind (Entity (Exp)) in E_Constant | E_Variable
7211 then
7212 return True;
7214 else
7215 case Nkind (Exp) is
7216 -- The tag of a component or an aggregate of a specific tagged
7217 -- type T identifies T.
7219 when N_Indexed_Component
7220 | N_Selected_Component
7221 | N_Aggregate
7223 return True;
7225 -- The tag of the result returned by a function whose result
7226 -- type is a specific tagged type T identifies T.
7228 when N_Function_Call =>
7229 return True;
7231 when N_Explicit_Dereference =>
7232 return Is_Captured_Function_Call (Exp);
7234 -- For a tagged type, the operand of a qualified expression
7235 -- shall resolve to be of the type of the expression.
7237 when N_Qualified_Expression =>
7238 return Has_Tag_Of_Type (Expression (Exp));
7240 when others =>
7241 return False;
7242 end case;
7243 end if;
7244 end Has_Tag_Of_Type;
7246 --------------------
7247 -- Homonym_Number --
7248 --------------------
7250 function Homonym_Number (Subp : Entity_Id) return Pos is
7251 Hom : Entity_Id := Homonym (Subp);
7252 Count : Pos := 1;
7254 begin
7255 while Present (Hom) loop
7256 if Scope (Hom) = Scope (Subp) then
7257 Count := Count + 1;
7258 end if;
7260 Hom := Homonym (Hom);
7261 end loop;
7263 return Count;
7264 end Homonym_Number;
7266 -----------------------------------
7267 -- In_Library_Level_Package_Body --
7268 -----------------------------------
7270 function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean is
7271 begin
7272 -- First determine whether the entity appears at the library level, then
7273 -- look at the containing unit.
7275 if Is_Library_Level_Entity (Id) then
7276 declare
7277 Container : constant Node_Id := Cunit (Get_Source_Unit (Id));
7279 begin
7280 return Nkind (Unit (Container)) = N_Package_Body;
7281 end;
7282 end if;
7284 return False;
7285 end In_Library_Level_Package_Body;
7287 ------------------------------
7288 -- In_Unconditional_Context --
7289 ------------------------------
7291 function In_Unconditional_Context (Node : Node_Id) return Boolean is
7292 P : Node_Id;
7294 begin
7295 P := Node;
7296 while Present (P) loop
7297 case Nkind (P) is
7298 when N_Subprogram_Body => return True;
7299 when N_If_Statement => return False;
7300 when N_Loop_Statement => return False;
7301 when N_Case_Statement => return False;
7302 when others => P := Parent (P);
7303 end case;
7304 end loop;
7306 return False;
7307 end In_Unconditional_Context;
7309 -------------------
7310 -- Insert_Action --
7311 -------------------
7313 procedure Insert_Action
7314 (Assoc_Node : Node_Id;
7315 Ins_Action : Node_Id;
7316 Spec_Expr_OK : Boolean := False)
7318 begin
7319 if Present (Ins_Action) then
7320 Insert_Actions
7321 (Assoc_Node => Assoc_Node,
7322 Ins_Actions => New_List (Ins_Action),
7323 Spec_Expr_OK => Spec_Expr_OK);
7324 end if;
7325 end Insert_Action;
7327 -- Version with check(s) suppressed
7329 procedure Insert_Action
7330 (Assoc_Node : Node_Id;
7331 Ins_Action : Node_Id;
7332 Suppress : Check_Id;
7333 Spec_Expr_OK : Boolean := False)
7335 begin
7336 Insert_Actions
7337 (Assoc_Node => Assoc_Node,
7338 Ins_Actions => New_List (Ins_Action),
7339 Suppress => Suppress,
7340 Spec_Expr_OK => Spec_Expr_OK);
7341 end Insert_Action;
7343 -------------------------
7344 -- Insert_Action_After --
7345 -------------------------
7347 procedure Insert_Action_After
7348 (Assoc_Node : Node_Id;
7349 Ins_Action : Node_Id)
7351 begin
7352 Insert_Actions_After (Assoc_Node, New_List (Ins_Action));
7353 end Insert_Action_After;
7355 --------------------
7356 -- Insert_Actions --
7357 --------------------
7359 procedure Insert_Actions
7360 (Assoc_Node : Node_Id;
7361 Ins_Actions : List_Id;
7362 Spec_Expr_OK : Boolean := False)
7364 N : Node_Id;
7365 P : Node_Id;
7367 Wrapped_Node : Node_Id := Empty;
7369 begin
7370 if Is_Empty_List (Ins_Actions) then
7371 return;
7372 end if;
7374 -- Insert the action when the context is "Handling of Default and Per-
7375 -- Object Expressions" only when requested by the caller.
7377 if Spec_Expr_OK then
7378 null;
7380 -- Ignore insert of actions from inside default expression (or other
7381 -- similar "spec expression") in the special spec-expression analyze
7382 -- mode. Any insertions at this point have no relevance, since we are
7383 -- only doing the analyze to freeze the types of any static expressions.
7384 -- See section "Handling of Default and Per-Object Expressions" in the
7385 -- spec of package Sem for further details.
7387 elsif In_Spec_Expression then
7388 return;
7389 end if;
7391 -- If the action derives from stuff inside a record, then the actions
7392 -- are attached to the current scope, to be inserted and analyzed on
7393 -- exit from the scope. The reason for this is that we may also be
7394 -- generating freeze actions at the same time, and they must eventually
7395 -- be elaborated in the correct order.
7397 if Is_Record_Type (Current_Scope)
7398 and then not Is_Frozen (Current_Scope)
7399 then
7400 if No (Scope_Stack.Table
7401 (Scope_Stack.Last).Pending_Freeze_Actions)
7402 then
7403 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions :=
7404 Ins_Actions;
7405 else
7406 Append_List
7407 (Ins_Actions,
7408 Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions);
7409 end if;
7411 return;
7412 end if;
7414 -- We now intend to climb up the tree to find the right point to
7415 -- insert the actions. We start at Assoc_Node, unless this node is a
7416 -- subexpression in which case we start with its parent. We do this for
7417 -- two reasons. First it speeds things up. Second, if Assoc_Node is
7418 -- itself one of the special nodes like N_And_Then, then we assume that
7419 -- an initial request to insert actions for such a node does not expect
7420 -- the actions to get deposited in the node for later handling when the
7421 -- node is expanded, since clearly the node is being dealt with by the
7422 -- caller. Note that in the subexpression case, N is always the child we
7423 -- came from.
7425 -- N_Raise_xxx_Error is an annoying special case, it is a statement
7426 -- if it has type Standard_Void_Type, and a subexpression otherwise.
7427 -- Procedure calls, and similarly procedure attribute references, are
7428 -- also statements.
7430 if Nkind (Assoc_Node) in N_Subexpr
7431 and then (Nkind (Assoc_Node) not in N_Raise_xxx_Error
7432 or else Etype (Assoc_Node) /= Standard_Void_Type)
7433 and then Nkind (Assoc_Node) /= N_Procedure_Call_Statement
7434 and then (Nkind (Assoc_Node) /= N_Attribute_Reference
7435 or else not Is_Procedure_Attribute_Name
7436 (Attribute_Name (Assoc_Node)))
7437 then
7438 N := Assoc_Node;
7439 P := Parent (Assoc_Node);
7441 -- Nonsubexpression case. Note that N is initially Empty in this case
7442 -- (N is only guaranteed non-Empty in the subexpr case).
7444 else
7445 N := Empty;
7446 P := Assoc_Node;
7447 end if;
7449 -- Capture root of the transient scope
7451 if Scope_Is_Transient then
7452 Wrapped_Node := Node_To_Be_Wrapped;
7453 end if;
7455 loop
7456 pragma Assert (Present (P));
7458 -- Make sure that inserted actions stay in the transient scope
7460 if Present (Wrapped_Node) and then N = Wrapped_Node then
7461 Store_Before_Actions_In_Scope (Ins_Actions);
7462 return;
7463 end if;
7465 case Nkind (P) is
7467 -- Case of right operand of AND THEN or OR ELSE. Put the actions
7468 -- in the Actions field of the right operand. They will be moved
7469 -- out further when the AND THEN or OR ELSE operator is expanded.
7470 -- Nothing special needs to be done for the left operand since
7471 -- in that case the actions are executed unconditionally.
7473 when N_Short_Circuit =>
7474 if N = Right_Opnd (P) then
7476 -- We are now going to either append the actions to the
7477 -- actions field of the short-circuit operation. We will
7478 -- also analyze the actions now.
7480 -- This analysis is really too early, the proper thing would
7481 -- be to just park them there now, and only analyze them if
7482 -- we find we really need them, and to it at the proper
7483 -- final insertion point. However attempting to this proved
7484 -- tricky, so for now we just kill current values before and
7485 -- after the analyze call to make sure we avoid peculiar
7486 -- optimizations from this out of order insertion.
7488 Kill_Current_Values;
7490 -- If P has already been expanded, we can't park new actions
7491 -- on it, so we need to expand them immediately, introducing
7492 -- an Expression_With_Actions. N can't be an expression
7493 -- with actions, or else then the actions would have been
7494 -- inserted at an inner level.
7496 if Analyzed (P) then
7497 pragma Assert (Nkind (N) /= N_Expression_With_Actions);
7498 Rewrite (N,
7499 Make_Expression_With_Actions (Sloc (N),
7500 Actions => Ins_Actions,
7501 Expression => Relocate_Node (N)));
7502 Analyze_And_Resolve (N);
7504 elsif Present (Actions (P)) then
7505 Insert_List_After_And_Analyze
7506 (Last (Actions (P)), Ins_Actions);
7507 else
7508 Set_Actions (P, Ins_Actions);
7509 Analyze_List (Actions (P));
7510 end if;
7512 Kill_Current_Values;
7514 return;
7515 end if;
7517 -- Then or Else dependent expression of an if expression. Add
7518 -- actions to Then_Actions or Else_Actions field as appropriate.
7519 -- The actions will be moved further out when the if is expanded.
7521 when N_If_Expression =>
7522 declare
7523 ThenX : constant Node_Id := Next (First (Expressions (P)));
7524 ElseX : constant Node_Id := Next (ThenX);
7526 begin
7527 -- If the enclosing expression is already analyzed, as
7528 -- is the case for nested elaboration checks, insert the
7529 -- conditional further out.
7531 if Analyzed (P) then
7532 null;
7534 -- Actions belong to the then expression, temporarily place
7535 -- them as Then_Actions of the if expression. They will be
7536 -- moved to the proper place later when the if expression is
7537 -- expanded.
7539 elsif N = ThenX then
7540 if Present (Then_Actions (P)) then
7541 Insert_List_After_And_Analyze
7542 (Last (Then_Actions (P)), Ins_Actions);
7543 else
7544 Set_Then_Actions (P, Ins_Actions);
7545 Analyze_List (Then_Actions (P));
7546 end if;
7548 return;
7550 -- Else_Actions is treated the same as Then_Actions above
7552 elsif N = ElseX then
7553 if Present (Else_Actions (P)) then
7554 Insert_List_After_And_Analyze
7555 (Last (Else_Actions (P)), Ins_Actions);
7556 else
7557 Set_Else_Actions (P, Ins_Actions);
7558 Analyze_List (Else_Actions (P));
7559 end if;
7561 return;
7563 -- Actions belong to the condition. In this case they are
7564 -- unconditionally executed, and so we can continue the
7565 -- search for the proper insert point.
7567 else
7568 null;
7569 end if;
7570 end;
7572 -- Alternative of case expression, we place the action in the
7573 -- Actions field of the case expression alternative, this will
7574 -- be handled when the case expression is expanded.
7576 when N_Case_Expression_Alternative =>
7577 if Present (Actions (P)) then
7578 Insert_List_After_And_Analyze
7579 (Last (Actions (P)), Ins_Actions);
7580 else
7581 Set_Actions (P, Ins_Actions);
7582 Analyze_List (Actions (P));
7583 end if;
7585 return;
7587 -- Case of appearing within an Expressions_With_Actions node. When
7588 -- the new actions come from the expression of the expression with
7589 -- actions, they must be added to the existing actions. The other
7590 -- alternative is when the new actions are related to one of the
7591 -- existing actions of the expression with actions, and should
7592 -- never reach here: if actions are inserted on a statement
7593 -- within the Actions of an expression with actions, or on some
7594 -- subexpression of such a statement, then the outermost proper
7595 -- insertion point is right before the statement, and we should
7596 -- never climb up as far as the N_Expression_With_Actions itself.
7598 when N_Expression_With_Actions =>
7599 if N = Expression (P) then
7600 if Is_Empty_List (Actions (P)) then
7601 Append_List_To (Actions (P), Ins_Actions);
7602 Analyze_List (Actions (P));
7603 else
7604 Insert_List_After_And_Analyze
7605 (Last (Actions (P)), Ins_Actions);
7606 end if;
7608 return;
7610 else
7611 raise Program_Error;
7612 end if;
7614 -- Case of appearing in the condition of a while expression or
7615 -- elsif. We insert the actions into the Condition_Actions field.
7616 -- They will be moved further out when the while loop or elsif
7617 -- is analyzed.
7619 when N_Elsif_Part
7620 | N_Iteration_Scheme
7622 if Present (Condition (P)) and then N = Condition (P) then
7623 if Present (Condition_Actions (P)) then
7624 Insert_List_After_And_Analyze
7625 (Last (Condition_Actions (P)), Ins_Actions);
7626 else
7627 Set_Condition_Actions (P, Ins_Actions);
7629 -- Set the parent of the insert actions explicitly. This
7630 -- is not a syntactic field, but we need the parent field
7631 -- set, in particular so that freeze can understand that
7632 -- it is dealing with condition actions, and properly
7633 -- insert the freezing actions.
7635 Set_Parent (Ins_Actions, P);
7636 Analyze_List (Condition_Actions (P));
7637 end if;
7639 return;
7640 end if;
7642 -- Statements, declarations, pragmas, representation clauses
7644 when
7645 -- Statements
7647 N_Procedure_Call_Statement
7648 | N_Statement_Other_Than_Procedure_Call
7650 -- Pragmas
7652 | N_Pragma
7654 -- Representation_Clause
7656 | N_At_Clause
7657 | N_Attribute_Definition_Clause
7658 | N_Enumeration_Representation_Clause
7659 | N_Record_Representation_Clause
7661 -- Declarations
7663 | N_Abstract_Subprogram_Declaration
7664 | N_Entry_Body
7665 | N_Exception_Declaration
7666 | N_Exception_Renaming_Declaration
7667 | N_Expression_Function
7668 | N_Formal_Abstract_Subprogram_Declaration
7669 | N_Formal_Concrete_Subprogram_Declaration
7670 | N_Formal_Object_Declaration
7671 | N_Formal_Type_Declaration
7672 | N_Full_Type_Declaration
7673 | N_Function_Instantiation
7674 | N_Generic_Function_Renaming_Declaration
7675 | N_Generic_Package_Declaration
7676 | N_Generic_Package_Renaming_Declaration
7677 | N_Generic_Procedure_Renaming_Declaration
7678 | N_Generic_Subprogram_Declaration
7679 | N_Implicit_Label_Declaration
7680 | N_Incomplete_Type_Declaration
7681 | N_Number_Declaration
7682 | N_Object_Declaration
7683 | N_Object_Renaming_Declaration
7684 | N_Package_Body
7685 | N_Package_Body_Stub
7686 | N_Package_Declaration
7687 | N_Package_Instantiation
7688 | N_Package_Renaming_Declaration
7689 | N_Private_Extension_Declaration
7690 | N_Private_Type_Declaration
7691 | N_Procedure_Instantiation
7692 | N_Protected_Body
7693 | N_Protected_Body_Stub
7694 | N_Single_Task_Declaration
7695 | N_Subprogram_Body
7696 | N_Subprogram_Body_Stub
7697 | N_Subprogram_Declaration
7698 | N_Subprogram_Renaming_Declaration
7699 | N_Subtype_Declaration
7700 | N_Task_Body
7701 | N_Task_Body_Stub
7703 -- Use clauses can appear in lists of declarations
7705 | N_Use_Package_Clause
7706 | N_Use_Type_Clause
7708 -- Freeze entity behaves like a declaration or statement
7710 | N_Freeze_Entity
7711 | N_Freeze_Generic_Entity
7713 -- Do not insert here if the item is not a list member (this
7714 -- happens for example with a triggering statement, and the
7715 -- proper approach is to insert before the entire select).
7717 if not Is_List_Member (P) then
7718 null;
7720 -- Do not insert if parent of P is an N_Component_Association
7721 -- node (i.e. we are in the context of an N_Aggregate or
7722 -- N_Extension_Aggregate node. In this case we want to insert
7723 -- before the entire aggregate.
7725 elsif Nkind (Parent (P)) = N_Component_Association then
7726 null;
7728 -- Do not insert if the parent of P is either an N_Variant node
7729 -- or an N_Record_Definition node, meaning in either case that
7730 -- P is a member of a component list, and that therefore the
7731 -- actions should be inserted outside the complete record
7732 -- declaration.
7734 elsif Nkind (Parent (P)) in N_Variant | N_Record_Definition then
7735 null;
7737 -- Do not insert freeze nodes within the loop generated for
7738 -- an aggregate, because they may be elaborated too late for
7739 -- subsequent use in the back end: within a package spec the
7740 -- loop is part of the elaboration procedure and is only
7741 -- elaborated during the second pass.
7743 -- If the loop comes from source, or the entity is local to the
7744 -- loop itself it must remain within.
7746 elsif Nkind (Parent (P)) = N_Loop_Statement
7747 and then not Comes_From_Source (Parent (P))
7748 and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
7749 and then
7750 Scope (Entity (First (Ins_Actions))) /= Current_Scope
7751 then
7752 null;
7754 -- Otherwise we can go ahead and do the insertion
7756 elsif P = Wrapped_Node then
7757 Store_Before_Actions_In_Scope (Ins_Actions);
7758 return;
7760 else
7761 Insert_List_Before_And_Analyze (P, Ins_Actions);
7762 return;
7763 end if;
7765 -- the expansion of Task and protected type declarations can
7766 -- create declarations for temporaries which, like other actions
7767 -- are inserted and analyzed before the current declaraation.
7768 -- However, the current scope is the synchronized type, and
7769 -- for unnesting it is critical that the proper scope for these
7770 -- generated entities be the enclosing one.
7772 when N_Task_Type_Declaration
7773 | N_Protected_Type_Declaration =>
7775 Push_Scope (Scope (Current_Scope));
7776 Insert_List_Before_And_Analyze (P, Ins_Actions);
7777 Pop_Scope;
7778 return;
7780 -- A special case, N_Raise_xxx_Error can act either as a statement
7781 -- or a subexpression. We tell the difference by looking at the
7782 -- Etype. It is set to Standard_Void_Type in the statement case.
7784 when N_Raise_xxx_Error =>
7785 if Etype (P) = Standard_Void_Type then
7786 if P = Wrapped_Node then
7787 Store_Before_Actions_In_Scope (Ins_Actions);
7788 else
7789 Insert_List_Before_And_Analyze (P, Ins_Actions);
7790 end if;
7792 return;
7794 -- In the subexpression case, keep climbing
7796 else
7797 null;
7798 end if;
7800 -- If a component association appears within a loop created for
7801 -- an array aggregate, attach the actions to the association so
7802 -- they can be subsequently inserted within the loop. For other
7803 -- component associations insert outside of the aggregate. For
7804 -- an association that will generate a loop, its Loop_Actions
7805 -- attribute is already initialized (see exp_aggr.adb).
7807 -- The list of Loop_Actions can in turn generate additional ones,
7808 -- that are inserted before the associated node. If the associated
7809 -- node is outside the aggregate, the new actions are collected
7810 -- at the end of the Loop_Actions, to respect the order in which
7811 -- they are to be elaborated.
7813 when N_Component_Association
7814 | N_Iterated_Component_Association
7815 | N_Iterated_Element_Association
7817 if Nkind (Parent (P)) in N_Aggregate | N_Delta_Aggregate
7819 -- We must not climb up out of an N_Iterated_xxx_Association
7820 -- because the actions might contain references to the loop
7821 -- parameter, except if we come from the Discrete_Choices of
7822 -- N_Iterated_Component_Association which cannot contain any.
7823 -- But it turns out that setting the Loop_Actions field in
7824 -- the case of an N_Component_Association when the field was
7825 -- not already set can lead to gigi assertion failures that
7826 -- are presumably due to malformed trees, so don't do that.
7828 and then (Nkind (P) /= N_Iterated_Component_Association
7829 or else not Is_List_Member (N)
7830 or else
7831 List_Containing (N) /= Discrete_Choices (P))
7832 and then (Nkind (P) /= N_Component_Association
7833 or else Present (Loop_Actions (P)))
7834 then
7835 if Is_Empty_List (Loop_Actions (P)) then
7836 Set_Loop_Actions (P, Ins_Actions);
7837 Analyze_List (Ins_Actions);
7838 else
7839 declare
7840 Decl : Node_Id;
7842 begin
7843 -- Check whether these actions were generated by a
7844 -- declaration that is part of the Loop_Actions for
7845 -- the component_association.
7847 Decl := Assoc_Node;
7848 while Present (Decl) loop
7849 exit when Parent (Decl) = P
7850 and then Is_List_Member (Decl)
7851 and then
7852 List_Containing (Decl) = Loop_Actions (P);
7853 Decl := Parent (Decl);
7854 end loop;
7856 if Present (Decl) then
7857 Insert_List_Before_And_Analyze
7858 (Decl, Ins_Actions);
7859 else
7860 Insert_List_After_And_Analyze
7861 (Last (Loop_Actions (P)), Ins_Actions);
7862 end if;
7863 end;
7864 end if;
7866 return;
7868 else
7869 null;
7870 end if;
7872 -- Special case: an attribute denoting a procedure call
7874 when N_Attribute_Reference =>
7875 if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
7876 if P = Wrapped_Node then
7877 Store_Before_Actions_In_Scope (Ins_Actions);
7878 else
7879 Insert_List_Before_And_Analyze (P, Ins_Actions);
7880 end if;
7882 return;
7884 -- In the subexpression case, keep climbing
7886 else
7887 null;
7888 end if;
7890 -- Special case: a marker
7892 when N_Call_Marker
7893 | N_Variable_Reference_Marker
7895 if Is_List_Member (P) then
7896 Insert_List_Before_And_Analyze (P, Ins_Actions);
7897 return;
7898 end if;
7900 -- A contract node should not belong to the tree
7902 when N_Contract =>
7903 raise Program_Error;
7905 -- For all other node types, keep climbing tree
7907 when N_Abortable_Part
7908 | N_Accept_Alternative
7909 | N_Access_Definition
7910 | N_Access_Function_Definition
7911 | N_Access_Procedure_Definition
7912 | N_Access_To_Object_Definition
7913 | N_Aggregate
7914 | N_Allocator
7915 | N_Aspect_Specification
7916 | N_Case_Expression
7917 | N_Case_Statement_Alternative
7918 | N_Character_Literal
7919 | N_Compilation_Unit
7920 | N_Compilation_Unit_Aux
7921 | N_Component_Clause
7922 | N_Component_Declaration
7923 | N_Component_Definition
7924 | N_Component_List
7925 | N_Constrained_Array_Definition
7926 | N_Decimal_Fixed_Point_Definition
7927 | N_Defining_Character_Literal
7928 | N_Defining_Identifier
7929 | N_Defining_Operator_Symbol
7930 | N_Defining_Program_Unit_Name
7931 | N_Delay_Alternative
7932 | N_Delta_Aggregate
7933 | N_Delta_Constraint
7934 | N_Derived_Type_Definition
7935 | N_Designator
7936 | N_Digits_Constraint
7937 | N_Discriminant_Association
7938 | N_Discriminant_Specification
7939 | N_Empty
7940 | N_Entry_Body_Formal_Part
7941 | N_Entry_Call_Alternative
7942 | N_Entry_Declaration
7943 | N_Entry_Index_Specification
7944 | N_Enumeration_Type_Definition
7945 | N_Error
7946 | N_Exception_Handler
7947 | N_Expanded_Name
7948 | N_Explicit_Dereference
7949 | N_Extension_Aggregate
7950 | N_Floating_Point_Definition
7951 | N_Formal_Decimal_Fixed_Point_Definition
7952 | N_Formal_Derived_Type_Definition
7953 | N_Formal_Discrete_Type_Definition
7954 | N_Formal_Floating_Point_Definition
7955 | N_Formal_Modular_Type_Definition
7956 | N_Formal_Ordinary_Fixed_Point_Definition
7957 | N_Formal_Package_Declaration
7958 | N_Formal_Private_Type_Definition
7959 | N_Formal_Incomplete_Type_Definition
7960 | N_Formal_Signed_Integer_Type_Definition
7961 | N_Function_Call
7962 | N_Function_Specification
7963 | N_Generic_Association
7964 | N_Handled_Sequence_Of_Statements
7965 | N_Identifier
7966 | N_In
7967 | N_Index_Or_Discriminant_Constraint
7968 | N_Indexed_Component
7969 | N_Integer_Literal
7970 | N_Iterator_Specification
7971 | N_Interpolated_String_Literal
7972 | N_Itype_Reference
7973 | N_Label
7974 | N_Loop_Parameter_Specification
7975 | N_Mod_Clause
7976 | N_Modular_Type_Definition
7977 | N_Not_In
7978 | N_Null
7979 | N_Op_Abs
7980 | N_Op_Add
7981 | N_Op_And
7982 | N_Op_Concat
7983 | N_Op_Divide
7984 | N_Op_Eq
7985 | N_Op_Expon
7986 | N_Op_Ge
7987 | N_Op_Gt
7988 | N_Op_Le
7989 | N_Op_Lt
7990 | N_Op_Minus
7991 | N_Op_Mod
7992 | N_Op_Multiply
7993 | N_Op_Ne
7994 | N_Op_Not
7995 | N_Op_Or
7996 | N_Op_Plus
7997 | N_Op_Rem
7998 | N_Op_Rotate_Left
7999 | N_Op_Rotate_Right
8000 | N_Op_Shift_Left
8001 | N_Op_Shift_Right
8002 | N_Op_Shift_Right_Arithmetic
8003 | N_Op_Subtract
8004 | N_Op_Xor
8005 | N_Operator_Symbol
8006 | N_Ordinary_Fixed_Point_Definition
8007 | N_Others_Choice
8008 | N_Package_Specification
8009 | N_Parameter_Association
8010 | N_Parameter_Specification
8011 | N_Pop_Constraint_Error_Label
8012 | N_Pop_Program_Error_Label
8013 | N_Pop_Storage_Error_Label
8014 | N_Pragma_Argument_Association
8015 | N_Procedure_Specification
8016 | N_Protected_Definition
8017 | N_Push_Constraint_Error_Label
8018 | N_Push_Program_Error_Label
8019 | N_Push_Storage_Error_Label
8020 | N_Qualified_Expression
8021 | N_Quantified_Expression
8022 | N_Raise_Expression
8023 | N_Range
8024 | N_Range_Constraint
8025 | N_Real_Literal
8026 | N_Real_Range_Specification
8027 | N_Record_Definition
8028 | N_Reference
8029 | N_SCIL_Dispatch_Table_Tag_Init
8030 | N_SCIL_Dispatching_Call
8031 | N_SCIL_Membership_Test
8032 | N_Selected_Component
8033 | N_Signed_Integer_Type_Definition
8034 | N_Single_Protected_Declaration
8035 | N_Slice
8036 | N_String_Literal
8037 | N_Subtype_Indication
8038 | N_Subunit
8039 | N_Target_Name
8040 | N_Task_Definition
8041 | N_Terminate_Alternative
8042 | N_Triggering_Alternative
8043 | N_Type_Conversion
8044 | N_Unchecked_Expression
8045 | N_Unchecked_Type_Conversion
8046 | N_Unconstrained_Array_Definition
8047 | N_Unused_At_End
8048 | N_Unused_At_Start
8049 | N_Variant
8050 | N_Variant_Part
8051 | N_Validate_Unchecked_Conversion
8052 | N_With_Clause
8054 null;
8055 end case;
8057 -- If we fall through above tests, keep climbing tree
8059 N := P;
8061 if Nkind (Parent (N)) = N_Subunit then
8063 -- This is the proper body corresponding to a stub. Insertion must
8064 -- be done at the point of the stub, which is in the declarative
8065 -- part of the parent unit.
8067 P := Corresponding_Stub (Parent (N));
8069 else
8070 P := Parent (N);
8071 end if;
8072 end loop;
8073 end Insert_Actions;
8075 -- Version with check(s) suppressed
8077 procedure Insert_Actions
8078 (Assoc_Node : Node_Id;
8079 Ins_Actions : List_Id;
8080 Suppress : Check_Id;
8081 Spec_Expr_OK : Boolean := False)
8083 begin
8084 if Suppress = All_Checks then
8085 declare
8086 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
8087 begin
8088 Scope_Suppress.Suppress := (others => True);
8089 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8090 Scope_Suppress.Suppress := Sva;
8091 end;
8093 else
8094 declare
8095 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
8096 begin
8097 Scope_Suppress.Suppress (Suppress) := True;
8098 Insert_Actions (Assoc_Node, Ins_Actions, Spec_Expr_OK);
8099 Scope_Suppress.Suppress (Suppress) := Svg;
8100 end;
8101 end if;
8102 end Insert_Actions;
8104 --------------------------
8105 -- Insert_Actions_After --
8106 --------------------------
8108 procedure Insert_Actions_After
8109 (Assoc_Node : Node_Id;
8110 Ins_Actions : List_Id)
8112 begin
8113 if Scope_Is_Transient and then Assoc_Node = Node_To_Be_Wrapped then
8114 Store_After_Actions_In_Scope (Ins_Actions);
8115 else
8116 Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
8117 end if;
8118 end Insert_Actions_After;
8120 ---------------------------------
8121 -- Insert_Library_Level_Action --
8122 ---------------------------------
8124 procedure Insert_Library_Level_Action (N : Node_Id) is
8125 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8127 begin
8128 Push_Scope (Cunit_Entity (Current_Sem_Unit));
8129 -- And not Main_Unit as previously. If the main unit is a body,
8130 -- the scope needed to analyze the actions is the entity of the
8131 -- corresponding declaration.
8133 if No (Actions (Aux)) then
8134 Set_Actions (Aux, New_List (N));
8135 else
8136 Append (N, Actions (Aux));
8137 end if;
8139 Analyze (N);
8140 Pop_Scope;
8141 end Insert_Library_Level_Action;
8143 ----------------------------------
8144 -- Insert_Library_Level_Actions --
8145 ----------------------------------
8147 procedure Insert_Library_Level_Actions (L : List_Id) is
8148 Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
8150 begin
8151 if Is_Non_Empty_List (L) then
8152 Push_Scope (Cunit_Entity (Main_Unit));
8153 -- ??? should this be Current_Sem_Unit instead of Main_Unit?
8155 if No (Actions (Aux)) then
8156 Set_Actions (Aux, L);
8157 Analyze_List (L);
8158 else
8159 Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
8160 end if;
8162 Pop_Scope;
8163 end if;
8164 end Insert_Library_Level_Actions;
8166 ----------------------
8167 -- Inside_Init_Proc --
8168 ----------------------
8170 function Inside_Init_Proc return Boolean is
8171 begin
8172 return Present (Enclosing_Init_Proc);
8173 end Inside_Init_Proc;
8175 ----------------------
8176 -- Integer_Type_For --
8177 ----------------------
8179 function Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id is
8180 begin
8181 pragma Assert
8182 (Standard_Long_Integer_Size in
8183 Standard_Integer_Size | Standard_Long_Long_Integer_Size);
8184 -- So we don't need to check for Standard_Long_Integer_Size below
8185 pragma Assert (S <= System_Max_Integer_Size);
8187 -- This is the canonical 32-bit type
8189 if S <= Standard_Integer_Size then
8190 if Uns then
8191 return Standard_Unsigned;
8192 else
8193 return Standard_Integer;
8194 end if;
8196 -- This is the canonical 64-bit type
8198 elsif S <= Standard_Long_Long_Integer_Size then
8199 if Uns then
8200 return Standard_Long_Long_Unsigned;
8201 else
8202 return Standard_Long_Long_Integer;
8203 end if;
8205 -- This is the canonical 128-bit type
8207 elsif S <= Standard_Long_Long_Long_Integer_Size then
8208 if Uns then
8209 return Standard_Long_Long_Long_Unsigned;
8210 else
8211 return Standard_Long_Long_Long_Integer;
8212 end if;
8214 else
8215 raise Program_Error;
8216 end if;
8217 end Integer_Type_For;
8219 -------------------------------
8220 -- Is_Captured_Function_Call --
8221 -------------------------------
8223 function Is_Captured_Function_Call (N : Node_Id) return Boolean is
8224 begin
8225 if Nkind (N) = N_Explicit_Dereference
8226 and then Is_Entity_Name (Prefix (N))
8227 and then Ekind (Entity (Prefix (N))) = E_Constant
8228 then
8229 declare
8230 Value : constant Node_Id := Constant_Value (Entity (Prefix (N)));
8232 begin
8233 return Present (Value)
8234 and then Nkind (Value) = N_Reference
8235 and then Nkind (Prefix (Value)) = N_Function_Call;
8236 end;
8238 else
8239 return False;
8240 end if;
8241 end Is_Captured_Function_Call;
8243 ------------------------------
8244 -- Is_Finalizable_Transient --
8245 ------------------------------
8247 function Is_Finalizable_Transient
8248 (Decl : Node_Id;
8249 Rel_Node : Node_Id) return Boolean
8251 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
8252 Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id));
8254 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean;
8255 -- Determine whether transient object Trans_Id is initialized either
8256 -- by a function call which returns an access type or simply renames
8257 -- another pointer.
8259 function Initialized_By_Aliased_BIP_Func_Call
8260 (Trans_Id : Entity_Id) return Boolean;
8261 -- Determine whether transient object Trans_Id is initialized by a
8262 -- build-in-place function call where the BIPalloc parameter either
8263 -- does not exist or is Caller_Allocation, and BIPaccess is not null.
8264 -- This case creates an aliasing between the returned value and the
8265 -- value denoted by BIPaccess.
8267 function Is_Aliased
8268 (Trans_Id : Entity_Id;
8269 First_Stmt : Node_Id) return Boolean;
8270 -- Determine whether transient object Trans_Id has been renamed or
8271 -- aliased through 'reference in the statement list starting from
8272 -- First_Stmt.
8274 function Is_Allocated (Trans_Id : Entity_Id) return Boolean;
8275 -- Determine whether transient object Trans_Id is allocated on the heap
8277 function Is_Iterated_Container
8278 (Trans_Id : Entity_Id;
8279 First_Stmt : Node_Id) return Boolean;
8280 -- Determine whether transient object Trans_Id denotes a container which
8281 -- is in the process of being iterated in the statement list starting
8282 -- from First_Stmt.
8284 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean;
8285 -- Return True if N is directly part of a build-in-place return
8286 -- statement.
8288 ---------------------------
8289 -- Initialized_By_Access --
8290 ---------------------------
8292 function Initialized_By_Access (Trans_Id : Entity_Id) return Boolean is
8293 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8295 begin
8296 return
8297 Present (Expr)
8298 and then Nkind (Expr) /= N_Reference
8299 and then Is_Access_Type (Etype (Expr));
8300 end Initialized_By_Access;
8302 ------------------------------------------
8303 -- Initialized_By_Aliased_BIP_Func_Call --
8304 ------------------------------------------
8306 function Initialized_By_Aliased_BIP_Func_Call
8307 (Trans_Id : Entity_Id) return Boolean
8309 Call : Node_Id := Expression (Parent (Trans_Id));
8311 begin
8312 -- Build-in-place calls usually appear in 'reference format
8314 if Nkind (Call) = N_Reference then
8315 Call := Prefix (Call);
8316 end if;
8318 Call := Unqual_Conv (Call);
8320 if Is_Build_In_Place_Function_Call (Call) then
8321 declare
8322 Caller_Allocation_Val : constant Uint :=
8323 UI_From_Int (BIP_Allocation_Form'Pos (Caller_Allocation));
8325 Access_Nam : Name_Id := No_Name;
8326 Access_OK : Boolean := False;
8327 Actual : Node_Id;
8328 Alloc_Nam : Name_Id := No_Name;
8329 Alloc_OK : Boolean := True;
8330 Formal : Node_Id;
8331 Func_Id : Entity_Id;
8332 Param : Node_Id;
8334 begin
8335 -- Examine all parameter associations of the function call
8337 Param := First (Parameter_Associations (Call));
8338 while Present (Param) loop
8339 if Nkind (Param) = N_Parameter_Association
8340 and then Nkind (Selector_Name (Param)) = N_Identifier
8341 then
8342 Actual := Explicit_Actual_Parameter (Param);
8343 Formal := Selector_Name (Param);
8345 -- Construct the names of formals BIPaccess and BIPalloc
8346 -- using the function name retrieved from an arbitrary
8347 -- formal.
8349 if Access_Nam = No_Name
8350 and then Alloc_Nam = No_Name
8351 and then Present (Entity (Formal))
8352 then
8353 Func_Id := Scope (Entity (Formal));
8355 Access_Nam :=
8356 New_External_Name (Chars (Func_Id),
8357 BIP_Formal_Suffix (BIP_Object_Access));
8359 Alloc_Nam :=
8360 New_External_Name (Chars (Func_Id),
8361 BIP_Formal_Suffix (BIP_Alloc_Form));
8362 end if;
8364 -- A nonnull BIPaccess has been found
8366 if Chars (Formal) = Access_Nam
8367 and then Nkind (Actual) /= N_Null
8368 then
8369 Access_OK := True;
8370 end if;
8372 -- A BIPalloc has been found
8374 if Chars (Formal) = Alloc_Nam
8375 and then Nkind (Actual) = N_Integer_Literal
8376 then
8377 Alloc_OK := Intval (Actual) = Caller_Allocation_Val;
8378 end if;
8379 end if;
8381 Next (Param);
8382 end loop;
8384 return Access_OK and Alloc_OK;
8385 end;
8386 end if;
8388 return False;
8389 end Initialized_By_Aliased_BIP_Func_Call;
8391 ----------------
8392 -- Is_Aliased --
8393 ----------------
8395 function Is_Aliased
8396 (Trans_Id : Entity_Id;
8397 First_Stmt : Node_Id) return Boolean
8399 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id;
8400 -- Given an object renaming declaration, retrieve the entity of the
8401 -- renamed name. Return Empty if the renamed name is anything other
8402 -- than a variable or a constant.
8404 -------------------------
8405 -- Find_Renamed_Object --
8406 -------------------------
8408 function Find_Renamed_Object (Ren_Decl : Node_Id) return Entity_Id is
8409 Ren_Obj : Node_Id := Empty;
8411 function Find_Object (N : Node_Id) return Traverse_Result;
8412 -- Try to detect an object which is either a constant or a
8413 -- variable.
8415 -----------------
8416 -- Find_Object --
8417 -----------------
8419 function Find_Object (N : Node_Id) return Traverse_Result is
8420 begin
8421 -- Stop the search once a constant or a variable has been
8422 -- detected.
8424 if Nkind (N) = N_Identifier
8425 and then Present (Entity (N))
8426 and then Ekind (Entity (N)) in E_Constant | E_Variable
8427 then
8428 Ren_Obj := Entity (N);
8429 return Abandon;
8430 end if;
8432 return OK;
8433 end Find_Object;
8435 procedure Search is new Traverse_Proc (Find_Object);
8437 -- Local variables
8439 Typ : constant Entity_Id := Etype (Defining_Identifier (Ren_Decl));
8441 -- Start of processing for Find_Renamed_Object
8443 begin
8444 -- Actions related to dispatching calls may appear as renamings of
8445 -- tags. Do not process this type of renaming because it does not
8446 -- use the actual value of the object.
8448 if not Is_RTE (Typ, RE_Tag_Ptr) then
8449 Search (Name (Ren_Decl));
8450 end if;
8452 -- For renamings generated by Expand_N_Object_Declaration to deal
8453 -- with (class-wide) interface objects, there is an intermediate
8454 -- temporary of an anonymous access type used to hold the result
8455 -- of the displacement of the address of the renamed object.
8457 if Present (Ren_Obj)
8458 and then Ekind (Ren_Obj) = E_Constant
8459 and then Is_Itype (Etype (Ren_Obj))
8460 and then Ekind (Etype (Ren_Obj)) = E_Anonymous_Access_Type
8461 and then
8462 Is_Class_Wide_Type (Directly_Designated_Type (Etype (Ren_Obj)))
8463 and then
8464 Is_Interface (Directly_Designated_Type (Etype (Ren_Obj)))
8465 then
8466 Search (Constant_Value (Ren_Obj));
8467 end if;
8469 return Ren_Obj;
8470 end Find_Renamed_Object;
8472 -- Local variables
8474 Expr : Node_Id;
8475 Ren_Obj : Entity_Id;
8476 Stmt : Node_Id;
8478 -- Start of processing for Is_Aliased
8480 begin
8481 -- A controlled transient object is not considered aliased when it
8482 -- appears inside an expression_with_actions node even when there are
8483 -- explicit aliases of it:
8485 -- do
8486 -- Trans_Id : Ctrl_Typ ...; -- transient object
8487 -- Alias : ... := Trans_Id; -- object is aliased
8488 -- Val : constant Boolean :=
8489 -- ... Alias ...; -- aliasing ends
8490 -- <finalize Trans_Id> -- object safe to finalize
8491 -- in Val end;
8493 -- Expansion ensures that all aliases are encapsulated in the actions
8494 -- list and do not leak to the expression by forcing the evaluation
8495 -- of the expression.
8497 if Nkind (Rel_Node) = N_Expression_With_Actions then
8498 return False;
8500 -- Otherwise examine the statements after the controlled transient
8501 -- object and look for various forms of aliasing.
8503 else
8504 Stmt := First_Stmt;
8505 while Present (Stmt) loop
8506 if Nkind (Stmt) = N_Object_Declaration then
8507 Expr := Expression (Stmt);
8509 -- Aliasing of the form:
8510 -- Obj : ... := Trans_Id'reference;
8512 if Present (Expr)
8513 and then Nkind (Expr) = N_Reference
8514 and then Nkind (Prefix (Expr)) = N_Identifier
8515 and then Entity (Prefix (Expr)) = Trans_Id
8516 then
8517 return True;
8518 end if;
8520 elsif Nkind (Stmt) = N_Object_Renaming_Declaration then
8521 Ren_Obj := Find_Renamed_Object (Stmt);
8523 -- Aliasing of the form:
8524 -- Obj : ... renames ... Trans_Id ...;
8526 if Present (Ren_Obj) and then Ren_Obj = Trans_Id then
8527 return True;
8528 end if;
8529 end if;
8531 Next (Stmt);
8532 end loop;
8534 return False;
8535 end if;
8536 end Is_Aliased;
8538 ------------------
8539 -- Is_Allocated --
8540 ------------------
8542 function Is_Allocated (Trans_Id : Entity_Id) return Boolean is
8543 Expr : constant Node_Id := Expression (Parent (Trans_Id));
8544 begin
8545 return
8546 Is_Access_Type (Etype (Trans_Id))
8547 and then Present (Expr)
8548 and then Nkind (Expr) = N_Allocator;
8549 end Is_Allocated;
8551 ---------------------------
8552 -- Is_Iterated_Container --
8553 ---------------------------
8555 function Is_Iterated_Container
8556 (Trans_Id : Entity_Id;
8557 First_Stmt : Node_Id) return Boolean
8559 Aspect : Node_Id;
8560 Call : Node_Id;
8561 Iter : Entity_Id;
8562 Param : Node_Id;
8563 Stmt : Node_Id;
8564 Typ : Entity_Id;
8566 begin
8567 -- It is not possible to iterate over containers in non-Ada 2012 code
8569 if Ada_Version < Ada_2012 then
8570 return False;
8571 end if;
8573 Typ := Etype (Trans_Id);
8575 -- Handle access type created for secondary stack use
8577 if Is_Access_Type (Typ) then
8578 Typ := Designated_Type (Typ);
8579 end if;
8581 -- Look for aspect Default_Iterator. It may be part of a type
8582 -- declaration for a container, or inherited from a base type
8583 -- or parent type.
8585 Aspect := Find_Value_Of_Aspect (Typ, Aspect_Default_Iterator);
8587 if Present (Aspect) then
8588 Iter := Entity (Aspect);
8590 -- Examine the statements following the container object and
8591 -- look for a call to the default iterate routine where the
8592 -- first parameter is the transient. Such a call appears as:
8594 -- It : Access_To_CW_Iterator :=
8595 -- Iterate (Tran_Id.all, ...)'reference;
8597 Stmt := First_Stmt;
8598 while Present (Stmt) loop
8600 -- Detect an object declaration which is initialized by a
8601 -- secondary stack function call.
8603 if Nkind (Stmt) = N_Object_Declaration
8604 and then Present (Expression (Stmt))
8605 and then Nkind (Expression (Stmt)) = N_Reference
8606 and then Nkind (Prefix (Expression (Stmt))) = N_Function_Call
8607 then
8608 Call := Prefix (Expression (Stmt));
8610 -- The call must invoke the default iterate routine of
8611 -- the container and the transient object must appear as
8612 -- the first actual parameter. Skip any calls whose names
8613 -- are not entities.
8615 if Is_Entity_Name (Name (Call))
8616 and then Entity (Name (Call)) = Iter
8617 and then Present (Parameter_Associations (Call))
8618 then
8619 Param := First (Parameter_Associations (Call));
8621 if Nkind (Param) = N_Explicit_Dereference
8622 and then Entity (Prefix (Param)) = Trans_Id
8623 then
8624 return True;
8625 end if;
8626 end if;
8627 end if;
8629 Next (Stmt);
8630 end loop;
8631 end if;
8633 return False;
8634 end Is_Iterated_Container;
8636 -------------------------------------
8637 -- Is_Part_Of_BIP_Return_Statement --
8638 -------------------------------------
8640 function Is_Part_Of_BIP_Return_Statement (N : Node_Id) return Boolean is
8641 Subp : constant Entity_Id := Current_Subprogram;
8642 Context : Node_Id;
8643 begin
8644 -- First check if N is part of a BIP function
8646 if No (Subp)
8647 or else not Is_Build_In_Place_Function (Subp)
8648 then
8649 return False;
8650 end if;
8652 -- Then check whether N is a complete part of a return statement
8653 -- Should we consider other node kinds to go up the tree???
8655 Context := N;
8656 loop
8657 case Nkind (Context) is
8658 when N_Expression_With_Actions => Context := Parent (Context);
8659 when N_Simple_Return_Statement => return True;
8660 when others => return False;
8661 end case;
8662 end loop;
8663 end Is_Part_Of_BIP_Return_Statement;
8665 -- Local variables
8667 Desig : Entity_Id := Obj_Typ;
8669 -- Start of processing for Is_Finalizable_Transient
8671 begin
8672 -- Handle access types
8674 if Is_Access_Type (Desig) then
8675 Desig := Available_View (Designated_Type (Desig));
8676 end if;
8678 return
8679 Ekind (Obj_Id) in E_Constant | E_Variable
8680 and then Needs_Finalization (Desig)
8681 and then Nkind (Rel_Node) /= N_Simple_Return_Statement
8682 and then not Is_Part_Of_BIP_Return_Statement (Rel_Node)
8684 -- Do not consider a transient object that was already processed
8686 and then not Is_Finalized_Transient (Obj_Id)
8688 -- Do not consider renamed or 'reference-d transient objects because
8689 -- the act of renaming extends the object's lifetime.
8691 and then not Is_Aliased (Obj_Id, Decl)
8693 -- Do not consider transient objects allocated on the heap since
8694 -- they are attached to a finalization master.
8696 and then not Is_Allocated (Obj_Id)
8698 -- If the transient object is a pointer, check that it is not
8699 -- initialized by a function that returns a pointer or acts as a
8700 -- renaming of another pointer.
8702 and then not
8703 (Is_Access_Type (Obj_Typ) and then Initialized_By_Access (Obj_Id))
8705 -- Do not consider transient objects which act as indirect aliases
8706 -- of build-in-place function results.
8708 and then not Initialized_By_Aliased_BIP_Func_Call (Obj_Id)
8710 -- Do not consider iterators because those are treated as normal
8711 -- controlled objects and are processed by the usual finalization
8712 -- machinery. This avoids the double finalization of an iterator.
8714 and then not Is_Iterator (Desig)
8716 -- Do not consider containers in the context of iterator loops. Such
8717 -- transient objects must exist for as long as the loop is around,
8718 -- otherwise any operation carried out by the iterator will fail.
8720 and then not Is_Iterated_Container (Obj_Id, Decl);
8721 end Is_Finalizable_Transient;
8723 ---------------------------------
8724 -- Is_Fully_Repped_Tagged_Type --
8725 ---------------------------------
8727 function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
8728 U : constant Entity_Id := Underlying_Type (T);
8729 Comp : Entity_Id;
8731 begin
8732 if No (U) or else not Is_Tagged_Type (U) then
8733 return False;
8734 elsif Has_Discriminants (U) then
8735 return False;
8736 elsif not Has_Specified_Layout (U) then
8737 return False;
8738 end if;
8740 -- Here we have a tagged type, see if it has any component (other than
8741 -- tag and parent) with no component_clause. If so, we return False.
8743 Comp := First_Component (U);
8744 while Present (Comp) loop
8745 if not Is_Tag (Comp)
8746 and then Chars (Comp) /= Name_uParent
8747 and then No (Component_Clause (Comp))
8748 then
8749 return False;
8750 else
8751 Next_Component (Comp);
8752 end if;
8753 end loop;
8755 -- All components have clauses
8757 return True;
8758 end Is_Fully_Repped_Tagged_Type;
8760 ----------------------------------
8761 -- Is_Library_Level_Tagged_Type --
8762 ----------------------------------
8764 function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
8765 begin
8766 return Is_Tagged_Type (Typ) and then Is_Library_Level_Entity (Typ);
8767 end Is_Library_Level_Tagged_Type;
8769 --------------------------
8770 -- Is_Non_BIP_Func_Call --
8771 --------------------------
8773 function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean is
8774 begin
8775 -- The expected call is of the format
8777 -- Func_Call'reference
8779 return
8780 Nkind (Expr) = N_Reference
8781 and then Nkind (Prefix (Expr)) = N_Function_Call
8782 and then not Is_Build_In_Place_Function_Call (Prefix (Expr));
8783 end Is_Non_BIP_Func_Call;
8785 ----------------------------------
8786 -- Is_Possibly_Unaligned_Object --
8787 ----------------------------------
8789 function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
8790 T : constant Entity_Id := Etype (N);
8792 begin
8793 -- If renamed object, apply test to underlying object
8795 if Is_Entity_Name (N)
8796 and then Is_Object (Entity (N))
8797 and then Present (Renamed_Object (Entity (N)))
8798 then
8799 return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
8800 end if;
8802 -- Tagged and controlled types and aliased types are always aligned, as
8803 -- are concurrent types.
8805 if Is_Aliased (T)
8806 or else Has_Controlled_Component (T)
8807 or else Is_Concurrent_Type (T)
8808 or else Is_Tagged_Type (T)
8809 or else Is_Controlled (T)
8810 then
8811 return False;
8812 end if;
8814 -- If this is an element of a packed array, may be unaligned
8816 if Is_Ref_To_Bit_Packed_Array (N) then
8817 return True;
8818 end if;
8820 -- Case of indexed component reference: test whether prefix is unaligned
8822 if Nkind (N) = N_Indexed_Component then
8823 return Is_Possibly_Unaligned_Object (Prefix (N));
8825 -- Case of selected component reference
8827 elsif Nkind (N) = N_Selected_Component then
8828 declare
8829 P : constant Node_Id := Prefix (N);
8830 C : constant Entity_Id := Entity (Selector_Name (N));
8831 M : Nat;
8832 S : Nat;
8834 begin
8835 -- If component reference is for an array with nonstatic bounds,
8836 -- then it is always aligned: we can only process unaligned arrays
8837 -- with static bounds (more precisely compile time known bounds).
8839 if Is_Array_Type (T)
8840 and then not Compile_Time_Known_Bounds (T)
8841 then
8842 return False;
8843 end if;
8845 -- If component is aliased, it is definitely properly aligned
8847 if Is_Aliased (C) then
8848 return False;
8849 end if;
8851 -- If component is for a type implemented as a scalar, and the
8852 -- record is packed, and the component is other than the first
8853 -- component of the record, then the component may be unaligned.
8855 if Is_Packed (Etype (P))
8856 and then Represented_As_Scalar (Etype (C))
8857 and then First_Entity (Scope (C)) /= C
8858 then
8859 return True;
8860 end if;
8862 -- Compute maximum possible alignment for T
8864 -- If alignment is known, then that settles things
8866 if Known_Alignment (T) then
8867 M := UI_To_Int (Alignment (T));
8869 -- If alignment is not known, tentatively set max alignment
8871 else
8872 M := Ttypes.Maximum_Alignment;
8874 -- We can reduce this if the Esize is known since the default
8875 -- alignment will never be more than the smallest power of 2
8876 -- that does not exceed this Esize value.
8878 if Known_Esize (T) then
8879 S := UI_To_Int (Esize (T));
8881 while (M / 2) >= S loop
8882 M := M / 2;
8883 end loop;
8884 end if;
8885 end if;
8887 -- Case of component clause present which may specify an
8888 -- unaligned position.
8890 if Present (Component_Clause (C)) then
8892 -- Otherwise we can do a test to make sure that the actual
8893 -- start position in the record, and the length, are both
8894 -- consistent with the required alignment. If not, we know
8895 -- that we are unaligned.
8897 declare
8898 Align_In_Bits : constant Nat := M * System_Storage_Unit;
8899 Comp : Entity_Id;
8901 begin
8902 Comp := C;
8904 -- For a component inherited in a record extension, the
8905 -- clause is inherited but position and size are not set.
8907 if Is_Base_Type (Etype (P))
8908 and then Is_Tagged_Type (Etype (P))
8909 and then Present (Original_Record_Component (Comp))
8910 then
8911 Comp := Original_Record_Component (Comp);
8912 end if;
8914 if Component_Bit_Offset (Comp) mod Align_In_Bits /= 0
8915 or else Esize (Comp) mod Align_In_Bits /= 0
8916 then
8917 return True;
8918 end if;
8919 end;
8920 end if;
8922 -- Otherwise, for a component reference, test prefix
8924 return Is_Possibly_Unaligned_Object (P);
8925 end;
8927 -- If not a component reference, must be aligned
8929 else
8930 return False;
8931 end if;
8932 end Is_Possibly_Unaligned_Object;
8934 ---------------------------------
8935 -- Is_Possibly_Unaligned_Slice --
8936 ---------------------------------
8938 function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
8939 begin
8940 -- Go to renamed object
8942 if Is_Entity_Name (N)
8943 and then Is_Object (Entity (N))
8944 and then Present (Renamed_Object (Entity (N)))
8945 then
8946 return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
8947 end if;
8949 -- The reference must be a slice
8951 if Nkind (N) /= N_Slice then
8952 return False;
8953 end if;
8955 -- If it is a slice, then look at the array type being sliced
8957 declare
8958 Sarr : constant Node_Id := Prefix (N);
8959 -- Prefix of the slice, i.e. the array being sliced
8961 Styp : constant Entity_Id := Etype (Prefix (N));
8962 -- Type of the array being sliced
8964 Pref : Node_Id;
8965 Ptyp : Entity_Id;
8967 begin
8968 -- The problems arise if the array object that is being sliced
8969 -- is a component of a record or array, and we cannot guarantee
8970 -- the alignment of the array within its containing object.
8972 -- To investigate this, we look at successive prefixes to see
8973 -- if we have a worrisome indexed or selected component.
8975 Pref := Sarr;
8976 loop
8977 -- Case of array is part of an indexed component reference
8979 if Nkind (Pref) = N_Indexed_Component then
8980 Ptyp := Etype (Prefix (Pref));
8982 -- The only problematic case is when the array is packed, in
8983 -- which case we really know nothing about the alignment of
8984 -- individual components.
8986 if Is_Bit_Packed_Array (Ptyp) then
8987 return True;
8988 end if;
8990 -- Case of array is part of a selected component reference
8992 elsif Nkind (Pref) = N_Selected_Component then
8993 Ptyp := Etype (Prefix (Pref));
8995 -- We are definitely in trouble if the record in question
8996 -- has an alignment, and either we know this alignment is
8997 -- inconsistent with the alignment of the slice, or we don't
8998 -- know what the alignment of the slice should be. But this
8999 -- really matters only if the target has strict alignment.
9001 if Target_Strict_Alignment
9002 and then Known_Alignment (Ptyp)
9003 and then (not Known_Alignment (Styp)
9004 or else Alignment (Styp) > Alignment (Ptyp))
9005 then
9006 return True;
9007 end if;
9009 -- We are in potential trouble if the record type is packed.
9010 -- We could special case when we know that the array is the
9011 -- first component, but that's not such a simple case ???
9013 if Is_Packed (Ptyp) then
9014 return True;
9015 end if;
9017 -- We are in trouble if there is a component clause, and
9018 -- either we do not know the alignment of the slice, or
9019 -- the alignment of the slice is inconsistent with the
9020 -- bit position specified by the component clause.
9022 declare
9023 Field : constant Entity_Id := Entity (Selector_Name (Pref));
9024 begin
9025 if Present (Component_Clause (Field))
9026 and then
9027 (not Known_Alignment (Styp)
9028 or else
9029 (Component_Bit_Offset (Field) mod
9030 (System_Storage_Unit * Alignment (Styp))) /= 0)
9031 then
9032 return True;
9033 end if;
9034 end;
9036 -- For cases other than selected or indexed components we know we
9037 -- are OK, since no issues arise over alignment.
9039 else
9040 return False;
9041 end if;
9043 -- We processed an indexed component or selected component
9044 -- reference that looked safe, so keep checking prefixes.
9046 Pref := Prefix (Pref);
9047 end loop;
9048 end;
9049 end Is_Possibly_Unaligned_Slice;
9051 -------------------------------
9052 -- Is_Related_To_Func_Return --
9053 -------------------------------
9055 function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean is
9056 Expr : constant Node_Id := Related_Expression (Id);
9057 begin
9058 -- In the case of a function with a class-wide result that returns
9059 -- a call to a function with a specific result, we introduce a
9060 -- type conversion for the return expression. We do not want that
9061 -- type conversion to influence the result of this function.
9063 return
9064 Present (Expr)
9065 and then Nkind (Unqual_Conv (Expr)) = N_Explicit_Dereference
9066 and then (Nkind (Parent (Expr)) = N_Simple_Return_Statement
9067 or else
9068 (Nkind (Parent (Expr)) in N_Object_Declaration
9069 | N_Object_Renaming_Declaration
9070 and then
9071 Is_Return_Object (Defining_Entity (Parent (Expr)))));
9072 end Is_Related_To_Func_Return;
9074 --------------------------------
9075 -- Is_Ref_To_Bit_Packed_Array --
9076 --------------------------------
9078 function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
9079 Result : Boolean;
9080 Expr : Node_Id;
9082 begin
9083 if Is_Entity_Name (N)
9084 and then Is_Object (Entity (N))
9085 and then Present (Renamed_Object (Entity (N)))
9086 then
9087 return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
9088 end if;
9090 if Nkind (N) in N_Indexed_Component | N_Selected_Component then
9091 if Is_Bit_Packed_Array (Etype (Prefix (N))) then
9092 Result := True;
9093 else
9094 Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
9095 end if;
9097 if Result and then Nkind (N) = N_Indexed_Component then
9098 Expr := First (Expressions (N));
9099 while Present (Expr) loop
9100 Force_Evaluation (Expr);
9101 Next (Expr);
9102 end loop;
9103 end if;
9105 return Result;
9107 else
9108 return False;
9109 end if;
9110 end Is_Ref_To_Bit_Packed_Array;
9112 --------------------------------
9113 -- Is_Ref_To_Bit_Packed_Slice --
9114 --------------------------------
9116 function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
9117 begin
9118 if Nkind (N) = N_Type_Conversion then
9119 return Is_Ref_To_Bit_Packed_Slice (Expression (N));
9121 elsif Is_Entity_Name (N)
9122 and then Is_Object (Entity (N))
9123 and then Present (Renamed_Object (Entity (N)))
9124 then
9125 return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
9127 elsif Nkind (N) = N_Slice
9128 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
9129 then
9130 return True;
9132 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9133 return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
9135 else
9136 return False;
9137 end if;
9138 end Is_Ref_To_Bit_Packed_Slice;
9140 -----------------------
9141 -- Is_Renamed_Object --
9142 -----------------------
9144 function Is_Renamed_Object (N : Node_Id) return Boolean is
9145 Pnod : constant Node_Id := Parent (N);
9146 Kind : constant Node_Kind := Nkind (Pnod);
9147 begin
9148 if Kind = N_Object_Renaming_Declaration then
9149 return True;
9150 elsif Kind in N_Indexed_Component | N_Selected_Component then
9151 return Is_Renamed_Object (Pnod);
9152 else
9153 return False;
9154 end if;
9155 end Is_Renamed_Object;
9157 --------------------------------------
9158 -- Is_Secondary_Stack_BIP_Func_Call --
9159 --------------------------------------
9161 function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean is
9162 Actual : Node_Id;
9163 Call : Node_Id := Expr;
9164 Formal : Node_Id;
9165 Param : Node_Id;
9167 begin
9168 -- Build-in-place calls usually appear in 'reference format. Note that
9169 -- the accessibility check machinery may add an extra 'reference due to
9170 -- side effect removal.
9172 while Nkind (Call) = N_Reference loop
9173 Call := Prefix (Call);
9174 end loop;
9176 Call := Unqual_Conv (Call);
9178 if Is_Build_In_Place_Function_Call (Call) then
9180 -- Examine all parameter associations of the function call
9182 Param := First (Parameter_Associations (Call));
9183 while Present (Param) loop
9184 if Nkind (Param) = N_Parameter_Association then
9185 Formal := Selector_Name (Param);
9186 Actual := Explicit_Actual_Parameter (Param);
9188 -- A match for BIPalloc => 2 has been found
9190 if Is_Build_In_Place_Entity (Formal)
9191 and then BIP_Suffix_Kind (Formal) = BIP_Alloc_Form
9192 and then Nkind (Actual) = N_Integer_Literal
9193 and then Intval (Actual) = Uint_2
9194 then
9195 return True;
9196 end if;
9197 end if;
9199 Next (Param);
9200 end loop;
9201 end if;
9203 return False;
9204 end Is_Secondary_Stack_BIP_Func_Call;
9206 ------------------------------
9207 -- Is_Secondary_Stack_Thunk --
9208 ------------------------------
9210 function Is_Secondary_Stack_Thunk (Id : Entity_Id) return Boolean is
9211 begin
9212 return Ekind (Id) = E_Function
9213 and then Is_Thunk (Id)
9214 and then Has_Controlling_Result (Id);
9215 end Is_Secondary_Stack_Thunk;
9217 --------------------------------
9218 -- Is_Uninitialized_Aggregate --
9219 --------------------------------
9221 function Is_Uninitialized_Aggregate
9222 (Exp : Node_Id;
9223 T : Entity_Id) return Boolean
9225 Comp : Node_Id;
9226 Comp_Type : Entity_Id;
9227 Typ : Entity_Id;
9229 begin
9230 if Nkind (Exp) /= N_Aggregate then
9231 return False;
9232 end if;
9234 Preanalyze_And_Resolve (Exp, T);
9235 Typ := Etype (Exp);
9237 if No (Typ)
9238 or else Ekind (Typ) /= E_Array_Subtype
9239 or else Present (Expressions (Exp))
9240 or else No (Component_Associations (Exp))
9241 then
9242 return False;
9243 else
9244 Comp_Type := Component_Type (Typ);
9245 Comp := First (Component_Associations (Exp));
9247 if not Box_Present (Comp)
9248 or else Present (Next (Comp))
9249 then
9250 return False;
9251 end if;
9253 return Is_Scalar_Type (Comp_Type)
9254 and then No (Default_Aspect_Component_Value (Typ));
9255 end if;
9256 end Is_Uninitialized_Aggregate;
9258 ----------------------------
9259 -- Is_Untagged_Derivation --
9260 ----------------------------
9262 function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
9263 begin
9264 return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
9265 or else
9266 (Is_Private_Type (T) and then Present (Full_View (T))
9267 and then not Is_Tagged_Type (Full_View (T))
9268 and then Is_Derived_Type (Full_View (T))
9269 and then Etype (Full_View (T)) /= T);
9270 end Is_Untagged_Derivation;
9272 ------------------------------------
9273 -- Is_Untagged_Private_Derivation --
9274 ------------------------------------
9276 function Is_Untagged_Private_Derivation
9277 (Priv_Typ : Entity_Id;
9278 Full_Typ : Entity_Id) return Boolean
9280 begin
9281 return
9282 Present (Priv_Typ)
9283 and then Is_Untagged_Derivation (Priv_Typ)
9284 and then Is_Private_Type (Etype (Priv_Typ))
9285 and then Present (Full_Typ)
9286 and then Is_Itype (Full_Typ);
9287 end Is_Untagged_Private_Derivation;
9289 ------------------------------
9290 -- Is_Verifiable_DIC_Pragma --
9291 ------------------------------
9293 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
9294 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9296 begin
9297 -- To qualify as verifiable, a DIC pragma must have a non-null argument
9299 return
9300 Present (Args)
9302 -- If there are args, but the first arg is Empty, then treat the
9303 -- pragma the same as having no args (there may be a second arg that
9304 -- is an implicitly added type arg, and Empty is a placeholder).
9306 and then Present (Get_Pragma_Arg (First (Args)))
9308 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
9309 end Is_Verifiable_DIC_Pragma;
9311 ---------------------------
9312 -- Is_Volatile_Reference --
9313 ---------------------------
9315 function Is_Volatile_Reference (N : Node_Id) return Boolean is
9316 begin
9317 -- Only source references are to be treated as volatile, internally
9318 -- generated stuff cannot have volatile external effects.
9320 if not Comes_From_Source (N) then
9321 return False;
9323 -- Never true for reference to a type
9325 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
9326 return False;
9328 -- Never true for a compile time known constant
9330 elsif Compile_Time_Known_Value (N) then
9331 return False;
9333 -- True if object reference with volatile type
9335 elsif Is_Volatile_Object_Ref (N) then
9336 return True;
9338 -- True if reference to volatile entity
9340 elsif Is_Entity_Name (N) then
9341 return Treat_As_Volatile (Entity (N));
9343 -- True for slice of volatile array
9345 elsif Nkind (N) = N_Slice then
9346 return Is_Volatile_Reference (Prefix (N));
9348 -- True if volatile component
9350 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
9351 if (Is_Entity_Name (Prefix (N))
9352 and then Has_Volatile_Components (Entity (Prefix (N))))
9353 or else (Present (Etype (Prefix (N)))
9354 and then Has_Volatile_Components (Etype (Prefix (N))))
9355 then
9356 return True;
9357 else
9358 return Is_Volatile_Reference (Prefix (N));
9359 end if;
9361 -- Otherwise false
9363 else
9364 return False;
9365 end if;
9366 end Is_Volatile_Reference;
9368 --------------------
9369 -- Kill_Dead_Code --
9370 --------------------
9372 procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
9373 W : Boolean := Warn;
9374 -- Set False if warnings suppressed
9376 begin
9377 if Present (N) then
9378 Remove_Warning_Messages (N);
9380 -- Update the internal structures of the ABE mechanism in case the
9381 -- dead node is an elaboration scenario.
9383 Kill_Elaboration_Scenario (N);
9385 -- Generate warning if appropriate
9387 if W then
9389 -- We suppress the warning if this code is under control of an
9390 -- if/case statement and either
9391 -- a) we are in an instance and the condition/selector
9392 -- has a statically known value; or
9393 -- b) the condition/selector is a simple identifier and
9394 -- warnings off is set for this identifier.
9395 -- Dead code is common and reasonable in instances, so we don't
9396 -- want a warning in that case.
9398 declare
9399 C : Node_Id := Empty;
9400 begin
9401 if Nkind (Parent (N)) = N_If_Statement then
9402 C := Condition (Parent (N));
9403 elsif Nkind (Parent (N)) = N_Case_Statement_Alternative then
9404 C := Expression (Parent (Parent (N)));
9405 end if;
9407 if Present (C) then
9408 if (In_Instance and Compile_Time_Known_Value (C))
9409 or else (Nkind (C) = N_Identifier
9410 and then Present (Entity (C))
9411 and then Has_Warnings_Off (Entity (C)))
9412 then
9413 W := False;
9414 end if;
9415 end if;
9416 end;
9418 -- Generate warning if not suppressed
9420 if W then
9421 Error_Msg_F
9422 ("?t?this code can never be executed and has been deleted!",
9424 end if;
9425 end if;
9427 -- Recurse into block statements and bodies to process declarations
9428 -- and statements.
9430 if Nkind (N) = N_Block_Statement
9431 or else Nkind (N) = N_Subprogram_Body
9432 or else Nkind (N) = N_Package_Body
9433 then
9434 Kill_Dead_Code (Declarations (N), False);
9435 Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
9437 if Nkind (N) = N_Subprogram_Body then
9438 Set_Is_Eliminated (Defining_Entity (N));
9439 end if;
9441 elsif Nkind (N) = N_Package_Declaration then
9442 Kill_Dead_Code (Visible_Declarations (Specification (N)));
9443 Kill_Dead_Code (Private_Declarations (Specification (N)));
9445 -- ??? After this point, Delete_Tree has been called on all
9446 -- declarations in Specification (N), so references to entities
9447 -- therein look suspicious.
9449 declare
9450 E : Entity_Id := First_Entity (Defining_Entity (N));
9452 begin
9453 while Present (E) loop
9454 if Ekind (E) = E_Operator then
9455 Set_Is_Eliminated (E);
9456 end if;
9458 Next_Entity (E);
9459 end loop;
9460 end;
9462 -- Recurse into composite statement to kill individual statements in
9463 -- particular instantiations.
9465 elsif Nkind (N) = N_If_Statement then
9466 Kill_Dead_Code (Then_Statements (N));
9467 Kill_Dead_Code (Elsif_Parts (N));
9468 Kill_Dead_Code (Else_Statements (N));
9470 elsif Nkind (N) = N_Loop_Statement then
9471 Kill_Dead_Code (Statements (N));
9473 elsif Nkind (N) = N_Case_Statement then
9474 declare
9475 Alt : Node_Id;
9476 begin
9477 Alt := First (Alternatives (N));
9478 while Present (Alt) loop
9479 Kill_Dead_Code (Statements (Alt));
9480 Next (Alt);
9481 end loop;
9482 end;
9484 elsif Nkind (N) = N_Case_Statement_Alternative then
9485 Kill_Dead_Code (Statements (N));
9487 -- Deal with dead instances caused by deleting instantiations
9489 elsif Nkind (N) in N_Generic_Instantiation then
9490 Remove_Dead_Instance (N);
9491 end if;
9492 end if;
9493 end Kill_Dead_Code;
9495 -- Case where argument is a list of nodes to be killed
9497 procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
9498 N : Node_Id;
9499 W : Boolean;
9501 begin
9502 W := Warn;
9504 N := First (L);
9505 while Present (N) loop
9506 Kill_Dead_Code (N, W);
9507 W := False;
9508 Next (N);
9509 end loop;
9510 end Kill_Dead_Code;
9512 -----------------------------
9513 -- Make_CW_Equivalent_Type --
9514 -----------------------------
9516 -- Create a record type used as an equivalent of any member of the class
9517 -- which takes its size from exp.
9519 -- Generate the following code:
9521 -- type Equiv_T is record
9522 -- _parent : T (List of discriminant constraints taken from Exp);
9523 -- Cnn : Storage_Array (1 .. (Exp'size - Typ'object_size)/Storage_Unit);
9524 -- end Equiv_T;
9526 -- Note that this type does not guarantee same alignment as all derived
9527 -- types.
9529 -- Note: for the freezing circuitry, this looks like a record extension,
9530 -- and so we need to make sure that the scalar storage order is the same
9531 -- as that of the parent type. (This does not change anything for the
9532 -- representation of the extension part.)
9534 function Make_CW_Equivalent_Type
9535 (T : Entity_Id;
9536 E : Node_Id) return Entity_Id
9538 Loc : constant Source_Ptr := Sloc (E);
9539 Root_Typ : constant Entity_Id := Root_Type (T);
9540 Root_Utyp : constant Entity_Id := Underlying_Type (Root_Typ);
9541 List_Def : constant List_Id := Empty_List;
9542 Comp_List : constant List_Id := New_List;
9544 Equiv_Type : Entity_Id;
9545 Range_Type : Entity_Id;
9546 Str_Type : Entity_Id;
9547 Constr_Root : Entity_Id;
9548 Size_Attr : Node_Id;
9549 Size_Expr : Node_Id;
9551 begin
9552 -- If the root type is already constrained, there are no discriminants
9553 -- in the expression.
9555 if not Has_Discriminants (Root_Typ)
9556 or else Is_Constrained (Root_Typ)
9557 then
9558 Constr_Root := Root_Typ;
9560 -- At this point in the expansion, nonlimited view of the type
9561 -- must be available, otherwise the error will be reported later.
9563 if From_Limited_With (Constr_Root)
9564 and then Present (Non_Limited_View (Constr_Root))
9565 then
9566 Constr_Root := Non_Limited_View (Constr_Root);
9567 end if;
9569 else
9570 Constr_Root := Make_Temporary (Loc, 'R');
9572 -- subtype cstr__n is T (List of discr constraints taken from Exp)
9574 Append_To (List_Def,
9575 Make_Subtype_Declaration (Loc,
9576 Defining_Identifier => Constr_Root,
9577 Subtype_Indication => Make_Subtype_From_Expr (E, Root_Typ)));
9578 end if;
9580 -- Generate the range subtype declaration
9582 Range_Type := Make_Temporary (Loc, 'G');
9584 -- If the expression is known to have the tag of its type, then we can
9585 -- use it directly for the prefix of the Size attribute; otherwise we
9586 -- need to convert it first to the class-wide type to force a call to
9587 -- the _Size primitive operation.
9589 if Has_Tag_Of_Type (E) then
9590 if not Has_Discriminants (Etype (E))
9591 or else Is_Constrained (Etype (E))
9592 then
9593 Size_Attr :=
9594 Make_Attribute_Reference (Loc,
9595 Prefix => New_Occurrence_Of (Etype (E), Loc),
9596 Attribute_Name => Name_Object_Size);
9598 else
9599 Size_Attr :=
9600 Make_Attribute_Reference (Loc,
9601 Prefix => Duplicate_Subexpr_No_Checks (E),
9602 Attribute_Name => Name_Size);
9603 end if;
9605 else
9606 Size_Attr :=
9607 Make_Attribute_Reference (Loc,
9608 Prefix => OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
9609 Attribute_Name => Name_Size);
9610 end if;
9612 if not Is_Interface (Root_Typ) then
9614 -- subtype rg__xx is
9615 -- Storage_Offset range 1 .. (Exp'size - Typ'object_size)
9616 -- / Storage_Unit
9618 Size_Expr :=
9619 Make_Op_Subtract (Loc,
9620 Left_Opnd => Size_Attr,
9621 Right_Opnd =>
9622 Make_Attribute_Reference (Loc,
9623 Prefix => New_Occurrence_Of (Constr_Root, Loc),
9624 Attribute_Name => Name_Object_Size));
9625 else
9626 -- subtype rg__xx is
9627 -- Storage_Offset range 1 .. (Exp'size - Ada.Tags.Tag'object_size)
9628 -- / Storage_Unit
9630 Size_Expr :=
9631 Make_Op_Subtract (Loc,
9632 Left_Opnd => Size_Attr,
9633 Right_Opnd =>
9634 Make_Attribute_Reference (Loc,
9635 Prefix => New_Occurrence_Of (RTE (RE_Tag), Loc),
9636 Attribute_Name => Name_Object_Size));
9637 end if;
9639 Set_Paren_Count (Size_Expr, 1);
9641 Append_To (List_Def,
9642 Make_Subtype_Declaration (Loc,
9643 Defining_Identifier => Range_Type,
9644 Subtype_Indication =>
9645 Make_Subtype_Indication (Loc,
9646 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Offset), Loc),
9647 Constraint => Make_Range_Constraint (Loc,
9648 Range_Expression =>
9649 Make_Range (Loc,
9650 Low_Bound => Make_Integer_Literal (Loc, 1),
9651 High_Bound =>
9652 Make_Op_Divide (Loc,
9653 Left_Opnd => Size_Expr,
9654 Right_Opnd => Make_Integer_Literal (Loc,
9655 Intval => System_Storage_Unit)))))));
9657 -- subtype str__nn is Storage_Array (rg__x);
9659 Str_Type := Make_Temporary (Loc, 'S');
9660 Append_To (List_Def,
9661 Make_Subtype_Declaration (Loc,
9662 Defining_Identifier => Str_Type,
9663 Subtype_Indication =>
9664 Make_Subtype_Indication (Loc,
9665 Subtype_Mark => New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9666 Constraint =>
9667 Make_Index_Or_Discriminant_Constraint (Loc,
9668 Constraints =>
9669 New_List (New_Occurrence_Of (Range_Type, Loc))))));
9671 -- type Equiv_T is record
9672 -- _Parent : Snn; -- not interface
9673 -- _Tag : Ada.Tags.Tag -- interface
9674 -- Cnn : Str_Type;
9675 -- end Equiv_T;
9677 Equiv_Type := Make_Temporary (Loc, 'T');
9678 Mutate_Ekind (Equiv_Type, E_Record_Type);
9680 if not Is_Interface (Root_Typ) then
9681 Set_Parent_Subtype (Equiv_Type, Constr_Root);
9682 end if;
9684 -- Set Is_Class_Wide_Equivalent_Type very early to trigger the special
9685 -- treatment for this type. In particular, even though _parent's type
9686 -- is a controlled type or contains controlled components, we do not
9687 -- want to set Has_Controlled_Component on it to avoid making it gain
9688 -- an unwanted _controller component.
9690 Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
9692 -- A class-wide equivalent type does not require initialization
9694 Set_Suppress_Initialization (Equiv_Type);
9696 if not Is_Interface (Root_Typ) then
9697 Append_To (Comp_List,
9698 Make_Component_Declaration (Loc,
9699 Defining_Identifier =>
9700 Make_Defining_Identifier (Loc, Name_uParent),
9701 Component_Definition =>
9702 Make_Component_Definition (Loc,
9703 Aliased_Present => False,
9704 Subtype_Indication => New_Occurrence_Of (Constr_Root, Loc))));
9706 Set_Reverse_Storage_Order
9707 (Equiv_Type, Reverse_Storage_Order (Base_Type (Root_Utyp)));
9708 Set_Reverse_Bit_Order
9709 (Equiv_Type, Reverse_Bit_Order (Base_Type (Root_Utyp)));
9711 else
9712 Append_To (Comp_List,
9713 Make_Component_Declaration (Loc,
9714 Defining_Identifier =>
9715 Make_Defining_Identifier (Loc, Name_uTag),
9716 Component_Definition =>
9717 Make_Component_Definition (Loc,
9718 Aliased_Present => False,
9719 Subtype_Indication =>
9720 New_Occurrence_Of (RTE (RE_Tag), Loc))));
9721 end if;
9723 Append_To (Comp_List,
9724 Make_Component_Declaration (Loc,
9725 Defining_Identifier => Make_Temporary (Loc, 'C'),
9726 Component_Definition =>
9727 Make_Component_Definition (Loc,
9728 Aliased_Present => False,
9729 Subtype_Indication => New_Occurrence_Of (Str_Type, Loc))));
9731 Append_To (List_Def,
9732 Make_Full_Type_Declaration (Loc,
9733 Defining_Identifier => Equiv_Type,
9734 Type_Definition =>
9735 Make_Record_Definition (Loc,
9736 Component_List =>
9737 Make_Component_List (Loc,
9738 Component_Items => Comp_List,
9739 Variant_Part => Empty))));
9741 -- Suppress all checks during the analysis of the expanded code to avoid
9742 -- the generation of spurious warnings under ZFP run-time.
9744 Insert_Actions (E, List_Def, Suppress => All_Checks);
9746 -- In the case of an interface type mark the tag for First_Tag_Component
9748 if Is_Interface (Root_Typ) then
9749 Set_Is_Tag (First_Entity (Equiv_Type));
9750 end if;
9752 return Equiv_Type;
9753 end Make_CW_Equivalent_Type;
9755 -------------------------
9756 -- Make_Invariant_Call --
9757 -------------------------
9759 function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
9760 Loc : constant Source_Ptr := Sloc (Expr);
9761 Typ : constant Entity_Id := Base_Type (Etype (Expr));
9762 pragma Assert (Has_Invariants (Typ));
9763 Proc_Id : constant Entity_Id := Invariant_Procedure (Typ);
9764 pragma Assert (Present (Proc_Id));
9765 begin
9766 -- The invariant procedure has a null body if assertions are disabled or
9767 -- Assertion_Policy Ignore is in effect. In that case, generate a null
9768 -- statement instead of a call to the invariant procedure.
9770 if Has_Null_Body (Proc_Id) then
9771 return Make_Null_Statement (Loc);
9772 else
9773 return
9774 Make_Procedure_Call_Statement (Loc,
9775 Name => New_Occurrence_Of (Proc_Id, Loc),
9776 Parameter_Associations => New_List (Relocate_Node (Expr)));
9777 end if;
9778 end Make_Invariant_Call;
9780 ------------------------
9781 -- Make_Literal_Range --
9782 ------------------------
9784 function Make_Literal_Range
9785 (Loc : Source_Ptr;
9786 Literal_Typ : Entity_Id) return Node_Id
9788 Lo : constant Node_Id :=
9789 New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
9790 Index : constant Entity_Id := Etype (Lo);
9791 Length_Expr : constant Node_Id :=
9792 Make_Op_Subtract (Loc,
9793 Left_Opnd =>
9794 Make_Integer_Literal (Loc,
9795 Intval => String_Literal_Length (Literal_Typ)),
9796 Right_Opnd => Make_Integer_Literal (Loc, 1));
9798 Hi : Node_Id;
9800 begin
9801 Set_Analyzed (Lo, False);
9803 if Is_Integer_Type (Index) then
9804 Hi :=
9805 Make_Op_Add (Loc,
9806 Left_Opnd => New_Copy_Tree (Lo),
9807 Right_Opnd => Length_Expr);
9808 else
9809 Hi :=
9810 Make_Attribute_Reference (Loc,
9811 Attribute_Name => Name_Val,
9812 Prefix => New_Occurrence_Of (Index, Loc),
9813 Expressions => New_List (
9814 Make_Op_Add (Loc,
9815 Left_Opnd =>
9816 Make_Attribute_Reference (Loc,
9817 Attribute_Name => Name_Pos,
9818 Prefix => New_Occurrence_Of (Index, Loc),
9819 Expressions => New_List (New_Copy_Tree (Lo))),
9820 Right_Opnd => Length_Expr)));
9821 end if;
9823 return
9824 Make_Range (Loc,
9825 Low_Bound => Lo,
9826 High_Bound => Hi);
9827 end Make_Literal_Range;
9829 --------------------------
9830 -- Make_Non_Empty_Check --
9831 --------------------------
9833 function Make_Non_Empty_Check
9834 (Loc : Source_Ptr;
9835 N : Node_Id) return Node_Id
9837 begin
9838 return
9839 Make_Op_Ne (Loc,
9840 Left_Opnd =>
9841 Make_Attribute_Reference (Loc,
9842 Attribute_Name => Name_Length,
9843 Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
9844 Right_Opnd =>
9845 Make_Integer_Literal (Loc, 0));
9846 end Make_Non_Empty_Check;
9848 -------------------------
9849 -- Make_Predicate_Call --
9850 -------------------------
9852 -- WARNING: This routine manages Ghost regions. Return statements must be
9853 -- replaced by gotos which jump to the end of the routine and restore the
9854 -- Ghost mode.
9856 function Make_Predicate_Call
9857 (Typ : Entity_Id;
9858 Expr : Node_Id;
9859 Static_Mem : Boolean := False;
9860 Dynamic_Mem : Node_Id := Empty) return Node_Id
9862 Loc : constant Source_Ptr := Sloc (Expr);
9864 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9865 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9866 -- Save the Ghost-related attributes to restore on exit
9868 Call : Node_Id;
9869 Func_Id : Entity_Id;
9870 Param_Assocs : List_Id;
9871 begin
9872 Func_Id := Predicate_Function (Typ);
9873 pragma Assert (Present (Func_Id));
9875 -- The related type may be subject to pragma Ghost. Set the mode now to
9876 -- ensure that the call is properly marked as Ghost.
9878 Set_Ghost_Mode (Typ);
9880 -- Case of calling normal predicate function
9882 -- If the type is tagged, the expression may be class-wide, in which
9883 -- case it has to be converted to its root type, given that the
9884 -- generated predicate function is not dispatching. The conversion is
9885 -- type-safe and does not need validation, which matters when private
9886 -- extensions are involved.
9888 if Is_Tagged_Type (Typ) then
9889 Param_Assocs := New_List (OK_Convert_To (Typ, Relocate_Node (Expr)));
9890 else
9891 Param_Assocs := New_List (Relocate_Node (Expr));
9892 end if;
9894 if Predicate_Function_Needs_Membership_Parameter (Typ) then
9895 -- Pass in parameter indicating whether this call is for a
9896 -- membership test.
9897 Append ((if Present (Dynamic_Mem)
9898 then Dynamic_Mem
9899 else New_Occurrence_Of
9900 (Boolean_Literals (Static_Mem), Loc)),
9901 Param_Assocs);
9902 end if;
9904 Call :=
9905 Make_Function_Call (Loc,
9906 Name => New_Occurrence_Of (Func_Id, Loc),
9907 Parameter_Associations => Param_Assocs);
9909 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9911 return Call;
9912 end Make_Predicate_Call;
9914 --------------------------
9915 -- Make_Predicate_Check --
9916 --------------------------
9918 function Make_Predicate_Check
9919 (Typ : Entity_Id;
9920 Expr : Node_Id) return Node_Id
9922 Loc : constant Source_Ptr := Sloc (Expr);
9924 -- Local variables
9926 Args : List_Id;
9927 Nam : Name_Id;
9929 -- Start of processing for Make_Predicate_Check
9931 begin
9932 -- If predicate checks are suppressed, then return a null statement. For
9933 -- this call, we check only the scope setting. If the caller wants to
9934 -- check a specific entity's setting, they must do it manually.
9936 if Predicate_Checks_Suppressed (Empty) then
9937 return Make_Null_Statement (Loc);
9938 end if;
9940 -- Do not generate a check within stream functions and the like.
9942 if not Predicate_Check_In_Scope (Expr) then
9943 return Make_Null_Statement (Loc);
9944 end if;
9946 -- Compute proper name to use, we need to get this right so that the
9947 -- right set of check policies apply to the Check pragma we are making.
9949 if Has_Dynamic_Predicate_Aspect (Typ) then
9950 Nam := Name_Dynamic_Predicate;
9951 elsif Has_Static_Predicate_Aspect (Typ) then
9952 Nam := Name_Static_Predicate;
9953 else
9954 Nam := Name_Predicate;
9955 end if;
9957 Args := New_List (
9958 Make_Pragma_Argument_Association (Loc,
9959 Expression => Make_Identifier (Loc, Nam)),
9960 Make_Pragma_Argument_Association (Loc,
9961 Expression => Make_Predicate_Call (Typ, Expr)));
9963 -- If the subtype is subject to pragma Predicate_Failure, add the
9964 -- failure expression as an additional parameter.
9966 return
9967 Make_Pragma (Loc,
9968 Chars => Name_Check,
9969 Pragma_Argument_Associations => Args);
9970 end Make_Predicate_Check;
9972 ----------------------------
9973 -- Make_Subtype_From_Expr --
9974 ----------------------------
9976 -- 1. If Expr is an unconstrained array expression, creates
9977 -- Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
9979 -- 2. If Expr is a unconstrained discriminated type expression, creates
9980 -- Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
9982 -- 3. If Expr is class-wide, creates an implicit class-wide subtype
9984 function Make_Subtype_From_Expr
9985 (E : Node_Id;
9986 Unc_Typ : Entity_Id;
9987 Related_Id : Entity_Id := Empty) return Node_Id
9989 List_Constr : constant List_Id := New_List;
9990 Loc : constant Source_Ptr := Sloc (E);
9991 D : Entity_Id;
9992 Full_Exp : Node_Id;
9993 Full_Subtyp : Entity_Id;
9994 High_Bound : Entity_Id;
9995 Index_Typ : Entity_Id;
9996 Low_Bound : Entity_Id;
9997 Priv_Subtyp : Entity_Id;
9998 Utyp : Entity_Id;
10000 begin
10001 if Is_Private_Type (Unc_Typ)
10002 and then Has_Unknown_Discriminants (Unc_Typ)
10003 then
10004 -- The caller requests a unique external name for both the private
10005 -- and the full subtype.
10007 if Present (Related_Id) then
10008 Full_Subtyp :=
10009 Make_Defining_Identifier (Loc,
10010 Chars => New_External_Name (Chars (Related_Id), 'C'));
10011 Priv_Subtyp :=
10012 Make_Defining_Identifier (Loc,
10013 Chars => New_External_Name (Chars (Related_Id), 'P'));
10015 else
10016 Full_Subtyp := Make_Temporary (Loc, 'C');
10017 Priv_Subtyp := Make_Temporary (Loc, 'P');
10018 end if;
10020 -- Prepare the subtype completion. Use the base type to find the
10021 -- underlying type because the type may be a generic actual or an
10022 -- explicit subtype.
10024 Utyp := Underlying_Type (Base_Type (Unc_Typ));
10026 Full_Exp :=
10027 Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
10028 Set_Parent (Full_Exp, Parent (E));
10030 Insert_Action (E,
10031 Make_Subtype_Declaration (Loc,
10032 Defining_Identifier => Full_Subtyp,
10033 Subtype_Indication => Make_Subtype_From_Expr (Full_Exp, Utyp)));
10035 -- Define the dummy private subtype
10037 Mutate_Ekind (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
10038 Set_Etype (Priv_Subtyp, Base_Type (Unc_Typ));
10039 Set_Scope (Priv_Subtyp, Full_Subtyp);
10040 Set_Is_Constrained (Priv_Subtyp);
10041 Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
10042 Set_Is_Itype (Priv_Subtyp);
10043 Set_Associated_Node_For_Itype (Priv_Subtyp, E);
10045 if Is_Tagged_Type (Priv_Subtyp) then
10046 Set_Class_Wide_Type
10047 (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
10048 Set_Direct_Primitive_Operations (Priv_Subtyp,
10049 Direct_Primitive_Operations (Unc_Typ));
10050 end if;
10052 Set_Full_View (Priv_Subtyp, Full_Subtyp);
10054 return New_Occurrence_Of (Priv_Subtyp, Loc);
10056 elsif Is_Array_Type (Unc_Typ) then
10057 Index_Typ := First_Index (Unc_Typ);
10058 for J in 1 .. Number_Dimensions (Unc_Typ) loop
10060 -- Capture the bounds of each index constraint in case the context
10061 -- is an object declaration of an unconstrained type initialized
10062 -- by a function call:
10064 -- Obj : Unconstr_Typ := Func_Call;
10066 -- This scenario requires secondary scope management and the index
10067 -- constraint cannot depend on the temporary used to capture the
10068 -- result of the function call.
10070 -- SS_Mark;
10071 -- Temp : Unconstr_Typ_Ptr := Func_Call'reference;
10072 -- subtype S is Unconstr_Typ (Temp.all'First .. Temp.all'Last);
10073 -- Obj : S := Temp.all;
10074 -- SS_Release; -- Temp is gone at this point, bounds of S are
10075 -- -- non existent.
10077 -- Generate:
10078 -- Low_Bound : constant Base_Type (Index_Typ) := E'First (J);
10080 Low_Bound := Make_Temporary (Loc, 'B');
10081 Insert_Action (E,
10082 Make_Object_Declaration (Loc,
10083 Defining_Identifier => Low_Bound,
10084 Object_Definition =>
10085 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10086 Constant_Present => True,
10087 Expression =>
10088 Make_Attribute_Reference (Loc,
10089 Prefix => Duplicate_Subexpr_No_Checks (E),
10090 Attribute_Name => Name_First,
10091 Expressions => New_List (
10092 Make_Integer_Literal (Loc, J)))));
10094 -- Generate:
10095 -- High_Bound : constant Base_Type (Index_Typ) := E'Last (J);
10097 High_Bound := Make_Temporary (Loc, 'B');
10098 Insert_Action (E,
10099 Make_Object_Declaration (Loc,
10100 Defining_Identifier => High_Bound,
10101 Object_Definition =>
10102 New_Occurrence_Of (Base_Type (Etype (Index_Typ)), Loc),
10103 Constant_Present => True,
10104 Expression =>
10105 Make_Attribute_Reference (Loc,
10106 Prefix => Duplicate_Subexpr_No_Checks (E),
10107 Attribute_Name => Name_Last,
10108 Expressions => New_List (
10109 Make_Integer_Literal (Loc, J)))));
10111 Append_To (List_Constr,
10112 Make_Range (Loc,
10113 Low_Bound => New_Occurrence_Of (Low_Bound, Loc),
10114 High_Bound => New_Occurrence_Of (High_Bound, Loc)));
10116 Next_Index (Index_Typ);
10117 end loop;
10119 elsif Is_Class_Wide_Type (Unc_Typ) then
10120 declare
10121 CW_Subtype : constant Entity_Id :=
10122 New_Class_Wide_Subtype (Unc_Typ, E);
10124 begin
10125 -- A class-wide equivalent type is not needed on VM targets
10126 -- because the VM back-ends handle the class-wide object
10127 -- initialization itself (and doesn't need or want the
10128 -- additional intermediate type to handle the assignment).
10130 if Expander_Active and then Tagged_Type_Expansion then
10132 -- If this is the class-wide type of a completion that is a
10133 -- record subtype, set the type of the class-wide type to be
10134 -- the full base type, for use in the expanded code for the
10135 -- equivalent type. Should this be done earlier when the
10136 -- completion is analyzed ???
10138 if Is_Private_Type (Etype (Unc_Typ))
10139 and then
10140 Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
10141 then
10142 Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
10143 end if;
10145 Set_Equivalent_Type
10146 (CW_Subtype, Make_CW_Equivalent_Type (Unc_Typ, E));
10147 end if;
10149 Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
10151 return New_Occurrence_Of (CW_Subtype, Loc);
10152 end;
10154 -- Indefinite record type with discriminants
10156 else
10157 D := First_Discriminant (Unc_Typ);
10158 while Present (D) loop
10159 Append_To (List_Constr,
10160 Make_Selected_Component (Loc,
10161 Prefix => Duplicate_Subexpr_No_Checks (E),
10162 Selector_Name => New_Occurrence_Of (D, Loc)));
10164 Next_Discriminant (D);
10165 end loop;
10166 end if;
10168 return
10169 Make_Subtype_Indication (Loc,
10170 Subtype_Mark => New_Occurrence_Of (Unc_Typ, Loc),
10171 Constraint =>
10172 Make_Index_Or_Discriminant_Constraint (Loc,
10173 Constraints => List_Constr));
10174 end Make_Subtype_From_Expr;
10176 -----------------------------
10177 -- Make_Variant_Comparison --
10178 -----------------------------
10180 function Make_Variant_Comparison
10181 (Loc : Source_Ptr;
10182 Typ : Entity_Id;
10183 Mode : Name_Id;
10184 Curr_Val : Node_Id;
10185 Old_Val : Node_Id) return Node_Id
10187 function Big_Integer_Lt return Entity_Id;
10188 -- Returns the entity of the predefined "<" function from
10189 -- Ada.Numerics.Big_Numbers.Big_Integers.
10191 --------------------
10192 -- Big_Integer_Lt --
10193 --------------------
10195 function Big_Integer_Lt return Entity_Id is
10196 Big_Integers : constant Entity_Id :=
10197 RTU_Entity (Ada_Numerics_Big_Numbers_Big_Integers);
10199 E : Entity_Id := First_Entity (Big_Integers);
10201 begin
10202 while Present (E) loop
10203 if Chars (E) = Name_Op_Lt then
10204 return E;
10205 end if;
10206 Next_Entity (E);
10207 end loop;
10209 raise Program_Error;
10210 end Big_Integer_Lt;
10212 -- Start of processing for Make_Variant_Comparison
10214 begin
10215 if Mode = Name_Increases then
10216 return Make_Op_Gt (Loc, Curr_Val, Old_Val);
10218 else pragma Assert (Mode = Name_Decreases);
10220 -- For discrete expressions use the "<" operator
10222 if Is_Discrete_Type (Typ) then
10223 return Make_Op_Lt (Loc, Curr_Val, Old_Val);
10225 -- For Big_Integer expressions use the "<" function, because the
10226 -- operator on private type might not be visible and won't be
10227 -- resolved.
10229 else pragma Assert (Is_RTE (Base_Type (Typ), RE_Big_Integer));
10230 return
10231 Make_Function_Call (Loc,
10232 Name =>
10233 New_Occurrence_Of (Big_Integer_Lt, Loc),
10234 Parameter_Associations =>
10235 New_List (Curr_Val, Old_Val));
10236 end if;
10237 end if;
10238 end Make_Variant_Comparison;
10240 -----------------
10241 -- Map_Formals --
10242 -----------------
10244 procedure Map_Formals
10245 (Parent_Subp : Entity_Id;
10246 Derived_Subp : Entity_Id;
10247 Force_Update : Boolean := False)
10249 Par_Formal : Entity_Id := First_Formal (Parent_Subp);
10250 Subp_Formal : Entity_Id := First_Formal (Derived_Subp);
10252 begin
10253 if Force_Update then
10254 Type_Map.Set (Parent_Subp, Derived_Subp);
10255 end if;
10257 -- At this stage either we are under regular processing and the caller
10258 -- has previously ensured that these primitives are already mapped (by
10259 -- means of calling previously to Update_Primitives_Mapping), or we are
10260 -- processing a late-overriding primitive and Force_Update updated above
10261 -- the mapping of these primitives.
10263 while Present (Par_Formal) and then Present (Subp_Formal) loop
10264 Type_Map.Set (Par_Formal, Subp_Formal);
10265 Next_Formal (Par_Formal);
10266 Next_Formal (Subp_Formal);
10267 end loop;
10268 end Map_Formals;
10270 ---------------
10271 -- Map_Types --
10272 ---------------
10274 procedure Map_Types (Parent_Type : Entity_Id; Derived_Type : Entity_Id) is
10276 -- NOTE: Most of the routines in Map_Types are intentionally unnested to
10277 -- avoid deep indentation of code.
10279 -- NOTE: Routines which deal with discriminant mapping operate on the
10280 -- [underlying/record] full view of various types because those views
10281 -- contain all discriminants and stored constraints.
10283 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id);
10284 -- Subsidiary to Map_Primitives. Find a primitive in the inheritance or
10285 -- overriding chain starting from Prim whose dispatching type is parent
10286 -- type Par_Typ and add a mapping between the result and primitive Prim.
10288 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id;
10289 -- Subsidiary to Map_Primitives. Return the next ancestor primitive in
10290 -- the inheritance or overriding chain of subprogram Subp. Return Empty
10291 -- if no such primitive is available.
10293 function Build_Chain
10294 (Par_Typ : Entity_Id;
10295 Deriv_Typ : Entity_Id) return Elist_Id;
10296 -- Subsidiary to Map_Discriminants. Recreate the derivation chain from
10297 -- parent type Par_Typ leading down towards derived type Deriv_Typ. The
10298 -- list has the form:
10300 -- head tail
10301 -- v v
10302 -- <Ancestor_N> -> <Ancestor_N-1> -> <Ancestor_1> -> Deriv_Typ
10304 -- Note that Par_Typ is not part of the resulting derivation chain
10306 function Discriminated_View (Typ : Entity_Id) return Entity_Id;
10307 -- Return the view of type Typ which could potentially contains either
10308 -- the discriminants or stored constraints of the type.
10310 function Find_Discriminant_Value
10311 (Discr : Entity_Id;
10312 Par_Typ : Entity_Id;
10313 Deriv_Typ : Entity_Id;
10314 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id;
10315 -- Subsidiary to Map_Discriminants. Find the value of discriminant Discr
10316 -- in the derivation chain starting from parent type Par_Typ leading to
10317 -- derived type Deriv_Typ. The returned value is one of the following:
10319 -- * An entity which is either a discriminant or a nondiscriminant
10320 -- name, and renames/constraints Discr.
10322 -- * An expression which constraints Discr
10324 -- Typ_Elmt is an element of the derivation chain created by routine
10325 -- Build_Chain and denotes the current ancestor being examined.
10327 procedure Map_Discriminants
10328 (Par_Typ : Entity_Id;
10329 Deriv_Typ : Entity_Id);
10330 -- Map each discriminant of type Par_Typ to a meaningful constraint
10331 -- from the point of view of type Deriv_Typ.
10333 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id);
10334 -- Map each primitive of type Par_Typ to a corresponding primitive of
10335 -- type Deriv_Typ.
10337 -------------------
10338 -- Add_Primitive --
10339 -------------------
10341 procedure Add_Primitive (Prim : Entity_Id; Par_Typ : Entity_Id) is
10342 Par_Prim : Entity_Id;
10344 begin
10345 -- Inspect the inheritance chain through the Alias attribute and the
10346 -- overriding chain through the Overridden_Operation looking for an
10347 -- ancestor primitive with the appropriate dispatching type.
10349 Par_Prim := Prim;
10350 while Present (Par_Prim) loop
10351 exit when Find_Dispatching_Type (Par_Prim) = Par_Typ;
10352 Par_Prim := Ancestor_Primitive (Par_Prim);
10353 end loop;
10355 -- Create a mapping of the form:
10357 -- parent type primitive -> derived type primitive
10359 if Present (Par_Prim) then
10360 Type_Map.Set (Par_Prim, Prim);
10361 end if;
10362 end Add_Primitive;
10364 ------------------------
10365 -- Ancestor_Primitive --
10366 ------------------------
10368 function Ancestor_Primitive (Subp : Entity_Id) return Entity_Id is
10369 Inher_Prim : constant Entity_Id := Alias (Subp);
10370 Over_Prim : constant Entity_Id := Overridden_Operation (Subp);
10372 begin
10373 -- The current subprogram overrides an ancestor primitive
10375 if Present (Over_Prim) then
10376 return Over_Prim;
10378 -- The current subprogram is an internally generated alias of an
10379 -- inherited ancestor primitive.
10381 elsif Present (Inher_Prim) then
10382 -- It is possible that an internally generated alias could be
10383 -- set to a subprogram which overrides the same aliased primitive,
10384 -- so return Empty in this case.
10386 if Ancestor_Primitive (Inher_Prim) = Subp then
10387 return Empty;
10388 end if;
10390 return Inher_Prim;
10392 -- Otherwise the current subprogram is the root of the inheritance or
10393 -- overriding chain.
10395 else
10396 return Empty;
10397 end if;
10398 end Ancestor_Primitive;
10400 -----------------
10401 -- Build_Chain --
10402 -----------------
10404 function Build_Chain
10405 (Par_Typ : Entity_Id;
10406 Deriv_Typ : Entity_Id) return Elist_Id
10408 Anc_Typ : Entity_Id;
10409 Chain : Elist_Id;
10410 Curr_Typ : Entity_Id;
10412 begin
10413 Chain := New_Elmt_List;
10415 -- Add the derived type to the derivation chain
10417 Prepend_Elmt (Deriv_Typ, Chain);
10419 -- Examine all ancestors starting from the derived type climbing
10420 -- towards parent type Par_Typ.
10422 Curr_Typ := Deriv_Typ;
10423 loop
10424 -- Handle the case where the current type is a record which
10425 -- derives from a subtype.
10427 -- subtype Sub_Typ is Par_Typ ...
10428 -- type Deriv_Typ is Sub_Typ ...
10430 if Ekind (Curr_Typ) = E_Record_Type
10431 and then Present (Parent_Subtype (Curr_Typ))
10432 then
10433 Anc_Typ := Parent_Subtype (Curr_Typ);
10435 -- Handle the case where the current type is a record subtype of
10436 -- another subtype.
10438 -- subtype Sub_Typ1 is Par_Typ ...
10439 -- subtype Sub_Typ2 is Sub_Typ1 ...
10441 elsif Ekind (Curr_Typ) = E_Record_Subtype
10442 and then Present (Cloned_Subtype (Curr_Typ))
10443 then
10444 Anc_Typ := Cloned_Subtype (Curr_Typ);
10446 -- Otherwise use the direct parent type
10448 else
10449 Anc_Typ := Etype (Curr_Typ);
10450 end if;
10452 -- Use the first subtype when dealing with itypes
10454 if Is_Itype (Anc_Typ) then
10455 Anc_Typ := First_Subtype (Anc_Typ);
10456 end if;
10458 -- Work with the view which contains the discriminants and stored
10459 -- constraints.
10461 Anc_Typ := Discriminated_View (Anc_Typ);
10463 -- Stop the climb when either the parent type has been reached or
10464 -- there are no more ancestors left to examine.
10466 exit when Anc_Typ = Curr_Typ or else Anc_Typ = Par_Typ;
10468 Prepend_Unique_Elmt (Anc_Typ, Chain);
10469 Curr_Typ := Anc_Typ;
10470 end loop;
10472 return Chain;
10473 end Build_Chain;
10475 ------------------------
10476 -- Discriminated_View --
10477 ------------------------
10479 function Discriminated_View (Typ : Entity_Id) return Entity_Id is
10480 T : Entity_Id;
10482 begin
10483 T := Typ;
10485 -- Use the [underlying] full view when dealing with private types
10486 -- because the view contains all inherited discriminants or stored
10487 -- constraints.
10489 if Is_Private_Type (T) then
10490 if Present (Underlying_Full_View (T)) then
10491 T := Underlying_Full_View (T);
10493 elsif Present (Full_View (T)) then
10494 T := Full_View (T);
10495 end if;
10496 end if;
10498 -- Use the underlying record view when the type is an extenstion of
10499 -- a parent type with unknown discriminants because the view contains
10500 -- all inherited discriminants or stored constraints.
10502 if Ekind (T) = E_Record_Type
10503 and then Present (Underlying_Record_View (T))
10504 then
10505 T := Underlying_Record_View (T);
10506 end if;
10508 return T;
10509 end Discriminated_View;
10511 -----------------------------
10512 -- Find_Discriminant_Value --
10513 -----------------------------
10515 function Find_Discriminant_Value
10516 (Discr : Entity_Id;
10517 Par_Typ : Entity_Id;
10518 Deriv_Typ : Entity_Id;
10519 Typ_Elmt : Elmt_Id) return Node_Or_Entity_Id
10521 Discr_Pos : constant Uint := Discriminant_Number (Discr);
10522 Typ : constant Entity_Id := Node (Typ_Elmt);
10524 function Find_Constraint_Value
10525 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id;
10526 -- Given constraint Constr, find what it denotes. This is either:
10528 -- * An entity which is either a discriminant or a name
10530 -- * An expression
10532 ---------------------------
10533 -- Find_Constraint_Value --
10534 ---------------------------
10536 function Find_Constraint_Value
10537 (Constr : Node_Or_Entity_Id) return Node_Or_Entity_Id
10539 begin
10540 if Nkind (Constr) in N_Entity then
10542 -- The constraint denotes a discriminant of the curren type
10543 -- which renames the ancestor discriminant:
10545 -- vv
10546 -- type Typ (D1 : ...; DN : ...) is
10547 -- new Anc (Discr => D1) with ...
10548 -- ^^
10550 if Ekind (Constr) = E_Discriminant then
10552 -- The discriminant belongs to derived type Deriv_Typ. This
10553 -- is the final value for the ancestor discriminant as the
10554 -- derivations chain has been fully exhausted.
10556 if Typ = Deriv_Typ then
10557 return Constr;
10559 -- Otherwise the discriminant may be renamed or constrained
10560 -- at a lower level. Continue looking down the derivation
10561 -- chain.
10563 else
10564 return
10565 Find_Discriminant_Value
10566 (Discr => Constr,
10567 Par_Typ => Par_Typ,
10568 Deriv_Typ => Deriv_Typ,
10569 Typ_Elmt => Next_Elmt (Typ_Elmt));
10570 end if;
10572 -- Otherwise the constraint denotes a reference to some name
10573 -- which results in a Stored discriminant:
10575 -- vvvv
10576 -- Name : ...;
10577 -- type Typ (D1 : ...; DN : ...) is
10578 -- new Anc (Discr => Name) with ...
10579 -- ^^^^
10581 -- Return the name as this is the proper constraint of the
10582 -- discriminant.
10584 else
10585 return Constr;
10586 end if;
10588 -- The constraint denotes a reference to a name
10590 elsif Is_Entity_Name (Constr) then
10591 return Find_Constraint_Value (Entity (Constr));
10593 -- Otherwise the current constraint is an expression which yields
10594 -- a Stored discriminant:
10596 -- type Typ (D1 : ...; DN : ...) is
10597 -- new Anc (Discr => <expression>) with ...
10598 -- ^^^^^^^^^^
10600 -- Return the expression as this is the proper constraint of the
10601 -- discriminant.
10603 else
10604 return Constr;
10605 end if;
10606 end Find_Constraint_Value;
10608 -- Local variables
10610 Constrs : constant Elist_Id := Stored_Constraint (Typ);
10612 Constr_Elmt : Elmt_Id;
10613 Pos : Uint;
10614 Typ_Discr : Entity_Id;
10616 -- Start of processing for Find_Discriminant_Value
10618 begin
10619 -- The algorithm for finding the value of a discriminant works as
10620 -- follows. First, it recreates the derivation chain from Par_Typ
10621 -- to Deriv_Typ as a list:
10623 -- Par_Typ (shown for completeness)
10624 -- v
10625 -- Ancestor_N <-- head of chain
10626 -- v
10627 -- Ancestor_1
10628 -- v
10629 -- Deriv_Typ <-- tail of chain
10631 -- The algorithm then traces the fate of a parent discriminant down
10632 -- the derivation chain. At each derivation level, the discriminant
10633 -- may be either inherited or constrained.
10635 -- 1) Discriminant is inherited: there are two cases, depending on
10636 -- which type is inheriting.
10638 -- 1.1) Deriv_Typ is inheriting:
10640 -- type Ancestor (D_1 : ...) is tagged ...
10641 -- type Deriv_Typ is new Ancestor ...
10643 -- In this case the inherited discriminant is the final value of
10644 -- the parent discriminant because the end of the derivation chain
10645 -- has been reached.
10647 -- 1.2) Some other type is inheriting:
10649 -- type Ancestor_1 (D_1 : ...) is tagged ...
10650 -- type Ancestor_2 is new Ancestor_1 ...
10652 -- In this case the algorithm continues to trace the fate of the
10653 -- inherited discriminant down the derivation chain because it may
10654 -- be further inherited or constrained.
10656 -- 2) Discriminant is constrained: there are three cases, depending
10657 -- on what the constraint is.
10659 -- 2.1) The constraint is another discriminant (aka renaming):
10661 -- type Ancestor_1 (D_1 : ...) is tagged ...
10662 -- type Ancestor_2 (D_2 : ...) is new Ancestor_1 (D_1 => D_2) ...
10664 -- In this case the constraining discriminant becomes the one to
10665 -- track down the derivation chain. The algorithm already knows
10666 -- that D_2 constrains D_1, therefore if the algorithm finds the
10667 -- value of D_2, then this would also be the value for D_1.
10669 -- 2.2) The constraint is a name (aka Stored):
10671 -- Name : ...
10672 -- type Ancestor_1 (D_1 : ...) is tagged ...
10673 -- type Ancestor_2 is new Ancestor_1 (D_1 => Name) ...
10675 -- In this case the name is the final value of D_1 because the
10676 -- discriminant cannot be further constrained.
10678 -- 2.3) The constraint is an expression (aka Stored):
10680 -- type Ancestor_1 (D_1 : ...) is tagged ...
10681 -- type Ancestor_2 is new Ancestor_1 (D_1 => 1 + 2) ...
10683 -- Similar to 2.2, the expression is the final value of D_1
10685 Pos := Uint_1;
10687 -- When a derived type constrains its parent type, all constaints
10688 -- appear in the Stored_Constraint list. Examine the list looking
10689 -- for a positional match.
10691 if Present (Constrs) then
10692 Constr_Elmt := First_Elmt (Constrs);
10693 while Present (Constr_Elmt) loop
10695 -- The position of the current constraint matches that of the
10696 -- ancestor discriminant.
10698 if Pos = Discr_Pos then
10699 return Find_Constraint_Value (Node (Constr_Elmt));
10700 end if;
10702 Next_Elmt (Constr_Elmt);
10703 Pos := Pos + 1;
10704 end loop;
10706 -- Otherwise the derived type does not constraint its parent type in
10707 -- which case it inherits the parent discriminants.
10709 else
10710 Typ_Discr := First_Discriminant (Typ);
10711 while Present (Typ_Discr) loop
10713 -- The position of the current discriminant matches that of the
10714 -- ancestor discriminant.
10716 if Pos = Discr_Pos then
10717 return Find_Constraint_Value (Typ_Discr);
10718 end if;
10720 Next_Discriminant (Typ_Discr);
10721 Pos := Pos + 1;
10722 end loop;
10723 end if;
10725 -- A discriminant must always have a corresponding value. This is
10726 -- either another discriminant, a name, or an expression. If this
10727 -- point is reached, them most likely the derivation chain employs
10728 -- the wrong views of types.
10730 pragma Assert (False);
10732 return Empty;
10733 end Find_Discriminant_Value;
10735 -----------------------
10736 -- Map_Discriminants --
10737 -----------------------
10739 procedure Map_Discriminants
10740 (Par_Typ : Entity_Id;
10741 Deriv_Typ : Entity_Id)
10743 Deriv_Chain : constant Elist_Id := Build_Chain (Par_Typ, Deriv_Typ);
10745 Discr : Entity_Id;
10746 Discr_Val : Node_Or_Entity_Id;
10748 begin
10749 -- Examine each discriminant of parent type Par_Typ and find a
10750 -- suitable value for it from the point of view of derived type
10751 -- Deriv_Typ.
10753 if Has_Discriminants (Par_Typ) then
10754 Discr := First_Discriminant (Par_Typ);
10755 while Present (Discr) loop
10756 Discr_Val :=
10757 Find_Discriminant_Value
10758 (Discr => Discr,
10759 Par_Typ => Par_Typ,
10760 Deriv_Typ => Deriv_Typ,
10761 Typ_Elmt => First_Elmt (Deriv_Chain));
10763 -- Create a mapping of the form:
10765 -- parent type discriminant -> value
10767 Type_Map.Set (Discr, Discr_Val);
10769 Next_Discriminant (Discr);
10770 end loop;
10771 end if;
10772 end Map_Discriminants;
10774 --------------------
10775 -- Map_Primitives --
10776 --------------------
10778 procedure Map_Primitives (Par_Typ : Entity_Id; Deriv_Typ : Entity_Id) is
10779 Deriv_Prim : Entity_Id;
10780 Par_Prim : Entity_Id;
10781 Par_Prims : Elist_Id;
10782 Prim_Elmt : Elmt_Id;
10784 begin
10785 -- Inspect the primitives of the derived type and determine whether
10786 -- they relate to the primitives of the parent type. If there is a
10787 -- meaningful relation, create a mapping of the form:
10789 -- parent type primitive -> derived type primitive
10791 if Present (Direct_Primitive_Operations (Deriv_Typ)) then
10792 Prim_Elmt := First_Elmt (Direct_Primitive_Operations (Deriv_Typ));
10793 while Present (Prim_Elmt) loop
10794 Deriv_Prim := Node (Prim_Elmt);
10796 if Is_Subprogram (Deriv_Prim)
10797 and then Find_Dispatching_Type (Deriv_Prim) = Deriv_Typ
10798 then
10799 Add_Primitive (Deriv_Prim, Par_Typ);
10800 end if;
10802 Next_Elmt (Prim_Elmt);
10803 end loop;
10804 end if;
10806 -- If the parent operation is an interface operation, the overriding
10807 -- indicator is not present. Instead, we get from the interface
10808 -- operation the primitive of the current type that implements it.
10810 if Is_Interface (Par_Typ) then
10811 Par_Prims := Collect_Primitive_Operations (Par_Typ);
10813 if Present (Par_Prims) then
10814 Prim_Elmt := First_Elmt (Par_Prims);
10816 while Present (Prim_Elmt) loop
10817 Par_Prim := Node (Prim_Elmt);
10818 Deriv_Prim :=
10819 Find_Primitive_Covering_Interface (Deriv_Typ, Par_Prim);
10821 if Present (Deriv_Prim) then
10822 Type_Map.Set (Par_Prim, Deriv_Prim);
10823 end if;
10825 Next_Elmt (Prim_Elmt);
10826 end loop;
10827 end if;
10828 end if;
10829 end Map_Primitives;
10831 -- Start of processing for Map_Types
10833 begin
10834 -- Nothing to do if there are no types to work with
10836 if No (Parent_Type) or else No (Derived_Type) then
10837 return;
10839 -- Nothing to do if the mapping already exists
10841 elsif Type_Map.Get (Parent_Type) = Derived_Type then
10842 return;
10844 -- Nothing to do if both types are not tagged. Note that untagged types
10845 -- do not have primitive operations and their discriminants are already
10846 -- handled by gigi.
10848 elsif not Is_Tagged_Type (Parent_Type)
10849 or else not Is_Tagged_Type (Derived_Type)
10850 then
10851 return;
10852 end if;
10854 -- Create a mapping of the form
10856 -- parent type -> derived type
10858 -- to prevent any subsequent attempts to produce the same relations
10860 Type_Map.Set (Parent_Type, Derived_Type);
10862 -- Create mappings of the form
10864 -- parent type discriminant -> derived type discriminant
10865 -- <or>
10866 -- parent type discriminant -> constraint
10868 -- Note that mapping of discriminants breaks privacy because it needs to
10869 -- work with those views which contains the discriminants and any stored
10870 -- constraints.
10872 Map_Discriminants
10873 (Par_Typ => Discriminated_View (Parent_Type),
10874 Deriv_Typ => Discriminated_View (Derived_Type));
10876 -- Create mappings of the form
10878 -- parent type primitive -> derived type primitive
10880 Map_Primitives
10881 (Par_Typ => Parent_Type,
10882 Deriv_Typ => Derived_Type);
10883 end Map_Types;
10885 ----------------------------
10886 -- Matching_Standard_Type --
10887 ----------------------------
10889 function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id is
10890 pragma Assert (Is_Scalar_Type (Typ));
10891 Siz : constant Uint := Esize (Typ);
10893 begin
10894 -- Floating-point cases
10896 if Is_Floating_Point_Type (Typ) then
10897 if Siz <= Esize (Standard_Short_Float) then
10898 return Standard_Short_Float;
10899 elsif Siz <= Esize (Standard_Float) then
10900 return Standard_Float;
10901 elsif Siz <= Esize (Standard_Long_Float) then
10902 return Standard_Long_Float;
10903 elsif Siz <= Esize (Standard_Long_Long_Float) then
10904 return Standard_Long_Long_Float;
10905 else
10906 raise Program_Error;
10907 end if;
10909 -- Integer cases (includes fixed-point types)
10911 -- Unsigned integer cases (includes normal enumeration types)
10913 else
10914 return Small_Integer_Type_For (Siz, Is_Unsigned_Type (Typ));
10915 end if;
10916 end Matching_Standard_Type;
10918 -----------------------------
10919 -- May_Generate_Large_Temp --
10920 -----------------------------
10922 -- At the current time, the only types that we return False for (i.e. where
10923 -- we decide we know they cannot generate large temps) are ones where we
10924 -- know the size is 256 bits or less at compile time, and we are still not
10925 -- doing a thorough job on arrays and records.
10927 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
10928 begin
10929 if not Size_Known_At_Compile_Time (Typ) then
10930 return False;
10931 end if;
10933 if Known_Esize (Typ) and then Esize (Typ) <= 256 then
10934 return False;
10935 end if;
10937 if Is_Array_Type (Typ)
10938 and then Present (Packed_Array_Impl_Type (Typ))
10939 then
10940 return May_Generate_Large_Temp (Packed_Array_Impl_Type (Typ));
10941 end if;
10943 return True;
10944 end May_Generate_Large_Temp;
10946 --------------------------------------------
10947 -- Needs_Conditional_Null_Excluding_Check --
10948 --------------------------------------------
10950 function Needs_Conditional_Null_Excluding_Check
10951 (Typ : Entity_Id) return Boolean
10953 begin
10954 return
10955 Is_Array_Type (Typ) and then Can_Never_Be_Null (Component_Type (Typ));
10956 end Needs_Conditional_Null_Excluding_Check;
10958 ----------------------------
10959 -- Needs_Constant_Address --
10960 ----------------------------
10962 function Needs_Constant_Address
10963 (Decl : Node_Id;
10964 Typ : Entity_Id) return Boolean
10966 begin
10967 -- If we have no initialization of any kind, then we don't need to place
10968 -- any restrictions on the address clause, because the object will be
10969 -- elaborated after the address clause is evaluated. This happens if the
10970 -- declaration has no initial expression, or the type has no implicit
10971 -- initialization, or the object is imported.
10973 -- The same holds for all initialized scalar types and all access types.
10974 -- Packed bit array types of size up to the maximum integer size are
10975 -- represented using a modular type with an initialization (to zero) and
10976 -- can be processed like other initialized scalar types.
10978 -- If the type is controlled, code to attach the object to a
10979 -- finalization chain is generated at the point of declaration, and
10980 -- therefore the elaboration of the object cannot be delayed: the
10981 -- address expression must be a constant.
10983 if No (Expression (Decl))
10984 and then not Needs_Finalization (Typ)
10985 and then
10986 (not Has_Non_Null_Base_Init_Proc (Typ)
10987 or else Is_Imported (Defining_Identifier (Decl)))
10988 then
10989 return False;
10991 elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
10992 or else Is_Access_Type (Typ)
10993 or else
10994 (Is_Bit_Packed_Array (Typ)
10995 and then Is_Modular_Integer_Type (Packed_Array_Impl_Type (Typ)))
10996 then
10997 return False;
10999 else
11000 -- Otherwise, we require the address clause to be constant because
11001 -- the call to the initialization procedure (or the attach code) has
11002 -- to happen at the point of the declaration.
11004 -- Actually the IP call has been moved to the freeze actions anyway,
11005 -- so maybe we can relax this restriction???
11007 return True;
11008 end if;
11009 end Needs_Constant_Address;
11011 ----------------------------
11012 -- New_Class_Wide_Subtype --
11013 ----------------------------
11015 function New_Class_Wide_Subtype
11016 (CW_Typ : Entity_Id;
11017 N : Node_Id) return Entity_Id
11019 Res : constant Entity_Id := Create_Itype (E_Void, N);
11021 -- Capture relevant attributes of the class-wide subtype which must be
11022 -- restored after the copy.
11024 Res_Chars : constant Name_Id := Chars (Res);
11025 Res_Is_CGE : constant Boolean := Is_Checked_Ghost_Entity (Res);
11026 Res_Is_IGE : constant Boolean := Is_Ignored_Ghost_Entity (Res);
11027 Res_Is_IGN : constant Boolean := Is_Ignored_Ghost_Node (Res);
11028 Res_Scope : constant Entity_Id := Scope (Res);
11030 begin
11031 Copy_Node (CW_Typ, Res);
11033 -- Restore the relevant attributes of the class-wide subtype
11035 Set_Chars (Res, Res_Chars);
11036 Set_Is_Checked_Ghost_Entity (Res, Res_Is_CGE);
11037 Set_Is_Ignored_Ghost_Entity (Res, Res_Is_IGE);
11038 Set_Is_Ignored_Ghost_Node (Res, Res_Is_IGN);
11039 Set_Scope (Res, Res_Scope);
11041 -- Decorate the class-wide subtype
11043 Set_Associated_Node_For_Itype (Res, N);
11044 Set_Comes_From_Source (Res, False);
11045 Mutate_Ekind (Res, E_Class_Wide_Subtype);
11046 Set_Etype (Res, Base_Type (CW_Typ));
11047 Set_Freeze_Node (Res, Empty);
11048 Set_Is_Frozen (Res, False);
11049 Set_Is_Itype (Res);
11050 Set_Is_Public (Res, False);
11051 Set_Next_Entity (Res, Empty);
11052 Set_Prev_Entity (Res, Empty);
11053 Set_Sloc (Res, Sloc (N));
11055 Set_Public_Status (Res);
11057 return Res;
11058 end New_Class_Wide_Subtype;
11060 -----------------------------------
11061 -- OK_To_Do_Constant_Replacement --
11062 -----------------------------------
11064 function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
11065 ES : constant Entity_Id := Scope (E);
11066 CS : Entity_Id;
11068 begin
11069 -- Do not replace statically allocated objects, because they may be
11070 -- modified outside the current scope.
11072 if Is_Statically_Allocated (E) then
11073 return False;
11075 -- Do not replace aliased or volatile objects, since we don't know what
11076 -- else might change the value.
11078 elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
11079 return False;
11081 -- Debug flag -gnatdM disconnects this optimization
11083 elsif Debug_Flag_MM then
11084 return False;
11086 -- Otherwise check scopes
11088 else
11089 CS := Current_Scope;
11091 loop
11092 -- If we are in right scope, replacement is safe
11094 if CS = ES then
11095 return True;
11097 -- Packages do not affect the determination of safety
11099 elsif Ekind (CS) = E_Package then
11100 exit when CS = Standard_Standard;
11101 CS := Scope (CS);
11103 -- Blocks do not affect the determination of safety
11105 elsif Ekind (CS) = E_Block then
11106 CS := Scope (CS);
11108 -- Loops do not affect the determination of safety. Note that we
11109 -- kill all current values on entry to a loop, so we are just
11110 -- talking about processing within a loop here.
11112 elsif Ekind (CS) = E_Loop then
11113 CS := Scope (CS);
11115 -- Otherwise, the reference is dubious, and we cannot be sure that
11116 -- it is safe to do the replacement.
11118 else
11119 exit;
11120 end if;
11121 end loop;
11123 return False;
11124 end if;
11125 end OK_To_Do_Constant_Replacement;
11127 ------------------------------------
11128 -- Possible_Bit_Aligned_Component --
11129 ------------------------------------
11131 function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
11132 begin
11133 -- Do not process an unanalyzed node because it is not yet decorated and
11134 -- most checks performed below will fail.
11136 if not Analyzed (N) then
11137 return False;
11138 end if;
11140 -- There are never alignment issues in CodePeer mode
11142 if CodePeer_Mode then
11143 return False;
11144 end if;
11146 case Nkind (N) is
11148 -- Case of indexed component
11150 when N_Indexed_Component =>
11151 declare
11152 P : constant Node_Id := Prefix (N);
11153 Ptyp : constant Entity_Id := Etype (P);
11155 begin
11156 -- If we know the component size and it is not larger than the
11157 -- maximum integer size, then we are OK. The back end does the
11158 -- assignment of small misaligned objects correctly.
11160 if Known_Static_Component_Size (Ptyp)
11161 and then Component_Size (Ptyp) <= System_Max_Integer_Size
11162 then
11163 return False;
11165 -- Otherwise, we need to test the prefix, to see if we are
11166 -- indexing from a possibly unaligned component.
11168 else
11169 return Possible_Bit_Aligned_Component (P);
11170 end if;
11171 end;
11173 -- Case of selected component
11175 when N_Selected_Component =>
11176 declare
11177 P : constant Node_Id := Prefix (N);
11178 Comp : constant Entity_Id := Entity (Selector_Name (N));
11180 begin
11181 -- This is the crucial test: if the component itself causes
11182 -- trouble, then we can stop and return True.
11184 if Component_May_Be_Bit_Aligned (Comp) then
11185 return True;
11187 -- Otherwise, we need to test the prefix, to see if we are
11188 -- selecting from a possibly unaligned component.
11190 else
11191 return Possible_Bit_Aligned_Component (P);
11192 end if;
11193 end;
11195 -- For a slice, test the prefix, if that is possibly misaligned,
11196 -- then for sure the slice is.
11198 when N_Slice =>
11199 return Possible_Bit_Aligned_Component (Prefix (N));
11201 -- For an unchecked conversion, check whether the expression may
11202 -- be bit aligned.
11204 when N_Unchecked_Type_Conversion =>
11205 return Possible_Bit_Aligned_Component (Expression (N));
11207 -- If we have none of the above, it means that we have fallen off the
11208 -- top testing prefixes recursively, and we now have a stand alone
11209 -- object, where we don't have a problem, unless this is a renaming,
11210 -- in which case we need to look into the renamed object.
11212 when others =>
11213 if Is_Entity_Name (N)
11214 and then Is_Object (Entity (N))
11215 and then Present (Renamed_Object (Entity (N)))
11216 then
11217 return
11218 Possible_Bit_Aligned_Component (Renamed_Object (Entity (N)));
11219 else
11220 return False;
11221 end if;
11222 end case;
11223 end Possible_Bit_Aligned_Component;
11225 -----------------------------------------------
11226 -- Process_Statements_For_Controlled_Objects --
11227 -----------------------------------------------
11229 procedure Process_Statements_For_Controlled_Objects (N : Node_Id) is
11230 Loc : constant Source_Ptr := Sloc (N);
11232 function Are_Wrapped (L : List_Id) return Boolean;
11233 -- Determine whether list L contains only one statement which is a block
11235 function Wrap_Statements_In_Block
11236 (L : List_Id;
11237 Scop : Entity_Id := Current_Scope) return Node_Id;
11238 -- Given a list of statements L, wrap it in a block statement and return
11239 -- the generated node. Scop is either the current scope or the scope of
11240 -- the context (if applicable).
11242 -----------------
11243 -- Are_Wrapped --
11244 -----------------
11246 function Are_Wrapped (L : List_Id) return Boolean is
11247 Stmt : constant Node_Id := First (L);
11248 begin
11249 return
11250 Present (Stmt)
11251 and then No (Next (Stmt))
11252 and then Nkind (Stmt) = N_Block_Statement;
11253 end Are_Wrapped;
11255 ------------------------------
11256 -- Wrap_Statements_In_Block --
11257 ------------------------------
11259 function Wrap_Statements_In_Block
11260 (L : List_Id;
11261 Scop : Entity_Id := Current_Scope) return Node_Id
11263 Block_Id : Entity_Id;
11264 Block_Nod : Node_Id;
11265 Iter_Loop : Entity_Id;
11267 begin
11268 Block_Nod :=
11269 Make_Block_Statement (Loc,
11270 Declarations => No_List,
11271 Handled_Statement_Sequence =>
11272 Make_Handled_Sequence_Of_Statements (Loc,
11273 Statements => L));
11275 -- Create a label for the block in case the block needs to manage the
11276 -- secondary stack. A label allows for flag Uses_Sec_Stack to be set.
11278 Add_Block_Identifier (Block_Nod, Block_Id, Scop);
11280 -- When wrapping the statements of an iterator loop, check whether
11281 -- the loop requires secondary stack management and if so, propagate
11282 -- the appropriate flags to the block. This ensures that the cursor
11283 -- is properly cleaned up at each iteration of the loop.
11285 Iter_Loop := Find_Enclosing_Iterator_Loop (Scop);
11287 if Present (Iter_Loop) then
11288 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Iter_Loop));
11290 -- Secondary stack reclamation is suppressed when the associated
11291 -- iterator loop contains a return statement which uses the stack.
11293 Set_Sec_Stack_Needed_For_Return
11294 (Block_Id, Sec_Stack_Needed_For_Return (Iter_Loop));
11295 end if;
11297 return Block_Nod;
11298 end Wrap_Statements_In_Block;
11300 -- Local variables
11302 Block : Node_Id;
11304 -- Start of processing for Process_Statements_For_Controlled_Objects
11306 begin
11307 -- Whenever a non-handled statement list is wrapped in a block, the
11308 -- block must be explicitly analyzed to redecorate all entities in the
11309 -- list and ensure that a finalizer is properly built.
11311 case Nkind (N) is
11312 when N_Conditional_Entry_Call
11313 | N_Elsif_Part
11314 | N_If_Statement
11315 | N_Selective_Accept
11317 -- Check the "then statements" for elsif parts and if statements
11319 if Nkind (N) in N_Elsif_Part | N_If_Statement
11320 and then not Is_Empty_List (Then_Statements (N))
11321 and then not Are_Wrapped (Then_Statements (N))
11322 and then Requires_Cleanup_Actions
11323 (L => Then_Statements (N),
11324 Lib_Level => False,
11325 Nested_Constructs => False)
11326 then
11327 Block := Wrap_Statements_In_Block (Then_Statements (N));
11328 Set_Then_Statements (N, New_List (Block));
11330 Analyze (Block);
11331 end if;
11333 -- Check the "else statements" for conditional entry calls, if
11334 -- statements and selective accepts.
11336 if Nkind (N) in
11337 N_Conditional_Entry_Call | N_If_Statement | N_Selective_Accept
11338 and then not Is_Empty_List (Else_Statements (N))
11339 and then not Are_Wrapped (Else_Statements (N))
11340 and then Requires_Cleanup_Actions
11341 (L => Else_Statements (N),
11342 Lib_Level => False,
11343 Nested_Constructs => False)
11344 then
11345 Block := Wrap_Statements_In_Block (Else_Statements (N));
11346 Set_Else_Statements (N, New_List (Block));
11348 Analyze (Block);
11349 end if;
11351 when N_Abortable_Part
11352 | N_Accept_Alternative
11353 | N_Case_Statement_Alternative
11354 | N_Delay_Alternative
11355 | N_Entry_Call_Alternative
11356 | N_Exception_Handler
11357 | N_Loop_Statement
11358 | N_Triggering_Alternative
11360 if not Is_Empty_List (Statements (N))
11361 and then not Are_Wrapped (Statements (N))
11362 and then Requires_Cleanup_Actions
11363 (L => Statements (N),
11364 Lib_Level => False,
11365 Nested_Constructs => False)
11366 then
11367 if Nkind (N) = N_Loop_Statement
11368 and then Present (Identifier (N))
11369 then
11370 Block :=
11371 Wrap_Statements_In_Block
11372 (L => Statements (N),
11373 Scop => Entity (Identifier (N)));
11374 else
11375 Block := Wrap_Statements_In_Block (Statements (N));
11376 end if;
11378 Set_Statements (N, New_List (Block));
11379 Analyze (Block);
11380 end if;
11382 -- Could be e.g. a loop that was transformed into a block or null
11383 -- statement. Do nothing for terminate alternatives.
11385 when N_Block_Statement
11386 | N_Null_Statement
11387 | N_Terminate_Alternative
11389 null;
11391 when others =>
11392 raise Program_Error;
11393 end case;
11394 end Process_Statements_For_Controlled_Objects;
11396 ------------------
11397 -- Power_Of_Two --
11398 ------------------
11400 function Power_Of_Two (N : Node_Id) return Nat is
11401 Typ : constant Entity_Id := Etype (N);
11402 pragma Assert (Is_Integer_Type (Typ));
11404 Siz : constant Nat := UI_To_Int (Esize (Typ));
11405 Val : Uint;
11407 begin
11408 if not Compile_Time_Known_Value (N) then
11409 return 0;
11411 else
11412 Val := Expr_Value (N);
11413 for J in 1 .. Siz - 1 loop
11414 if Val = Uint_2 ** J then
11415 return J;
11416 end if;
11417 end loop;
11419 return 0;
11420 end if;
11421 end Power_Of_Two;
11423 ----------------------
11424 -- Remove_Init_Call --
11425 ----------------------
11427 function Remove_Init_Call
11428 (Var : Entity_Id;
11429 Rep_Clause : Node_Id) return Node_Id
11431 Par : constant Node_Id := Parent (Var);
11432 Typ : constant Entity_Id := Etype (Var);
11434 Init_Proc : Entity_Id;
11435 -- Initialization procedure for Typ
11437 function Find_Init_Call_In_List (From : Node_Id) return Node_Id;
11438 -- Look for init call for Var starting at From and scanning the
11439 -- enclosing list until Rep_Clause or the end of the list is reached.
11441 ----------------------------
11442 -- Find_Init_Call_In_List --
11443 ----------------------------
11445 function Find_Init_Call_In_List (From : Node_Id) return Node_Id is
11446 Init_Call : Node_Id;
11448 begin
11449 Init_Call := From;
11450 while Present (Init_Call) and then Init_Call /= Rep_Clause loop
11451 if Nkind (Init_Call) = N_Procedure_Call_Statement
11452 and then Is_Entity_Name (Name (Init_Call))
11453 and then Entity (Name (Init_Call)) = Init_Proc
11454 then
11455 return Init_Call;
11456 end if;
11458 Next (Init_Call);
11459 end loop;
11461 return Empty;
11462 end Find_Init_Call_In_List;
11464 Init_Call : Node_Id;
11466 -- Start of processing for Remove_Init_Call
11468 begin
11469 if Present (Initialization_Statements (Var)) then
11470 Init_Call := Initialization_Statements (Var);
11471 Set_Initialization_Statements (Var, Empty);
11473 elsif not Has_Non_Null_Base_Init_Proc (Typ) then
11475 -- No init proc for the type, so obviously no call to be found
11477 return Empty;
11479 else
11480 -- We might be able to handle other cases below by just properly
11481 -- setting Initialization_Statements at the point where the init proc
11482 -- call is generated???
11484 Init_Proc := Base_Init_Proc (Typ);
11486 -- First scan the list containing the declaration of Var
11488 Init_Call := Find_Init_Call_In_List (From => Next (Par));
11490 -- If not found, also look on Var's freeze actions list, if any,
11491 -- since the init call may have been moved there (case of an address
11492 -- clause applying to Var).
11494 if No (Init_Call) and then Present (Freeze_Node (Var)) then
11495 Init_Call :=
11496 Find_Init_Call_In_List (First (Actions (Freeze_Node (Var))));
11497 end if;
11499 -- If the initialization call has actuals that use the secondary
11500 -- stack, the call may have been wrapped into a temporary block, in
11501 -- which case the block itself has to be removed.
11503 if No (Init_Call) and then Nkind (Next (Par)) = N_Block_Statement then
11504 declare
11505 Blk : constant Node_Id := Next (Par);
11506 begin
11507 if Present
11508 (Find_Init_Call_In_List
11509 (First (Statements (Handled_Statement_Sequence (Blk)))))
11510 then
11511 Init_Call := Blk;
11512 end if;
11513 end;
11514 end if;
11515 end if;
11517 if Present (Init_Call) then
11518 -- If restrictions have forbidden Aborts, the initialization call
11519 -- for objects that require deep initialization has not been wrapped
11520 -- into the following block (see Exp_Ch3, Default_Initialize_Object)
11521 -- so if present remove it as well, and include the IP call in it,
11522 -- in the rare case the caller may need to simply displace the
11523 -- initialization, as is done for a later address specification.
11525 if Nkind (Next (Init_Call)) = N_Block_Statement
11526 and then Is_Initialization_Block (Next (Init_Call))
11527 then
11528 declare
11529 IP_Call : constant Node_Id := Init_Call;
11530 begin
11531 Init_Call := Next (IP_Call);
11532 Remove (IP_Call);
11533 Prepend (IP_Call,
11534 Statements (Handled_Statement_Sequence (Init_Call)));
11535 end;
11536 end if;
11538 Remove (Init_Call);
11539 end if;
11541 return Init_Call;
11542 end Remove_Init_Call;
11544 -------------------------
11545 -- Remove_Side_Effects --
11546 -------------------------
11548 procedure Remove_Side_Effects
11549 (Exp : Node_Id;
11550 Name_Req : Boolean := False;
11551 Renaming_Req : Boolean := False;
11552 Variable_Ref : Boolean := False;
11553 Related_Id : Entity_Id := Empty;
11554 Is_Low_Bound : Boolean := False;
11555 Is_High_Bound : Boolean := False;
11556 Discr_Number : Int := 0;
11557 Check_Side_Effects : Boolean := True)
11559 function Build_Temporary
11560 (Loc : Source_Ptr;
11561 Id : Character;
11562 Related_Nod : Node_Id := Empty) return Entity_Id;
11563 -- Create an external symbol of the form xxx_FIRST/_LAST if Related_Nod
11564 -- is present (xxx is taken from the Chars field of Related_Nod),
11565 -- otherwise it generates an internal temporary. The created temporary
11566 -- entity is marked as internal.
11568 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean;
11569 -- Computes whether a side effect is possible in SPARK, which should
11570 -- be handled by removing it from the expression for GNATprove. Note
11571 -- that other side effects related to volatile variables are handled
11572 -- separately.
11574 ---------------------
11575 -- Build_Temporary --
11576 ---------------------
11578 function Build_Temporary
11579 (Loc : Source_Ptr;
11580 Id : Character;
11581 Related_Nod : Node_Id := Empty) return Entity_Id
11583 Temp_Id : Entity_Id;
11584 Temp_Nam : Name_Id;
11585 Should_Set_Related_Expression : Boolean := False;
11587 begin
11588 -- The context requires an external symbol : expression is
11589 -- the bound of an array, or a discriminant value. We create
11590 -- a unique string using the related entity and an appropriate
11591 -- suffix, rather than a numeric serial number (used for internal
11592 -- entities) that may vary depending on compilation options, in
11593 -- particular on the Assertions_Enabled mode. This avoids spurious
11594 -- link errors.
11596 if Present (Related_Id) then
11597 if Is_Low_Bound then
11598 Temp_Nam := New_External_Name (Chars (Related_Id), "_FIRST");
11600 elsif Is_High_Bound then
11601 Temp_Nam := New_External_Name (Chars (Related_Id), "_LAST");
11603 else
11604 pragma Assert (Discr_Number > 0);
11606 -- We don't have any intelligible way of printing T_DISCR in
11607 -- CodePeer. Thus, set a related expression in this case.
11609 Should_Set_Related_Expression := True;
11611 -- Use fully qualified name to avoid ambiguities.
11613 Temp_Nam :=
11614 New_External_Name
11615 (Get_Qualified_Name (Related_Id), "_DISCR", Discr_Number);
11616 end if;
11618 Temp_Id := Make_Defining_Identifier (Loc, Temp_Nam);
11620 if Should_Set_Related_Expression then
11621 Set_Related_Expression (Temp_Id, Related_Nod);
11622 end if;
11624 -- Otherwise generate an internal temporary
11626 else
11627 Temp_Id := Make_Temporary (Loc, Id, Related_Nod);
11628 end if;
11630 Set_Is_Internal (Temp_Id);
11632 return Temp_Id;
11633 end Build_Temporary;
11635 -----------------------------------
11636 -- Possible_Side_Effect_In_SPARK --
11637 -----------------------------------
11639 function Possible_Side_Effect_In_SPARK (Exp : Node_Id) return Boolean is
11640 begin
11641 -- Side-effect removal in SPARK should only occur when not inside a
11642 -- generic and not doing a preanalysis, inside an object renaming or
11643 -- a type declaration or a for-loop iteration scheme.
11645 return not Inside_A_Generic
11646 and then Full_Analysis
11647 and then Nkind (Enclosing_Declaration (Exp)) in
11648 N_Component_Declaration
11649 | N_Full_Type_Declaration
11650 | N_Iterator_Specification
11651 | N_Loop_Parameter_Specification
11652 | N_Object_Renaming_Declaration
11653 | N_Subtype_Declaration;
11654 end Possible_Side_Effect_In_SPARK;
11656 -- Local variables
11658 Loc : constant Source_Ptr := Sloc (Exp);
11659 Exp_Type : constant Entity_Id := Etype (Exp);
11660 Svg_Suppress : constant Suppress_Record := Scope_Suppress;
11661 Def_Id : Entity_Id;
11662 E : Node_Id;
11663 New_Exp : Node_Id;
11664 Ptr_Typ_Decl : Node_Id;
11665 Ref_Type : Entity_Id;
11666 Res : Node_Id;
11668 -- Start of processing for Remove_Side_Effects
11670 begin
11671 -- Handle cases in which there is nothing to do. In GNATprove mode,
11672 -- removal of side effects is useful for the light expansion of
11673 -- renamings.
11675 if not Expander_Active
11676 and then not
11677 (GNATprove_Mode and then Possible_Side_Effect_In_SPARK (Exp))
11678 then
11679 return;
11681 -- Cannot generate temporaries if the invocation to remove side effects
11682 -- was issued too early and the type of the expression is not resolved
11683 -- (this happens because routines Duplicate_Subexpr_XX implicitly invoke
11684 -- Remove_Side_Effects).
11686 elsif No (Exp_Type)
11687 or else Ekind (Exp_Type) = E_Access_Attribute_Type
11688 then
11689 return;
11691 -- Nothing to do if prior expansion determined that a function call does
11692 -- not require side effect removal.
11694 elsif Nkind (Exp) = N_Function_Call
11695 and then No_Side_Effect_Removal (Exp)
11696 then
11697 return;
11699 -- No action needed for side-effect free expressions
11701 elsif Check_Side_Effects
11702 and then Side_Effect_Free (Exp, Name_Req, Variable_Ref)
11703 then
11704 return;
11706 -- Generating C code we cannot remove side effect of function returning
11707 -- class-wide types since there is no secondary stack (required to use
11708 -- 'reference).
11710 elsif Modify_Tree_For_C
11711 and then Nkind (Exp) = N_Function_Call
11712 and then Is_Class_Wide_Type (Etype (Exp))
11713 then
11714 return;
11715 end if;
11717 -- The remaining processing is done with all checks suppressed
11719 -- Note: from now on, don't use return statements, instead do a goto
11720 -- Leave, to ensure that we properly restore Scope_Suppress.Suppress.
11722 Scope_Suppress.Suppress := (others => True);
11724 -- If this is a side-effect free attribute reference whose expressions
11725 -- are also side-effect free and whose prefix is not a name, remove the
11726 -- side effects of the prefix. A copy of the prefix is required in this
11727 -- case and it is better not to make an additional one for the attribute
11728 -- itself, because the return type of many of them is universal integer,
11729 -- which is a very large type for a temporary.
11730 -- The prefix of an attribute reference Reduce may be syntactically an
11731 -- aggregate, but will be expanded into a loop, so no need to remove
11732 -- side-effects.
11734 if Nkind (Exp) = N_Attribute_Reference
11735 and then Side_Effect_Free_Attribute (Attribute_Name (Exp))
11736 and then Side_Effect_Free (Expressions (Exp), Name_Req, Variable_Ref)
11737 and then (Attribute_Name (Exp) /= Name_Reduce
11738 or else Nkind (Prefix (Exp)) /= N_Aggregate)
11739 and then not Is_Name_Reference (Prefix (Exp))
11740 then
11741 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11742 goto Leave;
11744 -- If this is an elementary or a small not-by-reference record type, and
11745 -- we need to capture the value, just make a constant; this is cheap and
11746 -- objects of both kinds of types can be bit aligned, so it might not be
11747 -- possible to generate a reference to them. Likewise if this is not a
11748 -- name reference, except for a type conversion, because we would enter
11749 -- an infinite recursion with Checks.Apply_Predicate_Check if the target
11750 -- type has predicates (and type conversions need a specific treatment
11751 -- anyway, see below). Also do it if we have a volatile reference and
11752 -- Name_Req is not set (see comments for Side_Effect_Free).
11754 elsif (Is_Elementary_Type (Exp_Type)
11755 or else (Is_Record_Type (Exp_Type)
11756 and then Known_Static_RM_Size (Exp_Type)
11757 and then RM_Size (Exp_Type) <= System_Max_Integer_Size
11758 and then not Has_Discriminants (Exp_Type)
11759 and then not Is_By_Reference_Type (Exp_Type)))
11760 and then (Variable_Ref
11761 or else (not Is_Name_Reference (Exp)
11762 and then Nkind (Exp) /= N_Type_Conversion)
11763 or else (not Name_Req
11764 and then Is_Volatile_Reference (Exp)))
11765 then
11766 Def_Id := Build_Temporary (Loc, 'R', Exp);
11767 Set_Etype (Def_Id, Exp_Type);
11768 Res := New_Occurrence_Of (Def_Id, Loc);
11770 -- If the expression is a packed reference, it must be reanalyzed and
11771 -- expanded, depending on context. This is the case for actuals where
11772 -- a constraint check may capture the actual before expansion of the
11773 -- call is complete.
11775 if Nkind (Exp) = N_Indexed_Component
11776 and then Is_Packed (Etype (Prefix (Exp)))
11777 then
11778 Set_Analyzed (Exp, False);
11779 Set_Analyzed (Prefix (Exp), False);
11780 end if;
11782 -- Generate:
11783 -- Rnn : Exp_Type renames Expr;
11785 -- In GNATprove mode, we prefer to use renamings for intermediate
11786 -- variables to definition of constants, due to the implicit move
11787 -- operation that such a constant definition causes as part of the
11788 -- support in GNATprove for ownership pointers. Hence, we generate
11789 -- a renaming for a reference to an object of a nonscalar type.
11791 if Renaming_Req
11792 or else (GNATprove_Mode
11793 and then Is_Object_Reference (Exp)
11794 and then not Is_Scalar_Type (Exp_Type))
11795 then
11796 E :=
11797 Make_Object_Renaming_Declaration (Loc,
11798 Defining_Identifier => Def_Id,
11799 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11800 Name => Relocate_Node (Exp));
11802 -- Generate:
11803 -- Rnn : constant Exp_Type := Expr;
11805 else
11806 E :=
11807 Make_Object_Declaration (Loc,
11808 Defining_Identifier => Def_Id,
11809 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11810 Constant_Present => True,
11811 Expression => Relocate_Node (Exp));
11813 Set_Assignment_OK (E);
11814 end if;
11816 Insert_Action (Exp, E);
11818 -- If the expression has the form v.all then we can just capture the
11819 -- pointer, and then do an explicit dereference on the result, but
11820 -- this is not right if this is a volatile reference.
11822 elsif Nkind (Exp) = N_Explicit_Dereference
11823 and then not Is_Volatile_Reference (Exp)
11824 then
11825 Def_Id := Build_Temporary (Loc, 'R', Exp);
11826 Res :=
11827 Make_Explicit_Dereference (Loc, New_Occurrence_Of (Def_Id, Loc));
11829 Insert_Action (Exp,
11830 Make_Object_Declaration (Loc,
11831 Defining_Identifier => Def_Id,
11832 Object_Definition =>
11833 New_Occurrence_Of (Etype (Prefix (Exp)), Loc),
11834 Constant_Present => True,
11835 Expression => Relocate_Node (Prefix (Exp))));
11837 -- Similar processing for an unchecked conversion of an expression of
11838 -- the form v.all, where we want the same kind of treatment.
11840 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11841 and then Nkind (Expression (Exp)) = N_Explicit_Dereference
11842 then
11843 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11844 goto Leave;
11846 -- If this is a type conversion, leave the type conversion and remove
11847 -- side effects in the expression, unless it is of universal integer,
11848 -- which is a very large type for a temporary. This is important in
11849 -- several circumstances: for change of representations and also when
11850 -- this is a view conversion to a smaller object, where gigi can end
11851 -- up creating its own temporary of the wrong size.
11853 elsif Nkind (Exp) = N_Type_Conversion
11854 and then Etype (Expression (Exp)) /= Universal_Integer
11855 then
11856 Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
11858 -- Generating C code the type conversion of an access to constrained
11859 -- array type into an access to unconstrained array type involves
11860 -- initializing a fat pointer and the expression must be free of
11861 -- side effects to safely compute its bounds.
11863 if Modify_Tree_For_C
11864 and then Is_Access_Type (Etype (Exp))
11865 and then Is_Array_Type (Designated_Type (Etype (Exp)))
11866 and then not Is_Constrained (Designated_Type (Etype (Exp)))
11867 then
11868 Def_Id := Build_Temporary (Loc, 'R', Exp);
11869 Set_Etype (Def_Id, Exp_Type);
11870 Res := New_Occurrence_Of (Def_Id, Loc);
11872 Insert_Action (Exp,
11873 Make_Object_Declaration (Loc,
11874 Defining_Identifier => Def_Id,
11875 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11876 Constant_Present => True,
11877 Expression => Relocate_Node (Exp)));
11878 else
11879 goto Leave;
11880 end if;
11882 -- If this is an unchecked conversion that Gigi can't handle, make
11883 -- a copy or a use a renaming to capture the value.
11885 elsif Nkind (Exp) = N_Unchecked_Type_Conversion
11886 and then not Safe_Unchecked_Type_Conversion (Exp)
11887 then
11888 if CW_Or_Needs_Finalization (Exp_Type) then
11890 -- Use a renaming to capture the expression, rather than create
11891 -- a controlled temporary.
11893 Def_Id := Build_Temporary (Loc, 'R', Exp);
11894 Res := New_Occurrence_Of (Def_Id, Loc);
11896 Insert_Action (Exp,
11897 Make_Object_Renaming_Declaration (Loc,
11898 Defining_Identifier => Def_Id,
11899 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11900 Name => Relocate_Node (Exp)));
11902 else
11903 Def_Id := Build_Temporary (Loc, 'R', Exp);
11904 Set_Etype (Def_Id, Exp_Type);
11905 Res := New_Occurrence_Of (Def_Id, Loc);
11907 E :=
11908 Make_Object_Declaration (Loc,
11909 Defining_Identifier => Def_Id,
11910 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
11911 Constant_Present => not Is_Variable (Exp),
11912 Expression => Relocate_Node (Exp));
11914 Set_Assignment_OK (E);
11915 Insert_Action (Exp, E);
11916 end if;
11918 -- If this is a packed array component or a selected component with a
11919 -- nonstandard representation, we cannot generate a reference because
11920 -- the component may be unaligned, so we must use a renaming and this
11921 -- renaming is handled by the front end, as the back end may balk at
11922 -- the nonstandard representation (see Evaluation_Required in Exp_Ch8).
11924 elsif (Nkind (Exp) in N_Indexed_Component | N_Selected_Component
11925 and then Has_Non_Standard_Rep (Etype (Prefix (Exp))))
11927 -- For an expression that denotes a name, we can use a renaming
11928 -- scheme. This is needed for correctness in the case of a volatile
11929 -- object of a nonvolatile type because the Make_Reference call of the
11930 -- "default" approach would generate an illegal access value (an
11931 -- access value cannot designate such an object - see
11932 -- Analyze_Reference).
11934 or else (Is_Name_Reference (Exp)
11936 -- We skip using this scheme if we have an object of a volatile
11937 -- type and we do not have Name_Req set true (see comments for
11938 -- Side_Effect_Free).
11940 and then (Name_Req or else not Treat_As_Volatile (Exp_Type)))
11941 then
11942 Def_Id := Build_Temporary (Loc, 'R', Exp);
11943 Res := New_Occurrence_Of (Def_Id, Loc);
11945 Insert_Action (Exp,
11946 Make_Object_Renaming_Declaration (Loc,
11947 Defining_Identifier => Def_Id,
11948 Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
11949 Name => Relocate_Node (Exp)));
11951 -- Avoid generating a variable-sized temporary, by generating the
11952 -- reference just for the function call. The transformation could be
11953 -- refined to apply only when the array component is constrained by a
11954 -- discriminant???
11956 elsif Nkind (Exp) = N_Selected_Component
11957 and then Nkind (Prefix (Exp)) = N_Function_Call
11958 and then Is_Array_Type (Exp_Type)
11959 then
11960 Remove_Side_Effects (Prefix (Exp), Name_Req, Variable_Ref);
11961 goto Leave;
11963 -- Otherwise we generate a reference to the expression
11965 else
11966 -- When generating C code we cannot consider side effect free object
11967 -- declarations that have discriminants and are initialized by means
11968 -- of a function call since on this target there is no secondary
11969 -- stack to store the return value and the expander may generate an
11970 -- extra call to the function to compute the discriminant value. In
11971 -- addition, for targets that have secondary stack, the expansion of
11972 -- functions with side effects involves the generation of an access
11973 -- type to capture the return value stored in the secondary stack;
11974 -- by contrast when generating C code such expansion generates an
11975 -- internal object declaration (no access type involved) which must
11976 -- be identified here to avoid entering into a never-ending loop
11977 -- generating internal object declarations.
11979 if Modify_Tree_For_C
11980 and then Nkind (Parent (Exp)) = N_Object_Declaration
11981 and then
11982 (Nkind (Exp) /= N_Function_Call
11983 or else not Has_Discriminants (Exp_Type)
11984 or else Is_Internal_Name
11985 (Chars (Defining_Identifier (Parent (Exp)))))
11986 then
11987 goto Leave;
11988 end if;
11990 -- Special processing for function calls that return a limited type.
11991 -- We need to build a declaration that will enable build-in-place
11992 -- expansion of the call. This is not done if the context is already
11993 -- an object declaration, to prevent infinite recursion.
11995 -- This is relevant only in Ada 2005 mode. In Ada 95 programs we have
11996 -- to accommodate functions returning limited objects by reference.
11998 if Ada_Version >= Ada_2005
11999 and then Nkind (Exp) = N_Function_Call
12000 and then Is_Limited_View (Etype (Exp))
12001 and then Nkind (Parent (Exp)) /= N_Object_Declaration
12002 then
12003 declare
12004 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
12005 Decl : Node_Id;
12007 begin
12008 Decl :=
12009 Make_Object_Declaration (Loc,
12010 Defining_Identifier => Obj,
12011 Object_Definition => New_Occurrence_Of (Exp_Type, Loc),
12012 Expression => Relocate_Node (Exp));
12014 Insert_Action (Exp, Decl);
12015 Set_Etype (Obj, Exp_Type);
12016 Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
12017 goto Leave;
12018 end;
12019 end if;
12021 Def_Id := Build_Temporary (Loc, 'R', Exp);
12023 -- The regular expansion of functions with side effects involves the
12024 -- generation of an access type to capture the return value found on
12025 -- the secondary stack. Since SPARK (and why) cannot process access
12026 -- types, use a different approach which ignores the secondary stack
12027 -- and "copies" the returned object.
12028 -- When generating C code, no need for a 'reference since the
12029 -- secondary stack is not supported.
12031 if GNATprove_Mode or Modify_Tree_For_C then
12032 Res := New_Occurrence_Of (Def_Id, Loc);
12033 Ref_Type := Exp_Type;
12035 -- Regular expansion utilizing an access type and 'reference
12037 else
12038 Res :=
12039 Make_Explicit_Dereference (Loc,
12040 Prefix => New_Occurrence_Of (Def_Id, Loc));
12042 -- Generate:
12043 -- type Ann is access all <Exp_Type>;
12045 Ref_Type := Make_Temporary (Loc, 'A');
12047 Ptr_Typ_Decl :=
12048 Make_Full_Type_Declaration (Loc,
12049 Defining_Identifier => Ref_Type,
12050 Type_Definition =>
12051 Make_Access_To_Object_Definition (Loc,
12052 All_Present => True,
12053 Subtype_Indication =>
12054 New_Occurrence_Of (Exp_Type, Loc)));
12056 Insert_Action (Exp, Ptr_Typ_Decl);
12057 end if;
12059 E := Exp;
12060 if Nkind (E) = N_Explicit_Dereference then
12061 New_Exp := Relocate_Node (Prefix (E));
12063 else
12064 E := Relocate_Node (E);
12066 -- Do not generate a 'reference in SPARK mode or C generation
12067 -- since the access type is not created in the first place.
12069 if GNATprove_Mode or Modify_Tree_For_C then
12070 New_Exp := E;
12072 -- Otherwise generate reference, marking the value as non-null
12073 -- since we know it cannot be null and we don't want a check.
12075 else
12076 New_Exp := Make_Reference (Loc, E);
12077 Set_Is_Known_Non_Null (Def_Id);
12078 end if;
12079 end if;
12081 if Is_Delayed_Aggregate (E) then
12083 -- The expansion of nested aggregates is delayed until the
12084 -- enclosing aggregate is expanded. As aggregates are often
12085 -- qualified, the predicate applies to qualified expressions as
12086 -- well, indicating that the enclosing aggregate has not been
12087 -- expanded yet. At this point the aggregate is part of a
12088 -- stand-alone declaration, and must be fully expanded.
12090 if Nkind (E) = N_Qualified_Expression then
12091 Set_Expansion_Delayed (Expression (E), False);
12092 Set_Analyzed (Expression (E), False);
12093 else
12094 Set_Expansion_Delayed (E, False);
12095 end if;
12097 Set_Analyzed (E, False);
12098 end if;
12100 -- Generating C code of object declarations that have discriminants
12101 -- and are initialized by means of a function call we propagate the
12102 -- discriminants of the parent type to the internally built object.
12103 -- This is needed to avoid generating an extra call to the called
12104 -- function.
12106 -- For example, if we generate here the following declaration, it
12107 -- will be expanded later adding an extra call to evaluate the value
12108 -- of the discriminant (needed to compute the size of the object).
12110 -- type Rec (D : Integer) is ...
12111 -- Obj : constant Rec := SomeFunc;
12113 if Modify_Tree_For_C
12114 and then Nkind (Parent (Exp)) = N_Object_Declaration
12115 and then Has_Discriminants (Exp_Type)
12116 and then Nkind (Exp) = N_Function_Call
12117 then
12118 Insert_Action (Exp,
12119 Make_Object_Declaration (Loc,
12120 Defining_Identifier => Def_Id,
12121 Object_Definition => New_Copy_Tree
12122 (Object_Definition (Parent (Exp))),
12123 Constant_Present => True,
12124 Expression => New_Exp));
12125 else
12126 Insert_Action (Exp,
12127 Make_Object_Declaration (Loc,
12128 Defining_Identifier => Def_Id,
12129 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
12130 Constant_Present => True,
12131 Expression => New_Exp));
12132 end if;
12133 end if;
12135 -- Preserve the Assignment_OK flag in all copies, since at least one
12136 -- copy may be used in a context where this flag must be set (otherwise
12137 -- why would the flag be set in the first place).
12139 Set_Assignment_OK (Res, Assignment_OK (Exp));
12141 -- Preserve the Do_Range_Check flag in all copies
12143 Set_Do_Range_Check (Res, Do_Range_Check (Exp));
12145 -- Finally rewrite the original expression and we are done
12147 Rewrite (Exp, Res);
12148 Analyze_And_Resolve (Exp, Exp_Type);
12150 <<Leave>>
12151 Scope_Suppress := Svg_Suppress;
12152 end Remove_Side_Effects;
12154 ------------------------
12155 -- Replace_References --
12156 ------------------------
12158 procedure Replace_References
12159 (Expr : Node_Id;
12160 Par_Typ : Entity_Id;
12161 Deriv_Typ : Entity_Id;
12162 Par_Obj : Entity_Id := Empty;
12163 Deriv_Obj : Entity_Id := Empty)
12165 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean;
12166 -- Determine whether node Ref denotes some component of Deriv_Obj
12168 function Replace_Ref (Ref : Node_Id) return Traverse_Result;
12169 -- Substitute a reference to an entity with the corresponding value
12170 -- stored in table Type_Map.
12172 function Type_Of_Formal
12173 (Call : Node_Id;
12174 Actual : Node_Id) return Entity_Id;
12175 -- Find the type of the formal parameter which corresponds to actual
12176 -- parameter Actual in subprogram call Call.
12178 ----------------------
12179 -- Is_Deriv_Obj_Ref --
12180 ----------------------
12182 function Is_Deriv_Obj_Ref (Ref : Node_Id) return Boolean is
12183 Par : constant Node_Id := Parent (Ref);
12185 begin
12186 -- Detect the folowing selected component form:
12188 -- Deriv_Obj.(something)
12190 return
12191 Nkind (Par) = N_Selected_Component
12192 and then Is_Entity_Name (Prefix (Par))
12193 and then Entity (Prefix (Par)) = Deriv_Obj;
12194 end Is_Deriv_Obj_Ref;
12196 -----------------
12197 -- Replace_Ref --
12198 -----------------
12200 function Replace_Ref (Ref : Node_Id) return Traverse_Result is
12201 procedure Remove_Controlling_Arguments (From_Arg : Node_Id);
12202 -- Reset the Controlling_Argument of all function calls that
12203 -- encapsulate node From_Arg.
12205 ----------------------------------
12206 -- Remove_Controlling_Arguments --
12207 ----------------------------------
12209 procedure Remove_Controlling_Arguments (From_Arg : Node_Id) is
12210 Par : Node_Id;
12212 begin
12213 Par := From_Arg;
12214 while Present (Par) loop
12215 if Nkind (Par) = N_Function_Call
12216 and then Present (Controlling_Argument (Par))
12217 then
12218 Set_Controlling_Argument (Par, Empty);
12220 -- Prevent the search from going too far
12222 elsif Is_Body_Or_Package_Declaration (Par) then
12223 exit;
12224 end if;
12226 Par := Parent (Par);
12227 end loop;
12228 end Remove_Controlling_Arguments;
12230 -- Local variables
12232 Context : constant Node_Id :=
12233 (if No (Ref) then Empty else Parent (Ref));
12235 Loc : constant Source_Ptr := Sloc (Ref);
12236 Ref_Id : Entity_Id;
12237 Result : Traverse_Result;
12239 New_Ref : Node_Id;
12240 -- The new reference which is intended to substitute the old one
12242 Old_Ref : Node_Id;
12243 -- The reference designated for replacement. In certain cases this
12244 -- may be a node other than Ref.
12246 Val : Node_Or_Entity_Id;
12247 -- The corresponding value of Ref from the type map
12249 -- Start of processing for Replace_Ref
12251 begin
12252 -- Assume that the input reference is to be replaced and that the
12253 -- traversal should examine the children of the reference.
12255 Old_Ref := Ref;
12256 Result := OK;
12258 -- The input denotes a meaningful reference
12260 if Nkind (Ref) in N_Has_Entity and then Present (Entity (Ref)) then
12261 Ref_Id := Entity (Ref);
12262 Val := Type_Map.Get (Ref_Id);
12264 -- The reference has a corresponding value in the type map, a
12265 -- substitution is possible.
12267 if Present (Val) then
12269 -- The reference denotes a discriminant
12271 if Ekind (Ref_Id) = E_Discriminant then
12272 if Nkind (Val) in N_Entity then
12274 -- The value denotes another discriminant. Replace as
12275 -- follows:
12277 -- _object.Discr -> _object.Val
12279 if Ekind (Val) = E_Discriminant then
12280 New_Ref := New_Occurrence_Of (Val, Loc);
12282 -- Otherwise the value denotes the entity of a name which
12283 -- constraints the discriminant. Replace as follows:
12285 -- _object.Discr -> Val
12287 else
12288 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
12290 New_Ref := New_Occurrence_Of (Val, Loc);
12291 Old_Ref := Parent (Old_Ref);
12292 end if;
12294 -- Otherwise the value denotes an arbitrary expression which
12295 -- constraints the discriminant. Replace as follows:
12297 -- _object.Discr -> Val
12299 else
12300 pragma Assert (Is_Deriv_Obj_Ref (Old_Ref));
12302 New_Ref := New_Copy_Tree (Val);
12303 Old_Ref := Parent (Old_Ref);
12304 end if;
12306 -- Otherwise the reference denotes a primitive. Replace as
12307 -- follows:
12309 -- Primitive -> Val
12311 else
12312 pragma Assert (Nkind (Val) in N_Entity);
12313 New_Ref := New_Occurrence_Of (Val, Loc);
12314 end if;
12316 -- The reference mentions the _object parameter of the parent
12317 -- type's DIC or type invariant procedure. Replace as follows:
12319 -- _object -> _object
12321 elsif Present (Par_Obj)
12322 and then Present (Deriv_Obj)
12323 and then Ref_Id = Par_Obj
12324 then
12325 New_Ref := New_Occurrence_Of (Deriv_Obj, Loc);
12327 -- The type of the _object parameter is class-wide when the
12328 -- expression comes from an assertion pragma that applies to
12329 -- an abstract parent type or an interface. The class-wide type
12330 -- facilitates the preanalysis of the expression by treating
12331 -- calls to abstract primitives that mention the current
12332 -- instance of the type as dispatching. Once the calls are
12333 -- remapped to invoke overriding or inherited primitives, the
12334 -- calls no longer need to be dispatching. Examine all function
12335 -- calls that encapsulate the _object parameter and reset their
12336 -- Controlling_Argument attribute.
12338 if Is_Class_Wide_Type (Etype (Par_Obj))
12339 and then Is_Abstract_Type (Root_Type (Etype (Par_Obj)))
12340 then
12341 Remove_Controlling_Arguments (Old_Ref);
12342 end if;
12344 -- The reference to _object acts as an actual parameter in a
12345 -- subprogram call which may be invoking a primitive of the
12346 -- parent type:
12348 -- Primitive (... _object ...);
12350 -- The parent type primitive may not be overridden nor
12351 -- inherited when it is declared after the derived type
12352 -- definition:
12354 -- type Parent is tagged private;
12355 -- type Child is new Parent with private;
12356 -- procedure Primitive (Obj : Parent);
12358 -- In this scenario the _object parameter is converted to the
12359 -- parent type. Due to complications with partial/full views
12360 -- and view swaps, the parent type is taken from the formal
12361 -- parameter of the subprogram being called.
12363 if Nkind (Context) in N_Subprogram_Call
12364 and then No (Type_Map.Get (Entity (Name (Context))))
12365 then
12366 declare
12367 -- We need to use the Original_Node of the callee, in
12368 -- case it was already modified. Note that we are using
12369 -- Traverse_Proc to walk the tree, and it is defined to
12370 -- walk subtrees in an arbitrary order.
12372 Callee : constant Entity_Id :=
12373 Entity (Original_Node (Name (Context)));
12374 begin
12375 if No (Type_Map.Get (Callee)) then
12376 New_Ref :=
12377 Convert_To
12378 (Type_Of_Formal (Context, Old_Ref), New_Ref);
12380 -- Do not process the generated type conversion
12381 -- because both the parent type and the derived type
12382 -- are in the Type_Map table. This will clobber the
12383 -- type conversion by resetting its subtype mark.
12385 Result := Skip;
12386 end if;
12387 end;
12388 end if;
12390 -- Otherwise there is nothing to replace
12392 else
12393 New_Ref := Empty;
12394 end if;
12396 if Present (New_Ref) then
12397 Rewrite (Old_Ref, New_Ref);
12399 -- Update the return type when the context of the reference
12400 -- acts as the name of a function call. Note that the update
12401 -- should not be performed when the reference appears as an
12402 -- actual in the call.
12404 if Nkind (Context) = N_Function_Call
12405 and then Name (Context) = Old_Ref
12406 then
12407 Set_Etype (Context, Etype (Val));
12408 end if;
12409 end if;
12410 end if;
12412 -- Reanalyze the reference due to potential replacements
12414 if Nkind (Old_Ref) in N_Has_Etype then
12415 Set_Analyzed (Old_Ref, False);
12416 end if;
12418 return Result;
12419 end Replace_Ref;
12421 procedure Replace_Refs is new Traverse_Proc (Replace_Ref);
12423 --------------------
12424 -- Type_Of_Formal --
12425 --------------------
12427 function Type_Of_Formal
12428 (Call : Node_Id;
12429 Actual : Node_Id) return Entity_Id
12431 A : Node_Id;
12432 F : Entity_Id;
12434 begin
12435 -- Examine the list of actual and formal parameters in parallel
12437 A := First (Parameter_Associations (Call));
12438 F := First_Formal (Entity (Name (Call)));
12439 while Present (A) and then Present (F) loop
12440 if A = Actual then
12441 return Etype (F);
12442 end if;
12444 Next (A);
12445 Next_Formal (F);
12446 end loop;
12448 -- The actual parameter must always have a corresponding formal
12450 pragma Assert (False);
12452 return Empty;
12453 end Type_Of_Formal;
12455 -- Start of processing for Replace_References
12457 begin
12458 -- Map the attributes of the parent type to the proper corresponding
12459 -- attributes of the derived type.
12461 Map_Types
12462 (Parent_Type => Par_Typ,
12463 Derived_Type => Deriv_Typ);
12465 -- Inspect the input expression and perform substitutions where
12466 -- necessary.
12468 Replace_Refs (Expr);
12469 end Replace_References;
12471 -----------------------------
12472 -- Replace_Type_References --
12473 -----------------------------
12475 procedure Replace_Type_References
12476 (Expr : Node_Id;
12477 Typ : Entity_Id;
12478 Obj_Id : Entity_Id)
12480 procedure Replace_Type_Ref (N : Node_Id);
12481 -- Substitute a single reference of the current instance of type Typ
12482 -- with a reference to Obj_Id.
12484 ----------------------
12485 -- Replace_Type_Ref --
12486 ----------------------
12488 procedure Replace_Type_Ref (N : Node_Id) is
12489 begin
12490 -- Decorate the reference to Typ even though it may be rewritten
12491 -- further down. This is done so that routines which examine
12492 -- properties of the Original_Node have some semantic information.
12494 if Nkind (N) = N_Identifier then
12495 Set_Entity (N, Typ);
12496 Set_Etype (N, Typ);
12498 elsif Nkind (N) = N_Selected_Component then
12499 Analyze (Prefix (N));
12500 Set_Entity (Selector_Name (N), Typ);
12501 Set_Etype (Selector_Name (N), Typ);
12502 end if;
12504 -- Perform the following substitution:
12506 -- Typ --> _object
12508 Rewrite (N, New_Occurrence_Of (Obj_Id, Sloc (N)));
12509 Set_Comes_From_Source (N, True);
12510 end Replace_Type_Ref;
12512 procedure Replace_Type_Refs is
12513 new Replace_Type_References_Generic (Replace_Type_Ref);
12515 -- Start of processing for Replace_Type_References
12517 begin
12518 Replace_Type_Refs (Expr, Typ);
12519 end Replace_Type_References;
12521 ---------------------------
12522 -- Represented_As_Scalar --
12523 ---------------------------
12525 function Represented_As_Scalar (T : Entity_Id) return Boolean is
12526 UT : constant Entity_Id := Underlying_Type (T);
12527 begin
12528 return Is_Scalar_Type (UT)
12529 or else (Is_Bit_Packed_Array (UT)
12530 and then Is_Scalar_Type (Packed_Array_Impl_Type (UT)));
12531 end Represented_As_Scalar;
12533 ------------------------------
12534 -- Requires_Cleanup_Actions --
12535 ------------------------------
12537 function Requires_Cleanup_Actions
12538 (N : Node_Id;
12539 Lib_Level : Boolean) return Boolean
12541 At_Lib_Level : constant Boolean :=
12542 Lib_Level
12543 and then Nkind (N) in N_Package_Body | N_Package_Specification;
12544 -- N is at the library level if the top-most context is a package and
12545 -- the path taken to reach N does not include nonpackage constructs.
12547 begin
12548 case Nkind (N) is
12549 when N_Accept_Statement
12550 | N_Block_Statement
12551 | N_Entry_Body
12552 | N_Package_Body
12553 | N_Subprogram_Body
12554 | N_Task_Body
12556 return
12557 Requires_Cleanup_Actions
12558 (L => Declarations (N),
12559 Lib_Level => At_Lib_Level,
12560 Nested_Constructs => True)
12561 or else
12562 (Present (Handled_Statement_Sequence (N))
12563 and then
12564 Requires_Cleanup_Actions
12565 (L =>
12566 Statements (Handled_Statement_Sequence (N)),
12567 Lib_Level => At_Lib_Level,
12568 Nested_Constructs => True));
12570 -- Extended return statements are the same as the above, except that
12571 -- there is no Declarations field. We do not want to clean up the
12572 -- Return_Object_Declarations.
12574 when N_Extended_Return_Statement =>
12575 return
12576 Present (Handled_Statement_Sequence (N))
12577 and then Requires_Cleanup_Actions
12578 (L =>
12579 Statements (Handled_Statement_Sequence (N)),
12580 Lib_Level => At_Lib_Level,
12581 Nested_Constructs => True);
12583 when N_Package_Specification =>
12584 return
12585 Requires_Cleanup_Actions
12586 (L => Visible_Declarations (N),
12587 Lib_Level => At_Lib_Level,
12588 Nested_Constructs => True)
12589 or else
12590 Requires_Cleanup_Actions
12591 (L => Private_Declarations (N),
12592 Lib_Level => At_Lib_Level,
12593 Nested_Constructs => True);
12595 when others =>
12596 raise Program_Error;
12597 end case;
12598 end Requires_Cleanup_Actions;
12600 ------------------------------
12601 -- Requires_Cleanup_Actions --
12602 ------------------------------
12604 function Requires_Cleanup_Actions
12605 (L : List_Id;
12606 Lib_Level : Boolean;
12607 Nested_Constructs : Boolean) return Boolean
12609 Decl : Node_Id;
12610 Expr : Node_Id;
12611 Obj_Id : Entity_Id;
12612 Obj_Typ : Entity_Id;
12613 Pack_Id : Entity_Id;
12614 Typ : Entity_Id;
12616 begin
12617 Decl := First (L);
12618 while Present (Decl) loop
12620 -- Library-level tagged types
12622 if Nkind (Decl) = N_Full_Type_Declaration then
12623 Typ := Defining_Identifier (Decl);
12625 -- Ignored Ghost types do not need any cleanup actions because
12626 -- they will not appear in the final tree.
12628 if Is_Ignored_Ghost_Entity (Typ) then
12629 null;
12631 elsif Is_Tagged_Type (Typ)
12632 and then Is_Library_Level_Entity (Typ)
12633 and then Convention (Typ) = Convention_Ada
12634 and then Present (Access_Disp_Table (Typ))
12635 and then not Is_Abstract_Type (Typ)
12636 and then not No_Run_Time_Mode
12637 and then not Restriction_Active (No_Tagged_Type_Registration)
12638 and then RTE_Available (RE_Unregister_Tag)
12639 then
12640 return True;
12641 end if;
12643 -- Regular object declarations
12645 elsif Nkind (Decl) = N_Object_Declaration then
12646 Obj_Id := Defining_Identifier (Decl);
12647 Obj_Typ := Base_Type (Etype (Obj_Id));
12648 Expr := Expression (Decl);
12650 -- Bypass any form of processing for objects which have their
12651 -- finalization disabled. This applies only to objects at the
12652 -- library level.
12654 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12655 null;
12657 -- Finalization of transient objects are treated separately in
12658 -- order to handle sensitive cases. These include:
12660 -- * Aggregate expansion
12661 -- * If, case, and expression with actions expansion
12662 -- * Transient scopes
12664 -- If one of those contexts has marked the transient object as
12665 -- ignored, do not generate finalization actions for it.
12667 elsif Is_Finalized_Transient (Obj_Id)
12668 or else Is_Ignored_Transient (Obj_Id)
12669 then
12670 null;
12672 -- Ignored Ghost objects do not need any cleanup actions because
12673 -- they will not appear in the final tree.
12675 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12676 null;
12678 -- The object is of the form:
12679 -- Obj : [constant] Typ [:= Expr];
12681 -- Do not process the incomplete view of a deferred constant.
12682 -- Note that an object initialized by means of a BIP function
12683 -- call may appear as a deferred constant after expansion
12684 -- activities. These kinds of objects must be finalized.
12686 elsif not Is_Imported (Obj_Id)
12687 and then Needs_Finalization (Obj_Typ)
12688 and then not (Ekind (Obj_Id) = E_Constant
12689 and then not Has_Completion (Obj_Id)
12690 and then No (BIP_Initialization_Call (Obj_Id)))
12691 then
12692 return True;
12694 -- The object is of the form:
12695 -- Obj : Access_Typ := Non_BIP_Function_Call'reference;
12697 -- Obj : Access_Typ :=
12698 -- BIP_Function_Call (BIPalloc => 2, ...)'reference;
12700 elsif Is_Access_Type (Obj_Typ)
12701 and then Needs_Finalization
12702 (Available_View (Designated_Type (Obj_Typ)))
12703 and then Present (Expr)
12704 and then
12705 (Is_Secondary_Stack_BIP_Func_Call (Expr)
12706 or else
12707 (Is_Non_BIP_Func_Call (Expr)
12708 and then not Is_Related_To_Func_Return (Obj_Id)))
12709 then
12710 return True;
12712 -- Processing for "hook" objects generated for transient objects
12713 -- declared inside an Expression_With_Actions.
12715 elsif Is_Access_Type (Obj_Typ)
12716 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12717 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12718 N_Object_Declaration
12719 then
12720 return True;
12722 -- Processing for intermediate results of if expressions where
12723 -- one of the alternatives uses a controlled function call.
12725 elsif Is_Access_Type (Obj_Typ)
12726 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12727 and then Nkind (Status_Flag_Or_Transient_Decl (Obj_Id)) =
12728 N_Defining_Identifier
12729 and then Present (Expr)
12730 and then Nkind (Expr) = N_Null
12731 then
12732 return True;
12734 -- Simple protected objects which use type System.Tasking.
12735 -- Protected_Objects.Protection to manage their locks should be
12736 -- treated as controlled since they require manual cleanup.
12738 elsif Ekind (Obj_Id) = E_Variable
12739 and then (Is_Simple_Protected_Type (Obj_Typ)
12740 or else Has_Simple_Protected_Object (Obj_Typ))
12741 then
12742 return True;
12743 end if;
12745 -- Specific cases of object renamings
12747 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
12748 Obj_Id := Defining_Identifier (Decl);
12749 Obj_Typ := Base_Type (Etype (Obj_Id));
12751 -- Bypass any form of processing for objects which have their
12752 -- finalization disabled. This applies only to objects at the
12753 -- library level.
12755 if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then
12756 null;
12758 -- Ignored Ghost object renamings do not need any cleanup actions
12759 -- because they will not appear in the final tree.
12761 elsif Is_Ignored_Ghost_Entity (Obj_Id) then
12762 null;
12764 -- Return object of a build-in-place function. This case is
12765 -- recognized and marked by the expansion of an extended return
12766 -- statement (see Expand_N_Extended_Return_Statement).
12768 elsif Needs_Finalization (Obj_Typ)
12769 and then Is_Return_Object (Obj_Id)
12770 and then Present (Status_Flag_Or_Transient_Decl (Obj_Id))
12771 then
12772 return True;
12773 end if;
12775 -- Inspect the freeze node of an access-to-controlled type and look
12776 -- for a delayed finalization master. This case arises when the
12777 -- freeze actions are inserted at a later time than the expansion of
12778 -- the context. Since Build_Finalizer is never called on a single
12779 -- construct twice, the master will be ultimately left out and never
12780 -- finalized. This is also needed for freeze actions of designated
12781 -- types themselves, since in some cases the finalization master is
12782 -- associated with a designated type's freeze node rather than that
12783 -- of the access type (see handling for freeze actions in
12784 -- Build_Finalization_Master).
12786 elsif Nkind (Decl) = N_Freeze_Entity
12787 and then Present (Actions (Decl))
12788 then
12789 Typ := Entity (Decl);
12791 -- Freeze nodes for ignored Ghost types do not need cleanup
12792 -- actions because they will never appear in the final tree.
12794 if Is_Ignored_Ghost_Entity (Typ) then
12795 null;
12797 elsif ((Is_Access_Object_Type (Typ)
12798 and then Needs_Finalization
12799 (Available_View (Designated_Type (Typ))))
12800 or else (Is_Type (Typ) and then Needs_Finalization (Typ)))
12801 and then Requires_Cleanup_Actions
12802 (Actions (Decl), Lib_Level, Nested_Constructs)
12803 then
12804 return True;
12805 end if;
12807 -- Nested package declarations
12809 elsif Nested_Constructs
12810 and then Nkind (Decl) = N_Package_Declaration
12811 then
12812 Pack_Id := Defining_Entity (Decl);
12814 -- Do not inspect an ignored Ghost package because all code found
12815 -- within will not appear in the final tree.
12817 if Is_Ignored_Ghost_Entity (Pack_Id) then
12818 null;
12820 elsif Ekind (Pack_Id) /= E_Generic_Package
12821 and then Requires_Cleanup_Actions
12822 (Specification (Decl), Lib_Level)
12823 then
12824 return True;
12825 end if;
12827 -- Nested package bodies
12829 elsif Nested_Constructs and then Nkind (Decl) = N_Package_Body then
12831 -- Do not inspect an ignored Ghost package body because all code
12832 -- found within will not appear in the final tree.
12834 if Is_Ignored_Ghost_Entity (Defining_Entity (Decl)) then
12835 null;
12837 elsif Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12838 and then Requires_Cleanup_Actions (Decl, Lib_Level)
12839 then
12840 return True;
12841 end if;
12843 elsif Nkind (Decl) = N_Block_Statement
12844 and then
12846 -- Handle a rare case caused by a controlled transient object
12847 -- created as part of a record init proc. The variable is wrapped
12848 -- in a block, but the block is not associated with a transient
12849 -- scope.
12851 (Inside_Init_Proc
12853 -- Handle the case where the original context has been wrapped in
12854 -- a block to avoid interference between exception handlers and
12855 -- At_End handlers. Treat the block as transparent and process its
12856 -- contents.
12858 or else Is_Finalization_Wrapper (Decl))
12859 then
12860 if Requires_Cleanup_Actions (Decl, Lib_Level) then
12861 return True;
12862 end if;
12863 end if;
12865 Next (Decl);
12866 end loop;
12868 return False;
12869 end Requires_Cleanup_Actions;
12871 ------------------------------------
12872 -- Safe_Unchecked_Type_Conversion --
12873 ------------------------------------
12875 -- Note: this function knows quite a bit about the exact requirements of
12876 -- Gigi with respect to unchecked type conversions, and its code must be
12877 -- coordinated with any changes in Gigi in this area.
12879 -- The above requirements should be documented in Sinfo ???
12881 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
12882 Otyp : Entity_Id;
12883 Ityp : Entity_Id;
12884 Oalign : Uint;
12885 Ialign : Uint;
12886 Pexp : constant Node_Id := Parent (Exp);
12888 begin
12889 -- If the expression is the RHS of an assignment or object declaration
12890 -- we are always OK because there will always be a target.
12892 -- Object renaming declarations, (generated for view conversions of
12893 -- actuals in inlined calls), like object declarations, provide an
12894 -- explicit type, and are safe as well.
12896 if (Nkind (Pexp) = N_Assignment_Statement
12897 and then Expression (Pexp) = Exp)
12898 or else Nkind (Pexp)
12899 in N_Object_Declaration | N_Object_Renaming_Declaration
12900 then
12901 return True;
12903 -- If the expression is the prefix of an N_Selected_Component we should
12904 -- also be OK because GCC knows to look inside the conversion except if
12905 -- the type is discriminated. We assume that we are OK anyway if the
12906 -- type is not set yet or if it is controlled since we can't afford to
12907 -- introduce a temporary in this case.
12909 elsif Nkind (Pexp) = N_Selected_Component
12910 and then Prefix (Pexp) = Exp
12911 then
12912 return No (Etype (Pexp))
12913 or else not Is_Type (Etype (Pexp))
12914 or else not Has_Discriminants (Etype (Pexp))
12915 or else Is_Constrained (Etype (Pexp));
12916 end if;
12918 -- Set the output type, this comes from Etype if it is set, otherwise we
12919 -- take it from the subtype mark, which we assume was already fully
12920 -- analyzed.
12922 if Present (Etype (Exp)) then
12923 Otyp := Etype (Exp);
12924 else
12925 Otyp := Entity (Subtype_Mark (Exp));
12926 end if;
12928 -- The input type always comes from the expression, and we assume this
12929 -- is indeed always analyzed, so we can simply get the Etype.
12931 Ityp := Etype (Expression (Exp));
12933 -- Initialize alignments to unknown so far
12935 Oalign := No_Uint;
12936 Ialign := No_Uint;
12938 -- Replace a concurrent type by its corresponding record type and each
12939 -- type by its underlying type and do the tests on those. The original
12940 -- type may be a private type whose completion is a concurrent type, so
12941 -- find the underlying type first.
12943 if Present (Underlying_Type (Otyp)) then
12944 Otyp := Underlying_Type (Otyp);
12945 end if;
12947 if Present (Underlying_Type (Ityp)) then
12948 Ityp := Underlying_Type (Ityp);
12949 end if;
12951 if Is_Concurrent_Type (Otyp) then
12952 Otyp := Corresponding_Record_Type (Otyp);
12953 end if;
12955 if Is_Concurrent_Type (Ityp) then
12956 Ityp := Corresponding_Record_Type (Ityp);
12957 end if;
12959 -- If the base types are the same, we know there is no problem since
12960 -- this conversion will be a noop.
12962 if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
12963 return True;
12965 -- Same if this is an upwards conversion of an untagged type, and there
12966 -- are no constraints involved (could be more general???)
12968 elsif Etype (Ityp) = Otyp
12969 and then not Is_Tagged_Type (Ityp)
12970 and then not Has_Discriminants (Ityp)
12971 and then No (First_Rep_Item (Base_Type (Ityp)))
12972 then
12973 return True;
12975 -- If the expression has an access type (object or subprogram) we assume
12976 -- that the conversion is safe, because the size of the target is safe,
12977 -- even if it is a record (which might be treated as having unknown size
12978 -- at this point).
12980 elsif Is_Access_Type (Ityp) then
12981 return True;
12983 -- If the size of output type is known at compile time, there is never
12984 -- a problem. Note that unconstrained records are considered to be of
12985 -- known size, but we can't consider them that way here, because we are
12986 -- talking about the actual size of the object.
12988 -- We also make sure that in addition to the size being known, we do not
12989 -- have a case which might generate an embarrassingly large temp in
12990 -- stack checking mode.
12992 elsif Size_Known_At_Compile_Time (Otyp)
12993 and then
12994 (not Stack_Checking_Enabled
12995 or else not May_Generate_Large_Temp (Otyp))
12996 and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
12997 then
12998 return True;
13000 -- If either type is tagged, then we know the alignment is OK so Gigi
13001 -- will be able to use pointer punning.
13003 elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
13004 return True;
13006 -- If either type is a limited record type, we cannot do a copy, so say
13007 -- safe since there's nothing else we can do.
13009 elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
13010 return True;
13012 -- Conversions to and from packed array types are always ignored and
13013 -- hence are safe.
13015 elsif Is_Packed_Array_Impl_Type (Otyp)
13016 or else Is_Packed_Array_Impl_Type (Ityp)
13017 then
13018 return True;
13019 end if;
13021 -- The only other cases known to be safe is if the input type's
13022 -- alignment is known to be at least the maximum alignment for the
13023 -- target or if both alignments are known and the output type's
13024 -- alignment is no stricter than the input's. We can use the component
13025 -- type alignment for an array if a type is an unpacked array type.
13027 if Present (Alignment_Clause (Otyp)) then
13028 Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
13030 elsif Is_Array_Type (Otyp)
13031 and then Present (Alignment_Clause (Component_Type (Otyp)))
13032 then
13033 Oalign := Expr_Value (Expression (Alignment_Clause
13034 (Component_Type (Otyp))));
13035 end if;
13037 if Present (Alignment_Clause (Ityp)) then
13038 Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
13040 elsif Is_Array_Type (Ityp)
13041 and then Present (Alignment_Clause (Component_Type (Ityp)))
13042 then
13043 Ialign := Expr_Value (Expression (Alignment_Clause
13044 (Component_Type (Ityp))));
13045 end if;
13047 if Present (Ialign) and then Ialign > Maximum_Alignment then
13048 return True;
13050 elsif Present (Ialign)
13051 and then Present (Oalign)
13052 and then Ialign <= Oalign
13053 then
13054 return True;
13056 -- Otherwise, Gigi cannot handle this and we must make a temporary
13058 else
13059 return False;
13060 end if;
13061 end Safe_Unchecked_Type_Conversion;
13063 ---------------------------------
13064 -- Set_Current_Value_Condition --
13065 ---------------------------------
13067 -- Note: the implementation of this procedure is very closely tied to the
13068 -- implementation of Get_Current_Value_Condition. Here we set required
13069 -- Current_Value fields, and in Get_Current_Value_Condition, we interpret
13070 -- them, so they must have a consistent view.
13072 procedure Set_Current_Value_Condition (Cnode : Node_Id) is
13074 procedure Set_Entity_Current_Value (N : Node_Id);
13075 -- If N is an entity reference, where the entity is of an appropriate
13076 -- kind, then set the current value of this entity to Cnode, unless
13077 -- there is already a definite value set there.
13079 procedure Set_Expression_Current_Value (N : Node_Id);
13080 -- If N is of an appropriate form, sets an appropriate entry in current
13081 -- value fields of relevant entities. Multiple entities can be affected
13082 -- in the case of an AND or AND THEN.
13084 ------------------------------
13085 -- Set_Entity_Current_Value --
13086 ------------------------------
13088 procedure Set_Entity_Current_Value (N : Node_Id) is
13089 begin
13090 if Is_Entity_Name (N) then
13091 declare
13092 Ent : constant Entity_Id := Entity (N);
13094 begin
13095 -- Don't capture if not safe to do so
13097 if not Safe_To_Capture_Value (N, Ent, Cond => True) then
13098 return;
13099 end if;
13101 -- Here we have a case where the Current_Value field may need
13102 -- to be set. We set it if it is not already set to a compile
13103 -- time expression value.
13105 -- Note that this represents a decision that one condition
13106 -- blots out another previous one. That's certainly right if
13107 -- they occur at the same level. If the second one is nested,
13108 -- then the decision is neither right nor wrong (it would be
13109 -- equally OK to leave the outer one in place, or take the new
13110 -- inner one). Really we should record both, but our data
13111 -- structures are not that elaborate.
13113 if Nkind (Current_Value (Ent)) not in N_Subexpr then
13114 Set_Current_Value (Ent, Cnode);
13115 end if;
13116 end;
13117 end if;
13118 end Set_Entity_Current_Value;
13120 ----------------------------------
13121 -- Set_Expression_Current_Value --
13122 ----------------------------------
13124 procedure Set_Expression_Current_Value (N : Node_Id) is
13125 Cond : Node_Id;
13127 begin
13128 Cond := N;
13130 -- Loop to deal with (ignore for now) any NOT operators present. The
13131 -- presence of NOT operators will be handled properly when we call
13132 -- Get_Current_Value_Condition.
13134 while Nkind (Cond) = N_Op_Not loop
13135 Cond := Right_Opnd (Cond);
13136 end loop;
13138 -- For an AND or AND THEN, recursively process operands
13140 if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
13141 Set_Expression_Current_Value (Left_Opnd (Cond));
13142 Set_Expression_Current_Value (Right_Opnd (Cond));
13143 return;
13144 end if;
13146 -- Check possible relational operator
13148 if Nkind (Cond) in N_Op_Compare then
13149 if Compile_Time_Known_Value (Right_Opnd (Cond)) then
13150 Set_Entity_Current_Value (Left_Opnd (Cond));
13151 elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
13152 Set_Entity_Current_Value (Right_Opnd (Cond));
13153 end if;
13155 elsif Nkind (Cond) in N_Type_Conversion
13156 | N_Qualified_Expression
13157 | N_Expression_With_Actions
13158 then
13159 Set_Expression_Current_Value (Expression (Cond));
13161 -- Check possible boolean variable reference
13163 else
13164 Set_Entity_Current_Value (Cond);
13165 end if;
13166 end Set_Expression_Current_Value;
13168 -- Start of processing for Set_Current_Value_Condition
13170 begin
13171 Set_Expression_Current_Value (Condition (Cnode));
13172 end Set_Current_Value_Condition;
13174 --------------------------
13175 -- Set_Elaboration_Flag --
13176 --------------------------
13178 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
13179 Loc : constant Source_Ptr := Sloc (N);
13180 Ent : constant Entity_Id := Elaboration_Entity (Spec_Id);
13181 Asn : Node_Id;
13183 begin
13184 if Present (Ent) then
13186 -- Nothing to do if at the compilation unit level, because in this
13187 -- case the flag is set by the binder generated elaboration routine.
13189 if Nkind (Parent (N)) = N_Compilation_Unit then
13190 null;
13192 -- Here we do need to generate an assignment statement
13194 else
13195 Check_Restriction (No_Elaboration_Code, N);
13197 Asn :=
13198 Make_Assignment_Statement (Loc,
13199 Name => New_Occurrence_Of (Ent, Loc),
13200 Expression => Make_Integer_Literal (Loc, Uint_1));
13202 -- Mark the assignment statement as elaboration code. This allows
13203 -- the early call region mechanism (see Sem_Elab) to properly
13204 -- ignore such assignments even though they are nonpreelaborable
13205 -- code.
13207 Set_Is_Elaboration_Code (Asn);
13209 if Nkind (Parent (N)) = N_Subunit then
13210 Insert_After (Corresponding_Stub (Parent (N)), Asn);
13211 else
13212 Insert_After (N, Asn);
13213 end if;
13215 Analyze (Asn);
13217 -- Kill current value indication. This is necessary because the
13218 -- tests of this flag are inserted out of sequence and must not
13219 -- pick up bogus indications of the wrong constant value.
13221 Set_Current_Value (Ent, Empty);
13223 -- If the subprogram is in the current declarative part and
13224 -- 'access has been applied to it, generate an elaboration
13225 -- check at the beginning of the declarations of the body.
13227 if Nkind (N) = N_Subprogram_Body
13228 and then Address_Taken (Spec_Id)
13229 and then
13230 Ekind (Scope (Spec_Id)) in E_Block | E_Procedure | E_Function
13231 then
13232 declare
13233 Loc : constant Source_Ptr := Sloc (N);
13234 Decls : constant List_Id := Declarations (N);
13235 Chk : Node_Id;
13237 begin
13238 -- No need to generate this check if first entry in the
13239 -- declaration list is a raise of Program_Error now.
13241 if Present (Decls)
13242 and then Nkind (First (Decls)) = N_Raise_Program_Error
13243 then
13244 return;
13245 end if;
13247 -- Otherwise generate the check
13249 Chk :=
13250 Make_Raise_Program_Error (Loc,
13251 Condition =>
13252 Make_Op_Eq (Loc,
13253 Left_Opnd => New_Occurrence_Of (Ent, Loc),
13254 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
13255 Reason => PE_Access_Before_Elaboration);
13257 if No (Decls) then
13258 Set_Declarations (N, New_List (Chk));
13259 else
13260 Prepend (Chk, Decls);
13261 end if;
13263 Analyze (Chk);
13264 end;
13265 end if;
13266 end if;
13267 end if;
13268 end Set_Elaboration_Flag;
13270 ----------------------------
13271 -- Set_Renamed_Subprogram --
13272 ----------------------------
13274 procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
13275 begin
13276 -- If input node is an identifier, we can just reset it
13278 if Nkind (N) = N_Identifier then
13279 Set_Chars (N, Chars (E));
13280 Set_Entity (N, E);
13282 -- Otherwise we have to do a rewrite, preserving Comes_From_Source
13284 else
13285 declare
13286 CS : constant Boolean := Comes_From_Source (N);
13287 begin
13288 Rewrite (N, Make_Identifier (Sloc (N), Chars (E)));
13289 Set_Entity (N, E);
13290 Set_Comes_From_Source (N, CS);
13291 Set_Analyzed (N, True);
13292 end;
13293 end if;
13294 end Set_Renamed_Subprogram;
13296 ----------------------
13297 -- Side_Effect_Free --
13298 ----------------------
13300 function Side_Effect_Free
13301 (N : Node_Id;
13302 Name_Req : Boolean := False;
13303 Variable_Ref : Boolean := False) return Boolean
13305 Typ : constant Entity_Id := Etype (N);
13306 -- Result type of the expression
13308 function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
13309 -- The argument N is a construct where the Prefix is dereferenced if it
13310 -- is an access type and the result is a variable. The call returns True
13311 -- if the construct is side effect free (not considering side effects in
13312 -- other than the prefix which are to be tested by the caller).
13314 function Within_In_Parameter (N : Node_Id) return Boolean;
13315 -- Determines if N is a subcomponent of a composite in-parameter. If so,
13316 -- N is not side-effect free when the actual is global and modifiable
13317 -- indirectly from within a subprogram, because it may be passed by
13318 -- reference. The front-end must be conservative here and assume that
13319 -- this may happen with any array or record type. On the other hand, we
13320 -- cannot create temporaries for all expressions for which this
13321 -- condition is true, for various reasons that might require clearing up
13322 -- ??? For example, discriminant references that appear out of place, or
13323 -- spurious type errors with class-wide expressions. As a result, we
13324 -- limit the transformation to loop bounds, which is so far the only
13325 -- case that requires it.
13327 -----------------------------
13328 -- Safe_Prefixed_Reference --
13329 -----------------------------
13331 function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
13332 begin
13333 -- If prefix is not side effect free, definitely not safe
13335 if not Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref) then
13336 return False;
13338 -- If the prefix is of an access type that is not access-to-constant,
13339 -- then this construct is a variable reference, which means it is to
13340 -- be considered to have side effects if Variable_Ref is set True.
13342 elsif Is_Access_Type (Etype (Prefix (N)))
13343 and then not Is_Access_Constant (Etype (Prefix (N)))
13344 and then Variable_Ref
13345 then
13346 -- Exception is a prefix that is the result of a previous removal
13347 -- of side effects.
13349 return Is_Entity_Name (Prefix (N))
13350 and then not Comes_From_Source (Prefix (N))
13351 and then Ekind (Entity (Prefix (N))) = E_Constant
13352 and then Is_Internal_Name (Chars (Entity (Prefix (N))));
13354 -- If the prefix is an explicit dereference then this construct is a
13355 -- variable reference, which means it is to be considered to have
13356 -- side effects if Variable_Ref is True.
13358 -- We do NOT exclude dereferences of access-to-constant types because
13359 -- we handle them as constant view of variables.
13361 elsif Nkind (Prefix (N)) = N_Explicit_Dereference
13362 and then Variable_Ref
13363 then
13364 return False;
13366 -- Note: The following test is the simplest way of solving a complex
13367 -- problem uncovered by the following test (Side effect on loop bound
13368 -- that is a subcomponent of a global variable:
13370 -- with Text_Io; use Text_Io;
13371 -- procedure Tloop is
13372 -- type X is
13373 -- record
13374 -- V : Natural := 4;
13375 -- S : String (1..5) := (others => 'a');
13376 -- end record;
13377 -- X1 : X;
13379 -- procedure Modi;
13381 -- generic
13382 -- with procedure Action;
13383 -- procedure Loop_G (Arg : X; Msg : String)
13385 -- procedure Loop_G (Arg : X; Msg : String) is
13386 -- begin
13387 -- Put_Line ("begin loop_g " & Msg & " will loop till: "
13388 -- & Natural'Image (Arg.V));
13389 -- for Index in 1 .. Arg.V loop
13390 -- Text_Io.Put_Line
13391 -- (Natural'Image (Index) & " " & Arg.S (Index));
13392 -- if Index > 2 then
13393 -- Modi;
13394 -- end if;
13395 -- end loop;
13396 -- Put_Line ("end loop_g " & Msg);
13397 -- end;
13399 -- procedure Loop1 is new Loop_G (Modi);
13400 -- procedure Modi is
13401 -- begin
13402 -- X1.V := 1;
13403 -- Loop1 (X1, "from modi");
13404 -- end;
13406 -- begin
13407 -- Loop1 (X1, "initial");
13408 -- end;
13410 -- The output of the above program should be:
13412 -- begin loop_g initial will loop till: 4
13413 -- 1 a
13414 -- 2 a
13415 -- 3 a
13416 -- begin loop_g from modi will loop till: 1
13417 -- 1 a
13418 -- end loop_g from modi
13419 -- 4 a
13420 -- begin loop_g from modi will loop till: 1
13421 -- 1 a
13422 -- end loop_g from modi
13423 -- end loop_g initial
13425 -- If a loop bound is a subcomponent of a global variable, a
13426 -- modification of that variable within the loop may incorrectly
13427 -- affect the execution of the loop.
13429 elsif Parent_Kind (Parent (N)) = N_Loop_Parameter_Specification
13430 and then Within_In_Parameter (Prefix (N))
13431 and then Variable_Ref
13432 then
13433 return False;
13435 -- All other cases are side effect free
13437 else
13438 return True;
13439 end if;
13440 end Safe_Prefixed_Reference;
13442 -------------------------
13443 -- Within_In_Parameter --
13444 -------------------------
13446 function Within_In_Parameter (N : Node_Id) return Boolean is
13447 begin
13448 if not Comes_From_Source (N) then
13449 return False;
13451 elsif Is_Entity_Name (N) then
13452 return Ekind (Entity (N)) = E_In_Parameter;
13454 elsif Nkind (N) in N_Indexed_Component | N_Selected_Component then
13455 return Within_In_Parameter (Prefix (N));
13457 else
13458 return False;
13459 end if;
13460 end Within_In_Parameter;
13462 -- Start of processing for Side_Effect_Free
13464 begin
13465 -- If volatile reference, always consider it to have side effects
13467 if Is_Volatile_Reference (N) then
13468 return False;
13469 end if;
13471 -- Note on checks that could raise Constraint_Error. Strictly, if we
13472 -- take advantage of 11.6, these checks do not count as side effects.
13473 -- However, we would prefer to consider that they are side effects,
13474 -- since the back end CSE does not work very well on expressions which
13475 -- can raise Constraint_Error. On the other hand if we don't consider
13476 -- them to be side effect free, then we get some awkward expansions
13477 -- in -gnato mode, resulting in code insertions at a point where we
13478 -- do not have a clear model for performing the insertions.
13480 -- Special handling for entity names
13482 if Is_Entity_Name (N) then
13484 -- A type reference is always side effect free
13486 if Is_Type (Entity (N)) then
13487 return True;
13489 -- Variables are considered to be a side effect if Variable_Ref
13490 -- is set or if we have a volatile reference and Name_Req is off.
13491 -- If Name_Req is True then we can't help returning a name which
13492 -- effectively allows multiple references in any case.
13494 elsif Is_Variable (N, Use_Original_Node => False) then
13495 return not Variable_Ref
13496 and then (not Is_Volatile_Reference (N) or else Name_Req);
13498 -- Any other entity (e.g. a subtype name) is definitely side
13499 -- effect free.
13501 else
13502 return True;
13503 end if;
13505 -- A value known at compile time is always side effect free
13507 elsif Compile_Time_Known_Value (N) then
13508 return True;
13510 -- A variable renaming is not side-effect free, because the renaming
13511 -- will function like a macro in the front-end in some cases, and an
13512 -- assignment can modify the component designated by N, so we need to
13513 -- create a temporary for it.
13515 -- The guard testing for Entity being present is needed at least in
13516 -- the case of rewritten predicate expressions, and may well also be
13517 -- appropriate elsewhere. Obviously we can't go testing the entity
13518 -- field if it does not exist, so it's reasonable to say that this is
13519 -- not the renaming case if it does not exist.
13521 elsif Is_Entity_Name (Original_Node (N))
13522 and then Present (Entity (Original_Node (N)))
13523 and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
13524 and then Ekind (Entity (Original_Node (N))) /= E_Constant
13525 then
13526 declare
13527 RO : constant Node_Id :=
13528 Renamed_Object (Entity (Original_Node (N)));
13530 begin
13531 -- If the renamed object is an indexed component, or an
13532 -- explicit dereference, then the designated object could
13533 -- be modified by an assignment.
13535 if Nkind (RO) in N_Indexed_Component | N_Explicit_Dereference then
13536 return False;
13538 -- A selected component must have a safe prefix
13540 elsif Nkind (RO) = N_Selected_Component then
13541 return Safe_Prefixed_Reference (RO);
13543 -- In all other cases, designated object cannot be changed so
13544 -- we are side effect free.
13546 else
13547 return True;
13548 end if;
13549 end;
13551 -- Remove_Side_Effects generates an object renaming declaration to
13552 -- capture the expression of a class-wide expression. In VM targets
13553 -- the frontend performs no expansion for dispatching calls to
13554 -- class- wide types since they are handled by the VM. Hence, we must
13555 -- locate here if this node corresponds to a previous invocation of
13556 -- Remove_Side_Effects to avoid a never ending loop in the frontend.
13558 elsif not Tagged_Type_Expansion
13559 and then not Comes_From_Source (N)
13560 and then Nkind (Parent (N)) = N_Object_Renaming_Declaration
13561 and then Is_Class_Wide_Type (Typ)
13562 then
13563 return True;
13565 -- Generating C the type conversion of an access to constrained array
13566 -- type into an access to unconstrained array type involves initializing
13567 -- a fat pointer and the expression cannot be assumed to be free of side
13568 -- effects since it must referenced several times to compute its bounds.
13570 elsif Modify_Tree_For_C
13571 and then Nkind (N) = N_Type_Conversion
13572 and then Is_Access_Type (Typ)
13573 and then Is_Array_Type (Designated_Type (Typ))
13574 and then not Is_Constrained (Designated_Type (Typ))
13575 then
13576 return False;
13577 end if;
13579 -- For other than entity names and compile time known values,
13580 -- check the node kind for special processing.
13582 case Nkind (N) is
13584 -- An attribute reference is side-effect free if its expressions
13585 -- are side-effect free and its prefix is side-effect free or is
13586 -- an entity reference.
13588 when N_Attribute_Reference =>
13589 return Side_Effect_Free_Attribute (Attribute_Name (N))
13590 and then
13591 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13592 and then
13593 (Is_Entity_Name (Prefix (N))
13594 or else
13595 Side_Effect_Free (Prefix (N), Name_Req, Variable_Ref));
13597 -- A binary operator is side effect free if and both operands are
13598 -- side effect free. For this purpose binary operators include
13599 -- short circuit forms.
13601 when N_Binary_Op
13602 | N_Short_Circuit
13604 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13605 and then
13606 Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13608 -- Membership tests may have either Right_Opnd or Alternatives set
13610 when N_Membership_Test =>
13611 return Side_Effect_Free (Left_Opnd (N), Name_Req, Variable_Ref)
13612 and then
13613 (if Present (Right_Opnd (N))
13614 then Side_Effect_Free
13615 (Right_Opnd (N), Name_Req, Variable_Ref)
13616 else Side_Effect_Free
13617 (Alternatives (N), Name_Req, Variable_Ref));
13619 -- An explicit dereference is side effect free only if it is
13620 -- a side effect free prefixed reference.
13622 when N_Explicit_Dereference =>
13623 return Safe_Prefixed_Reference (N);
13625 -- An expression with action is side effect free if its expression
13626 -- is side effect free and it has no actions.
13628 when N_Expression_With_Actions =>
13629 return
13630 Is_Empty_List (Actions (N))
13631 and then Side_Effect_Free
13632 (Expression (N), Name_Req, Variable_Ref);
13634 -- A call to _rep_to_pos is side effect free, since we generate
13635 -- this pure function call ourselves. Moreover it is critically
13636 -- important to make this exception, since otherwise we can have
13637 -- discriminants in array components which don't look side effect
13638 -- free in the case of an array whose index type is an enumeration
13639 -- type with an enumeration rep clause.
13641 -- All other function calls are not side effect free
13643 when N_Function_Call =>
13644 return
13645 Nkind (Name (N)) = N_Identifier
13646 and then Is_TSS (Name (N), TSS_Rep_To_Pos)
13647 and then Side_Effect_Free
13648 (First (Parameter_Associations (N)),
13649 Name_Req, Variable_Ref);
13651 -- An IF expression is side effect free if it's of a scalar type, and
13652 -- all its components are all side effect free (conditions and then
13653 -- actions and else actions). We restrict to scalar types, since it
13654 -- is annoying to deal with things like (if A then B else C)'First
13655 -- where the type involved is a string type.
13657 when N_If_Expression =>
13658 return
13659 Is_Scalar_Type (Typ)
13660 and then Side_Effect_Free
13661 (Expressions (N), Name_Req, Variable_Ref);
13663 -- An indexed component is side effect free if it is a side
13664 -- effect free prefixed reference and all the indexing
13665 -- expressions are side effect free.
13667 when N_Indexed_Component =>
13668 return
13669 Side_Effect_Free (Expressions (N), Name_Req, Variable_Ref)
13670 and then Safe_Prefixed_Reference (N);
13672 -- A type qualification, type conversion, or unchecked expression is
13673 -- side effect free if the expression is side effect free.
13675 when N_Qualified_Expression
13676 | N_Type_Conversion
13677 | N_Unchecked_Expression
13679 return Side_Effect_Free (Expression (N), Name_Req, Variable_Ref);
13681 -- A selected component is side effect free only if it is a side
13682 -- effect free prefixed reference.
13684 when N_Selected_Component =>
13685 return Safe_Prefixed_Reference (N);
13687 -- A range is side effect free if the bounds are side effect free
13689 when N_Range =>
13690 return Side_Effect_Free (Low_Bound (N), Name_Req, Variable_Ref)
13691 and then
13692 Side_Effect_Free (High_Bound (N), Name_Req, Variable_Ref);
13694 -- A slice is side effect free if it is a side effect free
13695 -- prefixed reference and the bounds are side effect free.
13697 when N_Slice =>
13698 return
13699 Side_Effect_Free (Discrete_Range (N), Name_Req, Variable_Ref)
13700 and then Safe_Prefixed_Reference (N);
13702 -- A unary operator is side effect free if the operand
13703 -- is side effect free.
13705 when N_Unary_Op =>
13706 return Side_Effect_Free (Right_Opnd (N), Name_Req, Variable_Ref);
13708 -- An unchecked type conversion is side effect free only if it
13709 -- is safe and its argument is side effect free.
13711 when N_Unchecked_Type_Conversion =>
13712 return
13713 Safe_Unchecked_Type_Conversion (N)
13714 and then Side_Effect_Free
13715 (Expression (N), Name_Req, Variable_Ref);
13717 -- A literal is side effect free
13719 when N_Character_Literal
13720 | N_Integer_Literal
13721 | N_Real_Literal
13722 | N_String_Literal
13724 return True;
13726 -- An aggregate is side effect free if all its values are compile
13727 -- time known.
13729 when N_Aggregate =>
13730 return Compile_Time_Known_Aggregate (N);
13732 -- We consider that anything else has side effects. This is a bit
13733 -- crude, but we are pretty close for most common cases, and we
13734 -- are certainly correct (i.e. we never return True when the
13735 -- answer should be False).
13737 when others =>
13738 return False;
13739 end case;
13740 end Side_Effect_Free;
13742 -- A list is side effect free if all elements of the list are side
13743 -- effect free.
13745 function Side_Effect_Free
13746 (L : List_Id;
13747 Name_Req : Boolean := False;
13748 Variable_Ref : Boolean := False) return Boolean
13750 N : Node_Id;
13752 begin
13753 if L = No_List or else L = Error_List then
13754 return True;
13756 else
13757 N := First (L);
13758 while Present (N) loop
13759 if not Side_Effect_Free (N, Name_Req, Variable_Ref) then
13760 return False;
13761 else
13762 Next (N);
13763 end if;
13764 end loop;
13766 return True;
13767 end if;
13768 end Side_Effect_Free;
13770 --------------------------------
13771 -- Side_Effect_Free_Attribute --
13772 --------------------------------
13774 function Side_Effect_Free_Attribute (Name : Name_Id) return Boolean is
13775 begin
13776 case Name is
13777 when Name_Input =>
13778 return False;
13780 when Name_Image
13781 | Name_Img
13782 | Name_Wide_Image
13783 | Name_Wide_Wide_Image
13785 -- CodePeer doesn't want to see replicated copies of 'Image calls
13787 return not CodePeer_Mode;
13789 when others =>
13790 return True;
13791 end case;
13792 end Side_Effect_Free_Attribute;
13794 ----------------------------------
13795 -- Silly_Boolean_Array_Not_Test --
13796 ----------------------------------
13798 -- This procedure implements an odd and silly test. We explicitly check
13799 -- for the case where the 'First of the component type is equal to the
13800 -- 'Last of this component type, and if this is the case, we make sure
13801 -- that constraint error is raised. The reason is that the NOT is bound
13802 -- to cause CE in this case, and we will not otherwise catch it.
13804 -- No such check is required for AND and OR, since for both these cases
13805 -- False op False = False, and True op True = True. For the XOR case,
13806 -- see Silly_Boolean_Array_Xor_Test.
13808 -- Believe it or not, this was reported as a bug. Note that nearly always,
13809 -- the test will evaluate statically to False, so the code will be
13810 -- statically removed, and no extra overhead caused.
13812 procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
13813 Loc : constant Source_Ptr := Sloc (N);
13814 CT : constant Entity_Id := Component_Type (T);
13816 begin
13817 -- The check we install is
13819 -- constraint_error when
13820 -- component_type'first = component_type'last
13821 -- and then array_type'Length /= 0)
13823 -- We need the last guard because we don't want to raise CE for empty
13824 -- arrays since no out of range values result. (Empty arrays with a
13825 -- component type of True .. True -- very useful -- even the ACATS
13826 -- does not test that marginal case).
13828 Insert_Action (N,
13829 Make_Raise_Constraint_Error (Loc,
13830 Condition =>
13831 Make_And_Then (Loc,
13832 Left_Opnd =>
13833 Make_Op_Eq (Loc,
13834 Left_Opnd =>
13835 Make_Attribute_Reference (Loc,
13836 Prefix => New_Occurrence_Of (CT, Loc),
13837 Attribute_Name => Name_First),
13839 Right_Opnd =>
13840 Make_Attribute_Reference (Loc,
13841 Prefix => New_Occurrence_Of (CT, Loc),
13842 Attribute_Name => Name_Last)),
13844 Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
13845 Reason => CE_Range_Check_Failed));
13846 end Silly_Boolean_Array_Not_Test;
13848 ----------------------------------
13849 -- Silly_Boolean_Array_Xor_Test --
13850 ----------------------------------
13852 -- This procedure implements an odd and silly test. We explicitly check
13853 -- for the XOR case where the component type is True .. True, since this
13854 -- will raise constraint error. A special check is required since CE
13855 -- will not be generated otherwise (cf Expand_Packed_Not).
13857 -- No such check is required for AND and OR, since for both these cases
13858 -- False op False = False, and True op True = True, and no check is
13859 -- required for the case of False .. False, since False xor False = False.
13860 -- See also Silly_Boolean_Array_Not_Test
13862 procedure Silly_Boolean_Array_Xor_Test
13863 (N : Node_Id;
13864 R : Node_Id;
13865 T : Entity_Id)
13867 Loc : constant Source_Ptr := Sloc (N);
13868 CT : constant Entity_Id := Component_Type (T);
13870 begin
13871 -- The check we install is
13873 -- constraint_error when
13874 -- Boolean (component_type'First)
13875 -- and then Boolean (component_type'Last)
13876 -- and then array_type'Length /= 0)
13878 -- We need the last guard because we don't want to raise CE for empty
13879 -- arrays since no out of range values result (Empty arrays with a
13880 -- component type of True .. True -- very useful -- even the ACATS
13881 -- does not test that marginal case).
13883 Insert_Action (N,
13884 Make_Raise_Constraint_Error (Loc,
13885 Condition =>
13886 Make_And_Then (Loc,
13887 Left_Opnd =>
13888 Make_And_Then (Loc,
13889 Left_Opnd =>
13890 Convert_To (Standard_Boolean,
13891 Make_Attribute_Reference (Loc,
13892 Prefix => New_Occurrence_Of (CT, Loc),
13893 Attribute_Name => Name_First)),
13895 Right_Opnd =>
13896 Convert_To (Standard_Boolean,
13897 Make_Attribute_Reference (Loc,
13898 Prefix => New_Occurrence_Of (CT, Loc),
13899 Attribute_Name => Name_Last))),
13901 Right_Opnd => Make_Non_Empty_Check (Loc, R)),
13902 Reason => CE_Range_Check_Failed));
13903 end Silly_Boolean_Array_Xor_Test;
13905 ----------------------------
13906 -- Small_Integer_Type_For --
13907 ----------------------------
13909 function Small_Integer_Type_For (S : Uint; Uns : Boolean) return Entity_Id
13911 begin
13912 -- The only difference between this and Integer_Type_For is that this
13913 -- can return small (8- or 16-bit) types.
13915 if S <= Standard_Short_Short_Integer_Size then
13916 if Uns then
13917 return Standard_Short_Short_Unsigned;
13918 else
13919 return Standard_Short_Short_Integer;
13920 end if;
13922 elsif S <= Standard_Short_Integer_Size then
13923 if Uns then
13924 return Standard_Short_Unsigned;
13925 else
13926 return Standard_Short_Integer;
13927 end if;
13929 else
13930 return Integer_Type_For (S, Uns);
13931 end if;
13932 end Small_Integer_Type_For;
13934 ------------------
13935 -- Thunk_Target --
13936 ------------------
13938 function Thunk_Target (Thunk : Entity_Id) return Entity_Id is
13939 Target : Entity_Id := Thunk;
13941 begin
13942 pragma Assert (Is_Thunk (Thunk));
13944 while Is_Thunk (Target) loop
13945 Target := Thunk_Entity (Target);
13946 end loop;
13948 return Target;
13949 end Thunk_Target;
13951 -------------------
13952 -- Type_Map_Hash --
13953 -------------------
13955 function Type_Map_Hash (Id : Entity_Id) return Type_Map_Header is
13956 begin
13957 return Type_Map_Header (Id mod Type_Map_Size);
13958 end Type_Map_Hash;
13960 ------------------------------------------
13961 -- Type_May_Have_Bit_Aligned_Components --
13962 ------------------------------------------
13964 function Type_May_Have_Bit_Aligned_Components
13965 (Typ : Entity_Id) return Boolean
13967 begin
13968 -- Array type, check component type
13970 if Is_Array_Type (Typ) then
13971 return
13972 Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
13974 -- Record type, check components
13976 elsif Is_Record_Type (Typ) then
13977 declare
13978 E : Entity_Id;
13980 begin
13981 E := First_Component_Or_Discriminant (Typ);
13982 while Present (E) loop
13983 -- This is the crucial test: if the component itself causes
13984 -- trouble, then we can stop and return True.
13986 if Component_May_Be_Bit_Aligned (E) then
13987 return True;
13988 end if;
13990 -- Otherwise, we need to test its type, to see if it may
13991 -- itself contain a troublesome component.
13993 if Type_May_Have_Bit_Aligned_Components (Etype (E)) then
13994 return True;
13995 end if;
13997 Next_Component_Or_Discriminant (E);
13998 end loop;
14000 return False;
14001 end;
14003 -- Type other than array or record is always OK
14005 else
14006 return False;
14007 end if;
14008 end Type_May_Have_Bit_Aligned_Components;
14010 -------------------------------
14011 -- Update_Primitives_Mapping --
14012 -------------------------------
14014 procedure Update_Primitives_Mapping
14015 (Inher_Id : Entity_Id;
14016 Subp_Id : Entity_Id)
14018 Parent_Type : constant Entity_Id := Find_Dispatching_Type (Inher_Id);
14019 Derived_Type : constant Entity_Id := Find_Dispatching_Type (Subp_Id);
14021 begin
14022 pragma Assert (Parent_Type /= Derived_Type);
14023 Map_Types (Parent_Type, Derived_Type);
14024 end Update_Primitives_Mapping;
14026 ----------------------------------
14027 -- Within_Case_Or_If_Expression --
14028 ----------------------------------
14030 function Within_Case_Or_If_Expression (N : Node_Id) return Boolean is
14031 Par : Node_Id;
14033 begin
14034 -- Locate an enclosing case or if expression. Note that these constructs
14035 -- can be expanded into Expression_With_Actions, hence the test of the
14036 -- original node.
14038 Par := Parent (N);
14039 while Present (Par) loop
14040 if Nkind (Original_Node (Par)) in N_Case_Expression | N_If_Expression
14041 then
14042 return True;
14044 -- Prevent the search from going too far
14046 elsif Is_Body_Or_Package_Declaration (Par) then
14047 return False;
14048 end if;
14050 Par := Parent (Par);
14051 end loop;
14053 return False;
14054 end Within_Case_Or_If_Expression;
14056 ------------------------------
14057 -- Predicate_Check_In_Scope --
14058 ------------------------------
14060 function Predicate_Check_In_Scope (N : Node_Id) return Boolean is
14061 S : Entity_Id;
14063 begin
14064 S := Current_Scope;
14065 while Present (S) and then not Is_Subprogram (S) loop
14066 S := Scope (S);
14067 end loop;
14069 if Present (S) then
14071 -- Predicate checks should only be enabled in init procs for
14072 -- expressions coming from source.
14074 if Is_Init_Proc (S) then
14075 return Comes_From_Source (N);
14077 elsif Get_TSS_Name (S) /= TSS_Null
14078 and then not Is_Predicate_Function (S)
14079 then
14080 return False;
14081 end if;
14082 end if;
14084 return True;
14085 end Predicate_Check_In_Scope;
14087 end Exp_Util;