1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2014-2018, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Debug
; use Debug
;
28 with Einfo
; use Einfo
;
29 with Elists
; use Elists
;
31 with Namet
; use Namet
;
32 with Nlists
; use Nlists
;
33 with Nmake
; use Nmake
;
35 with Output
; use Output
;
36 with Rtsfind
; use Rtsfind
;
38 with Sem_Aux
; use Sem_Aux
;
39 with Sem_Ch8
; use Sem_Ch8
;
40 with Sem_Mech
; use Sem_Mech
;
41 with Sem_Res
; use Sem_Res
;
42 with Sem_Util
; use Sem_Util
;
43 with Sinfo
; use Sinfo
;
44 with Sinput
; use Sinput
;
45 with Snames
; use Snames
;
46 with Stand
; use Stand
;
47 with Tbuild
; use Tbuild
;
48 with Uintp
; use Uintp
;
50 package body Exp_Unst
is
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
56 procedure Unnest_Subprogram
(Subp
: Entity_Id
; Subp_Body
: Node_Id
);
57 -- Subp is a library-level subprogram which has nested subprograms, and
58 -- Subp_Body is the corresponding N_Subprogram_Body node. This procedure
59 -- declares the AREC types and objects, adds assignments to the AREC record
60 -- as required, defines the xxxPTR types for uplevel referenced objects,
61 -- adds the ARECP parameter to all nested subprograms which need it, and
62 -- modifies all uplevel references appropriately.
68 -- Table to record calls within the nest being analyzed. These are the
69 -- calls which may need to have an AREC actual added. This table is built
70 -- new for each subprogram nest and cleared at the end of processing each
73 type Call_Entry
is record
78 -- Entity of the subprogram containing the call (can be at any level)
81 -- Entity of the subprogram called (always at level 2 or higher). Note
82 -- that in accordance with the basic rules of nesting, the level of To
83 -- is either less than or equal to the level of From, or one greater.
86 package Calls
is new Table
.Table
(
87 Table_Component_Type
=> Call_Entry
,
88 Table_Index_Type
=> Nat
,
91 Table_Increment
=> 200,
92 Table_Name
=> "Unnest_Calls");
93 -- Records each call within the outer subprogram and all nested subprograms
94 -- that are to other subprograms nested within the outer subprogram. These
95 -- are the calls that may need an additional parameter.
97 procedure Append_Unique_Call
(Call
: Call_Entry
);
98 -- Append a call entry to the Calls table. A check is made to see if the
99 -- table already contains this entry and if so it has no effect.
101 ----------------------------------
102 -- Subprograms For Fat Pointers --
103 ----------------------------------
105 function Build_Access_Type_Decl
107 Scop
: Entity_Id
) return Node_Id
;
108 -- For an uplevel reference that involves an unconstrained array type,
109 -- build an access type declaration for the corresponding activation
110 -- record component. The relevant attributes of the access type are
111 -- set here to avoid a full analysis that would require a scope stack.
113 function Needs_Fat_Pointer
(E
: Entity_Id
) return Boolean;
114 -- A formal parameter of an unconstrained array type that appears in an
115 -- uplevel reference requires the construction of an access type, to be
116 -- used in the corresponding component declaration.
122 -- Table to record explicit uplevel references to objects (variables,
123 -- constants, formal parameters). These are the references that will
124 -- need rewriting to use the activation table (AREC) pointers. Also
125 -- included are implicit and explicit uplevel references to types, but
126 -- these do not get rewritten by the front end. This table is built new
127 -- for each subprogram nest and cleared at the end of processing each
130 type Uref_Entry
is record
132 -- The reference itself. For objects this is always an entity reference
133 -- and the referenced entity will have its Is_Uplevel_Referenced_Entity
134 -- flag set and will appear in the Uplevel_Referenced_Entities list of
135 -- the subprogram declaring this entity.
138 -- The Entity_Id of the uplevel referenced object or type
141 -- The entity for the subprogram immediately containing this entity
144 -- The entity for the subprogram containing the referenced entity. Note
145 -- that the level of Callee must be less than the level of Caller, since
146 -- this is an uplevel reference.
149 package Urefs
is new Table
.Table
(
150 Table_Component_Type
=> Uref_Entry
,
151 Table_Index_Type
=> Nat
,
152 Table_Low_Bound
=> 1,
153 Table_Initial
=> 100,
154 Table_Increment
=> 200,
155 Table_Name
=> "Unnest_Urefs");
157 ------------------------
158 -- Append_Unique_Call --
159 ------------------------
161 procedure Append_Unique_Call
(Call
: Call_Entry
) is
163 for J
in Calls
.First
.. Calls
.Last
loop
164 if Calls
.Table
(J
) = Call
then
170 end Append_Unique_Call
;
172 -----------------------------
173 -- Build_Access_Type_Decl --
174 -----------------------------
176 function Build_Access_Type_Decl
178 Scop
: Entity_Id
) return Node_Id
180 Loc
: constant Source_Ptr
:= Sloc
(E
);
184 Typ
:= Make_Temporary
(Loc
, 'S');
185 Set_Ekind
(Typ
, E_General_Access_Type
);
186 Set_Etype
(Typ
, Typ
);
187 Set_Scope
(Typ
, Scop
);
188 Set_Directly_Designated_Type
(Typ
, Etype
(E
));
191 Make_Full_Type_Declaration
(Loc
,
192 Defining_Identifier
=> Typ
,
194 Make_Access_To_Object_Definition
(Loc
,
195 Subtype_Indication
=> New_Occurrence_Of
(Etype
(E
), Loc
)));
196 end Build_Access_Type_Decl
;
202 function Get_Level
(Subp
: Entity_Id
; Sub
: Entity_Id
) return Nat
is
214 S
:= Enclosing_Subprogram
(S
);
219 --------------------------
220 -- In_Synchronized_Unit --
221 --------------------------
223 function In_Synchronized_Unit
(Subp
: Entity_Id
) return Boolean is
224 S
: Entity_Id
:= Scope
(Subp
);
227 while Present
(S
) and then S
/= Standard_Standard
loop
228 if Is_Concurrent_Type
(S
) then
236 end In_Synchronized_Unit
;
238 -----------------------
239 -- Needs_Fat_Pointer --
240 -----------------------
242 function Needs_Fat_Pointer
(E
: Entity_Id
) return Boolean is
245 and then Is_Array_Type
(Etype
(E
))
246 and then not Is_Constrained
(Etype
(E
));
247 end Needs_Fat_Pointer
;
253 function Subp_Index
(Sub
: Entity_Id
) return SI_Type
is
254 E
: Entity_Id
:= Sub
;
257 pragma Assert
(Is_Subprogram
(E
));
259 if Subps_Index
(E
) = Uint_0
then
260 E
:= Ultimate_Alias
(E
);
262 if Ekind
(E
) = E_Function
263 and then Rewritten_For_C
(E
)
264 and then Present
(Corresponding_Procedure
(E
))
266 E
:= Corresponding_Procedure
(E
);
270 pragma Assert
(Subps_Index
(E
) /= Uint_0
);
271 return SI_Type
(UI_To_Int
(Subps_Index
(E
)));
274 -----------------------
275 -- Unnest_Subprogram --
276 -----------------------
278 procedure Unnest_Subprogram
(Subp
: Entity_Id
; Subp_Body
: Node_Id
) is
279 function AREC_Name
(J
: Pos
; S
: String) return Name_Id
;
280 -- Returns name for string ARECjS, where j is the decimal value of j
282 function Enclosing_Subp
(Subp
: SI_Type
) return SI_Type
;
283 -- Subp is the index of a subprogram which has a Lev greater than 1.
284 -- This function returns the index of the enclosing subprogram which
285 -- will have a Lev value one less than this.
287 function Img_Pos
(N
: Pos
) return String;
288 -- Return image of N without leading blank
293 Clist
: List_Id
) return Name_Id
;
294 -- This function returns the name to be used in the activation record to
295 -- reference the variable uplevel. Clist is the list of components that
296 -- have been created in the activation record so far. Normally the name
297 -- is just a copy of the Chars field of the entity. The exception is
298 -- when the name has already been used, in which case we suffix the name
299 -- with the index value Index to avoid duplication. This happens with
300 -- declare blocks and generic parameters at least.
306 function AREC_Name
(J
: Pos
; S
: String) return Name_Id
is
308 return Name_Find
("AREC" & Img_Pos
(J
) & S
);
315 function Enclosing_Subp
(Subp
: SI_Type
) return SI_Type
is
316 STJ
: Subp_Entry
renames Subps
.Table
(Subp
);
317 Ret
: constant SI_Type
:= Subp_Index
(Enclosing_Subprogram
(STJ
.Ent
));
319 pragma Assert
(STJ
.Lev
> 1);
320 pragma Assert
(Subps
.Table
(Ret
).Lev
= STJ
.Lev
- 1);
328 function Img_Pos
(N
: Pos
) return String is
329 Buf
: String (1 .. 20);
337 Buf
(Ptr
) := Character'Val (48 + NV
mod 10);
342 return Buf
(Ptr
+ 1 .. Buf
'Last);
352 Clist
: List_Id
) return Name_Id
361 elsif Chars
(Defining_Identifier
(C
)) = Chars
(Ent
) then
363 Name_Find
(Get_Name_String
(Chars
(Ent
)) & Img_Pos
(Index
));
370 -- Start of processing for Unnest_Subprogram
373 -- Nothing to do inside a generic (all processing is for instance)
375 if Inside_A_Generic
then
379 -- If the main unit is a package body then we need to examine the spec
380 -- to determine whether the main unit is generic (the scope stack is not
381 -- present when this is called on the main unit).
383 if Ekind
(Cunit_Entity
(Main_Unit
)) = E_Package_Body
384 and then Is_Generic_Unit
(Spec_Entity
(Cunit_Entity
(Main_Unit
)))
389 -- Only unnest when generating code for the main source unit
391 if not In_Extended_Main_Code_Unit
(Subp_Body
) then
395 -- This routine is called late, after the scope stack is gone. The
396 -- following creates a suitable dummy scope stack to be used for the
397 -- analyze/expand calls made from this routine.
401 -- First step, we must mark all nested subprograms that require a static
402 -- link (activation record) because either they contain explicit uplevel
403 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
404 -- this point), or they make calls to other subprograms in the same nest
405 -- that require a static link (in which case we set this flag).
407 -- This is a recursive definition, and to implement this, we have to
408 -- build a call graph for the set of nested subprograms, and then go
409 -- over this graph to implement recursively the invariant that if a
410 -- subprogram has a call to a subprogram requiring a static link, then
411 -- the calling subprogram requires a static link.
413 -- First populate the above tables
415 Subps_First
:= Subps
.Last
+ 1;
419 Build_Tables
: declare
420 Current_Subprogram
: Entity_Id
;
421 -- When we scan a subprogram body, we set Current_Subprogram to the
422 -- corresponding entity. This gets recursively saved and restored.
424 function Visit_Node
(N
: Node_Id
) return Traverse_Result
;
425 -- Visit a single node in Subp
431 procedure Visit
is new Traverse_Proc
(Visit_Node
);
432 -- Used to traverse the body of Subp, populating the tables
438 function Visit_Node
(N
: Node_Id
) return Traverse_Result
is
443 procedure Check_Static_Type
444 (T
: Entity_Id
; N
: Node_Id
; DT
: in out Boolean);
445 -- Given a type T, checks if it is a static type defined as a type
446 -- with no dynamic bounds in sight. If so, the only action is to
447 -- set Is_Static_Type True for T. If T is not a static type, then
448 -- all types with dynamic bounds associated with T are detected,
449 -- and their bounds are marked as uplevel referenced if not at the
450 -- library level, and DT is set True. If N is specified, it's the
451 -- node that will need to be replaced. If not specified, it means
452 -- we can't do a replacement because the bound is implicit.
454 procedure Note_Uplevel_Ref
459 -- Called when we detect an explicit or implicit uplevel reference
460 -- from within Caller to entity E declared in Callee. E can be a
461 -- an object or a type.
463 procedure Register_Subprogram
(E
: Entity_Id
; Bod
: Node_Id
);
464 -- Enter a subprogram whose body is visible or which is a
465 -- subprogram instance into the subprogram table.
467 -----------------------
468 -- Check_Static_Type --
469 -----------------------
471 procedure Check_Static_Type
472 (T
: Entity_Id
; N
: Node_Id
; DT
: in out Boolean)
474 procedure Note_Uplevel_Bound
(N
: Node_Id
; Ref
: Node_Id
);
475 -- N is the bound of a dynamic type. This procedure notes that
476 -- this bound is uplevel referenced, it can handle references
477 -- to entities (typically _FIRST and _LAST entities), and also
478 -- attribute references of the form T'name (name is typically
479 -- FIRST or LAST) where T is the uplevel referenced bound.
480 -- Ref, if Present, is the location of the reference to
483 ------------------------
484 -- Note_Uplevel_Bound --
485 ------------------------
487 procedure Note_Uplevel_Bound
(N
: Node_Id
; Ref
: Node_Id
) is
489 -- Entity name case. Make sure that the entity is declared
490 -- in a subprogram. This may not be the case for for a type
491 -- in a loop appearing in a precondition.
492 -- Exclude explicitly discriminants (that can appear
493 -- in bounds of discriminated components).
495 if Is_Entity_Name
(N
) then
496 if Present
(Entity
(N
))
497 and then Present
(Enclosing_Subprogram
(Entity
(N
)))
498 and then Ekind
(Entity
(N
)) /= E_Discriminant
503 Caller
=> Current_Subprogram
,
504 Callee
=> Enclosing_Subprogram
(Entity
(N
)));
507 -- Attribute or indexed component case
509 elsif Nkind_In
(N
, N_Attribute_Reference
,
512 Note_Uplevel_Bound
(Prefix
(N
), Ref
);
514 -- The indices of the indexed components, or the
515 -- associated expressions of an attribute reference,
516 -- may also involve uplevel references.
522 Expr
:= First
(Expressions
(N
));
523 while Present
(Expr
) loop
524 Note_Uplevel_Bound
(Expr
, Ref
);
531 elsif Nkind
(N
) = N_Type_Conversion
then
532 Note_Uplevel_Bound
(Expression
(N
), Ref
);
534 end Note_Uplevel_Bound
;
536 -- Start of processing for Check_Static_Type
539 -- If already marked static, immediate return
541 if Is_Static_Type
(T
) then
545 -- If the type is at library level, always consider it static,
546 -- since such uplevel references are irrelevant.
548 if Is_Library_Level_Entity
(T
) then
549 Set_Is_Static_Type
(T
);
553 -- Otherwise figure out what the story is with this type
555 -- For a scalar type, check bounds
557 if Is_Scalar_Type
(T
) then
559 -- If both bounds static, then this is a static type
562 LB
: constant Node_Id
:= Type_Low_Bound
(T
);
563 UB
: constant Node_Id
:= Type_High_Bound
(T
);
566 if not Is_Static_Expression
(LB
) then
567 Note_Uplevel_Bound
(LB
, N
);
571 if not Is_Static_Expression
(UB
) then
572 Note_Uplevel_Bound
(UB
, N
);
577 -- For record type, check all components and discriminant
578 -- constraints if present.
580 elsif Is_Record_Type
(T
) then
586 C
:= First_Component_Or_Discriminant
(T
);
587 while Present
(C
) loop
588 Check_Static_Type
(Etype
(C
), N
, DT
);
589 Next_Component_Or_Discriminant
(C
);
592 if Has_Discriminants
(T
)
593 and then Present
(Discriminant_Constraint
(T
))
595 D
:= First_Elmt
(Discriminant_Constraint
(T
));
596 while Present
(D
) loop
597 if not Is_Static_Expression
(Node
(D
)) then
598 Note_Uplevel_Bound
(Node
(D
), N
);
607 -- For array type, check index types and component type
609 elsif Is_Array_Type
(T
) then
613 Check_Static_Type
(Component_Type
(T
), N
, DT
);
615 IX
:= First_Index
(T
);
616 while Present
(IX
) loop
617 Check_Static_Type
(Etype
(IX
), N
, DT
);
622 -- For private type, examine whether full view is static
624 elsif Is_Private_Type
(T
) and then Present
(Full_View
(T
)) then
625 Check_Static_Type
(Full_View
(T
), N
, DT
);
627 if Is_Static_Type
(Full_View
(T
)) then
628 Set_Is_Static_Type
(T
);
631 -- For now, ignore other types
638 Set_Is_Static_Type
(T
);
640 end Check_Static_Type
;
642 ----------------------
643 -- Note_Uplevel_Ref --
644 ----------------------
646 procedure Note_Uplevel_Ref
652 Full_E
: Entity_Id
:= E
;
654 -- Nothing to do for static type
656 if Is_Static_Type
(E
) then
660 -- Nothing to do if Caller and Callee are the same
662 if Caller
= Callee
then
665 -- Callee may be a function that returns an array, and that has
666 -- been rewritten as a procedure. If caller is that procedure,
667 -- nothing to do either.
669 elsif Ekind
(Callee
) = E_Function
670 and then Rewritten_For_C
(Callee
)
671 and then Corresponding_Procedure
(Callee
) = Caller
676 -- We have a new uplevel referenced entity
678 if Ekind
(E
) = E_Constant
and then Present
(Full_View
(E
)) then
679 Full_E
:= Full_View
(E
);
682 -- All we do at this stage is to add the uplevel reference to
683 -- the table. It's too early to do anything else, since this
684 -- uplevel reference may come from an unreachable subprogram
685 -- in which case the entry will be deleted.
687 Urefs
.Append
((N
, Full_E
, Caller
, Callee
));
688 end Note_Uplevel_Ref
;
690 -------------------------
691 -- Register_Subprogram --
692 -------------------------
694 procedure Register_Subprogram
(E
: Entity_Id
; Bod
: Node_Id
) is
695 L
: constant Nat
:= Get_Level
(Subp
, E
);
704 Declares_AREC
=> False,
714 Set_Subps_Index
(E
, UI_From_Int
(Subps
.Last
));
715 end Register_Subprogram
;
717 -- Start of processing for Visit_Node
722 -- Record a subprogram call
725 | N_Procedure_Call_Statement
727 -- We are only interested in direct calls, not indirect
728 -- calls (where Name (N) is an explicit dereference) at
731 if Nkind
(Name
(N
)) in N_Has_Entity
then
732 Ent
:= Entity
(Name
(N
));
734 -- We are only interested in calls to subprograms nested
735 -- within Subp. Calls to Subp itself or to subprograms
736 -- outside the nested structure do not affect us.
738 if Scope_Within
(Ent
, Subp
)
739 and then Is_Subprogram
(Ent
)
740 and then not Is_Imported
(Ent
)
742 Append_Unique_Call
((N
, Current_Subprogram
, Ent
));
746 -- For all calls where the formal is an unconstrained array
747 -- and the actual is constrained we need to check the bounds
748 -- for uplevel references.
752 DT
: Boolean := False;
757 if Nkind
(Name
(N
)) = N_Explicit_Dereference
then
758 Subp
:= Etype
(Name
(N
));
760 Subp
:= Entity
(Name
(N
));
763 Actual
:= First_Actual
(N
);
764 Formal
:= First_Formal_With_Extras
(Subp
);
765 while Present
(Actual
) loop
766 if Is_Array_Type
(Etype
(Formal
))
767 and then not Is_Constrained
(Etype
(Formal
))
768 and then Is_Constrained
(Etype
(Actual
))
770 Check_Static_Type
(Etype
(Actual
), Empty
, DT
);
773 Next_Actual
(Actual
);
774 Next_Formal_With_Extras
(Formal
);
778 -- An At_End_Proc in a statement sequence indicates that there
779 -- is a call from the enclosing construct or block to that
780 -- subprogram. As above, the called entity must be local and
783 when N_Handled_Sequence_Of_Statements
=>
784 if Present
(At_End_Proc
(N
))
785 and then Scope_Within
(Entity
(At_End_Proc
(N
)), Subp
)
786 and then not Is_Imported
(Entity
(At_End_Proc
(N
)))
789 ((N
, Current_Subprogram
, Entity
(At_End_Proc
(N
))));
792 -- Similarly, the following constructs include a semantic
793 -- attribute Procedure_To_Call that must be handled like
797 | N_Extended_Return_Statement
799 | N_Simple_Return_Statement
802 Proc
: constant Entity_Id
:= Procedure_To_Call
(N
);
805 and then Scope_Within
(Proc
, Subp
)
806 and then not Is_Imported
(Proc
)
808 Append_Unique_Call
((N
, Current_Subprogram
, Proc
));
812 -- For an allocator with a qualified expression, check type
813 -- of expression being qualified. The explicit type name is
814 -- handled as an entity reference.
816 if Nkind
(N
) = N_Allocator
817 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
820 DT
: Boolean := False;
823 (Etype
(Expression
(Expression
(N
))), Empty
, DT
);
827 -- A 'Access reference is a (potential) call. Other attributes
828 -- require special handling.
830 when N_Attribute_Reference
=>
832 Attr
: constant Attribute_Id
:=
833 Get_Attribute_Id
(Attribute_Name
(N
));
836 when Attribute_Access
837 | Attribute_Unchecked_Access
838 | Attribute_Unrestricted_Access
840 if Nkind
(Prefix
(N
)) in N_Has_Entity
then
841 Ent
:= Entity
(Prefix
(N
));
843 -- We only need to examine calls to subprograms
844 -- nested within current Subp.
846 if Scope_Within
(Ent
, Subp
) then
847 if Is_Imported
(Ent
) then
850 elsif Is_Subprogram
(Ent
) then
852 ((N
, Current_Subprogram
, Ent
));
857 -- References to bounds can be uplevel references if
858 -- the type isn't static.
864 -- Special-case attributes of objects whose bounds
865 -- may be uplevel references. More complex prefixes
866 -- handled during full traversal. Note that if the
867 -- nominal subtype of the prefix is unconstrained,
868 -- the bound must be obtained from the object, not
869 -- from the (possibly) uplevel reference.
871 if Is_Constrained
(Etype
(Prefix
(N
))) then
873 DT
: Boolean := False;
876 (Etype
(Prefix
(N
)), Empty
, DT
);
887 -- Component associations in aggregates are either static or
888 -- else the aggregate will be expanded into assignments, in
889 -- which case the expression is analyzed later and provides
890 -- no relevant code generation.
892 when N_Component_Association
=>
893 if No
(Etype
(Expression
(N
))) then
897 -- Generic associations are not analyzed: the actuals are
898 -- transferred to renaming and subtype declarations that
899 -- are the ones that must be examined.
901 when N_Generic_Association
=>
904 -- Indexed references can be uplevel if the type isn't static
905 -- and if the lower bound (or an inner bound for a multi-
906 -- dimensional array) is uplevel.
908 when N_Indexed_Component | N_Slice
=>
909 if Is_Constrained
(Etype
(Prefix
(N
))) then
911 DT
: Boolean := False;
913 Check_Static_Type
(Etype
(Prefix
(N
)), Empty
, DT
);
917 -- A selected component can have an implicit up-level
918 -- reference due to the bounds of previous fields in the
919 -- record. We simplify the processing here by examining
920 -- all components of the record.
922 -- Selected components appear as unit names and end labels
923 -- for child units. Prefixes of these nodes denote parent
924 -- units and carry no type information so they are skipped.
926 when N_Selected_Component
=>
927 if Present
(Etype
(Prefix
(N
))) then
929 DT
: Boolean := False;
931 Check_Static_Type
(Etype
(Prefix
(N
)), Empty
, DT
);
935 -- Record a subprogram. We record a subprogram body that acts
936 -- as a spec. Otherwise we record a subprogram declaration,
937 -- providing that it has a corresponding body we can get hold
938 -- of. The case of no corresponding body being available is
941 when N_Subprogram_Body
=>
942 Ent
:= Unique_Defining_Entity
(N
);
944 -- Ignore generic subprogram
946 if Is_Generic_Subprogram
(Ent
) then
950 -- Make new entry in subprogram table if not already made
952 Register_Subprogram
(Ent
, N
);
954 -- We make a recursive call to scan the subprogram body, so
955 -- that we can save and restore Current_Subprogram.
958 Save_CS
: constant Entity_Id
:= Current_Subprogram
;
962 Current_Subprogram
:= Ent
;
966 Decl
:= First
(Declarations
(N
));
967 while Present
(Decl
) loop
974 Visit
(Handled_Statement_Sequence
(N
));
976 -- Restore current subprogram setting
978 Current_Subprogram
:= Save_CS
;
981 -- Now at this level, return skipping the subprogram body
982 -- descendants, since we already took care of them!
986 -- If we have a body stub, visit the associated subunit, which
987 -- is a semantic descendant of the stub.
990 Visit
(Library_Unit
(N
));
992 -- A declaration of a wrapper package indicates a subprogram
993 -- instance for which there is no explicit body. Enter the
994 -- subprogram instance in the table.
996 when N_Package_Declaration
=>
997 if Is_Wrapper_Package
(Defining_Entity
(N
)) then
999 (Related_Instance
(Defining_Entity
(N
)), Empty
);
1002 -- Skip generic declarations
1004 when N_Generic_Declaration
=>
1007 -- Skip generic package body
1009 when N_Package_Body
=>
1010 if Present
(Corresponding_Spec
(N
))
1011 and then Ekind
(Corresponding_Spec
(N
)) = E_Generic_Package
1016 -- Otherwise record an uplevel reference in a local
1020 if Nkind
(N
) in N_Has_Entity
1021 and then Present
(Entity
(N
))
1025 -- Only interested in entities declared within our nest
1027 if not Is_Library_Level_Entity
(Ent
)
1028 and then Scope_Within_Or_Same
(Scope
(Ent
), Subp
)
1030 -- Skip entities defined in inlined subprograms
1033 Chars
(Enclosing_Subprogram
(Ent
)) /= Name_uParent
1035 -- Constants and variables are potentially uplevel
1036 -- references to global declarations.
1039 (Ekind_In
(Ent
, E_Constant
, E_Variable
)
1041 -- Formals are interesting, but not if being used as
1042 -- mere names of parameters for name notation calls.
1047 (Nkind
(Parent
(N
)) = N_Parameter_Association
1048 and then Selector_Name
(Parent
(N
)) = N
))
1050 -- Types other than known Is_Static types are
1051 -- potentially interesting.
1053 or else (Is_Type
(Ent
)
1054 and then not Is_Static_Type
(Ent
)))
1056 -- Here we have a potentially interesting uplevel
1057 -- reference to examine.
1059 if Is_Type
(Ent
) then
1061 DT
: Boolean := False;
1064 Check_Static_Type
(Ent
, N
, DT
);
1066 if Is_Static_Type
(Ent
) then
1072 Caller
:= Current_Subprogram
;
1073 Callee
:= Enclosing_Subprogram
(Ent
);
1076 and then (not Is_Static_Type
(Ent
)
1077 or else Needs_Fat_Pointer
(Ent
))
1079 Note_Uplevel_Ref
(Ent
, N
, Caller
, Callee
);
1081 -- Check the type of a formal parameter of the current
1082 -- subprogram, whose formal type may be an uplevel
1085 elsif Is_Formal
(Ent
)
1086 and then Scope
(Ent
) = Current_Subprogram
1089 DT
: Boolean := False;
1092 Check_Static_Type
(Etype
(Ent
), Empty
, DT
);
1099 -- Fall through to continue scanning children of this node
1104 -- Start of processing for Build_Tables
1107 -- Traverse the body to get subprograms, calls and uplevel references
1112 -- Now do the first transitive closure which determines which
1113 -- subprograms in the nest are actually reachable.
1115 Reachable_Closure
: declare
1119 Subps
.Table
(Subps_First
).Reachable
:= True;
1121 -- We use a simple minded algorithm as follows (obviously this can
1122 -- be done more efficiently, using one of the standard algorithms
1123 -- for efficient transitive closure computation, but this is simple
1124 -- and most likely fast enough that its speed does not matter).
1126 -- Repeatedly scan the list of calls. Any time we find a call from
1127 -- A to B, where A is reachable, but B is not, then B is reachable,
1128 -- and note that we have made a change by setting Modified True. We
1129 -- repeat this until we make a pass with no modifications.
1133 Inner
: for J
in Calls
.First
.. Calls
.Last
loop
1135 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1137 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1138 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1140 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1141 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1144 if SUBF
.Reachable
and then not SUBT
.Reachable
then
1145 SUBT
.Reachable
:= True;
1151 exit Outer
when not Modified
;
1153 end Reachable_Closure
;
1155 -- Remove calls from unreachable subprograms
1162 for J
in Calls
.First
.. Calls
.Last
loop
1164 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1166 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1167 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1169 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1170 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1173 if SUBF
.Reachable
then
1174 pragma Assert
(SUBT
.Reachable
);
1175 New_Index
:= New_Index
+ 1;
1176 Calls
.Table
(New_Index
) := Calls
.Table
(J
);
1181 Calls
.Set_Last
(New_Index
);
1184 -- Remove uplevel references from unreachable subprograms
1191 for J
in Urefs
.First
.. Urefs
.Last
loop
1193 URJ
: Uref_Entry
renames Urefs
.Table
(J
);
1195 SINF
: constant SI_Type
:= Subp_Index
(URJ
.Caller
);
1196 SINT
: constant SI_Type
:= Subp_Index
(URJ
.Callee
);
1198 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1199 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1204 -- Keep reachable reference
1206 if SUBF
.Reachable
then
1207 New_Index
:= New_Index
+ 1;
1208 Urefs
.Table
(New_Index
) := Urefs
.Table
(J
);
1210 -- And since we know we are keeping this one, this is a good
1211 -- place to fill in information for a good reference.
1213 -- Mark all enclosing subprograms need to declare AREC
1217 S
:= Enclosing_Subprogram
(S
);
1219 -- if we are at the top level, as can happen with
1220 -- references to formals in aspects of nested subprogram
1221 -- declarations, there are no further subprograms to
1222 -- mark as requiring activation records.
1225 Subps
.Table
(Subp_Index
(S
)).Declares_AREC
:= True;
1226 exit when S
= URJ
.Callee
;
1229 -- Add to list of uplevel referenced entities for Callee.
1230 -- We do not add types to this list, only actual references
1231 -- to objects that will be referenced uplevel, and we use
1232 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1233 -- duplicate entries in the list.
1234 -- Discriminants are also excluded, only the enclosing
1235 -- object can appear in the list.
1237 if not Is_Uplevel_Referenced_Entity
(URJ
.Ent
)
1238 and then Ekind
(URJ
.Ent
) /= E_Discriminant
1240 Set_Is_Uplevel_Referenced_Entity
(URJ
.Ent
);
1242 if not Is_Type
(URJ
.Ent
) then
1243 Append_New_Elmt
(URJ
.Ent
, SUBT
.Uents
);
1247 -- And set uplevel indication for caller
1249 if SUBT
.Lev
< SUBF
.Uplevel_Ref
then
1250 SUBF
.Uplevel_Ref
:= SUBT
.Lev
;
1256 Urefs
.Set_Last
(New_Index
);
1259 -- Remove unreachable subprograms from Subps table. Note that we do
1260 -- this after eliminating entries from the other two tables, since
1261 -- those elimination steps depend on referencing the Subps table.
1267 New_SI
:= Subps_First
- 1;
1268 for J
in Subps_First
.. Subps
.Last
loop
1270 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1275 -- Subprograms declared in tasks and protected types are
1276 -- reachable and cannot be eliminated.
1278 if In_Synchronized_Unit
(STJ
.Ent
) then
1279 STJ
.Reachable
:= True;
1282 -- Subprogram is reachable, copy and reset index
1284 if STJ
.Reachable
then
1285 New_SI
:= New_SI
+ 1;
1286 Subps
.Table
(New_SI
) := STJ
;
1287 Set_Subps_Index
(STJ
.Ent
, UI_From_Int
(New_SI
));
1289 -- Subprogram is not reachable
1292 -- Clear index, since no longer active
1294 Set_Subps_Index
(Subps
.Table
(J
).Ent
, Uint_0
);
1296 -- Output debug information if -gnatd.3 set
1298 if Debug_Flag_Dot_3
then
1299 Write_Str
("Eliminate ");
1300 Write_Name
(Chars
(Subps
.Table
(J
).Ent
));
1302 Write_Location
(Sloc
(Subps
.Table
(J
).Ent
));
1303 Write_Str
(" (not referenced)");
1307 -- Rewrite declaration and body to null statements
1309 -- A subprogram instantiation does not have an explicit
1310 -- body. If unused, we could remove the corresponding
1311 -- wrapper package and its body (TBD).
1313 if Present
(STJ
.Bod
) then
1314 Spec
:= Corresponding_Spec
(STJ
.Bod
);
1316 if Present
(Spec
) then
1317 Decl
:= Parent
(Declaration_Node
(Spec
));
1318 Rewrite
(Decl
, Make_Null_Statement
(Sloc
(Decl
)));
1321 Rewrite
(STJ
.Bod
, Make_Null_Statement
(Sloc
(STJ
.Bod
)));
1327 Subps
.Set_Last
(New_SI
);
1330 -- Now it is time for the second transitive closure, which follows calls
1331 -- and makes sure that A calls B, and B has uplevel references, then A
1332 -- is also marked as having uplevel references.
1334 Closure_Uplevel
: declare
1338 -- We use a simple minded algorithm as follows (obviously this can
1339 -- be done more efficiently, using one of the standard algorithms
1340 -- for efficient transitive closure computation, but this is simple
1341 -- and most likely fast enough that its speed does not matter).
1343 -- Repeatedly scan the list of calls. Any time we find a call from
1344 -- A to B, where B has uplevel references, make sure that A is marked
1345 -- as having at least the same level of uplevel referencing.
1349 Inner2
: for J
in Calls
.First
.. Calls
.Last
loop
1351 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1352 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1353 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1354 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1355 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1357 if SUBT
.Lev
> SUBT
.Uplevel_Ref
1358 and then SUBF
.Uplevel_Ref
> SUBT
.Uplevel_Ref
1360 SUBF
.Uplevel_Ref
:= SUBT
.Uplevel_Ref
;
1366 exit Outer2
when not Modified
;
1368 end Closure_Uplevel
;
1370 -- We have one more step before the tables are complete. An uplevel
1371 -- call from subprogram A to subprogram B where subprogram B has uplevel
1372 -- references is in effect an uplevel reference, and must arrange for
1373 -- the proper activation link to be passed.
1375 for J
in Calls
.First
.. Calls
.Last
loop
1377 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1379 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1380 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1382 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1383 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1388 -- If callee has uplevel references
1390 if SUBT
.Uplevel_Ref
< SUBT
.Lev
1392 -- And this is an uplevel call
1394 and then SUBT
.Lev
< SUBF
.Lev
1396 -- We need to arrange for finding the uplink
1400 A
:= Enclosing_Subprogram
(A
);
1401 Subps
.Table
(Subp_Index
(A
)).Declares_AREC
:= True;
1402 exit when A
= CTJ
.Callee
;
1404 -- In any case exit when we get to the outer level. This
1405 -- happens in some odd cases with generics (in particular
1406 -- sem_ch3.adb does not compile without this kludge ???).
1414 -- The tables are now complete, so we can record the last index in the
1415 -- Subps table for later reference in Cprint.
1417 Subps
.Table
(Subps_First
).Last
:= Subps
.Last
;
1419 -- Next step, create the entities for code we will insert. We do this
1420 -- at the start so that all the entities are defined, regardless of the
1421 -- order in which we do the code insertions.
1423 Create_Entities
: for J
in Subps_First
.. Subps
.Last
loop
1425 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1426 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1429 -- First we create the ARECnF entity for the additional formal for
1430 -- all subprograms which need an activation record passed.
1432 if STJ
.Uplevel_Ref
< STJ
.Lev
then
1434 Make_Defining_Identifier
(Loc
, Chars
=> AREC_Name
(J
, "F"));
1437 -- Define the AREC entities for the activation record if needed
1439 if STJ
.Declares_AREC
then
1441 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, ""));
1443 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "T"));
1445 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "PT"));
1447 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "P"));
1449 -- Define uplink component entity if inner nesting case
1451 if Present
(STJ
.ARECnF
) then
1453 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "U"));
1457 end loop Create_Entities
;
1459 -- Loop through subprograms
1462 Addr
: constant Entity_Id
:= RTE
(RE_Address
);
1465 for J
in Subps_First
.. Subps
.Last
loop
1467 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1470 -- First add the extra formal if needed. This applies to all
1471 -- nested subprograms that require an activation record to be
1472 -- passed, as indicated by ARECnF being defined.
1474 if Present
(STJ
.ARECnF
) then
1476 -- Here we need the extra formal. We do the expansion and
1477 -- analysis of this manually, since it is fairly simple,
1478 -- and it is not obvious how we can get what we want if we
1479 -- try to use the normal Analyze circuit.
1481 Add_Extra_Formal
: declare
1482 Encl
: constant SI_Type
:= Enclosing_Subp
(J
);
1483 STJE
: Subp_Entry
renames Subps
.Table
(Encl
);
1484 -- Index and Subp_Entry for enclosing routine
1486 Form
: constant Entity_Id
:= STJ
.ARECnF
;
1487 -- The formal to be added. Note that n here is one less
1488 -- than the level of the subprogram itself (STJ.Ent).
1490 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
);
1491 -- S is an N_Function/Procedure_Specification node, and F
1492 -- is the new entity to add to this subprogramn spec as
1493 -- the last Extra_Formal.
1495 ----------------------
1496 -- Add_Form_To_Spec --
1497 ----------------------
1499 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
) is
1500 Sub
: constant Entity_Id
:= Defining_Entity
(S
);
1504 -- Case of at least one Extra_Formal is present, set
1505 -- ARECnF as the new last entry in the list.
1507 if Present
(Extra_Formals
(Sub
)) then
1508 Ent
:= Extra_Formals
(Sub
);
1509 while Present
(Extra_Formal
(Ent
)) loop
1510 Ent
:= Extra_Formal
(Ent
);
1513 Set_Extra_Formal
(Ent
, F
);
1515 -- No Extra formals present
1518 Set_Extra_Formals
(Sub
, F
);
1519 Ent
:= Last_Formal
(Sub
);
1521 if Present
(Ent
) then
1522 Set_Extra_Formal
(Ent
, F
);
1525 end Add_Form_To_Spec
;
1527 -- Start of processing for Add_Extra_Formal
1530 -- Decorate the new formal entity
1532 Set_Scope
(Form
, STJ
.Ent
);
1533 Set_Ekind
(Form
, E_In_Parameter
);
1534 Set_Etype
(Form
, STJE
.ARECnPT
);
1535 Set_Mechanism
(Form
, By_Copy
);
1536 Set_Never_Set_In_Source
(Form
, True);
1537 Set_Analyzed
(Form
, True);
1538 Set_Comes_From_Source
(Form
, False);
1539 Set_Is_Activation_Record
(Form
, True);
1541 -- Case of only body present
1543 if Acts_As_Spec
(STJ
.Bod
) then
1544 Add_Form_To_Spec
(Form
, Specification
(STJ
.Bod
));
1546 -- Case of separate spec
1549 Add_Form_To_Spec
(Form
, Parent
(STJ
.Ent
));
1551 end Add_Extra_Formal
;
1554 -- Processing for subprograms that declare an activation record
1556 if Present
(STJ
.ARECn
) then
1558 -- Local declarations for one such subprogram
1561 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1563 Decls
: constant List_Id
:= New_List
;
1564 -- List of new declarations we create
1569 Decl_Assign
: Node_Id
;
1570 -- Assigment to set uplink, Empty if none
1572 Decl_ARECnT
: Node_Id
;
1573 Decl_ARECnPT
: Node_Id
;
1574 Decl_ARECn
: Node_Id
;
1575 Decl_ARECnP
: Node_Id
;
1576 -- Declaration nodes for the AREC entities we build
1579 -- Build list of component declarations for ARECnT
1581 Clist
:= Empty_List
;
1583 -- If we are in a subprogram that has a static link that
1584 -- is passed in (as indicated by ARECnF being defined),
1585 -- then include ARECnU : ARECmPT where ARECmPT comes from
1586 -- the level one higher than the current level, and the
1587 -- entity ARECnPT comes from the enclosing subprogram.
1589 if Present
(STJ
.ARECnF
) then
1592 renames Subps
.Table
(Enclosing_Subp
(J
));
1595 Make_Component_Declaration
(Loc
,
1596 Defining_Identifier
=> STJ
.ARECnU
,
1597 Component_Definition
=>
1598 Make_Component_Definition
(Loc
,
1599 Subtype_Indication
=>
1600 New_Occurrence_Of
(STJE
.ARECnPT
, Loc
))));
1604 -- Add components for uplevel referenced entities
1606 if Present
(STJ
.Uents
) then
1613 -- 1's origin of index in list of elements. This is
1614 -- used to uniquify names if needed in Upref_Name.
1617 Elmt
:= First_Elmt
(STJ
.Uents
);
1619 while Present
(Elmt
) loop
1620 Uent
:= Node
(Elmt
);
1624 Make_Defining_Identifier
(Loc
,
1625 Chars
=> Upref_Name
(Uent
, Indx
, Clist
));
1627 Set_Activation_Record_Component
1630 if Needs_Fat_Pointer
(Uent
) then
1632 -- Build corresponding access type
1635 Build_Access_Type_Decl
1636 (Etype
(Uent
), STJ
.Ent
);
1637 Append_To
(Decls
, Ptr_Decl
);
1639 -- And use its type in the corresponding
1643 Make_Component_Declaration
(Loc
,
1644 Defining_Identifier
=> Comp
,
1645 Component_Definition
=>
1646 Make_Component_Definition
(Loc
,
1647 Subtype_Indication
=>
1649 (Defining_Identifier
(Ptr_Decl
),
1653 Make_Component_Declaration
(Loc
,
1654 Defining_Identifier
=> Comp
,
1655 Component_Definition
=>
1656 Make_Component_Definition
(Loc
,
1657 Subtype_Indication
=>
1658 New_Occurrence_Of
(Addr
, Loc
))));
1665 -- Now we can insert the AREC declarations into the body
1666 -- type ARECnT is record .. end record;
1667 -- pragma Suppress_Initialization (ARECnT);
1669 -- Note that we need to set the Suppress_Initialization
1670 -- flag after Decl_ARECnT has been analyzed.
1673 Make_Full_Type_Declaration
(Loc
,
1674 Defining_Identifier
=> STJ
.ARECnT
,
1676 Make_Record_Definition
(Loc
,
1678 Make_Component_List
(Loc
,
1679 Component_Items
=> Clist
)));
1680 Append_To
(Decls
, Decl_ARECnT
);
1682 -- type ARECnPT is access all ARECnT;
1685 Make_Full_Type_Declaration
(Loc
,
1686 Defining_Identifier
=> STJ
.ARECnPT
,
1688 Make_Access_To_Object_Definition
(Loc
,
1689 All_Present
=> True,
1690 Subtype_Indication
=>
1691 New_Occurrence_Of
(STJ
.ARECnT
, Loc
)));
1692 Append_To
(Decls
, Decl_ARECnPT
);
1694 -- ARECn : aliased ARECnT;
1697 Make_Object_Declaration
(Loc
,
1698 Defining_Identifier
=> STJ
.ARECn
,
1699 Aliased_Present
=> True,
1700 Object_Definition
=>
1701 New_Occurrence_Of
(STJ
.ARECnT
, Loc
));
1702 Append_To
(Decls
, Decl_ARECn
);
1704 -- ARECnP : constant ARECnPT := ARECn'Access;
1707 Make_Object_Declaration
(Loc
,
1708 Defining_Identifier
=> STJ
.ARECnP
,
1709 Constant_Present
=> True,
1710 Object_Definition
=>
1711 New_Occurrence_Of
(STJ
.ARECnPT
, Loc
),
1713 Make_Attribute_Reference
(Loc
,
1715 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1716 Attribute_Name
=> Name_Access
));
1717 Append_To
(Decls
, Decl_ARECnP
);
1719 -- If we are in a subprogram that has a static link that
1720 -- is passed in (as indicated by ARECnF being defined),
1721 -- then generate ARECn.ARECmU := ARECmF where m is
1722 -- one less than the current level to set the uplink.
1724 if Present
(STJ
.ARECnF
) then
1726 Make_Assignment_Statement
(Loc
,
1728 Make_Selected_Component
(Loc
,
1730 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1732 New_Occurrence_Of
(STJ
.ARECnU
, Loc
)),
1734 New_Occurrence_Of
(STJ
.ARECnF
, Loc
));
1735 Append_To
(Decls
, Decl_Assign
);
1738 Decl_Assign
:= Empty
;
1741 Prepend_List_To
(Declarations
(STJ
.Bod
), Decls
);
1743 -- Analyze the newly inserted declarations. Note that we
1744 -- do not need to establish the whole scope stack, since
1745 -- we have already set all entity fields (so there will
1746 -- be no searching of upper scopes to resolve names). But
1747 -- we do set the scope of the current subprogram, so that
1748 -- newly created entities go in the right entity chain.
1750 -- We analyze with all checks suppressed (since we do
1751 -- not expect any exceptions).
1753 Push_Scope
(STJ
.Ent
);
1754 Analyze
(Decl_ARECnT
, Suppress
=> All_Checks
);
1756 -- Note that we need to call Set_Suppress_Initialization
1757 -- after Decl_ARECnT has been analyzed, but before
1758 -- analyzing Decl_ARECnP so that the flag is properly
1759 -- taking into account.
1761 Set_Suppress_Initialization
(STJ
.ARECnT
);
1763 Analyze
(Decl_ARECnPT
, Suppress
=> All_Checks
);
1764 Analyze
(Decl_ARECn
, Suppress
=> All_Checks
);
1765 Analyze
(Decl_ARECnP
, Suppress
=> All_Checks
);
1767 if Present
(Decl_Assign
) then
1768 Analyze
(Decl_Assign
, Suppress
=> All_Checks
);
1773 -- Next step, for each uplevel referenced entity, add
1774 -- assignment operations to set the component in the
1775 -- activation record.
1777 if Present
(STJ
.Uents
) then
1782 Elmt
:= First_Elmt
(STJ
.Uents
);
1783 while Present
(Elmt
) loop
1785 Ent
: constant Entity_Id
:= Node
(Elmt
);
1786 Loc
: constant Source_Ptr
:= Sloc
(Ent
);
1787 Dec
: constant Node_Id
:=
1788 Declaration_Node
(Ent
);
1795 -- For parameters, we insert the assignment
1796 -- right after the declaration of ARECnP.
1797 -- For all other entities, we insert
1798 -- the assignment immediately after the
1799 -- declaration of the entity.
1801 -- Note: we don't need to mark the entity
1802 -- as being aliased, because the address
1803 -- attribute will mark it as Address_Taken,
1804 -- and that is good enough.
1806 if Is_Formal
(Ent
) then
1812 -- Build and insert the assignment:
1813 -- ARECn.nam := nam'Address
1814 -- or else 'Access for unconstrained array
1816 if Needs_Fat_Pointer
(Ent
) then
1817 Attr
:= Name_Access
;
1819 Attr
:= Name_Address
;
1823 Make_Assignment_Statement
(Loc
,
1825 Make_Selected_Component
(Loc
,
1827 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1830 (Activation_Record_Component
1835 Make_Attribute_Reference
(Loc
,
1837 New_Occurrence_Of
(Ent
, Loc
),
1838 Attribute_Name
=> Attr
));
1840 Insert_After
(Ins
, Asn
);
1842 -- Analyze the assignment statement. We do
1843 -- not need to establish the relevant scope
1844 -- stack entries here, because we have
1845 -- already set the correct entity references,
1846 -- so no name resolution is required, and no
1847 -- new entities are created, so we don't even
1848 -- need to set the current scope.
1850 -- We analyze with all checks suppressed
1851 -- (since we do not expect any exceptions).
1853 Analyze
(Asn
, Suppress
=> All_Checks
);
1866 -- Next step, process uplevel references. This has to be done in a
1867 -- separate pass, after completing the processing in Sub_Loop because we
1868 -- need all the AREC declarations generated, inserted, and analyzed so
1869 -- that the uplevel references can be successfully analyzed.
1871 Uplev_Refs
: for J
in Urefs
.First
.. Urefs
.Last
loop
1873 UPJ
: Uref_Entry
renames Urefs
.Table
(J
);
1876 -- Ignore type references, these are implicit references that do
1877 -- not need rewriting (e.g. the appearence in a conversion).
1878 -- Also ignore if no reference was specified.
1880 if Is_Type
(UPJ
.Ent
) or else No
(UPJ
.Ref
) then
1884 -- Also ignore uplevel references to bounds of types that come
1885 -- from the original type reference.
1887 if Is_Entity_Name
(UPJ
.Ref
)
1888 and then Present
(Entity
(UPJ
.Ref
))
1889 and then Is_Type
(Entity
(UPJ
.Ref
))
1894 -- Rewrite one reference
1896 Rewrite_One_Ref
: declare
1897 Loc
: constant Source_Ptr
:= Sloc
(UPJ
.Ref
);
1898 -- Source location for the reference
1900 Typ
: constant Entity_Id
:= Etype
(UPJ
.Ent
);
1901 -- The type of the referenced entity
1903 Atyp
: constant Entity_Id
:= Get_Actual_Subtype
(UPJ
.Ref
);
1904 -- The actual subtype of the reference
1906 RS_Caller
: constant SI_Type
:= Subp_Index
(UPJ
.Caller
);
1907 -- Subp_Index for caller containing reference
1909 STJR
: Subp_Entry
renames Subps
.Table
(RS_Caller
);
1910 -- Subp_Entry for subprogram containing reference
1912 RS_Callee
: constant SI_Type
:= Subp_Index
(UPJ
.Callee
);
1913 -- Subp_Index for subprogram containing referenced entity
1915 STJE
: Subp_Entry
renames Subps
.Table
(RS_Callee
);
1916 -- Subp_Entry for subprogram containing referenced entity
1923 -- Ignore if no ARECnF entity for enclosing subprogram which
1924 -- probably happens as a result of not properly treating
1925 -- instance bodies. To be examined ???
1927 -- If this test is omitted, then the compilation of freeze.adb
1928 -- and inline.adb fail in unnesting mode.
1930 if No
(STJR
.ARECnF
) then
1934 -- Push the current scope, so that the pointer type Tnn, and
1935 -- any subsidiary entities resulting from the analysis of the
1936 -- rewritten reference, go in the right entity chain.
1938 Push_Scope
(STJR
.Ent
);
1940 -- Now we need to rewrite the reference. We have a reference
1941 -- from level STJR.Lev to level STJE.Lev. The general form of
1942 -- the rewritten reference for entity X is:
1944 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
1946 -- where a,b,c,d .. m =
1947 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
1949 pragma Assert
(STJR
.Lev
> STJE
.Lev
);
1951 -- Compute the prefix of X. Here are examples to make things
1952 -- clear (with parens to show groupings, the prefix is
1953 -- everything except the .X at the end).
1955 -- level 2 to level 1
1959 -- level 3 to level 1
1961 -- (AREC2F.AREC1U).X
1963 -- level 4 to level 1
1965 -- ((AREC3F.AREC2U).AREC1U).X
1967 -- level 6 to level 2
1969 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
1971 -- In the above, ARECnF and ARECnU are pointers, so there are
1972 -- explicit dereferences required for these occurrences.
1975 Make_Explicit_Dereference
(Loc
,
1976 Prefix
=> New_Occurrence_Of
(STJR
.ARECnF
, Loc
));
1978 for L
in STJE
.Lev
.. STJR
.Lev
- 2 loop
1979 SI
:= Enclosing_Subp
(SI
);
1981 Make_Explicit_Dereference
(Loc
,
1983 Make_Selected_Component
(Loc
,
1986 New_Occurrence_Of
(Subps
.Table
(SI
).ARECnU
, Loc
)));
1989 -- Get activation record component (must exist)
1991 Comp
:= Activation_Record_Component
(UPJ
.Ent
);
1992 pragma Assert
(Present
(Comp
));
1994 -- Do the replacement. If the component type is an access type,
1995 -- this is an uplevel reference for an entity that requires a
1996 -- fat pointer, so dereference the component.
1998 if Is_Access_Type
(Etype
(Comp
)) then
2000 Make_Explicit_Dereference
(Loc
,
2002 Make_Selected_Component
(Loc
,
2005 New_Occurrence_Of
(Comp
, Loc
))));
2009 Make_Attribute_Reference
(Loc
,
2010 Prefix
=> New_Occurrence_Of
(Atyp
, Loc
),
2011 Attribute_Name
=> Name_Deref
,
2012 Expressions
=> New_List
(
2013 Make_Selected_Component
(Loc
,
2016 New_Occurrence_Of
(Comp
, Loc
)))));
2019 -- Analyze and resolve the new expression. We do not need to
2020 -- establish the relevant scope stack entries here, because we
2021 -- have already set all the correct entity references, so no
2022 -- name resolution is needed. We have already set the current
2023 -- scope, so that any new entities created will be in the right
2026 -- We analyze with all checks suppressed (since we do not
2027 -- expect any exceptions)
2029 Analyze_And_Resolve
(UPJ
.Ref
, Typ
, Suppress
=> All_Checks
);
2031 end Rewrite_One_Ref
;
2036 end loop Uplev_Refs
;
2038 -- Finally, loop through all calls adding extra actual for the
2039 -- activation record where it is required.
2041 Adjust_Calls
: for J
in Calls
.First
.. Calls
.Last
loop
2043 -- Process a single call, we are only interested in a call to a
2044 -- subprogram that actually needs a pointer to an activation record,
2045 -- as indicated by the ARECnF entity being set. This excludes the
2046 -- top level subprogram, and any subprogram not having uplevel refs.
2048 Adjust_One_Call
: declare
2049 CTJ
: Call_Entry
renames Calls
.Table
(J
);
2050 STF
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Caller
));
2051 STT
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Callee
));
2053 Loc
: constant Source_Ptr
:= Sloc
(CTJ
.N
);
2061 if Present
(STT
.ARECnF
)
2062 and then Nkind
(CTJ
.N
) in N_Subprogram_Call
2064 -- CTJ.N is a call to a subprogram which may require a pointer
2065 -- to an activation record. The subprogram containing the call
2066 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2067 -- have a call from level STF.Lev to level STT.Lev.
2069 -- There are three possibilities:
2071 -- For a call to the same level, we just pass the activation
2072 -- record passed to the calling subprogram.
2074 if STF
.Lev
= STT
.Lev
then
2075 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
2077 -- For a call that goes down a level, we pass a pointer to the
2078 -- activation record constructed within the caller (which may
2079 -- be the outer-level subprogram, but also may be a more deeply
2082 elsif STT
.Lev
= STF
.Lev
+ 1 then
2083 Extra
:= New_Occurrence_Of
(STF
.ARECnP
, Loc
);
2085 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2086 -- since it is not possible to do a downcall of more than
2089 -- For a call from level STF.Lev to level STT.Lev, we
2090 -- have to find the activation record needed by the
2091 -- callee. This is as follows:
2093 -- ARECaF.ARECbU.ARECcU....ARECmU
2095 -- where a,b,c .. m =
2096 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2099 pragma Assert
(STT
.Lev
< STF
.Lev
);
2101 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
2102 SubX
:= Subp_Index
(CTJ
.Caller
);
2103 for K
in reverse STT
.Lev
.. STF
.Lev
- 1 loop
2104 SubX
:= Enclosing_Subp
(SubX
);
2106 Make_Selected_Component
(Loc
,
2110 (Subps
.Table
(SubX
).ARECnU
, Loc
));
2114 -- Extra is the additional parameter to be added. Build a
2115 -- parameter association that we can append to the actuals.
2118 Make_Parameter_Association
(Loc
,
2120 New_Occurrence_Of
(STT
.ARECnF
, Loc
),
2121 Explicit_Actual_Parameter
=> Extra
);
2123 if No
(Parameter_Associations
(CTJ
.N
)) then
2124 Set_Parameter_Associations
(CTJ
.N
, Empty_List
);
2127 Append
(ExtraP
, Parameter_Associations
(CTJ
.N
));
2129 -- We need to deal with the actual parameter chain as well. The
2130 -- newly added parameter is always the last actual.
2132 Act
:= First_Named_Actual
(CTJ
.N
);
2135 Set_First_Named_Actual
(CTJ
.N
, Extra
);
2137 -- If call has been relocated (as with an expression in
2138 -- an aggregate), set First_Named pointer in original node
2139 -- as well, because that's the parent of the parameter list.
2141 Set_First_Named_Actual
2142 (Parent
(List_Containing
(ExtraP
)), Extra
);
2144 -- Here we must follow the chain and append the new entry
2153 PAN
:= Parent
(Act
);
2154 pragma Assert
(Nkind
(PAN
) = N_Parameter_Association
);
2155 NNA
:= Next_Named_Actual
(PAN
);
2158 Set_Next_Named_Actual
(PAN
, Extra
);
2167 -- Analyze and resolve the new actual. We do not need to
2168 -- establish the relevant scope stack entries here, because
2169 -- we have already set all the correct entity references, so
2170 -- no name resolution is needed.
2172 -- We analyze with all checks suppressed (since we do not
2173 -- expect any exceptions, and also we temporarily turn off
2174 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2175 -- references (not needed at this stage, and in fact causes
2176 -- a bit of recursive chaos).
2178 Opt
.Unnest_Subprogram_Mode
:= False;
2180 (Extra
, Etype
(STT
.ARECnF
), Suppress
=> All_Checks
);
2181 Opt
.Unnest_Subprogram_Mode
:= True;
2183 end Adjust_One_Call
;
2184 end loop Adjust_Calls
;
2187 end Unnest_Subprogram
;
2189 ------------------------
2190 -- Unnest_Subprograms --
2191 ------------------------
2193 procedure Unnest_Subprograms
(N
: Node_Id
) is
2194 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
;
2195 -- Tree visitor that search for outer level procedures with nested
2196 -- subprograms and invokes Unnest_Subprogram()
2202 procedure Do_Search
is new Traverse_Proc
(Search_Subprograms
);
2203 -- Subtree visitor instantiation
2205 ------------------------
2206 -- Search_Subprograms --
2207 ------------------------
2209 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
is
2211 if Nkind_In
(N
, N_Subprogram_Body
, N_Subprogram_Body_Stub
) then
2213 Spec_Id
: constant Entity_Id
:= Unique_Defining_Entity
(N
);
2216 -- We are only interested in subprograms (not generic
2217 -- subprograms), that have nested subprograms.
2219 if Is_Subprogram
(Spec_Id
)
2220 and then Has_Nested_Subprogram
(Spec_Id
)
2221 and then Is_Library_Level_Entity
(Spec_Id
)
2223 Unnest_Subprogram
(Spec_Id
, N
);
2228 -- The proper body of a stub may contain nested subprograms, and
2229 -- therefore must be visited explicitly. Nested stubs are examined
2230 -- recursively in Visit_Node.
2232 if Nkind
(N
) in N_Body_Stub
then
2233 Do_Search
(Library_Unit
(N
));
2237 end Search_Subprograms
;
2239 -- Start of processing for Unnest_Subprograms
2242 if not Opt
.Unnest_Subprogram_Mode
then
2246 -- A specification will contain bodies if it contains instantiations so
2247 -- examine package or subprogram declaration of the main unit, when it
2250 if Nkind
(Unit
(N
)) = N_Package_Body
2251 or else (Nkind
(Unit
(N
)) = N_Subprogram_Body
2252 and then not Acts_As_Spec
(N
))
2254 Do_Search
(Library_Unit
(N
));
2258 end Unnest_Subprograms
;