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