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