]> git.saurik.com Git - bison.git/blob - data/c.m4
regen
[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 m4_include(b4_pkgdatadir/[c-like.m4])
21
22 # b4_tocpp(STRING)
23 # ----------------
24 # Convert STRING into a valid C macro name.
25 m4_define([b4_tocpp],
26 [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
27
28
29 # b4_cpp_guard(FILE)
30 # ------------------
31 # A valid C macro name to use as a CPP header guard for FILE.
32 m4_define([b4_cpp_guard],
33 [b4_tocpp(m4_defn([b4_prefix])/[$1])])
34
35
36 # b4_cpp_guard_open(FILE)
37 # b4_cpp_guard_close(FILE)
38 # ------------------------
39 # Open/close CPP inclusion guards for FILE.
40 m4_define([b4_cpp_guard_open],
41 [#ifndef b4_cpp_guard([$1])
42 # define b4_cpp_guard([$1])])
43
44 m4_define([b4_cpp_guard_close],
45 [#endif b4_comment([!b4_cpp_guard([$1])])])
46
47
48 ## ---------------- ##
49 ## Identification. ##
50 ## ---------------- ##
51
52 # b4_comment_(TEXT, OPEN, CONTINUE, END)
53 # -------------------------------------
54 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
55 # Avoid adding indentation to the first line, as the indentation comes
56 # from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
57 #
58 # Prefix all the output lines with PREFIX.
59 m4_define([b4_comment_], [$2[]m4_bpatsubst([$1], [
60 \(.\)], [
61 $3\1])$4])
62
63
64 # b4_c_comment(TEXT, [PREFIX])
65 # ----------------------------
66 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
67 # Avoid adding indentation to the first line, as the indentation comes
68 # from "/*". That's why we don't patsubst([$1], [^\(.\)], [ \1]).
69 #
70 # Prefix all the output lines with PREFIX.
71 m4_define([b4_c_comment],
72 [b4_comment_([$1], [$2/* ], [$2 ], [$2 */])])
73
74
75 # b4_comment(TEXT, [PREFIX])
76 # --------------------------
77 # By default, C comments.
78 m4_define([b4_comment], [b4_c_comment($@)])
79
80
81 # b4_identification
82 # -----------------
83 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
84 # b4_pull_flag if they use the values of the %define variables api.pure or
85 # api.push-pull.
86 m4_define([b4_identification],
87 [[/* Identify Bison output. */
88 #define YYBISON 1
89
90 /* Bison version. */
91 #define YYBISON_VERSION "]b4_version["
92
93 /* Skeleton name. */
94 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
95
96 /* Pure parsers. */
97 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
98
99 /* Push parsers. */
100 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
101
102 /* Pull parsers. */
103 #define YYPULL ]b4_pull_flag])[
104 ]])
105
106
107 ## ---------------- ##
108 ## Default values. ##
109 ## ---------------- ##
110
111 # b4_api_prefix, b4_api_PREFIX
112 # ----------------------------
113 # Corresponds to %define api.prefix
114 b4_percent_define_default([[api.prefix]], [[yy]])
115 m4_define([b4_api_prefix],
116 [b4_percent_define_get([[api.prefix]])])
117 m4_define([b4_api_PREFIX],
118 [m4_toupper(b4_api_prefix)])
119
120
121 # b4_prefix
122 # ---------
123 # If the %name-prefix is not given, it is api.prefix.
124 m4_define_default([b4_prefix], [b4_api_prefix])
125
126 # If the %union is not named, its name is YYSTYPE.
127 m4_define_default([b4_union_name], [b4_api_PREFIX[]STYPE])
128
129
130 ## ------------------------ ##
131 ## Pure/impure interfaces. ##
132 ## ------------------------ ##
133
134 # b4_user_args
135 # ------------
136 m4_define([b4_user_args],
137 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
138
139
140 # b4_parse_param
141 # --------------
142 # If defined, b4_parse_param arrives double quoted, but below we prefer
143 # it to be single quoted.
144 m4_define([b4_parse_param],
145 b4_parse_param)
146
147
148 # b4_parse_param_for(DECL, FORMAL, BODY)
149 # ---------------------------------------
150 # Iterate over the user parameters, binding the declaration to DECL,
151 # the formal name to FORMAL, and evaluating the BODY.
152 m4_define([b4_parse_param_for],
153 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
154 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
155 m4_pushdef([$2], m4_shift($1_$2))dnl
156 $3[]dnl
157 m4_popdef([$2])dnl
158 m4_popdef([$1])dnl
159 ])])
160
161 # b4_parse_param_use([VAL], [LOC])
162 # --------------------------------
163 # `YYUSE' VAL, LOC if locations are enabled, and all the parse-params.
164 m4_define([b4_parse_param_use],
165 [m4_ifvaln([$1], [ YYUSE([$1]);])dnl
166 b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl
167 b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
168 ])dnl
169 ])
170
171
172 ## ------------ ##
173 ## Data Types. ##
174 ## ------------ ##
175
176 # b4_int_type(MIN, MAX)
177 # ---------------------
178 # Return the smallest int type able to handle numbers ranging from
179 # MIN to MAX (included).
180 m4_define([b4_int_type],
181 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
182 b4_ints_in($@, [-128], [127]), [1], [signed char],
183
184 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
185 b4_ints_in($@, [-32768], [32767]), [1], [short int],
186
187 m4_eval([0 <= $1]), [1], [unsigned int],
188
189 [int])])
190
191
192 # b4_int_type_for(NAME)
193 # ---------------------
194 # Return the smallest int type able to handle numbers ranging from
195 # `NAME_min' to `NAME_max' (included).
196 m4_define([b4_int_type_for],
197 [b4_int_type($1_min, $1_max)])
198
199
200 # b4_table_value_equals(TABLE, VALUE, LITERAL)
201 # --------------------------------------------
202 # Without inducing a comparison warning from the compiler, check if the
203 # literal value LITERAL equals VALUE from table TABLE, which must have
204 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
205 # function that suppresses warnings about constant conditions.
206 m4_define([b4_table_value_equals],
207 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
208 || m4_indir([b4_]$1[_max]) < $3), [1],
209 [[YYID (0)]],
210 [[((]$2[) == (]$3[))]])])
211
212
213 ## ---------##
214 ## Values. ##
215 ## ---------##
216
217
218 # b4_null_define
219 # --------------
220 # Portability issues: define a YY_NULL appropriate for the current
221 # language (C, C++98, or C++11).
222 m4_define([b4_null_define],
223 [# ifndef YY_NULL
224 # if defined __cplusplus && 201103L <= __cplusplus
225 # define YY_NULL nullptr
226 # else
227 # define YY_NULL 0
228 # endif
229 # endif[]dnl
230 ])
231
232
233 # b4_null
234 # -------
235 # Return a null pointer constant.
236 m4_define([b4_null], [YY_NULL])
237
238 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
239 # -------------------------------------------------------------
240 # Define "yy<TABLE-NAME>" which contents is CONTENT.
241 m4_define([b4_integral_parser_table_define],
242 [m4_ifvaln([$3], [b4_c_comment([$3], [ ])])dnl
243 static const b4_int_type_for([$2]) yy$1[[]] =
244 {
245 $2
246 };dnl
247 ])
248
249
250 ## ------------------------- ##
251 ## Assigning token numbers. ##
252 ## ------------------------- ##
253
254 # b4_token_define(TOKEN-NUM)
255 # --------------------------
256 # Output the definition of this token as #define.
257 m4_define([b4_token_define],
258 [b4_token_format([#define %s %s], [$1])])
259
260 # b4_token_defines
261 # ----------------
262 # Output the definition of the tokens.
263 m4_define([b4_token_defines],
264 [b4_any_token_visible_if([/* Tokens. */
265 m4_join([
266 ], b4_symbol_map([b4_token_define]))
267 ])])
268
269
270 # b4_token_enum(TOKEN-NUM)
271 # ------------------------
272 # Output the definition of this token as an enum.
273 m4_define([b4_token_enum],
274 [b4_token_format([%s = %s], [$1])])
275
276
277 # b4_token_enums
278 # --------------
279 # Output the definition of the tokens (if there are) as enums.
280 m4_define([b4_token_enums],
281 [b4_any_token_visible_if([[/* Tokens. */
282 #ifndef ]b4_api_PREFIX[TOKENTYPE
283 # define ]b4_api_PREFIX[TOKENTYPE
284 /* Put the tokens into the symbol table, so that GDB and other debuggers
285 know about them. */
286 enum ]b4_api_prefix[tokentype
287 {
288 ]m4_join([,
289 ],
290 b4_symbol_map([b4_token_enum]))[
291 };
292 #endif
293 ]])])
294
295
296 # b4_token_enums_defines
297 # ----------------------
298 # Output the definition of the tokens (if there are any) as enums and,
299 # if POSIX Yacc is enabled, as #defines.
300 m4_define([b4_token_enums_defines],
301 [b4_token_enums[]b4_yacc_if([b4_token_defines])])
302
303
304 ## ----------------- ##
305 ## Semantic Values. ##
306 ## ----------------- ##
307
308
309 # b4_symbol_value(VAL, [TYPE])
310 # ----------------------------
311 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
312 # TYPE if TYPE is given, otherwise just return VAL. The result can be
313 # used safetly, it is put in parens to avoid nasty precedence issues.
314 # TYPE is *not* put in braces, provide some if needed.
315 m4_define([b4_symbol_value],
316 [($1[]m4_ifval([$2], [.$2]))])
317
318
319
320 ## ---------------------- ##
321 ## Defining C functions. ##
322 ## ---------------------- ##
323
324
325 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
326 # ----------------------------------------------------------
327 # Declare the function NAME in C.
328 m4_define([b4_c_function_def],
329 [$2
330 $1 (b4_c_formals(m4_shift2($@)))[]dnl
331 ])
332
333
334 # b4_c_formals([DECL1, NAME1], ...)
335 # ---------------------------------
336 # The formal arguments of a C function definition.
337 m4_define([b4_c_formals],
338 [m4_if([$#], [0], [void],
339 [$#$1], [1], [void],
340 [m4_map_sep([b4_c_formal], [, ], [$@])])])
341
342 m4_define([b4_c_formal],
343 [$1])
344
345
346
347 ## ----------------------- ##
348 ## Declaring C functions. ##
349 ## ----------------------- ##
350
351
352 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
353 # -----------------------------------------------------------
354 # Declare the function NAME.
355 m4_define([b4_c_function_decl],
356 [$2 $1 (b4_c_formals(m4_shift2($@)));[]dnl
357 ])
358
359
360
361
362 ## --------------------- ##
363 ## Calling C functions. ##
364 ## --------------------- ##
365
366
367 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
368 # -----------------------------------------------------------
369 # Call the function NAME with arguments NAME1, NAME2 etc.
370 m4_define([b4_c_function_call],
371 [$1 (b4_c_args(m4_shift2($@)))[]dnl
372 ])
373
374
375 # b4_c_args([DECL1, NAME1], ...)
376 # ------------------------------
377 # Output the arguments NAME1, NAME2...
378 m4_define([b4_c_args],
379 [m4_map_sep([b4_c_arg], [, ], [$@])])
380
381 m4_define([b4_c_arg],
382 [$2])
383
384
385 ## ----------- ##
386 ## Synclines. ##
387 ## ----------- ##
388
389 # b4_sync_start(LINE, FILE)
390 # -----------------------
391 m4_define([b4_sync_start], [[#]line $1 $2])
392
393
394 ## -------------- ##
395 ## User actions. ##
396 ## -------------- ##
397
398 # b4_case(LABEL, STATEMENTS)
399 # --------------------------
400 m4_define([b4_case],
401 [ case $1:
402 $2
403 b4_syncline([@oline@], [@ofile@])
404 break;])
405
406
407 # b4_predicate_case(LABEL, CONDITIONS)
408 # ------------------------------------
409 m4_define([b4_predicate_case],
410 [ case $1:
411 if (! ($2)) YYERROR;
412 b4_syncline([@oline@], [@ofile@])
413 break;])
414
415
416 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
417 # -------------------------------------------
418 # Generate the "yydestruct" function, which declaration is issued using
419 # FUNCTION-DECLARATOR, which may be "b4_c_function_def".
420 m4_define_default([b4_yydestruct_generate],
421 [[/*-----------------------------------------------.
422 | Release the memory associated to this symbol. |
423 `-----------------------------------------------*/
424
425 /*ARGSUSED*/
426 ]$1([yydestruct],
427 [static void],
428 [[const char *yymsg], [yymsg]],
429 [[int yytype], [yytype]],
430 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
431 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
432 m4_ifset([b4_parse_param], [, b4_parse_param]))[
433 {
434 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
435 [ if (!yymsg)
436 yymsg = "Deleting";
437 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
438
439 switch (yytype)
440 {
441 ]b4_symbol_foreach([b4_symbol_destructor])dnl
442 [ default:
443 break;
444 }
445 }]dnl
446 ])
447
448
449 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
450 # ------------------------------------------------
451 # Generate the "yy_symbol_print" function, which declaration is issued using
452 # FUNCTION-DECLARATOR, which may be "b4_c_function_def".
453 m4_define_default([b4_yy_symbol_print_generate],
454 [[
455 /*--------------------------------.
456 | Print this symbol on YYOUTPUT. |
457 `--------------------------------*/
458
459 /*ARGSUSED*/
460 ]$1([yy_symbol_value_print],
461 [static void],
462 [[FILE *yyoutput], [yyoutput]],
463 [[int yytype], [yytype]],
464 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
465 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
466 m4_ifset([b4_parse_param], [, b4_parse_param]))[
467 {
468 FILE *yyo = yyoutput;
469 ]b4_parse_param_use([yyo], [yylocationp])dnl
470 [ if (!yyvaluep)
471 return;]
472 dnl glr.c does not feature yytoknum.
473 m4_if(b4_skeleton, ["yacc.c"],
474 [[# ifdef YYPRINT
475 if (yytype < YYNTOKENS)
476 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
477 # endif
478 ]])dnl
479 [ switch (yytype)
480 {
481 ]b4_symbol_foreach([b4_symbol_printer])dnl
482 [ default:
483 break;
484 }
485 }
486
487
488 /*--------------------------------.
489 | Print this symbol on YYOUTPUT. |
490 `--------------------------------*/
491
492 ]$1([yy_symbol_print],
493 [static void],
494 [[FILE *yyoutput], [yyoutput]],
495 [[int yytype], [yytype]],
496 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
497 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
498 m4_ifset([b4_parse_param], [, b4_parse_param]))[
499 {
500 if (yytype < YYNTOKENS)
501 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
502 else
503 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
504
505 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
506 YYFPRINTF (yyoutput, ": ");
507 ])dnl
508 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
509 b4_locations_if([, yylocationp])[]b4_user_args[);
510 YYFPRINTF (yyoutput, ")");
511 }]dnl
512 ])
513
514 ## -------------- ##
515 ## Declarations. ##
516 ## -------------- ##
517
518 # b4_declare_yylstype
519 # -------------------
520 # Declarations that might either go into the header (if --defines) or
521 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
522 m4_define([b4_declare_yylstype],
523 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
524 ]m4_ifdef([b4_stype],
525 [[typedef union ]b4_union_name[
526 {
527 ]b4_user_stype[
528 } ]b4_api_PREFIX[STYPE;
529 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]],
530 [m4_if(b4_tag_seen_flag, 0,
531 [[typedef int ]b4_api_PREFIX[STYPE;
532 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]])])[
533 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
534 #endif]b4_locations_if([[
535
536 #if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
537 typedef struct ]b4_api_PREFIX[LTYPE
538 {
539 int first_line;
540 int first_column;
541 int last_line;
542 int last_column;
543 } ]b4_api_PREFIX[LTYPE;
544 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
545 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
546 #endif]])
547
548 b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
549 ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
550 ])
551
552 # b4_YYDEBUG_define
553 # ------------------
554 m4_define([b4_YYDEBUG_define],
555 [[/* Enabling traces. */
556 ]m4_if(b4_api_prefix, [yy],
557 [[#ifndef YYDEBUG
558 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
559 #endif]],
560 [[#ifndef ]b4_api_PREFIX[DEBUG
561 # if defined YYDEBUG
562 #if YYDEBUG
563 # define ]b4_api_PREFIX[DEBUG 1
564 # else
565 # define ]b4_api_PREFIX[DEBUG 0
566 # endif
567 # else /* ! defined YYDEBUG */
568 # define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[
569 # endif /* ! defined ]b4_api_PREFIX[DEBUG */
570 #endif /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl
571 ])
572
573 # b4_declare_yydebug
574 # ------------------
575 m4_define([b4_declare_yydebug],
576 [b4_YYDEBUG_define[
577 #if ]b4_api_PREFIX[DEBUG
578 extern int ]b4_prefix[debug;
579 #endif][]dnl
580 ])
581
582 # b4_yylloc_default_define
583 # ------------------------
584 # Define YYLLOC_DEFAULT.
585 m4_define([b4_yylloc_default_define],
586 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
587 If N is 0, then set CURRENT to the empty location which ends
588 the previous symbol: RHS[0] (always defined). */
589
590 #ifndef YYLLOC_DEFAULT
591 # define YYLLOC_DEFAULT(Current, Rhs, N) \
592 do \
593 if (YYID (N)) \
594 { \
595 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
596 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
597 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
598 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
599 } \
600 else \
601 { \
602 (Current).first_line = (Current).last_line = \
603 YYRHSLOC (Rhs, 0).last_line; \
604 (Current).first_column = (Current).last_column = \
605 YYRHSLOC (Rhs, 0).last_column; \
606 } \
607 while (YYID (0))
608 #endif
609 ]])