decl.c revision 1.93 1 /* $NetBSD: decl.c,v 1.93 2021/01/01 09:28:22 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.93 2021/01/01 09:28:22 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 lint_assert(0);
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 lint_assert(0);
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 {
1536 lint_assert(sc == EXTERN);
1537 sym->s_def = DECL;
1538 }
1539 break;
1540 case PARG:
1541 sym->s_arg = 1;
1542 /* FALLTHROUGH */
1543 case ARG:
1544 if ((sc = dcs->d_scl) == NOSCL) {
1545 sc = AUTO;
1546 } else {
1547 lint_assert(sc == REG);
1548 sym->s_reg = 1;
1549 sc = AUTO;
1550 }
1551 sym->s_def = DEF;
1552 break;
1553 case AUTO:
1554 if ((sc = dcs->d_scl) == NOSCL) {
1555 /*
1556 * XXX somewhat ugly because we dont know whether
1557 * this is AUTO or EXTERN (functions). If we are
1558 * wrong it must be corrected in decl1loc(), where
1559 * we have the necessary type information.
1560 */
1561 sc = AUTO;
1562 sym->s_def = DEF;
1563 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1564 sym->s_def = DEF;
1565 } else if (sc == REG) {
1566 sym->s_reg = 1;
1567 sc = AUTO;
1568 sym->s_def = DEF;
1569 } else {
1570 lint_assert(sc == EXTERN);
1571 sym->s_def = DECL;
1572 }
1573 break;
1574 default:
1575 lint_assert(0);
1576 }
1577 sym->s_scl = sc;
1578
1579 sym->s_type = dcs->d_type;
1580
1581 dcs->d_fpsyms = NULL;
1582
1583 return sym;
1584 }
1585
1586 /*
1587 * Process a name in the list of formal params in an old style function
1588 * definition.
1589 */
1590 sym_t *
1591 old_style_function_name(sym_t *sym)
1592 {
1593
1594 if (sym->s_scl != NOSCL) {
1595 if (blklev == sym->s_blklev) {
1596 /* redeclaration of formal parameter %s */
1597 error(21, sym->s_name);
1598 lint_assert(sym->s_defarg);
1599 }
1600 sym = pushdown(sym);
1601 }
1602 sym->s_type = gettyp(INT);
1603 sym->s_scl = AUTO;
1604 sym->s_def = DEF;
1605 sym->s_defarg = sym->s_arg = 1;
1606 return sym;
1607 }
1608
1609 /*
1610 * Create the type of a tag.
1611 *
1612 * tag points to the symbol table entry of the tag
1613 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1614 * decl is 1 if the type of the tag will be completed in this declaration
1615 * (the following token is T_LBRACE)
1616 * semi is 1 if the following token is T_SEMI
1617 */
1618 type_t *
1619 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1620 {
1621 scl_t scl = NOSCL;
1622 type_t *tp;
1623
1624 if (kind == STRUCT) {
1625 scl = STRTAG;
1626 } else if (kind == UNION) {
1627 scl = UNIONTAG;
1628 } else {
1629 lint_assert(kind == ENUM);
1630 scl = ENUMTAG;
1631 }
1632
1633 if (tag != NULL) {
1634 if (tag->s_scl != NOSCL) {
1635 tag = newtag(tag, scl, decl, semi);
1636 } else {
1637 /* a new tag, no empty declaration */
1638 dcs->d_next->d_nedecl = 1;
1639 if (scl == ENUMTAG && !decl) {
1640 if (!tflag && (sflag || pflag))
1641 /* forward reference to enum type */
1642 warning(42);
1643 }
1644 }
1645 if (tag->s_scl == NOSCL) {
1646 tag->s_scl = scl;
1647 tag->s_type = tp = getblk(sizeof (type_t));
1648 tp->t_ispacked = dcs->d_ispacked;
1649 } else {
1650 tp = tag->s_type;
1651 }
1652 } else {
1653 tag = getblk(sizeof (sym_t));
1654 tag->s_name = unnamed;
1655 UNIQUE_CURR_POS(tag->s_def_pos);
1656 tag->s_kind = FTAG;
1657 tag->s_scl = scl;
1658 tag->s_blklev = -1;
1659 tag->s_type = tp = getblk(sizeof (type_t));
1660 tp->t_ispacked = dcs->d_ispacked;
1661 dcs->d_next->d_nedecl = 1;
1662 }
1663
1664 if (tp->t_tspec == NOTSPEC) {
1665 tp->t_tspec = kind;
1666 if (kind != ENUM) {
1667 tp->t_str = getblk(sizeof (str_t));
1668 tp->t_str->align = CHAR_BIT;
1669 tp->t_str->stag = tag;
1670 } else {
1671 tp->t_isenum = 1;
1672 tp->t_enum = getblk(sizeof(*tp->t_enum));
1673 tp->t_enum->etag = tag;
1674 }
1675 setcomplete(tp, 0);
1676 }
1677 return tp;
1678 }
1679
1680 /*
1681 * Checks all possible cases of tag redeclarations.
1682 * decl is 1 if T_LBRACE follows
1683 * semi is 1 if T_SEMI follows
1684 */
1685 static sym_t *
1686 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1687 {
1688
1689 if (tag->s_blklev < blklev) {
1690 if (semi) {
1691 /* "struct a;" */
1692 if (!tflag) {
1693 if (!sflag)
1694 /* decl. introduces new type ... */
1695 warning(44, storage_class_name(scl),
1696 tag->s_name);
1697 tag = pushdown(tag);
1698 } else if (tag->s_scl != scl) {
1699 /* base type is really '%s %s' */
1700 warning(45, storage_class_name(tag->s_scl),
1701 tag->s_name);
1702 }
1703 dcs->d_next->d_nedecl = 1;
1704 } else if (decl) {
1705 /* "struct a { ... } " */
1706 if (hflag)
1707 /* redefinition hides earlier one: %s */
1708 warning(43, tag->s_name);
1709 tag = pushdown(tag);
1710 dcs->d_next->d_nedecl = 1;
1711 } else if (tag->s_scl != scl) {
1712 /* base type is really '%s %s' */
1713 warning(45, storage_class_name(tag->s_scl),
1714 tag->s_name);
1715 /* declaration introduces new type in ANSI C: %s %s */
1716 if (!sflag) {
1717 warning(44, storage_class_name(scl),
1718 tag->s_name);
1719 }
1720 tag = pushdown(tag);
1721 dcs->d_next->d_nedecl = 1;
1722 }
1723 } else {
1724 if (tag->s_scl != scl) {
1725 /* (%s) tag redeclared */
1726 error(46, storage_class_name(tag->s_scl));
1727 print_previous_declaration(-1, tag);
1728 tag = pushdown(tag);
1729 dcs->d_next->d_nedecl = 1;
1730 } else if (decl && !incompl(tag->s_type)) {
1731 /* (%s) tag redeclared */
1732 error(46, storage_class_name(tag->s_scl));
1733 print_previous_declaration(-1, tag);
1734 tag = pushdown(tag);
1735 dcs->d_next->d_nedecl = 1;
1736 } else if (semi || decl) {
1737 dcs->d_next->d_nedecl = 1;
1738 }
1739 }
1740 return tag;
1741 }
1742
1743 const char *
1744 storage_class_name(scl_t sc)
1745 {
1746 const char *s;
1747
1748 switch (sc) {
1749 case EXTERN: s = "extern"; break;
1750 case STATIC: s = "static"; break;
1751 case AUTO: s = "auto"; break;
1752 case REG: s = "register"; break;
1753 case TYPEDEF: s = "typedef"; break;
1754 case STRTAG: s = "struct"; break;
1755 case UNIONTAG: s = "union"; break;
1756 case ENUMTAG: s = "enum"; break;
1757 default: lint_assert(0);
1758 }
1759 return s;
1760 }
1761
1762 /*
1763 * tp points to the type of the tag, fmem to the list of members.
1764 */
1765 type_t *
1766 complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1767 {
1768 tspec_t t;
1769 str_t *sp;
1770 int n;
1771 sym_t *mem;
1772
1773 setcomplete(tp, 1);
1774
1775 t = tp->t_tspec;
1776 align(dcs->d_stralign, 0);
1777 sp = tp->t_str;
1778 sp->align = dcs->d_stralign;
1779 sp->memb = fmem;
1780 if (tp->t_ispacked)
1781 setpackedsize(tp);
1782 else
1783 sp->size = dcs->d_offset;
1784
1785 if (sp->size == 0) {
1786 /* zero sized %s */
1787 (void)c99ism(47, ttab[t].tt_name);
1788 }
1789
1790 n = 0;
1791 for (mem = fmem; mem != NULL; mem = mem->s_next) {
1792 /* bind anonymous members to the structure */
1793 if (mem->s_styp == NULL) {
1794 mem->s_styp = sp;
1795 if (mem->s_type->t_isfield) {
1796 sp->size += bitfieldsize(&mem);
1797 if (mem == NULL)
1798 break;
1799 }
1800 sp->size += tsize(mem->s_type);
1801 }
1802 if (mem->s_name != unnamed)
1803 n++;
1804 }
1805
1806 if (n == 0 && sp->size != 0) {
1807 /* %s has no named members */
1808 warning(65, t == STRUCT ? "structure" : "union");
1809 }
1810 return tp;
1811 }
1812
1813 type_t *
1814 complete_tag_enum(type_t *tp, sym_t *fmem)
1815 {
1816
1817 setcomplete(tp, 1);
1818 tp->t_enum->elem = fmem;
1819 return tp;
1820 }
1821
1822 /*
1823 * Processes the name of an enumerator in an enum declaration.
1824 *
1825 * sym points to the enumerator
1826 * val is the value of the enumerator
1827 * impl is 1 if the value of the enumerator was not explicitly specified.
1828 */
1829 sym_t *
1830 ename(sym_t *sym, int val, int impl)
1831 {
1832
1833 if (sym->s_scl) {
1834 if (sym->s_blklev == blklev) {
1835 /* no hflag, because this is illegal!!! */
1836 if (sym->s_arg) {
1837 /* enumeration constant hides parameter: %s */
1838 warning(57, sym->s_name);
1839 } else {
1840 /* redeclaration of %s */
1841 error(27, sym->s_name);
1842 /*
1843 * inside blocks it should not be too
1844 * complicated to find the position of the
1845 * previous declaration
1846 */
1847 if (blklev == 0)
1848 print_previous_declaration(-1, sym);
1849 }
1850 } else {
1851 if (hflag)
1852 /* redefinition hides earlier one: %s */
1853 warning(43, sym->s_name);
1854 }
1855 sym = pushdown(sym);
1856 }
1857 sym->s_scl = ENUMCON;
1858 sym->s_type = dcs->d_tagtyp;
1859 sym->s_value.v_tspec = INT;
1860 sym->s_value.v_quad = val;
1861 if (impl && val - 1 == TARG_INT_MAX) {
1862 /* overflow in enumeration values: %s */
1863 warning(48, sym->s_name);
1864 }
1865 enumval = val + 1;
1866 return sym;
1867 }
1868
1869 /*
1870 * Process a single external declarator.
1871 */
1872 void
1873 decl1ext(sym_t *dsym, int initflg)
1874 {
1875 int dowarn, rval, redec;
1876 sym_t *rdsym;
1877
1878 check_function_definition(dsym, 1);
1879
1880 check_type(dsym);
1881
1882 if (initflg && !(initerr = check_init(dsym)))
1883 dsym->s_def = DEF;
1884
1885 /*
1886 * Declarations of functions are marked as "tentative" in
1887 * declarator_name(). This is wrong because there are no
1888 * tentative function definitions.
1889 */
1890 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1891 dsym->s_def = DECL;
1892
1893 if (dcs->d_inline) {
1894 if (dsym->s_type->t_tspec == FUNC) {
1895 dsym->s_inline = 1;
1896 } else {
1897 /* variable declared inline: %s */
1898 warning(268, dsym->s_name);
1899 }
1900 }
1901
1902 /* Write the declaration into the output file */
1903 if (plibflg && llibflg &&
1904 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1905 /*
1906 * With both LINTLIBRARY and PROTOLIB the prototyp is
1907 * written as a function definition to the output file.
1908 */
1909 rval = dsym->s_type->t_subt->t_tspec != VOID;
1910 outfdef(dsym, &dsym->s_def_pos, rval, 0, NULL);
1911 } else {
1912 outsym(dsym, dsym->s_scl, dsym->s_def);
1913 }
1914
1915 if ((rdsym = dcs->d_rdcsym) != NULL) {
1916
1917 /*
1918 * If the old symbol stems from an old style function
1919 * definition, we have remembered the params in rdsmy->s_args
1920 * and compare them with the params of the prototype.
1921 */
1922 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1923 redec = check_old_style_definition(rdsym, dsym);
1924 } else {
1925 redec = 0;
1926 }
1927
1928 if (!redec && !check_redeclaration(dsym, (dowarn = 0, &dowarn))) {
1929
1930 if (dowarn) {
1931 /* redeclaration of %s */
1932 (*(sflag ? error : warning))(27, dsym->s_name);
1933 print_previous_declaration(-1, rdsym);
1934 }
1935
1936 /*
1937 * Take over the remembered params if the new symbol
1938 * is not a prototype.
1939 */
1940 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1941 dsym->s_osdef = rdsym->s_osdef;
1942 dsym->s_args = rdsym->s_args;
1943 dsym->s_def_pos = rdsym->s_def_pos;
1944 }
1945
1946 /*
1947 * Remember the position of the declaration if the
1948 * old symbol was a prototype and the new is not.
1949 * Also remember the position if the old symbol
1950 * was defined and the new is not.
1951 */
1952 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1953 dsym->s_def_pos = rdsym->s_def_pos;
1954 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1955 dsym->s_def_pos = rdsym->s_def_pos;
1956 }
1957
1958 /*
1959 * Copy usage information of the name into the new
1960 * symbol.
1961 */
1962 copy_usage_info(dsym, rdsym);
1963
1964 /* Once a name is defined, it remains defined. */
1965 if (rdsym->s_def == DEF)
1966 dsym->s_def = DEF;
1967
1968 /* once a function is inline, it remains inline */
1969 if (rdsym->s_inline)
1970 dsym->s_inline = 1;
1971
1972 complete_type(dsym, rdsym);
1973
1974 }
1975
1976 rmsym(rdsym);
1977 }
1978
1979 if (dsym->s_scl == TYPEDEF) {
1980 dsym->s_type = duptyp(dsym->s_type);
1981 dsym->s_type->t_typedef = 1;
1982 settdsym(dsym->s_type, dsym);
1983 }
1984
1985 }
1986
1987 /*
1988 * Copies information about usage into a new symbol table entry of
1989 * the same symbol.
1990 */
1991 void
1992 copy_usage_info(sym_t *sym, sym_t *rdsym)
1993 {
1994
1995 sym->s_set_pos = rdsym->s_set_pos;
1996 sym->s_use_pos = rdsym->s_use_pos;
1997 sym->s_set = rdsym->s_set;
1998 sym->s_used = rdsym->s_used;
1999 }
2000
2001 /*
2002 * Prints an error and returns 1 if a symbol is redeclared/redefined.
2003 * Otherwise returns 0 and, in some cases of minor problems, prints
2004 * a warning.
2005 */
2006 int
2007 check_redeclaration(sym_t *dsym, int *dowarn)
2008 {
2009 sym_t *rsym;
2010
2011 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
2012 /* redeclaration of %s */
2013 error(27, dsym->s_name);
2014 print_previous_declaration(-1, rsym);
2015 return 1;
2016 }
2017 if (rsym->s_scl == TYPEDEF) {
2018 /* typedef redeclared: %s */
2019 error(89, dsym->s_name);
2020 print_previous_declaration(-1, rsym);
2021 return 1;
2022 }
2023 if (dsym->s_scl == TYPEDEF) {
2024 /* redeclaration of %s */
2025 error(27, dsym->s_name);
2026 print_previous_declaration(-1, rsym);
2027 return 1;
2028 }
2029 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2030 /* redefinition of %s */
2031 error(28, dsym->s_name);
2032 print_previous_declaration(-1, rsym);
2033 return(1);
2034 }
2035 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) {
2036 /* redeclaration of %s */
2037 error(27, dsym->s_name);
2038 print_previous_declaration(-1, rsym);
2039 return(1);
2040 }
2041 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2042 return(0);
2043 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2044 return(0);
2045 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2046 return(0);
2047 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2048 /*
2049 * All cases except "int a = 1; static int a;" are caught
2050 * above with or without a warning
2051 */
2052 /* redeclaration of %s */
2053 error(27, dsym->s_name);
2054 print_previous_declaration(-1, rsym);
2055 return(1);
2056 }
2057 if (rsym->s_scl == EXTERN) {
2058 /* previously declared extern, becomes static: %s */
2059 warning(29, dsym->s_name);
2060 print_previous_declaration(-1, rsym);
2061 return(0);
2062 }
2063 /*
2064 * Now it's one of:
2065 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2066 */
2067 /* redeclaration of %s; ANSI C requires "static" */
2068 if (sflag) {
2069 warning(30, dsym->s_name);
2070 print_previous_declaration(-1, rsym);
2071 }
2072 dsym->s_scl = STATIC;
2073 return 0;
2074 }
2075
2076 static int
2077 chkqual(type_t *tp1, type_t *tp2, int ignqual)
2078 {
2079 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2080 return 0;
2081
2082 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2083 return 0;
2084
2085 return 1;
2086 }
2087
2088 int
2089 eqptrtype(type_t *tp1, type_t *tp2, int ignqual)
2090 {
2091 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2092 return 0;
2093
2094 if (!chkqual(tp1, tp2, ignqual))
2095 return 0;
2096
2097 return 1;
2098 }
2099
2100
2101 /*
2102 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
2103 *
2104 * ignqual ignore qualifiers of type; used for function params
2105 * promot promote left type; used for comparison of params of
2106 * old style function definitions with params of prototypes.
2107 * *dowarn set to 1 if an old style function declaration is not
2108 * compatible with a prototype
2109 */
2110 int
2111 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn)
2112 {
2113 tspec_t t;
2114
2115 while (tp1 != NULL && tp2 != NULL) {
2116
2117 t = tp1->t_tspec;
2118 if (promot) {
2119 if (t == FLOAT) {
2120 t = DOUBLE;
2121 } else if (t == CHAR || t == SCHAR) {
2122 t = INT;
2123 } else if (t == UCHAR) {
2124 t = tflag ? UINT : INT;
2125 } else if (t == SHORT) {
2126 t = INT;
2127 } else if (t == USHORT) {
2128 /* CONSTCOND */
2129 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
2130 }
2131 }
2132
2133 if (t != tp2->t_tspec)
2134 return 0;
2135
2136 if (!chkqual(tp1, tp2, ignqual))
2137 return 0;
2138
2139 if (t == STRUCT || t == UNION)
2140 return tp1->t_str == tp2->t_str;
2141
2142 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2143 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2144 return 0;
2145 }
2146
2147 /* dont check prototypes for traditional */
2148 if (t == FUNC && !tflag) {
2149 if (tp1->t_proto && tp2->t_proto) {
2150 if (!eqargs(tp1, tp2, dowarn))
2151 return 0;
2152 } else if (tp1->t_proto) {
2153 if (!mnoarg(tp1, dowarn))
2154 return 0;
2155 } else if (tp2->t_proto) {
2156 if (!mnoarg(tp2, dowarn))
2157 return 0;
2158 }
2159 }
2160
2161 tp1 = tp1->t_subt;
2162 tp2 = tp2->t_subt;
2163 ignqual = promot = 0;
2164
2165 }
2166
2167 return tp1 == tp2;
2168 }
2169
2170 /*
2171 * Compares the parameter types of two prototypes.
2172 */
2173 static int
2174 eqargs(type_t *tp1, type_t *tp2, int *dowarn)
2175 {
2176 sym_t *a1, *a2;
2177
2178 if (tp1->t_vararg != tp2->t_vararg)
2179 return 0;
2180
2181 a1 = tp1->t_args;
2182 a2 = tp2->t_args;
2183
2184 while (a1 != NULL && a2 != NULL) {
2185
2186 if (eqtype(a1->s_type, a2->s_type, 1, 0, dowarn) == 0)
2187 return 0;
2188
2189 a1 = a1->s_next;
2190 a2 = a2->s_next;
2191
2192 }
2193
2194 return a1 == a2;
2195 }
2196
2197 /*
2198 * mnoarg() (matches functions with no argument type information)
2199 * returns 1 if all parameters of a prototype are compatible with
2200 * an old style function declaration.
2201 * This is the case if the following conditions are met:
2202 * 1. the prototype must have a fixed number of parameters
2203 * 2. no parameter is of type float
2204 * 3. no parameter is converted to another type if integer promotion
2205 * is applied on it
2206 */
2207 static int
2208 mnoarg(type_t *tp, int *dowarn)
2209 {
2210 sym_t *arg;
2211 tspec_t t;
2212
2213 if (tp->t_vararg) {
2214 if (dowarn != NULL)
2215 *dowarn = 1;
2216 }
2217 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2218 if ((t = arg->s_type->t_tspec) == FLOAT ||
2219 t == CHAR || t == SCHAR || t == UCHAR ||
2220 t == SHORT || t == USHORT) {
2221 if (dowarn != NULL)
2222 *dowarn = 1;
2223 }
2224 }
2225 return 1;
2226 }
2227
2228 /*
2229 * Compares a prototype declaration with the remembered arguments of
2230 * a previous old style function definition.
2231 */
2232 static int
2233 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2234 {
2235 sym_t *args, *pargs, *arg, *parg;
2236 int narg, nparg, n;
2237 int dowarn, msg;
2238
2239 args = rdsym->s_args;
2240 pargs = dsym->s_type->t_args;
2241
2242 msg = 0;
2243
2244 narg = nparg = 0;
2245 for (arg = args; arg != NULL; arg = arg->s_next)
2246 narg++;
2247 for (parg = pargs; parg != NULL; parg = parg->s_next)
2248 nparg++;
2249 if (narg != nparg) {
2250 /* prototype does not match old-style definition */
2251 error(63);
2252 msg = 1;
2253 goto end;
2254 }
2255
2256 arg = args;
2257 parg = pargs;
2258 n = 1;
2259 while (narg--) {
2260 dowarn = 0;
2261 /*
2262 * If it does not match due to promotion and sflag is
2263 * not set we print only a warning.
2264 */
2265 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &dowarn) || dowarn) {
2266 /* prototype does not match old style defn., arg #%d */
2267 error(299, n);
2268 msg = 1;
2269 }
2270 arg = arg->s_next;
2271 parg = parg->s_next;
2272 n++;
2273 }
2274
2275 end:
2276 if (msg)
2277 /* old style definition */
2278 print_previous_declaration(300, rdsym);
2279
2280 return msg;
2281 }
2282
2283 /*
2284 * Completes a type by copying the dimension and prototype information
2285 * from a second compatible type.
2286 *
2287 * Following lines are legal:
2288 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2289 * "typedef ft(); ft f; f(int); ft g; g(long);"
2290 * This means that, if a type is completed, the type structure must
2291 * be duplicated.
2292 */
2293 void
2294 complete_type(sym_t *dsym, sym_t *ssym)
2295 {
2296 type_t **dstp, *src;
2297 type_t *dst;
2298
2299 dstp = &dsym->s_type;
2300 src = ssym->s_type;
2301
2302 while ((dst = *dstp) != NULL) {
2303 lint_assert(src != NULL);
2304 lint_assert(dst->t_tspec == src->t_tspec);
2305 if (dst->t_tspec == ARRAY) {
2306 if (dst->t_dim == 0 && src->t_dim != 0) {
2307 *dstp = dst = duptyp(dst);
2308 dst->t_dim = src->t_dim;
2309 setcomplete(dst, 1);
2310 }
2311 } else if (dst->t_tspec == FUNC) {
2312 if (!dst->t_proto && src->t_proto) {
2313 *dstp = dst = duptyp(dst);
2314 dst->t_proto = 1;
2315 dst->t_args = src->t_args;
2316 }
2317 }
2318 dstp = &dst->t_subt;
2319 src = src->t_subt;
2320 }
2321 }
2322
2323 /*
2324 * Completes the declaration of a single argument.
2325 */
2326 sym_t *
2327 decl1arg(sym_t *sym, int initflg)
2328 {
2329 tspec_t t;
2330
2331 check_function_definition(sym, 1);
2332
2333 check_type(sym);
2334
2335 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2336 /* redeclaration of formal parameter %s */
2337 error(237, sym->s_name);
2338 rmsym(dcs->d_rdcsym);
2339 sym->s_arg = 1;
2340 }
2341
2342 if (!sym->s_arg) {
2343 /* declared argument %s is missing */
2344 error(53, sym->s_name);
2345 sym->s_arg = 1;
2346 }
2347
2348 if (initflg) {
2349 /* cannot initialize parameter: %s */
2350 error(52, sym->s_name);
2351 initerr = 1;
2352 }
2353
2354 if ((t = sym->s_type->t_tspec) == ARRAY) {
2355 sym->s_type = incref(sym->s_type->t_subt, PTR);
2356 } else if (t == FUNC) {
2357 if (tflag)
2358 /* a function is declared as an argument: %s */
2359 warning(50, sym->s_name);
2360 sym->s_type = incref(sym->s_type, PTR);
2361 } else if (t == FLOAT) {
2362 if (tflag)
2363 sym->s_type = gettyp(DOUBLE);
2364 }
2365
2366 if (dcs->d_inline)
2367 /* argument declared inline: %s */
2368 warning(269, sym->s_name);
2369
2370 /*
2371 * Arguments must have complete types. lengths() prints the needed
2372 * error messages (null dimension is impossible because arrays are
2373 * converted to pointers).
2374 */
2375 if (sym->s_type->t_tspec != VOID)
2376 (void)length(sym->s_type, sym->s_name);
2377
2378 sym->s_used = dcs->d_used;
2379 mark_as_set(sym);
2380
2381 return sym;
2382 }
2383
2384 void
2385 check_func_lint_directives(void)
2386 {
2387 sym_t *arg;
2388 int narg, n;
2389 tspec_t t;
2390
2391 /* check for illegal combinations of lint directives */
2392 if (prflstrg != -1 && scflstrg != -1) {
2393 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2394 warning(289);
2395 prflstrg = scflstrg = -1;
2396 }
2397 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2398 /* dubious use of ** VARARGS ** with ** %s ** */
2399 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2400 nvararg = -1;
2401 }
2402
2403 /*
2404 * check if the argument of a lint directive is compatible with the
2405 * number of arguments.
2406 */
2407 narg = 0;
2408 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_next)
2409 narg++;
2410 if (nargusg > narg) {
2411 /* argument number mismatch with directive: ** %s ** */
2412 warning(283, "ARGSUSED");
2413 nargusg = 0;
2414 }
2415 if (nvararg > narg) {
2416 /* argument number mismatch with directive: ** %s ** */
2417 warning(283, "VARARGS");
2418 nvararg = 0;
2419 }
2420 if (prflstrg > narg) {
2421 /* argument number mismatch with directive: ** %s ** */
2422 warning(283, "PRINTFLIKE");
2423 prflstrg = -1;
2424 } else if (prflstrg == 0) {
2425 prflstrg = -1;
2426 }
2427 if (scflstrg > narg) {
2428 /* argument number mismatch with directive: ** %s ** */
2429 warning(283, "SCANFLIKE");
2430 scflstrg = -1;
2431 } else if (scflstrg == 0) {
2432 scflstrg = -1;
2433 }
2434 if (prflstrg != -1 || scflstrg != -1) {
2435 narg = prflstrg != -1 ? prflstrg : scflstrg;
2436 arg = dcs->d_fargs;
2437 for (n = 1; n < narg; n++)
2438 arg = arg->s_next;
2439 if (arg->s_type->t_tspec != PTR ||
2440 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2441 t != UCHAR && t != SCHAR)) {
2442 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2443 warning(293, narg);
2444 prflstrg = scflstrg = -1;
2445 }
2446 }
2447 }
2448
2449 /*
2450 * Warn about arguments in old style function definitions that default to int.
2451 * Check that an old style function definition is compatible to a previous
2452 * prototype.
2453 */
2454 void
2455 check_func_old_style_arguments(void)
2456 {
2457 sym_t *args, *arg, *pargs, *parg;
2458 int narg, nparg, msg;
2459
2460 args = funcsym->s_args;
2461 pargs = funcsym->s_type->t_args;
2462
2463 /*
2464 * print a warning for each argument of an old style function
2465 * definition which defaults to int
2466 */
2467 for (arg = args; arg != NULL; arg = arg->s_next) {
2468 if (arg->s_defarg) {
2469 /* argument type defaults to 'int': %s */
2470 warning(32, arg->s_name);
2471 arg->s_defarg = 0;
2472 mark_as_set(arg);
2473 }
2474 }
2475
2476 /*
2477 * If this is an old style function definition and a prototype
2478 * exists, compare the types of arguments.
2479 */
2480 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2481 /*
2482 * If the number of arguments does not match, we need not
2483 * continue.
2484 */
2485 narg = nparg = 0;
2486 msg = 0;
2487 for (parg = pargs; parg != NULL; parg = parg->s_next)
2488 nparg++;
2489 for (arg = args; arg != NULL; arg = arg->s_next)
2490 narg++;
2491 if (narg != nparg) {
2492 /* parameter mismatch: %d declared, %d defined */
2493 error(51, nparg, narg);
2494 msg = 1;
2495 } else {
2496 parg = pargs;
2497 arg = args;
2498 while (narg--) {
2499 msg |= check_prototype_declaration(arg, parg);
2500 parg = parg->s_next;
2501 arg = arg->s_next;
2502 }
2503 }
2504 if (msg)
2505 /* prototype declaration */
2506 print_previous_declaration(285, dcs->d_rdcsym);
2507
2508 /* from now on the prototype is valid */
2509 funcsym->s_osdef = 0;
2510 funcsym->s_args = NULL;
2511 }
2512 }
2513
2514 /*
2515 * Checks compatibility of an old style function definition with a previous
2516 * prototype declaration.
2517 * Returns 1 if the position of the previous declaration should be reported.
2518 */
2519 static int
2520 check_prototype_declaration(sym_t *arg, sym_t *parg)
2521 {
2522 type_t *tp, *ptp;
2523 int dowarn, msg;
2524
2525 tp = arg->s_type;
2526 ptp = parg->s_type;
2527
2528 msg = 0;
2529 dowarn = 0;
2530
2531 if (!eqtype(tp, ptp, 1, 1, &dowarn)) {
2532 if (eqtype(tp, ptp, 1, 0, &dowarn)) {
2533 /* type does not match prototype: %s */
2534 msg = gnuism(58, arg->s_name);
2535 } else {
2536 /* type does not match prototype: %s */
2537 error(58, arg->s_name);
2538 msg = 1;
2539 }
2540 } else if (dowarn) {
2541 /* type does not match prototype: %s */
2542 (*(sflag ? error : warning))(58, arg->s_name);
2543 msg = 1;
2544 }
2545
2546 return msg;
2547 }
2548
2549 /*
2550 * Completes a single local declaration/definition.
2551 */
2552 void
2553 decl1loc(sym_t *dsym, int initflg)
2554 {
2555
2556 /* Correct a mistake done in declarator_name(). */
2557 if (dsym->s_type->t_tspec == FUNC) {
2558 dsym->s_def = DECL;
2559 if (dcs->d_scl == NOSCL)
2560 dsym->s_scl = EXTERN;
2561 }
2562
2563 if (dsym->s_type->t_tspec == FUNC) {
2564 if (dsym->s_scl == STATIC) {
2565 /* dubious static function at block level: %s */
2566 warning(93, dsym->s_name);
2567 dsym->s_scl = EXTERN;
2568 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2569 /* function has illegal storage class: %s */
2570 error(94, dsym->s_name);
2571 dsym->s_scl = EXTERN;
2572 }
2573 }
2574
2575 /*
2576 * functions may be declared inline at local scope, although
2577 * this has no effect for a later definition of the same
2578 * function.
2579 * XXX it should have an effect if tflag is set. this would
2580 * also be the way gcc behaves.
2581 */
2582 if (dcs->d_inline) {
2583 if (dsym->s_type->t_tspec == FUNC) {
2584 dsym->s_inline = 1;
2585 } else {
2586 /* variable declared inline: %s */
2587 warning(268, dsym->s_name);
2588 }
2589 }
2590
2591 check_function_definition(dsym, 1);
2592
2593 check_type(dsym);
2594
2595 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2596 ledecl(dsym);
2597
2598 if (dsym->s_scl == EXTERN) {
2599 /*
2600 * XXX if the static variable at level 0 is only defined
2601 * later, checking will be possible.
2602 */
2603 if (dsym->s_ext_sym == NULL) {
2604 outsym(dsym, EXTERN, dsym->s_def);
2605 } else {
2606 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2607 }
2608 }
2609
2610 if (dcs->d_rdcsym != NULL) {
2611
2612 if (dcs->d_rdcsym->s_blklev == 0) {
2613
2614 switch (dsym->s_scl) {
2615 case AUTO:
2616 /* automatic hides external declaration: %s */
2617 if (hflag)
2618 warning(86, dsym->s_name);
2619 break;
2620 case STATIC:
2621 /* static hides external declaration: %s */
2622 if (hflag)
2623 warning(87, dsym->s_name);
2624 break;
2625 case TYPEDEF:
2626 /* typedef hides external declaration: %s */
2627 if (hflag)
2628 warning(88, dsym->s_name);
2629 break;
2630 case EXTERN:
2631 /*
2632 * Warnings and errors are printed in ledecl()
2633 */
2634 break;
2635 default:
2636 lint_assert(0);
2637 }
2638
2639 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2640
2641 /* no hflag, because it's illegal! */
2642 if (dcs->d_rdcsym->s_arg) {
2643 /*
2644 * if !tflag, a "redeclaration of %s" error
2645 * is produced below
2646 */
2647 if (tflag) {
2648 if (hflag)
2649 /* decl. hides parameter: %s */
2650 warning(91, dsym->s_name);
2651 rmsym(dcs->d_rdcsym);
2652 }
2653 }
2654
2655 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2656
2657 if (hflag)
2658 /* declaration hides earlier one: %s */
2659 warning(95, dsym->s_name);
2660
2661 }
2662
2663 if (dcs->d_rdcsym->s_blklev == blklev) {
2664
2665 /* redeclaration of %s */
2666 error(27, dsym->s_name);
2667 rmsym(dcs->d_rdcsym);
2668
2669 }
2670
2671 }
2672
2673 if (initflg && !(initerr = check_init(dsym))) {
2674 dsym->s_def = DEF;
2675 mark_as_set(dsym);
2676 }
2677
2678 if (dsym->s_scl == TYPEDEF) {
2679 dsym->s_type = duptyp(dsym->s_type);
2680 dsym->s_type->t_typedef = 1;
2681 settdsym(dsym->s_type, dsym);
2682 }
2683
2684 /*
2685 * Before we can check the size we must wait for a initialization
2686 * which may follow.
2687 */
2688 }
2689
2690 /*
2691 * Processes (re)declarations of external symbols inside blocks.
2692 */
2693 static void
2694 ledecl(sym_t *dsym)
2695 {
2696 int eqt, dowarn;
2697 sym_t *esym;
2698
2699 /* look for a symbol with the same name */
2700 esym = dcs->d_rdcsym;
2701 while (esym != NULL && esym->s_blklev != 0) {
2702 while ((esym = esym->s_link) != NULL) {
2703 if (esym->s_kind != FVFT)
2704 continue;
2705 if (strcmp(dsym->s_name, esym->s_name) == 0)
2706 break;
2707 }
2708 }
2709 if (esym == NULL)
2710 return;
2711 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2712 /* gcc accepts this without a warning, pcc prints an error. */
2713 /* redeclaration of %s */
2714 warning(27, dsym->s_name);
2715 print_previous_declaration(-1, esym);
2716 return;
2717 }
2718
2719 dowarn = 0;
2720 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &dowarn);
2721
2722 if (!eqt || dowarn) {
2723 if (esym->s_scl == EXTERN) {
2724 /* inconsistent redeclaration of extern: %s */
2725 warning(90, dsym->s_name);
2726 print_previous_declaration(-1, esym);
2727 } else {
2728 /* inconsistent redeclaration of static: %s */
2729 warning(92, dsym->s_name);
2730 print_previous_declaration(-1, esym);
2731 }
2732 }
2733
2734 if (eqt) {
2735 /*
2736 * Remember the external symbol so we can update usage
2737 * information at the end of the block.
2738 */
2739 dsym->s_ext_sym = esym;
2740 }
2741 }
2742
2743 /*
2744 * Print an error or a warning if the symbol cannot be initialized due
2745 * to type/storage class. Return 1 if an error has been detected.
2746 */
2747 static int
2748 check_init(sym_t *sym)
2749 {
2750 int erred;
2751
2752 erred = 0;
2753
2754 if (sym->s_type->t_tspec == FUNC) {
2755 /* cannot initialize function: %s */
2756 error(24, sym->s_name);
2757 erred = 1;
2758 } else if (sym->s_scl == TYPEDEF) {
2759 /* cannot initialize typedef: %s */
2760 error(25, sym->s_name);
2761 erred = 1;
2762 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2763 /* cannot initialize "extern" declaration: %s */
2764 if (dcs->d_ctx == EXTERN) {
2765 warning(26, sym->s_name);
2766 } else {
2767 error(26, sym->s_name);
2768 erred = 1;
2769 }
2770 }
2771
2772 return erred;
2773 }
2774
2775 /*
2776 * Create a symbol for an abstract declaration.
2777 */
2778 sym_t *
2779 abstract_name(void)
2780 {
2781 sym_t *sym;
2782
2783 lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PARG);
2784
2785 sym = getblk(sizeof (sym_t));
2786
2787 sym->s_name = unnamed;
2788 sym->s_def = DEF;
2789 sym->s_scl = ABSTRACT;
2790 sym->s_blklev = -1;
2791
2792 if (dcs->d_ctx == PARG)
2793 sym->s_arg = 1;
2794
2795 sym->s_type = dcs->d_type;
2796 dcs->d_rdcsym = NULL;
2797 dcs->d_vararg = 0;
2798
2799 return sym;
2800 }
2801
2802 /*
2803 * Removes anything which has nothing to do on global level.
2804 */
2805 void
2806 global_clean_up(void)
2807 {
2808
2809 while (dcs->d_next != NULL)
2810 popdecl();
2811
2812 cleanup();
2813 blklev = 0;
2814 mblklev = 0;
2815
2816 /*
2817 * remove all information about pending lint directives without
2818 * warnings.
2819 */
2820 global_clean_up_decl(1);
2821 }
2822
2823 /*
2824 * Process an abstract type declaration
2825 */
2826 sym_t *
2827 declare_1_abstract(sym_t *sym)
2828 {
2829
2830 check_function_definition(sym, 1);
2831 check_type(sym);
2832 return sym;
2833 }
2834
2835 /*
2836 * Checks size after declarations of variables and their initialisation.
2837 */
2838 void
2839 check_size(sym_t *dsym)
2840 {
2841
2842 if (dsym->s_def != DEF)
2843 return;
2844 if (dsym->s_scl == TYPEDEF)
2845 return;
2846 if (dsym->s_type->t_tspec == FUNC)
2847 return;
2848
2849 if (length(dsym->s_type, dsym->s_name) == 0 &&
2850 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2851 /* empty array declaration: %s */
2852 if (tflag) {
2853 warning(190, dsym->s_name);
2854 } else {
2855 error(190, dsym->s_name);
2856 }
2857 }
2858 }
2859
2860 /*
2861 * Mark an object as set if it is not already
2862 */
2863 void
2864 mark_as_set(sym_t *sym)
2865 {
2866
2867 if (!sym->s_set) {
2868 sym->s_set = 1;
2869 UNIQUE_CURR_POS(sym->s_set_pos);
2870 }
2871 }
2872
2873 /*
2874 * Mark an object as used if it is not already
2875 */
2876 void
2877 mark_as_used(sym_t *sym, int fcall, int szof)
2878 {
2879
2880 if (!sym->s_used) {
2881 sym->s_used = 1;
2882 UNIQUE_CURR_POS(sym->s_use_pos);
2883 }
2884 /*
2885 * for function calls another record is written
2886 *
2887 * XXX Should symbols used in sizeof() be treated as used or not?
2888 * Probably not, because there is no sense to declare an
2889 * external variable only to get their size.
2890 */
2891 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2892 outusg(sym);
2893 }
2894
2895 /*
2896 * Prints warnings for a list of variables and labels (concatenated
2897 * with s_dlnxt) if these are not used or only set.
2898 */
2899 void
2900 check_usage(dinfo_t *di)
2901 {
2902 sym_t *sym;
2903 int mklwarn;
2904
2905 /* for this warning LINTED has no effect */
2906 mklwarn = lwarn;
2907 lwarn = LWARN_ALL;
2908
2909 #ifdef DEBUG
2910 printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2911 lwarn);
2912 #endif
2913 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
2914 check_usage_sym(di->d_asm, sym);
2915 lwarn = mklwarn;
2916 #ifdef DEBUG
2917 printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2918 lwarn);
2919 #endif
2920 }
2921
2922 /*
2923 * Prints a warning for a single variable or label if it is not used or
2924 * only set.
2925 */
2926 void
2927 check_usage_sym(int novar, sym_t *sym)
2928 {
2929 pos_t cpos;
2930
2931 if (sym->s_blklev == -1)
2932 return;
2933
2934 cpos = curr_pos;
2935
2936 if (sym->s_kind == FVFT) {
2937 if (sym->s_arg) {
2938 check_argument_usage(novar, sym);
2939 } else {
2940 check_variable_usage(novar, sym);
2941 }
2942 } else if (sym->s_kind == FLABEL) {
2943 check_label_usage(sym);
2944 } else if (sym->s_kind == FTAG) {
2945 check_tag_usage(sym);
2946 }
2947
2948 curr_pos = cpos;
2949 }
2950
2951 static void
2952 check_argument_usage(int novar, sym_t *arg)
2953 {
2954
2955 lint_assert(arg->s_set);
2956
2957 if (novar)
2958 return;
2959
2960 if (!arg->s_used && vflag) {
2961 curr_pos = arg->s_def_pos;
2962 /* argument %s unused in function %s */
2963 warning(231, arg->s_name, funcsym->s_name);
2964 }
2965 }
2966
2967 static void
2968 check_variable_usage(int novar, sym_t *sym)
2969 {
2970 scl_t sc;
2971 sym_t *xsym;
2972
2973 lint_assert(blklev != 0);
2974 lint_assert(sym->s_blklev != 0);
2975
2976 /* errors in expressions easily cause lots of these warnings */
2977 if (nerr != 0)
2978 return;
2979
2980 /*
2981 * XXX Only variables are checked, although types should
2982 * probably also be checked
2983 */
2984 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
2985 sc != AUTO && sc != REG) {
2986 return;
2987 }
2988
2989 if (novar)
2990 return;
2991
2992 if (sc == EXTERN) {
2993 if (!sym->s_used && !sym->s_set) {
2994 curr_pos = sym->s_def_pos;
2995 /* %s unused in function %s */
2996 warning(192, sym->s_name, funcsym->s_name);
2997 }
2998 } else {
2999 if (sym->s_set && !sym->s_used) {
3000 curr_pos = sym->s_set_pos;
3001 /* %s set but not used in function %s */
3002 warning(191, sym->s_name, funcsym->s_name);
3003 } else if (!sym->s_used) {
3004 curr_pos = sym->s_def_pos;
3005 /* %s unused in function %s */
3006 warning(192, sym->s_name, funcsym->s_name);
3007 }
3008 }
3009
3010 if (sc == EXTERN) {
3011 /*
3012 * information about usage is taken over into the symbol
3013 * table entry at level 0 if the symbol was locally declared
3014 * as an external symbol.
3015 *
3016 * XXX This is wrong for symbols declared static at level 0
3017 * if the usage information stems from sizeof(). This is
3018 * because symbols at level 0 only used in sizeof() are
3019 * considered to not be used.
3020 */
3021 if ((xsym = sym->s_ext_sym) != NULL) {
3022 if (sym->s_used && !xsym->s_used) {
3023 xsym->s_used = 1;
3024 xsym->s_use_pos = sym->s_use_pos;
3025 }
3026 if (sym->s_set && !xsym->s_set) {
3027 xsym->s_set = 1;
3028 xsym->s_set_pos = sym->s_set_pos;
3029 }
3030 }
3031 }
3032 }
3033
3034 static void
3035 check_label_usage(sym_t *lab)
3036 {
3037
3038 lint_assert(blklev == 1);
3039 lint_assert(lab->s_blklev == 1);
3040
3041 if (lab->s_set && !lab->s_used) {
3042 curr_pos = lab->s_set_pos;
3043 /* %s unused in function %s */
3044 warning(192, lab->s_name, funcsym->s_name);
3045 } else if (!lab->s_set) {
3046 curr_pos = lab->s_use_pos;
3047 /* undefined label %s */
3048 warning(23, lab->s_name);
3049 }
3050 }
3051
3052 static void
3053 check_tag_usage(sym_t *sym)
3054 {
3055
3056 if (!incompl(sym->s_type))
3057 return;
3058
3059 /* always complain about incomplete tags declared inside blocks */
3060 if (!zflag || dcs->d_ctx != EXTERN)
3061 return;
3062
3063 curr_pos = sym->s_def_pos;
3064 switch (sym->s_type->t_tspec) {
3065 case STRUCT:
3066 /* struct %s never defined */
3067 warning(233, sym->s_name);
3068 break;
3069 case UNION:
3070 /* union %s never defined */
3071 warning(234, sym->s_name);
3072 break;
3073 case ENUM:
3074 /* enum %s never defined */
3075 warning(235, sym->s_name);
3076 break;
3077 default:
3078 lint_assert(0);
3079 }
3080 }
3081
3082 /*
3083 * Called after the entire translation unit has been parsed.
3084 * Changes tentative definitions into definitions.
3085 * Performs some tests on global symbols. Detected problems are:
3086 * - defined variables of incomplete type
3087 * - constant variables which are not initialized
3088 * - static symbols which are never used
3089 */
3090 void
3091 check_global_symbols(void)
3092 {
3093 sym_t *sym;
3094 pos_t cpos;
3095
3096 if (blklev != 0 || dcs->d_next != NULL)
3097 norecover();
3098
3099 cpos = curr_pos;
3100
3101 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3102 if (sym->s_blklev == -1)
3103 continue;
3104 if (sym->s_kind == FVFT) {
3105 check_global_variable(sym);
3106 } else if (sym->s_kind == FTAG) {
3107 check_tag_usage(sym);
3108 } else {
3109 lint_assert(sym->s_kind == FMEMBER);
3110 }
3111 }
3112
3113 curr_pos = cpos;
3114 }
3115
3116 static void
3117 check_global_variable(sym_t *sym)
3118 {
3119
3120 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON)
3121 return;
3122
3123 lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
3124
3125 check_global_variable_size(sym);
3126
3127 if (sym->s_scl == STATIC) {
3128 if (sym->s_type->t_tspec == FUNC) {
3129 if (sym->s_used && sym->s_def != DEF) {
3130 curr_pos = sym->s_use_pos;
3131 /* static func. called but not def... */
3132 error(225, sym->s_name);
3133 }
3134 }
3135 if (!sym->s_used) {
3136 curr_pos = sym->s_def_pos;
3137 if (sym->s_type->t_tspec == FUNC) {
3138 if (sym->s_def == DEF) {
3139 if (!sym->s_inline)
3140 /* static function %s unused */
3141 warning(236, sym->s_name);
3142 } else {
3143 /* static function %s declared but... */
3144 warning(290, sym->s_name);
3145 }
3146 } else if (!sym->s_set) {
3147 /* static variable %s unused */
3148 warning(226, sym->s_name);
3149 } else {
3150 /* static variable %s set but not used */
3151 warning(307, sym->s_name);
3152 }
3153 }
3154 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3155 curr_pos = sym->s_def_pos;
3156 /* const object %s should have initializer */
3157 warning(227, sym->s_name);
3158 }
3159 }
3160 }
3161
3162 static void
3163 check_global_variable_size(sym_t *sym)
3164 {
3165
3166 if (sym->s_def == TDEF) {
3167 if (sym->s_type->t_tspec == FUNC)
3168 /*
3169 * this can happen if an syntax error occurred
3170 * after a function declaration
3171 */
3172 return;
3173 curr_pos = sym->s_def_pos;
3174 if (length(sym->s_type, sym->s_name) == 0 &&
3175 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3176 /* empty array declaration: %s */
3177 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3178 warning(190, sym->s_name);
3179 } else {
3180 error(190, sym->s_name);
3181 }
3182 }
3183 }
3184 }
3185
3186 /*
3187 * Prints information about location of previous definition/declaration.
3188 */
3189 void
3190 print_previous_declaration(int msg, sym_t *psym)
3191 {
3192 pos_t cpos;
3193
3194 if (!rflag)
3195 return;
3196
3197 cpos = curr_pos;
3198 curr_pos = psym->s_def_pos;
3199 if (msg != -1) {
3200 message(msg);
3201 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3202 /* previous definition of %s */
3203 message(261, psym->s_name);
3204 } else {
3205 /* previous declaration of %s */
3206 message(260, psym->s_name);
3207 }
3208 curr_pos = cpos;
3209 }
3210