decl.c revision 1.257 1 /* $NetBSD: decl.c,v 1.257 2022/04/01 22:28:21 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.257 2022/04/01 22:28:21 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 '%s' */
977 error(15, type_name(tp));
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 } else 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 debug_step("add_pointer: unchanged '%s'",
1311 type_name(decl->s_type));
1312 return decl;
1313 }
1314
1315 while (p != NULL) {
1316 *tpp = tp = block_zero_alloc(sizeof(*tp));
1317 tp->t_tspec = PTR;
1318 tp->t_const = p->p_const;
1319 tp->t_volatile = p->p_volatile;
1320 *(tpp = &tp->t_subt) = dcs->d_type;
1321 next = p->p_next;
1322 free(p);
1323 p = next;
1324 }
1325 debug_step("add_pointer: '%s'", type_name(decl->s_type));
1326 return decl;
1327 }
1328
1329 /*
1330 * If a dimension was specified, dim is true, otherwise false
1331 * n is the specified dimension
1332 */
1333 sym_t *
1334 add_array(sym_t *decl, bool dim, int n)
1335 {
1336 type_t **tpp, *tp;
1337
1338 tpp = &decl->s_type;
1339 while (*tpp != NULL && *tpp != dcs->d_type)
1340 tpp = &(*tpp)->t_subt;
1341 if (*tpp == NULL) {
1342 debug_step("add_array: unchanged '%s'",
1343 type_name(decl->s_type));
1344 return decl;
1345 }
1346
1347 *tpp = tp = block_zero_alloc(sizeof(*tp));
1348 tp->t_tspec = ARRAY;
1349 tp->t_subt = dcs->d_type;
1350 tp->t_dim = n;
1351
1352 if (n < 0) {
1353 /* negative array dimension (%d) */
1354 error(20, n);
1355 n = 0;
1356 } else if (n == 0 && dim) {
1357 /* zero sized array is a C99 extension */
1358 c99ism(322);
1359 } else if (n == 0 && !dim) {
1360 setcomplete(tp, false);
1361 }
1362
1363 debug_step("add_array: '%s'", type_name(decl->s_type));
1364 return decl;
1365 }
1366
1367 sym_t *
1368 add_function(sym_t *decl, sym_t *args)
1369 {
1370 type_t **tpp, *tp;
1371
1372 if (dcs->d_proto) {
1373 if (tflag)
1374 /* function prototypes are illegal in traditional C */
1375 warning(270);
1376 args = new_style_function(decl, args);
1377 } else {
1378 old_style_function(decl, args);
1379 }
1380
1381 /*
1382 * The symbols are removed from the symbol table by
1383 * end_declaration_level after add_function. To be able to restore
1384 * them if this is a function definition, a pointer to the list of all
1385 * symbols is stored in dcs->d_next->d_func_proto_syms. Also a list of
1386 * the arguments (concatenated by s_next) is stored in
1387 * dcs->d_next->d_func_args. (dcs->d_next must be used because *dcs is
1388 * the declaration stack element created for the list of params and is
1389 * removed after add_function.)
1390 */
1391 if (dcs->d_next->d_ctx == EXTERN &&
1392 decl->s_type == dcs->d_next->d_type) {
1393 dcs->d_next->d_func_proto_syms = dcs->d_dlsyms;
1394 dcs->d_next->d_func_args = args;
1395 }
1396
1397 /*
1398 * XXX: What is this code doing on a semantic level, and why?
1399 * Returning decl leads to the wrong function types in msg_347.
1400 */
1401 tpp = &decl->s_type;
1402 if (*tpp == NULL)
1403 decl->s_type = dcs->d_next->d_type;
1404 while (*tpp != NULL && *tpp != dcs->d_next->d_type)
1405 /*
1406 * XXX: accessing INT->t_subt feels strange, even though it
1407 * may even be guaranteed to be NULL.
1408 */
1409 tpp = &(*tpp)->t_subt;
1410 if (*tpp == NULL) {
1411 debug_step("add_function: unchanged '%s'",
1412 type_name(decl->s_type));
1413 return decl; /* see msg_347 */
1414 }
1415
1416 *tpp = tp = block_zero_alloc(sizeof(*tp));
1417 tp->t_tspec = FUNC;
1418 tp->t_subt = dcs->d_next->d_type;
1419 if ((tp->t_proto = dcs->d_proto) != false)
1420 tp->t_args = args;
1421 tp->t_vararg = dcs->d_vararg;
1422
1423 debug_step("add_function: '%s'", type_name(decl->s_type));
1424 return decl;
1425 }
1426
1427 /*
1428 * Called for new style function declarations.
1429 */
1430 /* ARGSUSED */
1431 static sym_t *
1432 new_style_function(sym_t *decl, sym_t *args)
1433 {
1434 sym_t *arg, *sym;
1435 scl_t sc;
1436
1437 /*
1438 * Declarations of structs/unions/enums in param lists are legal,
1439 * but senseless.
1440 */
1441 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_level_next) {
1442 sc = sym->s_scl;
1443 if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) {
1444 /* dubious tag declaration: %s %s */
1445 warning(85, storage_class_name(sc), sym->s_name);
1446 }
1447 }
1448
1449 for (arg = args; arg != NULL; arg = arg->s_next) {
1450 if (arg->s_type->t_tspec == VOID &&
1451 !(arg == args && arg->s_next == NULL)) {
1452 /* void must be sole parameter */
1453 error(60);
1454 arg->s_type = gettyp(INT);
1455 }
1456 }
1457
1458 if (args == NULL || args->s_type->t_tspec == VOID)
1459 return NULL;
1460 return args;
1461 }
1462
1463 /*
1464 * Called for old style function declarations.
1465 */
1466 static void
1467 old_style_function(sym_t *decl, sym_t *args)
1468 {
1469
1470 /*
1471 * Remember list of parameters only if this really seems to be a
1472 * function definition.
1473 */
1474 if (dcs->d_next->d_ctx == EXTERN &&
1475 decl->s_type == dcs->d_next->d_type) {
1476 /*
1477 * We assume that this becomes a function definition. If
1478 * we are wrong, it's corrected in check_function_definition.
1479 */
1480 if (args != NULL) {
1481 decl->s_osdef = true;
1482 decl->s_args = args;
1483 }
1484 } else {
1485 if (args != NULL)
1486 /* function prototype parameters must have types */
1487 warning(62);
1488 }
1489 }
1490
1491 /*
1492 * Lists of identifiers in functions declarations are allowed only if
1493 * it's also a function definition. If this is not the case, print an
1494 * error message.
1495 */
1496 void
1497 check_function_definition(sym_t *sym, bool msg)
1498 {
1499
1500 if (sym->s_osdef) {
1501 if (msg) {
1502 /* incomplete or misplaced function definition */
1503 error(22);
1504 }
1505 sym->s_osdef = false;
1506 sym->s_args = NULL;
1507 }
1508 }
1509
1510 /*
1511 * Process the name in a declarator.
1512 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
1513 * TYPEDEF.
1514 * s_def and s_register are valid after declarator_name().
1515 */
1516 sym_t *
1517 declarator_name(sym_t *sym)
1518 {
1519 scl_t sc = NOSCL;
1520
1521 if (sym->s_scl == NOSCL) {
1522 dcs->d_redeclared_symbol = NULL;
1523 } else if (sym->s_defarg) {
1524 sym->s_defarg = false;
1525 dcs->d_redeclared_symbol = NULL;
1526 } else {
1527 dcs->d_redeclared_symbol = sym;
1528 sym = pushdown(sym);
1529 }
1530
1531 switch (dcs->d_ctx) {
1532 case MOS:
1533 case MOU:
1534 /* Set parent */
1535 sym->s_sou_type = dcs->d_tagtyp->t_str;
1536 sym->s_def = DEF;
1537 sym->s_value.v_tspec = INT;
1538 sc = dcs->d_ctx;
1539 break;
1540 case EXTERN:
1541 /*
1542 * static and external symbols without "extern" are
1543 * considered to be tentative defined, external
1544 * symbols with "extern" are declared, and typedef names
1545 * are defined. Tentative defined and declared symbols
1546 * may become defined if an initializer is present or
1547 * this is a function definition.
1548 */
1549 if ((sc = dcs->d_scl) == NOSCL) {
1550 sc = EXTERN;
1551 sym->s_def = TDEF;
1552 } else if (sc == STATIC) {
1553 sym->s_def = TDEF;
1554 } else if (sc == TYPEDEF) {
1555 sym->s_def = DEF;
1556 } else {
1557 lint_assert(sc == EXTERN);
1558 sym->s_def = DECL;
1559 }
1560 break;
1561 case PROTO_ARG:
1562 sym->s_arg = true;
1563 /* FALLTHROUGH */
1564 case OLD_STYLE_ARG:
1565 if ((sc = dcs->d_scl) == NOSCL) {
1566 sc = AUTO;
1567 } else {
1568 lint_assert(sc == REG);
1569 sym->s_register = true;
1570 sc = AUTO;
1571 }
1572 sym->s_def = DEF;
1573 break;
1574 case AUTO:
1575 if ((sc = dcs->d_scl) == NOSCL) {
1576 /*
1577 * XXX somewhat ugly because we dont know whether
1578 * this is AUTO or EXTERN (functions). If we are
1579 * wrong it must be corrected in declare_local(),
1580 * where we have the necessary type information.
1581 */
1582 sc = AUTO;
1583 sym->s_def = DEF;
1584 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1585 sym->s_def = DEF;
1586 } else if (sc == REG) {
1587 sym->s_register = true;
1588 sc = AUTO;
1589 sym->s_def = DEF;
1590 } else {
1591 lint_assert(sc == EXTERN);
1592 sym->s_def = DECL;
1593 }
1594 break;
1595 default:
1596 lint_assert(/*CONSTCOND*/false);
1597 }
1598 sym->s_scl = sc;
1599
1600 sym->s_type = dcs->d_type;
1601
1602 dcs->d_func_proto_syms = NULL;
1603
1604 return sym;
1605 }
1606
1607 /*
1608 * Process a name in the list of formal parameters in an old style function
1609 * definition.
1610 */
1611 sym_t *
1612 old_style_function_name(sym_t *sym)
1613 {
1614
1615 if (sym->s_scl != NOSCL) {
1616 if (block_level == sym->s_block_level) {
1617 /* redeclaration of formal parameter %s */
1618 error(21, sym->s_name);
1619 lint_assert(sym->s_defarg);
1620 }
1621 sym = pushdown(sym);
1622 }
1623 sym->s_type = gettyp(INT);
1624 sym->s_scl = AUTO;
1625 sym->s_def = DEF;
1626 sym->s_defarg = sym->s_arg = true;
1627 return sym;
1628 }
1629
1630 /*
1631 * Create the type of a tag.
1632 *
1633 * tag points to the symbol table entry of the tag
1634 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1635 * decl is true if the type of the tag will be completed in this declaration
1636 * (the following token is T_LBRACE)
1637 * semi is true if the following token is T_SEMI
1638 */
1639 type_t *
1640 mktag(sym_t *tag, tspec_t kind, bool decl, bool semi)
1641 {
1642 scl_t scl;
1643 type_t *tp;
1644
1645 if (kind == STRUCT) {
1646 scl = STRUCT_TAG;
1647 } else if (kind == UNION) {
1648 scl = UNION_TAG;
1649 } else {
1650 lint_assert(kind == ENUM);
1651 scl = ENUM_TAG;
1652 }
1653
1654 if (tag != NULL) {
1655 if (tag->s_scl != NOSCL) {
1656 tag = newtag(tag, scl, decl, semi);
1657 } else {
1658 /* a new tag, no empty declaration */
1659 dcs->d_next->d_nonempty_decl = true;
1660 if (scl == ENUM_TAG && !decl) {
1661 if (!tflag && (sflag || pflag))
1662 /* forward reference to enum type */
1663 warning(42);
1664 }
1665 }
1666 if (tag->s_scl == NOSCL) {
1667 tag->s_scl = scl;
1668 tag->s_type = tp = block_zero_alloc(sizeof(*tp));
1669 tp->t_packed = dcs->d_packed;
1670 } else {
1671 tp = tag->s_type;
1672 }
1673 } else {
1674 tag = block_zero_alloc(sizeof(*tag));
1675 tag->s_name = unnamed;
1676 UNIQUE_CURR_POS(tag->s_def_pos);
1677 tag->s_kind = FTAG;
1678 tag->s_scl = scl;
1679 tag->s_block_level = -1;
1680 tag->s_type = tp = block_zero_alloc(sizeof(*tp));
1681 tp->t_packed = dcs->d_packed;
1682 dcs->d_next->d_nonempty_decl = true;
1683 }
1684
1685 if (tp->t_tspec == NOTSPEC) {
1686 tp->t_tspec = kind;
1687 if (kind != ENUM) {
1688 tp->t_str = block_zero_alloc(sizeof(*tp->t_str));
1689 tp->t_str->sou_align_in_bits = CHAR_SIZE;
1690 tp->t_str->sou_tag = tag;
1691 } else {
1692 tp->t_is_enum = true;
1693 tp->t_enum = block_zero_alloc(sizeof(*tp->t_enum));
1694 tp->t_enum->en_tag = tag;
1695 }
1696 setcomplete(tp, false);
1697 }
1698 return tp;
1699 }
1700
1701 /*
1702 * Checks all possible cases of tag redeclarations.
1703 * decl is true if T_LBRACE follows
1704 * semi is true if T_SEMI follows
1705 */
1706 static sym_t *
1707 newtag(sym_t *tag, scl_t scl, bool decl, bool semi)
1708 {
1709
1710 if (tag->s_block_level < block_level) {
1711 if (semi) {
1712 /* "struct a;" */
1713 if (!tflag) {
1714 if (!sflag)
1715 /* declaration introduces new ... */
1716 warning(44, storage_class_name(scl),
1717 tag->s_name);
1718 tag = pushdown(tag);
1719 } else if (tag->s_scl != scl) {
1720 /* base type is really '%s %s' */
1721 warning(45, storage_class_name(tag->s_scl),
1722 tag->s_name);
1723 }
1724 dcs->d_next->d_nonempty_decl = true;
1725 } else if (decl) {
1726 /* "struct a { ... } " */
1727 if (hflag)
1728 /* redefinition hides earlier one: %s */
1729 warning(43, tag->s_name);
1730 tag = pushdown(tag);
1731 dcs->d_next->d_nonempty_decl = true;
1732 } else if (tag->s_scl != scl) {
1733 /* base type is really '%s %s' */
1734 warning(45, storage_class_name(tag->s_scl),
1735 tag->s_name);
1736 if (!sflag) {
1737 /* declaration introduces new type in ... */
1738 warning(44, storage_class_name(scl),
1739 tag->s_name);
1740 }
1741 tag = pushdown(tag);
1742 dcs->d_next->d_nonempty_decl = true;
1743 }
1744 } else {
1745 if (tag->s_scl != scl ||
1746 (decl && !is_incomplete(tag->s_type))) {
1747 /* %s tag '%s' redeclared as %s */
1748 error(46, storage_class_name(tag->s_scl),
1749 tag->s_name, storage_class_name(scl));
1750 print_previous_declaration(-1, tag);
1751 tag = pushdown(tag);
1752 dcs->d_next->d_nonempty_decl = true;
1753 } else if (semi || decl) {
1754 dcs->d_next->d_nonempty_decl = true;
1755 }
1756 }
1757 return tag;
1758 }
1759
1760 const char *
1761 storage_class_name(scl_t sc)
1762 {
1763 switch (sc) {
1764 case EXTERN: return "extern";
1765 case STATIC: return "static";
1766 case AUTO: return "auto";
1767 case REG: return "register";
1768 case TYPEDEF: return "typedef";
1769 case STRUCT_TAG:return "struct";
1770 case UNION_TAG: return "union";
1771 case ENUM_TAG: return "enum";
1772 default: lint_assert(/*CONSTCOND*/false);
1773 }
1774 /* NOTREACHED */
1775 }
1776
1777 /*
1778 * tp points to the type of the tag, fmem to the list of members.
1779 */
1780 type_t *
1781 complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1782 {
1783 tspec_t t;
1784 struct_or_union *sp;
1785 int n;
1786 sym_t *mem;
1787
1788 if (tp == NULL) /* in case of syntax errors */
1789 return gettyp(INT);
1790
1791 setcomplete(tp, true);
1792
1793 t = tp->t_tspec;
1794 align((u_int)dcs->d_sou_align_in_bits, 0);
1795 sp = tp->t_str;
1796 sp->sou_align_in_bits = dcs->d_sou_align_in_bits;
1797 sp->sou_first_member = fmem;
1798 if (tp->t_packed)
1799 setpackedsize(tp);
1800 else
1801 sp->sou_size_in_bits = dcs->d_offset;
1802
1803 if (sp->sou_size_in_bits == 0) {
1804 /* zero sized %s is a C99 feature */
1805 c99ism(47, ttab[t].tt_name);
1806 }
1807
1808 n = 0;
1809 for (mem = fmem; mem != NULL; mem = mem->s_next) {
1810 /* bind anonymous members to the structure */
1811 if (mem->s_sou_type == NULL) {
1812 mem->s_sou_type = sp;
1813 if (mem->s_type->t_bitfield) {
1814 sp->sou_size_in_bits += bitfieldsize(&mem);
1815 if (mem == NULL)
1816 break;
1817 }
1818 sp->sou_size_in_bits +=
1819 type_size_in_bits(mem->s_type);
1820 }
1821 if (mem->s_name != unnamed)
1822 n++;
1823 }
1824
1825 if (n == 0 && sp->sou_size_in_bits != 0) {
1826 /* %s has no named members */
1827 warning(65, t == STRUCT ? "structure" : "union");
1828 }
1829 return tp;
1830 }
1831
1832 type_t *
1833 complete_tag_enum(type_t *tp, sym_t *fmem)
1834 {
1835
1836 setcomplete(tp, true);
1837 tp->t_enum->en_first_enumerator = fmem;
1838 return tp;
1839 }
1840
1841 /*
1842 * Processes the name of an enumerator in an enum declaration.
1843 *
1844 * sym points to the enumerator
1845 * val is the value of the enumerator
1846 * impl is true if the value of the enumerator was not explicitly specified.
1847 */
1848 sym_t *
1849 enumeration_constant(sym_t *sym, int val, bool impl)
1850 {
1851
1852 if (sym->s_scl != NOSCL) {
1853 if (sym->s_block_level == block_level) {
1854 /* no hflag, because this is illegal!!! */
1855 if (sym->s_arg) {
1856 /* enumeration constant hides parameter: %s */
1857 warning(57, sym->s_name);
1858 } else {
1859 /* redeclaration of %s */
1860 error(27, sym->s_name);
1861 /*
1862 * inside blocks it should not be too
1863 * complicated to find the position of the
1864 * previous declaration
1865 */
1866 if (block_level == 0)
1867 print_previous_declaration(-1, sym);
1868 }
1869 } else {
1870 if (hflag)
1871 /* redefinition hides earlier one: %s */
1872 warning(43, sym->s_name);
1873 }
1874 sym = pushdown(sym);
1875 }
1876 sym->s_scl = CTCONST;
1877 sym->s_type = dcs->d_tagtyp;
1878 sym->s_value.v_tspec = INT;
1879 sym->s_value.v_quad = val;
1880 if (impl && val - 1 == TARG_INT_MAX) {
1881 /* overflow in enumeration values: %s */
1882 warning(48, sym->s_name);
1883 }
1884 enumval = val + 1;
1885 return sym;
1886 }
1887
1888 /*
1889 * Process a single external declarator.
1890 */
1891 static void
1892 declare_extern(sym_t *dsym, bool initflg, sbuf_t *renaming)
1893 {
1894 bool dowarn, rval, redec;
1895 sym_t *rdsym;
1896 char *s;
1897
1898 if (renaming != NULL) {
1899 lint_assert(dsym->s_rename == NULL);
1900
1901 s = level_zero_alloc(1, renaming->sb_len + 1);
1902 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1903 dsym->s_rename = s;
1904 }
1905
1906 check_function_definition(dsym, true);
1907
1908 check_type(dsym);
1909
1910 if (initflg && !check_init(dsym))
1911 dsym->s_def = DEF;
1912
1913 /*
1914 * Declarations of functions are marked as "tentative" in
1915 * declarator_name(). This is wrong because there are no
1916 * tentative function definitions.
1917 */
1918 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1919 dsym->s_def = DECL;
1920
1921 if (dcs->d_inline) {
1922 if (dsym->s_type->t_tspec == FUNC) {
1923 dsym->s_inline = true;
1924 } else {
1925 /* variable declared inline: %s */
1926 warning(268, dsym->s_name);
1927 }
1928 }
1929
1930 /* Write the declaration into the output file */
1931 if (plibflg && llibflg &&
1932 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1933 /*
1934 * With both LINTLIBRARY and PROTOLIB the prototype is
1935 * written as a function definition to the output file.
1936 */
1937 rval = dsym->s_type->t_subt->t_tspec != VOID;
1938 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1939 } else if (!is_compiler_builtin(dsym->s_name)) {
1940 outsym(dsym, dsym->s_scl, dsym->s_def);
1941 }
1942
1943 if ((rdsym = dcs->d_redeclared_symbol) != NULL) {
1944
1945 /*
1946 * If the old symbol stems from an old style function
1947 * definition, we have remembered the params in rdsym->s_args
1948 * and compare them with the params of the prototype.
1949 */
1950 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1951 redec = check_old_style_definition(rdsym, dsym);
1952 } else {
1953 redec = false;
1954 }
1955
1956 if (!redec &&
1957 !check_redeclaration(dsym, (dowarn = false, &dowarn))) {
1958
1959 if (dowarn) {
1960 if (sflag)
1961 /* redeclaration of %s */
1962 error(27, dsym->s_name);
1963 else
1964 /* redeclaration of %s */
1965 warning(27, dsym->s_name);
1966 print_previous_declaration(-1, rdsym);
1967 }
1968
1969 /*
1970 * Take over the remembered params if the new symbol
1971 * is not a prototype.
1972 */
1973 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1974 dsym->s_osdef = rdsym->s_osdef;
1975 dsym->s_args = rdsym->s_args;
1976 dsym->s_def_pos = rdsym->s_def_pos;
1977 }
1978
1979 /*
1980 * Remember the position of the declaration if the
1981 * old symbol was a prototype and the new is not.
1982 * Also remember the position if the old symbol
1983 * was defined and the new is not.
1984 */
1985 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1986 dsym->s_def_pos = rdsym->s_def_pos;
1987 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1988 dsym->s_def_pos = rdsym->s_def_pos;
1989 }
1990
1991 /*
1992 * Copy usage information of the name into the new
1993 * symbol.
1994 */
1995 copy_usage_info(dsym, rdsym);
1996
1997 /* Once a name is defined, it remains defined. */
1998 if (rdsym->s_def == DEF)
1999 dsym->s_def = DEF;
2000
2001 /* once a function is inline, it remains inline */
2002 if (rdsym->s_inline)
2003 dsym->s_inline = true;
2004
2005 complete_type(dsym, rdsym);
2006
2007 }
2008
2009 rmsym(rdsym);
2010 }
2011
2012 if (dsym->s_scl == TYPEDEF) {
2013 dsym->s_type = block_dup_type(dsym->s_type);
2014 dsym->s_type->t_typedef = true;
2015 settdsym(dsym->s_type, dsym);
2016 }
2017
2018 }
2019
2020 void
2021 declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2022 {
2023
2024 if (dcs->d_ctx == EXTERN) {
2025 declare_extern(decl, initflg, renaming);
2026 } else if (dcs->d_ctx == OLD_STYLE_ARG || dcs->d_ctx == PROTO_ARG) {
2027 if (renaming != NULL) {
2028 /* symbol renaming can't be used on function arguments */
2029 error(310);
2030 } else
2031 (void)declare_argument(decl, initflg);
2032 } else {
2033 lint_assert(dcs->d_ctx == AUTO);
2034 if (renaming != NULL) {
2035 /* symbol renaming can't be used on automatic variables */
2036 error(311);
2037 } else
2038 declare_local(decl, initflg);
2039 }
2040 }
2041
2042 /*
2043 * Copies information about usage into a new symbol table entry of
2044 * the same symbol.
2045 */
2046 void
2047 copy_usage_info(sym_t *sym, sym_t *rdsym)
2048 {
2049
2050 sym->s_set_pos = rdsym->s_set_pos;
2051 sym->s_use_pos = rdsym->s_use_pos;
2052 sym->s_set = rdsym->s_set;
2053 sym->s_used = rdsym->s_used;
2054 }
2055
2056 /*
2057 * Prints an error and returns true if a symbol is redeclared/redefined.
2058 * Otherwise returns false and, in some cases of minor problems, prints
2059 * a warning.
2060 */
2061 bool
2062 check_redeclaration(sym_t *dsym, bool *dowarn)
2063 {
2064 sym_t *rsym;
2065
2066 if ((rsym = dcs->d_redeclared_symbol)->s_scl == CTCONST) {
2067 /* redeclaration of %s */
2068 error(27, dsym->s_name);
2069 print_previous_declaration(-1, rsym);
2070 return true;
2071 }
2072 if (rsym->s_scl == TYPEDEF) {
2073 /* typedef redeclared: %s */
2074 error(89, dsym->s_name);
2075 print_previous_declaration(-1, rsym);
2076 return true;
2077 }
2078 if (dsym->s_scl == TYPEDEF) {
2079 /* redeclaration of %s */
2080 error(27, dsym->s_name);
2081 print_previous_declaration(-1, rsym);
2082 return true;
2083 }
2084 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2085 /* redefinition of %s */
2086 error(28, dsym->s_name);
2087 print_previous_declaration(-1, rsym);
2088 return true;
2089 }
2090 if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) {
2091 /* redeclaration of '%s' with type '%s', expected '%s' */
2092 error(347, dsym->s_name,
2093 type_name(dsym->s_type), type_name(rsym->s_type));
2094 print_previous_declaration(-1, rsym);
2095 return true;
2096 }
2097 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2098 return false;
2099 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2100 return false;
2101 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2102 return false;
2103 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2104 /*
2105 * All cases except "int a = 1; static int a;" are caught
2106 * above with or without a warning
2107 */
2108 /* redeclaration of %s */
2109 error(27, dsym->s_name);
2110 print_previous_declaration(-1, rsym);
2111 return true;
2112 }
2113 if (rsym->s_scl == EXTERN) {
2114 /* previously declared extern, becomes static: %s */
2115 warning(29, dsym->s_name);
2116 print_previous_declaration(-1, rsym);
2117 return false;
2118 }
2119 /*
2120 * Now it's one of:
2121 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2122 */
2123 /* redeclaration of %s; ANSI C requires "static" */
2124 if (sflag) {
2125 /* redeclaration of %s; ANSI C requires static */
2126 warning(30, dsym->s_name);
2127 print_previous_declaration(-1, rsym);
2128 }
2129 dsym->s_scl = STATIC;
2130 return false;
2131 }
2132
2133 static bool
2134 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
2135 {
2136 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2137 return false;
2138
2139 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2140 return false;
2141
2142 return true;
2143 }
2144
2145 bool
2146 eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual)
2147 {
2148 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2149 return false;
2150
2151 if (!qualifiers_correspond(tp1, tp2, ignqual))
2152 return false;
2153
2154 return true;
2155 }
2156
2157
2158 /*
2159 * Checks if two types are compatible.
2160 *
2161 * ignqual ignore qualifiers of type; used for function params
2162 * promot promote left type; used for comparison of params of
2163 * old style function definitions with params of prototypes.
2164 * *dowarn set to true if an old style function declaration is not
2165 * compatible with a prototype
2166 */
2167 bool
2168 eqtype(const type_t *tp1, const type_t *tp2,
2169 bool ignqual, bool promot, bool *dowarn)
2170 {
2171 tspec_t t;
2172
2173 while (tp1 != NULL && tp2 != NULL) {
2174
2175 t = tp1->t_tspec;
2176 if (promot) {
2177 if (t == FLOAT) {
2178 t = DOUBLE;
2179 } else if (t == CHAR || t == SCHAR) {
2180 t = INT;
2181 } else if (t == UCHAR) {
2182 t = tflag ? UINT : INT;
2183 } else if (t == SHORT) {
2184 t = INT;
2185 } else if (t == USHORT) {
2186 /* CONSTCOND */
2187 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag
2188 ? UINT : INT;
2189 }
2190 }
2191
2192 if (t != tp2->t_tspec)
2193 return false;
2194
2195 if (!qualifiers_correspond(tp1, tp2, ignqual))
2196 return false;
2197
2198 if (t == STRUCT || t == UNION)
2199 return tp1->t_str == tp2->t_str;
2200
2201 if (t == ENUM && eflag)
2202 return tp1->t_enum == tp2->t_enum;
2203
2204 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2205 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2206 return false;
2207 }
2208
2209 /* don't check prototypes for traditional */
2210 if (t == FUNC && !tflag) {
2211 if (tp1->t_proto && tp2->t_proto) {
2212 if (!eqargs(tp1, tp2, dowarn))
2213 return false;
2214 } else if (tp1->t_proto) {
2215 if (!mnoarg(tp1, dowarn))
2216 return false;
2217 } else if (tp2->t_proto) {
2218 if (!mnoarg(tp2, dowarn))
2219 return false;
2220 }
2221 }
2222
2223 tp1 = tp1->t_subt;
2224 tp2 = tp2->t_subt;
2225 ignqual = promot = false;
2226
2227 }
2228
2229 return tp1 == tp2;
2230 }
2231
2232 /*
2233 * Compares the parameter types of two prototypes.
2234 */
2235 static bool
2236 eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn)
2237 {
2238 sym_t *a1, *a2;
2239
2240 if (tp1->t_vararg != tp2->t_vararg)
2241 return false;
2242
2243 a1 = tp1->t_args;
2244 a2 = tp2->t_args;
2245
2246 while (a1 != NULL && a2 != NULL) {
2247
2248 if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn))
2249 return false;
2250
2251 a1 = a1->s_next;
2252 a2 = a2->s_next;
2253
2254 }
2255
2256 return a1 == a2;
2257 }
2258
2259 /*
2260 * mnoarg() (matches functions with no argument type information)
2261 * returns whether all parameters of a prototype are compatible with
2262 * an old style function declaration.
2263 * This is the case if the following conditions are met:
2264 * 1. the prototype has a fixed number of parameters
2265 * 2. no parameter is of type float
2266 * 3. no parameter is converted to another type if integer promotion
2267 * is applied on it
2268 */
2269 static bool
2270 mnoarg(const type_t *tp, bool *dowarn)
2271 {
2272 sym_t *arg;
2273 tspec_t t;
2274
2275 if (tp->t_vararg) {
2276 if (dowarn != NULL)
2277 *dowarn = true;
2278 }
2279 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2280 if ((t = arg->s_type->t_tspec) == FLOAT ||
2281 t == CHAR || t == SCHAR || t == UCHAR ||
2282 t == SHORT || t == USHORT) {
2283 if (dowarn != NULL)
2284 *dowarn = true;
2285 }
2286 }
2287 return true;
2288 }
2289
2290 /*
2291 * Compares a prototype declaration with the remembered arguments of
2292 * a previous old style function definition.
2293 */
2294 static bool
2295 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2296 {
2297 sym_t *args, *pargs, *arg, *parg;
2298 int narg, nparg, n;
2299 bool dowarn, msg;
2300
2301 args = rdsym->s_args;
2302 pargs = dsym->s_type->t_args;
2303
2304 msg = false;
2305
2306 narg = nparg = 0;
2307 for (arg = args; arg != NULL; arg = arg->s_next)
2308 narg++;
2309 for (parg = pargs; parg != NULL; parg = parg->s_next)
2310 nparg++;
2311 if (narg != nparg) {
2312 /* prototype does not match old-style definition */
2313 error(63);
2314 msg = true;
2315 goto end;
2316 }
2317
2318 arg = args;
2319 parg = pargs;
2320 n = 1;
2321 while (narg-- > 0) {
2322 dowarn = false;
2323 /*
2324 * If it does not match due to promotion and sflag is
2325 * not set we print only a warning.
2326 */
2327 if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) ||
2328 dowarn) {
2329 /* prototype does not match old style ... */
2330 error(299, n);
2331 msg = true;
2332 }
2333 arg = arg->s_next;
2334 parg = parg->s_next;
2335 n++;
2336 }
2337
2338 end:
2339 if (msg)
2340 /* old style definition */
2341 print_previous_declaration(300, rdsym);
2342
2343 return msg;
2344 }
2345
2346 /*
2347 * Completes a type by copying the dimension and prototype information
2348 * from a second compatible type.
2349 *
2350 * Following lines are legal:
2351 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2352 * "typedef ft(); ft f; f(int); ft g; g(long);"
2353 * This means that, if a type is completed, the type structure must
2354 * be duplicated.
2355 */
2356 void
2357 complete_type(sym_t *dsym, sym_t *ssym)
2358 {
2359 type_t **dstp, *src;
2360 type_t *dst;
2361
2362 dstp = &dsym->s_type;
2363 src = ssym->s_type;
2364
2365 while ((dst = *dstp) != NULL) {
2366 lint_assert(src != NULL);
2367 lint_assert(dst->t_tspec == src->t_tspec);
2368 if (dst->t_tspec == ARRAY) {
2369 if (dst->t_dim == 0 && src->t_dim != 0) {
2370 *dstp = dst = block_dup_type(dst);
2371 dst->t_dim = src->t_dim;
2372 setcomplete(dst, true);
2373 }
2374 } else if (dst->t_tspec == FUNC) {
2375 if (!dst->t_proto && src->t_proto) {
2376 *dstp = dst = block_dup_type(dst);
2377 dst->t_proto = true;
2378 dst->t_args = src->t_args;
2379 }
2380 }
2381 dstp = &dst->t_subt;
2382 src = src->t_subt;
2383 }
2384 }
2385
2386 /*
2387 * Completes the declaration of a single argument.
2388 */
2389 sym_t *
2390 declare_argument(sym_t *sym, bool initflg)
2391 {
2392 tspec_t t;
2393
2394 check_function_definition(sym, true);
2395
2396 check_type(sym);
2397
2398 if (dcs->d_redeclared_symbol != NULL &&
2399 dcs->d_redeclared_symbol->s_block_level == block_level) {
2400 /* redeclaration of formal parameter %s */
2401 error(237, sym->s_name);
2402 rmsym(dcs->d_redeclared_symbol);
2403 sym->s_arg = true;
2404 }
2405
2406 if (!sym->s_arg) {
2407 /* declared argument %s is missing */
2408 error(53, sym->s_name);
2409 sym->s_arg = true;
2410 }
2411
2412 if (initflg) {
2413 /* cannot initialize parameter: %s */
2414 error(52, sym->s_name);
2415 }
2416
2417 if (sym->s_type == NULL) /* for c(void()) */
2418 sym->s_type = gettyp(VOID);
2419
2420 if ((t = sym->s_type->t_tspec) == ARRAY) {
2421 sym->s_type = block_derive_type(sym->s_type->t_subt, PTR);
2422 } else if (t == FUNC) {
2423 if (tflag)
2424 /* a function is declared as an argument: %s */
2425 warning(50, sym->s_name);
2426 sym->s_type = block_derive_type(sym->s_type, PTR);
2427 } else if (t == FLOAT) {
2428 if (tflag)
2429 sym->s_type = gettyp(DOUBLE);
2430 }
2431
2432 if (dcs->d_inline)
2433 /* argument declared inline: %s */
2434 warning(269, sym->s_name);
2435
2436 /*
2437 * Arguments must have complete types. length() prints the needed
2438 * error messages (null dimension is impossible because arrays are
2439 * converted to pointers).
2440 */
2441 if (sym->s_type->t_tspec != VOID)
2442 (void)length(sym->s_type, sym->s_name);
2443
2444 sym->s_used = dcs->d_used;
2445 mark_as_set(sym);
2446
2447 return sym;
2448 }
2449
2450 void
2451 check_func_lint_directives(void)
2452 {
2453 sym_t *arg;
2454 int narg, n;
2455 tspec_t t;
2456
2457 /* check for illegal combinations of lint directives */
2458 if (printflike_argnum != -1 && scanflike_argnum != -1) {
2459 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2460 warning(289);
2461 printflike_argnum = scanflike_argnum = -1;
2462 }
2463 if (nvararg != -1 &&
2464 (printflike_argnum != -1 || scanflike_argnum != -1)) {
2465 /* dubious use of ** VARARGS ** with ** %s ** */
2466 warning(288,
2467 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2468 nvararg = -1;
2469 }
2470
2471 /*
2472 * check if the argument of a lint directive is compatible with the
2473 * number of arguments.
2474 */
2475 narg = 0;
2476 for (arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
2477 narg++;
2478 if (nargusg > narg) {
2479 /* argument number mismatch with directive: ** %s ** */
2480 warning(283, "ARGSUSED");
2481 nargusg = 0;
2482 }
2483 if (nvararg > narg) {
2484 /* argument number mismatch with directive: ** %s ** */
2485 warning(283, "VARARGS");
2486 nvararg = 0;
2487 }
2488 if (printflike_argnum > narg) {
2489 /* argument number mismatch with directive: ** %s ** */
2490 warning(283, "PRINTFLIKE");
2491 printflike_argnum = -1;
2492 } else if (printflike_argnum == 0) {
2493 printflike_argnum = -1;
2494 }
2495 if (scanflike_argnum > narg) {
2496 /* argument number mismatch with directive: ** %s ** */
2497 warning(283, "SCANFLIKE");
2498 scanflike_argnum = -1;
2499 } else if (scanflike_argnum == 0) {
2500 scanflike_argnum = -1;
2501 }
2502 if (printflike_argnum != -1 || scanflike_argnum != -1) {
2503 narg = printflike_argnum != -1
2504 ? printflike_argnum : scanflike_argnum;
2505 arg = dcs->d_func_args;
2506 for (n = 1; n < narg; n++)
2507 arg = arg->s_next;
2508 if (arg->s_type->t_tspec != PTR ||
2509 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2510 t != UCHAR && t != SCHAR)) {
2511 /* argument %d must be 'char *' for PRINTFLIKE/... */
2512 warning(293, narg);
2513 printflike_argnum = scanflike_argnum = -1;
2514 }
2515 }
2516 }
2517
2518 /*
2519 * Warn about arguments in old style function definitions that default to int.
2520 * Check that an old style function definition is compatible to a previous
2521 * prototype.
2522 */
2523 void
2524 check_func_old_style_arguments(void)
2525 {
2526 sym_t *args, *arg, *pargs, *parg;
2527 int narg, nparg;
2528 bool msg;
2529
2530 args = funcsym->s_args;
2531 pargs = funcsym->s_type->t_args;
2532
2533 /*
2534 * print a warning for each argument of an old style function
2535 * definition which defaults to int
2536 */
2537 for (arg = args; arg != NULL; arg = arg->s_next) {
2538 if (arg->s_defarg) {
2539 /* argument type defaults to 'int': %s */
2540 warning(32, arg->s_name);
2541 arg->s_defarg = false;
2542 mark_as_set(arg);
2543 }
2544 }
2545
2546 /*
2547 * If this is an old style function definition and a prototype
2548 * exists, compare the types of arguments.
2549 */
2550 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2551 /*
2552 * If the number of arguments does not match, we need not
2553 * continue.
2554 */
2555 narg = nparg = 0;
2556 msg = false;
2557 for (parg = pargs; parg != NULL; parg = parg->s_next)
2558 nparg++;
2559 for (arg = args; arg != NULL; arg = arg->s_next)
2560 narg++;
2561 if (narg != nparg) {
2562 /* parameter mismatch: %d declared, %d defined */
2563 error(51, nparg, narg);
2564 msg = true;
2565 } else {
2566 parg = pargs;
2567 arg = args;
2568 while (narg-- > 0) {
2569 msg |= check_prototype_declaration(arg, parg);
2570 parg = parg->s_next;
2571 arg = arg->s_next;
2572 }
2573 }
2574 if (msg)
2575 /* prototype declaration */
2576 print_previous_declaration(285,
2577 dcs->d_redeclared_symbol);
2578
2579 /* from now on the prototype is valid */
2580 funcsym->s_osdef = false;
2581 funcsym->s_args = NULL;
2582 }
2583 }
2584
2585 /*
2586 * Checks compatibility of an old style function definition with a previous
2587 * prototype declaration.
2588 * Returns true if the position of the previous declaration should be reported.
2589 */
2590 static bool
2591 check_prototype_declaration(sym_t *arg, sym_t *parg)
2592 {
2593 type_t *tp, *ptp;
2594 bool dowarn, msg;
2595
2596 tp = arg->s_type;
2597 ptp = parg->s_type;
2598
2599 msg = false;
2600 dowarn = false;
2601
2602 if (!eqtype(tp, ptp, true, true, &dowarn)) {
2603 if (eqtype(tp, ptp, true, false, &dowarn)) {
2604 /* type does not match prototype: %s */
2605 gnuism(58, arg->s_name);
2606 msg = sflag || !gflag;
2607 } else {
2608 /* type does not match prototype: %s */
2609 error(58, arg->s_name);
2610 msg = true;
2611 }
2612 } else if (dowarn) {
2613 if (sflag)
2614 /* type does not match prototype: %s */
2615 error(58, arg->s_name);
2616 else
2617 /* type does not match prototype: %s */
2618 warning(58, arg->s_name);
2619 msg = true;
2620 }
2621
2622 return msg;
2623 }
2624
2625 static void
2626 check_local_hiding(const sym_t *dsym)
2627 {
2628 switch (dsym->s_scl) {
2629 case AUTO:
2630 /* automatic hides external declaration: %s */
2631 warning(86, dsym->s_name);
2632 break;
2633 case STATIC:
2634 /* static hides external declaration: %s */
2635 warning(87, dsym->s_name);
2636 break;
2637 case TYPEDEF:
2638 /* typedef hides external declaration: %s */
2639 warning(88, dsym->s_name);
2640 break;
2641 case EXTERN:
2642 /* Already checked in declare_external_in_block. */
2643 break;
2644 default:
2645 lint_assert(/*CONSTCOND*/false);
2646 }
2647 }
2648
2649 static void
2650 check_local_redeclaration(const sym_t *dsym, sym_t *rsym)
2651 {
2652 if (rsym->s_block_level == 0) {
2653 if (hflag)
2654 check_local_hiding(dsym);
2655
2656 } else if (rsym->s_block_level == block_level) {
2657
2658 /* no hflag, because it's illegal! */
2659 if (rsym->s_arg) {
2660 /*
2661 * if !tflag, a "redeclaration of %s" error
2662 * is produced below
2663 */
2664 if (tflag) {
2665 if (hflag)
2666 /* declaration hides parameter: %s */
2667 warning(91, dsym->s_name);
2668 rmsym(rsym);
2669 }
2670 }
2671
2672 } else if (rsym->s_block_level < block_level) {
2673 if (hflag)
2674 /* declaration hides earlier one: %s */
2675 warning(95, dsym->s_name);
2676 }
2677
2678 if (rsym->s_block_level == block_level) {
2679 /* redeclaration of %s */
2680 error(27, dsym->s_name);
2681 rmsym(rsym);
2682 }
2683 }
2684
2685 /*
2686 * Completes a single local declaration/definition.
2687 */
2688 void
2689 declare_local(sym_t *dsym, bool initflg)
2690 {
2691
2692 /* Correct a mistake done in declarator_name(). */
2693 if (dsym->s_type->t_tspec == FUNC) {
2694 dsym->s_def = DECL;
2695 if (dcs->d_scl == NOSCL)
2696 dsym->s_scl = EXTERN;
2697 }
2698
2699 if (dsym->s_type->t_tspec == FUNC) {
2700 if (dsym->s_scl == STATIC) {
2701 /* dubious static function at block level: %s */
2702 warning(93, dsym->s_name);
2703 dsym->s_scl = EXTERN;
2704 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2705 /* function has illegal storage class: %s */
2706 error(94, dsym->s_name);
2707 dsym->s_scl = EXTERN;
2708 }
2709 }
2710
2711 /*
2712 * functions may be declared inline at local scope, although
2713 * this has no effect for a later definition of the same
2714 * function.
2715 * XXX it should have an effect if tflag is set. this would
2716 * also be the way gcc behaves.
2717 */
2718 if (dcs->d_inline) {
2719 if (dsym->s_type->t_tspec == FUNC) {
2720 dsym->s_inline = true;
2721 } else {
2722 /* variable declared inline: %s */
2723 warning(268, dsym->s_name);
2724 }
2725 }
2726
2727 check_function_definition(dsym, true);
2728
2729 check_type(dsym);
2730
2731 if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
2732 declare_external_in_block(dsym);
2733
2734 if (dsym->s_scl == EXTERN) {
2735 /*
2736 * XXX if the static variable at level 0 is only defined
2737 * later, checking will be possible.
2738 */
2739 if (dsym->s_ext_sym == NULL) {
2740 outsym(dsym, EXTERN, dsym->s_def);
2741 } else {
2742 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2743 }
2744 }
2745
2746 if (dcs->d_redeclared_symbol != NULL)
2747 check_local_redeclaration(dsym, dcs->d_redeclared_symbol);
2748
2749 if (initflg && !check_init(dsym)) {
2750 dsym->s_def = DEF;
2751 mark_as_set(dsym);
2752 }
2753
2754 if (dsym->s_scl == TYPEDEF) {
2755 dsym->s_type = block_dup_type(dsym->s_type);
2756 dsym->s_type->t_typedef = true;
2757 settdsym(dsym->s_type, dsym);
2758 }
2759
2760 /*
2761 * Before we can check the size we must wait for a initialization
2762 * which may follow.
2763 */
2764 }
2765
2766 /*
2767 * Processes (re)declarations of external symbols inside blocks.
2768 */
2769 static void
2770 declare_external_in_block(sym_t *dsym)
2771 {
2772 bool eqt, dowarn;
2773 sym_t *esym;
2774
2775 /* look for a symbol with the same name */
2776 esym = dcs->d_redeclared_symbol;
2777 while (esym != NULL && esym->s_block_level != 0) {
2778 while ((esym = esym->s_symtab_next) != NULL) {
2779 if (esym->s_kind != FVFT)
2780 continue;
2781 if (strcmp(dsym->s_name, esym->s_name) == 0)
2782 break;
2783 }
2784 }
2785 if (esym == NULL)
2786 return;
2787 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2788 /* gcc accepts this without a warning, pcc prints an error. */
2789 /* redeclaration of %s */
2790 warning(27, dsym->s_name);
2791 print_previous_declaration(-1, esym);
2792 return;
2793 }
2794
2795 dowarn = false;
2796 eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn);
2797
2798 if (!eqt || dowarn) {
2799 if (esym->s_scl == EXTERN) {
2800 /* inconsistent redeclaration of extern: %s */
2801 warning(90, dsym->s_name);
2802 print_previous_declaration(-1, esym);
2803 } else {
2804 /* inconsistent redeclaration of static: %s */
2805 warning(92, dsym->s_name);
2806 print_previous_declaration(-1, esym);
2807 }
2808 }
2809
2810 if (eqt) {
2811 /*
2812 * Remember the external symbol so we can update usage
2813 * information at the end of the block.
2814 */
2815 dsym->s_ext_sym = esym;
2816 }
2817 }
2818
2819 /*
2820 * Print an error or a warning if the symbol cannot be initialized due
2821 * to type/storage class. Return whether an error has been detected.
2822 */
2823 static bool
2824 check_init(sym_t *sym)
2825 {
2826 bool erred;
2827
2828 erred = false;
2829
2830 if (sym->s_type->t_tspec == FUNC) {
2831 /* cannot initialize function: %s */
2832 error(24, sym->s_name);
2833 erred = true;
2834 } else if (sym->s_scl == TYPEDEF) {
2835 /* cannot initialize typedef: %s */
2836 error(25, sym->s_name);
2837 erred = true;
2838 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2839 /* cannot initialize "extern" declaration: %s */
2840 if (dcs->d_ctx == EXTERN) {
2841 /* cannot initialize extern declaration: %s */
2842 warning(26, sym->s_name);
2843 } else {
2844 /* cannot initialize extern declaration: %s */
2845 error(26, sym->s_name);
2846 erred = true;
2847 }
2848 }
2849
2850 return erred;
2851 }
2852
2853 /*
2854 * Create a symbol for an abstract declaration.
2855 */
2856 sym_t *
2857 abstract_name(void)
2858 {
2859 sym_t *sym;
2860
2861 lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PROTO_ARG);
2862
2863 sym = block_zero_alloc(sizeof(*sym));
2864
2865 sym->s_name = unnamed;
2866 sym->s_def = DEF;
2867 sym->s_scl = ABSTRACT;
2868 sym->s_block_level = -1;
2869
2870 if (dcs->d_ctx == PROTO_ARG)
2871 sym->s_arg = true;
2872
2873 /*
2874 * At this point, dcs->d_type contains only the basic type. That
2875 * type will be updated later, adding pointers, arrays and functions
2876 * as necessary.
2877 */
2878 /*
2879 * XXX: This is not the correct type. For example in msg_347, it is
2880 * the type of the last prototype parameter, but it should rather be
2881 * the return type of the function.
2882 */
2883 sym->s_type = dcs->d_type;
2884 dcs->d_redeclared_symbol = NULL;
2885 dcs->d_vararg = false;
2886
2887 return sym;
2888 }
2889
2890 /*
2891 * Removes anything which has nothing to do on global level.
2892 */
2893 void
2894 global_clean_up(void)
2895 {
2896
2897 while (dcs->d_next != NULL)
2898 end_declaration_level();
2899
2900 clean_up_after_error();
2901 block_level = 0;
2902 mem_block_level = 0;
2903
2904 /*
2905 * remove all information about pending lint directives without
2906 * warnings.
2907 */
2908 global_clean_up_decl(true);
2909 }
2910
2911 /*
2912 * Process an abstract type declaration
2913 */
2914 sym_t *
2915 declare_1_abstract(sym_t *sym)
2916 {
2917
2918 check_function_definition(sym, true);
2919 check_type(sym);
2920 return sym;
2921 }
2922
2923 /*
2924 * Checks size after declarations of variables and their initialization.
2925 */
2926 void
2927 check_size(sym_t *dsym)
2928 {
2929
2930 if (dsym->s_def != DEF)
2931 return;
2932 if (dsym->s_scl == TYPEDEF)
2933 return;
2934 if (dsym->s_type->t_tspec == FUNC)
2935 return;
2936
2937 if (length(dsym->s_type, dsym->s_name) == 0 &&
2938 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2939 if (tflag) {
2940 /* empty array declaration: %s */
2941 warning(190, dsym->s_name);
2942 } else {
2943 /* empty array declaration: %s */
2944 error(190, dsym->s_name);
2945 }
2946 }
2947 }
2948
2949 /*
2950 * Mark an object as set if it is not already
2951 */
2952 void
2953 mark_as_set(sym_t *sym)
2954 {
2955
2956 if (!sym->s_set) {
2957 sym->s_set = true;
2958 UNIQUE_CURR_POS(sym->s_set_pos);
2959 }
2960 }
2961
2962 /*
2963 * Mark an object as used if it is not already
2964 */
2965 void
2966 mark_as_used(sym_t *sym, bool fcall, bool szof)
2967 {
2968
2969 if (!sym->s_used) {
2970 sym->s_used = true;
2971 UNIQUE_CURR_POS(sym->s_use_pos);
2972 }
2973 /*
2974 * for function calls another record is written
2975 *
2976 * XXX Should symbols used in sizeof() be treated as used or not?
2977 * Probably not, because there is no sense to declare an
2978 * external variable only to get their size.
2979 */
2980 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2981 outusg(sym);
2982 }
2983
2984 /*
2985 * Prints warnings for a list of variables and labels (concatenated
2986 * with s_level_next) if these are not used or only set.
2987 */
2988 void
2989 check_usage(dinfo_t *di)
2990 {
2991 sym_t *sym;
2992 int mklwarn;
2993
2994 /* for this warning LINTED has no effect */
2995 mklwarn = lwarn;
2996 lwarn = LWARN_ALL;
2997
2998 debug_step("begin lwarn %d", lwarn);
2999 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_level_next)
3000 check_usage_sym(di->d_asm, sym);
3001 lwarn = mklwarn;
3002 debug_step("end lwarn %d", lwarn);
3003 }
3004
3005 /*
3006 * Prints a warning for a single variable or label if it is not used or
3007 * only set.
3008 */
3009 void
3010 check_usage_sym(bool novar, sym_t *sym)
3011 {
3012
3013 if (sym->s_block_level == -1)
3014 return;
3015
3016 if (sym->s_kind == FVFT && sym->s_arg)
3017 check_argument_usage(novar, sym);
3018 else if (sym->s_kind == FVFT)
3019 check_variable_usage(novar, sym);
3020 else if (sym->s_kind == FLABEL)
3021 check_label_usage(sym);
3022 else if (sym->s_kind == FTAG)
3023 check_tag_usage(sym);
3024 }
3025
3026 static void
3027 check_argument_usage(bool novar, sym_t *arg)
3028 {
3029
3030 lint_assert(arg->s_set);
3031
3032 if (novar)
3033 return;
3034
3035 if (!arg->s_used && vflag) {
3036 /* argument '%s' unused in function '%s' */
3037 warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name);
3038 }
3039 }
3040
3041 static void
3042 check_variable_usage(bool novar, sym_t *sym)
3043 {
3044 scl_t sc;
3045 sym_t *xsym;
3046
3047 lint_assert(block_level != 0);
3048
3049 /* example at file scope: int c = ({ return 3; }); */
3050 if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0]))
3051 return;
3052
3053 /* errors in expressions easily cause lots of these warnings */
3054 if (nerr != 0)
3055 return;
3056
3057 /*
3058 * XXX Only variables are checked, although types should
3059 * probably also be checked
3060 */
3061 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
3062 sc != AUTO && sc != REG) {
3063 return;
3064 }
3065
3066 if (novar)
3067 return;
3068
3069 if (sc == EXTERN) {
3070 if (!sym->s_used && !sym->s_set) {
3071 /* '%s' unused in function '%s' */
3072 warning_at(192, &sym->s_def_pos,
3073 sym->s_name, funcsym->s_name);
3074 }
3075 } else {
3076 if (sym->s_set && !sym->s_used) {
3077 /* '%s' set but not used in function '%s' */
3078 warning_at(191, &sym->s_set_pos,
3079 sym->s_name, funcsym->s_name);
3080 } else if (!sym->s_used) {
3081 /* '%s' unused in function '%s' */
3082 warning_at(192, &sym->s_def_pos,
3083 sym->s_name, funcsym->s_name);
3084 }
3085 }
3086
3087 if (sc == EXTERN) {
3088 /*
3089 * information about usage is taken over into the symbol
3090 * table entry at level 0 if the symbol was locally declared
3091 * as an external symbol.
3092 *
3093 * XXX This is wrong for symbols declared static at level 0
3094 * if the usage information stems from sizeof(). This is
3095 * because symbols at level 0 only used in sizeof() are
3096 * considered to not be used.
3097 */
3098 if ((xsym = sym->s_ext_sym) != NULL) {
3099 if (sym->s_used && !xsym->s_used) {
3100 xsym->s_used = true;
3101 xsym->s_use_pos = sym->s_use_pos;
3102 }
3103 if (sym->s_set && !xsym->s_set) {
3104 xsym->s_set = true;
3105 xsym->s_set_pos = sym->s_set_pos;
3106 }
3107 }
3108 }
3109 }
3110
3111 static void
3112 check_label_usage(sym_t *lab)
3113 {
3114
3115 lint_assert(block_level == 1);
3116 lint_assert(lab->s_block_level == 1);
3117
3118 if (lab->s_set && !lab->s_used) {
3119 /* label '%s' unused in function '%s' */
3120 warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name);
3121 } else if (!lab->s_set) {
3122 /* undefined label '%s' */
3123 warning_at(23, &lab->s_use_pos, lab->s_name);
3124 }
3125 }
3126
3127 static void
3128 check_tag_usage(sym_t *sym)
3129 {
3130
3131 if (!is_incomplete(sym->s_type))
3132 return;
3133
3134 /* always complain about incomplete tags declared inside blocks */
3135 if (!zflag || dcs->d_ctx != EXTERN)
3136 return;
3137
3138 switch (sym->s_type->t_tspec) {
3139 case STRUCT:
3140 /* struct %s never defined */
3141 warning_at(233, &sym->s_def_pos, sym->s_name);
3142 break;
3143 case UNION:
3144 /* union %s never defined */
3145 warning_at(234, &sym->s_def_pos, sym->s_name);
3146 break;
3147 case ENUM:
3148 /* enum %s never defined */
3149 warning_at(235, &sym->s_def_pos, sym->s_name);
3150 break;
3151 default:
3152 lint_assert(/*CONSTCOND*/false);
3153 }
3154 }
3155
3156 /*
3157 * Called after the entire translation unit has been parsed.
3158 * Changes tentative definitions into definitions.
3159 * Performs some tests on global symbols. Detected problems are:
3160 * - defined variables of incomplete type
3161 * - constant variables which are not initialized
3162 * - static symbols which are never used
3163 */
3164 void
3165 check_global_symbols(void)
3166 {
3167 sym_t *sym;
3168
3169 if (block_level != 0 || dcs->d_next != NULL)
3170 norecover();
3171
3172 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_level_next) {
3173 if (sym->s_block_level == -1)
3174 continue;
3175 if (sym->s_kind == FVFT) {
3176 check_global_variable(sym);
3177 } else if (sym->s_kind == FTAG) {
3178 check_tag_usage(sym);
3179 } else {
3180 lint_assert(sym->s_kind == FMEMBER);
3181 }
3182 }
3183 }
3184
3185 static void
3186 check_unused_static_global_variable(const sym_t *sym)
3187 {
3188 if (sym->s_type->t_tspec == FUNC) {
3189 if (sym->s_def == DEF) {
3190 if (!sym->s_inline)
3191 /* static function %s unused */
3192 warning_at(236, &sym->s_def_pos, sym->s_name);
3193 } else {
3194 /* static function %s declared but not defined */
3195 warning_at(290, &sym->s_def_pos, sym->s_name);
3196 }
3197 } else if (!sym->s_set) {
3198 /* static variable %s unused */
3199 warning_at(226, &sym->s_def_pos, sym->s_name);
3200 } else {
3201 /* static variable %s set but not used */
3202 warning_at(307, &sym->s_def_pos, sym->s_name);
3203 }
3204 }
3205
3206 static void
3207 check_static_global_variable(const sym_t *sym)
3208 {
3209 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3210 /* static function called but not defined: %s() */
3211 error_at(225, &sym->s_use_pos, sym->s_name);
3212 }
3213
3214 if (!sym->s_used)
3215 check_unused_static_global_variable(sym);
3216
3217 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3218 /* const object %s should have initializer */
3219 warning_at(227, &sym->s_def_pos, sym->s_name);
3220 }
3221 }
3222
3223 static void
3224 check_global_variable(const sym_t *sym)
3225 {
3226
3227 if (sym->s_scl == TYPEDEF || sym->s_scl == CTCONST)
3228 return;
3229
3230 if (sym->s_scl == NOSCL)
3231 return; /* May be caused by a syntax error. */
3232
3233 lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
3234
3235 check_global_variable_size(sym);
3236
3237 if (sym->s_scl == STATIC)
3238 check_static_global_variable(sym);
3239 }
3240
3241 static void
3242 check_global_variable_size(const sym_t *sym)
3243 {
3244 pos_t cpos;
3245 int length_in_bits;
3246
3247 if (sym->s_def != TDEF)
3248 return;
3249 if (sym->s_type->t_tspec == FUNC)
3250 /*
3251 * this can happen if a syntax error occurred after a
3252 * function declaration
3253 */
3254 return;
3255 if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID)
3256 return; /* prevent internal error in length() below */
3257
3258 cpos = curr_pos;
3259 curr_pos = sym->s_def_pos;
3260 length_in_bits = length(sym->s_type, sym->s_name);
3261 curr_pos = cpos;
3262
3263 if (length_in_bits == 0 &&
3264 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3265 if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3266 /* empty array declaration: %s */
3267 warning_at(190, &sym->s_def_pos, sym->s_name);
3268 } else {
3269 /* empty array declaration: %s */
3270 error_at(190, &sym->s_def_pos, sym->s_name);
3271 }
3272 }
3273 }
3274
3275 /*
3276 * Prints information about location of previous definition/declaration.
3277 */
3278 void
3279 print_previous_declaration(int msg, const sym_t *psym)
3280 {
3281
3282 if (!rflag)
3283 return;
3284
3285 if (msg != -1) {
3286 (message_at)(msg, &psym->s_def_pos);
3287 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3288 /* previous definition of %s */
3289 message_at(261, &psym->s_def_pos, psym->s_name);
3290 } else {
3291 /* previous declaration of %s */
3292 message_at(260, &psym->s_def_pos, psym->s_name);
3293 }
3294 }
3295
3296 /*
3297 * Gets a node for a constant and returns the value of this constant
3298 * as integer.
3299 *
3300 * If the node is not constant or too large for int or of type float,
3301 * a warning will be printed.
3302 *
3303 * to_int_constant() should be used only inside declarations. If it is used in
3304 * expressions, it frees the memory used for the expression.
3305 */
3306 int
3307 to_int_constant(tnode_t *tn, bool required)
3308 {
3309 int i;
3310 tspec_t t;
3311 val_t *v;
3312
3313 v = constant(tn, required);
3314
3315 if (tn == NULL) {
3316 i = 1;
3317 goto done;
3318 }
3319
3320 /*
3321 * Abstract declarations are used inside expression. To free
3322 * the memory would be a fatal error.
3323 * We don't free blocks that are inside casts because these
3324 * will be used later to match types.
3325 */
3326 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
3327 expr_free_all();
3328
3329 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
3330 i = (int)v->v_ldbl;
3331 /* integral constant expression expected */
3332 error(55);
3333 } else {
3334 i = (int)v->v_quad;
3335 if (is_uinteger(t)) {
3336 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
3337 /* integral constant too large */
3338 warning(56);
3339 }
3340 } else {
3341 if (v->v_quad > (int64_t)TARG_INT_MAX ||
3342 v->v_quad < (int64_t)TARG_INT_MIN) {
3343 /* integral constant too large */
3344 warning(56);
3345 }
3346 }
3347 }
3348
3349 done:
3350 free(v);
3351 return i;
3352 }
3353