6170585272ea923cb14f1b62a8a4709ca2f9b07e
[gcc.git] / gcc / ada / elists.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E L I S T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
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.
34
35 with Alloc;
36 with Debug; use Debug;
37 with Output; use Output;
38 with Table;
39
40 package body Elists is
41
42 -------------------------------------
43 -- Implementation of Element Lists --
44 -------------------------------------
45
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:
51
52 -- +----------------------------------------------------+
53 -- | +------------------------------------------+ |
54 -- | | | |
55 -- V | V |
56 -- +-----|--+ +-------+ +-------+ +-------+ |
57 -- | Elmt | | 1st | | 2nd | | Last | |
58 -- | List |--->| Elmt |--->| Elmt ---...-->| Elmt ---+
59 -- | Header | | | | | | | | | |
60 -- +--------+ +---|---+ +---|---+ +---|---+
61 -- | | |
62 -- V V V
63 -- +-------+ +-------+ +-------+
64 -- | | | | | |
65 -- | Node1 | | Node2 | | Node3 |
66 -- | | | | | |
67 -- +-------+ +-------+ +-------+
68
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.
74
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.
79
80 -------------------------
81 -- Element List Tables --
82 -------------------------
83
84 type Elist_Header is record
85 First : Elmt_Id;
86 Last : Elmt_Id;
87 end record;
88
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");
96
97 type Elmt_Item is record
98 Node : Node_Or_Entity_Id;
99 Next : Union_Id;
100 end record;
101
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");
109
110 -----------------
111 -- Append_Elmt --
112 -----------------
113
114 procedure Append_Elmt (N : Node_Or_Entity_Id; To : Elist_Id) is
115 L : constant Elmt_Id := Elists.Table (To).Last;
116
117 begin
118 Elmts.Increment_Last;
119 Elmts.Table (Elmts.Last).Node := N;
120 Elmts.Table (Elmts.Last).Next := Union_Id (To);
121
122 if L = No_Elmt then
123 Elists.Table (To).First := Elmts.Last;
124 else
125 Elmts.Table (L).Next := Union_Id (Elmts.Last);
126 end if;
127
128 Elists.Table (To).Last := Elmts.Last;
129
130 if Debug_Flag_N then
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 = ");
136 Write_Int (Int (N));
137 Write_Eol;
138 end if;
139 end Append_Elmt;
140
141 ------------------------
142 -- Append_Unique_Elmt --
143 ------------------------
144
145 procedure Append_Unique_Elmt (N : Node_Or_Entity_Id; To : Elist_Id) is
146 Elmt : Elmt_Id;
147 begin
148 Elmt := First_Elmt (To);
149 loop
150 if No (Elmt) then
151 Append_Elmt (N, To);
152 return;
153 elsif Node (Elmt) = N then
154 return;
155 else
156 Next_Elmt (Elmt);
157 end if;
158 end loop;
159 end Append_Unique_Elmt;
160
161 --------------
162 -- Contains --
163 --------------
164
165 function Contains (List : Elist_Id; N : Node_Or_Entity_Id) return Boolean is
166 Elmt : Elmt_Id;
167
168 begin
169 if Present (List) then
170 Elmt := First_Elmt (List);
171 while Present (Elmt) loop
172 if Node (Elmt) = N then
173 return True;
174 end if;
175
176 Next_Elmt (Elmt);
177 end loop;
178 end if;
179
180 return False;
181 end Contains;
182
183 --------------------
184 -- Elists_Address --
185 --------------------
186
187 function Elists_Address return System.Address is
188 begin
189 return Elists.Table (First_Elist_Id)'Address;
190 end Elists_Address;
191
192 -------------------
193 -- Elmts_Address --
194 -------------------
195
196 function Elmts_Address return System.Address is
197 begin
198 return Elmts.Table (First_Elmt_Id)'Address;
199 end Elmts_Address;
200
201 ----------------
202 -- First_Elmt --
203 ----------------
204
205 function First_Elmt (List : Elist_Id) return Elmt_Id is
206 begin
207 pragma Assert (List > Elist_Low_Bound);
208 return Elists.Table (List).First;
209 end First_Elmt;
210
211 ----------------
212 -- Initialize --
213 ----------------
214
215 procedure Initialize is
216 begin
217 Elists.Init;
218 Elmts.Init;
219 end Initialize;
220
221 -----------------------
222 -- Insert_Elmt_After --
223 -----------------------
224
225 procedure Insert_Elmt_After (N : Node_Or_Entity_Id; Elmt : Elmt_Id) is
226 Nxt : constant Union_Id := Elmts.Table (Elmt).Next;
227
228 begin
229 pragma Assert (Elmt /= No_Elmt);
230
231 Elmts.Increment_Last;
232 Elmts.Table (Elmts.Last).Node := N;
233 Elmts.Table (Elmts.Last).Next := Nxt;
234
235 Elmts.Table (Elmt).Next := Union_Id (Elmts.Last);
236
237 if Nxt in Elist_Range then
238 Elists.Table (Elist_Id (Nxt)).Last := Elmts.Last;
239 end if;
240 end Insert_Elmt_After;
241
242 ------------------------
243 -- Is_Empty_Elmt_List --
244 ------------------------
245
246 function Is_Empty_Elmt_List (List : Elist_Id) return Boolean is
247 begin
248 return Elists.Table (List).First = No_Elmt;
249 end Is_Empty_Elmt_List;
250
251 -------------------
252 -- Last_Elist_Id --
253 -------------------
254
255 function Last_Elist_Id return Elist_Id is
256 begin
257 return Elists.Last;
258 end Last_Elist_Id;
259
260 ---------------
261 -- Last_Elmt --
262 ---------------
263
264 function Last_Elmt (List : Elist_Id) return Elmt_Id is
265 begin
266 return Elists.Table (List).Last;
267 end Last_Elmt;
268
269 ------------------
270 -- Last_Elmt_Id --
271 ------------------
272
273 function Last_Elmt_Id return Elmt_Id is
274 begin
275 return Elmts.Last;
276 end Last_Elmt_Id;
277
278 ----------
279 -- Lock --
280 ----------
281
282 procedure Lock is
283 begin
284 Elists.Locked := True;
285 Elmts.Locked := True;
286 Elists.Release;
287 Elmts.Release;
288 end Lock;
289
290 -------------------
291 -- New_Elmt_List --
292 -------------------
293
294 function New_Elmt_List return Elist_Id is
295 begin
296 Elists.Increment_Last;
297 Elists.Table (Elists.Last).First := No_Elmt;
298 Elists.Table (Elists.Last).Last := No_Elmt;
299
300 if Debug_Flag_N then
301 Write_Str ("Allocate new element list, returned ID = ");
302 Write_Int (Int (Elists.Last));
303 Write_Eol;
304 end if;
305
306 return Elists.Last;
307 end New_Elmt_List;
308
309 ---------------
310 -- Next_Elmt --
311 ---------------
312
313 function Next_Elmt (Elmt : Elmt_Id) return Elmt_Id is
314 N : constant Union_Id := Elmts.Table (Elmt).Next;
315
316 begin
317 if N in Elist_Range then
318 return No_Elmt;
319 else
320 return Elmt_Id (N);
321 end if;
322 end Next_Elmt;
323
324 procedure Next_Elmt (Elmt : in out Elmt_Id) is
325 begin
326 Elmt := Next_Elmt (Elmt);
327 end Next_Elmt;
328
329 --------
330 -- No --
331 --------
332
333 function No (List : Elist_Id) return Boolean is
334 begin
335 return List = No_Elist;
336 end No;
337
338 function No (Elmt : Elmt_Id) return Boolean is
339 begin
340 return Elmt = No_Elmt;
341 end No;
342
343 ----------
344 -- Node --
345 ----------
346
347 function Node (Elmt : Elmt_Id) return Node_Or_Entity_Id is
348 begin
349 if Elmt = No_Elmt then
350 return Empty;
351 else
352 return Elmts.Table (Elmt).Node;
353 end if;
354 end Node;
355
356 ----------------
357 -- Num_Elists --
358 ----------------
359
360 function Num_Elists return Nat is
361 begin
362 return Int (Elmts.Last) - Int (Elmts.First) + 1;
363 end Num_Elists;
364
365 ------------------
366 -- Prepend_Elmt --
367 ------------------
368
369 procedure Prepend_Elmt (N : Node_Or_Entity_Id; To : Elist_Id) is
370 F : constant Elmt_Id := Elists.Table (To).First;
371
372 begin
373 Elmts.Increment_Last;
374 Elmts.Table (Elmts.Last).Node := N;
375
376 if F = No_Elmt then
377 Elists.Table (To).Last := Elmts.Last;
378 Elmts.Table (Elmts.Last).Next := Union_Id (To);
379 else
380 Elmts.Table (Elmts.Last).Next := Union_Id (F);
381 end if;
382
383 Elists.Table (To).First := Elmts.Last;
384 end Prepend_Elmt;
385
386 -------------
387 -- Present --
388 -------------
389
390 function Present (List : Elist_Id) return Boolean is
391 begin
392 return List /= No_Elist;
393 end Present;
394
395 function Present (Elmt : Elmt_Id) return Boolean is
396 begin
397 return Elmt /= No_Elmt;
398 end Present;
399
400 -----------------
401 -- Remove_Elmt --
402 -----------------
403
404 procedure Remove_Elmt (List : Elist_Id; Elmt : Elmt_Id) is
405 Nxt : Elmt_Id;
406 Prv : Elmt_Id;
407
408 begin
409 Nxt := Elists.Table (List).First;
410
411 -- Case of removing only element in the list
412
413 if Elmts.Table (Nxt).Next in Elist_Range then
414 pragma Assert (Nxt = Elmt);
415
416 Elists.Table (List).First := No_Elmt;
417 Elists.Table (List).Last := No_Elmt;
418
419 -- Case of removing the first element in the list
420
421 elsif Nxt = Elmt then
422 Elists.Table (List).First := Elmt_Id (Elmts.Table (Nxt).Next);
423
424 -- Case of removing second or later element in the list
425
426 else
427 loop
428 Prv := Nxt;
429 Nxt := Elmt_Id (Elmts.Table (Prv).Next);
430 exit when Nxt = Elmt
431 or else Elmts.Table (Nxt).Next in Elist_Range;
432 end loop;
433
434 pragma Assert (Nxt = Elmt);
435
436 Elmts.Table (Prv).Next := Elmts.Table (Nxt).Next;
437
438 if Elmts.Table (Prv).Next in Elist_Range then
439 Elists.Table (List).Last := Prv;
440 end if;
441 end if;
442 end Remove_Elmt;
443
444 ----------------------
445 -- Remove_Last_Elmt --
446 ----------------------
447
448 procedure Remove_Last_Elmt (List : Elist_Id) is
449 Nxt : Elmt_Id;
450 Prv : Elmt_Id;
451
452 begin
453 Nxt := Elists.Table (List).First;
454
455 -- Case of removing only element in the list
456
457 if Elmts.Table (Nxt).Next in Elist_Range then
458 Elists.Table (List).First := No_Elmt;
459 Elists.Table (List).Last := No_Elmt;
460
461 -- Case of at least two elements in list
462
463 else
464 loop
465 Prv := Nxt;
466 Nxt := Elmt_Id (Elmts.Table (Prv).Next);
467 exit when Elmts.Table (Nxt).Next in Elist_Range;
468 end loop;
469
470 Elmts.Table (Prv).Next := Elmts.Table (Nxt).Next;
471 Elists.Table (List).Last := Prv;
472 end if;
473 end Remove_Last_Elmt;
474
475 ------------------
476 -- Replace_Elmt --
477 ------------------
478
479 procedure Replace_Elmt (Elmt : Elmt_Id; New_Node : Node_Or_Entity_Id) is
480 begin
481 Elmts.Table (Elmt).Node := New_Node;
482 end Replace_Elmt;
483
484 ---------------
485 -- Tree_Read --
486 ---------------
487
488 procedure Tree_Read is
489 begin
490 Elists.Tree_Read;
491 Elmts.Tree_Read;
492 end Tree_Read;
493
494 ----------------
495 -- Tree_Write --
496 ----------------
497
498 procedure Tree_Write is
499 begin
500 Elists.Tree_Write;
501 Elmts.Tree_Write;
502 end Tree_Write;
503
504 ------------
505 -- Unlock --
506 ------------
507
508 procedure Unlock is
509 begin
510 Elists.Locked := False;
511 Elmts.Locked := False;
512 end Unlock;
513
514 end Elists;