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