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