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