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