]> git.saurik.com Git - bison.git/blob - data/c.m4
maint: maintainer-release-check.
[bison.git] / data / c.m4
1 -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 ## ---------------- ##
21 ## Identification. ##
22 ## ---------------- ##
23
24 # b4_comment(TEXT)
25 # ----------------
26 m4_define([b4_comment], [/* m4_bpatsubst([$1], [
27 ], [
28 ]) */])
29
30 # b4_identification
31 # -----------------
32 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
33 # b4_pull_flag if they use the values of the %define variables api.pure or
34 # api.push-pull.
35 m4_define([b4_identification],
36 [[/* Identify Bison output. */
37 #define YYBISON 1
38
39 /* Bison version. */
40 #define YYBISON_VERSION "]b4_version["
41
42 /* Skeleton name. */
43 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
44
45 /* Pure parsers. */
46 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
47
48 /* Push parsers. */
49 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
50
51 /* Pull parsers. */
52 #define YYPULL ]b4_pull_flag])[
53
54 /* Using locations. */
55 #define YYLSP_NEEDED ]b4_locations_flag[
56 ]])
57
58
59 ## ---------------- ##
60 ## Default values. ##
61 ## ---------------- ##
62
63 # If the %union is not named, its name is YYSTYPE.
64 m4_define_default([b4_union_name], [YYSTYPE])
65
66 # If the %name-prefix is not given, it is yy.
67 m4_define_default([b4_prefix], [yy])
68
69 ## ------------------------ ##
70 ## Pure/impure interfaces. ##
71 ## ------------------------ ##
72
73 # b4_user_args
74 # ------------
75 m4_define([b4_user_args],
76 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
77
78
79 # b4_parse_param
80 # --------------
81 # If defined, b4_parse_param arrives double quoted, but below we prefer
82 # it to be single quoted.
83 m4_define([b4_parse_param],
84 b4_parse_param)
85
86
87 # b4_parse_param_for(DECL, FORMAL, BODY)
88 # ---------------------------------------
89 # Iterate over the user parameters, binding the declaration to DECL,
90 # the formal name to FORMAL, and evaluating the BODY.
91 m4_define([b4_parse_param_for],
92 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
93 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
94 m4_pushdef([$2], m4_shift($1_$2))dnl
95 $3[]dnl
96 m4_popdef([$2])dnl
97 m4_popdef([$1])dnl
98 ])])
99
100 # b4_parse_param_use
101 # ------------------
102 # `YYUSE' all the parse-params.
103 m4_define([b4_parse_param_use],
104 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
105 ])dnl
106 ])
107
108
109 ## ------------ ##
110 ## Data Types. ##
111 ## ------------ ##
112
113 # b4_int_type(MIN, MAX)
114 # ---------------------
115 # Return the smallest int type able to handle numbers ranging from
116 # MIN to MAX (included).
117 m4_define([b4_int_type],
118 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
119 b4_ints_in($@, [-128], [127]), [1], [signed char],
120
121 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
122 b4_ints_in($@, [-32768], [32767]), [1], [short int],
123
124 m4_eval([0 <= $1]), [1], [unsigned int],
125
126 [int])])
127
128
129 # b4_int_type_for(NAME)
130 # ---------------------
131 # Return the smallest int type able to handle numbers ranging from
132 # `NAME_min' to `NAME_max' (included).
133 m4_define([b4_int_type_for],
134 [b4_int_type($1_min, $1_max)])
135
136
137 # b4_table_value_equals(TABLE, VALUE, LITERAL)
138 # --------------------------------------------
139 # Without inducing a comparison warning from the compiler, check if the
140 # literal value LITERAL equals VALUE from table TABLE, which must have
141 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
142 # function that suppresses warnings about constant conditions.
143 m4_define([b4_table_value_equals],
144 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
145 || m4_indir([b4_]$1[_max]) < $3), [1],
146 [[YYID (0)]],
147 [[((]$2[) == (]$3[))]])])
148
149
150 ## ---------##
151 ## Values. ##
152 ## ---------##
153
154
155 # b4_null_define
156 # --------------
157 # Portability issues: define a YY_NULL appropriate for the current
158 # language (C, C++98, or C++11).
159 m4_define([b4_null_define],
160 [# ifndef YY_NULL
161 # if defined __cplusplus && 201103L <= __cplusplus
162 # define YY_NULL nullptr
163 # else
164 # define YY_NULL 0
165 # endif
166 # endif[]dnl
167 ])
168
169
170 # b4_null
171 # -------
172 # Return a null pointer constant.
173 m4_define([b4_null], [YY_NULL])
174
175
176
177 ## ------------------------- ##
178 ## Assigning token numbers. ##
179 ## ------------------------- ##
180
181 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
182 # -----------------------------------------
183 # Output the definition of this token as #define.
184 m4_define([b4_token_define],
185 [#define $1 $2
186 ])
187
188
189 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
190 # -------------------------------------------------------
191 # Output the definition of the tokens (if there are) as #defines.
192 m4_define([b4_token_defines],
193 [m4_if([$#$1], [1], [],
194 [/* Tokens. */
195 m4_map([b4_token_define], [$@])])
196 ])
197
198
199 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
200 # ---------------------------------------
201 # Output the definition of this token as an enum.
202 m4_define([b4_token_enum],
203 [$1 = $2])
204
205
206 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
207 # -----------------------------------------------------
208 # Output the definition of the tokens (if there are) as enums.
209 m4_define([b4_token_enums],
210 [m4_if([$#$1], [1], [],
211 [/* Tokens. */
212 #ifndef YYTOKENTYPE
213 # define YYTOKENTYPE
214 /* Put the tokens into the symbol table, so that GDB and other debuggers
215 know about them. */
216 enum yytokentype {
217 m4_map_sep([ b4_token_enum], [,
218 ],
219 [$@])
220 };
221 #endif
222 ])])
223
224
225 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
226 # -------------------------------------------------------------
227 # Output the definition of the tokens (if there are any) as enums and, if POSIX
228 # Yacc is enabled, as #defines.
229 m4_define([b4_token_enums_defines],
230 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
231 ])
232
233
234
235 ## --------------------------------------------- ##
236 ## Defining C functions in both K&R and ANSI-C. ##
237 ## --------------------------------------------- ##
238
239
240 # b4_modern_c
241 # -----------
242 # A predicate useful in #if to determine whether C is ancient or modern.
243 #
244 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
245 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
246 # reasons, but it defines __C99__FUNC__ so check that as well.
247 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
248 # Consider a C++ compiler to be modern if it defines __cplusplus.
249 #
250 m4_define([b4_c_modern],
251 [[(defined __STDC__ || defined __C99__FUNC__ \
252 || defined __cplusplus || defined _MSC_VER)]])
253
254 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
255 # ----------------------------------------------------------
256 # Declare the function NAME.
257 m4_define([b4_c_function_def],
258 [#if b4_c_modern
259 b4_c_ansi_function_def($@)
260 #else
261 $2
262 $1 (b4_c_knr_formal_names(m4_shift2($@)))
263 b4_c_knr_formal_decls(m4_shift2($@))
264 #endif[]dnl
265 ])
266
267
268 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
269 # ---------------------------------------------------------------
270 # Declare the function NAME in ANSI.
271 m4_define([b4_c_ansi_function_def],
272 [$2
273 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
274 ])
275
276
277 # b4_c_ansi_formals([DECL1, NAME1], ...)
278 # --------------------------------------
279 # Output the arguments ANSI-C definition.
280 m4_define([b4_c_ansi_formals],
281 [m4_if([$#], [0], [void],
282 [$#$1], [1], [void],
283 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
284
285 m4_define([b4_c_ansi_formal],
286 [$1])
287
288
289 # b4_c_knr_formal_names([DECL1, NAME1], ...)
290 # ------------------------------------------
291 # Output the argument names.
292 m4_define([b4_c_knr_formal_names],
293 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
294
295 m4_define([b4_c_knr_formal_name],
296 [$2])
297
298
299 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
300 # ------------------------------------------
301 # Output the K&R argument declarations.
302 m4_define([b4_c_knr_formal_decls],
303 [m4_map_sep([b4_c_knr_formal_decl],
304 [
305 ],
306 [$@])])
307
308 m4_define([b4_c_knr_formal_decl],
309 [ $1;])
310
311
312
313 ## ------------------------------------------------------------ ##
314 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
315 ## ------------------------------------------------------------ ##
316
317
318 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
319 # -----------------------------------------------------------
320 # Declare the function NAME.
321 m4_define([b4_c_function_decl],
322 [#if defined __STDC__ || defined __cplusplus
323 b4_c_ansi_function_decl($@)
324 #else
325 $2 $1 ();
326 #endif[]dnl
327 ])
328
329
330 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
331 # ----------------------------------------------------------------
332 # Declare the function NAME.
333 m4_define([b4_c_ansi_function_decl],
334 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
335 ])
336
337
338
339
340 ## --------------------- ##
341 ## Calling C functions. ##
342 ## --------------------- ##
343
344
345 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
346 # -----------------------------------------------------------
347 # Call the function NAME with arguments NAME1, NAME2 etc.
348 m4_define([b4_c_function_call],
349 [$1 (b4_c_args(m4_shift2($@)))[]dnl
350 ])
351
352
353 # b4_c_args([DECL1, NAME1], ...)
354 # ------------------------------
355 # Output the arguments NAME1, NAME2...
356 m4_define([b4_c_args],
357 [m4_map_sep([b4_c_arg], [, ], [$@])])
358
359 m4_define([b4_c_arg],
360 [$2])
361
362
363 ## ----------- ##
364 ## Synclines. ##
365 ## ----------- ##
366
367 # b4_sync_start(LINE, FILE)
368 # -----------------------
369 m4_define([b4_sync_start], [[#]line $1 $2])
370
371
372 ## -------------- ##
373 ## User actions. ##
374 ## -------------- ##
375
376 # b4_case(LABEL, STATEMENTS)
377 # --------------------------
378 m4_define([b4_case],
379 [ case $1:
380 $2
381 break;])
382
383 # b4_symbol_actions(FILENAME, LINENO,
384 # SYMBOL-TAG, SYMBOL-NUM,
385 # SYMBOL-ACTION, SYMBOL-TYPENAME)
386 # -------------------------------------------------
387 m4_define([b4_symbol_actions],
388 [m4_pushdef([b4_dollar_dollar],
389 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
390 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
391 case $4: /* $3 */
392 b4_syncline([$2], [$1])
393 $5;
394 b4_syncline([@oline@], [@ofile@])
395 break;
396 m4_popdef([b4_at_dollar])dnl
397 m4_popdef([b4_dollar_dollar])dnl
398 ])
399
400
401 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
402 # -------------------------------------------
403 # Generate the "yydestruct" function, which declaration is issued using
404 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
405 # or "b4_c_function_def" for K&R.
406 m4_define_default([b4_yydestruct_generate],
407 [[/*-----------------------------------------------.
408 | Release the memory associated to this symbol. |
409 `-----------------------------------------------*/
410
411 /*ARGSUSED*/
412 ]$1([yydestruct],
413 [static void],
414 [[const char *yymsg], [yymsg]],
415 [[int yytype], [yytype]],
416 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
417 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
418 m4_ifset([b4_parse_param], [, b4_parse_param]))[
419 {
420 YYUSE (yyvaluep);
421 ]b4_locations_if([ YYUSE (yylocationp);
422 ])dnl
423 b4_parse_param_use[]dnl
424 [
425 if (!yymsg)
426 yymsg = "Deleting";
427 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
428
429 switch (yytype)
430 {
431 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
432 default:
433 break;
434 }
435 }]dnl
436 ])
437
438
439 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
440 # ------------------------------------------------
441 # Generate the "yy_symbol_print" function, which declaration is issued using
442 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
443 # or "b4_c_function_def" for K&R.
444 m4_define_default([b4_yy_symbol_print_generate],
445 [[
446 /*--------------------------------.
447 | Print this symbol on YYOUTPUT. |
448 `--------------------------------*/
449
450 /*ARGSUSED*/
451 ]$1([yy_symbol_value_print],
452 [static void],
453 [[FILE *yyoutput], [yyoutput]],
454 [[int yytype], [yytype]],
455 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
456 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
457 m4_ifset([b4_parse_param], [, b4_parse_param]))[
458 {
459 FILE *yyo = yyoutput;
460 YYUSE (yyo);
461 if (!yyvaluep)
462 return;
463 ]b4_locations_if([ YYUSE (yylocationp);
464 ])dnl
465 b4_parse_param_use[]dnl
466 [# ifdef YYPRINT
467 if (yytype < YYNTOKENS)
468 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
469 # else
470 YYUSE (yyoutput);
471 # endif
472 switch (yytype)
473 {
474 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
475 [ default:
476 break;
477 }
478 }
479
480
481 /*--------------------------------.
482 | Print this symbol on YYOUTPUT. |
483 `--------------------------------*/
484
485 ]$1([yy_symbol_print],
486 [static void],
487 [[FILE *yyoutput], [yyoutput]],
488 [[int yytype], [yytype]],
489 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
490 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
491 m4_ifset([b4_parse_param], [, b4_parse_param]))[
492 {
493 if (yytype < YYNTOKENS)
494 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
495 else
496 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
497
498 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
499 YYFPRINTF (yyoutput, ": ");
500 ])dnl
501 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
502 b4_locations_if([, yylocationp])[]b4_user_args[);
503 YYFPRINTF (yyoutput, ")");
504 }]dnl
505 ])