Debian Codegraph @

gcc/m2/mc-boot/Gkeyc.cc

raw
1 /* do not edit automatically generated by mc from keyc. */
2 /* keyc maintains the C name scope and avoids C/C++ name conflicts.
3 Copyright (C) 2016-2025 Free Software Foundation, Inc.
4
5 This file is part of GNU Modula-2.
6
7 GNU Modula-2 is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
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 "GStorage.h"
39 # include "Gmcrts.h"
40 #if defined(__cplusplus)
41 # undef NULL
42 # define NULL 0
43 #endif
44 #define _keyc_C
45
46 #include "Gkeyc.h"
47 # include "GmcPretty.h"
48 # include "GStorage.h"
49 # include "GDynamicStrings.h"
50 # include "GsymbolKey.h"
51 # include "GnameKey.h"
52 # include "GmcOptions.h"
53 # include "GM2RTS.h"
54
55 #if !defined (decl_node_D)
56 # define decl_node_D
57 typedef void *decl_node;
58 #endif
59
60 typedef struct keyc__T1_r keyc__T1;
61
62 typedef keyc__T1 *keyc_scope;
63
64 struct keyc__T1_r {
65 decl_node scoped;
66 symbolKey_symbolTree symbols;
67 keyc_scope next;
68 };
69
70 static keyc_scope stack;
71 static keyc_scope freeList;
72 static symbolKey_symbolTree keywords;
73 static symbolKey_symbolTree macros;
74 static bool initializedCP;
75 static bool initializedGCC;
76 static bool seenGccTree;
77 static bool seenGccLocation;
78 static bool seenIntMin;
79 static bool seenUIntMin;
80 static bool seenLongMin;
81 static bool seenULongMin;
82 static bool seenCharMin;
83 static bool seenUCharMin;
84 static bool seenIntMax;
85 static bool seenUIntMax;
86 static bool seenLongMax;
87 static bool seenULongMax;
88 static bool seenCharMax;
89 static bool seenUCharMax;
90 static bool seenLabs;
91 static bool seenAbs;
92 static bool seenFabs;
93 static bool seenFabsl;
94 static bool seenSize_t;
95 static bool seenSSize_t;
96 static bool seenUnistd;
97 static bool seenSysTypes;
98 static bool seenThrow;
99 static bool seenFree;
100 static bool seenMalloc;
101 static bool seenStorage;
102 static bool seenProc;
103 static bool seenTrue;
104 static bool seenFalse;
105 static bool seenNull;
106 static bool seenMemcpy;
107 static bool seenException;
108 static bool seenComplex;
109 static bool seenM2RTS;
110 static bool seenStrlen;
111 static bool seenCtype;
112
113 /*
114 useGccTree - indicate we have imported tree from gcctypes.
115 */
116
117 extern "C" void keyc_useGccTree (void);
118
119 /*
120 useGccLocation - indicate we have imported tree from gcctypes.
121 */
122
123 extern "C" void keyc_useGccLocation (void);
124
125 /*
126 useUnistd - need to use unistd.h call using open/close/read/write require this header.
127 */
128
129 extern "C" void keyc_useUnistd (void);
130
131 /*
132 useThrow - use the throw function.
133 */
134
135 extern "C" void keyc_useThrow (void);
136
137 /*
138 useStorage - indicate we have used storage.
139 */
140
141 extern "C" void keyc_useStorage (void);
142
143 /*
144 useFree - indicate we have used free.
145 */
146
147 extern "C" void keyc_useFree (void);
148
149 /*
150 useMalloc - indicate we have used malloc.
151 */
152
153 extern "C" void keyc_useMalloc (void);
154
155 /*
156 useProc - indicate we have used proc.
157 */
158
159 extern "C" void keyc_useProc (void);
160
161 /*
162 useTrue - indicate we have used TRUE.
163 */
164
165 extern "C" void keyc_useTrue (void);
166
167 /*
168 useFalse - indicate we have used FALSE.
169 */
170
171 extern "C" void keyc_useFalse (void);
172
173 /*
174 useNull - indicate we have used NULL.
175 */
176
177 extern "C" void keyc_useNull (void);
178
179 /*
180 useMemcpy - indicate we have used memcpy.
181 */
182
183 extern "C" void keyc_useMemcpy (void);
184
185 /*
186 useIntMin - indicate we have used INT_MIN.
187 */
188
189 extern "C" void keyc_useIntMin (void);
190
191 /*
192 useUIntMin - indicate we have used UINT_MIN.
193 */
194
195 extern "C" void keyc_useUIntMin (void);
196
197 /*
198 useLongMin - indicate we have used LONG_MIN.
199 */
200
201 extern "C" void keyc_useLongMin (void);
202
203 /*
204 useULongMin - indicate we have used ULONG_MIN.
205 */
206
207 extern "C" void keyc_useULongMin (void);
208
209 /*
210 useCharMin - indicate we have used CHAR_MIN.
211 */
212
213 extern "C" void keyc_useCharMin (void);
214
215 /*
216 useUCharMin - indicate we have used UCHAR_MIN.
217 */
218
219 extern "C" void keyc_useUCharMin (void);
220
221 /*
222 useIntMax - indicate we have used INT_MAX.
223 */
224
225 extern "C" void keyc_useIntMax (void);
226
227 /*
228 useUIntMax - indicate we have used UINT_MAX.
229 */
230
231 extern "C" void keyc_useUIntMax (void);
232
233 /*
234 useLongMax - indicate we have used LONG_MAX.
235 */
236
237 extern "C" void keyc_useLongMax (void);
238
239 /*
240 useULongMax - indicate we have used ULONG_MAX.
241 */
242
243 extern "C" void keyc_useULongMax (void);
244
245 /*
246 useCharMax - indicate we have used CHAR_MAX.
247 */
248
249 extern "C" void keyc_useCharMax (void);
250
251 /*
252 useUCharMax - indicate we have used UChar_MAX.
253 */
254
255 extern "C" void keyc_useUCharMax (void);
256
257 /*
258 useSize_t - indicate we have used size_t.
259 */
260
261 extern "C" void keyc_useSize_t (void);
262
263 /*
264 useSSize_t - indicate we have used ssize_t.
265 */
266
267 extern "C" void keyc_useSSize_t (void);
268
269 /*
270 useLabs - indicate we have used labs.
271 */
272
273 extern "C" void keyc_useLabs (void);
274
275 /*
276 useAbs - indicate we have used abs.
277 */
278
279 extern "C" void keyc_useAbs (void);
280
281 /*
282 useFabs - indicate we have used fabs.
283 */
284
285 extern "C" void keyc_useFabs (void);
286
287 /*
288 useFabsl - indicate we have used fabsl.
289 */
290
291 extern "C" void keyc_useFabsl (void);
292
293 /*
294 useException - use the exceptions module, mcrts.
295 */
296
297 extern "C" void keyc_useException (void);
298
299 /*
300 useComplex - use the complex data type.
301 */
302
303 extern "C" void keyc_useComplex (void);
304
305 /*
306 useM2RTS - indicate we have used M2RTS in the converted code.
307 */
308
309 extern "C" void keyc_useM2RTS (void);
310
311 /*
312 useStrlen - indicate we have used strlen in the converted code.
313 */
314
315 extern "C" void keyc_useStrlen (void);
316
317 /*
318 useCtype - indicate we have used the toupper function.
319 */
320
321 extern "C" void keyc_useCtype (void);
322
323 /*
324 genDefs - generate definitions or includes for all
325 macros and prototypes used.
326 */
327
328 extern "C" void keyc_genDefs (mcPretty_pretty p);
329
330 /*
331 genConfigSystem - generate include files for config.h and system.h
332 within the GCC framework.
333 */
334
335 extern "C" void keyc_genConfigSystem (mcPretty_pretty p);
336
337 /*
338 enterScope - enter a scope defined by, n.
339 */
340
341 extern "C" void keyc_enterScope (decl_node n);
342
343 /*
344 leaveScope - leave the scope defined by, n.
345 */
346
347 extern "C" void keyc_leaveScope (decl_node n);
348
349 /*
350 cname - attempts to declare a symbol with name, n, in the
351 current scope. If there is no conflict with the
352 target language then NIL is returned, otherwise
353 a mangled name is returned as a String.
354 If scopes is FALSE then only the keywords and
355 macros are detected for a clash (all scoping
356 is ignored).
357 */
358
359 extern "C" DynamicStrings_String keyc_cname (nameKey_Name n, bool scopes);
360
361 /*
362 cnamen - attempts to declare a symbol with name, n, in the
363 current scope. If there is no conflict with the
364 target language then NIL is returned, otherwise
365 a mangled name is returned as a Name
366 If scopes is FALSE then only the keywords and
367 macros are detected for a clash (all scoping
368 is ignored).
369 */
370
371 extern "C" nameKey_Name keyc_cnamen (nameKey_Name n, bool scopes);
372
373 /*
374 cp - include C++ keywords and standard declarations to avoid.
375 */
376
377 extern "C" void keyc_cp (void);
378
379 /*
380 checkGccConfigSystem - issues the GCC include config.h, include system.h
381 instead of the standard host include.
382 */
383
384 static void checkGccConfigSystem (mcPretty_pretty p);
385
386 /*
387 checkGccTypes - if we have imported tree or location_t from gcctypes
388 then we include the gcc headers.
389 */
390
391 static void checkGccTypes (mcPretty_pretty p);
392
393 /*
394 checkCtype -
395 */
396
397 static void checkCtype (mcPretty_pretty p);
398
399 /*
400 checkAbs - check to see if the abs family, size_t or ssize_t have been used.
401 */
402
403 static void checkAbs (mcPretty_pretty p);
404
405 /*
406 checkLimits -
407 */
408
409 static void checkLimits (mcPretty_pretty p);
410
411 /*
412 checkFreeMalloc -
413 */
414
415 static void checkFreeMalloc (mcPretty_pretty p);
416
417 /*
418 checkStorage -
419 */
420
421 static void checkStorage (mcPretty_pretty p);
422
423 /*
424 checkProc -
425 */
426
427 static void checkProc (mcPretty_pretty p);
428
429 /*
430 checkTrue -
431 */
432
433 static void checkTrue (mcPretty_pretty p);
434
435 /*
436 checkFalse -
437 */
438
439 static void checkFalse (mcPretty_pretty p);
440
441 /*
442 checkNull -
443 */
444
445 static void checkNull (mcPretty_pretty p);
446
447 /*
448 checkMemcpy -
449 */
450
451 static void checkMemcpy (mcPretty_pretty p);
452
453 /*
454 checkM2RTS -
455 */
456
457 static void checkM2RTS (mcPretty_pretty p);
458
459 /*
460 checkException - check to see if exceptions were used.
461 */
462
463 static void checkException (mcPretty_pretty p);
464
465 /*
466 checkThrow - check to see if the throw function is used.
467 */
468
469 static void checkThrow (mcPretty_pretty p);
470
471 /*
472 checkUnistd - check to see if the unistd.h header file is required.
473 */
474
475 static void checkUnistd (mcPretty_pretty p);
476
477 /*
478 checkComplex - check to see if the type complex was used.
479 */
480
481 static void checkComplex (mcPretty_pretty p);
482
483 /*
484 checkSysTypes - emit header for sys/types.h if necessary.
485 */
486
487 static void checkSysTypes (mcPretty_pretty p);
488
489 /*
490 fixNullPointerConst - fixup for NULL on some C++11 systems.
491 */
492
493 static void fixNullPointerConst (mcPretty_pretty p);
494
495 /*
496 genBool -
497 */
498
499 static void genBool (mcPretty_pretty p);
500
501 /*
502 new -
503 */
504
505 static keyc_scope new_ (decl_node n);
506
507 /*
508 mangle1 - returns TRUE if name is unique if we add _
509 to its end.
510 */
511
512 static bool mangle1 (nameKey_Name n, DynamicStrings_String *m, bool scopes);
513
514 /*
515 mangle2 - returns TRUE if name is unique if we prepend _
516 to, n.
517 */
518
519 static bool mangle2 (nameKey_Name n, DynamicStrings_String *m, bool scopes);
520
521 /*
522 mangleN - keep adding '_' to the end of n until it
523 no longer clashes.
524 */
525
526 static bool mangleN (nameKey_Name n, DynamicStrings_String *m, bool scopes);
527
528 /*
529 clash - returns TRUE if there is a clash with name, n,
530 in the current scope or C keywords or C macros.
531 */
532
533 static bool clash (nameKey_Name n, bool scopes);
534
535 /*
536 initCP - add the extra keywords and standard definitions used by C++.
537 */
538
539 static void initCP (void);
540
541 /*
542 add -
543 */
544
545 static void add (symbolKey_symbolTree s, const char *a_, unsigned int _a_high);
546
547 /*
548 initMacros - macros and library function names to avoid.
549 */
550
551 static void initMacros (void);
552
553 /*
554 initKeywords - keywords to avoid.
555 */
556
557 static void initKeywords (void);
558
559 /*
560 init -
561 */
562
563 static void init (void);
564
565
566 /*
567 checkGccConfigSystem - issues the GCC include config.h, include system.h
568 instead of the standard host include.
569 */
570
571 static void checkGccConfigSystem (mcPretty_pretty p)
572 {
573 if (mcOptions_getGccConfigSystem ())
574 {
575 if (! initializedGCC)
576 {
577 initializedGCC = true;
578 mcPretty_print (p, (const char *) "#include \"config.h\"\\n", 21);
579 mcPretty_print (p, (const char *) "#include \"system.h\"\\n", 21);
580 checkGccTypes (p);
581 }
582 }
583 }
584
585
586 /*
587 checkGccTypes - if we have imported tree or location_t from gcctypes
588 then we include the gcc headers.
589 */
590
591 static void checkGccTypes (mcPretty_pretty p)
592 {
593 if (seenGccTree || seenGccLocation)
594 {
595 mcPretty_print (p, (const char *) "#include \"gcc-consolidation.h\"\\n\\n", 34);
596 }
597 }
598
599
600 /*
601 checkCtype -
602 */
603
604 static void checkCtype (mcPretty_pretty p)
605 {
606 if (seenCtype)
607 {
608 checkGccConfigSystem (p);
609 if (mcOptions_getGccConfigSystem ())
610 {
611 /* GCC header files use a safe variant. */
612 mcPretty_print (p, (const char *) "#include <safe-ctype.h>\\n", 25);
613 }
614 else
615 {
616 mcPretty_print (p, (const char *) "#include <ctype.h>\\n", 20);
617 }
618 }
619 }
620
621
622 /*
623 checkAbs - check to see if the abs family, size_t or ssize_t have been used.
624 */
625
626 static void checkAbs (mcPretty_pretty p)
627 {
628 if (((((seenLabs || seenAbs) || seenFabs) || seenFabsl) || seenSize_t) || seenSSize_t)
629 {
630 checkGccConfigSystem (p);
631 if (! (mcOptions_getGccConfigSystem ()))
632 {
633 mcPretty_print (p, (const char *) "#include <stdlib.h>\\n", 21);
634 }
635 }
636 }
637
638
639 /*
640 checkLimits -
641 */
642
643 static void checkLimits (mcPretty_pretty p)
644 {
645 if ((((((((((((seenMemcpy || seenIntMin) || seenUIntMin) || seenLongMin) || seenULongMin) || seenCharMin) || seenUCharMin) || seenIntMax) || seenUIntMax) || seenLongMax) || seenULongMax) || seenCharMax) || seenUCharMax) /* OR seenUIntMax */
646 {
647 checkGccConfigSystem (p);
648 if (! (mcOptions_getGccConfigSystem ()))
649 {
650 mcPretty_print (p, (const char *) "#include <limits.h>\\n", 21);
651 }
652 }
653 }
654
655
656 /*
657 checkFreeMalloc -
658 */
659
660 static void checkFreeMalloc (mcPretty_pretty p)
661 {
662 if (seenFree || seenMalloc)
663 {
664 checkGccConfigSystem (p);
665 if (! (mcOptions_getGccConfigSystem ()))
666 {
667 mcPretty_print (p, (const char *) "#include <stdlib.h>\\n", 21);
668 }
669 }
670 }
671
672
673 /*
674 checkStorage -
675 */
676
677 static void checkStorage (mcPretty_pretty p)
678 {
679 if (seenStorage)
680 {
681 mcPretty_print (p, (const char *) "# include \"", 13);
682 mcPretty_prints (p, mcOptions_getHPrefix ());
683 mcPretty_print (p, (const char *) "Storage.h\"\\n", 12);
684 }
685 }
686
687
688 /*
689 checkProc -
690 */
691
692 static void checkProc (mcPretty_pretty p)
693 {
694 if (seenProc)
695 {
696 mcPretty_print (p, (const char *) "# if !defined (PROC_D)\\n", 26);
697 mcPretty_print (p, (const char *) "# define PROC_D\\n", 22);
698 mcPretty_print (p, (const char *) " typedef void (*PROC_t) (void);\\n", 39);
699 mcPretty_print (p, (const char *) " typedef struct { PROC_t proc; } PROC;\\n", 46);
700 mcPretty_print (p, (const char *) "# endif\\n\\n", 13);
701 }
702 }
703
704
705 /*
706 checkTrue -
707 */
708
709 static void checkTrue (mcPretty_pretty p)
710 {
711 if (seenTrue)
712 {
713 mcPretty_print (p, (const char *) "# if !defined (TRUE)\\n", 24);
714 mcPretty_print (p, (const char *) "# define TRUE (1==1)\\n", 27);
715 mcPretty_print (p, (const char *) "# endif\\n\\n", 13);
716 }
717 }
718
719
720 /*
721 checkFalse -
722 */
723
724 static void checkFalse (mcPretty_pretty p)
725 {
726 if (seenFalse)
727 {
728 mcPretty_print (p, (const char *) "# if !defined (FALSE)\\n", 25);
729 mcPretty_print (p, (const char *) "# define FALSE (1==0)\\n", 28);
730 mcPretty_print (p, (const char *) "# endif\\n\\n", 13);
731 }
732 }
733
734
735 /*
736 checkNull -
737 */
738
739 static void checkNull (mcPretty_pretty p)
740 {
741 if (seenNull)
742 {
743 checkGccConfigSystem (p);
744 if (! (mcOptions_getGccConfigSystem ()))
745 {
746 mcPretty_print (p, (const char *) "#include <stddef.h>\\n", 21);
747 }
748 }
749 }
750
751
752 /*
753 checkMemcpy -
754 */
755
756 static void checkMemcpy (mcPretty_pretty p)
757 {
758 if (seenMemcpy || seenStrlen)
759 {
760 checkGccConfigSystem (p);
761 if (! (mcOptions_getGccConfigSystem ()))
762 {
763 mcPretty_print (p, (const char *) "#include <string.h>\\n", 21);
764 }
765 }
766 }
767
768
769 /*
770 checkM2RTS -
771 */
772
773 static void checkM2RTS (mcPretty_pretty p)
774 {
775 if (seenM2RTS)
776 {
777 mcPretty_print (p, (const char *) "# include \"", 13);
778 mcPretty_prints (p, mcOptions_getHPrefix ());
779 mcPretty_print (p, (const char *) "M2RTS.h\"\\n", 10);
780 }
781 }
782
783
784 /*
785 checkException - check to see if exceptions were used.
786 */
787
788 static void checkException (mcPretty_pretty p)
789 {
790 if (seenException)
791 {
792 mcPretty_print (p, (const char *) "# include \"Gmcrts.h\"\\n", 24);
793 }
794 }
795
796
797 /*
798 checkThrow - check to see if the throw function is used.
799 */
800
801 static void checkThrow (mcPretty_pretty p)
802 {
803 if (seenThrow)
804 {
805 /* print (p, '# include "sys/cdefs.h"
806 ') ; */
807 mcPretty_print (p, (const char *) "#ifndef __cplusplus\\n", 21);
808 mcPretty_print (p, (const char *) "extern void throw (unsigned int);\\n", 35);
809 mcPretty_print (p, (const char *) "#endif\\n", 8);
810 }
811 }
812
813
814 /*
815 checkUnistd - check to see if the unistd.h header file is required.
816 */
817
818 static void checkUnistd (mcPretty_pretty p)
819 {
820 if (seenUnistd)
821 {
822 checkGccConfigSystem (p);
823 if (! (mcOptions_getGccConfigSystem ()))
824 {
825 mcPretty_print (p, (const char *) "#include <unistd.h>\\n", 21);
826 }
827 }
828 }
829
830
831 /*
832 checkComplex - check to see if the type complex was used.
833 */
834
835 static void checkComplex (mcPretty_pretty p)
836 {
837 if (seenComplex)
838 {
839 checkGccConfigSystem (p);
840 if (! (mcOptions_getGccConfigSystem ()))
841 {
842 mcPretty_print (p, (const char *) "# include <complex.h>\\n", 25);
843 }
844 }
845 }
846
847
848 /*
849 checkSysTypes - emit header for sys/types.h if necessary.
850 */
851
852 static void checkSysTypes (mcPretty_pretty p)
853 {
854 if (seenSysTypes)
855 {
856 checkGccConfigSystem (p);
857 if (! (mcOptions_getGccConfigSystem ()))
858 {
859 mcPretty_print (p, (const char *) "# include <sys/types.h>\\n", 27);
860 }
861 }
862 }
863
864
865 /*
866 fixNullPointerConst - fixup for NULL on some C++11 systems.
867 */
868
869 static void fixNullPointerConst (mcPretty_pretty p)
870 {
871 if (seenNull)
872 {
873 mcPretty_print (p, (const char *) "#if defined(__cplusplus)\\n", 26);
874 mcPretty_print (p, (const char *) "# undef NULL\\n", 16);
875 mcPretty_print (p, (const char *) "# define NULL 0\\n", 19);
876 mcPretty_print (p, (const char *) "#endif\\n", 8);
877 }
878 }
879
880
881 /*
882 genBool -
883 */
884
885 static void genBool (mcPretty_pretty p)
886 {
887 if (mcOptions_useBool ())
888 {
889 mcPretty_print (p, (const char *) "#include <stdbool.h>\\n", 22);
890 }
891 }
892
893
894 /*
895 new -
896 */
897
898 static keyc_scope new_ (decl_node n)
899 {
900 keyc_scope s;
901
902 if (freeList == NULL)
903 {
904 Storage_ALLOCATE ((void **) &s, sizeof (keyc__T1));
905 }
906 else
907 {
908 s = freeList;
909 freeList = freeList->next;
910 }
911 return s;
912 /* static analysis guarentees a RETURN statement will be used before here. */
913 __builtin_unreachable ();
914 }
915
916
917 /*
918 mangle1 - returns TRUE if name is unique if we add _
919 to its end.
920 */
921
922 static bool mangle1 (nameKey_Name n, DynamicStrings_String *m, bool scopes)
923 {
924 (*m) = DynamicStrings_KillString ((*m));
925 (*m) = DynamicStrings_InitStringCharStar (nameKey_keyToCharStar (n));
926 (*m) = DynamicStrings_ConCatChar ((*m), '_');
927 return ! (clash (nameKey_makekey (DynamicStrings_string ((*m))), scopes));
928 /* static analysis guarentees a RETURN statement will be used before here. */
929 __builtin_unreachable ();
930 }
931
932
933 /*
934 mangle2 - returns TRUE if name is unique if we prepend _
935 to, n.
936 */
937
938 static bool mangle2 (nameKey_Name n, DynamicStrings_String *m, bool scopes)
939 {
940 (*m) = DynamicStrings_KillString ((*m));
941 (*m) = DynamicStrings_InitStringCharStar (nameKey_keyToCharStar (n));
942 (*m) = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "_", 1), DynamicStrings_Mark ((*m)));
943 return ! (clash (nameKey_makekey (DynamicStrings_string ((*m))), scopes));
944 /* static analysis guarentees a RETURN statement will be used before here. */
945 __builtin_unreachable ();
946 }
947
948
949 /*
950 mangleN - keep adding '_' to the end of n until it
951 no longer clashes.
952 */
953
954 static bool mangleN (nameKey_Name n, DynamicStrings_String *m, bool scopes)
955 {
956 (*m) = DynamicStrings_KillString ((*m));
957 (*m) = DynamicStrings_InitStringCharStar (nameKey_keyToCharStar (n));
958 for (;;)
959 {
960 (*m) = DynamicStrings_ConCatChar ((*m), '_');
961 if (! (clash (nameKey_makekey (DynamicStrings_string ((*m))), scopes)))
962 {
963 return true;
964 }
965 }
966 ReturnException ("../../gcc/m2/mc/keyc.def", 20, 1);
967 __builtin_unreachable ();
968 }
969
970
971 /*
972 clash - returns TRUE if there is a clash with name, n,
973 in the current scope or C keywords or C macros.
974 */
975
976 static bool clash (nameKey_Name n, bool scopes)
977 {
978 if (((symbolKey_getSymKey (macros, n)) != NULL) || ((symbolKey_getSymKey (keywords, n)) != NULL))
979 {
980 return true;
981 }
982 return scopes && ((symbolKey_getSymKey (stack->symbols, n)) != NULL);
983 /* static analysis guarentees a RETURN statement will be used before here. */
984 __builtin_unreachable ();
985 }
986
987
988 /*
989 initCP - add the extra keywords and standard definitions used by C++.
990 */
991
992 static void initCP (void)
993 {
994 add (keywords, (const char *) "delete", 6);
995 add (keywords, (const char *) "try", 3);
996 add (keywords, (const char *) "catch", 5);
997 add (keywords, (const char *) "operator", 8);
998 add (keywords, (const char *) "complex", 7);
999 add (keywords, (const char *) "export", 6);
1000 add (keywords, (const char *) "public", 6);
1001 }
1002
1003
1004 /*
1005 add -
1006 */
1007
1008 static void add (symbolKey_symbolTree s, const char *a_, unsigned int _a_high)
1009 {
1010 char a[_a_high+1];
1011
1012 /* make a local copy of each unbounded array. */
1013 memcpy (a, a_, _a_high+1);
1014
1015 symbolKey_putSymKey (s, nameKey_makeKey ((const char *) a, _a_high), reinterpret_cast <void *> (DynamicStrings_InitString ((const char *) a, _a_high)));
1016 }
1017
1018
1019 /*
1020 initMacros - macros and library function names to avoid.
1021 */
1022
1023 static void initMacros (void)
1024 {
1025 macros = symbolKey_initTree ();
1026 add (macros, (const char *) "FILE", 4);
1027 add (macros, (const char *) "EOF", 3);
1028 add (macros, (const char *) "stdio", 5);
1029 add (macros, (const char *) "stdout", 6);
1030 add (macros, (const char *) "stderr", 6);
1031 add (macros, (const char *) "write", 5);
1032 add (macros, (const char *) "read", 4);
1033 add (macros, (const char *) "exit", 4);
1034 add (macros, (const char *) "abs", 3);
1035 add (macros, (const char *) "optarg", 6);
1036 add (macros, (const char *) "div", 3);
1037 add (macros, (const char *) "sin", 3);
1038 add (macros, (const char *) "cos", 3);
1039 add (macros, (const char *) "tan", 3);
1040 add (macros, (const char *) "log10", 5);
1041 add (macros, (const char *) "trunc", 5);
1042 add (macros, (const char *) "I", 1);
1043 add (macros, (const char *) "csqrt", 5);
1044 add (macros, (const char *) "strlen", 6);
1045 add (macros, (const char *) "strcpy", 6);
1046 add (macros, (const char *) "free", 4);
1047 add (macros, (const char *) "malloc", 6);
1048 add (macros, (const char *) "time", 4);
1049 add (macros, (const char *) "main", 4);
1050 add (macros, (const char *) "true", 4);
1051 add (macros, (const char *) "false", 5);
1052 add (macros, (const char *) "sigfpe", 6);
1053 }
1054
1055
1056 /*
1057 initKeywords - keywords to avoid.
1058 */
1059
1060 static void initKeywords (void)
1061 {
1062 keywords = symbolKey_initTree ();
1063 add (keywords, (const char *) "auto", 4);
1064 add (keywords, (const char *) "break", 5);
1065 add (keywords, (const char *) "case", 4);
1066 add (keywords, (const char *) "char", 4);
1067 add (keywords, (const char *) "const", 5);
1068 add (keywords, (const char *) "continue", 8);
1069 add (keywords, (const char *) "default", 7);
1070 add (keywords, (const char *) "do", 2);
1071 add (keywords, (const char *) "double", 6);
1072 add (keywords, (const char *) "else", 4);
1073 add (keywords, (const char *) "enum", 4);
1074 add (keywords, (const char *) "extern", 6);
1075 add (keywords, (const char *) "float", 5);
1076 add (keywords, (const char *) "for", 3);
1077 add (keywords, (const char *) "goto", 4);
1078 add (keywords, (const char *) "if", 2);
1079 add (keywords, (const char *) "int", 3);
1080 add (keywords, (const char *) "long", 4);
1081 add (keywords, (const char *) "register", 8);
1082 add (keywords, (const char *) "return", 6);
1083 add (keywords, (const char *) "short", 5);
1084 add (keywords, (const char *) "signed", 6);
1085 add (keywords, (const char *) "sizeof", 6);
1086 add (keywords, (const char *) "static", 6);
1087 add (keywords, (const char *) "struct", 6);
1088 add (keywords, (const char *) "switch", 6);
1089 add (keywords, (const char *) "typedef", 7);
1090 add (keywords, (const char *) "union", 5);
1091 add (keywords, (const char *) "unsigned", 8);
1092 add (keywords, (const char *) "void", 4);
1093 add (keywords, (const char *) "volatile", 8);
1094 add (keywords, (const char *) "while", 5);
1095 add (keywords, (const char *) "and", 3);
1096 add (keywords, (const char *) "or", 2);
1097 add (keywords, (const char *) "not", 3);
1098 add (keywords, (const char *) "throw", 5);
1099 add (keywords, (const char *) "new", 3);
1100 }
1101
1102
1103 /*
1104 init -
1105 */
1106
1107 static void init (void)
1108 {
1109 seenUnistd = false;
1110 seenThrow = false;
1111 seenFree = false;
1112 seenMalloc = false;
1113 seenStorage = false;
1114 seenProc = false;
1115 seenTrue = false;
1116 seenFalse = false;
1117 seenNull = false;
1118 seenMemcpy = false;
1119 seenIntMin = false;
1120 seenUIntMin = false;
1121 seenLongMin = false;
1122 seenULongMin = false;
1123 seenCharMin = false;
1124 seenUCharMin = false;
1125 seenIntMax = false;
1126 seenUIntMax = false;
1127 seenLongMax = false;
1128 seenULongMax = false;
1129 seenCharMax = false;
1130 seenUCharMax = false;
1131 seenLabs = false;
1132 seenAbs = false;
1133 seenFabs = false;
1134 seenFabsl = false;
1135 seenException = false;
1136 seenComplex = false;
1137 seenM2RTS = false;
1138 seenStrlen = false;
1139 seenCtype = false;
1140 seenSize_t = false;
1141 seenSSize_t = false;
1142 seenSysTypes = false;
1143 seenGccTree = false;
1144 seenGccLocation = false;
1145 initializedCP = false;
1146 initializedGCC = false;
1147 stack = NULL;
1148 freeList = NULL;
1149 initKeywords ();
1150 initMacros ();
1151 }
1152
1153
1154 /*
1155 useGccTree - indicate we have imported tree from gcctypes.
1156 */
1157
1158 extern "C" void keyc_useGccTree (void)
1159 {
1160 seenGccTree = true;
1161 }
1162
1163
1164 /*
1165 useGccLocation - indicate we have imported tree from gcctypes.
1166 */
1167
1168 extern "C" void keyc_useGccLocation (void)
1169 {
1170 seenGccLocation = true;
1171 }
1172
1173
1174 /*
1175 useUnistd - need to use unistd.h call using open/close/read/write require this header.
1176 */
1177
1178 extern "C" void keyc_useUnistd (void)
1179 {
1180 seenUnistd = true;
1181 }
1182
1183
1184 /*
1185 useThrow - use the throw function.
1186 */
1187
1188 extern "C" void keyc_useThrow (void)
1189 {
1190 seenThrow = true;
1191 }
1192
1193
1194 /*
1195 useStorage - indicate we have used storage.
1196 */
1197
1198 extern "C" void keyc_useStorage (void)
1199 {
1200 seenStorage = true;
1201 }
1202
1203
1204 /*
1205 useFree - indicate we have used free.
1206 */
1207
1208 extern "C" void keyc_useFree (void)
1209 {
1210 seenFree = true;
1211 }
1212
1213
1214 /*
1215 useMalloc - indicate we have used malloc.
1216 */
1217
1218 extern "C" void keyc_useMalloc (void)
1219 {
1220 seenMalloc = true;
1221 }
1222
1223
1224 /*
1225 useProc - indicate we have used proc.
1226 */
1227
1228 extern "C" void keyc_useProc (void)
1229 {
1230 seenProc = true;
1231 }
1232
1233
1234 /*
1235 useTrue - indicate we have used TRUE.
1236 */
1237
1238 extern "C" void keyc_useTrue (void)
1239 {
1240 seenTrue = true;
1241 }
1242
1243
1244 /*
1245 useFalse - indicate we have used FALSE.
1246 */
1247
1248 extern "C" void keyc_useFalse (void)
1249 {
1250 seenFalse = true;
1251 }
1252
1253
1254 /*
1255 useNull - indicate we have used NULL.
1256 */
1257
1258 extern "C" void keyc_useNull (void)
1259 {
1260 seenNull = true;
1261 }
1262
1263
1264 /*
1265 useMemcpy - indicate we have used memcpy.
1266 */
1267
1268 extern "C" void keyc_useMemcpy (void)
1269 {
1270 seenMemcpy = true;
1271 }
1272
1273
1274 /*
1275 useIntMin - indicate we have used INT_MIN.
1276 */
1277
1278 extern "C" void keyc_useIntMin (void)
1279 {
1280 seenIntMin = true;
1281 }
1282
1283
1284 /*
1285 useUIntMin - indicate we have used UINT_MIN.
1286 */
1287
1288 extern "C" void keyc_useUIntMin (void)
1289 {
1290 seenUIntMin = true;
1291 }
1292
1293
1294 /*
1295 useLongMin - indicate we have used LONG_MIN.
1296 */
1297
1298 extern "C" void keyc_useLongMin (void)
1299 {
1300 seenLongMin = true;
1301 }
1302
1303
1304 /*
1305 useULongMin - indicate we have used ULONG_MIN.
1306 */
1307
1308 extern "C" void keyc_useULongMin (void)
1309 {
1310 seenULongMin = true;
1311 }
1312
1313
1314 /*
1315 useCharMin - indicate we have used CHAR_MIN.
1316 */
1317
1318 extern "C" void keyc_useCharMin (void)
1319 {
1320 seenCharMin = true;
1321 }
1322
1323
1324 /*
1325 useUCharMin - indicate we have used UCHAR_MIN.
1326 */
1327
1328 extern "C" void keyc_useUCharMin (void)
1329 {
1330 seenUCharMin = true;
1331 }
1332
1333
1334 /*
1335 useIntMax - indicate we have used INT_MAX.
1336 */
1337
1338 extern "C" void keyc_useIntMax (void)
1339 {
1340 seenIntMax = true;
1341 }
1342
1343
1344 /*
1345 useUIntMax - indicate we have used UINT_MAX.
1346 */
1347
1348 extern "C" void keyc_useUIntMax (void)
1349 {
1350 seenUIntMax = true;
1351 }
1352
1353
1354 /*
1355 useLongMax - indicate we have used LONG_MAX.
1356 */
1357
1358 extern "C" void keyc_useLongMax (void)
1359 {
1360 seenLongMax = true;
1361 }
1362
1363
1364 /*
1365 useULongMax - indicate we have used ULONG_MAX.
1366 */
1367
1368 extern "C" void keyc_useULongMax (void)
1369 {
1370 seenULongMax = true;
1371 }
1372
1373
1374 /*
1375 useCharMax - indicate we have used CHAR_MAX.
1376 */
1377
1378 extern "C" void keyc_useCharMax (void)
1379 {
1380 seenCharMax = true;
1381 }
1382
1383
1384 /*
1385 useUCharMax - indicate we have used UChar_MAX.
1386 */
1387
1388 extern "C" void keyc_useUCharMax (void)
1389 {
1390 seenUCharMax = true;
1391 }
1392
1393
1394 /*
1395 useSize_t - indicate we have used size_t.
1396 */
1397
1398 extern "C" void keyc_useSize_t (void)
1399 {
1400 seenSize_t = true;
1401 }
1402
1403
1404 /*
1405 useSSize_t - indicate we have used ssize_t.
1406 */
1407
1408 extern "C" void keyc_useSSize_t (void)
1409 {
1410 seenSSize_t = true;
1411 seenSysTypes = true;
1412 }
1413
1414
1415 /*
1416 useLabs - indicate we have used labs.
1417 */
1418
1419 extern "C" void keyc_useLabs (void)
1420 {
1421 seenLabs = true;
1422 }
1423
1424
1425 /*
1426 useAbs - indicate we have used abs.
1427 */
1428
1429 extern "C" void keyc_useAbs (void)
1430 {
1431 seenAbs = true;
1432 }
1433
1434
1435 /*
1436 useFabs - indicate we have used fabs.
1437 */
1438
1439 extern "C" void keyc_useFabs (void)
1440 {
1441 seenFabs = true;
1442 }
1443
1444
1445 /*
1446 useFabsl - indicate we have used fabsl.
1447 */
1448
1449 extern "C" void keyc_useFabsl (void)
1450 {
1451 seenFabsl = true;
1452 }
1453
1454
1455 /*
1456 useException - use the exceptions module, mcrts.
1457 */
1458
1459 extern "C" void keyc_useException (void)
1460 {
1461 seenException = true;
1462 }
1463
1464
1465 /*
1466 useComplex - use the complex data type.
1467 */
1468
1469 extern "C" void keyc_useComplex (void)
1470 {
1471 seenComplex = true;
1472 }
1473
1474
1475 /*
1476 useM2RTS - indicate we have used M2RTS in the converted code.
1477 */
1478
1479 extern "C" void keyc_useM2RTS (void)
1480 {
1481 seenM2RTS = true;
1482 }
1483
1484
1485 /*
1486 useStrlen - indicate we have used strlen in the converted code.
1487 */
1488
1489 extern "C" void keyc_useStrlen (void)
1490 {
1491 seenStrlen = true;
1492 }
1493
1494
1495 /*
1496 useCtype - indicate we have used the toupper function.
1497 */
1498
1499 extern "C" void keyc_useCtype (void)
1500 {
1501 seenCtype = true;
1502 }
1503
1504
1505 /*
1506 genDefs - generate definitions or includes for all
1507 macros and prototypes used.
1508 */
1509
1510 extern "C" void keyc_genDefs (mcPretty_pretty p)
1511 {
1512 genBool (p);
1513 checkFreeMalloc (p);
1514 checkProc (p);
1515 checkTrue (p);
1516 checkFalse (p);
1517 checkNull (p);
1518 checkMemcpy (p);
1519 checkLimits (p);
1520 checkAbs (p);
1521 checkStorage (p);
1522 checkException (p);
1523 checkComplex (p);
1524 checkCtype (p);
1525 checkUnistd (p);
1526 checkSysTypes (p);
1527 checkM2RTS (p);
1528 checkThrow (p);
1529 fixNullPointerConst (p);
1530 }
1531
1532
1533 /*
1534 genConfigSystem - generate include files for config.h and system.h
1535 within the GCC framework.
1536 */
1537
1538 extern "C" void keyc_genConfigSystem (mcPretty_pretty p)
1539 {
1540 checkGccConfigSystem (p);
1541 }
1542
1543
1544 /*
1545 enterScope - enter a scope defined by, n.
1546 */
1547
1548 extern "C" void keyc_enterScope (decl_node n)
1549 {
1550 keyc_scope s;
1551
1552 s = new_ (n);
1553 s->scoped = n;
1554 s->symbols = symbolKey_initTree ();
1555 s->next = stack;
1556 stack = s;
1557 }
1558
1559
1560 /*
1561 leaveScope - leave the scope defined by, n.
1562 */
1563
1564 extern "C" void keyc_leaveScope (decl_node n)
1565 {
1566 keyc_scope s;
1567
1568 if (n == stack->scoped)
1569 {
1570 s = stack;
1571 stack = stack->next;
1572 s->scoped = static_cast<decl_node> (NULL);
1573 symbolKey_killTree (&s->symbols);
1574 s->next = NULL;
1575 }
1576 else
1577 {
1578 M2RTS_HALT (-1);
1579 __builtin_unreachable ();
1580 }
1581 }
1582
1583
1584 /*
1585 cname - attempts to declare a symbol with name, n, in the
1586 current scope. If there is no conflict with the
1587 target language then NIL is returned, otherwise
1588 a mangled name is returned as a String.
1589 If scopes is FALSE then only the keywords and
1590 macros are detected for a clash (all scoping
1591 is ignored).
1592 */
1593
1594 extern "C" DynamicStrings_String keyc_cname (nameKey_Name n, bool scopes)
1595 {
1596 DynamicStrings_String m;
1597
1598 m = static_cast<DynamicStrings_String> (NULL);
1599 if (clash (n, scopes))
1600 {
1601 if (((mangle1 (n, &m, scopes)) || (mangle2 (n, &m, scopes))) || (mangleN (n, &m, scopes)))
1602 {
1603 /* avoid dangling else. */
1604 if (scopes)
1605 {
1606 /* no longer a clash with, m, so add it to the current scope. */
1607 n = nameKey_makekey (DynamicStrings_string (m));
1608 symbolKey_putSymKey (stack->symbols, n, reinterpret_cast <void *> (m));
1609 }
1610 }
1611 else
1612 {
1613 /* mangleN must always succeed. */
1614 M2RTS_HALT (-1);
1615 __builtin_unreachable ();
1616 }
1617 }
1618 else if (scopes)
1619 {
1620 /* avoid dangling else. */
1621 /* no clash, add it to the current scope. */
1622 symbolKey_putSymKey (stack->symbols, n, reinterpret_cast <void *> (DynamicStrings_InitStringCharStar (nameKey_keyToCharStar (n))));
1623 }
1624 return m;
1625 /* static analysis guarentees a RETURN statement will be used before here. */
1626 __builtin_unreachable ();
1627 }
1628
1629
1630 /*
1631 cnamen - attempts to declare a symbol with name, n, in the
1632 current scope. If there is no conflict with the
1633 target language then NIL is returned, otherwise
1634 a mangled name is returned as a Name
1635 If scopes is FALSE then only the keywords and
1636 macros are detected for a clash (all scoping
1637 is ignored).
1638 */
1639
1640 extern "C" nameKey_Name keyc_cnamen (nameKey_Name n, bool scopes)
1641 {
1642 DynamicStrings_String m;
1643
1644 m = static_cast<DynamicStrings_String> (NULL);
1645 if (clash (n, scopes))
1646 {
1647 if (((mangle1 (n, &m, scopes)) || (mangle2 (n, &m, scopes))) || (mangleN (n, &m, scopes)))
1648 {
1649 /* avoid dangling else. */
1650 n = nameKey_makekey (DynamicStrings_string (m));
1651 if (scopes)
1652 {
1653 /* no longer a clash with, m, so add it to the current scope. */
1654 symbolKey_putSymKey (stack->symbols, n, reinterpret_cast <void *> (m));
1655 }
1656 }
1657 else
1658 {
1659 /* mangleN must always succeed. */
1660 M2RTS_HALT (-1);
1661 __builtin_unreachable ();
1662 }
1663 }
1664 else if (scopes)
1665 {
1666 /* avoid dangling else. */
1667 /* no clash, add it to the current scope. */
1668 symbolKey_putSymKey (stack->symbols, n, reinterpret_cast <void *> (DynamicStrings_InitStringCharStar (nameKey_keyToCharStar (n))));
1669 }
1670 m = DynamicStrings_KillString (m);
1671 return n;
1672 /* static analysis guarentees a RETURN statement will be used before here. */
1673 __builtin_unreachable ();
1674 }
1675
1676
1677 /*
1678 cp - include C++ keywords and standard declarations to avoid.
1679 */
1680
1681 extern "C" void keyc_cp (void)
1682 {
1683 if (! initializedCP)
1684 {
1685 initializedCP = true;
1686 initCP ();
1687 }
1688 }
1689
1690 extern "C" void _M2_keyc_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1691 {
1692 init ();
1693 }
1694
1695 extern "C" void _M2_keyc_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1696 {
1697 }