cgram.y revision 1.475 1 %{
2 /* $NetBSD: cgram.y,v 1.475 2023/10/17 19:29:09 rillig Exp $ */
3
4 /*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Jochen Pohl for
20 * The NetBSD Project.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID)
38 __RCSID("$NetBSD: cgram.y,v 1.475 2023/10/17 19:29:09 rillig Exp $");
39 #endif
40
41 #include <limits.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "lint1.h"
46
47 extern char *yytext;
48
49 /*
50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body.
52 */
53 int block_level;
54
55 /*
56 * level for memory allocation. Normally the same as block_level.
57 * An exception is the declaration of parameters in prototypes. Memory
58 * for these can't be freed after the declaration, but symbols must
59 * be removed from the symbol table after the declaration.
60 */
61 size_t mem_block_level;
62
63 /*
64 * Save the no-warns state and restore it to avoid the problem where
65 * if (expr) { stmt } / * NOLINT * / stmt;
66 */
67 #define LWARN_NOTHING_SAVED (-3)
68 static int saved_lwarn = LWARN_NOTHING_SAVED;
69
70 static void cgram_declare(sym_t *, bool, sbuf_t *);
71 static void read_until_rparen(void);
72 static sym_t *symbolrename(sym_t *, sbuf_t *);
73
74
75 /* ARGSUSED */
76 static void
77 clear_warning_flags_loc(const char *file, size_t line)
78 {
79 debug_step("%s:%zu: clearing flags", file, line);
80 clear_warn_flags();
81 saved_lwarn = LWARN_NOTHING_SAVED;
82 }
83
84 /* ARGSUSED */
85 static void
86 save_warning_flags_loc(const char *file, size_t line)
87 {
88 /*
89 * There used to be an assertion that saved_lwarn is
90 * LWARN_NOTHING_SAVED here, but that triggered for the following
91 * code:
92 *
93 * void function(int x) { if (x > 0) if (x > 1) return; }
94 *
95 * It didn't trigger if the inner 'if' was enclosed in braces though.
96 *
97 * TODO: If actually needed, add support for nested suppression of
98 * warnings.
99 */
100 debug_step("%s:%zu: saving flags %d", file, line, lwarn);
101 saved_lwarn = lwarn;
102 }
103
104 /* ARGSUSED */
105 static void
106 restore_warning_flags_loc(const char *file, size_t line)
107 {
108 if (saved_lwarn != LWARN_NOTHING_SAVED) {
109 lwarn = saved_lwarn;
110 debug_step("%s:%zu: restoring flags %d", file, line, lwarn);
111 /*
112 * Do not set 'saved_lwarn = LWARN_NOTHING_SAVED' here, to
113 * avoid triggering the assertion in save_warning_flags_loc.
114 */
115 } else
116 clear_warning_flags_loc(file, line);
117 }
118
119 #define clear_warning_flags() clear_warning_flags_loc(__FILE__, __LINE__)
120 #define save_warning_flags() save_warning_flags_loc(__FILE__, __LINE__)
121 #define restore_warning_flags() restore_warning_flags_loc(__FILE__, __LINE__)
122
123 static bool
124 is_either(const char *s, const char *a, const char *b)
125 {
126 return strcmp(s, a) == 0 || strcmp(s, b) == 0;
127 }
128
129 #if YYDEBUG && YYBYACC
130 #define YYSTYPE_TOSTRING cgram_to_string
131 #endif
132
133 %}
134
135 %expect 104
136
137 %union {
138 val_t *y_val;
139 sbuf_t *y_name;
140 sym_t *y_sym;
141 bool y_inc;
142 op_t y_op;
143 scl_t y_scl;
144 tspec_t y_tspec;
145 type_qualifiers y_type_qualifiers;
146 function_specifier y_function_specifier;
147 struct parameter_list y_parameter_list;
148 type_t *y_type;
149 tnode_t *y_tnode;
150 range_t y_range;
151 strg_t *y_string;
152 qual_ptr *y_qual_ptr;
153 bool y_seen_statement;
154 struct generic_association *y_generic;
155 struct array_size y_array_size;
156 bool y_in_system_header;
157 };
158
159 /* for Bison:
160 %printer {
161 if (is_integer($$->v_tspec))
162 fprintf(yyo, "%lld", (long long)$$->u.integer);
163 else
164 fprintf(yyo, "%Lg", $$->u.floating);
165 } <y_val>
166 %printer { fprintf(yyo, "'%s'", $$ != NULL ? $$->sb_name : "<null>"); } <y_name>
167 %printer {
168 bool indented = debug_push_indented(true);
169 debug_sym("", $$, "");
170 debug_pop_indented(indented);
171 } <y_sym>
172 %printer { fprintf(yyo, "%s", $$ ? "++" : "--"); } <y_inc>
173 %printer { fprintf(yyo, "%s", op_name($$)); } <y_op>
174 %printer { fprintf(yyo, "%s", scl_name($$)); } <y_scl>
175 %printer { fprintf(yyo, "%s", tspec_name($$)); } <y_tspec>
176 %printer { fprintf(yyo, "%s", type_qualifiers_string($$)); } <y_type_qualifiers>
177 %printer {
178 fprintf(yyo, "%s", function_specifier_name($$));
179 } <y_function_specifier>
180 %printer { fprintf(yyo, "%s", type_name($$)); } <y_type>
181 %printer {
182 if ($$ == NULL)
183 fprintf(yyo, "<null>");
184 else
185 fprintf(yyo, "%s '%s'",
186 op_name($$->tn_op), type_name($$->tn_type));
187 } <y_tnode>
188 %printer { fprintf(yyo, "%zu to %zu", $$.lo, $$.hi); } <y_range>
189 %printer { fprintf(yyo, "length %zu", $$->st_len); } <y_string>
190 %printer {
191 fprintf(yyo, "%s *", type_qualifiers_string($$->qualifiers));
192 } <y_qual_ptr>
193 %printer { fprintf(yyo, "%s", $$ ? "yes" : "no"); } <y_seen_statement>
194 %printer { fprintf(yyo, "%s", type_name($$->ga_arg)); } <y_generic>
195 %printer { fprintf(yyo, "%d", $$.dim); } <y_array_size>
196 %printer { fprintf(yyo, "%s", $$ ? "yes" : "no"); } <y_in_system_header>
197 */
198
199 %token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN
200 %token T_POINT T_ARROW
201 %token T_COMPLEMENT T_LOGNOT
202 %token <y_inc> T_INCDEC
203 %token T_SIZEOF
204 %token T_BUILTIN_OFFSETOF
205 %token T_TYPEOF
206 %token T_EXTENSION
207 %token T_ALIGNAS
208 %token T_ALIGNOF
209 %token T_ASTERISK
210 %token <y_op> T_MULTIPLICATIVE
211 %token <y_op> T_ADDITIVE
212 %token <y_op> T_SHIFT
213 %token <y_op> T_RELATIONAL
214 %token <y_op> T_EQUALITY
215 %token T_AMPER
216 %token T_BITXOR
217 %token T_BITOR
218 %token T_LOGAND
219 %token T_LOGOR
220 %token T_QUEST
221 %token T_COLON
222 %token T_ASSIGN
223 %token <y_op> T_OPASSIGN
224 %token T_COMMA
225 %token T_SEMI
226 %token T_ELLIPSIS
227 %token T_REAL
228 %token T_IMAG
229 %token T_GENERIC
230
231 /* storage classes (extern, static, auto, register and typedef) */
232 %token <y_scl> T_SCLASS
233 %token <y_function_specifier> T_FUNCTION_SPECIFIER
234
235 /*
236 * predefined type keywords (char, int, short, long, unsigned, signed,
237 * float, double, void); see T_TYPENAME for types from typedef
238 */
239 %token <y_tspec> T_TYPE
240
241 %token <y_type_qualifiers> T_QUAL
242 %token <y_type_qualifiers> T_ATOMIC
243
244 /* struct or union */
245 %token <y_tspec> T_STRUCT_OR_UNION
246
247 /* remaining keywords */
248 %token T_ASM
249 %token T_BREAK
250 %token T_CASE
251 %token T_CONTINUE
252 %token T_DEFAULT
253 %token T_DO
254 %token T_ELSE
255 %token T_ENUM
256 %token T_FOR
257 %token T_GOTO
258 %token T_IF
259 %token T_PACKED
260 %token T_RETURN
261 %token T_SWITCH
262 %token T_SYMBOLRENAME
263 %token T_WHILE
264 %token T_STATIC_ASSERT
265
266 %token T_ATTRIBUTE
267
268 %left T_THEN
269 %left T_ELSE
270 %right T_QUEST T_COLON
271 %left T_LOGOR
272 %left T_LOGAND
273 %left T_BITOR
274 %left T_BITXOR
275 %left T_AMPER
276 %left T_EQUALITY
277 %left T_RELATIONAL
278 %left T_SHIFT
279 %left T_ADDITIVE
280 %left T_ASTERISK T_MULTIPLICATIVE
281
282 %token <y_name> T_NAME
283 %token <y_name> T_TYPENAME
284 %token <y_val> T_CON
285 %token <y_string> T_STRING
286
287 /* No type for program. */
288 %type <y_sym> identifier_sym
289 %type <y_name> identifier
290 %type <y_string> string
291 %type <y_tnode> primary_expression
292 %type <y_tnode> generic_selection
293 %type <y_generic> generic_assoc_list
294 %type <y_generic> generic_association
295 %type <y_tnode> postfix_expression
296 /* No type for comma_opt. */
297 %type <y_tnode> gcc_statement_expr_list
298 %type <y_tnode> gcc_statement_expr_item
299 %type <y_op> point_or_arrow
300 %type <y_tnode> argument_expression_list
301 %type <y_tnode> unary_expression
302 %type <y_tnode> cast_expression
303 %type <y_tnode> expression_opt
304 %type <y_tnode> conditional_expression
305 %type <y_tnode> assignment_expression
306 %type <y_tnode> expression
307 %type <y_tnode> constant_expression
308 /* No type for declaration_or_error. */
309 /* No type for declaration. */
310 /* No type for begin_type_declaration_specifiers. */
311 /* No type for begin_type_declmods. */
312 /* No type for begin_type_specifier_qualifier_list. */
313 /* No type for begin_type_specifier_qualifier_list_postfix. */
314 %type <y_type> begin_type_typespec
315 /* No type for begin_type_qualifier_list. */
316 /* No type for declmod. */
317 /* No type for type_attribute_list_opt. */
318 /* No type for type_attribute_list. */
319 /* No type for type_attribute_opt. */
320 /* No type for type_attribute. */
321 /* No type for begin_type. */
322 /* No type for end_type. */
323 %type <y_type> type_specifier
324 %type <y_type> notype_type_specifier
325 %type <y_type> atomic_type_specifier
326 %type <y_type> struct_or_union_specifier
327 %type <y_tspec> struct_or_union
328 %type <y_sym> braced_member_declaration_list
329 /* No type for member_declaration_lbrace. */
330 %type <y_sym> member_declaration_list_with_rbrace
331 %type <y_sym> member_declaration_list
332 %type <y_sym> member_declaration
333 %type <y_sym> notype_member_declarators
334 %type <y_sym> type_member_declarators
335 %type <y_sym> notype_member_declarator
336 %type <y_sym> type_member_declarator
337 %type <y_type> enum_specifier
338 /* No type for enum. */
339 %type <y_sym> enum_declaration
340 /* No type for enum_decl_lbrace. */
341 %type <y_sym> enums_with_opt_comma
342 %type <y_sym> enumerator_list
343 %type <y_sym> enumerator
344 %type <y_type_qualifiers> type_qualifier
345 /* No type for atomic. */
346 %type <y_qual_ptr> pointer
347 %type <y_type_qualifiers> type_qualifier_list_opt
348 %type <y_type_qualifiers> type_qualifier_list
349 /* No type for notype_init_declarators. */
350 /* No type for type_init_declarators. */
351 /* No type for notype_init_declarator. */
352 /* No type for type_init_declarator. */
353 %type <y_sym> notype_declarator
354 %type <y_sym> type_declarator
355 %type <y_sym> notype_direct_declarator
356 %type <y_sym> type_direct_declarator
357 %type <y_sym> type_param_declarator
358 %type <y_sym> notype_param_declarator
359 %type <y_sym> direct_param_declarator
360 %type <y_sym> direct_notype_param_declarator
361 %type <y_parameter_list> param_list
362 /* No type for id_list_lparen. */
363 %type <y_array_size> array_size_opt
364 %type <y_tnode> array_size
365 %type <y_sym> identifier_list
366 %type <y_type> type_name
367 %type <y_sym> abstract_declaration
368 %type <y_sym> abstract_declarator
369 %type <y_sym> direct_abstract_declarator
370 %type <y_parameter_list> abstract_decl_param_list
371 /* No type for abstract_decl_lparen. */
372 %type <y_parameter_list> vararg_parameter_type_list
373 %type <y_parameter_list> parameter_type_list
374 %type <y_sym> parameter_declaration
375 /* No type for braced_initializer. */
376 /* No type for initializer. */
377 /* No type for initializer_list. */
378 /* No type for initializer_list_item. */
379 /* No type for designation. */
380 /* No type for begin_designation. */
381 /* No type for designator_list. */
382 /* No type for designator. */
383 /* No type for static_assert_declaration. */
384 %type <y_range> range
385 /* No type for init_lbrace. */
386 /* No type for init_rbrace. */
387 %type <y_name> asm_or_symbolrename_opt
388 /* No type for statement. */
389 /* No type for non_expr_statement. */
390 /* No type for labeled_statement. */
391 /* No type for label. */
392 /* No type for compound_statement. */
393 /* No type for compound_statement_lbrace. */
394 /* No type for compound_statement_rbrace. */
395 %type <y_seen_statement> block_item_list
396 %type <y_seen_statement> block_item
397 /* No type for expression_statement. */
398 /* No type for selection_statement. */
399 /* No type for if_without_else. */
400 /* No type for if_expr. */
401 /* No type for switch_expr. */
402 /* No type for iteration_statement. */
403 /* No type for while_expr. */
404 /* No type for do_statement. */
405 /* No type for do. */
406 %type <y_tnode> do_while_expr
407 /* No type for for_start. */
408 /* No type for for_exprs. */
409 /* No type for jump_statement. */
410 /* No type for goto. */
411 /* No type for asm_statement. */
412 /* No type for read_until_rparen. */
413 /* No type for translation_unit. */
414 /* No type for external_declaration. */
415 /* No type for top_level_declaration. */
416 /* No type for function_definition. */
417 %type <y_sym> func_declarator
418 /* No type for arg_declaration_list_opt. */
419 /* No type for arg_declaration_list. */
420 /* No type for arg_declaration. */
421 /* No type for gcc_attribute_specifier_list_opt. */
422 /* No type for gcc_attribute_specifier_list. */
423 /* No type for gcc_attribute_specifier. */
424 /* No type for gcc_attribute_list. */
425 /* No type for gcc_attribute. */
426 /* No type for gcc_attribute_parameters. */
427 %type <y_in_system_header> sys
428
429 %%
430
431 program:
432 /* empty */ {
433 /* TODO: Make this an error in C99 mode as well. */
434 if (!allow_trad && !allow_c99) {
435 /* empty translation unit */
436 error(272);
437 } else if (allow_c90) {
438 /* empty translation unit */
439 warning(272);
440 }
441 }
442 | translation_unit
443 ;
444
445 identifier_sym: /* helper for struct/union/enum */
446 identifier {
447 $$ = getsym($1);
448 }
449 ;
450
451 /* K&R ???, C90 ???, C99 6.4.2.1, C11 ??? */
452 identifier:
453 T_NAME {
454 debug_step("cgram: name '%s'", $1->sb_name);
455 $$ = $1;
456 }
457 | T_TYPENAME {
458 debug_step("cgram: typename '%s'", $1->sb_name);
459 $$ = $1;
460 }
461 ;
462
463 /* see C99 6.4.5, string literals are joined by 5.1.1.2 */
464 string:
465 T_STRING
466 | string T_STRING {
467 if (!allow_c90) {
468 /* concatenated strings are illegal in traditional C */
469 warning(219);
470 }
471 $$ = cat_strings($1, $2);
472 }
473 ;
474
475 /* K&R 7.1, C90 ???, C99 6.5.1, C11 6.5.1 */
476 primary_expression:
477 T_NAME {
478 bool sys_name, sys_next;
479 sys_name = in_system_header;
480 if (yychar < 0)
481 yychar = yylex();
482 sys_next = in_system_header;
483 in_system_header = sys_name;
484 $$ = build_name(getsym($1), yychar == T_LPAREN);
485 in_system_header = sys_next;
486 }
487 | T_CON {
488 $$ = build_constant(gettyp($1->v_tspec), $1);
489 }
490 | string {
491 $$ = build_string($1);
492 }
493 | T_LPAREN expression T_RPAREN {
494 if ($2 != NULL)
495 $2->tn_parenthesized = true;
496 $$ = $2;
497 }
498 | generic_selection
499 /* GCC primary-expression, see c_parser_postfix_expression */
500 /* TODO: C99 7.17p3 allows not only an identifier but a designator. */
501 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
502 set_symtyp(FMEMBER);
503 $$ = build_offsetof($3, getsym($5));
504 }
505 ;
506
507 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
508 generic_selection:
509 T_GENERIC T_LPAREN assignment_expression T_COMMA
510 generic_assoc_list T_RPAREN {
511 /* generic selection requires C11 or later */
512 c11ism(345);
513 $$ = build_generic_selection($3, $5);
514 }
515 ;
516
517 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
518 generic_assoc_list:
519 generic_association
520 | generic_assoc_list T_COMMA generic_association {
521 $3->ga_prev = $1;
522 $$ = $3;
523 }
524 ;
525
526 /* K&R ---, C90 ---, C99 ---, C11 6.5.1.1 */
527 generic_association:
528 type_name T_COLON assignment_expression {
529 $$ = block_zero_alloc(sizeof(*$$), "generic");
530 $$->ga_arg = $1;
531 $$->ga_result = $3;
532 }
533 | T_DEFAULT T_COLON assignment_expression {
534 $$ = block_zero_alloc(sizeof(*$$), "generic");
535 $$->ga_arg = NULL;
536 $$->ga_result = $3;
537 }
538 ;
539
540 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2, C23 6.5.2 */
541 postfix_expression:
542 primary_expression
543 | postfix_expression T_LBRACK sys expression T_RBRACK {
544 $$ = build_unary(INDIR, $3, build_binary($1, PLUS, $3, $4));
545 }
546 | postfix_expression T_LPAREN sys T_RPAREN {
547 $$ = build_function_call($1, $3, NULL);
548 }
549 | postfix_expression T_LPAREN sys argument_expression_list T_RPAREN {
550 $$ = build_function_call($1, $3, $4);
551 }
552 | postfix_expression point_or_arrow sys T_NAME {
553 $$ = build_member_access($1, $2, $3, $4);
554 }
555 | postfix_expression T_INCDEC sys {
556 $$ = build_unary($2 ? INCAFT : DECAFT, $3, $1);
557 }
558 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
559 sym_t *tmp = mktempsym($2);
560 begin_initialization(tmp);
561 cgram_declare(tmp, true, NULL);
562 } braced_initializer {
563 if (!allow_c99)
564 /* compound literals are a C99/GCC extension */
565 gnuism(319);
566 $$ = build_name(current_initsym(), false);
567 end_initialization();
568 }
569 | T_LPAREN compound_statement_lbrace {
570 begin_statement_expr();
571 } gcc_statement_expr_list {
572 do_statement_expr($4);
573 } compound_statement_rbrace T_RPAREN {
574 $$ = end_statement_expr();
575 }
576 ;
577
578 comma_opt: /* helper for 'postfix_expression' */
579 /* empty */
580 | T_COMMA
581 ;
582
583 /*
584 * The inner part of a GCC statement-expression of the form ({ ... }).
585 *
586 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
587 */
588 gcc_statement_expr_list:
589 gcc_statement_expr_item
590 | gcc_statement_expr_list gcc_statement_expr_item {
591 $$ = $2;
592 }
593 ;
594
595 gcc_statement_expr_item:
596 declaration_or_error {
597 clear_warning_flags();
598 $$ = NULL;
599 }
600 | non_expr_statement {
601 $$ = expr_alloc_tnode();
602 $$->tn_type = gettyp(VOID);
603 }
604 | T_SEMI {
605 $$ = expr_alloc_tnode();
606 $$->tn_type = gettyp(VOID);
607 }
608 | expression T_SEMI {
609 if ($1 == NULL) { /* in case of syntax errors */
610 $$ = expr_alloc_tnode();
611 $$->tn_type = gettyp(VOID);
612 } else {
613 /* XXX: do that only on the last name */
614 if ($1->tn_op == NAME)
615 $1->tn_sym->s_used = true;
616 expr($1, false, false, false, false);
617 suppress_fallthrough = false;
618 $$ = $1;
619 }
620 }
621 ;
622
623 point_or_arrow: /* helper for 'postfix_expression' */
624 T_POINT {
625 set_symtyp(FMEMBER);
626 $$ = POINT;
627 }
628 | T_ARROW {
629 set_symtyp(FMEMBER);
630 $$ = ARROW;
631 }
632 ;
633
634 /* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2 */
635 argument_expression_list:
636 assignment_expression {
637 $$ = build_function_argument(NULL, $1);
638 }
639 | argument_expression_list T_COMMA assignment_expression {
640 $$ = build_function_argument($1, $3);
641 }
642 ;
643
644 /* K&R 7.2, C90 ???, C99 6.5.3, C11 6.5.3 */
645 unary_expression:
646 postfix_expression
647 | T_INCDEC sys unary_expression {
648 $$ = build_unary($1 ? INCBEF : DECBEF, $2, $3);
649 }
650 | T_AMPER sys cast_expression {
651 $$ = build_unary(ADDR, $2, $3);
652 }
653 | T_ASTERISK sys cast_expression {
654 $$ = build_unary(INDIR, $2, $3);
655 }
656 | T_ADDITIVE sys cast_expression {
657 if (!allow_c90 && $1 == PLUS) {
658 /* unary '+' is illegal in traditional C */
659 warning(100);
660 }
661 $$ = build_unary($1 == PLUS ? UPLUS : UMINUS, $2, $3);
662 }
663 | T_COMPLEMENT sys cast_expression {
664 $$ = build_unary(COMPL, $2, $3);
665 }
666 | T_LOGNOT sys cast_expression {
667 $$ = build_unary(NOT, $2, $3);
668 }
669 | T_REAL sys cast_expression { /* GCC c_parser_unary_expression */
670 $$ = build_unary(REAL, $2, $3);
671 }
672 | T_IMAG sys cast_expression { /* GCC c_parser_unary_expression */
673 $$ = build_unary(IMAG, $2, $3);
674 }
675 | T_EXTENSION cast_expression { /* GCC c_parser_unary_expression */
676 $$ = $2;
677 }
678 | T_SIZEOF unary_expression {
679 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
680 if ($$ != NULL)
681 check_expr_misc($2,
682 false, false, false, false, false, true);
683 }
684 | T_SIZEOF T_LPAREN type_name T_RPAREN {
685 $$ = build_sizeof($3);
686 }
687 | T_ALIGNOF unary_expression {
688 /* non type argument to alignof is a GCC extension */
689 gnuism(349);
690 lint_assert($2 != NULL);
691 $$ = build_alignof($2->tn_type);
692 }
693 /* K&R ---, C90 ---, C99 ---, C11 6.5.3 */
694 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
695 /* TODO: c11ism */
696 $$ = build_alignof($3);
697 }
698 ;
699
700 /* The rule 'unary_operator' is inlined into unary_expression. */
701
702 /* K&R 7.2, C90 ???, C99 6.5.4, C11 6.5.4 */
703 cast_expression:
704 unary_expression
705 | T_LPAREN type_name T_RPAREN cast_expression {
706 $$ = cast($4, $2);
707 }
708 ;
709
710 expression_opt:
711 /* empty */ {
712 $$ = NULL;
713 }
714 | expression
715 ;
716
717 /* 'conditional_expression' also implements 'multiplicative_expression'. */
718 /* 'conditional_expression' also implements 'additive_expression'. */
719 /* 'conditional_expression' also implements 'shift_expression'. */
720 /* 'conditional_expression' also implements 'relational_expression'. */
721 /* 'conditional_expression' also implements 'equality_expression'. */
722 /* 'conditional_expression' also implements 'AND_expression'. */
723 /* 'conditional_expression' also implements 'exclusive_OR_expression'. */
724 /* 'conditional_expression' also implements 'inclusive_OR_expression'. */
725 /* 'conditional_expression' also implements 'logical_AND_expression'. */
726 /* 'conditional_expression' also implements 'logical_OR_expression'. */
727 /* K&R ???, C90 ???, C99 6.5.5 to 6.5.15, C11 6.5.5 to 6.5.15 */
728 conditional_expression:
729 cast_expression
730 | conditional_expression T_ASTERISK sys conditional_expression {
731 $$ = build_binary($1, MULT, $3, $4);
732 }
733 | conditional_expression T_MULTIPLICATIVE sys conditional_expression {
734 $$ = build_binary($1, $2, $3, $4);
735 }
736 | conditional_expression T_ADDITIVE sys conditional_expression {
737 $$ = build_binary($1, $2, $3, $4);
738 }
739 | conditional_expression T_SHIFT sys conditional_expression {
740 $$ = build_binary($1, $2, $3, $4);
741 }
742 | conditional_expression T_RELATIONAL sys conditional_expression {
743 $$ = build_binary($1, $2, $3, $4);
744 }
745 | conditional_expression T_EQUALITY sys conditional_expression {
746 $$ = build_binary($1, $2, $3, $4);
747 }
748 | conditional_expression T_AMPER sys conditional_expression {
749 $$ = build_binary($1, BITAND, $3, $4);
750 }
751 | conditional_expression T_BITXOR sys conditional_expression {
752 $$ = build_binary($1, BITXOR, $3, $4);
753 }
754 | conditional_expression T_BITOR sys conditional_expression {
755 $$ = build_binary($1, BITOR, $3, $4);
756 }
757 | conditional_expression T_LOGAND sys conditional_expression {
758 $$ = build_binary($1, LOGAND, $3, $4);
759 }
760 | conditional_expression T_LOGOR sys conditional_expression {
761 $$ = build_binary($1, LOGOR, $3, $4);
762 }
763 | conditional_expression T_QUEST sys
764 expression T_COLON sys conditional_expression {
765 $$ = build_binary($1, QUEST, $3,
766 build_binary($4, COLON, $6, $7));
767 }
768 ;
769
770 /* K&R ???, C90 ???, C99 6.5.16, C11 6.5.16 */
771 assignment_expression:
772 conditional_expression
773 | unary_expression T_ASSIGN sys assignment_expression {
774 $$ = build_binary($1, ASSIGN, $3, $4);
775 }
776 | unary_expression T_OPASSIGN sys assignment_expression {
777 $$ = build_binary($1, $2, $3, $4);
778 }
779 ;
780
781 /* K&R ???, C90 ???, C99 6.5.17, C11 6.5.17 */
782 expression:
783 assignment_expression
784 | expression T_COMMA sys assignment_expression {
785 $$ = build_binary($1, COMMA, $3, $4);
786 }
787 ;
788
789 /* K&R ???, C90 ???, C99 6.6, C11 ???, C23 6.6 */
790 constant_expression:
791 conditional_expression
792 ;
793
794 declaration_or_error:
795 declaration
796 | error T_SEMI
797 ;
798
799 /* K&R ???, C90 ???, C99 6.7, C11 ???, C23 6.7 */
800 declaration:
801 begin_type_declmods end_type T_SEMI {
802 if (dcs->d_scl == TYPEDEF) {
803 /* typedef declares no type name */
804 warning(72);
805 } else {
806 /* empty declaration */
807 warning(2);
808 }
809 }
810 | begin_type_declmods end_type notype_init_declarators T_SEMI {
811 if (dcs->d_scl == TYPEDEF) {
812 /* syntax error '%s' */
813 error(249, "missing base type for typedef");
814 } else {
815 /* old-style declaration; add 'int' */
816 error(1);
817 }
818 }
819 | begin_type_declaration_specifiers end_type T_SEMI {
820 if (dcs->d_scl == TYPEDEF) {
821 /* typedef declares no type name */
822 warning(72);
823 } else if (!dcs->d_nonempty_decl) {
824 /* empty declaration */
825 warning(2);
826 }
827 }
828 | begin_type_declaration_specifiers end_type
829 type_init_declarators T_SEMI
830 | static_assert_declaration
831 ;
832
833 begin_type_declaration_specifiers: /* see C99 6.7 */
834 begin_type_typespec {
835 dcs_add_type($1);
836 }
837 | begin_type_declmods type_specifier {
838 dcs_add_type($2);
839 }
840 | type_attribute begin_type_declaration_specifiers
841 | begin_type_declaration_specifiers declmod
842 | begin_type_declaration_specifiers notype_type_specifier {
843 dcs_add_type($2);
844 }
845 ;
846
847 begin_type_declmods: /* see C99 6.7 */
848 begin_type type_qualifier {
849 dcs_add_qualifiers($2);
850 }
851 | begin_type T_SCLASS {
852 dcs_add_storage_class($2);
853 }
854 | begin_type T_FUNCTION_SPECIFIER {
855 dcs_add_function_specifier($2);
856 }
857 | begin_type_declmods declmod
858 ;
859
860 begin_type_specifier_qualifier_list: /* see C11 6.7.2.1 */
861 begin_type_specifier_qualifier_list_postfix
862 | type_attribute_list begin_type_specifier_qualifier_list_postfix
863 ;
864
865 begin_type_specifier_qualifier_list_postfix:
866 begin_type_typespec {
867 dcs_add_type($1);
868 }
869 | begin_type_qualifier_list type_specifier {
870 dcs_add_type($2);
871 }
872 | begin_type_specifier_qualifier_list_postfix type_qualifier {
873 dcs_add_qualifiers($2);
874 }
875 | begin_type_specifier_qualifier_list_postfix notype_type_specifier {
876 dcs_add_type($2);
877 }
878 | begin_type_specifier_qualifier_list_postfix type_attribute
879 ;
880
881 begin_type_typespec:
882 begin_type notype_type_specifier {
883 $$ = $2;
884 }
885 | begin_type T_TYPENAME {
886 $$ = getsym($2)->s_type;
887 }
888 ;
889
890 begin_type_qualifier_list:
891 begin_type type_qualifier {
892 dcs_add_qualifiers($2);
893 }
894 | begin_type_qualifier_list type_qualifier {
895 dcs_add_qualifiers($2);
896 }
897 ;
898
899 declmod:
900 type_qualifier {
901 dcs_add_qualifiers($1);
902 }
903 | T_SCLASS {
904 dcs_add_storage_class($1);
905 }
906 | T_FUNCTION_SPECIFIER {
907 dcs_add_function_specifier($1);
908 }
909 | type_attribute_list
910 ;
911
912 type_attribute_list_opt:
913 /* empty */
914 | type_attribute_list
915 ;
916
917 type_attribute_list:
918 type_attribute
919 | type_attribute_list type_attribute
920 ;
921
922 type_attribute_opt:
923 /* empty */
924 | type_attribute
925 ;
926
927 type_attribute: /* See C11 6.7 declaration-specifiers */
928 gcc_attribute_specifier
929 | T_ALIGNAS T_LPAREN type_specifier T_RPAREN /* C11 6.7.5 */
930 | T_ALIGNAS T_LPAREN constant_expression T_RPAREN /* C11 6.7.5 */
931 | T_PACKED {
932 dcs_add_packed();
933 }
934 ;
935
936 begin_type:
937 /* empty */ {
938 dcs_begin_type();
939 }
940 ;
941
942 end_type:
943 /* empty */ {
944 dcs_end_type();
945 }
946 ;
947
948 type_specifier: /* C99 6.7.2 */
949 notype_type_specifier
950 | T_TYPENAME {
951 $$ = getsym($1)->s_type;
952 }
953 ;
954
955 notype_type_specifier: /* see C99 6.7.2 */
956 T_TYPE {
957 $$ = gettyp($1);
958 }
959 | T_TYPEOF T_LPAREN expression T_RPAREN { /* GCC extension */
960 $$ = $3 != NULL ? block_dup_type($3->tn_type) : gettyp(INT);
961 $$->t_typeof = true;
962 }
963 | atomic_type_specifier
964 | struct_or_union_specifier {
965 end_declaration_level();
966 $$ = $1;
967 }
968 | enum_specifier {
969 end_declaration_level();
970 $$ = $1;
971 }
972 ;
973
974 /* K&R ---, C90 ---, C99 ---, C11 6.7.2.4 */
975 atomic_type_specifier:
976 atomic T_LPAREN type_name T_RPAREN {
977 $$ = $3;
978 }
979 ;
980
981 /* K&R ---, C90 ---, C99 6.7.2.1, C11 ???, C23 6.7.2.1 */
982 struct_or_union_specifier:
983 struct_or_union identifier_sym {
984 /*
985 * STDC requires that "struct a;" always introduces
986 * a new tag if "a" is not declared at current level
987 *
988 * yychar is valid because otherwise the parser would not
989 * have been able to decide if it must shift or reduce
990 */
991 $$ = make_tag_type($2, $1, false, yychar == T_SEMI);
992 }
993 | struct_or_union identifier_sym {
994 dcs->d_tag_type = make_tag_type($2, $1, true, false);
995 } braced_member_declaration_list {
996 $$ = complete_struct_or_union($4);
997 }
998 | struct_or_union {
999 dcs->d_tag_type = make_tag_type(NULL, $1, true, false);
1000 } braced_member_declaration_list {
1001 $$ = complete_struct_or_union($3);
1002 }
1003 | struct_or_union error {
1004 set_symtyp(FVFT);
1005 $$ = gettyp(INT);
1006 }
1007 ;
1008
1009 /* K&R ---, C90 ---, C99 6.7.2.1, C11 ???, C23 6.7.2.1 */
1010 struct_or_union:
1011 T_STRUCT_OR_UNION {
1012 set_symtyp(FTAG);
1013 begin_declaration_level($1 == STRUCT ? DLK_STRUCT : DLK_UNION);
1014 dcs->d_sou_size_in_bits = 0;
1015 dcs->d_sou_align_in_bits = CHAR_SIZE;
1016 $$ = $1;
1017 }
1018 | struct_or_union type_attribute
1019 ;
1020
1021 braced_member_declaration_list: /* see C99 6.7.2.1 */
1022 member_declaration_lbrace member_declaration_list_with_rbrace {
1023 $$ = $2;
1024 }
1025 ;
1026
1027 member_declaration_lbrace: /* see C99 6.7.2.1 */
1028 T_LBRACE {
1029 set_symtyp(FVFT);
1030 }
1031 ;
1032
1033 member_declaration_list_with_rbrace: /* see C99 6.7.2.1 */
1034 member_declaration_list T_RBRACE
1035 | T_RBRACE {
1036 /* XXX: This is not allowed by any C standard. */
1037 $$ = NULL;
1038 }
1039 ;
1040
1041 /* K&R ???, C90 ???, C99 6.7.2.1, C11 6.7.2.1, C23 6.7.2.1 */
1042 /* Was named struct_declaration_list until C11. */
1043 member_declaration_list:
1044 member_declaration
1045 | member_declaration_list member_declaration {
1046 $$ = concat_symbols($1, $2);
1047 }
1048 ;
1049
1050 /* Was named struct_declaration until C11. */
1051 /* K&R ???, C90 ???, C99 6.7.2.1, C11 6.7.2.1, C23 6.7.2.1 */
1052 member_declaration:
1053 begin_type_qualifier_list end_type {
1054 /* ^^ There is no check for the missing type-specifier. */
1055 /* too late, i know, but getsym() compensates it */
1056 set_symtyp(FMEMBER);
1057 } notype_member_declarators T_SEMI {
1058 set_symtyp(FVFT);
1059 $$ = $4;
1060 }
1061 | begin_type_specifier_qualifier_list end_type {
1062 set_symtyp(FMEMBER);
1063 } type_member_declarators T_SEMI {
1064 set_symtyp(FVFT);
1065 $$ = $4;
1066 }
1067 | begin_type_qualifier_list end_type type_attribute_opt T_SEMI {
1068 /* syntax error '%s' */
1069 error(249, "member without type");
1070 $$ = NULL;
1071 }
1072 | begin_type_specifier_qualifier_list end_type type_attribute_opt
1073 T_SEMI {
1074 set_symtyp(FVFT);
1075 if (!allow_c11 && !allow_gcc)
1076 /* anonymous struct/union members is a C11 feature */
1077 warning(49);
1078 if (is_struct_or_union(dcs->d_type->t_tspec))
1079 $$ = declare_unnamed_member();
1080 else {
1081 /* syntax error '%s' */
1082 error(249, "unnamed member");
1083 $$ = NULL;
1084 }
1085 }
1086 | static_assert_declaration {
1087 $$ = NULL;
1088 }
1089 | error T_SEMI {
1090 set_symtyp(FVFT);
1091 $$ = NULL;
1092 }
1093 ;
1094
1095 /* Was named struct_declarators until C11. */
1096 notype_member_declarators:
1097 notype_member_declarator {
1098 $$ = declare_member($1);
1099 }
1100 | notype_member_declarators {
1101 set_symtyp(FMEMBER);
1102 } T_COMMA type_member_declarator {
1103 $$ = concat_symbols($1, declare_member($4));
1104 }
1105 ;
1106
1107 /* Was named struct_declarators until C11. */
1108 type_member_declarators:
1109 type_member_declarator {
1110 $$ = declare_member($1);
1111 }
1112 | type_member_declarators {
1113 set_symtyp(FMEMBER);
1114 } T_COMMA type_member_declarator {
1115 $$ = concat_symbols($1, declare_member($4));
1116 }
1117 ;
1118
1119 /* Was named struct_declarator until C11. */
1120 notype_member_declarator:
1121 notype_declarator
1122 /* C99 6.7.2.1 */
1123 | notype_declarator T_COLON constant_expression {
1124 $$ = set_bit_field_width($1, to_int_constant($3, true));
1125 }
1126 /* C99 6.7.2.1 */
1127 | {
1128 set_symtyp(FVFT);
1129 } T_COLON constant_expression {
1130 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1131 }
1132 ;
1133
1134 /* Was named struct_declarator until C11. */
1135 type_member_declarator:
1136 type_declarator
1137 | type_declarator T_COLON constant_expression type_attribute_list_opt {
1138 $$ = set_bit_field_width($1, to_int_constant($3, true));
1139 }
1140 | {
1141 set_symtyp(FVFT);
1142 } T_COLON constant_expression type_attribute_list_opt {
1143 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1144 }
1145 ;
1146
1147 /* K&R ---, C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2 */
1148 enum_specifier:
1149 enum gcc_attribute_specifier_list_opt identifier_sym {
1150 $$ = make_tag_type($3, ENUM, false, false);
1151 }
1152 | enum gcc_attribute_specifier_list_opt identifier_sym {
1153 dcs->d_tag_type = make_tag_type($3, ENUM, true, false);
1154 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1155 $$ = complete_enum($5);
1156 }
1157 | enum gcc_attribute_specifier_list_opt {
1158 dcs->d_tag_type = make_tag_type(NULL, ENUM, true, false);
1159 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1160 $$ = complete_enum($4);
1161 }
1162 | enum error {
1163 set_symtyp(FVFT);
1164 $$ = gettyp(INT);
1165 }
1166 ;
1167
1168 enum: /* helper for C99 6.7.2.2 */
1169 T_ENUM {
1170 set_symtyp(FTAG);
1171 begin_declaration_level(DLK_ENUM);
1172 }
1173 ;
1174
1175 enum_declaration: /* helper for C99 6.7.2.2 */
1176 enum_decl_lbrace enums_with_opt_comma T_RBRACE {
1177 $$ = $2;
1178 }
1179 ;
1180
1181 enum_decl_lbrace: /* helper for C99 6.7.2.2 */
1182 T_LBRACE {
1183 set_symtyp(FVFT);
1184 enumval = 0;
1185 }
1186 ;
1187
1188 enums_with_opt_comma: /* helper for C99 6.7.2.2 */
1189 enumerator_list
1190 | enumerator_list T_COMMA {
1191 if (!allow_c99 && !allow_trad) {
1192 /* trailing ',' in enum declaration requires C99 ... */
1193 error(54);
1194 } else {
1195 /* trailing ',' in enum declaration requires C99 ... */
1196 c99ism(54);
1197 }
1198 $$ = $1;
1199 }
1200 ;
1201
1202 enumerator_list: /* C99 6.7.2.2 */
1203 enumerator
1204 | enumerator_list T_COMMA enumerator {
1205 $$ = concat_symbols($1, $3);
1206 }
1207 | error {
1208 $$ = NULL;
1209 }
1210 ;
1211
1212 enumerator: /* C99 6.7.2.2 */
1213 identifier_sym gcc_attribute_specifier_list_opt {
1214 $$ = enumeration_constant($1, enumval, true);
1215 }
1216 | identifier_sym gcc_attribute_specifier_list_opt
1217 T_ASSIGN constant_expression {
1218 $$ = enumeration_constant($1, to_int_constant($4, true),
1219 false);
1220 }
1221 ;
1222
1223 type_qualifier: /* C99 6.7.3 */
1224 T_QUAL
1225 | atomic {
1226 $$ = (type_qualifiers){ .tq_atomic = true };
1227 }
1228 ;
1229
1230 atomic: /* helper */
1231 T_ATOMIC {
1232 /* TODO: First fix c11ism, then use it here. */
1233 if (!allow_c11)
1234 /* '_Atomic' requires C11 or later */
1235 error(350);
1236 }
1237 ;
1238
1239 pointer: /* C99 6.7.5 */
1240 T_ASTERISK type_qualifier_list_opt {
1241 $$ = xcalloc(1, sizeof(*$$));
1242 add_type_qualifiers(&$$->qualifiers, $2);
1243 }
1244 | T_ASTERISK type_qualifier_list_opt pointer {
1245 $$ = xcalloc(1, sizeof(*$$));
1246 add_type_qualifiers(&$$->qualifiers, $2);
1247 $$ = append_qualified_pointer($$, $3);
1248 }
1249 ;
1250
1251 type_qualifier_list_opt: /* see C99 6.7.5 */
1252 /* empty */ {
1253 $$ = (type_qualifiers){ .tq_const = false };
1254 }
1255 | type_qualifier_list
1256 ;
1257
1258 type_qualifier_list: /* C99 6.7.5 */
1259 type_qualifier
1260 | type_qualifier_list type_qualifier {
1261 $$ = $1;
1262 add_type_qualifiers(&$$, $2);
1263 }
1264 ;
1265
1266 /*
1267 * For an explanation of 'notype' in the following rules, see
1268 * https://www.gnu.org/software/bison/manual/bison.html#Semantic-Tokens.
1269 */
1270
1271 notype_init_declarators:
1272 notype_init_declarator
1273 | notype_init_declarators T_COMMA type_init_declarator
1274 ;
1275
1276 type_init_declarators:
1277 type_init_declarator
1278 | type_init_declarators T_COMMA type_init_declarator
1279 ;
1280
1281 notype_init_declarator:
1282 notype_declarator asm_or_symbolrename_opt {
1283 cgram_declare($1, false, $2);
1284 check_size($1);
1285 }
1286 | notype_declarator asm_or_symbolrename_opt {
1287 begin_initialization($1);
1288 cgram_declare($1, true, $2);
1289 } T_ASSIGN initializer {
1290 check_size($1);
1291 end_initialization();
1292 }
1293 ;
1294
1295 type_init_declarator:
1296 type_declarator asm_or_symbolrename_opt {
1297 cgram_declare($1, false, $2);
1298 check_size($1);
1299 }
1300 | type_declarator asm_or_symbolrename_opt {
1301 begin_initialization($1);
1302 cgram_declare($1, true, $2);
1303 } T_ASSIGN initializer {
1304 check_size($1);
1305 end_initialization();
1306 }
1307 ;
1308
1309 notype_declarator:
1310 notype_direct_declarator
1311 | pointer notype_direct_declarator {
1312 $$ = add_pointer($2, $1);
1313 }
1314 ;
1315
1316 type_declarator:
1317 type_direct_declarator
1318 | pointer type_direct_declarator {
1319 $$ = add_pointer($2, $1);
1320 }
1321 ;
1322
1323 notype_direct_declarator:
1324 type_attribute_list_opt T_NAME {
1325 $$ = declarator_name(getsym($2));
1326 }
1327 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1328 $$ = $3;
1329 }
1330 | notype_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1331 $$ = add_array($1, $3.has_dim, $3.dim);
1332 }
1333 | notype_direct_declarator param_list asm_or_symbolrename_opt {
1334 $$ = add_function(symbolrename($1, $3), $2);
1335 end_declaration_level();
1336 block_level--;
1337 }
1338 | notype_direct_declarator type_attribute
1339 ;
1340
1341 type_direct_declarator:
1342 type_attribute_list_opt identifier {
1343 $$ = declarator_name(getsym($2));
1344 }
1345 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1346 $$ = $3;
1347 }
1348 | type_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1349 $$ = add_array($1, $3.has_dim, $3.dim);
1350 }
1351 | type_direct_declarator param_list asm_or_symbolrename_opt {
1352 $$ = add_function(symbolrename($1, $3), $2);
1353 end_declaration_level();
1354 block_level--;
1355 }
1356 | type_direct_declarator type_attribute
1357 ;
1358
1359 /*
1360 * The two distinct rules type_param_declarator and notype_param_declarator
1361 * avoid a conflict in parameter lists. A typename enclosed in parentheses is
1362 * always treated as a typename, not an argument name. For example, after
1363 * "typedef double a;", the declaration "f(int (a));" is interpreted as
1364 * "f(int (double));", not "f(int a);".
1365 */
1366 type_param_declarator:
1367 direct_param_declarator
1368 | pointer direct_param_declarator {
1369 $$ = add_pointer($2, $1);
1370 }
1371 ;
1372
1373 notype_param_declarator:
1374 direct_notype_param_declarator
1375 | pointer direct_notype_param_declarator {
1376 $$ = add_pointer($2, $1);
1377 }
1378 ;
1379
1380 direct_param_declarator:
1381 identifier type_attribute_list {
1382 $$ = declarator_name(getsym($1));
1383 }
1384 | identifier {
1385 $$ = declarator_name(getsym($1));
1386 }
1387 | T_LPAREN notype_param_declarator T_RPAREN {
1388 $$ = $2;
1389 }
1390 | direct_param_declarator T_LBRACK array_size_opt T_RBRACK
1391 gcc_attribute_specifier_list_opt {
1392 $$ = add_array($1, $3.has_dim, $3.dim);
1393 }
1394 | direct_param_declarator param_list asm_or_symbolrename_opt {
1395 $$ = add_function(symbolrename($1, $3), $2);
1396 end_declaration_level();
1397 block_level--;
1398 }
1399 ;
1400
1401 direct_notype_param_declarator:
1402 identifier {
1403 $$ = declarator_name(getsym($1));
1404 }
1405 | T_LPAREN notype_param_declarator T_RPAREN {
1406 $$ = $2;
1407 }
1408 | direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK {
1409 $$ = add_array($1, $3.has_dim, $3.dim);
1410 }
1411 | direct_notype_param_declarator param_list asm_or_symbolrename_opt {
1412 $$ = add_function(symbolrename($1, $3), $2);
1413 end_declaration_level();
1414 block_level--;
1415 }
1416 ;
1417
1418 param_list:
1419 id_list_lparen identifier_list T_RPAREN {
1420 $$ = (struct parameter_list){ .first = $2 };
1421 }
1422 | abstract_decl_param_list
1423 ;
1424
1425 id_list_lparen:
1426 T_LPAREN {
1427 block_level++;
1428 begin_declaration_level(DLK_PROTO_PARAMS);
1429 }
1430 ;
1431
1432 array_size_opt:
1433 /* empty */ {
1434 $$.has_dim = false;
1435 $$.dim = 0;
1436 }
1437 | T_ASTERISK {
1438 /* since C99; variable length array of unspecified size */
1439 $$.has_dim = false; /* TODO: maybe change to true */
1440 $$.dim = 0; /* just as a placeholder */
1441 }
1442 | array_size {
1443 $$.has_dim = true;
1444 $$.dim = $1 == NULL ? 0 : to_int_constant($1, false);
1445 }
1446 ;
1447
1448 array_size:
1449 type_qualifier_list_opt T_SCLASS constant_expression {
1450 /* C11 6.7.6.3p7 */
1451 if ($2 != STATIC)
1452 yyerror("Bad attribute");
1453 /* static array size requires C11 or later */
1454 c11ism(343);
1455 $$ = $3;
1456 }
1457 | type_qualifier {
1458 /* C11 6.7.6.2 */
1459 if (!$1.tq_restrict)
1460 yyerror("Bad attribute");
1461 $$ = NULL;
1462 }
1463 | constant_expression
1464 ;
1465
1466 identifier_list: /* C99 6.7.5 */
1467 T_NAME {
1468 $$ = old_style_function_parameter_name(getsym($1));
1469 }
1470 | identifier_list T_COMMA T_NAME {
1471 $$ = concat_symbols($1,
1472 old_style_function_parameter_name(getsym($3)));
1473 }
1474 | identifier_list error
1475 ;
1476
1477 /* XXX: C99 requires an additional specifier-qualifier-list. */
1478 type_name: /* C99 6.7.6 */
1479 {
1480 begin_declaration_level(DLK_ABSTRACT);
1481 } abstract_declaration {
1482 end_declaration_level();
1483 $$ = $2->s_type;
1484 }
1485 ;
1486
1487 abstract_declaration: /* specific to lint */
1488 begin_type_qualifier_list end_type {
1489 $$ = declare_abstract_type(abstract_name());
1490 }
1491 | begin_type_specifier_qualifier_list end_type {
1492 $$ = declare_abstract_type(abstract_name());
1493 }
1494 | begin_type_qualifier_list end_type abstract_declarator {
1495 $$ = declare_abstract_type($3);
1496 }
1497 | begin_type_specifier_qualifier_list end_type abstract_declarator {
1498 $$ = declare_abstract_type($3);
1499 }
1500 ;
1501
1502 /* K&R 8.7, C90 ???, C99 6.7.6, C11 6.7.7 */
1503 /* In K&R, abstract-declarator could be empty and was still simpler. */
1504 abstract_declarator:
1505 pointer {
1506 $$ = add_pointer(abstract_name(), $1);
1507 }
1508 | direct_abstract_declarator
1509 | pointer direct_abstract_declarator {
1510 $$ = add_pointer($2, $1);
1511 }
1512 | type_attribute_list direct_abstract_declarator {
1513 $$ = $2;
1514 }
1515 | pointer type_attribute_list direct_abstract_declarator {
1516 $$ = add_pointer($3, $1);
1517 }
1518 ;
1519
1520 /* K&R ---, C90 ???, C99 6.7.6, C11 6.7.7 */
1521 direct_abstract_declarator:
1522 /* TODO: sort rules according to C99 */
1523 T_LPAREN abstract_declarator T_RPAREN {
1524 $$ = $2;
1525 }
1526 | T_LBRACK array_size_opt T_RBRACK {
1527 $$ = add_array(abstract_name(), $2.has_dim, $2.dim);
1528 }
1529 | direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK {
1530 $$ = add_array($1, $3.has_dim, $3.dim);
1531 }
1532 | abstract_decl_param_list asm_or_symbolrename_opt {
1533 sym_t *name = abstract_enclosing_name();
1534 $$ = add_function(symbolrename(name, $2), $1);
1535 end_declaration_level();
1536 block_level--;
1537 }
1538 | direct_abstract_declarator abstract_decl_param_list
1539 asm_or_symbolrename_opt {
1540 $$ = add_function(symbolrename($1, $3), $2);
1541 end_declaration_level();
1542 block_level--;
1543 }
1544 | direct_abstract_declarator type_attribute_list
1545 ;
1546
1547 abstract_decl_param_list: /* specific to lint */
1548 abstract_decl_lparen T_RPAREN type_attribute_opt {
1549 $$ = (struct parameter_list){ .first = NULL };
1550 }
1551 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN
1552 type_attribute_opt {
1553 $$ = $2;
1554 $$.prototype = true;
1555 }
1556 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1557 $$ = (struct parameter_list){ .first = NULL };
1558 }
1559 ;
1560
1561 abstract_decl_lparen: /* specific to lint */
1562 T_LPAREN {
1563 block_level++;
1564 begin_declaration_level(DLK_PROTO_PARAMS);
1565 }
1566 ;
1567
1568 vararg_parameter_type_list: /* specific to lint */
1569 parameter_type_list
1570 | parameter_type_list T_COMMA T_ELLIPSIS {
1571 $$ = $1;
1572 $$.vararg = true;
1573 }
1574 | T_ELLIPSIS {
1575 /* TODO: C99 6.7.5 makes this an error as well. */
1576 if (!allow_trad && !allow_c99) {
1577 /* C90 to C17 require formal parameter before '...' */
1578 error(84);
1579 } else if (allow_c90) {
1580 /* C90 to C17 require formal parameter before '...' */
1581 warning(84);
1582 }
1583 $$ = (struct parameter_list){ .vararg = true };
1584 }
1585 ;
1586
1587 /* XXX: C99 6.7.5 defines the same name, but it looks different. */
1588 parameter_type_list:
1589 parameter_declaration {
1590 $$ = (struct parameter_list){ .first = $1 };
1591 }
1592 | parameter_type_list T_COMMA parameter_declaration {
1593 $$ = $1;
1594 $$.first = concat_symbols($1.first, $3);
1595 }
1596 ;
1597
1598 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1599 parameter_declaration:
1600 begin_type_declmods end_type {
1601 /* ^^ There is no check for the missing type-specifier. */
1602 $$ = declare_parameter(abstract_name(), false);
1603 }
1604 | begin_type_declaration_specifiers end_type {
1605 $$ = declare_parameter(abstract_name(), false);
1606 }
1607 | begin_type_declmods end_type notype_param_declarator {
1608 /* ^^ There is no check for the missing type-specifier. */
1609 $$ = declare_parameter($3, false);
1610 }
1611 /*
1612 * type_param_declarator is needed because of following conflict:
1613 * "typedef int a; f(int (a));" could be parsed as
1614 * "function with argument a of type int", or
1615 * "function with an unnamed (abstract) argument of type function".
1616 * This grammar realizes the second case.
1617 */
1618 | begin_type_declaration_specifiers end_type type_param_declarator {
1619 $$ = declare_parameter($3, false);
1620 }
1621 | begin_type_declmods end_type abstract_declarator {
1622 /* ^^ There is no check for the missing type-specifier. */
1623 $$ = declare_parameter($3, false);
1624 }
1625 | begin_type_declaration_specifiers end_type abstract_declarator {
1626 $$ = declare_parameter($3, false);
1627 }
1628 ;
1629
1630 braced_initializer:
1631 /* K&R ---, C90 ---, C99 ---, C11 ---, C23 6.7.10 */
1632 init_lbrace init_rbrace {
1633 /* empty initializer braces require C23 or later */
1634 c23ism(353);
1635 }
1636 /* K&R ---, C90 ---, C99 6.7.8, C11 6.7.9, C23 6.7.10 */
1637 | init_lbrace initializer_list comma_opt init_rbrace
1638 ;
1639
1640 initializer: /* C99 6.7.8 "Initialization" */
1641 assignment_expression {
1642 init_expr($1);
1643 }
1644 | init_lbrace init_rbrace {
1645 /* XXX: Empty braces are not covered by C99 6.7.8. */
1646 }
1647 | init_lbrace initializer_list comma_opt init_rbrace
1648 /* XXX: What is this error handling for? */
1649 | error
1650 ;
1651
1652 initializer_list: /* C99 6.7.8 "Initialization" */
1653 initializer_list_item
1654 | initializer_list T_COMMA initializer_list_item
1655 ;
1656
1657 initializer_list_item: /* helper */
1658 designation initializer
1659 | initializer
1660 ;
1661
1662 designation: /* C99 6.7.8 "Initialization" */
1663 begin_designation designator_list T_ASSIGN
1664 | identifier T_COLON {
1665 /* GCC style struct or union member name in initializer */
1666 gnuism(315);
1667 begin_designation();
1668 add_designator_member($1);
1669 }
1670 ;
1671
1672 begin_designation: /* lint-specific helper */
1673 /* empty */ {
1674 begin_designation();
1675 }
1676 ;
1677
1678 designator_list: /* C99 6.7.8 "Initialization" */
1679 designator
1680 | designator_list designator
1681 ;
1682
1683 designator: /* C99 6.7.8 "Initialization" */
1684 T_LBRACK range T_RBRACK {
1685 if (!allow_c99)
1686 /* array initializer with designators is a C99 ... */
1687 warning(321);
1688 add_designator_subscript($2);
1689 }
1690 | T_POINT identifier {
1691 if (!allow_c99)
1692 /* struct or union member name in initializer is ... */
1693 warning(313);
1694 add_designator_member($2);
1695 }
1696 ;
1697
1698 static_assert_declaration:
1699 T_STATIC_ASSERT T_LPAREN constant_expression T_COMMA T_STRING
1700 T_RPAREN T_SEMI {
1701 /* '_Static_assert' requires C11 or later */
1702 c11ism(354);
1703 }
1704 | T_STATIC_ASSERT T_LPAREN constant_expression T_RPAREN T_SEMI {
1705 /* '_Static_assert' without message requires C23 or later */
1706 c23ism(355);
1707 }
1708 ;
1709
1710 range:
1711 constant_expression {
1712 $$.lo = to_int_constant($1, true);
1713 $$.hi = $$.lo;
1714 }
1715 | constant_expression T_ELLIPSIS constant_expression {
1716 $$.lo = to_int_constant($1, true);
1717 $$.hi = to_int_constant($3, true);
1718 /* initialization with '[a...b]' is a GCC extension */
1719 gnuism(340);
1720 }
1721 ;
1722
1723 init_lbrace: /* helper */
1724 T_LBRACE {
1725 init_lbrace();
1726 }
1727 ;
1728
1729 init_rbrace: /* helper */
1730 T_RBRACE {
1731 init_rbrace();
1732 }
1733 ;
1734
1735 asm_or_symbolrename_opt: /* GCC extensions */
1736 /* empty */ {
1737 $$ = NULL;
1738 }
1739 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt {
1740 freeyyv(&$3, T_STRING);
1741 $$ = NULL;
1742 }
1743 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN
1744 gcc_attribute_specifier_list_opt {
1745 $$ = $3;
1746 }
1747 ;
1748
1749 /* K&R ???, C90 ???, C99 6.8, C11 ???, C23 6.8 */
1750 statement:
1751 expression_statement
1752 | non_expr_statement
1753 ;
1754
1755 non_expr_statement: /* helper for C99 6.8 */
1756 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI
1757 | labeled_statement
1758 | compound_statement
1759 | selection_statement
1760 | iteration_statement
1761 | jump_statement {
1762 suppress_fallthrough = false;
1763 }
1764 | asm_statement
1765 ;
1766
1767 labeled_statement: /* C99 6.8.1 */
1768 label gcc_attribute_specifier_list_opt statement
1769 ;
1770
1771 label:
1772 T_NAME T_COLON {
1773 set_symtyp(FLABEL);
1774 named_label(getsym($1));
1775 }
1776 | T_CASE constant_expression T_COLON {
1777 case_label($2);
1778 suppress_fallthrough = true;
1779 }
1780 | T_CASE constant_expression T_ELLIPSIS constant_expression T_COLON {
1781 /* XXX: We don't fill all cases */
1782 case_label($2);
1783 suppress_fallthrough = true;
1784 }
1785 | T_DEFAULT T_COLON {
1786 default_label();
1787 suppress_fallthrough = true;
1788 }
1789 ;
1790
1791 compound_statement: /* C99 6.8.2 */
1792 compound_statement_lbrace compound_statement_rbrace
1793 | compound_statement_lbrace block_item_list compound_statement_rbrace
1794 ;
1795
1796 compound_statement_lbrace:
1797 T_LBRACE {
1798 block_level++;
1799 mem_block_level++;
1800 debug_step("%s: mem_block_level = %zu",
1801 "compound_statement_lbrace", mem_block_level);
1802 begin_declaration_level(DLK_AUTO);
1803 }
1804 ;
1805
1806 compound_statement_rbrace:
1807 T_RBRACE {
1808 end_declaration_level();
1809 if (!in_statement_expr())
1810 level_free_all(mem_block_level); /* leak */
1811 mem_block_level--;
1812 debug_step("%s: mem_block_level = %zu",
1813 "compound_statement_rbrace", mem_block_level);
1814 block_level--;
1815 suppress_fallthrough = false;
1816 }
1817 ;
1818
1819 block_item_list: /* C99 6.8.2 */
1820 block_item
1821 | block_item_list block_item {
1822 if ($1 && !$2)
1823 /* declarations after statements is a C99 feature */
1824 c99ism(327);
1825 $$ = $1 || $2;
1826 }
1827 ;
1828
1829 block_item: /* C99 6.8.2 */
1830 declaration_or_error {
1831 $$ = false;
1832 restore_warning_flags();
1833 }
1834 | statement {
1835 $$ = true;
1836 restore_warning_flags();
1837 }
1838 ;
1839
1840 expression_statement: /* C99 6.8.3 */
1841 expression T_SEMI {
1842 expr($1, false, false, false, false);
1843 suppress_fallthrough = false;
1844 }
1845 | T_SEMI {
1846 check_statement_reachable();
1847 suppress_fallthrough = false;
1848 }
1849 ;
1850
1851 selection_statement: /* C99 6.8.4 */
1852 if_without_else %prec T_THEN {
1853 save_warning_flags();
1854 stmt_if_then_stmt();
1855 stmt_if_else_stmt(false);
1856 }
1857 | if_without_else T_ELSE {
1858 save_warning_flags();
1859 stmt_if_then_stmt();
1860 } statement {
1861 clear_warning_flags();
1862 stmt_if_else_stmt(true);
1863 }
1864 | if_without_else T_ELSE error {
1865 clear_warning_flags();
1866 stmt_if_else_stmt(false);
1867 }
1868 | switch_expr statement {
1869 clear_warning_flags();
1870 stmt_switch_expr_stmt();
1871 }
1872 | switch_expr error {
1873 clear_warning_flags();
1874 stmt_switch_expr_stmt();
1875 }
1876 ;
1877
1878 if_without_else: /* see C99 6.8.4 */
1879 if_expr statement
1880 | if_expr error
1881 ;
1882
1883 if_expr: /* see C99 6.8.4 */
1884 T_IF T_LPAREN expression T_RPAREN {
1885 stmt_if_expr($3);
1886 clear_warning_flags();
1887 }
1888 ;
1889
1890 switch_expr: /* see C99 6.8.4 */
1891 T_SWITCH T_LPAREN expression T_RPAREN {
1892 stmt_switch_expr($3);
1893 clear_warning_flags();
1894 }
1895 ;
1896
1897 iteration_statement: /* C99 6.8.5 */
1898 while_expr statement {
1899 clear_warning_flags();
1900 stmt_while_expr_stmt();
1901 }
1902 | while_expr error {
1903 clear_warning_flags();
1904 stmt_while_expr_stmt();
1905 }
1906 | do_statement do_while_expr {
1907 stmt_do_while_expr($2);
1908 suppress_fallthrough = false;
1909 }
1910 | do error {
1911 clear_warning_flags();
1912 stmt_do_while_expr(NULL);
1913 }
1914 | for_exprs statement {
1915 clear_warning_flags();
1916 stmt_for_exprs_stmt();
1917 end_declaration_level();
1918 block_level--;
1919 }
1920 | for_exprs error {
1921 clear_warning_flags();
1922 stmt_for_exprs_stmt();
1923 end_declaration_level();
1924 block_level--;
1925 }
1926 ;
1927
1928 while_expr: /* see C99 6.8.5 */
1929 T_WHILE T_LPAREN expression T_RPAREN {
1930 stmt_while_expr($3);
1931 clear_warning_flags();
1932 }
1933 ;
1934
1935 do_statement: /* see C99 6.8.5 */
1936 do statement {
1937 clear_warning_flags();
1938 }
1939 ;
1940
1941 do: /* see C99 6.8.5 */
1942 T_DO {
1943 stmt_do();
1944 }
1945 ;
1946
1947 do_while_expr: /* see C99 6.8.5 */
1948 T_WHILE T_LPAREN expression T_RPAREN T_SEMI {
1949 $$ = $3;
1950 }
1951 ;
1952
1953 for_start: /* see C99 6.8.5 */
1954 T_FOR T_LPAREN {
1955 begin_declaration_level(DLK_AUTO);
1956 block_level++;
1957 }
1958 ;
1959
1960 for_exprs: /* see C99 6.8.5 */
1961 for_start
1962 begin_type_declaration_specifiers end_type
1963 notype_init_declarators T_SEMI
1964 expression_opt T_SEMI
1965 expression_opt T_RPAREN {
1966 /* variable declaration in for loop */
1967 c99ism(325);
1968 stmt_for_exprs(NULL, $6, $8);
1969 clear_warning_flags();
1970 }
1971 | for_start
1972 expression_opt T_SEMI
1973 expression_opt T_SEMI
1974 expression_opt T_RPAREN {
1975 stmt_for_exprs($2, $4, $6);
1976 clear_warning_flags();
1977 }
1978 ;
1979
1980 jump_statement: /* C99 6.8.6 */
1981 goto identifier T_SEMI {
1982 stmt_goto(getsym($2));
1983 }
1984 | goto error T_SEMI {
1985 set_symtyp(FVFT);
1986 }
1987 | T_CONTINUE T_SEMI {
1988 stmt_continue();
1989 }
1990 | T_BREAK T_SEMI {
1991 stmt_break();
1992 }
1993 | T_RETURN sys T_SEMI {
1994 stmt_return($2, NULL);
1995 }
1996 | T_RETURN sys expression T_SEMI {
1997 stmt_return($2, $3);
1998 }
1999 ;
2000
2001 goto: /* see C99 6.8.6 */
2002 T_GOTO {
2003 set_symtyp(FLABEL);
2004 }
2005 ;
2006
2007 asm_statement: /* GCC extension */
2008 T_ASM T_LPAREN read_until_rparen T_SEMI {
2009 dcs_set_asm();
2010 }
2011 | T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI {
2012 dcs_set_asm();
2013 }
2014 | T_ASM error
2015 ;
2016
2017 read_until_rparen: /* helper for 'asm_statement' */
2018 /* empty */ {
2019 read_until_rparen();
2020 }
2021 ;
2022
2023 translation_unit: /* C99 6.9 */
2024 external_declaration
2025 | translation_unit external_declaration
2026 ;
2027
2028 external_declaration: /* C99 6.9 */
2029 function_definition {
2030 global_clean_up_decl(false);
2031 clear_warning_flags();
2032 }
2033 | top_level_declaration {
2034 global_clean_up_decl(false);
2035 clear_warning_flags();
2036 }
2037 | asm_statement /* GCC extension */
2038 | T_SEMI { /* GCC extension */
2039 /*
2040 * TODO: Only allow this in GCC mode, not in plain C99.
2041 * This is one of the top 10 warnings in the NetBSD build.
2042 */
2043 if (!allow_trad && !allow_c99) {
2044 /* empty declaration */
2045 error(0);
2046 } else if (allow_c90) {
2047 /* empty declaration */
2048 warning(0);
2049 }
2050 }
2051 ;
2052
2053 /*
2054 * On the top level, lint allows several forms of declarations that it doesn't
2055 * allow in functions. For example, a single ';' is an empty declaration and
2056 * is supported by some compilers, but in a function it would be an empty
2057 * statement, not a declaration. This makes a difference in C90 mode, where
2058 * a statement must not be followed by a declaration.
2059 *
2060 * See 'declaration' for all other declarations.
2061 */
2062 top_level_declaration: /* C99 6.9 calls this 'declaration' */
2063 begin_type end_type notype_init_declarators T_SEMI {
2064 /* TODO: Make this an error in C99 mode as well. */
2065 if (!allow_trad && !allow_c99) {
2066 /* old-style declaration; add 'int' */
2067 error(1);
2068 } else if (allow_c90) {
2069 /* old-style declaration; add 'int' */
2070 warning(1);
2071 }
2072 }
2073 | declaration
2074 | error T_SEMI {
2075 global_clean_up();
2076 }
2077 | error T_RBRACE {
2078 global_clean_up();
2079 }
2080 ;
2081
2082 function_definition: /* C99 6.9.1 */
2083 func_declarator {
2084 if ($1->s_type->t_tspec != FUNC) {
2085 /* syntax error '%s' */
2086 error(249, yytext);
2087 YYERROR;
2088 }
2089 if ($1->s_type->t_typedef) {
2090 /* ()-less function definition */
2091 error(64);
2092 YYERROR;
2093 }
2094 check_extern_declaration($1);
2095 begin_function($1);
2096 block_level++;
2097 begin_declaration_level(DLK_OLD_STYLE_PARAMS);
2098 if (lwarn == LWARN_NONE)
2099 $1->s_used = true;
2100 } arg_declaration_list_opt {
2101 end_declaration_level();
2102 block_level--;
2103 check_func_lint_directives();
2104 check_func_old_style_parameters();
2105 begin_control_statement(CS_FUNCTION_BODY);
2106 } compound_statement {
2107 end_function();
2108 end_control_statement(CS_FUNCTION_BODY);
2109 }
2110 ;
2111
2112 func_declarator:
2113 begin_type end_type notype_declarator {
2114 if (!allow_trad) {
2115 /* old-style declaration; add 'int' */
2116 error(1);
2117 }
2118 $$ = $3;
2119 }
2120 | begin_type_declmods end_type notype_declarator {
2121 if (!allow_trad) {
2122 /* old-style declaration; add 'int' */
2123 error(1);
2124 }
2125 $$ = $3;
2126 }
2127 | begin_type_declaration_specifiers end_type type_declarator {
2128 $$ = $3;
2129 }
2130 ;
2131
2132 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
2133 /* empty */
2134 | arg_declaration_list
2135 ;
2136
2137 arg_declaration_list: /* C99 6.9.1p13 example 1 */
2138 arg_declaration
2139 | arg_declaration_list arg_declaration
2140 /* XXX or better "arg_declaration error" ? */
2141 | error
2142 ;
2143
2144 /*
2145 * "arg_declaration" is separated from "declaration" because it
2146 * needs other error handling.
2147 */
2148 arg_declaration:
2149 begin_type_declmods end_type T_SEMI {
2150 /* empty declaration */
2151 warning(2);
2152 }
2153 | begin_type_declmods end_type notype_init_declarators T_SEMI
2154 | begin_type_declaration_specifiers end_type T_SEMI {
2155 if (!dcs->d_nonempty_decl) {
2156 /* empty declaration */
2157 warning(2);
2158 } else {
2159 /* '%s' declared in parameter declaration list */
2160 warning(3, type_name(dcs->d_type));
2161 }
2162 }
2163 | begin_type_declaration_specifiers end_type
2164 type_init_declarators T_SEMI {
2165 if (dcs->d_nonempty_decl) {
2166 /* '%s' declared in parameter declaration list */
2167 warning(3, type_name(dcs->d_type));
2168 }
2169 }
2170 | begin_type_declmods error
2171 | begin_type_declaration_specifiers error
2172 ;
2173
2174 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
2175 gcc_attribute_specifier_list_opt:
2176 /* empty */
2177 | gcc_attribute_specifier_list
2178 ;
2179
2180 gcc_attribute_specifier_list:
2181 gcc_attribute_specifier
2182 | gcc_attribute_specifier_list gcc_attribute_specifier
2183 ;
2184
2185 gcc_attribute_specifier:
2186 T_ATTRIBUTE T_LPAREN T_LPAREN {
2187 in_gcc_attribute = true;
2188 } gcc_attribute_list {
2189 in_gcc_attribute = false;
2190 } T_RPAREN T_RPAREN
2191 ;
2192
2193 gcc_attribute_list:
2194 gcc_attribute
2195 | gcc_attribute_list T_COMMA gcc_attribute
2196 ;
2197
2198 gcc_attribute:
2199 /* empty */
2200 | T_NAME {
2201 const char *name = $1->sb_name;
2202 if (is_either(name, "packed", "__packed__"))
2203 dcs_add_packed();
2204 else if (is_either(name, "used", "__used__") ||
2205 is_either(name, "unused", "__unused__"))
2206 dcs_set_used();
2207 else if (is_either(name, "fallthrough", "__fallthrough__"))
2208 suppress_fallthrough = true;
2209 }
2210 | T_NAME T_LPAREN T_RPAREN
2211 | T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN
2212 | type_qualifier {
2213 if (!$1.tq_const)
2214 yyerror("Bad attribute");
2215 }
2216 ;
2217
2218 gcc_attribute_parameters:
2219 constant_expression
2220 | gcc_attribute_parameters T_COMMA constant_expression
2221 ;
2222
2223 sys:
2224 /* empty */ {
2225 $$ = in_system_header;
2226 }
2227 ;
2228
2229 %%
2230
2231 /* ARGSUSED */
2232 int
2233 yyerror(const char *msg)
2234 {
2235 /* syntax error '%s' */
2236 error(249, yytext);
2237 if (++sytxerr >= 5)
2238 norecover();
2239 return 0;
2240 }
2241
2242 #if YYDEBUG && YYBYACC
2243 static const char *
2244 cgram_to_string(int token, YYSTYPE val)
2245 {
2246
2247 switch (token) {
2248 case T_INCDEC:
2249 return val.y_inc ? "++" : "--";
2250 case T_MULTIPLICATIVE:
2251 case T_ADDITIVE:
2252 case T_SHIFT:
2253 case T_RELATIONAL:
2254 case T_EQUALITY:
2255 case T_OPASSIGN:
2256 return op_name(val.y_op);
2257 case T_SCLASS:
2258 return scl_name(val.y_scl);
2259 case T_TYPE:
2260 case T_STRUCT_OR_UNION:
2261 return tspec_name(val.y_tspec);
2262 case T_QUAL:
2263 return type_qualifiers_string(val.y_type_qualifiers);
2264 case T_FUNCTION_SPECIFIER:
2265 return function_specifier_name(val.y_function_specifier);
2266 case T_NAME:
2267 return val.y_name->sb_name;
2268 default:
2269 return "<none>";
2270 }
2271 }
2272 #endif
2273
2274 static void
2275 cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
2276 {
2277 declare(decl, has_initializer, renaming);
2278 if (renaming != NULL)
2279 freeyyv(&renaming, T_NAME);
2280 }
2281
2282 /*
2283 * Discard all input tokens up to and including the next unmatched right
2284 * parenthesis.
2285 */
2286 static void
2287 read_until_rparen(void)
2288 {
2289 int level;
2290
2291 if (yychar < 0)
2292 yychar = yylex();
2293 freeyyv(&yylval, yychar);
2294
2295 level = 1;
2296 while (yychar > 0) {
2297 if (yychar == T_LPAREN)
2298 level++;
2299 if (yychar == T_RPAREN && --level == 0)
2300 break;
2301 freeyyv(&yylval, yychar = yylex());
2302 }
2303
2304 yyclearin;
2305 }
2306
2307 static sym_t *
2308 symbolrename(sym_t *s, sbuf_t *sb)
2309 {
2310 if (sb != NULL)
2311 s->s_rename = sb->sb_name;
2312 return s;
2313 }
2314