1 /* System-dependent definitions for Bison.
2
3 Copyright (C) 2000-2007, 2009-2015, 2018-2021 Free Software
4 Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19 #ifndef BISON_SYSTEM_H
20 # define BISON_SYSTEM_H
21
22 /* flex 2.5.31 gratuitously defines macros like INT8_MIN. But this
23 runs afoul of pre-C99 compilers that have <inttypes.h> or
24 <stdint.h>, which are included below if available. It also runs
25 afoul of pre-C99 compilers that define these macros in <limits.h>. */
26 # if ! defined __STDC_VERSION__ || __STDC_VERSION__ < 199901
27 # undef INT8_MIN
28 # undef INT16_MIN
29 # undef INT32_MIN
30 # undef INT8_MAX
31 # undef INT16_MAX
32 # undef UINT8_MAX
33 # undef INT32_MAX
34 # undef UINT16_MAX
35 # undef UINT32_MAX
36 # endif
37
38 # include <limits.h>
39 # include <stddef.h>
40 # include <stdlib.h>
41 # include <string.h>
42
43 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
44 # define STREQ(L, R) (strcmp(L, R) == 0)
45 # define STRNEQ(L, R) (!STREQ(L, R))
46
47 /* Just like strncmp, but the second argument must be a literal string
48 and you don't specify the length. */
49 # define STRNCMP_LIT(S, Literal) \
50 strncmp (S, "" Literal "", sizeof (Literal) - 1)
51
52 /* Whether Literal is a prefix of S. */
53 # define STRPREFIX_LIT(Literal, S) \
54 (STRNCMP_LIT (S, Literal) == 0)
55
56 # include <unistd.h>
57 # include <inttypes.h>
58
59 # ifndef UINTPTR_MAX
60 /* This isn't perfect, but it's good enough for Bison, which needs
61 only to hash pointers. */
62 typedef size_t uintptr_t;
63 # endif
64
65 /* Version mismatch. */
66 # define EX_MISMATCH 63
67
68 /*---------.
69 | Gnulib. |
70 `---------*/
71
72 # include <unlocked-io.h>
73 # include <verify.h>
74 # include <xalloc.h>
75
76 // Clang and ICC like to pretend they are GCC.
77 # if defined __GNUC__ && !defined __clang__ && !defined __ICC
78 # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
79 # endif
80
81 // See https://lists.gnu.org/r/bug-bison/2019-10/msg00061.html
82 // and https://trac.macports.org/ticket/59927.
83 # if defined GCC_VERSION && 405 <= GCC_VERSION
84 # define IGNORE_TYPE_LIMITS_BEGIN \
85 _Pragma ("GCC diagnostic push") \
86 _Pragma ("GCC diagnostic ignored \"-Wtype-limits\"")
87 # define IGNORE_TYPE_LIMITS_END \
88 _Pragma ("GCC diagnostic pop")
89 # else
90 # define IGNORE_TYPE_LIMITS_BEGIN
91 # define IGNORE_TYPE_LIMITS_END
92 # endif
93
94
95 /*-----------------.
96 | GCC extensions. |
97 `-----------------*/
98
99 /* Use PACIFY_CC to indicate that Code is unimportant to the logic of Bison
100 but that it is necessary for suppressing compiler warnings. For example,
101 Code might be a variable initializer that's always overwritten before the
102 variable is used.
103
104 PACIFY_CC is intended to be useful only as a comment as it does not alter
105 Code. It is tempting to redefine PACIFY_CC so that it will suppress Code
106 when configuring without --enable-gcc-warnings. However, that would mean
107 that, for maintainers, Bison would compile with potentially less warnings
108 and safer logic than it would for users. Due to the overhead of M4,
109 suppressing Code is unlikely to offer any significant improvement in
110 Bison's performance anyway. */
111 # define PACIFY_CC(Code) Code
112
113 # include <attribute.h>
114
115
116 /*------.
117 | NLS. |
118 `------*/
119
120 # include <locale.h>
121
122 # include <gettext.h>
123 # define _(Msgid) gettext (Msgid)
124 # define N_(Msgid) (Msgid)
125
126
127 /*-----------.
128 | Booleans. |
129 `-----------*/
130
131 # include <stdbool.h>
132
133
134 /*-----------.
135 | Integers. |
136 `-----------*/
137
138 static inline int
139 min_int (int a, int b)
140 {
141 return a < b ? a : b;
142 }
143
144 static inline int
145 max_int (int a, int b)
146 {
147 return a >= b ? a : b;
148 }
149
150
151 /*-------------.
152 | Assertions. |
153 `-------------*/
154
155 /* In the past, Bison defined aver to simply invoke abort in the case of
156 a failed assertion. The rationale was that <assert.h>'s assertions
157 were too heavyweight and could be disabled too easily. See
158 discussions at
159 <https://lists.gnu.org/r/bison-patches/2006-01/msg00080.html>
160 <https://lists.gnu.org/r/bison-patches/2006-09/msg00111.html>.
161
162 However, normal assert output can be helpful during development and
163 in bug reports from users. Moreover, it's not clear now that
164 <assert.h>'s assertions are significantly heavyweight. Finally, if
165 users want to experiment with disabling assertions, it's debatable
166 whether it's our responsibility to stop them. See discussion
167 starting at
168 <https://lists.gnu.org/r/bison-patches/2009-09/msg00013.html>.
169
170 For now, we use assert but we call it aver throughout Bison in case
171 we later wish to try another scheme.
172 */
173 # include <assert.h>
174 # define aver assert
175
176
177 /*-----------.
178 | Obstacks. |
179 `-----------*/
180
181 # define obstack_chunk_alloc xmalloc
182 # define obstack_chunk_free free
183 # include <obstack.h>
184
185 /* String-grow: append Str to Obs. */
186
187 # define obstack_sgrow(Obs, Str) \
188 obstack_grow (Obs, Str, strlen (Str))
189
190 /* Output Str escaped to be a string.
191
192 For instance "\"foo\"" -> "\\\"foo\\\"". */
193
194 # define obstack_backslash(Obs, Str) \
195 do { \
196 char const *p__; \
197 for (p__ = Str; *p__; p__++) \
198 switch (*p__) \
199 { \
200 case '"': obstack_sgrow (Obs, "\\\""); break; \
201 case '\\': obstack_sgrow (Obs, "\\\\"); break; \
202 default: obstack_1grow (Obs, *p__); break; \
203 } \
204 } while (0)
205
206
207 /* Output Str escaped for our postprocessing (i.e., escape M4 special
208 characters).
209
210 For instance "[foo]" -> "@{foo@}", "$$" -> "$][$][". */
211
212 # define obstack_escape(Obs, Str) \
213 do { \
214 char const *p__; \
215 for (p__ = Str; *p__; p__++) \
216 switch (*p__) \
217 { \
218 case '$': obstack_sgrow (Obs, "$]["); break; \
219 case '@': obstack_sgrow (Obs, "@@" ); break; \
220 case '[': obstack_sgrow (Obs, "@{" ); break; \
221 case ']': obstack_sgrow (Obs, "@}" ); break; \
222 default: obstack_1grow (Obs, *p__ ); break; \
223 } \
224 } while (0)
225
226
227 /* Output Str both quoted for M4 (i.e., embed in [[...]]), and escaped
228 for our postprocessing (i.e., escape M4 special characters). If
229 Str is empty (or NULL), output "[]" instead of "[[]]" as it makes
230 M4 programming easier (m4_ifval can be used).
231
232 For instance "[foo]" -> "[[@{foo@}]]", "$$" -> "[[$][$][]]". */
233
234 # define obstack_quote(Obs, Str) \
235 do { \
236 char const* obstack_quote_p = Str; \
237 if (obstack_quote_p && obstack_quote_p[0]) \
238 { \
239 obstack_sgrow (Obs, "[["); \
240 obstack_escape (Obs, obstack_quote_p); \
241 obstack_sgrow (Obs, "]]"); \
242 } \
243 else \
244 obstack_sgrow (Obs, "[]"); \
245 } while (0)
246
247
248 /* Append the ending 0, finish Obs, and return the string. */
249
250 # define obstack_finish0(Obs) \
251 (obstack_1grow (Obs, '\0'), (char *) obstack_finish (Obs))
252
253
254 /*-----------------------------------------.
255 | Extensions to use for the output files. |
256 `-----------------------------------------*/
257
258 # ifndef OUTPUT_EXT
259 # define OUTPUT_EXT ".output"
260 # endif
261
262 # ifndef TAB_EXT
263 # define TAB_EXT ".tab"
264 # endif
265
266
267
268 /*---------------------.
269 | Free a linked list. |
270 `---------------------*/
271
272 # define LIST_FREE(Type, List) \
273 do { \
274 Type *_node, *_next; \
275 for (_node = List; _node; _node = _next) \
276 { \
277 _next = _node->next; \
278 free (_node); \
279 } \
280 } while (0)
281
282 #endif /* ! BISON_SYSTEM_H */