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