]> git.saurik.com Git - bison.git/blob - data/c.m4
8c6c4979659cdb60a12b57c98a75833847425ef6
[bison.git] / data / c.m4
1 -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 # b4_tocpp(STRING)
22 # ----------------
23 # Convert STRING into a valid C macro name.
24 m4_define([b4_tocpp],
25 [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
26
27
28 # b4_cpp_guard(FILE)
29 # ------------------
30 # A valid C macro name to use as a CPP header guard for FILE.
31 m4_define([b4_cpp_guard],
32 [b4_tocpp(m4_defn([b4_prefix])/[$1])])
33
34
35 # b4_cpp_guard_open(FILE)
36 # b4_cpp_guard_close(FILE)
37 # ------------------------
38 # Open/close CPP inclusion guards for FILE.
39 m4_define([b4_cpp_guard_open],
40 [#ifndef b4_cpp_guard([$1])
41 # define b4_cpp_guard([$1])])
42
43 m4_define([b4_cpp_guard_close],
44 [#endif b4_comment([!b4_cpp_guard([$1])])])
45
46
47 ## ---------------- ##
48 ## Identification. ##
49 ## ---------------- ##
50
51 # b4_comment(TEXT)
52 # ----------------
53 m4_define([b4_comment], [/* m4_bpatsubst([$1], [
54 ], [
55 ]) */])
56
57 # b4_identification
58 # -----------------
59 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
60 # b4_pull_flag if they use the values of the %define variables api.pure or
61 # api.push-pull.
62 m4_define([b4_identification],
63 [[/* Identify Bison output. */
64 #define YYBISON 1
65
66 /* Bison version. */
67 #define YYBISON_VERSION "]b4_version["
68
69 /* Skeleton name. */
70 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
71
72 /* Pure parsers. */
73 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
74
75 /* Push parsers. */
76 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
77
78 /* Pull parsers. */
79 #define YYPULL ]b4_pull_flag])[
80
81 /* Using locations. */
82 #define YYLSP_NEEDED ]b4_locations_flag[
83 ]])
84
85
86 ## ---------------- ##
87 ## Default values. ##
88 ## ---------------- ##
89
90 # If the %union is not named, its name is YYSTYPE.
91 m4_define_default([b4_union_name], [YYSTYPE])
92
93 # If the %name-prefix is not given, it is yy.
94 m4_define_default([b4_prefix], [yy])
95
96 ## ------------------------ ##
97 ## Pure/impure interfaces. ##
98 ## ------------------------ ##
99
100 # b4_user_args
101 # ------------
102 m4_define([b4_user_args],
103 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
104
105
106 # b4_parse_param
107 # --------------
108 # If defined, b4_parse_param arrives double quoted, but below we prefer
109 # it to be single quoted.
110 m4_define([b4_parse_param],
111 b4_parse_param)
112
113
114 # b4_parse_param_for(DECL, FORMAL, BODY)
115 # ---------------------------------------
116 # Iterate over the user parameters, binding the declaration to DECL,
117 # the formal name to FORMAL, and evaluating the BODY.
118 m4_define([b4_parse_param_for],
119 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
120 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
121 m4_pushdef([$2], m4_shift($1_$2))dnl
122 $3[]dnl
123 m4_popdef([$2])dnl
124 m4_popdef([$1])dnl
125 ])])
126
127 # b4_parse_param_use
128 # ------------------
129 # `YYUSE' all the parse-params.
130 m4_define([b4_parse_param_use],
131 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
132 ])dnl
133 ])
134
135
136 ## ------------ ##
137 ## Data Types. ##
138 ## ------------ ##
139
140 # b4_int_type(MIN, MAX)
141 # ---------------------
142 # Return the smallest int type able to handle numbers ranging from
143 # MIN to MAX (included).
144 m4_define([b4_int_type],
145 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
146 b4_ints_in($@, [-128], [127]), [1], [signed char],
147
148 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
149 b4_ints_in($@, [-32768], [32767]), [1], [short int],
150
151 m4_eval([0 <= $1]), [1], [unsigned int],
152
153 [int])])
154
155
156 # b4_int_type_for(NAME)
157 # ---------------------
158 # Return the smallest int type able to handle numbers ranging from
159 # `NAME_min' to `NAME_max' (included).
160 m4_define([b4_int_type_for],
161 [b4_int_type($1_min, $1_max)])
162
163
164 # b4_table_value_equals(TABLE, VALUE, LITERAL)
165 # --------------------------------------------
166 # Without inducing a comparison warning from the compiler, check if the
167 # literal value LITERAL equals VALUE from table TABLE, which must have
168 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
169 # function that suppresses warnings about constant conditions.
170 m4_define([b4_table_value_equals],
171 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
172 || m4_indir([b4_]$1[_max]) < $3), [1],
173 [[YYID (0)]],
174 [[((]$2[) == (]$3[))]])])
175
176
177 ## ---------##
178 ## Values. ##
179 ## ---------##
180
181
182 # b4_null_define
183 # --------------
184 # Portability issues: define a YY_NULL appropriate for the current
185 # language (C, C++98, or C++11).
186 m4_define([b4_null_define],
187 [# ifndef YY_NULL
188 # if defined __cplusplus && 201103L <= __cplusplus
189 # define YY_NULL nullptr
190 # else
191 # define YY_NULL 0
192 # endif
193 # endif[]dnl
194 ])
195
196
197 # b4_null
198 # -------
199 # Return a null pointer constant.
200 m4_define([b4_null], [YY_NULL])
201
202
203
204 ## ------------------------- ##
205 ## Assigning token numbers. ##
206 ## ------------------------- ##
207
208 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
209 # -----------------------------------------
210 # Output the definition of this token as #define.
211 m4_define([b4_token_define],
212 [#define $1 $2
213 ])
214
215
216 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
217 # -------------------------------------------------------
218 # Output the definition of the tokens (if there are) as #defines.
219 m4_define([b4_token_defines],
220 [m4_if([$#$1], [1], [],
221 [/* Tokens. */
222 m4_map([b4_token_define], [$@])])
223 ])
224
225
226 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
227 # ---------------------------------------
228 # Output the definition of this token as an enum.
229 m4_define([b4_token_enum],
230 [$1 = $2])
231
232
233 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
234 # -----------------------------------------------------
235 # Output the definition of the tokens (if there are) as enums.
236 m4_define([b4_token_enums],
237 [m4_if([$#$1], [1], [],
238 [/* Tokens. */
239 #ifndef YYTOKENTYPE
240 # define YYTOKENTYPE
241 /* Put the tokens into the symbol table, so that GDB and other debuggers
242 know about them. */
243 enum yytokentype {
244 m4_map_sep([ b4_token_enum], [,
245 ],
246 [$@])
247 };
248 #endif
249 ])])
250
251
252 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
253 # -------------------------------------------------------------
254 # Output the definition of the tokens (if there are any) as enums and, if POSIX
255 # Yacc is enabled, as #defines.
256 m4_define([b4_token_enums_defines],
257 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
258 ])
259
260
261
262 ## --------------------------------------------- ##
263 ## Defining C functions in both K&R and ANSI-C. ##
264 ## --------------------------------------------- ##
265
266
267 # b4_modern_c
268 # -----------
269 # A predicate useful in #if to determine whether C is ancient or modern.
270 #
271 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
272 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
273 # reasons, but it defines __C99__FUNC__ so check that as well.
274 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
275 # Consider a C++ compiler to be modern if it defines __cplusplus.
276 #
277 m4_define([b4_c_modern],
278 [[(defined __STDC__ || defined __C99__FUNC__ \
279 || defined __cplusplus || defined _MSC_VER)]])
280
281 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
282 # ----------------------------------------------------------
283 # Declare the function NAME.
284 m4_define([b4_c_function_def],
285 [#if b4_c_modern
286 b4_c_ansi_function_def($@)
287 #else
288 $2
289 $1 (b4_c_knr_formal_names(m4_shift2($@)))
290 b4_c_knr_formal_decls(m4_shift2($@))
291 #endif[]dnl
292 ])
293
294
295 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
296 # ---------------------------------------------------------------
297 # Declare the function NAME in ANSI.
298 m4_define([b4_c_ansi_function_def],
299 [$2
300 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
301 ])
302
303
304 # b4_c_ansi_formals([DECL1, NAME1], ...)
305 # --------------------------------------
306 # Output the arguments ANSI-C definition.
307 m4_define([b4_c_ansi_formals],
308 [m4_if([$#], [0], [void],
309 [$#$1], [1], [void],
310 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
311
312 m4_define([b4_c_ansi_formal],
313 [$1])
314
315
316 # b4_c_knr_formal_names([DECL1, NAME1], ...)
317 # ------------------------------------------
318 # Output the argument names.
319 m4_define([b4_c_knr_formal_names],
320 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
321
322 m4_define([b4_c_knr_formal_name],
323 [$2])
324
325
326 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
327 # ------------------------------------------
328 # Output the K&R argument declarations.
329 m4_define([b4_c_knr_formal_decls],
330 [m4_map_sep([b4_c_knr_formal_decl],
331 [
332 ],
333 [$@])])
334
335 m4_define([b4_c_knr_formal_decl],
336 [ $1;])
337
338
339
340 ## ------------------------------------------------------------ ##
341 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
342 ## ------------------------------------------------------------ ##
343
344
345 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
346 # -----------------------------------------------------------
347 # Declare the function NAME.
348 m4_define([b4_c_function_decl],
349 [#if defined __STDC__ || defined __cplusplus
350 b4_c_ansi_function_decl($@)
351 #else
352 $2 $1 ();
353 #endif[]dnl
354 ])
355
356
357 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
358 # ----------------------------------------------------------------
359 # Declare the function NAME.
360 m4_define([b4_c_ansi_function_decl],
361 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
362 ])
363
364
365
366
367 ## --------------------- ##
368 ## Calling C functions. ##
369 ## --------------------- ##
370
371
372 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
373 # -----------------------------------------------------------
374 # Call the function NAME with arguments NAME1, NAME2 etc.
375 m4_define([b4_c_function_call],
376 [$1 (b4_c_args(m4_shift2($@)))[]dnl
377 ])
378
379
380 # b4_c_args([DECL1, NAME1], ...)
381 # ------------------------------
382 # Output the arguments NAME1, NAME2...
383 m4_define([b4_c_args],
384 [m4_map_sep([b4_c_arg], [, ], [$@])])
385
386 m4_define([b4_c_arg],
387 [$2])
388
389
390 ## ----------- ##
391 ## Synclines. ##
392 ## ----------- ##
393
394 # b4_sync_start(LINE, FILE)
395 # -----------------------
396 m4_define([b4_sync_start], [[#]line $1 $2])
397
398
399 ## -------------- ##
400 ## User actions. ##
401 ## -------------- ##
402
403 # b4_case(LABEL, STATEMENTS)
404 # --------------------------
405 m4_define([b4_case],
406 [ case $1:
407 $2
408 break;])
409
410 # b4_symbol_actions(FILENAME, LINENO,
411 # SYMBOL-TAG, SYMBOL-NUM,
412 # SYMBOL-ACTION, SYMBOL-TYPENAME)
413 # -------------------------------------------------
414 m4_define([b4_symbol_actions],
415 [m4_pushdef([b4_dollar_dollar],
416 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
417 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
418 case $4: /* $3 */
419 b4_syncline([$2], [$1])
420 $5;
421 b4_syncline([@oline@], [@ofile@])
422 break;
423 m4_popdef([b4_at_dollar])dnl
424 m4_popdef([b4_dollar_dollar])dnl
425 ])
426
427
428 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
429 # -------------------------------------------
430 # Generate the "yydestruct" function, which declaration is issued using
431 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
432 # or "b4_c_function_def" for K&R.
433 m4_define_default([b4_yydestruct_generate],
434 [[/*-----------------------------------------------.
435 | Release the memory associated to this symbol. |
436 `-----------------------------------------------*/
437
438 /*ARGSUSED*/
439 ]$1([yydestruct],
440 [static void],
441 [[const char *yymsg], [yymsg]],
442 [[int yytype], [yytype]],
443 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
444 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
445 m4_ifset([b4_parse_param], [, b4_parse_param]))[
446 {
447 YYUSE (yyvaluep);
448 ]b4_locations_if([ YYUSE (yylocationp);
449 ])dnl
450 b4_parse_param_use[]dnl
451 [
452 if (!yymsg)
453 yymsg = "Deleting";
454 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
455
456 switch (yytype)
457 {
458 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
459 default:
460 break;
461 }
462 }]dnl
463 ])
464
465
466 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
467 # ------------------------------------------------
468 # Generate the "yy_symbol_print" function, which declaration is issued using
469 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
470 # or "b4_c_function_def" for K&R.
471 m4_define_default([b4_yy_symbol_print_generate],
472 [[
473 /*--------------------------------.
474 | Print this symbol on YYOUTPUT. |
475 `--------------------------------*/
476
477 /*ARGSUSED*/
478 ]$1([yy_symbol_value_print],
479 [static void],
480 [[FILE *yyoutput], [yyoutput]],
481 [[int yytype], [yytype]],
482 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
483 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
484 m4_ifset([b4_parse_param], [, b4_parse_param]))[
485 {
486 FILE *yyo = yyoutput;
487 YYUSE (yyo);
488 if (!yyvaluep)
489 return;
490 ]b4_locations_if([ YYUSE (yylocationp);
491 ])dnl
492 b4_parse_param_use[]dnl
493 [# ifdef YYPRINT
494 if (yytype < YYNTOKENS)
495 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
496 # else
497 YYUSE (yyoutput);
498 # endif
499 switch (yytype)
500 {
501 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
502 [ default:
503 break;
504 }
505 }
506
507
508 /*--------------------------------.
509 | Print this symbol on YYOUTPUT. |
510 `--------------------------------*/
511
512 ]$1([yy_symbol_print],
513 [static void],
514 [[FILE *yyoutput], [yyoutput]],
515 [[int yytype], [yytype]],
516 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
517 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
518 m4_ifset([b4_parse_param], [, b4_parse_param]))[
519 {
520 if (yytype < YYNTOKENS)
521 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
522 else
523 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
524
525 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
526 YYFPRINTF (yyoutput, ": ");
527 ])dnl
528 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
529 b4_locations_if([, yylocationp])[]b4_user_args[);
530 YYFPRINTF (yyoutput, ")");
531 }]dnl
532 ])
533
534 ## -------------- ##
535 ## Declarations. ##
536 ## -------------- ##
537
538 # b4_declare_yylstype
539 # ------------------
540 # Declarations that might either go into the header (if --defines) or
541 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
542 m4_define([b4_declare_yylstype],
543 [[#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
544 ]m4_ifdef([b4_stype],
545 [[typedef union ]b4_union_name[
546 {
547 ]b4_user_stype[
548 } YYSTYPE;
549 # define YYSTYPE_IS_TRIVIAL 1]],
550 [m4_if(b4_tag_seen_flag, 0,
551 [[typedef int YYSTYPE;
552 # define YYSTYPE_IS_TRIVIAL 1]])])[
553 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
554 # define YYSTYPE_IS_DECLARED 1
555 #endif]b4_locations_if([[
556
557 #if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
558 typedef struct YYLTYPE
559 {
560 int first_line;
561 int first_column;
562 int last_line;
563 int last_column;
564 } YYLTYPE;
565 # define yyltype YYLTYPE /* obsolescent; will be withdrawn */
566 # define YYLTYPE_IS_DECLARED 1
567 # define YYLTYPE_IS_TRIVIAL 1
568 #endif]])
569
570 b4_pure_if([], [[extern YYSTYPE ]b4_prefix[lval;
571 ]b4_locations_if([[extern YYLTYPE ]b4_prefix[lloc;]])])[]dnl
572 ])
573
574 # b4_declare_yydebug
575 # ------------------
576 m4_define([b4_declare_yydebug],
577 [[/* Enabling traces. */
578 #ifndef YYDEBUG
579 # define YYDEBUG ]b4_debug_flag[
580 #endif
581 #if YYDEBUG
582 extern int ]b4_prefix[debug;
583 #endif][]dnl
584 ])