decl.c revision 1.162 1 /* $NetBSD: decl.c,v 1.162 2021/03/26 20:51:34 christos 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.162 2021/03/26 20:51:34 christos 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 *dcs);
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 *typetab);
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 *ntp);
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 begin_declaration_level(scl_t sc)
592 {
593 dinfo_t *di;
594
595 /* put a new element on the declaration stack */
596 di = xcalloc(1, sizeof *di);
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("%s(%p %d)\n", __func__, dcs, (int)sc);
603
604 }
605
606 /*
607 * Go back to previous declaration level
608 */
609 void
610 end_declaration_level(void)
611 {
612 dinfo_t *di;
613
614 if (dflag)
615 (void)printf("%s(%p %d)\n", __func__, 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 *dsym);
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 *tp);
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 *tp);
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
1397 * end_declaration_level after add_function. To be able to restore
1398 * them if this is a function definition, a pointer to the list of all
1399 * symbols is stored in dcs->d_next->d_func_proto_syms. Also a list of
1400 * the arguments (concatenated by s_next) is stored in
1401 * dcs->d_next->d_func_args. (dcs->d_next must be used because *dcs is
1402 * the declaration stack element created for the list of params and is
1403 * removed after 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 *tp);
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 *tp);
1672 tp->t_packed = dcs->d_packed;
1673 } else {
1674 tp = tag->s_type;
1675 }
1676 } else {
1677 tag = getblk(sizeof *tag);
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 *tp);
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 *tp->t_str);
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 switch (dcs->d_ctx) {
1900 case EXTERN:
1901 if (renaming != NULL) {
1902 lint_assert(decl->s_rename == NULL);
1903
1904 s = getlblk(1, renaming->sb_len + 1);
1905 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1906 decl->s_rename = s;
1907 }
1908 decl1ext(decl, initflg);
1909 break;
1910 case ARG:
1911 if (renaming != NULL) {
1912 /* symbol renaming can't be used on function arguments */
1913 error(310);
1914 break;
1915 }
1916 (void)declare_argument(decl, initflg);
1917 break;
1918 default:
1919 lint_assert(dcs->d_ctx == AUTO);
1920 if (renaming != NULL) {
1921 /* symbol renaming can't be used on automatic variables */
1922 error(311);
1923 break;
1924 }
1925 declare_local(decl, initflg);
1926 break;
1927 }
1928
1929 if (initflg && !*current_initerr())
1930 initstack_init();
1931 }
1932
1933 /*
1934 * Process a single external declarator.
1935 */
1936 void
1937 decl1ext(sym_t *dsym, bool initflg)
1938 {
1939 bool dowarn, rval, redec;
1940 sym_t *rdsym;
1941
1942 check_function_definition(dsym, true);
1943
1944 check_type(dsym);
1945
1946 if (initflg && !(*current_initerr() = check_init(dsym)))
1947 dsym->s_def = DEF;
1948
1949 /*
1950 * Declarations of functions are marked as "tentative" in
1951 * declarator_name(). This is wrong because there are no
1952 * tentative function definitions.
1953 */
1954 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1955 dsym->s_def = DECL;
1956
1957 if (dcs->d_inline) {
1958 if (dsym->s_type->t_tspec == FUNC) {
1959 dsym->s_inline = true;
1960 } else {
1961 /* variable declared inline: %s */
1962 warning(268, dsym->s_name);
1963 }
1964 }
1965
1966 /* Write the declaration into the output file */
1967 if (plibflg && llibflg &&
1968 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1969 /*
1970 * With both LINTLIBRARY and PROTOLIB the prototype is
1971 * written as a function definition to the output file.
1972 */
1973 rval = dsym->s_type->t_subt->t_tspec != VOID;
1974 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1975 } else {
1976 outsym(dsym, dsym->s_scl, dsym->s_def);
1977 }
1978
1979 if ((rdsym = dcs->d_redeclared_symbol) != NULL) {
1980
1981 /*
1982 * If the old symbol stems from an old style function
1983 * definition, we have remembered the params in rdsmy->s_args
1984 * and compare them with the params of the prototype.
1985 */
1986 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1987 redec = check_old_style_definition(rdsym, dsym);
1988 } else {
1989 redec = false;
1990 }
1991
1992 if (!redec &&
1993 !check_redeclaration(dsym, (dowarn = false, &dowarn))) {
1994
1995 if (dowarn) {
1996 if (sflag)
1997 /* redeclaration of %s */
1998 error(27, dsym->s_name);
1999 else
2000 /* redeclaration of %s */
2001 warning(27, dsym->s_name);
2002 print_previous_declaration(-1, rdsym);
2003 }
2004
2005 /*
2006 * Take over the remembered params if the new symbol
2007 * is not a prototype.
2008 */
2009 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
2010 dsym->s_osdef = rdsym->s_osdef;
2011 dsym->s_args = rdsym->s_args;
2012 dsym->s_def_pos = rdsym->s_def_pos;
2013 }
2014
2015 /*
2016 * Remember the position of the declaration if the
2017 * old symbol was a prototype and the new is not.
2018 * Also remember the position if the old symbol
2019 * was defined and the new is not.
2020 */
2021 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
2022 dsym->s_def_pos = rdsym->s_def_pos;
2023 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
2024 dsym->s_def_pos = rdsym->s_def_pos;
2025 }
2026
2027 /*
2028 * Copy usage information of the name into the new
2029 * symbol.
2030 */
2031 copy_usage_info(dsym, rdsym);
2032
2033 /* Once a name is defined, it remains defined. */
2034 if (rdsym->s_def == DEF)
2035 dsym->s_def = DEF;
2036
2037 /* once a function is inline, it remains inline */
2038 if (rdsym->s_inline)
2039 dsym->s_inline = true;
2040
2041 complete_type(dsym, rdsym);
2042
2043 }
2044
2045 rmsym(rdsym);
2046 }
2047
2048 if (dsym->s_scl == TYPEDEF) {
2049 dsym->s_type = duptyp(dsym->s_type);
2050 dsym->s_type->t_typedef = true;
2051 settdsym(dsym->s_type, dsym);
2052 }
2053
2054 }
2055
2056 /*
2057 * Copies information about usage into a new symbol table entry of
2058 * the same symbol.
2059 */
2060 void
2061 copy_usage_info(sym_t *sym, sym_t *rdsym)
2062 {
2063
2064 sym->s_set_pos = rdsym->s_set_pos;
2065 sym->s_use_pos = rdsym->s_use_pos;
2066 sym->s_set = rdsym->s_set;
2067 sym->s_used = rdsym->s_used;
2068 }
2069
2070 /*
2071 * Prints an error and returns true if a symbol is redeclared/redefined.
2072 * Otherwise returns false and, in some cases of minor problems, prints
2073 * a warning.
2074 */
2075 bool
2076 check_redeclaration(sym_t *dsym, bool *dowarn)
2077 {
2078 sym_t *rsym;
2079
2080 if ((rsym = dcs->d_redeclared_symbol)->s_scl == CTCONST) {
2081 /* redeclaration of %s */
2082 error(27, dsym->s_name);
2083 print_previous_declaration(-1, rsym);
2084 return true;
2085 }
2086 if (rsym->s_scl == TYPEDEF) {
2087 /* typedef redeclared: %s */
2088 error(89, dsym->s_name);
2089 print_previous_declaration(-1, rsym);
2090 return true;
2091 }
2092 if (dsym->s_scl == TYPEDEF) {
2093 /* redeclaration of %s */
2094 error(27, dsym->s_name);
2095 print_previous_declaration(-1, rsym);
2096 return true;
2097 }
2098 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2099 /* redefinition of %s */
2100 error(28, dsym->s_name);
2101 print_previous_declaration(-1, rsym);
2102 return true;
2103 }
2104 if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) {
2105 /* redeclaration of %s */
2106 error(27, dsym->s_name);
2107 print_previous_declaration(-1, rsym);
2108 return true;
2109 }
2110 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2111 return false;
2112 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2113 return false;
2114 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2115 return false;
2116 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2117 /*
2118 * All cases except "int a = 1; static int a;" are caught
2119 * above with or without a warning
2120 */
2121 /* redeclaration of %s */
2122 error(27, dsym->s_name);
2123 print_previous_declaration(-1, rsym);
2124 return true;
2125 }
2126 if (rsym->s_scl == EXTERN) {
2127 /* previously declared extern, becomes static: %s */
2128 warning(29, dsym->s_name);
2129 print_previous_declaration(-1, rsym);
2130 return false;
2131 }
2132 /*
2133 * Now it's one of:
2134 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2135 */
2136 /* redeclaration of %s; ANSI C requires "static" */
2137 if (sflag) {
2138 /* redeclaration of %s; ANSI C requires static */
2139 warning(30, dsym->s_name);
2140 print_previous_declaration(-1, rsym);
2141 }
2142 dsym->s_scl = STATIC;
2143 return false;
2144 }
2145
2146 static bool
2147 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
2148 {
2149 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2150 return false;
2151
2152 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2153 return false;
2154
2155 return true;
2156 }
2157
2158 bool
2159 eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual)
2160 {
2161 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2162 return false;
2163
2164 if (!qualifiers_correspond(tp1, tp2, ignqual))
2165 return false;
2166
2167 return true;
2168 }
2169
2170
2171 /*
2172 * Checks if two types are compatible.
2173 *
2174 * ignqual ignore qualifiers of type; used for function params
2175 * promot promote left type; used for comparison of params of
2176 * old style function definitions with params of prototypes.
2177 * *dowarn set to true if an old style function declaration is not
2178 * compatible with a prototype
2179 */
2180 bool
2181 eqtype(const type_t *tp1, const type_t *tp2,
2182 bool ignqual, bool promot, bool *dowarn)
2183 {
2184 tspec_t t;
2185
2186 while (tp1 != NULL && tp2 != NULL) {
2187
2188 t = tp1->t_tspec;
2189 if (promot) {
2190 if (t == FLOAT) {
2191 t = DOUBLE;
2192 } else if (t == CHAR || t == SCHAR) {
2193 t = INT;
2194 } else if (t == UCHAR) {
2195 t = tflag ? UINT : INT;
2196 } else if (t == SHORT) {
2197 t = INT;
2198 } else if (t == USHORT) {
2199 /* CONSTCOND */
2200 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
2201 }
2202 }
2203
2204 if (t != tp2->t_tspec)
2205 return false;
2206
2207 if (!qualifiers_correspond(tp1, tp2, ignqual))
2208 return false;
2209
2210 if (t == STRUCT || t == UNION)
2211 return tp1->t_str == tp2->t_str;
2212
2213 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2214 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2215 return false;
2216 }
2217
2218 /* dont check prototypes for traditional */
2219 if (t == FUNC && !tflag) {
2220 if (tp1->t_proto && tp2->t_proto) {
2221 if (!eqargs(tp1, tp2, dowarn))
2222 return false;
2223 } else if (tp1->t_proto) {
2224 if (!mnoarg(tp1, dowarn))
2225 return false;
2226 } else if (tp2->t_proto) {
2227 if (!mnoarg(tp2, dowarn))
2228 return false;
2229 }
2230 }
2231
2232 tp1 = tp1->t_subt;
2233 tp2 = tp2->t_subt;
2234 ignqual = promot = false;
2235
2236 }
2237
2238 return tp1 == tp2;
2239 }
2240
2241 /*
2242 * Compares the parameter types of two prototypes.
2243 */
2244 static bool
2245 eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn)
2246 {
2247 sym_t *a1, *a2;
2248
2249 if (tp1->t_vararg != tp2->t_vararg)
2250 return false;
2251
2252 a1 = tp1->t_args;
2253 a2 = tp2->t_args;
2254
2255 while (a1 != NULL && a2 != NULL) {
2256
2257 if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn))
2258 return false;
2259
2260 a1 = a1->s_next;
2261 a2 = a2->s_next;
2262
2263 }
2264
2265 return a1 == a2;
2266 }
2267
2268 /*
2269 * mnoarg() (matches functions with no argument type information)
2270 * returns whether all parameters of a prototype are compatible with
2271 * an old style function declaration.
2272 * This is the case if the following conditions are met:
2273 * 1. the prototype has a fixed number of parameters
2274 * 2. no parameter is of type float
2275 * 3. no parameter is converted to another type if integer promotion
2276 * is applied on it
2277 */
2278 static bool
2279 mnoarg(const type_t *tp, bool *dowarn)
2280 {
2281 sym_t *arg;
2282 tspec_t t;
2283
2284 if (tp->t_vararg) {
2285 if (dowarn != NULL)
2286 *dowarn = true;
2287 }
2288 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2289 if ((t = arg->s_type->t_tspec) == FLOAT ||
2290 t == CHAR || t == SCHAR || t == UCHAR ||
2291 t == SHORT || t == USHORT) {
2292 if (dowarn != NULL)
2293 *dowarn = true;
2294 }
2295 }
2296 return true;
2297 }
2298
2299 /*
2300 * Compares a prototype declaration with the remembered arguments of
2301 * a previous old style function definition.
2302 */
2303 static bool
2304 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2305 {
2306 sym_t *args, *pargs, *arg, *parg;
2307 int narg, nparg, n;
2308 bool dowarn, msg;
2309
2310 args = rdsym->s_args;
2311 pargs = dsym->s_type->t_args;
2312
2313 msg = false;
2314
2315 narg = nparg = 0;
2316 for (arg = args; arg != NULL; arg = arg->s_next)
2317 narg++;
2318 for (parg = pargs; parg != NULL; parg = parg->s_next)
2319 nparg++;
2320 if (narg != nparg) {
2321 /* prototype does not match old-style definition */
2322 error(63);
2323 msg = true;
2324 goto end;
2325 }
2326
2327 arg = args;
2328 parg = pargs;
2329 n = 1;
2330 while (narg-- > 0) {
2331 dowarn = false;
2332 /*
2333 * If it does not match due to promotion and sflag is
2334 * not set we print only a warning.
2335 */
2336 if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) ||
2337 dowarn) {
2338 /* prototype does not match old style defn., arg #%d */
2339 error(299, n);
2340 msg = true;
2341 }
2342 arg = arg->s_next;
2343 parg = parg->s_next;
2344 n++;
2345 }
2346
2347 end:
2348 if (msg)
2349 /* old style definition */
2350 print_previous_declaration(300, rdsym);
2351
2352 return msg;
2353 }
2354
2355 /*
2356 * Completes a type by copying the dimension and prototype information
2357 * from a second compatible type.
2358 *
2359 * Following lines are legal:
2360 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2361 * "typedef ft(); ft f; f(int); ft g; g(long);"
2362 * This means that, if a type is completed, the type structure must
2363 * be duplicated.
2364 */
2365 void
2366 complete_type(sym_t *dsym, sym_t *ssym)
2367 {
2368 type_t **dstp, *src;
2369 type_t *dst;
2370
2371 dstp = &dsym->s_type;
2372 src = ssym->s_type;
2373
2374 while ((dst = *dstp) != NULL) {
2375 lint_assert(src != NULL);
2376 lint_assert(dst->t_tspec == src->t_tspec);
2377 if (dst->t_tspec == ARRAY) {
2378 if (dst->t_dim == 0 && src->t_dim != 0) {
2379 *dstp = dst = duptyp(dst);
2380 dst->t_dim = src->t_dim;
2381 setcomplete(dst, true);
2382 }
2383 } else if (dst->t_tspec == FUNC) {
2384 if (!dst->t_proto && src->t_proto) {
2385 *dstp = dst = duptyp(dst);
2386 dst->t_proto = true;
2387 dst->t_args = src->t_args;
2388 }
2389 }
2390 dstp = &dst->t_subt;
2391 src = src->t_subt;
2392 }
2393 }
2394
2395 /*
2396 * Completes the declaration of a single argument.
2397 */
2398 sym_t *
2399 declare_argument(sym_t *sym, bool initflg)
2400 {
2401 tspec_t t;
2402
2403 check_function_definition(sym, true);
2404
2405 check_type(sym);
2406
2407 if (dcs->d_redeclared_symbol != NULL &&
2408 dcs->d_redeclared_symbol->s_block_level == block_level) {
2409 /* redeclaration of formal parameter %s */
2410 error(237, sym->s_name);
2411 rmsym(dcs->d_redeclared_symbol);
2412 sym->s_arg = true;
2413 }
2414
2415 if (!sym->s_arg) {
2416 /* declared argument %s is missing */
2417 error(53, sym->s_name);
2418 sym->s_arg = true;
2419 }
2420
2421 if (initflg) {
2422 /* cannot initialize parameter: %s */
2423 error(52, sym->s_name);
2424 *current_initerr() = true;
2425 }
2426
2427 if ((t = sym->s_type->t_tspec) == ARRAY) {
2428 sym->s_type = incref(sym->s_type->t_subt, PTR);
2429 } else if (t == FUNC) {
2430 if (tflag)
2431 /* a function is declared as an argument: %s */
2432 warning(50, sym->s_name);
2433 sym->s_type = incref(sym->s_type, PTR);
2434 } else if (t == FLOAT) {
2435 if (tflag)
2436 sym->s_type = gettyp(DOUBLE);
2437 }
2438
2439 if (dcs->d_inline)
2440 /* argument declared inline: %s */
2441 warning(269, sym->s_name);
2442
2443 /*
2444 * Arguments must have complete types. lengths() prints the needed
2445 * error messages (null dimension is impossible because arrays are
2446 * converted to pointers).
2447 */
2448 if (sym->s_type->t_tspec != VOID)
2449 (void)length(sym->s_type, sym->s_name);
2450
2451 sym->s_used = dcs->d_used;
2452 mark_as_set(sym);
2453
2454 return sym;
2455 }
2456
2457 void
2458 check_func_lint_directives(void)
2459 {
2460 sym_t *arg;
2461 int narg, n;
2462 tspec_t t;
2463
2464 /* check for illegal combinations of lint directives */
2465 if (printflike_argnum != -1 && scanflike_argnum != -1) {
2466 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2467 warning(289);
2468 printflike_argnum = scanflike_argnum = -1;
2469 }
2470 if (nvararg != -1 &&
2471 (printflike_argnum != -1 || scanflike_argnum != -1)) {
2472 /* dubious use of ** VARARGS ** with ** %s ** */
2473 warning(288,
2474 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2475 nvararg = -1;
2476 }
2477
2478 /*
2479 * check if the argument of a lint directive is compatible with the
2480 * number of arguments.
2481 */
2482 narg = 0;
2483 for (arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
2484 narg++;
2485 if (nargusg > narg) {
2486 /* argument number mismatch with directive: ** %s ** */
2487 warning(283, "ARGSUSED");
2488 nargusg = 0;
2489 }
2490 if (nvararg > narg) {
2491 /* argument number mismatch with directive: ** %s ** */
2492 warning(283, "VARARGS");
2493 nvararg = 0;
2494 }
2495 if (printflike_argnum > narg) {
2496 /* argument number mismatch with directive: ** %s ** */
2497 warning(283, "PRINTFLIKE");
2498 printflike_argnum = -1;
2499 } else if (printflike_argnum == 0) {
2500 printflike_argnum = -1;
2501 }
2502 if (scanflike_argnum > narg) {
2503 /* argument number mismatch with directive: ** %s ** */
2504 warning(283, "SCANFLIKE");
2505 scanflike_argnum = -1;
2506 } else if (scanflike_argnum == 0) {
2507 scanflike_argnum = -1;
2508 }
2509 if (printflike_argnum != -1 || scanflike_argnum != -1) {
2510 narg = printflike_argnum != -1
2511 ? printflike_argnum : scanflike_argnum;
2512 arg = dcs->d_func_args;
2513 for (n = 1; n < narg; n++)
2514 arg = arg->s_next;
2515 if (arg->s_type->t_tspec != PTR ||
2516 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2517 t != UCHAR && t != SCHAR)) {
2518 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2519 warning(293, narg);
2520 printflike_argnum = scanflike_argnum = -1;
2521 }
2522 }
2523 }
2524
2525 /*
2526 * Warn about arguments in old style function definitions that default to int.
2527 * Check that an old style function definition is compatible to a previous
2528 * prototype.
2529 */
2530 void
2531 check_func_old_style_arguments(void)
2532 {
2533 sym_t *args, *arg, *pargs, *parg;
2534 int narg, nparg;
2535 bool msg;
2536
2537 args = funcsym->s_args;
2538 pargs = funcsym->s_type->t_args;
2539
2540 /*
2541 * print a warning for each argument of an old style function
2542 * definition which defaults to int
2543 */
2544 for (arg = args; arg != NULL; arg = arg->s_next) {
2545 if (arg->s_defarg) {
2546 /* argument type defaults to 'int': %s */
2547 warning(32, arg->s_name);
2548 arg->s_defarg = false;
2549 mark_as_set(arg);
2550 }
2551 }
2552
2553 /*
2554 * If this is an old style function definition and a prototype
2555 * exists, compare the types of arguments.
2556 */
2557 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2558 /*
2559 * If the number of arguments does not match, we need not
2560 * continue.
2561 */
2562 narg = nparg = 0;
2563 msg = false;
2564 for (parg = pargs; parg != NULL; parg = parg->s_next)
2565 nparg++;
2566 for (arg = args; arg != NULL; arg = arg->s_next)
2567 narg++;
2568 if (narg != nparg) {
2569 /* parameter mismatch: %d declared, %d defined */
2570 error(51, nparg, narg);
2571 msg = true;
2572 } else {
2573 parg = pargs;
2574 arg = args;
2575 while (narg-- > 0) {
2576 msg |= check_prototype_declaration(arg, parg);
2577 parg = parg->s_next;
2578 arg = arg->s_next;
2579 }
2580 }
2581 if (msg)
2582 /* prototype declaration */
2583 print_previous_declaration(285,
2584 dcs->d_redeclared_symbol);
2585
2586 /* from now on the prototype is valid */
2587 funcsym->s_osdef = false;
2588 funcsym->s_args = NULL;
2589 }
2590 }
2591
2592 /*
2593 * Checks compatibility of an old style function definition with a previous
2594 * prototype declaration.
2595 * Returns true if the position of the previous declaration should be reported.
2596 */
2597 static bool
2598 check_prototype_declaration(sym_t *arg, sym_t *parg)
2599 {
2600 type_t *tp, *ptp;
2601 bool dowarn, msg;
2602
2603 tp = arg->s_type;
2604 ptp = parg->s_type;
2605
2606 msg = false;
2607 dowarn = false;
2608
2609 if (!eqtype(tp, ptp, true, true, &dowarn)) {
2610 if (eqtype(tp, ptp, true, false, &dowarn)) {
2611 /* type does not match prototype: %s */
2612 gnuism(58, arg->s_name);
2613 msg = sflag || !gflag;
2614 } else {
2615 /* type does not match prototype: %s */
2616 error(58, arg->s_name);
2617 msg = true;
2618 }
2619 } else if (dowarn) {
2620 if (sflag)
2621 /* type does not match prototype: %s */
2622 error(58, arg->s_name);
2623 else
2624 /* type does not match prototype: %s */
2625 warning(58, arg->s_name);
2626 msg = true;
2627 }
2628
2629 return msg;
2630 }
2631
2632 /*
2633 * Completes a single local declaration/definition.
2634 */
2635 void
2636 declare_local(sym_t *dsym, bool initflg)
2637 {
2638
2639 /* Correct a mistake done in declarator_name(). */
2640 if (dsym->s_type->t_tspec == FUNC) {
2641 dsym->s_def = DECL;
2642 if (dcs->d_scl == NOSCL)
2643 dsym->s_scl = EXTERN;
2644 }
2645
2646 if (dsym->s_type->t_tspec == FUNC) {
2647 if (dsym->s_scl == STATIC) {
2648 /* dubious static function at block level: %s */
2649 warning(93, dsym->s_name);
2650 dsym->s_scl = EXTERN;
2651 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2652 /* function has illegal storage class: %s */
2653 error(94, dsym->s_name);
2654 dsym->s_scl = EXTERN;
2655 }
2656 }
2657
2658 /*
2659 * functions may be declared inline at local scope, although
2660 * this has no effect for a later definition of the same
2661 * function.
2662 * XXX it should have an effect if tflag is set. this would
2663 * also be the way gcc behaves.
2664 */
2665 if (dcs->d_inline) {
2666 if (dsym->s_type->t_tspec == FUNC) {
2667 dsym->s_inline = true;
2668 } else {
2669 /* variable declared inline: %s */
2670 warning(268, dsym->s_name);
2671 }
2672 }
2673
2674 check_function_definition(dsym, true);
2675
2676 check_type(dsym);
2677
2678 if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
2679 declare_external_in_block(dsym);
2680
2681 if (dsym->s_scl == EXTERN) {
2682 /*
2683 * XXX if the static variable at level 0 is only defined
2684 * later, checking will be possible.
2685 */
2686 if (dsym->s_ext_sym == NULL) {
2687 outsym(dsym, EXTERN, dsym->s_def);
2688 } else {
2689 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2690 }
2691 }
2692
2693 if (dcs->d_redeclared_symbol != NULL) {
2694
2695 if (dcs->d_redeclared_symbol->s_block_level == 0) {
2696
2697 switch (dsym->s_scl) {
2698 case AUTO:
2699 if (hflag)
2700 /* automatic hides external decl.: %s */
2701 warning(86, dsym->s_name);
2702 break;
2703 case STATIC:
2704 if (hflag)
2705 /* static hides external decl.: %s */
2706 warning(87, dsym->s_name);
2707 break;
2708 case TYPEDEF:
2709 if (hflag)
2710 /* typedef hides external decl.: %s */
2711 warning(88, dsym->s_name);
2712 break;
2713 case EXTERN:
2714 /*
2715 * Warnings and errors are printed in
2716 * declare_external_in_block()
2717 */
2718 break;
2719 default:
2720 lint_assert(/*CONSTCOND*/false);
2721 }
2722
2723 } else if (dcs->d_redeclared_symbol->s_block_level ==
2724 block_level) {
2725
2726 /* no hflag, because it's illegal! */
2727 if (dcs->d_redeclared_symbol->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_redeclared_symbol);
2737 }
2738 }
2739
2740 } else if (dcs->d_redeclared_symbol->s_block_level <
2741 block_level) {
2742
2743 if (hflag)
2744 /* declaration hides earlier one: %s */
2745 warning(95, dsym->s_name);
2746
2747 }
2748
2749 if (dcs->d_redeclared_symbol->s_block_level == block_level) {
2750
2751 /* redeclaration of %s */
2752 error(27, dsym->s_name);
2753 rmsym(dcs->d_redeclared_symbol);
2754
2755 }
2756
2757 }
2758
2759 if (initflg && !(*current_initerr() = check_init(dsym))) {
2760 dsym->s_def = DEF;
2761 mark_as_set(dsym);
2762 }
2763
2764 if (dsym->s_scl == TYPEDEF) {
2765 dsym->s_type = duptyp(dsym->s_type);
2766 dsym->s_type->t_typedef = true;
2767 settdsym(dsym->s_type, dsym);
2768 }
2769
2770 /*
2771 * Before we can check the size we must wait for a initialization
2772 * which may follow.
2773 */
2774 }
2775
2776 /*
2777 * Processes (re)declarations of external symbols inside blocks.
2778 */
2779 static void
2780 declare_external_in_block(sym_t *dsym)
2781 {
2782 bool eqt, dowarn;
2783 sym_t *esym;
2784
2785 /* look for a symbol with the same name */
2786 esym = dcs->d_redeclared_symbol;
2787 while (esym != NULL && esym->s_block_level != 0) {
2788 while ((esym = esym->s_link) != NULL) {
2789 if (esym->s_kind != FVFT)
2790 continue;
2791 if (strcmp(dsym->s_name, esym->s_name) == 0)
2792 break;
2793 }
2794 }
2795 if (esym == NULL)
2796 return;
2797 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2798 /* gcc accepts this without a warning, pcc prints an error. */
2799 /* redeclaration of %s */
2800 warning(27, dsym->s_name);
2801 print_previous_declaration(-1, esym);
2802 return;
2803 }
2804
2805 dowarn = false;
2806 eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn);
2807
2808 if (!eqt || dowarn) {
2809 if (esym->s_scl == EXTERN) {
2810 /* inconsistent redeclaration of extern: %s */
2811 warning(90, dsym->s_name);
2812 print_previous_declaration(-1, esym);
2813 } else {
2814 /* inconsistent redeclaration of static: %s */
2815 warning(92, dsym->s_name);
2816 print_previous_declaration(-1, esym);
2817 }
2818 }
2819
2820 if (eqt) {
2821 /*
2822 * Remember the external symbol so we can update usage
2823 * information at the end of the block.
2824 */
2825 dsym->s_ext_sym = esym;
2826 }
2827 }
2828
2829 /*
2830 * Print an error or a warning if the symbol cannot be initialized due
2831 * to type/storage class. Return whether an error has been detected.
2832 */
2833 static bool
2834 check_init(sym_t *sym)
2835 {
2836 bool erred;
2837
2838 erred = false;
2839
2840 if (sym->s_type->t_tspec == FUNC) {
2841 /* cannot initialize function: %s */
2842 error(24, sym->s_name);
2843 erred = true;
2844 } else if (sym->s_scl == TYPEDEF) {
2845 /* cannot initialize typedef: %s */
2846 error(25, sym->s_name);
2847 erred = true;
2848 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2849 /* cannot initialize "extern" declaration: %s */
2850 if (dcs->d_ctx == EXTERN) {
2851 /* cannot initialize extern declaration: %s */
2852 warning(26, sym->s_name);
2853 } else {
2854 /* cannot initialize extern declaration: %s */
2855 error(26, sym->s_name);
2856 erred = true;
2857 }
2858 }
2859
2860 return erred;
2861 }
2862
2863 /*
2864 * Create a symbol for an abstract declaration.
2865 */
2866 sym_t *
2867 abstract_name(void)
2868 {
2869 sym_t *sym;
2870
2871 lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PROTO_ARG);
2872
2873 sym = getblk(sizeof *sym);
2874
2875 sym->s_name = unnamed;
2876 sym->s_def = DEF;
2877 sym->s_scl = ABSTRACT;
2878 sym->s_block_level = -1;
2879
2880 if (dcs->d_ctx == PROTO_ARG)
2881 sym->s_arg = true;
2882
2883 sym->s_type = dcs->d_type;
2884 dcs->d_redeclared_symbol = NULL;
2885 dcs->d_vararg = false;
2886
2887 return sym;
2888 }
2889
2890 /*
2891 * Removes anything which has nothing to do on global level.
2892 */
2893 void
2894 global_clean_up(void)
2895 {
2896
2897 while (dcs->d_next != NULL)
2898 end_declaration_level();
2899
2900 cleanup();
2901 block_level = 0;
2902 mem_block_level = 0;
2903
2904 /*
2905 * remove all information about pending lint directives without
2906 * warnings.
2907 */
2908 global_clean_up_decl(true);
2909 }
2910
2911 /*
2912 * Process an abstract type declaration
2913 */
2914 sym_t *
2915 declare_1_abstract(sym_t *sym)
2916 {
2917
2918 check_function_definition(sym, true);
2919 check_type(sym);
2920 return sym;
2921 }
2922
2923 /*
2924 * Checks size after declarations of variables and their initialization.
2925 */
2926 void
2927 check_size(sym_t *dsym)
2928 {
2929
2930 if (dsym->s_def != DEF)
2931 return;
2932 if (dsym->s_scl == TYPEDEF)
2933 return;
2934 if (dsym->s_type->t_tspec == FUNC)
2935 return;
2936
2937 if (length(dsym->s_type, dsym->s_name) == 0 &&
2938 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2939 if (tflag) {
2940 /* empty array declaration: %s */
2941 warning(190, dsym->s_name);
2942 } else {
2943 /* empty array declaration: %s */
2944 error(190, dsym->s_name);
2945 }
2946 }
2947 }
2948
2949 /*
2950 * Mark an object as set if it is not already
2951 */
2952 void
2953 mark_as_set(sym_t *sym)
2954 {
2955
2956 if (!sym->s_set) {
2957 sym->s_set = true;
2958 UNIQUE_CURR_POS(sym->s_set_pos);
2959 }
2960 }
2961
2962 /*
2963 * Mark an object as used if it is not already
2964 */
2965 void
2966 mark_as_used(sym_t *sym, bool fcall, bool szof)
2967 {
2968
2969 if (!sym->s_used) {
2970 sym->s_used = true;
2971 UNIQUE_CURR_POS(sym->s_use_pos);
2972 }
2973 /*
2974 * for function calls another record is written
2975 *
2976 * XXX Should symbols used in sizeof() be treated as used or not?
2977 * Probably not, because there is no sense to declare an
2978 * external variable only to get their size.
2979 */
2980 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2981 outusg(sym);
2982 }
2983
2984 /*
2985 * Prints warnings for a list of variables and labels (concatenated
2986 * with s_dlnxt) if these are not used or only set.
2987 */
2988 void
2989 check_usage(dinfo_t *di)
2990 {
2991 sym_t *sym;
2992 int mklwarn;
2993
2994 /* for this warning LINTED has no effect */
2995 mklwarn = lwarn;
2996 lwarn = LWARN_ALL;
2997
2998 #ifdef DEBUG
2999 printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
3000 lwarn);
3001 #endif
3002 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
3003 check_usage_sym(di->d_asm, sym);
3004 lwarn = mklwarn;
3005 #ifdef DEBUG
3006 printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
3007 lwarn);
3008 #endif
3009 }
3010
3011 /*
3012 * Prints a warning for a single variable or label if it is not used or
3013 * only set.
3014 */
3015 void
3016 check_usage_sym(bool novar, sym_t *sym)
3017 {
3018 pos_t cpos;
3019
3020 if (sym->s_block_level == -1)
3021 return;
3022
3023 cpos = curr_pos;
3024
3025 if (sym->s_kind == FVFT) {
3026 if (sym->s_arg) {
3027 check_argument_usage(novar, sym);
3028 } else {
3029 check_variable_usage(novar, sym);
3030 }
3031 } else if (sym->s_kind == FLABEL) {
3032 check_label_usage(sym);
3033 } else if (sym->s_kind == FTAG) {
3034 check_tag_usage(sym);
3035 }
3036
3037 curr_pos = cpos;
3038 }
3039
3040 static void
3041 check_argument_usage(bool novar, sym_t *arg)
3042 {
3043
3044 lint_assert(arg->s_set);
3045
3046 if (novar)
3047 return;
3048
3049 if (!arg->s_used && vflag) {
3050 curr_pos = arg->s_def_pos;
3051 /* argument %s unused in function %s */
3052 warning(231, arg->s_name, funcsym->s_name);
3053 }
3054 }
3055
3056 static void
3057 check_variable_usage(bool novar, sym_t *sym)
3058 {
3059 scl_t sc;
3060 sym_t *xsym;
3061
3062 lint_assert(block_level != 0);
3063 lint_assert(sym->s_block_level != 0);
3064
3065 /* errors in expressions easily cause lots of these warnings */
3066 if (nerr != 0)
3067 return;
3068
3069 /*
3070 * XXX Only variables are checked, although types should
3071 * probably also be checked
3072 */
3073 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
3074 sc != AUTO && sc != REG) {
3075 return;
3076 }
3077
3078 if (novar)
3079 return;
3080
3081 if (sc == EXTERN) {
3082 if (!sym->s_used && !sym->s_set) {
3083 curr_pos = sym->s_def_pos;
3084 /* %s unused in function %s */
3085 warning(192, sym->s_name, funcsym->s_name);
3086 }
3087 } else {
3088 if (sym->s_set && !sym->s_used) {
3089 curr_pos = sym->s_set_pos;
3090 /* %s set but not used in function %s */
3091 warning(191, sym->s_name, funcsym->s_name);
3092 } else if (!sym->s_used) {
3093 curr_pos = sym->s_def_pos;
3094 /* %s unused in function %s */
3095 warning(192, sym->s_name, funcsym->s_name);
3096 }
3097 }
3098
3099 if (sc == EXTERN) {
3100 /*
3101 * information about usage is taken over into the symbol
3102 * table entry at level 0 if the symbol was locally declared
3103 * as an external symbol.
3104 *
3105 * XXX This is wrong for symbols declared static at level 0
3106 * if the usage information stems from sizeof(). This is
3107 * because symbols at level 0 only used in sizeof() are
3108 * considered to not be used.
3109 */
3110 if ((xsym = sym->s_ext_sym) != NULL) {
3111 if (sym->s_used && !xsym->s_used) {
3112 xsym->s_used = true;
3113 xsym->s_use_pos = sym->s_use_pos;
3114 }
3115 if (sym->s_set && !xsym->s_set) {
3116 xsym->s_set = true;
3117 xsym->s_set_pos = sym->s_set_pos;
3118 }
3119 }
3120 }
3121 }
3122
3123 static void
3124 check_label_usage(sym_t *lab)
3125 {
3126
3127 lint_assert(block_level == 1);
3128 lint_assert(lab->s_block_level == 1);
3129
3130 if (lab->s_set && !lab->s_used) {
3131 curr_pos = lab->s_set_pos;
3132 /* label %s unused in function %s */
3133 warning(232, lab->s_name, funcsym->s_name);
3134 } else if (!lab->s_set) {
3135 curr_pos = lab->s_use_pos;
3136 /* undefined label %s */
3137 warning(23, lab->s_name);
3138 }
3139 }
3140
3141 static void
3142 check_tag_usage(sym_t *sym)
3143 {
3144
3145 if (!is_incomplete(sym->s_type))
3146 return;
3147
3148 /* always complain about incomplete tags declared inside blocks */
3149 if (!zflag || dcs->d_ctx != EXTERN)
3150 return;
3151
3152 curr_pos = sym->s_def_pos;
3153 switch (sym->s_type->t_tspec) {
3154 case STRUCT:
3155 /* struct %s never defined */
3156 warning(233, sym->s_name);
3157 break;
3158 case UNION:
3159 /* union %s never defined */
3160 warning(234, sym->s_name);
3161 break;
3162 case ENUM:
3163 /* enum %s never defined */
3164 warning(235, sym->s_name);
3165 break;
3166 default:
3167 lint_assert(/*CONSTCOND*/false);
3168 }
3169 }
3170
3171 /*
3172 * Called after the entire translation unit has been parsed.
3173 * Changes tentative definitions into definitions.
3174 * Performs some tests on global symbols. Detected problems are:
3175 * - defined variables of incomplete type
3176 * - constant variables which are not initialized
3177 * - static symbols which are never used
3178 */
3179 void
3180 check_global_symbols(void)
3181 {
3182 sym_t *sym;
3183 pos_t cpos;
3184
3185 if (block_level != 0 || dcs->d_next != NULL)
3186 norecover();
3187
3188 cpos = curr_pos;
3189
3190 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
3191 if (sym->s_block_level == -1)
3192 continue;
3193 if (sym->s_kind == FVFT) {
3194 check_global_variable(sym);
3195 } else if (sym->s_kind == FTAG) {
3196 check_tag_usage(sym);
3197 } else {
3198 lint_assert(sym->s_kind == FMEMBER);
3199 }
3200 }
3201
3202 curr_pos = cpos;
3203 }
3204
3205 static void
3206 check_unused_static_global_variable(const sym_t *sym)
3207 {
3208 curr_pos = sym->s_def_pos;
3209 if (sym->s_type->t_tspec == FUNC) {
3210 if (sym->s_def == DEF) {
3211 if (!sym->s_inline)
3212 /* static function %s unused */
3213 warning(236, sym->s_name);
3214 } else {
3215 /* static function %s declared but not defined */
3216 warning(290, sym->s_name);
3217 }
3218 } else if (!sym->s_set) {
3219 /* static variable %s unused */
3220 warning(226, sym->s_name);
3221 } else {
3222 /* static variable %s set but not used */
3223 warning(307, sym->s_name);
3224 }
3225 }
3226
3227 static void
3228 check_static_global_variable(const sym_t *sym)
3229 {
3230 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3231 curr_pos = sym->s_use_pos;
3232 /* static function called but not defined: %s() */
3233 error(225, sym->s_name);
3234 }
3235
3236 if (!sym->s_used)
3237 check_unused_static_global_variable(sym);
3238
3239 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3240 curr_pos = sym->s_def_pos;
3241 /* const object %s should have initializer */
3242 warning(227, sym->s_name);
3243 }
3244 }
3245
3246 static void
3247 check_global_variable(const sym_t *sym)
3248 {
3249
3250 if (sym->s_scl == TYPEDEF || sym->s_scl == CTCONST)
3251 return;
3252
3253 if (sym->s_scl == NOSCL)
3254 return; /* May be caused by a syntax error. */
3255
3256 lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
3257
3258 check_global_variable_size(sym);
3259
3260 if (sym->s_scl == STATIC)
3261 check_static_global_variable(sym);
3262 }
3263
3264 static void
3265 check_global_variable_size(const sym_t *sym)
3266 {
3267
3268 if (sym->s_def != TDEF)
3269 return;
3270 if (sym->s_type->t_tspec == FUNC)
3271 /*
3272 * this can happen if a syntax error occurred after a
3273 * function declaration
3274 */
3275 return;
3276
3277 curr_pos = sym->s_def_pos;
3278 if (length(sym->s_type, sym->s_name) == 0 &&
3279 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3280 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3281 /* empty array declaration: %s */
3282 warning(190, sym->s_name);
3283 } else {
3284 /* empty array declaration: %s */
3285 error(190, sym->s_name);
3286 }
3287 }
3288 }
3289
3290 /*
3291 * Prints information about location of previous definition/declaration.
3292 */
3293 void
3294 print_previous_declaration(int msg, const sym_t *psym)
3295 {
3296 pos_t cpos;
3297
3298 if (!rflag)
3299 return;
3300
3301 cpos = curr_pos;
3302 curr_pos = psym->s_def_pos;
3303 if (msg != -1) {
3304 (message)(msg);
3305 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3306 /* previous definition of %s */
3307 message(261, psym->s_name);
3308 } else {
3309 /* previous declaration of %s */
3310 message(260, psym->s_name);
3311 }
3312 curr_pos = cpos;
3313 }
3314
3315 /*
3316 * Gets a node for a constant and returns the value of this constant
3317 * as integer.
3318 *
3319 * If the node is not constant or too large for int or of type float,
3320 * a warning will be printed.
3321 *
3322 * to_int_constant() should be used only inside declarations. If it is used in
3323 * expressions, it frees the memory used for the expression.
3324 */
3325 int
3326 to_int_constant(tnode_t *tn, bool required)
3327 {
3328 int i;
3329 tspec_t t;
3330 val_t *v;
3331
3332 v = constant(tn, required);
3333
3334 if (tn == NULL) {
3335 i = 1;
3336 goto done;
3337 }
3338
3339 /*
3340 * Abstract declarations are used inside expression. To free
3341 * the memory would be a fatal error.
3342 * We don't free blocks that are inside casts because these
3343 * will be used later to match types.
3344 */
3345 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
3346 tfreeblk();
3347
3348 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
3349 i = (int)v->v_ldbl;
3350 /* integral constant expression expected */
3351 error(55);
3352 } else {
3353 i = (int)v->v_quad;
3354 if (is_uinteger(t)) {
3355 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
3356 /* integral constant too large */
3357 warning(56);
3358 }
3359 } else {
3360 if (v->v_quad > (int64_t)TARG_INT_MAX ||
3361 v->v_quad < (int64_t)TARG_INT_MIN) {
3362 /* integral constant too large */
3363 warning(56);
3364 }
3365 }
3366 }
3367
3368 done:
3369 free(v);
3370 return i;
3371 }
3372