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