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