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