PR libgomp/64635
[official-gcc.git] / gcc / ada / elists.adb
blobfbfb9e7b46b57ec16142e221ae928d5c7e27b516
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-2014, 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 ------------------------------------------------------------------------------
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.
35 with Alloc;
36 with Debug; use Debug;
37 with Output; use Output;
38 with Table;
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 -- | +------------------------------------------+ |
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 -- +-------+ +-------+ +-------+
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
85 First : Elmt_Id;
86 Last : Elmt_Id;
87 end 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;
99 Next : Union_Id;
100 end record;
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");
110 -----------------
111 -- Append_Elmt --
112 -----------------
114 procedure Append_Elmt (N : Node_Or_Entity_Id; To : Elist_Id) is
115 L : constant Elmt_Id := Elists.Table (To).Last;
117 begin
118 Elmts.Increment_Last;
119 Elmts.Table (Elmts.Last).Node := N;
120 Elmts.Table (Elmts.Last).Next := Union_Id (To);
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;
128 Elists.Table (To).Last := Elmts.Last;
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;
141 ---------------------
142 -- Append_New_Elmt --
143 ---------------------
145 procedure Append_New_Elmt (N : Node_Or_Entity_Id; To : in out Elist_Id) is
146 begin
147 if To = No_Elist then
148 To := New_Elmt_List;
149 end if;
151 Append_Elmt (N, To);
152 end Append_New_Elmt;
154 ------------------------
155 -- Append_Unique_Elmt --
156 ------------------------
158 procedure Append_Unique_Elmt (N : Node_Or_Entity_Id; To : Elist_Id) is
159 Elmt : Elmt_Id;
160 begin
161 Elmt := First_Elmt (To);
162 loop
163 if No (Elmt) then
164 Append_Elmt (N, To);
165 return;
166 elsif Node (Elmt) = N then
167 return;
168 else
169 Next_Elmt (Elmt);
170 end if;
171 end loop;
172 end Append_Unique_Elmt;
174 --------------
175 -- Contains --
176 --------------
178 function Contains (List : Elist_Id; N : Node_Or_Entity_Id) return Boolean is
179 Elmt : Elmt_Id;
181 begin
182 if Present (List) then
183 Elmt := First_Elmt (List);
184 while Present (Elmt) loop
185 if Node (Elmt) = N then
186 return True;
187 end if;
189 Next_Elmt (Elmt);
190 end loop;
191 end if;
193 return False;
194 end Contains;
196 --------------------
197 -- Elists_Address --
198 --------------------
200 function Elists_Address return System.Address is
201 begin
202 return Elists.Table (First_Elist_Id)'Address;
203 end Elists_Address;
205 -------------------
206 -- Elmts_Address --
207 -------------------
209 function Elmts_Address return System.Address is
210 begin
211 return Elmts.Table (First_Elmt_Id)'Address;
212 end Elmts_Address;
214 ----------------
215 -- First_Elmt --
216 ----------------
218 function First_Elmt (List : Elist_Id) return Elmt_Id is
219 begin
220 pragma Assert (List > Elist_Low_Bound);
221 return Elists.Table (List).First;
222 end First_Elmt;
224 ----------------
225 -- Initialize --
226 ----------------
228 procedure Initialize is
229 begin
230 Elists.Init;
231 Elmts.Init;
232 end Initialize;
234 -----------------------
235 -- Insert_Elmt_After --
236 -----------------------
238 procedure Insert_Elmt_After (N : Node_Or_Entity_Id; Elmt : Elmt_Id) is
239 Nxt : constant Union_Id := Elmts.Table (Elmt).Next;
241 begin
242 pragma Assert (Elmt /= No_Elmt);
244 Elmts.Increment_Last;
245 Elmts.Table (Elmts.Last).Node := N;
246 Elmts.Table (Elmts.Last).Next := Nxt;
248 Elmts.Table (Elmt).Next := Union_Id (Elmts.Last);
250 if Nxt in Elist_Range then
251 Elists.Table (Elist_Id (Nxt)).Last := Elmts.Last;
252 end if;
253 end Insert_Elmt_After;
255 ------------------------
256 -- Is_Empty_Elmt_List --
257 ------------------------
259 function Is_Empty_Elmt_List (List : Elist_Id) return Boolean is
260 begin
261 return Elists.Table (List).First = No_Elmt;
262 end Is_Empty_Elmt_List;
264 -------------------
265 -- Last_Elist_Id --
266 -------------------
268 function Last_Elist_Id return Elist_Id is
269 begin
270 return Elists.Last;
271 end Last_Elist_Id;
273 ---------------
274 -- Last_Elmt --
275 ---------------
277 function Last_Elmt (List : Elist_Id) return Elmt_Id is
278 begin
279 return Elists.Table (List).Last;
280 end Last_Elmt;
282 ------------------
283 -- Last_Elmt_Id --
284 ------------------
286 function Last_Elmt_Id return Elmt_Id is
287 begin
288 return Elmts.Last;
289 end Last_Elmt_Id;
291 ----------
292 -- Lock --
293 ----------
295 procedure Lock is
296 begin
297 Elists.Locked := True;
298 Elmts.Locked := True;
299 Elists.Release;
300 Elmts.Release;
301 end Lock;
303 --------------------
304 -- New_Copy_Elist --
305 --------------------
307 function New_Copy_Elist (List : Elist_Id) return Elist_Id is
308 Result : Elist_Id;
309 Elmt : Elmt_Id;
311 begin
312 if List = No_Elist then
313 return No_Elist;
315 -- Replicate the contents of the input list while preserving the
316 -- original order.
318 else
319 Result := New_Elmt_List;
321 Elmt := First_Elmt (List);
322 while Present (Elmt) loop
323 Append_Elmt (Node (Elmt), Result);
324 Next_Elmt (Elmt);
325 end loop;
327 return Result;
328 end if;
329 end New_Copy_Elist;
331 -------------------
332 -- New_Elmt_List --
333 -------------------
335 function New_Elmt_List return Elist_Id is
336 begin
337 Elists.Increment_Last;
338 Elists.Table (Elists.Last).First := No_Elmt;
339 Elists.Table (Elists.Last).Last := No_Elmt;
341 if Debug_Flag_N then
342 Write_Str ("Allocate new element list, returned ID = ");
343 Write_Int (Int (Elists.Last));
344 Write_Eol;
345 end if;
347 return Elists.Last;
348 end New_Elmt_List;
350 ---------------
351 -- Next_Elmt --
352 ---------------
354 function Next_Elmt (Elmt : Elmt_Id) return Elmt_Id is
355 N : constant Union_Id := Elmts.Table (Elmt).Next;
357 begin
358 if N in Elist_Range then
359 return No_Elmt;
360 else
361 return Elmt_Id (N);
362 end if;
363 end Next_Elmt;
365 procedure Next_Elmt (Elmt : in out Elmt_Id) is
366 begin
367 Elmt := Next_Elmt (Elmt);
368 end Next_Elmt;
370 --------
371 -- No --
372 --------
374 function No (List : Elist_Id) return Boolean is
375 begin
376 return List = No_Elist;
377 end No;
379 function No (Elmt : Elmt_Id) return Boolean is
380 begin
381 return Elmt = No_Elmt;
382 end No;
384 ----------
385 -- Node --
386 ----------
388 function Node (Elmt : Elmt_Id) return Node_Or_Entity_Id is
389 begin
390 if Elmt = No_Elmt then
391 return Empty;
392 else
393 return Elmts.Table (Elmt).Node;
394 end if;
395 end Node;
397 ----------------
398 -- Num_Elists --
399 ----------------
401 function Num_Elists return Nat is
402 begin
403 return Int (Elmts.Last) - Int (Elmts.First) + 1;
404 end Num_Elists;
406 ------------------
407 -- Prepend_Elmt --
408 ------------------
410 procedure Prepend_Elmt (N : Node_Or_Entity_Id; To : Elist_Id) is
411 F : constant Elmt_Id := Elists.Table (To).First;
413 begin
414 Elmts.Increment_Last;
415 Elmts.Table (Elmts.Last).Node := N;
417 if F = No_Elmt then
418 Elists.Table (To).Last := Elmts.Last;
419 Elmts.Table (Elmts.Last).Next := Union_Id (To);
420 else
421 Elmts.Table (Elmts.Last).Next := Union_Id (F);
422 end if;
424 Elists.Table (To).First := Elmts.Last;
425 end Prepend_Elmt;
427 -------------
428 -- Present --
429 -------------
431 function Present (List : Elist_Id) return Boolean is
432 begin
433 return List /= No_Elist;
434 end Present;
436 function Present (Elmt : Elmt_Id) return Boolean is
437 begin
438 return Elmt /= No_Elmt;
439 end Present;
441 ------------
442 -- Remove --
443 ------------
445 procedure Remove (List : Elist_Id; N : Node_Or_Entity_Id) is
446 Elmt : Elmt_Id;
448 begin
449 if Present (List) then
450 Elmt := First_Elmt (List);
451 while Present (Elmt) loop
452 if Node (Elmt) = N then
453 Remove_Elmt (List, Elmt);
454 exit;
455 end if;
457 Next_Elmt (Elmt);
458 end loop;
459 end if;
460 end Remove;
462 -----------------
463 -- Remove_Elmt --
464 -----------------
466 procedure Remove_Elmt (List : Elist_Id; Elmt : Elmt_Id) is
467 Nxt : Elmt_Id;
468 Prv : Elmt_Id;
470 begin
471 Nxt := Elists.Table (List).First;
473 -- Case of removing only element in the list
475 if Elmts.Table (Nxt).Next in Elist_Range then
476 pragma Assert (Nxt = Elmt);
478 Elists.Table (List).First := No_Elmt;
479 Elists.Table (List).Last := No_Elmt;
481 -- Case of removing the first element in the list
483 elsif Nxt = Elmt then
484 Elists.Table (List).First := Elmt_Id (Elmts.Table (Nxt).Next);
486 -- Case of removing second or later element in the list
488 else
489 loop
490 Prv := Nxt;
491 Nxt := Elmt_Id (Elmts.Table (Prv).Next);
492 exit when Nxt = Elmt
493 or else Elmts.Table (Nxt).Next in Elist_Range;
494 end loop;
496 pragma Assert (Nxt = Elmt);
498 Elmts.Table (Prv).Next := Elmts.Table (Nxt).Next;
500 if Elmts.Table (Prv).Next in Elist_Range then
501 Elists.Table (List).Last := Prv;
502 end if;
503 end if;
504 end Remove_Elmt;
506 ----------------------
507 -- Remove_Last_Elmt --
508 ----------------------
510 procedure Remove_Last_Elmt (List : Elist_Id) is
511 Nxt : Elmt_Id;
512 Prv : Elmt_Id;
514 begin
515 Nxt := Elists.Table (List).First;
517 -- Case of removing only element in the list
519 if Elmts.Table (Nxt).Next in Elist_Range then
520 Elists.Table (List).First := No_Elmt;
521 Elists.Table (List).Last := No_Elmt;
523 -- Case of at least two elements in list
525 else
526 loop
527 Prv := Nxt;
528 Nxt := Elmt_Id (Elmts.Table (Prv).Next);
529 exit when Elmts.Table (Nxt).Next in Elist_Range;
530 end loop;
532 Elmts.Table (Prv).Next := Elmts.Table (Nxt).Next;
533 Elists.Table (List).Last := Prv;
534 end if;
535 end Remove_Last_Elmt;
537 ------------------
538 -- Replace_Elmt --
539 ------------------
541 procedure Replace_Elmt (Elmt : Elmt_Id; New_Node : Node_Or_Entity_Id) is
542 begin
543 Elmts.Table (Elmt).Node := New_Node;
544 end Replace_Elmt;
546 ---------------
547 -- Tree_Read --
548 ---------------
550 procedure Tree_Read is
551 begin
552 Elists.Tree_Read;
553 Elmts.Tree_Read;
554 end Tree_Read;
556 ----------------
557 -- Tree_Write --
558 ----------------
560 procedure Tree_Write is
561 begin
562 Elists.Tree_Write;
563 Elmts.Tree_Write;
564 end Tree_Write;
566 ------------
567 -- Unlock --
568 ------------
570 procedure Unlock is
571 begin
572 Elists.Locked := False;
573 Elmts.Locked := False;
574 end Unlock;
576 end Elists;