decl.c revision 1.376 1 /* $NetBSD: decl.c,v 1.376 2023/08/02 21:26:12 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.376 2023/08/02 21:26:12 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 type is 'void' or an incomplete array, struct, union
165 * or enum.
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_PARAMS:
538 /*
539 * All symbols in dcs->d_first_dlsym are introduced in
540 * old-style parameter declarations (it's not clean, but
541 * possible). They are appended to the list of symbols declared
542 * in an old-style parameter 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 parameters 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_params = 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_PARAMS ||
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 lint_assert(*tpp != NULL);
1207 while (*tpp != dcs->d_type) {
1208 tpp = &(*tpp)->t_subt;
1209 lint_assert(*tpp != NULL);
1210 }
1211
1212 while (p != NULL) {
1213 *tpp = block_derive_pointer(dcs->d_type,
1214 p->qualifiers.tq_const, p->qualifiers.tq_volatile);
1215
1216 tpp = &(*tpp)->t_subt;
1217
1218 qual_ptr *next = p->p_next;
1219 free(p);
1220 p = next;
1221 }
1222 debug_step("add_pointer: '%s'", type_name(decl->s_type));
1223 return decl;
1224 }
1225
1226 static type_t *
1227 block_derive_array(type_t *stp, bool dim, int len)
1228 {
1229
1230 type_t *tp = block_derive_type(stp, ARRAY);
1231 tp->t_dim = len;
1232
1233 #if 0
1234 /*
1235 * As of 2022-04-03, the implementation of the type parser (see
1236 * add_function, add_array, add_pointer) is strange. When it sees
1237 * the type 'void *b[4]', it first creates 'void b[4]' and only later
1238 * inserts the '*' in the middle of the type. Late modifications like
1239 * these should not be done at all, instead the parser should be fixed
1240 * to process the type names in the proper syntactical order.
1241 *
1242 * Since the intermediate type would be an array of void, but the
1243 * final type is valid, this check cannot be enabled yet.
1244 */
1245 if (stp->t_tspec == VOID) {
1246 /* array of incomplete type */
1247 error(301);
1248 tp->t_subt = gettyp(CHAR);
1249 }
1250 #endif
1251 if (len < 0) {
1252 /* negative array dimension (%d) */
1253 error(20, len);
1254 } else if (len == 0 && dim) {
1255 /* zero sized array is a C99 extension */
1256 c99ism(322);
1257 } else if (len == 0 && !dim)
1258 tp->t_incomplete_array = true;
1259
1260 debug_step("%s: '%s'", __func__, type_name(tp));
1261 return tp;
1262 }
1263
1264 /*
1265 * If a dimension was specified, dim is true, otherwise false
1266 * n is the specified dimension
1267 */
1268 sym_t *
1269 add_array(sym_t *decl, bool dim, int n)
1270 {
1271
1272 debug_dcs();
1273
1274 type_t **tpp = &decl->s_type;
1275 lint_assert(*tpp != NULL);
1276 while (*tpp != dcs->d_type) {
1277 tpp = &(*tpp)->t_subt;
1278 lint_assert(*tpp != NULL);
1279 }
1280
1281 *tpp = block_derive_array(dcs->d_type, dim, n);
1282
1283 debug_step("%s: '%s'", __func__, type_name(decl->s_type));
1284 return decl;
1285 }
1286
1287 static type_t *
1288 block_derive_function(type_t *ret, bool proto, sym_t *params, bool vararg)
1289 {
1290
1291 type_t *tp = block_derive_type(ret, FUNC);
1292 tp->t_proto = proto;
1293 if (proto)
1294 tp->t_params = params;
1295 tp->t_vararg = vararg;
1296 debug_step("%s: '%s'", __func__, type_name(tp));
1297 return tp;
1298 }
1299
1300 static void
1301 check_prototype_parameters(sym_t *args)
1302 {
1303
1304 for (sym_t *sym = dcs->d_first_dlsym;
1305 sym != NULL; sym = sym->s_level_next) {
1306 scl_t sc = sym->s_scl;
1307 if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) {
1308 /* dubious tag declaration '%s %s' */
1309 warning(85, storage_class_name(sc), sym->s_name);
1310 }
1311 }
1312
1313 for (sym_t *arg = args; arg != NULL; arg = arg->s_next) {
1314 if (arg->s_type->t_tspec == VOID &&
1315 !(arg == args && arg->s_next == NULL)) {
1316 /* void must be sole parameter */
1317 error(60);
1318 arg->s_type = gettyp(INT);
1319 }
1320 }
1321 }
1322
1323 static void
1324 old_style_function(sym_t *decl, sym_t *params)
1325 {
1326
1327 /*
1328 * Remember the list of parameters only if this really seems to be a
1329 * function definition.
1330 */
1331 if (dcs->d_enclosing->d_kind == DLK_EXTERN &&
1332 decl->s_type == dcs->d_enclosing->d_type) {
1333 /*
1334 * Assume that this becomes a function definition. If not, it
1335 * will be corrected in check_function_definition.
1336 */
1337 if (params != NULL) {
1338 decl->s_osdef = true;
1339 decl->u.s_old_style_params = params;
1340 }
1341 } else {
1342 if (params != NULL)
1343 /* function prototype parameters must have types */
1344 warning(62);
1345 }
1346 }
1347
1348 sym_t *
1349 add_function(sym_t *decl, struct parameter_list params)
1350 {
1351
1352 debug_enter();
1353 debug_dcs_all();
1354 debug_sym("decl: ", decl, "\n");
1355 #ifdef DEBUG
1356 for (const sym_t *arg = params.first; arg != NULL; arg = arg->s_next)
1357 debug_sym("arg: ", arg, "\n");
1358 #endif
1359
1360 if (params.prototype) {
1361 if (!allow_c90)
1362 /* function prototypes are illegal in traditional C */
1363 warning(270);
1364 check_prototype_parameters(params.first);
1365 if (params.first != NULL
1366 && params.first->s_type->t_tspec == VOID)
1367 params.first = NULL;
1368 } else
1369 old_style_function(decl, params.first);
1370
1371 /*
1372 * The symbols are removed from the symbol table by
1373 * end_declaration_level after add_function. To be able to restore
1374 * them if this is a function definition, a pointer to the list of
1375 * all symbols is stored in dcs->d_enclosing->d_func_proto_syms. Also,
1376 * a list of the parameters (concatenated by s_next) is stored in
1377 * dcs->d_enclosing->d_func_params. (dcs->d_enclosing must be used
1378 * because *dcs is the declaration stack element created for the list
1379 * of params and is removed after add_function.)
1380 */
1381 if (dcs->d_enclosing->d_kind == DLK_EXTERN &&
1382 decl->s_type == dcs->d_enclosing->d_type) {
1383 dcs->d_enclosing->d_func_proto_syms = dcs->d_first_dlsym;
1384 dcs->d_enclosing->d_func_params = params.first;
1385 debug_dcs_all();
1386 }
1387
1388 type_t **tpp = &decl->s_type;
1389 lint_assert(*tpp != NULL);
1390 while (*tpp != dcs->d_enclosing->d_type) {
1391 tpp = &(*tpp)->t_subt;
1392 lint_assert(*tpp != NULL);
1393 }
1394
1395 *tpp = block_derive_function(dcs->d_enclosing->d_type,
1396 params.prototype, params.first, params.vararg);
1397
1398 debug_step("add_function: '%s'", type_name(decl->s_type));
1399 debug_dcs_all();
1400 debug_leave();
1401 return decl;
1402 }
1403
1404 /*
1405 * In a function declaration, a list of identifiers (as opposed to a list of
1406 * types) is allowed only if it's also a function definition.
1407 */
1408 void
1409 check_function_definition(sym_t *sym, bool msg)
1410 {
1411
1412 if (sym->s_osdef) {
1413 if (msg) {
1414 /* incomplete or misplaced function definition */
1415 error(22);
1416 }
1417 sym->s_osdef = false;
1418 sym->u.s_old_style_params = NULL;
1419 }
1420 }
1421
1422 /* The symbol gets a storage class and a definedness. */
1423 sym_t *
1424 declarator_name(sym_t *sym)
1425 {
1426 scl_t sc = NOSCL;
1427
1428 if (sym->s_scl == NOSCL)
1429 dcs->d_redeclared_symbol = NULL;
1430 else if (sym->s_defparam) {
1431 sym->s_defparam = false;
1432 dcs->d_redeclared_symbol = NULL;
1433 } else {
1434 dcs->d_redeclared_symbol = sym;
1435 sym = pushdown(sym);
1436 }
1437
1438 switch (dcs->d_kind) {
1439 case DLK_STRUCT:
1440 case DLK_UNION:
1441 sym->u.s_member.sm_containing_type = dcs->d_tag_type->t_sou;
1442 sym->s_def = DEF;
1443 sc = dcs->d_kind == DLK_STRUCT ? STRUCT_MEMBER : UNION_MEMBER;
1444 break;
1445 case DLK_EXTERN:
1446 /*
1447 * Symbols that are 'static' or without any storage class are
1448 * tentatively defined. Symbols that are tentatively defined or
1449 * declared may later become defined if an initializer is seen
1450 * or this is a function definition.
1451 */
1452 sc = dcs->d_scl;
1453 if (sc == NOSCL || sc == THREAD_LOCAL) {
1454 sc = EXTERN;
1455 sym->s_def = TDEF;
1456 } else if (sc == STATIC)
1457 sym->s_def = TDEF;
1458 else if (sc == TYPEDEF)
1459 sym->s_def = DEF;
1460 else {
1461 lint_assert(sc == EXTERN);
1462 sym->s_def = DECL;
1463 }
1464 break;
1465 case DLK_PROTO_PARAMS:
1466 sym->s_param = true;
1467 /* FALLTHROUGH */
1468 case DLK_OLD_STYLE_PARAMS:
1469 lint_assert(dcs->d_scl == NOSCL || dcs->d_scl == REG);
1470 sym->s_register = dcs->d_scl == REG;
1471 sc = AUTO;
1472 sym->s_def = DEF;
1473 break;
1474 case DLK_AUTO:
1475 if ((sc = dcs->d_scl) == NOSCL) {
1476 /*
1477 * XXX somewhat ugly because we don't know whether this
1478 * is AUTO or EXTERN (functions). If we are wrong, it
1479 * must be corrected in declare_local, when the
1480 * necessary type information is available.
1481 */
1482 sc = AUTO;
1483 sym->s_def = DEF;
1484 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF
1485 || sc == THREAD_LOCAL)
1486 sym->s_def = DEF;
1487 else if (sc == REG) {
1488 sym->s_register = true;
1489 sc = AUTO;
1490 sym->s_def = DEF;
1491 } else {
1492 lint_assert(sc == EXTERN);
1493 sym->s_def = DECL;
1494 }
1495 break;
1496 default:
1497 lint_assert(dcs->d_kind == DLK_ABSTRACT);
1498 /* try to continue after syntax errors */
1499 sc = NOSCL;
1500 }
1501 sym->s_scl = sc;
1502
1503 sym->s_type = dcs->d_type;
1504
1505 dcs->d_func_proto_syms = NULL;
1506
1507 debug_sym("declarator_name: ", sym, "\n");
1508 return sym;
1509 }
1510
1511 sym_t *
1512 old_style_function_parameter_name(sym_t *sym)
1513 {
1514
1515 if (sym->s_scl != NOSCL) {
1516 if (block_level == sym->s_block_level) {
1517 /* redeclaration of formal parameter '%s' */
1518 error(21, sym->s_name);
1519 lint_assert(sym->s_defparam);
1520 }
1521 sym = pushdown(sym);
1522 }
1523 sym->s_type = gettyp(INT);
1524 sym->s_scl = AUTO;
1525 sym->s_def = DEF;
1526 sym->s_defparam = true;
1527 sym->s_param = true;
1528 debug_sym("old_style_function_parameter_name: ", sym, "\n");
1529 return sym;
1530 }
1531
1532 /*-
1533 * Checks all possible cases of tag redeclarations.
1534 *
1535 * decl whether T_LBRACE follows
1536 * semi whether T_SEMI follows
1537 */
1538 static sym_t *
1539 new_tag(sym_t *tag, scl_t scl, bool decl, bool semi)
1540 {
1541
1542 if (tag->s_block_level < block_level) {
1543 if (semi) {
1544 /* "struct a;" */
1545 if (allow_c90) {
1546 /* XXX: Why is this warning suppressed in C90 mode? */
1547 if (allow_trad || allow_c99)
1548 /* declaration of '%s %s' intro... */
1549 warning(44, storage_class_name(scl),
1550 tag->s_name);
1551 tag = pushdown(tag);
1552 } else if (tag->s_scl != scl) {
1553 /* base type is really '%s %s' */
1554 warning(45, storage_class_name(tag->s_scl),
1555 tag->s_name);
1556 }
1557 dcs->d_enclosing->d_nonempty_decl = true;
1558 } else if (decl) {
1559 /* "struct a { ... } " */
1560 if (hflag)
1561 /* redefinition of '%s' hides earlier one */
1562 warning(43, tag->s_name);
1563 tag = pushdown(tag);
1564 dcs->d_enclosing->d_nonempty_decl = true;
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 /* XXX: Why is this warning suppressed in C90 mode? */
1570 if (allow_trad || allow_c99) {
1571 /* declaration of '%s %s' introduces ... */
1572 warning(44, storage_class_name(scl),
1573 tag->s_name);
1574 }
1575 tag = pushdown(tag);
1576 dcs->d_enclosing->d_nonempty_decl = true;
1577 }
1578 } else {
1579 if (tag->s_scl != scl ||
1580 (decl && !is_incomplete(tag->s_type))) {
1581 /* %s tag '%s' redeclared as %s */
1582 error(46, storage_class_name(tag->s_scl),
1583 tag->s_name, storage_class_name(scl));
1584 print_previous_declaration(tag);
1585 tag = pushdown(tag);
1586 dcs->d_enclosing->d_nonempty_decl = true;
1587 } else if (semi || decl)
1588 dcs->d_enclosing->d_nonempty_decl = true;
1589 }
1590 debug_sym("new_tag: ", tag, "\n");
1591 return tag;
1592 }
1593
1594 /*-
1595 * tag the symbol table entry of the tag
1596 * kind the kind of the tag (STRUCT/UNION/ENUM)
1597 * decl whether the tag type will be completed in this declaration
1598 * (when the following token is T_LBRACE)
1599 * semi whether the following token is T_SEMI
1600 */
1601 type_t *
1602 make_tag_type(sym_t *tag, tspec_t kind, bool decl, bool semi)
1603 {
1604 scl_t scl;
1605 type_t *tp;
1606
1607 if (kind == STRUCT)
1608 scl = STRUCT_TAG;
1609 else if (kind == UNION)
1610 scl = UNION_TAG;
1611 else {
1612 lint_assert(kind == ENUM);
1613 scl = ENUM_TAG;
1614 }
1615
1616 if (tag != NULL) {
1617 if (tag->s_scl != NOSCL)
1618 tag = new_tag(tag, scl, decl, semi);
1619 else {
1620 /* a new tag, no empty declaration */
1621 dcs->d_enclosing->d_nonempty_decl = true;
1622 if (scl == ENUM_TAG && !decl) {
1623 /* TODO: Make this an error in C99 mode as well. */
1624 if (allow_c90 &&
1625 ((!allow_trad && !allow_c99) || pflag))
1626 /* forward reference to enum type */
1627 warning(42);
1628 }
1629 }
1630 if (tag->s_scl == NOSCL) {
1631 tag->s_scl = scl;
1632 tag->s_type = tp =
1633 block_zero_alloc(sizeof(*tp), "type");
1634 tp->t_packed = dcs->d_packed;
1635 } else
1636 tp = tag->s_type;
1637
1638 } else {
1639 tag = block_zero_alloc(sizeof(*tag), "sym");
1640 tag->s_name = unnamed;
1641 tag->s_def_pos = unique_curr_pos();
1642 tag->s_kind = FTAG;
1643 tag->s_scl = scl;
1644 tag->s_block_level = -1;
1645 tag->s_type = tp = block_zero_alloc(sizeof(*tp), "type");
1646 tp->t_packed = dcs->d_packed;
1647 dcs->d_enclosing->d_nonempty_decl = true;
1648 }
1649
1650 if (tp->t_tspec == NO_TSPEC) {
1651 tp->t_tspec = kind;
1652 if (kind != ENUM) {
1653 tp->t_sou = block_zero_alloc(sizeof(*tp->t_sou),
1654 "struct_or_union");
1655 tp->t_sou->sou_align_in_bits = CHAR_SIZE;
1656 tp->t_sou->sou_tag = tag;
1657 tp->t_sou->sou_incomplete = true;
1658 } else {
1659 tp->t_is_enum = true;
1660 tp->t_enum = block_zero_alloc(sizeof(*tp->t_enum),
1661 "enumeration");
1662 tp->t_enum->en_tag = tag;
1663 tp->t_enum->en_incomplete = true;
1664 }
1665 }
1666 debug_printf("%s: '%s'", __func__, type_name(tp));
1667 debug_sym(" ", tag, "\n");
1668 return tp;
1669 }
1670
1671 const char *
1672 storage_class_name(scl_t sc)
1673 {
1674 switch (sc) {
1675 case EXTERN: return "extern";
1676 case STATIC: return "static";
1677 case AUTO: return "auto";
1678 case REG: return "register";
1679 case TYPEDEF: return "typedef";
1680 case STRUCT_TAG:return "struct";
1681 case UNION_TAG: return "union";
1682 case ENUM_TAG: return "enum";
1683 default: lint_assert(/*CONSTCOND*/false);
1684 }
1685 /* NOTREACHED */
1686 }
1687
1688 static bool
1689 has_named_member(const type_t *tp)
1690 {
1691 for (const sym_t *mem = tp->t_sou->sou_first_member;
1692 mem != NULL; mem = mem->s_next) {
1693 if (mem->s_name != unnamed)
1694 return true;
1695 if (is_struct_or_union(mem->s_type->t_tspec)
1696 && has_named_member(mem->s_type))
1697 return true;
1698 }
1699 return false;
1700 }
1701
1702 type_t *
1703 complete_struct_or_union(sym_t *first_member)
1704 {
1705
1706 type_t *tp = dcs->d_tag_type;
1707 if (tp == NULL) /* in case of syntax errors */
1708 return gettyp(INT);
1709
1710 dcs_align(dcs->d_sou_align_in_bits, 0);
1711
1712 struct_or_union *sou = tp->t_sou;
1713 sou->sou_align_in_bits = dcs->d_sou_align_in_bits;
1714 sou->sou_incomplete = false;
1715 sou->sou_first_member = first_member;
1716 if (tp->t_packed)
1717 pack_struct_or_union(tp);
1718 else
1719 sou->sou_size_in_bits = dcs->d_sou_size_in_bits;
1720
1721 if (sou->sou_size_in_bits == 0) {
1722 /* zero sized %s is a C99 feature */
1723 c99ism(47, tspec_name(tp->t_tspec));
1724 } else if (!has_named_member(tp)) {
1725 /* '%s' has no named members */
1726 warning(65, type_name(tp));
1727 }
1728 debug_step("%s: '%s'", __func__, type_name(tp));
1729 return tp;
1730 }
1731
1732 type_t *
1733 complete_enum(sym_t *first_enumerator)
1734 {
1735
1736 type_t *tp = dcs->d_tag_type;
1737 tp->t_enum->en_incomplete = false;
1738 tp->t_enum->en_first_enumerator = first_enumerator;
1739 debug_step("%s: '%s'", __func__, type_name(tp));
1740 return tp;
1741 }
1742
1743 /*
1744 * Processes the name of an enumerator in an enum declaration.
1745 *
1746 * sym points to the enumerator
1747 * val is the value of the enumerator
1748 * impl is true if the value of the enumerator was not explicitly specified.
1749 */
1750 sym_t *
1751 enumeration_constant(sym_t *sym, int val, bool impl)
1752 {
1753
1754 if (sym->s_scl != NOSCL) {
1755 if (sym->s_block_level == block_level) {
1756 /* no hflag, because this is illegal */
1757 if (sym->s_param) {
1758 /* enumeration constant '%s' hides parameter */
1759 warning(57, sym->s_name);
1760 } else {
1761 /* redeclaration of '%s' */
1762 error(27, sym->s_name);
1763 /*
1764 * Inside blocks, it should not be too
1765 * complicated to find the position of the
1766 * previous declaration
1767 */
1768 if (block_level == 0)
1769 print_previous_declaration(sym);
1770 }
1771 } else {
1772 if (hflag)
1773 /* redefinition of '%s' hides earlier one */
1774 warning(43, sym->s_name);
1775 }
1776 sym = pushdown(sym);
1777 }
1778
1779 sym->s_scl = ENUM_CONST;
1780 sym->s_type = dcs->d_tag_type;
1781 sym->u.s_enum_constant = val;
1782
1783 if (impl && val == TARG_INT_MIN) {
1784 /* enumeration value '%s' overflows */
1785 warning(48, sym->s_name);
1786 }
1787
1788 enumval = val == TARG_INT_MAX ? TARG_INT_MIN : val + 1;
1789 debug_sym("enumeration_constant: ", sym, "\n");
1790 return sym;
1791 }
1792
1793 static bool
1794 ends_with(const char *s, const char *suffix)
1795 {
1796 size_t s_len = strlen(s);
1797 size_t suffix_len = strlen(suffix);
1798 return s_len >= suffix_len &&
1799 memcmp(s + s_len - suffix_len, suffix, suffix_len) == 0;
1800 }
1801
1802 void
1803 check_extern_declaration(const sym_t *sym)
1804 {
1805
1806 if (sym->s_scl == EXTERN &&
1807 dcs->d_redeclared_symbol == NULL &&
1808 ends_with(curr_pos.p_file, ".c") &&
1809 allow_c90 &&
1810 !ch_isdigit(sym->s_name[0]) && /* see mktempsym */
1811 strcmp(sym->s_name, "main") != 0) {
1812 /* missing%s header declaration for '%s' */
1813 warning(351, sym->s_type->t_tspec == FUNC ? "" : " 'extern'",
1814 sym->s_name);
1815 }
1816 if (any_query_enabled &&
1817 sym->s_type->t_tspec == FUNC &&
1818 sym->s_scl == EXTERN &&
1819 sym->s_def == DECL &&
1820 !in_system_header) {
1821 /* redundant 'extern' in function declaration of '%s' */
1822 query_message(13, sym->s_name);
1823 }
1824 }
1825
1826 /*
1827 * Check whether the symbol cannot be initialized due to type/storage class.
1828 * Return whether an error has been detected.
1829 */
1830 static bool
1831 check_init(sym_t *sym)
1832 {
1833
1834 if (sym->s_type->t_tspec == FUNC) {
1835 /* cannot initialize function '%s' */
1836 error(24, sym->s_name);
1837 return true;
1838 }
1839 if (sym->s_scl == TYPEDEF) {
1840 /* cannot initialize typedef '%s' */
1841 error(25, sym->s_name);
1842 return true;
1843 }
1844 if (sym->s_scl == EXTERN && sym->s_def == DECL) {
1845 if (dcs->d_kind == DLK_EXTERN) {
1846 /* cannot initialize extern declaration '%s' */
1847 warning(26, sym->s_name);
1848 } else {
1849 /* cannot initialize extern declaration '%s' */
1850 error(26, sym->s_name);
1851 return true;
1852 }
1853 }
1854
1855 return false;
1856 }
1857
1858 /*
1859 * Compares a prototype declaration with the remembered parameters of a
1860 * previous old-style function definition.
1861 */
1862 static bool
1863 check_old_style_definition(const sym_t *rdsym, const sym_t *dsym)
1864 {
1865
1866 const sym_t *old_params = rdsym->u.s_old_style_params;
1867 const sym_t *proto_params = dsym->s_type->t_params;
1868
1869 bool msg = false;
1870
1871 int old_n = 0;
1872 for (const sym_t *p = old_params; p != NULL; p = p->s_next)
1873 old_n++;
1874 int proto_n = 0;
1875 for (const sym_t *p = proto_params; p != NULL; p = p->s_next)
1876 proto_n++;
1877 if (old_n != proto_n) {
1878 /* prototype does not match old-style definition */
1879 error(63);
1880 msg = true;
1881 goto end;
1882 }
1883
1884 const sym_t *arg = old_params;
1885 const sym_t *parg = proto_params;
1886 int n = 1;
1887 while (old_n-- > 0) {
1888 bool dowarn = false;
1889 if (!types_compatible(arg->s_type, parg->s_type,
1890 true, true, &dowarn) ||
1891 dowarn) {
1892 /* prototype does not match old-style ... */
1893 error(299, n);
1894 msg = true;
1895 }
1896 arg = arg->s_next;
1897 parg = parg->s_next;
1898 n++;
1899 }
1900
1901 end:
1902 if (msg && rflag) {
1903 /* old-style definition */
1904 message_at(300, &rdsym->s_def_pos);
1905 }
1906
1907 return msg;
1908 }
1909
1910 /* Process a single external or 'static' declarator. */
1911 static void
1912 declare_extern(sym_t *dsym, bool has_initializer, sbuf_t *renaming)
1913 {
1914
1915 if (renaming != NULL) {
1916 lint_assert(dsym->s_rename == NULL);
1917
1918 char *s = level_zero_alloc(1, renaming->sb_len + 1, "string");
1919 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1920 dsym->s_rename = s;
1921 }
1922
1923 check_extern_declaration(dsym);
1924
1925 check_function_definition(dsym, true);
1926
1927 check_type(dsym);
1928
1929 if (has_initializer && !check_init(dsym))
1930 dsym->s_def = DEF;
1931
1932 /*
1933 * Declarations of functions are marked as "tentative" in
1934 * declarator_name(). This is wrong because there are no
1935 * tentative function definitions.
1936 */
1937 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1938 dsym->s_def = DECL;
1939
1940 if (dcs->d_inline) {
1941 if (dsym->s_type->t_tspec == FUNC) {
1942 dsym->s_inline = true;
1943 } else {
1944 /* variable '%s' declared inline */
1945 warning(268, dsym->s_name);
1946 }
1947 }
1948
1949 /* Write the declaration into the output file */
1950 if (plibflg && llibflg &&
1951 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1952 /*
1953 * With both LINTLIBRARY and PROTOLIB the prototype is
1954 * written as a function definition to the output file.
1955 */
1956 bool rval = dsym->s_type->t_subt->t_tspec != VOID;
1957 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1958 } else if (!is_compiler_builtin(dsym->s_name)
1959 && !(has_initializer && dsym->s_type->t_incomplete_array)) {
1960 outsym(dsym, dsym->s_scl, dsym->s_def);
1961 }
1962
1963 sym_t *rdsym = dcs->d_redeclared_symbol;
1964 if (rdsym != NULL) {
1965
1966 /*
1967 * If the old symbol stems from an old-style function
1968 * definition, we have remembered the params in
1969 * rdsym->s_old_style_params and compare them with the params
1970 * of the prototype.
1971 */
1972 bool redec = rdsym->s_osdef && dsym->s_type->t_proto &&
1973 check_old_style_definition(rdsym, dsym);
1974
1975 bool dowarn = false;
1976 if (!redec && !check_redeclaration(dsym, &dowarn)) {
1977 if (dowarn) {
1978 /* TODO: Make this an error in C99 mode as well. */
1979 if (!allow_trad && !allow_c99)
1980 /* redeclaration of '%s' */
1981 error(27, dsym->s_name);
1982 else
1983 /* redeclaration of '%s' */
1984 warning(27, dsym->s_name);
1985 print_previous_declaration(rdsym);
1986 }
1987
1988 /*
1989 * Take over the remembered params if the new symbol
1990 * is not a prototype.
1991 */
1992 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1993 dsym->s_osdef = rdsym->s_osdef;
1994 dsym->u.s_old_style_params =
1995 rdsym->u.s_old_style_params;
1996 dsym->s_def_pos = rdsym->s_def_pos;
1997 }
1998
1999 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto)
2000 dsym->s_def_pos = rdsym->s_def_pos;
2001 else if (rdsym->s_def == DEF && dsym->s_def != DEF)
2002 dsym->s_def_pos = rdsym->s_def_pos;
2003
2004 copy_usage_info(dsym, rdsym);
2005
2006 /* Once a name is defined, it remains defined. */
2007 if (rdsym->s_def == DEF)
2008 dsym->s_def = DEF;
2009
2010 /* once a function is inline, it remains inline */
2011 if (rdsym->s_inline)
2012 dsym->s_inline = true;
2013
2014 complete_type(dsym, rdsym);
2015 }
2016
2017 rmsym(rdsym);
2018 }
2019
2020 if (dsym->s_scl == TYPEDEF) {
2021 dsym->s_type = block_dup_type(dsym->s_type);
2022 dsym->s_type->t_typedef = true;
2023 set_first_typedef(dsym->s_type, dsym);
2024 }
2025 debug_printf("%s: ", __func__);
2026 debug_sym("", dsym, "\n");
2027 }
2028
2029 void
2030 declare(sym_t *decl, bool has_initializer, sbuf_t *renaming)
2031 {
2032
2033 if (dcs->d_kind == DLK_EXTERN)
2034 declare_extern(decl, has_initializer, renaming);
2035 else if (dcs->d_kind == DLK_OLD_STYLE_PARAMS ||
2036 dcs->d_kind == DLK_PROTO_PARAMS) {
2037 if (renaming != NULL) {
2038 /* symbol renaming can't be used on function ... */
2039 error(310);
2040 } else
2041 (void)declare_parameter(decl, has_initializer);
2042 } else {
2043 lint_assert(dcs->d_kind == DLK_AUTO);
2044 if (renaming != NULL) {
2045 /* symbol renaming can't be used on automatic ... */
2046 error(311);
2047 } else
2048 declare_local(decl, has_initializer);
2049 }
2050 debug_printf("%s: ", __func__);
2051 debug_sym("", decl, "\n");
2052 }
2053
2054 /*
2055 * Copies information about usage into a new symbol table entry of
2056 * the same symbol.
2057 */
2058 void
2059 copy_usage_info(sym_t *sym, sym_t *rdsym)
2060 {
2061
2062 sym->s_set_pos = rdsym->s_set_pos;
2063 sym->s_use_pos = rdsym->s_use_pos;
2064 sym->s_set = rdsym->s_set;
2065 sym->s_used = rdsym->s_used;
2066 }
2067
2068 /*
2069 * Prints an error and returns true if a symbol is redeclared/redefined.
2070 * Otherwise, returns false and, in some cases of minor problems, prints
2071 * a warning.
2072 */
2073 bool
2074 check_redeclaration(sym_t *dsym, bool *dowarn)
2075 {
2076
2077 sym_t *rdsym = dcs->d_redeclared_symbol;
2078 if (rdsym->s_scl == ENUM_CONST) {
2079 /* redeclaration of '%s' */
2080 error(27, dsym->s_name);
2081 print_previous_declaration(rdsym);
2082 return true;
2083 }
2084 if (rdsym->s_scl == TYPEDEF) {
2085 /* typedef '%s' redeclared */
2086 error(89, dsym->s_name);
2087 print_previous_declaration(rdsym);
2088 return true;
2089 }
2090 if (dsym->s_scl == TYPEDEF) {
2091 /* redeclaration of '%s' */
2092 error(27, dsym->s_name);
2093 print_previous_declaration(rdsym);
2094 return true;
2095 }
2096 if (rdsym->s_def == DEF && dsym->s_def == DEF) {
2097 /* redefinition of '%s' */
2098 error(28, dsym->s_name);
2099 print_previous_declaration(rdsym);
2100 return true;
2101 }
2102 if (!types_compatible(rdsym->s_type, dsym->s_type,
2103 false, false, dowarn)) {
2104 /* redeclaration of '%s' with type '%s', expected '%s' */
2105 error(347, dsym->s_name,
2106 type_name(dsym->s_type), type_name(rdsym->s_type));
2107 print_previous_declaration(rdsym);
2108 return true;
2109 }
2110 if (rdsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2111 return false;
2112 if (rdsym->s_scl == STATIC && dsym->s_scl == STATIC)
2113 return false;
2114 if (rdsym->s_scl == STATIC && dsym->s_def == DECL)
2115 return false;
2116 if (rdsym->s_scl == EXTERN && rdsym->s_def == DEF) {
2117 /*
2118 * All cases except "int a = 1; static int a;" are caught
2119 * above with or without a warning
2120 */
2121 /* redeclaration of '%s' */
2122 error(27, dsym->s_name);
2123 print_previous_declaration(rdsym);
2124 return true;
2125 }
2126 if (rdsym->s_scl == EXTERN) {
2127 /* '%s' was previously declared extern, becomes static */
2128 warning(29, dsym->s_name);
2129 print_previous_declaration(rdsym);
2130 return false;
2131 }
2132 /*
2133 * Now it's one of:
2134 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2135 */
2136 /* TODO: Make this an error in C99 mode as well. */
2137 if (!allow_trad && !allow_c99) {
2138 /* redeclaration of '%s'; ANSI C requires static */
2139 warning(30, dsym->s_name);
2140 print_previous_declaration(rdsym);
2141 }
2142 dsym->s_scl = STATIC;
2143 return false;
2144 }
2145
2146 static bool
2147 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
2148 {
2149
2150 if (tp1->t_const != tp2->t_const && !ignqual && allow_c90)
2151 return false;
2152 if (tp1->t_volatile != tp2->t_volatile && !ignqual && allow_c90)
2153 return false;
2154 return true;
2155 }
2156
2157 bool
2158 pointer_types_are_compatible(const type_t *tp1, const type_t *tp2, bool ignqual)
2159 {
2160
2161 return tp1->t_tspec == VOID || tp2->t_tspec == VOID ||
2162 qualifiers_correspond(tp1, tp2, ignqual);
2163 }
2164
2165 static bool
2166 prototypes_compatible(const type_t *tp1, const type_t *tp2, bool *dowarn)
2167 {
2168
2169 if (tp1->t_vararg != tp2->t_vararg)
2170 return false;
2171
2172 const sym_t *p1 = tp1->t_params;
2173 const sym_t *p2 = tp2->t_params;
2174
2175 for (; p1 != NULL && p2 != NULL; p1 = p1->s_next, p2 = p2->s_next) {
2176 if (!types_compatible(p1->s_type, p2->s_type,
2177 true, false, dowarn))
2178 return false;
2179 }
2180 return p1 == p2;
2181 }
2182
2183 /*
2184 * Returns whether all parameters of a prototype are compatible with an
2185 * old-style function declaration.
2186 *
2187 * This is the case if the following conditions are met:
2188 * 1. the prototype has a fixed number of parameters
2189 * 2. no parameter is of type float
2190 * 3. no parameter is converted to another type if integer promotion
2191 * is applied on it
2192 */
2193 static bool
2194 matches_no_arg_function(const type_t *tp, bool *dowarn)
2195 {
2196
2197 if (tp->t_vararg && dowarn != NULL)
2198 *dowarn = true;
2199 for (const sym_t *p = tp->t_params; p != NULL; p = p->s_next) {
2200 tspec_t t = p->s_type->t_tspec;
2201 if (t == FLOAT ||
2202 t == CHAR || t == SCHAR || t == UCHAR ||
2203 t == SHORT || t == USHORT) {
2204 if (dowarn != NULL)
2205 *dowarn = true;
2206 }
2207 }
2208 /* FIXME: Always returning true cannot be correct. */
2209 return true;
2210 }
2211
2212 /*-
2213 * ignqual ignore type qualifiers; used for function parameters
2214 * promot promote the left type; used for comparison of parameters of
2215 * old-style function definitions with parameters of prototypes.
2216 * *dowarn is set to true if an old-style function declaration is not
2217 * compatible with a prototype
2218 */
2219 bool
2220 types_compatible(const type_t *tp1, const type_t *tp2,
2221 bool ignqual, bool promot, bool *dowarn)
2222 {
2223
2224 while (tp1 != NULL && tp2 != NULL) {
2225 tspec_t t = tp1->t_tspec;
2226 if (promot) {
2227 if (t == FLOAT)
2228 t = DOUBLE;
2229 else if (t == CHAR || t == SCHAR)
2230 t = INT;
2231 else if (t == UCHAR)
2232 t = allow_c90 ? INT : UINT;
2233 else if (t == SHORT)
2234 t = INT;
2235 else if (t == USHORT) {
2236 /* CONSTCOND */
2237 t = TARG_INT_MAX < TARG_USHRT_MAX || !allow_c90
2238 ? UINT : INT;
2239 }
2240 }
2241
2242 if (t != tp2->t_tspec)
2243 return false;
2244
2245 if (!qualifiers_correspond(tp1, tp2, ignqual))
2246 return false;
2247
2248 if (is_struct_or_union(t))
2249 return tp1->t_sou == tp2->t_sou;
2250
2251 if (t == ENUM && eflag)
2252 return tp1->t_enum == tp2->t_enum;
2253
2254 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2255 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2256 return false;
2257 }
2258
2259 /* don't check prototypes for traditional */
2260 if (t == FUNC && allow_c90) {
2261 if (tp1->t_proto && tp2->t_proto) {
2262 if (!prototypes_compatible(tp1, tp2, dowarn))
2263 return false;
2264 } else if (tp1->t_proto) {
2265 if (!matches_no_arg_function(tp1, dowarn))
2266 return false;
2267 } else if (tp2->t_proto) {
2268 if (!matches_no_arg_function(tp2, dowarn))
2269 return false;
2270 }
2271 }
2272
2273 tp1 = tp1->t_subt;
2274 tp2 = tp2->t_subt;
2275 ignqual = promot = false;
2276 }
2277
2278 return tp1 == tp2;
2279 }
2280
2281 /*
2282 * Completes a type by copying the dimension and prototype information from a
2283 * second compatible type.
2284 *
2285 * Following lines are legal:
2286 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2287 * "typedef ft(); ft f; f(int); ft g; g(long);"
2288 * This means that, if a type is completed, the type structure must be
2289 * duplicated.
2290 */
2291 void
2292 complete_type(sym_t *dsym, sym_t *ssym)
2293 {
2294 type_t **dstp = &dsym->s_type;
2295 type_t *src = ssym->s_type;
2296
2297 while (*dstp != NULL) {
2298 type_t *dst = *dstp;
2299 lint_assert(src != NULL);
2300 lint_assert(dst->t_tspec == src->t_tspec);
2301 if (dst->t_tspec == ARRAY) {
2302 if (dst->t_dim == 0 && src->t_dim != 0) {
2303 *dstp = dst = block_dup_type(dst);
2304 dst->t_dim = src->t_dim;
2305 dst->t_incomplete_array = false;
2306 }
2307 } else if (dst->t_tspec == FUNC) {
2308 if (!dst->t_proto && src->t_proto) {
2309 *dstp = dst = block_dup_type(dst);
2310 dst->t_proto = true;
2311 dst->t_params = src->t_params;
2312 }
2313 }
2314 dstp = &dst->t_subt;
2315 src = src->t_subt;
2316 }
2317 debug_printf("%s: ", __func__);
2318 debug_sym("dsym: ", dsym, "");
2319 debug_sym("ssym: ", ssym, "\n");
2320 }
2321
2322 sym_t *
2323 declare_parameter(sym_t *sym, bool has_initializer)
2324 {
2325
2326 check_function_definition(sym, true);
2327
2328 check_type(sym);
2329
2330 if (dcs->d_redeclared_symbol != NULL &&
2331 dcs->d_redeclared_symbol->s_block_level == block_level) {
2332 /* redeclaration of formal parameter '%s' */
2333 error(237, sym->s_name);
2334 rmsym(dcs->d_redeclared_symbol);
2335 sym->s_param = true;
2336 }
2337
2338 if (!sym->s_param) {
2339 /* declared parameter '%s' is missing */
2340 error(53, sym->s_name);
2341 sym->s_param = true;
2342 }
2343
2344 if (has_initializer) {
2345 /* cannot initialize parameter '%s' */
2346 error(52, sym->s_name);
2347 }
2348
2349 if (sym->s_type == NULL) /* for c(void()) */
2350 sym->s_type = gettyp(VOID);
2351
2352 tspec_t t = sym->s_type->t_tspec;
2353 if (t == ARRAY)
2354 sym->s_type = block_derive_type(sym->s_type->t_subt, PTR);
2355 if (t == FUNC) {
2356 if (!allow_c90)
2357 /* parameter '%s' has function type, should be ... */
2358 warning(50, sym->s_name);
2359 sym->s_type = block_derive_type(sym->s_type, PTR);
2360 }
2361 if (t == FLOAT && !allow_c90)
2362 sym->s_type = gettyp(DOUBLE);
2363
2364 if (dcs->d_inline)
2365 /* parameter '%s' declared inline */
2366 warning(269, sym->s_name);
2367
2368 /*
2369 * Arguments must have complete types. length_in_bits prints the
2370 * needed error messages (null dimension is impossible because arrays
2371 * are converted to pointers).
2372 */
2373 if (sym->s_type->t_tspec != VOID)
2374 (void)length_in_bits(sym->s_type, sym->s_name);
2375
2376 sym->s_used = dcs->d_used;
2377 mark_as_set(sym);
2378
2379 debug_printf("%s: ", __func__);
2380 debug_sym("", sym, "\n");
2381 return sym;
2382 }
2383
2384 static bool
2385 is_character_pointer(const type_t *tp)
2386 {
2387 tspec_t st;
2388
2389 return tp->t_tspec == PTR &&
2390 (st = tp->t_subt->t_tspec,
2391 st == CHAR || st == SCHAR || st == UCHAR);
2392 }
2393
2394 void
2395 check_func_lint_directives(void)
2396 {
2397
2398 /* check for illegal combinations of lint directives */
2399 if (printflike_argnum != -1 && scanflike_argnum != -1) {
2400 /* ** PRINTFLIKE ** and ** SCANFLIKE ** cannot be combined */
2401 warning(289);
2402 printflike_argnum = scanflike_argnum = -1;
2403 }
2404 if (nvararg != -1 &&
2405 (printflike_argnum != -1 || scanflike_argnum != -1)) {
2406 /* dubious use of ** VARARGS ** with ** %s ** */
2407 warning(288,
2408 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2409 nvararg = -1;
2410 }
2411
2412 /*
2413 * check if the numeric argument of a lint directive is compatible with
2414 * the number of parameters of the function.
2415 */
2416 int narg = 0;
2417 for (const sym_t *p = dcs->d_func_params; p != NULL; p = p->s_next)
2418 narg++;
2419 if (nargusg > narg) {
2420 /* parameter number mismatch in comment ** %s ** */
2421 warning(283, "ARGSUSED");
2422 nargusg = 0;
2423 }
2424 if (nvararg > narg) {
2425 /* parameter number mismatch in comment ** %s ** */
2426 warning(283, "VARARGS");
2427 nvararg = 0;
2428 }
2429 if (printflike_argnum > narg) {
2430 /* parameter number mismatch in comment ** %s ** */
2431 warning(283, "PRINTFLIKE");
2432 printflike_argnum = -1;
2433 } else if (printflike_argnum == 0) {
2434 printflike_argnum = -1;
2435 }
2436 if (scanflike_argnum > narg) {
2437 /* parameter number mismatch in comment ** %s ** */
2438 warning(283, "SCANFLIKE");
2439 scanflike_argnum = -1;
2440 } else if (scanflike_argnum == 0) {
2441 scanflike_argnum = -1;
2442 }
2443 if (printflike_argnum != -1 || scanflike_argnum != -1) {
2444 narg = printflike_argnum != -1
2445 ? printflike_argnum : scanflike_argnum;
2446 const sym_t *param = dcs->d_func_params;
2447 for (int n = 1; n < narg; n++)
2448 param = param->s_next;
2449 if (!is_character_pointer(param->s_type)) {
2450 /* parameter %d must be 'char *' for PRINTFLIKE/... */
2451 warning(293, narg);
2452 printflike_argnum = scanflike_argnum = -1;
2453 }
2454 }
2455 }
2456
2457 /*
2458 * Checks compatibility of an old-style function definition with a previous
2459 * prototype declaration.
2460 * Returns true if the position of the previous declaration should be reported.
2461 */
2462 static bool
2463 check_prototype_declaration(const sym_t *old_param, const sym_t *proto_param)
2464 {
2465 type_t *old_tp = old_param->s_type;
2466 type_t *proto_tp = proto_param->s_type;
2467 bool dowarn = false;
2468
2469 if (!types_compatible(old_tp, proto_tp, true, true, &dowarn)) {
2470 if (types_compatible(old_tp, proto_tp, true, false, &dowarn)) {
2471 /* type of '%s' does not match prototype */
2472 return gnuism(58, old_param->s_name);
2473 } else {
2474 /* type of '%s' does not match prototype */
2475 error(58, old_param->s_name);
2476 return true;
2477 }
2478 }
2479 if (dowarn) {
2480 /* TODO: Make this an error in C99 mode as well. */
2481 if (!allow_trad && !allow_c99)
2482 /* type of '%s' does not match prototype */
2483 error(58, old_param->s_name);
2484 else
2485 /* type of '%s' does not match prototype */
2486 warning(58, old_param->s_name);
2487 return true;
2488 }
2489
2490 return false;
2491 }
2492
2493 /*
2494 * Warn about parameters in old-style function definitions that default to int.
2495 * Check that an old-style function definition is compatible to a previous
2496 * prototype.
2497 */
2498 void
2499 check_func_old_style_parameters(void)
2500 {
2501 sym_t *old_params = funcsym->u.s_old_style_params;
2502 sym_t *proto_params = funcsym->s_type->t_params;
2503
2504 for (sym_t *arg = old_params; arg != NULL; arg = arg->s_next) {
2505 if (arg->s_defparam) {
2506 /* type of parameter '%s' defaults to 'int' */
2507 warning(32, arg->s_name);
2508 arg->s_defparam = false;
2509 mark_as_set(arg);
2510 }
2511 }
2512
2513 /*
2514 * If this is an old-style function definition and a prototype
2515 * exists, compare the types of parameters.
2516 */
2517 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2518 /*
2519 * If the number of parameters does not match, we need not
2520 * continue.
2521 */
2522 int old_n = 0, proto_n = 0;
2523 bool msg = false;
2524 for (const sym_t *p = proto_params; p != NULL; p = p->s_next)
2525 proto_n++;
2526 for (const sym_t *p = old_params; p != NULL; p = p->s_next)
2527 old_n++;
2528 if (old_n != proto_n) {
2529 /* parameter mismatch: %d declared, %d defined */
2530 error(51, proto_n, old_n);
2531 msg = true;
2532 } else {
2533 const sym_t *proto_param = proto_params;
2534 const sym_t *old_param = old_params;
2535 while (old_n-- > 0) {
2536 msg |= check_prototype_declaration(old_param, proto_param);
2537 proto_param = proto_param->s_next;
2538 old_param = old_param->s_next;
2539 }
2540 }
2541 if (msg && rflag) {
2542 /* prototype declaration */
2543 message_at(285, &dcs->d_redeclared_symbol->s_def_pos);
2544 }
2545
2546 /* from now on the prototype is valid */
2547 funcsym->s_osdef = false;
2548 funcsym->u.s_old_style_params = NULL;
2549 }
2550 }
2551
2552 static void
2553 check_local_hiding(const sym_t *dsym)
2554 {
2555 switch (dsym->s_scl) {
2556 case AUTO:
2557 /* automatic '%s' hides external declaration */
2558 warning(86, dsym->s_name);
2559 break;
2560 case STATIC:
2561 /* static '%s' hides external declaration */
2562 warning(87, dsym->s_name);
2563 break;
2564 case TYPEDEF:
2565 /* typedef '%s' hides external declaration */
2566 warning(88, dsym->s_name);
2567 break;
2568 case EXTERN:
2569 /* Already checked in declare_external_in_block. */
2570 break;
2571 default:
2572 lint_assert(/*CONSTCOND*/false);
2573 }
2574 }
2575
2576 static void
2577 check_local_redeclaration(const sym_t *dsym, sym_t *rdsym)
2578 {
2579 if (rdsym->s_block_level == 0) {
2580 if (hflag)
2581 check_local_hiding(dsym);
2582
2583 } else if (rdsym->s_block_level == block_level) {
2584
2585 /* no hflag, because it's illegal! */
2586 if (rdsym->s_param) {
2587 /*
2588 * if allow_c90, a "redeclaration of '%s'" error
2589 * is produced below
2590 */
2591 if (!allow_c90) {
2592 if (hflag) {
2593 /* declaration of '%s' hides ... */
2594 warning(91, dsym->s_name);
2595 }
2596 rmsym(rdsym);
2597 }
2598 }
2599
2600 } else if (rdsym->s_block_level < block_level) {
2601 if (hflag) {
2602 /* declaration of '%s' hides earlier one */
2603 warning(95, dsym->s_name);
2604 }
2605 }
2606
2607 if (rdsym->s_block_level == block_level) {
2608 /* redeclaration of '%s' */
2609 error(27, dsym->s_name);
2610 rmsym(rdsym);
2611 }
2612 }
2613
2614 /* Processes (re)declarations of external symbols inside blocks. */
2615 static void
2616 declare_external_in_block(sym_t *dsym)
2617 {
2618
2619 /* look for a symbol with the same name */
2620 sym_t *esym = dcs->d_redeclared_symbol;
2621 while (esym != NULL && esym->s_block_level != 0) {
2622 while ((esym = esym->s_symtab_next) != NULL) {
2623 if (esym->s_kind != FVFT)
2624 continue;
2625 if (strcmp(dsym->s_name, esym->s_name) == 0)
2626 break;
2627 }
2628 }
2629 if (esym == NULL)
2630 return;
2631 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2632 /* gcc accepts this without a warning, pcc prints an error. */
2633 /* redeclaration of '%s' */
2634 warning(27, dsym->s_name);
2635 print_previous_declaration(esym);
2636 return;
2637 }
2638
2639 bool dowarn = false;
2640 bool compatible = types_compatible(esym->s_type, dsym->s_type,
2641 false, false, &dowarn);
2642
2643 if (!compatible || dowarn) {
2644 if (esym->s_scl == EXTERN) {
2645 /* inconsistent redeclaration of extern '%s' */
2646 warning(90, dsym->s_name);
2647 print_previous_declaration(esym);
2648 } else {
2649 /* inconsistent redeclaration of static '%s' */
2650 warning(92, dsym->s_name);
2651 print_previous_declaration(esym);
2652 }
2653 }
2654
2655 if (compatible) {
2656 /*
2657 * Remember the external symbol, so we can update usage
2658 * information at the end of the block.
2659 */
2660 dsym->s_ext_sym = esym;
2661 }
2662 }
2663
2664 /*
2665 * Completes a single local declaration/definition.
2666 */
2667 void
2668 declare_local(sym_t *dsym, bool has_initializer)
2669 {
2670
2671 /* Correct a mistake done in declarator_name(). */
2672 if (dsym->s_type->t_tspec == FUNC) {
2673 dsym->s_def = DECL;
2674 if (dcs->d_scl == NOSCL)
2675 dsym->s_scl = EXTERN;
2676 }
2677
2678 if (dsym->s_scl == EXTERN) {
2679 /* nested 'extern' declaration of '%s' */
2680 warning(352, dsym->s_name);
2681 }
2682
2683 if (dsym->s_type->t_tspec == FUNC) {
2684 if (dsym->s_scl == STATIC) {
2685 /* dubious static function '%s' at block level */
2686 warning(93, dsym->s_name);
2687 dsym->s_scl = EXTERN;
2688 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2689 /* function '%s' has illegal storage class */
2690 error(94, dsym->s_name);
2691 dsym->s_scl = EXTERN;
2692 }
2693 }
2694
2695 /*
2696 * functions may be declared inline at local scope, although
2697 * this has no effect for a later definition of the same
2698 * function.
2699 * XXX it should have an effect if !allow_c90 is set. this would
2700 * also be the way gcc behaves.
2701 */
2702 if (dcs->d_inline) {
2703 if (dsym->s_type->t_tspec == FUNC)
2704 dsym->s_inline = true;
2705 else {
2706 /* variable '%s' declared inline */
2707 warning(268, dsym->s_name);
2708 }
2709 }
2710
2711 check_function_definition(dsym, true);
2712
2713 check_type(dsym);
2714
2715 if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
2716 declare_external_in_block(dsym);
2717
2718 if (dsym->s_scl == EXTERN) {
2719 /*
2720 * XXX if the static variable at level 0 is only defined
2721 * later, checking will be possible.
2722 */
2723 if (dsym->s_ext_sym == NULL)
2724 outsym(dsym, EXTERN, dsym->s_def);
2725 else
2726 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2727 }
2728
2729 if (dcs->d_redeclared_symbol != NULL)
2730 check_local_redeclaration(dsym, dcs->d_redeclared_symbol);
2731
2732 if (has_initializer && !check_init(dsym)) {
2733 dsym->s_def = DEF;
2734 mark_as_set(dsym);
2735 }
2736
2737 if (dsym->s_scl == TYPEDEF) {
2738 dsym->s_type = block_dup_type(dsym->s_type);
2739 dsym->s_type->t_typedef = true;
2740 set_first_typedef(dsym->s_type, dsym);
2741 }
2742
2743 if (dsym->s_scl == STATIC && any_query_enabled) {
2744 /* static variable '%s' in function */
2745 query_message(11, dsym->s_name);
2746 }
2747
2748 debug_printf("%s: ", __func__);
2749 debug_sym("", dsym, "\n");
2750 }
2751
2752 /* Create a symbol for an abstract declaration. */
2753 static sym_t *
2754 abstract_name_level(bool enclosing)
2755 {
2756
2757 lint_assert(dcs->d_kind == DLK_ABSTRACT
2758 || dcs->d_kind == DLK_PROTO_PARAMS);
2759
2760 sym_t *sym = block_zero_alloc(sizeof(*sym), "sym");
2761 sym->s_name = unnamed;
2762 sym->s_def = DEF;
2763 sym->s_scl = ABSTRACT;
2764 sym->s_block_level = -1;
2765 sym->s_param = dcs->d_kind == DLK_PROTO_PARAMS;
2766
2767 /*
2768 * At this point, dcs->d_type contains only the basic type. That
2769 * type will be updated later, adding pointers, arrays and functions
2770 * as necessary.
2771 */
2772 sym->s_type = (enclosing ? dcs->d_enclosing : dcs)->d_type;
2773 dcs->d_redeclared_symbol = NULL;
2774
2775 debug_printf("%s: ", __func__);
2776 debug_sym("", sym, "\n");
2777 return sym;
2778 }
2779
2780 sym_t *
2781 abstract_name(void)
2782 {
2783 return abstract_name_level(false);
2784 }
2785
2786 sym_t *
2787 abstract_enclosing_name(void)
2788 {
2789 return abstract_name_level(true);
2790 }
2791
2792 /* Removes anything which has nothing to do on global level. */
2793 void
2794 global_clean_up(void)
2795 {
2796
2797 while (dcs->d_enclosing != NULL)
2798 end_declaration_level();
2799
2800 clean_up_after_error();
2801 block_level = 0;
2802 mem_block_level = 0;
2803 debug_step("%s: mem_block_level = %zu", __func__, mem_block_level);
2804 global_clean_up_decl(true);
2805 }
2806
2807 sym_t *
2808 declare_abstract_type(sym_t *sym)
2809 {
2810
2811 check_function_definition(sym, true);
2812 check_type(sym);
2813 return sym;
2814 }
2815
2816 /* Checks size after declarations of variables and their initialization. */
2817 void
2818 check_size(const sym_t *dsym)
2819 {
2820
2821 if (dsym->s_def == DEF &&
2822 dsym->s_scl != TYPEDEF &&
2823 dsym->s_type->t_tspec != FUNC &&
2824 length_in_bits(dsym->s_type, dsym->s_name) == 0 &&
2825 dsym->s_type->t_tspec == ARRAY &&
2826 dsym->s_type->t_dim == 0) {
2827 if (!allow_c90)
2828 /* empty array declaration for '%s' */
2829 warning(190, dsym->s_name);
2830 else
2831 /* empty array declaration for '%s' */
2832 error(190, dsym->s_name);
2833 }
2834 }
2835
2836 /* Mark an object as set if it is not already. */
2837 void
2838 mark_as_set(sym_t *sym)
2839 {
2840
2841 if (!sym->s_set) {
2842 sym->s_set = true;
2843 sym->s_set_pos = unique_curr_pos();
2844 }
2845 }
2846
2847 /* Mark an object as used if it is not already. */
2848 void
2849 mark_as_used(sym_t *sym, bool fcall, bool szof)
2850 {
2851
2852 if (!sym->s_used) {
2853 sym->s_used = true;
2854 sym->s_use_pos = unique_curr_pos();
2855 }
2856 /*
2857 * For function calls, another record is written.
2858 *
2859 * XXX: Should symbols used in sizeof() be treated as used or not?
2860 * Probably not, because there is no point in declaring an external
2861 * variable only to get its size.
2862 */
2863 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2864 outusg(sym);
2865 }
2866
2867 /* Warns about variables and labels that are not used or only set. */
2868 void
2869 check_usage(const decl_level *dl)
2870 {
2871 /* for this warning LINTED has no effect */
2872 int saved_lwarn = lwarn;
2873 lwarn = LWARN_ALL;
2874
2875 debug_step("begin lwarn %d", lwarn);
2876 for (sym_t *sym = dl->d_first_dlsym;
2877 sym != NULL; sym = sym->s_level_next)
2878 check_usage_sym(dl->d_asm, sym);
2879 lwarn = saved_lwarn;
2880 debug_step("end lwarn %d", lwarn);
2881 }
2882
2883 static void
2884 check_parameter_usage(bool novar, const sym_t *arg)
2885 {
2886
2887 lint_assert(arg->s_set);
2888
2889 if (novar)
2890 return;
2891
2892 if (!arg->s_used && !vflag) {
2893 /* parameter '%s' unused in function '%s' */
2894 warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name);
2895 }
2896 }
2897
2898 static void
2899 check_variable_usage(bool novar, const sym_t *sym)
2900 {
2901
2902 lint_assert(block_level != 0);
2903
2904 /* example at file scope: int c = ({ return 3; }); */
2905 if (sym->s_block_level == 0 && ch_isdigit(sym->s_name[0]))
2906 return;
2907
2908 /* errors in expressions easily cause lots of these warnings */
2909 if (seen_error)
2910 return;
2911
2912 /*
2913 * XXX Only variables are checked, although types should
2914 * probably also be checked
2915 */
2916 scl_t sc = sym->s_scl;
2917 if (sc != EXTERN && sc != STATIC && sc != AUTO && sc != REG)
2918 return;
2919
2920 if (novar)
2921 return;
2922
2923 if (sc == EXTERN) {
2924 if (!sym->s_used && !sym->s_set) {
2925 /* '%s' unused in function '%s' */
2926 warning_at(192, &sym->s_def_pos,
2927 sym->s_name, funcsym->s_name);
2928 }
2929 } else {
2930 if (sym->s_set && !sym->s_used) {
2931 /* '%s' set but not used in function '%s' */
2932 warning_at(191, &sym->s_set_pos,
2933 sym->s_name, funcsym->s_name);
2934 } else if (!sym->s_used) {
2935 /* '%s' unused in function '%s' */
2936 warning_at(192, &sym->s_def_pos,
2937 sym->s_name, funcsym->s_name);
2938 }
2939 }
2940
2941 if (sc == EXTERN) {
2942 /*
2943 * information about usage is taken over into the symbol
2944 * table entry at level 0 if the symbol was locally declared
2945 * as an external symbol.
2946 *
2947 * XXX This is wrong for symbols declared static at level 0
2948 * if the usage information stems from sizeof(). This is
2949 * because symbols at level 0 only used in sizeof() are
2950 * considered to not be used.
2951 */
2952 sym_t *xsym = sym->s_ext_sym;
2953 if (xsym != NULL) {
2954 if (sym->s_used && !xsym->s_used) {
2955 xsym->s_used = true;
2956 xsym->s_use_pos = sym->s_use_pos;
2957 }
2958 if (sym->s_set && !xsym->s_set) {
2959 xsym->s_set = true;
2960 xsym->s_set_pos = sym->s_set_pos;
2961 }
2962 }
2963 }
2964 }
2965
2966 static void
2967 check_label_usage(const sym_t *lab)
2968 {
2969
2970 lint_assert(block_level == 1);
2971 lint_assert(lab->s_block_level == 1);
2972
2973 if (funcsym == NULL)
2974 /* syntax error '%s' */
2975 error(249, "labels are only valid inside a function");
2976 else if (lab->s_set && !lab->s_used)
2977 /* label '%s' unused in function '%s' */
2978 warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name);
2979 else if (!lab->s_set)
2980 /* undefined label '%s' */
2981 warning_at(23, &lab->s_use_pos, lab->s_name);
2982 }
2983
2984 static void
2985 check_tag_usage(const sym_t *sym)
2986 {
2987
2988 if (!is_incomplete(sym->s_type))
2989 return;
2990
2991 /* always complain about incomplete tags declared inside blocks */
2992 if (zflag || dcs->d_kind != DLK_EXTERN)
2993 return;
2994
2995 switch (sym->s_type->t_tspec) {
2996 case STRUCT:
2997 /* struct '%s' never defined */
2998 warning_at(233, &sym->s_def_pos, sym->s_name);
2999 break;
3000 case UNION:
3001 /* union '%s' never defined */
3002 warning_at(234, &sym->s_def_pos, sym->s_name);
3003 break;
3004 default:
3005 lint_assert(sym->s_type->t_tspec == ENUM);
3006 /* enum '%s' never defined */
3007 warning_at(235, &sym->s_def_pos, sym->s_name);
3008 break;
3009 }
3010 }
3011
3012 /* Warns about a variable or a label that is not used or only set. */
3013 void
3014 check_usage_sym(bool novar, const sym_t *sym)
3015 {
3016
3017 if (sym->s_block_level == -1)
3018 return;
3019
3020 if (sym->s_kind == FVFT && sym->s_param)
3021 check_parameter_usage(novar, sym);
3022 else if (sym->s_kind == FVFT)
3023 check_variable_usage(novar, sym);
3024 else if (sym->s_kind == FLABEL)
3025 check_label_usage(sym);
3026 else if (sym->s_kind == FTAG)
3027 check_tag_usage(sym);
3028 }
3029
3030 static void
3031 check_global_variable_size(const sym_t *sym)
3032 {
3033
3034 if (sym->s_def != TDEF)
3035 return;
3036 if (sym->s_type->t_tspec == FUNC) {
3037 /* Maybe a syntax error after a function declaration. */
3038 return;
3039 }
3040 if (sym->s_def == TDEF && sym->s_type->t_tspec == VOID) {
3041 /* Prevent an internal error in length_in_bits below. */
3042 return;
3043 }
3044
3045 pos_t cpos = curr_pos;
3046 curr_pos = sym->s_def_pos;
3047 int len_in_bits = length_in_bits(sym->s_type, sym->s_name);
3048 curr_pos = cpos;
3049
3050 if (len_in_bits == 0 &&
3051 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3052 /* TODO: C99 6.7.5.2p1 defines this as an error as well. */
3053 if (!allow_c90 ||
3054 (sym->s_scl == EXTERN && (allow_trad || allow_c99))) {
3055 /* empty array declaration for '%s' */
3056 warning_at(190, &sym->s_def_pos, sym->s_name);
3057 } else {
3058 /* empty array declaration for '%s' */
3059 error_at(190, &sym->s_def_pos, sym->s_name);
3060 }
3061 }
3062 }
3063
3064 static void
3065 check_unused_static_global_variable(const sym_t *sym)
3066 {
3067 if (sym->s_type->t_tspec == FUNC) {
3068 if (sym->s_def == DEF) {
3069 if (!sym->s_inline)
3070 /* static function '%s' unused */
3071 warning_at(236, &sym->s_def_pos, sym->s_name);
3072 } else {
3073 /* static function '%s' declared but not defined */
3074 warning_at(290, &sym->s_def_pos, sym->s_name);
3075 }
3076 } else if (!sym->s_set) {
3077 /* static variable '%s' unused */
3078 warning_at(226, &sym->s_def_pos, sym->s_name);
3079 } else {
3080 /* static variable '%s' set but not used */
3081 warning_at(307, &sym->s_def_pos, sym->s_name);
3082 }
3083 }
3084
3085 static void
3086 check_static_global_variable(const sym_t *sym)
3087 {
3088 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3089 /* static function '%s' called but not defined */
3090 error_at(225, &sym->s_use_pos, sym->s_name);
3091 }
3092
3093 if (!sym->s_used)
3094 check_unused_static_global_variable(sym);
3095
3096 if (allow_c90 && sym->s_def == TDEF && sym->s_type->t_const) {
3097 /* const object '%s' should have initializer */
3098 warning_at(227, &sym->s_def_pos, sym->s_name);
3099 }
3100 }
3101
3102 static void
3103 check_global_variable(const sym_t *sym)
3104 {
3105 scl_t scl = sym->s_scl;
3106
3107 if (scl == TYPEDEF || scl == BOOL_CONST || scl == ENUM_CONST)
3108 return;
3109
3110 if (scl == NOSCL)
3111 return; /* May be caused by a syntax error. */
3112
3113 lint_assert(scl == EXTERN || scl == STATIC);
3114
3115 check_global_variable_size(sym);
3116
3117 if (scl == STATIC)
3118 check_static_global_variable(sym);
3119 }
3120
3121 void
3122 end_translation_unit(void)
3123 {
3124
3125 if (block_level != 0 || dcs->d_enclosing != NULL)
3126 norecover();
3127
3128 for (const sym_t *sym = dcs->d_first_dlsym;
3129 sym != NULL; sym = sym->s_level_next) {
3130 if (sym->s_block_level == -1)
3131 continue;
3132 if (sym->s_kind == FVFT)
3133 check_global_variable(sym);
3134 else if (sym->s_kind == FTAG)
3135 check_tag_usage(sym);
3136 else
3137 lint_assert(sym->s_kind == FMEMBER);
3138 }
3139 }
3140
3141 /*
3142 * Prints information about location of previous definition/declaration.
3143 */
3144 void
3145 print_previous_declaration(const sym_t *psym)
3146 {
3147
3148 if (!rflag)
3149 return;
3150
3151 if (psym->s_def == DEF || psym->s_def == TDEF) {
3152 /* previous definition of '%s' */
3153 message_at(261, &psym->s_def_pos, psym->s_name);
3154 } else {
3155 /* previous declaration of '%s' */
3156 message_at(260, &psym->s_def_pos, psym->s_name);
3157 }
3158 }
3159
3160 /*
3161 * Gets a node for a constant and returns the value of this constant
3162 * as integer.
3163 *
3164 * If the node is not constant or too large for int or of type float,
3165 * a warning will be printed.
3166 *
3167 * to_int_constant() should be used only inside declarations. If it is used in
3168 * expressions, it frees the memory used for the expression.
3169 */
3170 int
3171 to_int_constant(tnode_t *tn, bool required)
3172 {
3173
3174 if (tn == NULL)
3175 return 1;
3176
3177 val_t *v = integer_constant(tn, required);
3178 bool is_unsigned = is_uinteger(v->v_tspec);
3179 int64_t val = v->u.integer;
3180 free(v);
3181
3182 /*
3183 * Abstract declarations are used inside expression. To free
3184 * the memory would be a fatal error.
3185 * We don't free blocks that are inside casts because these
3186 * will be used later to match types.
3187 */
3188 if (tn->tn_op != CON && dcs->d_kind != DLK_ABSTRACT)
3189 expr_free_all();
3190
3191 bool out_of_bounds = is_unsigned
3192 ? (uint64_t)val > (uint64_t)TARG_INT_MAX
3193 : val > (int64_t)TARG_INT_MAX || val < (int64_t)TARG_INT_MIN;
3194 if (out_of_bounds)
3195 /* integral constant too large */
3196 warning(56);
3197 return (int)val;
3198 }
3199