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