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