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