1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2014-2018, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
31 with Namet; use Namet;
32 with Nlists; use Nlists;
33 with Nmake; use Nmake;
35 with Output; use Output;
36 with Rtsfind; use Rtsfind;
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.
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
72 type Call_Entry is record
77 -- Entity of the subprogram containing the call (can be at any level)
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.
85 package Calls is new Table.Table (
86 Table_Component_Type => Call_Entry,
87 Table_Index_Type => Nat,
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.
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
112 type Uref_Entry is record
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.
120 -- The Entity_Id of the uplevel referenced object or type
123 -- The entity for the subprogram immediately containing this entity
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.
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
145 for J in Calls.First .. Calls.Last loop
146 if Calls.Table (J) = Call then
152 end Append_Unique_Call;
158 function Get_Level (Subp : Entity_Id; Sub : Entity_Id) return Nat is
170 S := Enclosing_Subprogram (S);
179 function Subp_Index (Sub : Entity_Id) return SI_Type is
180 E : Entity_Id := Sub;
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))
192 E := Corresponding_Procedure (E);
196 pragma Assert (Subps_Index (E) /= Uint_0);
197 return SI_Type (UI_To_Int (Subps_Index (E)));
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
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.
232 function AREC_Name (J : Pos; S : String) return Name_Id is
234 return Name_Find ("AREC" & Img_Pos (J) & S);
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));
245 pragma Assert (STJ.Lev > 1);
246 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
254 function Img_Pos (N : Pos) return String is
255 Buf : String (1 .. 20);
263 Buf (Ptr) := Character'Val (48 + NV mod 10);
268 return Buf (Ptr + 1 .. Buf'Last);
278 Clist : List_Id) return Name_Id
287 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
289 Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
296 -- Start of processing for Unnest_Subprogram
299 -- Nothing to do inside a generic (all processing is for instance)
301 if Inside_A_Generic then
305 -- If the main unit is a package body then we need to examine the spec
306 -- to determine whether the main unit is generic (the scope stack is not
307 -- present when this is called on the main unit).
309 if Ekind (Cunit_Entity (Main_Unit)) = E_Package_Body
310 and then Is_Generic_Unit (Spec_Entity (Cunit_Entity (Main_Unit)))
315 -- Only unnest when generating code for the main source unit
317 if not In_Extended_Main_Code_Unit (Subp_Body) then
321 -- This routine is called late, after the scope stack is gone. The
322 -- following creates a suitable dummy scope stack to be used for the
323 -- analyze/expand calls made from this routine.
327 -- First step, we must mark all nested subprograms that require a static
328 -- link (activation record) because either they contain explicit uplevel
329 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
330 -- this point), or they make calls to other subprograms in the same nest
331 -- that require a static link (in which case we set this flag).
333 -- This is a recursive definition, and to implement this, we have to
334 -- build a call graph for the set of nested subprograms, and then go
335 -- over this graph to implement recursively the invariant that if a
336 -- subprogram has a call to a subprogram requiring a static link, then
337 -- the calling subprogram requires a static link.
339 -- First populate the above tables
341 Subps_First := Subps.Last + 1;
345 Build_Tables : declare
346 Current_Subprogram : Entity_Id;
347 -- When we scan a subprogram body, we set Current_Subprogram to the
348 -- corresponding entity. This gets recursively saved and restored.
350 function Visit_Node (N : Node_Id) return Traverse_Result;
351 -- Visit a single node in Subp
357 procedure Visit is new Traverse_Proc (Visit_Node);
358 -- Used to traverse the body of Subp, populating the tables
364 function Visit_Node (N : Node_Id) return Traverse_Result is
369 procedure Check_Static_Type
370 (T : Entity_Id; N : Node_Id; DT : in out Boolean);
371 -- Given a type T, checks if it is a static type defined as a type
372 -- with no dynamic bounds in sight. If so, the only action is to
373 -- set Is_Static_Type True for T. If T is not a static type, then
374 -- all types with dynamic bounds associated with T are detected,
375 -- and their bounds are marked as uplevel referenced if not at the
376 -- library level, and DT is set True. If N is specified, it's the
377 -- node that will need to be replaced. If not specified, it means
378 -- we can't do a replacement because the bound is implicit.
380 procedure Note_Uplevel_Ref
385 -- Called when we detect an explicit or implicit uplevel reference
386 -- from within Caller to entity E declared in Callee. E can be a
387 -- an object or a type.
389 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id);
390 -- Enter a subprogram whose body is visible or which is a
391 -- subprogram instance into the subprogram table.
393 -----------------------
394 -- Check_Static_Type --
395 -----------------------
397 procedure Check_Static_Type
398 (T : Entity_Id; N : Node_Id; DT : in out Boolean)
400 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id);
401 -- N is the bound of a dynamic type. This procedure notes that
402 -- this bound is uplevel referenced, it can handle references
403 -- to entities (typically _FIRST and _LAST entities), and also
404 -- attribute references of the form T'name (name is typically
405 -- FIRST or LAST) where T is the uplevel referenced bound.
406 -- Ref, if Present, is the location of the reference to
409 ------------------------
410 -- Note_Uplevel_Bound --
411 ------------------------
413 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id) is
415 -- Entity name case. Make sure that the entity is declared
416 -- in a subprogram. This may not be the case for for a type
417 -- in a loop appearing in a precondition.
418 -- Exclude explicitly discriminants (that can appear
419 -- in bounds of discriminated components).
421 if Is_Entity_Name (N) then
422 if Present (Entity (N))
423 and then Present (Enclosing_Subprogram (Entity (N)))
424 and then Ekind (Entity (N)) /= E_Discriminant
429 Caller => Current_Subprogram,
430 Callee => Enclosing_Subprogram (Entity (N)));
433 -- Attribute or indexed component case
435 elsif Nkind_In (N, N_Attribute_Reference,
438 Note_Uplevel_Bound (Prefix (N), Ref);
440 -- The indices of the indexed components, or the
441 -- associated expressions of an attribute reference,
442 -- may also involve uplevel references.
448 Expr := First (Expressions (N));
449 while Present (Expr) loop
450 Note_Uplevel_Bound (Expr, Ref);
457 elsif Nkind (N) = N_Type_Conversion then
458 Note_Uplevel_Bound (Expression (N), Ref);
460 end Note_Uplevel_Bound;
462 -- Start of processing for Check_Static_Type
465 -- If already marked static, immediate return
467 if Is_Static_Type (T) then
471 -- If the type is at library level, always consider it static,
472 -- since such uplevel references are irrelevant.
474 if Is_Library_Level_Entity (T) then
475 Set_Is_Static_Type (T);
479 -- Otherwise figure out what the story is with this type
481 -- For a scalar type, check bounds
483 if Is_Scalar_Type (T) then
485 -- If both bounds static, then this is a static type
488 LB : constant Node_Id := Type_Low_Bound (T);
489 UB : constant Node_Id := Type_High_Bound (T);
492 if not Is_Static_Expression (LB) then
493 Note_Uplevel_Bound (LB, N);
497 if not Is_Static_Expression (UB) then
498 Note_Uplevel_Bound (UB, N);
503 -- For record type, check all components and discriminant
504 -- constraints if present.
506 elsif Is_Record_Type (T) then
512 C := First_Component_Or_Discriminant (T);
513 while Present (C) loop
514 Check_Static_Type (Etype (C), N, DT);
515 Next_Component_Or_Discriminant (C);
518 if Has_Discriminants (T)
519 and then Present (Discriminant_Constraint (T))
521 D := First_Elmt (Discriminant_Constraint (T));
522 while Present (D) loop
523 if not Is_Static_Expression (Node (D)) then
524 Note_Uplevel_Bound (Node (D), N);
533 -- For array type, check index types and component type
535 elsif Is_Array_Type (T) then
539 Check_Static_Type (Component_Type (T), N, DT);
541 IX := First_Index (T);
542 while Present (IX) loop
543 Check_Static_Type (Etype (IX), N, DT);
548 -- For private type, examine whether full view is static
550 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
551 Check_Static_Type (Full_View (T), N, DT);
553 if Is_Static_Type (Full_View (T)) then
554 Set_Is_Static_Type (T);
557 -- For now, ignore other types
564 Set_Is_Static_Type (T);
566 end Check_Static_Type;
568 ----------------------
569 -- Note_Uplevel_Ref --
570 ----------------------
572 procedure Note_Uplevel_Ref
578 Full_E : Entity_Id := E;
580 -- Nothing to do for static type
582 if Is_Static_Type (E) then
586 -- Nothing to do if Caller and Callee are the same
588 if Caller = Callee then
591 -- Callee may be a function that returns an array, and that has
592 -- been rewritten as a procedure. If caller is that procedure,
593 -- nothing to do either.
595 elsif Ekind (Callee) = E_Function
596 and then Rewritten_For_C (Callee)
597 and then Corresponding_Procedure (Callee) = Caller
602 -- We have a new uplevel referenced entity
604 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
605 Full_E := Full_View (E);
608 -- All we do at this stage is to add the uplevel reference to
609 -- the table. It's too early to do anything else, since this
610 -- uplevel reference may come from an unreachable subprogram
611 -- in which case the entry will be deleted.
613 Urefs.Append ((N, Full_E, Caller, Callee));
614 end Note_Uplevel_Ref;
616 -------------------------
617 -- Register_Subprogram --
618 -------------------------
620 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
621 L : constant Nat := Get_Level (Subp, E);
630 Declares_AREC => False,
640 Set_Subps_Index (E, UI_From_Int (Subps.Last));
641 end Register_Subprogram;
643 -- Start of processing for Visit_Node
648 -- Record a subprogram call
651 | N_Procedure_Call_Statement
653 -- We are only interested in direct calls, not indirect
654 -- calls (where Name (N) is an explicit dereference) at
657 if Nkind (Name (N)) in N_Has_Entity then
658 Ent := Entity (Name (N));
660 -- We are only interested in calls to subprograms nested
661 -- within Subp. Calls to Subp itself or to subprograms
662 -- outside the nested structure do not affect us.
664 if Scope_Within (Ent, Subp)
665 and then Is_Subprogram (Ent)
666 and then not Is_Imported (Ent)
668 Append_Unique_Call ((N, Current_Subprogram, Ent));
672 -- For all calls where the formal is an unconstrained array
673 -- and the actual is constrained we need to check the bounds
674 -- for uplevel references.
678 DT : Boolean := False;
683 if Nkind (Name (N)) = N_Explicit_Dereference then
684 Subp := Etype (Name (N));
686 Subp := Entity (Name (N));
689 Actual := First_Actual (N);
690 Formal := First_Formal_With_Extras (Subp);
691 while Present (Actual) loop
692 if Is_Array_Type (Etype (Formal))
693 and then not Is_Constrained (Etype (Formal))
694 and then Is_Constrained (Etype (Actual))
696 Check_Static_Type (Etype (Actual), Empty, DT);
699 Next_Actual (Actual);
700 Next_Formal_With_Extras (Formal);
704 -- An At_End_Proc in a statement sequence indicates that there
705 -- is a call from the enclosing construct or block to that
706 -- subprogram. As above, the called entity must be local and
709 when N_Handled_Sequence_Of_Statements =>
710 if Present (At_End_Proc (N))
711 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
712 and then not Is_Imported (Entity (At_End_Proc (N)))
715 ((N, Current_Subprogram, Entity (At_End_Proc (N))));
718 -- Similarly, the following constructs include a semantic
719 -- attribute Procedure_To_Call that must be handled like
723 | N_Extended_Return_Statement
725 | N_Simple_Return_Statement
728 Proc : constant Entity_Id := Procedure_To_Call (N);
731 and then Scope_Within (Proc, Subp)
732 and then not Is_Imported (Proc)
734 Append_Unique_Call ((N, Current_Subprogram, Proc));
738 -- A 'Access reference is a (potential) call. Other attributes
739 -- require special handling.
741 when N_Attribute_Reference =>
743 Attr : constant Attribute_Id :=
744 Get_Attribute_Id (Attribute_Name (N));
747 when Attribute_Access
748 | Attribute_Unchecked_Access
749 | Attribute_Unrestricted_Access
751 if Nkind (Prefix (N)) in N_Has_Entity then
752 Ent := Entity (Prefix (N));
754 -- We only need to examine calls to subprograms
755 -- nested within current Subp.
757 if Scope_Within (Ent, Subp) then
758 if Is_Imported (Ent) then
761 elsif Is_Subprogram (Ent) then
763 ((N, Current_Subprogram, Ent));
768 -- References to bounds can be uplevel references if
769 -- the type isn't static.
775 -- Special-case attributes of objects whose bounds
776 -- may be uplevel references. More complex prefixes
777 -- handled during full traversal. Note that if the
778 -- nominal subtype of the prefix is unconstrained,
779 -- the bound must be obtained from the object, not
780 -- from the (possibly) uplevel reference.
782 if Is_Constrained (Etype (Prefix (N))) then
784 DT : Boolean := False;
787 (Etype (Prefix (N)), Empty, DT);
798 -- Component associations in aggregates are either static or
799 -- else the aggregate will be expanded into assignments, in
800 -- which case the expression is analyzed later and provides
801 -- no relevant code generation.
803 when N_Component_Association =>
804 if No (Etype (Expression (N))) then
808 -- Indexed references can be uplevel if the type isn't static
809 -- and if the lower bound (or an inner bound for a multi-
810 -- dimensional array) is uplevel.
812 when N_Indexed_Component | N_Slice =>
813 if Is_Constrained (Etype (Prefix (N))) then
815 DT : Boolean := False;
817 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
821 -- A selected component can have an implicit up-level
822 -- reference due to the bounds of previous fields in the
823 -- record. We simplify the processing here by examining
824 -- all components of the record.
826 -- Selected components appear as unit names and end labels
827 -- for child units. Prefixes of these nodes denote parent
828 -- units and carry no type information so they are skipped.
830 when N_Selected_Component =>
831 if Present (Etype (Prefix (N))) then
833 DT : Boolean := False;
835 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
839 -- Record a subprogram. We record a subprogram body that acts
840 -- as a spec. Otherwise we record a subprogram declaration,
841 -- providing that it has a corresponding body we can get hold
842 -- of. The case of no corresponding body being available is
845 when N_Subprogram_Body =>
846 Ent := Unique_Defining_Entity (N);
848 -- Ignore generic subprogram
850 if Is_Generic_Subprogram (Ent) then
854 -- Make new entry in subprogram table if not already made
856 Register_Subprogram (Ent, N);
858 -- We make a recursive call to scan the subprogram body, so
859 -- that we can save and restore Current_Subprogram.
862 Save_CS : constant Entity_Id := Current_Subprogram;
866 Current_Subprogram := Ent;
870 Decl := First (Declarations (N));
871 while Present (Decl) loop
878 Visit (Handled_Statement_Sequence (N));
880 -- Restore current subprogram setting
882 Current_Subprogram := Save_CS;
885 -- Now at this level, return skipping the subprogram body
886 -- descendants, since we already took care of them!
890 -- If we have a body stub, visit the associated subunit, which
891 -- is a semantic descendant of the stub.
894 Visit (Library_Unit (N));
896 -- A declaration of a wrapper package indicates a subprogram
897 -- instance for which there is no explicit body. Enter the
898 -- subprogram instance in the table.
900 when N_Package_Declaration =>
901 if Is_Wrapper_Package (Defining_Entity (N)) then
903 (Related_Instance (Defining_Entity (N)), Empty);
906 -- Skip generic declarations
908 when N_Generic_Declaration =>
911 -- Skip generic package body
913 when N_Package_Body =>
914 if Present (Corresponding_Spec (N))
915 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
920 -- Otherwise record an uplevel reference
923 if Nkind (N) in N_Has_Entity
924 and then Present (Entity (N))
928 -- Only interested in entities declared within our nest
930 if not Is_Library_Level_Entity (Ent)
931 and then Scope_Within_Or_Same (Scope (Ent), Subp)
933 -- Skip entities defined in inlined subprograms
936 Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
938 -- Constants and variables are potentially uplevel
939 -- references to global declarations.
942 (Ekind_In (Ent, E_Constant, E_Variable)
944 -- Formals are interesting, but not if being used as
945 -- mere names of parameters for name notation calls.
950 (Nkind (Parent (N)) = N_Parameter_Association
951 and then Selector_Name (Parent (N)) = N))
953 -- Types other than known Is_Static types are
954 -- potentially interesting.
956 or else (Is_Type (Ent)
957 and then not Is_Static_Type (Ent)))
959 -- Here we have a potentially interesting uplevel
960 -- reference to examine.
962 if Is_Type (Ent) then
964 DT : Boolean := False;
967 Check_Static_Type (Ent, N, DT);
969 if Is_Static_Type (Ent) then
975 Caller := Current_Subprogram;
976 Callee := Enclosing_Subprogram (Ent);
979 and then not Is_Static_Type (Ent)
981 Note_Uplevel_Ref (Ent, N, Caller, Callee);
987 -- Fall through to continue scanning children of this node
992 -- Start of processing for Build_Tables
995 -- Traverse the body to get subprograms, calls and uplevel references
1000 -- Now do the first transitive closure which determines which
1001 -- subprograms in the nest are actually reachable.
1003 Reachable_Closure : declare
1007 Subps.Table (Subps_First).Reachable := True;
1009 -- We use a simple minded algorithm as follows (obviously this can
1010 -- be done more efficiently, using one of the standard algorithms
1011 -- for efficient transitive closure computation, but this is simple
1012 -- and most likely fast enough that its speed does not matter).
1014 -- Repeatedly scan the list of calls. Any time we find a call from
1015 -- A to B, where A is reachable, but B is not, then B is reachable,
1016 -- and note that we have made a change by setting Modified True. We
1017 -- repeat this until we make a pass with no modifications.
1021 Inner : for J in Calls.First .. Calls.Last loop
1023 CTJ : Call_Entry renames Calls.Table (J);
1025 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1026 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1028 SUBF : Subp_Entry renames Subps.Table (SINF);
1029 SUBT : Subp_Entry renames Subps.Table (SINT);
1032 if SUBF.Reachable and then not SUBT.Reachable then
1033 SUBT.Reachable := True;
1039 exit Outer when not Modified;
1041 end Reachable_Closure;
1043 -- Remove calls from unreachable subprograms
1050 for J in Calls.First .. Calls.Last loop
1052 CTJ : Call_Entry renames Calls.Table (J);
1054 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1055 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1057 SUBF : Subp_Entry renames Subps.Table (SINF);
1058 SUBT : Subp_Entry renames Subps.Table (SINT);
1061 if SUBF.Reachable then
1062 pragma Assert (SUBT.Reachable);
1063 New_Index := New_Index + 1;
1064 Calls.Table (New_Index) := Calls.Table (J);
1069 Calls.Set_Last (New_Index);
1072 -- Remove uplevel references from unreachable subprograms
1079 for J in Urefs.First .. Urefs.Last loop
1081 URJ : Uref_Entry renames Urefs.Table (J);
1083 SINF : constant SI_Type := Subp_Index (URJ.Caller);
1084 SINT : constant SI_Type := Subp_Index (URJ.Callee);
1086 SUBF : Subp_Entry renames Subps.Table (SINF);
1087 SUBT : Subp_Entry renames Subps.Table (SINT);
1092 -- Keep reachable reference
1094 if SUBF.Reachable then
1095 New_Index := New_Index + 1;
1096 Urefs.Table (New_Index) := Urefs.Table (J);
1098 -- And since we know we are keeping this one, this is a good
1099 -- place to fill in information for a good reference.
1101 -- Mark all enclosing subprograms need to declare AREC
1105 S := Enclosing_Subprogram (S);
1107 -- if we are at the top level, as can happen with
1108 -- references to formals in aspects of nested subprogram
1109 -- declarations, there are no further subprograms to
1110 -- mark as requiring activation records.
1113 Subps.Table (Subp_Index (S)).Declares_AREC := True;
1114 exit when S = URJ.Callee;
1117 -- Add to list of uplevel referenced entities for Callee.
1118 -- We do not add types to this list, only actual references
1119 -- to objects that will be referenced uplevel, and we use
1120 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1121 -- duplicate entries in the list.
1122 -- Discriminants are also excluded, only the enclosing
1123 -- object can appear in the list.
1125 if not Is_Uplevel_Referenced_Entity (URJ.Ent)
1126 and then Ekind (URJ.Ent) /= E_Discriminant
1128 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
1130 if not Is_Type (URJ.Ent) then
1131 Append_New_Elmt (URJ.Ent, SUBT.Uents);
1135 -- And set uplevel indication for caller
1137 if SUBT.Lev < SUBF.Uplevel_Ref then
1138 SUBF.Uplevel_Ref := SUBT.Lev;
1144 Urefs.Set_Last (New_Index);
1147 -- Remove unreachable subprograms from Subps table. Note that we do
1148 -- this after eliminating entries from the other two tables, since
1149 -- those elimination steps depend on referencing the Subps table.
1155 New_SI := Subps_First - 1;
1156 for J in Subps_First .. Subps.Last loop
1158 STJ : Subp_Entry renames Subps.Table (J);
1163 -- Subprogram is reachable, copy and reset index
1165 if STJ.Reachable then
1166 New_SI := New_SI + 1;
1167 Subps.Table (New_SI) := STJ;
1168 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
1170 -- Subprogram is not reachable
1173 -- Clear index, since no longer active
1175 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
1177 -- Output debug information if -gnatd.3 set
1179 if Debug_Flag_Dot_3 then
1180 Write_Str ("Eliminate ");
1181 Write_Name (Chars (Subps.Table (J).Ent));
1183 Write_Location (Sloc (Subps.Table (J).Ent));
1184 Write_Str (" (not referenced)");
1188 -- Rewrite declaration and body to null statements
1190 -- A subprogram instantiation does not have an explicit
1191 -- body. If unused, we could remove the corresponding
1192 -- wrapper package and its body (TBD).
1194 if Present (STJ.Bod) then
1195 Spec := Corresponding_Spec (STJ.Bod);
1197 if Present (Spec) then
1198 Decl := Parent (Declaration_Node (Spec));
1199 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
1202 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
1208 Subps.Set_Last (New_SI);
1211 -- Now it is time for the second transitive closure, which follows calls
1212 -- and makes sure that A calls B, and B has uplevel references, then A
1213 -- is also marked as having uplevel references.
1215 Closure_Uplevel : declare
1219 -- We use a simple minded algorithm as follows (obviously this can
1220 -- be done more efficiently, using one of the standard algorithms
1221 -- for efficient transitive closure computation, but this is simple
1222 -- and most likely fast enough that its speed does not matter).
1224 -- Repeatedly scan the list of calls. Any time we find a call from
1225 -- A to B, where B has uplevel references, make sure that A is marked
1226 -- as having at least the same level of uplevel referencing.
1230 Inner2 : for J in Calls.First .. Calls.Last loop
1232 CTJ : Call_Entry renames Calls.Table (J);
1233 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1234 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1235 SUBF : Subp_Entry renames Subps.Table (SINF);
1236 SUBT : Subp_Entry renames Subps.Table (SINT);
1238 if SUBT.Lev > SUBT.Uplevel_Ref
1239 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
1241 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
1247 exit Outer2 when not Modified;
1249 end Closure_Uplevel;
1251 -- We have one more step before the tables are complete. An uplevel
1252 -- call from subprogram A to subprogram B where subprogram B has uplevel
1253 -- references is in effect an uplevel reference, and must arrange for
1254 -- the proper activation link to be passed.
1256 for J in Calls.First .. Calls.Last loop
1258 CTJ : Call_Entry renames Calls.Table (J);
1260 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1261 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1263 SUBF : Subp_Entry renames Subps.Table (SINF);
1264 SUBT : Subp_Entry renames Subps.Table (SINT);
1269 -- If callee has uplevel references
1271 if SUBT.Uplevel_Ref < SUBT.Lev
1273 -- And this is an uplevel call
1275 and then SUBT.Lev < SUBF.Lev
1277 -- We need to arrange for finding the uplink
1281 A := Enclosing_Subprogram (A);
1282 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1283 exit when A = CTJ.Callee;
1285 -- In any case exit when we get to the outer level. This
1286 -- happens in some odd cases with generics (in particular
1287 -- sem_ch3.adb does not compile without this kludge ???).
1295 -- The tables are now complete, so we can record the last index in the
1296 -- Subps table for later reference in Cprint.
1298 Subps.Table (Subps_First).Last := Subps.Last;
1300 -- Next step, create the entities for code we will insert. We do this
1301 -- at the start so that all the entities are defined, regardless of the
1302 -- order in which we do the code insertions.
1304 Create_Entities : for J in Subps_First .. Subps.Last loop
1306 STJ : Subp_Entry renames Subps.Table (J);
1307 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1310 -- First we create the ARECnF entity for the additional formal for
1311 -- all subprograms which need an activation record passed.
1313 if STJ.Uplevel_Ref < STJ.Lev then
1315 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1318 -- Define the AREC entities for the activation record if needed
1320 if STJ.Declares_AREC then
1322 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1324 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1326 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1328 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1330 -- Define uplink component entity if inner nesting case
1332 if Present (STJ.ARECnF) then
1334 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1338 end loop Create_Entities;
1340 -- Loop through subprograms
1343 Addr : constant Entity_Id := RTE (RE_Address);
1346 for J in Subps_First .. Subps.Last loop
1348 STJ : Subp_Entry renames Subps.Table (J);
1351 -- First add the extra formal if needed. This applies to all
1352 -- nested subprograms that require an activation record to be
1353 -- passed, as indicated by ARECnF being defined.
1355 if Present (STJ.ARECnF) then
1357 -- Here we need the extra formal. We do the expansion and
1358 -- analysis of this manually, since it is fairly simple,
1359 -- and it is not obvious how we can get what we want if we
1360 -- try to use the normal Analyze circuit.
1362 Add_Extra_Formal : declare
1363 Encl : constant SI_Type := Enclosing_Subp (J);
1364 STJE : Subp_Entry renames Subps.Table (Encl);
1365 -- Index and Subp_Entry for enclosing routine
1367 Form : constant Entity_Id := STJ.ARECnF;
1368 -- The formal to be added. Note that n here is one less
1369 -- than the level of the subprogram itself (STJ.Ent).
1371 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1372 -- S is an N_Function/Procedure_Specification node, and F
1373 -- is the new entity to add to this subprogramn spec as
1374 -- the last Extra_Formal.
1376 ----------------------
1377 -- Add_Form_To_Spec --
1378 ----------------------
1380 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1381 Sub : constant Entity_Id := Defining_Entity (S);
1385 -- Case of at least one Extra_Formal is present, set
1386 -- ARECnF as the new last entry in the list.
1388 if Present (Extra_Formals (Sub)) then
1389 Ent := Extra_Formals (Sub);
1390 while Present (Extra_Formal (Ent)) loop
1391 Ent := Extra_Formal (Ent);
1394 Set_Extra_Formal (Ent, F);
1396 -- No Extra formals present
1399 Set_Extra_Formals (Sub, F);
1400 Ent := Last_Formal (Sub);
1402 if Present (Ent) then
1403 Set_Extra_Formal (Ent, F);
1406 end Add_Form_To_Spec;
1408 -- Start of processing for Add_Extra_Formal
1411 -- Decorate the new formal entity
1413 Set_Scope (Form, STJ.Ent);
1414 Set_Ekind (Form, E_In_Parameter);
1415 Set_Etype (Form, STJE.ARECnPT);
1416 Set_Mechanism (Form, By_Copy);
1417 Set_Never_Set_In_Source (Form, True);
1418 Set_Analyzed (Form, True);
1419 Set_Comes_From_Source (Form, False);
1420 Set_Is_Activation_Record (Form, True);
1422 -- Case of only body present
1424 if Acts_As_Spec (STJ.Bod) then
1425 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1427 -- Case of separate spec
1430 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1432 end Add_Extra_Formal;
1435 -- Processing for subprograms that declare an activation record
1437 if Present (STJ.ARECn) then
1439 -- Local declarations for one such subprogram
1442 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1446 Decl_ARECnT : Node_Id;
1447 Decl_ARECnPT : Node_Id;
1448 Decl_ARECn : Node_Id;
1449 Decl_ARECnP : Node_Id;
1450 -- Declaration nodes for the AREC entities we build
1452 Decl_Assign : Node_Id;
1453 -- Assigment to set uplink, Empty if none
1456 -- List of new declarations we create
1459 -- Build list of component declarations for ARECnT
1461 Clist := Empty_List;
1463 -- If we are in a subprogram that has a static link that
1464 -- is passed in (as indicated by ARECnF being defined),
1465 -- then include ARECnU : ARECmPT where ARECmPT comes from
1466 -- the level one higher than the current level, and the
1467 -- entity ARECnPT comes from the enclosing subprogram.
1469 if Present (STJ.ARECnF) then
1472 renames Subps.Table (Enclosing_Subp (J));
1475 Make_Component_Declaration (Loc,
1476 Defining_Identifier => STJ.ARECnU,
1477 Component_Definition =>
1478 Make_Component_Definition (Loc,
1479 Subtype_Indication =>
1480 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1484 -- Add components for uplevel referenced entities
1486 if Present (STJ.Uents) then
1492 -- 1's origin of index in list of elements. This is
1493 -- used to uniquify names if needed in Upref_Name.
1496 Elmt := First_Elmt (STJ.Uents);
1498 while Present (Elmt) loop
1499 Uent := Node (Elmt);
1503 Make_Defining_Identifier (Loc,
1504 Chars => Upref_Name (Uent, Indx, Clist));
1506 Set_Activation_Record_Component
1510 Make_Component_Declaration (Loc,
1511 Defining_Identifier => Comp,
1512 Component_Definition =>
1513 Make_Component_Definition (Loc,
1514 Subtype_Indication =>
1515 New_Occurrence_Of (Addr, Loc))));
1522 -- Now we can insert the AREC declarations into the body
1524 -- type ARECnT is record .. end record;
1525 -- pragma Suppress_Initialization (ARECnT);
1527 -- Note that we need to set the Suppress_Initialization
1528 -- flag after Decl_ARECnT has been analyzed.
1531 Make_Full_Type_Declaration (Loc,
1532 Defining_Identifier => STJ.ARECnT,
1534 Make_Record_Definition (Loc,
1536 Make_Component_List (Loc,
1537 Component_Items => Clist)));
1538 Decls := New_List (Decl_ARECnT);
1540 -- type ARECnPT is access all ARECnT;
1543 Make_Full_Type_Declaration (Loc,
1544 Defining_Identifier => STJ.ARECnPT,
1546 Make_Access_To_Object_Definition (Loc,
1547 All_Present => True,
1548 Subtype_Indication =>
1549 New_Occurrence_Of (STJ.ARECnT, Loc)));
1550 Append_To (Decls, Decl_ARECnPT);
1552 -- ARECn : aliased ARECnT;
1555 Make_Object_Declaration (Loc,
1556 Defining_Identifier => STJ.ARECn,
1557 Aliased_Present => True,
1558 Object_Definition =>
1559 New_Occurrence_Of (STJ.ARECnT, Loc));
1560 Append_To (Decls, Decl_ARECn);
1562 -- ARECnP : constant ARECnPT := ARECn'Access;
1565 Make_Object_Declaration (Loc,
1566 Defining_Identifier => STJ.ARECnP,
1567 Constant_Present => True,
1568 Object_Definition =>
1569 New_Occurrence_Of (STJ.ARECnPT, Loc),
1571 Make_Attribute_Reference (Loc,
1573 New_Occurrence_Of (STJ.ARECn, Loc),
1574 Attribute_Name => Name_Access));
1575 Append_To (Decls, Decl_ARECnP);
1577 -- If we are in a subprogram that has a static link that
1578 -- is passed in (as indicated by ARECnF being defined),
1579 -- then generate ARECn.ARECmU := ARECmF where m is
1580 -- one less than the current level to set the uplink.
1582 if Present (STJ.ARECnF) then
1584 Make_Assignment_Statement (Loc,
1586 Make_Selected_Component (Loc,
1588 New_Occurrence_Of (STJ.ARECn, Loc),
1590 New_Occurrence_Of (STJ.ARECnU, Loc)),
1592 New_Occurrence_Of (STJ.ARECnF, Loc));
1593 Append_To (Decls, Decl_Assign);
1596 Decl_Assign := Empty;
1599 Prepend_List_To (Declarations (STJ.Bod), Decls);
1601 -- Analyze the newly inserted declarations. Note that we
1602 -- do not need to establish the whole scope stack, since
1603 -- we have already set all entity fields (so there will
1604 -- be no searching of upper scopes to resolve names). But
1605 -- we do set the scope of the current subprogram, so that
1606 -- newly created entities go in the right entity chain.
1608 -- We analyze with all checks suppressed (since we do
1609 -- not expect any exceptions).
1611 Push_Scope (STJ.Ent);
1612 Analyze (Decl_ARECnT, Suppress => All_Checks);
1614 -- Note that we need to call Set_Suppress_Initialization
1615 -- after Decl_ARECnT has been analyzed, but before
1616 -- analyzing Decl_ARECnP so that the flag is properly
1617 -- taking into account.
1619 Set_Suppress_Initialization (STJ.ARECnT);
1621 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1622 Analyze (Decl_ARECn, Suppress => All_Checks);
1623 Analyze (Decl_ARECnP, Suppress => All_Checks);
1625 if Present (Decl_Assign) then
1626 Analyze (Decl_Assign, Suppress => All_Checks);
1631 -- Next step, for each uplevel referenced entity, add
1632 -- assignment operations to set the component in the
1633 -- activation record.
1635 if Present (STJ.Uents) then
1640 Elmt := First_Elmt (STJ.Uents);
1641 while Present (Elmt) loop
1643 Ent : constant Entity_Id := Node (Elmt);
1644 Loc : constant Source_Ptr := Sloc (Ent);
1645 Dec : constant Node_Id :=
1646 Declaration_Node (Ent);
1651 -- For parameters, we insert the assignment
1652 -- right after the declaration of ARECnP.
1653 -- For all other entities, we insert
1654 -- the assignment immediately after
1655 -- the declaration of the entity.
1657 -- Note: we don't need to mark the entity
1658 -- as being aliased, because the address
1659 -- attribute will mark it as Address_Taken,
1660 -- and that is good enough.
1662 if Is_Formal (Ent) then
1668 -- Build and insert the assignment:
1669 -- ARECn.nam := nam'Address
1672 Make_Assignment_Statement (Loc,
1674 Make_Selected_Component (Loc,
1676 New_Occurrence_Of (STJ.ARECn, Loc),
1679 (Activation_Record_Component
1684 Make_Attribute_Reference (Loc,
1686 New_Occurrence_Of (Ent, Loc),
1687 Attribute_Name => Name_Address));
1689 Insert_After (Ins, Asn);
1691 -- Analyze the assignment statement. We do
1692 -- not need to establish the relevant scope
1693 -- stack entries here, because we have
1694 -- already set the correct entity references,
1695 -- so no name resolution is required, and no
1696 -- new entities are created, so we don't even
1697 -- need to set the current scope.
1699 -- We analyze with all checks suppressed
1700 -- (since we do not expect any exceptions).
1702 Analyze (Asn, Suppress => All_Checks);
1715 -- Next step, process uplevel references. This has to be done in a
1716 -- separate pass, after completing the processing in Sub_Loop because we
1717 -- need all the AREC declarations generated, inserted, and analyzed so
1718 -- that the uplevel references can be successfully analyzed.
1720 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
1722 UPJ : Uref_Entry renames Urefs.Table (J);
1725 -- Ignore type references, these are implicit references that do
1726 -- not need rewriting (e.g. the appearence in a conversion).
1727 -- Also ignore if no reference was specified.
1729 if Is_Type (UPJ.Ent) or else No (UPJ.Ref) then
1733 -- Also ignore uplevel references to bounds of types that come
1734 -- from the original type reference.
1736 if Is_Entity_Name (UPJ.Ref)
1737 and then Present (Entity (UPJ.Ref))
1738 and then Is_Type (Entity (UPJ.Ref))
1743 -- Rewrite one reference
1745 Rewrite_One_Ref : declare
1746 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
1747 -- Source location for the reference
1749 Typ : constant Entity_Id := Etype (UPJ.Ent);
1750 -- The type of the referenced entity
1752 Atyp : constant Entity_Id := Get_Actual_Subtype (UPJ.Ref);
1753 -- The actual subtype of the reference
1755 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
1756 -- Subp_Index for caller containing reference
1758 STJR : Subp_Entry renames Subps.Table (RS_Caller);
1759 -- Subp_Entry for subprogram containing reference
1761 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
1762 -- Subp_Index for subprogram containing referenced entity
1764 STJE : Subp_Entry renames Subps.Table (RS_Callee);
1765 -- Subp_Entry for subprogram containing referenced entity
1772 -- Ignore if no ARECnF entity for enclosing subprogram which
1773 -- probably happens as a result of not properly treating
1774 -- instance bodies. To be examined ???
1776 -- If this test is omitted, then the compilation of freeze.adb
1777 -- and inline.adb fail in unnesting mode.
1779 if No (STJR.ARECnF) then
1783 -- Push the current scope, so that the pointer type Tnn, and
1784 -- any subsidiary entities resulting from the analysis of the
1785 -- rewritten reference, go in the right entity chain.
1787 Push_Scope (STJR.Ent);
1789 -- Now we need to rewrite the reference. We have a reference
1790 -- from level STJR.Lev to level STJE.Lev. The general form of
1791 -- the rewritten reference for entity X is:
1793 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
1795 -- where a,b,c,d .. m =
1796 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
1798 pragma Assert (STJR.Lev > STJE.Lev);
1800 -- Compute the prefix of X. Here are examples to make things
1801 -- clear (with parens to show groupings, the prefix is
1802 -- everything except the .X at the end).
1804 -- level 2 to level 1
1808 -- level 3 to level 1
1810 -- (AREC2F.AREC1U).X
1812 -- level 4 to level 1
1814 -- ((AREC3F.AREC2U).AREC1U).X
1816 -- level 6 to level 2
1818 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
1820 -- In the above, ARECnF and ARECnU are pointers, so there are
1821 -- explicit dereferences required for these occurrences.
1824 Make_Explicit_Dereference (Loc,
1825 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
1827 for L in STJE.Lev .. STJR.Lev - 2 loop
1828 SI := Enclosing_Subp (SI);
1830 Make_Explicit_Dereference (Loc,
1832 Make_Selected_Component (Loc,
1835 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
1838 -- Get activation record component (must exist)
1840 Comp := Activation_Record_Component (UPJ.Ent);
1841 pragma Assert (Present (Comp));
1843 -- Do the replacement
1846 Make_Attribute_Reference (Loc,
1847 Prefix => New_Occurrence_Of (Atyp, Loc),
1848 Attribute_Name => Name_Deref,
1849 Expressions => New_List (
1850 Make_Selected_Component (Loc,
1853 New_Occurrence_Of (Comp, Loc)))));
1855 -- Analyze and resolve the new expression. We do not need to
1856 -- establish the relevant scope stack entries here, because we
1857 -- have already set all the correct entity references, so no
1858 -- name resolution is needed. We have already set the current
1859 -- scope, so that any new entities created will be in the right
1862 -- We analyze with all checks suppressed (since we do not
1863 -- expect any exceptions)
1865 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
1867 end Rewrite_One_Ref;
1872 end loop Uplev_Refs;
1874 -- Finally, loop through all calls adding extra actual for the
1875 -- activation record where it is required.
1877 Adjust_Calls : for J in Calls.First .. Calls.Last loop
1879 -- Process a single call, we are only interested in a call to a
1880 -- subprogram that actually needs a pointer to an activation record,
1881 -- as indicated by the ARECnF entity being set. This excludes the
1882 -- top level subprogram, and any subprogram not having uplevel refs.
1884 Adjust_One_Call : declare
1885 CTJ : Call_Entry renames Calls.Table (J);
1886 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
1887 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
1889 Loc : constant Source_Ptr := Sloc (CTJ.N);
1897 if Present (STT.ARECnF)
1898 and then Nkind (CTJ.N) in N_Subprogram_Call
1900 -- CTJ.N is a call to a subprogram which may require a pointer
1901 -- to an activation record. The subprogram containing the call
1902 -- is CTJ.From and the subprogram being called is CTJ.To, so we
1903 -- have a call from level STF.Lev to level STT.Lev.
1905 -- There are three possibilities:
1907 -- For a call to the same level, we just pass the activation
1908 -- record passed to the calling subprogram.
1910 if STF.Lev = STT.Lev then
1911 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1913 -- For a call that goes down a level, we pass a pointer to the
1914 -- activation record constructed within the caller (which may
1915 -- be the outer-level subprogram, but also may be a more deeply
1918 elsif STT.Lev = STF.Lev + 1 then
1919 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
1921 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
1922 -- since it is not possible to do a downcall of more than
1925 -- For a call from level STF.Lev to level STT.Lev, we
1926 -- have to find the activation record needed by the
1927 -- callee. This is as follows:
1929 -- ARECaF.ARECbU.ARECcU....ARECmU
1931 -- where a,b,c .. m =
1932 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
1935 pragma Assert (STT.Lev < STF.Lev);
1937 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
1938 SubX := Subp_Index (CTJ.Caller);
1939 for K in reverse STT.Lev .. STF.Lev - 1 loop
1940 SubX := Enclosing_Subp (SubX);
1942 Make_Selected_Component (Loc,
1946 (Subps.Table (SubX).ARECnU, Loc));
1950 -- Extra is the additional parameter to be added. Build a
1951 -- parameter association that we can append to the actuals.
1954 Make_Parameter_Association (Loc,
1956 New_Occurrence_Of (STT.ARECnF, Loc),
1957 Explicit_Actual_Parameter => Extra);
1959 if No (Parameter_Associations (CTJ.N)) then
1960 Set_Parameter_Associations (CTJ.N, Empty_List);
1963 Append (ExtraP, Parameter_Associations (CTJ.N));
1965 -- We need to deal with the actual parameter chain as well. The
1966 -- newly added parameter is always the last actual.
1968 Act := First_Named_Actual (CTJ.N);
1971 Set_First_Named_Actual (CTJ.N, Extra);
1973 -- If call has been relocated (as with an expression in
1974 -- an aggregate), set First_Named pointer in original node
1975 -- as well, because that's the parent of the parameter list.
1977 Set_First_Named_Actual
1978 (Parent (List_Containing (ExtraP)), Extra);
1980 -- Here we must follow the chain and append the new entry
1989 PAN := Parent (Act);
1990 pragma Assert (Nkind (PAN) = N_Parameter_Association);
1991 NNA := Next_Named_Actual (PAN);
1994 Set_Next_Named_Actual (PAN, Extra);
2003 -- Analyze and resolve the new actual. We do not need to
2004 -- establish the relevant scope stack entries here, because
2005 -- we have already set all the correct entity references, so
2006 -- no name resolution is needed.
2008 -- We analyze with all checks suppressed (since we do not
2009 -- expect any exceptions, and also we temporarily turn off
2010 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2011 -- references (not needed at this stage, and in fact causes
2012 -- a bit of recursive chaos).
2014 Opt.Unnest_Subprogram_Mode := False;
2016 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
2017 Opt.Unnest_Subprogram_Mode := True;
2019 end Adjust_One_Call;
2020 end loop Adjust_Calls;
2023 end Unnest_Subprogram;
2025 ------------------------
2026 -- Unnest_Subprograms --
2027 ------------------------
2029 procedure Unnest_Subprograms (N : Node_Id) is
2030 function Search_Subprograms (N : Node_Id) return Traverse_Result;
2031 -- Tree visitor that search for outer level procedures with nested
2032 -- subprograms and invokes Unnest_Subprogram()
2034 ------------------------
2035 -- Search_Subprograms --
2036 ------------------------
2038 function Search_Subprograms (N : Node_Id) return Traverse_Result is
2040 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
2042 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
2045 -- We are only interested in subprograms (not generic
2046 -- subprograms), that have nested subprograms.
2048 if Is_Subprogram (Spec_Id)
2049 and then Has_Nested_Subprogram (Spec_Id)
2050 and then Is_Library_Level_Entity (Spec_Id)
2052 Unnest_Subprogram (Spec_Id, N);
2058 end Search_Subprograms;
2064 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
2065 -- Subtree visitor instantiation
2067 -- Start of processing for Unnest_Subprograms
2070 if not Opt.Unnest_Subprogram_Mode then
2074 -- A specification will contain bodies if it contains instantiations so
2075 -- examine package or subprogram declaration of the main unit, when it
2078 if Nkind (Unit (N)) = N_Package_Body
2079 or else (Nkind (Unit (N)) = N_Subprogram_Body
2080 and then not Acts_As_Spec (N))
2082 Do_Search (Library_Unit (N));
2086 end Unnest_Subprograms;