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