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