Thu Jul 15 23:42:50 2021 UTC ()
lint: replace switch statement in dcs_merge_declaration_specifiers

Grouping the rules by their abstract type took a lot of visual space.
Instead, move each of the rules from C11 6.7.2 into its own if
statement, so that the rules almost read like in the standard.

No functional change.


(rillig)
diff -r1.204 -r1.205 src/usr.bin/xlint/lint1/decl.c

cvs diff -r1.204 -r1.205 src/usr.bin/xlint/lint1/decl.c (switch to unified diff)

--- src/usr.bin/xlint/lint1/decl.c 2021/07/15 23:07:05 1.204
+++ src/usr.bin/xlint/lint1/decl.c 2021/07/15 23:42:49 1.205
@@ -1,1826 +1,1809 @@ @@ -1,1826 +1,1809 @@
1/* $NetBSD: decl.c,v 1.204 2021/07/15 23:07:05 rillig Exp $ */ 1/* $NetBSD: decl.c,v 1.205 2021/07/15 23:42:49 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl 5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software 16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement: 17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for 18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project. 19 * The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products 20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission. 21 * derived from this software without specific prior written permission.
22 * 22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40#if defined(__RCSID) && !defined(lint) 40#if defined(__RCSID) && !defined(lint)
41__RCSID("$NetBSD: decl.c,v 1.204 2021/07/15 23:07:05 rillig Exp $"); 41__RCSID("$NetBSD: decl.c,v 1.205 2021/07/15 23:42:49 rillig Exp $");
42#endif 42#endif
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <limits.h> 45#include <limits.h>
46#include <stdlib.h> 46#include <stdlib.h>
47#include <string.h> 47#include <string.h>
48 48
49#include "lint1.h" 49#include "lint1.h"
50 50
51const char *unnamed = "<unnamed>"; 51const char *unnamed = "<unnamed>";
52 52
53/* shared type structures for arithmetic types and void */ 53/* shared type structures for arithmetic types and void */
54static type_t *typetab; 54static type_t *typetab;
55 55
56/* value of next enumerator during declaration of enum types */ 56/* value of next enumerator during declaration of enum types */
57int enumval; 57int enumval;
58 58
59/* 59/*
60 * pointer to top element of a stack which contains information local 60 * pointer to top element of a stack which contains information local
61 * to nested declarations 61 * to nested declarations
62 */ 62 */
63dinfo_t *dcs; 63dinfo_t *dcs;
64 64
65static type_t *tdeferr(type_t *, tspec_t); 65static type_t *tdeferr(type_t *, tspec_t);
66static void settdsym(type_t *, sym_t *); 66static void settdsym(type_t *, sym_t *);
67static tspec_t merge_type_specifiers(tspec_t, tspec_t); 67static tspec_t merge_type_specifiers(tspec_t, tspec_t);
68static void align(int, int); 68static void align(int, int);
69static sym_t *newtag(sym_t *, scl_t, bool, bool); 69static sym_t *newtag(sym_t *, scl_t, bool, bool);
70static bool eqargs(const type_t *, const type_t *, bool *); 70static bool eqargs(const type_t *, const type_t *, bool *);
71static bool mnoarg(const type_t *, bool *); 71static bool mnoarg(const type_t *, bool *);
72static bool check_old_style_definition(sym_t *, sym_t *); 72static bool check_old_style_definition(sym_t *, sym_t *);
73static bool check_prototype_declaration(sym_t *, sym_t *); 73static bool check_prototype_declaration(sym_t *, sym_t *);
74static sym_t *new_style_function(sym_t *, sym_t *); 74static sym_t *new_style_function(sym_t *, sym_t *);
75static void old_style_function(sym_t *, sym_t *); 75static void old_style_function(sym_t *, sym_t *);
76static void declare_external_in_block(sym_t *); 76static void declare_external_in_block(sym_t *);
77static bool check_init(sym_t *); 77static bool check_init(sym_t *);
78static void check_argument_usage(bool, sym_t *); 78static void check_argument_usage(bool, sym_t *);
79static void check_variable_usage(bool, sym_t *); 79static void check_variable_usage(bool, sym_t *);
80static void check_label_usage(sym_t *); 80static void check_label_usage(sym_t *);
81static void check_tag_usage(sym_t *); 81static void check_tag_usage(sym_t *);
82static void check_global_variable(const sym_t *); 82static void check_global_variable(const sym_t *);
83static void check_global_variable_size(const sym_t *); 83static void check_global_variable_size(const sym_t *);
84 84
85/* 85/*
86 * initializes all global vars used in declarations 86 * initializes all global vars used in declarations
87 */ 87 */
88void 88void
89#ifdef __sh3__ 89#ifdef __sh3__
90/* XXX port-sh3/56311 */ 90/* XXX port-sh3/56311 */
91__attribute__((optimize("O0"))) 91__attribute__((optimize("O0")))
92#endif 92#endif
93initdecl(void) 93initdecl(void)
94{ 94{
95 int i; 95 int i;
96 96
97 /* declaration stack */ 97 /* declaration stack */
98 dcs = xcalloc(1, sizeof(*dcs)); 98 dcs = xcalloc(1, sizeof(*dcs));
99 dcs->d_ctx = EXTERN; 99 dcs->d_ctx = EXTERN;
100 dcs->d_ldlsym = &dcs->d_dlsyms; 100 dcs->d_ldlsym = &dcs->d_dlsyms;
101 101
102 /* type information and classification */ 102 /* type information and classification */
103 inittyp(); 103 inittyp();
104 104
105 /* shared type structures */ 105 /* shared type structures */
106 typetab = xcalloc(NTSPEC, sizeof(*typetab)); 106 typetab = xcalloc(NTSPEC, sizeof(*typetab));
107 for (i = 0; i < NTSPEC; i++) 107 for (i = 0; i < NTSPEC; i++)
108 typetab[i].t_tspec = NOTSPEC; 108 typetab[i].t_tspec = NOTSPEC;
109 typetab[BOOL].t_tspec = BOOL; 109 typetab[BOOL].t_tspec = BOOL;
110 typetab[CHAR].t_tspec = CHAR; 110 typetab[CHAR].t_tspec = CHAR;
111 typetab[SCHAR].t_tspec = SCHAR; 111 typetab[SCHAR].t_tspec = SCHAR;
112 typetab[UCHAR].t_tspec = UCHAR; 112 typetab[UCHAR].t_tspec = UCHAR;
113 typetab[SHORT].t_tspec = SHORT; 113 typetab[SHORT].t_tspec = SHORT;
114 typetab[USHORT].t_tspec = USHORT; 114 typetab[USHORT].t_tspec = USHORT;
115 typetab[INT].t_tspec = INT; 115 typetab[INT].t_tspec = INT;
116 typetab[UINT].t_tspec = UINT; 116 typetab[UINT].t_tspec = UINT;
117 typetab[LONG].t_tspec = LONG; 117 typetab[LONG].t_tspec = LONG;
118 typetab[ULONG].t_tspec = ULONG; 118 typetab[ULONG].t_tspec = ULONG;
119 typetab[QUAD].t_tspec = QUAD; 119 typetab[QUAD].t_tspec = QUAD;
120 typetab[UQUAD].t_tspec = UQUAD; 120 typetab[UQUAD].t_tspec = UQUAD;
121 typetab[FLOAT].t_tspec = FLOAT; 121 typetab[FLOAT].t_tspec = FLOAT;
122 typetab[DOUBLE].t_tspec = DOUBLE; 122 typetab[DOUBLE].t_tspec = DOUBLE;
123 typetab[LDOUBLE].t_tspec = LDOUBLE; 123 typetab[LDOUBLE].t_tspec = LDOUBLE;
124 typetab[FCOMPLEX].t_tspec = FCOMPLEX; 124 typetab[FCOMPLEX].t_tspec = FCOMPLEX;
125 typetab[DCOMPLEX].t_tspec = DCOMPLEX; 125 typetab[DCOMPLEX].t_tspec = DCOMPLEX;
126 typetab[LCOMPLEX].t_tspec = LCOMPLEX; 126 typetab[LCOMPLEX].t_tspec = LCOMPLEX;
127 typetab[COMPLEX].t_tspec = COMPLEX; 127 typetab[COMPLEX].t_tspec = COMPLEX;
128 typetab[VOID].t_tspec = VOID; 128 typetab[VOID].t_tspec = VOID;
129 /* 129 /*
130 * Next two are not real types. They are only used by the parser 130 * Next two are not real types. They are only used by the parser
131 * to return keywords "signed" and "unsigned" 131 * to return keywords "signed" and "unsigned"
132 */ 132 */
133 typetab[SIGNED].t_tspec = SIGNED; 133 typetab[SIGNED].t_tspec = SIGNED;
134 typetab[UNSIGN].t_tspec = UNSIGN; 134 typetab[UNSIGN].t_tspec = UNSIGN;
135} 135}
136 136
137/* 137/*
138 * Returns a shared type structure for arithmetic types and void. 138 * Returns a shared type structure for arithmetic types and void.
139 * 139 *
140 * It's important to duplicate this structure (using dup_type() or 140 * It's important to duplicate this structure (using dup_type() or
141 * expr_dup_type()) if it is to be modified (adding qualifiers or anything 141 * expr_dup_type()) if it is to be modified (adding qualifiers or anything
142 * else). 142 * else).
143 */ 143 */
144type_t * 144type_t *
145gettyp(tspec_t t) 145gettyp(tspec_t t)
146{ 146{
147 147
148 /* TODO: make the return type 'const' */ 148 /* TODO: make the return type 'const' */
149 return &typetab[t]; 149 return &typetab[t];
150} 150}
151 151
152type_t * 152type_t *
153dup_type(const type_t *tp) 153dup_type(const type_t *tp)
154{ 154{
155 type_t *ntp; 155 type_t *ntp;
156 156
157 ntp = getblk(sizeof(*ntp)); 157 ntp = getblk(sizeof(*ntp));
158 *ntp = *tp; 158 *ntp = *tp;
159 return ntp; 159 return ntp;
160} 160}
161 161
162/* 162/*
163 * Use expr_dup_type() instead of dup_type() inside expressions (if the 163 * Use expr_dup_type() instead of dup_type() inside expressions (if the
164 * allocated memory should be freed after the expr). 164 * allocated memory should be freed after the expr).
165 */ 165 */
166type_t * 166type_t *
167expr_dup_type(const type_t *tp) 167expr_dup_type(const type_t *tp)
168{ 168{
169 type_t *ntp; 169 type_t *ntp;
170 170
171 ntp = expr_zalloc(sizeof(*ntp)); 171 ntp = expr_zalloc(sizeof(*ntp));
172 *ntp = *tp; 172 *ntp = *tp;
173 return ntp; 173 return ntp;
174} 174}
175 175
176/* 176/*
177 * Returns whether the argument is void or an incomplete array, 177 * Returns whether the argument is void or an incomplete array,
178 * struct, union or enum type. 178 * struct, union or enum type.
179 */ 179 */
180bool 180bool
181is_incomplete(const type_t *tp) 181is_incomplete(const type_t *tp)
182{ 182{
183 tspec_t t; 183 tspec_t t;
184 184
185 if ((t = tp->t_tspec) == VOID) { 185 if ((t = tp->t_tspec) == VOID) {
186 return true; 186 return true;
187 } else if (t == ARRAY) { 187 } else if (t == ARRAY) {
188 return tp->t_incomplete_array; 188 return tp->t_incomplete_array;
189 } else if (t == STRUCT || t == UNION) { 189 } else if (t == STRUCT || t == UNION) {
190 return tp->t_str->sou_incomplete; 190 return tp->t_str->sou_incomplete;
191 } else if (t == ENUM) { 191 } else if (t == ENUM) {
192 return tp->t_enum->en_incomplete; 192 return tp->t_enum->en_incomplete;
193 } 193 }
194 return false; 194 return false;
195} 195}
196 196
197/* 197/*
198 * Mark an array, struct, union or enum type as complete or incomplete. 198 * Mark an array, struct, union or enum type as complete or incomplete.
199 */ 199 */
200void 200void
201setcomplete(type_t *tp, bool complete) 201setcomplete(type_t *tp, bool complete)
202{ 202{
203 tspec_t t; 203 tspec_t t;
204 204
205 lint_assert(tp != NULL); 205 lint_assert(tp != NULL);
206 if ((t = tp->t_tspec) == ARRAY) { 206 if ((t = tp->t_tspec) == ARRAY) {
207 tp->t_incomplete_array = !complete; 207 tp->t_incomplete_array = !complete;
208 } else if (t == STRUCT || t == UNION) { 208 } else if (t == STRUCT || t == UNION) {
209 tp->t_str->sou_incomplete = !complete; 209 tp->t_str->sou_incomplete = !complete;
210 } else { 210 } else {
211 lint_assert(t == ENUM); 211 lint_assert(t == ENUM);
212 tp->t_enum->en_incomplete = !complete; 212 tp->t_enum->en_incomplete = !complete;
213 } 213 }
214} 214}
215 215
216/* 216/*
217 * Remember the storage class of the current declaration in dcs->d_scl 217 * Remember the storage class of the current declaration in dcs->d_scl
218 * (the top element of the declaration stack) and detect multiple 218 * (the top element of the declaration stack) and detect multiple
219 * storage classes. 219 * storage classes.
220 */ 220 */
221void 221void
222add_storage_class(scl_t sc) 222add_storage_class(scl_t sc)
223{ 223{
224 224
225 if (sc == INLINE) { 225 if (sc == INLINE) {
226 if (dcs->d_inline) 226 if (dcs->d_inline)
227 /* duplicate '%s' */ 227 /* duplicate '%s' */
228 warning(10, "inline"); 228 warning(10, "inline");
229 dcs->d_inline = true; 229 dcs->d_inline = true;
230 return; 230 return;
231 } 231 }
232 if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC || 232 if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC ||
233 dcs->d_sign_mod != NOTSPEC || dcs->d_rank_mod != NOTSPEC) { 233 dcs->d_sign_mod != NOTSPEC || dcs->d_rank_mod != NOTSPEC) {
234 /* storage class after type is obsolescent */ 234 /* storage class after type is obsolescent */
235 warning(83); 235 warning(83);
236 } 236 }
237 if (dcs->d_scl == NOSCL) { 237 if (dcs->d_scl == NOSCL) {
238 dcs->d_scl = sc; 238 dcs->d_scl = sc;
239 } else { 239 } else {
240 /* 240 /*
241 * multiple storage classes. An error will be reported in 241 * multiple storage classes. An error will be reported in
242 * end_type(). 242 * end_type().
243 */ 243 */
244 dcs->d_mscl = true; 244 dcs->d_mscl = true;
245 } 245 }
246} 246}
247 247
248/* 248/*
249 * Remember the type, modifier or typedef name returned by the parser 249 * Remember the type, modifier or typedef name returned by the parser
250 * in *dcs (top element of decl stack). This information is used in 250 * in *dcs (top element of decl stack). This information is used in
251 * end_type() to build the type used for all declarators in this 251 * end_type() to build the type used for all declarators in this
252 * declaration. 252 * declaration.
253 * 253 *
254 * If tp->t_typedef is 1, the type comes from a previously defined typename. 254 * If tp->t_typedef is 1, the type comes from a previously defined typename.
255 * Otherwise it comes from a type specifier (int, long, ...) or a 255 * Otherwise it comes from a type specifier (int, long, ...) or a
256 * struct/union/enum tag. 256 * struct/union/enum tag.
257 */ 257 */
258void 258void
259add_type(type_t *tp) 259add_type(type_t *tp)
260{ 260{
261 tspec_t t; 261 tspec_t t;
262#ifdef DEBUG 262#ifdef DEBUG
263 printf("%s: %s\n", __func__, type_name(tp)); 263 printf("%s: %s\n", __func__, type_name(tp));
264#endif 264#endif
265 if (tp->t_typedef) { 265 if (tp->t_typedef) {
266 /* 266 /*
267 * something like "typedef int a; int a b;" 267 * something like "typedef int a; int a b;"
268 * This should not happen with current grammar. 268 * This should not happen with current grammar.
269 */ 269 */
270 lint_assert(dcs->d_type == NULL); 270 lint_assert(dcs->d_type == NULL);
271 lint_assert(dcs->d_abstract_type == NOTSPEC); 271 lint_assert(dcs->d_abstract_type == NOTSPEC);
272 lint_assert(dcs->d_sign_mod == NOTSPEC); 272 lint_assert(dcs->d_sign_mod == NOTSPEC);
273 lint_assert(dcs->d_rank_mod == NOTSPEC); 273 lint_assert(dcs->d_rank_mod == NOTSPEC);
274 274
275 dcs->d_type = tp; 275 dcs->d_type = tp;
276 return; 276 return;
277 } 277 }
278 278
279 t = tp->t_tspec; 279 t = tp->t_tspec;
280 280
281 if (t == STRUCT || t == UNION || t == ENUM) { 281 if (t == STRUCT || t == UNION || t == ENUM) {
282 /* 282 /*
283 * something like "int struct a ..." 283 * something like "int struct a ..."
284 * struct/union/enum with anything else is not allowed 284 * struct/union/enum with anything else is not allowed
285 */ 285 */
286 if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC || 286 if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC ||
287 dcs->d_rank_mod != NOTSPEC || dcs->d_sign_mod != NOTSPEC) { 287 dcs->d_rank_mod != NOTSPEC || dcs->d_sign_mod != NOTSPEC) {
288 /* 288 /*
289 * remember that an error must be reported in 289 * remember that an error must be reported in
290 * end_type(). 290 * end_type().
291 */ 291 */
292 dcs->d_terr = true; 292 dcs->d_terr = true;
293 dcs->d_abstract_type = NOTSPEC; 293 dcs->d_abstract_type = NOTSPEC;
294 dcs->d_sign_mod = NOTSPEC; 294 dcs->d_sign_mod = NOTSPEC;
295 dcs->d_rank_mod = NOTSPEC; 295 dcs->d_rank_mod = NOTSPEC;
296 } 296 }
297 dcs->d_type = tp; 297 dcs->d_type = tp;
298 return; 298 return;
299 } 299 }
300 300
301 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { 301 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
302 /* 302 /*
303 * something like "struct a int" 303 * something like "struct a int"
304 * struct/union/enum with anything else is not allowed 304 * struct/union/enum with anything else is not allowed
305 */ 305 */
306 dcs->d_terr = true; 306 dcs->d_terr = true;
307 return; 307 return;
308 } 308 }
309 309
310 if (t == COMPLEX) { 310 if (t == COMPLEX) {
311 if (dcs->d_complex_mod == FLOAT) 311 if (dcs->d_complex_mod == FLOAT)
312 t = FCOMPLEX; 312 t = FCOMPLEX;
313 else if (dcs->d_complex_mod == DOUBLE) 313 else if (dcs->d_complex_mod == DOUBLE)
314 t = DCOMPLEX; 314 t = DCOMPLEX;
315 else { 315 else {
316 /* invalid type for _Complex */ 316 /* invalid type for _Complex */
317 error(308); 317 error(308);
318 t = DCOMPLEX; /* just as a fallback */ 318 t = DCOMPLEX; /* just as a fallback */
319 } 319 }
320 dcs->d_complex_mod = NOTSPEC; 320 dcs->d_complex_mod = NOTSPEC;
321 } 321 }
322 322
323 if (t == LONG && dcs->d_rank_mod == LONG) { 323 if (t == LONG && dcs->d_rank_mod == LONG) {
324 /* "long long" or "long ... long" */ 324 /* "long long" or "long ... long" */
325 t = QUAD; 325 t = QUAD;
326 dcs->d_rank_mod = NOTSPEC; 326 dcs->d_rank_mod = NOTSPEC;
327 if (!quadflg) 327 if (!quadflg)
328 /* %s C does not support 'long long' */ 328 /* %s C does not support 'long long' */
329 c99ism(265, tflag ? "traditional" : "c89"); 329 c99ism(265, tflag ? "traditional" : "c89");
330 } 330 }
331 331
332 if (dcs->d_type != NULL && dcs->d_type->t_typedef) { 332 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
333 /* something like "typedef int a; a long ..." */ 333 /* something like "typedef int a; a long ..." */
334 dcs->d_type = tdeferr(dcs->d_type, t); 334 dcs->d_type = tdeferr(dcs->d_type, t);
335 return; 335 return;
336 } 336 }
337 337
338 /* now it can be only a combination of arithmetic types and void */ 338 /* now it can be only a combination of arithmetic types and void */
339 if (t == SIGNED || t == UNSIGN) { 339 if (t == SIGNED || t == UNSIGN) {
340 /* 340 /*
341 * remember specifiers "signed" & "unsigned" in 341 * remember specifiers "signed" & "unsigned" in
342 * dcs->d_sign_mod 342 * dcs->d_sign_mod
343 */ 343 */
344 if (dcs->d_sign_mod != NOTSPEC) 344 if (dcs->d_sign_mod != NOTSPEC)
345 /* 345 /*
346 * more than one "signed" and/or "unsigned"; print 346 * more than one "signed" and/or "unsigned"; print
347 * an error in end_type() 347 * an error in end_type()
348 */ 348 */
349 dcs->d_terr = true; 349 dcs->d_terr = true;
350 dcs->d_sign_mod = t; 350 dcs->d_sign_mod = t;
351 } else if (t == SHORT || t == LONG || t == QUAD) { 351 } else if (t == SHORT || t == LONG || t == QUAD) {
352 /* 352 /*
353 * remember specifiers "short", "long" and "long long" in 353 * remember specifiers "short", "long" and "long long" in
354 * dcs->d_rank_mod 354 * dcs->d_rank_mod
355 */ 355 */
356 if (dcs->d_rank_mod != NOTSPEC) 356 if (dcs->d_rank_mod != NOTSPEC)
357 /* more than one, print error in end_type() */ 357 /* more than one, print error in end_type() */
358 dcs->d_terr = true; 358 dcs->d_terr = true;
359 dcs->d_rank_mod = t; 359 dcs->d_rank_mod = t;
360 } else if (t == FLOAT || t == DOUBLE) { 360 } else if (t == FLOAT || t == DOUBLE) {
361 if (dcs->d_rank_mod == NOTSPEC || dcs->d_rank_mod == LONG) { 361 if (dcs->d_rank_mod == NOTSPEC || dcs->d_rank_mod == LONG) {
362 if (dcs->d_complex_mod != NOTSPEC 362 if (dcs->d_complex_mod != NOTSPEC
363 || (t == FLOAT && dcs->d_rank_mod == LONG)) 363 || (t == FLOAT && dcs->d_rank_mod == LONG))
364 dcs->d_terr = true; 364 dcs->d_terr = true;
365 dcs->d_complex_mod = t; 365 dcs->d_complex_mod = t;
366 } else { 366 } else {
367 if (dcs->d_abstract_type != NOTSPEC) 367 if (dcs->d_abstract_type != NOTSPEC)
368 dcs->d_terr = true; 368 dcs->d_terr = true;
369 dcs->d_abstract_type = t; 369 dcs->d_abstract_type = t;
370 } 370 }
371 } else if (t == PTR) { 371 } else if (t == PTR) {
372 dcs->d_type = tp; 372 dcs->d_type = tp;
373 } else { 373 } else {
374 /* 374 /*
375 * remember specifiers "void", "char", "int", 375 * remember specifiers "void", "char", "int",
376 * or "_Complex" in dcs->d_abstract_type 376 * or "_Complex" in dcs->d_abstract_type
377 */ 377 */
378 if (dcs->d_abstract_type != NOTSPEC) 378 if (dcs->d_abstract_type != NOTSPEC)
379 /* more than one, print error in end_type() */ 379 /* more than one, print error in end_type() */
380 dcs->d_terr = true; 380 dcs->d_terr = true;
381 dcs->d_abstract_type = t; 381 dcs->d_abstract_type = t;
382 } 382 }
383} 383}
384 384
385/* 385/*
386 * called if a list of declaration specifiers contains a typedef name 386 * called if a list of declaration specifiers contains a typedef name
387 * and other specifiers (except struct, union, enum, typedef name) 387 * and other specifiers (except struct, union, enum, typedef name)
388 */ 388 */
389static type_t * 389static type_t *
390tdeferr(type_t *td, tspec_t t) 390tdeferr(type_t *td, tspec_t t)
391{ 391{
392 tspec_t t2; 392 tspec_t t2;
393 393
394 t2 = td->t_tspec; 394 t2 = td->t_tspec;
395 395
396 switch (t) { 396 switch (t) {
397 case SIGNED: 397 case SIGNED:
398 case UNSIGN: 398 case UNSIGN:
399 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || 399 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
400 t2 == QUAD) { 400 t2 == QUAD) {
401 if (!tflag) 401 if (!tflag)
402 /* modifying typedef with '%s'; only ... */ 402 /* modifying typedef with '%s'; only ... */
403 warning(5, ttab[t].tt_name); 403 warning(5, ttab[t].tt_name);
404 td = dup_type(gettyp(merge_type_specifiers(t2, t))); 404 td = dup_type(gettyp(merge_type_specifiers(t2, t)));
405 td->t_typedef = true; 405 td->t_typedef = true;
406 return td; 406 return td;
407 } 407 }
408 break; 408 break;
409 case SHORT: 409 case SHORT:
410 if (t2 == INT || t2 == UINT) { 410 if (t2 == INT || t2 == UINT) {
411 /* modifying typedef with '%s'; only qualifiers ... */ 411 /* modifying typedef with '%s'; only qualifiers ... */
412 warning(5, "short"); 412 warning(5, "short");
413 td = dup_type(gettyp(t2 == INT ? SHORT : USHORT)); 413 td = dup_type(gettyp(t2 == INT ? SHORT : USHORT));
414 td->t_typedef = true; 414 td->t_typedef = true;
415 return td; 415 return td;
416 } 416 }
417 break; 417 break;
418 case LONG: 418 case LONG:
419 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG || 419 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
420 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) { 420 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) {
421 /* modifying typedef with '%s'; only qualifiers ... */ 421 /* modifying typedef with '%s'; only qualifiers ... */
422 warning(5, "long"); 422 warning(5, "long");
423 if (t2 == INT) { 423 if (t2 == INT) {
424 td = gettyp(LONG); 424 td = gettyp(LONG);
425 } else if (t2 == UINT) { 425 } else if (t2 == UINT) {
426 td = gettyp(ULONG); 426 td = gettyp(ULONG);
427 } else if (t2 == LONG) { 427 } else if (t2 == LONG) {
428 td = gettyp(QUAD); 428 td = gettyp(QUAD);
429 } else if (t2 == ULONG) { 429 } else if (t2 == ULONG) {
430 td = gettyp(UQUAD); 430 td = gettyp(UQUAD);
431 } else if (t2 == FLOAT) { 431 } else if (t2 == FLOAT) {
432 td = gettyp(DOUBLE); 432 td = gettyp(DOUBLE);
433 } else if (t2 == DOUBLE) { 433 } else if (t2 == DOUBLE) {
434 td = gettyp(LDOUBLE); 434 td = gettyp(LDOUBLE);
435 } else if (t2 == DCOMPLEX) { 435 } else if (t2 == DCOMPLEX) {
436 td = gettyp(LCOMPLEX); 436 td = gettyp(LCOMPLEX);
437 } 437 }
438 td = dup_type(td); 438 td = dup_type(td);
439 td->t_typedef = true; 439 td->t_typedef = true;
440 return td; 440 return td;
441 } 441 }
442 break; 442 break;
443 /* LINTED206: (enumeration values not handled in switch) */ 443 /* LINTED206: (enumeration values not handled in switch) */
444 case NOTSPEC: 444 case NOTSPEC:
445 case USHORT: 445 case USHORT:
446 case UCHAR: 446 case UCHAR:
447 case SCHAR: 447 case SCHAR:
448 case CHAR: 448 case CHAR:
449 case BOOL: 449 case BOOL:
450 case FUNC: 450 case FUNC:
451 case ARRAY: 451 case ARRAY:
452 case PTR: 452 case PTR:
453 case ENUM: 453 case ENUM:
454 case UNION: 454 case UNION:
455 case STRUCT: 455 case STRUCT:
456 case VOID: 456 case VOID:
457 case LDOUBLE: 457 case LDOUBLE:
458 case FLOAT: 458 case FLOAT:
459 case DOUBLE: 459 case DOUBLE:
460 case UQUAD: 460 case UQUAD:
461 case QUAD: 461 case QUAD:
462#ifdef INT128_SIZE 462#ifdef INT128_SIZE
463 case UINT128: 463 case UINT128:
464 case INT128: 464 case INT128:
465#endif 465#endif
466 case ULONG: 466 case ULONG:
467 case UINT: 467 case UINT:
468 case INT: 468 case INT:
469 case FCOMPLEX: 469 case FCOMPLEX:
470 case DCOMPLEX: 470 case DCOMPLEX:
471 case LCOMPLEX: 471 case LCOMPLEX:
472 case COMPLEX: 472 case COMPLEX:
473 break; 473 break;
474 } 474 }
475 475
476 /* Anything other is not accepted. */ 476 /* Anything other is not accepted. */
477 477
478 dcs->d_terr = true; 478 dcs->d_terr = true;
479 return td; 479 return td;
480} 480}
481 481
482/* 482/*
483 * Remember the symbol of a typedef name (2nd arg) in a struct, union 483 * Remember the symbol of a typedef name (2nd arg) in a struct, union
484 * or enum tag if the typedef name is the first defined for this tag. 484 * or enum tag if the typedef name is the first defined for this tag.
485 * 485 *
486 * If the tag is unnamed, the typdef name is used for identification 486 * If the tag is unnamed, the typdef name is used for identification
487 * of this tag in lint2. Although it's possible that more than one typedef 487 * of this tag in lint2. Although it's possible that more than one typedef
488 * name is defined for one tag, the first name defined should be unique 488 * name is defined for one tag, the first name defined should be unique
489 * if the tag is unnamed. 489 * if the tag is unnamed.
490 */ 490 */
491static void 491static void
492settdsym(type_t *tp, sym_t *sym) 492settdsym(type_t *tp, sym_t *sym)
493{ 493{
494 tspec_t t; 494 tspec_t t;
495 495
496 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 496 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
497 if (tp->t_str->sou_first_typedef == NULL) 497 if (tp->t_str->sou_first_typedef == NULL)
498 tp->t_str->sou_first_typedef = sym; 498 tp->t_str->sou_first_typedef = sym;
499 } else if (t == ENUM) { 499 } else if (t == ENUM) {
500 if (tp->t_enum->en_first_typedef == NULL) 500 if (tp->t_enum->en_first_typedef == NULL)
501 tp->t_enum->en_first_typedef = sym; 501 tp->t_enum->en_first_typedef = sym;
502 } 502 }
503} 503}
504 504
505static size_t 505static size_t
506bitfieldsize(sym_t **mem) 506bitfieldsize(sym_t **mem)
507{ 507{
508 size_t len = (*mem)->s_type->t_flen; 508 size_t len = (*mem)->s_type->t_flen;
509 while (*mem != NULL && (*mem)->s_type->t_bitfield) { 509 while (*mem != NULL && (*mem)->s_type->t_bitfield) {
510 len += (*mem)->s_type->t_flen; 510 len += (*mem)->s_type->t_flen;
511 *mem = (*mem)->s_next; 511 *mem = (*mem)->s_next;
512 } 512 }
513 return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE; 513 return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE;
514} 514}
515 515
516static void 516static void
517setpackedsize(type_t *tp) 517setpackedsize(type_t *tp)
518{ 518{
519 struct_or_union *sp; 519 struct_or_union *sp;
520 sym_t *mem; 520 sym_t *mem;
521 521
522 switch (tp->t_tspec) { 522 switch (tp->t_tspec) {
523 case STRUCT: 523 case STRUCT:
524 case UNION: 524 case UNION:
525 sp = tp->t_str; 525 sp = tp->t_str;
526 sp->sou_size_in_bits = 0; 526 sp->sou_size_in_bits = 0;
527 for (mem = sp->sou_first_member; 527 for (mem = sp->sou_first_member;
528 mem != NULL; mem = mem->s_next) { 528 mem != NULL; mem = mem->s_next) {
529 if (mem->s_type->t_bitfield) { 529 if (mem->s_type->t_bitfield) {
530 sp->sou_size_in_bits += bitfieldsize(&mem); 530 sp->sou_size_in_bits += bitfieldsize(&mem);
531 if (mem == NULL) 531 if (mem == NULL)
532 break; 532 break;
533 } 533 }
534 size_t x = (size_t)type_size_in_bits(mem->s_type); 534 size_t x = (size_t)type_size_in_bits(mem->s_type);
535 if (tp->t_tspec == STRUCT) 535 if (tp->t_tspec == STRUCT)
536 sp->sou_size_in_bits += x; 536 sp->sou_size_in_bits += x;
537 else if (x > sp->sou_size_in_bits) 537 else if (x > sp->sou_size_in_bits)
538 sp->sou_size_in_bits = x; 538 sp->sou_size_in_bits = x;
539 } 539 }
540 break; 540 break;
541 default: 541 default:
542 /* %s attribute ignored for %s */ 542 /* %s attribute ignored for %s */
543 warning(326, "packed", type_name(tp)); 543 warning(326, "packed", type_name(tp));
544 break; 544 break;
545 } 545 }
546} 546}
547 547
548void 548void
549addpacked(void) 549addpacked(void)
550{ 550{
551 if (dcs->d_type == NULL) 551 if (dcs->d_type == NULL)
552 dcs->d_packed = true; 552 dcs->d_packed = true;
553 else 553 else
554 setpackedsize(dcs->d_type); 554 setpackedsize(dcs->d_type);
555} 555}
556 556
557void 557void
558add_attr_used(void) 558add_attr_used(void)
559{ 559{
560 dcs->d_used = true; 560 dcs->d_used = true;
561} 561}
562 562
563/* 563/*
564 * Remember a qualifier which is part of the declaration specifiers 564 * Remember a qualifier which is part of the declaration specifiers
565 * (and not the declarator) in the top element of the declaration stack. 565 * (and not the declarator) in the top element of the declaration stack.
566 * Also detect multiple qualifiers of the same kind. 566 * Also detect multiple qualifiers of the same kind.
567 567
568 * The remembered qualifier is used by end_type() to construct the type 568 * The remembered qualifier is used by end_type() to construct the type
569 * for all declarators. 569 * for all declarators.
570 */ 570 */
571void 571void
572add_qualifier(tqual_t q) 572add_qualifier(tqual_t q)
573{ 573{
574 574
575 if (q == CONST) { 575 if (q == CONST) {
576 if (dcs->d_const) { 576 if (dcs->d_const) {
577 /* duplicate '%s' */ 577 /* duplicate '%s' */
578 warning(10, "const"); 578 warning(10, "const");
579 } 579 }
580 dcs->d_const = true; 580 dcs->d_const = true;
581 } else if (q == VOLATILE) { 581 } else if (q == VOLATILE) {
582 if (dcs->d_volatile) { 582 if (dcs->d_volatile) {
583 /* duplicate '%s' */ 583 /* duplicate '%s' */
584 warning(10, "volatile"); 584 warning(10, "volatile");
585 } 585 }
586 dcs->d_volatile = true; 586 dcs->d_volatile = true;
587 } else { 587 } else {
588 lint_assert(q == RESTRICT || q == THREAD); 588 lint_assert(q == RESTRICT || q == THREAD);
589 /* Silently ignore these qualifiers. */ 589 /* Silently ignore these qualifiers. */
590 } 590 }
591} 591}
592 592
593/* 593/*
594 * Go to the next declaration level (structs, nested structs, blocks, 594 * Go to the next declaration level (structs, nested structs, blocks,
595 * argument declaration lists ...) 595 * argument declaration lists ...)
596 */ 596 */
597void 597void
598begin_declaration_level(scl_t sc) 598begin_declaration_level(scl_t sc)
599{ 599{
600 dinfo_t *di; 600 dinfo_t *di;
601 601
602 /* put a new element on the declaration stack */ 602 /* put a new element on the declaration stack */
603 di = xcalloc(1, sizeof(*di)); 603 di = xcalloc(1, sizeof(*di));
604 di->d_next = dcs; 604 di->d_next = dcs;
605 dcs = di; 605 dcs = di;
606 di->d_ctx = sc; 606 di->d_ctx = sc;
607 di->d_ldlsym = &di->d_dlsyms; 607 di->d_ldlsym = &di->d_dlsyms;
608 if (dflag) 608 if (dflag)
609 (void)printf("%s(%p %s)\n", __func__, dcs, scl_name(sc)); 609 (void)printf("%s(%p %s)\n", __func__, dcs, scl_name(sc));
610} 610}
611 611
612/* 612/*
613 * Go back to previous declaration level 613 * Go back to previous declaration level
614 */ 614 */
615void 615void
616end_declaration_level(void) 616end_declaration_level(void)
617{ 617{
618 dinfo_t *di; 618 dinfo_t *di;
619 619
620 if (dflag) 620 if (dflag)
621 (void)printf("%s(%p %s)\n", 621 (void)printf("%s(%p %s)\n",
622 __func__, dcs, scl_name(dcs->d_ctx)); 622 __func__, dcs, scl_name(dcs->d_ctx));
623 623
624 lint_assert(dcs->d_next != NULL); 624 lint_assert(dcs->d_next != NULL);
625 di = dcs; 625 di = dcs;
626 dcs = di->d_next; 626 dcs = di->d_next;
627 switch (di->d_ctx) { 627 switch (di->d_ctx) {
628 case MOS: 628 case MOS:
629 case MOU: 629 case MOU:
630 case CTCONST: 630 case CTCONST:
631 /* 631 /*
632 * Symbols declared in (nested) structs or enums are 632 * Symbols declared in (nested) structs or enums are
633 * part of the next level (they are removed from the 633 * part of the next level (they are removed from the
634 * symbol table if the symbols of the outer level are 634 * symbol table if the symbols of the outer level are
635 * removed). 635 * removed).
636 */ 636 */
637 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 637 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
638 dcs->d_ldlsym = di->d_ldlsym; 638 dcs->d_ldlsym = di->d_ldlsym;
639 break; 639 break;
640 case ARG: 640 case ARG:
641 /* 641 /*
642 * All symbols in dcs->d_dlsyms are introduced in old style 642 * All symbols in dcs->d_dlsyms are introduced in old style
643 * argument declarations (it's not clean, but possible). 643 * argument declarations (it's not clean, but possible).
644 * They are appended to the list of symbols declared in 644 * They are appended to the list of symbols declared in
645 * an old style argument identifier list or a new style 645 * an old style argument identifier list or a new style
646 * parameter type list. 646 * parameter type list.
647 */ 647 */
648 if (di->d_dlsyms != NULL) { 648 if (di->d_dlsyms != NULL) {
649 *di->d_ldlsym = dcs->d_func_proto_syms; 649 *di->d_ldlsym = dcs->d_func_proto_syms;
650 dcs->d_func_proto_syms = di->d_dlsyms; 650 dcs->d_func_proto_syms = di->d_dlsyms;
651 } 651 }
652 break; 652 break;
653 case ABSTRACT: 653 case ABSTRACT:
654 /* 654 /*
655 * casts and sizeof 655 * casts and sizeof
656 * Append all symbols declared in the abstract declaration 656 * Append all symbols declared in the abstract declaration
657 * to the list of symbols declared in the surrounding 657 * to the list of symbols declared in the surrounding
658 * declaration or block. 658 * declaration or block.
659 * XXX I'm not sure whether they should be removed from the 659 * XXX I'm not sure whether they should be removed from the
660 * symbol table now or later. 660 * symbol table now or later.
661 */ 661 */
662 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 662 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
663 dcs->d_ldlsym = di->d_ldlsym; 663 dcs->d_ldlsym = di->d_ldlsym;
664 break; 664 break;
665 case AUTO: 665 case AUTO:
666 /* check usage of local vars */ 666 /* check usage of local vars */
667 check_usage(di); 667 check_usage(di);
668 /* FALLTHROUGH */ 668 /* FALLTHROUGH */
669 case PROTO_ARG: 669 case PROTO_ARG:
670 /* usage of arguments will be checked by funcend() */ 670 /* usage of arguments will be checked by funcend() */
671 rmsyms(di->d_dlsyms); 671 rmsyms(di->d_dlsyms);
672 break; 672 break;
673 case EXTERN: 673 case EXTERN:
674 /* there is nothing after external declarations */ 674 /* there is nothing after external declarations */
675 /* FALLTHROUGH */ 675 /* FALLTHROUGH */
676 default: 676 default:
677 lint_assert(/*CONSTCOND*/false); 677 lint_assert(/*CONSTCOND*/false);
678 } 678 }
679 free(di); 679 free(di);
680} 680}
681 681
682/* 682/*
683 * Set flag d_asm in all declaration stack elements up to the 683 * Set flag d_asm in all declaration stack elements up to the
684 * outermost one. 684 * outermost one.
685 * 685 *
686 * This is used to mark compound statements which have, possibly in 686 * This is used to mark compound statements which have, possibly in
687 * nested compound statements, asm statements. For these compound 687 * nested compound statements, asm statements. For these compound
688 * statements no warnings about unused or uninitialized variables are 688 * statements no warnings about unused or uninitialized variables are
689 * printed. 689 * printed.
690 * 690 *
691 * There is no need to clear d_asm in dinfo structs with context AUTO, 691 * There is no need to clear d_asm in dinfo structs with context AUTO,
692 * because these structs are freed at the end of the compound statement. 692 * because these structs are freed at the end of the compound statement.
693 * But it must be cleared in the outermost dinfo struct, which has 693 * But it must be cleared in the outermost dinfo struct, which has
694 * context EXTERN. This could be done in begin_type() and would work for C90, 694 * context EXTERN. This could be done in begin_type() and would work for C90,
695 * but not for C99 or C++ (due to mixed statements and declarations). Thus 695 * but not for C99 or C++ (due to mixed statements and declarations). Thus
696 * we clear it in global_clean_up_decl(), which is used to do some cleanup 696 * we clear it in global_clean_up_decl(), which is used to do some cleanup
697 * after global declarations/definitions. 697 * after global declarations/definitions.
698 */ 698 */
699void 699void
700setasm(void) 700setasm(void)
701{ 701{
702 dinfo_t *di; 702 dinfo_t *di;
703 703
704 for (di = dcs; di != NULL; di = di->d_next) 704 for (di = dcs; di != NULL; di = di->d_next)
705 di->d_asm = true; 705 di->d_asm = true;
706} 706}
707 707
708/* 708/*
709 * Clean all elements of the top element of declaration stack which 709 * Clean all elements of the top element of declaration stack which
710 * will be used by the next declaration 710 * will be used by the next declaration
711 */ 711 */
712void 712void
713begin_type(void) 713begin_type(void)
714{ 714{
715 715
716 dcs->d_abstract_type = NOTSPEC; 716 dcs->d_abstract_type = NOTSPEC;
717 dcs->d_complex_mod = NOTSPEC; 717 dcs->d_complex_mod = NOTSPEC;
718 dcs->d_sign_mod = NOTSPEC; 718 dcs->d_sign_mod = NOTSPEC;
719 dcs->d_rank_mod = NOTSPEC; 719 dcs->d_rank_mod = NOTSPEC;
720 dcs->d_scl = NOSCL; 720 dcs->d_scl = NOSCL;
721 dcs->d_type = NULL; 721 dcs->d_type = NULL;
722 dcs->d_const = false; 722 dcs->d_const = false;
723 dcs->d_volatile = false; 723 dcs->d_volatile = false;
724 dcs->d_inline = false; 724 dcs->d_inline = false;
725 dcs->d_mscl = false; 725 dcs->d_mscl = false;
726 dcs->d_terr = false; 726 dcs->d_terr = false;
727 dcs->d_nonempty_decl = false; 727 dcs->d_nonempty_decl = false;
728 dcs->d_notyp = false; 728 dcs->d_notyp = false;
729} 729}
730 730
731static void 731static void
732dcs_adjust_storage_class(void) 732dcs_adjust_storage_class(void)
733{ 733{
734 if (dcs->d_ctx == EXTERN) { 734 if (dcs->d_ctx == EXTERN) {
735 if (dcs->d_scl == REG || dcs->d_scl == AUTO) { 735 if (dcs->d_scl == REG || dcs->d_scl == AUTO) {
736 /* illegal storage class */ 736 /* illegal storage class */
737 error(8); 737 error(8);
738 dcs->d_scl = NOSCL; 738 dcs->d_scl = NOSCL;
739 } 739 }
740 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) { 740 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) {
741 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) { 741 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) {
742 /* only register valid as formal parameter storage... */ 742 /* only register valid as formal parameter storage... */
743 error(9); 743 error(9);
744 dcs->d_scl = NOSCL; 744 dcs->d_scl = NOSCL;
745 } 745 }
746 } 746 }
747} 747}
748 748
749/* Merge the declaration specifiers from dcs into dcs->d_type. */ 749/*
 750 * Merge the declaration specifiers from dcs into dcs->d_type.
 751 *
 752 * See C99 6.7.2 "Type specifiers".
 753 */
750static void 754static void
751dcs_merge_declaration_specifiers(void) 755dcs_merge_declaration_specifiers(void)
752{ 756{
753 tspec_t t, s, l, c; 757 tspec_t t, s, l, c;
754 type_t *tp; 758 type_t *tp;
755 759
756 t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */ 760 t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */
757 c = dcs->d_complex_mod; /* FLOAT or DOUBLE */ 761 c = dcs->d_complex_mod; /* FLOAT or DOUBLE */
758 s = dcs->d_sign_mod; /* SIGNED or UNSIGN */ 762 s = dcs->d_sign_mod; /* SIGNED or UNSIGN */
759 l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */ 763 l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */
760 tp = dcs->d_type; 764 tp = dcs->d_type;
761 765
762#ifdef DEBUG 766#ifdef DEBUG
763 printf("%s: %s\n", __func__, type_name(tp)); 767 printf("%s: %s\n", __func__, type_name(tp));
764#endif 768#endif
765 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC && 769 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
766 tp == NULL) 770 tp == NULL)
767 dcs->d_notyp = true; 771 dcs->d_notyp = true;
768 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) && 772 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
769 tp == NULL) 773 tp == NULL)
770 t = c; 774 t = c;
771 775
772 if (tp != NULL) { 776 if (tp != NULL) {
773 lint_assert(t == NOTSPEC); 777 lint_assert(t == NOTSPEC);
774 lint_assert(s == NOTSPEC); 778 lint_assert(s == NOTSPEC);
775 lint_assert(l == NOTSPEC); 779 lint_assert(l == NOTSPEC);
776 return; 780 return;
777 } 781 }
778 782
779 switch (t) { 783 if (t == NOTSPEC)
780 case BOOL: 
781 break; 
782 case NOTSPEC: 
783 t = INT; 784 t = INT;
784 /* FALLTHROUGH */ 785 if (s == NOTSPEC && t == INT)
785 case INT: 786 s = SIGNED;
786 if (s == NOTSPEC) 787 if (l != NOTSPEC && t == CHAR) {
787 s = SIGNED; 788 dcs->d_terr = true;
788 break; 789 l = NOTSPEC;
789 case CHAR: 790 }
790 if (l != NOTSPEC) { 791 if (l == LONG && t == FLOAT) {
791 dcs->d_terr = true; 792 l = NOTSPEC;
792 l = NOTSPEC; 793 t = DOUBLE;
793 } 794 if (!tflag)
794 break; 795 /* use 'double' instead of 'long float' */
795 case FLOAT: 796 warning(6);
796 if (l == LONG) { 797 }
797 l = NOTSPEC; 798 if ((l == LONG && t == DOUBLE) || t == LDOUBLE) {
798 t = DOUBLE; 
799 if (!tflag) 
800 /* use 'double' instead of 'long float' */ 
801 warning(6); 
802 } 
803 break; 
804 case DOUBLE: 
805 if (l != LONG) 
806 break; 
807 /* FALLTHROUGH */ 
808 case LDOUBLE: 
809 l = NOTSPEC; 799 l = NOTSPEC;
810 t = LDOUBLE; 800 t = LDOUBLE;
811 if (tflag) 
812 /* 'long double' is illegal in traditional C */ 
813 warning(266); 
814 break; 
815 case DCOMPLEX: 
816 if (l == LONG) { 
817 l = NOTSPEC; 
818 t = LCOMPLEX; 
819 } 
820 break; 
821 case VOID: 
822 case FCOMPLEX: 
823 case LCOMPLEX: 
824 break; 
825 default: 
826 lint_assert(is_integer(t)); 
827 } 801 }
 802 if (t == LDOUBLE && tflag) {
 803 /* 'long double' is illegal in traditional C */
 804 warning(266);
 805 }
 806 if (l == LONG && t == DCOMPLEX) {
 807 l = NOTSPEC;
 808 t = LCOMPLEX;
 809 }
 810
828 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { 811 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
829 dcs->d_terr = true; 812 dcs->d_terr = true;
830 l = s = NOTSPEC; 813 l = s = NOTSPEC;
831 } 814 }
832 if (l != NOTSPEC) 815 if (l != NOTSPEC)
833 t = l; 816 t = l;
834 dcs->d_type = gettyp(merge_type_specifiers(t, s)); 817 dcs->d_type = gettyp(merge_type_specifiers(t, s));
835} 818}
836 819
837/* 820/*
838 * Create a type structure from the information gathered in 821 * Create a type structure from the information gathered in
839 * the declaration stack. 822 * the declaration stack.
840 * Complain about storage classes which are not possible in current 823 * Complain about storage classes which are not possible in current
841 * context. 824 * context.
842 */ 825 */
843void 826void
844end_type(void) 827end_type(void)
845{ 828{
846 829
847 dcs_merge_declaration_specifiers(); 830 dcs_merge_declaration_specifiers();
848 831
849 if (dcs->d_mscl) { 832 if (dcs->d_mscl) {
850 /* only one storage class allowed */ 833 /* only one storage class allowed */
851 error(7); 834 error(7);
852 } 835 }
853 if (dcs->d_terr) { 836 if (dcs->d_terr) {
854 /* illegal type combination */ 837 /* illegal type combination */
855 error(4); 838 error(4);
856 } 839 }
857 840
858 dcs_adjust_storage_class(); 841 dcs_adjust_storage_class();
859 842
860 if (dcs->d_const && dcs->d_type->t_const) { 843 if (dcs->d_const && dcs->d_type->t_const) {
861 lint_assert(dcs->d_type->t_typedef); 844 lint_assert(dcs->d_type->t_typedef);
862 /* typedef already qualified with '%s' */ 845 /* typedef already qualified with '%s' */
863 warning(68, "const"); 846 warning(68, "const");
864 } 847 }
865 if (dcs->d_volatile && dcs->d_type->t_volatile) { 848 if (dcs->d_volatile && dcs->d_type->t_volatile) {
866 lint_assert(dcs->d_type->t_typedef); 849 lint_assert(dcs->d_type->t_typedef);
867 /* typedef already qualified with '%s' */ 850 /* typedef already qualified with '%s' */
868 warning(68, "volatile"); 851 warning(68, "volatile");
869 } 852 }
870 853
871 if (dcs->d_const || dcs->d_volatile) { 854 if (dcs->d_const || dcs->d_volatile) {
872 dcs->d_type = dup_type(dcs->d_type); 855 dcs->d_type = dup_type(dcs->d_type);
873 dcs->d_type->t_const |= dcs->d_const; 856 dcs->d_type->t_const |= dcs->d_const;
874 dcs->d_type->t_volatile |= dcs->d_volatile; 857 dcs->d_type->t_volatile |= dcs->d_volatile;
875 } 858 }
876} 859}
877 860
878/* 861/*
879 * Merge type specifiers (char, ..., long long, signed, unsigned). 862 * Merge type specifiers (char, ..., long long, signed, unsigned).
880 */ 863 */
881static tspec_t 864static tspec_t
882merge_type_specifiers(tspec_t t, tspec_t s) 865merge_type_specifiers(tspec_t t, tspec_t s)
883{ 866{
884 867
885 if (s != SIGNED && s != UNSIGN) 868 if (s != SIGNED && s != UNSIGN)
886 return t; 869 return t;
887 870
888 if (t == CHAR) 871 if (t == CHAR)
889 return s == SIGNED ? SCHAR : UCHAR; 872 return s == SIGNED ? SCHAR : UCHAR;
890 if (t == SHORT) 873 if (t == SHORT)
891 return s == SIGNED ? SHORT : USHORT; 874 return s == SIGNED ? SHORT : USHORT;
892 if (t == INT) 875 if (t == INT)
893 return s == SIGNED ? INT : UINT; 876 return s == SIGNED ? INT : UINT;
894 if (t == LONG) 877 if (t == LONG)
895 return s == SIGNED ? LONG : ULONG; 878 return s == SIGNED ? LONG : ULONG;
896 if (t == QUAD) 879 if (t == QUAD)
897 return s == SIGNED ? QUAD : UQUAD; 880 return s == SIGNED ? QUAD : UQUAD;
898 return t; 881 return t;
899} 882}
900 883
901/* 884/*
902 * Return the length of a type in bits. 885 * Return the length of a type in bits.
903 * 886 *
904 * Printing a message if the outermost dimension of an array is 0 must 887 * Printing a message if the outermost dimension of an array is 0 must
905 * be done by the caller. All other problems are reported by length() 888 * be done by the caller. All other problems are reported by length()
906 * if name is not NULL. 889 * if name is not NULL.
907 */ 890 */
908int 891int
909length(const type_t *tp, const char *name) 892length(const type_t *tp, const char *name)
910{ 893{
911 int elem, elsz; 894 int elem, elsz;
912 895
913 elem = 1; 896 elem = 1;
914 while (tp != NULL && tp->t_tspec == ARRAY) { 897 while (tp != NULL && tp->t_tspec == ARRAY) {
915 elem *= tp->t_dim; 898 elem *= tp->t_dim;
916 tp = tp->t_subt; 899 tp = tp->t_subt;
917 } 900 }
918 if (tp == NULL) 901 if (tp == NULL)
919 return -1; 902 return -1;
920 903
921 switch (tp->t_tspec) { 904 switch (tp->t_tspec) {
922 case FUNC: 905 case FUNC:
923 /* compiler takes size of function */ 906 /* compiler takes size of function */
924 INTERNAL_ERROR("%s", msgs[12]); 907 INTERNAL_ERROR("%s", msgs[12]);
925 /* NOTREACHED */ 908 /* NOTREACHED */
926 case STRUCT: 909 case STRUCT:
927 case UNION: 910 case UNION:
928 if (is_incomplete(tp) && name != NULL) { 911 if (is_incomplete(tp) && name != NULL) {
929 /* '%s' has incomplete type '%s' */ 912 /* '%s' has incomplete type '%s' */
930 error(31, name, type_name(tp)); 913 error(31, name, type_name(tp));
931 } 914 }
932 elsz = tp->t_str->sou_size_in_bits; 915 elsz = tp->t_str->sou_size_in_bits;
933 break; 916 break;
934 case ENUM: 917 case ENUM:
935 if (is_incomplete(tp) && name != NULL) { 918 if (is_incomplete(tp) && name != NULL) {
936 /* incomplete enum type: %s */ 919 /* incomplete enum type: %s */
937 warning(13, name); 920 warning(13, name);
938 } 921 }
939 /* FALLTHROUGH */ 922 /* FALLTHROUGH */
940 default: 923 default:
941 elsz = size_in_bits(tp->t_tspec); 924 elsz = size_in_bits(tp->t_tspec);
942 if (elsz <= 0) 925 if (elsz <= 0)
943 INTERNAL_ERROR("length(%d)", elsz); 926 INTERNAL_ERROR("length(%d)", elsz);
944 break; 927 break;
945 } 928 }
946 return elem * elsz; 929 return elem * elsz;
947} 930}
948 931
949int 932int
950alignment_in_bits(const type_t *tp) 933alignment_in_bits(const type_t *tp)
951{ 934{
952 size_t a; 935 size_t a;
953 tspec_t t; 936 tspec_t t;
954 937
955 while (tp != NULL && tp->t_tspec == ARRAY) 938 while (tp != NULL && tp->t_tspec == ARRAY)
956 tp = tp->t_subt; 939 tp = tp->t_subt;
957 940
958 if (tp == NULL) 941 if (tp == NULL)
959 return -1; 942 return -1;
960 943
961 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 944 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
962 a = tp->t_str->sou_align_in_bits; 945 a = tp->t_str->sou_align_in_bits;
963 } else if (t == FUNC) { 946 } else if (t == FUNC) {
964 /* compiler takes alignment of function */ 947 /* compiler takes alignment of function */
965 error(14); 948 error(14);
966 a = WORST_ALIGN(1) * CHAR_SIZE; 949 a = WORST_ALIGN(1) * CHAR_SIZE;
967 } else { 950 } else {
968 if ((a = size_in_bits(t)) == 0) { 951 if ((a = size_in_bits(t)) == 0) {
969 a = CHAR_SIZE; 952 a = CHAR_SIZE;
970 } else if (a > WORST_ALIGN(1) * CHAR_SIZE) { 953 } else if (a > WORST_ALIGN(1) * CHAR_SIZE) {
971 a = WORST_ALIGN(1) * CHAR_SIZE; 954 a = WORST_ALIGN(1) * CHAR_SIZE;
972 } 955 }
973 } 956 }
974 lint_assert(a >= CHAR_SIZE); 957 lint_assert(a >= CHAR_SIZE);
975 lint_assert(a <= WORST_ALIGN(1) * CHAR_SIZE); 958 lint_assert(a <= WORST_ALIGN(1) * CHAR_SIZE);
976 return a; 959 return a;
977} 960}
978 961
979/* 962/*
980 * Concatenate two lists of symbols by s_next. Used by declarations of 963 * Concatenate two lists of symbols by s_next. Used by declarations of
981 * struct/union/enum elements and parameters. 964 * struct/union/enum elements and parameters.
982 */ 965 */
983sym_t * 966sym_t *
984lnklst(sym_t *l1, sym_t *l2) 967lnklst(sym_t *l1, sym_t *l2)
985{ 968{
986 sym_t *l; 969 sym_t *l;
987 970
988 if ((l = l1) == NULL) 971 if ((l = l1) == NULL)
989 return l2; 972 return l2;
990 while (l1->s_next != NULL) 973 while (l1->s_next != NULL)
991 l1 = l1->s_next; 974 l1 = l1->s_next;
992 l1->s_next = l2; 975 l1->s_next = l2;
993 return l; 976 return l;
994} 977}
995 978
996/* 979/*
997 * Check if the type of the given symbol is valid and print an error 980 * Check if the type of the given symbol is valid and print an error
998 * message if it is not. 981 * message if it is not.
999 * 982 *
1000 * Invalid types are: 983 * Invalid types are:
1001 * - arrays of incomplete types or functions 984 * - arrays of incomplete types or functions
1002 * - functions returning arrays or functions 985 * - functions returning arrays or functions
1003 * - void types other than type of function or pointer 986 * - void types other than type of function or pointer
1004 */ 987 */
1005void 988void
1006check_type(sym_t *sym) 989check_type(sym_t *sym)
1007{ 990{
1008 tspec_t to, t; 991 tspec_t to, t;
1009 type_t **tpp, *tp; 992 type_t **tpp, *tp;
1010 993
1011 tpp = &sym->s_type; 994 tpp = &sym->s_type;
1012 to = NOTSPEC; 995 to = NOTSPEC;
1013 while ((tp = *tpp) != NULL) { 996 while ((tp = *tpp) != NULL) {
1014 t = tp->t_tspec; 997 t = tp->t_tspec;
1015 /* 998 /*
1016 * If this is the type of an old style function definition, 999 * If this is the type of an old style function definition,
1017 * a better warning is printed in funcdef(). 1000 * a better warning is printed in funcdef().
1018 */ 1001 */
1019 if (t == FUNC && !tp->t_proto && 1002 if (t == FUNC && !tp->t_proto &&
1020 !(to == NOTSPEC && sym->s_osdef)) { 1003 !(to == NOTSPEC && sym->s_osdef)) {
1021 if (sflag && hflag) 1004 if (sflag && hflag)
1022 /* function declaration is not a prototype */ 1005 /* function declaration is not a prototype */
1023 warning(287); 1006 warning(287);
1024 } 1007 }
1025 if (to == FUNC) { 1008 if (to == FUNC) {
1026 if (t == FUNC || t == ARRAY) { 1009 if (t == FUNC || t == ARRAY) {
1027 /* function returns illegal type */ 1010 /* function returns illegal type */
1028 error(15); 1011 error(15);
1029 if (t == FUNC) { 1012 if (t == FUNC) {
1030 *tpp = derive_type(*tpp, PTR); 1013 *tpp = derive_type(*tpp, PTR);
1031 } else { 1014 } else {
1032 *tpp = derive_type((*tpp)->t_subt, PTR); 1015 *tpp = derive_type((*tpp)->t_subt, PTR);
1033 } 1016 }
1034 return; 1017 return;
1035 } else if (tp->t_const || tp->t_volatile) { 1018 } else if (tp->t_const || tp->t_volatile) {
1036 if (sflag) { /* XXX or better !tflag ? */ 1019 if (sflag) { /* XXX or better !tflag ? */
1037 /* function cannot return const... */ 1020 /* function cannot return const... */
1038 warning(228); 1021 warning(228);
1039 } 1022 }
1040 } 1023 }
1041 } if (to == ARRAY) { 1024 } if (to == ARRAY) {
1042 if (t == FUNC) { 1025 if (t == FUNC) {
1043 /* array of function is illegal */ 1026 /* array of function is illegal */
1044 error(16); 1027 error(16);
1045 *tpp = gettyp(INT); 1028 *tpp = gettyp(INT);
1046 return; 1029 return;
1047 } else if (t == ARRAY && tp->t_dim == 0) { 1030 } else if (t == ARRAY && tp->t_dim == 0) {
1048 /* null dimension */ 1031 /* null dimension */
1049 error(17); 1032 error(17);
1050 return; 1033 return;
1051 } else if (t == VOID) { 1034 } else if (t == VOID) {
1052 /* illegal use of 'void' */ 1035 /* illegal use of 'void' */
1053 error(18); 1036 error(18);
1054 *tpp = gettyp(INT); 1037 *tpp = gettyp(INT);
1055#if 0 /* errors are produced by length() */ 1038#if 0 /* errors are produced by length() */
1056 } else if (is_incomplete(tp)) { 1039 } else if (is_incomplete(tp)) {
1057 /* array of incomplete type */ 1040 /* array of incomplete type */
1058 if (sflag) { 1041 if (sflag) {
1059 /* array of incomplete type */ 1042 /* array of incomplete type */
1060 error(301); 1043 error(301);
1061 } else { 1044 } else {
1062 /* array of incomplete type */ 1045 /* array of incomplete type */
1063 warning(301); 1046 warning(301);
1064 } 1047 }
1065#endif 1048#endif
1066 } 1049 }
1067 } else if (to == NOTSPEC && t == VOID) { 1050 } else if (to == NOTSPEC && t == VOID) {
1068 if (dcs->d_ctx == PROTO_ARG) { 1051 if (dcs->d_ctx == PROTO_ARG) {
1069 if (sym->s_scl != ABSTRACT) { 1052 if (sym->s_scl != ABSTRACT) {
1070 lint_assert(sym->s_name != unnamed); 1053 lint_assert(sym->s_name != unnamed);
1071 /* void param. cannot have name: %s */ 1054 /* void param. cannot have name: %s */
1072 error(61, sym->s_name); 1055 error(61, sym->s_name);
1073 *tpp = gettyp(INT); 1056 *tpp = gettyp(INT);
1074 } 1057 }
1075 } else if (dcs->d_ctx == ABSTRACT) { 1058 } else if (dcs->d_ctx == ABSTRACT) {
1076 /* ok */ 1059 /* ok */
1077 } else if (sym->s_scl != TYPEDEF) { 1060 } else if (sym->s_scl != TYPEDEF) {
1078 /* void type for '%s' */ 1061 /* void type for '%s' */
1079 error(19, sym->s_name); 1062 error(19, sym->s_name);
1080 *tpp = gettyp(INT); 1063 *tpp = gettyp(INT);
1081 } 1064 }
1082 } 1065 }
1083 if (t == VOID && to != PTR) { 1066 if (t == VOID && to != PTR) {
1084 if (tp->t_const || tp->t_volatile) { 1067 if (tp->t_const || tp->t_volatile) {
1085 /* inappropriate qualifiers with 'void' */ 1068 /* inappropriate qualifiers with 'void' */
1086 warning(69); 1069 warning(69);
1087 tp->t_const = tp->t_volatile = false; 1070 tp->t_const = tp->t_volatile = false;
1088 } 1071 }
1089 } 1072 }
1090 tpp = &tp->t_subt; 1073 tpp = &tp->t_subt;
1091 to = t; 1074 to = t;
1092 } 1075 }
1093} 1076}
1094 1077
1095/* 1078/*
1096 * In traditional C, the only portable type for bit-fields is unsigned int. 1079 * In traditional C, the only portable type for bit-fields is unsigned int.
1097 * 1080 *
1098 * In C90, the only allowed types for bit-fields are int, signed int and 1081 * In C90, the only allowed types for bit-fields are int, signed int and
1099 * unsigned int (3.5.2.1). There is no mention of implementation-defined 1082 * unsigned int (3.5.2.1). There is no mention of implementation-defined
1100 * types. 1083 * types.
1101 * 1084 *
1102 * In C99, the only portable types for bit-fields are _Bool, signed int and 1085 * In C99, the only portable types for bit-fields are _Bool, signed int and
1103 * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other 1086 * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other
1104 * implementation-defined type". 1087 * implementation-defined type".
1105 */ 1088 */
1106static void 1089static void
1107check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t) 1090check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t)
1108{ 1091{
1109 type_t *tp = *inout_tp; 1092 type_t *tp = *inout_tp;
1110 tspec_t t = *inout_t; 1093 tspec_t t = *inout_t;
1111 1094
1112 if (t == CHAR || t == UCHAR || t == SCHAR || 1095 if (t == CHAR || t == UCHAR || t == SCHAR ||
1113 t == SHORT || t == USHORT || t == ENUM) { 1096 t == SHORT || t == USHORT || t == ENUM) {
1114 if (!bitfieldtype_ok) { 1097 if (!bitfieldtype_ok) {
1115 if (sflag) { 1098 if (sflag) {
1116 /* bit-field type '%s' invalid in ANSI C */ 1099 /* bit-field type '%s' invalid in ANSI C */
1117 warning(273, type_name(tp)); 1100 warning(273, type_name(tp));
1118 } else if (pflag) { 1101 } else if (pflag) {
1119 /* nonportable bit-field type '%s' */ 1102 /* nonportable bit-field type '%s' */
1120 warning(34, type_name(tp)); 1103 warning(34, type_name(tp));
1121 } 1104 }
1122 } 1105 }
1123 } else if (t == INT && dcs->d_sign_mod == NOTSPEC) { 1106 } else if (t == INT && dcs->d_sign_mod == NOTSPEC) {
1124 if (pflag && !bitfieldtype_ok) { 1107 if (pflag && !bitfieldtype_ok) {
1125 /* bit-field of type plain 'int' has ... */ 1108 /* bit-field of type plain 'int' has ... */
1126 warning(344); 1109 warning(344);
1127 } 1110 }
1128 } else if (t != INT && t != UINT && t != BOOL) { 1111 } else if (t != INT && t != UINT && t != BOOL) {
1129 /* 1112 /*
1130 * Non-integer types are always illegal for bitfields, 1113 * Non-integer types are always illegal for bitfields,
1131 * regardless of BITFIELDTYPE. Integer types not dealt with 1114 * regardless of BITFIELDTYPE. Integer types not dealt with
1132 * above are okay only if BITFIELDTYPE is in effect. 1115 * above are okay only if BITFIELDTYPE is in effect.
1133 */ 1116 */
1134 if (!(bitfieldtype_ok || gflag) || !is_integer(t)) { 1117 if (!(bitfieldtype_ok || gflag) || !is_integer(t)) {
1135 /* illegal bit-field type '%s' */ 1118 /* illegal bit-field type '%s' */
1136 warning(35, type_name(tp)); 1119 warning(35, type_name(tp));
1137 int sz = tp->t_flen; 1120 int sz = tp->t_flen;
1138 dsym->s_type = tp = dup_type(gettyp(t = INT)); 1121 dsym->s_type = tp = dup_type(gettyp(t = INT));
1139 if ((tp->t_flen = sz) > size_in_bits(t)) 1122 if ((tp->t_flen = sz) > size_in_bits(t))
1140 tp->t_flen = size_in_bits(t); 1123 tp->t_flen = size_in_bits(t);
1141 *inout_t = t; 1124 *inout_t = t;
1142 *inout_tp = tp; 1125 *inout_tp = tp;
1143 } 1126 }
1144 } 1127 }
1145} 1128}
1146 1129
1147static void 1130static void
1148declare_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp) 1131declare_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp)
1149{ 1132{
1150 1133
1151 check_bit_field_type(dsym, inout_tp, inout_t); 1134 check_bit_field_type(dsym, inout_tp, inout_t);
1152 1135
1153 type_t *const tp = *inout_tp; 1136 type_t *const tp = *inout_tp;
1154 tspec_t const t = *inout_t; 1137 tspec_t const t = *inout_t;
1155 if (tp->t_flen < 0 || tp->t_flen > (ssize_t)size_in_bits(t)) { 1138 if (tp->t_flen < 0 || tp->t_flen > (ssize_t)size_in_bits(t)) {
1156 /* illegal bit-field size: %d */ 1139 /* illegal bit-field size: %d */
1157 error(36, tp->t_flen); 1140 error(36, tp->t_flen);
1158 tp->t_flen = size_in_bits(t); 1141 tp->t_flen = size_in_bits(t);
1159 } else if (tp->t_flen == 0 && dsym->s_name != unnamed) { 1142 } else if (tp->t_flen == 0 && dsym->s_name != unnamed) {
1160 /* zero size bit-field */ 1143 /* zero size bit-field */
1161 error(37); 1144 error(37);
1162 tp->t_flen = size_in_bits(t); 1145 tp->t_flen = size_in_bits(t);
1163 } 1146 }
1164 if (dsym->s_scl == MOU) { 1147 if (dsym->s_scl == MOU) {
1165 /* illegal use of bit-field */ 1148 /* illegal use of bit-field */
1166 error(41); 1149 error(41);
1167 dsym->s_type->t_bitfield = false; 1150 dsym->s_type->t_bitfield = false;
1168 dsym->s_bitfield = false; 1151 dsym->s_bitfield = false;
1169 } 1152 }
1170} 1153}
1171 1154
1172/* 1155/*
1173 * Process the declarator of a struct/union element. 1156 * Process the declarator of a struct/union element.
1174 */ 1157 */
1175sym_t * 1158sym_t *
1176declarator_1_struct_union(sym_t *dsym) 1159declarator_1_struct_union(sym_t *dsym)
1177{ 1160{
1178 type_t *tp; 1161 type_t *tp;
1179 tspec_t t; 1162 tspec_t t;
1180 int sz; 1163 int sz;
1181 int o = 0; /* Appease GCC */ 1164 int o = 0; /* Appease GCC */
1182 1165
1183 lint_assert(dsym->s_scl == MOS || dsym->s_scl == MOU); 1166 lint_assert(dsym->s_scl == MOS || dsym->s_scl == MOU);
1184 1167
1185 if (dcs->d_redeclared_symbol != NULL) { 1168 if (dcs->d_redeclared_symbol != NULL) {
1186 /* should be ensured by storesym() */ 1169 /* should be ensured by storesym() */
1187 lint_assert(dcs->d_redeclared_symbol->s_scl == MOS || 1170 lint_assert(dcs->d_redeclared_symbol->s_scl == MOS ||
1188 dcs->d_redeclared_symbol->s_scl == MOU); 1171 dcs->d_redeclared_symbol->s_scl == MOU);
1189 1172
1190 if (dsym->s_styp == dcs->d_redeclared_symbol->s_styp) { 1173 if (dsym->s_styp == dcs->d_redeclared_symbol->s_styp) {
1191 /* duplicate member name: %s */ 1174 /* duplicate member name: %s */
1192 error(33, dsym->s_name); 1175 error(33, dsym->s_name);
1193 rmsym(dcs->d_redeclared_symbol); 1176 rmsym(dcs->d_redeclared_symbol);
1194 } 1177 }
1195 } 1178 }
1196 1179
1197 check_type(dsym); 1180 check_type(dsym);
1198 1181
1199 t = (tp = dsym->s_type)->t_tspec; 1182 t = (tp = dsym->s_type)->t_tspec;
1200 1183
1201 if (dsym->s_bitfield) { 1184 if (dsym->s_bitfield) {
1202 declare_bit_field(dsym, &t, &tp); 1185 declare_bit_field(dsym, &t, &tp);
1203 } else if (t == FUNC) { 1186 } else if (t == FUNC) {
1204 /* function illegal in structure or union */ 1187 /* function illegal in structure or union */
1205 error(38); 1188 error(38);
1206 dsym->s_type = tp = derive_type(tp, t = PTR); 1189 dsym->s_type = tp = derive_type(tp, t = PTR);
1207 } 1190 }
1208 1191
1209 /* 1192 /*
1210 * bit-fields of length 0 are not warned about because length() 1193 * bit-fields of length 0 are not warned about because length()
1211 * does not return the length of the bit-field but the length 1194 * does not return the length of the bit-field but the length
1212 * of the type the bit-field is packed in (it's ok) 1195 * of the type the bit-field is packed in (it's ok)
1213 */ 1196 */
1214 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) { 1197 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1215 if (t == ARRAY && dsym->s_type->t_dim == 0) { 1198 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1216 /* zero sized array in struct is a C99 extension: %s */ 1199 /* zero sized array in struct is a C99 extension: %s */
1217 c99ism(39, dsym->s_name); 1200 c99ism(39, dsym->s_name);
1218 } 1201 }
1219 } 1202 }
1220 1203
1221 if (dcs->d_ctx == MOU) { 1204 if (dcs->d_ctx == MOU) {
1222 o = dcs->d_offset; 1205 o = dcs->d_offset;
1223 dcs->d_offset = 0; 1206 dcs->d_offset = 0;
1224 } 1207 }
1225 if (dsym->s_bitfield) { 1208 if (dsym->s_bitfield) {
1226 align(alignment_in_bits(tp), tp->t_flen); 1209 align(alignment_in_bits(tp), tp->t_flen);
1227 dsym->s_value.v_quad = 1210 dsym->s_value.v_quad =
1228 (dcs->d_offset / size_in_bits(t)) * size_in_bits(t); 1211 (dcs->d_offset / size_in_bits(t)) * size_in_bits(t);
1229 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad; 1212 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1230 dcs->d_offset += tp->t_flen; 1213 dcs->d_offset += tp->t_flen;
1231 } else { 1214 } else {
1232 align(alignment_in_bits(tp), 0); 1215 align(alignment_in_bits(tp), 0);
1233 dsym->s_value.v_quad = dcs->d_offset; 1216 dsym->s_value.v_quad = dcs->d_offset;
1234 dcs->d_offset += sz; 1217 dcs->d_offset += sz;
1235 } 1218 }
1236 if (dcs->d_ctx == MOU) { 1219 if (dcs->d_ctx == MOU) {
1237 if (o > dcs->d_offset) 1220 if (o > dcs->d_offset)
1238 dcs->d_offset = o; 1221 dcs->d_offset = o;
1239 } 1222 }
1240 1223
1241 check_function_definition(dsym, false); 1224 check_function_definition(dsym, false);
1242 1225
1243 /* 1226 /*
1244 * Clear the BITFIELDTYPE indicator after processing each 1227 * Clear the BITFIELDTYPE indicator after processing each
1245 * structure element. 1228 * structure element.
1246 */ 1229 */
1247 bitfieldtype_ok = false; 1230 bitfieldtype_ok = false;
1248 1231
1249 return dsym; 1232 return dsym;
1250} 1233}
1251 1234
1252/* 1235/*
1253 * Aligns next structure element as required. 1236 * Aligns next structure element as required.
1254 * 1237 *
1255 * al contains the required alignment, len the length of a bit-field. 1238 * al contains the required alignment, len the length of a bit-field.
1256 */ 1239 */
1257static void 1240static void
1258align(int al, int len) 1241align(int al, int len)
1259{ 1242{
1260 int no; 1243 int no;
1261 1244
1262 /* 1245 /*
1263 * The alignment of the current element becomes the alignment of 1246 * The alignment of the current element becomes the alignment of
1264 * the struct/union if it is larger than the current alignment 1247 * the struct/union if it is larger than the current alignment
1265 * of the struct/union. 1248 * of the struct/union.
1266 */ 1249 */
1267 if (al > dcs->d_sou_align_in_bits) 1250 if (al > dcs->d_sou_align_in_bits)
1268 dcs->d_sou_align_in_bits = al; 1251 dcs->d_sou_align_in_bits = al;
1269 1252
1270 no = (dcs->d_offset + (al - 1)) & ~(al - 1); 1253 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1271 if (len == 0 || dcs->d_offset + len > no) 1254 if (len == 0 || dcs->d_offset + len > no)
1272 dcs->d_offset = no; 1255 dcs->d_offset = no;
1273} 1256}
1274 1257
1275/* 1258/*
1276 * Remember the width of the field in its type structure. 1259 * Remember the width of the field in its type structure.
1277 */ 1260 */
1278sym_t * 1261sym_t *
1279bitfield(sym_t *dsym, int len) 1262bitfield(sym_t *dsym, int len)
1280{ 1263{
1281 1264
1282 if (dsym == NULL) { 1265 if (dsym == NULL) {
1283 dsym = getblk(sizeof(*dsym)); 1266 dsym = getblk(sizeof(*dsym));
1284 dsym->s_name = unnamed; 1267 dsym->s_name = unnamed;
1285 dsym->s_kind = FMEMBER; 1268 dsym->s_kind = FMEMBER;
1286 dsym->s_scl = MOS; 1269 dsym->s_scl = MOS;
1287 dsym->s_type = gettyp(UINT); 1270 dsym->s_type = gettyp(UINT);
1288 dsym->s_block_level = -1; 1271 dsym->s_block_level = -1;
1289 } 1272 }
1290 dsym->s_type = dup_type(dsym->s_type); 1273 dsym->s_type = dup_type(dsym->s_type);
1291 dsym->s_type->t_bitfield = true; 1274 dsym->s_type->t_bitfield = true;
1292 dsym->s_type->t_flen = len; 1275 dsym->s_type->t_flen = len;
1293 dsym->s_bitfield = true; 1276 dsym->s_bitfield = true;
1294 return dsym; 1277 return dsym;
1295} 1278}
1296 1279
1297/* 1280/*
1298 * A sequence of asterisks and qualifiers, from right to left. For example, 1281 * A sequence of asterisks and qualifiers, from right to left. For example,
1299 * 'const ***volatile **const volatile' results in [cvp, p, vp, p, p]. The 1282 * 'const ***volatile **const volatile' results in [cvp, p, vp, p, p]. The
1300 * leftmost 'const' is not included in this list, it is stored in dcs->d_const 1283 * leftmost 'const' is not included in this list, it is stored in dcs->d_const
1301 * instead. 1284 * instead.
1302 */ 1285 */
1303qual_ptr * 1286qual_ptr *
1304merge_qualified_pointer(qual_ptr *p1, qual_ptr *p2) 1287merge_qualified_pointer(qual_ptr *p1, qual_ptr *p2)
1305{ 1288{
1306 qual_ptr *tail; 1289 qual_ptr *tail;
1307 1290
1308 if (p2 == NULL) 1291 if (p2 == NULL)
1309 return p1; /* for optional qualifiers */ 1292 return p1; /* for optional qualifiers */
1310 1293
1311 if (p2->p_pointer) { 1294 if (p2->p_pointer) {
1312 /* append p1 to p2, keeping p2 */ 1295 /* append p1 to p2, keeping p2 */
1313 for (tail = p2; tail->p_next != NULL; tail = tail->p_next) 1296 for (tail = p2; tail->p_next != NULL; tail = tail->p_next)
1314 continue; 1297 continue;
1315 tail->p_next = p1; 1298 tail->p_next = p1;
1316 return p2; 1299 return p2;
1317 } 1300 }
1318 1301
1319 /* merge p2 into p1, keeping p1 */ 1302 /* merge p2 into p1, keeping p1 */
1320 if (p2->p_const) { 1303 if (p2->p_const) {
1321 if (p1->p_const) { 1304 if (p1->p_const) {
1322 /* duplicate '%s' */ 1305 /* duplicate '%s' */
1323 warning(10, "const"); 1306 warning(10, "const");
1324 } 1307 }
1325 p1->p_const = true; 1308 p1->p_const = true;
1326 } 1309 }
1327 if (p2->p_volatile) { 1310 if (p2->p_volatile) {
1328 if (p1->p_volatile) { 1311 if (p1->p_volatile) {
1329 /* duplicate '%s' */ 1312 /* duplicate '%s' */
1330 warning(10, "volatile"); 1313 warning(10, "volatile");
1331 } 1314 }
1332 p1->p_volatile = true; 1315 p1->p_volatile = true;
1333 } 1316 }
1334 free(p2); 1317 free(p2);
1335 return p1; 1318 return p1;
1336} 1319}
1337 1320
1338/* 1321/*
1339 * The following 3 functions extend the type of a declarator with 1322 * The following 3 functions extend the type of a declarator with
1340 * pointer, function and array types. 1323 * pointer, function and array types.
1341 * 1324 *
1342 * The current type is the type built by end_type() (dcs->d_type) and 1325 * The current type is the type built by end_type() (dcs->d_type) and
1343 * pointer, function and array types already added for this 1326 * pointer, function and array types already added for this
1344 * declarator. The new type extension is inserted between both. 1327 * declarator. The new type extension is inserted between both.
1345 */ 1328 */
1346sym_t * 1329sym_t *
1347add_pointer(sym_t *decl, qual_ptr *p) 1330add_pointer(sym_t *decl, qual_ptr *p)
1348{ 1331{
1349 type_t **tpp, *tp; 1332 type_t **tpp, *tp;
1350 qual_ptr *next; 1333 qual_ptr *next;
1351 1334
1352 tpp = &decl->s_type; 1335 tpp = &decl->s_type;
1353 while (*tpp != NULL && *tpp != dcs->d_type) 1336 while (*tpp != NULL && *tpp != dcs->d_type)
1354 tpp = &(*tpp)->t_subt; 1337 tpp = &(*tpp)->t_subt;
1355 if (*tpp == NULL) 1338 if (*tpp == NULL)
1356 return decl; 1339 return decl;
1357 1340
1358 while (p != NULL) { 1341 while (p != NULL) {
1359 *tpp = tp = getblk(sizeof(*tp)); 1342 *tpp = tp = getblk(sizeof(*tp));
1360 tp->t_tspec = PTR; 1343 tp->t_tspec = PTR;
1361 tp->t_const = p->p_const; 1344 tp->t_const = p->p_const;
1362 tp->t_volatile = p->p_volatile; 1345 tp->t_volatile = p->p_volatile;
1363 *(tpp = &tp->t_subt) = dcs->d_type; 1346 *(tpp = &tp->t_subt) = dcs->d_type;
1364 next = p->p_next; 1347 next = p->p_next;
1365 free(p); 1348 free(p);
1366 p = next; 1349 p = next;
1367 } 1350 }
1368 return decl; 1351 return decl;
1369} 1352}
1370 1353
1371/* 1354/*
1372 * If a dimension was specified, dim is true, otherwise false 1355 * If a dimension was specified, dim is true, otherwise false
1373 * n is the specified dimension 1356 * n is the specified dimension
1374 */ 1357 */
1375sym_t * 1358sym_t *
1376add_array(sym_t *decl, bool dim, int n) 1359add_array(sym_t *decl, bool dim, int n)
1377{ 1360{
1378 type_t **tpp, *tp; 1361 type_t **tpp, *tp;
1379 1362
1380 tpp = &decl->s_type; 1363 tpp = &decl->s_type;
1381 while (*tpp != NULL && *tpp != dcs->d_type) 1364 while (*tpp != NULL && *tpp != dcs->d_type)
1382 tpp = &(*tpp)->t_subt; 1365 tpp = &(*tpp)->t_subt;
1383 if (*tpp == NULL) 1366 if (*tpp == NULL)
1384 return decl; 1367 return decl;
1385 1368
1386 *tpp = tp = getblk(sizeof(*tp)); 1369 *tpp = tp = getblk(sizeof(*tp));
1387 tp->t_tspec = ARRAY; 1370 tp->t_tspec = ARRAY;
1388 tp->t_subt = dcs->d_type; 1371 tp->t_subt = dcs->d_type;
1389 tp->t_dim = n; 1372 tp->t_dim = n;
1390 1373
1391 if (n < 0) { 1374 if (n < 0) {
1392 /* negative array dimension (%d) */ 1375 /* negative array dimension (%d) */
1393 error(20, n); 1376 error(20, n);
1394 n = 0; 1377 n = 0;
1395 } else if (n == 0 && dim) { 1378 } else if (n == 0 && dim) {
1396 /* zero sized array is a C99 extension */ 1379 /* zero sized array is a C99 extension */
1397 c99ism(322); 1380 c99ism(322);
1398 } else if (n == 0 && !dim) { 1381 } else if (n == 0 && !dim) {
1399 setcomplete(tp, false); 1382 setcomplete(tp, false);
1400 } 1383 }
1401 1384
1402 return decl; 1385 return decl;
1403} 1386}
1404 1387
1405sym_t * 1388sym_t *
1406add_function(sym_t *decl, sym_t *args) 1389add_function(sym_t *decl, sym_t *args)
1407{ 1390{
1408 type_t **tpp, *tp; 1391 type_t **tpp, *tp;
1409 1392
1410 if (dcs->d_proto) { 1393 if (dcs->d_proto) {
1411 if (tflag) 1394 if (tflag)
1412 /* function prototypes are illegal in traditional C */ 1395 /* function prototypes are illegal in traditional C */
1413 warning(270); 1396 warning(270);
1414 args = new_style_function(decl, args); 1397 args = new_style_function(decl, args);
1415 } else { 1398 } else {
1416 old_style_function(decl, args); 1399 old_style_function(decl, args);
1417 } 1400 }
1418 1401
1419 /* 1402 /*
1420 * The symbols are removed from the symbol table by 1403 * The symbols are removed from the symbol table by
1421 * end_declaration_level after add_function. To be able to restore 1404 * end_declaration_level after add_function. To be able to restore
1422 * them if this is a function definition, a pointer to the list of all 1405 * them if this is a function definition, a pointer to the list of all
1423 * symbols is stored in dcs->d_next->d_func_proto_syms. Also a list of 1406 * symbols is stored in dcs->d_next->d_func_proto_syms. Also a list of
1424 * the arguments (concatenated by s_next) is stored in 1407 * the arguments (concatenated by s_next) is stored in
1425 * dcs->d_next->d_func_args. (dcs->d_next must be used because *dcs is 1408 * dcs->d_next->d_func_args. (dcs->d_next must be used because *dcs is
1426 * the declaration stack element created for the list of params and is 1409 * the declaration stack element created for the list of params and is
1427 * removed after add_function.) 1410 * removed after add_function.)
1428 */ 1411 */
1429 if (dcs->d_next->d_ctx == EXTERN && 1412 if (dcs->d_next->d_ctx == EXTERN &&
1430 decl->s_type == dcs->d_next->d_type) { 1413 decl->s_type == dcs->d_next->d_type) {
1431 dcs->d_next->d_func_proto_syms = dcs->d_dlsyms; 1414 dcs->d_next->d_func_proto_syms = dcs->d_dlsyms;
1432 dcs->d_next->d_func_args = args; 1415 dcs->d_next->d_func_args = args;
1433 } 1416 }
1434 1417
1435 tpp = &decl->s_type; 1418 tpp = &decl->s_type;
1436 while (*tpp != NULL && *tpp != dcs->d_next->d_type) 1419 while (*tpp != NULL && *tpp != dcs->d_next->d_type)
1437 tpp = &(*tpp)->t_subt; 1420 tpp = &(*tpp)->t_subt;
1438 if (*tpp == NULL) 1421 if (*tpp == NULL)
1439 return decl; 1422 return decl;
1440 1423
1441 *tpp = tp = getblk(sizeof(*tp)); 1424 *tpp = tp = getblk(sizeof(*tp));
1442 tp->t_tspec = FUNC; 1425 tp->t_tspec = FUNC;
1443 tp->t_subt = dcs->d_next->d_type; 1426 tp->t_subt = dcs->d_next->d_type;
1444 if ((tp->t_proto = dcs->d_proto) != false) 1427 if ((tp->t_proto = dcs->d_proto) != false)
1445 tp->t_args = args; 1428 tp->t_args = args;
1446 tp->t_vararg = dcs->d_vararg; 1429 tp->t_vararg = dcs->d_vararg;
1447 1430
1448 return decl; 1431 return decl;
1449} 1432}
1450 1433
1451/* 1434/*
1452 * Called for new style function declarations. 1435 * Called for new style function declarations.
1453 */ 1436 */
1454/* ARGSUSED */ 1437/* ARGSUSED */
1455static sym_t * 1438static sym_t *
1456new_style_function(sym_t *decl, sym_t *args) 1439new_style_function(sym_t *decl, sym_t *args)
1457{ 1440{
1458 sym_t *arg, *sym; 1441 sym_t *arg, *sym;
1459 scl_t sc; 1442 scl_t sc;
1460 int n; 1443 int n;
1461 1444
1462 /* 1445 /*
1463 * Declarations of structs/unions/enums in param lists are legal, 1446 * Declarations of structs/unions/enums in param lists are legal,
1464 * but senseless. 1447 * but senseless.
1465 */ 1448 */
1466 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { 1449 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1467 sc = sym->s_scl; 1450 sc = sym->s_scl;
1468 if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) { 1451 if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) {
1469 /* dubious tag declaration: %s %s */ 1452 /* dubious tag declaration: %s %s */
1470 warning(85, storage_class_name(sc), sym->s_name); 1453 warning(85, storage_class_name(sc), sym->s_name);
1471 } 1454 }
1472 } 1455 }
1473 1456
1474 n = 1; 1457 n = 1;
1475 for (arg = args; arg != NULL; arg = arg->s_next) { 1458 for (arg = args; arg != NULL; arg = arg->s_next) {
1476 if (arg->s_type->t_tspec == VOID) { 1459 if (arg->s_type->t_tspec == VOID) {
1477 if (n > 1 || arg->s_next != NULL) { 1460 if (n > 1 || arg->s_next != NULL) {
1478 /* void must be sole parameter */ 1461 /* void must be sole parameter */
1479 error(60); 1462 error(60);
1480 arg->s_type = gettyp(INT); 1463 arg->s_type = gettyp(INT);
1481 } 1464 }
1482 } 1465 }
1483 n++; 1466 n++;
1484 } 1467 }
1485 1468
1486 /* return NULL if first param is VOID */ 1469 /* return NULL if first param is VOID */
1487 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL; 1470 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL;
1488} 1471}
1489 1472
1490/* 1473/*
1491 * Called for old style function declarations. 1474 * Called for old style function declarations.
1492 */ 1475 */
1493static void 1476static void
1494old_style_function(sym_t *decl, sym_t *args) 1477old_style_function(sym_t *decl, sym_t *args)
1495{ 1478{
1496 1479
1497 /* 1480 /*
1498 * Remember list of params only if this is really seams to be 1481 * Remember list of params only if this is really seams to be
1499 * a function definition. 1482 * a function definition.
1500 */ 1483 */
1501 if (dcs->d_next->d_ctx == EXTERN && 1484 if (dcs->d_next->d_ctx == EXTERN &&
1502 decl->s_type == dcs->d_next->d_type) { 1485 decl->s_type == dcs->d_next->d_type) {
1503 /* 1486 /*
1504 * We assume that this becomes a function definition. If 1487 * We assume that this becomes a function definition. If
1505 * we are wrong, it's corrected in check_function_definition(). 1488 * we are wrong, it's corrected in check_function_definition().
1506 */ 1489 */
1507 if (args != NULL) { 1490 if (args != NULL) {
1508 decl->s_osdef = true; 1491 decl->s_osdef = true;
1509 decl->s_args = args; 1492 decl->s_args = args;
1510 } 1493 }
1511 } else { 1494 } else {
1512 if (args != NULL) 1495 if (args != NULL)
1513 /* function prototype parameters must have types */ 1496 /* function prototype parameters must have types */
1514 warning(62); 1497 warning(62);
1515 } 1498 }
1516} 1499}
1517 1500
1518/* 1501/*
1519 * Lists of identifiers in functions declarations are allowed only if 1502 * Lists of identifiers in functions declarations are allowed only if
1520 * it's also a function definition. If this is not the case, print an 1503 * it's also a function definition. If this is not the case, print an
1521 * error message. 1504 * error message.
1522 */ 1505 */
1523void 1506void
1524check_function_definition(sym_t *sym, bool msg) 1507check_function_definition(sym_t *sym, bool msg)
1525{ 1508{
1526 1509
1527 if (sym->s_osdef) { 1510 if (sym->s_osdef) {
1528 if (msg) { 1511 if (msg) {
1529 /* incomplete or misplaced function definition */ 1512 /* incomplete or misplaced function definition */
1530 error(22); 1513 error(22);
1531 } 1514 }
1532 sym->s_osdef = false; 1515 sym->s_osdef = false;
1533 sym->s_args = NULL; 1516 sym->s_args = NULL;
1534 } 1517 }
1535} 1518}
1536 1519
1537/* 1520/*
1538 * Process the name in a declarator. 1521 * Process the name in a declarator.
1539 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or 1522 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
1540 * TYPEDEF. 1523 * TYPEDEF.
1541 * s_def and s_reg are valid after declarator_name(). 1524 * s_def and s_reg are valid after declarator_name().
1542 */ 1525 */
1543sym_t * 1526sym_t *
1544declarator_name(sym_t *sym) 1527declarator_name(sym_t *sym)
1545{ 1528{
1546 scl_t sc = NOSCL; 1529 scl_t sc = NOSCL;
1547 1530
1548 if (sym->s_scl == NOSCL) { 1531 if (sym->s_scl == NOSCL) {
1549 dcs->d_redeclared_symbol = NULL; 1532 dcs->d_redeclared_symbol = NULL;
1550 } else if (sym->s_defarg) { 1533 } else if (sym->s_defarg) {
1551 sym->s_defarg = false; 1534 sym->s_defarg = false;
1552 dcs->d_redeclared_symbol = NULL; 1535 dcs->d_redeclared_symbol = NULL;
1553 } else { 1536 } else {
1554 dcs->d_redeclared_symbol = sym; 1537 dcs->d_redeclared_symbol = sym;
1555 sym = pushdown(sym); 1538 sym = pushdown(sym);
1556 } 1539 }
1557 1540
1558 switch (dcs->d_ctx) { 1541 switch (dcs->d_ctx) {
1559 case MOS: 1542 case MOS:
1560 case MOU: 1543 case MOU:
1561 /* Set parent */ 1544 /* Set parent */
1562 sym->s_styp = dcs->d_tagtyp->t_str; 1545 sym->s_styp = dcs->d_tagtyp->t_str;
1563 sym->s_def = DEF; 1546 sym->s_def = DEF;
1564 sym->s_value.v_tspec = INT; 1547 sym->s_value.v_tspec = INT;
1565 sc = dcs->d_ctx; 1548 sc = dcs->d_ctx;
1566 break; 1549 break;
1567 case EXTERN: 1550 case EXTERN:
1568 /* 1551 /*
1569 * static and external symbols without "extern" are 1552 * static and external symbols without "extern" are
1570 * considered to be tentative defined, external 1553 * considered to be tentative defined, external
1571 * symbols with "extern" are declared, and typedef names 1554 * symbols with "extern" are declared, and typedef names
1572 * are defined. Tentative defined and declared symbols 1555 * are defined. Tentative defined and declared symbols
1573 * may become defined if an initializer is present or 1556 * may become defined if an initializer is present or
1574 * this is a function definition. 1557 * this is a function definition.
1575 */ 1558 */
1576 if ((sc = dcs->d_scl) == NOSCL) { 1559 if ((sc = dcs->d_scl) == NOSCL) {
1577 sc = EXTERN; 1560 sc = EXTERN;
1578 sym->s_def = TDEF; 1561 sym->s_def = TDEF;
1579 } else if (sc == STATIC) { 1562 } else if (sc == STATIC) {
1580 sym->s_def = TDEF; 1563 sym->s_def = TDEF;
1581 } else if (sc == TYPEDEF) { 1564 } else if (sc == TYPEDEF) {
1582 sym->s_def = DEF; 1565 sym->s_def = DEF;
1583 } else { 1566 } else {
1584 lint_assert(sc == EXTERN); 1567 lint_assert(sc == EXTERN);
1585 sym->s_def = DECL; 1568 sym->s_def = DECL;
1586 } 1569 }
1587 break; 1570 break;
1588 case PROTO_ARG: 1571 case PROTO_ARG:
1589 sym->s_arg = true; 1572 sym->s_arg = true;
1590 /* FALLTHROUGH */ 1573 /* FALLTHROUGH */
1591 case ARG: 1574 case ARG:
1592 if ((sc = dcs->d_scl) == NOSCL) { 1575 if ((sc = dcs->d_scl) == NOSCL) {
1593 sc = AUTO; 1576 sc = AUTO;
1594 } else { 1577 } else {
1595 lint_assert(sc == REG); 1578 lint_assert(sc == REG);
1596 sym->s_reg = true; 1579 sym->s_reg = true;
1597 sc = AUTO; 1580 sc = AUTO;
1598 } 1581 }
1599 sym->s_def = DEF; 1582 sym->s_def = DEF;
1600 break; 1583 break;
1601 case AUTO: 1584 case AUTO:
1602 if ((sc = dcs->d_scl) == NOSCL) { 1585 if ((sc = dcs->d_scl) == NOSCL) {
1603 /* 1586 /*
1604 * XXX somewhat ugly because we dont know whether 1587 * XXX somewhat ugly because we dont know whether
1605 * this is AUTO or EXTERN (functions). If we are 1588 * this is AUTO or EXTERN (functions). If we are
1606 * wrong it must be corrected in declare_local(), 1589 * wrong it must be corrected in declare_local(),
1607 * where we have the necessary type information. 1590 * where we have the necessary type information.
1608 */ 1591 */
1609 sc = AUTO; 1592 sc = AUTO;
1610 sym->s_def = DEF; 1593 sym->s_def = DEF;
1611 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) { 1594 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1612 sym->s_def = DEF; 1595 sym->s_def = DEF;
1613 } else if (sc == REG) { 1596 } else if (sc == REG) {
1614 sym->s_reg = true; 1597 sym->s_reg = true;
1615 sc = AUTO; 1598 sc = AUTO;
1616 sym->s_def = DEF; 1599 sym->s_def = DEF;
1617 } else { 1600 } else {
1618 lint_assert(sc == EXTERN); 1601 lint_assert(sc == EXTERN);
1619 sym->s_def = DECL; 1602 sym->s_def = DECL;
1620 } 1603 }
1621 break; 1604 break;
1622 default: 1605 default:
1623 lint_assert(/*CONSTCOND*/false); 1606 lint_assert(/*CONSTCOND*/false);
1624 } 1607 }
1625 sym->s_scl = sc; 1608 sym->s_scl = sc;
1626 1609
1627 sym->s_type = dcs->d_type; 1610 sym->s_type = dcs->d_type;
1628 1611
1629 dcs->d_func_proto_syms = NULL; 1612 dcs->d_func_proto_syms = NULL;
1630 1613
1631 return sym; 1614 return sym;
1632} 1615}
1633 1616
1634/* 1617/*
1635 * Process a name in the list of formal parameters in an old style function 1618 * Process a name in the list of formal parameters in an old style function
1636 * definition. 1619 * definition.
1637 */ 1620 */
1638sym_t * 1621sym_t *
1639old_style_function_name(sym_t *sym) 1622old_style_function_name(sym_t *sym)
1640{ 1623{
1641 1624
1642 if (sym->s_scl != NOSCL) { 1625 if (sym->s_scl != NOSCL) {
1643 if (block_level == sym->s_block_level) { 1626 if (block_level == sym->s_block_level) {
1644 /* redeclaration of formal parameter %s */ 1627 /* redeclaration of formal parameter %s */
1645 error(21, sym->s_name); 1628 error(21, sym->s_name);
1646 lint_assert(sym->s_defarg); 1629 lint_assert(sym->s_defarg);
1647 } 1630 }
1648 sym = pushdown(sym); 1631 sym = pushdown(sym);
1649 } 1632 }
1650 sym->s_type = gettyp(INT); 1633 sym->s_type = gettyp(INT);
1651 sym->s_scl = AUTO; 1634 sym->s_scl = AUTO;
1652 sym->s_def = DEF; 1635 sym->s_def = DEF;
1653 sym->s_defarg = sym->s_arg = true; 1636 sym->s_defarg = sym->s_arg = true;
1654 return sym; 1637 return sym;
1655} 1638}
1656 1639
1657/* 1640/*
1658 * Create the type of a tag. 1641 * Create the type of a tag.
1659 * 1642 *
1660 * tag points to the symbol table entry of the tag 1643 * tag points to the symbol table entry of the tag
1661 * kind is the kind of the tag (STRUCT/UNION/ENUM) 1644 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1662 * decl is true if the type of the tag will be completed in this declaration 1645 * decl is true if the type of the tag will be completed in this declaration
1663 * (the following token is T_LBRACE) 1646 * (the following token is T_LBRACE)
1664 * semi is true if the following token is T_SEMI 1647 * semi is true if the following token is T_SEMI
1665 */ 1648 */
1666type_t * 1649type_t *
1667mktag(sym_t *tag, tspec_t kind, bool decl, bool semi) 1650mktag(sym_t *tag, tspec_t kind, bool decl, bool semi)
1668{ 1651{
1669 scl_t scl; 1652 scl_t scl;
1670 type_t *tp; 1653 type_t *tp;
1671 1654
1672 if (kind == STRUCT) { 1655 if (kind == STRUCT) {
1673 scl = STRUCT_TAG; 1656 scl = STRUCT_TAG;
1674 } else if (kind == UNION) { 1657 } else if (kind == UNION) {
1675 scl = UNION_TAG; 1658 scl = UNION_TAG;
1676 } else { 1659 } else {
1677 lint_assert(kind == ENUM); 1660 lint_assert(kind == ENUM);
1678 scl = ENUM_TAG; 1661 scl = ENUM_TAG;
1679 } 1662 }
1680 1663
1681 if (tag != NULL) { 1664 if (tag != NULL) {
1682 if (tag->s_scl != NOSCL) { 1665 if (tag->s_scl != NOSCL) {
1683 tag = newtag(tag, scl, decl, semi); 1666 tag = newtag(tag, scl, decl, semi);
1684 } else { 1667 } else {
1685 /* a new tag, no empty declaration */ 1668 /* a new tag, no empty declaration */
1686 dcs->d_next->d_nonempty_decl = true; 1669 dcs->d_next->d_nonempty_decl = true;
1687 if (scl == ENUM_TAG && !decl) { 1670 if (scl == ENUM_TAG && !decl) {
1688 if (!tflag && (sflag || pflag)) 1671 if (!tflag && (sflag || pflag))
1689 /* forward reference to enum type */ 1672 /* forward reference to enum type */
1690 warning(42); 1673 warning(42);
1691 } 1674 }
1692 } 1675 }
1693 if (tag->s_scl == NOSCL) { 1676 if (tag->s_scl == NOSCL) {
1694 tag->s_scl = scl; 1677 tag->s_scl = scl;
1695 tag->s_type = tp = getblk(sizeof(*tp)); 1678 tag->s_type = tp = getblk(sizeof(*tp));
1696 tp->t_packed = dcs->d_packed; 1679 tp->t_packed = dcs->d_packed;
1697 } else { 1680 } else {
1698 tp = tag->s_type; 1681 tp = tag->s_type;
1699 } 1682 }
1700 } else { 1683 } else {
1701 tag = getblk(sizeof(*tag)); 1684 tag = getblk(sizeof(*tag));
1702 tag->s_name = unnamed; 1685 tag->s_name = unnamed;
1703 UNIQUE_CURR_POS(tag->s_def_pos); 1686 UNIQUE_CURR_POS(tag->s_def_pos);
1704 tag->s_kind = FTAG; 1687 tag->s_kind = FTAG;
1705 tag->s_scl = scl; 1688 tag->s_scl = scl;
1706 tag->s_block_level = -1; 1689 tag->s_block_level = -1;
1707 tag->s_type = tp = getblk(sizeof(*tp)); 1690 tag->s_type = tp = getblk(sizeof(*tp));
1708 tp->t_packed = dcs->d_packed; 1691 tp->t_packed = dcs->d_packed;
1709 dcs->d_next->d_nonempty_decl = true; 1692 dcs->d_next->d_nonempty_decl = true;
1710 } 1693 }
1711 1694
1712 if (tp->t_tspec == NOTSPEC) { 1695 if (tp->t_tspec == NOTSPEC) {
1713 tp->t_tspec = kind; 1696 tp->t_tspec = kind;
1714 if (kind != ENUM) { 1697 if (kind != ENUM) {
1715 tp->t_str = getblk(sizeof(*tp->t_str)); 1698 tp->t_str = getblk(sizeof(*tp->t_str));
1716 tp->t_str->sou_align_in_bits = CHAR_SIZE; 1699 tp->t_str->sou_align_in_bits = CHAR_SIZE;
1717 tp->t_str->sou_tag = tag; 1700 tp->t_str->sou_tag = tag;
1718 } else { 1701 } else {
1719 tp->t_is_enum = true; 1702 tp->t_is_enum = true;
1720 tp->t_enum = getblk(sizeof(*tp->t_enum)); 1703 tp->t_enum = getblk(sizeof(*tp->t_enum));
1721 tp->t_enum->en_tag = tag; 1704 tp->t_enum->en_tag = tag;
1722 } 1705 }
1723 setcomplete(tp, false); 1706 setcomplete(tp, false);
1724 } 1707 }
1725 return tp; 1708 return tp;
1726} 1709}
1727 1710
1728/* 1711/*
1729 * Checks all possible cases of tag redeclarations. 1712 * Checks all possible cases of tag redeclarations.
1730 * decl is true if T_LBRACE follows 1713 * decl is true if T_LBRACE follows
1731 * semi is true if T_SEMI follows 1714 * semi is true if T_SEMI follows
1732 */ 1715 */
1733static sym_t * 1716static sym_t *
1734newtag(sym_t *tag, scl_t scl, bool decl, bool semi) 1717newtag(sym_t *tag, scl_t scl, bool decl, bool semi)
1735{ 1718{
1736 1719
1737 if (tag->s_block_level < block_level) { 1720 if (tag->s_block_level < block_level) {
1738 if (semi) { 1721 if (semi) {
1739 /* "struct a;" */ 1722 /* "struct a;" */
1740 if (!tflag) { 1723 if (!tflag) {
1741 if (!sflag) 1724 if (!sflag)
1742 /* decl. introduces new type ... */ 1725 /* decl. introduces new type ... */
1743 warning(44, storage_class_name(scl), 1726 warning(44, storage_class_name(scl),
1744 tag->s_name); 1727 tag->s_name);
1745 tag = pushdown(tag); 1728 tag = pushdown(tag);
1746 } else if (tag->s_scl != scl) { 1729 } else if (tag->s_scl != scl) {
1747 /* base type is really '%s %s' */ 1730 /* base type is really '%s %s' */
1748 warning(45, storage_class_name(tag->s_scl), 1731 warning(45, storage_class_name(tag->s_scl),
1749 tag->s_name); 1732 tag->s_name);
1750 } 1733 }
1751 dcs->d_next->d_nonempty_decl = true; 1734 dcs->d_next->d_nonempty_decl = true;
1752 } else if (decl) { 1735 } else if (decl) {
1753 /* "struct a { ... } " */ 1736 /* "struct a { ... } " */
1754 if (hflag) 1737 if (hflag)
1755 /* redefinition hides earlier one: %s */ 1738 /* redefinition hides earlier one: %s */
1756 warning(43, tag->s_name); 1739 warning(43, tag->s_name);
1757 tag = pushdown(tag); 1740 tag = pushdown(tag);
1758 dcs->d_next->d_nonempty_decl = true; 1741 dcs->d_next->d_nonempty_decl = true;
1759 } else if (tag->s_scl != scl) { 1742 } else if (tag->s_scl != scl) {
1760 /* base type is really '%s %s' */ 1743 /* base type is really '%s %s' */
1761 warning(45, storage_class_name(tag->s_scl), 1744 warning(45, storage_class_name(tag->s_scl),
1762 tag->s_name); 1745 tag->s_name);
1763 /* declaration introduces new type in ANSI C: %s %s */ 1746 /* declaration introduces new type in ANSI C: %s %s */
1764 if (!sflag) { 1747 if (!sflag) {
1765 /* decl. introduces new type in ANSI C: %s %s */ 1748 /* decl. introduces new type in ANSI C: %s %s */
1766 warning(44, storage_class_name(scl), 1749 warning(44, storage_class_name(scl),
1767 tag->s_name); 1750 tag->s_name);
1768 } 1751 }
1769 tag = pushdown(tag); 1752 tag = pushdown(tag);
1770 dcs->d_next->d_nonempty_decl = true; 1753 dcs->d_next->d_nonempty_decl = true;
1771 } 1754 }
1772 } else { 1755 } else {
1773 if (tag->s_scl != scl) { 1756 if (tag->s_scl != scl) {
1774 /* (%s) tag redeclared */ 1757 /* (%s) tag redeclared */
1775 error(46, storage_class_name(tag->s_scl)); 1758 error(46, storage_class_name(tag->s_scl));
1776 print_previous_declaration(-1, tag); 1759 print_previous_declaration(-1, tag);
1777 tag = pushdown(tag); 1760 tag = pushdown(tag);
1778 dcs->d_next->d_nonempty_decl = true; 1761 dcs->d_next->d_nonempty_decl = true;
1779 } else if (decl && !is_incomplete(tag->s_type)) { 1762 } else if (decl && !is_incomplete(tag->s_type)) {
1780 /* (%s) tag redeclared */ 1763 /* (%s) tag redeclared */
1781 error(46, storage_class_name(tag->s_scl)); 1764 error(46, storage_class_name(tag->s_scl));
1782 print_previous_declaration(-1, tag); 1765 print_previous_declaration(-1, tag);
1783 tag = pushdown(tag); 1766 tag = pushdown(tag);
1784 dcs->d_next->d_nonempty_decl = true; 1767 dcs->d_next->d_nonempty_decl = true;
1785 } else if (semi || decl) { 1768 } else if (semi || decl) {
1786 dcs->d_next->d_nonempty_decl = true; 1769 dcs->d_next->d_nonempty_decl = true;
1787 } 1770 }
1788 } 1771 }
1789 return tag; 1772 return tag;
1790} 1773}
1791 1774
1792const char * 1775const char *
1793storage_class_name(scl_t sc) 1776storage_class_name(scl_t sc)
1794{ 1777{
1795 switch (sc) { 1778 switch (sc) {
1796 case EXTERN: return "extern"; 1779 case EXTERN: return "extern";
1797 case STATIC: return "static"; 1780 case STATIC: return "static";
1798 case AUTO: return "auto"; 1781 case AUTO: return "auto";
1799 case REG: return "register"; 1782 case REG: return "register";
1800 case TYPEDEF: return "typedef"; 1783 case TYPEDEF: return "typedef";
1801 case STRUCT_TAG:return "struct"; 1784 case STRUCT_TAG:return "struct";
1802 case UNION_TAG: return "union"; 1785 case UNION_TAG: return "union";
1803 case ENUM_TAG: return "enum"; 1786 case ENUM_TAG: return "enum";
1804 default: lint_assert(/*CONSTCOND*/false); 1787 default: lint_assert(/*CONSTCOND*/false);
1805 } 1788 }
1806} 1789}
1807 1790
1808/* 1791/*
1809 * tp points to the type of the tag, fmem to the list of members. 1792 * tp points to the type of the tag, fmem to the list of members.
1810 */ 1793 */
1811type_t * 1794type_t *
1812complete_tag_struct_or_union(type_t *tp, sym_t *fmem) 1795complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1813{ 1796{
1814 tspec_t t; 1797 tspec_t t;
1815 struct_or_union *sp; 1798 struct_or_union *sp;
1816 int n; 1799 int n;
1817 sym_t *mem; 1800 sym_t *mem;
1818 1801
1819 if (tp == NULL) /* in case of syntax errors */ 1802 if (tp == NULL) /* in case of syntax errors */
1820 return gettyp(INT); 1803 return gettyp(INT);
1821 1804
1822 setcomplete(tp, true); 1805 setcomplete(tp, true);
1823 1806
1824 t = tp->t_tspec; 1807 t = tp->t_tspec;
1825 align(dcs->d_sou_align_in_bits, 0); 1808 align(dcs->d_sou_align_in_bits, 0);
1826 sp = tp->t_str; 1809 sp = tp->t_str;