1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2014-2019, 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
57 (Subp
: Entity_Id
; Subp_Body
: Node_Id
; For_Inline
: Boolean := False);
58 -- Subp is a library-level subprogram which has nested subprograms, and
59 -- Subp_Body is the corresponding N_Subprogram_Body node. This procedure
60 -- declares the AREC types and objects, adds assignments to the AREC record
61 -- as required, defines the xxxPTR types for uplevel referenced objects,
62 -- adds the ARECP parameter to all nested subprograms which need it, and
63 -- modifies all uplevel references appropriately. If For_Inline is True,
64 -- we're unnesting this subprogram because it's on the list of inlined
65 -- subprograms and should unnest it despite it not being part of the main
72 -- Table to record calls within the nest being analyzed. These are the
73 -- calls which may need to have an AREC actual added. This table is built
74 -- new for each subprogram nest and cleared at the end of processing each
77 type Call_Entry
is record
82 -- Entity of the subprogram containing the call (can be at any level)
85 -- Entity of the subprogram called (always at level 2 or higher). Note
86 -- that in accordance with the basic rules of nesting, the level of To
87 -- is either less than or equal to the level of From, or one greater.
90 package Calls
is new Table
.Table
(
91 Table_Component_Type
=> Call_Entry
,
92 Table_Index_Type
=> Nat
,
95 Table_Increment
=> 200,
96 Table_Name
=> "Unnest_Calls");
97 -- Records each call within the outer subprogram and all nested subprograms
98 -- that are to other subprograms nested within the outer subprogram. These
99 -- are the calls that may need an additional parameter.
101 procedure Append_Unique_Call
(Call
: Call_Entry
);
102 -- Append a call entry to the Calls table. A check is made to see if the
103 -- table already contains this entry and if so it has no effect.
105 ----------------------------------
106 -- Subprograms For Fat Pointers --
107 ----------------------------------
109 function Build_Access_Type_Decl
111 Scop
: Entity_Id
) return Node_Id
;
112 -- For an uplevel reference that involves an unconstrained array type,
113 -- build an access type declaration for the corresponding activation
114 -- record component. The relevant attributes of the access type are
115 -- set here to avoid a full analysis that would require a scope stack.
117 function Needs_Fat_Pointer
(E
: Entity_Id
) return Boolean;
118 -- A formal parameter of an unconstrained array type that appears in an
119 -- uplevel reference requires the construction of an access type, to be
120 -- used in the corresponding component declaration.
126 -- Table to record explicit uplevel references to objects (variables,
127 -- constants, formal parameters). These are the references that will
128 -- need rewriting to use the activation table (AREC) pointers. Also
129 -- included are implicit and explicit uplevel references to types, but
130 -- these do not get rewritten by the front end. This table is built new
131 -- for each subprogram nest and cleared at the end of processing each
134 type Uref_Entry
is record
136 -- The reference itself. For objects this is always an entity reference
137 -- and the referenced entity will have its Is_Uplevel_Referenced_Entity
138 -- flag set and will appear in the Uplevel_Referenced_Entities list of
139 -- the subprogram declaring this entity.
142 -- The Entity_Id of the uplevel referenced object or type
145 -- The entity for the subprogram immediately containing this entity
148 -- The entity for the subprogram containing the referenced entity. Note
149 -- that the level of Callee must be less than the level of Caller, since
150 -- this is an uplevel reference.
153 package Urefs
is new Table
.Table
(
154 Table_Component_Type
=> Uref_Entry
,
155 Table_Index_Type
=> Nat
,
156 Table_Low_Bound
=> 1,
157 Table_Initial
=> 100,
158 Table_Increment
=> 200,
159 Table_Name
=> "Unnest_Urefs");
161 ------------------------
162 -- Append_Unique_Call --
163 ------------------------
165 procedure Append_Unique_Call
(Call
: Call_Entry
) is
167 for J
in Calls
.First
.. Calls
.Last
loop
168 if Calls
.Table
(J
) = Call
then
174 end Append_Unique_Call
;
176 -----------------------------
177 -- Build_Access_Type_Decl --
178 -----------------------------
180 function Build_Access_Type_Decl
182 Scop
: Entity_Id
) return Node_Id
184 Loc
: constant Source_Ptr
:= Sloc
(E
);
188 Typ
:= Make_Temporary
(Loc
, 'S');
189 Set_Ekind
(Typ
, E_General_Access_Type
);
190 Set_Etype
(Typ
, Typ
);
191 Set_Scope
(Typ
, Scop
);
192 Set_Directly_Designated_Type
(Typ
, Etype
(E
));
195 Make_Full_Type_Declaration
(Loc
,
196 Defining_Identifier
=> Typ
,
198 Make_Access_To_Object_Definition
(Loc
,
199 Subtype_Indication
=> New_Occurrence_Of
(Etype
(E
), Loc
)));
200 end Build_Access_Type_Decl
;
206 function Get_Level
(Subp
: Entity_Id
; Sub
: Entity_Id
) return Nat
is
218 S
:= Enclosing_Subprogram
(S
);
223 --------------------------
224 -- In_Synchronized_Unit --
225 --------------------------
227 function In_Synchronized_Unit
(Subp
: Entity_Id
) return Boolean is
228 S
: Entity_Id
:= Scope
(Subp
);
231 while Present
(S
) and then S
/= Standard_Standard
loop
232 if Is_Concurrent_Type
(S
) then
235 elsif Is_Private_Type
(S
)
236 and then Present
(Full_View
(S
))
237 and then Is_Concurrent_Type
(Full_View
(S
))
246 end In_Synchronized_Unit
;
248 -----------------------
249 -- Needs_Fat_Pointer --
250 -----------------------
252 function Needs_Fat_Pointer
(E
: Entity_Id
) return Boolean is
255 if Is_Formal
(E
) then
257 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
258 Typ
:= Full_View
(Typ
);
261 return Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
);
265 end Needs_Fat_Pointer
;
271 function Subp_Index
(Sub
: Entity_Id
) return SI_Type
is
272 E
: Entity_Id
:= Sub
;
275 pragma Assert
(Is_Subprogram
(E
));
277 if Subps_Index
(E
) = Uint_0
then
278 E
:= Ultimate_Alias
(E
);
280 -- The body of a protected operation has a different name and
281 -- has been scanned at this point, and thus has an entry in the
284 if E
= Sub
and then Convention
(E
) = Convention_Protected
then
285 E
:= Protected_Body_Subprogram
(E
);
288 if Ekind
(E
) = E_Function
289 and then Rewritten_For_C
(E
)
290 and then Present
(Corresponding_Procedure
(E
))
292 E
:= Corresponding_Procedure
(E
);
296 pragma Assert
(Subps_Index
(E
) /= Uint_0
);
297 return SI_Type
(UI_To_Int
(Subps_Index
(E
)));
300 -----------------------
301 -- Unnest_Subprogram --
302 -----------------------
304 procedure Unnest_Subprogram
305 (Subp
: Entity_Id
; Subp_Body
: Node_Id
; For_Inline
: Boolean := False) is
306 function AREC_Name
(J
: Pos
; S
: String) return Name_Id
;
307 -- Returns name for string ARECjS, where j is the decimal value of j
309 function Enclosing_Subp
(Subp
: SI_Type
) return SI_Type
;
310 -- Subp is the index of a subprogram which has a Lev greater than 1.
311 -- This function returns the index of the enclosing subprogram which
312 -- will have a Lev value one less than this.
314 function Img_Pos
(N
: Pos
) return String;
315 -- Return image of N without leading blank
320 Clist
: List_Id
) return Name_Id
;
321 -- This function returns the name to be used in the activation record to
322 -- reference the variable uplevel. Clist is the list of components that
323 -- have been created in the activation record so far. Normally the name
324 -- is just a copy of the Chars field of the entity. The exception is
325 -- when the name has already been used, in which case we suffix the name
326 -- with the index value Index to avoid duplication. This happens with
327 -- declare blocks and generic parameters at least.
333 function AREC_Name
(J
: Pos
; S
: String) return Name_Id
is
335 return Name_Find
("AREC" & Img_Pos
(J
) & S
);
342 function Enclosing_Subp
(Subp
: SI_Type
) return SI_Type
is
343 STJ
: Subp_Entry
renames Subps
.Table
(Subp
);
344 Ret
: constant SI_Type
:= Subp_Index
(Enclosing_Subprogram
(STJ
.Ent
));
346 pragma Assert
(STJ
.Lev
> 1);
347 pragma Assert
(Subps
.Table
(Ret
).Lev
= STJ
.Lev
- 1);
355 function Img_Pos
(N
: Pos
) return String is
356 Buf
: String (1 .. 20);
364 Buf
(Ptr
) := Character'Val (48 + NV
mod 10);
369 return Buf
(Ptr
+ 1 .. Buf
'Last);
379 Clist
: List_Id
) return Name_Id
388 elsif Chars
(Defining_Identifier
(C
)) = Chars
(Ent
) then
390 Name_Find
(Get_Name_String
(Chars
(Ent
)) & Img_Pos
(Index
));
397 -- Start of processing for Unnest_Subprogram
400 -- Nothing to do inside a generic (all processing is for instance)
402 if Inside_A_Generic
then
406 -- If the main unit is a package body then we need to examine the spec
407 -- to determine whether the main unit is generic (the scope stack is not
408 -- present when this is called on the main unit).
411 and then Ekind
(Cunit_Entity
(Main_Unit
)) = E_Package_Body
412 and then Is_Generic_Unit
(Spec_Entity
(Cunit_Entity
(Main_Unit
)))
416 -- Only unnest when generating code for the main source unit or if we're
417 -- unnesting for inline.
420 and then not In_Extended_Main_Code_Unit
(Subp_Body
)
425 -- This routine is called late, after the scope stack is gone. The
426 -- following creates a suitable dummy scope stack to be used for the
427 -- analyze/expand calls made from this routine.
431 -- First step, we must mark all nested subprograms that require a static
432 -- link (activation record) because either they contain explicit uplevel
433 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
434 -- this point), or they make calls to other subprograms in the same nest
435 -- that require a static link (in which case we set this flag).
437 -- This is a recursive definition, and to implement this, we have to
438 -- build a call graph for the set of nested subprograms, and then go
439 -- over this graph to implement recursively the invariant that if a
440 -- subprogram has a call to a subprogram requiring a static link, then
441 -- the calling subprogram requires a static link.
443 -- First populate the above tables
445 Subps_First
:= Subps
.Last
+ 1;
449 Build_Tables
: declare
450 Current_Subprogram
: Entity_Id
:= Empty
;
451 -- When we scan a subprogram body, we set Current_Subprogram to the
452 -- corresponding entity. This gets recursively saved and restored.
454 function Visit_Node
(N
: Node_Id
) return Traverse_Result
;
455 -- Visit a single node in Subp
461 procedure Visit
is new Traverse_Proc
(Visit_Node
);
462 -- Used to traverse the body of Subp, populating the tables
468 function Visit_Node
(N
: Node_Id
) return Traverse_Result
is
473 procedure Check_Static_Type
477 Check_Designated
: Boolean := False);
478 -- Given a type T, checks if it is a static type defined as a type
479 -- with no dynamic bounds in sight. If so, the only action is to
480 -- set Is_Static_Type True for T. If T is not a static type, then
481 -- all types with dynamic bounds associated with T are detected,
482 -- and their bounds are marked as uplevel referenced if not at the
483 -- library level, and DT is set True. If N is specified, it's the
484 -- node that will need to be replaced. If not specified, it means
485 -- we can't do a replacement because the bound is implicit.
487 -- If Check_Designated is True and T or its full view is an access
488 -- type, check whether the designated type has dynamic bounds.
490 procedure Note_Uplevel_Ref
495 -- Called when we detect an explicit or implicit uplevel reference
496 -- from within Caller to entity E declared in Callee. E can be a
497 -- an object or a type.
499 procedure Register_Subprogram
(E
: Entity_Id
; Bod
: Node_Id
);
500 -- Enter a subprogram whose body is visible or which is a
501 -- subprogram instance into the subprogram table.
503 -----------------------
504 -- Check_Static_Type --
505 -----------------------
507 procedure Check_Static_Type
511 Check_Designated
: Boolean := False)
513 procedure Note_Uplevel_Bound
(N
: Node_Id
; Ref
: Node_Id
);
514 -- N is the bound of a dynamic type. This procedure notes that
515 -- this bound is uplevel referenced, it can handle references
516 -- to entities (typically _FIRST and _LAST entities), and also
517 -- attribute references of the form T'name (name is typically
518 -- FIRST or LAST) where T is the uplevel referenced bound.
519 -- Ref, if Present, is the location of the reference to
522 ------------------------
523 -- Note_Uplevel_Bound --
524 ------------------------
526 procedure Note_Uplevel_Bound
(N
: Node_Id
; Ref
: Node_Id
) is
528 -- Entity name case. Make sure that the entity is declared
529 -- in a subprogram. This may not be the case for for a type
530 -- in a loop appearing in a precondition.
531 -- Exclude explicitly discriminants (that can appear
532 -- in bounds of discriminated components).
534 if Is_Entity_Name
(N
) then
535 if Present
(Entity
(N
))
536 and then not Is_Type
(Entity
(N
))
537 and then Present
(Enclosing_Subprogram
(Entity
(N
)))
538 and then Ekind
(Entity
(N
)) /= E_Discriminant
543 Caller
=> Current_Subprogram
,
544 Callee
=> Enclosing_Subprogram
(Entity
(N
)));
547 -- Attribute or indexed component case
549 elsif Nkind_In
(N
, N_Attribute_Reference
,
552 Note_Uplevel_Bound
(Prefix
(N
), Ref
);
554 -- The indices of the indexed components, or the
555 -- associated expressions of an attribute reference,
556 -- may also involve uplevel references.
562 Expr
:= First
(Expressions
(N
));
563 while Present
(Expr
) loop
564 Note_Uplevel_Bound
(Expr
, Ref
);
569 -- The type of the prefix may be have an uplevel
570 -- reference if this needs bounds.
572 if Nkind
(N
) = N_Attribute_Reference
then
574 Attr
: constant Attribute_Id
:=
575 Get_Attribute_Id
(Attribute_Name
(N
));
576 DT
: Boolean := False;
579 if (Attr
= Attribute_First
580 or else Attr
= Attribute_Last
581 or else Attr
= Attribute_Length
)
582 and then Is_Constrained
(Etype
(Prefix
(N
)))
585 (Etype
(Prefix
(N
)), Empty
, DT
);
590 -- Binary operator cases. These can apply to arrays for
591 -- which we may need bounds.
593 elsif Nkind
(N
) in N_Binary_Op
then
594 Note_Uplevel_Bound
(Left_Opnd
(N
), Ref
);
595 Note_Uplevel_Bound
(Right_Opnd
(N
), Ref
);
597 -- Unary operator case
599 elsif Nkind
(N
) in N_Unary_Op
then
600 Note_Uplevel_Bound
(Right_Opnd
(N
), Ref
);
602 -- Explicit dereference and selected component case
604 elsif Nkind_In
(N
, N_Explicit_Dereference
,
605 N_Selected_Component
)
607 Note_Uplevel_Bound
(Prefix
(N
), Ref
);
609 -- Conditional expressions
611 elsif Nkind
(N
) = N_If_Expression
then
616 Expr
:= First
(Expressions
(N
));
617 while Present
(Expr
) loop
618 Note_Uplevel_Bound
(Expr
, Ref
);
623 elsif Nkind
(N
) = N_Case_Expression
then
625 Alternative
: Node_Id
;
628 Note_Uplevel_Bound
(Expression
(N
), Ref
);
630 Alternative
:= First
(Alternatives
(N
));
631 while Present
(Alternative
) loop
632 Note_Uplevel_Bound
(Expression
(Alternative
), Ref
);
638 elsif Nkind
(N
) = N_Type_Conversion
then
639 Note_Uplevel_Bound
(Expression
(N
), Ref
);
641 end Note_Uplevel_Bound
;
643 -- Start of processing for Check_Static_Type
646 -- If already marked static, immediate return
648 if Is_Static_Type
(T
) and then not Check_Designated
then
652 -- If the type is at library level, always consider it static,
653 -- since such uplevel references are irrelevant.
655 if Is_Library_Level_Entity
(T
) then
656 Set_Is_Static_Type
(T
);
660 -- Otherwise figure out what the story is with this type
662 -- For a scalar type, check bounds
664 if Is_Scalar_Type
(T
) then
666 -- If both bounds static, then this is a static type
669 LB
: constant Node_Id
:= Type_Low_Bound
(T
);
670 UB
: constant Node_Id
:= Type_High_Bound
(T
);
673 if not Is_Static_Expression
(LB
) then
674 Note_Uplevel_Bound
(LB
, N
);
678 if not Is_Static_Expression
(UB
) then
679 Note_Uplevel_Bound
(UB
, N
);
684 -- For record type, check all components and discriminant
685 -- constraints if present.
687 elsif Is_Record_Type
(T
) then
693 C
:= First_Component_Or_Discriminant
(T
);
694 while Present
(C
) loop
695 Check_Static_Type
(Etype
(C
), N
, DT
);
696 Next_Component_Or_Discriminant
(C
);
699 if Has_Discriminants
(T
)
700 and then Present
(Discriminant_Constraint
(T
))
702 D
:= First_Elmt
(Discriminant_Constraint
(T
));
703 while Present
(D
) loop
704 if not Is_Static_Expression
(Node
(D
)) then
705 Note_Uplevel_Bound
(Node
(D
), N
);
714 -- For array type, check index types and component type
716 elsif Is_Array_Type
(T
) then
720 Check_Static_Type
(Component_Type
(T
), N
, DT
);
722 IX
:= First_Index
(T
);
723 while Present
(IX
) loop
724 Check_Static_Type
(Etype
(IX
), N
, DT
);
729 -- For private type, examine whether full view is static
731 elsif Is_Incomplete_Or_Private_Type
(T
)
732 and then Present
(Full_View
(T
))
734 Check_Static_Type
(Full_View
(T
), N
, DT
, Check_Designated
);
736 if Is_Static_Type
(Full_View
(T
)) then
737 Set_Is_Static_Type
(T
);
740 -- For access types, check designated type when required
742 elsif Is_Access_Type
(T
) and then Check_Designated
then
743 Check_Static_Type
(Directly_Designated_Type
(T
), N
, DT
);
745 -- For now, ignore other types
752 Set_Is_Static_Type
(T
);
754 end Check_Static_Type
;
756 ----------------------
757 -- Note_Uplevel_Ref --
758 ----------------------
760 procedure Note_Uplevel_Ref
766 Full_E
: Entity_Id
:= E
;
768 -- Nothing to do for static type
770 if Is_Static_Type
(E
) then
774 -- Nothing to do if Caller and Callee are the same
776 if Caller
= Callee
then
779 -- Callee may be a function that returns an array, and that has
780 -- been rewritten as a procedure. If caller is that procedure,
781 -- nothing to do either.
783 elsif Ekind
(Callee
) = E_Function
784 and then Rewritten_For_C
(Callee
)
785 and then Corresponding_Procedure
(Callee
) = Caller
789 elsif Ekind_In
(Callee
, E_Entry
, E_Entry_Family
) then
793 -- We have a new uplevel referenced entity
795 if Ekind
(E
) = E_Constant
and then Present
(Full_View
(E
)) then
796 Full_E
:= Full_View
(E
);
799 -- All we do at this stage is to add the uplevel reference to
800 -- the table. It's too early to do anything else, since this
801 -- uplevel reference may come from an unreachable subprogram
802 -- in which case the entry will be deleted.
804 Urefs
.Append
((N
, Full_E
, Caller
, Callee
));
805 end Note_Uplevel_Ref
;
807 -------------------------
808 -- Register_Subprogram --
809 -------------------------
811 procedure Register_Subprogram
(E
: Entity_Id
; Bod
: Node_Id
) is
812 L
: constant Nat
:= Get_Level
(Subp
, E
);
815 -- Subprograms declared in tasks and protected types cannot be
816 -- eliminated because calls to them may be in other units, so
817 -- they must be treated as reachable.
823 Reachable
=> In_Synchronized_Unit
(E
)
824 or else Address_Taken
(E
),
826 Declares_AREC
=> False,
836 Set_Subps_Index
(E
, UI_From_Int
(Subps
.Last
));
838 -- If we marked this reachable because it's in a synchronized
839 -- unit, we have to mark all enclosing subprograms as reachable
842 if In_Synchronized_Unit
(E
) then
847 for J
in reverse 1 .. L
- 1 loop
848 S
:= Enclosing_Subprogram
(S
);
849 Subps
.Table
(Subp_Index
(S
)).Reachable
:= True;
853 end Register_Subprogram
;
855 -- Start of processing for Visit_Node
860 -- Record a subprogram call
863 | N_Procedure_Call_Statement
865 -- We are only interested in direct calls, not indirect
866 -- calls (where Name (N) is an explicit dereference) at
869 if Nkind
(Name
(N
)) in N_Has_Entity
then
870 Ent
:= Entity
(Name
(N
));
872 -- We are only interested in calls to subprograms nested
873 -- within Subp. Calls to Subp itself or to subprograms
874 -- outside the nested structure do not affect us.
876 if Scope_Within
(Ent
, Subp
)
877 and then Is_Subprogram
(Ent
)
878 and then not Is_Imported
(Ent
)
880 Append_Unique_Call
((N
, Current_Subprogram
, Ent
));
884 -- For all calls where the formal is an unconstrained array
885 -- and the actual is constrained we need to check the bounds
886 -- for uplevel references.
890 DT
: Boolean := False;
895 if Nkind
(Name
(N
)) = N_Explicit_Dereference
then
896 Subp
:= Etype
(Name
(N
));
898 Subp
:= Entity
(Name
(N
));
901 Actual
:= First_Actual
(N
);
902 Formal
:= First_Formal_With_Extras
(Subp
);
903 while Present
(Actual
) loop
904 if Is_Array_Type
(Etype
(Formal
))
905 and then not Is_Constrained
(Etype
(Formal
))
906 and then Is_Constrained
(Etype
(Actual
))
908 Check_Static_Type
(Etype
(Actual
), Empty
, DT
);
911 Next_Actual
(Actual
);
912 Next_Formal_With_Extras
(Formal
);
916 -- An At_End_Proc in a statement sequence indicates that there
917 -- is a call from the enclosing construct or block to that
918 -- subprogram. As above, the called entity must be local and
921 when N_Handled_Sequence_Of_Statements
=>
922 if Present
(At_End_Proc
(N
))
923 and then Scope_Within
(Entity
(At_End_Proc
(N
)), Subp
)
924 and then not Is_Imported
(Entity
(At_End_Proc
(N
)))
927 ((N
, Current_Subprogram
, Entity
(At_End_Proc
(N
))));
930 -- Similarly, the following constructs include a semantic
931 -- attribute Procedure_To_Call that must be handled like
932 -- other calls. Likewise for attribute Storage_Pool.
935 | N_Extended_Return_Statement
937 | N_Simple_Return_Statement
940 Pool
: constant Entity_Id
:= Storage_Pool
(N
);
941 Proc
: constant Entity_Id
:= Procedure_To_Call
(N
);
945 and then Scope_Within
(Proc
, Subp
)
946 and then not Is_Imported
(Proc
)
948 Append_Unique_Call
((N
, Current_Subprogram
, Proc
));
952 and then not Is_Library_Level_Entity
(Pool
)
953 and then Scope_Within_Or_Same
(Scope
(Pool
), Subp
)
955 Caller
:= Current_Subprogram
;
956 Callee
:= Enclosing_Subprogram
(Pool
);
958 if Callee
/= Caller
then
959 Note_Uplevel_Ref
(Pool
, Empty
, Caller
, Callee
);
964 -- For an allocator with a qualified expression, check type
965 -- of expression being qualified. The explicit type name is
966 -- handled as an entity reference.
968 if Nkind
(N
) = N_Allocator
969 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
972 DT
: Boolean := False;
975 (Etype
(Expression
(Expression
(N
))), Empty
, DT
);
978 -- For a Return or Free (all other nodes we handle here),
979 -- we usually need the size of the object, so we need to be
980 -- sure that any nonstatic bounds of the expression's type
981 -- that are uplevel are handled.
983 elsif Nkind
(N
) /= N_Allocator
984 and then Present
(Expression
(N
))
987 DT
: Boolean := False;
990 (Etype
(Expression
(N
)),
993 Check_Designated
=> Nkind
(N
) = N_Free_Statement
);
997 -- A 'Access reference is a (potential) call. So is 'Address,
998 -- in particular on imported subprograms. Other attributes
999 -- require special handling.
1001 when N_Attribute_Reference
=>
1003 Attr
: constant Attribute_Id
:=
1004 Get_Attribute_Id
(Attribute_Name
(N
));
1007 when Attribute_Access
1008 | Attribute_Unchecked_Access
1009 | Attribute_Unrestricted_Access
1012 if Nkind
(Prefix
(N
)) in N_Has_Entity
then
1013 Ent
:= Entity
(Prefix
(N
));
1015 -- We only need to examine calls to subprograms
1016 -- nested within current Subp.
1018 if Scope_Within
(Ent
, Subp
) then
1019 if Is_Imported
(Ent
) then
1022 elsif Is_Subprogram
(Ent
) then
1024 ((N
, Current_Subprogram
, Ent
));
1029 -- References to bounds can be uplevel references if
1030 -- the type isn't static.
1032 when Attribute_First
1036 -- Special-case attributes of objects whose bounds
1037 -- may be uplevel references. More complex prefixes
1038 -- handled during full traversal. Note that if the
1039 -- nominal subtype of the prefix is unconstrained,
1040 -- the bound must be obtained from the object, not
1041 -- from the (possibly) uplevel reference.
1043 if Is_Constrained
(Etype
(Prefix
(N
))) then
1045 DT
: Boolean := False;
1048 (Etype
(Prefix
(N
)), Empty
, DT
);
1059 -- Component associations in aggregates are either static or
1060 -- else the aggregate will be expanded into assignments, in
1061 -- which case the expression is analyzed later and provides
1062 -- no relevant code generation.
1064 when N_Component_Association
=>
1065 if No
(Expression
(N
))
1066 or else No
(Etype
(Expression
(N
)))
1071 -- Generic associations are not analyzed: the actuals are
1072 -- transferred to renaming and subtype declarations that
1073 -- are the ones that must be examined.
1075 when N_Generic_Association
=>
1078 -- Indexed references can be uplevel if the type isn't static
1079 -- and if the lower bound (or an inner bound for a multi-
1080 -- dimensional array) is uplevel.
1082 when N_Indexed_Component
1085 if Is_Constrained
(Etype
(Prefix
(N
))) then
1087 DT
: Boolean := False;
1089 Check_Static_Type
(Etype
(Prefix
(N
)), Empty
, DT
);
1093 -- A selected component can have an implicit up-level
1094 -- reference due to the bounds of previous fields in the
1095 -- record. We simplify the processing here by examining
1096 -- all components of the record.
1098 -- Selected components appear as unit names and end labels
1099 -- for child units. Prefixes of these nodes denote parent
1100 -- units and carry no type information so they are skipped.
1102 when N_Selected_Component
=>
1103 if Present
(Etype
(Prefix
(N
))) then
1105 DT
: Boolean := False;
1107 Check_Static_Type
(Etype
(Prefix
(N
)), Empty
, DT
);
1111 -- For EQ/NE comparisons, we need the type of the operands
1112 -- in order to do the comparison, which means we need the
1119 DT
: Boolean := False;
1121 Check_Static_Type
(Etype
(Left_Opnd
(N
)), Empty
, DT
);
1122 Check_Static_Type
(Etype
(Right_Opnd
(N
)), Empty
, DT
);
1125 -- Likewise we need the sizes to compute how much to move in
1128 when N_Assignment_Statement
=>
1130 DT
: Boolean := False;
1132 Check_Static_Type
(Etype
(Name
(N
)), Empty
, DT
);
1133 Check_Static_Type
(Etype
(Expression
(N
)), Empty
, DT
);
1136 -- Record a subprogram. We record a subprogram body that acts
1137 -- as a spec. Otherwise we record a subprogram declaration,
1138 -- providing that it has a corresponding body we can get hold
1139 -- of. The case of no corresponding body being available is
1142 when N_Subprogram_Body
=>
1143 Ent
:= Unique_Defining_Entity
(N
);
1145 -- Ignore generic subprogram
1147 if Is_Generic_Subprogram
(Ent
) then
1151 -- Make new entry in subprogram table if not already made
1153 Register_Subprogram
(Ent
, N
);
1155 -- We make a recursive call to scan the subprogram body, so
1156 -- that we can save and restore Current_Subprogram.
1159 Save_CS
: constant Entity_Id
:= Current_Subprogram
;
1163 Current_Subprogram
:= Ent
;
1165 -- Scan declarations
1167 Decl
:= First
(Declarations
(N
));
1168 while Present
(Decl
) loop
1175 Visit
(Handled_Statement_Sequence
(N
));
1177 -- Restore current subprogram setting
1179 Current_Subprogram
:= Save_CS
;
1182 -- Now at this level, return skipping the subprogram body
1183 -- descendants, since we already took care of them!
1187 -- If we have a body stub, visit the associated subunit, which
1188 -- is a semantic descendant of the stub.
1191 Visit
(Library_Unit
(N
));
1193 -- A declaration of a wrapper package indicates a subprogram
1194 -- instance for which there is no explicit body. Enter the
1195 -- subprogram instance in the table.
1197 when N_Package_Declaration
=>
1198 if Is_Wrapper_Package
(Defining_Entity
(N
)) then
1200 (Related_Instance
(Defining_Entity
(N
)), Empty
);
1203 -- Skip generic declarations
1205 when N_Generic_Declaration
=>
1208 -- Skip generic package body
1210 when N_Package_Body
=>
1211 if Present
(Corresponding_Spec
(N
))
1212 and then Ekind
(Corresponding_Spec
(N
)) = E_Generic_Package
1217 -- Pragmas and component declarations are ignored. Quantified
1218 -- expressions are expanded into explicit loops and the
1219 -- original epression must be ignored.
1221 when N_Component_Declaration
1223 | N_Quantified_Expression
1227 -- We want to skip the function spec for a generic function
1228 -- to avoid looking at any generic types that might be in
1231 when N_Function_Specification
=>
1232 if Is_Generic_Subprogram
(Unique_Defining_Entity
(N
)) then
1236 -- Otherwise record an uplevel reference in a local identifier
1239 if Nkind
(N
) in N_Has_Entity
1240 and then Present
(Entity
(N
))
1244 -- Only interested in entities declared within our nest
1246 if not Is_Library_Level_Entity
(Ent
)
1247 and then Scope_Within_Or_Same
(Scope
(Ent
), Subp
)
1249 -- Skip entities defined in inlined subprograms
1252 Chars
(Enclosing_Subprogram
(Ent
)) /= Name_uParent
1254 -- Constants and variables are potentially uplevel
1255 -- references to global declarations.
1258 (Ekind_In
(Ent
, E_Constant
,
1262 -- Formals are interesting, but not if being used
1263 -- as mere names of parameters for name notation
1269 (Nkind
(Parent
(N
)) = N_Parameter_Association
1270 and then Selector_Name
(Parent
(N
)) = N
))
1272 -- Types other than known Is_Static types are
1273 -- potentially interesting.
1276 (Is_Type
(Ent
) and then not Is_Static_Type
(Ent
)))
1278 -- Here we have a potentially interesting uplevel
1279 -- reference to examine.
1281 if Is_Type
(Ent
) then
1283 DT
: Boolean := False;
1286 Check_Static_Type
(Ent
, N
, DT
);
1291 Caller
:= Current_Subprogram
;
1292 Callee
:= Enclosing_Subprogram
(Ent
);
1295 and then (not Is_Static_Type
(Ent
)
1296 or else Needs_Fat_Pointer
(Ent
))
1298 Note_Uplevel_Ref
(Ent
, N
, Caller
, Callee
);
1300 -- Check the type of a formal parameter of the current
1301 -- subprogram, whose formal type may be an uplevel
1304 elsif Is_Formal
(Ent
)
1305 and then Scope
(Ent
) = Current_Subprogram
1308 DT
: Boolean := False;
1311 Check_Static_Type
(Etype
(Ent
), Empty
, DT
);
1318 -- Fall through to continue scanning children of this node
1323 -- Start of processing for Build_Tables
1326 -- Traverse the body to get subprograms, calls and uplevel references
1331 -- Now do the first transitive closure which determines which
1332 -- subprograms in the nest are actually reachable.
1334 Reachable_Closure
: declare
1338 Subps
.Table
(Subps_First
).Reachable
:= True;
1340 -- We use a simple minded algorithm as follows (obviously this can
1341 -- be done more efficiently, using one of the standard algorithms
1342 -- for efficient transitive closure computation, but this is simple
1343 -- and most likely fast enough that its speed does not matter).
1345 -- Repeatedly scan the list of calls. Any time we find a call from
1346 -- A to B, where A is reachable, but B is not, then B is reachable,
1347 -- and note that we have made a change by setting Modified True. We
1348 -- repeat this until we make a pass with no modifications.
1352 Inner
: for J
in Calls
.First
.. Calls
.Last
loop
1354 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1356 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1357 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1359 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1360 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1363 if SUBF
.Reachable
and then not SUBT
.Reachable
then
1364 SUBT
.Reachable
:= True;
1370 exit Outer
when not Modified
;
1372 end Reachable_Closure
;
1374 -- Remove calls from unreachable subprograms
1381 for J
in Calls
.First
.. Calls
.Last
loop
1383 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1385 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1386 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1388 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1389 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1392 if SUBF
.Reachable
then
1393 pragma Assert
(SUBT
.Reachable
);
1394 New_Index
:= New_Index
+ 1;
1395 Calls
.Table
(New_Index
) := Calls
.Table
(J
);
1400 Calls
.Set_Last
(New_Index
);
1403 -- Remove uplevel references from unreachable subprograms
1410 for J
in Urefs
.First
.. Urefs
.Last
loop
1412 URJ
: Uref_Entry
renames Urefs
.Table
(J
);
1414 SINF
: constant SI_Type
:= Subp_Index
(URJ
.Caller
);
1415 SINT
: constant SI_Type
:= Subp_Index
(URJ
.Callee
);
1417 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1418 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1423 -- Keep reachable reference
1425 if SUBF
.Reachable
then
1426 New_Index
:= New_Index
+ 1;
1427 Urefs
.Table
(New_Index
) := Urefs
.Table
(J
);
1429 -- And since we know we are keeping this one, this is a good
1430 -- place to fill in information for a good reference.
1432 -- Mark all enclosing subprograms need to declare AREC
1436 S
:= Enclosing_Subprogram
(S
);
1438 -- If we are at the top level, as can happen with
1439 -- references to formals in aspects of nested subprogram
1440 -- declarations, there are no further subprograms to mark
1441 -- as requiring activation records.
1446 SUBI
: Subp_Entry
renames Subps
.Table
(Subp_Index
(S
));
1448 SUBI
.Declares_AREC
:= True;
1450 -- If this entity was marked reachable because it is
1451 -- in a task or protected type, there may not appear
1452 -- to be any calls to it, which would normally adjust
1453 -- the levels of the parent subprograms. So we need to
1454 -- be sure that the uplevel reference of that entity
1455 -- takes into account possible calls.
1457 if In_Synchronized_Unit
(SUBF
.Ent
)
1458 and then SUBT
.Lev
< SUBI
.Uplevel_Ref
1460 SUBI
.Uplevel_Ref
:= SUBT
.Lev
;
1464 exit when S
= URJ
.Callee
;
1467 -- Add to list of uplevel referenced entities for Callee.
1468 -- We do not add types to this list, only actual references
1469 -- to objects that will be referenced uplevel, and we use
1470 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1471 -- duplicate entries in the list. Discriminants are also
1472 -- excluded, only the enclosing object can appear in the
1475 if not Is_Uplevel_Referenced_Entity
(URJ
.Ent
)
1476 and then Ekind
(URJ
.Ent
) /= E_Discriminant
1478 Set_Is_Uplevel_Referenced_Entity
(URJ
.Ent
);
1479 Append_New_Elmt
(URJ
.Ent
, SUBT
.Uents
);
1482 -- And set uplevel indication for caller
1484 if SUBT
.Lev
< SUBF
.Uplevel_Ref
then
1485 SUBF
.Uplevel_Ref
:= SUBT
.Lev
;
1491 Urefs
.Set_Last
(New_Index
);
1494 -- Remove unreachable subprograms from Subps table. Note that we do
1495 -- this after eliminating entries from the other two tables, since
1496 -- those elimination steps depend on referencing the Subps table.
1502 New_SI
:= Subps_First
- 1;
1503 for J
in Subps_First
.. Subps
.Last
loop
1505 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1510 -- Subprogram is reachable, copy and reset index
1512 if STJ
.Reachable
then
1513 New_SI
:= New_SI
+ 1;
1514 Subps
.Table
(New_SI
) := STJ
;
1515 Set_Subps_Index
(STJ
.Ent
, UI_From_Int
(New_SI
));
1517 -- Subprogram is not reachable
1520 -- Clear index, since no longer active
1522 Set_Subps_Index
(Subps
.Table
(J
).Ent
, Uint_0
);
1524 -- Output debug information if -gnatd.3 set
1526 if Debug_Flag_Dot_3
then
1527 Write_Str
("Eliminate ");
1528 Write_Name
(Chars
(Subps
.Table
(J
).Ent
));
1530 Write_Location
(Sloc
(Subps
.Table
(J
).Ent
));
1531 Write_Str
(" (not referenced)");
1535 -- Rewrite declaration, body, and corresponding freeze node
1536 -- to null statements.
1538 -- A subprogram instantiation does not have an explicit
1539 -- body. If unused, we could remove the corresponding
1540 -- wrapper package and its body (TBD).
1542 if Present
(STJ
.Bod
) then
1543 Spec
:= Corresponding_Spec
(STJ
.Bod
);
1545 if Present
(Spec
) then
1546 Decl
:= Parent
(Declaration_Node
(Spec
));
1547 Rewrite
(Decl
, Make_Null_Statement
(Sloc
(Decl
)));
1549 if Present
(Freeze_Node
(Spec
)) then
1550 Rewrite
(Freeze_Node
(Spec
),
1551 Make_Null_Statement
(Sloc
(Decl
)));
1555 Rewrite
(STJ
.Bod
, Make_Null_Statement
(Sloc
(STJ
.Bod
)));
1561 Subps
.Set_Last
(New_SI
);
1564 -- Now it is time for the second transitive closure, which follows calls
1565 -- and makes sure that A calls B, and B has uplevel references, then A
1566 -- is also marked as having uplevel references.
1568 Closure_Uplevel
: declare
1572 -- We use a simple minded algorithm as follows (obviously this can
1573 -- be done more efficiently, using one of the standard algorithms
1574 -- for efficient transitive closure computation, but this is simple
1575 -- and most likely fast enough that its speed does not matter).
1577 -- Repeatedly scan the list of calls. Any time we find a call from
1578 -- A to B, where B has uplevel references, make sure that A is marked
1579 -- as having at least the same level of uplevel referencing.
1583 Inner2
: for J
in Calls
.First
.. Calls
.Last
loop
1585 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1586 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1587 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1588 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1589 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1591 if SUBT
.Lev
> SUBT
.Uplevel_Ref
1592 and then SUBF
.Uplevel_Ref
> SUBT
.Uplevel_Ref
1594 SUBF
.Uplevel_Ref
:= SUBT
.Uplevel_Ref
;
1600 exit Outer2
when not Modified
;
1602 end Closure_Uplevel
;
1604 -- We have one more step before the tables are complete. An uplevel
1605 -- call from subprogram A to subprogram B where subprogram B has uplevel
1606 -- references is in effect an uplevel reference, and must arrange for
1607 -- the proper activation link to be passed.
1609 for J
in Calls
.First
.. Calls
.Last
loop
1611 CTJ
: Call_Entry
renames Calls
.Table
(J
);
1613 SINF
: constant SI_Type
:= Subp_Index
(CTJ
.Caller
);
1614 SINT
: constant SI_Type
:= Subp_Index
(CTJ
.Callee
);
1616 SUBF
: Subp_Entry
renames Subps
.Table
(SINF
);
1617 SUBT
: Subp_Entry
renames Subps
.Table
(SINT
);
1622 -- If callee has uplevel references
1624 if SUBT
.Uplevel_Ref
< SUBT
.Lev
1626 -- And this is an uplevel call
1628 and then SUBT
.Lev
< SUBF
.Lev
1630 -- We need to arrange for finding the uplink
1634 A
:= Enclosing_Subprogram
(A
);
1635 Subps
.Table
(Subp_Index
(A
)).Declares_AREC
:= True;
1636 exit when A
= CTJ
.Callee
;
1638 -- In any case exit when we get to the outer level. This
1639 -- happens in some odd cases with generics (in particular
1640 -- sem_ch3.adb does not compile without this kludge ???).
1648 -- The tables are now complete, so we can record the last index in the
1649 -- Subps table for later reference in Cprint.
1651 Subps
.Table
(Subps_First
).Last
:= Subps
.Last
;
1653 -- Next step, create the entities for code we will insert. We do this
1654 -- at the start so that all the entities are defined, regardless of the
1655 -- order in which we do the code insertions.
1657 Create_Entities
: for J
in Subps_First
.. Subps
.Last
loop
1659 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1660 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1663 -- First we create the ARECnF entity for the additional formal for
1664 -- all subprograms which need an activation record passed.
1666 if STJ
.Uplevel_Ref
< STJ
.Lev
then
1668 Make_Defining_Identifier
(Loc
, Chars
=> AREC_Name
(J
, "F"));
1671 -- Define the AREC entities for the activation record if needed
1673 if STJ
.Declares_AREC
then
1675 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, ""));
1677 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "T"));
1679 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "PT"));
1681 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "P"));
1683 -- Define uplink component entity if inner nesting case
1685 if Present
(STJ
.ARECnF
) then
1687 Make_Defining_Identifier
(Loc
, AREC_Name
(J
, "U"));
1691 end loop Create_Entities
;
1693 -- Loop through subprograms
1696 Addr
: Entity_Id
:= Empty
;
1699 for J
in Subps_First
.. Subps
.Last
loop
1701 STJ
: Subp_Entry
renames Subps
.Table
(J
);
1704 -- First add the extra formal if needed. This applies to all
1705 -- nested subprograms that require an activation record to be
1706 -- passed, as indicated by ARECnF being defined.
1708 if Present
(STJ
.ARECnF
) then
1710 -- Here we need the extra formal. We do the expansion and
1711 -- analysis of this manually, since it is fairly simple,
1712 -- and it is not obvious how we can get what we want if we
1713 -- try to use the normal Analyze circuit.
1715 Add_Extra_Formal
: declare
1716 Encl
: constant SI_Type
:= Enclosing_Subp
(J
);
1717 STJE
: Subp_Entry
renames Subps
.Table
(Encl
);
1718 -- Index and Subp_Entry for enclosing routine
1720 Form
: constant Entity_Id
:= STJ
.ARECnF
;
1721 -- The formal to be added. Note that n here is one less
1722 -- than the level of the subprogram itself (STJ.Ent).
1724 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
);
1725 -- S is an N_Function/Procedure_Specification node, and F
1726 -- is the new entity to add to this subprogramn spec as
1727 -- the last Extra_Formal.
1729 ----------------------
1730 -- Add_Form_To_Spec --
1731 ----------------------
1733 procedure Add_Form_To_Spec
(F
: Entity_Id
; S
: Node_Id
) is
1734 Sub
: constant Entity_Id
:= Defining_Entity
(S
);
1738 -- Case of at least one Extra_Formal is present, set
1739 -- ARECnF as the new last entry in the list.
1741 if Present
(Extra_Formals
(Sub
)) then
1742 Ent
:= Extra_Formals
(Sub
);
1743 while Present
(Extra_Formal
(Ent
)) loop
1744 Ent
:= Extra_Formal
(Ent
);
1747 Set_Extra_Formal
(Ent
, F
);
1749 -- No Extra formals present
1752 Set_Extra_Formals
(Sub
, F
);
1753 Ent
:= Last_Formal
(Sub
);
1755 if Present
(Ent
) then
1756 Set_Extra_Formal
(Ent
, F
);
1759 end Add_Form_To_Spec
;
1761 -- Start of processing for Add_Extra_Formal
1764 -- Decorate the new formal entity
1766 Set_Scope
(Form
, STJ
.Ent
);
1767 Set_Ekind
(Form
, E_In_Parameter
);
1768 Set_Etype
(Form
, STJE
.ARECnPT
);
1769 Set_Mechanism
(Form
, By_Copy
);
1770 Set_Never_Set_In_Source
(Form
, True);
1771 Set_Analyzed
(Form
, True);
1772 Set_Comes_From_Source
(Form
, False);
1773 Set_Is_Activation_Record
(Form
, True);
1775 -- Case of only body present
1777 if Acts_As_Spec
(STJ
.Bod
) then
1778 Add_Form_To_Spec
(Form
, Specification
(STJ
.Bod
));
1780 -- Case of separate spec
1783 Add_Form_To_Spec
(Form
, Parent
(STJ
.Ent
));
1785 end Add_Extra_Formal
;
1788 -- Processing for subprograms that declare an activation record
1790 if Present
(STJ
.ARECn
) then
1792 -- Local declarations for one such subprogram
1795 Loc
: constant Source_Ptr
:= Sloc
(STJ
.Bod
);
1797 Decls
: constant List_Id
:= New_List
;
1798 -- List of new declarations we create
1803 Decl_Assign
: Node_Id
;
1804 -- Assigment to set uplink, Empty if none
1806 Decl_ARECnT
: Node_Id
;
1807 Decl_ARECnPT
: Node_Id
;
1808 Decl_ARECn
: Node_Id
;
1809 Decl_ARECnP
: Node_Id
;
1810 -- Declaration nodes for the AREC entities we build
1813 -- Build list of component declarations for ARECnT and
1814 -- load System.Address.
1816 Clist
:= Empty_List
;
1819 Addr
:= RTE
(RE_Address
);
1822 -- If we are in a subprogram that has a static link that
1823 -- is passed in (as indicated by ARECnF being defined),
1824 -- then include ARECnU : ARECmPT where ARECmPT comes from
1825 -- the level one higher than the current level, and the
1826 -- entity ARECnPT comes from the enclosing subprogram.
1828 if Present
(STJ
.ARECnF
) then
1831 renames Subps
.Table
(Enclosing_Subp
(J
));
1834 Make_Component_Declaration
(Loc
,
1835 Defining_Identifier
=> STJ
.ARECnU
,
1836 Component_Definition
=>
1837 Make_Component_Definition
(Loc
,
1838 Subtype_Indication
=>
1839 New_Occurrence_Of
(STJE
.ARECnPT
, Loc
))));
1843 -- Add components for uplevel referenced entities
1845 if Present
(STJ
.Uents
) then
1852 -- 1's origin of index in list of elements. This is
1853 -- used to uniquify names if needed in Upref_Name.
1856 Elmt
:= First_Elmt
(STJ
.Uents
);
1858 while Present
(Elmt
) loop
1859 Uent
:= Node
(Elmt
);
1863 Make_Defining_Identifier
(Loc
,
1864 Chars
=> Upref_Name
(Uent
, Indx
, Clist
));
1866 Set_Activation_Record_Component
1869 if Needs_Fat_Pointer
(Uent
) then
1871 -- Build corresponding access type
1874 Build_Access_Type_Decl
1875 (Etype
(Uent
), STJ
.Ent
);
1876 Append_To
(Decls
, Ptr_Decl
);
1878 -- And use its type in the corresponding
1882 Make_Component_Declaration
(Loc
,
1883 Defining_Identifier
=> Comp
,
1884 Component_Definition
=>
1885 Make_Component_Definition
(Loc
,
1886 Subtype_Indication
=>
1888 (Defining_Identifier
(Ptr_Decl
),
1892 Make_Component_Declaration
(Loc
,
1893 Defining_Identifier
=> Comp
,
1894 Component_Definition
=>
1895 Make_Component_Definition
(Loc
,
1896 Subtype_Indication
=>
1897 New_Occurrence_Of
(Addr
, Loc
))));
1904 -- Now we can insert the AREC declarations into the body
1905 -- type ARECnT is record .. end record;
1906 -- pragma Suppress_Initialization (ARECnT);
1908 -- Note that we need to set the Suppress_Initialization
1909 -- flag after Decl_ARECnT has been analyzed.
1912 Make_Full_Type_Declaration
(Loc
,
1913 Defining_Identifier
=> STJ
.ARECnT
,
1915 Make_Record_Definition
(Loc
,
1917 Make_Component_List
(Loc
,
1918 Component_Items
=> Clist
)));
1919 Append_To
(Decls
, Decl_ARECnT
);
1921 -- type ARECnPT is access all ARECnT;
1924 Make_Full_Type_Declaration
(Loc
,
1925 Defining_Identifier
=> STJ
.ARECnPT
,
1927 Make_Access_To_Object_Definition
(Loc
,
1928 All_Present
=> True,
1929 Subtype_Indication
=>
1930 New_Occurrence_Of
(STJ
.ARECnT
, Loc
)));
1931 Append_To
(Decls
, Decl_ARECnPT
);
1933 -- ARECn : aliased ARECnT;
1936 Make_Object_Declaration
(Loc
,
1937 Defining_Identifier
=> STJ
.ARECn
,
1938 Aliased_Present
=> True,
1939 Object_Definition
=>
1940 New_Occurrence_Of
(STJ
.ARECnT
, Loc
));
1941 Append_To
(Decls
, Decl_ARECn
);
1943 -- ARECnP : constant ARECnPT := ARECn'Access;
1946 Make_Object_Declaration
(Loc
,
1947 Defining_Identifier
=> STJ
.ARECnP
,
1948 Constant_Present
=> True,
1949 Object_Definition
=>
1950 New_Occurrence_Of
(STJ
.ARECnPT
, Loc
),
1952 Make_Attribute_Reference
(Loc
,
1954 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1955 Attribute_Name
=> Name_Access
));
1956 Append_To
(Decls
, Decl_ARECnP
);
1958 -- If we are in a subprogram that has a static link that
1959 -- is passed in (as indicated by ARECnF being defined),
1960 -- then generate ARECn.ARECmU := ARECmF where m is
1961 -- one less than the current level to set the uplink.
1963 if Present
(STJ
.ARECnF
) then
1965 Make_Assignment_Statement
(Loc
,
1967 Make_Selected_Component
(Loc
,
1969 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
1971 New_Occurrence_Of
(STJ
.ARECnU
, Loc
)),
1973 New_Occurrence_Of
(STJ
.ARECnF
, Loc
));
1974 Append_To
(Decls
, Decl_Assign
);
1977 Decl_Assign
:= Empty
;
1980 if No
(Declarations
(STJ
.Bod
)) then
1981 Set_Declarations
(STJ
.Bod
, Decls
);
1983 Prepend_List_To
(Declarations
(STJ
.Bod
), Decls
);
1986 -- Analyze the newly inserted declarations. Note that we
1987 -- do not need to establish the whole scope stack, since
1988 -- we have already set all entity fields (so there will
1989 -- be no searching of upper scopes to resolve names). But
1990 -- we do set the scope of the current subprogram, so that
1991 -- newly created entities go in the right entity chain.
1993 -- We analyze with all checks suppressed (since we do
1994 -- not expect any exceptions).
1996 Push_Scope
(STJ
.Ent
);
1997 Analyze
(Decl_ARECnT
, Suppress
=> All_Checks
);
1999 -- Note that we need to call Set_Suppress_Initialization
2000 -- after Decl_ARECnT has been analyzed, but before
2001 -- analyzing Decl_ARECnP so that the flag is properly
2002 -- taking into account.
2004 Set_Suppress_Initialization
(STJ
.ARECnT
);
2006 Analyze
(Decl_ARECnPT
, Suppress
=> All_Checks
);
2007 Analyze
(Decl_ARECn
, Suppress
=> All_Checks
);
2008 Analyze
(Decl_ARECnP
, Suppress
=> All_Checks
);
2010 if Present
(Decl_Assign
) then
2011 Analyze
(Decl_Assign
, Suppress
=> All_Checks
);
2016 -- Next step, for each uplevel referenced entity, add
2017 -- assignment operations to set the component in the
2018 -- activation record.
2020 if Present
(STJ
.Uents
) then
2025 Elmt
:= First_Elmt
(STJ
.Uents
);
2026 while Present
(Elmt
) loop
2028 Ent
: constant Entity_Id
:= Node
(Elmt
);
2029 Loc
: constant Source_Ptr
:= Sloc
(Ent
);
2030 Dec
: constant Node_Id
:=
2031 Declaration_Node
(Ent
);
2040 -- For parameters, we insert the assignment
2041 -- right after the declaration of ARECnP.
2042 -- For all other entities, we insert the
2043 -- assignment immediately after the
2044 -- declaration of the entity or after the
2045 -- freeze node if present.
2047 -- Note: we don't need to mark the entity
2048 -- as being aliased, because the address
2049 -- attribute will mark it as Address_Taken,
2050 -- and that is good enough.
2052 if Is_Formal
(Ent
) then
2055 elsif Has_Delayed_Freeze
(Ent
) then
2056 Ins
:= Freeze_Node
(Ent
);
2062 -- Build and insert the assignment:
2063 -- ARECn.nam := nam'Address
2064 -- or else 'Access for unconstrained array
2066 if Needs_Fat_Pointer
(Ent
) then
2067 Attr
:= Name_Access
;
2069 Attr
:= Name_Address
;
2073 Make_Attribute_Reference
(Loc
,
2075 New_Occurrence_Of
(Ent
, Loc
),
2076 Attribute_Name
=> Attr
);
2078 -- If the entity is an unconstrained formal
2079 -- we wrap the attribute reference in an
2080 -- unchecked conversion to the type of the
2081 -- activation record component, to prevent
2082 -- spurious subtype conformance errors within
2086 and then not Is_Constrained
(Etype
(Ent
))
2088 -- Find target component and its type
2090 Comp
:= First_Component
(STJ
.ARECnT
);
2091 while Chars
(Comp
) /= Chars
(Ent
) loop
2092 Comp
:= Next_Component
(Comp
);
2096 Unchecked_Convert_To
(Etype
(Comp
), Rhs
);
2100 Make_Assignment_Statement
(Loc
,
2102 Make_Selected_Component
(Loc
,
2104 New_Occurrence_Of
(STJ
.ARECn
, Loc
),
2107 (Activation_Record_Component
2112 -- If we have a loop parameter, we have
2113 -- to insert before the first statement
2114 -- of the loop. Ins points to the
2115 -- N_Loop_Parameter_Specification or to
2116 -- an N_Iterator_Specification.
2119 (Ins
, N_Iterator_Specification
,
2120 N_Loop_Parameter_Specification
)
2122 -- Quantified expression are rewritten as
2123 -- loops during expansion.
2125 if Nkind
(Parent
(Ins
)) =
2126 N_Quantified_Expression
2134 (Parent
(Parent
(Ins
))));
2135 Insert_Before
(Ins
, Asn
);
2139 Insert_After
(Ins
, Asn
);
2142 -- Analyze the assignment statement. We do
2143 -- not need to establish the relevant scope
2144 -- stack entries here, because we have
2145 -- already set the correct entity references,
2146 -- so no name resolution is required, and no
2147 -- new entities are created, so we don't even
2148 -- need to set the current scope.
2150 -- We analyze with all checks suppressed
2151 -- (since we do not expect any exceptions).
2153 Analyze
(Asn
, Suppress
=> All_Checks
);
2166 -- Next step, process uplevel references. This has to be done in a
2167 -- separate pass, after completing the processing in Sub_Loop because we
2168 -- need all the AREC declarations generated, inserted, and analyzed so
2169 -- that the uplevel references can be successfully analyzed.
2171 Uplev_Refs
: for J
in Urefs
.First
.. Urefs
.Last
loop
2173 UPJ
: Uref_Entry
renames Urefs
.Table
(J
);
2176 -- Ignore type references, these are implicit references that do
2177 -- not need rewriting (e.g. the appearence in a conversion).
2178 -- Also ignore if no reference was specified or if the rewriting
2179 -- has already been done (this can happen if the N_Identifier
2180 -- occurs more than one time in the tree).
2183 or else not Is_Entity_Name
(UPJ
.Ref
)
2184 or else not Present
(Entity
(UPJ
.Ref
))
2189 -- Rewrite one reference
2191 Rewrite_One_Ref
: declare
2192 Loc
: constant Source_Ptr
:= Sloc
(UPJ
.Ref
);
2193 -- Source location for the reference
2195 Typ
: constant Entity_Id
:= Etype
(UPJ
.Ent
);
2196 -- The type of the referenced entity
2199 -- The actual subtype of the reference
2201 RS_Caller
: constant SI_Type
:= Subp_Index
(UPJ
.Caller
);
2202 -- Subp_Index for caller containing reference
2204 STJR
: Subp_Entry
renames Subps
.Table
(RS_Caller
);
2205 -- Subp_Entry for subprogram containing reference
2207 RS_Callee
: constant SI_Type
:= Subp_Index
(UPJ
.Callee
);
2208 -- Subp_Index for subprogram containing referenced entity
2210 STJE
: Subp_Entry
renames Subps
.Table
(RS_Callee
);
2211 -- Subp_Entry for subprogram containing referenced entity
2218 Atyp
:= Etype
(UPJ
.Ref
);
2220 if Ekind
(Atyp
) /= E_Record_Subtype
then
2221 Atyp
:= Get_Actual_Subtype
(UPJ
.Ref
);
2224 -- Ignore if no ARECnF entity for enclosing subprogram which
2225 -- probably happens as a result of not properly treating
2226 -- instance bodies. To be examined ???
2228 -- If this test is omitted, then the compilation of freeze.adb
2229 -- and inline.adb fail in unnesting mode.
2231 if No
(STJR
.ARECnF
) then
2235 -- If this is a reference to a global constant, use its value
2236 -- rather than create a reference. It is more efficient and
2237 -- furthermore indispensable if the context requires a
2238 -- constant, such as a branch of a case statement.
2240 if Ekind
(UPJ
.Ent
) = E_Constant
2241 and then Is_True_Constant
(UPJ
.Ent
)
2242 and then Present
(Constant_Value
(UPJ
.Ent
))
2243 and then Is_Static_Expression
(Constant_Value
(UPJ
.Ent
))
2245 Rewrite
(UPJ
.Ref
, New_Copy_Tree
(Constant_Value
(UPJ
.Ent
)));
2249 -- Push the current scope, so that the pointer type Tnn, and
2250 -- any subsidiary entities resulting from the analysis of the
2251 -- rewritten reference, go in the right entity chain.
2253 Push_Scope
(STJR
.Ent
);
2255 -- Now we need to rewrite the reference. We have a reference
2256 -- from level STJR.Lev to level STJE.Lev. The general form of
2257 -- the rewritten reference for entity X is:
2259 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2261 -- where a,b,c,d .. m =
2262 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2264 pragma Assert
(STJR
.Lev
> STJE
.Lev
);
2266 -- Compute the prefix of X. Here are examples to make things
2267 -- clear (with parens to show groupings, the prefix is
2268 -- everything except the .X at the end).
2270 -- level 2 to level 1
2274 -- level 3 to level 1
2276 -- (AREC2F.AREC1U).X
2278 -- level 4 to level 1
2280 -- ((AREC3F.AREC2U).AREC1U).X
2282 -- level 6 to level 2
2284 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2286 -- In the above, ARECnF and ARECnU are pointers, so there are
2287 -- explicit dereferences required for these occurrences.
2290 Make_Explicit_Dereference
(Loc
,
2291 Prefix
=> New_Occurrence_Of
(STJR
.ARECnF
, Loc
));
2293 for L
in STJE
.Lev
.. STJR
.Lev
- 2 loop
2294 SI
:= Enclosing_Subp
(SI
);
2296 Make_Explicit_Dereference
(Loc
,
2298 Make_Selected_Component
(Loc
,
2301 New_Occurrence_Of
(Subps
.Table
(SI
).ARECnU
, Loc
)));
2304 -- Get activation record component (must exist)
2306 Comp
:= Activation_Record_Component
(UPJ
.Ent
);
2307 pragma Assert
(Present
(Comp
));
2309 -- Do the replacement. If the component type is an access type,
2310 -- this is an uplevel reference for an entity that requires a
2311 -- fat pointer, so dereference the component.
2313 if Is_Access_Type
(Etype
(Comp
)) then
2315 Make_Explicit_Dereference
(Loc
,
2317 Make_Selected_Component
(Loc
,
2320 New_Occurrence_Of
(Comp
, Loc
))));
2324 Make_Attribute_Reference
(Loc
,
2325 Prefix
=> New_Occurrence_Of
(Atyp
, Loc
),
2326 Attribute_Name
=> Name_Deref
,
2327 Expressions
=> New_List
(
2328 Make_Selected_Component
(Loc
,
2331 New_Occurrence_Of
(Comp
, Loc
)))));
2334 -- Analyze and resolve the new expression. We do not need to
2335 -- establish the relevant scope stack entries here, because we
2336 -- have already set all the correct entity references, so no
2337 -- name resolution is needed. We have already set the current
2338 -- scope, so that any new entities created will be in the right
2341 -- We analyze with all checks suppressed (since we do not
2342 -- expect any exceptions)
2344 Analyze_And_Resolve
(UPJ
.Ref
, Typ
, Suppress
=> All_Checks
);
2346 end Rewrite_One_Ref
;
2351 end loop Uplev_Refs
;
2353 -- Finally, loop through all calls adding extra actual for the
2354 -- activation record where it is required.
2356 Adjust_Calls
: for J
in Calls
.First
.. Calls
.Last
loop
2358 -- Process a single call, we are only interested in a call to a
2359 -- subprogram that actually needs a pointer to an activation record,
2360 -- as indicated by the ARECnF entity being set. This excludes the
2361 -- top level subprogram, and any subprogram not having uplevel refs.
2363 Adjust_One_Call
: declare
2364 CTJ
: Call_Entry
renames Calls
.Table
(J
);
2365 STF
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Caller
));
2366 STT
: Subp_Entry
renames Subps
.Table
(Subp_Index
(CTJ
.Callee
));
2368 Loc
: constant Source_Ptr
:= Sloc
(CTJ
.N
);
2376 if Present
(STT
.ARECnF
)
2377 and then Nkind
(CTJ
.N
) in N_Subprogram_Call
2379 -- CTJ.N is a call to a subprogram which may require a pointer
2380 -- to an activation record. The subprogram containing the call
2381 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2382 -- have a call from level STF.Lev to level STT.Lev.
2384 -- There are three possibilities:
2386 -- For a call to the same level, we just pass the activation
2387 -- record passed to the calling subprogram.
2389 if STF
.Lev
= STT
.Lev
then
2390 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
2392 -- For a call that goes down a level, we pass a pointer to the
2393 -- activation record constructed within the caller (which may
2394 -- be the outer-level subprogram, but also may be a more deeply
2397 elsif STT
.Lev
= STF
.Lev
+ 1 then
2398 Extra
:= New_Occurrence_Of
(STF
.ARECnP
, Loc
);
2400 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2401 -- since it is not possible to do a downcall of more than
2404 -- For a call from level STF.Lev to level STT.Lev, we
2405 -- have to find the activation record needed by the
2406 -- callee. This is as follows:
2408 -- ARECaF.ARECbU.ARECcU....ARECmU
2410 -- where a,b,c .. m =
2411 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2414 pragma Assert
(STT
.Lev
< STF
.Lev
);
2416 Extra
:= New_Occurrence_Of
(STF
.ARECnF
, Loc
);
2417 SubX
:= Subp_Index
(CTJ
.Caller
);
2418 for K
in reverse STT
.Lev
.. STF
.Lev
- 1 loop
2419 SubX
:= Enclosing_Subp
(SubX
);
2421 Make_Selected_Component
(Loc
,
2425 (Subps
.Table
(SubX
).ARECnU
, Loc
));
2429 -- Extra is the additional parameter to be added. Build a
2430 -- parameter association that we can append to the actuals.
2433 Make_Parameter_Association
(Loc
,
2435 New_Occurrence_Of
(STT
.ARECnF
, Loc
),
2436 Explicit_Actual_Parameter
=> Extra
);
2438 if No
(Parameter_Associations
(CTJ
.N
)) then
2439 Set_Parameter_Associations
(CTJ
.N
, Empty_List
);
2442 Append
(ExtraP
, Parameter_Associations
(CTJ
.N
));
2444 -- We need to deal with the actual parameter chain as well. The
2445 -- newly added parameter is always the last actual.
2447 Act
:= First_Named_Actual
(CTJ
.N
);
2450 Set_First_Named_Actual
(CTJ
.N
, Extra
);
2452 -- If call has been relocated (as with an expression in
2453 -- an aggregate), set First_Named pointer in original node
2454 -- as well, because that's the parent of the parameter list.
2456 Set_First_Named_Actual
2457 (Parent
(List_Containing
(ExtraP
)), Extra
);
2459 -- Here we must follow the chain and append the new entry
2468 PAN
:= Parent
(Act
);
2469 pragma Assert
(Nkind
(PAN
) = N_Parameter_Association
);
2470 NNA
:= Next_Named_Actual
(PAN
);
2473 Set_Next_Named_Actual
(PAN
, Extra
);
2482 -- Analyze and resolve the new actual. We do not need to
2483 -- establish the relevant scope stack entries here, because
2484 -- we have already set all the correct entity references, so
2485 -- no name resolution is needed.
2487 -- We analyze with all checks suppressed (since we do not
2488 -- expect any exceptions, and also we temporarily turn off
2489 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2490 -- references (not needed at this stage, and in fact causes
2491 -- a bit of recursive chaos).
2493 Opt
.Unnest_Subprogram_Mode
:= False;
2495 (Extra
, Etype
(STT
.ARECnF
), Suppress
=> All_Checks
);
2496 Opt
.Unnest_Subprogram_Mode
:= True;
2498 end Adjust_One_Call
;
2499 end loop Adjust_Calls
;
2502 end Unnest_Subprogram
;
2504 ------------------------
2505 -- Unnest_Subprograms --
2506 ------------------------
2508 procedure Unnest_Subprograms
(N
: Node_Id
) is
2509 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
;
2510 -- Tree visitor that search for outer level procedures with nested
2511 -- subprograms and invokes Unnest_Subprogram()
2517 procedure Do_Search
is new Traverse_Proc
(Search_Subprograms
);
2518 -- Subtree visitor instantiation
2520 ------------------------
2521 -- Search_Subprograms --
2522 ------------------------
2524 function Search_Subprograms
(N
: Node_Id
) return Traverse_Result
is
2526 if Nkind_In
(N
, N_Subprogram_Body
, N_Subprogram_Body_Stub
) then
2528 Spec_Id
: constant Entity_Id
:= Unique_Defining_Entity
(N
);
2531 -- We are only interested in subprograms (not generic
2532 -- subprograms), that have nested subprograms.
2534 if Is_Subprogram
(Spec_Id
)
2535 and then Has_Nested_Subprogram
(Spec_Id
)
2536 and then Is_Library_Level_Entity
(Spec_Id
)
2538 Unnest_Subprogram
(Spec_Id
, N
);
2542 -- The proper body of a stub may contain nested subprograms, and
2543 -- therefore must be visited explicitly. Nested stubs are examined
2544 -- recursively in Visit_Node.
2546 elsif Nkind
(N
) in N_Body_Stub
then
2547 Do_Search
(Library_Unit
(N
));
2549 -- Skip generic packages
2551 elsif Nkind
(N
) = N_Package_Body
2552 and then Ekind
(Corresponding_Spec
(N
)) = E_Generic_Package
2558 end Search_Subprograms
;
2561 Subp_Body
: Node_Id
;
2563 -- Start of processing for Unnest_Subprograms
2566 if not Opt
.Unnest_Subprogram_Mode
or not Opt
.Expander_Active
then
2570 -- A specification will contain bodies if it contains instantiations so
2571 -- examine package or subprogram declaration of the main unit, when it
2574 if Nkind
(Unit
(N
)) = N_Package_Body
2575 or else (Nkind
(Unit
(N
)) = N_Subprogram_Body
2576 and then not Acts_As_Spec
(N
))
2578 Do_Search
(Library_Unit
(N
));
2583 -- Unnest any subprograms passed on the list of inlined subprograms
2585 Subp
:= First_Inlined_Subprogram
(N
);
2587 while Present
(Subp
) loop
2588 Subp_Body
:= Parent
(Declaration_Node
(Subp
));
2590 if Nkind
(Subp_Body
) = N_Subprogram_Declaration
2591 and then Present
(Corresponding_Body
(Subp_Body
))
2593 Subp_Body
:= Parent
(Declaration_Node
2594 (Corresponding_Body
(Subp_Body
)));
2595 Unnest_Subprogram
(Subp
, Subp_Body
, For_Inline
=> True);
2598 Next_Inlined_Subprogram
(Subp
);
2600 end Unnest_Subprograms
;