Improve fstack_protector effective target
[official-gcc.git] / gcc / ada / exp_unst.adb
blob9e5465bc6de11ad6f1b14a44c7447898ac201bd2
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 'Access as a (potential) call
579 elsif Nkind (N) = N_Attribute_Reference then
580 declare
581 Attr : constant Attribute_Id :=
582 Get_Attribute_Id (Attribute_Name (N));
583 begin
584 case Attr is
585 when Attribute_Access
586 | Attribute_Unchecked_Access
587 | Attribute_Unrestricted_Access
589 Ent := Entity (Prefix (N));
591 -- We are only interested in calls to subprograms
592 -- nested within Subp.
594 if Scope_Within (Ent, Subp) then
595 if Is_Imported (Ent) then
596 null;
598 elsif Is_Subprogram (Ent) then
599 Append_Unique_Call
600 ((N, Current_Subprogram, Ent));
601 end if;
602 end if;
604 when others =>
605 null;
606 end case;
607 end;
609 -- Record a subprogram. We record a subprogram body that acts as
610 -- a spec. Otherwise we record a subprogram declaration, providing
611 -- that it has a corresponding body we can get hold of. The case
612 -- of no corresponding body being available is ignored for now.
614 elsif Nkind (N) = N_Subprogram_Body then
615 Ent := Unique_Defining_Entity (N);
617 -- Ignore generic subprogram
619 if Is_Generic_Subprogram (Ent) then
620 return Skip;
621 end if;
623 -- Make new entry in subprogram table if not already made
625 declare
626 L : constant Nat := Get_Level (Subp, Ent);
627 begin
628 Subps.Append
629 ((Ent => Ent,
630 Bod => N,
631 Lev => L,
632 Reachable => False,
633 Uplevel_Ref => L,
634 Declares_AREC => False,
635 Uents => No_Elist,
636 Last => 0,
637 ARECnF => Empty,
638 ARECn => Empty,
639 ARECnT => Empty,
640 ARECnPT => Empty,
641 ARECnP => Empty,
642 ARECnU => Empty));
643 Set_Subps_Index (Ent, UI_From_Int (Subps.Last));
644 end;
646 -- We make a recursive call to scan the subprogram body, so
647 -- that we can save and restore Current_Subprogram.
649 declare
650 Save_CS : constant Entity_Id := Current_Subprogram;
651 Decl : Node_Id;
653 begin
654 Current_Subprogram := Ent;
656 -- Scan declarations
658 Decl := First (Declarations (N));
659 while Present (Decl) loop
660 Visit (Decl);
661 Next (Decl);
662 end loop;
664 -- Scan statements
666 Visit (Handled_Statement_Sequence (N));
668 -- Restore current subprogram setting
670 Current_Subprogram := Save_CS;
671 end;
673 -- Now at this level, return skipping the subprogram body
674 -- descendants, since we already took care of them!
676 return Skip;
678 -- Record an uplevel reference
680 elsif Nkind (N) in N_Has_Entity and then Present (Entity (N)) then
681 Ent := Entity (N);
683 -- Only interested in entities declared within our nest
685 if not Is_Library_Level_Entity (Ent)
686 and then Scope_Within_Or_Same (Scope (Ent), Subp)
688 -- Skip entities defined in inlined subprograms
690 and then Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
691 and then
693 -- Constants and variables are interesting
695 (Ekind_In (Ent, E_Constant, E_Variable)
697 -- Formals are interesting, but not if being used as mere
698 -- names of parameters for name notation calls.
700 or else
701 (Is_Formal (Ent)
702 and then not
703 (Nkind (Parent (N)) = N_Parameter_Association
704 and then Selector_Name (Parent (N)) = N))
706 -- Types other than known Is_Static types are interesting
708 or else (Is_Type (Ent)
709 and then not Is_Static_Type (Ent)))
710 then
711 -- Here we have a possible interesting uplevel reference
713 if Is_Type (Ent) then
714 declare
715 DT : Boolean := False;
717 begin
718 Check_Static_Type (Ent, DT);
720 if Is_Static_Type (Ent) then
721 return OK;
722 end if;
723 end;
724 end if;
726 Caller := Current_Subprogram;
727 Callee := Enclosing_Subprogram (Ent);
729 if Callee /= Caller and then not Is_Static_Type (Ent) then
730 Note_Uplevel_Ref (Ent, Caller, Callee);
731 end if;
732 end if;
734 -- If we have a body stub, visit the associated subunit
736 elsif Nkind (N) in N_Body_Stub then
737 Visit (Library_Unit (N));
739 -- Skip generic declarations
741 elsif Nkind (N) in N_Generic_Declaration then
742 return Skip;
744 -- Skip generic package body
746 elsif Nkind (N) = N_Package_Body
747 and then Present (Corresponding_Spec (N))
748 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
749 then
750 return Skip;
751 end if;
753 -- Fall through to continue scanning children of this node
755 return OK;
756 end Visit_Node;
758 -- Start of processing for Build_Tables
760 begin
761 -- Traverse the body to get subprograms, calls and uplevel references
763 Visit (Subp_Body);
764 end Build_Tables;
766 -- Now do the first transitive closure which determines which
767 -- subprograms in the nest are actually reachable.
769 Reachable_Closure : declare
770 Modified : Boolean;
772 begin
773 Subps.Table (Subps_First).Reachable := True;
775 -- We use a simple minded algorithm as follows (obviously this can
776 -- be done more efficiently, using one of the standard algorithms
777 -- for efficient transitive closure computation, but this is simple
778 -- and most likely fast enough that its speed does not matter).
780 -- Repeatedly scan the list of calls. Any time we find a call from
781 -- A to B, where A is reachable, but B is not, then B is reachable,
782 -- and note that we have made a change by setting Modified True. We
783 -- repeat this until we make a pass with no modifications.
785 Outer : loop
786 Modified := False;
787 Inner : for J in Calls.First .. Calls.Last loop
788 declare
789 CTJ : Call_Entry renames Calls.Table (J);
791 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
792 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
794 SUBF : Subp_Entry renames Subps.Table (SINF);
795 SUBT : Subp_Entry renames Subps.Table (SINT);
797 begin
798 if SUBF.Reachable and then not SUBT.Reachable then
799 SUBT.Reachable := True;
800 Modified := True;
801 end if;
802 end;
803 end loop Inner;
805 exit Outer when not Modified;
806 end loop Outer;
807 end Reachable_Closure;
809 -- Remove calls from unreachable subprograms
811 declare
812 New_Index : Nat;
814 begin
815 New_Index := 0;
816 for J in Calls.First .. Calls.Last loop
817 declare
818 CTJ : Call_Entry renames Calls.Table (J);
820 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
821 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
823 SUBF : Subp_Entry renames Subps.Table (SINF);
824 SUBT : Subp_Entry renames Subps.Table (SINT);
826 begin
827 if SUBF.Reachable then
828 pragma Assert (SUBT.Reachable);
829 New_Index := New_Index + 1;
830 Calls.Table (New_Index) := Calls.Table (J);
831 end if;
832 end;
833 end loop;
835 Calls.Set_Last (New_Index);
836 end;
838 -- Remove uplevel references from unreachable subprograms
840 declare
841 New_Index : Nat;
843 begin
844 New_Index := 0;
845 for J in Urefs.First .. Urefs.Last loop
846 declare
847 URJ : Uref_Entry renames Urefs.Table (J);
849 SINF : constant SI_Type := Subp_Index (URJ.Caller);
850 SINT : constant SI_Type := Subp_Index (URJ.Callee);
852 SUBF : Subp_Entry renames Subps.Table (SINF);
853 SUBT : Subp_Entry renames Subps.Table (SINT);
855 S : Entity_Id;
857 begin
858 -- Keep reachable reference
860 if SUBF.Reachable then
861 New_Index := New_Index + 1;
862 Urefs.Table (New_Index) := Urefs.Table (J);
864 -- And since we know we are keeping this one, this is a good
865 -- place to fill in information for a good reference.
867 -- Mark all enclosing subprograms need to declare AREC
869 S := URJ.Caller;
870 loop
871 S := Enclosing_Subprogram (S);
873 -- if we are at the top level, as can happen with
874 -- references to formals in aspects of nested subprogram
875 -- declarations, there are no further subprograms to
876 -- mark as requiring activation records.
878 exit when No (S);
879 Subps.Table (Subp_Index (S)).Declares_AREC := True;
880 exit when S = URJ.Callee;
881 end loop;
883 -- Add to list of uplevel referenced entities for Callee.
884 -- We do not add types to this list, only actual references
885 -- to objects that will be referenced uplevel, and we use
886 -- the flag Is_Uplevel_Referenced_Entity to avoid making
887 -- duplicate entries in the list.
889 if not Is_Uplevel_Referenced_Entity (URJ.Ent) then
890 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
892 if not Is_Type (URJ.Ent) then
893 Append_New_Elmt (URJ.Ent, SUBT.Uents);
894 end if;
895 end if;
897 -- And set uplevel indication for caller
899 if SUBT.Lev < SUBF.Uplevel_Ref then
900 SUBF.Uplevel_Ref := SUBT.Lev;
901 end if;
902 end if;
903 end;
904 end loop;
906 Urefs.Set_Last (New_Index);
907 end;
909 -- Remove unreachable subprograms from Subps table. Note that we do
910 -- this after eliminating entries from the other two tables, since
911 -- those elimination steps depend on referencing the Subps table.
913 declare
914 New_SI : SI_Type;
916 begin
917 New_SI := Subps_First - 1;
918 for J in Subps_First .. Subps.Last loop
919 declare
920 STJ : Subp_Entry renames Subps.Table (J);
921 Spec : Node_Id;
922 Decl : Node_Id;
924 begin
925 -- Subprogram is reachable, copy and reset index
927 if STJ.Reachable then
928 New_SI := New_SI + 1;
929 Subps.Table (New_SI) := STJ;
930 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
932 -- Subprogram is not reachable
934 else
935 -- Clear index, since no longer active
937 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
939 -- Output debug information if -gnatd.3 set
941 if Debug_Flag_Dot_3 then
942 Write_Str ("Eliminate ");
943 Write_Name (Chars (Subps.Table (J).Ent));
944 Write_Str (" at ");
945 Write_Location (Sloc (Subps.Table (J).Ent));
946 Write_Str (" (not referenced)");
947 Write_Eol;
948 end if;
950 -- Rewrite declaration and body to null statements
952 Spec := Corresponding_Spec (STJ.Bod);
954 if Present (Spec) then
955 Decl := Parent (Declaration_Node (Spec));
956 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
957 end if;
959 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
960 end if;
961 end;
962 end loop;
964 Subps.Set_Last (New_SI);
965 end;
967 -- Now it is time for the second transitive closure, which follows calls
968 -- and makes sure that A calls B, and B has uplevel references, then A
969 -- is also marked as having uplevel references.
971 Closure_Uplevel : declare
972 Modified : Boolean;
974 begin
975 -- We use a simple minded algorithm as follows (obviously this can
976 -- be done more efficiently, using one of the standard algorithms
977 -- for efficient transitive closure computation, but this is simple
978 -- and most likely fast enough that its speed does not matter).
980 -- Repeatedly scan the list of calls. Any time we find a call from
981 -- A to B, where B has uplevel references, make sure that A is marked
982 -- as having at least the same level of uplevel referencing.
984 Outer2 : loop
985 Modified := False;
986 Inner2 : for J in Calls.First .. Calls.Last loop
987 declare
988 CTJ : Call_Entry renames Calls.Table (J);
989 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
990 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
991 SUBF : Subp_Entry renames Subps.Table (SINF);
992 SUBT : Subp_Entry renames Subps.Table (SINT);
993 begin
994 if SUBT.Lev > SUBT.Uplevel_Ref
995 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
996 then
997 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
998 Modified := True;
999 end if;
1000 end;
1001 end loop Inner2;
1003 exit Outer2 when not Modified;
1004 end loop Outer2;
1005 end Closure_Uplevel;
1007 -- We have one more step before the tables are complete. An uplevel
1008 -- call from subprogram A to subprogram B where subprogram B has uplevel
1009 -- references is in effect an uplevel reference, and must arrange for
1010 -- the proper activation link to be passed.
1012 for J in Calls.First .. Calls.Last loop
1013 declare
1014 CTJ : Call_Entry renames Calls.Table (J);
1016 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1017 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1019 SUBF : Subp_Entry renames Subps.Table (SINF);
1020 SUBT : Subp_Entry renames Subps.Table (SINT);
1022 A : Entity_Id;
1024 begin
1025 -- If callee has uplevel references
1027 if SUBT.Uplevel_Ref < SUBT.Lev
1029 -- And this is an uplevel call
1031 and then SUBT.Lev < SUBF.Lev
1032 then
1033 -- We need to arrange for finding the uplink
1035 A := CTJ.Caller;
1036 loop
1037 A := Enclosing_Subprogram (A);
1038 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1039 exit when A = CTJ.Callee;
1041 -- In any case exit when we get to the outer level. This
1042 -- happens in some odd cases with generics (in particular
1043 -- sem_ch3.adb does not compile without this kludge ???).
1045 exit when A = Subp;
1046 end loop;
1047 end if;
1048 end;
1049 end loop;
1051 -- The tables are now complete, so we can record the last index in the
1052 -- Subps table for later reference in Cprint.
1054 Subps.Table (Subps_First).Last := Subps.Last;
1056 -- Next step, create the entities for code we will insert. We do this
1057 -- at the start so that all the entities are defined, regardless of the
1058 -- order in which we do the code insertions.
1060 Create_Entities : for J in Subps_First .. Subps.Last loop
1061 declare
1062 STJ : Subp_Entry renames Subps.Table (J);
1063 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1065 begin
1066 -- First we create the ARECnF entity for the additional formal for
1067 -- all subprograms which need an activation record passed.
1069 if STJ.Uplevel_Ref < STJ.Lev then
1070 STJ.ARECnF :=
1071 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1072 end if;
1074 -- Define the AREC entities for the activation record if needed
1076 if STJ.Declares_AREC then
1077 STJ.ARECn :=
1078 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1079 STJ.ARECnT :=
1080 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1081 STJ.ARECnPT :=
1082 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1083 STJ.ARECnP :=
1084 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1086 -- Define uplink component entity if inner nesting case
1088 if Present (STJ.ARECnF) then
1089 STJ.ARECnU :=
1090 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1091 end if;
1092 end if;
1093 end;
1094 end loop Create_Entities;
1096 -- Loop through subprograms
1098 Subp_Loop : declare
1099 Addr : constant Entity_Id := RTE (RE_Address);
1101 begin
1102 for J in Subps_First .. Subps.Last loop
1103 declare
1104 STJ : Subp_Entry renames Subps.Table (J);
1106 begin
1107 -- First add the extra formal if needed. This applies to all
1108 -- nested subprograms that require an activation record to be
1109 -- passed, as indicated by ARECnF being defined.
1111 if Present (STJ.ARECnF) then
1113 -- Here we need the extra formal. We do the expansion and
1114 -- analysis of this manually, since it is fairly simple,
1115 -- and it is not obvious how we can get what we want if we
1116 -- try to use the normal Analyze circuit.
1118 Add_Extra_Formal : declare
1119 Encl : constant SI_Type := Enclosing_Subp (J);
1120 STJE : Subp_Entry renames Subps.Table (Encl);
1121 -- Index and Subp_Entry for enclosing routine
1123 Form : constant Entity_Id := STJ.ARECnF;
1124 -- The formal to be added. Note that n here is one less
1125 -- than the level of the subprogram itself (STJ.Ent).
1127 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1128 -- S is an N_Function/Procedure_Specification node, and F
1129 -- is the new entity to add to this subprogramn spec as
1130 -- the last Extra_Formal.
1132 ----------------------
1133 -- Add_Form_To_Spec --
1134 ----------------------
1136 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1137 Sub : constant Entity_Id := Defining_Entity (S);
1138 Ent : Entity_Id;
1140 begin
1141 -- Case of at least one Extra_Formal is present, set
1142 -- ARECnF as the new last entry in the list.
1144 if Present (Extra_Formals (Sub)) then
1145 Ent := Extra_Formals (Sub);
1146 while Present (Extra_Formal (Ent)) loop
1147 Ent := Extra_Formal (Ent);
1148 end loop;
1150 Set_Extra_Formal (Ent, F);
1152 -- No Extra formals present
1154 else
1155 Set_Extra_Formals (Sub, F);
1156 Ent := Last_Formal (Sub);
1158 if Present (Ent) then
1159 Set_Extra_Formal (Ent, F);
1160 end if;
1161 end if;
1162 end Add_Form_To_Spec;
1164 -- Start of processing for Add_Extra_Formal
1166 begin
1167 -- Decorate the new formal entity
1169 Set_Scope (Form, STJ.Ent);
1170 Set_Ekind (Form, E_In_Parameter);
1171 Set_Etype (Form, STJE.ARECnPT);
1172 Set_Mechanism (Form, By_Copy);
1173 Set_Never_Set_In_Source (Form, True);
1174 Set_Analyzed (Form, True);
1175 Set_Comes_From_Source (Form, False);
1177 -- Case of only body present
1179 if Acts_As_Spec (STJ.Bod) then
1180 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1182 -- Case of separate spec
1184 else
1185 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1186 end if;
1187 end Add_Extra_Formal;
1188 end if;
1190 -- Processing for subprograms that declare an activation record
1192 if Present (STJ.ARECn) then
1194 -- Local declarations for one such subprogram
1196 declare
1197 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1198 Clist : List_Id;
1199 Comp : Entity_Id;
1201 Decl_ARECnT : Node_Id;
1202 Decl_ARECnPT : Node_Id;
1203 Decl_ARECn : Node_Id;
1204 Decl_ARECnP : Node_Id;
1205 -- Declaration nodes for the AREC entities we build
1207 Decl_Assign : Node_Id;
1208 -- Assigment to set uplink, Empty if none
1210 Decls : List_Id;
1211 -- List of new declarations we create
1213 begin
1214 -- Build list of component declarations for ARECnT
1216 Clist := Empty_List;
1218 -- If we are in a subprogram that has a static link that
1219 -- is passed in (as indicated by ARECnF being defined),
1220 -- then include ARECnU : ARECmPT where ARECmPT comes from
1221 -- the level one higher than the current level, and the
1222 -- entity ARECnPT comes from the enclosing subprogram.
1224 if Present (STJ.ARECnF) then
1225 declare
1226 STJE : Subp_Entry
1227 renames Subps.Table (Enclosing_Subp (J));
1228 begin
1229 Append_To (Clist,
1230 Make_Component_Declaration (Loc,
1231 Defining_Identifier => STJ.ARECnU,
1232 Component_Definition =>
1233 Make_Component_Definition (Loc,
1234 Subtype_Indication =>
1235 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1236 end;
1237 end if;
1239 -- Add components for uplevel referenced entities
1241 if Present (STJ.Uents) then
1242 declare
1243 Elmt : Elmt_Id;
1244 Uent : Entity_Id;
1246 Indx : Nat;
1247 -- 1's origin of index in list of elements. This is
1248 -- used to uniquify names if needed in Upref_Name.
1250 begin
1251 Elmt := First_Elmt (STJ.Uents);
1252 Indx := 0;
1253 while Present (Elmt) loop
1254 Uent := Node (Elmt);
1255 Indx := Indx + 1;
1257 Comp :=
1258 Make_Defining_Identifier (Loc,
1259 Chars => Upref_Name (Uent, Indx, Clist));
1261 Set_Activation_Record_Component
1262 (Uent, Comp);
1264 Append_To (Clist,
1265 Make_Component_Declaration (Loc,
1266 Defining_Identifier => Comp,
1267 Component_Definition =>
1268 Make_Component_Definition (Loc,
1269 Subtype_Indication =>
1270 New_Occurrence_Of (Addr, Loc))));
1272 Next_Elmt (Elmt);
1273 end loop;
1274 end;
1275 end if;
1277 -- Now we can insert the AREC declarations into the body
1279 -- type ARECnT is record .. end record;
1280 -- pragma Suppress_Initialization (ARECnT);
1282 -- Note that we need to set the Suppress_Initialization
1283 -- flag after Decl_ARECnT has been analyzed.
1285 Decl_ARECnT :=
1286 Make_Full_Type_Declaration (Loc,
1287 Defining_Identifier => STJ.ARECnT,
1288 Type_Definition =>
1289 Make_Record_Definition (Loc,
1290 Component_List =>
1291 Make_Component_List (Loc,
1292 Component_Items => Clist)));
1293 Decls := New_List (Decl_ARECnT);
1295 -- type ARECnPT is access all ARECnT;
1297 Decl_ARECnPT :=
1298 Make_Full_Type_Declaration (Loc,
1299 Defining_Identifier => STJ.ARECnPT,
1300 Type_Definition =>
1301 Make_Access_To_Object_Definition (Loc,
1302 All_Present => True,
1303 Subtype_Indication =>
1304 New_Occurrence_Of (STJ.ARECnT, Loc)));
1305 Append_To (Decls, Decl_ARECnPT);
1307 -- ARECn : aliased ARECnT;
1309 Decl_ARECn :=
1310 Make_Object_Declaration (Loc,
1311 Defining_Identifier => STJ.ARECn,
1312 Aliased_Present => True,
1313 Object_Definition =>
1314 New_Occurrence_Of (STJ.ARECnT, Loc));
1315 Append_To (Decls, Decl_ARECn);
1317 -- ARECnP : constant ARECnPT := ARECn'Access;
1319 Decl_ARECnP :=
1320 Make_Object_Declaration (Loc,
1321 Defining_Identifier => STJ.ARECnP,
1322 Constant_Present => True,
1323 Object_Definition =>
1324 New_Occurrence_Of (STJ.ARECnPT, Loc),
1325 Expression =>
1326 Make_Attribute_Reference (Loc,
1327 Prefix =>
1328 New_Occurrence_Of (STJ.ARECn, Loc),
1329 Attribute_Name => Name_Access));
1330 Append_To (Decls, Decl_ARECnP);
1332 -- If we are in a subprogram that has a static link that
1333 -- is passed in (as indicated by ARECnF being defined),
1334 -- then generate ARECn.ARECmU := ARECmF where m is
1335 -- one less than the current level to set the uplink.
1337 if Present (STJ.ARECnF) then
1338 Decl_Assign :=
1339 Make_Assignment_Statement (Loc,
1340 Name =>
1341 Make_Selected_Component (Loc,
1342 Prefix =>
1343 New_Occurrence_Of (STJ.ARECn, Loc),
1344 Selector_Name =>
1345 New_Occurrence_Of (STJ.ARECnU, Loc)),
1346 Expression =>
1347 New_Occurrence_Of (STJ.ARECnF, Loc));
1348 Append_To (Decls, Decl_Assign);
1350 else
1351 Decl_Assign := Empty;
1352 end if;
1354 Prepend_List_To (Declarations (STJ.Bod), Decls);
1356 -- Analyze the newly inserted declarations. Note that we
1357 -- do not need to establish the whole scope stack, since
1358 -- we have already set all entity fields (so there will
1359 -- be no searching of upper scopes to resolve names). But
1360 -- we do set the scope of the current subprogram, so that
1361 -- newly created entities go in the right entity chain.
1363 -- We analyze with all checks suppressed (since we do
1364 -- not expect any exceptions).
1366 Push_Scope (STJ.Ent);
1367 Analyze (Decl_ARECnT, Suppress => All_Checks);
1369 -- Note that we need to call Set_Suppress_Initialization
1370 -- after Decl_ARECnT has been analyzed, but before
1371 -- analyzing Decl_ARECnP so that the flag is properly
1372 -- taking into account.
1374 Set_Suppress_Initialization (STJ.ARECnT);
1376 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1377 Analyze (Decl_ARECn, Suppress => All_Checks);
1378 Analyze (Decl_ARECnP, Suppress => All_Checks);
1380 if Present (Decl_Assign) then
1381 Analyze (Decl_Assign, Suppress => All_Checks);
1382 end if;
1384 Pop_Scope;
1386 -- Next step, for each uplevel referenced entity, add
1387 -- assignment operations to set the component in the
1388 -- activation record.
1390 if Present (STJ.Uents) then
1391 declare
1392 Elmt : Elmt_Id;
1394 begin
1395 Elmt := First_Elmt (STJ.Uents);
1396 while Present (Elmt) loop
1397 declare
1398 Ent : constant Entity_Id := Node (Elmt);
1399 Loc : constant Source_Ptr := Sloc (Ent);
1400 Dec : constant Node_Id :=
1401 Declaration_Node (Ent);
1402 Ins : Node_Id;
1403 Asn : Node_Id;
1405 begin
1406 -- For parameters, we insert the assignment
1407 -- right after the declaration of ARECnP.
1408 -- For all other entities, we insert
1409 -- the assignment immediately after
1410 -- the declaration of the entity.
1412 -- Note: we don't need to mark the entity
1413 -- as being aliased, because the address
1414 -- attribute will mark it as Address_Taken,
1415 -- and that is good enough.
1417 if Is_Formal (Ent) then
1418 Ins := Decl_ARECnP;
1419 else
1420 Ins := Dec;
1421 end if;
1423 -- Build and insert the assignment:
1424 -- ARECn.nam := nam'Address
1426 Asn :=
1427 Make_Assignment_Statement (Loc,
1428 Name =>
1429 Make_Selected_Component (Loc,
1430 Prefix =>
1431 New_Occurrence_Of (STJ.ARECn, Loc),
1432 Selector_Name =>
1433 New_Occurrence_Of
1434 (Activation_Record_Component
1435 (Ent),
1436 Loc)),
1438 Expression =>
1439 Make_Attribute_Reference (Loc,
1440 Prefix =>
1441 New_Occurrence_Of (Ent, Loc),
1442 Attribute_Name => Name_Address));
1444 Insert_After (Ins, Asn);
1446 -- Analyze the assignment statement. We do
1447 -- not need to establish the relevant scope
1448 -- stack entries here, because we have
1449 -- already set the correct entity references,
1450 -- so no name resolution is required, and no
1451 -- new entities are created, so we don't even
1452 -- need to set the current scope.
1454 -- We analyze with all checks suppressed
1455 -- (since we do not expect any exceptions).
1457 Analyze (Asn, Suppress => All_Checks);
1458 end;
1460 Next_Elmt (Elmt);
1461 end loop;
1462 end;
1463 end if;
1464 end;
1465 end if;
1466 end;
1467 end loop;
1468 end Subp_Loop;
1470 -- Next step, process uplevel references. This has to be done in a
1471 -- separate pass, after completing the processing in Sub_Loop because we
1472 -- need all the AREC declarations generated, inserted, and analyzed so
1473 -- that the uplevel references can be successfully analyzed.
1475 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
1476 declare
1477 UPJ : Uref_Entry renames Urefs.Table (J);
1479 begin
1480 -- Ignore type references, these are implicit references that do
1481 -- not need rewriting (e.g. the appearence in a conversion).
1483 if Is_Type (UPJ.Ent) then
1484 goto Continue;
1485 end if;
1487 -- Also ignore uplevel references to bounds of types that come
1488 -- from the original type reference.
1490 if Is_Entity_Name (UPJ.Ref)
1491 and then Present (Entity (UPJ.Ref))
1492 and then Is_Type (Entity (UPJ.Ref))
1493 then
1494 goto Continue;
1495 end if;
1497 -- Rewrite one reference
1499 Rewrite_One_Ref : declare
1500 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
1501 -- Source location for the reference
1503 Typ : constant Entity_Id := Etype (UPJ.Ent);
1504 -- The type of the referenced entity
1506 Atyp : constant Entity_Id := Get_Actual_Subtype (UPJ.Ref);
1507 -- The actual subtype of the reference
1509 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
1510 -- Subp_Index for caller containing reference
1512 STJR : Subp_Entry renames Subps.Table (RS_Caller);
1513 -- Subp_Entry for subprogram containing reference
1515 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
1516 -- Subp_Index for subprogram containing referenced entity
1518 STJE : Subp_Entry renames Subps.Table (RS_Callee);
1519 -- Subp_Entry for subprogram containing referenced entity
1521 Pfx : Node_Id;
1522 Comp : Entity_Id;
1523 SI : SI_Type;
1525 begin
1526 -- Ignore if no ARECnF entity for enclosing subprogram which
1527 -- probably happens as a result of not properly treating
1528 -- instance bodies. To be examined ???
1530 -- If this test is omitted, then the compilation of freeze.adb
1531 -- and inline.adb fail in unnesting mode.
1533 if No (STJR.ARECnF) then
1534 goto Continue;
1535 end if;
1537 -- Push the current scope, so that the pointer type Tnn, and
1538 -- any subsidiary entities resulting from the analysis of the
1539 -- rewritten reference, go in the right entity chain.
1541 Push_Scope (STJR.Ent);
1543 -- Now we need to rewrite the reference. We have a reference
1544 -- from level STJR.Lev to level STJE.Lev. The general form of
1545 -- the rewritten reference for entity X is:
1547 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECm.X)
1549 -- where a,b,c,d .. m =
1550 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
1552 pragma Assert (STJR.Lev > STJE.Lev);
1554 -- Compute the prefix of X. Here are examples to make things
1555 -- clear (with parens to show groupings, the prefix is
1556 -- everything except the .X at the end).
1558 -- level 2 to level 1
1560 -- AREC1F.X
1562 -- level 3 to level 1
1564 -- (AREC2F.AREC1U).X
1566 -- level 4 to level 1
1568 -- ((AREC3F.AREC2U).AREC1U).X
1570 -- level 6 to level 2
1572 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
1574 -- In the above, ARECnF and ARECnU are pointers, so there are
1575 -- explicit dereferences required for these occurrences.
1577 Pfx :=
1578 Make_Explicit_Dereference (Loc,
1579 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
1580 SI := RS_Caller;
1581 for L in STJE.Lev .. STJR.Lev - 2 loop
1582 SI := Enclosing_Subp (SI);
1583 Pfx :=
1584 Make_Explicit_Dereference (Loc,
1585 Prefix =>
1586 Make_Selected_Component (Loc,
1587 Prefix => Pfx,
1588 Selector_Name =>
1589 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
1590 end loop;
1592 -- Get activation record component (must exist)
1594 Comp := Activation_Record_Component (UPJ.Ent);
1595 pragma Assert (Present (Comp));
1597 -- Do the replacement
1599 Rewrite (UPJ.Ref,
1600 Make_Attribute_Reference (Loc,
1601 Prefix => New_Occurrence_Of (Atyp, Loc),
1602 Attribute_Name => Name_Deref,
1603 Expressions => New_List (
1604 Make_Selected_Component (Loc,
1605 Prefix => Pfx,
1606 Selector_Name =>
1607 New_Occurrence_Of (Comp, Loc)))));
1609 -- Analyze and resolve the new expression. We do not need to
1610 -- establish the relevant scope stack entries here, because we
1611 -- have already set all the correct entity references, so no
1612 -- name resolution is needed. We have already set the current
1613 -- scope, so that any new entities created will be in the right
1614 -- scope.
1616 -- We analyze with all checks suppressed (since we do not
1617 -- expect any exceptions)
1619 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
1620 Pop_Scope;
1621 end Rewrite_One_Ref;
1622 end;
1624 <<Continue>>
1625 null;
1626 end loop Uplev_Refs;
1628 -- Finally, loop through all calls adding extra actual for the
1629 -- activation record where it is required.
1631 Adjust_Calls : for J in Calls.First .. Calls.Last loop
1633 -- Process a single call, we are only interested in a call to a
1634 -- subprogram that actually needs a pointer to an activation record,
1635 -- as indicated by the ARECnF entity being set. This excludes the
1636 -- top level subprogram, and any subprogram not having uplevel refs.
1638 Adjust_One_Call : declare
1639 CTJ : Call_Entry renames Calls.Table (J);
1640 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
1641 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
1643 Loc : constant Source_Ptr := Sloc (CTJ.N);
1645 Extra : Node_Id;
1646 ExtraP : Node_Id;
1647 SubX : SI_Type;
1648 Act : Node_Id;
1650 begin
1651 if Present (STT.ARECnF)
1652 and then Nkind (CTJ.N) /= N_Attribute_Reference
1653 then
1655 -- CTJ.N is a call to a subprogram which may require a pointer
1656 -- to an activation record. The subprogram containing the call
1657 -- is CTJ.From and the subprogram being called is CTJ.To, so we
1658 -- have a call from level STF.Lev to level STT.Lev.
1660 -- There are three possibilities:
1662 -- For a call to the same level, we just pass the activation
1663 -- record passed to the calling subprogram.
1665 if STF.Lev = STT.Lev then
1666 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1668 -- For a call that goes down a level, we pass a pointer to the
1669 -- activation record constructed within the caller (which may
1670 -- be the outer-level subprogram, but also may be a more deeply
1671 -- nested caller).
1673 elsif STT.Lev = STF.Lev + 1 then
1674 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
1676 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
1677 -- since it is not possible to do a downcall of more than
1678 -- one level.
1680 -- For a call from level STF.Lev to level STT.Lev, we
1681 -- have to find the activation record needed by the
1682 -- callee. This is as follows:
1684 -- ARECaF.ARECbU.ARECcU....ARECm
1686 -- where a,b,c .. m =
1687 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
1689 else
1690 pragma Assert (STT.Lev < STF.Lev);
1692 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1693 SubX := Subp_Index (CTJ.Caller);
1694 for K in reverse STT.Lev .. STF.Lev - 1 loop
1695 SubX := Enclosing_Subp (SubX);
1696 Extra :=
1697 Make_Selected_Component (Loc,
1698 Prefix => Extra,
1699 Selector_Name =>
1700 New_Occurrence_Of
1701 (Subps.Table (SubX).ARECnU, Loc));
1702 end loop;
1703 end if;
1705 -- Extra is the additional parameter to be added. Build a
1706 -- parameter association that we can append to the actuals.
1708 ExtraP :=
1709 Make_Parameter_Association (Loc,
1710 Selector_Name =>
1711 New_Occurrence_Of (STT.ARECnF, Loc),
1712 Explicit_Actual_Parameter => Extra);
1714 if No (Parameter_Associations (CTJ.N)) then
1715 Set_Parameter_Associations (CTJ.N, Empty_List);
1716 end if;
1718 Append (ExtraP, Parameter_Associations (CTJ.N));
1720 -- We need to deal with the actual parameter chain as well. The
1721 -- newly added parameter is always the last actual.
1723 Act := First_Named_Actual (CTJ.N);
1725 if No (Act) then
1726 Set_First_Named_Actual (CTJ.N, Extra);
1728 -- Here we must follow the chain and append the new entry
1730 else
1731 loop
1732 declare
1733 PAN : Node_Id;
1734 NNA : Node_Id;
1736 begin
1737 PAN := Parent (Act);
1738 pragma Assert (Nkind (PAN) = N_Parameter_Association);
1739 NNA := Next_Named_Actual (PAN);
1741 if No (NNA) then
1742 Set_Next_Named_Actual (PAN, Extra);
1743 exit;
1744 end if;
1746 Act := NNA;
1747 end;
1748 end loop;
1749 end if;
1751 -- Analyze and resolve the new actual. We do not need to
1752 -- establish the relevant scope stack entries here, because
1753 -- we have already set all the correct entity references, so
1754 -- no name resolution is needed.
1756 -- We analyze with all checks suppressed (since we do not
1757 -- expect any exceptions, and also we temporarily turn off
1758 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
1759 -- references (not needed at this stage, and in fact causes
1760 -- a bit of recursive chaos).
1762 Opt.Unnest_Subprogram_Mode := False;
1763 Analyze_And_Resolve
1764 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
1765 Opt.Unnest_Subprogram_Mode := True;
1766 end if;
1767 end Adjust_One_Call;
1768 end loop Adjust_Calls;
1770 return;
1771 end Unnest_Subprogram;
1773 ------------------------
1774 -- Unnest_Subprograms --
1775 ------------------------
1777 procedure Unnest_Subprograms (N : Node_Id) is
1778 function Search_Subprograms (N : Node_Id) return Traverse_Result;
1779 -- Tree visitor that search for outer level procedures with nested
1780 -- subprograms and invokes Unnest_Subprogram()
1782 ------------------------
1783 -- Search_Subprograms --
1784 ------------------------
1786 function Search_Subprograms (N : Node_Id) return Traverse_Result is
1787 begin
1788 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
1789 declare
1790 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
1792 begin
1793 -- We are only interested in subprograms (not generic
1794 -- subprograms), that have nested subprograms.
1796 if Is_Subprogram (Spec_Id)
1797 and then Has_Nested_Subprogram (Spec_Id)
1798 and then Is_Library_Level_Entity (Spec_Id)
1799 then
1800 Unnest_Subprogram (Spec_Id, N);
1801 end if;
1802 end;
1803 end if;
1805 return OK;
1806 end Search_Subprograms;
1808 ---------------
1809 -- Do_Search --
1810 ---------------
1812 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
1813 -- Subtree visitor instantiation
1815 -- Start of processing for Unnest_Subprograms
1817 begin
1818 if not Opt.Unnest_Subprogram_Mode then
1819 return;
1820 end if;
1822 Do_Search (N);
1823 end Unnest_Subprograms;
1825 end Exp_Unst;