Debian Codegraph @

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