1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2014-2016, 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 -- At least for now, do not unnest anything but main source unit
307 if not In_Extended_Main_Source_Unit
(Subp_Body
) then
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.
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;
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
347 procedure Visit
is new Traverse_Proc
(Visit_Node
);
348 -- Used to traverse the body of Subp, populating the tables
354 function Visit_Node
(N
: Node_Id
) return Traverse_Result
is
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
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
395 if Is_Entity_Name
(N
) then
396 if Present
(Entity
(N
)) then
399 Caller
=> Current_Subprogram
,
400 Callee
=> Enclosing_Subprogram
(Entity
(N
)));
405 elsif Nkind
(N
) = N_Attribute_Reference
then
406 Note_Uplevel_Bound
(Prefix
(N
));
408 end Note_Uplevel_Bound
;
410 -- Start of processing for Check_Static_Type
413 -- If already marked static, immediate return
415 if Is_Static_Type
(T
) then
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
);
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
436 LB
: constant Node_Id
:= Type_Low_Bound
(T
);
437 UB
: constant Node_Id
:= Type_High_Bound
(T
);
440 if not Is_Static_Expression
(LB
) then
441 Note_Uplevel_Bound
(LB
);
445 if not Is_Static_Expression
(UB
) then
446 Note_Uplevel_Bound
(UB
);
451 -- For record type, check all components
453 elsif Is_Record_Type
(T
) then
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
);
464 -- For array type, check index types and component type
466 elsif Is_Array_Type
(T
) then
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
);
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
);
488 -- For now, ignore other types
495 Set_Is_Static_Type
(T
);
497 end Check_Static_Type
;
499 ----------------------
500 -- Note_Uplevel_Ref --
501 ----------------------
503 procedure Note_Uplevel_Ref
509 -- Nothing to do for static type
511 if Is_Static_Type
(E
) then
515 -- Nothing to do if Caller and Callee are the same
517 if Caller
= Callee
then
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
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
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
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
566 -- Here we have a call to keep and analyze
569 -- Both caller and callee must be subprograms
571 if Is_Subprogram
(Ent
) then
572 Append_Unique_Call
((N
, Current_Subprogram
, Ent
));
577 -- Record a subprogram. We record a subprogram body that acts as
578 -- a spec. Otherwise we record a subprogram declaration, providing
579 -- that it has a corresponding body we can get hold of. The case
580 -- of no corresponding body being available is ignored for now.
582 elsif Nkind
(N
) = N_Subprogram_Body
then
583 Ent
:= Unique_Defining_Entity
(N
);
585 -- Ignore generic subprogram
587 if Is_Generic_Subprogram
(Ent
) then
591 -- Make new entry in subprogram table if not already made
594 L
: constant Nat
:= Get_Level
(Subp
, Ent
);
602 Declares_AREC
=> False,
611 Set_Subps_Index
(Ent
, UI_From_Int
(Subps
.Last
));
614 -- We make a recursive call to scan the subprogram body, so
615 -- that we can save and restore Current_Subprogram.
618 Save_CS
: constant Entity_Id
:= Current_Subprogram
;
622 Current_Subprogram
:= Ent
;
626 Decl
:= First
(Declarations
(N
));
627 while Present
(Decl
) loop
634 Visit
(Handled_Statement_Sequence
(N
));
636 -- Restore current subprogram setting
638 Current_Subprogram
:= Save_CS
;
641 -- Now at this level, return skipping the subprogram body
642 -- descendants, since we already took care of them!
646 -- Record an uplevel reference
648 elsif Nkind
(N
) in N_Has_Entity
and then Present
(Entity
(N
)) then
651 -- Only interested in entities declared within our nest
653 if not Is_Library_Level_Entity
(Ent
)
654 and then Scope_Within_Or_Same
(Scope
(Ent
), Subp
)
656 -- Skip entities defined in inlined subprograms
658 and then Chars
(Enclosing_Subprogram
(Ent
)) /= Name_uParent
661 -- Constants and variables are interesting
663 (Ekind_In
(Ent
, E_Constant
, E_Variable
)
665 -- Formals are interesting, but not if being used as mere
666 -- names of parameters for name notation calls.
671 (Nkind
(Parent
(N
)) = N_Parameter_Association
672 and then Selector_Name
(Parent
(N
)) = N
))
674 -- Types other than known Is_Static types are interesting
676 or else (Is_Type
(Ent
)
677 and then not Is_Static_Type
(Ent
)))
679 -- Here we have a possible interesting uplevel reference
681 if Is_Type
(Ent
) then
683 DT
: Boolean := False;
686 Check_Static_Type
(Ent
, DT
);
688 if Is_Static_Type
(Ent
) then
694 Caller
:= Current_Subprogram
;
695 Callee
:= Enclosing_Subprogram
(Ent
);
697 if Callee
/= Caller
and then not Is_Static_Type
(Ent
) then
698 Note_Uplevel_Ref
(Ent
, Caller
, Callee
);
702 -- If we have a body stub, visit the associated subunit
704 elsif Nkind
(N
) in N_Body_Stub
then
705 Visit
(Library_Unit
(N
));
707 -- Skip generic declarations
709 elsif Nkind
(N
) in N_Generic_Declaration
then
712 -- Skip generic package body
714 elsif Nkind
(N
) = N_Package_Body
715 and then Present
(Corresponding_Spec
(N
))
716 and then Ekind
(Corresponding_Spec
(N
)) = E_Generic_Package
721 -- Fall through to continue scanning children of this node
726 -- Start of processing for Build_Tables
729 -- Traverse the body to get subprograms, calls and uplevel references
734 -- Now do the first transitive closure which determines which
735 -- subprograms in the nest are actually reachable.
737 Reachable_Closure
: declare
741 Subps
.Table
(Subps_First
).Reachable
:= True;
743 -- We use a simple minded algorithm as follows (obviously this can
744 -- be done more efficiently, using one of the standard algorithms
745 -- for efficient transitive closure computation, but this is simple
746 -- and most likely fast enough that its speed does not matter).
748 -- Repeatedly scan the list of calls. Any time we find a call from
749 -- A to B, where A is reachable, but B is not, then B is reachable,
750 -- and note that we have made a change by setting Modified True. We
751 -- repeat this until we make a pass with no modifications.
755 Inner
: for J
in Calls
.First
.. Calls
.Last
loop
757 CTJ
: Call_Entry
renames Calls
.Table
(J
);
759 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
760 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
762 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
763 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
766 if SUBF
.Reachable
and then not SUBT
.Reachable
then
767 SUBT
.Reachable
:= True;
773 exit Outer
when not Modified
;
775 end Reachable_Closure
;
777 -- Remove calls from unreachable subprograms
784 for J
in Calls
.First
.. Calls
.Last
loop
786 CTJ
: Call_Entry
renames Calls
.Table
(J
);
788 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
789 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
791 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
792 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
795 if SUBF
.Reachable
then
796 pragma Assert
(SUBT
.Reachable
);
797 New_Index
:= New_Index
+ 1;
798 Calls
.Table
(New_Index
) := Calls
.Table
(J
);
803 Calls
.Set_Last
(New_Index
);
806 -- Remove uplevel references from unreachable subprograms
813 for J
in Urefs
.First
.. Urefs
.Last
loop
815 URJ
: Uref_Entry
renames Urefs
.Table
(J
);
817 SINF
: constant SI_Type
:= Subp_Index
(URJ
.Caller
);
818 SINT
: constant SI_Type
:= Subp_Index
(URJ
.Callee
);
820 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
821 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
826 -- Keep reachable reference
828 if SUBF
.Reachable
then
829 New_Index
:= New_Index
+ 1;
830 Urefs
.Table
(New_Index
) := Urefs
.Table
(J
);
832 -- And since we know we are keeping this one, this is a good
833 -- place to fill in information for a good reference.
835 -- Mark all enclosing subprograms need to declare AREC
839 S
:= Enclosing_Subprogram
(S
);
841 -- if we are at the top level, as can happen with
842 -- references to formals in aspects of nested subprogram
843 -- declarations, there are no further subprograms to
844 -- mark as requiring activation records.
847 Subps
.Table
(Subp_Index
(S
)).Declares_AREC
:= True;
848 exit when S
= URJ
.Callee
;
851 -- Add to list of uplevel referenced entities for Callee.
852 -- We do not add types to this list, only actual references
853 -- to objects that will be referenced uplevel, and we use
854 -- the flag Is_Uplevel_Referenced_Entity to avoid making
855 -- duplicate entries in the list.
857 if not Is_Uplevel_Referenced_Entity
(URJ
.Ent
) then
858 Set_Is_Uplevel_Referenced_Entity
(URJ
.Ent
);
860 if not Is_Type
(URJ
.Ent
) then
861 Append_New_Elmt
(URJ
.Ent
, SUBT
.Uents
);
865 -- And set uplevel indication for caller
867 if SUBT
.Lev
< SUBF
.Uplevel_Ref
then
868 SUBF
.Uplevel_Ref
:= SUBT
.Lev
;
874 Urefs
.Set_Last
(New_Index
);
877 -- Remove unreachable subprograms from Subps table. Note that we do
878 -- this after eliminating entries from the other two tables, since
879 -- those elimination steps depend on referencing the Subps table.
885 New_SI
:= Subps_First
- 1;
886 for J
in Subps_First
.. Subps
.Last
loop
888 STJ
: Subp_Entry
renames Subps
.Table
(J
);
893 -- Subprogram is reachable, copy and reset index
895 if STJ
.Reachable
then
896 New_SI
:= New_SI
+ 1;
897 Subps
.Table
(New_SI
) := STJ
;
898 Set_Subps_Index
(STJ
.Ent
, UI_From_Int
(New_SI
));
900 -- Subprogram is not reachable
903 -- Clear index, since no longer active
905 Set_Subps_Index
(Subps
.Table
(J
).Ent
, Uint_0
);
907 -- Output debug information if -gnatd.3 set
909 if Debug_Flag_Dot_3
then
910 Write_Str
("Eliminate ");
911 Write_Name
(Chars
(Subps
.Table
(J
).Ent
));
913 Write_Location
(Sloc
(Subps
.Table
(J
).Ent
));
914 Write_Str
(" (not referenced)");
918 -- Rewrite declaration and body to null statements
920 Spec
:= Corresponding_Spec
(STJ
.Bod
);
922 if Present
(Spec
) then
923 Decl
:= Parent
(Declaration_Node
(Spec
));
924 Rewrite
(Decl
, Make_Null_Statement
(Sloc
(Decl
)));
927 Rewrite
(STJ
.Bod
, Make_Null_Statement
(Sloc
(STJ
.Bod
)));
932 Subps
.Set_Last
(New_SI
);
935 -- Now it is time for the second transitive closure, which follows calls
936 -- and makes sure that A calls B, and B has uplevel references, then A
937 -- is also marked as having uplevel references.
939 Closure_Uplevel
: declare
943 -- We use a simple minded algorithm as follows (obviously this can
944 -- be done more efficiently, using one of the standard algorithms
945 -- for efficient transitive closure computation, but this is simple
946 -- and most likely fast enough that its speed does not matter).
948 -- Repeatedly scan the list of calls. Any time we find a call from
949 -- A to B, where B has uplevel references, make sure that A is marked
950 -- as having at least the same level of uplevel referencing.
954 Inner2
: for J
in Calls
.First
.. Calls
.Last
loop
956 CTJ
: Call_Entry
renames Calls
.Table
(J
);
957 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
958 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
959 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
960 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
962 if SUBT
.Lev
> SUBT
.Uplevel_Ref
963 and then SUBF
.Uplevel_Ref
> SUBT
.Uplevel_Ref
965 SUBF
.Uplevel_Ref
:= SUBT
.Uplevel_Ref
;
971 exit Outer2
when not Modified
;
975 -- We have one more step before the tables are complete. An uplevel
976 -- call from subprogram A to subprogram B where subprogram B has uplevel
977 -- references is in effect an uplevel reference, and must arrange for
978 -- the proper activation link to be passed.
980 for J
in Calls
.First
.. Calls
.Last
loop
982 CTJ
: Call_Entry
renames Calls
.Table
(J
);
984 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
985 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
987 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
988 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
993 -- If callee has uplevel references
995 if SUBT
.Uplevel_Ref
< SUBT
.Lev
997 -- And this is an uplevel call
999 and then SUBT
.Lev
< SUBF
.Lev
1001 -- We need to arrange for finding the uplink
1005 A
:= Enclosing_Subprogram
(A
);
1006 Subps
.Table
(Subp_Index
(A
)).Declares_AREC
:= True;
1007 exit when A
= CTJ
.Callee
;
1009 -- In any case exit when we get to the outer level. This
1010 -- happens in some odd cases with generics (in particular
1011 -- sem_ch3.adb does not compile without this kludge ???).
1019 -- The tables are now complete, so we can record the last index in the
1020 -- Subps table for later reference in Cprint.
1022 Subps
.Table
(Subps_First
).Last
:= Subps
.Last
;
1024 -- Next step, create the entities for code we will insert. We do this
1025 -- at the start so that all the entities are defined, regardless of the
1026 -- order in which we do the code insertions.
1028 Create_Entities
: for J
in Subps_First
.. Subps
.Last
loop
1030 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1031 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1034 -- First we create the ARECnF entity for the additional formal for
1035 -- all subprograms which need an activation record passed.
1037 if STJ
.Uplevel_Ref
< STJ
.Lev
then
1039 Make_Defining_Identifier
(Loc
, Chars
=> AREC_Name
(J
, "F"));
1042 -- Define the AREC entities for the activation record if needed
1044 if STJ
.Declares_AREC
then
1046 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, ""));
1048 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "T"));
1050 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "PT"));
1052 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "P"));
1054 -- Define uplink component entity if inner nesting case
1056 if Present
(STJ
.ARECnF
) then
1058 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "U"));
1062 end loop Create_Entities
;
1064 -- Loop through subprograms
1067 Addr
: constant Entity_Id
:= RTE
(RE_Address
);
1070 for J
in Subps_First
.. Subps
.Last
loop
1072 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1075 -- First add the extra formal if needed. This applies to all
1076 -- nested subprograms that require an activation record to be
1077 -- passed, as indicated by ARECnF being defined.
1079 if Present
(STJ
.ARECnF
) then
1081 -- Here we need the extra formal. We do the expansion and
1082 -- analysis of this manually, since it is fairly simple,
1083 -- and it is not obvious how we can get what we want if we
1084 -- try to use the normal Analyze circuit.
1086 Add_Extra_Formal
: declare
1087 Encl
: constant SI_Type
:= Enclosing_Subp
(J
);
1088 STJE
: Subp_Entry
renames Subps
.Table
(Encl
);
1089 -- Index and Subp_Entry for enclosing routine
1091 Form
: constant Entity_Id
:= STJ
.ARECnF
;
1092 -- The formal to be added. Note that n here is one less
1093 -- than the level of the subprogram itself (STJ.Ent).
1095 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
);
1096 -- S is an N_Function/Procedure_Specification node, and F
1097 -- is the new entity to add to this subprogramn spec as
1098 -- the last Extra_Formal.
1100 ----------------------
1101 -- Add_Form_To_Spec --
1102 ----------------------
1104 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
) is
1105 Sub
: constant Entity_Id
:= Defining_Entity
(S
);
1109 -- Case of at least one Extra_Formal is present, set
1110 -- ARECnF as the new last entry in the list.
1112 if Present
(Extra_Formals
(Sub
)) then
1113 Ent
:= Extra_Formals
(Sub
);
1114 while Present
(Extra_Formal
(Ent
)) loop
1115 Ent
:= Extra_Formal
(Ent
);
1118 Set_Extra_Formal
(Ent
, F
);
1120 -- No Extra formals present
1123 Set_Extra_Formals
(Sub
, F
);
1124 Ent
:= Last_Formal
(Sub
);
1126 if Present
(Ent
) then
1127 Set_Extra_Formal
(Ent
, F
);
1130 end Add_Form_To_Spec
;
1132 -- Start of processing for Add_Extra_Formal
1135 -- Decorate the new formal entity
1137 Set_Scope
(Form
, STJ
.Ent
);
1138 Set_Ekind
(Form
, E_In_Parameter
);
1139 Set_Etype
(Form
, STJE
.ARECnPT
);
1140 Set_Mechanism
(Form
, By_Copy
);
1141 Set_Never_Set_In_Source
(Form
, True);
1142 Set_Analyzed
(Form
, True);
1143 Set_Comes_From_Source
(Form
, False);
1145 -- Case of only body present
1147 if Acts_As_Spec
(STJ
.Bod
) then
1148 Add_Form_To_Spec
(Form
, Specification
(STJ
.Bod
));
1150 -- Case of separate spec
1153 Add_Form_To_Spec
(Form
, Parent
(STJ
.Ent
));
1155 end Add_Extra_Formal
;
1158 -- Processing for subprograms that declare an activation record
1160 if Present
(STJ
.ARECn
) then
1162 -- Local declarations for one such subprogram
1165 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1169 Decl_ARECnT
: Node_Id
;
1170 Decl_ARECnPT
: Node_Id
;
1171 Decl_ARECn
: Node_Id
;
1172 Decl_ARECnP
: Node_Id
;
1173 -- Declaration nodes for the AREC entities we build
1175 Decl_Assign
: Node_Id
;
1176 -- Assigment to set uplink, Empty if none
1179 -- List of new declarations we create
1182 -- Build list of component declarations for ARECnT
1184 Clist
:= Empty_List
;
1186 -- If we are in a subprogram that has a static link that
1187 -- is passed in (as indicated by ARECnF being defined),
1188 -- then include ARECnU : ARECmPT where ARECmPT comes from
1189 -- the level one higher than the current level, and the
1190 -- entity ARECnPT comes from the enclosing subprogram.
1192 if Present
(STJ
.ARECnF
) then
1195 renames Subps
.Table
(Enclosing_Subp
(J
));
1198 Make_Component_Declaration
(Loc
,
1199 Defining_Identifier
=> STJ
.ARECnU
,
1200 Component_Definition
=>
1201 Make_Component_Definition
(Loc
,
1202 Subtype_Indication
=>
1203 New_Occurrence_Of
(STJE
.ARECnPT
, Loc
))));
1207 -- Add components for uplevel referenced entities
1209 if Present
(STJ
.Uents
) then
1215 -- 1's origin of index in list of elements. This is
1216 -- used to uniquify names if needed in Upref_Name.
1219 Elmt
:= First_Elmt
(STJ
.Uents
);
1221 while Present
(Elmt
) loop
1222 Uent
:= Node
(Elmt
);
1226 Make_Defining_Identifier
(Loc
,
1227 Chars
=> Upref_Name
(Uent
, Indx
, Clist
));
1229 Set_Activation_Record_Component
1233 Make_Component_Declaration
(Loc
,
1234 Defining_Identifier
=> Comp
,
1235 Component_Definition
=>
1236 Make_Component_Definition
(Loc
,
1237 Subtype_Indication
=>
1238 New_Occurrence_Of
(Addr
, Loc
))));
1245 -- Now we can insert the AREC declarations into the body
1247 -- type ARECnT is record .. end record;
1248 -- pragma Suppress_Initialization (ARECnT);
1250 -- Note that we need to set the Suppress_Initialization
1251 -- flag after Decl_ARECnT has been analyzed.
1254 Make_Full_Type_Declaration
(Loc
,
1255 Defining_Identifier
=> STJ
.ARECnT
,
1257 Make_Record_Definition
(Loc
,
1259 Make_Component_List
(Loc
,
1260 Component_Items
=> Clist
)));
1261 Decls
:= New_List
(Decl_ARECnT
);
1263 -- type ARECnPT is access all ARECnT;
1266 Make_Full_Type_Declaration
(Loc
,
1267 Defining_Identifier
=> STJ
.ARECnPT
,
1269 Make_Access_To_Object_Definition
(Loc
,
1270 All_Present
=> True,
1271 Subtype_Indication
=>
1272 New_Occurrence_Of
(STJ
.ARECnT
, Loc
)));
1273 Append_To
(Decls
, Decl_ARECnPT
);
1275 -- ARECn : aliased ARECnT;
1278 Make_Object_Declaration
(Loc
,
1279 Defining_Identifier
=> STJ
.ARECn
,
1280 Aliased_Present
=> True,
1281 Object_Definition
=>
1282 New_Occurrence_Of
(STJ
.ARECnT
, Loc
));
1283 Append_To
(Decls
, Decl_ARECn
);
1285 -- ARECnP : constant ARECnPT := ARECn'Access;
1288 Make_Object_Declaration
(Loc
,
1289 Defining_Identifier
=> STJ
.ARECnP
,
1290 Constant_Present
=> True,
1291 Object_Definition
=>
1292 New_Occurrence_Of
(STJ
.ARECnPT
, Loc
),
1294 Make_Attribute_Reference
(Loc
,
1296 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1297 Attribute_Name
=> Name_Access
));
1298 Append_To
(Decls
, Decl_ARECnP
);
1300 -- If we are in a subprogram that has a static link that
1301 -- is passed in (as indicated by ARECnF being defined),
1302 -- then generate ARECn.ARECmU := ARECmF where m is
1303 -- one less than the current level to set the uplink.
1305 if Present
(STJ
.ARECnF
) then
1307 Make_Assignment_Statement
(Loc
,
1309 Make_Selected_Component
(Loc
,
1311 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1313 New_Occurrence_Of
(STJ
.ARECnU
, Loc
)),
1315 New_Occurrence_Of
(STJ
.ARECnF
, Loc
));
1316 Append_To
(Decls
, Decl_Assign
);
1319 Decl_Assign
:= Empty
;
1322 Prepend_List_To
(Declarations
(STJ
.Bod
), Decls
);
1324 -- Analyze the newly inserted declarations. Note that we
1325 -- do not need to establish the whole scope stack, since
1326 -- we have already set all entity fields (so there will
1327 -- be no searching of upper scopes to resolve names). But
1328 -- we do set the scope of the current subprogram, so that
1329 -- newly created entities go in the right entity chain.
1331 -- We analyze with all checks suppressed (since we do
1332 -- not expect any exceptions).
1334 Push_Scope
(STJ
.Ent
);
1335 Analyze
(Decl_ARECnT
, Suppress
=> All_Checks
);
1337 -- Note that we need to call Set_Suppress_Initialization
1338 -- after Decl_ARECnT has been analyzed, but before
1339 -- analyzing Decl_ARECnP so that the flag is properly
1340 -- taking into account.
1342 Set_Suppress_Initialization
(STJ
.ARECnT
);
1344 Analyze
(Decl_ARECnPT
, Suppress
=> All_Checks
);
1345 Analyze
(Decl_ARECn
, Suppress
=> All_Checks
);
1346 Analyze
(Decl_ARECnP
, Suppress
=> All_Checks
);
1348 if Present
(Decl_Assign
) then
1349 Analyze
(Decl_Assign
, Suppress
=> All_Checks
);
1354 -- Next step, for each uplevel referenced entity, add
1355 -- assignment operations to set the component in the
1356 -- activation record.
1358 if Present
(STJ
.Uents
) then
1363 Elmt
:= First_Elmt
(STJ
.Uents
);
1364 while Present
(Elmt
) loop
1366 Ent
: constant Entity_Id
:= Node
(Elmt
);
1367 Loc
: constant Source_Ptr
:= Sloc
(Ent
);
1368 Dec
: constant Node_Id
:=
1369 Declaration_Node
(Ent
);
1374 -- For parameters, we insert the assignment
1375 -- right after the declaration of ARECnP.
1376 -- For all other entities, we insert
1377 -- the assignment immediately after
1378 -- the declaration of the entity.
1380 -- Note: we don't need to mark the entity
1381 -- as being aliased, because the address
1382 -- attribute will mark it as Address_Taken,
1383 -- and that is good enough.
1385 if Is_Formal
(Ent
) then
1391 -- Build and insert the assignment:
1392 -- ARECn.nam := nam'Address
1395 Make_Assignment_Statement
(Loc
,
1397 Make_Selected_Component
(Loc
,
1399 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1402 (Activation_Record_Component
1407 Make_Attribute_Reference
(Loc
,
1409 New_Occurrence_Of
(Ent
, Loc
),
1410 Attribute_Name
=> Name_Address
));
1412 Insert_After
(Ins
, Asn
);
1414 -- Analyze the assignment statement. We do
1415 -- not need to establish the relevant scope
1416 -- stack entries here, because we have
1417 -- already set the correct entity references,
1418 -- so no name resolution is required, and no
1419 -- new entities are created, so we don't even
1420 -- need to set the current scope.
1422 -- We analyze with all checks suppressed
1423 -- (since we do not expect any exceptions).
1425 Analyze
(Asn
, Suppress
=> All_Checks
);
1438 -- Next step, process uplevel references. This has to be done in a
1439 -- separate pass, after completing the processing in Sub_Loop because we
1440 -- need all the AREC declarations generated, inserted, and analyzed so
1441 -- that the uplevel references can be successfully analyzed.
1443 Uplev_Refs
: for J
in Urefs
.First
.. Urefs
.Last
loop
1445 UPJ
: Uref_Entry
renames Urefs
.Table
(J
);
1448 -- Ignore type references, these are implicit references that do
1449 -- not need rewriting (e.g. the appearence in a conversion).
1451 if Is_Type
(UPJ
.Ent
) then
1455 -- Also ignore uplevel references to bounds of types that come
1456 -- from the original type reference.
1458 if Is_Entity_Name
(UPJ
.Ref
)
1459 and then Present
(Entity
(UPJ
.Ref
))
1460 and then Is_Type
(Entity
(UPJ
.Ref
))
1465 -- Rewrite one reference
1467 Rewrite_One_Ref
: declare
1468 Loc
: constant Source_Ptr
:= Sloc
(UPJ
.Ref
);
1469 -- Source location for the reference
1471 Typ
: constant Entity_Id
:= Etype
(UPJ
.Ent
);
1472 -- The type of the referenced entity
1474 Atyp
: constant Entity_Id
:= Get_Actual_Subtype
(UPJ
.Ref
);
1475 -- The actual subtype of the reference
1477 RS_Caller
: constant SI_Type
:= Subp_Index
(UPJ
.Caller
);
1478 -- Subp_Index for caller containing reference
1480 STJR
: Subp_Entry
renames Subps
.Table
(RS_Caller
);
1481 -- Subp_Entry for subprogram containing reference
1483 RS_Callee
: constant SI_Type
:= Subp_Index
(UPJ
.Callee
);
1484 -- Subp_Index for subprogram containing referenced entity
1486 STJE
: Subp_Entry
renames Subps
.Table
(RS_Callee
);
1487 -- Subp_Entry for subprogram containing referenced entity
1494 -- Ignore if no ARECnF entity for enclosing subprogram which
1495 -- probably happens as a result of not properly treating
1496 -- instance bodies. To be examined ???
1498 -- If this test is omitted, then the compilation of freeze.adb
1499 -- and inline.adb fail in unnesting mode.
1501 if No
(STJR
.ARECnF
) then
1505 -- Push the current scope, so that the pointer type Tnn, and
1506 -- any subsidiary entities resulting from the analysis of the
1507 -- rewritten reference, go in the right entity chain.
1509 Push_Scope
(STJR
.Ent
);
1511 -- Now we need to rewrite the reference. We have a reference
1512 -- from level STJR.Lev to level STJE.Lev. The general form of
1513 -- the rewritten reference for entity X is:
1515 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECm.X)
1517 -- where a,b,c,d .. m =
1518 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
1520 pragma Assert
(STJR
.Lev
> STJE
.Lev
);
1522 -- Compute the prefix of X. Here are examples to make things
1523 -- clear (with parens to show groupings, the prefix is
1524 -- everything except the .X at the end).
1526 -- level 2 to level 1
1530 -- level 3 to level 1
1532 -- (AREC2F.AREC1U).X
1534 -- level 4 to level 1
1536 -- ((AREC3F.AREC2U).AREC1U).X
1538 -- level 6 to level 2
1540 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
1542 -- In the above, ARECnF and ARECnU are pointers, so there are
1543 -- explicit dereferences required for these occurrences.
1546 Make_Explicit_Dereference
(Loc
,
1547 Prefix
=> New_Occurrence_Of
(STJR
.ARECnF
, Loc
));
1549 for L
in STJE
.Lev
.. STJR
.Lev
- 2 loop
1550 SI
:= Enclosing_Subp
(SI
);
1552 Make_Explicit_Dereference
(Loc
,
1554 Make_Selected_Component
(Loc
,
1557 New_Occurrence_Of
(Subps
.Table
(SI
).ARECnU
, Loc
)));
1560 -- Get activation record component (must exist)
1562 Comp
:= Activation_Record_Component
(UPJ
.Ent
);
1563 pragma Assert
(Present
(Comp
));
1565 -- Do the replacement
1568 Make_Attribute_Reference
(Loc
,
1569 Prefix
=> New_Occurrence_Of
(Atyp
, Loc
),
1570 Attribute_Name
=> Name_Deref
,
1571 Expressions
=> New_List
(
1572 Make_Selected_Component
(Loc
,
1575 New_Occurrence_Of
(Comp
, Loc
)))));
1577 -- Analyze and resolve the new expression. We do not need to
1578 -- establish the relevant scope stack entries here, because we
1579 -- have already set all the correct entity references, so no
1580 -- name resolution is needed. We have already set the current
1581 -- scope, so that any new entities created will be in the right
1584 -- We analyze with all checks suppressed (since we do not
1585 -- expect any exceptions)
1587 Analyze_And_Resolve
(UPJ
.Ref
, Typ
, Suppress
=> All_Checks
);
1589 end Rewrite_One_Ref
;
1594 end loop Uplev_Refs
;
1596 -- Finally, loop through all calls adding extra actual for the
1597 -- activation record where it is required.
1599 Adjust_Calls
: for J
in Calls
.First
.. Calls
.Last
loop
1601 -- Process a single call, we are only interested in a call to a
1602 -- subprogram that actually needs a pointer to an activation record,
1603 -- as indicated by the ARECnF entity being set. This excludes the
1604 -- top level subprogram, and any subprogram not having uplevel refs.
1606 Adjust_One_Call
: declare
1607 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1608 STF
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Caller
));
1609 STT
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Callee
));
1611 Loc
: constant Source_Ptr
:= Sloc
(CTJ
.N
);
1619 if Present
(STT
.ARECnF
) then
1621 -- CTJ.N is a call to a subprogram which may require a pointer
1622 -- to an activation record. The subprogram containing the call
1623 -- is CTJ.From and the subprogram being called is CTJ.To, so we
1624 -- have a call from level STF.Lev to level STT.Lev.
1626 -- There are three possibilities:
1628 -- For a call to the same level, we just pass the activation
1629 -- record passed to the calling subprogram.
1631 if STF
.Lev
= STT
.Lev
then
1632 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
1634 -- For a call that goes down a level, we pass a pointer to the
1635 -- activation record constructed within the caller (which may
1636 -- be the outer-level subprogram, but also may be a more deeply
1639 elsif STT
.Lev
= STF
.Lev
+ 1 then
1640 Extra
:= New_Occurrence_Of
(STF
.ARECnP
, Loc
);
1642 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
1643 -- since it is not possible to do a downcall of more than
1646 -- For a call from level STF.Lev to level STT.Lev, we
1647 -- have to find the activation record needed by the
1648 -- callee. This is as follows:
1650 -- ARECaF.ARECbU.ARECcU....ARECm
1652 -- where a,b,c .. m =
1653 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
1656 pragma Assert
(STT
.Lev
< STF
.Lev
);
1658 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
1659 SubX
:= Subp_Index
(CTJ
.Caller
);
1660 for K
in reverse STT
.Lev
.. STF
.Lev
- 1 loop
1661 SubX
:= Enclosing_Subp
(SubX
);
1663 Make_Selected_Component
(Loc
,
1667 (Subps
.Table
(SubX
).ARECnU
, Loc
));
1671 -- Extra is the additional parameter to be added. Build a
1672 -- parameter association that we can append to the actuals.
1675 Make_Parameter_Association
(Loc
,
1677 New_Occurrence_Of
(STT
.ARECnF
, Loc
),
1678 Explicit_Actual_Parameter
=> Extra
);
1680 if No
(Parameter_Associations
(CTJ
.N
)) then
1681 Set_Parameter_Associations
(CTJ
.N
, Empty_List
);
1684 Append
(ExtraP
, Parameter_Associations
(CTJ
.N
));
1686 -- We need to deal with the actual parameter chain as well. The
1687 -- newly added parameter is always the last actual.
1689 Act
:= First_Named_Actual
(CTJ
.N
);
1692 Set_First_Named_Actual
(CTJ
.N
, Extra
);
1694 -- Here we must follow the chain and append the new entry
1703 PAN
:= Parent
(Act
);
1704 pragma Assert
(Nkind
(PAN
) = N_Parameter_Association
);
1705 NNA
:= Next_Named_Actual
(PAN
);
1708 Set_Next_Named_Actual
(PAN
, Extra
);
1717 -- Analyze and resolve the new actual. We do not need to
1718 -- establish the relevant scope stack entries here, because
1719 -- we have already set all the correct entity references, so
1720 -- no name resolution is needed.
1722 -- We analyze with all checks suppressed (since we do not
1723 -- expect any exceptions, and also we temporarily turn off
1724 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
1725 -- references (not needed at this stage, and in fact causes
1726 -- a bit of recursive chaos).
1728 Opt
.Unnest_Subprogram_Mode
:= False;
1730 (Extra
, Etype
(STT
.ARECnF
), Suppress
=> All_Checks
);
1731 Opt
.Unnest_Subprogram_Mode
:= True;
1733 end Adjust_One_Call
;
1734 end loop Adjust_Calls
;
1737 end Unnest_Subprogram
;
1739 ------------------------
1740 -- Unnest_Subprograms --
1741 ------------------------
1743 procedure Unnest_Subprograms
(N
: Node_Id
) is
1744 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
;
1745 -- Tree visitor that search for outer level procedures with nested
1746 -- subprograms and invokes Unnest_Subprogram()
1748 ------------------------
1749 -- Search_Subprograms --
1750 ------------------------
1752 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
is
1754 if Nkind_In
(N
, N_Subprogram_Body
, N_Subprogram_Body_Stub
) then
1756 Spec_Id
: constant Entity_Id
:= Unique_Defining_Entity
(N
);
1759 -- We are only interested in subprograms (not generic
1760 -- subprograms), that have nested subprograms.
1762 if Is_Subprogram
(Spec_Id
)
1763 and then Has_Nested_Subprogram
(Spec_Id
)
1764 and then Is_Library_Level_Entity
(Spec_Id
)
1766 Unnest_Subprogram
(Spec_Id
, N
);
1772 end Search_Subprograms
;
1778 procedure Do_Search
is new Traverse_Proc
(Search_Subprograms
);
1779 -- Subtree visitor instantiation
1781 -- Start of processing for Unnest_Subprograms
1784 if not Opt
.Unnest_Subprogram_Mode
then
1789 end Unnest_Subprograms
;