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