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