decl.c revision 1.77 1 /* $NetBSD: decl.c,v 1.77 2020/12/29 13:33:03 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.77 2020/12/29 13:33:03 rillig Exp $");
42 #endif
43
44 #include <sys/param.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "lint1.h"
50
51 const char *unnamed = "<unnamed>";
52
53 /* shared type structures for arithmtic types and void */
54 static type_t *typetab;
55
56 /* value of next enumerator during declaration of enum types */
57 int enumval;
58
59 /*
60 * pointer to top element of a stack which contains information local
61 * to nested declarations
62 */
63 dinfo_t *dcs;
64
65 static type_t *tdeferr(type_t *, tspec_t);
66 static void settdsym(type_t *, sym_t *);
67 static tspec_t merge_type_specifiers(tspec_t, tspec_t);
68 static void align(int, int);
69 static sym_t *newtag(sym_t *, scl_t, int, int);
70 static int eqargs(type_t *, type_t *, int *);
71 static int mnoarg(type_t *, int *);
72 static int check_old_style_definition(sym_t *, sym_t *);
73 static int check_prototype_declaration(sym_t *, sym_t *);
74 static sym_t *new_style_function(sym_t *, sym_t *);
75 static void old_style_function(sym_t *, sym_t *);
76 static void ledecl(sym_t *);
77 static int check_init(sym_t *);
78 static void check_argument_usage(int, sym_t *);
79 static void check_variable_usage(int, sym_t *);
80 static void check_label_usage(sym_t *);
81 static void check_tag_usage(sym_t *);
82 static void check_global_variable(sym_t *);
83 static void check_global_variable_size(sym_t *);
84
85 /*
86 * initializes all global vars used in declarations
87 */
88 void
89 initdecl(void)
90 {
91 int i;
92
93 /* declaration stack */
94 dcs = xcalloc(1, sizeof (dinfo_t));
95 dcs->d_ctx = EXTERN;
96 dcs->d_ldlsym = &dcs->d_dlsyms;
97
98 /* type information and classification */
99 inittyp();
100
101 /* shared type structures */
102 typetab = xcalloc(NTSPEC, sizeof (type_t));
103 for (i = 0; i < NTSPEC; i++)
104 typetab[i].t_tspec = NOTSPEC;
105 typetab[BOOL].t_tspec = BOOL;
106 typetab[CHAR].t_tspec = CHAR;
107 typetab[SCHAR].t_tspec = SCHAR;
108 typetab[UCHAR].t_tspec = UCHAR;
109 typetab[SHORT].t_tspec = SHORT;
110 typetab[USHORT].t_tspec = USHORT;
111 typetab[INT].t_tspec = INT;
112 typetab[UINT].t_tspec = UINT;
113 typetab[LONG].t_tspec = LONG;
114 typetab[ULONG].t_tspec = ULONG;
115 typetab[QUAD].t_tspec = QUAD;
116 typetab[UQUAD].t_tspec = UQUAD;
117 typetab[FLOAT].t_tspec = FLOAT;
118 typetab[DOUBLE].t_tspec = DOUBLE;
119 typetab[LDOUBLE].t_tspec = LDOUBLE;
120 typetab[FCOMPLEX].t_tspec = FCOMPLEX;
121 typetab[DCOMPLEX].t_tspec = DCOMPLEX;
122 typetab[LCOMPLEX].t_tspec = LCOMPLEX;
123 typetab[COMPLEX].t_tspec = COMPLEX;
124 typetab[VOID].t_tspec = VOID;
125 /*
126 * Next two are not real types. They are only used by the parser
127 * to return keywords "signed" and "unsigned"
128 */
129 typetab[SIGNED].t_tspec = SIGNED;
130 typetab[UNSIGN].t_tspec = UNSIGN;
131 }
132
133 /*
134 * Returns a shared type structure for arithmetic types and void.
135 *
136 * It's important to duplicate this structure (using duptyp() or 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 * Mark an array, struct, union or enum type as complete or incomplete.
193 */
194 void
195 setcomplete(type_t *tp, int complete)
196 {
197 tspec_t t;
198
199 if ((t = tp->t_tspec) == ARRAY) {
200 tp->t_aincompl = !complete;
201 } else if (t == STRUCT || t == UNION) {
202 tp->t_str->sincompl = !complete;
203 } else {
204 if (t != ENUM)
205 LERROR("setcomplete()");
206 tp->t_enum->eincompl = !complete;
207 }
208 }
209
210 /*
211 * Remember the storage class of the current declaration in dcs->d_scl
212 * (the top element of the declaration stack) and detect multiple
213 * storage classes.
214 */
215 void
216 add_storage_class(scl_t sc)
217 {
218
219 if (sc == INLINE) {
220 if (dcs->d_inline)
221 /* duplicate '%s' */
222 warning(10, "inline");
223 dcs->d_inline = 1;
224 return;
225 }
226 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
227 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) {
228 /* storage class after type is obsolescent */
229 warning(83);
230 }
231 if (dcs->d_scl == NOSCL) {
232 dcs->d_scl = sc;
233 } else {
234 /*
235 * multiple storage classes. An error will be reported in
236 * deftyp().
237 */
238 dcs->d_mscl = 1;
239 }
240 }
241
242 /*
243 * Remember the type, modifier or typedef name returned by the parser
244 * in *dcs (top element of decl stack). This information is used in
245 * deftyp() to build the type used for all declarators in this
246 * declaration.
247 *
248 * If tp->t_typedef is 1, the type comes from a previously defined typename.
249 * Otherwise it comes from a type specifier (int, long, ...) or a
250 * struct/union/enum tag.
251 */
252 void
253 add_type(type_t *tp)
254 {
255 tspec_t t;
256 #ifdef DEBUG
257 char buf[1024];
258 printf("%s: %s\n", __func__, tyname(buf, sizeof(buf), tp));
259 #endif
260 if (tp->t_typedef) {
261 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
262 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
263 /*
264 * something like "typedef int a; int a b;"
265 * This should not happen with current grammar.
266 */
267 LERROR("add_type()");
268 }
269 dcs->d_type = tp;
270 return;
271 }
272
273 t = tp->t_tspec;
274
275 if (t == STRUCT || t == UNION || t == ENUM) {
276 /*
277 * something like "int struct a ..."
278 * struct/union/enum with anything else is not allowed
279 */
280 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
281 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
282 /*
283 * remember that an error must be reported in
284 * deftyp().
285 */
286 dcs->d_terr = 1;
287 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC;
288 }
289 dcs->d_type = tp;
290 return;
291 }
292
293 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
294 /*
295 * something like "struct a int"
296 * struct/union/enum with anything else is not allowed
297 */
298 dcs->d_terr = 1;
299 return;
300 }
301
302 if (t == COMPLEX) {
303 if (dcs->d_cmod == FLOAT)
304 t = FCOMPLEX;
305 else if (dcs->d_cmod == DOUBLE) {
306 t = DCOMPLEX;
307 } else
308 error(308, basic_type_name(dcs->d_cmod));
309 dcs->d_cmod = NOTSPEC;
310 }
311
312 if (t == LONG && dcs->d_lmod == LONG) {
313 /* "long long" or "long ... long" */
314 t = QUAD;
315 dcs->d_lmod = NOTSPEC;
316 if (!quadflg)
317 /* %s C does not support 'long long' */
318 (void)c99ism(265, tflag ? "traditional" : "c89");
319 }
320
321 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
322 /* something like "typedef int a; a long ..." */
323 dcs->d_type = tdeferr(dcs->d_type, t);
324 return;
325 }
326
327 /* now it can be only a combination of arithmetic types and void */
328 if (t == SIGNED || t == UNSIGN) {
329 /* remember specifiers "signed" & "unsigned" in dcs->d_smod */
330 if (dcs->d_smod != NOTSPEC)
331 /*
332 * more than one "signed" and/or "unsigned"; print
333 * an error in deftyp()
334 */
335 dcs->d_terr = 1;
336 dcs->d_smod = t;
337 } else if (t == SHORT || t == LONG || t == QUAD) {
338 /*
339 * remember specifiers "short", "long" and "long long" in
340 * dcs->d_lmod
341 */
342 if (dcs->d_lmod != NOTSPEC)
343 /* more than one, print error in deftyp() */
344 dcs->d_terr = 1;
345 dcs->d_lmod = t;
346 } else if (t == FLOAT || t == DOUBLE) {
347 if (dcs->d_lmod == NOTSPEC || dcs->d_lmod == LONG) {
348 if (dcs->d_cmod != NOTSPEC
349 || (t == FLOAT && dcs->d_lmod == LONG))
350 dcs->d_terr = 1;
351 dcs->d_cmod = t;
352 } else {
353 if (dcs->d_atyp != NOTSPEC)
354 dcs->d_terr = 1;
355 dcs->d_atyp = t;
356 }
357 } else if (t == PTR) {
358 dcs->d_type = tp;
359 } else {
360 /*
361 * remember specifiers "void", "char", "int",
362 * or "_Complex" int dcs->d_atyp
363 */
364 if (dcs->d_atyp != NOTSPEC)
365 /* more than one, print error in deftyp() */
366 dcs->d_terr = 1;
367 dcs->d_atyp = t;
368 }
369 }
370
371 /*
372 * called if a list of declaration specifiers contains a typedef name
373 * and other specifiers (except struct, union, enum, typedef name)
374 */
375 static type_t *
376 tdeferr(type_t *td, tspec_t t)
377 {
378 tspec_t t2;
379
380 t2 = td->t_tspec;
381
382 switch (t) {
383 case SIGNED:
384 case UNSIGN:
385 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
386 t2 == QUAD) {
387 if (!tflag)
388 /* modifying typedef with ... */
389 warning(5, ttab[t].tt_name);
390 td = duptyp(gettyp(merge_type_specifiers(t2, t)));
391 td->t_typedef = 1;
392 return td;
393 }
394 break;
395 case SHORT:
396 if (t2 == INT || t2 == UINT) {
397 /* modifying typedef with ... */
398 warning(5, "short");
399 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
400 td->t_typedef = 1;
401 return td;
402 }
403 break;
404 case LONG:
405 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
406 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) {
407 /* modifying typedef with ... */
408 warning(5, "long");
409 if (t2 == INT) {
410 td = gettyp(LONG);
411 } else if (t2 == UINT) {
412 td = gettyp(ULONG);
413 } else if (t2 == LONG) {
414 td = gettyp(QUAD);
415 } else if (t2 == ULONG) {
416 td = gettyp(UQUAD);
417 } else if (t2 == FLOAT) {
418 td = gettyp(DOUBLE);
419 } else if (t2 == DOUBLE) {
420 td = gettyp(LDOUBLE);
421 } else if (t2 == DCOMPLEX) {
422 td = gettyp(LCOMPLEX);
423 }
424 td = duptyp(td);
425 td->t_typedef = 1;
426 return td;
427 }
428 break;
429 /* LINTED206: (enumeration values not handled in switch) */
430 case NOTSPEC:
431 case USHORT:
432 case UCHAR:
433 case SCHAR:
434 case CHAR:
435 case BOOL:
436 case FUNC:
437 case ARRAY:
438 case PTR:
439 case ENUM:
440 case UNION:
441 case STRUCT:
442 case VOID:
443 case LDOUBLE:
444 case FLOAT:
445 case DOUBLE:
446 case UQUAD:
447 case QUAD:
448 #ifdef INT128_SIZE
449 case UINT128:
450 case INT128:
451 #endif
452 case ULONG:
453 case UINT:
454 case INT:
455 case FCOMPLEX:
456 case DCOMPLEX:
457 case LCOMPLEX:
458 case COMPLEX:
459 break;
460
461 case NTSPEC: /* this value unused */
462 break;
463 }
464
465 /* Anything other is not accepted. */
466
467 dcs->d_terr = 1;
468 return td;
469 }
470
471 /*
472 * Remember the symbol of a typedef name (2nd arg) in a struct, union
473 * or enum tag if the typedef name is the first defined for this tag.
474 *
475 * If the tag is unnamed, the typdef name is used for identification
476 * of this tag in lint2. Although its possible that more than one typedef
477 * name is defined for one tag, the first name defined should be unique
478 * if the tag is unnamed.
479 */
480 static void
481 settdsym(type_t *tp, sym_t *sym)
482 {
483 tspec_t t;
484
485 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
486 if (tp->t_str->stdef == NULL)
487 tp->t_str->stdef = sym;
488 } else if (t == ENUM) {
489 if (tp->t_enum->etdef == NULL)
490 tp->t_enum->etdef = sym;
491 }
492 }
493
494 static size_t
495 bitfieldsize(sym_t **mem)
496 {
497 size_t len = (*mem)->s_type->t_flen;
498 while (*mem && (*mem)->s_type->t_isfield) {
499 len += (*mem)->s_type->t_flen;
500 *mem = (*mem)->s_nxt;
501 }
502 return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE;
503 }
504
505 static void
506 setpackedsize(type_t *tp)
507 {
508 str_t *sp;
509 sym_t *mem;
510 char buf[256];
511
512 switch (tp->t_tspec) {
513 case STRUCT:
514 case UNION:
515 sp = tp->t_str;
516 sp->size = 0;
517 for (mem = sp->memb; mem != NULL; mem = mem->s_nxt) {
518 if (mem->s_type->t_isfield) {
519 sp->size += bitfieldsize(&mem);
520 if (mem == NULL)
521 break;
522 }
523 size_t x = (size_t)tsize(mem->s_type);
524 if (tp->t_tspec == STRUCT)
525 sp->size += x;
526 else if (x > sp->size)
527 sp->size = x;
528 }
529 break;
530 default:
531 warning(326, "packed", tyname(buf, sizeof(buf), tp));
532 break;
533 }
534 }
535
536 void
537 addpacked(void)
538 {
539 if (dcs->d_type == NULL)
540 dcs->d_ispacked = 1;
541 else
542 setpackedsize(dcs->d_type);
543 }
544
545 void
546 add_attr_used(void)
547 {
548 dcs->d_used = 1;
549 }
550
551 /*
552 * Remember a qualifier which is part of the declaration specifiers
553 * (and not the declarator) in the top element of the declaration stack.
554 * Also detect multiple qualifiers of the same kind.
555
556 * The remembered qualifier is used by deftyp() to construct the type
557 * for all declarators.
558 */
559 void
560 add_qualifier(tqual_t q)
561 {
562
563 if (q == CONST) {
564 if (dcs->d_const) {
565 /* duplicate "%s" */
566 warning(10, "const");
567 }
568 dcs->d_const = 1;
569 } else {
570 if (q == THREAD)
571 return;
572 if (q != VOLATILE)
573 LERROR("add_qualifier()");
574 if (dcs->d_volatile) {
575 /* duplicate "%s" */
576 warning(10, "volatile");
577 }
578 dcs->d_volatile = 1;
579 }
580 }
581
582 /*
583 * Go to the next declaration level (structs, nested structs, blocks,
584 * argument declaration lists ...)
585 */
586 void
587 pushdecl(scl_t sc)
588 {
589 dinfo_t *di;
590
591 /* put a new element on the declaration stack */
592 di = xcalloc(1, sizeof (dinfo_t));
593 di->d_nxt = dcs;
594 dcs = di;
595 di->d_ctx = sc;
596 di->d_ldlsym = &di->d_dlsyms;
597 if (dflag)
598 (void)printf("pushdecl(%p %d)\n", dcs, (int)sc);
599
600 }
601
602 /*
603 * Go back to previous declaration level
604 */
605 void
606 popdecl(void)
607 {
608 dinfo_t *di;
609
610 if (dflag)
611 (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx);
612
613 if (dcs->d_nxt == NULL)
614 LERROR("popdecl()");
615 di = dcs;
616 dcs = di->d_nxt;
617 switch (di->d_ctx) {
618 case EXTERN:
619 /* there is nothing after external declarations */
620 LERROR("popdecl()");
621 /* NOTREACHED */
622 case MOS:
623 case MOU:
624 case ENUMCON:
625 /*
626 * Symbols declared in (nested) structs or enums are
627 * part of the next level (they are removed from the
628 * symbol table if the symbols of the outher level are
629 * removed).
630 */
631 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
632 dcs->d_ldlsym = di->d_ldlsym;
633 break;
634 case ARG:
635 /*
636 * All symbols in dcs->d_dlsyms are introduced in old style
637 * argument declarations (it's not clean, but possible).
638 * They are appended to the list of symbols declared in
639 * an old style argument identifier list or a new style
640 * parameter type list.
641 */
642 if (di->d_dlsyms != NULL) {
643 *di->d_ldlsym = dcs->d_fpsyms;
644 dcs->d_fpsyms = di->d_dlsyms;
645 }
646 break;
647 case ABSTRACT:
648 /*
649 * casts and sizeof
650 * Append all symbols declared in the abstract declaration
651 * to the list of symbols declared in the surrounding
652 * declaration or block.
653 * XXX I'm not sure whether they should be removed from the
654 * symbol table now or later.
655 */
656 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
657 dcs->d_ldlsym = di->d_ldlsym;
658 break;
659 case AUTO:
660 /* check usage of local vars */
661 check_usage(di);
662 /* FALLTHROUGH */
663 case PARG:
664 /* usage of arguments will be checked by funcend() */
665 rmsyms(di->d_dlsyms);
666 break;
667 default:
668 LERROR("popdecl()");
669 }
670 free(di);
671 }
672
673 /*
674 * Set flag d_asm in all declaration stack elements up to the
675 * outermost one.
676 *
677 * This is used to mark compound statements which have, possibly in
678 * nested compound statements, asm statements. For these compound
679 * statements no warnings about unused or unitialized variables are
680 * printed.
681 *
682 * There is no need to clear d_asm in dinfo structs with context AUTO,
683 * because these structs are freed at the end of the compound statement.
684 * But it must be cleared in the outermost dinfo struct, which has
685 * context EXTERN. This could be done in clrtyp() and would work for
686 * C, but not for C++ (due to mixed statements and declarations). Thus
687 * we clear it in global_clean_up_decl(), which is used to do some cleanup
688 * after global declarations/definitions.
689 */
690 void
691 setasm(void)
692 {
693 dinfo_t *di;
694
695 for (di = dcs; di != NULL; di = di->d_nxt)
696 di->d_asm = 1;
697 }
698
699 /*
700 * Clean all elements of the top element of declaration stack which
701 * will be used by the next declaration
702 */
703 void
704 clrtyp(void)
705 {
706
707 dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC;
708 dcs->d_scl = NOSCL;
709 dcs->d_type = NULL;
710 dcs->d_const = dcs->d_volatile = 0;
711 dcs->d_inline = 0;
712 dcs->d_mscl = dcs->d_terr = 0;
713 dcs->d_nedecl = 0;
714 dcs->d_notyp = 0;
715 }
716
717 /*
718 * Create a type structure from the information gathered in
719 * the declaration stack.
720 * Complain about storage classes which are not possible in current
721 * context.
722 */
723 void
724 deftyp(void)
725 {
726 tspec_t t, s, l, c;
727 type_t *tp;
728 scl_t scl;
729
730 t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */
731 s = dcs->d_smod; /* SIGNED, UNSIGNED */
732 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
733 c = dcs->d_cmod; /* FLOAT, DOUBLE */
734 tp = dcs->d_type;
735 scl = dcs->d_scl;
736
737 #ifdef DEBUG
738 char buf[1024];
739 printf("%s: %s\n", __func__, tyname(buf, sizeof(buf), tp));
740 #endif
741 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
742 tp == NULL)
743 dcs->d_notyp = 1;
744 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
745 tp == NULL)
746 t = c;
747
748 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) {
749 /* should never happen */
750 LERROR("deftyp()");
751 }
752
753 if (tp == NULL) {
754 switch (t) {
755 case BOOL:
756 break;
757 case NOTSPEC:
758 t = INT;
759 /* FALLTHROUGH */
760 case INT:
761 if (s == NOTSPEC)
762 s = SIGNED;
763 break;
764 case CHAR:
765 if (l != NOTSPEC) {
766 dcs->d_terr = 1;
767 l = NOTSPEC;
768 }
769 break;
770 case FLOAT:
771 if (l == LONG) {
772 l = NOTSPEC;
773 t = DOUBLE;
774 if (!tflag)
775 /* use 'double' instead of ... */
776 warning(6);
777 }
778 break;
779 case DOUBLE:
780 if (l == LONG) {
781 case LDOUBLE:
782 l = NOTSPEC;
783 t = LDOUBLE;
784 if (tflag)
785 /* 'long double' is illegal in ... */
786 warning(266);
787 }
788 break;
789 case DCOMPLEX:
790 if (l == LONG) {
791 l = NOTSPEC;
792 t = LCOMPLEX;
793 if (tflag)
794 /* 'long double' is illegal in ... */
795 warning(266);
796 }
797 break;
798 case VOID:
799 case FCOMPLEX:
800 case LCOMPLEX:
801 break;
802 default:
803 LERROR("deftyp(%s)", basic_type_name(t));
804 }
805 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
806 dcs->d_terr = 1;
807 l = s = NOTSPEC;
808 }
809 if (l != NOTSPEC)
810 t = l;
811 dcs->d_type = gettyp(merge_type_specifiers(t, s));
812 }
813
814 if (dcs->d_mscl) {
815 /* only one storage class allowed */
816 error(7);
817 }
818 if (dcs->d_terr) {
819 /* illegal type combination */
820 error(4);
821 }
822
823 if (dcs->d_ctx == EXTERN) {
824 if (scl == REG || scl == AUTO) {
825 /* illegal storage class */
826 error(8);
827 scl = NOSCL;
828 }
829 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
830 if (scl != NOSCL && scl != REG) {
831 /* only "register" valid ... */
832 error(9);
833 scl = NOSCL;
834 }
835 }
836
837 dcs->d_scl = scl;
838
839 if (dcs->d_const && dcs->d_type->t_const) {
840 if (!dcs->d_type->t_typedef)
841 LERROR("deftyp()");
842 /* typedef already qualified with "%s" */
843 warning(68, "const");
844 }
845 if (dcs->d_volatile && dcs->d_type->t_volatile) {
846 if (!dcs->d_type->t_typedef)
847 LERROR("deftyp()");
848 /* typedef already qualified with "%s" */
849 warning(68, "volatile");
850 }
851
852 if (dcs->d_const || dcs->d_volatile) {
853 dcs->d_type = duptyp(dcs->d_type);
854 dcs->d_type->t_const |= dcs->d_const;
855 dcs->d_type->t_volatile |= dcs->d_volatile;
856 }
857 }
858
859 /*
860 * Merge type specifiers (char, ..., long long, signed, unsigned).
861 */
862 static tspec_t
863 merge_type_specifiers(tspec_t t, tspec_t s)
864 {
865
866 if (s == SIGNED || s == UNSIGN) {
867 if (t == CHAR) {
868 t = s == SIGNED ? SCHAR : UCHAR;
869 } else if (t == SHORT) {
870 t = s == SIGNED ? SHORT : USHORT;
871 } else if (t == INT) {
872 t = s == SIGNED ? INT : UINT;
873 } else if (t == LONG) {
874 t = s == SIGNED ? LONG : ULONG;
875 } else if (t == QUAD) {
876 t = s == SIGNED ? QUAD : UQUAD;
877 }
878 }
879
880 return t;
881 }
882
883 /*
884 * Return the length of a type in bits.
885 *
886 * Printing a message if the outhermost dimension of an array is 0 must
887 * be done by the caller. All other problems are reported by length()
888 * if name is not NULL.
889 */
890 int
891 length(type_t *tp, const char *name)
892 {
893 int elem, elsz;
894
895 elem = 1;
896 while (tp && tp->t_tspec == ARRAY) {
897 elem *= tp->t_dim;
898 tp = tp->t_subt;
899 }
900 if (tp == NULL)
901 return -1;
902
903 switch (tp->t_tspec) {
904 case FUNC:
905 /* compiler takes size of function */
906 LERROR("%s", msgs[12]);
907 /* NOTREACHED */
908 case STRUCT:
909 case UNION:
910 if (incompl(tp) && name != NULL) {
911 /* incomplete structure or union %s: %s */
912 error(31, tp->t_str->stag->s_name, name);
913 }
914 elsz = tp->t_str->size;
915 break;
916 case ENUM:
917 if (incompl(tp) && name != NULL) {
918 /* incomplete enum type: %s */
919 warning(13, name);
920 }
921 /* FALLTHROUGH */
922 default:
923 elsz = size(tp->t_tspec);
924 if (elsz <= 0)
925 LERROR("length(%d)", elsz);
926 break;
927 }
928 return elem * elsz;
929 }
930
931 /*
932 * Get the alignment of the given Type in bits.
933 */
934 int
935 getbound(type_t *tp)
936 {
937 size_t a;
938 tspec_t t;
939
940 while (tp && tp->t_tspec == ARRAY)
941 tp = tp->t_subt;
942
943 if (tp == NULL)
944 return -1;
945
946 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
947 a = tp->t_str->align;
948 } else if (t == FUNC) {
949 /* compiler takes alignment of function */
950 error(14);
951 a = WORST_ALIGN(1) * CHAR_BIT;
952 } else {
953 if ((a = size(t)) == 0) {
954 a = CHAR_BIT;
955 } else if (a > WORST_ALIGN(1) * CHAR_BIT) {
956 a = WORST_ALIGN(1) * CHAR_BIT;
957 }
958 }
959 if (a < CHAR_BIT || a > WORST_ALIGN(1) * CHAR_BIT)
960 LERROR("getbound()");
961 return a;
962 }
963
964 /*
965 * Concatenate two lists of symbols by s_nxt. Used by declarations of
966 * struct/union/enum elements and parameters.
967 */
968 sym_t *
969 lnklst(sym_t *l1, sym_t *l2)
970 {
971 sym_t *l;
972
973 if ((l = l1) == NULL)
974 return l2;
975 while (l1->s_nxt != NULL)
976 l1 = l1->s_nxt;
977 l1->s_nxt = l2;
978 return l;
979 }
980
981 /*
982 * Check if the type of the given symbol is valid and print an error
983 * message if it is not.
984 *
985 * Invalid types are:
986 * - arrays of incomlete types or functions
987 * - functions returning arrays or functions
988 * - void types other than type of function or pointer
989 */
990 void
991 check_type(sym_t *sym)
992 {
993 tspec_t to, t;
994 type_t **tpp, *tp;
995
996 tpp = &sym->s_type;
997 to = NOTSPEC;
998 while ((tp = *tpp) != NULL) {
999 t = tp->t_tspec;
1000 /*
1001 * If this is the type of an old style function definition,
1002 * a better warning is printed in funcdef().
1003 */
1004 if (t == FUNC && !tp->t_proto &&
1005 !(to == NOTSPEC && sym->s_osdef)) {
1006 if (sflag && hflag)
1007 /* function declaration is not a prototype */
1008 warning(287);
1009 }
1010 if (to == FUNC) {
1011 if (t == FUNC || t == ARRAY) {
1012 /* function returns illegal type */
1013 error(15);
1014 if (t == FUNC) {
1015 *tpp = incref(*tpp, PTR);
1016 } else {
1017 *tpp = incref((*tpp)->t_subt, PTR);
1018 }
1019 return;
1020 } else if (tp->t_const || tp->t_volatile) {
1021 if (sflag) { /* XXX oder better !tflag ? */
1022 /* function cannot return const... */
1023 warning(228);
1024 }
1025 }
1026 } if (to == ARRAY) {
1027 if (t == FUNC) {
1028 /* array of function is illegal */
1029 error(16);
1030 *tpp = gettyp(INT);
1031 return;
1032 } else if (t == ARRAY && tp->t_dim == 0) {
1033 /* null dimension */
1034 error(17);
1035 return;
1036 } else if (t == VOID) {
1037 /* illegal use of void */
1038 error(18);
1039 *tpp = gettyp(INT);
1040 #if 0 /* errors are produced by length() */
1041 } else if (incompl(tp)) {
1042 /* array of incomplete type */
1043 if (sflag) {
1044 error(301);
1045 } else {
1046 warning(301);
1047 }
1048 #endif
1049 }
1050 } else if (to == NOTSPEC && t == VOID) {
1051 if (dcs->d_ctx == PARG) {
1052 if (sym->s_scl != ABSTRACT) {
1053 if (sym->s_name == unnamed)
1054 LERROR("check_type()");
1055 /* void param cannot have name: %s */
1056 error(61, sym->s_name);
1057 *tpp = gettyp(INT);
1058 }
1059 } else if (dcs->d_ctx == ABSTRACT) {
1060 /* ok */
1061 } else if (sym->s_scl != TYPEDEF) {
1062 /* void type for %s */
1063 error(19, sym->s_name);
1064 *tpp = gettyp(INT);
1065 }
1066 }
1067 if (t == VOID && to != PTR) {
1068 if (tp->t_const || tp->t_volatile) {
1069 /* inappropriate qualifiers with "void" */
1070 warning(69);
1071 tp->t_const = tp->t_volatile = 0;
1072 }
1073 }
1074 tpp = &tp->t_subt;
1075 to = t;
1076 }
1077 }
1078
1079 /*
1080 * Process the declarator of a struct/union element.
1081 */
1082 sym_t *
1083 declarator_1_struct_union(sym_t *dsym)
1084 {
1085 type_t *tp;
1086 tspec_t t;
1087 int sz, len;
1088 int o = 0; /* Appease gcc */
1089 scl_t sc;
1090
1091 if ((sc = dsym->s_scl) != MOS && sc != MOU)
1092 LERROR("declarator_1_struct_union()");
1093
1094 if (dcs->d_rdcsym != NULL) {
1095 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU)
1096 /* should be ensured by storesym() */
1097 LERROR("declarator_1_struct_union()");
1098 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1099 /* duplicate member name: %s */
1100 error(33, dsym->s_name);
1101 rmsym(dcs->d_rdcsym);
1102 }
1103 }
1104
1105 check_type(dsym);
1106
1107 t = (tp = dsym->s_type)->t_tspec;
1108
1109 if (dsym->s_field) {
1110 /*
1111 * bit field
1112 *
1113 * only unsigned and signed int are portable bit-field types
1114 *(at least in ANSI C, in traditional C only unsigned int)
1115 */
1116 if (t == CHAR || t == UCHAR || t == SCHAR ||
1117 t == SHORT || t == USHORT || t == ENUM) {
1118 if (bitfieldtype_ok == 0) {
1119 if (sflag) {
1120 char buf[64];
1121 /*
1122 * bit-field type '%s' invalid in
1123 * ANSI C
1124 */
1125 warning(273,
1126 tyname(buf, sizeof(buf), tp));
1127 } else if (pflag) {
1128 /* nonportable bit-field type */
1129 warning(34);
1130 }
1131 }
1132 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1133 if (pflag && bitfieldtype_ok == 0) {
1134 /* nonportable bit-field type */
1135 warning(34);
1136 }
1137 } else if (t != INT && t != UINT) {
1138 /*
1139 * Non-integer types are always illegal for
1140 * bitfields, regardless of BITFIELDTYPE.
1141 * Integer types not dealt with above are
1142 * okay only if BITFIELDTYPE is in effect.
1143 */
1144 if (bitfieldtype_ok == 0 || tspec_is_int(t) == 0) {
1145 /* illegal bit-field type */
1146 warning(35);
1147 sz = tp->t_flen;
1148 dsym->s_type = tp = duptyp(gettyp(t = INT));
1149 if ((tp->t_flen = sz) > size(t))
1150 tp->t_flen = size(t);
1151 }
1152 }
1153 if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) {
1154 /* illegal bit-field size */
1155 error(36, len);
1156 tp->t_flen = size(t);
1157 } else if (len == 0 && dsym->s_name != unnamed) {
1158 /* zero size bit-field */
1159 error(37);
1160 tp->t_flen = size(t);
1161 }
1162 if (dsym->s_scl == MOU) {
1163 /* illegal use of bit-field */
1164 error(41);
1165 dsym->s_type->t_isfield = 0;
1166 dsym->s_field = 0;
1167 }
1168 } else if (t == FUNC) {
1169 /* function illegal in structure or union */
1170 error(38);
1171 dsym->s_type = tp = incref(tp, t = PTR);
1172 }
1173
1174 /*
1175 * bit-fields of length 0 are not warned about because length()
1176 * does not return the length of the bit-field but the length
1177 * of the type the bit-field is packed in (its ok)
1178 */
1179 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1180 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1181 /* illegal zero sized structure member: %s */
1182 c99ism(39, dsym->s_name);
1183 }
1184 }
1185
1186 if (dcs->d_ctx == MOU) {
1187 o = dcs->d_offset;
1188 dcs->d_offset = 0;
1189 }
1190 if (dsym->s_field) {
1191 align(getbound(tp), tp->t_flen);
1192 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1193 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1194 dcs->d_offset += tp->t_flen;
1195 } else {
1196 align(getbound(tp), 0);
1197 dsym->s_value.v_quad = dcs->d_offset;
1198 dcs->d_offset += sz;
1199 }
1200 if (dcs->d_ctx == MOU) {
1201 if (o > dcs->d_offset)
1202 dcs->d_offset = o;
1203 }
1204
1205 check_function_definition(dsym, 0);
1206
1207 /*
1208 * Clear the BITFIELDTYPE indicator after processing each
1209 * structure element.
1210 */
1211 bitfieldtype_ok = 0;
1212
1213 return dsym;
1214 }
1215
1216 /*
1217 * Aligns next structure element as required.
1218 *
1219 * al contains the required alignment, len the length of a bit-field.
1220 */
1221 static void
1222 align(int al, int len)
1223 {
1224 int no;
1225
1226 /*
1227 * The alignment of the current element becomes the alignment of
1228 * the struct/union if it is larger than the current alignment
1229 * of the struct/union.
1230 */
1231 if (al > dcs->d_stralign)
1232 dcs->d_stralign = al;
1233
1234 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1235 if (len == 0 || dcs->d_offset + len > no)
1236 dcs->d_offset = no;
1237 }
1238
1239 /*
1240 * Remember the width of the field in its type structure.
1241 */
1242 sym_t *
1243 bitfield(sym_t *dsym, int len)
1244 {
1245
1246 if (dsym == NULL) {
1247 dsym = getblk(sizeof (sym_t));
1248 dsym->s_name = unnamed;
1249 dsym->s_kind = FMOS;
1250 dsym->s_scl = MOS;
1251 dsym->s_type = gettyp(UINT);
1252 dsym->s_blklev = -1;
1253 }
1254 dsym->s_type = duptyp(dsym->s_type);
1255 dsym->s_type->t_isfield = 1;
1256 dsym->s_type->t_flen = len;
1257 dsym->s_field = 1;
1258 return dsym;
1259 }
1260
1261 /*
1262 * Collect informations about a sequence of asterisks and qualifiers
1263 * in a list of type pqinf_t.
1264 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1265 * will be at the top of the list.
1266 */
1267 pqinf_t *
1268 merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2)
1269 {
1270 pqinf_t *p;
1271
1272 if (p2->p_pcnt != 0) {
1273 /* left '*' at the end of the list */
1274 for (p = p2; p->p_nxt != NULL; p = p->p_nxt)
1275 continue;
1276 p->p_nxt = p1;
1277 return p2;
1278 } else {
1279 if (p2->p_const) {
1280 if (p1->p_const) {
1281 /* duplicate %s */
1282 warning(10, "const");
1283 }
1284 p1->p_const = 1;
1285 }
1286 if (p2->p_volatile) {
1287 if (p1->p_volatile) {
1288 /* duplicate %s */
1289 warning(10, "volatile");
1290 }
1291 p1->p_volatile = 1;
1292 }
1293 free(p2);
1294 return p1;
1295 }
1296 }
1297
1298 /*
1299 * The following 3 functions extend the type of a declarator with
1300 * pointer, function and array types.
1301 *
1302 * The current type is the Type built by deftyp() (dcs->d_type) and
1303 * pointer, function and array types already added for this
1304 * declarator. The new type extension is inserted between both.
1305 */
1306 sym_t *
1307 add_pointer(sym_t *decl, pqinf_t *pi)
1308 {
1309 type_t **tpp, *tp;
1310 pqinf_t *npi;
1311
1312 tpp = &decl->s_type;
1313 while (*tpp && *tpp != dcs->d_type)
1314 tpp = &(*tpp)->t_subt;
1315 if (*tpp == NULL)
1316 return decl;
1317
1318 while (pi != NULL) {
1319 *tpp = tp = getblk(sizeof (type_t));
1320 tp->t_tspec = PTR;
1321 tp->t_const = pi->p_const;
1322 tp->t_volatile = pi->p_volatile;
1323 *(tpp = &tp->t_subt) = dcs->d_type;
1324 npi = pi->p_nxt;
1325 free(pi);
1326 pi = npi;
1327 }
1328 return decl;
1329 }
1330
1331 /*
1332 * If a dimension was specified, dim is 1, otherwise 0
1333 * n is the specified dimension
1334 */
1335 sym_t *
1336 add_array(sym_t *decl, int dim, int n)
1337 {
1338 type_t **tpp, *tp;
1339
1340 tpp = &decl->s_type;
1341 while (*tpp && *tpp != dcs->d_type)
1342 tpp = &(*tpp)->t_subt;
1343 if (*tpp == NULL)
1344 return decl;
1345
1346 *tpp = tp = getblk(sizeof (type_t));
1347 tp->t_tspec = ARRAY;
1348 tp->t_subt = dcs->d_type;
1349 tp->t_dim = n;
1350
1351 if (n < 0) {
1352 /* negative array dimension */
1353 error(20, n);
1354 n = 0;
1355 } else if (n == 0 && dim) {
1356 /* zero array dimension */
1357 c99ism(322, dim);
1358 } else if (n == 0 && !dim) {
1359 setcomplete(tp, 0);
1360 }
1361
1362 return decl;
1363 }
1364
1365 sym_t *
1366 add_function(sym_t *decl, sym_t *args)
1367 {
1368 type_t **tpp, *tp;
1369
1370 if (dcs->d_proto) {
1371 if (tflag)
1372 /* function prototypes are illegal in traditional C */
1373 warning(270);
1374 args = new_style_function(decl, args);
1375 } else {
1376 old_style_function(decl, args);
1377 }
1378
1379 /*
1380 * The symbols are removed from the symbol table by popdecl() after
1381 * add_function(). To be able to restore them if this is a function
1382 * definition, a pointer to the list of all symbols is stored in
1383 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1384 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1385 * (dcs->d_nxt must be used because *dcs is the declaration stack
1386 * element created for the list of params and is removed after
1387 * add_function())
1388 */
1389 if (dcs->d_nxt->d_ctx == EXTERN &&
1390 decl->s_type == dcs->d_nxt->d_type) {
1391 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1392 dcs->d_nxt->d_fargs = args;
1393 }
1394
1395 tpp = &decl->s_type;
1396 while (*tpp && *tpp != dcs->d_nxt->d_type)
1397 tpp = &(*tpp)->t_subt;
1398 if (*tpp == NULL)
1399 return decl;
1400
1401 *tpp = tp = getblk(sizeof (type_t));
1402 tp->t_tspec = FUNC;
1403 tp->t_subt = dcs->d_nxt->d_type;
1404 if ((tp->t_proto = dcs->d_proto) != 0)
1405 tp->t_args = args;
1406 tp->t_vararg = dcs->d_vararg;
1407
1408 return decl;
1409 }
1410
1411 /*
1412 * Called for new style function declarations.
1413 */
1414 /* ARGSUSED */
1415 static sym_t *
1416 new_style_function(sym_t *decl, sym_t *args)
1417 {
1418 sym_t *arg, *sym;
1419 scl_t sc;
1420 int n;
1421
1422 /*
1423 * Declarations of structs/unions/enums in param lists are legal,
1424 * but senseless.
1425 */
1426 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1427 sc = sym->s_scl;
1428 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1429 /* dubious tag declaration: %s %s */
1430 warning(85, storage_class_name(sc), sym->s_name);
1431 }
1432 }
1433
1434 n = 1;
1435 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1436 if (arg->s_type->t_tspec == VOID) {
1437 if (n > 1 || arg->s_nxt != NULL) {
1438 /* "void" must be sole parameter */
1439 error(60);
1440 arg->s_type = gettyp(INT);
1441 }
1442 }
1443 n++;
1444 }
1445
1446 /* return NULL if first param is VOID */
1447 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL;
1448 }
1449
1450 /*
1451 * Called for old style function declarations.
1452 */
1453 static void
1454 old_style_function(sym_t *decl, sym_t *args)
1455 {
1456
1457 /*
1458 * Remember list of params only if this is really seams to be
1459 * a function definition.
1460 */
1461 if (dcs->d_nxt->d_ctx == EXTERN &&
1462 decl->s_type == dcs->d_nxt->d_type) {
1463 /*
1464 * We assume that this becomes a function definition. If
1465 * we are wrong, its corrected in check_function_definition().
1466 */
1467 if (args != NULL) {
1468 decl->s_osdef = 1;
1469 decl->s_args = args;
1470 }
1471 } else {
1472 if (args != NULL)
1473 /* function prototype parameters must have types */
1474 warning(62);
1475 }
1476 }
1477
1478 /*
1479 * Lists of Identifiers in functions declarations are allowed only if
1480 * its also a function definition. If this is not the case, print a
1481 * error message.
1482 */
1483 void
1484 check_function_definition(sym_t *sym, int msg)
1485 {
1486
1487 if (sym->s_osdef) {
1488 if (msg) {
1489 /* incomplete or misplaced function definition */
1490 error(22);
1491 }
1492 sym->s_osdef = 0;
1493 sym->s_args = NULL;
1494 }
1495 }
1496
1497 /*
1498 * Process the name in a declarator.
1499 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
1500 * TYPEDEF.
1501 * s_def and s_reg are valid after declarator_name().
1502 */
1503 sym_t *
1504 declarator_name(sym_t *sym)
1505 {
1506 scl_t sc = NOSCL;
1507
1508 if (sym->s_scl == NOSCL) {
1509 dcs->d_rdcsym = NULL;
1510 } else if (sym->s_defarg) {
1511 sym->s_defarg = 0;
1512 dcs->d_rdcsym = NULL;
1513 } else {
1514 dcs->d_rdcsym = sym;
1515 sym = pushdown(sym);
1516 }
1517
1518 switch (dcs->d_ctx) {
1519 case MOS:
1520 case MOU:
1521 /* Set parent */
1522 sym->s_styp = dcs->d_tagtyp->t_str;
1523 sym->s_def = DEF;
1524 sym->s_value.v_tspec = INT;
1525 sc = dcs->d_ctx;
1526 break;
1527 case EXTERN:
1528 /*
1529 * static and external symbols without "extern" are
1530 * considered to be tentative defined, external
1531 * symbols with "extern" are declared, and typedef names
1532 * are defined. Tentative defined and declared symbols
1533 * may become defined if an initializer is present or
1534 * this is a function definition.
1535 */
1536 if ((sc = dcs->d_scl) == NOSCL) {
1537 sc = EXTERN;
1538 sym->s_def = TDEF;
1539 } else if (sc == STATIC) {
1540 sym->s_def = TDEF;
1541 } else if (sc == TYPEDEF) {
1542 sym->s_def = DEF;
1543 } else if (sc == EXTERN) {
1544 sym->s_def = DECL;
1545 } else {
1546 LERROR("declarator_name()");
1547 }
1548 break;
1549 case PARG:
1550 sym->s_arg = 1;
1551 /* FALLTHROUGH */
1552 case ARG:
1553 if ((sc = dcs->d_scl) == NOSCL) {
1554 sc = AUTO;
1555 } else if (sc == REG) {
1556 sym->s_reg = 1;
1557 sc = AUTO;
1558 } else {
1559 LERROR("declarator_name()");
1560 }
1561 sym->s_def = DEF;
1562 break;
1563 case AUTO:
1564 if ((sc = dcs->d_scl) == NOSCL) {
1565 /*
1566 * XXX somewhat ugly because we dont know whether
1567 * this is AUTO or EXTERN (functions). If we are
1568 * wrong it must be corrected in decl1loc(), where
1569 * we have the necessary type information.
1570 */
1571 sc = AUTO;
1572 sym->s_def = DEF;
1573 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1574 sym->s_def = DEF;
1575 } else if (sc == REG) {
1576 sym->s_reg = 1;
1577 sc = AUTO;
1578 sym->s_def = DEF;
1579 } else if (sc == EXTERN) {
1580 sym->s_def = DECL;
1581 } else {
1582 LERROR("declarator_name()");
1583 }
1584 break;
1585 default:
1586 LERROR("declarator_name()");
1587 }
1588 sym->s_scl = sc;
1589
1590 sym->s_type = dcs->d_type;
1591
1592 dcs->d_fpsyms = NULL;
1593
1594 return sym;
1595 }
1596
1597 /*
1598 * Process a name in the list of formal params in an old style function
1599 * definition.
1600 */
1601 sym_t *
1602 old_style_function_name(sym_t *sym)
1603 {
1604
1605 if (sym->s_scl != NOSCL) {
1606 if (blklev == sym->s_blklev) {
1607 /* redeclaration of formal parameter %s */
1608 error(21, sym->s_name);
1609 if (!sym->s_defarg)
1610 LERROR("old_style_function_name()");
1611 }
1612 sym = pushdown(sym);
1613 }
1614 sym->s_type = gettyp(INT);
1615 sym->s_scl = AUTO;
1616 sym->s_def = DEF;
1617 sym->s_defarg = sym->s_arg = 1;
1618 return sym;
1619 }
1620
1621 /*
1622 * Create the type of a tag.
1623 *
1624 * tag points to the symbol table entry of the tag
1625 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1626 * decl is 1 if the type of the tag will be completed in this declaration
1627 * (the following token is T_LBRACE)
1628 * semi is 1 if the following token is T_SEMI
1629 */
1630 type_t *
1631 mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1632 {
1633 scl_t scl = NOSCL;
1634 type_t *tp;
1635
1636 if (kind == STRUCT) {
1637 scl = STRTAG;
1638 } else if (kind == UNION) {
1639 scl = UNIONTAG;
1640 } else if (kind == ENUM) {
1641 scl = ENUMTAG;
1642 } else {
1643 LERROR("mktag()");
1644 }
1645
1646 if (tag != NULL) {
1647 if (tag->s_scl != NOSCL) {
1648 tag = newtag(tag, scl, decl, semi);
1649 } else {
1650 /* a new tag, no empty declaration */
1651 dcs->d_nxt->d_nedecl = 1;
1652 if (scl == ENUMTAG && !decl) {
1653 if (!tflag && (sflag || pflag))
1654 /* forward reference to enum type */
1655 warning(42);
1656 }
1657 }
1658 if (tag->s_scl == NOSCL) {
1659 tag->s_scl = scl;
1660 tag->s_type = tp = getblk(sizeof (type_t));
1661 tp->t_ispacked = dcs->d_ispacked;
1662 } else {
1663 tp = tag->s_type;
1664 }
1665 } else {
1666 tag = getblk(sizeof (sym_t));
1667 tag->s_name = unnamed;
1668 UNIQUE_CURR_POS(tag->s_dpos);
1669 tag->s_kind = FTAG;
1670 tag->s_scl = scl;
1671 tag->s_blklev = -1;
1672 tag->s_type = tp = getblk(sizeof (type_t));
1673 tp->t_ispacked = dcs->d_ispacked;
1674 dcs->d_nxt->d_nedecl = 1;
1675 }
1676
1677 if (tp->t_tspec == NOTSPEC) {
1678 tp->t_tspec = kind;
1679 if (kind != ENUM) {
1680 tp->t_str = getblk(sizeof (str_t));
1681 tp->t_str->align = CHAR_BIT;
1682 tp->t_str->stag = tag;
1683 } else {
1684 tp->t_isenum = 1;
1685 tp->t_enum = getblk(sizeof(*tp->t_enum));
1686 tp->t_enum->etag = tag;
1687 }
1688 setcomplete(tp, 0);
1689 }
1690 return tp;
1691 }
1692
1693 /*
1694 * Checks all possible cases of tag redeclarations.
1695 * decl is 1 if T_LBRACE follows
1696 * semi is 1 if T_SEMI follows
1697 */
1698 static sym_t *
1699 newtag(sym_t *tag, scl_t scl, int decl, int semi)
1700 {
1701
1702 if (tag->s_blklev < blklev) {
1703 if (semi) {
1704 /* "struct a;" */
1705 if (!tflag) {
1706 if (!sflag)
1707 /* decl. introduces new type ... */
1708 warning(44, storage_class_name(scl),
1709 tag->s_name);
1710 tag = pushdown(tag);
1711 } else if (tag->s_scl != scl) {
1712 /* base type is really "%s %s" */
1713 warning(45, storage_class_name(tag->s_scl),
1714 tag->s_name);
1715 }
1716 dcs->d_nxt->d_nedecl = 1;
1717 } else if (decl) {
1718 /* "struct a { ... } " */
1719 if (hflag)
1720 /* redefinition hides earlier one: %s */
1721 warning(43, tag->s_name);
1722 tag = pushdown(tag);
1723 dcs->d_nxt->d_nedecl = 1;
1724 } else if (tag->s_scl != scl) {
1725 /* base type is really "%s %s" */
1726 warning(45, storage_class_name(tag->s_scl),
1727 tag->s_name);
1728 /* declaration introduces new type in ANSI C: %s %s */
1729 if (!sflag) {
1730 warning(44, storage_class_name(scl),
1731 tag->s_name);
1732 }
1733 tag = pushdown(tag);
1734 dcs->d_nxt->d_nedecl = 1;
1735 }
1736 } else {
1737 if (tag->s_scl != scl) {
1738 /* (%s) tag redeclared */
1739 error(46, storage_class_name(tag->s_scl));
1740 print_previous_declaration(-1, tag);
1741 tag = pushdown(tag);
1742 dcs->d_nxt->d_nedecl = 1;
1743 } else if (decl && !incompl(tag->s_type)) {
1744 /* (%s) tag redeclared */
1745 error(46, storage_class_name(tag->s_scl));
1746 print_previous_declaration(-1, tag);
1747 tag = pushdown(tag);
1748 dcs->d_nxt->d_nedecl = 1;
1749 } else if (semi || decl) {
1750 dcs->d_nxt->d_nedecl = 1;
1751 }
1752 }
1753 return tag;
1754 }
1755
1756 const char *
1757 storage_class_name(scl_t sc)
1758 {
1759 const char *s;
1760
1761 switch (sc) {
1762 case EXTERN: s = "extern"; break;
1763 case STATIC: s = "static"; break;
1764 case AUTO: s = "auto"; break;
1765 case REG: s = "register"; break;
1766 case TYPEDEF: s = "typedef"; break;
1767 case STRTAG: s = "struct"; break;
1768 case UNIONTAG: s = "union"; break;
1769 case ENUMTAG: s = "enum"; break;
1770 default: LERROR("tagttoa()");
1771 }
1772 return s;
1773 }
1774
1775 /*
1776 * tp points to the type of the tag, fmem to the list of members/enums.
1777 */
1778 type_t *
1779 complete_tag(type_t *tp, sym_t *fmem)
1780 {
1781 tspec_t t;
1782 str_t *sp;
1783 int n;
1784 sym_t *mem;
1785
1786 setcomplete(tp, 1);
1787
1788 if ((t = tp->t_tspec) != ENUM) {
1789 align(dcs->d_stralign, 0);
1790 sp = tp->t_str;
1791 sp->align = dcs->d_stralign;
1792 sp->memb = fmem;
1793 if (tp->t_ispacked)
1794 setpackedsize(tp);
1795 else
1796 sp->size = dcs->d_offset;
1797
1798 if (sp->size == 0) {
1799 /* zero sized %s */
1800 (void)c99ism(47, ttab[t].tt_name);
1801 }
1802
1803 n = 0;
1804 for (mem = fmem; mem != NULL; mem = mem->s_nxt) {
1805 /* bind anonymous members to the structure */
1806 if (mem->s_styp == NULL) {
1807 mem->s_styp = sp;
1808 if (mem->s_type->t_isfield) {
1809 sp->size += bitfieldsize(&mem);
1810 if (mem == NULL)
1811 break;
1812 }
1813 sp->size += tsize(mem->s_type);
1814 }
1815 if (mem->s_name != unnamed)
1816 n++;
1817 }
1818
1819 if (n == 0 && sp->size != 0) {
1820 /* %s has no named members */
1821 warning(65, t == STRUCT ? "structure" : "union");
1822 }
1823 } else {
1824 tp->t_enum->elem = fmem;
1825 }
1826 return tp;
1827 }
1828
1829 /*
1830 * Processes the name of an enumerator in an enum declaration.
1831 *
1832 * sym points to the enumerator
1833 * val is the value of the enumerator
1834 * impl is 1 if the value of the enumerator was not explicitly specified.
1835 */
1836 sym_t *
1837 ename(sym_t *sym, int val, int impl)
1838 {
1839
1840 if (sym->s_scl) {
1841 if (sym->s_blklev == blklev) {
1842 /* no hflag, because this is illegal!!! */
1843 if (sym->s_arg) {
1844 /* enumeration constant hides parameter: %s */
1845 warning(57, sym->s_name);
1846 } else {
1847 /* redeclaration of %s */
1848 error(27, sym->s_name);
1849 /*
1850 * inside blocks it should not be too
1851 * complicated to find the position of the
1852 * previous declaration
1853 */
1854 if (blklev == 0)
1855 print_previous_declaration(-1, sym);
1856 }
1857 } else {
1858 if (hflag)
1859 /* redefinition hides earlier one: %s */
1860 warning(43, sym->s_name);
1861 }
1862 sym = pushdown(sym);
1863 }
1864 sym->s_scl = ENUMCON;
1865 sym->s_type = dcs->d_tagtyp;
1866 sym->s_value.v_tspec = INT;
1867 sym->s_value.v_quad = val;
1868 if (impl && val - 1 == TARG_INT_MAX) {
1869 /* overflow in enumeration values: %s */
1870 warning(48, sym->s_name);
1871 }
1872 enumval = val + 1;
1873 return sym;
1874 }
1875
1876 /*
1877 * Process a single external declarator.
1878 */
1879 void
1880 decl1ext(sym_t *dsym, int initflg)
1881 {
1882 int dowarn, rval, redec;
1883 sym_t *rdsym;
1884
1885 check_function_definition(dsym, 1);
1886
1887 check_type(dsym);
1888
1889 if (initflg && !(initerr = check_init(dsym)))
1890 dsym->s_def = DEF;
1891
1892 /*
1893 * Declarations of functions are marked as "tentative" in
1894 * declarator_name(). This is wrong because there are no
1895 * tentative function definitions.
1896 */
1897 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1898 dsym->s_def = DECL;
1899
1900 if (dcs->d_inline) {
1901 if (dsym->s_type->t_tspec == FUNC) {
1902 dsym->s_inline = 1;
1903 } else {
1904 /* variable declared inline: %s */
1905 warning(268, dsym->s_name);
1906 }
1907 }
1908
1909 /* Write the declaration into the output file */
1910 if (plibflg && llibflg &&
1911 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1912 /*
1913 * With both LINTLIBRARY and PROTOLIB the prototyp is
1914 * written as a function definition to the output file.
1915 */
1916 rval = dsym->s_type->t_subt->t_tspec != VOID;
1917 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL);
1918 } else {
1919 outsym(dsym, dsym->s_scl, dsym->s_def);
1920 }
1921
1922 if ((rdsym = dcs->d_rdcsym) != NULL) {
1923
1924 /*
1925 * If the old symbol stems from an old style function
1926 * definition, we have remembered the params in rdsmy->s_args
1927 * and compare them with the params of the prototype.
1928 */
1929 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1930 redec = check_old_style_definition(rdsym, dsym);
1931 } else {
1932 redec = 0;
1933 }
1934
1935 if (!redec && !check_redeclaration(dsym, (dowarn = 0, &dowarn))) {
1936
1937 if (dowarn) {
1938 /* redeclaration of %s */
1939 (*(sflag ? error : warning))(27, dsym->s_name);
1940 print_previous_declaration(-1, rdsym);
1941 }
1942
1943 /*
1944 * Take over the remembered params if the new symbol
1945 * is not a prototype.
1946 */
1947 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1948 dsym->s_osdef = rdsym->s_osdef;
1949 dsym->s_args = rdsym->s_args;
1950 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1951 }
1952
1953 /*
1954 * Remember the position of the declaration if the
1955 * old symbol was a prototype and the new is not.
1956 * Also remember the position if the old symbol
1957 * was defined and the new is not.
1958 */
1959 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1960 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1961 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1962 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos);
1963 }
1964
1965 /*
1966 * Copy informations about usage of the name into
1967 * the new symbol.
1968 */
1969 copy_usage_info(dsym, rdsym);
1970
1971 /* Once a name is defined, it remains defined. */
1972 if (rdsym->s_def == DEF)
1973 dsym->s_def = DEF;
1974
1975 /* once a function is inline, it remains inline */
1976 if (rdsym->s_inline)
1977 dsym->s_inline = 1;
1978
1979 complete_type(dsym, rdsym);
1980
1981 }
1982
1983 rmsym(rdsym);
1984 }
1985
1986 if (dsym->s_scl == TYPEDEF) {
1987 dsym->s_type = duptyp(dsym->s_type);
1988 dsym->s_type->t_typedef = 1;
1989 settdsym(dsym->s_type, dsym);
1990 }
1991
1992 }
1993
1994 /*
1995 * Copies information about usage into a new symbol table entry of
1996 * the same symbol.
1997 */
1998 void
1999 copy_usage_info(sym_t *sym, sym_t *rdsym)
2000 {
2001
2002 sym->s_spos = rdsym->s_spos;
2003 sym->s_upos = rdsym->s_upos;
2004 sym->s_set = rdsym->s_set;
2005 sym->s_used = rdsym->s_used;
2006 }
2007
2008 /*
2009 * Prints an error and returns 1 if a symbol is redeclared/redefined.
2010 * Otherwise returns 0 and, in some cases of minor problems, prints
2011 * a warning.
2012 */
2013 int
2014 check_redeclaration(sym_t *dsym, int *dowarn)
2015 {
2016 sym_t *rsym;
2017
2018 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
2019 /* redeclaration of %s */
2020 error(27, dsym->s_name);
2021 print_previous_declaration(-1, rsym);
2022 return 1;
2023 }
2024 if (rsym->s_scl == TYPEDEF) {
2025 /* typedef redeclared: %s */
2026 error(89, dsym->s_name);
2027 print_previous_declaration(-1, rsym);
2028 return 1;
2029 }
2030 if (dsym->s_scl == TYPEDEF) {
2031 /* redeclaration of %s */
2032 error(27, dsym->s_name);
2033 print_previous_declaration(-1, rsym);
2034 return 1;
2035 }
2036 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2037 /* redefinition of %s */
2038 error(28, dsym->s_name);
2039 print_previous_declaration(-1, rsym);
2040 return(1);
2041 }
2042 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) {
2043 /* redeclaration of %s */
2044 error(27, dsym->s_name);
2045 print_previous_declaration(-1, rsym);
2046 return(1);
2047 }
2048 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2049 return(0);
2050 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2051 return(0);
2052 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2053 return(0);
2054 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2055 /*
2056 * All cases except "int a = 1; static int a;" are caught
2057 * above with or without a warning
2058 */
2059 /* redeclaration of %s */
2060 error(27, dsym->s_name);
2061 print_previous_declaration(-1, rsym);
2062 return(1);
2063 }
2064 if (rsym->s_scl == EXTERN) {
2065 /* previously declared extern, becomes static: %s */
2066 warning(29, dsym->s_name);
2067 print_previous_declaration(-1, rsym);
2068 return(0);
2069 }
2070 /*
2071 * Now its on of:
2072 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2073 */
2074 /* redeclaration of %s; ANSI C requires "static" */
2075 if (sflag) {
2076 warning(30, dsym->s_name);
2077 print_previous_declaration(-1, rsym);
2078 }
2079 dsym->s_scl = STATIC;
2080 return 0;
2081 }
2082
2083 static int
2084 chkqual(type_t *tp1, type_t *tp2, int ignqual)
2085 {
2086 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2087 return 0;
2088
2089 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2090 return 0;
2091
2092 return 1;
2093 }
2094
2095 int
2096 eqptrtype(type_t *tp1, type_t *tp2, int ignqual)
2097 {
2098 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2099 return 0;
2100
2101 if (!chkqual(tp1, tp2, ignqual))
2102 return 0;
2103
2104 return 1;
2105 }
2106
2107
2108 /*
2109 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
2110 *
2111 * ignqual ignore qualifiers of type; used for function params
2112 * promot promote left type; used for comparison of params of
2113 * old style function definitions with params of prototypes.
2114 * *dowarn set to 1 if an old style function declaration is not
2115 * compatible with a prototype
2116 */
2117 int
2118 eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn)
2119 {
2120 tspec_t t;
2121
2122 while (tp1 != NULL && tp2 != NULL) {
2123
2124 t = tp1->t_tspec;
2125 if (promot) {
2126 if (t == FLOAT) {
2127 t = DOUBLE;
2128 } else if (t == CHAR || t == SCHAR) {
2129 t = INT;
2130 } else if (t == UCHAR) {
2131 t = tflag ? UINT : INT;
2132 } else if (t == SHORT) {
2133 t = INT;
2134 } else if (t == USHORT) {
2135 /* CONSTCOND */
2136 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
2137 }
2138 }
2139
2140 if (t != tp2->t_tspec)
2141 return 0;
2142
2143 if (!chkqual(tp1, tp2, ignqual))
2144 return 0;
2145
2146 if (t == STRUCT || t == UNION)
2147 return tp1->t_str == tp2->t_str;
2148
2149 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2150 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2151 return 0;
2152 }
2153
2154 /* dont check prototypes for traditional */
2155 if (t == FUNC && !tflag) {
2156 if (tp1->t_proto && tp2->t_proto) {
2157 if (!eqargs(tp1, tp2, dowarn))
2158 return 0;
2159 } else if (tp1->t_proto) {
2160 if (!mnoarg(tp1, dowarn))
2161 return 0;
2162 } else if (tp2->t_proto) {
2163 if (!mnoarg(tp2, dowarn))
2164 return 0;
2165 }
2166 }
2167
2168 tp1 = tp1->t_subt;
2169 tp2 = tp2->t_subt;
2170 ignqual = promot = 0;
2171
2172 }
2173
2174 return tp1 == tp2;
2175 }
2176
2177 /*
2178 * Compares the parameter types of two prototypes.
2179 */
2180 static int
2181 eqargs(type_t *tp1, type_t *tp2, int *dowarn)
2182 {
2183 sym_t *a1, *a2;
2184
2185 if (tp1->t_vararg != tp2->t_vararg)
2186 return 0;
2187
2188 a1 = tp1->t_args;
2189 a2 = tp2->t_args;
2190
2191 while (a1 != NULL && a2 != NULL) {
2192
2193 if (eqtype(a1->s_type, a2->s_type, 1, 0, dowarn) == 0)
2194 return 0;
2195
2196 a1 = a1->s_nxt;
2197 a2 = a2->s_nxt;
2198
2199 }
2200
2201 return a1 == a2;
2202 }
2203
2204 /*
2205 * mnoarg() (matches functions with no argument type information)
2206 * returns 1 if all parameters of a prototype are compatible with
2207 * an old style function declaration.
2208 * This is the case if the following conditions are met:
2209 * 1. the prototype must have a fixed number of parameters
2210 * 2. no parameter is of type float
2211 * 3. no parameter is converted to another type if integer promotion
2212 * is applied on it
2213 */
2214 static int
2215 mnoarg(type_t *tp, int *dowarn)
2216 {
2217 sym_t *arg;
2218 tspec_t t;
2219
2220 if (tp->t_vararg) {
2221 if (dowarn != NULL)
2222 *dowarn = 1;
2223 }
2224 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) {
2225 if ((t = arg->s_type->t_tspec) == FLOAT ||
2226 t == CHAR || t == SCHAR || t == UCHAR ||
2227 t == SHORT || t == USHORT) {
2228 if (dowarn != NULL)
2229 *dowarn = 1;
2230 }
2231 }
2232 return 1;
2233 }
2234
2235 /*
2236 * Compares a prototype declaration with the remembered arguments of
2237 * a previous old style function definition.
2238 */
2239 static int
2240 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2241 {
2242 sym_t *args, *pargs, *arg, *parg;
2243 int narg, nparg, n;
2244 int dowarn, msg;
2245
2246 args = rdsym->s_args;
2247 pargs = dsym->s_type->t_args;
2248
2249 msg = 0;
2250
2251 narg = nparg = 0;
2252 for (arg = args; arg != NULL; arg = arg->s_nxt)
2253 narg++;
2254 for (parg = pargs; parg != NULL; parg = parg->s_nxt)
2255 nparg++;
2256 if (narg != nparg) {
2257 /* prototype does not match old-style definition */
2258 error(63);
2259 msg = 1;
2260 goto end;
2261 }
2262
2263 arg = args;
2264 parg = pargs;
2265 n = 1;
2266 while (narg--) {
2267 dowarn = 0;
2268 /*
2269 * If it does not match due to promotion and sflag is
2270 * not set we print only a warning.
2271 */
2272 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &dowarn) || dowarn) {
2273 /* prototype does not match old-style def., arg #%d */
2274 error(299, n);
2275 msg = 1;
2276 }
2277 arg = arg->s_nxt;
2278 parg = parg->s_nxt;
2279 n++;
2280 }
2281
2282 end:
2283 if (msg)
2284 /* old style definition */
2285 print_previous_declaration(300, rdsym);
2286
2287 return msg;
2288 }
2289
2290 /*
2291 * Completes a type by copying the dimension and prototype information
2292 * from a second compatible type.
2293 *
2294 * Following lines are legal:
2295 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2296 * "typedef ft(); ft f; f(int); ft g; g(long);"
2297 * This means that, if a type is completed, the type structure must
2298 * be duplicated.
2299 */
2300 void
2301 complete_type(sym_t *dsym, sym_t *ssym)
2302 {
2303 type_t **dstp, *src;
2304 type_t *dst;
2305
2306 dstp = &dsym->s_type;
2307 src = ssym->s_type;
2308
2309 while ((dst = *dstp) != NULL) {
2310 if (src == NULL || dst->t_tspec != src->t_tspec)
2311 LERROR("complete_type()");
2312 if (dst->t_tspec == ARRAY) {
2313 if (dst->t_dim == 0 && src->t_dim != 0) {
2314 *dstp = dst = duptyp(dst);
2315 dst->t_dim = src->t_dim;
2316 setcomplete(dst, 1);
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 check_function_definition(sym, 1);
2339
2340 check_type(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 mark_as_set(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 mark_as_set(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 |= check_prototype_declaration(arg, parg);
2502 parg = parg->s_nxt;
2503 arg = arg->s_nxt;
2504 }
2505 }
2506 if (msg)
2507 /* prototype declaration */
2508 print_previous_declaration(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 check_prototype_declaration(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 declarator_name(). */
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 check_function_definition(dsym, 1);
2596
2597 check_type(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 = check_init(dsym))) {
2678 dsym->s_def = DEF;
2679 mark_as_set(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 print_previous_declaration(-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 print_previous_declaration(-1, esym);
2731 } else {
2732 /* inconsistent redeclaration of static: %s */
2733 warning(92, dsym->s_name);
2734 print_previous_declaration(-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 check_init(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 abstract_name(void)
2784 {
2785 sym_t *sym;
2786
2787 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG)
2788 LERROR("abstract_name()");
2789
2790 sym = getblk(sizeof (sym_t));
2791
2792 sym->s_name = unnamed;
2793 sym->s_def = DEF;
2794 sym->s_scl = ABSTRACT;
2795 sym->s_blklev = -1;
2796
2797 if (dcs->d_ctx == PARG)
2798 sym->s_arg = 1;
2799
2800 sym->s_type = dcs->d_type;
2801 dcs->d_rdcsym = NULL;
2802 dcs->d_vararg = 0;
2803
2804 return sym;
2805 }
2806
2807 /*
2808 * Removes anything which has nothing to do on global level.
2809 */
2810 void
2811 global_clean_up(void)
2812 {
2813
2814 while (dcs->d_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 global_clean_up_decl(1);
2826 }
2827
2828 /*
2829 * Process an abstract type declaration
2830 */
2831 sym_t *
2832 declare_1_abstract(sym_t *sym)
2833 {
2834
2835 check_function_definition(sym, 1);
2836 check_type(sym);
2837 return sym;
2838 }
2839
2840 /*
2841 * Checks size after declarations of variables and their initialisation.
2842 */
2843 void
2844 check_size(sym_t *dsym)
2845 {
2846
2847 if (dsym->s_def != DEF)
2848 return;
2849 if (dsym->s_scl == TYPEDEF)
2850 return;
2851 if (dsym->s_type->t_tspec == FUNC)
2852 return;
2853
2854 if (length(dsym->s_type, dsym->s_name) == 0 &&
2855 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2856 /* 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 mark_as_set(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 mark_as_used(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 check_usage(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 check_usage_sym(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 check_usage_sym(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 check_argument_usage(novar, sym);
2944 } else {
2945 check_variable_usage(novar, sym);
2946 }
2947 } else if (sym->s_kind == FLAB) {
2948 check_label_usage(sym);
2949 } else if (sym->s_kind == FTAG) {
2950 check_tag_usage(sym);
2951 }
2952
2953 STRUCT_ASSIGN(curr_pos, cpos);
2954 }
2955
2956 static void
2957 check_argument_usage(int novar, sym_t *arg)
2958 {
2959
2960 if (!arg->s_set)
2961 LERROR("check_argument_usage()");
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 check_variable_usage(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("check_variable_usage()");
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 check_label_usage(sym_t *lab)
3042 {
3043
3044 if (blklev != 1 || lab->s_blklev != 1)
3045 LERROR("check_label_usage()");
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 check_tag_usage(sym_t *sym)
3060 {
3061
3062 if (!incompl(sym->s_type))
3063 return;
3064
3065 /* always complain about incomplete tags declared inside blocks */
3066 if (!zflag || dcs->d_ctx != EXTERN)
3067 return;
3068
3069 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("check_tag_usage()");
3085 }
3086 }
3087
3088 /*
3089 * Called after the entire translation unit has been parsed.
3090 * Changes tentative definitions into definitions.
3091 * Performs some tests on global symbols. Detected problems are:
3092 * - defined variables of incomplete type
3093 * - constant variables which are not initialized
3094 * - static symbols which are never used
3095 */
3096 void
3097 check_global_symbols(void)
3098 {
3099 sym_t *sym;
3100 pos_t cpos;
3101
3102 if (blklev != 0 || dcs->d_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 check_global_variable(sym);
3112 } else if (sym->s_kind == FTAG) {
3113 check_tag_usage(sym);
3114 } else {
3115 if (sym->s_kind != FMOS)
3116 LERROR("check_global_symbols()");
3117 }
3118 }
3119
3120 STRUCT_ASSIGN(curr_pos, cpos);
3121 }
3122
3123 static void
3124 check_global_variable(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("check_global_variable()");
3132
3133 check_global_variable_size(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 check_global_variable_size(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 print_previous_declaration(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