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