1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- WARNING: There is a C version of this package. Any changes to this
33 -- source file must be properly reflected in the C header a-elists.h.
36 with Debug
; use Debug
;
37 with Output
; use Output
;
40 package body Elists
is
42 -------------------------------------
43 -- Implementation of Element Lists --
44 -------------------------------------
46 -- Element lists are composed of three types of entities. The element
47 -- list header, which references the first and last elements of the
48 -- list, the elements themselves which are singly linked and also
49 -- reference the nodes on the list, and finally the nodes themselves.
50 -- The following diagram shows how an element list is represented:
52 -- +----------------------------------------------------+
53 -- | +------------------------------------------+ |
56 -- +-----|--+ +-------+ +-------+ +-------+ |
57 -- | Elmt | | 1st | | 2nd | | Last | |
58 -- | List |--->| Elmt |--->| Elmt ---...-->| Elmt ---+
59 -- | Header | | | | | | | | | |
60 -- +--------+ +---|---+ +---|---+ +---|---+
63 -- +-------+ +-------+ +-------+
65 -- | Node1 | | Node2 | | Node3 |
67 -- +-------+ +-------+ +-------+
69 -- The list header is an entry in the Elists table. The values used for
70 -- the type Elist_Id are subscripts into this table. The First_Elmt field
71 -- (Lfield1) points to the first element on the list, or to No_Elmt in the
72 -- case of an empty list. Similarly the Last_Elmt field (Lfield2) points to
73 -- the last element on the list or to No_Elmt in the case of an empty list.
75 -- The elements themselves are entries in the Elmts table. The Next field
76 -- of each entry points to the next element, or to the Elist header if this
77 -- is the last item in the list. The Node field points to the node which
78 -- is referenced by the corresponding list entry.
80 -------------------------
81 -- Element List Tables --
82 -------------------------
84 type Elist_Header
is record
89 package Elists
is new Table
.Table
(
90 Table_Component_Type
=> Elist_Header
,
91 Table_Index_Type
=> Elist_Id
'Base,
92 Table_Low_Bound
=> First_Elist_Id
,
93 Table_Initial
=> Alloc
.Elists_Initial
,
94 Table_Increment
=> Alloc
.Elists_Increment
,
95 Table_Name
=> "Elists");
97 type Elmt_Item
is record
98 Node
: Node_Or_Entity_Id
;
102 package Elmts
is new Table
.Table
(
103 Table_Component_Type
=> Elmt_Item
,
104 Table_Index_Type
=> Elmt_Id
'Base,
105 Table_Low_Bound
=> First_Elmt_Id
,
106 Table_Initial
=> Alloc
.Elmts_Initial
,
107 Table_Increment
=> Alloc
.Elmts_Increment
,
108 Table_Name
=> "Elmts");
114 procedure Append_Elmt
(N
: Node_Or_Entity_Id
; To
: Elist_Id
) is
115 L
: constant Elmt_Id
:= Elists
.Table
(To
).Last
;
118 Elmts
.Increment_Last
;
119 Elmts
.Table
(Elmts
.Last
).Node
:= N
;
120 Elmts
.Table
(Elmts
.Last
).Next
:= Union_Id
(To
);
123 Elists
.Table
(To
).First
:= Elmts
.Last
;
125 Elmts
.Table
(L
).Next
:= Union_Id
(Elmts
.Last
);
128 Elists
.Table
(To
).Last
:= Elmts
.Last
;
131 Write_Str
("Append new element Elmt_Id = ");
132 Write_Int
(Int
(Elmts
.Last
));
133 Write_Str
(" to list Elist_Id = ");
134 Write_Int
(Int
(To
));
135 Write_Str
(" referencing Node_Or_Entity_Id = ");
141 ------------------------
142 -- Append_Unique_Elmt --
143 ------------------------
145 procedure Append_Unique_Elmt
(N
: Node_Or_Entity_Id
; To
: Elist_Id
) is
148 Elmt
:= First_Elmt
(To
);
153 elsif Node
(Elmt
) = N
then
159 end Append_Unique_Elmt
;
165 function Contains
(List
: Elist_Id
; N
: Node_Or_Entity_Id
) return Boolean is
169 if Present
(List
) then
170 Elmt
:= First_Elmt
(List
);
171 while Present
(Elmt
) loop
172 if Node
(Elmt
) = N
then
187 function Elists_Address
return System
.Address
is
189 return Elists
.Table
(First_Elist_Id
)'Address;
196 function Elmts_Address
return System
.Address
is
198 return Elmts
.Table
(First_Elmt_Id
)'Address;
205 function First_Elmt
(List
: Elist_Id
) return Elmt_Id
is
207 pragma Assert
(List
> Elist_Low_Bound
);
208 return Elists
.Table
(List
).First
;
215 procedure Initialize
is
221 -----------------------
222 -- Insert_Elmt_After --
223 -----------------------
225 procedure Insert_Elmt_After
(N
: Node_Or_Entity_Id
; Elmt
: Elmt_Id
) is
226 Nxt
: constant Union_Id
:= Elmts
.Table
(Elmt
).Next
;
229 pragma Assert
(Elmt
/= No_Elmt
);
231 Elmts
.Increment_Last
;
232 Elmts
.Table
(Elmts
.Last
).Node
:= N
;
233 Elmts
.Table
(Elmts
.Last
).Next
:= Nxt
;
235 Elmts
.Table
(Elmt
).Next
:= Union_Id
(Elmts
.Last
);
237 if Nxt
in Elist_Range
then
238 Elists
.Table
(Elist_Id
(Nxt
)).Last
:= Elmts
.Last
;
240 end Insert_Elmt_After
;
242 ------------------------
243 -- Is_Empty_Elmt_List --
244 ------------------------
246 function Is_Empty_Elmt_List
(List
: Elist_Id
) return Boolean is
248 return Elists
.Table
(List
).First
= No_Elmt
;
249 end Is_Empty_Elmt_List
;
255 function Last_Elist_Id
return Elist_Id
is
264 function Last_Elmt
(List
: Elist_Id
) return Elmt_Id
is
266 return Elists
.Table
(List
).Last
;
273 function Last_Elmt_Id
return Elmt_Id
is
284 Elists
.Locked
:= True;
285 Elmts
.Locked
:= True;
294 function New_Copy_Elist
(List
: Elist_Id
) return Elist_Id
is
299 if List
= No_Elist
then
302 -- Replicate the contents of the input list while preserving the
306 Result
:= New_Elmt_List
;
308 Elmt
:= First_Elmt
(List
);
309 while Present
(Elmt
) loop
310 Append_Elmt
(Node
(Elmt
), Result
);
322 function New_Elmt_List
return Elist_Id
is
324 Elists
.Increment_Last
;
325 Elists
.Table
(Elists
.Last
).First
:= No_Elmt
;
326 Elists
.Table
(Elists
.Last
).Last
:= No_Elmt
;
329 Write_Str
("Allocate new element list, returned ID = ");
330 Write_Int
(Int
(Elists
.Last
));
341 function Next_Elmt
(Elmt
: Elmt_Id
) return Elmt_Id
is
342 N
: constant Union_Id
:= Elmts
.Table
(Elmt
).Next
;
345 if N
in Elist_Range
then
352 procedure Next_Elmt
(Elmt
: in out Elmt_Id
) is
354 Elmt
:= Next_Elmt
(Elmt
);
361 function No
(List
: Elist_Id
) return Boolean is
363 return List
= No_Elist
;
366 function No
(Elmt
: Elmt_Id
) return Boolean is
368 return Elmt
= No_Elmt
;
375 function Node
(Elmt
: Elmt_Id
) return Node_Or_Entity_Id
is
377 if Elmt
= No_Elmt
then
380 return Elmts
.Table
(Elmt
).Node
;
388 function Num_Elists
return Nat
is
390 return Int
(Elmts
.Last
) - Int
(Elmts
.First
) + 1;
397 procedure Prepend_Elmt
(N
: Node_Or_Entity_Id
; To
: Elist_Id
) is
398 F
: constant Elmt_Id
:= Elists
.Table
(To
).First
;
401 Elmts
.Increment_Last
;
402 Elmts
.Table
(Elmts
.Last
).Node
:= N
;
405 Elists
.Table
(To
).Last
:= Elmts
.Last
;
406 Elmts
.Table
(Elmts
.Last
).Next
:= Union_Id
(To
);
408 Elmts
.Table
(Elmts
.Last
).Next
:= Union_Id
(F
);
411 Elists
.Table
(To
).First
:= Elmts
.Last
;
418 function Present
(List
: Elist_Id
) return Boolean is
420 return List
/= No_Elist
;
423 function Present
(Elmt
: Elmt_Id
) return Boolean is
425 return Elmt
/= No_Elmt
;
432 procedure Remove
(List
: Elist_Id
; N
: Node_Or_Entity_Id
) is
436 if Present
(List
) then
437 Elmt
:= First_Elmt
(List
);
438 while Present
(Elmt
) loop
439 if Node
(Elmt
) = N
then
440 Remove_Elmt
(List
, Elmt
);
453 procedure Remove_Elmt
(List
: Elist_Id
; Elmt
: Elmt_Id
) is
458 Nxt
:= Elists
.Table
(List
).First
;
460 -- Case of removing only element in the list
462 if Elmts
.Table
(Nxt
).Next
in Elist_Range
then
463 pragma Assert
(Nxt
= Elmt
);
465 Elists
.Table
(List
).First
:= No_Elmt
;
466 Elists
.Table
(List
).Last
:= No_Elmt
;
468 -- Case of removing the first element in the list
470 elsif Nxt
= Elmt
then
471 Elists
.Table
(List
).First
:= Elmt_Id
(Elmts
.Table
(Nxt
).Next
);
473 -- Case of removing second or later element in the list
478 Nxt
:= Elmt_Id
(Elmts
.Table
(Prv
).Next
);
480 or else Elmts
.Table
(Nxt
).Next
in Elist_Range
;
483 pragma Assert
(Nxt
= Elmt
);
485 Elmts
.Table
(Prv
).Next
:= Elmts
.Table
(Nxt
).Next
;
487 if Elmts
.Table
(Prv
).Next
in Elist_Range
then
488 Elists
.Table
(List
).Last
:= Prv
;
493 ----------------------
494 -- Remove_Last_Elmt --
495 ----------------------
497 procedure Remove_Last_Elmt
(List
: Elist_Id
) is
502 Nxt
:= Elists
.Table
(List
).First
;
504 -- Case of removing only element in the list
506 if Elmts
.Table
(Nxt
).Next
in Elist_Range
then
507 Elists
.Table
(List
).First
:= No_Elmt
;
508 Elists
.Table
(List
).Last
:= No_Elmt
;
510 -- Case of at least two elements in list
515 Nxt
:= Elmt_Id
(Elmts
.Table
(Prv
).Next
);
516 exit when Elmts
.Table
(Nxt
).Next
in Elist_Range
;
519 Elmts
.Table
(Prv
).Next
:= Elmts
.Table
(Nxt
).Next
;
520 Elists
.Table
(List
).Last
:= Prv
;
522 end Remove_Last_Elmt
;
528 procedure Replace_Elmt
(Elmt
: Elmt_Id
; New_Node
: Node_Or_Entity_Id
) is
530 Elmts
.Table
(Elmt
).Node
:= New_Node
;
537 procedure Tree_Read
is
547 procedure Tree_Write
is
559 Elists
.Locked
:= False;
560 Elmts
.Locked
:= False;