Daily bump.
[official-gcc.git] / gcc / ada / exp_unst.adb
blobc2a72431d34cb5ed130f3ae0e0485b56b37b29f2
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_Name (J : Pos; S : String) return Name_Id;
128 -- Returns name for string ARECjS, where j is the decimal value of j
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 Img_Pos (N : Pos) return String;
141 -- Return image of N without leading blank
143 function Subp_Index (Sub : Entity_Id) return SI_Type;
144 -- Given the entity for a subprogram, return corresponding Subps index
146 function Upref_Name
147 (Ent : Entity_Id;
148 Index : Pos;
149 Clist : List_Id) return Name_Id;
150 -- This function returns the name to be used in the activation record to
151 -- reference the variable uplevel. Clist is the list of components that
152 -- have been created in the activation record so far. Normally the name
153 -- is just a copy of the Chars field of the entity. The exception is
154 -- when the name has already been used, in which case we suffix the name
155 -- with the index value Index to avoid duplication. This happens with
156 -- declare blocks and generic parameters at least.
158 ---------------
159 -- AREC_Name --
160 ---------------
162 function AREC_Name (J : Pos; S : String) return Name_Id is
163 begin
164 return Name_Find_Str ("AREC" & Img_Pos (J) & S);
165 end AREC_Name;
167 --------------------
168 -- Enclosing_Subp --
169 --------------------
171 function Enclosing_Subp (Subp : SI_Type) return SI_Type is
172 STJ : Subp_Entry renames Subps.Table (Subp);
173 Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
174 begin
175 pragma Assert (STJ.Lev > 1);
176 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
177 return Ret;
178 end Enclosing_Subp;
180 ---------------
181 -- Get_Level --
182 ---------------
184 function Get_Level (Sub : Entity_Id) return Nat is
185 Lev : Nat;
186 S : Entity_Id;
187 begin
188 Lev := 1;
189 S := Sub;
190 loop
191 if S = Subp then
192 return Lev;
193 else
194 S := Enclosing_Subprogram (S);
195 Lev := Lev + 1;
196 end if;
197 end loop;
198 end Get_Level;
200 -------------
201 -- Img_Pos --
202 -------------
204 function Img_Pos (N : Pos) return String is
205 Buf : String (1 .. 20);
206 Ptr : Natural;
207 NV : Nat;
209 begin
210 Ptr := Buf'Last;
211 NV := N;
212 while NV /= 0 loop
213 Buf (Ptr) := Character'Val (48 + NV mod 10);
214 Ptr := Ptr - 1;
215 NV := NV / 10;
216 end loop;
218 return Buf (Ptr + 1 .. Buf'Last);
219 end Img_Pos;
221 ----------------
222 -- Subp_Index --
223 ----------------
225 function Subp_Index (Sub : Entity_Id) return SI_Type is
226 begin
227 pragma Assert (Is_Subprogram (Sub));
228 return SI_Type (UI_To_Int (Subps_Index (Sub)));
229 end Subp_Index;
231 ----------------
232 -- Upref_Name --
233 ----------------
235 function Upref_Name
236 (Ent : Entity_Id;
237 Index : Pos;
238 Clist : List_Id) return Name_Id
240 C : Node_Id;
241 begin
242 C := First (Clist);
243 loop
244 if No (C) then
245 return Chars (Ent);
246 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
247 return Name_Find_Str
248 (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
249 else
250 Next (C);
251 end if;
252 end loop;
253 end Upref_Name;
255 -- Start of processing for Unnest_Subprogram
257 begin
258 -- Nothing to do inside a generic (all processing is for instance)
260 if Inside_A_Generic then
261 return;
262 end if;
264 -- At least for now, do not unnest anything but main source unit
266 if not In_Extended_Main_Source_Unit (Subp_Body) then
267 return;
268 end if;
270 -- This routine is called late, after the scope stack is gone. The
271 -- following creates a suitable dummy scope stack to be used for the
272 -- analyze/expand calls made from this routine.
274 Push_Scope (Subp);
276 -- First step, we must mark all nested subprograms that require a static
277 -- link (activation record) because either they contain explicit uplevel
278 -- references (as indicated by ??? being set at this
279 -- point), or they make calls to other subprograms in the same nest that
280 -- require a static link (in which case we set this flag).
282 -- This is a recursive definition, and to implement this, we have to
283 -- build a call graph for the set of nested subprograms, and then go
284 -- over this graph to implement recursively the invariant that if a
285 -- subprogram has a call to a subprogram requiring a static link, then
286 -- the calling subprogram requires a static link.
288 -- First populate the above tables
290 Subps_First := Subps.Last + 1;
291 Calls.Init;
292 Urefs.Init;
294 Build_Tables : declare
295 Current_Subprogram : Entity_Id;
296 -- When we scan a subprogram body, we set Current_Subprogram to the
297 -- corresponding entity. This gets recursively saved and restored.
299 function Visit_Node (N : Node_Id) return Traverse_Result;
300 -- Visit a single node in Subp
302 -----------
303 -- Visit --
304 -----------
306 procedure Visit is new Traverse_Proc (Visit_Node);
307 -- Used to traverse the body of Subp, populating the tables
309 ----------------
310 -- Visit_Node --
311 ----------------
313 function Visit_Node (N : Node_Id) return Traverse_Result is
314 Ent : Entity_Id;
315 Caller : Entity_Id;
316 Callee : Entity_Id;
318 procedure Check_Static_Type (T : Entity_Id; DT : in out Boolean);
319 -- Given a type T, checks if it is a static type defined as a
320 -- type with no dynamic bounds in sight. If so, the only action
321 -- is to set Is_Static_Type True for T. If T is not a static
322 -- type, then all types with dynamic bounds associated with
323 -- T are detected, and their bounds are marked as uplevel
324 -- referenced if not at the library level, and DT is set True.
326 procedure Note_Uplevel_Ref
327 (E : Entity_Id;
328 Caller : Entity_Id;
329 Callee : Entity_Id);
330 -- Called when we detect an explicit or implicit uplevel reference
331 -- from within Caller to entity E declared in Callee. E can be a
332 -- an object or a type.
334 -----------------------
335 -- Check_Static_Type --
336 -----------------------
338 procedure Check_Static_Type (T : Entity_Id; DT : in out Boolean) is
339 procedure Note_Uplevel_Bound (N : Node_Id);
340 -- N is the bound of a dynamic type. This procedure notes that
341 -- this bound is uplevel referenced, it can handle references
342 -- to entities (typically _FIRST and _LAST entities), and also
343 -- attribute references of the form T'name (name is typically
344 -- FIRST or LAST) where T is the uplevel referenced bound.
346 ------------------------
347 -- Note_Uplevel_Bound --
348 ------------------------
350 procedure Note_Uplevel_Bound (N : Node_Id) is
351 begin
352 -- Entity name case
354 if Is_Entity_Name (N) then
355 if Present (Entity (N)) then
356 Note_Uplevel_Ref
357 (E => Entity (N),
358 Caller => Current_Subprogram,
359 Callee => Enclosing_Subprogram (Entity (N)));
360 end if;
362 -- Attribute case
364 elsif Nkind (N) = N_Attribute_Reference then
365 Note_Uplevel_Bound (Prefix (N));
366 end if;
367 end Note_Uplevel_Bound;
369 -- Start of processing for Check_Static_Type
371 begin
372 -- If already marked static, immediate return
374 if Is_Static_Type (T) then
375 return;
376 end if;
378 -- If the type is at library level, always consider it static,
379 -- since such uplevel references are irrelevant.
381 if Is_Library_Level_Entity (T) then
382 Set_Is_Static_Type (T);
383 return;
384 end if;
386 -- Otherwise figure out what the story is with this type
388 -- For a scalar type, check bounds
390 if Is_Scalar_Type (T) then
392 -- If both bounds static, then this is a static type
394 declare
395 LB : constant Node_Id := Type_Low_Bound (T);
396 UB : constant Node_Id := Type_High_Bound (T);
398 begin
399 if not Is_Static_Expression (LB) then
400 Note_Uplevel_Bound (LB);
401 DT := True;
402 end if;
404 if not Is_Static_Expression (UB) then
405 Note_Uplevel_Bound (UB);
406 DT := True;
407 end if;
408 end;
410 -- For record type, check all components
412 elsif Is_Record_Type (T) then
413 declare
414 C : Entity_Id;
415 begin
416 C := First_Component_Or_Discriminant (T);
417 while Present (C) loop
418 Check_Static_Type (Etype (C), DT);
419 Next_Component_Or_Discriminant (C);
420 end loop;
421 end;
423 -- For array type, check index types and component type
425 elsif Is_Array_Type (T) then
426 declare
427 IX : Node_Id;
428 begin
429 Check_Static_Type (Component_Type (T), DT);
431 IX := First_Index (T);
432 while Present (IX) loop
433 Check_Static_Type (Etype (IX), DT);
434 Next_Index (IX);
435 end loop;
436 end;
438 -- For now, ignore other types
440 else
441 return;
442 end if;
444 if not DT then
445 Set_Is_Static_Type (T);
446 end if;
447 end Check_Static_Type;
449 ----------------------
450 -- Note_Uplevel_Ref --
451 ----------------------
453 procedure Note_Uplevel_Ref
454 (E : Entity_Id;
455 Caller : Entity_Id;
456 Callee : Entity_Id)
458 begin
459 -- Nothing to do for static type
461 if Is_Static_Type (E) then
462 return;
463 end if;
465 -- Nothing to do if Caller and Callee are the same
467 if Caller = Callee then
468 return;
469 end if;
471 -- We have a new uplevel referenced entity
473 -- All we do at this stage is to add the uplevel reference to
474 -- the table. It's too earch to do anything else, since this
475 -- uplevel reference may come from an unreachable subprogram
476 -- in which case the entry will be deleted.
478 Urefs.Append ((N, E, Caller, Callee));
479 end Note_Uplevel_Ref;
481 -- Start of processing for Visit_Node
483 begin
484 -- Record a call
486 if Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
488 -- We are only interested in direct calls, not indirect calls
489 -- (where Name (N) is an explicit dereference) at least for now!
491 and then Nkind (Name (N)) in N_Has_Entity
492 then
493 Ent := Entity (Name (N));
495 -- We are only interested in calls to subprograms nested
496 -- within Subp. Calls to Subp itself or to subprograms that
497 -- are outside the nested structure do not affect us.
499 if Scope_Within (Ent, Subp) then
501 -- Ignore calls to imported routines
503 if Is_Imported (Ent) then
504 null;
506 -- Here we have a call to keep and analyze
508 else
509 -- Both caller and callee must be subprograms
511 if Is_Subprogram (Ent) then
512 Calls.Append ((N, Current_Subprogram, Ent));
513 end if;
514 end if;
515 end if;
517 -- Record a subprogram. We record a subprogram body that acts as
518 -- a spec. Otherwise we record a subprogram declaration, providing
519 -- that it has a corresponding body we can get hold of. The case
520 -- of no corresponding body being available is ignored for now.
522 elsif Nkind (N) = N_Subprogram_Body then
523 Ent := Corresponding_Spec_Of (N);
525 -- Ignore generic subprogram
527 if Is_Generic_Subprogram (Ent) then
528 return Skip;
529 end if;
531 -- Make new entry in subprogram table if not already made
533 declare
534 L : constant Nat := Get_Level (Ent);
535 begin
536 Subps.Append
537 ((Ent => Ent,
538 Bod => N,
539 Lev => L,
540 Reachable => False,
541 Uplevel_Ref => L,
542 Declares_AREC => False,
543 Uents => No_Elist,
544 Last => 0,
545 ARECnF => Empty,
546 ARECn => Empty,
547 ARECnT => Empty,
548 ARECnPT => Empty,
549 ARECnP => Empty,
550 ARECnU => Empty));
551 Set_Subps_Index (Ent, UI_From_Int (Subps.Last));
552 end;
554 -- We make a recursive call to scan the subprogram body, so
555 -- that we can save and restore Current_Subprogram.
557 declare
558 Save_CS : constant Entity_Id := Current_Subprogram;
559 Decl : Node_Id;
561 begin
562 Current_Subprogram := Ent;
564 -- Scan declarations
566 Decl := First (Declarations (N));
567 while Present (Decl) loop
568 Visit (Decl);
569 Next (Decl);
570 end loop;
572 -- Scan statements
574 Visit (Handled_Statement_Sequence (N));
576 -- Restore current subprogram setting
578 Current_Subprogram := Save_CS;
579 end;
581 -- Now at this level, return skipping the subprogram body
582 -- descendents, since we already took care of them!
584 return Skip;
586 -- Record an uplevel reference
588 elsif Nkind (N) in N_Has_Entity and then Present (Entity (N)) then
589 Ent := Entity (N);
591 -- Only interested in entities declared within our nest
593 if not Is_Library_Level_Entity (Ent)
594 and then Scope_Within_Or_Same (Scope (Ent), Subp)
595 and then
597 -- Constants and variables are interesting
599 (Ekind_In (Ent, E_Constant, E_Variable)
601 -- Formals are interesting, but not if being used as mere
602 -- names of parameters for name notation calls.
604 or else
605 (Is_Formal (Ent)
606 and then not
607 (Nkind (Parent (N)) = N_Parameter_Association
608 and then Selector_Name (Parent (N)) = N))
610 -- Types other than known Is_Static types are interesting
612 or else (Is_Type (Ent)
613 and then not Is_Static_Type (Ent)))
614 then
615 -- Here we have a possible interesting uplevel reference
617 if Is_Type (Ent) then
618 declare
619 DT : Boolean := False;
621 begin
622 Check_Static_Type (Ent, DT);
624 if Is_Static_Type (Ent) then
625 return OK;
626 end if;
627 end;
628 end if;
630 Caller := Current_Subprogram;
631 Callee := Enclosing_Subprogram (Ent);
633 if Callee /= Caller and then not Is_Static_Type (Ent) then
634 Note_Uplevel_Ref (Ent, Caller, Callee);
635 end if;
636 end if;
638 -- If we have a body stub, visit the associated subunit
640 elsif Nkind (N) in N_Body_Stub then
641 Visit (Library_Unit (N));
643 -- Skip generic declarations
645 elsif Nkind (N) in N_Generic_Declaration then
646 return Skip;
648 -- Skip generic package body
650 elsif Nkind (N) = N_Package_Body
651 and then Present (Corresponding_Spec (N))
652 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
653 then
654 return Skip;
655 end if;
657 -- Fall through to continue scanning children of this node
659 return OK;
660 end Visit_Node;
662 -- Start of processing for Build_Tables
664 begin
665 -- Traverse the body to get subprograms, calls and uplevel references
667 Visit (Subp_Body);
668 end Build_Tables;
670 -- Now do the first transitive closure which determines which
671 -- subprograms in the nest are actually reachable.
673 Reachable_Closure : declare
674 Modified : Boolean;
676 begin
677 Subps.Table (1).Reachable := True;
679 -- We use a simple minded algorithm as follows (obviously this can
680 -- be done more efficiently, using one of the standard algorithms
681 -- for efficient transitive closure computation, but this is simple
682 -- and most likely fast enough that its speed does not matter).
684 -- Repeatedly scan the list of calls. Any time we find a call from
685 -- A to B, where A is reachable, but B is not, then B is reachable,
686 -- and note that we have made a change by setting Modified True. We
687 -- repeat this until we make a pass with no modifications.
689 Outer : loop
690 Modified := False;
691 Inner : for J in Calls.First .. Calls.Last loop
692 declare
693 CTJ : Call_Entry renames Calls.Table (J);
695 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
696 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
698 SUBF : Subp_Entry renames Subps.Table (SINF);
699 SUBT : Subp_Entry renames Subps.Table (SINT);
701 begin
702 if SUBF.Reachable and then not SUBT.Reachable then
703 SUBT.Reachable := True;
704 Modified := True;
705 end if;
706 end;
707 end loop Inner;
709 exit Outer when not Modified;
710 end loop Outer;
711 end Reachable_Closure;
713 -- Remove calls from unreachable subprograms
715 declare
716 New_Index : Nat;
718 begin
719 New_Index := 0;
720 for J in Calls.First .. Calls.Last loop
721 declare
722 CTJ : Call_Entry renames Calls.Table (J);
724 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
725 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
727 SUBF : Subp_Entry renames Subps.Table (SINF);
728 SUBT : Subp_Entry renames Subps.Table (SINT);
730 begin
731 if SUBF.Reachable then
732 pragma Assert (SUBT.Reachable);
733 New_Index := New_Index + 1;
734 Calls.Table (New_Index) := Calls.Table (J);
735 end if;
736 end;
737 end loop;
739 Calls.Set_Last (New_Index);
740 end;
742 -- Remove uplevel references from unreachable subprograms
744 declare
745 New_Index : Nat;
747 begin
748 New_Index := 0;
749 for J in Urefs.First .. Urefs.Last loop
750 declare
751 URJ : Uref_Entry renames Urefs.Table (J);
753 SINF : constant SI_Type := Subp_Index (URJ.Caller);
754 SINT : constant SI_Type := Subp_Index (URJ.Callee);
756 SUBF : Subp_Entry renames Subps.Table (SINF);
757 SUBT : Subp_Entry renames Subps.Table (SINT);
759 S : Entity_Id;
761 begin
762 -- Keep reachable reference
764 if SUBF.Reachable then
765 New_Index := New_Index + 1;
766 Urefs.Table (New_Index) := Urefs.Table (J);
768 -- And since we know we are keeping this one, this is a good
769 -- place to fill in information for a good reference.
771 -- Mark all enclosing subprograms need to declare AREC
773 S := URJ.Caller;
774 loop
775 S := Enclosing_Subprogram (S);
776 Subps.Table (Subp_Index (S)).Declares_AREC := True;
777 exit when S = URJ.Callee;
778 end loop;
780 -- Add to list of uplevel referenced entities for Callee.
781 -- We do not add types to this list, only actual references
782 -- to objects that will be referenced uplevel, and we use
783 -- the flag Is_Uplevel_Referenced_Entity to avoid making
784 -- duplicate entries in the list.
786 if not Is_Uplevel_Referenced_Entity (URJ.Ent) then
787 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
789 if not Is_Type (URJ.Ent) then
790 Append_New_Elmt (URJ.Ent, SUBT.Uents);
791 end if;
792 end if;
794 -- And set uplevel indication for caller
796 if SUBT.Lev < SUBF.Uplevel_Ref then
797 SUBF.Uplevel_Ref := SUBT.Lev;
798 end if;
799 end if;
800 end;
801 end loop;
803 Urefs.Set_Last (New_Index);
804 end;
806 -- Remove unreachable subprograms from Subps table. Note that we do
807 -- this after eliminating entries from the other two tables, since
808 -- thos elimination steps depend on referencing the Subps table.
810 declare
811 New_SI : SI_Type;
813 begin
814 New_SI := 0;
815 for J in Subps_First .. Subps.Last loop
816 declare
817 STJ : Subp_Entry renames Subps.Table (J);
818 Spec : Node_Id;
819 Decl : Node_Id;
821 begin
822 -- Subprogram is reachable, copy and reset index
824 if STJ.Reachable then
825 New_SI := New_SI + 1;
826 Subps.Table (New_SI) := STJ;
827 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
829 -- Subprogram is not reachable
831 else
832 -- Clear index, since no longer active
834 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
836 -- Output debug information if -gnatd.3 set
838 if Debug_Flag_Dot_3 then
839 Write_Str ("Eliminate ");
840 Write_Name (Chars (Subps.Table (J).Ent));
841 Write_Str (" at ");
842 Write_Location (Sloc (Subps.Table (J).Ent));
843 Write_Str (" (not referenced)");
844 Write_Eol;
845 end if;
847 -- Rewrite declaration and body to null statements
849 Spec := Corresponding_Spec (STJ.Bod);
851 if Present (Spec) then
852 Decl := Parent (Declaration_Node (Spec));
853 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
854 end if;
856 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
857 end if;
858 end;
859 end loop;
861 Subps.Set_Last (New_SI);
862 end;
864 -- Now it is time for the second transitive closure, which follows calls
865 -- and makes sure that A calls B, and B has uplevel references, then A
866 -- is also marked as having uplevel references.
868 Closure_Uplevel : declare
869 Modified : Boolean;
871 begin
872 -- We use a simple minded algorithm as follows (obviously this can
873 -- be done more efficiently, using one of the standard algorithms
874 -- for efficient transitive closure computation, but this is simple
875 -- and most likely fast enough that its speed does not matter).
877 -- Repeatedly scan the list of calls. Any time we find a call from
878 -- A to B, where B has uplevel references, make sure that A is marked
879 -- as having at least the same level of uplevel referencing.
881 Outer2 : loop
882 Modified := False;
883 Inner2 : for J in Calls.First .. Calls.Last loop
884 declare
885 CTJ : Call_Entry renames Calls.Table (J);
886 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
887 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
888 SUBF : Subp_Entry renames Subps.Table (SINF);
889 SUBT : Subp_Entry renames Subps.Table (SINT);
890 begin
891 if SUBT.Lev > SUBT.Uplevel_Ref
892 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
893 then
894 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
895 Modified := True;
896 end if;
897 end;
898 end loop Inner2;
900 exit Outer2 when not Modified;
901 end loop Outer2;
902 end Closure_Uplevel;
904 -- We have one more step before the tables are complete. An uplevel
905 -- call from subprogram A to subprogram B where subprogram B has uplevel
906 -- references is in effect an uplevel reference, and must arrange for
907 -- the proper activation link to be passed.
909 for J in Calls.First .. Calls.Last loop
910 declare
911 CTJ : Call_Entry renames Calls.Table (J);
913 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
914 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
916 SUBF : Subp_Entry renames Subps.Table (SINF);
917 SUBT : Subp_Entry renames Subps.Table (SINT);
919 A : Entity_Id;
921 begin
922 -- If callee has uplevel references
924 if SUBT.Uplevel_Ref < SUBT.Lev
926 -- And this is an uplevel call
928 and then SUBT.Lev < SUBF.Lev
929 then
930 -- We need to arrange for finding the uplink
932 A := CTJ.Caller;
933 loop
934 A := Enclosing_Subprogram (A);
935 Subps.Table (Subp_Index (A)).Declares_AREC := True;
936 exit when A = CTJ.Callee;
938 -- In any case exit when we get to the outer level. This
939 -- happens in some odd cases with generics (in particular
940 -- sem_ch3.adb does not compile without this kludge ???).
942 exit when A = Subp;
943 end loop;
944 end if;
945 end;
946 end loop;
948 -- The tables are now complete, so we can record the last index in the
949 -- Subps table for later reference in Cprint.
951 Subps.Table (Subps_First).Last := Subps.Last;
953 -- Next step, create the entities for code we will insert. We do this
954 -- at the start so that all the entities are defined, regardless of the
955 -- order in which we do the code insertions.
957 Create_Entities : for J in Subps_First .. Subps.Last loop
958 declare
959 STJ : Subp_Entry renames Subps.Table (J);
960 Loc : constant Source_Ptr := Sloc (STJ.Bod);
962 begin
963 -- First we create the ARECnF entity for the additional formal for
964 -- all subprograms which need an activation record passed.
966 if STJ.Uplevel_Ref < STJ.Lev then
967 STJ.ARECnF :=
968 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
969 end if;
971 -- Define the AREC entities for the activation record if needed
973 if STJ.Declares_AREC then
974 STJ.ARECn :=
975 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
976 STJ.ARECnT :=
977 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
978 STJ.ARECnPT :=
979 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
980 STJ.ARECnP :=
981 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
983 -- Define uplink component entity if inner nesting case
985 if Present (STJ.ARECnF) then
986 STJ.ARECnU :=
987 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
988 end if;
989 end if;
990 end;
991 end loop Create_Entities;
993 -- Loop through subprograms
995 Subp_Loop : declare
996 Addr : constant Entity_Id := RTE (RE_Address);
998 begin
999 for J in Subps_First .. Subps.Last loop
1000 declare
1001 STJ : Subp_Entry renames Subps.Table (J);
1003 begin
1004 -- First add the extra formal if needed. This applies to all
1005 -- nested subprograms that require an activation record to be
1006 -- passed, as indicated by ARECnF being defined.
1008 if Present (STJ.ARECnF) then
1010 -- Here we need the extra formal. We do the expansion and
1011 -- analysis of this manually, since it is fairly simple,
1012 -- and it is not obvious how we can get what we want if we
1013 -- try to use the normal Analyze circuit.
1015 Add_Extra_Formal : declare
1016 Encl : constant SI_Type := Enclosing_Subp (J);
1017 STJE : Subp_Entry renames Subps.Table (Encl);
1018 -- Index and Subp_Entry for enclosing routine
1020 Form : constant Entity_Id := STJ.ARECnF;
1021 -- The formal to be added. Note that n here is one less
1022 -- than the level of the subprogram itself (STJ.Ent).
1024 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1025 -- S is an N_Function/Procedure_Specification node, and F
1026 -- is the new entity to add to this subprogramn spec as
1027 -- the last Extra_Formal.
1029 ----------------------
1030 -- Add_Form_To_Spec --
1031 ----------------------
1033 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1034 Sub : constant Entity_Id := Defining_Entity (S);
1035 Ent : Entity_Id;
1037 begin
1038 -- Case of at least one Extra_Formal is present, set
1039 -- ARECnF as the new last entry in the list.
1041 if Present (Extra_Formals (Sub)) then
1042 Ent := Extra_Formals (Sub);
1043 while Present (Extra_Formal (Ent)) loop
1044 Ent := Extra_Formal (Ent);
1045 end loop;
1047 Set_Extra_Formal (Ent, F);
1049 -- No Extra formals present
1051 else
1052 Set_Extra_Formals (Sub, F);
1053 Ent := Last_Formal (Sub);
1055 if Present (Ent) then
1056 Set_Extra_Formal (Ent, F);
1057 end if;
1058 end if;
1059 end Add_Form_To_Spec;
1061 -- Start of processing for Add_Extra_Formal
1063 begin
1064 -- Decorate the new formal entity
1066 Set_Scope (Form, STJ.Ent);
1067 Set_Ekind (Form, E_In_Parameter);
1068 Set_Etype (Form, STJE.ARECnPT);
1069 Set_Mechanism (Form, By_Copy);
1070 Set_Never_Set_In_Source (Form, True);
1071 Set_Analyzed (Form, True);
1072 Set_Comes_From_Source (Form, False);
1074 -- Case of only body present
1076 if Acts_As_Spec (STJ.Bod) then
1077 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1079 -- Case of separate spec
1081 else
1082 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1083 end if;
1084 end Add_Extra_Formal;
1085 end if;
1087 -- Processing for subprograms that declare an activation record
1089 if Present (STJ.ARECn) then
1091 -- Local declarations for one such subprogram
1093 declare
1094 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1095 Clist : List_Id;
1096 Comp : Entity_Id;
1098 Decl_ARECnT : Node_Id;
1099 Decl_ARECnPT : Node_Id;
1100 Decl_ARECn : Node_Id;
1101 Decl_ARECnP : Node_Id;
1102 -- Declaration nodes for the AREC entities we build
1104 Decl_Assign : Node_Id;
1105 -- Assigment to set uplink, Empty if none
1107 Decls : List_Id;
1108 -- List of new declarations we create
1110 begin
1111 -- Build list of component declarations for ARECnT
1113 Clist := Empty_List;
1115 -- If we are in a subprogram that has a static link that
1116 -- is passed in (as indicated by ARECnF being defined),
1117 -- then include ARECnU : ARECmPT where ARECmPT comes from
1118 -- the level one higher than the current level, and the
1119 -- entity ARECnPT comes from the enclosing subprogram.
1121 if Present (STJ.ARECnF) then
1122 declare
1123 STJE : Subp_Entry
1124 renames Subps.Table (Enclosing_Subp (J));
1125 begin
1126 Append_To (Clist,
1127 Make_Component_Declaration (Loc,
1128 Defining_Identifier => STJ.ARECnU,
1129 Component_Definition =>
1130 Make_Component_Definition (Loc,
1131 Subtype_Indication =>
1132 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1133 end;
1134 end if;
1136 -- Add components for uplevel referenced entities
1138 if Present (STJ.Uents) then
1139 declare
1140 Elmt : Elmt_Id;
1141 Uent : Entity_Id;
1143 Indx : Nat;
1144 -- 1's origin of index in list of elements. This is
1145 -- used to uniquify names if needed in Upref_Name.
1147 begin
1148 Elmt := First_Elmt (STJ.Uents);
1149 Indx := 0;
1150 while Present (Elmt) loop
1151 Uent := Node (Elmt);
1152 Indx := Indx + 1;
1154 Comp :=
1155 Make_Defining_Identifier (Loc,
1156 Chars => Upref_Name (Uent, Indx, Clist));
1158 Set_Activation_Record_Component
1159 (Uent, Comp);
1161 Append_To (Clist,
1162 Make_Component_Declaration (Loc,
1163 Defining_Identifier => Comp,
1164 Component_Definition =>
1165 Make_Component_Definition (Loc,
1166 Subtype_Indication =>
1167 New_Occurrence_Of (Addr, Loc))));
1169 Next_Elmt (Elmt);
1170 end loop;
1171 end;
1172 end if;
1174 -- Now we can insert the AREC declarations into the body
1176 -- type ARECnT is record .. end record;
1178 Decl_ARECnT :=
1179 Make_Full_Type_Declaration (Loc,
1180 Defining_Identifier => STJ.ARECnT,
1181 Type_Definition =>
1182 Make_Record_Definition (Loc,
1183 Component_List =>
1184 Make_Component_List (Loc,
1185 Component_Items => Clist)));
1186 Decls := New_List (Decl_ARECnT);
1188 -- type ARECnPT is access all ARECnT;
1190 Decl_ARECnPT :=
1191 Make_Full_Type_Declaration (Loc,
1192 Defining_Identifier => STJ.ARECnPT,
1193 Type_Definition =>
1194 Make_Access_To_Object_Definition (Loc,
1195 All_Present => True,
1196 Subtype_Indication =>
1197 New_Occurrence_Of (STJ.ARECnT, Loc)));
1198 Append_To (Decls, Decl_ARECnPT);
1200 -- ARECn : aliased ARECnT;
1202 Decl_ARECn :=
1203 Make_Object_Declaration (Loc,
1204 Defining_Identifier => STJ.ARECn,
1205 Aliased_Present => True,
1206 Object_Definition =>
1207 New_Occurrence_Of (STJ.ARECnT, Loc));
1208 Append_To (Decls, Decl_ARECn);
1210 -- ARECnP : constant ARECnPT := ARECn'Access;
1212 Decl_ARECnP :=
1213 Make_Object_Declaration (Loc,
1214 Defining_Identifier => STJ.ARECnP,
1215 Constant_Present => True,
1216 Object_Definition =>
1217 New_Occurrence_Of (STJ.ARECnPT, Loc),
1218 Expression =>
1219 Make_Attribute_Reference (Loc,
1220 Prefix =>
1221 New_Occurrence_Of (STJ.ARECn, Loc),
1222 Attribute_Name => Name_Access));
1223 Append_To (Decls, Decl_ARECnP);
1225 -- If we are in a subprogram that has a static link that
1226 -- is passed in (as indicated by ARECnF being defined),
1227 -- then generate ARECn.ARECmU := ARECmF where m is
1228 -- one less than the current level to set the uplink.
1230 if Present (STJ.ARECnF) then
1231 Decl_Assign :=
1232 Make_Assignment_Statement (Loc,
1233 Name =>
1234 Make_Selected_Component (Loc,
1235 Prefix =>
1236 New_Occurrence_Of (STJ.ARECn, Loc),
1237 Selector_Name =>
1238 New_Occurrence_Of (STJ.ARECnU, Loc)),
1239 Expression =>
1240 New_Occurrence_Of (STJ.ARECnF, Loc));
1241 Append_To (Decls, Decl_Assign);
1243 else
1244 Decl_Assign := Empty;
1245 end if;
1247 Prepend_List_To (Declarations (STJ.Bod), Decls);
1249 -- Analyze the newly inserted declarations. Note that we
1250 -- do not need to establish the whole scope stack, since
1251 -- we have already set all entity fields (so there will
1252 -- be no searching of upper scopes to resolve names). But
1253 -- we do set the scope of the current subprogram, so that
1254 -- newly created entities go in the right entity chain.
1256 -- We analyze with all checks suppressed (since we do
1257 -- not expect any exceptions).
1259 Push_Scope (STJ.Ent);
1260 Analyze (Decl_ARECnT, Suppress => All_Checks);
1261 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1262 Analyze (Decl_ARECn, Suppress => All_Checks);
1263 Analyze (Decl_ARECnP, Suppress => All_Checks);
1265 if Present (Decl_Assign) then
1266 Analyze (Decl_Assign, Suppress => All_Checks);
1267 end if;
1269 Pop_Scope;
1271 -- Mark the types as needing typedefs
1273 Set_Needs_Typedef (STJ.ARECnT);
1274 Set_Needs_Typedef (STJ.ARECnPT);
1276 -- Next step, for each uplevel referenced entity, add
1277 -- assignment operations to set the component in the
1278 -- activation record.
1280 if Present (STJ.Uents) then
1281 declare
1282 Elmt : Elmt_Id;
1284 begin
1285 Elmt := First_Elmt (STJ.Uents);
1286 while Present (Elmt) loop
1287 declare
1288 Ent : constant Entity_Id := Node (Elmt);
1289 Loc : constant Source_Ptr := Sloc (Ent);
1290 Dec : constant Node_Id :=
1291 Declaration_Node (Ent);
1292 Ins : Node_Id;
1293 Asn : Node_Id;
1295 begin
1296 -- For parameters, we insert the assignment
1297 -- right after the declaration of ARECnP.
1298 -- For all other entities, we insert
1299 -- the assignment immediately after
1300 -- the declaration of the entity.
1302 -- Note: we don't need to mark the entity
1303 -- as being aliased, because the address
1304 -- attribute will mark it as Address_Taken,
1305 -- and that is good enough.
1307 if Is_Formal (Ent) then
1308 Ins := Decl_ARECnP;
1309 else
1310 Ins := Dec;
1311 end if;
1313 -- Build and insert the assignment:
1314 -- ARECn.nam := nam'Address
1316 Asn :=
1317 Make_Assignment_Statement (Loc,
1318 Name =>
1319 Make_Selected_Component (Loc,
1320 Prefix =>
1321 New_Occurrence_Of (STJ.ARECn, Loc),
1322 Selector_Name =>
1323 New_Occurrence_Of
1324 (Activation_Record_Component
1325 (Ent),
1326 Loc)),
1328 Expression =>
1329 Make_Attribute_Reference (Loc,
1330 Prefix =>
1331 New_Occurrence_Of (Ent, Loc),
1332 Attribute_Name => Name_Address));
1334 Insert_After (Ins, Asn);
1336 -- Analyze the assignment statement. We do
1337 -- not need to establish the relevant scope
1338 -- stack entries here, because we have
1339 -- already set the correct entity references,
1340 -- so no name resolution is required, and no
1341 -- new entities are created, so we don't even
1342 -- need to set the current scope.
1344 -- We analyze with all checks suppressed
1345 -- (since we do not expect any exceptions).
1347 Analyze (Asn, Suppress => All_Checks);
1348 end;
1350 Next_Elmt (Elmt);
1351 end loop;
1352 end;
1353 end if;
1354 end;
1355 end if;
1356 end;
1357 end loop;
1358 end Subp_Loop;
1360 -- Next step, process uplevel references. This has to be done in a
1361 -- separate pass, after completing the processing in Sub_Loop because we
1362 -- need all the AREC declarations generated, inserted, and analyzed so
1363 -- that the uplevel references can be successfully analyzed.
1365 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
1366 declare
1367 UPJ : Uref_Entry renames Urefs.Table (J);
1369 begin
1370 -- Ignore type references, these are implicit references that do
1371 -- not need rewriting (e.g. the appearence in a conversion).
1373 if Is_Type (UPJ.Ent) then
1374 goto Continue;
1375 end if;
1377 -- Also ignore uplevel references to bounds of types that come
1378 -- from the original type reference.
1380 if Is_Entity_Name (UPJ.Ref)
1381 and then Present (Entity (UPJ.Ref))
1382 and then Is_Type (Entity (UPJ.Ref))
1383 then
1384 goto Continue;
1385 end if;
1387 -- Rewrite one reference
1389 Rewrite_One_Ref : declare
1390 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
1391 -- Source location for the reference
1393 Typ : constant Entity_Id := Etype (UPJ.Ent);
1394 -- The type of the referenced entity
1396 Atyp : constant Entity_Id := Get_Actual_Subtype (UPJ.Ref);
1397 -- The actual subtype of the reference
1399 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
1400 -- Subp_Index for caller containing reference
1402 STJR : Subp_Entry renames Subps.Table (RS_Caller);
1403 -- Subp_Entry for subprogram containing reference
1405 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
1406 -- Subp_Index for subprogram containing referenced entity
1408 STJE : Subp_Entry renames Subps.Table (RS_Callee);
1409 -- Subp_Entry for subprogram containing referenced entity
1411 Pfx : Node_Id;
1412 Comp : Entity_Id;
1413 SI : SI_Type;
1415 begin
1416 -- Ignore if no ARECnF entity for enclosing subprogram which
1417 -- probably happens as a result of not properly treating
1418 -- instance bodies. To be examined ???
1420 -- If this test is omitted, then the compilation of
1421 -- freeze.adb and inline.adb fail in unnesting mode.
1423 if No (STJR.ARECnF) then
1424 goto Continue;
1425 end if;
1427 -- Push the current scope, so that the pointer type Tnn, and
1428 -- any subsidiary entities resulting from the analysis of the
1429 -- rewritten reference, go in the right entity chain.
1431 Push_Scope (STJR.Ent);
1433 -- Now we need to rewrite the reference. We have a
1434 -- reference is from level STJR.Lev to level STJE.Lev.
1435 -- The general form of the rewritten reference for
1436 -- entity X is:
1438 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECm.X)
1440 -- where a,b,c,d .. m =
1441 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
1443 pragma Assert (STJR.Lev > STJE.Lev);
1445 -- Compute the prefix of X. Here are examples to make things
1446 -- clear (with parens to show groupings, the prefix is
1447 -- everything except the .X at the end).
1449 -- level 2 to level 1
1451 -- AREC1F.X
1453 -- level 3 to level 1
1455 -- (AREC2F.AREC1U).X
1457 -- level 4 to level 1
1459 -- ((AREC3F.AREC2U).AREC1U).X
1461 -- level 6 to level 2
1463 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
1465 -- In the above, ARECnF and ARECnU are pointers, so there are
1466 -- explicit dereferences required for these occurrences.
1468 Pfx :=
1469 Make_Explicit_Dereference (Loc,
1470 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
1471 SI := RS_Caller;
1472 for L in STJE.Lev .. STJR.Lev - 2 loop
1473 SI := Enclosing_Subp (SI);
1474 Pfx :=
1475 Make_Explicit_Dereference (Loc,
1476 Prefix =>
1477 Make_Selected_Component (Loc,
1478 Prefix => Pfx,
1479 Selector_Name =>
1480 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
1481 end loop;
1483 -- Get activation record component (must exist)
1485 Comp := Activation_Record_Component (UPJ.Ent);
1486 pragma Assert (Present (Comp));
1488 -- Do the replacement
1490 Rewrite (UPJ.Ref,
1491 Make_Attribute_Reference (Loc,
1492 Prefix => New_Occurrence_Of (Atyp, Loc),
1493 Attribute_Name => Name_Deref,
1494 Expressions => New_List (
1495 Make_Selected_Component (Loc,
1496 Prefix => Pfx,
1497 Selector_Name =>
1498 New_Occurrence_Of (Comp, Loc)))));
1500 -- Analyze and resolve the new expression. We do not need to
1501 -- establish the relevant scope stack entries here, because we
1502 -- have already set all the correct entity references, so no
1503 -- name resolution is needed. We have already set the current
1504 -- scope, so that any new entities created will be in the right
1505 -- scope.
1507 -- We analyze with all checks suppressed (since we do not
1508 -- expect any exceptions)
1510 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
1511 Pop_Scope;
1512 end Rewrite_One_Ref;
1513 end;
1515 <<Continue>>
1516 null;
1517 end loop Uplev_Refs;
1519 -- Finally, loop through all calls adding extra actual for the
1520 -- activation record where it is required.
1522 Adjust_Calls : for J in Calls.First .. Calls.Last loop
1524 -- Process a single call, we are only interested in a call to a
1525 -- subprogram that actually needs a pointer to an activation record,
1526 -- as indicated by the ARECnF entity being set. This excludes the
1527 -- top level subprogram, and any subprogram not having uplevel refs.
1529 Adjust_One_Call : declare
1530 CTJ : Call_Entry renames Calls.Table (J);
1531 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
1532 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
1534 Loc : constant Source_Ptr := Sloc (CTJ.N);
1536 Extra : Node_Id;
1537 ExtraP : Node_Id;
1538 SubX : SI_Type;
1539 Act : Node_Id;
1541 begin
1542 if Present (STT.ARECnF) then
1544 -- CTJ.N is a call to a subprogram which may require
1545 -- a pointer to an activation record. The subprogram
1546 -- containing the call is CTJ.From and the subprogram being
1547 -- called is CTJ.To, so we have a call from level STF.Lev to
1548 -- level STT.Lev.
1550 -- There are three possibilities:
1552 -- For a call to the same level, we just pass the activation
1553 -- record passed to the calling subprogram.
1555 if STF.Lev = STT.Lev then
1556 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1558 -- For a call that goes down a level, we pass a pointer
1559 -- to the activation record constructed within the caller
1560 -- (which may be the outer level subprogram, but also may
1561 -- be a more deeply nested caller).
1563 elsif STT.Lev = STF.Lev + 1 then
1564 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
1566 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
1567 -- since it is not possible to do a downcall of more than
1568 -- one level.
1570 -- For a call from level STF.Lev to level STT.Lev, we
1571 -- have to find the activation record needed by the
1572 -- callee. This is as follows:
1574 -- ARECaF.ARECbU.ARECcU....ARECm
1576 -- where a,b,c .. m =
1577 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
1579 else
1580 pragma Assert (STT.Lev < STF.Lev);
1582 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1583 SubX := Subp_Index (CTJ.Caller);
1584 for K in reverse STT.Lev .. STF.Lev - 1 loop
1585 SubX := Enclosing_Subp (SubX);
1586 Extra :=
1587 Make_Selected_Component (Loc,
1588 Prefix => Extra,
1589 Selector_Name =>
1590 New_Occurrence_Of
1591 (Subps.Table (SubX).ARECnU, Loc));
1592 end loop;
1593 end if;
1595 -- Extra is the additional parameter to be added. Build a
1596 -- parameter association that we can append to the actuals.
1598 ExtraP :=
1599 Make_Parameter_Association (Loc,
1600 Selector_Name =>
1601 New_Occurrence_Of (STT.ARECnF, Loc),
1602 Explicit_Actual_Parameter => Extra);
1604 if No (Parameter_Associations (CTJ.N)) then
1605 Set_Parameter_Associations (CTJ.N, Empty_List);
1606 end if;
1608 Append (ExtraP, Parameter_Associations (CTJ.N));
1610 -- We need to deal with the actual parameter chain as well.
1611 -- The newly added parameter is always the last actual.
1613 Act := First_Named_Actual (CTJ.N);
1615 if No (Act) then
1616 Set_First_Named_Actual (CTJ.N, Extra);
1618 -- Here we must follow the chain and append the new entry
1620 else
1621 loop
1622 declare
1623 PAN : Node_Id;
1624 NNA : Node_Id;
1626 begin
1627 PAN := Parent (Act);
1628 pragma Assert (Nkind (PAN) = N_Parameter_Association);
1629 NNA := Next_Named_Actual (PAN);
1631 if No (NNA) then
1632 Set_Next_Named_Actual (PAN, Extra);
1633 exit;
1634 end if;
1636 Act := NNA;
1637 end;
1638 end loop;
1639 end if;
1641 -- Analyze and resolve the new actual. We do not need to
1642 -- establish the relevant scope stack entries here, because
1643 -- we have already set all the correct entity references, so
1644 -- no name resolution is needed.
1646 -- We analyze with all checks suppressed (since we do not
1647 -- expect any exceptions, and also we temporarily turn off
1648 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
1649 -- references (not needed at this stage, and in fact causes
1650 -- a bit of recursive chaos).
1652 Opt.Unnest_Subprogram_Mode := False;
1653 Analyze_And_Resolve
1654 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
1655 Opt.Unnest_Subprogram_Mode := True;
1656 end if;
1657 end Adjust_One_Call;
1658 end loop Adjust_Calls;
1660 return;
1661 end Unnest_Subprogram;
1663 end Exp_Unst;