Debian Codegraph @

gcc/m2/pge-boot/GSymbolKey.cc

raw
1 /* do not edit automatically generated by mc from SymbolKey. */
2 /* SymbolKey.mod binary tree operations for storing symbols.
3
4 Copyright (C) 2001-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 <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
29
30 # if !defined (FALSE)
31 # define FALSE (1==0)
32 # endif
33
34 #include <stddef.h>
35 # include "GStorage.h"
36 #if defined(__cplusplus)
37 # undef NULL
38 # define NULL 0
39 #endif
40 #define _SymbolKey_C
41
42 #include "GSymbolKey.h"
43 # include "GStorage.h"
44 # include "GStrIO.h"
45 # include "GNumberIO.h"
46 # include "GNameKey.h"
47 # include "GAssertion.h"
48 # include "GDebug.h"
49
50 # define SymbolKey_NulKey 0
51 typedef struct SymbolKey_IsSymbol_p SymbolKey_IsSymbol;
52
53 typedef struct SymbolKey_PerformOperation_p SymbolKey_PerformOperation;
54
55 typedef struct SymbolKey_Node_r SymbolKey_Node;
56
57 typedef SymbolKey_Node *SymbolKey_SymbolTree__opaque;
58
59 struct SymbolKey_Node_r {
60 NameKey_Name KeyName;
61 unsigned int KeySym;
62 SymbolKey_SymbolTree__opaque Left;
63 SymbolKey_SymbolTree__opaque Right;
64 };
65
66 extern "C" void SymbolKey_InitTree (SymbolKey_SymbolTree *t);
67 extern "C" void SymbolKey_KillTree (SymbolKey_SymbolTree *t);
68
69 /*
70 ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
71 */
72
73 extern "C" unsigned int SymbolKey_GetSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey);
74
75 /*
76 ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
77 */
78
79 extern "C" void SymbolKey_PutSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey, unsigned int SymKey);
80
81 /*
82 DelSymKey - deletes an entry in the binary tree.
83
84 NB in order for this to work we must ensure that the InitTree sets
85 both Left and Right to NIL.
86 */
87
88 extern "C" void SymbolKey_DelSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey);
89
90 /*
91 IsEmptyTree - returns true if SymbolTree, t, is empty.
92 */
93
94 extern "C" bool SymbolKey_IsEmptyTree (SymbolKey_SymbolTree t);
95
96 /*
97 DoesTreeContainAny - returns true if SymbolTree, t, contains any
98 symbols which in turn return true when procedure,
99 P, is called with a symbol as its parameter.
100 The SymbolTree root is empty apart from the field,
101 Left, hence we need two procedures.
102 */
103
104 extern "C" bool SymbolKey_DoesTreeContainAny (SymbolKey_SymbolTree t, SymbolKey_IsSymbol P);
105
106 /*
107 ForeachNodeDo - for each node in SymbolTree, t, a procedure, P,
108 is called with the node symbol as its parameter.
109 The tree root node only contains a legal Left pointer,
110 therefore we need two procedures to examine this tree.
111 */
112
113 extern "C" void SymbolKey_ForeachNodeDo (SymbolKey_SymbolTree t, SymbolKey_PerformOperation P);
114
115 /*
116 ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
117 */
118
119 extern "C" bool SymbolKey_ContainsSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey);
120
121 /*
122 NoOfNodes - returns the number of nodes in the tree t.
123 */
124
125 extern "C" unsigned int SymbolKey_NoOfNodes (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition);
126
127 /*
128 ForeachNodeConditionDo - traverse the tree t and for any node which satisfied
129 condition call P.
130 */
131
132 extern "C" void SymbolKey_ForeachNodeConditionDo (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition, SymbolKey_PerformOperation P);
133
134 /*
135 FindNodeParentInTree - find a node, child, in a binary tree, t, with name equal to n.
136 if an entry is found, parent is set to the node above child.
137 */
138
139 static void FindNodeParentInTree (SymbolKey_SymbolTree__opaque t, NameKey_Name n, SymbolKey_SymbolTree__opaque *child, SymbolKey_SymbolTree__opaque *parent);
140
141 /*
142 SearchForAny - performs the search required for DoesTreeContainAny.
143 The root node always contains a nul data value,
144 therefore we must skip over it.
145 */
146
147 static bool SearchForAny (SymbolKey_SymbolTree__opaque t, SymbolKey_IsSymbol P);
148
149 /*
150 SearchAndDo - searches all the nodes in SymbolTree, t, and
151 calls procedure, P, with a node as its parameter.
152 It traverse the tree in order.
153 */
154
155 static void SearchAndDo (SymbolKey_SymbolTree__opaque t, SymbolKey_PerformOperation P);
156
157 /*
158 CountNodes - wrapper for NoOfNodes.
159 */
160
161 static unsigned int CountNodes (SymbolKey_SymbolTree__opaque t, SymbolKey_IsSymbol condition, unsigned int count);
162
163 /*
164 SearchConditional - wrapper for ForeachNodeConditionDo.
165 */
166
167 static void SearchConditional (SymbolKey_SymbolTree__opaque t, SymbolKey_IsSymbol condition, SymbolKey_PerformOperation P);
168
169
170 /*
171 FindNodeParentInTree - find a node, child, in a binary tree, t, with name equal to n.
172 if an entry is found, parent is set to the node above child.
173 */
174
175 static void FindNodeParentInTree (SymbolKey_SymbolTree__opaque t, NameKey_Name n, SymbolKey_SymbolTree__opaque *child, SymbolKey_SymbolTree__opaque *parent)
176 {
177 /* remember to skip the sentinal value and assign parent and child */
178 (*parent) = t;
179 if (t == NULL)
180 {
181 Debug_Halt ((const char *) "parameter t should never be NIL", 31, (const char *) "../../gcc/m2/gm2-compiler/SymbolKey.mod", 39, (const char *) "FindNodeParentInTree", 20, 241);
182 }
183 Assertion_Assert (t->Right == NULL);
184 (*child) = t->Left;
185 if ((*child) != NULL)
186 {
187 do {
188 if (n < (*child)->KeyName)
189 {
190 (*parent) = (*child);
191 (*child) = (*child)->Left;
192 }
193 else if (n > (*child)->KeyName)
194 {
195 /* avoid dangling else. */
196 (*parent) = (*child);
197 (*child) = (*child)->Right;
198 }
199 } while (! (((*child) == NULL) || (n == (*child)->KeyName)));
200 }
201 }
202
203
204 /*
205 SearchForAny - performs the search required for DoesTreeContainAny.
206 The root node always contains a nul data value,
207 therefore we must skip over it.
208 */
209
210 static bool SearchForAny (SymbolKey_SymbolTree__opaque t, SymbolKey_IsSymbol P)
211 {
212 if (t == NULL)
213 {
214 return false;
215 }
216 else
217 {
218 return (((*P.proc) (t->KeySym)) || (SearchForAny (t->Left, P))) || (SearchForAny (t->Right, P));
219 }
220 /* static analysis guarentees a RETURN statement will be used before here. */
221 __builtin_unreachable ();
222 }
223
224
225 /*
226 SearchAndDo - searches all the nodes in SymbolTree, t, and
227 calls procedure, P, with a node as its parameter.
228 It traverse the tree in order.
229 */
230
231 static void SearchAndDo (SymbolKey_SymbolTree__opaque t, SymbolKey_PerformOperation P)
232 {
233 if (t != NULL)
234 {
235 SearchAndDo (t->Right, P);
236 (*P.proc) (t->KeySym);
237 SearchAndDo (t->Left, P);
238 }
239 }
240
241
242 /*
243 CountNodes - wrapper for NoOfNodes.
244 */
245
246 static unsigned int CountNodes (SymbolKey_SymbolTree__opaque t, SymbolKey_IsSymbol condition, unsigned int count)
247 {
248 if (t != NULL)
249 {
250 if ((*condition.proc) (t->KeySym))
251 {
252 count += 1;
253 }
254 count = CountNodes (t->Left, condition, count);
255 count = CountNodes (t->Right, condition, count);
256 }
257 return count;
258 /* static analysis guarentees a RETURN statement will be used before here. */
259 __builtin_unreachable ();
260 }
261
262
263 /*
264 SearchConditional - wrapper for ForeachNodeConditionDo.
265 */
266
267 static void SearchConditional (SymbolKey_SymbolTree__opaque t, SymbolKey_IsSymbol condition, SymbolKey_PerformOperation P)
268 {
269 if (t != NULL)
270 {
271 SearchConditional (t->Right, condition, P);
272 if ((t->KeySym != 0) && ((*condition.proc) (t->KeySym)))
273 {
274 (*P.proc) (t->KeySym);
275 }
276 SearchConditional (t->Left, condition, P);
277 }
278 }
279
280 extern "C" void SymbolKey_InitTree (SymbolKey_SymbolTree *t)
281 {
282 Storage_ALLOCATE ((void **) &(*t), sizeof (SymbolKey_Node)); /* The value entity */
283 static_cast<SymbolKey_SymbolTree__opaque> ((*t))->Left = static_cast<SymbolKey_SymbolTree__opaque> (NULL);
284 static_cast<SymbolKey_SymbolTree__opaque> ((*t))->Right = static_cast<SymbolKey_SymbolTree__opaque> (NULL);
285 }
286
287 extern "C" void SymbolKey_KillTree (SymbolKey_SymbolTree *t)
288 {
289 /*
290 we used to get problems compiling KillTree below - so it was split
291 into the two procedures below.
292
293
294 PROCEDURE KillTree (VAR t: SymbolTree) ;
295 BEGIN
296 IF t#NIL
297 THEN
298 Kill(t) ; Would like to place Kill in here but the compiler
299 gives a type incompatible error... so i've split
300 the procedure into two. - Problem i think with
301 VAR t at the top?
302 t := NIL
303 END
304 END KillTree ;
305
306
307 PROCEDURE Kill (t: SymbolTree) ;
308 BEGIN
309 IF t#NIL
310 THEN
311 Kill(t^.Left) ;
312 Kill(t^.Right) ;
313 DISPOSE(t)
314 END
315 END Kill ;
316 */
317 if ((*t) != NULL)
318 {
319 SymbolKey_KillTree (reinterpret_cast<SymbolKey_SymbolTree *> (&static_cast<SymbolKey_SymbolTree__opaque> ((*t))->Left));
320 SymbolKey_KillTree (reinterpret_cast<SymbolKey_SymbolTree *> (&static_cast<SymbolKey_SymbolTree__opaque> ((*t))->Right));
321 Storage_DEALLOCATE ((void **) &(*t), sizeof (SymbolKey_Node));
322 (*t) = static_cast<SymbolKey_SymbolTree> (NULL);
323 }
324 }
325
326
327 /*
328 ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
329 */
330
331 extern "C" unsigned int SymbolKey_GetSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey)
332 {
333 SymbolKey_SymbolTree__opaque father;
334 SymbolKey_SymbolTree__opaque child;
335
336 FindNodeParentInTree (static_cast<SymbolKey_SymbolTree__opaque> (t), NameKey, &child, &father);
337 if (child == NULL)
338 {
339 return static_cast<unsigned int> (SymbolKey_NulKey);
340 }
341 else
342 {
343 return child->KeySym;
344 }
345 /* static analysis guarentees a RETURN statement will be used before here. */
346 __builtin_unreachable ();
347 }
348
349
350 /*
351 ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
352 */
353
354 extern "C" void SymbolKey_PutSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey, unsigned int SymKey)
355 {
356 SymbolKey_SymbolTree__opaque father;
357 SymbolKey_SymbolTree__opaque child;
358
359 FindNodeParentInTree (static_cast<SymbolKey_SymbolTree__opaque> (t), NameKey, &child, &father);
360 if (child == NULL)
361 {
362 /* no child found, now is NameKey less than father or greater? */
363 if (father == t)
364 {
365 /* empty tree, add it to the left branch of t */
366 Storage_ALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
367 father->Left = child;
368 }
369 else
370 {
371 if (NameKey < father->KeyName)
372 {
373 Storage_ALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
374 father->Left = child;
375 }
376 else if (NameKey > father->KeyName)
377 {
378 /* avoid dangling else. */
379 Storage_ALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
380 father->Right = child;
381 }
382 }
383 child->Right = static_cast<SymbolKey_SymbolTree__opaque> (NULL);
384 child->Left = static_cast<SymbolKey_SymbolTree__opaque> (NULL);
385 child->KeySym = SymKey;
386 child->KeyName = NameKey;
387 }
388 else
389 {
390 Debug_Halt ((const char *) "symbol already stored", 21, (const char *) "../../gcc/m2/gm2-compiler/SymbolKey.mod", 39, (const char *) "PutSymKey", 9, 156);
391 }
392 }
393
394
395 /*
396 DelSymKey - deletes an entry in the binary tree.
397
398 NB in order for this to work we must ensure that the InitTree sets
399 both Left and Right to NIL.
400 */
401
402 extern "C" void SymbolKey_DelSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey)
403 {
404 SymbolKey_SymbolTree__opaque i;
405 SymbolKey_SymbolTree__opaque child;
406 SymbolKey_SymbolTree__opaque father;
407
408 FindNodeParentInTree (static_cast<SymbolKey_SymbolTree__opaque> (t), NameKey, &child, &father); /* find father and child of the node */
409 if ((child != NULL) && (child->KeyName == NameKey))
410 {
411 /* Have found the node to be deleted */
412 if (father->Right == child)
413 {
414 /* most branch of child^.Left. */
415 if (child->Left != NULL)
416 {
417 /* Scan for Right most node of child^.Left */
418 i = child->Left;
419 while (i->Right != NULL)
420 {
421 i = i->Right;
422 }
423 i->Right = child->Right;
424 father->Right = child->Left;
425 }
426 else
427 {
428 /* (as in a single linked list) to child^.Right */
429 father->Right = child->Right;
430 }
431 Storage_DEALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
432 }
433 else
434 {
435 /* branch of child^.Right */
436 if (child->Right != NULL)
437 {
438 /* Scan for Left most node of child^.Right */
439 i = child->Right;
440 while (i->Left != NULL)
441 {
442 i = i->Left;
443 }
444 i->Left = child->Left;
445 father->Left = child->Right;
446 }
447 else
448 {
449 /* (as in a single linked list) to child^.Left. */
450 father->Left = child->Left;
451 }
452 Storage_DEALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
453 }
454 }
455 else
456 {
457 Debug_Halt ((const char *) "trying to delete a symbol that is not in the tree - the compiler never expects this to occur", 92, (const char *) "../../gcc/m2/gm2-compiler/SymbolKey.mod", 39, (const char *) "DelSymKey", 9, 223);
458 }
459 }
460
461
462 /*
463 IsEmptyTree - returns true if SymbolTree, t, is empty.
464 */
465
466 extern "C" bool SymbolKey_IsEmptyTree (SymbolKey_SymbolTree t)
467 {
468 return static_cast<SymbolKey_SymbolTree__opaque> (t)->Left == NULL;
469 /* static analysis guarentees a RETURN statement will be used before here. */
470 __builtin_unreachable ();
471 }
472
473
474 /*
475 DoesTreeContainAny - returns true if SymbolTree, t, contains any
476 symbols which in turn return true when procedure,
477 P, is called with a symbol as its parameter.
478 The SymbolTree root is empty apart from the field,
479 Left, hence we need two procedures.
480 */
481
482 extern "C" bool SymbolKey_DoesTreeContainAny (SymbolKey_SymbolTree t, SymbolKey_IsSymbol P)
483 {
484 return SearchForAny (static_cast<SymbolKey_SymbolTree__opaque> (t)->Left, P);
485 /* static analysis guarentees a RETURN statement will be used before here. */
486 __builtin_unreachable ();
487 }
488
489
490 /*
491 ForeachNodeDo - for each node in SymbolTree, t, a procedure, P,
492 is called with the node symbol as its parameter.
493 The tree root node only contains a legal Left pointer,
494 therefore we need two procedures to examine this tree.
495 */
496
497 extern "C" void SymbolKey_ForeachNodeDo (SymbolKey_SymbolTree t, SymbolKey_PerformOperation P)
498 {
499 SearchAndDo (static_cast<SymbolKey_SymbolTree__opaque> (t)->Left, P);
500 }
501
502
503 /*
504 ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
505 */
506
507 extern "C" bool SymbolKey_ContainsSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey)
508 {
509 SymbolKey_SymbolTree__opaque father;
510 SymbolKey_SymbolTree__opaque child;
511
512 FindNodeParentInTree (static_cast<SymbolKey_SymbolTree__opaque> (t), NameKey, &child, &father);
513 return child != NULL;
514 /* static analysis guarentees a RETURN statement will be used before here. */
515 __builtin_unreachable ();
516 }
517
518
519 /*
520 NoOfNodes - returns the number of nodes in the tree t.
521 */
522
523 extern "C" unsigned int SymbolKey_NoOfNodes (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition)
524 {
525 return CountNodes (static_cast<SymbolKey_SymbolTree__opaque> (t)->Left, condition, 0);
526 /* static analysis guarentees a RETURN statement will be used before here. */
527 __builtin_unreachable ();
528 }
529
530
531 /*
532 ForeachNodeConditionDo - traverse the tree t and for any node which satisfied
533 condition call P.
534 */
535
536 extern "C" void SymbolKey_ForeachNodeConditionDo (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition, SymbolKey_PerformOperation P)
537 {
538 if (t != NULL)
539 {
540 Assertion_Assert (static_cast<SymbolKey_SymbolTree__opaque> (t)->Right == NULL);
541 SearchConditional (static_cast<SymbolKey_SymbolTree__opaque> (t)->Left, condition, P);
542 }
543 }
544
545 extern "C" void _M2_SymbolKey_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
546 {
547 }
548
549 extern "C" void _M2_SymbolKey_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
550 {
551 }