| 1 |
/* do not edit automatically generated by mc from symbolKey. */ |
| 2 |
/* symbolKey.mod provides binary tree operations for storing symbols. |
| 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 (FALSE) |
| 33 |
# define FALSE (1==0) |
| 34 |
# endif |
| 35 |
|
| 36 |
# include "GStorage.h" |
| 37 |
#if defined(__cplusplus) |
| 38 |
# undef NULL |
| 39 |
# define NULL 0 |
| 40 |
#endif |
| 41 |
#define _symbolKey_C |
| 42 |
|
| 43 |
#include "GsymbolKey.h" |
| 44 |
# include "GStorage.h" |
| 45 |
# include "GStrIO.h" |
| 46 |
# include "GNumberIO.h" |
| 47 |
# include "GDebug.h" |
| 48 |
# include "GnameKey.h" |
| 49 |
|
| 50 |
# define symbolKey_NulKey NULL |
| 51 |
typedef struct symbolKey_isSymbol_p symbolKey_isSymbol; |
| 52 |
|
| 53 |
typedef struct symbolKey_performOperation_p symbolKey_performOperation; |
| 54 |
|
| 55 |
typedef struct symbolKey__T1_r symbolKey__T1; |
| 56 |
|
| 57 |
typedef symbolKey__T1 *symbolKey_symbolTree__opaque; |
| 58 |
|
| 59 |
struct symbolKey__T1_r { |
| 60 |
nameKey_Name name; |
| 61 |
void * key; |
| 62 |
symbolKey_symbolTree__opaque left; |
| 63 |
symbolKey_symbolTree__opaque right; |
| 64 |
}; |
| 65 |
|
| 66 |
extern "C" symbolKey_symbolTree symbolKey_initTree (void); |
| 67 |
extern "C" void symbolKey_killTree (symbolKey_symbolTree *t); |
| 68 |
extern "C" void * symbolKey_getSymKey (symbolKey_symbolTree t, nameKey_Name name); |
| 69 |
extern "C" void symbolKey_putSymKey (symbolKey_symbolTree t, nameKey_Name name, void * key); |
| 70 |
|
| 71 |
/* |
| 72 |
delSymKey - deletes an entry in the binary tree. |
| 73 |
|
| 74 |
NB in order for this to work we must ensure that the InitTree sets |
| 75 |
both left and right to NIL. |
| 76 |
*/ |
| 77 |
|
| 78 |
extern "C" void symbolKey_delSymKey (symbolKey_symbolTree t, nameKey_Name name); |
| 79 |
|
| 80 |
/* |
| 81 |
isEmptyTree - returns true if symbolTree, t, is empty. |
| 82 |
*/ |
| 83 |
|
| 84 |
extern "C" bool symbolKey_isEmptyTree (symbolKey_symbolTree t); |
| 85 |
|
| 86 |
/* |
| 87 |
doesTreeContainAny - returns true if symbolTree, t, contains any |
| 88 |
symbols which in turn return true when procedure, |
| 89 |
p, is called with a symbol as its parameter. |
| 90 |
The symbolTree root is empty apart from the field, |
| 91 |
left, hence we need two procedures. |
| 92 |
*/ |
| 93 |
|
| 94 |
extern "C" bool symbolKey_doesTreeContainAny (symbolKey_symbolTree t, symbolKey_isSymbol p); |
| 95 |
|
| 96 |
/* |
| 97 |
foreachNodeDo - for each node in symbolTree, t, a procedure, p, |
| 98 |
is called with the node symbol as its parameter. |
| 99 |
The tree root node only contains a legal left pointer, |
| 100 |
therefore we need two procedures to examine this tree. |
| 101 |
*/ |
| 102 |
|
| 103 |
extern "C" void symbolKey_foreachNodeDo (symbolKey_symbolTree t, symbolKey_performOperation p); |
| 104 |
|
| 105 |
/* |
| 106 |
findNodeAndParentInTree - find a node, child, in a binary tree, t, with name equal to n. |
| 107 |
if an entry is found, father is set to the node above child. |
| 108 |
*/ |
| 109 |
|
| 110 |
static void findNodeAndParentInTree (symbolKey_symbolTree__opaque t, nameKey_Name n, symbolKey_symbolTree__opaque *child, symbolKey_symbolTree__opaque *father); |
| 111 |
|
| 112 |
/* |
| 113 |
searchForAny - performs the search required for doesTreeContainAny. |
| 114 |
The root node always contains a nul data value, |
| 115 |
therefore we must skip over it. |
| 116 |
*/ |
| 117 |
|
| 118 |
static bool searchForAny (symbolKey_symbolTree__opaque t, symbolKey_isSymbol p); |
| 119 |
|
| 120 |
/* |
| 121 |
searchAndDo - searches all the nodes in symbolTree, t, and |
| 122 |
calls procedure, p, with a node as its parameter. |
| 123 |
It traverse the tree in order. |
| 124 |
*/ |
| 125 |
|
| 126 |
static void searchAndDo (symbolKey_symbolTree__opaque t, symbolKey_performOperation p); |
| 127 |
|
| 128 |
|
| 129 |
/* |
| 130 |
findNodeAndParentInTree - find a node, child, in a binary tree, t, with name equal to n. |
| 131 |
if an entry is found, father is set to the node above child. |
| 132 |
*/ |
| 133 |
|
| 134 |
static void findNodeAndParentInTree (symbolKey_symbolTree__opaque t, nameKey_Name n, symbolKey_symbolTree__opaque *child, symbolKey_symbolTree__opaque *father) |
| 135 |
{ |
| 136 |
/* remember to skip the sentinal value and assign father and child */ |
| 137 |
(*father) = t; |
| 138 |
if (t == NULL) |
| 139 |
{ |
| 140 |
Debug_Halt ((const char *) "parameter t should never be NIL", 31, (const char *) "../../gcc/m2/mc/symbolKey.mod", 29, (const char *) "findNodeAndParentInTree", 23, 203); |
| 141 |
} |
| 142 |
(*child) = t->left; |
| 143 |
if ((*child) != NULL) |
| 144 |
{ |
| 145 |
do { |
| 146 |
if (n < (*child)->name) |
| 147 |
{ |
| 148 |
(*father) = (*child); |
| 149 |
(*child) = (*child)->left; |
| 150 |
} |
| 151 |
else if (n > (*child)->name) |
| 152 |
{ |
| 153 |
/* avoid dangling else. */ |
| 154 |
(*father) = (*child); |
| 155 |
(*child) = (*child)->right; |
| 156 |
} |
| 157 |
} while (! (((*child) == NULL) || (n == (*child)->name))); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
/* |
| 163 |
searchForAny - performs the search required for doesTreeContainAny. |
| 164 |
The root node always contains a nul data value, |
| 165 |
therefore we must skip over it. |
| 166 |
*/ |
| 167 |
|
| 168 |
static bool searchForAny (symbolKey_symbolTree__opaque t, symbolKey_isSymbol p) |
| 169 |
{ |
| 170 |
if (t == NULL) |
| 171 |
{ |
| 172 |
return false; |
| 173 |
} |
| 174 |
else |
| 175 |
{ |
| 176 |
return (((*p.proc) (t->key)) || (searchForAny (t->left, p))) || (searchForAny (t->right, p)); |
| 177 |
} |
| 178 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 179 |
__builtin_unreachable (); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
/* |
| 184 |
searchAndDo - searches all the nodes in symbolTree, t, and |
| 185 |
calls procedure, p, with a node as its parameter. |
| 186 |
It traverse the tree in order. |
| 187 |
*/ |
| 188 |
|
| 189 |
static void searchAndDo (symbolKey_symbolTree__opaque t, symbolKey_performOperation p) |
| 190 |
{ |
| 191 |
if (t != NULL) |
| 192 |
{ |
| 193 |
searchAndDo (t->right, p); |
| 194 |
(*p.proc) (t->key); |
| 195 |
searchAndDo (t->left, p); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
extern "C" symbolKey_symbolTree symbolKey_initTree (void) |
| 200 |
{ |
| 201 |
symbolKey_symbolTree__opaque t; |
| 202 |
|
| 203 |
Storage_ALLOCATE ((void **) &t, sizeof (symbolKey__T1)); /* The value entity */ |
| 204 |
t->left = static_cast<symbolKey_symbolTree__opaque> (NULL); |
| 205 |
t->right = static_cast<symbolKey_symbolTree__opaque> (NULL); |
| 206 |
return static_cast<symbolKey_symbolTree> (t); |
| 207 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 208 |
__builtin_unreachable (); |
| 209 |
} |
| 210 |
|
| 211 |
extern "C" void symbolKey_killTree (symbolKey_symbolTree *t) |
| 212 |
{ |
| 213 |
if ((*t) != NULL) |
| 214 |
{ |
| 215 |
symbolKey_killTree (reinterpret_cast<symbolKey_symbolTree *> (&static_cast<symbolKey_symbolTree__opaque> ((*t))->left)); |
| 216 |
symbolKey_killTree (reinterpret_cast<symbolKey_symbolTree *> (&static_cast<symbolKey_symbolTree__opaque> ((*t))->right)); |
| 217 |
Storage_DEALLOCATE ((void **) &(*t), sizeof (symbolKey__T1)); |
| 218 |
(*t) = static_cast<symbolKey_symbolTree> (NULL); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
extern "C" void * symbolKey_getSymKey (symbolKey_symbolTree t, nameKey_Name name) |
| 223 |
{ |
| 224 |
symbolKey_symbolTree__opaque father; |
| 225 |
symbolKey_symbolTree__opaque child; |
| 226 |
|
| 227 |
if (t == NULL) |
| 228 |
{ |
| 229 |
return symbolKey_NulKey; |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
findNodeAndParentInTree (static_cast<symbolKey_symbolTree__opaque> (t), name, &child, &father); |
| 234 |
if (child == NULL) |
| 235 |
{ |
| 236 |
return symbolKey_NulKey; |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
return child->key; |
| 241 |
} |
| 242 |
} |
| 243 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 244 |
__builtin_unreachable (); |
| 245 |
} |
| 246 |
|
| 247 |
extern "C" void symbolKey_putSymKey (symbolKey_symbolTree t, nameKey_Name name, void * key) |
| 248 |
{ |
| 249 |
symbolKey_symbolTree__opaque father; |
| 250 |
symbolKey_symbolTree__opaque child; |
| 251 |
|
| 252 |
findNodeAndParentInTree (static_cast<symbolKey_symbolTree__opaque> (t), name, &child, &father); |
| 253 |
if (child == NULL) |
| 254 |
{ |
| 255 |
/* no child found, now is name less than father or greater? */ |
| 256 |
if (father == t) |
| 257 |
{ |
| 258 |
/* empty tree, add it to the left branch of t */ |
| 259 |
Storage_ALLOCATE ((void **) &child, sizeof (symbolKey__T1)); |
| 260 |
father->left = child; |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
if (name < father->name) |
| 265 |
{ |
| 266 |
Storage_ALLOCATE ((void **) &child, sizeof (symbolKey__T1)); |
| 267 |
father->left = child; |
| 268 |
} |
| 269 |
else if (name > father->name) |
| 270 |
{ |
| 271 |
/* avoid dangling else. */ |
| 272 |
Storage_ALLOCATE ((void **) &child, sizeof (symbolKey__T1)); |
| 273 |
father->right = child; |
| 274 |
} |
| 275 |
} |
| 276 |
child->right = static_cast<symbolKey_symbolTree__opaque> (NULL); |
| 277 |
child->left = static_cast<symbolKey_symbolTree__opaque> (NULL); |
| 278 |
child->key = key; |
| 279 |
child->name = name; |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
Debug_Halt ((const char *) "symbol already stored", 21, (const char *) "../../gcc/m2/mc/symbolKey.mod", 29, (const char *) "putSymKey", 9, 119); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/* |
| 289 |
delSymKey - deletes an entry in the binary tree. |
| 290 |
|
| 291 |
NB in order for this to work we must ensure that the InitTree sets |
| 292 |
both left and right to NIL. |
| 293 |
*/ |
| 294 |
|
| 295 |
extern "C" void symbolKey_delSymKey (symbolKey_symbolTree t, nameKey_Name name) |
| 296 |
{ |
| 297 |
symbolKey_symbolTree__opaque i; |
| 298 |
symbolKey_symbolTree__opaque child; |
| 299 |
symbolKey_symbolTree__opaque father; |
| 300 |
|
| 301 |
findNodeAndParentInTree (static_cast<symbolKey_symbolTree__opaque> (t), name, &child, &father); /* find father and child of the node */ |
| 302 |
if ((child != NULL) && (child->name == name)) |
| 303 |
{ |
| 304 |
/* Have found the node to be deleted */ |
| 305 |
if (father->right == child) |
| 306 |
{ |
| 307 |
/* most branch of child^.left. */ |
| 308 |
if (child->left != NULL) |
| 309 |
{ |
| 310 |
/* Scan for right most node of child^.left */ |
| 311 |
i = child->left; |
| 312 |
while (i->right != NULL) |
| 313 |
{ |
| 314 |
i = i->right; |
| 315 |
} |
| 316 |
i->right = child->right; |
| 317 |
father->right = child->left; |
| 318 |
} |
| 319 |
else |
| 320 |
{ |
| 321 |
/* (as in a single linked list) to child^.right */ |
| 322 |
father->right = child->right; |
| 323 |
} |
| 324 |
Storage_DEALLOCATE ((void **) &child, sizeof (symbolKey__T1)); |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
/* branch of child^.right */ |
| 329 |
if (child->right != NULL) |
| 330 |
{ |
| 331 |
/* Scan for left most node of child^.right */ |
| 332 |
i = child->right; |
| 333 |
while (i->left != NULL) |
| 334 |
{ |
| 335 |
i = i->left; |
| 336 |
} |
| 337 |
i->left = child->left; |
| 338 |
father->left = child->right; |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
/* (as in a single linked list) to child^.left. */ |
| 343 |
father->left = child->left; |
| 344 |
} |
| 345 |
Storage_DEALLOCATE ((void **) &child, sizeof (symbolKey__T1)); |
| 346 |
} |
| 347 |
} |
| 348 |
else |
| 349 |
{ |
| 350 |
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/mc/symbolKey.mod", 29, (const char *) "delSymKey", 9, 186); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
/* |
| 356 |
isEmptyTree - returns true if symbolTree, t, is empty. |
| 357 |
*/ |
| 358 |
|
| 359 |
extern "C" bool symbolKey_isEmptyTree (symbolKey_symbolTree t) |
| 360 |
{ |
| 361 |
return static_cast<symbolKey_symbolTree__opaque> (t)->left == NULL; |
| 362 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 363 |
__builtin_unreachable (); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
/* |
| 368 |
doesTreeContainAny - returns true if symbolTree, t, contains any |
| 369 |
symbols which in turn return true when procedure, |
| 370 |
p, is called with a symbol as its parameter. |
| 371 |
The symbolTree root is empty apart from the field, |
| 372 |
left, hence we need two procedures. |
| 373 |
*/ |
| 374 |
|
| 375 |
extern "C" bool symbolKey_doesTreeContainAny (symbolKey_symbolTree t, symbolKey_isSymbol p) |
| 376 |
{ |
| 377 |
return searchForAny (static_cast<symbolKey_symbolTree__opaque> (t)->left, p); |
| 378 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 379 |
__builtin_unreachable (); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
/* |
| 384 |
foreachNodeDo - for each node in symbolTree, t, a procedure, p, |
| 385 |
is called with the node symbol as its parameter. |
| 386 |
The tree root node only contains a legal left pointer, |
| 387 |
therefore we need two procedures to examine this tree. |
| 388 |
*/ |
| 389 |
|
| 390 |
extern "C" void symbolKey_foreachNodeDo (symbolKey_symbolTree t, symbolKey_performOperation p) |
| 391 |
{ |
| 392 |
searchAndDo (static_cast<symbolKey_symbolTree__opaque> (t)->left, p); |
| 393 |
} |
| 394 |
|
| 395 |
extern "C" void _M2_symbolKey_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 396 |
{ |
| 397 |
} |
| 398 |
|
| 399 |
extern "C" void _M2_symbolKey_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 400 |
{ |
| 401 |
} |