2015-05-22 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / exp_unst.adb
blob872a35fda67e0c199717653af5bd38874b6ab6d2
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-2015, 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; use Opt;
35 with Output; use Output;
36 with Rtsfind; use Rtsfind;
37 with Sem; use Sem;
38 with Sem_Ch8; use Sem_Ch8;
39 with Sem_Mech; use Sem_Mech;
40 with Sem_Res; use Sem_Res;
41 with Sem_Util; use Sem_Util;
42 with Sinfo; use Sinfo;
43 with Sinput; use Sinput;
44 with Snames; use Snames;
45 with Tbuild; use Tbuild;
46 with Uintp; use Uintp;
48 package body Exp_Unst is
50 -----------
51 -- Calls --
52 -----------
54 -- Table to record calls within the nest being analyzed. These are the
55 -- calls which may need to have an AREC actual added. This table is built
56 -- new for each subprogram nest and cleared at the end of processing each
57 -- subprogram nest.
59 type Call_Entry is record
60 N : Node_Id;
61 -- The actual call
63 Caller : Entity_Id;
64 -- Entity of the subprogram containing the call (can be at any level)
66 Callee : Entity_Id;
67 -- Entity of the subprogram called (always at level 2 or higher). Note
68 -- that in accordance with the basic rules of nesting, the level of To
69 -- is either less than or equal to the level of From, or one greater.
70 end record;
72 package Calls is new Table.Table (
73 Table_Component_Type => Call_Entry,
74 Table_Index_Type => Nat,
75 Table_Low_Bound => 1,
76 Table_Initial => 100,
77 Table_Increment => 200,
78 Table_Name => "Unnest_Calls");
79 -- Records each call within the outer subprogram and all nested subprograms
80 -- that are to other subprograms nested within the outer subprogram. These
81 -- are the calls that may need an additional parameter.
83 -----------
84 -- Urefs --
85 -----------
87 -- Table to record explicit uplevel references to objects (variables,
88 -- constants, formal parameters). These are the references that will
89 -- need rewriting to use the activation table (AREC) pointers. Also
90 -- included are implicit and explicit uplevel references to types, but
91 -- these do not get rewritten by the front end. This table is built new
92 -- for each subprogram nest and cleared at the end of processing each
93 -- subprogram nest.
95 type Uref_Entry is record
96 Ref : Node_Id;
97 -- The reference itself. For objects this is always an entity reference
98 -- and the referenced entity will have its Is_Uplevel_Referenced_Entity
99 -- flag set and will appear in the Uplevel_Referenced_Entities list of
100 -- the subprogram declaring this entity.
102 Ent : Entity_Id;
103 -- The Entity_Id of the uplevel referenced object or type
105 Caller : Entity_Id;
106 -- The entity for the subprogram immediately containing this entity
108 Callee : Entity_Id;
109 -- The entity for the subprogram containing the referenced entity. Note
110 -- that the level of Callee must be less than the level of Caller, since
111 -- this is an uplevel reference.
112 end record;
114 package Urefs is new Table.Table (
115 Table_Component_Type => Uref_Entry,
116 Table_Index_Type => Nat,
117 Table_Low_Bound => 1,
118 Table_Initial => 100,
119 Table_Increment => 200,
120 Table_Name => "Unnest_Urefs");
122 -----------------------
123 -- Unnest_Subprogram --
124 -----------------------
126 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id) is
127 function AREC_String (Lev : Pos) return String;
128 -- Given a level value, 1, 2, ... returns the string AREC, AREC2, ...
130 function Enclosing_Subp (Subp : SI_Type) return SI_Type;
131 -- Subp is the index of a subprogram which has a Lev greater than 1.
132 -- This function returns the index of the enclosing subprogram which
133 -- will have a Lev value one less than this.
135 function Get_Level (Sub : Entity_Id) return Nat;
136 -- Sub is either Subp itself, or a subprogram nested within Subp. This
137 -- function returns the level of nesting (Subp = 1, subprograms that
138 -- are immediately nested within Subp = 2, etc).
140 function Subp_Index (Sub : Entity_Id) return SI_Type;
141 -- Given the entity for a subprogram, return corresponding Subps index
143 function Suffixed_Name (Ent : Entity_Id) return Name_Id;
144 -- Given an entity Ent, return its name (Char (Ent)) suffixed with
145 -- two underscores and the entity number, to ensure a unique name.
147 function Upref_Name (Ent : Entity_Id; Clist : List_Id) return Name_Id;
148 -- This function returns the name to be used in the activation record to
149 -- reference the variable uplevel. Clist is the list of components that
150 -- have been created in the activation record so far. Normally this is
151 -- just a copy of the Chars field of the entity. The exception is when
152 -- the name has already been used, in which case we suffix the name with
153 -- the entity number to avoid duplication. This happens with declare
154 -- blocks and generic parameters at least.
156 -----------------
157 -- AREC_String --
158 -----------------
160 function AREC_String (Lev : Pos) return String is
161 begin
162 if Lev > 9 then
163 return AREC_String (Lev / 10) & Character'Val (Lev mod 10 + 48);
164 else
165 return "AREC" & Character'Val (Lev + 48);
166 end if;
167 end AREC_String;
169 --------------------
170 -- Enclosing_Subp --
171 --------------------
173 function Enclosing_Subp (Subp : SI_Type) return SI_Type is
174 STJ : Subp_Entry renames Subps.Table (Subp);
175 Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
176 begin
177 pragma Assert (STJ.Lev > 1);
178 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
179 return Ret;
180 end Enclosing_Subp;
182 ---------------
183 -- Get_Level --
184 ---------------
186 function Get_Level (Sub : Entity_Id) return Nat is
187 Lev : Nat;
188 S : Entity_Id;
189 begin
190 Lev := 1;
191 S := Sub;
192 loop
193 if S = Subp then
194 return Lev;
195 else
196 S := Enclosing_Subprogram (S);
197 Lev := Lev + 1;
198 end if;
199 end loop;
200 end Get_Level;
202 ----------------
203 -- Subp_Index --
204 ----------------
206 function Subp_Index (Sub : Entity_Id) return SI_Type is
207 begin
208 pragma Assert (Is_Subprogram (Sub));
209 return SI_Type (UI_To_Int (Subps_Index (Sub)));
210 end Subp_Index;
212 -------------------
213 -- Suffixed_Name --
214 -------------------
216 function Suffixed_Name (Ent : Entity_Id) return Name_Id is
217 begin
218 Get_Name_String (Chars (Ent));
219 Add_Str_To_Name_Buffer ("__");
220 Add_Nat_To_Name_Buffer (Nat (Ent));
221 return Name_Enter;
222 end Suffixed_Name;
224 ----------------
225 -- Upref_Name --
226 ----------------
228 function Upref_Name (Ent : Entity_Id; Clist : List_Id) return Name_Id is
229 C : Node_Id;
230 begin
231 C := First (Clist);
232 loop
233 if No (C) then
234 return Chars (Ent);
235 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
236 return Suffixed_Name (Ent);
237 else
238 Next (C);
239 end if;
240 end loop;
241 end Upref_Name;
243 -- Start of processing for Unnest_Subprogram
245 begin
246 -- Nothing to do inside a generic (all processing is for instance)
248 if Inside_A_Generic then
249 return;
250 end if;
252 -- At least for now, do not unnest anything but main source unit
254 if not In_Extended_Main_Source_Unit (Subp_Body) then
255 return;
256 end if;
258 -- This routine is called late, after the scope stack is gone. The
259 -- following creates a suitable dummy scope stack to be used for the
260 -- analyze/expand calls made from this routine.
262 Push_Scope (Subp);
264 -- First step, we must mark all nested subprograms that require a static
265 -- link (activation record) because either they contain explicit uplevel
266 -- references (as indicated by ??? being set at this
267 -- point), or they make calls to other subprograms in the same nest that
268 -- require a static link (in which case we set this flag).
270 -- This is a recursive definition, and to implement this, we have to
271 -- build a call graph for the set of nested subprograms, and then go
272 -- over this graph to implement recursively the invariant that if a
273 -- subprogram has a call to a subprogram requiring a static link, then
274 -- the calling subprogram requires a static link.
276 -- First populate the above tables
278 Subps_First := Subps.Last + 1;
279 Calls.Init;
280 Urefs.Init;
282 Build_Tables : declare
283 Current_Subprogram : Entity_Id;
284 -- When we scan a subprogram body, we set Current_Subprogram to the
285 -- corresponding entity. This gets recursively saved and restored.
287 function Visit_Node (N : Node_Id) return Traverse_Result;
288 -- Visit a single node in Subp
290 -----------
291 -- Visit --
292 -----------
294 procedure Visit is new Traverse_Proc (Visit_Node);
295 -- Used to traverse the body of Subp, populating the tables
297 ----------------
298 -- Visit_Node --
299 ----------------
301 function Visit_Node (N : Node_Id) return Traverse_Result is
302 Ent : Entity_Id;
303 Caller : Entity_Id;
304 Callee : Entity_Id;
306 procedure Check_Static_Type (T : Entity_Id; DT : in out Boolean);
307 -- Given a type T, checks if it is a static type defined as a
308 -- type with no dynamic bounds in sight. If so, the only action
309 -- is to set Is_Static_Type True for T. If T is not a static
310 -- type, then all types with dynamic bounds associated with
311 -- T are detected, and their bounds are marked as uplevel
312 -- referenced if not at the library level, and DT is set True.
314 procedure Note_Uplevel_Ref
315 (E : Entity_Id;
316 Caller : Entity_Id;
317 Callee : Entity_Id);
318 -- Called when we detect an explicit or implicit uplevel reference
319 -- from within Caller to entity E declared in Callee. E can be a
320 -- an object or a type.
322 -----------------------
323 -- Check_Static_Type --
324 -----------------------
326 procedure Check_Static_Type (T : Entity_Id; DT : in out Boolean) is
327 procedure Note_Uplevel_Bound (N : Node_Id);
328 -- N is the bound of a dynamic type. This procedure notes that
329 -- this bound is uplevel referenced, it can handle references
330 -- to entities (typically _FIRST and _LAST entities), and also
331 -- attribute references of the form T'name (name is typically
332 -- FIRST or LAST) where T is the uplevel referenced bound.
334 ------------------------
335 -- Note_Uplevel_Bound --
336 ------------------------
338 procedure Note_Uplevel_Bound (N : Node_Id) is
339 begin
340 -- Entity name case
342 if Is_Entity_Name (N) then
343 if Present (Entity (N)) then
344 Note_Uplevel_Ref
345 (E => Entity (N),
346 Caller => Current_Subprogram,
347 Callee => Enclosing_Subprogram (Entity (N)));
348 end if;
350 -- Attribute case
352 elsif Nkind (N) = N_Attribute_Reference then
353 Note_Uplevel_Bound (Prefix (N));
354 end if;
355 end Note_Uplevel_Bound;
357 -- Start of processing for Check_Static_Type
359 begin
360 -- If already marked static, immediate return
362 if Is_Static_Type (T) then
363 return;
364 end if;
366 -- If the type is at library level, always consider it static,
367 -- since such uplevel references are irrelevant.
369 if Is_Library_Level_Entity (T) then
370 Set_Is_Static_Type (T);
371 return;
372 end if;
374 -- Otherwise figure out what the story is with this type
376 -- For a scalar type, check bounds
378 if Is_Scalar_Type (T) then
380 -- If both bounds static, then this is a static type
382 declare
383 LB : constant Node_Id := Type_Low_Bound (T);
384 UB : constant Node_Id := Type_High_Bound (T);
386 begin
387 if not Is_Static_Expression (LB) then
388 Note_Uplevel_Bound (LB);
389 DT := True;
390 end if;
392 if not Is_Static_Expression (UB) then
393 Note_Uplevel_Bound (UB);
394 DT := True;
395 end if;
396 end;
398 -- For record type, check all components
400 elsif Is_Record_Type (T) then
401 declare
402 C : Entity_Id;
403 begin
404 C := First_Component_Or_Discriminant (T);
405 while Present (C) loop
406 Check_Static_Type (Etype (C), DT);
407 Next_Component_Or_Discriminant (C);
408 end loop;
409 end;
411 -- For array type, check index types and component type
413 elsif Is_Array_Type (T) then
414 declare
415 IX : Node_Id;
416 begin
417 Check_Static_Type (Component_Type (T), DT);
419 IX := First_Index (T);
420 while Present (IX) loop
421 Check_Static_Type (Etype (IX), DT);
422 Next_Index (IX);
423 end loop;
424 end;
426 -- For now, ignore other types
428 else
429 return;
430 end if;
432 if not DT then
433 Set_Is_Static_Type (T);
434 end if;
435 end Check_Static_Type;
437 ----------------------
438 -- Note_Uplevel_Ref --
439 ----------------------
441 procedure Note_Uplevel_Ref
442 (E : Entity_Id;
443 Caller : Entity_Id;
444 Callee : Entity_Id)
446 begin
447 -- Nothing to do for static type
449 if Is_Static_Type (E) then
450 return;
451 end if;
453 -- Nothing to do if Caller and Callee are the same
455 if Caller = Callee then
456 return;
457 end if;
459 -- We have a new uplevel referenced entity
461 -- All we do at this stage is to add the uplevel reference to
462 -- the table. It's too earch to do anything else, since this
463 -- uplevel reference may come from an unreachable subprogram
464 -- in which case the entry will be deleted.
466 Urefs.Append ((N, E, Caller, Callee));
467 end Note_Uplevel_Ref;
469 -- Start of processing for Visit_Node
471 begin
472 -- Record a call
474 if Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
476 -- We are only interested in direct calls, not indirect calls
477 -- (where Name (N) is an explicit dereference) at least for now!
479 and then Nkind (Name (N)) in N_Has_Entity
480 then
481 Ent := Entity (Name (N));
483 -- We are only interested in calls to subprograms nested
484 -- within Subp. Calls to Subp itself or to subprograms that
485 -- are outside the nested structure do not affect us.
487 if Scope_Within (Ent, Subp) then
489 -- Ignore calls to imported routines
491 if Is_Imported (Ent) then
492 null;
494 -- Here we have a call to keep and analyze
496 else
497 -- Both caller and callee must be subprograms
499 if Is_Subprogram (Ent) then
500 Calls.Append ((N, Current_Subprogram, Ent));
501 end if;
502 end if;
503 end if;
505 -- Record a subprogram. We record a subprogram body that acts as
506 -- a spec. Otherwise we record a subprogram declaration, providing
507 -- that it has a corresponding body we can get hold of. The case
508 -- of no corresponding body being available is ignored for now.
510 elsif Nkind (N) = N_Subprogram_Body then
511 Ent := Corresponding_Spec_Of (N);
513 -- Ignore generic subprogram
515 if Is_Generic_Subprogram (Ent) then
516 return Skip;
517 end if;
519 -- Make new entry in subprogram table if not already made
521 declare
522 L : constant Nat := Get_Level (Ent);
523 begin
524 Subps.Append
525 ((Ent => Ent,
526 Bod => N,
527 Lev => L,
528 Reachable => False,
529 Uplevel_Ref => L,
530 Declares_AREC => False,
531 Uents => No_Elist,
532 Last => 0,
533 ARECnF => Empty,
534 ARECn => Empty,
535 ARECnT => Empty,
536 ARECnPT => Empty,
537 ARECnP => Empty,
538 ARECnU => Empty));
539 Set_Subps_Index (Ent, UI_From_Int (Subps.Last));
540 end;
542 -- We make a recursive call to scan the subprogram body, so
543 -- that we can save and restore Current_Subprogram.
545 declare
546 Save_CS : constant Entity_Id := Current_Subprogram;
547 Decl : Node_Id;
549 begin
550 Current_Subprogram := Ent;
552 -- Scan declarations
554 Decl := First (Declarations (N));
555 while Present (Decl) loop
556 Visit (Decl);
557 Next (Decl);
558 end loop;
560 -- Scan statements
562 Visit (Handled_Statement_Sequence (N));
564 -- Restore current subprogram setting
566 Current_Subprogram := Save_CS;
567 end;
569 -- Now at this level, return skipping the subprogram body
570 -- descendents, since we already took care of them!
572 return Skip;
574 -- Record an uplevel reference
576 elsif Nkind (N) in N_Has_Entity and then Present (Entity (N)) then
577 Ent := Entity (N);
579 -- Only interested in entities declared within our nest
581 if not Is_Library_Level_Entity (Ent)
582 and then Scope_Within_Or_Same (Scope (Ent), Subp)
583 and then
585 -- Constants and variables are interesting
587 (Ekind_In (Ent, E_Constant, E_Variable)
589 -- Formals are interesting, but not if being used as mere
590 -- names of parameters for name notation calls.
592 or else
593 (Is_Formal (Ent)
594 and then not
595 (Nkind (Parent (N)) = N_Parameter_Association
596 and then Selector_Name (Parent (N)) = N))
598 -- Types other than known Is_Static types are interesting
600 or else (Is_Type (Ent)
601 and then not Is_Static_Type (Ent)))
602 then
603 -- Here we have a possible interesting uplevel reference
605 if Is_Type (Ent) then
606 declare
607 DT : Boolean := False;
609 begin
610 Check_Static_Type (Ent, DT);
612 if Is_Static_Type (Ent) then
613 return OK;
614 end if;
615 end;
616 end if;
618 Caller := Current_Subprogram;
619 Callee := Enclosing_Subprogram (Ent);
621 if Callee /= Caller and then not Is_Static_Type (Ent) then
622 Note_Uplevel_Ref (Ent, Caller, Callee);
623 end if;
624 end if;
626 -- If we have a body stub, visit the associated subunit
628 elsif Nkind (N) in N_Body_Stub then
629 Visit (Library_Unit (N));
631 -- Skip generic declarations
633 elsif Nkind (N) in N_Generic_Declaration then
634 return Skip;
636 -- Skip generic package body
638 elsif Nkind (N) = N_Package_Body
639 and then Present (Corresponding_Spec (N))
640 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
641 then
642 return Skip;
643 end if;
645 -- Fall through to continue scanning children of this node
647 return OK;
648 end Visit_Node;
650 -- Start of processing for Build_Tables
652 begin
653 -- Traverse the body to get subprograms, calls and uplevel references
655 Visit (Subp_Body);
656 end Build_Tables;
658 -- Now do the first transitive closure which determines which
659 -- subprograms in the nest are actually reachable.
661 Reachable_Closure : declare
662 Modified : Boolean;
664 begin
665 Subps.Table (1).Reachable := True;
667 -- We use a simple minded algorithm as follows (obviously this can
668 -- be done more efficiently, using one of the standard algorithms
669 -- for efficient transitive closure computation, but this is simple
670 -- and most likely fast enough that its speed does not matter).
672 -- Repeatedly scan the list of calls. Any time we find a call from
673 -- A to B, where A is reachable, but B is not, then B is reachable,
674 -- and note that we have made a change by setting Modified True. We
675 -- repeat this until we make a pass with no modifications.
677 Outer : loop
678 Modified := False;
679 Inner : for J in Calls.First .. Calls.Last loop
680 declare
681 CTJ : Call_Entry renames Calls.Table (J);
683 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
684 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
686 SUBF : Subp_Entry renames Subps.Table (SINF);
687 SUBT : Subp_Entry renames Subps.Table (SINT);
689 begin
690 if SUBF.Reachable and then not SUBT.Reachable then
691 SUBT.Reachable := True;
692 Modified := True;
693 end if;
694 end;
695 end loop Inner;
697 exit Outer when not Modified;
698 end loop Outer;
699 end Reachable_Closure;
701 -- Remove calls from unreachable subprograms
703 declare
704 New_Index : Nat;
706 begin
707 New_Index := 0;
708 for J in Calls.First .. Calls.Last loop
709 declare
710 CTJ : Call_Entry renames Calls.Table (J);
712 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
713 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
715 SUBF : Subp_Entry renames Subps.Table (SINF);
716 SUBT : Subp_Entry renames Subps.Table (SINT);
718 begin
719 if SUBF.Reachable then
720 pragma Assert (SUBT.Reachable);
721 New_Index := New_Index + 1;
722 Calls.Table (New_Index) := Calls.Table (J);
723 end if;
724 end;
725 end loop;
727 Calls.Set_Last (New_Index);
728 end;
730 -- Remove uplevel references from unreachable subprograms
732 declare
733 New_Index : Nat;
735 begin
736 New_Index := 0;
737 for J in Urefs.First .. Urefs.Last loop
738 declare
739 URJ : Uref_Entry renames Urefs.Table (J);
741 SINF : constant SI_Type := Subp_Index (URJ.Caller);
742 SINT : constant SI_Type := Subp_Index (URJ.Callee);
744 SUBF : Subp_Entry renames Subps.Table (SINF);
745 SUBT : Subp_Entry renames Subps.Table (SINT);
747 S : Entity_Id;
749 begin
750 -- Keep reachable reference
752 if SUBF.Reachable then
753 New_Index := New_Index + 1;
754 Urefs.Table (New_Index) := Urefs.Table (J);
756 -- And since we know we are keeping this one, this is a good
757 -- place to fill in information for a good reference.
759 -- Mark all enclosing subprograms need to declare AREC
761 S := URJ.Caller;
762 loop
763 S := Enclosing_Subprogram (S);
764 Subps.Table (Subp_Index (S)).Declares_AREC := True;
765 exit when S = URJ.Callee;
766 end loop;
768 -- Add to list of uplevel referenced entities for Callee.
769 -- We do not add types to this list, only actual references
770 -- to objects that will be referenced uplevel, and we use
771 -- the flag Is_Uplevel_Referenced_Entity to avoid making
772 -- duplicate entries in the list.
774 if not Is_Uplevel_Referenced_Entity (URJ.Ent) then
775 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
777 if not Is_Type (URJ.Ent) then
778 Append_New_Elmt (URJ.Ent, SUBT.Uents);
779 end if;
780 end if;
782 -- And set uplevel indication for caller
784 if SUBT.Lev < SUBF.Uplevel_Ref then
785 SUBF.Uplevel_Ref := SUBT.Lev;
786 end if;
787 end if;
788 end;
789 end loop;
791 Urefs.Set_Last (New_Index);
792 end;
794 -- Remove unreachable subprograms from Subps table. Note that we do
795 -- this after eliminating entries from the other two tables, since
796 -- thos elimination steps depend on referencing the Subps table.
798 declare
799 New_SI : SI_Type;
801 begin
802 New_SI := 0;
803 for J in Subps_First .. Subps.Last loop
804 declare
805 STJ : Subp_Entry renames Subps.Table (J);
806 Spec : Node_Id;
807 Decl : Node_Id;
809 begin
810 -- Subprogram is reachable, copy and reset index
812 if STJ.Reachable then
813 New_SI := New_SI + 1;
814 Subps.Table (New_SI) := STJ;
815 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
817 -- Subprogram is not reachable
819 else
820 -- Clear index, since no longer active
822 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
824 -- Output debug information if -gnatd.3 set
826 if Debug_Flag_Dot_3 then
827 Write_Str ("Eliminate ");
828 Write_Name (Chars (Subps.Table (J).Ent));
829 Write_Str (" at ");
830 Write_Location (Sloc (Subps.Table (J).Ent));
831 Write_Str (" (not referenced)");
832 Write_Eol;
833 end if;
835 -- Rewrite declaration and body to null statements
837 Spec := Corresponding_Spec (STJ.Bod);
839 if Present (Spec) then
840 Decl := Parent (Declaration_Node (Spec));
841 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
842 end if;
844 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
845 end if;
846 end;
847 end loop;
849 Subps.Set_Last (New_SI);
850 end;
852 -- Now it is time for the second transitive closure, which follows calls
853 -- and makes sure that A calls B, and B has uplevel references, then A
854 -- is also marked as having uplevel references.
856 Closure_Uplevel : declare
857 Modified : Boolean;
859 begin
860 -- We use a simple minded algorithm as follows (obviously this can
861 -- be done more efficiently, using one of the standard algorithms
862 -- for efficient transitive closure computation, but this is simple
863 -- and most likely fast enough that its speed does not matter).
865 -- Repeatedly scan the list of calls. Any time we find a call from
866 -- A to B, where B has uplevel references, make sure that A is marked
867 -- as having at least the same level of uplevel referencing.
869 Outer2 : loop
870 Modified := False;
871 Inner2 : for J in Calls.First .. Calls.Last loop
872 declare
873 CTJ : Call_Entry renames Calls.Table (J);
874 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
875 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
876 SUBF : Subp_Entry renames Subps.Table (SINF);
877 SUBT : Subp_Entry renames Subps.Table (SINT);
878 begin
879 if SUBT.Lev > SUBT.Uplevel_Ref
880 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
881 then
882 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
883 Modified := True;
884 end if;
885 end;
886 end loop Inner2;
888 exit Outer2 when not Modified;
889 end loop Outer2;
890 end Closure_Uplevel;
892 -- We have one more step before the tables are complete. An uplevel
893 -- call from subprogram A to subprogram B where subprogram B has uplevel
894 -- references is in effect an uplevel reference, and must arrange for
895 -- the proper activation link to be passed.
897 for J in Calls.First .. Calls.Last loop
898 declare
899 CTJ : Call_Entry renames Calls.Table (J);
901 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
902 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
904 SUBF : Subp_Entry renames Subps.Table (SINF);
905 SUBT : Subp_Entry renames Subps.Table (SINT);
907 A : Entity_Id;
909 begin
910 -- If callee has uplevel references
912 if SUBT.Uplevel_Ref < SUBT.Lev
914 -- And this is an uplevel call
916 and then SUBT.Lev < SUBF.Lev
917 then
918 -- We need to arrange for finding the uplink
920 A := CTJ.Caller;
921 loop
922 A := Enclosing_Subprogram (A);
923 Subps.Table (Subp_Index (A)).Declares_AREC := True;
924 exit when A = CTJ.Callee;
926 -- In any case exit when we get to the outer level. This
927 -- happens in some odd cases with generics (in particular
928 -- sem_ch3.adb does not compile without this kludge ???).
930 exit when A = Subp;
931 end loop;
932 end if;
933 end;
934 end loop;
936 -- The tables are now complete, so we can record the last index in the
937 -- Subps table for later reference in Cprint.
939 Subps.Table (Subps_First).Last := Subps.Last;
941 -- Next step, create the entities for code we will insert. We do this
942 -- at the start so that all the entities are defined, regardless of the
943 -- order in which we do the code insertions.
945 Create_Entities : for J in Subps_First .. Subps.Last loop
946 declare
947 STJ : Subp_Entry renames Subps.Table (J);
948 Loc : constant Source_Ptr := Sloc (STJ.Bod);
949 ARS : constant String := AREC_String (STJ.Lev);
951 begin
952 -- First we create the ARECnF entity for the additional formal for
953 -- all subprograms which need an activation record passed.
955 if STJ.Uplevel_Ref < STJ.Lev then
956 STJ.ARECnF :=
957 Make_Defining_Identifier (Loc,
958 Chars => Name_Find_Str (AREC_String (STJ.Lev - 1) & "F"));
959 end if;
961 -- Define the AREC entities for the activation record if needed
963 if STJ.Declares_AREC then
964 STJ.ARECn :=
965 Make_Defining_Identifier (Loc, Name_Find_Str (ARS));
966 STJ.ARECnT :=
967 Make_Defining_Identifier (Loc, Name_Find_Str (ARS & "T"));
968 STJ.ARECnPT :=
969 Make_Defining_Identifier (Loc, Name_Find_Str (ARS & "PT"));
970 STJ.ARECnP :=
971 Make_Defining_Identifier (Loc, Name_Find_Str (ARS & "P"));
973 -- Define uplink component entity if inner nesting case
975 if Present (STJ.ARECnF) then
976 declare
977 ARS1 : constant String := AREC_String (STJ.Lev - 1);
978 begin
979 STJ.ARECnU :=
980 Make_Defining_Identifier (Loc,
981 Chars => Name_Find_Str (ARS1 & "U"));
982 end;
983 end if;
984 end if;
985 end;
986 end loop Create_Entities;
988 -- Loop through subprograms
990 Subp_Loop : declare
991 Addr : constant Entity_Id := RTE (RE_Address);
993 begin
994 for J in Subps_First .. Subps.Last loop
995 declare
996 STJ : Subp_Entry renames Subps.Table (J);
998 begin
999 -- First add the extra formal if needed. This applies to all
1000 -- nested subprograms that require an activation record to be
1001 -- passed, as indicated by ARECnF being defined.
1003 if Present (STJ.ARECnF) then
1005 -- Here we need the extra formal. We do the expansion and
1006 -- analysis of this manually, since it is fairly simple,
1007 -- and it is not obvious how we can get what we want if we
1008 -- try to use the normal Analyze circuit.
1010 Add_Extra_Formal : declare
1011 Encl : constant SI_Type := Enclosing_Subp (J);
1012 STJE : Subp_Entry renames Subps.Table (Encl);
1013 -- Index and Subp_Entry for enclosing routine
1015 Form : constant Entity_Id := STJ.ARECnF;
1016 -- The formal to be added. Note that n here is one less
1017 -- than the level of the subprogram itself (STJ.Ent).
1019 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1020 -- S is an N_Function/Procedure_Specification node, and F
1021 -- is the new entity to add to this subprogramn spec as
1022 -- the last Extra_Formal.
1024 ----------------------
1025 -- Add_Form_To_Spec --
1026 ----------------------
1028 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1029 Sub : constant Entity_Id := Defining_Entity (S);
1030 Ent : Entity_Id;
1032 begin
1033 -- Case of at least one Extra_Formal is present, set
1034 -- ARECnF as the new last entry in the list.
1036 if Present (Extra_Formals (Sub)) then
1037 Ent := Extra_Formals (Sub);
1038 while Present (Extra_Formal (Ent)) loop
1039 Ent := Extra_Formal (Ent);
1040 end loop;
1042 Set_Extra_Formal (Ent, F);
1044 -- No Extra formals present
1046 else
1047 Set_Extra_Formals (Sub, F);
1048 Ent := Last_Formal (Sub);
1050 if Present (Ent) then
1051 Set_Extra_Formal (Ent, F);
1052 end if;
1053 end if;
1054 end Add_Form_To_Spec;
1056 -- Start of processing for Add_Extra_Formal
1058 begin
1059 -- Decorate the new formal entity
1061 Set_Scope (Form, STJ.Ent);
1062 Set_Ekind (Form, E_In_Parameter);
1063 Set_Etype (Form, STJE.ARECnPT);
1064 Set_Mechanism (Form, By_Copy);
1065 Set_Never_Set_In_Source (Form, True);
1066 Set_Analyzed (Form, True);
1067 Set_Comes_From_Source (Form, False);
1069 -- Case of only body present
1071 if Acts_As_Spec (STJ.Bod) then
1072 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1074 -- Case of separate spec
1076 else
1077 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1078 end if;
1079 end Add_Extra_Formal;
1080 end if;
1082 -- Processing for subprograms that declare an activation record
1084 if Present (STJ.ARECn) then
1086 -- Local declarations for one such subprogram
1088 declare
1089 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1090 Clist : List_Id;
1091 Comp : Entity_Id;
1093 Decl_ARECnT : Node_Id;
1094 Decl_ARECnPT : Node_Id;
1095 Decl_ARECn : Node_Id;
1096 Decl_ARECnP : Node_Id;
1097 -- Declaration nodes for the AREC entities we build
1099 Decl_Assign : Node_Id;
1100 -- Assigment to set uplink, Empty if none
1102 Decls : List_Id;
1103 -- List of new declarations we create
1105 begin
1106 -- Suffix the ARECnT and ARECnPT names to make sure that
1107 -- they are unique when Cprint moves the declarations to
1108 -- the outer level.
1110 Set_Chars (STJ.ARECnT, Suffixed_Name (STJ.ARECnT));
1111 Set_Chars (STJ.ARECnPT, Suffixed_Name (STJ.ARECnPT));
1113 -- Build list of component declarations for ARECnT
1115 Clist := Empty_List;
1117 -- If we are in a subprogram that has a static link that
1118 -- is passed in (as indicated by ARECnF being defined),
1119 -- then include ARECnU : ARECmPT where m is one less than
1120 -- the current level and the entity ARECnPT comes from
1121 -- the enclosing subprogram.
1123 if Present (STJ.ARECnF) then
1124 declare
1125 STJE : Subp_Entry
1126 renames Subps.Table (Enclosing_Subp (J));
1127 begin
1128 Append_To (Clist,
1129 Make_Component_Declaration (Loc,
1130 Defining_Identifier => STJ.ARECnU,
1131 Component_Definition =>
1132 Make_Component_Definition (Loc,
1133 Subtype_Indication =>
1134 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1135 end;
1136 end if;
1138 -- Add components for uplevel referenced entities
1140 if Present (STJ.Uents) then
1141 declare
1142 Elmt : Elmt_Id;
1143 Uent : Entity_Id;
1145 begin
1146 Elmt := First_Elmt (STJ.Uents);
1147 while Present (Elmt) loop
1148 Uent := Node (Elmt);
1150 Comp :=
1151 Make_Defining_Identifier (Loc,
1152 Chars => Upref_Name (Uent, Clist));
1154 Set_Activation_Record_Component
1155 (Uent, Comp);
1157 Append_To (Clist,
1158 Make_Component_Declaration (Loc,
1159 Defining_Identifier => Comp,
1160 Component_Definition =>
1161 Make_Component_Definition (Loc,
1162 Subtype_Indication =>
1163 New_Occurrence_Of (Addr, Loc))));
1165 Next_Elmt (Elmt);
1166 end loop;
1167 end;
1168 end if;
1170 -- Now we can insert the AREC declarations into the body
1172 -- type ARECnT is record .. end record;
1174 Decl_ARECnT :=
1175 Make_Full_Type_Declaration (Loc,
1176 Defining_Identifier => STJ.ARECnT,
1177 Type_Definition =>
1178 Make_Record_Definition (Loc,
1179 Component_List =>
1180 Make_Component_List (Loc,
1181 Component_Items => Clist)));
1182 Decls := New_List (Decl_ARECnT);
1184 -- type ARECnPT is access all ARECnT;
1186 Decl_ARECnPT :=
1187 Make_Full_Type_Declaration (Loc,
1188 Defining_Identifier => STJ.ARECnPT,
1189 Type_Definition =>
1190 Make_Access_To_Object_Definition (Loc,
1191 All_Present => True,
1192 Subtype_Indication =>
1193 New_Occurrence_Of (STJ.ARECnT, Loc)));
1194 Append_To (Decls, Decl_ARECnPT);
1196 -- ARECn : aliased ARECnT;
1198 Decl_ARECn :=
1199 Make_Object_Declaration (Loc,
1200 Defining_Identifier => STJ.ARECn,
1201 Aliased_Present => True,
1202 Object_Definition =>
1203 New_Occurrence_Of (STJ.ARECnT, Loc));
1204 Append_To (Decls, Decl_ARECn);
1206 -- ARECnP : constant ARECnPT := ARECn'Access;
1208 Decl_ARECnP :=
1209 Make_Object_Declaration (Loc,
1210 Defining_Identifier => STJ.ARECnP,
1211 Constant_Present => True,
1212 Object_Definition =>
1213 New_Occurrence_Of (STJ.ARECnPT, Loc),
1214 Expression =>
1215 Make_Attribute_Reference (Loc,
1216 Prefix =>
1217 New_Occurrence_Of (STJ.ARECn, Loc),
1218 Attribute_Name => Name_Access));
1219 Append_To (Decls, Decl_ARECnP);
1221 -- If we are in a subprogram that has a static link that
1222 -- is passed in (as indicated by ARECnF being defined),
1223 -- then generate ARECn.ARECmU := ARECmF where m is
1224 -- one less than the current level to set the uplink.
1226 if Present (STJ.ARECnF) then
1227 Decl_Assign :=
1228 Make_Assignment_Statement (Loc,
1229 Name =>
1230 Make_Selected_Component (Loc,
1231 Prefix =>
1232 New_Occurrence_Of (STJ.ARECn, Loc),
1233 Selector_Name =>
1234 New_Occurrence_Of (STJ.ARECnU, Loc)),
1235 Expression =>
1236 New_Occurrence_Of (STJ.ARECnF, Loc));
1237 Append_To (Decls, Decl_Assign);
1239 else
1240 Decl_Assign := Empty;
1241 end if;
1243 Prepend_List_To (Declarations (STJ.Bod), Decls);
1245 -- Analyze the newly inserted declarations. Note that we
1246 -- do not need to establish the whole scope stack, since
1247 -- we have already set all entity fields (so there will
1248 -- be no searching of upper scopes to resolve names). But
1249 -- we do set the scope of the current subprogram, so that
1250 -- newly created entities go in the right entity chain.
1252 -- We analyze with all checks suppressed (since we do
1253 -- not expect any exceptions).
1255 Push_Scope (STJ.Ent);
1256 Analyze (Decl_ARECnT, Suppress => All_Checks);
1257 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1258 Analyze (Decl_ARECn, Suppress => All_Checks);
1259 Analyze (Decl_ARECnP, Suppress => All_Checks);
1261 if Present (Decl_Assign) then
1262 Analyze (Decl_Assign, Suppress => All_Checks);
1263 end if;
1265 Pop_Scope;
1267 -- Mark the types as needing typedefs
1269 Set_Needs_Typedef (STJ.ARECnT);
1270 Set_Needs_Typedef (STJ.ARECnPT);
1272 -- Next step, for each uplevel referenced entity, add
1273 -- assignment operations to set the component in the
1274 -- activation record.
1276 if Present (STJ.Uents) then
1277 declare
1278 Elmt : Elmt_Id;
1280 begin
1281 Elmt := First_Elmt (STJ.Uents);
1282 while Present (Elmt) loop
1283 declare
1284 Ent : constant Entity_Id := Node (Elmt);
1285 Loc : constant Source_Ptr := Sloc (Ent);
1286 Dec : constant Node_Id :=
1287 Declaration_Node (Ent);
1288 Ins : Node_Id;
1289 Asn : Node_Id;
1291 begin
1292 -- For parameters, we insert the assignment
1293 -- right after the declaration of ARECnP.
1294 -- For all other entities, we insert
1295 -- the assignment immediately after
1296 -- the declaration of the entity.
1298 -- Note: we don't need to mark the entity
1299 -- as being aliased, because the address
1300 -- attribute will mark it as Address_Taken,
1301 -- and that is good enough.
1303 if Is_Formal (Ent) then
1304 Ins := Decl_ARECnP;
1305 else
1306 Ins := Dec;
1307 end if;
1309 -- Build and insert the assignment:
1310 -- ARECn.nam := nam'Address
1312 Asn :=
1313 Make_Assignment_Statement (Loc,
1314 Name =>
1315 Make_Selected_Component (Loc,
1316 Prefix =>
1317 New_Occurrence_Of (STJ.ARECn, Loc),
1318 Selector_Name =>
1319 New_Occurrence_Of
1320 (Activation_Record_Component
1321 (Ent),
1322 Loc)),
1324 Expression =>
1325 Make_Attribute_Reference (Loc,
1326 Prefix =>
1327 New_Occurrence_Of (Ent, Loc),
1328 Attribute_Name => Name_Address));
1330 Insert_After (Ins, Asn);
1332 -- Analyze the assignment statement. We do
1333 -- not need to establish the relevant scope
1334 -- stack entries here, because we have
1335 -- already set the correct entity references,
1336 -- so no name resolution is required, and no
1337 -- new entities are created, so we don't even
1338 -- need to set the current scope.
1340 -- We analyze with all checks suppressed
1341 -- (since we do not expect any exceptions).
1343 Analyze (Asn, Suppress => All_Checks);
1344 end;
1346 Next_Elmt (Elmt);
1347 end loop;
1348 end;
1349 end if;
1350 end;
1351 end if;
1352 end;
1353 end loop;
1354 end Subp_Loop;
1356 -- Next step, process uplevel references. This has to be done in a
1357 -- separate pass, after completing the processing in Sub_Loop because we
1358 -- need all the AREC declarations generated, inserted, and analyzed so
1359 -- that the uplevel references can be successfully analyzed.
1361 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
1362 declare
1363 UPJ : Uref_Entry renames Urefs.Table (J);
1365 begin
1366 -- Ignore type references, these are implicit references that do
1367 -- not need rewriting (e.g. the appearence in a conversion).
1369 if Is_Type (UPJ.Ent) then
1370 goto Continue;
1371 end if;
1373 -- Also ignore uplevel references to bounds of types that come
1374 -- from the original type reference.
1376 if Is_Entity_Name (UPJ.Ref)
1377 and then Present (Entity (UPJ.Ref))
1378 and then Is_Type (Entity (UPJ.Ref))
1379 then
1380 goto Continue;
1381 end if;
1383 -- Rewrite one reference
1385 Rewrite_One_Ref : declare
1386 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
1387 -- Source location for the reference
1389 Typ : constant Entity_Id := Etype (UPJ.Ent);
1390 -- The type of the referenced entity
1392 Atyp : constant Entity_Id := Get_Actual_Subtype (UPJ.Ref);
1393 -- The actual subtype of the reference
1395 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
1396 -- Subp_Index for caller containing reference
1398 STJR : Subp_Entry renames Subps.Table (RS_Caller);
1399 -- Subp_Entry for subprogram containing reference
1401 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
1402 -- Subp_Index for subprogram containing referenced entity
1404 STJE : Subp_Entry renames Subps.Table (RS_Callee);
1405 -- Subp_Entry for subprogram containing referenced entity
1407 Pfx : Node_Id;
1408 Comp : Entity_Id;
1409 SI : SI_Type;
1411 begin
1412 -- Ignore if no ARECnF entity for enclosing subprogram which
1413 -- probably happens as a result of not properly treating
1414 -- instance bodies. To be examined ???
1416 -- If this test is omitted, then the compilation of
1417 -- freeze.adb and inline.adb fail in unnesting mode.
1419 if No (STJR.ARECnF) then
1420 goto Continue;
1421 end if;
1423 -- Push the current scope, so that the pointer type Tnn, and
1424 -- any subsidiary entities resulting from the analysis of the
1425 -- rewritten reference, go in the right entity chain.
1427 Push_Scope (STJR.Ent);
1429 -- Now we need to rewrite the reference. We have a
1430 -- reference is from level STJR.Lev to level STJE.Lev.
1431 -- The general form of the rewritten reference for
1432 -- entity X is:
1434 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECm.X)
1436 -- where a,b,c,d .. m =
1437 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
1439 pragma Assert (STJR.Lev > STJE.Lev);
1441 -- Compute the prefix of X. Here are examples to make things
1442 -- clear (with parens to show groupings, the prefix is
1443 -- everything except the .X at the end).
1445 -- level 2 to level 1
1447 -- AREC1F.X
1449 -- level 3 to level 1
1451 -- (AREC2F.AREC1U).X
1453 -- level 4 to level 1
1455 -- ((AREC3F.AREC2U).AREC1U).X
1457 -- level 6 to level 2
1459 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
1461 -- In the above, ARECnF and ARECnU are pointers, so there are
1462 -- explicit dereferences required for these occurrences.
1464 Pfx :=
1465 Make_Explicit_Dereference (Loc,
1466 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
1467 SI := RS_Caller;
1468 for L in STJE.Lev .. STJR.Lev - 2 loop
1469 SI := Enclosing_Subp (SI);
1470 Pfx :=
1471 Make_Explicit_Dereference (Loc,
1472 Prefix =>
1473 Make_Selected_Component (Loc,
1474 Prefix => Pfx,
1475 Selector_Name =>
1476 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
1477 end loop;
1479 -- Get activation record component (must exist)
1481 Comp := Activation_Record_Component (UPJ.Ent);
1482 pragma Assert (Present (Comp));
1484 -- Do the replacement
1486 Rewrite (UPJ.Ref,
1487 Make_Attribute_Reference (Loc,
1488 Prefix => New_Occurrence_Of (Atyp, Loc),
1489 Attribute_Name => Name_Deref,
1490 Expressions => New_List (
1491 Make_Selected_Component (Loc,
1492 Prefix => Pfx,
1493 Selector_Name =>
1494 New_Occurrence_Of (Comp, Loc)))));
1496 -- Analyze and resolve the new expression. We do not need to
1497 -- establish the relevant scope stack entries here, because we
1498 -- have already set all the correct entity references, so no
1499 -- name resolution is needed. We have already set the current
1500 -- scope, so that any new entities created will be in the right
1501 -- scope.
1503 -- We analyze with all checks suppressed (since we do not
1504 -- expect any exceptions)
1506 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
1507 Pop_Scope;
1508 end Rewrite_One_Ref;
1509 end;
1511 <<Continue>>
1512 null;
1513 end loop Uplev_Refs;
1515 -- Finally, loop through all calls adding extra actual for the
1516 -- activation record where it is required.
1518 Adjust_Calls : for J in Calls.First .. Calls.Last loop
1520 -- Process a single call, we are only interested in a call to a
1521 -- subprogram that actually needs a pointer to an activation record,
1522 -- as indicated by the ARECnF entity being set. This excludes the
1523 -- top level subprogram, and any subprogram not having uplevel refs.
1525 Adjust_One_Call : declare
1526 CTJ : Call_Entry renames Calls.Table (J);
1527 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
1528 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
1530 Loc : constant Source_Ptr := Sloc (CTJ.N);
1532 Extra : Node_Id;
1533 ExtraP : Node_Id;
1534 SubX : SI_Type;
1535 Act : Node_Id;
1537 begin
1538 if Present (STT.ARECnF) then
1540 -- CTJ.N is a call to a subprogram which may require
1541 -- a pointer to an activation record. The subprogram
1542 -- containing the call is CTJ.From and the subprogram being
1543 -- called is CTJ.To, so we have a call from level STF.Lev to
1544 -- level STT.Lev.
1546 -- There are three possibilities:
1548 -- For a call to the same level, we just pass the activation
1549 -- record passed to the calling subprogram.
1551 if STF.Lev = STT.Lev then
1552 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1554 -- For a call that goes down a level, we pass a pointer
1555 -- to the activation record constructed within the caller
1556 -- (which may be the outer level subprogram, but also may
1557 -- be a more deeply nested caller).
1559 elsif STT.Lev = STF.Lev + 1 then
1560 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
1562 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
1563 -- since it is not possible to do a downcall of more than
1564 -- one level.
1566 -- For a call from level STF.Lev to level STT.Lev, we
1567 -- have to find the activation record needed by the
1568 -- callee. This is as follows:
1570 -- ARECaF.ARECbU.ARECcU....ARECm
1572 -- where a,b,c .. m =
1573 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
1575 else
1576 pragma Assert (STT.Lev < STF.Lev);
1578 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1579 SubX := Subp_Index (CTJ.Caller);
1580 for K in reverse STT.Lev .. STF.Lev - 1 loop
1581 SubX := Enclosing_Subp (SubX);
1582 Extra :=
1583 Make_Selected_Component (Loc,
1584 Prefix => Extra,
1585 Selector_Name =>
1586 New_Occurrence_Of
1587 (Subps.Table (SubX).ARECnU, Loc));
1588 end loop;
1589 end if;
1591 -- Extra is the additional parameter to be added. Build a
1592 -- parameter association that we can append to the actuals.
1594 ExtraP :=
1595 Make_Parameter_Association (Loc,
1596 Selector_Name =>
1597 New_Occurrence_Of (STT.ARECnF, Loc),
1598 Explicit_Actual_Parameter => Extra);
1600 if No (Parameter_Associations (CTJ.N)) then
1601 Set_Parameter_Associations (CTJ.N, Empty_List);
1602 end if;
1604 Append (ExtraP, Parameter_Associations (CTJ.N));
1606 -- We need to deal with the actual parameter chain as well.
1607 -- The newly added parameter is always the last actual.
1609 Act := First_Named_Actual (CTJ.N);
1611 if No (Act) then
1612 Set_First_Named_Actual (CTJ.N, Extra);
1614 -- Here we must follow the chain and append the new entry
1616 else
1617 loop
1618 declare
1619 PAN : Node_Id;
1620 NNA : Node_Id;
1622 begin
1623 PAN := Parent (Act);
1624 pragma Assert (Nkind (PAN) = N_Parameter_Association);
1625 NNA := Next_Named_Actual (PAN);
1627 if No (NNA) then
1628 Set_Next_Named_Actual (PAN, Extra);
1629 exit;
1630 end if;
1632 Act := NNA;
1633 end;
1634 end loop;
1635 end if;
1637 -- Analyze and resolve the new actual. We do not need to
1638 -- establish the relevant scope stack entries here, because
1639 -- we have already set all the correct entity references, so
1640 -- no name resolution is needed.
1642 -- We analyze with all checks suppressed (since we do not
1643 -- expect any exceptions, and also we temporarily turn off
1644 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
1645 -- references (not needed at this stage, and in fact causes
1646 -- a bit of recursive chaos).
1648 Opt.Unnest_Subprogram_Mode := False;
1649 Analyze_And_Resolve
1650 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
1651 Opt.Unnest_Subprogram_Mode := True;
1652 end if;
1653 end Adjust_One_Call;
1654 end loop Adjust_Calls;
1656 return;
1657 end Unnest_Subprogram;
1659 end Exp_Unst;