libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / m2 / mc-boot / Gkeyc.cc
blob3a5dd5ccc1de44704d12f200c1717a76c5723748
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-2024 Free Software Foundation, Inc.
5 This file is part of GNU Modula-2.
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.
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.
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/>. */
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
30 # if !defined (TRUE)
31 # define TRUE (1==1)
32 # endif
34 # if !defined (FALSE)
35 # define FALSE (1==0)
36 # endif
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
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"
55 #if !defined (decl_node_D)
56 # define decl_node_D
57 typedef void *decl_node;
58 #endif
60 typedef struct keyc__T1_r keyc__T1;
62 typedef keyc__T1 *keyc_scope;
64 struct keyc__T1_r {
65 decl_node scoped;
66 symbolKey_symbolTree symbols;
67 keyc_scope next;
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;
114 useGccTree - indicate we have imported tree from gcctypes.
117 extern "C" void keyc_useGccTree (void);
120 useGccLocation - indicate we have imported tree from gcctypes.
123 extern "C" void keyc_useGccLocation (void);
126 useUnistd - need to use unistd.h call using open/close/read/write require this header.
129 extern "C" void keyc_useUnistd (void);
132 useThrow - use the throw function.
135 extern "C" void keyc_useThrow (void);
138 useStorage - indicate we have used storage.
141 extern "C" void keyc_useStorage (void);
144 useFree - indicate we have used free.
147 extern "C" void keyc_useFree (void);
150 useMalloc - indicate we have used malloc.
153 extern "C" void keyc_useMalloc (void);
156 useProc - indicate we have used proc.
159 extern "C" void keyc_useProc (void);
162 useTrue - indicate we have used TRUE.
165 extern "C" void keyc_useTrue (void);
168 useFalse - indicate we have used FALSE.
171 extern "C" void keyc_useFalse (void);
174 useNull - indicate we have used NULL.
177 extern "C" void keyc_useNull (void);
180 useMemcpy - indicate we have used memcpy.
183 extern "C" void keyc_useMemcpy (void);
186 useIntMin - indicate we have used INT_MIN.
189 extern "C" void keyc_useIntMin (void);
192 useUIntMin - indicate we have used UINT_MIN.
195 extern "C" void keyc_useUIntMin (void);
198 useLongMin - indicate we have used LONG_MIN.
201 extern "C" void keyc_useLongMin (void);
204 useULongMin - indicate we have used ULONG_MIN.
207 extern "C" void keyc_useULongMin (void);
210 useCharMin - indicate we have used CHAR_MIN.
213 extern "C" void keyc_useCharMin (void);
216 useUCharMin - indicate we have used UCHAR_MIN.
219 extern "C" void keyc_useUCharMin (void);
222 useIntMax - indicate we have used INT_MAX.
225 extern "C" void keyc_useIntMax (void);
228 useUIntMax - indicate we have used UINT_MAX.
231 extern "C" void keyc_useUIntMax (void);
234 useLongMax - indicate we have used LONG_MAX.
237 extern "C" void keyc_useLongMax (void);
240 useULongMax - indicate we have used ULONG_MAX.
243 extern "C" void keyc_useULongMax (void);
246 useCharMax - indicate we have used CHAR_MAX.
249 extern "C" void keyc_useCharMax (void);
252 useUCharMax - indicate we have used UChar_MAX.
255 extern "C" void keyc_useUCharMax (void);
258 useSize_t - indicate we have used size_t.
261 extern "C" void keyc_useSize_t (void);
264 useSSize_t - indicate we have used ssize_t.
267 extern "C" void keyc_useSSize_t (void);
270 useLabs - indicate we have used labs.
273 extern "C" void keyc_useLabs (void);
276 useAbs - indicate we have used abs.
279 extern "C" void keyc_useAbs (void);
282 useFabs - indicate we have used fabs.
285 extern "C" void keyc_useFabs (void);
288 useFabsl - indicate we have used fabsl.
291 extern "C" void keyc_useFabsl (void);
294 useException - use the exceptions module, mcrts.
297 extern "C" void keyc_useException (void);
300 useComplex - use the complex data type.
303 extern "C" void keyc_useComplex (void);
306 useM2RTS - indicate we have used M2RTS in the converted code.
309 extern "C" void keyc_useM2RTS (void);
312 useStrlen - indicate we have used strlen in the converted code.
315 extern "C" void keyc_useStrlen (void);
318 useCtype - indicate we have used the toupper function.
321 extern "C" void keyc_useCtype (void);
324 genDefs - generate definitions or includes for all
325 macros and prototypes used.
328 extern "C" void keyc_genDefs (mcPretty_pretty p);
331 genConfigSystem - generate include files for config.h and system.h
332 within the GCC framework.
335 extern "C" void keyc_genConfigSystem (mcPretty_pretty p);
338 enterScope - enter a scope defined by, n.
341 extern "C" void keyc_enterScope (decl_node n);
344 leaveScope - leave the scope defined by, n.
347 extern "C" void keyc_leaveScope (decl_node n);
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).
359 extern "C" DynamicStrings_String keyc_cname (nameKey_Name n, bool scopes);
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).
371 extern "C" nameKey_Name keyc_cnamen (nameKey_Name n, bool scopes);
374 cp - include C++ keywords and standard declarations to avoid.
377 extern "C" void keyc_cp (void);
380 checkGccConfigSystem - issues the GCC include config.h, include system.h
381 instead of the standard host include.
384 static void checkGccConfigSystem (mcPretty_pretty p);
387 checkGccTypes - if we have imported tree or location_t from gcctypes
388 then we include the gcc headers.
391 static void checkGccTypes (mcPretty_pretty p);
394 checkCtype -
397 static void checkCtype (mcPretty_pretty p);
400 checkAbs - check to see if the abs family, size_t or ssize_t have been used.
403 static void checkAbs (mcPretty_pretty p);
406 checkLimits -
409 static void checkLimits (mcPretty_pretty p);
412 checkFreeMalloc -
415 static void checkFreeMalloc (mcPretty_pretty p);
418 checkStorage -
421 static void checkStorage (mcPretty_pretty p);
424 checkProc -
427 static void checkProc (mcPretty_pretty p);
430 checkTrue -
433 static void checkTrue (mcPretty_pretty p);
436 checkFalse -
439 static void checkFalse (mcPretty_pretty p);
442 checkNull -
445 static void checkNull (mcPretty_pretty p);
448 checkMemcpy -
451 static void checkMemcpy (mcPretty_pretty p);
454 checkM2RTS -
457 static void checkM2RTS (mcPretty_pretty p);
460 checkException - check to see if exceptions were used.
463 static void checkException (mcPretty_pretty p);
466 checkThrow - check to see if the throw function is used.
469 static void checkThrow (mcPretty_pretty p);
472 checkUnistd - check to see if the unistd.h header file is required.
475 static void checkUnistd (mcPretty_pretty p);
478 checkComplex - check to see if the type complex was used.
481 static void checkComplex (mcPretty_pretty p);
484 checkSysTypes - emit header for sys/types.h if necessary.
487 static void checkSysTypes (mcPretty_pretty p);
490 fixNullPointerConst - fixup for NULL on some C++11 systems.
493 static void fixNullPointerConst (mcPretty_pretty p);
496 genBool -
499 static void genBool (mcPretty_pretty p);
502 new -
505 static keyc_scope new_ (decl_node n);
508 mangle1 - returns TRUE if name is unique if we add _
509 to its end.
512 static bool mangle1 (nameKey_Name n, DynamicStrings_String *m, bool scopes);
515 mangle2 - returns TRUE if name is unique if we prepend _
516 to, n.
519 static bool mangle2 (nameKey_Name n, DynamicStrings_String *m, bool scopes);
522 mangleN - keep adding '_' to the end of n until it
523 no longer clashes.
526 static bool mangleN (nameKey_Name n, DynamicStrings_String *m, bool scopes);
529 clash - returns TRUE if there is a clash with name, n,
530 in the current scope or C keywords or C macros.
533 static bool clash (nameKey_Name n, bool scopes);
536 initCP - add the extra keywords and standard definitions used by C++.
539 static void initCP (void);
542 add -
545 static void add (symbolKey_symbolTree s, const char *a_, unsigned int _a_high);
548 initMacros - macros and library function names to avoid.
551 static void initMacros (void);
554 initKeywords - keywords to avoid.
557 static void initKeywords (void);
560 init -
563 static void init (void);
567 checkGccConfigSystem - issues the GCC include config.h, include system.h
568 instead of the standard host include.
571 static void checkGccConfigSystem (mcPretty_pretty p)
573 if (mcOptions_getGccConfigSystem ())
575 if (! initializedGCC)
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);
587 checkGccTypes - if we have imported tree or location_t from gcctypes
588 then we include the gcc headers.
591 static void checkGccTypes (mcPretty_pretty p)
593 if (seenGccTree || seenGccLocation)
595 mcPretty_print (p, (const char *) "#include \"gcc-consolidation.h\"\\n\\n", 34);
601 checkCtype -
604 static void checkCtype (mcPretty_pretty p)
606 if (seenCtype)
608 checkGccConfigSystem (p);
609 if (mcOptions_getGccConfigSystem ())
611 /* GCC header files use a safe variant. */
612 mcPretty_print (p, (const char *) "#include <safe-ctype.h>\\n", 25);
614 else
616 mcPretty_print (p, (const char *) "#include <ctype.h>\\n", 20);
623 checkAbs - check to see if the abs family, size_t or ssize_t have been used.
626 static void checkAbs (mcPretty_pretty p)
628 if (((((seenLabs || seenAbs) || seenFabs) || seenFabsl) || seenSize_t) || seenSSize_t)
630 checkGccConfigSystem (p);
631 if (! (mcOptions_getGccConfigSystem ()))
633 mcPretty_print (p, (const char *) "#include <stdlib.h>\\n", 21);
640 checkLimits -
643 static void checkLimits (mcPretty_pretty p)
645 if ((((((((((((seenMemcpy || seenIntMin) || seenUIntMin) || seenLongMin) || seenULongMin) || seenCharMin) || seenUCharMin) || seenIntMax) || seenUIntMax) || seenLongMax) || seenULongMax) || seenCharMax) || seenUCharMax) /* OR seenUIntMax */
647 checkGccConfigSystem (p);
648 if (! (mcOptions_getGccConfigSystem ()))
650 mcPretty_print (p, (const char *) "#include <limits.h>\\n", 21);
657 checkFreeMalloc -
660 static void checkFreeMalloc (mcPretty_pretty p)
662 if (seenFree || seenMalloc)
664 checkGccConfigSystem (p);
665 if (! (mcOptions_getGccConfigSystem ()))
667 mcPretty_print (p, (const char *) "#include <stdlib.h>\\n", 21);
674 checkStorage -
677 static void checkStorage (mcPretty_pretty p)
679 if (seenStorage)
681 mcPretty_print (p, (const char *) "# include \"", 13);
682 mcPretty_prints (p, mcOptions_getHPrefix ());
683 mcPretty_print (p, (const char *) "Storage.h\"\\n", 12);
689 checkProc -
692 static void checkProc (mcPretty_pretty p)
694 if (seenProc)
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);
706 checkTrue -
709 static void checkTrue (mcPretty_pretty p)
711 if (seenTrue)
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);
721 checkFalse -
724 static void checkFalse (mcPretty_pretty p)
726 if (seenFalse)
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);
736 checkNull -
739 static void checkNull (mcPretty_pretty p)
741 if (seenNull)
743 checkGccConfigSystem (p);
744 if (! (mcOptions_getGccConfigSystem ()))
746 mcPretty_print (p, (const char *) "#include <stddef.h>\\n", 21);
753 checkMemcpy -
756 static void checkMemcpy (mcPretty_pretty p)
758 if (seenMemcpy || seenStrlen)
760 checkGccConfigSystem (p);
761 if (! (mcOptions_getGccConfigSystem ()))
763 mcPretty_print (p, (const char *) "#include <string.h>\\n", 21);
770 checkM2RTS -
773 static void checkM2RTS (mcPretty_pretty p)
775 if (seenM2RTS)
777 mcPretty_print (p, (const char *) "# include \"", 13);
778 mcPretty_prints (p, mcOptions_getHPrefix ());
779 mcPretty_print (p, (const char *) "M2RTS.h\"\\n", 10);
785 checkException - check to see if exceptions were used.
788 static void checkException (mcPretty_pretty p)
790 if (seenException)
792 mcPretty_print (p, (const char *) "# include \"Gmcrts.h\"\\n", 24);
798 checkThrow - check to see if the throw function is used.
801 static void checkThrow (mcPretty_pretty p)
803 if (seenThrow)
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);
815 checkUnistd - check to see if the unistd.h header file is required.
818 static void checkUnistd (mcPretty_pretty p)
820 if (seenUnistd)
822 checkGccConfigSystem (p);
823 if (! (mcOptions_getGccConfigSystem ()))
825 mcPretty_print (p, (const char *) "#include <unistd.h>\\n", 21);
832 checkComplex - check to see if the type complex was used.
835 static void checkComplex (mcPretty_pretty p)
837 if (seenComplex)
839 checkGccConfigSystem (p);
840 if (! (mcOptions_getGccConfigSystem ()))
842 mcPretty_print (p, (const char *) "# include <complex.h>\\n", 25);
849 checkSysTypes - emit header for sys/types.h if necessary.
852 static void checkSysTypes (mcPretty_pretty p)
854 if (seenSysTypes)
856 checkGccConfigSystem (p);
857 if (! (mcOptions_getGccConfigSystem ()))
859 mcPretty_print (p, (const char *) "# include <sys/types.h>\\n", 27);
866 fixNullPointerConst - fixup for NULL on some C++11 systems.
869 static void fixNullPointerConst (mcPretty_pretty p)
871 if (seenNull)
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);
882 genBool -
885 static void genBool (mcPretty_pretty p)
887 if (mcOptions_useBool ())
889 mcPretty_print (p, (const char *) "#include <stdbool.h>\\n", 22);
895 new -
898 static keyc_scope new_ (decl_node n)
900 keyc_scope s;
902 if (freeList == NULL)
904 Storage_ALLOCATE ((void **) &s, sizeof (keyc__T1));
906 else
908 s = freeList;
909 freeList = freeList->next;
911 return s;
912 /* static analysis guarentees a RETURN statement will be used before here. */
913 __builtin_unreachable ();
918 mangle1 - returns TRUE if name is unique if we add _
919 to its end.
922 static bool mangle1 (nameKey_Name n, DynamicStrings_String *m, bool scopes)
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 ();
934 mangle2 - returns TRUE if name is unique if we prepend _
935 to, n.
938 static bool mangle2 (nameKey_Name n, DynamicStrings_String *m, bool scopes)
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 ();
950 mangleN - keep adding '_' to the end of n until it
951 no longer clashes.
954 static bool mangleN (nameKey_Name n, DynamicStrings_String *m, bool scopes)
956 (*m) = DynamicStrings_KillString ((*m));
957 (*m) = DynamicStrings_InitStringCharStar (nameKey_keyToCharStar (n));
958 for (;;)
960 (*m) = DynamicStrings_ConCatChar ((*m), '_');
961 if (! (clash (nameKey_makekey (DynamicStrings_string ((*m))), scopes)))
963 return true;
966 ReturnException ("../../gcc/m2/mc/keyc.def", 20, 1);
967 __builtin_unreachable ();
972 clash - returns TRUE if there is a clash with name, n,
973 in the current scope or C keywords or C macros.
976 static bool clash (nameKey_Name n, bool scopes)
978 if (((symbolKey_getSymKey (macros, n)) != NULL) || ((symbolKey_getSymKey (keywords, n)) != NULL))
980 return true;
982 return scopes && ((symbolKey_getSymKey (stack->symbols, n)) != NULL);
983 /* static analysis guarentees a RETURN statement will be used before here. */
984 __builtin_unreachable ();
989 initCP - add the extra keywords and standard definitions used by C++.
992 static void initCP (void)
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);
1005 add -
1008 static void add (symbolKey_symbolTree s, const char *a_, unsigned int _a_high)
1010 char a[_a_high+1];
1012 /* make a local copy of each unbounded array. */
1013 memcpy (a, a_, _a_high+1);
1015 symbolKey_putSymKey (s, nameKey_makeKey ((const char *) a, _a_high), reinterpret_cast <void *> (DynamicStrings_InitString ((const char *) a, _a_high)));
1020 initMacros - macros and library function names to avoid.
1023 static void initMacros (void)
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);
1057 initKeywords - keywords to avoid.
1060 static void initKeywords (void)
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);
1104 init -
1107 static void init (void)
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 ();
1155 useGccTree - indicate we have imported tree from gcctypes.
1158 extern "C" void keyc_useGccTree (void)
1160 seenGccTree = true;
1165 useGccLocation - indicate we have imported tree from gcctypes.
1168 extern "C" void keyc_useGccLocation (void)
1170 seenGccLocation = true;
1175 useUnistd - need to use unistd.h call using open/close/read/write require this header.
1178 extern "C" void keyc_useUnistd (void)
1180 seenUnistd = true;
1185 useThrow - use the throw function.
1188 extern "C" void keyc_useThrow (void)
1190 seenThrow = true;
1195 useStorage - indicate we have used storage.
1198 extern "C" void keyc_useStorage (void)
1200 seenStorage = true;
1205 useFree - indicate we have used free.
1208 extern "C" void keyc_useFree (void)
1210 seenFree = true;
1215 useMalloc - indicate we have used malloc.
1218 extern "C" void keyc_useMalloc (void)
1220 seenMalloc = true;
1225 useProc - indicate we have used proc.
1228 extern "C" void keyc_useProc (void)
1230 seenProc = true;
1235 useTrue - indicate we have used TRUE.
1238 extern "C" void keyc_useTrue (void)
1240 seenTrue = true;
1245 useFalse - indicate we have used FALSE.
1248 extern "C" void keyc_useFalse (void)
1250 seenFalse = true;
1255 useNull - indicate we have used NULL.
1258 extern "C" void keyc_useNull (void)
1260 seenNull = true;
1265 useMemcpy - indicate we have used memcpy.
1268 extern "C" void keyc_useMemcpy (void)
1270 seenMemcpy = true;
1275 useIntMin - indicate we have used INT_MIN.
1278 extern "C" void keyc_useIntMin (void)
1280 seenIntMin = true;
1285 useUIntMin - indicate we have used UINT_MIN.
1288 extern "C" void keyc_useUIntMin (void)
1290 seenUIntMin = true;
1295 useLongMin - indicate we have used LONG_MIN.
1298 extern "C" void keyc_useLongMin (void)
1300 seenLongMin = true;
1305 useULongMin - indicate we have used ULONG_MIN.
1308 extern "C" void keyc_useULongMin (void)
1310 seenULongMin = true;
1315 useCharMin - indicate we have used CHAR_MIN.
1318 extern "C" void keyc_useCharMin (void)
1320 seenCharMin = true;
1325 useUCharMin - indicate we have used UCHAR_MIN.
1328 extern "C" void keyc_useUCharMin (void)
1330 seenUCharMin = true;
1335 useIntMax - indicate we have used INT_MAX.
1338 extern "C" void keyc_useIntMax (void)
1340 seenIntMax = true;
1345 useUIntMax - indicate we have used UINT_MAX.
1348 extern "C" void keyc_useUIntMax (void)
1350 seenUIntMax = true;
1355 useLongMax - indicate we have used LONG_MAX.
1358 extern "C" void keyc_useLongMax (void)
1360 seenLongMax = true;
1365 useULongMax - indicate we have used ULONG_MAX.
1368 extern "C" void keyc_useULongMax (void)
1370 seenULongMax = true;
1375 useCharMax - indicate we have used CHAR_MAX.
1378 extern "C" void keyc_useCharMax (void)
1380 seenCharMax = true;
1385 useUCharMax - indicate we have used UChar_MAX.
1388 extern "C" void keyc_useUCharMax (void)
1390 seenUCharMax = true;
1395 useSize_t - indicate we have used size_t.
1398 extern "C" void keyc_useSize_t (void)
1400 seenSize_t = true;
1405 useSSize_t - indicate we have used ssize_t.
1408 extern "C" void keyc_useSSize_t (void)
1410 seenSSize_t = true;
1411 seenSysTypes = true;
1416 useLabs - indicate we have used labs.
1419 extern "C" void keyc_useLabs (void)
1421 seenLabs = true;
1426 useAbs - indicate we have used abs.
1429 extern "C" void keyc_useAbs (void)
1431 seenAbs = true;
1436 useFabs - indicate we have used fabs.
1439 extern "C" void keyc_useFabs (void)
1441 seenFabs = true;
1446 useFabsl - indicate we have used fabsl.
1449 extern "C" void keyc_useFabsl (void)
1451 seenFabsl = true;
1456 useException - use the exceptions module, mcrts.
1459 extern "C" void keyc_useException (void)
1461 seenException = true;
1466 useComplex - use the complex data type.
1469 extern "C" void keyc_useComplex (void)
1471 seenComplex = true;
1476 useM2RTS - indicate we have used M2RTS in the converted code.
1479 extern "C" void keyc_useM2RTS (void)
1481 seenM2RTS = true;
1486 useStrlen - indicate we have used strlen in the converted code.
1489 extern "C" void keyc_useStrlen (void)
1491 seenStrlen = true;
1496 useCtype - indicate we have used the toupper function.
1499 extern "C" void keyc_useCtype (void)
1501 seenCtype = true;
1506 genDefs - generate definitions or includes for all
1507 macros and prototypes used.
1510 extern "C" void keyc_genDefs (mcPretty_pretty p)
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);
1534 genConfigSystem - generate include files for config.h and system.h
1535 within the GCC framework.
1538 extern "C" void keyc_genConfigSystem (mcPretty_pretty p)
1540 checkGccConfigSystem (p);
1545 enterScope - enter a scope defined by, n.
1548 extern "C" void keyc_enterScope (decl_node n)
1550 keyc_scope s;
1552 s = new_ (n);
1553 s->scoped = n;
1554 s->symbols = symbolKey_initTree ();
1555 s->next = stack;
1556 stack = s;
1561 leaveScope - leave the scope defined by, n.
1564 extern "C" void keyc_leaveScope (decl_node n)
1566 keyc_scope s;
1568 if (n == stack->scoped)
1570 s = stack;
1571 stack = stack->next;
1572 s->scoped = static_cast<decl_node> (NULL);
1573 symbolKey_killTree (&s->symbols);
1574 s->next = NULL;
1576 else
1578 M2RTS_HALT (-1);
1579 __builtin_unreachable ();
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).
1594 extern "C" DynamicStrings_String keyc_cname (nameKey_Name n, bool scopes)
1596 DynamicStrings_String m;
1598 m = static_cast<DynamicStrings_String> (NULL);
1599 if (clash (n, scopes))
1601 if (((mangle1 (n, &m, scopes)) || (mangle2 (n, &m, scopes))) || (mangleN (n, &m, scopes)))
1603 /* avoid dangling else. */
1604 if (scopes)
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));
1611 else
1613 /* mangleN must always succeed. */
1614 M2RTS_HALT (-1);
1615 __builtin_unreachable ();
1618 else if (scopes)
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))));
1624 return m;
1625 /* static analysis guarentees a RETURN statement will be used before here. */
1626 __builtin_unreachable ();
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).
1640 extern "C" nameKey_Name keyc_cnamen (nameKey_Name n, bool scopes)
1642 DynamicStrings_String m;
1644 m = static_cast<DynamicStrings_String> (NULL);
1645 if (clash (n, scopes))
1647 if (((mangle1 (n, &m, scopes)) || (mangle2 (n, &m, scopes))) || (mangleN (n, &m, scopes)))
1649 /* avoid dangling else. */
1650 n = nameKey_makekey (DynamicStrings_string (m));
1651 if (scopes)
1653 /* no longer a clash with, m, so add it to the current scope. */
1654 symbolKey_putSymKey (stack->symbols, n, reinterpret_cast <void *> (m));
1657 else
1659 /* mangleN must always succeed. */
1660 M2RTS_HALT (-1);
1661 __builtin_unreachable ();
1664 else if (scopes)
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))));
1670 m = DynamicStrings_KillString (m);
1671 return n;
1672 /* static analysis guarentees a RETURN statement will be used before here. */
1673 __builtin_unreachable ();
1678 cp - include C++ keywords and standard declarations to avoid.
1681 extern "C" void keyc_cp (void)
1683 if (! initializedCP)
1685 initializedCP = true;
1686 initCP ();
1690 extern "C" void _M2_keyc_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1692 init ();
1695 extern "C" void _M2_keyc_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])