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