decl.c revision 1.346 1 /* $NetBSD: decl.c,v 1.346 2023/07/09 11:01:27 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.346 2023/07/09 11:01:27 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 /* TODO: make the return type 'const' */
134 return &typetab[t];
135 }
136
137 type_t *
138 block_dup_type(const type_t *tp)
139 {
140
141 type_t *ntp = block_zero_alloc(sizeof(*ntp));
142 *ntp = *tp;
143 return ntp;
144 }
145
146 /* Duplicate a type, free the allocated memory after the expression. */
147 type_t *
148 expr_dup_type(const type_t *tp)
149 {
150
151 type_t *ntp = expr_zero_alloc(sizeof(*ntp));
152 *ntp = *tp;
153 return ntp;
154 }
155
156 /*
157 * Return the unqualified version of the type. The returned type is freed at
158 * the end of the current expression.
159 *
160 * See C99 6.2.5p25.
161 */
162 type_t *
163 expr_unqualified_type(const type_t *tp)
164 {
165
166 type_t *ntp = expr_zero_alloc(sizeof(*ntp));
167 *ntp = *tp;
168 ntp->t_const = false;
169 ntp->t_volatile = false;
170
171 /*
172 * In case of a struct or union type, the members should lose their
173 * qualifiers as well, but that would require a deep copy of the
174 * struct or union type. This in turn would defeat the type
175 * comparison in types_compatible, which simply tests whether
176 * tp1->t_sou == tp2->t_sou.
177 */
178
179 return ntp;
180 }
181
182 /*
183 * Returns whether the argument is void or an incomplete array, struct, union
184 * or enum type.
185 */
186 bool
187 is_incomplete(const type_t *tp)
188 {
189 tspec_t t = tp->t_tspec;
190
191 if (t == VOID)
192 return true;
193 if (t == ARRAY)
194 return tp->t_incomplete_array;
195 if (is_struct_or_union(t))
196 return tp->t_sou->sou_incomplete;
197 if (t == ENUM)
198 return tp->t_enum->en_incomplete;
199 return false;
200 }
201
202 /*
203 * Remember the storage class of the current declaration and detect multiple
204 * storage classes.
205 */
206 void
207 dcs_add_storage_class(scl_t sc)
208 {
209
210 if (sc == INLINE) {
211 if (dcs->d_inline)
212 /* duplicate '%s' */
213 warning(10, "inline");
214 dcs->d_inline = true;
215 return;
216 }
217
218 if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC ||
219 dcs->d_sign_mod != NO_TSPEC || dcs->d_rank_mod != NO_TSPEC) {
220 /* storage class after type is obsolescent */
221 warning(83);
222 }
223
224 if (dcs->d_scl == NOSCL)
225 dcs->d_scl = sc;
226 else
227 dcs->d_multiple_storage_classes = true;
228 }
229
230 /*
231 * Remember the type, modifier or typedef name returned by the parser in the
232 * top element of the declaration stack. This information is used in
233 * dcs_end_type to build the type used for all declarators in this declaration.
234 *
235 * If tp->t_typedef is true, the type comes from a previously defined typename.
236 * Otherwise, it comes from a type specifier (int, long, ...) or a
237 * struct/union/enum tag.
238 */
239 void
240 dcs_add_type(type_t *tp)
241 {
242
243 debug_step("%s: %s", __func__, type_name(tp));
244 if (tp->t_typedef) {
245 /*
246 * something like "typedef int a; int a b;"
247 * This should not happen with current grammar.
248 */
249 lint_assert(dcs->d_type == NULL);
250 lint_assert(dcs->d_abstract_type == NO_TSPEC);
251 lint_assert(dcs->d_sign_mod == NO_TSPEC);
252 lint_assert(dcs->d_rank_mod == NO_TSPEC);
253
254 dcs->d_type = tp;
255 return;
256 }
257
258 tspec_t t = tp->t_tspec;
259 if (is_struct_or_union(t) || t == ENUM) {
260 /*
261 * something like "int struct a ..."
262 * struct/union/enum with anything else is not allowed
263 */
264 if (dcs->d_type != NULL || dcs->d_abstract_type != NO_TSPEC ||
265 dcs->d_rank_mod != NO_TSPEC || dcs->d_sign_mod != NO_TSPEC) {
266 dcs->d_invalid_type_combination = true;
267 dcs->d_abstract_type = NO_TSPEC;
268 dcs->d_sign_mod = NO_TSPEC;
269 dcs->d_rank_mod = NO_TSPEC;
270 }
271 dcs->d_type = tp;
272 return;
273 }
274
275 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
276 /*
277 * something like "struct a int"
278 * struct/union/enum with anything else is not allowed
279 */
280 dcs->d_invalid_type_combination = true;
281 return;
282 }
283
284 if (t == COMPLEX) {
285 if (dcs->d_complex_mod == FLOAT)
286 t = FCOMPLEX;
287 else if (dcs->d_complex_mod == DOUBLE)
288 t = DCOMPLEX;
289 else {
290 /* invalid type for _Complex */
291 error(308);
292 t = DCOMPLEX; /* just as a fallback */
293 }
294 dcs->d_complex_mod = NO_TSPEC;
295 }
296
297 if (t == LONG && dcs->d_rank_mod == LONG) {
298 /* "long long" or "long ... long" */
299 t = LLONG;
300 dcs->d_rank_mod = NO_TSPEC;
301 if (!long_long_flag)
302 /* %s does not support 'long long' */
303 c99ism(265, allow_c90 ? "C90" : "traditional C");
304 }
305
306 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
307 /* something like "typedef int a; a long ..." */
308 dcs->d_type = typedef_error(dcs->d_type, t);
309 return;
310 }
311
312 /* now it can be only a combination of arithmetic types and void */
313 if (t == SIGNED || t == UNSIGN) {
314 if (dcs->d_sign_mod != NO_TSPEC)
315 dcs->d_invalid_type_combination = true;
316 dcs->d_sign_mod = t;
317 } else if (t == SHORT || t == LONG || t == LLONG) {
318 if (dcs->d_rank_mod != NO_TSPEC)
319 dcs->d_invalid_type_combination = true;
320 dcs->d_rank_mod = t;
321 } else if (t == FLOAT || t == DOUBLE) {
322 if (dcs->d_rank_mod == NO_TSPEC || dcs->d_rank_mod == LONG) {
323 if (dcs->d_complex_mod != NO_TSPEC
324 || (t == FLOAT && dcs->d_rank_mod == LONG))
325 dcs->d_invalid_type_combination = true;
326 dcs->d_complex_mod = t;
327 } else {
328 if (dcs->d_abstract_type != NO_TSPEC)
329 dcs->d_invalid_type_combination = true;
330 dcs->d_abstract_type = t;
331 }
332 } else if (t == PTR) {
333 dcs->d_type = tp;
334 } else {
335 if (dcs->d_abstract_type != NO_TSPEC)
336 dcs->d_invalid_type_combination = true;
337 dcs->d_abstract_type = t;
338 }
339 }
340
341 /* Merge the signedness into the abstract type. */
342 static tspec_t
343 merge_signedness(tspec_t t, tspec_t s)
344 {
345
346 if (s == SIGNED)
347 return t == CHAR ? SCHAR : t;
348 if (s != UNSIGN)
349 return t;
350 return t == CHAR ? UCHAR
351 : t == SHORT ? USHORT
352 : t == INT ? UINT
353 : t == LONG ? ULONG
354 : t == LLONG ? ULLONG
355 : t;
356 }
357
358 /*
359 * Called if a list of declaration specifiers contains a typedef name
360 * and other specifiers (except struct, union, enum, typedef name).
361 */
362 static type_t *
363 typedef_error(type_t *td, tspec_t t)
364 {
365
366 tspec_t t2 = td->t_tspec;
367
368 if ((t == SIGNED || t == UNSIGN) &&
369 (t2 == CHAR || t2 == SHORT || t2 == INT ||
370 t2 == LONG || t2 == LLONG)) {
371 if (allow_c90)
372 /* modifying typedef with '%s'; only qualifiers... */
373 warning(5, tspec_name(t));
374 td = block_dup_type(gettyp(merge_signedness(t2, t)));
375 td->t_typedef = true;
376 return td;
377 }
378
379 if (t == SHORT && (t2 == INT || t2 == UINT)) {
380 /* modifying typedef with '%s'; only qualifiers allowed */
381 warning(5, "short");
382 td = block_dup_type(gettyp(t2 == INT ? SHORT : USHORT));
383 td->t_typedef = true;
384 return td;
385 }
386
387 if (t != LONG)
388 goto invalid;
389
390 if (t2 == INT)
391 td = gettyp(LONG);
392 else if (t2 == UINT)
393 td = gettyp(ULONG);
394 else if (t2 == LONG)
395 td = gettyp(LLONG);
396 else if (t2 == ULONG)
397 td = gettyp(ULLONG);
398 else if (t2 == FLOAT)
399 td = gettyp(DOUBLE);
400 else if (t2 == DOUBLE)
401 td = gettyp(LDOUBLE);
402 else if (t2 == DCOMPLEX)
403 td = gettyp(LCOMPLEX);
404 else
405 goto invalid;
406
407 /* modifying typedef with '%s'; only qualifiers allowed */
408 warning(5, "long");
409 td = block_dup_type(td);
410 td->t_typedef = true;
411 return td;
412
413 invalid:
414 /* Anything else is not accepted. */
415 dcs->d_invalid_type_combination = true;
416 return td;
417 }
418
419 static void
420 set_first_typedef(type_t *tp, sym_t *sym)
421 {
422
423 tspec_t t = tp->t_tspec;
424 if (is_struct_or_union(t) && tp->t_sou->sou_first_typedef == NULL)
425 tp->t_sou->sou_first_typedef = sym;
426 if (t == ENUM && tp->t_enum->en_first_typedef == NULL)
427 tp->t_enum->en_first_typedef = sym;
428 }
429
430 static unsigned int
431 bit_fields_width(const sym_t **mem, bool *named)
432 {
433 unsigned int width = 0;
434 unsigned int align = 0;
435 while (*mem != NULL && (*mem)->s_type->t_bitfield) {
436 if ((*mem)->s_name != unnamed)
437 *named = true;
438 width += (*mem)->s_type->t_bit_field_width;
439 unsigned int mem_align = alignment_in_bits((*mem)->s_type);
440 if (mem_align > align)
441 align = mem_align;
442 *mem = (*mem)->s_next;
443 }
444 return (width + align - 1) & -align;
445 }
446
447 static void
448 pack_struct_or_union(type_t *tp)
449 {
450
451 if (!is_struct_or_union(tp->t_tspec)) {
452 /* attribute '%s' ignored for '%s' */
453 warning(326, "packed", type_name(tp));
454 return;
455 }
456
457 unsigned int bits = 0;
458 bool named = false;
459 for (const sym_t *mem = tp->t_sou->sou_first_member;
460 mem != NULL; mem = mem->s_next) {
461 // TODO: Maybe update mem->u.s_member.sm_offset_in_bits.
462 if (mem->s_type->t_bitfield) {
463 bits += bit_fields_width(&mem, &named);
464 if (mem == NULL)
465 break;
466 }
467 unsigned int mem_bits = type_size_in_bits(mem->s_type);
468 if (tp->t_tspec == STRUCT)
469 bits += mem_bits;
470 else if (mem_bits > bits)
471 bits = mem_bits;
472 }
473 tp->t_sou->sou_size_in_bits = bits;
474 }
475
476 void
477 dcs_add_packed(void)
478 {
479 if (dcs->d_type == NULL)
480 dcs->d_packed = true;
481 else
482 pack_struct_or_union(dcs->d_type);
483 }
484
485 void
486 dcs_set_used(void)
487 {
488 dcs->d_used = true;
489 }
490
491 /*
492 * Remember a qualifier that is part of the declaration specifiers (and not the
493 * declarator). The remembered qualifier is used by dcs_end_type for all
494 * declarators.
495 */
496 void
497 dcs_add_qualifier(tqual_t q)
498 {
499
500 if (q == CONST) {
501 if (dcs->d_const) {
502 /* duplicate '%s' */
503 warning(10, "const");
504 }
505 dcs->d_const = true;
506 } else if (q == VOLATILE) {
507 if (dcs->d_volatile) {
508 /* duplicate '%s' */
509 warning(10, "volatile");
510 }
511 dcs->d_volatile = true;
512 } else {
513 lint_assert(q == RESTRICT || q == THREAD || q == ATOMIC);
514 /* Silently ignore these qualifiers. */
515 }
516 }
517
518 void
519 begin_declaration_level(decl_level_kind kind)
520 {
521
522 decl_level *dl = xcalloc(1, sizeof(*dl));
523 dl->d_enclosing = dcs;
524 dl->d_kind = kind;
525 dl->d_last_dlsym = &dl->d_first_dlsym;
526 dcs = dl;
527 debug_enter();
528 debug_dcs(true);
529 }
530
531 void
532 end_declaration_level(void)
533 {
534
535 debug_dcs(true);
536 debug_leave();
537
538 decl_level *dl = dcs;
539 dcs = dl->d_enclosing;
540 lint_assert(dcs != NULL);
541
542 switch (dl->d_kind) {
543 case DLK_STRUCT:
544 case DLK_UNION:
545 case DLK_ENUM:
546 /*
547 * Symbols declared in (nested) structs or enums are part of
548 * the next level (they are removed from the symbol table if
549 * the symbols of the outer level are removed).
550 */
551 if ((*dcs->d_last_dlsym = dl->d_first_dlsym) != NULL)
552 dcs->d_last_dlsym = dl->d_last_dlsym;
553 break;
554 case DLK_OLD_STYLE_ARGS:
555 /*
556 * All symbols in dcs->d_first_dlsym are introduced in
557 * old-style argument declarations (it's not clean, but
558 * possible). They are appended to the list of symbols declared
559 * in an old-style argument identifier list or a new-style
560 * parameter type list.
561 */
562 if (dl->d_first_dlsym != NULL) {
563 *dl->d_last_dlsym = dcs->d_func_proto_syms;
564 dcs->d_func_proto_syms = dl->d_first_dlsym;
565 }
566 break;
567 case DLK_ABSTRACT:
568 /*
569 * Append all symbols declared in the abstract declaration to
570 * the list of symbols declared in the surrounding declaration
571 * or block.
572 *
573 * XXX I'm not sure whether they should be removed from the
574 * symbol table now or later.
575 */
576 if ((*dcs->d_last_dlsym = dl->d_first_dlsym) != NULL)
577 dcs->d_last_dlsym = dl->d_last_dlsym;
578 break;
579 case DLK_AUTO:
580 check_usage(dl);
581 /* FALLTHROUGH */
582 case DLK_PROTO_PARAMS:
583 /* usage of arguments will be checked by end_function() */
584 symtab_remove_level(dl->d_first_dlsym);
585 break;
586 case DLK_EXTERN:
587 /* there is nothing around an external declarations */
588 /* FALLTHROUGH */
589 default:
590 lint_assert(/*CONSTCOND*/false);
591 }
592 free(dl);
593 }
594
595 /*
596 * Set flag d_asm in all declaration stack elements up to the outermost one.
597 *
598 * This is used to mark compound statements which have, possibly in nested
599 * compound statements, asm statements. For these compound statements, no
600 * warnings about unused or uninitialized variables are printed.
601 *
602 * There is no need to clear d_asm in decl_level structs with context AUTO, as
603 * these structs are freed at the end of the compound statement. But it must be
604 * cleared in the outermost decl_level struct, which has context EXTERN. This
605 * could be done in dcs_begin_type and would work for C90, but not for C99 or
606 * C++ (due to mixed statements and declarations). Thus, we clear it in
607 * global_clean_up_decl.
608 */
609 void
610 dcs_set_asm(void)
611 {
612
613 for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
614 dl->d_asm = true;
615 }
616
617 void
618 dcs_begin_type(void)
619 {
620
621 dcs->d_abstract_type = NO_TSPEC;
622 dcs->d_complex_mod = NO_TSPEC;
623 dcs->d_sign_mod = NO_TSPEC;
624 dcs->d_rank_mod = NO_TSPEC;
625 dcs->d_scl = NOSCL;
626 dcs->d_type = NULL;
627 dcs->d_const = false;
628 dcs->d_volatile = false;
629 dcs->d_inline = false;
630 dcs->d_multiple_storage_classes = false;
631 dcs->d_invalid_type_combination = false;
632 dcs->d_nonempty_decl = false;
633 dcs->d_no_type_specifier = false;
634 }
635
636 static void
637 dcs_adjust_storage_class(void)
638 {
639 if (dcs->d_kind == DLK_EXTERN) {
640 if (dcs->d_scl == REG || dcs->d_scl == AUTO) {
641 /* illegal storage class */
642 error(8);
643 dcs->d_scl = NOSCL;
644 }
645 } else if (dcs->d_kind == DLK_OLD_STYLE_ARGS ||
646 dcs->d_kind == DLK_PROTO_PARAMS) {
647 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) {
648 /* only 'register' is valid as storage class ... */
649 error(9);
650 dcs->d_scl = NOSCL;
651 }
652 }
653 }
654
655 /*
656 * Merge the declaration specifiers from dcs into dcs->d_type.
657 *
658 * See C99 6.7.2 "Type specifiers".
659 */
660 static void
661 dcs_merge_declaration_specifiers(void)
662 {
663 tspec_t t = dcs->d_abstract_type;
664 tspec_t c = dcs->d_complex_mod;
665 tspec_t s = dcs->d_sign_mod;
666 tspec_t l = dcs->d_rank_mod;
667 type_t *tp = dcs->d_type;
668
669 if (tp != NULL) {
670 lint_assert(t == NO_TSPEC);
671 lint_assert(s == NO_TSPEC);
672 lint_assert(l == NO_TSPEC);
673 return;
674 }
675
676 debug_step("%s: %s", __func__, type_name(tp));
677
678 if (t == NO_TSPEC && s == NO_TSPEC && l == NO_TSPEC && c == NO_TSPEC)
679 dcs->d_no_type_specifier = true;
680 if (t == NO_TSPEC && s == NO_TSPEC && (l == NO_TSPEC || l == LONG))
681 t = c;
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_size = 0;
1028 if (dcs->d_kind == DLK_UNION) {
1029 union_size = dcs->d_sou_size_in_bits;
1030 dcs->d_sou_size_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_sou_size_in_bits
1037 - dcs->d_sou_size_in_bits % size_in_bits(tp->t_tspec);
1038 tp->t_bit_field_offset = dcs->d_sou_size_in_bits
1039 - mem->u.s_member.sm_offset_in_bits;
1040 dcs->d_sou_size_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_sou_size_in_bits;
1044 dcs->d_sou_size_in_bits += type_size_in_bits(tp);
1045 }
1046
1047 if (union_size > dcs->d_sou_size_in_bits)
1048 dcs->d_sou_size_in_bits = union_size;
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_sou_size_in_bits + member_alignment - 1)
1129 & ~(member_alignment - 1);
1130 if (bit_field_width == 0
1131 || dcs->d_sou_size_in_bits + bit_field_width > offset)
1132 dcs->d_sou_size_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 *sou = tp->t_sou;
1737 sou->sou_align_in_bits = dcs->d_sou_align_in_bits;
1738 sou->sou_incomplete = false;
1739 sou->sou_first_member = first_member;
1740 if (tp->t_packed)
1741 pack_struct_or_union(tp);
1742 else
1743 sou->sou_size_in_bits = dcs->d_sou_size_in_bits;
1744
1745 if (sou->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 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 allow_c90 &&
1831 !ch_isdigit(sym->s_name[0]) && /* see mktempsym */
1832 strcmp(sym->s_name, "main") != 0) {
1833 /* missing%s header declaration for '%s' */
1834 warning(351, sym->s_type->t_tspec == FUNC ? "" : " 'extern'",
1835 sym->s_name);
1836 }
1837 if (any_query_enabled &&
1838 sym->s_type->t_tspec == FUNC &&
1839 sym->s_scl == EXTERN &&
1840 sym->s_def == DECL &&
1841 !in_system_header) {
1842 /* redundant 'extern' in function declaration of '%s' */
1843 query_message(13, sym->s_name);
1844 }
1845 }
1846
1847 /* Process a single external or 'static' declarator. */
1848 static void
1849 declare_extern(sym_t *dsym, bool has_initializer, sbuf_t *renaming)
1850 {
1851
1852 if (renaming != NULL) {
1853 lint_assert(dsym->s_rename == NULL);
1854
1855 char *s = level_zero_alloc(1, renaming->sb_len + 1);
1856 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1857 dsym->s_rename = s;
1858 }
1859
1860 check_extern_declaration(dsym);
1861
1862 check_function_definition(dsym, true);
1863
1864 check_type(dsym);
1865
1866 if (has_initializer && !check_init(dsym))
1867 dsym->s_def = DEF;
1868
1869 /*
1870 * Declarations of functions are marked as "tentative" in
1871 * declarator_name(). This is wrong because there are no
1872 * tentative function definitions.
1873 */
1874 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1875 dsym->s_def = DECL;
1876
1877 if (dcs->d_inline) {
1878 if (dsym->s_type->t_tspec == FUNC) {
1879 dsym->s_inline = true;
1880 } else {
1881 /* variable '%s' declared inline */
1882 warning(268, dsym->s_name);
1883 }
1884 }
1885
1886 /* Write the declaration into the output file */
1887 if (plibflg && llibflg &&
1888 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1889 /*
1890 * With both LINTLIBRARY and PROTOLIB the prototype is
1891 * written as a function definition to the output file.
1892 */
1893 bool rval = dsym->s_type->t_subt->t_tspec != VOID;
1894 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1895 } else if (!is_compiler_builtin(dsym->s_name)
1896 && !(has_initializer && dsym->s_type->t_incomplete_array)) {
1897 outsym(dsym, dsym->s_scl, dsym->s_def);
1898 }
1899
1900 sym_t *rdsym = dcs->d_redeclared_symbol;
1901 if (rdsym != NULL) {
1902
1903 /*
1904 * If the old symbol stems from an old-style function
1905 * definition, we have remembered the params in
1906 * rdsym->s_old_style_args and compare them with the params
1907 * of the prototype.
1908 */
1909 bool redec = rdsym->s_osdef && dsym->s_type->t_proto &&
1910 check_old_style_definition(rdsym, dsym);
1911
1912 bool dowarn = false;
1913 if (!redec && !check_redeclaration(dsym, &dowarn)) {
1914 if (dowarn) {
1915 /* TODO: Make this an error in C99 mode as well. */
1916 if (!allow_trad && !allow_c99)
1917 /* redeclaration of '%s' */
1918 error(27, dsym->s_name);
1919 else
1920 /* redeclaration of '%s' */
1921 warning(27, dsym->s_name);
1922 print_previous_declaration(rdsym);
1923 }
1924
1925 /*
1926 * Take over the remembered params if the new symbol
1927 * is not a prototype.
1928 */
1929 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1930 dsym->s_osdef = rdsym->s_osdef;
1931 dsym->u.s_old_style_args =
1932 rdsym->u.s_old_style_args;
1933 dsym->s_def_pos = rdsym->s_def_pos;
1934 }
1935
1936 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto)
1937 dsym->s_def_pos = rdsym->s_def_pos;
1938 else if (rdsym->s_def == DEF && dsym->s_def != DEF)
1939 dsym->s_def_pos = rdsym->s_def_pos;
1940
1941 copy_usage_info(dsym, rdsym);
1942
1943 /* Once a name is defined, it remains defined. */
1944 if (rdsym->s_def == DEF)
1945 dsym->s_def = DEF;
1946
1947 /* once a function is inline, it remains inline */
1948 if (rdsym->s_inline)
1949 dsym->s_inline = true;
1950
1951 complete_type(dsym, rdsym);
1952 }
1953
1954 rmsym(rdsym);
1955 }
1956
1957 if (dsym->s_scl == TYPEDEF) {
1958 dsym->s_type = block_dup_type(dsym->s_type);
1959 dsym->s_type->t_typedef = true;
1960 set_first_typedef(dsym->s_type, dsym);
1961 }
1962 }
1963
1964 void
1965 declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
1966 {
1967
1968 if (dcs->d_kind == DLK_EXTERN)
1969 declare_extern(decl, has_initializer, renaming);
1970 else if (dcs->d_kind == DLK_OLD_STYLE_ARGS ||
1971 dcs->d_kind == DLK_PROTO_PARAMS) {
1972 if (renaming != NULL) {
1973 /* symbol renaming can't be used on function arguments */
1974 error(310);
1975 } else
1976 (void)declare_argument(decl, has_initializer);
1977 } else {
1978 lint_assert(dcs->d_kind == DLK_AUTO);
1979 if (renaming != NULL) {
1980 /* symbol renaming can't be used on automatic variables */
1981 error(311);
1982 } else
1983 declare_local(decl, has_initializer);
1984 }
1985 }
1986
1987 /*
1988 * Copies information about usage into a new symbol table entry of
1989 * the same symbol.
1990 */
1991 void
1992 copy_usage_info(sym_t *sym, sym_t *rdsym)
1993 {
1994
1995 sym->s_set_pos = rdsym->s_set_pos;
1996 sym->s_use_pos = rdsym->s_use_pos;
1997 sym->s_set = rdsym->s_set;
1998 sym->s_used = rdsym->s_used;
1999 }
2000
2001 /*
2002 * Prints an error and returns true if a symbol is redeclared/redefined.
2003 * Otherwise, returns false and, in some cases of minor problems, prints
2004 * a warning.
2005 */
2006 bool
2007 check_redeclaration(sym_t *dsym, bool *dowarn)
2008 {
2009
2010 sym_t *rdsym = dcs->d_redeclared_symbol;
2011 if (rdsym->s_scl == ENUM_CONST) {
2012 /* redeclaration of '%s' */
2013 error(27, dsym->s_name);
2014 print_previous_declaration(rdsym);
2015 return true;
2016 }
2017 if (rdsym->s_scl == TYPEDEF) {
2018 /* typedef '%s' redeclared */
2019 error(89, dsym->s_name);
2020 print_previous_declaration(rdsym);
2021 return true;
2022 }
2023 if (dsym->s_scl == TYPEDEF) {
2024 /* redeclaration of '%s' */
2025 error(27, dsym->s_name);
2026 print_previous_declaration(rdsym);
2027 return true;
2028 }
2029 if (rdsym->s_def == DEF && dsym->s_def == DEF) {
2030 /* redefinition of '%s' */
2031 error(28, dsym->s_name);
2032 print_previous_declaration(rdsym);
2033 return true;
2034 }
2035 if (!types_compatible(rdsym->s_type, dsym->s_type,
2036 false, false, dowarn)) {
2037 /* redeclaration of '%s' with type '%s', expected '%s' */
2038 error(347, dsym->s_name,
2039 type_name(dsym->s_type), type_name(rdsym->s_type));
2040 print_previous_declaration(rdsym);
2041 return true;
2042 }
2043 if (rdsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2044 return false;
2045 if (rdsym->s_scl == STATIC && dsym->s_scl == STATIC)
2046 return false;
2047 if (rdsym->s_scl == STATIC && dsym->s_def == DECL)
2048 return false;
2049 if (rdsym->s_scl == EXTERN && rdsym->s_def == DEF) {
2050 /*
2051 * All cases except "int a = 1; static int a;" are caught
2052 * above with or without a warning
2053 */
2054 /* redeclaration of '%s' */
2055 error(27, dsym->s_name);
2056 print_previous_declaration(rdsym);
2057 return true;
2058 }
2059 if (rdsym->s_scl == EXTERN) {
2060 /* '%s' was previously declared extern, becomes static */
2061 warning(29, dsym->s_name);
2062 print_previous_declaration(rdsym);
2063 return false;
2064 }
2065 /*
2066 * Now it's one of:
2067 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2068 */
2069 /* TODO: Make this an error in C99 mode as well. */
2070 if (!allow_trad && !allow_c99) {
2071 /* redeclaration of '%s'; ANSI C requires static */
2072 warning(30, dsym->s_name);
2073 print_previous_declaration(rdsym);
2074 }
2075 dsym->s_scl = STATIC;
2076 return false;
2077 }
2078
2079 static bool
2080 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
2081 {
2082
2083 if (tp1->t_const != tp2->t_const && !ignqual && allow_c90)
2084 return false;
2085 if (tp1->t_volatile != tp2->t_volatile && !ignqual && allow_c90)
2086 return false;
2087 return true;
2088 }
2089
2090 bool
2091 pointer_types_are_compatible(const type_t *tp1, const type_t *tp2, bool ignqual)
2092 {
2093
2094 return tp1->t_tspec == VOID || tp2->t_tspec == VOID ||
2095 qualifiers_correspond(tp1, tp2, ignqual);
2096 }
2097
2098 /*-
2099 * ignqual ignore type qualifiers; used for function parameters
2100 * promot promote the left type; used for comparison of parameters of
2101 * old-style function definitions with parameters of prototypes.
2102 * *dowarn is set to true if an old-style function declaration is not
2103 * compatible with a prototype
2104 */
2105 bool
2106 types_compatible(const type_t *tp1, const type_t *tp2,
2107 bool ignqual, bool promot, bool *dowarn)
2108 {
2109
2110 while (tp1 != NULL && tp2 != NULL) {
2111 tspec_t t = tp1->t_tspec;
2112 if (promot) {
2113 if (t == FLOAT)
2114 t = DOUBLE;
2115 else if (t == CHAR || t == SCHAR)
2116 t = INT;
2117 else if (t == UCHAR)
2118 t = allow_c90 ? INT : UINT;
2119 else if (t == SHORT)
2120 t = INT;
2121 else if (t == USHORT) {
2122 /* CONSTCOND */
2123 t = TARG_INT_MAX < TARG_USHRT_MAX || !allow_c90
2124 ? UINT : INT;
2125 }
2126 }
2127
2128 if (t != tp2->t_tspec)
2129 return false;
2130
2131 if (!qualifiers_correspond(tp1, tp2, ignqual))
2132 return false;
2133
2134 if (is_struct_or_union(t))
2135 return tp1->t_sou == tp2->t_sou;
2136
2137 if (t == ENUM && eflag)
2138 return tp1->t_enum == tp2->t_enum;
2139
2140 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2141 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2142 return false;
2143 }
2144
2145 /* don't check prototypes for traditional */
2146 if (t == FUNC && allow_c90) {
2147 if (tp1->t_proto && tp2->t_proto) {
2148 if (!prototypes_compatible(tp1, tp2, dowarn))
2149 return false;
2150 } else if (tp1->t_proto) {
2151 if (!matches_no_arg_function(tp1, dowarn))
2152 return false;
2153 } else if (tp2->t_proto) {
2154 if (!matches_no_arg_function(tp2, dowarn))
2155 return false;
2156 }
2157 }
2158
2159 tp1 = tp1->t_subt;
2160 tp2 = tp2->t_subt;
2161 ignqual = promot = false;
2162 }
2163
2164 return tp1 == tp2;
2165 }
2166
2167 static bool
2168 prototypes_compatible(const type_t *tp1, const type_t *tp2, bool *dowarn)
2169 {
2170
2171 if (tp1->t_vararg != tp2->t_vararg)
2172 return false;
2173
2174 sym_t *a1 = tp1->t_args;
2175 sym_t *a2 = tp2->t_args;
2176
2177 for (; a1 != NULL && a2 != NULL; a1 = a1->s_next, a2 = a2->s_next) {
2178 if (!types_compatible(a1->s_type, a2->s_type,
2179 true, false, dowarn))
2180 return false;
2181 }
2182 return a1 == a2;
2183 }
2184
2185 /*
2186 * Returns whether all parameters of a prototype are compatible with an
2187 * old-style function declaration.
2188 *
2189 * This is the case if the following conditions are met:
2190 * 1. the prototype has a fixed number of parameters
2191 * 2. no parameter is of type float
2192 * 3. no parameter is converted to another type if integer promotion
2193 * is applied on it
2194 */
2195 static bool
2196 matches_no_arg_function(const type_t *tp, bool *dowarn)
2197 {
2198
2199 if (tp->t_vararg && dowarn != NULL)
2200 *dowarn = true;
2201 for (sym_t *arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2202 tspec_t t = arg->s_type->t_tspec;
2203 if (t == FLOAT ||
2204 t == CHAR || t == SCHAR || t == UCHAR ||
2205 t == SHORT || t == USHORT) {
2206 if (dowarn != NULL)
2207 *dowarn = true;
2208 }
2209 }
2210 /* FIXME: Always returning true cannot be correct. */
2211 return true;
2212 }
2213
2214 /*
2215 * Compares a prototype declaration with the remembered arguments of a previous
2216 * old-style function definition.
2217 */
2218 static bool
2219 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2220 {
2221
2222 sym_t *args = rdsym->u.s_old_style_args;
2223 sym_t *pargs = dsym->s_type->t_args;
2224
2225 bool msg = false;
2226
2227 int narg = 0;
2228 for (sym_t *arg = args; arg != NULL; arg = arg->s_next)
2229 narg++;
2230 int nparg = 0;
2231 for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next)
2232 nparg++;
2233 if (narg != nparg) {
2234 /* prototype does not match old-style definition */
2235 error(63);
2236 msg = true;
2237 goto end;
2238 }
2239
2240 sym_t *arg = args;
2241 sym_t *parg = pargs;
2242 int n = 1;
2243 while (narg-- > 0) {
2244 bool dowarn = false;
2245 /*
2246 * If it does not match due to promotion and lint runs in
2247 * "traditional to C90" migration mode, print only a warning.
2248 *
2249 * XXX: Where is this "only a warning"?
2250 */
2251 if (!types_compatible(arg->s_type, parg->s_type,
2252 true, true, &dowarn) ||
2253 dowarn) {
2254 /* prototype does not match old-style ... */
2255 error(299, n);
2256 msg = true;
2257 }
2258 arg = arg->s_next;
2259 parg = parg->s_next;
2260 n++;
2261 }
2262
2263 end:
2264 if (msg && rflag) {
2265 /* old-style definition */
2266 message_at(300, &rdsym->s_def_pos);
2267 }
2268
2269 return msg;
2270 }
2271
2272 /*
2273 * Completes a type by copying the dimension and prototype information from a
2274 * second compatible type.
2275 *
2276 * Following lines are legal:
2277 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2278 * "typedef ft(); ft f; f(int); ft g; g(long);"
2279 * This means that, if a type is completed, the type structure must be
2280 * duplicated.
2281 */
2282 void
2283 complete_type(sym_t *dsym, sym_t *ssym)
2284 {
2285 type_t **dstp = &dsym->s_type;
2286 type_t *src = ssym->s_type;
2287
2288 while (*dstp != NULL) {
2289 type_t *dst = *dstp;
2290 lint_assert(src != NULL);
2291 lint_assert(dst->t_tspec == src->t_tspec);
2292 if (dst->t_tspec == ARRAY) {
2293 if (dst->t_dim == 0 && src->t_dim != 0) {
2294 *dstp = dst = block_dup_type(dst);
2295 dst->t_dim = src->t_dim;
2296 dst->t_incomplete_array = false;
2297 }
2298 } else if (dst->t_tspec == FUNC) {
2299 if (!dst->t_proto && src->t_proto) {
2300 *dstp = dst = block_dup_type(dst);
2301 dst->t_proto = true;
2302 dst->t_args = src->t_args;
2303 }
2304 }
2305 dstp = &dst->t_subt;
2306 src = src->t_subt;
2307 }
2308 }
2309
2310 /*
2311 * Completes the declaration of a single argument.
2312 */
2313 sym_t *
2314 declare_argument(sym_t *sym, bool has_initializer)
2315 {
2316
2317 check_function_definition(sym, true);
2318
2319 check_type(sym);
2320
2321 if (dcs->d_redeclared_symbol != NULL &&
2322 dcs->d_redeclared_symbol->s_block_level == block_level) {
2323 /* redeclaration of formal parameter '%s' */
2324 error(237, sym->s_name);
2325 rmsym(dcs->d_redeclared_symbol);
2326 sym->s_arg = true;
2327 }
2328
2329 if (!sym->s_arg) {
2330 /* declared argument '%s' is missing */
2331 error(53, sym->s_name);
2332 sym->s_arg = true;
2333 }
2334
2335 if (has_initializer) {
2336 /* cannot initialize parameter '%s' */
2337 error(52, sym->s_name);
2338 }
2339
2340 if (sym->s_type == NULL) /* for c(void()) */
2341 sym->s_type = gettyp(VOID);
2342
2343 tspec_t t = sym->s_type->t_tspec;
2344 if (t == ARRAY)
2345 sym->s_type = block_derive_type(sym->s_type->t_subt, PTR);
2346 if (t == FUNC) {
2347 if (!allow_c90)
2348 /* argument '%s' has function type, should be ... */
2349 warning(50, sym->s_name);
2350 sym->s_type = block_derive_type(sym->s_type, PTR);
2351 }
2352 if (t == FLOAT && !allow_c90)
2353 sym->s_type = gettyp(DOUBLE);
2354
2355 if (dcs->d_inline)
2356 /* argument '%s' declared inline */
2357 warning(269, sym->s_name);
2358
2359 /*
2360 * Arguments must have complete types. length_in_bits prints the
2361 * needed error messages (null dimension is impossible because arrays
2362 * are converted to pointers).
2363 */
2364 if (sym->s_type->t_tspec != VOID)
2365 (void)length_in_bits(sym->s_type, sym->s_name);
2366
2367 sym->s_used = dcs->d_used;
2368 mark_as_set(sym);
2369
2370 return sym;
2371 }
2372
2373 static bool
2374 is_character_pointer(const type_t *tp)
2375 {
2376 tspec_t st;
2377
2378 return tp->t_tspec == PTR &&
2379 (st = tp->t_subt->t_tspec,
2380 st == CHAR || st == SCHAR || st == UCHAR);
2381 }
2382
2383 void
2384 check_func_lint_directives(void)
2385 {
2386
2387 /* check for illegal combinations of lint directives */
2388 if (printflike_argnum != -1 && scanflike_argnum != -1) {
2389 /* ** PRINTFLIKE ** and ** SCANFLIKE ** cannot be combined */
2390 warning(289);
2391 printflike_argnum = scanflike_argnum = -1;
2392 }
2393 if (nvararg != -1 &&
2394 (printflike_argnum != -1 || scanflike_argnum != -1)) {
2395 /* dubious use of ** VARARGS ** with ** %s ** */
2396 warning(288,
2397 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2398 nvararg = -1;
2399 }
2400
2401 /*
2402 * check if the argument of a lint directive is compatible with the
2403 * number of arguments.
2404 */
2405 int narg = 0;
2406 for (sym_t *arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
2407 narg++;
2408 if (nargusg > narg) {
2409 /* argument number mismatch in comment ** %s ** */
2410 warning(283, "ARGSUSED");
2411 nargusg = 0;
2412 }
2413 if (nvararg > narg) {
2414 /* argument number mismatch in comment ** %s ** */
2415 warning(283, "VARARGS");
2416 nvararg = 0;
2417 }
2418 if (printflike_argnum > narg) {
2419 /* argument number mismatch in comment ** %s ** */
2420 warning(283, "PRINTFLIKE");
2421 printflike_argnum = -1;
2422 } else if (printflike_argnum == 0) {
2423 printflike_argnum = -1;
2424 }
2425 if (scanflike_argnum > narg) {
2426 /* argument number mismatch in comment ** %s ** */
2427 warning(283, "SCANFLIKE");
2428 scanflike_argnum = -1;
2429 } else if (scanflike_argnum == 0) {
2430 scanflike_argnum = -1;
2431 }
2432 if (printflike_argnum != -1 || scanflike_argnum != -1) {
2433 narg = printflike_argnum != -1
2434 ? printflike_argnum : scanflike_argnum;
2435 sym_t *arg = dcs->d_func_args;
2436 for (int n = 1; n < narg; n++)
2437 arg = arg->s_next;
2438 if (!is_character_pointer(arg->s_type)) {
2439 /* argument %d must be 'char *' for PRINTFLIKE/... */
2440 warning(293, narg);
2441 printflike_argnum = scanflike_argnum = -1;
2442 }
2443 }
2444 }
2445
2446 /*
2447 * Warn about arguments in old-style function definitions that default to int.
2448 * Check that an old-style function definition is compatible to a previous
2449 * prototype.
2450 */
2451 void
2452 check_func_old_style_arguments(void)
2453 {
2454 int narg;
2455 int nparg;
2456 bool msg;
2457
2458 sym_t *args = funcsym->u.s_old_style_args;
2459 sym_t *pargs = funcsym->s_type->t_args;
2460
2461 /*
2462 * print a warning for each argument of an old-style function
2463 * definition which defaults to int
2464 */
2465 for (sym_t *arg = args; arg != NULL; arg = arg->s_next) {
2466 if (arg->s_defarg) {
2467 /* type of argument '%s' defaults to 'int' */
2468 warning(32, arg->s_name);
2469 arg->s_defarg = false;
2470 mark_as_set(arg);
2471 }
2472 }
2473
2474 /*
2475 * If this is an old-style function definition and a prototype
2476 * exists, compare the types of arguments.
2477 */
2478 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2479 /*
2480 * If the number of arguments does not match, we need not
2481 * continue.
2482 */
2483 narg = nparg = 0;
2484 msg = false;
2485 for (sym_t *parg = pargs; parg != NULL; parg = parg->s_next)
2486 nparg++;
2487 for (sym_t *arg = args; arg != NULL; arg = arg->s_next)
2488 narg++;
2489 if (narg != nparg) {
2490 /* parameter mismatch: %d declared, %d defined */
2491 error(51, nparg, narg);
2492 msg = true;
2493 } else {
2494 sym_t *parg = pargs;
2495 sym_t *arg = args;
2496 while (narg-- > 0) {
2497 msg |= check_prototype_declaration(arg, parg);
2498 parg = parg->s_next;
2499 arg = arg->s_next;
2500 }
2501 }
2502 if (msg && rflag) {
2503 /* prototype declaration */
2504 message_at(285, &dcs->d_redeclared_symbol->s_def_pos);
2505 }
2506
2507 /* from now on the prototype is valid */
2508 funcsym->s_osdef = false;
2509 funcsym->u.s_old_style_args = NULL;
2510 }
2511 }
2512
2513 /*
2514 * Checks compatibility of an old-style function definition with a previous
2515 * prototype declaration.
2516 * Returns true if the position of the previous declaration should be reported.
2517 */
2518 static bool
2519 check_prototype_declaration(sym_t *arg, sym_t *parg)
2520 {
2521 type_t *tp = arg->s_type;
2522 type_t *ptp = parg->s_type;
2523 bool dowarn = false;
2524
2525 if (!types_compatible(tp, ptp, true, true, &dowarn)) {
2526 if (types_compatible(tp, ptp, true, false, &dowarn)) {
2527 /* type of '%s' does not match prototype */
2528 return gnuism(58, arg->s_name);
2529 } else {
2530 /* type of '%s' does not match prototype */
2531 error(58, arg->s_name);
2532 return true;
2533 }
2534 }
2535 if (dowarn) {
2536 /* TODO: Make this an error in C99 mode as well. */
2537 if (!allow_trad && !allow_c99)
2538 /* type of '%s' does not match prototype */
2539 error(58, arg->s_name);
2540 else
2541 /* type of '%s' does not match prototype */
2542 warning(58, arg->s_name);
2543 return true;
2544 }
2545
2546 return false;
2547 }
2548
2549 static void
2550 check_local_hiding(const sym_t *dsym)
2551 {
2552 switch (dsym->s_scl) {
2553 case AUTO:
2554 /* automatic '%s' hides external declaration */
2555 warning(86, dsym->s_name);
2556 break;
2557 case STATIC:
2558 /* static '%s' hides external declaration */
2559 warning(87, dsym->s_name);
2560 break;
2561 case TYPEDEF:
2562 /* typedef '%s' hides external declaration */
2563 warning(88, dsym->s_name);
2564 break;
2565 case EXTERN:
2566 /* Already checked in declare_external_in_block. */
2567 break;
2568 default:
2569 lint_assert(/*CONSTCOND*/false);
2570 }
2571 }
2572
2573 static void
2574 check_local_redeclaration(const sym_t *dsym, sym_t *rdsym)
2575 {
2576 if (rdsym->s_block_level == 0) {
2577 if (hflag)
2578 check_local_hiding(dsym);
2579
2580 } else if (rdsym->s_block_level == block_level) {
2581
2582 /* no hflag, because it's illegal! */
2583 if (rdsym->s_arg) {
2584 /*
2585 * if allow_c90, a "redeclaration of '%s'" error
2586 * is produced below
2587 */
2588 if (!allow_c90) {
2589 if (hflag) {
2590 /* declaration of '%s' hides ... */
2591 warning(91, dsym->s_name);
2592 }
2593 rmsym(rdsym);
2594 }
2595 }
2596
2597 } else if (rdsym->s_block_level < block_level) {
2598 if (hflag) {
2599 /* declaration of '%s' hides earlier one */
2600 warning(95, dsym->s_name);
2601 }
2602 }
2603
2604 if (rdsym->s_block_level == block_level) {
2605 /* redeclaration of '%s' */
2606 error(27, dsym->s_name);
2607 rmsym(rdsym);
2608 }
2609 }
2610
2611 /*
2612 * Completes a single local declaration/definition.
2613 */
2614 void
2615 declare_local(sym_t *dsym, bool has_initializer)
2616 {
2617
2618 /* Correct a mistake done in declarator_name(). */
2619 if (dsym->s_type->t_tspec == FUNC) {
2620 dsym->s_def = DECL;
2621 if (dcs->d_scl == NOSCL)
2622 dsym->s_scl = EXTERN;
2623 }
2624
2625 if (dsym->s_scl == EXTERN) {
2626 /* nested 'extern' declaration of '%s' */
2627 warning(352, dsym->s_name);
2628 }
2629
2630 if (dsym->s_type->t_tspec == FUNC) {
2631 if (dsym->s_scl == STATIC) {
2632 /* dubious static function '%s' at block level */
2633 warning(93, dsym->s_name);
2634 dsym->s_scl = EXTERN;
2635 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2636 /* function '%s' has illegal storage class */
2637 error(94, dsym->s_name);
2638 dsym->s_scl = EXTERN;
2639 }
2640 }
2641
2642 /*
2643 * functions may be declared inline at local scope, although
2644 * this has no effect for a later definition of the same
2645 * function.
2646 * XXX it should have an effect if !allow_c90 is set. this would
2647 * also be the way gcc behaves.
2648 */
2649 if (dcs->d_inline) {
2650 if (dsym->s_type->t_tspec == FUNC)
2651 dsym->s_inline = true;
2652 else {
2653 /* variable '%s' declared inline */
2654 warning(268, dsym->s_name);
2655 }
2656 }
2657
2658 check_function_definition(dsym, true);
2659
2660 check_type(dsym);
2661
2662 if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
2663 declare_external_in_block(dsym);
2664
2665 if (dsym->s_scl == EXTERN) {
2666 /*
2667 * XXX if the static variable at level 0 is only defined
2668 * later, checking will be possible.
2669 */
2670 if (dsym->s_ext_sym == NULL)
2671 outsym(dsym, EXTERN, dsym->s_def);
2672 else
2673 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2674 }
2675
2676 if (dcs->d_redeclared_symbol != NULL)
2677 check_local_redeclaration(dsym, dcs->d_redeclared_symbol);
2678
2679 if (has_initializer && !check_init(dsym)) {
2680 dsym->s_def = DEF;
2681 mark_as_set(dsym);
2682 }
2683
2684 if (dsym->s_scl == TYPEDEF) {
2685 dsym->s_type = block_dup_type(dsym->s_type);
2686 dsym->s_type->t_typedef = true;
2687 set_first_typedef(dsym->s_type, dsym);
2688 }
2689
2690 if (dsym->s_scl == STATIC && any_query_enabled) {
2691 /* static variable '%s' in function */
2692 query_message(11, dsym->s_name);
2693 }
2694 }
2695
2696 /* Processes (re)declarations of external symbols inside blocks. */
2697 static void
2698 declare_external_in_block(sym_t *dsym)
2699 {
2700
2701 /* look for a symbol with the same name */
2702 sym_t *esym = dcs->d_redeclared_symbol;
2703 while (esym != NULL && esym->s_block_level != 0) {
2704 while ((esym = esym->s_symtab_next) != NULL) {
2705 if (esym->s_kind != FVFT)
2706 continue;
2707 if (strcmp(dsym->s_name, esym->s_name) == 0)
2708 break;
2709 }
2710 }
2711 if (esym == NULL)
2712 return;
2713 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2714 /* gcc accepts this without a warning, pcc prints an error. */
2715 /* redeclaration of '%s' */
2716 warning(27, dsym->s_name);
2717 print_previous_declaration(esym);
2718 return;
2719 }
2720
2721 bool dowarn = false;
2722 bool compatible = types_compatible(esym->s_type, dsym->s_type,
2723 false, false, &dowarn);
2724
2725 if (!compatible || dowarn) {
2726 if (esym->s_scl == EXTERN) {
2727 /* inconsistent redeclaration of extern '%s' */
2728 warning(90, dsym->s_name);
2729 print_previous_declaration(esym);
2730 } else {
2731 /* inconsistent redeclaration of static '%s' */
2732 warning(92, dsym->s_name);
2733 print_previous_declaration(esym);
2734 }
2735 }
2736
2737 if (compatible) {
2738 /*
2739 * Remember the external symbol, so we can update usage
2740 * information at the end of the block.
2741 */
2742 dsym->s_ext_sym = esym;
2743 }
2744 }
2745
2746 /*
2747 * Check whether the symbol cannot be initialized due to type/storage class.
2748 * Return whether an error has been detected.
2749 */
2750 static bool
2751 check_init(sym_t *sym)
2752 {
2753
2754 if (sym->s_type->t_tspec == FUNC) {
2755 /* cannot initialize function '%s' */
2756 error(24, sym->s_name);
2757 return true;
2758 }
2759 if (sym->s_scl == TYPEDEF) {
2760 /* cannot initialize typedef '%s' */
2761 error(25, sym->s_name);
2762 return true;
2763 }
2764 if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2765 if (dcs->d_kind == DLK_EXTERN) {
2766 /* cannot initialize extern declaration '%s' */
2767 warning(26, sym->s_name);
2768 } else {
2769 /* cannot initialize extern declaration '%s' */
2770 error(26, sym->s_name);
2771 return true;
2772 }
2773 }
2774
2775 return false;
2776 }
2777
2778 /* Create a symbol for an abstract declaration. */
2779 sym_t *
2780 abstract_name(void)
2781 {
2782
2783 lint_assert(dcs->d_kind == DLK_ABSTRACT
2784 || dcs->d_kind == DLK_PROTO_PARAMS);
2785
2786 sym_t *sym = block_zero_alloc(sizeof(*sym));
2787 sym->s_name = unnamed;
2788 sym->s_def = DEF;
2789 sym->s_scl = ABSTRACT;
2790 sym->s_block_level = -1;
2791 sym->s_arg = dcs->d_kind == DLK_PROTO_PARAMS;
2792
2793 /*
2794 * At this point, dcs->d_type contains only the basic type. That
2795 * type will be updated later, adding pointers, arrays and functions
2796 * as necessary.
2797 */
2798 /*
2799 * XXX: This is not the correct type. For example in msg_347, it is
2800 * the type of the last prototype parameter, but it should rather be
2801 * the return type of the function.
2802 */
2803 sym->s_type = dcs->d_type;
2804 dcs->d_redeclared_symbol = NULL;
2805 dcs->d_vararg = false;
2806
2807 return sym;
2808 }
2809
2810 /* Removes anything which has nothing to do on global level. */
2811 void
2812 global_clean_up(void)
2813 {
2814
2815 while (dcs->d_enclosing != NULL)
2816 end_declaration_level();
2817
2818 clean_up_after_error();
2819 block_level = 0;
2820 mem_block_level = 0;
2821 global_clean_up_decl(true);
2822 }
2823
2824 sym_t *
2825 declare_abstract_type(sym_t *sym)
2826 {
2827
2828 check_function_definition(sym, true);
2829 check_type(sym);
2830 return sym;
2831 }
2832
2833 /* Checks size after declarations of variables and their initialization. */
2834 void
2835 check_size(sym_t *dsym)
2836 {
2837
2838 if (dsym->s_def == DEF &&
2839 dsym->s_scl != TYPEDEF &&
2840 dsym->s_type->t_tspec != FUNC &&
2841 length_in_bits(dsym->s_type, dsym->s_name) == 0 &&
2842 dsym->s_type->t_tspec == ARRAY &&
2843 dsym->s_type->t_dim == 0) {
2844 if (!allow_c90)
2845 /* empty array declaration for '%s' */
2846 warning(190, dsym->s_name);
2847 else
2848 /* empty array declaration for '%s' */
2849 error(190, dsym->s_name);
2850 }
2851 }
2852
2853 /* Mark an object as set if it is not already. */
2854 void
2855 mark_as_set(sym_t *sym)
2856 {
2857
2858 if (!sym->s_set) {
2859 sym->s_set = true;
2860 sym->s_set_pos = unique_curr_pos();
2861 }
2862 }
2863
2864 /* Mark an object as used if it is not already. */
2865 void
2866 mark_as_used(sym_t *sym, bool fcall, bool szof)
2867 {
2868
2869 if (!sym->s_used) {
2870 sym->s_used = true;
2871 sym->s_use_pos = unique_curr_pos();
2872 }
2873 /*
2874 * For function calls, another record is written.
2875 *
2876 * XXX: Should symbols used in sizeof() be treated as used or not?
2877 * Probably not, because there is no point in declaring an external
2878 * variable only to get its size.
2879 */
2880 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2881 outusg(sym);
2882 }
2883
2884 /* Warns about variables and labels that are not used or only set. */
2885 void
2886 check_usage(decl_level *dl)
2887 {
2888 /* for this warning LINTED has no effect */
2889 int saved_lwarn = lwarn;
2890 lwarn = LWARN_ALL;
2891
2892 debug_step("begin lwarn %d", lwarn);
2893 for (sym_t *sym = dl->d_first_dlsym;
2894 sym != NULL; sym = sym->s_level_next)
2895 check_usage_sym(dl->d_asm, sym);
2896 lwarn = saved_lwarn;
2897 debug_step("end lwarn %d", lwarn);
2898 }
2899
2900 /* Warns about a variable or a label that is not used or only set. */
2901 void
2902 check_usage_sym(bool novar, sym_t *sym)
2903 {
2904
2905 if (sym->s_block_level == -1)
2906 return;
2907
2908 if (sym->s_kind == FVFT && sym->s_arg)
2909 check_argument_usage(novar, sym);
2910 else if (sym->s_kind == FVFT)
2911 check_variable_usage(novar, sym);
2912 else if (sym->s_kind == FLABEL)
2913 check_label_usage(sym);
2914 else if (sym->s_kind == FTAG)
2915 check_tag_usage(sym);
2916 }
2917
2918 static void
2919 check_argument_usage(bool novar, sym_t *arg)
2920 {
2921
2922 lint_assert(arg->s_set);
2923
2924 if (novar)
2925 return;
2926
2927 if (!arg->s_used && !vflag) {
2928 /* argument '%s' unused in function '%s' */
2929 warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name);
2930 }
2931 }
2932
2933 static void
2934 check_variable_usage(bool novar, sym_t *sym)
2935 {
2936
2937 lint_assert(block_level != 0);
2938
2939 /* example at file scope: int c = ({ return 3; }); */
2940 if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0]))
2941 return;
2942
2943 /* errors in expressions easily cause lots of these warnings */
2944 if (seen_error)
2945 return;
2946
2947 /*
2948 * XXX Only variables are checked, although types should
2949 * probably also be checked
2950 */
2951 scl_t sc = sym->s_scl;
2952 if (sc != EXTERN && sc != STATIC && sc != AUTO && sc != REG)
2953 return;
2954
2955 if (novar)
2956 return;
2957
2958 if (sc == EXTERN) {
2959 if (!sym->s_used && !sym->s_set) {
2960 /* '%s' unused in function '%s' */
2961 warning_at(192, &sym->s_def_pos,
2962 sym->s_name, funcsym->s_name);
2963 }
2964 } else {
2965 if (sym->s_set && !sym->s_used) {
2966 /* '%s' set but not used in function '%s' */
2967 warning_at(191, &sym->s_set_pos,
2968 sym->s_name, funcsym->s_name);
2969 } else if (!sym->s_used) {
2970 /* '%s' unused in function '%s' */
2971 warning_at(192, &sym->s_def_pos,
2972 sym->s_name, funcsym->s_name);
2973 }
2974 }
2975
2976 if (sc == EXTERN) {
2977 /*
2978 * information about usage is taken over into the symbol
2979 * table entry at level 0 if the symbol was locally declared
2980 * as an external symbol.
2981 *
2982 * XXX This is wrong for symbols declared static at level 0
2983 * if the usage information stems from sizeof(). This is
2984 * because symbols at level 0 only used in sizeof() are
2985 * considered to not be used.
2986 */
2987 sym_t *xsym = sym->s_ext_sym;
2988 if (xsym != NULL) {
2989 if (sym->s_used && !xsym->s_used) {
2990 xsym->s_used = true;
2991 xsym->s_use_pos = sym->s_use_pos;
2992 }
2993 if (sym->s_set && !xsym->s_set) {
2994 xsym->s_set = true;
2995 xsym->s_set_pos = sym->s_set_pos;
2996 }
2997 }
2998 }
2999 }
3000
3001 static void
3002 check_label_usage(sym_t *lab)
3003 {
3004
3005 lint_assert(block_level == 1);
3006 lint_assert(lab->s_block_level == 1);
3007
3008 if (funcsym == NULL)
3009 /* syntax error '%s' */
3010 error(249, "labels are only valid inside a function");
3011 else if (lab->s_set && !lab->s_used)
3012 /* label '%s' unused in function '%s' */
3013 warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name);
3014 else if (!lab->s_set)
3015 /* undefined label '%s' */
3016 warning_at(23, &lab->s_use_pos, lab->s_name);
3017 }
3018
3019 static void
3020 check_tag_usage(sym_t *sym)
3021 {
3022
3023 if (!is_incomplete(sym->s_type))
3024 return;
3025
3026 /* always complain about incomplete tags declared inside blocks */
3027 if (zflag || dcs->d_kind != DLK_EXTERN)
3028 return;
3029
3030 switch (sym->s_type->t_tspec) {
3031 case STRUCT:
3032 /* struct '%s' never defined */
3033 warning_at(233, &sym->s_def_pos, sym->s_name);
3034 break;
3035 case UNION:
3036 /* union '%s' never defined */
3037 warning_at(234, &sym->s_def_pos, sym->s_name);
3038 break;
3039 default:
3040 lint_assert(sym->s_type->t_tspec == ENUM);
3041 /* enum '%s' never defined */
3042 warning_at(235, &sym->s_def_pos, sym->s_name);
3043 break;
3044 }
3045 }
3046
3047 /*
3048 * Called after the entire translation unit has been parsed.
3049 * Changes tentative definitions into definitions.
3050 * Performs some tests on global symbols. Detected problems are:
3051 * - defined variables of incomplete type
3052 * - constant variables which are not initialized
3053 * - static symbols which are never used
3054 */
3055 void
3056 check_global_symbols(void)
3057 {
3058 sym_t *sym;
3059
3060 if (block_level != 0 || dcs->d_enclosing != NULL)
3061 norecover();
3062
3063 for (sym = dcs->d_first_dlsym; sym != NULL; sym = sym->s_level_next) {
3064 if (sym->s_block_level == -1)
3065 continue;
3066 if (sym->s_kind == FVFT)
3067 check_global_variable(sym);
3068 else if (sym->s_kind == FTAG)
3069 check_tag_usage(sym);
3070 else
3071 lint_assert(sym->s_kind == FMEMBER);
3072 }
3073 }
3074
3075 static void
3076 check_unused_static_global_variable(const sym_t *sym)
3077 {
3078 if (sym->s_type->t_tspec == FUNC) {
3079 if (sym->s_def == DEF) {
3080 if (!sym->s_inline)
3081 /* static function '%s' unused */
3082 warning_at(236, &sym->s_def_pos, sym->s_name);
3083 } else {
3084 /* static function '%s' declared but not defined */
3085 warning_at(290, &sym->s_def_pos, sym->s_name);
3086 }
3087 } else if (!sym->s_set) {
3088 /* static variable '%s' unused */
3089 warning_at(226, &sym->s_def_pos, sym->s_name);
3090 } else {
3091 /* static variable '%s' set but not used */
3092 warning_at(307, &sym->s_def_pos, sym->s_name);
3093 }
3094 }
3095
3096 static void
3097 check_static_global_variable(const sym_t *sym)
3098 {
3099 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3100 /* static function '%s' called but not defined */
3101 error_at(225, &sym->s_use_pos, sym->s_name);
3102 }
3103
3104 if (!sym->s_used)
3105 check_unused_static_global_variable(sym);
3106
3107 if (allow_c90 && sym->s_def == TDEF && sym->s_type->t_const) {
3108 /* const object '%s' should have initializer */
3109 warning_at(227, &sym->s_def_pos, sym->s_name);
3110 }
3111 }
3112
3113 static void
3114 check_global_variable(const sym_t *sym)
3115 {
3116 scl_t scl = sym->s_scl;
3117
3118 if (scl == TYPEDEF || scl == BOOL_CONST || scl == ENUM_CONST)
3119 return;
3120
3121 if (scl == NOSCL)
3122 return; /* May be caused by a syntax error. */
3123
3124 lint_assert(scl == EXTERN || scl == STATIC);
3125
3126 check_global_variable_size(sym);
3127
3128 if (scl == STATIC)
3129 check_static_global_variable(sym);
3130 }
3131
3132 static void
3133 check_global_variable_size(const sym_t *sym)
3134 {
3135
3136 if (sym->s_def != TDEF)
3137 return;
3138 if (sym->s_type->t_tspec == FUNC) {
3139 /* Maybe a syntax error after a function declaration. */
3140 return;
3141 }
3142 if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID) {
3143 /* Prevent an internal error in length_in_bits below. */
3144 return;
3145 }
3146
3147 pos_t cpos = curr_pos;
3148 curr_pos = sym->s_def_pos;
3149 int len_in_bits = length_in_bits(sym->s_type, sym->s_name);
3150 curr_pos = cpos;
3151
3152 if (len_in_bits == 0 &&
3153 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3154 /* TODO: C99 6.7.5.2p1 defines this as an error as well. */
3155 if (!allow_c90 ||
3156 (sym->s_scl == EXTERN && (allow_trad || allow_c99))) {
3157 /* empty array declaration for '%s' */
3158 warning_at(190, &sym->s_def_pos, sym->s_name);
3159 } else {
3160 /* empty array declaration for '%s' */
3161 error_at(190, &sym->s_def_pos, sym->s_name);
3162 }
3163 }
3164 }
3165
3166 /*
3167 * Prints information about location of previous definition/declaration.
3168 */
3169 void
3170 print_previous_declaration(const sym_t *psym)
3171 {
3172
3173 if (!rflag)
3174 return;
3175
3176 if (psym->s_def == DEF || psym->s_def == TDEF) {
3177 /* previous definition of '%s' */
3178 message_at(261, &psym->s_def_pos, psym->s_name);
3179 } else {
3180 /* previous declaration of '%s' */
3181 message_at(260, &psym->s_def_pos, psym->s_name);
3182 }
3183 }
3184
3185 /*
3186 * Gets a node for a constant and returns the value of this constant
3187 * as integer.
3188 *
3189 * If the node is not constant or too large for int or of type float,
3190 * a warning will be printed.
3191 *
3192 * to_int_constant() should be used only inside declarations. If it is used in
3193 * expressions, it frees the memory used for the expression.
3194 */
3195 int
3196 to_int_constant(tnode_t *tn, bool required)
3197 {
3198
3199 if (tn == NULL)
3200 return 1;
3201
3202 val_t *v = integer_constant(tn, required);
3203 bool is_unsigned = is_uinteger(v->v_tspec);
3204 int64_t val = v->u.integer;
3205 free(v);
3206
3207 /*
3208 * Abstract declarations are used inside expression. To free
3209 * the memory would be a fatal error.
3210 * We don't free blocks that are inside casts because these
3211 * will be used later to match types.
3212 */
3213 if (tn->tn_op != CON && dcs->d_kind != DLK_ABSTRACT)
3214 expr_free_all();
3215
3216 bool out_of_bounds = is_unsigned
3217 ? (uint64_t)val > (uint64_t)TARG_INT_MAX
3218 : val > (int64_t)TARG_INT_MAX || val < (int64_t)TARG_INT_MIN;
3219 if (out_of_bounds)
3220 /* integral constant too large */
3221 warning(56);
3222 return (int)val;
3223 }
3224