cgram.y revision 1.452 1 %{
2 /* $NetBSD: cgram.y,v 1.452 2023/07/10 19:58:47 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.452 2023/07/10 19:58:47 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_expr
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_struct_declaration_list
322 /* No type for struct_declaration_lbrace. */
323 %type <y_sym> struct_declaration_list_with_rbrace
324 %type <y_sym> struct_declaration_list
325 %type <y_sym> struct_declaration
326 %type <y_sym> notype_struct_declarators
327 %type <y_sym> type_struct_declarators
328 %type <y_sym> notype_struct_declarator
329 %type <y_sym> type_struct_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 */
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 constant_expr: /* C99 6.6 */
786 conditional_expression
787 ;
788
789 declaration_or_error:
790 declaration
791 | error T_SEMI
792 ;
793
794 declaration: /* C99 6.7 */
795 begin_type_declmods end_type T_SEMI {
796 if (dcs->d_scl == TYPEDEF) {
797 /* typedef declares no type name */
798 warning(72);
799 } else {
800 /* empty declaration */
801 warning(2);
802 }
803 }
804 | begin_type_declmods end_type notype_init_declarators T_SEMI {
805 if (dcs->d_scl == TYPEDEF) {
806 /* syntax error '%s' */
807 error(249, "missing base type for typedef");
808 } else {
809 /* old-style declaration; add 'int' */
810 error(1);
811 }
812 }
813 | begin_type_declaration_specifiers end_type T_SEMI {
814 if (dcs->d_scl == TYPEDEF) {
815 /* typedef declares no type name */
816 warning(72);
817 } else if (!dcs->d_nonempty_decl) {
818 /* empty declaration */
819 warning(2);
820 }
821 }
822 | begin_type_declaration_specifiers end_type
823 type_init_declarators T_SEMI
824 | static_assert_declaration
825 ;
826
827 begin_type_declaration_specifiers: /* see C99 6.7 */
828 begin_type_typespec {
829 dcs_add_type($1);
830 }
831 | begin_type_declmods type_specifier {
832 dcs_add_type($2);
833 }
834 | type_attribute begin_type_declaration_specifiers
835 | begin_type_declaration_specifiers declmod
836 | begin_type_declaration_specifiers notype_type_specifier {
837 dcs_add_type($2);
838 }
839 ;
840
841 begin_type_declmods: /* see C99 6.7 */
842 begin_type type_qualifier {
843 dcs_add_qualifier($2);
844 }
845 | begin_type T_SCLASS {
846 dcs_add_storage_class($2);
847 }
848 | begin_type_declmods declmod
849 ;
850
851 begin_type_specifier_qualifier_list: /* see C11 6.7.2.1 */
852 begin_type_specifier_qualifier_list_postfix
853 | type_attribute_list begin_type_specifier_qualifier_list_postfix
854 ;
855
856 begin_type_specifier_qualifier_list_postfix:
857 begin_type_typespec {
858 dcs_add_type($1);
859 }
860 | begin_type_qualifier_list type_specifier {
861 dcs_add_type($2);
862 }
863 | begin_type_specifier_qualifier_list_postfix type_qualifier {
864 dcs_add_qualifier($2);
865 }
866 | begin_type_specifier_qualifier_list_postfix notype_type_specifier {
867 dcs_add_type($2);
868 }
869 | begin_type_specifier_qualifier_list_postfix type_attribute
870 ;
871
872 begin_type_typespec:
873 begin_type notype_type_specifier {
874 $$ = $2;
875 }
876 | begin_type T_TYPENAME {
877 $$ = getsym($2)->s_type;
878 }
879 ;
880
881 begin_type_qualifier_list:
882 begin_type type_qualifier {
883 dcs_add_qualifier($2);
884 }
885 | begin_type_qualifier_list type_qualifier {
886 dcs_add_qualifier($2);
887 }
888 ;
889
890 declmod:
891 type_qualifier {
892 dcs_add_qualifier($1);
893 }
894 | T_SCLASS {
895 dcs_add_storage_class($1);
896 }
897 | type_attribute_list
898 ;
899
900 type_attribute_list_opt:
901 /* empty */
902 | type_attribute_list
903 ;
904
905 type_attribute_list:
906 type_attribute
907 | type_attribute_list type_attribute
908 ;
909
910 type_attribute_opt:
911 /* empty */
912 | type_attribute
913 ;
914
915 type_attribute: /* See C11 6.7 declaration-specifiers */
916 gcc_attribute_specifier
917 | T_ALIGNAS T_LPAREN type_specifier T_RPAREN /* C11 6.7.5 */
918 | T_ALIGNAS T_LPAREN constant_expr T_RPAREN /* C11 6.7.5 */
919 | T_PACKED {
920 dcs_add_packed();
921 }
922 | T_NORETURN
923 ;
924
925 begin_type:
926 /* empty */ {
927 dcs_begin_type();
928 }
929 ;
930
931 end_type:
932 /* empty */ {
933 dcs_end_type();
934 }
935 ;
936
937 type_specifier: /* C99 6.7.2 */
938 notype_type_specifier
939 | T_TYPENAME {
940 $$ = getsym($1)->s_type;
941 }
942 ;
943
944 notype_type_specifier: /* see C99 6.7.2 */
945 T_TYPE {
946 $$ = gettyp($1);
947 }
948 | T_TYPEOF T_LPAREN expression T_RPAREN { /* GCC extension */
949 $$ = $3 != NULL ? block_dup_type($3->tn_type) : gettyp(INT);
950 $$->t_typeof = true;
951 }
952 | atomic_type_specifier
953 | struct_or_union_specifier {
954 end_declaration_level();
955 $$ = $1;
956 }
957 | enum_specifier {
958 end_declaration_level();
959 $$ = $1;
960 }
961 ;
962
963 /* K&R ---, C90 ---, C99 ---, C11 6.7.2.4 */
964 atomic_type_specifier:
965 atomic T_LPAREN type_name T_RPAREN {
966 $$ = $3;
967 }
968 ;
969
970 struct_or_union_specifier: /* C99 6.7.2.1 */
971 struct_or_union identifier_sym {
972 /*
973 * STDC requires that "struct a;" always introduces
974 * a new tag if "a" is not declared at current level
975 *
976 * yychar is valid because otherwise the parser would not
977 * have been able to decide if it must shift or reduce
978 */
979 $$ = make_tag_type($2, $1, false, yychar == T_SEMI);
980 }
981 | struct_or_union identifier_sym {
982 dcs->d_tag_type = make_tag_type($2, $1, true, false);
983 } braced_struct_declaration_list {
984 $$ = complete_struct_or_union($4);
985 }
986 | struct_or_union {
987 dcs->d_tag_type = make_tag_type(NULL, $1, true, false);
988 } braced_struct_declaration_list {
989 $$ = complete_struct_or_union($3);
990 }
991 | struct_or_union error {
992 set_symtyp(FVFT);
993 $$ = gettyp(INT);
994 }
995 ;
996
997 struct_or_union: /* C99 6.7.2.1 */
998 T_STRUCT_OR_UNION {
999 set_symtyp(FTAG);
1000 begin_declaration_level($1 == STRUCT ? DLK_STRUCT : DLK_UNION);
1001 dcs->d_sou_size_in_bits = 0;
1002 dcs->d_sou_align_in_bits = CHAR_SIZE;
1003 $$ = $1;
1004 }
1005 | struct_or_union type_attribute
1006 ;
1007
1008 braced_struct_declaration_list: /* see C99 6.7.2.1 */
1009 struct_declaration_lbrace struct_declaration_list_with_rbrace {
1010 $$ = $2;
1011 }
1012 ;
1013
1014 struct_declaration_lbrace: /* see C99 6.7.2.1 */
1015 T_LBRACE {
1016 set_symtyp(FVFT);
1017 }
1018 ;
1019
1020 struct_declaration_list_with_rbrace: /* see C99 6.7.2.1 */
1021 struct_declaration_list T_RBRACE
1022 | T_RBRACE {
1023 /* XXX: This is not allowed by any C standard. */
1024 $$ = NULL;
1025 }
1026 ;
1027
1028 struct_declaration_list: /* C99 6.7.2.1 */
1029 struct_declaration
1030 | struct_declaration_list struct_declaration {
1031 $$ = concat_symbols($1, $2);
1032 }
1033 ;
1034
1035 struct_declaration: /* C99 6.7.2.1 */
1036 begin_type_qualifier_list end_type {
1037 /* ^^ There is no check for the missing type-specifier. */
1038 /* too late, i know, but getsym() compensates it */
1039 set_symtyp(FMEMBER);
1040 } notype_struct_declarators type_attribute_opt T_SEMI {
1041 set_symtyp(FVFT);
1042 $$ = $4;
1043 }
1044 | begin_type_specifier_qualifier_list end_type {
1045 set_symtyp(FMEMBER);
1046 } type_struct_declarators type_attribute_opt T_SEMI {
1047 set_symtyp(FVFT);
1048 $$ = $4;
1049 }
1050 | begin_type_qualifier_list end_type type_attribute_opt T_SEMI {
1051 /* syntax error '%s' */
1052 error(249, "member without type");
1053 $$ = NULL;
1054 }
1055 | begin_type_specifier_qualifier_list end_type type_attribute_opt
1056 T_SEMI {
1057 set_symtyp(FVFT);
1058 if (!allow_c11 && !allow_gcc)
1059 /* anonymous struct/union members is a C11 feature */
1060 warning(49);
1061 if (is_struct_or_union(dcs->d_type->t_tspec))
1062 $$ = declare_unnamed_member();
1063 else {
1064 /* syntax error '%s' */
1065 error(249, "unnamed member");
1066 $$ = NULL;
1067 }
1068 }
1069 | static_assert_declaration {
1070 $$ = NULL;
1071 }
1072 | error T_SEMI {
1073 set_symtyp(FVFT);
1074 $$ = NULL;
1075 }
1076 ;
1077
1078 notype_struct_declarators:
1079 notype_struct_declarator {
1080 $$ = declare_member($1);
1081 }
1082 | notype_struct_declarators {
1083 set_symtyp(FMEMBER);
1084 } T_COMMA type_struct_declarator {
1085 $$ = concat_symbols($1, declare_member($4));
1086 }
1087 ;
1088
1089 type_struct_declarators:
1090 type_struct_declarator {
1091 $$ = declare_member($1);
1092 }
1093 | type_struct_declarators {
1094 set_symtyp(FMEMBER);
1095 } T_COMMA type_struct_declarator {
1096 $$ = concat_symbols($1, declare_member($4));
1097 }
1098 ;
1099
1100 notype_struct_declarator:
1101 notype_declarator
1102 | notype_declarator T_COLON constant_expr { /* C99 6.7.2.1 */
1103 $$ = set_bit_field_width($1, to_int_constant($3, true));
1104 }
1105 | {
1106 set_symtyp(FVFT);
1107 } T_COLON constant_expr { /* C99 6.7.2.1 */
1108 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1109 }
1110 ;
1111
1112 type_struct_declarator:
1113 type_declarator
1114 | type_declarator T_COLON constant_expr {
1115 $$ = set_bit_field_width($1, to_int_constant($3, true));
1116 }
1117 | {
1118 set_symtyp(FVFT);
1119 } T_COLON constant_expr {
1120 $$ = set_bit_field_width(NULL, to_int_constant($3, true));
1121 }
1122 ;
1123
1124 /* K&R ---, C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2 */
1125 enum_specifier: /* C99 6.7.2.2 */
1126 enum gcc_attribute_specifier_list_opt identifier_sym {
1127 $$ = make_tag_type($3, ENUM, false, false);
1128 }
1129 | enum gcc_attribute_specifier_list_opt identifier_sym {
1130 dcs->d_tag_type = make_tag_type($3, ENUM, true, false);
1131 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1132 $$ = complete_enum($5);
1133 }
1134 | enum gcc_attribute_specifier_list_opt {
1135 dcs->d_tag_type = make_tag_type(NULL, ENUM, true, false);
1136 } enum_declaration /*gcc_attribute_specifier_list_opt*/ {
1137 $$ = complete_enum($4);
1138 }
1139 | enum error {
1140 set_symtyp(FVFT);
1141 $$ = gettyp(INT);
1142 }
1143 ;
1144
1145 enum: /* helper for C99 6.7.2.2 */
1146 T_ENUM {
1147 set_symtyp(FTAG);
1148 begin_declaration_level(DLK_ENUM);
1149 }
1150 ;
1151
1152 enum_declaration: /* helper for C99 6.7.2.2 */
1153 enum_decl_lbrace enums_with_opt_comma T_RBRACE {
1154 $$ = $2;
1155 }
1156 ;
1157
1158 enum_decl_lbrace: /* helper for C99 6.7.2.2 */
1159 T_LBRACE {
1160 set_symtyp(FVFT);
1161 enumval = 0;
1162 }
1163 ;
1164
1165 enums_with_opt_comma: /* helper for C99 6.7.2.2 */
1166 enumerator_list
1167 | enumerator_list T_COMMA {
1168 if (!allow_c99 && !allow_trad) {
1169 /* trailing ',' prohibited in enum declaration */
1170 error(54);
1171 } else {
1172 /* trailing ',' prohibited in enum declaration */
1173 c99ism(54);
1174 }
1175 $$ = $1;
1176 }
1177 ;
1178
1179 enumerator_list: /* C99 6.7.2.2 */
1180 enumerator
1181 | enumerator_list T_COMMA enumerator {
1182 $$ = concat_symbols($1, $3);
1183 }
1184 | error {
1185 $$ = NULL;
1186 }
1187 ;
1188
1189 enumerator: /* C99 6.7.2.2 */
1190 identifier_sym gcc_attribute_specifier_list_opt {
1191 $$ = enumeration_constant($1, enumval, true);
1192 }
1193 | identifier_sym gcc_attribute_specifier_list_opt
1194 T_ASSIGN constant_expr {
1195 $$ = enumeration_constant($1, to_int_constant($4, true),
1196 false);
1197 }
1198 ;
1199
1200 type_qualifier: /* C99 6.7.3 */
1201 T_QUAL
1202 | atomic {
1203 $$ = ATOMIC;
1204 }
1205 ;
1206
1207 atomic: /* helper */
1208 T_ATOMIC {
1209 /* TODO: First fix c11ism, then use it here. */
1210 if (!allow_c11)
1211 /* '_Atomic' requires C11 or later */
1212 error(350);
1213 }
1214 ;
1215
1216 pointer: /* C99 6.7.5 */
1217 asterisk type_qualifier_list_opt {
1218 $$ = merge_qualified_pointer($1, $2);
1219 }
1220 | asterisk type_qualifier_list_opt pointer {
1221 $$ = merge_qualified_pointer($1, $2);
1222 $$ = merge_qualified_pointer($$, $3);
1223 }
1224 ;
1225
1226 asterisk: /* helper for 'pointer' */
1227 T_ASTERISK {
1228 $$ = xcalloc(1, sizeof(*$$));
1229 $$->p_pointer = true;
1230 }
1231 ;
1232
1233 type_qualifier_list_opt: /* see C99 6.7.5 */
1234 /* empty */ {
1235 $$ = NULL;
1236 }
1237 | type_qualifier_list
1238 ;
1239
1240 type_qualifier_list: /* C99 6.7.5 */
1241 type_qualifier_list_elem
1242 | type_qualifier_list type_qualifier_list_elem {
1243 $$ = merge_qualified_pointer($1, $2);
1244 }
1245 ;
1246
1247 type_qualifier_list_elem: /* helper for 'pointer' */
1248 type_qualifier {
1249 $$ = xcalloc(1, sizeof(*$$));
1250 if ($1 == CONST)
1251 $$->p_const = true;
1252 if ($1 == VOLATILE)
1253 $$->p_volatile = true;
1254 }
1255 ;
1256
1257 /*
1258 * For an explanation of 'notype' in the following rules, see
1259 * https://www.gnu.org/software/bison/manual/bison.html#Semantic-Tokens.
1260 */
1261
1262 notype_init_declarators:
1263 notype_init_declarator
1264 | notype_init_declarators T_COMMA type_init_declarator
1265 ;
1266
1267 type_init_declarators:
1268 type_init_declarator
1269 | type_init_declarators T_COMMA type_init_declarator
1270 ;
1271
1272 notype_init_declarator:
1273 notype_declarator asm_or_symbolrename_opt {
1274 cgram_declare($1, false, $2);
1275 check_size($1);
1276 }
1277 | notype_declarator asm_or_symbolrename_opt {
1278 begin_initialization($1);
1279 cgram_declare($1, true, $2);
1280 } T_ASSIGN initializer {
1281 check_size($1);
1282 end_initialization();
1283 }
1284 ;
1285
1286 type_init_declarator:
1287 type_declarator asm_or_symbolrename_opt {
1288 cgram_declare($1, false, $2);
1289 check_size($1);
1290 }
1291 | type_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 notype_declarator:
1301 notype_direct_declarator
1302 | pointer notype_direct_declarator {
1303 $$ = add_pointer($2, $1);
1304 }
1305 ;
1306
1307 type_declarator:
1308 type_direct_declarator
1309 | pointer type_direct_declarator {
1310 $$ = add_pointer($2, $1);
1311 }
1312 ;
1313
1314 notype_direct_declarator:
1315 type_attribute_list_opt T_NAME {
1316 $$ = declarator_name(getsym($2));
1317 }
1318 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1319 $$ = $3;
1320 }
1321 | notype_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1322 $$ = add_array($1, $3.has_dim, $3.dim);
1323 }
1324 | notype_direct_declarator param_list asm_or_symbolrename_opt {
1325 $$ = add_function(symbolrename($1, $3), $2);
1326 end_declaration_level();
1327 block_level--;
1328 }
1329 | notype_direct_declarator type_attribute
1330 ;
1331
1332 type_direct_declarator:
1333 type_attribute_list_opt identifier {
1334 $$ = declarator_name(getsym($2));
1335 }
1336 | type_attribute_list_opt T_LPAREN type_declarator T_RPAREN {
1337 $$ = $3;
1338 }
1339 | type_direct_declarator T_LBRACK array_size_opt T_RBRACK {
1340 $$ = add_array($1, $3.has_dim, $3.dim);
1341 }
1342 | type_direct_declarator param_list asm_or_symbolrename_opt {
1343 $$ = add_function(symbolrename($1, $3), $2);
1344 end_declaration_level();
1345 block_level--;
1346 }
1347 | type_direct_declarator type_attribute
1348 ;
1349
1350 /*
1351 * The two distinct rules type_param_declarator and notype_param_declarator
1352 * avoid a conflict in argument lists. A typename enclosed in parentheses is
1353 * always treated as a typename, not an argument name. For example, after
1354 * "typedef double a;", the declaration "f(int (a));" is interpreted as
1355 * "f(int (double));", not "f(int a);".
1356 */
1357 type_param_declarator:
1358 direct_param_declarator
1359 | pointer direct_param_declarator {
1360 $$ = add_pointer($2, $1);
1361 }
1362 ;
1363
1364 notype_param_declarator:
1365 direct_notype_param_declarator
1366 | pointer direct_notype_param_declarator {
1367 $$ = add_pointer($2, $1);
1368 }
1369 ;
1370
1371 direct_param_declarator:
1372 identifier type_attribute_list {
1373 $$ = declarator_name(getsym($1));
1374 }
1375 | identifier {
1376 $$ = declarator_name(getsym($1));
1377 }
1378 | T_LPAREN notype_param_declarator T_RPAREN {
1379 $$ = $2;
1380 }
1381 | direct_param_declarator T_LBRACK array_size_opt T_RBRACK
1382 gcc_attribute_specifier_list_opt {
1383 $$ = add_array($1, $3.has_dim, $3.dim);
1384 }
1385 | direct_param_declarator param_list asm_or_symbolrename_opt {
1386 $$ = add_function(symbolrename($1, $3), $2);
1387 end_declaration_level();
1388 block_level--;
1389 }
1390 ;
1391
1392 direct_notype_param_declarator:
1393 identifier {
1394 $$ = declarator_name(getsym($1));
1395 }
1396 | T_LPAREN notype_param_declarator T_RPAREN {
1397 $$ = $2;
1398 }
1399 | direct_notype_param_declarator T_LBRACK array_size_opt T_RBRACK {
1400 $$ = add_array($1, $3.has_dim, $3.dim);
1401 }
1402 | direct_notype_param_declarator param_list asm_or_symbolrename_opt {
1403 $$ = add_function(symbolrename($1, $3), $2);
1404 end_declaration_level();
1405 block_level--;
1406 }
1407 ;
1408
1409 param_list:
1410 id_list_lparen identifier_list T_RPAREN {
1411 $$ = $2;
1412 }
1413 | abstract_decl_param_list
1414 ;
1415
1416 id_list_lparen:
1417 T_LPAREN {
1418 block_level++;
1419 begin_declaration_level(DLK_PROTO_PARAMS);
1420 }
1421 ;
1422
1423 array_size_opt:
1424 /* empty */ {
1425 $$.has_dim = false;
1426 $$.dim = 0;
1427 }
1428 | T_ASTERISK {
1429 /* since C99; variable length array of unspecified size */
1430 $$.has_dim = false; /* TODO: maybe change to true */
1431 $$.dim = 0; /* just as a placeholder */
1432 }
1433 | array_size {
1434 $$.has_dim = true;
1435 $$.dim = $1 == NULL ? 0 : to_int_constant($1, false);
1436 }
1437 ;
1438
1439 array_size:
1440 type_qualifier_list_opt T_SCLASS constant_expr {
1441 /* C11 6.7.6.3p7 */
1442 if ($2 != STATIC)
1443 yyerror("Bad attribute");
1444 /* static array size is a C11 extension */
1445 c11ism(343);
1446 $$ = $3;
1447 }
1448 | type_qualifier {
1449 /* C11 6.7.6.2 */
1450 if ($1 != RESTRICT)
1451 yyerror("Bad attribute");
1452 $$ = NULL;
1453 }
1454 | constant_expr
1455 ;
1456
1457 identifier_list: /* C99 6.7.5 */
1458 T_NAME {
1459 $$ = old_style_function_parameter_name(getsym($1));
1460 }
1461 | identifier_list T_COMMA T_NAME {
1462 $$ = concat_symbols($1,
1463 old_style_function_parameter_name(getsym($3)));
1464 }
1465 | identifier_list error
1466 ;
1467
1468 /* XXX: C99 requires an additional specifier-qualifier-list. */
1469 type_name: /* C99 6.7.6 */
1470 {
1471 begin_declaration_level(DLK_ABSTRACT);
1472 } abstract_declaration {
1473 end_declaration_level();
1474 $$ = $2->s_type;
1475 }
1476 ;
1477
1478 abstract_declaration: /* specific to lint */
1479 begin_type_qualifier_list end_type {
1480 $$ = declare_abstract_type(abstract_name());
1481 }
1482 | begin_type_specifier_qualifier_list end_type {
1483 $$ = declare_abstract_type(abstract_name());
1484 }
1485 | begin_type_qualifier_list end_type abstract_declarator {
1486 $$ = declare_abstract_type($3);
1487 }
1488 | begin_type_specifier_qualifier_list end_type abstract_declarator {
1489 $$ = declare_abstract_type($3);
1490 }
1491 ;
1492
1493 /* K&R 8.7, C90 ???, C99 6.7.6, C11 6.7.7 */
1494 /* In K&R, abstract-declarator could be empty and was still simpler. */
1495 abstract_declarator:
1496 pointer {
1497 $$ = add_pointer(abstract_name(), $1);
1498 }
1499 | direct_abstract_declarator
1500 | pointer direct_abstract_declarator {
1501 $$ = add_pointer($2, $1);
1502 }
1503 | type_attribute_list direct_abstract_declarator {
1504 $$ = $2;
1505 }
1506 | pointer type_attribute_list direct_abstract_declarator {
1507 $$ = add_pointer($3, $1);
1508 }
1509 ;
1510
1511 /* K&R ---, C90 ???, C99 6.7.6, C11 6.7.7 */
1512 direct_abstract_declarator:
1513 /* TODO: sort rules according to C99 */
1514 T_LPAREN abstract_declarator T_RPAREN {
1515 $$ = $2;
1516 }
1517 | T_LBRACK array_size_opt T_RBRACK {
1518 $$ = add_array(abstract_name(), $2.has_dim, $2.dim);
1519 }
1520 | direct_abstract_declarator T_LBRACK array_size_opt T_RBRACK {
1521 $$ = add_array($1, $3.has_dim, $3.dim);
1522 }
1523 | abstract_decl_param_list asm_or_symbolrename_opt {
1524 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1525 end_declaration_level();
1526 block_level--;
1527 }
1528 | direct_abstract_declarator abstract_decl_param_list
1529 asm_or_symbolrename_opt {
1530 $$ = add_function(symbolrename($1, $3), $2);
1531 end_declaration_level();
1532 block_level--;
1533 }
1534 | direct_abstract_declarator type_attribute_list
1535 ;
1536
1537 abstract_decl_param_list: /* specific to lint */
1538 abstract_decl_lparen T_RPAREN type_attribute_opt {
1539 $$ = NULL;
1540 }
1541 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN
1542 type_attribute_opt {
1543 dcs->d_prototype = true;
1544 $$ = $2;
1545 }
1546 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1547 $$ = NULL;
1548 }
1549 ;
1550
1551 abstract_decl_lparen: /* specific to lint */
1552 T_LPAREN {
1553 block_level++;
1554 begin_declaration_level(DLK_PROTO_PARAMS);
1555 }
1556 ;
1557
1558 vararg_parameter_type_list: /* specific to lint */
1559 parameter_type_list
1560 | parameter_type_list T_COMMA T_ELLIPSIS {
1561 dcs->d_vararg = true;
1562 $$ = $1;
1563 }
1564 | T_ELLIPSIS {
1565 /* TODO: C99 6.7.5 makes this an error as well. */
1566 if (!allow_trad && !allow_c99) {
1567 /* ANSI C requires formal parameter before '...' */
1568 error(84);
1569 } else if (allow_c90) {
1570 /* ANSI C requires formal parameter before '...' */
1571 warning(84);
1572 }
1573 dcs->d_vararg = true;
1574 $$ = NULL;
1575 }
1576 ;
1577
1578 /* XXX: C99 6.7.5 defines the same name, but it looks different. */
1579 parameter_type_list:
1580 parameter_declaration
1581 | parameter_type_list T_COMMA parameter_declaration {
1582 $$ = concat_symbols($1, $3);
1583 }
1584 ;
1585
1586 /* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1587 parameter_declaration:
1588 begin_type_declmods end_type {
1589 /* ^^ There is no check for the missing type-specifier. */
1590 $$ = declare_argument(abstract_name(), false);
1591 }
1592 | begin_type_declaration_specifiers end_type {
1593 $$ = declare_argument(abstract_name(), false);
1594 }
1595 | begin_type_declmods end_type notype_param_declarator {
1596 /* ^^ There is no check for the missing type-specifier. */
1597 $$ = declare_argument($3, false);
1598 }
1599 /*
1600 * type_param_declarator is needed because of following conflict:
1601 * "typedef int a; f(int (a));" could be parsed as
1602 * "function with argument a of type int", or
1603 * "function with an abstract argument of type function".
1604 * This grammar realizes the second case.
1605 */
1606 | begin_type_declaration_specifiers end_type type_param_declarator {
1607 $$ = declare_argument($3, false);
1608 }
1609 | begin_type_declmods end_type abstract_declarator {
1610 /* ^^ There is no check for the missing type-specifier. */
1611 $$ = declare_argument($3, false);
1612 }
1613 | begin_type_declaration_specifiers end_type abstract_declarator {
1614 $$ = declare_argument($3, false);
1615 }
1616 ;
1617
1618 braced_initializer:
1619 /* K&R ---, C90 ---, C99 ---, C11 ---, C23 6.7.10 */
1620 init_lbrace init_rbrace {
1621 /* empty initializer braces require C23 or later */
1622 c23ism(353);
1623 }
1624 /* K&R ---, C90 ---, C99 6.7.8, C11 6.7.9, C23 6.7.10 */
1625 | init_lbrace initializer_list comma_opt init_rbrace
1626 ;
1627
1628 initializer: /* C99 6.7.8 "Initialization" */
1629 assignment_expression {
1630 init_expr($1);
1631 }
1632 | init_lbrace init_rbrace {
1633 /* XXX: Empty braces are not covered by C99 6.7.8. */
1634 }
1635 | init_lbrace initializer_list comma_opt init_rbrace
1636 /* XXX: What is this error handling for? */
1637 | error
1638 ;
1639
1640 initializer_list: /* C99 6.7.8 "Initialization" */
1641 initializer_list_item
1642 | initializer_list T_COMMA initializer_list_item
1643 ;
1644
1645 initializer_list_item: /* helper */
1646 designation initializer
1647 | initializer
1648 ;
1649
1650 designation: /* C99 6.7.8 "Initialization" */
1651 begin_designation designator_list T_ASSIGN
1652 | identifier T_COLON {
1653 /* GCC style struct or union member name in initializer */
1654 gnuism(315);
1655 begin_designation();
1656 add_designator_member($1);
1657 }
1658 ;
1659
1660 begin_designation: /* lint-specific helper */
1661 /* empty */ {
1662 begin_designation();
1663 }
1664 ;
1665
1666 designator_list: /* C99 6.7.8 "Initialization" */
1667 designator
1668 | designator_list designator
1669 ;
1670
1671 designator: /* C99 6.7.8 "Initialization" */
1672 T_LBRACK range T_RBRACK {
1673 if (!allow_c99)
1674 /* array initializer with designators is a C99 ... */
1675 warning(321);
1676 add_designator_subscript($2);
1677 }
1678 | T_POINT identifier {
1679 if (!allow_c99)
1680 /* struct or union member name in initializer is ... */
1681 warning(313);
1682 add_designator_member($2);
1683 }
1684 ;
1685
1686 static_assert_declaration:
1687 T_STATIC_ASSERT T_LPAREN constant_expr T_COMMA T_STRING T_RPAREN T_SEMI /* C11 */
1688 | T_STATIC_ASSERT T_LPAREN constant_expr T_RPAREN T_SEMI /* C23 */
1689 ;
1690
1691 range:
1692 constant_expr {
1693 $$.lo = to_int_constant($1, true);
1694 $$.hi = $$.lo;
1695 }
1696 | constant_expr T_ELLIPSIS constant_expr {
1697 $$.lo = to_int_constant($1, true);
1698 $$.hi = to_int_constant($3, true);
1699 /* initialization with '[a...b]' is a GCC extension */
1700 gnuism(340);
1701 }
1702 ;
1703
1704 init_lbrace: /* helper */
1705 T_LBRACE {
1706 init_lbrace();
1707 }
1708 ;
1709
1710 init_rbrace: /* helper */
1711 T_RBRACE {
1712 init_rbrace();
1713 }
1714 ;
1715
1716 asm_or_symbolrename_opt: /* GCC extensions */
1717 /* empty */ {
1718 $$ = NULL;
1719 }
1720 | T_ASM T_LPAREN T_STRING T_RPAREN gcc_attribute_specifier_list_opt {
1721 freeyyv(&$3, T_STRING);
1722 $$ = NULL;
1723 }
1724 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN
1725 gcc_attribute_specifier_list_opt {
1726 $$ = $3;
1727 }
1728 ;
1729
1730 statement: /* C99 6.8 */
1731 expression_statement
1732 | non_expr_statement
1733 ;
1734
1735 non_expr_statement: /* helper for C99 6.8 */
1736 gcc_attribute_specifier /* ((__fallthrough__)) */ T_SEMI
1737 | labeled_statement
1738 | compound_statement
1739 | selection_statement
1740 | iteration_statement
1741 | jump_statement {
1742 seen_fallthrough = false;
1743 }
1744 | asm_statement
1745 ;
1746
1747 labeled_statement: /* C99 6.8.1 */
1748 label gcc_attribute_specifier_list_opt statement
1749 ;
1750
1751 label:
1752 T_NAME T_COLON {
1753 set_symtyp(FLABEL);
1754 named_label(getsym($1));
1755 }
1756 | T_CASE constant_expr T_COLON {
1757 case_label($2);
1758 seen_fallthrough = true;
1759 }
1760 | T_CASE constant_expr T_ELLIPSIS constant_expr T_COLON {
1761 /* XXX: We don't fill all cases */
1762 case_label($2);
1763 seen_fallthrough = true;
1764 }
1765 | T_DEFAULT T_COLON {
1766 default_label();
1767 seen_fallthrough = true;
1768 }
1769 ;
1770
1771 compound_statement: /* C99 6.8.2 */
1772 compound_statement_lbrace compound_statement_rbrace
1773 | compound_statement_lbrace block_item_list compound_statement_rbrace
1774 ;
1775
1776 compound_statement_lbrace:
1777 T_LBRACE {
1778 block_level++;
1779 mem_block_level++;
1780 begin_declaration_level(DLK_AUTO);
1781 }
1782 ;
1783
1784 compound_statement_rbrace:
1785 T_RBRACE {
1786 end_declaration_level();
1787 level_free_all(mem_block_level);
1788 mem_block_level--;
1789 block_level--;
1790 seen_fallthrough = false;
1791 }
1792 ;
1793
1794 block_item_list: /* C99 6.8.2 */
1795 block_item
1796 | block_item_list block_item {
1797 if ($1 && !$2)
1798 /* declarations after statements is a C99 feature */
1799 c99ism(327);
1800 $$ = $1 || $2;
1801 }
1802 ;
1803
1804 block_item: /* C99 6.8.2 */
1805 declaration_or_error {
1806 $$ = false;
1807 restore_warning_flags();
1808 }
1809 | statement {
1810 $$ = true;
1811 restore_warning_flags();
1812 }
1813 ;
1814
1815 expression_statement: /* C99 6.8.3 */
1816 expression T_SEMI {
1817 expr($1, false, false, false, false);
1818 seen_fallthrough = false;
1819 }
1820 | T_SEMI {
1821 check_statement_reachable();
1822 seen_fallthrough = false;
1823 }
1824 ;
1825
1826 selection_statement: /* C99 6.8.4 */
1827 if_without_else %prec T_THEN {
1828 save_warning_flags();
1829 stmt_if_then_stmt();
1830 stmt_if_else_stmt(false);
1831 }
1832 | if_without_else T_ELSE {
1833 save_warning_flags();
1834 stmt_if_then_stmt();
1835 } statement {
1836 clear_warning_flags();
1837 stmt_if_else_stmt(true);
1838 }
1839 | if_without_else T_ELSE error {
1840 clear_warning_flags();
1841 stmt_if_else_stmt(false);
1842 }
1843 | switch_expr statement {
1844 clear_warning_flags();
1845 stmt_switch_expr_stmt();
1846 }
1847 | switch_expr error {
1848 clear_warning_flags();
1849 stmt_switch_expr_stmt();
1850 }
1851 ;
1852
1853 if_without_else: /* see C99 6.8.4 */
1854 if_expr statement
1855 | if_expr error
1856 ;
1857
1858 if_expr: /* see C99 6.8.4 */
1859 T_IF T_LPAREN expression T_RPAREN {
1860 stmt_if_expr($3);
1861 clear_warning_flags();
1862 }
1863 ;
1864
1865 switch_expr: /* see C99 6.8.4 */
1866 T_SWITCH T_LPAREN expression T_RPAREN {
1867 stmt_switch_expr($3);
1868 clear_warning_flags();
1869 }
1870 ;
1871
1872 iteration_statement: /* C99 6.8.5 */
1873 while_expr statement {
1874 clear_warning_flags();
1875 stmt_while_expr_stmt();
1876 }
1877 | while_expr error {
1878 clear_warning_flags();
1879 stmt_while_expr_stmt();
1880 }
1881 | do_statement do_while_expr {
1882 stmt_do_while_expr($2);
1883 seen_fallthrough = false;
1884 }
1885 | do error {
1886 clear_warning_flags();
1887 stmt_do_while_expr(NULL);
1888 }
1889 | for_exprs statement {
1890 clear_warning_flags();
1891 stmt_for_exprs_stmt();
1892 end_declaration_level();
1893 block_level--;
1894 }
1895 | for_exprs error {
1896 clear_warning_flags();
1897 stmt_for_exprs_stmt();
1898 end_declaration_level();
1899 block_level--;
1900 }
1901 ;
1902
1903 while_expr: /* see C99 6.8.5 */
1904 T_WHILE T_LPAREN expression T_RPAREN {
1905 stmt_while_expr($3);
1906 clear_warning_flags();
1907 }
1908 ;
1909
1910 do_statement: /* see C99 6.8.5 */
1911 do statement {
1912 clear_warning_flags();
1913 }
1914 ;
1915
1916 do: /* see C99 6.8.5 */
1917 T_DO {
1918 stmt_do();
1919 }
1920 ;
1921
1922 do_while_expr: /* see C99 6.8.5 */
1923 T_WHILE T_LPAREN expression T_RPAREN T_SEMI {
1924 $$ = $3;
1925 }
1926 ;
1927
1928 for_start: /* see C99 6.8.5 */
1929 T_FOR T_LPAREN {
1930 begin_declaration_level(DLK_AUTO);
1931 block_level++;
1932 }
1933 ;
1934
1935 for_exprs: /* see C99 6.8.5 */
1936 for_start
1937 begin_type_declaration_specifiers end_type
1938 notype_init_declarators T_SEMI
1939 expression_opt T_SEMI
1940 expression_opt T_RPAREN {
1941 /* variable declaration in for loop */
1942 c99ism(325);
1943 stmt_for_exprs(NULL, $6, $8);
1944 clear_warning_flags();
1945 }
1946 | for_start
1947 expression_opt T_SEMI
1948 expression_opt T_SEMI
1949 expression_opt T_RPAREN {
1950 stmt_for_exprs($2, $4, $6);
1951 clear_warning_flags();
1952 }
1953 ;
1954
1955 jump_statement: /* C99 6.8.6 */
1956 goto identifier T_SEMI {
1957 stmt_goto(getsym($2));
1958 }
1959 | goto error T_SEMI {
1960 set_symtyp(FVFT);
1961 }
1962 | T_CONTINUE T_SEMI {
1963 stmt_continue();
1964 }
1965 | T_BREAK T_SEMI {
1966 stmt_break();
1967 }
1968 | T_RETURN sys T_SEMI {
1969 stmt_return($2, NULL);
1970 }
1971 | T_RETURN sys expression T_SEMI {
1972 stmt_return($2, $3);
1973 }
1974 ;
1975
1976 goto: /* see C99 6.8.6 */
1977 T_GOTO {
1978 set_symtyp(FLABEL);
1979 }
1980 ;
1981
1982 asm_statement: /* GCC extension */
1983 T_ASM T_LPAREN read_until_rparen T_SEMI {
1984 dcs_set_asm();
1985 }
1986 | T_ASM type_qualifier T_LPAREN read_until_rparen T_SEMI {
1987 dcs_set_asm();
1988 }
1989 | T_ASM error
1990 ;
1991
1992 read_until_rparen: /* helper for 'asm_statement' */
1993 /* empty */ {
1994 read_until_rparen();
1995 }
1996 ;
1997
1998 translation_unit: /* C99 6.9 */
1999 external_declaration
2000 | translation_unit external_declaration
2001 ;
2002
2003 external_declaration: /* C99 6.9 */
2004 function_definition {
2005 global_clean_up_decl(false);
2006 clear_warning_flags();
2007 }
2008 | top_level_declaration {
2009 global_clean_up_decl(false);
2010 clear_warning_flags();
2011 }
2012 | asm_statement /* GCC extension */
2013 | T_SEMI { /* GCC extension */
2014 /*
2015 * TODO: Only allow this in GCC mode, not in plain C99.
2016 * This is one of the top 10 warnings in the NetBSD build.
2017 */
2018 if (!allow_trad && !allow_c99) {
2019 /* empty declaration */
2020 error(0);
2021 } else if (allow_c90) {
2022 /* empty declaration */
2023 warning(0);
2024 }
2025 }
2026 ;
2027
2028 /*
2029 * On the top level, lint allows several forms of declarations that it doesn't
2030 * allow in functions. For example, a single ';' is an empty declaration and
2031 * is supported by some compilers, but in a function it would be an empty
2032 * statement, not a declaration. This makes a difference in C90 mode, where
2033 * a statement must not be followed by a declaration.
2034 *
2035 * See 'declaration' for all other declarations.
2036 */
2037 top_level_declaration: /* C99 6.9 calls this 'declaration' */
2038 begin_type end_type notype_init_declarators T_SEMI {
2039 /* TODO: Make this an error in C99 mode as well. */
2040 if (!allow_trad && !allow_c99) {
2041 /* old-style declaration; add 'int' */
2042 error(1);
2043 } else if (allow_c90) {
2044 /* old-style declaration; add 'int' */
2045 warning(1);
2046 }
2047 }
2048 | declaration
2049 | error T_SEMI {
2050 global_clean_up();
2051 }
2052 | error T_RBRACE {
2053 global_clean_up();
2054 }
2055 ;
2056
2057 function_definition: /* C99 6.9.1 */
2058 func_declarator {
2059 if ($1->s_type->t_tspec != FUNC) {
2060 /* syntax error '%s' */
2061 error(249, yytext);
2062 YYERROR;
2063 }
2064 if ($1->s_type->t_typedef) {
2065 /* ()-less function definition */
2066 error(64);
2067 YYERROR;
2068 }
2069 check_extern_declaration($1);
2070 begin_function($1);
2071 block_level++;
2072 begin_declaration_level(DLK_OLD_STYLE_ARGS);
2073 if (lwarn == LWARN_NONE)
2074 $1->s_used = true;
2075 } arg_declaration_list_opt {
2076 end_declaration_level();
2077 block_level--;
2078 check_func_lint_directives();
2079 check_func_old_style_arguments();
2080 begin_control_statement(CS_FUNCTION_BODY);
2081 } compound_statement {
2082 end_function();
2083 end_control_statement(CS_FUNCTION_BODY);
2084 }
2085 ;
2086
2087 func_declarator:
2088 begin_type end_type notype_declarator {
2089 if (!allow_trad) {
2090 /* old-style declaration; add 'int' */
2091 error(1);
2092 }
2093 $$ = $3;
2094 }
2095 | begin_type_declmods end_type notype_declarator {
2096 if (!allow_trad) {
2097 /* old-style declaration; add 'int' */
2098 error(1);
2099 }
2100 $$ = $3;
2101 }
2102 | begin_type_declaration_specifiers end_type type_declarator {
2103 $$ = $3;
2104 }
2105 ;
2106
2107 arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
2108 /* empty */
2109 | arg_declaration_list
2110 ;
2111
2112 arg_declaration_list: /* C99 6.9.1p13 example 1 */
2113 arg_declaration
2114 | arg_declaration_list arg_declaration
2115 /* XXX or better "arg_declaration error" ? */
2116 | error
2117 ;
2118
2119 /*
2120 * "arg_declaration" is separated from "declaration" because it
2121 * needs other error handling.
2122 */
2123 arg_declaration:
2124 begin_type_declmods end_type T_SEMI {
2125 /* empty declaration */
2126 warning(2);
2127 }
2128 | begin_type_declmods end_type notype_init_declarators T_SEMI
2129 | begin_type_declaration_specifiers end_type T_SEMI {
2130 if (!dcs->d_nonempty_decl) {
2131 /* empty declaration */
2132 warning(2);
2133 } else {
2134 /* '%s' declared in argument declaration list */
2135 warning(3, type_name(dcs->d_type));
2136 }
2137 }
2138 | begin_type_declaration_specifiers end_type
2139 type_init_declarators T_SEMI {
2140 if (dcs->d_nonempty_decl) {
2141 /* '%s' declared in argument declaration list */
2142 warning(3, type_name(dcs->d_type));
2143 }
2144 }
2145 | begin_type_declmods error
2146 | begin_type_declaration_specifiers error
2147 ;
2148
2149 /* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html */
2150 gcc_attribute_specifier_list_opt:
2151 /* empty */
2152 | gcc_attribute_specifier_list
2153 ;
2154
2155 gcc_attribute_specifier_list:
2156 gcc_attribute_specifier
2157 | gcc_attribute_specifier_list gcc_attribute_specifier
2158 ;
2159
2160 gcc_attribute_specifier:
2161 T_ATTRIBUTE T_LPAREN T_LPAREN {
2162 in_gcc_attribute = true;
2163 } gcc_attribute_list {
2164 in_gcc_attribute = false;
2165 } T_RPAREN T_RPAREN
2166 ;
2167
2168 gcc_attribute_list:
2169 gcc_attribute
2170 | gcc_attribute_list T_COMMA gcc_attribute
2171 ;
2172
2173 gcc_attribute:
2174 /* empty */
2175 | T_NAME {
2176 const char *name = $1->sb_name;
2177 if (is_either(name, "packed", "__packed__"))
2178 dcs_add_packed();
2179 else if (is_either(name, "used", "__used__") ||
2180 is_either(name, "unused", "__unused__"))
2181 dcs_set_used();
2182 else if (is_either(name, "fallthrough", "__fallthrough__"))
2183 fallthru(1);
2184 }
2185 | T_NAME T_LPAREN T_RPAREN
2186 | T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN
2187 | type_qualifier {
2188 if ($1 != CONST)
2189 yyerror("Bad attribute");
2190 }
2191 ;
2192
2193 gcc_attribute_parameters:
2194 constant_expr
2195 | gcc_attribute_parameters T_COMMA constant_expr
2196 ;
2197
2198 sys:
2199 /* empty */ {
2200 $$ = in_system_header;
2201 }
2202 ;
2203
2204 %%
2205
2206 /* ARGSUSED */
2207 int
2208 yyerror(const char *msg)
2209 {
2210 /* syntax error '%s' */
2211 error(249, yytext);
2212 if (++sytxerr >= 5)
2213 norecover();
2214 return 0;
2215 }
2216
2217 #if YYDEBUG && YYBYACC
2218 static const char *
2219 cgram_to_string(int token, YYSTYPE val)
2220 {
2221
2222 switch (token) {
2223 case T_INCDEC:
2224 case T_MULTIPLICATIVE:
2225 case T_ADDITIVE:
2226 case T_SHIFT:
2227 case T_RELATIONAL:
2228 case T_EQUALITY:
2229 case T_OPASSIGN:
2230 return op_name(val.y_op);
2231 case T_SCLASS:
2232 return scl_name(val.y_scl);
2233 case T_TYPE:
2234 case T_STRUCT_OR_UNION:
2235 return tspec_name(val.y_tspec);
2236 case T_QUAL:
2237 return tqual_name(val.y_tqual);
2238 case T_NAME:
2239 return val.y_name->sb_name;
2240 default:
2241 return "<none>";
2242 }
2243 }
2244 #endif
2245
2246 static void
2247 cgram_declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
2248 {
2249 declare(decl, has_initializer, renaming);
2250 if (renaming != NULL)
2251 freeyyv(&renaming, T_NAME);
2252 }
2253
2254 /*
2255 * Discard all input tokens up to and including the next
2256 * unmatched right paren
2257 */
2258 static void
2259 read_until_rparen(void)
2260 {
2261 int level;
2262
2263 if (yychar < 0)
2264 yychar = yylex();
2265 freeyyv(&yylval, yychar);
2266
2267 level = 1;
2268 while (yychar > 0) {
2269 if (yychar == T_LPAREN)
2270 level++;
2271 if (yychar == T_RPAREN && --level == 0)
2272 break;
2273 freeyyv(&yylval, yychar = yylex());
2274 }
2275
2276 yyclearin;
2277 }
2278
2279 static sym_t *
2280 symbolrename(sym_t *s, sbuf_t *sb)
2281 {
2282 if (sb != NULL)
2283 s->s_rename = sb->sb_name;
2284 return s;
2285 }
2286