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