decl.c revision 1.152 1 /* $NetBSD: decl.c,v 1.152 2021/03/20 13:25:31 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.152 2021/03/20 13:25:31 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_fpsyms;
643 dcs->d_fpsyms = 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_nedecl = 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 scl_t sc;
1163
1164 lint_assert((sc = dsym->s_scl) == MOS || sc == MOU);
1165
1166 if (dcs->d_rdcsym != NULL) {
1167 /* should be ensured by storesym() */
1168 lint_assert((sc = dcs->d_rdcsym->s_scl) == MOS || sc == MOU);
1169 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1170 /* duplicate member name: %s */
1171 error(33, dsym->s_name);
1172 rmsym(dcs->d_rdcsym);
1173 }
1174 }
1175
1176 check_type(dsym);
1177
1178 t = (tp = dsym->s_type)->t_tspec;
1179
1180 if (dsym->s_bitfield) {
1181 declare_bit_field(dsym, &t, &tp);
1182 } else if (t == FUNC) {
1183 /* function illegal in structure or union */
1184 error(38);
1185 dsym->s_type = tp = incref(tp, t = PTR);
1186 }
1187
1188 /*
1189 * bit-fields of length 0 are not warned about because length()
1190 * does not return the length of the bit-field but the length
1191 * of the type the bit-field is packed in (it's ok)
1192 */
1193 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1194 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1195 /* zero sized array in struct is a C99 extension: %s */
1196 c99ism(39, dsym->s_name);
1197 }
1198 }
1199
1200 if (dcs->d_ctx == MOU) {
1201 o = dcs->d_offset;
1202 dcs->d_offset = 0;
1203 }
1204 if (dsym->s_bitfield) {
1205 align(alignment_in_bits(tp), tp->t_flen);
1206 dsym->s_value.v_quad =
1207 (dcs->d_offset / size_in_bits(t)) * size_in_bits(t);
1208 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1209 dcs->d_offset += tp->t_flen;
1210 } else {
1211 align(alignment_in_bits(tp), 0);
1212 dsym->s_value.v_quad = dcs->d_offset;
1213 dcs->d_offset += sz;
1214 }
1215 if (dcs->d_ctx == MOU) {
1216 if (o > dcs->d_offset)
1217 dcs->d_offset = o;
1218 }
1219
1220 check_function_definition(dsym, false);
1221
1222 /*
1223 * Clear the BITFIELDTYPE indicator after processing each
1224 * structure element.
1225 */
1226 bitfieldtype_ok = false;
1227
1228 return dsym;
1229 }
1230
1231 /*
1232 * Aligns next structure element as required.
1233 *
1234 * al contains the required alignment, len the length of a bit-field.
1235 */
1236 static void
1237 align(int al, int len)
1238 {
1239 int no;
1240
1241 /*
1242 * The alignment of the current element becomes the alignment of
1243 * the struct/union if it is larger than the current alignment
1244 * of the struct/union.
1245 */
1246 if (al > dcs->d_stralign)
1247 dcs->d_stralign = al;
1248
1249 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1250 if (len == 0 || dcs->d_offset + len > no)
1251 dcs->d_offset = no;
1252 }
1253
1254 /*
1255 * Remember the width of the field in its type structure.
1256 */
1257 sym_t *
1258 bitfield(sym_t *dsym, int len)
1259 {
1260
1261 if (dsym == NULL) {
1262 dsym = getblk(sizeof (sym_t));
1263 dsym->s_name = unnamed;
1264 dsym->s_kind = FMEMBER;
1265 dsym->s_scl = MOS;
1266 dsym->s_type = gettyp(UINT);
1267 dsym->s_block_level = -1;
1268 }
1269 dsym->s_type = duptyp(dsym->s_type);
1270 dsym->s_type->t_bitfield = true;
1271 dsym->s_type->t_flen = len;
1272 dsym->s_bitfield = true;
1273 return dsym;
1274 }
1275
1276 /*
1277 * Collect information about a sequence of asterisks and qualifiers in a
1278 * list of type pqinf_t.
1279 * Qualifiers always refer to the left asterisk.
1280 * The rightmost asterisk will be at the top of the list.
1281 */
1282 pqinf_t *
1283 merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2)
1284 {
1285 pqinf_t *p;
1286
1287 if (p2->p_pcnt != 0) {
1288 /* left '*' at the end of the list */
1289 for (p = p2; p->p_next != NULL; p = p->p_next)
1290 continue;
1291 p->p_next = p1;
1292 return p2;
1293 } else {
1294 if (p2->p_const) {
1295 if (p1->p_const) {
1296 /* duplicate '%s' */
1297 warning(10, "const");
1298 }
1299 p1->p_const = true;
1300 }
1301 if (p2->p_volatile) {
1302 if (p1->p_volatile) {
1303 /* duplicate '%s' */
1304 warning(10, "volatile");
1305 }
1306 p1->p_volatile = true;
1307 }
1308 free(p2);
1309 return p1;
1310 }
1311 }
1312
1313 /*
1314 * The following 3 functions extend the type of a declarator with
1315 * pointer, function and array types.
1316 *
1317 * The current type is the type built by deftyp() (dcs->d_type) and
1318 * pointer, function and array types already added for this
1319 * declarator. The new type extension is inserted between both.
1320 */
1321 sym_t *
1322 add_pointer(sym_t *decl, pqinf_t *pi)
1323 {
1324 type_t **tpp, *tp;
1325 pqinf_t *npi;
1326
1327 tpp = &decl->s_type;
1328 while (*tpp != NULL && *tpp != dcs->d_type)
1329 tpp = &(*tpp)->t_subt;
1330 if (*tpp == NULL)
1331 return decl;
1332
1333 while (pi != NULL) {
1334 *tpp = tp = getblk(sizeof (type_t));
1335 tp->t_tspec = PTR;
1336 tp->t_const = pi->p_const;
1337 tp->t_volatile = pi->p_volatile;
1338 *(tpp = &tp->t_subt) = dcs->d_type;
1339 npi = pi->p_next;
1340 free(pi);
1341 pi = npi;
1342 }
1343 return decl;
1344 }
1345
1346 /*
1347 * If a dimension was specified, dim is 1, otherwise 0
1348 * n is the specified dimension
1349 */
1350 sym_t *
1351 add_array(sym_t *decl, bool dim, int n)
1352 {
1353 type_t **tpp, *tp;
1354
1355 tpp = &decl->s_type;
1356 while (*tpp != NULL && *tpp != dcs->d_type)
1357 tpp = &(*tpp)->t_subt;
1358 if (*tpp == NULL)
1359 return decl;
1360
1361 *tpp = tp = getblk(sizeof (type_t));
1362 tp->t_tspec = ARRAY;
1363 tp->t_subt = dcs->d_type;
1364 tp->t_dim = n;
1365
1366 if (n < 0) {
1367 /* negative array dimension (%d) */
1368 error(20, n);
1369 n = 0;
1370 } else if (n == 0 && dim) {
1371 /* zero sized array is a C99 extension */
1372 c99ism(322);
1373 } else if (n == 0 && !dim) {
1374 setcomplete(tp, false);
1375 }
1376
1377 return decl;
1378 }
1379
1380 sym_t *
1381 add_function(sym_t *decl, sym_t *args)
1382 {
1383 type_t **tpp, *tp;
1384
1385 if (dcs->d_proto) {
1386 if (tflag)
1387 /* function prototypes are illegal in traditional C */
1388 warning(270);
1389 args = new_style_function(decl, args);
1390 } else {
1391 old_style_function(decl, args);
1392 }
1393
1394 /*
1395 * The symbols are removed from the symbol table by popdecl() after
1396 * add_function(). To be able to restore them if this is a function
1397 * definition, a pointer to the list of all symbols is stored in
1398 * dcs->d_next->d_fpsyms. Also a list of the arguments (concatenated
1399 * by s_next) is stored in dcs->d_next->d_fargs.
1400 * (dcs->d_next must be used because *dcs is the declaration stack
1401 * element created for the list of params and is removed after
1402 * add_function())
1403 */
1404 if (dcs->d_next->d_ctx == EXTERN &&
1405 decl->s_type == dcs->d_next->d_type) {
1406 dcs->d_next->d_fpsyms = dcs->d_dlsyms;
1407 dcs->d_next->d_fargs = args;
1408 }
1409
1410 tpp = &decl->s_type;
1411 while (*tpp != NULL && *tpp != dcs->d_next->d_type)
1412 tpp = &(*tpp)->t_subt;
1413 if (*tpp == NULL)
1414 return decl;
1415
1416 *tpp = tp = getblk(sizeof (type_t));
1417 tp->t_tspec = FUNC;
1418 tp->t_subt = dcs->d_next->d_type;
1419 if ((tp->t_proto = dcs->d_proto) != false)
1420 tp->t_args = args;
1421 tp->t_vararg = dcs->d_vararg;
1422
1423 return decl;
1424 }
1425
1426 /*
1427 * Called for new style function declarations.
1428 */
1429 /* ARGSUSED */
1430 static sym_t *
1431 new_style_function(sym_t *decl, sym_t *args)
1432 {
1433 sym_t *arg, *sym;
1434 scl_t sc;
1435 int n;
1436
1437 /*
1438 * Declarations of structs/unions/enums in param lists are legal,
1439 * but senseless.
1440 */
1441 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1442 sc = sym->s_scl;
1443 if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) {
1444 /* dubious tag declaration: %s %s */
1445 warning(85, storage_class_name(sc), sym->s_name);
1446 }
1447 }
1448
1449 n = 1;
1450 for (arg = args; arg != NULL; arg = arg->s_next) {
1451 if (arg->s_type->t_tspec == VOID) {
1452 if (n > 1 || arg->s_next != NULL) {
1453 /* void must be sole parameter */
1454 error(60);
1455 arg->s_type = gettyp(INT);
1456 }
1457 }
1458 n++;
1459 }
1460
1461 /* return NULL if first param is VOID */
1462 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL;
1463 }
1464
1465 /*
1466 * Called for old style function declarations.
1467 */
1468 static void
1469 old_style_function(sym_t *decl, sym_t *args)
1470 {
1471
1472 /*
1473 * Remember list of params only if this is really seams to be
1474 * a function definition.
1475 */
1476 if (dcs->d_next->d_ctx == EXTERN &&
1477 decl->s_type == dcs->d_next->d_type) {
1478 /*
1479 * We assume that this becomes a function definition. If
1480 * we are wrong, it's corrected in check_function_definition().
1481 */
1482 if (args != NULL) {
1483 decl->s_osdef = true;
1484 decl->s_args = args;
1485 }
1486 } else {
1487 if (args != NULL)
1488 /* function prototype parameters must have types */
1489 warning(62);
1490 }
1491 }
1492
1493 /*
1494 * Lists of identifiers in functions declarations are allowed only if
1495 * it's also a function definition. If this is not the case, print an
1496 * error message.
1497 */
1498 void
1499 check_function_definition(sym_t *sym, bool msg)
1500 {
1501
1502 if (sym->s_osdef) {
1503 if (msg) {
1504 /* incomplete or misplaced function definition */
1505 error(22);
1506 }
1507 sym->s_osdef = false;
1508 sym->s_args = NULL;
1509 }
1510 }
1511
1512 /*
1513 * Process the name in a declarator.
1514 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
1515 * TYPEDEF.
1516 * s_def and s_reg are valid after declarator_name().
1517 */
1518 sym_t *
1519 declarator_name(sym_t *sym)
1520 {
1521 scl_t sc = NOSCL;
1522
1523 if (sym->s_scl == NOSCL) {
1524 dcs->d_rdcsym = NULL;
1525 } else if (sym->s_defarg) {
1526 sym->s_defarg = false;
1527 dcs->d_rdcsym = NULL;
1528 } else {
1529 dcs->d_rdcsym = sym;
1530 sym = pushdown(sym);
1531 }
1532
1533 switch (dcs->d_ctx) {
1534 case MOS:
1535 case MOU:
1536 /* Set parent */
1537 sym->s_styp = dcs->d_tagtyp->t_str;
1538 sym->s_def = DEF;
1539 sym->s_value.v_tspec = INT;
1540 sc = dcs->d_ctx;
1541 break;
1542 case EXTERN:
1543 /*
1544 * static and external symbols without "extern" are
1545 * considered to be tentative defined, external
1546 * symbols with "extern" are declared, and typedef names
1547 * are defined. Tentative defined and declared symbols
1548 * may become defined if an initializer is present or
1549 * this is a function definition.
1550 */
1551 if ((sc = dcs->d_scl) == NOSCL) {
1552 sc = EXTERN;
1553 sym->s_def = TDEF;
1554 } else if (sc == STATIC) {
1555 sym->s_def = TDEF;
1556 } else if (sc == TYPEDEF) {
1557 sym->s_def = DEF;
1558 } else {
1559 lint_assert(sc == EXTERN);
1560 sym->s_def = DECL;
1561 }
1562 break;
1563 case PROTO_ARG:
1564 sym->s_arg = true;
1565 /* FALLTHROUGH */
1566 case ARG:
1567 if ((sc = dcs->d_scl) == NOSCL) {
1568 sc = AUTO;
1569 } else {
1570 lint_assert(sc == REG);
1571 sym->s_reg = true;
1572 sc = AUTO;
1573 }
1574 sym->s_def = DEF;
1575 break;
1576 case AUTO:
1577 if ((sc = dcs->d_scl) == NOSCL) {
1578 /*
1579 * XXX somewhat ugly because we dont know whether
1580 * this is AUTO or EXTERN (functions). If we are
1581 * wrong it must be corrected in declare_local(),
1582 * where we have the necessary type information.
1583 */
1584 sc = AUTO;
1585 sym->s_def = DEF;
1586 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1587 sym->s_def = DEF;
1588 } else if (sc == REG) {
1589 sym->s_reg = true;
1590 sc = AUTO;
1591 sym->s_def = DEF;
1592 } else {
1593 lint_assert(sc == EXTERN);
1594 sym->s_def = DECL;
1595 }
1596 break;
1597 default:
1598 lint_assert(/*CONSTCOND*/false);
1599 }
1600 sym->s_scl = sc;
1601
1602 sym->s_type = dcs->d_type;
1603
1604 dcs->d_fpsyms = NULL;
1605
1606 return sym;
1607 }
1608
1609 /*
1610 * Process a name in the list of formal parameters in an old style function
1611 * definition.
1612 */
1613 sym_t *
1614 old_style_function_name(sym_t *sym)
1615 {
1616
1617 if (sym->s_scl != NOSCL) {
1618 if (block_level == sym->s_block_level) {
1619 /* redeclaration of formal parameter %s */
1620 error(21, sym->s_name);
1621 lint_assert(sym->s_defarg);
1622 }
1623 sym = pushdown(sym);
1624 }
1625 sym->s_type = gettyp(INT);
1626 sym->s_scl = AUTO;
1627 sym->s_def = DEF;
1628 sym->s_defarg = sym->s_arg = true;
1629 return sym;
1630 }
1631
1632 /*
1633 * Create the type of a tag.
1634 *
1635 * tag points to the symbol table entry of the tag
1636 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1637 * decl is true if the type of the tag will be completed in this declaration
1638 * (the following token is T_LBRACE)
1639 * semi is true if the following token is T_SEMI
1640 */
1641 type_t *
1642 mktag(sym_t *tag, tspec_t kind, bool decl, bool semi)
1643 {
1644 scl_t scl;
1645 type_t *tp;
1646
1647 if (kind == STRUCT) {
1648 scl = STRUCT_TAG;
1649 } else if (kind == UNION) {
1650 scl = UNION_TAG;
1651 } else {
1652 lint_assert(kind == ENUM);
1653 scl = ENUM_TAG;
1654 }
1655
1656 if (tag != NULL) {
1657 if (tag->s_scl != NOSCL) {
1658 tag = newtag(tag, scl, decl, semi);
1659 } else {
1660 /* a new tag, no empty declaration */
1661 dcs->d_next->d_nedecl = true;
1662 if (scl == ENUM_TAG && !decl) {
1663 if (!tflag && (sflag || pflag))
1664 /* forward reference to enum type */
1665 warning(42);
1666 }
1667 }
1668 if (tag->s_scl == NOSCL) {
1669 tag->s_scl = scl;
1670 tag->s_type = tp = getblk(sizeof (type_t));
1671 tp->t_packed = dcs->d_packed;
1672 } else {
1673 tp = tag->s_type;
1674 }
1675 } else {
1676 tag = getblk(sizeof (sym_t));
1677 tag->s_name = unnamed;
1678 UNIQUE_CURR_POS(tag->s_def_pos);
1679 tag->s_kind = FTAG;
1680 tag->s_scl = scl;
1681 tag->s_block_level = -1;
1682 tag->s_type = tp = getblk(sizeof (type_t));
1683 tp->t_packed = dcs->d_packed;
1684 dcs->d_next->d_nedecl = true;
1685 }
1686
1687 if (tp->t_tspec == NOTSPEC) {
1688 tp->t_tspec = kind;
1689 if (kind != ENUM) {
1690 tp->t_str = getblk(sizeof (struct_or_union));
1691 tp->t_str->sou_align_in_bits = CHAR_SIZE;
1692 tp->t_str->sou_tag = tag;
1693 } else {
1694 tp->t_is_enum = true;
1695 tp->t_enum = getblk(sizeof(*tp->t_enum));
1696 tp->t_enum->en_tag = tag;
1697 }
1698 setcomplete(tp, false);
1699 }
1700 return tp;
1701 }
1702
1703 /*
1704 * Checks all possible cases of tag redeclarations.
1705 * decl is true if T_LBRACE follows
1706 * semi is true if T_SEMI follows
1707 */
1708 static sym_t *
1709 newtag(sym_t *tag, scl_t scl, bool decl, bool semi)
1710 {
1711
1712 if (tag->s_block_level < block_level) {
1713 if (semi) {
1714 /* "struct a;" */
1715 if (!tflag) {
1716 if (!sflag)
1717 /* decl. introduces new type ... */
1718 warning(44, storage_class_name(scl),
1719 tag->s_name);
1720 tag = pushdown(tag);
1721 } else if (tag->s_scl != scl) {
1722 /* base type is really '%s %s' */
1723 warning(45, storage_class_name(tag->s_scl),
1724 tag->s_name);
1725 }
1726 dcs->d_next->d_nedecl = true;
1727 } else if (decl) {
1728 /* "struct a { ... } " */
1729 if (hflag)
1730 /* redefinition hides earlier one: %s */
1731 warning(43, tag->s_name);
1732 tag = pushdown(tag);
1733 dcs->d_next->d_nedecl = true;
1734 } else if (tag->s_scl != scl) {
1735 /* base type is really '%s %s' */
1736 warning(45, storage_class_name(tag->s_scl),
1737 tag->s_name);
1738 /* declaration introduces new type in ANSI C: %s %s */
1739 if (!sflag) {
1740 /* decl. introduces new type in ANSI C: %s %s */
1741 warning(44, storage_class_name(scl),
1742 tag->s_name);
1743 }
1744 tag = pushdown(tag);
1745 dcs->d_next->d_nedecl = true;
1746 }
1747 } else {
1748 if (tag->s_scl != scl) {
1749 /* (%s) tag redeclared */
1750 error(46, storage_class_name(tag->s_scl));
1751 print_previous_declaration(-1, tag);
1752 tag = pushdown(tag);
1753 dcs->d_next->d_nedecl = true;
1754 } else if (decl && !is_incomplete(tag->s_type)) {
1755 /* (%s) tag redeclared */
1756 error(46, storage_class_name(tag->s_scl));
1757 print_previous_declaration(-1, tag);
1758 tag = pushdown(tag);
1759 dcs->d_next->d_nedecl = true;
1760 } else if (semi || decl) {
1761 dcs->d_next->d_nedecl = true;
1762 }
1763 }
1764 return tag;
1765 }
1766
1767 const char *
1768 storage_class_name(scl_t sc)
1769 {
1770 const char *s;
1771
1772 switch (sc) {
1773 case EXTERN: s = "extern"; break;
1774 case STATIC: s = "static"; break;
1775 case AUTO: s = "auto"; break;
1776 case REG: s = "register"; break;
1777 case TYPEDEF: s = "typedef"; break;
1778 case STRUCT_TAG:s = "struct"; break;
1779 case UNION_TAG: s = "union"; break;
1780 case ENUM_TAG: s = "enum"; break;
1781 default: lint_assert(/*CONSTCOND*/false);
1782 }
1783 return s;
1784 }
1785
1786 /*
1787 * tp points to the type of the tag, fmem to the list of members.
1788 */
1789 type_t *
1790 complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1791 {
1792 tspec_t t;
1793 struct_or_union *sp;
1794 int n;
1795 sym_t *mem;
1796
1797 setcomplete(tp, true);
1798
1799 t = tp->t_tspec;
1800 align(dcs->d_stralign, 0);
1801 sp = tp->t_str;
1802 sp->sou_align_in_bits = dcs->d_stralign;
1803 sp->sou_first_member = fmem;
1804 if (tp->t_packed)
1805 setpackedsize(tp);
1806 else
1807 sp->sou_size_in_bits = dcs->d_offset;
1808
1809 if (sp->sou_size_in_bits == 0) {
1810 /* zero sized %s is a C9X feature */
1811 c99ism(47, ttab[t].tt_name);
1812 }
1813
1814 n = 0;
1815 for (mem = fmem; mem != NULL; mem = mem->s_next) {
1816 /* bind anonymous members to the structure */
1817 if (mem->s_styp == NULL) {
1818 mem->s_styp = sp;
1819 if (mem->s_type->t_bitfield) {
1820 sp->sou_size_in_bits += bitfieldsize(&mem);
1821 if (mem == NULL)
1822 break;
1823 }
1824 sp->sou_size_in_bits += type_size_in_bits(mem->s_type);
1825 }
1826 if (mem->s_name != unnamed)
1827 n++;
1828 }
1829
1830 if (n == 0 && sp->sou_size_in_bits != 0) {
1831 /* %s has no named members */
1832 warning(65, t == STRUCT ? "structure" : "union");
1833 }
1834 return tp;
1835 }
1836
1837 type_t *
1838 complete_tag_enum(type_t *tp, sym_t *fmem)
1839 {
1840
1841 setcomplete(tp, true);
1842 tp->t_enum->en_first_enumerator = fmem;
1843 return tp;
1844 }
1845
1846 /*
1847 * Processes the name of an enumerator in an enum declaration.
1848 *
1849 * sym points to the enumerator
1850 * val is the value of the enumerator
1851 * impl is true if the value of the enumerator was not explicitly specified.
1852 */
1853 sym_t *
1854 enumeration_constant(sym_t *sym, int val, bool impl)
1855 {
1856
1857 if (sym->s_scl != NOSCL) {
1858 if (sym->s_block_level == block_level) {
1859 /* no hflag, because this is illegal!!! */
1860 if (sym->s_arg) {
1861 /* enumeration constant hides parameter: %s */
1862 warning(57, sym->s_name);
1863 } else {
1864 /* redeclaration of %s */
1865 error(27, sym->s_name);
1866 /*
1867 * inside blocks it should not be too
1868 * complicated to find the position of the
1869 * previous declaration
1870 */
1871 if (block_level == 0)
1872 print_previous_declaration(-1, sym);
1873 }
1874 } else {
1875 if (hflag)
1876 /* redefinition hides earlier one: %s */
1877 warning(43, sym->s_name);
1878 }
1879 sym = pushdown(sym);
1880 }
1881 sym->s_scl = CTCONST;
1882 sym->s_type = dcs->d_tagtyp;
1883 sym->s_value.v_tspec = INT;
1884 sym->s_value.v_quad = val;
1885 if (impl && val - 1 == TARG_INT_MAX) {
1886 /* overflow in enumeration values: %s */
1887 warning(48, sym->s_name);
1888 }
1889 enumval = val + 1;
1890 return sym;
1891 }
1892
1893 void
1894 declare(sym_t *decl, bool initflg, sbuf_t *renaming)
1895 {
1896 char *s;
1897
1898 initerr = false;
1899 initsym = decl;
1900
1901 switch (dcs->d_ctx) {
1902 case EXTERN:
1903 if (renaming != NULL) {
1904 lint_assert(decl->s_rename == NULL);
1905
1906 s = getlblk(1, renaming->sb_len + 1);
1907 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1908 decl->s_rename = s;
1909 }
1910 decl1ext(decl, initflg);
1911 break;
1912 case ARG:
1913 if (renaming != NULL) {
1914 /* symbol renaming can't be used on function arguments */
1915 error(310);
1916 break;
1917 }
1918 (void)declare_argument(decl, initflg);
1919 break;
1920 default:
1921 lint_assert(dcs->d_ctx == AUTO);
1922 if (renaming != NULL) {
1923 /* symbol renaming can't be used on automatic variables */
1924 error(311);
1925 break;
1926 }
1927 declare_local(decl, initflg);
1928 break;
1929 }
1930
1931 if (initflg && !initerr)
1932 initstack_init();
1933 }
1934
1935 /*
1936 * Process a single external declarator.
1937 */
1938 void
1939 decl1ext(sym_t *dsym, bool initflg)
1940 {
1941 bool dowarn, rval, redec;
1942 sym_t *rdsym;
1943
1944 check_function_definition(dsym, true);
1945
1946 check_type(dsym);
1947
1948 if (initflg && !(initerr = check_init(dsym)))
1949 dsym->s_def = DEF;
1950
1951 /*
1952 * Declarations of functions are marked as "tentative" in
1953 * declarator_name(). This is wrong because there are no
1954 * tentative function definitions.
1955 */
1956 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1957 dsym->s_def = DECL;
1958
1959 if (dcs->d_inline) {
1960 if (dsym->s_type->t_tspec == FUNC) {
1961 dsym->s_inline = true;
1962 } else {
1963 /* variable declared inline: %s */
1964 warning(268, dsym->s_name);
1965 }
1966 }
1967
1968 /* Write the declaration into the output file */
1969 if (plibflg && llibflg &&
1970 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1971 /*
1972 * With both LINTLIBRARY and PROTOLIB the prototype is
1973 * written as a function definition to the output file.
1974 */
1975 rval = dsym->s_type->t_subt->t_tspec != VOID;
1976 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1977 } else {
1978 outsym(dsym, dsym->s_scl, dsym->s_def);
1979 }
1980
1981 if ((rdsym = dcs->d_rdcsym) != NULL) {
1982
1983 /*
1984 * If the old symbol stems from an old style function
1985 * definition, we have remembered the params in rdsmy->s_args
1986 * and compare them with the params of the prototype.
1987 */
1988 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1989 redec = check_old_style_definition(rdsym, dsym);
1990 } else {
1991 redec = false;
1992 }
1993
1994 if (!redec &&
1995 !check_redeclaration(dsym, (dowarn = false, &dowarn))) {
1996
1997 if (dowarn) {
1998 if (sflag)
1999 /* redeclaration of %s */
2000 error(27, dsym->s_name);
2001 else
2002 /* redeclaration of %s */
2003 warning(27, dsym->s_name);
2004 print_previous_declaration(-1, rdsym);
2005 }
2006
2007 /*
2008 * Take over the remembered params if the new symbol
2009 * is not a prototype.
2010 */
2011 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
2012 dsym->s_osdef = rdsym->s_osdef;
2013 dsym->s_args = rdsym->s_args;
2014 dsym->s_def_pos = rdsym->s_def_pos;
2015 }
2016
2017 /*
2018 * Remember the position of the declaration if the
2019 * old symbol was a prototype and the new is not.
2020 * Also remember the position if the old symbol
2021 * was defined and the new is not.
2022 */
2023 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
2024 dsym->s_def_pos = rdsym->s_def_pos;
2025 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
2026 dsym->s_def_pos = rdsym->s_def_pos;
2027 }
2028
2029 /*
2030 * Copy usage information of the name into the new
2031 * symbol.
2032 */
2033 copy_usage_info(dsym, rdsym);
2034
2035 /* Once a name is defined, it remains defined. */
2036 if (rdsym->s_def == DEF)
2037 dsym->s_def = DEF;
2038
2039 /* once a function is inline, it remains inline */
2040 if (rdsym->s_inline)
2041 dsym->s_inline = true;
2042
2043 complete_type(dsym, rdsym);
2044
2045 }
2046
2047 rmsym(rdsym);
2048 }
2049
2050 if (dsym->s_scl == TYPEDEF) {
2051 dsym->s_type = duptyp(dsym->s_type);
2052 dsym->s_type->t_typedef = true;
2053 settdsym(dsym->s_type, dsym);
2054 }
2055
2056 }
2057
2058 /*
2059 * Copies information about usage into a new symbol table entry of
2060 * the same symbol.
2061 */
2062 void
2063 copy_usage_info(sym_t *sym, sym_t *rdsym)
2064 {
2065
2066 sym->s_set_pos = rdsym->s_set_pos;
2067 sym->s_use_pos = rdsym->s_use_pos;
2068 sym->s_set = rdsym->s_set;
2069 sym->s_used = rdsym->s_used;
2070 }
2071
2072 /*
2073 * Prints an error and returns true if a symbol is redeclared/redefined.
2074 * Otherwise returns false and, in some cases of minor problems, prints
2075 * a warning.
2076 */
2077 bool
2078 check_redeclaration(sym_t *dsym, bool *dowarn)
2079 {
2080 sym_t *rsym;
2081
2082 if ((rsym = dcs->d_rdcsym)->s_scl == CTCONST) {
2083 /* redeclaration of %s */
2084 error(27, dsym->s_name);
2085 print_previous_declaration(-1, rsym);
2086 return true;
2087 }
2088 if (rsym->s_scl == TYPEDEF) {
2089 /* typedef redeclared: %s */
2090 error(89, dsym->s_name);
2091 print_previous_declaration(-1, rsym);
2092 return true;
2093 }
2094 if (dsym->s_scl == TYPEDEF) {
2095 /* redeclaration of %s */
2096 error(27, dsym->s_name);
2097 print_previous_declaration(-1, rsym);
2098 return true;
2099 }
2100 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2101 /* redefinition of %s */
2102 error(28, dsym->s_name);
2103 print_previous_declaration(-1, rsym);
2104 return true;
2105 }
2106 if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) {
2107 /* redeclaration of %s */
2108 error(27, dsym->s_name);
2109 print_previous_declaration(-1, rsym);
2110 return true;
2111 }
2112 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2113 return false;
2114 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2115 return false;
2116 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2117 return false;
2118 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2119 /*
2120 * All cases except "int a = 1; static int a;" are caught
2121 * above with or without a warning
2122 */
2123 /* redeclaration of %s */
2124 error(27, dsym->s_name);
2125 print_previous_declaration(-1, rsym);
2126 return true;
2127 }
2128 if (rsym->s_scl == EXTERN) {
2129 /* previously declared extern, becomes static: %s */
2130 warning(29, dsym->s_name);
2131 print_previous_declaration(-1, rsym);
2132 return false;
2133 }
2134 /*
2135 * Now it's one of:
2136 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2137 */
2138 /* redeclaration of %s; ANSI C requires "static" */
2139 if (sflag) {
2140 /* redeclaration of %s; ANSI C requires static */
2141 warning(30, dsym->s_name);
2142 print_previous_declaration(-1, rsym);
2143 }
2144 dsym->s_scl = STATIC;
2145 return false;
2146 }
2147
2148 static bool
2149 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
2150 {
2151 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2152 return false;
2153
2154 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2155 return false;
2156
2157 return true;
2158 }
2159
2160 bool
2161 eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual)
2162 {
2163 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2164 return false;
2165
2166 if (!qualifiers_correspond(tp1, tp2, ignqual))
2167 return false;
2168
2169 return true;
2170 }
2171
2172
2173 /*
2174 * Checks if two types are compatible.
2175 *
2176 * ignqual ignore qualifiers of type; used for function params
2177 * promot promote left type; used for comparison of params of
2178 * old style function definitions with params of prototypes.
2179 * *dowarn set to true if an old style function declaration is not
2180 * compatible with a prototype
2181 */
2182 bool
2183 eqtype(const type_t *tp1, const type_t *tp2,
2184 bool ignqual, bool promot, bool *dowarn)
2185 {
2186 tspec_t t;
2187
2188 while (tp1 != NULL && tp2 != NULL) {
2189
2190 t = tp1->t_tspec;
2191 if (promot) {
2192 if (t == FLOAT) {
2193 t = DOUBLE;
2194 } else if (t == CHAR || t == SCHAR) {
2195 t = INT;
2196 } else if (t == UCHAR) {
2197 t = tflag ? UINT : INT;
2198 } else if (t == SHORT) {
2199 t = INT;
2200 } else if (t == USHORT) {
2201 /* CONSTCOND */
2202 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
2203 }
2204 }
2205
2206 if (t != tp2->t_tspec)
2207 return false;
2208
2209 if (!qualifiers_correspond(tp1, tp2, ignqual))
2210 return false;
2211
2212 if (t == STRUCT || t == UNION)
2213 return tp1->t_str == tp2->t_str;
2214
2215 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2216 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2217 return false;
2218 }
2219
2220 /* dont check prototypes for traditional */
2221 if (t == FUNC && !tflag) {
2222 if (tp1->t_proto && tp2->t_proto) {
2223 if (!eqargs(tp1, tp2, dowarn))
2224 return false;
2225 } else if (tp1->t_proto) {
2226 if (!mnoarg(tp1, dowarn))
2227 return false;
2228 } else if (tp2->t_proto) {
2229 if (!mnoarg(tp2, dowarn))
2230 return false;
2231 }
2232 }
2233
2234 tp1 = tp1->t_subt;
2235 tp2 = tp2->t_subt;
2236 ignqual = promot = false;
2237
2238 }
2239
2240 return tp1 == tp2;
2241 }
2242
2243 /*
2244 * Compares the parameter types of two prototypes.
2245 */
2246 static bool
2247 eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn)
2248 {
2249 sym_t *a1, *a2;
2250
2251 if (tp1->t_vararg != tp2->t_vararg)
2252 return false;
2253
2254 a1 = tp1->t_args;
2255 a2 = tp2->t_args;
2256
2257 while (a1 != NULL && a2 != NULL) {
2258
2259 if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn))
2260 return false;
2261
2262 a1 = a1->s_next;
2263 a2 = a2->s_next;
2264
2265 }
2266
2267 return a1 == a2;
2268 }
2269
2270 /*
2271 * mnoarg() (matches functions with no argument type information)
2272 * returns whether all parameters of a prototype are compatible with
2273 * an old style function declaration.
2274 * This is the case if the following conditions are met:
2275 * 1. the prototype has a fixed number of parameters
2276 * 2. no parameter is of type float
2277 * 3. no parameter is converted to another type if integer promotion
2278 * is applied on it
2279 */
2280 static bool
2281 mnoarg(const type_t *tp, bool *dowarn)
2282 {
2283 sym_t *arg;
2284 tspec_t t;
2285
2286 if (tp->t_vararg) {
2287 if (dowarn != NULL)
2288 *dowarn = true;
2289 }
2290 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2291 if ((t = arg->s_type->t_tspec) == FLOAT ||
2292 t == CHAR || t == SCHAR || t == UCHAR ||
2293 t == SHORT || t == USHORT) {
2294 if (dowarn != NULL)
2295 *dowarn = true;
2296 }
2297 }
2298 return true;
2299 }
2300
2301 /*
2302 * Compares a prototype declaration with the remembered arguments of
2303 * a previous old style function definition.
2304 */
2305 static bool
2306 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2307 {
2308 sym_t *args, *pargs, *arg, *parg;
2309 int narg, nparg, n;
2310 bool dowarn, msg;
2311
2312 args = rdsym->s_args;
2313 pargs = dsym->s_type->t_args;
2314
2315 msg = false;
2316
2317 narg = nparg = 0;
2318 for (arg = args; arg != NULL; arg = arg->s_next)
2319 narg++;
2320 for (parg = pargs; parg != NULL; parg = parg->s_next)
2321 nparg++;
2322 if (narg != nparg) {
2323 /* prototype does not match old-style definition */
2324 error(63);
2325 msg = true;
2326 goto end;
2327 }
2328
2329 arg = args;
2330 parg = pargs;
2331 n = 1;
2332 while (narg-- > 0) {
2333 dowarn = false;
2334 /*
2335 * If it does not match due to promotion and sflag is
2336 * not set we print only a warning.
2337 */
2338 if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) ||
2339 dowarn) {
2340 /* prototype does not match old style defn., arg #%d */
2341 error(299, n);
2342 msg = true;
2343 }
2344 arg = arg->s_next;
2345 parg = parg->s_next;
2346 n++;
2347 }
2348
2349 end:
2350 if (msg)
2351 /* old style definition */
2352 print_previous_declaration(300, rdsym);
2353
2354 return msg;
2355 }
2356
2357 /*
2358 * Completes a type by copying the dimension and prototype information
2359 * from a second compatible type.
2360 *
2361 * Following lines are legal:
2362 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2363 * "typedef ft(); ft f; f(int); ft g; g(long);"
2364 * This means that, if a type is completed, the type structure must
2365 * be duplicated.
2366 */
2367 void
2368 complete_type(sym_t *dsym, sym_t *ssym)
2369 {
2370 type_t **dstp, *src;
2371 type_t *dst;
2372
2373 dstp = &dsym->s_type;
2374 src = ssym->s_type;
2375
2376 while ((dst = *dstp) != NULL) {
2377 lint_assert(src != NULL);
2378 lint_assert(dst->t_tspec == src->t_tspec);
2379 if (dst->t_tspec == ARRAY) {
2380 if (dst->t_dim == 0 && src->t_dim != 0) {
2381 *dstp = dst = duptyp(dst);
2382 dst->t_dim = src->t_dim;
2383 setcomplete(dst, true);
2384 }
2385 } else if (dst->t_tspec == FUNC) {
2386 if (!dst->t_proto && src->t_proto) {
2387 *dstp = dst = duptyp(dst);
2388 dst->t_proto = true;
2389 dst->t_args = src->t_args;
2390 }
2391 }
2392 dstp = &dst->t_subt;
2393 src = src->t_subt;
2394 }
2395 }
2396
2397 /*
2398 * Completes the declaration of a single argument.
2399 */
2400 sym_t *
2401 declare_argument(sym_t *sym, bool initflg)
2402 {
2403 tspec_t t;
2404
2405 check_function_definition(sym, true);
2406
2407 check_type(sym);
2408
2409 if (dcs->d_rdcsym != NULL &&
2410 dcs->d_rdcsym->s_block_level == block_level) {
2411 /* redeclaration of formal parameter %s */
2412 error(237, sym->s_name);
2413 rmsym(dcs->d_rdcsym);
2414 sym->s_arg = true;
2415 }
2416
2417 if (!sym->s_arg) {
2418 /* declared argument %s is missing */
2419 error(53, sym->s_name);
2420 sym->s_arg = true;
2421 }
2422
2423 if (initflg) {
2424 /* cannot initialize parameter: %s */
2425 error(52, sym->s_name);
2426 initerr = true;
2427 }
2428
2429 if ((t = sym->s_type->t_tspec) == ARRAY) {
2430 sym->s_type = incref(sym->s_type->t_subt, PTR);
2431 } else if (t == FUNC) {
2432 if (tflag)
2433 /* a function is declared as an argument: %s */
2434 warning(50, sym->s_name);
2435 sym->s_type = incref(sym->s_type, PTR);
2436 } else if (t == FLOAT) {
2437 if (tflag)
2438 sym->s_type = gettyp(DOUBLE);
2439 }
2440
2441 if (dcs->d_inline)
2442 /* argument declared inline: %s */
2443 warning(269, sym->s_name);
2444
2445 /*
2446 * Arguments must have complete types. lengths() prints the needed
2447 * error messages (null dimension is impossible because arrays are
2448 * converted to pointers).
2449 */
2450 if (sym->s_type->t_tspec != VOID)
2451 (void)length(sym->s_type, sym->s_name);
2452
2453 sym->s_used = dcs->d_used;
2454 mark_as_set(sym);
2455
2456 return sym;
2457 }
2458
2459 void
2460 check_func_lint_directives(void)
2461 {
2462 sym_t *arg;
2463 int narg, n;
2464 tspec_t t;
2465
2466 /* check for illegal combinations of lint directives */
2467 if (printflike_argnum != -1 && scanflike_argnum != -1) {
2468 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2469 warning(289);
2470 printflike_argnum = scanflike_argnum = -1;
2471 }
2472 if (nvararg != -1 &&
2473 (printflike_argnum != -1 || scanflike_argnum != -1)) {
2474 /* dubious use of ** VARARGS ** with ** %s ** */
2475 warning(288,
2476 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2477 nvararg = -1;
2478 }
2479
2480 /*
2481 * check if the argument of a lint directive is compatible with the
2482 * number of arguments.
2483 */
2484 narg = 0;
2485 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_next)
2486 narg++;
2487 if (nargusg > narg) {
2488 /* argument number mismatch with directive: ** %s ** */
2489 warning(283, "ARGSUSED");
2490 nargusg = 0;
2491 }
2492 if (nvararg > narg) {
2493 /* argument number mismatch with directive: ** %s ** */
2494 warning(283, "VARARGS");
2495 nvararg = 0;
2496 }
2497 if (printflike_argnum > narg) {
2498 /* argument number mismatch with directive: ** %s ** */
2499 warning(283, "PRINTFLIKE");
2500 printflike_argnum = -1;
2501 } else if (printflike_argnum == 0) {
2502 printflike_argnum = -1;
2503 }
2504 if (scanflike_argnum > narg) {
2505 /* argument number mismatch with directive: ** %s ** */
2506 warning(283, "SCANFLIKE");
2507 scanflike_argnum = -1;
2508 } else if (scanflike_argnum == 0) {
2509 scanflike_argnum = -1;
2510 }
2511 if (printflike_argnum != -1 || scanflike_argnum != -1) {
2512 narg = printflike_argnum != -1
2513 ? printflike_argnum : scanflike_argnum;
2514 arg = dcs->d_fargs;
2515 for (n = 1; n < narg; n++)
2516 arg = arg->s_next;
2517 if (arg->s_type->t_tspec != PTR ||
2518 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2519 t != UCHAR && t != SCHAR)) {
2520 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2521 warning(293, narg);
2522 printflike_argnum = scanflike_argnum = -1;
2523 }
2524 }
2525 }
2526
2527 /*
2528 * Warn about arguments in old style function definitions that default to int.
2529 * Check that an old style function definition is compatible to a previous
2530 * prototype.
2531 */
2532 void
2533 check_func_old_style_arguments(void)
2534 {
2535 sym_t *args, *arg, *pargs, *parg;
2536 int narg, nparg;
2537 bool msg;
2538
2539 args = funcsym->s_args;
2540 pargs = funcsym->s_type->t_args;
2541
2542 /*
2543 * print a warning for each argument of an old style function
2544 * definition which defaults to int
2545 */
2546 for (arg = args; arg != NULL; arg = arg->s_next) {
2547 if (arg->s_defarg) {
2548 /* argument type defaults to 'int': %s */
2549 warning(32, arg->s_name);
2550 arg->s_defarg = false;
2551 mark_as_set(arg);
2552 }
2553 }
2554
2555 /*
2556 * If this is an old style function definition and a prototype
2557 * exists, compare the types of arguments.
2558 */
2559 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2560 /*
2561 * If the number of arguments does not match, we need not
2562 * continue.
2563 */
2564 narg = nparg = 0;
2565 msg = false;
2566 for (parg = pargs; parg != NULL; parg = parg->s_next)
2567 nparg++;
2568 for (arg = args; arg != NULL; arg = arg->s_next)
2569 narg++;
2570 if (narg != nparg) {
2571 /* parameter mismatch: %d declared, %d defined */
2572 error(51, nparg, narg);
2573 msg = true;
2574 } else {
2575 parg = pargs;
2576 arg = args;
2577 while (narg-- > 0) {
2578 msg |= check_prototype_declaration(arg, parg);
2579 parg = parg->s_next;
2580 arg = arg->s_next;
2581 }
2582 }
2583 if (msg)
2584 /* prototype declaration */
2585 print_previous_declaration(285, dcs->d_rdcsym);
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_rdcsym != 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_rdcsym != NULL) {
2695
2696 if (dcs->d_rdcsym->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_rdcsym->s_block_level == block_level) {
2725
2726 /* no hflag, because it's illegal! */
2727 if (dcs->d_rdcsym->s_arg) {
2728 /*
2729 * if !tflag, a "redeclaration of %s" error
2730 * is produced below
2731 */
2732 if (tflag) {
2733 if (hflag)
2734 /* decl. hides parameter: %s */
2735 warning(91, dsym->s_name);
2736 rmsym(dcs->d_rdcsym);
2737 }
2738 }
2739
2740 } else if (dcs->d_rdcsym->s_block_level < block_level) {
2741
2742 if (hflag)
2743 /* declaration hides earlier one: %s */
2744 warning(95, dsym->s_name);
2745
2746 }
2747
2748 if (dcs->d_rdcsym->s_block_level == block_level) {
2749
2750 /* redeclaration of %s */
2751 error(27, dsym->s_name);
2752 rmsym(dcs->d_rdcsym);
2753
2754 }
2755
2756 }
2757
2758 if (initflg && !(initerr = check_init(dsym))) {
2759 dsym->s_def = DEF;
2760 mark_as_set(dsym);
2761 }
2762
2763 if (dsym->s_scl == TYPEDEF) {
2764 dsym->s_type = duptyp(dsym->s_type);
2765 dsym->s_type->t_typedef = true;
2766 settdsym(dsym->s_type, dsym);
2767 }
2768
2769 /*
2770 * Before we can check the size we must wait for a initialization
2771 * which may follow.
2772 */
2773 }
2774
2775 /*
2776 * Processes (re)declarations of external symbols inside blocks.
2777 */
2778 static void
2779 declare_external_in_block(sym_t *dsym)
2780 {
2781 bool eqt, dowarn;
2782 sym_t *esym;
2783
2784 /* look for a symbol with the same name */
2785 esym = dcs->d_rdcsym;
2786 while (esym != NULL && esym->s_block_level != 0) {
2787 while ((esym = esym->s_link) != NULL) {
2788 if (esym->s_kind != FVFT)
2789 continue;
2790 if (strcmp(dsym->s_name, esym->s_name) == 0)
2791 break;
2792 }
2793 }
2794 if (esym == NULL)
2795 return;
2796 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2797 /* gcc accepts this without a warning, pcc prints an error. */
2798 /* redeclaration of %s */
2799 warning(27, dsym->s_name);
2800 print_previous_declaration(-1, esym);
2801 return;
2802 }
2803
2804 dowarn = false;
2805 eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn);
2806
2807 if (!eqt || dowarn) {
2808 if (esym->s_scl == EXTERN) {
2809 /* inconsistent redeclaration of extern: %s */
2810 warning(90, dsym->s_name);
2811 print_previous_declaration(-1, esym);
2812 } else {
2813 /* inconsistent redeclaration of static: %s */
2814 warning(92, dsym->s_name);
2815 print_previous_declaration(-1, esym);
2816 }
2817 }
2818
2819 if (eqt) {
2820 /*
2821 * Remember the external symbol so we can update usage
2822 * information at the end of the block.
2823 */
2824 dsym->s_ext_sym = esym;
2825 }
2826 }
2827
2828 /*
2829 * Print an error or a warning if the symbol cannot be initialized due
2830 * to type/storage class. Return whether an error has been detected.
2831 */
2832 static bool
2833 check_init(sym_t *sym)
2834 {
2835 bool erred;
2836
2837 erred = false;
2838
2839 if (sym->s_type->t_tspec == FUNC) {
2840 /* cannot initialize function: %s */
2841 error(24, sym->s_name);
2842 erred = true;
2843 } else if (sym->s_scl == TYPEDEF) {
2844 /* cannot initialize typedef: %s */
2845 error(25, sym->s_name);
2846 erred = true;
2847 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2848 /* cannot initialize "extern" declaration: %s */
2849 if (dcs->d_ctx == EXTERN) {
2850 /* cannot initialize extern declaration: %s */
2851 warning(26, sym->s_name);
2852 } else {
2853 /* cannot initialize extern declaration: %s */
2854 error(26, sym->s_name);
2855 erred = true;
2856 }
2857 }
2858
2859 return erred;
2860 }
2861
2862 /*
2863 * Create a symbol for an abstract declaration.
2864 */
2865 sym_t *
2866 abstract_name(void)
2867 {
2868 sym_t *sym;
2869
2870 lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PROTO_ARG);
2871
2872 sym = getblk(sizeof (sym_t));
2873
2874 sym->s_name = unnamed;
2875 sym->s_def = DEF;
2876 sym->s_scl = ABSTRACT;
2877 sym->s_block_level = -1;
2878
2879 if (dcs->d_ctx == PROTO_ARG)
2880 sym->s_arg = true;
2881
2882 sym->s_type = dcs->d_type;
2883 dcs->d_rdcsym = NULL;
2884 dcs->d_vararg = false;
2885
2886 return sym;
2887 }
2888
2889 /*
2890 * Removes anything which has nothing to do on global level.
2891 */
2892 void
2893 global_clean_up(void)
2894 {
2895
2896 while (dcs->d_next != NULL)
2897 popdecl();
2898
2899 cleanup();
2900 block_level = 0;
2901 mem_block_level = 0;
2902
2903 /*
2904 * remove all information about pending lint directives without
2905 * warnings.
2906 */
2907 global_clean_up_decl(true);
2908 }
2909
2910 /*
2911 * Process an abstract type declaration
2912 */
2913 sym_t *
2914 declare_1_abstract(sym_t *sym)
2915 {
2916
2917 check_function_definition(sym, true);
2918 check_type(sym);
2919 return sym;
2920 }
2921
2922 /*
2923 * Checks size after declarations of variables and their initialization.
2924 */
2925 void
2926 check_size(sym_t *dsym)
2927 {
2928
2929 if (dsym->s_def != DEF)
2930 return;
2931 if (dsym->s_scl == TYPEDEF)
2932 return;
2933 if (dsym->s_type->t_tspec == FUNC)
2934 return;
2935
2936 if (length(dsym->s_type, dsym->s_name) == 0 &&
2937 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2938 if (tflag) {
2939 /* empty array declaration: %s */
2940 warning(190, dsym->s_name);
2941 } else {
2942 /* empty array declaration: %s */
2943 error(190, dsym->s_name);
2944 }
2945 }
2946 }
2947
2948 /*
2949 * Mark an object as set if it is not already
2950 */
2951 void
2952 mark_as_set(sym_t *sym)
2953 {
2954
2955 if (!sym->s_set) {
2956 sym->s_set = true;
2957 UNIQUE_CURR_POS(sym->s_set_pos);
2958 }
2959 }
2960
2961 /*
2962 * Mark an object as used if it is not already
2963 */
2964 void
2965 mark_as_used(sym_t *sym, bool fcall, bool szof)
2966 {
2967
2968 if (!sym->s_used) {
2969 sym->s_used = true;
2970 UNIQUE_CURR_POS(sym->s_use_pos);
2971 }
2972 /*
2973 * for function calls another record is written
2974 *
2975 * XXX Should symbols used in sizeof() be treated as used or not?
2976 * Probably not, because there is no sense to declare an
2977 * external variable only to get their size.
2978 */
2979 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2980 outusg(sym);
2981 }
2982
2983 /*
2984 * Prints warnings for a list of variables and labels (concatenated
2985 * with s_dlnxt) if these are not used or only set.
2986 */
2987 void
2988 check_usage(dinfo_t *di)
2989 {
2990 sym_t *sym;
2991 int mklwarn;
2992
2993 /* for this warning LINTED has no effect */
2994 mklwarn = lwarn;
2995 lwarn = LWARN_ALL;
2996
2997 #ifdef DEBUG
2998 printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2999 lwarn);
3000 #endif
3001 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
3002 check_usage_sym(di->d_asm, sym);
3003 lwarn = mklwarn;
3004 #ifdef DEBUG
3005 printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
3006 lwarn);
3007 #endif
3008 }
3009
3010 /*
3011 * Prints a warning for a single variable or label if it is not used or
3012 * only set.
3013 */
3014 void
3015 check_usage_sym(bool novar, sym_t *sym)
3016 {
3017 pos_t cpos;
3018
3019 if (sym->s_block_level == -1)
3020 return;
3021
3022 cpos = curr_pos;
3023
3024 if (sym->s_kind == FVFT) {
3025 if (sym->s_arg) {
3026 check_argument_usage(novar, sym);
3027 } else {
3028 check_variable_usage(novar, sym);
3029 }
3030 } else if (sym->s_kind == FLABEL) {
3031 check_label_usage(sym);
3032 } else if (sym->s_kind == FTAG) {
3033 check_tag_usage(sym);
3034 }
3035
3036 curr_pos = cpos;
3037 }
3038
3039 static void
3040 check_argument_usage(bool novar, sym_t *arg)
3041 {
3042
3043 lint_assert(arg->s_set);
3044
3045 if (novar)
3046 return;
3047
3048 if (!arg->s_used && vflag) {
3049 curr_pos = arg->s_def_pos;
3050 /* argument %s unused in function %s */
3051 warning(231, arg->s_name, funcsym->s_name);
3052 }
3053 }
3054
3055 static void
3056 check_variable_usage(bool novar, sym_t *sym)
3057 {
3058 scl_t sc;
3059 sym_t *xsym;
3060
3061 lint_assert(block_level != 0);
3062 lint_assert(sym->s_block_level != 0);
3063
3064 /* errors in expressions easily cause lots of these warnings */
3065 if (nerr != 0)
3066 return;
3067
3068 /*
3069 * XXX Only variables are checked, although types should
3070 * probably also be checked
3071 */
3072 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
3073 sc != AUTO && sc != REG) {
3074 return;
3075 }
3076
3077 if (novar)
3078 return;
3079
3080 if (sc == EXTERN) {
3081 if (!sym->s_used && !sym->s_set) {
3082 curr_pos = sym->s_def_pos;
3083 /* %s unused in function %s */
3084 warning(192, sym->s_name, funcsym->s_name);
3085 }
3086 } else {
3087 if (sym->s_set && !sym->s_used) {
3088 curr_pos = sym->s_set_pos;
3089 /* %s set but not used in function %s */
3090 warning(191, sym->s_name, funcsym->s_name);
3091 } else if (!sym->s_used) {
3092 curr_pos = sym->s_def_pos;
3093 /* %s unused in function %s */
3094 warning(192, sym->s_name, funcsym->s_name);
3095 }
3096 }
3097
3098 if (sc == EXTERN) {
3099 /*
3100 * information about usage is taken over into the symbol
3101 * table entry at level 0 if the symbol was locally declared
3102 * as an external symbol.
3103 *
3104 * XXX This is wrong for symbols declared static at level 0
3105 * if the usage information stems from sizeof(). This is
3106 * because symbols at level 0 only used in sizeof() are
3107 * considered to not be used.
3108 */
3109 if ((xsym = sym->s_ext_sym) != NULL) {
3110 if (sym->s_used && !xsym->s_used) {
3111 xsym->s_used = true;
3112 xsym->s_use_pos = sym->s_use_pos;
3113 }
3114 if (sym->s_set && !xsym->s_set) {
3115 xsym->s_set = true;
3116 xsym->s_set_pos = sym->s_set_pos;
3117 }
3118 }
3119 }
3120 }
3121
3122 static void
3123 check_label_usage(sym_t *lab)
3124 {
3125
3126 lint_assert(block_level == 1);
3127 lint_assert(lab->s_block_level == 1);
3128
3129 if (lab->s_set && !lab->s_used) {
3130 curr_pos = lab->s_set_pos;
3131 /* label %s unused in function %s */
3132 warning(232, lab->s_name, funcsym->s_name);
3133 } else if (!lab->s_set) {
3134 curr_pos = lab->s_use_pos;
3135 /* undefined label %s */
3136 warning(23, lab->s_name);
3137 }
3138 }
3139
3140 static void
3141 check_tag_usage(sym_t *sym)
3142 {
3143
3144 if (!is_incomplete(sym->s_type))
3145 return;
3146
3147 /* always complain about incomplete tags declared inside blocks */
3148 if (!zflag || dcs->d_ctx != EXTERN)
3149 return;
3150
3151 curr_pos = sym->s_def_pos;
3152 switch (sym->s_type->t_tspec) {
3153 case STRUCT:
3154 /* struct %s never defined */
3155 warning(233, sym->s_name);
3156 break;
3157 case UNION:
3158 /* union %s never defined */
3159 warning(234, sym->s_name);
3160 break;
3161 case ENUM:
3162 /* enum %s never defined */
3163 warning(235, sym->s_name);
3164 break;
3165 default:
3166 lint_assert(/*CONSTCOND*/false);
3167 }
3168 }
3169
3170 /*
3171 * Called after the entire translation unit has been parsed.
3172 * Changes tentative definitions into definitions.
3173 * Performs some tests on global symbols. Detected problems are:
3174 * - defined variables of incomplete type
3175 * - constant variables which are not initialized
3176 * - static symbols which are never used
3177 */
3178 void
3179 check_global_symbols(void)
3180 {
3181 sym_t *sym;
3182 pos_t cpos;
3183
3184 if (block_level != 0 || dcs->d_next != NULL)
3185 norecover();
3186
3187 cpos = curr_pos;
3188
3189 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3190 if (sym->s_block_level == -1)
3191 continue;
3192 if (sym->s_kind == FVFT) {
3193 check_global_variable(sym);
3194 } else if (sym->s_kind == FTAG) {
3195 check_tag_usage(sym);
3196 } else {
3197 lint_assert(sym->s_kind == FMEMBER);
3198 }
3199 }
3200
3201 curr_pos = cpos;
3202 }
3203
3204 static void
3205 check_unused_static_global_variable(const sym_t *sym)
3206 {
3207 curr_pos = sym->s_def_pos;
3208 if (sym->s_type->t_tspec == FUNC) {
3209 if (sym->s_def == DEF) {
3210 if (!sym->s_inline)
3211 /* static function %s unused */
3212 warning(236, sym->s_name);
3213 } else {
3214 /* static function %s declared but not defined */
3215 warning(290, sym->s_name);
3216 }
3217 } else if (!sym->s_set) {
3218 /* static variable %s unused */
3219 warning(226, sym->s_name);
3220 } else {
3221 /* static variable %s set but not used */
3222 warning(307, sym->s_name);
3223 }
3224 }
3225
3226 static void
3227 check_static_global_variable(const sym_t *sym)
3228 {
3229 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3230 curr_pos = sym->s_use_pos;
3231 /* static function called but not defined: %s() */
3232 error(225, sym->s_name);
3233 }
3234
3235 if (!sym->s_used)
3236 check_unused_static_global_variable(sym);
3237
3238 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3239 curr_pos = sym->s_def_pos;
3240 /* const object %s should have initializer */
3241 warning(227, sym->s_name);
3242 }
3243 }
3244
3245 static void
3246 check_global_variable(const sym_t *sym)
3247 {
3248
3249 if (sym->s_scl == TYPEDEF || sym->s_scl == CTCONST)
3250 return;
3251
3252 if (sym->s_scl == NOSCL)
3253 return; /* May be caused by a syntax error. */
3254
3255 lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
3256
3257 check_global_variable_size(sym);
3258
3259 if (sym->s_scl == STATIC)
3260 check_static_global_variable(sym);
3261 }
3262
3263 static void
3264 check_global_variable_size(const sym_t *sym)
3265 {
3266
3267 if (sym->s_def != TDEF)
3268 return;
3269 if (sym->s_type->t_tspec == FUNC)
3270 /*
3271 * this can happen if a syntax error occurred after a
3272 * function declaration
3273 */
3274 return;
3275
3276 curr_pos = sym->s_def_pos;
3277 if (length(sym->s_type, sym->s_name) == 0 &&
3278 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3279 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3280 /* empty array declaration: %s */
3281 warning(190, sym->s_name);
3282 } else {
3283 /* empty array declaration: %s */
3284 error(190, sym->s_name);
3285 }
3286 }
3287 }
3288
3289 /*
3290 * Prints information about location of previous definition/declaration.
3291 */
3292 void
3293 print_previous_declaration(int msg, const sym_t *psym)
3294 {
3295 pos_t cpos;
3296
3297 if (!rflag)
3298 return;
3299
3300 cpos = curr_pos;
3301 curr_pos = psym->s_def_pos;
3302 if (msg != -1) {
3303 (message)(msg);
3304 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3305 /* previous definition of %s */
3306 message(261, psym->s_name);
3307 } else {
3308 /* previous declaration of %s */
3309 message(260, psym->s_name);
3310 }
3311 curr_pos = cpos;
3312 }
3313
3314 /*
3315 * Gets a node for a constant and returns the value of this constant
3316 * as integer.
3317 *
3318 * If the node is not constant or too large for int or of type float,
3319 * a warning will be printed.
3320 *
3321 * to_int_constant() should be used only inside declarations. If it is used in
3322 * expressions, it frees the memory used for the expression.
3323 */
3324 int
3325 to_int_constant(tnode_t *tn, bool required)
3326 {
3327 int i;
3328 tspec_t t;
3329 val_t *v;
3330
3331 v = constant(tn, required);
3332
3333 if (tn == NULL) {
3334 i = 1;
3335 goto done;
3336 }
3337
3338 /*
3339 * Abstract declarations are used inside expression. To free
3340 * the memory would be a fatal error.
3341 * We don't free blocks that are inside casts because these
3342 * will be used later to match types.
3343 */
3344 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
3345 tfreeblk();
3346
3347 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
3348 i = (int)v->v_ldbl;
3349 /* integral constant expression expected */
3350 error(55);
3351 } else {
3352 i = (int)v->v_quad;
3353 if (is_uinteger(t)) {
3354 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
3355 /* integral constant too large */
3356 warning(56);
3357 }
3358 } else {
3359 if (v->v_quad > (int64_t)TARG_INT_MAX ||
3360 v->v_quad < (int64_t)TARG_INT_MIN) {
3361 /* integral constant too large */
3362 warning(56);
3363 }
3364 }
3365 }
3366
3367 done:
3368 free(v);
3369 return i;
3370 }
3371