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