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