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