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
231 elsif Is_Private_Type
(S
)
232 and then Present
(Full_View
(S
))
233 and then Is_Concurrent_Type
(Full_View
(S
))
242 end In_Synchronized_Unit
;
244 -----------------------
245 -- Needs_Fat_Pointer --
246 -----------------------
248 function Needs_Fat_Pointer
(E
: Entity_Id
) return Boolean is
251 and then Is_Array_Type
(Etype
(E
))
252 and then not Is_Constrained
(Etype
(E
));
253 end Needs_Fat_Pointer
;
259 function Subp_Index
(Sub
: Entity_Id
) return SI_Type
is
260 E
: Entity_Id
:= Sub
;
263 pragma Assert
(Is_Subprogram
(E
));
265 if Subps_Index
(E
) = Uint_0
then
266 E
:= Ultimate_Alias
(E
);
268 -- The body of a protected operation has a different name and
269 -- has been scanned at this point, and thus has an entry in the
272 if E
= Sub
and then Convention
(E
) = Convention_Protected
then
273 E
:= Protected_Body_Subprogram
(E
);
276 if Ekind
(E
) = E_Function
277 and then Rewritten_For_C
(E
)
278 and then Present
(Corresponding_Procedure
(E
))
280 E
:= Corresponding_Procedure
(E
);
284 pragma Assert
(Subps_Index
(E
) /= Uint_0
);
285 return SI_Type
(UI_To_Int
(Subps_Index
(E
)));
288 -----------------------
289 -- Unnest_Subprogram --
290 -----------------------
292 procedure Unnest_Subprogram
(Subp
: Entity_Id
; Subp_Body
: Node_Id
) is
293 function AREC_Name
(J
: Pos
; S
: String) return Name_Id
;
294 -- Returns name for string ARECjS, where j is the decimal value of j
296 function Enclosing_Subp
(Subp
: SI_Type
) return SI_Type
;
297 -- Subp is the index of a subprogram which has a Lev greater than 1.
298 -- This function returns the index of the enclosing subprogram which
299 -- will have a Lev value one less than this.
301 function Img_Pos
(N
: Pos
) return String;
302 -- Return image of N without leading blank
307 Clist
: List_Id
) return Name_Id
;
308 -- This function returns the name to be used in the activation record to
309 -- reference the variable uplevel. Clist is the list of components that
310 -- have been created in the activation record so far. Normally the name
311 -- is just a copy of the Chars field of the entity. The exception is
312 -- when the name has already been used, in which case we suffix the name
313 -- with the index value Index to avoid duplication. This happens with
314 -- declare blocks and generic parameters at least.
320 function AREC_Name
(J
: Pos
; S
: String) return Name_Id
is
322 return Name_Find
("AREC" & Img_Pos
(J
) & S
);
329 function Enclosing_Subp
(Subp
: SI_Type
) return SI_Type
is
330 STJ
: Subp_Entry
renames Subps
.Table
(Subp
);
331 Ret
: constant SI_Type
:= Subp_Index
(Enclosing_Subprogram
(STJ
.Ent
));
333 pragma Assert
(STJ
.Lev
> 1);
334 pragma Assert
(Subps
.Table
(Ret
).Lev
= STJ
.Lev
- 1);
342 function Img_Pos
(N
: Pos
) return String is
343 Buf
: String (1 .. 20);
351 Buf
(Ptr
) := Character'Val (48 + NV
mod 10);
356 return Buf
(Ptr
+ 1 .. Buf
'Last);
366 Clist
: List_Id
) return Name_Id
375 elsif Chars
(Defining_Identifier
(C
)) = Chars
(Ent
) then
377 Name_Find
(Get_Name_String
(Chars
(Ent
)) & Img_Pos
(Index
));
384 -- Start of processing for Unnest_Subprogram
387 -- Nothing to do inside a generic (all processing is for instance)
389 if Inside_A_Generic
then
393 -- If the main unit is a package body then we need to examine the spec
394 -- to determine whether the main unit is generic (the scope stack is not
395 -- present when this is called on the main unit).
397 if Ekind
(Cunit_Entity
(Main_Unit
)) = E_Package_Body
398 and then Is_Generic_Unit
(Spec_Entity
(Cunit_Entity
(Main_Unit
)))
403 -- Only unnest when generating code for the main source unit
405 if not In_Extended_Main_Code_Unit
(Subp_Body
) then
409 -- This routine is called late, after the scope stack is gone. The
410 -- following creates a suitable dummy scope stack to be used for the
411 -- analyze/expand calls made from this routine.
415 -- First step, we must mark all nested subprograms that require a static
416 -- link (activation record) because either they contain explicit uplevel
417 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
418 -- this point), or they make calls to other subprograms in the same nest
419 -- that require a static link (in which case we set this flag).
421 -- This is a recursive definition, and to implement this, we have to
422 -- build a call graph for the set of nested subprograms, and then go
423 -- over this graph to implement recursively the invariant that if a
424 -- subprogram has a call to a subprogram requiring a static link, then
425 -- the calling subprogram requires a static link.
427 -- First populate the above tables
429 Subps_First
:= Subps
.Last
+ 1;
433 Build_Tables
: declare
434 Current_Subprogram
: Entity_Id
;
435 -- When we scan a subprogram body, we set Current_Subprogram to the
436 -- corresponding entity. This gets recursively saved and restored.
438 function Visit_Node
(N
: Node_Id
) return Traverse_Result
;
439 -- Visit a single node in Subp
445 procedure Visit
is new Traverse_Proc
(Visit_Node
);
446 -- Used to traverse the body of Subp, populating the tables
452 function Visit_Node
(N
: Node_Id
) return Traverse_Result
is
457 procedure Check_Static_Type
458 (T
: Entity_Id
; N
: Node_Id
; DT
: in out Boolean);
459 -- Given a type T, checks if it is a static type defined as a type
460 -- with no dynamic bounds in sight. If so, the only action is to
461 -- set Is_Static_Type True for T. If T is not a static type, then
462 -- all types with dynamic bounds associated with T are detected,
463 -- and their bounds are marked as uplevel referenced if not at the
464 -- library level, and DT is set True. If N is specified, it's the
465 -- node that will need to be replaced. If not specified, it means
466 -- we can't do a replacement because the bound is implicit.
468 procedure Note_Uplevel_Ref
473 -- Called when we detect an explicit or implicit uplevel reference
474 -- from within Caller to entity E declared in Callee. E can be a
475 -- an object or a type.
477 procedure Register_Subprogram
(E
: Entity_Id
; Bod
: Node_Id
);
478 -- Enter a subprogram whose body is visible or which is a
479 -- subprogram instance into the subprogram table.
481 -----------------------
482 -- Check_Static_Type --
483 -----------------------
485 procedure Check_Static_Type
486 (T
: Entity_Id
; N
: Node_Id
; DT
: in out Boolean)
488 procedure Note_Uplevel_Bound
(N
: Node_Id
; Ref
: Node_Id
);
489 -- N is the bound of a dynamic type. This procedure notes that
490 -- this bound is uplevel referenced, it can handle references
491 -- to entities (typically _FIRST and _LAST entities), and also
492 -- attribute references of the form T'name (name is typically
493 -- FIRST or LAST) where T is the uplevel referenced bound.
494 -- Ref, if Present, is the location of the reference to
497 ------------------------
498 -- Note_Uplevel_Bound --
499 ------------------------
501 procedure Note_Uplevel_Bound
(N
: Node_Id
; Ref
: Node_Id
) is
503 -- Entity name case. Make sure that the entity is declared
504 -- in a subprogram. This may not be the case for for a type
505 -- in a loop appearing in a precondition.
506 -- Exclude explicitly discriminants (that can appear
507 -- in bounds of discriminated components).
509 if Is_Entity_Name
(N
) then
510 if Present
(Entity
(N
))
511 and then not Is_Type
(Entity
(N
))
512 and then Present
(Enclosing_Subprogram
(Entity
(N
)))
513 and then Ekind
(Entity
(N
)) /= E_Discriminant
518 Caller
=> Current_Subprogram
,
519 Callee
=> Enclosing_Subprogram
(Entity
(N
)));
522 -- Attribute or indexed component case
524 elsif Nkind_In
(N
, N_Attribute_Reference
,
527 Note_Uplevel_Bound
(Prefix
(N
), Ref
);
529 -- The indices of the indexed components, or the
530 -- associated expressions of an attribute reference,
531 -- may also involve uplevel references.
537 Expr
:= First
(Expressions
(N
));
538 while Present
(Expr
) loop
539 Note_Uplevel_Bound
(Expr
, Ref
);
544 -- The type of the prefix may be have an uplevel
545 -- reference if this needs bounds.
547 if Nkind
(N
) = N_Attribute_Reference
then
549 Attr
: constant Attribute_Id
:=
550 Get_Attribute_Id
(Attribute_Name
(N
));
551 DT
: Boolean := False;
554 if (Attr
= Attribute_First
555 or else Attr
= Attribute_Last
556 or else Attr
= Attribute_Length
)
557 and then Is_Constrained
(Etype
(Prefix
(N
)))
560 (Etype
(Prefix
(N
)), Empty
, DT
);
565 -- Binary operator cases. These can apply to arrays for
566 -- which we may need bounds.
568 elsif Nkind
(N
) in N_Binary_Op
then
569 Note_Uplevel_Bound
(Left_Opnd
(N
), Ref
);
570 Note_Uplevel_Bound
(Right_Opnd
(N
), Ref
);
572 -- Unary operator case
574 elsif Nkind
(N
) in N_Unary_Op
then
575 Note_Uplevel_Bound
(Right_Opnd
(N
), Ref
);
577 -- Explicit dereference and selected component case
579 elsif Nkind_In
(N
, N_Explicit_Dereference
,
580 N_Selected_Component
)
582 Note_Uplevel_Bound
(Prefix
(N
), Ref
);
586 elsif Nkind
(N
) = N_Type_Conversion
then
587 Note_Uplevel_Bound
(Expression
(N
), Ref
);
589 end Note_Uplevel_Bound
;
591 -- Start of processing for Check_Static_Type
594 -- If already marked static, immediate return
596 if Is_Static_Type
(T
) then
600 -- If the type is at library level, always consider it static,
601 -- since such uplevel references are irrelevant.
603 if Is_Library_Level_Entity
(T
) then
604 Set_Is_Static_Type
(T
);
608 -- Otherwise figure out what the story is with this type
610 -- For a scalar type, check bounds
612 if Is_Scalar_Type
(T
) then
614 -- If both bounds static, then this is a static type
617 LB
: constant Node_Id
:= Type_Low_Bound
(T
);
618 UB
: constant Node_Id
:= Type_High_Bound
(T
);
621 if not Is_Static_Expression
(LB
) then
622 Note_Uplevel_Bound
(LB
, N
);
626 if not Is_Static_Expression
(UB
) then
627 Note_Uplevel_Bound
(UB
, N
);
632 -- For record type, check all components and discriminant
633 -- constraints if present.
635 elsif Is_Record_Type
(T
) then
641 C
:= First_Component_Or_Discriminant
(T
);
642 while Present
(C
) loop
643 Check_Static_Type
(Etype
(C
), N
, DT
);
644 Next_Component_Or_Discriminant
(C
);
647 if Has_Discriminants
(T
)
648 and then Present
(Discriminant_Constraint
(T
))
650 D
:= First_Elmt
(Discriminant_Constraint
(T
));
651 while Present
(D
) loop
652 if not Is_Static_Expression
(Node
(D
)) then
653 Note_Uplevel_Bound
(Node
(D
), N
);
662 -- For array type, check index types and component type
664 elsif Is_Array_Type
(T
) then
668 Check_Static_Type
(Component_Type
(T
), N
, DT
);
670 IX
:= First_Index
(T
);
671 while Present
(IX
) loop
672 Check_Static_Type
(Etype
(IX
), N
, DT
);
677 -- For private type, examine whether full view is static
679 elsif Is_Private_Type
(T
) and then Present
(Full_View
(T
)) then
680 Check_Static_Type
(Full_View
(T
), N
, DT
);
682 if Is_Static_Type
(Full_View
(T
)) then
683 Set_Is_Static_Type
(T
);
686 -- For now, ignore other types
693 Set_Is_Static_Type
(T
);
695 end Check_Static_Type
;
697 ----------------------
698 -- Note_Uplevel_Ref --
699 ----------------------
701 procedure Note_Uplevel_Ref
707 Full_E
: Entity_Id
:= E
;
709 -- Nothing to do for static type
711 if Is_Static_Type
(E
) then
715 -- Nothing to do if Caller and Callee are the same
717 if Caller
= Callee
then
720 -- Callee may be a function that returns an array, and that has
721 -- been rewritten as a procedure. If caller is that procedure,
722 -- nothing to do either.
724 elsif Ekind
(Callee
) = E_Function
725 and then Rewritten_For_C
(Callee
)
726 and then Corresponding_Procedure
(Callee
) = Caller
730 elsif Ekind_In
(Callee
, E_Entry
, E_Entry_Family
) then
734 -- We have a new uplevel referenced entity
736 if Ekind
(E
) = E_Constant
and then Present
(Full_View
(E
)) then
737 Full_E
:= Full_View
(E
);
740 -- All we do at this stage is to add the uplevel reference to
741 -- the table. It's too early to do anything else, since this
742 -- uplevel reference may come from an unreachable subprogram
743 -- in which case the entry will be deleted.
745 Urefs
.Append
((N
, Full_E
, Caller
, Callee
));
746 end Note_Uplevel_Ref
;
748 -------------------------
749 -- Register_Subprogram --
750 -------------------------
752 procedure Register_Subprogram
(E
: Entity_Id
; Bod
: Node_Id
) is
753 L
: constant Nat
:= Get_Level
(Subp
, E
);
756 -- Subprograms declared in tasks and protected types cannot be
757 -- eliminated because calls to them may be in other units, so
758 -- they must be treated as reachable.
764 Reachable
=> In_Synchronized_Unit
(E
),
766 Declares_AREC
=> False,
776 Set_Subps_Index
(E
, UI_From_Int
(Subps
.Last
));
778 -- If we marked this reachable because it's in a synchronized
779 -- unit, we have to mark all enclosing subprograms as reachable
782 if In_Synchronized_Unit
(E
) then
787 for J
in reverse 1 .. L
- 1 loop
788 S
:= Enclosing_Subprogram
(S
);
789 Subps
.Table
(Subp_Index
(S
)).Reachable
:= True;
793 end Register_Subprogram
;
795 -- Start of processing for Visit_Node
800 -- Record a subprogram call
803 | N_Procedure_Call_Statement
805 -- We are only interested in direct calls, not indirect
806 -- calls (where Name (N) is an explicit dereference) at
809 if Nkind
(Name
(N
)) in N_Has_Entity
then
810 Ent
:= Entity
(Name
(N
));
812 -- We are only interested in calls to subprograms nested
813 -- within Subp. Calls to Subp itself or to subprograms
814 -- outside the nested structure do not affect us.
816 if Scope_Within
(Ent
, Subp
)
817 and then Is_Subprogram
(Ent
)
818 and then not Is_Imported
(Ent
)
820 Append_Unique_Call
((N
, Current_Subprogram
, Ent
));
824 -- For all calls where the formal is an unconstrained array
825 -- and the actual is constrained we need to check the bounds
826 -- for uplevel references.
830 DT
: Boolean := False;
835 if Nkind
(Name
(N
)) = N_Explicit_Dereference
then
836 Subp
:= Etype
(Name
(N
));
838 Subp
:= Entity
(Name
(N
));
841 Actual
:= First_Actual
(N
);
842 Formal
:= First_Formal_With_Extras
(Subp
);
843 while Present
(Actual
) loop
844 if Is_Array_Type
(Etype
(Formal
))
845 and then not Is_Constrained
(Etype
(Formal
))
846 and then Is_Constrained
(Etype
(Actual
))
848 Check_Static_Type
(Etype
(Actual
), Empty
, DT
);
851 Next_Actual
(Actual
);
852 Next_Formal_With_Extras
(Formal
);
856 -- An At_End_Proc in a statement sequence indicates that there
857 -- is a call from the enclosing construct or block to that
858 -- subprogram. As above, the called entity must be local and
861 when N_Handled_Sequence_Of_Statements
=>
862 if Present
(At_End_Proc
(N
))
863 and then Scope_Within
(Entity
(At_End_Proc
(N
)), Subp
)
864 and then not Is_Imported
(Entity
(At_End_Proc
(N
)))
867 ((N
, Current_Subprogram
, Entity
(At_End_Proc
(N
))));
870 -- Similarly, the following constructs include a semantic
871 -- attribute Procedure_To_Call that must be handled like
872 -- other calls. Likewise for attribute Storage_Pool.
875 | N_Extended_Return_Statement
877 | N_Simple_Return_Statement
880 Pool
: constant Entity_Id
:= Storage_Pool
(N
);
881 Proc
: constant Entity_Id
:= Procedure_To_Call
(N
);
885 and then Scope_Within
(Proc
, Subp
)
886 and then not Is_Imported
(Proc
)
888 Append_Unique_Call
((N
, Current_Subprogram
, Proc
));
892 and then not Is_Library_Level_Entity
(Pool
)
893 and then Scope_Within_Or_Same
(Scope
(Pool
), Subp
)
895 Caller
:= Current_Subprogram
;
896 Callee
:= Enclosing_Subprogram
(Pool
);
898 if Callee
/= Caller
then
899 Note_Uplevel_Ref
(Pool
, Empty
, Caller
, Callee
);
904 -- For an allocator with a qualified expression, check type
905 -- of expression being qualified. The explicit type name is
906 -- handled as an entity reference.
908 if Nkind
(N
) = N_Allocator
909 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
912 DT
: Boolean := False;
915 (Etype
(Expression
(Expression
(N
))), Empty
, DT
);
918 -- For a Return or Free (all other nodes we handle here),
919 -- we usually need the size of the object, so we need to be
920 -- sure that any nonstatic bounds of the expression's type
921 -- that are uplevel are handled.
923 elsif Nkind
(N
) /= N_Allocator
924 and then Present
(Expression
(N
))
927 DT
: Boolean := False;
929 Check_Static_Type
(Etype
(Expression
(N
)), Empty
, DT
);
933 -- A 'Access reference is a (potential) call. So is 'Address,
934 -- in particular on imported subprograms. Other attributes
935 -- require special handling.
937 when N_Attribute_Reference
=>
939 Attr
: constant Attribute_Id
:=
940 Get_Attribute_Id
(Attribute_Name
(N
));
943 when Attribute_Access
944 | Attribute_Unchecked_Access
945 | Attribute_Unrestricted_Access
948 if Nkind
(Prefix
(N
)) in N_Has_Entity
then
949 Ent
:= Entity
(Prefix
(N
));
951 -- We only need to examine calls to subprograms
952 -- nested within current Subp.
954 if Scope_Within
(Ent
, Subp
) then
955 if Is_Imported
(Ent
) then
958 elsif Is_Subprogram
(Ent
) then
960 ((N
, Current_Subprogram
, Ent
));
965 -- References to bounds can be uplevel references if
966 -- the type isn't static.
972 -- Special-case attributes of objects whose bounds
973 -- may be uplevel references. More complex prefixes
974 -- handled during full traversal. Note that if the
975 -- nominal subtype of the prefix is unconstrained,
976 -- the bound must be obtained from the object, not
977 -- from the (possibly) uplevel reference.
979 if Is_Constrained
(Etype
(Prefix
(N
))) then
981 DT
: Boolean := False;
984 (Etype
(Prefix
(N
)), Empty
, DT
);
995 -- Component associations in aggregates are either static or
996 -- else the aggregate will be expanded into assignments, in
997 -- which case the expression is analyzed later and provides
998 -- no relevant code generation.
1000 when N_Component_Association
=>
1001 if No
(Expression
(N
))
1002 or else No
(Etype
(Expression
(N
)))
1007 -- Generic associations are not analyzed: the actuals are
1008 -- transferred to renaming and subtype declarations that
1009 -- are the ones that must be examined.
1011 when N_Generic_Association
=>
1014 -- Indexed references can be uplevel if the type isn't static
1015 -- and if the lower bound (or an inner bound for a multi-
1016 -- dimensional array) is uplevel.
1018 when N_Indexed_Component
1021 if Is_Constrained
(Etype
(Prefix
(N
))) then
1023 DT
: Boolean := False;
1025 Check_Static_Type
(Etype
(Prefix
(N
)), Empty
, DT
);
1029 -- A selected component can have an implicit up-level
1030 -- reference due to the bounds of previous fields in the
1031 -- record. We simplify the processing here by examining
1032 -- all components of the record.
1034 -- Selected components appear as unit names and end labels
1035 -- for child units. Prefixes of these nodes denote parent
1036 -- units and carry no type information so they are skipped.
1038 when N_Selected_Component
=>
1039 if Present
(Etype
(Prefix
(N
))) then
1041 DT
: Boolean := False;
1043 Check_Static_Type
(Etype
(Prefix
(N
)), Empty
, DT
);
1047 -- For EQ/NE comparisons, we need the type of the operands
1048 -- in order to do the comparison, which means we need the
1055 DT
: Boolean := False;
1057 Check_Static_Type
(Etype
(Left_Opnd
(N
)), Empty
, DT
);
1058 Check_Static_Type
(Etype
(Right_Opnd
(N
)), Empty
, DT
);
1061 -- Likewise we need the sizes to compute how much to move in
1064 when N_Assignment_Statement
=>
1066 DT
: Boolean := False;
1068 Check_Static_Type
(Etype
(Name
(N
)), Empty
, DT
);
1069 Check_Static_Type
(Etype
(Expression
(N
)), Empty
, DT
);
1072 -- Record a subprogram. We record a subprogram body that acts
1073 -- as a spec. Otherwise we record a subprogram declaration,
1074 -- providing that it has a corresponding body we can get hold
1075 -- of. The case of no corresponding body being available is
1078 when N_Subprogram_Body
=>
1079 Ent
:= Unique_Defining_Entity
(N
);
1081 -- Ignore generic subprogram
1083 if Is_Generic_Subprogram
(Ent
) then
1087 -- Make new entry in subprogram table if not already made
1089 Register_Subprogram
(Ent
, N
);
1091 -- We make a recursive call to scan the subprogram body, so
1092 -- that we can save and restore Current_Subprogram.
1095 Save_CS
: constant Entity_Id
:= Current_Subprogram
;
1099 Current_Subprogram
:= Ent
;
1101 -- Scan declarations
1103 Decl
:= First
(Declarations
(N
));
1104 while Present
(Decl
) loop
1111 Visit
(Handled_Statement_Sequence
(N
));
1113 -- Restore current subprogram setting
1115 Current_Subprogram
:= Save_CS
;
1118 -- Now at this level, return skipping the subprogram body
1119 -- descendants, since we already took care of them!
1123 -- If we have a body stub, visit the associated subunit, which
1124 -- is a semantic descendant of the stub.
1127 Visit
(Library_Unit
(N
));
1129 -- A declaration of a wrapper package indicates a subprogram
1130 -- instance for which there is no explicit body. Enter the
1131 -- subprogram instance in the table.
1133 when N_Package_Declaration
=>
1134 if Is_Wrapper_Package
(Defining_Entity
(N
)) then
1136 (Related_Instance
(Defining_Entity
(N
)), Empty
);
1139 -- Skip generic declarations
1141 when N_Generic_Declaration
=>
1144 -- Skip generic package body
1146 when N_Package_Body
=>
1147 if Present
(Corresponding_Spec
(N
))
1148 and then Ekind
(Corresponding_Spec
(N
)) = E_Generic_Package
1153 -- Pragmas and component declarations are ignored. Quantified
1154 -- expressions are expanded into explicit loops and the
1155 -- original epression must be ignored.
1157 when N_Component_Declaration
1159 | N_Quantified_Expression
1163 -- We want to skip the function spec for a generic function
1164 -- to avoid looking at any generic types that might be in
1167 when N_Function_Specification
=>
1168 if Is_Generic_Subprogram
(Unique_Defining_Entity
(N
)) then
1172 -- Otherwise record an uplevel reference in a local identifier
1175 if Nkind
(N
) in N_Has_Entity
1176 and then Present
(Entity
(N
))
1180 -- Only interested in entities declared within our nest
1182 if not Is_Library_Level_Entity
(Ent
)
1183 and then Scope_Within_Or_Same
(Scope
(Ent
), Subp
)
1185 -- Skip entities defined in inlined subprograms
1188 Chars
(Enclosing_Subprogram
(Ent
)) /= Name_uParent
1190 -- Constants and variables are potentially uplevel
1191 -- references to global declarations.
1194 (Ekind_In
(Ent
, E_Constant
,
1198 -- Formals are interesting, but not if being used
1199 -- as mere names of parameters for name notation
1205 (Nkind
(Parent
(N
)) = N_Parameter_Association
1206 and then Selector_Name
(Parent
(N
)) = N
))
1208 -- Types other than known Is_Static types are
1209 -- potentially interesting.
1212 (Is_Type
(Ent
) and then not Is_Static_Type
(Ent
)))
1214 -- Here we have a potentially interesting uplevel
1215 -- reference to examine.
1217 if Is_Type
(Ent
) then
1219 DT
: Boolean := False;
1222 Check_Static_Type
(Ent
, N
, DT
);
1227 Caller
:= Current_Subprogram
;
1228 Callee
:= Enclosing_Subprogram
(Ent
);
1231 and then (not Is_Static_Type
(Ent
)
1232 or else Needs_Fat_Pointer
(Ent
))
1234 Note_Uplevel_Ref
(Ent
, N
, Caller
, Callee
);
1236 -- Check the type of a formal parameter of the current
1237 -- subprogram, whose formal type may be an uplevel
1240 elsif Is_Formal
(Ent
)
1241 and then Scope
(Ent
) = Current_Subprogram
1244 DT
: Boolean := False;
1247 Check_Static_Type
(Etype
(Ent
), Empty
, DT
);
1254 -- Fall through to continue scanning children of this node
1259 -- Start of processing for Build_Tables
1262 -- Traverse the body to get subprograms, calls and uplevel references
1267 -- Now do the first transitive closure which determines which
1268 -- subprograms in the nest are actually reachable.
1270 Reachable_Closure
: declare
1274 Subps
.Table
(Subps_First
).Reachable
:= True;
1276 -- We use a simple minded algorithm as follows (obviously this can
1277 -- be done more efficiently, using one of the standard algorithms
1278 -- for efficient transitive closure computation, but this is simple
1279 -- and most likely fast enough that its speed does not matter).
1281 -- Repeatedly scan the list of calls. Any time we find a call from
1282 -- A to B, where A is reachable, but B is not, then B is reachable,
1283 -- and note that we have made a change by setting Modified True. We
1284 -- repeat this until we make a pass with no modifications.
1288 Inner
: for J
in Calls
.First
.. Calls
.Last
loop
1290 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1292 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1293 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1295 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1296 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1299 if SUBF
.Reachable
and then not SUBT
.Reachable
then
1300 SUBT
.Reachable
:= True;
1306 exit Outer
when not Modified
;
1308 end Reachable_Closure
;
1310 -- Remove calls from unreachable subprograms
1317 for J
in Calls
.First
.. Calls
.Last
loop
1319 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1321 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1322 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1324 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1325 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1328 if SUBF
.Reachable
then
1329 pragma Assert
(SUBT
.Reachable
);
1330 New_Index
:= New_Index
+ 1;
1331 Calls
.Table
(New_Index
) := Calls
.Table
(J
);
1336 Calls
.Set_Last
(New_Index
);
1339 -- Remove uplevel references from unreachable subprograms
1346 for J
in Urefs
.First
.. Urefs
.Last
loop
1348 URJ
: Uref_Entry
renames Urefs
.Table
(J
);
1350 SINF
: constant SI_Type
:= Subp_Index
(URJ
.Caller
);
1351 SINT
: constant SI_Type
:= Subp_Index
(URJ
.Callee
);
1353 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1354 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1359 -- Keep reachable reference
1361 if SUBF
.Reachable
then
1362 New_Index
:= New_Index
+ 1;
1363 Urefs
.Table
(New_Index
) := Urefs
.Table
(J
);
1365 -- And since we know we are keeping this one, this is a good
1366 -- place to fill in information for a good reference.
1368 -- Mark all enclosing subprograms need to declare AREC
1372 S
:= Enclosing_Subprogram
(S
);
1374 -- If we are at the top level, as can happen with
1375 -- references to formals in aspects of nested subprogram
1376 -- declarations, there are no further subprograms to mark
1377 -- as requiring activation records.
1382 SUBI
: Subp_Entry
renames Subps
.Table
(Subp_Index
(S
));
1384 SUBI
.Declares_AREC
:= True;
1386 -- If this entity was marked reachable because it is
1387 -- in a task or protected type, there may not appear
1388 -- to be any calls to it, which would normally adjust
1389 -- the levels of the parent subprograms. So we need to
1390 -- be sure that the uplevel reference of that entity
1391 -- takes into account possible calls.
1393 if In_Synchronized_Unit
(SUBF
.Ent
)
1394 and then SUBT
.Lev
< SUBI
.Uplevel_Ref
1396 SUBI
.Uplevel_Ref
:= SUBT
.Lev
;
1400 exit when S
= URJ
.Callee
;
1403 -- Add to list of uplevel referenced entities for Callee.
1404 -- We do not add types to this list, only actual references
1405 -- to objects that will be referenced uplevel, and we use
1406 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1407 -- duplicate entries in the list. Discriminants are also
1408 -- excluded, only the enclosing object can appear in the
1411 if not Is_Uplevel_Referenced_Entity
(URJ
.Ent
)
1412 and then Ekind
(URJ
.Ent
) /= E_Discriminant
1414 Set_Is_Uplevel_Referenced_Entity
(URJ
.Ent
);
1415 Append_New_Elmt
(URJ
.Ent
, SUBT
.Uents
);
1418 -- And set uplevel indication for caller
1420 if SUBT
.Lev
< SUBF
.Uplevel_Ref
then
1421 SUBF
.Uplevel_Ref
:= SUBT
.Lev
;
1427 Urefs
.Set_Last
(New_Index
);
1430 -- Remove unreachable subprograms from Subps table. Note that we do
1431 -- this after eliminating entries from the other two tables, since
1432 -- those elimination steps depend on referencing the Subps table.
1438 New_SI
:= Subps_First
- 1;
1439 for J
in Subps_First
.. Subps
.Last
loop
1441 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1446 -- Subprogram is reachable, copy and reset index
1448 if STJ
.Reachable
then
1449 New_SI
:= New_SI
+ 1;
1450 Subps
.Table
(New_SI
) := STJ
;
1451 Set_Subps_Index
(STJ
.Ent
, UI_From_Int
(New_SI
));
1453 -- Subprogram is not reachable
1456 -- Clear index, since no longer active
1458 Set_Subps_Index
(Subps
.Table
(J
).Ent
, Uint_0
);
1460 -- Output debug information if -gnatd.3 set
1462 if Debug_Flag_Dot_3
then
1463 Write_Str
("Eliminate ");
1464 Write_Name
(Chars
(Subps
.Table
(J
).Ent
));
1466 Write_Location
(Sloc
(Subps
.Table
(J
).Ent
));
1467 Write_Str
(" (not referenced)");
1471 -- Rewrite declaration, body, and corresponding freeze node
1472 -- to null statements.
1474 -- A subprogram instantiation does not have an explicit
1475 -- body. If unused, we could remove the corresponding
1476 -- wrapper package and its body (TBD).
1478 if Present
(STJ
.Bod
) then
1479 Spec
:= Corresponding_Spec
(STJ
.Bod
);
1481 if Present
(Spec
) then
1482 Decl
:= Parent
(Declaration_Node
(Spec
));
1483 Rewrite
(Decl
, Make_Null_Statement
(Sloc
(Decl
)));
1485 if Present
(Freeze_Node
(Spec
)) then
1486 Rewrite
(Freeze_Node
(Spec
),
1487 Make_Null_Statement
(Sloc
(Decl
)));
1491 Rewrite
(STJ
.Bod
, Make_Null_Statement
(Sloc
(STJ
.Bod
)));
1497 Subps
.Set_Last
(New_SI
);
1500 -- Now it is time for the second transitive closure, which follows calls
1501 -- and makes sure that A calls B, and B has uplevel references, then A
1502 -- is also marked as having uplevel references.
1504 Closure_Uplevel
: declare
1508 -- We use a simple minded algorithm as follows (obviously this can
1509 -- be done more efficiently, using one of the standard algorithms
1510 -- for efficient transitive closure computation, but this is simple
1511 -- and most likely fast enough that its speed does not matter).
1513 -- Repeatedly scan the list of calls. Any time we find a call from
1514 -- A to B, where B has uplevel references, make sure that A is marked
1515 -- as having at least the same level of uplevel referencing.
1519 Inner2
: for J
in Calls
.First
.. Calls
.Last
loop
1521 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1522 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1523 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1524 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1525 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1527 if SUBT
.Lev
> SUBT
.Uplevel_Ref
1528 and then SUBF
.Uplevel_Ref
> SUBT
.Uplevel_Ref
1530 SUBF
.Uplevel_Ref
:= SUBT
.Uplevel_Ref
;
1536 exit Outer2
when not Modified
;
1538 end Closure_Uplevel
;
1540 -- We have one more step before the tables are complete. An uplevel
1541 -- call from subprogram A to subprogram B where subprogram B has uplevel
1542 -- references is in effect an uplevel reference, and must arrange for
1543 -- the proper activation link to be passed.
1545 for J
in Calls
.First
.. Calls
.Last
loop
1547 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1549 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1550 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1552 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1553 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1558 -- If callee has uplevel references
1560 if SUBT
.Uplevel_Ref
< SUBT
.Lev
1562 -- And this is an uplevel call
1564 and then SUBT
.Lev
< SUBF
.Lev
1566 -- We need to arrange for finding the uplink
1570 A
:= Enclosing_Subprogram
(A
);
1571 Subps
.Table
(Subp_Index
(A
)).Declares_AREC
:= True;
1572 exit when A
= CTJ
.Callee
;
1574 -- In any case exit when we get to the outer level. This
1575 -- happens in some odd cases with generics (in particular
1576 -- sem_ch3.adb does not compile without this kludge ???).
1584 -- The tables are now complete, so we can record the last index in the
1585 -- Subps table for later reference in Cprint.
1587 Subps
.Table
(Subps_First
).Last
:= Subps
.Last
;
1589 -- Next step, create the entities for code we will insert. We do this
1590 -- at the start so that all the entities are defined, regardless of the
1591 -- order in which we do the code insertions.
1593 Create_Entities
: for J
in Subps_First
.. Subps
.Last
loop
1595 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1596 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1599 -- First we create the ARECnF entity for the additional formal for
1600 -- all subprograms which need an activation record passed.
1602 if STJ
.Uplevel_Ref
< STJ
.Lev
then
1604 Make_Defining_Identifier
(Loc
, Chars
=> AREC_Name
(J
, "F"));
1607 -- Define the AREC entities for the activation record if needed
1609 if STJ
.Declares_AREC
then
1611 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, ""));
1613 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "T"));
1615 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "PT"));
1617 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "P"));
1619 -- Define uplink component entity if inner nesting case
1621 if Present
(STJ
.ARECnF
) then
1623 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "U"));
1627 end loop Create_Entities
;
1629 -- Loop through subprograms
1632 Addr
: Entity_Id
:= Empty
;
1635 for J
in Subps_First
.. Subps
.Last
loop
1637 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1640 -- First add the extra formal if needed. This applies to all
1641 -- nested subprograms that require an activation record to be
1642 -- passed, as indicated by ARECnF being defined.
1644 if Present
(STJ
.ARECnF
) then
1646 -- Here we need the extra formal. We do the expansion and
1647 -- analysis of this manually, since it is fairly simple,
1648 -- and it is not obvious how we can get what we want if we
1649 -- try to use the normal Analyze circuit.
1651 Add_Extra_Formal
: declare
1652 Encl
: constant SI_Type
:= Enclosing_Subp
(J
);
1653 STJE
: Subp_Entry
renames Subps
.Table
(Encl
);
1654 -- Index and Subp_Entry for enclosing routine
1656 Form
: constant Entity_Id
:= STJ
.ARECnF
;
1657 -- The formal to be added. Note that n here is one less
1658 -- than the level of the subprogram itself (STJ.Ent).
1660 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
);
1661 -- S is an N_Function/Procedure_Specification node, and F
1662 -- is the new entity to add to this subprogramn spec as
1663 -- the last Extra_Formal.
1665 ----------------------
1666 -- Add_Form_To_Spec --
1667 ----------------------
1669 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
) is
1670 Sub
: constant Entity_Id
:= Defining_Entity
(S
);
1674 -- Case of at least one Extra_Formal is present, set
1675 -- ARECnF as the new last entry in the list.
1677 if Present
(Extra_Formals
(Sub
)) then
1678 Ent
:= Extra_Formals
(Sub
);
1679 while Present
(Extra_Formal
(Ent
)) loop
1680 Ent
:= Extra_Formal
(Ent
);
1683 Set_Extra_Formal
(Ent
, F
);
1685 -- No Extra formals present
1688 Set_Extra_Formals
(Sub
, F
);
1689 Ent
:= Last_Formal
(Sub
);
1691 if Present
(Ent
) then
1692 Set_Extra_Formal
(Ent
, F
);
1695 end Add_Form_To_Spec
;
1697 -- Start of processing for Add_Extra_Formal
1700 -- Decorate the new formal entity
1702 Set_Scope
(Form
, STJ
.Ent
);
1703 Set_Ekind
(Form
, E_In_Parameter
);
1704 Set_Etype
(Form
, STJE
.ARECnPT
);
1705 Set_Mechanism
(Form
, By_Copy
);
1706 Set_Never_Set_In_Source
(Form
, True);
1707 Set_Analyzed
(Form
, True);
1708 Set_Comes_From_Source
(Form
, False);
1709 Set_Is_Activation_Record
(Form
, True);
1711 -- Case of only body present
1713 if Acts_As_Spec
(STJ
.Bod
) then
1714 Add_Form_To_Spec
(Form
, Specification
(STJ
.Bod
));
1716 -- Case of separate spec
1719 Add_Form_To_Spec
(Form
, Parent
(STJ
.Ent
));
1721 end Add_Extra_Formal
;
1724 -- Processing for subprograms that declare an activation record
1726 if Present
(STJ
.ARECn
) then
1728 -- Local declarations for one such subprogram
1731 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1733 Decls
: constant List_Id
:= New_List
;
1734 -- List of new declarations we create
1739 Decl_Assign
: Node_Id
;
1740 -- Assigment to set uplink, Empty if none
1742 Decl_ARECnT
: Node_Id
;
1743 Decl_ARECnPT
: Node_Id
;
1744 Decl_ARECn
: Node_Id
;
1745 Decl_ARECnP
: Node_Id
;
1746 -- Declaration nodes for the AREC entities we build
1749 -- Build list of component declarations for ARECnT and
1750 -- load System.Address.
1752 Clist
:= Empty_List
;
1755 Addr
:= RTE
(RE_Address
);
1758 -- If we are in a subprogram that has a static link that
1759 -- is passed in (as indicated by ARECnF being defined),
1760 -- then include ARECnU : ARECmPT where ARECmPT comes from
1761 -- the level one higher than the current level, and the
1762 -- entity ARECnPT comes from the enclosing subprogram.
1764 if Present
(STJ
.ARECnF
) then
1767 renames Subps
.Table
(Enclosing_Subp
(J
));
1770 Make_Component_Declaration
(Loc
,
1771 Defining_Identifier
=> STJ
.ARECnU
,
1772 Component_Definition
=>
1773 Make_Component_Definition
(Loc
,
1774 Subtype_Indication
=>
1775 New_Occurrence_Of
(STJE
.ARECnPT
, Loc
))));
1779 -- Add components for uplevel referenced entities
1781 if Present
(STJ
.Uents
) then
1788 -- 1's origin of index in list of elements. This is
1789 -- used to uniquify names if needed in Upref_Name.
1792 Elmt
:= First_Elmt
(STJ
.Uents
);
1794 while Present
(Elmt
) loop
1795 Uent
:= Node
(Elmt
);
1799 Make_Defining_Identifier
(Loc
,
1800 Chars
=> Upref_Name
(Uent
, Indx
, Clist
));
1802 Set_Activation_Record_Component
1805 if Needs_Fat_Pointer
(Uent
) then
1807 -- Build corresponding access type
1810 Build_Access_Type_Decl
1811 (Etype
(Uent
), STJ
.Ent
);
1812 Append_To
(Decls
, Ptr_Decl
);
1814 -- And use its type in the corresponding
1818 Make_Component_Declaration
(Loc
,
1819 Defining_Identifier
=> Comp
,
1820 Component_Definition
=>
1821 Make_Component_Definition
(Loc
,
1822 Subtype_Indication
=>
1824 (Defining_Identifier
(Ptr_Decl
),
1828 Make_Component_Declaration
(Loc
,
1829 Defining_Identifier
=> Comp
,
1830 Component_Definition
=>
1831 Make_Component_Definition
(Loc
,
1832 Subtype_Indication
=>
1833 New_Occurrence_Of
(Addr
, Loc
))));
1840 -- Now we can insert the AREC declarations into the body
1841 -- type ARECnT is record .. end record;
1842 -- pragma Suppress_Initialization (ARECnT);
1844 -- Note that we need to set the Suppress_Initialization
1845 -- flag after Decl_ARECnT has been analyzed.
1848 Make_Full_Type_Declaration
(Loc
,
1849 Defining_Identifier
=> STJ
.ARECnT
,
1851 Make_Record_Definition
(Loc
,
1853 Make_Component_List
(Loc
,
1854 Component_Items
=> Clist
)));
1855 Append_To
(Decls
, Decl_ARECnT
);
1857 -- type ARECnPT is access all ARECnT;
1860 Make_Full_Type_Declaration
(Loc
,
1861 Defining_Identifier
=> STJ
.ARECnPT
,
1863 Make_Access_To_Object_Definition
(Loc
,
1864 All_Present
=> True,
1865 Subtype_Indication
=>
1866 New_Occurrence_Of
(STJ
.ARECnT
, Loc
)));
1867 Append_To
(Decls
, Decl_ARECnPT
);
1869 -- ARECn : aliased ARECnT;
1872 Make_Object_Declaration
(Loc
,
1873 Defining_Identifier
=> STJ
.ARECn
,
1874 Aliased_Present
=> True,
1875 Object_Definition
=>
1876 New_Occurrence_Of
(STJ
.ARECnT
, Loc
));
1877 Append_To
(Decls
, Decl_ARECn
);
1879 -- ARECnP : constant ARECnPT := ARECn'Access;
1882 Make_Object_Declaration
(Loc
,
1883 Defining_Identifier
=> STJ
.ARECnP
,
1884 Constant_Present
=> True,
1885 Object_Definition
=>
1886 New_Occurrence_Of
(STJ
.ARECnPT
, Loc
),
1888 Make_Attribute_Reference
(Loc
,
1890 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1891 Attribute_Name
=> Name_Access
));
1892 Append_To
(Decls
, Decl_ARECnP
);
1894 -- If we are in a subprogram that has a static link that
1895 -- is passed in (as indicated by ARECnF being defined),
1896 -- then generate ARECn.ARECmU := ARECmF where m is
1897 -- one less than the current level to set the uplink.
1899 if Present
(STJ
.ARECnF
) then
1901 Make_Assignment_Statement
(Loc
,
1903 Make_Selected_Component
(Loc
,
1905 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1907 New_Occurrence_Of
(STJ
.ARECnU
, Loc
)),
1909 New_Occurrence_Of
(STJ
.ARECnF
, Loc
));
1910 Append_To
(Decls
, Decl_Assign
);
1913 Decl_Assign
:= Empty
;
1916 if No
(Declarations
(STJ
.Bod
)) then
1917 Set_Declarations
(STJ
.Bod
, Decls
);
1919 Prepend_List_To
(Declarations
(STJ
.Bod
), Decls
);
1922 -- Analyze the newly inserted declarations. Note that we
1923 -- do not need to establish the whole scope stack, since
1924 -- we have already set all entity fields (so there will
1925 -- be no searching of upper scopes to resolve names). But
1926 -- we do set the scope of the current subprogram, so that
1927 -- newly created entities go in the right entity chain.
1929 -- We analyze with all checks suppressed (since we do
1930 -- not expect any exceptions).
1932 Push_Scope
(STJ
.Ent
);
1933 Analyze
(Decl_ARECnT
, Suppress
=> All_Checks
);
1935 -- Note that we need to call Set_Suppress_Initialization
1936 -- after Decl_ARECnT has been analyzed, but before
1937 -- analyzing Decl_ARECnP so that the flag is properly
1938 -- taking into account.
1940 Set_Suppress_Initialization
(STJ
.ARECnT
);
1942 Analyze
(Decl_ARECnPT
, Suppress
=> All_Checks
);
1943 Analyze
(Decl_ARECn
, Suppress
=> All_Checks
);
1944 Analyze
(Decl_ARECnP
, Suppress
=> All_Checks
);
1946 if Present
(Decl_Assign
) then
1947 Analyze
(Decl_Assign
, Suppress
=> All_Checks
);
1952 -- Next step, for each uplevel referenced entity, add
1953 -- assignment operations to set the component in the
1954 -- activation record.
1956 if Present
(STJ
.Uents
) then
1961 Elmt
:= First_Elmt
(STJ
.Uents
);
1962 while Present
(Elmt
) loop
1964 Ent
: constant Entity_Id
:= Node
(Elmt
);
1965 Loc
: constant Source_Ptr
:= Sloc
(Ent
);
1966 Dec
: constant Node_Id
:=
1967 Declaration_Node
(Ent
);
1976 -- For parameters, we insert the assignment
1977 -- right after the declaration of ARECnP.
1978 -- For all other entities, we insert the
1979 -- assignment immediately after the
1980 -- declaration of the entity or after the
1981 -- freeze node if present.
1983 -- Note: we don't need to mark the entity
1984 -- as being aliased, because the address
1985 -- attribute will mark it as Address_Taken,
1986 -- and that is good enough.
1988 if Is_Formal
(Ent
) then
1991 elsif Has_Delayed_Freeze
(Ent
) then
1992 Ins
:= Freeze_Node
(Ent
);
1998 -- Build and insert the assignment:
1999 -- ARECn.nam := nam'Address
2000 -- or else 'Access for unconstrained array
2002 if Needs_Fat_Pointer
(Ent
) then
2003 Attr
:= Name_Access
;
2005 Attr
:= Name_Address
;
2009 Make_Attribute_Reference
(Loc
,
2011 New_Occurrence_Of
(Ent
, Loc
),
2012 Attribute_Name
=> Attr
);
2014 -- If the entity is an unconstrained formal
2015 -- we wrap the attribute reference in an
2016 -- unchecked conversion to the type of the
2017 -- activation record component, to prevent
2018 -- spurious subtype conformance errors within
2022 and then not Is_Constrained
(Etype
(Ent
))
2024 -- Find target component and its type
2026 Comp
:= First_Component
(STJ
.ARECnT
);
2027 while Chars
(Comp
) /= Chars
(Ent
) loop
2028 Comp
:= Next_Component
(Comp
);
2032 Unchecked_Convert_To
(Etype
(Comp
), Rhs
);
2036 Make_Assignment_Statement
(Loc
,
2038 Make_Selected_Component
(Loc
,
2040 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
2043 (Activation_Record_Component
2048 -- If we have a loop parameter, we have
2049 -- to insert before the first statement
2050 -- of the loop. Ins points to the
2051 -- N_Loop_Parameter_Specification or to
2052 -- an N_Iterator_Specification.
2055 (Ins
, N_Iterator_Specification
,
2056 N_Loop_Parameter_Specification
)
2058 -- Quantified expression are rewritten as
2059 -- loops during expansion.
2061 if Nkind
(Parent
(Ins
)) =
2062 N_Quantified_Expression
2070 (Parent
(Parent
(Ins
))));
2071 Insert_Before
(Ins
, Asn
);
2075 Insert_After
(Ins
, Asn
);
2078 -- Analyze the assignment statement. We do
2079 -- not need to establish the relevant scope
2080 -- stack entries here, because we have
2081 -- already set the correct entity references,
2082 -- so no name resolution is required, and no
2083 -- new entities are created, so we don't even
2084 -- need to set the current scope.
2086 -- We analyze with all checks suppressed
2087 -- (since we do not expect any exceptions).
2089 Analyze
(Asn
, Suppress
=> All_Checks
);
2102 -- Next step, process uplevel references. This has to be done in a
2103 -- separate pass, after completing the processing in Sub_Loop because we
2104 -- need all the AREC declarations generated, inserted, and analyzed so
2105 -- that the uplevel references can be successfully analyzed.
2107 Uplev_Refs
: for J
in Urefs
.First
.. Urefs
.Last
loop
2109 UPJ
: Uref_Entry
renames Urefs
.Table
(J
);
2112 -- Ignore type references, these are implicit references that do
2113 -- not need rewriting (e.g. the appearence in a conversion).
2114 -- Also ignore if no reference was specified or if the rewriting
2115 -- has already been done (this can happen if the N_Identifier
2116 -- occurs more than one time in the tree).
2119 or else not Is_Entity_Name
(UPJ
.Ref
)
2120 or else not Present
(Entity
(UPJ
.Ref
))
2125 -- Rewrite one reference
2127 Rewrite_One_Ref
: declare
2128 Loc
: constant Source_Ptr
:= Sloc
(UPJ
.Ref
);
2129 -- Source location for the reference
2131 Typ
: constant Entity_Id
:= Etype
(UPJ
.Ent
);
2132 -- The type of the referenced entity
2135 -- The actual subtype of the reference
2137 RS_Caller
: constant SI_Type
:= Subp_Index
(UPJ
.Caller
);
2138 -- Subp_Index for caller containing reference
2140 STJR
: Subp_Entry
renames Subps
.Table
(RS_Caller
);
2141 -- Subp_Entry for subprogram containing reference
2143 RS_Callee
: constant SI_Type
:= Subp_Index
(UPJ
.Callee
);
2144 -- Subp_Index for subprogram containing referenced entity
2146 STJE
: Subp_Entry
renames Subps
.Table
(RS_Callee
);
2147 -- Subp_Entry for subprogram containing referenced entity
2154 Atyp
:= Etype
(UPJ
.Ref
);
2156 if Ekind
(Atyp
) /= E_Record_Subtype
then
2157 Atyp
:= Get_Actual_Subtype
(UPJ
.Ref
);
2160 -- Ignore if no ARECnF entity for enclosing subprogram which
2161 -- probably happens as a result of not properly treating
2162 -- instance bodies. To be examined ???
2164 -- If this test is omitted, then the compilation of freeze.adb
2165 -- and inline.adb fail in unnesting mode.
2167 if No
(STJR
.ARECnF
) then
2171 -- Push the current scope, so that the pointer type Tnn, and
2172 -- any subsidiary entities resulting from the analysis of the
2173 -- rewritten reference, go in the right entity chain.
2175 Push_Scope
(STJR
.Ent
);
2177 -- Now we need to rewrite the reference. We have a reference
2178 -- from level STJR.Lev to level STJE.Lev. The general form of
2179 -- the rewritten reference for entity X is:
2181 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2183 -- where a,b,c,d .. m =
2184 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2186 pragma Assert
(STJR
.Lev
> STJE
.Lev
);
2188 -- Compute the prefix of X. Here are examples to make things
2189 -- clear (with parens to show groupings, the prefix is
2190 -- everything except the .X at the end).
2192 -- level 2 to level 1
2196 -- level 3 to level 1
2198 -- (AREC2F.AREC1U).X
2200 -- level 4 to level 1
2202 -- ((AREC3F.AREC2U).AREC1U).X
2204 -- level 6 to level 2
2206 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2208 -- In the above, ARECnF and ARECnU are pointers, so there are
2209 -- explicit dereferences required for these occurrences.
2212 Make_Explicit_Dereference
(Loc
,
2213 Prefix
=> New_Occurrence_Of
(STJR
.ARECnF
, Loc
));
2215 for L
in STJE
.Lev
.. STJR
.Lev
- 2 loop
2216 SI
:= Enclosing_Subp
(SI
);
2218 Make_Explicit_Dereference
(Loc
,
2220 Make_Selected_Component
(Loc
,
2223 New_Occurrence_Of
(Subps
.Table
(SI
).ARECnU
, Loc
)));
2226 -- Get activation record component (must exist)
2228 Comp
:= Activation_Record_Component
(UPJ
.Ent
);
2229 pragma Assert
(Present
(Comp
));
2231 -- Do the replacement. If the component type is an access type,
2232 -- this is an uplevel reference for an entity that requires a
2233 -- fat pointer, so dereference the component.
2235 if Is_Access_Type
(Etype
(Comp
)) then
2237 Make_Explicit_Dereference
(Loc
,
2239 Make_Selected_Component
(Loc
,
2242 New_Occurrence_Of
(Comp
, Loc
))));
2246 Make_Attribute_Reference
(Loc
,
2247 Prefix
=> New_Occurrence_Of
(Atyp
, Loc
),
2248 Attribute_Name
=> Name_Deref
,
2249 Expressions
=> New_List
(
2250 Make_Selected_Component
(Loc
,
2253 New_Occurrence_Of
(Comp
, Loc
)))));
2256 -- Analyze and resolve the new expression. We do not need to
2257 -- establish the relevant scope stack entries here, because we
2258 -- have already set all the correct entity references, so no
2259 -- name resolution is needed. We have already set the current
2260 -- scope, so that any new entities created will be in the right
2263 -- We analyze with all checks suppressed (since we do not
2264 -- expect any exceptions)
2266 Analyze_And_Resolve
(UPJ
.Ref
, Typ
, Suppress
=> All_Checks
);
2268 end Rewrite_One_Ref
;
2273 end loop Uplev_Refs
;
2275 -- Finally, loop through all calls adding extra actual for the
2276 -- activation record where it is required.
2278 Adjust_Calls
: for J
in Calls
.First
.. Calls
.Last
loop
2280 -- Process a single call, we are only interested in a call to a
2281 -- subprogram that actually needs a pointer to an activation record,
2282 -- as indicated by the ARECnF entity being set. This excludes the
2283 -- top level subprogram, and any subprogram not having uplevel refs.
2285 Adjust_One_Call
: declare
2286 CTJ
: Call_Entry
renames Calls
.Table
(J
);
2287 STF
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Caller
));
2288 STT
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Callee
));
2290 Loc
: constant Source_Ptr
:= Sloc
(CTJ
.N
);
2298 if Present
(STT
.ARECnF
)
2299 and then Nkind
(CTJ
.N
) in N_Subprogram_Call
2301 -- CTJ.N is a call to a subprogram which may require a pointer
2302 -- to an activation record. The subprogram containing the call
2303 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2304 -- have a call from level STF.Lev to level STT.Lev.
2306 -- There are three possibilities:
2308 -- For a call to the same level, we just pass the activation
2309 -- record passed to the calling subprogram.
2311 if STF
.Lev
= STT
.Lev
then
2312 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
2314 -- For a call that goes down a level, we pass a pointer to the
2315 -- activation record constructed within the caller (which may
2316 -- be the outer-level subprogram, but also may be a more deeply
2319 elsif STT
.Lev
= STF
.Lev
+ 1 then
2320 Extra
:= New_Occurrence_Of
(STF
.ARECnP
, Loc
);
2322 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2323 -- since it is not possible to do a downcall of more than
2326 -- For a call from level STF.Lev to level STT.Lev, we
2327 -- have to find the activation record needed by the
2328 -- callee. This is as follows:
2330 -- ARECaF.ARECbU.ARECcU....ARECmU
2332 -- where a,b,c .. m =
2333 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2336 pragma Assert
(STT
.Lev
< STF
.Lev
);
2338 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
2339 SubX
:= Subp_Index
(CTJ
.Caller
);
2340 for K
in reverse STT
.Lev
.. STF
.Lev
- 1 loop
2341 SubX
:= Enclosing_Subp
(SubX
);
2343 Make_Selected_Component
(Loc
,
2347 (Subps
.Table
(SubX
).ARECnU
, Loc
));
2351 -- Extra is the additional parameter to be added. Build a
2352 -- parameter association that we can append to the actuals.
2355 Make_Parameter_Association
(Loc
,
2357 New_Occurrence_Of
(STT
.ARECnF
, Loc
),
2358 Explicit_Actual_Parameter
=> Extra
);
2360 if No
(Parameter_Associations
(CTJ
.N
)) then
2361 Set_Parameter_Associations
(CTJ
.N
, Empty_List
);
2364 Append
(ExtraP
, Parameter_Associations
(CTJ
.N
));
2366 -- We need to deal with the actual parameter chain as well. The
2367 -- newly added parameter is always the last actual.
2369 Act
:= First_Named_Actual
(CTJ
.N
);
2372 Set_First_Named_Actual
(CTJ
.N
, Extra
);
2374 -- If call has been relocated (as with an expression in
2375 -- an aggregate), set First_Named pointer in original node
2376 -- as well, because that's the parent of the parameter list.
2378 Set_First_Named_Actual
2379 (Parent
(List_Containing
(ExtraP
)), Extra
);
2381 -- Here we must follow the chain and append the new entry
2390 PAN
:= Parent
(Act
);
2391 pragma Assert
(Nkind
(PAN
) = N_Parameter_Association
);
2392 NNA
:= Next_Named_Actual
(PAN
);
2395 Set_Next_Named_Actual
(PAN
, Extra
);
2404 -- Analyze and resolve the new actual. We do not need to
2405 -- establish the relevant scope stack entries here, because
2406 -- we have already set all the correct entity references, so
2407 -- no name resolution is needed.
2409 -- We analyze with all checks suppressed (since we do not
2410 -- expect any exceptions, and also we temporarily turn off
2411 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2412 -- references (not needed at this stage, and in fact causes
2413 -- a bit of recursive chaos).
2415 Opt
.Unnest_Subprogram_Mode
:= False;
2417 (Extra
, Etype
(STT
.ARECnF
), Suppress
=> All_Checks
);
2418 Opt
.Unnest_Subprogram_Mode
:= True;
2420 end Adjust_One_Call
;
2421 end loop Adjust_Calls
;
2424 end Unnest_Subprogram
;
2426 ------------------------
2427 -- Unnest_Subprograms --
2428 ------------------------
2430 procedure Unnest_Subprograms
(N
: Node_Id
) is
2431 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
;
2432 -- Tree visitor that search for outer level procedures with nested
2433 -- subprograms and invokes Unnest_Subprogram()
2439 procedure Do_Search
is new Traverse_Proc
(Search_Subprograms
);
2440 -- Subtree visitor instantiation
2442 ------------------------
2443 -- Search_Subprograms --
2444 ------------------------
2446 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
is
2448 if Nkind_In
(N
, N_Subprogram_Body
, N_Subprogram_Body_Stub
) then
2450 Spec_Id
: constant Entity_Id
:= Unique_Defining_Entity
(N
);
2453 -- We are only interested in subprograms (not generic
2454 -- subprograms), that have nested subprograms.
2456 if Is_Subprogram
(Spec_Id
)
2457 and then Has_Nested_Subprogram
(Spec_Id
)
2458 and then Is_Library_Level_Entity
(Spec_Id
)
2460 Unnest_Subprogram
(Spec_Id
, N
);
2464 -- The proper body of a stub may contain nested subprograms, and
2465 -- therefore must be visited explicitly. Nested stubs are examined
2466 -- recursively in Visit_Node.
2468 elsif Nkind
(N
) in N_Body_Stub
then
2469 Do_Search
(Library_Unit
(N
));
2471 -- Skip generic packages
2473 elsif Nkind
(N
) = N_Package_Body
2474 and then Ekind
(Corresponding_Spec
(N
)) = E_Generic_Package
2480 end Search_Subprograms
;
2482 -- Start of processing for Unnest_Subprograms
2485 if not Opt
.Unnest_Subprogram_Mode
or not Opt
.Expander_Active
then
2489 -- A specification will contain bodies if it contains instantiations so
2490 -- examine package or subprogram declaration of the main unit, when it
2493 if Nkind
(Unit
(N
)) = N_Package_Body
2494 or else (Nkind
(Unit
(N
)) = N_Subprogram_Body
2495 and then not Acts_As_Spec
(N
))
2497 Do_Search
(Library_Unit
(N
));
2501 end Unnest_Subprograms
;