1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 2000-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- Extensive contributions were provided by Ada Core Technologies Inc. --
27 ------------------------------------------------------------------------------
29 with Atree
; use Atree
;
30 with Einfo
; use Einfo
;
32 with Nlists
; use Nlists
;
33 with Sem_Util
; use Sem_Util
;
34 with Sinfo
; use Sinfo
;
35 with Types
; use Types
;
41 -- The Name_Set type is used to store the temporary mark bits
42 -- used by the garbage collection of entities. Using a separate
43 -- array prevents using up any valuable per-node space and possibly
44 -- results in better locality and cache usage.
46 type Name_Set
is array (Node_Id
range <>) of Boolean;
47 pragma Pack
(Name_Set
);
49 function Marked
(Marks
: Name_Set
; Name
: Node_Id
) return Boolean;
50 pragma Inline
(Marked
);
53 (Marks
: in out Name_Set
;
55 Mark
: Boolean := True);
56 pragma Inline
(Set_Marked
);
60 -- The problem of finding live entities is solved in two steps:
62 procedure Mark
(Root
: Node_Id
; Marks
: out Name_Set
);
63 -- Mark all live entities in Root as Marked.
65 procedure Sweep
(Root
: Node_Id
; Marks
: Name_Set
);
66 -- For all unmarked entities in Root set Is_Eliminated to true
68 -- The Mark phase is split into two phases:
70 procedure Init_Marked
(Root
: Node_Id
; Marks
: out Name_Set
);
71 -- For all subprograms, reset Is_Public flag if a pragma Eliminate
72 -- applies to the entity, and set the Marked flag to Is_Public
74 procedure Trace_Marked
(Root
: Node_Id
; Marks
: in out Name_Set
);
75 -- Traverse the tree skipping any unmarked subprogram bodies.
76 -- All visited entities are marked, as well as entities denoted
77 -- by a visited identifier or operator. When an entity is first
78 -- marked it is traced as well.
82 function Body_Of
(E
: Entity_Id
) return Node_Id
;
83 -- Returns subprogram body corresponding to entity E
85 function Spec_Of
(N
: Node_Id
) return Entity_Id
;
86 -- Given a subprogram body N, return defining identifier of its declaration
88 -- ??? the body of this package contains no comments at all, this
95 function Body_Of
(E
: Entity_Id
) return Node_Id
is
96 Decl
: Node_Id
:= Unit_Declaration_Node
(E
);
98 Kind
: Node_Kind
:= Nkind
(Decl
);
101 if Kind
= N_Subprogram_Body
then
104 elsif Kind
/= N_Subprogram_Declaration
105 and Kind
/= N_Subprogram_Body_Stub
110 Result
:= Corresponding_Body
(Decl
);
112 if Result
/= Empty
then
113 Result
:= Unit_Declaration_Node
(Result
);
120 ------------------------------
121 -- Collect_Garbage_Entities --
122 ------------------------------
124 procedure Collect_Garbage_Entities
is
125 Root
: constant Node_Id
:= Cunit
(Main_Unit
);
126 Marks
: Name_Set
(0 .. Last_Node_Id
);
131 end Collect_Garbage_Entities
;
137 procedure Init_Marked
(Root
: Node_Id
; Marks
: out Name_Set
) is
139 function Process
(N
: Node_Id
) return Traverse_Result
;
140 procedure Traverse
is new Traverse_Proc
(Process
);
142 function Process
(N
: Node_Id
) return Traverse_Result
is
145 when N_Entity
'Range =>
146 if Is_Eliminated
(N
) then
147 Set_Is_Public
(N
, False);
150 Set_Marked
(Marks
, N
, Is_Public
(N
));
152 when N_Subprogram_Body
=>
153 Traverse
(Spec_Of
(N
));
155 when N_Package_Body_Stub
=>
156 if Present
(Library_Unit
(N
)) then
157 Traverse
(Proper_Body
(Unit
(Library_Unit
(N
))));
160 when N_Package_Body
=>
162 Elmt
: Node_Id
:= First
(Declarations
(N
));
164 while Present
(Elmt
) loop
177 -- Start of processing for Init_Marked
180 Marks
:= (others => False);
188 procedure Mark
(Root
: Node_Id
; Marks
: out Name_Set
) is
190 Init_Marked
(Root
, Marks
);
191 Trace_Marked
(Root
, Marks
);
198 function Marked
(Marks
: Name_Set
; Name
: Node_Id
) return Boolean is
208 (Marks
: in out Name_Set
;
210 Mark
: Boolean := True)
213 Marks
(Name
) := Mark
;
220 function Spec_Of
(N
: Node_Id
) return Entity_Id
is
222 if Acts_As_Spec
(N
) then
223 return Defining_Entity
(N
);
225 return Corresponding_Spec
(N
);
233 procedure Sweep
(Root
: Node_Id
; Marks
: Name_Set
) is
235 function Process
(N
: Node_Id
) return Traverse_Result
;
236 procedure Traverse
is new Traverse_Proc
(Process
);
238 function Process
(N
: Node_Id
) return Traverse_Result
is
241 when N_Entity
'Range =>
242 Set_Is_Eliminated
(N
, not Marked
(Marks
, N
));
244 when N_Subprogram_Body
=>
245 Traverse
(Spec_Of
(N
));
247 when N_Package_Body_Stub
=>
248 if Present
(Library_Unit
(N
)) then
249 Traverse
(Proper_Body
(Unit
(Library_Unit
(N
))));
252 when N_Package_Body
=>
254 Elmt
: Node_Id
:= First
(Declarations
(N
));
256 while Present
(Elmt
) loop
276 procedure Trace_Marked
(Root
: Node_Id
; Marks
: in out Name_Set
) is
278 function Process
(N
: Node_Id
) return Traverse_Result
;
279 procedure Process
(N
: Node_Id
);
280 procedure Traverse
is new Traverse_Proc
(Process
);
282 procedure Process
(N
: Node_Id
) is
283 Result
: Traverse_Result
;
285 Result
:= Process
(N
);
288 function Process
(N
: Node_Id
) return Traverse_Result
is
289 Result
: Traverse_Result
:= OK
;
295 when N_Pragma | N_Generic_Declaration
'Range |
296 N_Subprogram_Declaration | N_Subprogram_Body_Stub
=>
299 when N_Subprogram_Body
=>
300 if not Marked
(Marks
, Spec_Of
(N
)) then
304 when N_Package_Body_Stub
=>
305 if Present
(Library_Unit
(N
)) then
306 Traverse
(Proper_Body
(Unit
(Library_Unit
(N
))));
309 when N_Identifier | N_Operator_Symbol | N_Expanded_Name
=>
312 if E
/= Empty
and then not Marked
(Marks
, E
) then
315 if Is_Subprogram
(E
) then
324 when N_Entity
'Range =>
325 if (Ekind
(N
) = E_Component
) and then not Marked
(Marks
, N
) then
326 if Present
(Discriminant_Checking_Func
(N
)) then
327 Process
(Discriminant_Checking_Func
(N
));
331 Set_Marked
(Marks
, N
);
340 -- Start of processing for Trace_Marked