MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / ada / exp_unst.adb
blob239cda02ea7c04a87ad479b1c54717de1c270bcb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ U N S T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2014-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 Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Einfo.Entities; use Einfo.Entities;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Exp_Util; use Exp_Util;
33 with Lib; use Lib;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
37 with Opt;
38 with Output; use Output;
39 with Rtsfind; use Rtsfind;
40 with Sem; use Sem;
41 with Sem_Aux; use Sem_Aux;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Mech; use Sem_Mech;
44 with Sem_Res; use Sem_Res;
45 with Sem_Util; use Sem_Util;
46 with Sinfo; use Sinfo;
47 with Sinfo.Nodes; use Sinfo.Nodes;
48 with Sinfo.Utils; use Sinfo.Utils;
49 with Sinput; use Sinput;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Tbuild; use Tbuild;
53 with Uintp; use Uintp;
55 package body Exp_Unst is
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 procedure Unnest_Subprogram
62 (Subp : Entity_Id; Subp_Body : Node_Id; For_Inline : Boolean := False);
63 -- Subp is a library-level subprogram which has nested subprograms, and
64 -- Subp_Body is the corresponding N_Subprogram_Body node. This procedure
65 -- declares the AREC types and objects, adds assignments to the AREC record
66 -- as required, defines the xxxPTR types for uplevel referenced objects,
67 -- adds the ARECP parameter to all nested subprograms which need it, and
68 -- modifies all uplevel references appropriately. If For_Inline is True,
69 -- we're unnesting this subprogram because it's on the list of inlined
70 -- subprograms and should unnest it despite it not being part of the main
71 -- unit.
73 -----------
74 -- Calls --
75 -----------
77 -- Table to record calls within the nest being analyzed. These are the
78 -- calls which may need to have an AREC actual added. This table is built
79 -- new for each subprogram nest and cleared at the end of processing each
80 -- subprogram nest.
82 type Call_Entry is record
83 N : Node_Id;
84 -- The actual call
86 Caller : Entity_Id;
87 -- Entity of the subprogram containing the call (can be at any level)
89 Callee : Entity_Id;
90 -- Entity of the subprogram called (always at level 2 or higher). Note
91 -- that in accordance with the basic rules of nesting, the level of To
92 -- is either less than or equal to the level of From, or one greater.
93 end record;
95 package Calls is new Table.Table (
96 Table_Component_Type => Call_Entry,
97 Table_Index_Type => Nat,
98 Table_Low_Bound => 1,
99 Table_Initial => 100,
100 Table_Increment => 200,
101 Table_Name => "Unnest_Calls");
102 -- Records each call within the outer subprogram and all nested subprograms
103 -- that are to other subprograms nested within the outer subprogram. These
104 -- are the calls that may need an additional parameter.
106 procedure Append_Unique_Call (Call : Call_Entry);
107 -- Append a call entry to the Calls table. A check is made to see if the
108 -- table already contains this entry and if so it has no effect.
110 ----------------------------------
111 -- Subprograms For Fat Pointers --
112 ----------------------------------
114 function Build_Access_Type_Decl
115 (E : Entity_Id;
116 Scop : Entity_Id) return Node_Id;
117 -- For an uplevel reference that involves an unconstrained array type,
118 -- build an access type declaration for the corresponding activation
119 -- record component. The relevant attributes of the access type are
120 -- set here to avoid a full analysis that would require a scope stack.
122 function Needs_Fat_Pointer (E : Entity_Id) return Boolean;
123 -- A formal parameter of an unconstrained array type that appears in an
124 -- uplevel reference requires the construction of an access type, to be
125 -- used in the corresponding component declaration.
127 -----------
128 -- Urefs --
129 -----------
131 -- Table to record explicit uplevel references to objects (variables,
132 -- constants, formal parameters). These are the references that will
133 -- need rewriting to use the activation table (AREC) pointers. Also
134 -- included are implicit and explicit uplevel references to types, but
135 -- these do not get rewritten by the front end. This table is built new
136 -- for each subprogram nest and cleared at the end of processing each
137 -- subprogram nest.
139 type Uref_Entry is record
140 Ref : Node_Id;
141 -- The reference itself. For objects this is always an entity reference
142 -- and the referenced entity will have its Is_Uplevel_Referenced_Entity
143 -- flag set and will appear in the Uplevel_Referenced_Entities list of
144 -- the subprogram declaring this entity.
146 Ent : Entity_Id;
147 -- The Entity_Id of the uplevel referenced object or type
149 Caller : Entity_Id;
150 -- The entity for the subprogram immediately containing this entity
152 Callee : Entity_Id;
153 -- The entity for the subprogram containing the referenced entity. Note
154 -- that the level of Callee must be less than the level of Caller, since
155 -- this is an uplevel reference.
156 end record;
158 package Urefs is new Table.Table (
159 Table_Component_Type => Uref_Entry,
160 Table_Index_Type => Nat,
161 Table_Low_Bound => 1,
162 Table_Initial => 100,
163 Table_Increment => 200,
164 Table_Name => "Unnest_Urefs");
166 ------------------------
167 -- Append_Unique_Call --
168 ------------------------
170 procedure Append_Unique_Call (Call : Call_Entry) is
171 begin
172 for J in Calls.First .. Calls.Last loop
173 if Calls.Table (J) = Call then
174 return;
175 end if;
176 end loop;
178 Calls.Append (Call);
179 end Append_Unique_Call;
181 -----------------------------
182 -- Build_Access_Type_Decl --
183 -----------------------------
185 function Build_Access_Type_Decl
186 (E : Entity_Id;
187 Scop : Entity_Id) return Node_Id
189 Loc : constant Source_Ptr := Sloc (E);
190 Typ : Entity_Id;
192 begin
193 Typ := Make_Temporary (Loc, 'S');
194 Mutate_Ekind (Typ, E_General_Access_Type);
195 Set_Etype (Typ, Typ);
196 Set_Scope (Typ, Scop);
197 Set_Directly_Designated_Type (Typ, Etype (E));
199 return
200 Make_Full_Type_Declaration (Loc,
201 Defining_Identifier => Typ,
202 Type_Definition =>
203 Make_Access_To_Object_Definition (Loc,
204 Subtype_Indication => New_Occurrence_Of (Etype (E), Loc)));
205 end Build_Access_Type_Decl;
207 ---------------
208 -- Get_Level --
209 ---------------
211 function Get_Level (Subp : Entity_Id; Sub : Entity_Id) return Nat is
212 Lev : Nat;
213 S : Entity_Id;
215 begin
216 Lev := 1;
217 S := Sub;
218 loop
219 if S = Subp then
220 return Lev;
221 else
222 Lev := Lev + 1;
223 S := Enclosing_Subprogram (S);
224 end if;
225 end loop;
226 end Get_Level;
228 --------------------------
229 -- In_Synchronized_Unit --
230 --------------------------
232 function In_Synchronized_Unit (Subp : Entity_Id) return Boolean is
233 S : Entity_Id := Scope (Subp);
235 begin
236 while Present (S) and then S /= Standard_Standard loop
237 if Is_Concurrent_Type (S) then
238 return True;
240 elsif Is_Private_Type (S)
241 and then Present (Full_View (S))
242 and then Is_Concurrent_Type (Full_View (S))
243 then
244 return True;
245 end if;
247 S := Scope (S);
248 end loop;
250 return False;
251 end In_Synchronized_Unit;
253 -----------------------
254 -- Needs_Fat_Pointer --
255 -----------------------
257 function Needs_Fat_Pointer (E : Entity_Id) return Boolean is
258 Typ : constant Entity_Id := Get_Fullest_View (Etype (E));
259 begin
260 return Is_Array_Type (Typ) and then not Is_Constrained (Typ);
261 end Needs_Fat_Pointer;
263 ----------------
264 -- Subp_Index --
265 ----------------
267 function Subp_Index (Sub : Entity_Id) return SI_Type is
268 E : Entity_Id := Sub;
270 begin
271 pragma Assert (Is_Subprogram (E));
273 if Field_Is_Initial_Zero (E, F_Subps_Index)
274 or else Subps_Index (E) = Uint_0
275 then
276 E := Ultimate_Alias (E);
278 -- The body of a protected operation has a different name and
279 -- has been scanned at this point, and thus has an entry in the
280 -- subprogram table.
282 if E = Sub and then Present (Protected_Body_Subprogram (E)) then
283 E := Protected_Body_Subprogram (E);
284 end if;
286 if Ekind (E) = E_Function
287 and then Rewritten_For_C (E)
288 and then Present (Corresponding_Procedure (E))
289 then
290 E := Corresponding_Procedure (E);
291 end if;
292 end if;
294 pragma Assert (Subps_Index (E) /= Uint_0);
295 return SI_Type (UI_To_Int (Subps_Index (E)));
296 end Subp_Index;
298 -----------------------
299 -- Unnest_Subprogram --
300 -----------------------
302 procedure Unnest_Subprogram
303 (Subp : Entity_Id; Subp_Body : Node_Id; For_Inline : Boolean := False) is
304 function AREC_Name (J : Pos; S : String) return Name_Id;
305 -- Returns name for string ARECjS, where j is the decimal value of j
307 function Enclosing_Subp (Subp : SI_Type) return SI_Type;
308 -- Subp is the index of a subprogram which has a Lev greater than 1.
309 -- This function returns the index of the enclosing subprogram which
310 -- will have a Lev value one less than this.
312 function Img_Pos (N : Pos) return String;
313 -- Return image of N without leading blank
315 function Upref_Name
316 (Ent : Entity_Id;
317 Index : Pos;
318 Clist : List_Id) return Name_Id;
319 -- This function returns the name to be used in the activation record to
320 -- reference the variable uplevel. Clist is the list of components that
321 -- have been created in the activation record so far. Normally the name
322 -- is just a copy of the Chars field of the entity. The exception is
323 -- when the name has already been used, in which case we suffix the name
324 -- with the index value Index to avoid duplication. This happens with
325 -- declare blocks and generic parameters at least.
327 ---------------
328 -- AREC_Name --
329 ---------------
331 function AREC_Name (J : Pos; S : String) return Name_Id is
332 begin
333 return Name_Find ("AREC" & Img_Pos (J) & S);
334 end AREC_Name;
336 --------------------
337 -- Enclosing_Subp --
338 --------------------
340 function Enclosing_Subp (Subp : SI_Type) return SI_Type is
341 STJ : Subp_Entry renames Subps.Table (Subp);
342 Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
343 begin
344 pragma Assert (STJ.Lev > 1);
345 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
346 return Ret;
347 end Enclosing_Subp;
349 -------------
350 -- Img_Pos --
351 -------------
353 function Img_Pos (N : Pos) return String is
354 Buf : String (1 .. 20);
355 Ptr : Natural;
356 NV : Nat;
358 begin
359 Ptr := Buf'Last;
360 NV := N;
361 while NV /= 0 loop
362 Buf (Ptr) := Character'Val (48 + NV mod 10);
363 Ptr := Ptr - 1;
364 NV := NV / 10;
365 end loop;
367 return Buf (Ptr + 1 .. Buf'Last);
368 end Img_Pos;
370 ----------------
371 -- Upref_Name --
372 ----------------
374 function Upref_Name
375 (Ent : Entity_Id;
376 Index : Pos;
377 Clist : List_Id) return Name_Id
379 C : Node_Id;
380 begin
381 C := First (Clist);
382 loop
383 if No (C) then
384 return Chars (Ent);
386 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
387 return
388 Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
389 else
390 Next (C);
391 end if;
392 end loop;
393 end Upref_Name;
395 -- Start of processing for Unnest_Subprogram
397 begin
398 -- Nothing to do inside a generic (all processing is for instance)
400 if Inside_A_Generic then
401 return;
402 end if;
404 -- If the main unit is a package body then we need to examine the spec
405 -- to determine whether the main unit is generic (the scope stack is not
406 -- present when this is called on the main unit).
408 if not For_Inline
409 and then Ekind (Cunit_Entity (Main_Unit)) = E_Package_Body
410 and then Is_Generic_Unit (Spec_Entity (Cunit_Entity (Main_Unit)))
411 then
412 return;
414 -- Only unnest when generating code for the main source unit or if
415 -- we're unnesting for inline. But in some Annex E cases the Sloc
416 -- points to a different unit, so also make sure that the Parent
417 -- isn't in something that we know we're generating code for.
419 elsif not For_Inline
420 and then not In_Extended_Main_Code_Unit (Subp_Body)
421 and then not In_Extended_Main_Code_Unit (Parent (Subp_Body))
422 then
423 return;
424 end if;
426 -- This routine is called late, after the scope stack is gone. The
427 -- following creates a suitable dummy scope stack to be used for the
428 -- analyze/expand calls made from this routine.
430 Push_Scope (Subp);
432 -- First step, we must mark all nested subprograms that require a static
433 -- link (activation record) because either they contain explicit uplevel
434 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
435 -- this point), or they make calls to other subprograms in the same nest
436 -- that require a static link (in which case we set this flag).
438 -- This is a recursive definition, and to implement this, we have to
439 -- build a call graph for the set of nested subprograms, and then go
440 -- over this graph to implement recursively the invariant that if a
441 -- subprogram has a call to a subprogram requiring a static link, then
442 -- the calling subprogram requires a static link.
444 -- First populate the above tables
446 Subps_First := Subps.Last + 1;
447 Calls.Init;
448 Urefs.Init;
450 Build_Tables : declare
451 Current_Subprogram : Entity_Id := Empty;
452 -- When we scan a subprogram body, we set Current_Subprogram to the
453 -- corresponding entity. This gets recursively saved and restored.
455 function Visit_Node (N : Node_Id) return Traverse_Result;
456 -- Visit a single node in Subp
458 -----------
459 -- Visit --
460 -----------
462 procedure Visit is new Traverse_Proc (Visit_Node);
463 -- Used to traverse the body of Subp, populating the tables
465 ----------------
466 -- Visit_Node --
467 ----------------
469 function Visit_Node (N : Node_Id) return Traverse_Result is
470 Ent : Entity_Id;
471 Caller : Entity_Id;
472 Callee : Entity_Id;
474 procedure Check_Static_Type
475 (In_T : Entity_Id;
476 N : Node_Id;
477 DT : in out Boolean;
478 Check_Designated : Boolean := False);
479 -- Given a type In_T, checks if it is a static type defined as
480 -- a type with no dynamic bounds in sight. If so, the only
481 -- action is to set Is_Static_Type True for In_T. If In_T is
482 -- not a static type, then all types with dynamic bounds
483 -- associated with In_T are detected, and their bounds are
484 -- marked as uplevel referenced if not at the library level,
485 -- and DT is set True. If N is specified, it's the node that
486 -- will need to be replaced. If not specified, it means we
487 -- can't do a replacement because the bound is implicit.
489 -- If Check_Designated is True and In_T or its full view
490 -- is an access type, check whether the designated type
491 -- has dynamic bounds.
493 procedure Note_Uplevel_Ref
494 (E : Entity_Id;
495 N : Node_Id;
496 Caller : Entity_Id;
497 Callee : Entity_Id);
498 -- Called when we detect an explicit or implicit uplevel reference
499 -- from within Caller to entity E declared in Callee. E can be a
500 -- an object or a type.
502 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id);
503 -- Enter a subprogram whose body is visible or which is a
504 -- subprogram instance into the subprogram table.
506 -----------------------
507 -- Check_Static_Type --
508 -----------------------
510 procedure Check_Static_Type
511 (In_T : Entity_Id;
512 N : Node_Id;
513 DT : in out Boolean;
514 Check_Designated : Boolean := False)
516 T : constant Entity_Id := Get_Fullest_View (In_T);
518 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id);
519 -- N is the bound of a dynamic type. This procedure notes that
520 -- this bound is uplevel referenced, it can handle references
521 -- to entities (typically _FIRST and _LAST entities), and also
522 -- attribute references of the form T'name (name is typically
523 -- FIRST or LAST) where T is the uplevel referenced bound.
524 -- Ref, if Present, is the location of the reference to
525 -- replace.
527 ------------------------
528 -- Note_Uplevel_Bound --
529 ------------------------
531 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id) is
532 begin
533 -- Entity name case. Make sure that the entity is declared
534 -- in a subprogram. This may not be the case for a type in a
535 -- loop appearing in a precondition.
536 -- Exclude explicitly discriminants (that can appear
537 -- in bounds of discriminated components) and enumeration
538 -- literals.
540 if Is_Entity_Name (N) then
541 if Present (Entity (N))
542 and then not Is_Type (Entity (N))
543 and then Present (Enclosing_Subprogram (Entity (N)))
544 and then
545 Ekind (Entity (N))
546 not in E_Discriminant | E_Enumeration_Literal
547 then
548 Note_Uplevel_Ref
549 (E => Entity (N),
550 N => Empty,
551 Caller => Current_Subprogram,
552 Callee => Enclosing_Subprogram (Entity (N)));
553 end if;
555 -- Attribute or indexed component case
557 elsif Nkind (N) in
558 N_Attribute_Reference | N_Indexed_Component
559 then
560 Note_Uplevel_Bound (Prefix (N), Ref);
562 -- The indices of the indexed components, or the
563 -- associated expressions of an attribute reference,
564 -- may also involve uplevel references.
566 declare
567 Expr : Node_Id;
569 begin
570 Expr := First (Expressions (N));
571 while Present (Expr) loop
572 Note_Uplevel_Bound (Expr, Ref);
573 Next (Expr);
574 end loop;
575 end;
577 -- The type of the prefix may be have an uplevel
578 -- reference if this needs bounds.
580 if Nkind (N) = N_Attribute_Reference then
581 declare
582 Attr : constant Attribute_Id :=
583 Get_Attribute_Id (Attribute_Name (N));
584 DT : Boolean := False;
586 begin
587 if (Attr = Attribute_First
588 or else Attr = Attribute_Last
589 or else Attr = Attribute_Length)
590 and then Is_Constrained (Etype (Prefix (N)))
591 then
592 Check_Static_Type
593 (Etype (Prefix (N)), Empty, DT);
594 end if;
595 end;
596 end if;
598 -- Binary operator cases. These can apply to arrays for
599 -- which we may need bounds.
601 elsif Nkind (N) in N_Binary_Op then
602 Note_Uplevel_Bound (Left_Opnd (N), Ref);
603 Note_Uplevel_Bound (Right_Opnd (N), Ref);
605 -- Unary operator case
607 elsif Nkind (N) in N_Unary_Op then
608 Note_Uplevel_Bound (Right_Opnd (N), Ref);
610 -- Explicit dereference and selected component case
612 elsif Nkind (N) in
613 N_Explicit_Dereference | N_Selected_Component
614 then
615 Note_Uplevel_Bound (Prefix (N), Ref);
617 -- Conditional expressions
619 elsif Nkind (N) = N_If_Expression then
620 declare
621 Expr : Node_Id;
623 begin
624 Expr := First (Expressions (N));
625 while Present (Expr) loop
626 Note_Uplevel_Bound (Expr, Ref);
627 Next (Expr);
628 end loop;
629 end;
631 elsif Nkind (N) = N_Case_Expression then
632 declare
633 Alternative : Node_Id;
635 begin
636 Note_Uplevel_Bound (Expression (N), Ref);
638 Alternative := First (Alternatives (N));
639 while Present (Alternative) loop
640 Note_Uplevel_Bound (Expression (Alternative), Ref);
641 end loop;
642 end;
644 -- Conversion case
646 elsif Nkind (N) = N_Type_Conversion then
647 Note_Uplevel_Bound (Expression (N), Ref);
648 end if;
649 end Note_Uplevel_Bound;
651 -- Start of processing for Check_Static_Type
653 begin
654 -- If already marked static, immediate return
656 if Is_Static_Type (T) and then not Check_Designated then
657 return;
658 end if;
660 -- If the type is at library level, always consider it static,
661 -- since such uplevel references are irrelevant.
663 if Is_Library_Level_Entity (T) then
664 Set_Is_Static_Type (T);
665 return;
666 end if;
668 -- Otherwise figure out what the story is with this type
670 -- For a scalar type, check bounds
672 if Is_Scalar_Type (T) then
674 -- If both bounds static, then this is a static type
676 declare
677 LB : constant Node_Id := Type_Low_Bound (T);
678 UB : constant Node_Id := Type_High_Bound (T);
680 begin
681 if not Is_Static_Expression (LB) then
682 Note_Uplevel_Bound (LB, N);
683 DT := True;
684 end if;
686 if not Is_Static_Expression (UB) then
687 Note_Uplevel_Bound (UB, N);
688 DT := True;
689 end if;
690 end;
692 -- For record type, check all components and discriminant
693 -- constraints if present.
695 elsif Is_Record_Type (T) then
696 declare
697 C : Entity_Id;
698 D : Elmt_Id;
700 begin
701 C := First_Component_Or_Discriminant (T);
702 while Present (C) loop
703 Check_Static_Type (Etype (C), N, DT);
704 Next_Component_Or_Discriminant (C);
705 end loop;
707 if Has_Discriminants (T)
708 and then Present (Discriminant_Constraint (T))
709 then
710 D := First_Elmt (Discriminant_Constraint (T));
711 while Present (D) loop
712 if not Is_Static_Expression (Node (D)) then
713 Note_Uplevel_Bound (Node (D), N);
714 DT := True;
715 end if;
717 Next_Elmt (D);
718 end loop;
719 end if;
720 end;
722 -- For array type, check index types and component type
724 elsif Is_Array_Type (T) then
725 declare
726 IX : Node_Id;
727 begin
728 Check_Static_Type (Component_Type (T), N, DT);
730 IX := First_Index (T);
731 while Present (IX) loop
732 Check_Static_Type (Etype (IX), N, DT);
733 Next_Index (IX);
734 end loop;
735 end;
737 -- For private type, examine whether full view is static
739 elsif Is_Incomplete_Or_Private_Type (T)
740 and then Present (Full_View (T))
741 then
742 Check_Static_Type (Full_View (T), N, DT, Check_Designated);
744 if Is_Static_Type (Full_View (T)) then
745 Set_Is_Static_Type (T);
746 end if;
748 -- For access types, check designated type when required
750 elsif Is_Access_Type (T) and then Check_Designated then
751 Check_Static_Type (Directly_Designated_Type (T), N, DT);
753 -- For now, ignore other types
755 else
756 return;
757 end if;
759 if not DT then
760 Set_Is_Static_Type (T);
761 end if;
762 end Check_Static_Type;
764 ----------------------
765 -- Note_Uplevel_Ref --
766 ----------------------
768 procedure Note_Uplevel_Ref
769 (E : Entity_Id;
770 N : Node_Id;
771 Caller : Entity_Id;
772 Callee : Entity_Id)
774 Full_E : Entity_Id := E;
775 begin
776 -- Nothing to do for static type
778 if Is_Static_Type (E) then
779 return;
780 end if;
782 -- Nothing to do if Caller and Callee are the same
784 if Caller = Callee then
785 return;
787 -- Callee may be a function that returns an array, and that has
788 -- been rewritten as a procedure. If caller is that procedure,
789 -- nothing to do either.
791 elsif Ekind (Callee) = E_Function
792 and then Rewritten_For_C (Callee)
793 and then Corresponding_Procedure (Callee) = Caller
794 then
795 return;
797 elsif Ekind (Callee) in E_Entry | E_Entry_Family then
798 return;
799 end if;
801 -- We have a new uplevel referenced entity
803 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
804 Full_E := Full_View (E);
805 end if;
807 -- All we do at this stage is to add the uplevel reference to
808 -- the table. It's too early to do anything else, since this
809 -- uplevel reference may come from an unreachable subprogram
810 -- in which case the entry will be deleted.
812 Urefs.Append ((N, Full_E, Caller, Callee));
813 end Note_Uplevel_Ref;
815 -------------------------
816 -- Register_Subprogram --
817 -------------------------
819 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
820 L : constant Nat := Get_Level (Subp, E);
822 begin
823 -- Subprograms declared in tasks and protected types cannot be
824 -- eliminated because calls to them may be in other units, so
825 -- they must be treated as reachable.
827 Subps.Append
828 ((Ent => E,
829 Bod => Bod,
830 Lev => L,
831 Reachable => In_Synchronized_Unit (E)
832 or else Address_Taken (E),
833 Uplevel_Ref => L,
834 Declares_AREC => False,
835 Uents => No_Elist,
836 Last => 0,
837 ARECnF => Empty,
838 ARECn => Empty,
839 ARECnT => Empty,
840 ARECnPT => Empty,
841 ARECnP => Empty,
842 ARECnU => Empty));
844 Set_Subps_Index (E, UI_From_Int (Subps.Last));
846 -- If we marked this reachable because it's in a synchronized
847 -- unit, we have to mark all enclosing subprograms as reachable
848 -- as well. We do the same for subprograms with Address_Taken,
849 -- because otherwise we can run into problems with looking at
850 -- enclosing subprograms in Subps.Table due to their being
851 -- unreachable (the Subp_Index of unreachable subps is later
852 -- set to zero and their entry in Subps.Table is removed).
854 if In_Synchronized_Unit (E) or else Address_Taken (E) then
855 declare
856 S : Entity_Id := E;
858 begin
859 for J in reverse 1 .. L - 1 loop
860 S := Enclosing_Subprogram (S);
861 Subps.Table (Subp_Index (S)).Reachable := True;
862 end loop;
863 end;
864 end if;
865 end Register_Subprogram;
867 -- Start of processing for Visit_Node
869 begin
870 case Nkind (N) is
872 -- Record a subprogram call
874 when N_Function_Call
875 | N_Procedure_Call_Statement
877 -- We are only interested in direct calls, not indirect
878 -- calls (where Name (N) is an explicit dereference) at
879 -- least for now!
881 if Nkind (Name (N)) in N_Has_Entity then
882 Ent := Entity (Name (N));
884 -- We are only interested in calls to subprograms nested
885 -- within Subp. Calls to Subp itself or to subprograms
886 -- outside the nested structure do not affect us.
888 if Is_Subprogram (Ent)
889 and then not Is_Generic_Subprogram (Ent)
890 and then not Is_Imported (Ent)
891 and then not Is_Intrinsic_Subprogram (Ent)
892 and then Scope_Within (Ultimate_Alias (Ent), Subp)
893 then
894 Append_Unique_Call ((N, Current_Subprogram, Ent));
895 end if;
896 end if;
898 -- For all calls where the formal is an unconstrained array
899 -- and the actual is constrained we need to check the bounds
900 -- for uplevel references.
902 declare
903 Actual : Entity_Id;
904 DT : Boolean := False;
905 Formal : Node_Id;
906 Subp : Entity_Id;
907 F_Type : Entity_Id;
908 A_Type : Entity_Id;
910 begin
911 if Nkind (Name (N)) = N_Explicit_Dereference then
912 Subp := Etype (Name (N));
913 else
914 Subp := Entity (Name (N));
915 end if;
917 Actual := First_Actual (N);
918 Formal := First_Formal_With_Extras (Subp);
920 while Present (Actual) loop
921 F_Type := Get_Fullest_View (Etype (Formal));
922 A_Type := Get_Fullest_View (Etype (Actual));
924 if Is_Array_Type (F_Type)
925 and then not Is_Constrained (F_Type)
926 and then Is_Constrained (A_Type)
927 then
928 Check_Static_Type (A_Type, Empty, DT);
929 end if;
931 Next_Actual (Actual);
932 Next_Formal_With_Extras (Formal);
933 end loop;
934 end;
936 -- An At_End_Proc in a statement sequence indicates that there
937 -- is a call from the enclosing construct or block to that
938 -- subprogram. As above, the called entity must be local and
939 -- not imported.
941 when N_Handled_Sequence_Of_Statements | N_Block_Statement =>
942 if Present (At_End_Proc (N))
943 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
944 and then not Is_Imported (Entity (At_End_Proc (N)))
945 then
946 Append_Unique_Call
947 ((N, Current_Subprogram, Entity (At_End_Proc (N))));
948 end if;
950 -- Similarly, the following constructs include a semantic
951 -- attribute Procedure_To_Call that must be handled like
952 -- other calls. Likewise for attribute Storage_Pool.
954 when N_Allocator
955 | N_Extended_Return_Statement
956 | N_Free_Statement
957 | N_Simple_Return_Statement
959 declare
960 Pool : constant Entity_Id := Storage_Pool (N);
961 Proc : constant Entity_Id := Procedure_To_Call (N);
963 begin
964 if Present (Proc)
965 and then Scope_Within (Proc, Subp)
966 and then not Is_Imported (Proc)
967 then
968 Append_Unique_Call ((N, Current_Subprogram, Proc));
969 end if;
971 if Present (Pool)
972 and then not Is_Library_Level_Entity (Pool)
973 and then Scope_Within_Or_Same (Scope (Pool), Subp)
974 then
975 Caller := Current_Subprogram;
976 Callee := Enclosing_Subprogram (Pool);
978 if Callee /= Caller then
979 Note_Uplevel_Ref (Pool, Empty, Caller, Callee);
980 end if;
981 end if;
982 end;
984 -- For an allocator with a qualified expression, check type
985 -- of expression being qualified. The explicit type name is
986 -- handled as an entity reference.
988 if Nkind (N) = N_Allocator
989 and then Nkind (Expression (N)) = N_Qualified_Expression
990 then
991 declare
992 DT : Boolean := False;
993 begin
994 Check_Static_Type
995 (Etype (Expression (Expression (N))), Empty, DT);
996 end;
998 -- For a Return or Free (all other nodes we handle here),
999 -- we usually need the size of the object, so we need to be
1000 -- sure that any nonstatic bounds of the expression's type
1001 -- that are uplevel are handled.
1003 elsif Nkind (N) /= N_Allocator
1004 and then Present (Expression (N))
1005 then
1006 declare
1007 DT : Boolean := False;
1008 begin
1009 Check_Static_Type
1010 (Etype (Expression (N)),
1011 Empty,
1013 Check_Designated => Nkind (N) = N_Free_Statement);
1014 end;
1015 end if;
1017 -- A 'Access reference is a (potential) call. So is 'Address,
1018 -- in particular on imported subprograms. Other attributes
1019 -- require special handling.
1021 when N_Attribute_Reference =>
1022 declare
1023 Attr : constant Attribute_Id :=
1024 Get_Attribute_Id (Attribute_Name (N));
1025 begin
1026 case Attr is
1027 when Attribute_Access
1028 | Attribute_Unchecked_Access
1029 | Attribute_Unrestricted_Access
1030 | Attribute_Address
1032 if Nkind (Prefix (N)) in N_Has_Entity then
1033 Ent := Entity (Prefix (N));
1035 -- We only need to examine calls to subprograms
1036 -- nested within current Subp.
1038 if Scope_Within (Ent, Subp) then
1039 if Is_Imported (Ent) then
1040 null;
1042 elsif Is_Subprogram (Ent) then
1043 Append_Unique_Call
1044 ((N, Current_Subprogram, Ent));
1045 end if;
1046 end if;
1047 end if;
1049 -- References to bounds can be uplevel references if
1050 -- the type isn't static.
1052 when Attribute_First
1053 | Attribute_Last
1054 | Attribute_Length
1056 -- Special-case attributes of objects whose bounds
1057 -- may be uplevel references. More complex prefixes
1058 -- handled during full traversal. Note that if the
1059 -- nominal subtype of the prefix is unconstrained,
1060 -- the bound must be obtained from the object, not
1061 -- from the (possibly) uplevel reference. We call
1062 -- Get_Referenced_Object to deal with prefixes that
1063 -- are object renamings (prefixes that are types
1064 -- can be passed and will simply be returned). But
1065 -- it's also legal to get the bounds from the type
1066 -- of the prefix, so we have to handle both cases.
1068 declare
1069 DT : Boolean := False;
1071 begin
1072 if Is_Constrained
1073 (Etype (Get_Referenced_Object (Prefix (N))))
1074 then
1075 Check_Static_Type
1076 (Etype (Get_Referenced_Object (Prefix (N))),
1077 Empty, DT);
1078 end if;
1080 if Is_Constrained (Etype (Prefix (N))) then
1081 Check_Static_Type
1082 (Etype (Prefix (N)), Empty, DT);
1083 end if;
1084 end;
1086 when others =>
1087 null;
1088 end case;
1089 end;
1091 -- Component associations in aggregates are either static or
1092 -- else the aggregate will be expanded into assignments, in
1093 -- which case the expression is analyzed later and provides
1094 -- no relevant code generation.
1096 when N_Component_Association =>
1097 if No (Expression (N))
1098 or else No (Etype (Expression (N)))
1099 then
1100 return Skip;
1101 end if;
1103 -- Generic associations are not analyzed: the actuals are
1104 -- transferred to renaming and subtype declarations that
1105 -- are the ones that must be examined.
1107 when N_Generic_Association =>
1108 return Skip;
1110 -- Indexed references can be uplevel if the type isn't static
1111 -- and if the lower bound (or an inner bound for a multi-
1112 -- dimensional array) is uplevel.
1114 when N_Indexed_Component
1115 | N_Slice
1117 if Is_Constrained (Etype (Prefix (N))) then
1118 declare
1119 DT : Boolean := False;
1120 begin
1121 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1122 end;
1123 end if;
1125 -- A selected component can have an implicit up-level
1126 -- reference due to the bounds of previous fields in the
1127 -- record. We simplify the processing here by examining
1128 -- all components of the record.
1130 -- Selected components appear as unit names and end labels
1131 -- for child units. Prefixes of these nodes denote parent
1132 -- units and carry no type information so they are skipped.
1134 when N_Selected_Component =>
1135 if Present (Etype (Prefix (N))) then
1136 declare
1137 DT : Boolean := False;
1138 begin
1139 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1140 end;
1141 end if;
1143 -- For EQ/NE comparisons, we need the type of the operands
1144 -- in order to do the comparison, which means we need the
1145 -- bounds.
1147 when N_Op_Eq
1148 | N_Op_Ne
1150 declare
1151 DT : Boolean := False;
1152 begin
1153 Check_Static_Type (Etype (Left_Opnd (N)), Empty, DT);
1154 Check_Static_Type (Etype (Right_Opnd (N)), Empty, DT);
1155 end;
1157 -- Likewise we need the sizes to compute how much to move in
1158 -- an assignment.
1160 when N_Assignment_Statement =>
1161 declare
1162 DT : Boolean := False;
1163 begin
1164 Check_Static_Type (Etype (Name (N)), Empty, DT);
1165 Check_Static_Type (Etype (Expression (N)), Empty, DT);
1166 end;
1168 -- Record a subprogram. We record a subprogram body that acts
1169 -- as a spec. Otherwise we record a subprogram declaration,
1170 -- providing that it has a corresponding body we can get hold
1171 -- of. The case of no corresponding body being available is
1172 -- ignored for now.
1174 when N_Subprogram_Body =>
1175 Ent := Unique_Defining_Entity (N);
1177 -- Ignore generic subprogram
1179 if Is_Generic_Subprogram (Ent) then
1180 return Skip;
1181 end if;
1183 -- Make new entry in subprogram table if not already made
1185 Register_Subprogram (Ent, N);
1187 -- Record a call from an At_End_Proc
1189 if Present (At_End_Proc (N))
1190 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
1191 and then not Is_Imported (Entity (At_End_Proc (N)))
1192 then
1193 Append_Unique_Call ((N, Ent, Entity (At_End_Proc (N))));
1194 end if;
1196 -- We make a recursive call to scan the subprogram body, so
1197 -- that we can save and restore Current_Subprogram.
1199 declare
1200 Save_CS : constant Entity_Id := Current_Subprogram;
1201 Decl : Node_Id;
1203 begin
1204 Current_Subprogram := Ent;
1206 -- Scan declarations
1208 Decl := First (Declarations (N));
1209 while Present (Decl) loop
1210 Visit (Decl);
1211 Next (Decl);
1212 end loop;
1214 -- Scan statements
1216 Visit (Handled_Statement_Sequence (N));
1218 -- Restore current subprogram setting
1220 Current_Subprogram := Save_CS;
1221 end;
1223 -- Now at this level, return skipping the subprogram body
1224 -- descendants, since we already took care of them!
1226 return Skip;
1228 -- If we have a body stub, visit the associated subunit, which
1229 -- is a semantic descendant of the stub.
1231 when N_Body_Stub =>
1232 Visit (Library_Unit (N));
1234 -- A declaration of a wrapper package indicates a subprogram
1235 -- instance for which there is no explicit body. Enter the
1236 -- subprogram instance in the table.
1238 when N_Package_Declaration =>
1239 if Is_Wrapper_Package (Defining_Entity (N)) then
1240 Register_Subprogram
1241 (Related_Instance (Defining_Entity (N)), Empty);
1242 end if;
1244 -- Skip generic declarations
1246 when N_Generic_Declaration =>
1247 return Skip;
1249 -- Skip generic package body
1251 when N_Package_Body =>
1252 if Present (Corresponding_Spec (N))
1253 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
1254 then
1255 return Skip;
1256 end if;
1258 -- Pragmas and component declarations are ignored. Quantified
1259 -- expressions are expanded into explicit loops and the
1260 -- original epression must be ignored.
1262 when N_Component_Declaration
1263 | N_Pragma
1264 | N_Quantified_Expression
1266 return Skip;
1268 -- We want to skip the function spec for a generic function
1269 -- to avoid looking at any generic types that might be in
1270 -- its formals.
1272 when N_Function_Specification =>
1273 if Is_Generic_Subprogram (Unique_Defining_Entity (N)) then
1274 return Skip;
1275 end if;
1277 -- Otherwise record an uplevel reference in a local identifier
1279 when others =>
1280 if Nkind (N) in N_Has_Entity
1281 and then Present (Entity (N))
1282 then
1283 Ent := Entity (N);
1285 -- Only interested in entities declared within our nest
1287 if not Is_Library_Level_Entity (Ent)
1288 and then Scope_Within_Or_Same (Scope (Ent), Subp)
1290 -- Skip entities defined in inlined subprograms
1292 and then
1293 Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
1295 -- Constants and variables are potentially uplevel
1296 -- references to global declarations.
1298 and then
1299 (Ekind (Ent) in E_Constant
1300 | E_Loop_Parameter
1301 | E_Variable
1303 -- Formals are interesting, but not if being used
1304 -- as mere names of parameters for name notation
1305 -- calls.
1307 or else
1308 (Is_Formal (Ent)
1309 and then not
1310 (Nkind (Parent (N)) = N_Parameter_Association
1311 and then Selector_Name (Parent (N)) = N))
1313 -- Types other than known Is_Static types are
1314 -- potentially interesting.
1316 or else
1317 (Is_Type (Ent) and then not Is_Static_Type (Ent)))
1318 then
1319 -- Here we have a potentially interesting uplevel
1320 -- reference to examine.
1322 if Is_Type (Ent) then
1323 declare
1324 DT : Boolean := False;
1326 begin
1327 Check_Static_Type (Ent, N, DT);
1328 return OK;
1329 end;
1330 end if;
1332 Caller := Current_Subprogram;
1333 Callee := Enclosing_Subprogram (Ent);
1335 if Callee /= Caller
1336 and then (not Is_Static_Type (Ent)
1337 or else Needs_Fat_Pointer (Ent))
1338 then
1339 Note_Uplevel_Ref (Ent, N, Caller, Callee);
1341 -- Check the type of a formal parameter of the current
1342 -- subprogram, whose formal type may be an uplevel
1343 -- reference.
1345 elsif Is_Formal (Ent)
1346 and then Scope (Ent) = Current_Subprogram
1347 then
1348 declare
1349 DT : Boolean := False;
1351 begin
1352 Check_Static_Type (Etype (Ent), Empty, DT);
1353 end;
1354 end if;
1355 end if;
1356 end if;
1357 end case;
1359 -- Fall through to continue scanning children of this node
1361 return OK;
1362 end Visit_Node;
1364 -- Start of processing for Build_Tables
1366 begin
1367 -- Traverse the body to get subprograms, calls and uplevel references
1369 Visit (Subp_Body);
1370 end Build_Tables;
1372 -- Now do the first transitive closure which determines which
1373 -- subprograms in the nest are actually reachable.
1375 Reachable_Closure : declare
1376 Modified : Boolean;
1378 begin
1379 Subps.Table (Subps_First).Reachable := True;
1381 -- We use a simple minded algorithm as follows (obviously this can
1382 -- be done more efficiently, using one of the standard algorithms
1383 -- for efficient transitive closure computation, but this is simple
1384 -- and most likely fast enough that its speed does not matter).
1386 -- Repeatedly scan the list of calls. Any time we find a call from
1387 -- A to B, where A is reachable, but B is not, then B is reachable,
1388 -- and note that we have made a change by setting Modified True. We
1389 -- repeat this until we make a pass with no modifications.
1391 Outer : loop
1392 Modified := False;
1393 Inner : for J in Calls.First .. Calls.Last loop
1394 declare
1395 CTJ : Call_Entry renames Calls.Table (J);
1397 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1398 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1400 SUBF : Subp_Entry renames Subps.Table (SINF);
1401 SUBT : Subp_Entry renames Subps.Table (SINT);
1403 begin
1404 if SUBF.Reachable and then not SUBT.Reachable then
1405 SUBT.Reachable := True;
1406 Modified := True;
1407 end if;
1408 end;
1409 end loop Inner;
1411 exit Outer when not Modified;
1412 end loop Outer;
1413 end Reachable_Closure;
1415 -- Remove calls from unreachable subprograms
1417 declare
1418 New_Index : Nat;
1420 begin
1421 New_Index := 0;
1422 for J in Calls.First .. Calls.Last loop
1423 declare
1424 CTJ : Call_Entry renames Calls.Table (J);
1426 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1427 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1429 SUBF : Subp_Entry renames Subps.Table (SINF);
1430 SUBT : Subp_Entry renames Subps.Table (SINT);
1432 begin
1433 if SUBF.Reachable then
1434 pragma Assert (SUBT.Reachable);
1435 New_Index := New_Index + 1;
1436 Calls.Table (New_Index) := Calls.Table (J);
1437 end if;
1438 end;
1439 end loop;
1441 Calls.Set_Last (New_Index);
1442 end;
1444 -- Remove uplevel references from unreachable subprograms
1446 declare
1447 New_Index : Nat;
1449 begin
1450 New_Index := 0;
1451 for J in Urefs.First .. Urefs.Last loop
1452 declare
1453 URJ : Uref_Entry renames Urefs.Table (J);
1455 SINF : constant SI_Type := Subp_Index (URJ.Caller);
1456 SINT : constant SI_Type := Subp_Index (URJ.Callee);
1458 SUBF : Subp_Entry renames Subps.Table (SINF);
1459 SUBT : Subp_Entry renames Subps.Table (SINT);
1461 S : Entity_Id;
1463 begin
1464 -- Keep reachable reference
1466 if SUBF.Reachable then
1467 New_Index := New_Index + 1;
1468 Urefs.Table (New_Index) := Urefs.Table (J);
1470 -- And since we know we are keeping this one, this is a good
1471 -- place to fill in information for a good reference.
1473 -- Mark all enclosing subprograms need to declare AREC
1475 S := URJ.Caller;
1476 loop
1477 S := Enclosing_Subprogram (S);
1479 -- If we are at the top level, as can happen with
1480 -- references to formals in aspects of nested subprogram
1481 -- declarations, there are no further subprograms to mark
1482 -- as requiring activation records.
1484 exit when No (S);
1486 declare
1487 SUBI : Subp_Entry renames Subps.Table (Subp_Index (S));
1488 begin
1489 SUBI.Declares_AREC := True;
1491 -- If this entity was marked reachable because it is
1492 -- in a task or protected type, there may not appear
1493 -- to be any calls to it, which would normally adjust
1494 -- the levels of the parent subprograms. So we need to
1495 -- be sure that the uplevel reference of that entity
1496 -- takes into account possible calls.
1498 if In_Synchronized_Unit (SUBF.Ent)
1499 and then SUBT.Lev < SUBI.Uplevel_Ref
1500 then
1501 SUBI.Uplevel_Ref := SUBT.Lev;
1502 end if;
1503 end;
1505 exit when S = URJ.Callee;
1506 end loop;
1508 -- Add to list of uplevel referenced entities for Callee.
1509 -- We do not add types to this list, only actual references
1510 -- to objects that will be referenced uplevel, and we use
1511 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1512 -- duplicate entries in the list. Discriminants are also
1513 -- excluded, only the enclosing object can appear in the
1514 -- list.
1516 if not Is_Uplevel_Referenced_Entity (URJ.Ent)
1517 and then Ekind (URJ.Ent) /= E_Discriminant
1518 then
1519 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
1520 Append_New_Elmt (URJ.Ent, SUBT.Uents);
1521 end if;
1523 -- And set uplevel indication for caller
1525 if SUBT.Lev < SUBF.Uplevel_Ref then
1526 SUBF.Uplevel_Ref := SUBT.Lev;
1527 end if;
1528 end if;
1529 end;
1530 end loop;
1532 Urefs.Set_Last (New_Index);
1533 end;
1535 -- Remove unreachable subprograms from Subps table. Note that we do
1536 -- this after eliminating entries from the other two tables, since
1537 -- those elimination steps depend on referencing the Subps table.
1539 declare
1540 New_SI : SI_Type;
1542 begin
1543 New_SI := Subps_First - 1;
1544 for J in Subps_First .. Subps.Last loop
1545 declare
1546 STJ : Subp_Entry renames Subps.Table (J);
1547 Spec : Node_Id;
1548 Decl : Node_Id;
1550 begin
1551 -- Subprogram is reachable, copy and reset index
1553 if STJ.Reachable then
1554 New_SI := New_SI + 1;
1555 Subps.Table (New_SI) := STJ;
1556 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
1558 -- Subprogram is not reachable
1560 else
1561 -- Clear index, since no longer active
1563 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
1565 -- Output debug information if -gnatd.3 set
1567 if Debug_Flag_Dot_3 then
1568 Write_Str ("Eliminate ");
1569 Write_Name (Chars (Subps.Table (J).Ent));
1570 Write_Str (" at ");
1571 Write_Location (Sloc (Subps.Table (J).Ent));
1572 Write_Str (" (not referenced)");
1573 Write_Eol;
1574 end if;
1576 -- Rewrite declaration, body, and corresponding freeze node
1577 -- to null statements.
1579 -- A subprogram instantiation does not have an explicit
1580 -- body. If unused, we could remove the corresponding
1581 -- wrapper package and its body.
1583 if Present (STJ.Bod) then
1584 Spec := Corresponding_Spec (STJ.Bod);
1586 if Present (Spec) then
1587 Decl := Parent (Declaration_Node (Spec));
1588 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
1590 if Present (Freeze_Node (Spec)) then
1591 Rewrite (Freeze_Node (Spec),
1592 Make_Null_Statement (Sloc (Decl)));
1593 end if;
1594 end if;
1596 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
1597 end if;
1598 end if;
1599 end;
1600 end loop;
1602 Subps.Set_Last (New_SI);
1603 end;
1605 -- Now it is time for the second transitive closure, which follows calls
1606 -- and makes sure that A calls B, and B has uplevel references, then A
1607 -- is also marked as having uplevel references.
1609 Closure_Uplevel : declare
1610 Modified : Boolean;
1612 begin
1613 -- We use a simple minded algorithm as follows (obviously this can
1614 -- be done more efficiently, using one of the standard algorithms
1615 -- for efficient transitive closure computation, but this is simple
1616 -- and most likely fast enough that its speed does not matter).
1618 -- Repeatedly scan the list of calls. Any time we find a call from
1619 -- A to B, where B has uplevel references, make sure that A is marked
1620 -- as having at least the same level of uplevel referencing.
1622 Outer2 : loop
1623 Modified := False;
1624 Inner2 : for J in Calls.First .. Calls.Last loop
1625 declare
1626 CTJ : Call_Entry renames Calls.Table (J);
1627 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1628 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1629 SUBF : Subp_Entry renames Subps.Table (SINF);
1630 SUBT : Subp_Entry renames Subps.Table (SINT);
1631 begin
1632 if SUBT.Lev > SUBT.Uplevel_Ref
1633 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
1634 then
1635 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
1636 Modified := True;
1637 end if;
1638 end;
1639 end loop Inner2;
1641 exit Outer2 when not Modified;
1642 end loop Outer2;
1643 end Closure_Uplevel;
1645 -- We have one more step before the tables are complete. An uplevel
1646 -- call from subprogram A to subprogram B where subprogram B has uplevel
1647 -- references is in effect an uplevel reference, and must arrange for
1648 -- the proper activation link to be passed.
1650 for J in Calls.First .. Calls.Last loop
1651 declare
1652 CTJ : Call_Entry renames Calls.Table (J);
1654 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1655 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1657 SUBF : Subp_Entry renames Subps.Table (SINF);
1658 SUBT : Subp_Entry renames Subps.Table (SINT);
1660 A : Entity_Id;
1662 begin
1663 -- If callee has uplevel references
1665 if SUBT.Uplevel_Ref < SUBT.Lev
1667 -- And this is an uplevel call
1669 and then SUBT.Lev < SUBF.Lev
1670 then
1671 -- We need to arrange for finding the uplink
1673 A := CTJ.Caller;
1674 loop
1675 A := Enclosing_Subprogram (A);
1676 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1677 exit when A = CTJ.Callee;
1679 -- In any case exit when we get to the outer level. This
1680 -- happens in some odd cases with generics (in particular
1681 -- sem_ch3.adb does not compile without this kludge ???).
1683 exit when A = Subp;
1684 end loop;
1685 end if;
1686 end;
1687 end loop;
1689 -- The tables are now complete, so we can record the last index in the
1690 -- Subps table for later reference in Cprint.
1692 Subps.Table (Subps_First).Last := Subps.Last;
1694 -- Next step, create the entities for code we will insert. We do this
1695 -- at the start so that all the entities are defined, regardless of the
1696 -- order in which we do the code insertions.
1698 Create_Entities : for J in Subps_First .. Subps.Last loop
1699 declare
1700 STJ : Subp_Entry renames Subps.Table (J);
1701 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1703 begin
1704 -- First we create the ARECnF entity for the additional formal for
1705 -- all subprograms which need an activation record passed.
1707 if STJ.Uplevel_Ref < STJ.Lev then
1708 STJ.ARECnF :=
1709 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1710 end if;
1712 -- Define the AREC entities for the activation record if needed
1714 if STJ.Declares_AREC then
1715 STJ.ARECn :=
1716 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1717 STJ.ARECnT :=
1718 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1719 STJ.ARECnPT :=
1720 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1721 STJ.ARECnP :=
1722 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1724 -- Define uplink component entity if inner nesting case
1726 if Present (STJ.ARECnF) then
1727 STJ.ARECnU :=
1728 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1729 end if;
1730 end if;
1731 end;
1732 end loop Create_Entities;
1734 -- Loop through subprograms
1736 Subp_Loop : declare
1737 Addr : Entity_Id := Empty;
1739 begin
1740 for J in Subps_First .. Subps.Last loop
1741 declare
1742 STJ : Subp_Entry renames Subps.Table (J);
1744 begin
1745 -- First add the extra formal if needed. This applies to all
1746 -- nested subprograms that require an activation record to be
1747 -- passed, as indicated by ARECnF being defined.
1749 if Present (STJ.ARECnF) then
1751 -- Here we need the extra formal. We do the expansion and
1752 -- analysis of this manually, since it is fairly simple,
1753 -- and it is not obvious how we can get what we want if we
1754 -- try to use the normal Analyze circuit.
1756 Add_Extra_Formal : declare
1757 Encl : constant SI_Type := Enclosing_Subp (J);
1758 STJE : Subp_Entry renames Subps.Table (Encl);
1759 -- Index and Subp_Entry for enclosing routine
1761 Form : constant Entity_Id := STJ.ARECnF;
1762 -- The formal to be added. Note that n here is one less
1763 -- than the level of the subprogram itself (STJ.Ent).
1765 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1766 -- S is an N_Function/Procedure_Specification node, and F
1767 -- is the new entity to add to this subprogram spec as
1768 -- the last Extra_Formal.
1770 ----------------------
1771 -- Add_Form_To_Spec --
1772 ----------------------
1774 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1775 Sub : constant Entity_Id := Defining_Entity (S);
1776 Ent : Entity_Id;
1778 begin
1779 -- Case of at least one Extra_Formal is present, set
1780 -- ARECnF as the new last entry in the list.
1782 if Present (Extra_Formals (Sub)) then
1783 Ent := Extra_Formals (Sub);
1784 while Present (Extra_Formal (Ent)) loop
1785 Ent := Extra_Formal (Ent);
1786 end loop;
1788 Set_Extra_Formal (Ent, F);
1790 -- No Extra formals present
1792 else
1793 Set_Extra_Formals (Sub, F);
1794 Ent := Last_Formal (Sub);
1796 if Present (Ent) then
1797 Set_Extra_Formal (Ent, F);
1798 end if;
1799 end if;
1800 end Add_Form_To_Spec;
1802 -- Start of processing for Add_Extra_Formal
1804 begin
1805 -- Decorate the new formal entity
1807 Set_Scope (Form, STJ.Ent);
1808 Mutate_Ekind (Form, E_In_Parameter);
1809 Set_Etype (Form, STJE.ARECnPT);
1810 Set_Mechanism (Form, By_Copy);
1811 Set_Never_Set_In_Source (Form, True);
1812 Set_Analyzed (Form, True);
1813 Set_Comes_From_Source (Form, False);
1814 Set_Is_Activation_Record (Form, True);
1816 -- Case of only body present
1818 if Acts_As_Spec (STJ.Bod) then
1819 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1821 -- Case of separate spec
1823 else
1824 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1825 end if;
1826 end Add_Extra_Formal;
1827 end if;
1829 -- Processing for subprograms that declare an activation record
1831 if Present (STJ.ARECn) then
1833 -- Local declarations for one such subprogram
1835 declare
1836 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1838 Decls : constant List_Id := New_List;
1839 -- List of new declarations we create
1841 Clist : List_Id;
1842 Comp : Entity_Id;
1844 Decl_Assign : Node_Id;
1845 -- Assignment to set uplink, Empty if none
1847 Decl_ARECnT : Node_Id;
1848 Decl_ARECnPT : Node_Id;
1849 Decl_ARECn : Node_Id;
1850 Decl_ARECnP : Node_Id;
1851 -- Declaration nodes for the AREC entities we build
1853 begin
1854 -- Build list of component declarations for ARECnT and
1855 -- load System.Address.
1857 Clist := Empty_List;
1859 if No (Addr) then
1860 Addr := RTE (RE_Address);
1861 end if;
1863 -- If we are in a subprogram that has a static link that
1864 -- is passed in (as indicated by ARECnF being defined),
1865 -- then include ARECnU : ARECmPT where ARECmPT comes from
1866 -- the level one higher than the current level, and the
1867 -- entity ARECnPT comes from the enclosing subprogram.
1869 if Present (STJ.ARECnF) then
1870 declare
1871 STJE : Subp_Entry
1872 renames Subps.Table (Enclosing_Subp (J));
1873 begin
1874 Append_To (Clist,
1875 Make_Component_Declaration (Loc,
1876 Defining_Identifier => STJ.ARECnU,
1877 Component_Definition =>
1878 Make_Component_Definition (Loc,
1879 Subtype_Indication =>
1880 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1881 end;
1882 end if;
1884 -- Add components for uplevel referenced entities
1886 if Present (STJ.Uents) then
1887 declare
1888 Elmt : Elmt_Id;
1889 Ptr_Decl : Node_Id;
1890 Uent : Entity_Id;
1892 Indx : Nat;
1893 -- 1's origin of index in list of elements. This is
1894 -- used to uniquify names if needed in Upref_Name.
1896 begin
1897 Elmt := First_Elmt (STJ.Uents);
1898 Indx := 0;
1899 while Present (Elmt) loop
1900 Uent := Node (Elmt);
1901 Indx := Indx + 1;
1903 Comp :=
1904 Make_Defining_Identifier (Loc,
1905 Chars => Upref_Name (Uent, Indx, Clist));
1907 Set_Activation_Record_Component
1908 (Uent, Comp);
1910 if Needs_Fat_Pointer (Uent) then
1912 -- Build corresponding access type
1914 Ptr_Decl :=
1915 Build_Access_Type_Decl
1916 (Etype (Uent), STJ.Ent);
1917 Append_To (Decls, Ptr_Decl);
1919 -- And use its type in the corresponding
1920 -- component.
1922 Append_To (Clist,
1923 Make_Component_Declaration (Loc,
1924 Defining_Identifier => Comp,
1925 Component_Definition =>
1926 Make_Component_Definition (Loc,
1927 Subtype_Indication =>
1928 New_Occurrence_Of
1929 (Defining_Identifier (Ptr_Decl),
1930 Loc))));
1931 else
1932 Append_To (Clist,
1933 Make_Component_Declaration (Loc,
1934 Defining_Identifier => Comp,
1935 Component_Definition =>
1936 Make_Component_Definition (Loc,
1937 Subtype_Indication =>
1938 New_Occurrence_Of (Addr, Loc))));
1939 end if;
1940 Next_Elmt (Elmt);
1941 end loop;
1942 end;
1943 end if;
1945 -- Now we can insert the AREC declarations into the body
1946 -- type ARECnT is record .. end record;
1947 -- pragma Suppress_Initialization (ARECnT);
1949 -- Note that we need to set the Suppress_Initialization
1950 -- flag after Decl_ARECnT has been analyzed.
1952 Decl_ARECnT :=
1953 Make_Full_Type_Declaration (Loc,
1954 Defining_Identifier => STJ.ARECnT,
1955 Type_Definition =>
1956 Make_Record_Definition (Loc,
1957 Component_List =>
1958 Make_Component_List (Loc,
1959 Component_Items => Clist)));
1960 Append_To (Decls, Decl_ARECnT);
1962 -- type ARECnPT is access all ARECnT;
1964 Decl_ARECnPT :=
1965 Make_Full_Type_Declaration (Loc,
1966 Defining_Identifier => STJ.ARECnPT,
1967 Type_Definition =>
1968 Make_Access_To_Object_Definition (Loc,
1969 All_Present => True,
1970 Subtype_Indication =>
1971 New_Occurrence_Of (STJ.ARECnT, Loc)));
1972 Append_To (Decls, Decl_ARECnPT);
1974 -- ARECn : aliased ARECnT;
1976 Decl_ARECn :=
1977 Make_Object_Declaration (Loc,
1978 Defining_Identifier => STJ.ARECn,
1979 Aliased_Present => True,
1980 Object_Definition =>
1981 New_Occurrence_Of (STJ.ARECnT, Loc));
1982 Append_To (Decls, Decl_ARECn);
1984 -- ARECnP : constant ARECnPT := ARECn'Access;
1986 Decl_ARECnP :=
1987 Make_Object_Declaration (Loc,
1988 Defining_Identifier => STJ.ARECnP,
1989 Constant_Present => True,
1990 Object_Definition =>
1991 New_Occurrence_Of (STJ.ARECnPT, Loc),
1992 Expression =>
1993 Make_Attribute_Reference (Loc,
1994 Prefix =>
1995 New_Occurrence_Of (STJ.ARECn, Loc),
1996 Attribute_Name => Name_Access));
1997 Append_To (Decls, Decl_ARECnP);
1999 -- If we are in a subprogram that has a static link that
2000 -- is passed in (as indicated by ARECnF being defined),
2001 -- then generate ARECn.ARECmU := ARECmF where m is
2002 -- one less than the current level to set the uplink.
2004 if Present (STJ.ARECnF) then
2005 Decl_Assign :=
2006 Make_Assignment_Statement (Loc,
2007 Name =>
2008 Make_Selected_Component (Loc,
2009 Prefix =>
2010 New_Occurrence_Of (STJ.ARECn, Loc),
2011 Selector_Name =>
2012 New_Occurrence_Of (STJ.ARECnU, Loc)),
2013 Expression =>
2014 New_Occurrence_Of (STJ.ARECnF, Loc));
2015 Append_To (Decls, Decl_Assign);
2017 else
2018 Decl_Assign := Empty;
2019 end if;
2021 if No (Declarations (STJ.Bod)) then
2022 Set_Declarations (STJ.Bod, Decls);
2023 else
2024 Prepend_List_To (Declarations (STJ.Bod), Decls);
2025 end if;
2027 -- Analyze the newly inserted declarations. Note that we
2028 -- do not need to establish the whole scope stack, since
2029 -- we have already set all entity fields (so there will
2030 -- be no searching of upper scopes to resolve names). But
2031 -- we do set the scope of the current subprogram, so that
2032 -- newly created entities go in the right entity chain.
2034 -- We analyze with all checks suppressed (since we do
2035 -- not expect any exceptions).
2037 Push_Scope (STJ.Ent);
2038 Analyze (Decl_ARECnT, Suppress => All_Checks);
2040 -- Note that we need to call Set_Suppress_Initialization
2041 -- after Decl_ARECnT has been analyzed, but before
2042 -- analyzing Decl_ARECnP so that the flag is properly
2043 -- taking into account.
2045 Set_Suppress_Initialization (STJ.ARECnT);
2047 Analyze (Decl_ARECnPT, Suppress => All_Checks);
2048 Analyze (Decl_ARECn, Suppress => All_Checks);
2049 Analyze (Decl_ARECnP, Suppress => All_Checks);
2051 if Present (Decl_Assign) then
2052 Analyze (Decl_Assign, Suppress => All_Checks);
2053 end if;
2055 Pop_Scope;
2057 -- Next step, for each uplevel referenced entity, add
2058 -- assignment operations to set the component in the
2059 -- activation record.
2061 if Present (STJ.Uents) then
2062 declare
2063 Elmt : Elmt_Id;
2065 begin
2066 Elmt := First_Elmt (STJ.Uents);
2067 while Present (Elmt) loop
2068 declare
2069 Ent : constant Entity_Id := Node (Elmt);
2070 Loc : constant Source_Ptr := Sloc (Ent);
2071 Dec : constant Node_Id :=
2072 Declaration_Node (Ent);
2074 Asn : Node_Id;
2075 Attr : Name_Id;
2076 Comp : Entity_Id;
2077 Ins : Node_Id;
2078 Rhs : Node_Id;
2080 begin
2081 -- For parameters, we insert the assignment
2082 -- right after the declaration of ARECnP.
2083 -- For all other entities, we insert the
2084 -- assignment immediately after the
2085 -- declaration of the entity or after the
2086 -- freeze node if present.
2088 -- Note: we don't need to mark the entity
2089 -- as being aliased, because the address
2090 -- attribute will mark it as Address_Taken,
2091 -- and that is good enough.
2093 if Is_Formal (Ent) then
2094 Ins := Decl_ARECnP;
2096 elsif Has_Delayed_Freeze (Ent) then
2097 Ins := Freeze_Node (Ent);
2099 else
2100 Ins := Dec;
2101 end if;
2103 -- Build and insert the assignment:
2104 -- ARECn.nam := nam'Address
2105 -- or else 'Unchecked_Access for
2106 -- unconstrained array.
2108 if Needs_Fat_Pointer (Ent) then
2109 Attr := Name_Unchecked_Access;
2110 else
2111 Attr := Name_Address;
2112 end if;
2114 Rhs :=
2115 Make_Attribute_Reference (Loc,
2116 Prefix =>
2117 New_Occurrence_Of (Ent, Loc),
2118 Attribute_Name => Attr);
2120 -- If the entity is an unconstrained formal
2121 -- we wrap the attribute reference in an
2122 -- unchecked conversion to the type of the
2123 -- activation record component, to prevent
2124 -- spurious subtype conformance errors within
2125 -- instances.
2127 if Is_Formal (Ent)
2128 and then not Is_Constrained (Etype (Ent))
2129 then
2130 -- Find target component and its type
2132 Comp := First_Component (STJ.ARECnT);
2133 while Chars (Comp) /= Chars (Ent) loop
2134 Next_Component (Comp);
2135 end loop;
2137 Rhs :=
2138 Unchecked_Convert_To (Etype (Comp), Rhs);
2139 end if;
2141 Asn :=
2142 Make_Assignment_Statement (Loc,
2143 Name =>
2144 Make_Selected_Component (Loc,
2145 Prefix =>
2146 New_Occurrence_Of (STJ.ARECn, Loc),
2147 Selector_Name =>
2148 New_Occurrence_Of
2149 (Activation_Record_Component
2150 (Ent),
2151 Loc)),
2152 Expression => Rhs);
2154 -- If we have a loop parameter, we have
2155 -- to insert before the first statement
2156 -- of the loop. Ins points to the
2157 -- N_Loop_Parameter_Specification or to
2158 -- an N_Iterator_Specification.
2160 if Nkind (Ins) in
2161 N_Iterator_Specification |
2162 N_Loop_Parameter_Specification
2163 then
2164 -- Quantified expression are rewritten as
2165 -- loops during expansion.
2167 if Nkind (Parent (Ins)) =
2168 N_Quantified_Expression
2169 then
2170 null;
2172 else
2173 Ins :=
2174 First
2175 (Statements
2176 (Parent (Parent (Ins))));
2177 Insert_Before (Ins, Asn);
2178 end if;
2180 else
2181 Insert_After (Ins, Asn);
2182 end if;
2184 -- Analyze the assignment statement. We do
2185 -- not need to establish the relevant scope
2186 -- stack entries here, because we have
2187 -- already set the correct entity references,
2188 -- so no name resolution is required, and no
2189 -- new entities are created, so we don't even
2190 -- need to set the current scope.
2192 -- We analyze with all checks suppressed
2193 -- (since we do not expect any exceptions).
2195 Analyze (Asn, Suppress => All_Checks);
2196 end;
2198 Next_Elmt (Elmt);
2199 end loop;
2200 end;
2201 end if;
2202 end;
2203 end if;
2204 end;
2205 end loop;
2206 end Subp_Loop;
2208 -- Next step, process uplevel references. This has to be done in a
2209 -- separate pass, after completing the processing in Sub_Loop because we
2210 -- need all the AREC declarations generated, inserted, and analyzed so
2211 -- that the uplevel references can be successfully analyzed.
2213 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
2214 declare
2215 UPJ : Uref_Entry renames Urefs.Table (J);
2217 begin
2218 -- Ignore type references, these are implicit references that do
2219 -- not need rewriting (e.g. the appearance in a conversion).
2220 -- Also ignore if no reference was specified or if the rewriting
2221 -- has already been done (this can happen if the N_Identifier
2222 -- occurs more than one time in the tree). Also ignore references
2223 -- when not generating C code (in particular for the case of LLVM,
2224 -- since GNAT-LLVM will handle the processing for up-level refs).
2226 if No (UPJ.Ref)
2227 or else not Is_Entity_Name (UPJ.Ref)
2228 or else No (Entity (UPJ.Ref))
2229 or else not Opt.Generate_C_Code
2230 then
2231 goto Continue;
2232 end if;
2234 -- Rewrite one reference
2236 Rewrite_One_Ref : declare
2237 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
2238 -- Source location for the reference
2240 Typ : constant Entity_Id := Etype (UPJ.Ent);
2241 -- The type of the referenced entity
2243 Atyp : Entity_Id;
2244 -- The actual subtype of the reference
2246 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
2247 -- Subp_Index for caller containing reference
2249 STJR : Subp_Entry renames Subps.Table (RS_Caller);
2250 -- Subp_Entry for subprogram containing reference
2252 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
2253 -- Subp_Index for subprogram containing referenced entity
2255 STJE : Subp_Entry renames Subps.Table (RS_Callee);
2256 -- Subp_Entry for subprogram containing referenced entity
2258 Pfx : Node_Id;
2259 Comp : Entity_Id;
2260 SI : SI_Type;
2262 begin
2263 Atyp := Etype (UPJ.Ref);
2265 if Ekind (Atyp) /= E_Record_Subtype then
2266 Atyp := Get_Actual_Subtype (UPJ.Ref);
2267 end if;
2269 -- Ignore if no ARECnF entity for enclosing subprogram which
2270 -- probably happens as a result of not properly treating
2271 -- instance bodies. To be examined ???
2273 -- If this test is omitted, then the compilation of freeze.adb
2274 -- and inline.adb fail in unnesting mode.
2276 if No (STJR.ARECnF) then
2277 goto Continue;
2278 end if;
2280 -- If this is a reference to a global constant, use its value
2281 -- rather than create a reference. It is more efficient and
2282 -- furthermore indispensable if the context requires a
2283 -- constant, such as a branch of a case statement.
2285 if Ekind (UPJ.Ent) = E_Constant
2286 and then Is_True_Constant (UPJ.Ent)
2287 and then Present (Constant_Value (UPJ.Ent))
2288 and then Is_Static_Expression (Constant_Value (UPJ.Ent))
2289 then
2290 Rewrite (UPJ.Ref, New_Copy_Tree (Constant_Value (UPJ.Ent)));
2291 goto Continue;
2292 end if;
2294 -- Push the current scope, so that the pointer type Tnn, and
2295 -- any subsidiary entities resulting from the analysis of the
2296 -- rewritten reference, go in the right entity chain.
2298 Push_Scope (STJR.Ent);
2300 -- Now we need to rewrite the reference. We have a reference
2301 -- from level STJR.Lev to level STJE.Lev. The general form of
2302 -- the rewritten reference for entity X is:
2304 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2306 -- where a,b,c,d .. m =
2307 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2309 pragma Assert (STJR.Lev > STJE.Lev);
2311 -- Compute the prefix of X. Here are examples to make things
2312 -- clear (with parens to show groupings, the prefix is
2313 -- everything except the .X at the end).
2315 -- level 2 to level 1
2317 -- AREC1F.X
2319 -- level 3 to level 1
2321 -- (AREC2F.AREC1U).X
2323 -- level 4 to level 1
2325 -- ((AREC3F.AREC2U).AREC1U).X
2327 -- level 6 to level 2
2329 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2331 -- In the above, ARECnF and ARECnU are pointers, so there are
2332 -- explicit dereferences required for these occurrences.
2334 Pfx :=
2335 Make_Explicit_Dereference (Loc,
2336 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
2337 SI := RS_Caller;
2338 for L in STJE.Lev .. STJR.Lev - 2 loop
2339 SI := Enclosing_Subp (SI);
2340 Pfx :=
2341 Make_Explicit_Dereference (Loc,
2342 Prefix =>
2343 Make_Selected_Component (Loc,
2344 Prefix => Pfx,
2345 Selector_Name =>
2346 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
2347 end loop;
2349 -- Get activation record component (must exist)
2351 Comp := Activation_Record_Component (UPJ.Ent);
2352 pragma Assert (Present (Comp));
2354 -- Do the replacement. If the component type is an access type,
2355 -- this is an uplevel reference for an entity that requires a
2356 -- fat pointer, so dereference the component.
2358 if Is_Access_Type (Etype (Comp)) then
2359 Rewrite (UPJ.Ref,
2360 Make_Explicit_Dereference (Loc,
2361 Prefix =>
2362 Make_Selected_Component (Loc,
2363 Prefix => Pfx,
2364 Selector_Name =>
2365 New_Occurrence_Of (Comp, Loc))));
2367 else
2368 Rewrite (UPJ.Ref,
2369 Make_Attribute_Reference (Loc,
2370 Prefix => New_Occurrence_Of (Atyp, Loc),
2371 Attribute_Name => Name_Deref,
2372 Expressions => New_List (
2373 Make_Selected_Component (Loc,
2374 Prefix => Pfx,
2375 Selector_Name =>
2376 New_Occurrence_Of (Comp, Loc)))));
2377 end if;
2379 -- Analyze and resolve the new expression. We do not need to
2380 -- establish the relevant scope stack entries here, because we
2381 -- have already set all the correct entity references, so no
2382 -- name resolution is needed. We have already set the current
2383 -- scope, so that any new entities created will be in the right
2384 -- scope.
2386 -- We analyze with all checks suppressed (since we do not
2387 -- expect any exceptions)
2389 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
2391 -- Generate an extra temporary to facilitate the C backend
2392 -- processing this dereference
2394 if Opt.Modify_Tree_For_C
2395 and then Nkind (Parent (UPJ.Ref)) in
2396 N_Type_Conversion | N_Unchecked_Type_Conversion
2397 then
2398 Force_Evaluation (UPJ.Ref, Mode => Strict);
2399 end if;
2401 Pop_Scope;
2402 end Rewrite_One_Ref;
2403 end;
2405 <<Continue>>
2406 null;
2407 end loop Uplev_Refs;
2409 -- Finally, loop through all calls adding extra actual for the
2410 -- activation record where it is required.
2412 Adjust_Calls : for J in Calls.First .. Calls.Last loop
2414 -- Process a single call, we are only interested in a call to a
2415 -- subprogram that actually needs a pointer to an activation record,
2416 -- as indicated by the ARECnF entity being set. This excludes the
2417 -- top level subprogram, and any subprogram not having uplevel refs.
2419 Adjust_One_Call : declare
2420 CTJ : Call_Entry renames Calls.Table (J);
2421 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
2422 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
2424 Loc : constant Source_Ptr := Sloc (CTJ.N);
2426 Extra : Node_Id;
2427 ExtraP : Node_Id;
2428 SubX : SI_Type;
2429 Act : Node_Id;
2431 begin
2432 if Present (STT.ARECnF)
2433 and then Nkind (CTJ.N) in N_Subprogram_Call
2434 then
2435 -- CTJ.N is a call to a subprogram which may require a pointer
2436 -- to an activation record. The subprogram containing the call
2437 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2438 -- have a call from level STF.Lev to level STT.Lev.
2440 -- There are three possibilities:
2442 -- For a call to the same level, we just pass the activation
2443 -- record passed to the calling subprogram.
2445 if STF.Lev = STT.Lev then
2446 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2448 -- For a call that goes down a level, we pass a pointer to the
2449 -- activation record constructed within the caller (which may
2450 -- be the outer-level subprogram, but also may be a more deeply
2451 -- nested caller).
2453 elsif STT.Lev = STF.Lev + 1 then
2454 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
2456 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2457 -- since it is not possible to do a downcall of more than
2458 -- one level.
2460 -- For a call from level STF.Lev to level STT.Lev, we
2461 -- have to find the activation record needed by the
2462 -- callee. This is as follows:
2464 -- ARECaF.ARECbU.ARECcU....ARECmU
2466 -- where a,b,c .. m =
2467 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2469 else
2470 pragma Assert (STT.Lev < STF.Lev);
2472 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2473 SubX := Subp_Index (CTJ.Caller);
2474 for K in reverse STT.Lev .. STF.Lev - 1 loop
2475 SubX := Enclosing_Subp (SubX);
2476 Extra :=
2477 Make_Selected_Component (Loc,
2478 Prefix => Extra,
2479 Selector_Name =>
2480 New_Occurrence_Of
2481 (Subps.Table (SubX).ARECnU, Loc));
2482 end loop;
2483 end if;
2485 -- Extra is the additional parameter to be added. Build a
2486 -- parameter association that we can append to the actuals.
2488 ExtraP :=
2489 Make_Parameter_Association (Loc,
2490 Selector_Name =>
2491 New_Occurrence_Of (STT.ARECnF, Loc),
2492 Explicit_Actual_Parameter => Extra);
2494 if No (Parameter_Associations (CTJ.N)) then
2495 Set_Parameter_Associations (CTJ.N, Empty_List);
2496 end if;
2498 Append (ExtraP, Parameter_Associations (CTJ.N));
2500 -- We need to deal with the actual parameter chain as well. The
2501 -- newly added parameter is always the last actual.
2503 Act := First_Named_Actual (CTJ.N);
2505 if No (Act) then
2506 Set_First_Named_Actual (CTJ.N, Extra);
2508 -- If call has been relocated (as with an expression in
2509 -- an aggregate), set First_Named pointer in original node
2510 -- as well, because that's the parent of the parameter list.
2512 Set_First_Named_Actual
2513 (Parent (List_Containing (ExtraP)), Extra);
2515 -- Here we must follow the chain and append the new entry
2517 else
2518 loop
2519 declare
2520 PAN : Node_Id;
2521 NNA : Node_Id;
2523 begin
2524 PAN := Parent (Act);
2525 pragma Assert (Nkind (PAN) = N_Parameter_Association);
2526 NNA := Next_Named_Actual (PAN);
2528 if No (NNA) then
2529 Set_Next_Named_Actual (PAN, Extra);
2530 exit;
2531 end if;
2533 Act := NNA;
2534 end;
2535 end loop;
2536 end if;
2538 -- Analyze and resolve the new actual. We do not need to
2539 -- establish the relevant scope stack entries here, because
2540 -- we have already set all the correct entity references, so
2541 -- no name resolution is needed.
2543 -- We analyze with all checks suppressed (since we do not
2544 -- expect any exceptions, and also we temporarily turn off
2545 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2546 -- references (not needed at this stage, and in fact causes
2547 -- a bit of recursive chaos).
2549 Opt.Unnest_Subprogram_Mode := False;
2550 Analyze_And_Resolve
2551 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
2552 Opt.Unnest_Subprogram_Mode := True;
2553 end if;
2554 end Adjust_One_Call;
2555 end loop Adjust_Calls;
2557 return;
2558 end Unnest_Subprogram;
2560 ------------------------
2561 -- Unnest_Subprograms --
2562 ------------------------
2564 procedure Unnest_Subprograms (N : Node_Id) is
2565 function Search_Subprograms (N : Node_Id) return Traverse_Result;
2566 -- Tree visitor that search for outer level procedures with nested
2567 -- subprograms and invokes Unnest_Subprogram()
2569 ---------------
2570 -- Do_Search --
2571 ---------------
2573 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
2574 -- Subtree visitor instantiation
2576 ------------------------
2577 -- Search_Subprograms --
2578 ------------------------
2580 function Search_Subprograms (N : Node_Id) return Traverse_Result is
2581 begin
2582 if Nkind (N) in N_Subprogram_Body | N_Subprogram_Body_Stub then
2583 declare
2584 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
2586 begin
2587 -- We are only interested in subprograms (not generic
2588 -- subprograms), that have nested subprograms.
2590 if Is_Subprogram (Spec_Id)
2591 and then Has_Nested_Subprogram (Spec_Id)
2592 and then Is_Library_Level_Entity (Spec_Id)
2593 then
2594 Unnest_Subprogram (Spec_Id, N);
2595 else
2596 return Skip;
2597 end if;
2598 end;
2600 -- The proper body of a stub may contain nested subprograms, and
2601 -- therefore must be visited explicitly. Nested stubs are examined
2602 -- recursively in Visit_Node.
2604 elsif Nkind (N) in N_Body_Stub then
2605 Do_Search (Library_Unit (N));
2607 -- Skip generic packages
2609 elsif Nkind (N) = N_Package_Body
2610 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
2611 then
2612 return Skip;
2613 end if;
2615 return OK;
2616 end Search_Subprograms;
2618 Subp : Entity_Id;
2619 Subp_Body : Node_Id;
2621 -- Start of processing for Unnest_Subprograms
2623 begin
2624 if not Opt.Unnest_Subprogram_Mode or not Opt.Expander_Active then
2625 return;
2626 end if;
2628 -- A specification will contain bodies if it contains instantiations so
2629 -- examine package or subprogram declaration of the main unit, when it
2630 -- is present.
2632 if Nkind (Unit (N)) = N_Package_Body
2633 or else (Nkind (Unit (N)) = N_Subprogram_Body
2634 and then not Acts_As_Spec (N))
2635 then
2636 Do_Search (Library_Unit (N));
2637 end if;
2639 Do_Search (N);
2641 -- Unnest any subprograms passed on the list of inlined subprograms
2643 Subp := First_Inlined_Subprogram (N);
2645 while Present (Subp) loop
2646 Subp_Body := Parent (Declaration_Node (Subp));
2648 if Nkind (Subp_Body) = N_Subprogram_Declaration
2649 and then Present (Corresponding_Body (Subp_Body))
2650 then
2651 Subp_Body := Parent (Declaration_Node
2652 (Corresponding_Body (Subp_Body)));
2653 end if;
2655 Unnest_Subprogram (Subp, Subp_Body, For_Inline => True);
2656 Next_Inlined_Subprogram (Subp);
2657 end loop;
2658 end Unnest_Subprograms;
2660 end Exp_Unst;