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