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