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