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