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