]> git.saurik.com Git - bison.git/blame - data/c.m4
Use the new code_props interface for rule actions.
[bison.git] / data / c.m4
CommitLineData
55b929ca 1m4_divert(-1) -*- Autoconf -*-
fb8135fa
AD
2
3# C M4 Macros for Bison.
e2a21b6f 4# Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
fb8135fa
AD
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 2 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, write to the Free Software
0fb669f9
PE
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19# 02110-1301 USA
fb8135fa 20
2a8d363a
AD
21## ---------------- ##
22## Identification. ##
23## ---------------- ##
fb8135fa 24
90b9908d
PB
25# b4_comment(TEXT)
26# ----------------
27m4_define([b4_comment], [/* m4_bpatsubst([$1], [
28], [
29 ]) */])
fb8135fa 30
2a8d363a
AD
31# b4_identification
32# -----------------
33m4_define([b4_identification],
34[/* Identify Bison output. */
35[#]define YYBISON 1
36
55f64b82
PE
37/* Bison version. */
38[#]define YYBISON_VERSION "b4_version"
39
2a8d363a 40/* Skeleton name. */
fa3f2a88 41[#]define YYSKELETON_NAME b4_skeleton
2a8d363a
AD
42
43/* Pure parsers. */
327afc7c 44[#]define YYPURE b4_pure_flag
2a8d363a 45
31c10e38
PE
46/* Push parsers. */
47[#]define YYPUSH b4_push_flag
48
7172e23e
JD
49/* Pull parsers. */
50[#]define YYPULL b4_pull_flag
51
2a8d363a
AD
52/* Using locations. */
53[#]define YYLSP_NEEDED b4_locations_flag
54])
55
56
7ec2d4cd
AD
57## ---------------- ##
58## Default values. ##
59## ---------------- ##
60
1b818f33
AD
61# If the %union is not named, its name is YYSTYPE.
62m4_define_default([b4_union_name], [YYSTYPE])
7ec2d4cd 63
90b9908d
PB
64# If the %name-prefix is not given, it is yy.
65m4_define_default([b4_prefix], [yy])
cd48d21d 66
2a8d363a
AD
67## ------------------------ ##
68## Pure/impure interfaces. ##
69## ------------------------ ##
70
2a8d363a
AD
71# b4_user_args
72# ------------
73m4_define([b4_user_args],
74[m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
75
76
77# b4_parse_param
78# --------------
79# If defined, b4_parse_param arrives double quoted, but below we prefer
80# it to be single quoted.
2a8d363a
AD
81m4_define([b4_parse_param],
82b4_parse_param))
83
8f7e3cf9 84
613d8952
AD
85# b4_parse_param_for(DECL, FORMAL, BODY)
86# ---------------------------------------
87# Iterate over the user parameters, binding the declaration to DECL,
88# the formal name to FORMAL, and evaluating the BODY.
89m4_define([b4_parse_param_for],
90[m4_foreach([$1_$2], m4_defn([b4_parse_param]),
91[m4_pushdef([$1], m4_fst($1_$2))dnl
92m4_pushdef([$2], m4_shift($1_$2))dnl
93$3[]dnl
94m4_popdef([$2])dnl
95m4_popdef([$1])dnl
96])])
97
98# b4_parse_param_use
99# ------------------
100# `YYUSE' all the parse-params.
613d8952
AD
101m4_define([b4_parse_param_use],
102[b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
103])dnl
104])
2a8d363a 105
8e1687ae 106
fb8135fa
AD
107## ------------ ##
108## Data Types. ##
109## ------------ ##
110
a762e609
AD
111# b4_int_type(MIN, MAX)
112# ---------------------
113# Return the smallest int type able to handle numbers ranging from
114# MIN to MAX (included).
115m4_define([b4_int_type],
116[m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
f1886bb2 117 b4_ints_in($@, [-128], [127]), [1], [signed char],
a762e609 118
779e7ceb
PE
119 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
120 b4_ints_in($@, [-32768], [32767]), [1], [short int],
a762e609
AD
121
122 m4_eval([0 <= $1]), [1], [unsigned int],
123
02650b7f 124 [int])])
a762e609 125
f1886bb2 126
a762e609
AD
127# b4_int_type_for(NAME)
128# ---------------------
129# Return the smallest int type able to handle numbers ranging from
130# `NAME_min' to `NAME_max' (included).
131m4_define([b4_int_type_for],
132[b4_int_type($1_min, $1_max)])
fb8135fa 133
8e1687ae
PE
134## ---------##
135## Values. ##
136## ---------##
137
138# b4_null
139---------
140# Return a null pointer constant. NULL infringes on the user name
141# space in C, so use 0 rather than NULL.
142m4_define([b4_null], [0])
143
144
fb8135fa
AD
145## ------------------------- ##
146## Assigning token numbers. ##
147## ------------------------- ##
148
149# b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
150# -----------------------------------------
151# Output the definition of this token as #define.
152m4_define([b4_token_define],
153[#define $1 $2
154])
155
156
cf147260
AD
157# b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
158# -------------------------------------------------------
159# Output the definition of the tokens (if there are) as #defines.
160m4_define([b4_token_defines],
161[m4_if([$@], [[]], [],
162[/* Tokens. */
163m4_map([b4_token_define], [$@])])
164])
165
166
fb8135fa
AD
167# b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
168# ---------------------------------------
169# Output the definition of this token as an enum.
170m4_define([b4_token_enum],
171[$1 = $2])
172
173
cf147260
AD
174# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
175# -----------------------------------------------------
176# Output the definition of the tokens (if there are) as enums.
177m4_define([b4_token_enums],
fb8135fa
AD
178[m4_if([$@], [[]], [],
179[/* Tokens. */
6b8c3254
PE
180#ifndef YYTOKENTYPE
181# define YYTOKENTYPE
fb8135fa
AD
182 /* Put the tokens into the symbol table, so that GDB and other debuggers
183 know about them. */
184 enum yytokentype {
185m4_map_sep([ b4_token_enum], [,
186],
02650b7f 187 [$@])
fb8135fa 188 };
095b9f05 189#endif
cf147260
AD
190])])
191
192
193# b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
194# -------------------------------------------------------------
b931235e
JD
195# Output the definition of the tokens (if there are any) as enums and, if POSIX
196# Yacc is enabled, as #defines.
cf147260 197m4_define([b4_token_enums_defines],
b931235e 198[b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
fb8135fa 199])
4a2a22f4
AD
200
201
ae7453f2 202
0245f82d
AD
203## --------------------------------------------- ##
204## Defining C functions in both K&R and ANSI-C. ##
205## --------------------------------------------- ##
4a2a22f4
AD
206
207
1b9c21fb
PE
208# b4_modern_c
209# -----------
210# A predicate useful in #if to determine whether C is ancient or modern.
211#
212# If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
213# as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
214# reasons, but it defines __C99__FUNC__ so check that as well.
215# Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
7a0db73e 216# Consider a C++ compiler to be modern if it defines __cplusplus.
1b9c21fb
PE
217#
218m4_define([b4_c_modern],
02650b7f
PE
219 [[(defined __STDC__ || defined __C99__FUNC__ \
220 || defined __cplusplus || defined _MSC_VER)]])
1b9c21fb 221
0245f82d
AD
222# b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
223# ----------------------------------------------------------
4a2a22f4 224# Declare the function NAME.
0245f82d 225m4_define([b4_c_function_def],
1b9c21fb 226[#if b4_c_modern
0245f82d 227b4_c_ansi_function_def($@)
4a2a22f4 228#else
0245f82d
AD
229$2
230$1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
231b4_c_knr_formal_decls(m4_shiftn(2, $@))
4a2a22f4
AD
232#endif[]dnl
233])
234
235
0245f82d
AD
236# b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
237# ---------------------------------------------------------------
238# Declare the function NAME in ANSI.
239m4_define([b4_c_ansi_function_def],
240[$2
241$1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
242])
243
244
245# b4_c_ansi_formals([DECL1, NAME1], ...)
246# --------------------------------------
4a2a22f4 247# Output the arguments ANSI-C definition.
0245f82d
AD
248m4_define([b4_c_ansi_formals],
249[m4_case([$@],
02650b7f
PE
250 [], [void],
251 [[]], [void],
252 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
4a2a22f4 253
0245f82d
AD
254m4_define([b4_c_ansi_formal],
255[$1])
4a2a22f4
AD
256
257
0245f82d
AD
258# b4_c_knr_formal_names([DECL1, NAME1], ...)
259# ------------------------------------------
4a2a22f4 260# Output the argument names.
0245f82d
AD
261m4_define([b4_c_knr_formal_names],
262[m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
4a2a22f4 263
0245f82d 264m4_define([b4_c_knr_formal_name],
4a2a22f4
AD
265[$2])
266
267
0245f82d
AD
268# b4_c_knr_formal_decls([DECL1, NAME1], ...)
269# ------------------------------------------
4a2a22f4 270# Output the K&R argument declarations.
0245f82d
AD
271m4_define([b4_c_knr_formal_decls],
272[m4_map_sep([b4_c_knr_formal_decl],
02650b7f 273 [
4a2a22f4 274],
02650b7f 275 [$@])])
4a2a22f4 276
0245f82d
AD
277m4_define([b4_c_knr_formal_decl],
278[ $1;])
21964f43
AD
279
280
281
0245f82d
AD
282## ------------------------------------------------------------ ##
283## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
284## ------------------------------------------------------------ ##
21964f43
AD
285
286
0245f82d
AD
287# b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
288# -----------------------------------------------------------
289# Declare the function NAME.
290m4_define([b4_c_function_decl],
02650b7f 291[#if defined __STDC__ || defined __cplusplus
0245f82d
AD
292b4_c_ansi_function_decl($@)
293#else
294$2 $1 ();
295#endif[]dnl
296])
21964f43
AD
297
298
0245f82d
AD
299# b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
300# ----------------------------------------------------------------
301# Declare the function NAME.
302m4_define([b4_c_ansi_function_decl],
303[$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
304])
305
306
307
308
309## --------------------- ##
310## Calling C functions. ##
311## --------------------- ##
312
313
314# b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
315# -----------------------------------------------------------
316# Call the function NAME with arguments NAME1, NAME2 etc.
317m4_define([b4_c_function_call],
318[$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
319])
320
321
322# b4_c_args([DECL1, NAME1], ...)
323# ------------------------------
324# Output the arguments NAME1, NAME2...
325m4_define([b4_c_args],
326[m4_map_sep([b4_c_arg], [, ], [$@])])
327
328m4_define([b4_c_arg],
329[$2])
437c2d80
AD
330
331
332## ----------- ##
333## Synclines. ##
334## ----------- ##
335
90b9908d 336# b4_sync_start(LINE, FILE)
8ec0a172 337# -----------------------
90b9908d 338m4_define([b4_sync_start], [[#]line $1 $2])
8ec0a172 339
613d8952
AD
340
341## -------------- ##
342## User actions. ##
343## -------------- ##
344
8e1687ae
PE
345# b4_case(LABEL, STATEMENTS)
346# --------------------------
347m4_define([b4_case],
348[ case $1:
349$2
350 break;])
351
b0400cc6
AD
352# b4_symbol_actions(FILENAME, LINENO,
353# SYMBOL-TAG, SYMBOL-NUM,
354# SYMBOL-ACTION, SYMBOL-TYPENAME)
355# -------------------------------------------------
356m4_define([b4_symbol_actions],
60c82948
PE
357[m4_pushdef([b4_dollar_dollar],
358 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
7bd6c77e 359m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
b0400cc6
AD
360 case $4: /* $3 */
361b4_syncline([$2], [$1])
02650b7f 362 $5;
b0400cc6 363b4_syncline([@oline@], [@ofile@])
02650b7f 364 break;
b0400cc6
AD
365m4_popdef([b4_at_dollar])dnl
366m4_popdef([b4_dollar_dollar])dnl
367])
368
369
14740648
PE
370# b4_yydestruct_generate(FUNCTION-DECLARATOR)
371# -------------------------------------------
b0400cc6 372# Generate the "yydestruct" function, which declaration is issued using
14740648 373# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
b0400cc6 374# or "b4_c_function_def" for K&R.
cf147260 375m4_define_default([b4_yydestruct_generate],
b0400cc6
AD
376[[/*-----------------------------------------------.
377| Release the memory associated to this symbol. |
378`-----------------------------------------------*/
379
12ce2df6 380/*ARGSUSED*/
b0400cc6
AD
381]$1([yydestruct],
382 [static void],
a0e68930 383 [[const char *yymsg], [yymsg]],
7bd6c77e 384 [[int yytype], [yytype]],
4b367315 385 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
327afc7c 386b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
4b367315 387m4_ifset([b4_parse_param], [, b4_parse_param]))[
b0400cc6 388{
2a4647a3 389 YYUSE (yyvaluep);
327afc7c 390]b4_locations_if([ YYUSE (yylocationp);
613d8952
AD
391])dnl
392b4_parse_param_use[]dnl
393[
a0e68930
AD
394 if (!yymsg)
395 yymsg = "Deleting";
396 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
397
b0400cc6
AD
398 switch (yytype)
399 {
400]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
401 default:
02650b7f 402 break;
b0400cc6
AD
403 }
404}]dnl
405])
7bd6c77e
AD
406
407
a0af42fc
AD
408# b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
409# ------------------------------------------------
410# Generate the "yy_symbol_print" function, which declaration is issued using
14740648 411# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
7bd6c77e 412# or "b4_c_function_def" for K&R.
a0af42fc
AD
413m4_define_default([b4_yy_symbol_print_generate],
414[[
415/*--------------------------------.
14740648
PE
416| Print this symbol on YYOUTPUT. |
417`--------------------------------*/
7bd6c77e 418
12ce2df6 419/*ARGSUSED*/
a0af42fc 420]$1([yy_symbol_value_print],
7bd6c77e 421 [static void],
02650b7f
PE
422 [[FILE *yyoutput], [yyoutput]],
423 [[int yytype], [yytype]],
50cce58e
PE
424 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
425b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
4b367315 426m4_ifset([b4_parse_param], [, b4_parse_param]))[
7bd6c77e 427{
05449a2c
JD
428 if (!yyvaluep)
429 return;
327afc7c 430]b4_locations_if([ YYUSE (yylocationp);
7bd6c77e 431])dnl
613d8952 432b4_parse_param_use[]dnl
a0af42fc 433[# ifdef YYPRINT
619404e3
AD
434 if (yytype < YYNTOKENS)
435 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
c66dfadd
PE
436# else
437 YYUSE (yyoutput);
619404e3 438# endif
7bd6c77e
AD
439 switch (yytype)
440 {
3fc16193
AD
441]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
442[ default:
02650b7f 443 break;
7bd6c77e 444 }
a0af42fc
AD
445}
446
447
448/*--------------------------------.
449| Print this symbol on YYOUTPUT. |
450`--------------------------------*/
451
452]$1([yy_symbol_print],
453 [static void],
02650b7f
PE
454 [[FILE *yyoutput], [yyoutput]],
455 [[int yytype], [yytype]],
50cce58e
PE
456 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
457b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
a0af42fc
AD
458m4_ifset([b4_parse_param], [, b4_parse_param]))[
459{
460 if (yytype < YYNTOKENS)
461 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
462 else
463 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
464
327afc7c 465]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
a0af42fc
AD
466 YYFPRINTF (yyoutput, ": ");
467])dnl
468[ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
327afc7c 469b4_locations_if([, yylocationp])[]b4_user_args[);
2f4f028d 470 YYFPRINTF (yyoutput, ")");
4b367315
AD
471}]dnl
472])