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