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