1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2014-2023, 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 Einfo
.Entities
; use Einfo
.Entities
;
30 with Einfo
.Utils
; use Einfo
.Utils
;
31 with Elists
; use Elists
;
32 with Exp_Util
; use Exp_Util
;
34 with Namet
; use Namet
;
35 with Nlists
; use Nlists
;
36 with Nmake
; use Nmake
;
38 with Output
; use Output
;
39 with Rtsfind
; use Rtsfind
;
41 with Sem_Aux
; use Sem_Aux
;
42 with Sem_Ch8
; use Sem_Ch8
;
43 with Sem_Mech
; use Sem_Mech
;
44 with Sem_Res
; use Sem_Res
;
45 with Sem_Util
; use Sem_Util
;
46 with Sinfo
; use Sinfo
;
47 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
48 with Sinfo
.Utils
; use Sinfo
.Utils
;
49 with Sinput
; use Sinput
;
50 with Snames
; use Snames
;
51 with Stand
; use Stand
;
52 with Tbuild
; use Tbuild
;
53 with Uintp
; use Uintp
;
55 package body Exp_Unst
is
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 procedure Unnest_Subprogram
62 (Subp
: Entity_Id
; Subp_Body
: Node_Id
; For_Inline
: Boolean := False);
63 -- Subp is a library-level subprogram which has nested subprograms, and
64 -- Subp_Body is the corresponding N_Subprogram_Body node. This procedure
65 -- declares the AREC types and objects, adds assignments to the AREC record
66 -- as required, defines the xxxPTR types for uplevel referenced objects,
67 -- adds the ARECP parameter to all nested subprograms which need it, and
68 -- modifies all uplevel references appropriately. If For_Inline is True,
69 -- we're unnesting this subprogram because it's on the list of inlined
70 -- subprograms and should unnest it despite it not being part of the main
77 -- Table to record calls within the nest being analyzed. These are the
78 -- calls which may need to have an AREC actual added. This table is built
79 -- new for each subprogram nest and cleared at the end of processing each
82 type Call_Entry
is record
87 -- Entity of the subprogram containing the call (can be at any level)
90 -- Entity of the subprogram called (always at level 2 or higher). Note
91 -- that in accordance with the basic rules of nesting, the level of To
92 -- is either less than or equal to the level of From, or one greater.
95 package Calls
is new Table
.Table
(
96 Table_Component_Type
=> Call_Entry
,
97 Table_Index_Type
=> Nat
,
100 Table_Increment
=> 200,
101 Table_Name
=> "Unnest_Calls");
102 -- Records each call within the outer subprogram and all nested subprograms
103 -- that are to other subprograms nested within the outer subprogram. These
104 -- are the calls that may need an additional parameter.
106 procedure Append_Unique_Call
(Call
: Call_Entry
);
107 -- Append a call entry to the Calls table. A check is made to see if the
108 -- table already contains this entry and if so it has no effect.
110 ----------------------------------
111 -- Subprograms For Fat Pointers --
112 ----------------------------------
114 function Build_Access_Type_Decl
116 Scop
: Entity_Id
) return Node_Id
;
117 -- For an uplevel reference that involves an unconstrained array type,
118 -- build an access type declaration for the corresponding activation
119 -- record component. The relevant attributes of the access type are
120 -- set here to avoid a full analysis that would require a scope stack.
122 function Needs_Fat_Pointer
(E
: Entity_Id
) return Boolean;
123 -- A formal parameter of an unconstrained array type that appears in an
124 -- uplevel reference requires the construction of an access type, to be
125 -- used in the corresponding component declaration.
131 -- Table to record explicit uplevel references to objects (variables,
132 -- constants, formal parameters). These are the references that will
133 -- need rewriting to use the activation table (AREC) pointers. Also
134 -- included are implicit and explicit uplevel references to types, but
135 -- these do not get rewritten by the front end. This table is built new
136 -- for each subprogram nest and cleared at the end of processing each
139 type Uref_Entry
is record
141 -- The reference itself. For objects this is always an entity reference
142 -- and the referenced entity will have its Is_Uplevel_Referenced_Entity
143 -- flag set and will appear in the Uplevel_Referenced_Entities list of
144 -- the subprogram declaring this entity.
147 -- The Entity_Id of the uplevel referenced object or type
150 -- The entity for the subprogram immediately containing this entity
153 -- The entity for the subprogram containing the referenced entity. Note
154 -- that the level of Callee must be less than the level of Caller, since
155 -- this is an uplevel reference.
158 package Urefs
is new Table
.Table
(
159 Table_Component_Type
=> Uref_Entry
,
160 Table_Index_Type
=> Nat
,
161 Table_Low_Bound
=> 1,
162 Table_Initial
=> 100,
163 Table_Increment
=> 200,
164 Table_Name
=> "Unnest_Urefs");
166 ------------------------
167 -- Append_Unique_Call --
168 ------------------------
170 procedure Append_Unique_Call
(Call
: Call_Entry
) is
172 for J
in Calls
.First
.. Calls
.Last
loop
173 if Calls
.Table
(J
) = Call
then
179 end Append_Unique_Call
;
181 -----------------------------
182 -- Build_Access_Type_Decl --
183 -----------------------------
185 function Build_Access_Type_Decl
187 Scop
: Entity_Id
) return Node_Id
189 Loc
: constant Source_Ptr
:= Sloc
(E
);
193 Typ
:= Make_Temporary
(Loc
, 'S');
194 Mutate_Ekind
(Typ
, E_General_Access_Type
);
195 Set_Etype
(Typ
, Typ
);
196 Set_Scope
(Typ
, Scop
);
197 Set_Directly_Designated_Type
(Typ
, Etype
(E
));
200 Make_Full_Type_Declaration
(Loc
,
201 Defining_Identifier
=> Typ
,
203 Make_Access_To_Object_Definition
(Loc
,
204 Subtype_Indication
=> New_Occurrence_Of
(Etype
(E
), Loc
)));
205 end Build_Access_Type_Decl
;
211 function Get_Level
(Subp
: Entity_Id
; Sub
: Entity_Id
) return Nat
is
223 S
:= Enclosing_Subprogram
(S
);
228 --------------------------
229 -- In_Synchronized_Unit --
230 --------------------------
232 function In_Synchronized_Unit
(Subp
: Entity_Id
) return Boolean is
233 S
: Entity_Id
:= Scope
(Subp
);
236 while Present
(S
) and then S
/= Standard_Standard
loop
237 if Is_Concurrent_Type
(S
) then
240 elsif Is_Private_Type
(S
)
241 and then Present
(Full_View
(S
))
242 and then Is_Concurrent_Type
(Full_View
(S
))
251 end In_Synchronized_Unit
;
253 -----------------------
254 -- Needs_Fat_Pointer --
255 -----------------------
257 function Needs_Fat_Pointer
(E
: Entity_Id
) return Boolean is
258 Typ
: constant Entity_Id
:= Get_Fullest_View
(Etype
(E
));
260 return Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
);
261 end Needs_Fat_Pointer
;
267 function Subp_Index
(Sub
: Entity_Id
) return SI_Type
is
268 E
: Entity_Id
:= Sub
;
271 pragma Assert
(Is_Subprogram
(E
));
273 if Field_Is_Initial_Zero
(E
, F_Subps_Index
)
274 or else Subps_Index
(E
) = Uint_0
276 E
:= Ultimate_Alias
(E
);
278 -- The body of a protected operation has a different name and
279 -- has been scanned at this point, and thus has an entry in the
282 if E
= Sub
and then Present
(Protected_Body_Subprogram
(E
)) then
283 E
:= Protected_Body_Subprogram
(E
);
286 if Ekind
(E
) = E_Function
287 and then Rewritten_For_C
(E
)
288 and then Present
(Corresponding_Procedure
(E
))
290 E
:= Corresponding_Procedure
(E
);
294 pragma Assert
(Subps_Index
(E
) /= Uint_0
);
295 return SI_Type
(UI_To_Int
(Subps_Index
(E
)));
298 -----------------------
299 -- Unnest_Subprogram --
300 -----------------------
302 procedure Unnest_Subprogram
303 (Subp
: Entity_Id
; Subp_Body
: Node_Id
; For_Inline
: Boolean := False) is
304 function AREC_Name
(J
: Pos
; S
: String) return Name_Id
;
305 -- Returns name for string ARECjS, where j is the decimal value of j
307 function Enclosing_Subp
(Subp
: SI_Type
) return SI_Type
;
308 -- Subp is the index of a subprogram which has a Lev greater than 1.
309 -- This function returns the index of the enclosing subprogram which
310 -- will have a Lev value one less than this.
312 function Img_Pos
(N
: Pos
) return String;
313 -- Return image of N without leading blank
318 Clist
: List_Id
) return Name_Id
;
319 -- This function returns the name to be used in the activation record to
320 -- reference the variable uplevel. Clist is the list of components that
321 -- have been created in the activation record so far. Normally the name
322 -- is just a copy of the Chars field of the entity. The exception is
323 -- when the name has already been used, in which case we suffix the name
324 -- with the index value Index to avoid duplication. This happens with
325 -- declare blocks and generic parameters at least.
331 function AREC_Name
(J
: Pos
; S
: String) return Name_Id
is
333 return Name_Find
("AREC" & Img_Pos
(J
) & S
);
340 function Enclosing_Subp
(Subp
: SI_Type
) return SI_Type
is
341 STJ
: Subp_Entry
renames Subps
.Table
(Subp
);
342 Ret
: constant SI_Type
:= Subp_Index
(Enclosing_Subprogram
(STJ
.Ent
));
344 pragma Assert
(STJ
.Lev
> 1);
345 pragma Assert
(Subps
.Table
(Ret
).Lev
= STJ
.Lev
- 1);
353 function Img_Pos
(N
: Pos
) return String is
354 Buf
: String (1 .. 20);
362 Buf
(Ptr
) := Character'Val (48 + NV
mod 10);
367 return Buf
(Ptr
+ 1 .. Buf
'Last);
377 Clist
: List_Id
) return Name_Id
386 elsif Chars
(Defining_Identifier
(C
)) = Chars
(Ent
) then
388 Name_Find
(Get_Name_String
(Chars
(Ent
)) & Img_Pos
(Index
));
395 -- Start of processing for Unnest_Subprogram
398 -- Nothing to do inside a generic (all processing is for instance)
400 if Inside_A_Generic
then
404 -- If the main unit is a package body then we need to examine the spec
405 -- to determine whether the main unit is generic (the scope stack is not
406 -- present when this is called on the main unit).
409 and then Ekind
(Cunit_Entity
(Main_Unit
)) = E_Package_Body
410 and then Is_Generic_Unit
(Spec_Entity
(Cunit_Entity
(Main_Unit
)))
414 -- Only unnest when generating code for the main source unit or if
415 -- we're unnesting for inline. But in some Annex E cases the Sloc
416 -- points to a different unit, so also make sure that the Parent
417 -- isn't in something that we know we're generating code for.
420 and then not In_Extended_Main_Code_Unit
(Subp_Body
)
421 and then not In_Extended_Main_Code_Unit
(Parent
(Subp_Body
))
426 -- This routine is called late, after the scope stack is gone. The
427 -- following creates a suitable dummy scope stack to be used for the
428 -- analyze/expand calls made from this routine.
432 -- First step, we must mark all nested subprograms that require a static
433 -- link (activation record) because either they contain explicit uplevel
434 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
435 -- this point), or they make calls to other subprograms in the same nest
436 -- that require a static link (in which case we set this flag).
438 -- This is a recursive definition, and to implement this, we have to
439 -- build a call graph for the set of nested subprograms, and then go
440 -- over this graph to implement recursively the invariant that if a
441 -- subprogram has a call to a subprogram requiring a static link, then
442 -- the calling subprogram requires a static link.
444 -- First populate the above tables
446 Subps_First
:= Subps
.Last
+ 1;
450 Build_Tables
: declare
451 Current_Subprogram
: Entity_Id
:= Empty
;
452 -- When we scan a subprogram body, we set Current_Subprogram to the
453 -- corresponding entity. This gets recursively saved and restored.
455 function Visit_Node
(N
: Node_Id
) return Traverse_Result
;
456 -- Visit a single node in Subp
462 procedure Visit
is new Traverse_Proc
(Visit_Node
);
463 -- Used to traverse the body of Subp, populating the tables
469 function Visit_Node
(N
: Node_Id
) return Traverse_Result
is
474 procedure Check_Static_Type
478 Check_Designated
: Boolean := False);
479 -- Given a type In_T, checks if it is a static type defined as
480 -- a type with no dynamic bounds in sight. If so, the only
481 -- action is to set Is_Static_Type True for In_T. If In_T is
482 -- not a static type, then all types with dynamic bounds
483 -- associated with In_T are detected, and their bounds are
484 -- marked as uplevel referenced if not at the library level,
485 -- and DT is set True. If N is specified, it's the node that
486 -- will need to be replaced. If not specified, it means we
487 -- can't do a replacement because the bound is implicit.
489 -- If Check_Designated is True and In_T or its full view
490 -- is an access type, check whether the designated type
491 -- has dynamic bounds.
493 procedure Note_Uplevel_Ref
498 -- Called when we detect an explicit or implicit uplevel reference
499 -- from within Caller to entity E declared in Callee. E can be a
500 -- an object or a type.
502 procedure Register_Subprogram
(E
: Entity_Id
; Bod
: Node_Id
);
503 -- Enter a subprogram whose body is visible or which is a
504 -- subprogram instance into the subprogram table.
506 -----------------------
507 -- Check_Static_Type --
508 -----------------------
510 procedure Check_Static_Type
514 Check_Designated
: Boolean := False)
516 T
: constant Entity_Id
:= Get_Fullest_View
(In_T
);
518 procedure Note_Uplevel_Bound
(N
: Node_Id
; Ref
: Node_Id
);
519 -- N is the bound of a dynamic type. This procedure notes that
520 -- this bound is uplevel referenced, it can handle references
521 -- to entities (typically _FIRST and _LAST entities), and also
522 -- attribute references of the form T'name (name is typically
523 -- FIRST or LAST) where T is the uplevel referenced bound.
524 -- Ref, if Present, is the location of the reference to
527 ------------------------
528 -- Note_Uplevel_Bound --
529 ------------------------
531 procedure Note_Uplevel_Bound
(N
: Node_Id
; Ref
: Node_Id
) is
533 -- Entity name case. Make sure that the entity is declared
534 -- in a subprogram. This may not be the case for a type in a
535 -- loop appearing in a precondition.
536 -- Exclude explicitly discriminants (that can appear
537 -- in bounds of discriminated components) and enumeration
540 if Is_Entity_Name
(N
) then
541 if Present
(Entity
(N
))
542 and then not Is_Type
(Entity
(N
))
543 and then Present
(Enclosing_Subprogram
(Entity
(N
)))
546 not in E_Discriminant | E_Enumeration_Literal
551 Caller
=> Current_Subprogram
,
552 Callee
=> Enclosing_Subprogram
(Entity
(N
)));
555 -- Attribute or indexed component case
558 N_Attribute_Reference | N_Indexed_Component
560 Note_Uplevel_Bound
(Prefix
(N
), Ref
);
562 -- The indices of the indexed components, or the
563 -- associated expressions of an attribute reference,
564 -- may also involve uplevel references.
570 Expr
:= First
(Expressions
(N
));
571 while Present
(Expr
) loop
572 Note_Uplevel_Bound
(Expr
, Ref
);
577 -- The type of the prefix may be have an uplevel
578 -- reference if this needs bounds.
580 if Nkind
(N
) = N_Attribute_Reference
then
582 Attr
: constant Attribute_Id
:=
583 Get_Attribute_Id
(Attribute_Name
(N
));
584 DT
: Boolean := False;
587 if (Attr
= Attribute_First
588 or else Attr
= Attribute_Last
589 or else Attr
= Attribute_Length
)
590 and then Is_Constrained
(Etype
(Prefix
(N
)))
593 (Etype
(Prefix
(N
)), Empty
, DT
);
598 -- Binary operator cases. These can apply to arrays for
599 -- which we may need bounds.
601 elsif Nkind
(N
) in N_Binary_Op
then
602 Note_Uplevel_Bound
(Left_Opnd
(N
), Ref
);
603 Note_Uplevel_Bound
(Right_Opnd
(N
), Ref
);
605 -- Unary operator case
607 elsif Nkind
(N
) in N_Unary_Op
then
608 Note_Uplevel_Bound
(Right_Opnd
(N
), Ref
);
610 -- Explicit dereference and selected component case
613 N_Explicit_Dereference | N_Selected_Component
615 Note_Uplevel_Bound
(Prefix
(N
), Ref
);
617 -- Conditional expressions
619 elsif Nkind
(N
) = N_If_Expression
then
624 Expr
:= First
(Expressions
(N
));
625 while Present
(Expr
) loop
626 Note_Uplevel_Bound
(Expr
, Ref
);
631 elsif Nkind
(N
) = N_Case_Expression
then
633 Alternative
: Node_Id
;
636 Note_Uplevel_Bound
(Expression
(N
), Ref
);
638 Alternative
:= First
(Alternatives
(N
));
639 while Present
(Alternative
) loop
640 Note_Uplevel_Bound
(Expression
(Alternative
), Ref
);
646 elsif Nkind
(N
) = N_Type_Conversion
then
647 Note_Uplevel_Bound
(Expression
(N
), Ref
);
649 end Note_Uplevel_Bound
;
651 -- Start of processing for Check_Static_Type
654 -- If already marked static, immediate return
656 if Is_Static_Type
(T
) and then not Check_Designated
then
660 -- If the type is at library level, always consider it static,
661 -- since such uplevel references are irrelevant.
663 if Is_Library_Level_Entity
(T
) then
664 Set_Is_Static_Type
(T
);
668 -- Otherwise figure out what the story is with this type
670 -- For a scalar type, check bounds
672 if Is_Scalar_Type
(T
) then
674 -- If both bounds static, then this is a static type
677 LB
: constant Node_Id
:= Type_Low_Bound
(T
);
678 UB
: constant Node_Id
:= Type_High_Bound
(T
);
681 if not Is_Static_Expression
(LB
) then
682 Note_Uplevel_Bound
(LB
, N
);
686 if not Is_Static_Expression
(UB
) then
687 Note_Uplevel_Bound
(UB
, N
);
692 -- For record type, check all components and discriminant
693 -- constraints if present.
695 elsif Is_Record_Type
(T
) then
701 C
:= First_Component_Or_Discriminant
(T
);
702 while Present
(C
) loop
703 Check_Static_Type
(Etype
(C
), N
, DT
);
704 Next_Component_Or_Discriminant
(C
);
707 if Has_Discriminants
(T
)
708 and then Present
(Discriminant_Constraint
(T
))
710 D
:= First_Elmt
(Discriminant_Constraint
(T
));
711 while Present
(D
) loop
712 if not Is_Static_Expression
(Node
(D
)) then
713 Note_Uplevel_Bound
(Node
(D
), N
);
722 -- For array type, check index types and component type
724 elsif Is_Array_Type
(T
) then
728 Check_Static_Type
(Component_Type
(T
), N
, DT
);
730 IX
:= First_Index
(T
);
731 while Present
(IX
) loop
732 Check_Static_Type
(Etype
(IX
), N
, DT
);
737 -- For private type, examine whether full view is static
739 elsif Is_Incomplete_Or_Private_Type
(T
)
740 and then Present
(Full_View
(T
))
742 Check_Static_Type
(Full_View
(T
), N
, DT
, Check_Designated
);
744 if Is_Static_Type
(Full_View
(T
)) then
745 Set_Is_Static_Type
(T
);
748 -- For access types, check designated type when required
750 elsif Is_Access_Type
(T
) and then Check_Designated
then
751 Check_Static_Type
(Directly_Designated_Type
(T
), N
, DT
);
753 -- For now, ignore other types
760 Set_Is_Static_Type
(T
);
762 end Check_Static_Type
;
764 ----------------------
765 -- Note_Uplevel_Ref --
766 ----------------------
768 procedure Note_Uplevel_Ref
774 Full_E
: Entity_Id
:= E
;
776 -- Nothing to do for static type
778 if Is_Static_Type
(E
) then
782 -- Nothing to do if Caller and Callee are the same
784 if Caller
= Callee
then
787 -- Callee may be a function that returns an array, and that has
788 -- been rewritten as a procedure. If caller is that procedure,
789 -- nothing to do either.
791 elsif Ekind
(Callee
) = E_Function
792 and then Rewritten_For_C
(Callee
)
793 and then Corresponding_Procedure
(Callee
) = Caller
797 elsif Ekind
(Callee
) in E_Entry | E_Entry_Family
then
801 -- We have a new uplevel referenced entity
803 if Ekind
(E
) = E_Constant
and then Present
(Full_View
(E
)) then
804 Full_E
:= Full_View
(E
);
807 -- All we do at this stage is to add the uplevel reference to
808 -- the table. It's too early to do anything else, since this
809 -- uplevel reference may come from an unreachable subprogram
810 -- in which case the entry will be deleted.
812 Urefs
.Append
((N
, Full_E
, Caller
, Callee
));
813 end Note_Uplevel_Ref
;
815 -------------------------
816 -- Register_Subprogram --
817 -------------------------
819 procedure Register_Subprogram
(E
: Entity_Id
; Bod
: Node_Id
) is
820 L
: constant Nat
:= Get_Level
(Subp
, E
);
823 -- Subprograms declared in tasks and protected types cannot be
824 -- eliminated because calls to them may be in other units, so
825 -- they must be treated as reachable.
831 Reachable
=> In_Synchronized_Unit
(E
)
832 or else Address_Taken
(E
),
834 Declares_AREC
=> False,
844 Set_Subps_Index
(E
, UI_From_Int
(Subps
.Last
));
846 -- If we marked this reachable because it's in a synchronized
847 -- unit, we have to mark all enclosing subprograms as reachable
848 -- as well. We do the same for subprograms with Address_Taken,
849 -- because otherwise we can run into problems with looking at
850 -- enclosing subprograms in Subps.Table due to their being
851 -- unreachable (the Subp_Index of unreachable subps is later
852 -- set to zero and their entry in Subps.Table is removed).
854 if In_Synchronized_Unit
(E
) or else Address_Taken
(E
) then
859 for J
in reverse 1 .. L
- 1 loop
860 S
:= Enclosing_Subprogram
(S
);
861 Subps
.Table
(Subp_Index
(S
)).Reachable
:= True;
865 end Register_Subprogram
;
867 -- Start of processing for Visit_Node
872 -- Record a subprogram call
875 | N_Procedure_Call_Statement
877 -- We are only interested in direct calls, not indirect
878 -- calls (where Name (N) is an explicit dereference) at
881 if Nkind
(Name
(N
)) in N_Has_Entity
then
882 Ent
:= Entity
(Name
(N
));
884 -- We are only interested in calls to subprograms nested
885 -- within Subp. Calls to Subp itself or to subprograms
886 -- outside the nested structure do not affect us.
888 if Is_Subprogram
(Ent
)
889 and then not Is_Generic_Subprogram
(Ent
)
890 and then not Is_Imported
(Ent
)
891 and then not Is_Intrinsic_Subprogram
(Ent
)
892 and then Scope_Within
(Ultimate_Alias
(Ent
), Subp
)
894 Append_Unique_Call
((N
, Current_Subprogram
, Ent
));
898 -- For all calls where the formal is an unconstrained array
899 -- and the actual is constrained we need to check the bounds
900 -- for uplevel references.
904 DT
: Boolean := False;
911 if Nkind
(Name
(N
)) = N_Explicit_Dereference
then
912 Subp
:= Etype
(Name
(N
));
914 Subp
:= Entity
(Name
(N
));
917 Actual
:= First_Actual
(N
);
918 Formal
:= First_Formal_With_Extras
(Subp
);
920 while Present
(Actual
) loop
921 F_Type
:= Get_Fullest_View
(Etype
(Formal
));
922 A_Type
:= Get_Fullest_View
(Etype
(Actual
));
924 if Is_Array_Type
(F_Type
)
925 and then not Is_Constrained
(F_Type
)
926 and then Is_Constrained
(A_Type
)
928 Check_Static_Type
(A_Type
, Empty
, DT
);
931 Next_Actual
(Actual
);
932 Next_Formal_With_Extras
(Formal
);
936 -- An At_End_Proc in a statement sequence indicates that there
937 -- is a call from the enclosing construct or block to that
938 -- subprogram. As above, the called entity must be local and
941 when N_Handled_Sequence_Of_Statements | N_Block_Statement
=>
942 if Present
(At_End_Proc
(N
))
943 and then Scope_Within
(Entity
(At_End_Proc
(N
)), Subp
)
944 and then not Is_Imported
(Entity
(At_End_Proc
(N
)))
947 ((N
, Current_Subprogram
, Entity
(At_End_Proc
(N
))));
950 -- Similarly, the following constructs include a semantic
951 -- attribute Procedure_To_Call that must be handled like
952 -- other calls. Likewise for attribute Storage_Pool.
955 | N_Extended_Return_Statement
957 | N_Simple_Return_Statement
960 Pool
: constant Entity_Id
:= Storage_Pool
(N
);
961 Proc
: constant Entity_Id
:= Procedure_To_Call
(N
);
965 and then Scope_Within
(Proc
, Subp
)
966 and then not Is_Imported
(Proc
)
968 Append_Unique_Call
((N
, Current_Subprogram
, Proc
));
972 and then not Is_Library_Level_Entity
(Pool
)
973 and then Scope_Within_Or_Same
(Scope
(Pool
), Subp
)
975 Caller
:= Current_Subprogram
;
976 Callee
:= Enclosing_Subprogram
(Pool
);
978 if Callee
/= Caller
then
979 Note_Uplevel_Ref
(Pool
, Empty
, Caller
, Callee
);
984 -- For an allocator with a qualified expression, check type
985 -- of expression being qualified. The explicit type name is
986 -- handled as an entity reference.
988 if Nkind
(N
) = N_Allocator
989 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
992 DT
: Boolean := False;
995 (Etype
(Expression
(Expression
(N
))), Empty
, DT
);
998 -- For a Return or Free (all other nodes we handle here),
999 -- we usually need the size of the object, so we need to be
1000 -- sure that any nonstatic bounds of the expression's type
1001 -- that are uplevel are handled.
1003 elsif Nkind
(N
) /= N_Allocator
1004 and then Present
(Expression
(N
))
1007 DT
: Boolean := False;
1010 (Etype
(Expression
(N
)),
1013 Check_Designated
=> Nkind
(N
) = N_Free_Statement
);
1017 -- A 'Access reference is a (potential) call. So is 'Address,
1018 -- in particular on imported subprograms. Other attributes
1019 -- require special handling.
1021 when N_Attribute_Reference
=>
1023 Attr
: constant Attribute_Id
:=
1024 Get_Attribute_Id
(Attribute_Name
(N
));
1027 when Attribute_Access
1028 | Attribute_Unchecked_Access
1029 | Attribute_Unrestricted_Access
1032 if Nkind
(Prefix
(N
)) in N_Has_Entity
then
1033 Ent
:= Entity
(Prefix
(N
));
1035 -- We only need to examine calls to subprograms
1036 -- nested within current Subp.
1038 if Scope_Within
(Ent
, Subp
) then
1039 if Is_Imported
(Ent
) then
1042 elsif Is_Subprogram
(Ent
) then
1044 ((N
, Current_Subprogram
, Ent
));
1049 -- References to bounds can be uplevel references if
1050 -- the type isn't static.
1052 when Attribute_First
1056 -- Special-case attributes of objects whose bounds
1057 -- may be uplevel references. More complex prefixes
1058 -- handled during full traversal. Note that if the
1059 -- nominal subtype of the prefix is unconstrained,
1060 -- the bound must be obtained from the object, not
1061 -- from the (possibly) uplevel reference. We call
1062 -- Get_Referenced_Object to deal with prefixes that
1063 -- are object renamings (prefixes that are types
1064 -- can be passed and will simply be returned). But
1065 -- it's also legal to get the bounds from the type
1066 -- of the prefix, so we have to handle both cases.
1069 DT
: Boolean := False;
1073 (Etype
(Get_Referenced_Object
(Prefix
(N
))))
1076 (Etype
(Get_Referenced_Object
(Prefix
(N
))),
1080 if Is_Constrained
(Etype
(Prefix
(N
))) then
1082 (Etype
(Prefix
(N
)), Empty
, DT
);
1091 -- Component associations in aggregates are either static or
1092 -- else the aggregate will be expanded into assignments, in
1093 -- which case the expression is analyzed later and provides
1094 -- no relevant code generation.
1096 when N_Component_Association
=>
1097 if No
(Expression
(N
))
1098 or else No
(Etype
(Expression
(N
)))
1103 -- Generic associations are not analyzed: the actuals are
1104 -- transferred to renaming and subtype declarations that
1105 -- are the ones that must be examined.
1107 when N_Generic_Association
=>
1110 -- Indexed references can be uplevel if the type isn't static
1111 -- and if the lower bound (or an inner bound for a multi-
1112 -- dimensional array) is uplevel.
1114 when N_Indexed_Component
1117 if Is_Constrained
(Etype
(Prefix
(N
))) then
1119 DT
: Boolean := False;
1121 Check_Static_Type
(Etype
(Prefix
(N
)), Empty
, DT
);
1125 -- A selected component can have an implicit up-level
1126 -- reference due to the bounds of previous fields in the
1127 -- record. We simplify the processing here by examining
1128 -- all components of the record.
1130 -- Selected components appear as unit names and end labels
1131 -- for child units. Prefixes of these nodes denote parent
1132 -- units and carry no type information so they are skipped.
1134 when N_Selected_Component
=>
1135 if Present
(Etype
(Prefix
(N
))) then
1137 DT
: Boolean := False;
1139 Check_Static_Type
(Etype
(Prefix
(N
)), Empty
, DT
);
1143 -- For EQ/NE comparisons, we need the type of the operands
1144 -- in order to do the comparison, which means we need the
1151 DT
: Boolean := False;
1153 Check_Static_Type
(Etype
(Left_Opnd
(N
)), Empty
, DT
);
1154 Check_Static_Type
(Etype
(Right_Opnd
(N
)), Empty
, DT
);
1157 -- Likewise we need the sizes to compute how much to move in
1160 when N_Assignment_Statement
=>
1162 DT
: Boolean := False;
1164 Check_Static_Type
(Etype
(Name
(N
)), Empty
, DT
);
1165 Check_Static_Type
(Etype
(Expression
(N
)), Empty
, DT
);
1168 -- Record a subprogram. We record a subprogram body that acts
1169 -- as a spec. Otherwise we record a subprogram declaration,
1170 -- providing that it has a corresponding body we can get hold
1171 -- of. The case of no corresponding body being available is
1174 when N_Subprogram_Body
=>
1175 Ent
:= Unique_Defining_Entity
(N
);
1177 -- Ignore generic subprogram
1179 if Is_Generic_Subprogram
(Ent
) then
1183 -- Make new entry in subprogram table if not already made
1185 Register_Subprogram
(Ent
, N
);
1187 -- Record a call from an At_End_Proc
1189 if Present
(At_End_Proc
(N
))
1190 and then Scope_Within
(Entity
(At_End_Proc
(N
)), Subp
)
1191 and then not Is_Imported
(Entity
(At_End_Proc
(N
)))
1193 Append_Unique_Call
((N
, Ent
, Entity
(At_End_Proc
(N
))));
1196 -- We make a recursive call to scan the subprogram body, so
1197 -- that we can save and restore Current_Subprogram.
1200 Save_CS
: constant Entity_Id
:= Current_Subprogram
;
1204 Current_Subprogram
:= Ent
;
1206 -- Scan declarations
1208 Decl
:= First
(Declarations
(N
));
1209 while Present
(Decl
) loop
1216 Visit
(Handled_Statement_Sequence
(N
));
1218 -- Restore current subprogram setting
1220 Current_Subprogram
:= Save_CS
;
1223 -- Now at this level, return skipping the subprogram body
1224 -- descendants, since we already took care of them!
1228 -- If we have a body stub, visit the associated subunit, which
1229 -- is a semantic descendant of the stub.
1232 Visit
(Library_Unit
(N
));
1234 -- A declaration of a wrapper package indicates a subprogram
1235 -- instance for which there is no explicit body. Enter the
1236 -- subprogram instance in the table.
1238 when N_Package_Declaration
=>
1239 if Is_Wrapper_Package
(Defining_Entity
(N
)) then
1241 (Related_Instance
(Defining_Entity
(N
)), Empty
);
1244 -- Skip generic declarations
1246 when N_Generic_Declaration
=>
1249 -- Skip generic package body
1251 when N_Package_Body
=>
1252 if Present
(Corresponding_Spec
(N
))
1253 and then Ekind
(Corresponding_Spec
(N
)) = E_Generic_Package
1258 -- Pragmas and component declarations are ignored. Quantified
1259 -- expressions are expanded into explicit loops and the
1260 -- original epression must be ignored.
1262 when N_Component_Declaration
1264 | N_Quantified_Expression
1268 -- We want to skip the function spec for a generic function
1269 -- to avoid looking at any generic types that might be in
1272 when N_Function_Specification
=>
1273 if Is_Generic_Subprogram
(Unique_Defining_Entity
(N
)) then
1277 -- Otherwise record an uplevel reference in a local identifier
1280 if Nkind
(N
) in N_Has_Entity
1281 and then Present
(Entity
(N
))
1285 -- Only interested in entities declared within our nest
1287 if not Is_Library_Level_Entity
(Ent
)
1288 and then Scope_Within_Or_Same
(Scope
(Ent
), Subp
)
1290 -- Skip entities defined in inlined subprograms
1293 Chars
(Enclosing_Subprogram
(Ent
)) /= Name_uParent
1295 -- Constants and variables are potentially uplevel
1296 -- references to global declarations.
1299 (Ekind
(Ent
) in E_Constant
1303 -- Formals are interesting, but not if being used
1304 -- as mere names of parameters for name notation
1310 (Nkind
(Parent
(N
)) = N_Parameter_Association
1311 and then Selector_Name
(Parent
(N
)) = N
))
1313 -- Types other than known Is_Static types are
1314 -- potentially interesting.
1317 (Is_Type
(Ent
) and then not Is_Static_Type
(Ent
)))
1319 -- Here we have a potentially interesting uplevel
1320 -- reference to examine.
1322 if Is_Type
(Ent
) then
1324 DT
: Boolean := False;
1327 Check_Static_Type
(Ent
, N
, DT
);
1332 Caller
:= Current_Subprogram
;
1333 Callee
:= Enclosing_Subprogram
(Ent
);
1336 and then (not Is_Static_Type
(Ent
)
1337 or else Needs_Fat_Pointer
(Ent
))
1339 Note_Uplevel_Ref
(Ent
, N
, Caller
, Callee
);
1341 -- Check the type of a formal parameter of the current
1342 -- subprogram, whose formal type may be an uplevel
1345 elsif Is_Formal
(Ent
)
1346 and then Scope
(Ent
) = Current_Subprogram
1349 DT
: Boolean := False;
1352 Check_Static_Type
(Etype
(Ent
), Empty
, DT
);
1359 -- Fall through to continue scanning children of this node
1364 -- Start of processing for Build_Tables
1367 -- Traverse the body to get subprograms, calls and uplevel references
1372 -- Now do the first transitive closure which determines which
1373 -- subprograms in the nest are actually reachable.
1375 Reachable_Closure
: declare
1379 Subps
.Table
(Subps_First
).Reachable
:= True;
1381 -- We use a simple minded algorithm as follows (obviously this can
1382 -- be done more efficiently, using one of the standard algorithms
1383 -- for efficient transitive closure computation, but this is simple
1384 -- and most likely fast enough that its speed does not matter).
1386 -- Repeatedly scan the list of calls. Any time we find a call from
1387 -- A to B, where A is reachable, but B is not, then B is reachable,
1388 -- and note that we have made a change by setting Modified True. We
1389 -- repeat this until we make a pass with no modifications.
1393 Inner
: for J
in Calls
.First
.. Calls
.Last
loop
1395 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1397 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1398 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1400 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1401 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1404 if SUBF
.Reachable
and then not SUBT
.Reachable
then
1405 SUBT
.Reachable
:= True;
1411 exit Outer
when not Modified
;
1413 end Reachable_Closure
;
1415 -- Remove calls from unreachable subprograms
1422 for J
in Calls
.First
.. Calls
.Last
loop
1424 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1426 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1427 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1429 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1430 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1433 if SUBF
.Reachable
then
1434 pragma Assert
(SUBT
.Reachable
);
1435 New_Index
:= New_Index
+ 1;
1436 Calls
.Table
(New_Index
) := Calls
.Table
(J
);
1441 Calls
.Set_Last
(New_Index
);
1444 -- Remove uplevel references from unreachable subprograms
1451 for J
in Urefs
.First
.. Urefs
.Last
loop
1453 URJ
: Uref_Entry
renames Urefs
.Table
(J
);
1455 SINF
: constant SI_Type
:= Subp_Index
(URJ
.Caller
);
1456 SINT
: constant SI_Type
:= Subp_Index
(URJ
.Callee
);
1458 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1459 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1464 -- Keep reachable reference
1466 if SUBF
.Reachable
then
1467 New_Index
:= New_Index
+ 1;
1468 Urefs
.Table
(New_Index
) := Urefs
.Table
(J
);
1470 -- And since we know we are keeping this one, this is a good
1471 -- place to fill in information for a good reference.
1473 -- Mark all enclosing subprograms need to declare AREC
1477 S
:= Enclosing_Subprogram
(S
);
1479 -- If we are at the top level, as can happen with
1480 -- references to formals in aspects of nested subprogram
1481 -- declarations, there are no further subprograms to mark
1482 -- as requiring activation records.
1487 SUBI
: Subp_Entry
renames Subps
.Table
(Subp_Index
(S
));
1489 SUBI
.Declares_AREC
:= True;
1491 -- If this entity was marked reachable because it is
1492 -- in a task or protected type, there may not appear
1493 -- to be any calls to it, which would normally adjust
1494 -- the levels of the parent subprograms. So we need to
1495 -- be sure that the uplevel reference of that entity
1496 -- takes into account possible calls.
1498 if In_Synchronized_Unit
(SUBF
.Ent
)
1499 and then SUBT
.Lev
< SUBI
.Uplevel_Ref
1501 SUBI
.Uplevel_Ref
:= SUBT
.Lev
;
1505 exit when S
= URJ
.Callee
;
1508 -- Add to list of uplevel referenced entities for Callee.
1509 -- We do not add types to this list, only actual references
1510 -- to objects that will be referenced uplevel, and we use
1511 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1512 -- duplicate entries in the list. Discriminants are also
1513 -- excluded, only the enclosing object can appear in the
1516 if not Is_Uplevel_Referenced_Entity
(URJ
.Ent
)
1517 and then Ekind
(URJ
.Ent
) /= E_Discriminant
1519 Set_Is_Uplevel_Referenced_Entity
(URJ
.Ent
);
1520 Append_New_Elmt
(URJ
.Ent
, SUBT
.Uents
);
1523 -- And set uplevel indication for caller
1525 if SUBT
.Lev
< SUBF
.Uplevel_Ref
then
1526 SUBF
.Uplevel_Ref
:= SUBT
.Lev
;
1532 Urefs
.Set_Last
(New_Index
);
1535 -- Remove unreachable subprograms from Subps table. Note that we do
1536 -- this after eliminating entries from the other two tables, since
1537 -- those elimination steps depend on referencing the Subps table.
1543 New_SI
:= Subps_First
- 1;
1544 for J
in Subps_First
.. Subps
.Last
loop
1546 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1551 -- Subprogram is reachable, copy and reset index
1553 if STJ
.Reachable
then
1554 New_SI
:= New_SI
+ 1;
1555 Subps
.Table
(New_SI
) := STJ
;
1556 Set_Subps_Index
(STJ
.Ent
, UI_From_Int
(New_SI
));
1558 -- Subprogram is not reachable
1561 -- Clear index, since no longer active
1563 Set_Subps_Index
(Subps
.Table
(J
).Ent
, Uint_0
);
1565 -- Output debug information if -gnatd.3 set
1567 if Debug_Flag_Dot_3
then
1568 Write_Str
("Eliminate ");
1569 Write_Name
(Chars
(Subps
.Table
(J
).Ent
));
1571 Write_Location
(Sloc
(Subps
.Table
(J
).Ent
));
1572 Write_Str
(" (not referenced)");
1576 -- Rewrite declaration, body, and corresponding freeze node
1577 -- to null statements.
1579 -- A subprogram instantiation does not have an explicit
1580 -- body. If unused, we could remove the corresponding
1581 -- wrapper package and its body.
1583 if Present
(STJ
.Bod
) then
1584 Spec
:= Corresponding_Spec
(STJ
.Bod
);
1586 if Present
(Spec
) then
1587 Decl
:= Parent
(Declaration_Node
(Spec
));
1588 Rewrite
(Decl
, Make_Null_Statement
(Sloc
(Decl
)));
1590 if Present
(Freeze_Node
(Spec
)) then
1591 Rewrite
(Freeze_Node
(Spec
),
1592 Make_Null_Statement
(Sloc
(Decl
)));
1596 Rewrite
(STJ
.Bod
, Make_Null_Statement
(Sloc
(STJ
.Bod
)));
1602 Subps
.Set_Last
(New_SI
);
1605 -- Now it is time for the second transitive closure, which follows calls
1606 -- and makes sure that A calls B, and B has uplevel references, then A
1607 -- is also marked as having uplevel references.
1609 Closure_Uplevel
: declare
1613 -- We use a simple minded algorithm as follows (obviously this can
1614 -- be done more efficiently, using one of the standard algorithms
1615 -- for efficient transitive closure computation, but this is simple
1616 -- and most likely fast enough that its speed does not matter).
1618 -- Repeatedly scan the list of calls. Any time we find a call from
1619 -- A to B, where B has uplevel references, make sure that A is marked
1620 -- as having at least the same level of uplevel referencing.
1624 Inner2
: for J
in Calls
.First
.. Calls
.Last
loop
1626 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1627 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1628 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1629 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1630 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1632 if SUBT
.Lev
> SUBT
.Uplevel_Ref
1633 and then SUBF
.Uplevel_Ref
> SUBT
.Uplevel_Ref
1635 SUBF
.Uplevel_Ref
:= SUBT
.Uplevel_Ref
;
1641 exit Outer2
when not Modified
;
1643 end Closure_Uplevel
;
1645 -- We have one more step before the tables are complete. An uplevel
1646 -- call from subprogram A to subprogram B where subprogram B has uplevel
1647 -- references is in effect an uplevel reference, and must arrange for
1648 -- the proper activation link to be passed.
1650 for J
in Calls
.First
.. Calls
.Last
loop
1652 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1654 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1655 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1657 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1658 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1663 -- If callee has uplevel references
1665 if SUBT
.Uplevel_Ref
< SUBT
.Lev
1667 -- And this is an uplevel call
1669 and then SUBT
.Lev
< SUBF
.Lev
1671 -- We need to arrange for finding the uplink
1675 A
:= Enclosing_Subprogram
(A
);
1676 Subps
.Table
(Subp_Index
(A
)).Declares_AREC
:= True;
1677 exit when A
= CTJ
.Callee
;
1679 -- In any case exit when we get to the outer level. This
1680 -- happens in some odd cases with generics (in particular
1681 -- sem_ch3.adb does not compile without this kludge ???).
1689 -- The tables are now complete, so we can record the last index in the
1690 -- Subps table for later reference in Cprint.
1692 Subps
.Table
(Subps_First
).Last
:= Subps
.Last
;
1694 -- Next step, create the entities for code we will insert. We do this
1695 -- at the start so that all the entities are defined, regardless of the
1696 -- order in which we do the code insertions.
1698 Create_Entities
: for J
in Subps_First
.. Subps
.Last
loop
1700 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1701 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1704 -- First we create the ARECnF entity for the additional formal for
1705 -- all subprograms which need an activation record passed.
1707 if STJ
.Uplevel_Ref
< STJ
.Lev
then
1709 Make_Defining_Identifier
(Loc
, Chars
=> AREC_Name
(J
, "F"));
1712 -- Define the AREC entities for the activation record if needed
1714 if STJ
.Declares_AREC
then
1716 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, ""));
1718 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "T"));
1720 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "PT"));
1722 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "P"));
1724 -- Define uplink component entity if inner nesting case
1726 if Present
(STJ
.ARECnF
) then
1728 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "U"));
1732 end loop Create_Entities
;
1734 -- Loop through subprograms
1737 Addr
: Entity_Id
:= Empty
;
1740 for J
in Subps_First
.. Subps
.Last
loop
1742 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1745 -- First add the extra formal if needed. This applies to all
1746 -- nested subprograms that require an activation record to be
1747 -- passed, as indicated by ARECnF being defined.
1749 if Present
(STJ
.ARECnF
) then
1751 -- Here we need the extra formal. We do the expansion and
1752 -- analysis of this manually, since it is fairly simple,
1753 -- and it is not obvious how we can get what we want if we
1754 -- try to use the normal Analyze circuit.
1756 Add_Extra_Formal
: declare
1757 Encl
: constant SI_Type
:= Enclosing_Subp
(J
);
1758 STJE
: Subp_Entry
renames Subps
.Table
(Encl
);
1759 -- Index and Subp_Entry for enclosing routine
1761 Form
: constant Entity_Id
:= STJ
.ARECnF
;
1762 -- The formal to be added. Note that n here is one less
1763 -- than the level of the subprogram itself (STJ.Ent).
1765 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
);
1766 -- S is an N_Function/Procedure_Specification node, and F
1767 -- is the new entity to add to this subprogram spec as
1768 -- the last Extra_Formal.
1770 ----------------------
1771 -- Add_Form_To_Spec --
1772 ----------------------
1774 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
) is
1775 Sub
: constant Entity_Id
:= Defining_Entity
(S
);
1779 -- Case of at least one Extra_Formal is present, set
1780 -- ARECnF as the new last entry in the list.
1782 if Present
(Extra_Formals
(Sub
)) then
1783 Ent
:= Extra_Formals
(Sub
);
1784 while Present
(Extra_Formal
(Ent
)) loop
1785 Ent
:= Extra_Formal
(Ent
);
1788 Set_Extra_Formal
(Ent
, F
);
1790 -- No Extra formals present
1793 Set_Extra_Formals
(Sub
, F
);
1794 Ent
:= Last_Formal
(Sub
);
1796 if Present
(Ent
) then
1797 Set_Extra_Formal
(Ent
, F
);
1800 end Add_Form_To_Spec
;
1802 -- Start of processing for Add_Extra_Formal
1805 -- Decorate the new formal entity
1807 Set_Scope
(Form
, STJ
.Ent
);
1808 Mutate_Ekind
(Form
, E_In_Parameter
);
1809 Set_Etype
(Form
, STJE
.ARECnPT
);
1810 Set_Mechanism
(Form
, By_Copy
);
1811 Set_Never_Set_In_Source
(Form
, True);
1812 Set_Analyzed
(Form
, True);
1813 Set_Comes_From_Source
(Form
, False);
1814 Set_Is_Activation_Record
(Form
, True);
1816 -- Case of only body present
1818 if Acts_As_Spec
(STJ
.Bod
) then
1819 Add_Form_To_Spec
(Form
, Specification
(STJ
.Bod
));
1821 -- Case of separate spec
1824 Add_Form_To_Spec
(Form
, Parent
(STJ
.Ent
));
1826 end Add_Extra_Formal
;
1829 -- Processing for subprograms that declare an activation record
1831 if Present
(STJ
.ARECn
) then
1833 -- Local declarations for one such subprogram
1836 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1838 Decls
: constant List_Id
:= New_List
;
1839 -- List of new declarations we create
1844 Decl_Assign
: Node_Id
;
1845 -- Assignment to set uplink, Empty if none
1847 Decl_ARECnT
: Node_Id
;
1848 Decl_ARECnPT
: Node_Id
;
1849 Decl_ARECn
: Node_Id
;
1850 Decl_ARECnP
: Node_Id
;
1851 -- Declaration nodes for the AREC entities we build
1854 -- Build list of component declarations for ARECnT and
1855 -- load System.Address.
1857 Clist
:= Empty_List
;
1860 Addr
:= RTE
(RE_Address
);
1863 -- If we are in a subprogram that has a static link that
1864 -- is passed in (as indicated by ARECnF being defined),
1865 -- then include ARECnU : ARECmPT where ARECmPT comes from
1866 -- the level one higher than the current level, and the
1867 -- entity ARECnPT comes from the enclosing subprogram.
1869 if Present
(STJ
.ARECnF
) then
1872 renames Subps
.Table
(Enclosing_Subp
(J
));
1875 Make_Component_Declaration
(Loc
,
1876 Defining_Identifier
=> STJ
.ARECnU
,
1877 Component_Definition
=>
1878 Make_Component_Definition
(Loc
,
1879 Subtype_Indication
=>
1880 New_Occurrence_Of
(STJE
.ARECnPT
, Loc
))));
1884 -- Add components for uplevel referenced entities
1886 if Present
(STJ
.Uents
) then
1893 -- 1's origin of index in list of elements. This is
1894 -- used to uniquify names if needed in Upref_Name.
1897 Elmt
:= First_Elmt
(STJ
.Uents
);
1899 while Present
(Elmt
) loop
1900 Uent
:= Node
(Elmt
);
1904 Make_Defining_Identifier
(Loc
,
1905 Chars
=> Upref_Name
(Uent
, Indx
, Clist
));
1907 Set_Activation_Record_Component
1910 if Needs_Fat_Pointer
(Uent
) then
1912 -- Build corresponding access type
1915 Build_Access_Type_Decl
1916 (Etype
(Uent
), STJ
.Ent
);
1917 Append_To
(Decls
, Ptr_Decl
);
1919 -- And use its type in the corresponding
1923 Make_Component_Declaration
(Loc
,
1924 Defining_Identifier
=> Comp
,
1925 Component_Definition
=>
1926 Make_Component_Definition
(Loc
,
1927 Subtype_Indication
=>
1929 (Defining_Identifier
(Ptr_Decl
),
1933 Make_Component_Declaration
(Loc
,
1934 Defining_Identifier
=> Comp
,
1935 Component_Definition
=>
1936 Make_Component_Definition
(Loc
,
1937 Subtype_Indication
=>
1938 New_Occurrence_Of
(Addr
, Loc
))));
1945 -- Now we can insert the AREC declarations into the body
1946 -- type ARECnT is record .. end record;
1947 -- pragma Suppress_Initialization (ARECnT);
1949 -- Note that we need to set the Suppress_Initialization
1950 -- flag after Decl_ARECnT has been analyzed.
1953 Make_Full_Type_Declaration
(Loc
,
1954 Defining_Identifier
=> STJ
.ARECnT
,
1956 Make_Record_Definition
(Loc
,
1958 Make_Component_List
(Loc
,
1959 Component_Items
=> Clist
)));
1960 Append_To
(Decls
, Decl_ARECnT
);
1962 -- type ARECnPT is access all ARECnT;
1965 Make_Full_Type_Declaration
(Loc
,
1966 Defining_Identifier
=> STJ
.ARECnPT
,
1968 Make_Access_To_Object_Definition
(Loc
,
1969 All_Present
=> True,
1970 Subtype_Indication
=>
1971 New_Occurrence_Of
(STJ
.ARECnT
, Loc
)));
1972 Append_To
(Decls
, Decl_ARECnPT
);
1974 -- ARECn : aliased ARECnT;
1977 Make_Object_Declaration
(Loc
,
1978 Defining_Identifier
=> STJ
.ARECn
,
1979 Aliased_Present
=> True,
1980 Object_Definition
=>
1981 New_Occurrence_Of
(STJ
.ARECnT
, Loc
));
1982 Append_To
(Decls
, Decl_ARECn
);
1984 -- ARECnP : constant ARECnPT := ARECn'Access;
1987 Make_Object_Declaration
(Loc
,
1988 Defining_Identifier
=> STJ
.ARECnP
,
1989 Constant_Present
=> True,
1990 Object_Definition
=>
1991 New_Occurrence_Of
(STJ
.ARECnPT
, Loc
),
1993 Make_Attribute_Reference
(Loc
,
1995 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1996 Attribute_Name
=> Name_Access
));
1997 Append_To
(Decls
, Decl_ARECnP
);
1999 -- If we are in a subprogram that has a static link that
2000 -- is passed in (as indicated by ARECnF being defined),
2001 -- then generate ARECn.ARECmU := ARECmF where m is
2002 -- one less than the current level to set the uplink.
2004 if Present
(STJ
.ARECnF
) then
2006 Make_Assignment_Statement
(Loc
,
2008 Make_Selected_Component
(Loc
,
2010 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
2012 New_Occurrence_Of
(STJ
.ARECnU
, Loc
)),
2014 New_Occurrence_Of
(STJ
.ARECnF
, Loc
));
2015 Append_To
(Decls
, Decl_Assign
);
2018 Decl_Assign
:= Empty
;
2021 if No
(Declarations
(STJ
.Bod
)) then
2022 Set_Declarations
(STJ
.Bod
, Decls
);
2024 Prepend_List_To
(Declarations
(STJ
.Bod
), Decls
);
2027 -- Analyze the newly inserted declarations. Note that we
2028 -- do not need to establish the whole scope stack, since
2029 -- we have already set all entity fields (so there will
2030 -- be no searching of upper scopes to resolve names). But
2031 -- we do set the scope of the current subprogram, so that
2032 -- newly created entities go in the right entity chain.
2034 -- We analyze with all checks suppressed (since we do
2035 -- not expect any exceptions).
2037 Push_Scope
(STJ
.Ent
);
2038 Analyze
(Decl_ARECnT
, Suppress
=> All_Checks
);
2040 -- Note that we need to call Set_Suppress_Initialization
2041 -- after Decl_ARECnT has been analyzed, but before
2042 -- analyzing Decl_ARECnP so that the flag is properly
2043 -- taking into account.
2045 Set_Suppress_Initialization
(STJ
.ARECnT
);
2047 Analyze
(Decl_ARECnPT
, Suppress
=> All_Checks
);
2048 Analyze
(Decl_ARECn
, Suppress
=> All_Checks
);
2049 Analyze
(Decl_ARECnP
, Suppress
=> All_Checks
);
2051 if Present
(Decl_Assign
) then
2052 Analyze
(Decl_Assign
, Suppress
=> All_Checks
);
2057 -- Next step, for each uplevel referenced entity, add
2058 -- assignment operations to set the component in the
2059 -- activation record.
2061 if Present
(STJ
.Uents
) then
2066 Elmt
:= First_Elmt
(STJ
.Uents
);
2067 while Present
(Elmt
) loop
2069 Ent
: constant Entity_Id
:= Node
(Elmt
);
2070 Loc
: constant Source_Ptr
:= Sloc
(Ent
);
2071 Dec
: constant Node_Id
:=
2072 Declaration_Node
(Ent
);
2081 -- For parameters, we insert the assignment
2082 -- right after the declaration of ARECnP.
2083 -- For all other entities, we insert the
2084 -- assignment immediately after the
2085 -- declaration of the entity or after the
2086 -- freeze node if present.
2088 -- Note: we don't need to mark the entity
2089 -- as being aliased, because the address
2090 -- attribute will mark it as Address_Taken,
2091 -- and that is good enough.
2093 if Is_Formal
(Ent
) then
2096 elsif Has_Delayed_Freeze
(Ent
) then
2097 Ins
:= Freeze_Node
(Ent
);
2103 -- Build and insert the assignment:
2104 -- ARECn.nam := nam'Address
2105 -- or else 'Unchecked_Access for
2106 -- unconstrained array.
2108 if Needs_Fat_Pointer
(Ent
) then
2109 Attr
:= Name_Unchecked_Access
;
2111 Attr
:= Name_Address
;
2115 Make_Attribute_Reference
(Loc
,
2117 New_Occurrence_Of
(Ent
, Loc
),
2118 Attribute_Name
=> Attr
);
2120 -- If the entity is an unconstrained formal
2121 -- we wrap the attribute reference in an
2122 -- unchecked conversion to the type of the
2123 -- activation record component, to prevent
2124 -- spurious subtype conformance errors within
2128 and then not Is_Constrained
(Etype
(Ent
))
2130 -- Find target component and its type
2132 Comp
:= First_Component
(STJ
.ARECnT
);
2133 while Chars
(Comp
) /= Chars
(Ent
) loop
2134 Next_Component
(Comp
);
2138 Unchecked_Convert_To
(Etype
(Comp
), Rhs
);
2142 Make_Assignment_Statement
(Loc
,
2144 Make_Selected_Component
(Loc
,
2146 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
2149 (Activation_Record_Component
2154 -- If we have a loop parameter, we have
2155 -- to insert before the first statement
2156 -- of the loop. Ins points to the
2157 -- N_Loop_Parameter_Specification or to
2158 -- an N_Iterator_Specification.
2161 N_Iterator_Specification |
2162 N_Loop_Parameter_Specification
2164 -- Quantified expression are rewritten as
2165 -- loops during expansion.
2167 if Nkind
(Parent
(Ins
)) =
2168 N_Quantified_Expression
2176 (Parent
(Parent
(Ins
))));
2177 Insert_Before
(Ins
, Asn
);
2181 Insert_After
(Ins
, Asn
);
2184 -- Analyze the assignment statement. We do
2185 -- not need to establish the relevant scope
2186 -- stack entries here, because we have
2187 -- already set the correct entity references,
2188 -- so no name resolution is required, and no
2189 -- new entities are created, so we don't even
2190 -- need to set the current scope.
2192 -- We analyze with all checks suppressed
2193 -- (since we do not expect any exceptions).
2195 Analyze
(Asn
, Suppress
=> All_Checks
);
2208 -- Next step, process uplevel references. This has to be done in a
2209 -- separate pass, after completing the processing in Sub_Loop because we
2210 -- need all the AREC declarations generated, inserted, and analyzed so
2211 -- that the uplevel references can be successfully analyzed.
2213 Uplev_Refs
: for J
in Urefs
.First
.. Urefs
.Last
loop
2215 UPJ
: Uref_Entry
renames Urefs
.Table
(J
);
2218 -- Ignore type references, these are implicit references that do
2219 -- not need rewriting (e.g. the appearance in a conversion).
2220 -- Also ignore if no reference was specified or if the rewriting
2221 -- has already been done (this can happen if the N_Identifier
2222 -- occurs more than one time in the tree). Also ignore references
2223 -- when not generating C code (in particular for the case of LLVM,
2224 -- since GNAT-LLVM will handle the processing for up-level refs).
2227 or else not Is_Entity_Name
(UPJ
.Ref
)
2228 or else No
(Entity
(UPJ
.Ref
))
2229 or else not Opt
.Generate_C_Code
2234 -- Rewrite one reference
2236 Rewrite_One_Ref
: declare
2237 Loc
: constant Source_Ptr
:= Sloc
(UPJ
.Ref
);
2238 -- Source location for the reference
2240 Typ
: constant Entity_Id
:= Etype
(UPJ
.Ent
);
2241 -- The type of the referenced entity
2244 -- The actual subtype of the reference
2246 RS_Caller
: constant SI_Type
:= Subp_Index
(UPJ
.Caller
);
2247 -- Subp_Index for caller containing reference
2249 STJR
: Subp_Entry
renames Subps
.Table
(RS_Caller
);
2250 -- Subp_Entry for subprogram containing reference
2252 RS_Callee
: constant SI_Type
:= Subp_Index
(UPJ
.Callee
);
2253 -- Subp_Index for subprogram containing referenced entity
2255 STJE
: Subp_Entry
renames Subps
.Table
(RS_Callee
);
2256 -- Subp_Entry for subprogram containing referenced entity
2263 Atyp
:= Etype
(UPJ
.Ref
);
2265 if Ekind
(Atyp
) /= E_Record_Subtype
then
2266 Atyp
:= Get_Actual_Subtype
(UPJ
.Ref
);
2269 -- Ignore if no ARECnF entity for enclosing subprogram which
2270 -- probably happens as a result of not properly treating
2271 -- instance bodies. To be examined ???
2273 -- If this test is omitted, then the compilation of freeze.adb
2274 -- and inline.adb fail in unnesting mode.
2276 if No
(STJR
.ARECnF
) then
2280 -- If this is a reference to a global constant, use its value
2281 -- rather than create a reference. It is more efficient and
2282 -- furthermore indispensable if the context requires a
2283 -- constant, such as a branch of a case statement.
2285 if Ekind
(UPJ
.Ent
) = E_Constant
2286 and then Is_True_Constant
(UPJ
.Ent
)
2287 and then Present
(Constant_Value
(UPJ
.Ent
))
2288 and then Is_Static_Expression
(Constant_Value
(UPJ
.Ent
))
2290 Rewrite
(UPJ
.Ref
, New_Copy_Tree
(Constant_Value
(UPJ
.Ent
)));
2294 -- Push the current scope, so that the pointer type Tnn, and
2295 -- any subsidiary entities resulting from the analysis of the
2296 -- rewritten reference, go in the right entity chain.
2298 Push_Scope
(STJR
.Ent
);
2300 -- Now we need to rewrite the reference. We have a reference
2301 -- from level STJR.Lev to level STJE.Lev. The general form of
2302 -- the rewritten reference for entity X is:
2304 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2306 -- where a,b,c,d .. m =
2307 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2309 pragma Assert
(STJR
.Lev
> STJE
.Lev
);
2311 -- Compute the prefix of X. Here are examples to make things
2312 -- clear (with parens to show groupings, the prefix is
2313 -- everything except the .X at the end).
2315 -- level 2 to level 1
2319 -- level 3 to level 1
2321 -- (AREC2F.AREC1U).X
2323 -- level 4 to level 1
2325 -- ((AREC3F.AREC2U).AREC1U).X
2327 -- level 6 to level 2
2329 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2331 -- In the above, ARECnF and ARECnU are pointers, so there are
2332 -- explicit dereferences required for these occurrences.
2335 Make_Explicit_Dereference
(Loc
,
2336 Prefix
=> New_Occurrence_Of
(STJR
.ARECnF
, Loc
));
2338 for L
in STJE
.Lev
.. STJR
.Lev
- 2 loop
2339 SI
:= Enclosing_Subp
(SI
);
2341 Make_Explicit_Dereference
(Loc
,
2343 Make_Selected_Component
(Loc
,
2346 New_Occurrence_Of
(Subps
.Table
(SI
).ARECnU
, Loc
)));
2349 -- Get activation record component (must exist)
2351 Comp
:= Activation_Record_Component
(UPJ
.Ent
);
2352 pragma Assert
(Present
(Comp
));
2354 -- Do the replacement. If the component type is an access type,
2355 -- this is an uplevel reference for an entity that requires a
2356 -- fat pointer, so dereference the component.
2358 if Is_Access_Type
(Etype
(Comp
)) then
2360 Make_Explicit_Dereference
(Loc
,
2362 Make_Selected_Component
(Loc
,
2365 New_Occurrence_Of
(Comp
, Loc
))));
2369 Make_Attribute_Reference
(Loc
,
2370 Prefix
=> New_Occurrence_Of
(Atyp
, Loc
),
2371 Attribute_Name
=> Name_Deref
,
2372 Expressions
=> New_List
(
2373 Make_Selected_Component
(Loc
,
2376 New_Occurrence_Of
(Comp
, Loc
)))));
2379 -- Analyze and resolve the new expression. We do not need to
2380 -- establish the relevant scope stack entries here, because we
2381 -- have already set all the correct entity references, so no
2382 -- name resolution is needed. We have already set the current
2383 -- scope, so that any new entities created will be in the right
2386 -- We analyze with all checks suppressed (since we do not
2387 -- expect any exceptions)
2389 Analyze_And_Resolve
(UPJ
.Ref
, Typ
, Suppress
=> All_Checks
);
2391 -- Generate an extra temporary to facilitate the C backend
2392 -- processing this dereference
2394 if Opt
.Modify_Tree_For_C
2395 and then Nkind
(Parent
(UPJ
.Ref
)) in
2396 N_Type_Conversion | N_Unchecked_Type_Conversion
2398 Force_Evaluation
(UPJ
.Ref
, Mode
=> Strict
);
2402 end Rewrite_One_Ref
;
2407 end loop Uplev_Refs
;
2409 -- Finally, loop through all calls adding extra actual for the
2410 -- activation record where it is required.
2412 Adjust_Calls
: for J
in Calls
.First
.. Calls
.Last
loop
2414 -- Process a single call, we are only interested in a call to a
2415 -- subprogram that actually needs a pointer to an activation record,
2416 -- as indicated by the ARECnF entity being set. This excludes the
2417 -- top level subprogram, and any subprogram not having uplevel refs.
2419 Adjust_One_Call
: declare
2420 CTJ
: Call_Entry
renames Calls
.Table
(J
);
2421 STF
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Caller
));
2422 STT
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Callee
));
2424 Loc
: constant Source_Ptr
:= Sloc
(CTJ
.N
);
2432 if Present
(STT
.ARECnF
)
2433 and then Nkind
(CTJ
.N
) in N_Subprogram_Call
2435 -- CTJ.N is a call to a subprogram which may require a pointer
2436 -- to an activation record. The subprogram containing the call
2437 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2438 -- have a call from level STF.Lev to level STT.Lev.
2440 -- There are three possibilities:
2442 -- For a call to the same level, we just pass the activation
2443 -- record passed to the calling subprogram.
2445 if STF
.Lev
= STT
.Lev
then
2446 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
2448 -- For a call that goes down a level, we pass a pointer to the
2449 -- activation record constructed within the caller (which may
2450 -- be the outer-level subprogram, but also may be a more deeply
2453 elsif STT
.Lev
= STF
.Lev
+ 1 then
2454 Extra
:= New_Occurrence_Of
(STF
.ARECnP
, Loc
);
2456 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2457 -- since it is not possible to do a downcall of more than
2460 -- For a call from level STF.Lev to level STT.Lev, we
2461 -- have to find the activation record needed by the
2462 -- callee. This is as follows:
2464 -- ARECaF.ARECbU.ARECcU....ARECmU
2466 -- where a,b,c .. m =
2467 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2470 pragma Assert
(STT
.Lev
< STF
.Lev
);
2472 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
2473 SubX
:= Subp_Index
(CTJ
.Caller
);
2474 for K
in reverse STT
.Lev
.. STF
.Lev
- 1 loop
2475 SubX
:= Enclosing_Subp
(SubX
);
2477 Make_Selected_Component
(Loc
,
2481 (Subps
.Table
(SubX
).ARECnU
, Loc
));
2485 -- Extra is the additional parameter to be added. Build a
2486 -- parameter association that we can append to the actuals.
2489 Make_Parameter_Association
(Loc
,
2491 New_Occurrence_Of
(STT
.ARECnF
, Loc
),
2492 Explicit_Actual_Parameter
=> Extra
);
2494 if No
(Parameter_Associations
(CTJ
.N
)) then
2495 Set_Parameter_Associations
(CTJ
.N
, Empty_List
);
2498 Append
(ExtraP
, Parameter_Associations
(CTJ
.N
));
2500 -- We need to deal with the actual parameter chain as well. The
2501 -- newly added parameter is always the last actual.
2503 Act
:= First_Named_Actual
(CTJ
.N
);
2506 Set_First_Named_Actual
(CTJ
.N
, Extra
);
2508 -- If call has been relocated (as with an expression in
2509 -- an aggregate), set First_Named pointer in original node
2510 -- as well, because that's the parent of the parameter list.
2512 Set_First_Named_Actual
2513 (Parent
(List_Containing
(ExtraP
)), Extra
);
2515 -- Here we must follow the chain and append the new entry
2524 PAN
:= Parent
(Act
);
2525 pragma Assert
(Nkind
(PAN
) = N_Parameter_Association
);
2526 NNA
:= Next_Named_Actual
(PAN
);
2529 Set_Next_Named_Actual
(PAN
, Extra
);
2538 -- Analyze and resolve the new actual. We do not need to
2539 -- establish the relevant scope stack entries here, because
2540 -- we have already set all the correct entity references, so
2541 -- no name resolution is needed.
2543 -- We analyze with all checks suppressed (since we do not
2544 -- expect any exceptions, and also we temporarily turn off
2545 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2546 -- references (not needed at this stage, and in fact causes
2547 -- a bit of recursive chaos).
2549 Opt
.Unnest_Subprogram_Mode
:= False;
2551 (Extra
, Etype
(STT
.ARECnF
), Suppress
=> All_Checks
);
2552 Opt
.Unnest_Subprogram_Mode
:= True;
2554 end Adjust_One_Call
;
2555 end loop Adjust_Calls
;
2558 end Unnest_Subprogram
;
2560 ------------------------
2561 -- Unnest_Subprograms --
2562 ------------------------
2564 procedure Unnest_Subprograms
(N
: Node_Id
) is
2565 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
;
2566 -- Tree visitor that search for outer level procedures with nested
2567 -- subprograms and invokes Unnest_Subprogram()
2573 procedure Do_Search
is new Traverse_Proc
(Search_Subprograms
);
2574 -- Subtree visitor instantiation
2576 ------------------------
2577 -- Search_Subprograms --
2578 ------------------------
2580 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
is
2582 if Nkind
(N
) in N_Subprogram_Body | N_Subprogram_Body_Stub
then
2584 Spec_Id
: constant Entity_Id
:= Unique_Defining_Entity
(N
);
2587 -- We are only interested in subprograms (not generic
2588 -- subprograms), that have nested subprograms.
2590 if Is_Subprogram
(Spec_Id
)
2591 and then Has_Nested_Subprogram
(Spec_Id
)
2592 and then Is_Library_Level_Entity
(Spec_Id
)
2594 Unnest_Subprogram
(Spec_Id
, N
);
2600 -- The proper body of a stub may contain nested subprograms, and
2601 -- therefore must be visited explicitly. Nested stubs are examined
2602 -- recursively in Visit_Node.
2604 elsif Nkind
(N
) in N_Body_Stub
then
2605 Do_Search
(Library_Unit
(N
));
2607 -- Skip generic packages
2609 elsif Nkind
(N
) = N_Package_Body
2610 and then Ekind
(Corresponding_Spec
(N
)) = E_Generic_Package
2616 end Search_Subprograms
;
2619 Subp_Body
: Node_Id
;
2621 -- Start of processing for Unnest_Subprograms
2624 if not Opt
.Unnest_Subprogram_Mode
or not Opt
.Expander_Active
then
2628 -- A specification will contain bodies if it contains instantiations so
2629 -- examine package or subprogram declaration of the main unit, when it
2632 if Nkind
(Unit
(N
)) = N_Package_Body
2633 or else (Nkind
(Unit
(N
)) = N_Subprogram_Body
2634 and then not Acts_As_Spec
(N
))
2636 Do_Search
(Library_Unit
(N
));
2641 -- Unnest any subprograms passed on the list of inlined subprograms
2643 Subp
:= First_Inlined_Subprogram
(N
);
2645 while Present
(Subp
) loop
2646 Subp_Body
:= Parent
(Declaration_Node
(Subp
));
2648 if Nkind
(Subp_Body
) = N_Subprogram_Declaration
2649 and then Present
(Corresponding_Body
(Subp_Body
))
2651 Subp_Body
:= Parent
(Declaration_Node
2652 (Corresponding_Body
(Subp_Body
)));
2655 Unnest_Subprogram
(Subp
, Subp_Body
, For_Inline
=> True);
2656 Next_Inlined_Subprogram
(Subp
);
2658 end Unnest_Subprograms
;