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