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