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