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