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