[multiple changes]
[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 -- Clone --
163 ------------
164
165 function Clone (List : Elist_Id) return Elist_Id is
166 Result : Elist_Id;
167 Elmt : Elmt_Id;
168
169 begin
170 if List = No_Elist then
171 return No_Elist;
172
173 -- Replicate the contents of the input list while preserving the
174 -- original order.
175
176 else
177 Result := New_Elmt_List;
178
179 Elmt := First_Elmt (List);
180 while Present (Elmt) loop
181 Append_Elmt (Node (Elmt), Result);
182 Next_Elmt (Elmt);
183 end loop;
184
185 return Result;
186 end if;
187 end Clone;
188
189 --------------
190 -- Contains --
191 --------------
192
193 function Contains (List : Elist_Id; N : Node_Or_Entity_Id) return Boolean is
194 Elmt : Elmt_Id;
195
196 begin
197 if Present (List) then
198 Elmt := First_Elmt (List);
199 while Present (Elmt) loop
200 if Node (Elmt) = N then
201 return True;
202 end if;
203
204 Next_Elmt (Elmt);
205 end loop;
206 end if;
207
208 return False;
209 end Contains;
210
211 --------------------
212 -- Elists_Address --
213 --------------------
214
215 function Elists_Address return System.Address is
216 begin
217 return Elists.Table (First_Elist_Id)'Address;
218 end Elists_Address;
219
220 -------------------
221 -- Elmts_Address --
222 -------------------
223
224 function Elmts_Address return System.Address is
225 begin
226 return Elmts.Table (First_Elmt_Id)'Address;
227 end Elmts_Address;
228
229 ----------------
230 -- First_Elmt --
231 ----------------
232
233 function First_Elmt (List : Elist_Id) return Elmt_Id is
234 begin
235 pragma Assert (List > Elist_Low_Bound);
236 return Elists.Table (List).First;
237 end First_Elmt;
238
239 ----------------
240 -- Initialize --
241 ----------------
242
243 procedure Initialize is
244 begin
245 Elists.Init;
246 Elmts.Init;
247 end Initialize;
248
249 -----------------------
250 -- Insert_Elmt_After --
251 -----------------------
252
253 procedure Insert_Elmt_After (N : Node_Or_Entity_Id; Elmt : Elmt_Id) is
254 Nxt : constant Union_Id := Elmts.Table (Elmt).Next;
255
256 begin
257 pragma Assert (Elmt /= No_Elmt);
258
259 Elmts.Increment_Last;
260 Elmts.Table (Elmts.Last).Node := N;
261 Elmts.Table (Elmts.Last).Next := Nxt;
262
263 Elmts.Table (Elmt).Next := Union_Id (Elmts.Last);
264
265 if Nxt in Elist_Range then
266 Elists.Table (Elist_Id (Nxt)).Last := Elmts.Last;
267 end if;
268 end Insert_Elmt_After;
269
270 ------------------------
271 -- Is_Empty_Elmt_List --
272 ------------------------
273
274 function Is_Empty_Elmt_List (List : Elist_Id) return Boolean is
275 begin
276 return Elists.Table (List).First = No_Elmt;
277 end Is_Empty_Elmt_List;
278
279 -------------------
280 -- Last_Elist_Id --
281 -------------------
282
283 function Last_Elist_Id return Elist_Id is
284 begin
285 return Elists.Last;
286 end Last_Elist_Id;
287
288 ---------------
289 -- Last_Elmt --
290 ---------------
291
292 function Last_Elmt (List : Elist_Id) return Elmt_Id is
293 begin
294 return Elists.Table (List).Last;
295 end Last_Elmt;
296
297 ------------------
298 -- Last_Elmt_Id --
299 ------------------
300
301 function Last_Elmt_Id return Elmt_Id is
302 begin
303 return Elmts.Last;
304 end Last_Elmt_Id;
305
306 ----------
307 -- Lock --
308 ----------
309
310 procedure Lock is
311 begin
312 Elists.Locked := True;
313 Elmts.Locked := True;
314 Elists.Release;
315 Elmts.Release;
316 end Lock;
317
318 -------------------
319 -- New_Elmt_List --
320 -------------------
321
322 function New_Elmt_List return Elist_Id is
323 begin
324 Elists.Increment_Last;
325 Elists.Table (Elists.Last).First := No_Elmt;
326 Elists.Table (Elists.Last).Last := No_Elmt;
327
328 if Debug_Flag_N then
329 Write_Str ("Allocate new element list, returned ID = ");
330 Write_Int (Int (Elists.Last));
331 Write_Eol;
332 end if;
333
334 return Elists.Last;
335 end New_Elmt_List;
336
337 ---------------
338 -- Next_Elmt --
339 ---------------
340
341 function Next_Elmt (Elmt : Elmt_Id) return Elmt_Id is
342 N : constant Union_Id := Elmts.Table (Elmt).Next;
343
344 begin
345 if N in Elist_Range then
346 return No_Elmt;
347 else
348 return Elmt_Id (N);
349 end if;
350 end Next_Elmt;
351
352 procedure Next_Elmt (Elmt : in out Elmt_Id) is
353 begin
354 Elmt := Next_Elmt (Elmt);
355 end Next_Elmt;
356
357 --------
358 -- No --
359 --------
360
361 function No (List : Elist_Id) return Boolean is
362 begin
363 return List = No_Elist;
364 end No;
365
366 function No (Elmt : Elmt_Id) return Boolean is
367 begin
368 return Elmt = No_Elmt;
369 end No;
370
371 ----------
372 -- Node --
373 ----------
374
375 function Node (Elmt : Elmt_Id) return Node_Or_Entity_Id is
376 begin
377 if Elmt = No_Elmt then
378 return Empty;
379 else
380 return Elmts.Table (Elmt).Node;
381 end if;
382 end Node;
383
384 ----------------
385 -- Num_Elists --
386 ----------------
387
388 function Num_Elists return Nat is
389 begin
390 return Int (Elmts.Last) - Int (Elmts.First) + 1;
391 end Num_Elists;
392
393 ------------------
394 -- Prepend_Elmt --
395 ------------------
396
397 procedure Prepend_Elmt (N : Node_Or_Entity_Id; To : Elist_Id) is
398 F : constant Elmt_Id := Elists.Table (To).First;
399
400 begin
401 Elmts.Increment_Last;
402 Elmts.Table (Elmts.Last).Node := N;
403
404 if F = No_Elmt then
405 Elists.Table (To).Last := Elmts.Last;
406 Elmts.Table (Elmts.Last).Next := Union_Id (To);
407 else
408 Elmts.Table (Elmts.Last).Next := Union_Id (F);
409 end if;
410
411 Elists.Table (To).First := Elmts.Last;
412 end Prepend_Elmt;
413
414 -------------
415 -- Present --
416 -------------
417
418 function Present (List : Elist_Id) return Boolean is
419 begin
420 return List /= No_Elist;
421 end Present;
422
423 function Present (Elmt : Elmt_Id) return Boolean is
424 begin
425 return Elmt /= No_Elmt;
426 end Present;
427
428 -----------------
429 -- Remove_Elmt --
430 -----------------
431
432 procedure Remove_Elmt (List : Elist_Id; Elmt : Elmt_Id) is
433 Nxt : Elmt_Id;
434 Prv : Elmt_Id;
435
436 begin
437 Nxt := Elists.Table (List).First;
438
439 -- Case of removing only element in the list
440
441 if Elmts.Table (Nxt).Next in Elist_Range then
442 pragma Assert (Nxt = Elmt);
443
444 Elists.Table (List).First := No_Elmt;
445 Elists.Table (List).Last := No_Elmt;
446
447 -- Case of removing the first element in the list
448
449 elsif Nxt = Elmt then
450 Elists.Table (List).First := Elmt_Id (Elmts.Table (Nxt).Next);
451
452 -- Case of removing second or later element in the list
453
454 else
455 loop
456 Prv := Nxt;
457 Nxt := Elmt_Id (Elmts.Table (Prv).Next);
458 exit when Nxt = Elmt
459 or else Elmts.Table (Nxt).Next in Elist_Range;
460 end loop;
461
462 pragma Assert (Nxt = Elmt);
463
464 Elmts.Table (Prv).Next := Elmts.Table (Nxt).Next;
465
466 if Elmts.Table (Prv).Next in Elist_Range then
467 Elists.Table (List).Last := Prv;
468 end if;
469 end if;
470 end Remove_Elmt;
471
472 ----------------------
473 -- Remove_Last_Elmt --
474 ----------------------
475
476 procedure Remove_Last_Elmt (List : Elist_Id) is
477 Nxt : Elmt_Id;
478 Prv : Elmt_Id;
479
480 begin
481 Nxt := Elists.Table (List).First;
482
483 -- Case of removing only element in the list
484
485 if Elmts.Table (Nxt).Next in Elist_Range then
486 Elists.Table (List).First := No_Elmt;
487 Elists.Table (List).Last := No_Elmt;
488
489 -- Case of at least two elements in list
490
491 else
492 loop
493 Prv := Nxt;
494 Nxt := Elmt_Id (Elmts.Table (Prv).Next);
495 exit when Elmts.Table (Nxt).Next in Elist_Range;
496 end loop;
497
498 Elmts.Table (Prv).Next := Elmts.Table (Nxt).Next;
499 Elists.Table (List).Last := Prv;
500 end if;
501 end Remove_Last_Elmt;
502
503 ------------------
504 -- Replace_Elmt --
505 ------------------
506
507 procedure Replace_Elmt (Elmt : Elmt_Id; New_Node : Node_Or_Entity_Id) is
508 begin
509 Elmts.Table (Elmt).Node := New_Node;
510 end Replace_Elmt;
511
512 ---------------
513 -- Tree_Read --
514 ---------------
515
516 procedure Tree_Read is
517 begin
518 Elists.Tree_Read;
519 Elmts.Tree_Read;
520 end Tree_Read;
521
522 ----------------
523 -- Tree_Write --
524 ----------------
525
526 procedure Tree_Write is
527 begin
528 Elists.Tree_Write;
529 Elmts.Tree_Write;
530 end Tree_Write;
531
532 ------------
533 -- Unlock --
534 ------------
535
536 procedure Unlock is
537 begin
538 Elists.Locked := False;
539 Elmts.Locked := False;
540 end Unlock;
541
542 end Elists;