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