]> git.saurik.com Git - bison.git/blob - data/c.m4
* data/c.m4 (b4_location_initial_column, b4_location_initial_line):
[bison.git] / data / c.m4
1 m4_divert(-1) -*- Autoconf -*-
2
3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301 USA
20
21
22 ## ---------------- ##
23 ## Identification. ##
24 ## ---------------- ##
25
26 # b4_copyright(TITLE, YEARS)
27 # --------------------------
28 m4_define([b4_copyright],
29 [/* A Bison parser, made by GNU Bison b4_version. */
30
31 /* $1
32
33 m4_text_wrap([Copyright (C) $2 Free Software Foundation, Inc.], [ ])
34
35 This program is free software; you can redistribute it and/or modify
36 it under the terms of the GNU General Public License as published by
37 the Free Software Foundation; either version 2, or (at your option)
38 any later version.
39
40 This program is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
44
45 You should have received a copy of the GNU General Public License
46 along with this program; if not, write to the Free Software
47 Foundation, Inc., 51 Franklin Street, Fifth Floor,
48 Boston, MA 02110-1301, USA. */
49
50 /* As a special exception, you may create a larger work that contains
51 part or all of the Bison parser skeleton and distribute that work
52 under terms of your choice, so long as that work isn't itself a
53 parser generator using the skeleton or a modified version thereof
54 as a parser skeleton. Alternatively, if you modify or redistribute
55 the parser skeleton itself, you may (at your option) remove this
56 special exception, which will cause the skeleton and the resulting
57 Bison output files to be licensed under the GNU General Public
58 License without this special exception.
59
60 This special exception was added by the Free Software Foundation in
61 version 2.2 of Bison. */])
62
63
64 # b4_identification
65 # -----------------
66 m4_define([b4_identification],
67 [/* Identify Bison output. */
68 [#]define YYBISON 1
69
70 /* Bison version. */
71 [#]define YYBISON_VERSION "b4_version"
72
73 /* Skeleton name. */
74 [#]define YYSKELETON_NAME b4_skeleton
75
76 /* Pure parsers. */
77 [#]define YYPURE b4_pure_flag
78
79 /* Using locations. */
80 [#]define YYLSP_NEEDED b4_locations_flag
81 ])
82
83
84
85 ## ---------------- ##
86 ## Default values. ##
87 ## ---------------- ##
88
89 m4_define_default([b4_epilogue], [])
90
91 # If the %union is not named, its name is YYSTYPE.
92 m4_define_default([b4_union_name], [YYSTYPE])
93
94 # The initial column and line.
95 m4_define_default([b4_location_initial_column], [1])
96 m4_define_default([b4_location_initial_line], [1])
97
98
99 ## ------------------------ ##
100 ## Pure/impure interfaces. ##
101 ## ------------------------ ##
102
103
104 # b4_user_args
105 # ------------
106 m4_define([b4_user_args],
107 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
108
109
110 # b4_parse_param
111 # --------------
112 # If defined, b4_parse_param arrives double quoted, but below we prefer
113 # it to be single quoted.
114 m4_define_default([b4_parse_param])
115 m4_define([b4_parse_param],
116 b4_parse_param))
117
118
119 # b4_parse_param_for(DECL, FORMAL, BODY)
120 # ---------------------------------------
121 # Iterate over the user parameters, binding the declaration to DECL,
122 # the formal name to FORMAL, and evaluating the BODY.
123 m4_define([b4_parse_param_for],
124 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
125 [m4_pushdef([$1], m4_fst($1_$2))dnl
126 m4_pushdef([$2], m4_shift($1_$2))dnl
127 $3[]dnl
128 m4_popdef([$2])dnl
129 m4_popdef([$1])dnl
130 ])])
131
132 # b4_parse_param_use
133 # ------------------
134 # `YYUSE' all the parse-params.
135 m4_define([b4_parse_param_use],
136 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
137 ])dnl
138 ])
139
140 ## ------------ ##
141 ## Data Types. ##
142 ## ------------ ##
143
144
145 # b4_ints_in(INT1, INT2, LOW, HIGH)
146 # ---------------------------------
147 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
148 m4_define([b4_ints_in],
149 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
150
151
152 # b4_int_type(MIN, MAX)
153 # ---------------------
154 # Return the smallest int type able to handle numbers ranging from
155 # MIN to MAX (included).
156 m4_define([b4_int_type],
157 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
158 b4_ints_in($@, [-128], [127]), [1], [signed char],
159
160 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
161 b4_ints_in($@, [-32768], [32767]), [1], [short int],
162
163 m4_eval([0 <= $1]), [1], [unsigned int],
164
165 [int])])
166
167
168 # b4_int_type_for(NAME)
169 # ---------------------
170 # Return the smallest int type able to handle numbers ranging from
171 # `NAME_min' to `NAME_max' (included).
172 m4_define([b4_int_type_for],
173 [b4_int_type($1_min, $1_max)])
174
175
176 ## ------------------ ##
177 ## Decoding options. ##
178 ## ------------------ ##
179
180 # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
181 # -----------------------------------
182 # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
183 m4_define([b4_flag_if],
184 [m4_case(b4_$1_flag,
185 [0], [$3],
186 [1], [$2],
187 [m4_fatal([invalid $1 value: ]$1)])])
188
189
190 # b4_define_flag_if(FLAG)
191 # -----------------------
192 # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
193 # value of the Boolean FLAG.
194 m4_define([b4_define_flag_if],
195 [_b4_define_flag_if($[1], $[2], [$1])])
196
197 # _b4_define_flag_if($1, $2, FLAG)
198 # --------------------------------
199 # This macro works around the impossibility to define macros
200 # inside macros, because issuing `[$1]' is not possible in M4 :(.
201 # This sucks hard, GNU M4 should really provide M5 like $$1.
202 m4_define([_b4_define_flag_if],
203 [m4_if([$1$2], $[1]$[2], [],
204 [m4_fatal([$0: Invalid arguments: $@])])dnl
205 m4_define([b4_$3_if],
206 [b4_flag_if([$3], [$1], [$2])])])
207
208
209 # b4_FLAG_if(IF-TRUE, IF-FALSE)
210 # -----------------------------
211 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
212 b4_define_flag_if([defines]) # Whether headers are requested.
213 b4_define_flag_if([error_verbose]) # Wheter error are verbose.
214 b4_define_flag_if([locations]) # Whether locations are tracked.
215 b4_define_flag_if([pure]) # Whether the interface is pure.
216 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
217
218
219
220 ## ------------------------- ##
221 ## Assigning token numbers. ##
222 ## ------------------------- ##
223
224 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
225 # -----------------------------------------
226 # Output the definition of this token as #define.
227 m4_define([b4_token_define],
228 [#define $1 $2
229 ])
230
231
232 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
233 # -------------------------------------------------------
234 # Output the definition of the tokens (if there are) as #defines.
235 m4_define([b4_token_defines],
236 [m4_if([$@], [[]], [],
237 [/* Tokens. */
238 m4_map([b4_token_define], [$@])])
239 ])
240
241
242 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
243 # ---------------------------------------
244 # Output the definition of this token as an enum.
245 m4_define([b4_token_enum],
246 [$1 = $2])
247
248
249 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
250 # -----------------------------------------------------
251 # Output the definition of the tokens (if there are) as enums.
252 m4_define([b4_token_enums],
253 [m4_if([$@], [[]], [],
254 [/* Tokens. */
255 #ifndef YYTOKENTYPE
256 # define YYTOKENTYPE
257 /* Put the tokens into the symbol table, so that GDB and other debuggers
258 know about them. */
259 enum yytokentype {
260 m4_map_sep([ b4_token_enum], [,
261 ],
262 [$@])
263 };
264 #endif
265 ])])
266
267
268 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
269 # -------------------------------------------------------------
270 # Output the definition of the tokens (if there are any) as enums and, if POSIX
271 # Yacc is enabled, as #defines.
272 m4_define([b4_token_enums_defines],
273 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
274 ])
275
276
277
278 ## --------------------------------------------- ##
279 ## Defining C functions in both K&R and ANSI-C. ##
280 ## --------------------------------------------- ##
281
282
283 # b4_modern_c
284 # -----------
285 # A predicate useful in #if to determine whether C is ancient or modern.
286 #
287 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
288 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
289 # reasons, but it defines __C99__FUNC__ so check that as well.
290 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
291 # Consider a C++ compiler to be modern if it defines __cplusplus.
292 #
293 m4_define([b4_c_modern],
294 [[(defined __STDC__ || defined __C99__FUNC__ \
295 || defined __cplusplus || defined _MSC_VER)]])
296
297 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
298 # ----------------------------------------------------------
299 # Declare the function NAME.
300 m4_define([b4_c_function_def],
301 [#if b4_c_modern
302 b4_c_ansi_function_def($@)
303 #else
304 $2
305 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
306 b4_c_knr_formal_decls(m4_shiftn(2, $@))
307 #endif[]dnl
308 ])
309
310
311 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
312 # ---------------------------------------------------------------
313 # Declare the function NAME in ANSI.
314 m4_define([b4_c_ansi_function_def],
315 [$2
316 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
317 ])
318
319
320 # b4_c_ansi_formals([DECL1, NAME1], ...)
321 # --------------------------------------
322 # Output the arguments ANSI-C definition.
323 m4_define([b4_c_ansi_formals],
324 [m4_case([$@],
325 [], [void],
326 [[]], [void],
327 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
328
329 m4_define([b4_c_ansi_formal],
330 [$1])
331
332
333 # b4_c_knr_formal_names([DECL1, NAME1], ...)
334 # ------------------------------------------
335 # Output the argument names.
336 m4_define([b4_c_knr_formal_names],
337 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
338
339 m4_define([b4_c_knr_formal_name],
340 [$2])
341
342
343 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
344 # ------------------------------------------
345 # Output the K&R argument declarations.
346 m4_define([b4_c_knr_formal_decls],
347 [m4_map_sep([b4_c_knr_formal_decl],
348 [
349 ],
350 [$@])])
351
352 m4_define([b4_c_knr_formal_decl],
353 [ $1;])
354
355
356
357 ## ------------------------------------------------------------ ##
358 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
359 ## ------------------------------------------------------------ ##
360
361
362 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
363 # -----------------------------------------------------------
364 # Declare the function NAME.
365 m4_define([b4_c_function_decl],
366 [#if defined __STDC__ || defined __cplusplus
367 b4_c_ansi_function_decl($@)
368 #else
369 $2 $1 ();
370 #endif[]dnl
371 ])
372
373
374 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
375 # ----------------------------------------------------------------
376 # Declare the function NAME.
377 m4_define([b4_c_ansi_function_decl],
378 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
379 ])
380
381
382
383
384 ## --------------------- ##
385 ## Calling C functions. ##
386 ## --------------------- ##
387
388
389 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
390 # -----------------------------------------------------------
391 # Call the function NAME with arguments NAME1, NAME2 etc.
392 m4_define([b4_c_function_call],
393 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
394 ])
395
396
397 # b4_c_args([DECL1, NAME1], ...)
398 # ------------------------------
399 # Output the arguments NAME1, NAME2...
400 m4_define([b4_c_args],
401 [m4_map_sep([b4_c_arg], [, ], [$@])])
402
403 m4_define([b4_c_arg],
404 [$2])
405
406
407 ## ----------- ##
408 ## Synclines. ##
409 ## ----------- ##
410
411 # b4_basename(NAME)
412 # -----------------
413 # Beware that NAME is not evaluated.
414 m4_define([b4_basename],
415 [m4_case([$1],
416 [/], [/],
417 [m4_bpatsubst([$1], [^.*/\([^/]+\)/?$], [\1])])])
418
419
420 # b4_syncline(LINE, FILE)
421 # -----------------------
422 m4_define([b4_syncline],
423 [b4_flag_if([synclines],
424 [/* Line __line__ of b4_basename(m4_quote(__file__)). */
425 [#]line $1 $2])])
426
427
428 # b4_user_code(USER-CODE)
429 # -----------------------
430 # Emit code from the user, ending it with synclines.
431 m4_define([b4_user_code],
432 [$1
433 b4_syncline([@oline@], [@ofile@])])
434
435
436 # b4_define_user_code(MACRO)
437 # --------------------------
438 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
439 m4_define([b4_define_user_code],
440 [m4_define([b4_user_$1],
441 [b4_user_code([b4_$1])])])
442
443
444 # b4_user_actions
445 # b4_user_initial_action
446 # b4_user_post_prologue
447 # b4_user_start_header
448 # b4_user_stype
449 # ----------------------
450 # Macros that issue user code, ending with synclines.
451 b4_define_user_code([actions])
452 b4_define_user_code([initial_action])
453 b4_define_user_code([post_prologue])
454 b4_define_user_code([start_header])
455 b4_define_user_code([stype])
456
457
458 ## -------------- ##
459 ## User actions. ##
460 ## -------------- ##
461
462 # b4_symbol_actions(FILENAME, LINENO,
463 # SYMBOL-TAG, SYMBOL-NUM,
464 # SYMBOL-ACTION, SYMBOL-TYPENAME)
465 # -------------------------------------------------
466 m4_define([b4_symbol_actions],
467 [m4_pushdef([b4_dollar_dollar],
468 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
469 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
470 case $4: /* $3 */
471 b4_syncline([$2], [$1])
472 $5;
473 b4_syncline([@oline@], [@ofile@])
474 break;
475 m4_popdef([b4_at_dollar])dnl
476 m4_popdef([b4_dollar_dollar])dnl
477 ])
478
479
480 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
481 # -------------------------------------------
482 # Generate the "yydestruct" function, which declaration is issued using
483 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
484 # or "b4_c_function_def" for K&R.
485 m4_define_default([b4_yydestruct_generate],
486 [[/*-----------------------------------------------.
487 | Release the memory associated to this symbol. |
488 `-----------------------------------------------*/
489
490 /*ARGSUSED*/
491 ]$1([yydestruct],
492 [static void],
493 [[const char *yymsg], [yymsg]],
494 [[int yytype], [yytype]],
495 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
496 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
497 m4_ifset([b4_parse_param], [, b4_parse_param]))[
498 {
499 YYUSE (yyvaluep);
500 ]b4_locations_if([ YYUSE (yylocationp);
501 ])dnl
502 b4_parse_param_use[]dnl
503 [
504 if (!yymsg)
505 yymsg = "Deleting";
506 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
507
508 switch (yytype)
509 {
510 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
511 default:
512 break;
513 }
514 }]dnl
515 ])
516
517
518 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
519 # ------------------------------------------------
520 # Generate the "yy_symbol_print" function, which declaration is issued using
521 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
522 # or "b4_c_function_def" for K&R.
523 m4_define_default([b4_yy_symbol_print_generate],
524 [[
525 /*--------------------------------.
526 | Print this symbol on YYOUTPUT. |
527 `--------------------------------*/
528
529 /*ARGSUSED*/
530 ]$1([yy_symbol_value_print],
531 [static void],
532 [[FILE *yyoutput], [yyoutput]],
533 [[int yytype], [yytype]],
534 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
535 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
536 m4_ifset([b4_parse_param], [, b4_parse_param]))[
537 {
538 if (!yyvaluep)
539 return;
540 ]b4_locations_if([ YYUSE (yylocationp);
541 ])dnl
542 b4_parse_param_use[]dnl
543 [# ifdef YYPRINT
544 if (yytype < YYNTOKENS)
545 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
546 # else
547 YYUSE (yyoutput);
548 # endif
549 switch (yytype)
550 {
551 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
552 [ default:
553 break;
554 }
555 }
556
557
558 /*--------------------------------.
559 | Print this symbol on YYOUTPUT. |
560 `--------------------------------*/
561
562 ]$1([yy_symbol_print],
563 [static void],
564 [[FILE *yyoutput], [yyoutput]],
565 [[int yytype], [yytype]],
566 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
567 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
568 m4_ifset([b4_parse_param], [, b4_parse_param]))[
569 {
570 if (yytype < YYNTOKENS)
571 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
572 else
573 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
574
575 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
576 YYFPRINTF (yyoutput, ": ");
577 ])dnl
578 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
579 b4_locations_if([, yylocationp])[]b4_user_args[);
580 YYFPRINTF (yyoutput, ")");
581 }]dnl
582 ])