]> git.saurik.com Git - bison.git/blame_incremental - data/c.m4
Do not use date ranges in copyright notices.
[bison.git] / data / c.m4
... / ...
CommitLineData
1 -*- Autoconf -*-
2
3# C M4 Macros for Bison.
4
5# Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
6# Software Foundation, Inc.
7
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
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.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21## ---------------- ##
22## Identification. ##
23## ---------------- ##
24
25# b4_comment(TEXT)
26# ----------------
27m4_define([b4_comment], [/* m4_bpatsubst([$1], [
28], [
29 ]) */])
30
31# b4_identification
32# -----------------
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.
36m4_define([b4_identification],
37[[/* Identify Bison output. */
38#define YYBISON 1
39
40/* Bison version. */
41#define YYBISON_VERSION "]b4_version["
42
43/* Skeleton name. */
44#define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
45
46/* Pure parsers. */
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])[
54
55/* Using locations. */
56#define YYLSP_NEEDED ]b4_locations_flag[
57]])
58
59
60## ---------------- ##
61## Default values. ##
62## ---------------- ##
63
64# If the %union is not named, its name is YYSTYPE.
65m4_define_default([b4_union_name], [YYSTYPE])
66
67# If the %name-prefix is not given, it is yy.
68m4_define_default([b4_prefix], [yy])
69
70## ------------------------ ##
71## Pure/impure interfaces. ##
72## ------------------------ ##
73
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.
84m4_define([b4_parse_param],
85b4_parse_param)
86
87
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]),
94[m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
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.
104m4_define([b4_parse_param_use],
105[b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
106])dnl
107])
108
109
110## ------------ ##
111## Data Types. ##
112## ------------ ##
113
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],
120 b4_ints_in($@, [-128], [127]), [1], [signed char],
121
122 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
123 b4_ints_in($@, [-32768], [32767]), [1], [short int],
124
125 m4_eval([0 <= $1]), [1], [unsigned int],
126
127 [int])])
128
129
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)])
136
137
138
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
150
151
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
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],
168[m4_if([$#$1], [1], [],
169[/* Tokens. */
170m4_map([b4_token_define], [$@])])
171])
172
173
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
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],
185[m4_if([$#$1], [1], [],
186[/* Tokens. */
187#ifndef YYTOKENTYPE
188# define YYTOKENTYPE
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],
194 [$@])
195 };
196#endif
197])])
198
199
200# b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
201# -------------------------------------------------------------
202# Output the definition of the tokens (if there are any) as enums and, if POSIX
203# Yacc is enabled, as #defines.
204m4_define([b4_token_enums_defines],
205[b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
206])
207
208
209
210## --------------------------------------------- ##
211## Defining C functions in both K&R and ANSI-C. ##
212## --------------------------------------------- ##
213
214
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.
223# Consider a C++ compiler to be modern if it defines __cplusplus.
224#
225m4_define([b4_c_modern],
226 [[(defined __STDC__ || defined __C99__FUNC__ \
227 || defined __cplusplus || defined _MSC_VER)]])
228
229# b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
230# ----------------------------------------------------------
231# Declare the function NAME.
232m4_define([b4_c_function_def],
233[#if b4_c_modern
234b4_c_ansi_function_def($@)
235#else
236$2
237$1 (b4_c_knr_formal_names(m4_shift2($@)))
238b4_c_knr_formal_decls(m4_shift2($@))
239#endif[]dnl
240])
241
242
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
248$1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
249])
250
251
252# b4_c_ansi_formals([DECL1, NAME1], ...)
253# --------------------------------------
254# Output the arguments ANSI-C definition.
255m4_define([b4_c_ansi_formals],
256[m4_if([$#], [0], [void],
257 [$#$1], [1], [void],
258 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
259
260m4_define([b4_c_ansi_formal],
261[$1])
262
263
264# b4_c_knr_formal_names([DECL1, NAME1], ...)
265# ------------------------------------------
266# Output the argument names.
267m4_define([b4_c_knr_formal_names],
268[m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
269
270m4_define([b4_c_knr_formal_name],
271[$2])
272
273
274# b4_c_knr_formal_decls([DECL1, NAME1], ...)
275# ------------------------------------------
276# Output the K&R argument declarations.
277m4_define([b4_c_knr_formal_decls],
278[m4_map_sep([b4_c_knr_formal_decl],
279 [
280],
281 [$@])])
282
283m4_define([b4_c_knr_formal_decl],
284[ $1;])
285
286
287
288## ------------------------------------------------------------ ##
289## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
290## ------------------------------------------------------------ ##
291
292
293# b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
294# -----------------------------------------------------------
295# Declare the function NAME.
296m4_define([b4_c_function_decl],
297[#if defined __STDC__ || defined __cplusplus
298b4_c_ansi_function_decl($@)
299#else
300$2 $1 ();
301#endif[]dnl
302])
303
304
305# b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
306# ----------------------------------------------------------------
307# Declare the function NAME.
308m4_define([b4_c_ansi_function_decl],
309[$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
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],
324[$1 (b4_c_args(m4_shift2($@)))[]dnl
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])
336
337
338## ----------- ##
339## Synclines. ##
340## ----------- ##
341
342# b4_sync_start(LINE, FILE)
343# -----------------------
344m4_define([b4_sync_start], [[#]line $1 $2])
345
346
347## -------------- ##
348## User actions. ##
349## -------------- ##
350
351# b4_case(LABEL, STATEMENTS)
352# --------------------------
353m4_define([b4_case],
354[ case $1:
355$2
356 break;])
357
358# b4_symbol_actions(FILENAME, LINENO,
359# SYMBOL-TAG, SYMBOL-NUM,
360# SYMBOL-ACTION, SYMBOL-TYPENAME)
361# -------------------------------------------------
362m4_define([b4_symbol_actions],
363[m4_pushdef([b4_dollar_dollar],
364 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
365m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
366 case $4: /* $3 */
367b4_syncline([$2], [$1])
368 $5;
369b4_syncline([@oline@], [@ofile@])
370 break;
371m4_popdef([b4_at_dollar])dnl
372m4_popdef([b4_dollar_dollar])dnl
373])
374
375
376# b4_yydestruct_generate(FUNCTION-DECLARATOR)
377# -------------------------------------------
378# Generate the "yydestruct" function, which declaration is issued using
379# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
380# or "b4_c_function_def" for K&R.
381m4_define_default([b4_yydestruct_generate],
382[[/*-----------------------------------------------.
383| Release the memory associated to this symbol. |
384`-----------------------------------------------*/
385
386/*ARGSUSED*/
387]$1([yydestruct],
388 [static void],
389 [[const char *yymsg], [yymsg]],
390 [[int yytype], [yytype]],
391 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
392b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
393m4_ifset([b4_parse_param], [, b4_parse_param]))[
394{
395 YYUSE (yyvaluep);
396]b4_locations_if([ YYUSE (yylocationp);
397])dnl
398b4_parse_param_use[]dnl
399[
400 if (!yymsg)
401 yymsg = "Deleting";
402 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
403
404 switch (yytype)
405 {
406]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
407 default:
408 break;
409 }
410}]dnl
411])
412
413
414# b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
415# ------------------------------------------------
416# Generate the "yy_symbol_print" function, which declaration is issued using
417# FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
418# or "b4_c_function_def" for K&R.
419m4_define_default([b4_yy_symbol_print_generate],
420[[
421/*--------------------------------.
422| Print this symbol on YYOUTPUT. |
423`--------------------------------*/
424
425/*ARGSUSED*/
426]$1([yy_symbol_value_print],
427 [static void],
428 [[FILE *yyoutput], [yyoutput]],
429 [[int yytype], [yytype]],
430 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
431b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
432m4_ifset([b4_parse_param], [, b4_parse_param]))[
433{
434 if (!yyvaluep)
435 return;
436]b4_locations_if([ YYUSE (yylocationp);
437])dnl
438b4_parse_param_use[]dnl
439[# ifdef YYPRINT
440 if (yytype < YYNTOKENS)
441 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
442# else
443 YYUSE (yyoutput);
444# endif
445 switch (yytype)
446 {
447]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
448[ default:
449 break;
450 }
451}
452
453
454/*--------------------------------.
455| Print this symbol on YYOUTPUT. |
456`--------------------------------*/
457
458]$1([yy_symbol_print],
459 [static void],
460 [[FILE *yyoutput], [yyoutput]],
461 [[int yytype], [yytype]],
462 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
463b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
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
471]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
472 YYFPRINTF (yyoutput, ": ");
473])dnl
474[ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
475b4_locations_if([, yylocationp])[]b4_user_args[);
476 YYFPRINTF (yyoutput, ")");
477}]dnl
478])