decl.c revision 1.283 1 /* $NetBSD: decl.c,v 1.283 2022/05/31 00:35: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)
41 __RCSID("$NetBSD: decl.c,v 1.283 2022/05/31 00:35: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_kind = DK_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, allow_c90 ? "C90" : "traditional C");
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 (allow_c90)
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(declaration_kind dk)
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_kind = dk;
586 di->d_ldlsym = &di->d_dlsyms;
587 debug_step("%s(%s)", __func__, declaration_kind_name(dk));
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__, declaration_kind_name(dcs->d_kind));
599
600 lint_assert(dcs->d_enclosing != NULL);
601 di = dcs;
602 dcs = di->d_enclosing;
603 switch (di->d_kind) {
604 case DK_MOS:
605 case DK_MOU:
606 case DK_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 DK_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 DK_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 DK_AUTO:
642 /* check usage of local vars */
643 check_usage(di);
644 /* FALLTHROUGH */
645 case DK_PROTO_ARG:
646 /* usage of arguments will be checked by funcend() */
647 rmsyms(di->d_dlsyms);
648 break;
649 case DK_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_kind == DK_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_kind == DK_OLD_STYLE_ARG ||
717 dcs->d_kind == DK_PROTO_ARG) {
718 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) {
719 /* only register valid as formal parameter storage... */
720 error(9);
721 dcs->d_scl = NOSCL;
722 }
723 }
724 }
725
726 /*
727 * Merge the declaration specifiers from dcs into dcs->d_type.
728 *
729 * See C99 6.7.2 "Type specifiers".
730 */
731 static void
732 dcs_merge_declaration_specifiers(void)
733 {
734 tspec_t t, s, l, c;
735 type_t *tp;
736
737 t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */
738 c = dcs->d_complex_mod; /* FLOAT or DOUBLE */
739 s = dcs->d_sign_mod; /* SIGNED or UNSIGN */
740 l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */
741 tp = dcs->d_type;
742
743 debug_step("%s: %s", __func__, type_name(tp));
744 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
745 tp == NULL)
746 dcs->d_notyp = true;
747 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
748 tp == NULL)
749 t = c;
750
751 if (tp != NULL) {
752 lint_assert(t == NOTSPEC);
753 lint_assert(s == NOTSPEC);
754 lint_assert(l == NOTSPEC);
755 return;
756 }
757
758 if (t == NOTSPEC)
759 t = INT;
760 if (s == NOTSPEC && t == INT)
761 s = SIGNED;
762 if (l != NOTSPEC && t == CHAR) {
763 dcs->d_invalid_type_combination = true;
764 l = NOTSPEC;
765 }
766 if (l == LONG && t == FLOAT) {
767 l = NOTSPEC;
768 t = DOUBLE;
769 if (allow_c90)
770 /* use 'double' instead of 'long float' */
771 warning(6);
772 }
773 if ((l == LONG && t == DOUBLE) || t == LDOUBLE) {
774 l = NOTSPEC;
775 t = LDOUBLE;
776 }
777 if (t == LDOUBLE && !allow_c90) {
778 /* 'long double' is illegal in traditional C */
779 warning(266);
780 }
781 if (l == LONG && t == DCOMPLEX) {
782 l = NOTSPEC;
783 t = LCOMPLEX;
784 }
785
786 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
787 dcs->d_invalid_type_combination = true;
788 l = s = NOTSPEC;
789 }
790 if (l != NOTSPEC)
791 t = l;
792 dcs->d_type = gettyp(merge_signedness(t, s));
793 }
794
795 /*
796 * Create a type structure from the information gathered in
797 * the declaration stack.
798 * Complain about storage classes which are not possible in current
799 * context.
800 */
801 void
802 end_type(void)
803 {
804
805 dcs_merge_declaration_specifiers();
806
807 if (dcs->d_multiple_storage_classes) {
808 /* only one storage class allowed */
809 error(7);
810 }
811 if (dcs->d_invalid_type_combination) {
812 /* illegal type combination */
813 error(4);
814 }
815
816 dcs_adjust_storage_class();
817
818 if (dcs->d_const && dcs->d_type->t_const && !dcs->d_type->t_typeof) {
819 lint_assert(dcs->d_type->t_typedef);
820 /* typedef already qualified with '%s' */
821 warning(68, "const");
822 }
823 if (dcs->d_volatile && dcs->d_type->t_volatile &&
824 !dcs->d_type->t_typeof) {
825 lint_assert(dcs->d_type->t_typedef);
826 /* typedef already qualified with '%s' */
827 warning(68, "volatile");
828 }
829
830 if (dcs->d_const || dcs->d_volatile) {
831 dcs->d_type = block_dup_type(dcs->d_type);
832 dcs->d_type->t_const |= dcs->d_const;
833 dcs->d_type->t_volatile |= dcs->d_volatile;
834 }
835 }
836
837 /*
838 * Return the length of a type in bits.
839 *
840 * Printing a message if the outermost dimension of an array is 0 must
841 * be done by the caller. All other problems are reported by this function
842 * if name is not NULL.
843 */
844 int
845 length_in_bits(const type_t *tp, const char *name)
846 {
847 unsigned int elem, elsz;
848
849 elem = 1;
850 while (tp != NULL && tp->t_tspec == ARRAY) {
851 elem *= tp->t_dim;
852 tp = tp->t_subt;
853 }
854 if (tp == NULL)
855 return -1;
856
857 switch (tp->t_tspec) {
858 case FUNC:
859 /* compiler takes size of function */
860 INTERNAL_ERROR("%s", msgs[12]);
861 /* NOTREACHED */
862 case STRUCT:
863 case UNION:
864 if (is_incomplete(tp) && name != NULL) {
865 /* '%s' has incomplete type '%s' */
866 error(31, name, type_name(tp));
867 }
868 elsz = tp->t_str->sou_size_in_bits;
869 break;
870 case ENUM:
871 if (is_incomplete(tp) && name != NULL) {
872 /* incomplete enum type: %s */
873 warning(13, name);
874 }
875 /* FALLTHROUGH */
876 default:
877 elsz = size_in_bits(tp->t_tspec);
878 /*
879 * Workaround until the type parser (see add_function,
880 * add_array, add_pointer) does not construct the invalid
881 * intermediate declaration 'void b[4]' for the legitimate
882 * declaration 'void *b[4]'.
883 */
884 if (sytxerr > 0 && elsz == 0)
885 elsz = CHAR_SIZE;
886 lint_assert(elsz > 0);
887 break;
888 }
889 return (int)(elem * elsz);
890 }
891
892 unsigned int
893 alignment_in_bits(const type_t *tp)
894 {
895 unsigned int a;
896 tspec_t t;
897
898 /* Super conservative so that it works for most systems. */
899 unsigned int worst_align_in_bits = 2 * LONG_SIZE;
900
901 while (tp->t_tspec == ARRAY)
902 tp = tp->t_subt;
903
904 if (is_struct_or_union(t = tp->t_tspec)) {
905 a = tp->t_str->sou_align_in_bits;
906 } else {
907 lint_assert(t != FUNC);
908 if ((a = size_in_bits(t)) == 0) {
909 a = CHAR_SIZE;
910 } else if (a > worst_align_in_bits) {
911 a = worst_align_in_bits;
912 }
913 }
914 lint_assert(a >= CHAR_SIZE);
915 lint_assert(a <= worst_align_in_bits);
916 return a;
917 }
918
919 /*
920 * Concatenate two lists of symbols by s_next. Used by declarations of
921 * struct/union/enum elements and parameters.
922 */
923 sym_t *
924 lnklst(sym_t *l1, sym_t *l2)
925 {
926 sym_t *l;
927
928 if ((l = l1) == NULL)
929 return l2;
930 while (l1->s_next != NULL)
931 l1 = l1->s_next;
932 l1->s_next = l2;
933 return l;
934 }
935
936 /*
937 * Check if the type of the given symbol is valid and print an error
938 * message if it is not.
939 *
940 * Invalid types are:
941 * - arrays of incomplete types or functions
942 * - functions returning arrays or functions
943 * - void types other than type of function or pointer
944 */
945 void
946 check_type(sym_t *sym)
947 {
948 tspec_t to, t;
949 type_t **tpp, *tp;
950
951 tpp = &sym->s_type;
952 to = NOTSPEC;
953 while ((tp = *tpp) != NULL) {
954 t = tp->t_tspec;
955 /*
956 * If this is the type of an old style function definition,
957 * a better warning is printed in funcdef().
958 */
959 if (t == FUNC && !tp->t_proto &&
960 !(to == NOTSPEC && sym->s_osdef)) {
961 /* TODO: Make this an error in C99 mode as well. */
962 if ((!allow_trad && !allow_c99) && hflag)
963 /* function declaration is not a prototype */
964 warning(287);
965 }
966 if (to == FUNC) {
967 if (t == FUNC || t == ARRAY) {
968 /* function returns illegal type '%s' */
969 error(15, type_name(tp));
970 if (t == FUNC) {
971 *tpp = block_derive_type(*tpp, PTR);
972 } else {
973 *tpp = block_derive_type(
974 (*tpp)->t_subt, PTR);
975 }
976 return;
977 } else if (tp->t_const || tp->t_volatile) {
978 /* TODO: Make this a warning in C99 mode as well. */
979 if (!allow_trad && !allow_c99) { /* XXX or better allow_c90? */
980 /* function cannot return const... */
981 warning(228);
982 }
983 }
984 } else if (to == ARRAY) {
985 if (t == FUNC) {
986 /* array of function is illegal */
987 error(16);
988 *tpp = gettyp(INT);
989 return;
990 } else if (t == ARRAY && tp->t_dim == 0) {
991 /* null dimension */
992 error(17);
993 return;
994 } else if (t == VOID) {
995 /* illegal use of 'void' */
996 error(18);
997 *tpp = gettyp(INT);
998 #if 0 /* errors are produced by length_in_bits */
999 } else if (is_incomplete(tp)) {
1000 /* array of incomplete type */
1001 /* TODO: Make this an error in C99 mode as well. */
1002 if (!allow_trad && !allow_c99) {
1003 /* array of incomplete type */
1004 error(301);
1005 } else {
1006 /* array of incomplete type */
1007 warning(301);
1008 }
1009 #endif
1010 }
1011 } else if (to == NOTSPEC && t == VOID) {
1012 if (dcs->d_kind == DK_PROTO_ARG) {
1013 if (sym->s_scl != ABSTRACT) {
1014 lint_assert(sym->s_name != unnamed);
1015 /* void parameter cannot have ... */
1016 error(61, sym->s_name);
1017 *tpp = gettyp(INT);
1018 }
1019 } else if (dcs->d_kind == DK_ABSTRACT) {
1020 /* ok */
1021 } else if (sym->s_scl != TYPEDEF) {
1022 /* void type for '%s' */
1023 error(19, sym->s_name);
1024 *tpp = gettyp(INT);
1025 }
1026 }
1027 if (t == VOID && to != PTR) {
1028 if (tp->t_const || tp->t_volatile) {
1029 /* inappropriate qualifiers with 'void' */
1030 warning(69);
1031 tp->t_const = tp->t_volatile = false;
1032 }
1033 }
1034 tpp = &tp->t_subt;
1035 to = t;
1036 }
1037 }
1038
1039 /*
1040 * In traditional C, the only portable type for bit-fields is unsigned int.
1041 *
1042 * In C90, the only allowed types for bit-fields are int, signed int and
1043 * unsigned int (3.5.2.1). There is no mention of implementation-defined
1044 * types.
1045 *
1046 * In C99, the only portable types for bit-fields are _Bool, signed int and
1047 * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other
1048 * implementation-defined type".
1049 */
1050 static void
1051 check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t)
1052 {
1053 type_t *tp = *inout_tp;
1054 tspec_t t = *inout_t;
1055
1056 if (t == CHAR || t == UCHAR || t == SCHAR ||
1057 t == SHORT || t == USHORT || t == ENUM) {
1058 if (!bitfieldtype_ok) {
1059 /* TODO: Make this an error in C99 mode as well. */
1060 if (!allow_trad && !allow_c99) {
1061 /* bit-field type '%s' invalid in ANSI C */
1062 warning(273, type_name(tp));
1063 } else if (pflag) {
1064 /* nonportable bit-field type '%s' */
1065 warning(34, type_name(tp));
1066 }
1067 }
1068 } else if (t == INT && dcs->d_sign_mod == NOTSPEC) {
1069 if (pflag && !bitfieldtype_ok) {
1070 /* bit-field of type plain 'int' has ... */
1071 warning(344);
1072 }
1073 } else if (!(t == INT || t == UINT || t == BOOL ||
1074 (is_integer(t) && (bitfieldtype_ok || allow_gcc)))) {
1075
1076 /* illegal bit-field type '%s' */
1077 warning(35, type_name(tp));
1078
1079 unsigned int sz = tp->t_flen;
1080 dsym->s_type = tp = block_dup_type(gettyp(t = INT));
1081 if ((tp->t_flen = sz) > size_in_bits(t))
1082 tp->t_flen = size_in_bits(t);
1083 *inout_t = t;
1084 *inout_tp = tp;
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_kind == DK_MOU) {
1164 o = dcs->d_offset_in_bits;
1165 dcs->d_offset_in_bits = 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 = dcs->d_offset_in_bits -
1170 dcs->d_offset_in_bits % size_in_bits(t);
1171 tp->t_foffs = dcs->d_offset_in_bits -
1172 dsym->u.s_member.sm_offset_in_bits;
1173 dcs->d_offset_in_bits += tp->t_flen;
1174 } else {
1175 align(alignment_in_bits(tp), 0);
1176 dsym->u.s_member.sm_offset_in_bits = dcs->d_offset_in_bits;
1177 dcs->d_offset_in_bits += sz;
1178 }
1179 if (dcs->d_kind == DK_MOU) {
1180 if (o > dcs->d_offset_in_bits)
1181 dcs->d_offset_in_bits = 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_in_bits + (al - 1)) & ~(al - 1);
1214 if (len == 0 || dcs->d_offset_in_bits + len > no)
1215 dcs->d_offset_in_bits = 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 (!allow_c90)
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_kind == DK_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_kind == DK_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_kind) {
1574 case DK_MOS:
1575 case DK_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_kind == DK_MOS ? MOS : MOU;
1581 break;
1582 case DK_EXTERN:
1583 /*
1584 * static and external symbols without "extern" are
1585 * considered to be tentatively 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 DK_PROTO_ARG:
1604 sym->s_arg = true;
1605 /* FALLTHROUGH */
1606 case DK_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 DK_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 DK_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 /* TODO: Make this an error in C99 mode as well. */
1707 if (allow_c90 && ((!allow_trad && !allow_c99) || 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 (allow_c90) {
1761 /* XXX: Why is this warning suppressed in C90 mode? */
1762 if (allow_trad || allow_c99)
1763 /* declaration introduces new ... */
1764 warning(44, storage_class_name(scl),
1765 tag->s_name);
1766 tag = pushdown(tag);
1767 } else if (tag->s_scl != scl) {
1768 /* base type is really '%s %s' */
1769 warning(45, storage_class_name(tag->s_scl),
1770 tag->s_name);
1771 }
1772 dcs->d_enclosing->d_nonempty_decl = true;
1773 } else if (decl) {
1774 /* "struct a { ... } " */
1775 if (hflag)
1776 /* redefinition hides earlier one: %s */
1777 warning(43, tag->s_name);
1778 tag = pushdown(tag);
1779 dcs->d_enclosing->d_nonempty_decl = true;
1780 } else if (tag->s_scl != scl) {
1781 /* base type is really '%s %s' */
1782 warning(45, storage_class_name(tag->s_scl),
1783 tag->s_name);
1784 /* XXX: Why is this warning suppressed in C90 mode? */
1785 if (allow_trad || allow_c99) {
1786 /* declaration introduces new type in ... */
1787 warning(44, storage_class_name(scl),
1788 tag->s_name);
1789 }
1790 tag = pushdown(tag);
1791 dcs->d_enclosing->d_nonempty_decl = true;
1792 }
1793 } else {
1794 if (tag->s_scl != scl ||
1795 (decl && !is_incomplete(tag->s_type))) {
1796 /* %s tag '%s' redeclared as %s */
1797 error(46, storage_class_name(tag->s_scl),
1798 tag->s_name, storage_class_name(scl));
1799 print_previous_declaration(-1, tag);
1800 tag = pushdown(tag);
1801 dcs->d_enclosing->d_nonempty_decl = true;
1802 } else if (semi || decl) {
1803 dcs->d_enclosing->d_nonempty_decl = true;
1804 }
1805 }
1806 return tag;
1807 }
1808
1809 const char *
1810 storage_class_name(scl_t sc)
1811 {
1812 switch (sc) {
1813 case EXTERN: return "extern";
1814 case STATIC: return "static";
1815 case AUTO: return "auto";
1816 case REG: return "register";
1817 case TYPEDEF: return "typedef";
1818 case STRUCT_TAG:return "struct";
1819 case UNION_TAG: return "union";
1820 case ENUM_TAG: return "enum";
1821 default: lint_assert(/*CONSTCOND*/false);
1822 }
1823 /* NOTREACHED */
1824 }
1825
1826 /*
1827 * tp points to the type of the tag, fmem to the list of members.
1828 */
1829 type_t *
1830 complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1831 {
1832 tspec_t t;
1833 struct_or_union *sp;
1834 int n;
1835 sym_t *mem;
1836
1837 if (tp == NULL) /* in case of syntax errors */
1838 return gettyp(INT);
1839
1840 if (tp->t_tspec == ENUM)
1841 tp->t_enum->en_incomplete = false;
1842 else
1843 tp->t_str->sou_incomplete = false;
1844
1845 t = tp->t_tspec;
1846 align((u_int)dcs->d_sou_align_in_bits, 0);
1847 sp = tp->t_str;
1848 sp->sou_align_in_bits = dcs->d_sou_align_in_bits;
1849 sp->sou_first_member = fmem;
1850 if (tp->t_packed)
1851 setpackedsize(tp);
1852 else
1853 sp->sou_size_in_bits = dcs->d_offset_in_bits;
1854
1855 if (sp->sou_size_in_bits == 0) {
1856 /* zero sized %s is a C99 feature */
1857 c99ism(47, ttab[t].tt_name);
1858 }
1859
1860 n = 0;
1861 for (mem = fmem; mem != NULL; mem = mem->s_next) {
1862 /* bind anonymous members to the structure */
1863 if (mem->u.s_member.sm_sou_type == NULL) {
1864 mem->u.s_member.sm_sou_type = sp;
1865 if (mem->s_type->t_bitfield) {
1866 sp->sou_size_in_bits += bitfieldsize(&mem);
1867 if (mem == NULL)
1868 break;
1869 }
1870 sp->sou_size_in_bits +=
1871 type_size_in_bits(mem->s_type);
1872 }
1873 if (mem->s_name != unnamed)
1874 n++;
1875 }
1876
1877 if (n == 0 && sp->sou_size_in_bits != 0) {
1878 /* %s has no named members */
1879 warning(65, t == STRUCT ? "structure" : "union");
1880 }
1881 return tp;
1882 }
1883
1884 type_t *
1885 complete_tag_enum(type_t *tp, sym_t *fmem)
1886 {
1887
1888 tp->t_enum->en_incomplete = false;
1889 tp->t_enum->en_first_enumerator = fmem;
1890 return tp;
1891 }
1892
1893 /*
1894 * Processes the name of an enumerator in an enum declaration.
1895 *
1896 * sym points to the enumerator
1897 * val is the value of the enumerator
1898 * impl is true if the value of the enumerator was not explicitly specified.
1899 */
1900 sym_t *
1901 enumeration_constant(sym_t *sym, int val, bool impl)
1902 {
1903
1904 if (sym->s_scl != NOSCL) {
1905 if (sym->s_block_level == block_level) {
1906 /* no hflag, because this is illegal!!! */
1907 if (sym->s_arg) {
1908 /* enumeration constant hides parameter: %s */
1909 warning(57, sym->s_name);
1910 } else {
1911 /* redeclaration of %s */
1912 error(27, sym->s_name);
1913 /*
1914 * inside blocks it should not be too
1915 * complicated to find the position of the
1916 * previous declaration
1917 */
1918 if (block_level == 0)
1919 print_previous_declaration(-1, sym);
1920 }
1921 } else {
1922 if (hflag)
1923 /* redefinition hides earlier one: %s */
1924 warning(43, sym->s_name);
1925 }
1926 sym = pushdown(sym);
1927 }
1928 sym->s_scl = ENUM_CONST;
1929 sym->s_type = dcs->d_tagtyp;
1930 sym->u.s_enum_constant = val;
1931 if (impl && val == TARG_INT_MIN) {
1932 /* overflow in enumeration values: %s */
1933 warning(48, sym->s_name);
1934 }
1935 enumval = val == TARG_INT_MAX ? TARG_INT_MIN : val + 1;
1936 return sym;
1937 }
1938
1939 /*
1940 * Process a single external declarator.
1941 */
1942 static void
1943 declare_extern(sym_t *dsym, bool initflg, sbuf_t *renaming)
1944 {
1945 bool dowarn, rval, redec;
1946 sym_t *rdsym;
1947 char *s;
1948
1949 if (renaming != NULL) {
1950 lint_assert(dsym->s_rename == NULL);
1951
1952 s = level_zero_alloc(1, renaming->sb_len + 1);
1953 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1954 dsym->s_rename = s;
1955 }
1956
1957 check_function_definition(dsym, true);
1958
1959 check_type(dsym);
1960
1961 if (initflg && !check_init(dsym))
1962 dsym->s_def = DEF;
1963
1964 /*
1965 * Declarations of functions are marked as "tentative" in
1966 * declarator_name(). This is wrong because there are no
1967 * tentative function definitions.
1968 */
1969 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1970 dsym->s_def = DECL;
1971
1972 if (dcs->d_inline) {
1973 if (dsym->s_type->t_tspec == FUNC) {
1974 dsym->s_inline = true;
1975 } else {
1976 /* variable declared inline: %s */
1977 warning(268, dsym->s_name);
1978 }
1979 }
1980
1981 /* Write the declaration into the output file */
1982 if (plibflg && llibflg &&
1983 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1984 /*
1985 * With both LINTLIBRARY and PROTOLIB the prototype is
1986 * written as a function definition to the output file.
1987 */
1988 rval = dsym->s_type->t_subt->t_tspec != VOID;
1989 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1990 } else if (!is_compiler_builtin(dsym->s_name)) {
1991 outsym(dsym, dsym->s_scl, dsym->s_def);
1992 }
1993
1994 if ((rdsym = dcs->d_redeclared_symbol) != NULL) {
1995
1996 /*
1997 * If the old symbol stems from an old style function
1998 * definition, we have remembered the params in
1999 * rdsym->s_old_style_args and compare them with the params
2000 * of the prototype.
2001 */
2002 if (rdsym->s_osdef && dsym->s_type->t_proto) {
2003 redec = check_old_style_definition(rdsym, dsym);
2004 } else {
2005 redec = false;
2006 }
2007
2008 if (!redec &&
2009 !check_redeclaration(dsym, (dowarn = false, &dowarn))) {
2010
2011 if (dowarn) {
2012 /* TODO: Make this an error in C99 mode as well. */
2013 if (!allow_trad && !allow_c99)
2014 /* redeclaration of %s */
2015 error(27, dsym->s_name);
2016 else
2017 /* redeclaration of %s */
2018 warning(27, dsym->s_name);
2019 print_previous_declaration(-1, rdsym);
2020 }
2021
2022 /*
2023 * Take over the remembered params if the new symbol
2024 * is not a prototype.
2025 */
2026 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
2027 dsym->s_osdef = rdsym->s_osdef;
2028 dsym->u.s_old_style_args =
2029 rdsym->u.s_old_style_args;
2030 dsym->s_def_pos = rdsym->s_def_pos;
2031 }
2032
2033 /*
2034 * Remember the position of the declaration if the
2035 * old symbol was a prototype and the new is not.
2036 * Also remember the position if the old symbol
2037 * was defined and the new is not.
2038 */
2039 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
2040 dsym->s_def_pos = rdsym->s_def_pos;
2041 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
2042 dsym->s_def_pos = rdsym->s_def_pos;
2043 }
2044
2045 /*
2046 * Copy usage information of the name into the new
2047 * symbol.
2048 */
2049 copy_usage_info(dsym, rdsym);
2050
2051 /* Once a name is defined, it remains defined. */
2052 if (rdsym->s_def == DEF)
2053 dsym->s_def = DEF;
2054
2055 /* once a function is inline, it remains inline */
2056 if (rdsym->s_inline)
2057 dsym->s_inline = true;
2058
2059 complete_type(dsym, rdsym);
2060
2061 }
2062
2063 rmsym(rdsym);
2064 }
2065
2066 if (dsym->s_scl == TYPEDEF) {
2067 dsym->s_type = block_dup_type(dsym->s_type);
2068 dsym->s_type->t_typedef = true;
2069 settdsym(dsym->s_type, dsym);
2070 }
2071
2072 }
2073
2074 void
2075 declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2076 {
2077
2078 if (dcs->d_kind == DK_EXTERN) {
2079 declare_extern(decl, initflg, renaming);
2080 } else if (dcs->d_kind == DK_OLD_STYLE_ARG ||
2081 dcs->d_kind == DK_PROTO_ARG) {
2082 if (renaming != NULL) {
2083 /* symbol renaming can't be used on function arguments */
2084 error(310);
2085 } else
2086 (void)declare_argument(decl, initflg);
2087 } else {
2088 lint_assert(dcs->d_kind == DK_AUTO);
2089 if (renaming != NULL) {
2090 /* symbol renaming can't be used on automatic variables */
2091 error(311);
2092 } else
2093 declare_local(decl, initflg);
2094 }
2095 }
2096
2097 /*
2098 * Copies information about usage into a new symbol table entry of
2099 * the same symbol.
2100 */
2101 void
2102 copy_usage_info(sym_t *sym, sym_t *rdsym)
2103 {
2104
2105 sym->s_set_pos = rdsym->s_set_pos;
2106 sym->s_use_pos = rdsym->s_use_pos;
2107 sym->s_set = rdsym->s_set;
2108 sym->s_used = rdsym->s_used;
2109 }
2110
2111 /*
2112 * Prints an error and returns true if a symbol is redeclared/redefined.
2113 * Otherwise returns false and, in some cases of minor problems, prints
2114 * a warning.
2115 */
2116 bool
2117 check_redeclaration(sym_t *dsym, bool *dowarn)
2118 {
2119 sym_t *rsym;
2120
2121 rsym = dcs->d_redeclared_symbol;
2122 if (rsym->s_scl == ENUM_CONST) {
2123 /* redeclaration of %s */
2124 error(27, dsym->s_name);
2125 print_previous_declaration(-1, rsym);
2126 return true;
2127 }
2128 if (rsym->s_scl == TYPEDEF) {
2129 /* typedef redeclared: %s */
2130 error(89, dsym->s_name);
2131 print_previous_declaration(-1, rsym);
2132 return true;
2133 }
2134 if (dsym->s_scl == TYPEDEF) {
2135 /* redeclaration of %s */
2136 error(27, dsym->s_name);
2137 print_previous_declaration(-1, rsym);
2138 return true;
2139 }
2140 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2141 /* redefinition of %s */
2142 error(28, dsym->s_name);
2143 print_previous_declaration(-1, rsym);
2144 return true;
2145 }
2146 if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) {
2147 /* redeclaration of '%s' with type '%s', expected '%s' */
2148 error(347, dsym->s_name,
2149 type_name(dsym->s_type), type_name(rsym->s_type));
2150 print_previous_declaration(-1, rsym);
2151 return true;
2152 }
2153 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2154 return false;
2155 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2156 return false;
2157 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2158 return false;
2159 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2160 /*
2161 * All cases except "int a = 1; static int a;" are caught
2162 * above with or without a warning
2163 */
2164 /* redeclaration of %s */
2165 error(27, dsym->s_name);
2166 print_previous_declaration(-1, rsym);
2167 return true;
2168 }
2169 if (rsym->s_scl == EXTERN) {
2170 /* previously declared extern, becomes static: %s */
2171 warning(29, dsym->s_name);
2172 print_previous_declaration(-1, rsym);
2173 return false;
2174 }
2175 /*
2176 * Now it's one of:
2177 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2178 */
2179 /* redeclaration of %s; ANSI C requires "static" */
2180 /* TODO: Make this an error in C99 mode as well. */
2181 if (!allow_trad && !allow_c99) {
2182 /* redeclaration of %s; ANSI C requires static */
2183 warning(30, dsym->s_name);
2184 print_previous_declaration(-1, rsym);
2185 }
2186 dsym->s_scl = STATIC;
2187 return false;
2188 }
2189
2190 static bool
2191 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
2192 {
2193 if (tp1->t_const != tp2->t_const && !ignqual && allow_c90)
2194 return false;
2195
2196 if (tp1->t_volatile != tp2->t_volatile && !ignqual && allow_c90)
2197 return false;
2198
2199 return true;
2200 }
2201
2202 bool
2203 eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual)
2204 {
2205 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2206 return false;
2207
2208 if (!qualifiers_correspond(tp1, tp2, ignqual))
2209 return false;
2210
2211 return true;
2212 }
2213
2214
2215 /*
2216 * Checks if two types are compatible.
2217 *
2218 * ignqual ignore qualifiers of type; used for function params
2219 * promot promote left type; used for comparison of params of
2220 * old style function definitions with params of prototypes.
2221 * *dowarn set to true if an old style function declaration is not
2222 * compatible with a prototype
2223 */
2224 bool
2225 eqtype(const type_t *tp1, const type_t *tp2,
2226 bool ignqual, bool promot, bool *dowarn)
2227 {
2228 tspec_t t;
2229
2230 while (tp1 != NULL && tp2 != NULL) {
2231
2232 t = tp1->t_tspec;
2233 if (promot) {
2234 if (t == FLOAT) {
2235 t = DOUBLE;
2236 } else if (t == CHAR || t == SCHAR) {
2237 t = INT;
2238 } else if (t == UCHAR) {
2239 t = allow_c90 ? INT : UINT;
2240 } else if (t == SHORT) {
2241 t = INT;
2242 } else if (t == USHORT) {
2243 /* CONSTCOND */
2244 t = TARG_INT_MAX < TARG_USHRT_MAX || !allow_c90
2245 ? UINT : INT;
2246 }
2247 }
2248
2249 if (t != tp2->t_tspec)
2250 return false;
2251
2252 if (!qualifiers_correspond(tp1, tp2, ignqual))
2253 return false;
2254
2255 if (t == STRUCT || t == UNION)
2256 return tp1->t_str == tp2->t_str;
2257
2258 if (t == ENUM && eflag)
2259 return tp1->t_enum == tp2->t_enum;
2260
2261 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2262 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2263 return false;
2264 }
2265
2266 /* don't check prototypes for traditional */
2267 if (t == FUNC && allow_c90) {
2268 if (tp1->t_proto && tp2->t_proto) {
2269 if (!eqargs(tp1, tp2, dowarn))
2270 return false;
2271 } else if (tp1->t_proto) {
2272 if (!mnoarg(tp1, dowarn))
2273 return false;
2274 } else if (tp2->t_proto) {
2275 if (!mnoarg(tp2, dowarn))
2276 return false;
2277 }
2278 }
2279
2280 tp1 = tp1->t_subt;
2281 tp2 = tp2->t_subt;
2282 ignqual = promot = false;
2283
2284 }
2285
2286 return tp1 == tp2;
2287 }
2288
2289 /*
2290 * Compares the parameter types of two prototypes.
2291 */
2292 static bool
2293 eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn)
2294 {
2295 sym_t *a1, *a2;
2296
2297 if (tp1->t_vararg != tp2->t_vararg)
2298 return false;
2299
2300 a1 = tp1->t_args;
2301 a2 = tp2->t_args;
2302
2303 while (a1 != NULL && a2 != NULL) {
2304
2305 if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn))
2306 return false;
2307
2308 a1 = a1->s_next;
2309 a2 = a2->s_next;
2310
2311 }
2312
2313 return a1 == a2;
2314 }
2315
2316 /*
2317 * mnoarg() (matches functions with no argument type information)
2318 * returns whether all parameters of a prototype are compatible with
2319 * an old style function declaration.
2320 * This is the case if the following conditions are met:
2321 * 1. the prototype has a fixed number of parameters
2322 * 2. no parameter is of type float
2323 * 3. no parameter is converted to another type if integer promotion
2324 * is applied on it
2325 */
2326 static bool
2327 mnoarg(const type_t *tp, bool *dowarn)
2328 {
2329 sym_t *arg;
2330 tspec_t t;
2331
2332 if (tp->t_vararg) {
2333 if (dowarn != NULL)
2334 *dowarn = true;
2335 }
2336 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2337 if ((t = arg->s_type->t_tspec) == FLOAT ||
2338 t == CHAR || t == SCHAR || t == UCHAR ||
2339 t == SHORT || t == USHORT) {
2340 if (dowarn != NULL)
2341 *dowarn = true;
2342 }
2343 }
2344 return true;
2345 }
2346
2347 /*
2348 * Compares a prototype declaration with the remembered arguments of
2349 * a previous old style function definition.
2350 */
2351 static bool
2352 check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2353 {
2354 sym_t *args, *pargs, *arg, *parg;
2355 int narg, nparg, n;
2356 bool dowarn, msg;
2357
2358 args = rdsym->u.s_old_style_args;
2359 pargs = dsym->s_type->t_args;
2360
2361 msg = false;
2362
2363 narg = nparg = 0;
2364 for (arg = args; arg != NULL; arg = arg->s_next)
2365 narg++;
2366 for (parg = pargs; parg != NULL; parg = parg->s_next)
2367 nparg++;
2368 if (narg != nparg) {
2369 /* prototype does not match old-style definition */
2370 error(63);
2371 msg = true;
2372 goto end;
2373 }
2374
2375 arg = args;
2376 parg = pargs;
2377 n = 1;
2378 while (narg-- > 0) {
2379 dowarn = false;
2380 /*
2381 * If it does not match due to promotion and lint runs in
2382 * "traditional to C90" migration mode, print only a warning.
2383 *
2384 * XXX: Where is this "only a warning"?
2385 */
2386 if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) ||
2387 dowarn) {
2388 /* prototype does not match old style ... */
2389 error(299, n);
2390 msg = true;
2391 }
2392 arg = arg->s_next;
2393 parg = parg->s_next;
2394 n++;
2395 }
2396
2397 end:
2398 if (msg)
2399 /* old style definition */
2400 print_previous_declaration(300, rdsym);
2401
2402 return msg;
2403 }
2404
2405 /*
2406 * Completes a type by copying the dimension and prototype information
2407 * from a second compatible type.
2408 *
2409 * Following lines are legal:
2410 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2411 * "typedef ft(); ft f; f(int); ft g; g(long);"
2412 * This means that, if a type is completed, the type structure must
2413 * be duplicated.
2414 */
2415 void
2416 complete_type(sym_t *dsym, sym_t *ssym)
2417 {
2418 type_t **dstp, *src;
2419 type_t *dst;
2420
2421 dstp = &dsym->s_type;
2422 src = ssym->s_type;
2423
2424 while ((dst = *dstp) != NULL) {
2425 lint_assert(src != NULL);
2426 lint_assert(dst->t_tspec == src->t_tspec);
2427 if (dst->t_tspec == ARRAY) {
2428 if (dst->t_dim == 0 && src->t_dim != 0) {
2429 *dstp = dst = block_dup_type(dst);
2430 dst->t_dim = src->t_dim;
2431 dst->t_incomplete_array = false;
2432 }
2433 } else if (dst->t_tspec == FUNC) {
2434 if (!dst->t_proto && src->t_proto) {
2435 *dstp = dst = block_dup_type(dst);
2436 dst->t_proto = true;
2437 dst->t_args = src->t_args;
2438 }
2439 }
2440 dstp = &dst->t_subt;
2441 src = src->t_subt;
2442 }
2443 }
2444
2445 /*
2446 * Completes the declaration of a single argument.
2447 */
2448 sym_t *
2449 declare_argument(sym_t *sym, bool initflg)
2450 {
2451 tspec_t t;
2452
2453 check_function_definition(sym, true);
2454
2455 check_type(sym);
2456
2457 if (dcs->d_redeclared_symbol != NULL &&
2458 dcs->d_redeclared_symbol->s_block_level == block_level) {
2459 /* redeclaration of formal parameter %s */
2460 error(237, sym->s_name);
2461 rmsym(dcs->d_redeclared_symbol);
2462 sym->s_arg = true;
2463 }
2464
2465 if (!sym->s_arg) {
2466 /* declared argument %s is missing */
2467 error(53, sym->s_name);
2468 sym->s_arg = true;
2469 }
2470
2471 if (initflg) {
2472 /* cannot initialize parameter: %s */
2473 error(52, sym->s_name);
2474 }
2475
2476 if (sym->s_type == NULL) /* for c(void()) */
2477 sym->s_type = gettyp(VOID);
2478
2479 if ((t = sym->s_type->t_tspec) == ARRAY) {
2480 sym->s_type = block_derive_type(sym->s_type->t_subt, PTR);
2481 } else if (t == FUNC) {
2482 if (!allow_c90)
2483 /* a function is declared as an argument: %s */
2484 warning(50, sym->s_name);
2485 sym->s_type = block_derive_type(sym->s_type, PTR);
2486 } else if (t == FLOAT) {
2487 if (!allow_c90)
2488 sym->s_type = gettyp(DOUBLE);
2489 }
2490
2491 if (dcs->d_inline)
2492 /* argument declared inline: %s */
2493 warning(269, sym->s_name);
2494
2495 /*
2496 * Arguments must have complete types. length_in_bits prints the
2497 * needed error messages (null dimension is impossible because arrays
2498 * are converted to pointers).
2499 */
2500 if (sym->s_type->t_tspec != VOID)
2501 (void)length_in_bits(sym->s_type, sym->s_name);
2502
2503 sym->s_used = dcs->d_used;
2504 mark_as_set(sym);
2505
2506 return sym;
2507 }
2508
2509 void
2510 check_func_lint_directives(void)
2511 {
2512 sym_t *arg;
2513 int narg, n;
2514 tspec_t t;
2515
2516 /* check for illegal combinations of lint directives */
2517 if (printflike_argnum != -1 && scanflike_argnum != -1) {
2518 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2519 warning(289);
2520 printflike_argnum = scanflike_argnum = -1;
2521 }
2522 if (nvararg != -1 &&
2523 (printflike_argnum != -1 || scanflike_argnum != -1)) {
2524 /* dubious use of ** VARARGS ** with ** %s ** */
2525 warning(288,
2526 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2527 nvararg = -1;
2528 }
2529
2530 /*
2531 * check if the argument of a lint directive is compatible with the
2532 * number of arguments.
2533 */
2534 narg = 0;
2535 for (arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
2536 narg++;
2537 if (nargusg > narg) {
2538 /* argument number mismatch with directive: ** %s ** */
2539 warning(283, "ARGSUSED");
2540 nargusg = 0;
2541 }
2542 if (nvararg > narg) {
2543 /* argument number mismatch with directive: ** %s ** */
2544 warning(283, "VARARGS");
2545 nvararg = 0;
2546 }
2547 if (printflike_argnum > narg) {
2548 /* argument number mismatch with directive: ** %s ** */
2549 warning(283, "PRINTFLIKE");
2550 printflike_argnum = -1;
2551 } else if (printflike_argnum == 0) {
2552 printflike_argnum = -1;
2553 }
2554 if (scanflike_argnum > narg) {
2555 /* argument number mismatch with directive: ** %s ** */
2556 warning(283, "SCANFLIKE");
2557 scanflike_argnum = -1;
2558 } else if (scanflike_argnum == 0) {
2559 scanflike_argnum = -1;
2560 }
2561 if (printflike_argnum != -1 || scanflike_argnum != -1) {
2562 narg = printflike_argnum != -1
2563 ? printflike_argnum : scanflike_argnum;
2564 arg = dcs->d_func_args;
2565 for (n = 1; n < narg; n++)
2566 arg = arg->s_next;
2567 if (arg->s_type->t_tspec != PTR ||
2568 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2569 t != UCHAR && t != SCHAR)) {
2570 /* argument %d must be 'char *' for PRINTFLIKE/... */
2571 warning(293, narg);
2572 printflike_argnum = scanflike_argnum = -1;
2573 }
2574 }
2575 }
2576
2577 /*
2578 * Warn about arguments in old style function definitions that default to int.
2579 * Check that an old style function definition is compatible to a previous
2580 * prototype.
2581 */
2582 void
2583 check_func_old_style_arguments(void)
2584 {
2585 sym_t *args, *arg, *pargs, *parg;
2586 int narg, nparg;
2587 bool msg;
2588
2589 args = funcsym->u.s_old_style_args;
2590 pargs = funcsym->s_type->t_args;
2591
2592 /*
2593 * print a warning for each argument of an old style function
2594 * definition which defaults to int
2595 */
2596 for (arg = args; arg != NULL; arg = arg->s_next) {
2597 if (arg->s_defarg) {
2598 /* argument type defaults to 'int': %s */
2599 warning(32, arg->s_name);
2600 arg->s_defarg = false;
2601 mark_as_set(arg);
2602 }
2603 }
2604
2605 /*
2606 * If this is an old style function definition and a prototype
2607 * exists, compare the types of arguments.
2608 */
2609 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2610 /*
2611 * If the number of arguments does not match, we need not
2612 * continue.
2613 */
2614 narg = nparg = 0;
2615 msg = false;
2616 for (parg = pargs; parg != NULL; parg = parg->s_next)
2617 nparg++;
2618 for (arg = args; arg != NULL; arg = arg->s_next)
2619 narg++;
2620 if (narg != nparg) {
2621 /* parameter mismatch: %d declared, %d defined */
2622 error(51, nparg, narg);
2623 msg = true;
2624 } else {
2625 parg = pargs;
2626 arg = args;
2627 while (narg-- > 0) {
2628 msg |= check_prototype_declaration(arg, parg);
2629 parg = parg->s_next;
2630 arg = arg->s_next;
2631 }
2632 }
2633 if (msg)
2634 /* prototype declaration */
2635 print_previous_declaration(285,
2636 dcs->d_redeclared_symbol);
2637
2638 /* from now on the prototype is valid */
2639 funcsym->s_osdef = false;
2640 funcsym->u.s_old_style_args = NULL;
2641 }
2642 }
2643
2644 /*
2645 * Checks compatibility of an old style function definition with a previous
2646 * prototype declaration.
2647 * Returns true if the position of the previous declaration should be reported.
2648 */
2649 static bool
2650 check_prototype_declaration(sym_t *arg, sym_t *parg)
2651 {
2652 type_t *tp, *ptp;
2653 bool dowarn;
2654
2655 tp = arg->s_type;
2656 ptp = parg->s_type;
2657
2658 dowarn = false;
2659
2660 if (!eqtype(tp, ptp, true, true, &dowarn)) {
2661 if (eqtype(tp, ptp, true, false, &dowarn)) {
2662 /* type does not match prototype: %s */
2663 return gnuism(58, arg->s_name);
2664 } else {
2665 /* type does not match prototype: %s */
2666 error(58, arg->s_name);
2667 return true;
2668 }
2669 } else if (dowarn) {
2670 /* TODO: Make this an error in C99 mode as well. */
2671 if (!allow_trad && !allow_c99)
2672 /* type does not match prototype: %s */
2673 error(58, arg->s_name);
2674 else
2675 /* type does not match prototype: %s */
2676 warning(58, arg->s_name);
2677 return true;
2678 }
2679
2680 return false;
2681 }
2682
2683 static void
2684 check_local_hiding(const sym_t *dsym)
2685 {
2686 switch (dsym->s_scl) {
2687 case AUTO:
2688 /* automatic hides external declaration: %s */
2689 warning(86, dsym->s_name);
2690 break;
2691 case STATIC:
2692 /* static hides external declaration: %s */
2693 warning(87, dsym->s_name);
2694 break;
2695 case TYPEDEF:
2696 /* typedef hides external declaration: %s */
2697 warning(88, dsym->s_name);
2698 break;
2699 case EXTERN:
2700 /* Already checked in declare_external_in_block. */
2701 break;
2702 default:
2703 lint_assert(/*CONSTCOND*/false);
2704 }
2705 }
2706
2707 static void
2708 check_local_redeclaration(const sym_t *dsym, sym_t *rsym)
2709 {
2710 if (rsym->s_block_level == 0) {
2711 if (hflag)
2712 check_local_hiding(dsym);
2713
2714 } else if (rsym->s_block_level == block_level) {
2715
2716 /* no hflag, because it's illegal! */
2717 if (rsym->s_arg) {
2718 /*
2719 * if allow_c90, a "redeclaration of %s" error
2720 * is produced below
2721 */
2722 if (!allow_c90) {
2723 if (hflag)
2724 /* declaration hides parameter: %s */
2725 warning(91, dsym->s_name);
2726 rmsym(rsym);
2727 }
2728 }
2729
2730 } else if (rsym->s_block_level < block_level) {
2731 if (hflag)
2732 /* declaration hides earlier one: %s */
2733 warning(95, dsym->s_name);
2734 }
2735
2736 if (rsym->s_block_level == block_level) {
2737 /* redeclaration of %s */
2738 error(27, dsym->s_name);
2739 rmsym(rsym);
2740 }
2741 }
2742
2743 /*
2744 * Completes a single local declaration/definition.
2745 */
2746 void
2747 declare_local(sym_t *dsym, bool initflg)
2748 {
2749
2750 /* Correct a mistake done in declarator_name(). */
2751 if (dsym->s_type->t_tspec == FUNC) {
2752 dsym->s_def = DECL;
2753 if (dcs->d_scl == NOSCL)
2754 dsym->s_scl = EXTERN;
2755 }
2756
2757 if (dsym->s_type->t_tspec == FUNC) {
2758 if (dsym->s_scl == STATIC) {
2759 /* dubious static function at block level: %s */
2760 warning(93, dsym->s_name);
2761 dsym->s_scl = EXTERN;
2762 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2763 /* function has illegal storage class: %s */
2764 error(94, dsym->s_name);
2765 dsym->s_scl = EXTERN;
2766 }
2767 }
2768
2769 /*
2770 * functions may be declared inline at local scope, although
2771 * this has no effect for a later definition of the same
2772 * function.
2773 * XXX it should have an effect if !allow_c90 is set. this would
2774 * also be the way gcc behaves.
2775 */
2776 if (dcs->d_inline) {
2777 if (dsym->s_type->t_tspec == FUNC) {
2778 dsym->s_inline = true;
2779 } else {
2780 /* variable declared inline: %s */
2781 warning(268, dsym->s_name);
2782 }
2783 }
2784
2785 check_function_definition(dsym, true);
2786
2787 check_type(dsym);
2788
2789 if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
2790 declare_external_in_block(dsym);
2791
2792 if (dsym->s_scl == EXTERN) {
2793 /*
2794 * XXX if the static variable at level 0 is only defined
2795 * later, checking will be possible.
2796 */
2797 if (dsym->s_ext_sym == NULL) {
2798 outsym(dsym, EXTERN, dsym->s_def);
2799 } else {
2800 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2801 }
2802 }
2803
2804 if (dcs->d_redeclared_symbol != NULL)
2805 check_local_redeclaration(dsym, dcs->d_redeclared_symbol);
2806
2807 if (initflg && !check_init(dsym)) {
2808 dsym->s_def = DEF;
2809 mark_as_set(dsym);
2810 }
2811
2812 if (dsym->s_scl == TYPEDEF) {
2813 dsym->s_type = block_dup_type(dsym->s_type);
2814 dsym->s_type->t_typedef = true;
2815 settdsym(dsym->s_type, dsym);
2816 }
2817
2818 /*
2819 * Before we can check the size we must wait for a initialization
2820 * which may follow.
2821 */
2822 }
2823
2824 /*
2825 * Processes (re)declarations of external symbols inside blocks.
2826 */
2827 static void
2828 declare_external_in_block(sym_t *dsym)
2829 {
2830 bool eqt, dowarn;
2831 sym_t *esym;
2832
2833 /* look for a symbol with the same name */
2834 esym = dcs->d_redeclared_symbol;
2835 while (esym != NULL && esym->s_block_level != 0) {
2836 while ((esym = esym->s_symtab_next) != NULL) {
2837 if (esym->s_kind != FVFT)
2838 continue;
2839 if (strcmp(dsym->s_name, esym->s_name) == 0)
2840 break;
2841 }
2842 }
2843 if (esym == NULL)
2844 return;
2845 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2846 /* gcc accepts this without a warning, pcc prints an error. */
2847 /* redeclaration of %s */
2848 warning(27, dsym->s_name);
2849 print_previous_declaration(-1, esym);
2850 return;
2851 }
2852
2853 dowarn = false;
2854 eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn);
2855
2856 if (!eqt || dowarn) {
2857 if (esym->s_scl == EXTERN) {
2858 /* inconsistent redeclaration of extern: %s */
2859 warning(90, dsym->s_name);
2860 print_previous_declaration(-1, esym);
2861 } else {
2862 /* inconsistent redeclaration of static: %s */
2863 warning(92, dsym->s_name);
2864 print_previous_declaration(-1, esym);
2865 }
2866 }
2867
2868 if (eqt) {
2869 /*
2870 * Remember the external symbol so we can update usage
2871 * information at the end of the block.
2872 */
2873 dsym->s_ext_sym = esym;
2874 }
2875 }
2876
2877 /*
2878 * Print an error or a warning if the symbol cannot be initialized due
2879 * to type/storage class. Return whether an error has been detected.
2880 */
2881 static bool
2882 check_init(sym_t *sym)
2883 {
2884 bool erred;
2885
2886 erred = false;
2887
2888 if (sym->s_type->t_tspec == FUNC) {
2889 /* cannot initialize function: %s */
2890 error(24, sym->s_name);
2891 erred = true;
2892 } else if (sym->s_scl == TYPEDEF) {
2893 /* cannot initialize typedef: %s */
2894 error(25, sym->s_name);
2895 erred = true;
2896 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2897 /* cannot initialize "extern" declaration: %s */
2898 if (dcs->d_kind == DK_EXTERN) {
2899 /* cannot initialize extern declaration: %s */
2900 warning(26, sym->s_name);
2901 } else {
2902 /* cannot initialize extern declaration: %s */
2903 error(26, sym->s_name);
2904 erred = true;
2905 }
2906 }
2907
2908 return erred;
2909 }
2910
2911 /*
2912 * Create a symbol for an abstract declaration.
2913 */
2914 sym_t *
2915 abstract_name(void)
2916 {
2917 sym_t *sym;
2918
2919 lint_assert(dcs->d_kind == DK_ABSTRACT ||
2920 dcs->d_kind == DK_PROTO_ARG);
2921
2922 sym = block_zero_alloc(sizeof(*sym));
2923
2924 sym->s_name = unnamed;
2925 sym->s_def = DEF;
2926 sym->s_scl = ABSTRACT;
2927 sym->s_block_level = -1;
2928
2929 if (dcs->d_kind == DK_PROTO_ARG)
2930 sym->s_arg = true;
2931
2932 /*
2933 * At this point, dcs->d_type contains only the basic type. That
2934 * type will be updated later, adding pointers, arrays and functions
2935 * as necessary.
2936 */
2937 /*
2938 * XXX: This is not the correct type. For example in msg_347, it is
2939 * the type of the last prototype parameter, but it should rather be
2940 * the return type of the function.
2941 */
2942 sym->s_type = dcs->d_type;
2943 dcs->d_redeclared_symbol = NULL;
2944 dcs->d_vararg = false;
2945
2946 return sym;
2947 }
2948
2949 /*
2950 * Removes anything which has nothing to do on global level.
2951 */
2952 void
2953 global_clean_up(void)
2954 {
2955
2956 while (dcs->d_enclosing != NULL)
2957 end_declaration_level();
2958
2959 clean_up_after_error();
2960 block_level = 0;
2961 mem_block_level = 0;
2962
2963 /*
2964 * remove all information about pending lint directives without
2965 * warnings.
2966 */
2967 global_clean_up_decl(true);
2968 }
2969
2970 /*
2971 * Process an abstract type declaration
2972 */
2973 sym_t *
2974 declare_1_abstract(sym_t *sym)
2975 {
2976
2977 check_function_definition(sym, true);
2978 check_type(sym);
2979 return sym;
2980 }
2981
2982 /*
2983 * Checks size after declarations of variables and their initialization.
2984 */
2985 void
2986 check_size(sym_t *dsym)
2987 {
2988
2989 if (dsym->s_def != DEF)
2990 return;
2991 if (dsym->s_scl == TYPEDEF)
2992 return;
2993 if (dsym->s_type->t_tspec == FUNC)
2994 return;
2995
2996 if (length_in_bits(dsym->s_type, dsym->s_name) == 0 &&
2997 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2998 if (!allow_c90) {
2999 /* empty array declaration: %s */
3000 warning(190, dsym->s_name);
3001 } else {
3002 /* empty array declaration: %s */
3003 error(190, dsym->s_name);
3004 }
3005 }
3006 }
3007
3008 /*
3009 * Mark an object as set if it is not already
3010 */
3011 void
3012 mark_as_set(sym_t *sym)
3013 {
3014
3015 if (!sym->s_set) {
3016 sym->s_set = true;
3017 UNIQUE_CURR_POS(sym->s_set_pos);
3018 }
3019 }
3020
3021 /*
3022 * Mark an object as used if it is not already
3023 */
3024 void
3025 mark_as_used(sym_t *sym, bool fcall, bool szof)
3026 {
3027
3028 if (!sym->s_used) {
3029 sym->s_used = true;
3030 UNIQUE_CURR_POS(sym->s_use_pos);
3031 }
3032 /*
3033 * for function calls another record is written
3034 *
3035 * XXX Should symbols used in sizeof() be treated as used or not?
3036 * Probably not, because there is no sense to declare an
3037 * external variable only to get their size.
3038 */
3039 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
3040 outusg(sym);
3041 }
3042
3043 /*
3044 * Prints warnings for a list of variables and labels (concatenated
3045 * with s_level_next) if these are not used or only set.
3046 */
3047 void
3048 check_usage(dinfo_t *di)
3049 {
3050 sym_t *sym;
3051 int saved_lwarn;
3052
3053 /* for this warning LINTED has no effect */
3054 saved_lwarn = lwarn;
3055 lwarn = LWARN_ALL;
3056
3057 debug_step("begin lwarn %d", lwarn);
3058 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_level_next)
3059 check_usage_sym(di->d_asm, sym);
3060 lwarn = saved_lwarn;
3061 debug_step("end lwarn %d", lwarn);
3062 }
3063
3064 /*
3065 * Prints a warning for a single variable or label if it is not used or
3066 * only set.
3067 */
3068 void
3069 check_usage_sym(bool novar, sym_t *sym)
3070 {
3071
3072 if (sym->s_block_level == -1)
3073 return;
3074
3075 if (sym->s_kind == FVFT && sym->s_arg)
3076 check_argument_usage(novar, sym);
3077 else if (sym->s_kind == FVFT)
3078 check_variable_usage(novar, sym);
3079 else if (sym->s_kind == FLABEL)
3080 check_label_usage(sym);
3081 else if (sym->s_kind == FTAG)
3082 check_tag_usage(sym);
3083 }
3084
3085 static void
3086 check_argument_usage(bool novar, sym_t *arg)
3087 {
3088
3089 lint_assert(arg->s_set);
3090
3091 if (novar)
3092 return;
3093
3094 if (!arg->s_used && vflag) {
3095 /* argument '%s' unused in function '%s' */
3096 warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name);
3097 }
3098 }
3099
3100 static void
3101 check_variable_usage(bool novar, sym_t *sym)
3102 {
3103 scl_t sc;
3104 sym_t *xsym;
3105
3106 lint_assert(block_level != 0);
3107
3108 /* example at file scope: int c = ({ return 3; }); */
3109 if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0]))
3110 return;
3111
3112 /* errors in expressions easily cause lots of these warnings */
3113 if (nerr != 0)
3114 return;
3115
3116 /*
3117 * XXX Only variables are checked, although types should
3118 * probably also be checked
3119 */
3120 if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
3121 sc != AUTO && sc != REG) {
3122 return;
3123 }
3124
3125 if (novar)
3126 return;
3127
3128 if (sc == EXTERN) {
3129 if (!sym->s_used && !sym->s_set) {
3130 /* '%s' unused in function '%s' */
3131 warning_at(192, &sym->s_def_pos,
3132 sym->s_name, funcsym->s_name);
3133 }
3134 } else {
3135 if (sym->s_set && !sym->s_used) {
3136 /* '%s' set but not used in function '%s' */
3137 warning_at(191, &sym->s_set_pos,
3138 sym->s_name, funcsym->s_name);
3139 } else if (!sym->s_used) {
3140 /* '%s' unused in function '%s' */
3141 warning_at(192, &sym->s_def_pos,
3142 sym->s_name, funcsym->s_name);
3143 }
3144 }
3145
3146 if (sc == EXTERN) {
3147 /*
3148 * information about usage is taken over into the symbol
3149 * table entry at level 0 if the symbol was locally declared
3150 * as an external symbol.
3151 *
3152 * XXX This is wrong for symbols declared static at level 0
3153 * if the usage information stems from sizeof(). This is
3154 * because symbols at level 0 only used in sizeof() are
3155 * considered to not be used.
3156 */
3157 if ((xsym = sym->s_ext_sym) != NULL) {
3158 if (sym->s_used && !xsym->s_used) {
3159 xsym->s_used = true;
3160 xsym->s_use_pos = sym->s_use_pos;
3161 }
3162 if (sym->s_set && !xsym->s_set) {
3163 xsym->s_set = true;
3164 xsym->s_set_pos = sym->s_set_pos;
3165 }
3166 }
3167 }
3168 }
3169
3170 static void
3171 check_label_usage(sym_t *lab)
3172 {
3173
3174 lint_assert(block_level == 1);
3175 lint_assert(lab->s_block_level == 1);
3176
3177 if (funcsym == NULL) {
3178 /* syntax error '%s' */
3179 error(249, "labels are only valid inside a function");
3180 } else if (lab->s_set && !lab->s_used) {
3181 /* label '%s' unused in function '%s' */
3182 warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name);
3183 } else if (!lab->s_set) {
3184 /* undefined label '%s' */
3185 warning_at(23, &lab->s_use_pos, lab->s_name);
3186 }
3187 }
3188
3189 static void
3190 check_tag_usage(sym_t *sym)
3191 {
3192
3193 if (!is_incomplete(sym->s_type))
3194 return;
3195
3196 /* always complain about incomplete tags declared inside blocks */
3197 if (!zflag || dcs->d_kind != DK_EXTERN)
3198 return;
3199
3200 switch (sym->s_type->t_tspec) {
3201 case STRUCT:
3202 /* struct %s never defined */
3203 warning_at(233, &sym->s_def_pos, sym->s_name);
3204 break;
3205 case UNION:
3206 /* union %s never defined */
3207 warning_at(234, &sym->s_def_pos, sym->s_name);
3208 break;
3209 case ENUM:
3210 /* enum %s never defined */
3211 warning_at(235, &sym->s_def_pos, sym->s_name);
3212 break;
3213 default:
3214 lint_assert(/*CONSTCOND*/false);
3215 }
3216 }
3217
3218 /*
3219 * Called after the entire translation unit has been parsed.
3220 * Changes tentative definitions into definitions.
3221 * Performs some tests on global symbols. Detected problems are:
3222 * - defined variables of incomplete type
3223 * - constant variables which are not initialized
3224 * - static symbols which are never used
3225 */
3226 void
3227 check_global_symbols(void)
3228 {
3229 sym_t *sym;
3230
3231 if (block_level != 0 || dcs->d_enclosing != NULL)
3232 norecover();
3233
3234 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_level_next) {
3235 if (sym->s_block_level == -1)
3236 continue;
3237 if (sym->s_kind == FVFT) {
3238 check_global_variable(sym);
3239 } else if (sym->s_kind == FTAG) {
3240 check_tag_usage(sym);
3241 } else {
3242 lint_assert(sym->s_kind == FMEMBER);
3243 }
3244 }
3245 }
3246
3247 static void
3248 check_unused_static_global_variable(const sym_t *sym)
3249 {
3250 if (sym->s_type->t_tspec == FUNC) {
3251 if (sym->s_def == DEF) {
3252 if (!sym->s_inline)
3253 /* static function %s unused */
3254 warning_at(236, &sym->s_def_pos, sym->s_name);
3255 } else {
3256 /* static function %s declared but not defined */
3257 warning_at(290, &sym->s_def_pos, sym->s_name);
3258 }
3259 } else if (!sym->s_set) {
3260 /* static variable %s unused */
3261 warning_at(226, &sym->s_def_pos, sym->s_name);
3262 } else {
3263 /* static variable %s set but not used */
3264 warning_at(307, &sym->s_def_pos, sym->s_name);
3265 }
3266 }
3267
3268 static void
3269 check_static_global_variable(const sym_t *sym)
3270 {
3271 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3272 /* static function called but not defined: %s() */
3273 error_at(225, &sym->s_use_pos, sym->s_name);
3274 }
3275
3276 if (!sym->s_used)
3277 check_unused_static_global_variable(sym);
3278
3279 if (allow_c90 && sym->s_def == TDEF && sym->s_type->t_const) {
3280 /* const object %s should have initializer */
3281 warning_at(227, &sym->s_def_pos, sym->s_name);
3282 }
3283 }
3284
3285 static void
3286 check_global_variable(const sym_t *sym)
3287 {
3288 scl_t scl = sym->s_scl;
3289
3290 if (scl == TYPEDEF || scl == BOOL_CONST || scl == ENUM_CONST)
3291 return;
3292
3293 if (scl == NOSCL)
3294 return; /* May be caused by a syntax error. */
3295
3296 lint_assert(scl == EXTERN || scl == STATIC);
3297
3298 check_global_variable_size(sym);
3299
3300 if (scl == STATIC)
3301 check_static_global_variable(sym);
3302 }
3303
3304 static void
3305 check_global_variable_size(const sym_t *sym)
3306 {
3307 pos_t cpos;
3308 int len_in_bits;
3309
3310 if (sym->s_def != TDEF)
3311 return;
3312 if (sym->s_type->t_tspec == FUNC)
3313 /*
3314 * this can happen if a syntax error occurred after a
3315 * function declaration
3316 */
3317 return;
3318 if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID)
3319 return; /* prevent internal error in length_in_bits
3320 * below */
3321
3322 cpos = curr_pos;
3323 curr_pos = sym->s_def_pos;
3324 len_in_bits = length_in_bits(sym->s_type, sym->s_name);
3325 curr_pos = cpos;
3326
3327 if (len_in_bits == 0 &&
3328 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3329 /* TODO: C99 6.7.5.2p1 defines this as an error as well. */
3330 if (!allow_c90 ||
3331 (sym->s_scl == EXTERN && (allow_trad || allow_c99))) {
3332 /* empty array declaration: %s */
3333 warning_at(190, &sym->s_def_pos, sym->s_name);
3334 } else {
3335 /* empty array declaration: %s */
3336 error_at(190, &sym->s_def_pos, sym->s_name);
3337 }
3338 }
3339 }
3340
3341 /*
3342 * Prints information about location of previous definition/declaration.
3343 */
3344 void
3345 print_previous_declaration(int msg, const sym_t *psym)
3346 {
3347
3348 if (!rflag)
3349 return;
3350
3351 if (msg != -1) {
3352 (message_at)(msg, &psym->s_def_pos);
3353 } else if (psym->s_def == DEF || psym->s_def == TDEF) {
3354 /* previous definition of %s */
3355 message_at(261, &psym->s_def_pos, psym->s_name);
3356 } else {
3357 /* previous declaration of %s */
3358 message_at(260, &psym->s_def_pos, psym->s_name);
3359 }
3360 }
3361
3362 /*
3363 * Gets a node for a constant and returns the value of this constant
3364 * as integer.
3365 *
3366 * If the node is not constant or too large for int or of type float,
3367 * a warning will be printed.
3368 *
3369 * to_int_constant() should be used only inside declarations. If it is used in
3370 * expressions, it frees the memory used for the expression.
3371 */
3372 int
3373 to_int_constant(tnode_t *tn, bool required)
3374 {
3375 int i;
3376 tspec_t t;
3377 val_t *v;
3378
3379 v = constant(tn, required);
3380
3381 if (tn == NULL) {
3382 i = 1;
3383 goto done;
3384 }
3385
3386 /*
3387 * Abstract declarations are used inside expression. To free
3388 * the memory would be a fatal error.
3389 * We don't free blocks that are inside casts because these
3390 * will be used later to match types.
3391 */
3392 if (tn->tn_op != CON && dcs->d_kind != DK_ABSTRACT)
3393 expr_free_all();
3394
3395 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
3396 i = (int)v->v_ldbl;
3397 /* integral constant expression expected */
3398 error(55);
3399 } else {
3400 i = (int)v->v_quad;
3401 if (is_uinteger(t)) {
3402 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
3403 /* integral constant too large */
3404 warning(56);
3405 }
3406 } else {
3407 if (v->v_quad > (int64_t)TARG_INT_MAX ||
3408 v->v_quad < (int64_t)TARG_INT_MIN) {
3409 /* integral constant too large */
3410 warning(56);
3411 }
3412 }
3413 }
3414
3415 done:
3416 free(v);
3417 return i;
3418 }
3419