1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-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 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 -- WARNING: There is a C version of this package. Any changes to this
37 -- source file must be properly reflected in the C header a-elists.h.
40 with Debug; use Debug;
41 with Output; use Output;
44 package body Elists is
46 -------------------------------------
47 -- Implementation of Element Lists --
48 -------------------------------------
50 -- Element lists are composed of three types of entities. The element
51 -- list header, which references the first and last elements of the
52 -- list, the elements themselves which are singly linked and also
53 -- reference the nodes on the list, and finally the nodes themselves.
54 -- The following diagram shows how an element list is represented:
56 -- +----------------------------------------------------+
57 -- | +------------------------------------------+ |
60 -- +-----|--+ +-------+ +-------+ +-------+ |
61 -- | Elmt | | 1st | | 2nd | | Last | |
62 -- | List |--->| Elmt |--->| Elmt ---...-->| Elmt ---+
63 -- | Header | | | | | | | | | |
64 -- +--------+ +---|---+ +---|---+ +---|---+
67 -- +-------+ +-------+ +-------+
69 -- | Node1 | | Node2 | | Node3 |
71 -- +-------+ +-------+ +-------+
73 -- The list header is an entry in the Elists table. The values used for
74 -- the type Elist_Id are subscripts into this table. The First_Elmt field
75 -- (Lfield1) points to the first element on the list, or to No_Elmt in the
76 -- case of an empty list. Similarly the Last_Elmt field (Lfield2) points to
77 -- the last element on the list or to No_Elmt in the case of an empty list.
79 -- The elements themselves are entries in the Elmts table. The Next field
80 -- of each entry points to the next element, or to the Elist header if this
81 -- is the last item in the list. The Node field points to the node which
82 -- is referenced by the corresponding list entry.
84 --------------------------
85 -- Element List Tables --
86 --------------------------
88 type Elist_Header is record
93 package Elists is new Table.Table (
94 Table_Component_Type => Elist_Header,
95 Table_Index_Type => Elist_Id,
96 Table_Low_Bound => First_Elist_Id,
97 Table_Initial => Alloc.Elists_Initial,
98 Table_Increment => Alloc.Elists_Increment,
99 Table_Name => "Elists");
101 type Elmt_Item is record
106 package Elmts is new Table.Table (
107 Table_Component_Type => Elmt_Item,
108 Table_Index_Type => Elmt_Id,
109 Table_Low_Bound => First_Elmt_Id,
110 Table_Initial => Alloc.Elmts_Initial,
111 Table_Increment => Alloc.Elmts_Increment,
112 Table_Name => "Elmts");
118 procedure Append_Elmt (Node : Node_Id; To : Elist_Id) is
119 L : constant Elmt_Id := Elists.Table (To).Last;
122 Elmts.Increment_Last;
123 Elmts.Table (Elmts.Last).Node := Node;
124 Elmts.Table (Elmts.Last).Next := Union_Id (To);
127 Elists.Table (To).First := Elmts.Last;
129 Elmts.Table (L).Next := Union_Id (Elmts.Last);
132 Elists.Table (To).Last := Elmts.Last;
135 Write_Str ("Append new element Elmt_Id = ");
136 Write_Int (Int (Elmts.Last));
137 Write_Str (" to list Elist_Id = ");
138 Write_Int (Int (To));
139 Write_Str (" referencing Node_Id = ");
140 Write_Int (Int (Node));
149 function Elists_Address return System.Address is
151 return Elists.Table (First_Elist_Id)'Address;
158 function Elmts_Address return System.Address is
160 return Elmts.Table (First_Elmt_Id)'Address;
167 function First_Elmt (List : Elist_Id) return Elmt_Id is
169 pragma Assert (List > Elist_Low_Bound);
170 return Elists.Table (List).First;
177 procedure Initialize is
183 -----------------------
184 -- Insert_Elmt_After --
185 -----------------------
187 procedure Insert_Elmt_After (Node : Node_Id; Elmt : Elmt_Id) is
188 N : constant Union_Id := Elmts.Table (Elmt).Next;
192 pragma Assert (Elmt /= No_Elmt);
194 Elmts.Increment_Last;
195 Elmts.Table (Elmts.Last).Node := Node;
196 Elmts.Table (Elmts.Last).Next := N;
198 Elmts.Table (Elmt).Next := Union_Id (Elmts.Last);
200 if N in Elist_Range then
201 Elists.Table (Elist_Id (N)).Last := Elmts.Last;
203 end Insert_Elmt_After;
205 ------------------------
206 -- Is_Empty_Elmt_List --
207 ------------------------
209 function Is_Empty_Elmt_List (List : Elist_Id) return Boolean is
211 return Elists.Table (List).First = No_Elmt;
212 end Is_Empty_Elmt_List;
218 function Last_Elist_Id return Elist_Id is
227 function Last_Elmt (List : Elist_Id) return Elmt_Id is
229 return Elists.Table (List).Last;
236 function Last_Elmt_Id return Elmt_Id is
247 Elists.Locked := True;
248 Elmts.Locked := True;
257 function New_Elmt_List return Elist_Id is
259 Elists.Increment_Last;
260 Elists.Table (Elists.Last).First := No_Elmt;
261 Elists.Table (Elists.Last).Last := No_Elmt;
264 Write_Str ("Allocate new element list, returned ID = ");
265 Write_Int (Int (Elists.Last));
276 function Next_Elmt (Elmt : Elmt_Id) return Elmt_Id is
277 N : constant Union_Id := Elmts.Table (Elmt).Next;
280 if N in Elist_Range then
287 procedure Next_Elmt (Elmt : in out Elmt_Id) is
289 Elmt := Next_Elmt (Elmt);
296 function No (List : Elist_Id) return Boolean is
298 return List = No_Elist;
301 function No (Elmt : Elmt_Id) return Boolean is
303 return Elmt = No_Elmt;
310 function Node (Elmt : Elmt_Id) return Node_Id is
312 if Elmt = No_Elmt then
315 return Elmts.Table (Elmt).Node;
323 function Num_Elists return Nat is
325 return Int (Elmts.Last) - Int (Elmts.First) + 1;
332 procedure Prepend_Elmt (Node : Node_Id; To : Elist_Id) is
333 F : constant Elmt_Id := Elists.Table (To).First;
336 Elmts.Increment_Last;
337 Elmts.Table (Elmts.Last).Node := Node;
340 Elists.Table (To).Last := Elmts.Last;
341 Elmts.Table (Elmts.Last).Next := Union_Id (To);
343 Elmts.Table (Elmts.Last).Next := Union_Id (F);
346 Elists.Table (To).First := Elmts.Last;
354 function Present (List : Elist_Id) return Boolean is
356 return List /= No_Elist;
359 function Present (Elmt : Elmt_Id) return Boolean is
361 return Elmt /= No_Elmt;
368 procedure Remove_Elmt (List : Elist_Id; Elmt : Elmt_Id) is
373 Nxt := Elists.Table (List).First;
375 -- Case of removing only element in the list
377 if Elmts.Table (Nxt).Next in Elist_Range then
379 pragma Assert (Nxt = Elmt);
381 Elists.Table (List).First := No_Elmt;
382 Elists.Table (List).Last := No_Elmt;
384 -- Case of removing the first element in the list
386 elsif Nxt = Elmt then
387 Elists.Table (List).First := Elmt_Id (Elmts.Table (Nxt).Next);
389 -- Case of removing second or later element in the list
394 Nxt := Elmt_Id (Elmts.Table (Prv).Next);
396 or else Elmts.Table (Nxt).Next in Elist_Range;
399 pragma Assert (Nxt = Elmt);
401 Elmts.Table (Prv).Next := Elmts.Table (Nxt).Next;
403 if Elmts.Table (Prv).Next in Elist_Range then
404 Elists.Table (List).Last := Prv;
409 ----------------------
410 -- Remove_Last_Elmt --
411 ----------------------
413 procedure Remove_Last_Elmt (List : Elist_Id) is
418 Nxt := Elists.Table (List).First;
420 -- Case of removing only element in the list
422 if Elmts.Table (Nxt).Next in Elist_Range then
423 Elists.Table (List).First := No_Elmt;
424 Elists.Table (List).Last := No_Elmt;
426 -- Case of at least two elements in list
431 Nxt := Elmt_Id (Elmts.Table (Prv).Next);
432 exit when Elmts.Table (Nxt).Next in Elist_Range;
435 Elmts.Table (Prv).Next := Elmts.Table (Nxt).Next;
436 Elists.Table (List).Last := Prv;
438 end Remove_Last_Elmt;
444 procedure Replace_Elmt (Elmt : Elmt_Id; New_Node : Node_Id) is
446 Elmts.Table (Elmt).Node := New_Node;
453 procedure Tree_Read is
463 procedure Tree_Write is