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