Debian Codegraph @

gcc/m2/mc-boot/Galists.cc

raw
1 /* do not edit automatically generated by mc from alists. */
2 /* alists.mod address lists module.
3
4 Copyright (C) 2015-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Modula-2; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include <stdbool.h>
26 # if !defined (PROC_D)
27 # define PROC_D
28 typedef void (*PROC_t) (void);
29 typedef struct { PROC_t proc; } PROC;
30 # endif
31
32 # if !defined (TRUE)
33 # define TRUE (1==1)
34 # endif
35
36 # if !defined (FALSE)
37 # define FALSE (1==0)
38 # endif
39
40 # include "GStorage.h"
41 #if defined(__cplusplus)
42 # undef NULL
43 # define NULL 0
44 #endif
45 #define _alists_C
46
47 #include "Galists.h"
48 # include "GStorage.h"
49
50 typedef struct alists_performOperation_p alists_performOperation;
51
52 # define MaxnoOfelements 5
53 typedef struct alists__T1_r alists__T1;
54
55 typedef struct alists__T2_a alists__T2;
56
57 typedef alists__T1 *alists_alist__opaque;
58
59 struct alists__T2_a { void * array[MaxnoOfelements-1+1]; };
60 struct alists__T1_r {
61 unsigned int noOfelements;
62 alists__T2 elements;
63 alists_alist__opaque next;
64 };
65
66
67 /*
68 initList - creates a new alist, l.
69 */
70
71 extern "C" alists_alist alists_initList (void);
72
73 /*
74 killList - deletes the complete alist, l.
75 */
76
77 extern "C" void alists_killList (alists_alist *l);
78
79 /*
80 putItemIntoList - places an ADDRESS, c, into alist, l.
81 */
82
83 extern "C" void alists_putItemIntoList (alists_alist l, void * c);
84
85 /*
86 getItemFromList - retrieves the nth WORD from alist, l.
87 */
88
89 extern "C" void * alists_getItemFromList (alists_alist l, unsigned int n);
90
91 /*
92 getIndexOfList - returns the index for WORD, c, in alist, l.
93 If more than one WORD, c, exists the index
94 for the first is returned.
95 */
96
97 extern "C" unsigned int alists_getIndexOfList (alists_alist l, void * c);
98
99 /*
100 noOfItemsInList - returns the number of items in alist, l.
101 */
102
103 extern "C" unsigned int alists_noOfItemsInList (alists_alist l);
104
105 /*
106 includeItemIntoList - adds an ADDRESS, c, into a alist providing
107 the value does not already exist.
108 */
109
110 extern "C" void alists_includeItemIntoList (alists_alist l, void * c);
111
112 /*
113 removeItemFromList - removes a ADDRESS, c, from a alist.
114 It assumes that this value only appears once.
115 */
116
117 extern "C" void alists_removeItemFromList (alists_alist l, void * c);
118
119 /*
120 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
121 */
122
123 extern "C" bool alists_isItemInList (alists_alist l, void * c);
124
125 /*
126 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
127 */
128
129 extern "C" void alists_foreachItemInListDo (alists_alist l, alists_performOperation p);
130
131 /*
132 duplicateList - returns a duplicate alist derived from, l.
133 */
134
135 extern "C" alists_alist alists_duplicateList (alists_alist l);
136
137 /*
138 equalList - returns TRUE if left contains the same information as right.
139 */
140
141 extern "C" bool alists_equalList (alists_alist left, alists_alist right);
142
143 /*
144 removeItem - remove an element at index, i, from the alist data type.
145 */
146
147 static void removeItem (alists_alist__opaque p, alists_alist__opaque l, unsigned int i);
148
149
150 /*
151 removeItem - remove an element at index, i, from the alist data type.
152 */
153
154 static void removeItem (alists_alist__opaque p, alists_alist__opaque l, unsigned int i)
155 {
156 l->noOfelements -= 1;
157 while (i <= l->noOfelements)
158 {
159 l->elements.array[i-1] = l->elements.array[i+1-1];
160 i += 1;
161 }
162 if ((l->noOfelements == 0) && (p != NULL))
163 {
164 p->next = l->next;
165 Storage_DEALLOCATE ((void **) &l, sizeof (alists__T1));
166 }
167 }
168
169
170 /*
171 initList - creates a new alist, l.
172 */
173
174 extern "C" alists_alist alists_initList (void)
175 {
176 alists_alist__opaque l;
177
178 Storage_ALLOCATE ((void **) &l, sizeof (alists__T1));
179 l->noOfelements = 0;
180 l->next = static_cast<alists_alist__opaque> (NULL);
181 return static_cast<alists_alist> (l);
182 /* static analysis guarentees a RETURN statement will be used before here. */
183 __builtin_unreachable ();
184 }
185
186
187 /*
188 killList - deletes the complete alist, l.
189 */
190
191 extern "C" void alists_killList (alists_alist *l)
192 {
193 if ((*l) != NULL)
194 {
195 if (static_cast<alists_alist__opaque> ((*l))->next != NULL)
196 {
197 alists_killList (reinterpret_cast<alists_alist *> (&static_cast<alists_alist__opaque> ((*l))->next));
198 }
199 Storage_DEALLOCATE ((void **) &(*l), sizeof (alists__T1));
200 }
201 }
202
203
204 /*
205 putItemIntoList - places an ADDRESS, c, into alist, l.
206 */
207
208 extern "C" void alists_putItemIntoList (alists_alist l, void * c)
209 {
210 if (static_cast<alists_alist__opaque> (l)->noOfelements < MaxnoOfelements)
211 {
212 static_cast<alists_alist__opaque> (l)->noOfelements += 1;
213 static_cast<alists_alist__opaque> (l)->elements.array[static_cast<alists_alist__opaque> (l)->noOfelements-1] = c;
214 }
215 else if (static_cast<alists_alist__opaque> (l)->next != NULL)
216 {
217 /* avoid dangling else. */
218 alists_putItemIntoList (static_cast<alists_alist> (static_cast<alists_alist__opaque> (l)->next), c);
219 }
220 else
221 {
222 /* avoid dangling else. */
223 static_cast<alists_alist__opaque> (l)->next = static_cast<alists_alist__opaque> (alists_initList ());
224 alists_putItemIntoList (static_cast<alists_alist> (static_cast<alists_alist__opaque> (l)->next), c);
225 }
226 }
227
228
229 /*
230 getItemFromList - retrieves the nth WORD from alist, l.
231 */
232
233 extern "C" void * alists_getItemFromList (alists_alist l, unsigned int n)
234 {
235 while (l != NULL)
236 {
237 if (n <= static_cast<alists_alist__opaque> (l)->noOfelements)
238 {
239 return static_cast<alists_alist__opaque> (l)->elements.array[n-1];
240 }
241 else
242 {
243 n -= static_cast<alists_alist__opaque> (l)->noOfelements;
244 }
245 l = static_cast<alists_alist> (static_cast<alists_alist__opaque> (l)->next);
246 }
247 return static_cast<void *> (0);
248 /* static analysis guarentees a RETURN statement will be used before here. */
249 __builtin_unreachable ();
250 }
251
252
253 /*
254 getIndexOfList - returns the index for WORD, c, in alist, l.
255 If more than one WORD, c, exists the index
256 for the first is returned.
257 */
258
259 extern "C" unsigned int alists_getIndexOfList (alists_alist l, void * c)
260 {
261 unsigned int i;
262
263 if (l == NULL)
264 {
265 return 0;
266 }
267 else
268 {
269 i = 1;
270 while (i <= static_cast<alists_alist__opaque> (l)->noOfelements)
271 {
272 if (static_cast<alists_alist__opaque> (l)->elements.array[i-1] == c)
273 {
274 return i;
275 }
276 else
277 {
278 i += 1;
279 }
280 }
281 return static_cast<alists_alist__opaque> (l)->noOfelements+(alists_getIndexOfList (static_cast<alists_alist> (static_cast<alists_alist__opaque> (l)->next), c));
282 }
283 /* static analysis guarentees a RETURN statement will be used before here. */
284 __builtin_unreachable ();
285 }
286
287
288 /*
289 noOfItemsInList - returns the number of items in alist, l.
290 */
291
292 extern "C" unsigned int alists_noOfItemsInList (alists_alist l)
293 {
294 unsigned int t;
295
296 if (l == NULL)
297 {
298 return 0;
299 }
300 else
301 {
302 t = 0;
303 do {
304 t += static_cast<alists_alist__opaque> (l)->noOfelements;
305 l = static_cast<alists_alist> (static_cast<alists_alist__opaque> (l)->next);
306 } while (! (l == NULL));
307 return t;
308 }
309 /* static analysis guarentees a RETURN statement will be used before here. */
310 __builtin_unreachable ();
311 }
312
313
314 /*
315 includeItemIntoList - adds an ADDRESS, c, into a alist providing
316 the value does not already exist.
317 */
318
319 extern "C" void alists_includeItemIntoList (alists_alist l, void * c)
320 {
321 if (! (alists_isItemInList (l, c)))
322 {
323 alists_putItemIntoList (l, c);
324 }
325 }
326
327
328 /*
329 removeItemFromList - removes a ADDRESS, c, from a alist.
330 It assumes that this value only appears once.
331 */
332
333 extern "C" void alists_removeItemFromList (alists_alist l, void * c)
334 {
335 alists_alist__opaque p;
336 unsigned int i;
337 bool found;
338
339 if (l != NULL)
340 {
341 found = false;
342 p = static_cast<alists_alist__opaque> (NULL);
343 do {
344 i = 1;
345 while ((i <= static_cast<alists_alist__opaque> (l)->noOfelements) && (static_cast<alists_alist__opaque> (l)->elements.array[i-1] != c))
346 {
347 i += 1;
348 }
349 if ((i <= static_cast<alists_alist__opaque> (l)->noOfelements) && (static_cast<alists_alist__opaque> (l)->elements.array[i-1] == c))
350 {
351 found = true;
352 }
353 else
354 {
355 p = static_cast<alists_alist__opaque> (l);
356 l = static_cast<alists_alist> (static_cast<alists_alist__opaque> (l)->next);
357 }
358 } while (! ((l == NULL) || found));
359 if (found)
360 {
361 removeItem (p, static_cast<alists_alist__opaque> (l), i);
362 }
363 }
364 }
365
366
367 /*
368 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
369 */
370
371 extern "C" bool alists_isItemInList (alists_alist l, void * c)
372 {
373 unsigned int i;
374
375 do {
376 i = 1;
377 while (i <= static_cast<alists_alist__opaque> (l)->noOfelements)
378 {
379 if (static_cast<alists_alist__opaque> (l)->elements.array[i-1] == c)
380 {
381 return true;
382 }
383 else
384 {
385 i += 1;
386 }
387 }
388 l = static_cast<alists_alist> (static_cast<alists_alist__opaque> (l)->next);
389 } while (! (l == NULL));
390 return false;
391 /* static analysis guarentees a RETURN statement will be used before here. */
392 __builtin_unreachable ();
393 }
394
395
396 /*
397 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
398 */
399
400 extern "C" void alists_foreachItemInListDo (alists_alist l, alists_performOperation p)
401 {
402 unsigned int i;
403 unsigned int n;
404
405 n = alists_noOfItemsInList (l);
406 i = 1;
407 while (i <= n)
408 {
409 (*p.proc) (alists_getItemFromList (l, i));
410 i += 1;
411 }
412 }
413
414
415 /*
416 duplicateList - returns a duplicate alist derived from, l.
417 */
418
419 extern "C" alists_alist alists_duplicateList (alists_alist l)
420 {
421 alists_alist__opaque m;
422 unsigned int n;
423 unsigned int i;
424
425 m = static_cast<alists_alist__opaque> (alists_initList ());
426 n = alists_noOfItemsInList (l);
427 i = 1;
428 while (i <= n)
429 {
430 alists_putItemIntoList (static_cast<alists_alist> (m), alists_getItemFromList (l, i));
431 i += 1;
432 }
433 return static_cast<alists_alist> (m);
434 /* static analysis guarentees a RETURN statement will be used before here. */
435 __builtin_unreachable ();
436 }
437
438
439 /*
440 equalList - returns TRUE if left contains the same information as right.
441 */
442
443 extern "C" bool alists_equalList (alists_alist left, alists_alist right)
444 {
445 unsigned int leftn;
446 unsigned int rightn;
447 unsigned int i;
448
449 leftn = alists_noOfItemsInList (left);
450 rightn = alists_noOfItemsInList (right);
451 if (leftn == rightn)
452 {
453 i = 1;
454 while (i <= leftn)
455 {
456 if (alists_isItemInList (right, alists_getItemFromList (left, i)))
457 {
458 i += 1;
459 }
460 else
461 {
462 return false;
463 }
464 }
465 }
466 else
467 {
468 return false;
469 }
470 return true;
471 /* static analysis guarentees a RETURN statement will be used before here. */
472 __builtin_unreachable ();
473 }
474
475 extern "C" void _M2_alists_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
476 {
477 }
478
479 extern "C" void _M2_alists_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
480 {
481 }