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