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