]> git.saurik.com Git - bison.git/blame - data/c.m4
* data/yacc.c (m4_int_type): New.
[bison.git] / data / c.m4
CommitLineData
55b929ca 1m4_divert(-1) -*- Autoconf -*-
fb8135fa
AD
2
3# C M4 Macros for Bison.
4# Copyright (C) 2002 Free Software Foundation, Inc.
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
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19# 02111-1307 USA
20
21
22## ----------- ##
21964f43 23## Copyright. ##
fb8135fa
AD
24## ----------- ##
25
26# b4_copyright(TITLE, YEARS)
27# --------------------------
28m4_define([b4_copyright],
29[/* A Bison parser, made from b4_filename, by GNU bison b4_version. */
30
31/* $1,
c76e14da 32 Copyright (C) $2 Free Software Foundation, Inc.
fb8135fa
AD
33
34 This program is free software; you can redistribute it and/or modify
35 it under the terms of the GNU General Public License as published by
36 the Free Software Foundation; either version 2, or (at your option)
37 any later version.
38
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU General Public License for more details.
43
44 You should have received a copy of the GNU General Public License
45 along with this program; if not, write to the Free Software
46 Foundation, Inc., 59 Temple Place - Suite 330,
ad66f664 47 Boston, MA 02111-1307, USA. */])
fb8135fa
AD
48
49
50## ------------ ##
51## Data Types. ##
52## ------------ ##
53
fb8135fa 54
a762e609
AD
55# b4_ints_in(INT1, INT2, LOW, HIGH)
56# ---------------------------------
57# Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
58m4_define([b4_ints_in],
59[m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
60
61
62# b4_int_type(MIN, MAX)
63# ---------------------
64# Return the smallest int type able to handle numbers ranging from
65# MIN to MAX (included).
66m4_define([b4_int_type],
67[m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
f1886bb2 68 b4_ints_in($@, [-128], [127]), [1], [signed char],
a762e609
AD
69
70 b4_ints_in($@, [0], [65535]), [1], [unsigned short],
71 b4_ints_in($@, [-32768], [32767]), [1], [short],
72
73 m4_eval([0 <= $1]), [1], [unsigned int],
74
75 [int])])
76
f1886bb2 77
a762e609
AD
78# b4_int_type_for(NAME)
79# ---------------------
80# Return the smallest int type able to handle numbers ranging from
81# `NAME_min' to `NAME_max' (included).
82m4_define([b4_int_type_for],
83[b4_int_type($1_min, $1_max)])
fb8135fa
AD
84
85
0245f82d
AD
86## ------------------ ##
87## Decoding options. ##
88## ------------------ ##
89
90
91# b4_location_if(IF-TRUE, IF-FALSE)
92# ---------------------------------
93# Expand IF-TRUE, if locations are used, IF-FALSE otherwise.
94m4_define([b4_location_if],
95[m4_if(b4_locations_flag, [1],
96 [$1],
97 [$2])])
98
99
100# b4_pure_if(IF-TRUE, IF-FALSE)
101# -----------------------------
102# Expand IF-TRUE, if %pure-parser, IF-FALSE otherwise.
103m4_define([b4_pure_if],
104[m4_if(b4_pure, [1],
105 [$1],
106 [$2])])
107
108
109
fb8135fa
AD
110## ------------------------- ##
111## Assigning token numbers. ##
112## ------------------------- ##
113
114# b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
115# -----------------------------------------
116# Output the definition of this token as #define.
117m4_define([b4_token_define],
118[#define $1 $2
119])
120
121
122# b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
123# ---------------------------------------
124# Output the definition of this token as an enum.
125m4_define([b4_token_enum],
126[$1 = $2])
127
128
129# b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
130# -------------------------------------------------------
131# Output the definition of the tokens (if there are) as enums and #define.
132m4_define([b4_token_defines],
133[m4_if([$@], [[]], [],
134[/* Tokens. */
6b8c3254
PE
135#ifndef YYTOKENTYPE
136# define YYTOKENTYPE
fb8135fa
AD
137 /* Put the tokens into the symbol table, so that GDB and other debuggers
138 know about them. */
139 enum yytokentype {
140m4_map_sep([ b4_token_enum], [,
141],
142 [$@])
143 };
095b9f05 144#endif
fb8135fa
AD
145m4_map([b4_token_define], [$@])
146])
147])
4a2a22f4
AD
148
149
ae7453f2 150
0245f82d
AD
151## --------------------------------------------- ##
152## Defining C functions in both K&R and ANSI-C. ##
153## --------------------------------------------- ##
4a2a22f4
AD
154
155
0245f82d
AD
156# b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
157# ----------------------------------------------------------
4a2a22f4 158# Declare the function NAME.
0245f82d
AD
159m4_define([b4_c_function_def],
160[#if defined (__STDC__) || defined (__cplusplus)
161b4_c_ansi_function_def($@)
4a2a22f4 162#else
0245f82d
AD
163$2
164$1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
165b4_c_knr_formal_decls(m4_shiftn(2, $@))
4a2a22f4
AD
166#endif[]dnl
167])
168
169
0245f82d
AD
170# b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
171# ---------------------------------------------------------------
172# Declare the function NAME in ANSI.
173m4_define([b4_c_ansi_function_def],
174[$2
175$1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
176])
177
178
179# b4_c_ansi_formals([DECL1, NAME1], ...)
180# --------------------------------------
4a2a22f4 181# Output the arguments ANSI-C definition.
0245f82d
AD
182m4_define([b4_c_ansi_formals],
183[m4_case([$@],
184 [], [void],
185 [[]], [void],
186 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
4a2a22f4 187
0245f82d
AD
188m4_define([b4_c_ansi_formal],
189[$1])
4a2a22f4
AD
190
191
0245f82d
AD
192# b4_c_knr_formal_names([DECL1, NAME1], ...)
193# ------------------------------------------
4a2a22f4 194# Output the argument names.
0245f82d
AD
195m4_define([b4_c_knr_formal_names],
196[m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
4a2a22f4 197
0245f82d 198m4_define([b4_c_knr_formal_name],
4a2a22f4
AD
199[$2])
200
201
0245f82d
AD
202# b4_c_knr_formal_decls([DECL1, NAME1], ...)
203# ------------------------------------------
4a2a22f4 204# Output the K&R argument declarations.
0245f82d
AD
205m4_define([b4_c_knr_formal_decls],
206[m4_map_sep([b4_c_knr_formal_decl],
4a2a22f4
AD
207 [
208],
209 [$@])])
210
0245f82d
AD
211m4_define([b4_c_knr_formal_decl],
212[ $1;])
21964f43
AD
213
214
215
0245f82d
AD
216## ------------------------------------------------------------ ##
217## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
218## ------------------------------------------------------------ ##
21964f43
AD
219
220
0245f82d
AD
221# b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
222# -----------------------------------------------------------
223# Declare the function NAME.
224m4_define([b4_c_function_decl],
225[#if defined (__STDC__) || defined (__cplusplus)
226b4_c_ansi_function_decl($@)
227#else
228$2 $1 ();
229#endif[]dnl
230])
21964f43
AD
231
232
0245f82d
AD
233# b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
234# ----------------------------------------------------------------
235# Declare the function NAME.
236m4_define([b4_c_ansi_function_decl],
237[$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
238])
239
240
241
242
243## --------------------- ##
244## Calling C functions. ##
245## --------------------- ##
246
247
248# b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
249# -----------------------------------------------------------
250# Call the function NAME with arguments NAME1, NAME2 etc.
251m4_define([b4_c_function_call],
252[$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
253])
254
255
256# b4_c_args([DECL1, NAME1], ...)
257# ------------------------------
258# Output the arguments NAME1, NAME2...
259m4_define([b4_c_args],
260[m4_map_sep([b4_c_arg], [, ], [$@])])
261
262m4_define([b4_c_arg],
263[$2])