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