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