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