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