c++: add fixed test [PR94100]
[official-gcc.git] / gcc / m2 / mc-boot / Glists.cc
blobf29edec1a140b6e9bb223cb37b4eca5cf977d5a8
1 /* do not edit automatically generated by mc from lists. */
2 /* Dynamic list library for pointers.
3 Copyright (C) 2015-2025 Free Software Foundation, Inc.
5 This file is part of GNU Modula-2.
7 GNU Modula-2 is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include <stdbool.h>
24 # if !defined (PROC_D)
25 # define PROC_D
26 typedef void (*PROC_t) (void);
27 typedef struct { PROC_t proc; } PROC;
28 # endif
30 # if !defined (TRUE)
31 # define TRUE (1==1)
32 # endif
34 # if !defined (FALSE)
35 # define FALSE (1==0)
36 # endif
38 # include "GStorage.h"
39 #if defined(__cplusplus)
40 # undef NULL
41 # define NULL 0
42 #endif
43 #define _lists_C
45 #include "Glists.h"
46 # include "GStorage.h"
48 typedef struct symbolKey_performOperation_p symbolKey_performOperation;
50 # define MaxnoOfelements 5
51 typedef struct lists__T1_r lists__T1;
53 typedef struct lists__T2_a lists__T2;
55 typedef lists__T1 *lists_list__opaque;
57 struct lists__T2_a { void * array[MaxnoOfelements-1+1]; };
58 struct lists__T1_r {
59 unsigned int noOfelements;
60 lists__T2 elements;
61 lists_list__opaque next;
66 initList - creates a new list, l.
69 extern "C" lists_list lists_initList (void);
72 killList - deletes the complete list, l.
75 extern "C" void lists_killList (lists_list *l);
78 putItemIntoList - places an ADDRESS, c, into list, l.
81 extern "C" void lists_putItemIntoList (lists_list l, void * c);
84 getItemFromList - retrieves the nth WORD from list, l.
87 extern "C" void * lists_getItemFromList (lists_list l, unsigned int n);
90 getIndexOfList - returns the index for WORD, c, in list, l.
91 If more than one WORD, c, exists the index
92 for the first is returned.
95 extern "C" unsigned int lists_getIndexOfList (lists_list l, void * c);
98 noOfItemsInList - returns the number of items in list, l.
101 extern "C" unsigned int lists_noOfItemsInList (lists_list l);
104 includeItemIntoList - adds an ADDRESS, c, into a list providing
105 the value does not already exist.
108 extern "C" void lists_includeItemIntoList (lists_list l, void * c);
111 removeItemFromList - removes a ADDRESS, c, from a list.
112 It assumes that this value only appears once.
115 extern "C" void lists_removeItemFromList (lists_list l, void * c);
118 isItemInList - returns true if a ADDRESS, c, was found in list, l.
121 extern "C" bool lists_isItemInList (lists_list l, void * c);
124 foreachItemInListDo - calls procedure, P, foreach item in list, l.
127 extern "C" void lists_foreachItemInListDo (lists_list l, symbolKey_performOperation p);
130 duplicateList - returns a duplicate list derived from, l.
133 extern "C" lists_list lists_duplicateList (lists_list l);
136 removeItem - remove an element at index, i, from the list data type.
139 static void removeItem (lists_list__opaque p, lists_list__opaque l, unsigned int i);
143 removeItem - remove an element at index, i, from the list data type.
146 static void removeItem (lists_list__opaque p, lists_list__opaque l, unsigned int i)
148 l->noOfelements -= 1;
149 while (i <= l->noOfelements)
151 l->elements.array[i-1] = l->elements.array[i+1-1];
152 i += 1;
154 if ((l->noOfelements == 0) && (p != NULL))
156 p->next = l->next;
157 Storage_DEALLOCATE ((void **) &l, sizeof (lists__T1));
163 initList - creates a new list, l.
166 extern "C" lists_list lists_initList (void)
168 lists_list__opaque l;
170 Storage_ALLOCATE ((void **) &l, sizeof (lists__T1));
171 l->noOfelements = 0;
172 l->next = static_cast<lists_list__opaque> (NULL);
173 return static_cast<lists_list> (l);
174 /* static analysis guarentees a RETURN statement will be used before here. */
175 __builtin_unreachable ();
180 killList - deletes the complete list, l.
183 extern "C" void lists_killList (lists_list *l)
185 if ((*l) != NULL)
187 if (static_cast<lists_list__opaque> ((*l))->next != NULL)
189 lists_killList (reinterpret_cast<lists_list *> (&static_cast<lists_list__opaque> ((*l))->next));
191 Storage_DEALLOCATE ((void **) &(*l), sizeof (lists__T1));
197 putItemIntoList - places an ADDRESS, c, into list, l.
200 extern "C" void lists_putItemIntoList (lists_list l, void * c)
202 if (static_cast<lists_list__opaque> (l)->noOfelements < MaxnoOfelements)
204 static_cast<lists_list__opaque> (l)->noOfelements += 1;
205 static_cast<lists_list__opaque> (l)->elements.array[static_cast<lists_list__opaque> (l)->noOfelements-1] = c;
207 else if (static_cast<lists_list__opaque> (l)->next != NULL)
209 /* avoid dangling else. */
210 lists_putItemIntoList (static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next), c);
212 else
214 /* avoid dangling else. */
215 static_cast<lists_list__opaque> (l)->next = static_cast<lists_list__opaque> (lists_initList ());
216 lists_putItemIntoList (static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next), c);
222 getItemFromList - retrieves the nth WORD from list, l.
225 extern "C" void * lists_getItemFromList (lists_list l, unsigned int n)
227 while (l != NULL)
229 if (n <= static_cast<lists_list__opaque> (l)->noOfelements)
231 return static_cast<lists_list__opaque> (l)->elements.array[n-1];
233 else
235 n -= static_cast<lists_list__opaque> (l)->noOfelements;
237 l = static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next);
239 return static_cast<void *> (0);
240 /* static analysis guarentees a RETURN statement will be used before here. */
241 __builtin_unreachable ();
246 getIndexOfList - returns the index for WORD, c, in list, l.
247 If more than one WORD, c, exists the index
248 for the first is returned.
251 extern "C" unsigned int lists_getIndexOfList (lists_list l, void * c)
253 unsigned int i;
255 if (l == NULL)
257 return 0;
259 else
261 i = 1;
262 while (i <= static_cast<lists_list__opaque> (l)->noOfelements)
264 if (static_cast<lists_list__opaque> (l)->elements.array[i-1] == c)
266 return i;
268 else
270 i += 1;
273 return static_cast<lists_list__opaque> (l)->noOfelements+(lists_getIndexOfList (static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next), c));
275 /* static analysis guarentees a RETURN statement will be used before here. */
276 __builtin_unreachable ();
281 noOfItemsInList - returns the number of items in list, l.
284 extern "C" unsigned int lists_noOfItemsInList (lists_list l)
286 unsigned int t;
288 if (l == NULL)
290 return 0;
292 else
294 t = 0;
295 do {
296 t += static_cast<lists_list__opaque> (l)->noOfelements;
297 l = static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next);
298 } while (! (l == NULL));
299 return t;
301 /* static analysis guarentees a RETURN statement will be used before here. */
302 __builtin_unreachable ();
307 includeItemIntoList - adds an ADDRESS, c, into a list providing
308 the value does not already exist.
311 extern "C" void lists_includeItemIntoList (lists_list l, void * c)
313 if (! (lists_isItemInList (l, c)))
315 lists_putItemIntoList (l, c);
321 removeItemFromList - removes a ADDRESS, c, from a list.
322 It assumes that this value only appears once.
325 extern "C" void lists_removeItemFromList (lists_list l, void * c)
327 lists_list__opaque p;
328 unsigned int i;
329 bool found;
331 if (l != NULL)
333 found = false;
334 p = static_cast<lists_list__opaque> (NULL);
335 do {
336 i = 1;
337 while ((i <= static_cast<lists_list__opaque> (l)->noOfelements) && (static_cast<lists_list__opaque> (l)->elements.array[i-1] != c))
339 i += 1;
341 if ((i <= static_cast<lists_list__opaque> (l)->noOfelements) && (static_cast<lists_list__opaque> (l)->elements.array[i-1] == c))
343 found = true;
345 else
347 p = static_cast<lists_list__opaque> (l);
348 l = static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next);
350 } while (! ((l == NULL) || found));
351 if (found)
353 removeItem (p, static_cast<lists_list__opaque> (l), i);
360 isItemInList - returns true if a ADDRESS, c, was found in list, l.
363 extern "C" bool lists_isItemInList (lists_list l, void * c)
365 unsigned int i;
367 do {
368 i = 1;
369 while (i <= static_cast<lists_list__opaque> (l)->noOfelements)
371 if (static_cast<lists_list__opaque> (l)->elements.array[i-1] == c)
373 return true;
375 else
377 i += 1;
380 l = static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next);
381 } while (! (l == NULL));
382 return false;
383 /* static analysis guarentees a RETURN statement will be used before here. */
384 __builtin_unreachable ();
389 foreachItemInListDo - calls procedure, P, foreach item in list, l.
392 extern "C" void lists_foreachItemInListDo (lists_list l, symbolKey_performOperation p)
394 unsigned int i;
395 unsigned int n;
397 n = lists_noOfItemsInList (l);
398 i = 1;
399 while (i <= n)
401 (*p.proc) (lists_getItemFromList (l, i));
402 i += 1;
408 duplicateList - returns a duplicate list derived from, l.
411 extern "C" lists_list lists_duplicateList (lists_list l)
413 lists_list__opaque m;
414 unsigned int n;
415 unsigned int i;
417 m = static_cast<lists_list__opaque> (lists_initList ());
418 n = lists_noOfItemsInList (l);
419 i = 1;
420 while (i <= n)
422 lists_putItemIntoList (static_cast<lists_list> (m), lists_getItemFromList (l, i));
423 i += 1;
425 return static_cast<lists_list> (m);
426 /* static analysis guarentees a RETURN statement will be used before here. */
427 __builtin_unreachable ();
430 extern "C" void _M2_lists_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
434 extern "C" void _M2_lists_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])