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