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