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