tree.c revision 1.152 1 /* $NetBSD: tree.c,v 1.152 2021/01/14 07:42:31 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
18 * The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: tree.c,v 1.152 2021/01/14 07:42:31 rillig Exp $");
41 #endif
42
43 #include <float.h>
44 #include <limits.h>
45 #include <math.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "lint1.h"
51 #include "cgram.h"
52
53 static tnode_t *new_integer_constant_node(tspec_t, int64_t);
54 static void check_pointer_comparison(op_t,
55 const tnode_t *, const tnode_t *);
56 static int check_assign_types_compatible(op_t, int,
57 const tnode_t *, const tnode_t *);
58 static void check_bad_enum_operation(op_t,
59 const tnode_t *, const tnode_t *);
60 static void check_enum_type_mismatch(op_t, int,
61 const tnode_t *, const tnode_t *);
62 static void check_enum_int_mismatch(op_t, int,
63 const tnode_t *, const tnode_t *);
64 static tnode_t *new_tnode(op_t, type_t *, tnode_t *, tnode_t *);
65 static void balance(op_t, tnode_t **, tnode_t **);
66 static void warn_incompatible_types(op_t, tspec_t, tspec_t);
67 static void warn_incompatible_pointers(const mod_t *,
68 const type_t *, const type_t *);
69 static void merge_qualifiers(type_t **, type_t *, type_t *);
70 static bool has_constant_member(const type_t *);
71 static void check_prototype_conversion(int, tspec_t, tspec_t, type_t *,
72 tnode_t *);
73 static void check_integer_conversion(op_t, int, tspec_t, tspec_t, type_t *,
74 tnode_t *);
75 static void check_pointer_integer_conversion(op_t, tspec_t, type_t *,
76 tnode_t *);
77 static void check_pointer_conversion(op_t, tnode_t *, type_t *);
78 static tnode_t *build_struct_access(op_t, tnode_t *, tnode_t *);
79 static tnode_t *build_prepost_incdec(op_t, tnode_t *);
80 static tnode_t *build_real_imag(op_t, tnode_t *);
81 static tnode_t *build_ampersand(tnode_t *, int);
82 static tnode_t *build_plus_minus(op_t, tnode_t *, tnode_t *);
83 static tnode_t *build_bit_shift(op_t, tnode_t *, tnode_t *);
84 static tnode_t *build_colon(tnode_t *, tnode_t *);
85 static tnode_t *build_assignment(op_t, tnode_t *, tnode_t *);
86 static tnode_t *plength(type_t *);
87 static tnode_t *fold(tnode_t *);
88 static tnode_t *fold_test(tnode_t *);
89 static tnode_t *fold_float(tnode_t *);
90 static tnode_t *check_function_arguments(type_t *, tnode_t *);
91 static tnode_t *check_prototype_argument(int, type_t *, tnode_t *);
92 static void check_null_effect(const tnode_t *);
93 static void display_expression(const tnode_t *, int);
94 static void check_array_index(tnode_t *, int);
95 static void check_integer_comparison(op_t, tnode_t *, tnode_t *);
96 static void check_precedence_confusion(tnode_t *);
97
98 extern sig_atomic_t fpe;
99
100 #ifdef DEBUG
101 static void
102 dprint_node(const tnode_t *tn)
103 {
104 static int indent = 0;
105
106 op_t op;
107
108 if (tn == NULL) {
109 printf("%*s" "null\n", indent, "");
110 return;
111 }
112
113 op = tn->tn_op;
114 printf("%*s%s: %s%s%s",
115 indent, "",
116 op == CVT && !tn->tn_cast ? "convert" :
117 op == NAME ? "name" : getopname(op),
118 type_name(tn->tn_type), tn->tn_lvalue ? " lvalue" : "",
119 tn->tn_parenthesized ? " ()" : "");
120
121 if (op == NAME)
122 printf(" %s\n", tn->tn_sym->s_name);
123 else if (op == CON)
124 printf(" value=?\n");
125 else if (op == STRING)
126 printf(" length=%zu\n", tn->tn_string->st_len);
127 else {
128 printf("\n");
129
130 indent += 2;
131 dprint_node(tn->tn_left);
132 if (modtab[op].m_binary || tn->tn_right != NULL)
133 dprint_node(tn->tn_right);
134 indent -= 2;
135 }
136 }
137 #else
138 /*ARGSUSED*/
139 static void
140 dprint_node(const tnode_t *tn)
141 {
142 }
143 #endif
144
145 /*
146 * Increase degree of reference.
147 * This is most often used to change type "T" in type "pointer to T".
148 */
149 type_t *
150 incref(type_t *tp, tspec_t t)
151 {
152 type_t *tp2;
153
154 tp2 = getblk(sizeof (type_t));
155 tp2->t_tspec = t;
156 tp2->t_subt = tp;
157 return tp2;
158 }
159
160 /*
161 * same for use in expressions
162 */
163 type_t *
164 tincref(type_t *tp, tspec_t t)
165 {
166 type_t *tp2;
167
168 tp2 = tgetblk(sizeof (type_t));
169 tp2->t_tspec = t;
170 tp2->t_subt = tp;
171 return tp2;
172 }
173
174 /*
175 * Create a node for a constant.
176 */
177 tnode_t *
178 new_constant_node(type_t *tp, val_t *v)
179 {
180 tnode_t *n;
181
182 n = getnode();
183 n->tn_op = CON;
184 n->tn_type = tp;
185 n->tn_val = tgetblk(sizeof (val_t));
186 n->tn_val->v_tspec = tp->t_tspec;
187 n->tn_val->v_ansiu = v->v_ansiu;
188 n->tn_val->v_u = v->v_u;
189 free(v);
190 return n;
191 }
192
193 static tnode_t *
194 new_integer_constant_node(tspec_t t, int64_t q)
195 {
196 tnode_t *n;
197
198 n = getnode();
199 n->tn_op = CON;
200 n->tn_type = gettyp(t);
201 n->tn_val = tgetblk(sizeof (val_t));
202 n->tn_val->v_tspec = t;
203 n->tn_val->v_quad = q;
204 return n;
205 }
206
207 /*
208 * Create a node for a name (symbol table entry).
209 * ntok is the token which follows the name.
210 */
211 tnode_t *
212 new_name_node(sym_t *sym, int ntok)
213 {
214 tnode_t *n;
215
216 if (sym->s_scl == NOSCL) {
217 sym->s_scl = EXTERN;
218 sym->s_def = DECL;
219 if (ntok == T_LPAREN) {
220 if (sflag) {
221 /* function implicitly declared to ... */
222 warning(215);
223 }
224 /*
225 * XXX if tflag is set the symbol should be
226 * exported to level 0
227 */
228 sym->s_type = incref(sym->s_type, FUNC);
229 } else {
230 if (!blklev) {
231 /* %s undefined */
232 error(99, sym->s_name);
233 } else {
234 int fixtype;
235 if (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
236 strcmp(sym->s_name, "__PRETTY_FUNCTION__")
237 == 0) {
238 /* __FUNCTION__/__PRETTY_FUNCTION... */
239 gnuism(316);
240 fixtype = 1;
241 } else if (strcmp(sym->s_name, "__func__") == 0) {
242 if (!Sflag)
243 /* __func__ is a C9X feature */
244 warning(317);
245 fixtype = 1;
246 } else {
247 /* %s undefined */
248 error(99, sym->s_name);
249 fixtype = 0;
250 }
251 if (fixtype) {
252 sym->s_type = incref(gettyp(CHAR), PTR);
253 sym->s_type->t_const = 1;
254 }
255 }
256 }
257 }
258
259 if (sym->s_kind != FVFT && sym->s_kind != FMEMBER)
260 LERROR("new_name_node(%d)", sym->s_kind);
261
262 n = getnode();
263 n->tn_type = sym->s_type;
264 if (sym->s_scl != ENUMCON) {
265 n->tn_op = NAME;
266 n->tn_sym = sym;
267 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
268 n->tn_lvalue = 1;
269 } else {
270 n->tn_op = CON;
271 n->tn_val = tgetblk(sizeof (val_t));
272 *n->tn_val = sym->s_value;
273 }
274
275 return n;
276 }
277
278 tnode_t *
279 new_string_node(strg_t *strg)
280 {
281 size_t len;
282 tnode_t *n;
283
284 len = strg->st_len;
285
286 n = getnode();
287
288 n->tn_op = STRING;
289 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
290 n->tn_type->t_dim = len + 1;
291 n->tn_lvalue = 1;
292
293 n->tn_string = tgetblk(sizeof (strg_t));
294 n->tn_string->st_tspec = strg->st_tspec;
295 n->tn_string->st_len = len;
296
297 if (strg->st_tspec == CHAR) {
298 n->tn_string->st_cp = tgetblk(len + 1);
299 (void)memcpy(n->tn_string->st_cp, strg->st_cp, len + 1);
300 free(strg->st_cp);
301 } else {
302 n->tn_string->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
303 (void)memcpy(n->tn_string->st_wcp, strg->st_wcp,
304 (len + 1) * sizeof (wchar_t));
305 free(strg->st_wcp);
306 }
307 free(strg);
308
309 return n;
310 }
311
312 /*
313 * Returns a symbol which has the same name as the msym argument and is a
314 * member of the struct or union specified by the tn argument.
315 */
316 sym_t *
317 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
318 {
319 str_t *str;
320 type_t *tp;
321 sym_t *sym, *csym;
322 int eq;
323 tspec_t t;
324
325 /*
326 * Remove the member if it was unknown until now (Which means
327 * that no defined struct or union has a member with the same name).
328 */
329 if (msym->s_scl == NOSCL) {
330 /* undefined struct/union member: %s */
331 error(101, msym->s_name);
332 rmsym(msym);
333 msym->s_kind = FMEMBER;
334 msym->s_scl = MOS;
335 msym->s_styp = tgetblk(sizeof (str_t));
336 msym->s_styp->stag = tgetblk(sizeof (sym_t));
337 msym->s_styp->stag->s_name = unnamed;
338 msym->s_value.v_tspec = INT;
339 return msym;
340 }
341
342 /* Set str to the tag of which msym is expected to be a member. */
343 str = NULL;
344 t = (tp = tn->tn_type)->t_tspec;
345 if (op == POINT) {
346 if (t == STRUCT || t == UNION)
347 str = tp->t_str;
348 } else if (op == ARROW && t == PTR) {
349 t = (tp = tp->t_subt)->t_tspec;
350 if (t == STRUCT || t == UNION)
351 str = tp->t_str;
352 }
353
354 /*
355 * If this struct/union has a member with the name of msym, return
356 * return this it.
357 */
358 if (str != NULL) {
359 for (sym = msym; sym != NULL; sym = sym->s_link) {
360 if (sym->s_scl != MOS && sym->s_scl != MOU)
361 continue;
362 if (sym->s_styp != str)
363 continue;
364 if (strcmp(sym->s_name, msym->s_name) != 0)
365 continue;
366 return sym;
367 }
368 }
369
370 /*
371 * Set eq to 0 if there are struct/union members with the same name
372 * and different types and/or offsets.
373 */
374 eq = 1;
375 for (csym = msym; csym != NULL; csym = csym->s_link) {
376 if (csym->s_scl != MOS && csym->s_scl != MOU)
377 continue;
378 if (strcmp(msym->s_name, csym->s_name) != 0)
379 continue;
380 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
381 int w;
382
383 if (sym->s_scl != MOS && sym->s_scl != MOU)
384 continue;
385 if (strcmp(csym->s_name, sym->s_name) != 0)
386 continue;
387 if (csym->s_value.v_quad != sym->s_value.v_quad) {
388 eq = 0;
389 break;
390 }
391 w = 0;
392 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w;
393 if (!eq)
394 break;
395 if (csym->s_bitfield != sym->s_bitfield) {
396 eq = 0;
397 break;
398 }
399 if (csym->s_bitfield) {
400 type_t *tp1, *tp2;
401
402 tp1 = csym->s_type;
403 tp2 = sym->s_type;
404 if (tp1->t_flen != tp2->t_flen) {
405 eq = 0;
406 break;
407 }
408 if (tp1->t_foffs != tp2->t_foffs) {
409 eq = 0;
410 break;
411 }
412 }
413 }
414 if (!eq)
415 break;
416 }
417
418 /*
419 * Now handle the case in which the left operand refers really
420 * to a struct/union, but the right operand is not member of it.
421 */
422 if (str != NULL) {
423 if (eq && tflag) {
424 /* illegal member use: %s */
425 warning(102, msym->s_name);
426 } else {
427 /* illegal member use: %s */
428 error(102, msym->s_name);
429 }
430 return msym;
431 }
432
433 /*
434 * Now the left operand of ARROW does not point to a struct/union
435 * or the left operand of POINT is no struct/union.
436 */
437 if (eq) {
438 if (op == POINT) {
439 if (tflag) {
440 /* left operand of '.' must be struct/... */
441 warning(103);
442 } else {
443 /* left operand of '.' must be struct/... */
444 error(103);
445 }
446 } else {
447 /* left operand of "->" must be pointer to ... */
448 if (tflag && tn->tn_type->t_tspec == PTR) {
449 /* left operand of '->' must be pointer ... */
450 warning(104, type_name(tn->tn_type));
451 } else {
452 /* left operand of '->' must be pointer ... */
453 error(104, type_name(tn->tn_type));
454 }
455 }
456 } else {
457 if (tflag) {
458 /* non-unique member requires struct/union %s */
459 error(105, op == POINT ? "object" : "pointer");
460 } else {
461 /* unacceptable operand of '%s' */
462 error(111, modtab[op].m_name);
463 }
464 }
465
466 return msym;
467 }
468
469 /*
470 * Create a tree node. Called for most operands except function calls,
471 * sizeof and casts.
472 *
473 * op operator
474 * ln left operand
475 * rn if not NULL, right operand
476 */
477 tnode_t *
478 build(op_t op, tnode_t *ln, tnode_t *rn)
479 {
480 mod_t *mp;
481 tnode_t *ntn;
482 type_t *rtp;
483
484 mp = &modtab[op];
485
486 /* If there was an error in one of the operands, return. */
487 if (ln == NULL || (mp->m_binary && rn == NULL))
488 return NULL;
489
490 /*
491 * Apply class conversions to the left operand, but only if its
492 * value is needed or it is compared with null.
493 */
494 if (mp->m_vctx || mp->m_tctx)
495 ln = cconv(ln);
496 /*
497 * The right operand is almost always in a test or value context,
498 * except if it is a struct or union member.
499 */
500 if (mp->m_binary && op != ARROW && op != POINT)
501 rn = cconv(rn);
502
503 /*
504 * Print some warnings for comparisons of unsigned values with
505 * constants lower than or equal to null. This must be done
506 * before promote() because otherwise unsigned char and unsigned
507 * short would be promoted to int. Also types are tested to be
508 * CHAR, which would also become int.
509 */
510 if (mp->m_comp)
511 check_integer_comparison(op, ln, rn);
512
513 /*
514 * Promote the left operand if it is in a test or value context
515 */
516 if (mp->m_vctx || mp->m_tctx)
517 ln = promote(op, 0, ln);
518 /*
519 * Promote the right operand, but only if it is no struct or
520 * union member, or if it is not to be assigned to the left operand
521 */
522 if (mp->m_binary && op != ARROW && op != POINT &&
523 op != ASSIGN && op != RETURN) {
524 rn = promote(op, 0, rn);
525 }
526
527 /*
528 * If the result of the operation is different for signed or
529 * unsigned operands and one of the operands is signed only in
530 * ANSI C, print a warning.
531 */
532 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
533 /* ANSI C treats constant as unsigned, op %s */
534 warning(218, mp->m_name);
535 ln->tn_val->v_ansiu = 0;
536 }
537 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
538 /* ANSI C treats constant as unsigned, op %s */
539 warning(218, mp->m_name);
540 rn->tn_val->v_ansiu = 0;
541 }
542
543 /* Make sure both operands are of the same type */
544 if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
545 balance(op, &ln, &rn);
546
547 /*
548 * Check types for compatibility with the operation and mutual
549 * compatibility. Return if there are serious problems.
550 */
551 if (!typeok(op, 0, ln, rn))
552 return NULL;
553
554 /* And now create the node. */
555 switch (op) {
556 case POINT:
557 case ARROW:
558 ntn = build_struct_access(op, ln, rn);
559 break;
560 case INCAFT:
561 case DECAFT:
562 case INCBEF:
563 case DECBEF:
564 ntn = build_prepost_incdec(op, ln);
565 break;
566 case AMPER:
567 ntn = build_ampersand(ln, 0);
568 break;
569 case STAR:
570 ntn = new_tnode(STAR, ln->tn_type->t_subt, ln, NULL);
571 break;
572 case PLUS:
573 case MINUS:
574 ntn = build_plus_minus(op, ln, rn);
575 break;
576 case SHL:
577 case SHR:
578 ntn = build_bit_shift(op, ln, rn);
579 break;
580 case COLON:
581 ntn = build_colon(ln, rn);
582 break;
583 case ASSIGN:
584 case MULASS:
585 case DIVASS:
586 case MODASS:
587 case ADDASS:
588 case SUBASS:
589 case SHLASS:
590 case SHRASS:
591 case ANDASS:
592 case XORASS:
593 case ORASS:
594 case RETURN:
595 ntn = build_assignment(op, ln, rn);
596 break;
597 case COMMA:
598 case QUEST:
599 ntn = new_tnode(op, rn->tn_type, ln, rn);
600 break;
601 case REAL:
602 case IMAG:
603 ntn = build_real_imag(op, ln);
604 break;
605 default:
606 rtp = mp->m_returns_bool
607 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
608 lint_assert(mp->m_binary || rn == NULL);
609 ntn = new_tnode(op, rtp, ln, rn);
610 break;
611 }
612
613 /* Return if an error occurred. */
614 if (ntn == NULL)
615 return NULL;
616
617 /* Print a warning if precedence confusion is possible */
618 if (mp->m_possible_precedence_confusion)
619 check_precedence_confusion(ntn);
620
621 /*
622 * Print a warning if one of the operands is in a context where
623 * it is compared with null and if this operand is a constant.
624 */
625 if (mp->m_tctx) {
626 if (ln->tn_op == CON ||
627 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
628 if (hflag && !constcond_flag)
629 /* constant in conditional context */
630 warning(161);
631 }
632 }
633
634 /* Fold if the operator requires it */
635 if (mp->m_fold) {
636 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
637 if (mp->m_tctx) {
638 ntn = fold_test(ntn);
639 } else if (is_floating(ntn->tn_type->t_tspec)) {
640 ntn = fold_float(ntn);
641 } else {
642 ntn = fold(ntn);
643 }
644 } else if (op == QUEST && ln->tn_op == CON) {
645 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
646 }
647 }
648
649 return ntn;
650 }
651
652 /*
653 * Perform class conversions.
654 *
655 * Arrays of type T are converted into pointers to type T.
656 * Functions are converted to pointers to functions.
657 * Lvalues are converted to rvalues.
658 */
659 tnode_t *
660 cconv(tnode_t *tn)
661 {
662 type_t *tp;
663
664 /*
665 * Array-lvalue (array of type T) is converted into rvalue
666 * (pointer to type T)
667 */
668 if (tn->tn_type->t_tspec == ARRAY) {
669 if (!tn->tn_lvalue) {
670 /* XXX print correct operator */
671 /* %soperand of '%s' must be lvalue */
672 gnuism(114, "", modtab[AMPER].m_name);
673 }
674 tn = new_tnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
675 tn, NULL);
676 }
677
678 /*
679 * Expression of type function (function with return value of type T)
680 * in rvalue-expression (pointer to function with return value
681 * of type T)
682 */
683 if (tn->tn_type->t_tspec == FUNC)
684 tn = build_ampersand(tn, 1);
685
686 /* lvalue to rvalue */
687 if (tn->tn_lvalue) {
688 tp = tduptyp(tn->tn_type);
689 tp->t_const = tp->t_volatile = 0;
690 tn = new_tnode(LOAD, tp, tn, NULL);
691 }
692
693 return tn;
694 }
695
696 static const tnode_t *
697 before_conversion(const tnode_t *tn)
698 {
699 while (tn->tn_op == CVT && !tn->tn_cast)
700 tn = tn->tn_left;
701 return tn;
702 }
703
704 static bool
705 is_strict_bool_constant(const tnode_t *tn)
706 {
707 tspec_t t;
708
709 tn = before_conversion(tn);
710 t = tn->tn_type->t_tspec;
711
712 if (t == BOOL)
713 return true;
714
715 return t == INT && tn->tn_op == CON &&
716 (tn->tn_val->v_quad == 0 || tn->tn_val->v_quad == 1);
717 }
718
719 /* In strict bool mode, see if the node's type is compatible with bool. */
720 bool
721 is_strict_bool(const tnode_t *tn)
722 {
723 tspec_t t;
724
725 tn = before_conversion(tn);
726 t = tn->tn_type->t_tspec;
727
728 if (t == BOOL)
729 return true;
730
731 if (t == INT && tn->tn_op == CON &&
732 (tn->tn_val->v_quad == 0 || tn->tn_val->v_quad == 1))
733 return true;
734
735 /* For enums that are used as bit sets, allow "flags & FLAG". */
736 if (tn->tn_op == AND &&
737 tn->tn_left->tn_op == CVT &&
738 tn->tn_left->tn_type->t_tspec == INT && !tn->tn_left->tn_cast &&
739 tn->tn_left->tn_left->tn_type->t_tspec == ENUM &&
740 /*
741 * XXX: Somehow the type information got lost here. The type
742 * of the enum constant on the right-hand side should still be
743 * ENUM, but is INT.
744 */
745 tn->tn_right->tn_type->t_tspec == INT)
746 return true;
747
748 return false;
749 }
750
751 static bool
752 typeok_incdec(const mod_t *mp, const tnode_t *tn, const type_t *tp)
753 {
754 /* operand has scalar type (checked in typeok) */
755 if (!tn->tn_lvalue) {
756 if (tn->tn_op == CVT && tn->tn_cast &&
757 tn->tn_left->tn_op == LOAD) {
758 if (tn->tn_type->t_tspec == PTR)
759 return true;
760 /* a cast does not yield an lvalue */
761 error(163);
762 }
763 /* %soperand of '%s' must be lvalue */
764 error(114, "", mp->m_name);
765 return false;
766 } else if (tp->t_const) {
767 if (!tflag)
768 /* %soperand of '%s' must be modifiable ... */
769 warning(115, "", mp->m_name);
770 }
771 return true;
772 }
773
774 static bool
775 typeok_amper(const mod_t *mp,
776 const tnode_t *tn, const type_t *tp, tspec_t t)
777 {
778 if (t == ARRAY || t == FUNC) {
779 /* ok, a warning comes later (in build_ampersand()) */
780 } else if (!tn->tn_lvalue) {
781 if (tn->tn_op == CVT && tn->tn_cast &&
782 tn->tn_left->tn_op == LOAD) {
783 if (tn->tn_type->t_tspec == PTR)
784 return true;
785 /* a cast does not yield an lvalue */
786 error(163);
787 }
788 /* %soperand of '%s' must be lvalue */
789 error(114, "", mp->m_name);
790 return false;
791 } else if (is_scalar(t)) {
792 if (tp->t_bitfield) {
793 /* cannot take address of bit-field */
794 error(112);
795 return false;
796 }
797 } else if (t != STRUCT && t != UNION) {
798 /* unacceptable operand of '%s' */
799 error(111, mp->m_name);
800 return false;
801 }
802 if (tn->tn_op == NAME && tn->tn_sym->s_reg) {
803 /* cannot take address of register %s */
804 error(113, tn->tn_sym->s_name);
805 return false;
806 }
807 return true;
808 }
809
810 static bool
811 typeok_star(tspec_t t)
812 {
813 /* until now there were no type checks for this operator */
814 if (t != PTR) {
815 /* cannot dereference non-pointer type */
816 error(96);
817 return false;
818 }
819 return true;
820 }
821
822 static bool
823 typeok_plus(op_t op, tspec_t lt, tspec_t rt)
824 {
825 /* operands have scalar types (checked above) */
826 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
827 warn_incompatible_types(op, lt, rt);
828 return false;
829 }
830 return true;
831 }
832
833 static bool
834 typeok_minus(op_t op,
835 const type_t *ltp, tspec_t lt,
836 const type_t *rtp, tspec_t rt)
837 {
838 /* operands have scalar types (checked above) */
839 if (lt == PTR && (!is_integer(rt) && rt != PTR)) {
840 warn_incompatible_types(op, lt, rt);
841 return false;
842 } else if (rt == PTR && lt != PTR) {
843 warn_incompatible_types(op, lt, rt);
844 return false;
845 }
846 if (lt == PTR && rt == PTR) {
847 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
848 /* illegal pointer subtraction */
849 error(116);
850 }
851 }
852 return true;
853 }
854
855 static void
856 typeok_shr(const mod_t *mp,
857 const tnode_t *ln, tspec_t lt,
858 const tnode_t *rn, tspec_t rt)
859 {
860 tspec_t olt, ort;
861
862 olt = before_conversion(ln)->tn_type->t_tspec;
863 ort = before_conversion(rn)->tn_type->t_tspec;
864
865 /* operands have integer types (checked above) */
866 if (pflag && !is_uinteger(lt)) {
867 /*
868 * The left operand is signed. This means that
869 * the operation is (possibly) nonportable.
870 */
871 if (ln->tn_op != CON) {
872 /* bitop. on signed value possibly nonportable */
873 warning(117);
874 } else if (ln->tn_val->v_quad < 0) {
875 /* bitop. on signed value nonportable */
876 warning(120);
877 }
878 } else if (!tflag && !sflag && !is_uinteger(olt) && is_uinteger(ort)) {
879 /*
880 * The left operand would become unsigned in
881 * traditional C.
882 */
883 if (hflag &&
884 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
885 /* semantics of '%s' change in ANSI C; ... */
886 warning(118, mp->m_name);
887 }
888 } else if (!tflag && !sflag && !is_uinteger(olt) && !is_uinteger(ort) &&
889 psize(lt) < psize(rt)) {
890 /*
891 * In traditional C the left operand would be extended,
892 * possibly with 1, and then shifted.
893 */
894 if (hflag &&
895 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
896 /* semantics of '%s' change in ANSI C; use ... */
897 warning(118, mp->m_name);
898 }
899 }
900 }
901
902 static void
903 typeok_shl(const mod_t *mp, tspec_t lt, tspec_t rt) {
904 /*
905 * ANSI C does not perform balancing for shift operations,
906 * but traditional C does. If the width of the right operand
907 * is greater than the width of the left operand, than in
908 * traditional C the left operand would be extended to the
909 * width of the right operand. For SHL this may result in
910 * different results.
911 */
912 if (psize(lt) < psize(rt)) {
913 /*
914 * XXX If both operands are constant, make sure
915 * that there is really a difference between
916 * ANSI C and traditional C.
917 */
918 if (hflag)
919 /* semantics of '%s' change in ANSI C; use ... */
920 warning(118, mp->m_name);
921 }
922 }
923
924 static void
925 typeok_shift(tspec_t lt, const tnode_t *rn, tspec_t rt)
926 {
927 if (rn->tn_op == CON) {
928 if (!is_uinteger(rt) && rn->tn_val->v_quad < 0) {
929 /* negative shift */
930 warning(121);
931 } else if ((uint64_t)rn->tn_val->v_quad == (uint64_t)size(lt)) {
932 /* shift equal to size of object */
933 warning(267);
934 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size(lt)) {
935 /* shift greater than size of object */
936 warning(122);
937 }
938 }
939 }
940
941 static bool
942 typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
943 {
944 if (lt == PTR && ((rt == PTR && rn->tn_type->t_tspec == VOID) ||
945 is_integer(rt))) {
946 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
947 return true;
948 }
949 if (rt == PTR && ((lt == PTR && ln->tn_type->t_tspec == VOID) ||
950 is_integer(lt))) {
951 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
952 return true;
953 }
954 return false;
955 }
956
957 static bool
958 typeok_ordered_comparison(op_t op, const mod_t *mp,
959 const tnode_t *ln, const type_t *ltp, tspec_t lt,
960 const tnode_t *rn, const type_t *rtp, tspec_t rt)
961 {
962 if ((lt == PTR || rt == PTR) && lt != rt) {
963 if (is_integer(lt) || is_integer(rt)) {
964 const char *lx = lt == PTR ?
965 "pointer" : "integer";
966 const char *rx = rt == PTR ?
967 "pointer" : "integer";
968 /* illegal combination of %s (%s) and ... */
969 warning(123, lx, type_name(ltp),
970 rx, type_name(rtp), mp->m_name);
971 } else {
972 warn_incompatible_types(op, lt, rt);
973 return false;
974 }
975 } else if (lt == PTR && rt == PTR) {
976 check_pointer_comparison(op, ln, rn);
977 }
978 return true;
979 }
980
981 static bool
982 typeok_quest(tspec_t lt, const tnode_t **rn)
983 {
984 if (!is_scalar(lt)) {
985 /* first operand must have scalar type, op ? : */
986 error(170);
987 return false;
988 }
989 while ((*rn)->tn_op == CVT)
990 *rn = (*rn)->tn_left;
991 lint_assert((*rn)->tn_op == COLON);
992 return true;
993 }
994
995 static bool
996 typeok_colon(const mod_t *mp,
997 const tnode_t *ln, const type_t *ltp, tspec_t lt,
998 const tnode_t *rn, const type_t *rtp, tspec_t rt)
999 {
1000 type_t *lstp, *rstp;
1001 tspec_t lst, rst;
1002
1003 if (is_arithmetic(lt) && is_arithmetic(rt))
1004 return true;
1005
1006 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1007 return true;
1008 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1009 return true;
1010
1011 lstp = lt == PTR ? ltp->t_subt : NULL;
1012 rstp = rt == PTR ? rtp->t_subt : NULL;
1013 lst = lstp != NULL ? lstp->t_tspec : NOTSPEC;
1014 rst = rstp != NULL ? rstp->t_tspec : NOTSPEC;
1015
1016 /* combination of any pointer and 0, 0L or (void *)0 is ok */
1017 if (lt == PTR && ((rt == PTR && rst == VOID) || is_integer(rt))) {
1018 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1019 return true;
1020 }
1021 if (rt == PTR && ((lt == PTR && lst == VOID) || is_integer(lt))) {
1022 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1023 return true;
1024 }
1025
1026 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
1027 const char *lx = lt == PTR ? "pointer" : "integer";
1028 const char *rx = rt == PTR ? "pointer" : "integer";
1029 /* illegal combination of %s (%s) and %s (%s), op %s */
1030 warning(123, lx, type_name(ltp),
1031 rx, type_name(rtp), mp->m_name);
1032 return true;
1033 }
1034
1035 if (lt == VOID || rt == VOID) {
1036 if (lt != VOID || rt != VOID)
1037 /* incompatible types in conditional */
1038 warning(126);
1039 return true;
1040 }
1041
1042 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1043 (lst == FUNC && rst == VOID))) {
1044 /* (void *)0 handled above */
1045 if (sflag)
1046 /* ANSI C forbids conv. of %s to %s, op %s */
1047 warning(305, "function pointer", "'void *'",
1048 mp->m_name);
1049 return true;
1050 }
1051
1052 if (rt == PTR && lt == PTR) {
1053 if (eqptrtype(lstp, rstp, 1))
1054 return true;
1055 if (!eqtype(lstp, rstp, 1, 0, NULL))
1056 warn_incompatible_pointers(mp, ltp, rtp);
1057 return true;
1058 }
1059
1060 /* incompatible types in conditional */
1061 error(126);
1062 return false;
1063 }
1064
1065 static bool
1066 typeok_assign(const mod_t *mp, const tnode_t *ln, const type_t *ltp, tspec_t lt)
1067 {
1068 if (!ln->tn_lvalue) {
1069 if (ln->tn_op == CVT && ln->tn_cast &&
1070 ln->tn_left->tn_op == LOAD) {
1071 if (ln->tn_type->t_tspec == PTR)
1072 return true;
1073 /* a cast does not yield an lvalue */
1074 error(163);
1075 }
1076 /* %soperand of '%s' must be lvalue */
1077 error(114, "left ", mp->m_name);
1078 return false;
1079 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1080 has_constant_member(ltp))) {
1081 if (!tflag)
1082 /* %soperand of '%s' must be modifiable lvalue */
1083 warning(115, "left ", mp->m_name);
1084 }
1085 return true;
1086 }
1087
1088 /*
1089 * For assignment operators in strict bool mode, the type check is stricter
1090 * than for operators that compare to 0. Code that passes this strict check
1091 * can be compiled in a pre-C99 environment that doesn't implement the
1092 * special rule C99 6.3.1.2, without silent change in behavior.
1093 */
1094 static bool
1095 typeok_strict_bool_assign(op_t op, int arg,
1096 const tnode_t *ln, tspec_t lt,
1097 const tnode_t *rn, tspec_t rt)
1098 {
1099 if ((lt == BOOL) == (rt == BOOL))
1100 return true;
1101
1102 if (lt == BOOL && is_strict_bool_constant(rn))
1103 return true;
1104
1105 if (op == FARG) {
1106 /* argument #%d expects '%s', gets passed '%s' */
1107 error(334,
1108 arg,
1109 tspec_name(ln->tn_type->t_tspec),
1110 tspec_name(rn->tn_type->t_tspec));
1111 } else if (op == RETURN) {
1112 /* return value type mismatch (%s) and (%s) */
1113 error(211,
1114 type_name(ln->tn_type),
1115 type_name(rn->tn_type));
1116 return false;
1117 } else {
1118 /* operands of '%s' have incompatible types (%s != %s) */
1119 error(107,
1120 getopname(op),
1121 tspec_name(ln->tn_type->t_tspec),
1122 tspec_name(rn->tn_type->t_tspec));
1123 }
1124
1125 return false;
1126 }
1127
1128 /*
1129 * In strict bool mode, check whether the types of the operands match the
1130 * operator.
1131 */
1132 static bool
1133 typeok_scalar_strict_bool(op_t op, const mod_t *mp, int arg,
1134 const tnode_t *ln,
1135 const tnode_t *rn)
1136
1137 {
1138 tspec_t lt, rt;
1139
1140 ln = before_conversion(ln);
1141 lt = ln->tn_type->t_tspec;
1142
1143 if (rn != NULL) {
1144 rn = before_conversion(rn);
1145 rt = rn->tn_type->t_tspec;
1146 } else {
1147 rt = NOTSPEC;
1148 }
1149
1150 if (op == ASSIGN || op == FARG || op == RETURN || op == COLON)
1151 return typeok_strict_bool_assign(op, arg, ln, lt, rn, rt);
1152
1153 if (mp->m_takes_only_bool || op == QUEST) {
1154 bool binary = mp->m_binary;
1155 bool lbool = is_strict_bool(ln);
1156 bool ok = true;
1157
1158 if (!binary && !lbool) {
1159 /* operand of '%s' must be bool, not '%s' */
1160 error(330, getopname(op), tspec_name(lt));
1161 ok = false;
1162 }
1163 if (binary && !lbool) {
1164 /* left operand of '%s' must be bool, not '%s' */
1165 error(331, getopname(op), tspec_name(lt));
1166 ok = false;
1167 }
1168 if (binary && op != QUEST && !is_strict_bool(rn)) {
1169 /* right operand of '%s' must be bool, not '%s' */
1170 error(332, getopname(op), tspec_name(rt));
1171 ok = false;
1172 }
1173 return ok;
1174 }
1175
1176 if (!mp->m_takes_bool) {
1177 bool binary = mp->m_binary;
1178 bool lbool = before_conversion(ln)->tn_type->t_tspec == BOOL;
1179 bool ok = true;
1180
1181 if (!binary && lbool) {
1182 /* operand of '%s' must not be bool */
1183 error(335, getopname(op));
1184 ok = false;
1185 }
1186 if (binary && lbool) {
1187 /* left operand of '%s' must not be bool */
1188 error(336, getopname(op));
1189 ok = false;
1190 }
1191 if (binary && before_conversion(rn)->tn_type->t_tspec == BOOL) {
1192 /* right operand of '%s' must not be bool */
1193 error(337, getopname(op));
1194 ok = false;
1195 }
1196 return ok;
1197 }
1198
1199 return true;
1200 }
1201
1202 /* Check the types using the information from modtab[]. */
1203 static bool
1204 typeok_scalar(op_t op, const mod_t *mp,
1205 const tnode_t *ln, tspec_t lt,
1206 const tnode_t *rn, tspec_t rt)
1207 {
1208 if (mp->m_requires_integer) {
1209 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
1210 warn_incompatible_types(op, lt, rt);
1211 return false;
1212 }
1213 } else if (mp->m_requires_integer_or_complex) {
1214 if ((!is_integer(lt) && !is_complex(lt)) ||
1215 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
1216 warn_incompatible_types(op, lt, rt);
1217 return false;
1218 }
1219 } else if (mp->m_requires_scalar) {
1220 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
1221 warn_incompatible_types(op, lt, rt);
1222 return false;
1223 }
1224 } else if (mp->m_requires_arith) {
1225 if (!is_arithmetic(lt) ||
1226 (mp->m_binary && !is_arithmetic(rt))) {
1227 warn_incompatible_types(op, lt, rt);
1228 return false;
1229 }
1230 }
1231 return true;
1232 }
1233
1234 /* Check the types for specific operators and type combinations. */
1235 static bool
1236 typeok_op(op_t op, const mod_t *mp, int arg,
1237 const tnode_t *ln, const type_t *ltp, tspec_t lt,
1238 const tnode_t *rn, const type_t *rtp, tspec_t rt)
1239 {
1240 switch (op) {
1241 case POINT:
1242 /*
1243 * Most errors required by ANSI C are reported in
1244 * struct_or_union_member().
1245 * Here we only must check for totally wrong things.
1246 */
1247 if (lt == FUNC || lt == VOID || ltp->t_bitfield ||
1248 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
1249 /* Without tflag we got already an error */
1250 if (tflag)
1251 /* unacceptable operand of '%s' */
1252 error(111, mp->m_name);
1253 return false;
1254 }
1255 /* Now we have an object we can create a pointer to */
1256 break;
1257 case ARROW:
1258 if (lt != PTR && !(tflag && is_integer(lt))) {
1259 /* Without tflag we got already an error */
1260 if (tflag)
1261 /* unacceptable operand of '%s' */
1262 error(111, mp->m_name);
1263 return false;
1264 }
1265 break;
1266 case INCAFT:
1267 case DECAFT:
1268 case INCBEF:
1269 case DECBEF:
1270 if (!typeok_incdec(mp, ln, ltp))
1271 return false;
1272 break;
1273 case AMPER:
1274 if (!typeok_amper(mp, ln, ltp, lt))
1275 return false;
1276 break;
1277 case STAR:
1278 if (!typeok_star(lt))
1279 return false;
1280 break;
1281 case PLUS:
1282 if (!typeok_plus(op, lt, rt))
1283 return false;
1284 break;
1285 case MINUS:
1286 if (!typeok_minus(op, ltp, lt, rtp, rt))
1287 return false;
1288 break;
1289 case SHR:
1290 typeok_shr(mp, ln, lt, rn, rt);
1291 goto shift;
1292 case SHL:
1293 typeok_shl(mp, lt, rt);
1294 shift:
1295 typeok_shift(lt, rn, rt);
1296 break;
1297 case EQ:
1298 case NE:
1299 /*
1300 * Accept some things which are allowed with EQ and NE,
1301 * but not with ordered comparisons.
1302 */
1303 if (typeok_eq(ln, lt, rn, rt))
1304 break;
1305 /* FALLTHROUGH */
1306 case LT:
1307 case GT:
1308 case LE:
1309 case GE:
1310 if (!typeok_ordered_comparison(op, mp,
1311 ln, ltp, lt, rn, rtp, rt))
1312 return false;
1313 break;
1314 case QUEST:
1315 if (!typeok_quest(lt, &rn))
1316 return false;
1317 break;
1318 case COLON:
1319 if (!typeok_colon(mp, ln, ltp, lt, rn, rtp, rt))
1320 return false;
1321 break;
1322 case ASSIGN:
1323 case INIT:
1324 case FARG:
1325 case RETURN:
1326 if (!check_assign_types_compatible(op, arg, ln, rn))
1327 return false;
1328 goto assign;
1329 case MULASS:
1330 case DIVASS:
1331 case MODASS:
1332 goto assign;
1333 case ADDASS:
1334 case SUBASS:
1335 /* operands have scalar types (checked above) */
1336 if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
1337 warn_incompatible_types(op, lt, rt);
1338 return false;
1339 }
1340 goto assign;
1341 case SHLASS:
1342 goto assign;
1343 case SHRASS:
1344 if (pflag && !is_uinteger(lt) && !(tflag && is_uinteger(rt))) {
1345 /* bitop. on signed value possibly nonportable */
1346 warning(117);
1347 }
1348 goto assign;
1349 case ANDASS:
1350 case XORASS:
1351 case ORASS:
1352 goto assign;
1353 assign:
1354 if (!typeok_assign(mp, ln, ltp, lt))
1355 return false;
1356 break;
1357 case COMMA:
1358 if (!modtab[ln->tn_op].m_sideeff)
1359 check_null_effect(ln);
1360 break;
1361 /* LINTED206: (enumeration values not handled in switch) */
1362 case CON:
1363 case CASE:
1364 case PUSH:
1365 case LOAD:
1366 case ICALL:
1367 case CVT:
1368 case CALL:
1369 case FSEL:
1370 case STRING:
1371 case NAME:
1372 case LOGOR:
1373 case LOGAND:
1374 case OR:
1375 case XOR:
1376 case AND:
1377 case MOD:
1378 case DIV:
1379 case MULT:
1380 case UMINUS:
1381 case UPLUS:
1382 case DEC:
1383 case INC:
1384 case COMPL:
1385 case NOT:
1386 case NOOP:
1387 case REAL:
1388 case IMAG:
1389 break;
1390 }
1391 return true;
1392 }
1393
1394 static void
1395 typeok_enum(op_t op, const mod_t *mp, int arg,
1396 const tnode_t *ln, const type_t *ltp,
1397 const tnode_t *rn, const type_t *rtp)
1398 {
1399 if (mp->m_bad_on_enum &&
1400 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1401 check_bad_enum_operation(op, ln, rn);
1402 } else if (mp->m_valid_on_enum &&
1403 (ltp->t_isenum && rtp && rtp->t_isenum)) {
1404 check_enum_type_mismatch(op, arg, ln, rn);
1405 } else if (mp->m_valid_on_enum &&
1406 (ltp->t_isenum || (rtp && rtp->t_isenum))) {
1407 check_enum_int_mismatch(op, arg, ln, rn);
1408 }
1409 }
1410
1411 /* Perform most type checks. Return whether the types are ok. */
1412 bool
1413 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1414 {
1415 mod_t *mp;
1416 tspec_t lt, rt;
1417 type_t *ltp, *rtp;
1418
1419 mp = &modtab[op];
1420
1421 lint_assert((ltp = ln->tn_type) != NULL);
1422 lt = ltp->t_tspec;
1423
1424 if (mp->m_binary) {
1425 lint_assert((rtp = rn->tn_type) != NULL);
1426 rt = rtp->t_tspec;
1427 } else {
1428 rtp = NULL;
1429 rt = NOTSPEC;
1430 }
1431
1432 if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
1433 return false;
1434 if (!typeok_scalar(op, mp, ln, lt, rn, rt))
1435 return false;
1436
1437 if (!typeok_op(op, mp, arg, ln, ltp, lt, rn, rtp, rt))
1438 return false;
1439
1440 typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
1441 return true;
1442 }
1443
1444 static void
1445 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
1446 {
1447 type_t *ltp, *rtp;
1448 tspec_t lt, rt;
1449 const char *lts, *rts;
1450
1451 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1452 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1453
1454 if (lt == VOID || rt == VOID) {
1455 if (sflag && (lt == FUNC || rt == FUNC)) {
1456 /* (void *)0 already handled in typeok() */
1457 *(lt == FUNC ? <s : &rts) = "function pointer";
1458 *(lt == VOID ? <s : &rts) = "'void *'";
1459 /* ANSI C forbids comparison of %s with %s */
1460 warning(274, lts, rts);
1461 }
1462 return;
1463 }
1464
1465 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1466 warn_incompatible_pointers(&modtab[op], ltp, rtp);
1467 return;
1468 }
1469
1470 if (lt == FUNC && rt == FUNC) {
1471 if (sflag && op != EQ && op != NE)
1472 /* ANSI C forbids ordered comparisons of ... */
1473 warning(125);
1474 }
1475 }
1476
1477 /*
1478 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1479 * and prints warnings/errors if necessary.
1480 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1481 */
1482 static int
1483 check_assign_types_compatible(op_t op, int arg,
1484 const tnode_t *ln, const tnode_t *rn)
1485 {
1486 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
1487 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
1488 mod_t *mp;
1489 const char *lts, *rts;
1490
1491 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1492 lst = (lstp = ltp->t_subt)->t_tspec;
1493 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1494 rst = (rstp = rtp->t_subt)->t_tspec;
1495 mp = &modtab[op];
1496
1497 if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */
1498 return 1;
1499
1500 if (is_arithmetic(lt) && is_arithmetic(rt))
1501 return 1;
1502
1503 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1504 /* both are struct or union */
1505 return ltp->t_str == rtp->t_str;
1506
1507 /* 0, 0L and (void *)0 may be assigned to any pointer */
1508 if (lt == PTR && ((rt == PTR && rst == VOID) || is_integer(rt))) {
1509 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1510 return 1;
1511 }
1512
1513 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1514 /* two pointers, at least one pointer to void */
1515 if (sflag && (lst == FUNC || rst == FUNC)) {
1516 /* comb. of ptr to func and ptr to void */
1517 *(lst == FUNC ? <s : &rts) = "function pointer";
1518 *(lst == VOID ? <s : &rts) = "'void *'";
1519 switch (op) {
1520 case INIT:
1521 case RETURN:
1522 /* ANSI C forbids conversion of %s to %s */
1523 warning(303, rts, lts);
1524 break;
1525 case FARG:
1526 /* ANSI C forbids conv. of %s to %s, arg #%d */
1527 warning(304, rts, lts, arg);
1528 break;
1529 default:
1530 /* ANSI C forbids conv. of %s to %s, op %s */
1531 warning(305, rts, lts, mp->m_name);
1532 break;
1533 }
1534 }
1535 }
1536
1537 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1538 eqtype(lstp, rstp, 1, 0, NULL))) {
1539 /* compatible pointer types (qualifiers ignored) */
1540 if (!tflag &&
1541 ((!lstp->t_const && rstp->t_const) ||
1542 (!lstp->t_volatile && rstp->t_volatile))) {
1543 /* left side has not all qualifiers of right */
1544 switch (op) {
1545 case INIT:
1546 case RETURN:
1547 /* incompatible pointer types (%s != %s) */
1548 warning(182, type_name(lstp), type_name(rstp));
1549 break;
1550 case FARG:
1551 /* argument has incompatible pointer type... */
1552 warning(153,
1553 arg, type_name(lstp), type_name(rstp));
1554 break;
1555 default:
1556 /* operands have incompatible pointer type... */
1557 warning(128, mp->m_name,
1558 type_name(lstp), type_name(rstp));
1559 break;
1560 }
1561 }
1562 return 1;
1563 }
1564
1565 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
1566 const char *lx = lt == PTR ? "pointer" : "integer";
1567 const char *rx = rt == PTR ? "pointer" : "integer";
1568
1569 switch (op) {
1570 case INIT:
1571 case RETURN:
1572 /* illegal combination of %s (%s) and %s (%s) */
1573 warning(183, lx, type_name(ltp), rx, type_name(rtp));
1574 break;
1575 case FARG:
1576 /* illegal comb. of %s (%s) and %s (%s), arg #%d */
1577 warning(154,
1578 lx, type_name(ltp), rx, type_name(rtp), arg);
1579 break;
1580 default:
1581 /* illegal combination of %s (%s) and %s (%s), op %s */
1582 warning(123,
1583 lx, type_name(ltp), rx, type_name(rtp), mp->m_name);
1584 break;
1585 }
1586 return 1;
1587 }
1588
1589 if (lt == PTR && rt == PTR) {
1590 switch (op) {
1591 case INIT:
1592 case RETURN:
1593 warn_incompatible_pointers(NULL, ltp, rtp);
1594 break;
1595 case FARG:
1596 /* arg. has incomp. pointer type, arg #%d (%s != %s) */
1597 warning(153, arg, type_name(ltp), type_name(rtp));
1598 break;
1599 default:
1600 warn_incompatible_pointers(mp, ltp, rtp);
1601 break;
1602 }
1603 return 1;
1604 }
1605
1606 switch (op) {
1607 case INIT:
1608 /* initialisation type mismatch (%s) and (%s) */
1609 error(185, type_name(ltp), type_name(rtp));
1610 break;
1611 case RETURN:
1612 /* return value type mismatch (%s) and (%s) */
1613 error(211, type_name(ltp), type_name(rtp));
1614 break;
1615 case FARG:
1616 /* argument is incompatible with prototype, arg #%d */
1617 warning(155, arg);
1618 break;
1619 default:
1620 warn_incompatible_types(op, lt, rt);
1621 break;
1622 }
1623
1624 return 0;
1625 }
1626
1627 /*
1628 * Prints a warning if an operator, which should be senseless for an
1629 * enum type, is applied to an enum type.
1630 */
1631 static void
1632 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
1633 {
1634 mod_t *mp;
1635
1636 if (!eflag)
1637 return;
1638
1639 mp = &modtab[op];
1640
1641 if (!(ln->tn_type->t_isenum ||
1642 (mp->m_binary && rn->tn_type->t_isenum))) {
1643 return;
1644 }
1645
1646 /*
1647 * Enum as offset to a pointer is an exception (otherwise enums
1648 * could not be used as array indices).
1649 */
1650 if (op == PLUS &&
1651 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1652 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1653 return;
1654 }
1655
1656 /* dubious operation on enum, op %s */
1657 warning(241, mp->m_name);
1658
1659 }
1660
1661 /*
1662 * Prints a warning if an operator is applied to two different enum types.
1663 */
1664 static void
1665 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1666 {
1667 mod_t *mp;
1668
1669 mp = &modtab[op];
1670
1671 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1672 switch (op) {
1673 case INIT:
1674 /* enum type mismatch in initialisation */
1675 warning(210);
1676 break;
1677 case FARG:
1678 /* enum type mismatch, arg #%d (%s != %s) */
1679 warning(156, arg,
1680 type_name(ln->tn_type), type_name(rn->tn_type));
1681 break;
1682 case RETURN:
1683 /* return value type mismatch (%s) and (%s) */
1684 warning(211,
1685 type_name(ln->tn_type), type_name(rn->tn_type));
1686 break;
1687 default:
1688 /* enum type mismatch, op %s */
1689 warning(130, mp->m_name);
1690 break;
1691 }
1692 } else if (Pflag && mp->m_comp && op != EQ && op != NE) {
1693 if (eflag)
1694 /* dubious comparison of enums, op %s */
1695 warning(243, mp->m_name);
1696 }
1697 }
1698
1699 /*
1700 * Prints a warning if an operator has both enum and other integer
1701 * types.
1702 */
1703 static void
1704 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1705 {
1706
1707 if (!eflag)
1708 return;
1709
1710 switch (op) {
1711 case INIT:
1712 /*
1713 * Initializations with 0 should be allowed. Otherwise,
1714 * we should complain about all uninitialized enums,
1715 * consequently.
1716 */
1717 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1718 is_integer(rn->tn_type->t_tspec) &&
1719 rn->tn_val->v_quad == 0) {
1720 return;
1721 }
1722 /* initialisation of '%s' with '%s' */
1723 warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
1724 break;
1725 case FARG:
1726 /* combination of '%s' and '%s', arg #%d */
1727 warning(278,
1728 type_name(ln->tn_type), type_name(rn->tn_type), arg);
1729 break;
1730 case RETURN:
1731 /* combination of '%s' and '%s' in return */
1732 warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
1733 break;
1734 default:
1735 /* combination of '%s' and '%s', op %s */
1736 warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
1737 modtab[op].m_name);
1738 break;
1739 }
1740 }
1741
1742 /*
1743 * Build and initialize a new node.
1744 */
1745 static tnode_t *
1746 new_tnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
1747 {
1748 tnode_t *ntn;
1749 tspec_t t;
1750 #ifdef notyet
1751 size_t l;
1752 uint64_t rnum;
1753 #endif
1754
1755 ntn = getnode();
1756
1757 ntn->tn_op = op;
1758 ntn->tn_type = type;
1759 ntn->tn_left = ln;
1760 ntn->tn_right = rn;
1761
1762 switch (op) {
1763 #ifdef notyet
1764 case SHR:
1765 if (rn->tn_op != CON)
1766 break;
1767 rnum = rn->tn_val->v_quad;
1768 l = tsize(ln->tn_type) / CHAR_SIZE;
1769 t = ln->tn_type->t_tspec;
1770 switch (l) {
1771 case 8:
1772 if (rnum >= 56)
1773 t = UCHAR;
1774 else if (rnum >= 48)
1775 t = USHORT;
1776 else if (rnum >= 32)
1777 t = UINT;
1778 break;
1779 case 4:
1780 if (rnum >= 24)
1781 t = UCHAR;
1782 else if (rnum >= 16)
1783 t = USHORT;
1784 break;
1785 case 2:
1786 if (rnum >= 8)
1787 t = UCHAR;
1788 break;
1789 default:
1790 break;
1791 }
1792 if (t != ln->tn_type->t_tspec)
1793 ntn->tn_type->t_tspec = t;
1794 break;
1795 #endif
1796 case STAR:
1797 case FSEL:
1798 lint_assert(ln->tn_type->t_tspec == PTR);
1799 t = ln->tn_type->t_subt->t_tspec;
1800 if (t != FUNC && t != VOID)
1801 ntn->tn_lvalue = 1;
1802 break;
1803 default:
1804 break;
1805 }
1806
1807 return ntn;
1808 }
1809
1810 /*
1811 * Performs usual conversion of operands to (unsigned) int.
1812 *
1813 * If tflag is set or the operand is a function argument with no
1814 * type information (no prototype or variable # of args), convert
1815 * float to double.
1816 */
1817 tnode_t *
1818 promote(op_t op, int farg, tnode_t *tn)
1819 {
1820 tspec_t t;
1821 type_t *ntp;
1822 u_int len;
1823
1824 t = tn->tn_type->t_tspec;
1825
1826 if (!is_arithmetic(t))
1827 return tn;
1828
1829 if (!tflag) {
1830 /*
1831 * ANSI C requires that the result is always of type INT
1832 * if INT can represent all possible values of the previous
1833 * type.
1834 */
1835 if (tn->tn_type->t_bitfield) {
1836 len = tn->tn_type->t_flen;
1837 if (size(INT) > len) {
1838 t = INT;
1839 } else {
1840 lint_assert(len == size(INT));
1841 if (is_uinteger(t)) {
1842 t = UINT;
1843 } else {
1844 t = INT;
1845 }
1846 }
1847 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1848 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1849 INT : UINT;
1850 } else if (t == SHORT || t == USHORT) {
1851 t = (size(SHORT) < size(INT) || t == SHORT) ?
1852 INT : UINT;
1853 } else if (t == ENUM) {
1854 t = INT;
1855 } else if (farg && t == FLOAT) {
1856 t = DOUBLE;
1857 }
1858 } else {
1859 /*
1860 * In traditional C, keep unsigned and promote FLOAT
1861 * to DOUBLE.
1862 */
1863 if (t == UCHAR || t == USHORT) {
1864 t = UINT;
1865 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1866 t = INT;
1867 } else if (t == FLOAT) {
1868 t = DOUBLE;
1869 } else if (t == ENUM) {
1870 t = INT;
1871 }
1872 }
1873
1874 if (t != tn->tn_type->t_tspec) {
1875 ntp = tduptyp(tn->tn_type);
1876 ntp->t_tspec = t;
1877 /*
1878 * Keep t_isenum so we are later able to check compatibility
1879 * of enum types.
1880 */
1881 tn = convert(op, 0, ntp, tn);
1882 }
1883
1884 return tn;
1885 }
1886
1887 /*
1888 * Insert conversions which are necessary to give both operands the same
1889 * type. This is done in different ways for traditional C and ANIS C.
1890 */
1891 static void
1892 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1893 {
1894 tspec_t lt, rt, t;
1895 int i, u;
1896 type_t *ntp;
1897 static tspec_t tl[] = {
1898 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1899 };
1900
1901 lt = (*lnp)->tn_type->t_tspec;
1902 rt = (*rnp)->tn_type->t_tspec;
1903
1904 if (!is_arithmetic(lt) || !is_arithmetic(rt))
1905 return;
1906
1907 if (!tflag) {
1908 if (lt == rt) {
1909 t = lt;
1910 } else if (lt == LCOMPLEX || rt == LCOMPLEX) {
1911 t = LCOMPLEX;
1912 } else if (lt == DCOMPLEX || rt == DCOMPLEX) {
1913 t = DCOMPLEX;
1914 } else if (lt == COMPLEX || rt == COMPLEX) {
1915 t = COMPLEX;
1916 } else if (lt == FCOMPLEX || rt == FCOMPLEX) {
1917 t = FCOMPLEX;
1918 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1919 t = LDOUBLE;
1920 } else if (lt == DOUBLE || rt == DOUBLE) {
1921 t = DOUBLE;
1922 } else if (lt == FLOAT || rt == FLOAT) {
1923 t = FLOAT;
1924 } else {
1925 /*
1926 * If type A has more bits than type B it should
1927 * be able to hold all possible values of type B.
1928 */
1929 if (size(lt) > size(rt)) {
1930 t = lt;
1931 } else if (size(lt) < size(rt)) {
1932 t = rt;
1933 } else {
1934 for (i = 3; tl[i] != INT; i++) {
1935 if (tl[i] == lt || tl[i] == rt)
1936 break;
1937 }
1938 if ((is_uinteger(lt) || is_uinteger(rt)) &&
1939 !is_uinteger(tl[i])) {
1940 i--;
1941 }
1942 t = tl[i];
1943 }
1944 }
1945 } else {
1946 /* Keep unsigned in traditional C */
1947 u = is_uinteger(lt) || is_uinteger(rt);
1948 for (i = 0; tl[i] != INT; i++) {
1949 if (lt == tl[i] || rt == tl[i])
1950 break;
1951 }
1952 t = tl[i];
1953 if (u && is_integer(t) && !is_uinteger(t))
1954 t = unsigned_type(t);
1955 }
1956
1957 if (t != lt) {
1958 ntp = tduptyp((*lnp)->tn_type);
1959 ntp->t_tspec = t;
1960 *lnp = convert(op, 0, ntp, *lnp);
1961 }
1962 if (t != rt) {
1963 ntp = tduptyp((*rnp)->tn_type);
1964 ntp->t_tspec = t;
1965 *rnp = convert(op, 0, ntp, *rnp);
1966 }
1967 }
1968
1969 /*
1970 * Insert a conversion operator, which converts the type of the node
1971 * to another given type.
1972 * If op is FARG, arg is the number of the argument (used for warnings).
1973 */
1974 tnode_t *
1975 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1976 {
1977 tnode_t *ntn;
1978 tspec_t nt, ot, ost = NOTSPEC;
1979
1980 nt = tp->t_tspec;
1981 if ((ot = tn->tn_type->t_tspec) == PTR)
1982 ost = tn->tn_type->t_subt->t_tspec;
1983
1984 if (!tflag && !sflag && op == FARG)
1985 check_prototype_conversion(arg, nt, ot, tp, tn);
1986 if (is_integer(nt) && is_integer(ot)) {
1987 check_integer_conversion(op, arg, nt, ot, tp, tn);
1988 } else if (nt == PTR && ((ot == PTR && ost == VOID) ||
1989 is_integer(ot)) && tn->tn_op == CON &&
1990 tn->tn_val->v_quad == 0) {
1991 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1992 } else if (is_integer(nt) && nt != BOOL && ot == PTR) {
1993 check_pointer_integer_conversion(op, nt, tp, tn);
1994 } else if (nt == PTR && ot == PTR) {
1995 check_pointer_conversion(op, tn, tp);
1996 }
1997
1998 ntn = getnode();
1999 ntn->tn_op = CVT;
2000 ntn->tn_type = tp;
2001 ntn->tn_cast = op == CVT;
2002 ntn->tn_right = NULL;
2003 if (tn->tn_op != CON || nt == VOID) {
2004 ntn->tn_left = tn;
2005 } else {
2006 ntn->tn_op = CON;
2007 ntn->tn_val = tgetblk(sizeof (val_t));
2008 convert_constant(op, arg, ntn->tn_type, ntn->tn_val,
2009 tn->tn_val);
2010 }
2011
2012 return ntn;
2013 }
2014
2015 /*
2016 * Print a warning if a prototype causes a type conversion that is
2017 * different from what would happen to the same argument in the
2018 * absence of a prototype.
2019 *
2020 * Errors/Warnings about illegal type combinations are already printed
2021 * in check_assign_types_compatible().
2022 */
2023 static void
2024 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
2025 tnode_t *tn)
2026 {
2027 tnode_t *ptn;
2028
2029 if (!is_arithmetic(nt) || !is_arithmetic(ot))
2030 return;
2031
2032 /*
2033 * If the type of the formal parameter is char/short, a warning
2034 * would be useless, because functions declared the old style
2035 * can't expect char/short arguments.
2036 */
2037 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
2038 return;
2039
2040 /* get default promotion */
2041 ptn = promote(NOOP, 1, tn);
2042 ot = ptn->tn_type->t_tspec;
2043
2044 /* return if types are the same with and without prototype */
2045 if (nt == ot || (nt == ENUM && ot == INT))
2046 return;
2047
2048 if (is_floating(nt) != is_floating(ot) ||
2049 psize(nt) != psize(ot)) {
2050 /* representation and/or width change */
2051 if (!is_integer(ot) || psize(ot) > psize(INT)) {
2052 /* conversion to '%s' due to prototype, arg #%d */
2053 warning(259, type_name(tp), arg);
2054 }
2055 } else if (hflag) {
2056 /*
2057 * they differ in sign or base type (char, short, int,
2058 * long, long long, float, double, long double)
2059 *
2060 * if they differ only in sign and the argument is a constant
2061 * and the msb of the argument is not set, print no warning
2062 */
2063 if (ptn->tn_op == CON && is_integer(nt) &&
2064 signed_type(nt) == signed_type(ot) &&
2065 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
2066 /* ok */
2067 } else {
2068 /* conversion to '%s' due to prototype, arg #%d */
2069 warning(259, type_name(tp), arg);
2070 }
2071 }
2072 }
2073
2074 /*
2075 * Print warnings for conversions of integer types which may cause problems.
2076 */
2077 /* ARGSUSED */
2078 static void
2079 check_integer_conversion(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp,
2080 tnode_t *tn)
2081 {
2082 char opbuf[16];
2083
2084 if (tn->tn_op == CON)
2085 return;
2086
2087 if (op == CVT)
2088 return;
2089
2090 if (Pflag && psize(nt) > psize(ot) &&
2091 is_uinteger(nt) != is_uinteger(ot)) {
2092 if (aflag && pflag) {
2093 if (op == FARG) {
2094 /* conversion to '%s' may sign-extend ... */
2095 warning(297, type_name(tp), arg);
2096 } else {
2097 /* conversion to '%s' may sign-extend ... */
2098 warning(131, type_name(tp));
2099 }
2100 }
2101 }
2102
2103 if (Pflag && psize(nt) > psize(ot)) {
2104 switch (tn->tn_op) {
2105 case PLUS:
2106 case MINUS:
2107 case MULT:
2108 case SHL:
2109 /* suggest cast from '%s' to '%s' on op %s to ... */
2110 warning(324, type_name(gettyp(ot)), type_name(tp),
2111 print_tnode(opbuf, sizeof(opbuf), tn));
2112 break;
2113 default:
2114 break;
2115 }
2116 }
2117
2118 if (psize(nt) < psize(ot) &&
2119 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
2120 aflag > 1)) {
2121 /* conversion from '%s' may lose accuracy */
2122 if (aflag) {
2123 if (op == FARG) {
2124 /* conv. from '%s' to '%s' may lose ... */
2125 warning(298,
2126 type_name(tn->tn_type), type_name(tp), arg);
2127 } else {
2128 /* conv. from '%s' to '%s' may lose accuracy */
2129 warning(132,
2130 type_name(tn->tn_type), type_name(tp));
2131 }
2132 }
2133 }
2134 }
2135
2136 /*
2137 * Print warnings for dubious conversions of pointer to integer.
2138 */
2139 static void
2140 check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
2141 {
2142
2143 if (tn->tn_op == CON)
2144 return;
2145
2146 if (op != CVT) {
2147 /* We got already an error. */
2148 return;
2149 }
2150
2151 if (psize(nt) < psize(PTR)) {
2152 if (pflag && size(nt) >= size(PTR)) {
2153 /* conversion of pointer to '%s' may lose bits */
2154 warning(134, type_name(tp));
2155 } else {
2156 /* conversion of pointer to '%s' loses bits */
2157 warning(133, type_name(tp));
2158 }
2159 }
2160 }
2161
2162 /*
2163 * Print warnings for questionable pointer conversions.
2164 */
2165 static void
2166 check_pointer_conversion(op_t op, tnode_t *tn, type_t *tp)
2167 {
2168 tspec_t nt, ot;
2169 const char *nts, *ots;
2170
2171 /*
2172 * We got already an error (pointers of different types
2173 * without a cast) or we will not get a warning.
2174 */
2175 if (op != CVT)
2176 return;
2177
2178 nt = tp->t_subt->t_tspec;
2179 ot = tn->tn_type->t_subt->t_tspec;
2180
2181 if (nt == VOID || ot == VOID) {
2182 if (sflag && (nt == FUNC || ot == FUNC)) {
2183 /* (void *)0 already handled in convert() */
2184 *(nt == FUNC ? &nts : &ots) = "function pointer";
2185 *(nt == VOID ? &nts : &ots) = "'void *'";
2186 /* ANSI C forbids conversion of %s to %s */
2187 warning(303, ots, nts);
2188 }
2189 return;
2190 } else if (nt == FUNC && ot == FUNC) {
2191 return;
2192 } else if (nt == FUNC || ot == FUNC) {
2193 /* questionable conversion of function pointer */
2194 warning(229);
2195 return;
2196 }
2197
2198 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
2199 if (hflag)
2200 /* possible pointer alignment problem */
2201 warning(135);
2202 }
2203 if (((nt == STRUCT || nt == UNION) &&
2204 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
2205 psize(nt) != psize(ot)) {
2206 if (cflag) {
2207 /* pointer casts may be troublesome */
2208 warning(247);
2209 }
2210 }
2211 }
2212
2213 /*
2214 * Converts a typed constant in a constant of another type.
2215 *
2216 * op operator which requires conversion
2217 * arg if op is FARG, # of argument
2218 * tp type in which to convert the constant
2219 * nv new constant
2220 * v old constant
2221 */
2222 void
2223 convert_constant(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
2224 {
2225 tspec_t ot, nt;
2226 ldbl_t max = 0.0, min = 0.0;
2227 int sz, rchk;
2228 int64_t xmask, xmsk1;
2229 int osz, nsz;
2230
2231 ot = v->v_tspec;
2232 nt = nv->v_tspec = tp->t_tspec;
2233 rchk = 0;
2234
2235 if (nt == BOOL) { /* C99 6.3.1.2 */
2236 nv->v_ansiu = 0;
2237 nv->v_quad = ot == FLOAT || ot == DOUBLE || ot == LDOUBLE
2238 ? v->v_ldbl != 0.0
2239 : v->v_quad != 0;
2240 return;
2241 }
2242
2243 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
2244 switch (nt) {
2245 case CHAR:
2246 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
2247 case UCHAR:
2248 max = TARG_UCHAR_MAX; min = 0; break;
2249 case SCHAR:
2250 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
2251 case SHORT:
2252 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
2253 case USHORT:
2254 max = TARG_USHRT_MAX; min = 0; break;
2255 case ENUM:
2256 case INT:
2257 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
2258 case UINT:
2259 max = (u_int)TARG_UINT_MAX;min = 0; break;
2260 case LONG:
2261 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
2262 case ULONG:
2263 max = (u_long)TARG_ULONG_MAX; min = 0; break;
2264 case QUAD:
2265 max = QUAD_MAX; min = QUAD_MIN; break;
2266 case UQUAD:
2267 max = (uint64_t)UQUAD_MAX; min = 0; break;
2268 case FLOAT:
2269 case FCOMPLEX:
2270 max = FLT_MAX; min = -FLT_MAX; break;
2271 case DOUBLE:
2272 case DCOMPLEX:
2273 max = DBL_MAX; min = -DBL_MAX; break;
2274 case PTR:
2275 /* Got already an error because of float --> ptr */
2276 case LDOUBLE:
2277 case LCOMPLEX:
2278 max = LDBL_MAX; min = -LDBL_MAX; break;
2279 default:
2280 lint_assert(/*CONSTCOND*/0);
2281 }
2282 if (v->v_ldbl > max || v->v_ldbl < min) {
2283 lint_assert(nt != LDOUBLE);
2284 if (op == FARG) {
2285 /* conv. of '%s' to '%s' is out of range, ... */
2286 warning(295,
2287 type_name(gettyp(ot)), type_name(tp), arg);
2288 } else {
2289 /* conversion of '%s' to '%s' is out of range */
2290 warning(119,
2291 type_name(gettyp(ot)), type_name(tp));
2292 }
2293 v->v_ldbl = v->v_ldbl > 0 ? max : min;
2294 }
2295 if (nt == FLOAT) {
2296 nv->v_ldbl = (float)v->v_ldbl;
2297 } else if (nt == DOUBLE) {
2298 nv->v_ldbl = (double)v->v_ldbl;
2299 } else if (nt == LDOUBLE) {
2300 nv->v_ldbl = v->v_ldbl;
2301 } else {
2302 nv->v_quad = (nt == PTR || is_uinteger(nt)) ?
2303 (int64_t)v->v_ldbl : (int64_t)v->v_ldbl;
2304 }
2305 } else {
2306 if (nt == FLOAT) {
2307 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2308 (float)(uint64_t)v->v_quad : (float)v->v_quad;
2309 } else if (nt == DOUBLE) {
2310 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2311 (double)(uint64_t)v->v_quad : (double)v->v_quad;
2312 } else if (nt == LDOUBLE) {
2313 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2314 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
2315 } else {
2316 rchk = 1; /* Check for lost precision. */
2317 nv->v_quad = v->v_quad;
2318 }
2319 }
2320
2321 if (v->v_ansiu && is_floating(nt)) {
2322 /* ANSI C treats constant as unsigned */
2323 warning(157);
2324 v->v_ansiu = 0;
2325 } else if (v->v_ansiu && (is_integer(nt) && !is_uinteger(nt) &&
2326 psize(nt) > psize(ot))) {
2327 /* ANSI C treats constant as unsigned */
2328 warning(157);
2329 v->v_ansiu = 0;
2330 }
2331
2332 switch (nt) {
2333 case FLOAT:
2334 case FCOMPLEX:
2335 case DOUBLE:
2336 case DCOMPLEX:
2337 case LDOUBLE:
2338 case LCOMPLEX:
2339 break;
2340 default:
2341 sz = tp->t_bitfield ? tp->t_flen : size(nt);
2342 nv->v_quad = xsign(nv->v_quad, nt, sz);
2343 break;
2344 }
2345
2346 if (rchk && op != CVT) {
2347 osz = size(ot);
2348 nsz = tp->t_bitfield ? tp->t_flen : size(nt);
2349 xmask = qlmasks[nsz] ^ qlmasks[osz];
2350 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
2351 /*
2352 * For bitwise operations we are not interested in the
2353 * value, but in the bits itself.
2354 */
2355 if (op == ORASS || op == OR || op == XOR) {
2356 /*
2357 * Print a warning if bits which were set are
2358 * lost due to the conversion.
2359 * This can happen with operator ORASS only.
2360 */
2361 if (nsz < osz && (v->v_quad & xmask) != 0) {
2362 /* constant truncated by conv., op %s */
2363 warning(306, modtab[op].m_name);
2364 }
2365 } else if (op == ANDASS || op == AND) {
2366 /*
2367 * Print a warning if additional bits are not all 1
2368 * and the most significant bit of the old value is 1,
2369 * or if at least one (but not all) removed bit was 0.
2370 */
2371 if (nsz > osz &&
2372 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2373 (nv->v_quad & xmask) != xmask) {
2374 /* extra bits set to 0 in conv. of '%s' ... */
2375 warning(309, type_name(gettyp(ot)),
2376 type_name(tp), modtab[op].m_name);
2377 } else if (nsz < osz &&
2378 (v->v_quad & xmask) != xmask &&
2379 (v->v_quad & xmask) != 0) {
2380 /* constant truncated by conv., op %s */
2381 warning(306, modtab[op].m_name);
2382 }
2383 } else if ((nt != PTR && is_uinteger(nt)) &&
2384 (ot != PTR && !is_uinteger(ot)) &&
2385 v->v_quad < 0) {
2386 if (op == ASSIGN) {
2387 /* assignment of negative constant to ... */
2388 warning(164);
2389 } else if (op == INIT) {
2390 /* initialisation of unsigned with neg... */
2391 warning(221);
2392 } else if (op == FARG) {
2393 /* conversion of negative constant to ... */
2394 warning(296, arg);
2395 } else if (modtab[op].m_comp) {
2396 /* handled by check_integer_comparison() */
2397 } else {
2398 /* conversion of negative constant to ... */
2399 warning(222);
2400 }
2401 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2402 (v->v_quad & xmask) != 0 &&
2403 (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2404 /*
2405 * Loss of significant bit(s). All truncated bits
2406 * of unsigned types or all truncated bits plus the
2407 * msb of the target for signed types are considered
2408 * to be significant bits. Loss of significant bits
2409 * means that at least on of the bits was set in an
2410 * unsigned type or that at least one, but not all of
2411 * the bits was set in an signed type.
2412 * Loss of significant bits means that it is not
2413 * possible, also not with necessary casts, to convert
2414 * back to the original type. A example for a
2415 * necessary cast is:
2416 * char c; int i; c = 128;
2417 * i = c; ** yields -128 **
2418 * i = (unsigned char)c; ** yields 128 **
2419 */
2420 if (op == ASSIGN && tp->t_bitfield) {
2421 /* precision lost in bit-field assignment */
2422 warning(166);
2423 } else if (op == ASSIGN) {
2424 /* constant truncated by assignment */
2425 warning(165);
2426 } else if (op == INIT && tp->t_bitfield) {
2427 /* bit-field initializer does not fit */
2428 warning(180);
2429 } else if (op == INIT) {
2430 /* initializer does not fit */
2431 warning(178);
2432 } else if (op == CASE) {
2433 /* case label affected by conversion */
2434 warning(196);
2435 } else if (op == FARG) {
2436 /* conv. of '%s' to '%s' is out of range, ... */
2437 warning(295,
2438 type_name(gettyp(ot)), type_name(tp), arg);
2439 } else {
2440 /* conversion of '%s' to '%s' is out of range */
2441 warning(119,
2442 type_name(gettyp(ot)), type_name(tp));
2443 }
2444 } else if (nv->v_quad != v->v_quad) {
2445 if (op == ASSIGN && tp->t_bitfield) {
2446 /* precision lost in bit-field assignment */
2447 warning(166);
2448 } else if (op == INIT && tp->t_bitfield) {
2449 /* bit-field initializer out of range */
2450 warning(11);
2451 } else if (op == CASE) {
2452 /* case label affected by conversion */
2453 warning(196);
2454 } else if (op == FARG) {
2455 /* conv. of '%s' to '%s' is out of range, ... */
2456 warning(295,
2457 type_name(gettyp(ot)), type_name(tp), arg);
2458 } else {
2459 /* conversion of '%s' to '%s' is out of range */
2460 warning(119,
2461 type_name(gettyp(ot)), type_name(tp));
2462 }
2463 }
2464 }
2465 }
2466
2467 /*
2468 * Called if incompatible types were detected.
2469 * Prints a appropriate warning.
2470 */
2471 static void
2472 warn_incompatible_types(op_t op, tspec_t lt, tspec_t rt)
2473 {
2474 mod_t *mp;
2475
2476 mp = &modtab[op];
2477
2478 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2479 /* void type illegal in expression */
2480 error(109);
2481 } else if (op == ASSIGN) {
2482 if ((lt == STRUCT || lt == UNION) &&
2483 (rt == STRUCT || rt == UNION)) {
2484 /* assignment of different structures (%s != %s) */
2485 error(240, tspec_name(lt), tspec_name(rt));
2486 } else {
2487 /* assignment type mismatch (%s != %s) */
2488 error(171, tspec_name(lt), tspec_name(rt));
2489 }
2490 } else if (mp->m_binary) {
2491 /* operands of '%s' have incompatible types (%s != %s) */
2492 error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
2493 } else {
2494 lint_assert(rt == NOTSPEC);
2495 /* operand of '%s' has invalid type (%s) */
2496 error(108, mp->m_name, tspec_name(lt));
2497 }
2498 }
2499
2500 /*
2501 * Called if incompatible pointer types are detected.
2502 * Print an appropriate warning.
2503 */
2504 static void
2505 warn_incompatible_pointers(const mod_t *mp,
2506 const type_t *ltp, const type_t *rtp)
2507 {
2508 tspec_t lt, rt;
2509
2510 lint_assert(ltp->t_tspec == PTR);
2511 lint_assert(rtp->t_tspec == PTR);
2512
2513 lt = ltp->t_subt->t_tspec;
2514 rt = rtp->t_subt->t_tspec;
2515
2516 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2517 if (mp == NULL) {
2518 /* illegal structure pointer combination */
2519 warning(244);
2520 } else {
2521 /* illegal structure pointer combination, op %s */
2522 warning(245, mp->m_name);
2523 }
2524 } else {
2525 if (mp == NULL) {
2526 /* illegal pointer combination */
2527 warning(184);
2528 } else {
2529 /* illegal pointer combination (%s) and (%s), op %s */
2530 warning(124,
2531 type_name(ltp), type_name(rtp), mp->m_name);
2532 }
2533 }
2534 }
2535
2536 /*
2537 * Make sure type (*tpp)->t_subt has at least the qualifiers
2538 * of tp1->t_subt and tp2->t_subt.
2539 */
2540 static void
2541 merge_qualifiers(type_t **tpp, type_t *tp1, type_t *tp2)
2542 {
2543
2544 lint_assert((*tpp)->t_tspec == PTR);
2545 lint_assert(tp1->t_tspec == PTR);
2546 lint_assert(tp2->t_tspec == PTR);
2547
2548 if ((*tpp)->t_subt->t_const ==
2549 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2550 (*tpp)->t_subt->t_volatile ==
2551 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2552 return;
2553 }
2554
2555 *tpp = tduptyp(*tpp);
2556 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2557 (*tpp)->t_subt->t_const =
2558 tp1->t_subt->t_const | tp2->t_subt->t_const;
2559 (*tpp)->t_subt->t_volatile =
2560 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2561 }
2562
2563 /*
2564 * Returns 1 if the given structure or union has a constant member
2565 * (maybe recursively).
2566 */
2567 static bool
2568 has_constant_member(const type_t *tp)
2569 {
2570 sym_t *m;
2571 tspec_t t;
2572
2573 lint_assert((t = tp->t_tspec) == STRUCT || t == UNION);
2574
2575 for (m = tp->t_str->memb; m != NULL; m = m->s_next) {
2576 tp = m->s_type;
2577 if (tp->t_const)
2578 return true;
2579 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2580 if (has_constant_member(m->s_type))
2581 return true;
2582 }
2583 }
2584 return false;
2585 }
2586
2587 /*
2588 * Create a new node for one of the operators POINT and ARROW.
2589 */
2590 static tnode_t *
2591 build_struct_access(op_t op, tnode_t *ln, tnode_t *rn)
2592 {
2593 tnode_t *ntn, *ctn;
2594 int nolval;
2595
2596 lint_assert(rn->tn_op == NAME);
2597 lint_assert(rn->tn_sym->s_value.v_tspec == INT);
2598 lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU);
2599
2600 /*
2601 * Remember if the left operand is an lvalue (structure members
2602 * are lvalues if and only if the structure itself is an lvalue).
2603 */
2604 nolval = op == POINT && !ln->tn_lvalue;
2605
2606 if (op == POINT) {
2607 ln = build_ampersand(ln, 1);
2608 } else if (ln->tn_type->t_tspec != PTR) {
2609 lint_assert(tflag);
2610 lint_assert(is_integer(ln->tn_type->t_tspec));
2611 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2612 }
2613
2614 #if PTRDIFF_IS_LONG
2615 ctn = new_integer_constant_node(LONG,
2616 rn->tn_sym->s_value.v_quad / CHAR_SIZE);
2617 #else
2618 ctn = new_integer_constant_node(INT,
2619 rn->tn_sym->s_value.v_quad / CHAR_SIZE);
2620 #endif
2621
2622 ntn = new_tnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2623 if (ln->tn_op == CON)
2624 ntn = fold(ntn);
2625
2626 if (rn->tn_type->t_bitfield) {
2627 ntn = new_tnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2628 } else {
2629 ntn = new_tnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2630 }
2631
2632 if (nolval)
2633 ntn->tn_lvalue = 0;
2634
2635 return ntn;
2636 }
2637
2638 /*
2639 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2640 */
2641 static tnode_t *
2642 build_prepost_incdec(op_t op, tnode_t *ln)
2643 {
2644 tnode_t *cn, *ntn;
2645
2646 lint_assert(ln != NULL);
2647
2648 if (ln->tn_type->t_tspec == PTR) {
2649 cn = plength(ln->tn_type);
2650 } else {
2651 cn = new_integer_constant_node(INT, (int64_t)1);
2652 }
2653 ntn = new_tnode(op, ln->tn_type, ln, cn);
2654
2655 return ntn;
2656 }
2657
2658 /*
2659 * Create a node for REAL, IMAG
2660 */
2661 static tnode_t *
2662 build_real_imag(op_t op, tnode_t *ln)
2663 {
2664 tnode_t *cn, *ntn;
2665
2666 lint_assert(ln != NULL);
2667
2668 switch (ln->tn_type->t_tspec) {
2669 case LCOMPLEX:
2670 cn = new_integer_constant_node(LDOUBLE, (int64_t)1);
2671 break;
2672 case DCOMPLEX:
2673 cn = new_integer_constant_node(DOUBLE, (int64_t)1);
2674 break;
2675 case FCOMPLEX:
2676 cn = new_integer_constant_node(FLOAT, (int64_t)1);
2677 break;
2678 default:
2679 /* __%s__ is illegal for type %s */
2680 error(276, op == REAL ? "real" : "imag",
2681 type_name(ln->tn_type));
2682 return NULL;
2683 }
2684 ntn = new_tnode(op, cn->tn_type, ln, cn);
2685 ntn->tn_lvalue = 1;
2686
2687 return ntn;
2688 }
2689 /*
2690 * Create a tree node for the & operator
2691 */
2692 static tnode_t *
2693 build_ampersand(tnode_t *tn, int noign)
2694 {
2695 tnode_t *ntn;
2696 tspec_t t;
2697
2698 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2699 if (tflag)
2700 /* '&' before array or function: ignored */
2701 warning(127);
2702 return tn;
2703 }
2704
2705 /* eliminate &* */
2706 if (tn->tn_op == STAR &&
2707 tn->tn_left->tn_type->t_tspec == PTR &&
2708 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2709 return tn->tn_left;
2710 }
2711
2712 ntn = new_tnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2713
2714 return ntn;
2715 }
2716
2717 /*
2718 * Create a node for operators PLUS and MINUS.
2719 */
2720 static tnode_t *
2721 build_plus_minus(op_t op, tnode_t *ln, tnode_t *rn)
2722 {
2723 tnode_t *ntn, *ctn;
2724 type_t *tp;
2725
2726 /* If pointer and integer, then pointer to the lhs. */
2727 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
2728 ntn = ln;
2729 ln = rn;
2730 rn = ntn;
2731 }
2732
2733 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2734
2735 lint_assert(is_integer(rn->tn_type->t_tspec));
2736
2737 ctn = plength(ln->tn_type);
2738 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2739 rn = convert(NOOP, 0, ctn->tn_type, rn);
2740 rn = new_tnode(MULT, rn->tn_type, rn, ctn);
2741 if (rn->tn_left->tn_op == CON)
2742 rn = fold(rn);
2743 ntn = new_tnode(op, ln->tn_type, ln, rn);
2744
2745 } else if (rn->tn_type->t_tspec == PTR) {
2746
2747 lint_assert(ln->tn_type->t_tspec == PTR);
2748 lint_assert(op == MINUS);
2749 #if PTRDIFF_IS_LONG
2750 tp = gettyp(LONG);
2751 #else
2752 tp = gettyp(INT);
2753 #endif
2754 ntn = new_tnode(op, tp, ln, rn);
2755 if (ln->tn_op == CON && rn->tn_op == CON)
2756 ntn = fold(ntn);
2757 ctn = plength(ln->tn_type);
2758 balance(NOOP, &ntn, &ctn);
2759 ntn = new_tnode(DIV, tp, ntn, ctn);
2760
2761 } else {
2762
2763 ntn = new_tnode(op, ln->tn_type, ln, rn);
2764
2765 }
2766 return ntn;
2767 }
2768
2769 /*
2770 * Create a node for operators SHL and SHR.
2771 */
2772 static tnode_t *
2773 build_bit_shift(op_t op, tnode_t *ln, tnode_t *rn)
2774 {
2775 tspec_t t;
2776 tnode_t *ntn;
2777
2778 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2779 rn = convert(CVT, 0, gettyp(INT), rn);
2780 ntn = new_tnode(op, ln->tn_type, ln, rn);
2781 return ntn;
2782 }
2783
2784 /*
2785 * Create a node for COLON.
2786 */
2787 static tnode_t *
2788 build_colon(tnode_t *ln, tnode_t *rn)
2789 {
2790 tspec_t lt, rt, pdt;
2791 type_t *rtp;
2792 tnode_t *ntn;
2793
2794 lt = ln->tn_type->t_tspec;
2795 rt = rn->tn_type->t_tspec;
2796 #if PTRDIFF_IS_LONG
2797 pdt = LONG;
2798 #else
2799 pdt = INT;
2800 #endif
2801
2802 /*
2803 * Arithmetic types are balanced, all other type combinations
2804 * still need to be handled.
2805 */
2806 if (is_arithmetic(lt) && is_arithmetic(rt)) {
2807 rtp = ln->tn_type;
2808 } else if (lt == VOID || rt == VOID) {
2809 rtp = gettyp(VOID);
2810 } else if (lt == STRUCT || lt == UNION) {
2811 /* Both types must be identical. */
2812 lint_assert(rt == STRUCT || rt == UNION);
2813 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2814 if (incompl(ln->tn_type)) {
2815 /* unknown operand size, op %s */
2816 error(138, modtab[COLON].m_name);
2817 return NULL;
2818 }
2819 rtp = ln->tn_type;
2820 } else if (lt == PTR && is_integer(rt)) {
2821 if (rt != pdt) {
2822 rn = convert(NOOP, 0, gettyp(pdt), rn);
2823 rt = pdt;
2824 }
2825 rtp = ln->tn_type;
2826 } else if (rt == PTR && is_integer(lt)) {
2827 if (lt != pdt) {
2828 ln = convert(NOOP, 0, gettyp(pdt), ln);
2829 lt = pdt;
2830 }
2831 rtp = rn->tn_type;
2832 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2833 lint_assert(rt == PTR);
2834 rtp = rn->tn_type;
2835 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2836 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2837 lint_assert(lt == PTR);
2838 rtp = ln->tn_type;
2839 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2840 } else {
2841 lint_assert(lt == PTR);
2842 lint_assert(rt == PTR);
2843 /*
2844 * XXX For now we simply take the left type. This is
2845 * probably wrong, if one type contains a function prototype
2846 * and the other one, at the same place, only an old style
2847 * declaration.
2848 */
2849 rtp = ln->tn_type;
2850 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2851 }
2852
2853 ntn = new_tnode(COLON, rtp, ln, rn);
2854
2855 return ntn;
2856 }
2857
2858 /*
2859 * Create a node for an assignment operator (both = and op= ).
2860 */
2861 static tnode_t *
2862 build_assignment(op_t op, tnode_t *ln, tnode_t *rn)
2863 {
2864 tspec_t lt, rt;
2865 tnode_t *ntn, *ctn;
2866
2867 lint_assert(ln != NULL);
2868 lint_assert(rn != NULL);
2869
2870 lt = ln->tn_type->t_tspec;
2871 rt = rn->tn_type->t_tspec;
2872
2873 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2874 lint_assert(is_integer(rt));
2875 ctn = plength(ln->tn_type);
2876 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2877 rn = convert(NOOP, 0, ctn->tn_type, rn);
2878 rn = new_tnode(MULT, rn->tn_type, rn, ctn);
2879 if (rn->tn_left->tn_op == CON)
2880 rn = fold(rn);
2881 }
2882
2883 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2884 lint_assert(lt == rt);
2885 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2886 if (incompl(ln->tn_type)) {
2887 if (op == RETURN) {
2888 /* cannot return incomplete type */
2889 error(212);
2890 } else {
2891 /* unknown operand size, op %s */
2892 error(138, modtab[op].m_name);
2893 }
2894 return NULL;
2895 }
2896 }
2897
2898 if (op == SHLASS) {
2899 if (psize(lt) < psize(rt)) {
2900 if (hflag)
2901 /* semantics of '%s' change in ANSI C; ... */
2902 warning(118, "<<=");
2903 }
2904 } else if (op != SHRASS) {
2905 if (op == ASSIGN || lt != PTR) {
2906 if (lt != rt ||
2907 (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
2908 rn = convert(op, 0, ln->tn_type, rn);
2909 rt = lt;
2910 }
2911 }
2912 }
2913
2914 ntn = new_tnode(op, ln->tn_type, ln, rn);
2915
2916 return ntn;
2917 }
2918
2919 /*
2920 * Get length of type tp->t_subt.
2921 */
2922 static tnode_t *
2923 plength(type_t *tp)
2924 {
2925 int elem, elsz;
2926 tspec_t st;
2927
2928 lint_assert(tp->t_tspec == PTR);
2929 tp = tp->t_subt;
2930
2931 elem = 1;
2932 elsz = 0;
2933
2934 while (tp->t_tspec == ARRAY) {
2935 elem *= tp->t_dim;
2936 tp = tp->t_subt;
2937 }
2938
2939 switch (tp->t_tspec) {
2940 case FUNC:
2941 /* pointer to function is not allowed here */
2942 error(110);
2943 break;
2944 case VOID:
2945 /* cannot do pointer arithmetic on operand of ... */
2946 gnuism(136);
2947 break;
2948 case STRUCT:
2949 case UNION:
2950 if ((elsz = tp->t_str->size) == 0)
2951 /* cannot do pointer arithmetic on operand of ... */
2952 error(136);
2953 break;
2954 case ENUM:
2955 if (incompl(tp)) {
2956 /* cannot do pointer arithmetic on operand of ... */
2957 warning(136);
2958 }
2959 /* FALLTHROUGH */
2960 default:
2961 if ((elsz = size(tp->t_tspec)) == 0) {
2962 /* cannot do pointer arithmetic on operand of ... */
2963 error(136);
2964 } else {
2965 lint_assert(elsz != -1);
2966 }
2967 break;
2968 }
2969
2970 if (elem == 0 && elsz != 0) {
2971 /* cannot do pointer arithmetic on operand of ... */
2972 error(136);
2973 }
2974
2975 if (elsz == 0)
2976 elsz = CHAR_SIZE;
2977
2978 #if PTRDIFF_IS_LONG
2979 st = LONG;
2980 #else
2981 st = INT;
2982 #endif
2983
2984 return new_integer_constant_node(st, (int64_t)(elem * elsz / CHAR_SIZE));
2985 }
2986
2987 /*
2988 * XXX
2989 * Note: There appear to be a number of bugs in detecting overflow in
2990 * this function. An audit and a set of proper regression tests are needed.
2991 * --Perry Metzger, Nov. 16, 2001
2992 */
2993 /*
2994 * Do only as much as necessary to compute constant expressions.
2995 * Called only if the operator allows folding and (both) operands
2996 * are constants.
2997 */
2998 static tnode_t *
2999 fold(tnode_t *tn)
3000 {
3001 val_t *v;
3002 tspec_t t;
3003 int utyp, ovfl;
3004 int64_t sl, sr = 0, q = 0, mask;
3005 uint64_t ul, ur = 0;
3006 tnode_t *cn;
3007
3008 v = xcalloc(1, sizeof (val_t));
3009 v->v_tspec = t = tn->tn_type->t_tspec;
3010
3011 utyp = t == PTR || is_uinteger(t);
3012 ul = sl = tn->tn_left->tn_val->v_quad;
3013 if (modtab[tn->tn_op].m_binary)
3014 ur = sr = tn->tn_right->tn_val->v_quad;
3015
3016 mask = qlmasks[size(t)];
3017 ovfl = 0;
3018
3019 switch (tn->tn_op) {
3020 case UPLUS:
3021 q = sl;
3022 break;
3023 case UMINUS:
3024 q = -sl;
3025 if (sl != 0 && msb(q, t, -1) == msb(sl, t, -1))
3026 ovfl = 1;
3027 break;
3028 case COMPL:
3029 q = ~sl;
3030 break;
3031 case MULT:
3032 if (utyp) {
3033 q = ul * ur;
3034 if (q != (q & mask))
3035 ovfl = 1;
3036 else if ((ul != 0) && ((q / ul) != ur))
3037 ovfl = 1;
3038 } else {
3039 q = sl * sr;
3040 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
3041 ovfl = 1;
3042 }
3043 break;
3044 case DIV:
3045 if (sr == 0) {
3046 /* division by 0 */
3047 error(139);
3048 q = utyp ? UQUAD_MAX : QUAD_MAX;
3049 } else {
3050 q = utyp ? (int64_t)(ul / ur) : sl / sr;
3051 }
3052 break;
3053 case MOD:
3054 if (sr == 0) {
3055 /* modulus by 0 */
3056 error(140);
3057 q = 0;
3058 } else {
3059 q = utyp ? (int64_t)(ul % ur) : sl % sr;
3060 }
3061 break;
3062 case PLUS:
3063 q = utyp ? (int64_t)(ul + ur) : sl + sr;
3064 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
3065 if (msb(q, t, -1) == 0)
3066 ovfl = 1;
3067 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
3068 if (msb(q, t, -1) != 0)
3069 ovfl = 1;
3070 }
3071 break;
3072 case MINUS:
3073 q = utyp ? (int64_t)(ul - ur) : sl - sr;
3074 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
3075 if (msb(q, t, -1) == 0)
3076 ovfl = 1;
3077 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
3078 if (msb(q, t, -1) != 0)
3079 ovfl = 1;
3080 }
3081 break;
3082 case SHL:
3083 q = utyp ? (int64_t)(ul << sr) : sl << sr;
3084 break;
3085 case SHR:
3086 /*
3087 * The sign must be explicitly extended because
3088 * shifts of signed values are implementation dependent.
3089 */
3090 q = ul >> sr;
3091 q = xsign(q, t, size(t) - (int)sr);
3092 break;
3093 case LT:
3094 q = utyp ? ul < ur : sl < sr;
3095 break;
3096 case LE:
3097 q = utyp ? ul <= ur : sl <= sr;
3098 break;
3099 case GE:
3100 q = utyp ? ul >= ur : sl >= sr;
3101 break;
3102 case GT:
3103 q = utyp ? ul > ur : sl > sr;
3104 break;
3105 case EQ:
3106 q = utyp ? ul == ur : sl == sr;
3107 break;
3108 case NE:
3109 q = utyp ? ul != ur : sl != sr;
3110 break;
3111 case AND:
3112 q = utyp ? (int64_t)(ul & ur) : sl & sr;
3113 break;
3114 case XOR:
3115 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
3116 break;
3117 case OR:
3118 q = utyp ? (int64_t)(ul | ur) : sl | sr;
3119 break;
3120 default:
3121 lint_assert(/*CONSTCOND*/0);
3122 }
3123
3124 /* XXX does not work for quads. */
3125 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 &&
3126 (q & ~mask) != 0)) {
3127 if (hflag)
3128 /* integer overflow detected, op %s */
3129 warning(141, modtab[tn->tn_op].m_name);
3130 }
3131
3132 v->v_quad = xsign(q, t, -1);
3133
3134 cn = new_constant_node(tn->tn_type, v);
3135
3136 return cn;
3137 }
3138
3139 /*
3140 * Same for operators whose operands are compared with 0 (test context).
3141 */
3142 static tnode_t *
3143 fold_test(tnode_t *tn)
3144 {
3145 int l, r = 0;
3146 val_t *v;
3147
3148 v = xcalloc(1, sizeof (val_t));
3149 v->v_tspec = tn->tn_type->t_tspec;
3150 lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
3151
3152 if (is_floating(tn->tn_left->tn_type->t_tspec)) {
3153 l = tn->tn_left->tn_val->v_ldbl != 0.0;
3154 } else {
3155 l = tn->tn_left->tn_val->v_quad != 0;
3156 }
3157
3158 if (modtab[tn->tn_op].m_binary) {
3159 if (is_floating(tn->tn_right->tn_type->t_tspec)) {
3160 r = tn->tn_right->tn_val->v_ldbl != 0.0;
3161 } else {
3162 r = tn->tn_right->tn_val->v_quad != 0;
3163 }
3164 }
3165
3166 switch (tn->tn_op) {
3167 case NOT:
3168 if (hflag && !constcond_flag)
3169 /* constant argument to NOT */
3170 warning(239);
3171 v->v_quad = !l;
3172 break;
3173 case LOGAND:
3174 v->v_quad = l && r;
3175 break;
3176 case LOGOR:
3177 v->v_quad = l || r;
3178 break;
3179 default:
3180 lint_assert(/*CONSTCOND*/0);
3181 }
3182
3183 return new_constant_node(tn->tn_type, v);
3184 }
3185
3186 /*
3187 * Same for operands with floating point type.
3188 */
3189 static tnode_t *
3190 fold_float(tnode_t *tn)
3191 {
3192 val_t *v;
3193 tspec_t t;
3194 ldbl_t l, r = 0;
3195
3196 fpe = 0;
3197 v = xcalloc(1, sizeof (val_t));
3198 v->v_tspec = t = tn->tn_type->t_tspec;
3199
3200 lint_assert(is_floating(t));
3201 lint_assert(t == tn->tn_left->tn_type->t_tspec);
3202 lint_assert(!modtab[tn->tn_op].m_binary ||
3203 t == tn->tn_right->tn_type->t_tspec);
3204
3205 l = tn->tn_left->tn_val->v_ldbl;
3206 if (modtab[tn->tn_op].m_binary)
3207 r = tn->tn_right->tn_val->v_ldbl;
3208
3209 switch (tn->tn_op) {
3210 case UPLUS:
3211 v->v_ldbl = l;
3212 break;
3213 case UMINUS:
3214 v->v_ldbl = -l;
3215 break;
3216 case MULT:
3217 v->v_ldbl = l * r;
3218 break;
3219 case DIV:
3220 if (r == 0.0) {
3221 /* division by 0 */
3222 error(139);
3223 if (t == FLOAT) {
3224 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
3225 } else if (t == DOUBLE) {
3226 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
3227 } else {
3228 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
3229 }
3230 } else {
3231 v->v_ldbl = l / r;
3232 }
3233 break;
3234 case PLUS:
3235 v->v_ldbl = l + r;
3236 break;
3237 case MINUS:
3238 v->v_ldbl = l - r;
3239 break;
3240 case LT:
3241 v->v_quad = l < r;
3242 break;
3243 case LE:
3244 v->v_quad = l <= r;
3245 break;
3246 case GE:
3247 v->v_quad = l >= r;
3248 break;
3249 case GT:
3250 v->v_quad = l > r;
3251 break;
3252 case EQ:
3253 v->v_quad = l == r;
3254 break;
3255 case NE:
3256 v->v_quad = l != r;
3257 break;
3258 default:
3259 lint_assert(/*CONSTCOND*/0);
3260 }
3261
3262 lint_assert(fpe || !isnan((double)v->v_ldbl));
3263 if (fpe || !finite((double)v->v_ldbl) ||
3264 (t == FLOAT &&
3265 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
3266 (t == DOUBLE &&
3267 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3268 /* floating point overflow detected, op %s */
3269 warning(142, modtab[tn->tn_op].m_name);
3270 if (t == FLOAT) {
3271 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3272 } else if (t == DOUBLE) {
3273 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3274 } else {
3275 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
3276 }
3277 fpe = 0;
3278 }
3279
3280 return new_constant_node(tn->tn_type, v);
3281 }
3282
3283
3284 /*
3285 * Create a constant node for sizeof.
3286 */
3287 tnode_t *
3288 build_sizeof(type_t *tp)
3289 {
3290 tspec_t st;
3291 #if SIZEOF_IS_ULONG
3292 st = ULONG;
3293 #else
3294 st = UINT;
3295 #endif
3296 return new_integer_constant_node(st, tsize(tp) / CHAR_SIZE);
3297 }
3298
3299 /*
3300 * Create a constant node for offsetof.
3301 */
3302 tnode_t *
3303 build_offsetof(type_t *tp, sym_t *sym)
3304 {
3305 tspec_t st;
3306 #if SIZEOF_IS_ULONG
3307 st = ULONG;
3308 #else
3309 st = UINT;
3310 #endif
3311 tspec_t t = tp->t_tspec;
3312 if (t != STRUCT && t != UNION)
3313 /* unacceptable operand of '%s' */
3314 error(111, "offsetof");
3315
3316 // XXX: wrong size, no checking for sym fixme
3317 return new_integer_constant_node(st, tsize(tp) / CHAR_SIZE);
3318 }
3319
3320 int64_t
3321 tsize(type_t *tp)
3322 {
3323 int elem, elsz, flex;
3324
3325 elem = 1;
3326 flex = 0;
3327 while (tp->t_tspec == ARRAY) {
3328 flex = 1; /* allow c99 flex arrays [] [0] */
3329 elem *= tp->t_dim;
3330 tp = tp->t_subt;
3331 }
3332 if (elem == 0) {
3333 if (!flex) {
3334 /* cannot take size/alignment of incomplete type */
3335 error(143);
3336 elem = 1;
3337 }
3338 }
3339 switch (tp->t_tspec) {
3340 case FUNC:
3341 /* cannot take size/alignment of function */
3342 error(144);
3343 elsz = 1;
3344 break;
3345 case STRUCT:
3346 case UNION:
3347 if (incompl(tp)) {
3348 /* cannot take size/alignment of incomplete type */
3349 error(143);
3350 elsz = 1;
3351 } else {
3352 elsz = tp->t_str->size;
3353 }
3354 break;
3355 case ENUM:
3356 if (incompl(tp)) {
3357 /* cannot take size/alignment of incomplete type */
3358 warning(143);
3359 }
3360 /* FALLTHROUGH */
3361 default:
3362 if (tp->t_bitfield) {
3363 /* cannot take size/alignment of bit-field */
3364 error(145);
3365 }
3366 if (tp->t_tspec == VOID) {
3367 /* cannot take size/alignment of void */
3368 error(146);
3369 elsz = 1;
3370 } else {
3371 elsz = size(tp->t_tspec);
3372 lint_assert(elsz > 0);
3373 }
3374 break;
3375 }
3376
3377 /* XXX: type conversion is too late */
3378 return (int64_t)(elem * elsz);
3379 }
3380
3381 /*
3382 */
3383 tnode_t *
3384 build_alignof(type_t *tp)
3385 {
3386 tspec_t st;
3387
3388 switch (tp->t_tspec) {
3389 case ARRAY:
3390 break;
3391
3392 case FUNC:
3393 /* cannot take size/alignment of function */
3394 error(144);
3395 return 0;
3396
3397 case STRUCT:
3398 case UNION:
3399 if (incompl(tp)) {
3400 /* cannot take size/alignment of incomplete type */
3401 error(143);
3402 return 0;
3403 }
3404 break;
3405 case ENUM:
3406 break;
3407 default:
3408 if (tp->t_bitfield) {
3409 /* cannot take size/alignment of bit-field */
3410 error(145);
3411 return 0;
3412 }
3413 if (tp->t_tspec == VOID) {
3414 /* cannot take size/alignment of void */
3415 error(146);
3416 return 0;
3417 }
3418 break;
3419 }
3420
3421 #if SIZEOF_IS_ULONG
3422 st = ULONG;
3423 #else
3424 st = UINT;
3425 #endif
3426
3427 return new_integer_constant_node(st, (int64_t)getbound(tp) / CHAR_SIZE);
3428 }
3429
3430 /*
3431 * Type casts.
3432 */
3433 tnode_t *
3434 cast(tnode_t *tn, type_t *tp)
3435 {
3436 tspec_t nt, ot;
3437
3438 if (tn == NULL)
3439 return NULL;
3440
3441 tn = cconv(tn);
3442
3443 nt = tp->t_tspec;
3444 ot = tn->tn_type->t_tspec;
3445
3446 if (nt == VOID) {
3447 /*
3448 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
3449 * But this seams really questionable.
3450 */
3451 } else if (nt == UNION) {
3452 sym_t *m;
3453 str_t *str = tp->t_str;
3454 if (!Sflag) {
3455 /* union cast is a C9X feature */
3456 error(328);
3457 return NULL;
3458 }
3459 for (m = str->memb; m != NULL; m = m->s_next) {
3460 if (sametype(m->s_type, tn->tn_type)) {
3461 tn = getnode();
3462 tn->tn_op = CVT;
3463 tn->tn_type = tp;
3464 tn->tn_cast = 1;
3465 tn->tn_right = NULL;
3466 return tn;
3467 }
3468 }
3469 /* type '%s' is not a member of '%s' */
3470 error(329, type_name(tn->tn_type), type_name(tp));
3471 return NULL;
3472 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
3473 if (!Sflag || nt == ARRAY || nt == FUNC) {
3474 /* invalid cast expression */
3475 error(147);
3476 return NULL;
3477 }
3478 } else if (ot == STRUCT || ot == UNION) {
3479 /* invalid cast expression */
3480 error(147);
3481 return NULL;
3482 } else if (ot == VOID) {
3483 /* improper cast of void expression */
3484 error(148);
3485 return NULL;
3486 } else if (is_integer(nt) && is_scalar(ot)) {
3487 /* ok */
3488 } else if (is_floating(nt) && is_arithmetic(ot)) {
3489 /* ok */
3490 } else if (nt == PTR && is_integer(ot)) {
3491 /* ok */
3492 } else if (nt == PTR && ot == PTR) {
3493 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3494 if (hflag)
3495 /* cast discards 'const' from ... */
3496 warning(275);
3497 }
3498 } else {
3499 /* invalid cast expression */
3500 error(147);
3501 return NULL;
3502 }
3503
3504 tn = convert(CVT, 0, tp, tn);
3505 tn->tn_cast = 1;
3506
3507 return tn;
3508 }
3509
3510 /*
3511 * Create the node for a function argument.
3512 * All necessary conversions and type checks are done in
3513 * new_function_call_node because new_function_argument_node has no
3514 * information about expected argument types.
3515 */
3516 tnode_t *
3517 new_function_argument_node(tnode_t *args, tnode_t *arg)
3518 {
3519 tnode_t *ntn;
3520
3521 /*
3522 * If there was a serious error in the expression for the argument,
3523 * create a dummy argument so the positions of the remaining arguments
3524 * will not change.
3525 */
3526 if (arg == NULL)
3527 arg = new_integer_constant_node(INT, (int64_t)0);
3528
3529 ntn = new_tnode(PUSH, arg->tn_type, arg, args);
3530
3531 return ntn;
3532 }
3533
3534 /*
3535 * Create the node for a function call. Also check types of
3536 * function arguments and insert conversions, if necessary.
3537 */
3538 tnode_t *
3539 new_function_call_node(tnode_t *func, tnode_t *args)
3540 {
3541 tnode_t *ntn;
3542 op_t fcop;
3543
3544 if (func == NULL)
3545 return NULL;
3546
3547 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3548 fcop = CALL;
3549 } else {
3550 fcop = ICALL;
3551 }
3552
3553 /*
3554 * after cconv() func will always be a pointer to a function
3555 * if it is a valid function designator.
3556 */
3557 func = cconv(func);
3558
3559 if (func->tn_type->t_tspec != PTR ||
3560 func->tn_type->t_subt->t_tspec != FUNC) {
3561 /* illegal function (type %s) */
3562 error(149, type_name(func->tn_type));
3563 return NULL;
3564 }
3565
3566 args = check_function_arguments(func->tn_type->t_subt, args);
3567
3568 ntn = new_tnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3569
3570 return ntn;
3571 }
3572
3573 /*
3574 * Check types of all function arguments and insert conversions,
3575 * if necessary.
3576 */
3577 static tnode_t *
3578 check_function_arguments(type_t *ftp, tnode_t *args)
3579 {
3580 tnode_t *arg;
3581 sym_t *asym;
3582 tspec_t at;
3583 int narg, npar, n, i;
3584
3585 /* get # of args in the prototype */
3586 npar = 0;
3587 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
3588 npar++;
3589
3590 /* get # of args in function call */
3591 narg = 0;
3592 for (arg = args; arg != NULL; arg = arg->tn_right)
3593 narg++;
3594
3595 asym = ftp->t_args;
3596 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3597 /* argument mismatch: %d arg%s passed, %d expected */
3598 error(150, narg, narg > 1 ? "s" : "", npar);
3599 asym = NULL;
3600 }
3601
3602 for (n = 1; n <= narg; n++) {
3603
3604 /*
3605 * The rightmost argument is at the top of the argument
3606 * subtree.
3607 */
3608 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3609 continue;
3610
3611 /* some things which are always not allowed */
3612 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3613 /* void expressions may not be arguments, arg #%d */
3614 error(151, n);
3615 return NULL;
3616 } else if ((at == STRUCT || at == UNION) &&
3617 incompl(arg->tn_left->tn_type)) {
3618 /* argument cannot have unknown size, arg #%d */
3619 error(152, n);
3620 return NULL;
3621 } else if (is_integer(at) &&
3622 arg->tn_left->tn_type->t_isenum &&
3623 incompl(arg->tn_left->tn_type)) {
3624 /* argument cannot have unknown size, arg #%d */
3625 warning(152, n);
3626 }
3627
3628 /* class conversions (arg in value context) */
3629 arg->tn_left = cconv(arg->tn_left);
3630
3631 if (asym != NULL) {
3632 arg->tn_left = check_prototype_argument(
3633 n, asym->s_type, arg->tn_left);
3634 } else {
3635 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3636 }
3637 arg->tn_type = arg->tn_left->tn_type;
3638
3639 if (asym != NULL)
3640 asym = asym->s_next;
3641 }
3642
3643 return args;
3644 }
3645
3646 /*
3647 * Compare the type of an argument with the corresponding type of a
3648 * prototype parameter. If it is a valid combination, but both types
3649 * are not the same, insert a conversion to convert the argument into
3650 * the type of the parameter.
3651 */
3652 static tnode_t *
3653 check_prototype_argument(
3654 int n, /* pos of arg */
3655 type_t *tp, /* expected type (from prototype) */
3656 tnode_t *tn) /* argument */
3657 {
3658 tnode_t *ln;
3659 int dowarn;
3660
3661 ln = xcalloc(1, sizeof (tnode_t));
3662 ln->tn_type = tduptyp(tp);
3663 ln->tn_type->t_const = 0;
3664 ln->tn_lvalue = 1;
3665 if (typeok(FARG, n, ln, tn)) {
3666 if (!eqtype(tp, tn->tn_type, 1, 0, (dowarn = 0, &dowarn)) || dowarn)
3667 tn = convert(FARG, n, tp, tn);
3668 }
3669 free(ln);
3670 return tn;
3671 }
3672
3673 /*
3674 * Return the value of an integral constant expression.
3675 * If the expression is not constant or its type is not an integer
3676 * type, an error message is printed.
3677 */
3678 val_t *
3679 constant(tnode_t *tn, int required)
3680 {
3681 val_t *v;
3682
3683 if (tn != NULL)
3684 tn = cconv(tn);
3685 if (tn != NULL)
3686 tn = promote(NOOP, 0, tn);
3687
3688 v = xcalloc(1, sizeof (val_t));
3689
3690 if (tn == NULL) {
3691 lint_assert(nerr != 0);
3692 if (dflag)
3693 printf("constant node is null; returning 1 instead\n");
3694 v->v_tspec = INT;
3695 v->v_quad = 1;
3696 return v;
3697 }
3698
3699 v->v_tspec = tn->tn_type->t_tspec;
3700
3701 if (tn->tn_op == CON) {
3702 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
3703 if (is_integer(tn->tn_val->v_tspec)) {
3704 v->v_ansiu = tn->tn_val->v_ansiu;
3705 v->v_quad = tn->tn_val->v_quad;
3706 return v;
3707 }
3708 v->v_quad = tn->tn_val->v_ldbl;
3709 } else {
3710 v->v_quad = 1;
3711 }
3712
3713 if (required)
3714 /* integral constant expression expected */
3715 error(55);
3716 else
3717 /* variable array dimension is a C99/GCC extension */
3718 c99ism(318);
3719
3720 if (!is_integer(v->v_tspec))
3721 v->v_tspec = INT;
3722
3723 return v;
3724 }
3725
3726 /*
3727 * Perform some tests on expressions which can't be done in build() and
3728 * functions called by build(). These tests must be done here because
3729 * we need some information about the context in which the operations
3730 * are performed.
3731 * After all tests are performed, expr() frees the memory which is used
3732 * for the expression.
3733 */
3734 void
3735 expr(tnode_t *tn, int vctx, int tctx, int dofreeblk)
3736 {
3737
3738 lint_assert(tn != NULL || nerr != 0);
3739
3740 if (tn == NULL) {
3741 tfreeblk();
3742 return;
3743 }
3744
3745 /* expr() is also called in global initialisations */
3746 if (dcs->d_ctx != EXTERN)
3747 check_statement_reachable();
3748
3749 check_expr_misc(tn, vctx, tctx, !tctx, 0, 0, 0);
3750 if (tn->tn_op == ASSIGN) {
3751 if (hflag && tctx)
3752 /* assignment in conditional context */
3753 warning(159);
3754 } else if (tn->tn_op == CON) {
3755 if (hflag && tctx && !constcond_flag)
3756 /* constant in conditional context */
3757 warning(161);
3758 }
3759 if (!modtab[tn->tn_op].m_sideeff) {
3760 /*
3761 * for left operands of COMMA this warning is already
3762 * printed
3763 */
3764 if (tn->tn_op != COMMA && !vctx && !tctx)
3765 check_null_effect(tn);
3766 }
3767 if (dflag)
3768 display_expression(tn, 0);
3769
3770 /* free the tree memory */
3771 if (dofreeblk)
3772 tfreeblk();
3773 }
3774
3775 static void
3776 check_null_effect(const tnode_t *tn)
3777 {
3778
3779 if (!hflag)
3780 return;
3781
3782 while (!modtab[tn->tn_op].m_sideeff) {
3783 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3784 tn = tn->tn_left;
3785 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3786 /*
3787 * && and || have a side effect if the right operand
3788 * has a side effect.
3789 */
3790 tn = tn->tn_right;
3791 } else if (tn->tn_op == QUEST) {
3792 /*
3793 * ? has a side effect if at least one of its right
3794 * operands has a side effect
3795 */
3796 tn = tn->tn_right;
3797 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3798 /*
3799 * : has a side effect if at least one of its operands
3800 * has a side effect
3801 */
3802 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3803 tn = tn->tn_left;
3804 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3805 tn = tn->tn_right;
3806 } else {
3807 break;
3808 }
3809 } else {
3810 break;
3811 }
3812 }
3813 if (!modtab[tn->tn_op].m_sideeff)
3814 /* expression has null effect */
3815 warning(129);
3816 }
3817
3818 /*
3819 * Dump an expression to stdout
3820 * only used for debugging
3821 */
3822 static void
3823 display_expression(const tnode_t *tn, int offs)
3824 {
3825 uint64_t uq;
3826
3827 if (tn == NULL) {
3828 (void)printf("%*s%s\n", offs, "", "NULL");
3829 return;
3830 }
3831 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3832
3833 if (tn->tn_op == NAME) {
3834 (void)printf("%s: %s ",
3835 tn->tn_sym->s_name,
3836 storage_class_name(tn->tn_sym->s_scl));
3837 } else if (tn->tn_op == CON && is_floating(tn->tn_type->t_tspec)) {
3838 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3839 } else if (tn->tn_op == CON && is_integer(tn->tn_type->t_tspec)) {
3840 uq = tn->tn_val->v_quad;
3841 (void)printf("0x %08lx %08lx ",
3842 (long)(uq >> 32) & 0xffffffffl,
3843 (long)uq & 0xffffffffl);
3844 } else if (tn->tn_op == CON) {
3845 lint_assert(tn->tn_type->t_tspec == PTR);
3846 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3847 (u_long)tn->tn_val->v_quad);
3848 } else if (tn->tn_op == STRING) {
3849 if (tn->tn_string->st_tspec == CHAR) {
3850 (void)printf("\"%s\"", tn->tn_string->st_cp);
3851 } else {
3852 char *s;
3853 size_t n;
3854 n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
3855 s = xmalloc(n);
3856 (void)wcstombs(s, tn->tn_string->st_wcp, n);
3857 (void)printf("L\"%s\"", s);
3858 free(s);
3859 }
3860 (void)printf(" ");
3861 } else if (tn->tn_op == FSEL) {
3862 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3863 tn->tn_type->t_flen);
3864 }
3865 (void)printf("%s\n", ttos(tn->tn_type));
3866 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3867 return;
3868 display_expression(tn->tn_left, offs + 2);
3869 if (modtab[tn->tn_op].m_binary ||
3870 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3871 display_expression(tn->tn_right, offs + 2);
3872 }
3873 }
3874
3875 /*
3876 * Called by expr() to recursively perform some tests.
3877 */
3878 /* ARGSUSED */
3879 void
3880 check_expr_misc(const tnode_t *tn, int vctx, int tctx,
3881 int eqwarn, int fcall, int rvdisc, int szof)
3882 {
3883 tnode_t *ln, *rn;
3884 mod_t *mp;
3885 int nrvdisc, cvctx, ctctx;
3886 op_t op;
3887 scl_t sc;
3888 dinfo_t *di;
3889
3890 if (tn == NULL)
3891 return;
3892
3893 ln = tn->tn_left;
3894 rn = tn->tn_right;
3895 mp = &modtab[op = tn->tn_op];
3896
3897 switch (op) {
3898 case AMPER:
3899 if (ln->tn_op == NAME && (reached || rchflg)) {
3900 if (!szof)
3901 mark_as_set(ln->tn_sym);
3902 mark_as_used(ln->tn_sym, fcall, szof);
3903 }
3904 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3905 /* check the range of array indices */
3906 check_array_index(ln->tn_left, 1);
3907 break;
3908 case LOAD:
3909 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3910 /* check the range of array indices */
3911 check_array_index(ln->tn_left, 0);
3912 /* FALLTHROUGH */
3913 case PUSH:
3914 case INCBEF:
3915 case DECBEF:
3916 case INCAFT:
3917 case DECAFT:
3918 case ADDASS:
3919 case SUBASS:
3920 case MULASS:
3921 case DIVASS:
3922 case MODASS:
3923 case ANDASS:
3924 case ORASS:
3925 case XORASS:
3926 case SHLASS:
3927 case SHRASS:
3928 case REAL:
3929 case IMAG:
3930 if (ln->tn_op == NAME && (reached || rchflg)) {
3931 sc = ln->tn_sym->s_scl;
3932 /*
3933 * Look if there was a asm statement in one of the
3934 * compound statements we are in. If not, we don't
3935 * print a warning.
3936 */
3937 for (di = dcs; di != NULL; di = di->d_next) {
3938 if (di->d_asm)
3939 break;
3940 }
3941 if (sc != EXTERN && sc != STATIC &&
3942 !ln->tn_sym->s_set && !szof && di == NULL) {
3943 /* %s may be used before set */
3944 warning(158, ln->tn_sym->s_name);
3945 mark_as_set(ln->tn_sym);
3946 }
3947 mark_as_used(ln->tn_sym, 0, 0);
3948 }
3949 break;
3950 case ASSIGN:
3951 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3952 mark_as_set(ln->tn_sym);
3953 if (ln->tn_sym->s_scl == EXTERN)
3954 outusg(ln->tn_sym);
3955 }
3956 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3957 /* check the range of array indices */
3958 check_array_index(ln->tn_left, 0);
3959 break;
3960 case CALL:
3961 lint_assert(ln->tn_op == AMPER);
3962 lint_assert(ln->tn_left->tn_op == NAME);
3963 if (!szof)
3964 outcall(tn, vctx || tctx, rvdisc);
3965 break;
3966 case EQ:
3967 if (hflag && eqwarn)
3968 /* operator '==' found where '=' was expected */
3969 warning(160);
3970 break;
3971 case CON:
3972 case NAME:
3973 case STRING:
3974 return;
3975 /* LINTED206: (enumeration values not handled in switch) */
3976 case OR:
3977 case XOR:
3978 case NE:
3979 case GE:
3980 case GT:
3981 case LE:
3982 case LT:
3983 case SHR:
3984 case SHL:
3985 case MINUS:
3986 case PLUS:
3987 case MOD:
3988 case DIV:
3989 case MULT:
3990 case STAR:
3991 case UMINUS:
3992 case UPLUS:
3993 case DEC:
3994 case INC:
3995 case COMPL:
3996 case NOT:
3997 case POINT:
3998 case ARROW:
3999 case NOOP:
4000 case AND:
4001 case FARG:
4002 case CASE:
4003 case INIT:
4004 case RETURN:
4005 case ICALL:
4006 case CVT:
4007 case COMMA:
4008 case FSEL:
4009 case COLON:
4010 case QUEST:
4011 case LOGOR:
4012 case LOGAND:
4013 break;
4014 }
4015
4016 cvctx = mp->m_vctx;
4017 ctctx = mp->m_tctx;
4018 /*
4019 * values of operands of ':' are not used if the type of at least
4020 * one of the operands (for gcc compatibility) is void
4021 * XXX test/value context of QUEST should probably be used as
4022 * context for both operands of COLON
4023 */
4024 if (op == COLON && tn->tn_type->t_tspec == VOID)
4025 cvctx = ctctx = 0;
4026 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
4027 check_expr_misc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
4028
4029 switch (op) {
4030 case PUSH:
4031 if (rn != NULL)
4032 check_expr_misc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
4033 break;
4034 case LOGAND:
4035 case LOGOR:
4036 check_expr_misc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
4037 break;
4038 case COLON:
4039 check_expr_misc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
4040 break;
4041 case COMMA:
4042 check_expr_misc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
4043 break;
4044 default:
4045 if (mp->m_binary)
4046 check_expr_misc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
4047 break;
4048 }
4049
4050 }
4051
4052 /*
4053 * Checks the range of array indices, if possible.
4054 * amper is set if only the address of the element is used. This
4055 * means that the index is allowed to refer to the first element
4056 * after the array.
4057 */
4058 static void
4059 check_array_index(tnode_t *tn, int amper)
4060 {
4061 int dim;
4062 tnode_t *ln, *rn;
4063 int elsz;
4064 int64_t con;
4065
4066 ln = tn->tn_left;
4067 rn = tn->tn_right;
4068
4069 /* We can only check constant indices. */
4070 if (rn->tn_op != CON)
4071 return;
4072
4073 /* Return if the left node does not stem from an array. */
4074 if (ln->tn_op != AMPER)
4075 return;
4076 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
4077 return;
4078 if (ln->tn_left->tn_type->t_tspec != ARRAY)
4079 return;
4080
4081 /*
4082 * For incomplete array types, we can print a warning only if
4083 * the index is negative.
4084 */
4085 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
4086 return;
4087
4088 /* Get the size of one array element */
4089 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
4090 return;
4091 elsz /= CHAR_SIZE;
4092
4093 /* Change the unit of the index from bytes to element size. */
4094 if (is_uinteger(rn->tn_type->t_tspec)) {
4095 con = (uint64_t)rn->tn_val->v_quad / elsz;
4096 } else {
4097 con = rn->tn_val->v_quad / elsz;
4098 }
4099
4100 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
4101
4102 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
4103 /* array subscript cannot be negative: %ld */
4104 warning(167, (long)con);
4105 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
4106 /* array subscript cannot be > %d: %ld */
4107 warning(168, dim - 1, (long)con);
4108 }
4109 }
4110
4111 /*
4112 * Check for ordered comparisons of unsigned values with 0.
4113 */
4114 static void
4115 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
4116 {
4117 tspec_t lt, rt;
4118 mod_t *mp;
4119
4120 lt = ln->tn_type->t_tspec;
4121 rt = rn->tn_type->t_tspec;
4122 mp = &modtab[op];
4123
4124 if (ln->tn_op != CON && rn->tn_op != CON)
4125 return;
4126
4127 if (!is_integer(lt) || !is_integer(rt))
4128 return;
4129
4130 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
4131 (rn->tn_val->v_quad < 0 ||
4132 rn->tn_val->v_quad > (int)~(~0U << (CHAR_SIZE - 1)))) {
4133 /* nonportable character comparison, op %s */
4134 warning(230, mp->m_name);
4135 return;
4136 }
4137 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
4138 (ln->tn_val->v_quad < 0 ||
4139 ln->tn_val->v_quad > (int)~(~0U << (CHAR_SIZE - 1)))) {
4140 /* nonportable character comparison, op %s */
4141 warning(230, mp->m_name);
4142 return;
4143 }
4144 if (is_uinteger(lt) && !is_uinteger(rt) &&
4145 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
4146 if (rn->tn_val->v_quad < 0) {
4147 /* comparison of %s with %s, op %s */
4148 warning(162, type_name(ln->tn_type),
4149 "negative constant", mp->m_name);
4150 } else if (op == LT || op == GE || (hflag && op == LE)) {
4151 /* comparison of %s with %s, op %s */
4152 warning(162, type_name(ln->tn_type), "0", mp->m_name);
4153 }
4154 return;
4155 }
4156 if (is_uinteger(rt) && !is_uinteger(lt) &&
4157 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
4158 if (ln->tn_val->v_quad < 0) {
4159 /* comparison of %s with %s, op %s */
4160 warning(162, "negative constant",
4161 type_name(rn->tn_type), mp->m_name);
4162 } else if (op == GT || op == LE || (hflag && op == GE)) {
4163 /* comparison of %s with %s, op %s */
4164 warning(162, "0", type_name(rn->tn_type), mp->m_name);
4165 }
4166 return;
4167 }
4168 }
4169
4170 /*
4171 * Takes an expression an returns 0 if this expression can be used
4172 * for static initialisation, otherwise -1.
4173 *
4174 * Constant initialisation expressions must be constant or an address
4175 * of a static object with an optional offset. In the first case,
4176 * the result is returned in *offsp. In the second case, the static
4177 * object is returned in *symp and the offset in *offsp.
4178 *
4179 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
4180 * CON. Type conversions are allowed if they do not change binary
4181 * representation (including width).
4182 */
4183 int
4184 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
4185 {
4186 sym_t *sym;
4187 ptrdiff_t offs1, offs2;
4188 tspec_t t, ot;
4189
4190 switch (tn->tn_op) {
4191 case MINUS:
4192 if (tn->tn_right->tn_op == CVT)
4193 return conaddr(tn->tn_right, symp, offsp);
4194 else if (tn->tn_right->tn_op != CON)
4195 return -1;
4196 /* FALLTHROUGH */
4197 case PLUS:
4198 offs1 = offs2 = 0;
4199 if (tn->tn_left->tn_op == CON) {
4200 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
4201 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
4202 return -1;
4203 } else if (tn->tn_right->tn_op == CON) {
4204 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
4205 if (tn->tn_op == MINUS)
4206 offs2 = -offs2;
4207 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
4208 return -1;
4209 } else {
4210 return -1;
4211 }
4212 *symp = sym;
4213 *offsp = offs1 + offs2;
4214 break;
4215 case AMPER:
4216 if (tn->tn_left->tn_op == NAME) {
4217 *symp = tn->tn_left->tn_sym;
4218 *offsp = 0;
4219 } else if (tn->tn_left->tn_op == STRING) {
4220 /*
4221 * If this would be the front end of a compiler we
4222 * would return a label instead of 0.
4223 */
4224 *offsp = 0;
4225 }
4226 break;
4227 case CVT:
4228 t = tn->tn_type->t_tspec;
4229 ot = tn->tn_left->tn_type->t_tspec;
4230 if ((!is_integer(t) && t != PTR) ||
4231 (!is_integer(ot) && ot != PTR)) {
4232 return -1;
4233 }
4234 #ifdef notdef
4235 /*
4236 * consider:
4237 * struct foo {
4238 * unsigned char a;
4239 * } f = {
4240 * (u_char)(u_long)(&(((struct foo *)0)->a))
4241 * };
4242 * since psize(u_long) != psize(u_char) this fails.
4243 */
4244 else if (psize(t) != psize(ot))
4245 return -1;
4246 #endif
4247 if (conaddr(tn->tn_left, symp, offsp) == -1)
4248 return -1;
4249 break;
4250 default:
4251 return -1;
4252 }
4253 return 0;
4254 }
4255
4256 /*
4257 * Concatenate two string constants.
4258 */
4259 strg_t *
4260 cat_strings(strg_t *strg1, strg_t *strg2)
4261 {
4262 size_t len1, len2, len;
4263
4264 if (strg1->st_tspec != strg2->st_tspec) {
4265 /* cannot concatenate wide and regular string literals */
4266 error(292);
4267 return strg1;
4268 }
4269
4270 len1 = strg1->st_len;
4271 len2 = strg2->st_len + 1; /* + NUL */
4272 len = len1 + len2;
4273
4274 #define COPY(F) \
4275 do { \
4276 strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \
4277 (void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \
4278 free(strg2->F); \
4279 } while (/*CONSTCOND*/0)
4280
4281 if (strg1->st_tspec == CHAR)
4282 COPY(st_cp);
4283 else
4284 COPY(st_wcp);
4285
4286 strg1->st_len = len - 1; /* - NUL */
4287 free(strg2);
4288
4289 return strg1;
4290 }
4291
4292 static bool
4293 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
4294 {
4295
4296 if (op == SHL || op == SHR) {
4297 if (!lparen && (lop == PLUS || lop == MINUS))
4298 return true;
4299 if (!rparen && (rop == PLUS || rop == MINUS))
4300 return true;
4301 return false;
4302 }
4303
4304 if (op == LOGOR) {
4305 if (!lparen && lop == LOGAND)
4306 return true;
4307 if (!rparen && rop == LOGAND)
4308 return true;
4309 return false;
4310 }
4311
4312 lint_assert(op == AND || op == XOR || op == OR);
4313 if (!lparen && lop != op) {
4314 if (lop == PLUS || lop == MINUS)
4315 return true;
4316 if (lop == AND || lop == XOR)
4317 return true;
4318 }
4319 if (!rparen && rop != op) {
4320 if (rop == PLUS || rop == MINUS)
4321 return true;
4322 if (rop == AND || rop == XOR)
4323 return true;
4324 }
4325 return false;
4326 }
4327
4328 /*
4329 * Print a warning if the given node has operands which should be
4330 * parenthesized.
4331 *
4332 * XXX Does not work if an operand is a constant expression. Constant
4333 * expressions are already folded.
4334 */
4335 static void
4336 check_precedence_confusion(tnode_t *tn)
4337 {
4338 tnode_t *ln, *rn;
4339
4340 if (!hflag)
4341 return;
4342
4343 dprint_node(tn);
4344
4345 lint_assert(modtab[tn->tn_op].m_binary);
4346 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4347 continue;
4348 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4349 continue;
4350
4351 if (is_confusing_precedence(tn->tn_op,
4352 ln->tn_op, ln->tn_parenthesized,
4353 rn->tn_op, rn->tn_parenthesized)) {
4354 /* precedence confusion possible: parenthesize! */
4355 warning(169);
4356 }
4357 }
4358