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