* tree-vect-loop-manip.c (vect_do_peeling): Do not use
[official-gcc.git] / gcc / ada / exp_unst.adb
blob063b60f93548126b407083dcb5acdce9693e60cb
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-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Lib; use Lib;
31 with Namet; use Namet;
32 with Nlists; use Nlists;
33 with Nmake; use Nmake;
34 with Opt;
35 with Output; use Output;
36 with Rtsfind; use Rtsfind;
37 with Sem; use Sem;
38 with Sem_Aux; use Sem_Aux;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Mech; use Sem_Mech;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Sinput; use Sinput;
45 with Snames; use Snames;
46 with Tbuild; use Tbuild;
47 with Uintp; use Uintp;
49 package body Exp_Unst is
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id);
56 -- Subp is a library-level subprogram which has nested subprograms, and
57 -- Subp_Body is the corresponding N_Subprogram_Body node. This procedure
58 -- declares the AREC types and objects, adds assignments to the AREC record
59 -- as required, defines the xxxPTR types for uplevel referenced objects,
60 -- adds the ARECP parameter to all nested subprograms which need it, and
61 -- modifies all uplevel references appropriately.
63 -----------
64 -- Calls --
65 -----------
67 -- Table to record calls within the nest being analyzed. These are the
68 -- calls which may need to have an AREC actual added. This table is built
69 -- new for each subprogram nest and cleared at the end of processing each
70 -- subprogram nest.
72 type Call_Entry is record
73 N : Node_Id;
74 -- The actual call
76 Caller : Entity_Id;
77 -- Entity of the subprogram containing the call (can be at any level)
79 Callee : Entity_Id;
80 -- Entity of the subprogram called (always at level 2 or higher). Note
81 -- that in accordance with the basic rules of nesting, the level of To
82 -- is either less than or equal to the level of From, or one greater.
83 end record;
85 package Calls is new Table.Table (
86 Table_Component_Type => Call_Entry,
87 Table_Index_Type => Nat,
88 Table_Low_Bound => 1,
89 Table_Initial => 100,
90 Table_Increment => 200,
91 Table_Name => "Unnest_Calls");
92 -- Records each call within the outer subprogram and all nested subprograms
93 -- that are to other subprograms nested within the outer subprogram. These
94 -- are the calls that may need an additional parameter.
96 procedure Append_Unique_Call (Call : Call_Entry);
97 -- Append a call entry to the Calls table. A check is made to see if the
98 -- table already contains this entry and if so it has no effect.
100 -----------
101 -- Urefs --
102 -----------
104 -- Table to record explicit uplevel references to objects (variables,
105 -- constants, formal parameters). These are the references that will
106 -- need rewriting to use the activation table (AREC) pointers. Also
107 -- included are implicit and explicit uplevel references to types, but
108 -- these do not get rewritten by the front end. This table is built new
109 -- for each subprogram nest and cleared at the end of processing each
110 -- subprogram nest.
112 type Uref_Entry is record
113 Ref : Node_Id;
114 -- The reference itself. For objects this is always an entity reference
115 -- and the referenced entity will have its Is_Uplevel_Referenced_Entity
116 -- flag set and will appear in the Uplevel_Referenced_Entities list of
117 -- the subprogram declaring this entity.
119 Ent : Entity_Id;
120 -- The Entity_Id of the uplevel referenced object or type
122 Caller : Entity_Id;
123 -- The entity for the subprogram immediately containing this entity
125 Callee : Entity_Id;
126 -- The entity for the subprogram containing the referenced entity. Note
127 -- that the level of Callee must be less than the level of Caller, since
128 -- this is an uplevel reference.
129 end record;
131 package Urefs is new Table.Table (
132 Table_Component_Type => Uref_Entry,
133 Table_Index_Type => Nat,
134 Table_Low_Bound => 1,
135 Table_Initial => 100,
136 Table_Increment => 200,
137 Table_Name => "Unnest_Urefs");
139 ------------------------
140 -- Append_Unique_Call --
141 ------------------------
143 procedure Append_Unique_Call (Call : Call_Entry) is
144 begin
145 for J in Calls.First .. Calls.Last loop
146 if Calls.Table (J) = Call then
147 return;
148 end if;
149 end loop;
151 Calls.Append (Call);
152 end Append_Unique_Call;
154 ---------------
155 -- Get_Level --
156 ---------------
158 function Get_Level (Subp : Entity_Id; Sub : Entity_Id) return Nat is
159 Lev : Nat;
160 S : Entity_Id;
162 begin
163 Lev := 1;
164 S := Sub;
165 loop
166 if S = Subp then
167 return Lev;
168 else
169 Lev := Lev + 1;
170 S := Enclosing_Subprogram (S);
171 end if;
172 end loop;
173 end Get_Level;
175 ----------------
176 -- Subp_Index --
177 ----------------
179 function Subp_Index (Sub : Entity_Id) return SI_Type is
180 E : Entity_Id := Sub;
182 begin
183 pragma Assert (Is_Subprogram (E));
185 if Subps_Index (E) = Uint_0 then
186 E := Ultimate_Alias (E);
188 if Ekind (E) = E_Function
189 and then Rewritten_For_C (E)
190 and then Present (Corresponding_Procedure (E))
191 then
192 E := Corresponding_Procedure (E);
193 end if;
194 end if;
196 pragma Assert (Subps_Index (E) /= Uint_0);
197 return SI_Type (UI_To_Int (Subps_Index (E)));
198 end Subp_Index;
200 -----------------------
201 -- Unnest_Subprogram --
202 -----------------------
204 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id) is
205 function AREC_Name (J : Pos; S : String) return Name_Id;
206 -- Returns name for string ARECjS, where j is the decimal value of j
208 function Enclosing_Subp (Subp : SI_Type) return SI_Type;
209 -- Subp is the index of a subprogram which has a Lev greater than 1.
210 -- This function returns the index of the enclosing subprogram which
211 -- will have a Lev value one less than this.
213 function Img_Pos (N : Pos) return String;
214 -- Return image of N without leading blank
216 function Upref_Name
217 (Ent : Entity_Id;
218 Index : Pos;
219 Clist : List_Id) return Name_Id;
220 -- This function returns the name to be used in the activation record to
221 -- reference the variable uplevel. Clist is the list of components that
222 -- have been created in the activation record so far. Normally the name
223 -- is just a copy of the Chars field of the entity. The exception is
224 -- when the name has already been used, in which case we suffix the name
225 -- with the index value Index to avoid duplication. This happens with
226 -- declare blocks and generic parameters at least.
228 ---------------
229 -- AREC_Name --
230 ---------------
232 function AREC_Name (J : Pos; S : String) return Name_Id is
233 begin
234 return Name_Find ("AREC" & Img_Pos (J) & S);
235 end AREC_Name;
237 --------------------
238 -- Enclosing_Subp --
239 --------------------
241 function Enclosing_Subp (Subp : SI_Type) return SI_Type is
242 STJ : Subp_Entry renames Subps.Table (Subp);
243 Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
244 begin
245 pragma Assert (STJ.Lev > 1);
246 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
247 return Ret;
248 end Enclosing_Subp;
250 -------------
251 -- Img_Pos --
252 -------------
254 function Img_Pos (N : Pos) return String is
255 Buf : String (1 .. 20);
256 Ptr : Natural;
257 NV : Nat;
259 begin
260 Ptr := Buf'Last;
261 NV := N;
262 while NV /= 0 loop
263 Buf (Ptr) := Character'Val (48 + NV mod 10);
264 Ptr := Ptr - 1;
265 NV := NV / 10;
266 end loop;
268 return Buf (Ptr + 1 .. Buf'Last);
269 end Img_Pos;
271 ----------------
272 -- Upref_Name --
273 ----------------
275 function Upref_Name
276 (Ent : Entity_Id;
277 Index : Pos;
278 Clist : List_Id) return Name_Id
280 C : Node_Id;
281 begin
282 C := First (Clist);
283 loop
284 if No (C) then
285 return Chars (Ent);
287 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
288 return
289 Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
290 else
291 Next (C);
292 end if;
293 end loop;
294 end Upref_Name;
296 -- Start of processing for Unnest_Subprogram
298 begin
299 -- Nothing to do inside a generic (all processing is for instance)
301 if Inside_A_Generic then
302 return;
303 end if;
305 -- At least for now, do not unnest anything but main source unit
307 if not In_Extended_Main_Source_Unit (Subp_Body) then
308 return;
309 end if;
311 -- This routine is called late, after the scope stack is gone. The
312 -- following creates a suitable dummy scope stack to be used for the
313 -- analyze/expand calls made from this routine.
315 Push_Scope (Subp);
317 -- First step, we must mark all nested subprograms that require a static
318 -- link (activation record) because either they contain explicit uplevel
319 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
320 -- this point), or they make calls to other subprograms in the same nest
321 -- that require a static link (in which case we set this flag).
323 -- This is a recursive definition, and to implement this, we have to
324 -- build a call graph for the set of nested subprograms, and then go
325 -- over this graph to implement recursively the invariant that if a
326 -- subprogram has a call to a subprogram requiring a static link, then
327 -- the calling subprogram requires a static link.
329 -- First populate the above tables
331 Subps_First := Subps.Last + 1;
332 Calls.Init;
333 Urefs.Init;
335 Build_Tables : declare
336 Current_Subprogram : Entity_Id;
337 -- When we scan a subprogram body, we set Current_Subprogram to the
338 -- corresponding entity. This gets recursively saved and restored.
340 function Visit_Node (N : Node_Id) return Traverse_Result;
341 -- Visit a single node in Subp
343 -----------
344 -- Visit --
345 -----------
347 procedure Visit is new Traverse_Proc (Visit_Node);
348 -- Used to traverse the body of Subp, populating the tables
350 ----------------
351 -- Visit_Node --
352 ----------------
354 function Visit_Node (N : Node_Id) return Traverse_Result is
355 Ent : Entity_Id;
356 Caller : Entity_Id;
357 Callee : Entity_Id;
359 procedure Check_Static_Type (T : Entity_Id; DT : in out Boolean);
360 -- Given a type T, checks if it is a static type defined as a type
361 -- with no dynamic bounds in sight. If so, the only action is to
362 -- set Is_Static_Type True for T. If T is not a static type, then
363 -- all types with dynamic bounds associated with T are detected,
364 -- and their bounds are marked as uplevel referenced if not at the
365 -- library level, and DT is set True.
367 procedure Note_Uplevel_Ref
368 (E : Entity_Id;
369 Caller : Entity_Id;
370 Callee : Entity_Id);
371 -- Called when we detect an explicit or implicit uplevel reference
372 -- from within Caller to entity E declared in Callee. E can be a
373 -- an object or a type.
375 -----------------------
376 -- Check_Static_Type --
377 -----------------------
379 procedure Check_Static_Type (T : Entity_Id; DT : in out Boolean) is
380 procedure Note_Uplevel_Bound (N : Node_Id);
381 -- N is the bound of a dynamic type. This procedure notes that
382 -- this bound is uplevel referenced, it can handle references
383 -- to entities (typically _FIRST and _LAST entities), and also
384 -- attribute references of the form T'name (name is typically
385 -- FIRST or LAST) where T is the uplevel referenced bound.
387 ------------------------
388 -- Note_Uplevel_Bound --
389 ------------------------
391 procedure Note_Uplevel_Bound (N : Node_Id) is
392 begin
393 -- Entity name case
395 if Is_Entity_Name (N) then
396 if Present (Entity (N)) then
397 Note_Uplevel_Ref
398 (E => Entity (N),
399 Caller => Current_Subprogram,
400 Callee => Enclosing_Subprogram (Entity (N)));
401 end if;
403 -- Attribute case
405 elsif Nkind (N) = N_Attribute_Reference then
406 Note_Uplevel_Bound (Prefix (N));
407 end if;
408 end Note_Uplevel_Bound;
410 -- Start of processing for Check_Static_Type
412 begin
413 -- If already marked static, immediate return
415 if Is_Static_Type (T) then
416 return;
417 end if;
419 -- If the type is at library level, always consider it static,
420 -- since such uplevel references are irrelevant.
422 if Is_Library_Level_Entity (T) then
423 Set_Is_Static_Type (T);
424 return;
425 end if;
427 -- Otherwise figure out what the story is with this type
429 -- For a scalar type, check bounds
431 if Is_Scalar_Type (T) then
433 -- If both bounds static, then this is a static type
435 declare
436 LB : constant Node_Id := Type_Low_Bound (T);
437 UB : constant Node_Id := Type_High_Bound (T);
439 begin
440 if not Is_Static_Expression (LB) then
441 Note_Uplevel_Bound (LB);
442 DT := True;
443 end if;
445 if not Is_Static_Expression (UB) then
446 Note_Uplevel_Bound (UB);
447 DT := True;
448 end if;
449 end;
451 -- For record type, check all components
453 elsif Is_Record_Type (T) then
454 declare
455 C : Entity_Id;
456 begin
457 C := First_Component_Or_Discriminant (T);
458 while Present (C) loop
459 Check_Static_Type (Etype (C), DT);
460 Next_Component_Or_Discriminant (C);
461 end loop;
462 end;
464 -- For array type, check index types and component type
466 elsif Is_Array_Type (T) then
467 declare
468 IX : Node_Id;
469 begin
470 Check_Static_Type (Component_Type (T), DT);
472 IX := First_Index (T);
473 while Present (IX) loop
474 Check_Static_Type (Etype (IX), DT);
475 Next_Index (IX);
476 end loop;
477 end;
479 -- For private type, examine whether full view is static
481 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
482 Check_Static_Type (Full_View (T), DT);
484 if Is_Static_Type (Full_View (T)) then
485 Set_Is_Static_Type (T);
486 end if;
488 -- For now, ignore other types
490 else
491 return;
492 end if;
494 if not DT then
495 Set_Is_Static_Type (T);
496 end if;
497 end Check_Static_Type;
499 ----------------------
500 -- Note_Uplevel_Ref --
501 ----------------------
503 procedure Note_Uplevel_Ref
504 (E : Entity_Id;
505 Caller : Entity_Id;
506 Callee : Entity_Id)
508 begin
509 -- Nothing to do for static type
511 if Is_Static_Type (E) then
512 return;
513 end if;
515 -- Nothing to do if Caller and Callee are the same
517 if Caller = Callee then
518 return;
520 -- Callee may be a function that returns an array, and that has
521 -- been rewritten as a procedure. If caller is that procedure,
522 -- nothing to do either.
524 elsif Ekind (Callee) = E_Function
525 and then Rewritten_For_C (Callee)
526 and then Corresponding_Procedure (Callee) = Caller
527 then
528 return;
529 end if;
531 -- We have a new uplevel referenced entity
533 -- All we do at this stage is to add the uplevel reference to
534 -- the table. It's too early to do anything else, since this
535 -- uplevel reference may come from an unreachable subprogram
536 -- in which case the entry will be deleted.
538 Urefs.Append ((N, E, Caller, Callee));
539 end Note_Uplevel_Ref;
541 -- Start of processing for Visit_Node
543 begin
544 -- Record a call
546 if Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
548 -- We are only interested in direct calls, not indirect calls
549 -- (where Name (N) is an explicit dereference) at least for now!
551 and then Nkind (Name (N)) in N_Has_Entity
552 then
553 Ent := Entity (Name (N));
555 -- We are only interested in calls to subprograms nested
556 -- within Subp. Calls to Subp itself or to subprograms that
557 -- are outside the nested structure do not affect us.
559 if Scope_Within (Ent, Subp) then
561 -- Ignore calls to imported routines
563 if Is_Imported (Ent) then
564 null;
566 -- Here we have a call to keep and analyze
568 else
569 -- Both caller and callee must be subprograms
571 if Is_Subprogram (Ent) then
572 Append_Unique_Call ((N, Current_Subprogram, Ent));
573 end if;
574 end if;
575 end if;
577 -- Record a subprogram. We record a subprogram body that acts as
578 -- a spec. Otherwise we record a subprogram declaration, providing
579 -- that it has a corresponding body we can get hold of. The case
580 -- of no corresponding body being available is ignored for now.
582 elsif Nkind (N) = N_Subprogram_Body then
583 Ent := Unique_Defining_Entity (N);
585 -- Ignore generic subprogram
587 if Is_Generic_Subprogram (Ent) then
588 return Skip;
589 end if;
591 -- Make new entry in subprogram table if not already made
593 declare
594 L : constant Nat := Get_Level (Subp, Ent);
595 begin
596 Subps.Append
597 ((Ent => Ent,
598 Bod => N,
599 Lev => L,
600 Reachable => False,
601 Uplevel_Ref => L,
602 Declares_AREC => False,
603 Uents => No_Elist,
604 Last => 0,
605 ARECnF => Empty,
606 ARECn => Empty,
607 ARECnT => Empty,
608 ARECnPT => Empty,
609 ARECnP => Empty,
610 ARECnU => Empty));
611 Set_Subps_Index (Ent, UI_From_Int (Subps.Last));
612 end;
614 -- We make a recursive call to scan the subprogram body, so
615 -- that we can save and restore Current_Subprogram.
617 declare
618 Save_CS : constant Entity_Id := Current_Subprogram;
619 Decl : Node_Id;
621 begin
622 Current_Subprogram := Ent;
624 -- Scan declarations
626 Decl := First (Declarations (N));
627 while Present (Decl) loop
628 Visit (Decl);
629 Next (Decl);
630 end loop;
632 -- Scan statements
634 Visit (Handled_Statement_Sequence (N));
636 -- Restore current subprogram setting
638 Current_Subprogram := Save_CS;
639 end;
641 -- Now at this level, return skipping the subprogram body
642 -- descendants, since we already took care of them!
644 return Skip;
646 -- Record an uplevel reference
648 elsif Nkind (N) in N_Has_Entity and then Present (Entity (N)) then
649 Ent := Entity (N);
651 -- Only interested in entities declared within our nest
653 if not Is_Library_Level_Entity (Ent)
654 and then Scope_Within_Or_Same (Scope (Ent), Subp)
656 -- Skip entities defined in inlined subprograms
658 and then Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
659 and then
661 -- Constants and variables are interesting
663 (Ekind_In (Ent, E_Constant, E_Variable)
665 -- Formals are interesting, but not if being used as mere
666 -- names of parameters for name notation calls.
668 or else
669 (Is_Formal (Ent)
670 and then not
671 (Nkind (Parent (N)) = N_Parameter_Association
672 and then Selector_Name (Parent (N)) = N))
674 -- Types other than known Is_Static types are interesting
676 or else (Is_Type (Ent)
677 and then not Is_Static_Type (Ent)))
678 then
679 -- Here we have a possible interesting uplevel reference
681 if Is_Type (Ent) then
682 declare
683 DT : Boolean := False;
685 begin
686 Check_Static_Type (Ent, DT);
688 if Is_Static_Type (Ent) then
689 return OK;
690 end if;
691 end;
692 end if;
694 Caller := Current_Subprogram;
695 Callee := Enclosing_Subprogram (Ent);
697 if Callee /= Caller and then not Is_Static_Type (Ent) then
698 Note_Uplevel_Ref (Ent, Caller, Callee);
699 end if;
700 end if;
702 -- If we have a body stub, visit the associated subunit
704 elsif Nkind (N) in N_Body_Stub then
705 Visit (Library_Unit (N));
707 -- Skip generic declarations
709 elsif Nkind (N) in N_Generic_Declaration then
710 return Skip;
712 -- Skip generic package body
714 elsif Nkind (N) = N_Package_Body
715 and then Present (Corresponding_Spec (N))
716 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
717 then
718 return Skip;
719 end if;
721 -- Fall through to continue scanning children of this node
723 return OK;
724 end Visit_Node;
726 -- Start of processing for Build_Tables
728 begin
729 -- Traverse the body to get subprograms, calls and uplevel references
731 Visit (Subp_Body);
732 end Build_Tables;
734 -- Now do the first transitive closure which determines which
735 -- subprograms in the nest are actually reachable.
737 Reachable_Closure : declare
738 Modified : Boolean;
740 begin
741 Subps.Table (Subps_First).Reachable := True;
743 -- We use a simple minded algorithm as follows (obviously this can
744 -- be done more efficiently, using one of the standard algorithms
745 -- for efficient transitive closure computation, but this is simple
746 -- and most likely fast enough that its speed does not matter).
748 -- Repeatedly scan the list of calls. Any time we find a call from
749 -- A to B, where A is reachable, but B is not, then B is reachable,
750 -- and note that we have made a change by setting Modified True. We
751 -- repeat this until we make a pass with no modifications.
753 Outer : loop
754 Modified := False;
755 Inner : for J in Calls.First .. Calls.Last loop
756 declare
757 CTJ : Call_Entry renames Calls.Table (J);
759 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
760 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
762 SUBF : Subp_Entry renames Subps.Table (SINF);
763 SUBT : Subp_Entry renames Subps.Table (SINT);
765 begin
766 if SUBF.Reachable and then not SUBT.Reachable then
767 SUBT.Reachable := True;
768 Modified := True;
769 end if;
770 end;
771 end loop Inner;
773 exit Outer when not Modified;
774 end loop Outer;
775 end Reachable_Closure;
777 -- Remove calls from unreachable subprograms
779 declare
780 New_Index : Nat;
782 begin
783 New_Index := 0;
784 for J in Calls.First .. Calls.Last loop
785 declare
786 CTJ : Call_Entry renames Calls.Table (J);
788 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
789 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
791 SUBF : Subp_Entry renames Subps.Table (SINF);
792 SUBT : Subp_Entry renames Subps.Table (SINT);
794 begin
795 if SUBF.Reachable then
796 pragma Assert (SUBT.Reachable);
797 New_Index := New_Index + 1;
798 Calls.Table (New_Index) := Calls.Table (J);
799 end if;
800 end;
801 end loop;
803 Calls.Set_Last (New_Index);
804 end;
806 -- Remove uplevel references from unreachable subprograms
808 declare
809 New_Index : Nat;
811 begin
812 New_Index := 0;
813 for J in Urefs.First .. Urefs.Last loop
814 declare
815 URJ : Uref_Entry renames Urefs.Table (J);
817 SINF : constant SI_Type := Subp_Index (URJ.Caller);
818 SINT : constant SI_Type := Subp_Index (URJ.Callee);
820 SUBF : Subp_Entry renames Subps.Table (SINF);
821 SUBT : Subp_Entry renames Subps.Table (SINT);
823 S : Entity_Id;
825 begin
826 -- Keep reachable reference
828 if SUBF.Reachable then
829 New_Index := New_Index + 1;
830 Urefs.Table (New_Index) := Urefs.Table (J);
832 -- And since we know we are keeping this one, this is a good
833 -- place to fill in information for a good reference.
835 -- Mark all enclosing subprograms need to declare AREC
837 S := URJ.Caller;
838 loop
839 S := Enclosing_Subprogram (S);
841 -- if we are at the top level, as can happen with
842 -- references to formals in aspects of nested subprogram
843 -- declarations, there are no further subprograms to
844 -- mark as requiring activation records.
846 exit when No (S);
847 Subps.Table (Subp_Index (S)).Declares_AREC := True;
848 exit when S = URJ.Callee;
849 end loop;
851 -- Add to list of uplevel referenced entities for Callee.
852 -- We do not add types to this list, only actual references
853 -- to objects that will be referenced uplevel, and we use
854 -- the flag Is_Uplevel_Referenced_Entity to avoid making
855 -- duplicate entries in the list.
857 if not Is_Uplevel_Referenced_Entity (URJ.Ent) then
858 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
860 if not Is_Type (URJ.Ent) then
861 Append_New_Elmt (URJ.Ent, SUBT.Uents);
862 end if;
863 end if;
865 -- And set uplevel indication for caller
867 if SUBT.Lev < SUBF.Uplevel_Ref then
868 SUBF.Uplevel_Ref := SUBT.Lev;
869 end if;
870 end if;
871 end;
872 end loop;
874 Urefs.Set_Last (New_Index);
875 end;
877 -- Remove unreachable subprograms from Subps table. Note that we do
878 -- this after eliminating entries from the other two tables, since
879 -- those elimination steps depend on referencing the Subps table.
881 declare
882 New_SI : SI_Type;
884 begin
885 New_SI := Subps_First - 1;
886 for J in Subps_First .. Subps.Last loop
887 declare
888 STJ : Subp_Entry renames Subps.Table (J);
889 Spec : Node_Id;
890 Decl : Node_Id;
892 begin
893 -- Subprogram is reachable, copy and reset index
895 if STJ.Reachable then
896 New_SI := New_SI + 1;
897 Subps.Table (New_SI) := STJ;
898 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
900 -- Subprogram is not reachable
902 else
903 -- Clear index, since no longer active
905 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
907 -- Output debug information if -gnatd.3 set
909 if Debug_Flag_Dot_3 then
910 Write_Str ("Eliminate ");
911 Write_Name (Chars (Subps.Table (J).Ent));
912 Write_Str (" at ");
913 Write_Location (Sloc (Subps.Table (J).Ent));
914 Write_Str (" (not referenced)");
915 Write_Eol;
916 end if;
918 -- Rewrite declaration and body to null statements
920 Spec := Corresponding_Spec (STJ.Bod);
922 if Present (Spec) then
923 Decl := Parent (Declaration_Node (Spec));
924 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
925 end if;
927 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
928 end if;
929 end;
930 end loop;
932 Subps.Set_Last (New_SI);
933 end;
935 -- Now it is time for the second transitive closure, which follows calls
936 -- and makes sure that A calls B, and B has uplevel references, then A
937 -- is also marked as having uplevel references.
939 Closure_Uplevel : declare
940 Modified : Boolean;
942 begin
943 -- We use a simple minded algorithm as follows (obviously this can
944 -- be done more efficiently, using one of the standard algorithms
945 -- for efficient transitive closure computation, but this is simple
946 -- and most likely fast enough that its speed does not matter).
948 -- Repeatedly scan the list of calls. Any time we find a call from
949 -- A to B, where B has uplevel references, make sure that A is marked
950 -- as having at least the same level of uplevel referencing.
952 Outer2 : loop
953 Modified := False;
954 Inner2 : for J in Calls.First .. Calls.Last loop
955 declare
956 CTJ : Call_Entry renames Calls.Table (J);
957 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
958 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
959 SUBF : Subp_Entry renames Subps.Table (SINF);
960 SUBT : Subp_Entry renames Subps.Table (SINT);
961 begin
962 if SUBT.Lev > SUBT.Uplevel_Ref
963 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
964 then
965 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
966 Modified := True;
967 end if;
968 end;
969 end loop Inner2;
971 exit Outer2 when not Modified;
972 end loop Outer2;
973 end Closure_Uplevel;
975 -- We have one more step before the tables are complete. An uplevel
976 -- call from subprogram A to subprogram B where subprogram B has uplevel
977 -- references is in effect an uplevel reference, and must arrange for
978 -- the proper activation link to be passed.
980 for J in Calls.First .. Calls.Last loop
981 declare
982 CTJ : Call_Entry renames Calls.Table (J);
984 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
985 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
987 SUBF : Subp_Entry renames Subps.Table (SINF);
988 SUBT : Subp_Entry renames Subps.Table (SINT);
990 A : Entity_Id;
992 begin
993 -- If callee has uplevel references
995 if SUBT.Uplevel_Ref < SUBT.Lev
997 -- And this is an uplevel call
999 and then SUBT.Lev < SUBF.Lev
1000 then
1001 -- We need to arrange for finding the uplink
1003 A := CTJ.Caller;
1004 loop
1005 A := Enclosing_Subprogram (A);
1006 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1007 exit when A = CTJ.Callee;
1009 -- In any case exit when we get to the outer level. This
1010 -- happens in some odd cases with generics (in particular
1011 -- sem_ch3.adb does not compile without this kludge ???).
1013 exit when A = Subp;
1014 end loop;
1015 end if;
1016 end;
1017 end loop;
1019 -- The tables are now complete, so we can record the last index in the
1020 -- Subps table for later reference in Cprint.
1022 Subps.Table (Subps_First).Last := Subps.Last;
1024 -- Next step, create the entities for code we will insert. We do this
1025 -- at the start so that all the entities are defined, regardless of the
1026 -- order in which we do the code insertions.
1028 Create_Entities : for J in Subps_First .. Subps.Last loop
1029 declare
1030 STJ : Subp_Entry renames Subps.Table (J);
1031 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1033 begin
1034 -- First we create the ARECnF entity for the additional formal for
1035 -- all subprograms which need an activation record passed.
1037 if STJ.Uplevel_Ref < STJ.Lev then
1038 STJ.ARECnF :=
1039 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1040 end if;
1042 -- Define the AREC entities for the activation record if needed
1044 if STJ.Declares_AREC then
1045 STJ.ARECn :=
1046 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1047 STJ.ARECnT :=
1048 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1049 STJ.ARECnPT :=
1050 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1051 STJ.ARECnP :=
1052 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1054 -- Define uplink component entity if inner nesting case
1056 if Present (STJ.ARECnF) then
1057 STJ.ARECnU :=
1058 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1059 end if;
1060 end if;
1061 end;
1062 end loop Create_Entities;
1064 -- Loop through subprograms
1066 Subp_Loop : declare
1067 Addr : constant Entity_Id := RTE (RE_Address);
1069 begin
1070 for J in Subps_First .. Subps.Last loop
1071 declare
1072 STJ : Subp_Entry renames Subps.Table (J);
1074 begin
1075 -- First add the extra formal if needed. This applies to all
1076 -- nested subprograms that require an activation record to be
1077 -- passed, as indicated by ARECnF being defined.
1079 if Present (STJ.ARECnF) then
1081 -- Here we need the extra formal. We do the expansion and
1082 -- analysis of this manually, since it is fairly simple,
1083 -- and it is not obvious how we can get what we want if we
1084 -- try to use the normal Analyze circuit.
1086 Add_Extra_Formal : declare
1087 Encl : constant SI_Type := Enclosing_Subp (J);
1088 STJE : Subp_Entry renames Subps.Table (Encl);
1089 -- Index and Subp_Entry for enclosing routine
1091 Form : constant Entity_Id := STJ.ARECnF;
1092 -- The formal to be added. Note that n here is one less
1093 -- than the level of the subprogram itself (STJ.Ent).
1095 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1096 -- S is an N_Function/Procedure_Specification node, and F
1097 -- is the new entity to add to this subprogramn spec as
1098 -- the last Extra_Formal.
1100 ----------------------
1101 -- Add_Form_To_Spec --
1102 ----------------------
1104 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1105 Sub : constant Entity_Id := Defining_Entity (S);
1106 Ent : Entity_Id;
1108 begin
1109 -- Case of at least one Extra_Formal is present, set
1110 -- ARECnF as the new last entry in the list.
1112 if Present (Extra_Formals (Sub)) then
1113 Ent := Extra_Formals (Sub);
1114 while Present (Extra_Formal (Ent)) loop
1115 Ent := Extra_Formal (Ent);
1116 end loop;
1118 Set_Extra_Formal (Ent, F);
1120 -- No Extra formals present
1122 else
1123 Set_Extra_Formals (Sub, F);
1124 Ent := Last_Formal (Sub);
1126 if Present (Ent) then
1127 Set_Extra_Formal (Ent, F);
1128 end if;
1129 end if;
1130 end Add_Form_To_Spec;
1132 -- Start of processing for Add_Extra_Formal
1134 begin
1135 -- Decorate the new formal entity
1137 Set_Scope (Form, STJ.Ent);
1138 Set_Ekind (Form, E_In_Parameter);
1139 Set_Etype (Form, STJE.ARECnPT);
1140 Set_Mechanism (Form, By_Copy);
1141 Set_Never_Set_In_Source (Form, True);
1142 Set_Analyzed (Form, True);
1143 Set_Comes_From_Source (Form, False);
1145 -- Case of only body present
1147 if Acts_As_Spec (STJ.Bod) then
1148 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1150 -- Case of separate spec
1152 else
1153 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1154 end if;
1155 end Add_Extra_Formal;
1156 end if;
1158 -- Processing for subprograms that declare an activation record
1160 if Present (STJ.ARECn) then
1162 -- Local declarations for one such subprogram
1164 declare
1165 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1166 Clist : List_Id;
1167 Comp : Entity_Id;
1169 Decl_ARECnT : Node_Id;
1170 Decl_ARECnPT : Node_Id;
1171 Decl_ARECn : Node_Id;
1172 Decl_ARECnP : Node_Id;
1173 -- Declaration nodes for the AREC entities we build
1175 Decl_Assign : Node_Id;
1176 -- Assigment to set uplink, Empty if none
1178 Decls : List_Id;
1179 -- List of new declarations we create
1181 begin
1182 -- Build list of component declarations for ARECnT
1184 Clist := Empty_List;
1186 -- If we are in a subprogram that has a static link that
1187 -- is passed in (as indicated by ARECnF being defined),
1188 -- then include ARECnU : ARECmPT where ARECmPT comes from
1189 -- the level one higher than the current level, and the
1190 -- entity ARECnPT comes from the enclosing subprogram.
1192 if Present (STJ.ARECnF) then
1193 declare
1194 STJE : Subp_Entry
1195 renames Subps.Table (Enclosing_Subp (J));
1196 begin
1197 Append_To (Clist,
1198 Make_Component_Declaration (Loc,
1199 Defining_Identifier => STJ.ARECnU,
1200 Component_Definition =>
1201 Make_Component_Definition (Loc,
1202 Subtype_Indication =>
1203 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1204 end;
1205 end if;
1207 -- Add components for uplevel referenced entities
1209 if Present (STJ.Uents) then
1210 declare
1211 Elmt : Elmt_Id;
1212 Uent : Entity_Id;
1214 Indx : Nat;
1215 -- 1's origin of index in list of elements. This is
1216 -- used to uniquify names if needed in Upref_Name.
1218 begin
1219 Elmt := First_Elmt (STJ.Uents);
1220 Indx := 0;
1221 while Present (Elmt) loop
1222 Uent := Node (Elmt);
1223 Indx := Indx + 1;
1225 Comp :=
1226 Make_Defining_Identifier (Loc,
1227 Chars => Upref_Name (Uent, Indx, Clist));
1229 Set_Activation_Record_Component
1230 (Uent, Comp);
1232 Append_To (Clist,
1233 Make_Component_Declaration (Loc,
1234 Defining_Identifier => Comp,
1235 Component_Definition =>
1236 Make_Component_Definition (Loc,
1237 Subtype_Indication =>
1238 New_Occurrence_Of (Addr, Loc))));
1240 Next_Elmt (Elmt);
1241 end loop;
1242 end;
1243 end if;
1245 -- Now we can insert the AREC declarations into the body
1247 -- type ARECnT is record .. end record;
1248 -- pragma Suppress_Initialization (ARECnT);
1250 -- Note that we need to set the Suppress_Initialization
1251 -- flag after Decl_ARECnT has been analyzed.
1253 Decl_ARECnT :=
1254 Make_Full_Type_Declaration (Loc,
1255 Defining_Identifier => STJ.ARECnT,
1256 Type_Definition =>
1257 Make_Record_Definition (Loc,
1258 Component_List =>
1259 Make_Component_List (Loc,
1260 Component_Items => Clist)));
1261 Decls := New_List (Decl_ARECnT);
1263 -- type ARECnPT is access all ARECnT;
1265 Decl_ARECnPT :=
1266 Make_Full_Type_Declaration (Loc,
1267 Defining_Identifier => STJ.ARECnPT,
1268 Type_Definition =>
1269 Make_Access_To_Object_Definition (Loc,
1270 All_Present => True,
1271 Subtype_Indication =>
1272 New_Occurrence_Of (STJ.ARECnT, Loc)));
1273 Append_To (Decls, Decl_ARECnPT);
1275 -- ARECn : aliased ARECnT;
1277 Decl_ARECn :=
1278 Make_Object_Declaration (Loc,
1279 Defining_Identifier => STJ.ARECn,
1280 Aliased_Present => True,
1281 Object_Definition =>
1282 New_Occurrence_Of (STJ.ARECnT, Loc));
1283 Append_To (Decls, Decl_ARECn);
1285 -- ARECnP : constant ARECnPT := ARECn'Access;
1287 Decl_ARECnP :=
1288 Make_Object_Declaration (Loc,
1289 Defining_Identifier => STJ.ARECnP,
1290 Constant_Present => True,
1291 Object_Definition =>
1292 New_Occurrence_Of (STJ.ARECnPT, Loc),
1293 Expression =>
1294 Make_Attribute_Reference (Loc,
1295 Prefix =>
1296 New_Occurrence_Of (STJ.ARECn, Loc),
1297 Attribute_Name => Name_Access));
1298 Append_To (Decls, Decl_ARECnP);
1300 -- If we are in a subprogram that has a static link that
1301 -- is passed in (as indicated by ARECnF being defined),
1302 -- then generate ARECn.ARECmU := ARECmF where m is
1303 -- one less than the current level to set the uplink.
1305 if Present (STJ.ARECnF) then
1306 Decl_Assign :=
1307 Make_Assignment_Statement (Loc,
1308 Name =>
1309 Make_Selected_Component (Loc,
1310 Prefix =>
1311 New_Occurrence_Of (STJ.ARECn, Loc),
1312 Selector_Name =>
1313 New_Occurrence_Of (STJ.ARECnU, Loc)),
1314 Expression =>
1315 New_Occurrence_Of (STJ.ARECnF, Loc));
1316 Append_To (Decls, Decl_Assign);
1318 else
1319 Decl_Assign := Empty;
1320 end if;
1322 Prepend_List_To (Declarations (STJ.Bod), Decls);
1324 -- Analyze the newly inserted declarations. Note that we
1325 -- do not need to establish the whole scope stack, since
1326 -- we have already set all entity fields (so there will
1327 -- be no searching of upper scopes to resolve names). But
1328 -- we do set the scope of the current subprogram, so that
1329 -- newly created entities go in the right entity chain.
1331 -- We analyze with all checks suppressed (since we do
1332 -- not expect any exceptions).
1334 Push_Scope (STJ.Ent);
1335 Analyze (Decl_ARECnT, Suppress => All_Checks);
1337 -- Note that we need to call Set_Suppress_Initialization
1338 -- after Decl_ARECnT has been analyzed, but before
1339 -- analyzing Decl_ARECnP so that the flag is properly
1340 -- taking into account.
1342 Set_Suppress_Initialization (STJ.ARECnT);
1344 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1345 Analyze (Decl_ARECn, Suppress => All_Checks);
1346 Analyze (Decl_ARECnP, Suppress => All_Checks);
1348 if Present (Decl_Assign) then
1349 Analyze (Decl_Assign, Suppress => All_Checks);
1350 end if;
1352 Pop_Scope;
1354 -- Next step, for each uplevel referenced entity, add
1355 -- assignment operations to set the component in the
1356 -- activation record.
1358 if Present (STJ.Uents) then
1359 declare
1360 Elmt : Elmt_Id;
1362 begin
1363 Elmt := First_Elmt (STJ.Uents);
1364 while Present (Elmt) loop
1365 declare
1366 Ent : constant Entity_Id := Node (Elmt);
1367 Loc : constant Source_Ptr := Sloc (Ent);
1368 Dec : constant Node_Id :=
1369 Declaration_Node (Ent);
1370 Ins : Node_Id;
1371 Asn : Node_Id;
1373 begin
1374 -- For parameters, we insert the assignment
1375 -- right after the declaration of ARECnP.
1376 -- For all other entities, we insert
1377 -- the assignment immediately after
1378 -- the declaration of the entity.
1380 -- Note: we don't need to mark the entity
1381 -- as being aliased, because the address
1382 -- attribute will mark it as Address_Taken,
1383 -- and that is good enough.
1385 if Is_Formal (Ent) then
1386 Ins := Decl_ARECnP;
1387 else
1388 Ins := Dec;
1389 end if;
1391 -- Build and insert the assignment:
1392 -- ARECn.nam := nam'Address
1394 Asn :=
1395 Make_Assignment_Statement (Loc,
1396 Name =>
1397 Make_Selected_Component (Loc,
1398 Prefix =>
1399 New_Occurrence_Of (STJ.ARECn, Loc),
1400 Selector_Name =>
1401 New_Occurrence_Of
1402 (Activation_Record_Component
1403 (Ent),
1404 Loc)),
1406 Expression =>
1407 Make_Attribute_Reference (Loc,
1408 Prefix =>
1409 New_Occurrence_Of (Ent, Loc),
1410 Attribute_Name => Name_Address));
1412 Insert_After (Ins, Asn);
1414 -- Analyze the assignment statement. We do
1415 -- not need to establish the relevant scope
1416 -- stack entries here, because we have
1417 -- already set the correct entity references,
1418 -- so no name resolution is required, and no
1419 -- new entities are created, so we don't even
1420 -- need to set the current scope.
1422 -- We analyze with all checks suppressed
1423 -- (since we do not expect any exceptions).
1425 Analyze (Asn, Suppress => All_Checks);
1426 end;
1428 Next_Elmt (Elmt);
1429 end loop;
1430 end;
1431 end if;
1432 end;
1433 end if;
1434 end;
1435 end loop;
1436 end Subp_Loop;
1438 -- Next step, process uplevel references. This has to be done in a
1439 -- separate pass, after completing the processing in Sub_Loop because we
1440 -- need all the AREC declarations generated, inserted, and analyzed so
1441 -- that the uplevel references can be successfully analyzed.
1443 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
1444 declare
1445 UPJ : Uref_Entry renames Urefs.Table (J);
1447 begin
1448 -- Ignore type references, these are implicit references that do
1449 -- not need rewriting (e.g. the appearence in a conversion).
1451 if Is_Type (UPJ.Ent) then
1452 goto Continue;
1453 end if;
1455 -- Also ignore uplevel references to bounds of types that come
1456 -- from the original type reference.
1458 if Is_Entity_Name (UPJ.Ref)
1459 and then Present (Entity (UPJ.Ref))
1460 and then Is_Type (Entity (UPJ.Ref))
1461 then
1462 goto Continue;
1463 end if;
1465 -- Rewrite one reference
1467 Rewrite_One_Ref : declare
1468 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
1469 -- Source location for the reference
1471 Typ : constant Entity_Id := Etype (UPJ.Ent);
1472 -- The type of the referenced entity
1474 Atyp : constant Entity_Id := Get_Actual_Subtype (UPJ.Ref);
1475 -- The actual subtype of the reference
1477 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
1478 -- Subp_Index for caller containing reference
1480 STJR : Subp_Entry renames Subps.Table (RS_Caller);
1481 -- Subp_Entry for subprogram containing reference
1483 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
1484 -- Subp_Index for subprogram containing referenced entity
1486 STJE : Subp_Entry renames Subps.Table (RS_Callee);
1487 -- Subp_Entry for subprogram containing referenced entity
1489 Pfx : Node_Id;
1490 Comp : Entity_Id;
1491 SI : SI_Type;
1493 begin
1494 -- Ignore if no ARECnF entity for enclosing subprogram which
1495 -- probably happens as a result of not properly treating
1496 -- instance bodies. To be examined ???
1498 -- If this test is omitted, then the compilation of freeze.adb
1499 -- and inline.adb fail in unnesting mode.
1501 if No (STJR.ARECnF) then
1502 goto Continue;
1503 end if;
1505 -- Push the current scope, so that the pointer type Tnn, and
1506 -- any subsidiary entities resulting from the analysis of the
1507 -- rewritten reference, go in the right entity chain.
1509 Push_Scope (STJR.Ent);
1511 -- Now we need to rewrite the reference. We have a reference
1512 -- from level STJR.Lev to level STJE.Lev. The general form of
1513 -- the rewritten reference for entity X is:
1515 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECm.X)
1517 -- where a,b,c,d .. m =
1518 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
1520 pragma Assert (STJR.Lev > STJE.Lev);
1522 -- Compute the prefix of X. Here are examples to make things
1523 -- clear (with parens to show groupings, the prefix is
1524 -- everything except the .X at the end).
1526 -- level 2 to level 1
1528 -- AREC1F.X
1530 -- level 3 to level 1
1532 -- (AREC2F.AREC1U).X
1534 -- level 4 to level 1
1536 -- ((AREC3F.AREC2U).AREC1U).X
1538 -- level 6 to level 2
1540 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
1542 -- In the above, ARECnF and ARECnU are pointers, so there are
1543 -- explicit dereferences required for these occurrences.
1545 Pfx :=
1546 Make_Explicit_Dereference (Loc,
1547 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
1548 SI := RS_Caller;
1549 for L in STJE.Lev .. STJR.Lev - 2 loop
1550 SI := Enclosing_Subp (SI);
1551 Pfx :=
1552 Make_Explicit_Dereference (Loc,
1553 Prefix =>
1554 Make_Selected_Component (Loc,
1555 Prefix => Pfx,
1556 Selector_Name =>
1557 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
1558 end loop;
1560 -- Get activation record component (must exist)
1562 Comp := Activation_Record_Component (UPJ.Ent);
1563 pragma Assert (Present (Comp));
1565 -- Do the replacement
1567 Rewrite (UPJ.Ref,
1568 Make_Attribute_Reference (Loc,
1569 Prefix => New_Occurrence_Of (Atyp, Loc),
1570 Attribute_Name => Name_Deref,
1571 Expressions => New_List (
1572 Make_Selected_Component (Loc,
1573 Prefix => Pfx,
1574 Selector_Name =>
1575 New_Occurrence_Of (Comp, Loc)))));
1577 -- Analyze and resolve the new expression. We do not need to
1578 -- establish the relevant scope stack entries here, because we
1579 -- have already set all the correct entity references, so no
1580 -- name resolution is needed. We have already set the current
1581 -- scope, so that any new entities created will be in the right
1582 -- scope.
1584 -- We analyze with all checks suppressed (since we do not
1585 -- expect any exceptions)
1587 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
1588 Pop_Scope;
1589 end Rewrite_One_Ref;
1590 end;
1592 <<Continue>>
1593 null;
1594 end loop Uplev_Refs;
1596 -- Finally, loop through all calls adding extra actual for the
1597 -- activation record where it is required.
1599 Adjust_Calls : for J in Calls.First .. Calls.Last loop
1601 -- Process a single call, we are only interested in a call to a
1602 -- subprogram that actually needs a pointer to an activation record,
1603 -- as indicated by the ARECnF entity being set. This excludes the
1604 -- top level subprogram, and any subprogram not having uplevel refs.
1606 Adjust_One_Call : declare
1607 CTJ : Call_Entry renames Calls.Table (J);
1608 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
1609 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
1611 Loc : constant Source_Ptr := Sloc (CTJ.N);
1613 Extra : Node_Id;
1614 ExtraP : Node_Id;
1615 SubX : SI_Type;
1616 Act : Node_Id;
1618 begin
1619 if Present (STT.ARECnF) then
1621 -- CTJ.N is a call to a subprogram which may require a pointer
1622 -- to an activation record. The subprogram containing the call
1623 -- is CTJ.From and the subprogram being called is CTJ.To, so we
1624 -- have a call from level STF.Lev to level STT.Lev.
1626 -- There are three possibilities:
1628 -- For a call to the same level, we just pass the activation
1629 -- record passed to the calling subprogram.
1631 if STF.Lev = STT.Lev then
1632 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1634 -- For a call that goes down a level, we pass a pointer to the
1635 -- activation record constructed within the caller (which may
1636 -- be the outer-level subprogram, but also may be a more deeply
1637 -- nested caller).
1639 elsif STT.Lev = STF.Lev + 1 then
1640 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
1642 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
1643 -- since it is not possible to do a downcall of more than
1644 -- one level.
1646 -- For a call from level STF.Lev to level STT.Lev, we
1647 -- have to find the activation record needed by the
1648 -- callee. This is as follows:
1650 -- ARECaF.ARECbU.ARECcU....ARECm
1652 -- where a,b,c .. m =
1653 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
1655 else
1656 pragma Assert (STT.Lev < STF.Lev);
1658 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1659 SubX := Subp_Index (CTJ.Caller);
1660 for K in reverse STT.Lev .. STF.Lev - 1 loop
1661 SubX := Enclosing_Subp (SubX);
1662 Extra :=
1663 Make_Selected_Component (Loc,
1664 Prefix => Extra,
1665 Selector_Name =>
1666 New_Occurrence_Of
1667 (Subps.Table (SubX).ARECnU, Loc));
1668 end loop;
1669 end if;
1671 -- Extra is the additional parameter to be added. Build a
1672 -- parameter association that we can append to the actuals.
1674 ExtraP :=
1675 Make_Parameter_Association (Loc,
1676 Selector_Name =>
1677 New_Occurrence_Of (STT.ARECnF, Loc),
1678 Explicit_Actual_Parameter => Extra);
1680 if No (Parameter_Associations (CTJ.N)) then
1681 Set_Parameter_Associations (CTJ.N, Empty_List);
1682 end if;
1684 Append (ExtraP, Parameter_Associations (CTJ.N));
1686 -- We need to deal with the actual parameter chain as well. The
1687 -- newly added parameter is always the last actual.
1689 Act := First_Named_Actual (CTJ.N);
1691 if No (Act) then
1692 Set_First_Named_Actual (CTJ.N, Extra);
1694 -- Here we must follow the chain and append the new entry
1696 else
1697 loop
1698 declare
1699 PAN : Node_Id;
1700 NNA : Node_Id;
1702 begin
1703 PAN := Parent (Act);
1704 pragma Assert (Nkind (PAN) = N_Parameter_Association);
1705 NNA := Next_Named_Actual (PAN);
1707 if No (NNA) then
1708 Set_Next_Named_Actual (PAN, Extra);
1709 exit;
1710 end if;
1712 Act := NNA;
1713 end;
1714 end loop;
1715 end if;
1717 -- Analyze and resolve the new actual. We do not need to
1718 -- establish the relevant scope stack entries here, because
1719 -- we have already set all the correct entity references, so
1720 -- no name resolution is needed.
1722 -- We analyze with all checks suppressed (since we do not
1723 -- expect any exceptions, and also we temporarily turn off
1724 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
1725 -- references (not needed at this stage, and in fact causes
1726 -- a bit of recursive chaos).
1728 Opt.Unnest_Subprogram_Mode := False;
1729 Analyze_And_Resolve
1730 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
1731 Opt.Unnest_Subprogram_Mode := True;
1732 end if;
1733 end Adjust_One_Call;
1734 end loop Adjust_Calls;
1736 return;
1737 end Unnest_Subprogram;
1739 ------------------------
1740 -- Unnest_Subprograms --
1741 ------------------------
1743 procedure Unnest_Subprograms (N : Node_Id) is
1744 function Search_Subprograms (N : Node_Id) return Traverse_Result;
1745 -- Tree visitor that search for outer level procedures with nested
1746 -- subprograms and invokes Unnest_Subprogram()
1748 ------------------------
1749 -- Search_Subprograms --
1750 ------------------------
1752 function Search_Subprograms (N : Node_Id) return Traverse_Result is
1753 begin
1754 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
1755 declare
1756 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
1758 begin
1759 -- We are only interested in subprograms (not generic
1760 -- subprograms), that have nested subprograms.
1762 if Is_Subprogram (Spec_Id)
1763 and then Has_Nested_Subprogram (Spec_Id)
1764 and then Is_Library_Level_Entity (Spec_Id)
1765 then
1766 Unnest_Subprogram (Spec_Id, N);
1767 end if;
1768 end;
1769 end if;
1771 return OK;
1772 end Search_Subprograms;
1774 ---------------
1775 -- Do_Search --
1776 ---------------
1778 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
1779 -- Subtree visitor instantiation
1781 -- Start of processing for Unnest_Subprograms
1783 begin
1784 if not Opt.Unnest_Subprogram_Mode then
1785 return;
1786 end if;
1788 Do_Search (N);
1789 end Unnest_Subprograms;
1791 end Exp_Unst;