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