]> git.saurik.com Git - bison.git/blame - data/c.m4
* data/c.m4 (b4_null, b4_case): Define.
[bison.git] / data / c.m4
CommitLineData
55b929ca 1m4_divert(-1) -*- Autoconf -*-
fb8135fa
AD
2
3# C M4 Macros for Bison.
e2a21b6f 4# Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
fb8135fa
AD
5
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10
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.
15
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
0fb669f9
PE
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19# 02110-1301 USA
fb8135fa
AD
20
21
2a8d363a
AD
22## ---------------- ##
23## Identification. ##
24## ---------------- ##
fb8135fa
AD
25
26# b4_copyright(TITLE, YEARS)
27# --------------------------
28m4_define([b4_copyright],
fa3f2a88 29[/* A Bison parser, made by GNU Bison b4_version. */
fb8135fa 30
6e93d810
PE
31/* $1
32
33m4_text_wrap([Copyright (C) $2 Free Software Foundation, Inc.], [ ])
fb8135fa
AD
34
35 This program is free software; you can redistribute it and/or modify
36 it under the terms of the GNU General Public License as published by
37 the Free Software Foundation; either version 2, or (at your option)
38 any later version.
39
40 This program is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
44
45 You should have received a copy of the GNU General Public License
46 along with this program; if not, write to the Free Software
0fb669f9 47 Foundation, Inc., 51 Franklin Street, Fifth Floor,
6e93d810
PE
48 Boston, MA 02110-1301, USA. */
49
50/* As a special exception, you may create a larger work that contains
51 part or all of the Bison parser skeleton and distribute that work
52 under terms of your choice, so long as that work isn't itself a
53 parser generator using the skeleton or a modified version thereof
54 as a parser skeleton. Alternatively, if you modify or redistribute
55 the parser skeleton itself, you may (at your option) remove this
56 special exception, which will cause the skeleton and the resulting
57 Bison output files to be licensed under the GNU General Public
58 License without this special exception.
59
60 This special exception was added by the Free Software Foundation in
61 version 2.2 of Bison. */])
fb8135fa
AD
62
63
2a8d363a
AD
64# b4_identification
65# -----------------
66m4_define([b4_identification],
67[/* Identify Bison output. */
68[#]define YYBISON 1
69
55f64b82
PE
70/* Bison version. */
71[#]define YYBISON_VERSION "b4_version"
72
2a8d363a 73/* Skeleton name. */
fa3f2a88 74[#]define YYSKELETON_NAME b4_skeleton
2a8d363a
AD
75
76/* Pure parsers. */
327afc7c 77[#]define YYPURE b4_pure_flag
2a8d363a
AD
78
79/* Using locations. */
80[#]define YYLSP_NEEDED b4_locations_flag
81])
82
83
7ec2d4cd
AD
84## ---------------- ##
85## Default values. ##
86## ---------------- ##
87
88m4_define_default([b4_epilogue], [])
89
1b818f33
AD
90# If the %union is not named, its name is YYSTYPE.
91m4_define_default([b4_union_name], [YYSTYPE])
7ec2d4cd 92
cd48d21d
AD
93# The initial column and line.
94m4_define_default([b4_location_initial_column], [1])
95m4_define_default([b4_location_initial_line], [1])
96
97
2a8d363a
AD
98## ------------------------ ##
99## Pure/impure interfaces. ##
100## ------------------------ ##
101
2a8d363a
AD
102# b4_user_args
103# ------------
104m4_define([b4_user_args],
105[m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
106
107
108# b4_parse_param
109# --------------
110# If defined, b4_parse_param arrives double quoted, but below we prefer
111# it to be single quoted.
112m4_define_default([b4_parse_param])
113m4_define([b4_parse_param],
114b4_parse_param))
115
8f7e3cf9 116
613d8952
AD
117# b4_parse_param_for(DECL, FORMAL, BODY)
118# ---------------------------------------
119# Iterate over the user parameters, binding the declaration to DECL,
120# the formal name to FORMAL, and evaluating the BODY.
121m4_define([b4_parse_param_for],
122[m4_foreach([$1_$2], m4_defn([b4_parse_param]),
123[m4_pushdef([$1], m4_fst($1_$2))dnl
124m4_pushdef([$2], m4_shift($1_$2))dnl
125$3[]dnl
126m4_popdef([$2])dnl
127m4_popdef([$1])dnl
128])])
129
130# b4_parse_param_use
131# ------------------
132# `YYUSE' all the parse-params.
613d8952
AD
133m4_define([b4_parse_param_use],
134[b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
135])dnl
136])
2a8d363a 137
8e1687ae 138
fb8135fa
AD
139## ------------ ##
140## Data Types. ##
141## ------------ ##
142
a762e609
AD
143# b4_ints_in(INT1, INT2, LOW, HIGH)
144# ---------------------------------
145# Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
146m4_define([b4_ints_in],
147[m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
148
149
150# b4_int_type(MIN, MAX)
151# ---------------------
152# Return the smallest int type able to handle numbers ranging from
153# MIN to MAX (included).
154m4_define([b4_int_type],
155[m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
f1886bb2 156 b4_ints_in($@, [-128], [127]), [1], [signed char],
a762e609 157
779e7ceb
PE
158 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
159 b4_ints_in($@, [-32768], [32767]), [1], [short int],
a762e609
AD
160
161 m4_eval([0 <= $1]), [1], [unsigned int],
162
02650b7f 163 [int])])
a762e609 164
f1886bb2 165
a762e609
AD
166# b4_int_type_for(NAME)
167# ---------------------
168# Return the smallest int type able to handle numbers ranging from
169# `NAME_min' to `NAME_max' (included).
170m4_define([b4_int_type_for],
171[b4_int_type($1_min, $1_max)])
fb8135fa
AD
172
173
8e1687ae
PE
174## ---------##
175## Values. ##
176## ---------##
177
178# b4_null
179---------
180# Return a null pointer constant. NULL infringes on the user name
181# space in C, so use 0 rather than NULL.
182m4_define([b4_null], [0])
183
184
0245f82d
AD
185## ------------------ ##
186## Decoding options. ##
187## ------------------ ##
188
327afc7c
AD
189# b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
190# -----------------------------------
191# Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
192m4_define([b4_flag_if],
193[m4_case(b4_$1_flag,
194 [0], [$3],
195 [1], [$2],
196 [m4_fatal([invalid $1 value: ]$1)])])
0245f82d
AD
197
198
327afc7c
AD
199# b4_define_flag_if(FLAG)
200# -----------------------
201# Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
202# value of the Boolean FLAG.
203m4_define([b4_define_flag_if],
204[_b4_define_flag_if($[1], $[2], [$1])])
205
206# _b4_define_flag_if($1, $2, FLAG)
207# --------------------------------
208# This macro works around the impossibility to define macros
209# inside macros, because issuing `[$1]' is not possible in M4 :(.
210# This sucks hard, GNU M4 should really provide M5 like $$1.
211m4_define([_b4_define_flag_if],
212[m4_if([$1$2], $[1]$[2], [],
213 [m4_fatal([$0: Invalid arguments: $@])])dnl
1b818f33 214m4_define([b4_$3_if],
327afc7c
AD
215 [b4_flag_if([$3], [$1], [$2])])])
216
217
218# b4_FLAG_if(IF-TRUE, IF-FALSE)
0245f82d 219# -----------------------------
327afc7c
AD
220# Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
221b4_define_flag_if([defines]) # Whether headers are requested.
a5d80ba5 222b4_define_flag_if([error_verbose]) # Whether error are verbose.
327afc7c
AD
223b4_define_flag_if([locations]) # Whether locations are tracked.
224b4_define_flag_if([pure]) # Whether the interface is pure.
b931235e 225b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
0245f82d
AD
226
227
228
fb8135fa
AD
229## ------------------------- ##
230## Assigning token numbers. ##
231## ------------------------- ##
232
233# b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
234# -----------------------------------------
235# Output the definition of this token as #define.
236m4_define([b4_token_define],
237[#define $1 $2
238])
239
240
cf147260
AD
241# b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
242# -------------------------------------------------------
243# Output the definition of the tokens (if there are) as #defines.
244m4_define([b4_token_defines],
245[m4_if([$@], [[]], [],
246[/* Tokens. */
247m4_map([b4_token_define], [$@])])
248])
249
250
fb8135fa
AD
251# b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
252# ---------------------------------------
253# Output the definition of this token as an enum.
254m4_define([b4_token_enum],
255[$1 = $2])
256
257
cf147260
AD
258# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
259# -----------------------------------------------------
260# Output the definition of the tokens (if there are) as enums.
261m4_define([b4_token_enums],
fb8135fa
AD
262[m4_if([$@], [[]], [],
263[/* Tokens. */
6b8c3254
PE
264#ifndef YYTOKENTYPE
265# define YYTOKENTYPE
fb8135fa
AD
266 /* Put the tokens into the symbol table, so that GDB and other debuggers
267 know about them. */
268 enum yytokentype {
269m4_map_sep([ b4_token_enum], [,
270],
02650b7f 271 [$@])
fb8135fa 272 };
095b9f05 273#endif
cf147260
AD
274])])
275
276
277# b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
278# -------------------------------------------------------------
b931235e
JD
279# Output the definition of the tokens (if there are any) as enums and, if POSIX
280# Yacc is enabled, as #defines.
cf147260 281m4_define([b4_token_enums_defines],
b931235e 282[b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
fb8135fa 283])
4a2a22f4
AD
284
285
ae7453f2 286
0245f82d
AD
287## --------------------------------------------- ##
288## Defining C functions in both K&R and ANSI-C. ##
289## --------------------------------------------- ##
4a2a22f4
AD
290
291
1b9c21fb
PE
292# b4_modern_c
293# -----------
294# A predicate useful in #if to determine whether C is ancient or modern.
295#
296# If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
297# as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
298# reasons, but it defines __C99__FUNC__ so check that as well.
299# Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
7a0db73e 300# Consider a C++ compiler to be modern if it defines __cplusplus.
1b9c21fb
PE
301#
302m4_define([b4_c_modern],
02650b7f
PE
303 [[(defined __STDC__ || defined __C99__FUNC__ \
304 || defined __cplusplus || defined _MSC_VER)]])
1b9c21fb 305
0245f82d
AD
306# b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
307# ----------------------------------------------------------
4a2a22f4 308# Declare the function NAME.
0245f82d 309m4_define([b4_c_function_def],
1b9c21fb 310[#if b4_c_modern
0245f82d 311b4_c_ansi_function_def($@)
4a2a22f4 312#else
0245f82d
AD
313$2
314$1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
315b4_c_knr_formal_decls(m4_shiftn(2, $@))
4a2a22f4
AD
316#endif[]dnl
317])
318
319
0245f82d
AD
320# b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
321# ---------------------------------------------------------------
322# Declare the function NAME in ANSI.
323m4_define([b4_c_ansi_function_def],
324[$2
325$1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
326])
327
328
329# b4_c_ansi_formals([DECL1, NAME1], ...)
330# --------------------------------------
4a2a22f4 331# Output the arguments ANSI-C definition.
0245f82d
AD
332m4_define([b4_c_ansi_formals],
333[m4_case([$@],
02650b7f
PE
334 [], [void],
335 [[]], [void],
336 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
4a2a22f4 337
0245f82d
AD
338m4_define([b4_c_ansi_formal],
339[$1])
4a2a22f4
AD
340
341
0245f82d
AD
342# b4_c_knr_formal_names([DECL1, NAME1], ...)
343# ------------------------------------------
4a2a22f4 344# Output the argument names.
0245f82d
AD
345m4_define([b4_c_knr_formal_names],
346[m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
4a2a22f4 347
0245f82d 348m4_define([b4_c_knr_formal_name],
4a2a22f4
AD
349[$2])
350
351
0245f82d
AD
352# b4_c_knr_formal_decls([DECL1, NAME1], ...)
353# ------------------------------------------
4a2a22f4 354# Output the K&R argument declarations.
0245f82d
AD
355m4_define([b4_c_knr_formal_decls],
356[m4_map_sep([b4_c_knr_formal_decl],
02650b7f 357 [
4a2a22f4 358],
02650b7f 359 [$@])])
4a2a22f4 360
0245f82d
AD
361m4_define([b4_c_knr_formal_decl],
362[ $1;])
21964f43
AD
363
364
365
0245f82d
AD
366## ------------------------------------------------------------ ##
367## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
368## ------------------------------------------------------------ ##
21964f43
AD
369
370
0245f82d
AD
371# b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
372# -----------------------------------------------------------
373# Declare the function NAME.
374m4_define([b4_c_function_decl],
02650b7f 375[#if defined __STDC__ || defined __cplusplus
0245f82d
AD
376b4_c_ansi_function_decl($@)
377#else
378$2 $1 ();
379#endif[]dnl
380])
21964f43
AD
381
382
0245f82d
AD
383# b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
384# ----------------------------------------------------------------
385# Declare the function NAME.
386m4_define([b4_c_ansi_function_decl],
387[$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
388])
389
390
391
392
393## --------------------- ##
394## Calling C functions. ##
395## --------------------- ##
396
397
398# b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
399# -----------------------------------------------------------
400# Call the function NAME with arguments NAME1, NAME2 etc.
401m4_define([b4_c_function_call],
402[$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
403])
404
405
406# b4_c_args([DECL1, NAME1], ...)
407# ------------------------------
408# Output the arguments NAME1, NAME2...
409m4_define([b4_c_args],
410[m4_map_sep([b4_c_arg], [, ], [$@])])
411
412m4_define([b4_c_arg],
413[$2])
437c2d80
AD
414
415
416## ----------- ##
417## Synclines. ##
418## ----------- ##
419
8ec0a172
AD
420# b4_basename(NAME)
421# -----------------
b8445a15 422# Similar to POSIX basename; the differences don't matter here.
8ec0a172
AD
423# Beware that NAME is not evaluated.
424m4_define([b4_basename],
b8445a15
PE
425[m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
426
8ec0a172 427
437c2d80
AD
428# b4_syncline(LINE, FILE)
429# -----------------------
430m4_define([b4_syncline],
b8445a15 431[b4_flag_if([synclines],
8ec0a172
AD
432[/* Line __line__ of b4_basename(m4_quote(__file__)). */
433[#]line $1 $2])])
b0400cc6
AD
434
435
8ec0a172
AD
436# b4_user_code(USER-CODE)
437# -----------------------
438# Emit code from the user, ending it with synclines.
439m4_define([b4_user_code],
440[$1
441b4_syncline([@oline@], [@ofile@])])
442
443
444# b4_define_user_code(MACRO)
445# --------------------------
446# From b4_MACRO, build b4_user_MACRO that includes the synclines.
447m4_define([b4_define_user_code],
448[m4_define([b4_user_$1],
449[b4_user_code([b4_$1])])])
450
451
452# b4_user_actions
453# b4_user_initial_action
454# b4_user_post_prologue
455# b4_user_start_header
456# b4_user_stype
457# ----------------------
458# Macros that issue user code, ending with synclines.
459b4_define_user_code([actions])
460b4_define_user_code([initial_action])
461b4_define_user_code([post_prologue])
462b4_define_user_code([start_header])
463b4_define_user_code([stype])
464
613d8952
AD
465
466## -------------- ##
467## User actions. ##
468## -------------- ##
469
8e1687ae
PE
470# b4_case(LABEL, STATEMENTS)
471# --------------------------
472m4_define([b4_case],
473[ case $1:
474$2
475 break;])
476
b0400cc6
AD
477# b4_symbol_actions(FILENAME, LINENO,
478# SYMBOL-TAG, SYMBOL-NUM,
479# SYMBOL-ACTION, SYMBOL-TYPENAME)
480# -------------------------------------------------
481m4_define([b4_symbol_actions],
60c82948
PE
482[m4_pushdef([b4_dollar_dollar],
483 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
7bd6c77e 484m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
b0400cc6
AD
485 case $4: /* $3 */
486b4_syncline([$2], [$1])
02650b7f 487 $5;
b0400cc6 488b4_syncline([@oline@], [@ofile@])
02650b7f 489 break;
b0400cc6
AD
490m4_popdef([b4_at_dollar])dnl
491m4_popdef([b4_dollar_dollar])dnl
492])
493
494
14740648
PE
495# b4_yydestruct_generate(FUNCTION-DECLARATOR)
496# -------------------------------------------
b0400cc6 497# Generate the "yydestruct" function, which declaration is issued using
14740648 498# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
b0400cc6 499# or "b4_c_function_def" for K&R.
cf147260 500m4_define_default([b4_yydestruct_generate],
b0400cc6
AD
501[[/*-----------------------------------------------.
502| Release the memory associated to this symbol. |
503`-----------------------------------------------*/
504
12ce2df6 505/*ARGSUSED*/
b0400cc6
AD
506]$1([yydestruct],
507 [static void],
a0e68930 508 [[const char *yymsg], [yymsg]],
7bd6c77e 509 [[int yytype], [yytype]],
4b367315 510 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
327afc7c 511b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
4b367315 512m4_ifset([b4_parse_param], [, b4_parse_param]))[
b0400cc6 513{
2a4647a3 514 YYUSE (yyvaluep);
327afc7c 515]b4_locations_if([ YYUSE (yylocationp);
613d8952
AD
516])dnl
517b4_parse_param_use[]dnl
518[
a0e68930
AD
519 if (!yymsg)
520 yymsg = "Deleting";
521 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
522
b0400cc6
AD
523 switch (yytype)
524 {
525]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
526 default:
02650b7f 527 break;
b0400cc6
AD
528 }
529}]dnl
530])
7bd6c77e
AD
531
532
a0af42fc
AD
533# b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
534# ------------------------------------------------
535# Generate the "yy_symbol_print" function, which declaration is issued using
14740648 536# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
7bd6c77e 537# or "b4_c_function_def" for K&R.
a0af42fc
AD
538m4_define_default([b4_yy_symbol_print_generate],
539[[
540/*--------------------------------.
14740648
PE
541| Print this symbol on YYOUTPUT. |
542`--------------------------------*/
7bd6c77e 543
12ce2df6 544/*ARGSUSED*/
a0af42fc 545]$1([yy_symbol_value_print],
7bd6c77e 546 [static void],
02650b7f
PE
547 [[FILE *yyoutput], [yyoutput]],
548 [[int yytype], [yytype]],
50cce58e
PE
549 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
550b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
4b367315 551m4_ifset([b4_parse_param], [, b4_parse_param]))[
7bd6c77e 552{
05449a2c
JD
553 if (!yyvaluep)
554 return;
327afc7c 555]b4_locations_if([ YYUSE (yylocationp);
7bd6c77e 556])dnl
613d8952 557b4_parse_param_use[]dnl
a0af42fc 558[# ifdef YYPRINT
619404e3
AD
559 if (yytype < YYNTOKENS)
560 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
c66dfadd
PE
561# else
562 YYUSE (yyoutput);
619404e3 563# endif
7bd6c77e
AD
564 switch (yytype)
565 {
3fc16193
AD
566]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
567[ default:
02650b7f 568 break;
7bd6c77e 569 }
a0af42fc
AD
570}
571
572
573/*--------------------------------.
574| Print this symbol on YYOUTPUT. |
575`--------------------------------*/
576
577]$1([yy_symbol_print],
578 [static void],
02650b7f
PE
579 [[FILE *yyoutput], [yyoutput]],
580 [[int yytype], [yytype]],
50cce58e
PE
581 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
582b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
a0af42fc
AD
583m4_ifset([b4_parse_param], [, b4_parse_param]))[
584{
585 if (yytype < YYNTOKENS)
586 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
587 else
588 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
589
327afc7c 590]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
a0af42fc
AD
591 YYFPRINTF (yyoutput, ": ");
592])dnl
593[ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
327afc7c 594b4_locations_if([, yylocationp])[]b4_user_args[);
2f4f028d 595 YYFPRINTF (yyoutput, ")");
4b367315
AD
596}]dnl
597])