decl.c revision 1.329 1 /* $NetBSD: decl.c,v 1.329 2023/06/30 21:06:18 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project.
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.
22 *
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
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID)
41 __RCSID("$NetBSD: decl.c,v 1.329 2023/06/30 21:06:18 rillig Exp $");
42 #endif
43
44 #include <sys/param.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "lint1.h"
50
51 const char unnamed[] = "<unnamed>";
52
53 /* shared type structures for arithmetic types and void */
54 static type_t typetab[NTSPEC];
55
56 /* value of next enumerator during declaration of enum types */
57 int enumval;
58
59 /*
60 * pointer to innermost element of a stack which contains information local
61 * to nested declarations
62 */
63 dinfo_t *dcs;
64
65 static type_t *typedef_error(type_t *, tspec_t);
66 static void set_first_typedef(type_t *, sym_t *);
67 static void dcs_align(unsigned int, unsigned int);
68 static sym_t *new_tag(sym_t *, scl_t, bool, bool);
69 static bool prototypes_compatible(const type_t *, const type_t *, bool *);
70 static bool matches_no_arg_function(const type_t *, bool *);
71 static bool check_old_style_definition(sym_t *, sym_t *);
72 static bool check_prototype_declaration(sym_t *, sym_t *);
73 static sym_t *new_style_function(sym_t *);
74 static void old_style_function(sym_t *, sym_t *);
75 static void declare_external_in_block(sym_t *);
76 static bool check_init(sym_t *);
77 static void check_argument_usage(bool, sym_t *);
78 static void check_variable_usage(bool, sym_t *);
79 static void check_label_usage(sym_t *);
80 static void check_tag_usage(sym_t *);
81 static void check_global_variable(const sym_t *);
82 static void check_global_variable_size(const sym_t *);
83
84 /*
85 * initializes all global vars used in declarations
86 */
87 void
88 #ifdef __sh3__
89 /* XXX port-sh3/56311 */
90 __attribute__((optimize("O0")))
91 #endif
92 initdecl(void)
93 {
94
95 /* declaration stack */
96 dcs = xcalloc(1, sizeof(*dcs));
97 dcs->d_kind = DK_EXTERN;
98 dcs->d_last_dlsym = &dcs->d_first_dlsym;
99
100 if (!pflag) {
101 for (size_t i = 0; i < NTSPEC; i++)
102 ttab[i].tt_portable_size_in_bits =
103 ttab[i].tt_size_in_bits;
104 }
105
106 if (Tflag) {
107 ttab[BOOL].tt_is_integer = false;
108 ttab[BOOL].tt_is_uinteger = false;
109 ttab[BOOL].tt_is_arithmetic = false;
110 }
111
112 /* struct, union, enum, ptr, array and func are not shared. */
113 for (int i = (int)SIGNED; i < (int)STRUCT; i++)
114 typetab[i].t_tspec = (tspec_t)i;
115 }
116
117 /*
118 * Returns a shared type structure for arithmetic types and void.
119 *
120 * It's important to duplicate this structure using block_dup_type or
121 * expr_dup_type if it is to be modified (adding qualifiers or anything
122 * else).
123 */
124 type_t *
125 gettyp(tspec_t t)
126 {
127
128 /* TODO: make the return type 'const' */
129 return &typetab[t];
130 }
131
132 type_t *
133 block_dup_type(const type_t *tp)
134 {
135
136 type_t *ntp = block_zero_alloc(sizeof(*ntp));
137 *ntp = *tp;
138 return ntp;
139 }
140
141 /* Duplicate a type, free the allocated memory after the expression. */
142 type_t *
143 expr_dup_type(const type_t *tp)
144 {
145
146 type_t *ntp = expr_zero_alloc(sizeof(*ntp));
147 *ntp = *tp;
148 return ntp;
149 }
150
151 /*
152 * Return the unqualified version of the type. The returned type is freed at
153 * the end of the current expression.
154 *
155 * See C99 6.2.5p25.
156 */
157 type_t *
158 expr_unqualified_type(const type_t *tp)
159 {
160
161 type_t *ntp = expr_zero_alloc(sizeof(*ntp));
162 *ntp = *tp;
163 ntp->t_const = false;
164 ntp->t_volatile = false;
165
166 /*
167 * In case of a struct or union type, the members should lose their
168 * qualifiers as well, but that would require a deep copy of the
169 * struct or union type. This in turn would defeat the type
170 * comparison in types_compatible, which simply tests whether
171 * tp1->t_sou == tp2->t_sou.
172 */
173
174 return ntp;
175 }
176
177 /*
178 * Returns whether the argument is void or an incomplete array, struct, union
179 * or enum type.
180 */
181 bool
182 is_incomplete(const type_t *tp)
183 {
184 tspec_t t = tp->t_tspec;
185
186 if (t == VOID)
187 return true;
188 if (t == ARRAY)
189 return tp->t_incomplete_array;
190 if (is_struct_or_union(t))
191 return tp->t_sou->sou_incomplete;
192 if (t == ENUM)
193 return tp->t_enum->en_incomplete;
194 return false;
195 }
196
197 /*
198 * Remember the storage class of the current declaration and detect multiple
199 * storage classes.
200 */
201 void
202 dcs_add_storage_class(scl_t sc)
203 {
204
205 if (sc == INLINE) {
206 if (dcs->d_inline)
207 /* duplicate '%s' */
208 warning(10, "inline");
209 dcs->d_inline = true;
210 return;
211 }
212
213 if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC ||
214 dcs->d_sign_mod != NO_TSPEC || dcs->d_rank_mod != NO_TSPEC) {
215 /* storage class after type is obsolescent */
216 warning(83);
217 }
218
219 if (dcs->d_scl == NOSCL)
220 dcs->d_scl = sc;
221 else
222 dcs->d_multiple_storage_classes = true;
223 }
224
225 /*
226 * Remember the type, modifier or typedef name returned by the parser in the
227 * top element of the declaration stack. This information is used in
228 * dcs_end_type to build the type used for all declarators in this declaration.
229 *
230 * If tp->t_typedef is true, the type comes from a previously defined typename.
231 * Otherwise, it comes from a type specifier (int, long, ...) or a
232 * struct/union/enum tag.
233 */
234 void
235 dcs_add_type(type_t *tp)
236 {
237
238 debug_step("%s: %s", __func__, type_name(tp));
239 if (tp->t_typedef) {
240 /*
241 * something like "typedef int a; int a b;"
242 * This should not happen with current grammar.
243 */
244 lint_assert(dcs->d_type == NULL);
245 lint_assert(dcs->d_abstract_type == NO_TSPEC);
246 lint_assert(dcs->d_sign_mod == NO_TSPEC);
247 lint_assert(dcs->d_rank_mod == NO_TSPEC);
248
249 dcs->d_type = tp;
250 return;
251 }
252
253 tspec_t t = tp->t_tspec;
254 if (is_struct_or_union(t) || t == ENUM) {
255 /*
256 * something like "int struct a ..."
257 * struct/union/enum with anything else is not allowed
258 */
259 if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC ||
260 dcs->d_rank_mod != NO_TSPEC || dcs->d_sign_mod != NO_TSPEC) {
261 dcs->d_invalid_type_combination = true;
262 dcs->d_abstract_type = NO_TSPEC;
263 dcs->d_sign_mod = NO_TSPEC;
264 dcs->d_rank_mod = NO_TSPEC;
265 }
266 dcs->d_type = tp;
267 return;
268 }
269
270 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
271 /*
272 * something like "struct a int"
273 * struct/union/enum with anything else is not allowed
274 */
275 dcs->d_invalid_type_combination = true;
276 return;
277 }
278
279 if (t == COMPLEX) {
280 if (dcs->d_complex_mod == FLOAT)
281 t = FCOMPLEX;
282 else if (dcs->d_complex_mod == DOUBLE)
283 t = DCOMPLEX;
284 else {
285 /* invalid type for _Complex */
286 error(308);
287 t = DCOMPLEX; /* just as a fallback */
288 }
289 dcs->d_complex_mod = NO_TSPEC;
290 }
291
292 if (t == LONG && dcs->d_rank_mod == LONG) {
293 /* "long long" or "long ... long" */
294 t = QUAD;
295 dcs->d_rank_mod = NO_TSPEC;
296 if (!quadflg)
297 /* %s does not support 'long long' */
298 c99ism(265, allow_c90 ? "C90" : "traditional C");
299 }
300
301 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
302 /* something like "typedef int a; a long ..." */
303 dcs->d_type = typedef_error(dcs->d_type, t);
304 return;
305 }
306
307 /* now it can be only a combination of arithmetic types and void */
308 if (t == SIGNED || t == UNSIGN) {
309 if (dcs->d_sign_mod != NO_TSPEC)
310 dcs->d_invalid_type_combination = true;
311 dcs->d_sign_mod = t;
312 } else if (t == SHORT || t == LONG || t == QUAD) {
313 if (dcs->d_rank_mod != NO_TSPEC)
314 dcs->d_invalid_type_combination = true;
315 dcs->d_rank_mod = t;
316 } else if (t == FLOAT || t == DOUBLE) {
317 if (dcs->d_rank_mod == NO_TSPEC || dcs->d_rank_mod == LONG) {
318 if (dcs->d_complex_mod != NO_TSPEC
319 || (t == FLOAT && dcs->d_rank_mod == LONG))
320 dcs->d_invalid_type_combination = true;
321 dcs->d_complex_mod = t;
322 } else {
323 if (dcs->d_abstract_type != NO_TSPEC)
324 dcs->d_invalid_type_combination = true;
325 dcs->d_abstract_type = t;
326 }
327 } else if (t == PTR) {
328 dcs->d_type = tp;
329 } else {
330 if (dcs->d_abstract_type != NO_TSPEC)
331 dcs->d_invalid_type_combination = true;
332 dcs->d_abstract_type = t;
333 }
334 }
335
336 /* Merge the signedness into the abstract type. */
337 static tspec_t
338 merge_signedness(tspec_t t, tspec_t s)
339 {
340
341 if (s == SIGNED)
342 return t == CHAR ? SCHAR : t;
343 if (s != UNSIGN)
344 return t;
345 return t == CHAR ? UCHAR
346 : t == SHORT ? USHORT
347 : t == INT ? UINT
348 : t == LONG ? ULONG
349 : t == QUAD ? UQUAD
350 : t;
351 }
352
353 /*
354 * Called if a list of declaration specifiers contains a typedef name
355 * and other specifiers (except struct, union, enum, typedef name).
356 */
357 static type_t *
358 typedef_error(type_t *td, tspec_t t)
359 {
360
361 tspec_t t2 = td->t_tspec;
362
363 if ((t == SIGNED || t == UNSIGN) &&
364 (t2 == CHAR || t2 == SHORT || t2 == INT ||
365 t2 == LONG || t2 == QUAD)) {
366 if (allow_c90)
367 /* modifying typedef with '%s'; only qualifiers... */
368 warning(5, tspec_name(t));
369 td = block_dup_type(gettyp(merge_signedness(t2, t)));
370 td->t_typedef = true;
371 return td;
372 }
373
374 if (t == SHORT && (t2 == INT || t2 == UINT)) {
375 /* modifying typedef with '%s'; only qualifiers allowed */
376 warning(5, "short");
377 td = block_dup_type(gettyp(t2 == INT ? SHORT : USHORT));
378 td->t_typedef = true;
379 return td;
380 }
381
382 if (t != LONG)
383 goto invalid;
384
385 if (t2 == INT)
386 td = gettyp(LONG);
387 else if (t2 == UINT)
388 td = gettyp(ULONG);
389 else if (t2 == LONG)
390 td = gettyp(QUAD);
391 else if (t2 == ULONG)
392 td = gettyp(UQUAD);
393 else if (t2 == FLOAT)
394 td = gettyp(DOUBLE);
395 else if (t2 == DOUBLE)
396 td = gettyp(LDOUBLE);
397 else if (t2 == DCOMPLEX)
398 td = gettyp(LCOMPLEX);
399 else
400 goto invalid;
401
402 /* modifying typedef with '%s'; only qualifiers allowed */
403 warning(5, "long");
404 td = block_dup_type(td);
405 td->t_typedef = true;
406 return td;
407
408 invalid:
409 /* Anything else is not accepted. */
410 dcs->d_invalid_type_combination = true;
411 return td;
412 }
413
414 static void
415 set_first_typedef(type_t *tp, sym_t *sym)
416 {
417
418 tspec_t t = tp->t_tspec;
419 if (is_struct_or_union(t) && tp->t_sou->sou_first_typedef == NULL)
420 tp->t_sou->sou_first_typedef = sym;
421 if (t == ENUM && tp->t_enum->en_first_typedef == NULL)
422 tp->t_enum->en_first_typedef = sym;
423 }
424
425 static unsigned int
426 bit_fields_width(sym_t **mem, bool *named)
427 {
428 unsigned int width = 0;
429 unsigned int align = 0;
430 while (*mem != NULL && (*mem)->s_type->t_bitfield) {
431 if ((*mem)->s_name != unnamed)
432 *named = true;
433 width += (*mem)->s_type->t_bit_field_width;
434 unsigned int mem_align = alignment_in_bits((*mem)->s_type);
435 if (mem_align > align)
436 align = mem_align;
437 *mem = (*mem)->s_next;
438 }
439 return (width + align - 1) & -align;
440 }
441
442 static void
443 pack_struct_or_union(type_t *tp)
444 {
445
446 if (!is_struct_or_union(tp->t_tspec)) {
447 /* attribute '%s' ignored for '%s' */
448 warning(326, "packed", type_name(tp));
449 return;
450 }
451
452 unsigned int bits = 0;
453 bool named = false;
454 for (sym_t *mem = tp->t_sou->sou_first_member;
455 mem != NULL; mem = mem->s_next) {
456 // TODO: Maybe update mem->u.s_member.sm_offset_in_bits.
457 if (mem->s_type->t_bitfield) {
458 bits += bit_fields_width(&mem, &named);
459 if (mem == NULL)
460 break;
461 }
462 unsigned int mem_bits = type_size_in_bits(mem->s_type);
463 if (tp->t_tspec == STRUCT)
464 bits += mem_bits;
465 else if (mem_bits > bits)
466 bits = mem_bits;
467 }
468 tp->t_sou->sou_size_in_bits = bits;
469 }
470
471 void
472 dcs_add_packed(void)
473 {
474 if (dcs->d_type == NULL)
475 dcs->d_packed = true;
476 else
477 pack_struct_or_union(dcs->d_type);
478 }
479
480 void
481 dcs_set_used(void)
482 {
483 dcs->d_used = true;
484 }
485
486 /*
487 * Remember a qualifier that is part of the declaration specifiers (and not the
488 * declarator). The remembered qualifier is used by dcs_end_type for all
489 * declarators.
490 */
491 void
492 dcs_add_qualifier(tqual_t q)
493 {
494
495 if (q == CONST) {
496 if (dcs->d_const) {
497 /* duplicate '%s' */
498 warning(10, "const");
499 }
500 dcs->d_const = true;
501 } else if (q == VOLATILE) {
502 if (dcs->d_volatile) {
503 /* duplicate '%s' */
504 warning(10, "volatile");
505 }
506 dcs->d_volatile = true;
507 } else {
508 lint_assert(q == RESTRICT || q == THREAD || q == ATOMIC);
509 /* Silently ignore these qualifiers. */
510 }
511 }
512
513 void
514 begin_declaration_level(declaration_kind dk)
515 {
516
517 dinfo_t *di = xcalloc(1, sizeof(*di));
518 di->d_enclosing = dcs;
519 di->d_kind = dk;
520 di->d_last_dlsym = &di->d_first_dlsym;
521 dcs = di;
522 debug_step("%s(%s)", __func__, declaration_kind_name(dk));
523 }
524
525 void
526 end_declaration_level(void)
527 {
528 dinfo_t *di;
529
530 debug_step("%s(%s)", __func__, declaration_kind_name(dcs->d_kind));
531
532 lint_assert(dcs->d_enclosing != NULL);
533 di = dcs;
534 dcs = di->d_enclosing;
535
536 switch (di->d_kind) {
537 case DK_STRUCT_MEMBER:
538 case DK_UNION_MEMBER:
539 case DK_ENUM_CONSTANT:
540 /*
541 * Symbols declared in (nested) structs or enums are part of
542 * the next level (they are removed from the symbol table if
543 * the symbols of the outer level are removed).
544 */
545 if ((*dcs->d_last_dlsym = di->d_first_dlsym) != NULL)
546 dcs->d_last_dlsym = di->d_last_dlsym;
547 break;
548 case DK_OLD_STYLE_ARG:
549 /*
550 * All symbols in dcs->d_first_dlsym are introduced in
551 * old-style argument declarations (it's not clean, but
552 * possible). They are appended to the list of symbols declared
553 * in an old-style argument identifier list or a new-style
554 * parameter type list.
555 */
556 if (di->d_first_dlsym != NULL) {
557 *di->d_last_dlsym = dcs->d_func_proto_syms;
558 dcs->d_func_proto_syms = di->d_first_dlsym;
559 }
560 break;
561 case DK_ABSTRACT: /* casts and sizeof */
562 /*
563 * Append all symbols declared in the abstract declaration to
564 * the list of symbols declared in the surrounding declaration
565 * or block.
566 *
567 * XXX I'm not sure whether they should be removed from the
568 * symbol table now or later.
569 */
570 if ((*dcs->d_last_dlsym = di->d_first_dlsym) != NULL)
571 dcs->d_last_dlsym = di->d_last_dlsym;
572 break;
573 case DK_AUTO:
574 check_usage(di);
575 /* FALLTHROUGH */
576 case DK_PROTO_ARG:
577 /* usage of arguments will be checked by end_function() */
578 rmsyms(di->d_first_dlsym);
579 break;
580 case DK_EXTERN:
581 /* there is nothing around an external declarations */
582 /* FALLTHROUGH */
583 default:
584 lint_assert(/*CONSTCOND*/false);
585 }
586 free(di);
587 }
588
589 /*
590 * Set flag d_asm in all declaration stack elements up to the outermost one.
591 *
592 * This is used to mark compound statements which have, possibly in nested
593 * compound statements, asm statements. For these compound statements, no
594 * warnings about unused or uninitialized variables are printed.
595 *
596 * There is no need to clear d_asm in dinfo structs with context AUTO, because
597 * these structs are freed at the end of the compound statement. But it must be
598 * cleared in the outermost dinfo struct, which has context EXTERN. This could
599 * be done in dcs_begin_type and would work for C90, but not for C99 or C++
600 * (due to mixed statements and declarations). Thus, we clear it in
601 * global_clean_up_decl.
602 */
603 void
604 dcs_set_asm(void)
605 {
606
607 for (dinfo_t *di = dcs; di != NULL; di = di->d_enclosing)
608 di->d_asm = true;
609 }
610
611 void
612 dcs_begin_type(void)
613 {
614
615 dcs->d_abstract_type = NO_TSPEC;
616 dcs->d_complex_mod = NO_TSPEC;
617 dcs->d_sign_mod = NO_TSPEC;
618 dcs->d_rank_mod = NO_TSPEC;
619 dcs->d_scl = NOSCL;
620 dcs->d_type = NULL;
621 dcs->d_const = false;
622 dcs->d_volatile = false;
623 dcs->d_inline = false;
624 dcs->d_multiple_storage_classes = false;
625 dcs->d_invalid_type_combination = false;
626 dcs->d_nonempty_decl = false;
627 dcs->d_no_type_specifier = false;
628 }
629
630 static void
631 dcs_adjust_storage_class(void)
632 {
633 if (dcs->d_kind == DK_EXTERN) {
634 if (dcs->d_scl == REG || dcs->d_scl == AUTO) {
635 /* illegal storage class */
636 error(8);
637 dcs->d_scl = NOSCL;
638 }
639 } else if (dcs->d_kind == DK_OLD_STYLE_ARG ||
640 dcs->d_kind == DK_PROTO_ARG) {
641 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) {
642 /* only register valid as formal parameter ... */
643 error(9);
644 dcs->d_scl = NOSCL;
645 }
646 }
647 }
648
649 /*
650 * Merge the declaration specifiers from dcs into dcs->d_type.
651 *
652 * See C99 6.7.2 "Type specifiers".
653 */
654 static void
655 dcs_merge_declaration_specifiers(void)
656 {
657 tspec_t t, s, l, c;
658 type_t *tp;
659
660 t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */
661 c = dcs->d_complex_mod; /* FLOAT or DOUBLE */
662 s = dcs->d_sign_mod; /* SIGNED or UNSIGN */
663 l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */
664 tp = dcs->d_type;
665
666 debug_step("%s: %s", __func__, type_name(tp));
667 if (t == NO_TSPEC && s == NO_TSPEC && l == NO_TSPEC && c == NO_TSPEC &&
668 tp == NULL)
669 dcs->d_no_type_specifier = true;
670 if (t == NO_TSPEC && s == NO_TSPEC && (l == NO_TSPEC || l == LONG) &&
671 tp == NULL)
672 t = c;
673
674 if (tp != NULL) {
675 lint_assert(t == NO_TSPEC);
676 lint_assert(s == NO_TSPEC);
677 lint_assert(l == NO_TSPEC);
678 return;
679 }
680
681 if (t == NO_TSPEC)
682 t = INT;
683 if (s == NO_TSPEC && t == INT)
684 s = SIGNED;
685 if (l != NO_TSPEC && t == CHAR) {
686 dcs->d_invalid_type_combination = true;
687 l = NO_TSPEC;
688 }
689 if (l == LONG && t == FLOAT) {
690 l = NO_TSPEC;
691 t = DOUBLE;
692 if (allow_c90)
693 /* use 'double' instead of 'long float' */
694 warning(6);
695 }
696 if ((l == LONG && t == DOUBLE) || t == LDOUBLE) {
697 l = NO_TSPEC;
698 t = LDOUBLE;
699 }
700 if (t == LDOUBLE && !allow_c90) {
701 /* 'long double' is illegal in traditional C */
702 warning(266);
703 }
704 if (l == LONG && t == DCOMPLEX) {
705 l = NO_TSPEC;
706 t = LCOMPLEX;
707 }
708
709 if (t != INT && t != CHAR && (s != NO_TSPEC || l != NO_TSPEC)) {
710 dcs->d_invalid_type_combination = true;
711 l = s = NO_TSPEC;
712 }
713 if (l != NO_TSPEC)
714 t = l;
715 dcs->d_type = gettyp(merge_signedness(t, s));
716 }
717
718 /* Create a type in 'dcs->d_type' from the information gathered in 'dcs'. */
719 void
720 dcs_end_type(void)
721 {
722
723 dcs_merge_declaration_specifiers();
724
725 if (dcs->d_multiple_storage_classes) {
726 /* only one storage class allowed */
727 error(7);
728 }
729 if (dcs->d_invalid_type_combination) {
730 /* illegal type combination */
731 error(4);
732 }
733
734 dcs_adjust_storage_class();
735
736 if (dcs->d_const && dcs->d_type->t_const && !dcs->d_type->t_typeof) {
737 lint_assert(dcs->d_type->t_typedef);
738 /* typedef already qualified with '%s' */
739 warning(68, "const");
740 }
741 if (dcs->d_volatile && dcs->d_type->t_volatile &&
742 !dcs->d_type->t_typeof) {
743 lint_assert(dcs->d_type->t_typedef);
744 /* typedef already qualified with '%s' */
745 warning(68, "volatile");
746 }
747
748 if (dcs->d_const || dcs->d_volatile) {
749 dcs->d_type = block_dup_type(dcs->d_type);
750 dcs->d_type->t_const |= dcs->d_const;
751 dcs->d_type->t_volatile |= dcs->d_volatile;
752 }
753 }
754
755 /*
756 * Return the length of a type in bits. For bit-fields, return the length of
757 * the underlying storage type.
758 *
759 * Printing a message if the outermost dimension of an array is 0 must
760 * be done by the caller. All other problems are reported by this function
761 * if name is not NULL.
762 */
763 int
764 length_in_bits(const type_t *tp, const char *name)
765 {
766
767 if (tp == NULL)
768 return -1;
769
770 unsigned int elem = 1;
771 while (tp->t_tspec == ARRAY) {
772 elem *= tp->t_dim;
773 tp = tp->t_subt;
774 }
775
776 if (is_struct_or_union(tp->t_tspec)) {
777 if (is_incomplete(tp) && name != NULL) {
778 /* '%s' has incomplete type '%s' */
779 error(31, name, type_name(tp));
780 }
781 return (int)(elem * tp->t_sou->sou_size_in_bits);
782 }
783
784 if (tp->t_tspec == ENUM && is_incomplete(tp) && name != NULL)
785 /* incomplete enum type '%s' */
786 warning(13, name);
787
788 lint_assert(tp->t_tspec != FUNC);
789
790 unsigned int elsz = size_in_bits(tp->t_tspec);
791 /*
792 * Workaround until the type parser (see add_function, add_array,
793 * add_pointer) does not construct the invalid intermediate declaration
794 * 'void b[4]' for the legitimate declaration 'void *b[4]'.
795 */
796 if (sytxerr > 0 && elsz == 0)
797 elsz = CHAR_SIZE;
798 lint_assert(elsz > 0);
799 return (int)(elem * elsz);
800 }
801
802 unsigned int
803 alignment_in_bits(const type_t *tp)
804 {
805
806 /* Super conservative so that it works for most systems. */
807 unsigned int worst_align_in_bits = 2 * LONG_SIZE;
808
809 while (tp->t_tspec == ARRAY)
810 tp = tp->t_subt;
811
812 tspec_t t = tp->t_tspec;
813 unsigned int a;
814 if (is_struct_or_union(t))
815 a = tp->t_sou->sou_align_in_bits;
816 else {
817 lint_assert(t != FUNC);
818 if ((a = size_in_bits(t)) == 0)
819 a = CHAR_SIZE;
820 else if (a > worst_align_in_bits)
821 a = worst_align_in_bits;
822 }
823 lint_assert(a >= CHAR_SIZE);
824 lint_assert(a <= worst_align_in_bits);
825 return a;
826 }
827
828 /*
829 * Concatenate two lists of symbols by s_next. Used by declarations of
830 * struct/union/enum elements and parameters.
831 */
832 sym_t *
833 concat_symbols(sym_t *l1, sym_t *l2)
834 {
835
836 if (l1 == NULL)
837 return l2;
838 sym_t *l = l1;
839 while (l->s_next != NULL)
840 l = l->s_next;
841 l->s_next = l2;
842 return l1;
843 }
844
845 /*
846 * Check if the type of the given symbol is valid.
847 *
848 * Invalid types are:
849 * - arrays of incomplete types or functions
850 * - functions returning arrays or functions
851 * - void types other than type of function or pointer
852 */
853 void
854 check_type(sym_t *sym)
855 {
856
857 type_t **tpp = &sym->s_type;
858 tspec_t to = NO_TSPEC;
859 while (*tpp != NULL) {
860 type_t *tp = *tpp;
861 tspec_t t = tp->t_tspec;
862 /*
863 * If this is the type of an old-style function definition,
864 * a better warning is printed in begin_function().
865 */
866 if (t == FUNC && !tp->t_proto &&
867 !(to == NO_TSPEC && sym->s_osdef)) {
868 /* TODO: Make this an error in C99 mode as well. */
869 if ((!allow_trad && !allow_c99) && hflag)
870 /* function declaration is not a prototype */
871 warning(287);
872 }
873 if (to == FUNC) {
874 if (t == FUNC || t == ARRAY) {
875 /* function returns illegal type '%s' */
876 error(15, type_name(tp));
877 *tpp = block_derive_type(
878 t == FUNC ? *tpp : (*tpp)->t_subt, PTR);
879 return;
880 }
881 if (tp->t_const || tp->t_volatile) {
882 /* TODO: Make this a warning in C99 mode as well. */
883 if (!allow_trad && !allow_c99) { /* XXX or better allow_c90? */
884 /* function cannot return const... */
885 warning(228);
886 }
887 }
888 } else if (to == ARRAY) {
889 if (t == FUNC) {
890 /* array of function is illegal */
891 error(16);
892 *tpp = gettyp(INT);
893 return;
894 }
895 if (t == ARRAY && tp->t_dim == 0) {
896 /* null dimension */
897 error(17);
898 return;
899 }
900 if (t == VOID) {
901 /* illegal use of 'void' */
902 error(18);
903 *tpp = gettyp(INT);
904 }
905 /*
906 * No need to check for incomplete types here as
907 * length_in_bits already does this.
908 */
909 } else if (to == NO_TSPEC && t == VOID) {
910 if (dcs->d_kind == DK_PROTO_ARG) {
911 if (sym->s_scl != ABSTRACT) {
912 lint_assert(sym->s_name != unnamed);
913 /* void parameter '%s' cannot ... */
914 error(61, sym->s_name);
915 *tpp = gettyp(INT);
916 }
917 } else if (dcs->d_kind == DK_ABSTRACT) {
918 /* ok */
919 } else if (sym->s_scl != TYPEDEF) {
920 /* void type for '%s' */
921 error(19, sym->s_name);
922 *tpp = gettyp(INT);
923 }
924 }
925 if (t == VOID && to != PTR) {
926 if (tp->t_const || tp->t_volatile) {
927 /* inappropriate qualifiers with 'void' */
928 warning(69);
929 tp->t_const = tp->t_volatile = false;
930 }
931 }
932 tpp = &tp->t_subt;
933 to = t;
934 }
935 }
936
937 /*
938 * In traditional C, the only portable type for bit-fields is unsigned int.
939 *
940 * In C90, the only allowed types for bit-fields are int, signed int and
941 * unsigned int (3.5.2.1). There is no mention of implementation-defined
942 * types.
943 *
944 * In C99, the only portable types for bit-fields are _Bool, signed int and
945 * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other
946 * implementation-defined type".
947 */
948 static void
949 check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t)
950 {
951 type_t *tp = *inout_tp;
952 tspec_t t = *inout_t;
953
954 if (t == CHAR || t == UCHAR || t == SCHAR ||
955 t == SHORT || t == USHORT || t == ENUM) {
956 if (!bitfieldtype_ok) {
957 /* TODO: Make this an error in C99 mode as well. */
958 if (!allow_trad && !allow_c99) {
959 type_t *btp = block_dup_type(tp);
960 btp->t_bitfield = false;
961 /* bit-field type '%s' invalid in ANSI C */
962 warning(273, type_name(btp));
963 } else if (pflag) {
964 type_t *btp = block_dup_type(tp);
965 btp->t_bitfield = false;
966 /* nonportable bit-field type '%s' */
967 warning(34, type_name(btp));
968 }
969 }
970 } else if (t == INT && dcs->d_sign_mod == NO_TSPEC) {
971 if (pflag && !bitfieldtype_ok) {
972 /* bit-field of type plain 'int' has ... */
973 warning(344);
974 }
975 } else if (!(t == INT || t == UINT || t == BOOL ||
976 (is_integer(t) && (bitfieldtype_ok || allow_gcc)))) {
977
978 type_t *btp = block_dup_type(tp);
979 btp->t_bitfield = false;
980 /* illegal bit-field type '%s' */
981 warning(35, type_name(btp));
982
983 // XXX: What about _Bool bit-fields since C99 6.7.2.1?
984 unsigned int width = tp->t_bit_field_width;
985 dsym->s_type = tp = block_dup_type(gettyp(t = INT));
986 if ((tp->t_bit_field_width = width) > size_in_bits(t))
987 tp->t_bit_field_width = size_in_bits(t);
988 *inout_t = t;
989 *inout_tp = tp;
990 }
991 }
992
993 static void
994 check_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp)
995 {
996
997 check_bit_field_type(dsym, inout_tp, inout_t);
998
999 type_t *tp = *inout_tp;
1000 tspec_t t = *inout_t;
1001 unsigned int t_width = size_in_bits(t);
1002 if (tp->t_bit_field_width > t_width) {
1003 /* illegal bit-field size: %d */
1004 error(36, (int)tp->t_bit_field_width);
1005 tp->t_bit_field_width = t_width;
1006 } else if (tp->t_bit_field_width == 0 && dsym->s_name != unnamed) {
1007 /* zero size bit-field */
1008 error(37);
1009 tp->t_bit_field_width = t_width;
1010 }
1011 if (dsym->s_scl == UNION_MEMBER) {
1012 /* bit-field in union is very unusual */
1013 warning(41);
1014 dsym->s_type->t_bitfield = false;
1015 dsym->s_bitfield = false;
1016 }
1017 }
1018
1019 /* Add a member to the struct or union type that is being built in 'dcs'. */
1020 static void
1021 dcs_add_member(sym_t *mem)
1022 {
1023 type_t *tp = mem->s_type;
1024
1025 unsigned int union_offset = 0;
1026 if (dcs->d_kind == DK_UNION_MEMBER) {
1027 union_offset = dcs->d_offset_in_bits;
1028 dcs->d_offset_in_bits = 0;
1029 }
1030
1031 if (mem->s_bitfield) {
1032 dcs_align(alignment_in_bits(tp), tp->t_bit_field_width);
1033 // XXX: Why round down?
1034 mem->u.s_member.sm_offset_in_bits = dcs->d_offset_in_bits
1035 - dcs->d_offset_in_bits % size_in_bits(tp->t_tspec);
1036 tp->t_bit_field_offset = dcs->d_offset_in_bits
1037 - mem->u.s_member.sm_offset_in_bits;
1038 dcs->d_offset_in_bits += tp->t_bit_field_width;
1039 } else {
1040 dcs_align(alignment_in_bits(tp), 0);
1041 mem->u.s_member.sm_offset_in_bits = dcs->d_offset_in_bits;
1042 dcs->d_offset_in_bits += type_size_in_bits(tp);
1043 }
1044
1045 if (union_offset > dcs->d_offset_in_bits)
1046 dcs->d_offset_in_bits = union_offset;
1047 }
1048
1049 sym_t *
1050 declare_unnamed_member(void)
1051 {
1052
1053 sym_t *mem = block_zero_alloc(sizeof(*mem));
1054 mem->s_name = unnamed;
1055 mem->s_type = dcs->d_type;
1056
1057 dcs_add_member(mem);
1058 bitfieldtype_ok = false;
1059 return mem;
1060 }
1061
1062 sym_t *
1063 declare_member(sym_t *dsym)
1064 {
1065
1066 lint_assert(is_member(dsym));
1067
1068 if (dcs->d_redeclared_symbol != NULL) {
1069 lint_assert(is_member(dcs->d_redeclared_symbol));
1070
1071 if (dsym->u.s_member.sm_sou_type ==
1072 dcs->d_redeclared_symbol->u.s_member.sm_sou_type) {
1073 /* duplicate member name '%s' */
1074 error(33, dsym->s_name);
1075 rmsym(dcs->d_redeclared_symbol);
1076 }
1077 }
1078
1079 check_type(dsym);
1080
1081 type_t *tp = dsym->s_type;
1082 tspec_t t = tp->t_tspec;
1083 if (dsym->s_bitfield)
1084 check_bit_field(dsym, &t, &tp);
1085 else if (t == FUNC) {
1086 /* function illegal in structure or union */
1087 error(38);
1088 dsym->s_type = tp = block_derive_type(tp, t = PTR);
1089 }
1090
1091 /*
1092 * bit-fields of length 0 are not warned about because length_in_bits
1093 * does not return the length of the bit-field but the length
1094 * of the type the bit-field is packed in (it's ok)
1095 */
1096 int sz = length_in_bits(dsym->s_type, dsym->s_name);
1097 if (sz == 0 && t == ARRAY && dsym->s_type->t_dim == 0) {
1098 /* zero-sized array '%s' in struct is a C99 extension */
1099 c99ism(39, dsym->s_name);
1100 }
1101
1102 dcs_add_member(dsym);
1103
1104 check_function_definition(dsym, false);
1105
1106 /*
1107 * Clear the BITFIELDTYPE indicator after processing each
1108 * structure element.
1109 */
1110 bitfieldtype_ok = false;
1111
1112 return dsym;
1113 }
1114
1115 /* Aligns the next structure element as required. */
1116 static void
1117 dcs_align(unsigned int member_alignment, unsigned int bit_field_width)
1118 {
1119
1120 if (member_alignment > dcs->d_sou_align_in_bits)
1121 dcs->d_sou_align_in_bits = member_alignment;
1122
1123 unsigned int offset = (dcs->d_offset_in_bits + member_alignment - 1)
1124 & ~(member_alignment - 1);
1125 if (bit_field_width == 0
1126 || dcs->d_offset_in_bits + bit_field_width > offset)
1127 dcs->d_offset_in_bits = offset;
1128 }
1129
1130 sym_t *
1131 set_bit_field_width(sym_t *dsym, int bit_field_width)
1132 {
1133
1134 if (dsym == NULL) {
1135 dsym = block_zero_alloc(sizeof(*dsym));
1136 dsym->s_name = unnamed;
1137 dsym->s_kind = FMEMBER;
1138 dsym->s_scl = STRUCT_MEMBER;
1139 dsym->s_type = gettyp(UINT);
1140 dsym->s_block_level = -1;
1141 }
1142 dsym->s_type = block_dup_type(dsym->s_type);
1143 dsym->s_type->t_bitfield = true;
1144 dsym->s_type->t_bit_field_width = bit_field_width;
1145 dsym->s_bitfield = true;
1146 return dsym;
1147 }
1148
1149 /*
1150 * A sequence of asterisks and qualifiers, from right to left. For example,
1151 * 'const ***volatile **const volatile' results in [cvp, p, vp, p, p]. The
1152 * leftmost 'const' is not included in this list, it is stored in dcs->d_const
1153 * instead.
1154 */
1155 qual_ptr *
1156 merge_qualified_pointer(qual_ptr *p1, qual_ptr *p2)
1157 {
1158
1159 if (p2 == NULL)
1160 return p1; /* for optional qualifiers */
1161
1162 if (p2->p_pointer) {
1163 /* append p1 to p2, keeping p2 */
1164 qual_ptr *tail = p2;
1165 while (tail->p_next != NULL)
1166 tail = tail->p_next;
1167 tail->p_next = p1;
1168 return p2;
1169 }
1170
1171 /* merge p2 into p1, keeping p1 */
1172 if (p2->p_const) {
1173 if (p1->p_const) {
1174 /* duplicate '%s' */
1175 warning(10, "const");
1176 }
1177 p1->p_const = true;
1178 }
1179 if (p2->p_volatile) {
1180 if (p1->p_volatile) {
1181 /* duplicate '%s' */
1182 warning(10, "volatile");
1183 }
1184 p1->p_volatile = true;
1185 }
1186 free(p2);
1187 return p1;
1188 }
1189
1190 static type_t *
1191 block_derive_pointer(type_t *stp, bool is_const, bool is_volatile)
1192 {
1193
1194 type_t *tp = block_derive_type(stp, PTR);
1195 tp->t_const = is_const;
1196 tp->t_volatile = is_volatile;
1197 return tp;
1198 }
1199
1200 /*
1201 * The following 3 functions extend the type of a declarator with
1202 * pointer, function and array types.
1203 *
1204 * The current type is the type built by dcs_end_type (dcs->d_type) and
1205 * pointer, function and array types already added for this
1206 * declarator. The new type extension is inserted between both.
1207 */
1208 sym_t *
1209 add_pointer(sym_t *decl, qual_ptr *p)
1210 {
1211
1212 debug_dinfo(dcs);
1213
1214 type_t **tpp = &decl->s_type;
1215 while (*tpp != NULL && *tpp != dcs->d_type)
1216 tpp = &(*tpp)->t_subt;
1217 if (*tpp == NULL) {
1218 debug_step("add_pointer: unchanged '%s'",
1219 type_name(decl->s_type));
1220 return decl;
1221 }
1222
1223 while (p != NULL) {
1224 *tpp = block_derive_pointer(dcs->d_type,
1225 p->p_const, p->p_volatile);
1226
1227 tpp = &(*tpp)->t_subt;
1228
1229 qual_ptr *next = p->p_next;
1230 free(p);
1231 p = next;
1232 }
1233 debug_step("add_pointer: '%s'", type_name(decl->s_type));
1234 return decl;
1235 }
1236
1237 static type_t *
1238 block_derive_array(type_t *stp, bool dim, int len)
1239 {
1240
1241 type_t *tp = block_derive_type(stp, ARRAY);
1242 tp->t_dim = len;
1243
1244 #if 0
1245 /*
1246 * As of 2022-04-03, the implementation of the type parser (see
1247 * add_function, add_array, add_pointer) is strange. When it sees
1248 * the type 'void *b[4]', it first creates 'void b[4]' and only later
1249 * inserts the '*' in the middle of the type. Late modifications like
1250 * these should not be done at all, instead the parser should be fixed
1251 * to process the type names in the proper syntactical order.
1252 *
1253 * Since the intermediate type would be an array of void, but the
1254 * final type is valid, this check cannot be enabled yet.
1255 */
1256 if (stp->t_tspec == VOID) {
1257 /* array of incomplete type */
1258 error(301);
1259 tp->t_subt = gettyp(CHAR);
1260 }
1261 #endif
1262 if (len < 0) {
1263 /* negative array dimension (%d) */
1264 error(20, len);
1265 } else if (len == 0 && dim) {
1266 /* zero sized array is a C99 extension */
1267 c99ism(322);
1268 } else if (len == 0 && !dim)
1269 tp->t_incomplete_array = true;
1270
1271 return tp;
1272 }
1273
1274 /*
1275 * If a dimension was specified, dim is true, otherwise false
1276 * n is the specified dimension
1277 */
1278 sym_t *
1279 add_array(sym_t *decl, bool dim, int n)
1280 {
1281
1282 debug_dinfo(dcs);
1283
1284 type_t **tpp = &decl->s_type;
1285 while (*tpp != NULL && *tpp != dcs->d_type)
1286 tpp = &(*tpp)->t_subt;
1287 if (*tpp == NULL) {
1288 debug_step("add_array: unchanged '%s'",
1289 type_name(decl->s_type));
1290 return decl;
1291 }
1292
1293 *tpp = block_derive_array(dcs->d_type, dim, n);
1294
1295 debug_step("add_array: '%s'", type_name(decl->s_type));
1296 return decl;
1297 }
1298
1299 static type_t *
1300 block_derive_function(type_t *ret, bool proto, sym_t *args, bool vararg)
1301 {
1302
1303 type_t *tp = block_derive_type(ret, FUNC);
1304 tp->t_proto = proto;
1305 if (proto)
1306 tp->t_args = args;
1307 tp->t_vararg = vararg;
1308 return tp;
1309 }
1310
1311 sym_t *
1312 add_function(sym_t *decl, sym_t *args)
1313 {
1314
1315 debug_enter();
1316 debug_dinfo(dcs);
1317 debug_sym("decl: ", decl, "\n");
1318 #ifdef DEBUG
1319 for (const sym_t *arg = args; arg != NULL; arg = arg->s_next)
1320 debug_sym("arg: ", arg, "\n");
1321 #endif
1322
1323 if (dcs->d_prototype) {
1324 if (!allow_c90)
1325 /* function prototypes are illegal in traditional C */
1326 warning(270);
1327 args = new_style_function(args);
1328 } else
1329 old_style_function(decl, args);
1330
1331 /*
1332 * The symbols are removed from the symbol table by
1333 * end_declaration_level after add_function. To be able to restore
1334 * them if this is a function definition, a pointer to the list of
1335 * all symbols is stored in dcs->d_enclosing->d_func_proto_syms. Also,
1336 * a list of the arguments (concatenated by s_next) is stored in
1337 * dcs->d_enclosing->d_func_args. (dcs->d_enclosing must be used
1338 * because *dcs is the declaration stack element created for the list
1339 * of params and is removed after add_function.)
1340 */
1341 if (dcs->d_enclosing->d_kind == DK_EXTERN &&
1342 decl->s_type == dcs->d_enclosing->d_type) {
1343 dcs->d_enclosing->d_func_proto_syms = dcs->d_first_dlsym;
1344 dcs->d_enclosing->d_func_args = args;
1345 }
1346
1347 /*
1348 * XXX: What is this code doing on a semantic level, and why?
1349 * Returning decl leads to the wrong function types in msg_347.
1350 */
1351 type_t **tpp = &decl->s_type;
1352 if (*tpp == NULL)
1353 decl->s_type = dcs->d_enclosing->d_type;
1354 while (*tpp != NULL && *tpp != dcs->d_enclosing->d_type)
1355 /*
1356 * XXX: accessing INT->t_subt feels strange, even though it
1357 * may even be guaranteed to be NULL.
1358 */
1359 tpp = &(*tpp)->t_subt;
1360 if (*tpp == NULL) {
1361 debug_step("add_function: unchanged '%s'",
1362 type_name(decl->s_type));
1363 debug_leave();
1364 return decl; /* see msg_347 */
1365 }
1366
1367 *tpp = block_derive_function(dcs->d_enclosing->d_type,
1368 dcs->d_prototype, args, dcs->d_vararg);
1369
1370 debug_step("add_function: '%s'", type_name(decl->s_type));
1371 debug_leave();
1372 return decl;
1373 }
1374
1375 static sym_t *
1376 new_style_function(sym_t *args)
1377 {
1378
1379 for (sym_t *sym = dcs->d_first_dlsym;
1380 sym != NULL; sym = sym->s_level_next) {
1381 scl_t sc = sym->s_scl;
1382 if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) {
1383 /* dubious tag declaration '%s %s' */
1384 warning(85, storage_class_name(sc), sym->s_name);
1385 }
1386 }
1387
1388 for (sym_t *arg = args; arg != NULL; arg = arg->s_next) {
1389 if (arg->s_type->t_tspec == VOID &&
1390 !(arg == args && arg->s_next == NULL)) {
1391 /* void must be sole parameter */
1392 error(60);
1393 arg->s_type = gettyp(INT);
1394 }
1395 }
1396
1397 if (args == NULL || args->s_type->t_tspec == VOID)
1398 return NULL;
1399 return args;
1400 }
1401
1402 static void
1403 old_style_function(sym_t *decl, sym_t *args)
1404 {
1405
1406 /*
1407 * Remember the list of parameters only if this really seems to be a
1408 * function definition.
1409 */
1410 if (dcs->d_enclosing->d_kind == DK_EXTERN &&
1411 decl->s_type == dcs->d_enclosing->d_type) {
1412 /*
1413 * Assume that this becomes a function definition. If not, it
1414 * will be corrected in check_function_definition.
1415 */
1416 if (args != NULL) {
1417 decl->s_osdef = true;
1418 decl->u.s_old_style_args = args;
1419 }
1420 } else {
1421 if (args != NULL)
1422 /* function prototype parameters must have types */
1423 warning(62);
1424 }
1425 }
1426
1427 /*
1428 * In a function declaration, a list of identifiers (as opposed to a list of
1429 * types) is allowed only if it's also a function definition.
1430 */
1431 void
1432 check_function_definition(sym_t *sym, bool msg)
1433 {
1434
1435 if (sym->s_osdef) {
1436 if (msg) {
1437 /* incomplete or misplaced function definition */
1438 error(22);
1439 }
1440 sym->s_osdef = false;
1441 sym->u.s_old_style_args = NULL;
1442 }
1443 }
1444
1445 /*
1446 * Process the name in a declarator. The symbol gets one of the storage classes
1447 * EXTERN, STATIC, AUTO or TYPEDEF, as well as a definedness in 's_def'.
1448 */
1449 sym_t *
1450 declarator_name(sym_t *sym)
1451 {
1452 scl_t sc = NOSCL;
1453
1454 if (sym->s_scl == NOSCL)
1455 dcs->d_redeclared_symbol = NULL;
1456 else if (sym->s_defarg) {
1457 sym->s_defarg = false;
1458 dcs->d_redeclared_symbol = NULL;
1459 } else {
1460 dcs->d_redeclared_symbol = sym;
1461 sym = pushdown(sym);
1462 }
1463
1464 switch (dcs->d_kind) {
1465 case DK_STRUCT_MEMBER:
1466 case DK_UNION_MEMBER:
1467 /* Set parent */
1468 sym->u.s_member.sm_sou_type = dcs->d_tag_type->t_sou;
1469 sym->s_def = DEF;
1470 sc = dcs->d_kind == DK_STRUCT_MEMBER
1471 ? STRUCT_MEMBER
1472 : UNION_MEMBER;
1473 break;
1474 case DK_EXTERN:
1475 /*
1476 * static and external symbols without "extern" are considered
1477 * to be tentatively defined, external symbols with "extern"
1478 * are declared, and typedef names are defined. Tentatively
1479 * defined and declared symbols may become defined if an
1480 * initializer is present or this is a function definition.
1481 */
1482 if ((sc = dcs->d_scl) == NOSCL) {
1483 sc = EXTERN;
1484 sym->s_def = TDEF;
1485 } else if (sc == STATIC)
1486 sym->s_def = TDEF;
1487 else if (sc == TYPEDEF)
1488 sym->s_def = DEF;
1489 else {
1490 lint_assert(sc == EXTERN);
1491 sym->s_def = DECL;
1492 }
1493 break;
1494 case DK_PROTO_ARG:
1495 sym->s_arg = true;
1496 /* FALLTHROUGH */
1497 case DK_OLD_STYLE_ARG:
1498 if ((sc = dcs->d_scl) == NOSCL)
1499 sc = AUTO;
1500 else {
1501 lint_assert(sc == REG);
1502 sym->s_register = true;
1503 sc = AUTO;
1504 }
1505 sym->s_def = DEF;
1506 break;
1507 case DK_AUTO:
1508 if ((sc = dcs->d_scl) == NOSCL) {
1509 /*
1510 * XXX somewhat ugly because we don't know whether this
1511 * is AUTO or EXTERN (functions). If we are wrong, it
1512 * must be corrected in declare_local, when the
1513 * necessary type information is available.
1514 */
1515 sc = AUTO;
1516 sym->s_def = DEF;
1517 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF)
1518 sym->s_def = DEF;
1519 else if (sc == REG) {
1520 sym->s_register = true;
1521 sc = AUTO;
1522 sym->s_def = DEF;
1523 } else {
1524 lint_assert(sc == EXTERN);
1525 sym->s_def = DECL;
1526 }
1527 break;
1528 default:
1529 lint_assert(dcs->d_kind == DK_ABSTRACT);
1530 /* try to continue after syntax errors */
1531 sc = NOSCL;
1532 }
1533 sym->s_scl = sc;
1534
1535 sym->s_type = dcs->d_type;
1536
1537 dcs->d_func_proto_syms = NULL;
1538
1539 return sym;
1540 }
1541
1542 sym_t *
1543 old_style_function_parameter_name(sym_t *sym)
1544 {
1545
1546 if (sym->s_scl != NOSCL) {
1547 if (block_level == sym->s_block_level) {
1548 /* redeclaration of formal parameter '%s' */
1549 error(21, sym->s_name);
1550 lint_assert(sym->s_defarg);
1551 }
1552 sym = pushdown(sym);
1553 }
1554 sym->s_type = gettyp(INT);
1555 sym->s_scl = AUTO;
1556 sym->s_def = DEF;
1557 sym->s_defarg = sym->s_arg = true;
1558 return sym;
1559 }
1560
1561 /*-
1562 * tag the symbol table entry of the tag
1563 * kind the kind of the tag (STRUCT/UNION/ENUM)
1564 * decl whether the tag type will be completed in this declaration
1565 * (when the following token is T_LBRACE)
1566 * semi whether the following token is T_SEMI
1567 */
1568 type_t *
1569 make_tag_type(sym_t *tag, tspec_t kind, bool decl, bool semi)
1570 {
1571 scl_t scl;
1572 type_t *tp;
1573
1574 if (kind == STRUCT)
1575 scl = STRUCT_TAG;
1576 else if (kind == UNION)
1577 scl = UNION_TAG;
1578 else {
1579 lint_assert(kind == ENUM);
1580 scl = ENUM_TAG;
1581 }
1582
1583 if (tag != NULL) {
1584 if (tag->s_scl != NOSCL)
1585 tag = new_tag(tag, scl, decl, semi);
1586 else {
1587 /* a new tag, no empty declaration */
1588 dcs->d_enclosing->d_nonempty_decl = true;
1589 if (scl == ENUM_TAG && !decl) {
1590 /* TODO: Make this an error in C99 mode as well. */
1591 if (allow_c90 &&
1592 ((!allow_trad && !allow_c99) || pflag))
1593 /* forward reference to enum type */
1594 warning(42);
1595 }
1596 }
1597 if (tag->s_scl == NOSCL) {
1598 tag->s_scl = scl;
1599 tag->s_type = tp = block_zero_alloc(sizeof(*tp));
1600 tp->t_packed = dcs->d_packed;
1601 } else
1602 tp = tag->s_type;
1603
1604 } else {
1605 tag = block_zero_alloc(sizeof(*tag));
1606 tag->s_name = unnamed;
1607 tag->s_def_pos = unique_curr_pos();
1608 tag->s_kind = FTAG;
1609 tag->s_scl = scl;
1610 tag->s_block_level = -1;
1611 tag->s_type = tp = block_zero_alloc(sizeof(*tp));
1612 tp->t_packed = dcs->d_packed;
1613 dcs->d_enclosing->d_nonempty_decl = true;
1614 }
1615
1616 if (tp->t_tspec == NO_TSPEC) {
1617 tp->t_tspec = kind;
1618 if (kind != ENUM) {
1619 tp->t_sou = block_zero_alloc(sizeof(*tp->t_sou));
1620 tp->t_sou->sou_align_in_bits = CHAR_SIZE;
1621 tp->t_sou->sou_tag = tag;
1622 tp->t_sou->sou_incomplete = true;
1623 } else {
1624 tp->t_is_enum = true;
1625 tp->t_enum = block_zero_alloc(sizeof(*tp->t_enum));
1626 tp->t_enum->en_tag = tag;
1627 tp->t_enum->en_incomplete = true;
1628 }
1629 }
1630 return tp;
1631 }
1632
1633 /*-
1634 * Checks all possible cases of tag redeclarations.
1635 *
1636 * decl whether T_LBRACE follows
1637 * semi whether T_SEMI follows
1638 */
1639 static sym_t *
1640 new_tag(sym_t *tag, scl_t scl, bool decl, bool semi)
1641 {
1642
1643 if (tag->s_block_level < block_level) {
1644 if (semi) {
1645 /* "struct a;" */
1646 if (allow_c90) {
1647 /* XXX: Why is this warning suppressed in C90 mode? */
1648 if (allow_trad || allow_c99)
1649 /* declaration of '%s %s' intro... */
1650 warning(44, storage_class_name(scl),
1651 tag->s_name);
1652 tag = pushdown(tag);
1653 } else if (tag->s_scl != scl) {
1654 /* base type is really '%s %s' */
1655 warning(45, storage_class_name(tag->s_scl),
1656 tag->s_name);
1657 }
1658 dcs->d_enclosing->d_nonempty_decl = true;
1659 } else if (decl) {
1660 /* "struct a { ... } " */
1661 if (hflag)
1662 /* redefinition of '%s' hides earlier one */
1663 warning(43, tag->s_name);
1664 tag = pushdown(tag);
1665 dcs->d_enclosing->d_nonempty_decl = true;
1666 } else if (tag->s_scl != scl) {
1667 /* base type is really '%s %s' */
1668 warning(45, storage_class_name(tag->s_scl),
1669 tag->s_name);
1670 /* XXX: Why is this warning suppressed in C90 mode? */
1671 if (allow_trad || allow_c99) {
1672 /* declaration of '%s %s' introduces ... */
1673 warning(44, storage_class_name(scl),
1674 tag->s_name);
1675 }
1676 tag = pushdown(tag);
1677 dcs->d_enclosing->d_nonempty_decl = true;
1678 }
1679 } else {
1680 if (tag->s_scl != scl ||
1681 (decl && !is_incomplete(tag->s_type))) {
1682 /* %s tag '%s' redeclared as %s */
1683 error(46, storage_class_name(tag->s_scl),
1684 tag->s_name, storage_class_name(scl));
1685 print_previous_declaration(tag);
1686 tag = pushdown(tag);
1687 dcs->d_enclosing->d_nonempty_decl = true;
1688 } else if (semi || decl)
1689 dcs->d_enclosing->d_nonempty_decl = true;
1690 }
1691 return tag;
1692 }
1693
1694 const char *
1695 storage_class_name(scl_t sc)
1696 {
1697 switch (sc) {
1698 case EXTERN: return "extern";
1699 case STATIC: return "static";
1700 case AUTO: return "auto";
1701 case REG: return "register";
1702 case TYPEDEF: return "typedef";
1703 case STRUCT_TAG:return "struct";
1704 case UNION_TAG: return "union";
1705 case ENUM_TAG: return "enum";
1706 default: lint_assert(/*CONSTCOND*/false);
1707 }
1708 /* NOTREACHED */
1709 }
1710
1711 static bool
1712 has_named_member(const type_t *tp)
1713 {
1714 for (const sym_t *mem = tp->t_sou->sou_first_member;
1715 mem != NULL; mem = mem->s_next) {
1716 if (mem->s_name != unnamed)
1717 return true;
1718 if (is_struct_or_union(mem->s_type->t_tspec)
1719 && has_named_member(mem->s_type))
1720 return true;
1721 }
1722 return false;
1723 }
1724
1725 type_t *
1726 complete_struct_or_union(sym_t *first_member)
1727 {
1728
1729 type_t *tp = dcs->d_tag_type;
1730 if (tp == NULL) /* in case of syntax errors */
1731 return gettyp(INT);
1732
1733 dcs_align((u_int)dcs->d_sou_align_in_bits, 0);
1734
1735 struct_or_union *sp = tp->t_sou;
1736 sp->sou_align_in_bits = dcs->d_sou_align_in_bits;
1737 sp->sou_incomplete = false;
1738 sp->sou_first_member = first_member;
1739 if (tp->t_packed)
1740 pack_struct_or_union(tp);
1741 else
1742 sp->sou_size_in_bits = dcs->d_offset_in_bits;
1743
1744 if (sp->sou_size_in_bits == 0) {
1745 /* zero sized %s is a C99 feature */
1746 c99ism(47, tspec_name(tp->t_tspec));
1747 } else if (!has_named_member(tp)) {
1748 /* '%s' has no named members */
1749 warning(65, type_name(tp));
1750 }
1751 return tp;
1752 }
1753
1754 type_t *
1755 complete_enum(sym_t *first_enumerator)
1756 {
1757
1758 type_t *tp = dcs->d_tag_type;
1759 tp->t_enum->en_incomplete = false;
1760 tp->t_enum->en_first_enumerator = first_enumerator;
1761 return tp;
1762 }
1763
1764 /*
1765 * Processes the name of an enumerator in an enum declaration.
1766 *
1767 * sym points to the enumerator
1768 * val is the value of the enumerator
1769 * impl is true if the value of the enumerator was not explicitly specified.
1770 */
1771 sym_t *
1772 enumeration_constant(sym_t *sym, int val, bool impl)
1773 {
1774
1775 if (sym->s_scl != NOSCL) {
1776 if (sym->s_block_level == block_level) {
1777 /* no hflag, because this is illegal */
1778 if (sym->s_arg) {
1779 /* enumeration constant '%s' hides parameter */
1780 warning(57, sym->s_name);
1781 } else {
1782 /* redeclaration of '%s' */
1783 error(27, sym->s_name);
1784 /*
1785 * Inside blocks, it should not be too
1786 * complicated to find the position of the
1787 * previous declaration
1788 */
1789 if (block_level == 0)
1790 print_previous_declaration(sym);
1791 }
1792 } else {
1793 if (hflag)
1794 /* redefinition of '%s' hides earlier one */
1795 warning(43, sym->s_name);
1796 }
1797 sym = pushdown(sym);
1798 }
1799
1800 sym->s_scl = ENUM_CONST;
1801 sym->s_type = dcs->d_tag_type;
1802 sym->u.s_enum_constant = val;
1803
1804 if (impl && val == TARG_INT_MIN) {
1805 /* enumeration value '%s' overflows */
1806 warning(48, sym->s_name);
1807 }
1808
1809 enumval = val == TARG_INT_MAX ? TARG_INT_MIN : val + 1;
1810 return sym;
1811 }
1812
1813 static bool
1814 ends_with(const char *s, const char *suffix)
1815 {
1816 size_t s_len = strlen(s);
1817 size_t suffix_len = strlen(suffix);
1818 return s_len >= suffix_len &&
1819 memcmp(s + s_len - suffix_len, suffix, suffix_len) == 0;
1820 }
1821
1822 static void
1823 check_extern_declaration(const sym_t *sym)
1824 {
1825
1826 if (sym->s_scl == EXTERN &&
1827 dcs->d_redeclared_symbol == NULL &&
1828 ends_with(curr_pos.p_file, ".c") &&
1829 !ch_isdigit(sym->s_name[0])) { /* see mktempsym */
1830 /* missing%s header declaration for '%s' */
1831 warning(351, sym->s_type->t_tspec == FUNC ? "" : " 'extern'",
1832 sym->s_name);
1833 }
1834 if (any_query_enabled &&
1835 sym->s_type->t_tspec == FUNC &&
1836 sym->s_scl == EXTERN &&
1837 sym->s_def == DECL &&
1838 !in_system_header) {
1839 /* redundant 'extern' in function declaration of '%s' */
1840 query_message(13, sym->s_name);
1841 }
1842 }
1843
1844 /* Process a single external or 'static' declarator. */
1845 static void
1846 declare_extern(sym_t *dsym, bool has_initializer, sbuf_t *renaming)
1847 {
1848
1849 if (renaming != NULL) {
1850 lint_assert(dsym->s_rename == NULL);
1851
1852 char *s = level_zero_alloc(1, renaming->sb_len + 1);
1853 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1854 dsym->s_rename = s;
1855 }
1856
1857 check_extern_declaration(dsym);
1858
1859 check_function_definition(dsym, true);
1860
1861 check_type(dsym);
1862
1863 if (has_initializer && !check_init(dsym))
1864 dsym->s_def = DEF;
1865
1866 /*
1867 * Declarations of functions are marked as "tentative" in
1868 * declarator_name(). This is wrong because there are no
1869 * tentative function definitions.
1870 */
1871 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1872 dsym->s_def = DECL;
1873
1874 if (dcs->d_inline) {
1875 if (dsym->s_type->t_tspec == FUNC) {
1876 dsym->s_inline = true;
1877 } else {
1878 /* variable '%s' declared inline */
1879 warning(268, dsym->s_name);
1880 }
1881 }
1882
1883 /* Write the declaration into the output file */
1884 if (plibflg && llibflg &&
1885 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1886 /*
1887 * With both LINTLIBRARY and PROTOLIB the prototype is
1888 * written as a function definition to the output file.
1889 */
1890 bool rval = dsym->s_type->t_subt->t_tspec != VOID;
1891 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1892 } else if (!is_compiler_builtin(dsym->s_name)
1893 && !(has_initializer && dsym->s_type->t_incomplete_array)) {
1894 outsym(dsym, dsym->s_scl, dsym->s_def);
1895 }
1896
1897 sym_t *rdsym = dcs->d_redeclared_symbol;
1898 if (rdsym != NULL) {
1899
1900 /*
1901 * If the old symbol stems from an old-style function
1902 * definition, we have remembered the params in
1903 * rdsym->s_old_style_args and compare them with the params
1904 * of the prototype.
1905 */
1906 bool redec = rdsym->s_osdef && dsym->s_type->t_proto &&
1907 check_old_style_definition(rdsym, dsym);
1908
1909 bool dowarn = false;
1910 if (!redec && !check_redeclaration(dsym, &dowarn)) {
1911 if (dowarn) {
1912 /* TODO: Make this an error in C99 mode as well. */
1913 if (!allow_trad && !allow_c99)
1914 /* redeclaration of '%s' */
1915 error(27, dsym->s_name);
1916 else
1917 /* redeclaration of '%s' */
1918 warning(27, dsym->s_name);
1919 print_previous_declaration(rdsym);
1920 }
1921
1922 /*
1923 * Take over the remembered params if the new symbol
1924 * is not a prototype.
1925 */
1926 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1927 dsym->s_osdef = rdsym->s_osdef;
1928 dsym->u.s_old_style_args =
1929 rdsym->u.s_old_style_args;
1930 dsym->s_def_pos = rdsym->s_def_pos;
1931 }
1932
1933 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto)
1934 dsym->s_def_pos = rdsym->s_def_pos;
1935 else if (rdsym->s_def == DEF && dsym->s_def != DEF)
1936 dsym->s_def_pos = rdsym->s_def_pos;
1937
1938 copy_usage_info(dsym, rdsym);
1939
1940 /* Once a name is defined, it remains defined. */
1941 if (rdsym->s_def == DEF)
1942 dsym->s_def = DEF;
1943
1944 /* once a function is inline, it remains inline */
1945 if (rdsym->s_inline)
1946 dsym->s_inline = true;
1947
1948 complete_type(dsym, rdsym);
1949 }
1950
1951 rmsym(rdsym);
1952 }
1953
1954 if (dsym->s_scl == TYPEDEF) {
1955 dsym->s_type = block_dup_type(dsym->s_type);
1956 dsym->s_type->t_typedef = true;
1957 set_first_typedef(dsym->s_type, dsym);
1958 }
1959 }
1960
1961 void
1962 declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
1963 {
1964
1965 if (dcs->d_kind == DK_EXTERN)
1966 declare_extern(decl, has_initializer, renaming);
1967 else if (dcs->d_kind == DK_OLD_STYLE_ARG ||
1968 dcs->d_kind == DK_PROTO_ARG) {
1969 if (renaming != NULL) {
1970 /* symbol renaming can't be used on function arguments */
1971 error(310);
1972 } else
1973 (void)declare_argument(decl, has_initializer);
1974 } else {
1975 lint_assert(dcs->d_kind == DK_AUTO);
1976 if (renaming != NULL) {
1977 /* symbol renaming can't be used on automatic variables */
1978 error(311);
1979 } else
1980 declare_local(decl, has_initializer);
1981 }
1982 }
1983
1984 /*
1985 * Copies information about usage into a new symbol table entry of
1986 * the same symbol.
1987 */
1988 void
1989 copy_usage_info(sym_t *sym, sym_t *rdsym)
1990 {
1991
1992 sym->s_set_pos = rdsym->s_set_pos;
1993 sym->s_use_pos = rdsym->s_use_pos;
1994 sym->s_set = rdsym->s_set;
1995 sym->s_used = rdsym->s_used;
1996 }
1997
1998 /*
1999 * Prints an error and returns true if a symbol is redeclared/redefined.
2000 * Otherwise, returns false and, in some cases of minor problems, prints
2001 * a warning.
2002 */
2003 bool
2004 check_redeclaration(sym_t *dsym, bool *dowarn)
2005 {
2006
2007 sym_t *rdsym = dcs->d_redeclared_symbol;
2008 if (rdsym->s_scl == ENUM_CONST) {
2009 /* redeclaration of '%s' */
2010 error(27, dsym->s_name);
2011 print_previous_declaration(rdsym);
2012 return true;
2013 }
2014 if (rdsym->s_scl == TYPEDEF) {
2015 /* typedef '%s' redeclared */
2016 error(89, dsym->s_name);
2017 print_previous_declaration(rdsym);
2018 return true;
2019 }
2020 if (dsym->s_scl == TYPEDEF) {
2021 /* redeclaration of '%s' */
2022 error(27, dsym->s_name);
2023 print_previous_declaration(rdsym);
2024 return true;
2025 }
2026 if (rdsym->s_def == DEF && dsym->s_def == DEF) {
2027 /* redefinition of '%s' */
2028 error(28, dsym->s_name);
2029 print_previous_declaration(rdsym);
2030 return true;
2031 }
2032 if (!types_compatible(rdsym->s_type, dsym->s_type,
2033 false, false, dowarn)) {
2034 /* redeclaration of '%s' with type '%s', expected '%s' */
2035 error(347, dsym->s_name,
2036 type_name(dsym->s_type), type_name(rdsym->s_type));
2037 print_previous_declaration(rdsym);
2038 return true;
2039 }
2040 if (rdsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2041 return false;
2042 if (rdsym->s_scl == STATIC && dsym->s_scl == STATIC)
2043 return false;
2044 if (rdsym->s_scl == STATIC && dsym->s_def == DECL)
2045 return false;
2046 if (rdsym->s_scl == EXTERN && rdsym->s_def == DEF) {
2047 /*
2048 * All cases except "int a = 1; static int a;" are caught
2049 * above with or without a warning
2050 */
2051 /* redeclaration of '%s' */
2052 error(27, dsym->s_name);
2053 print_previous_declaration(rdsym);
2054 return true;
2055 }
2056 if (rdsym->s_scl == EXTERN) {
2057 /* '%s' was previously declared extern, becomes static */
2058 warning(29, dsym->s_name);
2059 print_previous_declaration(rdsym);
2060 return false;
2061 }
2062 /*
2063 * Now it's one of:
2064 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2065 */
2066 /* TODO: Make this an error in C99 mode as well. */
2067 if (!allow_trad && !allow_c99) {
2068 /* redeclaration of '%s'; ANSI C requires static */
2069 warning(30, dsym->s_name);
2070 print_previous_declaration(rdsym);
2071 }
2072 dsym->s_scl = STATIC;
2073 return false;
2074 }
2075
2076 static bool
2077 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
2078 {
2079
2080 if (tp1->t_const != tp2->t_const && !ignqual && allow_c90)
2081 return false;
2082 if (tp1->t_volatile != tp2->t_volatile && !ignqual && allow_c90)
2083 return false;
2084 return true;
2085 }
2086
2087 bool
2088 pointer_types_are_compatible(const type_t *tp1, const type_t *tp2, bool ignqual)
2089 {
2090
2091 return tp1->t_tspec == VOID || tp2->t_tspec == VOID ||
2092 qualifiers_correspond(tp1, tp2, ignqual);
2093 }
2094
2095 /*-
2096 * ignqual ignore type qualifiers; used for function parameters
2097 * promot promote the left type; used for comparison of parameters of
2098 * old-style function definitions with parameters of prototypes.
2099 * *dowarn is set to true if an old-style function declaration is not
2100 * compatible with a prototype
2101 */
2102 bool
2103 types_compatible(const type_t *tp1, const type_t *tp2,
2104 bool ignqual, bool promot, bool *dowarn)
2105 {
2106
2107 while (tp1 != NULL && tp2 != NULL) {
2108 tspec_t t = tp1->t_tspec;
2109 if (promot) {
2110 if (t == FLOAT)
2111 t = DOUBLE;
2112 else if (t == CHAR || t == SCHAR)
2113 t = INT;
2114 else if (t == UCHAR)
2115 t = allow_c90 ? INT : UINT;
2116 else if (t == SHORT)
2117 t = INT;
2118 else if (t == USHORT) {
2119 /* CONSTCOND */
2120 t = TARG_INT_MAX < TARG_USHRT_MAX || !allow_c90
2121 ? UINT : INT;
2122 }
2123 }
2124
2125 if (t != tp2->t_tspec)
2126 return false;
2127
2128 if (!qualifiers_correspond(tp1, tp2, ignqual))
2129 return false;
2130
2131 if (is_struct_or_union(t))
2132 return tp1->t_sou == tp2->t_sou;
2133
2134 if (t == ENUM && eflag)
2135 return tp1->t_enum == tp2->t_enum;
2136
2137 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2138 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2139 return false;
2140 }
2141
2142 /* don't check prototypes for traditional */
2143 if (t == FUNC && allow_c90) {
2144 if (tp1->t_proto && tp2->t_proto) {
2145 if (!prototypes_compatible(tp1, tp2, dowarn))
2146 return false;
2147 } else if (tp1->t_proto) {
2148 if (!matches_no_arg_function(tp1, dowarn))
2149 return false;
2150 } else if (tp2->t_proto) {
2151 if (!matches_no_arg_function(tp2, dowarn))
2152 return false;
2153 }
2154 }
2155
2156 tp1 = tp1->t_subt;
2157 tp2 = tp2->t_subt;
2158 ignqual = promot = false;
2159 }
2160
2161 return tp1 == tp2;
2162 }
2163
2164 static bool
2165 prototypes_compatible(const type_t *tp1, const type_t *tp2, bool *dowarn)
2166 {
2167
2168 if (tp1->t_vararg != tp2->t_vararg)
2169 return false;
2170
2171 sym_t *a1 = tp1->t_args;
2172 sym_t *a2 = tp2->t_args;
2173
2174 for (; a1 != NULL && a2 != NULL; a1 = a1->s_next, a2 = a2->s_next) {
2175 if (!types_compatible(a1->s_type, a2->s_type,
2176 true, false, dowarn))
2177 return false;
2178 }
2179 return a1 == a2;
2180 }
2181
2182 /*
2183 * Returns whether all parameters of a prototype are compatible with an
2184 * old-style function declaration.
2185 *
2186 * This is the case if the following conditions are met:
2187 * 1. the prototype has a fixed number of parameters
2188 * 2. no parameter is of type float
2189 * 3. no parameter is converted to another type if integer promotion
2190 * is applied on it
2191 */
2192 static bool
2193 matches_no_arg_function(const type_t *tp, bool *dowarn)
2194 {
2195
2196 if (tp->t_vararg && dowarn != NULL)
2197 *dowarn = true;
2198 for (sym_t *arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2199 tspec_t t = arg->s_type->t_tspec;
2200 if (t == FLOAT ||
2201 t == CHAR || t == SCHAR || t == UCHAR ||
2202 t == SHORT || t == USHORT) {
2203 if (dowarn != NULL)
2204 *dowarn = true;
2205 }
2206 }
2207 /* FIXME: Always returning true cannot be correct. */
2208 return true;
2209 }
2210
2211 /*
2212 * Compares a prototype declaration with the remembered arguments of a previous
2213 * old-style function definition.
2214 */
2215 static bool
2216 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2217 {
2218
2219 sym_t *args = rdsym->u.s_old_style_args;
2220 sym_t *pargs = dsym->s_type->t_args;
2221
2222 bool msg = false;
2223
2224 int narg = 0;
2225 for (sym_t *arg = args; arg != NULL; arg = arg->s_next)
2226 narg++;
2227 int nparg = 0;
2228 for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next)
2229 nparg++;
2230 if (narg != nparg) {
2231 /* prototype does not match old-style definition */
2232 error(63);
2233 msg = true;
2234 goto end;
2235 }
2236
2237 sym_t *arg = args;
2238 sym_t *parg = pargs;
2239 int n = 1;
2240 while (narg-- > 0) {
2241 bool dowarn = false;
2242 /*
2243 * If it does not match due to promotion and lint runs in
2244 * "traditional to C90" migration mode, print only a warning.
2245 *
2246 * XXX: Where is this "only a warning"?
2247 */
2248 if (!types_compatible(arg->s_type, parg->s_type,
2249 true, true, &dowarn) ||
2250 dowarn) {
2251 /* prototype does not match old-style ... */
2252 error(299, n);
2253 msg = true;
2254 }
2255 arg = arg->s_next;
2256 parg = parg->s_next;
2257 n++;
2258 }
2259
2260 end:
2261 if (msg && rflag) {
2262 /* old-style definition */
2263 message_at(300, &rdsym->s_def_pos);
2264 }
2265
2266 return msg;
2267 }
2268
2269 /*
2270 * Completes a type by copying the dimension and prototype information from a
2271 * second compatible type.
2272 *
2273 * Following lines are legal:
2274 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2275 * "typedef ft(); ft f; f(int); ft g; g(long);"
2276 * This means that, if a type is completed, the type structure must be
2277 * duplicated.
2278 */
2279 void
2280 complete_type(sym_t *dsym, sym_t *ssym)
2281 {
2282 type_t **dstp = &dsym->s_type;
2283 type_t *src = ssym->s_type;
2284
2285 while (*dstp != NULL) {
2286 type_t *dst = *dstp;
2287 lint_assert(src != NULL);
2288 lint_assert(dst->t_tspec == src->t_tspec);
2289 if (dst->t_tspec == ARRAY) {
2290 if (dst->t_dim == 0 && src->t_dim != 0) {
2291 *dstp = dst = block_dup_type(dst);
2292 dst->t_dim = src->t_dim;
2293 dst->t_incomplete_array = false;
2294 }
2295 } else if (dst->t_tspec == FUNC) {
2296 if (!dst->t_proto && src->t_proto) {
2297 *dstp = dst = block_dup_type(dst);
2298 dst->t_proto = true;
2299 dst->t_args = src->t_args;
2300 }
2301 }
2302 dstp = &dst->t_subt;
2303 src = src->t_subt;
2304 }
2305 }
2306
2307 /*
2308 * Completes the declaration of a single argument.
2309 */
2310 sym_t *
2311 declare_argument(sym_t *sym, bool has_initializer)
2312 {
2313
2314 check_function_definition(sym, true);
2315
2316 check_type(sym);
2317
2318 if (dcs->d_redeclared_symbol != NULL &&
2319 dcs->d_redeclared_symbol->s_block_level == block_level) {
2320 /* redeclaration of formal parameter '%s' */
2321 error(237, sym->s_name);
2322 rmsym(dcs->d_redeclared_symbol);
2323 sym->s_arg = true;
2324 }
2325
2326 if (!sym->s_arg) {
2327 /* declared argument '%s' is missing */
2328 error(53, sym->s_name);
2329 sym->s_arg = true;
2330 }
2331
2332 if (has_initializer) {
2333 /* cannot initialize parameter '%s' */
2334 error(52, sym->s_name);
2335 }
2336
2337 if (sym->s_type == NULL) /* for c(void()) */
2338 sym->s_type = gettyp(VOID);
2339
2340 tspec_t t = sym->s_type->t_tspec;
2341 if (t == ARRAY)
2342 sym->s_type = block_derive_type(sym->s_type->t_subt, PTR);
2343 if (t == FUNC) {
2344 if (!allow_c90)
2345 /* argument '%s' has function type, should be ... */
2346 warning(50, sym->s_name);
2347 sym->s_type = block_derive_type(sym->s_type, PTR);
2348 }
2349 if (t == FLOAT && !allow_c90)
2350 sym->s_type = gettyp(DOUBLE);
2351
2352 if (dcs->d_inline)
2353 /* argument '%s' declared inline */
2354 warning(269, sym->s_name);
2355
2356 /*
2357 * Arguments must have complete types. length_in_bits prints the
2358 * needed error messages (null dimension is impossible because arrays
2359 * are converted to pointers).
2360 */
2361 if (sym->s_type->t_tspec != VOID)
2362 (void)length_in_bits(sym->s_type, sym->s_name);
2363
2364 sym->s_used = dcs->d_used;
2365 mark_as_set(sym);
2366
2367 return sym;
2368 }
2369
2370 static bool
2371 is_character_pointer(const type_t *tp)
2372 {
2373 tspec_t st;
2374
2375 return tp->t_tspec == PTR &&
2376 (st = tp->t_subt->t_tspec,
2377 st == CHAR || st == SCHAR || st == UCHAR);
2378 }
2379
2380 void
2381 check_func_lint_directives(void)
2382 {
2383
2384 /* check for illegal combinations of lint directives */
2385 if (printflike_argnum != -1 && scanflike_argnum != -1) {
2386 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2387 warning(289);
2388 printflike_argnum = scanflike_argnum = -1;
2389 }
2390 if (nvararg != -1 &&
2391 (printflike_argnum != -1 || scanflike_argnum != -1)) {
2392 /* dubious use of ** VARARGS ** with ** %s ** */
2393 warning(288,
2394 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2395 nvararg = -1;
2396 }
2397
2398 /*
2399 * check if the argument of a lint directive is compatible with the
2400 * number of arguments.
2401 */
2402 int narg = 0;
2403 for (sym_t *arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
2404 narg++;
2405 if (nargusg > narg) {
2406 /* argument number mismatch with directive ** %s ** */
2407 warning(283, "ARGSUSED");
2408 nargusg = 0;
2409 }
2410 if (nvararg > narg) {
2411 /* argument number mismatch with directive ** %s ** */
2412 warning(283, "VARARGS");
2413 nvararg = 0;
2414 }
2415 if (printflike_argnum > narg) {
2416 /* argument number mismatch with directive ** %s ** */
2417 warning(283, "PRINTFLIKE");
2418 printflike_argnum = -1;
2419 } else if (printflike_argnum == 0) {
2420 printflike_argnum = -1;
2421 }
2422 if (scanflike_argnum > narg) {
2423 /* argument number mismatch with directive ** %s ** */
2424 warning(283, "SCANFLIKE");
2425 scanflike_argnum = -1;
2426 } else if (scanflike_argnum == 0) {
2427 scanflike_argnum = -1;
2428 }
2429 if (printflike_argnum != -1 || scanflike_argnum != -1) {
2430 narg = printflike_argnum != -1
2431 ? printflike_argnum : scanflike_argnum;
2432 sym_t *arg = dcs->d_func_args;
2433 for (int n = 1; n < narg; n++)
2434 arg = arg->s_next;
2435 if (!is_character_pointer(arg->s_type)) {
2436 /* argument %d must be 'char *' for PRINTFLIKE/... */
2437 warning(293, narg);
2438 printflike_argnum = scanflike_argnum = -1;
2439 }
2440 }
2441 }
2442
2443 /*
2444 * Warn about arguments in old-style function definitions that default to int.
2445 * Check that an old-style function definition is compatible to a previous
2446 * prototype.
2447 */
2448 void
2449 check_func_old_style_arguments(void)
2450 {
2451 int narg;
2452 int nparg;
2453 bool msg;
2454
2455 sym_t *args = funcsym->u.s_old_style_args;
2456 sym_t *pargs = funcsym->s_type->t_args;
2457
2458 /*
2459 * print a warning for each argument of an old-style function
2460 * definition which defaults to int
2461 */
2462 for (sym_t *arg = args; arg != NULL; arg = arg->s_next) {
2463 if (arg->s_defarg) {
2464 /* type of argument '%s' defaults to 'int' */
2465 warning(32, arg->s_name);
2466 arg->s_defarg = false;
2467 mark_as_set(arg);
2468 }
2469 }
2470
2471 /*
2472 * If this is an old-style function definition and a prototype
2473 * exists, compare the types of arguments.
2474 */
2475 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2476 /*
2477 * If the number of arguments does not match, we need not
2478 * continue.
2479 */
2480 narg = nparg = 0;
2481 msg = false;
2482 for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next)
2483 nparg++;
2484 for (sym_t *arg = args; arg != NULL; arg = arg->s_next)
2485 narg++;
2486 if (narg != nparg) {
2487 /* parameter mismatch: %d declared, %d defined */
2488 error(51, nparg, narg);
2489 msg = true;
2490 } else {
2491 sym_t *parg = pargs;
2492 sym_t *arg = args;
2493 while (narg-- > 0) {
2494 msg |= check_prototype_declaration(arg, parg);
2495 parg = parg->s_next;
2496 arg = arg->s_next;
2497 }
2498 }
2499 if (msg && rflag) {
2500 /* prototype declaration */
2501 message_at(285, &dcs->d_redeclared_symbol->s_def_pos);
2502 }
2503
2504 /* from now on the prototype is valid */
2505 funcsym->s_osdef = false;
2506 funcsym->u.s_old_style_args = NULL;
2507 }
2508 }
2509
2510 /*
2511 * Checks compatibility of an old-style function definition with a previous
2512 * prototype declaration.
2513 * Returns true if the position of the previous declaration should be reported.
2514 */
2515 static bool
2516 check_prototype_declaration(sym_t *arg, sym_t *parg)
2517 {
2518 type_t *tp = arg->s_type;
2519 type_t *ptp = parg->s_type;
2520 bool dowarn = false;
2521
2522 if (!types_compatible(tp, ptp, true, true, &dowarn)) {
2523 if (types_compatible(tp, ptp, true, false, &dowarn)) {
2524 /* type of '%s' does not match prototype */
2525 return gnuism(58, arg->s_name);
2526 } else {
2527 /* type of '%s' does not match prototype */
2528 error(58, arg->s_name);
2529 return true;
2530 }
2531 }
2532 if (dowarn) {
2533 /* TODO: Make this an error in C99 mode as well. */
2534 if (!allow_trad && !allow_c99)
2535 /* type of '%s' does not match prototype */
2536 error(58, arg->s_name);
2537 else
2538 /* type of '%s' does not match prototype */
2539 warning(58, arg->s_name);
2540 return true;
2541 }
2542
2543 return false;
2544 }
2545
2546 static void
2547 check_local_hiding(const sym_t *dsym)
2548 {
2549 switch (dsym->s_scl) {
2550 case AUTO:
2551 /* automatic '%s' hides external declaration */
2552 warning(86, dsym->s_name);
2553 break;
2554 case STATIC:
2555 /* static '%s' hides external declaration */
2556 warning(87, dsym->s_name);
2557 break;
2558 case TYPEDEF:
2559 /* typedef '%s' hides external declaration */
2560 warning(88, dsym->s_name);
2561 break;
2562 case EXTERN:
2563 /* Already checked in declare_external_in_block. */
2564 break;
2565 default:
2566 lint_assert(/*CONSTCOND*/false);
2567 }
2568 }
2569
2570 static void
2571 check_local_redeclaration(const sym_t *dsym, sym_t *rdsym)
2572 {
2573 if (rdsym->s_block_level == 0) {
2574 if (hflag)
2575 check_local_hiding(dsym);
2576
2577 } else if (rdsym->s_block_level == block_level) {
2578
2579 /* no hflag, because it's illegal! */
2580 if (rdsym->s_arg) {
2581 /*
2582 * if allow_c90, a "redeclaration of '%s'" error
2583 * is produced below
2584 */
2585 if (!allow_c90) {
2586 if (hflag) {
2587 /* declaration of '%s' hides ... */
2588 warning(91, dsym->s_name);
2589 }
2590 rmsym(rdsym);
2591 }
2592 }
2593
2594 } else if (rdsym->s_block_level < block_level) {
2595 if (hflag) {
2596 /* declaration of '%s' hides earlier one */
2597 warning(95, dsym->s_name);
2598 }
2599 }
2600
2601 if (rdsym->s_block_level == block_level) {
2602 /* redeclaration of '%s' */
2603 error(27, dsym->s_name);
2604 rmsym(rdsym);
2605 }
2606 }
2607
2608 /*
2609 * Completes a single local declaration/definition.
2610 */
2611 void
2612 declare_local(sym_t *dsym, bool has_initializer)
2613 {
2614
2615 /* Correct a mistake done in declarator_name(). */
2616 if (dsym->s_type->t_tspec == FUNC) {
2617 dsym->s_def = DECL;
2618 if (dcs->d_scl == NOSCL)
2619 dsym->s_scl = EXTERN;
2620 }
2621
2622 if (dsym->s_scl == EXTERN) {
2623 /* nested 'extern' declaration of '%s' */
2624 warning(352, dsym->s_name);
2625 }
2626
2627 if (dsym->s_type->t_tspec == FUNC) {
2628 if (dsym->s_scl == STATIC) {
2629 /* dubious static function '%s' at block level */
2630 warning(93, dsym->s_name);
2631 dsym->s_scl = EXTERN;
2632 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2633 /* function '%s' has illegal storage class */
2634 error(94, dsym->s_name);
2635 dsym->s_scl = EXTERN;
2636 }
2637 }
2638
2639 /*
2640 * functions may be declared inline at local scope, although
2641 * this has no effect for a later definition of the same
2642 * function.
2643 * XXX it should have an effect if !allow_c90 is set. this would
2644 * also be the way gcc behaves.
2645 */
2646 if (dcs->d_inline) {
2647 if (dsym->s_type->t_tspec == FUNC)
2648 dsym->s_inline = true;
2649 else {
2650 /* variable '%s' declared inline */
2651 warning(268, dsym->s_name);
2652 }
2653 }
2654
2655 check_function_definition(dsym, true);
2656
2657 check_type(dsym);
2658
2659 if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
2660 declare_external_in_block(dsym);
2661
2662 if (dsym->s_scl == EXTERN) {
2663 /*
2664 * XXX if the static variable at level 0 is only defined
2665 * later, checking will be possible.
2666 */
2667 if (dsym->s_ext_sym == NULL)
2668 outsym(dsym, EXTERN, dsym->s_def);
2669 else
2670 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2671 }
2672
2673 if (dcs->d_redeclared_symbol != NULL)
2674 check_local_redeclaration(dsym, dcs->d_redeclared_symbol);
2675
2676 if (has_initializer && !check_init(dsym)) {
2677 dsym->s_def = DEF;
2678 mark_as_set(dsym);
2679 }
2680
2681 if (dsym->s_scl == TYPEDEF) {
2682 dsym->s_type = block_dup_type(dsym->s_type);
2683 dsym->s_type->t_typedef = true;
2684 set_first_typedef(dsym->s_type, dsym);
2685 }
2686
2687 if (dsym->s_scl == STATIC && any_query_enabled) {
2688 /* static variable '%s' in function */
2689 query_message(11, dsym->s_name);
2690 }
2691 }
2692
2693 /* Processes (re)declarations of external symbols inside blocks. */
2694 static void
2695 declare_external_in_block(sym_t *dsym)
2696 {
2697
2698 /* look for a symbol with the same name */
2699 sym_t *esym = dcs->d_redeclared_symbol;
2700 while (esym != NULL && esym->s_block_level != 0) {
2701 while ((esym = esym->s_symtab_next) != NULL) {
2702 if (esym->s_kind != FVFT)
2703 continue;
2704 if (strcmp(dsym->s_name, esym->s_name) == 0)
2705 break;
2706 }
2707 }
2708 if (esym == NULL)
2709 return;
2710 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2711 /* gcc accepts this without a warning, pcc prints an error. */
2712 /* redeclaration of '%s' */
2713 warning(27, dsym->s_name);
2714 print_previous_declaration(esym);
2715 return;
2716 }
2717
2718 bool dowarn = false;
2719 bool compatible = types_compatible(esym->s_type, dsym->s_type,
2720 false, false, &dowarn);
2721
2722 if (!compatible || dowarn) {
2723 if (esym->s_scl == EXTERN) {
2724 /* inconsistent redeclaration of extern '%s' */
2725 warning(90, dsym->s_name);
2726 print_previous_declaration(esym);
2727 } else {
2728 /* inconsistent redeclaration of static '%s' */
2729 warning(92, dsym->s_name);
2730 print_previous_declaration(esym);
2731 }
2732 }
2733
2734 if (compatible) {
2735 /*
2736 * Remember the external symbol, so we can update usage
2737 * information at the end of the block.
2738 */
2739 dsym->s_ext_sym = esym;
2740 }
2741 }
2742
2743 /*
2744 * Check whether the symbol cannot be initialized due to type/storage class.
2745 * Return whether an error has been detected.
2746 */
2747 static bool
2748 check_init(sym_t *sym)
2749 {
2750
2751 if (sym->s_type->t_tspec == FUNC) {
2752 /* cannot initialize function '%s' */
2753 error(24, sym->s_name);
2754 return true;
2755 }
2756 if (sym->s_scl == TYPEDEF) {
2757 /* cannot initialize typedef '%s' */
2758 error(25, sym->s_name);
2759 return true;
2760 }
2761 if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2762 if (dcs->d_kind == DK_EXTERN) {
2763 /* cannot initialize extern declaration '%s' */
2764 warning(26, sym->s_name);
2765 } else {
2766 /* cannot initialize extern declaration '%s' */
2767 error(26, sym->s_name);
2768 return true;
2769 }
2770 }
2771
2772 return false;
2773 }
2774
2775 /* Create a symbol for an abstract declaration. */
2776 sym_t *
2777 abstract_name(void)
2778 {
2779
2780 lint_assert(dcs->d_kind == DK_ABSTRACT || dcs->d_kind == DK_PROTO_ARG);
2781
2782 sym_t *sym = block_zero_alloc(sizeof(*sym));
2783 sym->s_name = unnamed;
2784 sym->s_def = DEF;
2785 sym->s_scl = ABSTRACT;
2786 sym->s_block_level = -1;
2787 sym->s_arg = dcs->d_kind == DK_PROTO_ARG;
2788
2789 /*
2790 * At this point, dcs->d_type contains only the basic type. That
2791 * type will be updated later, adding pointers, arrays and functions
2792 * as necessary.
2793 */
2794 /*
2795 * XXX: This is not the correct type. For example in msg_347, it is
2796 * the type of the last prototype parameter, but it should rather be
2797 * the return type of the function.
2798 */
2799 sym->s_type = dcs->d_type;
2800 dcs->d_redeclared_symbol = NULL;
2801 dcs->d_vararg = false;
2802
2803 return sym;
2804 }
2805
2806 /* Removes anything which has nothing to do on global level. */
2807 void
2808 global_clean_up(void)
2809 {
2810
2811 while (dcs->d_enclosing != NULL)
2812 end_declaration_level();
2813
2814 clean_up_after_error();
2815 block_level = 0;
2816 mem_block_level = 0;
2817 global_clean_up_decl(true);
2818 }
2819
2820 sym_t *
2821 declare_abstract_type(sym_t *sym)
2822 {
2823
2824 check_function_definition(sym, true);
2825 check_type(sym);
2826 return sym;
2827 }
2828
2829 /* Checks size after declarations of variables and their initialization. */
2830 void
2831 check_size(sym_t *dsym)
2832 {
2833
2834 if (dsym->s_def == DEF &&
2835 dsym->s_scl != TYPEDEF &&
2836 dsym->s_type->t_tspec != FUNC &&
2837 length_in_bits(dsym->s_type, dsym->s_name) == 0 &&
2838 dsym->s_type->t_tspec == ARRAY &&
2839 dsym->s_type->t_dim == 0) {
2840 if (!allow_c90)
2841 /* empty array declaration for '%s' */
2842 warning(190, dsym->s_name);
2843 else
2844 /* empty array declaration for '%s' */
2845 error(190, dsym->s_name);
2846 }
2847 }
2848
2849 /* Mark an object as set if it is not already. */
2850 void
2851 mark_as_set(sym_t *sym)
2852 {
2853
2854 if (!sym->s_set) {
2855 sym->s_set = true;
2856 sym->s_set_pos = unique_curr_pos();
2857 }
2858 }
2859
2860 /* Mark an object as used if it is not already. */
2861 void
2862 mark_as_used(sym_t *sym, bool fcall, bool szof)
2863 {
2864
2865 if (!sym->s_used) {
2866 sym->s_used = true;
2867 sym->s_use_pos = unique_curr_pos();
2868 }
2869 /*
2870 * For function calls, another record is written.
2871 *
2872 * XXX: Should symbols used in sizeof() be treated as used or not?
2873 * Probably not, because there is no point in declaring an external
2874 * variable only to get its size.
2875 */
2876 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2877 outusg(sym);
2878 }
2879
2880 /* Warns about variables and labels that are not used or only set. */
2881 void
2882 check_usage(dinfo_t *di)
2883 {
2884 /* for this warning LINTED has no effect */
2885 int saved_lwarn = lwarn;
2886 lwarn = LWARN_ALL;
2887
2888 debug_step("begin lwarn %d", lwarn);
2889 for (sym_t *sym = di->d_first_dlsym;
2890 sym != NULL; sym = sym->s_level_next)
2891 check_usage_sym(di->d_asm, sym);
2892 lwarn = saved_lwarn;
2893 debug_step("end lwarn %d", lwarn);
2894 }
2895
2896 /* Warns about a variable or a label that is not used or only set. */
2897 void
2898 check_usage_sym(bool novar, sym_t *sym)
2899 {
2900
2901 if (sym->s_block_level == -1)
2902 return;
2903
2904 if (sym->s_kind == FVFT && sym->s_arg)
2905 check_argument_usage(novar, sym);
2906 else if (sym->s_kind == FVFT)
2907 check_variable_usage(novar, sym);
2908 else if (sym->s_kind == FLABEL)
2909 check_label_usage(sym);
2910 else if (sym->s_kind == FTAG)
2911 check_tag_usage(sym);
2912 }
2913
2914 static void
2915 check_argument_usage(bool novar, sym_t *arg)
2916 {
2917
2918 lint_assert(arg->s_set);
2919
2920 if (novar)
2921 return;
2922
2923 if (!arg->s_used && vflag) {
2924 /* argument '%s' unused in function '%s' */
2925 warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name);
2926 }
2927 }
2928
2929 static void
2930 check_variable_usage(bool novar, sym_t *sym)
2931 {
2932
2933 lint_assert(block_level != 0);
2934
2935 /* example at file scope: int c = ({ return 3; }); */
2936 if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0]))
2937 return;
2938
2939 /* errors in expressions easily cause lots of these warnings */
2940 if (nerr != 0)
2941 return;
2942
2943 /*
2944 * XXX Only variables are checked, although types should
2945 * probably also be checked
2946 */
2947 scl_t sc = sym->s_scl;
2948 if (sc != EXTERN && sc != STATIC && sc != AUTO && sc != REG)
2949 return;
2950
2951 if (novar)
2952 return;
2953
2954 if (sc == EXTERN) {
2955 if (!sym->s_used && !sym->s_set) {
2956 /* '%s' unused in function '%s' */
2957 warning_at(192, &sym->s_def_pos,
2958 sym->s_name, funcsym->s_name);
2959 }
2960 } else {
2961 if (sym->s_set && !sym->s_used) {
2962 /* '%s' set but not used in function '%s' */
2963 warning_at(191, &sym->s_set_pos,
2964 sym->s_name, funcsym->s_name);
2965 } else if (!sym->s_used) {
2966 /* '%s' unused in function '%s' */
2967 warning_at(192, &sym->s_def_pos,
2968 sym->s_name, funcsym->s_name);
2969 }
2970 }
2971
2972 if (sc == EXTERN) {
2973 /*
2974 * information about usage is taken over into the symbol
2975 * table entry at level 0 if the symbol was locally declared
2976 * as an external symbol.
2977 *
2978 * XXX This is wrong for symbols declared static at level 0
2979 * if the usage information stems from sizeof(). This is
2980 * because symbols at level 0 only used in sizeof() are
2981 * considered to not be used.
2982 */
2983 sym_t *xsym = sym->s_ext_sym;
2984 if (xsym != NULL) {
2985 if (sym->s_used && !xsym->s_used) {
2986 xsym->s_used = true;
2987 xsym->s_use_pos = sym->s_use_pos;
2988 }
2989 if (sym->s_set && !xsym->s_set) {
2990 xsym->s_set = true;
2991 xsym->s_set_pos = sym->s_set_pos;
2992 }
2993 }
2994 }
2995 }
2996
2997 static void
2998 check_label_usage(sym_t *lab)
2999 {
3000
3001 lint_assert(block_level == 1);
3002 lint_assert(lab->s_block_level == 1);
3003
3004 if (funcsym == NULL)
3005 /* syntax error '%s' */
3006 error(249, "labels are only valid inside a function");
3007 else if (lab->s_set && !lab->s_used)
3008 /* label '%s' unused in function '%s' */
3009 warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name);
3010 else if (!lab->s_set)
3011 /* undefined label '%s' */
3012 warning_at(23, &lab->s_use_pos, lab->s_name);
3013 }
3014
3015 static void
3016 check_tag_usage(sym_t *sym)
3017 {
3018
3019 if (!is_incomplete(sym->s_type))
3020 return;
3021
3022 /* always complain about incomplete tags declared inside blocks */
3023 if (!zflag || dcs->d_kind != DK_EXTERN)
3024 return;
3025
3026 switch (sym->s_type->t_tspec) {
3027 case STRUCT:
3028 /* struct '%s' never defined */
3029 warning_at(233, &sym->s_def_pos, sym->s_name);
3030 break;
3031 case UNION:
3032 /* union '%s' never defined */
3033 warning_at(234, &sym->s_def_pos, sym->s_name);
3034 break;
3035 default:
3036 lint_assert(sym->s_type->t_tspec == ENUM);
3037 /* enum '%s' never defined */
3038 warning_at(235, &sym->s_def_pos, sym->s_name);
3039 break;
3040 }
3041 }
3042
3043 /*
3044 * Called after the entire translation unit has been parsed.
3045 * Changes tentative definitions into definitions.
3046 * Performs some tests on global symbols. Detected problems are:
3047 * - defined variables of incomplete type
3048 * - constant variables which are not initialized
3049 * - static symbols which are never used
3050 */
3051 void
3052 check_global_symbols(void)
3053 {
3054 sym_t *sym;
3055
3056 if (block_level != 0 || dcs->d_enclosing != NULL)
3057 norecover();
3058
3059 for (sym = dcs->d_first_dlsym; sym != NULL; sym = sym->s_level_next) {
3060 if (sym->s_block_level == -1)
3061 continue;
3062 if (sym->s_kind == FVFT)
3063 check_global_variable(sym);
3064 else if (sym->s_kind == FTAG)
3065 check_tag_usage(sym);
3066 else
3067 lint_assert(sym->s_kind == FMEMBER);
3068 }
3069 }
3070
3071 static void
3072 check_unused_static_global_variable(const sym_t *sym)
3073 {
3074 if (sym->s_type->t_tspec == FUNC) {
3075 if (sym->s_def == DEF) {
3076 if (!sym->s_inline)
3077 /* static function '%s' unused */
3078 warning_at(236, &sym->s_def_pos, sym->s_name);
3079 } else {
3080 /* static function '%s' declared but not defined */
3081 warning_at(290, &sym->s_def_pos, sym->s_name);
3082 }
3083 } else if (!sym->s_set) {
3084 /* static variable '%s' unused */
3085 warning_at(226, &sym->s_def_pos, sym->s_name);
3086 } else {
3087 /* static variable '%s' set but not used */
3088 warning_at(307, &sym->s_def_pos, sym->s_name);
3089 }
3090 }
3091
3092 static void
3093 check_static_global_variable(const sym_t *sym)
3094 {
3095 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3096 /* static function '%s' called but not defined */
3097 error_at(225, &sym->s_use_pos, sym->s_name);
3098 }
3099
3100 if (!sym->s_used)
3101 check_unused_static_global_variable(sym);
3102
3103 if (allow_c90 && sym->s_def == TDEF && sym->s_type->t_const) {
3104 /* const object '%s' should have initializer */
3105 warning_at(227, &sym->s_def_pos, sym->s_name);
3106 }
3107 }
3108
3109 static void
3110 check_global_variable(const sym_t *sym)
3111 {
3112 scl_t scl = sym->s_scl;
3113
3114 if (scl == TYPEDEF || scl == BOOL_CONST || scl == ENUM_CONST)
3115 return;
3116
3117 if (scl == NOSCL)
3118 return; /* May be caused by a syntax error. */
3119
3120 lint_assert(scl == EXTERN || scl == STATIC);
3121
3122 check_global_variable_size(sym);
3123
3124 if (scl == STATIC)
3125 check_static_global_variable(sym);
3126 }
3127
3128 static void
3129 check_global_variable_size(const sym_t *sym)
3130 {
3131
3132 if (sym->s_def != TDEF)
3133 return;
3134 if (sym->s_type->t_tspec == FUNC) {
3135 /* Maybe a syntax error after a function declaration. */
3136 return;
3137 }
3138 if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID) {
3139 /* Prevent an internal error in length_in_bits below. */
3140 return;
3141 }
3142
3143 pos_t cpos = curr_pos;
3144 curr_pos = sym->s_def_pos;
3145 int len_in_bits = length_in_bits(sym->s_type, sym->s_name);
3146 curr_pos = cpos;
3147
3148 if (len_in_bits == 0 &&
3149 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3150 /* TODO: C99 6.7.5.2p1 defines this as an error as well. */
3151 if (!allow_c90 ||
3152 (sym->s_scl == EXTERN && (allow_trad || allow_c99))) {
3153 /* empty array declaration for '%s' */
3154 warning_at(190, &sym->s_def_pos, sym->s_name);
3155 } else {
3156 /* empty array declaration for '%s' */
3157 error_at(190, &sym->s_def_pos, sym->s_name);
3158 }
3159 }
3160 }
3161
3162 /*
3163 * Prints information about location of previous definition/declaration.
3164 */
3165 void
3166 print_previous_declaration(const sym_t *psym)
3167 {
3168
3169 if (!rflag)
3170 return;
3171
3172 if (psym->s_def == DEF || psym->s_def == TDEF) {
3173 /* previous definition of '%s' */
3174 message_at(261, &psym->s_def_pos, psym->s_name);
3175 } else {
3176 /* previous declaration of '%s' */
3177 message_at(260, &psym->s_def_pos, psym->s_name);
3178 }
3179 }
3180
3181 /*
3182 * Gets a node for a constant and returns the value of this constant
3183 * as integer.
3184 *
3185 * If the node is not constant or too large for int or of type float,
3186 * a warning will be printed.
3187 *
3188 * to_int_constant() should be used only inside declarations. If it is used in
3189 * expressions, it frees the memory used for the expression.
3190 */
3191 int
3192 to_int_constant(tnode_t *tn, bool required)
3193 {
3194
3195 if (tn == NULL)
3196 return 1;
3197
3198 val_t *v = constant(tn, required);
3199
3200 /*
3201 * Abstract declarations are used inside expression. To free
3202 * the memory would be a fatal error.
3203 * We don't free blocks that are inside casts because these
3204 * will be used later to match types.
3205 */
3206 if (tn->tn_op != CON && dcs->d_kind != DK_ABSTRACT)
3207 expr_free_all();
3208
3209 tspec_t t = v->v_tspec;
3210 int i;
3211 if (t == FLOAT || t == DOUBLE || t == LDOUBLE) {
3212 i = (int)v->v_ldbl;
3213 /* integral constant expression expected */
3214 error(55);
3215 } else {
3216 i = (int)v->v_quad;
3217 if (is_uinteger(t)) {
3218 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
3219 /* integral constant too large */
3220 warning(56);
3221 }
3222 } else {
3223 if (v->v_quad > (int64_t)TARG_INT_MAX ||
3224 v->v_quad < (int64_t)TARG_INT_MIN) {
3225 /* integral constant too large */
3226 warning(56);
3227 }
3228 }
3229 }
3230
3231 free(v);
3232 return i;
3233 }
3234