]> git.saurik.com Git - bison.git/blob - data/c.m4
2006-10-08 Paolo Bonzini <bonzini@gnu.org>
[bison.git] / data / c.m4
1 m4_divert(-1) -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301 USA
20
21 ## ---------------- ##
22 ## Identification. ##
23 ## ---------------- ##
24
25 # b4_comment(TEXT)
26 # ----------------
27 m4_define([b4_comment], [/* m4_bpatsubst([$1], [
28 ], [
29 ]) */])
30
31 # b4_identification
32 # -----------------
33 m4_define([b4_identification],
34 [/* Identify Bison output. */
35 [#]define YYBISON 1
36
37 /* Bison version. */
38 [#]define YYBISON_VERSION "b4_version"
39
40 /* Skeleton name. */
41 [#]define YYSKELETON_NAME b4_skeleton
42
43 /* Pure parsers. */
44 [#]define YYPURE b4_pure_flag
45
46 /* Push parsers. */
47 [#]define YYPUSH b4_push_flag
48
49 /* Using locations. */
50 [#]define YYLSP_NEEDED b4_locations_flag
51 ])
52
53
54 ## ---------------- ##
55 ## Default values. ##
56 ## ---------------- ##
57
58 # If the %union is not named, its name is YYSTYPE.
59 m4_define_default([b4_union_name], [YYSTYPE])
60
61 # If the %name-prefix is not given, it is yy.
62 m4_define_default([b4_prefix], [yy])
63
64 ## ------------------------ ##
65 ## Pure/impure interfaces. ##
66 ## ------------------------ ##
67
68 # b4_user_args
69 # ------------
70 m4_define([b4_user_args],
71 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
72
73
74 # b4_parse_param
75 # --------------
76 # If defined, b4_parse_param arrives double quoted, but below we prefer
77 # it to be single quoted.
78 m4_define([b4_parse_param],
79 b4_parse_param))
80
81
82 # b4_parse_param_for(DECL, FORMAL, BODY)
83 # ---------------------------------------
84 # Iterate over the user parameters, binding the declaration to DECL,
85 # the formal name to FORMAL, and evaluating the BODY.
86 m4_define([b4_parse_param_for],
87 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
88 [m4_pushdef([$1], m4_fst($1_$2))dnl
89 m4_pushdef([$2], m4_shift($1_$2))dnl
90 $3[]dnl
91 m4_popdef([$2])dnl
92 m4_popdef([$1])dnl
93 ])])
94
95 # b4_parse_param_use
96 # ------------------
97 # `YYUSE' all the parse-params.
98 m4_define([b4_parse_param_use],
99 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
100 ])dnl
101 ])
102
103
104 ## ------------ ##
105 ## Data Types. ##
106 ## ------------ ##
107
108 # b4_int_type(MIN, MAX)
109 # ---------------------
110 # Return the smallest int type able to handle numbers ranging from
111 # MIN to MAX (included).
112 m4_define([b4_int_type],
113 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
114 b4_ints_in($@, [-128], [127]), [1], [signed char],
115
116 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
117 b4_ints_in($@, [-32768], [32767]), [1], [short int],
118
119 m4_eval([0 <= $1]), [1], [unsigned int],
120
121 [int])])
122
123
124 # b4_int_type_for(NAME)
125 # ---------------------
126 # Return the smallest int type able to handle numbers ranging from
127 # `NAME_min' to `NAME_max' (included).
128 m4_define([b4_int_type_for],
129 [b4_int_type($1_min, $1_max)])
130
131 ## ---------##
132 ## Values. ##
133 ## ---------##
134
135 # b4_null
136 ---------
137 # Return a null pointer constant. NULL infringes on the user name
138 # space in C, so use 0 rather than NULL.
139 m4_define([b4_null], [0])
140
141
142 ## ------------------------- ##
143 ## Assigning token numbers. ##
144 ## ------------------------- ##
145
146 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
147 # -----------------------------------------
148 # Output the definition of this token as #define.
149 m4_define([b4_token_define],
150 [#define $1 $2
151 ])
152
153
154 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
155 # -------------------------------------------------------
156 # Output the definition of the tokens (if there are) as #defines.
157 m4_define([b4_token_defines],
158 [m4_if([$@], [[]], [],
159 [/* Tokens. */
160 m4_map([b4_token_define], [$@])])
161 ])
162
163
164 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
165 # ---------------------------------------
166 # Output the definition of this token as an enum.
167 m4_define([b4_token_enum],
168 [$1 = $2])
169
170
171 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
172 # -----------------------------------------------------
173 # Output the definition of the tokens (if there are) as enums.
174 m4_define([b4_token_enums],
175 [m4_if([$@], [[]], [],
176 [/* Tokens. */
177 #ifndef YYTOKENTYPE
178 # define YYTOKENTYPE
179 /* Put the tokens into the symbol table, so that GDB and other debuggers
180 know about them. */
181 enum yytokentype {
182 m4_map_sep([ b4_token_enum], [,
183 ],
184 [$@])
185 };
186 #endif
187 ])])
188
189
190 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
191 # -------------------------------------------------------------
192 # Output the definition of the tokens (if there are any) as enums and, if POSIX
193 # Yacc is enabled, as #defines.
194 m4_define([b4_token_enums_defines],
195 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
196 ])
197
198
199
200 ## --------------------------------------------- ##
201 ## Defining C functions in both K&R and ANSI-C. ##
202 ## --------------------------------------------- ##
203
204
205 # b4_modern_c
206 # -----------
207 # A predicate useful in #if to determine whether C is ancient or modern.
208 #
209 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
210 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
211 # reasons, but it defines __C99__FUNC__ so check that as well.
212 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
213 # Consider a C++ compiler to be modern if it defines __cplusplus.
214 #
215 m4_define([b4_c_modern],
216 [[(defined __STDC__ || defined __C99__FUNC__ \
217 || defined __cplusplus || defined _MSC_VER)]])
218
219 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
220 # ----------------------------------------------------------
221 # Declare the function NAME.
222 m4_define([b4_c_function_def],
223 [#if b4_c_modern
224 b4_c_ansi_function_def($@)
225 #else
226 $2
227 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
228 b4_c_knr_formal_decls(m4_shiftn(2, $@))
229 #endif[]dnl
230 ])
231
232
233 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
234 # ---------------------------------------------------------------
235 # Declare the function NAME in ANSI.
236 m4_define([b4_c_ansi_function_def],
237 [$2
238 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
239 ])
240
241
242 # b4_c_ansi_formals([DECL1, NAME1], ...)
243 # --------------------------------------
244 # Output the arguments ANSI-C definition.
245 m4_define([b4_c_ansi_formals],
246 [m4_case([$@],
247 [], [void],
248 [[]], [void],
249 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
250
251 m4_define([b4_c_ansi_formal],
252 [$1])
253
254
255 # b4_c_knr_formal_names([DECL1, NAME1], ...)
256 # ------------------------------------------
257 # Output the argument names.
258 m4_define([b4_c_knr_formal_names],
259 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
260
261 m4_define([b4_c_knr_formal_name],
262 [$2])
263
264
265 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
266 # ------------------------------------------
267 # Output the K&R argument declarations.
268 m4_define([b4_c_knr_formal_decls],
269 [m4_map_sep([b4_c_knr_formal_decl],
270 [
271 ],
272 [$@])])
273
274 m4_define([b4_c_knr_formal_decl],
275 [ $1;])
276
277
278
279 ## ------------------------------------------------------------ ##
280 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
281 ## ------------------------------------------------------------ ##
282
283
284 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
285 # -----------------------------------------------------------
286 # Declare the function NAME.
287 m4_define([b4_c_function_decl],
288 [#if defined __STDC__ || defined __cplusplus
289 b4_c_ansi_function_decl($@)
290 #else
291 $2 $1 ();
292 #endif[]dnl
293 ])
294
295
296 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
297 # ----------------------------------------------------------------
298 # Declare the function NAME.
299 m4_define([b4_c_ansi_function_decl],
300 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
301 ])
302
303
304
305
306 ## --------------------- ##
307 ## Calling C functions. ##
308 ## --------------------- ##
309
310
311 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
312 # -----------------------------------------------------------
313 # Call the function NAME with arguments NAME1, NAME2 etc.
314 m4_define([b4_c_function_call],
315 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
316 ])
317
318
319 # b4_c_args([DECL1, NAME1], ...)
320 # ------------------------------
321 # Output the arguments NAME1, NAME2...
322 m4_define([b4_c_args],
323 [m4_map_sep([b4_c_arg], [, ], [$@])])
324
325 m4_define([b4_c_arg],
326 [$2])
327
328
329 ## ----------- ##
330 ## Synclines. ##
331 ## ----------- ##
332
333 # b4_sync_start(LINE, FILE)
334 # -----------------------
335 m4_define([b4_sync_start], [[#]line $1 $2])
336
337
338 ## -------------- ##
339 ## User actions. ##
340 ## -------------- ##
341
342 # b4_case(LABEL, STATEMENTS)
343 # --------------------------
344 m4_define([b4_case],
345 [ case $1:
346 $2
347 break;])
348
349 # b4_symbol_actions(FILENAME, LINENO,
350 # SYMBOL-TAG, SYMBOL-NUM,
351 # SYMBOL-ACTION, SYMBOL-TYPENAME)
352 # -------------------------------------------------
353 m4_define([b4_symbol_actions],
354 [m4_pushdef([b4_dollar_dollar],
355 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
356 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
357 case $4: /* $3 */
358 b4_syncline([$2], [$1])
359 $5;
360 b4_syncline([@oline@], [@ofile@])
361 break;
362 m4_popdef([b4_at_dollar])dnl
363 m4_popdef([b4_dollar_dollar])dnl
364 ])
365
366
367 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
368 # -------------------------------------------
369 # Generate the "yydestruct" function, which declaration is issued using
370 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
371 # or "b4_c_function_def" for K&R.
372 m4_define_default([b4_yydestruct_generate],
373 [[/*-----------------------------------------------.
374 | Release the memory associated to this symbol. |
375 `-----------------------------------------------*/
376
377 /*ARGSUSED*/
378 ]$1([yydestruct],
379 [static void],
380 [[const char *yymsg], [yymsg]],
381 [[int yytype], [yytype]],
382 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
383 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
384 m4_ifset([b4_parse_param], [, b4_parse_param]))[
385 {
386 YYUSE (yyvaluep);
387 ]b4_locations_if([ YYUSE (yylocationp);
388 ])dnl
389 b4_parse_param_use[]dnl
390 [
391 if (!yymsg)
392 yymsg = "Deleting";
393 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
394
395 switch (yytype)
396 {
397 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
398 default:
399 break;
400 }
401 }]dnl
402 ])
403
404
405 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
406 # ------------------------------------------------
407 # Generate the "yy_symbol_print" function, which declaration is issued using
408 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
409 # or "b4_c_function_def" for K&R.
410 m4_define_default([b4_yy_symbol_print_generate],
411 [[
412 /*--------------------------------.
413 | Print this symbol on YYOUTPUT. |
414 `--------------------------------*/
415
416 /*ARGSUSED*/
417 ]$1([yy_symbol_value_print],
418 [static void],
419 [[FILE *yyoutput], [yyoutput]],
420 [[int yytype], [yytype]],
421 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
422 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
423 m4_ifset([b4_parse_param], [, b4_parse_param]))[
424 {
425 if (!yyvaluep)
426 return;
427 ]b4_locations_if([ YYUSE (yylocationp);
428 ])dnl
429 b4_parse_param_use[]dnl
430 [# ifdef YYPRINT
431 if (yytype < YYNTOKENS)
432 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
433 # else
434 YYUSE (yyoutput);
435 # endif
436 switch (yytype)
437 {
438 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
439 [ default:
440 break;
441 }
442 }
443
444
445 /*--------------------------------.
446 | Print this symbol on YYOUTPUT. |
447 `--------------------------------*/
448
449 ]$1([yy_symbol_print],
450 [static void],
451 [[FILE *yyoutput], [yyoutput]],
452 [[int yytype], [yytype]],
453 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
454 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
455 m4_ifset([b4_parse_param], [, b4_parse_param]))[
456 {
457 if (yytype < YYNTOKENS)
458 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
459 else
460 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
461
462 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
463 YYFPRINTF (yyoutput, ": ");
464 ])dnl
465 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
466 b4_locations_if([, yylocationp])[]b4_user_args[);
467 YYFPRINTF (yyoutput, ")");
468 }]dnl
469 ])