Sun Jul 11 20:37:21 2021 UTC ()
lint: make _Generic a primary-expression

C11 says so, and unless the _Generic expression was wrapped in
parentheses, it was not possible before to use it as a function call
expression.


(rillig)
diff -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/c11_generic_expression.c
diff -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/c11_generic_expression.exp
diff -r1.316 -r1.317 src/usr.bin/xlint/lint1/cgram.y

cvs diff -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/c11_generic_expression.c (switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/c11_generic_expression.c 2021/07/11 20:34:05 1.7
+++ src/tests/usr.bin/xlint/lint1/c11_generic_expression.c 2021/07/11 20:37:21 1.8
@@ -1,98 +1,96 @@ @@ -1,98 +1,96 @@
1/* $NetBSD: c11_generic_expression.c,v 1.7 2021/07/11 20:34:05 rillig Exp $ */ 1/* $NetBSD: c11_generic_expression.c,v 1.8 2021/07/11 20:37:21 rillig Exp $ */
2# 3 "c11_generic_expression.c" 2# 3 "c11_generic_expression.c"
3 3
4/* 4/*
5 * C99 added support for type-generic macros, but these were limited to the 5 * C99 added support for type-generic macros, but these were limited to the
6 * header <tgmath.h>. C11 made this feature generally available. 6 * header <tgmath.h>. C11 made this feature generally available.
7 * 7 *
8 * The generic selection is typically used with macros, but since lint1 works 8 * The generic selection is typically used with macros, but since lint1 works
9 * on the preprocessed source, the test cases look a bit strange. 9 * on the preprocessed source, the test cases look a bit strange.
10 * 10 *
11 * C99 6.5.1.1 "Generic selection" 11 * C99 6.5.1.1 "Generic selection"
12 */ 12 */
13 13
14/* lint1-extra-flags: -Ac11 */ 14/* lint1-extra-flags: -Ac11 */
15 15
16/* 16/*
17 * The type of 'var' is not compatible with any of the types from the 17 * The type of 'var' is not compatible with any of the types from the
18 * generic-association. This is a compile-time error. 18 * generic-association. This is a compile-time error.
19 */ 19 */
20const char * 20const char *
21classify_type_without_default(double var) 21classify_type_without_default(double var)
22{ 22{
23 /* expect-2: argument 'var' unused */ 23 /* expect-2: argument 'var' unused */
24 24
25 return _Generic(var, 25 return _Generic(var,
26 long double: "long double", 26 long double: "long double",
27 long long: "long long", 27 long long: "long long",
28 unsigned: "unsigned" 28 unsigned: "unsigned"
29 ); 29 );
30 /* expect-1: expects to return value [214] */ 30 /* expect-1: expects to return value [214] */
31} 31}
32 32
33/* 33/*
34 * In this case, the 'default' expression is selected. 34 * In this case, the 'default' expression is selected.
35 */ 35 */
36const char * 36const char *
37classify_type_with_default(double var) 37classify_type_with_default(double var)
38{ 38{
39 /* expect-2: argument 'var' unused */ 39 /* expect-2: argument 'var' unused */
40 40
41 return _Generic(var, 41 return _Generic(var,
42 long double: "long double", 42 long double: "long double",
43 long long: "long long", 43 long long: "long long",
44 unsigned: "unsigned", 44 unsigned: "unsigned",
45 default: "unknown" 45 default: "unknown"
46 ); 46 );
47} 47}
48 48
49/* 49/*
50 * The type of a _Generic expression is the one from the selected association. 50 * The type of a _Generic expression is the one from the selected association.
51 */ 51 */
52const char * 52const char *
53classify_char(char c) 53classify_char(char c)
54{ 54{
55 /* expect-2: argument 'c' unused */ 55 /* expect-2: argument 'c' unused */
56 56
57 return _Generic(c, 57 return _Generic(c,
58 char: "yes", 58 char: "yes",
59 default: 0.0 59 default: 0.0
60 ); 60 );
61} 61}
62 62
63/* 63/*
64 * Before cgram.y 1.238 from 2021-06-27, lint accepted a comma-expression, 64 * Before cgram.y 1.238 from 2021-06-27, lint accepted a comma-expression,
65 * which looked as if _Generic would accept multiple arguments before the 65 * which looked as if _Generic would accept multiple arguments before the
66 * selection. 66 * selection.
67 */ 67 */
68/* ARGSUSED */ 68/* ARGSUSED */
69const int * 69const int *
70comma_expression(char first, double second) 70comma_expression(char first, double second)
71{ 71{
72 return _Generic(first, second, /* expect: syntax error 'second' */ 72 return _Generic(first, second, /* expect: syntax error 'second' */
73 char: "first", 73 char: "first",
74 double: 2.0 74 double: 2.0
75 ); 75 );
76 /* expect+1: without returning value [217] */ 76 /* expect+1: without returning value [217] */
77} 77}
78 78
79/* 79/*
80 * Ensure that assignment-expressions are accepted by the grammar, as 80 * Ensure that assignment-expressions are accepted by the grammar, as
81 * opposed to comma-expressions. 81 * opposed to comma-expressions.
82 */ 82 */
83/* ARGSUSED */ 83/* ARGSUSED */
84int 84int
85assignment_expression(int first, int second) 85assignment_expression(int first, int second)
86{ 86{
87 return _Generic(first = second, 87 return _Generic(first = second,
88 int: second = first 88 int: second = first
89 ); 89 );
90} 90}
91 91
92int 92int
93primary_expression(void) 93primary_expression(void)
94{ 94{
95 /*FIXME*//* expect+1: syntax error '(' [249] */ 
96 return _Generic(0, int: assignment_expression)(0, 0); 95 return _Generic(0, int: assignment_expression)(0, 0);
97} 96}
98/* expect-1: falls off */ 

cvs diff -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/Attic/c11_generic_expression.exp (switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/Attic/c11_generic_expression.exp 2021/07/11 20:34:05 1.6
+++ src/tests/usr.bin/xlint/lint1/Attic/c11_generic_expression.exp 2021/07/11 20:37:21 1.7
@@ -1,8 +1,6 @@ @@ -1,8 +1,6 @@
1c11_generic_expression.c(29): warning: function classify_type_without_default expects to return value [214] 1c11_generic_expression.c(29): warning: function classify_type_without_default expects to return value [214]
2c11_generic_expression.c(21): warning: argument 'var' unused in function 'classify_type_without_default' [231] 2c11_generic_expression.c(21): warning: argument 'var' unused in function 'classify_type_without_default' [231]
3c11_generic_expression.c(37): warning: argument 'var' unused in function 'classify_type_with_default' [231] 3c11_generic_expression.c(37): warning: argument 'var' unused in function 'classify_type_with_default' [231]
4c11_generic_expression.c(53): warning: argument 'c' unused in function 'classify_char' [231] 4c11_generic_expression.c(53): warning: argument 'c' unused in function 'classify_char' [231]
5c11_generic_expression.c(72): error: syntax error 'second' [249] 5c11_generic_expression.c(72): error: syntax error 'second' [249]
6c11_generic_expression.c(77): warning: function comma_expression falls off bottom without returning value [217] 6c11_generic_expression.c(77): warning: function comma_expression falls off bottom without returning value [217]
7c11_generic_expression.c(96): error: syntax error '(' [249] 
8c11_generic_expression.c(97): warning: function primary_expression falls off bottom without returning value [217] 

cvs diff -r1.316 -r1.317 src/usr.bin/xlint/lint1/cgram.y (switch to unified diff)

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