decl.c revision 1.92 1 /* $NetBSD: decl.c,v 1.92 2021/01/01 09:11:40 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.92 2021/01/01 09:11:40 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 error(308, tspec_name(dcs->d_cmod));
309 dcs->d_cmod = NOTSPEC;
310 }
311
312 if (t == LONG && dcs->d_lmod == LONG) {
313 /* "long long" or "long ... long" */
314 t = QUAD;
315 dcs->d_lmod = NOTSPEC;
316 if (!quadflg)
317 /* %s C does not support 'long long' */
318 (void)c99ism(265, tflag ? "traditional" : "c89");
319 }
320
321 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
322 /* something like "typedef int a; a long ..." */
323 dcs->d_type = tdeferr(dcs->d_type, t);
324 return;
325 }
326
327 /* now it can be only a combination of arithmetic types and void */
328 if (t == SIGNED || t == UNSIGN) {
329 /* remember specifiers "signed" & "unsigned" in dcs->d_smod */
330 if (dcs->d_smod != NOTSPEC)
331 /*
332 * more than one "signed" and/or "unsigned"; print
333 * an error in deftyp()
334 */
335 dcs->d_terr = 1;
336 dcs->d_smod = t;
337 } else if (t == SHORT || t == LONG || t == QUAD) {
338 /*
339 * remember specifiers "short", "long" and "long long" in
340 * dcs->d_lmod
341 */
342 if (dcs->d_lmod != NOTSPEC)
343 /* more than one, print error in deftyp() */
344 dcs->d_terr = 1;
345 dcs->d_lmod = t;
346 } else if (t == FLOAT || t == DOUBLE) {
347 if (dcs->d_lmod == NOTSPEC || dcs->d_lmod == LONG) {
348 if (dcs->d_cmod != NOTSPEC
349 || (t == FLOAT && dcs->d_lmod == LONG))
350 dcs->d_terr = 1;
351 dcs->d_cmod = t;
352 } else {
353 if (dcs->d_atyp != NOTSPEC)
354 dcs->d_terr = 1;
355 dcs->d_atyp = t;
356 }
357 } else if (t == PTR) {
358 dcs->d_type = tp;
359 } else {
360 /*
361 * remember specifiers "void", "char", "int",
362 * or "_Complex" int dcs->d_atyp
363 */
364 if (dcs->d_atyp != NOTSPEC)
365 /* more than one, print error in deftyp() */
366 dcs->d_terr = 1;
367 dcs->d_atyp = t;
368 }
369 }
370
371 /*
372 * called if a list of declaration specifiers contains a typedef name
373 * and other specifiers (except struct, union, enum, typedef name)
374 */
375 static type_t *
376 tdeferr(type_t *td, tspec_t t)
377 {
378 tspec_t t2;
379
380 t2 = td->t_tspec;
381
382 switch (t) {
383 case SIGNED:
384 case UNSIGN:
385 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
386 t2 == QUAD) {
387 if (!tflag)
388 /* modifying typedef with ... */
389 warning(5, ttab[t].tt_name);
390 td = duptyp(gettyp(merge_type_specifiers(t2, t)));
391 td->t_typedef = 1;
392 return td;
393 }
394 break;
395 case SHORT:
396 if (t2 == INT || t2 == UINT) {
397 /* modifying typedef with ... */
398 warning(5, "short");
399 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
400 td->t_typedef = 1;
401 return td;
402 }
403 break;
404 case LONG:
405 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
406 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) {
407 /* modifying typedef with ... */
408 warning(5, "long");
409 if (t2 == INT) {
410 td = gettyp(LONG);
411 } else if (t2 == UINT) {
412 td = gettyp(ULONG);
413 } else if (t2 == LONG) {
414 td = gettyp(QUAD);
415 } else if (t2 == ULONG) {
416 td = gettyp(UQUAD);
417 } else if (t2 == FLOAT) {
418 td = gettyp(DOUBLE);
419 } else if (t2 == DOUBLE) {
420 td = gettyp(LDOUBLE);
421 } else if (t2 == DCOMPLEX) {
422 td = gettyp(LCOMPLEX);
423 }
424 td = duptyp(td);
425 td->t_typedef = 1;
426 return td;
427 }
428 break;
429 /* LINTED206: (enumeration values not handled in switch) */
430 case NOTSPEC:
431 case USHORT:
432 case UCHAR:
433 case SCHAR:
434 case CHAR:
435 case BOOL:
436 case FUNC:
437 case ARRAY:
438 case PTR:
439 case ENUM:
440 case UNION:
441 case STRUCT:
442 case VOID:
443 case LDOUBLE:
444 case FLOAT:
445 case DOUBLE:
446 case UQUAD:
447 case QUAD:
448 #ifdef INT128_SIZE
449 case UINT128:
450 case INT128:
451 #endif
452 case ULONG:
453 case UINT:
454 case INT:
455 case FCOMPLEX:
456 case DCOMPLEX:
457 case LCOMPLEX:
458 case COMPLEX:
459 break;
460
461 case NTSPEC: /* this value unused */
462 break;
463 }
464
465 /* Anything other is not accepted. */
466
467 dcs->d_terr = 1;
468 return td;
469 }
470
471 /*
472 * Remember the symbol of a typedef name (2nd arg) in a struct, union
473 * or enum tag if the typedef name is the first defined for this tag.
474 *
475 * If the tag is unnamed, the typdef name is used for identification
476 * of this tag in lint2. Although it's possible that more than one typedef
477 * name is defined for one tag, the first name defined should be unique
478 * if the tag is unnamed.
479 */
480 static void
481 settdsym(type_t *tp, sym_t *sym)
482 {
483 tspec_t t;
484
485 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
486 if (tp->t_str->stdef == NULL)
487 tp->t_str->stdef = sym;
488 } else if (t == ENUM) {
489 if (tp->t_enum->etdef == NULL)
490 tp->t_enum->etdef = sym;
491 }
492 }
493
494 static size_t
495 bitfieldsize(sym_t **mem)
496 {
497 size_t len = (*mem)->s_type->t_flen;
498 while (*mem && (*mem)->s_type->t_isfield) {
499 len += (*mem)->s_type->t_flen;
500 *mem = (*mem)->s_next;
501 }
502 return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE;
503 }
504
505 static void
506 setpackedsize(type_t *tp)
507 {
508 str_t *sp;
509 sym_t *mem;
510 char buf[256];
511
512 switch (tp->t_tspec) {
513 case STRUCT:
514 case UNION:
515 sp = tp->t_str;
516 sp->size = 0;
517 for (mem = sp->memb; mem != NULL; mem = mem->s_next) {
518 if (mem->s_type->t_isfield) {
519 sp->size += bitfieldsize(&mem);
520 if (mem == NULL)
521 break;
522 }
523 size_t x = (size_t)tsize(mem->s_type);
524 if (tp->t_tspec == STRUCT)
525 sp->size += x;
526 else if (x > sp->size)
527 sp->size = x;
528 }
529 break;
530 default:
531 warning(326, "packed", tyname(buf, sizeof(buf), tp));
532 break;
533 }
534 }
535
536 void
537 addpacked(void)
538 {
539 if (dcs->d_type == NULL)
540 dcs->d_ispacked = 1;
541 else
542 setpackedsize(dcs->d_type);
543 }
544
545 void
546 add_attr_used(void)
547 {
548 dcs->d_used = 1;
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 = 1;
569 } else {
570 if (q == THREAD)
571 return;
572 lint_assert(q == VOLATILE);
573 if (dcs->d_volatile) {
574 /* duplicate '%s' */
575 warning(10, "volatile");
576 }
577 dcs->d_volatile = 1;
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 EXTERN:
617 /* there is nothing after external declarations */
618 LERROR("popdecl()");
619 /* NOTREACHED */
620 case MOS:
621 case MOU:
622 case ENUMCON:
623 /*
624 * Symbols declared in (nested) structs or enums are
625 * part of the next level (they are removed from the
626 * symbol table if the symbols of the outher level are
627 * removed).
628 */
629 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
630 dcs->d_ldlsym = di->d_ldlsym;
631 break;
632 case ARG:
633 /*
634 * All symbols in dcs->d_dlsyms are introduced in old style
635 * argument declarations (it's not clean, but possible).
636 * They are appended to the list of symbols declared in
637 * an old style argument identifier list or a new style
638 * parameter type list.
639 */
640 if (di->d_dlsyms != NULL) {
641 *di->d_ldlsym = dcs->d_fpsyms;
642 dcs->d_fpsyms = di->d_dlsyms;
643 }
644 break;
645 case ABSTRACT:
646 /*
647 * casts and sizeof
648 * Append all symbols declared in the abstract declaration
649 * to the list of symbols declared in the surrounding
650 * declaration or block.
651 * XXX I'm not sure whether they should be removed from the
652 * symbol table now or later.
653 */
654 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
655 dcs->d_ldlsym = di->d_ldlsym;
656 break;
657 case AUTO:
658 /* check usage of local vars */
659 check_usage(di);
660 /* FALLTHROUGH */
661 case PARG:
662 /* usage of arguments will be checked by funcend() */
663 rmsyms(di->d_dlsyms);
664 break;
665 default:
666 LERROR("popdecl()");
667 }
668 free(di);
669 }
670
671 /*
672 * Set flag d_asm in all declaration stack elements up to the
673 * outermost one.
674 *
675 * This is used to mark compound statements which have, possibly in
676 * nested compound statements, asm statements. For these compound
677 * statements no warnings about unused or unitialized variables are
678 * printed.
679 *
680 * There is no need to clear d_asm in dinfo structs with context AUTO,
681 * because these structs are freed at the end of the compound statement.
682 * But it must be cleared in the outermost dinfo struct, which has
683 * context EXTERN. This could be done in clrtyp() and would work for
684 * C, but not for C++ (due to mixed statements and declarations). Thus
685 * we clear it in global_clean_up_decl(), which is used to do some cleanup
686 * after global declarations/definitions.
687 */
688 void
689 setasm(void)
690 {
691 dinfo_t *di;
692
693 for (di = dcs; di != NULL; di = di->d_next)
694 di->d_asm = 1;
695 }
696
697 /*
698 * Clean all elements of the top element of declaration stack which
699 * will be used by the next declaration
700 */
701 void
702 clrtyp(void)
703 {
704
705 dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC;
706 dcs->d_scl = NOSCL;
707 dcs->d_type = NULL;
708 dcs->d_const = dcs->d_volatile = 0;
709 dcs->d_inline = 0;
710 dcs->d_mscl = dcs->d_terr = 0;
711 dcs->d_nedecl = 0;
712 dcs->d_notyp = 0;
713 }
714
715 /*
716 * Create a type structure from the information gathered in
717 * the declaration stack.
718 * Complain about storage classes which are not possible in current
719 * context.
720 */
721 void
722 deftyp(void)
723 {
724 tspec_t t, s, l, c;
725 type_t *tp;
726 scl_t scl;
727
728 t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */
729 s = dcs->d_smod; /* SIGNED, UNSIGNED */
730 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
731 c = dcs->d_cmod; /* FLOAT, DOUBLE */
732 tp = dcs->d_type;
733 scl = dcs->d_scl;
734
735 #ifdef DEBUG
736 char buf[1024];
737 printf("%s: %s\n", __func__, tyname(buf, sizeof(buf), tp));
738 #endif
739 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
740 tp == NULL)
741 dcs->d_notyp = 1;
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 = 1;
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 = 1;
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 == PARG) {
829 if (scl != NOSCL && scl != REG) {
830 /* only register valid ... */
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 outhermost 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(type_t *tp, const char *name)
889 {
890 int elem, elsz;
891
892 elem = 1;
893 while (tp && 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 (incompl(tp) && name != NULL) {
908 /* incomplete structure or union %s: %s */
909 error(31, tp->t_str->stag->s_name, name);
910 }
911 elsz = tp->t_str->size;
912 break;
913 case ENUM:
914 if (incompl(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 /*
929 * Get the alignment of the given Type in bits.
930 */
931 int
932 getbound(type_t *tp)
933 {
934 size_t a;
935 tspec_t t;
936
937 while (tp && tp->t_tspec == ARRAY)
938 tp = tp->t_subt;
939
940 if (tp == NULL)
941 return -1;
942
943 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
944 a = tp->t_str->align;
945 } else if (t == FUNC) {
946 /* compiler takes alignment of function */
947 error(14);
948 a = WORST_ALIGN(1) * CHAR_BIT;
949 } else {
950 if ((a = size(t)) == 0) {
951 a = CHAR_BIT;
952 } else if (a > WORST_ALIGN(1) * CHAR_BIT) {
953 a = WORST_ALIGN(1) * CHAR_BIT;
954 }
955 }
956 lint_assert(a >= CHAR_BIT);
957 lint_assert(a <= WORST_ALIGN(1) * CHAR_BIT);
958 return a;
959 }
960
961 /*
962 * Concatenate two lists of symbols by s_next. Used by declarations of
963 * struct/union/enum elements and parameters.
964 */
965 sym_t *
966 lnklst(sym_t *l1, sym_t *l2)
967 {
968 sym_t *l;
969
970 if ((l = l1) == NULL)
971 return l2;
972 while (l1->s_next != NULL)
973 l1 = l1->s_next;
974 l1->s_next = l2;
975 return l;
976 }
977
978 /*
979 * Check if the type of the given symbol is valid and print an error
980 * message if it is not.
981 *
982 * Invalid types are:
983 * - arrays of incomlete types or functions
984 * - functions returning arrays or functions
985 * - void types other than type of function or pointer
986 */
987 void
988 check_type(sym_t *sym)
989 {
990 tspec_t to, t;
991 type_t **tpp, *tp;
992
993 tpp = &sym->s_type;
994 to = NOTSPEC;
995 while ((tp = *tpp) != NULL) {
996 t = tp->t_tspec;
997 /*
998 * If this is the type of an old style function definition,
999 * a better warning is printed in funcdef().
1000 */
1001 if (t == FUNC && !tp->t_proto &&
1002 !(to == NOTSPEC && sym->s_osdef)) {
1003 if (sflag && hflag)
1004 /* function declaration is not a prototype */
1005 warning(287);
1006 }
1007 if (to == FUNC) {
1008 if (t == FUNC || t == ARRAY) {
1009 /* function returns illegal type */
1010 error(15);
1011 if (t == FUNC) {
1012 *tpp = incref(*tpp, PTR);
1013 } else {
1014 *tpp = incref((*tpp)->t_subt, PTR);
1015 }
1016 return;
1017 } else if (tp->t_const || tp->t_volatile) {
1018 if (sflag) { /* XXX oder better !tflag ? */
1019 /* function cannot return const... */
1020 warning(228);
1021 }
1022 }
1023 } if (to == ARRAY) {
1024 if (t == FUNC) {
1025 /* array of function is illegal */
1026 error(16);
1027 *tpp = gettyp(INT);
1028 return;
1029 } else if (t == ARRAY && tp->t_dim == 0) {
1030 /* null dimension */
1031 error(17);
1032 return;
1033 } else if (t == VOID) {
1034 /* illegal use of 'void' */
1035 error(18);
1036 *tpp = gettyp(INT);
1037 #if 0 /* errors are produced by length() */
1038 } else if (incompl(tp)) {
1039 /* array of incomplete type */
1040 if (sflag) {
1041 error(301);
1042 } else {
1043 warning(301);
1044 }
1045 #endif
1046 }
1047 } else if (to == NOTSPEC && t == VOID) {
1048 if (dcs->d_ctx == PARG) {
1049 if (sym->s_scl != ABSTRACT) {
1050 lint_assert(sym->s_name != unnamed);
1051 /* void param. cannot have name: %s */
1052 error(61, sym->s_name);
1053 *tpp = gettyp(INT);
1054 }
1055 } else if (dcs->d_ctx == ABSTRACT) {
1056 /* ok */
1057 } else if (sym->s_scl != TYPEDEF) {
1058 /* void type for %s */
1059 error(19, sym->s_name);
1060 *tpp = gettyp(INT);
1061 }
1062 }
1063 if (t == VOID && to != PTR) {
1064 if (tp->t_const || tp->t_volatile) {
1065 /* inappropriate qualifiers with 'void' */
1066 warning(69);
1067 tp->t_const = tp->t_volatile = 0;
1068 }
1069 }
1070 tpp = &tp->t_subt;
1071 to = t;
1072 }
1073 }
1074
1075 /*
1076 * Process the declarator of a struct/union element.
1077 */
1078 sym_t *
1079 declarator_1_struct_union(sym_t *dsym)
1080 {
1081 type_t *tp;
1082 tspec_t t;
1083 int sz, len;
1084 int o = 0; /* Appease gcc */
1085 scl_t sc;
1086
1087 lint_assert((sc = dsym->s_scl) == MOS || sc == MOU);
1088
1089 if (dcs->d_rdcsym != NULL) {
1090 /* should be ensured by storesym() */
1091 lint_assert((sc = dcs->d_rdcsym->s_scl) == MOS || sc == MOU);
1092 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1093 /* duplicate member name: %s */
1094 error(33, dsym->s_name);
1095 rmsym(dcs->d_rdcsym);
1096 }
1097 }
1098
1099 check_type(dsym);
1100
1101 t = (tp = dsym->s_type)->t_tspec;
1102
1103 if (dsym->s_bitfield) {
1104 /*
1105 * only unsigned and signed int are portable bit-field types
1106 *(at least in ANSI C, in traditional C only unsigned int)
1107 */
1108 if (t == CHAR || t == UCHAR || t == SCHAR ||
1109 t == SHORT || t == USHORT || t == ENUM) {
1110 if (bitfieldtype_ok == 0) {
1111 if (sflag) {
1112 char buf[64];
1113 /*
1114 * bit-field type '%s' invalid in
1115 * ANSI C
1116 */
1117 warning(273,
1118 tyname(buf, sizeof(buf), tp));
1119 } else if (pflag) {
1120 /* nonportable bit-field type */
1121 warning(34);
1122 }
1123 }
1124 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1125 if (pflag && bitfieldtype_ok == 0) {
1126 /* nonportable bit-field type */
1127 warning(34);
1128 }
1129 } else if (t != INT && t != UINT) {
1130 /*
1131 * Non-integer types are always illegal for
1132 * bitfields, regardless of BITFIELDTYPE.
1133 * Integer types not dealt with above are
1134 * okay only if BITFIELDTYPE is in effect.
1135 */
1136 if (bitfieldtype_ok == 0 || tspec_is_int(t) == 0) {
1137 /* illegal bit-field type */
1138 warning(35);
1139 sz = tp->t_flen;
1140 dsym->s_type = tp = duptyp(gettyp(t = INT));
1141 if ((tp->t_flen = sz) > size(t))
1142 tp->t_flen = size(t);
1143 }
1144 }
1145 if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) {
1146 /* illegal bit-field size: %d */
1147 error(36, len);
1148 tp->t_flen = size(t);
1149 } else if (len == 0 && dsym->s_name != unnamed) {
1150 /* zero size bit-field */
1151 error(37);
1152 tp->t_flen = size(t);
1153 }
1154 if (dsym->s_scl == MOU) {
1155 /* illegal use of bit-field */
1156 error(41);
1157 dsym->s_type->t_isfield = 0;
1158 dsym->s_bitfield = 0;
1159 }
1160 } else if (t == FUNC) {
1161 /* function illegal in structure or union */
1162 error(38);
1163 dsym->s_type = tp = incref(tp, t = PTR);
1164 }
1165
1166 /*
1167 * bit-fields of length 0 are not warned about because length()
1168 * does not return the length of the bit-field but the length
1169 * of the type the bit-field is packed in (it's ok)
1170 */
1171 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1172 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1173 /* zero sized array in struct is a C99 extension: %s */
1174 c99ism(39, dsym->s_name);
1175 }
1176 }
1177
1178 if (dcs->d_ctx == MOU) {
1179 o = dcs->d_offset;
1180 dcs->d_offset = 0;
1181 }
1182 if (dsym->s_bitfield) {
1183 align(getbound(tp), tp->t_flen);
1184 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1185 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1186 dcs->d_offset += tp->t_flen;
1187 } else {
1188 align(getbound(tp), 0);
1189 dsym->s_value.v_quad = dcs->d_offset;
1190 dcs->d_offset += sz;
1191 }
1192 if (dcs->d_ctx == MOU) {
1193 if (o > dcs->d_offset)
1194 dcs->d_offset = o;
1195 }
1196
1197 check_function_definition(dsym, 0);
1198
1199 /*
1200 * Clear the BITFIELDTYPE indicator after processing each
1201 * structure element.
1202 */
1203 bitfieldtype_ok = 0;
1204
1205 return dsym;
1206 }
1207
1208 /*
1209 * Aligns next structure element as required.
1210 *
1211 * al contains the required alignment, len the length of a bit-field.
1212 */
1213 static void
1214 align(int al, int len)
1215 {
1216 int no;
1217
1218 /*
1219 * The alignment of the current element becomes the alignment of
1220 * the struct/union if it is larger than the current alignment
1221 * of the struct/union.
1222 */
1223 if (al > dcs->d_stralign)
1224 dcs->d_stralign = al;
1225
1226 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1227 if (len == 0 || dcs->d_offset + len > no)
1228 dcs->d_offset = no;
1229 }
1230
1231 /*
1232 * Remember the width of the field in its type structure.
1233 */
1234 sym_t *
1235 bitfield(sym_t *dsym, int len)
1236 {
1237
1238 if (dsym == NULL) {
1239 dsym = getblk(sizeof (sym_t));
1240 dsym->s_name = unnamed;
1241 dsym->s_kind = FMEMBER;
1242 dsym->s_scl = MOS;
1243 dsym->s_type = gettyp(UINT);
1244 dsym->s_blklev = -1;
1245 }
1246 dsym->s_type = duptyp(dsym->s_type);
1247 dsym->s_type->t_isfield = 1;
1248 dsym->s_type->t_flen = len;
1249 dsym->s_bitfield = 1;
1250 return dsym;
1251 }
1252
1253 /*
1254 * Collect information about a sequence of asterisks and qualifiers in a
1255 * list of type pqinf_t.
1256 * Qualifiers always refer to the left asterisk.
1257 * The rightmost asterisk will be at the top of the list.
1258 */
1259 pqinf_t *
1260 merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2)
1261 {
1262 pqinf_t *p;
1263
1264 if (p2->p_pcnt != 0) {
1265 /* left '*' at the end of the list */
1266 for (p = p2; p->p_next != NULL; p = p->p_next)
1267 continue;
1268 p->p_next = p1;
1269 return p2;
1270 } else {
1271 if (p2->p_const) {
1272 if (p1->p_const) {
1273 /* duplicate '%s' */
1274 warning(10, "const");
1275 }
1276 p1->p_const = 1;
1277 }
1278 if (p2->p_volatile) {
1279 if (p1->p_volatile) {
1280 /* duplicate '%s' */
1281 warning(10, "volatile");
1282 }
1283 p1->p_volatile = 1;
1284 }
1285 free(p2);
1286 return p1;
1287 }
1288 }
1289
1290 /*
1291 * The following 3 functions extend the type of a declarator with
1292 * pointer, function and array types.
1293 *
1294 * The current type is the Type built by deftyp() (dcs->d_type) and
1295 * pointer, function and array types already added for this
1296 * declarator. The new type extension is inserted between both.
1297 */
1298 sym_t *
1299 add_pointer(sym_t *decl, pqinf_t *pi)
1300 {
1301 type_t **tpp, *tp;
1302 pqinf_t *npi;
1303
1304 tpp = &decl->s_type;
1305 while (*tpp && *tpp != dcs->d_type)
1306 tpp = &(*tpp)->t_subt;
1307 if (*tpp == NULL)
1308 return decl;
1309
1310 while (pi != NULL) {
1311 *tpp = tp = getblk(sizeof (type_t));
1312 tp->t_tspec = PTR;
1313 tp->t_const = pi->p_const;
1314 tp->t_volatile = pi->p_volatile;
1315 *(tpp = &tp->t_subt) = dcs->d_type;
1316 npi = pi->p_next;
1317 free(pi);
1318 pi = npi;
1319 }
1320 return decl;
1321 }
1322
1323 /*
1324 * If a dimension was specified, dim is 1, otherwise 0
1325 * n is the specified dimension
1326 */
1327 sym_t *
1328 add_array(sym_t *decl, int dim, int n)
1329 {
1330 type_t **tpp, *tp;
1331
1332 tpp = &decl->s_type;
1333 while (*tpp && *tpp != dcs->d_type)
1334 tpp = &(*tpp)->t_subt;
1335 if (*tpp == NULL)
1336 return decl;
1337
1338 *tpp = tp = getblk(sizeof (type_t));
1339 tp->t_tspec = ARRAY;
1340 tp->t_subt = dcs->d_type;
1341 tp->t_dim = n;
1342
1343 if (n < 0) {
1344 /* negative array dimension (%d) */
1345 error(20, n);
1346 n = 0;
1347 } else if (n == 0 && dim) {
1348 /* zero sized array is a C99 extension */
1349 c99ism(322);
1350 } else if (n == 0 && !dim) {
1351 setcomplete(tp, 0);
1352 }
1353
1354 return decl;
1355 }
1356
1357 sym_t *
1358 add_function(sym_t *decl, sym_t *args)
1359 {
1360 type_t **tpp, *tp;
1361
1362 if (dcs->d_proto) {
1363 if (tflag)
1364 /* function prototypes are illegal in traditional C */
1365 warning(270);
1366 args = new_style_function(decl, args);
1367 } else {
1368 old_style_function(decl, args);
1369 }
1370
1371 /*
1372 * The symbols are removed from the symbol table by popdecl() after
1373 * add_function(). To be able to restore them if this is a function
1374 * definition, a pointer to the list of all symbols is stored in
1375 * dcs->d_next->d_fpsyms. Also a list of the arguments (concatenated
1376 * by s_next) is stored in dcs->d_next->d_fargs.
1377 * (dcs->d_next must be used because *dcs is the declaration stack
1378 * element created for the list of params and is removed after
1379 * add_function())
1380 */
1381 if (dcs->d_next->d_ctx == EXTERN &&
1382 decl->s_type == dcs->d_next->d_type) {
1383 dcs->d_next->d_fpsyms = dcs->d_dlsyms;
1384 dcs->d_next->d_fargs = args;
1385 }
1386
1387 tpp = &decl->s_type;
1388 while (*tpp && *tpp != dcs->d_next->d_type)
1389 tpp = &(*tpp)->t_subt;
1390 if (*tpp == NULL)
1391 return decl;
1392
1393 *tpp = tp = getblk(sizeof (type_t));
1394 tp->t_tspec = FUNC;
1395 tp->t_subt = dcs->d_next->d_type;
1396 if ((tp->t_proto = dcs->d_proto) != 0)
1397 tp->t_args = args;
1398 tp->t_vararg = dcs->d_vararg;
1399
1400 return decl;
1401 }
1402
1403 /*
1404 * Called for new style function declarations.
1405 */
1406 /* ARGSUSED */
1407 static sym_t *
1408 new_style_function(sym_t *decl, sym_t *args)
1409 {
1410 sym_t *arg, *sym;
1411 scl_t sc;
1412 int n;
1413
1414 /*
1415 * Declarations of structs/unions/enums in param lists are legal,
1416 * but senseless.
1417 */
1418 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1419 sc = sym->s_scl;
1420 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1421 /* dubious tag declaration: %s %s */
1422 warning(85, storage_class_name(sc), sym->s_name);
1423 }
1424 }
1425
1426 n = 1;
1427 for (arg = args; arg != NULL; arg = arg->s_next) {
1428 if (arg->s_type->t_tspec == VOID) {
1429 if (n > 1 || arg->s_next != NULL) {
1430 /* void must be sole parameter */
1431 error(60);
1432 arg->s_type = gettyp(INT);
1433 }
1434 }
1435 n++;
1436 }
1437
1438 /* return NULL if first param is VOID */
1439 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL;
1440 }
1441
1442 /*
1443 * Called for old style function declarations.
1444 */
1445 static void
1446 old_style_function(sym_t *decl, sym_t *args)
1447 {
1448
1449 /*
1450 * Remember list of params only if this is really seams to be
1451 * a function definition.
1452 */
1453 if (dcs->d_next->d_ctx == EXTERN &&
1454 decl->s_type == dcs->d_next->d_type) {
1455 /*
1456 * We assume that this becomes a function definition. If
1457 * we are wrong, it's corrected in check_function_definition().
1458 */
1459 if (args != NULL) {
1460 decl->s_osdef = 1;
1461 decl->s_args = args;
1462 }
1463 } else {
1464 if (args != NULL)
1465 /* function prototype parameters must have types */
1466 warning(62);
1467 }
1468 }
1469
1470 /*
1471 * Lists of Identifiers in functions declarations are allowed only if
1472 * it's also a function definition. If this is not the case, print a
1473 * error message.
1474 */
1475 void
1476 check_function_definition(sym_t *sym, int msg)
1477 {
1478
1479 if (sym->s_osdef) {
1480 if (msg) {
1481 /* incomplete or misplaced function definition */
1482 error(22);
1483 }
1484 sym->s_osdef = 0;
1485 sym->s_args = NULL;
1486 }
1487 }
1488
1489 /*
1490 * Process the name in a declarator.
1491 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
1492 * TYPEDEF.
1493 * s_def and s_reg are valid after declarator_name().
1494 */
1495 sym_t *
1496 declarator_name(sym_t *sym)
1497 {
1498 scl_t sc = NOSCL;
1499
1500 if (sym->s_scl == NOSCL) {
1501 dcs->d_rdcsym = NULL;
1502 } else if (sym->s_defarg) {
1503 sym->s_defarg = 0;
1504 dcs->d_rdcsym = NULL;
1505 } else {
1506 dcs->d_rdcsym = sym;
1507 sym = pushdown(sym);
1508 }
1509
1510 switch (dcs->d_ctx) {
1511 case MOS:
1512 case MOU:
1513 /* Set parent */
1514 sym->s_styp = dcs->d_tagtyp->t_str;
1515 sym->s_def = DEF;
1516 sym->s_value.v_tspec = INT;
1517 sc = dcs->d_ctx;
1518 break;
1519 case EXTERN:
1520 /*
1521 * static and external symbols without "extern" are
1522 * considered to be tentative defined, external
1523 * symbols with "extern" are declared, and typedef names
1524 * are defined. Tentative defined and declared symbols
1525 * may become defined if an initializer is present or
1526 * this is a function definition.
1527 */
1528 if ((sc = dcs->d_scl) == NOSCL) {
1529 sc = EXTERN;
1530 sym->s_def = TDEF;
1531 } else if (sc == STATIC) {
1532 sym->s_def = TDEF;
1533 } else if (sc == TYPEDEF) {
1534 sym->s_def = DEF;
1535 } else if (sc == EXTERN) {
1536 sym->s_def = DECL;
1537 } else {
1538 LERROR("declarator_name()");
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 if (sc == REG) {
1548 sym->s_reg = 1;
1549 sc = AUTO;
1550 } else {
1551 LERROR("declarator_name()");
1552 }
1553 sym->s_def = DEF;
1554 break;
1555 case AUTO:
1556 if ((sc = dcs->d_scl) == NOSCL) {
1557 /*
1558 * XXX somewhat ugly because we dont know whether
1559 * this is AUTO or EXTERN (functions). If we are
1560 * wrong it must be corrected in decl1loc(), where
1561 * we have the necessary type information.
1562 */
1563 sc = AUTO;
1564 sym->s_def = DEF;
1565 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1566 sym->s_def = DEF;
1567 } else if (sc == REG) {
1568 sym->s_reg = 1;
1569 sc = AUTO;
1570 sym->s_def = DEF;
1571 } else if (sc == EXTERN) {
1572 sym->s_def = DECL;
1573 } else {
1574 LERROR("declarator_name()");
1575 }
1576 break;
1577 default:
1578 LERROR("declarator_name()");
1579 }
1580 sym->s_scl = sc;
1581
1582 sym->s_type = dcs->d_type;
1583
1584 dcs->d_fpsyms = NULL;
1585
1586 return sym;
1587 }
1588
1589 /*
1590 * Process a name in the list of formal params in an old style function
1591 * definition.
1592 */
1593 sym_t *
1594 old_style_function_name(sym_t *sym)
1595 {
1596
1597 if (sym->s_scl != NOSCL) {
1598 if (blklev == sym->s_blklev) {
1599 /* redeclaration of formal parameter %s */
1600 error(21, sym->s_name);
1601 lint_assert(sym->s_defarg);
1602 }
1603 sym = pushdown(sym);
1604 }
1605 sym->s_type = gettyp(INT);
1606 sym->s_scl = AUTO;
1607 sym->s_def = DEF;
1608 sym->s_defarg = sym->s_arg = 1;
1609 return sym;
1610 }
1611
1612 /*
1613 * Create the type of a tag.
1614 *
1615 * tag points to the symbol table entry of the tag
1616 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1617 * decl is 1 if the type of the tag will be completed in this declaration
1618 * (the following token is T_LBRACE)
1619 * semi is 1 if the following token is T_SEMI
1620 */
1621 type_t *
1622 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1623 {
1624 scl_t scl = NOSCL;
1625 type_t *tp;
1626
1627 if (kind == STRUCT) {
1628 scl = STRTAG;
1629 } else if (kind == UNION) {
1630 scl = UNIONTAG;
1631 } else {
1632 lint_assert(kind == ENUM);
1633 scl = ENUMTAG;
1634 }
1635
1636 if (tag != NULL) {
1637 if (tag->s_scl != NOSCL) {
1638 tag = newtag(tag, scl, decl, semi);
1639 } else {
1640 /* a new tag, no empty declaration */
1641 dcs->d_next->d_nedecl = 1;
1642 if (scl == ENUMTAG && !decl) {
1643 if (!tflag && (sflag || pflag))
1644 /* forward reference to enum type */
1645 warning(42);
1646 }
1647 }
1648 if (tag->s_scl == NOSCL) {
1649 tag->s_scl = scl;
1650 tag->s_type = tp = getblk(sizeof (type_t));
1651 tp->t_ispacked = dcs->d_ispacked;
1652 } else {
1653 tp = tag->s_type;
1654 }
1655 } else {
1656 tag = getblk(sizeof (sym_t));
1657 tag->s_name = unnamed;
1658 UNIQUE_CURR_POS(tag->s_def_pos);
1659 tag->s_kind = FTAG;
1660 tag->s_scl = scl;
1661 tag->s_blklev = -1;
1662 tag->s_type = tp = getblk(sizeof (type_t));
1663 tp->t_ispacked = dcs->d_ispacked;
1664 dcs->d_next->d_nedecl = 1;
1665 }
1666
1667 if (tp->t_tspec == NOTSPEC) {
1668 tp->t_tspec = kind;
1669 if (kind != ENUM) {
1670 tp->t_str = getblk(sizeof (str_t));
1671 tp->t_str->align = CHAR_BIT;
1672 tp->t_str->stag = tag;
1673 } else {
1674 tp->t_isenum = 1;
1675 tp->t_enum = getblk(sizeof(*tp->t_enum));
1676 tp->t_enum->etag = tag;
1677 }
1678 setcomplete(tp, 0);
1679 }
1680 return tp;
1681 }
1682
1683 /*
1684 * Checks all possible cases of tag redeclarations.
1685 * decl is 1 if T_LBRACE follows
1686 * semi is 1 if T_SEMI follows
1687 */
1688 static sym_t *
1689 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1690 {
1691
1692 if (tag->s_blklev < blklev) {
1693 if (semi) {
1694 /* "struct a;" */
1695 if (!tflag) {
1696 if (!sflag)
1697 /* decl. introduces new type ... */
1698 warning(44, storage_class_name(scl),
1699 tag->s_name);
1700 tag = pushdown(tag);
1701 } else if (tag->s_scl != scl) {
1702 /* base type is really '%s %s' */
1703 warning(45, storage_class_name(tag->s_scl),
1704 tag->s_name);
1705 }
1706 dcs->d_next->d_nedecl = 1;
1707 } else if (decl) {
1708 /* "struct a { ... } " */
1709 if (hflag)
1710 /* redefinition hides earlier one: %s */
1711 warning(43, tag->s_name);
1712 tag = pushdown(tag);
1713 dcs->d_next->d_nedecl = 1;
1714 } else if (tag->s_scl != scl) {
1715 /* base type is really '%s %s' */
1716 warning(45, storage_class_name(tag->s_scl),
1717 tag->s_name);
1718 /* declaration introduces new type in ANSI C: %s %s */
1719 if (!sflag) {
1720 warning(44, storage_class_name(scl),
1721 tag->s_name);
1722 }
1723 tag = pushdown(tag);
1724 dcs->d_next->d_nedecl = 1;
1725 }
1726 } else {
1727 if (tag->s_scl != scl) {
1728 /* (%s) tag redeclared */
1729 error(46, storage_class_name(tag->s_scl));
1730 print_previous_declaration(-1, tag);
1731 tag = pushdown(tag);
1732 dcs->d_next->d_nedecl = 1;
1733 } else if (decl && !incompl(tag->s_type)) {
1734 /* (%s) tag redeclared */
1735 error(46, storage_class_name(tag->s_scl));
1736 print_previous_declaration(-1, tag);
1737 tag = pushdown(tag);
1738 dcs->d_next->d_nedecl = 1;
1739 } else if (semi || decl) {
1740 dcs->d_next->d_nedecl = 1;
1741 }
1742 }
1743 return tag;
1744 }
1745
1746 const char *
1747 storage_class_name(scl_t sc)
1748 {
1749 const char *s;
1750
1751 switch (sc) {
1752 case EXTERN: s = "extern"; break;
1753 case STATIC: s = "static"; break;
1754 case AUTO: s = "auto"; break;
1755 case REG: s = "register"; break;
1756 case TYPEDEF: s = "typedef"; break;
1757 case STRTAG: s = "struct"; break;
1758 case UNIONTAG: s = "union"; break;
1759 case ENUMTAG: s = "enum"; break;
1760 default: LERROR("tagttoa()");
1761 }
1762 return s;
1763 }
1764
1765 /*
1766 * tp points to the type of the tag, fmem to the list of members.
1767 */
1768 type_t *
1769 complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1770 {
1771 tspec_t t;
1772 str_t *sp;
1773 int n;
1774 sym_t *mem;
1775
1776 setcomplete(tp, 1);
1777
1778 t = tp->t_tspec;
1779 align(dcs->d_stralign, 0);
1780 sp = tp->t_str;
1781 sp->align = dcs->d_stralign;
1782 sp->memb = fmem;
1783 if (tp->t_ispacked)
1784 setpackedsize(tp);
1785 else
1786 sp->size = dcs->d_offset;
1787
1788 if (sp->size == 0) {
1789 /* zero sized %s */
1790 (void)c99ism(47, ttab[t].tt_name);
1791 }
1792
1793 n = 0;
1794 for (mem = fmem; mem != NULL; mem = mem->s_next) {
1795 /* bind anonymous members to the structure */
1796 if (mem->s_styp == NULL) {
1797 mem->s_styp = sp;
1798 if (mem->s_type->t_isfield) {
1799 sp->size += bitfieldsize(&mem);
1800 if (mem == NULL)
1801 break;
1802 }
1803 sp->size += tsize(mem->s_type);
1804 }
1805 if (mem->s_name != unnamed)
1806 n++;
1807 }
1808
1809 if (n == 0 && sp->size != 0) {
1810 /* %s has no named members */
1811 warning(65, t == STRUCT ? "structure" : "union");
1812 }
1813 return tp;
1814 }
1815
1816 type_t *
1817 complete_tag_enum(type_t *tp, sym_t *fmem)
1818 {
1819
1820 setcomplete(tp, 1);
1821 tp->t_enum->elem = fmem;
1822 return tp;
1823 }
1824
1825 /*
1826 * Processes the name of an enumerator in an enum declaration.
1827 *
1828 * sym points to the enumerator
1829 * val is the value of the enumerator
1830 * impl is 1 if the value of the enumerator was not explicitly specified.
1831 */
1832 sym_t *
1833 ename(sym_t *sym, int val, int impl)
1834 {
1835
1836 if (sym->s_scl) {
1837 if (sym->s_blklev == blklev) {
1838 /* no hflag, because this is illegal!!! */
1839 if (sym->s_arg) {
1840 /* enumeration constant hides parameter: %s */
1841 warning(57, sym->s_name);
1842 } else {
1843 /* redeclaration of %s */
1844 error(27, sym->s_name);
1845 /*
1846 * inside blocks it should not be too
1847 * complicated to find the position of the
1848 * previous declaration
1849 */
1850 if (blklev == 0)
1851 print_previous_declaration(-1, sym);
1852 }
1853 } else {
1854 if (hflag)
1855 /* redefinition hides earlier one: %s */
1856 warning(43, sym->s_name);
1857 }
1858 sym = pushdown(sym);
1859 }
1860 sym->s_scl = ENUMCON;
1861 sym->s_type = dcs->d_tagtyp;
1862 sym->s_value.v_tspec = INT;
1863 sym->s_value.v_quad = val;
1864 if (impl && val - 1 == TARG_INT_MAX) {
1865 /* overflow in enumeration values: %s */
1866 warning(48, sym->s_name);
1867 }
1868 enumval = val + 1;
1869 return sym;
1870 }
1871
1872 /*
1873 * Process a single external declarator.
1874 */
1875 void
1876 decl1ext(sym_t *dsym, int initflg)
1877 {
1878 int dowarn, rval, redec;
1879 sym_t *rdsym;
1880
1881 check_function_definition(dsym, 1);
1882
1883 check_type(dsym);
1884
1885 if (initflg && !(initerr = check_init(dsym)))
1886 dsym->s_def = DEF;
1887
1888 /*
1889 * Declarations of functions are marked as "tentative" in
1890 * declarator_name(). This is wrong because there are no
1891 * tentative function definitions.
1892 */
1893 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1894 dsym->s_def = DECL;
1895
1896 if (dcs->d_inline) {
1897 if (dsym->s_type->t_tspec == FUNC) {
1898 dsym->s_inline = 1;
1899 } else {
1900 /* variable declared inline: %s */
1901 warning(268, dsym->s_name);
1902 }
1903 }
1904
1905 /* Write the declaration into the output file */
1906 if (plibflg && llibflg &&
1907 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1908 /*
1909 * With both LINTLIBRARY and PROTOLIB the prototyp is
1910 * written as a function definition to the output file.
1911 */
1912 rval = dsym->s_type->t_subt->t_tspec != VOID;
1913 outfdef(dsym, &dsym->s_def_pos, rval, 0, NULL);
1914 } else {
1915 outsym(dsym, dsym->s_scl, dsym->s_def);
1916 }
1917
1918 if ((rdsym = dcs->d_rdcsym) != NULL) {
1919
1920 /*
1921 * If the old symbol stems from an old style function
1922 * definition, we have remembered the params in rdsmy->s_args
1923 * and compare them with the params of the prototype.
1924 */
1925 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1926 redec = check_old_style_definition(rdsym, dsym);
1927 } else {
1928 redec = 0;
1929 }
1930
1931 if (!redec && !check_redeclaration(dsym, (dowarn = 0, &dowarn))) {
1932
1933 if (dowarn) {
1934 /* redeclaration of %s */
1935 (*(sflag ? error : warning))(27, dsym->s_name);
1936 print_previous_declaration(-1, rdsym);
1937 }
1938
1939 /*
1940 * Take over the remembered params if the new symbol
1941 * is not a prototype.
1942 */
1943 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1944 dsym->s_osdef = rdsym->s_osdef;
1945 dsym->s_args = rdsym->s_args;
1946 dsym->s_def_pos = rdsym->s_def_pos;
1947 }
1948
1949 /*
1950 * Remember the position of the declaration if the
1951 * old symbol was a prototype and the new is not.
1952 * Also remember the position if the old symbol
1953 * was defined and the new is not.
1954 */
1955 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1956 dsym->s_def_pos = rdsym->s_def_pos;
1957 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1958 dsym->s_def_pos = rdsym->s_def_pos;
1959 }
1960
1961 /*
1962 * Copy usage information of the name into the new
1963 * symbol.
1964 */
1965 copy_usage_info(dsym, rdsym);
1966
1967 /* Once a name is defined, it remains defined. */
1968 if (rdsym->s_def == DEF)
1969 dsym->s_def = DEF;
1970
1971 /* once a function is inline, it remains inline */
1972 if (rdsym->s_inline)
1973 dsym->s_inline = 1;
1974
1975 complete_type(dsym, rdsym);
1976
1977 }
1978
1979 rmsym(rdsym);
1980 }
1981
1982 if (dsym->s_scl == TYPEDEF) {
1983 dsym->s_type = duptyp(dsym->s_type);
1984 dsym->s_type->t_typedef = 1;
1985 settdsym(dsym->s_type, dsym);
1986 }
1987
1988 }
1989
1990 /*
1991 * Copies information about usage into a new symbol table entry of
1992 * the same symbol.
1993 */
1994 void
1995 copy_usage_info(sym_t *sym, sym_t *rdsym)
1996 {
1997
1998 sym->s_set_pos = rdsym->s_set_pos;
1999 sym->s_use_pos = rdsym->s_use_pos;
2000 sym->s_set = rdsym->s_set;
2001 sym->s_used = rdsym->s_used;
2002 }
2003
2004 /*
2005 * Prints an error and returns 1 if a symbol is redeclared/redefined.
2006 * Otherwise returns 0 and, in some cases of minor problems, prints
2007 * a warning.
2008 */
2009 int
2010 check_redeclaration(sym_t *dsym, int *dowarn)
2011 {
2012 sym_t *rsym;
2013
2014 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
2015 /* redeclaration of %s */
2016 error(27, dsym->s_name);
2017 print_previous_declaration(-1, rsym);
2018 return 1;
2019 }
2020 if (rsym->s_scl == TYPEDEF) {
2021 /* typedef redeclared: %s */
2022 error(89, dsym->s_name);
2023 print_previous_declaration(-1, rsym);
2024 return 1;
2025 }
2026 if (dsym->s_scl == TYPEDEF) {
2027 /* redeclaration of %s */
2028 error(27, dsym->s_name);
2029 print_previous_declaration(-1, rsym);
2030 return 1;
2031 }
2032 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2033 /* redefinition of %s */
2034 error(28, dsym->s_name);
2035 print_previous_declaration(-1, rsym);
2036 return(1);
2037 }
2038 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) {
2039 /* redeclaration of %s */
2040 error(27, dsym->s_name);
2041 print_previous_declaration(-1, rsym);
2042 return(1);
2043 }
2044 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2045 return(0);
2046 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2047 return(0);
2048 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2049 return(0);
2050 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2051 /*
2052 * All cases except "int a = 1; static int a;" are caught
2053 * above with or without a warning
2054 */
2055 /* redeclaration of %s */
2056 error(27, dsym->s_name);
2057 print_previous_declaration(-1, rsym);
2058 return(1);
2059 }
2060 if (rsym->s_scl == EXTERN) {
2061 /* previously declared extern, becomes static: %s */
2062 warning(29, dsym->s_name);
2063 print_previous_declaration(-1, rsym);
2064 return(0);
2065 }
2066 /*
2067 * Now it's one of:
2068 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2069 */
2070 /* redeclaration of %s; ANSI C requires "static" */
2071 if (sflag) {
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 /* automatic hides external declaration: %s */
2620 if (hflag)
2621 warning(86, dsym->s_name);
2622 break;
2623 case STATIC:
2624 /* static hides external declaration: %s */
2625 if (hflag)
2626 warning(87, dsym->s_name);
2627 break;
2628 case TYPEDEF:
2629 /* typedef hides external declaration: %s */
2630 if (hflag)
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 LERROR("decl1loc()");
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 warning(26, sym->s_name);
2769 } else {
2770 error(26, sym->s_name);
2771 erred = 1;
2772 }
2773 }
2774
2775 return erred;
2776 }
2777
2778 /*
2779 * Create a symbol for an abstract declaration.
2780 */
2781 sym_t *
2782 abstract_name(void)
2783 {
2784 sym_t *sym;
2785
2786 lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PARG);
2787
2788 sym = getblk(sizeof (sym_t));
2789
2790 sym->s_name = unnamed;
2791 sym->s_def = DEF;
2792 sym->s_scl = ABSTRACT;
2793 sym->s_blklev = -1;
2794
2795 if (dcs->d_ctx == PARG)
2796 sym->s_arg = 1;
2797
2798 sym->s_type = dcs->d_type;
2799 dcs->d_rdcsym = NULL;
2800 dcs->d_vararg = 0;
2801
2802 return sym;
2803 }
2804
2805 /*
2806 * Removes anything which has nothing to do on global level.
2807 */
2808 void
2809 global_clean_up(void)
2810 {
2811
2812 while (dcs->d_next != NULL)
2813 popdecl();
2814
2815 cleanup();
2816 blklev = 0;
2817 mblklev = 0;
2818
2819 /*
2820 * remove all information about pending lint directives without
2821 * warnings.
2822 */
2823 global_clean_up_decl(1);
2824 }
2825
2826 /*
2827 * Process an abstract type declaration
2828 */
2829 sym_t *
2830 declare_1_abstract(sym_t *sym)
2831 {
2832
2833 check_function_definition(sym, 1);
2834 check_type(sym);
2835 return sym;
2836 }
2837
2838 /*
2839 * Checks size after declarations of variables and their initialisation.
2840 */
2841 void
2842 check_size(sym_t *dsym)
2843 {
2844
2845 if (dsym->s_def != DEF)
2846 return;
2847 if (dsym->s_scl == TYPEDEF)
2848 return;
2849 if (dsym->s_type->t_tspec == FUNC)
2850 return;
2851
2852 if (length(dsym->s_type, dsym->s_name) == 0 &&
2853 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2854 /* empty array declaration: %s */
2855 if (tflag) {
2856 warning(190, dsym->s_name);
2857 } else {
2858 error(190, dsym->s_name);
2859 }
2860 }
2861 }
2862
2863 /*
2864 * Mark an object as set if it is not already
2865 */
2866 void
2867 mark_as_set(sym_t *sym)
2868 {
2869
2870 if (!sym->s_set) {
2871 sym->s_set = 1;
2872 UNIQUE_CURR_POS(sym->s_set_pos);
2873 }
2874 }
2875
2876 /*
2877 * Mark an object as used if it is not already
2878 */
2879 void
2880 mark_as_used(sym_t *sym, int fcall, int szof)
2881 {
2882
2883 if (!sym->s_used) {
2884 sym->s_used = 1;
2885 UNIQUE_CURR_POS(sym->s_use_pos);
2886 }
2887 /*
2888 * for function calls another record is written
2889 *
2890 * XXX Should symbols used in sizeof() be treated as used or not?
2891 * Probably not, because there is no sense to declare an
2892 * external variable only to get their size.
2893 */
2894 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2895 outusg(sym);
2896 }
2897
2898 /*
2899 * Prints warnings for a list of variables and labels (concatenated
2900 * with s_dlnxt) if these are not used or only set.
2901 */
2902 void
2903 check_usage(dinfo_t *di)
2904 {
2905 sym_t *sym;
2906 int mklwarn;
2907
2908 /* for this warning LINTED has no effect */
2909 mklwarn = lwarn;
2910 lwarn = LWARN_ALL;
2911
2912 #ifdef DEBUG
2913 printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2914 lwarn);
2915 #endif
2916 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2917 check_usage_sym(di->d_asm, sym);
2918 lwarn = mklwarn;
2919 #ifdef DEBUG
2920 printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2921 lwarn);
2922 #endif
2923 }
2924
2925 /*
2926 * Prints a warning for a single variable or label if it is not used or
2927 * only set.
2928 */
2929 void
2930 check_usage_sym(int novar, sym_t *sym)
2931 {
2932 pos_t cpos;
2933
2934 if (sym->s_blklev == -1)
2935 return;
2936
2937 cpos = curr_pos;
2938
2939 if (sym->s_kind == FVFT) {
2940 if (sym->s_arg) {
2941 check_argument_usage(novar, sym);
2942 } else {
2943 check_variable_usage(novar, sym);
2944 }
2945 } else if (sym->s_kind == FLABEL) {
2946 check_label_usage(sym);
2947 } else if (sym->s_kind == FTAG) {
2948 check_tag_usage(sym);
2949 }
2950
2951 curr_pos = cpos;
2952 }
2953
2954 static void
2955 check_argument_usage(int novar, sym_t *arg)
2956 {
2957
2958 lint_assert(arg->s_set);
2959
2960 if (novar)
2961 return;
2962
2963 if (!arg->s_used && vflag) {
2964 curr_pos = arg->s_def_pos;
2965 /* argument %s unused in function %s */
2966 warning(231, arg->s_name, funcsym->s_name);
2967 }
2968 }
2969
2970 static void
2971 check_variable_usage(int novar, sym_t *sym)
2972 {
2973 scl_t sc;
2974 sym_t *xsym;
2975
2976 lint_assert(blklev != 0);
2977 lint_assert(sym->s_blklev != 0);
2978
2979 /* errors in expressions easily cause lots of these warnings */
2980 if (nerr != 0)
2981 return;
2982
2983 /*
2984 * XXX Only variables are checked, although types should
2985 * probably also be checked
2986 */
2987 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2988 sc != AUTO && sc != REG) {
2989 return;
2990 }
2991
2992 if (novar)
2993 return;
2994
2995 if (sc == EXTERN) {
2996 if (!sym->s_used && !sym->s_set) {
2997 curr_pos = sym->s_def_pos;
2998 /* %s unused in function %s */
2999 warning(192, sym->s_name, funcsym->s_name);
3000 }
3001 } else {
3002 if (sym->s_set && !sym->s_used) {
3003 curr_pos = sym->s_set_pos;
3004 /* %s set but not used in function %s */
3005 warning(191, sym->s_name, funcsym->s_name);
3006 } else if (!sym->s_used) {
3007 curr_pos = sym->s_def_pos;
3008 /* %s unused in function %s */
3009 warning(192, sym->s_name, funcsym->s_name);
3010 }
3011 }
3012
3013 if (sc == EXTERN) {
3014 /*
3015 * information about usage is taken over into the symbol
3016 * table entry at level 0 if the symbol was locally declared
3017 * as an external symbol.
3018 *
3019 * XXX This is wrong for symbols declared static at level 0
3020 * if the usage information stems from sizeof(). This is
3021 * because symbols at level 0 only used in sizeof() are
3022 * considered to not be used.
3023 */
3024 if ((xsym = sym->s_ext_sym) != NULL) {
3025 if (sym->s_used && !xsym->s_used) {
3026 xsym->s_used = 1;
3027 xsym->s_use_pos = sym->s_use_pos;
3028 }
3029 if (sym->s_set && !xsym->s_set) {
3030 xsym->s_set = 1;
3031 xsym->s_set_pos = sym->s_set_pos;
3032 }
3033 }
3034 }
3035 }
3036
3037 static void
3038 check_label_usage(sym_t *lab)
3039 {
3040
3041 lint_assert(blklev == 1);
3042 lint_assert(lab->s_blklev == 1);
3043
3044 if (lab->s_set && !lab->s_used) {
3045 curr_pos = lab->s_set_pos;
3046 /* %s unused in function %s */
3047 warning(192, lab->s_name, funcsym->s_name);
3048 } else if (!lab->s_set) {
3049 curr_pos = lab->s_use_pos;
3050 /* undefined label %s */
3051 warning(23, lab->s_name);
3052 }
3053 }
3054
3055 static void
3056 check_tag_usage(sym_t *sym)
3057 {
3058
3059 if (!incompl(sym->s_type))
3060 return;
3061
3062 /* always complain about incomplete tags declared inside blocks */
3063 if (!zflag || dcs->d_ctx != EXTERN)
3064 return;
3065
3066 curr_pos = sym->s_def_pos;
3067 switch (sym->s_type->t_tspec) {
3068 case STRUCT:
3069 /* struct %s never defined */
3070 warning(233, sym->s_name);
3071 break;
3072 case UNION:
3073 /* union %s never defined */
3074 warning(234, sym->s_name);
3075 break;
3076 case ENUM:
3077 /* enum %s never defined */
3078 warning(235, sym->s_name);
3079 break;
3080 default:
3081 LERROR("check_tag_usage()");
3082 }
3083 }
3084
3085 /*
3086 * Called after the entire translation unit has been parsed.
3087 * Changes tentative definitions into definitions.
3088 * Performs some tests on global symbols. Detected problems are:
3089 * - defined variables of incomplete type
3090 * - constant variables which are not initialized
3091 * - static symbols which are never used
3092 */
3093 void
3094 check_global_symbols(void)
3095 {
3096 sym_t *sym;
3097 pos_t cpos;
3098
3099 if (blklev != 0 || dcs->d_next != NULL)
3100 norecover();
3101
3102 cpos = curr_pos;
3103
3104 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3105 if (sym->s_blklev == -1)
3106 continue;
3107 if (sym->s_kind == FVFT) {
3108 check_global_variable(sym);
3109 } else if (sym->s_kind == FTAG) {
3110 check_tag_usage(sym);
3111 } else {
3112 lint_assert(sym->s_kind == FMEMBER);
3113 }
3114 }
3115
3116 curr_pos = cpos;
3117 }
3118
3119 static void
3120 check_global_variable(sym_t *sym)
3121 {
3122
3123 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
3124 return;
3125
3126 lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
3127
3128 check_global_variable_size(sym);
3129
3130 if (sym->s_scl == STATIC) {
3131 if (sym->s_type->t_tspec == FUNC) {
3132 if (sym->s_used && sym->s_def != DEF) {
3133 curr_pos = sym->s_use_pos;
3134 /* static func. called but not def... */
3135 error(225, sym->s_name);
3136 }
3137 }
3138 if (!sym->s_used) {
3139 curr_pos = sym->s_def_pos;
3140 if (sym->s_type->t_tspec == FUNC) {
3141 if (sym->s_def == DEF) {
3142 if (!sym->s_inline)
3143 /* static function %s unused */
3144 warning(236, sym->s_name);
3145 } else {
3146 /* static function %s declared but... */
3147 warning(290, sym->s_name);
3148 }
3149 } else if (!sym->s_set) {
3150 /* static variable %s unused */
3151 warning(226, sym->s_name);
3152 } else {
3153 /* static variable %s set but not used */
3154 warning(307, sym->s_name);
3155 }
3156 }
3157 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3158 curr_pos = sym->s_def_pos;
3159 /* const object %s should have initializer */
3160 warning(227, sym->s_name);
3161 }
3162 }
3163 }
3164
3165 static void
3166 check_global_variable_size(sym_t *sym)
3167 {
3168
3169 if (sym->s_def == TDEF) {
3170 if (sym->s_type->t_tspec == FUNC)
3171 /*
3172 * this can happen if an syntax error occurred
3173 * after a function declaration
3174 */
3175 return;
3176 curr_pos = sym->s_def_pos;
3177 if (length(sym->s_type, sym->s_name) == 0 &&
3178 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3179 /* empty array declaration: %s */
3180 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3181 warning(190, sym->s_name);
3182 } else {
3183 error(190, sym->s_name);
3184 }
3185 }
3186 }
3187 }
3188
3189 /*
3190 * Prints information about location of previous definition/declaration.
3191 */
3192 void
3193 print_previous_declaration(int msg, sym_t *psym)
3194 {
3195 pos_t cpos;
3196
3197 if (!rflag)
3198 return;
3199
3200 cpos = curr_pos;
3201 curr_pos = psym->s_def_pos;
3202 if (msg != -1) {
3203 message(msg);
3204 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3205 /* previous definition of %s */
3206 message(261, psym->s_name);
3207 } else {
3208 /* previous declaration of %s */
3209 message(260, psym->s_name);
3210 }
3211 curr_pos = cpos;
3212 }
3213