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