[Ada] Unnesting: avoid unnecessary loads of System
[official-gcc.git] / gcc / ada / exp_unst.adb
blob0b63aa66fc59de535e130ab096f74d18f8d01b91
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-2018, 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 Elists; use Elists;
30 with Lib; use Lib;
31 with Namet; use Namet;
32 with Nlists; use Nlists;
33 with Nmake; use Nmake;
34 with Opt;
35 with Output; use Output;
36 with Rtsfind; use Rtsfind;
37 with Sem; use Sem;
38 with Sem_Aux; use Sem_Aux;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Mech; use Sem_Mech;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Sinput; use Sinput;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Tbuild; use Tbuild;
48 with Uintp; use Uintp;
50 package body Exp_Unst is
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
56 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id);
57 -- Subp is a library-level subprogram which has nested subprograms, and
58 -- Subp_Body is the corresponding N_Subprogram_Body node. This procedure
59 -- declares the AREC types and objects, adds assignments to the AREC record
60 -- as required, defines the xxxPTR types for uplevel referenced objects,
61 -- adds the ARECP parameter to all nested subprograms which need it, and
62 -- modifies all uplevel references appropriately.
64 -----------
65 -- Calls --
66 -----------
68 -- Table to record calls within the nest being analyzed. These are the
69 -- calls which may need to have an AREC actual added. This table is built
70 -- new for each subprogram nest and cleared at the end of processing each
71 -- subprogram nest.
73 type Call_Entry is record
74 N : Node_Id;
75 -- The actual call
77 Caller : Entity_Id;
78 -- Entity of the subprogram containing the call (can be at any level)
80 Callee : Entity_Id;
81 -- Entity of the subprogram called (always at level 2 or higher). Note
82 -- that in accordance with the basic rules of nesting, the level of To
83 -- is either less than or equal to the level of From, or one greater.
84 end record;
86 package Calls is new Table.Table (
87 Table_Component_Type => Call_Entry,
88 Table_Index_Type => Nat,
89 Table_Low_Bound => 1,
90 Table_Initial => 100,
91 Table_Increment => 200,
92 Table_Name => "Unnest_Calls");
93 -- Records each call within the outer subprogram and all nested subprograms
94 -- that are to other subprograms nested within the outer subprogram. These
95 -- are the calls that may need an additional parameter.
97 procedure Append_Unique_Call (Call : Call_Entry);
98 -- Append a call entry to the Calls table. A check is made to see if the
99 -- table already contains this entry and if so it has no effect.
101 ----------------------------------
102 -- Subprograms For Fat Pointers --
103 ----------------------------------
105 function Build_Access_Type_Decl
106 (E : Entity_Id;
107 Scop : Entity_Id) return Node_Id;
108 -- For an uplevel reference that involves an unconstrained array type,
109 -- build an access type declaration for the corresponding activation
110 -- record component. The relevant attributes of the access type are
111 -- set here to avoid a full analysis that would require a scope stack.
113 function Needs_Fat_Pointer (E : Entity_Id) return Boolean;
114 -- A formal parameter of an unconstrained array type that appears in an
115 -- uplevel reference requires the construction of an access type, to be
116 -- used in the corresponding component declaration.
118 -----------
119 -- Urefs --
120 -----------
122 -- Table to record explicit uplevel references to objects (variables,
123 -- constants, formal parameters). These are the references that will
124 -- need rewriting to use the activation table (AREC) pointers. Also
125 -- included are implicit and explicit uplevel references to types, but
126 -- these do not get rewritten by the front end. This table is built new
127 -- for each subprogram nest and cleared at the end of processing each
128 -- subprogram nest.
130 type Uref_Entry is record
131 Ref : Node_Id;
132 -- The reference itself. For objects this is always an entity reference
133 -- and the referenced entity will have its Is_Uplevel_Referenced_Entity
134 -- flag set and will appear in the Uplevel_Referenced_Entities list of
135 -- the subprogram declaring this entity.
137 Ent : Entity_Id;
138 -- The Entity_Id of the uplevel referenced object or type
140 Caller : Entity_Id;
141 -- The entity for the subprogram immediately containing this entity
143 Callee : Entity_Id;
144 -- The entity for the subprogram containing the referenced entity. Note
145 -- that the level of Callee must be less than the level of Caller, since
146 -- this is an uplevel reference.
147 end record;
149 package Urefs is new Table.Table (
150 Table_Component_Type => Uref_Entry,
151 Table_Index_Type => Nat,
152 Table_Low_Bound => 1,
153 Table_Initial => 100,
154 Table_Increment => 200,
155 Table_Name => "Unnest_Urefs");
157 ------------------------
158 -- Append_Unique_Call --
159 ------------------------
161 procedure Append_Unique_Call (Call : Call_Entry) is
162 begin
163 for J in Calls.First .. Calls.Last loop
164 if Calls.Table (J) = Call then
165 return;
166 end if;
167 end loop;
169 Calls.Append (Call);
170 end Append_Unique_Call;
172 -----------------------------
173 -- Build_Access_Type_Decl --
174 -----------------------------
176 function Build_Access_Type_Decl
177 (E : Entity_Id;
178 Scop : Entity_Id) return Node_Id
180 Loc : constant Source_Ptr := Sloc (E);
181 Typ : Entity_Id;
183 begin
184 Typ := Make_Temporary (Loc, 'S');
185 Set_Ekind (Typ, E_General_Access_Type);
186 Set_Etype (Typ, Typ);
187 Set_Scope (Typ, Scop);
188 Set_Directly_Designated_Type (Typ, Etype (E));
190 return
191 Make_Full_Type_Declaration (Loc,
192 Defining_Identifier => Typ,
193 Type_Definition =>
194 Make_Access_To_Object_Definition (Loc,
195 Subtype_Indication => New_Occurrence_Of (Etype (E), Loc)));
196 end Build_Access_Type_Decl;
198 ---------------
199 -- Get_Level --
200 ---------------
202 function Get_Level (Subp : Entity_Id; Sub : Entity_Id) return Nat is
203 Lev : Nat;
204 S : Entity_Id;
206 begin
207 Lev := 1;
208 S := Sub;
209 loop
210 if S = Subp then
211 return Lev;
212 else
213 Lev := Lev + 1;
214 S := Enclosing_Subprogram (S);
215 end if;
216 end loop;
217 end Get_Level;
219 --------------------------
220 -- In_Synchronized_Unit --
221 --------------------------
223 function In_Synchronized_Unit (Subp : Entity_Id) return Boolean is
224 S : Entity_Id := Scope (Subp);
226 begin
227 while Present (S) and then S /= Standard_Standard loop
228 if Is_Concurrent_Type (S) then
229 return True;
230 end if;
232 S := Scope (S);
233 end loop;
235 return False;
236 end In_Synchronized_Unit;
238 -----------------------
239 -- Needs_Fat_Pointer --
240 -----------------------
242 function Needs_Fat_Pointer (E : Entity_Id) return Boolean is
243 begin
244 return Is_Formal (E)
245 and then Is_Array_Type (Etype (E))
246 and then not Is_Constrained (Etype (E));
247 end Needs_Fat_Pointer;
249 ----------------
250 -- Subp_Index --
251 ----------------
253 function Subp_Index (Sub : Entity_Id) return SI_Type is
254 E : Entity_Id := Sub;
256 begin
257 pragma Assert (Is_Subprogram (E));
259 if Subps_Index (E) = Uint_0 then
260 E := Ultimate_Alias (E);
262 -- The body of a protected operation has a different name and
263 -- has been scanned at this point, and thus has an entry in the
264 -- subprogram table.
266 if E = Sub and then Convention (E) = Convention_Protected then
267 E := Protected_Body_Subprogram (E);
268 end if;
270 if Ekind (E) = E_Function
271 and then Rewritten_For_C (E)
272 and then Present (Corresponding_Procedure (E))
273 then
274 E := Corresponding_Procedure (E);
275 end if;
276 end if;
278 pragma Assert (Subps_Index (E) /= Uint_0);
279 return SI_Type (UI_To_Int (Subps_Index (E)));
280 end Subp_Index;
282 -----------------------
283 -- Unnest_Subprogram --
284 -----------------------
286 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id) is
287 function AREC_Name (J : Pos; S : String) return Name_Id;
288 -- Returns name for string ARECjS, where j is the decimal value of j
290 function Enclosing_Subp (Subp : SI_Type) return SI_Type;
291 -- Subp is the index of a subprogram which has a Lev greater than 1.
292 -- This function returns the index of the enclosing subprogram which
293 -- will have a Lev value one less than this.
295 function Img_Pos (N : Pos) return String;
296 -- Return image of N without leading blank
298 function Upref_Name
299 (Ent : Entity_Id;
300 Index : Pos;
301 Clist : List_Id) return Name_Id;
302 -- This function returns the name to be used in the activation record to
303 -- reference the variable uplevel. Clist is the list of components that
304 -- have been created in the activation record so far. Normally the name
305 -- is just a copy of the Chars field of the entity. The exception is
306 -- when the name has already been used, in which case we suffix the name
307 -- with the index value Index to avoid duplication. This happens with
308 -- declare blocks and generic parameters at least.
310 ---------------
311 -- AREC_Name --
312 ---------------
314 function AREC_Name (J : Pos; S : String) return Name_Id is
315 begin
316 return Name_Find ("AREC" & Img_Pos (J) & S);
317 end AREC_Name;
319 --------------------
320 -- Enclosing_Subp --
321 --------------------
323 function Enclosing_Subp (Subp : SI_Type) return SI_Type is
324 STJ : Subp_Entry renames Subps.Table (Subp);
325 Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
326 begin
327 pragma Assert (STJ.Lev > 1);
328 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
329 return Ret;
330 end Enclosing_Subp;
332 -------------
333 -- Img_Pos --
334 -------------
336 function Img_Pos (N : Pos) return String is
337 Buf : String (1 .. 20);
338 Ptr : Natural;
339 NV : Nat;
341 begin
342 Ptr := Buf'Last;
343 NV := N;
344 while NV /= 0 loop
345 Buf (Ptr) := Character'Val (48 + NV mod 10);
346 Ptr := Ptr - 1;
347 NV := NV / 10;
348 end loop;
350 return Buf (Ptr + 1 .. Buf'Last);
351 end Img_Pos;
353 ----------------
354 -- Upref_Name --
355 ----------------
357 function Upref_Name
358 (Ent : Entity_Id;
359 Index : Pos;
360 Clist : List_Id) return Name_Id
362 C : Node_Id;
363 begin
364 C := First (Clist);
365 loop
366 if No (C) then
367 return Chars (Ent);
369 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
370 return
371 Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
372 else
373 Next (C);
374 end if;
375 end loop;
376 end Upref_Name;
378 -- Start of processing for Unnest_Subprogram
380 begin
381 -- Nothing to do inside a generic (all processing is for instance)
383 if Inside_A_Generic then
384 return;
385 end if;
387 -- If the main unit is a package body then we need to examine the spec
388 -- to determine whether the main unit is generic (the scope stack is not
389 -- present when this is called on the main unit).
391 if Ekind (Cunit_Entity (Main_Unit)) = E_Package_Body
392 and then Is_Generic_Unit (Spec_Entity (Cunit_Entity (Main_Unit)))
393 then
394 return;
395 end if;
397 -- Only unnest when generating code for the main source unit
399 if not In_Extended_Main_Code_Unit (Subp_Body) then
400 return;
401 end if;
403 -- This routine is called late, after the scope stack is gone. The
404 -- following creates a suitable dummy scope stack to be used for the
405 -- analyze/expand calls made from this routine.
407 Push_Scope (Subp);
409 -- First step, we must mark all nested subprograms that require a static
410 -- link (activation record) because either they contain explicit uplevel
411 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
412 -- this point), or they make calls to other subprograms in the same nest
413 -- that require a static link (in which case we set this flag).
415 -- This is a recursive definition, and to implement this, we have to
416 -- build a call graph for the set of nested subprograms, and then go
417 -- over this graph to implement recursively the invariant that if a
418 -- subprogram has a call to a subprogram requiring a static link, then
419 -- the calling subprogram requires a static link.
421 -- First populate the above tables
423 Subps_First := Subps.Last + 1;
424 Calls.Init;
425 Urefs.Init;
427 Build_Tables : declare
428 Current_Subprogram : Entity_Id;
429 -- When we scan a subprogram body, we set Current_Subprogram to the
430 -- corresponding entity. This gets recursively saved and restored.
432 function Visit_Node (N : Node_Id) return Traverse_Result;
433 -- Visit a single node in Subp
435 -----------
436 -- Visit --
437 -----------
439 procedure Visit is new Traverse_Proc (Visit_Node);
440 -- Used to traverse the body of Subp, populating the tables
442 ----------------
443 -- Visit_Node --
444 ----------------
446 function Visit_Node (N : Node_Id) return Traverse_Result is
447 Ent : Entity_Id;
448 Caller : Entity_Id;
449 Callee : Entity_Id;
451 procedure Check_Static_Type
452 (T : Entity_Id; N : Node_Id; DT : in out Boolean);
453 -- Given a type T, checks if it is a static type defined as a type
454 -- with no dynamic bounds in sight. If so, the only action is to
455 -- set Is_Static_Type True for T. If T is not a static type, then
456 -- all types with dynamic bounds associated with T are detected,
457 -- and their bounds are marked as uplevel referenced if not at the
458 -- library level, and DT is set True. If N is specified, it's the
459 -- node that will need to be replaced. If not specified, it means
460 -- we can't do a replacement because the bound is implicit.
462 procedure Note_Uplevel_Ref
463 (E : Entity_Id;
464 N : Node_Id;
465 Caller : Entity_Id;
466 Callee : Entity_Id);
467 -- Called when we detect an explicit or implicit uplevel reference
468 -- from within Caller to entity E declared in Callee. E can be a
469 -- an object or a type.
471 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id);
472 -- Enter a subprogram whose body is visible or which is a
473 -- subprogram instance into the subprogram table.
475 -----------------------
476 -- Check_Static_Type --
477 -----------------------
479 procedure Check_Static_Type
480 (T : Entity_Id; N : Node_Id; DT : in out Boolean)
482 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id);
483 -- N is the bound of a dynamic type. This procedure notes that
484 -- this bound is uplevel referenced, it can handle references
485 -- to entities (typically _FIRST and _LAST entities), and also
486 -- attribute references of the form T'name (name is typically
487 -- FIRST or LAST) where T is the uplevel referenced bound.
488 -- Ref, if Present, is the location of the reference to
489 -- replace.
491 ------------------------
492 -- Note_Uplevel_Bound --
493 ------------------------
495 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id) is
496 begin
497 -- Entity name case. Make sure that the entity is declared
498 -- in a subprogram. This may not be the case for for a type
499 -- in a loop appearing in a precondition.
500 -- Exclude explicitly discriminants (that can appear
501 -- in bounds of discriminated components).
503 if Is_Entity_Name (N) then
504 if Present (Entity (N))
505 and then not Is_Type (Entity (N))
506 and then Present (Enclosing_Subprogram (Entity (N)))
507 and then Ekind (Entity (N)) /= E_Discriminant
508 then
509 Note_Uplevel_Ref
510 (E => Entity (N),
511 N => Empty,
512 Caller => Current_Subprogram,
513 Callee => Enclosing_Subprogram (Entity (N)));
514 end if;
516 -- Attribute or indexed component case
518 elsif Nkind_In (N, N_Attribute_Reference,
519 N_Indexed_Component)
520 then
521 Note_Uplevel_Bound (Prefix (N), Ref);
523 -- The indices of the indexed components, or the
524 -- associated expressions of an attribute reference,
525 -- may also involve uplevel references.
527 declare
528 Expr : Node_Id;
530 begin
531 Expr := First (Expressions (N));
532 while Present (Expr) loop
533 Note_Uplevel_Bound (Expr, Ref);
534 Next (Expr);
535 end loop;
536 end;
538 -- The type of the prefix may be have an uplevel
539 -- reference if this needs bounds.
541 if Nkind (N) = N_Attribute_Reference then
542 declare
543 Attr : constant Attribute_Id :=
544 Get_Attribute_Id (Attribute_Name (N));
545 DT : Boolean := False;
547 begin
548 if (Attr = Attribute_First
549 or else Attr = Attribute_Last
550 or else Attr = Attribute_Length)
551 and then Is_Constrained (Etype (Prefix (N)))
552 then
553 Check_Static_Type
554 (Etype (Prefix (N)), Empty, DT);
555 end if;
556 end;
557 end if;
559 -- Binary operator cases. These can apply to arrays for
560 -- which we may need bounds.
562 elsif Nkind (N) in N_Binary_Op then
563 Note_Uplevel_Bound (Left_Opnd (N), Ref);
564 Note_Uplevel_Bound (Right_Opnd (N), Ref);
566 -- Unary operator case
568 elsif Nkind (N) in N_Unary_Op then
569 Note_Uplevel_Bound (Right_Opnd (N), Ref);
571 -- Explicit dereference and selected component case
573 elsif Nkind_In (N, N_Explicit_Dereference,
574 N_Selected_Component)
575 then
576 Note_Uplevel_Bound (Prefix (N), Ref);
578 -- Conversion case
580 elsif Nkind (N) = N_Type_Conversion then
581 Note_Uplevel_Bound (Expression (N), Ref);
582 end if;
583 end Note_Uplevel_Bound;
585 -- Start of processing for Check_Static_Type
587 begin
588 -- If already marked static, immediate return
590 if Is_Static_Type (T) then
591 return;
592 end if;
594 -- If the type is at library level, always consider it static,
595 -- since such uplevel references are irrelevant.
597 if Is_Library_Level_Entity (T) then
598 Set_Is_Static_Type (T);
599 return;
600 end if;
602 -- Otherwise figure out what the story is with this type
604 -- For a scalar type, check bounds
606 if Is_Scalar_Type (T) then
608 -- If both bounds static, then this is a static type
610 declare
611 LB : constant Node_Id := Type_Low_Bound (T);
612 UB : constant Node_Id := Type_High_Bound (T);
614 begin
615 if not Is_Static_Expression (LB) then
616 Note_Uplevel_Bound (LB, N);
617 DT := True;
618 end if;
620 if not Is_Static_Expression (UB) then
621 Note_Uplevel_Bound (UB, N);
622 DT := True;
623 end if;
624 end;
626 -- For record type, check all components and discriminant
627 -- constraints if present.
629 elsif Is_Record_Type (T) then
630 declare
631 C : Entity_Id;
632 D : Elmt_Id;
634 begin
635 C := First_Component_Or_Discriminant (T);
636 while Present (C) loop
637 Check_Static_Type (Etype (C), N, DT);
638 Next_Component_Or_Discriminant (C);
639 end loop;
641 if Has_Discriminants (T)
642 and then Present (Discriminant_Constraint (T))
643 then
644 D := First_Elmt (Discriminant_Constraint (T));
645 while Present (D) loop
646 if not Is_Static_Expression (Node (D)) then
647 Note_Uplevel_Bound (Node (D), N);
648 DT := True;
649 end if;
651 Next_Elmt (D);
652 end loop;
653 end if;
654 end;
656 -- For array type, check index types and component type
658 elsif Is_Array_Type (T) then
659 declare
660 IX : Node_Id;
661 begin
662 Check_Static_Type (Component_Type (T), N, DT);
664 IX := First_Index (T);
665 while Present (IX) loop
666 Check_Static_Type (Etype (IX), N, DT);
667 Next_Index (IX);
668 end loop;
669 end;
671 -- For private type, examine whether full view is static
673 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
674 Check_Static_Type (Full_View (T), N, DT);
676 if Is_Static_Type (Full_View (T)) then
677 Set_Is_Static_Type (T);
678 end if;
680 -- For now, ignore other types
682 else
683 return;
684 end if;
686 if not DT then
687 Set_Is_Static_Type (T);
688 end if;
689 end Check_Static_Type;
691 ----------------------
692 -- Note_Uplevel_Ref --
693 ----------------------
695 procedure Note_Uplevel_Ref
696 (E : Entity_Id;
697 N : Node_Id;
698 Caller : Entity_Id;
699 Callee : Entity_Id)
701 Full_E : Entity_Id := E;
702 begin
703 -- Nothing to do for static type
705 if Is_Static_Type (E) then
706 return;
707 end if;
709 -- Nothing to do if Caller and Callee are the same
711 if Caller = Callee then
712 return;
714 -- Callee may be a function that returns an array, and that has
715 -- been rewritten as a procedure. If caller is that procedure,
716 -- nothing to do either.
718 elsif Ekind (Callee) = E_Function
719 and then Rewritten_For_C (Callee)
720 and then Corresponding_Procedure (Callee) = Caller
721 then
722 return;
724 elsif Ekind_In (Callee, E_Entry, E_Entry_Family) then
725 return;
726 end if;
728 -- We have a new uplevel referenced entity
730 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
731 Full_E := Full_View (E);
732 end if;
734 -- All we do at this stage is to add the uplevel reference to
735 -- the table. It's too early to do anything else, since this
736 -- uplevel reference may come from an unreachable subprogram
737 -- in which case the entry will be deleted.
739 Urefs.Append ((N, Full_E, Caller, Callee));
740 end Note_Uplevel_Ref;
742 -------------------------
743 -- Register_Subprogram --
744 -------------------------
746 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
747 L : constant Nat := Get_Level (Subp, E);
749 -- Subprograms declared in tasks and protected types cannot
750 -- be eliminated because calls to them may be in other units,
751 -- so they must be treated as reachable.
753 begin
754 -- Subprograms declared in tasks and protected types cannot
755 -- be eliminated because calls to them may be in other units,
756 -- so they must be treated as reachable.
758 Subps.Append
759 ((Ent => E,
760 Bod => Bod,
761 Lev => L,
762 Reachable => In_Synchronized_Unit (E),
763 Uplevel_Ref => L,
764 Declares_AREC => False,
765 Uents => No_Elist,
766 Last => 0,
767 ARECnF => Empty,
768 ARECn => Empty,
769 ARECnT => Empty,
770 ARECnPT => Empty,
771 ARECnP => Empty,
772 ARECnU => Empty));
774 Set_Subps_Index (E, UI_From_Int (Subps.Last));
776 -- If we marked this reachable because it's in a synchronized
777 -- unit, we have to mark all enclosing subprograms as reachable
778 -- as well.
780 if In_Synchronized_Unit (E) then
781 declare
782 S : Entity_Id := E;
784 begin
785 for J in reverse 1 .. L - 1 loop
786 S := Enclosing_Subprogram (S);
787 Subps.Table (Subp_Index (S)).Reachable := True;
788 end loop;
789 end;
790 end if;
791 end Register_Subprogram;
793 -- Start of processing for Visit_Node
795 begin
796 case Nkind (N) is
798 -- Record a subprogram call
800 when N_Function_Call
801 | N_Procedure_Call_Statement
803 -- We are only interested in direct calls, not indirect
804 -- calls (where Name (N) is an explicit dereference) at
805 -- least for now!
807 if Nkind (Name (N)) in N_Has_Entity then
808 Ent := Entity (Name (N));
810 -- We are only interested in calls to subprograms nested
811 -- within Subp. Calls to Subp itself or to subprograms
812 -- outside the nested structure do not affect us.
814 if Scope_Within (Ent, Subp)
815 and then Is_Subprogram (Ent)
816 and then not Is_Imported (Ent)
817 then
818 Append_Unique_Call ((N, Current_Subprogram, Ent));
819 end if;
820 end if;
822 -- For all calls where the formal is an unconstrained array
823 -- and the actual is constrained we need to check the bounds
824 -- for uplevel references.
826 declare
827 Actual : Entity_Id;
828 DT : Boolean := False;
829 Formal : Node_Id;
830 Subp : Entity_Id;
832 begin
833 if Nkind (Name (N)) = N_Explicit_Dereference then
834 Subp := Etype (Name (N));
835 else
836 Subp := Entity (Name (N));
837 end if;
839 Actual := First_Actual (N);
840 Formal := First_Formal_With_Extras (Subp);
841 while Present (Actual) loop
842 if Is_Array_Type (Etype (Formal))
843 and then not Is_Constrained (Etype (Formal))
844 and then Is_Constrained (Etype (Actual))
845 then
846 Check_Static_Type (Etype (Actual), Empty, DT);
847 end if;
849 Next_Actual (Actual);
850 Next_Formal_With_Extras (Formal);
851 end loop;
852 end;
854 -- An At_End_Proc in a statement sequence indicates that there
855 -- is a call from the enclosing construct or block to that
856 -- subprogram. As above, the called entity must be local and
857 -- not imported.
859 when N_Handled_Sequence_Of_Statements =>
860 if Present (At_End_Proc (N))
861 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
862 and then not Is_Imported (Entity (At_End_Proc (N)))
863 then
864 Append_Unique_Call
865 ((N, Current_Subprogram, Entity (At_End_Proc (N))));
866 end if;
868 -- Similarly, the following constructs include a semantic
869 -- attribute Procedure_To_Call that must be handled like
870 -- other calls. Likewise for attribute Storage_Pool.
872 when N_Allocator
873 | N_Extended_Return_Statement
874 | N_Free_Statement
875 | N_Simple_Return_Statement
877 declare
878 Pool : constant Entity_Id := Storage_Pool (N);
879 Proc : constant Entity_Id := Procedure_To_Call (N);
881 begin
882 if Present (Proc)
883 and then Scope_Within (Proc, Subp)
884 and then not Is_Imported (Proc)
885 then
886 Append_Unique_Call ((N, Current_Subprogram, Proc));
887 end if;
889 if Present (Pool)
890 and then not Is_Library_Level_Entity (Pool)
891 and then Scope_Within_Or_Same (Scope (Pool), Subp)
892 then
893 Caller := Current_Subprogram;
894 Callee := Enclosing_Subprogram (Pool);
896 if Callee /= Caller then
897 Note_Uplevel_Ref (Pool, Empty, Caller, Callee);
898 end if;
899 end if;
900 end;
902 -- For an allocator with a qualified expression, check type
903 -- of expression being qualified. The explicit type name is
904 -- handled as an entity reference.
906 if Nkind (N) = N_Allocator
907 and then Nkind (Expression (N)) = N_Qualified_Expression
908 then
909 declare
910 DT : Boolean := False;
911 begin
912 Check_Static_Type
913 (Etype (Expression (Expression (N))), Empty, DT);
914 end;
916 -- For a Return or Free (all other nodes we handle here),
917 -- we usually need the size of the object, so we need to be
918 -- sure that any nonstatic bounds of the expression's type
919 -- that are uplevel are handled.
921 elsif Nkind (N) /= N_Allocator
922 and then Present (Expression (N))
923 then
924 declare
925 DT : Boolean := False;
926 begin
927 Check_Static_Type (Etype (Expression (N)), Empty, DT);
928 end;
929 end if;
931 -- A 'Access reference is a (potential) call. So is 'Address,
932 -- in particular on imported subprograms. Other attributes
933 -- require special handling.
935 when N_Attribute_Reference =>
936 declare
937 Attr : constant Attribute_Id :=
938 Get_Attribute_Id (Attribute_Name (N));
939 begin
940 case Attr is
941 when Attribute_Access
942 | Attribute_Unchecked_Access
943 | Attribute_Unrestricted_Access
944 | Attribute_Address
946 if Nkind (Prefix (N)) in N_Has_Entity then
947 Ent := Entity (Prefix (N));
949 -- We only need to examine calls to subprograms
950 -- nested within current Subp.
952 if Scope_Within (Ent, Subp) then
953 if Is_Imported (Ent) then
954 null;
956 elsif Is_Subprogram (Ent) then
957 Append_Unique_Call
958 ((N, Current_Subprogram, Ent));
959 end if;
960 end if;
961 end if;
963 -- References to bounds can be uplevel references if
964 -- the type isn't static.
966 when Attribute_First
967 | Attribute_Last
968 | Attribute_Length
970 -- Special-case attributes of objects whose bounds
971 -- may be uplevel references. More complex prefixes
972 -- handled during full traversal. Note that if the
973 -- nominal subtype of the prefix is unconstrained,
974 -- the bound must be obtained from the object, not
975 -- from the (possibly) uplevel reference.
977 if Is_Constrained (Etype (Prefix (N))) then
978 declare
979 DT : Boolean := False;
980 begin
981 Check_Static_Type
982 (Etype (Prefix (N)), Empty, DT);
983 end;
985 return OK;
986 end if;
988 when others =>
989 null;
990 end case;
991 end;
993 -- Component associations in aggregates are either static or
994 -- else the aggregate will be expanded into assignments, in
995 -- which case the expression is analyzed later and provides
996 -- no relevant code generation.
998 when N_Component_Association =>
999 if No (Expression (N))
1000 or else No (Etype (Expression (N)))
1001 then
1002 return Skip;
1003 end if;
1005 -- Generic associations are not analyzed: the actuals are
1006 -- transferred to renaming and subtype declarations that
1007 -- are the ones that must be examined.
1009 when N_Generic_Association =>
1010 return Skip;
1012 -- Indexed references can be uplevel if the type isn't static
1013 -- and if the lower bound (or an inner bound for a multi-
1014 -- dimensional array) is uplevel.
1016 when N_Indexed_Component
1017 | N_Slice
1019 if Is_Constrained (Etype (Prefix (N))) then
1020 declare
1021 DT : Boolean := False;
1022 begin
1023 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1024 end;
1025 end if;
1027 -- A selected component can have an implicit up-level
1028 -- reference due to the bounds of previous fields in the
1029 -- record. We simplify the processing here by examining
1030 -- all components of the record.
1032 -- Selected components appear as unit names and end labels
1033 -- for child units. Prefixes of these nodes denote parent
1034 -- units and carry no type information so they are skipped.
1036 when N_Selected_Component =>
1037 if Present (Etype (Prefix (N))) then
1038 declare
1039 DT : Boolean := False;
1040 begin
1041 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1042 end;
1043 end if;
1045 -- For EQ/NE comparisons, we need the type of the operands
1046 -- in order to do the comparison, which means we need the
1047 -- bounds.
1049 when N_Op_Eq
1050 | N_Op_Ne
1052 declare
1053 DT : Boolean := False;
1054 begin
1055 Check_Static_Type (Etype (Left_Opnd (N)), Empty, DT);
1056 Check_Static_Type (Etype (Right_Opnd (N)), Empty, DT);
1057 end;
1059 -- Likewise we need the sizes to compute how much to move in
1060 -- an assignment.
1062 when N_Assignment_Statement =>
1063 declare
1064 DT : Boolean := False;
1065 begin
1066 Check_Static_Type (Etype (Name (N)), Empty, DT);
1067 Check_Static_Type (Etype (Expression (N)), Empty, DT);
1068 end;
1070 -- Record a subprogram. We record a subprogram body that acts
1071 -- as a spec. Otherwise we record a subprogram declaration,
1072 -- providing that it has a corresponding body we can get hold
1073 -- of. The case of no corresponding body being available is
1074 -- ignored for now.
1076 when N_Subprogram_Body =>
1077 Ent := Unique_Defining_Entity (N);
1079 -- Ignore generic subprogram
1081 if Is_Generic_Subprogram (Ent) then
1082 return Skip;
1083 end if;
1085 -- Make new entry in subprogram table if not already made
1087 Register_Subprogram (Ent, N);
1089 -- We make a recursive call to scan the subprogram body, so
1090 -- that we can save and restore Current_Subprogram.
1092 declare
1093 Save_CS : constant Entity_Id := Current_Subprogram;
1094 Decl : Node_Id;
1096 begin
1097 Current_Subprogram := Ent;
1099 -- Scan declarations
1101 Decl := First (Declarations (N));
1102 while Present (Decl) loop
1103 Visit (Decl);
1104 Next (Decl);
1105 end loop;
1107 -- Scan statements
1109 Visit (Handled_Statement_Sequence (N));
1111 -- Restore current subprogram setting
1113 Current_Subprogram := Save_CS;
1114 end;
1116 -- Now at this level, return skipping the subprogram body
1117 -- descendants, since we already took care of them!
1119 return Skip;
1121 -- If we have a body stub, visit the associated subunit, which
1122 -- is a semantic descendant of the stub.
1124 when N_Body_Stub =>
1125 Visit (Library_Unit (N));
1127 -- A declaration of a wrapper package indicates a subprogram
1128 -- instance for which there is no explicit body. Enter the
1129 -- subprogram instance in the table.
1131 when N_Package_Declaration =>
1132 if Is_Wrapper_Package (Defining_Entity (N)) then
1133 Register_Subprogram
1134 (Related_Instance (Defining_Entity (N)), Empty);
1135 end if;
1137 -- Skip generic declarations
1139 when N_Generic_Declaration =>
1140 return Skip;
1142 -- Skip generic package body
1144 when N_Package_Body =>
1145 if Present (Corresponding_Spec (N))
1146 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
1147 then
1148 return Skip;
1149 end if;
1151 -- Pragmas and component declarations can be ignored.
1152 -- Quantified expressions are expanded into explicit loops
1153 -- and the original epression must be ignored.
1155 when N_Component_Declaration
1156 | N_Pragma
1157 | N_Quantified_Expression
1159 return Skip;
1161 -- We want to skip the function spec for a generic function
1162 -- to avoid looking at any generic types that might be in
1163 -- its formals.
1165 when N_Function_Specification =>
1166 if Is_Generic_Subprogram (Unique_Defining_Entity (N)) then
1167 return Skip;
1168 end if;
1170 -- Otherwise record an uplevel reference in a local identifier
1172 when others =>
1173 if Nkind (N) in N_Has_Entity
1174 and then Present (Entity (N))
1175 then
1176 Ent := Entity (N);
1178 -- Only interested in entities declared within our nest
1180 if not Is_Library_Level_Entity (Ent)
1181 and then Scope_Within_Or_Same (Scope (Ent), Subp)
1183 -- Skip entities defined in inlined subprograms
1185 and then
1186 Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
1188 -- Constants and variables are potentially uplevel
1189 -- references to global declarations.
1191 and then
1192 (Ekind_In (Ent, E_Constant,
1193 E_Loop_Parameter,
1194 E_Variable)
1196 -- Formals are interesting, but not if being used
1197 -- as mere names of parameters for name notation
1198 -- calls.
1200 or else
1201 (Is_Formal (Ent)
1202 and then not
1203 (Nkind (Parent (N)) = N_Parameter_Association
1204 and then Selector_Name (Parent (N)) = N))
1206 -- Types other than known Is_Static types are
1207 -- potentially interesting.
1209 or else
1210 (Is_Type (Ent) and then not Is_Static_Type (Ent)))
1211 then
1212 -- Here we have a potentially interesting uplevel
1213 -- reference to examine.
1215 if Is_Type (Ent) then
1216 declare
1217 DT : Boolean := False;
1219 begin
1220 Check_Static_Type (Ent, N, DT);
1221 return OK;
1222 end;
1223 end if;
1225 Caller := Current_Subprogram;
1226 Callee := Enclosing_Subprogram (Ent);
1228 if Callee /= Caller
1229 and then (not Is_Static_Type (Ent)
1230 or else Needs_Fat_Pointer (Ent))
1231 then
1232 Note_Uplevel_Ref (Ent, N, Caller, Callee);
1234 -- Check the type of a formal parameter of the current
1235 -- subprogram, whose formal type may be an uplevel
1236 -- reference.
1238 elsif Is_Formal (Ent)
1239 and then Scope (Ent) = Current_Subprogram
1240 then
1241 declare
1242 DT : Boolean := False;
1244 begin
1245 Check_Static_Type (Etype (Ent), Empty, DT);
1246 end;
1247 end if;
1248 end if;
1249 end if;
1250 end case;
1252 -- Fall through to continue scanning children of this node
1254 return OK;
1255 end Visit_Node;
1257 -- Start of processing for Build_Tables
1259 begin
1260 -- Traverse the body to get subprograms, calls and uplevel references
1262 Visit (Subp_Body);
1263 end Build_Tables;
1265 -- Now do the first transitive closure which determines which
1266 -- subprograms in the nest are actually reachable.
1268 Reachable_Closure : declare
1269 Modified : Boolean;
1271 begin
1272 Subps.Table (Subps_First).Reachable := True;
1274 -- We use a simple minded algorithm as follows (obviously this can
1275 -- be done more efficiently, using one of the standard algorithms
1276 -- for efficient transitive closure computation, but this is simple
1277 -- and most likely fast enough that its speed does not matter).
1279 -- Repeatedly scan the list of calls. Any time we find a call from
1280 -- A to B, where A is reachable, but B is not, then B is reachable,
1281 -- and note that we have made a change by setting Modified True. We
1282 -- repeat this until we make a pass with no modifications.
1284 Outer : loop
1285 Modified := False;
1286 Inner : for J in Calls.First .. Calls.Last loop
1287 declare
1288 CTJ : Call_Entry renames Calls.Table (J);
1290 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1291 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1293 SUBF : Subp_Entry renames Subps.Table (SINF);
1294 SUBT : Subp_Entry renames Subps.Table (SINT);
1296 begin
1297 if SUBF.Reachable and then not SUBT.Reachable then
1298 SUBT.Reachable := True;
1299 Modified := True;
1300 end if;
1301 end;
1302 end loop Inner;
1304 exit Outer when not Modified;
1305 end loop Outer;
1306 end Reachable_Closure;
1308 -- Remove calls from unreachable subprograms
1310 declare
1311 New_Index : Nat;
1313 begin
1314 New_Index := 0;
1315 for J in Calls.First .. Calls.Last loop
1316 declare
1317 CTJ : Call_Entry renames Calls.Table (J);
1319 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1320 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1322 SUBF : Subp_Entry renames Subps.Table (SINF);
1323 SUBT : Subp_Entry renames Subps.Table (SINT);
1325 begin
1326 if SUBF.Reachable then
1327 pragma Assert (SUBT.Reachable);
1328 New_Index := New_Index + 1;
1329 Calls.Table (New_Index) := Calls.Table (J);
1330 end if;
1331 end;
1332 end loop;
1334 Calls.Set_Last (New_Index);
1335 end;
1337 -- Remove uplevel references from unreachable subprograms
1339 declare
1340 New_Index : Nat;
1342 begin
1343 New_Index := 0;
1344 for J in Urefs.First .. Urefs.Last loop
1345 declare
1346 URJ : Uref_Entry renames Urefs.Table (J);
1348 SINF : constant SI_Type := Subp_Index (URJ.Caller);
1349 SINT : constant SI_Type := Subp_Index (URJ.Callee);
1351 SUBF : Subp_Entry renames Subps.Table (SINF);
1352 SUBT : Subp_Entry renames Subps.Table (SINT);
1354 S : Entity_Id;
1356 begin
1357 -- Keep reachable reference
1359 if SUBF.Reachable then
1360 New_Index := New_Index + 1;
1361 Urefs.Table (New_Index) := Urefs.Table (J);
1363 -- And since we know we are keeping this one, this is a good
1364 -- place to fill in information for a good reference.
1366 -- Mark all enclosing subprograms need to declare AREC
1368 S := URJ.Caller;
1369 loop
1370 S := Enclosing_Subprogram (S);
1372 -- If we are at the top level, as can happen with
1373 -- references to formals in aspects of nested subprogram
1374 -- declarations, there are no further subprograms to mark
1375 -- as requiring activation records.
1377 exit when No (S);
1379 declare
1380 SUBI : Subp_Entry renames Subps.Table (Subp_Index (S));
1381 begin
1382 SUBI.Declares_AREC := True;
1384 -- If this entity was marked reachable because it is
1385 -- in a task or protected type, there may not appear
1386 -- to be any calls to it, which would normally
1387 -- adjust the levels of the parent subprograms.
1388 -- So we need to be sure that the uplevel reference
1389 -- of that entity takes into account possible calls.
1391 if In_Synchronized_Unit (SUBF.Ent)
1392 and then SUBT.Lev < SUBI.Uplevel_Ref
1393 then
1394 SUBI.Uplevel_Ref := SUBT.Lev;
1395 end if;
1396 end;
1398 exit when S = URJ.Callee;
1399 end loop;
1401 -- Add to list of uplevel referenced entities for Callee.
1402 -- We do not add types to this list, only actual references
1403 -- to objects that will be referenced uplevel, and we use
1404 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1405 -- duplicate entries in the list.
1406 -- Discriminants are also excluded, only the enclosing
1407 -- object can appear in the list.
1409 if not Is_Uplevel_Referenced_Entity (URJ.Ent)
1410 and then Ekind (URJ.Ent) /= E_Discriminant
1411 then
1412 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
1413 Append_New_Elmt (URJ.Ent, SUBT.Uents);
1414 end if;
1416 -- And set uplevel indication for caller
1418 if SUBT.Lev < SUBF.Uplevel_Ref then
1419 SUBF.Uplevel_Ref := SUBT.Lev;
1420 end if;
1421 end if;
1422 end;
1423 end loop;
1425 Urefs.Set_Last (New_Index);
1426 end;
1428 -- Remove unreachable subprograms from Subps table. Note that we do
1429 -- this after eliminating entries from the other two tables, since
1430 -- those elimination steps depend on referencing the Subps table.
1432 declare
1433 New_SI : SI_Type;
1435 begin
1436 New_SI := Subps_First - 1;
1437 for J in Subps_First .. Subps.Last loop
1438 declare
1439 STJ : Subp_Entry renames Subps.Table (J);
1440 Spec : Node_Id;
1441 Decl : Node_Id;
1443 begin
1444 -- Subprogram is reachable, copy and reset index
1446 if STJ.Reachable then
1447 New_SI := New_SI + 1;
1448 Subps.Table (New_SI) := STJ;
1449 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
1451 -- Subprogram is not reachable
1453 else
1454 -- Clear index, since no longer active
1456 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
1458 -- Output debug information if -gnatd.3 set
1460 if Debug_Flag_Dot_3 then
1461 Write_Str ("Eliminate ");
1462 Write_Name (Chars (Subps.Table (J).Ent));
1463 Write_Str (" at ");
1464 Write_Location (Sloc (Subps.Table (J).Ent));
1465 Write_Str (" (not referenced)");
1466 Write_Eol;
1467 end if;
1469 -- Rewrite declaration, body, and corresponding freeze node
1470 -- to null statements.
1472 -- A subprogram instantiation does not have an explicit
1473 -- body. If unused, we could remove the corresponding
1474 -- wrapper package and its body (TBD).
1476 if Present (STJ.Bod) then
1477 Spec := Corresponding_Spec (STJ.Bod);
1479 if Present (Spec) then
1480 Decl := Parent (Declaration_Node (Spec));
1481 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
1483 if Present (Freeze_Node (Spec)) then
1484 Rewrite (Freeze_Node (Spec),
1485 Make_Null_Statement (Sloc (Decl)));
1486 end if;
1487 end if;
1489 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
1490 end if;
1491 end if;
1492 end;
1493 end loop;
1495 Subps.Set_Last (New_SI);
1496 end;
1498 -- Now it is time for the second transitive closure, which follows calls
1499 -- and makes sure that A calls B, and B has uplevel references, then A
1500 -- is also marked as having uplevel references.
1502 Closure_Uplevel : declare
1503 Modified : Boolean;
1505 begin
1506 -- We use a simple minded algorithm as follows (obviously this can
1507 -- be done more efficiently, using one of the standard algorithms
1508 -- for efficient transitive closure computation, but this is simple
1509 -- and most likely fast enough that its speed does not matter).
1511 -- Repeatedly scan the list of calls. Any time we find a call from
1512 -- A to B, where B has uplevel references, make sure that A is marked
1513 -- as having at least the same level of uplevel referencing.
1515 Outer2 : loop
1516 Modified := False;
1517 Inner2 : for J in Calls.First .. Calls.Last loop
1518 declare
1519 CTJ : Call_Entry renames Calls.Table (J);
1520 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1521 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1522 SUBF : Subp_Entry renames Subps.Table (SINF);
1523 SUBT : Subp_Entry renames Subps.Table (SINT);
1524 begin
1525 if SUBT.Lev > SUBT.Uplevel_Ref
1526 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
1527 then
1528 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
1529 Modified := True;
1530 end if;
1531 end;
1532 end loop Inner2;
1534 exit Outer2 when not Modified;
1535 end loop Outer2;
1536 end Closure_Uplevel;
1538 -- We have one more step before the tables are complete. An uplevel
1539 -- call from subprogram A to subprogram B where subprogram B has uplevel
1540 -- references is in effect an uplevel reference, and must arrange for
1541 -- the proper activation link to be passed.
1543 for J in Calls.First .. Calls.Last loop
1544 declare
1545 CTJ : Call_Entry renames Calls.Table (J);
1547 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1548 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1550 SUBF : Subp_Entry renames Subps.Table (SINF);
1551 SUBT : Subp_Entry renames Subps.Table (SINT);
1553 A : Entity_Id;
1555 begin
1556 -- If callee has uplevel references
1558 if SUBT.Uplevel_Ref < SUBT.Lev
1560 -- And this is an uplevel call
1562 and then SUBT.Lev < SUBF.Lev
1563 then
1564 -- We need to arrange for finding the uplink
1566 A := CTJ.Caller;
1567 loop
1568 A := Enclosing_Subprogram (A);
1569 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1570 exit when A = CTJ.Callee;
1572 -- In any case exit when we get to the outer level. This
1573 -- happens in some odd cases with generics (in particular
1574 -- sem_ch3.adb does not compile without this kludge ???).
1576 exit when A = Subp;
1577 end loop;
1578 end if;
1579 end;
1580 end loop;
1582 -- The tables are now complete, so we can record the last index in the
1583 -- Subps table for later reference in Cprint.
1585 Subps.Table (Subps_First).Last := Subps.Last;
1587 -- Next step, create the entities for code we will insert. We do this
1588 -- at the start so that all the entities are defined, regardless of the
1589 -- order in which we do the code insertions.
1591 Create_Entities : for J in Subps_First .. Subps.Last loop
1592 declare
1593 STJ : Subp_Entry renames Subps.Table (J);
1594 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1596 begin
1597 -- First we create the ARECnF entity for the additional formal for
1598 -- all subprograms which need an activation record passed.
1600 if STJ.Uplevel_Ref < STJ.Lev then
1601 STJ.ARECnF :=
1602 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1603 end if;
1605 -- Define the AREC entities for the activation record if needed
1607 if STJ.Declares_AREC then
1608 STJ.ARECn :=
1609 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1610 STJ.ARECnT :=
1611 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1612 STJ.ARECnPT :=
1613 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1614 STJ.ARECnP :=
1615 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1617 -- Define uplink component entity if inner nesting case
1619 if Present (STJ.ARECnF) then
1620 STJ.ARECnU :=
1621 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1622 end if;
1623 end if;
1624 end;
1625 end loop Create_Entities;
1627 -- Loop through subprograms
1629 Subp_Loop : declare
1630 Addr : Entity_Id := Empty;
1632 begin
1633 for J in Subps_First .. Subps.Last loop
1634 declare
1635 STJ : Subp_Entry renames Subps.Table (J);
1637 begin
1638 -- First add the extra formal if needed. This applies to all
1639 -- nested subprograms that require an activation record to be
1640 -- passed, as indicated by ARECnF being defined.
1642 if Present (STJ.ARECnF) then
1644 -- Here we need the extra formal. We do the expansion and
1645 -- analysis of this manually, since it is fairly simple,
1646 -- and it is not obvious how we can get what we want if we
1647 -- try to use the normal Analyze circuit.
1649 Add_Extra_Formal : declare
1650 Encl : constant SI_Type := Enclosing_Subp (J);
1651 STJE : Subp_Entry renames Subps.Table (Encl);
1652 -- Index and Subp_Entry for enclosing routine
1654 Form : constant Entity_Id := STJ.ARECnF;
1655 -- The formal to be added. Note that n here is one less
1656 -- than the level of the subprogram itself (STJ.Ent).
1658 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1659 -- S is an N_Function/Procedure_Specification node, and F
1660 -- is the new entity to add to this subprogramn spec as
1661 -- the last Extra_Formal.
1663 ----------------------
1664 -- Add_Form_To_Spec --
1665 ----------------------
1667 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1668 Sub : constant Entity_Id := Defining_Entity (S);
1669 Ent : Entity_Id;
1671 begin
1672 -- Case of at least one Extra_Formal is present, set
1673 -- ARECnF as the new last entry in the list.
1675 if Present (Extra_Formals (Sub)) then
1676 Ent := Extra_Formals (Sub);
1677 while Present (Extra_Formal (Ent)) loop
1678 Ent := Extra_Formal (Ent);
1679 end loop;
1681 Set_Extra_Formal (Ent, F);
1683 -- No Extra formals present
1685 else
1686 Set_Extra_Formals (Sub, F);
1687 Ent := Last_Formal (Sub);
1689 if Present (Ent) then
1690 Set_Extra_Formal (Ent, F);
1691 end if;
1692 end if;
1693 end Add_Form_To_Spec;
1695 -- Start of processing for Add_Extra_Formal
1697 begin
1698 -- Decorate the new formal entity
1700 Set_Scope (Form, STJ.Ent);
1701 Set_Ekind (Form, E_In_Parameter);
1702 Set_Etype (Form, STJE.ARECnPT);
1703 Set_Mechanism (Form, By_Copy);
1704 Set_Never_Set_In_Source (Form, True);
1705 Set_Analyzed (Form, True);
1706 Set_Comes_From_Source (Form, False);
1707 Set_Is_Activation_Record (Form, True);
1709 -- Case of only body present
1711 if Acts_As_Spec (STJ.Bod) then
1712 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1714 -- Case of separate spec
1716 else
1717 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1718 end if;
1719 end Add_Extra_Formal;
1720 end if;
1722 -- Processing for subprograms that declare an activation record
1724 if Present (STJ.ARECn) then
1726 -- Local declarations for one such subprogram
1728 declare
1729 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1731 Decls : constant List_Id := New_List;
1732 -- List of new declarations we create
1734 Clist : List_Id;
1735 Comp : Entity_Id;
1737 Decl_Assign : Node_Id;
1738 -- Assigment to set uplink, Empty if none
1740 Decl_ARECnT : Node_Id;
1741 Decl_ARECnPT : Node_Id;
1742 Decl_ARECn : Node_Id;
1743 Decl_ARECnP : Node_Id;
1744 -- Declaration nodes for the AREC entities we build
1746 begin
1747 -- Build list of component declarations for ARECnT
1748 -- and load System.Address.
1750 Clist := Empty_List;
1752 if No (Addr) then
1753 Addr := RTE (RE_Address);
1754 end if;
1756 -- If we are in a subprogram that has a static link that
1757 -- is passed in (as indicated by ARECnF being defined),
1758 -- then include ARECnU : ARECmPT where ARECmPT comes from
1759 -- the level one higher than the current level, and the
1760 -- entity ARECnPT comes from the enclosing subprogram.
1762 if Present (STJ.ARECnF) then
1763 declare
1764 STJE : Subp_Entry
1765 renames Subps.Table (Enclosing_Subp (J));
1766 begin
1767 Append_To (Clist,
1768 Make_Component_Declaration (Loc,
1769 Defining_Identifier => STJ.ARECnU,
1770 Component_Definition =>
1771 Make_Component_Definition (Loc,
1772 Subtype_Indication =>
1773 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1774 end;
1775 end if;
1777 -- Add components for uplevel referenced entities
1779 if Present (STJ.Uents) then
1780 declare
1781 Elmt : Elmt_Id;
1782 Ptr_Decl : Node_Id;
1783 Uent : Entity_Id;
1785 Indx : Nat;
1786 -- 1's origin of index in list of elements. This is
1787 -- used to uniquify names if needed in Upref_Name.
1789 begin
1790 Elmt := First_Elmt (STJ.Uents);
1791 Indx := 0;
1792 while Present (Elmt) loop
1793 Uent := Node (Elmt);
1794 Indx := Indx + 1;
1796 Comp :=
1797 Make_Defining_Identifier (Loc,
1798 Chars => Upref_Name (Uent, Indx, Clist));
1800 Set_Activation_Record_Component
1801 (Uent, Comp);
1803 if Needs_Fat_Pointer (Uent) then
1805 -- Build corresponding access type
1807 Ptr_Decl :=
1808 Build_Access_Type_Decl
1809 (Etype (Uent), STJ.Ent);
1810 Append_To (Decls, Ptr_Decl);
1812 -- And use its type in the corresponding
1813 -- component.
1815 Append_To (Clist,
1816 Make_Component_Declaration (Loc,
1817 Defining_Identifier => Comp,
1818 Component_Definition =>
1819 Make_Component_Definition (Loc,
1820 Subtype_Indication =>
1821 New_Occurrence_Of
1822 (Defining_Identifier (Ptr_Decl),
1823 Loc))));
1824 else
1825 Append_To (Clist,
1826 Make_Component_Declaration (Loc,
1827 Defining_Identifier => Comp,
1828 Component_Definition =>
1829 Make_Component_Definition (Loc,
1830 Subtype_Indication =>
1831 New_Occurrence_Of (Addr, Loc))));
1832 end if;
1833 Next_Elmt (Elmt);
1834 end loop;
1835 end;
1836 end if;
1838 -- Now we can insert the AREC declarations into the body
1839 -- type ARECnT is record .. end record;
1840 -- pragma Suppress_Initialization (ARECnT);
1842 -- Note that we need to set the Suppress_Initialization
1843 -- flag after Decl_ARECnT has been analyzed.
1845 Decl_ARECnT :=
1846 Make_Full_Type_Declaration (Loc,
1847 Defining_Identifier => STJ.ARECnT,
1848 Type_Definition =>
1849 Make_Record_Definition (Loc,
1850 Component_List =>
1851 Make_Component_List (Loc,
1852 Component_Items => Clist)));
1853 Append_To (Decls, Decl_ARECnT);
1855 -- type ARECnPT is access all ARECnT;
1857 Decl_ARECnPT :=
1858 Make_Full_Type_Declaration (Loc,
1859 Defining_Identifier => STJ.ARECnPT,
1860 Type_Definition =>
1861 Make_Access_To_Object_Definition (Loc,
1862 All_Present => True,
1863 Subtype_Indication =>
1864 New_Occurrence_Of (STJ.ARECnT, Loc)));
1865 Append_To (Decls, Decl_ARECnPT);
1867 -- ARECn : aliased ARECnT;
1869 Decl_ARECn :=
1870 Make_Object_Declaration (Loc,
1871 Defining_Identifier => STJ.ARECn,
1872 Aliased_Present => True,
1873 Object_Definition =>
1874 New_Occurrence_Of (STJ.ARECnT, Loc));
1875 Append_To (Decls, Decl_ARECn);
1877 -- ARECnP : constant ARECnPT := ARECn'Access;
1879 Decl_ARECnP :=
1880 Make_Object_Declaration (Loc,
1881 Defining_Identifier => STJ.ARECnP,
1882 Constant_Present => True,
1883 Object_Definition =>
1884 New_Occurrence_Of (STJ.ARECnPT, Loc),
1885 Expression =>
1886 Make_Attribute_Reference (Loc,
1887 Prefix =>
1888 New_Occurrence_Of (STJ.ARECn, Loc),
1889 Attribute_Name => Name_Access));
1890 Append_To (Decls, Decl_ARECnP);
1892 -- If we are in a subprogram that has a static link that
1893 -- is passed in (as indicated by ARECnF being defined),
1894 -- then generate ARECn.ARECmU := ARECmF where m is
1895 -- one less than the current level to set the uplink.
1897 if Present (STJ.ARECnF) then
1898 Decl_Assign :=
1899 Make_Assignment_Statement (Loc,
1900 Name =>
1901 Make_Selected_Component (Loc,
1902 Prefix =>
1903 New_Occurrence_Of (STJ.ARECn, Loc),
1904 Selector_Name =>
1905 New_Occurrence_Of (STJ.ARECnU, Loc)),
1906 Expression =>
1907 New_Occurrence_Of (STJ.ARECnF, Loc));
1908 Append_To (Decls, Decl_Assign);
1910 else
1911 Decl_Assign := Empty;
1912 end if;
1914 if No (Declarations (STJ.Bod)) then
1915 Set_Declarations (STJ.Bod, Decls);
1916 else
1917 Prepend_List_To (Declarations (STJ.Bod), Decls);
1918 end if;
1920 -- Analyze the newly inserted declarations. Note that we
1921 -- do not need to establish the whole scope stack, since
1922 -- we have already set all entity fields (so there will
1923 -- be no searching of upper scopes to resolve names). But
1924 -- we do set the scope of the current subprogram, so that
1925 -- newly created entities go in the right entity chain.
1927 -- We analyze with all checks suppressed (since we do
1928 -- not expect any exceptions).
1930 Push_Scope (STJ.Ent);
1931 Analyze (Decl_ARECnT, Suppress => All_Checks);
1933 -- Note that we need to call Set_Suppress_Initialization
1934 -- after Decl_ARECnT has been analyzed, but before
1935 -- analyzing Decl_ARECnP so that the flag is properly
1936 -- taking into account.
1938 Set_Suppress_Initialization (STJ.ARECnT);
1940 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1941 Analyze (Decl_ARECn, Suppress => All_Checks);
1942 Analyze (Decl_ARECnP, Suppress => All_Checks);
1944 if Present (Decl_Assign) then
1945 Analyze (Decl_Assign, Suppress => All_Checks);
1946 end if;
1948 Pop_Scope;
1950 -- Next step, for each uplevel referenced entity, add
1951 -- assignment operations to set the component in the
1952 -- activation record.
1954 if Present (STJ.Uents) then
1955 declare
1956 Elmt : Elmt_Id;
1958 begin
1959 Elmt := First_Elmt (STJ.Uents);
1960 while Present (Elmt) loop
1961 declare
1962 Ent : constant Entity_Id := Node (Elmt);
1963 Loc : constant Source_Ptr := Sloc (Ent);
1964 Dec : constant Node_Id :=
1965 Declaration_Node (Ent);
1967 Asn : Node_Id;
1968 Attr : Name_Id;
1969 Ins : Node_Id;
1971 begin
1972 -- For parameters, we insert the assignment
1973 -- right after the declaration of ARECnP.
1974 -- For all other entities, we insert the
1975 -- assignment immediately after the
1976 -- declaration of the entity or after the
1977 -- freeze node if present.
1979 -- Note: we don't need to mark the entity
1980 -- as being aliased, because the address
1981 -- attribute will mark it as Address_Taken,
1982 -- and that is good enough.
1984 if Is_Formal (Ent) then
1985 Ins := Decl_ARECnP;
1987 elsif Has_Delayed_Freeze (Ent) then
1988 Ins := Freeze_Node (Ent);
1990 else
1991 Ins := Dec;
1992 end if;
1994 -- Build and insert the assignment:
1995 -- ARECn.nam := nam'Address
1996 -- or else 'Access for unconstrained array
1998 if Needs_Fat_Pointer (Ent) then
1999 Attr := Name_Access;
2000 else
2001 Attr := Name_Address;
2002 end if;
2004 Asn :=
2005 Make_Assignment_Statement (Loc,
2006 Name =>
2007 Make_Selected_Component (Loc,
2008 Prefix =>
2009 New_Occurrence_Of (STJ.ARECn, Loc),
2010 Selector_Name =>
2011 New_Occurrence_Of
2012 (Activation_Record_Component
2013 (Ent),
2014 Loc)),
2016 Expression =>
2017 Make_Attribute_Reference (Loc,
2018 Prefix =>
2019 New_Occurrence_Of (Ent, Loc),
2020 Attribute_Name => Attr));
2022 -- If we have a loop parameter, we have
2023 -- to insert before the first statement
2024 -- of the loop. Ins points to the
2025 -- N_Loop_Parameter_Specification or to
2026 -- an N_Iterator_Specification.
2028 if Nkind_In
2029 (Ins, N_Iterator_Specification,
2030 N_Loop_Parameter_Specification)
2031 then
2032 -- Quantified expression are rewritten as
2033 -- loops during expansion.
2035 if Nkind (Parent (Ins)) =
2036 N_Quantified_Expression
2037 then
2038 null;
2040 else
2041 Ins :=
2042 First
2043 (Statements
2044 (Parent (Parent (Ins))));
2045 Insert_Before (Ins, Asn);
2046 end if;
2048 else
2049 Insert_After (Ins, Asn);
2050 end if;
2052 -- Analyze the assignment statement. We do
2053 -- not need to establish the relevant scope
2054 -- stack entries here, because we have
2055 -- already set the correct entity references,
2056 -- so no name resolution is required, and no
2057 -- new entities are created, so we don't even
2058 -- need to set the current scope.
2060 -- We analyze with all checks suppressed
2061 -- (since we do not expect any exceptions).
2063 Analyze (Asn, Suppress => All_Checks);
2064 end;
2066 Next_Elmt (Elmt);
2067 end loop;
2068 end;
2069 end if;
2070 end;
2071 end if;
2072 end;
2073 end loop;
2074 end Subp_Loop;
2076 -- Next step, process uplevel references. This has to be done in a
2077 -- separate pass, after completing the processing in Sub_Loop because we
2078 -- need all the AREC declarations generated, inserted, and analyzed so
2079 -- that the uplevel references can be successfully analyzed.
2081 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
2082 declare
2083 UPJ : Uref_Entry renames Urefs.Table (J);
2085 begin
2086 -- Ignore type references, these are implicit references that do
2087 -- not need rewriting (e.g. the appearence in a conversion).
2088 -- Also ignore if no reference was specified or if the rewriting
2089 -- has already been done (this can happen if the N_Identifier
2090 -- occurs more than one time in the tree).
2092 if No (UPJ.Ref)
2093 or else not Is_Entity_Name (UPJ.Ref)
2094 or else not Present (Entity (UPJ.Ref))
2095 then
2096 goto Continue;
2097 end if;
2099 -- Rewrite one reference
2101 Rewrite_One_Ref : declare
2102 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
2103 -- Source location for the reference
2105 Typ : constant Entity_Id := Etype (UPJ.Ent);
2106 -- The type of the referenced entity
2108 Atyp : Entity_Id;
2109 -- The actual subtype of the reference
2111 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
2112 -- Subp_Index for caller containing reference
2114 STJR : Subp_Entry renames Subps.Table (RS_Caller);
2115 -- Subp_Entry for subprogram containing reference
2117 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
2118 -- Subp_Index for subprogram containing referenced entity
2120 STJE : Subp_Entry renames Subps.Table (RS_Callee);
2121 -- Subp_Entry for subprogram containing referenced entity
2123 Pfx : Node_Id;
2124 Comp : Entity_Id;
2125 SI : SI_Type;
2127 begin
2128 Atyp := Etype (UPJ.Ref);
2130 if Ekind (Atyp) /= E_Record_Subtype then
2131 Atyp := Get_Actual_Subtype (UPJ.Ref);
2132 end if;
2134 -- Ignore if no ARECnF entity for enclosing subprogram which
2135 -- probably happens as a result of not properly treating
2136 -- instance bodies. To be examined ???
2138 -- If this test is omitted, then the compilation of freeze.adb
2139 -- and inline.adb fail in unnesting mode.
2141 if No (STJR.ARECnF) then
2142 goto Continue;
2143 end if;
2145 -- Push the current scope, so that the pointer type Tnn, and
2146 -- any subsidiary entities resulting from the analysis of the
2147 -- rewritten reference, go in the right entity chain.
2149 Push_Scope (STJR.Ent);
2151 -- Now we need to rewrite the reference. We have a reference
2152 -- from level STJR.Lev to level STJE.Lev. The general form of
2153 -- the rewritten reference for entity X is:
2155 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2157 -- where a,b,c,d .. m =
2158 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2160 pragma Assert (STJR.Lev > STJE.Lev);
2162 -- Compute the prefix of X. Here are examples to make things
2163 -- clear (with parens to show groupings, the prefix is
2164 -- everything except the .X at the end).
2166 -- level 2 to level 1
2168 -- AREC1F.X
2170 -- level 3 to level 1
2172 -- (AREC2F.AREC1U).X
2174 -- level 4 to level 1
2176 -- ((AREC3F.AREC2U).AREC1U).X
2178 -- level 6 to level 2
2180 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2182 -- In the above, ARECnF and ARECnU are pointers, so there are
2183 -- explicit dereferences required for these occurrences.
2185 Pfx :=
2186 Make_Explicit_Dereference (Loc,
2187 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
2188 SI := RS_Caller;
2189 for L in STJE.Lev .. STJR.Lev - 2 loop
2190 SI := Enclosing_Subp (SI);
2191 Pfx :=
2192 Make_Explicit_Dereference (Loc,
2193 Prefix =>
2194 Make_Selected_Component (Loc,
2195 Prefix => Pfx,
2196 Selector_Name =>
2197 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
2198 end loop;
2200 -- Get activation record component (must exist)
2202 Comp := Activation_Record_Component (UPJ.Ent);
2203 pragma Assert (Present (Comp));
2205 -- Do the replacement. If the component type is an access type,
2206 -- this is an uplevel reference for an entity that requires a
2207 -- fat pointer, so dereference the component.
2209 if Is_Access_Type (Etype (Comp)) then
2210 Rewrite (UPJ.Ref,
2211 Make_Explicit_Dereference (Loc,
2212 Prefix =>
2213 Make_Selected_Component (Loc,
2214 Prefix => Pfx,
2215 Selector_Name =>
2216 New_Occurrence_Of (Comp, Loc))));
2218 else
2219 Rewrite (UPJ.Ref,
2220 Make_Attribute_Reference (Loc,
2221 Prefix => New_Occurrence_Of (Atyp, Loc),
2222 Attribute_Name => Name_Deref,
2223 Expressions => New_List (
2224 Make_Selected_Component (Loc,
2225 Prefix => Pfx,
2226 Selector_Name =>
2227 New_Occurrence_Of (Comp, Loc)))));
2228 end if;
2230 -- Analyze and resolve the new expression. We do not need to
2231 -- establish the relevant scope stack entries here, because we
2232 -- have already set all the correct entity references, so no
2233 -- name resolution is needed. We have already set the current
2234 -- scope, so that any new entities created will be in the right
2235 -- scope.
2237 -- We analyze with all checks suppressed (since we do not
2238 -- expect any exceptions)
2240 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
2241 Pop_Scope;
2242 end Rewrite_One_Ref;
2243 end;
2245 <<Continue>>
2246 null;
2247 end loop Uplev_Refs;
2249 -- Finally, loop through all calls adding extra actual for the
2250 -- activation record where it is required.
2252 Adjust_Calls : for J in Calls.First .. Calls.Last loop
2254 -- Process a single call, we are only interested in a call to a
2255 -- subprogram that actually needs a pointer to an activation record,
2256 -- as indicated by the ARECnF entity being set. This excludes the
2257 -- top level subprogram, and any subprogram not having uplevel refs.
2259 Adjust_One_Call : declare
2260 CTJ : Call_Entry renames Calls.Table (J);
2261 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
2262 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
2264 Loc : constant Source_Ptr := Sloc (CTJ.N);
2266 Extra : Node_Id;
2267 ExtraP : Node_Id;
2268 SubX : SI_Type;
2269 Act : Node_Id;
2271 begin
2272 if Present (STT.ARECnF)
2273 and then Nkind (CTJ.N) in N_Subprogram_Call
2274 then
2275 -- CTJ.N is a call to a subprogram which may require a pointer
2276 -- to an activation record. The subprogram containing the call
2277 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2278 -- have a call from level STF.Lev to level STT.Lev.
2280 -- There are three possibilities:
2282 -- For a call to the same level, we just pass the activation
2283 -- record passed to the calling subprogram.
2285 if STF.Lev = STT.Lev then
2286 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2288 -- For a call that goes down a level, we pass a pointer to the
2289 -- activation record constructed within the caller (which may
2290 -- be the outer-level subprogram, but also may be a more deeply
2291 -- nested caller).
2293 elsif STT.Lev = STF.Lev + 1 then
2294 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
2296 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2297 -- since it is not possible to do a downcall of more than
2298 -- one level.
2300 -- For a call from level STF.Lev to level STT.Lev, we
2301 -- have to find the activation record needed by the
2302 -- callee. This is as follows:
2304 -- ARECaF.ARECbU.ARECcU....ARECmU
2306 -- where a,b,c .. m =
2307 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2309 else
2310 pragma Assert (STT.Lev < STF.Lev);
2312 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2313 SubX := Subp_Index (CTJ.Caller);
2314 for K in reverse STT.Lev .. STF.Lev - 1 loop
2315 SubX := Enclosing_Subp (SubX);
2316 Extra :=
2317 Make_Selected_Component (Loc,
2318 Prefix => Extra,
2319 Selector_Name =>
2320 New_Occurrence_Of
2321 (Subps.Table (SubX).ARECnU, Loc));
2322 end loop;
2323 end if;
2325 -- Extra is the additional parameter to be added. Build a
2326 -- parameter association that we can append to the actuals.
2328 ExtraP :=
2329 Make_Parameter_Association (Loc,
2330 Selector_Name =>
2331 New_Occurrence_Of (STT.ARECnF, Loc),
2332 Explicit_Actual_Parameter => Extra);
2334 if No (Parameter_Associations (CTJ.N)) then
2335 Set_Parameter_Associations (CTJ.N, Empty_List);
2336 end if;
2338 Append (ExtraP, Parameter_Associations (CTJ.N));
2340 -- We need to deal with the actual parameter chain as well. The
2341 -- newly added parameter is always the last actual.
2343 Act := First_Named_Actual (CTJ.N);
2345 if No (Act) then
2346 Set_First_Named_Actual (CTJ.N, Extra);
2348 -- If call has been relocated (as with an expression in
2349 -- an aggregate), set First_Named pointer in original node
2350 -- as well, because that's the parent of the parameter list.
2352 Set_First_Named_Actual
2353 (Parent (List_Containing (ExtraP)), Extra);
2355 -- Here we must follow the chain and append the new entry
2357 else
2358 loop
2359 declare
2360 PAN : Node_Id;
2361 NNA : Node_Id;
2363 begin
2364 PAN := Parent (Act);
2365 pragma Assert (Nkind (PAN) = N_Parameter_Association);
2366 NNA := Next_Named_Actual (PAN);
2368 if No (NNA) then
2369 Set_Next_Named_Actual (PAN, Extra);
2370 exit;
2371 end if;
2373 Act := NNA;
2374 end;
2375 end loop;
2376 end if;
2378 -- Analyze and resolve the new actual. We do not need to
2379 -- establish the relevant scope stack entries here, because
2380 -- we have already set all the correct entity references, so
2381 -- no name resolution is needed.
2383 -- We analyze with all checks suppressed (since we do not
2384 -- expect any exceptions, and also we temporarily turn off
2385 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2386 -- references (not needed at this stage, and in fact causes
2387 -- a bit of recursive chaos).
2389 Opt.Unnest_Subprogram_Mode := False;
2390 Analyze_And_Resolve
2391 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
2392 Opt.Unnest_Subprogram_Mode := True;
2393 end if;
2394 end Adjust_One_Call;
2395 end loop Adjust_Calls;
2397 return;
2398 end Unnest_Subprogram;
2400 ------------------------
2401 -- Unnest_Subprograms --
2402 ------------------------
2404 procedure Unnest_Subprograms (N : Node_Id) is
2405 function Search_Subprograms (N : Node_Id) return Traverse_Result;
2406 -- Tree visitor that search for outer level procedures with nested
2407 -- subprograms and invokes Unnest_Subprogram()
2409 ---------------
2410 -- Do_Search --
2411 ---------------
2413 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
2414 -- Subtree visitor instantiation
2416 ------------------------
2417 -- Search_Subprograms --
2418 ------------------------
2420 function Search_Subprograms (N : Node_Id) return Traverse_Result is
2421 begin
2422 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
2423 declare
2424 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
2426 begin
2427 -- We are only interested in subprograms (not generic
2428 -- subprograms), that have nested subprograms.
2430 if Is_Subprogram (Spec_Id)
2431 and then Has_Nested_Subprogram (Spec_Id)
2432 and then Is_Library_Level_Entity (Spec_Id)
2433 then
2434 Unnest_Subprogram (Spec_Id, N);
2435 end if;
2436 end;
2438 -- The proper body of a stub may contain nested subprograms, and
2439 -- therefore must be visited explicitly. Nested stubs are examined
2440 -- recursively in Visit_Node.
2442 elsif Nkind (N) in N_Body_Stub then
2443 Do_Search (Library_Unit (N));
2445 -- Skip generic packages
2447 elsif Nkind (N) = N_Package_Body
2448 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
2449 then
2450 return Skip;
2451 end if;
2453 return OK;
2454 end Search_Subprograms;
2456 -- Start of processing for Unnest_Subprograms
2458 begin
2459 if not Opt.Unnest_Subprogram_Mode or not Opt.Expander_Active then
2460 return;
2461 end if;
2463 -- A specification will contain bodies if it contains instantiations so
2464 -- examine package or subprogram declaration of the main unit, when it
2465 -- is present.
2467 if Nkind (Unit (N)) = N_Package_Body
2468 or else (Nkind (Unit (N)) = N_Subprogram_Body
2469 and then not Acts_As_Spec (N))
2470 then
2471 Do_Search (Library_Unit (N));
2472 end if;
2474 Do_Search (N);
2475 end Unnest_Subprograms;
2477 end Exp_Unst;