Debian Codegraph @

gcc/m2/mc-boot/GnameKey.cc

raw
1 /* do not edit automatically generated by mc from nameKey. */
2 /* nameKey.mod provides a dynamic binary tree name to key.
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 # include "Gmcrts.h"
42 #if defined(__cplusplus)
43 # undef NULL
44 # define NULL 0
45 #endif
46 #define _nameKey_C
47
48 #include "GnameKey.h"
49 # include "GSYSTEM.h"
50 # include "GStorage.h"
51 # include "GIndexing.h"
52 # include "GStrIO.h"
53 # include "GStdIO.h"
54 # include "GNumberIO.h"
55 # include "GStrLib.h"
56 # include "Glibc.h"
57 # include "GASCII.h"
58 # include "GM2RTS.h"
59
60 # define nameKey_NulName 0
61 typedef unsigned int nameKey_Name;
62
63 typedef struct nameKey__T1_r nameKey__T1;
64
65 typedef char *nameKey_ptrToChar;
66
67 typedef nameKey__T1 *nameKey_nameNode;
68
69 typedef enum {nameKey_less, nameKey_equal, nameKey_greater} nameKey_comparison;
70
71 struct nameKey__T1_r {
72 nameKey_ptrToChar data;
73 nameKey_Name key;
74 nameKey_nameNode left;
75 nameKey_nameNode right;
76 };
77
78 static nameKey_nameNode binaryTree;
79 static Indexing_Index keyIndex;
80 static unsigned int lastIndice;
81
82 /*
83 makeKey - returns the Key of the symbol, a. If a is not in the
84 name table then it is added, otherwise the Key of a is returned
85 directly. Note that the name table has no scope - it merely
86 presents a more convienient way of expressing strings. By a Key.
87 */
88
89 extern "C" nameKey_Name nameKey_makeKey (const char *a_, unsigned int _a_high);
90
91 /*
92 makekey - returns the Key of the symbol, a. If a is not in the
93 name table then it is added, otherwise the Key of a is returned
94 directly. Note that the name table has no scope - it merely
95 presents a more convienient way of expressing strings. By a Key.
96 These keys last for the duration of compilation.
97 */
98
99 extern "C" nameKey_Name nameKey_makekey (void * a);
100
101 /*
102 getKey - returns the name, a, of the key, Key.
103 */
104
105 extern "C" void nameKey_getKey (nameKey_Name key, char *a, unsigned int _a_high);
106
107 /*
108 lengthKey - returns the StrLen of Key.
109 */
110
111 extern "C" unsigned int nameKey_lengthKey (nameKey_Name key);
112
113 /*
114 isKey - returns TRUE if string, a, is currently a key.
115 We dont use the Compare function, we inline it and avoid
116 converting, a, into a String, for speed.
117 */
118
119 extern "C" bool nameKey_isKey (const char *a_, unsigned int _a_high);
120
121 /*
122 keyToCharStar - returns the C char * string equivalent for, key.
123 */
124
125 extern "C" void nameKey_writeKey (nameKey_Name key);
126
127 /*
128 isSameExcludingCase - returns TRUE if key1 and key2 are
129 the same. It is case insensitive.
130 This function deliberately inlines CAP for speed.
131 */
132
133 extern "C" bool nameKey_isSameExcludingCase (nameKey_Name key1, nameKey_Name key2);
134
135 /*
136 keyToCharStar - returns the C char * string equivalent for, key.
137 */
138
139 extern "C" void * nameKey_keyToCharStar (nameKey_Name key);
140
141 /*
142 doMakeKey - finds the name, n, in the tree or else create a name.
143 If a name is found then the string, n, is deallocated.
144 */
145
146 static nameKey_Name doMakeKey (nameKey_ptrToChar n, unsigned int higha);
147
148 /*
149 compare - return the result of Names[i] with Names[j]
150 */
151
152 static nameKey_comparison compare (nameKey_ptrToChar pi, nameKey_Name j);
153
154 /*
155 findNodeAndParentInTree - search BinaryTree for a name.
156 If this name is found in the BinaryTree then
157 child is set to this name and father is set to the node above.
158 A comparison is returned to assist adding entries into this tree.
159 */
160
161 static nameKey_comparison findNodeAndParentInTree (nameKey_ptrToChar n, nameKey_nameNode *child, nameKey_nameNode *father);
162
163
164 /*
165 doMakeKey - finds the name, n, in the tree or else create a name.
166 If a name is found then the string, n, is deallocated.
167 */
168
169 static nameKey_Name doMakeKey (nameKey_ptrToChar n, unsigned int higha)
170 {
171 nameKey_comparison result;
172 nameKey_nameNode father;
173 nameKey_nameNode child;
174 nameKey_Name k;
175
176 result = findNodeAndParentInTree (n, &child, &father);
177 if (child == NULL)
178 {
179 if (result == nameKey_less)
180 {
181 Storage_ALLOCATE ((void **) &child, sizeof (nameKey__T1));
182 father->left = child;
183 }
184 else if (result == nameKey_greater)
185 {
186 /* avoid dangling else. */
187 Storage_ALLOCATE ((void **) &child, sizeof (nameKey__T1));
188 father->right = child;
189 }
190 child->right = NULL;
191 child->left = NULL;
192 lastIndice += 1;
193 child->key = lastIndice;
194 child->data = n;
195 Indexing_PutIndice (keyIndex, child->key, reinterpret_cast <void *> (n));
196 k = lastIndice;
197 }
198 else
199 {
200 Storage_DEALLOCATE (reinterpret_cast <void **> (&n), higha+1);
201 k = child->key;
202 }
203 return k;
204 /* static analysis guarentees a RETURN statement will be used before here. */
205 __builtin_unreachable ();
206 }
207
208
209 /*
210 compare - return the result of Names[i] with Names[j]
211 */
212
213 static nameKey_comparison compare (nameKey_ptrToChar pi, nameKey_Name j)
214 {
215 nameKey_ptrToChar pj;
216 char c1;
217 char c2;
218
219 pj = static_cast<nameKey_ptrToChar> (nameKey_keyToCharStar (j));
220 c1 = (*pi);
221 c2 = (*pj);
222 while ((c1 != ASCII_nul) || (c2 != ASCII_nul))
223 {
224 if (c1 < c2)
225 {
226 return nameKey_less;
227 }
228 else if (c1 > c2)
229 {
230 /* avoid dangling else. */
231 return nameKey_greater;
232 }
233 else
234 {
235 /* avoid dangling else. */
236 pi += 1;
237 pj += 1;
238 c1 = (*pi);
239 c2 = (*pj);
240 }
241 }
242 return nameKey_equal;
243 /* static analysis guarentees a RETURN statement will be used before here. */
244 __builtin_unreachable ();
245 }
246
247
248 /*
249 findNodeAndParentInTree - search BinaryTree for a name.
250 If this name is found in the BinaryTree then
251 child is set to this name and father is set to the node above.
252 A comparison is returned to assist adding entries into this tree.
253 */
254
255 static nameKey_comparison findNodeAndParentInTree (nameKey_ptrToChar n, nameKey_nameNode *child, nameKey_nameNode *father)
256 {
257 nameKey_comparison result;
258
259 /* firstly set up the initial values of child and father, using sentinal node */
260 (*father) = binaryTree;
261 (*child) = binaryTree->left;
262 if ((*child) == NULL)
263 {
264 return nameKey_less;
265 }
266 else
267 {
268 do {
269 result = compare (n, (*child)->key);
270 if (result == nameKey_less)
271 {
272 (*father) = (*child);
273 (*child) = (*child)->left;
274 }
275 else if (result == nameKey_greater)
276 {
277 /* avoid dangling else. */
278 (*father) = (*child);
279 (*child) = (*child)->right;
280 }
281 } while (! (((*child) == NULL) || (result == nameKey_equal)));
282 return result;
283 }
284 /* static analysis guarentees a RETURN statement will be used before here. */
285 __builtin_unreachable ();
286 }
287
288
289 /*
290 makeKey - returns the Key of the symbol, a. If a is not in the
291 name table then it is added, otherwise the Key of a is returned
292 directly. Note that the name table has no scope - it merely
293 presents a more convienient way of expressing strings. By a Key.
294 */
295
296 extern "C" nameKey_Name nameKey_makeKey (const char *a_, unsigned int _a_high)
297 {
298 nameKey_ptrToChar n;
299 nameKey_ptrToChar p;
300 unsigned int i;
301 unsigned int higha;
302 char a[_a_high+1];
303
304 /* make a local copy of each unbounded array. */
305 memcpy (a, a_, _a_high+1);
306
307 higha = StrLib_StrLen ((const char *) a, _a_high);
308 Storage_ALLOCATE (reinterpret_cast <void **> (&p), higha+1);
309 if (p == NULL)
310 {
311 M2RTS_HALT (-1); /* out of memory error */
312 __builtin_unreachable ();
313 }
314 else
315 {
316 n = p;
317 i = 0;
318 while (i < higha)
319 {
320 (*p) = a[i];
321 i += 1;
322 p += 1;
323 }
324 (*p) = ASCII_nul;
325 return doMakeKey (n, higha);
326 }
327 ReturnException ("../../gcc/m2/mc/nameKey.def", 20, 1);
328 __builtin_unreachable ();
329 }
330
331
332 /*
333 makekey - returns the Key of the symbol, a. If a is not in the
334 name table then it is added, otherwise the Key of a is returned
335 directly. Note that the name table has no scope - it merely
336 presents a more convienient way of expressing strings. By a Key.
337 These keys last for the duration of compilation.
338 */
339
340 extern "C" nameKey_Name nameKey_makekey (void * a)
341 {
342 nameKey_ptrToChar n;
343 nameKey_ptrToChar p;
344 nameKey_ptrToChar pa;
345 unsigned int i;
346 unsigned int higha;
347
348 if (a == NULL)
349 {
350 return nameKey_NulName;
351 }
352 else
353 {
354 higha = static_cast<unsigned int> (libc_strlen (a));
355 Storage_ALLOCATE (reinterpret_cast <void **> (&p), higha+1);
356 if (p == NULL)
357 {
358 M2RTS_HALT (-1); /* out of memory error */
359 __builtin_unreachable ();
360 }
361 else
362 {
363 n = p;
364 pa = static_cast<nameKey_ptrToChar> (a);
365 i = 0;
366 while (i < higha)
367 {
368 (*p) = (*pa);
369 i += 1;
370 p += 1;
371 pa += 1;
372 }
373 (*p) = ASCII_nul;
374 return doMakeKey (n, higha);
375 }
376 }
377 ReturnException ("../../gcc/m2/mc/nameKey.def", 20, 1);
378 __builtin_unreachable ();
379 }
380
381
382 /*
383 getKey - returns the name, a, of the key, Key.
384 */
385
386 extern "C" void nameKey_getKey (nameKey_Name key, char *a, unsigned int _a_high)
387 {
388 nameKey_ptrToChar p;
389 unsigned int i;
390 unsigned int higha;
391
392 p = static_cast<nameKey_ptrToChar> (nameKey_keyToCharStar (key));
393 i = 0;
394 higha = _a_high;
395 while (((p != NULL) && (i <= higha)) && ((*p) != ASCII_nul))
396 {
397 const_cast<char *>(a)[i] = (*p);
398 p += 1;
399 i += 1;
400 }
401 if (i <= higha)
402 {
403 const_cast<char *>(a)[i] = ASCII_nul;
404 }
405 }
406
407
408 /*
409 lengthKey - returns the StrLen of Key.
410 */
411
412 extern "C" unsigned int nameKey_lengthKey (nameKey_Name key)
413 {
414 unsigned int i;
415 nameKey_ptrToChar p;
416
417 p = static_cast<nameKey_ptrToChar> (nameKey_keyToCharStar (key));
418 i = 0;
419 while ((*p) != ASCII_nul)
420 {
421 i += 1;
422 p += 1;
423 }
424 return i;
425 /* static analysis guarentees a RETURN statement will be used before here. */
426 __builtin_unreachable ();
427 }
428
429
430 /*
431 isKey - returns TRUE if string, a, is currently a key.
432 We dont use the Compare function, we inline it and avoid
433 converting, a, into a String, for speed.
434 */
435
436 extern "C" bool nameKey_isKey (const char *a_, unsigned int _a_high)
437 {
438 nameKey_nameNode child;
439 nameKey_ptrToChar p;
440 unsigned int i;
441 unsigned int higha;
442 char a[_a_high+1];
443
444 /* make a local copy of each unbounded array. */
445 memcpy (a, a_, _a_high+1);
446
447 /* firstly set up the initial values of child, using sentinal node */
448 child = binaryTree->left;
449 if (child != NULL)
450 {
451 do {
452 i = 0;
453 higha = _a_high;
454 p = static_cast<nameKey_ptrToChar> (nameKey_keyToCharStar (child->key));
455 while ((i <= higha) && (a[i] != ASCII_nul))
456 {
457 if (a[i] < (*p))
458 {
459 child = child->left;
460 i = higha;
461 }
462 else if (a[i] > (*p))
463 {
464 /* avoid dangling else. */
465 child = child->right;
466 i = higha;
467 }
468 else
469 {
470 /* avoid dangling else. */
471 if ((a[i] == ASCII_nul) || (i == higha))
472 {
473 /* avoid gcc warning by using compound statement even if not strictly necessary. */
474 if ((*p) == ASCII_nul)
475 {
476 return true;
477 }
478 else
479 {
480 child = child->left;
481 }
482 }
483 p += 1;
484 }
485 i += 1;
486 }
487 } while (! (child == NULL));
488 }
489 return false;
490 /* static analysis guarentees a RETURN statement will be used before here. */
491 __builtin_unreachable ();
492 }
493
494
495 /*
496 keyToCharStar - returns the C char * string equivalent for, key.
497 */
498
499 extern "C" void nameKey_writeKey (nameKey_Name key)
500 {
501 nameKey_ptrToChar s;
502
503 s = static_cast<nameKey_ptrToChar> (nameKey_keyToCharStar (key));
504 while ((s != NULL) && ((*s) != ASCII_nul))
505 {
506 StdIO_Write ((*s));
507 s += 1;
508 }
509 }
510
511
512 /*
513 isSameExcludingCase - returns TRUE if key1 and key2 are
514 the same. It is case insensitive.
515 This function deliberately inlines CAP for speed.
516 */
517
518 extern "C" bool nameKey_isSameExcludingCase (nameKey_Name key1, nameKey_Name key2)
519 {
520 nameKey_ptrToChar pi;
521 nameKey_ptrToChar pj;
522 char c1;
523 char c2;
524
525 if (key1 == key2)
526 {
527 return true;
528 }
529 else
530 {
531 pi = static_cast<nameKey_ptrToChar> (nameKey_keyToCharStar (key1));
532 pj = static_cast<nameKey_ptrToChar> (nameKey_keyToCharStar (key2));
533 c1 = (*pi);
534 c2 = (*pj);
535 while ((c1 != ASCII_nul) && (c2 != ASCII_nul))
536 {
537 if (((c1 == c2) || (((c1 >= 'A') && (c1 <= 'Z')) && (c2 == ((char) (( ((unsigned int) (c1))- ((unsigned int) ('A')))+ ((unsigned int) ('a'))))))) || (((c2 >= 'A') && (c2 <= 'Z')) && (c1 == ((char) (( ((unsigned int) (c2))- ((unsigned int) ('A')))+ ((unsigned int) ('a')))))))
538 {
539 pi += 1;
540 pj += 1;
541 c1 = (*pi);
542 c2 = (*pj);
543 }
544 else
545 {
546 /* difference found */
547 return false;
548 }
549 }
550 return c1 == c2;
551 }
552 /* static analysis guarentees a RETURN statement will be used before here. */
553 __builtin_unreachable ();
554 }
555
556
557 /*
558 keyToCharStar - returns the C char * string equivalent for, key.
559 */
560
561 extern "C" void * nameKey_keyToCharStar (nameKey_Name key)
562 {
563 if ((key == nameKey_NulName) || (! (Indexing_InBounds (keyIndex, key))))
564 {
565 return NULL;
566 }
567 else
568 {
569 return Indexing_GetIndice (keyIndex, key);
570 }
571 /* static analysis guarentees a RETURN statement will be used before here. */
572 __builtin_unreachable ();
573 }
574
575 extern "C" void _M2_nameKey_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
576 {
577 lastIndice = 0;
578 keyIndex = Indexing_InitIndex (1);
579 Storage_ALLOCATE ((void **) &binaryTree, sizeof (nameKey__T1));
580 binaryTree->left = NULL;
581 }
582
583 extern "C" void _M2_nameKey_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
584 {
585 }