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