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